Skip to content

Commit

Permalink
Unified NPM AppAgent Provider (#454)
Browse files Browse the repository at this point in the history
- Split inline agents (dispatcher/system) to be its own provider, so
that eventually "builtin" agents can live in a separate package.
- Unified built-in agents and external agent provider into npm agent
provider, which only different in the "require" resolve path.
- Add capability for npm agent provider to specify the path of the
package in the config, instead of having it install/deploy to the user
profile.
- Add test agent that does simple addition and add test that inject the
path of the agent using the npm agent provider.
  • Loading branch information
curtisman authored Dec 4, 2024
1 parent 25d6d33 commit 5563175
Show file tree
Hide file tree
Showing 34 changed files with 602 additions and 479 deletions.
14 changes: 5 additions & 9 deletions ts/packages/actionSchema/test/regen.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,11 @@ function addTest(name: string, config: Config, dir: string) {

const dispatcherRequire = createRequire(`${dispatcherPath}/src`);
for (const [name, entry] of Object.entries(config.agents) as [string, any][]) {
if (entry.type === "module") {
const manifestModulePath = `${entry.name}/agent/manifest`;
const manifestPath = dispatcherRequire.resolve(manifestModulePath);
const manifest = dispatcherRequire(manifestPath);
const manifestDir = path.dirname(manifestPath);
addTest(name, manifest, manifestDir);
} else {
addTest(name, entry, dispatcherPath);
}
const manifestModulePath = `${entry.name}/agent/manifest`;
const manifestPath = dispatcherRequire.resolve(manifestModulePath);
const manifest = dispatcherRequire(manifestPath);
const manifestDir = path.dirname(manifestPath);
addTest(name, manifest, manifestDir);
}

import prettier from "prettier";
Expand Down
21 changes: 21 additions & 0 deletions ts/packages/agents/test/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) Microsoft Corporation.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE
11 changes: 11 additions & 0 deletions ts/packages/agents/test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# test

Test dispatcher agent

## Trademarks

This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft
trademarks or logos is subject to and must follow
[Microsoft's Trademark & Brand Guidelines](https://www.microsoft.com/en-us/legal/intellectualproperty/trademarks/usage/general).
Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship.
Any use of third-party trademarks or logos are subject to those third-party's policies.
33 changes: 33 additions & 0 deletions ts/packages/agents/test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "test-agent",
"version": "0.0.1",
"description": "Dispatch agent for testing",
"homepage": "https://github.com/microsoft/TypeAgent#readme",
"repository": {
"type": "git",
"url": "https://github.com/microsoft/TypeAgent.git",
"directory": "ts/packages/agents/test"
},
"license": "MIT",
"author": "Microsoft",
"type": "module",
"exports": {
"./agent/manifest": "./src/manifest.json",
"./agent/handlers": "./dist/handler.js"
},
"scripts": {
"build": "npm run tsc",
"clean": "rimraf --glob dist *.tsbuildinfo *.done.build.log",
"prettier": "prettier --check . --ignore-path ../../../.prettierignore",
"prettier:fix": "prettier --write . --ignore-path ../../../.prettierignore",
"tsc": "tsc -b"
},
"dependencies": {
"@typeagent/agent-sdk": "workspace:*"
},
"devDependencies": {
"prettier": "^3.2.5",
"rimraf": "^5.0.5",
"typescript": "^5.4.2"
}
}
22 changes: 22 additions & 0 deletions ts/packages/agents/test/src/handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { ActionContext, AppAgent } from "@typeagent/agent-sdk";
import { AddAction } from "./schema.js";
import { createActionResult } from "@typeagent/agent-sdk/helpers/action";

export function instantiate(): AppAgent {
return {
executeAction,
};
}

async function executeAction(action: AddAction, context: ActionContext<void>) {
switch (action.actionName) {
case "add":
const { a, b } = action.parameters;
return createActionResult(`The sum of ${a} and ${b} is ${a + b}`);
default:
throw new Error(`Unknown action: ${action.actionName}`);
}
}
9 changes: 9 additions & 0 deletions ts/packages/agents/test/src/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"emojiChar": "",
"description": "Agent to do addition",
"schema": {
"description": "Math agent with action to do addition",
"schemaFile": "./schema.ts",
"schemaType": "AddAction"
}
}
10 changes: 10 additions & 0 deletions ts/packages/agents/test/src/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

