Skip to content

Commit

Permalink
Switch Dispatcher command handler to use ActionContext (#145)
Browse files Browse the repository at this point in the history
- Command handlers get an ActionContext (instead of just the
CommandHandlerContext)
- Remove special `executDispatcherCommand` and use the standard helper
from the SDK.
- Switch most dispatcher command handlers to use `ActionIO`. Remove
`RequestIO` API that is no longer used.
- Add `block` append mode for `appendDisplay`
  • Loading branch information
curtisman authored Sep 14, 2024
1 parent 9afa30a commit 05569d2
Show file tree
Hide file tree
Showing 35 changed files with 740 additions and 536 deletions.
21 changes: 1 addition & 20 deletions ts/packages/agentSdk/src/agentInterface.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { ActionIO, DisplayType, DynamicDisplay } from "./display.js";
import { Profiler } from "./profiler.js";

//==============================================================================
Expand Down Expand Up @@ -40,13 +41,6 @@ export interface AppActionWithParameters extends AppAction {
parameters: { [key: string]: any };
}

export type DisplayType = "html" | "text";

export type DynamicDisplay = {
content: DisplayContent;
nextRefreshMs: number; // in milliseconds, -1 means no more refresh.
};

export type CommandDescriptor = {
description: string;
help?: string;
Expand Down Expand Up @@ -152,19 +146,6 @@ export interface Storage {
getTokenCachePersistence(): Promise<TokenCachePersistence>;
}

export type DisplayContent =
| string
| {
type: DisplayType;
content: string;
};

export interface ActionIO {
readonly type: DisplayType;
setDisplay(content: DisplayContent): void;
appendDisplay(content: DisplayContent): void;
}

export interface ActionContext<T = void> {
profiler?: Profiler | undefined;
streamingContext: unknown;
Expand Down
24 changes: 24 additions & 0 deletions ts/packages/agentSdk/src/display.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

export type DisplayType = "html" | "text";

export type DynamicDisplay = {
content: DisplayContent;
nextRefreshMs: number; // in milliseconds, -1 means no more refresh.
};

export type DisplayContent =
| string
| {
type: DisplayType;
content: string;
};

export type DisplayAppendMode = "inline" | "block" | "temporary";

export interface ActionIO {
readonly type: DisplayType;
setDisplay(content: DisplayContent): void;
appendDisplay(content: DisplayContent, mode?: DisplayAppendMode): void;
}
10 changes: 7 additions & 3 deletions ts/packages/agentSdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ export {
StorageEncoding,
TokenCachePersistence,
ActionContext,
CommandDescriptor,
CommandDescriptorTable,
} from "./agentInterface.js";

export {
ActionIO,
DisplayType,
DynamicDisplay,
DisplayContent,
CommandDescriptor,
CommandDescriptorTable,
} from "./agentInterface.js";
DisplayAppendMode,
} from "./display.js";
export * from "./memory.js";

export { Profiler } from "./profiler.js";
2 changes: 1 addition & 1 deletion ts/packages/agentSdk/src/memory.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { DisplayContent } from "./agentInterface.js";
import { DisplayContent } from "./display.js";

export interface Entity {
// the name of the entity such as "Bach" or "frog"
Expand Down
19 changes: 9 additions & 10 deletions ts/packages/cli/src/commands/run/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
// Licensed under the MIT License.

import { Args, Command, Flags } from "@oclif/core";
import { createDispatcher } from "agent-dispatcher";
import {
getCacheFactory,
initializeCommandHandlerContext,
RequestCommandHandler,
getBuiltinTranslatorNames,
} from "agent-dispatcher/internal";
import chalk from "chalk";
Expand Down Expand Up @@ -47,14 +46,14 @@ export default class RequestCommand extends Command {
const translators = flags.translator
? Object.fromEntries(flags.translator.map((name) => [name, true]))
: undefined;
const handler = new RequestCommandHandler();
await handler.run(
args.request,
await initializeCommandHandlerContext("cli run request", {
translators,
explainerName: flags.explainer,
cache: false,
}),
const dispatcher = await createDispatcher("cli run request", {
translators,
explainerName: flags.explainer,
cache: false,
});
await dispatcher.processCommand(
`@request ${args.request}`,
undefined,
this.loadAttachment(args.attachment),
);
}
Expand Down
23 changes: 9 additions & 14 deletions ts/packages/cli/src/commands/run/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
// Licensed under the MIT License.

import { Args, Command, Flags } from "@oclif/core";
import {
initializeCommandHandlerContext,
TranslateCommandHandler,
getBuiltinTranslatorNames,
} from "agent-dispatcher/internal";
import { createDispatcher } from "agent-dispatcher";
import { getBuiltinTranslatorNames } from "agent-dispatcher/internal";

export default class TranslateCommand extends Command {
static args = {
Expand All @@ -32,17 +29,15 @@ export default class TranslateCommand extends Command {

async run(): Promise<void> {
const { args, flags } = await this.parse(TranslateCommand);
const handler = new TranslateCommandHandler();
const translators = flags.translator
? Object.fromEntries(flags.translator.map((name) => [name, true]))
: undefined;
await handler.run(
args.request,
await initializeCommandHandlerContext("cli run translate", {
translators,
actions: {}, // We don't need any actions
cache: false,
}),
);

const dispatcher = await createDispatcher("cli run translate", {
translators,
actions: {}, // We don't need any actions
cache: false,
});
await dispatcher.processCommand(`@translate ${args.request}`);
}
}
13 changes: 11 additions & 2 deletions ts/packages/dispatcher/src/action/actionHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
DisplayType,
DisplayContent,
ActionContext,
DisplayAppendMode,
} from "@typeagent/agent-sdk";
import { MatchResult } from "agent-cache";
import { getStorage } from "./storageImpl.js";
Expand All @@ -40,8 +41,16 @@ function getActionContext(
setDisplay(content: DisplayContent): void {
context.requestIO.setDisplay(content, actionIndex, appAgentName);
},
appendDisplay(content: DisplayContent): void {
context.requestIO.appendDisplay(content, actionIndex, appAgentName);
appendDisplay(
content: DisplayContent,
mode: DisplayAppendMode = "inline",
): void {
context.requestIO.appendDisplay(
content,
actionIndex,
appAgentName,
mode,
);
},
};
return {
Expand Down
7 changes: 5 additions & 2 deletions ts/packages/dispatcher/src/agent/agentProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
TokenCachePersistence,
ActionIO,
DisplayType,
DisplayContent,
DisplayAppendMode,
AppAgentEvent,
} from "@typeagent/agent-sdk";

Expand Down Expand Up @@ -316,16 +318,17 @@ function getActionContextShim(
get type(): DisplayType {
return "text";
},
setDisplay(content: string): void {
setDisplay(content: DisplayContent): void {
rpc.send("setDisplay", {
actionContextId,
content,
});
},
appendDisplay(content: string): void {
appendDisplay(content: DisplayContent, mode?: DisplayAppendMode): void {
rpc.send("appendDisplay", {
actionContextId,
content,
mode,
});
},
};
Expand Down
4 changes: 3 additions & 1 deletion ts/packages/dispatcher/src/agent/agentProcessShim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
CommandDescriptor,
CommandDescriptorTable,
DisplayContent,
DisplayAppendMode,
} from "@typeagent/agent-sdk";
import {
AgentCallFunctions,
Expand Down Expand Up @@ -235,10 +236,11 @@ export async function createAgentProcessShim(
appendDisplay: (param: {
actionContextId: number;
message: DisplayContent;
mode: DisplayAppendMode;
}) => {
actionContextMap
.get(param.actionContextId)
.actionIO.appendDisplay(param.message);
.actionIO.appendDisplay(param.message, param.mode);
},
};

Expand Down
2 changes: 2 additions & 0 deletions ts/packages/dispatcher/src/agent/agentProcessTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import {
AppAgentEvent,
DisplayAppendMode,
DisplayContent,
DisplayType,
StorageListOptions,
Expand All @@ -22,6 +23,7 @@ export type AgentContextCallFunctions = {
appendDisplay: (param: {
actionContextId: number;
message: DisplayContent;
mode: DisplayAppendMode;
}) => void;
};

Expand Down
Loading

0 comments on commit 05569d2

Please sign in to comment.