@deepagents/text2sql 0.12.0 → 0.13.0

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 (29) hide show
  1. package/dist/index.js +263 -81
  2. package/dist/index.js.map +4 -4
  3. package/dist/lib/adapters/groundings/index.js +167 -11
  4. package/dist/lib/adapters/groundings/index.js.map +4 -4
  5. package/dist/lib/adapters/mysql/index.js +167 -11
  6. package/dist/lib/adapters/mysql/index.js.map +4 -4
  7. package/dist/lib/adapters/postgres/index.js +167 -11
  8. package/dist/lib/adapters/postgres/index.js.map +4 -4
  9. package/dist/lib/adapters/spreadsheet/index.js +2 -0
  10. package/dist/lib/adapters/spreadsheet/index.js.map +3 -3
  11. package/dist/lib/adapters/sqlite/index.js +167 -11
  12. package/dist/lib/adapters/sqlite/index.js.map +4 -4
  13. package/dist/lib/adapters/sqlserver/column-stats.sqlserver.grounding.d.ts.map +1 -1
  14. package/dist/lib/adapters/sqlserver/index.js +168 -14
  15. package/dist/lib/adapters/sqlserver/index.js.map +4 -4
  16. package/dist/lib/agents/result-tools.d.ts +12 -3
  17. package/dist/lib/agents/result-tools.d.ts.map +1 -1
  18. package/dist/lib/agents/sql.agent.d.ts +1 -1
  19. package/dist/lib/agents/sql.agent.d.ts.map +1 -1
  20. package/dist/lib/instructions.d.ts.map +1 -1
  21. package/dist/lib/sql.d.ts +4 -15
  22. package/dist/lib/sql.d.ts.map +1 -1
  23. package/dist/lib/synthesis/index.js +176 -10
  24. package/dist/lib/synthesis/index.js.map +4 -4
  25. package/dist/lib/synthesis/synthesizers/depth-evolver.d.ts +3 -3
  26. package/dist/lib/synthesis/synthesizers/depth-evolver.d.ts.map +1 -1
  27. package/dist/lib/synthesis/synthesizers/schema-synthesizer.d.ts +1 -1
  28. package/dist/lib/synthesis/synthesizers/schema-synthesizer.d.ts.map +1 -1
  29. package/package.json +4 -4
@@ -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";
@@ -770,7 +771,7 @@ function message(content) {
770
771
  } : content;
771
772
  return {
772
773
  id: message2.id,
773
- name: "message",
774
+ name: message2.role,
774
775
  data: "content",
775
776
  type: "message",
776
777
  persist: true,
@@ -792,6 +793,22 @@ function assistantText(content, options) {
792
793
  parts: [{ type: "text", text: content }]
793
794
  });
794
795
  }
