@statelyai/agent 1.1.5 → 2.0.0-alpha.10
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/LICENSE +21 -0
- package/dist/ai-sdk.cjs +342 -0
- package/dist/ai-sdk.d.cts +206 -0
- package/dist/ai-sdk.d.mts +206 -0
- package/dist/ai-sdk.mjs +331 -0
- package/dist/cli.cjs +64 -0
- package/dist/cli.d.cts +1 -0
- package/dist/cli.d.mts +1 -0
- package/dist/cli.mjs +65 -0
- package/dist/decision-CQdrKc8k.mjs +978 -0
- package/dist/decision-b-lkcs4L.cjs +1295 -0
- package/dist/index.cjs +59 -0
- package/dist/index.d.cts +212 -0
- package/dist/index.d.mts +201 -417
- package/dist/index.mjs +4 -583
- package/dist/openai-compat.cjs +319 -0
- package/dist/openai-compat.d.cts +98 -0
- package/dist/openai-compat.d.mts +98 -0
- package/dist/openai-compat.mjs +312 -0
- package/dist/run-agent-BFMkuI1F.cjs +1599 -0
- package/dist/run-agent-BVMd--1l.d.cts +1103 -0
- package/dist/run-agent-BoAOq_0Z.mjs +1540 -0
- package/dist/run-agent-DzYJb3CK.d.mts +1103 -0
- package/dist/src-D-77Ha5p.cjs +1087 -0
- package/dist/src-DUeIFjv-.mjs +1016 -0
- package/dist/text-logic-C7WJpCIc.d.mts +710 -0
- package/dist/text-logic-CZjyACzQ.d.cts +710 -0
- package/dist/types-C9QiMjre.d.cts +219 -0
- package/dist/types-qm00QF91.d.mts +219 -0
- package/dist/utils-Dri7aeEG.d.cts +108 -0
- package/dist/utils-Y6GDRGGE.d.mts +108 -0
- package/dist/zod.cjs +31 -0
- package/dist/zod.d.cts +30 -0
- package/dist/zod.d.mts +30 -0
- package/dist/zod.mjs +30 -0
- package/package.json +110 -29
- package/readme.md +143 -6
- package/schemas/agent-workflow.json +527 -0
- package/.changeset/README.md +0 -8
- package/.changeset/config.json +0 -11
- package/.env.template +0 -3
- package/.github/actions/ci-setup/action.yml +0 -24
- package/.github/workflows/release.yml +0 -46
- package/.vscode/launch.json +0 -28
- package/CHANGELOG.md +0 -215
- package/dist/index.d.ts +0 -428
- package/dist/index.js +0 -616
- package/examples/chatbot.ts +0 -71
- package/examples/cot.ts +0 -89
- package/examples/email.ts +0 -118
- package/examples/example.ts +0 -81
- package/examples/goal.ts +0 -94
- package/examples/helpers/helpers.ts +0 -17
- package/examples/helpers/loader.ts +0 -32
- package/examples/helpers/runner.ts +0 -27
- package/examples/joke.ts +0 -225
- package/examples/multi.ts +0 -103
- package/examples/newspaper.ts +0 -324
- package/examples/number.ts +0 -102
- package/examples/raffle.ts +0 -105
- package/examples/sandbox.ts +0 -28
- package/examples/simple.ts +0 -39
- package/examples/support.ts +0 -147
- package/examples/ticTacToe.ts +0 -224
- package/examples/todo.ts +0 -137
- package/examples/tutor.ts +0 -100
- package/examples/verify.ts +0 -120
- package/examples/weather.ts +0 -178
- package/examples/wiki.ts +0 -30
- package/examples/word.ts +0 -171
- package/src/adapters/vercel.ts +0 -7
- package/src/agent-experimental.ts +0 -221
- package/src/agent.test.ts +0 -506
- package/src/agent.ts +0 -300
- package/src/decision.test.ts +0 -179
- package/src/decision.ts +0 -84
- package/src/index.ts +0 -4
- package/src/memory.ts +0 -25
- package/src/planners/shortestPathPlanner.ts +0 -22
- package/src/planners/simplePlanner.ts +0 -139
- package/src/schemas.ts +0 -11
- package/src/strategies/chain-of-note.ts +0 -155
- package/src/templates/defaultText.ts +0 -18
- package/src/text.ts +0 -236
- package/src/types.ts +0 -499
- package/src/utils.ts +0 -72
- package/tsconfig.json +0 -109
- package/vitest.config.ts +0 -9
package/dist/index.mjs
CHANGED
|
@@ -1,583 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
} from "xstate";
|
|
6
|
-
|
|
7
|
-
// src/planners/simplePlanner.ts
|
|
8
|
-
import { tool } from "ai";
|
|
9
|
-
|
|
10
|
-
// src/utils.ts
|
|
11
|
-
import hash from "object-hash";
|
|
12
|
-
function getAllTransitions(state) {
|
|
13
|
-
const nodes = state._nodes;
|
|
14
|
-
const transitions = nodes.map((node) => [...node.transitions.values()]).map((nodeTransitions) => {
|
|
15
|
-
return nodeTransitions.map((nodeEventTransitions) => {
|
|
16
|
-
return nodeEventTransitions.map((transition) => {
|
|
17
|
-
return {
|
|
18
|
-
...transition,
|
|
19
|
-
guard: typeof transition.guard === "string" ? { type: transition.guard } : transition.guard
|
|
20
|
-
// TODO: fix
|
|
21
|
-
};
|
|
22
|
-
});
|
|
23
|
-
});
|
|
24
|
-
}).flat(2);
|
|
25
|
-
return transitions;
|
|
26
|
-
}
|
|
27
|
-
function getAllMachineTransitions(stateNode) {
|
|
28
|
-
const transitions = [...stateNode.transitions.values()].map((nodeTransitions) => {
|
|
29
|
-
return nodeTransitions.map((transition) => {
|
|
30
|
-
return {
|
|
31
|
-
...transition,
|
|
32
|
-
guard: typeof transition.guard === "string" ? { type: transition.guard } : transition.guard
|
|
33
|
-
// TODO: fix
|
|
34
|
-
};
|
|
35
|
-
});
|
|
36
|
-
}).flat(2);
|
|
37
|
-
for (const s of Object.values(stateNode.states)) {
|
|
38
|
-
const stateTransitions = getAllMachineTransitions(s);
|
|
39
|
-
transitions.push(...stateTransitions);
|
|
40
|
-
}
|
|
41
|
-
return transitions;
|
|
42
|
-
}
|
|
43
|
-
function wrapInXml(tagName, content) {
|
|
44
|
-
return `<${tagName}>${content}</${tagName}>`;
|
|
45
|
-
}
|
|
46
|
-
function randomId() {
|
|
47
|
-
const timestamp = Date.now().toString(36);
|
|
48
|
-
const random = Math.random().toString(36).substring(2, 9);
|
|
49
|
-
return timestamp + random;
|
|
50
|
-
}
|
|
51
|
-
var machineHashes = /* @__PURE__ */ new WeakMap();
|
|
52
|
-
function getMachineHash(machine) {
|
|
53
|
-
if (machineHashes.has(machine)) return machineHashes.get(machine);
|
|
54
|
-
const transitions = getAllMachineTransitions(machine.root);
|
|
55
|
-
const machineHash = hash(transitions);
|
|
56
|
-
machineHashes.set(machine, machineHash);
|
|
57
|
-
return machineHash;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// src/templates/defaultText.ts
|
|
61
|
-
var defaultTextTemplate = (data) => {
|
|
62
|
-
const preamble = [
|
|
63
|
-
data.context ? wrapInXml("context", JSON.stringify(data.context)) : void 0
|
|
64
|
-
].filter(Boolean).join("\n");
|
|
65
|
-
return `
|
|
66
|
-
${preamble}
|
|
67
|
-
|
|
68
|
-
${data.goal}
|
|
69
|
-
`.trim();
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
// src/text.ts
|
|
73
|
-
import {
|
|
74
|
-
fromObservable,
|
|
75
|
-
fromPromise,
|
|
76
|
-
toObserver
|
|
77
|
-
} from "xstate";
|
|
78
|
-
async function getMessages(agent, prompt, options) {
|
|
79
|
-
let messages = [];
|
|
80
|
-
if (typeof options.messages === "function") {
|
|
81
|
-
messages = await options.messages(agent);
|
|
82
|
-
} else if (options.messages) {
|
|
83
|
-
messages = options.messages;
|
|
84
|
-
}
|
|
85
|
-
messages = messages.concat({
|
|
86
|
-
role: "user",
|
|
87
|
-
content: prompt
|
|
88
|
-
});
|
|
89
|
-
return messages;
|
|
90
|
-
}
|
|
91
|
-
async function agentGenerateText(agent, options) {
|
|
92
|
-
const resolvedOptions = {
|
|
93
|
-
...agent.defaultOptions,
|
|
94
|
-
...options,
|
|
95
|
-
correlationId: options.correlationId ?? randomId()
|
|
96
|
-
};
|
|
97
|
-
const template = resolvedOptions.template ?? defaultTextTemplate;
|
|
98
|
-
const id = randomId();
|
|
99
|
-
const goal = typeof resolvedOptions.prompt === "string" ? resolvedOptions.prompt : await resolvedOptions.prompt(agent);
|
|
100
|
-
const promptWithContext = template({
|
|
101
|
-
goal,
|
|
102
|
-
context: resolvedOptions.context
|
|
103
|
-
});
|
|
104
|
-
const messages = await getMessages(agent, promptWithContext, resolvedOptions);
|
|
105
|
-
agent.addMessage({
|
|
106
|
-
id,
|
|
107
|
-
role: "user",
|
|
108
|
-
content: promptWithContext,
|
|
109
|
-
timestamp: Date.now(),
|
|
110
|
-
correlationId: resolvedOptions.correlationId,
|
|
111
|
-
parentCorrelationId: resolvedOptions.parentCorrelationId
|
|
112
|
-
});
|
|
113
|
-
const result = await agent.adapter.generateText({
|
|
114
|
-
...resolvedOptions,
|
|
115
|
-
prompt: void 0,
|
|
116
|
-
messages
|
|
117
|
-
});
|
|
118
|
-
agent.addMessage({
|
|
119
|
-
content: result.text,
|
|
120
|
-
id,
|
|
121
|
-
role: "assistant",
|
|
122
|
-
timestamp: Date.now(),
|
|
123
|
-
responseId: id,
|
|
124
|
-
result,
|
|
125
|
-
correlationId: resolvedOptions.correlationId,
|
|
126
|
-
parentCorrelationId: resolvedOptions.parentCorrelationId
|
|
127
|
-
});
|
|
128
|
-
return {
|
|
129
|
-
...result,
|
|
130
|
-
parentCorrelationId: resolvedOptions.parentCorrelationId,
|
|
131
|
-
correlationId: resolvedOptions.correlationId
|
|
132
|
-
};
|
|
133
|
-
}
|
|
134
|
-
async function agentStreamText(agent, options) {
|
|
135
|
-
const resolvedOptions = {
|
|
136
|
-
...agent.defaultOptions,
|
|
137
|
-
...options,
|
|
138
|
-
correlationId: options.correlationId ?? randomId()
|
|
139
|
-
};
|
|
140
|
-
const template = resolvedOptions.template ?? defaultTextTemplate;
|
|
141
|
-
const id = randomId();
|
|
142
|
-
const goal = typeof resolvedOptions.prompt === "string" ? resolvedOptions.prompt : await resolvedOptions.prompt(agent);
|
|
143
|
-
const promptWithContext = template({
|
|
144
|
-
goal,
|
|
145
|
-
context: resolvedOptions.context
|
|
146
|
-
});
|
|
147
|
-
const messages = await getMessages(agent, promptWithContext, resolvedOptions);
|
|
148
|
-
agent.addMessage({
|
|
149
|
-
role: "user",
|
|
150
|
-
content: promptWithContext,
|
|
151
|
-
id,
|
|
152
|
-
timestamp: Date.now(),
|
|
153
|
-
correlationId: resolvedOptions.correlationId,
|
|
154
|
-
parentCorrelationId: resolvedOptions.parentCorrelationId
|
|
155
|
-
});
|
|
156
|
-
const result = await agent.adapter.streamText({
|
|
157
|
-
...resolvedOptions,
|
|
158
|
-
prompt: void 0,
|
|
159
|
-
messages,
|
|
160
|
-
onFinish: async (res) => {
|
|
161
|
-
agent.addMessage({
|
|
162
|
-
role: "assistant",
|
|
163
|
-
result: {
|
|
164
|
-
text: res.text,
|
|
165
|
-
finishReason: res.finishReason,
|
|
166
|
-
logprobs: void 0,
|
|
167
|
-
responseMessages: [],
|
|
168
|
-
toolCalls: [],
|
|
169
|
-
toolResults: [],
|
|
170
|
-
usage: res.usage,
|
|
171
|
-
warnings: res.warnings,
|
|
172
|
-
rawResponse: res.rawResponse,
|
|
173
|
-
roundtrips: []
|
|
174
|
-
// TODO: how do we get this information?
|
|
175
|
-
},
|
|
176
|
-
content: res.text,
|
|
177
|
-
id: randomId(),
|
|
178
|
-
timestamp: Date.now(),
|
|
179
|
-
responseId: id,
|
|
180
|
-
correlationId: resolvedOptions.correlationId,
|
|
181
|
-
parentCorrelationId: resolvedOptions.parentCorrelationId
|
|
182
|
-
});
|
|
183
|
-
}
|
|
184
|
-
});
|
|
185
|
-
return {
|
|
186
|
-
...result,
|
|
187
|
-
parentCorrelationId: resolvedOptions.parentCorrelationId,
|
|
188
|
-
correlationId: resolvedOptions.correlationId
|
|
189
|
-
};
|
|
190
|
-
}
|
|
191
|
-
function fromTextStream(agent, defaultOptions) {
|
|
192
|
-
return fromObservable(({ input }) => {
|
|
193
|
-
const observers = /* @__PURE__ */ new Set();
|
|
194
|
-
(async () => {
|
|
195
|
-
const result = await agentStreamText(agent, {
|
|
196
|
-
...defaultOptions,
|
|
197
|
-
...input,
|
|
198
|
-
context: input.context
|
|
199
|
-
});
|
|
200
|
-
for await (const part of result.fullStream) {
|
|
201
|
-
if (part.type === "text-delta") {
|
|
202
|
-
observers.forEach((observer) => {
|
|
203
|
-
observer.next?.(part);
|
|
204
|
-
});
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
})();
|
|
208
|
-
return {
|
|
209
|
-
subscribe: (...args) => {
|
|
210
|
-
const observer = toObserver(...args);
|
|
211
|
-
observers.add(observer);
|
|
212
|
-
return {
|
|
213
|
-
unsubscribe: () => {
|
|
214
|
-
observers.delete(observer);
|
|
215
|
-
}
|
|
216
|
-
};
|
|
217
|
-
}
|
|
218
|
-
};
|
|
219
|
-
});
|
|
220
|
-
}
|
|
221
|
-
function fromText(agent, defaultOptions) {
|
|
222
|
-
return fromPromise(async ({ input }) => {
|
|
223
|
-
return await agentGenerateText(agent, {
|
|
224
|
-
...input,
|
|
225
|
-
...defaultOptions,
|
|
226
|
-
context: input.context
|
|
227
|
-
});
|
|
228
|
-
});
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
// src/planners/simplePlanner.ts
|
|
232
|
-
function getTransitions(state, machine) {
|
|
233
|
-
if (!machine) {
|
|
234
|
-
return [];
|
|
235
|
-
}
|
|
236
|
-
const resolvedState = machine.resolveState(state);
|
|
237
|
-
return getAllTransitions(resolvedState);
|
|
238
|
-
}
|
|
239
|
-
var simplePlannerPromptTemplate = (data) => {
|
|
240
|
-
return `
|
|
241
|
-
${defaultTextTemplate(data)}
|
|
242
|
-
|
|
243
|
-
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.
|
|
244
|
-
`.trim();
|
|
245
|
-
};
|
|
246
|
-
async function simplePlanner(agent, input) {
|
|
247
|
-
const transitions = input.machine ? getTransitions(input.state, input.machine) : Object.entries(input.events).map(([eventType, { description }]) => ({
|
|
248
|
-
eventType,
|
|
249
|
-
description
|
|
250
|
-
}));
|
|
251
|
-
const filter = (eventType) => Object.keys(input.events).includes(eventType);
|
|
252
|
-
const functionNameMapping = {};
|
|
253
|
-
const toolTransitions = transitions.filter((t) => {
|
|
254
|
-
return filter(t.eventType);
|
|
255
|
-
}).map((t) => {
|
|
256
|
-
const name = t.eventType.replace(/\./g, "_");
|
|
257
|
-
functionNameMapping[name] = t.eventType;
|
|
258
|
-
return {
|
|
259
|
-
type: "function",
|
|
260
|
-
eventType: t.eventType,
|
|
261
|
-
description: t.description,
|
|
262
|
-
name
|
|
263
|
-
};
|
|
264
|
-
});
|
|
265
|
-
const toolMap = {};
|
|
266
|
-
for (const toolTransitionData of toolTransitions) {
|
|
267
|
-
const toolZodType = input.events?.[toolTransitionData.eventType];
|
|
268
|
-
if (!toolZodType) {
|
|
269
|
-
continue;
|
|
270
|
-
}
|
|
271
|
-
toolMap[toolTransitionData.name] = tool({
|
|
272
|
-
description: toolZodType?.description ?? toolTransitionData.description,
|
|
273
|
-
parameters: toolZodType,
|
|
274
|
-
execute: async (params) => {
|
|
275
|
-
const event = {
|
|
276
|
-
type: toolTransitionData.eventType,
|
|
277
|
-
...params
|
|
278
|
-
};
|
|
279
|
-
return event;
|
|
280
|
-
}
|
|
281
|
-
});
|
|
282
|
-
}
|
|
283
|
-
if (!Object.keys(toolMap).length) {
|
|
284
|
-
return void 0;
|
|
285
|
-
}
|
|
286
|
-
const prompt = simplePlannerPromptTemplate({
|
|
287
|
-
context: input.state.context,
|
|
288
|
-
goal: input.goal
|
|
289
|
-
});
|
|
290
|
-
const messages = await getMessages(agent, prompt, input);
|
|
291
|
-
const result = await agent.generateText({
|
|
292
|
-
toolChoice: "required",
|
|
293
|
-
...input,
|
|
294
|
-
prompt,
|
|
295
|
-
messages,
|
|
296
|
-
tools: toolMap
|
|
297
|
-
});
|
|
298
|
-
const singleResult = result.toolResults[0];
|
|
299
|
-
if (!singleResult) {
|
|
300
|
-
console.warn("No tool call results returned");
|
|
301
|
-
return void 0;
|
|
302
|
-
}
|
|
303
|
-
return {
|
|
304
|
-
goal: input.goal,
|
|
305
|
-
state: input.state,
|
|
306
|
-
execute: async (state) => {
|
|
307
|
-
if (JSON.stringify(state) === JSON.stringify(input.state)) {
|
|
308
|
-
return singleResult.result;
|
|
309
|
-
}
|
|
310
|
-
return void 0;
|
|
311
|
-
},
|
|
312
|
-
nextEvent: singleResult.result,
|
|
313
|
-
sessionId: agent.sessionId,
|
|
314
|
-
timestamp: Date.now()
|
|
315
|
-
};
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
// src/decision.ts
|
|
319
|
-
import { fromPromise as fromPromise2 } from "xstate";
|
|
320
|
-
async function agentDecide(agent, options) {
|
|
321
|
-
const resolvedOptions = {
|
|
322
|
-
...agent.defaultOptions,
|
|
323
|
-
...options
|
|
324
|
-
};
|
|
325
|
-
const {
|
|
326
|
-
planner = simplePlanner,
|
|
327
|
-
goal,
|
|
328
|
-
events = agent.events,
|
|
329
|
-
state,
|
|
330
|
-
machine,
|
|
331
|
-
model = agent.model,
|
|
332
|
-
...otherPlanInput
|
|
333
|
-
} = resolvedOptions;
|
|
334
|
-
const plan = await planner(agent, {
|
|
335
|
-
model,
|
|
336
|
-
goal,
|
|
337
|
-
events,
|
|
338
|
-
state,
|
|
339
|
-
machine,
|
|
340
|
-
...otherPlanInput
|
|
341
|
-
});
|
|
342
|
-
if (plan?.nextEvent) {
|
|
343
|
-
agent.addPlan(plan);
|
|
344
|
-
await resolvedOptions.execute?.(plan.nextEvent);
|
|
345
|
-
}
|
|
346
|
-
return plan;
|
|
347
|
-
}
|
|
348
|
-
function fromDecision(agent, defaultInput) {
|
|
349
|
-
return fromPromise2(async ({ input, self }) => {
|
|
350
|
-
const parentRef = self._parent;
|
|
351
|
-
if (!parentRef) {
|
|
352
|
-
return;
|
|
353
|
-
}
|
|
354
|
-
const snapshot = parentRef.getSnapshot();
|
|
355
|
-
const inputObject = typeof input === "string" ? { goal: input } : input;
|
|
356
|
-
const resolvedInput = {
|
|
357
|
-
...defaultInput,
|
|
358
|
-
...inputObject
|
|
359
|
-
};
|
|
360
|
-
const contextToInclude = resolvedInput.context === true ? (
|
|
361
|
-
// include entire context
|
|
362
|
-
parentRef.getSnapshot().context
|
|
363
|
-
) : resolvedInput.context;
|
|
364
|
-
const state = {
|
|
365
|
-
value: snapshot.value,
|
|
366
|
-
context: contextToInclude
|
|
367
|
-
};
|
|
368
|
-
const plan = await agentDecide(agent, {
|
|
369
|
-
machine: parentRef.logic,
|
|
370
|
-
state,
|
|
371
|
-
execute: async (event) => {
|
|
372
|
-
parentRef.send(event);
|
|
373
|
-
},
|
|
374
|
-
...resolvedInput
|
|
375
|
-
});
|
|
376
|
-
return plan;
|
|
377
|
-
});
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
// src/adapters/vercel.ts
|
|
381
|
-
import { generateText, streamText } from "ai";
|
|
382
|
-
var vercelAdapter = {
|
|
383
|
-
generateText,
|
|
384
|
-
streamText
|
|
385
|
-
};
|
|
386
|
-
|
|
387
|
-
// src/agent.ts
|
|
388
|
-
var agentLogic = fromTransition(
|
|
389
|
-
(state, event, { emit }) => {
|
|
390
|
-
switch (event.type) {
|
|
391
|
-
case "agent.feedback": {
|
|
392
|
-
state.feedback.push(event.feedback);
|
|
393
|
-
emit({
|
|
394
|
-
type: "feedback",
|
|
395
|
-
// @ts-ignore TODO: fix types in XState
|
|
396
|
-
feedback: event.feedback
|
|
397
|
-
});
|
|
398
|
-
break;
|
|
399
|
-
}
|
|
400
|
-
case "agent.observe": {
|
|
401
|
-
state.observations.push(event.observation);
|
|
402
|
-
emit({
|
|
403
|
-
type: "observation",
|
|
404
|
-
// @ts-ignore TODO: fix types in XState
|
|
405
|
-
observation: event.observation
|
|
406
|
-
});
|
|
407
|
-
break;
|
|
408
|
-
}
|
|
409
|
-
case "agent.message": {
|
|
410
|
-
state.messages.push(event.message);
|
|
411
|
-
emit({
|
|
412
|
-
type: "message",
|
|
413
|
-
// @ts-ignore TODO: fix types in XState
|
|
414
|
-
message: event.message
|
|
415
|
-
});
|
|
416
|
-
break;
|
|
417
|
-
}
|
|
418
|
-
case "agent.plan": {
|
|
419
|
-
state.plans.push(event.plan);
|
|
420
|
-
emit({
|
|
421
|
-
type: "plan",
|
|
422
|
-
// @ts-ignore TODO: fix types in XState
|
|
423
|
-
plan: event.plan
|
|
424
|
-
});
|
|
425
|
-
break;
|
|
426
|
-
}
|
|
427
|
-
default:
|
|
428
|
-
break;
|
|
429
|
-
}
|
|
430
|
-
return state;
|
|
431
|
-
},
|
|
432
|
-
() => ({
|
|
433
|
-
feedback: [],
|
|
434
|
-
messages: [],
|
|
435
|
-
observations: [],
|
|
436
|
-
plans: []
|
|
437
|
-
})
|
|
438
|
-
);
|
|
439
|
-
function createAgent({
|
|
440
|
-
name,
|
|
441
|
-
description,
|
|
442
|
-
model,
|
|
443
|
-
events,
|
|
444
|
-
context,
|
|
445
|
-
planner = simplePlanner,
|
|
446
|
-
stringify = JSON.stringify,
|
|
447
|
-
getMemory,
|
|
448
|
-
logic = agentLogic,
|
|
449
|
-
adapter = vercelAdapter,
|
|
450
|
-
...generateTextOptions
|
|
451
|
-
}) {
|
|
452
|
-
const agent = createActor(logic);
|
|
453
|
-
agent.events = events;
|
|
454
|
-
agent.model = model;
|
|
455
|
-
agent.name = name;
|
|
456
|
-
agent.description = description;
|
|
457
|
-
agent.adapter = adapter;
|
|
458
|
-
agent.defaultOptions = { ...generateTextOptions, model };
|
|
459
|
-
agent.select = (selector) => {
|
|
460
|
-
return selector(agent.getSnapshot().context);
|
|
461
|
-
};
|
|
462
|
-
agent.memory = getMemory ? getMemory(agent) : void 0;
|
|
463
|
-
agent.onMessage = (callback) => {
|
|
464
|
-
agent.on("message", (ev) => callback(ev.message));
|
|
465
|
-
};
|
|
466
|
-
agent.decide = (opts) => {
|
|
467
|
-
return agentDecide(agent, opts);
|
|
468
|
-
};
|
|
469
|
-
agent.addMessage = (messageInput) => {
|
|
470
|
-
const message = {
|
|
471
|
-
...messageInput,
|
|
472
|
-
id: messageInput.id ?? randomId(),
|
|
473
|
-
timestamp: messageInput.timestamp ?? Date.now(),
|
|
474
|
-
sessionId: agent.sessionId,
|
|
475
|
-
correlationId: messageInput.correlationId ?? randomId()
|
|
476
|
-
};
|
|
477
|
-
agent.send({
|
|
478
|
-
type: "agent.message",
|
|
479
|
-
message
|
|
480
|
-
});
|
|
481
|
-
return message;
|
|
482
|
-
};
|
|
483
|
-
agent.getMessages = () => agent.getSnapshot().context.messages;
|
|
484
|
-
agent.generateText = (opts) => agentGenerateText(agent, opts);
|
|
485
|
-
agent.streamText = (opts) => agentStreamText(agent, opts);
|
|
486
|
-
agent.addFeedback = (feedbackInput) => {
|
|
487
|
-
const feedback = {
|
|
488
|
-
...feedbackInput,
|
|
489
|
-
attributes: { ...feedbackInput.attributes },
|
|
490
|
-
reward: feedbackInput.reward ?? 0,
|
|
491
|
-
timestamp: feedbackInput.timestamp ?? Date.now(),
|
|
492
|
-
sessionId: agent.sessionId
|
|
493
|
-
};
|
|
494
|
-
agent.send({
|
|
495
|
-
type: "agent.feedback",
|
|
496
|
-
feedback
|
|
497
|
-
});
|
|
498
|
-
return feedback;
|
|
499
|
-
};
|
|
500
|
-
agent.getFeedback = () => agent.getSnapshot().context.feedback;
|
|
501
|
-
agent.addObservation = (observationInput) => {
|
|
502
|
-
const { prevState, event, state } = observationInput;
|
|
503
|
-
const observation = {
|
|
504
|
-
prevState,
|
|
505
|
-
event,
|
|
506
|
-
state,
|
|
507
|
-
id: observationInput.id ?? randomId(),
|
|
508
|
-
sessionId: agent.sessionId,
|
|
509
|
-
timestamp: observationInput.timestamp ?? Date.now(),
|
|
510
|
-
machineHash: observationInput.machine ? getMachineHash(observationInput.machine) : void 0
|
|
511
|
-
};
|
|
512
|
-
agent.send({
|
|
513
|
-
type: "agent.observe",
|
|
514
|
-
observation
|
|
515
|
-
});
|
|
516
|
-
return observation;
|
|
517
|
-
};
|
|
518
|
-
agent.getObservations = () => agent.getSnapshot().context.observations;
|
|
519
|
-
agent.addPlan = (plan) => {
|
|
520
|
-
agent.send({
|
|
521
|
-
type: "agent.plan",
|
|
522
|
-
plan
|
|
523
|
-
});
|
|
524
|
-
};
|
|
525
|
-
agent.getPlans = () => agent.getSnapshot().context.plans;
|
|
526
|
-
agent.interact = (actorRef, getInput) => {
|
|
527
|
-
let prevState = void 0;
|
|
528
|
-
let subscribed = true;
|
|
529
|
-
async function handleObservation(observationInput) {
|
|
530
|
-
const observation = agent.addObservation(observationInput);
|
|
531
|
-
const input = getInput?.(observation);
|
|
532
|
-
if (input) {
|
|
533
|
-
await agentDecide(agent, {
|
|
534
|
-
machine: actorRef.src,
|
|
535
|
-
state: observation.state,
|
|
536
|
-
execute: async (event) => {
|
|
537
|
-
actorRef.send(event);
|
|
538
|
-
},
|
|
539
|
-
...input
|
|
540
|
-
});
|
|
541
|
-
}
|
|
542
|
-
prevState = observationInput.state;
|
|
543
|
-
}
|
|
544
|
-
actorRef.system.inspect({
|
|
545
|
-
next: async (inspEvent) => {
|
|
546
|
-
if (!subscribed || inspEvent.actorRef !== actorRef || inspEvent.type !== "@xstate.snapshot") {
|
|
547
|
-
return;
|
|
548
|
-
}
|
|
549
|
-
const observationInput = {
|
|
550
|
-
event: inspEvent.event,
|
|
551
|
-
prevState,
|
|
552
|
-
state: inspEvent.snapshot,
|
|
553
|
-
machine: actorRef.src
|
|
554
|
-
};
|
|
555
|
-
await handleObservation(observationInput);
|
|
556
|
-
}
|
|
557
|
-
});
|
|
558
|
-
if (actorRef._processingStatus === 1) {
|
|
559
|
-
handleObservation({
|
|
560
|
-
prevState: void 0,
|
|
561
|
-
event: { type: "" },
|
|
562
|
-
// TODO: unknown events?
|
|
563
|
-
state: actorRef.getSnapshot(),
|
|
564
|
-
machine: actorRef.src
|
|
565
|
-
});
|
|
566
|
-
}
|
|
567
|
-
return {
|
|
568
|
-
unsubscribe: () => {
|
|
569
|
-
subscribed = false;
|
|
570
|
-
}
|
|
571
|
-
// TODO: make this actually unsubscribe
|
|
572
|
-
};
|
|
573
|
-
};
|
|
574
|
-
agent.types = {};
|
|
575
|
-
agent.start();
|
|
576
|
-
return agent;
|
|
577
|
-
}
|
|
578
|
-
export {
|
|
579
|
-
createAgent,
|
|
580
|
-
fromDecision,
|
|
581
|
-
fromText,
|
|
582
|
-
fromTextStream
|
|
583
|
-
};
|
|
1
|
+
import { a as runAgent, c as setupAgent, i as inspectTransitions, l as appendMessages, n as IllegalResumeEventError, o as runAgentToCompletion, r as SnapshotVersionMismatchError, s as createAgentSchemas, t as AgentIdleError, u as messagesSchema } from "./run-agent-BoAOq_0Z.mjs";
|
|
2
|
+
import { $ as validateSchemaSync, A as parseOutput, C as createTextLogic, E as isStructuredOutputSchema, G as getJsonSchemaSync, J as isStandardSchema, K as getMachineStructuralHash, Q as userMessage, T as getAgentOutputMode, U as getAgentMessages, V as assistantMessage, W as getJsonSchema, X as systemMessage, Y as persistSnapshot, Z as toolMessage, b as bindRequestExecutor, d as EVENT_TOOL_PREFIX, f as getAcceptedEvents, j as parseStructuredEnvelope, k as parseModelRef, l as renderDecisionAttempts, m as parseAgentEvent, n as PLAN_DONE_EVENT_TYPE, p as matchesEventPattern, q as getStateMeta, t as DecisionExhaustedError, u as resolveDecision, x as buildEnvelopeSchema } from "./decision-CQdrKc8k.mjs";
|
|
3
|
+
import { a as lintAgentMachine, c as getAgentRequests, d as resolveAgentStep, f as transitionAgentStep, i as explorePaths, l as initialAgentStep, n as assertAgentMachine, o as simulateAgent, r as canReach, s as executeAgentRequest, t as AgentLintError, u as resolveAgentRequests } from "./src-DUeIFjv-.mjs";
|
|
4
|
+
export { AgentIdleError, AgentLintError, DecisionExhaustedError, EVENT_TOOL_PREFIX, IllegalResumeEventError, PLAN_DONE_EVENT_TYPE, SnapshotVersionMismatchError, appendMessages, assertAgentMachine, assistantMessage, bindRequestExecutor, buildEnvelopeSchema, canReach, createAgentSchemas, createTextLogic, executeAgentRequest, explorePaths, getAcceptedEvents, getAgentMessages, getAgentOutputMode, getAgentRequests, getJsonSchema, getJsonSchemaSync, getMachineStructuralHash, getStateMeta, initialAgentStep, inspectTransitions, isStandardSchema, isStructuredOutputSchema, lintAgentMachine, matchesEventPattern, messagesSchema, parseAgentEvent, parseModelRef, parseOutput, parseStructuredEnvelope, persistSnapshot, renderDecisionAttempts, resolveAgentRequests, resolveAgentStep, resolveDecision, runAgent, runAgentToCompletion, setupAgent, simulateAgent, systemMessage, toolMessage, transitionAgentStep, userMessage, validateSchemaSync };
|