@economic/agents 2.0.0 → 2.1.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/dist/v2.d.mts +10 -7
- package/dist/v2.mjs +86 -71
- package/package.json +1 -1
package/dist/v2.d.mts
CHANGED
|
@@ -19,6 +19,13 @@ interface AgentEnv {
|
|
|
19
19
|
AGENTS_ANALYTICS: AnalyticsEngineDataset;
|
|
20
20
|
SKILLS_BUCKET: R2Bucket;
|
|
21
21
|
}
|
|
22
|
+
type AgentConnectionStatus = "connecting" | "connected" | "disconnected" | "unauthorized";
|
|
23
|
+
type AgentConnectionType = "agent" | "chat" | "assistant";
|
|
24
|
+
type AgentConnectionState = {
|
|
25
|
+
status: AgentConnectionStatus;
|
|
26
|
+
type: AgentConnectionType; /** Set by `Assistant` so clients can derive the sub-agent routing name from state. */
|
|
27
|
+
subAgentName?: string;
|
|
28
|
+
};
|
|
22
29
|
//#endregion
|
|
23
30
|
//#region src/server/v2/util/tools.d.ts
|
|
24
31
|
type ToolSet = Record<string, Tool>;
|
|
@@ -39,12 +46,6 @@ declare function skill<Context extends Record<string, unknown> = Record<string,
|
|
|
39
46
|
//#endregion
|
|
40
47
|
//#region src/server/v2/agents/Agent.d.ts
|
|
41
48
|
declare function getCurrentToolContext(): any;
|
|
42
|
-
type AgentConnectionStatus = "connecting" | "connected" | "disconnected" | "unauthorized";
|
|
43
|
-
type AgentConnectionType = "agent" | "chat" | "assistant";
|
|
44
|
-
type AgentConnectionState = {
|
|
45
|
-
status: AgentConnectionStatus;
|
|
46
|
-
type: AgentConnectionType;
|
|
47
|
-
};
|
|
48
49
|
declare abstract class Agent<RequestContext extends Record<string, unknown> = Record<string, unknown>, UserContext extends Record<string, unknown> = Record<string, unknown>> extends Think<Cloudflare.Env & AgentEnv, AgentConnectionState> {
|
|
49
50
|
initialState: AgentConnectionState;
|
|
50
51
|
protected clientIp?: string;
|
|
@@ -58,6 +59,7 @@ declare abstract class Agent<RequestContext extends Record<string, unknown> = Re
|
|
|
58
59
|
abstract getSystemPrompt(ctx?: ToolContext<RequestContext, UserContext>): string;
|
|
59
60
|
configureSession(session: Session): Session;
|
|
60
61
|
onStart(): Promise<void>;
|
|
62
|
+
onClose(): Promise<void>;
|
|
61
63
|
onConnect(connection: Connection, ctx: ConnectionContext): Promise<void>;
|
|
62
64
|
/**
|
|
63
65
|
* Merges the client request `body` into `experimental_context` for tools
|
|
@@ -152,6 +154,7 @@ declare abstract class Assistant extends Agent$1<Cloudflare.Env, AgentConnection
|
|
|
152
154
|
protected abstract agent: SubAgentClass<ChatAgent>;
|
|
153
155
|
protected abstract fastModel: LanguageModel;
|
|
154
156
|
onStart(): void;
|
|
157
|
+
onClose(): Promise<void>;
|
|
155
158
|
onConnect(): Promise<void>;
|
|
156
159
|
createConversation(): Promise<string>;
|
|
157
160
|
deleteConversation(id: string): Promise<void>;
|
|
@@ -161,4 +164,4 @@ declare abstract class Assistant extends Agent$1<Cloudflare.Env, AgentConnection
|
|
|
161
164
|
private scheduleConversationForAutoDeletion;
|
|
162
165
|
}
|
|
163
166
|
//#endregion
|
|
164
|
-
export { Agent, type AgentEnv, Assistant, ChatAgent, type Skill, type Tool, type ToolContext, type ToolSet, getCurrentToolContext, skill, tool };
|
|
167
|
+
export { Agent, type AgentConnectionState, type AgentConnectionStatus, type AgentConnectionType, type AgentEnv, Assistant, ChatAgent, type Skill, type Tool, type ToolContext, type ToolSet, getCurrentToolContext, skill, tool };
|
package/dist/v2.mjs
CHANGED
|
@@ -3,8 +3,8 @@ import { Output, convertToModelMessages, generateText, jsonSchema, pruneMessages
|
|
|
3
3
|
import { Agent as Agent$1, callable, getCurrentAgent } from "agents";
|
|
4
4
|
import { Think } from "@cloudflare/think";
|
|
5
5
|
import { R2SkillProvider } from "agents/experimental/memory/session";
|
|
6
|
-
import { createCompactFunction } from "agents/experimental/memory/utils";
|
|
7
6
|
import { nanoid } from "nanoid";
|
|
7
|
+
import { createCompactFunction } from "agents/experimental/memory/utils";
|
|
8
8
|
//#region src/server/v2/util/tools.ts
|
|
9
9
|
function tool(tool) {
|
|
10
10
|
return tool$1(tool);
|
|
@@ -81,7 +81,7 @@ var Agent = class extends Think {
|
|
|
81
81
|
}
|
|
82
82
|
async onStart() {
|
|
83
83
|
this.setState({
|
|
84
|
-
|
|
84
|
+
...this.initialState,
|
|
85
85
|
status: "connecting"
|
|
86
86
|
});
|
|
87
87
|
let hasCorrectBindings = true;
|
|
@@ -100,12 +100,18 @@ var Agent = class extends Think {
|
|
|
100
100
|
}
|
|
101
101
|
if (!hasCorrectBindings) {
|
|
102
102
|
this.setState({
|
|
103
|
-
|
|
103
|
+
...this.initialState,
|
|
104
104
|
status: "disconnected"
|
|
105
105
|
});
|
|
106
106
|
throw new Error("Could not connect to agent, bindings not found");
|
|
107
107
|
}
|
|
108
108
|
}
|
|
109
|
+
async onClose() {
|
|
110
|
+
this.setState({
|
|
111
|
+
...this.initialState,
|
|
112
|
+
status: "disconnected"
|
|
113
|
+
});
|
|
114
|
+
}
|
|
109
115
|
async onConnect(connection, ctx) {
|
|
110
116
|
this.clientIp = ctx.request.headers.get("CF-Connecting-IP") ?? ctx.request.headers.get("X-Forwarded-For")?.split(",")[0]?.trim();
|
|
111
117
|
this.forwardedFor = ctx.request.headers.get("X-Forwarded-For") ?? void 0;
|
|
@@ -118,7 +124,7 @@ var Agent = class extends Think {
|
|
|
118
124
|
result = await verifyJwt(ctx.request, config);
|
|
119
125
|
} catch (error) {
|
|
120
126
|
this.setState({
|
|
121
|
-
|
|
127
|
+
...this.initialState,
|
|
122
128
|
status: "unauthorized"
|
|
123
129
|
});
|
|
124
130
|
console.error(`[Agent] JWT verification error - ${error}`);
|
|
@@ -127,7 +133,7 @@ var Agent = class extends Think {
|
|
|
127
133
|
}
|
|
128
134
|
if (!result.success) {
|
|
129
135
|
this.setState({
|
|
130
|
-
|
|
136
|
+
...this.initialState,
|
|
131
137
|
status: "unauthorized"
|
|
132
138
|
});
|
|
133
139
|
console.error(`[Agent] JWT verification error - ${result.message}`);
|
|
@@ -145,7 +151,7 @@ var Agent = class extends Think {
|
|
|
145
151
|
}
|
|
146
152
|
}
|
|
147
153
|
this.setState({
|
|
148
|
-
|
|
154
|
+
...this.initialState,
|
|
149
155
|
status: "connected"
|
|
150
156
|
});
|
|
151
157
|
}
|
|
@@ -271,69 +277,6 @@ var Agent = class extends Think {
|
|
|
271
277
|
_userContext;
|
|
272
278
|
};
|
|
273
279
|
//#endregion
|
|
274
|
-
//#region src/server/v2/features/messages.ts
|
|
275
|
-
const COMPACTION_TOKEN_THRESHOLD = 1e5;
|
|
276
|
-
const createCompactFn = (model) => createCompactFunction({ summarize: (prompt) => generateText({
|
|
277
|
-
model,
|
|
278
|
-
prompt
|
|
279
|
-
}).then((r) => r.text) });
|
|
280
|
-
/**
|
|
281
|
-
* Ensures that the ratings table exists.
|
|
282
|
-
* @param sql - The SQL function to use to execute the query.
|
|
283
|
-
*/
|
|
284
|
-
function ensureRatingsTableExists(sql) {
|
|
285
|
-
try {
|
|
286
|
-
sql`CREATE TABLE IF NOT EXISTS assistant_messages_ratings (
|
|
287
|
-
message_id TEXT NOT NULL,
|
|
288
|
-
durable_object_name TEXT NOT NULL,
|
|
289
|
-
rating INTEGER,
|
|
290
|
-
comment TEXT,
|
|
291
|
-
created_at TEXT NOT NULL,
|
|
292
|
-
updated_at TEXT NOT NULL,
|
|
293
|
-
PRIMARY KEY (message_id, durable_object_name)
|
|
294
|
-
)`;
|
|
295
|
-
} catch (error) {
|
|
296
|
-
console.error("[Agent] Failed to create ratings table", error);
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
/**
|
|
300
|
-
* Rates a message.
|
|
301
|
-
* @param sql - The SQL function to use to execute the query.
|
|
302
|
-
* @param messageId - The ID of the message to rate.
|
|
303
|
-
* @param durable_object_name - The name of the Durable Object to rate the message for.
|
|
304
|
-
* @param rating - The rating to give the message.
|
|
305
|
-
* @param now - The date and time to use for the created_at and updated_at columns.
|
|
306
|
-
*/
|
|
307
|
-
function rateMessage(sql, messageId, durable_object_name, rating, comment, now = /* @__PURE__ */ new Date()) {
|
|
308
|
-
try {
|
|
309
|
-
sql`INSERT INTO assistant_messages_ratings (message_id, durable_object_name, rating, comment, created_at, updated_at)
|
|
310
|
-
VALUES (${messageId}, ${durable_object_name}, ${rating}, ${comment ?? null}, ${now.toISOString()}, ${now.toISOString()})
|
|
311
|
-
ON CONFLICT (message_id, durable_object_name) DO UPDATE SET
|
|
312
|
-
rating = excluded.rating,
|
|
313
|
-
comment = excluded.comment,
|
|
314
|
-
updated_at = excluded.updated_at`;
|
|
315
|
-
} catch (error) {
|
|
316
|
-
console.error("[Agent] Failed to rate message", error);
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
/**
|
|
320
|
-
* Gets the ratings for a message.
|
|
321
|
-
* @param sql - The SQL function to use to execute the query.
|
|
322
|
-
* @param durable_object_name - The name of the Durable Object to get the ratings for.
|
|
323
|
-
* @returns A record of message IDs and their ratings.
|
|
324
|
-
*/
|
|
325
|
-
function getMessageRatings(sql, durable_object_name) {
|
|
326
|
-
try {
|
|
327
|
-
const ratings = sql`SELECT message_id, rating, comment FROM assistant_messages_ratings WHERE durable_object_name = ${durable_object_name}`;
|
|
328
|
-
return Object.fromEntries(ratings.map((row) => [row.message_id, {
|
|
329
|
-
rating: row.rating,
|
|
330
|
-
comment: row.comment
|
|
331
|
-
}]));
|
|
332
|
-
} catch {
|
|
333
|
-
return {};
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
//#endregion
|
|
337
280
|
//#region src/server/v2/features/conversations.ts
|
|
338
281
|
/**
|
|
339
282
|
* Ensures that the conversations table exists.
|
|
@@ -437,14 +380,23 @@ var Assistant = class extends Agent$1 {
|
|
|
437
380
|
onStart() {
|
|
438
381
|
this.setState({
|
|
439
382
|
type: "assistant",
|
|
440
|
-
status: "connecting"
|
|
383
|
+
status: "connecting",
|
|
384
|
+
subAgentName: this.agent.name
|
|
441
385
|
});
|
|
442
386
|
ensureConversationsTableExists(this.sql.bind(this));
|
|
443
387
|
}
|
|
388
|
+
async onClose() {
|
|
389
|
+
this.setState({
|
|
390
|
+
type: "assistant",
|
|
391
|
+
status: "disconnected",
|
|
392
|
+
subAgentName: this.agent.name
|
|
393
|
+
});
|
|
394
|
+
}
|
|
444
395
|
async onConnect() {
|
|
445
396
|
this.setState({
|
|
446
397
|
type: "assistant",
|
|
447
|
-
status: "connected"
|
|
398
|
+
status: "connected",
|
|
399
|
+
subAgentName: this.agent.name
|
|
448
400
|
});
|
|
449
401
|
}
|
|
450
402
|
@callable() async createConversation() {
|
|
@@ -477,6 +429,69 @@ var Assistant = class extends Agent$1 {
|
|
|
477
429
|
}
|
|
478
430
|
};
|
|
479
431
|
//#endregion
|
|
432
|
+
//#region src/server/v2/features/messages.ts
|
|
433
|
+
const COMPACTION_TOKEN_THRESHOLD = 1e5;
|
|
434
|
+
const createCompactFn = (model) => createCompactFunction({ summarize: (prompt) => generateText({
|
|
435
|
+
model,
|
|
436
|
+
prompt
|
|
437
|
+
}).then((r) => r.text) });
|
|
438
|
+
/**
|
|
439
|
+
* Ensures that the ratings table exists.
|
|
440
|
+
* @param sql - The SQL function to use to execute the query.
|
|
441
|
+
*/
|
|
442
|
+
function ensureRatingsTableExists(sql) {
|
|
443
|
+
try {
|
|
444
|
+
sql`CREATE TABLE IF NOT EXISTS assistant_messages_ratings (
|
|
445
|
+
message_id TEXT NOT NULL,
|
|
446
|
+
durable_object_name TEXT NOT NULL,
|
|
447
|
+
rating INTEGER,
|
|
448
|
+
comment TEXT,
|
|
449
|
+
created_at TEXT NOT NULL,
|
|
450
|
+
updated_at TEXT NOT NULL,
|
|
451
|
+
PRIMARY KEY (message_id, durable_object_name)
|
|
452
|
+
)`;
|
|
453
|
+
} catch (error) {
|
|
454
|
+
console.error("[Agent] Failed to create ratings table", error);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
/**
|
|
458
|
+
* Rates a message.
|
|
459
|
+
* @param sql - The SQL function to use to execute the query.
|
|
460
|
+
* @param messageId - The ID of the message to rate.
|
|
461
|
+
* @param durable_object_name - The name of the Durable Object to rate the message for.
|
|
462
|
+
* @param rating - The rating to give the message.
|
|
463
|
+
* @param now - The date and time to use for the created_at and updated_at columns.
|
|
464
|
+
*/
|
|
465
|
+
function rateMessage(sql, messageId, durable_object_name, rating, comment, now = /* @__PURE__ */ new Date()) {
|
|
466
|
+
try {
|
|
467
|
+
sql`INSERT INTO assistant_messages_ratings (message_id, durable_object_name, rating, comment, created_at, updated_at)
|
|
468
|
+
VALUES (${messageId}, ${durable_object_name}, ${rating}, ${comment ?? null}, ${now.toISOString()}, ${now.toISOString()})
|
|
469
|
+
ON CONFLICT (message_id, durable_object_name) DO UPDATE SET
|
|
470
|
+
rating = excluded.rating,
|
|
471
|
+
comment = excluded.comment,
|
|
472
|
+
updated_at = excluded.updated_at`;
|
|
473
|
+
} catch (error) {
|
|
474
|
+
console.error("[Agent] Failed to rate message", error);
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
/**
|
|
478
|
+
* Gets the ratings for a message.
|
|
479
|
+
* @param sql - The SQL function to use to execute the query.
|
|
480
|
+
* @param durable_object_name - The name of the Durable Object to get the ratings for.
|
|
481
|
+
* @returns A record of message IDs and their ratings.
|
|
482
|
+
*/
|
|
483
|
+
function getMessageRatings(sql, durable_object_name) {
|
|
484
|
+
try {
|
|
485
|
+
const ratings = sql`SELECT message_id, rating, comment FROM assistant_messages_ratings WHERE durable_object_name = ${durable_object_name}`;
|
|
486
|
+
return Object.fromEntries(ratings.map((row) => [row.message_id, {
|
|
487
|
+
rating: row.rating,
|
|
488
|
+
comment: row.comment
|
|
489
|
+
}]));
|
|
490
|
+
} catch {
|
|
491
|
+
return {};
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
//#endregion
|
|
480
495
|
//#region src/server/v2/agents/ChatAgent.ts
|
|
481
496
|
var ChatAgent = class extends Agent {
|
|
482
497
|
initialState = {
|