Skip to content

Commit

Permalink
Added appendDiagnosticData() to clientIO interface (#784)
Browse files Browse the repository at this point in the history
  • Loading branch information
robgruen authored Mar 5, 2025
1 parent 4935a69 commit 50db864
Show file tree
Hide file tree
Showing 20 changed files with 84 additions and 31 deletions.
8 changes: 8 additions & 0 deletions ts/packages/agentRpc/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,14 @@ export async function createAgentRpcClient(
.get(param.actionContextId)
.actionIO.setDisplay(param.content);
},
appendDiagnosticData: (param: {
actionContextId: number;
data: any;
}) => {
actionContextMap
.get(param.actionContextId)
.actionIO.appendDiagnosticData(param.data);
},
appendDisplay: (param: {
actionContextId: number;
content: DisplayContent;
Expand Down
3 changes: 3 additions & 0 deletions ts/packages/agentRpc/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,9 @@ export function createAgentRpcServer(
content,
});
},
appendDiagnosticData(data): void {
rpc.send("appendDiagnosticData", { actionContextId, data });
},
appendDisplay(
content: DisplayContent,
mode: DisplayAppendMode,
Expand Down
4 changes: 4 additions & 0 deletions ts/packages/agentRpc/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export type AgentContextCallFunctions = {
action: ClientAction;
data?: unknown;
}) => void;
appendDiagnosticData: (param: {
actionContextId: number;
data: any;
}) => void;
};

