@statelyai/agent 2.0.0-next.4 → 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.
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,10 +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
- 'model' | 'prompt' | 'tools'
32
+ AgentGenerateTextOptions,
33
+ 'model' | 'prompt' | 'tools' | 'toolChoice'
37
34
  > & {
38
- episodeId?: string;
35
+ /**
36
+ * The parent decision that this decision is a part of.
37
+ */
38
+ decisionId?: string;
39
39
  /**
40
40
  * The currently observed state.
41
41
  */
@@ -62,7 +62,7 @@ export type AgentDecideInput<TAgent extends AnyAgent> = Omit<
62
62
  machine?: AnyStateMachine;
63
63
 
64
64
  /**
65
- * 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.
66
66
  */
67
67
  costFunction?: CostFunction<TAgent>;
68
68
 
@@ -71,50 +71,80 @@ export type AgentDecideInput<TAgent extends AnyAgent> = Omit<
71
71
  * Defaults to 2.
72
72
  */
73
73
  maxAttempts?: number;
74
- strategy?: AgentStrategy<TAgent>;
74
+ /**
75
+ * The policy to use for making a decision.
76
+ */
77
+ policy?: AgentPolicy<TAgent>;
75
78
  model?: LanguageModel;
76
- };
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;
77
97
 
78
98
  export type AgentStep<TAgent extends AnyAgent> = {
79
99
  /** The event to take */
80
100
  event: EventFromAgent<TAgent>;
81
101
  /** The next expected state after taking the event */
82
- state: ObservedState<TAgent> | undefined;
102
+ state: ObservedState<TAgent> | null;
83
103
  };
84
104
 
85
105
  export type AgentPath<TAgent extends AnyAgent> = {
86
106
  /** The expected ending state of the path */
87
- state: ObservedState<TAgent> | undefined;
107
+ state: ObservedState<TAgent> | null;
88
108
  /** The steps to reach the ending state */
89
109
  steps: Array<AgentStep<TAgent>>;
90
110
  weight?: number;
91
111
  };
92
112
 
93
- export type AgentDecision<TAgent extends AnyAgent> = {
94
- 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;
95
128
  /**
96
- * The strategy used to generate the decision
129
+ * The policy used to generate the decision
97
130
  */
98
- strategy: string;
131
+ policy: string | null;
99
132
  goal: string;
100
133
  /**
101
134
  * The ending state of the decision.
102
135
  */
103
- goalState: ObservedState<TAgent> | undefined;
136
+ goalState: ObservedState<TAgent> | null;
104
137
  /**
105
138
  * The next event that the agent decided needs to occur to achieve the `goal`.
106
139
  *
107
140
  * This next event is chosen from the
108
141
  */
109
- nextEvent: EventFromAgent<TAgent> | undefined;
142
+ nextEvent: EventFromAgent<TAgent> | null;
110
143
  /**
111
144
  * The paths that the agent can take to achieve the goal.
112
145
  */
113
146
  paths: AgentPath<TAgent>[];
114
- episodeId: string;
115
- timestamp: number;
116
- // result: GenerateObjectResult<any>;
117
- };
147
+ }
118
148
 
