@kweaver-ai/kweaver-sdk 0.8.1 → 0.8.3

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 (214) hide show
  1. package/README.md +40 -52
  2. package/README.zh.md +41 -46
  3. package/dist/agent-providers/index.d.ts +7 -0
  4. package/dist/agent-providers/index.js +5 -0
  5. package/dist/agent-providers/prompt-template.d.ts +62 -0
  6. package/dist/agent-providers/prompt-template.js +105 -0
  7. package/dist/agent-providers/prompts/rubric-judge-v1.prompt.md +51 -0
  8. package/dist/agent-providers/prompts/within-trace-synthesizer-v1.prompt.md +60 -0
  9. package/dist/agent-providers/providers/claude-code-subprocess.d.ts +74 -0
  10. package/dist/agent-providers/providers/claude-code-subprocess.js +259 -0
  11. package/dist/agent-providers/providers/stub.d.ts +47 -0
  12. package/dist/agent-providers/providers/stub.js +77 -0
  13. package/dist/agent-providers/registry.d.ts +45 -0
  14. package/dist/agent-providers/registry.js +77 -0
  15. package/dist/agent-providers/types.d.ts +91 -0
  16. package/dist/agent-providers/types.js +25 -0
  17. package/dist/api/agent-chat.js +8 -6
  18. package/dist/api/context-loader.d.ts +1 -0
  19. package/dist/api/resources.d.ts +94 -0
  20. package/dist/api/resources.js +166 -0
  21. package/dist/api/semantic-search.d.ts +5 -0
  22. package/dist/api/semantic-search.js +5 -0
  23. package/dist/api/skills.d.ts +75 -2
  24. package/dist/api/skills.js +108 -12
  25. package/dist/api/trace.d.ts +5 -0
  26. package/dist/api/trace.js +4 -0
  27. package/dist/cli.js +109 -15
  28. package/dist/client.d.ts +3 -3
  29. package/dist/client.js +5 -5
  30. package/dist/commands/agent/mode.d.ts +6 -0
  31. package/dist/commands/agent/mode.js +75 -0
  32. package/dist/commands/agent-members.js +27 -11
  33. package/dist/commands/agent.js +469 -286
  34. package/dist/commands/auth.js +184 -71
  35. package/dist/commands/bkn-metric.js +37 -16
  36. package/dist/commands/bkn-ops.js +164 -86
  37. package/dist/commands/bkn-query.js +99 -31
  38. package/dist/commands/bkn-schema.d.ts +3 -3
  39. package/dist/commands/bkn-schema.js +127 -86
  40. package/dist/commands/bkn.js +153 -114
  41. package/dist/commands/call.js +23 -13
  42. package/dist/commands/config.js +22 -12
  43. package/dist/commands/context-loader.js +625 -49
  44. package/dist/commands/dataflow.js +14 -6
  45. package/dist/commands/ds.js +52 -30
  46. package/dist/commands/explore.js +18 -15
  47. package/dist/commands/model.js +53 -42
  48. package/dist/commands/resource.d.ts +1 -0
  49. package/dist/commands/{dataview.js → resource.js} +62 -84
  50. package/dist/commands/skill.d.ts +21 -1
  51. package/dist/commands/skill.js +567 -43
  52. package/dist/commands/token.js +11 -0
  53. package/dist/commands/tool.js +46 -29
  54. package/dist/commands/toolbox.js +31 -15
  55. package/dist/commands/trace.d.ts +26 -1
  56. package/dist/commands/trace.js +515 -15
  57. package/dist/commands/vega.js +466 -250
  58. package/dist/help/format.d.ts +65 -0
  59. package/dist/help/format.js +141 -0
  60. package/dist/index.d.ts +5 -5
  61. package/dist/index.js +3 -3
  62. package/dist/resources/bkn.d.ts +5 -0
  63. package/dist/resources/bkn.js +5 -0
  64. package/dist/resources/{dataviews.d.ts → resources.d.ts} +10 -11
  65. package/dist/resources/{dataviews.js → resources.js} +12 -13
  66. package/dist/resources/skills.d.ts +17 -1
  67. package/dist/resources/skills.js +32 -1
  68. package/dist/trace-ai/diagnose/agent-binding.d.ts +67 -0
  69. package/dist/trace-ai/diagnose/agent-binding.js +257 -0
  70. package/dist/trace-ai/diagnose/builtin-rules/tool-retry-intent-mismatch.yaml +68 -0
  71. package/dist/trace-ai/diagnose/index.d.ts +32 -0
  72. package/dist/trace-ai/diagnose/index.js +246 -0
  73. package/dist/trace-ai/diagnose/output-schema-converter.d.ts +24 -0
  74. package/dist/trace-ai/diagnose/output-schema-converter.js +81 -0
  75. package/dist/trace-ai/diagnose/query-extractor.d.ts +14 -0
  76. package/dist/trace-ai/diagnose/query-extractor.js +45 -0
  77. package/dist/trace-ai/diagnose/report-assembler.d.ts +31 -0
  78. package/dist/{trace-core → trace-ai}/diagnose/report-assembler.js +19 -9
  79. package/dist/trace-ai/diagnose/report-markdown.d.ts +18 -0
  80. package/dist/trace-ai/diagnose/report-markdown.js +192 -0
  81. package/dist/{trace-core → trace-ai}/diagnose/rule-loader.js +42 -8
  82. package/dist/{trace-core → trace-ai}/diagnose/schemas.d.ts +77 -2
  83. package/dist/trace-ai/diagnose/schemas.js +154 -0
  84. package/dist/trace-ai/diagnose/signal-probe.d.ts +17 -0
  85. package/dist/trace-ai/diagnose/signal-probe.js +39 -0
  86. package/dist/trace-ai/diagnose/synthesizer-agent.d.ts +40 -0
  87. package/dist/trace-ai/diagnose/synthesizer-agent.js +158 -0
  88. package/dist/{trace-core → trace-ai}/diagnose/trace-shaper.js +1 -0
  89. package/dist/{trace-core → trace-ai}/diagnose/types.d.ts +55 -6
  90. package/dist/trace-ai/eval-set/assertion-evaluator.d.ts +29 -0
  91. package/dist/trace-ai/eval-set/assertion-evaluator.js +100 -0
  92. package/dist/trace-ai/eval-set/builder.d.ts +36 -0
  93. package/dist/trace-ai/eval-set/builder.js +126 -0
  94. package/dist/trace-ai/eval-set/index.d.ts +15 -0
  95. package/dist/trace-ai/eval-set/index.js +10 -0
  96. package/dist/trace-ai/eval-set/output-writer.d.ts +27 -0
  97. package/dist/trace-ai/eval-set/output-writer.js +126 -0
  98. package/dist/trace-ai/eval-set/query-picker.d.ts +37 -0
  99. package/dist/trace-ai/eval-set/query-picker.js +147 -0
  100. package/dist/trace-ai/eval-set/redactor.d.ts +42 -0
  101. package/dist/trace-ai/eval-set/redactor.js +133 -0
  102. package/dist/trace-ai/eval-set/rubric-templates/answer-match-reference.prompt.md +19 -0
  103. package/dist/trace-ai/eval-set/schemas.d.ts +136 -0
  104. package/dist/trace-ai/eval-set/schemas.js +130 -0
  105. package/dist/trace-ai/eval-set/semantic-match-provider.d.ts +33 -0
  106. package/dist/trace-ai/eval-set/semantic-match-provider.js +51 -0
  107. package/dist/trace-ai/eval-set/test-runner.d.ts +34 -0
  108. package/dist/trace-ai/eval-set/test-runner.js +153 -0
  109. package/dist/trace-ai/eval-set/types.d.ts +46 -0
  110. package/dist/trace-ai/eval-set/types.js +8 -0
  111. package/dist/trace-ai/exp/bundle-writer.d.ts +10 -0
  112. package/dist/trace-ai/exp/bundle-writer.js +54 -0
  113. package/dist/trace-ai/exp/claude-binary.d.ts +5 -0
  114. package/dist/trace-ai/exp/claude-binary.js +30 -0
  115. package/dist/trace-ai/exp/coordinator.d.ts +45 -0
  116. package/dist/trace-ai/exp/coordinator.js +203 -0
  117. package/dist/trace-ai/exp/eval-runner.d.ts +14 -0
  118. package/dist/trace-ai/exp/eval-runner.js +47 -0
  119. package/dist/trace-ai/exp/exp-store/abort-signal.d.ts +3 -0
  120. package/dist/trace-ai/exp/exp-store/abort-signal.js +27 -0
  121. package/dist/trace-ai/exp/exp-store/candidate-lineage-yaml.d.ts +4 -0
  122. package/dist/trace-ai/exp/exp-store/candidate-lineage-yaml.js +37 -0
  123. package/dist/trace-ai/exp/exp-store/events-jsonl.d.ts +17 -0
  124. package/dist/trace-ai/exp/exp-store/events-jsonl.js +60 -0
  125. package/dist/trace-ai/exp/exp-store/exp-registry.d.ts +6 -0
  126. package/dist/trace-ai/exp/exp-store/exp-registry.js +41 -0
  127. package/dist/trace-ai/exp/exp-store/index.d.ts +46 -0
  128. package/dist/trace-ai/exp/exp-store/index.js +59 -0
  129. package/dist/trace-ai/exp/exp-store/lock.d.ts +3 -0
  130. package/dist/trace-ai/exp/exp-store/lock.js +73 -0
  131. package/dist/trace-ai/exp/exp-store/mission-md.d.ts +3 -0
  132. package/dist/trace-ai/exp/exp-store/mission-md.js +37 -0
  133. package/dist/trace-ai/exp/exp-store/readme-template.d.ts +5 -0
  134. package/dist/trace-ai/exp/exp-store/readme-template.js +25 -0
  135. package/dist/trace-ai/exp/exp-store/round-yaml.d.ts +3 -0
  136. package/dist/trace-ai/exp/exp-store/round-yaml.js +33 -0
  137. package/dist/trace-ai/exp/index.d.ts +8 -0
  138. package/dist/trace-ai/exp/index.js +238 -0
  139. package/dist/trace-ai/exp/info.d.ts +35 -0
  140. package/dist/trace-ai/exp/info.js +120 -0
  141. package/dist/trace-ai/exp/patch/agent-config.d.ts +1 -0
  142. package/dist/trace-ai/exp/patch/agent-config.js +26 -0
  143. package/dist/trace-ai/exp/patch/index.d.ts +2 -0
  144. package/dist/trace-ai/exp/patch/index.js +13 -0
  145. package/dist/trace-ai/exp/patch/skill.d.ts +1 -0
  146. package/dist/trace-ai/exp/patch/skill.js +24 -0
  147. package/dist/trace-ai/exp/providers/synthesizer-client.d.ts +14 -0
  148. package/dist/trace-ai/exp/providers/synthesizer-client.js +39 -0
  149. package/dist/trace-ai/exp/providers/triage-client.d.ts +19 -0
  150. package/dist/trace-ai/exp/providers/triage-client.js +51 -0
  151. package/dist/trace-ai/exp/schemas.d.ts +147 -0
  152. package/dist/trace-ai/exp/schemas.js +50 -0
  153. package/dist/trace-ai/exp/scoring.d.ts +2 -0
  154. package/dist/trace-ai/exp/scoring.js +46 -0
  155. package/dist/trace-ai/scan/aggregator.d.ts +20 -0
  156. package/dist/trace-ai/scan/aggregator.js +26 -0
  157. package/dist/trace-ai/scan/artifacts/paths.d.ts +12 -0
  158. package/dist/trace-ai/scan/artifacts/paths.js +18 -0
  159. package/dist/trace-ai/scan/artifacts/writer.d.ts +67 -0
  160. package/dist/trace-ai/scan/artifacts/writer.js +96 -0
  161. package/dist/trace-ai/scan/batched-rubric.d.ts +55 -0
  162. package/dist/trace-ai/scan/batched-rubric.js +159 -0
  163. package/dist/trace-ai/scan/cross-trace-synthesizer.d.ts +24 -0
  164. package/dist/trace-ai/scan/cross-trace-synthesizer.js +93 -0
  165. package/dist/trace-ai/scan/index.d.ts +31 -0
  166. package/dist/trace-ai/scan/index.js +390 -0
  167. package/dist/trace-ai/scan/prompts/builtin/cross-trace-synthesizer-v1.prompt.md +44 -0
  168. package/dist/trace-ai/scan/prompts/builtin/rubric-judge-batch-v1.prompt.md +44 -0
  169. package/dist/trace-ai/scan/runner.d.ts +25 -0
  170. package/dist/trace-ai/scan/runner.js +42 -0
  171. package/dist/trace-ai/scan/sampler.d.ts +18 -0
  172. package/dist/trace-ai/scan/sampler.js +81 -0
  173. package/dist/trace-ai/scan/scan-summary-markdown.d.ts +2 -0
  174. package/dist/trace-ai/scan/scan-summary-markdown.js +71 -0
  175. package/dist/trace-ai/scan/scan-summary-schema.d.ts +73 -0
  176. package/dist/trace-ai/scan/scan-summary-schema.js +61 -0
  177. package/dist/trace-ai/scan/single-agent-validator.d.ts +23 -0
  178. package/dist/trace-ai/scan/single-agent-validator.js +42 -0
  179. package/dist/trace-ai/scan/traces-list-parser.d.ts +15 -0
  180. package/dist/trace-ai/scan/traces-list-parser.js +46 -0
  181. package/package.json +2 -2
  182. package/dist/api/dataviews.d.ts +0 -117
  183. package/dist/api/dataviews.js +0 -265
  184. package/dist/commands/dataview.d.ts +0 -8
  185. package/dist/trace-core/diagnose/index.d.ts +0 -9
  186. package/dist/trace-core/diagnose/index.js +0 -104
  187. package/dist/trace-core/diagnose/report-assembler.d.ts +0 -12
  188. package/dist/trace-core/diagnose/schemas.js +0 -94
  189. package/dist/trace-core/diagnose/signal-probe.d.ts +0 -5
  190. package/dist/trace-core/diagnose/signal-probe.js +0 -21
  191. /package/dist/{trace-core → trace-ai}/diagnose/builtin-rules/excessive-tool-calls-per-turn.d.ts +0 -0
  192. /package/dist/{trace-core → trace-ai}/diagnose/builtin-rules/excessive-tool-calls-per-turn.js +0 -0
  193. /package/dist/{trace-core → trace-ai}/diagnose/builtin-rules/excessive-tool-calls-per-turn.yaml +0 -0
  194. /package/dist/{trace-core → trace-ai}/diagnose/builtin-rules/llm-response-truncated-no-continue.d.ts +0 -0
  195. /package/dist/{trace-core → trace-ai}/diagnose/builtin-rules/llm-response-truncated-no-continue.js +0 -0
  196. /package/dist/{trace-core → trace-ai}/diagnose/builtin-rules/llm-response-truncated-no-continue.yaml +0 -0
  197. /package/dist/{trace-core → trace-ai}/diagnose/builtin-rules/register.d.ts +0 -0
  198. /package/dist/{trace-core → trace-ai}/diagnose/builtin-rules/register.js +0 -0
  199. /package/dist/{trace-core → trace-ai}/diagnose/builtin-rules/retrieval-empty-no-fallback.d.ts +0 -0
  200. /package/dist/{trace-core → trace-ai}/diagnose/builtin-rules/retrieval-empty-no-fallback.js +0 -0
  201. /package/dist/{trace-core → trace-ai}/diagnose/builtin-rules/retrieval-empty-no-fallback.yaml +0 -0
  202. /package/dist/{trace-core → trace-ai}/diagnose/builtin-rules/tool-error-swallowed.d.ts +0 -0
  203. /package/dist/{trace-core → trace-ai}/diagnose/builtin-rules/tool-error-swallowed.js +0 -0
  204. /package/dist/{trace-core → trace-ai}/diagnose/builtin-rules/tool-error-swallowed.yaml +0 -0
  205. /package/dist/{trace-core → trace-ai}/diagnose/builtin-rules/tool-loop-no-state-change.d.ts +0 -0
  206. /package/dist/{trace-core → trace-ai}/diagnose/builtin-rules/tool-loop-no-state-change.js +0 -0
  207. /package/dist/{trace-core → trace-ai}/diagnose/builtin-rules/tool-loop-no-state-change.yaml +0 -0
  208. /package/dist/{trace-core → trace-ai}/diagnose/predicate-registry.d.ts +0 -0
  209. /package/dist/{trace-core → trace-ai}/diagnose/predicate-registry.js +0 -0
  210. /package/dist/{trace-core → trace-ai}/diagnose/rule-loader.d.ts +0 -0
  211. /package/dist/{trace-core → trace-ai}/diagnose/synthesizer-template.d.ts +0 -0
  212. /package/dist/{trace-core → trace-ai}/diagnose/synthesizer-template.js +0 -0
  213. /package/dist/{trace-core → trace-ai}/diagnose/trace-shaper.d.ts +0 -0
  214. /package/dist/{trace-core → trace-ai}/diagnose/types.js +0 -0
