@statelyai/agent 2.0.0-next.1 → 2.0.0-next.2
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/grumpy-dolphins-think.md +17 -0
- package/.changeset/old-teachers-tap.md +5 -0
- package/.changeset/pre.json +4 -1
- package/.changeset/smart-yaks-pull.md +23 -0
- package/CHANGELOG.md +40 -0
- package/dist/index.d.mts +55 -46
- package/dist/index.d.ts +55 -46
- package/dist/index.js +78 -50
- package/dist/index.mjs +81 -53
- package/examples/chatbot.ts +2 -2
- package/examples/cot.ts +6 -24
- package/examples/customer-service-sim.ts +3 -3
- package/examples/email.ts +9 -3
- package/examples/example.ts +2 -2
- package/examples/goal.ts +2 -2
- package/examples/joke.ts +2 -2
- package/examples/jugs.ts +4 -7
- package/examples/learn-from-feedback.ts +100 -0
- package/examples/number.ts +2 -2
- package/examples/raffle.ts +2 -2
- package/examples/river-crossing.ts +4 -7
- package/examples/simple.ts +13 -10
- package/examples/summary.ts +2 -5
- package/examples/support.ts +42 -38
- package/examples/ticTacToe.ts +46 -4
- package/examples/todo.ts +2 -2
- package/examples/tutor.ts +2 -2
- package/examples/verify.ts +2 -2
- package/examples/weather-agent.ts +141 -0
- package/examples/weather.ts +23 -23
- package/examples/word.ts +8 -6
- package/package.json +2 -1
- package/src/agent.test.ts +17 -15
- package/src/agent.ts +48 -35
- package/src/decide.test.ts +56 -8
- package/src/decide.ts +34 -24
- package/src/strategies/chainOfThought.ts +48 -0
- package/src/{planners → strategies}/shortestPath.test.ts +4 -7
- package/src/strategies/shortestPath.ts +173 -0
- package/src/{planners → strategies}/simple.ts +28 -18
- package/src/types.ts +62 -28
- package/src/utils.ts +12 -0
- package/src/planners/shortestPath.ts +0 -177
- package/src/strategies/chain-of-note.ts +0 -106
package/dist/index.d.ts
CHANGED
|
@@ -13,14 +13,14 @@ 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
|
|
16
|
+
type AgentDecideInput<TEvent extends EventObject> = Omit<AgentGenerateTextOptions, 'prompt' | 'tools'> & {
|
|
17
17
|
/**
|
|
18
18
|
* The currently observed state.
|
|
19
19
|
*/
|
|
20
20
|
state: ObservedState;
|
|
21
21
|
/**
|
|
22
22
|
* The goal for the agent to accomplish.
|
|
23
|
-
* The agent will
|
|
23
|
+
* The agent will make a decision based on this goal.
|
|
24
24
|
*/
|
|
25
25
|
goal: string;
|
|
26
26
|
/**
|
|
@@ -34,15 +34,15 @@ type AgentPlanInput<TEvent extends EventObject> = Omit<AgentGenerateTextOptions,
|
|
|
34
34
|
*/
|
|
35
35
|
machine?: AnyStateMachine;
|
|
36
36
|
/**
|
|
37
|
-
* The previous
|
|
37
|
+
* The previous decision made by the agent.
|
|
38
38
|
*/
|
|
39
|
-
|
|
39
|
+
prevDecision?: AgentDecision<TEvent>;
|
|
40
40
|
/**
|
|
41
41
|
* The total cost of the path to the goal state.
|
|
42
42
|
*/
|
|
43
43
|
costFunction?: CostFunction<TEvent>;
|
|
44
44
|
/**
|
|
45
|
-
* The maximum number of attempts to
|
|
45
|
+
* The maximum number of attempts to make a decision.
|
|
46
46
|
* Defaults to 2.
|
|
47
47
|
*/
|
|
48
48
|
maxAttempts?: number;
|
|
@@ -60,14 +60,14 @@ type AgentPath<TEvent extends EventObject> = {
|
|
|
60
60
|
steps: Array<AgentStep<TEvent>>;
|
|
61
61
|
weight?: number;
|
|
62
62
|
};
|
|
63
|
-
type
|
|
63
|
+
type AgentDecision<TEvent extends EventObject> = {
|
|
64
64
|
/**
|
|
65
|
-
* The
|
|
65
|
+
* The strategy used to generate the decision
|
|
66
66
|
*/
|
|
67
|
-
|
|
67
|
+
strategy: string;
|
|
68
68
|
goal: string;
|
|
69
69
|
/**
|
|
70
|
-
* The ending state of the
|
|
70
|
+
* The ending state of the decision.
|
|
71
71
|
*/
|
|
72
72
|
goalState: ObservedState | undefined;
|
|
73
73
|
/**
|
|
@@ -117,17 +117,20 @@ type PromptTemplate<TEvents extends EventObject> = (data: {
|
|
|
117
117
|
observations?: AgentObservation<any>[];
|
|
118
118
|
feedback?: AgentFeedback[];
|
|
119
119
|
messages?: AgentMessage[];
|
|
120
|
-
|
|
120
|
+
decisions?: AgentDecision<TEvents>[];
|
|
121
121
|
}) => string;
|
|
122
|
-
type
|
|
122
|
+
type AgentStrategy<T extends AnyAgent> = (agent: T, input: AgentDecideInput<EventsFromAgent<T>>) => Promise<AgentDecision<EventsFromAgent<T>> | undefined>;
|
|
123
|
+
type AgentInteractInput<T extends AnyAgent> = Omit<AgentDecideOptions<T>, 'state'>;
|
|
123
124
|
type AgentDecideOptions<T extends AnyAgent> = {
|
|
124
125
|
goal: string;
|
|
125
|
-
model?: LanguageModel;
|
|
126
126
|
state: ObservedState;
|
|
127
|
+
context?: Record<string, any>;
|
|
127
128
|
machine?: AnyStateMachine;
|
|
129
|
+
model?: LanguageModel;
|
|
128
130
|
execute?: (event: AnyEventObject) => Promise<void>;
|
|
129
|
-
|
|
131
|
+
strategy?: AgentStrategy<T>;
|
|
130
132
|
events?: ZodEventMapping;
|
|
133
|
+
allowedEvents?: Array<EventsFromAgent<T>['type']>;
|
|
131
134
|
/**
|
|
132
135
|
* The maximum number of times the agent will attempt to make a decision.
|
|
133
136
|
* Defaults to 2.
|
|
@@ -135,22 +138,20 @@ type AgentDecideOptions<T extends AnyAgent> = {
|
|
|
135
138
|
maxAttempts?: number;
|
|
136
139
|
} & Omit<Parameters<typeof generateText>[0], 'model' | 'tools' | 'prompt'>;
|
|
137
140
|
interface AgentFeedback {
|
|
138
|
-
goal
|
|
139
|
-
observationId
|
|
141
|
+
goal: string;
|
|
142
|
+
observationId: string;
|
|
140
143
|
/**
|
|
141
144
|
* The message correlation that the feedback is relevant for
|
|
142
145
|
*/
|
|
143
146
|
attributes: Record<string, any>;
|
|
144
|
-
reward: number;
|
|
145
147
|
timestamp: number;
|
|
146
148
|
episodeId: string;
|
|
147
149
|
}
|
|
148
150
|
interface AgentFeedbackInput {
|
|
149
|
-
goal
|
|
150
|
-
observationId
|
|
151
|
-
attributes
|
|
151
|
+
goal: string;
|
|
152
|
+
observationId: string;
|
|
153
|
+
attributes: Record<string, any>;
|
|
152
154
|
timestamp?: number;
|
|
153
|
-
reward?: number;
|
|
154
155
|
}
|
|
155
156
|
type AgentMessage = CoreMessage & {
|
|
156
157
|
timestamp: number;
|
|
@@ -235,7 +236,7 @@ type AgentDecisionInput = {
|
|
|
235
236
|
model?: LanguageModel;
|
|
236
237
|
context?: Record<string, any>;
|
|
237
238
|
} & Omit<Parameters<typeof generateText>[0], 'model' | 'tools' | 'prompt'>;
|
|
238
|
-
type AgentDecisionLogic<TEvents extends EventObject> = PromiseActorLogic<
|
|
239
|
+
type AgentDecisionLogic<TEvents extends EventObject> = PromiseActorLogic<AgentDecision<TEvents> | undefined, AgentDecisionInput | string>;
|
|
239
240
|
type AgentEmitted<TEvents extends EventObject> = {
|
|
240
241
|
type: 'feedback';
|
|
241
242
|
feedback: AgentFeedback;
|
|
@@ -246,8 +247,8 @@ type AgentEmitted<TEvents extends EventObject> = {
|
|
|
246
247
|
type: 'message';
|
|
247
248
|
message: AgentMessage;
|
|
248
249
|
} | {
|
|
249
|
-
type: '
|
|
250
|
-
|
|
250
|
+
type: 'decision';
|
|
251
|
+
decision: AgentDecision<TEvents>;
|
|
251
252
|
};
|
|
252
253
|
type AgentLogic<TEvents extends EventObject> = ActorLogic<TransitionSnapshot<AgentMemoryContext>, {
|
|
253
254
|
type: 'agent.feedback';
|
|
@@ -259,8 +260,8 @@ type AgentLogic<TEvents extends EventObject> = ActorLogic<TransitionSnapshot<Age
|
|
|
259
260
|
type: 'agent.message';
|
|
260
261
|
message: AgentMessage;
|
|
261
262
|
} | {
|
|
262
|
-
type: 'agent.
|
|
263
|
-
|
|
263
|
+
type: 'agent.decision';
|
|
264
|
+
decision: AgentDecision<TEvents>;
|
|
264
265
|
}, any, // TODO: input
|
|
265
266
|
any, AgentEmitted<TEvents>>;
|
|
266
267
|
type EventsFromZodEventMapping<TEventSchemas extends ZodEventMapping> = Values<{
|
|
@@ -297,7 +298,7 @@ type ObservedStateFrom<TActor extends ActorRefLike> = Pick<SnapshotFrom<TActor>,
|
|
|
297
298
|
type AgentMemoryContext = {
|
|
298
299
|
observations: AgentObservation<any>[];
|
|
299
300
|
messages: AgentMessage[];
|
|
300
|
-
|
|
301
|
+
decisions: AgentDecision<any>[];
|
|
301
302
|
feedback: AgentFeedback[];
|
|
302
303
|
};
|
|
303
304
|
interface AgentLongTermMemory {
|
|
@@ -308,8 +309,15 @@ interface AgentLongTermMemory {
|
|
|
308
309
|
type Compute<A extends any> = {
|
|
309
310
|
[K in keyof A]: A[K];
|
|
310
311
|
} & unknown;
|
|
312
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
313
|
+
type EventsFromAgent<T extends AnyAgent> = T extends Agent<infer _, infer __, infer TEvents, infer ___> ? TEvents : never;
|
|
314
|
+
type TypesFromAgent<T extends AnyAgent> = T extends Agent<infer TContextSchema, infer TEventSchema> ? {
|
|
315
|
+
context: ContextFromZodContextMapping<TContextSchema>;
|
|
316
|
+
events: EventsFromZodEventMapping<TEventSchema>;
|
|
317
|
+
} : never;
|
|
318
|
+
type ContextFromAgent<T extends AnyAgent> = T extends Agent<infer TContextSchema, infer _TEventSchema> ? ContextFromZodContextMapping<TContextSchema> : never;
|
|
311
319
|
|
|
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,
|
|
320
|
+
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, episodeId, strategy, logic, }: {
|
|
313
321
|
/**
|
|
314
322
|
* The unique identifier for the agent.
|
|
315
323
|
*
|
|
@@ -335,7 +343,7 @@ declare function createAgent<const TContextSchema extends ZodContextMapping, con
|
|
|
335
343
|
*/
|
|
336
344
|
events: TEventSchemas;
|
|
337
345
|
context?: TContextSchema;
|
|
338
|
-
|
|
346
|
+
strategy?: AgentStrategy<Agent<TContextSchema, TEventSchemas>>;
|
|
339
347
|
stringify?: typeof JSON.stringify;
|
|
340
348
|
/**
|
|
341
349
|
* A function that retrieves the agent's long term memory
|
|
@@ -346,7 +354,8 @@ declare function createAgent<const TContextSchema extends ZodContextMapping, con
|
|
|
346
354
|
*/
|
|
347
355
|
logic?: AgentLogic<TEvents>;
|
|
348
356
|
model: LanguageModel;
|
|
349
|
-
|
|
357
|
+
episodeId?: string;
|
|
358
|
+
}): Agent<TContextSchema, TEventSchemas>;
|
|
350
359
|
declare class Agent<const TContextSchema extends ZodContextMapping, const TEventSchemas extends ZodEventMapping, TEvents extends EventObject = EventsFromZodEventMapping<TEventSchemas>, TContext = ContextFromZodContextMapping<TContextSchema>> extends Actor<AgentLogic<TEvents>> {
|
|
351
360
|
/**
|
|
352
361
|
* The name of the agent. All agents with the same name are related and
|
|
@@ -360,15 +369,11 @@ declare class Agent<const TContextSchema extends ZodContextMapping, const TEvent
|
|
|
360
369
|
description?: string;
|
|
361
370
|
events: TEventSchemas;
|
|
362
371
|
context?: TContextSchema;
|
|
363
|
-
|
|
364
|
-
types: {
|
|
365
|
-
events: TEvents;
|
|
366
|
-
context: Compute<TContext>;
|
|
367
|
-
};
|
|
372
|
+
strategy: AgentStrategy<Agent<TContextSchema, TEventSchemas>>;
|
|
368
373
|
model: LanguageModel;
|
|
369
374
|
memory: AgentLongTermMemory | undefined;
|
|
370
375
|
defaultOptions: AgentDecideOptions<AnyAgent> | undefined;
|
|
371
|
-
constructor({ logic, id, name, description, model, events, context,
|
|
376
|
+
constructor({ logic, id, name, description, model, events, context, episodeId, strategy, }: {
|
|
372
377
|
logic: AgentLogic<TEvents>;
|
|
373
378
|
id?: string;
|
|
374
379
|
name?: string;
|
|
@@ -376,12 +381,17 @@ declare class Agent<const TContextSchema extends ZodContextMapping, const TEvent
|
|
|
376
381
|
model: GenerateTextOptions['model'];
|
|
377
382
|
events: TEventSchemas;
|
|
378
383
|
context?: TContextSchema;
|
|
379
|
-
|
|
384
|
+
strategy?: AgentStrategy<Agent<TContextSchema, TEventSchemas>>;
|
|
385
|
+
episodeId?: string;
|
|
380
386
|
});
|
|
381
387
|
/**
|
|
382
388
|
* Called whenever the agent (LLM assistant) receives or sends a message.
|
|
383
389
|
*/
|
|
384
390
|
onMessage(fn: (message: AgentMessage) => void): Subscription;
|
|
391
|
+
/**
|
|
392
|
+
* Called whenever the agent (LLM assistant) receives some feedback.
|
|
393
|
+
*/
|
|
394
|
+
onFeedback(fn: (feedback: AgentFeedback) => void): Subscription;
|
|
385
395
|
/**
|
|
386
396
|
* Retrieves messages from the agent's short-term (local) memory.
|
|
387
397
|
*/
|
|
@@ -427,11 +437,10 @@ declare class Agent<const TContextSchema extends ZodContextMapping, const TEvent
|
|
|
427
437
|
attributes: {
|
|
428
438
|
[x: string]: any;
|
|
429
439
|
};
|
|
430
|
-
reward: number;
|
|
431
440
|
timestamp: number;
|
|
432
441
|
episodeId: string;
|
|
433
|
-
goal
|
|
434
|
-
observationId
|
|
442
|
+
goal: string;
|
|
443
|
+
observationId: string;
|
|
435
444
|
};
|
|
436
445
|
/**
|
|
437
446
|
* Retrieves feedback from the agent's short-term (local) memory.
|
|
@@ -442,11 +451,11 @@ declare class Agent<const TContextSchema extends ZodContextMapping, const TEvent
|
|
|
442
451
|
* Retrieves observations from the agent's short-term (local) memory.
|
|
443
452
|
*/
|
|
444
453
|
getObservations(): AgentObservation<any>[];
|
|
445
|
-
|
|
454
|
+
addDecision(decision: AgentDecision<TEvents>): void;
|
|
446
455
|
/**
|
|
447
456
|
* Retrieves strategies from the agent's short-term (local) memory.
|
|
448
457
|
*/
|
|
449
|
-
|
|
458
|
+
getDecisions(): AgentDecision<any>[];
|
|
450
459
|
/**
|
|
451
460
|
* Interacts with this state machine actor by inspecting state transitions and storing them as observations.
|
|
452
461
|
*
|
|
@@ -491,18 +500,18 @@ declare class Agent<const TContextSchema extends ZodContextMapping, const TEvent
|
|
|
491
500
|
* actor.start();
|
|
492
501
|
* ```
|
|
493
502
|
*/
|
|
494
|
-
interact<TActor extends ActorRefLike>(actorRef: TActor, getInput: (observation: AgentObservation<TActor>) =>
|
|
503
|
+
interact<TActor extends ActorRefLike>(actorRef: TActor, getInput: (observation: AgentObservation<TActor>) => AgentInteractInput<this> | void): Subscription;
|
|
495
504
|
observe<TActor extends ActorRefLike>(actorRef: TActor): Subscription;
|
|
496
505
|
wrap(modelToWrap: LanguageModelV1): LanguageModelV1;
|
|
497
506
|
/**
|
|
498
|
-
* Resolves with an `
|
|
507
|
+
* Resolves with an `AgentDecision` based on the information provided in the `options`, including:
|
|
499
508
|
*
|
|
500
509
|
* - The `goal` for the agent to achieve
|
|
501
510
|
* - The observed current `state`
|
|
502
511
|
* - The `machine` (e.g. a state machine) that specifies what can happen next
|
|
503
512
|
* - Additional `context`
|
|
504
513
|
*/
|
|
505
|
-
decide(opts: AgentDecideOptions<this>): Promise<
|
|
514
|
+
decide(opts: AgentDecideOptions<this>): Promise<AgentDecision<EventsFromAgent<this>> | undefined>;
|
|
506
515
|
}
|
|
507
516
|
|
|
508
517
|
declare function fromTextStream<T extends AnyAgent>(agent: T, options?: AgentStreamTextOptions): ObservableActorLogic<{
|
|
@@ -514,6 +523,6 @@ declare function fromText<T extends AnyAgent>(agent: T, options?: AgentGenerateT
|
|
|
514
523
|
context?: AgentGenerateTextOptions['context'];
|
|
515
524
|
}>;
|
|
516
525
|
|
|
517
|
-
declare function fromDecision(agent:
|
|
526
|
+
declare function fromDecision<T extends AnyAgent>(agent: T, defaultInput?: AgentDecideInput<EventsFromAgent<T>>): AgentDecisionLogic<any>;
|
|
518
527
|
|
|
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
|
|
528
|
+
export { type AgentDecideInput, type AgentDecideOptions, type AgentDecision, type AgentDecisionInput, type AgentDecisionLogic, type AgentEmitted, type AgentFeedback, type AgentFeedbackInput, type AgentGenerateTextOptions, type AgentInteractInput, type AgentLogic, type AgentLongTermMemory, type AgentMemoryContext, type AgentMessage, type AgentMessageInput, type AgentObservation, type AgentObservationInput, type AgentPath, type AgentStep, type AgentStrategy, type AgentStreamTextOptions, type AnyAgent, type CommonTextOptions, type Compute, type ContextFromAgent, type ContextFromZodContextMapping, type CostFunction, type EventsFromAgent, type EventsFromZodEventMapping, type FromAgent, type GenerateTextOptions, type LanguageModelV1TextPart, type LanguageModelV1ToolCallPart, type MaybePromise, type ObservedState, type ObservedStateFrom, type PromptTemplate, type StreamTextOptions, type TransitionData, type TypesFromAgent, createAgent, fromDecision, fromText, fromTextStream };
|
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/
|
|
43
|
+
// src/strategies/simple.ts
|
|
44
44
|
var import_ai3 = require("ai");
|
|
45
45
|
|
|
46
46
|
// src/utils.ts
|
|
@@ -79,6 +79,15 @@ function getAllMachineTransitions(stateNode) {
|
|
|
79
79
|
function wrapInXml(tagName, content) {
|
|
80
80
|
return `<${tagName}>${content}</${tagName}>`;
|
|
81
81
|
}
|
|
82
|
+
function convertToXml(obj) {
|
|
83
|
+
return Object.entries(obj).map(([key, value]) => {
|
|
84
|
+
if (typeof value === "object" && value !== null) {
|
|
85
|
+
return wrapInXml(key, convertToXml(value));
|
|
86
|
+
} else {
|
|
87
|
+
return wrapInXml(key, value);
|
|
88
|
+
}
|
|
89
|
+
}).join("");
|
|
90
|
+
}
|
|
82
91
|
function randomId(prefix) {
|
|
83
92
|
const timestamp = Date.now().toString(36);
|
|
84
93
|
const random = Math.random().toString(36).substring(2, 9);
|
|
@@ -110,9 +119,12 @@ function isMachineActor(actor) {
|
|
|
110
119
|
return "src" in actor && typeof actor.src === "object" && actor.src !== null && "definition" in actor.src;
|
|
111
120
|
}
|
|
112
121
|
|
|
113
|
-
// src/
|
|
122
|
+
// src/strategies/simple.ts
|
|
114
123
|
var import_xstate3 = require("xstate");
|
|
115
124
|
|
|
125
|
+
// src/text.ts
|
|
126
|
+
var import_ai = require("ai");
|
|
127
|
+
|
|
116
128
|
// src/templates/defaultText.ts
|
|
117
129
|
var defaultTextTemplate = (data) => {
|
|
118
130
|
const preamble = [
|
|
@@ -126,7 +138,6 @@ ${data.goal}
|
|
|
126
138
|
};
|
|
127
139
|
|
|
128
140
|
// src/text.ts
|
|
129
|
-
var import_ai = require("ai");
|
|
130
141
|
var import_xstate = require("xstate");
|
|
131
142
|
async function getMessages(agent, prompt, options) {
|
|
132
143
|
let messages = [];
|
|
@@ -215,35 +226,42 @@ async function agentDecide(agent, options) {
|
|
|
215
226
|
...options
|
|
216
227
|
};
|
|
217
228
|
const {
|
|
218
|
-
|
|
229
|
+
strategy = agent.strategy,
|
|
219
230
|
goal,
|
|
231
|
+
allowedEvents,
|
|
220
232
|
events = agent.events,
|
|
221
233
|
state,
|
|
222
234
|
machine,
|
|
223
235
|
model = agent.model,
|
|
224
236
|
messages,
|
|
225
|
-
...
|
|
237
|
+
...otherDecideInput
|
|
226
238
|
} = resolvedOptions;
|
|
239
|
+
const filteredEventSchemas = allowedEvents ? Object.fromEntries(
|
|
240
|
+
Object.entries(events).filter(([key]) => {
|
|
241
|
+
return allowedEvents.includes(key);
|
|
242
|
+
})
|
|
243
|
+
) : events;
|
|
227
244
|
let attempts = 0;
|
|
228
245
|
const maxAttempts = resolvedOptions.maxAttempts ?? 2;
|
|
229
|
-
let
|
|
246
|
+
let decision;
|
|
230
247
|
while (attempts++ < maxAttempts) {
|
|
231
|
-
|
|
248
|
+
decision = await strategy(agent, {
|
|
232
249
|
model,
|
|
233
250
|
goal,
|
|
234
|
-
events,
|
|
251
|
+
events: filteredEventSchemas,
|
|
235
252
|
state,
|
|
236
253
|
machine,
|
|
237
254
|
messages,
|
|
238
255
|
// TODO: fix UIMessage thing
|
|
239
|
-
...
|
|
256
|
+
...otherDecideInput
|
|
240
257
|
});
|
|
241
|
-
if (
|
|
242
|
-
agent.
|
|
243
|
-
await resolvedOptions.execute?.(
|
|
258
|
+
if (decision?.nextEvent) {
|
|
259
|
+
agent.addDecision(decision);
|
|
260
|
+
await resolvedOptions.execute?.(decision.nextEvent);
|
|
261
|
+
break;
|
|
244
262
|
}
|
|
245
263
|
}
|
|
246
|
-
return
|
|
264
|
+
return decision;
|
|
247
265
|
}
|
|
248
266
|
function fromDecision(agent, defaultInput) {
|
|
249
267
|
return (0, import_xstate2.fromPromise)(async ({ input, self }) => {
|
|
@@ -261,15 +279,18 @@ function fromDecision(agent, defaultInput) {
|
|
|
261
279
|
value: snapshot.value,
|
|
262
280
|
context: resolvedInput.context
|
|
263
281
|
};
|
|
264
|
-
const
|
|
282
|
+
const decision = await agentDecide(agent, {
|
|
265
283
|
machine: parentRef.logic,
|
|
266
|
-
state,
|
|
284
|
+
state: snapshot,
|
|
285
|
+
context: resolvedInput.context,
|
|
267
286
|
execute: async (event) => {
|
|
268
287
|
parentRef.send(event);
|
|
269
288
|
},
|
|
270
|
-
...resolvedInput
|
|
289
|
+
...resolvedInput,
|
|
290
|
+
// @ts-ignore
|
|
291
|
+
messages: resolvedInput.messages
|
|
271
292
|
});
|
|
272
|
-
return
|
|
293
|
+
return decision;
|
|
273
294
|
});
|
|
274
295
|
}
|
|
275
296
|
function getToolMap(_agent, input) {
|
|
@@ -315,37 +336,38 @@ function getToolMap(_agent, input) {
|
|
|
315
336
|
return toolMap;
|
|
316
337
|
}
|
|
317
338
|
|
|
318
|
-
// src/
|
|
319
|
-
var
|
|
339
|
+
// src/strategies/simple.ts
|
|
340
|
+
var simpleStrategyPromptTemplate = (data) => {
|
|
320
341
|
return `
|
|
321
|
-
${
|
|
342
|
+
${convertToXml(data)}
|
|
322
343
|
|
|
323
344
|
Make at most one tool call to achieve the above goal. If the goal cannot be achieved with any tool calls, do not make any tool call.
|
|
324
345
|
`.trim();
|
|
325
346
|
};
|
|
326
|
-
async function
|
|
347
|
+
async function simpleStrategy(agent, input) {
|
|
327
348
|
const toolMap = getToolMap(agent, input);
|
|
328
349
|
if (!toolMap) {
|
|
329
350
|
return void 0;
|
|
330
351
|
}
|
|
331
|
-
const prompt =
|
|
332
|
-
context: input.
|
|
352
|
+
const prompt = simpleStrategyPromptTemplate({
|
|
353
|
+
context: input.context,
|
|
333
354
|
goal: input.goal
|
|
334
355
|
});
|
|
335
356
|
const messages = await getMessages(agent, prompt, input);
|
|
336
357
|
const model = input.model ? agent.wrap(input.model) : agent.model;
|
|
337
358
|
const {
|
|
338
359
|
state,
|
|
360
|
+
context,
|
|
339
361
|
machine,
|
|
340
|
-
|
|
362
|
+
prevDecision,
|
|
341
363
|
events,
|
|
342
364
|
goal,
|
|
343
365
|
model: _,
|
|
344
366
|
...rest
|
|
345
367
|
} = input;
|
|
346
|
-
const machineState = input.machine ? input.machine.resolveState({
|
|
368
|
+
const machineState = input.machine && input.state ? input.machine.resolveState({
|
|
347
369
|
...input.state,
|
|
348
|
-
context: input.state.context
|
|
370
|
+
context: input.state.context ?? {}
|
|
349
371
|
}) : void 0;
|
|
350
372
|
const result = await (0, import_ai3.generateText)({
|
|
351
373
|
...rest,
|
|
@@ -369,7 +391,7 @@ async function simplePlanner(agent, input) {
|
|
|
369
391
|
return void 0;
|
|
370
392
|
}
|
|
371
393
|
return {
|
|
372
|
-
|
|
394
|
+
strategy: "simple",
|
|
373
395
|
goal: input.goal,
|
|
374
396
|
goalState: input.state,
|
|
375
397
|
nextEvent: singleResult.result,
|
|
@@ -486,12 +508,11 @@ var agentLogic = (0, import_xstate4.fromTransition)(
|
|
|
486
508
|
});
|
|
487
509
|
break;
|
|
488
510
|
}
|
|
489
|
-
case "agent.
|
|
490
|
-
state.
|
|
511
|
+
case "agent.decision": {
|
|
512
|
+
state.decisions.push(event.decision);
|
|
491
513
|
emit({
|
|
492
|
-
type: "
|
|
493
|
-
|
|
494
|
-
plan: event.plan
|
|
514
|
+
type: "decision",
|
|
515
|
+
decision: event.decision
|
|
495
516
|
});
|
|
496
517
|
break;
|
|
497
518
|
}
|
|
@@ -506,7 +527,7 @@ var agentLogic = (0, import_xstate4.fromTransition)(
|
|
|
506
527
|
feedback: [],
|
|
507
528
|
messages: [],
|
|
508
529
|
observations: [],
|
|
509
|
-
|
|
530
|
+
decisions: []
|
|
510
531
|
})
|
|
511
532
|
);
|
|
512
533
|
function createAgent({
|
|
@@ -515,7 +536,8 @@ function createAgent({
|
|
|
515
536
|
model,
|
|
516
537
|
events,
|
|
517
538
|
context,
|
|
518
|
-
|
|
539
|
+
episodeId,
|
|
540
|
+
strategy = simpleStrategy,
|
|
519
541
|
logic = agentLogic
|
|
520
542
|
}) {
|
|
521
543
|
return new Agent({
|
|
@@ -523,9 +545,10 @@ function createAgent({
|
|
|
523
545
|
context,
|
|
524
546
|
events,
|
|
525
547
|
description,
|
|
526
|
-
|
|
548
|
+
strategy,
|
|
527
549
|
model,
|
|
528
|
-
logic
|
|
550
|
+
logic,
|
|
551
|
+
episodeId
|
|
529
552
|
});
|
|
530
553
|
}
|
|
531
554
|
var Agent = class extends import_xstate4.Actor {
|
|
@@ -538,17 +561,18 @@ var Agent = class extends import_xstate4.Actor {
|
|
|
538
561
|
model,
|
|
539
562
|
events,
|
|
540
563
|
context,
|
|
541
|
-
|
|
564
|
+
episodeId,
|
|
565
|
+
strategy = simpleStrategy
|
|
542
566
|
}) {
|
|
543
567
|
super(logic);
|
|
544
568
|
this.model = model;
|
|
545
|
-
this.episodeId =
|
|
569
|
+
this.episodeId = episodeId ?? randomId("episode-");
|
|
546
570
|
this.name = name;
|
|
547
571
|
this.description = description;
|
|
548
572
|
this.events = events;
|
|
549
573
|
this.context = context;
|
|
550
|
-
this.
|
|
551
|
-
this.
|
|
574
|
+
this.strategy = strategy;
|
|
575
|
+
this.id = id ?? randomId();
|
|
552
576
|
this.start();
|
|
553
577
|
}
|
|
554
578
|
/**
|
|
@@ -557,6 +581,12 @@ var Agent = class extends import_xstate4.Actor {
|
|
|
557
581
|
onMessage(fn) {
|
|
558
582
|
return this.on("message", (ev) => fn(ev.message));
|
|
559
583
|
}
|
|
584
|
+
/**
|
|
585
|
+
* Called whenever the agent (LLM assistant) receives some feedback.
|
|
586
|
+
*/
|
|
587
|
+
onFeedback(fn) {
|
|
588
|
+
return this.on("feedback", (ev) => fn(ev.feedback));
|
|
589
|
+
}
|
|
560
590
|
/**
|
|
561
591
|
* Retrieves messages from the agent's short-term (local) memory.
|
|
562
592
|
*/
|
|
@@ -580,7 +610,6 @@ var Agent = class extends import_xstate4.Actor {
|
|
|
580
610
|
const feedback = {
|
|
581
611
|
...feedbackInput,
|
|
582
612
|
attributes: { ...feedbackInput.attributes },
|
|
583
|
-
reward: feedbackInput.reward ?? 0,
|
|
584
613
|
timestamp: feedbackInput.timestamp ?? Date.now(),
|
|
585
614
|
episodeId: this.episodeId
|
|
586
615
|
};
|
|
@@ -619,17 +648,17 @@ var Agent = class extends import_xstate4.Actor {
|
|
|
619
648
|
getObservations() {
|
|
620
649
|
return this.getSnapshot().context.observations;
|
|
621
650
|
}
|
|
622
|
-
|
|
651
|
+
addDecision(decision) {
|
|
623
652
|
this.send({
|
|
624
|
-
type: "agent.
|
|
625
|
-
|
|
653
|
+
type: "agent.decision",
|
|
654
|
+
decision
|
|
626
655
|
});
|
|
627
656
|
}
|
|
628
657
|
/**
|
|
629
658
|
* Retrieves strategies from the agent's short-term (local) memory.
|
|
630
659
|
*/
|
|
631
|
-
|
|
632
|
-
return this.getSnapshot().context.
|
|
660
|
+
getDecisions() {
|
|
661
|
+
return this.getSnapshot().context.decisions;
|
|
633
662
|
}
|
|
634
663
|
interact(actorRef, getInput) {
|
|
635
664
|
const actorRefCheck = isActorRef(actorRef) && actorRef.src;
|
|
@@ -669,8 +698,7 @@ var Agent = class extends import_xstate4.Actor {
|
|
|
669
698
|
if (actorRef._processingStatus === 1) {
|
|
670
699
|
handleObservation({
|
|
671
700
|
prevState: void 0,
|
|
672
|
-
event:
|
|
673
|
-
// TODO: unknown events?
|
|
701
|
+
event: void 0,
|
|
674
702
|
state: actorRef.getSnapshot(),
|
|
675
703
|
machine: actorRef.src
|
|
676
704
|
});
|
|
@@ -710,14 +738,14 @@ var Agent = class extends import_xstate4.Actor {
|
|
|
710
738
|
});
|
|
711
739
|
}
|
|
712
740
|
/**
|
|
713
|
-
* Resolves with an `
|
|
741
|
+
* Resolves with an `AgentDecision` based on the information provided in the `options`, including:
|
|
714
742
|
*
|
|
715
743
|
* - The `goal` for the agent to achieve
|
|
716
744
|
* - The observed current `state`
|
|
717
745
|
* - The `machine` (e.g. a state machine) that specifies what can happen next
|
|
718
746
|
* - Additional `context`
|
|
719
747
|
*/
|
|
720
|
-
decide(opts) {
|
|
748
|
+
async decide(opts) {
|
|
721
749
|
return agentDecide(this, opts);
|
|
722
750
|
}
|
|
723
751
|
};
|