@economic/agents 2.10.2 → 2.10.4

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
@@ -264,7 +264,7 @@ The `Assistant` keeps the chat list in its own SQLite (a `chats` table, created
264
264
  | `deleteChat(id)` | `void` | Deletes a chat facet and its record. |
265
265
  | `renameChat(id)` | `void` | Renames a registered chat. |
266
266
 
267
- - **Titles and summaries** — generated with `fastModel` after each turn (on the first turn, then refreshed as the chat grows).
267
+ - **Titles and summaries** — generated with `fastModel` after each turn (on the first turn, then refreshed as the chat grows). Written in the language of the conversation, following the user's most recent messages when a chat mixes languages.
268
268
  - **Empty chat cleanup** — a newly created facet is scheduled for deletion after one day. Its first completed turn replaces that schedule with the normal sliding retention period.
269
269
  - **Retention** — inactive chats are deleted after 90 days, via a Durable Object alarm.
270
270
 
package/dist/index.d.mts CHANGED
@@ -383,6 +383,8 @@ declare abstract class Assistant<State = unknown> extends Agent$1<Cloudflare.Env
383
383
  * Best-effort and non-blocking.
384
384
  */
385
385
  private touchInstance;
386
+ private syncChatToGlobalList;
387
+ private deleteChatFromGlobalList;
386
388
  observability?: Observability;
387
389
  }
388
390
  //#endregion