export type AgentContextInvokeFunctions = {
Expand Down
4 changes: 4 additions & 0 deletions ts/packages/agentSdk/src/display.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,12 @@ export type ClientAction =
| "automate-phone-ui";

export interface ActionIO {
// Set the display to the content provided
setDisplay(content: DisplayContent): void;

// Send diagnostic information back to the client
appendDiagnosticData(data: any): void;

// Append content to the display, default mode is "inline"
appendDisplay(content: DisplayContent, mode?: DisplayAppendMode): void;

Expand Down
2 changes: 2 additions & 0 deletions ts/packages/agents/greeting/src/greetingCommandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ export class GreetingCommandHandler implements CommandHandlerNoParams {
const response = await this.getTypeChatResponse(context);

if (response.success) {
context.actionIO.appendDiagnosticData(response.data);

let action: GreetingAction = response.data as GreetingAction;
let result: ActionResultSuccess | undefined = undefined;
switch (action.actionName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
MultiSinkLogger,
createDebugLoggerSink,
createMongoDBLoggerSink,
Profiler,
} from "telemetry";
import { AgentCache } from "agent-cache";
import { randomUUID } from "crypto";
Expand All @@ -33,7 +34,6 @@ import {
lockInstanceDir,
} from "../utils/userData.js";
import { ActionContext, AppAgentEvent } from "@typeagent/agent-sdk";
import { Profiler } from "telemetry";
import { conversation as Conversation } from "knowledge-processor";
import {
AppAgentManager,
Expand Down
2 changes: 1 addition & 1 deletion ts/packages/dispatcher/src/command/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import chalk from "chalk";
import registerDebug from "debug";
import { RequestId, makeClientIOMessage } from "../context/interactiveIO.js";
import { makeClientIOMessage, RequestId } from "../context/interactiveIO.js";
import { getDefaultExplainerName } from "agent-cache";
import {
CommandHandlerContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { TypeAgentTranslator } from "../translation/agentTranslators.js";
import { ActionConfigProvider } from "../translation/actionConfigProvider.js";
import { getCacheFactory } from "../utils/cacheFactory.js";
import { createServiceHost } from "./system/handlers/serviceHost/serviceHostCommandHandler.js";
import { ClientIO, RequestId, nullClientIO } from "./interactiveIO.js";
import { ClientIO, nullClientIO, RequestId } from "./interactiveIO.js";
import { ChatHistory, createChatHistory } from "./chatHistory.js";

import {
Expand Down
2 changes: 2 additions & 0 deletions ts/packages/dispatcher/src/context/interactiveIO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export interface ClientIO {
): void;
setDisplay(message: IAgentMessage): void;
appendDisplay(message: IAgentMessage, mode: DisplayAppendMode): void;
appendDiagnosticData(requestId: RequestId, data: any): void;
setDynamicDisplay(
source: string,
requestId: RequestId,
Expand Down Expand Up @@ -135,6 +136,7 @@ export const nullClientIO: ClientIO = {
setDisplayInfo: () => {},
setDisplay: () => {},
appendDisplay: () => {},
appendDiagnosticData: () => {},
setDynamicDisplay: () => {},
askYesNo: async (
message: string,
Expand Down
3 changes: 3 additions & 0 deletions ts/packages/dispatcher/src/execute/actionHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ function getActionContext(
takeAction(action: string, data: unknown): void {
context.clientIO.takeAction(action, data);
},
appendDiagnosticData(data): void {
context.clientIO.appendDiagnosticData(requestId, data);
},
};
const actionContext: ActionContext<unknown> = {
streamingContext: undefined,
Expand Down
3 changes: 3 additions & 0 deletions ts/packages/dispatcher/src/helpers/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ function createConsoleClientIO(rl?: readline.promises.Interface): ClientIO {
appendDisplay(message: IAgentMessage, mode: DisplayAppendMode): void {
displayContent(message.message, mode);
},
appendDiagnosticData(_requestId: RequestId, _data: any) {
// Ignored
},
setDynamicDisplay(
source: string,
requestId: RequestId,
Expand Down
2 changes: 1 addition & 1 deletion ts/packages/dispatcher/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export type {
} from "./agentProvider/agentProvider.js";
export type {
ClientIO,
RequestId,
IAgentMessage,
NotifyExplainedData,
RequestId,
} from "./context/interactiveIO.js";
export type { Timing, PhaseTiming, RequestMetrics } from "./utils/metrics.js";
export type {
Expand Down
5 changes: 3 additions & 2 deletions ts/packages/dispatcher/src/rpc/clientIOClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ export function createClientIORpcClient(channel: RpcChannel): ClientIO {
exit(): void {
return rpc.send("exit", undefined);
},

// Display
setDisplayInfo(
source: string,
requestId: RequestId,
Expand All @@ -47,6 +45,9 @@ export function createClientIORpcClient(channel: RpcChannel): ClientIO {
appendDisplay(message: IAgentMessage, mode: DisplayAppendMode): void {
return rpc.send("appendDisplay", { message, mode });
},
appendDiagnosticData(requestId: RequestId, data: any) {
return rpc.send("appendDiagnosticData", { requestId, data });
},
setDynamicDisplay(
source: string,
requestId: RequestId,
Expand Down
3 changes: 3 additions & 0 deletions ts/packages/dispatcher/src/rpc/clientIOServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ export function createClientIORpcServer(
setDisplay: (params) => clientIO.setDisplay(params.message),
appendDisplay: (params) =>
clientIO.appendDisplay(params.message, params.mode),
appendDiagnosticData: (params) => {
clientIO.appendDiagnosticData(params.requestId, params.data);
},
setDynamicDisplay: (params) =>
clientIO.setDynamicDisplay(
params.source,
Expand Down
1 change: 1 addition & 0 deletions ts/packages/dispatcher/src/rpc/clientIOTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export type ClientIOCallFunctions = {
message: IAgentMessage;
mode: DisplayAppendMode;
}): void;
appendDiagnosticData(params: { requestId: RequestId; data: any }): void;
setDynamicDisplay(params: {
source: string;
requestId: RequestId;
Expand Down
4 changes: 4 additions & 0 deletions ts/packages/shell/src/renderer/src/chatView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,10 @@ export class ChatView {
);
}

setActionData(requestId: RequestId, data: any) {
this.getMessageGroup(requestId)?.setActionData(requestId, data);
}

addAgentMessage(
msg: IAgentMessage,
options?: {
Expand Down
3 changes: 3 additions & 0 deletions ts/packages/shell/src/renderer/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ function addEvents(
appendDisplay: (message, mode) => {
chatView.addAgentMessage(message, { appendMode: mode });
},
appendDiagnosticData: (requestId, data) => {
chatView.setActionData(requestId, data);
},
setDynamicDisplay: (
source,
requestId,
Expand Down
46 changes: 21 additions & 25 deletions ts/packages/shell/src/renderer/src/messageContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,10 @@ export class MessageContainer {

public setDisplayInfo(source: string, action?: TypeAgentAction | string[]) {
this.defaultSource = source;
this.action = action;
this.updateSource();
if (action !== undefined) {
this.updateActionData(action);
}
}

private get sourceLabel() {
Expand All @@ -115,10 +117,13 @@ export class MessageContainer {
if (this.action !== undefined) {
if (Array.isArray(this.action)) {
return `${this.source} ${this.action.join(" ")}`;
} else if (
(this.action as TypeAgentAction).translatorName !== undefined
) {
return `${this.action.translatorName}.${this.action.actionName}`;
}
return `${this.action.translatorName}.${this.action.actionName}`;
}
return this.source;
return this.defaultSource;
}

private get sourceIcon() {
Expand All @@ -138,19 +143,21 @@ export class MessageContainer {
label.innerText = sourceLabel;
}

if (this.action !== undefined && !Array.isArray(this.action)) {
label.setAttribute(
"action-data",
"<pre>" +
JSON.stringify(this.action, undefined, 2) +
"</pre>",
);
this.iconDiv.innerText = this.sourceIcon;
}
}

// mark the span as clickable
this.nameSpan.classList.add("clickable");
}
public updateActionData(data: any) {
this.action = data;
const label = this.timestampDiv.firstChild as HTMLSpanElement;
if (this.action !== undefined && !Array.isArray(this.action)) {
label.setAttribute(
"action-data",
"<pre>" + JSON.stringify(this.action, undefined, 2) + "</pre>",
);

this.iconDiv.innerText = this.sourceIcon;
// mark the span as clickable
this.nameSpan.classList.add("clickable");
}
}

Expand Down Expand Up @@ -190,17 +197,6 @@ export class MessageContainer {
this.nameSpan.className = "agent-name";
this.nameSpan.addEventListener("click", () => {
swapContent(this.nameSpan, this.messageDiv);
// const data: string = this.nameSpan.getAttribute("action-data") ?? "";
// const originalMessage: string = this.messageDiv.innerHTML;

// if (this.messageDiv.classList.contains("chat-message-action-data")) {
// this.messageDiv.classList.remove("chat-message-action-data");
// } else {
// this.messageDiv.classList.add("chat-message-action-data");
// }

// this.nameSpan.setAttribute("action-data", originalMessage);
// this.messageDiv.innerHTML = data;
});

const timestampDiv = this.createTimestampDiv(
Expand Down
11 changes: 11 additions & 0 deletions ts/packages/shell/src/renderer/src/messageGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
CommandResult,
IAgentMessage,
NotifyExplainedData,
RequestId,
} from "agent-dispatcher";
import { RequestMetrics } from "agent-dispatcher";

Expand Down Expand Up @@ -83,6 +84,16 @@ export class MessageGroup {
agentMessage.setDisplayInfo(source, action);
}

public setActionData(_requestId: RequestId, data: any) {
const agentMessage = this.ensureAgentMessage({
message: "",
source: "",
actionIndex: undefined,
});

agentMessage.updateActionData(data);
}

private requestCompleted(metrics: RequestMetrics | undefined) {
this.updateMetrics(metrics);
if (this.statusMessage === undefined) {
Expand Down
5 changes: 5 additions & 0 deletions ts/packages/shell/src/renderer/src/setContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,11 @@ export function swapContent(
const data: string = sourceElement.getAttribute("action-data") ?? "";
const originalMessage: string = targetElement.innerHTML;

// don't do anything if there's no data in the action-data attribute
if (data.length === 0) {
return;
}

if (targetElement.classList.contains("chat-message-action-data")) {
targetElement.classList.remove("chat-message-action-data");
} else {
Expand Down

0 comments on commit 50db864

Please sign in to comment.