@deepagents/text2sql 0.12.1 → 0.13.1

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.
@@ -226,6 +226,7 @@ import z from "zod";
226
226
  import "@deepagents/agent";
227
227
 
228
228
  // packages/context/dist/index.js
229
+ import { mergeWith } from "lodash-es";
229
230
  import { encode } from "gpt-tokenizer";
230
231
  import { generateId } from "ai";
231
232
  import pluralize from "pluralize";
@@ -801,6 +802,8 @@ var ContextEngine = class {
801
802
  #branch = null;
802
803
  #chatData = null;
803
804
  #initialized = false;
805
+ /** Initial metadata to merge on first initialization */
806
+ #initialMetadata;
804
807
  constructor(options) {
805
808
  if (!options.chatId) {
806
809
  throw new Error("chatId is required");
@@ -812,6 +815,7 @@ var ContextEngine = class {
812
815
  this.#chatId = options.chatId;
813
816
  this.#userId = options.userId;
814
817
  this.#branchName = "main";
818
+ this.#initialMetadata = options.metadata;
815
819
  }
816
820
  /**
817
821
  * Initialize the chat and branch if they don't exist.
@@ -824,6 +828,15 @@ var ContextEngine = class {
824
828
  id: this.#chatId,
825
829
  userId: this.#userId
826
830
  });
831
+ if (this.#initialMetadata) {
832
+ this.#chatData = await this.#store.updateChat(this.#chatId, {
833
+ metadata: {
834
+ ...this.#chatData.metadata,
835
+ ...this.#initialMetadata
836
+ }
837
+ });
838
+ this.#initialMetadata = void 0;
839
+ }
827
840
  this.#branch = await this.#store.getActiveBranch(this.#chatId);
828
841
  this.#initialized = true;
829
842
  }
@@ -1297,6 +1310,36 @@ var ContextEngine = class {
1297
1310
  }
1298
1311
  this.#chatData = await this.#store.updateChat(this.#chatId, storeUpdates);
1299
1312
  }
1313
+ /**
1314
+ * Track token usage for the current chat.
1315
+ * Accumulates usage metrics in chat.metadata.usage.
1316
+ *
1317
+ * @param usage - Token usage from AI SDK (LanguageModelUsage)
1318
+ *
1319
+ * @example
1320
+ * ```ts
1321
+ * // In onFinish callback
1322
+ * const usage = await result.totalUsage;
1323
+ * await context.trackUsage(usage);
1324
+ * ```
1325
+ */
1326
+ async trackUsage(usage) {
1327
+ await this.#ensureInitialized();
1328
+ const freshChatData = await this.#store.getChat(this.#chatId);
1329
+ const currentUsage = freshChatData?.metadata?.usage ?? {};
1330
+ const updatedUsage = mergeWith(
1331
+ {},
1332
+ currentUsage,
1333
+ usage,
1334
+ (a, b) => typeof a === "number" || typeof b === "number" ? (a ?? 0) + (b ?? 0) : void 0
1335
+ );
1336
+ this.#chatData = await this.#store.updateChat(this.#chatId, {
1337
+ metadata: {
1338
+ ...freshChatData?.metadata,
1339
+ usage: updatedUsage
1340
+ }
1341
+ });
1342
+ }
1300
1343
  /**
1301
1344
  * Consolidate context fragments (no-op for now).
1302
1345
  *
@@ -1592,12 +1635,20 @@ var SqliteContextStore = class extends ContextStore {
1592
1635
  }
1593
1636
  async listChats(options) {
1594
1637
  const params = [];
1595
- let whereClause = "";
1638
+ const whereClauses = [];
1596
1639
  let limitClause = "";
1597
1640
  if (options?.userId) {
1598
- whereClause = "WHERE c.userId = ?";
1641
+ whereClauses.push("c.userId = ?");
1599
1642
  params.push(options.userId);
1600
1643
  }
1644
+ if (options?.metadata) {
1645
+ whereClauses.push(`json_extract(c.metadata, '$.' || ?) = ?`);
1646
+ params.push(options.metadata.key);
1647
+ params.push(
1648
+ typeof options.metadata.value === "boolean" ? options.metadata.value ? 1 : 0 : options.metadata.value
1649
+ );
1650
+ }
1651
+ const whereClause = whereClauses.length > 0 ? `WHERE ${whereClauses.join(" AND ")}` : "";
1601
1652
  if (options?.limit !== void 0) {
1602
1653
  limitClause = " LIMIT ?";
1603
1654
  params.push(options.limit);
@@ -1611,6 +1662,7 @@ var SqliteContextStore = class extends ContextStore {
1611
1662
  c.id,
1612
1663
  c.userId,
1613
1664
  c.title,
1665
+ c.metadata,
1614
1666
  c.createdAt,
1615
1667
  c.updatedAt,
1616
1668
  COUNT(DISTINCT m.id) as messageCount,
@@ -1626,6 +1678,7 @@ var SqliteContextStore = class extends ContextStore {
1626
1678
  id: row.id,
1627
1679
  userId: row.userId,
1628
1680
  title: row.title ?? void 0,
1681
+ metadata: row.metadata ? JSON.parse(row.metadata) : void 0,
1629
1682
  messageCount: row.messageCount,
1630
1683
  branchCount: row.branchCount,
1631
1684
  createdAt: row.createdAt,
@@ -1655,11 +1708,17 @@ var SqliteContextStore = class extends ContextStore {
1655
1708
  // Message Operations (Graph Nodes)
1656
1709
  // ==========================================================================
1657
1710
  async addMessage(message2) {
1658
- const existingParent = message2.parentId === message2.id ? this.#db.prepare("SELECT parentId FROM messages WHERE id = ?").get(message2.id) : void 0;
1659
- const parentId = message2.parentId === message2.id ? existingParent?.parentId ?? null : message2.parentId;
1660
1711
  this.#db.prepare(
1661
1712
  `INSERT INTO messages (id, chatId, parentId, name, type, data, createdAt)
1662
- VALUES (?, ?, ?, ?, ?, ?, ?)
1713
+ VALUES (
1714
+ ?1,
1715
+ ?2,
1716
+ CASE WHEN ?3 = ?1 THEN (SELECT parentId FROM messages WHERE id = ?1) ELSE ?3 END,
1717
+ ?4,
1718
+ ?5,
1719
+ ?6,
1720
+ ?7
1721
+ )
1663
1722
  ON CONFLICT(id) DO UPDATE SET
1664
1723
  name = excluded.name,
1665
1724
  type = excluded.type,
@@ -1667,7 +1726,7 @@ var SqliteContextStore = class extends ContextStore {
1667
1726
  ).run(
1668
1727
  message2.id,
1669
1728
  message2.chatId,
1670
- parentId,
1729
+ message2.parentId,
1671
1730
  message2.name,
1672
1731
  message2.type ?? null,
1673
1732
  JSON.stringify(message2.data),