@deepagents/text2sql 0.10.1 → 0.11.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 (98) hide show
  1. package/README.md +32 -41
  2. package/dist/index.d.ts +1 -6
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +2338 -2395
  5. package/dist/index.js.map +4 -4
  6. package/dist/lib/adapters/adapter.d.ts +13 -1
  7. package/dist/lib/adapters/adapter.d.ts.map +1 -1
  8. package/dist/lib/adapters/groundings/abstract.grounding.d.ts +19 -3
  9. package/dist/lib/adapters/groundings/abstract.grounding.d.ts.map +1 -1
  10. package/dist/lib/adapters/groundings/column-stats.grounding.d.ts +1 -2
  11. package/dist/lib/adapters/groundings/column-stats.grounding.d.ts.map +1 -1
  12. package/dist/lib/adapters/groundings/column-values.grounding.d.ts +1 -2
  13. package/dist/lib/adapters/groundings/column-values.grounding.d.ts.map +1 -1
  14. package/dist/lib/adapters/groundings/constraint.grounding.d.ts +1 -1
  15. package/dist/lib/adapters/groundings/constraint.grounding.d.ts.map +1 -1
  16. package/dist/lib/adapters/groundings/index.js +15 -222
  17. package/dist/lib/adapters/groundings/index.js.map +3 -3
  18. package/dist/lib/adapters/groundings/indexes.grounding.d.ts +1 -1
  19. package/dist/lib/adapters/groundings/indexes.grounding.d.ts.map +1 -1
  20. package/dist/lib/adapters/groundings/info.grounding.d.ts +1 -1
  21. package/dist/lib/adapters/groundings/info.grounding.d.ts.map +1 -1
  22. package/dist/lib/adapters/groundings/report.grounding.d.ts +1 -1
  23. package/dist/lib/adapters/groundings/report.grounding.d.ts.map +1 -1
  24. package/dist/lib/adapters/groundings/row-count.grounding.d.ts +1 -1
  25. package/dist/lib/adapters/groundings/row-count.grounding.d.ts.map +1 -1
  26. package/dist/lib/adapters/groundings/table.grounding.d.ts +3 -3
  27. package/dist/lib/adapters/groundings/table.grounding.d.ts.map +1 -1
  28. package/dist/lib/adapters/groundings/view.grounding.d.ts +1 -1
  29. package/dist/lib/adapters/groundings/view.grounding.d.ts.map +1 -1
  30. package/dist/lib/adapters/mysql/index.js +343 -315
  31. package/dist/lib/adapters/mysql/index.js.map +4 -4
  32. package/dist/lib/adapters/postgres/index.js +385 -357
  33. package/dist/lib/adapters/postgres/index.js.map +4 -4
  34. package/dist/lib/adapters/spreadsheet/index.js +290 -223
  35. package/dist/lib/adapters/spreadsheet/index.js.map +4 -4
  36. package/dist/lib/adapters/sqlite/index.js +307 -279
  37. package/dist/lib/adapters/sqlite/index.js.map +4 -4
  38. package/dist/lib/adapters/sqlserver/index.js +383 -355
  39. package/dist/lib/adapters/sqlserver/index.js.map +4 -4
  40. package/dist/lib/agents/developer.agent.d.ts +33 -23
  41. package/dist/lib/agents/developer.agent.d.ts.map +1 -1
  42. package/dist/lib/agents/sql.agent.d.ts +4 -4
  43. package/dist/lib/agents/sql.agent.d.ts.map +1 -1
  44. package/dist/lib/agents/teachables.agent.d.ts +2 -2
  45. package/dist/lib/agents/teachables.agent.d.ts.map +1 -1
  46. package/dist/lib/agents/text2sql.agent.d.ts +18 -71
  47. package/dist/lib/agents/text2sql.agent.d.ts.map +1 -1
  48. package/dist/lib/fragments/schema.d.ts +214 -0
  49. package/dist/lib/fragments/schema.d.ts.map +1 -0
  50. package/dist/lib/instructions.d.ts +29 -2
  51. package/dist/lib/instructions.d.ts.map +1 -1
  52. package/dist/lib/instructions.js +336 -319
  53. package/dist/lib/instructions.js.map +4 -4
  54. package/dist/lib/sql.d.ts +13 -103
  55. package/dist/lib/sql.d.ts.map +1 -1
  56. package/dist/lib/synthesis/extractors/base-contextual-extractor.d.ts +2 -2
  57. package/dist/lib/synthesis/extractors/base-contextual-extractor.d.ts.map +1 -1
  58. package/dist/lib/synthesis/extractors/message-extractor.d.ts +1 -2
  59. package/dist/lib/synthesis/extractors/message-extractor.d.ts.map +1 -1
  60. package/dist/lib/synthesis/extractors/sql-extractor.d.ts.map +1 -1
  61. package/dist/lib/synthesis/index.js +1794 -572
  62. package/dist/lib/synthesis/index.js.map +4 -4
  63. package/dist/lib/synthesis/synthesizers/depth-evolver.d.ts.map +1 -1
  64. package/dist/lib/synthesis/synthesizers/persona-generator.d.ts +7 -17
  65. package/dist/lib/synthesis/synthesizers/persona-generator.d.ts.map +1 -1
  66. package/dist/lib/synthesis/synthesizers/schema-synthesizer.d.ts +2 -2
  67. package/dist/lib/synthesis/synthesizers/schema-synthesizer.d.ts.map +1 -1
  68. package/dist/lib/synthesis/synthesizers/teachings-generator.d.ts +8 -20
  69. package/dist/lib/synthesis/synthesizers/teachings-generator.d.ts.map +1 -1
  70. package/dist/lib/teach/teachings.d.ts +2 -2
  71. package/dist/lib/teach/teachings.d.ts.map +1 -1
  72. package/package.json +4 -3
  73. package/dist/lib/agents/chat1.agent.d.ts +0 -50
  74. package/dist/lib/agents/chat1.agent.d.ts.map +0 -1
  75. package/dist/lib/agents/chat2.agent.d.ts +0 -68
  76. package/dist/lib/agents/chat2.agent.d.ts.map +0 -1
  77. package/dist/lib/agents/chat3.agent.d.ts +0 -80
  78. package/dist/lib/agents/chat3.agent.d.ts.map +0 -1
  79. package/dist/lib/agents/chat4.agent.d.ts +0 -88
  80. package/dist/lib/agents/chat4.agent.d.ts.map +0 -1
  81. package/dist/lib/history/history.d.ts +0 -41
  82. package/dist/lib/history/history.d.ts.map +0 -1
  83. package/dist/lib/history/memory.history.d.ts +0 -5
  84. package/dist/lib/history/memory.history.d.ts.map +0 -1
  85. package/dist/lib/history/sqlite.history.d.ts +0 -15
  86. package/dist/lib/history/sqlite.history.d.ts.map +0 -1
  87. package/dist/lib/memory/memory.prompt.d.ts +0 -3
  88. package/dist/lib/memory/memory.prompt.d.ts.map +0 -1
  89. package/dist/lib/memory/memory.store.d.ts +0 -5
  90. package/dist/lib/memory/memory.store.d.ts.map +0 -1
  91. package/dist/lib/memory/sqlite.store.d.ts +0 -14
  92. package/dist/lib/memory/sqlite.store.d.ts.map +0 -1
  93. package/dist/lib/memory/store.d.ts +0 -40
  94. package/dist/lib/memory/store.d.ts.map +0 -1
  95. package/dist/lib/teach/teachables.d.ts +0 -648
  96. package/dist/lib/teach/teachables.d.ts.map +0 -1
  97. package/dist/lib/teach/xml.d.ts +0 -6
  98. package/dist/lib/teach/xml.d.ts.map +0 -1
