@statelyai/agent 0.0.7 → 0.1.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.
Files changed (50) hide show
  1. package/.vscode/launch.json +12 -1
  2. package/CHANGELOG.md +47 -0
  3. package/dist/index.d.mts +3 -0
  4. package/dist/index.d.ts +282 -71
  5. package/dist/index.js +4389 -183
  6. package/dist/index.mjs +7 -0
  7. package/examples/chatbot.ts +79 -0
  8. package/examples/cot.ts +91 -0
  9. package/examples/email.ts +118 -0
  10. package/examples/example.ts +81 -0
  11. package/examples/goal.ts +94 -0
  12. package/examples/joke.ts +117 -110
  13. package/examples/multi.ts +103 -0
  14. package/examples/newspaper.ts +324 -0
  15. package/examples/number.ts +102 -0
  16. package/examples/raffle.ts +105 -0
  17. package/examples/simple.ts +39 -0
  18. package/examples/support.ts +147 -0
  19. package/examples/ticTacToe.ts +89 -124
  20. package/examples/todo.ts +132 -0
  21. package/examples/tutor.ts +100 -0
  22. package/examples/verify.ts +120 -0
  23. package/examples/weather.ts +65 -47
  24. package/examples/wiki.ts +30 -0
  25. package/examples/word.ts +168 -0
  26. package/package.json +18 -11
  27. package/readme.md +9 -38
  28. package/src/adapters/vercel.ts +7 -0
  29. package/src/agent-experimental.ts +221 -0
  30. package/src/agent.test.ts +187 -0
  31. package/src/agent.ts +260 -6
  32. package/src/decision.test.ts +179 -0
  33. package/src/decision.ts +83 -0
  34. package/src/index.ts +3 -2
  35. package/src/memory.ts +25 -0
  36. package/src/planners/shortestPathPlanner.ts +22 -0
  37. package/src/planners/simplePlanner.ts +126 -0
  38. package/src/schemas.ts +13 -38
  39. package/src/strategies/chain-of-note.ts +155 -0
  40. package/src/templates/defaultText.ts +18 -0
  41. package/src/templates/defaultToolCall.ts +10 -0
  42. package/src/text.ts +232 -0
  43. package/src/types.ts +363 -46
  44. package/src/utils.ts +13 -50
  45. package/tsconfig.json +1 -1
  46. package/examples/multiAgentCollaboration.ts +0 -0
  47. package/examples/numberGuesser.ts +0 -128
  48. package/examples/wordGuesser.ts +0 -156
  49. package/src/adapter.test.ts +0 -217
  50. package/src/adapters/openai.ts +0 -298
package/dist/index.mjs ADDED
@@ -0,0 +1,7 @@
1
+ // src/index.ts
2
+ function helloWorld() {
3
+ return "Hello World!";
4
+ }
5
+ export {
6
+ helloWorld
7
+ };
@@ -0,0 +1,79 @@
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: 'chatbot',
9
+ model: openai('gpt-4-turbo'),
10
+ events: {
11
+ 'agent.respond': z.object({
12
+ response: z.string().describe('The response from the agent'),
13
+ }),
14
+ 'agent.endConversation': z.object({}).describe('Stop the conversation'),
15
+ },
16
+ });
17
+
18
+ const machine = setup({
19
+ types: {
20
+ context: {} as {
21
+ conversation: string[];
22
+ },
23
+ events: agent.eventTypes,
24
+ },
25
+ actors: { agent: fromDecision(agent), getFromTerminal },
26
+ }).createMachine({
27
+ initial: 'listening',
28
+ context: {
29
+ conversation: [],
30
+ },
31
+ states: {
32
+ listening: {
33
+ invoke: {
34
+ src: 'getFromTerminal',
35
+ input: 'User:',
36
+ onDone: {
37
+ actions: assign({
38
+ conversation: (x) =>
39
+ x.context.conversation.concat('User: ' + x.event.output),
40
+ }),
41
+ target: 'responding',
42
+ },
43
+ },
44
+ },
45
+ responding: {
46
+ invoke: {
47
+ src: 'agent',
48
+ input: (x) => ({
49
+ context: {
50
+ conversation: x.context.conversation,
51
+ },
52
+ goal: 'Respond to the user, unless they want to end the conversation.',
53
+ }),
54
+ },
55
+ on: {
56
+ 'agent.respond': {
57
+ actions: [
58
+ assign({
59
+ conversation: (x) =>
60
+ x.context.conversation.concat('Assistant: ' + x.event.response),
61
+ }),
62
+ log((x) => `Agent: ${x.event.response}`),
63
+ ],
64
+ target: 'listening',
65
+ },
66
+ 'agent.endConversation': 'finished',
67
+ },
68
+ },
69
+ finished: {
70
+ type: 'final',
71
+ },
72
+ },
73
+ exit: () => {
74
+ console.log('End of conversation.');
75
+ process.exit();
76
+ },
77
+ });
78
+
79
+ createActor(machine).start();
@@ -0,0 +1,91 @@
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
+ });
21
+
22
+ const machine = setup({
23
+ types: {
24
+ context: {} as {
25
+ question: string | null;
26
+ thought: string | null;
27
+ },
28
+ events: agent.eventTypes,
29
+ },
30
+ actors: { agent: fromDecision(agent), getFromTerminal },
31
+ }).createMachine({
32
+ initial: 'asking',
33
+ context: {
34
+ question: null,
35
+ thought: null,
36
+ },
37
+ states: {
38
+ asking: {
39
+ invoke: {
40
+ src: 'getFromTerminal',
41
+ input: 'What would you like to ask?',
42
+ onDone: {
43
+ actions: assign({
44
+ question: (x) => x.event.output,
45
+ }),
46
+ target: 'thinking',
47
+ },
48
+ },
49
+ },
50
+ thinking: {
51
+ invoke: {
52
+ src: 'agent',
53
+ input: (x) => ({
54
+ context: x.context,
55
+ goal: 'Answer the question. Think step-by-step.',
56
+ }),
57
+ },
58
+ on: {
59
+ 'agent.think': {
60
+ actions: [
61
+ log((x) => x.event.thought),
62
+ assign({
63
+ thought: (x) => x.event.thought,
64
+ }),
65
+ ],
66
+ target: 'answering',
67
+ },
68
+ },
69
+ },
70
+ answering: {
71
+ invoke: {
72
+ src: 'agent',
73
+ input: (x) => ({
74
+ context: x.context,
75
+ goal: 'Answer the question',
76
+ }),
77
+ },
78
+ on: {
79
+ 'agent.answer': {
80
+ actions: [log((x) => x.event.answer)],
81
+ target: 'answered',
82
+ },
83
+ },
84
+ },
85
+ answered: {
86
+ type: 'final',
87
+ },
88
+ },
89
+ });
90
+
91
+ createActor(machine).start();
@@ -0,0 +1,118 @@
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.eventTypes,
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.select((ctx) => ctx.messages),
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();
@@ -0,0 +1,81 @@
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.eventTypes,
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: {
36
+ context: true,
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: {
49
+ context: true,
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();
@@ -0,0 +1,94 @@
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.eventTypes,
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();