119
149
  export interface TransitionData {
120
150
  eventType: string;
@@ -140,17 +170,30 @@ export type PromptTemplate<TAgent extends AnyAgent> = (data: {
140
170
  */
141
171
  transitions?: TransitionData[];
142
172
  /**
143
- * Past observations
173
+ * Relevant past observations
144
174
  */
145
175
  observations?: AgentObservation<any>[]; // TODO
176
+ /**
177
+ * Relevant feedback
178
+ */
146
179
  feedback?: AgentFeedback[];
180
+ /**
181
+ * Relevant messages
182
+ */
147
183
  messages?: AgentMessage[];
184
+ /**
185
+ * Relevant past decisions
186
+ */
148
187
  decisions?: AgentDecision<TAgent>[];
188
+ /**
189
+ * Relevant past insights
190
+ */
191
+ insights?: AgentInsight[];
149
192
  }) => string;
150
193
 
151
- export type AgentStrategy<TAgent extends AnyAgent> = (
194
+ export type AgentPolicy<TAgent extends AnyAgent = AnyAgent> = (
152
195
  agent: TAgent,
153
- input: AgentDecideInput<EventFromAgent<TAgent>>
196
+ input: AgentDecideInput<TAgent>
154
197
  ) => Promise<AgentDecision<TAgent> | undefined>;
155
198
 
156
199
  export type AgentInteractInput<T extends AnyAgent> = Omit<
@@ -160,39 +203,44 @@ export type AgentInteractInput<T extends AnyAgent> = Omit<
160
203
  state?: never;
161
204
  };
162
205
 
163
- export type AgentFeedback = {
164
- score: number;
206
+ export interface AgentFeedback extends BaseProperties {
207
+ decisionId: string;
208
+ reward: number;
165
209
  comment: string | undefined;
166
210
  attributes: Record<string, any>;
167
- timestamp: number;
168
- episodeId: string;
169
- } & (
170
- | { observationId: string; decisionId?: never }
171
- | { decisionId: string; observationId?: never }
172
- );
173
-
174
- export type AgentFeedbackInput = {
175
- episodeId?: string;
176
- score: number;
177
- comment?: string;
178
- attributes?: Record<string, any>;
179
- timestamp?: number;
180
- } & (
181
- | { observationId: string; decisionId?: never }
182
- | { decisionId: string; observationId?: never }
183
- );
211
+ }
184
212
 
185
- export type AgentMessage = CoreMessage & {
186
- timestamp: number;
213
+ interface BaseProperties {
187
214
  id: string;
215
+ episodeId: string;
216
+ timestamp: number;
217
+ }
218
+
219
+ type BaseInput = Partial<BaseProperties>;
220
+
221
+ export interface AgentFeedbackInput extends BaseInput {
188
222
  /**
189
- * The response ID of the message, which references
190
- * which message this message is responding to, if any.
223
+ * The decision ID that this feedback is relevant for.
191
224
  */
192
- responseId?: string;
193
- result?: GenerateTextResult<any>;
194
- episodeId: string;
195
- };
225
+ decisionId: string;
226
+ reward: number;
227
+ comment?: string;
228
+ attributes?: Record<string, any>;
229
+ }
230
+
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
+ };
196
244
 
