@statelyai/agent 1.1.6 → 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.
Files changed (88) hide show
  1. package/LICENSE +21 -0
  2. package/dist/ai-sdk.cjs +342 -0
  3. package/dist/ai-sdk.d.cts +206 -0
  4. package/dist/ai-sdk.d.mts +206 -0
  5. package/dist/ai-sdk.mjs +331 -0
  6. package/dist/cli.cjs +64 -0
  7. package/dist/cli.d.cts +1 -0
  8. package/dist/cli.d.mts +1 -0
  9. package/dist/cli.mjs +65 -0
  10. package/dist/decision-CQdrKc8k.mjs +978 -0
  11. package/dist/decision-b-lkcs4L.cjs +1295 -0
  12. package/dist/index.cjs +59 -0
  13. package/dist/index.d.cts +212 -0
  14. package/dist/index.d.mts +201 -417
  15. package/dist/index.mjs +4 -588
  16. package/dist/openai-compat.cjs +319 -0
  17. package/dist/openai-compat.d.cts +98 -0
  18. package/dist/openai-compat.d.mts +98 -0
  19. package/dist/openai-compat.mjs +312 -0
  20. package/dist/run-agent-BFMkuI1F.cjs +1599 -0
  21. package/dist/run-agent-BVMd--1l.d.cts +1103 -0
  22. package/dist/run-agent-BoAOq_0Z.mjs +1540 -0
  23. package/dist/run-agent-DzYJb3CK.d.mts +1103 -0
  24. package/dist/src-D-77Ha5p.cjs +1087 -0
  25. package/dist/src-DUeIFjv-.mjs +1016 -0
  26. package/dist/text-logic-C7WJpCIc.d.mts +710 -0
  27. package/dist/text-logic-CZjyACzQ.d.cts +710 -0
  28. package/dist/types-C9QiMjre.d.cts +219 -0
  29. package/dist/types-qm00QF91.d.mts +219 -0
  30. package/dist/utils-Dri7aeEG.d.cts +108 -0
  31. package/dist/utils-Y6GDRGGE.d.mts +108 -0
  32. package/dist/zod.cjs +31 -0
  33. package/dist/zod.d.cts +30 -0
  34. package/dist/zod.d.mts +30 -0
  35. package/dist/zod.mjs +30 -0
  36. package/package.json +109 -28
  37. package/readme.md +143 -6
  38. package/schemas/agent-workflow.json +527 -0
  39. package/.changeset/README.md +0 -8
  40. package/.changeset/config.json +0 -11
  41. package/.env.template +0 -3
  42. package/.github/actions/ci-setup/action.yml +0 -24
  43. package/.github/workflows/release.yml +0 -46
  44. package/.vscode/launch.json +0 -28
  45. package/CHANGELOG.md +0 -222
  46. package/dist/index.d.ts +0 -428
  47. package/dist/index.js +0 -621
  48. package/examples/chatbot.ts +0 -71
  49. package/examples/cot.ts +0 -89
  50. package/examples/email.ts +0 -118
  51. package/examples/example.ts +0 -81
  52. package/examples/goal.ts +0 -94
  53. package/examples/helpers/helpers.ts +0 -17
  54. package/examples/helpers/loader.ts +0 -32
  55. package/examples/helpers/runner.ts +0 -27
  56. package/examples/joke.ts +0 -225
  57. package/examples/multi.ts +0 -103
  58. package/examples/newspaper.ts +0 -324
  59. package/examples/number.ts +0 -102
  60. package/examples/raffle.ts +0 -105
  61. package/examples/sandbox.ts +0 -28
  62. package/examples/simple.ts +0 -39
  63. package/examples/support.ts +0 -147
  64. package/examples/ticTacToe.ts +0 -224
  65. package/examples/todo.ts +0 -137
  66. package/examples/tutor.ts +0 -100
  67. package/examples/verify.ts +0 -120
  68. package/examples/weather.ts +0 -178
  69. package/examples/wiki.ts +0 -30
  70. package/examples/word.ts +0 -171
  71. package/src/adapters/vercel.ts +0 -7
  72. package/src/agent-experimental.ts +0 -221
  73. package/src/agent.test.ts +0 -506
  74. package/src/agent.ts +0 -300
  75. package/src/decision.test.ts +0 -179
  76. package/src/decision.ts +0 -84
  77. package/src/index.ts +0 -4
  78. package/src/memory.ts +0 -25
  79. package/src/planners/shortestPathPlanner.ts +0 -22
  80. package/src/planners/simplePlanner.ts +0 -139
  81. package/src/schemas.ts +0 -11
  82. package/src/strategies/chain-of-note.ts +0 -155
  83. package/src/templates/defaultText.ts +0 -18
  84. package/src/text.ts +0 -241
  85. package/src/types.ts +0 -499
  86. package/src/utils.ts +0 -72
  87. package/tsconfig.json +0 -109
  88. package/vitest.config.ts +0 -9
