@economic/agents 2.3.7 → 2.3.11
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/index.d.mts +4 -5
- package/dist/index.mjs +322 -84
- package/dist/v1.mjs +159 -6
- package/package.json +3 -1
package/dist/index.d.mts
CHANGED
|
@@ -40,7 +40,7 @@ declare function tool<Context extends Record<string, unknown> = Record<string, u
|
|
|
40
40
|
//#endregion
|
|
41
41
|
//#region src/server/agents/Agent.d.ts
|
|
42
42
|
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> {
|
|
43
|
-
|
|
43
|
+
protected agentType: AgentConnectionType;
|
|
44
44
|
protected clientIp?: string;
|
|
45
45
|
protected forwardedFor?: string;
|
|
46
46
|
/**
|
|
@@ -51,7 +51,6 @@ declare abstract class Agent<RequestContext extends Record<string, unknown> = Re
|
|
|
51
51
|
abstract getModel(ctx?: ToolContext<RequestContext, UserContext>): LanguageModel;
|
|
52
52
|
abstract getSystemPrompt(ctx?: ToolContext<RequestContext, UserContext>): string;
|
|
53
53
|
onStart(): Promise<void>;
|
|
54
|
-
onClose(): Promise<void>;
|
|
55
54
|
onConnect(connection: Connection, ctx: ConnectionContext): Promise<void>;
|
|
56
55
|
configureSession(session: Session): Session;
|
|
57
56
|
private _toolsForCurrentTurn;
|
|
@@ -182,6 +181,7 @@ declare function migrateUserFromV1(deps: MigrationDeps): Promise<{
|
|
|
182
181
|
//#endregion
|
|
183
182
|
//#region src/server/agents/ChatAgent.d.ts
|
|
184
183
|
declare abstract class ChatAgent<RequestContext extends Record<string, unknown> = Record<string, unknown>, UserContext extends Record<string, unknown> = Record<string, unknown>> extends Agent<RequestContext, UserContext> {
|
|
184
|
+
protected agentType: AgentConnectionType;
|
|
185
185
|
initialState: AgentConnectionState;
|
|
186
186
|
onStart(): Promise<void>;
|
|
187
187
|
configureSession(session: Session): Session;
|
|
@@ -225,11 +225,10 @@ declare const DELETE_CHAT_CALLBACK: "deleteChatCallback";
|
|
|
225
225
|
//#endregion
|
|
226
226
|
//#region src/server/agents/Assistant.d.ts
|
|
227
227
|
declare abstract class Assistant extends Agent$1<Cloudflare.Env & AgentEnv, AgentConnectionState> {
|
|
228
|
-
|
|
229
|
-
protected abstract agent: SubAgentClass<ChatAgent>;
|
|
228
|
+
private _fix;
|
|
230
229
|
protected abstract fastModel: LanguageModel;
|
|
230
|
+
protected abstract agent: SubAgentClass<ChatAgent>;
|
|
231
231
|
onStart(): void;
|
|
232
|
-
onClose(): Promise<void>;
|
|
233
232
|
onConnect(connection: Connection, ctx: ConnectionContext): Promise<void>;
|
|
234
233
|
createChat(): Promise<string>;
|
|
235
234
|
deleteChat(id: string): Promise<void>;
|
package/dist/index.mjs
CHANGED
|
@@ -36,12 +36,23 @@ async function registerAgentInstance(db, input) {
|
|
|
36
36
|
ON CONFLICT (agent_name, durable_object_name) DO NOTHING`).bind(input.agentName, input.durableObjectName, input.actorId, input.createdAt).run();
|
|
37
37
|
}
|
|
38
38
|
//#endregion
|
|
39
|
+
//#region src/server/util/agent.ts
|
|
40
|
+
function sendConnectionStatus(connection, status) {
|
|
41
|
+
connection.send(JSON.stringify({
|
|
42
|
+
type: "connection_status",
|
|
43
|
+
status
|
|
44
|
+
}));
|
|
45
|
+
}
|
|
46
|
+
function sendAgentType(connection, agentType) {
|
|
47
|
+
connection.send(JSON.stringify({
|
|
48
|
+
type: "agent_type",
|
|
49
|
+
agentType
|
|
50
|
+
}));
|
|
51
|
+
}
|
|
52
|
+
//#endregion
|
|
39
53
|
//#region src/server/agents/Agent.ts
|
|
40
54
|
var Agent = class extends Think {
|
|
41
|
-
|
|
42
|
-
status: "connecting",
|
|
43
|
-
type: "agent"
|
|
44
|
-
};
|
|
55
|
+
agentType = "agent";
|
|
45
56
|
clientIp;
|
|
46
57
|
forwardedFor;
|
|
47
58
|
/**
|
|
@@ -58,10 +69,6 @@ var Agent = class extends Think {
|
|
|
58
69
|
}
|
|
59
70
|
}
|
|
60
71
|
async onStart() {
|
|
61
|
-
this.setState({
|
|
62
|
-
...this.initialState,
|
|
63
|
-
status: "connecting"
|
|
64
|
-
});
|
|
65
72
|
let hasCorrectBindings = true;
|
|
66
73
|
if (!this.env.AGENTS_DB) {
|
|
67
74
|
hasCorrectBindings = false;
|
|
@@ -72,22 +79,12 @@ var Agent = class extends Think {
|
|
|
72
79
|
console.error("[Agent] Connection rejected: no AGENTS_AUDIT_LOGS bound. Audit logs are required.");
|
|
73
80
|
}
|
|
74
81
|
if (!this.env.AGENTS_ANALYTICS) console.warn("[Agent] No AGENTS_ANALYTICS bound. Analytics will not be collected.");
|
|
75
|
-
if (!hasCorrectBindings)
|
|
76
|
-
this.setState({
|
|
77
|
-
...this.initialState,
|
|
78
|
-
status: "disconnected"
|
|
79
|
-
});
|
|
80
|
-
throw new Error("Could not connect to agent, bindings not found");
|
|
81
|
-
}
|
|
82
|
+
if (!hasCorrectBindings) throw new Error("Could not connect to agent, bindings not found");
|
|
82
83
|
this.registerInstance();
|
|
83
84
|
}
|
|
84
|
-
async onClose() {
|
|
85
|
-
this.setState({
|
|
86
|
-
...this.initialState,
|
|
87
|
-
status: "disconnected"
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
85
|
async onConnect(connection, ctx) {
|
|
86
|
+
sendAgentType(connection, this.agentType);
|
|
87
|
+
sendConnectionStatus(connection, "connecting");
|
|
91
88
|
this.clientIp = ctx.request.headers.get("CF-Connecting-IP") ?? ctx.request.headers.get("X-Forwarded-For")?.split(",")[0]?.trim();
|
|
92
89
|
this.forwardedFor = ctx.request.headers.get("X-Forwarded-For") ?? void 0;
|
|
93
90
|
const getJwtAuthConfig = this.constructor.getJwtAuthConfig;
|
|
@@ -98,37 +95,24 @@ var Agent = class extends Think {
|
|
|
98
95
|
try {
|
|
99
96
|
result = await verifyJwt(ctx.request, config);
|
|
100
97
|
} catch (error) {
|
|
101
|
-
|
|
102
|
-
...this.initialState,
|
|
103
|
-
status: "unauthorized"
|
|
104
|
-
});
|
|
98
|
+
sendConnectionStatus(connection, "unauthorized");
|
|
105
99
|
console.error(`[Agent] JWT verification error - ${error}`);
|
|
106
100
|
connection.close(4001, "Unauthorized");
|
|
107
101
|
return;
|
|
108
102
|
}
|
|
109
103
|
if (!result.success) {
|
|
110
|
-
|
|
111
|
-
...this.initialState,
|
|
112
|
-
status: "unauthorized"
|
|
113
|
-
});
|
|
104
|
+
sendConnectionStatus(connection, "unauthorized");
|
|
114
105
|
console.error(`[Agent] JWT verification error - ${result.message}`);
|
|
115
106
|
connection.close(result.status === 401 ? 4001 : 4003, result.message);
|
|
116
107
|
return;
|
|
117
108
|
}
|
|
118
|
-
connection.setState({
|
|
119
|
-
authenticated: true,
|
|
120
|
-
claims: result.claims
|
|
121
|
-
});
|
|
122
109
|
const token = extractTokenFromConnectRequest(ctx.request);
|
|
123
110
|
if (token && this.getUserContext) this._pendingUserContextRequest = this.getUserContext(token).then((userContext) => {
|
|
124
111
|
this.userContext = userContext;
|
|
125
112
|
});
|
|
126
113
|
}
|
|
127
114
|
}
|
|
128
|
-
|
|
129
|
-
...this.initialState,
|
|
130
|
-
status: "connected"
|
|
131
|
-
});
|
|
115
|
+
sendConnectionStatus(connection, "connected");
|
|
132
116
|
}
|
|
133
117
|
configureSession(session) {
|
|
134
118
|
return session.withContext("soul", { provider: { get: async () => {
|
|
@@ -480,40 +464,169 @@ async function migrateUserFromV1(deps) {
|
|
|
480
464
|
}
|
|
481
465
|
//#endregion
|
|
482
466
|
//#region src/server/agents/Assistant.ts
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
467
|
+
let _initProto$1;
|
|
468
|
+
function _applyDecs$1(e, t, n, r, o, i) {
|
|
469
|
+
var a, c, u, s, f, l, p, d = Symbol.metadata || Symbol.for("Symbol.metadata"), m = Object.defineProperty, h = Object.create, y = [h(null), h(null)], v = t.length;
|
|
470
|
+
function g(t, n, r) {
|
|
471
|
+
return function(o, i) {
|
|
472
|
+
n && (i = o, o = e);
|
|
473
|
+
for (var a = 0; a < t.length; a++) i = t[a].apply(o, r ? [i] : []);
|
|
474
|
+
return r ? i : o;
|
|
475
|
+
};
|
|
476
|
+
}
|
|
477
|
+
function b(e, t, n, r) {
|
|
478
|
+
if ("function" != typeof e && (r || void 0 !== e)) throw new TypeError(t + " must " + (n || "be") + " a function" + (r ? "" : " or undefined"));
|
|
479
|
+
return e;
|
|
480
|
+
}
|
|
481
|
+
function applyDec(e, t, n, r, o, i, u, s, f, l, p) {
|
|
482
|
+
function d(e) {
|
|
483
|
+
if (!p(e)) throw new TypeError("Attempted to access private element on non-instance");
|
|
484
|
+
}
|
|
485
|
+
var h = [].concat(t[0]), v = t[3], w = !u, D = 1 === o, S = 3 === o, j = 4 === o, E = 2 === o;
|
|
486
|
+
function I(t, n, r) {
|
|
487
|
+
return function(o, i) {
|
|
488
|
+
return n && (i = o, o = e), r && r(o), P[t].call(o, i);
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
if (!w) {
|
|
492
|
+
var P = {}, k = [], F = S ? "get" : j || D ? "set" : "value";
|
|
493
|
+
if (f ? (l || D ? P = {
|
|
494
|
+
get: _setFunctionName$1(function() {
|
|
495
|
+
return v(this);
|
|
496
|
+
}, r, "get"),
|
|
497
|
+
set: function(e) {
|
|
498
|
+
t[4](this, e);
|
|
499
|
+
}
|
|
500
|
+
} : P[F] = v, l || _setFunctionName$1(P[F], r, E ? "" : F)) : l || (P = Object.getOwnPropertyDescriptor(e, r)), !l && !f) {
|
|
501
|
+
if ((c = y[+s][r]) && 7 !== (c ^ o)) throw Error("Decorating two elements with the same name (" + P[F].name + ") is not supported yet");
|
|
502
|
+
y[+s][r] = o < 3 ? 1 : o;
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
for (var N = e, O = h.length - 1; O >= 0; O -= n ? 2 : 1) {
|
|
506
|
+
var T = b(h[O], "A decorator", "be", !0), z = n ? h[O - 1] : void 0, A = {}, H = {
|
|
507
|
+
kind: [
|
|
508
|
+
"field",
|
|
509
|
+
"accessor",
|
|
510
|
+
"method",
|
|
511
|
+
"getter",
|
|
512
|
+
"setter",
|
|
513
|
+
"class"
|
|
514
|
+
][o],
|
|
515
|
+
name: r,
|
|
516
|
+
metadata: a,
|
|
517
|
+
addInitializer: function(e, t) {
|
|
518
|
+
if (e.v) throw new TypeError("attempted to call addInitializer after decoration was finished");
|
|
519
|
+
b(t, "An initializer", "be", !0), i.push(t);
|
|
520
|
+
}.bind(null, A)
|
|
521
|
+
};
|
|
522
|
+
if (w) c = T.call(z, N, H), A.v = 1, b(c, "class decorators", "return") && (N = c);
|
|
523
|
+
else if (H.static = s, H.private = f, c = H.access = { has: f ? p.bind() : function(e) {
|
|
524
|
+
return r in e;
|
|
525
|
+
} }, j || (c.get = f ? E ? function(e) {
|
|
526
|
+
return d(e), P.value;
|
|
527
|
+
} : I("get", 0, d) : function(e) {
|
|
528
|
+
return e[r];
|
|
529
|
+
}), E || S || (c.set = f ? I("set", 0, d) : function(e, t) {
|
|
530
|
+
e[r] = t;
|
|
531
|
+
}), N = T.call(z, D ? {
|
|
532
|
+
get: P.get,
|
|
533
|
+
set: P.set
|
|
534
|
+
} : P[F], H), A.v = 1, D) {
|
|
535
|
+
if ("object" == typeof N && N) (c = b(N.get, "accessor.get")) && (P.get = c), (c = b(N.set, "accessor.set")) && (P.set = c), (c = b(N.init, "accessor.init")) && k.unshift(c);
|
|
536
|
+
else if (void 0 !== N) throw new TypeError("accessor decorators must return an object with get, set, or init properties or undefined");
|
|
537
|
+
} else b(N, (l ? "field" : "method") + " decorators", "return") && (l ? k.unshift(N) : P[F] = N);
|
|
538
|
+
}
|
|
539
|
+
return o < 2 && u.push(g(k, s, 1), g(i, s, 0)), l || w || (f ? D ? u.splice(-1, 0, I("get", s), I("set", s)) : u.push(E ? P[F] : b.call.bind(P[F])) : m(e, r, P)), N;
|
|
540
|
+
}
|
|
541
|
+
function w(e) {
|
|
542
|
+
return m(e, d, {
|
|
543
|
+
configurable: !0,
|
|
544
|
+
enumerable: !0,
|
|
545
|
+
value: a
|
|
546
|
+
});
|
|
547
|
+
}
|
|
548
|
+
return void 0 !== i && (a = i[d]), a = h(null == a ? null : a), f = [], l = function(e) {
|
|
549
|
+
e && f.push(g(e));
|
|
550
|
+
}, p = function(t, r) {
|
|
551
|
+
for (var i = 0; i < n.length; i++) {
|
|
552
|
+
var a = n[i], c = a[1], l = 7 & c;
|
|
553
|
+
if ((8 & c) == t && !l == r) {
|
|
554
|
+
var p = a[2], d = !!a[3], m = 16 & c;
|
|
555
|
+
applyDec(t ? e : e.prototype, a, m, d ? "#" + p : _toPropertyKey$1(p), l, l < 2 ? [] : t ? s = s || [] : u = u || [], f, !!t, d, r, t && d ? function(t) {
|
|
556
|
+
return _checkInRHS$1(t) === e;
|
|
557
|
+
} : o);
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
}, p(8, 0), p(0, 0), p(8, 1), p(0, 1), l(u), l(s), c = f, v || w(e), {
|
|
561
|
+
e: c,
|
|
562
|
+
get c() {
|
|
563
|
+
var n = [];
|
|
564
|
+
return v && [w(e = applyDec(e, [t], r, e.name, 5, n)), g(n, 1)];
|
|
565
|
+
}
|
|
487
566
|
};
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
567
|
+
}
|
|
568
|
+
function _toPropertyKey$1(t) {
|
|
569
|
+
var i = _toPrimitive$1(t, "string");
|
|
570
|
+
return "symbol" == typeof i ? i : i + "";
|
|
571
|
+
}
|
|
572
|
+
function _toPrimitive$1(t, r) {
|
|
573
|
+
if ("object" != typeof t || !t) return t;
|
|
574
|
+
var e = t[Symbol.toPrimitive];
|
|
575
|
+
if (void 0 !== e) {
|
|
576
|
+
var i = e.call(t, r || "default");
|
|
577
|
+
if ("object" != typeof i) return i;
|
|
578
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
579
|
+
}
|
|
580
|
+
return ("string" === r ? String : Number)(t);
|
|
581
|
+
}
|
|
582
|
+
function _setFunctionName$1(e, t, n) {
|
|
583
|
+
"symbol" == typeof t && (t = (t = t.description) ? "[" + t + "]" : "");
|
|
584
|
+
try {
|
|
585
|
+
Object.defineProperty(e, "name", {
|
|
586
|
+
configurable: !0,
|
|
587
|
+
value: n ? n + " " + t : t
|
|
493
588
|
});
|
|
589
|
+
} catch (e) {}
|
|
590
|
+
return e;
|
|
591
|
+
}
|
|
592
|
+
function _checkInRHS$1(e) {
|
|
593
|
+
if (Object(e) !== e) throw TypeError("right-hand side of 'in' should be an object, got " + (null !== e ? typeof e : "null"));
|
|
594
|
+
return e;
|
|
595
|
+
}
|
|
596
|
+
var Assistant = class extends Agent$1 {
|
|
597
|
+
static {
|
|
598
|
+
[_initProto$1] = _applyDecs$1(this, [], [
|
|
599
|
+
[
|
|
600
|
+
callable(),
|
|
601
|
+
2,
|
|
602
|
+
"createChat"
|
|
603
|
+
],
|
|
604
|
+
[
|
|
605
|
+
callable(),
|
|
606
|
+
2,
|
|
607
|
+
"deleteChat"
|
|
608
|
+
],
|
|
609
|
+
[
|
|
610
|
+
callable(),
|
|
611
|
+
2,
|
|
612
|
+
"getChats"
|
|
613
|
+
]
|
|
614
|
+
], 0, void 0, Agent$1).e;
|
|
615
|
+
}
|
|
616
|
+
_fix = (_initProto$1(this), "fix");
|
|
617
|
+
onStart() {
|
|
494
618
|
ensureChatsTableExists(this.sql.bind(this));
|
|
495
619
|
let hasCorrectBindings = true;
|
|
496
620
|
if (!this.env.AGENTS_DB) {
|
|
497
621
|
hasCorrectBindings = false;
|
|
498
622
|
console.warn("[Agent] Connection rejected: no AGENTS_DB bound. Agents database is required for registration.");
|
|
499
623
|
}
|
|
500
|
-
if (!hasCorrectBindings)
|
|
501
|
-
this.setState({
|
|
502
|
-
...this.initialState,
|
|
503
|
-
status: "disconnected"
|
|
504
|
-
});
|
|
505
|
-
throw new Error("Could not connect to agent, bindings not found");
|
|
506
|
-
}
|
|
624
|
+
if (!hasCorrectBindings) throw new Error("Could not connect to agent, bindings not found");
|
|
507
625
|
this.registerInstance();
|
|
508
626
|
}
|
|
509
|
-
async onClose() {
|
|
510
|
-
this.setState({
|
|
511
|
-
...this.initialState,
|
|
512
|
-
status: "disconnected",
|
|
513
|
-
subAgentName: this.agent.name
|
|
514
|
-
});
|
|
515
|
-
}
|
|
516
627
|
async onConnect(connection, ctx) {
|
|
628
|
+
sendAgentType(connection, "assistant");
|
|
629
|
+
sendConnectionStatus(connection, "connecting");
|
|
517
630
|
const getJwtAuthConfig = this.agent.getJwtAuthConfig;
|
|
518
631
|
if (getJwtAuthConfig) {
|
|
519
632
|
const config = getJwtAuthConfig(this.env);
|
|
@@ -522,50 +635,34 @@ var Assistant = class extends Agent$1 {
|
|
|
522
635
|
try {
|
|
523
636
|
result = await verifyJwt(ctx.request, config);
|
|
524
637
|
} catch (error) {
|
|
525
|
-
|
|
526
|
-
...this.initialState,
|
|
527
|
-
status: "unauthorized",
|
|
528
|
-
subAgentName: this.agent.name
|
|
529
|
-
});
|
|
638
|
+
sendConnectionStatus(connection, "unauthorized");
|
|
530
639
|
console.error(`[Assistant] JWT verification error - ${error}`);
|
|
531
640
|
connection.close(4001, "Unauthorized");
|
|
532
641
|
return;
|
|
533
642
|
}
|
|
534
643
|
if (!result.success) {
|
|
535
|
-
|
|
536
|
-
...this.initialState,
|
|
537
|
-
status: "unauthorized",
|
|
538
|
-
subAgentName: this.agent.name
|
|
539
|
-
});
|
|
644
|
+
sendConnectionStatus(connection, "unauthorized");
|
|
540
645
|
console.error(`[Assistant] JWT verification error - ${result.message}`);
|
|
541
646
|
connection.close(result.status === 401 ? 4001 : 4003, result.message);
|
|
542
647
|
return;
|
|
543
648
|
}
|
|
544
|
-
connection.setState({
|
|
545
|
-
authenticated: true,
|
|
546
|
-
claims: result.claims
|
|
547
|
-
});
|
|
548
649
|
}
|
|
549
650
|
}
|
|
550
651
|
await this.ensureMigrated();
|
|
551
|
-
|
|
552
|
-
...this.initialState,
|
|
553
|
-
status: "connected",
|
|
554
|
-
subAgentName: this.agent.name
|
|
555
|
-
});
|
|
652
|
+
sendConnectionStatus(connection, "connected");
|
|
556
653
|
}
|
|
557
|
-
|
|
654
|
+
async createChat() {
|
|
558
655
|
const id = nanoid();
|
|
559
656
|
const now = Date.now();
|
|
560
657
|
await this.subAgent(this.agent, id);
|
|
561
658
|
registerChat(this.sql.bind(this), id, now);
|
|
562
659
|
return id;
|
|
563
660
|
}
|
|
564
|
-
|
|
661
|
+
async deleteChat(id) {
|
|
565
662
|
await this.deleteSubAgent(this.agent, id);
|
|
566
663
|
deleteChat(this.sql.bind(this), id);
|
|
567
664
|
}
|
|
568
|
-
|
|
665
|
+
async getChats() {
|
|
569
666
|
return getChats(this.sql.bind(this));
|
|
570
667
|
}
|
|
571
668
|
async recordChatTurn(durableObjectName, messages) {
|
|
@@ -708,7 +805,148 @@ function getMessageFeedback(sql) {
|
|
|
708
805
|
}
|
|
709
806
|
//#endregion
|
|
710
807
|
//#region src/server/agents/ChatAgent.ts
|
|
808
|
+
let _initProto;
|
|
809
|
+
function _applyDecs(e, t, n, r, o, i) {
|
|
810
|
+
var a, c, u, s, f, l, p, d = Symbol.metadata || Symbol.for("Symbol.metadata"), m = Object.defineProperty, h = Object.create, y = [h(null), h(null)], v = t.length;
|
|
811
|
+
function g(t, n, r) {
|
|
812
|
+
return function(o, i) {
|
|
813
|
+
n && (i = o, o = e);
|
|
814
|
+
for (var a = 0; a < t.length; a++) i = t[a].apply(o, r ? [i] : []);
|
|
815
|
+
return r ? i : o;
|
|
816
|
+
};
|
|
817
|
+
}
|
|
818
|
+
function b(e, t, n, r) {
|
|
819
|
+
if ("function" != typeof e && (r || void 0 !== e)) throw new TypeError(t + " must " + (n || "be") + " a function" + (r ? "" : " or undefined"));
|
|
820
|
+
return e;
|
|
821
|
+
}
|
|
822
|
+
function applyDec(e, t, n, r, o, i, u, s, f, l, p) {
|
|
823
|
+
function d(e) {
|
|
824
|
+
if (!p(e)) throw new TypeError("Attempted to access private element on non-instance");
|
|
825
|
+
}
|
|
826
|
+
var h = [].concat(t[0]), v = t[3], w = !u, D = 1 === o, S = 3 === o, j = 4 === o, E = 2 === o;
|
|
827
|
+
function I(t, n, r) {
|
|
828
|
+
return function(o, i) {
|
|
829
|
+
return n && (i = o, o = e), r && r(o), P[t].call(o, i);
|
|
830
|
+
};
|
|
831
|
+
}
|
|
832
|
+
if (!w) {
|
|
833
|
+
var P = {}, k = [], F = S ? "get" : j || D ? "set" : "value";
|
|
834
|
+
if (f ? (l || D ? P = {
|
|
835
|
+
get: _setFunctionName(function() {
|
|
836
|
+
return v(this);
|
|
837
|
+
}, r, "get"),
|
|
838
|
+
set: function(e) {
|
|
839
|
+
t[4](this, e);
|
|
840
|
+
}
|
|
841
|
+
} : P[F] = v, l || _setFunctionName(P[F], r, E ? "" : F)) : l || (P = Object.getOwnPropertyDescriptor(e, r)), !l && !f) {
|
|
842
|
+
if ((c = y[+s][r]) && 7 !== (c ^ o)) throw Error("Decorating two elements with the same name (" + P[F].name + ") is not supported yet");
|
|
843
|
+
y[+s][r] = o < 3 ? 1 : o;
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
for (var N = e, O = h.length - 1; O >= 0; O -= n ? 2 : 1) {
|
|
847
|
+
var T = b(h[O], "A decorator", "be", !0), z = n ? h[O - 1] : void 0, A = {}, H = {
|
|
848
|
+
kind: [
|
|
849
|
+
"field",
|
|
850
|
+
"accessor",
|
|
851
|
+
"method",
|
|
852
|
+
"getter",
|
|
853
|
+
"setter",
|
|
854
|
+
"class"
|
|
855
|
+
][o],
|
|
856
|
+
name: r,
|
|
857
|
+
metadata: a,
|
|
858
|
+
addInitializer: function(e, t) {
|
|
859
|
+
if (e.v) throw new TypeError("attempted to call addInitializer after decoration was finished");
|
|
860
|
+
b(t, "An initializer", "be", !0), i.push(t);
|
|
861
|
+
}.bind(null, A)
|
|
862
|
+
};
|
|
863
|
+
if (w) c = T.call(z, N, H), A.v = 1, b(c, "class decorators", "return") && (N = c);
|
|
864
|
+
else if (H.static = s, H.private = f, c = H.access = { has: f ? p.bind() : function(e) {
|
|
865
|
+
return r in e;
|
|
866
|
+
} }, j || (c.get = f ? E ? function(e) {
|
|
867
|
+
return d(e), P.value;
|
|
868
|
+
} : I("get", 0, d) : function(e) {
|
|
869
|
+
return e[r];
|
|
870
|
+
}), E || S || (c.set = f ? I("set", 0, d) : function(e, t) {
|
|
871
|
+
e[r] = t;
|
|
872
|
+
}), N = T.call(z, D ? {
|
|
873
|
+
get: P.get,
|
|
874
|
+
set: P.set
|
|
875
|
+
} : P[F], H), A.v = 1, D) {
|
|
876
|
+
if ("object" == typeof N && N) (c = b(N.get, "accessor.get")) && (P.get = c), (c = b(N.set, "accessor.set")) && (P.set = c), (c = b(N.init, "accessor.init")) && k.unshift(c);
|
|
877
|
+
else if (void 0 !== N) throw new TypeError("accessor decorators must return an object with get, set, or init properties or undefined");
|
|
878
|
+
} else b(N, (l ? "field" : "method") + " decorators", "return") && (l ? k.unshift(N) : P[F] = N);
|
|
879
|
+
}
|
|
880
|
+
return o < 2 && u.push(g(k, s, 1), g(i, s, 0)), l || w || (f ? D ? u.splice(-1, 0, I("get", s), I("set", s)) : u.push(E ? P[F] : b.call.bind(P[F])) : m(e, r, P)), N;
|
|
881
|
+
}
|
|
882
|
+
function w(e) {
|
|
883
|
+
return m(e, d, {
|
|
884
|
+
configurable: !0,
|
|
885
|
+
enumerable: !0,
|
|
886
|
+
value: a
|
|
887
|
+
});
|
|
888
|
+
}
|
|
889
|
+
return void 0 !== i && (a = i[d]), a = h(null == a ? null : a), f = [], l = function(e) {
|
|
890
|
+
e && f.push(g(e));
|
|
891
|
+
}, p = function(t, r) {
|
|
892
|
+
for (var i = 0; i < n.length; i++) {
|
|
893
|
+
var a = n[i], c = a[1], l = 7 & c;
|
|
894
|
+
if ((8 & c) == t && !l == r) {
|
|
895
|
+
var p = a[2], d = !!a[3], m = 16 & c;
|
|
896
|
+
applyDec(t ? e : e.prototype, a, m, d ? "#" + p : _toPropertyKey(p), l, l < 2 ? [] : t ? s = s || [] : u = u || [], f, !!t, d, r, t && d ? function(t) {
|
|
897
|
+
return _checkInRHS(t) === e;
|
|
898
|
+
} : o);
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
}, p(8, 0), p(0, 0), p(8, 1), p(0, 1), l(u), l(s), c = f, v || w(e), {
|
|
902
|
+
e: c,
|
|
903
|
+
get c() {
|
|
904
|
+
var n = [];
|
|
905
|
+
return v && [w(e = applyDec(e, [t], r, e.name, 5, n)), g(n, 1)];
|
|
906
|
+
}
|
|
907
|
+
};
|
|
908
|
+
}
|
|
909
|
+
function _toPropertyKey(t) {
|
|
910
|
+
var i = _toPrimitive(t, "string");
|
|
911
|
+
return "symbol" == typeof i ? i : i + "";
|
|
912
|
+
}
|
|
913
|
+
function _toPrimitive(t, r) {
|
|
914
|
+
if ("object" != typeof t || !t) return t;
|
|
915
|
+
var e = t[Symbol.toPrimitive];
|
|
916
|
+
if (void 0 !== e) {
|
|
917
|
+
var i = e.call(t, r || "default");
|
|
918
|
+
if ("object" != typeof i) return i;
|
|
919
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
920
|
+
}
|
|
921
|
+
return ("string" === r ? String : Number)(t);
|
|
922
|
+
}
|
|
923
|
+
function _setFunctionName(e, t, n) {
|
|
924
|
+
"symbol" == typeof t && (t = (t = t.description) ? "[" + t + "]" : "");
|
|
925
|
+
try {
|
|
926
|
+
Object.defineProperty(e, "name", {
|
|
927
|
+
configurable: !0,
|
|
928
|
+
value: n ? n + " " + t : t
|
|
929
|
+
});
|
|
930
|
+
} catch (e) {}
|
|
931
|
+
return e;
|
|
932
|
+
}
|
|
933
|
+
function _checkInRHS(e) {
|
|
934
|
+
if (Object(e) !== e) throw TypeError("right-hand side of 'in' should be an object, got " + (null !== e ? typeof e : "null"));
|
|
935
|
+
return e;
|
|
936
|
+
}
|
|
711
937
|
var ChatAgent = class extends Agent {
|
|
938
|
+
static {
|
|
939
|
+
[_initProto] = _applyDecs(this, [], [[
|
|
940
|
+
callable({ description: "Submit feedback for a message by its id" }),
|
|
941
|
+
2,
|
|
942
|
+
"submitMessageFeedback"
|
|
943
|
+
], [
|
|
944
|
+
callable({ description: "Returns all message feedback for the current chat" }),
|
|
945
|
+
2,
|
|
946
|
+
"getMessageFeedback"
|
|
947
|
+
]], 0, void 0, Agent).e;
|
|
948
|
+
}
|
|
949
|
+
agentType = (_initProto(this), "chat");
|
|
712
950
|
initialState = {
|
|
713
951
|
status: "connecting",
|
|
714
952
|
type: "chat"
|
|
@@ -730,14 +968,14 @@ var ChatAgent = class extends Agent {
|
|
|
730
968
|
* @param rating - The rating to give the message. 1 = thumbs up, -1 = thumbs down.
|
|
731
969
|
* @returns The message id and the rating.
|
|
732
970
|
*/
|
|
733
|
-
|
|
971
|
+
async submitMessageFeedback(messageId, rating, comment) {
|
|
734
972
|
return submitMessageFeedback(this.sql.bind(this), messageId, rating, comment);
|
|
735
973
|
}
|
|
736
974
|
/**
|
|
737
975
|
* Returns all message feedback for the current chat.
|
|
738
976
|
* @returns All message feedback for the current chat.
|
|
739
977
|
*/
|
|
740
|
-
|
|
978
|
+
async getMessageFeedback() {
|
|
741
979
|
return getMessageFeedback(this.sql.bind(this));
|
|
742
980
|
}
|
|
743
981
|
/**
|
package/dist/v1.mjs
CHANGED
|
@@ -642,6 +642,135 @@ async function getMessageRatings(db, durableObjectName) {
|
|
|
642
642
|
}
|
|
643
643
|
//#endregion
|
|
644
644
|
//#region src/server/v1/agent-chat/ChatAgent.ts
|
|
645
|
+
let _initProto;
|
|
646
|
+
function _applyDecs(e, t, n, r, o, i) {
|
|
647
|
+
var a, c, u, s, f, l, p, d = Symbol.metadata || Symbol.for("Symbol.metadata"), m = Object.defineProperty, h = Object.create, y = [h(null), h(null)], v = t.length;
|
|
648
|
+
function g(t, n, r) {
|
|
649
|
+
return function(o, i) {
|
|
650
|
+
n && (i = o, o = e);
|
|
651
|
+
for (var a = 0; a < t.length; a++) i = t[a].apply(o, r ? [i] : []);
|
|
652
|
+
return r ? i : o;
|
|
653
|
+
};
|
|
654
|
+
}
|
|
655
|
+
function b(e, t, n, r) {
|
|
656
|
+
if ("function" != typeof e && (r || void 0 !== e)) throw new TypeError(t + " must " + (n || "be") + " a function" + (r ? "" : " or undefined"));
|
|
657
|
+
return e;
|
|
658
|
+
}
|
|
659
|
+
function applyDec(e, t, n, r, o, i, u, s, f, l, p) {
|
|
660
|
+
function d(e) {
|
|
661
|
+
if (!p(e)) throw new TypeError("Attempted to access private element on non-instance");
|
|
662
|
+
}
|
|
663
|
+
var h = [].concat(t[0]), v = t[3], w = !u, D = 1 === o, S = 3 === o, j = 4 === o, E = 2 === o;
|
|
664
|
+
function I(t, n, r) {
|
|
665
|
+
return function(o, i) {
|
|
666
|
+
return n && (i = o, o = e), r && r(o), P[t].call(o, i);
|
|
667
|
+
};
|
|
668
|
+
}
|
|
669
|
+
if (!w) {
|
|
670
|
+
var P = {}, k = [], F = S ? "get" : j || D ? "set" : "value";
|
|
671
|
+
if (f ? (l || D ? P = {
|
|
672
|
+
get: _setFunctionName(function() {
|
|
673
|
+
return v(this);
|
|
674
|
+
}, r, "get"),
|
|
675
|
+
set: function(e) {
|
|
676
|
+
t[4](this, e);
|
|
677
|
+
}
|
|
678
|
+
} : P[F] = v, l || _setFunctionName(P[F], r, E ? "" : F)) : l || (P = Object.getOwnPropertyDescriptor(e, r)), !l && !f) {
|
|
679
|
+
if ((c = y[+s][r]) && 7 !== (c ^ o)) throw Error("Decorating two elements with the same name (" + P[F].name + ") is not supported yet");
|
|
680
|
+
y[+s][r] = o < 3 ? 1 : o;
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
for (var N = e, O = h.length - 1; O >= 0; O -= n ? 2 : 1) {
|
|
684
|
+
var T = b(h[O], "A decorator", "be", !0), z = n ? h[O - 1] : void 0, A = {}, H = {
|
|
685
|
+
kind: [
|
|
686
|
+
"field",
|
|
687
|
+
"accessor",
|
|
688
|
+
"method",
|
|
689
|
+
"getter",
|
|
690
|
+
"setter",
|
|
691
|
+
"class"
|
|
692
|
+
][o],
|
|
693
|
+
name: r,
|
|
694
|
+
metadata: a,
|
|
695
|
+
addInitializer: function(e, t) {
|
|
696
|
+
if (e.v) throw new TypeError("attempted to call addInitializer after decoration was finished");
|
|
697
|
+
b(t, "An initializer", "be", !0), i.push(t);
|
|
698
|
+
}.bind(null, A)
|
|
699
|
+
};
|
|
700
|
+
if (w) c = T.call(z, N, H), A.v = 1, b(c, "class decorators", "return") && (N = c);
|
|
701
|
+
else if (H.static = s, H.private = f, c = H.access = { has: f ? p.bind() : function(e) {
|
|
702
|
+
return r in e;
|
|
703
|
+
} }, j || (c.get = f ? E ? function(e) {
|
|
704
|
+
return d(e), P.value;
|
|
705
|
+
} : I("get", 0, d) : function(e) {
|
|
706
|
+
return e[r];
|
|
707
|
+
}), E || S || (c.set = f ? I("set", 0, d) : function(e, t) {
|
|
708
|
+
e[r] = t;
|
|
709
|
+
}), N = T.call(z, D ? {
|
|
710
|
+
get: P.get,
|
|
711
|
+
set: P.set
|
|
712
|
+
} : P[F], H), A.v = 1, D) {
|
|
713
|
+
if ("object" == typeof N && N) (c = b(N.get, "accessor.get")) && (P.get = c), (c = b(N.set, "accessor.set")) && (P.set = c), (c = b(N.init, "accessor.init")) && k.unshift(c);
|
|
714
|
+
else if (void 0 !== N) throw new TypeError("accessor decorators must return an object with get, set, or init properties or undefined");
|
|
715
|
+
} else b(N, (l ? "field" : "method") + " decorators", "return") && (l ? k.unshift(N) : P[F] = N);
|
|
716
|
+
}
|
|
717
|
+
return o < 2 && u.push(g(k, s, 1), g(i, s, 0)), l || w || (f ? D ? u.splice(-1, 0, I("get", s), I("set", s)) : u.push(E ? P[F] : b.call.bind(P[F])) : m(e, r, P)), N;
|
|
718
|
+
}
|
|
719
|
+
function w(e) {
|
|
720
|
+
return m(e, d, {
|
|
721
|
+
configurable: !0,
|
|
722
|
+
enumerable: !0,
|
|
723
|
+
value: a
|
|
724
|
+
});
|
|
725
|
+
}
|
|
726
|
+
return void 0 !== i && (a = i[d]), a = h(null == a ? null : a), f = [], l = function(e) {
|
|
727
|
+
e && f.push(g(e));
|
|
728
|
+
}, p = function(t, r) {
|
|
729
|
+
for (var i = 0; i < n.length; i++) {
|
|
730
|
+
var a = n[i], c = a[1], l = 7 & c;
|
|
731
|
+
if ((8 & c) == t && !l == r) {
|
|
732
|
+
var p = a[2], d = !!a[3], m = 16 & c;
|
|
733
|
+
applyDec(t ? e : e.prototype, a, m, d ? "#" + p : _toPropertyKey(p), l, l < 2 ? [] : t ? s = s || [] : u = u || [], f, !!t, d, r, t && d ? function(t) {
|
|
734
|
+
return _checkInRHS(t) === e;
|
|
735
|
+
} : o);
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
}, p(8, 0), p(0, 0), p(8, 1), p(0, 1), l(u), l(s), c = f, v || w(e), {
|
|
739
|
+
e: c,
|
|
740
|
+
get c() {
|
|
741
|
+
var n = [];
|
|
742
|
+
return v && [w(e = applyDec(e, [t], r, e.name, 5, n)), g(n, 1)];
|
|
743
|
+
}
|
|
744
|
+
};
|
|
745
|
+
}
|
|
746
|
+
function _toPropertyKey(t) {
|
|
747
|
+
var i = _toPrimitive(t, "string");
|
|
748
|
+
return "symbol" == typeof i ? i : i + "";
|
|
749
|
+
}
|
|
750
|
+
function _toPrimitive(t, r) {
|
|
751
|
+
if ("object" != typeof t || !t) return t;
|
|
752
|
+
var e = t[Symbol.toPrimitive];
|
|
753
|
+
if (void 0 !== e) {
|
|
754
|
+
var i = e.call(t, r || "default");
|
|
755
|
+
if ("object" != typeof i) return i;
|
|
756
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
757
|
+
}
|
|
758
|
+
return ("string" === r ? String : Number)(t);
|
|
759
|
+
}
|
|
760
|
+
function _setFunctionName(e, t, n) {
|
|
761
|
+
"symbol" == typeof t && (t = (t = t.description) ? "[" + t + "]" : "");
|
|
762
|
+
try {
|
|
763
|
+
Object.defineProperty(e, "name", {
|
|
764
|
+
configurable: !0,
|
|
765
|
+
value: n ? n + " " + t : t
|
|
766
|
+
});
|
|
767
|
+
} catch (e) {}
|
|
768
|
+
return e;
|
|
769
|
+
}
|
|
770
|
+
function _checkInRHS(e) {
|
|
771
|
+
if (Object(e) !== e) throw TypeError("right-hand side of 'in' should be an object, got " + (null !== e ? typeof e : "null"));
|
|
772
|
+
return e;
|
|
773
|
+
}
|
|
645
774
|
/**
|
|
646
775
|
* Chat agent for Cloudflare Agents SDK: lazy skill loading, message persistence,
|
|
647
776
|
* compaction, and conversation metadata in D1.
|
|
@@ -654,10 +783,34 @@ async function getMessageRatings(db, durableObjectName) {
|
|
|
654
783
|
* `@economic/agents` inside `onChatMessage`.
|
|
655
784
|
*/
|
|
656
785
|
var ChatAgent = class extends AIChatAgent {
|
|
657
|
-
|
|
786
|
+
static {
|
|
787
|
+
[_initProto] = _applyDecs(this, [], [
|
|
788
|
+
[
|
|
789
|
+
callable({ description: "Rate a message by its id" }),
|
|
790
|
+
2,
|
|
791
|
+
"rateMessage"
|
|
792
|
+
],
|
|
793
|
+
[
|
|
794
|
+
callable({ description: "Returns all message ratings for the current conversation" }),
|
|
795
|
+
2,
|
|
796
|
+
"getMessageRatings"
|
|
797
|
+
],
|
|
798
|
+
[
|
|
799
|
+
callable({ description: "Returns all conversations for the current user" }),
|
|
800
|
+
2,
|
|
801
|
+
"getConversations"
|
|
802
|
+
],
|
|
803
|
+
[
|
|
804
|
+
callable({ description: "Delete a conversation by its id" }),
|
|
805
|
+
2,
|
|
806
|
+
"deleteConversation"
|
|
807
|
+
]
|
|
808
|
+
], 0, void 0, AIChatAgent).e;
|
|
809
|
+
}
|
|
810
|
+
initialState = (_initProto(this), {
|
|
658
811
|
status: "connecting",
|
|
659
812
|
type: "chat"
|
|
660
|
-
};
|
|
813
|
+
});
|
|
661
814
|
/**
|
|
662
815
|
* Number of days of inactivity before the full conversation is deleted.
|
|
663
816
|
*
|
|
@@ -815,13 +968,13 @@ var ChatAgent = class extends AIChatAgent {
|
|
|
815
968
|
recordConversationSummary(this.env.AGENT_DB, this.name, this.messages, this.getFastModel());
|
|
816
969
|
this.scheduleConversationForAutoDeletion();
|
|
817
970
|
}
|
|
818
|
-
|
|
971
|
+
async rateMessage(messageId, rating, comment) {
|
|
819
972
|
await rateMessage(this.env.AGENT_DB, this.name, messageId, rating, comment);
|
|
820
973
|
}
|
|
821
|
-
|
|
974
|
+
async getMessageRatings() {
|
|
822
975
|
return getMessageRatings(this.env.AGENT_DB, this.name);
|
|
823
976
|
}
|
|
824
|
-
|
|
977
|
+
async getConversations() {
|
|
825
978
|
return getConversations(this.env.AGENT_DB, this.getUserId());
|
|
826
979
|
}
|
|
827
980
|
/**
|
|
@@ -835,7 +988,7 @@ var ChatAgent = class extends AIChatAgent {
|
|
|
835
988
|
async exportForMigration() {
|
|
836
989
|
return { messages: this.messages };
|
|
837
990
|
}
|
|
838
|
-
|
|
991
|
+
async deleteConversation(id) {
|
|
839
992
|
if (!id.startsWith(`${this.getUserId()}:`)) {
|
|
840
993
|
console.error("[Agent] Failed to delete conversation: Not owned by current user", {
|
|
841
994
|
conversationName: id,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@economic/agents",
|
|
3
|
-
"version": "2.3.
|
|
3
|
+
"version": "2.3.11",
|
|
4
4
|
"description": "A starter for creating a TypeScript package.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"files": [
|
|
@@ -34,6 +34,8 @@
|
|
|
34
34
|
"nanoid": "^5.1.11"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
|
+
"@babel/core": "^7.29.7",
|
|
38
|
+
"@babel/plugin-proposal-decorators": "^7.29.7",
|
|
37
39
|
"@cloudflare/workers-types": "^4.20260527.1",
|
|
38
40
|
"@rolldown/plugin-babel": "^0.2.3",
|
|
39
41
|
"@types/node": "^25.6.0",
|