Skip to content

Commit a984dec

Browse files
authored
Match command and completion for match and translate commands (#1432)
Add `@dispatcher match` command to go thru the match request code path only similar to `@dispatcher translate`. Reuse the same completion using the cache used by the request command handler for translate and match command.
1 parent ac9df7b commit a984dec

File tree

3 files changed

+109
-16
lines changed

3 files changed

+109
-16
lines changed

ts/packages/dispatcher/src/context/dispatcher/dispatcherAgent.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@ import {
3939
} from "../../translation/translateRequest.js";
4040
import { ActivityActions } from "./schema/activityActionSchema.js";
4141
import { ClarifyEntityAction } from "../../execute/pendingActions.js";
42+
import { MatchCommandHandler } from "./handlers/matchCommandHandler.js";
4243

4344
const dispatcherHandlers: CommandHandlerTable = {
4445
description: "Type Agent Dispatcher Commands",
4546
commands: {
4647
request: new RequestCommandHandler(),
48+
match: new MatchCommandHandler(),
4749
translate: new TranslateCommandHandler(),
4850
explain: new ExplainCommandHandler(),
4951
},
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
import { CommandHandlerContext } from "../../commandHandlerContext.js";
5+
import {
6+
ActionContext,
7+
CompletionGroup,
8+
ParsedCommandParams,
9+
SessionContext,
10+
} from "@typeagent/agent-sdk";
11+
import { CommandHandler } from "@typeagent/agent-sdk/helpers/command";
12+
import {
13+
displayError,
14+
displayResult,
15+
} from "@typeagent/agent-sdk/helpers/display";
16+
import { getColorElapsedString } from "common-utils";
17+
import { matchRequest } from "../../../translation/matchRequest.js";
18+
import { requestCompletion } from "../../../translation/requestCompletion.js";
19+
20+
export class MatchCommandHandler implements CommandHandler {
21+
public readonly description = "Match a request";
22+
public readonly parameters = {
23+
args: {
24+
request: {
25+
description: "Request to match",
26+
implicitQuotes: true,
27+
},
28+
},
29+
} as const;
30+
public async run(
31+
context: ActionContext<CommandHandlerContext>,
32+
params: ParsedCommandParams<typeof this.parameters>,
33+
) {
34+
const matchResult = await matchRequest(params.args.request, context);
35+
if (matchResult) {
36+
const elapsedStr = getColorElapsedString(matchResult.elapsedMs);
37+
38+
displayResult(
39+
`${matchResult.requestAction} ${elapsedStr}\n\nJSON:\n${JSON.stringify(
40+
matchResult.requestAction.actions,
41+
undefined,
42+
2,
43+
)}`,
44+
context,
45+
);
46+
} else {
47+
displayError("No match found for the request.", context);
48+
}
49+
}
50+
public async getCompletion(
51+
context: SessionContext<CommandHandlerContext>,
52+
params: ParsedCommandParams<typeof this.parameters>,
53+
names: string[],
54+
): Promise<CompletionGroup[]> {
55+
const completions: CompletionGroup[] = [];
56+
for (const name of names) {
57+
if (name === "request") {
58+
const requestPrefix = params.args.request;
59+
completions.push(
60+
...(await requestCompletion(
61+
requestPrefix,
62+
context.agentContext,
63+
)),
64+
);
65+
}
66+
}
67+
return completions;
68+
}
69+
}

ts/packages/dispatcher/src/context/dispatcher/handlers/translateCommandHandler.ts

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@
22
// Licensed under the MIT License.
33

44
import { CommandHandlerContext } from "../../commandHandlerContext.js";
5-
import { ActionContext, ParsedCommandParams } from "@typeagent/agent-sdk";
5+
import {
6+
ActionContext,
7+
CompletionGroup,
8+
ParsedCommandParams,
9+
SessionContext,
10+
} from "@typeagent/agent-sdk";
611
import { CommandHandler } from "@typeagent/agent-sdk/helpers/command";
712
import { displayResult } from "@typeagent/agent-sdk/helpers/display";
813
import { getColorElapsedString } from "common-utils";
914
import { translateRequest } from "../../../translation/translateRequest.js";
15+
import { requestCompletion } from "../../../translation/requestCompletion.js";
1016

1117
export class TranslateCommandHandler implements CommandHandler {
1218
public readonly description = "Translate a request";
@@ -26,21 +32,37 @@ export class TranslateCommandHandler implements CommandHandler {
2632
params.args.request,
2733
context,
2834
);
29-
if (translationResult) {
30-
const elapsedStr = getColorElapsedString(
31-
translationResult.elapsedMs,
32-
);
33-
const usageStr = translationResult.tokenUsage
34-
? `(Tokens: ${translationResult.tokenUsage.prompt_tokens} + ${translationResult.tokenUsage.completion_tokens} = ${translationResult.tokenUsage.total_tokens})`
35-
: "";
36-
displayResult(
37-
`${translationResult.requestAction} ${elapsedStr}${usageStr}\n\nJSON:\n${JSON.stringify(
38-
translationResult.requestAction.actions,
39-
undefined,
40-
2,
41-
)}`,
42-
context,
43-
);
35+
36+
const elapsedStr = getColorElapsedString(translationResult.elapsedMs);
37+
const usageStr = translationResult.tokenUsage
38+
? `(Tokens: ${translationResult.tokenUsage.prompt_tokens} + ${translationResult.tokenUsage.completion_tokens} = ${translationResult.tokenUsage.total_tokens})`
39+
: "";
40+
displayResult(
41+
`${translationResult.requestAction} ${elapsedStr}${usageStr}\n\nJSON:\n${JSON.stringify(
42+
translationResult.requestAction.actions,
43+
undefined,
44+
2,
45+
)}`,
46+
context,
47+
);
48+
}
49+
public async getCompletion(
50+
context: SessionContext<CommandHandlerContext>,
51+
params: ParsedCommandParams<typeof this.parameters>,
52+
names: string[],
53+
): Promise<CompletionGroup[]> {
54+
const completions: CompletionGroup[] = [];
55+
for (const name of names) {
56+
if (name === "request") {
57+
const requestPrefix = params.args.request;
58+
completions.push(
59+
...(await requestCompletion(
60+
requestPrefix,
61+
context.agentContext,
62+
)),
63+
);
64+
}
4465
}
66+
return completions;
4567
}
4668
}

0 commit comments

Comments
 (0)