@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.js
DELETED
|
@@ -1,616 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
-
|
|
30
|
-
// src/index.ts
|
|
31
|
-
var src_exports = {};
|
|
32
|
-
__export(src_exports, {
|
|
33
|
-
createAgent: () => createAgent,
|
|
34
|
-
fromDecision: () => fromDecision,
|
|
35
|
-
fromText: () => fromText,
|
|
36
|
-
fromTextStream: () => fromTextStream
|
|
37
|
-
});
|
|
38
|
-
module.exports = __toCommonJS(src_exports);
|
|
39
|
-
|
|
40
|
-
// src/agent.ts
|
|
41
|
-
var import_xstate3 = require("xstate");
|
|
42
|
-
|
|
43
|
-
// src/planners/simplePlanner.ts
|
|
44
|
-
var import_ai = require("ai");
|
|
45
|
-
|
|
46
|
-
// src/utils.ts
|
|
47
|
-
var import_object_hash = __toESM(require("object-hash"));
|
|
48
|
-
function getAllTransitions(state) {
|
|
49
|
-
const nodes = state._nodes;
|
|
50
|
-
const transitions = nodes.map((node) => [...node.transitions.values()]).map((nodeTransitions) => {
|
|
51
|
-
return nodeTransitions.map((nodeEventTransitions) => {
|
|
52
|
-
return nodeEventTransitions.map((transition) => {
|
|
53
|
-
return {
|
|
54
|
-
...transition,
|
|
55
|
-
guard: typeof transition.guard === "string" ? { type: transition.guard } : transition.guard
|
|
56
|
-
// TODO: fix
|
|
57
|
-
};
|
|
58
|
-
});
|
|
59
|
-
});
|
|
60
|
-
}).flat(2);
|
|
61
|
-
return transitions;
|
|
62
|
-
}
|
|
63
|
-
function getAllMachineTransitions(stateNode) {
|
|
64
|
-
const transitions = [...stateNode.transitions.values()].map((nodeTransitions) => {
|
|
65
|
-
return nodeTransitions.map((transition) => {
|
|
66
|
-
return {
|
|
67
|
-
...transition,
|
|
68
|
-
guard: typeof transition.guard === "string" ? { type: transition.guard } : transition.guard
|
|
69
|
-
// TODO: fix
|
|
70
|
-
};
|
|
71
|
-
});
|
|
72
|
-
}).flat(2);
|
|
73
|
-
for (const s of Object.values(stateNode.states)) {
|
|
74
|
-
const stateTransitions = getAllMachineTransitions(s);
|
|
75
|
-
transitions.push(...stateTransitions);
|
|
76
|
-
}
|
|
77
|
-
return transitions;
|
|
78
|
-
}
|
|
79
|
-
function wrapInXml(tagName, content) {
|
|
80
|
-
return `<${tagName}>${content}</${tagName}>`;
|
|
81
|
-
}
|
|
82
|
-
function randomId() {
|
|
83
|
-
const timestamp = Date.now().toString(36);
|
|
84
|
-
const random = Math.random().toString(36).substring(2, 9);
|
|
85
|
-
return timestamp + random;
|
|
86
|
-
}
|
|
87
|
-
var machineHashes = /* @__PURE__ */ new WeakMap();
|
|
88
|
-
function getMachineHash(machine) {
|
|
89
|
-
if (machineHashes.has(machine)) return machineHashes.get(machine);
|
|
90
|
-
const transitions = getAllMachineTransitions(machine.root);
|
|
91
|
-
const machineHash = (0, import_object_hash.default)(transitions);
|
|
92
|
-
machineHashes.set(machine, machineHash);
|
|
93
|
-
return machineHash;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
// src/templates/defaultText.ts
|
|
97
|
-
var defaultTextTemplate = (data) => {
|
|
98
|
-
const preamble = [
|
|
99
|
-
data.context ? wrapInXml("context", JSON.stringify(data.context)) : void 0
|
|
100
|
-
].filter(Boolean).join("\n");
|
|
101
|
-
return `
|
|
102
|
-
${preamble}
|
|
103
|
-
|
|
104
|
-
${data.goal}
|
|
105
|
-
`.trim();
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
// src/text.ts
|
|
109
|
-
var import_xstate = require("xstate");
|
|
110
|
-
async function getMessages(agent, prompt, options) {
|
|
111
|
-
let messages = [];
|
|
112
|
-
if (typeof options.messages === "function") {
|
|
113
|
-
messages = await options.messages(agent);
|
|
114
|
-
} else if (options.messages) {
|
|
115
|
-
messages = options.messages;
|
|
116
|
-
}
|
|
117
|
-
messages = messages.concat({
|
|
118
|
-
role: "user",
|
|
119
|
-
content: prompt
|
|
120
|
-
});
|
|
121
|
-
return messages;
|
|
122
|
-
}
|
|
123
|
-
async function agentGenerateText(agent, options) {
|
|
124
|
-
const resolvedOptions = {
|
|
125
|
-
...agent.defaultOptions,
|
|
126
|
-
...options,
|
|
127
|
-
correlationId: options.correlationId ?? randomId()
|
|
128
|
-
};
|
|
129
|
-
const template = resolvedOptions.template ?? defaultTextTemplate;
|
|
130
|
-
const id = randomId();
|
|
131
|
-
const goal = typeof resolvedOptions.prompt === "string" ? resolvedOptions.prompt : await resolvedOptions.prompt(agent);
|
|
132
|
-
const promptWithContext = template({
|
|
133
|
-
goal,
|
|
134
|
-
context: resolvedOptions.context
|
|
135
|
-
});
|
|
136
|
-
const messages = await getMessages(agent, promptWithContext, resolvedOptions);
|
|
137
|
-
agent.addMessage({
|
|
138
|
-
id,
|
|
139
|
-
role: "user",
|
|
140
|
-
content: promptWithContext,
|
|
141
|
-
timestamp: Date.now(),
|
|
142
|
-
correlationId: resolvedOptions.correlationId,
|
|
143
|
-
parentCorrelationId: resolvedOptions.parentCorrelationId
|
|
144
|
-
});
|
|
145
|
-
const result = await agent.adapter.generateText({
|
|
146
|
-
...resolvedOptions,
|
|
147
|
-
prompt: void 0,
|
|
148
|
-
messages
|
|
149
|
-
});
|
|
150
|
-
agent.addMessage({
|
|
151
|
-
content: result.text,
|
|
152
|
-
id,
|
|
153
|
-
role: "assistant",
|
|
154
|
-
timestamp: Date.now(),
|
|
155
|
-
responseId: id,
|
|
156
|
-
result,
|
|
157
|
-
correlationId: resolvedOptions.correlationId,
|
|
158
|
-
parentCorrelationId: resolvedOptions.parentCorrelationId
|
|
159
|
-
});
|
|
160
|
-
return {
|
|
161
|
-
...result,
|
|
162
|
-
parentCorrelationId: resolvedOptions.parentCorrelationId,
|
|
163
|
-
correlationId: resolvedOptions.correlationId
|
|
164
|
-
};
|
|
165
|
-
}
|
|
166
|
-
async function agentStreamText(agent, options) {
|
|
167
|
-
const resolvedOptions = {
|
|
168
|
-
...agent.defaultOptions,
|
|
169
|
-
...options,
|
|
170
|
-
correlationId: options.correlationId ?? randomId()
|
|
171
|
-
};
|
|
172
|
-
const template = resolvedOptions.template ?? defaultTextTemplate;
|
|
173
|
-
const id = randomId();
|
|
174
|
-
const goal = typeof resolvedOptions.prompt === "string" ? resolvedOptions.prompt : await resolvedOptions.prompt(agent);
|
|
175
|
-
const promptWithContext = template({
|
|
176
|
-
goal,
|
|
177
|
-
context: resolvedOptions.context
|
|
178
|
-
});
|
|
179
|
-
const messages = await getMessages(agent, promptWithContext, resolvedOptions);
|
|
180
|
-
agent.addMessage({
|
|
181
|
-
role: "user",
|
|
182
|
-
content: promptWithContext,
|
|
183
|
-
id,
|
|
184
|
-
timestamp: Date.now(),
|
|
185
|
-
correlationId: resolvedOptions.correlationId,
|
|
186
|
-
parentCorrelationId: resolvedOptions.parentCorrelationId
|
|
187
|
-
});
|
|
188
|
-
const result = await agent.adapter.streamText({
|
|
189
|
-
...resolvedOptions,
|
|
190
|
-
prompt: void 0,
|
|
191
|
-
messages,
|
|
192
|
-
onFinish: async (res) => {
|
|
193
|
-
agent.addMessage({
|
|
194
|
-
role: "assistant",
|
|
195
|
-
result: {
|
|
196
|
-
text: res.text,
|
|
197
|
-
finishReason: res.finishReason,
|
|
198
|
-
logprobs: void 0,
|
|
199
|
-
responseMessages: [],
|
|
200
|
-
toolCalls: [],
|
|
201
|
-
toolResults: [],
|
|
202
|
-
usage: res.usage,
|
|
203
|
-
warnings: res.warnings,
|
|
204
|
-
rawResponse: res.rawResponse,
|
|
205
|
-
roundtrips: []
|
|
206
|
-
// TODO: how do we get this information?
|
|
207
|
-
},
|
|
208
|
-
content: res.text,
|
|
209
|
-
id: randomId(),
|
|
210
|
-
timestamp: Date.now(),
|
|
211
|
-
responseId: id,
|
|
212
|
-
correlationId: resolvedOptions.correlationId,
|
|
213
|
-
parentCorrelationId: resolvedOptions.parentCorrelationId
|
|
214
|
-
});
|
|
215
|
-
}
|
|
216
|
-
});
|
|
217
|
-
return {
|
|
218
|
-
...result,
|
|
219
|
-
parentCorrelationId: resolvedOptions.parentCorrelationId,
|
|
220
|
-
correlationId: resolvedOptions.correlationId
|
|
221
|
-
};
|
|
222
|
-
}
|
|
223
|
-
function fromTextStream(agent, defaultOptions) {
|
|
224
|
-
return (0, import_xstate.fromObservable)(({ input }) => {
|
|
225
|
-
const observers = /* @__PURE__ */ new Set();
|
|
226
|
-
(async () => {
|
|
227
|
-
const result = await agentStreamText(agent, {
|
|
228
|
-
...defaultOptions,
|
|
229
|
-
...input,
|
|
230
|
-
context: input.context
|
|
231
|
-
});
|
|
232
|
-
for await (const part of result.fullStream) {
|
|
233
|
-
if (part.type === "text-delta") {
|
|
234
|
-
observers.forEach((observer) => {
|
|
235
|
-
observer.next?.(part);
|
|
236
|
-
});
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
})();
|
|
240
|
-
return {
|
|
241
|
-
subscribe: (...args) => {
|
|
242
|
-
const observer = (0, import_xstate.toObserver)(...args);
|
|
243
|
-
observers.add(observer);
|
|
244
|
-
return {
|
|
245
|
-
unsubscribe: () => {
|
|
246
|
-
observers.delete(observer);
|
|
247
|
-
}
|
|
248
|
-
};
|
|
249
|
-
}
|
|
250
|
-
};
|
|
251
|
-
});
|
|
252
|
-
}
|
|
253
|
-
function fromText(agent, defaultOptions) {
|
|
254
|
-
return (0, import_xstate.fromPromise)(async ({ input }) => {
|
|
255
|
-
return await agentGenerateText(agent, {
|
|
256
|
-
...input,
|
|
257
|
-
...defaultOptions,
|
|
258
|
-
context: input.context
|
|
259
|
-
});
|
|
260
|
-
});
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
// src/planners/simplePlanner.ts
|
|
264
|
-
function getTransitions(state, machine) {
|
|
265
|
-
if (!machine) {
|
|
266
|
-
return [];
|
|
267
|
-
}
|
|
268
|
-
const resolvedState = machine.resolveState(state);
|
|
269
|
-
return getAllTransitions(resolvedState);
|
|
270
|
-
}
|
|
271
|
-
var simplePlannerPromptTemplate = (data) => {
|
|
272
|
-
return `
|
|
273
|
-
${defaultTextTemplate(data)}
|
|
274
|
-
|
|
275
|
-
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.
|
|
276
|
-
`.trim();
|
|
277
|
-
};
|
|
278
|
-
async function simplePlanner(agent, input) {
|
|
279
|
-
const transitions = input.machine ? getTransitions(input.state, input.machine) : Object.entries(input.events).map(([eventType, { description }]) => ({
|
|
280
|
-
eventType,
|
|
281
|
-
description
|
|
282
|
-
}));
|
|
283
|
-
const filter = (eventType) => Object.keys(input.events).includes(eventType);
|
|
284
|
-
const functionNameMapping = {};
|
|
285
|
-
const toolTransitions = transitions.filter((t) => {
|
|
286
|
-
return filter(t.eventType);
|
|
287
|
-
}).map((t) => {
|
|
288
|
-
const name = t.eventType.replace(/\./g, "_");
|
|
289
|
-
functionNameMapping[name] = t.eventType;
|
|
290
|
-
return {
|
|
291
|
-
type: "function",
|
|
292
|
-
eventType: t.eventType,
|
|
293
|
-
description: t.description,
|
|
294
|
-
name
|
|
295
|
-
};
|
|
296
|
-
});
|
|
297
|
-
const toolMap = {};
|
|
298
|
-
for (const toolTransitionData of toolTransitions) {
|
|
299
|
-
const toolZodType = input.events?.[toolTransitionData.eventType];
|
|
300
|
-
if (!toolZodType) {
|
|
301
|
-
continue;
|
|
302
|
-
}
|
|
303
|
-
toolMap[toolTransitionData.name] = (0, import_ai.tool)({
|
|
304
|
-
description: toolZodType?.description ?? toolTransitionData.description,
|
|
305
|
-
parameters: toolZodType,
|
|
306
|
-
execute: async (params) => {
|
|
307
|
-
const event = {
|
|
308
|
-
type: toolTransitionData.eventType,
|
|
309
|
-
...params
|
|
310
|
-
};
|
|
311
|
-
return event;
|
|
312
|
-
}
|
|
313
|
-
});
|
|
314
|
-
}
|
|
315
|
-
if (!Object.keys(toolMap).length) {
|
|
316
|
-
return void 0;
|
|
317
|
-
}
|
|
318
|
-
const prompt = simplePlannerPromptTemplate({
|
|
319
|
-
context: input.state.context,
|
|
320
|
-
goal: input.goal
|
|
321
|
-
});
|
|
322
|
-
const messages = await getMessages(agent, prompt, input);
|
|
323
|
-
const result = await agent.generateText({
|
|
324
|
-
toolChoice: "required",
|
|
325
|
-
...input,
|
|
326
|
-
prompt,
|
|
327
|
-
messages,
|
|
328
|
-
tools: toolMap
|
|
329
|
-
});
|
|
330
|
-
const singleResult = result.toolResults[0];
|
|
331
|
-
if (!singleResult) {
|
|
332
|
-
console.warn("No tool call results returned");
|
|
333
|
-
return void 0;
|
|
334
|
-
}
|
|
335
|
-
return {
|
|
336
|
-
goal: input.goal,
|
|
337
|
-
state: input.state,
|
|
338
|
-
execute: async (state) => {
|
|
339
|
-
if (JSON.stringify(state) === JSON.stringify(input.state)) {
|
|
340
|
-
return singleResult.result;
|
|
341
|
-
}
|
|
342
|
-
return void 0;
|
|
343
|
-
},
|
|
344
|
-
nextEvent: singleResult.result,
|
|
345
|
-
sessionId: agent.sessionId,
|
|
346
|
-
timestamp: Date.now()
|
|
347
|
-
};
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
// src/decision.ts
|
|
351
|
-
var import_xstate2 = require("xstate");
|
|
352
|
-
async function agentDecide(agent, options) {
|
|
353
|
-
const resolvedOptions = {
|
|
354
|
-
...agent.defaultOptions,
|
|
355
|
-
...options
|
|
356
|
-
};
|
|
357
|
-
const {
|
|
358
|
-
planner = simplePlanner,
|
|
359
|
-
goal,
|
|
360
|
-
events = agent.events,
|
|
361
|
-
state,
|
|
362
|
-
machine,
|
|
363
|
-
model = agent.model,
|
|
364
|
-
...otherPlanInput
|
|
365
|
-
} = resolvedOptions;
|
|
366
|
-
const plan = await planner(agent, {
|
|
367
|
-
model,
|
|
368
|
-
goal,
|
|
369
|
-
events,
|
|
370
|
-
state,
|
|
371
|
-
machine,
|
|
372
|
-
...otherPlanInput
|
|
373
|
-
});
|
|
374
|
-
if (plan?.nextEvent) {
|
|
375
|
-
agent.addPlan(plan);
|
|
376
|
-
await resolvedOptions.execute?.(plan.nextEvent);
|
|
377
|
-
}
|
|
378
|
-
return plan;
|
|
379
|
-
}
|
|
380
|
-
function fromDecision(agent, defaultInput) {
|
|
381
|
-
return (0, import_xstate2.fromPromise)(async ({ input, self }) => {
|
|
382
|
-
const parentRef = self._parent;
|
|
383
|
-
if (!parentRef) {
|
|
384
|
-
return;
|
|
385
|
-
}
|
|
386
|
-
const snapshot = parentRef.getSnapshot();
|
|
387
|
-
const inputObject = typeof input === "string" ? { goal: input } : input;
|
|
388
|
-
const resolvedInput = {
|
|
389
|
-
...defaultInput,
|
|
390
|
-
...inputObject
|
|
391
|
-
};
|
|
392
|
-
const contextToInclude = resolvedInput.context === true ? (
|
|
393
|
-
// include entire context
|
|
394
|
-
parentRef.getSnapshot().context
|
|
395
|
-
) : resolvedInput.context;
|
|
396
|
-
const state = {
|
|
397
|
-
value: snapshot.value,
|
|
398
|
-
context: contextToInclude
|
|
399
|
-
};
|
|
400
|
-
const plan = await agentDecide(agent, {
|
|
401
|
-
machine: parentRef.logic,
|
|
402
|
-
state,
|
|
403
|
-
execute: async (event) => {
|
|
404
|
-
parentRef.send(event);
|
|
405
|
-
},
|
|
406
|
-
...resolvedInput
|
|
407
|
-
});
|
|
408
|
-
return plan;
|
|
409
|
-
});
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
// src/adapters/vercel.ts
|
|
413
|
-
var import_ai2 = require("ai");
|
|
414
|
-
var vercelAdapter = {
|
|
415
|
-
generateText: import_ai2.generateText,
|
|
416
|
-
streamText: import_ai2.streamText
|
|
417
|
-
};
|
|
418
|
-
|
|
419
|
-
// src/agent.ts
|
|
420
|
-
var agentLogic = (0, import_xstate3.fromTransition)(
|
|
421
|
-
(state, event, { emit }) => {
|
|
422
|
-
switch (event.type) {
|
|
423
|
-
case "agent.feedback": {
|
|
424
|
-
state.feedback.push(event.feedback);
|
|
425
|
-
emit({
|
|
426
|
-
type: "feedback",
|
|
427
|
-
// @ts-ignore TODO: fix types in XState
|
|
428
|
-
feedback: event.feedback
|
|
429
|
-
});
|
|
430
|
-
break;
|
|
431
|
-
}
|
|
432
|
-
case "agent.observe": {
|
|
433
|
-
state.observations.push(event.observation);
|
|
434
|
-
emit({
|
|
435
|
-
type: "observation",
|
|
436
|
-
// @ts-ignore TODO: fix types in XState
|
|
437
|
-
observation: event.observation
|
|
438
|
-
});
|
|
439
|
-
break;
|
|
440
|
-
}
|
|
441
|
-
case "agent.message": {
|
|
442
|
-
state.messages.push(event.message);
|
|
443
|
-
emit({
|
|
444
|
-
type: "message",
|
|
445
|
-
// @ts-ignore TODO: fix types in XState
|
|
446
|
-
message: event.message
|
|
447
|
-
});
|
|
448
|
-
break;
|
|
449
|
-
}
|
|
450
|
-
case "agent.plan": {
|
|
451
|
-
state.plans.push(event.plan);
|
|
452
|
-
emit({
|
|
453
|
-
type: "plan",
|
|
454
|
-
// @ts-ignore TODO: fix types in XState
|
|
455
|
-
plan: event.plan
|
|
456
|
-
});
|
|
457
|
-
break;
|
|
458
|
-
}
|
|
459
|
-
default:
|
|
460
|
-
break;
|
|
461
|
-
}
|
|
462
|
-
return state;
|
|
463
|
-
},
|
|
464
|
-
() => ({
|
|
465
|
-
feedback: [],
|
|
466
|
-
messages: [],
|
|
467
|
-
observations: [],
|
|
468
|
-
plans: []
|
|
469
|
-
})
|
|
470
|
-
);
|
|
471
|
-
function createAgent({
|
|
472
|
-
name,
|
|
473
|
-
description,
|
|
474
|
-
model,
|
|
475
|
-
events,
|
|
476
|
-
context,
|
|
477
|
-
planner = simplePlanner,
|
|
478
|
-
stringify = JSON.stringify,
|
|
479
|
-
getMemory,
|
|
480
|
-
logic = agentLogic,
|
|
481
|
-
adapter = vercelAdapter,
|
|
482
|
-
...generateTextOptions
|
|
483
|
-
}) {
|
|
484
|
-
const agent = (0, import_xstate3.createActor)(logic);
|
|
485
|
-
agent.events = events;
|
|
486
|
-
agent.model = model;
|
|
487
|
-
agent.name = name;
|
|
488
|
-
agent.description = description;
|
|
489
|
-
agent.adapter = adapter;
|
|
490
|
-
agent.defaultOptions = { ...generateTextOptions, model };
|
|
491
|
-
agent.select = (selector) => {
|
|
492
|
-
return selector(agent.getSnapshot().context);
|
|
493
|
-
};
|
|
494
|
-
agent.memory = getMemory ? getMemory(agent) : void 0;
|
|
495
|
-
agent.onMessage = (callback) => {
|
|
496
|
-
agent.on("message", (ev) => callback(ev.message));
|
|
497
|
-
};
|
|
498
|
-
agent.decide = (opts) => {
|
|
499
|
-
return agentDecide(agent, opts);
|
|
500
|
-
};
|
|
501
|
-
agent.addMessage = (messageInput) => {
|
|
502
|
-
const message = {
|
|
503
|
-
...messageInput,
|
|
504
|
-
id: messageInput.id ?? randomId(),
|
|
505
|
-
timestamp: messageInput.timestamp ?? Date.now(),
|
|
506
|
-
sessionId: agent.sessionId,
|
|
507
|
-
correlationId: messageInput.correlationId ?? randomId()
|
|
508
|
-
};
|
|
509
|
-
agent.send({
|
|
510
|
-
type: "agent.message",
|
|
511
|
-
message
|
|
512
|
-
});
|
|
513
|
-
return message;
|
|
514
|
-
};
|
|
515
|
-
agent.getMessages = () => agent.getSnapshot().context.messages;
|
|
516
|
-
agent.generateText = (opts) => agentGenerateText(agent, opts);
|
|
517
|
-
agent.streamText = (opts) => agentStreamText(agent, opts);
|
|
518
|
-
agent.addFeedback = (feedbackInput) => {
|
|
519
|
-
const feedback = {
|
|
520
|
-
...feedbackInput,
|
|
521
|
-
attributes: { ...feedbackInput.attributes },
|
|
522
|
-
reward: feedbackInput.reward ?? 0,
|
|
523
|
-
timestamp: feedbackInput.timestamp ?? Date.now(),
|
|
524
|
-
sessionId: agent.sessionId
|
|
525
|
-
};
|
|
526
|
-
agent.send({
|
|
527
|
-
type: "agent.feedback",
|
|
528
|
-
feedback
|
|
529
|
-
});
|
|
530
|
-
return feedback;
|
|
531
|
-
};
|
|
532
|
-
agent.getFeedback = () => agent.getSnapshot().context.feedback;
|
|
533
|
-
agent.addObservation = (observationInput) => {
|
|
534
|
-
const { prevState, event, state } = observationInput;
|
|
535
|
-
const observation = {
|
|
536
|
-
prevState,
|
|
537
|
-
event,
|
|
538
|
-
state,
|
|
539
|
-
id: observationInput.id ?? randomId(),
|
|
540
|
-
sessionId: agent.sessionId,
|
|
541
|
-
timestamp: observationInput.timestamp ?? Date.now(),
|
|
542
|
-
machineHash: observationInput.machine ? getMachineHash(observationInput.machine) : void 0
|
|
543
|
-
};
|
|
544
|
-
agent.send({
|
|
545
|
-
type: "agent.observe",
|
|
546
|
-
observation
|
|
547
|
-
});
|
|
548
|
-
return observation;
|
|
549
|
-
};
|
|
550
|
-
agent.getObservations = () => agent.getSnapshot().context.observations;
|
|
551
|
-
agent.addPlan = (plan) => {
|
|
552
|
-
agent.send({
|
|
553
|
-
type: "agent.plan",
|
|
554
|
-
plan
|
|
555
|
-
});
|
|
556
|
-
};
|
|
557
|
-
agent.getPlans = () => agent.getSnapshot().context.plans;
|
|
558
|
-
agent.interact = (actorRef, getInput) => {
|
|
559
|
-
let prevState = void 0;
|
|
560
|
-
let subscribed = true;
|
|
561
|
-
async function handleObservation(observationInput) {
|
|
562
|
-
const observation = agent.addObservation(observationInput);
|
|
563
|
-
const input = getInput?.(observation);
|
|
564
|
-
if (input) {
|
|
565
|
-
await agentDecide(agent, {
|
|
566
|
-
machine: actorRef.src,
|
|
567
|
-
state: observation.state,
|
|
568
|
-
execute: async (event) => {
|
|
569
|
-
actorRef.send(event);
|
|
570
|
-
},
|
|
571
|
-
...input
|
|
572
|
-
});
|
|
573
|
-
}
|
|
574
|
-
prevState = observationInput.state;
|
|
575
|
-
}
|
|
576
|
-
actorRef.system.inspect({
|
|
577
|
-
next: async (inspEvent) => {
|
|
578
|
-
if (!subscribed || inspEvent.actorRef !== actorRef || inspEvent.type !== "@xstate.snapshot") {
|
|
579
|
-
return;
|
|
580
|
-
}
|
|
581
|
-
const observationInput = {
|
|
582
|
-
event: inspEvent.event,
|
|
583
|
-
prevState,
|
|
584
|
-
state: inspEvent.snapshot,
|
|
585
|
-
machine: actorRef.src
|
|
586
|
-
};
|
|
587
|
-
await handleObservation(observationInput);
|
|
588
|
-
}
|
|
589
|
-
});
|
|
590
|
-
if (actorRef._processingStatus === 1) {
|
|
591
|
-
handleObservation({
|
|
592
|
-
prevState: void 0,
|
|
593
|
-
event: { type: "" },
|
|
594
|
-
// TODO: unknown events?
|
|
595
|
-
state: actorRef.getSnapshot(),
|
|
596
|
-
machine: actorRef.src
|
|
597
|
-
});
|
|
598
|
-
}
|
|
599
|
-
return {
|
|
600
|
-
unsubscribe: () => {
|
|
601
|
-
subscribed = false;
|
|
602
|
-
}
|
|
603
|
-
// TODO: make this actually unsubscribe
|
|
604
|
-
};
|
|
605
|
-
};
|
|
606
|
-
agent.types = {};
|
|
607
|
-
agent.start();
|
|
608
|
-
return agent;
|
|
609
|
-
}
|
|
610
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
611
|
-
0 && (module.exports = {
|
|
612
|
-
createAgent,
|
|
613
|
-
fromDecision,
|
|
614
|
-
fromText,
|
|
615
|
-
fromTextStream
|
|
616
|
-
});
|
package/examples/chatbot.ts
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
|
-
import { createAgent, fromDecision } from '../src';
|
|
3
|
-
import { openai } from '@ai-sdk/openai';
|
|
4
|
-
import { assign, createActor, log, setup } from 'xstate';
|
|
5
|
-
import { getFromTerminal } from './helpers/helpers';
|
|
6
|
-
|
|
7
|
-
const agent = createAgent({
|
|
8
|
-
name: 'chatbot',
|
|
9
|
-
model: openai('gpt-4-turbo'),
|
|
10
|
-
events: {
|
|
11
|
-
'agent.respond': z.object({
|
|
12
|
-
response: z.string().describe('The response from the agent'),
|
|
13
|
-
}),
|
|
14
|
-
'agent.endConversation': z.object({}).describe('Stop the conversation'),
|
|
15
|
-
},
|
|
16
|
-
context: {
|
|
17
|
-
userMessage: z.string(),
|
|
18
|
-
},
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
const machine = setup({
|
|
22
|
-
types: agent.types,
|
|
23
|
-
actors: { agent: fromDecision(agent), getFromTerminal },
|
|
24
|
-
}).createMachine({
|
|
25
|
-
initial: 'listening',
|
|
26
|
-
context: {
|
|
27
|
-
userMessage: '',
|
|
28
|
-
},
|
|
29
|
-
states: {
|
|
30
|
-
listening: {
|
|
31
|
-
invoke: {
|
|
32
|
-
src: 'getFromTerminal',
|
|
33
|
-
input: 'User:',
|
|
34
|
-
onDone: {
|
|
35
|
-
actions: assign({
|
|
36
|
-
userMessage: (x) => x.event.output,
|
|
37
|
-
}),
|
|
38
|
-
target: 'responding',
|
|
39
|
-
},
|
|
40
|
-
},
|
|
41
|
-
},
|
|
42
|
-
responding: {
|
|
43
|
-
invoke: {
|
|
44
|
-
src: 'agent',
|
|
45
|
-
input: (x) => ({
|
|
46
|
-
context: {
|
|
47
|
-
userMessage: 'User says: ' + x.context.userMessage,
|
|
48
|
-
},
|
|
49
|
-
messages: agent.getMessages(),
|
|
50
|
-
goal: 'Respond to the user, unless they want to end the conversation.',
|
|
51
|
-
}),
|
|
52
|
-
},
|
|
53
|
-
on: {
|
|
54
|
-
'agent.respond': {
|
|
55
|
-
actions: [log((x) => `Agent: ${x.event.response}`)],
|
|
56
|
-
target: 'listening',
|
|
57
|
-
},
|
|
58
|
-
'agent.endConversation': 'finished',
|
|
59
|
-
},
|
|
60
|
-
},
|
|
61
|
-
finished: {
|
|
62
|
-
type: 'final',
|
|
63
|
-
},
|
|
64
|
-
},
|
|
65
|
-
exit: () => {
|
|
66
|
-
console.log('End of conversation.');
|
|
67
|
-
process.exit();
|
|
68
|
-
},
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
createActor(machine).start();
|