@@ -1,403 +1,420 @@
1
- // packages/text2sql/src/lib/teach/xml.ts
2
- function wrapBlock(tag, children) {
3
- const content = children.filter((child) => Boolean(child)).join("\n");
4
- if (!content) {
5
- return "";
6
- }
7
- return `<${tag}>
8
- ${indentBlock(content, 2)}
9
- </${tag}>`;
1
+ // packages/context/dist/index.js
2
+ import { encode } from "gpt-tokenizer";
3
+ import { generateId } from "ai";
4
+ import pluralize from "pluralize";
5
+ import { titlecase } from "stringcase";
6
+ import { defineCommand } from "just-bash";
7
+ import spawn from "nano-spawn";
8
+ import "bash-tool";
9
+ import spawn2 from "nano-spawn";
10
+ import {
11
+ createBashTool
12
+ } from "bash-tool";
13
+ import YAML from "yaml";
14
+ import { DatabaseSync } from "node:sqlite";
15
+ import {
16
+ Output,
17
+ convertToModelMessages,
18
+ createUIMessageStream,
19
+ generateId as generateId2,
20
+ generateText,
21
+ smoothStream,
22
+ stepCountIs,
23
+ streamText
24
+ } from "ai";
25
+ import chalk from "chalk";
26
+ import "zod";
27
+ import "@deepagents/agent";
28
+ function isFragment(data) {
29
+ return typeof data === "object" && data !== null && "name" in data && "data" in data && typeof data.name === "string";
10
30
  }
11
- function list(tag, values, childTag) {
12
- if (!values.length) {
13
- return "";
31
+ function isFragmentObject(data) {
32
+ return typeof data === "object" && data !== null && !Array.isArray(data) && !isFragment(data);
33
+ }
34
+ function fragment(name, ...children) {
35
+ return {
36
+ name,
37
+ data: children
38
+ };
39
+ }
40
+ function role(content) {
41
+ return {
42
+ name: "role",
43
+ data: content
44
+ };
45
+ }
46
+ var ContextRenderer = class {
47
+ options;
48
+ constructor(options = {}) {
49
+ this.options = options;
50
+ }
51
+ /**
52
+ * Check if data is a primitive (string, number, boolean).
53
+ */
54
+ isPrimitive(data) {
55
+ return typeof data === "string" || typeof data === "number" || typeof data === "boolean";
56
+ }
57
+ /**
58
+ * Group fragments by name for groupFragments option.
59
+ */
60
+ groupByName(fragments) {
61
+ const groups = /* @__PURE__ */ new Map();
62
+ for (const fragment2 of fragments) {
63
+ const existing = groups.get(fragment2.name) ?? [];
64
+ existing.push(fragment2);
65
+ groups.set(fragment2.name, existing);
66
+ }
67
+ return groups;
68
+ }
69
+ /**
70
+ * Template method - dispatches value to appropriate handler.
71
+ */
72
+ renderValue(key, value, ctx) {
73
+ if (value == null) {
74
+ return "";
75
+ }
76
+ if (isFragment(value)) {
77
+ return this.renderFragment(value, ctx);
78
+ }
79
+ if (Array.isArray(value)) {
80
+ return this.renderArray(key, value, ctx);
81
+ }
82
+ if (isFragmentObject(value)) {
83
+ return this.renderObject(key, value, ctx);
84
+ }
85
+ return this.renderPrimitive(key, String(value), ctx);
86
+ }
87
+ /**
88
+ * Render all entries of an object.
89
+ */
90
+ renderEntries(data, ctx) {
91
+ return Object.entries(data).map(([key, value]) => this.renderValue(key, value, ctx)).filter(Boolean);
92
+ }
93
+ };
94
+ var XmlRenderer = class extends ContextRenderer {
95
+ render(fragments) {
96
+ return fragments.map((f) => this.#renderTopLevel(f)).filter(Boolean).join("\n");
97
+ }
98
+ #renderTopLevel(fragment2) {
99
+ if (this.isPrimitive(fragment2.data)) {
100
+ return this.#leafRoot(fragment2.name, String(fragment2.data));
101
+ }
102
+ if (Array.isArray(fragment2.data)) {
103
+ return this.#renderArray(fragment2.name, fragment2.data, 0);
104
+ }
105
+ if (isFragment(fragment2.data)) {
106
+ const child = this.renderFragment(fragment2.data, { depth: 1, path: [] });
107
+ return this.#wrap(fragment2.name, [child]);
108
+ }
109
+ return this.#wrap(
110
+ fragment2.name,
111
+ this.renderEntries(fragment2.data, { depth: 1, path: [] })
112
+ );
14
113
  }
15
- const children = values.map((value) => leaf(childTag, value)).join("\n");
16
- return `<${tag}>
17
- ${indentBlock(children, 2)}
114
+ #renderArray(name, items, depth) {
115
+ const fragmentItems = items.filter(isFragment);
116
+ const nonFragmentItems = items.filter((item) => !isFragment(item));
117
+ const children = [];
118
+ for (const item of nonFragmentItems) {
119
+ if (item != null) {
120
+ children.push(
121
+ this.#leaf(pluralize.singular(name), String(item), depth + 1)
122
+ );
123
+ }
124
+ }
125
+ if (this.options.groupFragments && fragmentItems.length > 0) {
126
+ const groups = this.groupByName(fragmentItems);
127
+ for (const [groupName, groupFragments] of groups) {
128
+ const groupChildren = groupFragments.map(
129
+ (frag) => this.renderFragment(frag, { depth: depth + 2, path: [] })
130
+ );
131
+ const pluralName = pluralize.plural(groupName);
132
+ children.push(this.#wrapIndented(pluralName, groupChildren, depth + 1));
133
+ }
134
+ } else {
135
+ for (const frag of fragmentItems) {
136
+ children.push(
137
+ this.renderFragment(frag, { depth: depth + 1, path: [] })
138
+ );
139
+ }
140
+ }
141
+ return this.#wrap(name, children);
142
+ }
143
+ #leafRoot(tag, value) {
144
+ const safe = this.#escape(value);
145
+ if (safe.includes("\n")) {
146
+ return `<${tag}>
147
+ ${this.#indent(safe, 2)}
18
148
  </${tag}>`;
19
- }
20
- function leaf(tag, value) {
21
- const safe = escapeXml(value);
22
- if (safe.includes("\n")) {
149
+ }
150
+ return `<${tag}>${safe}</${tag}>`;
151
+ }
152
+ renderFragment(fragment2, ctx) {
153
+ const { name, data } = fragment2;
154
+ if (this.isPrimitive(data)) {
155
+ return this.#leaf(name, String(data), ctx.depth);
156
+ }
157
+ if (isFragment(data)) {
158
+ const child = this.renderFragment(data, { ...ctx, depth: ctx.depth + 1 });
159
+ return this.#wrapIndented(name, [child], ctx.depth);
160
+ }
161
+ if (Array.isArray(data)) {
162
+ return this.#renderArrayIndented(name, data, ctx.depth);
163
+ }
164
+ const children = this.renderEntries(data, { ...ctx, depth: ctx.depth + 1 });
165
+ return this.#wrapIndented(name, children, ctx.depth);
166
+ }
167
+ #renderArrayIndented(name, items, depth) {
168
+ const fragmentItems = items.filter(isFragment);
169
+ const nonFragmentItems = items.filter((item) => !isFragment(item));
170
+ const children = [];
171
+ for (const item of nonFragmentItems) {
172
+ if (item != null) {
173
+ children.push(
174
+ this.#leaf(pluralize.singular(name), String(item), depth + 1)
175
+ );
176
+ }
177
+ }
178
+ if (this.options.groupFragments && fragmentItems.length > 0) {
179
+ const groups = this.groupByName(fragmentItems);
180
+ for (const [groupName, groupFragments] of groups) {
181
+ const groupChildren = groupFragments.map(
182
+ (frag) => this.renderFragment(frag, { depth: depth + 2, path: [] })
183
+ );
184
+ const pluralName = pluralize.plural(groupName);
185
+ children.push(this.#wrapIndented(pluralName, groupChildren, depth + 1));
186
+ }
187
+ } else {
188
+ for (const frag of fragmentItems) {
189
+ children.push(
190
+ this.renderFragment(frag, { depth: depth + 1, path: [] })
191
+ );
192
+ }
193
+ }
194
+ return this.#wrapIndented(name, children, depth);
195
+ }
196
+ renderPrimitive(key, value, ctx) {
197
+ return this.#leaf(key, value, ctx.depth);
198
+ }
199
+ renderArray(key, items, ctx) {
200
+ if (!items.length) {
201
+ return "";
202
+ }
203
+ const itemTag = pluralize.singular(key);
204
+ const children = items.filter((item) => item != null).map((item) => this.#leaf(itemTag, String(item), ctx.depth + 1));
205
+ return this.#wrapIndented(key, children, ctx.depth);
206
+ }
207
+ renderObject(key, obj, ctx) {
208
+ const children = this.renderEntries(obj, { ...ctx, depth: ctx.depth + 1 });
209
+ return this.#wrapIndented(key, children, ctx.depth);
210
+ }
211
+ #escape(value) {
212
+ if (value == null) {
213
+ return "";
214
+ }
215
+ return value.replaceAll(/&/g, "&amp;").replaceAll(/</g, "&lt;").replaceAll(/>/g, "&gt;").replaceAll(/"/g, "&quot;").replaceAll(/'/g, "&apos;");
216
+ }
217
+ #indent(text, spaces) {
218
+ if (!text.trim()) {
219
+ return "";
220
+ }
221
+ const padding = " ".repeat(spaces);
222
+ return text.split("\n").map((line) => line.length ? padding + line : padding).join("\n");
223
+ }
224
+ #leaf(tag, value, depth) {
225
+ const safe = this.#escape(value);
226
+ const pad = " ".repeat(depth);
227
+ if (safe.includes("\n")) {
228
+ return `${pad}<${tag}>
229
+ ${this.#indent(safe, (depth + 1) * 2)}
230
+ ${pad}</${tag}>`;
231
+ }
232
+ return `${pad}<${tag}>${safe}</${tag}>`;
233
+ }
234
+ #wrap(tag, children) {
235
+ const content = children.filter(Boolean).join("\n");
236
+ if (!content) {
237
+ return "";
238
+ }
23
239
  return `<${tag}>
24
- ${indentBlock(safe, 2)}
240
+ ${content}
25
241
  </${tag}>`;
26
242
  }
27
- return `<${tag}>${safe}</${tag}>`;
28
- }
29
- function indentBlock(text, spaces) {
30
- if (!text.trim()) {
31
- return "";
32
- }
33
- const padding = " ".repeat(spaces);
34
- return text.split("\n").map((line) => line.length ? padding + line : padding).join("\n");
35
- }
36
- function escapeXml(value) {
37
- if (value == null) {
38
- return "";
243
+ #wrapIndented(tag, children, depth) {
244
+ const content = children.filter(Boolean).join("\n");
245
+ if (!content) {
246
+ return "";
247
+ }
248
+ const pad = " ".repeat(depth);
249
+ return `${pad}<${tag}>
250
+ ${content}
251
+ ${pad}</${tag}>`;
39
252
  }
40
- return value.replaceAll(/&/g, "&amp;").replaceAll(/</g, "&lt;").replaceAll(/>/g, "&gt;").replaceAll(/"/g, "&quot;").replaceAll(/'/g, "&apos;");
41
- }
42
-
43
- // packages/text2sql/src/lib/teach/teachables.ts
253
+ };
44
254
  function term(name, definition) {
45
255
  return {
46
- type: "term",
47
- encode: () => ({ type: "term", name, definition }),
48
- decode: () => wrapBlock("term", [leaf("name", name), leaf("definition", definition)])
256
+ name: "term",
257
+ data: { name, definition }
49
258
  };
50
259
  }
51
260
  function hint(text) {
52
261
  return {
53
- type: "hint",
54
- encode: () => ({ type: "hint", text }),
55
- decode: () => leaf("hint", text)
262
+ name: "hint",
263
+ data: text
56
264
  };
57
265
  }
58
266
  function guardrail(input) {
59
- const { rule, reason, action } = input;
60
267
  return {
61
- type: "guardrail",
62
- encode: () => ({ type: "guardrail", rule, reason, action }),
63
- decode: () => wrapBlock("guardrail", [
64
- leaf("rule", rule),
65
- reason ? leaf("reason", reason) : "",
66
- action ? leaf("action", action) : ""
67
- ])
268
+ name: "guardrail",
269
+ data: {
270
+ rule: input.rule,
271
+ ...input.reason && { reason: input.reason },
272
+ ...input.action && { action: input.action }
273
+ }
68
274
  };
69
275
  }
70
276
  function explain(input) {
71
- const { concept, explanation, therefore } = input;
72
277
  return {
73
- type: "explain",
74
- encode: () => ({ type: "explain", concept, explanation, therefore }),
75
- decode: () => wrapBlock("explanation", [
76
- leaf("concept", concept),
77
- leaf("details", explanation),
78
- therefore ? leaf("therefore", therefore) : ""
79
- ])
278
+ name: "explain",
279
+ data: {
280
+ concept: input.concept,
281
+ explanation: input.explanation,
282
+ ...input.therefore && { therefore: input.therefore }
283
+ }
80
284
  };
81
285
  }
82
286
  function example(input) {
83
- const { question, answer, note } = input;
84
287
  return {
85
- type: "example",
86
- encode: () => ({ type: "example", question, answer, note }),
87
- decode: () => wrapBlock("example", [
88
- leaf("question", question),
89
- leaf("answer", answer),
90
- note ? leaf("note", note) : ""
91
- ])
288
+ name: "example",
289
+ data: {
290
+ question: input.question,
291
+ answer: input.answer,
292
+ ...input.note && { note: input.note }
293
+ }
92
294
  };
93
295
  }
94
296
  function clarification(input) {
95
- const { when, ask, reason } = input;
96
297
  return {
97
- type: "clarification",
98
- encode: () => ({ type: "clarification", when, ask, reason }),
99
- decode: () => wrapBlock("clarification", [
100
- leaf("when", when),
101
- leaf("ask", ask),
102
- leaf("reason", reason)
103
- ])
298
+ name: "clarification",
299
+ data: {
300
+ when: input.when,
301
+ ask: input.ask,
302
+ reason: input.reason
303
+ }
104
304
  };
105
305
  }
106
306
  function workflow(input) {
107
- const { task, steps, triggers, notes } = input;
108
307
  return {
109
- type: "workflow",
110
- encode: () => ({ type: "workflow", task, steps, triggers, notes }),
111
- decode: () => wrapBlock("workflow", [
112
- leaf("task", task),
113
- triggers?.length ? list("triggers", triggers, "trigger") : "",
114
- list("steps", steps, "step"),
115
- notes ? leaf("notes", notes) : ""
116
- ])
308
+ name: "workflow",
309
+ data: {
310
+ task: input.task,
311
+ steps: input.steps,
312
+ ...input.triggers?.length && { triggers: input.triggers },
313
+ ...input.notes && { notes: input.notes }
314
+ }
117
315
  };
118
316
  }
119
317
  function quirk(input) {
120
- const { issue, workaround } = input;
121
318
  return {
122
- type: "quirk",
123
- encode: () => ({ type: "quirk", issue, workaround }),
124
- decode: () => wrapBlock("quirk", [
125
- leaf("issue", issue),
126
- leaf("workaround", workaround)
127
- ])
319
+ name: "quirk",
320
+ data: {
321
+ issue: input.issue,
322
+ workaround: input.workaround
323
+ }
128
324
  };
129
325
  }
130
326
  function styleGuide(input) {
131
- const { prefer, never, always } = input;
132
327
  return {
133
- type: "styleGuide",
134
- encode: () => ({ type: "styleGuide", prefer, never, always }),
135
- decode: () => wrapBlock("style_guide", [
136
- leaf("prefer", prefer),
137
- always ? leaf("always", always) : "",
138
- never ? leaf("never", never) : ""
139
- ])
328
+ name: "styleGuide",
329
+ data: {
330
+ prefer: input.prefer,
331
+ ...input.never && { never: input.never },
332
+ ...input.always && { always: input.always }
333
+ }
140
334
  };
141
335
  }
142
336
  function analogy(input) {
143
- const { concept, relationship, insight, therefore, pitfall } = input;
144
337
  return {
145
- type: "analogy",
146
- encode: () => ({
147
- type: "analogy",
148
- concept,
149
- relationship,
150
- insight,
151
- therefore,
152
- pitfall
153
- }),
154
- decode: () => wrapBlock("analogy", [
155
- list("concepts", concept, "concept"),
156
- leaf("relationship", relationship),
157
- insight ? leaf("insight", insight) : "",
158
- therefore ? leaf("therefore", therefore) : "",
159
- pitfall ? leaf("pitfall", pitfall) : ""
160
- ])
338
+ name: "analogy",
339
+ data: {
340
+ concepts: input.concepts,
341
+ relationship: input.relationship,
342
+ ...input.insight && { insight: input.insight },
343
+ ...input.therefore && { therefore: input.therefore },
344
+ ...input.pitfall && { pitfall: input.pitfall }
345
+ }
161
346
  };
162
347
  }
163
348
  function glossary(entries) {
164
349
  return {
165
- type: "glossary",
166
- encode: () => ({ type: "glossary", entries }),
167
- decode: () => wrapBlock(
168
- "glossary",
169
- Object.entries(entries).map(
170
- ([term2, sql]) => wrapBlock("entry", [leaf("term", term2), leaf("sql", sql)])
171
- )
172
- )
350
+ name: "glossary",
351
+ data: Object.entries(entries).map(([term2, expression]) => ({
352
+ term: term2,
353
+ expression
354
+ }))
173
355
  };
174
356
  }
175
357
  function identity(input) {
176
- const { name, role } = input;
177
358
  return {
178
- type: "identity",
179
- encode: () => ({ type: "identity", name, role }),
180
- decode: () => wrapBlock("identity", [
181
- name ? leaf("name", name) : "",
182
- role ? leaf("role", role) : ""
183
- ])
359
+ name: "identity",
360
+ data: {
361
+ ...input.name && { name: input.name },
362
+ ...input.role && { role: input.role }
363
+ }
184
364
  };
185
365
  }
186
366
  function persona(input) {
187
- const { name, role, tone } = input;
188
367
  return {
189
- type: "persona",
190
- encode: () => ({ type: "persona", name, role, tone: tone ?? "" }),
191
- decode: () => wrapBlock("persona", [
192
- leaf("name", name),
193
- leaf("role", role),
194
- tone ? leaf("tone", tone) : ""
195
- ])
368
+ name: "persona",
369
+ data: {
370
+ name: input.name,
371
+ role: input.role,
372
+ ...input.tone && { tone: input.tone }
373
+ }
196
374
  };
197
375
  }
198
- function alias(termName, meaning) {
376
+ function alias(term2, meaning) {
199
377
  return {
200
- type: "alias",
201
- encode: () => ({ type: "alias", term: termName, meaning }),
202
- decode: () => wrapBlock("alias", [leaf("term", termName), leaf("meaning", meaning)])
378
+ name: "alias",
379
+ data: { term: term2, meaning }
203
380
  };
204
381
  }
205
382
  function preference(aspect, value) {
206
383
  return {
207
- type: "preference",
208
- encode: () => ({ type: "preference", aspect, value }),
209
- decode: () => wrapBlock("preference", [leaf("aspect", aspect), leaf("value", value)])
384
+ name: "preference",
385
+ data: { aspect, value }
210
386
  };
211
387
  }
212
- function context(description) {
388
+ function userContext(description) {
213
389
  return {
214
- type: "context",
215
- encode: () => ({ type: "context", description }),
216
- decode: () => leaf("context", description)
390
+ name: "userContext",
391
+ data: description
217
392
  };
218
393
  }
219
394
  function correction(subject, clarification2) {
220
395
  return {
221
- type: "correction",
222
- encode: () => ({ type: "correction", subject, clarification: clarification2 }),
223
- decode: () => wrapBlock("correction", [
224
- leaf("subject", subject),
225
- leaf("clarification", clarification2)
226
- ])
227
- };
228
- }
229
- function teachable(tag, ...teachables) {
230
- return {
231
- type: "user_profile",
232
- encode: () => teachables[0]?.encode() ?? { type: "context", description: "" },
233
- decode: () => toInstructions(tag, ...teachables)
396
+ name: "correction",
397
+ data: { subject, clarification: clarification2 }
234
398
  };
235
399
  }
236
- function toInstructions(tag, ...teachables) {
237
- if (!teachables.length) {
238
- return "";
239
- }
240
- const grouped = /* @__PURE__ */ new Map();
241
- for (const teachable2 of teachables) {
242
- const existing = grouped.get(teachable2.type) ?? [];
243
- existing.push(teachable2);
244
- grouped.set(teachable2.type, existing);
245
- }
246
- const definedTypes = new Set(SECTION_ORDER.map((s) => s.type));
247
- const sections = SECTION_ORDER.map(({ type, tag: tag2 }) => {
248
- const items = grouped.get(type);
249
- if (!items?.length) {
250
- return "";
251
- }
252
- const renderedItems = items.map((item) => item.decode().trim()).filter(Boolean).map((item) => indentBlock(item, 2)).join("\n");
253
- if (!renderedItems.length) {
254
- return "";
255
- }
256
- return `<${tag2}>
257
- ${renderedItems}
258
- </${tag2}>`;
259
- }).filter((section) => Boolean(section));
260
- for (const [type, items] of grouped) {
261
- if (definedTypes.has(type)) {
262
- continue;
263
- }
264
- const renderedItems = items.map((item) => item.decode().trim()).filter(Boolean).map((item) => indentBlock(item, 2)).join("\n");
265
- if (renderedItems.length) {
266
- sections.push(renderedItems);
267
- }
268
- }
269
- if (!sections.length) {
400
+
401
+ // packages/text2sql/src/lib/instructions.ts
402
+ function toInstructions(tag, ...fragments) {
403
+ if (fragments.length === 0) {
270
404
  return "";
271
405
  }
272
- const content = indentBlock(sections.join("\n"), 2);
273
- return `<${tag}>
274
- ${content}
275
- </${tag}>`;
276
- }
277
- var SECTION_ORDER = [
278
- // User context (render first - most important for personalization)
279
- { type: "identity", tag: "identity" },
280
- { type: "persona", tag: "persona" },
281
- { type: "context", tag: "user_context" },
282
- { type: "preference", tag: "user_preferences" },
283
- { type: "alias", tag: "user_vocabulary" },
284
- { type: "correction", tag: "user_corrections" },
285
- // Domain knowledge
286
- { type: "guardrail", tag: "guardrails" },
287
- { type: "styleGuide", tag: "style_guides" },
288
- { type: "hint", tag: "hints" },
289
- { type: "clarification", tag: "clarifications" },
290
- { type: "workflow", tag: "workflows" },
291
- { type: "quirk", tag: "quirks" },
292
- { type: "term", tag: "terminology" },
293
- { type: "explain", tag: "explanations" },
294
- { type: "analogy", tag: "analogies" },
295
- { type: "glossary", tag: "glossary" },
296
- { type: "example", tag: "examples" }
297
- ];
298
- function toTeachables(generated) {
299
- return generated.map((item) => {
300
- switch (item.type) {
301
- case "persona":
302
- return persona({ name: item.name, role: item.role, tone: item.tone });
303
- case "term":
304
- return term(item.name, item.definition);
305
- case "hint":
306
- return hint(item.text);
307
- case "guardrail":
308
- return guardrail({
309
- rule: item.rule,
310
- reason: item.reason,
311
- action: item.action
312
- });
313
- case "explain":
314
- return explain({
315
- concept: item.concept,
316
- explanation: item.explanation,
317
- therefore: item.therefore
318
- });
319
- case "example":
320
- return example({
321
- question: item.question,
322
- answer: item.answer,
323
- note: item.note
324
- });
325
- case "clarification":
326
- return clarification({
327
- when: item.when,
328
- ask: item.ask,
329
- reason: item.reason
330
- });
331
- case "workflow":
332
- return workflow({
333
- task: item.task,
334
- steps: item.steps,
335
- triggers: item.triggers,
336
- notes: item.notes
337
- });
338
- case "quirk":
339
- return quirk({
340
- issue: item.issue,
341
- workaround: item.workaround
342
- });
343
- case "styleGuide":
344
- return styleGuide({
345
- prefer: item.prefer,
346
- never: item.never,
347
- always: item.always
348
- });
349
- case "analogy":
350
- return analogy({
351
- concept: item.concept,
352
- relationship: item.relationship,
353
- insight: item.insight,
354
- therefore: item.therefore,
355
- pitfall: item.pitfall
356
- });
357
- case "glossary":
358
- return glossary(item.entries);
359
- // User-specific teachable types
360
- case "identity":
361
- return identity({ name: item.name, role: item.role });
362
- case "alias":
363
- return alias(item.term, item.meaning);
364
- case "preference":
365
- return preference(item.aspect, item.value);
366
- case "context":
367
- return context(item.description);
368
- case "correction":
369
- return correction(item.subject, item.clarification);
370
- }
371
- });
406
+ const renderer = new XmlRenderer({ groupFragments: true });
407
+ const wrapped = fragment(tag, ...fragments);
408
+ return renderer.render([wrapped]);
372
409
  }
373
- function fromTeachables(teachables) {
374
- return teachables.map((t) => t.encode());
375
- }
376
- var teachables_default = {
377
- persona,
378
- term,
379
- hint,
380
- guardrail,
381
- explain,
382
- example,
383
- clarification,
384
- workflow,
385
- quirk,
386
- styleGuide,
387
- analogy,
388
- glossary,
389
- teachable
390
- };
391
410
  export {
392
411
  alias,
393
412
  analogy,
394
413
  clarification,
395
- context,
396
414
  correction,
397
- teachables_default as default,
398
415
  example,
399
416
  explain,
400
- fromTeachables,
417
+ fragment,
401
418
  glossary,
402
419
  guardrail,
403
420
  hint,
@@ -405,11 +422,11 @@ export {
405
422
  persona,
406
423
  preference,
407
424
  quirk,
425
+ role,
408
426
  styleGuide,
409
- teachable,
410
427
  term,
411
428
  toInstructions,
412
- toTeachables,
429
+ userContext,
413
430
  workflow
414
431
  };
415
432
  //# sourceMappingURL=instructions.js.map