197
245
  type JSONObject = {
198
246
  [key: string]: JSONValue;
@@ -205,24 +253,6 @@ type LanguageModelV1ProviderMetadata = Record<
205
253
  Record<string, JSONValue>
206
254
  >;
207
255
 
208
- interface LanguageModelV1ImagePart {
209
- type: 'image';
210
- /**
211
- Image data as a Uint8Array (e.g. from a Blob or Buffer) or a URL.
212
- */
213
- image: Uint8Array | URL;
214
- /**
215
- Optional mime type of the image.
216
- */
217
- mimeType?: string;
218
- /**
219
- * Additional provider-specific metadata. They are passed through
220
- * to the provider from the AI SDK and enable provider-specific
221
- * functionality that can be fully encapsulated in the provider.
222
- */
223
- providerMetadata?: LanguageModelV1ProviderMetadata;
224
- }
225
-
226
256
  export interface LanguageModelV1TextPart {
227
257
  type: 'text';
228
258
  /**
@@ -258,56 +288,6 @@ Arguments of the tool call. This is a JSON-serializable object that matches the
258
288
  */
259
289
  providerMetadata?: LanguageModelV1ProviderMetadata;
260
290
  }
261
- interface LanguageModelV1ToolResultPart {
262
- type: 'tool-result';
263
- /**
264
- ID of the tool call that this result is associated with.
265
- */
266
- toolCallId: string;
267
- /**
268
- Name of the tool that generated this result.
269
- */
270
- toolName: string;
271
- /**
272
- Result of the tool call. This is a JSON-serializable object.
273
- */
274
- result: unknown;
275
- /**
276
- Optional flag if the result is an error or an error message.
277
- */
278
- isError?: boolean;
279
- /**
280
- * Additional provider-specific metadata. They are passed through
281
- * to the provider from the AI SDK and enable provider-specific
282
- * functionality that can be fully encapsulated in the provider.
283
- */
284
- providerMetadata?: LanguageModelV1ProviderMetadata;
285
- }
286
- type LanguageModelV1Message = (
287
- | {
288
- role: 'system';
289
- content: string;
290
- }
291
- | {
292
- role: 'user';
293
- content: Array<LanguageModelV1TextPart | LanguageModelV1ImagePart>;
294
- }
295
- | {
296
- role: 'assistant';
297
- content: Array<LanguageModelV1TextPart | LanguageModelV1ToolCallPart>;
298
- }
299
- | {
300
- role: 'tool';
301
- content: Array<LanguageModelV1ToolResultPart>;
302
- }
303
- ) & {
304
- /**
305
- * Additional provider-specific metadata. They are passed through
306
- * to the provider from the AI SDK and enable provider-specific
307
- * functionality that can be fully encapsulated in the provider.
308
- */
309
- providerMetadata?: LanguageModelV1ProviderMetadata;
310
- };
311
291
 
312
292
  export type AgentMessageInput = CoreMessage & {
313
293
  timestamp?: number;
@@ -317,47 +297,36 @@ export type AgentMessageInput = CoreMessage & {
317
297
  * which message this message is responding to, if any.
318
298
  */
319
299
  responseId?: string;
320
- result?: GenerateTextResult<any>;
300
+ result?: GenerateTextResult<any, any>;
321
301
  };
322
302
 
323
303
  export interface AgentObservation<TActor extends ActorRefLike> {
324
304
  id: string;
305
+ episodeId: string;
306
+ /**
307
+ * The decision that this observation is relevant for
308
+ */
325
309
  decisionId?: string | undefined;
326
310
  goal?: string;
327
311
  prevState: SnapshotFrom<TActor> | undefined;
328
312
  event: EventFrom<TActor> | undefined;
329
313
  state: SnapshotFrom<TActor>;
330
314
  // machineHash: string | undefined;
331
- episodeId: string;
332
315
  timestamp: number;
333
316
  }
334
317
 
335
- export interface AgentObservationInput<TAgent extends AnyAgent> {
336
- id?: string;
337
- episodeId?: string;
318
+ export interface AgentObservationInput<TAgent extends AnyAgent>
319
+ extends BaseInput {
320
+ state: ObservedState<TAgent>;
338
321
  /**
339
322
  * The agent decision that the observation is relevant for
340
323
  */
341
324
  decisionId?: string | undefined;
342
325
  prevState?: ObservedState<TAgent>;
343
326
  event?: AnyEventObject;
344
- state: ObservedState<TAgent>;
345
- // machine?: AnyStateMachine;
346
- timestamp?: number;
347
327
  goal?: string | undefined;
348
328
  }
349
329
 
350
- export type AgentDecisionInput = {
351
- goal: string;
352
- model?: LanguageModel;
353
- context?: Record<string, any>;
354
- } & Omit<Parameters<typeof generateText>[0], 'model' | 'tools' | 'prompt'>;
355
-
356
- export type AgentDecisionLogic<TAgent extends AnyAgent> = PromiseActorLogic<
357
- AgentDecision<TAgent> | undefined,
358
- AgentDecisionInput | string
359
- >;
360
-
361
330
  export type AgentEmitted<TAgent extends AnyAgent> =
362
331
  | {
363
332
  type: 'feedback';
@@ -374,6 +343,10 @@ export type AgentEmitted<TAgent extends AnyAgent> =
374
343
  | {
375
344
  type: 'decision';
376
345
  decision: AgentDecision<TAgent>;
346
+ }
347
+ | {
348
+ type: 'insight';
349
+ insight: AgentInsight;
377
350
  };
378
351
 
379
352
  export type AgentLogic<TAgent extends AnyAgent> = ActorLogic<
@@ -393,6 +366,10 @@ export type AgentLogic<TAgent extends AnyAgent> = ActorLogic<
393
366
  | {
394
367
  type: 'agent.decision';
395
368
  decision: AgentDecision<TAgent>;
369
+ }
370
+ | {
371
+ type: 'agent.insight';
372
+ insight: AgentInsight;
396
373
  },
397
374
  any, // TODO: input
398
375
  any,
@@ -414,29 +391,29 @@ export type ContextFromZodContextMapping<
414
391
  [K in keyof TContextSchema & string]: TypeOf<TContextSchema[K]>;
415
392
  };
416
393
 
417
- export type AnyAgent = Agent<any, any, any, any>;
394
+ export type AnyAgent = Agent<any, any>;
418
395
 
419
396
  export type FromAgent<T> = T | ((agent: AnyAgent) => T | Promise<T>);
420
397
 
421
- export type CommonTextOptions<TAgent extends AnyAgent> = {
398
+ export type CommonTextOptions = {
422
399
  prompt: FromAgent<string>;
423
400
  model?: LanguageModel;
424
- messages?: FromAgent<CoreMessage[]>;
401
+ messages?: CoreMessage[];
425
402
  template?: PromptTemplate<any>;
426
403
  context?: Record<string, any>;
427
404
  };
428
405
 
429
- export type AgentGenerateTextOptions<TAgent extends AnyAgent> = Omit<
406
+ export type AgentGenerateTextOptions = Omit<
430
407
  GenerateTextOptions,
431
408
  'model' | 'prompt' | 'messages'
432
409
  > &
433
- CommonTextOptions<TAgent>;
410
+ CommonTextOptions;
434
411
 
435
- export type AgentStreamTextOptions<TAgent extends AnyAgent> = Omit<
412
+ export type AgentStreamTextOptions = Omit<
436
413
  StreamTextOptions,
437
414
  'model' | 'prompt' | 'messages'
438
415
  > &
439
- CommonTextOptions<TAgent>;
416
+ CommonTextOptions;
440
417
 
441
418
  export interface ObservedState<TAgent extends AnyAgent> {
442
419
  /**
@@ -456,10 +433,11 @@ export type ObservedStateFrom<TActor extends ActorRefLike> = Pick<
456
433
  >;
457
434
 
458
435
  export type AgentMemoryContext<TAgent extends AnyAgent> = {
459
- observations: AgentObservation<TAgent>[]; // TODO
436
+ observations: AgentObservation<any>[]; // TODO
460
437
  messages: AgentMessage[];
461
438
  decisions: AgentDecision<TAgent>[];
462
439
  feedback: AgentFeedback[];
440
+ insights: AgentInsight[];
463
441
  };
464
442
 
465
443
  export interface AgentLongTermMemory<TAgent extends AnyAgent> {
@@ -482,11 +460,9 @@ export type MaybePromise<T> = T | Promise<T>;
482
460
 
483
461
  export type EventFromAgent<T extends AnyAgent> = T extends Agent<
484
462
  infer _,
485
- infer __,
486
- infer TEvents,
487
- infer ___
463
+ infer TEventSchemas
488
464
  >
489
- ? TEvents
465
+ ? EventsFromZodEventMapping<TEventSchemas>
490
466
  : never;
491
467
 
492
468
  export type TypesFromAgent<T extends AnyAgent> = T extends Agent<
@@ -505,3 +481,31 @@ export type ContextFromAgent<T extends AnyAgent> = T extends Agent<
505
481
  >
506
482
  ? ContextFromZodContextMapping<TContextSchema>
507
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
+ }