-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initialize translate with history tests (#687)
- Add `@history insert` command to insert chat history entries. - Add the first translation with history test - Type validation: Improve error message with union type mismatch
- Loading branch information
Showing
15 changed files
with
372 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
import { SchemaType } from "./type.js"; | ||
|
||
export function toStringSchemaType( | ||
type: SchemaType, | ||
paran: boolean = false, | ||
): string { | ||
let result: string; | ||
switch (type.type) { | ||
case "string": | ||
return "string"; | ||
case "number": | ||
return "number"; | ||
case "boolean": | ||
return "boolean"; | ||
case "undefined": | ||
return "undefined"; | ||
case "object": | ||
return `{ ${Object.entries(type.fields) | ||
.map(([name, field]) => { | ||
return `${name}${field.optional ? "?" : ""}: ${toStringSchemaType(field.type)}`; | ||
}) | ||
.join(", ")}}`; | ||
case "array": | ||
return `${toStringSchemaType(type.elementType, true)}[]`; | ||
case "type-reference": | ||
return type.definition ? type.definition.name : "unknown"; | ||
case "string-union": | ||
result = type.typeEnum.join(" | "); | ||
paran = paran && type.typeEnum.length > 1; | ||
break; | ||
case "type-union": | ||
result = type.types.map((t) => toStringSchemaType(t)).join(" | "); | ||
paran = paran && type.types.length > 1; | ||
break; | ||
} | ||
return paran ? `(${result})` : result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
ts/packages/defaultAgentProvider/test/data/translate-history-e2e.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[ | ||
{ | ||
"request": "play that song again", | ||
"history": { | ||
"user": "play some random songs", | ||
"assistant": { "text": "Now playing: Soruwienf from album Wxifiel with artist Bnefisoe", "source": "player", "entities": [ | ||
{"name": "Soruwienf", "type": ["track", "song"], "uniqueId": "a"}, | ||
{"name": "Wxifiel", "type": ["album"], "uniqueId": "b"}, | ||
{"name": "Bnefisoe", "type": ["artist"], "uniqueId": "c"} | ||
]} | ||
}, | ||
"action": {"translatorName": "player","actionName": "playTrack", "parameters": {"trackName": "Soruwienf", "albumName": "Wxifiel", "artists": ["Bnefisoe"]}} | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,7 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
import dotenv from "dotenv"; | ||
dotenv.config({ path: new URL("../../../../.env", import.meta.url) }); | ||
|
||
import { getPackageFilePath } from "../src/utils/getPackageFilePath.js"; | ||
import { getDefaultAppAgentProviders } from "../src/defaultAgentProviders.js"; | ||
import fs from "node:fs"; | ||
import { createDispatcher, Dispatcher } from "agent-dispatcher"; | ||
|
||
import { defineTranslateTest } from "./translateTestCommon.js"; | ||
const dataFiles = ["test/data/translate-e2e.json"]; | ||
|
||
type TranslateTestRequest = { | ||
request: string; | ||
action: string | string[]; | ||
}; | ||
type TranslateTestEntry = TranslateTestRequest | TranslateTestRequest[]; | ||
type TranslateTestFile = TranslateTestEntry[]; | ||
|
||
const inputs: TranslateTestEntry[] = ( | ||
await Promise.all( | ||
dataFiles.map<Promise<TranslateTestFile>>(async (f) => { | ||
return JSON.parse( | ||
await fs.promises.readFile(getPackageFilePath(f), "utf-8"), | ||
); | ||
}), | ||
) | ||
).flat(); | ||
|
||
const repeat = 5; | ||
const defaultAppAgentProviders = getDefaultAppAgentProviders(undefined); | ||
|
||
describe("translation action stability", () => { | ||
let dispatchers: Dispatcher[]; | ||
beforeAll(async () => { | ||
const dispatcherP: Promise<Dispatcher>[] = []; | ||
for (let i = 0; i < repeat; i++) { | ||
dispatcherP.push( | ||
createDispatcher("cli test translate", { | ||
appAgentProviders: defaultAppAgentProviders, | ||
actions: null, | ||
commands: { dispatcher: true }, | ||
translation: { history: { enabled: false } }, | ||
explainer: { enabled: false }, | ||
cache: { enabled: false }, | ||
}), | ||
); | ||
} | ||
dispatchers = await Promise.all(dispatcherP); | ||
}); | ||
it.each(inputs)("translate '$request'", async (test) => { | ||
const requests = Array.isArray(test) ? test : [test]; | ||
await Promise.all( | ||
dispatchers.map(async (dispatcher) => { | ||
for (const { request, action } of requests) { | ||
const result = await dispatcher.processCommand(request); | ||
expect(result?.actions).toBeDefined(); | ||
|
||
const expected = | ||
typeof action === "string" ? [action] : action; | ||
expect(result?.actions).toHaveLength(expected.length); | ||
for (let i = 0; i < expected.length; i++) { | ||
expect( | ||
`${result?.actions?.[i].translatorName}.${result?.actions?.[i].actionName}`, | ||
).toBe(expected[i]); | ||
} | ||
} | ||
}), | ||
); | ||
}); | ||
afterAll(async () => { | ||
await Promise.all(dispatchers.map((d) => d.close())); | ||
dispatchers = []; | ||
}); | ||
}); | ||
await defineTranslateTest("translate (no history)", dataFiles); |
106 changes: 106 additions & 0 deletions
106
ts/packages/defaultAgentProvider/test/translateTestCommon.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
import dotenv from "dotenv"; | ||
dotenv.config({ path: new URL("../../../../.env", import.meta.url) }); | ||
|
||
import { getPackageFilePath } from "../src/utils/getPackageFilePath.js"; | ||
import { getDefaultAppAgentProviders } from "../src/defaultAgentProviders.js"; | ||
import fs from "node:fs"; | ||
import { createDispatcher, Dispatcher } from "agent-dispatcher"; | ||
import { ChatHistoryInput } from "agent-dispatcher/internal"; | ||
import { FullAction } from "agent-cache"; | ||
|
||
type TranslateTestRequest = { | ||
request: string; | ||
action: string | string[] | FullAction | FullAction[]; | ||
history?: ChatHistoryInput | ChatHistoryInput[]; | ||
match?: "exact" | "partial"; // default to "exact" | ||
}; | ||
type TranslateTestEntry = TranslateTestRequest | TranslateTestRequest[]; | ||
type TranslateTestFile = TranslateTestEntry[]; | ||
const repeat = 5; | ||
const defaultAppAgentProviders = getDefaultAppAgentProviders(undefined); | ||
|
||
export async function defineTranslateTest(name: string, dataFiles: string[]) { | ||
const inputs: TranslateTestEntry[] = ( | ||
await Promise.all( | ||
dataFiles.map<Promise<TranslateTestFile>>(async (f) => { | ||
return JSON.parse( | ||
await fs.promises.readFile(getPackageFilePath(f), "utf-8"), | ||
); | ||
}), | ||
) | ||
).flat(); | ||
|
||
describe(`${name} action stability`, () => { | ||
let dispatchers: Dispatcher[]; | ||
beforeAll(async () => { | ||
const dispatcherP: Promise<Dispatcher>[] = []; | ||
for (let i = 0; i < repeat; i++) { | ||
dispatcherP.push( | ||
createDispatcher("cli test translate", { | ||
appAgentProviders: defaultAppAgentProviders, | ||
actions: null, | ||
commands: { dispatcher: true }, | ||
translation: { history: { enabled: false } }, | ||
explainer: { enabled: false }, | ||
cache: { enabled: false }, | ||
}), | ||
); | ||
} | ||
dispatchers = await Promise.all(dispatcherP); | ||
}); | ||
it.each(inputs)(`${name} '$request'`, async (test) => { | ||
const requests = Array.isArray(test) ? test : [test]; | ||
await Promise.all( | ||
dispatchers.map(async (dispatcher) => { | ||
for (const { | ||
request, | ||
history, | ||
action, | ||
match, | ||
} of requests) { | ||
if (history !== undefined) { | ||
await dispatcher.processCommand( | ||
`@history insert ${JSON.stringify(history)}`, | ||
); | ||
} | ||
const result = await dispatcher.processCommand(request); | ||
const actions = result?.actions; | ||
expect(actions).toBeDefined(); | ||
|
||
const expectedValues = Array.isArray(action) | ||
? action | ||
: [action]; | ||
expect(actions).toHaveLength(expectedValues.length); | ||
for (let i = 0; i < expectedValues.length; i++) { | ||
const action = actions![i]; | ||
const expected = expectedValues[i]; | ||
if (typeof expected === "string") { | ||
const actualFullActionName = `${action.translatorName}.${action.actionName}`; | ||
if (match === "partial") { | ||
expect(actualFullActionName).toContain( | ||
expected, | ||
); | ||
} else { | ||
expect(actualFullActionName).toBe(expected); | ||
} | ||
} else { | ||
if (match === "partial") { | ||
expect(action).toMatchObject(expected); | ||
} else { | ||
expect(action).toEqual(expected); | ||
} | ||
} | ||
} | ||
} | ||
}), | ||
); | ||
}); | ||
afterAll(async () => { | ||
await Promise.all(dispatchers.map((d) => d.close())); | ||
dispatchers = []; | ||
}); | ||
}); | ||
} |
7 changes: 7 additions & 0 deletions
7
ts/packages/defaultAgentProvider/test/translate_history.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
import { defineTranslateTest } from "./translateTestCommon.js"; | ||
const dataFiles = ["test/data/translate-history-e2e.json"]; | ||
|
||
await defineTranslateTest("translate (w/history)", dataFiles); |
Oops, something went wrong.