@duckcodeailabs/dql-agent 1.4.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/LICENSE +123 -0
- package/dist/answer-loop.d.ts +74 -0
- package/dist/answer-loop.d.ts.map +1 -0
- package/dist/answer-loop.js +170 -0
- package/dist/answer-loop.js.map +1 -0
- package/dist/index.d.ts +51 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +87 -0
- package/dist/index.js.map +1 -0
- package/dist/kg/build.d.ts +17 -0
- package/dist/kg/build.d.ts.map +1 -0
- package/dist/kg/build.js +126 -0
- package/dist/kg/build.js.map +1 -0
- package/dist/kg/sqlite-fts.d.ts +39 -0
- package/dist/kg/sqlite-fts.d.ts.map +1 -0
- package/dist/kg/sqlite-fts.js +257 -0
- package/dist/kg/sqlite-fts.js.map +1 -0
- package/dist/kg/types.d.ts +64 -0
- package/dist/kg/types.d.ts.map +1 -0
- package/dist/kg/types.js +10 -0
- package/dist/kg/types.js.map +1 -0
- package/dist/providers/claude.d.ts +21 -0
- package/dist/providers/claude.d.ts.map +1 -0
- package/dist/providers/claude.js +56 -0
- package/dist/providers/claude.js.map +1 -0
- package/dist/providers/gemini.d.ts +18 -0
- package/dist/providers/gemini.d.ts.map +1 -0
- package/dist/providers/gemini.js +60 -0
- package/dist/providers/gemini.js.map +1 -0
- package/dist/providers/index.d.ts +24 -0
- package/dist/providers/index.d.ts.map +1 -0
- package/dist/providers/index.js +43 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/providers/ollama.d.ts +21 -0
- package/dist/providers/ollama.d.ts.map +1 -0
- package/dist/providers/ollama.js +49 -0
- package/dist/providers/ollama.js.map +1 -0
- package/dist/providers/openai.d.ts +19 -0
- package/dist/providers/openai.d.ts.map +1 -0
- package/dist/providers/openai.js +44 -0
- package/dist/providers/openai.js.map +1 -0
- package/dist/providers/types.d.ts +39 -0
- package/dist/providers/types.d.ts.map +1 -0
- package/dist/providers/types.js +15 -0
- package/dist/providers/types.js.map +1 -0
- package/dist/skills/loader.d.ts +49 -0
- package/dist/skills/loader.d.ts.map +1 -0
- package/dist/skills/loader.js +177 -0
- package/dist/skills/loader.js.map +1 -0
- package/package.json +46 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction, and
|
|
10
|
+
distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by the copyright
|
|
13
|
+
owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all other entities
|
|
16
|
+
that control, are controlled by, or are under common control with that entity.
|
|
17
|
+
For the purposes of this definition, "control" means (i) the power, direct or
|
|
18
|
+
indirect, to cause the direction or management of such entity, whether by
|
|
19
|
+
contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
20
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
21
|
+
|
|
22
|
+
"You" (or "Your") shall mean an individual or Legal Entity exercising
|
|
23
|
+
permissions granted by this License.
|
|
24
|
+
|
|
25
|
+
"Source" form shall mean the preferred form for making modifications, including
|
|
26
|
+
but not limited to software source code, documentation source, and configuration
|
|
27
|
+
files.
|
|
28
|
+
|
|
29
|
+
"Object" form shall mean any form resulting from mechanical transformation or
|
|
30
|
+
translation of a Source form, including but not limited to compiled object code,
|
|
31
|
+
generated documentation, and conversions to other media types.
|
|
32
|
+
|
|
33
|
+
"Work" shall mean the work of authorship, whether in Source or Object form, made
|
|
34
|
+
available under the License, as indicated by a copyright notice that is included
|
|
35
|
+
in or attached to the work.
|
|
36
|
+
|
|
37
|
+
"Derivative Works" shall mean any work, whether in Source or Object form, that
|
|
38
|
+
is based on (or derived from) the Work and for which the editorial revisions,
|
|
39
|
+
annotations, elaborations, or other modifications represent, as a whole, an
|
|
40
|
+
original work of authorship.
|
|
41
|
+
|
|
42
|
+
"Contribution" shall mean any work of authorship, including the original version
|
|
43
|
+
of the Work and any modifications or additions to that Work or Derivative Works
|
|
44
|
+
thereof, that is intentionally submitted to Licensor for inclusion in the Work
|
|
45
|
+
by the copyright owner or by an individual or Legal Entity authorized to submit
|
|
46
|
+
on behalf of the copyright owner.
|
|
47
|
+
|
|
48
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf
|
|
49
|
+
of whom a Contribution has been received by Licensor and subsequently
|
|
50
|
+
incorporated within the Work.
|
|
51
|
+
|
|
52
|
+
2. Grant of Copyright License.
|
|
53
|
+
|
|
54
|
+
Subject to the terms and conditions of this License, each Contributor hereby
|
|
55
|
+
grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
|
|
56
|
+
irrevocable copyright license to reproduce, prepare Derivative Works of,
|
|
57
|
+
publicly display, publicly perform, sublicense, and distribute the Work and such
|
|
58
|
+
Derivative Works in Source or Object form.
|
|
59
|
+
|
|
60
|
+
3. Grant of Patent License.
|
|
61
|
+
|
|
62
|
+
Subject to the terms and conditions of this License, each Contributor hereby
|
|
63
|
+
grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
|
|
64
|
+
irrevocable patent license to make, have made, use, offer to sell, sell, import,
|
|
65
|
+
and otherwise transfer the Work.
|
|
66
|
+
|
|
67
|
+
4. Redistribution.
|
|
68
|
+
|
|
69
|
+
You may reproduce and distribute copies of the Work or Derivative Works thereof
|
|
70
|
+
in any medium, with or without modifications, and in Source or Object form,
|
|
71
|
+
provided that You meet the following conditions:
|
|
72
|
+
|
|
73
|
+
(a) You must give any other recipients of the Work or Derivative Works a copy of
|
|
74
|
+
this License; and
|
|
75
|
+
|
|
76
|
+
(b) You must cause any modified files to carry prominent notices stating that
|
|
77
|
+
You changed the files; and
|
|
78
|
+
|
|
79
|
+
(c) You must retain, in the Source form of any Derivative Works that You
|
|
80
|
+
distribute, all copyright, patent, trademark, and attribution notices from the
|
|
81
|
+
Source form of the Work, excluding those notices that do not pertain to any part
|
|
82
|
+
of the Derivative Works; and
|
|
83
|
+
|
|
84
|
+
(d) If the Work includes a "NOTICE" text file as part of its distribution, then
|
|
85
|
+
any Derivative Works that You distribute must include a readable copy of the
|
|
86
|
+
attribution notices contained within such NOTICE file, excluding those notices
|
|
87
|
+
that do not pertain to any part of the Derivative Works.
|
|
88
|
+
|
|
89
|
+
5. Submission of Contributions.
|
|
90
|
+
|
|
91
|
+
Unless You explicitly state otherwise, any Contribution intentionally submitted
|
|
92
|
+
for inclusion in the Work by You to the Licensor shall be under the terms and
|
|
93
|
+
conditions of this License, without any additional terms or conditions.
|
|
94
|
+
|
|
95
|
+
6. Trademarks.
|
|
96
|
+
|
|
97
|
+
This License does not grant permission to use the trade names, trademarks,
|
|
98
|
+
service marks, or product names of the Licensor, except as required for
|
|
99
|
+
reasonable and customary use in describing the origin of the Work.
|
|
100
|
+
|
|
101
|
+
7. Disclaimer of Warranty.
|
|
102
|
+
|
|
103
|
+
Unless required by applicable law or agreed to in writing, Licensor provides the
|
|
104
|
+
Work on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
|
|
105
|
+
express or implied, including, without limitation, any warranties or conditions
|
|
106
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR
|
|
107
|
+
PURPOSE.
|
|
108
|
+
|
|
109
|
+
8. Limitation of Liability.
|
|
110
|
+
|
|
111
|
+
In no event and under no legal theory, whether in tort (including negligence),
|
|
112
|
+
contract, or otherwise, unless required by applicable law, shall any Contributor
|
|
113
|
+
be liable to You for damages, including any direct, indirect, special,
|
|
114
|
+
incidental, or consequential damages of any character arising as a result of this
|
|
115
|
+
License or out of the use or inability to use the Work.
|
|
116
|
+
|
|
117
|
+
9. Accepting Warranty or Additional Liability.
|
|
118
|
+
|
|
119
|
+
While redistributing the Work or Derivative Works thereof, You may choose to
|
|
120
|
+
offer, and charge a fee for, acceptance of support, warranty, indemnity, or
|
|
121
|
+
other liability obligations and/or rights consistent with this License.
|
|
122
|
+
|
|
123
|
+
END OF TERMS AND CONDITIONS
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Block-first answer loop.
|
|
3
|
+
*
|
|
4
|
+
* Stages:
|
|
5
|
+
* 1) FTS5 search the KG for blocks matching the question. If a strong
|
|
6
|
+
* `block` hit exists, return it as a Certified answer (use the block's
|
|
7
|
+
* SQL through `query_via_block`-equivalent semantics — caller runs it).
|
|
8
|
+
* 2) Otherwise, gather context (matching metrics, dimensions, dbt models,
|
|
9
|
+
* dashboards, Skills) and ask the LLM to propose SQL. Mark the answer
|
|
10
|
+
* Uncertified.
|
|
11
|
+
* 3) Always cite block ids/git SHAs.
|
|
12
|
+
*
|
|
13
|
+
* The loop is *deterministic* — provider invocation is the only stochastic
|
|
14
|
+
* step. Tests can mock the provider with a canned response and exercise the
|
|
15
|
+
* full pipeline.
|
|
16
|
+
*/
|
|
17
|
+
import type { KGStore } from './kg/sqlite-fts.js';
|
|
18
|
+
import type { KGNode, KGSearchHit } from './kg/types.js';
|
|
19
|
+
import type { AgentProvider } from './providers/types.js';
|
|
20
|
+
import type { Skill } from './skills/loader.js';
|
|
21
|
+
export type AnswerKind = 'certified' | 'uncertified' | 'no_answer';
|
|
22
|
+
export interface AgentCitation {
|
|
23
|
+
nodeId: string;
|
|
24
|
+
kind: KGNode['kind'];
|
|
25
|
+
name: string;
|
|
26
|
+
/** Frozen-in-time SHA at the moment of indexing. */
|
|
27
|
+
gitSha?: string;
|
|
28
|
+
}
|
|
29
|
+
export interface AgentAnswer {
|
|
30
|
+
kind: AnswerKind;
|
|
31
|
+
/** Final answer text (NL summary). */
|
|
32
|
+
text: string;
|
|
33
|
+
/** Certified path: the matched block — caller runs its SQL. */
|
|
34
|
+
block?: KGNode;
|
|
35
|
+
/** Uncertified path: the LLM-proposed SQL the analyst should review. */
|
|
36
|
+
proposedSql?: string;
|
|
37
|
+
/** Suggested viz type for the proposed SQL (line/bar/single_value/...). */
|
|
38
|
+
suggestedViz?: string;
|
|
39
|
+
citations: AgentCitation[];
|
|
40
|
+
/** Provider name used (for telemetry / UI badge). */
|
|
41
|
+
providerUsed?: string;
|
|
42
|
+
/** Top KG hits the loop considered, useful for the UI's "we considered" panel. */
|
|
43
|
+
considered: KGSearchHit[];
|
|
44
|
+
}
|
|
45
|
+
export interface AnswerLoopInput {
|
|
46
|
+
question: string;
|
|
47
|
+
/** Active user — used for Skills filtering and the "asked by" record. */
|
|
48
|
+
userId?: string;
|
|
49
|
+
/** Domain to scope the search. Optional. */
|
|
50
|
+
domain?: string;
|
|
51
|
+
/** Caller-supplied provider; the answer-loop never picks one itself. */
|
|
52
|
+
provider: AgentProvider;
|
|
53
|
+
/** Live KG store. */
|
|
54
|
+
kg: KGStore;
|
|
55
|
+
/** Project + user-level Skills. */
|
|
56
|
+
skills?: Skill[];
|
|
57
|
+
/** Hints to prefer specific blocks first (vocabulary mappings from Skills). */
|
|
58
|
+
blockHints?: string[];
|
|
59
|
+
/** Optional AbortSignal forwarded to the provider. */
|
|
60
|
+
signal?: AbortSignal;
|
|
61
|
+
}
|
|
62
|
+
export declare function answer(input: AnswerLoopInput): Promise<AgentAnswer>;
|
|
63
|
+
interface ParsedProposal {
|
|
64
|
+
text: string;
|
|
65
|
+
sql?: string;
|
|
66
|
+
viz?: string;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Public for tests. Pulls the first ```sql block and an optional Viz: line
|
|
70
|
+
* out of an LLM response.
|
|
71
|
+
*/
|
|
72
|
+
export declare function parseProposal(raw: string): ParsedProposal;
|
|
73
|
+
export {};
|
|
74
|
+
//# sourceMappingURL=answer-loop.d.ts.map
|
|
@@ -0,0 +1 @@
|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AAE,OAAO,CAAC;IACZ,mCAAmC;IACnC,MAAM,CAAC,EAAE,KAAK,EAAE,CAAC;IACjB,+EAA+E;IAC/E,UAAU,CAAC,EAAE,MAAM,EAAE,CAAC;IACtB,sDAAsD;IACtD,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB;AAKD,wBAAsB,MAAM,CAAC,KAAK,EAAE,eAAe,GAAG,OAAO,CAAC,WAAW,CAAC,CAqFzE;AA4BD,UAAU,cAAc;IACtB,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,cAAc,CAWzD"}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Block-first answer loop.
|
|
3
|
+
*
|
|
4
|
+
* Stages:
|
|
5
|
+
* 1) FTS5 search the KG for blocks matching the question. If a strong
|
|
6
|
+
* `block` hit exists, return it as a Certified answer (use the block's
|
|
7
|
+
* SQL through `query_via_block`-equivalent semantics — caller runs it).
|
|
8
|
+
* 2) Otherwise, gather context (matching metrics, dimensions, dbt models,
|
|
9
|
+
* dashboards, Skills) and ask the LLM to propose SQL. Mark the answer
|
|
10
|
+
* Uncertified.
|
|
11
|
+
* 3) Always cite block ids/git SHAs.
|
|
12
|
+
*
|
|
13
|
+
* The loop is *deterministic* — provider invocation is the only stochastic
|
|
14
|
+
* step. Tests can mock the provider with a canned response and exercise the
|
|
15
|
+
* full pipeline.
|
|
16
|
+
*/
|
|
17
|
+
import { buildSkillsPrompt } from './skills/loader.js';
|
|
18
|
+
const CERTIFIED_HIT_THRESHOLD = 0.18;
|
|
19
|
+
const HARD_NEGATIVE_RATIO = 0.5;
|
|
20
|
+
export async function answer(input) {
|
|
21
|
+
const { question, userId, domain, provider, kg, skills = [], blockHints = [] } = input;
|
|
22
|
+
const considered = kg.search({
|
|
23
|
+
query: question,
|
|
24
|
+
domain,
|
|
25
|
+
limit: 10,
|
|
26
|
+
});
|
|
27
|
+
// Stage 1: certified-block match
|
|
28
|
+
const blockHits = considered.filter((h) => h.node.kind === 'block');
|
|
29
|
+
const blockHit = pickCertifiedBlock(blockHits, blockHints, kg);
|
|
30
|
+
if (blockHit) {
|
|
31
|
+
return {
|
|
32
|
+
kind: 'certified',
|
|
33
|
+
text: composeCertifiedAnswer(blockHit.node, question),
|
|
34
|
+
block: blockHit.node,
|
|
35
|
+
citations: [
|
|
36
|
+
{
|
|
37
|
+
nodeId: blockHit.node.nodeId,
|
|
38
|
+
kind: blockHit.node.kind,
|
|
39
|
+
name: blockHit.node.name,
|
|
40
|
+
gitSha: blockHit.node.gitSha,
|
|
41
|
+
},
|
|
42
|
+
],
|
|
43
|
+
considered,
|
|
44
|
+
providerUsed: provider.name,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
// Stage 2: uncertified — gather context, ask the LLM
|
|
48
|
+
const contextNodes = considered.slice(0, 6).map((h) => h.node);
|
|
49
|
+
const contextBlocks = contextNodes.filter((n) => n.kind === 'block');
|
|
50
|
+
const contextOther = contextNodes.filter((n) => n.kind !== 'block');
|
|
51
|
+
const messages = [
|
|
52
|
+
{ role: 'system', content: SYSTEM_PROMPT },
|
|
53
|
+
];
|
|
54
|
+
const skillsPrompt = buildSkillsPrompt(skills, userId ?? null);
|
|
55
|
+
if (skillsPrompt)
|
|
56
|
+
messages.push({ role: 'system', content: skillsPrompt });
|
|
57
|
+
messages.push({
|
|
58
|
+
role: 'system',
|
|
59
|
+
content: renderContextPrompt(contextBlocks, contextOther),
|
|
60
|
+
});
|
|
61
|
+
messages.push({ role: 'user', content: question });
|
|
62
|
+
let proposed = '';
|
|
63
|
+
try {
|
|
64
|
+
proposed = await provider.generate(messages, { signal: input.signal });
|
|
65
|
+
}
|
|
66
|
+
catch (err) {
|
|
67
|
+
return {
|
|
68
|
+
kind: 'no_answer',
|
|
69
|
+
text: `Provider error: ${err.message}`,
|
|
70
|
+
citations: [],
|
|
71
|
+
considered,
|
|
72
|
+
providerUsed: provider.name,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
const parsed = parseProposal(proposed);
|
|
76
|
+
if (!parsed.sql) {
|
|
77
|
+
return {
|
|
78
|
+
kind: 'no_answer',
|
|
79
|
+
text: parsed.text || 'No answer (the model declined to propose SQL).',
|
|
80
|
+
citations: [],
|
|
81
|
+
considered,
|
|
82
|
+
providerUsed: provider.name,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
return {
|
|
86
|
+
kind: 'uncertified',
|
|
87
|
+
text: parsed.text,
|
|
88
|
+
proposedSql: parsed.sql,
|
|
89
|
+
suggestedViz: parsed.viz ?? 'table',
|
|
90
|
+
citations: contextNodes.slice(0, 4).map((n) => ({
|
|
91
|
+
nodeId: n.nodeId,
|
|
92
|
+
kind: n.kind,
|
|
93
|
+
name: n.name,
|
|
94
|
+
gitSha: n.gitSha,
|
|
95
|
+
})),
|
|
96
|
+
considered,
|
|
97
|
+
providerUsed: provider.name,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
const SYSTEM_PROMPT = `You are the DQL Analytics Agent.
|
|
101
|
+
|
|
102
|
+
Rules:
|
|
103
|
+
1. ALWAYS prefer existing certified DQL blocks. The analytics surface marks every
|
|
104
|
+
answer as Certified or AI-generated/Uncertified.
|
|
105
|
+
2. If you must generate SQL, return it inside a single \`\`\`sql code block.
|
|
106
|
+
3. Provide a one-paragraph natural-language summary BEFORE the SQL block.
|
|
107
|
+
4. Suggest a visualization type from this list, on a line starting with "Viz:":
|
|
108
|
+
line, bar, area, pie, single_value, table, pivot, kpi.
|
|
109
|
+
5. NEVER fabricate column names that are not present in the supplied schema context.
|
|
110
|
+
6. If the schema is insufficient to answer, say so explicitly and ask a clarifying question.`;
|
|
111
|
+
function renderContextPrompt(blocks, others) {
|
|
112
|
+
const blockSection = blocks.length > 0
|
|
113
|
+
? `## Certified blocks the user already has\n\n${blocks
|
|
114
|
+
.map((b) => `- \`${b.nodeId}\` (${b.domain ?? 'unscoped'}): ${b.description ?? b.llmContext ?? '(no description)'}`)
|
|
115
|
+
.join('\n')}`
|
|
116
|
+
: '## Certified blocks: (none matched)';
|
|
117
|
+
const otherSection = others.length > 0
|
|
118
|
+
? `\n\n## Related semantic + warehouse context\n\n${others
|
|
119
|
+
.map((n) => `- ${n.kind} \`${n.name}\`${n.domain ? ` (domain: ${n.domain})` : ''}${n.description ? ` — ${n.description}` : ''}`)
|
|
120
|
+
.join('\n')}`
|
|
121
|
+
: '';
|
|
122
|
+
return `${blockSection}${otherSection}`;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Public for tests. Pulls the first ```sql block and an optional Viz: line
|
|
126
|
+
* out of an LLM response.
|
|
127
|
+
*/
|
|
128
|
+
export function parseProposal(raw) {
|
|
129
|
+
const sqlMatch = raw.match(/```sql\s*([\s\S]*?)```/i);
|
|
130
|
+
const sql = sqlMatch ? sqlMatch[1].trim() : undefined;
|
|
131
|
+
const vizMatch = raw.match(/^Viz:\s*([a-z_]+)/im);
|
|
132
|
+
const viz = vizMatch ? vizMatch[1].trim().toLowerCase() : undefined;
|
|
133
|
+
// Strip the SQL block + Viz line from the prose to keep the summary clean.
|
|
134
|
+
const text = raw
|
|
135
|
+
.replace(/```sql[\s\S]*?```/gi, '')
|
|
136
|
+
.replace(/^Viz:.*$/gim, '')
|
|
137
|
+
.trim();
|
|
138
|
+
return { text, sql, viz };
|
|
139
|
+
}
|
|
140
|
+
function pickCertifiedBlock(blockHits, blockHints, kg) {
|
|
141
|
+
// Hint match wins immediately: the active Skill's vocabulary points the
|
|
142
|
+
// user at a specific block. We still validate it's certified.
|
|
143
|
+
for (const hint of blockHints) {
|
|
144
|
+
const node = kg.getNode(`block:${hint}`);
|
|
145
|
+
if (node && node.status === 'certified') {
|
|
146
|
+
return { node, score: 1, snippet: undefined };
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
// Otherwise: top FTS5 hit must be certified, exceed the score threshold,
|
|
150
|
+
// and not have a hard negative ratio in feedback.
|
|
151
|
+
for (const hit of blockHits) {
|
|
152
|
+
if (hit.score < CERTIFIED_HIT_THRESHOLD)
|
|
153
|
+
break;
|
|
154
|
+
if (hit.node.status !== 'certified')
|
|
155
|
+
continue;
|
|
156
|
+
const fb = kg.blockFeedbackScore(hit.node.nodeId);
|
|
157
|
+
const total = fb.up + fb.down;
|
|
158
|
+
if (total > 0 && fb.down / total > HARD_NEGATIVE_RATIO)
|
|
159
|
+
continue;
|
|
160
|
+
return hit;
|
|
161
|
+
}
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
function composeCertifiedAnswer(block, question) {
|
|
165
|
+
const desc = block.description ?? block.llmContext ?? '';
|
|
166
|
+
const tag = block.gitSha ? ` · ${block.gitSha.slice(0, 8)}` : '';
|
|
167
|
+
return `Answered by certified block **${block.name}**${tag}.\n\n${desc || 'Run the block to see the result.'}`
|
|
168
|
+
+ `\n\n_Question:_ ${question}`;
|
|
169
|
+
}
|
|
170
|
+
//# sourceMappingURL=answer-loop.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"answer-loop.js","sourceRoot":"","sources":["../src/answer-loop.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAMH,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AA+CvD,MAAM,uBAAuB,GAAG,IAAI,CAAC;AACrC,MAAM,mBAAmB,GAAG,GAAG,CAAC;AAEhC,MAAM,CAAC,KAAK,UAAU,MAAM,CAAC,KAAsB;IACjD,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,GAAG,EAAE,EAAE,UAAU,GAAG,EAAE,EAAE,GAAG,KAAK,CAAC;IAEvF,MAAM,UAAU,GAAG,EAAE,CAAC,MAAM,CAAC;QAC3B,KAAK,EAAE,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AAyE;IACzE,kDAAkD;IAClD,KAAK,MAAM,GAAG,IAAI,SAAS,EAAE,CAAC;QAC5B,IAAI,GAAG,CAAC,KAAK,GAAG,uBAAuB;YAAE,MAAM;QAC/C,IAAI,GAAG,CAAC,IAAI,CAAC,MAAM,KAAK,WAAW;YAAE,SAAS;QAC9C,MAAM,EAAE,GAAG,EAAE,CAAC,kBAAkB,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QAClD,MAAM,KAAK,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC;QAC9B,IAAI,KAAK,GAAG,CAAC,IAAI,EAAE,CAAC,IAAI,GAAG,KAAK,GAAG,mBAAmB;YAAE,SAAS;QACjE,OAAO,GAAG,CAAC;IACb,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED,SAAS,sBAAsB,CAAC,KAAa,EAAE,QAAgB;IAC7D,MAAM,IAAI,GAAG,KAAK,CAAC,WAAW,IAAI,KAAK,CAAC,UAAU,IAAI,EAAE,CAAC;IACzD,MAAM,GAAG,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;IACjE,OAAO,iCAAiC,KAAK,CAAC,IAAI,KAAK,GAAG,QAAQ,IAAI,IAAI,kCAAkC,EAAE;UAC1G,mBAAmB,QAAQ,EAAE,CAAC;AACpC,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public surface for `@duckcodeailabs/dql-agent`.
|
|
3
|
+
*
|
|
4
|
+
* Consumers (CLI, MCP server, notebook UI) typically import:
|
|
5
|
+
* - `reindexProject(root)` to rebuild the KG
|
|
6
|
+
* - `KGStore` for direct queries
|
|
7
|
+
* - `answer({ ... })` for the block-first answer loop
|
|
8
|
+
* - the providers (Claude/OpenAI/Gemini/Ollama)
|
|
9
|
+
*/
|
|
10
|
+
import type { DQLManifest } from '@duckcodeailabs/dql-core';
|
|
11
|
+
export { KGStore } from './kg/sqlite-fts.js';
|
|
12
|
+
export type { KGNode, KGEdge, KGNodeKind, KGSearchHit, KGFeedbackRow, KGSearchOptions } from './kg/types.js';
|
|
13
|
+
export { buildKGFromManifest } from './kg/build.js';
|
|
14
|
+
export { loadSkills, parseSkill, buildSkillsPrompt } from './skills/loader.js';
|
|
15
|
+
export type { Skill, SkillLoadResult } from './skills/loader.js';
|
|
16
|
+
export { answer, parseProposal } from './answer-loop.js';
|
|
17
|
+
export type { AgentAnswer, AgentCitation, AnswerKind, AnswerLoopInput } from './answer-loop.js';
|
|
18
|
+
export { ClaudeProvider, OpenAIProvider, GeminiProvider, OllamaProvider, pickProvider, buildProvider, } from './providers/index.js';
|
|
19
|
+
export type { AgentProvider, AgentMessage, ProviderName, ProviderRunOptions } from './providers/index.js';
|
|
20
|
+
/**
|
|
21
|
+
* Default location for the agent's SQLite KG file.
|
|
22
|
+
* Mirrors the manifest cache layout under `.dql/cache/`.
|
|
23
|
+
*/
|
|
24
|
+
export declare function defaultKgPath(projectRoot: string): string;
|
|
25
|
+
export interface ReindexOptions {
|
|
26
|
+
manifest?: DQLManifest;
|
|
27
|
+
/** Path to the KG sqlite file. Defaults to `.dql/cache/agent-kg.sqlite`. */
|
|
28
|
+
kgPath?: string;
|
|
29
|
+
/** Set to false to skip re-loading Skills. */
|
|
30
|
+
loadSkills?: boolean;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Rebuild the KG from the project's manifest. Safe to call on every save —
|
|
34
|
+
* incremental indexing can land later; the wholesale rebuild is fast on the
|
|
35
|
+
* scale dql projects realistically reach (thousands of nodes).
|
|
36
|
+
*/
|
|
37
|
+
export declare function reindexProject(projectRoot: string, opts?: ReindexOptions): Promise<{
|
|
38
|
+
nodes: number;
|
|
39
|
+
edges: number;
|
|
40
|
+
skills: number;
|
|
41
|
+
}>;
|
|
42
|
+
/**
|
|
43
|
+
* Promotion suggester — surface uncertified answers that have accumulated
|
|
44
|
+
* positive feedback so an analyst can certify them as proper blocks.
|
|
45
|
+
*/
|
|
46
|
+
export declare function getPromotionCandidates(projectRoot: string, minUps?: number): Array<{
|
|
47
|
+
blockId: string;
|
|
48
|
+
question: string;
|
|
49
|
+
ups: number;
|
|
50
|
+
}>;
|
|
51
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAIH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAO5D,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAC7C,YAAY,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,WAAW,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAC7G,OAAO,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AACpD,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAC/E,YAAY,EAAE,KAAK,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AACjE,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACzD,YAAY,EAAE,WAAW,EAAE,aAAa,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAChG,OAAO,EACL,cAAc,EAAE,cAAc,EAAE,cAAc,EAAE,cAAc,EAC9D,YAAY,EAAE,aAAa,GAC5B,MAAM,sBAAsB,CAAC;AAC9B,YAAY,EAAE,aAAa,EAAE,YAAY,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAE1G;;;GAGG;AACH,wBAAgB,aAAa,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,CAEzD;AAED,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,EAAE,WAAW,CAAC;IACvB,4EAA4E;IAC5E,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,8CAA8C;IAC9C,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB;AAED;;;;GAIG;AACH,wBAAsB,cAAc,CAClC,WAAW,EAAE,MAAM,EACnB,IAAI,GAAE,cAAmB,GACxB,OAAO,CAAC;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC,CA4B3D;AAED;;;GAGG;AACH,wBAAgB,sBAAsB,CAAC,WAAW,EAAE,MAAM,EAAE,MAAM,SAAI,GAAG,KAAK,CAAC;IAC7E,OAAO,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAC;CAChD,CAAC,CAOD"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public surface for `@duckcodeailabs/dql-agent`.
|
|
3
|
+
*
|
|
4
|
+
* Consumers (CLI, MCP server, notebook UI) typically import:
|
|
5
|
+
* - `reindexProject(root)` to rebuild the KG
|
|
6
|
+
* - `KGStore` for direct queries
|
|
7
|
+
* - `answer({ ... })` for the block-first answer loop
|
|
8
|
+
* - the providers (Claude/OpenAI/Gemini/Ollama)
|
|
9
|
+
*/
|
|
10
|
+
import { join } from 'node:path';
|
|
11
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
12
|
+
import { buildManifest } from '@duckcodeailabs/dql-core';
|
|
13
|
+
import { KGStore } from './kg/sqlite-fts.js';
|
|
14
|
+
import { buildKGFromManifest } from './kg/build.js';
|
|
15
|
+
import { loadSkills } from './skills/loader.js';
|
|
16
|
+
export { KGStore } from './kg/sqlite-fts.js';
|
|
17
|
+
export { buildKGFromManifest } from './kg/build.js';
|
|
18
|
+
export { loadSkills, parseSkill, buildSkillsPrompt } from './skills/loader.js';
|
|
19
|
+
export { answer, parseProposal } from './answer-loop.js';
|
|
20
|
+
export { ClaudeProvider, OpenAIProvider, GeminiProvider, OllamaProvider, pickProvider, buildProvider, } from './providers/index.js';
|
|
21
|
+
/**
|
|
22
|
+
* Default location for the agent's SQLite KG file.
|
|
23
|
+
* Mirrors the manifest cache layout under `.dql/cache/`.
|
|
24
|
+
*/
|
|
25
|
+
export function defaultKgPath(projectRoot) {
|
|
26
|
+
return join(projectRoot, '.dql', 'cache', 'agent-kg.sqlite');
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Rebuild the KG from the project's manifest. Safe to call on every save —
|
|
30
|
+
* incremental indexing can land later; the wholesale rebuild is fast on the
|
|
31
|
+
* scale dql projects realistically reach (thousands of nodes).
|
|
32
|
+
*/
|
|
33
|
+
export async function reindexProject(projectRoot, opts = {}) {
|
|
34
|
+
const manifest = opts.manifest ?? loadManifest(projectRoot);
|
|
35
|
+
const { nodes, edges } = buildKGFromManifest(manifest);
|
|
36
|
+
// Skills become KG nodes too so the agent can retrieve them.
|
|
37
|
+
let skills = [];
|
|
38
|
+
if (opts.loadSkills !== false) {
|
|
39
|
+
const result = loadSkills(projectRoot);
|
|
40
|
+
skills = result.skills;
|
|
41
|
+
for (const s of skills) {
|
|
42
|
+
nodes.push({
|
|
43
|
+
nodeId: `skill:${s.id}`,
|
|
44
|
+
kind: 'skill',
|
|
45
|
+
name: s.id,
|
|
46
|
+
description: s.description,
|
|
47
|
+
llmContext: s.body,
|
|
48
|
+
sourcePath: s.sourcePath,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
const kg = new KGStore(opts.kgPath ?? defaultKgPath(projectRoot));
|
|
53
|
+
try {
|
|
54
|
+
kg.rebuild(nodes, edges);
|
|
55
|
+
}
|
|
56
|
+
finally {
|
|
57
|
+
kg.close();
|
|
58
|
+
}
|
|
59
|
+
return { nodes: nodes.length, edges: edges.length, skills: skills.length };
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Promotion suggester — surface uncertified answers that have accumulated
|
|
63
|
+
* positive feedback so an analyst can certify them as proper blocks.
|
|
64
|
+
*/
|
|
65
|
+
export function getPromotionCandidates(projectRoot, minUps = 5) {
|
|
66
|
+
const kg = new KGStore(defaultKgPath(projectRoot));
|
|
67
|
+
try {
|
|
68
|
+
return kg.promotionCandidates(minUps);
|
|
69
|
+
}
|
|
70
|
+
finally {
|
|
71
|
+
kg.close();
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function loadManifest(projectRoot) {
|
|
75
|
+
// Prefer the on-disk compiled manifest, fall back to a fresh build.
|
|
76
|
+
const compiled = join(projectRoot, 'dql-manifest.json');
|
|
77
|
+
if (existsSync(compiled)) {
|
|
78
|
+
try {
|
|
79
|
+
return JSON.parse(readFileSync(compiled, 'utf-8'));
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
// fall through
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return buildManifest({ projectRoot });
|
|
86
|
+
}
|
|
87
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAEnD,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAC7C,OAAO,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AACpD,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAGhD,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAE7C,OAAO,EAA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ql-manifest.json (blocks, dashboards, apps, metrics, dimensions, sources)
|
|
6
|
+
* - dbt manifest (already merged into the DQL manifest's dbtImport.dbtDag)
|
|
7
|
+
* - project Skills folder (loaded separately by the Skills loader)
|
|
8
|
+
*
|
|
9
|
+
* The output is intentionally flat — caller passes it to `KGStore.rebuild()`.
|
|
10
|
+
*/
|
|
11
|
+
import type { DQLManifest } from '@duckcodeailabs/dql-core';
|
|
12
|
+
import type { KGNode, KGEdge } from './types.js';
|
|
13
|
+
export declare function buildKGFromManifest(manifest: DQLManifest): {
|
|
14
|
+
nodes: KGNode[];
|
|
15
|
+
edges: KGEdge[];
|
|
16
|
+
};
|
|
17
|
+
//# sourceMappingURL=build.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"build.d.ts","sourceRoot":"","sources":["../../src/kg/build.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAC5D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,EAAc,MAAM,YAAY,CAAC;AAE7D,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,WAAW,GAAG;IAC1D,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,KAAK,EAAE,MAAM,EAAE,CAAC;CACjB,CAkHA"}
|