@statelyai/agent 1.1.5 → 2.0.0-next.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.changeset/light-hats-drive.md +9 -0
- package/.changeset/pre.json +10 -0
- package/.vscode/launch.json +6 -0
- package/CHANGELOG.md +17 -0
- package/dist/index.d.mts +262 -165
- package/dist/index.d.ts +262 -165
- package/dist/index.js +368 -258
- package/dist/index.mjs +371 -256
- package/examples/chatbot-alt.ts +57 -0
- package/examples/chatbot.ts +11 -16
- package/examples/cot.ts +25 -22
- package/examples/customer-service-sim.ts +107 -0
- package/examples/email.ts +14 -14
- package/examples/example.ts +5 -5
- package/examples/executor.ts +66 -0
- package/examples/goal.ts +11 -11
- package/examples/helpers/helpers.ts +26 -14
- package/examples/joke.ts +78 -75
- package/examples/jugs.ts +125 -0
- package/examples/multi.ts +4 -4
- package/examples/newspaper.ts +98 -104
- package/examples/number.ts +5 -4
- package/examples/raffle.ts +10 -11
- package/examples/river-crossing.ts +140 -0
- package/examples/sandbox.ts +1 -1
- package/examples/simple.ts +4 -2
- package/examples/summary.ts +121 -0
- package/examples/support.ts +5 -5
- package/examples/ticTacToe.ts +86 -45
- package/examples/todo.ts +6 -6
- package/examples/tutor.ts +13 -13
- package/examples/verify.ts +2 -2
- package/examples/weather.ts +5 -8
- package/examples/wiki.ts +26 -7
- package/examples/word.ts +15 -10
- package/package.json +15 -12
- package/readme.md +1 -1
- package/src/agent-experimental.ts +1 -1
- package/src/agent.test.ts +117 -228
- package/src/agent.ts +469 -81
- package/src/{decision.test.ts → decide.test.ts} +26 -50
- package/src/decide.ts +153 -0
- package/src/index.ts +1 -1
- package/src/middleware.ts +103 -0
- package/src/mockModel.ts +47 -0
- package/src/planners/shortestPathPlanner.ts +151 -13
- package/src/planners/simplePlanner.ts +57 -85
- package/src/strategies/chain-of-note.ts +6 -55
- package/src/text.ts +51 -139
- package/src/types.ts +172 -204
- package/src/utils.ts +37 -4
- package/src/adapters/vercel.ts +0 -7
- package/src/decision.ts +0 -84
- package/src/memory.ts +0 -25
package/examples/verify.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { assign, createActor, setup, log } from 'xstate';
|
|
2
|
-
import {
|
|
2
|
+
import { fromTerminal } from './helpers/helpers';
|
|
3
3
|
import { createAgent, fromDecision } from '../src';
|
|
4
4
|
import { z } from 'zod';
|
|
5
5
|
import { openai } from '@ai-sdk/openai';
|
|
@@ -38,7 +38,7 @@ const machine = setup({
|
|
|
38
38
|
events: agent.types.events,
|
|
39
39
|
},
|
|
40
40
|
actors: {
|
|
41
|
-
getFromTerminal,
|
|
41
|
+
getFromTerminal: fromTerminal,
|
|
42
42
|
agent: fromDecision(agent),
|
|
43
43
|
},
|
|
44
44
|
}).createMachine({
|
package/examples/weather.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createAgent, fromDecision } from '../src';
|
|
2
2
|
import { assign, createActor, fromPromise, log, setup } from 'xstate';
|
|
3
|
-
import {
|
|
3
|
+
import { fromTerminal } from './helpers/helpers';
|
|
4
4
|
import { z } from 'zod';
|
|
5
5
|
import { openai } from '@ai-sdk/openai';
|
|
6
6
|
|
|
@@ -50,7 +50,7 @@ const getWeather = fromPromise(async ({ input }: { input: string }) => {
|
|
|
50
50
|
|
|
51
51
|
const agent = createAgent({
|
|
52
52
|
name: 'weather',
|
|
53
|
-
model: openai('gpt-
|
|
53
|
+
model: openai('gpt-4o-mini'),
|
|
54
54
|
events: {
|
|
55
55
|
'agent.getWeather': z.object({
|
|
56
56
|
location: z.string().describe('The location to get the weather for'),
|
|
@@ -83,7 +83,7 @@ const machine = setup({
|
|
|
83
83
|
actors: {
|
|
84
84
|
agent: fromDecision(agent),
|
|
85
85
|
getWeather,
|
|
86
|
-
getFromTerminal,
|
|
86
|
+
getFromTerminal: fromTerminal,
|
|
87
87
|
},
|
|
88
88
|
}).createMachine({
|
|
89
89
|
initial: 'getLocation',
|
|
@@ -149,6 +149,7 @@ const machine = setup({
|
|
|
149
149
|
src: 'agent',
|
|
150
150
|
input: ({ context }) => ({
|
|
151
151
|
goal: 'Report the weather', // TODO
|
|
152
|
+
context,
|
|
152
153
|
}),
|
|
153
154
|
},
|
|
154
155
|
on: {
|
|
@@ -167,11 +168,7 @@ const machine = setup({
|
|
|
167
168
|
},
|
|
168
169
|
});
|
|
169
170
|
|
|
170
|
-
const actor = createActor(machine
|
|
171
|
-
input: {
|
|
172
|
-
location: 'New York',
|
|
173
|
-
},
|
|
174
|
-
});
|
|
171
|
+
const actor = createActor(machine);
|
|
175
172
|
actor.subscribe((s) => {
|
|
176
173
|
console.log(s.value);
|
|
177
174
|
});
|
package/examples/wiki.ts
CHANGED
|
@@ -1,30 +1,49 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
2
|
import { createAgent } from '../src';
|
|
3
3
|
import { openai } from '@ai-sdk/openai';
|
|
4
|
+
import { CoreMessage, generateText, streamText } from 'ai';
|
|
4
5
|
|
|
5
6
|
const agent = createAgent({
|
|
6
7
|
name: 'wiki',
|
|
7
|
-
model: openai('gpt-
|
|
8
|
+
model: openai('gpt-4o-mini'),
|
|
8
9
|
events: {
|
|
9
10
|
provideAnswer: z.object({
|
|
10
11
|
answer: z.string().describe('The answer'),
|
|
11
12
|
}),
|
|
13
|
+
researchTopic: z.object({
|
|
14
|
+
topic: z.string().describe('The topic to research'),
|
|
15
|
+
}),
|
|
12
16
|
},
|
|
13
17
|
});
|
|
14
18
|
|
|
19
|
+
agent.onMessage((msg) => {
|
|
20
|
+
console.log(msg);
|
|
21
|
+
});
|
|
22
|
+
|
|
15
23
|
async function main() {
|
|
16
|
-
const
|
|
24
|
+
const response = await generateText({
|
|
25
|
+
model: agent.model,
|
|
17
26
|
prompt: 'When was Deadpool 2 released?',
|
|
18
27
|
});
|
|
19
28
|
|
|
20
|
-
|
|
29
|
+
for (const msg of await response.responseMessages) {
|
|
30
|
+
agent.addMessage(msg);
|
|
31
|
+
}
|
|
21
32
|
|
|
22
|
-
const response2 = await
|
|
23
|
-
|
|
24
|
-
|
|
33
|
+
const response2 = await streamText({
|
|
34
|
+
model: agent.model,
|
|
35
|
+
messages: (agent.getMessages() as CoreMessage[]).concat({
|
|
36
|
+
role: 'user',
|
|
37
|
+
content: 'What about the first one?',
|
|
38
|
+
}),
|
|
25
39
|
});
|
|
26
40
|
|
|
27
|
-
|
|
41
|
+
let text = '';
|
|
42
|
+
|
|
43
|
+
for await (const t of response2.textStream) {
|
|
44
|
+
text += t;
|
|
45
|
+
console.log(text);
|
|
46
|
+
}
|
|
28
47
|
}
|
|
29
48
|
|
|
30
49
|
main();
|
package/examples/word.ts
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
1
|
import { assign, createActor, log, setup } from 'xstate';
|
|
2
|
-
import {
|
|
2
|
+
import { fromTerminal } from './helpers/helpers';
|
|
3
3
|
import { createAgent, fromDecision } from '../src';
|
|
4
4
|
import { z } from 'zod';
|
|
5
5
|
import { openai } from '@ai-sdk/openai';
|
|
6
6
|
|
|
7
|
-
const context = {
|
|
8
|
-
word: null as string | null,
|
|
9
|
-
guessedWord: null as string | null,
|
|
10
|
-
lettersGuessed: [] as string[],
|
|
11
|
-
};
|
|
12
|
-
|
|
13
7
|
const agent = createAgent({
|
|
14
8
|
name: 'word',
|
|
15
|
-
model: openai('gpt-4o'),
|
|
9
|
+
model: openai('gpt-4o-mini'),
|
|
10
|
+
context: {
|
|
11
|
+
word: z.string().nullable().describe('The word to guess'),
|
|
12
|
+
guessedWord: z.string().nullable().describe('The guessed word'),
|
|
13
|
+
lettersGuessed: z.array(z.string()).describe('The letters guessed'),
|
|
14
|
+
},
|
|
16
15
|
events: {
|
|
17
16
|
'agent.guessLetter': z.object({
|
|
18
17
|
letter: z.string().min(1).max(1).describe('The letter guessed'),
|
|
@@ -33,14 +32,20 @@ const agent = createAgent({
|
|
|
33
32
|
},
|
|
34
33
|
});
|
|
35
34
|
|
|
35
|
+
const context = {
|
|
36
|
+
word: null,
|
|
37
|
+
guessedWord: null,
|
|
38
|
+
lettersGuessed: [],
|
|
39
|
+
} satisfies typeof agent.types.context;
|
|
40
|
+
|
|
36
41
|
const wordGuesserMachine = setup({
|
|
37
42
|
types: {
|
|
38
|
-
context:
|
|
43
|
+
context: agent.types.context,
|
|
39
44
|
events: agent.types.events,
|
|
40
45
|
},
|
|
41
46
|
actors: {
|
|
42
47
|
agent: fromDecision(agent),
|
|
43
|
-
getFromTerminal,
|
|
48
|
+
getFromTerminal: fromTerminal,
|
|
44
49
|
},
|
|
45
50
|
actions: {
|
|
46
51
|
resetContext: assign(context),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@statelyai/agent",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0-next.0",
|
|
4
4
|
"description": "Stateful agents that make decisions based on finite-state machine models",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -12,22 +12,24 @@
|
|
|
12
12
|
"rl",
|
|
13
13
|
"reinforcement learning"
|
|
14
14
|
],
|
|
15
|
-
"author": "",
|
|
15
|
+
"author": "David Khourshid <david@stately.ai>",
|
|
16
16
|
"license": "MIT",
|
|
17
17
|
"devDependencies": {
|
|
18
|
+
"@ai-sdk/openai": "^0.0.40",
|
|
18
19
|
"@changesets/changelog-github": "^0.5.0",
|
|
19
|
-
"@changesets/cli": "^2.27.
|
|
20
|
+
"@changesets/cli": "^2.27.9",
|
|
20
21
|
"@langchain/community": "^0.0.53",
|
|
21
22
|
"@langchain/core": "^0.1.63",
|
|
22
23
|
"@langchain/openai": "^0.0.28",
|
|
23
|
-
"@
|
|
24
|
+
"@tavily/core": "^0.0.2",
|
|
25
|
+
"@types/node": "^20.17.1",
|
|
24
26
|
"@types/object-hash": "^3.0.6",
|
|
25
27
|
"dotenv": "^16.4.5",
|
|
26
|
-
"json-schema-to-ts": "^3.1.
|
|
28
|
+
"json-schema-to-ts": "^3.1.1",
|
|
27
29
|
"ts-node": "^10.9.2",
|
|
28
|
-
"tsup": "^8.
|
|
29
|
-
"typescript": "^5.
|
|
30
|
-
"vitest": "^2.
|
|
30
|
+
"tsup": "^8.3.5",
|
|
31
|
+
"typescript": "^5.6.3",
|
|
32
|
+
"vitest": "^2.1.3",
|
|
31
33
|
"wikipedia": "^2.1.2",
|
|
32
34
|
"zod": "^3.23.8"
|
|
33
35
|
},
|
|
@@ -35,11 +37,12 @@
|
|
|
35
37
|
"access": "public"
|
|
36
38
|
},
|
|
37
39
|
"dependencies": {
|
|
38
|
-
"@
|
|
39
|
-
"
|
|
40
|
-
"
|
|
40
|
+
"@xstate/graph": "^2.0.1",
|
|
41
|
+
"ai": "^3.4.20",
|
|
42
|
+
"ajv": "^8.17.1",
|
|
41
43
|
"object-hash": "^3.0.0",
|
|
42
|
-
"xstate": "^5.
|
|
44
|
+
"xstate": "^5.18.2",
|
|
45
|
+
"zod-to-json-schema": "^3.23.5"
|
|
43
46
|
},
|
|
44
47
|
"scripts": {
|
|
45
48
|
"build": "tsup src/index.ts --format cjs,esm --dts",
|
package/readme.md
CHANGED
|
@@ -5,6 +5,6 @@ Stately Agent is a flexible framework for building AI agents using state machine
|
|
|
5
5
|
- Using state machines to guide the agent's behavior, powered by [XState](https://stately.ai/docs/xstate)
|
|
6
6
|
- Incorporating **observations**, **message history**, and **feedback** to the agent decision-making and text-generation processes, as needed
|
|
7
7
|
- Enabling custom **planning** abilities for agents to achieve specific goals based on state machine logic, observations, and feedback
|
|
8
|
-
-
|
|
8
|
+
- First-class integration with the [Vercel AI SDK](https://sdk.vercel.ai/) to easily support multiple model providers, such as OpenAI, Anthropic, Google, Mistral, Groq, Perplexity, and more
|
|
9
9
|
|
|
10
10
|
**Read the documentation: [stately.ai/docs/agents](https://stately.ai/docs/agents)**
|