Skip to content

Commit 9a05801

Browse files
committed
update
1 parent 8341dfa commit 9a05801

File tree

11 files changed

+259
-377
lines changed

11 files changed

+259
-377
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ Microsoft TypeAgent Repo is a mono-repo, with components organized with the foll
8282
- [`python`](./python) Python code ([Readme](./python/README.md))
8383
- [`dotnet`](./dotnet) Dotnet (C#) code ([Readme](./dotnet/README.md))
8484

85+
For developer who want to experiment with TypeAgent and see how typed action schema drives actions, the [Agent Shell](./ts/packages/shell) example allow additional agent to be installed/registered to extent functionality. The `Echo` agent [tutorial](./docs/tutorial/agent.md) is a starting point to create a plugin agent, and [Agent SDK](./ts/packages/agentSdk/) provides the details of the interface between [Dispatcher](./ts/packages/dispatcher) and the agent.
86+
8587
## Contributing
8688

8789
This project welcomes contributions and suggestions. Most contributions require you to

docs/tutorial/agent.md

Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
# External Agents
2+
3+
The TypeAgent repo includes several example [agents](../../packages/agents/). You can also build **your own application agents** **_outside_** the TypeAgent repo by using the [agent-sdk](./README.md). You can package these **_external_** agents as npm packages and surface them in the [TypeAgent Shell](../shell) and [TypeAgent CLI](../cli).
4+
5+
This document describes how you can build your own external application agents.
6+
7+
## Prerequisites
8+
9+
Begin by exploring the following:
10+
11+
- **Agent-Sdk**: Read about the architecture of the [**agent-sdk**](./README.md).
12+
- **Example Agents**:
13+
- Review agents under the [agents](../agents) directory. The [List](../agents/list/) agent provides a good example and template for building an agent.
14+
- The [Echo](../../examples/agentExamples/echo/) agent illustrates the basics of building your own external application agents.
15+
16+
## Steps to build an `Echo` agent:
17+
18+
For the rest of the documentation, we will assume that the external agent is named **echo**. The echo agent performs a single action: echos any input back to the user.
19+
20+
You can see the end result of this tutorial in [Echo](../../examples/agentExamples/echo/) with some modification (NOTE: The only difference is the @typeagent/agent-sdk dependency)
21+
22+
### Step 1: Create and author the `Echo` agent package
23+
24+
Follow the following steps to create the `Echo` agent packages manually. Start by create a directory `echo`. Then populate the directory with the following content:
25+
26+
**package.json** [package.json](../../examples/agentExamples/echo/package.json) :
27+
28+
The `package.json` contains references to **handler** and **manifest** files in the `exports` field.
29+
30+
```json
31+
{
32+
"name": "echo",
33+
"version": "0.0.1",
34+
"description": "Echo example for TypeAgent",
35+
"license": "MIT",
36+
"author": "Microsoft",
37+
"type": "module",
38+
"exports": {
39+
"./agent/manifest": "./src/echoManifest.json",
40+
"./agent/handlers": "./dist/echoActionHandler.js"
41+
},
42+
"scripts": {
43+
"build": "npm run tsc",
44+
"clean": "rimraf --glob dist *.tsbuildinfo *.done.build.log",
45+
"tsc": "tsc -b"
46+
},
47+
"keywords": [],
48+
"dependencies": {
49+
"@typeagent/agent-sdk": "0.0.1"
50+
},
51+
"devDependencies": {
52+
"rimraf": "^5.0.5",
53+
"typescript": "^5.4.2"
54+
}
55+
}
56+
```
57+
58+
Every application agent requires the following files to be present in the agent's [**source**](../../examples/agentExamples/echo/src/) directory.
59+
60+
- **Agent Manifest File**: The manifest file is used to register the agent with the TypeAgent ecosystem.
61+
- **Action Schema File**: The action schema file is used to define the actions that the agent can perform.
62+
- **Agent Action Handler**: Your code that perform's the agent's actions.
63+
64+
**Agent Manifest File** : [`src/echoManifest.json`](../../examples/agentExamples/echo/src/echoManifest.json)
65+
66+
The manifest file contain reference in `schemaFile` to the path to **schema** file (relative path to the **manifest** file) and the `schemaType` corresponds to the union type of all the actions.
67+
68+
```json
69+
{
70+
"emojiChar": "🦜",
71+
"schema": {
72+
"description": "A basic echo agent.",
73+
"schemaFile": "./echoActionSchema.ts",
74+
"schemaType": "EchoAction"
75+
}
76+
}
77+
```
78+
79+
**Agent Action Schema File** : [`src/echoActionSchema.ts`](../../examples/agentExamples/echo/src/echoActionSchema.ts)
80+
81+
```ts
82+
export type EchoAction = GenEchoAction;
83+
84+
// If the user asks to echo a message back, the system will return a GenEchoAction. The text parameter is the message to be echoed back.
85+
// will contain the text to be echoed back to the user.
86+
export type GenEchoAction = {
87+
actionName: "echoGen";
88+
parameters: {
89+
text: string;
90+
};
91+
};
92+
```
93+
94+
**Agent action handler** : [`src/echoActionHandler.ts`](../../examples/agentExamples/echo/src/echoActionHandler.ts)
95+
96+
```ts
97+
import { ActionContext, AppAgent, TypeAgentAction } from "@typeagent/agent-sdk";
98+
import {
99+
createActionResultFromTextDisplay,
100+
createActionResultFromError,
101+
} from "@typeagent/agent-sdk/helpers/action";
102+
import { EchoAction } from "./echoActionSchema.js";
103+
104+
export function instantiate(): AppAgent {
105+
return {
106+
initializeAgentContext: initializeEchoContext,
107+
executeAction: executeEchoAction,
108+
};
109+
}
110+
111+
type EchoActionContext = {
112+
echoCount: number;
113+
};
114+
115+
async function initializeEchoContext(): Promise<EchoActionContext> {
116+
return { echoCount: 0 };
117+
}
118+
119+
async function executeEchoAction(
120+
action: TypeAgentAction<EchoAction>,
121+
context: ActionContext<EchoActionContext>
122+
) {
123+
// The context created in initializeEchoContext is returned in the action context.
124+
const echoContext = context.sessionContext.agentContext;
125+
switch (action.actionName) {
126+
case "echoGen":
127+
const displayText = `>> Echo ${++echoContext.echoCount}: ${
128+
action.parameters.text
129+
}`;
130+
return createActionResultFromTextDisplay(displayText, displayText);
131+
132+
default:
133+
return createActionResultFromError("Unable to process the action");
134+
}
135+
}
136+
```
137+
138+
**Typescript build config file** [`tsconfig.json`](../../examples/agentExamples/echo/tsconfig.json)
139+
140+
```json
141+
{
142+
"compilerOptions": {
143+
"composite": true,
144+
"target": "es2021",
145+
"lib": ["es2021"],
146+
"module": "node16",
147+
"declaration": true,
148+
"declarationMap": true,
149+
"esModuleInterop": true,
150+
"exactOptionalPropertyTypes": true,
151+
"forceConsistentCasingInFileNames": true,
152+
"incremental": true,
153+
"noEmitOnError": true,
154+
"noUnusedLocals": true,
155+
"skipLibCheck": true,
156+
"strict": true,
157+
"sourceMap": true,
158+
"rootDir": "./src",
159+
"outDir": "./dist"
160+
},
161+
"include": ["./src/**/*"],
162+
"ts-node": {
163+
"esm": true
164+
}
165+
}
166+
```
167+
168+
#### Folder structure for **Echo** agent:
169+
170+
![alt text](./imgs/image-files.png)
171+
172+
<a id="install_agent"></a>
173+
174+
#### Step 2: Build the Agent
175+
176+
First make sure the [TypeAgent's typescript code](../..) is built.
177+
178+
- Go to `<repo>/ts`
179+
- `pnpm i`
180+
- `pnpm run build`
181+
182+
Then link the `@typeagent/agent-sdk` package to the `Echo` agent.
183+
184+
- Go to `<repo>/ts/packages/agentSdk`
185+
- `npm link`
186+
187+
In the `Echo` package, Run the following to build
188+
189+
- `npm link @typeagent/agent-sdk`
190+
- `npm install`
191+
- `npm run build`
192+
193+
### Step 3: Install `Echo` agent in TypeAgent cli or shell.
194+
195+
Start TypeAgent [Shell](../shell) or [CLI](../cli)
196+
197+
```bash
198+
# you can run these commands from the `ts` folder
199+
# in the TypeAgent root.
200+
201+
pnpm run cli interactive
202+
203+
or
204+
205+
pnpm run shell
206+
```
207+
208+
In the [Shell](../shell) or [CLI](../cli), install the echo agent and check the status by issuing the command:
209+
210+
```
211+
@install echo <path to echo package>
212+
@config agent
213+
```
214+
215+
The `Echo` agent should be in the list and enabled.
216+
217+
### Step 4: See the `Echo` agent in action
218+
219+
`Echo` agent is now ready. Test it out by issuing some request to see the `Echo` agent in action
220+
221+
When to run the cli this is how interaction with the `Echo` agent will look like:
222+
![alt text](./imgs/image-cli.png)
223+
224+
When to run the shell this is how interaction with the `Echo` agent will look like:
225+
![alt text](./imgs/image-shell.png)
226+
227+
The `Echo` agent will be reloaded again after installation. It can be uninstalled using the command:
228+
229+
```
230+
@uninstall echo
231+
```
232+
233+
## Next step
234+
235+
Start modifying the `Echo` agent and add new action schema and action handlers.
236+
237+
## Trademarks
238+
239+
This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft
240+
trademarks or logos is subject to and must follow
241+
[Microsoft's Trademark & Brand Guidelines](https://www.microsoft.com/en-us/legal/intellectualproperty/trademarks/usage/general).
242+
Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship.
243+
Any use of third-party trademarks or logos are subject to those third-party's policies.

ts/examples/agentExamples/echo/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Echo Agent
22

3-
The Echo Agent demonstrates how to use the [agent-sdk](../../../packages/agentSdk/ExternalAgents_README.md) to write agents as installable packages.
3+
The Echo Agent demonstrates how to use the [agent-sdk](../../../../docs/tutorial/agent.md) to write agents as installable packages.
44

55
## Trademarks
66

ts/examples/agentExamples/echo/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@
1414
"type": "module",
1515
"exports": {
1616
"./agent/handlers": "./dist/echoActionHandler.js",
17-
"./agent/manifest": "./dist/echoManifest.json"
17+
"./agent/manifest": "./src/echoManifest.json"
1818
},
1919
"scripts": {
2020
"build": "npm run tsc",
21-
"postbuild": "copyfiles -u 1 \"src/**/*Schema*.ts\" \"src/**/*Manifest*.json\" dist",
2221
"clean": "rimraf --glob dist *.tsbuildinfo *.done.build.log",
2322
"prettier": "prettier --check . --ignore-path ../../../.prettierignore",
2423
"prettier:fix": "prettier --write . --ignore-path ../../../.prettierignore",
Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
// Copyright (c) Microsoft Corporation.
2-
// Licensed under the MIT License.
1+
// Below is sample code for a simple echo agent.
32

43
import {
54
ActionContext,
65
AppAction,
76
AppAgent,
87
SessionContext,
9-
ActionResult,
108
} from "@typeagent/agent-sdk";
119
import {
1210
createActionResultFromTextDisplay,
1311
createActionResultFromError,
1412
} from "@typeagent/agent-sdk/helpers/action";
15-
16-
import { EchoAction } from "./echoActionsSchema.js";
13+
import { EchoAction } from "./echoActionSchema.js";
1714

1815
export function instantiate(): AppAgent {
1916
return {
@@ -25,25 +22,19 @@ export function instantiate(): AppAgent {
2522

2623
type EchoActionContext = {
2724
echoCount: number;
28-
echoRequests: Set<string> | undefined;
2925
};
3026

3127
async function initializeEchoContext() {
3228
return {
3329
echoCount: 0,
34-
echoRequests: undefined,
3530
};
3631
}
3732

3833
async function updateEchoContext(
3934
enable: boolean,
4035
context: SessionContext<EchoActionContext>,
4136
): Promise<void> {
42-
if (enable) {
43-
context.agentContext.echoRequests = new Set<string>();
44-
context.agentContext.echoCount = 0;
45-
}
46-
context.agentContext.echoCount++;
37+
// Do nothing
4738
}
4839

4940
async function executeEchoAction(
@@ -61,21 +52,12 @@ async function handleEchoAction(
6152
action: EchoAction,
6253
echoContext: EchoActionContext,
6354
) {
64-
let result: ActionResult | undefined = undefined;
65-
let displayText: string | undefined = undefined;
6655
switch (action.actionName) {
6756
case "echoGen":
68-
displayText = `>> Echo: ${action.parameters.text}`;
69-
result = createActionResultFromTextDisplay(
70-
displayText,
71-
displayText,
72-
);
73-
break;
57+
const displayText = `>> Echo ${++echoContext.echoCount}: ${action.parameters.text}`;
58+
return createActionResultFromTextDisplay(displayText, displayText);
59+
7460
default:
75-
result = createActionResultFromError(
76-
"Unable to process the action",
77-
);
78-
break;
61+
return createActionResultFromError("Unable to process the action");
7962
}
80-
return result;
8163
}

ts/examples/agentExamples/echo/src/echoManifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"emojiChar": "🦜",
33
"schema": {
44
"description": "A basic echo agent.",
5-
"schemaFile": "echoActionsSchema.ts",
5+
"schemaFile": "./echoActionSchema.ts",
66
"schemaType": "EchoAction"
77
}
88
}

ts/examples/agentExamples/measure/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Work in progress.
66

77
## Installation
88

9-
- Follow the standard exteneral [Agent installation steps](../../../packages/agentSdk/ExternalAgents_README.md#install_agent)
9+
- Follow the standard external [Agent installation steps](../../../../docs/tutorial/agent.md#step-3-install-echo-agent-in-typeagent-cli-or-shell)
1010

1111
## Trademarks
1212

0 commit comments

Comments
 (0)