export type AddAction = {
actionName: "add";
parameters: {
a: number;
b: number;
};
};
12 changes: 12 additions & 0 deletions ts/packages/agents/test/src/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "../../../../tsconfig.base.json",
"compilerOptions": {
"composite": true,
"rootDir": ".",
"outDir": "../dist",
},
"include": ["./**/*"],
"ts-node": {
"esm": true
}
}
13 changes: 13 additions & 0 deletions ts/packages/agents/test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"composite": true,
},
"include": [],
"references": [
{ "path": "./src" }
],
"ts-node": {
"esm": true
}
}
4 changes: 2 additions & 2 deletions ts/packages/cli/src/commands/constructions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Args, Command, Flags } from "@oclif/core";
import {
readTestData,
getCacheFactory,
loadBuiltinTranslatorSchemaConfig,
loadSchemaConfigFromDefaultAppAgentProviders,
} from "agent-dispatcher/internal";
import { printImportConstructionResult } from "agent-cache";
import fs from "node:fs";
Expand Down Expand Up @@ -36,7 +36,7 @@ export default class ConstructionsCommand extends Command {

const agentCache = getCacheFactory().create(
testDataFile.explainerName,
loadBuiltinTranslatorSchemaConfig,
loadSchemaConfigFromDefaultAppAgentProviders,
);
if (!flags.overwrite && args.output && fs.existsSync(args.output)) {
await agentCache.constructionStore.load(args.output);
Expand Down
4 changes: 2 additions & 2 deletions ts/packages/cli/src/commands/data/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
generateTestDataFiles,
printTestDataStats,
getEmptyTestData,
getBuiltinSchemaNames,
getSchemaNamesFromDefaultAppAgentProviders,
getCacheFactory,
} from "agent-dispatcher/internal";
import chalk from "chalk";
Expand Down Expand Up @@ -45,7 +45,7 @@ export default class ExplanationDataAddCommand extends Command {
}),
translator: Flags.string({
description: "Translator name",
options: getBuiltinSchemaNames(),
options: getSchemaNamesFromDefaultAppAgentProviders(),
}),
explainer: Flags.string({
description:
Expand Down
4 changes: 2 additions & 2 deletions ts/packages/cli/src/commands/data/regenerate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
GenerateDataInput,
getEmptyTestData,
getTestDataFiles,
loadBuiltinTranslatorSchemaConfig,
loadSchemaConfigFromDefaultAppAgentProviders,
} from "agent-dispatcher/internal";
import {
Actions,
Expand Down Expand Up @@ -484,7 +484,7 @@ export default class ExplanationDataRegenerateCommmand extends Command {
if (builtinConstructionConfig !== undefined) {
const agentCache = getCacheFactory().create(
flags.builtin!,
loadBuiltinTranslatorSchemaConfig,
loadSchemaConfigFromDefaultAppAgentProviders,
);
await agentCache.constructionStore.newCache(
flags.none ? undefined : builtinConstructionConfig.file,
Expand Down
4 changes: 2 additions & 2 deletions ts/packages/cli/src/commands/data/stat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import chalk from "chalk";
import { CorrectionRecord } from "agent-cache";
import {
getCacheFactory,
getBuiltinSchemaNames,
getSchemaNamesFromDefaultAppAgentProviders,
readTestData,
getTestDataFiles,
} from "agent-dispatcher/internal";
Expand Down Expand Up @@ -148,7 +148,7 @@ export default class ExplanationDataStatCommmand extends Command {
static flags = {
translator: Flags.string({
description: "Filter by translator",
options: getBuiltinSchemaNames(),
options: getSchemaNamesFromDefaultAppAgentProviders(),
multiple: true,
}),
explainer: Flags.string({
Expand Down
4 changes: 2 additions & 2 deletions ts/packages/cli/src/commands/interactive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { Args, Command, Flags } from "@oclif/core";
import {
getBuiltinSchemaNames,
getSchemaNamesFromDefaultAppAgentProviders,
getCacheFactory,
processCommand,
getPrompt,
Expand All @@ -25,7 +25,7 @@ export default class Interactive extends Command {
static flags = {
translator: Flags.string({
description: "Schema names",
options: getBuiltinSchemaNames(),
options: getSchemaNamesFromDefaultAppAgentProviders(),
multiple: true,
}),
explainer: Flags.string({
Expand Down
4 changes: 2 additions & 2 deletions ts/packages/cli/src/commands/run/explain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from "agent-cache";
import {
getCacheFactory,
getBuiltinSchemaNames,
getSchemaNamesFromDefaultAppAgentProviders,
initializeCommandHandlerContext,
closeCommandHandlerContext,
} from "agent-dispatcher/internal";
Expand All @@ -38,7 +38,7 @@ export default class ExplainCommand extends Command {
static flags = {
translator: Flags.string({
description: "Translator names",
options: getBuiltinSchemaNames(),
options: getSchemaNamesFromDefaultAppAgentProviders(),
multiple: true,
}),
explainer: Flags.string({
Expand Down
4 changes: 2 additions & 2 deletions ts/packages/cli/src/commands/run/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Args, Command, Flags } from "@oclif/core";
import { createDispatcher } from "agent-dispatcher";
import {
getCacheFactory,
getBuiltinSchemaNames,
getSchemaNamesFromDefaultAppAgentProviders,
} from "agent-dispatcher/internal";
import chalk from "chalk";
import { getChatModelNames } from "aiclient";
Expand All @@ -28,7 +28,7 @@ export default class RequestCommand extends Command {
static flags = {
translator: Flags.string({
description: "Schema name",
options: getBuiltinSchemaNames(),
options: getSchemaNamesFromDefaultAppAgentProviders(),
multiple: true,
}),
explainer: Flags.string({
Expand Down
4 changes: 2 additions & 2 deletions ts/packages/cli/src/commands/run/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { Args, Command, Flags } from "@oclif/core";
import { createDispatcher } from "agent-dispatcher";
import { getBuiltinSchemaNames } from "agent-dispatcher/internal";
import { getSchemaNamesFromDefaultAppAgentProviders } from "agent-dispatcher/internal";
import { getChatModelNames } from "aiclient";

const modelNames = await getChatModelNames();
Expand All @@ -19,7 +19,7 @@ export default class TranslateCommand extends Command {
static flags = {
translator: Flags.string({
description: "Translator name",
options: getBuiltinSchemaNames(),
options: getSchemaNamesFromDefaultAppAgentProviders(),
multiple: true,
}),
model: Flags.string({
Expand Down
10 changes: 5 additions & 5 deletions ts/packages/cli/src/commands/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { composeTranslatorSchemas } from "common-utils";
import {
getAssistantSelectionSchemas,
getFullSchemaText,
getBuiltinSchemaNames,
getBuiltinActionConfigProvider,
getSchemaNamesFromDefaultAppAgentProviders,
getActionConfigProviderFromDefaultAppAgentProviders,
getActionSchema,
} from "agent-dispatcher/internal";
import { generateSchemaTypeDefinition } from "action-schema";
Expand Down Expand Up @@ -39,7 +39,7 @@ export default class Schema extends Command {
translator: Args.string({
description: "Translator name",
required: true,
options: getBuiltinSchemaNames(),
options: getSchemaNamesFromDefaultAppAgentProviders(),
}),
actionName: Args.string({
description: "Action name",
Expand All @@ -49,7 +49,7 @@ export default class Schema extends Command {

async run(): Promise<void> {
const { args, flags } = await this.parse(Schema);
const provider = getBuiltinActionConfigProvider();
const provider = getActionConfigProviderFromDefaultAppAgentProviders();
if (!flags.assistant) {
if (args.actionName) {
const actionSchema = getActionSchema(
Expand Down Expand Up @@ -80,7 +80,7 @@ export default class Schema extends Command {
);
} else {
const schemas = getAssistantSelectionSchemas(
getBuiltinSchemaNames(),
getSchemaNamesFromDefaultAppAgentProviders(),
provider,
).map((entry) => entry.schema);
console.log(
Expand Down
Loading

0 comments on commit 5563175

Please sign in to comment.