@statelyai/agent 1.1.6 → 2.0.0-alpha.11

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 (92) hide show
  1. package/LICENSE +21 -0
  2. package/dist/adapter.cjs +15 -0
  3. package/dist/adapter.d.cts +4 -0
  4. package/dist/adapter.d.mts +4 -0
  5. package/dist/adapter.mjs +2 -0
  6. package/dist/ai-sdk.cjs +306 -0
  7. package/dist/ai-sdk.d.cts +96 -0
  8. package/dist/ai-sdk.d.mts +96 -0
  9. package/dist/ai-sdk.mjs +304 -0
  10. package/dist/decision-C3k4ve51.mjs +227 -0
  11. package/dist/decision-D8wJrM8W.cjs +286 -0
  12. package/dist/events-CRQj3VtP.cjs +1010 -0
  13. package/dist/events-JiVPYrct.mjs +759 -0
  14. package/dist/index.cjs +2528 -0
  15. package/dist/index.d.cts +1232 -0
  16. package/dist/index.d.mts +1217 -413
  17. package/dist/index.mjs +2489 -584
  18. package/dist/openai-compat.cjs +309 -0
  19. package/dist/openai-compat.d.cts +59 -0
  20. package/dist/openai-compat.d.mts +59 -0
  21. package/dist/openai-compat.mjs +308 -0
  22. package/dist/steps-BALp1eZo.d.mts +198 -0
  23. package/dist/steps-CVe54GPP.cjs +420 -0
  24. package/dist/steps-CkyyyuHd.mjs +379 -0
  25. package/dist/steps-MjnQI4aB.d.cts +198 -0
  26. package/dist/steps.cjs +12 -0
  27. package/dist/steps.d.cts +3 -0
  28. package/dist/steps.d.mts +3 -0
  29. package/dist/steps.mjs +3 -0
  30. package/dist/text-logic-CaKqgX4Y.d.mts +710 -0
  31. package/dist/text-logic-Ckhr2kKC.d.cts +710 -0
  32. package/dist/types-C9QiMjre.d.cts +219 -0
  33. package/dist/types-qm00QF91.d.mts +219 -0
  34. package/dist/utils-BYqT_Dyv.d.cts +108 -0
  35. package/dist/utils-Do5wIJrh.d.mts +108 -0
  36. package/dist/zod.cjs +31 -0
  37. package/dist/zod.d.cts +30 -0
  38. package/dist/zod.d.mts +30 -0
  39. package/dist/zod.mjs +30 -0
  40. package/package.json +132 -28
  41. package/readme.md +153 -6
  42. package/schemas/agent-workflow.json +526 -0
  43. package/.changeset/README.md +0 -8
  44. package/.changeset/config.json +0 -11
  45. package/.env.template +0 -3
  46. package/.github/actions/ci-setup/action.yml +0 -24
  47. package/.github/workflows/release.yml +0 -46
  48. package/.vscode/launch.json +0 -28
  49. package/CHANGELOG.md +0 -222
  50. package/dist/index.d.ts +0 -428
  51. package/dist/index.js +0 -621
  52. package/examples/chatbot.ts +0 -71
  53. package/examples/cot.ts +0 -89
  54. package/examples/email.ts +0 -118
  55. package/examples/example.ts +0 -81
  56. package/examples/goal.ts +0 -94
  57. package/examples/helpers/helpers.ts +0 -17
  58. package/examples/helpers/loader.ts +0 -32
  59. package/examples/helpers/runner.ts +0 -27
  60. package/examples/joke.ts +0 -225
  61. package/examples/multi.ts +0 -103
  62. package/examples/newspaper.ts +0 -324
  63. package/examples/number.ts +0 -102
  64. package/examples/raffle.ts +0 -105
  65. package/examples/sandbox.ts +0 -28
  66. package/examples/simple.ts +0 -39
  67. package/examples/support.ts +0 -147
  68. package/examples/ticTacToe.ts +0 -224
  69. package/examples/todo.ts +0 -137
  70. package/examples/tutor.ts +0 -100
  71. package/examples/verify.ts +0 -120
  72. package/examples/weather.ts +0 -178
  73. package/examples/wiki.ts +0 -30
  74. package/examples/word.ts +0 -171
  75. package/src/adapters/vercel.ts +0 -7
  76. package/src/agent-experimental.ts +0 -221
  77. package/src/agent.test.ts +0 -506
  78. package/src/agent.ts +0 -300
  79. package/src/decision.test.ts +0 -179
  80. package/src/decision.ts +0 -84
  81. package/src/index.ts +0 -4
  82. package/src/memory.ts +0 -25
  83. package/src/planners/shortestPathPlanner.ts +0 -22
  84. package/src/planners/simplePlanner.ts +0 -139
  85. package/src/schemas.ts +0 -11
  86. package/src/strategies/chain-of-note.ts +0 -155
  87. package/src/templates/defaultText.ts +0 -18
  88. package/src/text.ts +0 -241
  89. package/src/types.ts +0 -499
  90. package/src/utils.ts +0 -72
  91. package/tsconfig.json +0 -109
  92. 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
- };