@@ -0,0 +1,71 @@
1
+ function rel(p) { return p; }
2
+ export function renderScanSummaryMarkdown(s) {
3
+ const lines = [];
4
+ const scan = s.scan;
5
+ lines.push(`# Trace Diagnose Batch Summary — agent \`${scan.agent_id}\``);
6
+ lines.push("");
7
+ const resumeBanner = scan.resumed_from_partial
8
+ ? ` · resumed — ${scan.traces_reused} reused, ${scan.traces_freshly_diagnosed} freshly diagnosed`
9
+ : "";
10
+ lines.push(`> ${scan.trace_count} traces · ${scan.traces_with_findings} with findings · diagnosed ${scan.diagnosed_at} · cli \`${scan.cli_version}\`${resumeBanner}`);
11
+ lines.push("");
12
+ // ── Summary ────────────────────────────────────────────────────────────
13
+ lines.push("## Summary");
14
+ lines.push("");
15
+ if (s.summary === null) {
16
+ lines.push("_Stage-4 synthesizer did not complete; aggregates and per-trace reports are still emitted below._");
17
+ lines.push("");
18
+ }
19
+ else {
20
+ lines.push(`**${s.summary.headline}**`);
21
+ lines.push("");
22
+ if (s.summary.primary_root_cause) {
23
+ const rc = s.summary.primary_root_cause;
24
+ lines.push(`Primary root cause — rules ${rc.rule_ids.map((id) => `\`${id}\``).join(", ")}; target for fix: \`${rc.target_for_fix}\`.`);
25
+ lines.push("");
26
+ lines.push(`> ${rc.description.replace(/\r?\n+/g, " ")}`);
27
+ lines.push("");
28
+ }
29
+ }
30
+ // ── Fix priority ───────────────────────────────────────────────────────
31
+ if (s.summary && s.summary.fix_priority.length > 0) {
32
+ lines.push("## Fix priority");
33
+ lines.push("");
34
+ lines.push("| Order | Rule | Affected | Reason |");
35
+ lines.push("|---|---|---|---|");
36
+ s.summary.fix_priority.forEach((p, idx) => {
37
+ lines.push(`| ${idx + 1} | \`${p.rule_id}\` | ${p.affected_trace_count} | ${p.reason.replace(/\|/g, "\\|").replace(/\r?\n/g, " ")} |`);
38
+ });
39
+ lines.push("");
40
+ }
41
+ // ── Cross-rule links ───────────────────────────────────────────────────
42
+ if (s.summary && s.summary.cross_rule_links.length > 0) {
43
+ lines.push("## Cross-rule links");
44
+ lines.push("");
45
+ for (const link of s.summary.cross_rule_links) {
46
+ const ids = link.rule_ids.map((r) => `\`${r}\``).join(" ↔ ");
47
+ lines.push(`- ${ids} — ${link.relation}`);
48
+ }
49
+ lines.push("");
50
+ }
51
+ // ── Aggregates ─────────────────────────────────────────────────────────
52
+ lines.push("## Aggregates");
53
+ lines.push("");
54
+ lines.push("| Rule | Count | high | medium | low |");
55
+ lines.push("|---|---|---|---|---|");
56
+ for (const item of s.aggregates.rule_frequency) {
57
+ lines.push(`| \`${item.rule_id}\` | ${item.count} | ${item.severity_breakdown.high} | ${item.severity_breakdown.medium} | ${item.severity_breakdown.low} |`);
58
+ }
59
+ lines.push("");
60
+ // ── Per-trace index ────────────────────────────────────────────────────
61
+ lines.push("## Per-Trace Reports");
62
+ lines.push("");
63
+ lines.push("| conv_id | trace_id | findings | report |");
64
+ lines.push("|---|---|---|---|");
65
+ for (const item of s.per_trace_index) {
66
+ const mdPath = item.report_path.replace(/\.yaml$/, ".md");
67
+ lines.push(`| \`${item.conversation_id}\` | \`${item.trace_id.slice(0, 16)}…\` | ${item.finding_count} | [yaml](${item.report_path}) / [md](${mdPath}) |`);
68
+ }
69
+ lines.push("");
70
+ return lines.join("\n");
71
+ }
@@ -0,0 +1,73 @@
1
+ import { z } from "zod";
2
+ /**
3
+ * The `summary` block shape — used both as the embedded field of the full
4
+ * scan-summary report AND as the output schema the Stage-4 cross-trace
5
+ * synthesizer LLM must satisfy.
6
+ */
7
+ export declare const ScanSummaryShape: z.ZodObject<{
8
+ headline: z.ZodString;
9
+ primary_root_cause: z.ZodNullable<z.ZodObject<{
10
+ rule_ids: z.ZodArray<z.ZodString>;
11
+ description: z.ZodString;
12
+ target_for_fix: z.ZodString;
13
+ }, z.core.$strip>>;
14
+ fix_priority: z.ZodArray<z.ZodObject<{
15
+ rule_id: z.ZodString;
16
+ affected_trace_count: z.ZodNumber;
17
+ reason: z.ZodString;
18
+ }, z.core.$strip>>;
19
+ cross_rule_links: z.ZodArray<z.ZodObject<{
20
+ rule_ids: z.ZodArray<z.ZodString>;
21
+ relation: z.ZodString;
22
+ }, z.core.$strip>>;
23
+ }, z.core.$strip>;
24
+ export type ScanSummaryShape = z.infer<typeof ScanSummaryShape>;
25
+ export declare const ScanSummarySchema: z.ZodObject<{
26
+ schema_version: z.ZodLiteral<"scan-summary/v1">;
27
+ scan: z.ZodObject<{
28
+ agent_id: z.ZodString;
29
+ trace_count: z.ZodNumber;
30
+ traces_with_findings: z.ZodNumber;
31
+ traces_reused: z.ZodNumber;
32
+ traces_freshly_diagnosed: z.ZodNumber;
33
+ resumed_from_partial: z.ZodBoolean;
34
+ diagnosed_at: z.ZodString;
35
+ cli_version: z.ZodString;
36
+ synthesizer_mode: z.ZodLiteral<"agent">;
37
+ }, z.core.$strip>;
38
+ summary: z.ZodNullable<z.ZodObject<{
39
+ headline: z.ZodString;
40
+ primary_root_cause: z.ZodNullable<z.ZodObject<{
41
+ rule_ids: z.ZodArray<z.ZodString>;
42
+ description: z.ZodString;
43
+ target_for_fix: z.ZodString;
44
+ }, z.core.$strip>>;
45
+ fix_priority: z.ZodArray<z.ZodObject<{
46
+ rule_id: z.ZodString;
47
+ affected_trace_count: z.ZodNumber;
48
+ reason: z.ZodString;
49
+ }, z.core.$strip>>;
50
+ cross_rule_links: z.ZodArray<z.ZodObject<{
51
+ rule_ids: z.ZodArray<z.ZodString>;
52
+ relation: z.ZodString;
53
+ }, z.core.$strip>>;
54
+ }, z.core.$strip>>;
55
+ aggregates: z.ZodObject<{
56
+ rule_frequency: z.ZodArray<z.ZodObject<{
57
+ rule_id: z.ZodString;
58
+ count: z.ZodNumber;
59
+ severity_breakdown: z.ZodObject<{
60
+ high: z.ZodNumber;
61
+ medium: z.ZodNumber;
62
+ low: z.ZodNumber;
63
+ }, z.core.$strip>;
64
+ }, z.core.$strip>>;
65
+ }, z.core.$strip>;
66
+ per_trace_index: z.ZodArray<z.ZodObject<{
67
+ trace_id: z.ZodString;
68
+ conversation_id: z.ZodString;
69
+ report_path: z.ZodString;
70
+ finding_count: z.ZodNumber;
71
+ }, z.core.$strip>>;
72
+ }, z.core.$strip>;
73
+ export type ScanSummary = z.infer<typeof ScanSummarySchema>;
@@ -0,0 +1,61 @@
1
+ // packages/typescript/src/trace-ai/scan/scan-summary-schema.ts
2
+ import { z } from "zod";
3
+ const PrimaryRootCauseShape = z.object({
4
+ rule_ids: z.array(z.string()).min(1),
5
+ description: z.string(),
6
+ target_for_fix: z.string(),
7
+ });
8
+ const FixPriorityItemShape = z.object({
9
+ rule_id: z.string(),
10
+ affected_trace_count: z.number().int().nonnegative(),
11
+ reason: z.string(),
12
+ });
13
+ const CrossRuleLinkShape = z.object({
14
+ rule_ids: z.array(z.string()).min(2),
15
+ relation: z.string(),
16
+ });
17
+ /**
18
+ * The `summary` block shape — used both as the embedded field of the full
19
+ * scan-summary report AND as the output schema the Stage-4 cross-trace
20
+ * synthesizer LLM must satisfy.
21
+ */
22
+ export const ScanSummaryShape = z.object({
23
+ headline: z.string().max(160),
24
+ primary_root_cause: PrimaryRootCauseShape.nullable(),
25
+ fix_priority: z.array(FixPriorityItemShape),
26
+ cross_rule_links: z.array(CrossRuleLinkShape),
27
+ });
28
+ const RuleFrequencyItemShape = z.object({
29
+ rule_id: z.string(),
30
+ count: z.number().int().nonnegative(),
31
+ severity_breakdown: z.object({
32
+ high: z.number().int().nonnegative(),
33
+ medium: z.number().int().nonnegative(),
34
+ low: z.number().int().nonnegative(),
35
+ }),
36
+ });
37
+ const PerTraceIndexItemShape = z.object({
38
+ trace_id: z.string(),
39
+ conversation_id: z.string(),
40
+ report_path: z.string(),
41
+ finding_count: z.number().int().nonnegative(),
42
+ });
43
+ export const ScanSummarySchema = z.object({
44
+ schema_version: z.literal("scan-summary/v1"),
45
+ scan: z.object({
46
+ agent_id: z.string().min(1),
47
+ trace_count: z.number().int().nonnegative(),
48
+ traces_with_findings: z.number().int().nonnegative(),
49
+ traces_reused: z.number().int().nonnegative(),
50
+ traces_freshly_diagnosed: z.number().int().nonnegative(),
51
+ resumed_from_partial: z.boolean(),
52
+ diagnosed_at: z.string(),
53
+ cli_version: z.string(),
54
+ synthesizer_mode: z.literal("agent"),
55
+ }),
56
+ summary: ScanSummaryShape.nullable(),
57
+ aggregates: z.object({
58
+ rule_frequency: z.array(RuleFrequencyItemShape),
59
+ }),
60
+ per_trace_index: z.array(PerTraceIndexItemShape),
61
+ });
@@ -0,0 +1,23 @@
1
+ export type SingleAgentValidationErrorCode = "empty" | "no-spans" | "mixed";
2
+ export declare class SingleAgentValidationError extends Error {
3
+ readonly code: SingleAgentValidationErrorCode;
4
+ readonly byConvId: ReadonlyMap<string, string | undefined>;
5
+ constructor(code: SingleAgentValidationErrorCode, message: string, byConvId?: ReadonlyMap<string, string | undefined>);
6
+ }
7
+ export interface SingleAgentValidationResult {
8
+ agentId: string;
9
+ checkedConvIds: number;
10
+ }
11
+ export interface FetchSpansResult {
12
+ spans: Array<{
13
+ attributes: Record<string, unknown>;
14
+ }>;
15
+ conversation_id: string;
16
+ }
17
+ export type FetchSpansByConvId = (convId: string) => Promise<FetchSpansResult>;
18
+ /**
19
+ * Verify every conv_id in the batch resolves to spans owned by the same
20
+ * agent_id. Throws SingleAgentValidationError on mismatch with a discrepancy
21
+ * map for the CLI to print.
22
+ */
23
+ export declare function validateSingleAgent(convIds: string[], fetchSpansById: FetchSpansByConvId): Promise<SingleAgentValidationResult>;
@@ -0,0 +1,42 @@
1
+ export class SingleAgentValidationError extends Error {
2
+ code;
3
+ byConvId;
4
+ constructor(code, message, byConvId = new Map()) {
5
+ super(message);
6
+ this.code = code;
7
+ this.byConvId = byConvId;
8
+ this.name = "SingleAgentValidationError";
9
+ }
10
+ }
11
+ function extractAgentId(spans) {
12
+ for (const s of spans) {
13
+ const v = s.attributes["gen_ai.agent.id"];
14
+ if (typeof v === "string" && v.length > 0)
15
+ return v;
16
+ }
17
+ return undefined;
18
+ }
19
+ /**
20
+ * Verify every conv_id in the batch resolves to spans owned by the same
21
+ * agent_id. Throws SingleAgentValidationError on mismatch with a discrepancy
22
+ * map for the CLI to print.
23
+ */
24
+ export async function validateSingleAgent(convIds, fetchSpansById) {
25
+ if (convIds.length === 0) {
26
+ throw new SingleAgentValidationError("empty", "no conversation_ids supplied");
27
+ }
28
+ const byConvId = new Map();
29
+ for (const convId of convIds) {
30
+ const fetched = await fetchSpansById(convId);
31
+ if (fetched.spans.length === 0) {
32
+ throw new SingleAgentValidationError("no-spans", `conversation_id has no spans: ${convId}`);
33
+ }
34
+ byConvId.set(convId, extractAgentId(fetched.spans));
35
+ }
36
+ const agentIds = new Set(byConvId.values());
37
+ if (agentIds.size > 1 || (agentIds.size === 1 && agentIds.has(undefined))) {
38
+ const lines = [...byConvId.entries()].map(([c, a]) => ` ${c} → ${a ?? "(no agent.id)"}`).join("\n");
39
+ throw new SingleAgentValidationError("mixed", `--traces conversation_ids span multiple agents:\n${lines}`, byConvId);
40
+ }
41
+ return { agentId: [...agentIds][0], checkedConvIds: convIds.length };
42
+ }
@@ -0,0 +1,15 @@
1
+ export type TracesListErrorCode = "empty" | "file-not-found";
2
+ export declare class TracesListError extends Error {
3
+ readonly code: TracesListErrorCode;
4
+ constructor(code: TracesListErrorCode, message: string);
5
+ }
6
+ /**
7
+ * Parse the `--traces` argument value into an array of conversation_ids.
8
+ * Two forms:
9
+ * - comma-separated: "conv1,conv2,conv3"
10
+ * - @file path: "@/path/to/ids.txt" (one id per line; # comments and blanks ignored)
11
+ *
12
+ * Throws TracesListError with code='empty' for empty result, 'file-not-found'
13
+ * when @file path does not exist.
14
+ */
15
+ export declare function parseTracesList(arg: string): Promise<string[]>;
@@ -0,0 +1,46 @@
1
+ import fs from "node:fs/promises";
2
+ export class TracesListError extends Error {
3
+ code;
4
+ constructor(code, message) {
5
+ super(message);
6
+ this.code = code;
7
+ this.name = "TracesListError";
8
+ }
9
+ }
10
+ /**
11
+ * Parse the `--traces` argument value into an array of conversation_ids.
12
+ * Two forms:
13
+ * - comma-separated: "conv1,conv2,conv3"
14
+ * - @file path: "@/path/to/ids.txt" (one id per line; # comments and blanks ignored)
15
+ *
16
+ * Throws TracesListError with code='empty' for empty result, 'file-not-found'
17
+ * when @file path does not exist.
18
+ */
19
+ export async function parseTracesList(arg) {
20
+ if (arg.startsWith("@")) {
21
+ const filePath = arg.slice(1);
22
+ let raw;
23
+ try {
24
+ raw = await fs.readFile(filePath, "utf8");
25
+ }
26
+ catch {
27
+ throw new TracesListError("file-not-found", `--traces file not found: ${filePath}`);
28
+ }
29
+ const ids = raw
30
+ .split(/\r?\n/)
31
+ .map((line) => line.trim())
32
+ .filter((line) => line.length > 0 && !line.startsWith("#"));
33
+ if (ids.length === 0) {
34
+ throw new TracesListError("empty", `no conversation_ids found in ${filePath}`);
35
+ }
36
+ return ids;
37
+ }
38
+ const ids = arg
39
+ .split(",")
40
+ .map((s) => s.trim())
41
+ .filter((s) => s.length > 0);
42
+ if (ids.length === 0) {
43
+ throw new TracesListError("empty", "empty --traces value");
44
+ }
45
+ return ids;
46
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kweaver-ai/kweaver-sdk",
3
- "version": "0.8.1",
3
+ "version": "0.8.3",
4
4
  "description": "KWeaver TypeScript SDK — CLI tool and programmatic API for knowledge networks and Decision Agents.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -24,7 +24,7 @@
24
24
  ],