package/dist/index.mjs CHANGED
@@ -1527,7 +1527,8 @@ function recordChatFeedback(sql, durableObjectName, messageId, rating, dateTime
1527
1527
  WHERE chat_id = ${durableObjectName} AND rating < 0`;
1528
1528
  sql`UPDATE chats
1529
1529
  SET positive_feedback_count = ${Number(positiveRows[0]?.count ?? 0)},
1530
- negative_feedback_count = ${Number(negativeRows[0]?.count ?? 0)}
1530
+ negative_feedback_count = ${Number(negativeRows[0]?.count ?? 0)},
1531
+ updated_at = ${dateTime}
1531
1532
  WHERE durable_object_name = ${durableObjectName}`;
1532
1533
  }
1533
1534
  function getDeleteChatScheduleIds(schedules, durableObjectName) {
@@ -1546,7 +1547,10 @@ async function summariseChatWithAI(sql, durableObjectName, messages, model) {
1546
1547
  You will be given a list of messages and you need to generate a title and summary for the chat.
1547
1548
  The title should be a short title for the chat, max 8-10 words.
1548
1549
  The summary should be a short 1-2 sentence summary of the chat.
1549
- The summary should reflect the direction of the chat.`;
1550
+ The summary should reflect the direction of the chat.
1551
+ Write the title and the summary in the same language as the conversation, never
1552
+ translating them to English. If the conversation mixes languages, follow the
1553
+ language of the user's most recent messages.`;
1550
1554
  if (chat) systemPrompt += `\n\nThe previous summary: ${chat.summary}`;
1551
1555
  try {
1552
1556
  const { output: { title, summary } } = await generateText({
@@ -1591,6 +1595,24 @@ function getChatRetentionMs(days) {
1591
1595
  return Math.floor(days * 24 * 60 * 60 * 1e3);
1592
1596
  }
1593
1597
  //#endregion
1598
+ //#region src/server/features/global-chats.ts
1599
+ async function upsertGlobalChat(db, chat) {
1600
+ await db.prepare(`INSERT INTO agent_chats (
1601
+ agent_name, assistant_name, chat_id, title, summary, created_at, updated_at,
1602
+ positive_feedback_count, negative_feedback_count
1603
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
1604
+ ON CONFLICT (agent_name, assistant_name, chat_id) DO UPDATE SET
1605
+ title = excluded.title,
1606
+ summary = excluded.summary,
1607
+ updated_at = excluded.updated_at,
1608
+ positive_feedback_count = excluded.positive_feedback_count,
1609
+ negative_feedback_count = excluded.negative_feedback_count`).bind(chat.agentName, chat.assistantName, chat.chatId, chat.title, chat.summary, chat.createdAt, chat.updatedAt, chat.positiveFeedbackCount, chat.negativeFeedbackCount).run();
1610
+ }
1611
+ async function deleteGlobalChat(db, agentName, assistantName, chatId) {
1612
+ await db.prepare(`DELETE FROM agent_chats
1613
+ WHERE agent_name = ? AND assistant_name = ? AND chat_id = ?`).bind(agentName, assistantName, chatId).run();
1614
+ }
1615
+ //#endregion
1594
1616
  //#region src/server/agents/Assistant.ts
1595
1617
  let _initProto$1;
1596
1618
  function _applyDecs$1(e, t, n, r, o, i) {
@@ -1828,9 +1850,11 @@ var Assistant = class extends Agent$1 {
1828
1850
  async deleteChat(id) {
1829
1851
  await this.deleteSubAgent(this.agent, id);
1830
1852
  deleteChat(this.sql.bind(this), id);
1853
+ await this.deleteChatFromGlobalList(id);
1831
1854
  }
1832
1855
  async renameChat(id, title) {
1833
1856
  renameChat(this.sql.bind(this), id, title);
1857
+ await this.syncChatToGlobalList(id);
1834
1858
  }
1835
1859
  async getChats() {
1836
1860
  return getChats(this.sql.bind(this));
@@ -1840,6 +1864,7 @@ var Assistant = class extends Agent$1 {
1840
1864
  }
1841
1865
  async registerChat(durableObjectName) {
1842
1866
  registerChat(this.sql.bind(this), durableObjectName, Date.now());
1867
+ await this.syncChatToGlobalList(durableObjectName);
1843
1868
  }
1844
1869
  async recordChatTurn(durableObjectName, messages) {
1845
1870
  touchChat(this.sql.bind(this), durableObjectName);
@@ -1850,6 +1875,7 @@ var Assistant = class extends Agent$1 {
1850
1875
  error
1851
1876
  });
1852
1877
  })]);
1878
+ await this.syncChatToGlobalList(durableObjectName);
1853
1879
  }).catch((error) => {
1854
1880
  console.error("[Assistant] Failed to record chat metadata", {
1855
1881
  durableObjectName,
@@ -1859,6 +1885,7 @@ var Assistant = class extends Agent$1 {
1859
1885
  }
1860
1886
  async recordChatFeedback(durableObjectName, messageId, rating) {
1861
1887
  recordChatFeedback(this.sql.bind(this), durableObjectName, messageId, rating);
1888
+ await this.syncChatToGlobalList(durableObjectName);
1862
1889
  }
1863
1890
  async [DELETE_CHAT_CALLBACK](durableObjectName) {
1864
1891
  await this.deleteChat(durableObjectName);
@@ -1914,6 +1941,38 @@ var Assistant = class extends Agent$1 {
1914
1941
  console.error("[Assistant] Failed to update agent_registry activity", error);
1915
1942
  }
1916
1943
  }
1944
+ async syncChatToGlobalList(durableObjectName) {
1945
+ const chat = getChat(this.sql.bind(this), durableObjectName);
1946
+ if (!chat) return;
1947
+ try {
1948
+ await upsertGlobalChat(this.env.AGENTS_DB, {
1949
+ agentName: this.constructor.name,
1950
+ assistantName: this.name,
1951
+ chatId: chat.durable_object_name,
1952
+ title: chat.title ?? null,
1953
+ summary: chat.summary ?? null,
1954
+ createdAt: chat.created_at,
1955
+ updatedAt: chat.updated_at,
1956
+ positiveFeedbackCount: chat.positive_feedback_count,
1957
+ negativeFeedbackCount: chat.negative_feedback_count
1958
+ });
1959
+ } catch (error) {
1960
+ console.error("[Assistant] Failed to sync chat to agent_chats", {
1961
+ durableObjectName,
1962
+ error
1963
+ });
1964
+ }
1965
+ }
1966
+ async deleteChatFromGlobalList(durableObjectName) {
1967
+ try {
1968
+ await deleteGlobalChat(this.env.AGENTS_DB, this.constructor.name, this.name, durableObjectName);
1969
+ } catch (error) {
1970
+ console.error("[Assistant] Failed to delete chat from agent_chats", {
1971
+ durableObjectName,
1972
+ error
1973
+ });
1974
+ }
1975
+ }
1917
1976
  observability = createWorkerObservability({ getIdentity: () => ({
1918
1977
  agentName: this.constructor.name,
1919
1978
  agentClass: this.constructor.name
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@economic/agents",
3
- "version": "2.10.2",
3
+ "version": "2.10.4",
4
4
  "description": "A starter for creating a TypeScript package.",
5
5
  "license": "MIT",
6
6
  "files": [