@economic/agents 2.10.2 → 2.10.3

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 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) {
@@ -1591,6 +1592,24 @@ function getChatRetentionMs(days) {
1591
1592
  return Math.floor(days * 24 * 60 * 60 * 1e3);
1592
1593
  }
1593
1594
  //#endregion
1595
+ //#region src/server/features/global-chats.ts
1596
+ async function upsertGlobalChat(db, chat) {
1597
+ await db.prepare(`INSERT INTO agent_chats (
1598
+ agent_name, assistant_name, chat_id, title, summary, created_at, updated_at,
1599
+ positive_feedback_count, negative_feedback_count
1600
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
1601
+ ON CONFLICT (agent_name, assistant_name, chat_id) DO UPDATE SET
1602
+ title = excluded.title,
1603
+ summary = excluded.summary,
1604
+ updated_at = excluded.updated_at,
1605
+ positive_feedback_count = excluded.positive_feedback_count,
1606
+ 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();
1607
+ }
1608
+ async function deleteGlobalChat(db, agentName, assistantName, chatId) {
1609
+ await db.prepare(`DELETE FROM agent_chats
1610
+ WHERE agent_name = ? AND assistant_name = ? AND chat_id = ?`).bind(agentName, assistantName, chatId).run();
1611
+ }
1612
+ //#endregion
1594
1613
  //#region src/server/agents/Assistant.ts
1595
1614
  let _initProto$1;
1596
1615
  function _applyDecs$1(e, t, n, r, o, i) {
@@ -1828,9 +1847,11 @@ var Assistant = class extends Agent$1 {
1828
1847
  async deleteChat(id) {
1829
1848
  await this.deleteSubAgent(this.agent, id);
1830
1849
  deleteChat(this.sql.bind(this), id);
1850
+ await this.deleteChatFromGlobalList(id);
1831
1851
  }
1832
1852
  async renameChat(id, title) {
1833
1853
  renameChat(this.sql.bind(this), id, title);
1854
+ await this.syncChatToGlobalList(id);
1834
1855
  }
1835
1856
  async getChats() {
1836
1857
  return getChats(this.sql.bind(this));
@@ -1840,6 +1861,7 @@ var Assistant = class extends Agent$1 {
1840
1861
  }
1841
1862
  async registerChat(durableObjectName) {
1842
1863
  registerChat(this.sql.bind(this), durableObjectName, Date.now());
1864
+ await this.syncChatToGlobalList(durableObjectName);
1843
1865
  }
1844
1866
  async recordChatTurn(durableObjectName, messages) {
1845
1867
  touchChat(this.sql.bind(this), durableObjectName);
@@ -1850,6 +1872,7 @@ var Assistant = class extends Agent$1 {
1850
1872
  error
1851
1873
  });
1852
1874
  })]);
1875
+ await this.syncChatToGlobalList(durableObjectName);
1853
1876
  }).catch((error) => {
1854
1877
  console.error("[Assistant] Failed to record chat metadata", {
1855
1878
  durableObjectName,
@@ -1859,6 +1882,7 @@ var Assistant = class extends Agent$1 {
1859
1882
  }
1860
1883
  async recordChatFeedback(durableObjectName, messageId, rating) {
1861
1884
  recordChatFeedback(this.sql.bind(this), durableObjectName, messageId, rating);
1885
+ await this.syncChatToGlobalList(durableObjectName);
1862
1886
  }
1863
1887
  async [DELETE_CHAT_CALLBACK](durableObjectName) {
1864
1888
  await this.deleteChat(durableObjectName);
@@ -1914,6 +1938,38 @@ var Assistant = class extends Agent$1 {
1914
1938
  console.error("[Assistant] Failed to update agent_registry activity", error);
1915
1939
  }
1916
1940
  }
1941
+ async syncChatToGlobalList(durableObjectName) {
1942
+ const chat = getChat(this.sql.bind(this), durableObjectName);
1943
+ if (!chat) return;
1944
+ try {
1945
+ await upsertGlobalChat(this.env.AGENTS_DB, {
1946
+ agentName: this.constructor.name,
1947
+ assistantName: this.name,
1948
+ chatId: chat.durable_object_name,
1949
+ title: chat.title ?? null,
1950
+ summary: chat.summary ?? null,
1951
+ createdAt: chat.created_at,
1952
+ updatedAt: chat.updated_at,
1953
+ positiveFeedbackCount: chat.positive_feedback_count,
1954
+ negativeFeedbackCount: chat.negative_feedback_count
1955
+ });
1956
+ } catch (error) {
1957
+ console.error("[Assistant] Failed to sync chat to agent_chats", {
1958
+ durableObjectName,
1959
+ error
1960
+ });
1961
+ }
1962
+ }
1963
+ async deleteChatFromGlobalList(durableObjectName) {
1964
+ try {
1965
+ await deleteGlobalChat(this.env.AGENTS_DB, this.constructor.name, this.name, durableObjectName);
1966
+ } catch (error) {
1967
+ console.error("[Assistant] Failed to delete chat from agent_chats", {
1968
+ durableObjectName,
1969
+ error
1970
+ });
1971
+ }
1972
+ }
1917
1973
  observability = createWorkerObservability({ getIdentity: () => ({
1918
1974
  agentName: this.constructor.name,
1919
1975
  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.3",
4
4
  "description": "A starter for creating a TypeScript package.",
5
5
  "license": "MIT",
6
6
  "files": [