@statelyai/agent 1.1.5 → 2.0.0-alpha.10
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 +342 -0
- package/dist/ai-sdk.d.cts +206 -0
- package/dist/ai-sdk.d.mts +206 -0
- package/dist/ai-sdk.mjs +331 -0
- package/dist/cli.cjs +64 -0
- package/dist/cli.d.cts +1 -0
- package/dist/cli.d.mts +1 -0
- package/dist/cli.mjs +65 -0
- package/dist/decision-CQdrKc8k.mjs +978 -0
- package/dist/decision-b-lkcs4L.cjs +1295 -0
- package/dist/index.cjs +59 -0
- package/dist/index.d.cts +212 -0
- package/dist/index.d.mts +201 -417
- package/dist/index.mjs +4 -583
- 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/run-agent-BFMkuI1F.cjs +1599 -0
- package/dist/run-agent-BVMd--1l.d.cts +1103 -0
- package/dist/run-agent-BoAOq_0Z.mjs +1540 -0
- package/dist/run-agent-DzYJb3CK.d.mts +1103 -0
- package/dist/src-D-77Ha5p.cjs +1087 -0
- package/dist/src-DUeIFjv-.mjs +1016 -0
- package/dist/text-logic-C7WJpCIc.d.mts +710 -0
- package/dist/text-logic-CZjyACzQ.d.cts +710 -0
- package/dist/types-C9QiMjre.d.cts +219 -0
- package/dist/types-qm00QF91.d.mts +219 -0
- package/dist/utils-Dri7aeEG.d.cts +108 -0
- package/dist/utils-Y6GDRGGE.d.mts +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 +110 -29
- package/readme.md +143 -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 -215
- package/dist/index.d.ts +0 -428
- package/dist/index.js +0 -616
- 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 -236
- 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/cot.ts
DELETED
|
@@ -1,89 +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: 'chain-of-thought',
|
|
9
|
-
model: openai('gpt-4o'),
|
|
10
|
-
events: {
|
|
11
|
-
'agent.think': z.object({
|
|
12
|
-
thought: z
|
|
13
|
-
.string()
|
|
14
|
-
.describe('The thought process to answering the question'),
|
|
15
|
-
}),
|
|
16
|
-
'agent.answer': z.object({
|
|
17
|
-
answer: z.string().describe('The answer to the question'),
|
|
18
|
-
}),
|
|
19
|
-
},
|
|
20
|
-
context: {
|
|
21
|
-
question: z.string().nullable(),
|
|
22
|
-
thought: z.string().nullable(),
|
|
23
|
-
},
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
const machine = setup({
|
|
27
|
-
types: agent.types,
|
|
28
|
-
actors: { agent: fromDecision(agent), getFromTerminal },
|
|
29
|
-
}).createMachine({
|
|
30
|
-
initial: 'asking',
|
|
31
|
-
context: {
|
|
32
|
-
question: null,
|
|
33
|
-
thought: null,
|
|
34
|
-
},
|
|
35
|
-
states: {
|
|
36
|
-
asking: {
|
|
37
|
-
invoke: {
|
|
38
|
-
src: 'getFromTerminal',
|
|
39
|
-
input: 'What would you like to ask?',
|
|
40
|
-
onDone: {
|
|
41
|
-
actions: assign({
|
|
42
|
-
question: (x) => x.event.output,
|
|
43
|
-
}),
|
|
44
|
-
target: 'thinking',
|
|
45
|
-
},
|
|
46
|
-
},
|
|
47
|
-
},
|
|
48
|
-
thinking: {
|
|
49
|
-
invoke: {
|
|
50
|
-
src: 'agent',
|
|
51
|
-
input: (x) => ({
|
|
52
|
-
context: x.context,
|
|
53
|
-
goal: 'Answer the question. Think step-by-step.',
|
|
54
|
-
}),
|
|
55
|
-
},
|
|
56
|
-
on: {
|
|
57
|
-
'agent.think': {
|
|
58
|
-
actions: [
|
|
59
|
-
log((x) => x.event.thought),
|
|
60
|
-
assign({
|
|
61
|
-
thought: (x) => x.event.thought,
|
|
62
|
-
}),
|
|
63
|
-
],
|
|
64
|
-
target: 'answering',
|
|
65
|
-
},
|
|
66
|
-
},
|
|
67
|
-
},
|
|
68
|
-
answering: {
|
|
69
|
-
invoke: {
|
|
70
|
-
src: 'agent',
|
|
71
|
-
input: (x) => ({
|
|
72
|
-
context: x.context,
|
|
73
|
-
goal: 'Answer the question',
|
|
74
|
-
}),
|
|
75
|
-
},
|
|
76
|
-
on: {
|
|
77
|
-
'agent.answer': {
|
|
78
|
-
actions: [log((x) => x.event.answer)],
|
|
79
|
-
target: 'answered',
|
|
80
|
-
},
|
|
81
|
-
},
|
|
82
|
-
},
|
|
83
|
-
answered: {
|
|
84
|
-
type: 'final',
|
|
85
|
-
},
|
|
86
|
-
},
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
createActor(machine).start();
|
package/examples/email.ts
DELETED
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
|
-
import { createAgent, fromDecision } from '../src';
|
|
3
|
-
import { openai } from '@ai-sdk/openai';
|
|
4
|
-
import { assign, createActor, setup } from 'xstate';
|
|
5
|
-
import { getFromTerminal } from './helpers/helpers';
|
|
6
|
-
|
|
7
|
-
const agent = createAgent({
|
|
8
|
-
name: 'email',
|
|
9
|
-
model: openai('gpt-4'),
|
|
10
|
-
events: {
|
|
11
|
-
askForClarification: z.object({
|
|
12
|
-
questions: z.array(z.string()).describe('The questions to ask the agent'),
|
|
13
|
-
}),
|
|
14
|
-
submitEmail: z.object({
|
|
15
|
-
email: z.string().describe('The email to submit'),
|
|
16
|
-
}),
|
|
17
|
-
},
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
const machine = setup({
|
|
21
|
-
types: {
|
|
22
|
-
events: agent.types.events,
|
|
23
|
-
input: {} as {
|
|
24
|
-
email: string;
|
|
25
|
-
instructions: string;
|
|
26
|
-
},
|
|
27
|
-
context: {} as {
|
|
28
|
-
email: string;
|
|
29
|
-
instructions: string;
|
|
30
|
-
clarifications: string[];
|
|
31
|
-
replyEmail: string | null;
|
|
32
|
-
},
|
|
33
|
-
},
|
|
34
|
-
actors: { agent: fromDecision(agent), getFromTerminal },
|
|
35
|
-
}).createMachine({
|
|
36
|
-
initial: 'checking',
|
|
37
|
-
context: (x) => ({
|
|
38
|
-
email: x.input.email,
|
|
39
|
-
instructions: x.input.instructions,
|
|
40
|
-
clarifications: [],
|
|
41
|
-
replyEmail: null,
|
|
42
|
-
}),
|
|
43
|
-
states: {
|
|
44
|
-
checking: {
|
|
45
|
-
invoke: {
|
|
46
|
-
src: 'agent',
|
|
47
|
-
input: (x) => ({
|
|
48
|
-
context: {
|
|
49
|
-
email: x.context.email,
|
|
50
|
-
instructions: x.context.instructions,
|
|
51
|
-
clarifications: x.context.clarifications,
|
|
52
|
-
},
|
|
53
|
-
messages: agent.getMessages(),
|
|
54
|
-
goal: 'Respond to the email given the instructions and the provided clarifications. If not enough information is provided, ask for clarification. Otherwise, if you are absolutely sure that there is no ambiguous or missing information, create and submit a response email.',
|
|
55
|
-
}),
|
|
56
|
-
},
|
|
57
|
-
on: {
|
|
58
|
-
askForClarification: {
|
|
59
|
-
actions: (x) => console.log(x.event.questions.join('\n')),
|
|
60
|
-
target: 'clarifying',
|
|
61
|
-
},
|
|
62
|
-
submitEmail: {
|
|
63
|
-
target: 'submitting',
|
|
64
|
-
},
|
|
65
|
-
},
|
|
66
|
-
},
|
|
67
|
-
clarifying: {
|
|
68
|
-
invoke: {
|
|
69
|
-
src: 'getFromTerminal',
|
|
70
|
-
input: `Please provide answers to the questions above`,
|
|
71
|
-
onDone: {
|
|
72
|
-
actions: assign({
|
|
73
|
-
clarifications: (x) =>
|
|
74
|
-
x.context.clarifications.concat(x.event.output),
|
|
75
|
-
}),
|
|
76
|
-
target: 'checking',
|
|
77
|
-
},
|
|
78
|
-
},
|
|
79
|
-
},
|
|
80
|
-
submitting: {
|
|
81
|
-
invoke: {
|
|
82
|
-
src: 'agent',
|
|
83
|
-
input: ({ context }) => ({
|
|
84
|
-
context: {
|
|
85
|
-
email: context.email,
|
|
86
|
-
instructions: context.instructions,
|
|
87
|
-
clarifications: context.clarifications,
|
|
88
|
-
},
|
|
89
|
-
goal: `Create and submit an email based on the instructions.`,
|
|
90
|
-
}),
|
|
91
|
-
},
|
|
92
|
-
on: {
|
|
93
|
-
submitEmail: {
|
|
94
|
-
actions: assign({
|
|
95
|
-
replyEmail: ({ event }) => event.email,
|
|
96
|
-
}),
|
|
97
|
-
target: 'done',
|
|
98
|
-
},
|
|
99
|
-
},
|
|
100
|
-
},
|
|
101
|
-
done: {
|
|
102
|
-
type: 'final',
|
|
103
|
-
entry: (x) => console.log(x.context.replyEmail),
|
|
104
|
-
},
|
|
105
|
-
},
|
|
106
|
-
exit: () => {
|
|
107
|
-
console.log('End of conversation.');
|
|
108
|
-
process.exit();
|
|
109
|
-
},
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
createActor(machine, {
|
|
113
|
-
input: {
|
|
114
|
-
email: 'That sounds great! When are you available?',
|
|
115
|
-
instructions:
|
|
116
|
-
'Tell them exactly when I am available. Address them by his full (first and last) name.',
|
|
117
|
-
},
|
|
118
|
-
}).start();
|
package/examples/example.ts
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
|
-
import { createAgent, fromDecision, fromText } from '../src';
|
|
3
|
-
import { openai } from '@ai-sdk/openai';
|
|
4
|
-
import { assign, createActor, setup } from 'xstate';
|
|
5
|
-
|
|
6
|
-
const agent = createAgent({
|
|
7
|
-
name: 'example',
|
|
8
|
-
model: openai('gpt-4-turbo'),
|
|
9
|
-
events: {
|
|
10
|
-
'agent.englishSummary': z.object({
|
|
11
|
-
text: z.string().describe('The summary in English'),
|
|
12
|
-
}),
|
|
13
|
-
'agent.spanishSummary': z.object({
|
|
14
|
-
text: z.string().describe('The summary in Spanish'),
|
|
15
|
-
}),
|
|
16
|
-
},
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
const machine = setup({
|
|
20
|
-
types: {
|
|
21
|
-
events: agent.types.events,
|
|
22
|
-
},
|
|
23
|
-
actors: { agent: fromDecision(agent), summarizer: fromText(agent) },
|
|
24
|
-
}).createMachine({
|
|
25
|
-
initial: 'summarizing',
|
|
26
|
-
context: {
|
|
27
|
-
patientVisit:
|
|
28
|
-
'During my visit, the doctor explained my condition clearly. She listened to my concerns and recommended a treatment plan. My condition was diagnosed as X after a series of tests. I feel relieved to have a clear path forward with managing my health. Also, the staff were very friendly and helpful at check-in and check-out. Furthermore, the facilities were clean and well-maintained.',
|
|
29
|
-
},
|
|
30
|
-
states: {
|
|
31
|
-
summarizing: {
|
|
32
|
-
invoke: [
|
|
33
|
-
{
|
|
34
|
-
src: 'summarizer',
|
|
35
|
-
input: (x) => ({
|
|
36
|
-
context: x.context,
|
|
37
|
-
prompt:
|
|
38
|
-
'Summarize the patient visit in a single sentence. The summary should be in English.',
|
|
39
|
-
}),
|
|
40
|
-
onDone: {
|
|
41
|
-
actions: assign({
|
|
42
|
-
englishSummary: ({ event }) => event.output.text,
|
|
43
|
-
}),
|
|
44
|
-
},
|
|
45
|
-
},
|
|
46
|
-
{
|
|
47
|
-
src: 'summarizer',
|
|
48
|
-
input: (x) => ({
|
|
49
|
-
context: x.context,
|
|
50
|
-
prompt:
|
|
51
|
-
'Summarize the patient visit in a single sentence. The summary should be in Spanish.',
|
|
52
|
-
}),
|
|
53
|
-
onDone: {
|
|
54
|
-
actions: assign({
|
|
55
|
-
spanishSummary: ({ event }) => event.output.text,
|
|
56
|
-
}),
|
|
57
|
-
},
|
|
58
|
-
},
|
|
59
|
-
],
|
|
60
|
-
always: {
|
|
61
|
-
guard: ({ context }) =>
|
|
62
|
-
context.englishSummary && context.spanishSummary,
|
|
63
|
-
target: 'summarized',
|
|
64
|
-
},
|
|
65
|
-
},
|
|
66
|
-
summarized: {
|
|
67
|
-
entry: ({ context }) => {
|
|
68
|
-
console.log(context.englishSummary);
|
|
69
|
-
console.log(context.spanishSummary);
|
|
70
|
-
},
|
|
71
|
-
},
|
|
72
|
-
},
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
const actor = createActor(machine);
|
|
76
|
-
|
|
77
|
-
actor.subscribe((s) => {
|
|
78
|
-
console.log(s.context);
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
actor.start();
|
package/examples/goal.ts
DELETED
|
@@ -1,94 +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: 'goal',
|
|
9
|
-
model: openai('gpt-4-turbo'),
|
|
10
|
-
events: {
|
|
11
|
-
'agent.createGoal': z.object({
|
|
12
|
-
goal: z.string().describe('The goal for the conversation'),
|
|
13
|
-
}),
|
|
14
|
-
'agent.respond': z.object({
|
|
15
|
-
response: z.string().describe('The response from the agent'),
|
|
16
|
-
}),
|
|
17
|
-
},
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
const decider = fromDecision(agent);
|
|
21
|
-
|
|
22
|
-
const machine = setup({
|
|
23
|
-
types: {
|
|
24
|
-
context: {} as {
|
|
25
|
-
question: string | null;
|
|
26
|
-
goal: string | null;
|
|
27
|
-
},
|
|
28
|
-
events: agent.types.events,
|
|
29
|
-
},
|
|
30
|
-
actors: { decider, getFromTerminal },
|
|
31
|
-
}).createMachine({
|
|
32
|
-
initial: 'gettingQuestion',
|
|
33
|
-
context: {
|
|
34
|
-
question: null,
|
|
35
|
-
goal: null,
|
|
36
|
-
},
|
|
37
|
-
states: {
|
|
38
|
-
gettingQuestion: {
|
|
39
|
-
invoke: {
|
|
40
|
-
src: 'getFromTerminal',
|
|
41
|
-
input: 'What would you like to ask?',
|
|
42
|
-
onDone: {
|
|
43
|
-
actions: assign({
|
|
44
|
-
question: ({ event }) => event.output,
|
|
45
|
-
}),
|
|
46
|
-
target: 'makingGoal',
|
|
47
|
-
},
|
|
48
|
-
},
|
|
49
|
-
},
|
|
50
|
-
makingGoal: {
|
|
51
|
-
invoke: {
|
|
52
|
-
src: 'decider',
|
|
53
|
-
input: {
|
|
54
|
-
context: true,
|
|
55
|
-
goal: 'Determine what the user wants to accomplish. What is their ideal goal state? ',
|
|
56
|
-
maxRetries: 3,
|
|
57
|
-
},
|
|
58
|
-
},
|
|
59
|
-
on: {
|
|
60
|
-
'agent.createGoal': {
|
|
61
|
-
actions: [
|
|
62
|
-
assign({
|
|
63
|
-
goal: ({ event }) => event.goal,
|
|
64
|
-
}),
|
|
65
|
-
log((x) => x.event),
|
|
66
|
-
],
|
|
67
|
-
target: 'responding',
|
|
68
|
-
},
|
|
69
|
-
},
|
|
70
|
-
},
|
|
71
|
-
responding: {
|
|
72
|
-
invoke: {
|
|
73
|
-
src: 'decider',
|
|
74
|
-
input: {
|
|
75
|
-
context: true,
|
|
76
|
-
goal: 'Answer the question to achieve the stated goal, unless the goal is impossible to achieve.',
|
|
77
|
-
maxRetries: 3,
|
|
78
|
-
},
|
|
79
|
-
},
|
|
80
|
-
on: {
|
|
81
|
-
'agent.respond': {
|
|
82
|
-
actions: log(({ event }) => event),
|
|
83
|
-
},
|
|
84
|
-
},
|
|
85
|
-
},
|
|
86
|
-
responded: {
|
|
87
|
-
type: 'final',
|
|
88
|
-
},
|
|
89
|
-
},
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
const actor = createActor(machine);
|
|
93
|
-
|
|
94
|
-
actor.start();
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { fromPromise } from 'xstate';
|
|
2
|
-
|
|
3
|
-
export const getFromTerminal = fromPromise<string, string>(
|
|
4
|
-
async ({ input }) => {
|
|
5
|
-
const topic = await new Promise<string>((res) => {
|
|
6
|
-
console.log(input + '\n');
|
|
7
|
-
const listener = (data: Buffer) => {
|
|
8
|
-
const result = data.toString().trim();
|
|
9
|
-
process.stdin.off('data', listener);
|
|
10
|
-
res(result);
|
|
11
|
-
};
|
|
12
|
-
process.stdin.on('data', listener);
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
return topic;
|
|
16
|
-
}
|
|
17
|
-
);
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
// Adapted from https://stackoverflow.com/questions/34848505/how-to-make-a-loading-animation-in-console-application-written-in-javascript-or
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Create and display a loader in the console.
|
|
5
|
-
*
|
|
6
|
-
* @param {string} [text=""] Text to display after loader
|
|
7
|
-
* @param {array.<string>} [chars=["⠙", "⠘", "⠰", "⠴", "⠤", "⠦", "⠆", "⠃", "⠋", "⠉"]]
|
|
8
|
-
* Array of characters representing loader steps
|
|
9
|
-
* @param {number} [delay=100] Delay in ms between loader steps
|
|
10
|
-
* @example
|
|
11
|
-
* let loader = loadingAnimation("Loading…");
|
|
12
|
-
*
|
|
13
|
-
* // Stop loader after 1 second
|
|
14
|
-
* setTimeout(() => clearInterval(loader), 1000);
|
|
15
|
-
* @returns {number} An interval that can be cleared to stop the animation
|
|
16
|
-
*/
|
|
17
|
-
export function loadingAnimation(
|
|
18
|
-
text: string = '',
|
|
19
|
-
chars: Array<string> = ['⠙', '⠘', '⠰', '⠴', '⠤', '⠦', '⠆', '⠃', '⠋', '⠉'],
|
|
20
|
-
delay: number = 100
|
|
21
|
-
) {
|
|
22
|
-
let x = 0;
|
|
23
|
-
|
|
24
|
-
const i = setInterval(function () {
|
|
25
|
-
process.stdout.write('\r' + chars[x++] + ' ' + text);
|
|
26
|
-
x = x % chars.length;
|
|
27
|
-
}, delay);
|
|
28
|
-
|
|
29
|
-
return {
|
|
30
|
-
stop: () => clearInterval(i),
|
|
31
|
-
};
|
|
32
|
-
}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import dotenv from 'dotenv';
|
|
2
|
-
import { existsSync, readdirSync } from 'fs';
|
|
3
|
-
dotenv.config();
|
|
4
|
-
|
|
5
|
-
function showExamples() {
|
|
6
|
-
const exampleFiles = readdirSync('./examples', { withFileTypes: true });
|
|
7
|
-
exampleFiles.forEach((file) => {
|
|
8
|
-
if (file.isDirectory()) return;
|
|
9
|
-
const exampleName = file.name.split('.')[0];
|
|
10
|
-
console.log(`- ${exampleName}`);
|
|
11
|
-
});
|
|
12
|
-
process.exit();
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
const exampleParams = process.argv.slice(2);
|
|
16
|
-
if (exampleParams.length === 0) {
|
|
17
|
-
console.error('No example specified, you can choose from:');
|
|
18
|
-
showExamples();
|
|
19
|
-
}
|
|
20
|
-
const exampleName = exampleParams[0];
|
|
21
|
-
const filePath = `./examples/${exampleName}.ts`;
|
|
22
|
-
if (existsSync(filePath)) {
|
|
23
|
-
require(`../${exampleName}.ts`);
|
|
24
|
-
} else {
|
|
25
|
-
console.error(`Example ${exampleName} does not exist, you can choose from:`);
|
|
26
|
-
showExamples();
|
|
27
|
-
}
|
package/examples/joke.ts
DELETED
|
@@ -1,225 +0,0 @@
|
|
|
1
|
-
import { assign, createActor, fromCallback, log, setup } from 'xstate';
|
|
2
|
-
import { createAgent, fromDecision } from '../src';
|
|
3
|
-
import { loadingAnimation } from './helpers/loader';
|
|
4
|
-
import { z } from 'zod';
|
|
5
|
-
import { openai } from '@ai-sdk/openai';
|
|
6
|
-
import { getFromTerminal } from './helpers/helpers';
|
|
7
|
-
|
|
8
|
-
export function getRandomFunnyPhrase() {
|
|
9
|
-
const funnyPhrases = [
|
|
10
|
-
'Concocting chuckles...',
|
|
11
|
-
'Brewing belly laughs...',
|
|
12
|
-
'Fabricating funnies...',
|
|
13
|
-
'Assembling amusement...',
|
|
14
|
-
'Molding merriment...',
|
|
15
|
-
'Whipping up wisecracks...',
|
|
16
|
-
'Generating guffaws...',
|
|
17
|
-
'Inventing hilarity...',
|
|
18
|
-
'Cultivating chortles...',
|
|
19
|
-
'Hatching howlers...',
|
|
20
|
-
];
|
|
21
|
-
return funnyPhrases[Math.floor(Math.random() * funnyPhrases.length)]!;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export function getRandomRatingPhrase() {
|
|
25
|
-
const ratingPhrases = [
|
|
26
|
-
'Assessing amusement...',
|
|
27
|
-
'Evaluating hilarity...',
|
|
28
|
-
'Ranking chuckles...',
|
|
29
|
-
'Classifying cackles...',
|
|
30
|
-
'Scoring snickers...',
|
|
31
|
-
'Rating roars...',
|
|
32
|
-
'Judging jollity...',
|
|
33
|
-
'Measuring merriment...',
|
|
34
|
-
'Rating rib-ticklers...',
|
|
35
|
-
];
|
|
36
|
-
return ratingPhrases[Math.floor(Math.random() * ratingPhrases.length)]!;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const loader = fromCallback(({ input }: { input: string }) => {
|
|
40
|
-
const anim = loadingAnimation(input);
|
|
41
|
-
|
|
42
|
-
return () => {
|
|
43
|
-
anim.stop();
|
|
44
|
-
};
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
const agent = createAgent({
|
|
48
|
-
name: 'joke-teller',
|
|
49
|
-
model: openai('gpt-4-turbo'),
|
|
50
|
-
events: {
|
|
51
|
-
askForTopic: z.object({
|
|
52
|
-
topic: z.string().describe('The topic for the joke'),
|
|
53
|
-
}),
|
|
54
|
-
'agent.tellJoke': z.object({
|
|
55
|
-
joke: z.string().describe('The joke text'),
|
|
56
|
-
}),
|
|
57
|
-
'agent.endJokes': z.object({}).describe('End the jokes'),
|
|
58
|
-
'agent.rateJoke': z.object({
|
|
59
|
-
rating: z.number().min(1).max(10),
|
|
60
|
-
explanation: z.string(),
|
|
61
|
-
}),
|
|
62
|
-
'agent.continue': z.object({}).describe('Continue'),
|
|
63
|
-
'agent.markAsIrrelevant': z
|
|
64
|
-
.object({
|
|
65
|
-
explanation: z.string(),
|
|
66
|
-
})
|
|
67
|
-
.describe('Explains why the joke was irrelevant'),
|
|
68
|
-
'agent.markAsRelevant': z.object({}).describe('The joke was relevant'),
|
|
69
|
-
},
|
|
70
|
-
context: {
|
|
71
|
-
topic: z.string().describe('The topic for the joke'),
|
|
72
|
-
jokes: z.array(z.string()).describe('The jokes told so far'),
|
|
73
|
-
desire: z.string().nullable().describe('The user desire'),
|
|
74
|
-
lastRating: z.number().nullable().describe('The last joke rating'),
|
|
75
|
-
loader: z.string().nullable().describe('The loader text'),
|
|
76
|
-
},
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
const jokeMachine = setup({
|
|
80
|
-
types: agent.types,
|
|
81
|
-
actors: {
|
|
82
|
-
agent: fromDecision(agent),
|
|
83
|
-
loader,
|
|
84
|
-
getFromTerminal,
|
|
85
|
-
},
|
|
86
|
-
}).createMachine({
|
|
87
|
-
id: 'joke',
|
|
88
|
-
context: () => ({
|
|
89
|
-
topic: '',
|
|
90
|
-
jokes: [],
|
|
91
|
-
desire: null,
|
|
92
|
-
lastRating: null,
|
|
93
|
-
loader: null,
|
|
94
|
-
}),
|
|
95
|
-
initial: 'waitingForTopic',
|
|
96
|
-
states: {
|
|
97
|
-
waitingForTopic: {
|
|
98
|
-
invoke: {
|
|
99
|
-
src: 'getFromTerminal',
|
|
100
|
-
input: 'Give me a joke topic.',
|
|
101
|
-
onDone: {
|
|
102
|
-
actions: assign({
|
|
103
|
-
topic: ({ event }) => event.output,
|
|
104
|
-
}),
|
|
105
|
-
target: 'tellingJoke',
|
|
106
|
-
},
|
|
107
|
-
},
|
|
108
|
-
},
|
|
109
|
-
tellingJoke: {
|
|
110
|
-
invoke: [
|
|
111
|
-
{
|
|
112
|
-
src: 'agent',
|
|
113
|
-
input: ({ context }) => ({
|
|
114
|
-
context: {
|
|
115
|
-
topic: context.topic,
|
|
116
|
-
},
|
|
117
|
-
goal: `Tell me a joke about the topic. Do not make any joke that is not relevant to the topic.`,
|
|
118
|
-
}),
|
|
119
|
-
},
|
|
120
|
-
{
|
|
121
|
-
src: 'loader',
|
|
122
|
-
input: getRandomFunnyPhrase,
|
|
123
|
-
},
|
|
124
|
-
],
|
|
125
|
-
on: {
|
|
126
|
-
'agent.tellJoke': {
|
|
127
|
-
actions: [
|
|
128
|
-
assign({
|
|
129
|
-
jokes: ({ context, event }) => [...context.jokes, event.joke],
|
|
130
|
-
}),
|
|
131
|
-
log((x) => x.event.joke),
|
|
132
|
-
],
|
|
133
|
-
target: 'relevance',
|
|
134
|
-
},
|
|
135
|
-
},
|
|
136
|
-
},
|
|
137
|
-
relevance: {
|
|
138
|
-
invoke: {
|
|
139
|
-
src: 'agent',
|
|
140
|
-
input: (x) => ({
|
|
141
|
-
context: {
|
|
142
|
-
topic: x.context.topic,
|
|
143
|
-
lastJoke: x.context.jokes[x.context.jokes.length - 1],
|
|
144
|
-
},
|
|
145
|
-
goal: 'An irrelevant joke has no reference to the topic. If the last joke is completely irrelevant to the topic, ask for a new joke topic. Otherwise, continue.',
|
|
146
|
-
}),
|
|
147
|
-
},
|
|
148
|
-
on: {
|
|
149
|
-
'agent.markAsIrrelevant': {
|
|
150
|
-
actions: log((x) => 'Irrelevant joke: ' + x.event.explanation),
|
|
151
|
-
target: 'waitingForTopic',
|
|
152
|
-
description: 'Continue',
|
|
153
|
-
},
|
|
154
|
-
'agent.markAsRelevant': {
|
|
155
|
-
actions: log('Joke was relevant'),
|
|
156
|
-
target: 'rateJoke',
|
|
157
|
-
},
|
|
158
|
-
},
|
|
159
|
-
},
|
|
160
|
-
rateJoke: {
|
|
161
|
-
invoke: [
|
|
162
|
-
{
|
|
163
|
-
src: 'agent',
|
|
164
|
-
input: ({ context }) => ({
|
|
165
|
-
context: {
|
|
166
|
-
jokes: context.jokes,
|
|
167
|
-
},
|
|
168
|
-
goal: `Rate the last joke on a scale of 1 to 10.`,
|
|
169
|
-
}),
|
|
170
|
-
},
|
|
171
|
-
{
|
|
172
|
-
src: 'loader',
|
|
173
|
-
input: getRandomRatingPhrase,
|
|
174
|
-
},
|
|
175
|
-
],
|
|
176
|
-
on: {
|
|
177
|
-
'agent.rateJoke': {
|
|
178
|
-
actions: [
|
|
179
|
-
assign({
|
|
180
|
-
lastRating: ({ event }) => event.rating,
|
|
181
|
-
}),
|
|
182
|
-
log(
|
|
183
|
-
({ event }) => `Rating: ${event.rating}\n\n${event.explanation}`
|
|
184
|
-
),
|
|
185
|
-
],
|
|
186
|
-
target: 'decide',
|
|
187
|
-
},
|
|
188
|
-
},
|
|
189
|
-
},
|
|
190
|
-
decide: {
|
|
191
|
-
invoke: {
|
|
192
|
-
src: 'agent',
|
|
193
|
-
input: ({ context }) => ({
|
|
194
|
-
context: {
|
|
195
|
-
lastRating: context.lastRating,
|
|
196
|
-
},
|
|
197
|
-
goal: `Choose what to do next, given the previous rating of the joke.`,
|
|
198
|
-
}),
|
|
199
|
-
},
|
|
200
|
-
on: {
|
|
201
|
-
askForTopic: {
|
|
202
|
-
target: 'waitingForTopic',
|
|
203
|
-
actions: log("That joke wasn't good enough. Let's try again."),
|
|
204
|
-
description:
|
|
205
|
-
'Ask for a new topic, because the last joke rated 6 or lower',
|
|
206
|
-
},
|
|
207
|
-
'agent.endJokes': {
|
|
208
|
-
target: 'end',
|
|
209
|
-
actions: log('That joke was good enough. Goodbye!'),
|
|
210
|
-
description: 'End the jokes, since the last joke rated 7 or higher',
|
|
211
|
-
},
|
|
212
|
-
},
|
|
213
|
-
},
|
|
214
|
-
end: {
|
|
215
|
-
type: 'final',
|
|
216
|
-
},
|
|
217
|
-
},
|
|
218
|
-
exit: () => {
|
|
219
|
-
process.exit();
|
|
220
|
-
},
|
|
221
|
-
});
|
|
222
|
-
|
|
223
|
-
const actor = createActor(jokeMachine);
|
|
224
|
-
|
|
225
|
-
actor.start();
|