@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.
- package/README.md +32 -41
- package/dist/index.d.ts +1 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2338 -2395
- package/dist/index.js.map +4 -4
- package/dist/lib/adapters/adapter.d.ts +13 -1
- package/dist/lib/adapters/adapter.d.ts.map +1 -1
- package/dist/lib/adapters/groundings/abstract.grounding.d.ts +19 -3
- package/dist/lib/adapters/groundings/abstract.grounding.d.ts.map +1 -1
- package/dist/lib/adapters/groundings/column-stats.grounding.d.ts +1 -2
- package/dist/lib/adapters/groundings/column-stats.grounding.d.ts.map +1 -1
- package/dist/lib/adapters/groundings/column-values.grounding.d.ts +1 -2
- package/dist/lib/adapters/groundings/column-values.grounding.d.ts.map +1 -1
- package/dist/lib/adapters/groundings/constraint.grounding.d.ts +1 -1
- package/dist/lib/adapters/groundings/constraint.grounding.d.ts.map +1 -1
- package/dist/lib/adapters/groundings/index.js +15 -222
- package/dist/lib/adapters/groundings/index.js.map +3 -3
- package/dist/lib/adapters/groundings/indexes.grounding.d.ts +1 -1
- package/dist/lib/adapters/groundings/indexes.grounding.d.ts.map +1 -1
- package/dist/lib/adapters/groundings/info.grounding.d.ts +1 -1
- package/dist/lib/adapters/groundings/info.grounding.d.ts.map +1 -1
- package/dist/lib/adapters/groundings/report.grounding.d.ts +1 -1
- package/dist/lib/adapters/groundings/report.grounding.d.ts.map +1 -1
- package/dist/lib/adapters/groundings/row-count.grounding.d.ts +1 -1
- package/dist/lib/adapters/groundings/row-count.grounding.d.ts.map +1 -1
- package/dist/lib/adapters/groundings/table.grounding.d.ts +3 -3
- package/dist/lib/adapters/groundings/table.grounding.d.ts.map +1 -1
- package/dist/lib/adapters/groundings/view.grounding.d.ts +1 -1
- package/dist/lib/adapters/groundings/view.grounding.d.ts.map +1 -1
- package/dist/lib/adapters/mysql/index.js +343 -315
- package/dist/lib/adapters/mysql/index.js.map +4 -4
- package/dist/lib/adapters/postgres/index.js +385 -357
- package/dist/lib/adapters/postgres/index.js.map +4 -4
- package/dist/lib/adapters/spreadsheet/index.js +290 -223
- package/dist/lib/adapters/spreadsheet/index.js.map +4 -4
- package/dist/lib/adapters/sqlite/index.js +307 -279
- package/dist/lib/adapters/sqlite/index.js.map +4 -4
- package/dist/lib/adapters/sqlserver/index.js +383 -355
- package/dist/lib/adapters/sqlserver/index.js.map +4 -4
- package/dist/lib/agents/developer.agent.d.ts +33 -23
- package/dist/lib/agents/developer.agent.d.ts.map +1 -1
- package/dist/lib/agents/sql.agent.d.ts +4 -4
- package/dist/lib/agents/sql.agent.d.ts.map +1 -1
- package/dist/lib/agents/teachables.agent.d.ts +2 -2
- package/dist/lib/agents/teachables.agent.d.ts.map +1 -1
- package/dist/lib/agents/text2sql.agent.d.ts +18 -71
- package/dist/lib/agents/text2sql.agent.d.ts.map +1 -1
- package/dist/lib/fragments/schema.d.ts +214 -0
- package/dist/lib/fragments/schema.d.ts.map +1 -0
- package/dist/lib/instructions.d.ts +29 -2
- package/dist/lib/instructions.d.ts.map +1 -1
- package/dist/lib/instructions.js +336 -319
- package/dist/lib/instructions.js.map +4 -4
- package/dist/lib/sql.d.ts +13 -103
- package/dist/lib/sql.d.ts.map +1 -1
- package/dist/lib/synthesis/extractors/base-contextual-extractor.d.ts +2 -2
- package/dist/lib/synthesis/extractors/base-contextual-extractor.d.ts.map +1 -1
- package/dist/lib/synthesis/extractors/message-extractor.d.ts +1 -2
- package/dist/lib/synthesis/extractors/message-extractor.d.ts.map +1 -1
- package/dist/lib/synthesis/extractors/sql-extractor.d.ts.map +1 -1
- package/dist/lib/synthesis/index.js +1794 -572
- package/dist/lib/synthesis/index.js.map +4 -4
- package/dist/lib/synthesis/synthesizers/depth-evolver.d.ts.map +1 -1
- package/dist/lib/synthesis/synthesizers/persona-generator.d.ts +7 -17
- package/dist/lib/synthesis/synthesizers/persona-generator.d.ts.map +1 -1
- package/dist/lib/synthesis/synthesizers/schema-synthesizer.d.ts +2 -2
- package/dist/lib/synthesis/synthesizers/schema-synthesizer.d.ts.map +1 -1
- package/dist/lib/synthesis/synthesizers/teachings-generator.d.ts +8 -20
- package/dist/lib/synthesis/synthesizers/teachings-generator.d.ts.map +1 -1
- package/dist/lib/teach/teachings.d.ts +2 -2
- package/dist/lib/teach/teachings.d.ts.map +1 -1
- package/package.json +4 -3
- package/dist/lib/agents/chat1.agent.d.ts +0 -50
- package/dist/lib/agents/chat1.agent.d.ts.map +0 -1
- package/dist/lib/agents/chat2.agent.d.ts +0 -68
- package/dist/lib/agents/chat2.agent.d.ts.map +0 -1
- package/dist/lib/agents/chat3.agent.d.ts +0 -80
- package/dist/lib/agents/chat3.agent.d.ts.map +0 -1
- package/dist/lib/agents/chat4.agent.d.ts +0 -88
- package/dist/lib/agents/chat4.agent.d.ts.map +0 -1
- package/dist/lib/history/history.d.ts +0 -41
- package/dist/lib/history/history.d.ts.map +0 -1
- package/dist/lib/history/memory.history.d.ts +0 -5
- package/dist/lib/history/memory.history.d.ts.map +0 -1
- package/dist/lib/history/sqlite.history.d.ts +0 -15
- package/dist/lib/history/sqlite.history.d.ts.map +0 -1
- package/dist/lib/memory/memory.prompt.d.ts +0 -3
- package/dist/lib/memory/memory.prompt.d.ts.map +0 -1
- package/dist/lib/memory/memory.store.d.ts +0 -5
- package/dist/lib/memory/memory.store.d.ts.map +0 -1
- package/dist/lib/memory/sqlite.store.d.ts +0 -14
- package/dist/lib/memory/sqlite.store.d.ts.map +0 -1
- package/dist/lib/memory/store.d.ts +0 -40
- package/dist/lib/memory/store.d.ts.map +0 -1
- package/dist/lib/teach/teachables.d.ts +0 -648
- package/dist/lib/teach/teachables.d.ts.map +0 -1
- package/dist/lib/teach/xml.d.ts +0 -6
- package/dist/lib/teach/xml.d.ts.map +0 -1
package/dist/lib/instructions.js
CHANGED
|
@@ -1,403 +1,420 @@
|
|
|
1
|
-
// packages/
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
|
12
|
-
|
|
13
|
-
|
|
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
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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, "&").replaceAll(/</g, "<").replaceAll(/>/g, ">").replaceAll(/"/g, """).replaceAll(/'/g, "'");
|
|
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
|
-
${
|
|
240
|
+
${content}
|
|
25
241
|
</${tag}>`;
|
|
26
242
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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
|
-
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
// packages/text2sql/src/lib/teach/teachables.ts
|
|
253
|
+
};
|
|
44
254
|
function term(name, definition) {
|
|
45
255
|
return {
|
|
46
|
-
|
|
47
|
-
|
|
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
|
-
|
|
54
|
-
|
|
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
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
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
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
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
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
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
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
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
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
triggers?.length
|
|
114
|
-
|
|
115
|
-
|
|
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
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
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
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
always
|
|
138
|
-
|
|
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
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
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
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
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
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
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
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
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(
|
|
376
|
+
function alias(term2, meaning) {
|
|
199
377
|
return {
|
|
200
|
-
|
|
201
|
-
|
|
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
|
-
|
|
208
|
-
|
|
209
|
-
decode: () => wrapBlock("preference", [leaf("aspect", aspect), leaf("value", value)])
|
|
384
|
+
name: "preference",
|
|
385
|
+
data: { aspect, value }
|
|
210
386
|
};
|
|
211
387
|
}
|
|
212
|
-
function
|
|
388
|
+
function userContext(description) {
|
|
213
389
|
return {
|
|
214
|
-
|
|
215
|
-
|
|
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
|
-
|
|
222
|
-
|
|
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
|
-
|
|
237
|
-
|
|
238
|
-
|
|
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
|
|
273
|
-
|
|
274
|
-
|
|
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
|
-
|
|
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
|
-
|
|
429
|
+
userContext,
|
|
413
430
|
workflow
|
|
414
431
|
};
|
|
415
432
|
//# sourceMappingURL=instructions.js.map
|