@economic/agents 0.0.1-beta.6 → 1.0.0
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/README.md +325 -418
- package/bin/cli.mjs +2 -0
- package/dist/cli.d.mts +1 -0
- package/dist/cli.mjs +561 -0
- package/dist/hono.d.mts +21 -0
- package/dist/hono.mjs +71 -0
- package/dist/index.d.mts +163 -108
- package/dist/index.mjs +501 -327
- package/package.json +21 -8
- package/schema/agent.sql +12 -0
- package/schema/{schema.sql → chat.sql} +1 -5
- package/dist/react.d.mts +0 -25
- package/dist/react.mjs +0 -38
package/dist/index.d.mts
CHANGED
|
@@ -1,7 +1,26 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { Agent as Agent$1, Connection, ConnectionContext } from "agents";
|
|
2
|
+
import { AIChatAgent, OnChatMessageOptions } from "@cloudflare/ai-chat";
|
|
3
|
+
import { LanguageModel, StreamTextOnFinishCallback, ToolSet, UIMessage, generateText, streamText } from "ai";
|
|
4
4
|
|
|
5
|
+
//#region src/server/types.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* The context object available throughout an agent's lifetime — passed via
|
|
8
|
+
* `experimental_context` to tool `execute` functions. Contains the typed
|
|
9
|
+
* request body merged with platform capabilities like `logEvent`.
|
|
10
|
+
*
|
|
11
|
+
* Define your own body shape and compose:
|
|
12
|
+
* ```typescript
|
|
13
|
+
* interface MyBody { userTier: "free" | "pro" }
|
|
14
|
+
* type MyContext = AgentToolContext<MyBody>;
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
type AgentToolContext<TBody = Record<string, unknown>> = TBody & {
|
|
18
|
+
logEvent: (message: string, payload?: Record<string, unknown>) => void | Promise<void>;
|
|
19
|
+
};
|
|
20
|
+
interface AgentEnv {
|
|
21
|
+
AGENT_DB: D1Database;
|
|
22
|
+
}
|
|
23
|
+
//#endregion
|
|
5
24
|
//#region src/server/features/skills/index.d.ts
|
|
6
25
|
/**
|
|
7
26
|
* A named group of related tools that can be loaded together on demand.
|
|
@@ -26,60 +45,82 @@ interface Skill {
|
|
|
26
45
|
//#endregion
|
|
27
46
|
//#region src/server/llm.d.ts
|
|
28
47
|
type LLMParams = Parameters<typeof streamText>[0] & Parameters<typeof generateText>[0];
|
|
29
|
-
type BuildLLMParamsConfig = Omit<LLMParams, "
|
|
30
|
-
/**
|
|
31
|
-
messages: UIMessage[]; /** Skill names loaded in previous turns. Pass `await this.getLoadedSkills()`. */
|
|
32
|
-
activeSkills?: string[]; /** Skills available for on-demand loading this turn. */
|
|
48
|
+
type BuildLLMParamsConfig = Omit<LLMParams, "prompt"> & {
|
|
49
|
+
/** Skill names loaded in previous turns. Pass `await this.getLoadedSkills()`. */activeSkills?: string[]; /** Skills available for on-demand loading this turn. */
|
|
33
50
|
skills?: Skill[];
|
|
34
|
-
/**
|
|
35
|
-
* Number of recent messages to keep verbatim during compaction. Older messages
|
|
36
|
-
* beyond this count are summarised by `fastModel` before being sent to the LLM.
|
|
37
|
-
*
|
|
38
|
-
* Defaults to `DEFAULT_MAX_MESSAGES_BEFORE_COMPACTION` (30) when not provided.
|
|
39
|
-
* Set explicitly to `undefined` to disable compaction entirely.
|
|
40
|
-
*
|
|
41
|
-
* Compaction only runs when `fastModel` is also set on the agent class.
|
|
42
|
-
*
|
|
43
|
-
* @internal Injected by `AIChatAgent.buildLLMParams` — do not set this directly.
|
|
44
|
-
*/
|
|
45
|
-
maxMessagesBeforeCompaction?: number;
|
|
46
|
-
/**
|
|
47
|
-
* The fast/cheap model used for compaction and background summarization.
|
|
48
|
-
* Provided automatically from `AIChatAgent.fastModel` — do not set this directly.
|
|
49
|
-
*
|
|
50
|
-
* @internal
|
|
51
|
-
*/
|
|
52
|
-
fastModel?: LanguageModel;
|
|
53
51
|
};
|
|
54
52
|
/**
|
|
55
53
|
* Builds the parameter object for a Vercel AI SDK `streamText` or `generateText` call.
|
|
56
54
|
*
|
|
57
|
-
* Handles
|
|
58
|
-
* `list_capabilities`, `prepareStep`), and context/abort signal extraction from
|
|
59
|
-
* the Cloudflare Agents SDK `options` object.
|
|
55
|
+
* Handles skill wiring (`activate_skill`, `list_capabilities`, `prepareStep`).
|
|
60
56
|
*
|
|
61
57
|
* The returned object can be spread directly into `streamText` or `generateText`:
|
|
62
58
|
*
|
|
63
59
|
* ```typescript
|
|
64
|
-
* const params =
|
|
60
|
+
* const params = buildLLMParams({ ... });
|
|
65
61
|
* return streamText(params).toUIMessageStreamResponse();
|
|
66
62
|
* ```
|
|
67
63
|
*/
|
|
68
|
-
declare function buildLLMParams(config: BuildLLMParamsConfig):
|
|
64
|
+
declare function buildLLMParams(config: BuildLLMParamsConfig): LLMParams;
|
|
65
|
+
//#endregion
|
|
66
|
+
//#region src/server/agents/Agent.d.ts
|
|
67
|
+
/**
|
|
68
|
+
* Base agent for Cloudflare Agents SDK Durable Objects with lazy skill loading,
|
|
69
|
+
* audit logging, and `buildLLMParams` wiring.
|
|
70
|
+
*
|
|
71
|
+
* Handles CF infrastructure concerns: DO SQLite persistence for loaded skill state
|
|
72
|
+
* and writing audit events to D1.
|
|
73
|
+
*
|
|
74
|
+
* For chat agents with message history, compaction, and conversation recording,
|
|
75
|
+
* extend {@link ChatAgent} instead.
|
|
76
|
+
*/
|
|
77
|
+
declare abstract class Agent<Env extends Cloudflare.Env = Cloudflare.Env> extends Agent$1<Env & AgentEnv> {
|
|
78
|
+
/**
|
|
79
|
+
* Returns the user ID from the durable object name.
|
|
80
|
+
*/
|
|
81
|
+
protected getUserId(): string;
|
|
82
|
+
onConnect(connection: Connection, ctx: ConnectionContext): Promise<void>;
|
|
83
|
+
/**
|
|
84
|
+
* Writes an audit event to D1 if `AGENT_DB` is bound on the environment,
|
|
85
|
+
* otherwise silently does nothing.
|
|
86
|
+
*
|
|
87
|
+
* Called automatically at the end of each LLM turn (from `onFinish` in
|
|
88
|
+
* `buildLLMParams`). Also available via `experimental_context.logEvent` in tool
|
|
89
|
+
* `execute` functions.
|
|
90
|
+
*/
|
|
91
|
+
protected logEvent(message: string, payload?: Record<string, unknown>): Promise<void>;
|
|
92
|
+
/**
|
|
93
|
+
* Builds the parameter object for a `streamText` or `generateText` call,
|
|
94
|
+
* pre-filling `activeSkills` from this agent instance.
|
|
95
|
+
* Injects `logEvent` into `experimental_context` and wires `onFinish` for
|
|
96
|
+
* turn-completed audit events.
|
|
97
|
+
*/
|
|
98
|
+
protected buildLLMParams<TBody = Record<string, unknown>>(config: Omit<BuildLLMParamsConfig, "messages"> & {
|
|
99
|
+
options?: OnChatMessageOptions;
|
|
100
|
+
}): Promise<LLMParams>;
|
|
101
|
+
}
|
|
69
102
|
//#endregion
|
|
70
|
-
//#region src/server/agents/
|
|
103
|
+
//#region src/server/agents/ChatAgent.d.ts
|
|
71
104
|
/**
|
|
72
|
-
*
|
|
73
|
-
* and
|
|
105
|
+
* Chat agent for Cloudflare Agents SDK: lazy skill loading, audit logging,
|
|
106
|
+
* message persistence, compaction, and conversation metadata in D1.
|
|
74
107
|
*
|
|
75
|
-
* Handles CF infrastructure concerns
|
|
76
|
-
*
|
|
77
|
-
*
|
|
108
|
+
* Handles CF infrastructure concerns: DO SQLite for loaded skill state,
|
|
109
|
+
* stripping skill meta-tool messages before persistence, history replay to
|
|
110
|
+
* newly connected clients, and audit events to D1.
|
|
78
111
|
*
|
|
79
|
-
* Skill loading, compaction, and LLM
|
|
80
|
-
*
|
|
112
|
+
* Skill loading, compaction, and LLM calls use `buildLLMParams` from
|
|
113
|
+
* `@economic/agents` inside `onChatMessage`.
|
|
81
114
|
*/
|
|
82
|
-
declare abstract class
|
|
115
|
+
declare abstract class ChatAgent<Env extends Cloudflare.Env = Cloudflare.Env> extends AIChatAgent<Env & AgentEnv> {
|
|
116
|
+
/**
|
|
117
|
+
* The binding of the Durable Object instance for this agent.
|
|
118
|
+
*/
|
|
119
|
+
protected abstract get binding(): {
|
|
120
|
+
getByName(name: string): {
|
|
121
|
+
destroy(): Promise<void>;
|
|
122
|
+
};
|
|
123
|
+
};
|
|
83
124
|
/**
|
|
84
125
|
* Fast/cheap language model used for background tasks: compaction and conversation summarization.
|
|
85
126
|
*
|
|
@@ -92,101 +133,115 @@ declare abstract class AIChatAgent<Env extends Cloudflare.Env = Cloudflare.Env>
|
|
|
92
133
|
* To disable compaction for a specific call, pass `maxMessagesBeforeCompaction: undefined`
|
|
93
134
|
* to `buildLLMParams` rather than omitting or nulling out `fastModel`.
|
|
94
135
|
*/
|
|
95
|
-
protected abstract
|
|
96
|
-
protected getUserId(): string;
|
|
97
|
-
onConnect(connection: Connection, ctx: ConnectionContext): Promise<void>;
|
|
136
|
+
protected abstract getFastModel(): LanguageModel;
|
|
98
137
|
/**
|
|
99
|
-
*
|
|
100
|
-
*
|
|
138
|
+
* Number of days of inactivity before the full conversation is deleted.
|
|
139
|
+
*
|
|
140
|
+
* Leave `undefined` to disable automatic retention cleanup.
|
|
101
141
|
*/
|
|
102
|
-
|
|
142
|
+
protected conversationRetentionDays?: number;
|
|
103
143
|
/**
|
|
104
|
-
*
|
|
144
|
+
* Number of recent messages to keep verbatim when compaction runs.
|
|
145
|
+
* Older messages beyond this count are summarised into a single system message.
|
|
146
|
+
* Used as the default when `maxMessagesBeforeCompaction` is not provided to `buildLLMParams`.
|
|
147
|
+
*
|
|
148
|
+
* Default is 15.
|
|
105
149
|
*/
|
|
106
|
-
|
|
150
|
+
protected maxMessagesBeforeCompaction?: number | undefined;
|
|
107
151
|
/**
|
|
108
|
-
*
|
|
109
|
-
* otherwise silently does nothing.
|
|
110
|
-
*
|
|
111
|
-
* Called automatically after every turn (from `persistMessages`) and on
|
|
112
|
-
* non-clean finish reasons (from `buildLLMParams`). Also available via
|
|
113
|
-
* `experimental_context.log` in tool `execute` functions.
|
|
152
|
+
* Returns the user ID from the durable object name.
|
|
114
153
|
*/
|
|
115
|
-
protected
|
|
154
|
+
protected getUserId(): string;
|
|
155
|
+
onConnect(connection: Connection, ctx: ConnectionContext): Promise<void>;
|
|
116
156
|
/**
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
* from `persistMessages` after every turn.
|
|
157
|
+
* Writes an audit event to D1 if `AGENT_DB` is bound on the environment,
|
|
158
|
+
* otherwise silently does nothing.
|
|
120
159
|
*
|
|
121
|
-
*
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
* `SUMMARY_CONTEXT_MESSAGES` messages (when the context window fully turns
|
|
125
|
-
* over). Neither path blocks the response to the client.
|
|
160
|
+
* Called automatically at the end of each LLM turn (from `onFinish` in
|
|
161
|
+
* `buildLLMParams`). Also available via `experimental_context.logEvent` in tool
|
|
162
|
+
* `execute` functions.
|
|
126
163
|
*/
|
|
127
|
-
|
|
164
|
+
protected logEvent(message: string, payload?: Record<string, unknown>): Promise<void>;
|
|
128
165
|
/**
|
|
129
166
|
* Builds the parameter object for a `streamText` or `generateText` call,
|
|
130
167
|
* pre-filling `messages`, `activeSkills`, and `fastModel` from this agent instance.
|
|
131
|
-
* Injects `
|
|
168
|
+
* Injects `logEvent` into `experimental_context` and wires `onFinish` for
|
|
169
|
+
* turn-completed audit events and conversation recording.
|
|
132
170
|
*
|
|
133
171
|
* **Compaction** runs automatically when `fastModel` is set on the class, using
|
|
134
172
|
* `DEFAULT_MAX_MESSAGES_BEFORE_COMPACTION` (30) as the threshold. Override the
|
|
135
|
-
* threshold by
|
|
136
|
-
* by
|
|
137
|
-
*
|
|
138
|
-
* ```typescript
|
|
139
|
-
* // Compaction on (default threshold):
|
|
140
|
-
* const params = await this.buildLLMParams({ options, onFinish, model, system: "..." });
|
|
141
|
-
*
|
|
142
|
-
* // Compaction with custom threshold:
|
|
143
|
-
* const params = await this.buildLLMParams({ options, onFinish, model, maxMessagesBeforeCompaction: 50 });
|
|
144
|
-
*
|
|
145
|
-
* // Compaction off:
|
|
146
|
-
* const params = await this.buildLLMParams({ options, onFinish, model, maxMessagesBeforeCompaction: undefined });
|
|
147
|
-
*
|
|
148
|
-
* return streamText(params).toUIMessageStreamResponse();
|
|
149
|
-
* ```
|
|
173
|
+
* threshold by setting `maxMessagesBeforeCompaction` on the class. Disable compaction
|
|
174
|
+
* entirely by setting `maxMessagesBeforeCompaction = undefined` explicitly.
|
|
150
175
|
*/
|
|
151
|
-
protected buildLLMParams<TBody = Record<string, unknown>>(config:
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
* Returns an empty array if no skills have been loaded yet.
|
|
155
|
-
*/
|
|
156
|
-
protected getLoadedSkills(): Promise<string[]>;
|
|
176
|
+
protected buildLLMParams<TBody = Record<string, unknown>>(config: BuildLLMParamsConfig & {
|
|
177
|
+
options?: OnChatMessageOptions;
|
|
178
|
+
}): Promise<LLMParams>;
|
|
157
179
|
/**
|
|
158
180
|
* Extracts skill state from activate_skill results, persists to DO SQLite,
|
|
159
|
-
*
|
|
160
|
-
* delegating to super.
|
|
181
|
+
* then strips all skill meta-tool messages before delegating to super.
|
|
161
182
|
*
|
|
162
183
|
* 1. Scans activate_skill tool results for SKILL_STATE_SENTINEL. When found,
|
|
163
184
|
* the embedded JSON array of loaded skill names is written to DO SQLite.
|
|
164
185
|
*
|
|
165
|
-
* 2.
|
|
166
|
-
*
|
|
167
|
-
* 3. Strips all activate_skill and list_capabilities messages from history.
|
|
186
|
+
* 2. Strips all activate_skill and list_capabilities messages from history.
|
|
168
187
|
*
|
|
169
|
-
*
|
|
188
|
+
* 3. Delegates to super.persistMessages for message storage and WS broadcast.
|
|
170
189
|
*/
|
|
171
190
|
persistMessages(messages: UIMessage[], excludeBroadcastIds?: string[], options?: {
|
|
172
191
|
_deleteStaleRows?: boolean;
|
|
173
192
|
}): Promise<void>;
|
|
193
|
+
getConversations(): Promise<Record<string, unknown>[]>;
|
|
194
|
+
deleteConversation(id: string): Promise<boolean>;
|
|
195
|
+
destroy(): Promise<void>;
|
|
196
|
+
/**
|
|
197
|
+
* Records this conversation in the `conversations` D1 table and triggers
|
|
198
|
+
* LLM-based title/summary generation when appropriate. Called automatically
|
|
199
|
+
* from `onFinish` in `buildLLMParams` after each completed LLM turn.
|
|
200
|
+
*
|
|
201
|
+
* On the first turn (no existing row), awaits `generateTitleAndSummary` and
|
|
202
|
+
* inserts the row with title and summary already populated. On subsequent
|
|
203
|
+
* turns, upserts the timestamp and fire-and-forgets a summary refresh every
|
|
204
|
+
* `SUMMARY_CONTEXT_MESSAGES` messages (when the context window fully turns
|
|
205
|
+
* over). Neither path blocks the response to the client.
|
|
206
|
+
*/
|
|
207
|
+
private recordConversation;
|
|
208
|
+
private deleteConversationCallback;
|
|
209
|
+
private scheduleConversationForDeletion;
|
|
174
210
|
}
|
|
175
211
|
//#endregion
|
|
176
|
-
//#region src/server/
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
212
|
+
//#region src/server/harnesses/ChatAgentHarness.d.ts
|
|
213
|
+
declare abstract class ChatAgentHarness<Env extends Cloudflare.Env, RequestBody extends Record<string, unknown> = Record<string, unknown>> extends ChatAgent<Env> {
|
|
214
|
+
get binding(): {
|
|
215
|
+
getByName(name: string): {
|
|
216
|
+
destroy(): Promise<void>;
|
|
217
|
+
};
|
|
218
|
+
};
|
|
219
|
+
conversationRetentionDays: number;
|
|
220
|
+
/**
|
|
221
|
+
* Returns the model for the agent.
|
|
222
|
+
* @param ctx - The context object for the agent built from the request body.
|
|
223
|
+
* @returns The model for the agent.
|
|
224
|
+
*/
|
|
225
|
+
abstract getModel(ctx: AgentToolContext<RequestBody>): LanguageModel;
|
|
226
|
+
/**
|
|
227
|
+
* Returns the system prompt for the agent.
|
|
228
|
+
* @param ctx - The context object for the agent built from the request body.
|
|
229
|
+
* @returns The system prompt for the agent.
|
|
230
|
+
*/
|
|
231
|
+
abstract getSystemPrompt(ctx: AgentToolContext<RequestBody>): string;
|
|
232
|
+
/**
|
|
233
|
+
* Returns the tools for the agent.
|
|
234
|
+
* @param ctx - The context object for the agent built from the request body.
|
|
235
|
+
* @returns The tools for the agent.
|
|
236
|
+
*/
|
|
237
|
+
getTools(_ctx: AgentToolContext<RequestBody>): ToolSet;
|
|
238
|
+
/**
|
|
239
|
+
* Returns the skills for the agent.
|
|
240
|
+
* @param ctx - The context object for the agent built from the request body.
|
|
241
|
+
* @returns The skills for the agent.
|
|
242
|
+
*/
|
|
243
|
+
getSkills(_ctx: AgentToolContext<RequestBody>): Skill[];
|
|
244
|
+
onChatMessage(onFinish: StreamTextOnFinishCallback<ToolSet>, options?: OnChatMessageOptions): Promise<Response>;
|
|
245
|
+
}
|
|
191
246
|
//#endregion
|
|
192
|
-
export {
|
|
247
|
+
export { Agent, type AgentToolContext, type BuildLLMParamsConfig, ChatAgent, ChatAgentHarness, type Skill, buildLLMParams };
|