@economic/agents 2.3.6 → 2.3.9
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
CHANGED
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { n as JwtAuthConfig, t as routeAgentRequest } from "./route-agent-request-
|
|
1
|
+
import { n as JwtAuthConfig, t as routeAgentRequest } from "./route-agent-request-D2ll--nF.mjs";
|
|
2
2
|
import { Agent as Agent$1, Connection, ConnectionContext, SubAgentClass } from "agents";
|
|
3
3
|
import { ChatResponseResult, PrepareStepContext, Session, Think, ToolCallContext, ToolCallDecision, TurnConfig, TurnContext, skills } from "@cloudflare/think";
|
|
4
4
|
import { JSONValue, LanguageModel, Tool as Tool$1, UIMessage } from "ai";
|
package/dist/index.mjs
CHANGED
|
@@ -480,11 +480,159 @@ async function migrateUserFromV1(deps) {
|
|
|
480
480
|
}
|
|
481
481
|
//#endregion
|
|
482
482
|
//#region src/server/agents/Assistant.ts
|
|
483
|
+
let _initProto$1;
|
|
484
|
+
function _applyDecs$1(e, t, n, r, o, i) {
|
|
485
|
+
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;
|
|
486
|
+
function g(t, n, r) {
|
|
487
|
+
return function(o, i) {
|
|
488
|
+
n && (i = o, o = e);
|
|
489
|
+
for (var a = 0; a < t.length; a++) i = t[a].apply(o, r ? [i] : []);
|
|
490
|
+
return r ? i : o;
|
|
491
|
+
};
|
|
492
|
+
}
|
|
493
|
+
function b(e, t, n, r) {
|
|
494
|
+
if ("function" != typeof e && (r || void 0 !== e)) throw new TypeError(t + " must " + (n || "be") + " a function" + (r ? "" : " or undefined"));
|
|
495
|
+
return e;
|
|
496
|
+
}
|
|
497
|
+
function applyDec(e, t, n, r, o, i, u, s, f, l, p) {
|
|
498
|
+
function d(e) {
|
|
499
|
+
if (!p(e)) throw new TypeError("Attempted to access private element on non-instance");
|
|
500
|
+
}
|
|
501
|
+
var h = [].concat(t[0]), v = t[3], w = !u, D = 1 === o, S = 3 === o, j = 4 === o, E = 2 === o;
|
|
502
|
+
function I(t, n, r) {
|
|
503
|
+
return function(o, i) {
|
|
504
|
+
return n && (i = o, o = e), r && r(o), P[t].call(o, i);
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
if (!w) {
|
|
508
|
+
var P = {}, k = [], F = S ? "get" : j || D ? "set" : "value";
|
|
509
|
+
if (f ? (l || D ? P = {
|
|
510
|
+
get: _setFunctionName$1(function() {
|
|
511
|
+
return v(this);
|
|
512
|
+
}, r, "get"),
|
|
513
|
+
set: function(e) {
|
|
514
|
+
t[4](this, e);
|
|
515
|
+
}
|
|
516
|
+
} : P[F] = v, l || _setFunctionName$1(P[F], r, E ? "" : F)) : l || (P = Object.getOwnPropertyDescriptor(e, r)), !l && !f) {
|
|
517
|
+
if ((c = y[+s][r]) && 7 !== (c ^ o)) throw Error("Decorating two elements with the same name (" + P[F].name + ") is not supported yet");
|
|
518
|
+
y[+s][r] = o < 3 ? 1 : o;
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
for (var N = e, O = h.length - 1; O >= 0; O -= n ? 2 : 1) {
|
|
522
|
+
var T = b(h[O], "A decorator", "be", !0), z = n ? h[O - 1] : void 0, A = {}, H = {
|
|
523
|
+
kind: [
|
|
524
|
+
"field",
|
|
525
|
+
"accessor",
|
|
526
|
+
"method",
|
|
527
|
+
"getter",
|
|
528
|
+
"setter",
|
|
529
|
+
"class"
|
|
530
|
+
][o],
|
|
531
|
+
name: r,
|
|
532
|
+
metadata: a,
|
|
533
|
+
addInitializer: function(e, t) {
|
|
534
|
+
if (e.v) throw new TypeError("attempted to call addInitializer after decoration was finished");
|
|
535
|
+
b(t, "An initializer", "be", !0), i.push(t);
|
|
536
|
+
}.bind(null, A)
|
|
537
|
+
};
|
|
538
|
+
if (w) c = T.call(z, N, H), A.v = 1, b(c, "class decorators", "return") && (N = c);
|
|
539
|
+
else if (H.static = s, H.private = f, c = H.access = { has: f ? p.bind() : function(e) {
|
|
540
|
+
return r in e;
|
|
541
|
+
} }, j || (c.get = f ? E ? function(e) {
|
|
542
|
+
return d(e), P.value;
|
|
543
|
+
} : I("get", 0, d) : function(e) {
|
|
544
|
+
return e[r];
|
|
545
|
+
}), E || S || (c.set = f ? I("set", 0, d) : function(e, t) {
|
|
546
|
+
e[r] = t;
|
|
547
|
+
}), N = T.call(z, D ? {
|
|
548
|
+
get: P.get,
|
|
549
|
+
set: P.set
|
|
550
|
+
} : P[F], H), A.v = 1, D) {
|
|
551
|
+
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);
|
|
552
|
+
else if (void 0 !== N) throw new TypeError("accessor decorators must return an object with get, set, or init properties or undefined");
|
|
553
|
+
} else b(N, (l ? "field" : "method") + " decorators", "return") && (l ? k.unshift(N) : P[F] = N);
|
|
554
|
+
}
|
|
555
|
+
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;
|
|
556
|
+
}
|
|
557
|
+
function w(e) {
|
|
558
|
+
return m(e, d, {
|
|
559
|
+
configurable: !0,
|
|
560
|
+
enumerable: !0,
|
|
561
|
+
value: a
|
|
562
|
+
});
|
|
563
|
+
}
|
|
564
|
+
return void 0 !== i && (a = i[d]), a = h(null == a ? null : a), f = [], l = function(e) {
|
|
565
|
+
e && f.push(g(e));
|
|
566
|
+
}, p = function(t, r) {
|
|
567
|
+
for (var i = 0; i < n.length; i++) {
|
|
568
|
+
var a = n[i], c = a[1], l = 7 & c;
|
|
569
|
+
if ((8 & c) == t && !l == r) {
|
|
570
|
+
var p = a[2], d = !!a[3], m = 16 & c;
|
|
571
|
+
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) {
|
|
572
|
+
return _checkInRHS$1(t) === e;
|
|
573
|
+
} : o);
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
}, p(8, 0), p(0, 0), p(8, 1), p(0, 1), l(u), l(s), c = f, v || w(e), {
|
|
577
|
+
e: c,
|
|
578
|
+
get c() {
|
|
579
|
+
var n = [];
|
|
580
|
+
return v && [w(e = applyDec(e, [t], r, e.name, 5, n)), g(n, 1)];
|
|
581
|
+
}
|
|
582
|
+
};
|
|
583
|
+
}
|
|
584
|
+
function _toPropertyKey$1(t) {
|
|
585
|
+
var i = _toPrimitive$1(t, "string");
|
|
586
|
+
return "symbol" == typeof i ? i : i + "";
|
|
587
|
+
}
|
|
588
|
+
function _toPrimitive$1(t, r) {
|
|
589
|
+
if ("object" != typeof t || !t) return t;
|
|
590
|
+
var e = t[Symbol.toPrimitive];
|
|
591
|
+
if (void 0 !== e) {
|
|
592
|
+
var i = e.call(t, r || "default");
|
|
593
|
+
if ("object" != typeof i) return i;
|
|
594
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
595
|
+
}
|
|
596
|
+
return ("string" === r ? String : Number)(t);
|
|
597
|
+
}
|
|
598
|
+
function _setFunctionName$1(e, t, n) {
|
|
599
|
+
"symbol" == typeof t && (t = (t = t.description) ? "[" + t + "]" : "");
|
|
600
|
+
try {
|
|
601
|
+
Object.defineProperty(e, "name", {
|
|
602
|
+
configurable: !0,
|
|
603
|
+
value: n ? n + " " + t : t
|
|
604
|
+
});
|
|
605
|
+
} catch (e) {}
|
|
606
|
+
return e;
|
|
607
|
+
}
|
|
608
|
+
function _checkInRHS$1(e) {
|
|
609
|
+
if (Object(e) !== e) throw TypeError("right-hand side of 'in' should be an object, got " + (null !== e ? typeof e : "null"));
|
|
610
|
+
return e;
|
|
611
|
+
}
|
|
483
612
|
var Assistant = class extends Agent$1 {
|
|
484
|
-
|
|
613
|
+
static {
|
|
614
|
+
[_initProto$1] = _applyDecs$1(this, [], [
|
|
615
|
+
[
|
|
616
|
+
callable(),
|
|
617
|
+
2,
|
|
618
|
+
"createChat"
|
|
619
|
+
],
|
|
620
|
+
[
|
|
621
|
+
callable(),
|
|
622
|
+
2,
|
|
623
|
+
"deleteChat"
|
|
624
|
+
],
|
|
625
|
+
[
|
|
626
|
+
callable(),
|
|
627
|
+
2,
|
|
628
|
+
"getChats"
|
|
629
|
+
]
|
|
630
|
+
], 0, void 0, Agent$1).e;
|
|
631
|
+
}
|
|
632
|
+
initialState = (_initProto$1(this), {
|
|
485
633
|
status: "connecting",
|
|
486
634
|
type: "assistant"
|
|
487
|
-
};
|
|
635
|
+
});
|
|
488
636
|
onStart() {
|
|
489
637
|
this.setState({
|
|
490
638
|
...this.initialState,
|
|
@@ -554,18 +702,18 @@ var Assistant = class extends Agent$1 {
|
|
|
554
702
|
subAgentName: this.agent.name
|
|
555
703
|
});
|
|
556
704
|
}
|
|
557
|
-
|
|
705
|
+
async createChat() {
|
|
558
706
|
const id = nanoid();
|
|
559
707
|
const now = Date.now();
|
|
560
708
|
await this.subAgent(this.agent, id);
|
|
561
709
|
registerChat(this.sql.bind(this), id, now);
|
|
562
710
|
return id;
|
|
563
711
|
}
|
|
564
|
-
|
|
712
|
+
async deleteChat(id) {
|
|
565
713
|
await this.deleteSubAgent(this.agent, id);
|
|
566
714
|
deleteChat(this.sql.bind(this), id);
|
|
567
715
|
}
|
|
568
|
-
|
|
716
|
+
async getChats() {
|
|
569
717
|
return getChats(this.sql.bind(this));
|
|
570
718
|
}
|
|
571
719
|
async recordChatTurn(durableObjectName, messages) {
|
|
@@ -708,11 +856,151 @@ function getMessageFeedback(sql) {
|
|
|
708
856
|
}
|
|
709
857
|
//#endregion
|
|
710
858
|
//#region src/server/agents/ChatAgent.ts
|
|
859
|
+
let _initProto;
|
|
860
|
+
function _applyDecs(e, t, n, r, o, i) {
|
|
861
|
+
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;
|
|
862
|
+
function g(t, n, r) {
|
|
863
|
+
return function(o, i) {
|
|
864
|
+
n && (i = o, o = e);
|
|
865
|
+
for (var a = 0; a < t.length; a++) i = t[a].apply(o, r ? [i] : []);
|
|
866
|
+
return r ? i : o;
|
|
867
|
+
};
|
|
868
|
+
}
|
|
869
|
+
function b(e, t, n, r) {
|
|
870
|
+
if ("function" != typeof e && (r || void 0 !== e)) throw new TypeError(t + " must " + (n || "be") + " a function" + (r ? "" : " or undefined"));
|
|
871
|
+
return e;
|
|
872
|
+
}
|
|
873
|
+
function applyDec(e, t, n, r, o, i, u, s, f, l, p) {
|
|
874
|
+
function d(e) {
|
|
875
|
+
if (!p(e)) throw new TypeError("Attempted to access private element on non-instance");
|
|
876
|
+
}
|
|
877
|
+
var h = [].concat(t[0]), v = t[3], w = !u, D = 1 === o, S = 3 === o, j = 4 === o, E = 2 === o;
|
|
878
|
+
function I(t, n, r) {
|
|
879
|
+
return function(o, i) {
|
|
880
|
+
return n && (i = o, o = e), r && r(o), P[t].call(o, i);
|
|
881
|
+
};
|
|
882
|
+
}
|
|
883
|
+
if (!w) {
|
|
884
|
+
var P = {}, k = [], F = S ? "get" : j || D ? "set" : "value";
|
|
885
|
+
if (f ? (l || D ? P = {
|
|
886
|
+
get: _setFunctionName(function() {
|
|
887
|
+
return v(this);
|
|
888
|
+
}, r, "get"),
|
|
889
|
+
set: function(e) {
|
|
890
|
+
t[4](this, e);
|
|
891
|
+
}
|
|
892
|
+
} : P[F] = v, l || _setFunctionName(P[F], r, E ? "" : F)) : l || (P = Object.getOwnPropertyDescriptor(e, r)), !l && !f) {
|
|
893
|
+
if ((c = y[+s][r]) && 7 !== (c ^ o)) throw Error("Decorating two elements with the same name (" + P[F].name + ") is not supported yet");
|
|
894
|
+
y[+s][r] = o < 3 ? 1 : o;
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
for (var N = e, O = h.length - 1; O >= 0; O -= n ? 2 : 1) {
|
|
898
|
+
var T = b(h[O], "A decorator", "be", !0), z = n ? h[O - 1] : void 0, A = {}, H = {
|
|
899
|
+
kind: [
|
|
900
|
+
"field",
|
|
901
|
+
"accessor",
|
|
902
|
+
"method",
|
|
903
|
+
"getter",
|
|
904
|
+
"setter",
|
|
905
|
+
"class"
|
|
906
|
+
][o],
|
|
907
|
+
name: r,
|
|
908
|
+
metadata: a,
|
|
909
|
+
addInitializer: function(e, t) {
|
|
910
|
+
if (e.v) throw new TypeError("attempted to call addInitializer after decoration was finished");
|
|
911
|
+
b(t, "An initializer", "be", !0), i.push(t);
|
|
912
|
+
}.bind(null, A)
|
|
913
|
+
};
|
|
914
|
+
if (w) c = T.call(z, N, H), A.v = 1, b(c, "class decorators", "return") && (N = c);
|
|
915
|
+
else if (H.static = s, H.private = f, c = H.access = { has: f ? p.bind() : function(e) {
|
|
916
|
+
return r in e;
|
|
917
|
+
} }, j || (c.get = f ? E ? function(e) {
|
|
918
|
+
return d(e), P.value;
|
|
919
|
+
} : I("get", 0, d) : function(e) {
|
|
920
|
+
return e[r];
|
|
921
|
+
}), E || S || (c.set = f ? I("set", 0, d) : function(e, t) {
|
|
922
|
+
e[r] = t;
|
|
923
|
+
}), N = T.call(z, D ? {
|
|
924
|
+
get: P.get,
|
|
925
|
+
set: P.set
|
|
926
|
+
} : P[F], H), A.v = 1, D) {
|
|
927
|
+
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);
|
|
928
|
+
else if (void 0 !== N) throw new TypeError("accessor decorators must return an object with get, set, or init properties or undefined");
|
|
929
|
+
} else b(N, (l ? "field" : "method") + " decorators", "return") && (l ? k.unshift(N) : P[F] = N);
|
|
930
|
+
}
|
|
931
|
+
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;
|
|
932
|
+
}
|
|
933
|
+
function w(e) {
|
|
934
|
+
return m(e, d, {
|
|
935
|
+
configurable: !0,
|
|
936
|
+
enumerable: !0,
|
|
937
|
+
value: a
|
|
938
|
+
});
|
|
939
|
+
}
|
|
940
|
+
return void 0 !== i && (a = i[d]), a = h(null == a ? null : a), f = [], l = function(e) {
|
|
941
|
+
e && f.push(g(e));
|
|
942
|
+
}, p = function(t, r) {
|
|
943
|
+
for (var i = 0; i < n.length; i++) {
|
|
944
|
+
var a = n[i], c = a[1], l = 7 & c;
|
|
945
|
+
if ((8 & c) == t && !l == r) {
|
|
946
|
+
var p = a[2], d = !!a[3], m = 16 & c;
|
|
947
|
+
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) {
|
|
948
|
+
return _checkInRHS(t) === e;
|
|
949
|
+
} : o);
|
|
950
|
+
}
|
|
951
|
+
}
|
|
952
|
+
}, p(8, 0), p(0, 0), p(8, 1), p(0, 1), l(u), l(s), c = f, v || w(e), {
|
|
953
|
+
e: c,
|
|
954
|
+
get c() {
|
|
955
|
+
var n = [];
|
|
956
|
+
return v && [w(e = applyDec(e, [t], r, e.name, 5, n)), g(n, 1)];
|
|
957
|
+
}
|
|
958
|
+
};
|
|
959
|
+
}
|
|
960
|
+
function _toPropertyKey(t) {
|
|
961
|
+
var i = _toPrimitive(t, "string");
|
|
962
|
+
return "symbol" == typeof i ? i : i + "";
|
|
963
|
+
}
|
|
964
|
+
function _toPrimitive(t, r) {
|
|
965
|
+
if ("object" != typeof t || !t) return t;
|
|
966
|
+
var e = t[Symbol.toPrimitive];
|
|
967
|
+
if (void 0 !== e) {
|
|
968
|
+
var i = e.call(t, r || "default");
|
|
969
|
+
if ("object" != typeof i) return i;
|
|
970
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
971
|
+
}
|
|
972
|
+
return ("string" === r ? String : Number)(t);
|
|
973
|
+
}
|
|
974
|
+
function _setFunctionName(e, t, n) {
|
|
975
|
+
"symbol" == typeof t && (t = (t = t.description) ? "[" + t + "]" : "");
|
|
976
|
+
try {
|
|
977
|
+
Object.defineProperty(e, "name", {
|
|
978
|
+
configurable: !0,
|
|
979
|
+
value: n ? n + " " + t : t
|
|
980
|
+
});
|
|
981
|
+
} catch (e) {}
|
|
982
|
+
return e;
|
|
983
|
+
}
|
|
984
|
+
function _checkInRHS(e) {
|
|
985
|
+
if (Object(e) !== e) throw TypeError("right-hand side of 'in' should be an object, got " + (null !== e ? typeof e : "null"));
|
|
986
|
+
return e;
|
|
987
|
+
}
|
|
711
988
|
var ChatAgent = class extends Agent {
|
|
712
|
-
|
|
989
|
+
static {
|
|
990
|
+
[_initProto] = _applyDecs(this, [], [[
|
|
991
|
+
callable({ description: "Submit feedback for a message by its id" }),
|
|
992
|
+
2,
|
|
993
|
+
"submitMessageFeedback"
|
|
994
|
+
], [
|
|
995
|
+
callable({ description: "Returns all message feedback for the current chat" }),
|
|
996
|
+
2,
|
|
997
|
+
"getMessageFeedback"
|
|
998
|
+
]], 0, void 0, Agent).e;
|
|
999
|
+
}
|
|
1000
|
+
initialState = (_initProto(this), {
|
|
713
1001
|
status: "connecting",
|
|
714
1002
|
type: "chat"
|
|
715
|
-
};
|
|
1003
|
+
});
|
|
716
1004
|
async onStart() {
|
|
717
1005
|
await super.onStart();
|
|
718
1006
|
ensureFeedbackTableExists(this.sql.bind(this));
|
|
@@ -730,14 +1018,14 @@ var ChatAgent = class extends Agent {
|
|
|
730
1018
|
* @param rating - The rating to give the message. 1 = thumbs up, -1 = thumbs down.
|
|
731
1019
|
* @returns The message id and the rating.
|
|
732
1020
|
*/
|
|
733
|
-
|
|
1021
|
+
async submitMessageFeedback(messageId, rating, comment) {
|
|
734
1022
|
return submitMessageFeedback(this.sql.bind(this), messageId, rating, comment);
|
|
735
1023
|
}
|
|
736
1024
|
/**
|
|
737
1025
|
* Returns all message feedback for the current chat.
|
|
738
1026
|
* @returns All message feedback for the current chat.
|
|
739
1027
|
*/
|
|
740
|
-
|
|
1028
|
+
async getMessageFeedback() {
|
|
741
1029
|
return getMessageFeedback(this.sql.bind(this));
|
|
742
1030
|
}
|
|
743
1031
|
/**
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { AgentOptions } from "agents";
|
|
2
|
-
import { JWTPayload } from "jose";
|
|
3
2
|
|
|
4
3
|
//#region src/server/features/auth/index.d.ts
|
|
5
4
|
interface JwtAuthConfig<TClaims extends Record<string, unknown> = Record<string, unknown>> {
|
|
@@ -13,7 +12,7 @@ interface JwtAuthConfig<TClaims extends Record<string, unknown> = Record<string,
|
|
|
13
12
|
* Extract the claims you need from the verified JWT payload.
|
|
14
13
|
* These will be stored and made available in the agent's tool context.
|
|
15
14
|
*/
|
|
16
|
-
getClaims: (payload:
|
|
15
|
+
getClaims: (payload: Record<string, unknown>) => TClaims;
|
|
17
16
|
}
|
|
18
17
|
//#endregion
|
|
19
18
|
//#region src/server/route-agent-request.d.ts
|
package/dist/v1.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { n as JwtAuthConfig, t as routeAgentRequest } from "./route-agent-request-
|
|
1
|
+
import { n as JwtAuthConfig, t as routeAgentRequest } from "./route-agent-request-D2ll--nF.mjs";
|
|
2
2
|
import { Agent as Agent$1, Connection, ConnectionContext } from "agents";
|
|
3
3
|
import { LanguageModel, StreamTextOnFinishCallback, ToolSet, UIMessage, generateText, streamText } from "ai";
|
|
4
4
|
import { AIChatAgent, ChatResponseResult, OnChatMessageOptions } from "@cloudflare/ai-chat";
|
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.9",
|
|
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",
|