796
+ var LAZY_ID = Symbol("lazy-id");
797
+ function isLazyFragment(fragment2) {
798
+ return LAZY_ID in fragment2;
799
+ }
800
+ function lastAssistantMessage(content) {
801
+ return {
802
+ name: "assistant",
803
+ type: "message",
804
+ persist: true,
805
+ data: "content",
806
+ [LAZY_ID]: {
807
+ type: "last-assistant",
808
+ content
809
+ }
810
+ };
811
+ }
795
812
  var ContextRenderer = class {
796
813
  options;
797
814
  constructor(options = {}) {
@@ -1118,6 +1135,8 @@ var ContextEngine = class {
1118
1135
  #branch = null;
1119
1136
  #chatData = null;
1120
1137
  #initialized = false;
1138
+ /** Initial metadata to merge on first initialization */
1139
+ #initialMetadata;
1121
1140
  constructor(options) {
1122
1141
  if (!options.chatId) {
1123
1142
  throw new Error("chatId is required");
@@ -1129,6 +1148,7 @@ var ContextEngine = class {
1129
1148
  this.#chatId = options.chatId;
1130
1149
  this.#userId = options.userId;
1131
1150
  this.#branchName = "main";
1151
+ this.#initialMetadata = options.metadata;
1132
1152
  }
1133
1153
  /**
1134
1154
  * Initialize the chat and branch if they don't exist.
@@ -1141,6 +1161,15 @@ var ContextEngine = class {
1141
1161
  id: this.#chatId,
1142
1162
  userId: this.#userId
1143
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
+ }
1144
1173
  this.#branch = await this.#store.getActiveBranch(this.#chatId);
1145
1174
  this.#initialized = true;
1146
1175
  }
@@ -1288,6 +1317,12 @@ var ContextEngine = class {
1288
1317
  if (this.#pendingMessages.length === 0) {
1289
1318
  return;
1290
1319
  }
1320
+ for (let i = 0; i < this.#pendingMessages.length; i++) {
1321
+ const fragment2 = this.#pendingMessages[i];
1322
+ if (isLazyFragment(fragment2)) {
1323
+ this.#pendingMessages[i] = await this.#resolveLazyFragment(fragment2);
1324
+ }
1325
+ }
1291
1326
  let parentId = this.#branch.headMessageId;
1292
1327
  const now = Date.now();
1293
1328
  for (const fragment2 of this.#pendingMessages) {
@@ -1307,6 +1342,39 @@ var ContextEngine = class {
1307
1342
  this.#branch.headMessageId = parentId;
1308
1343
  this.#pendingMessages = [];
1309
1344
  }
1345
+ /**
1346
+ * Resolve a lazy fragment by finding the appropriate ID.
1347
+ */
1348
+ async #resolveLazyFragment(fragment2) {
1349
+ const lazy = fragment2[LAZY_ID];
1350
+ if (lazy.type === "last-assistant") {
1351
+ const lastId = await this.#getLastAssistantId();
1352
+ return assistantText(lazy.content, { id: lastId ?? crypto.randomUUID() });
1353
+ }
1354
+ throw new Error(`Unknown lazy fragment type: ${lazy.type}`);
1355
+ }
1356
+ /**
1357
+ * Find the most recent assistant message ID (pending or persisted).
1358
+ */
1359
+ async #getLastAssistantId() {
1360
+ for (let i = this.#pendingMessages.length - 1; i >= 0; i--) {
1361
+ const msg = this.#pendingMessages[i];
1362
+ if (msg.name === "assistant" && !isLazyFragment(msg)) {
1363
+ return msg.id;
1364
+ }
1365
+ }
1366
+ if (this.#branch?.headMessageId) {
1367
+ const chain = await this.#store.getMessageChain(
1368
+ this.#branch.headMessageId
1369
+ );
1370
+ for (let i = chain.length - 1; i >= 0; i--) {
1371
+ if (chain[i].name === "assistant") {
1372
+ return chain[i].id;
1373
+ }
1374
+ }
1375
+ }
1376
+ return void 0;
1377
+ }
1310
1378
  /**
1311
1379
  * Estimate token count and cost for the full context.
1312
1380
  *
@@ -1575,6 +1643,36 @@ var ContextEngine = class {
1575
1643
  }
1576
1644
  this.#chatData = await this.#store.updateChat(this.#chatId, storeUpdates);
1577
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
+ }
1578
1676
  /**
1579
1677
  * Consolidate context fragments (no-op for now).
1580
1678
  *
@@ -1586,6 +1684,35 @@ var ContextEngine = class {
1586
1684
  consolidate() {
1587
1685
  return void 0;
1588
1686
  }
1687
+ /**
1688
+ * Extract skill path mappings from available_skills fragments.
1689
+ * Returns array of { host, sandbox } for mounting in sandbox filesystem.
1690
+ *
1691
+ * Reads the original `paths` configuration stored in fragment metadata
1692
+ * by the skills() fragment helper.
1693
+ *
1694
+ * @example
1695
+ * ```ts
1696
+ * const context = new ContextEngine({ store, chatId, userId })
1697
+ * .set(skills({ paths: [{ host: './skills', sandbox: '/skills' }] }));
1698
+ *
1699
+ * const mounts = context.getSkillMounts();
1700
+ * // [{ host: './skills', sandbox: '/skills' }]
1701
+ * ```
1702
+ */
1703
+ getSkillMounts() {
1704
+ const mounts = [];
1705
+ for (const fragment2 of this.#fragments) {
1706
+ if (fragment2.name === "available_skills" && fragment2.metadata && Array.isArray(fragment2.metadata.paths)) {
1707
+ for (const mapping of fragment2.metadata.paths) {
1708
+ if (typeof mapping === "object" && mapping !== null && typeof mapping.host === "string" && typeof mapping.sandbox === "string") {
1709
+ mounts.push({ host: mapping.host, sandbox: mapping.sandbox });
1710
+ }
1711
+ }
1712
+ }
1713
+ }
1714
+ return mounts;
1715
+ }
1589
1716
  /**
1590
1717
  * Inspect the full context state for debugging.
1591
1718
  * Returns a JSON-serializable object with context information.
@@ -1841,12 +1968,20 @@ var SqliteContextStore = class extends ContextStore {
1841
1968
  }
1842
1969
  async listChats(options) {
1843
1970
  const params = [];
1844
- let whereClause = "";
1971
+ const whereClauses = [];
1845
1972
  let limitClause = "";
1846
1973
  if (options?.userId) {
1847
- whereClause = "WHERE c.userId = ?";
1974
+ whereClauses.push("c.userId = ?");
1848
1975
  params.push(options.userId);
1849
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 ")}` : "";
1850
1985
  if (options?.limit !== void 0) {
1851
1986
  limitClause = " LIMIT ?";
1852
1987
  params.push(options.limit);
@@ -1860,6 +1995,7 @@ var SqliteContextStore = class extends ContextStore {
1860
1995
  c.id,
1861
1996
  c.userId,
1862
1997
  c.title,
1998
+ c.metadata,
1863
1999
  c.createdAt,
1864
2000
  c.updatedAt,
1865
2001
  COUNT(DISTINCT m.id) as messageCount,
@@ -1875,6 +2011,7 @@ var SqliteContextStore = class extends ContextStore {
1875
2011
  id: row.id,
1876
2012
  userId: row.userId,
1877
2013
  title: row.title ?? void 0,
2014
+ metadata: row.metadata ? JSON.parse(row.metadata) : void 0,
1878
2015
  messageCount: row.messageCount,
1879
2016
  branchCount: row.branchCount,
1880
2017
  createdAt: row.createdAt,
@@ -1906,9 +2043,16 @@ var SqliteContextStore = class extends ContextStore {
1906
2043
  async addMessage(message2) {
1907
2044
  this.#db.prepare(
1908
2045
  `INSERT INTO messages (id, chatId, parentId, name, type, data, createdAt)
1909
- 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
+ )
1910
2055
  ON CONFLICT(id) DO UPDATE SET
1911
- parentId = excluded.parentId,
1912
2056
  name = excluded.name,
1913
2057
  type = excluded.type,
1914
2058
  data = excluded.data`
@@ -2383,13 +2527,9 @@ var Agent = class _Agent {
2383
2527
  writer.write({ type: "finish" });
2384
2528
  return;
2385
2529
  }
2386
- writer.write({
2387
- type: "text-delta",
2388
- id: generateId2(),
2389
- delta: ` ${failureFeedback}`
2390
- });
2530
+ writeText(writer, failureFeedback);
2391
2531
  const selfCorrectionText = accumulatedText + " " + failureFeedback;
2392
- context.set(assistantText(selfCorrectionText));
2532
+ context.set(lastAssistantMessage(selfCorrectionText));
2393
2533
  await context.save();
2394
2534
  currentResult = await this.#createRawStream(
2395
2535
  contextVariables,
@@ -2442,6 +2582,22 @@ var repairToolCall = async ({
2442
2582
  });
2443
2583
  return { ...toolCall, input: JSON.stringify(output) };
2444
2584
  };
2585
+ function writeText(writer, text) {
2586
+ const feedbackPartId = generateId2();
2587
+ writer.write({
2588
+ id: feedbackPartId,
2589
+ type: "text-start"
2590
+ });
2591
+ writer.write({
2592
+ id: feedbackPartId,
2593
+ type: "text-delta",
2594
+ delta: ` ${text}`
2595
+ });
2596
+ writer.write({
2597
+ id: feedbackPartId,
2598
+ type: "text-end"
2599
+ });
2600
+ }
2445
2601
 
2446
2602
  // packages/text2sql/src/lib/adapters/groundings/report.grounding.ts
2447
2603
  var ReportGrounding = class extends AbstractGrounding {