@economic/agents 2.3.22 → 2.3.23
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 -0
- package/dist/index.mjs +73 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -239,6 +239,8 @@ type Chat = {
|
|
|
239
239
|
summary?: string;
|
|
240
240
|
created_at: number;
|
|
241
241
|
updated_at: number;
|
|
242
|
+
positive_feedback_count: number;
|
|
243
|
+
negative_feedback_count: number;
|
|
242
244
|
};
|
|
243
245
|
declare const DELETE_CHAT_CALLBACK: "deleteChatCallback";
|
|
244
246
|
//#endregion
|
|
@@ -252,7 +254,9 @@ declare abstract class Assistant<State = unknown> extends Agent$1<Cloudflare.Env
|
|
|
252
254
|
createChat(): Promise<string>;
|
|
253
255
|
deleteChat(id: string): Promise<void>;
|
|
254
256
|
getChats(): Promise<Chat[]>;
|
|
257
|
+
getChatAgentClassName(): Promise<string>;
|
|
255
258
|
recordChatTurn(durableObjectName: string, messages: UIMessage[]): Promise<void>;
|
|
259
|
+
recordChatFeedback(durableObjectName: string, messageId: string, rating: number): Promise<void>;
|
|
256
260
|
private [DELETE_CHAT_CALLBACK];
|
|
257
261
|
private scheduleChatForAutoDeletion;
|
|
258
262
|
/**
|
package/dist/index.mjs
CHANGED
|
@@ -388,12 +388,33 @@ function ensureChatsTableExists(sql) {
|
|
|
388
388
|
summary TEXT,
|
|
389
389
|
created_at INTEGER NOT NULL,
|
|
390
390
|
updated_at INTEGER NOT NULL,
|
|
391
|
+
positive_feedback_count INTEGER NOT NULL DEFAULT 0,
|
|
392
|
+
negative_feedback_count INTEGER NOT NULL DEFAULT 0,
|
|
391
393
|
PRIMARY KEY (durable_object_name)
|
|
392
394
|
)`;
|
|
395
|
+
ensureChatsFeedbackColumnsExist(sql);
|
|
396
|
+
ensureChatMessageFeedbackTableExists(sql);
|
|
393
397
|
} catch (error) {
|
|
394
398
|
console.error("[Agent] Failed to create chats table", error);
|
|
395
399
|
}
|
|
396
400
|
}
|
|
401
|
+
function ensureChatsFeedbackColumnsExist(sql) {
|
|
402
|
+
try {
|
|
403
|
+
sql`ALTER TABLE chats ADD COLUMN positive_feedback_count INTEGER NOT NULL DEFAULT 0`;
|
|
404
|
+
} catch {}
|
|
405
|
+
try {
|
|
406
|
+
sql`ALTER TABLE chats ADD COLUMN negative_feedback_count INTEGER NOT NULL DEFAULT 0`;
|
|
407
|
+
} catch {}
|
|
408
|
+
}
|
|
409
|
+
function ensureChatMessageFeedbackTableExists(sql) {
|
|
410
|
+
sql`CREATE TABLE IF NOT EXISTS chat_message_feedback (
|
|
411
|
+
chat_id TEXT NOT NULL,
|
|
412
|
+
message_id TEXT NOT NULL,
|
|
413
|
+
rating INTEGER NOT NULL,
|
|
414
|
+
updated_at INTEGER NOT NULL,
|
|
415
|
+
PRIMARY KEY (chat_id, message_id)
|
|
416
|
+
)`;
|
|
417
|
+
}
|
|
397
418
|
function registerChat(sql, durableObjectName, dateTime) {
|
|
398
419
|
sql`INSERT INTO chats (durable_object_name, created_at, updated_at)
|
|
399
420
|
VALUES (${durableObjectName}, ${dateTime}, ${dateTime})`;
|
|
@@ -415,6 +436,7 @@ function registerChatWithMetadata(sql, durableObjectName, metadata) {
|
|
|
415
436
|
}
|
|
416
437
|
function deleteChat(sql, durableObjectName) {
|
|
417
438
|
sql`DELETE FROM chats WHERE durable_object_name = ${durableObjectName}`;
|
|
439
|
+
sql`DELETE FROM chat_message_feedback WHERE chat_id = ${durableObjectName}`;
|
|
418
440
|
}
|
|
419
441
|
function getChat(sql, durableObjectName) {
|
|
420
442
|
return sql`SELECT * FROM chats WHERE durable_object_name = ${durableObjectName}`[0] ?? null;
|
|
@@ -422,6 +444,23 @@ function getChat(sql, durableObjectName) {
|
|
|
422
444
|
async function getChats(sql) {
|
|
423
445
|
return sql`SELECT * FROM chats ORDER BY updated_at DESC`;
|
|
424
446
|
}
|
|
447
|
+
function recordChatFeedback(sql, durableObjectName, messageId, rating, dateTime = Date.now()) {
|
|
448
|
+
sql`INSERT INTO chat_message_feedback (chat_id, message_id, rating, updated_at)
|
|
449
|
+
VALUES (${durableObjectName}, ${messageId}, ${rating}, ${dateTime})
|
|
450
|
+
ON CONFLICT (chat_id, message_id) DO UPDATE SET
|
|
451
|
+
rating = excluded.rating,
|
|
452
|
+
updated_at = excluded.updated_at`;
|
|
453
|
+
const positiveRows = sql`SELECT COUNT(*) AS count
|
|
454
|
+
FROM chat_message_feedback
|
|
455
|
+
WHERE chat_id = ${durableObjectName} AND rating > 0`;
|
|
456
|
+
const negativeRows = sql`SELECT COUNT(*) AS count
|
|
457
|
+
FROM chat_message_feedback
|
|
458
|
+
WHERE chat_id = ${durableObjectName} AND rating < 0`;
|
|
459
|
+
sql`UPDATE chats
|
|
460
|
+
SET positive_feedback_count = ${Number(positiveRows[0]?.count ?? 0)},
|
|
461
|
+
negative_feedback_count = ${Number(negativeRows[0]?.count ?? 0)}
|
|
462
|
+
WHERE durable_object_name = ${durableObjectName}`;
|
|
463
|
+
}
|
|
425
464
|
function getDeleteChatScheduleIds(schedules) {
|
|
426
465
|
return schedules.filter((schedule) => schedule.callback === DELETE_CHAT_CALLBACK).map((schedule) => schedule.id);
|
|
427
466
|
}
|
|
@@ -717,6 +756,11 @@ var Assistant = class extends Agent$1 {
|
|
|
717
756
|
callable(),
|
|
718
757
|
2,
|
|
719
758
|
"getChats"
|
|
759
|
+
],
|
|
760
|
+
[
|
|
761
|
+
callable(),
|
|
762
|
+
2,
|
|
763
|
+
"getChatAgentClassName"
|
|
720
764
|
]
|
|
721
765
|
], 0, void 0, Agent$1).e;
|
|
722
766
|
}
|
|
@@ -774,10 +818,16 @@ var Assistant = class extends Agent$1 {
|
|
|
774
818
|
async getChats() {
|
|
775
819
|
return getChats(this.sql.bind(this));
|
|
776
820
|
}
|
|
821
|
+
async getChatAgentClassName() {
|
|
822
|
+
return this.agent.name;
|
|
823
|
+
}
|
|
777
824
|
async recordChatTurn(durableObjectName, messages) {
|
|
778
825
|
summariseChatWithAI(this.sql.bind(this), durableObjectName, messages, this.fastModel);
|
|
779
826
|
this.scheduleChatForAutoDeletion(durableObjectName);
|
|
780
827
|
}
|
|
828
|
+
async recordChatFeedback(durableObjectName, messageId, rating) {
|
|
829
|
+
recordChatFeedback(this.sql.bind(this), durableObjectName, messageId, rating);
|
|
830
|
+
}
|
|
781
831
|
async [DELETE_CHAT_CALLBACK](durableObjectName) {
|
|
782
832
|
await this.deleteChat(durableObjectName);
|
|
783
833
|
}
|
|
@@ -1092,11 +1142,21 @@ var ChatAgent = class extends Agent {
|
|
|
1092
1142
|
*/
|
|
1093
1143
|
async submitMessageFeedback(messageId, rating, comment) {
|
|
1094
1144
|
const result = submitMessageFeedback(this.sql.bind(this), messageId, rating, comment);
|
|
1145
|
+
const parent = await this.getParentAgent();
|
|
1095
1146
|
recordFeedbackAnalytics(this.env.AGENTS_ANALYTICS, {
|
|
1096
1147
|
agentName: this.getTelemetryAgentName(),
|
|
1097
1148
|
durableObjectName: this.name,
|
|
1098
1149
|
actorId: this.getTelemetryActorId()
|
|
1099
1150
|
}, messageId, rating);
|
|
1151
|
+
if (parent?.recordChatFeedback) try {
|
|
1152
|
+
await parent.recordChatFeedback(this.name, messageId, rating);
|
|
1153
|
+
} catch (error) {
|
|
1154
|
+
console.error("[ChatAgent] Failed to update parent chat feedback summary", {
|
|
1155
|
+
durableObjectName: this.name,
|
|
1156
|
+
messageId,
|
|
1157
|
+
error
|
|
1158
|
+
});
|
|
1159
|
+
}
|
|
1100
1160
|
return result;
|
|
1101
1161
|
}
|
|
1102
1162
|
/**
|
|
@@ -1120,11 +1180,23 @@ var ChatAgent = class extends Agent {
|
|
|
1120
1180
|
*/
|
|
1121
1181
|
async importLegacyMessages(messages, feedback = []) {
|
|
1122
1182
|
let parentId = null;
|
|
1183
|
+
const parent = await this.getParentAgent();
|
|
1123
1184
|
for (const message of messages) {
|
|
1124
1185
|
await this.appendMessageToHistory(message, parentId);
|
|
1125
1186
|
parentId = message.id;
|
|
1126
1187
|
}
|
|
1127
|
-
for (const item of feedback)
|
|
1188
|
+
for (const item of feedback) {
|
|
1189
|
+
submitMessageFeedback(this.sql.bind(this), item.messageId, item.rating, item.comment);
|
|
1190
|
+
try {
|
|
1191
|
+
await parent?.recordChatFeedback?.(this.name, item.messageId, item.rating);
|
|
1192
|
+
} catch (error) {
|
|
1193
|
+
console.error("[ChatAgent] Failed to import parent chat feedback summary", {
|
|
1194
|
+
durableObjectName: this.name,
|
|
1195
|
+
messageId: item.messageId,
|
|
1196
|
+
error
|
|
1197
|
+
});
|
|
1198
|
+
}
|
|
1199
|
+
}
|
|
1128
1200
|
}
|
|
1129
1201
|
};
|
|
1130
1202
|
//#endregion
|