@statelyai/agent 2.0.0-next.0 → 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/old-jobs-check.md +5 -0
- package/.changeset/pre.json +4 -1
- package/CHANGELOG.md +12 -0
- package/dist/index.d.mts +19 -25
- package/dist/index.d.ts +19 -25
- package/dist/index.js +43 -39
- package/dist/index.mjs +43 -39
- package/examples/chatbot-alt.ts +1 -1
- package/examples/chatbot.ts +1 -1
- package/examples/cot.ts +1 -1
- package/examples/customer-service-sim.ts +4 -4
- package/examples/email.ts +29 -33
- package/examples/example.ts +1 -1
- package/examples/goal.ts +1 -1
- package/examples/joke.ts +1 -1
- package/examples/jugs.ts +3 -3
- package/examples/multi.ts +1 -1
- package/examples/number.ts +1 -1
- package/examples/raffle.ts +1 -1
- package/examples/river-crossing.ts +3 -3
- package/examples/simple.ts +1 -1
- package/examples/summary.ts +1 -1
- package/examples/support.ts +1 -1
- package/examples/ticTacToe.ts +2 -2
- package/examples/todo.ts +1 -1
- package/examples/tutor.ts +2 -2
- package/examples/verify.ts +1 -1
- package/examples/weather.ts +1 -1
- package/examples/wiki.ts +1 -1
- package/examples/word.ts +1 -1
- package/package.json +5 -3
- package/src/agent.test.ts +150 -10
- package/src/agent.ts +20 -217
- package/src/decide.test.ts +124 -3
- package/src/decide.ts +24 -14
- package/src/middleware.ts +2 -14
- package/src/planners/shortestPath.test.ts +94 -0
- package/src/planners/shortestPath.ts +177 -0
- package/src/planners/{simplePlanner.ts → simple.ts} +6 -12
- package/src/types.ts +15 -8
- package/src/utils.ts +11 -0
- package/vitest.config.ts +9 -3
- package/src/planners/shortestPathPlanner.ts +0 -160
package/.changeset/pre.json
CHANGED
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
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
|
+
|
|
3
15
|
## 2.0.0-next.0
|
|
4
16
|
|
|
5
17
|
### Major Changes
|
package/dist/index.d.mts
CHANGED
|
@@ -13,7 +13,7 @@ type ZodContextMapping = {
|
|
|
13
13
|
type GenerateTextOptions = Parameters<typeof generateText>[0];
|
|
14
14
|
type StreamTextOptions = Parameters<typeof streamText>[0];
|
|
15
15
|
type CostFunction<TEvent extends EventObject> = (path: AgentPath<TEvent>) => number;
|
|
16
|
-
type AgentPlanInput<TEvent extends EventObject> = Omit<
|
|
16
|
+
type AgentPlanInput<TEvent extends EventObject> = Omit<AgentGenerateTextOptions, 'prompt' | 'tools'> & {
|
|
17
17
|
/**
|
|
18
18
|
* The currently observed state.
|
|
19
19
|
*/
|
|
@@ -41,6 +41,11 @@ type AgentPlanInput<TEvent extends EventObject> = Omit<GenerateTextOptions, 'pro
|
|
|
41
41
|
* The total cost of the path to the goal state.
|
|
42
42
|
*/
|
|
43
43
|
costFunction?: CostFunction<TEvent>;
|
|
44
|
+
/**
|
|
45
|
+
* The maximum number of attempts to generate a plan.
|
|
46
|
+
* Defaults to 2.
|
|
47
|
+
*/
|
|
48
|
+
maxAttempts?: number;
|
|
44
49
|
};
|
|
45
50
|
type AgentStep<TEvent extends EventObject> = {
|
|
46
51
|
/** The event to take */
|
|
@@ -115,14 +120,19 @@ type PromptTemplate<TEvents extends EventObject> = (data: {
|
|
|
115
120
|
plans?: AgentPlan<TEvents>[];
|
|
116
121
|
}) => string;
|
|
117
122
|
type AgentPlanner<T extends AnyAgent> = (agent: T, input: AgentPlanInput<T['types']['events']>) => Promise<AgentPlan<T['types']['events']> | undefined>;
|
|
118
|
-
type AgentDecideOptions = {
|
|
123
|
+
type AgentDecideOptions<T extends AnyAgent> = {
|
|
119
124
|
goal: string;
|
|
120
125
|
model?: LanguageModel;
|
|
121
126
|
state: ObservedState;
|
|
122
127
|
machine?: AnyStateMachine;
|
|
123
128
|
execute?: (event: AnyEventObject) => Promise<void>;
|
|
124
|
-
planner?: AgentPlanner<
|
|
129
|
+
planner?: AgentPlanner<T>;
|
|
125
130
|
events?: ZodEventMapping;
|
|
131
|
+
/**
|
|
132
|
+
* The maximum number of times the agent will attempt to make a decision.
|
|
133
|
+
* Defaults to 2.
|
|
134
|
+
*/
|
|
135
|
+
maxAttempts?: number;
|
|
126
136
|
} & Omit<Parameters<typeof generateText>[0], 'model' | 'tools' | 'prompt'>;
|
|
127
137
|
interface AgentFeedback {
|
|
128
138
|
goal?: string;
|
|
@@ -130,7 +140,6 @@ interface AgentFeedback {
|
|
|
130
140
|
/**
|
|
131
141
|
* The message correlation that the feedback is relevant for
|
|
132
142
|
*/
|
|
133
|
-
correlationId?: string;
|
|
134
143
|
attributes: Record<string, any>;
|
|
135
144
|
reward: number;
|
|
136
145
|
timestamp: number;
|
|
@@ -139,7 +148,6 @@ interface AgentFeedback {
|
|
|
139
148
|
interface AgentFeedbackInput {
|
|
140
149
|
goal?: string;
|
|
141
150
|
observationId?: string;
|
|
142
|
-
correlationId?: string;
|
|
143
151
|
attributes?: Record<string, any>;
|
|
144
152
|
timestamp?: number;
|
|
145
153
|
reward?: number;
|
|
@@ -203,8 +211,6 @@ type AgentMessageInput = CoreMessage & {
|
|
|
203
211
|
* which message this message is responding to, if any.
|
|
204
212
|
*/
|
|
205
213
|
responseId?: string;
|
|
206
|
-
correlationId?: string;
|
|
207
|
-
parentCorrelationId?: string;
|
|
208
214
|
result?: GenerateTextResult<any>;
|
|
209
215
|
};
|
|
210
216
|
interface AgentObservation<TActor extends ActorRefLike> {
|
|
@@ -265,7 +271,7 @@ type EventsFromZodEventMapping<TEventSchemas extends ZodEventMapping> = Values<{
|
|
|
265
271
|
type ContextFromZodContextMapping<TContextSchema extends ZodContextMapping> = {
|
|
266
272
|
[K in keyof TContextSchema & string]: TypeOf<TContextSchema[K]>;
|
|
267
273
|
};
|
|
268
|
-
type AnyAgent = Agent<any, any>;
|
|
274
|
+
type AnyAgent = Agent<any, any, any, any>;
|
|
269
275
|
type FromAgent<T> = T | ((agent: AnyAgent) => T | Promise<T>);
|
|
270
276
|
type CommonTextOptions = {
|
|
271
277
|
prompt: FromAgent<string>;
|
|
@@ -303,12 +309,12 @@ type Compute<A extends any> = {
|
|
|
303
309
|
[K in keyof A]: A[K];
|
|
304
310
|
} & unknown;
|
|
305
311
|
|
|
306
|
-
declare function createAgent<const TContextSchema extends ZodContextMapping, const TEventSchemas extends ZodEventMapping, TEvents extends EventObject = EventsFromZodEventMapping<TEventSchemas>, TContext = ContextFromZodContextMapping<TContextSchema>>({ id,
|
|
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, }: {
|
|
307
313
|
/**
|
|
308
314
|
* The unique identifier for the agent.
|
|
309
315
|
*
|
|
310
316
|
* This should be the same across all sessions of a specific agent, as it can be
|
|
311
|
-
* used to retrieve memory for this agent.
|
|
317
|
+
* used to retrieve memory for previous episodes of this agent.
|
|
312
318
|
*
|
|
313
319
|
* @example
|
|
314
320
|
* ```ts
|
|
@@ -319,10 +325,6 @@ declare function createAgent<const TContextSchema extends ZodContextMapping, con
|
|
|
319
325
|
* ```
|
|
320
326
|
*/
|
|
321
327
|
id?: string;
|
|
322
|
-
/**
|
|
323
|
-
* The name of the agent
|
|
324
|
-
*/
|
|
325
|
-
name?: string;
|
|
326
328
|
/**
|
|
327
329
|
* A description of the role of the agent
|
|
328
330
|
*/
|
|
@@ -343,6 +345,7 @@ declare function createAgent<const TContextSchema extends ZodContextMapping, con
|
|
|
343
345
|
* Agent logic
|
|
344
346
|
*/
|
|
345
347
|
logic?: AgentLogic<TEvents>;
|
|
348
|
+
model: LanguageModel;
|
|
346
349
|
} & GenerateTextOptions): Agent<TContextSchema, TEventSchemas>;
|
|
347
350
|
declare class Agent<const TContextSchema extends ZodContextMapping, const TEventSchemas extends ZodEventMapping, TEvents extends EventObject = EventsFromZodEventMapping<TEventSchemas>, TContext = ContextFromZodContextMapping<TContextSchema>> extends Actor<AgentLogic<TEvents>> {
|
|
348
351
|
/**
|
|
@@ -364,7 +367,7 @@ declare class Agent<const TContextSchema extends ZodContextMapping, const TEvent
|
|
|
364
367
|
};
|
|
365
368
|
model: LanguageModel;
|
|
366
369
|
memory: AgentLongTermMemory | undefined;
|
|
367
|
-
defaultOptions:
|
|
370
|
+
defaultOptions: AgentDecideOptions<AnyAgent> | undefined;
|
|
368
371
|
constructor({ logic, id, name, description, model, events, context, planner, }: {
|
|
369
372
|
logic: AgentLogic<TEvents>;
|
|
370
373
|
id?: string;
|
|
@@ -390,8 +393,6 @@ declare class Agent<const TContextSchema extends ZodContextMapping, const TEvent
|
|
|
390
393
|
content: ai.AssistantContent;
|
|
391
394
|
experimental_providerMetadata?: ai.ProviderMetadata;
|
|
392
395
|
responseId?: string;
|
|
393
|
-
correlationId?: string;
|
|
394
|
-
parentCorrelationId?: string;
|
|
395
396
|
result?: ai.GenerateTextResult<any>;
|
|
396
397
|
} | {
|
|
397
398
|
id: string;
|
|
@@ -401,8 +402,6 @@ declare class Agent<const TContextSchema extends ZodContextMapping, const TEvent
|
|
|
401
402
|
content: ai.ToolContent;
|
|
402
403
|
experimental_providerMetadata?: ai.ProviderMetadata;
|
|
403
404
|
responseId?: string;
|
|
404
|
-
correlationId?: string;
|
|
405
|
-
parentCorrelationId?: string;
|
|
406
405
|
result?: ai.GenerateTextResult<any>;
|
|
407
406
|
} | {
|
|
408
407
|
id: string;
|
|
@@ -412,8 +411,6 @@ declare class Agent<const TContextSchema extends ZodContextMapping, const TEvent
|
|
|
412
411
|
content: string;
|
|
413
412
|
experimental_providerMetadata?: ai.ProviderMetadata;
|
|
414
413
|
responseId?: string;
|
|
415
|
-
correlationId?: string;
|
|
416
|
-
parentCorrelationId?: string;
|
|
417
414
|
result?: ai.GenerateTextResult<any>;
|
|
418
415
|
} | {
|
|
419
416
|
id: string;
|
|
@@ -423,8 +420,6 @@ declare class Agent<const TContextSchema extends ZodContextMapping, const TEvent
|
|
|
423
420
|
content: ai.UserContent;
|
|
424
421
|
experimental_providerMetadata?: ai.ProviderMetadata;
|
|
425
422
|
responseId?: string;
|
|
426
|
-
correlationId?: string;
|
|
427
|
-
parentCorrelationId?: string;
|
|
428
423
|
result?: ai.GenerateTextResult<any>;
|
|
429
424
|
};
|
|
430
425
|
getMessages(): AgentMessage[];
|
|
@@ -437,7 +432,6 @@ declare class Agent<const TContextSchema extends ZodContextMapping, const TEvent
|
|
|
437
432
|
episodeId: string;
|
|
438
433
|
goal?: string;
|
|
439
434
|
observationId?: string;
|
|
440
|
-
correlationId?: string;
|
|
441
435
|
};
|
|
442
436
|
/**
|
|
443
437
|
* Retrieves feedback from the agent's short-term (local) memory.
|
|
@@ -508,7 +502,7 @@ declare class Agent<const TContextSchema extends ZodContextMapping, const TEvent
|
|
|
508
502
|
* - The `machine` (e.g. a state machine) that specifies what can happen next
|
|
509
503
|
* - Additional `context`
|
|
510
504
|
*/
|
|
511
|
-
decide(opts: AgentDecideOptions): Promise<AgentPlan<EventsFromZodEventMapping<this["events"]>> | undefined>;
|
|
505
|
+
decide(opts: AgentDecideOptions<this>): Promise<AgentPlan<EventsFromZodEventMapping<this["events"]>> | undefined>;
|
|
512
506
|
}
|
|
513
507
|
|
|
514
508
|
declare function fromTextStream<T extends AnyAgent>(agent: T, options?: AgentStreamTextOptions): ObservableActorLogic<{
|
package/dist/index.d.ts
CHANGED
|
@@ -13,7 +13,7 @@ type ZodContextMapping = {
|
|
|
13
13
|
type GenerateTextOptions = Parameters<typeof generateText>[0];
|
|
14
14
|
type StreamTextOptions = Parameters<typeof streamText>[0];
|
|
15
15
|
type CostFunction<TEvent extends EventObject> = (path: AgentPath<TEvent>) => number;
|
|
16
|
-
type AgentPlanInput<TEvent extends EventObject> = Omit<
|
|
16
|
+
type AgentPlanInput<TEvent extends EventObject> = Omit<AgentGenerateTextOptions, 'prompt' | 'tools'> & {
|
|
17
17
|
/**
|
|
18
18
|
* The currently observed state.
|
|
19
19
|
*/
|
|
@@ -41,6 +41,11 @@ type AgentPlanInput<TEvent extends EventObject> = Omit<GenerateTextOptions, 'pro
|
|
|
41
41
|
* The total cost of the path to the goal state.
|
|
42
42
|
*/
|
|
43
43
|
costFunction?: CostFunction<TEvent>;
|
|
44
|
+
/**
|
|
45
|
+
* The maximum number of attempts to generate a plan.
|
|
46
|
+
* Defaults to 2.
|
|
47
|
+
*/
|
|
48
|
+
maxAttempts?: number;
|
|
44
49
|
};
|
|
45
50
|
type AgentStep<TEvent extends EventObject> = {
|
|
46
51
|
/** The event to take */
|
|
@@ -115,14 +120,19 @@ type PromptTemplate<TEvents extends EventObject> = (data: {
|
|
|
115
120
|
plans?: AgentPlan<TEvents>[];
|
|
116
121
|
}) => string;
|
|
117
122
|
type AgentPlanner<T extends AnyAgent> = (agent: T, input: AgentPlanInput<T['types']['events']>) => Promise<AgentPlan<T['types']['events']> | undefined>;
|
|
118
|
-
type AgentDecideOptions = {
|
|
123
|
+
type AgentDecideOptions<T extends AnyAgent> = {
|
|
119
124
|
goal: string;
|
|
120
125
|
model?: LanguageModel;
|
|
121
126
|
state: ObservedState;
|
|
122
127
|
machine?: AnyStateMachine;
|
|
123
128
|
execute?: (event: AnyEventObject) => Promise<void>;
|
|
124
|
-
planner?: AgentPlanner<
|
|
129
|
+
planner?: AgentPlanner<T>;
|
|
125
130
|
events?: ZodEventMapping;
|
|
131
|
+
/**
|
|
132
|
+
* The maximum number of times the agent will attempt to make a decision.
|
|
133
|
+
* Defaults to 2.
|
|
134
|
+
*/
|
|
135
|
+
maxAttempts?: number;
|
|
126
136
|
} & Omit<Parameters<typeof generateText>[0], 'model' | 'tools' | 'prompt'>;
|
|
127
137
|
interface AgentFeedback {
|
|
128
138
|
goal?: string;
|
|
@@ -130,7 +140,6 @@ interface AgentFeedback {
|
|
|
130
140
|
/**
|
|
131
141
|
* The message correlation that the feedback is relevant for
|
|
132
142
|
*/
|
|
133
|
-
correlationId?: string;
|
|
134
143
|
attributes: Record<string, any>;
|
|
135
144
|
reward: number;
|
|
136
145
|
timestamp: number;
|
|
@@ -139,7 +148,6 @@ interface AgentFeedback {
|
|
|
139
148
|
interface AgentFeedbackInput {
|
|
140
149
|
goal?: string;
|
|
141
150
|
observationId?: string;
|
|
142
|
-
correlationId?: string;
|
|
143
151
|
attributes?: Record<string, any>;
|
|
144
152
|
timestamp?: number;
|
|
145
153
|
reward?: number;
|
|
@@ -203,8 +211,6 @@ type AgentMessageInput = CoreMessage & {
|
|
|
203
211
|
* which message this message is responding to, if any.
|
|
204
212
|
*/
|
|
205
213
|
responseId?: string;
|
|
206
|
-
correlationId?: string;
|
|
207
|
-
parentCorrelationId?: string;
|
|
208
214
|
result?: GenerateTextResult<any>;
|
|
209
215
|
};
|
|
210
216
|
interface AgentObservation<TActor extends ActorRefLike> {
|
|
@@ -265,7 +271,7 @@ type EventsFromZodEventMapping<TEventSchemas extends ZodEventMapping> = Values<{
|
|
|
265
271
|
type ContextFromZodContextMapping<TContextSchema extends ZodContextMapping> = {
|
|
266
272
|
[K in keyof TContextSchema & string]: TypeOf<TContextSchema[K]>;
|
|
267
273
|
};
|
|
268
|
-
type AnyAgent = Agent<any, any>;
|
|
274
|
+
type AnyAgent = Agent<any, any, any, any>;
|
|
269
275
|
type FromAgent<T> = T | ((agent: AnyAgent) => T | Promise<T>);
|
|
270
276
|
type CommonTextOptions = {
|
|
271
277
|
prompt: FromAgent<string>;
|
|
@@ -303,12 +309,12 @@ type Compute<A extends any> = {
|
|
|
303
309
|
[K in keyof A]: A[K];
|
|
304
310
|
} & unknown;
|
|
305
311
|
|
|
306
|
-
declare function createAgent<const TContextSchema extends ZodContextMapping, const TEventSchemas extends ZodEventMapping, TEvents extends EventObject = EventsFromZodEventMapping<TEventSchemas>, TContext = ContextFromZodContextMapping<TContextSchema>>({ id,
|
|
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, }: {
|
|
307
313
|
/**
|
|
308
314
|
* The unique identifier for the agent.
|
|
309
315
|
*
|
|
310
316
|
* This should be the same across all sessions of a specific agent, as it can be
|
|
311
|
-
* used to retrieve memory for this agent.
|
|
317
|
+
* used to retrieve memory for previous episodes of this agent.
|
|
312
318
|
*
|
|
313
319
|
* @example
|
|
314
320
|
* ```ts
|
|
@@ -319,10 +325,6 @@ declare function createAgent<const TContextSchema extends ZodContextMapping, con
|
|
|
319
325
|
* ```
|
|
320
326
|
*/
|
|
321
327
|
id?: string;
|
|
322
|
-
/**
|
|
323
|
-
* The name of the agent
|
|
324
|
-
*/
|
|
325
|
-
name?: string;
|
|
326
328
|
/**
|
|
327
329
|
* A description of the role of the agent
|
|
328
330
|
*/
|
|
@@ -343,6 +345,7 @@ declare function createAgent<const TContextSchema extends ZodContextMapping, con
|
|
|
343
345
|
* Agent logic
|
|
344
346
|
*/
|
|
345
347
|
logic?: AgentLogic<TEvents>;
|
|
348
|
+
model: LanguageModel;
|
|
346
349
|
} & GenerateTextOptions): Agent<TContextSchema, TEventSchemas>;
|
|
347
350
|
declare class Agent<const TContextSchema extends ZodContextMapping, const TEventSchemas extends ZodEventMapping, TEvents extends EventObject = EventsFromZodEventMapping<TEventSchemas>, TContext = ContextFromZodContextMapping<TContextSchema>> extends Actor<AgentLogic<TEvents>> {
|
|
348
351
|
/**
|
|
@@ -364,7 +367,7 @@ declare class Agent<const TContextSchema extends ZodContextMapping, const TEvent
|
|
|
364
367
|
};
|
|
365
368
|
model: LanguageModel;
|
|
366
369
|
memory: AgentLongTermMemory | undefined;
|
|
367
|
-
defaultOptions:
|
|
370
|
+
defaultOptions: AgentDecideOptions<AnyAgent> | undefined;
|
|
368
371
|
constructor({ logic, id, name, description, model, events, context, planner, }: {
|
|
369
372
|
logic: AgentLogic<TEvents>;
|
|
370
373
|
id?: string;
|
|
@@ -390,8 +393,6 @@ declare class Agent<const TContextSchema extends ZodContextMapping, const TEvent
|
|
|
390
393
|
content: ai.AssistantContent;
|
|
391
394
|
experimental_providerMetadata?: ai.ProviderMetadata;
|
|
392
395
|
responseId?: string;
|
|
393
|
-
correlationId?: string;
|
|
394
|
-
parentCorrelationId?: string;
|
|
395
396
|
result?: ai.GenerateTextResult<any>;
|
|
396
397
|
} | {
|
|
397
398
|
id: string;
|
|
@@ -401,8 +402,6 @@ declare class Agent<const TContextSchema extends ZodContextMapping, const TEvent
|
|
|
401
402
|
content: ai.ToolContent;
|
|
402
403
|
experimental_providerMetadata?: ai.ProviderMetadata;
|
|
403
404
|
responseId?: string;
|
|
404
|
-
correlationId?: string;
|
|
405
|
-
parentCorrelationId?: string;
|
|
406
405
|
result?: ai.GenerateTextResult<any>;
|
|
407
406
|
} | {
|
|
408
407
|
id: string;
|
|
@@ -412,8 +411,6 @@ declare class Agent<const TContextSchema extends ZodContextMapping, const TEvent
|
|
|
412
411
|
content: string;
|
|
413
412
|
experimental_providerMetadata?: ai.ProviderMetadata;
|
|
414
413
|
responseId?: string;
|
|
415
|
-
correlationId?: string;
|
|
416
|
-
parentCorrelationId?: string;
|
|
417
414
|
result?: ai.GenerateTextResult<any>;
|
|
418
415
|
} | {
|
|
419
416
|
id: string;
|
|
@@ -423,8 +420,6 @@ declare class Agent<const TContextSchema extends ZodContextMapping, const TEvent
|
|
|
423
420
|
content: ai.UserContent;
|
|
424
421
|
experimental_providerMetadata?: ai.ProviderMetadata;
|
|
425
422
|
responseId?: string;
|
|
426
|
-
correlationId?: string;
|
|
427
|
-
parentCorrelationId?: string;
|
|
428
423
|
result?: ai.GenerateTextResult<any>;
|
|
429
424
|
};
|
|
430
425
|
getMessages(): AgentMessage[];
|
|
@@ -437,7 +432,6 @@ declare class Agent<const TContextSchema extends ZodContextMapping, const TEvent
|
|
|
437
432
|
episodeId: string;
|
|
438
433
|
goal?: string;
|
|
439
434
|
observationId?: string;
|
|
440
|
-
correlationId?: string;
|
|
441
435
|
};
|
|
442
436
|
/**
|
|
443
437
|
* Retrieves feedback from the agent's short-term (local) memory.
|
|
@@ -508,7 +502,7 @@ declare class Agent<const TContextSchema extends ZodContextMapping, const TEvent
|
|
|
508
502
|
* - The `machine` (e.g. a state machine) that specifies what can happen next
|
|
509
503
|
* - Additional `context`
|
|
510
504
|
*/
|
|
511
|
-
decide(opts: AgentDecideOptions): Promise<AgentPlan<EventsFromZodEventMapping<this["events"]>> | undefined>;
|
|
505
|
+
decide(opts: AgentDecideOptions<this>): Promise<AgentPlan<EventsFromZodEventMapping<this["events"]>> | undefined>;
|
|
512
506
|
}
|
|
513
507
|
|
|
514
508
|
declare function fromTextStream<T extends AnyAgent>(agent: T, options?: AgentStreamTextOptions): ObservableActorLogic<{
|
package/dist/index.js
CHANGED
|
@@ -40,7 +40,7 @@ module.exports = __toCommonJS(src_exports);
|
|
|
40
40
|
// src/agent.ts
|
|
41
41
|
var import_xstate4 = require("xstate");
|
|
42
42
|
|
|
43
|
-
// src/planners/
|
|
43
|
+
// src/planners/simple.ts
|
|
44
44
|
var import_ai3 = require("ai");
|
|
45
45
|
|
|
46
46
|
// src/utils.ts
|
|
@@ -106,8 +106,11 @@ function getTransitions(state, machine) {
|
|
|
106
106
|
});
|
|
107
107
|
return getAllTransitions(resolvedState);
|
|
108
108
|
}
|
|
109
|
+
function isMachineActor(actor) {
|
|
110
|
+
return "src" in actor && typeof actor.src === "object" && actor.src !== null && "definition" in actor.src;
|
|
111
|
+
}
|
|
109
112
|
|
|
110
|
-
// src/planners/
|
|
113
|
+
// src/planners/simple.ts
|
|
111
114
|
var import_xstate3 = require("xstate");
|
|
112
115
|
|
|
113
116
|
// src/templates/defaultText.ts
|
|
@@ -218,19 +221,27 @@ async function agentDecide(agent, options) {
|
|
|
218
221
|
state,
|
|
219
222
|
machine,
|
|
220
223
|
model = agent.model,
|
|
224
|
+
messages,
|
|
221
225
|
...otherPlanInput
|
|
222
226
|
} = resolvedOptions;
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
227
|
+
let attempts = 0;
|
|
228
|
+
const maxAttempts = resolvedOptions.maxAttempts ?? 2;
|
|
229
|
+
let plan;
|
|
230
|
+
while (attempts++ < maxAttempts) {
|
|
231
|
+
plan = await planner(agent, {
|
|
232
|
+
model,
|
|
233
|
+
goal,
|
|
234
|
+
events,
|
|
235
|
+
state,
|
|
236
|
+
machine,
|
|
237
|
+
messages,
|
|
238
|
+
// TODO: fix UIMessage thing
|
|
239
|
+
...otherPlanInput
|
|
240
|
+
});
|
|
241
|
+
if (plan?.nextEvent) {
|
|
242
|
+
agent.addPlan(plan);
|
|
243
|
+
await resolvedOptions.execute?.(plan.nextEvent);
|
|
244
|
+
}
|
|
234
245
|
}
|
|
235
246
|
return plan;
|
|
236
247
|
}
|
|
@@ -304,7 +315,7 @@ function getToolMap(_agent, input) {
|
|
|
304
315
|
return toolMap;
|
|
305
316
|
}
|
|
306
317
|
|
|
307
|
-
// src/planners/
|
|
318
|
+
// src/planners/simple.ts
|
|
308
319
|
var simplePlannerPromptTemplate = (data) => {
|
|
309
320
|
return `
|
|
310
321
|
${defaultTextTemplate(data)}
|
|
@@ -338,12 +349,13 @@ async function simplePlanner(agent, input) {
|
|
|
338
349
|
}) : void 0;
|
|
339
350
|
const result = await (0, import_ai3.generateText)({
|
|
340
351
|
...rest,
|
|
352
|
+
system: input.system ?? agent.description,
|
|
341
353
|
model,
|
|
342
354
|
messages,
|
|
343
355
|
tools: toolMap,
|
|
344
356
|
toolChoice: input.toolChoice ?? "required"
|
|
345
357
|
});
|
|
346
|
-
result.
|
|
358
|
+
result.response.messages.forEach((m) => {
|
|
347
359
|
const message = m;
|
|
348
360
|
agent.addMessage({
|
|
349
361
|
...message,
|
|
@@ -388,13 +400,11 @@ function createAgentMiddleware(agent) {
|
|
|
388
400
|
},
|
|
389
401
|
wrapGenerate: async ({ doGenerate, params }) => {
|
|
390
402
|
const id = randomId();
|
|
391
|
-
params.prompt.forEach((
|
|
403
|
+
params.prompt.forEach((message) => {
|
|
392
404
|
agent.addMessage({
|
|
393
405
|
id,
|
|
394
|
-
...
|
|
395
|
-
timestamp: Date.now()
|
|
396
|
-
correlationId: params.providerMetadata?.correlationId,
|
|
397
|
-
parentCorrelationId: params.providerMetadata?.parentCorrelationId
|
|
406
|
+
...message,
|
|
407
|
+
timestamp: Date.now()
|
|
398
408
|
});
|
|
399
409
|
});
|
|
400
410
|
const result = await doGenerate();
|
|
@@ -407,9 +417,7 @@ function createAgentMiddleware(agent) {
|
|
|
407
417
|
agent.addMessage({
|
|
408
418
|
id,
|
|
409
419
|
...message,
|
|
410
|
-
timestamp: Date.now()
|
|
411
|
-
correlationId: params.providerMetadata?.correlationId,
|
|
412
|
-
parentCorrelationId: params.providerMetadata?.parentCorrelationId
|
|
420
|
+
timestamp: Date.now()
|
|
413
421
|
});
|
|
414
422
|
});
|
|
415
423
|
const { stream, ...rest } = await doStream();
|
|
@@ -434,9 +442,7 @@ function createAgentMiddleware(agent) {
|
|
|
434
442
|
timestamp: Date.now(),
|
|
435
443
|
role: "assistant",
|
|
436
444
|
content,
|
|
437
|
-
responseId: id
|
|
438
|
-
correlationId: params.providerMetadata?.correlationId,
|
|
439
|
-
parentCorrelationId: params.providerMetadata?.parentCorrelationId
|
|
445
|
+
responseId: id
|
|
440
446
|
});
|
|
441
447
|
}
|
|
442
448
|
});
|
|
@@ -489,8 +495,10 @@ var agentLogic = (0, import_xstate4.fromTransition)(
|
|
|
489
495
|
});
|
|
490
496
|
break;
|
|
491
497
|
}
|
|
492
|
-
default:
|
|
498
|
+
default: {
|
|
499
|
+
console.warn("Unrecognized event", event);
|
|
493
500
|
break;
|
|
501
|
+
}
|
|
494
502
|
}
|
|
495
503
|
return state;
|
|
496
504
|
},
|
|
@@ -503,22 +511,17 @@ var agentLogic = (0, import_xstate4.fromTransition)(
|
|
|
503
511
|
);
|
|
504
512
|
function createAgent({
|
|
505
513
|
id,
|
|
506
|
-
name,
|
|
507
514
|
description,
|
|
508
515
|
model,
|
|
509
516
|
events,
|
|
510
517
|
context,
|
|
511
518
|
planner = simplePlanner,
|
|
512
|
-
|
|
513
|
-
getMemory,
|
|
514
|
-
logic = agentLogic,
|
|
515
|
-
...generateTextOptions
|
|
519
|
+
logic = agentLogic
|
|
516
520
|
}) {
|
|
517
521
|
return new Agent({
|
|
518
522
|
id,
|
|
519
523
|
context,
|
|
520
524
|
events,
|
|
521
|
-
name,
|
|
522
525
|
description,
|
|
523
526
|
planner,
|
|
524
527
|
model,
|
|
@@ -629,7 +632,8 @@ var Agent = class extends import_xstate4.Actor {
|
|
|
629
632
|
return this.getSnapshot().context.plans;
|
|
630
633
|
}
|
|
631
634
|
interact(actorRef, getInput) {
|
|
632
|
-
const actorRefCheck = isActorRef(actorRef);
|
|
635
|
+
const actorRefCheck = isActorRef(actorRef) && actorRef.src;
|
|
636
|
+
const machine = isMachineActor(actorRef) ? actorRef.src : void 0;
|
|
633
637
|
let prevState = void 0;
|
|
634
638
|
let subscribed = true;
|
|
635
639
|
const agent = this;
|
|
@@ -637,14 +641,14 @@ var Agent = class extends import_xstate4.Actor {
|
|
|
637
641
|
const observation = agent.addObservation(observationInput);
|
|
638
642
|
const input = getInput?.(observation);
|
|
639
643
|
if (input) {
|
|
640
|
-
await agentDecide(agent, {
|
|
641
|
-
machine
|
|
644
|
+
const res = await agentDecide(agent, {
|
|
645
|
+
machine,
|
|
642
646
|
state: observation.state,
|
|
643
|
-
execute: async (event) => {
|
|
644
|
-
actorRef.send(event);
|
|
645
|
-
},
|
|
646
647
|
...input
|
|
647
648
|
});
|
|
649
|
+
if (res?.nextEvent) {
|
|
650
|
+
actorRef.send(res.nextEvent);
|
|
651
|
+
}
|
|
648
652
|
}
|
|
649
653
|
prevState = observationInput.state;
|
|
650
654
|
}
|