@@ -1,120 +0,0 @@
1
- import { assign, createActor, setup, log } from 'xstate';
2
- import { getFromTerminal } from './helpers/helpers';
3
- import { createAgent, fromDecision } from '../src';
4
- import { z } from 'zod';
5
- import { openai } from '@ai-sdk/openai';
6
-
7
- const agent = createAgent({
8
- name: 'verifier',
9
- model: openai('gpt-3.5-turbo-16k-0613'),
10
- events: {
11
- 'agent.validateAnswer': z.object({
12
- isValid: z.boolean(),
13
- feedback: z.string(),
14
- }),
15
- 'agent.answerQuestion': z.object({
16
- answer: z.string().describe('The answer from the agent'),
17
- }),
18
- 'agent.validateQuestion': z.object({
19
- isValid: z
20
- .boolean()
21
- .describe(
22
- 'Whether the question is a valid question; that is, is it possible to even answer this question in a verifiably correct way?'
23
- ),
24
- explanation: z
25
- .string()
26
- .describe('An explanation for why the question is or is not valid'),
27
- }),
28
- },
29
- });
30
-
31
- const machine = setup({
32
- types: {
33
- context: {} as {
34
- question: string | null;
35
- answer: string | null;
36
- validation: string | null;
37
- },
38
- events: agent.types.events,
39
- },
40
- actors: {
41
- getFromTerminal,
42
- agent: fromDecision(agent),
43
- },
44
- }).createMachine({
45
- initial: 'askQuestion',
46
- context: { question: null, answer: null, validation: null },
47
- states: {
48
- askQuestion: {
49
- invoke: {
50
- src: 'getFromTerminal',
51
- input: 'Ask a (potentially silly) question',
52
- onDone: {
53
- actions: assign({
54
- question: ({ event }) => event.output,
55
- }),
56
- target: 'validateQuestion',
57
- },
58
- },
59
- },
60
- validateQuestion: {
61
- invoke: {
62
- src: 'agent',
63
- input: ({ context }) => ({
64
- goal: `Validate this question: ${context.question!}`,
65
- }),
66
- },
67
- on: {
68
- 'agent.validateQuestion': [
69
- {
70
- target: 'askQuestion',
71
- guard: ({ event }) => !event.isValid,
72
- actions: log(({ event }) => event.explanation),
73
- },
74
- {
75
- target: 'answerQuestion',
76
- },
77
- ],
78
- },
79
- },
80
- answerQuestion: {
81
- invoke: {
82
- src: 'agent',
83
- input: ({ context }) => ({
84
- goal: `Answer this question: ${context.question}`,
85
- }),
86
- },
87
- on: {
88
- 'agent.answerQuestion': {
89
- actions: assign({
90
- answer: ({ event }) => event.answer,
91
- }),
92
- target: 'validateAnswer',
93
- },
94
- },
95
- },
96
- validateAnswer: {
97
- invoke: {
98
- src: 'agent',
99
- input: ({ context }) => ({
100
- goal: `Validate if this is a good answer to the question: ${context.question}\nAnswer provided: ${context.answer}`,
101
- }),
102
- },
103
- on: {
104
- 'agent.validateAnswer': {
105
- actions: assign({
106
- validation: ({ event }) => event.feedback,
107
- }),
108
- },
109
- },
110
- },
111
- },
112
- });
113
-
114
- const actor = createActor(machine, {});
115
-
116
- actor.subscribe((s) => {
117
- console.log(s.value, s.context);
118
- });
119
-
120
- actor.start();
@@ -1,178 +0,0 @@
1
- import { createAgent, fromDecision } from '../src';
2
- import { assign, createActor, fromPromise, log, setup } from 'xstate';
3
- import { getFromTerminal } from './helpers/helpers';
4
- import { z } from 'zod';
5
- import { openai } from '@ai-sdk/openai';
6
-
7
- async function searchTavily(
8
- input: string,
9
- options: {
10
- maxResults?: number;
11
- apiKey: string;
12
- }
13
- ) {
14
- const body: Record<string, unknown> = {
15
- query: input,
16
- max_results: options.maxResults,
17
- api_key: options.apiKey,
18
- };
19
-
20
- const response = await fetch('https://api.tavily.com/search', {
21
- method: 'POST',
22
- headers: {
23
- 'content-type': 'application/json',
24
- },
25
- body: JSON.stringify(body),
26
- });
27
-
28
- const json = await response.json();
29
- if (!response.ok) {
30
- throw new Error(
31
- `Request failed with status code ${response.status}: ${json.error}`
32
- );
33
- }
34
- if (!Array.isArray(json.results)) {
35
- throw new Error(`Could not parse Tavily results. Please try again.`);
36
- }
37
- return JSON.stringify(json.results);
38
- }
39
-
40
- const getWeather = fromPromise(async ({ input }: { input: string }) => {
41
- const results = await searchTavily(
42
- `Get the weather for this location: ${input}`,
43
- {
44
- maxResults: 5,
45
- apiKey: process.env.TAVILY_API_KEY!,
46
- }
47
- );
48
- return results;
49
- });
50
-
51
- const agent = createAgent({
52
- name: 'weather',
53
- model: openai('gpt-4-1106-preview'),
54
- events: {
55
- 'agent.getWeather': z.object({
56
- location: z.string().describe('The location to get the weather for'),
57
- }),
58
- 'agent.reportWeather': z.object({
59
- location: z
60
- .string()
61
- .describe('The location the weather is being reported for'),
62
- highF: z.number().describe('The high temperature today in Fahrenheit'),
63
- lowF: z.number().describe('The low temperature today in Fahrenheit'),
64
- summary: z.string().describe('A summary of the weather conditions'),
65
- }),
66
- 'agent.doSomethingElse': z
67
- .object({})
68
- .describe(
69
- 'Do something else, because the user did not provide a location'
70
- ),
71
- },
72
- });
73
-
74
- const machine = setup({
75
- types: {
76
- context: {} as {
77
- location: string;
78
- history: string[];
79
- count: number;
80
- },
81
- events: agent.types.events,
82
- },
83
- actors: {
84
- agent: fromDecision(agent),
85
- getWeather,
86
- getFromTerminal,
87
- },
88
- }).createMachine({
89
- initial: 'getLocation',
90
- context: {
91
- location: '',
92
- count: 0,
93
- history: [],
94
- },
95
- states: {
96
- getLocation: {
97
- invoke: {
98
- src: 'getFromTerminal',
99
- input: 'Location?',
100
- onDone: {
101
- actions: assign({
102
- location: ({ event }) => event.output,
103
- }),
104
- target: 'decide',
105
- },
106
- },
107
- always: {
108
- guard: ({ context }) => context.count >= 3,
109
- target: 'stopped',
110
- },
111
- },
112
- decide: {
113
- entry: log('Deciding...'),
114
- invoke: {
115
- src: 'agent',
116
- input: ({ context }) => ({
117
- context: {
118
- location: context.location,
119
- },
120
- goal: `Decide what to do based on the given location, which may or may not be a location`,
121
- }),
122
- },
123
- on: {
124
- 'agent.getWeather': {
125
- actions: log(({ event }) => event),
126
- target: 'gettingWeather',
127
- },
128
- 'agent.doSomethingElse': 'getLocation',
129
- },
130
- },
131
- gettingWeather: {
132
- entry: log('Getting weather...'),
133
- invoke: {
134
- src: 'getWeather',
135
- input: ({ context }) => context.location,
136
- onDone: {
137
- actions: [
138
- log(({ event }) => event.output),
139
- assign({
140
- count: ({ context }) => context.count + 1,
141
- }),
142
- ],
143
- target: 'reportWeather',
144
- },
145
- },
146
- },
147
- reportWeather: {
148
- invoke: {
149
- src: 'agent',
150
- input: ({ context }) => ({
151
- goal: 'Report the weather', // TODO
152
- }),
153
- },
154
- on: {
155
- 'agent.reportWeather': {
156
- actions: log(({ event }) => event),
157
- target: 'getLocation',
158
- },
159
- },
160
- },
161
- stopped: {
162
- entry: log('You have used up your search quota. Goodbye!'),
163
- },
164
- },
165
- exit: () => {
166
- process.exit();
167
- },
168
- });
169
-
170
- const actor = createActor(machine, {
171
- input: {
172
- location: 'New York',
173
- },
174
- });
175
- actor.subscribe((s) => {
176
- console.log(s.value);
177
- });
178
- actor.start();
package/examples/wiki.ts DELETED
@@ -1,30 +0,0 @@
1
- import { z } from 'zod';
2
- import { createAgent } from '../src';
3
- import { openai } from '@ai-sdk/openai';
4
-
5
- const agent = createAgent({
6
- name: 'wiki',
7
- model: openai('gpt-4-turbo'),
8
- events: {
9
- provideAnswer: z.object({
10
- answer: z.string().describe('The answer'),
11
- }),
12
- },
13
- });
14
-
15
- async function main() {
16
- const response1 = await agent.generateText({
17
- prompt: 'When was Deadpool 2 released?',
18
- });
19
-
20
- console.log(response1.text);
21
-
22
- const response2 = await agent.generateText({
23
- messages: (x) => x.select((ctx) => ctx.messages),
24
- prompt: 'What about the first one?',
25
- });
26
-
27
- console.log(response2.text);
28
- }
29
-
30
- main();
package/examples/word.ts DELETED
@@ -1,171 +0,0 @@
1
- import { assign, createActor, log, setup } from 'xstate';
2
- import { getFromTerminal } from './helpers/helpers';
3
- import { createAgent, fromDecision } from '../src';
4
- import { z } from 'zod';
5
- import { openai } from '@ai-sdk/openai';
6
-
7
- const context = {
8
- word: null as string | null,
9
- guessedWord: null as string | null,
10
- lettersGuessed: [] as string[],
11
- };
12
-
13
- const agent = createAgent({
14
- name: 'word',
15
- model: openai('gpt-4o'),
16
- events: {
17
- 'agent.guessLetter': z.object({
18
- letter: z.string().min(1).max(1).describe('The letter guessed'),
19
- reasoning: z.string().describe('The reasoning behind the guess'),
20
- }),
21
-
22
- 'agent.guessWord': z.object({
23
- word: z.string().describe('The word guessed'),
24
- }),
25
-
26
- 'agent.respond': z.object({
27
- response: z
28
- .string()
29
- .describe(
30
- 'The response from the agent, detailing why the guess was correct or incorrect based on the letters guessed.'
31
- ),
32
- }),
33
- },
34
- });
35
-
36
- const wordGuesserMachine = setup({
37
- types: {
38
- context: {} as typeof context,
39
- events: agent.types.events,
40
- },
41
- actors: {
42
- agent: fromDecision(agent),
43
- getFromTerminal,
44
- },
45
- actions: {
46
- resetContext: assign(context),
47
- },
48
- }).createMachine({
49
- initial: 'providingWord',
50
- context,
51
- states: {
52
- providingWord: {
53
- entry: 'resetContext',
54
- invoke: {
55
- src: 'getFromTerminal',
56
- input: 'Enter a word, and an agent will try to guess it.',
57
- onDone: {
58
- actions: assign({
59
- word: ({ event }) => event.output,
60
- }),
61
- target: 'guessing',
62
- },
63
- },
64
- },
65
- guessing: {
66
- always: {
67
- guard: ({ context }) => context.lettersGuessed.length > 10,
68
- target: 'finalGuess',
69
- },
70
- invoke: {
71
- src: 'agent',
72
- input: ({ context }) => ({
73
- context: {
74
- wordLength: context.word!.length,
75
- lettersGuessed: context.lettersGuessed,
76
- lettersMatched: context
77
- .word!.split('')
78
- .map((letter) =>
79
- context.lettersGuessed.includes(letter.toUpperCase())
80
- ? letter.toUpperCase()
81
- : '_'
82
- )
83
- .join(''),
84
- },
85
- goal: `You are trying to guess the word. Please make your next guess - guess a letter or, if you think you know the word, guess the full word. You can only make 10 total guesses. If you are confident you know the word, it is better to guess the word.`,
86
- }),
87
- },
88
- on: {
89
- 'agent.guessLetter': {
90
- actions: [
91
- assign({
92
- lettersGuessed: ({ context, event }) => {
93
- return [...context.lettersGuessed, event.letter.toUpperCase()];
94
- },
95
- }),
96
- log(({ event }) => event),
97
- ],
98
- target: 'guessing',
99
- reenter: true,
100
- },
101
- 'agent.guessWord': {
102
- actions: [
103
- assign({
104
- guessedWord: ({ event }) => event.word,
105
- }),
106
- log(({ event }) => event),
107
- ],
108
- target: 'gameOver',
109
- },
110
- },
111
- },
112
- finalGuess: {
113
- invoke: {
114
- src: 'agent',
115
- input: ({ context }) => ({
116
- context: {
117
- lettersGuessed: context.lettersGuessed,
118
- },
119
- goal: `You have used all 10 guesses. These letters matched: ${context
120
- .word!.split('')
121
- .map((letter) =>
122
- context.lettersGuessed.includes(letter.toUpperCase())
123
- ? letter.toUpperCase()
124
- : '_'
125
- )
126
- .join('')}. Guess the word.`,
127
- }),
128
- },
129
- on: {
130
- 'agent.guessWord': {
131
- actions: [
132
- assign({
133
- guessedWord: ({ event }) => event.word,
134
- }),
135
- log(({ event }) => event),
136
- ],
137
- target: 'gameOver',
138
- },
139
- },
140
- },
141
- gameOver: {
142
- invoke: {
143
- src: 'agent',
144
- input: ({ context }) => ({
145
- context,
146
- goal: `Why do you think you won or lost?`,
147
- }),
148
- },
149
- entry: log(({ context }) => {
150
- if (
151
- context.guessedWord?.toUpperCase() === context.word?.toUpperCase()
152
- ) {
153
- return 'The agent won!';
154
- } else {
155
- return 'The agent lost! The word was ' + context.word;
156
- }
157
- }),
158
- on: {
159
- 'agent.respond': {
160
- actions: log(({ event }) => event.response),
161
- target: 'providingWord',
162
- },
163
- },
164
- },
165
- },
166
- exit: () => process.exit(),
167
- });
168
-
169
- const game = createActor(wordGuesserMachine);
170
-
171
- game.start();
@@ -1,7 +0,0 @@
1
- import { generateText, streamText } from 'ai';
2
- import { AIAdapter } from '../types';
3
-
4
- export const vercelAdapter: AIAdapter = {
5
- generateText,
6
- streamText,
7
- };