@statelyai/agent 1.1.6 → 2.0.0-next.1
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/cyan-carpets-perform.md +5 -0
- package/.changeset/fast-donkeys-argue.md +5 -0
- package/.changeset/light-hats-drive.md +9 -0
- package/.changeset/old-jobs-check.md +5 -0
- package/.changeset/pre.json +13 -0
- package/.vscode/launch.json +6 -0
- package/CHANGELOG.md +22 -0
- package/dist/index.d.mts +262 -171
- package/dist/index.d.ts +262 -171
- package/dist/index.js +383 -274
- package/dist/index.mjs +386 -272
- package/examples/chatbot-alt.ts +57 -0
- package/examples/chatbot.ts +12 -17
- package/examples/cot.ts +26 -23
- package/examples/customer-service-sim.ts +107 -0
- package/examples/email.ts +37 -41
- package/examples/example.ts +6 -6
- package/examples/executor.ts +66 -0
- package/examples/goal.ts +12 -12
- package/examples/helpers/helpers.ts +26 -14
- package/examples/joke.ts +79 -76
- package/examples/jugs.ts +125 -0
- package/examples/multi.ts +5 -5
- package/examples/newspaper.ts +98 -104
- package/examples/number.ts +6 -5
- package/examples/raffle.ts +11 -12
- package/examples/river-crossing.ts +140 -0
- package/examples/sandbox.ts +1 -1
- package/examples/simple.ts +5 -3
- package/examples/summary.ts +121 -0
- package/examples/support.ts +6 -6
- package/examples/ticTacToe.ts +86 -45
- package/examples/todo.ts +7 -7
- package/examples/tutor.ts +15 -15
- package/examples/verify.ts +3 -3
- package/examples/weather.ts +6 -9
- package/examples/wiki.ts +27 -8
- package/examples/word.ts +16 -11
- package/package.json +16 -11
- package/readme.md +1 -1
- package/src/agent-experimental.ts +1 -1
- package/src/agent.test.ts +243 -214
- package/src/agent.ts +286 -95
- package/src/decide.test.ts +276 -0
- package/src/decide.ts +163 -0
- package/src/index.ts +1 -1
- package/src/middleware.ts +91 -0
- package/src/mockModel.ts +47 -0
- package/src/planners/shortestPath.test.ts +94 -0
- package/src/planners/shortestPath.ts +177 -0
- package/src/planners/simple.ts +105 -0
- package/src/strategies/chain-of-note.ts +6 -55
- package/src/text.ts +51 -144
- package/src/types.ts +187 -212
- package/src/utils.ts +48 -4
- package/vitest.config.ts +9 -3
- package/src/adapters/vercel.ts +0 -7
- package/src/decision.test.ts +0 -179
- package/src/decision.ts +0 -84
- package/src/memory.ts +0 -25
- package/src/planners/shortestPathPlanner.ts +0 -22
- package/src/planners/simplePlanner.ts +0 -139
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
---
|
|
2
|
+
'@statelyai/agent': major
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
- `agent.generateText(…)` is removed in favor of using the AI SDK's `generateText(…)` function with a wrapped model.
|
|
6
|
+
- `agent.streamText(…)` is removed in favor of using the AI SDK's `streamText(…)` function with a wrapped model.
|
|
7
|
+
- Custom adapters are removed for now, but may be re-added in future releases. Using the AI SDK is recommended for now.
|
|
8
|
+
- Correlation IDs are removed in favor of using [OpenTelemetry with the AI SDK](https://sdk.vercel.ai/docs/ai-sdk-core/telemetry#telemetry).
|
|
9
|
+
- The `createAgentMiddleware(…)` function was introduced to facilitate agent message history. You can also use `agent.wrap(model)` to wrap a model with Stately Agent middleware.
|
package/.vscode/launch.json
CHANGED
|
@@ -20,8 +20,14 @@
|
|
|
20
20
|
"program": "${file}",
|
|
21
21
|
"cwd": "${workspaceFolder}",
|
|
22
22
|
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/ts-node",
|
|
23
|
+
"runtimeArgs": ["--transpile-only", "-r", "dotenv/config"],
|
|
23
24
|
"outFiles": ["${workspaceFolder}/dist/**/*.js"],
|
|
24
25
|
"sourceMaps": true,
|
|
26
|
+
"smartStep": true,
|
|
27
|
+
"resolveSourceMapLocations": [
|
|
28
|
+
"${workspaceFolder}/**",
|
|
29
|
+
"!**/node_modules/**"
|
|
30
|
+
],
|
|
25
31
|
"console": "integratedTerminal"
|
|
26
32
|
}
|
|
27
33
|
]
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
# @statelyai/agent
|
|
2
2
|
|
|
3
|
+
## 2.0.0-next.1
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [`6a9861d`](https://github.com/statelyai/agent/commit/6a9861d959ce295114f53c95c5bdaa097348bacb) Thanks [@davidkpiano](https://github.com/davidkpiano)! - You can specify `maxAttempts` in `agent.decide({ maxAttempts: 5 })`. This will allow the agent to attempt to make a decision up to the specified number of `maxAttempts` before giving up. The default value is `2`.
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- [`8c3eab8`](https://github.com/statelyai/agent/commit/8c3eab8950cb85e662c6afb5d8cefb1d5ef54dd8) Thanks [@davidkpiano](https://github.com/davidkpiano)! - The `name` field in `createAgent({ name: '...' })` has been renamed to `id`.
|
|
12
|
+
|
|
13
|
+
- [`8c3eab8`](https://github.com/statelyai/agent/commit/8c3eab8950cb85e662c6afb5d8cefb1d5ef54dd8) Thanks [@davidkpiano](https://github.com/davidkpiano)! - The `description` field in `createAgent({ description: '...' })` is now used for the `system` prompt in agent decision making when a `system` prompt is not provided.
|
|
14
|
+
|
|
15
|
+
## 2.0.0-next.0
|
|
16
|
+
|
|
17
|
+
### Major Changes
|
|
18
|
+
|
|
19
|
+
- [#51](https://github.com/statelyai/agent/pull/51) [`574b6fd`](https://github.com/statelyai/agent/commit/574b6fd62e8a41df311aa1ea00fae60c32ad595e) Thanks [@davidkpiano](https://github.com/davidkpiano)! - - `agent.generateText(…)` is removed in favor of using the AI SDK's `generateText(…)` function with a wrapped model.
|
|
20
|
+
- `agent.streamText(…)` is removed in favor of using the AI SDK's `streamText(…)` function with a wrapped model.
|
|
21
|
+
- Custom adapters are removed for now, but may be re-added in future releases. Using the AI SDK is recommended for now.
|
|
22
|
+
- Correlation IDs are removed in favor of using [OpenTelemetry with the AI SDK](https://sdk.vercel.ai/docs/ai-sdk-core/telemetry#telemetry).
|
|
23
|
+
- The `createAgentMiddleware(…)` function was introduced to facilitate agent message history. You can also use `agent.wrap(model)` to wrap a model with Stately Agent middleware.
|
|
24
|
+
|
|
3
25
|
## 1.1.6
|
|
4
26
|
|
|
5
27
|
### Patch Changes
|
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as ai from 'ai';
|
|
2
|
+
import { generateText, streamText, LanguageModel, CoreMessage, GenerateTextResult, LanguageModelV1, CoreTool } from 'ai';
|
|
3
|
+
import { EventObject, AnyStateMachine, AnyEventObject, ActorRefLike, SnapshotFrom, EventFrom, PromiseActorLogic, ActorLogic, TransitionSnapshot, Values, StateValue, Actor, Subscription, ObservableActorLogic } from 'xstate';
|
|
2
4
|
import { SomeZodObject, ZodType, TypeOf } from 'zod';
|
|
3
|
-
import { generateText, streamText, LanguageModel, CoreMessage, GenerateTextResult, StreamTextResult, CoreTool } from 'ai';
|
|
4
5
|
|
|
5
6
|
type ZodEventMapping = {
|
|
6
7
|
[eventType: string]: SomeZodObject;
|
|
@@ -11,7 +12,8 @@ type ZodContextMapping = {
|
|
|
11
12
|
|
|
12
13
|
type GenerateTextOptions = Parameters<typeof generateText>[0];
|
|
13
14
|
type StreamTextOptions = Parameters<typeof streamText>[0];
|
|
14
|
-
type
|
|
15
|
+
type CostFunction<TEvent extends EventObject> = (path: AgentPath<TEvent>) => number;
|
|
16
|
+
type AgentPlanInput<TEvent extends EventObject> = Omit<AgentGenerateTextOptions, 'prompt' | 'tools'> & {
|
|
15
17
|
/**
|
|
16
18
|
* The currently observed state.
|
|
17
19
|
*/
|
|
@@ -35,18 +37,50 @@ type AgentPlanInput<TEvent extends EventObject> = Omit<GenerateTextOptions, 'pro
|
|
|
35
37
|
* The previous plan.
|
|
36
38
|
*/
|
|
37
39
|
previousPlan?: AgentPlan<TEvent>;
|
|
40
|
+
/**
|
|
41
|
+
* The total cost of the path to the goal state.
|
|
42
|
+
*/
|
|
43
|
+
costFunction?: CostFunction<TEvent>;
|
|
44
|
+
/**
|
|
45
|
+
* The maximum number of attempts to generate a plan.
|
|
46
|
+
* Defaults to 2.
|
|
47
|
+
*/
|
|
48
|
+
maxAttempts?: number;
|
|
49
|
+
};
|
|
50
|
+
type AgentStep<TEvent extends EventObject> = {
|
|
51
|
+
/** The event to take */
|
|
52
|
+
event: TEvent;
|
|
53
|
+
/** The next expected state after taking the event */
|
|
54
|
+
state: ObservedState | undefined;
|
|
55
|
+
};
|
|
56
|
+
type AgentPath<TEvent extends EventObject> = {
|
|
57
|
+
/** The expected ending state of the path */
|
|
58
|
+
state: ObservedState | undefined;
|
|
59
|
+
/** The steps to reach the ending state */
|
|
60
|
+
steps: Array<AgentStep<TEvent>>;
|
|
61
|
+
weight?: number;
|
|
38
62
|
};
|
|
39
63
|
type AgentPlan<TEvent extends EventObject> = {
|
|
64
|
+
/**
|
|
65
|
+
* The planner used to generate the plan
|
|
66
|
+
*/
|
|
67
|
+
planner: string;
|
|
40
68
|
goal: string;
|
|
41
|
-
state: ObservedState;
|
|
42
|
-
content?: string;
|
|
43
69
|
/**
|
|
44
|
-
*
|
|
45
|
-
|
|
70
|
+
* The ending state of the plan.
|
|
71
|
+
*/
|
|
72
|
+
goalState: ObservedState | undefined;
|
|
73
|
+
/**
|
|
74
|
+
* The next event that the agent decided needs to occur to achieve the `goal`.
|
|
75
|
+
*
|
|
76
|
+
* This next event is chosen from the
|
|
46
77
|
*/
|
|
47
|
-
execute: (state: ObservedState) => Promise<TEvent | undefined>;
|
|
48
78
|
nextEvent: TEvent | undefined;
|
|
49
|
-
|
|
79
|
+
/**
|
|
80
|
+
* The paths that the agent can take to achieve the goal.
|
|
81
|
+
*/
|
|
82
|
+
paths: AgentPath<TEvent>[];
|
|
83
|
+
episodeId: string;
|
|
50
84
|
timestamp: number;
|
|
51
85
|
};
|
|
52
86
|
interface TransitionData {
|
|
@@ -86,32 +120,34 @@ type PromptTemplate<TEvents extends EventObject> = (data: {
|
|
|
86
120
|
plans?: AgentPlan<TEvents>[];
|
|
87
121
|
}) => string;
|
|
88
122
|
type AgentPlanner<T extends AnyAgent> = (agent: T, input: AgentPlanInput<T['types']['events']>) => Promise<AgentPlan<T['types']['events']> | undefined>;
|
|
89
|
-
type AgentDecideOptions = {
|
|
123
|
+
type AgentDecideOptions<T extends AnyAgent> = {
|
|
90
124
|
goal: string;
|
|
91
125
|
model?: LanguageModel;
|
|
92
|
-
context?: any;
|
|
93
126
|
state: ObservedState;
|
|
94
|
-
machine
|
|
127
|
+
machine?: AnyStateMachine;
|
|
95
128
|
execute?: (event: AnyEventObject) => Promise<void>;
|
|
96
|
-
planner?: AgentPlanner<
|
|
129
|
+
planner?: AgentPlanner<T>;
|
|
97
130
|
events?: ZodEventMapping;
|
|
98
|
-
|
|
131
|
+
/**
|
|
132
|
+
* The maximum number of times the agent will attempt to make a decision.
|
|
133
|
+
* Defaults to 2.
|
|
134
|
+
*/
|
|
135
|
+
maxAttempts?: number;
|
|
136
|
+
} & Omit<Parameters<typeof generateText>[0], 'model' | 'tools' | 'prompt'>;
|
|
99
137
|
interface AgentFeedback {
|
|
100
138
|
goal?: string;
|
|
101
139
|
observationId?: string;
|
|
102
140
|
/**
|
|
103
141
|
* The message correlation that the feedback is relevant for
|
|
104
142
|
*/
|
|
105
|
-
correlationId?: string;
|
|
106
143
|
attributes: Record<string, any>;
|
|
107
144
|
reward: number;
|
|
108
145
|
timestamp: number;
|
|
109
|
-
|
|
146
|
+
episodeId: string;
|
|
110
147
|
}
|
|
111
148
|
interface AgentFeedbackInput {
|
|
112
149
|
goal?: string;
|
|
113
150
|
observationId?: string;
|
|
114
|
-
correlationId?: string;
|
|
115
151
|
attributes?: Record<string, any>;
|
|
116
152
|
timestamp?: number;
|
|
117
153
|
reward?: number;
|
|
@@ -125,10 +161,48 @@ type AgentMessage = CoreMessage & {
|
|
|
125
161
|
*/
|
|
126
162
|
responseId?: string;
|
|
127
163
|
result?: GenerateTextResult<any>;
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
164
|
+
episodeId: string;
|
|
165
|
+
};
|
|
166
|
+
type JSONObject = {
|
|
167
|
+
[key: string]: JSONValue;
|
|
131
168
|
};
|
|
169
|
+
type JSONArray = JSONValue[];
|
|
170
|
+
type JSONValue = null | string | number | boolean | JSONObject | JSONArray;
|
|
171
|
+
type LanguageModelV1ProviderMetadata = Record<string, Record<string, JSONValue>>;
|
|
172
|
+
interface LanguageModelV1TextPart {
|
|
173
|
+
type: 'text';
|
|
174
|
+
/**
|
|
175
|
+
The text content.
|
|
176
|
+
*/
|
|
177
|
+
text: string;
|
|
178
|
+
/**
|
|
179
|
+
* Additional provider-specific metadata. They are passed through
|
|
180
|
+
* to the provider from the AI SDK and enable provider-specific
|
|
181
|
+
* functionality that can be fully encapsulated in the provider.
|
|
182
|
+
*/
|
|
183
|
+
providerMetadata?: LanguageModelV1ProviderMetadata;
|
|
184
|
+
}
|
|
185
|
+
interface LanguageModelV1ToolCallPart {
|
|
186
|
+
type: 'tool-call';
|
|
187
|
+
/**
|
|
188
|
+
ID of the tool call. This ID is used to match the tool call with the tool result.
|
|
189
|
+
*/
|
|
190
|
+
toolCallId: string;
|
|
191
|
+
/**
|
|
192
|
+
Name of the tool that is being called.
|
|
193
|
+
*/
|
|
194
|
+
toolName: string;
|
|
195
|
+
/**
|
|
196
|
+
Arguments of the tool call. This is a JSON-serializable object that matches the tool's input schema.
|
|
197
|
+
*/
|
|
198
|
+
args: unknown;
|
|
199
|
+
/**
|
|
200
|
+
* Additional provider-specific metadata. They are passed through
|
|
201
|
+
* to the provider from the AI SDK and enable provider-specific
|
|
202
|
+
* functionality that can be fully encapsulated in the provider.
|
|
203
|
+
*/
|
|
204
|
+
providerMetadata?: LanguageModelV1ProviderMetadata;
|
|
205
|
+
}
|
|
132
206
|
type AgentMessageInput = CoreMessage & {
|
|
133
207
|
timestamp?: number;
|
|
134
208
|
id?: string;
|
|
@@ -137,23 +211,21 @@ type AgentMessageInput = CoreMessage & {
|
|
|
137
211
|
* which message this message is responding to, if any.
|
|
138
212
|
*/
|
|
139
213
|
responseId?: string;
|
|
140
|
-
correlationId?: string;
|
|
141
|
-
parentCorrelationId?: string;
|
|
142
214
|
result?: GenerateTextResult<any>;
|
|
143
215
|
};
|
|
144
|
-
interface AgentObservation<TActor extends
|
|
216
|
+
interface AgentObservation<TActor extends ActorRefLike> {
|
|
145
217
|
id: string;
|
|
146
218
|
prevState: SnapshotFrom<TActor> | undefined;
|
|
147
|
-
event: EventFrom<TActor
|
|
219
|
+
event: EventFrom<TActor> | undefined;
|
|
148
220
|
state: SnapshotFrom<TActor>;
|
|
149
221
|
machineHash: string | undefined;
|
|
150
|
-
|
|
222
|
+
episodeId: string;
|
|
151
223
|
timestamp: number;
|
|
152
224
|
}
|
|
153
225
|
interface AgentObservationInput {
|
|
154
226
|
id?: string;
|
|
155
|
-
prevState
|
|
156
|
-
event
|
|
227
|
+
prevState?: ObservedState;
|
|
228
|
+
event?: AnyEventObject;
|
|
157
229
|
state: ObservedState;
|
|
158
230
|
machine?: AnyStateMachine;
|
|
159
231
|
timestamp?: number;
|
|
@@ -161,7 +233,7 @@ interface AgentObservationInput {
|
|
|
161
233
|
type AgentDecisionInput = {
|
|
162
234
|
goal: string;
|
|
163
235
|
model?: LanguageModel;
|
|
164
|
-
context?: any
|
|
236
|
+
context?: Record<string, any>;
|
|
165
237
|
} & Omit<Parameters<typeof generateText>[0], 'model' | 'tools' | 'prompt'>;
|
|
166
238
|
type AgentDecisionLogic<TEvents extends EventObject> = PromiseActorLogic<AgentPlan<TEvents> | undefined, AgentDecisionInput | string>;
|
|
167
239
|
type AgentEmitted<TEvents extends EventObject> = {
|
|
@@ -199,7 +271,83 @@ type EventsFromZodEventMapping<TEventSchemas extends ZodEventMapping> = Values<{
|
|
|
199
271
|
type ContextFromZodContextMapping<TContextSchema extends ZodContextMapping> = {
|
|
200
272
|
[K in keyof TContextSchema & string]: TypeOf<TContextSchema[K]>;
|
|
201
273
|
};
|
|
202
|
-
type Agent<
|
|
274
|
+
type AnyAgent = Agent<any, any, any, any>;
|
|
275
|
+
type FromAgent<T> = T | ((agent: AnyAgent) => T | Promise<T>);
|
|
276
|
+
type CommonTextOptions = {
|
|
277
|
+
prompt: FromAgent<string>;
|
|
278
|
+
model?: LanguageModel;
|
|
279
|
+
context?: Record<string, any>;
|
|
280
|
+
messages?: FromAgent<CoreMessage[]>;
|
|
281
|
+
template?: PromptTemplate<any>;
|
|
282
|
+
};
|
|
283
|
+
type AgentGenerateTextOptions = Omit<GenerateTextOptions, 'model' | 'prompt' | 'messages'> & CommonTextOptions;
|
|
284
|
+
type AgentStreamTextOptions = Omit<StreamTextOptions, 'model' | 'prompt' | 'messages'> & CommonTextOptions;
|
|
285
|
+
interface ObservedState {
|
|
286
|
+
/**
|
|
287
|
+
* The current state value of the state machine, e.g.
|
|
288
|
+
* `"loading"` or `"processing"` or `"ready"`
|
|
289
|
+
*/
|
|
290
|
+
value: StateValue;
|
|
291
|
+
/**
|
|
292
|
+
* Additional contextual data related to the current state
|
|
293
|
+
*/
|
|
294
|
+
context?: Record<string, unknown>;
|
|
295
|
+
}
|
|
296
|
+
type ObservedStateFrom<TActor extends ActorRefLike> = Pick<SnapshotFrom<TActor>, 'value' | 'context'>;
|
|
297
|
+
type AgentMemoryContext = {
|
|
298
|
+
observations: AgentObservation<any>[];
|
|
299
|
+
messages: AgentMessage[];
|
|
300
|
+
plans: AgentPlan<any>[];
|
|
301
|
+
feedback: AgentFeedback[];
|
|
302
|
+
};
|
|
303
|
+
interface AgentLongTermMemory {
|
|
304
|
+
get<K extends keyof AgentMemoryContext>(key: K): Promise<AgentMemoryContext[K]>;
|
|
305
|
+
append<K extends keyof AgentMemoryContext>(key: K, item: AgentMemoryContext[K][0]): Promise<void>;
|
|
306
|
+
set<K extends keyof AgentMemoryContext>(key: K, items: AgentMemoryContext[K]): Promise<void>;
|
|
307
|
+
}
|
|
308
|
+
type Compute<A extends any> = {
|
|
309
|
+
[K in keyof A]: A[K];
|
|
310
|
+
} & unknown;
|
|
311
|
+
|
|
312
|
+
declare function createAgent<const TContextSchema extends ZodContextMapping, const TEventSchemas extends ZodEventMapping, TEvents extends EventObject = EventsFromZodEventMapping<TEventSchemas>, TContext = ContextFromZodContextMapping<TContextSchema>>({ id, description: description, model, events, context, planner, logic, }: {
|
|
313
|
+
/**
|
|
314
|
+
* The unique identifier for the agent.
|
|
315
|
+
*
|
|
316
|
+
* This should be the same across all sessions of a specific agent, as it can be
|
|
317
|
+
* used to retrieve memory for previous episodes of this agent.
|
|
318
|
+
*
|
|
319
|
+
* @example
|
|
320
|
+
* ```ts
|
|
321
|
+
* const agent = createAgent({
|
|
322
|
+
* id: 'recipe-assistant',
|
|
323
|
+
* // ...
|
|
324
|
+
* });
|
|
325
|
+
* ```
|
|
326
|
+
*/
|
|
327
|
+
id?: string;
|
|
328
|
+
/**
|
|
329
|
+
* A description of the role of the agent
|
|
330
|
+
*/
|
|
331
|
+
description?: string;
|
|
332
|
+
/**
|
|
333
|
+
* Events that the agent can cause (send) in an environment
|
|
334
|
+
* that the agent knows about.
|
|
335
|
+
*/
|
|
336
|
+
events: TEventSchemas;
|
|
337
|
+
context?: TContextSchema;
|
|
338
|
+
planner?: AgentPlanner<Agent<TContextSchema, TEventSchemas>>;
|
|
339
|
+
stringify?: typeof JSON.stringify;
|
|
340
|
+
/**
|
|
341
|
+
* A function that retrieves the agent's long term memory
|
|
342
|
+
*/
|
|
343
|
+
getMemory?: (agent: Agent<TContextSchema, TEventSchemas>) => AgentLongTermMemory;
|
|
344
|
+
/**
|
|
345
|
+
* Agent logic
|
|
346
|
+
*/
|
|
347
|
+
logic?: AgentLogic<TEvents>;
|
|
348
|
+
model: LanguageModel;
|
|
349
|
+
} & GenerateTextOptions): Agent<TContextSchema, TEventSchemas>;
|
|
350
|
+
declare class Agent<const TContextSchema extends ZodContextMapping, const TEventSchemas extends ZodEventMapping, TEvents extends EventObject = EventsFromZodEventMapping<TEventSchemas>, TContext = ContextFromZodContextMapping<TContextSchema>> extends Actor<AgentLogic<TEvents>> {
|
|
203
351
|
/**
|
|
204
352
|
* The name of the agent. All agents with the same name are related and
|
|
205
353
|
* able to share experiences (observations, feedback) with each other.
|
|
@@ -208,68 +356,97 @@ type Agent<TContext, TEvents extends EventObject> = ActorRefFrom<AgentLogic<TEve
|
|
|
208
356
|
/**
|
|
209
357
|
* The unique identifier for the agent.
|
|
210
358
|
*/
|
|
211
|
-
|
|
359
|
+
episodeId: string;
|
|
212
360
|
description?: string;
|
|
213
|
-
events:
|
|
361
|
+
events: TEventSchemas;
|
|
362
|
+
context?: TContextSchema;
|
|
363
|
+
planner?: AgentPlanner<Agent<TContextSchema, TEventSchemas>>;
|
|
214
364
|
types: {
|
|
215
365
|
events: TEvents;
|
|
216
366
|
context: Compute<TContext>;
|
|
217
367
|
};
|
|
218
368
|
model: LanguageModel;
|
|
219
|
-
defaultOptions: GenerateTextOptions;
|
|
220
369
|
memory: AgentLongTermMemory | undefined;
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
* - The observed current `state`
|
|
233
|
-
* - The `machine` (e.g. a state machine) that specifies what can happen next
|
|
234
|
-
* - Additional `context`
|
|
235
|
-
*/
|
|
236
|
-
decide: (options: AgentDecideOptions) => Promise<AgentPlan<TEvents> | undefined>;
|
|
237
|
-
generateText: (options: AgentGenerateTextOptions) => Promise<AgentGenerateTextResult>;
|
|
238
|
-
streamText: (options: AgentStreamTextOptions) => Promise<AgentStreamTextResult>;
|
|
239
|
-
addObservation: (observationInput: AgentObservationInput) => AgentObservation<any>;
|
|
240
|
-
addMessage: (messageInput: AgentMessageInput) => AgentMessage;
|
|
241
|
-
addFeedback: (feedbackInput: AgentFeedbackInput) => AgentFeedback;
|
|
242
|
-
addPlan: (plan: AgentPlan<TEvents>) => void;
|
|
370
|
+
defaultOptions: AgentDecideOptions<AnyAgent> | undefined;
|
|
371
|
+
constructor({ logic, id, name, description, model, events, context, planner, }: {
|
|
372
|
+
logic: AgentLogic<TEvents>;
|
|
373
|
+
id?: string;
|
|
374
|
+
name?: string;
|
|
375
|
+
description?: string;
|
|
376
|
+
model: GenerateTextOptions['model'];
|
|
377
|
+
events: TEventSchemas;
|
|
378
|
+
context?: TContextSchema;
|
|
379
|
+
planner?: AgentPlanner<Agent<TContextSchema, TEventSchemas>>;
|
|
380
|
+
});
|
|
243
381
|
/**
|
|
244
382
|
* Called whenever the agent (LLM assistant) receives or sends a message.
|
|
245
383
|
*/
|
|
246
|
-
onMessage
|
|
247
|
-
/**
|
|
248
|
-
* Selects agent data from its context.
|
|
249
|
-
*
|
|
250
|
-
* @deprecated Select from `agent.getSnapshot().context` directly or:
|
|
251
|
-
* - `agent.getMessages()`
|
|
252
|
-
* - `agent.getObservations()`
|
|
253
|
-
* - `agent.getFeedback()`
|
|
254
|
-
* - `agent.getPlans()`
|
|
255
|
-
*/
|
|
256
|
-
select: <T>(selector: (context: AgentMemoryContext) => T) => T;
|
|
384
|
+
onMessage(fn: (message: AgentMessage) => void): Subscription;
|
|
257
385
|
/**
|
|
258
386
|
* Retrieves messages from the agent's short-term (local) memory.
|
|
259
387
|
*/
|
|
260
|
-
|
|
388
|
+
addMessage(messageInput: AgentMessageInput): {
|
|
389
|
+
id: string;
|
|
390
|
+
timestamp: number;
|
|
391
|
+
episodeId: string;
|
|
392
|
+
role: "assistant";
|
|
393
|
+
content: ai.AssistantContent;
|
|
394
|
+
experimental_providerMetadata?: ai.ProviderMetadata;
|
|
395
|
+
responseId?: string;
|
|
396
|
+
result?: ai.GenerateTextResult<any>;
|
|
397
|
+
} | {
|
|
398
|
+
id: string;
|
|
399
|
+
timestamp: number;
|
|
400
|
+
episodeId: string;
|
|
401
|
+
role: "tool";
|
|
402
|
+
content: ai.ToolContent;
|
|
403
|
+
experimental_providerMetadata?: ai.ProviderMetadata;
|
|
404
|
+
responseId?: string;
|
|
405
|
+
result?: ai.GenerateTextResult<any>;
|
|
406
|
+
} | {
|
|
407
|
+
id: string;
|
|
408
|
+
timestamp: number;
|
|
409
|
+
episodeId: string;
|
|
410
|
+
role: "system";
|
|
411
|
+
content: string;
|
|
412
|
+
experimental_providerMetadata?: ai.ProviderMetadata;
|
|
413
|
+
responseId?: string;
|
|
414
|
+
result?: ai.GenerateTextResult<any>;
|
|
415
|
+
} | {
|
|
416
|
+
id: string;
|
|
417
|
+
timestamp: number;
|
|
418
|
+
episodeId: string;
|
|
419
|
+
role: "user";
|
|
420
|
+
content: ai.UserContent;
|
|
421
|
+
experimental_providerMetadata?: ai.ProviderMetadata;
|
|
422
|
+
responseId?: string;
|
|
423
|
+
result?: ai.GenerateTextResult<any>;
|
|
424
|
+
};
|
|
425
|
+
getMessages(): AgentMessage[];
|
|
426
|
+
addFeedback(feedbackInput: AgentFeedbackInput): {
|
|
427
|
+
attributes: {
|
|
428
|
+
[x: string]: any;
|
|
429
|
+
};
|
|
430
|
+
reward: number;
|
|
431
|
+
timestamp: number;
|
|
432
|
+
episodeId: string;
|
|
433
|
+
goal?: string;
|
|
434
|
+
observationId?: string;
|
|
435
|
+
};
|
|
261
436
|
/**
|
|
262
|
-
* Retrieves
|
|
437
|
+
* Retrieves feedback from the agent's short-term (local) memory.
|
|
263
438
|
*/
|
|
264
|
-
|
|
439
|
+
getFeedback(): AgentFeedback[];
|
|
440
|
+
addObservation(observationInput: AgentObservationInput): AgentObservation<any>;
|
|
265
441
|
/**
|
|
266
|
-
* Retrieves
|
|
442
|
+
* Retrieves observations from the agent's short-term (local) memory.
|
|
267
443
|
*/
|
|
268
|
-
|
|
444
|
+
getObservations(): AgentObservation<any>[];
|
|
445
|
+
addPlan(plan: AgentPlan<TEvents>): void;
|
|
269
446
|
/**
|
|
270
447
|
* Retrieves strategies from the agent's short-term (local) memory.
|
|
271
448
|
*/
|
|
272
|
-
getPlans
|
|
449
|
+
getPlans(): AgentPlan<any>[];
|
|
273
450
|
/**
|
|
274
451
|
* Interacts with this state machine actor by inspecting state transitions and storing them as observations.
|
|
275
452
|
*
|
|
@@ -286,7 +463,7 @@ type Agent<TContext, TEvents extends EventObject> = ActorRefFrom<AgentLogic<TEve
|
|
|
286
463
|
* actor.start();
|
|
287
464
|
* ```
|
|
288
465
|
*/
|
|
289
|
-
interact<TActor extends
|
|
466
|
+
interact<TActor extends ActorRefLike>(actorRef: TActor): Subscription;
|
|
290
467
|
/**
|
|
291
468
|
* Interacts with this state machine actor by:
|
|
292
469
|
* 1. Inspecting state transitions and storing them as observations
|
|
@@ -314,115 +491,29 @@ type Agent<TContext, TEvents extends EventObject> = ActorRefFrom<AgentLogic<TEve
|
|
|
314
491
|
* actor.start();
|
|
315
492
|
* ```
|
|
316
493
|
*/
|
|
317
|
-
interact<TActor extends
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
type FromAgent<T> = T | ((agent: AnyAgent) => T | Promise<T>);
|
|
321
|
-
type CommonTextOptions = {
|
|
322
|
-
prompt: FromAgent<string>;
|
|
323
|
-
model?: LanguageModel;
|
|
324
|
-
context?: Record<string, any>;
|
|
325
|
-
messages?: FromAgent<CoreMessage[]>;
|
|
326
|
-
template?: PromptTemplate<any>;
|
|
327
|
-
correlationId?: string;
|
|
328
|
-
parentCorrelationId?: string;
|
|
329
|
-
};
|
|
330
|
-
type TextResultMeta = {
|
|
331
|
-
correlationId: string;
|
|
332
|
-
parentCorrelationId?: string;
|
|
333
|
-
};
|
|
334
|
-
type AgentGenerateTextOptions = Omit<GenerateTextOptions, 'model' | 'prompt' | 'messages'> & CommonTextOptions;
|
|
335
|
-
type AgentGenerateTextResult = GenerateTextResult<any> & TextResultMeta;
|
|
336
|
-
type AgentStreamTextOptions = Omit<StreamTextOptions, 'model' | 'prompt' | 'messages'> & CommonTextOptions;
|
|
337
|
-
type AgentStreamTextResult = StreamTextResult<any> & TextResultMeta;
|
|
338
|
-
interface ObservedState {
|
|
494
|
+
interact<TActor extends ActorRefLike>(actorRef: TActor, getInput: (observation: AgentObservation<TActor>) => AgentDecisionInput | undefined): Subscription;
|
|
495
|
+
observe<TActor extends ActorRefLike>(actorRef: TActor): Subscription;
|
|
496
|
+
wrap(modelToWrap: LanguageModelV1): LanguageModelV1;
|
|
339
497
|
/**
|
|
340
|
-
*
|
|
341
|
-
* `"loading"` or `"processing"` or `"ready"`
|
|
342
|
-
*/
|
|
343
|
-
value: StateValue;
|
|
344
|
-
/**
|
|
345
|
-
* Additional contextual data related to the current state
|
|
346
|
-
*/
|
|
347
|
-
context: Record<string, unknown>;
|
|
348
|
-
}
|
|
349
|
-
type ObservedStateFrom<TActor extends AnyActorRef> = Pick<SnapshotFrom<TActor>, 'value' | 'context'>;
|
|
350
|
-
type AgentMemoryContext = {
|
|
351
|
-
observations: AgentObservation<any>[];
|
|
352
|
-
messages: AgentMessage[];
|
|
353
|
-
plans: AgentPlan<any>[];
|
|
354
|
-
feedback: AgentFeedback[];
|
|
355
|
-
};
|
|
356
|
-
type AgentMemory = AppendOnlyStorage<AgentMemoryContext>;
|
|
357
|
-
interface AppendOnlyStorage<T extends Record<string, any[]>> {
|
|
358
|
-
append<K extends keyof T>(sessionId: string, key: K, item: T[K][0]): Promise<void>;
|
|
359
|
-
getAll<K extends keyof T>(sessionId: string, key: K): Promise<T[K] | undefined>;
|
|
360
|
-
}
|
|
361
|
-
interface AgentLongTermMemory {
|
|
362
|
-
get<K extends keyof AgentMemoryContext>(key: K): Promise<AgentMemoryContext[K]>;
|
|
363
|
-
append<K extends keyof AgentMemoryContext>(key: K, item: AgentMemoryContext[K][0]): Promise<void>;
|
|
364
|
-
set<K extends keyof AgentMemoryContext>(key: K, items: AgentMemoryContext[K]): Promise<void>;
|
|
365
|
-
}
|
|
366
|
-
interface AIAdapter {
|
|
367
|
-
generateText: typeof generateText;
|
|
368
|
-
streamText: typeof streamText;
|
|
369
|
-
}
|
|
370
|
-
type Compute<A extends any> = {
|
|
371
|
-
[K in keyof A]: A[K];
|
|
372
|
-
} & unknown;
|
|
373
|
-
|
|
374
|
-
declare function createAgent<const TContextSchema extends ZodContextMapping, const TEventSchemas extends ZodEventMapping, TEvents extends EventObject = EventsFromZodEventMapping<TEventSchemas>, TContext = ContextFromZodContextMapping<TContextSchema>>({ name, description, model, events, context, planner, stringify, getMemory, logic, adapter, ...generateTextOptions }: {
|
|
375
|
-
/**
|
|
376
|
-
* The unique identifier for the agent.
|
|
377
|
-
*
|
|
378
|
-
* This should be the same across all sessions of a specific agent, as it can be
|
|
379
|
-
* used to retrieve memory for this agent.
|
|
498
|
+
* Resolves with an `AgentPlan` based on the information provided in the `options`, including:
|
|
380
499
|
*
|
|
381
|
-
*
|
|
382
|
-
*
|
|
383
|
-
*
|
|
384
|
-
*
|
|
385
|
-
* // ...
|
|
386
|
-
* });
|
|
387
|
-
* ```
|
|
388
|
-
*/
|
|
389
|
-
id?: string;
|
|
390
|
-
/**
|
|
391
|
-
* The name of the agent
|
|
392
|
-
*/
|
|
393
|
-
name?: string;
|
|
394
|
-
/**
|
|
395
|
-
* A description of the role of the agent
|
|
396
|
-
*/
|
|
397
|
-
description?: string;
|
|
398
|
-
/**
|
|
399
|
-
* Events that the agent can cause (send) in an environment
|
|
400
|
-
* that the agent knows about.
|
|
401
|
-
*/
|
|
402
|
-
events: TEventSchemas;
|
|
403
|
-
context?: TContextSchema;
|
|
404
|
-
planner?: AgentPlanner<Agent<TContext, TEvents>>;
|
|
405
|
-
stringify?: typeof JSON.stringify;
|
|
406
|
-
/**
|
|
407
|
-
* A function that retrieves the agent's long term memory
|
|
408
|
-
*/
|
|
409
|
-
getMemory?: (agent: Agent<TContext, TEvents>) => AgentLongTermMemory;
|
|
410
|
-
/**
|
|
411
|
-
* Agent logic
|
|
500
|
+
* - The `goal` for the agent to achieve
|
|
501
|
+
* - The observed current `state`
|
|
502
|
+
* - The `machine` (e.g. a state machine) that specifies what can happen next
|
|
503
|
+
* - Additional `context`
|
|
412
504
|
*/
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
} & GenerateTextOptions): Agent<TContext, TEvents>;
|
|
505
|
+
decide(opts: AgentDecideOptions<this>): Promise<AgentPlan<EventsFromZodEventMapping<this["events"]>> | undefined>;
|
|
506
|
+
}
|
|
416
507
|
|
|
417
|
-
declare function fromTextStream<T extends AnyAgent>(agent: T,
|
|
508
|
+
declare function fromTextStream<T extends AnyAgent>(agent: T, options?: AgentStreamTextOptions): ObservableActorLogic<{
|
|
418
509
|
textDelta: string;
|
|
419
510
|
}, Omit<AgentStreamTextOptions, 'context'> & {
|
|
420
511
|
context?: AgentStreamTextOptions['context'];
|
|
421
512
|
}>;
|
|
422
|
-
declare function fromText<T extends AnyAgent>(agent: T,
|
|
513
|
+
declare function fromText<T extends AnyAgent>(agent: T, options?: AgentGenerateTextOptions): PromiseActorLogic<GenerateTextResult<Record<string, CoreTool<any, any>>>, Omit<AgentGenerateTextOptions, 'context'> & {
|
|
423
514
|
context?: AgentGenerateTextOptions['context'];
|
|
424
515
|
}>;
|
|
425
516
|
|
|
426
517
|
declare function fromDecision(agent: AnyAgent, defaultInput?: AgentDecisionInput): AgentDecisionLogic<any>;
|
|
427
518
|
|
|
428
|
-
export { type
|
|
519
|
+
export { type AgentDecideOptions, type AgentDecisionInput, type AgentDecisionLogic, type AgentEmitted, type AgentFeedback, type AgentFeedbackInput, type AgentGenerateTextOptions, type AgentLogic, type AgentLongTermMemory, type AgentMemoryContext, type AgentMessage, type AgentMessageInput, type AgentObservation, type AgentObservationInput, type AgentPath, type AgentPlan, type AgentPlanInput, type AgentPlanner, type AgentStep, type AgentStreamTextOptions, type AnyAgent, type CommonTextOptions, type Compute, type ContextFromZodContextMapping, type CostFunction, type EventsFromZodEventMapping, type FromAgent, type GenerateTextOptions, type LanguageModelV1TextPart, type LanguageModelV1ToolCallPart, type ObservedState, type ObservedStateFrom, type PromptTemplate, type StreamTextOptions, type TransitionData, createAgent, fromDecision, fromText, fromTextStream };
|