@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.
- package/.changeset/calm-beans-talk.md +5 -0
- package/.changeset/long-guests-explode.md +5 -0
- package/.changeset/nice-pants-rule.md +10 -0
- package/.changeset/odd-kiwis-compare.md +5 -0
- package/.changeset/pre.json +4 -0
- package/CHANGELOG.md +23 -0
- package/architecture.tldr +797 -0
- package/dist/index.d.mts +198 -140
- package/dist/index.d.ts +198 -140
- package/dist/index.js +4397 -148
- package/dist/index.mjs +4397 -149
- package/examples/chatbot.ts +9 -5
- package/examples/cot.ts +2 -4
- package/examples/jugs.ts +2 -2
- package/examples/learn-from-feedback.ts +7 -7
- package/examples/newspaper.ts +1 -1
- package/examples/rewoo.ts +62 -0
- package/examples/river-crossing.ts +2 -2
- package/examples/serverless.ts +71 -0
- package/examples/simple.ts +1 -1
- package/examples/ticTacToe.ts +6 -2
- package/examples/wiki.ts +2 -2
- package/package.json +14 -12
- package/readme.md +57 -0
- package/src/agent.test.ts +387 -30
- package/src/agent.ts +177 -64
- package/src/decide.test.ts +24 -2
- package/src/decide.ts +34 -78
- package/src/index.ts +1 -0
- package/src/{strategies/chainOfThought.ts → policies/chainOfThoughtPolicy.ts} +7 -9
- package/src/policies/index.ts +3 -0
- package/src/{strategies/shortestPath.test.ts → policies/shortestPathPolicy.test.ts} +2 -2
- package/src/{strategies/shortestPath.ts → policies/shortestPathPolicy.ts} +8 -8
- package/src/{strategies/simple.ts → policies/toolPolicy.ts} +27 -26
- package/src/text.ts +17 -22
- package/src/types.ts +162 -166
- package/src/agent-experimental.ts +0 -221
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { generateText, streamText, LanguageModel, CoreMessage, GenerateTextResult, LanguageModelV1, CoreTool } from 'ai';
|
|
3
|
-
import { AnyStateMachine, AnyEventObject, ActorRefLike, SnapshotFrom, EventFrom, PromiseActorLogic, ActorLogic, TransitionSnapshot, Values, StateValue, EventObject, Actor, Subscription, ObservableActorLogic } from 'xstate';
|
|
1
|
+
import { AnyStateMachine, ActorRefLike, SnapshotFrom, EventFrom, AnyEventObject, ActorLogic, TransitionSnapshot, Values, StateValue, Actor, Subscription, ObservableActorLogic, PromiseActorLogic } from 'xstate';
|
|
4
2
|
import { SomeZodObject, ZodType, TypeOf } from 'zod';
|
|
3
|
+
import { generateText, streamText, LanguageModel, CoreMessage, GenerateTextResult, LanguageModelV1, CoreTool } from 'ai';
|
|
5
4
|
|
|
6
5
|
type ZodEventMapping = {
|
|
7
6
|
[eventType: string]: SomeZodObject;
|
|
@@ -13,7 +12,11 @@ type ZodContextMapping = {
|
|
|
13
12
|
type GenerateTextOptions = Parameters<typeof generateText>[0];
|
|
14
13
|
type StreamTextOptions = Parameters<typeof streamText>[0];
|
|
15
14
|
type CostFunction<TAgent extends AnyAgent> = (path: AgentPath<TAgent>) => number;
|
|
16
|
-
type AgentDecideInput<TAgent extends AnyAgent> = Omit<AgentGenerateTextOptions
|
|
15
|
+
type AgentDecideInput<TAgent extends AnyAgent> = Omit<AgentGenerateTextOptions, 'model' | 'prompt' | 'tools' | 'toolChoice'> & {
|
|
16
|
+
/**
|
|
17
|
+
* The parent decision that this decision is a part of.
|
|
18
|
+
*/
|
|
19
|
+
decisionId?: string;
|
|
17
20
|
/**
|
|
18
21
|
* The currently observed state.
|
|
19
22
|
*/
|
|
@@ -31,14 +34,15 @@ type AgentDecideInput<TAgent extends AnyAgent> = Omit<AgentGenerateTextOptions<T
|
|
|
31
34
|
* The events that the agent can trigger. This is a mapping of
|
|
32
35
|
* event types to Zod event schemas.
|
|
33
36
|
*/
|
|
34
|
-
events
|
|
37
|
+
events?: ZodEventMapping;
|
|
38
|
+
allowedEvents?: Array<EventFromAgent<TAgent>['type']>;
|
|
35
39
|
/**
|
|
36
40
|
* The state machine that represents the environment the agent
|
|
37
41
|
* is interacting with.
|
|
38
42
|
*/
|
|
39
43
|
machine?: AnyStateMachine;
|
|
40
44
|
/**
|
|
41
|
-
*
|
|
45
|
+
* A function that calculates the total cost of the path to the goal state.
|
|
42
46
|
*/
|
|
43
47
|
costFunction?: CostFunction<TAgent>;
|
|
44
48
|
/**
|
|
@@ -46,44 +50,75 @@ type AgentDecideInput<TAgent extends AnyAgent> = Omit<AgentGenerateTextOptions<T
|
|
|
46
50
|
* Defaults to 2.
|
|
47
51
|
*/
|
|
48
52
|
maxAttempts?: number;
|
|
49
|
-
|
|
53
|
+
/**
|
|
54
|
+
* The policy to use for making a decision.
|
|
55
|
+
*/
|
|
56
|
+
policy?: AgentPolicy<TAgent>;
|
|
57
|
+
model?: LanguageModel;
|
|
58
|
+
/**
|
|
59
|
+
* The previous relevant feedback from the agent.
|
|
60
|
+
*/
|
|
61
|
+
feedback?: AgentFeedback[];
|
|
62
|
+
/**
|
|
63
|
+
* The previous relevant observations from the agent.
|
|
64
|
+
*/
|
|
65
|
+
observations?: AgentObservation<any>[];
|
|
66
|
+
/**
|
|
67
|
+
* The previous relevant decisions from the agent.
|
|
68
|
+
*/
|
|
69
|
+
decisions?: AgentDecision<TAgent>[];
|
|
70
|
+
/**
|
|
71
|
+
* The previous relevant insights from the agent.
|
|
72
|
+
*/
|
|
73
|
+
insights?: AgentInsight[];
|
|
74
|
+
toolChoice?: 'auto' | 'none' | 'required';
|
|
75
|
+
} & BaseInput;
|
|
50
76
|
type AgentStep<TAgent extends AnyAgent> = {
|
|
51
77
|
/** The event to take */
|
|
52
78
|
event: EventFromAgent<TAgent>;
|
|
53
79
|
/** The next expected state after taking the event */
|
|
54
|
-
state: ObservedState<TAgent> |
|
|
80
|
+
state: ObservedState<TAgent> | null;
|
|
55
81
|
};
|
|
56
82
|
type AgentPath<TAgent extends AnyAgent> = {
|
|
57
83
|
/** The expected ending state of the path */
|
|
58
|
-
state: ObservedState<TAgent> |
|
|
84
|
+
state: ObservedState<TAgent> | null;
|
|
59
85
|
/** The steps to reach the ending state */
|
|
60
86
|
steps: Array<AgentStep<TAgent>>;
|
|
61
87
|
weight?: number;
|
|
62
88
|
};
|
|
63
|
-
|
|
64
|
-
|
|
89
|
+
interface AgentDecisionInput<TAgent extends AnyAgent> extends BaseInput {
|
|
90
|
+
goal: string;
|
|
91
|
+
decisionId?: string | null;
|
|
92
|
+
policy?: string | null;
|
|
93
|
+
goalState?: ObservedState<TAgent> | null;
|
|
94
|
+
nextEvent?: EventFromAgent<TAgent> | null;
|
|
95
|
+
paths?: AgentPath<TAgent>[];
|
|
96
|
+
}
|
|
97
|
+
interface AgentDecision<TAgent extends AnyAgent = AnyAgent> extends BaseProperties {
|
|
65
98
|
/**
|
|
66
|
-
* The
|
|
99
|
+
* The parent decision that this decision is a part of.
|
|
67
100
|
*/
|
|
68
|
-
|
|
101
|
+
decisionId: string | null;
|
|
102
|
+
/**
|
|
103
|
+
* The policy used to generate the decision
|
|
104
|
+
*/
|
|
105
|
+
policy: string | null;
|
|
69
106
|
goal: string;
|
|
70
107
|
/**
|
|
71
108
|
* The ending state of the decision.
|
|
72
109
|
*/
|
|
73
|
-
goalState: ObservedState<TAgent> |
|
|
110
|
+
goalState: ObservedState<TAgent> | null;
|
|
74
111
|
/**
|
|
75
112
|
* The next event that the agent decided needs to occur to achieve the `goal`.
|
|
76
113
|
*
|
|
77
114
|
* This next event is chosen from the
|
|
78
115
|
*/
|
|
79
|
-
nextEvent: EventFromAgent<TAgent> |
|
|
116
|
+
nextEvent: EventFromAgent<TAgent> | null;
|
|
80
117
|
/**
|
|
81
118
|
* The paths that the agent can take to achieve the goal.
|
|
82
119
|
*/
|
|
83
120
|
paths: AgentPath<TAgent>[];
|
|
84
|
-
|
|
85
|
-
timestamp: number;
|
|
86
|
-
};
|
|
121
|
+
}
|
|
87
122
|
interface TransitionData {
|
|
88
123
|
eventType: string;
|
|
89
124
|
description?: string;
|
|
@@ -109,64 +144,62 @@ type PromptTemplate<TAgent extends AnyAgent> = (data: {
|
|
|
109
144
|
*/
|
|
110
145
|
transitions?: TransitionData[];
|
|
111
146
|
/**
|
|
112
|
-
*
|
|
147
|
+
* Relevant past observations
|
|
113
148
|
*/
|
|
114
149
|
observations?: AgentObservation<any>[];
|
|
150
|
+
/**
|
|
151
|
+
* Relevant feedback
|
|
152
|
+
*/
|
|
115
153
|
feedback?: AgentFeedback[];
|
|
116
|
-
messages?: AgentMessage[];
|
|
117
|
-
decisions?: AgentDecision<TAgent>[];
|
|
118
|
-
}) => string;
|
|
119
|
-
type AgentStrategy<TAgent extends AnyAgent> = (agent: TAgent, input: AgentDecideInput<EventFromAgent<TAgent>>) => Promise<AgentDecision<TAgent> | undefined>;
|
|
120
|
-
type AgentInteractInput<T extends AnyAgent> = Omit<AgentDecideOptions<T>, 'state'> & {
|
|
121
|
-
state?: never;
|
|
122
|
-
};
|
|
123
|
-
type AgentDecideOptions<TAgent extends AnyAgent> = {
|
|
124
|
-
goal: string;
|
|
125
|
-
state: ObservedState<TAgent>;
|
|
126
154
|
/**
|
|
127
|
-
*
|
|
155
|
+
* Relevant messages
|
|
128
156
|
*/
|
|
129
|
-
|
|
130
|
-
machine?: AnyStateMachine;
|
|
131
|
-
model?: LanguageModel;
|
|
132
|
-
execute?: (event: AnyEventObject) => Promise<void>;
|
|
133
|
-
strategy?: AgentStrategy<TAgent>;
|
|
134
|
-
events?: ZodEventMapping;
|
|
135
|
-
allowedEvents?: Array<EventFromAgent<TAgent>['type']>;
|
|
157
|
+
messages?: AgentMessage[];
|
|
136
158
|
/**
|
|
137
|
-
*
|
|
138
|
-
* Defaults to 2.
|
|
159
|
+
* Relevant past decisions
|
|
139
160
|
*/
|
|
140
|
-
|
|
141
|
-
} & Omit<Parameters<typeof generateText>[0], 'model' | 'tools' | 'prompt'>;
|
|
142
|
-
interface AgentFeedback {
|
|
143
|
-
observationId: string;
|
|
144
|
-
score: number;
|
|
145
|
-
comment: string | undefined;
|
|
161
|
+
decisions?: AgentDecision<TAgent>[];
|
|
146
162
|
/**
|
|
147
|
-
*
|
|
163
|
+
* Relevant past insights
|
|
148
164
|
*/
|
|
165
|
+
insights?: AgentInsight[];
|
|
166
|
+
}) => string;
|
|
167
|
+
type AgentPolicy<TAgent extends AnyAgent = AnyAgent> = (agent: TAgent, input: AgentDecideInput<TAgent>) => Promise<AgentDecision<TAgent> | undefined>;
|
|
168
|
+
type AgentInteractInput<T extends AnyAgent> = Omit<AgentDecideInput<T>, 'state'> & {
|
|
169
|
+
state?: never;
|
|
170
|
+
};
|
|
171
|
+
interface AgentFeedback extends BaseProperties {
|
|
172
|
+
decisionId: string;
|
|
173
|
+
reward: number;
|
|
174
|
+
comment: string | undefined;
|
|
149
175
|
attributes: Record<string, any>;
|
|
150
|
-
|
|
176
|
+
}
|
|
177
|
+
interface BaseProperties {
|
|
178
|
+
id: string;
|
|
151
179
|
episodeId: string;
|
|
180
|
+
timestamp: number;
|
|
152
181
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
182
|
+
type BaseInput = Partial<BaseProperties>;
|
|
183
|
+
interface AgentFeedbackInput extends BaseInput {
|
|
184
|
+
/**
|
|
185
|
+
* The decision ID that this feedback is relevant for.
|
|
186
|
+
*/
|
|
187
|
+
decisionId: string;
|
|
188
|
+
reward: number;
|
|
156
189
|
comment?: string;
|
|
157
190
|
attributes?: Record<string, any>;
|
|
158
|
-
timestamp?: number;
|
|
159
191
|
}
|
|
160
|
-
type AgentMessage = CoreMessage & {
|
|
161
|
-
|
|
162
|
-
|
|
192
|
+
type AgentMessage = BaseProperties & CoreMessage & {
|
|
193
|
+
/**
|
|
194
|
+
* The parent decision that this message is a part of.
|
|
195
|
+
*/
|
|
196
|
+
decisionId?: string;
|
|
163
197
|
/**
|
|
164
198
|
* The response ID of the message, which references
|
|
165
199
|
* which message this message is responding to, if any.
|
|
166
200
|
*/
|
|
167
201
|
responseId?: string;
|
|
168
|
-
result?: GenerateTextResult<any>;
|
|
169
|
-
episodeId: string;
|
|
202
|
+
result?: GenerateTextResult<any, any>;
|
|
170
203
|
};
|
|
171
204
|
type JSONObject = {
|
|
172
205
|
[key: string]: JSONValue;
|
|
@@ -216,33 +249,31 @@ type AgentMessageInput = CoreMessage & {
|
|
|
216
249
|
* which message this message is responding to, if any.
|
|
217
250
|
*/
|
|
218
251
|
responseId?: string;
|
|
219
|
-
result?: GenerateTextResult<any>;
|
|
252
|
+
result?: GenerateTextResult<any, any>;
|
|
220
253
|
};
|
|
221
254
|
interface AgentObservation<TActor extends ActorRefLike> {
|
|
222
255
|
id: string;
|
|
256
|
+
episodeId: string;
|
|
257
|
+
/**
|
|
258
|
+
* The decision that this observation is relevant for
|
|
259
|
+
*/
|
|
260
|
+
decisionId?: string | undefined;
|
|
223
261
|
goal?: string;
|
|
224
262
|
prevState: SnapshotFrom<TActor> | undefined;
|
|
225
263
|
event: EventFrom<TActor> | undefined;
|
|
226
264
|
state: SnapshotFrom<TActor>;
|
|
227
|
-
machineHash: string | undefined;
|
|
228
|
-
episodeId: string;
|
|
229
265
|
timestamp: number;
|
|
230
266
|
}
|
|
231
|
-
interface AgentObservationInput<TAgent extends AnyAgent> {
|
|
232
|
-
|
|
267
|
+
interface AgentObservationInput<TAgent extends AnyAgent> extends BaseInput {
|
|
268
|
+
state: ObservedState<TAgent>;
|
|
269
|
+
/**
|
|
270
|
+
* The agent decision that the observation is relevant for
|
|
271
|
+
*/
|
|
272
|
+
decisionId?: string | undefined;
|
|
233
273
|
prevState?: ObservedState<TAgent>;
|
|
234
274
|
event?: AnyEventObject;
|
|
235
|
-
|
|
236
|
-
machine?: AnyStateMachine;
|
|
237
|
-
timestamp?: number;
|
|
238
|
-
goal: string | undefined;
|
|
275
|
+
goal?: string | undefined;
|
|
239
276
|
}
|
|
240
|
-
type AgentDecisionInput = {
|
|
241
|
-
goal: string;
|
|
242
|
-
model?: LanguageModel;
|
|
243
|
-
context?: Record<string, any>;
|
|
244
|
-
} & Omit<Parameters<typeof generateText>[0], 'model' | 'tools' | 'prompt'>;
|
|
245
|
-
type AgentDecisionLogic<TAgent extends AnyAgent> = PromiseActorLogic<AgentDecision<TAgent> | undefined, AgentDecisionInput | string>;
|
|
246
277
|
type AgentEmitted<TAgent extends AnyAgent> = {
|
|
247
278
|
type: 'feedback';
|
|
248
279
|
feedback: AgentFeedback;
|
|
@@ -255,6 +286,9 @@ type AgentEmitted<TAgent extends AnyAgent> = {
|
|
|
255
286
|
} | {
|
|
256
287
|
type: 'decision';
|
|
257
288
|
decision: AgentDecision<TAgent>;
|
|
289
|
+
} | {
|
|
290
|
+
type: 'insight';
|
|
291
|
+
insight: AgentInsight;
|
|
258
292
|
};
|
|
259
293
|
type AgentLogic<TAgent extends AnyAgent> = ActorLogic<TransitionSnapshot<AgentMemoryContext<TAgent>>, {
|
|
260
294
|
type: 'agent.feedback';
|
|
@@ -268,6 +302,9 @@ type AgentLogic<TAgent extends AnyAgent> = ActorLogic<TransitionSnapshot<AgentMe
|
|
|
268
302
|
} | {
|
|
269
303
|
type: 'agent.decision';
|
|
270
304
|
decision: AgentDecision<TAgent>;
|
|
305
|
+
} | {
|
|
306
|
+
type: 'agent.insight';
|
|
307
|
+
insight: AgentInsight;
|
|
271
308
|
}, any, // TODO: input
|
|
272
309
|
any, AgentEmitted<TAgent>>;
|
|
273
310
|
type EventsFromZodEventMapping<TEventSchemas extends ZodEventMapping> = Compute<Values<{
|
|
@@ -278,17 +315,17 @@ type EventsFromZodEventMapping<TEventSchemas extends ZodEventMapping> = Compute<
|
|
|
278
315
|
type ContextFromZodContextMapping<TContextSchema extends ZodContextMapping> = {
|
|
279
316
|
[K in keyof TContextSchema & string]: TypeOf<TContextSchema[K]>;
|
|
280
317
|
};
|
|
281
|
-
type AnyAgent = Agent<any, any
|
|
318
|
+
type AnyAgent = Agent<any, any>;
|
|
282
319
|
type FromAgent<T> = T | ((agent: AnyAgent) => T | Promise<T>);
|
|
283
|
-
type CommonTextOptions
|
|
320
|
+
type CommonTextOptions = {
|
|
284
321
|
prompt: FromAgent<string>;
|
|
285
322
|
model?: LanguageModel;
|
|
286
|
-
messages?:
|
|
323
|
+
messages?: CoreMessage[];
|
|
287
324
|
template?: PromptTemplate<any>;
|
|
288
325
|
context?: Record<string, any>;
|
|
289
326
|
};
|
|
290
|
-
type AgentGenerateTextOptions
|
|
291
|
-
type AgentStreamTextOptions
|
|
327
|
+
type AgentGenerateTextOptions = Omit<GenerateTextOptions, 'model' | 'prompt' | 'messages'> & CommonTextOptions;
|
|
328
|
+
type AgentStreamTextOptions = Omit<StreamTextOptions, 'model' | 'prompt' | 'messages'> & CommonTextOptions;
|
|
292
329
|
interface ObservedState<TAgent extends AnyAgent> {
|
|
293
330
|
/**
|
|
294
331
|
* The current state value of the state machine, e.g.
|
|
@@ -302,10 +339,11 @@ interface ObservedState<TAgent extends AnyAgent> {
|
|
|
302
339
|
}
|
|
303
340
|
type ObservedStateFrom<TActor extends ActorRefLike> = Pick<SnapshotFrom<TActor>, 'value' | 'context'>;
|
|
304
341
|
type AgentMemoryContext<TAgent extends AnyAgent> = {
|
|
305
|
-
observations: AgentObservation<
|
|
342
|
+
observations: AgentObservation<any>[];
|
|
306
343
|
messages: AgentMessage[];
|
|
307
344
|
decisions: AgentDecision<TAgent>[];
|
|
308
345
|
feedback: AgentFeedback[];
|
|
346
|
+
insights: AgentInsight[];
|
|
309
347
|
};
|
|
310
348
|
interface AgentLongTermMemory<TAgent extends AnyAgent> {
|
|
311
349
|
get<K extends keyof AgentMemoryContext<TAgent>>(key: K): Promise<AgentMemoryContext<TAgent>[K]>;
|
|
@@ -316,18 +354,37 @@ type Compute<A extends any> = {
|
|
|
316
354
|
[K in keyof A]: A[K];
|
|
317
355
|
} & unknown;
|
|
318
356
|
type MaybePromise<T> = T | Promise<T>;
|
|
319
|
-
type EventFromAgent<T extends AnyAgent> = T extends Agent<infer _, infer
|
|
357
|
+
type EventFromAgent<T extends AnyAgent> = T extends Agent<infer _, infer TEventSchemas> ? EventsFromZodEventMapping<TEventSchemas> : never;
|
|
320
358
|
type TypesFromAgent<T extends AnyAgent> = T extends Agent<infer TContextSchema, infer TEventSchema> ? {
|
|
321
359
|
context: ContextFromZodContextMapping<TContextSchema>;
|
|
322
360
|
events: EventsFromZodEventMapping<TEventSchema>;
|
|
323
361
|
} : never;
|
|
324
362
|
type ContextFromAgent<T extends AnyAgent> = T extends Agent<infer TContextSchema, infer _TEventSchema> ? ContextFromZodContextMapping<TContextSchema> : never;
|
|
363
|
+
interface StorageAdapter<TAgent extends AnyAgent, TQuery> {
|
|
364
|
+
addObservation(observationInput: AgentObservationInput<TAgent>): Promise<AgentObservation<any>>;
|
|
365
|
+
getObservations(queryObject?: TQuery): Promise<AgentObservation<any>[]>;
|
|
366
|
+
addFeedback(feedbackInput: AgentFeedbackInput): Promise<AgentFeedback>;
|
|
367
|
+
getFeedback(queryObject?: TQuery): Promise<AgentFeedback[]>;
|
|
368
|
+
addMessage(messageInput: AgentMessageInput): Promise<AgentMessage>;
|
|
369
|
+
getMessages(queryObject?: TQuery): Promise<AgentMessage[]>;
|
|
370
|
+
addDecision(decisionInput: AgentDecideInput<TAgent>): Promise<AgentDecision<TAgent>>;
|
|
371
|
+
getDecisions(queryObject?: TQuery): Promise<AgentDecision<TAgent>[]>;
|
|
372
|
+
}
|
|
373
|
+
type StorageAdapterQuery<T extends StorageAdapter<any, any>> = T extends StorageAdapter<infer _, infer TQuery> ? TQuery : never;
|
|
374
|
+
interface AgentInsightInput extends BaseInput {
|
|
375
|
+
observationId: string;
|
|
376
|
+
attributes: Record<string, any>;
|
|
377
|
+
}
|
|
378
|
+
interface AgentInsight extends BaseProperties {
|
|
379
|
+
observationId: string;
|
|
380
|
+
attributes: Record<string, any>;
|
|
381
|
+
}
|
|
325
382
|
|
|
326
|
-
declare function createAgent<const TContextSchema extends ZodContextMapping, const TEventSchemas extends ZodEventMapping,
|
|
383
|
+
declare function createAgent<const TContextSchema extends ZodContextMapping, const TEventSchemas extends ZodEventMapping, TAgent extends AnyAgent = Agent<TContextSchema, TEventSchemas>>({ id, description, model, events, context, episodeId, policy, logic, }: {
|
|
327
384
|
/**
|
|
328
385
|
* The unique identifier for the agent.
|
|
329
386
|
*
|
|
330
|
-
* This should be the same across all
|
|
387
|
+
* This should be the same across all episodes of a specific agent, as it can be
|
|
331
388
|
* used to retrieve memory for previous episodes of this agent.
|
|
332
389
|
*
|
|
333
390
|
* @example
|
|
@@ -340,29 +397,44 @@ declare function createAgent<const TContextSchema extends ZodContextMapping, con
|
|
|
340
397
|
*/
|
|
341
398
|
id?: string;
|
|
342
399
|
/**
|
|
343
|
-
* A description of the role of the agent
|
|
400
|
+
* A description of the role of the agent.
|
|
344
401
|
*/
|
|
345
402
|
description?: string;
|
|
346
403
|
/**
|
|
347
|
-
*
|
|
348
|
-
* that the agent knows about.
|
|
404
|
+
* Event schemas for events that the agent can trigger in an environment.
|
|
349
405
|
*/
|
|
350
406
|
events: TEventSchemas;
|
|
407
|
+
/**
|
|
408
|
+
* The state context schema for the states that the agent can observe.
|
|
409
|
+
*/
|
|
351
410
|
context?: TContextSchema;
|
|
352
|
-
|
|
353
|
-
|
|
411
|
+
/**
|
|
412
|
+
* The default policy to use for `agent.decide(…)`.
|
|
413
|
+
*
|
|
414
|
+
* A policy is a strategy that the agent uses to decide which event to trigger next.
|
|
415
|
+
*/
|
|
416
|
+
policy?: AgentPolicy<Agent<TContextSchema, TEventSchemas>>;
|
|
354
417
|
/**
|
|
355
418
|
* A function that retrieves the agent's long term memory
|
|
356
419
|
*/
|
|
357
420
|
getMemory?: (agent: Agent<TContextSchema, TEventSchemas>) => AgentLongTermMemory<TAgent>;
|
|
358
421
|
/**
|
|
359
|
-
*
|
|
422
|
+
* Custom agent logic, which receives events for handling feedback,
|
|
423
|
+
* observations, messages, decisions, and insights.
|
|
360
424
|
*/
|
|
361
425
|
logic?: AgentLogic<TAgent>;
|
|
426
|
+
/**
|
|
427
|
+
* The default language model for the agent to use in `agent.decide(…)`.
|
|
428
|
+
*/
|
|
362
429
|
model: LanguageModel;
|
|
430
|
+
/**
|
|
431
|
+
* The unique episode ID that this agent will run on.
|
|
432
|
+
*
|
|
433
|
+
* An episode is an instance of an agent interacting with an environment.
|
|
434
|
+
*/
|
|
363
435
|
episodeId?: string;
|
|
364
436
|
}): Agent<TContextSchema, TEventSchemas>;
|
|
365
|
-
declare class Agent<const TContextSchema extends ZodContextMapping, const TEventSchemas extends ZodEventMapping
|
|
437
|
+
declare class Agent<const TContextSchema extends ZodContextMapping, const TEventSchemas extends ZodEventMapping> extends Actor<AgentLogic<any>> {
|
|
366
438
|
/**
|
|
367
439
|
* The name of the agent. All agents with the same name are related and
|
|
368
440
|
* able to share experiences (observations, feedback) with each other.
|
|
@@ -375,11 +447,10 @@ declare class Agent<const TContextSchema extends ZodContextMapping, const TEvent
|
|
|
375
447
|
description?: string;
|
|
376
448
|
events: TEventSchemas;
|
|
377
449
|
context?: TContextSchema;
|
|
378
|
-
|
|
450
|
+
policy: AgentPolicy<Agent<TContextSchema, TEventSchemas>>;
|
|
379
451
|
model: LanguageModel;
|
|
380
452
|
memory: AgentLongTermMemory<this> | undefined;
|
|
381
|
-
|
|
382
|
-
constructor({ logic, id, name, description, model, events, context, episodeId, strategy, }: {
|
|
453
|
+
constructor({ logic, id, name, description, model, events, context, episodeId, policy, }: {
|
|
383
454
|
logic: AgentLogic<any>;
|
|
384
455
|
id?: string;
|
|
385
456
|
name?: string;
|
|
@@ -387,67 +458,40 @@ declare class Agent<const TContextSchema extends ZodContextMapping, const TEvent
|
|
|
387
458
|
model: GenerateTextOptions['model'];
|
|
388
459
|
events: TEventSchemas;
|
|
389
460
|
context?: TContextSchema;
|
|
390
|
-
|
|
461
|
+
policy?: AgentPolicy<Agent<TContextSchema, TEventSchemas>>;
|
|
391
462
|
episodeId?: string;
|
|
392
463
|
});
|
|
393
464
|
/**
|
|
394
|
-
* Called whenever the agent
|
|
465
|
+
* Called whenever the agent detects that a message was sent from the human, assistant, or system.
|
|
395
466
|
*/
|
|
396
467
|
onMessage(fn: (message: AgentMessage) => void): Subscription;
|
|
397
468
|
/**
|
|
398
|
-
* Called whenever the agent
|
|
469
|
+
* Called whenever the agent receives some feedback.
|
|
399
470
|
*/
|
|
400
471
|
onFeedback(fn: (feedback: AgentFeedback) => void): Subscription;
|
|
401
472
|
/**
|
|
402
|
-
*
|
|
473
|
+
* Called whenever the agent receives an observation.
|
|
403
474
|
*/
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
} | {
|
|
414
|
-
id: string;
|
|
415
|
-
timestamp: number;
|
|
416
|
-
episodeId: string;
|
|
417
|
-
role: "tool";
|
|
418
|
-
content: ai.ToolContent;
|
|
419
|
-
experimental_providerMetadata?: ai.ProviderMetadata;
|
|
420
|
-
responseId?: string;
|
|
421
|
-
result?: ai.GenerateTextResult<any>;
|
|
422
|
-
} | {
|
|
423
|
-
id: string;
|
|
424
|
-
timestamp: number;
|
|
425
|
-
episodeId: string;
|
|
426
|
-
role: "system";
|
|
427
|
-
content: string;
|
|
428
|
-
experimental_providerMetadata?: ai.ProviderMetadata;
|
|
429
|
-
responseId?: string;
|
|
430
|
-
result?: ai.GenerateTextResult<any>;
|
|
431
|
-
} | {
|
|
432
|
-
id: string;
|
|
433
|
-
timestamp: number;
|
|
434
|
-
episodeId: string;
|
|
435
|
-
role: "user";
|
|
436
|
-
content: ai.UserContent;
|
|
437
|
-
experimental_providerMetadata?: ai.ProviderMetadata;
|
|
438
|
-
responseId?: string;
|
|
439
|
-
result?: ai.GenerateTextResult<any>;
|
|
440
|
-
};
|
|
475
|
+
onObservation(fn: (observation: AgentObservation<this>) => void): Subscription;
|
|
476
|
+
/**
|
|
477
|
+
* Called whenever the agent makes a decision.
|
|
478
|
+
*/
|
|
479
|
+
onDecision(fn: (decision: AgentDecision<this>) => void): Subscription;
|
|
480
|
+
/**
|
|
481
|
+
* Adds a message to the agent's short-term (local) memory.
|
|
482
|
+
*/
|
|
483
|
+
addMessage(messageInput: AgentMessageInput): AgentMessage;
|
|
441
484
|
getMessages(): AgentMessage[];
|
|
442
485
|
addFeedback(feedbackInput: AgentFeedbackInput): {
|
|
486
|
+
id: string;
|
|
443
487
|
comment: string | undefined;
|
|
444
488
|
attributes: {
|
|
445
489
|
[x: string]: any;
|
|
446
490
|
};
|
|
447
491
|
timestamp: number;
|
|
448
492
|
episodeId: string;
|
|
449
|
-
|
|
450
|
-
|
|
493
|
+
decisionId: string;
|
|
494
|
+
reward: number;
|
|
451
495
|
};
|
|
452
496
|
/**
|
|
453
497
|
* Retrieves feedback from the agent's short-term (local) memory.
|
|
@@ -458,7 +502,9 @@ declare class Agent<const TContextSchema extends ZodContextMapping, const TEvent
|
|
|
458
502
|
* Retrieves observations from the agent's short-term (local) memory.
|
|
459
503
|
*/
|
|
460
504
|
getObservations(): AgentObservation<any>[];
|
|
461
|
-
|
|
505
|
+
addInsight(insightInput: AgentInsightInput): AgentInsight;
|
|
506
|
+
getInsights(): AgentInsight[];
|
|
507
|
+
addDecision(input: AgentDecisionInput<this>): void;
|
|
462
508
|
/**
|
|
463
509
|
* Retrieves strategies from the agent's short-term (local) memory.
|
|
464
510
|
*/
|
|
@@ -518,18 +564,30 @@ declare class Agent<const TContextSchema extends ZodContextMapping, const TEvent
|
|
|
518
564
|
* - The `machine` (e.g. a state machine) that specifies what can happen next
|
|
519
565
|
* - Additional `context`
|
|
520
566
|
*/
|
|
521
|
-
decide(
|
|
567
|
+
decide(input: AgentDecideInput<this>): Promise<AgentDecision<this> | undefined>;
|
|
522
568
|
}
|
|
523
569
|
|
|
524
|
-
declare function fromTextStream<TAgent extends AnyAgent>(agent: TAgent, options?: AgentStreamTextOptions
|
|
570
|
+
declare function fromTextStream<TAgent extends AnyAgent>(agent: TAgent, options?: AgentStreamTextOptions): ObservableActorLogic<{
|
|
525
571
|
textDelta: string;
|
|
526
|
-
}, Omit<AgentStreamTextOptions
|
|
572
|
+
}, Omit<AgentStreamTextOptions, 'context'> & {
|
|
527
573
|
context?: Record<string, any>;
|
|
528
574
|
}>;
|
|
529
|
-
declare function fromText<TAgent extends AnyAgent>(agent: TAgent, options?: AgentGenerateTextOptions
|
|
575
|
+
declare function fromText<TAgent extends AnyAgent>(agent: TAgent, options?: AgentGenerateTextOptions): PromiseActorLogic<GenerateTextResult<Record<string, CoreTool<any, any>>, any>, Omit<AgentGenerateTextOptions, 'context'> & {
|
|
530
576
|
context?: Record<string, any>;
|
|
531
577
|
}>;
|
|
532
578
|
|
|
533
|
-
|
|
579
|
+
type AgentDecideLogicInput = {
|
|
580
|
+
goal: string;
|
|
581
|
+
model?: LanguageModel;
|
|
582
|
+
context?: Record<string, any>;
|
|
583
|
+
} & Omit<Parameters<typeof generateText>[0], 'model' | 'tools' | 'prompt'>;
|
|
584
|
+
type MachineDecisionLogic<TAgent extends AnyAgent> = PromiseActorLogic<AgentDecision<TAgent> | undefined, AgentDecideLogicInput | string>;
|
|
585
|
+
declare function fromDecision<TAgent extends AnyAgent>(agent: TAgent, defaultInput?: AgentDecideInput<TAgent>): MachineDecisionLogic<any>;
|
|
586
|
+
|
|
587
|
+
declare function chainOfThoughtPolicy<T extends AnyAgent>(agent: T, input: AgentDecideInput<any>): Promise<AgentDecision<any> | undefined>;
|
|
588
|
+
|
|
589
|
+
declare function experimental_shortestPathPolicy<T extends AnyAgent>(agent: T, input: AgentDecideInput<any>): Promise<AgentDecision<any> | undefined>;
|
|
590
|
+
|
|
591
|
+
declare function toolPolicy<TAgent extends AnyAgent>(agent: TAgent, input: AgentDecideInput<TAgent>): Promise<AgentDecision<TAgent> | undefined>;
|
|
534
592
|
|
|
535
|
-
export { type AgentDecideInput, type
|
|
593
|
+
export { type AgentDecideInput, type AgentDecision, type AgentDecisionInput, type AgentEmitted, type AgentFeedback, type AgentFeedbackInput, type AgentGenerateTextOptions, type AgentInsight, type AgentInsightInput, type AgentInteractInput, type AgentLogic, type AgentLongTermMemory, type AgentMemoryContext, type AgentMessage, type AgentMessageInput, type AgentObservation, type AgentObservationInput, type AgentPath, type AgentPolicy, type AgentStep, type AgentStreamTextOptions, type AnyAgent, type CommonTextOptions, type Compute, type ContextFromAgent, type ContextFromZodContextMapping, type CostFunction, type EventFromAgent, type EventsFromZodEventMapping, type FromAgent, type GenerateTextOptions, type LanguageModelV1TextPart, type LanguageModelV1ToolCallPart, type MaybePromise, type ObservedState, type ObservedStateFrom, type PromptTemplate, type StorageAdapter, type StorageAdapterQuery, type StreamTextOptions, type TransitionData, type TypesFromAgent, chainOfThoughtPolicy, createAgent, experimental_shortestPathPolicy, fromDecision, fromText, fromTextStream, toolPolicy };
|