@statelyai/agent 1.1.6 ā 2.0.0-alpha.5
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/LICENSE +21 -0
- package/dist/ai-sdk.cjs +249 -0
- package/dist/ai-sdk.d.cts +168 -0
- package/dist/ai-sdk.d.mts +168 -0
- package/dist/ai-sdk.mjs +241 -0
- package/dist/cli.cjs +63 -0
- package/dist/cli.d.cts +1 -0
- package/dist/cli.d.mts +1 -0
- package/dist/cli.mjs +64 -0
- package/dist/decision-FTmbqSEe.mjs +938 -0
- package/dist/decision-pC-bY2DE.cjs +1231 -0
- package/dist/index.cjs +54 -0
- package/dist/index.d.cts +1217 -0
- package/dist/index.d.mts +1194 -405
- package/dist/index.mjs +3 -588
- package/dist/openai-compat.cjs +319 -0
- package/dist/openai-compat.d.cts +98 -0
- package/dist/openai-compat.d.mts +98 -0
- package/dist/openai-compat.mjs +312 -0
- package/dist/src-CjpHDU8F.mjs +2445 -0
- package/dist/src-DcRsWPfV.cjs +2564 -0
- package/dist/text-logic-1ZQkO3zr.d.cts +682 -0
- package/dist/text-logic-2EMJIS-n.d.mts +682 -0
- package/dist/types-BHjeDdch.d.cts +208 -0
- package/dist/types-Cq1YlAQ6.d.mts +208 -0
- package/dist/utils-CWUCa3pF.d.mts +108 -0
- package/dist/utils-lK1wnL2i.d.cts +108 -0
- package/dist/zod.cjs +31 -0
- package/dist/zod.d.cts +30 -0
- package/dist/zod.d.mts +30 -0
- package/dist/zod.mjs +30 -0
- package/package.json +109 -28
- package/readme.md +144 -6
- package/schemas/agent-workflow.json +527 -0
- package/.changeset/README.md +0 -8
- package/.changeset/config.json +0 -11
- package/.env.template +0 -3
- package/.github/actions/ci-setup/action.yml +0 -24
- package/.github/workflows/release.yml +0 -46
- package/.vscode/launch.json +0 -28
- package/CHANGELOG.md +0 -222
- package/dist/index.d.ts +0 -428
- package/dist/index.js +0 -621
- package/examples/chatbot.ts +0 -71
- package/examples/cot.ts +0 -89
- package/examples/email.ts +0 -118
- package/examples/example.ts +0 -81
- package/examples/goal.ts +0 -94
- package/examples/helpers/helpers.ts +0 -17
- package/examples/helpers/loader.ts +0 -32
- package/examples/helpers/runner.ts +0 -27
- package/examples/joke.ts +0 -225
- package/examples/multi.ts +0 -103
- package/examples/newspaper.ts +0 -324
- package/examples/number.ts +0 -102
- package/examples/raffle.ts +0 -105
- package/examples/sandbox.ts +0 -28
- package/examples/simple.ts +0 -39
- package/examples/support.ts +0 -147
- package/examples/ticTacToe.ts +0 -224
- package/examples/todo.ts +0 -137
- package/examples/tutor.ts +0 -100
- package/examples/verify.ts +0 -120
- package/examples/weather.ts +0 -178
- package/examples/wiki.ts +0 -30
- package/examples/word.ts +0 -171
- package/src/adapters/vercel.ts +0 -7
- package/src/agent-experimental.ts +0 -221
- package/src/agent.test.ts +0 -506
- package/src/agent.ts +0 -300
- package/src/decision.test.ts +0 -179
- package/src/decision.ts +0 -84
- package/src/index.ts +0 -4
- package/src/memory.ts +0 -25
- package/src/planners/shortestPathPlanner.ts +0 -22
- package/src/planners/simplePlanner.ts +0 -139
- package/src/schemas.ts +0 -11
- package/src/strategies/chain-of-note.ts +0 -155
- package/src/templates/defaultText.ts +0 -18
- package/src/text.ts +0 -241
- package/src/types.ts +0 -499
- package/src/utils.ts +0 -72
- package/tsconfig.json +0 -109
- package/vitest.config.ts +0 -9
package/examples/multi.ts
DELETED
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
import { createAgent, fromDecision } from '../src';
|
|
2
|
-
import { z } from 'zod';
|
|
3
|
-
import { assign, createActor, log, setup } from 'xstate';
|
|
4
|
-
import { getFromTerminal } from './helpers/helpers';
|
|
5
|
-
import { openai } from '@ai-sdk/openai';
|
|
6
|
-
|
|
7
|
-
const agent = createAgent({
|
|
8
|
-
name: 'multi',
|
|
9
|
-
model: openai('gpt-4-1106-preview'),
|
|
10
|
-
events: {
|
|
11
|
-
'agent.respond': z.object({
|
|
12
|
-
response: z.string().describe('The response from the agent'),
|
|
13
|
-
}),
|
|
14
|
-
},
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
const machine = setup({
|
|
18
|
-
types: {
|
|
19
|
-
context: {} as {
|
|
20
|
-
topic: string | null;
|
|
21
|
-
discourse: string[];
|
|
22
|
-
},
|
|
23
|
-
},
|
|
24
|
-
actors: {
|
|
25
|
-
getFromTerminal,
|
|
26
|
-
agent: fromDecision(agent),
|
|
27
|
-
},
|
|
28
|
-
}).createMachine({
|
|
29
|
-
initial: 'asking',
|
|
30
|
-
context: {
|
|
31
|
-
topic: null,
|
|
32
|
-
discourse: [],
|
|
33
|
-
},
|
|
34
|
-
states: {
|
|
35
|
-
asking: {
|
|
36
|
-
invoke: {
|
|
37
|
-
src: 'getFromTerminal',
|
|
38
|
-
input: 'What is the question?',
|
|
39
|
-
onDone: {
|
|
40
|
-
actions: assign({
|
|
41
|
-
topic: ({ event }) => event.output,
|
|
42
|
-
}),
|
|
43
|
-
target: 'positiveResponse',
|
|
44
|
-
},
|
|
45
|
-
},
|
|
46
|
-
},
|
|
47
|
-
positiveResponse: {
|
|
48
|
-
invoke: {
|
|
49
|
-
src: 'agent',
|
|
50
|
-
input: ({ context }) => ({
|
|
51
|
-
context,
|
|
52
|
-
goal: 'Debate the topic, and take the positive position. Respond directly to the last message of the discourse. Keep it short.',
|
|
53
|
-
}),
|
|
54
|
-
},
|
|
55
|
-
on: {
|
|
56
|
-
'agent.respond': {
|
|
57
|
-
actions: [
|
|
58
|
-
assign({
|
|
59
|
-
discourse: ({ context, event }) =>
|
|
60
|
-
context.discourse.concat(event.response),
|
|
61
|
-
}),
|
|
62
|
-
log(({ event }) => event.response),
|
|
63
|
-
],
|
|
64
|
-
target: 'negativeResponse',
|
|
65
|
-
},
|
|
66
|
-
},
|
|
67
|
-
},
|
|
68
|
-
negativeResponse: {
|
|
69
|
-
invoke: {
|
|
70
|
-
src: 'agent',
|
|
71
|
-
input: ({ context }) => ({
|
|
72
|
-
model: openai('gpt-3.5-turbo-16k-0613'),
|
|
73
|
-
context,
|
|
74
|
-
goal: 'Debate the topic, and take the negative position. Respond directly to the last message of the discourse. Keep it short.',
|
|
75
|
-
}),
|
|
76
|
-
},
|
|
77
|
-
on: {
|
|
78
|
-
'agent.respond': {
|
|
79
|
-
actions: [
|
|
80
|
-
assign({
|
|
81
|
-
discourse: ({ context, event }) =>
|
|
82
|
-
context.discourse.concat(event.response),
|
|
83
|
-
}),
|
|
84
|
-
log(({ event }) => event.response),
|
|
85
|
-
],
|
|
86
|
-
target: 'positiveResponse',
|
|
87
|
-
},
|
|
88
|
-
},
|
|
89
|
-
always: {
|
|
90
|
-
guard: ({ context }) => context.discourse.length >= 5,
|
|
91
|
-
target: 'debateOver',
|
|
92
|
-
},
|
|
93
|
-
},
|
|
94
|
-
debateOver: {
|
|
95
|
-
type: 'final',
|
|
96
|
-
},
|
|
97
|
-
},
|
|
98
|
-
exit: () => {
|
|
99
|
-
process.exit();
|
|
100
|
-
},
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
createActor(machine).start();
|
package/examples/newspaper.ts
DELETED
|
@@ -1,324 +0,0 @@
|
|
|
1
|
-
// Based on GPT Newspaper:
|
|
2
|
-
// https://github.com/assafelovic/gpt-newspaper
|
|
3
|
-
// https://gist.github.com/TheGreatBonnie/58dc21ebbeeb8cbb08df665db762738c
|
|
4
|
-
|
|
5
|
-
import { TavilySearchAPIRetriever } from '@langchain/community/retrievers/tavily_search_api';
|
|
6
|
-
import { ChatOpenAI } from '@langchain/openai';
|
|
7
|
-
import { HumanMessage, SystemMessage } from '@langchain/core/messages';
|
|
8
|
-
import { assign, createActor, fromPromise, setup } from 'xstate';
|
|
9
|
-
|
|
10
|
-
interface AgentState {
|
|
11
|
-
topic: string;
|
|
12
|
-
searchResults?: string;
|
|
13
|
-
article?: string;
|
|
14
|
-
critique?: string;
|
|
15
|
-
revisionCount: number;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function model() {
|
|
19
|
-
return new ChatOpenAI({
|
|
20
|
-
temperature: 0,
|
|
21
|
-
modelName: 'gpt-4-1106-preview',
|
|
22
|
-
openAIApiKey: process.env.OPENAI_API_KEY,
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
async function search({ topic }: Pick<AgentState, 'topic'>): Promise<string> {
|
|
27
|
-
const retriever = new TavilySearchAPIRetriever({
|
|
28
|
-
k: 10,
|
|
29
|
-
apiKey: process.env.TAVILY_API_KEY,
|
|
30
|
-
});
|
|
31
|
-
// let topic = state.agentState.topic;
|
|
32
|
-
// must be at least 5 characters long
|
|
33
|
-
if (topic.length < 5) {
|
|
34
|
-
topic = 'topic: ' + topic;
|
|
35
|
-
}
|
|
36
|
-
const docs = await retriever.invoke(topic);
|
|
37
|
-
return JSON.stringify(docs);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
async function curate(
|
|
41
|
-
input: Pick<AgentState, 'topic' | 'searchResults'>
|
|
42
|
-
): Promise<string> {
|
|
43
|
-
const response = await model().invoke(
|
|
44
|
-
[
|
|
45
|
-
new SystemMessage(
|
|
46
|
-
`You are a personal newspaper editor.
|
|
47
|
-
Your sole task is to return a list of URLs of the 5 most relevant articles for the provided topic or query as a JSON list of strings
|
|
48
|
-
in this format:
|
|
49
|
-
{
|
|
50
|
-
urls: ["url1", "url2", "url3", "url4", "url5"]
|
|
51
|
-
}
|
|
52
|
-
.`.replace(/\s+/g, ' ')
|
|
53
|
-
),
|
|
54
|
-
new HumanMessage(
|
|
55
|
-
`Today's date is ${new Date().toLocaleDateString('en-GB')}.
|
|
56
|
-
Topic or Query: ${input.topic}
|
|
57
|
-
|
|
58
|
-
Here is a list of articles:
|
|
59
|
-
${input.searchResults}`.replace(/\s+/g, ' ')
|
|
60
|
-
),
|
|
61
|
-
],
|
|
62
|
-
{
|
|
63
|
-
response_format: {
|
|
64
|
-
type: 'json_object',
|
|
65
|
-
},
|
|
66
|
-
}
|
|
67
|
-
);
|
|
68
|
-
const urls = JSON.parse(response.content as string).urls;
|
|
69
|
-
const searchResults = JSON.parse(input.searchResults!);
|
|
70
|
-
const newSearchResults = searchResults.filter((result: any) => {
|
|
71
|
-
return urls.includes(result.metadata.source);
|
|
72
|
-
});
|
|
73
|
-
return JSON.stringify(newSearchResults);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
async function critique(
|
|
77
|
-
input: Pick<AgentState, 'article' | 'critique'>
|
|
78
|
-
): Promise<string | undefined> {
|
|
79
|
-
let feedbackInstructions = '';
|
|
80
|
-
if (input.critique) {
|
|
81
|
-
feedbackInstructions =
|
|
82
|
-
`The writer has revised the article based on your previous critique: ${input.critique}
|
|
83
|
-
The writer might have left feedback for you encoded between <FEEDBACK> tags.
|
|
84
|
-
The feedback is only for you to see and will be removed from the final article.
|
|
85
|
-
`.replace(/\s+/g, ' ');
|
|
86
|
-
}
|
|
87
|
-
const response = await model().invoke([
|
|
88
|
-
new SystemMessage(
|
|
89
|
-
`You are a personal newspaper writing critique. Your sole purpose is to provide short feedback on a written
|
|
90
|
-
article so the writer will know what to fix.
|
|
91
|
-
Today's date is ${new Date().toLocaleDateString('en-GB')}
|
|
92
|
-
Your task is to provide a really short feedback on the article only if necessary.
|
|
93
|
-
if you think the article is good, please return [DONE].
|
|
94
|
-
you can provide feedback on the revised article or just
|
|
95
|
-
return [DONE] if you think the article is good.
|
|
96
|
-
Please return a string of your critique or [DONE].`.replace(/\s+/g, ' ')
|
|
97
|
-
),
|
|
98
|
-
new HumanMessage(
|
|
99
|
-
`${feedbackInstructions}
|
|
100
|
-
This is the article: ${input.article}`
|
|
101
|
-
),
|
|
102
|
-
]);
|
|
103
|
-
const content = response.content as string;
|
|
104
|
-
console.log('critique:', content);
|
|
105
|
-
return content.includes('[DONE]') ? undefined : content;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
async function write(
|
|
109
|
-
input: Pick<AgentState, 'searchResults' | 'topic'>
|
|
110
|
-
): Promise<string> {
|
|
111
|
-
const response = await model().invoke([
|
|
112
|
-
new SystemMessage(
|
|
113
|
-
`You are a personal newspaper writer. Your sole purpose is to write a well-written article about a
|
|
114
|
-
topic using a list of articles. Write 5 paragraphs in markdown.`.replace(
|
|
115
|
-
/\s+/g,
|
|
116
|
-
' '
|
|
117
|
-
)
|
|
118
|
-
),
|
|
119
|
-
new HumanMessage(
|
|
120
|
-
`Today's date is ${new Date().toLocaleDateString('en-GB')}.
|
|
121
|
-
Your task is to write a critically acclaimed article for me about the provided query or
|
|
122
|
-
topic based on the sources.
|
|
123
|
-
Here is a list of articles: ${input.searchResults}
|
|
124
|
-
This is the topic: ${input.topic}
|
|
125
|
-
Please return a well-written article based on the provided information.`.replace(
|
|
126
|
-
/\s+/g,
|
|
127
|
-
' '
|
|
128
|
-
)
|
|
129
|
-
),
|
|
130
|
-
]);
|
|
131
|
-
const content = response.content as string;
|
|
132
|
-
return content;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
async function revise(
|
|
136
|
-
input: Pick<AgentState, 'article' | 'critique'>
|
|
137
|
-
): Promise<string> {
|
|
138
|
-
const response = await model().invoke([
|
|
139
|
-
new SystemMessage(
|
|
140
|
-
`You are a personal newspaper editor. Your sole purpose is to edit a well-written article about a
|
|
141
|
-
topic based on given critique.`.replace(/\s+/g, ' ')
|
|
142
|
-
),
|
|
143
|
-
new HumanMessage(
|
|
144
|
-
`Your task is to edit the article based on the critique given.
|
|
145
|
-
This is the article: ${input.article}
|
|
146
|
-
This is the critique: ${input.critique}
|
|
147
|
-
Please return the edited article based on the critique given.
|
|
148
|
-
You may leave feedback about the critique encoded between <FEEDBACK> tags like this:
|
|
149
|
-
<FEEDBACK> here goes the feedback ...</FEEDBACK>`.replace(/\s+/g, ' ')
|
|
150
|
-
),
|
|
151
|
-
]);
|
|
152
|
-
const content = response.content as string;
|
|
153
|
-
return content;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
const machine = setup({
|
|
157
|
-
types: {
|
|
158
|
-
context: {} as AgentState,
|
|
159
|
-
},
|
|
160
|
-
actors: {
|
|
161
|
-
search: fromPromise(({ input }: { input: Pick<AgentState, 'topic'> }) => {
|
|
162
|
-
return search(input);
|
|
163
|
-
}),
|
|
164
|
-
curate: fromPromise(
|
|
165
|
-
({ input }: { input: Pick<AgentState, 'topic' | 'searchResults'> }) => {
|
|
166
|
-
return curate(input);
|
|
167
|
-
}
|
|
168
|
-
),
|
|
169
|
-
critique: fromPromise(
|
|
170
|
-
({ input }: { input: Pick<AgentState, 'article' | 'critique'> }) => {
|
|
171
|
-
return critique(input);
|
|
172
|
-
}
|
|
173
|
-
),
|
|
174
|
-
write: fromPromise(
|
|
175
|
-
({ input }: { input: Pick<AgentState, 'searchResults' | 'topic'> }) => {
|
|
176
|
-
return write(input);
|
|
177
|
-
}
|
|
178
|
-
),
|
|
179
|
-
revise: fromPromise(
|
|
180
|
-
({ input }: { input: Pick<AgentState, 'article' | 'critique'> }) => {
|
|
181
|
-
return revise(input);
|
|
182
|
-
}
|
|
183
|
-
),
|
|
184
|
-
},
|
|
185
|
-
}).createMachine({
|
|
186
|
-
context: {
|
|
187
|
-
topic: 'Orlando',
|
|
188
|
-
revisionCount: 0,
|
|
189
|
-
},
|
|
190
|
-
initial: 'search',
|
|
191
|
-
states: {
|
|
192
|
-
search: {
|
|
193
|
-
invoke: {
|
|
194
|
-
src: 'search',
|
|
195
|
-
input: ({ context }) => ({
|
|
196
|
-
topic: context.topic,
|
|
197
|
-
}),
|
|
198
|
-
onDone: {
|
|
199
|
-
actions: assign({
|
|
200
|
-
searchResults: ({ event }) => event.output,
|
|
201
|
-
}),
|
|
202
|
-
target: 'curate',
|
|
203
|
-
},
|
|
204
|
-
},
|
|
205
|
-
},
|
|
206
|
-
curate: {
|
|
207
|
-
invoke: {
|
|
208
|
-
src: 'curate',
|
|
209
|
-
input: ({ context }) => ({
|
|
210
|
-
topic: context.topic,
|
|
211
|
-
searchResults: context.searchResults!,
|
|
212
|
-
}),
|
|
213
|
-
onDone: {
|
|
214
|
-
actions: assign({
|
|
215
|
-
searchResults: ({ event }) => event.output,
|
|
216
|
-
}),
|
|
217
|
-
target: 'write',
|
|
218
|
-
},
|
|
219
|
-
},
|
|
220
|
-
},
|
|
221
|
-
write: {
|
|
222
|
-
invoke: {
|
|
223
|
-
src: 'write',
|
|
224
|
-
input: ({ context }) => ({
|
|
225
|
-
topic: context.topic,
|
|
226
|
-
searchResults: context.searchResults!,
|
|
227
|
-
}),
|
|
228
|
-
onDone: {
|
|
229
|
-
actions: assign({
|
|
230
|
-
article: ({ event }) => event.output,
|
|
231
|
-
}),
|
|
232
|
-
target: 'critique',
|
|
233
|
-
},
|
|
234
|
-
},
|
|
235
|
-
},
|
|
236
|
-
critique: {
|
|
237
|
-
invoke: {
|
|
238
|
-
src: 'critique',
|
|
239
|
-
input: ({ context }) => ({
|
|
240
|
-
article: context.article!,
|
|
241
|
-
critique: context.critique,
|
|
242
|
-
}),
|
|
243
|
-
onDone: [
|
|
244
|
-
{
|
|
245
|
-
guard: ({ event }) => event.output === undefined,
|
|
246
|
-
target: 'done',
|
|
247
|
-
},
|
|
248
|
-
{
|
|
249
|
-
actions: assign({
|
|
250
|
-
article: ({ event }) => event.output,
|
|
251
|
-
}),
|
|
252
|
-
target: 'revise',
|
|
253
|
-
},
|
|
254
|
-
],
|
|
255
|
-
},
|
|
256
|
-
},
|
|
257
|
-
revise: {
|
|
258
|
-
always: {
|
|
259
|
-
guard: ({ context }) => context.revisionCount > 3,
|
|
260
|
-
target: 'done',
|
|
261
|
-
},
|
|
262
|
-
entry: assign({
|
|
263
|
-
revisionCount: ({ context }) => context.revisionCount + 1,
|
|
264
|
-
}),
|
|
265
|
-
invoke: {
|
|
266
|
-
src: 'revise',
|
|
267
|
-
input: ({ context }) => ({
|
|
268
|
-
article: context.article!,
|
|
269
|
-
critique: context.critique,
|
|
270
|
-
}),
|
|
271
|
-
onDone: {
|
|
272
|
-
actions: assign({
|
|
273
|
-
article: ({ event }) => event.output,
|
|
274
|
-
}),
|
|
275
|
-
target: 'revise',
|
|
276
|
-
reenter: true,
|
|
277
|
-
},
|
|
278
|
-
},
|
|
279
|
-
},
|
|
280
|
-
done: {
|
|
281
|
-
type: 'final',
|
|
282
|
-
},
|
|
283
|
-
},
|
|
284
|
-
output: ({ context }) => context.article,
|
|
285
|
-
});
|
|
286
|
-
|
|
287
|
-
const actor = createActor(machine, {
|
|
288
|
-
// inspect: (inspEv) => {
|
|
289
|
-
// if (inspEv.type === '@xstate.event') {
|
|
290
|
-
// console.log(JSON.stringify(inspEv.event, null, 2));
|
|
291
|
-
// }
|
|
292
|
-
// },
|
|
293
|
-
});
|
|
294
|
-
|
|
295
|
-
actor.subscribe({
|
|
296
|
-
next: (s) => {
|
|
297
|
-
console.log('State:', s.value);
|
|
298
|
-
console.log(
|
|
299
|
-
'Context:',
|
|
300
|
-
JSON.stringify(
|
|
301
|
-
s.context,
|
|
302
|
-
(k, v) => {
|
|
303
|
-
if (typeof v === 'string') {
|
|
304
|
-
// truncate if longer than 50 chars
|
|
305
|
-
return v.length > 50 ? `${v.slice(0, 50)}...` : v;
|
|
306
|
-
}
|
|
307
|
-
return v;
|
|
308
|
-
},
|
|
309
|
-
2
|
|
310
|
-
)
|
|
311
|
-
);
|
|
312
|
-
},
|
|
313
|
-
complete: () => {
|
|
314
|
-
console.log(actor.getSnapshot().output);
|
|
315
|
-
},
|
|
316
|
-
error: (err) => {
|
|
317
|
-
console.error(err);
|
|
318
|
-
},
|
|
319
|
-
});
|
|
320
|
-
|
|
321
|
-
actor.start();
|
|
322
|
-
|
|
323
|
-
// keep the process alive by invoking a promise that never resolves
|
|
324
|
-
new Promise(() => {});
|
package/examples/number.ts
DELETED
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
import { createAgent, fromDecision } from '../src';
|
|
2
|
-
import { assign, createActor, log, setup } from 'xstate';
|
|
3
|
-
import { z } from 'zod';
|
|
4
|
-
import { openai } from '@ai-sdk/openai';
|
|
5
|
-
import { getFromTerminal } from './helpers/helpers';
|
|
6
|
-
|
|
7
|
-
const agent = createAgent({
|
|
8
|
-
name: 'number-guesser',
|
|
9
|
-
model: openai('gpt-3.5-turbo-1106'),
|
|
10
|
-
events: {
|
|
11
|
-
'agent.guess': z.object({
|
|
12
|
-
number: z.number().min(1).max(10).describe('The number guessed'),
|
|
13
|
-
}),
|
|
14
|
-
},
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
const machine = setup({
|
|
18
|
-
types: {
|
|
19
|
-
context: {} as {
|
|
20
|
-
previousGuesses: number[];
|
|
21
|
-
answer: number | null;
|
|
22
|
-
},
|
|
23
|
-
events: agent.types.events,
|
|
24
|
-
},
|
|
25
|
-
actors: {
|
|
26
|
-
agent: fromDecision(agent),
|
|
27
|
-
getFromTerminal,
|
|
28
|
-
},
|
|
29
|
-
}).createMachine({
|
|
30
|
-
context: {
|
|
31
|
-
answer: null,
|
|
32
|
-
previousGuesses: [],
|
|
33
|
-
},
|
|
34
|
-
initial: 'providing',
|
|
35
|
-
states: {
|
|
36
|
-
providing: {
|
|
37
|
-
invoke: {
|
|
38
|
-
src: 'getFromTerminal',
|
|
39
|
-
input: 'Enter a number between 1 and 10',
|
|
40
|
-
onDone: {
|
|
41
|
-
actions: assign({
|
|
42
|
-
answer: (x) => +x.event.output,
|
|
43
|
-
}),
|
|
44
|
-
target: 'guessing',
|
|
45
|
-
},
|
|
46
|
-
},
|
|
47
|
-
},
|
|
48
|
-
guessing: {
|
|
49
|
-
always: {
|
|
50
|
-
guard: ({ context }) =>
|
|
51
|
-
context.answer === context.previousGuesses.at(-1),
|
|
52
|
-
target: 'winner',
|
|
53
|
-
},
|
|
54
|
-
invoke: {
|
|
55
|
-
src: 'agent',
|
|
56
|
-
input: ({ context }) => ({
|
|
57
|
-
goal: `
|
|
58
|
-
Guess the number between 1 and 10. The previous guesses were ${
|
|
59
|
-
context.previousGuesses.length
|
|
60
|
-
? context.previousGuesses.join(', ')
|
|
61
|
-
: 'not made yet'
|
|
62
|
-
} and the last result was ${
|
|
63
|
-
context.previousGuesses.length === 0
|
|
64
|
-
? 'not given yet'
|
|
65
|
-
: context.previousGuesses.at(-1)! - context.answer! > 0
|
|
66
|
-
? 'too high'
|
|
67
|
-
: 'too low'
|
|
68
|
-
}.
|
|
69
|
-
`,
|
|
70
|
-
}),
|
|
71
|
-
},
|
|
72
|
-
on: {
|
|
73
|
-
'agent.guess': {
|
|
74
|
-
actions: [
|
|
75
|
-
assign({
|
|
76
|
-
previousGuesses: ({ context, event }) => [
|
|
77
|
-
...context.previousGuesses,
|
|
78
|
-
event.number,
|
|
79
|
-
],
|
|
80
|
-
}),
|
|
81
|
-
log((x) => x.event.number),
|
|
82
|
-
],
|
|
83
|
-
target: 'guessing',
|
|
84
|
-
reenter: true,
|
|
85
|
-
},
|
|
86
|
-
},
|
|
87
|
-
},
|
|
88
|
-
winner: {
|
|
89
|
-
entry: log('You guessed the correct number!'),
|
|
90
|
-
type: 'final',
|
|
91
|
-
},
|
|
92
|
-
},
|
|
93
|
-
exit: () => {
|
|
94
|
-
process.exit();
|
|
95
|
-
},
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
const actor = createActor(machine, {
|
|
99
|
-
input: { answer: 4 },
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
actor.start();
|
package/examples/raffle.ts
DELETED
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
|
-
import { createAgent, fromDecision } from '../src';
|
|
3
|
-
import { openai } from '@ai-sdk/openai';
|
|
4
|
-
import { assign, createActor, log, setup } from 'xstate';
|
|
5
|
-
import { getFromTerminal } from './helpers/helpers';
|
|
6
|
-
|
|
7
|
-
const agent = createAgent({
|
|
8
|
-
name: 'raffle-chooser',
|
|
9
|
-
model: openai('gpt-4-turbo'),
|
|
10
|
-
events: {
|
|
11
|
-
'agent.collectEntries': z.object({}).describe('Collect more entries'),
|
|
12
|
-
'agent.draw': z.object({}).describe('Draw a winner'),
|
|
13
|
-
'agent.reportWinner': z.object({
|
|
14
|
-
winningEntry: z.string().describe('The winning entry'),
|
|
15
|
-
firstRunnerUp: z.string().describe('The first runner up entry'),
|
|
16
|
-
secondRunnerUp: z.string().describe('The second runner up entry'),
|
|
17
|
-
explanation: z
|
|
18
|
-
.string()
|
|
19
|
-
.describe('Explanation for why you chose the winning entry'),
|
|
20
|
-
}),
|
|
21
|
-
},
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
const machine = setup({
|
|
25
|
-
types: {
|
|
26
|
-
context: {} as {
|
|
27
|
-
lastInput: string | null;
|
|
28
|
-
entries: string[];
|
|
29
|
-
},
|
|
30
|
-
events: {} as typeof agent.types.events | { type: 'draw' },
|
|
31
|
-
},
|
|
32
|
-
actors: { agent: fromDecision(agent), getFromTerminal },
|
|
33
|
-
}).createMachine({
|
|
34
|
-
/** @xstate-layout N4IgpgJg5mDOIC5QAoC2BDAxgCwJYDswBKAOjHwBcwAnAqAYggHtCSCA3JgazBLSzyFS5KrXxQEHJpnQVcLANoAGALrKViUAAcmsXHJaaQAD0QAmAOwBmEmYCsADgCMZgJxmALE4ceHSuwA0IACe5gBsZiR29q5KPnaOrmEOAL4pQfw4BMQkEGCiqAR09OgwlCSYTAA2VWCYFACilLRw6kY6egb4RqYIZg52JB52Ya5jSq5+To4eQaEILhYkA0qjVlYeSlsOYWFpGRhZQrn5NIX4xaUiudToAO5tSCAd+vLdT727SiRKHsl2FjCwxcrlmIUQTl2ticHk26ycMPsqXSIEyghyEFud0uZQoJGoYB01AoAHUCIRqI9tLpXoYPohXBYnCQnP4HK4nFYLF5vK45hCPDYmVtdk5uYzuWkUfgmHl4E80dkiO0aV0eogALRhfkIDWDMZjJn9MxhKwjSb7VGHdHCSg0OgqzpvdUIDxmHWc5l2caeKxhVYDFyWxXHPIFIriR2096gXoeVw2VmgiIOBzWJRiqwe2FRdzcpRWE0JPPB61Km73B1PF5q+kIPweEhWMWjCz+BJKJketwkQEWCyuOywv0WaJ2UsCcvY-AUqO12MQls-Ue+Px2KzspweoHLXxsjO-eNmMxSlJAA */
|
|
35
|
-
context: {
|
|
36
|
-
lastInput: null,
|
|
37
|
-
entries: [],
|
|
38
|
-
},
|
|
39
|
-
initial: 'entering',
|
|
40
|
-
states: {
|
|
41
|
-
entering: {
|
|
42
|
-
entry: log(({ context }) => context.entries),
|
|
43
|
-
invoke: {
|
|
44
|
-
src: 'getFromTerminal',
|
|
45
|
-
input: 'What technology are you most interested in right now?',
|
|
46
|
-
onDone: [
|
|
47
|
-
{
|
|
48
|
-
actions: assign({
|
|
49
|
-
lastInput: ({ event }) => event.output,
|
|
50
|
-
}),
|
|
51
|
-
target: 'determining',
|
|
52
|
-
},
|
|
53
|
-
],
|
|
54
|
-
},
|
|
55
|
-
},
|
|
56
|
-
determining: {
|
|
57
|
-
invoke: {
|
|
58
|
-
src: 'agent',
|
|
59
|
-
input: {
|
|
60
|
-
context: true,
|
|
61
|
-
goal: 'If the last input explicitly says to end the drawing and/or choose a winner, start the drawing process. Otherwise, get more entries.',
|
|
62
|
-
},
|
|
63
|
-
},
|
|
64
|
-
on: {
|
|
65
|
-
'agent.collectEntries': {
|
|
66
|
-
target: 'entering',
|
|
67
|
-
actions: assign({
|
|
68
|
-
entries: ({ context }) => [...context.entries, context.lastInput!],
|
|
69
|
-
lastInput: null,
|
|
70
|
-
}),
|
|
71
|
-
},
|
|
72
|
-
'agent.draw': 'drawing',
|
|
73
|
-
},
|
|
74
|
-
},
|
|
75
|
-
drawing: {
|
|
76
|
-
entry: log('And the winner is...'),
|
|
77
|
-
invoke: {
|
|
78
|
-
src: 'agent',
|
|
79
|
-
input: {
|
|
80
|
-
context: true,
|
|
81
|
-
goal: 'Choose the technology that sounds most exciting to you from the entries. Be as unbiased as possible in your choice. Explain why you chose the winning entry.',
|
|
82
|
-
},
|
|
83
|
-
},
|
|
84
|
-
on: {
|
|
85
|
-
'agent.reportWinner': {
|
|
86
|
-
actions: log(
|
|
87
|
-
({ event }) =>
|
|
88
|
-
`\nššš ${event.winningEntry} ššš\n\n${event.explanation}`
|
|
89
|
-
),
|
|
90
|
-
target: 'winner',
|
|
91
|
-
},
|
|
92
|
-
},
|
|
93
|
-
},
|
|
94
|
-
winner: {
|
|
95
|
-
type: 'final',
|
|
96
|
-
},
|
|
97
|
-
},
|
|
98
|
-
exit: () => {
|
|
99
|
-
process.exit(0);
|
|
100
|
-
},
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
const actor = createActor(machine);
|
|
104
|
-
|
|
105
|
-
actor.start();
|
package/examples/sandbox.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
|
-
import { createAgent } from '../src';
|
|
3
|
-
import { openai } from '@ai-sdk/openai';
|
|
4
|
-
import { createMachine } from 'xstate';
|
|
5
|
-
|
|
6
|
-
const agent = createAgent({
|
|
7
|
-
model: openai('gpt-4o'),
|
|
8
|
-
events: {
|
|
9
|
-
doSomething: z.object({}).describe('Do something'),
|
|
10
|
-
},
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
async function main() {
|
|
14
|
-
const machine = createMachine({
|
|
15
|
-
on: {
|
|
16
|
-
doSomething: {},
|
|
17
|
-
},
|
|
18
|
-
});
|
|
19
|
-
const result = await agent.decide({
|
|
20
|
-
goal: 'Do not do anything',
|
|
21
|
-
state: { value: {}, context: {} },
|
|
22
|
-
machine,
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
console.log(result);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
main();
|
package/examples/simple.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { createAgent, fromDecision } from '../src';
|
|
2
|
-
import { z } from 'zod';
|
|
3
|
-
import { setup, createActor } from 'xstate';
|
|
4
|
-
import { openai } from '@ai-sdk/openai';
|
|
5
|
-
|
|
6
|
-
const agent = createAgent({
|
|
7
|
-
name: 'simple',
|
|
8
|
-
model: openai('gpt-3.5-turbo-16k-0613'),
|
|
9
|
-
events: {
|
|
10
|
-
'agent.thought': z.object({
|
|
11
|
-
text: z.string().describe('The text of the thought'),
|
|
12
|
-
}),
|
|
13
|
-
},
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
const machine = setup({
|
|
17
|
-
actors: { agent: fromDecision(agent) },
|
|
18
|
-
}).createMachine({
|
|
19
|
-
initial: 'thinking',
|
|
20
|
-
states: {
|
|
21
|
-
thinking: {
|
|
22
|
-
invoke: {
|
|
23
|
-
src: 'agent',
|
|
24
|
-
input: 'Think about a random topic, and then share that thought.',
|
|
25
|
-
},
|
|
26
|
-
on: {
|
|
27
|
-
'agent.thought': {
|
|
28
|
-
actions: ({ event }) => console.log(event.text),
|
|
29
|
-
target: 'thought',
|
|
30
|
-
},
|
|
31
|
-
},
|
|
32
|
-
},
|
|
33
|
-
thought: {
|
|
34
|
-
type: 'final',
|
|
35
|
-
},
|
|
36
|
-
},
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
const actor = createActor(machine).start();
|