@economic/agents 2.10.0 → 2.10.2

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.
Files changed (2) hide show
  1. package/dist/index.mjs +26 -12
  2. package/package.json +1 -1
package/dist/index.mjs CHANGED
@@ -1480,6 +1480,11 @@ function registerChat(sql, durableObjectName, dateTime) {
1480
1480
  VALUES (${durableObjectName}, ${dateTime}, ${dateTime})
1481
1481
  ON CONFLICT (durable_object_name) DO NOTHING`;
1482
1482
  }
1483
+ function touchChat(sql, durableObjectName, dateTime = Date.now()) {
1484
+ sql`UPDATE chats
1485
+ SET updated_at = ${dateTime}
1486
+ WHERE durable_object_name = ${durableObjectName}`;
1487
+ }
1483
1488
  /**
1484
1489
  * Sets a chat's title on the user's behalf. Marks the title as user-set so
1485
1490
  * the AI summariser stops overwriting it (it keeps updating the summary).
@@ -1487,11 +1492,12 @@ function registerChat(sql, durableObjectName, dateTime) {
1487
1492
  function renameChat(sql, durableObjectName, title, dateTime = Date.now()) {
1488
1493
  const trimmed = title.trim();
1489
1494
  if (!trimmed) throw new Error("Chat title cannot be empty");
1490
- sql`UPDATE chats
1495
+ if (!sql`UPDATE chats
1491
1496
  SET title = ${trimmed},
1492
1497
  title_set_by_user = 1,
1493
1498
  updated_at = ${dateTime}
1494
- WHERE durable_object_name = ${durableObjectName}`;
1499
+ WHERE durable_object_name = ${durableObjectName}
1500
+ RETURNING durable_object_name`.length) throw new Error(`Chat not found: ${durableObjectName}`);
1495
1501
  }
1496
1502
  function deleteChat(sql, durableObjectName) {
1497
1503
  sql`DELETE FROM chats WHERE durable_object_name = ${durableObjectName}`;
@@ -1504,6 +1510,7 @@ async function getChats(sql) {
1504
1510
  return sql`SELECT * FROM chats ORDER BY updated_at DESC`;
1505
1511
  }
1506
1512
  function recordChatFeedback(sql, durableObjectName, messageId, rating, dateTime = Date.now()) {
1513
+ if (rating !== 1 && rating !== -1 && rating !== 0) throw new RangeError("Message feedback rating must be 1, -1, or 0");
1507
1514
  if (!getChat(sql, durableObjectName)) throw new Error(`Chat not found: ${durableObjectName}`);
1508
1515
  if (rating === 0) sql`DELETE FROM chat_message_feedback
1509
1516
  WHERE chat_id = ${durableObjectName} AND message_id = ${messageId}`;
@@ -1526,15 +1533,14 @@ function recordChatFeedback(sql, durableObjectName, messageId, rating, dateTime
1526
1533
  function getDeleteChatScheduleIds(schedules, durableObjectName) {
1527
1534
  return schedules.filter((schedule) => schedule.callback === "deleteChatCallback" && schedule.payload === durableObjectName).map((schedule) => schedule.id);
1528
1535
  }
1529
- /**
1530
- * Number of recent messages passed to `generateSummary` for rolling
1531
- * summarization. Keeping this bounded prevents the prompt growing
1532
- * unboundedly regardless of chat length.
1533
- */
1534
- const CHAT_RECENT_MESSAGES_COUNT = 20;
1536
+ const CHAT_SUMMARY_TURN_INTERVAL = 5;
1537
+ function isChatSummaryDue(messages) {
1538
+ const completedTurns = messages.filter((message) => message.role === "user").length;
1539
+ return completedTurns > 0 && completedTurns % CHAT_SUMMARY_TURN_INTERVAL === 0;
1540
+ }
1535
1541
  async function summariseChatWithAI(sql, durableObjectName, messages, model) {
1536
1542
  const chat = getChat(sql, durableObjectName);
1537
- if (!(!chat || !chat.title || messages.length % CHAT_RECENT_MESSAGES_COUNT === 0)) return;
1543
+ if (!(!chat || !chat.title || isChatSummaryDue(messages))) return;
1538
1544
  let systemPrompt = `
1539
1545
  You are a helpful assistant that summarises chats.
1540
1546
  You will be given a list of messages and you need to generate a title and summary for the chat.
@@ -1836,9 +1842,16 @@ var Assistant = class extends Agent$1 {
1836
1842
  registerChat(this.sql.bind(this), durableObjectName, Date.now());
1837
1843
  }
1838
1844
  async recordChatTurn(durableObjectName, messages) {
1839
- summariseChatWithAI(this.sql.bind(this), durableObjectName, messages, this.fastModel);
1840
- this.scheduleChatForAutoDeletion(durableObjectName).catch((error) => {
1841
- console.error("[Assistant] Failed to reschedule chat deletion", {
1845
+ touchChat(this.sql.bind(this), durableObjectName);
1846
+ this.keepAliveWhile(async () => {
1847
+ await Promise.all([summariseChatWithAI(this.sql.bind(this), durableObjectName, messages, this.fastModel), this.scheduleChatForAutoDeletion(durableObjectName).catch((error) => {
1848
+ console.error("[Assistant] Failed to reschedule chat deletion", {
1849
+ durableObjectName,
1850
+ error
1851
+ });
1852
+ })]);
1853
+ }).catch((error) => {
1854
+ console.error("[Assistant] Failed to record chat metadata", {
1842
1855
  durableObjectName,
1843
1856
  error
1844
1857
  });
@@ -1939,6 +1952,7 @@ function ensureFeedbackTableExists(sql) {
1939
1952
  * @param now - The date and time to use for the created_at and updated_at columns.
1940
1953
  */
1941
1954
  function submitMessageFeedback(sql, messageId, rating, comment, now = /* @__PURE__ */ new Date()) {
1955
+ if (rating !== 1 && rating !== -1 && rating !== 0) throw new RangeError("Message feedback rating must be 1, -1, or 0");
1942
1956
  try {
1943
1957
  if (rating === 0) {
1944
1958
  sql`DELETE FROM assistant_messages_feedback WHERE message_id = ${messageId}`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@economic/agents",
3
- "version": "2.10.0",
3
+ "version": "2.10.2",
4
4
  "description": "A starter for creating a TypeScript package.",
5
5
  "license": "MIT",
6
6
  "files": [