25
25
  "scripts": {
26
26
  "dev": "tsx watch src/cli.ts",
27
- "build": "node node_modules/typescript/bin/tsc -p tsconfig.json && rm -rf dist/templates && cp -R src/templates dist/templates && cp src/trace-core/diagnose/builtin-rules/*.yaml dist/trace-core/diagnose/builtin-rules/",
27
+ "build": "node node_modules/typescript/bin/tsc -p tsconfig.json && rm -rf dist/templates && cp -R src/templates dist/templates && cp src/trace-ai/diagnose/builtin-rules/*.yaml dist/trace-ai/diagnose/builtin-rules/ && mkdir -p dist/agent-providers/prompts && cp src/agent-providers/prompts/*.prompt.md dist/agent-providers/prompts/ && cp src/trace-ai/diagnose/builtin-rules/*.prompt.md dist/trace-ai/diagnose/builtin-rules/ 2>/dev/null || true && mkdir -p dist/trace-ai/scan/prompts/builtin && cp src/trace-ai/scan/prompts/builtin/*.prompt.md dist/trace-ai/scan/prompts/builtin/ && mkdir -p dist/trace-ai/eval-set/rubric-templates && cp src/trace-ai/eval-set/rubric-templates/*.prompt.md dist/trace-ai/eval-set/rubric-templates/",
28
28
  "start": "node ./dist/cli.js",
29
29
  "lint": "node node_modules/typescript/bin/tsc --noEmit -p tsconfig.json",
30
30
  "test": "node --import tsx --test test/*.test.ts",
@@ -1,117 +0,0 @@
1
- /** Field metadata returned by the data-views API. */
2
- export interface ViewField {
3
- name: string;
4
- type: string;
5
- display_name?: string;
6
- comment?: string;
7
- }
8
- /** Normalized data view model (mdl-data-model). */
9
- export interface DataView {
10
- id: string;
11
- name: string;
12
- query_type: string;
13
- datasource_id: string;
14
- /** View type, e.g. "atomic" or "custom". */
15
- type?: string;
16
- /** Underlying data source engine, e.g. "mysql", "postgresql". */
17
- data_source_type?: string;
18
- /** Human-readable data source name. */
19
- data_source_name?: string;
20
- /** Full SQL expression stored in the view definition (Trino catalog.schema.table). */
21
- sql_str?: string;
22
- /** Fully-qualified table reference (catalog."schema"."table"). */
23
- meta_table_name?: string;
24
- /** Field metadata. Populated by `get`; absent (`undefined`) in `list` results. */
25
- fields?: ViewField[];
26
- }
27
- export declare function parseDataView(raw: Record<string, unknown>): DataView;
28
- export interface CreateDataViewOptions {
29
- baseUrl: string;
30
- accessToken: string;
31
- name: string;
32
- datasourceId: string;
33
- table: string;
34
- fields?: Array<{
35
- name: string;
36
- type: string;
37
- }>;
38
- businessDomain?: string;
39
- }
40
- export declare function createDataView(options: CreateDataViewOptions): Promise<string>;
41
- export interface ListDataViewsOptions {
42
- baseUrl: string;
43
- accessToken: string;
44
- businessDomain?: string;
45
- /** Filter by data source id. */
46
- datasourceId?: string;
47
- /** Server-side keyword filter (fuzzy). */
48
- name?: string;
49
- /** View type filter (e.g. atomic, custom). */
50
- type?: string;
51
- /** Max items; default -1 (all). */
52
- limit?: number;
53
- }
54
- export declare function listDataViews(options: ListDataViewsOptions): Promise<DataView[]>;
55
- export interface DeleteDataViewOptions {
56
- baseUrl: string;
57
- accessToken: string;
58
- id: string;
59
- businessDomain?: string;
60
- }
61
- export declare function deleteDataView(options: DeleteDataViewOptions): Promise<void>;
62
- export interface GetDataViewOptions {
63
- baseUrl: string;
64
- accessToken: string;
65
- id: string;
66
- businessDomain?: string;
67
- }
68
- export declare function getDataView(options: GetDataViewOptions): Promise<DataView>;
69
- export interface FindDataViewOptions {
70
- baseUrl: string;
71
- accessToken: string;
72
- businessDomain?: string;
73
- /** View name to search for (sent as keyword to server). */
74
- name: string;
75
- /** Filter by data source id. */
76
- datasourceId?: string;
77
- /** When true, apply client-side exact name match after keyword search (default false). */
78
- exact?: boolean;
79
- /** When true, poll until a result appears or timeout (default false). */
80
- wait?: boolean;
81
- /** Total wait budget in ms (default 30000). Only used when wait is true. */
82
- timeoutMs?: number;
83
- }
84
- /**
85
- * Find data views by name. Uses server-side keyword filtering; when `exact` is true,
86
- * applies client-side `name ===` filter. Optional polling with exponential backoff.
87
- */
88
- export declare function findDataView(options: FindDataViewOptions): Promise<DataView[]>;
89
- /** Options for querying data view rows via mdl-uniquery (SQL / view definition). */
90
- export interface QueryDataViewOptions {
91
- baseUrl: string;
92
- accessToken: string;
93
- id: string;
94
- sql?: string;
95
- offset?: number;
96
- limit?: number;
97
- needTotal?: boolean;
98
- outputFields?: string[];
99
- filters?: Record<string, unknown>;
100
- sort?: Array<Record<string, unknown>>;
101
- businessDomain?: string;
102
- }
103
- /** Query result from mdl-uniquery data-views POST (shape varies by backend). */
104
- export interface DataViewQueryResult {
105
- columns?: Array<{
106
- name: string;
107
- type?: string;
108
- vega_type?: string;
109
- }>;
110
- entries?: unknown;
111
- total_count?: number;
112
- }
113
- /**
114
- * Execute a query against a data view (POST /api/mdl-uniquery/v1/data-views/:id).
115
- * When `sql` is omitted, the server uses the view's stored SQL definition.
116
- */
117
- export declare function queryDataView(options: QueryDataViewOptions): Promise<DataViewQueryResult>;