@statelyai/agent 2.0.0-next.2 → 2.0.0-next.3
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/.changeset/pink-eagles-deliver.md +13 -0
- package/.changeset/pre.json +6 -1
- package/.changeset/quiet-turtles-do.md +7 -0
- package/.changeset/sweet-clouds-mix.md +16 -0
- package/.changeset/swift-mangos-rush.md +5 -0
- package/.changeset/tough-ways-rhyme.md +5 -0
- package/CHANGELOG.md +39 -0
- package/dist/index.d.mts +85 -78
- package/dist/index.d.ts +85 -78
- package/dist/index.js +28 -26
- package/dist/index.mjs +28 -26
- package/examples/cot.ts +19 -53
- package/examples/customer-service-sim.ts +3 -3
- package/examples/email.ts +2 -10
- package/examples/example.ts +2 -2
- package/examples/goal.ts +2 -2
- package/examples/joke.ts +10 -10
- package/examples/learn-from-feedback.ts +47 -24
- package/examples/number.ts +2 -2
- package/examples/raffle.ts +2 -2
- package/examples/support.ts +7 -11
- package/examples/ticTacToe.ts +2 -2
- package/examples/todo.ts +3 -3
- package/examples/tutor.ts +2 -2
- package/examples/verify.ts +2 -2
- package/examples/weather-agent.ts +3 -5
- package/examples/weather.ts +10 -7
- package/package.json +1 -1
- package/src/agent.test.ts +20 -37
- package/src/agent.ts +48 -28
- package/src/decide.ts +12 -12
- package/src/strategies/chainOfThought.ts +4 -2
- package/src/strategies/shortestPath.ts +7 -2
- package/src/strategies/simple.ts +4 -15
- package/src/templates/defaultText.ts +3 -0
- package/src/text.ts +13 -13
- package/src/types.ts +83 -76
- package/src/utils.ts +1 -1
package/src/types.ts
CHANGED
|
@@ -27,18 +27,22 @@ export type GenerateTextOptions = Parameters<typeof generateText>[0];
|
|
|
27
27
|
|
|
28
28
|
export type StreamTextOptions = Parameters<typeof streamText>[0];
|
|
29
29
|
|
|
30
|
-
export type CostFunction<
|
|
31
|
-
path: AgentPath<
|
|
30
|
+
export type CostFunction<TAgent extends AnyAgent> = (
|
|
31
|
+
path: AgentPath<TAgent>
|
|
32
32
|
) => number;
|
|
33
33
|
|
|
34
|
-
export type AgentDecideInput<
|
|
35
|
-
AgentGenerateTextOptions
|
|
34
|
+
export type AgentDecideInput<TAgent extends AnyAgent> = Omit<
|
|
35
|
+
AgentGenerateTextOptions<TAgent>,
|
|
36
36
|
'prompt' | 'tools'
|
|
37
37
|
> & {
|
|
38
38
|
/**
|
|
39
39
|
* The currently observed state.
|
|
40
40
|
*/
|
|
41
|
-
state: ObservedState
|
|
41
|
+
state: ObservedState<TAgent>;
|
|
42
|
+
/**
|
|
43
|
+
* The context to provide in the prompt to the agent. This overrides the `state.context`.
|
|
44
|
+
*/
|
|
45
|
+
context?: Record<string, any>;
|
|
42
46
|
/**
|
|
43
47
|
* The goal for the agent to accomplish.
|
|
44
48
|
* The agent will make a decision based on this goal.
|
|
@@ -54,15 +58,11 @@ export type AgentDecideInput<TEvent extends EventObject> = Omit<
|
|
|
54
58
|
* is interacting with.
|
|
55
59
|
*/
|
|
56
60
|
machine?: AnyStateMachine;
|
|
57
|
-
/**
|
|
58
|
-
* The previous decision made by the agent.
|
|
59
|
-
*/
|
|
60
|
-
prevDecision?: AgentDecision<TEvent>;
|
|
61
61
|
|
|
62
62
|
/**
|
|
63
63
|
* The total cost of the path to the goal state.
|
|
64
64
|
*/
|
|
65
|
-
costFunction?: CostFunction<
|
|
65
|
+
costFunction?: CostFunction<TAgent>;
|
|
66
66
|
|
|
67
67
|
/**
|
|
68
68
|
* The maximum number of attempts to make a decision.
|
|
@@ -71,22 +71,23 @@ export type AgentDecideInput<TEvent extends EventObject> = Omit<
|
|
|
71
71
|
maxAttempts?: number;
|
|
72
72
|
};
|
|
73
73
|
|
|
74
|
-
export type AgentStep<
|
|
74
|
+
export type AgentStep<TAgent extends AnyAgent> = {
|
|
75
75
|
/** The event to take */
|
|
76
|
-
event:
|
|
76
|
+
event: EventFromAgent<TAgent>;
|
|
77
77
|
/** The next expected state after taking the event */
|
|
78
|
-
state: ObservedState | undefined;
|
|
78
|
+
state: ObservedState<TAgent> | undefined;
|
|
79
79
|
};
|
|
80
80
|
|
|
81
|
-
export type AgentPath<
|
|
81
|
+
export type AgentPath<TAgent extends AnyAgent> = {
|
|
82
82
|
/** The expected ending state of the path */
|
|
83
|
-
state: ObservedState | undefined;
|
|
83
|
+
state: ObservedState<TAgent> | undefined;
|
|
84
84
|
/** The steps to reach the ending state */
|
|
85
|
-
steps: Array<AgentStep<
|
|
85
|
+
steps: Array<AgentStep<TAgent>>;
|
|
86
86
|
weight?: number;
|
|
87
87
|
};
|
|
88
88
|
|
|
89
|
-
export type AgentDecision<
|
|
89
|
+
export type AgentDecision<TAgent extends AnyAgent> = {
|
|
90
|
+
id: string;
|
|
90
91
|
/**
|
|
91
92
|
* The strategy used to generate the decision
|
|
92
93
|
*/
|
|
@@ -95,17 +96,17 @@ export type AgentDecision<TEvent extends EventObject> = {
|
|
|
95
96
|
/**
|
|
96
97
|
* The ending state of the decision.
|
|
97
98
|
*/
|
|
98
|
-
goalState: ObservedState | undefined;
|
|
99
|
+
goalState: ObservedState<TAgent> | undefined;
|
|
99
100
|
/**
|
|
100
101
|
* The next event that the agent decided needs to occur to achieve the `goal`.
|
|
101
102
|
*
|
|
102
103
|
* This next event is chosen from the
|
|
103
104
|
*/
|
|
104
|
-
nextEvent:
|
|
105
|
+
nextEvent: EventFromAgent<TAgent> | undefined;
|
|
105
106
|
/**
|
|
106
107
|
* The paths that the agent can take to achieve the goal.
|
|
107
108
|
*/
|
|
108
|
-
paths: AgentPath<
|
|
109
|
+
paths: AgentPath<TAgent>[];
|
|
109
110
|
episodeId: string;
|
|
110
111
|
timestamp: number;
|
|
111
112
|
// result: GenerateObjectResult<any>;
|
|
@@ -118,17 +119,13 @@ export interface TransitionData {
|
|
|
118
119
|
target?: any;
|
|
119
120
|
}
|
|
120
121
|
|
|
121
|
-
export type PromptTemplate<
|
|
122
|
+
export type PromptTemplate<TAgent extends AnyAgent> = (data: {
|
|
122
123
|
goal: string;
|
|
123
124
|
/**
|
|
124
125
|
* The observed state
|
|
125
126
|
*/
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
* The context to provide.
|
|
129
|
-
* This overrides the observed state.context, if provided.
|
|
130
|
-
*/
|
|
131
|
-
context?: any;
|
|
127
|
+
stateValue?: any;
|
|
128
|
+
context?: Record<string, any>;
|
|
132
129
|
/**
|
|
133
130
|
* The state machine model of the observed environment
|
|
134
131
|
*/
|
|
@@ -144,29 +141,34 @@ export type PromptTemplate<TEvents extends EventObject> = (data: {
|
|
|
144
141
|
observations?: AgentObservation<any>[]; // TODO
|
|
145
142
|
feedback?: AgentFeedback[];
|
|
146
143
|
messages?: AgentMessage[];
|
|
147
|
-
decisions?: AgentDecision<
|
|
144
|
+
decisions?: AgentDecision<TAgent>[];
|
|
148
145
|
}) => string;
|
|
149
146
|
|
|
150
|
-
export type AgentStrategy<
|
|
151
|
-
agent:
|
|
152
|
-
input: AgentDecideInput<
|
|
153
|
-
) => Promise<AgentDecision<
|
|
147
|
+
export type AgentStrategy<TAgent extends AnyAgent> = (
|
|
148
|
+
agent: TAgent,
|
|
149
|
+
input: AgentDecideInput<EventFromAgent<TAgent>>
|
|
150
|
+
) => Promise<AgentDecision<TAgent> | undefined>;
|
|
154
151
|
|
|
155
152
|
export type AgentInteractInput<T extends AnyAgent> = Omit<
|
|
156
153
|
AgentDecideOptions<T>,
|
|
157
154
|
'state'
|
|
158
|
-
|
|
155
|
+
> & {
|
|
156
|
+
state?: never;
|
|
157
|
+
};
|
|
159
158
|
|
|
160
|
-
export type AgentDecideOptions<
|
|
159
|
+
export type AgentDecideOptions<TAgent extends AnyAgent> = {
|
|
161
160
|
goal: string;
|
|
162
|
-
state: ObservedState
|
|
161
|
+
state: ObservedState<TAgent>;
|
|
162
|
+
/**
|
|
163
|
+
* The context to provide in the prompt to the agent. This overrides the `state.context`.
|
|
164
|
+
*/
|
|
163
165
|
context?: Record<string, any>;
|
|
164
166
|
machine?: AnyStateMachine;
|
|
165
167
|
model?: LanguageModel;
|
|
166
168
|
execute?: (event: AnyEventObject) => Promise<void>;
|
|
167
|
-
strategy?: AgentStrategy<
|
|
169
|
+
strategy?: AgentStrategy<TAgent>;
|
|
168
170
|
events?: ZodEventMapping;
|
|
169
|
-
allowedEvents?: Array<
|
|
171
|
+
allowedEvents?: Array<EventFromAgent<TAgent>['type']>;
|
|
170
172
|
/**
|
|
171
173
|
* The maximum number of times the agent will attempt to make a decision.
|
|
172
174
|
* Defaults to 2.
|
|
@@ -175,8 +177,9 @@ export type AgentDecideOptions<T extends AnyAgent> = {
|
|
|
175
177
|
} & Omit<Parameters<typeof generateText>[0], 'model' | 'tools' | 'prompt'>;
|
|
176
178
|
|
|
177
179
|
export interface AgentFeedback {
|
|
178
|
-
goal: string;
|
|
179
180
|
observationId: string;
|
|
181
|
+
score: number;
|
|
182
|
+
comment: string | undefined;
|
|
180
183
|
/**
|
|
181
184
|
* The message correlation that the feedback is relevant for
|
|
182
185
|
*/
|
|
@@ -186,9 +189,10 @@ export interface AgentFeedback {
|
|
|
186
189
|
}
|
|
187
190
|
|
|
188
191
|
export interface AgentFeedbackInput {
|
|
189
|
-
goal: string;
|
|
190
192
|
observationId: string;
|
|
191
|
-
|
|
193
|
+
score: number;
|
|
194
|
+
comment?: string;
|
|
195
|
+
attributes?: Record<string, any>;
|
|
192
196
|
timestamp?: number;
|
|
193
197
|
}
|
|
194
198
|
|
|
@@ -332,7 +336,7 @@ export type AgentMessageInput = CoreMessage & {
|
|
|
332
336
|
|
|
333
337
|
export interface AgentObservation<TActor extends ActorRefLike> {
|
|
334
338
|
id: string;
|
|
335
|
-
|
|
339
|
+
goal?: string;
|
|
336
340
|
prevState: SnapshotFrom<TActor> | undefined;
|
|
337
341
|
event: EventFrom<TActor> | undefined;
|
|
338
342
|
state: SnapshotFrom<TActor>;
|
|
@@ -341,13 +345,14 @@ export interface AgentObservation<TActor extends ActorRefLike> {
|
|
|
341
345
|
timestamp: number;
|
|
342
346
|
}
|
|
343
347
|
|
|
344
|
-
export interface AgentObservationInput {
|
|
348
|
+
export interface AgentObservationInput<TAgent extends AnyAgent> {
|
|
345
349
|
id?: string;
|
|
346
|
-
prevState?: ObservedState
|
|
350
|
+
prevState?: ObservedState<TAgent>;
|
|
347
351
|
event?: AnyEventObject;
|
|
348
|
-
state: ObservedState
|
|
352
|
+
state: ObservedState<TAgent>;
|
|
349
353
|
machine?: AnyStateMachine;
|
|
350
354
|
timestamp?: number;
|
|
355
|
+
goal: string | undefined;
|
|
351
356
|
}
|
|
352
357
|
|
|
353
358
|
export type AgentDecisionInput = {
|
|
@@ -356,12 +361,12 @@ export type AgentDecisionInput = {
|
|
|
356
361
|
context?: Record<string, any>;
|
|
357
362
|
} & Omit<Parameters<typeof generateText>[0], 'model' | 'tools' | 'prompt'>;
|
|
358
363
|
|
|
359
|
-
export type AgentDecisionLogic<
|
|
360
|
-
AgentDecision<
|
|
364
|
+
export type AgentDecisionLogic<TAgent extends AnyAgent> = PromiseActorLogic<
|
|
365
|
+
AgentDecision<TAgent> | undefined,
|
|
361
366
|
AgentDecisionInput | string
|
|
362
367
|
>;
|
|
363
368
|
|
|
364
|
-
export type AgentEmitted<
|
|
369
|
+
export type AgentEmitted<TAgent extends AnyAgent> =
|
|
365
370
|
| {
|
|
366
371
|
type: 'feedback';
|
|
367
372
|
feedback: AgentFeedback;
|
|
@@ -376,11 +381,11 @@ export type AgentEmitted<TEvents extends EventObject> =
|
|
|
376
381
|
}
|
|
377
382
|
| {
|
|
378
383
|
type: 'decision';
|
|
379
|
-
decision: AgentDecision<
|
|
384
|
+
decision: AgentDecision<TAgent>;
|
|
380
385
|
};
|
|
381
386
|
|
|
382
|
-
export type AgentLogic<
|
|
383
|
-
TransitionSnapshot<AgentMemoryContext
|
|
387
|
+
export type AgentLogic<TAgent extends AnyAgent> = ActorLogic<
|
|
388
|
+
TransitionSnapshot<AgentMemoryContext<TAgent>>,
|
|
384
389
|
| {
|
|
385
390
|
type: 'agent.feedback';
|
|
386
391
|
feedback: AgentFeedback;
|
|
@@ -395,19 +400,21 @@ export type AgentLogic<TEvents extends EventObject> = ActorLogic<
|
|
|
395
400
|
}
|
|
396
401
|
| {
|
|
397
402
|
type: 'agent.decision';
|
|
398
|
-
decision: AgentDecision<
|
|
403
|
+
decision: AgentDecision<TAgent>;
|
|
399
404
|
},
|
|
400
405
|
any, // TODO: input
|
|
401
406
|
any,
|
|
402
|
-
AgentEmitted<
|
|
407
|
+
AgentEmitted<TAgent>
|
|
403
408
|
>;
|
|
404
409
|
|
|
405
410
|
export type EventsFromZodEventMapping<TEventSchemas extends ZodEventMapping> =
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
+
Compute<
|
|
412
|
+
Values<{
|
|
413
|
+
[K in keyof TEventSchemas & string]: {
|
|
414
|
+
type: K;
|
|
415
|
+
} & TypeOf<TEventSchemas[K]>;
|
|
416
|
+
}>
|
|
417
|
+
>;
|
|
411
418
|
|
|
412
419
|
export type ContextFromZodContextMapping<
|
|
413
420
|
TContextSchema extends ZodContextMapping
|
|
@@ -419,27 +426,27 @@ export type AnyAgent = Agent<any, any, any, any>;
|
|
|
419
426
|
|
|
420
427
|
export type FromAgent<T> = T | ((agent: AnyAgent) => T | Promise<T>);
|
|
421
428
|
|
|
422
|
-
export type CommonTextOptions = {
|
|
429
|
+
export type CommonTextOptions<TAgent extends AnyAgent> = {
|
|
423
430
|
prompt: FromAgent<string>;
|
|
424
431
|
model?: LanguageModel;
|
|
425
|
-
context?: Record<string, any>;
|
|
426
432
|
messages?: FromAgent<CoreMessage[]>;
|
|
427
433
|
template?: PromptTemplate<any>;
|
|
434
|
+
context?: Record<string, any>;
|
|
428
435
|
};
|
|
429
436
|
|
|
430
|
-
export type AgentGenerateTextOptions = Omit<
|
|
437
|
+
export type AgentGenerateTextOptions<TAgent extends AnyAgent> = Omit<
|
|
431
438
|
GenerateTextOptions,
|
|
432
439
|
'model' | 'prompt' | 'messages'
|
|
433
440
|
> &
|
|
434
|
-
CommonTextOptions
|
|
441
|
+
CommonTextOptions<TAgent>;
|
|
435
442
|
|
|
436
|
-
export type AgentStreamTextOptions = Omit<
|
|
443
|
+
export type AgentStreamTextOptions<TAgent extends AnyAgent> = Omit<
|
|
437
444
|
StreamTextOptions,
|
|
438
445
|
'model' | 'prompt' | 'messages'
|
|
439
446
|
> &
|
|
440
|
-
CommonTextOptions
|
|
447
|
+
CommonTextOptions<TAgent>;
|
|
441
448
|
|
|
442
|
-
export interface ObservedState {
|
|
449
|
+
export interface ObservedState<TAgent extends AnyAgent> {
|
|
443
450
|
/**
|
|
444
451
|
* The current state value of the state machine, e.g.
|
|
445
452
|
* `"loading"` or `"processing"` or `"ready"`
|
|
@@ -448,7 +455,7 @@ export interface ObservedState {
|
|
|
448
455
|
/**
|
|
449
456
|
* Additional contextual data related to the current state
|
|
450
457
|
*/
|
|
451
|
-
context?:
|
|
458
|
+
context?: ContextFromAgent<TAgent>;
|
|
452
459
|
}
|
|
453
460
|
|
|
454
461
|
export type ObservedStateFrom<TActor extends ActorRefLike> = Pick<
|
|
@@ -456,24 +463,24 @@ export type ObservedStateFrom<TActor extends ActorRefLike> = Pick<
|
|
|
456
463
|
'value' | 'context'
|
|
457
464
|
>;
|
|
458
465
|
|
|
459
|
-
export type AgentMemoryContext = {
|
|
460
|
-
observations: AgentObservation<
|
|
466
|
+
export type AgentMemoryContext<TAgent extends AnyAgent> = {
|
|
467
|
+
observations: AgentObservation<TAgent>[]; // TODO
|
|
461
468
|
messages: AgentMessage[];
|
|
462
|
-
decisions: AgentDecision<
|
|
469
|
+
decisions: AgentDecision<TAgent>[];
|
|
463
470
|
feedback: AgentFeedback[];
|
|
464
471
|
};
|
|
465
472
|
|
|
466
|
-
export interface AgentLongTermMemory {
|
|
467
|
-
get<K extends keyof AgentMemoryContext
|
|
473
|
+
export interface AgentLongTermMemory<TAgent extends AnyAgent> {
|
|
474
|
+
get<K extends keyof AgentMemoryContext<TAgent>>(
|
|
468
475
|
key: K
|
|
469
|
-
): Promise<AgentMemoryContext[K]>;
|
|
470
|
-
append<K extends keyof AgentMemoryContext
|
|
476
|
+
): Promise<AgentMemoryContext<TAgent>[K]>;
|
|
477
|
+
append<K extends keyof AgentMemoryContext<TAgent>>(
|
|
471
478
|
key: K,
|
|
472
|
-
item: AgentMemoryContext[K][0]
|
|
479
|
+
item: AgentMemoryContext<TAgent>[K][0]
|
|
473
480
|
): Promise<void>;
|
|
474
|
-
set<K extends keyof AgentMemoryContext
|
|
481
|
+
set<K extends keyof AgentMemoryContext<TAgent>>(
|
|
475
482
|
key: K,
|
|
476
|
-
items: AgentMemoryContext[K]
|
|
483
|
+
items: AgentMemoryContext<TAgent>[K]
|
|
477
484
|
): Promise<void>;
|
|
478
485
|
}
|
|
479
486
|
|
|
@@ -481,7 +488,7 @@ export type Compute<A extends any> = { [K in keyof A]: A[K] } & unknown;
|
|
|
481
488
|
|
|
482
489
|
export type MaybePromise<T> = T | Promise<T>;
|
|
483
490
|
|
|
484
|
-
export type
|
|
491
|
+
export type EventFromAgent<T extends AnyAgent> = T extends Agent<
|
|
485
492
|
infer _,
|
|
486
493
|
infer __,
|
|
487
494
|
infer TEvents,
|