@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.
@@ -559,6 +559,7 @@ import z from "zod";
559
559
  import "@deepagents/agent";
560
560
 
561
561
  // packages/context/dist/index.js
562
+ import { mergeWith } from "lodash-es";
562
563
  import { encode } from "gpt-tokenizer";
563
564
  import { generateId } from "ai";
564
565
  import pluralize from "pluralize";
@@ -1134,6 +1135,8 @@ var ContextEngine = class {
1134
1135
  #branch = null;
1135
1136
  #chatData = null;
1136
1137
  #initialized = false;
1138
+ /** Initial metadata to merge on first initialization */
1139
+ #initialMetadata;
1137
1140
  constructor(options) {
1138
1141
  if (!options.chatId) {
1139
1142
  throw new Error("chatId is required");
@@ -1145,6 +1148,7 @@ var ContextEngine = class {
1145
1148
  this.#chatId = options.chatId;
1146
1149
  this.#userId = options.userId;
1147
1150
  this.#branchName = "main";
1151
+ this.#initialMetadata = options.metadata;
1148
1152
  }
1149
1153
  /**
1150
1154
  * Initialize the chat and branch if they don't exist.
@@ -1157,6 +1161,15 @@ var ContextEngine = class {
1157
1161
  id: this.#chatId,
1158
1162
  userId: this.#userId
1159
1163
  });
1164
+ if (this.#initialMetadata) {
1165
+ this.#chatData = await this.#store.updateChat(this.#chatId, {
1166
+ metadata: {
1167
+ ...this.#chatData.metadata,
1168
+ ...this.#initialMetadata
1169
+ }
1170
+ });
1171
+ this.#initialMetadata = void 0;
1172
+ }
1160
1173
  this.#branch = await this.#store.getActiveBranch(this.#chatId);
1161
1174
  this.#initialized = true;
1162
1175
  }
@@ -1630,6 +1643,36 @@ var ContextEngine = class {
1630
1643
  }
1631
1644
  this.#chatData = await this.#store.updateChat(this.#chatId, storeUpdates);
1632
1645
  }
1646
+ /**
1647
+ * Track token usage for the current chat.
1648
+ * Accumulates usage metrics in chat.metadata.usage.
1649
+ *
1650
+ * @param usage - Token usage from AI SDK (LanguageModelUsage)
1651
+ *
1652
+ * @example
1653
+ * ```ts
1654
+ * // In onFinish callback
1655
+ * const usage = await result.totalUsage;
1656
+ * await context.trackUsage(usage);
1657
+ * ```
1658
+ */
1659
+ async trackUsage(usage) {
1660
+ await this.#ensureInitialized();
1661
+ const freshChatData = await this.#store.getChat(this.#chatId);
1662
+ const currentUsage = freshChatData?.metadata?.usage ?? {};
1663
+ const updatedUsage = mergeWith(
1664
+ {},
1665
+ currentUsage,
1666
+ usage,
1667
+ (a, b) => typeof a === "number" || typeof b === "number" ? (a ?? 0) + (b ?? 0) : void 0
1668
+ );
1669
+ this.#chatData = await this.#store.updateChat(this.#chatId, {
1670
+ metadata: {
1671
+ ...freshChatData?.metadata,
1672
+ usage: updatedUsage
1673
+ }
1674
+ });
1675
+ }
1633
1676
  /**
1634
1677
  * Consolidate context fragments (no-op for now).
1635
1678
  *
@@ -1925,12 +1968,20 @@ var SqliteContextStore = class extends ContextStore {
1925
1968
  }
1926
1969
  async listChats(options) {
1927
1970
  const params = [];
1928
- let whereClause = "";
1971
+ const whereClauses = [];
1929
1972
  let limitClause = "";
1930
1973
  if (options?.userId) {
1931
- whereClause = "WHERE c.userId = ?";
1974
+ whereClauses.push("c.userId = ?");
1932
1975
  params.push(options.userId);
1933
1976
  }
1977
+ if (options?.metadata) {
1978
+ whereClauses.push(`json_extract(c.metadata, '$.' || ?) = ?`);
1979
+ params.push(options.metadata.key);
1980
+ params.push(
1981
+ typeof options.metadata.value === "boolean" ? options.metadata.value ? 1 : 0 : options.metadata.value
1982
+ );
1983
+ }
1984
+ const whereClause = whereClauses.length > 0 ? `WHERE ${whereClauses.join(" AND ")}` : "";
1934
1985
  if (options?.limit !== void 0) {
1935
1986
  limitClause = " LIMIT ?";
1936
1987
  params.push(options.limit);
@@ -1944,6 +1995,7 @@ var SqliteContextStore = class extends ContextStore {
1944
1995
  c.id,
1945
1996
  c.userId,
1946
1997
  c.title,
1998
+ c.metadata,
1947
1999
  c.createdAt,
1948
2000
  c.updatedAt,
1949
2001
  COUNT(DISTINCT m.id) as messageCount,
@@ -1959,6 +2011,7 @@ var SqliteContextStore = class extends ContextStore {
1959
2011
  id: row.id,
1960
2012
  userId: row.userId,
1961
2013
  title: row.title ?? void 0,
2014
+ metadata: row.metadata ? JSON.parse(row.metadata) : void 0,
1962
2015
  messageCount: row.messageCount,
1963
2016
  branchCount: row.branchCount,
1964
2017
  createdAt: row.createdAt,
@@ -1988,11 +2041,17 @@ var SqliteContextStore = class extends ContextStore {
1988
2041
  // Message Operations (Graph Nodes)
1989
2042
  // ==========================================================================
1990
2043
  async addMessage(message2) {
1991
- const existingParent = message2.parentId === message2.id ? this.#db.prepare("SELECT parentId FROM messages WHERE id = ?").get(message2.id) : void 0;
1992
- const parentId = message2.parentId === message2.id ? existingParent?.parentId ?? null : message2.parentId;
1993
2044
  this.#db.prepare(
1994
2045
  `INSERT INTO messages (id, chatId, parentId, name, type, data, createdAt)
1995
- VALUES (?, ?, ?, ?, ?, ?, ?)
2046
+ VALUES (
2047
+ ?1,
2048
+ ?2,
2049
+ CASE WHEN ?3 = ?1 THEN (SELECT parentId FROM messages WHERE id = ?1) ELSE ?3 END,
2050
+ ?4,
2051
+ ?5,
2052
+ ?6,
2053
+ ?7
2054
+ )
1996
2055
  ON CONFLICT(id) DO UPDATE SET
1997
2056
  name = excluded.name,
1998
2057
  type = excluded.type,
@@ -2000,7 +2059,7 @@ var SqliteContextStore = class extends ContextStore {
2000
2059
  ).run(
2001
2060
  message2.id,
2002
2061
  message2.chatId,
2003
- parentId,
2062
+ message2.parentId,
2004
2063
  message2.name,
2005
2064
  message2.type ?? null,
2006
2065
  JSON.stringify(message2.data),