Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ts/packages/agents/desktop/jest.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

module.exports = require("../../../jest.config.js");
8 changes: 7 additions & 1 deletion ts/packages/agents/desktop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@
"asc:taskbar": "asc -i ./src/windows/taskbarActionsSchema.ts -o ./dist/taskbarSchema.pas.json -t DesktopTaskbarActions",
"build": "concurrently npm:tsc npm:asc:all npm:agc:all",
"clean": "rimraf --glob dist *.tsbuildinfo *.done.build.log",
"jest-esm": "node --no-warnings --experimental-vm-modules ./node_modules/jest/bin/jest.js",
"prettier": "prettier --check . --ignore-path ../../../.prettierignore",
"prettier:fix": "prettier --write . --ignore-path ../../../.prettierignore",
"tsc": "tsc -p src"
"test": "npm run test:local",
"test:local": "pnpm run jest-esm --testPathPattern=\".*[.]spec[.]js\"",
"tsc": "tsc -b src test"
},
"dependencies": {
"@typeagent/agent-sdk": "workspace:*",
Expand All @@ -57,16 +60,19 @@
"ws": "^8.17.1"
},
"devDependencies": {
"@jest/globals": "^29.7.0",
"@typeagent/action-schema-compiler": "workspace:*",
"@types/body-parser": "^1.19.5",
"@types/cors": "^2.8.17",
"@types/debug": "^4.1.12",
"@types/express": "^4.17.17",
"@types/find-config": "1.0.4",
"@types/jest": "^29.5.7",
"@types/ws": "^8.5.10",
"action-grammar-compiler": "workspace:*",
"concurrently": "^9.1.2",
"copyfiles": "^2.4.1",
"jest": "^29.7.0",
"prettier": "^3.5.3",
"rimraf": "^6.0.1",
"typescript": "~5.4.5"
Expand Down
22 changes: 12 additions & 10 deletions ts/packages/agents/desktop/src/programNameIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
NormalizedEmbedding,
SimilarityType,
generateEmbedding,
generateEmbeddingWithRetry,
ScoredItem,
NameValue,
} from "typeagent";
Expand Down Expand Up @@ -48,18 +49,20 @@ export function loadProgramNameIndex(
export function createProgramNameIndex(
vals: Record<string, string | undefined>,
initialEmbeddings?: Record<string, Float32Array>,
modelOverride?: TextEmbeddingModel,
) {
let programEmbeddings: Record<string, NormalizedEmbedding> =
initialEmbeddings ?? {};
let embeddingModel: TextEmbeddingModel;
const configValues = vals;

const aiSettings = openai.apiSettingsFromEnv(
openai.ModelType.Embedding,
configValues,
);

embeddingModel = openai.createEmbeddingModel(aiSettings);
const embeddingModel: TextEmbeddingModel =
modelOverride ??
(() => {
const aiSettings = openai.apiSettingsFromEnv(
openai.ModelType.Embedding,
vals,
);
return openai.createEmbeddingModel(aiSettings);
})();

return {
addOrUpdate,
Expand All @@ -84,7 +87,7 @@ export function createProgramNameIndex(
return;
}
try {
const embedding = await generateEmbedding(
const embedding = await generateEmbeddingWithRetry(
embeddingModel,
programName,
);
Expand All @@ -93,7 +96,6 @@ export function createProgramNameIndex(
debugError(
`Could not create embedding for ${programName}. ${e.message}`,
);
// TODO: Retry with back-off for 429 responses
}
}

Expand Down
1 change: 1 addition & 0 deletions ts/packages/agents/desktop/src/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "../../../../tsconfig.base.json",
"compilerOptions": {
"composite": true,
"rootDir": ".",
"outDir": "../dist",
"types": ["node"]
Expand Down
95 changes: 95 additions & 0 deletions ts/packages/agents/desktop/test/programNameIndex.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { jest } from "@jest/globals";
import { createProgramNameIndex } from "../src/programNameIndex.js";
import { TextEmbeddingModel } from "aiclient";

/** Build a minimal TextEmbeddingModel stub. */
function makeModel(
fn: (input: string) => Promise<{ success: true; data: number[] }>,
): TextEmbeddingModel {
return { generateEmbedding: fn, maxBatchSize: 1 } as TextEmbeddingModel;
}

const successModel = makeModel(async () => ({
success: true,
data: [1, 0],
}));

describe("createProgramNameIndex", () => {
test("stores embedding for a new program", async () => {
let calls = 0;
const model = makeModel(async () => {
calls++;
return { success: true, data: [1, 0] };
});

const index = createProgramNameIndex({}, undefined, model);
await index.addOrUpdate("notepad");

expect(calls).toBe(1);
// toJSON returns a base64 string for the stored embedding
expect(index.toJSON()).toHaveProperty("notepad");
});

test("skips model call for already-cached program", async () => {
let calls = 0;
const model = makeModel(async () => {
calls++;
return { success: true, data: [1, 0] };
});

const index = createProgramNameIndex({}, undefined, model);
await index.addOrUpdate("notepad");
await index.addOrUpdate("notepad"); // second call: already cached

expect(calls).toBe(1);
});

test("swallows error and does not store entry when model always fails", async () => {
jest.useFakeTimers();
const failModel = makeModel(async () => {
throw new Error("simulated 429");
});

const index = createProgramNameIndex({}, undefined, failModel);
const promise = index.addOrUpdate("chrome");

// Advance past all retry delays (generateEmbeddingWithRetry defaults: 3 retries, 2500ms each with backoff)
await jest.runAllTimersAsync();
await promise;

expect(index.toJSON()).not.toHaveProperty("chrome");
jest.useRealTimers();
});

test("retries on transient failure and stores embedding on success", async () => {
jest.useFakeTimers();
let callCount = 0;
const transientModel = makeModel(async () => {
callCount++;
if (callCount === 1) throw new Error("transient 429");
return { success: true, data: [0.5, 0.5] };
});

const index = createProgramNameIndex({}, undefined, transientModel);
const promise = index.addOrUpdate("explorer");

// Advance past the first retry delay
await jest.runAllTimersAsync();
await promise;

expect(callCount).toBeGreaterThanOrEqual(2);
expect(index.toJSON()).toHaveProperty("explorer");
jest.useRealTimers();
});

test("reset clears all stored embeddings", async () => {
const index = createProgramNameIndex({}, undefined, successModel);
await index.addOrUpdate("notepad");
await index.reset();

expect(index.toJSON()).toEqual({});
});
});
14 changes: 14 additions & 0 deletions ts/packages/agents/desktop/test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "../../../../tsconfig.base.json",
"compilerOptions": {
"composite": true,
"rootDir": ".",
"outDir": "../dist/test",
"types": ["node", "jest"]
},
"include": ["./**/*"],
"ts-node": {
"esm": true
},
"references": [{ "path": "../src" }]
}
9 changes: 9 additions & 0 deletions ts/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading