@statelyai/agent 2.0.0-next.3 → 2.0.0-next.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.
Files changed (37) hide show
  1. package/.changeset/calm-beans-talk.md +5 -0
  2. package/.changeset/long-guests-explode.md +5 -0
  3. package/.changeset/nice-pants-rule.md +10 -0
  4. package/.changeset/odd-kiwis-compare.md +5 -0
  5. package/.changeset/pre.json +4 -0
  6. package/CHANGELOG.md +23 -0
  7. package/architecture.tldr +797 -0
  8. package/dist/index.d.mts +198 -140
  9. package/dist/index.d.ts +198 -140
  10. package/dist/index.js +4397 -148
  11. package/dist/index.mjs +4397 -149
  12. package/examples/chatbot.ts +9 -5
  13. package/examples/cot.ts +2 -4
  14. package/examples/jugs.ts +2 -2
  15. package/examples/learn-from-feedback.ts +7 -7
  16. package/examples/newspaper.ts +1 -1
  17. package/examples/rewoo.ts +62 -0
  18. package/examples/river-crossing.ts +2 -2
  19. package/examples/serverless.ts +71 -0
  20. package/examples/simple.ts +1 -1
  21. package/examples/ticTacToe.ts +6 -2
  22. package/examples/wiki.ts +2 -2
  23. package/package.json +14 -12
  24. package/readme.md +57 -0
  25. package/src/agent.test.ts +387 -30
  26. package/src/agent.ts +177 -64
  27. package/src/decide.test.ts +24 -2
  28. package/src/decide.ts +34 -78
  29. package/src/index.ts +1 -0
  30. package/src/{strategies/chainOfThought.ts → policies/chainOfThoughtPolicy.ts} +7 -9
  31. package/src/policies/index.ts +3 -0
  32. package/src/{strategies/shortestPath.test.ts → policies/shortestPathPolicy.test.ts} +2 -2
  33. package/src/{strategies/shortestPath.ts → policies/shortestPathPolicy.ts} +8 -8
  34. package/src/{strategies/simple.ts → policies/toolPolicy.ts} +27 -26
  35. package/src/text.ts +17 -22
  36. package/src/types.ts +162 -166
  37. package/src/agent-experimental.ts +0 -221
package/src/types.ts CHANGED
@@ -4,8 +4,6 @@ import {
4
4
  AnyEventObject,
5
5
  AnyStateMachine,
6
6
  EventFrom,
7
- EventObject,
8
- PromiseActorLogic,
9
7
  SnapshotFrom,
10
8
  StateValue,
11
9
  TransitionSnapshot,
@@ -13,7 +11,6 @@ import {
13
11
  } from 'xstate';
14
12
  import {
15
13
  CoreMessage,
16
- GenerateObjectResult,
17
14
  generateText,
18
15
  GenerateTextResult,
19
16
  LanguageModel,
@@ -32,9 +29,13 @@ export type CostFunction<TAgent extends AnyAgent> = (
32
29
  ) => number;
33
30
 
34
31
  export type AgentDecideInput<TAgent extends AnyAgent> = Omit<
35
- AgentGenerateTextOptions<TAgent>,
36
- 'prompt' | 'tools'
32
+ AgentGenerateTextOptions,
33
+ 'model' | 'prompt' | 'tools' | 'toolChoice'
37
34
  > & {
35
+ /**
36
+ * The parent decision that this decision is a part of.
37
+ */
38
+ decisionId?: string;
38
39
  /**
39
40
  * The currently observed state.
40
41
  */
@@ -52,7 +53,8 @@ export type AgentDecideInput<TAgent extends AnyAgent> = Omit<
52
53
  * The events that the agent can trigger. This is a mapping of
53
54
  * event types to Zod event schemas.
54
55
  */
55
- events: ZodEventMapping;
56
+ events?: ZodEventMapping;
57
+ allowedEvents?: Array<EventFromAgent<TAgent>['type']>;
56
58
  /**
57
59
  * The state machine that represents the environment the agent
58
60
  * is interacting with.
@@ -60,7 +62,7 @@ export type AgentDecideInput<TAgent extends AnyAgent> = Omit<
60
62
  machine?: AnyStateMachine;
61
63
 
62
64
  /**
63
- * The total cost of the path to the goal state.
65
+ * A function that calculates the total cost of the path to the goal state.
64
66
  */
65
67
  costFunction?: CostFunction<TAgent>;
66
68
 
@@ -69,48 +71,80 @@ export type AgentDecideInput<TAgent extends AnyAgent> = Omit<
69
71
  * Defaults to 2.
70
72
  */
71
73
  maxAttempts?: number;
72
- };
74
+ /**
75
+ * The policy to use for making a decision.
76
+ */
77
+ policy?: AgentPolicy<TAgent>;
78
+ model?: LanguageModel;
79
+ /**
80
+ * The previous relevant feedback from the agent.
81
+ */
82
+ feedback?: AgentFeedback[];
83
+ /**
84
+ * The previous relevant observations from the agent.
85
+ */
86
+ observations?: AgentObservation<any>[];
87
+ /**
88
+ * The previous relevant decisions from the agent.
89
+ */
90
+ decisions?: AgentDecision<TAgent>[];
91
+ /**
92
+ * The previous relevant insights from the agent.
93
+ */
94
+ insights?: AgentInsight[];
95
+ toolChoice?: 'auto' | 'none' | 'required';
96
+ } & BaseInput;
73
97
 
74
98
  export type AgentStep<TAgent extends AnyAgent> = {
75
99
  /** The event to take */
76
100
  event: EventFromAgent<TAgent>;
77
101
  /** The next expected state after taking the event */
78
- state: ObservedState<TAgent> | undefined;
102
+ state: ObservedState<TAgent> | null;
79
103
  };
80
104
 
81
105
  export type AgentPath<TAgent extends AnyAgent> = {
82
106
  /** The expected ending state of the path */
83
- state: ObservedState<TAgent> | undefined;
107
+ state: ObservedState<TAgent> | null;
84
108
  /** The steps to reach the ending state */
85
109
  steps: Array<AgentStep<TAgent>>;
86
110
  weight?: number;
87
111
  };
88
112
 
89
- export type AgentDecision<TAgent extends AnyAgent> = {
90
- id: string;
113
+ export interface AgentDecisionInput<TAgent extends AnyAgent> extends BaseInput {
114
+ goal: string;
115
+ decisionId?: string | null;
116
+ policy?: string | null;
117
+ goalState?: ObservedState<TAgent> | null;
118
+ nextEvent?: EventFromAgent<TAgent> | null;
119
+ paths?: AgentPath<TAgent>[];
120
+ }
121
+
122
+ export interface AgentDecision<TAgent extends AnyAgent = AnyAgent>
123
+ extends BaseProperties {
124
+ /**
125
+ * The parent decision that this decision is a part of.
126
+ */
127
+ decisionId: string | null;
91
128
  /**
92
- * The strategy used to generate the decision
129
+ * The policy used to generate the decision
93
130
  */
94
- strategy: string;
131
+ policy: string | null;
95
132
  goal: string;
96
133
  /**
97
134
  * The ending state of the decision.
98
135
  */
99
- goalState: ObservedState<TAgent> | undefined;
136
+ goalState: ObservedState<TAgent> | null;
100
137
  /**
101
138
  * The next event that the agent decided needs to occur to achieve the `goal`.
102
139
  *
103
140
  * This next event is chosen from the
104
141
  */
105
- nextEvent: EventFromAgent<TAgent> | undefined;
142
+ nextEvent: EventFromAgent<TAgent> | null;
106
143
  /**
107
144
  * The paths that the agent can take to achieve the goal.
108
145
  */
109
146
  paths: AgentPath<TAgent>[];
110
- episodeId: string;
111
- timestamp: number;
112
- // result: GenerateObjectResult<any>;
113
- };
147
+ }
114
148
 
115
149
  export interface TransitionData {
116
150
  eventType: string;
@@ -136,77 +170,77 @@ export type PromptTemplate<TAgent extends AnyAgent> = (data: {
136
170
  */
137
171
  transitions?: TransitionData[];
138
172
  /**
139
- * Past observations
173
+ * Relevant past observations
140
174
  */
141
175
  observations?: AgentObservation<any>[]; // TODO
176
+ /**
177
+ * Relevant feedback
178
+ */
142
179
  feedback?: AgentFeedback[];
180
+ /**
181
+ * Relevant messages
182
+ */
143
183
  messages?: AgentMessage[];
184
+ /**
185
+ * Relevant past decisions
186
+ */
144
187
  decisions?: AgentDecision<TAgent>[];
188
+ /**
189
+ * Relevant past insights
190
+ */
191
+ insights?: AgentInsight[];
145
192
  }) => string;
146
193
 
147
- export type AgentStrategy<TAgent extends AnyAgent> = (
194
+ export type AgentPolicy<TAgent extends AnyAgent = AnyAgent> = (
148
195
  agent: TAgent,
149
- input: AgentDecideInput<EventFromAgent<TAgent>>
196
+ input: AgentDecideInput<TAgent>
150
197
  ) => Promise<AgentDecision<TAgent> | undefined>;
151
198
 
152
199
  export type AgentInteractInput<T extends AnyAgent> = Omit<
153
- AgentDecideOptions<T>,
200
+ AgentDecideInput<T>,
154
201
  'state'
155
202
  > & {
156
203
  state?: never;
157
204
  };
158
205
 
159
- export type AgentDecideOptions<TAgent extends AnyAgent> = {
160
- goal: string;
161
- state: ObservedState<TAgent>;
162
- /**
163
- * The context to provide in the prompt to the agent. This overrides the `state.context`.
164
- */
165
- context?: Record<string, any>;
166
- machine?: AnyStateMachine;
167
- model?: LanguageModel;
168
- execute?: (event: AnyEventObject) => Promise<void>;
169
- strategy?: AgentStrategy<TAgent>;
170
- events?: ZodEventMapping;
171
- allowedEvents?: Array<EventFromAgent<TAgent>['type']>;
172
- /**
173
- * The maximum number of times the agent will attempt to make a decision.
174
- * Defaults to 2.
175
- */
176
- maxAttempts?: number;
177
- } & Omit<Parameters<typeof generateText>[0], 'model' | 'tools' | 'prompt'>;
178
-
179
- export interface AgentFeedback {
180
- observationId: string;
181
- score: number;
206
+ export interface AgentFeedback extends BaseProperties {
207
+ decisionId: string;
208
+ reward: number;
182
209
  comment: string | undefined;
183
- /**
184
- * The message correlation that the feedback is relevant for
185
- */
186
210
  attributes: Record<string, any>;
187
- timestamp: number;
211
+ }
212
+
213
+ interface BaseProperties {
214
+ id: string;
188
215
  episodeId: string;
216
+ timestamp: number;
189
217
  }
190
218
 
191
- export interface AgentFeedbackInput {
192
- observationId: string;
193
- score: number;
219
+ type BaseInput = Partial<BaseProperties>;
220
+
221
+ export interface AgentFeedbackInput extends BaseInput {
222
+ /**
223
+ * The decision ID that this feedback is relevant for.
224
+ */
225
+ decisionId: string;
226
+ reward: number;
194
227
  comment?: string;
195
228
  attributes?: Record<string, any>;
196
- timestamp?: number;
197
229
  }
198
230
 
199
- export type AgentMessage = CoreMessage & {
200
- timestamp: number;
201
- id: string;
202
- /**
203
- * The response ID of the message, which references
204
- * which message this message is responding to, if any.
205
- */
206
- responseId?: string;
207
- result?: GenerateTextResult<any>;
208
- episodeId: string;
209
- };
231
+ export type AgentMessage = BaseProperties &
232
+ CoreMessage & {
233
+ /**
234
+ * The parent decision that this message is a part of.
235
+ */
236
+ decisionId?: string;
237
+ /**
238
+ * The response ID of the message, which references
239
+ * which message this message is responding to, if any.
240
+ */
241
+ responseId?: string;
242
+ result?: GenerateTextResult<any, any>;
243
+ };
210
244
 
211
245
  type JSONObject = {
212
246
  [key: string]: JSONValue;
@@ -219,24 +253,6 @@ type LanguageModelV1ProviderMetadata = Record<
219
253
  Record<string, JSONValue>
220
254
  >;
221
255
 
222
- interface LanguageModelV1ImagePart {
223
- type: 'image';
224
- /**
225
- Image data as a Uint8Array (e.g. from a Blob or Buffer) or a URL.
226
- */
227
- image: Uint8Array | URL;
228
- /**
229
- Optional mime type of the image.
230
- */
231
- mimeType?: string;
232
- /**
233
- * Additional provider-specific metadata. They are passed through
234
- * to the provider from the AI SDK and enable provider-specific
235
- * functionality that can be fully encapsulated in the provider.
236
- */
237
- providerMetadata?: LanguageModelV1ProviderMetadata;
238
- }
239
-
240
256
  export interface LanguageModelV1TextPart {
241
257
  type: 'text';
242
258
  /**
@@ -272,56 +288,6 @@ Arguments of the tool call. This is a JSON-serializable object that matches the
272
288
  */
273
289
  providerMetadata?: LanguageModelV1ProviderMetadata;
274
290
  }
275
- interface LanguageModelV1ToolResultPart {
276
- type: 'tool-result';
277
- /**
278
- ID of the tool call that this result is associated with.
279
- */
280
- toolCallId: string;
281
- /**
282
- Name of the tool that generated this result.
283
- */
284
- toolName: string;
285
- /**
286
- Result of the tool call. This is a JSON-serializable object.
287
- */
288
- result: unknown;
289
- /**
290
- Optional flag if the result is an error or an error message.
291
- */
292
- isError?: boolean;
293
- /**
294
- * Additional provider-specific metadata. They are passed through
295
- * to the provider from the AI SDK and enable provider-specific
296
- * functionality that can be fully encapsulated in the provider.
297
- */
298
- providerMetadata?: LanguageModelV1ProviderMetadata;
299
- }
300
- type LanguageModelV1Message = (
301
- | {
302
- role: 'system';
303
- content: string;
304
- }
305
- | {
306
- role: 'user';
307
- content: Array<LanguageModelV1TextPart | LanguageModelV1ImagePart>;
308
- }
309
- | {
310
- role: 'assistant';
311
- content: Array<LanguageModelV1TextPart | LanguageModelV1ToolCallPart>;
312
- }
313
- | {
314
- role: 'tool';
315
- content: Array<LanguageModelV1ToolResultPart>;
316
- }
317
- ) & {
318
- /**
319
- * Additional provider-specific metadata. They are passed through
320
- * to the provider from the AI SDK and enable provider-specific
321
- * functionality that can be fully encapsulated in the provider.
322
- */
323
- providerMetadata?: LanguageModelV1ProviderMetadata;
324
- };
325
291
 
326
292
  export type AgentMessageInput = CoreMessage & {
327
293
  timestamp?: number;
@@ -331,41 +297,36 @@ export type AgentMessageInput = CoreMessage & {
331
297
  * which message this message is responding to, if any.
332
298
  */
333
299
  responseId?: string;
334
- result?: GenerateTextResult<any>;
300
+ result?: GenerateTextResult<any, any>;
335
301
  };
336
302
 
337
303
  export interface AgentObservation<TActor extends ActorRefLike> {
338
304
  id: string;
305
+ episodeId: string;
306
+ /**
307
+ * The decision that this observation is relevant for
308
+ */
309
+ decisionId?: string | undefined;
339
310
  goal?: string;
340
311
  prevState: SnapshotFrom<TActor> | undefined;
341
312
  event: EventFrom<TActor> | undefined;
342
313
  state: SnapshotFrom<TActor>;
343
- machineHash: string | undefined;
344
- episodeId: string;
314
+ // machineHash: string | undefined;
345
315
  timestamp: number;
346
316
  }
347
317
 
348
- export interface AgentObservationInput<TAgent extends AnyAgent> {
349
- id?: string;
318
+ export interface AgentObservationInput<TAgent extends AnyAgent>
319
+ extends BaseInput {
320
+ state: ObservedState<TAgent>;
321
+ /**
322
+ * The agent decision that the observation is relevant for
323
+ */
324
+ decisionId?: string | undefined;
350
325
  prevState?: ObservedState<TAgent>;
351
326
  event?: AnyEventObject;
352
- state: ObservedState<TAgent>;
353
- machine?: AnyStateMachine;
354
- timestamp?: number;
355
- goal: string | undefined;
327
+ goal?: string | undefined;
356
328
  }
357
329
 
358
- export type AgentDecisionInput = {
359
- goal: string;
360
- model?: LanguageModel;
361
- context?: Record<string, any>;
362
- } & Omit<Parameters<typeof generateText>[0], 'model' | 'tools' | 'prompt'>;
363
-
364
- export type AgentDecisionLogic<TAgent extends AnyAgent> = PromiseActorLogic<
365
- AgentDecision<TAgent> | undefined,
366
- AgentDecisionInput | string
367
- >;
368
-
369
330
  export type AgentEmitted<TAgent extends AnyAgent> =
370
331
  | {
371
332
  type: 'feedback';
@@ -382,6 +343,10 @@ export type AgentEmitted<TAgent extends AnyAgent> =
382
343
  | {
383
344
  type: 'decision';
384
345
  decision: AgentDecision<TAgent>;
346
+ }
347
+ | {
348
+ type: 'insight';
349
+ insight: AgentInsight;
385
350
  };
386
351
 
387
352
  export type AgentLogic<TAgent extends AnyAgent> = ActorLogic<
@@ -401,6 +366,10 @@ export type AgentLogic<TAgent extends AnyAgent> = ActorLogic<
401
366
  | {
402
367
  type: 'agent.decision';
403
368
  decision: AgentDecision<TAgent>;
369
+ }
370
+ | {
371
+ type: 'agent.insight';
372
+ insight: AgentInsight;
404
373
  },
405
374
  any, // TODO: input
406
375
  any,
@@ -422,29 +391,29 @@ export type ContextFromZodContextMapping<
422
391
  [K in keyof TContextSchema & string]: TypeOf<TContextSchema[K]>;
423
392
  };
424
393
 
425
- export type AnyAgent = Agent<any, any, any, any>;
394
+ export type AnyAgent = Agent<any, any>;
426
395
 
427
396
  export type FromAgent<T> = T | ((agent: AnyAgent) => T | Promise<T>);
428
397
 
429
- export type CommonTextOptions<TAgent extends AnyAgent> = {
398
+ export type CommonTextOptions = {
430
399
  prompt: FromAgent<string>;
431
400
  model?: LanguageModel;
432
- messages?: FromAgent<CoreMessage[]>;
401
+ messages?: CoreMessage[];
433
402
  template?: PromptTemplate<any>;
434
403
  context?: Record<string, any>;
435
404
  };
436
405
 
437
- export type AgentGenerateTextOptions<TAgent extends AnyAgent> = Omit<
406
+ export type AgentGenerateTextOptions = Omit<
438
407
  GenerateTextOptions,
439
408
  'model' | 'prompt' | 'messages'
440
409
  > &
441
- CommonTextOptions<TAgent>;
410
+ CommonTextOptions;
442
411
 
443
- export type AgentStreamTextOptions<TAgent extends AnyAgent> = Omit<
412
+ export type AgentStreamTextOptions = Omit<
444
413
  StreamTextOptions,
445
414
  'model' | 'prompt' | 'messages'
446
415
  > &
447
- CommonTextOptions<TAgent>;
416
+ CommonTextOptions;
448
417
 
449
418
  export interface ObservedState<TAgent extends AnyAgent> {
450
419
  /**
@@ -464,10 +433,11 @@ export type ObservedStateFrom<TActor extends ActorRefLike> = Pick<
464
433
  >;
465
434
 
466
435
  export type AgentMemoryContext<TAgent extends AnyAgent> = {
467
- observations: AgentObservation<TAgent>[]; // TODO
436
+ observations: AgentObservation<any>[]; // TODO
468
437
  messages: AgentMessage[];
469
438
  decisions: AgentDecision<TAgent>[];
470
439
  feedback: AgentFeedback[];
440
+ insights: AgentInsight[];
471
441
  };
472
442
 
473
443
  export interface AgentLongTermMemory<TAgent extends AnyAgent> {
@@ -490,11 +460,9 @@ export type MaybePromise<T> = T | Promise<T>;
490
460
 
491
461
  export type EventFromAgent<T extends AnyAgent> = T extends Agent<
492
462
  infer _,
493
- infer __,
494
- infer TEvents,
495
- infer ___
463
+ infer TEventSchemas
496
464
  >
497
- ? TEvents
465
+ ? EventsFromZodEventMapping<TEventSchemas>
498
466
  : never;
499
467
 
500
468
  export type TypesFromAgent<T extends AnyAgent> = T extends Agent<
@@ -513,3 +481,31 @@ export type ContextFromAgent<T extends AnyAgent> = T extends Agent<
513
481
  >
514
482
  ? ContextFromZodContextMapping<TContextSchema>
515
483
  : never;
484
+
485
+ export interface StorageAdapter<TAgent extends AnyAgent, TQuery> {
486
+ addObservation(
487
+ observationInput: AgentObservationInput<TAgent>
488
+ ): Promise<AgentObservation<any>>;
489
+ getObservations(queryObject?: TQuery): Promise<AgentObservation<any>[]>;
490
+ addFeedback(feedbackInput: AgentFeedbackInput): Promise<AgentFeedback>;
491
+ getFeedback(queryObject?: TQuery): Promise<AgentFeedback[]>;
492
+ addMessage(messageInput: AgentMessageInput): Promise<AgentMessage>;
493
+ getMessages(queryObject?: TQuery): Promise<AgentMessage[]>;
494
+ addDecision(
495
+ decisionInput: AgentDecideInput<TAgent>
496
+ ): Promise<AgentDecision<TAgent>>;
497
+ getDecisions(queryObject?: TQuery): Promise<AgentDecision<TAgent>[]>;
498
+ }
499
+
500
+ export type StorageAdapterQuery<T extends StorageAdapter<any, any>> =
501
+ T extends StorageAdapter<infer _, infer TQuery> ? TQuery : never;
502
+
503
+ export interface AgentInsightInput extends BaseInput {
504
+ observationId: string;
505
+ attributes: Record<string, any>;
506
+ }
507
+
508
+ export interface AgentInsight extends BaseProperties {
509
+ observationId: string;
510
+ attributes: Record<string, any>;
511
+ }