@kweaver-ai/kweaver-sdk 0.7.3 → 0.8.1

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 (88) hide show
  1. package/README.md +49 -0
  2. package/README.zh.md +44 -0
  3. package/bin/kweaver.js +12 -11
  4. package/dist/api/agent-observability.d.ts +51 -0
  5. package/dist/api/agent-observability.js +108 -0
  6. package/dist/api/bkn-backend.d.ts +1 -0
  7. package/dist/api/bkn-backend.js +1 -1
  8. package/dist/api/bkn-metrics.d.ts +59 -0
  9. package/dist/api/bkn-metrics.js +129 -0
  10. package/dist/api/conversations.d.ts +43 -2
  11. package/dist/api/conversations.js +77 -23
  12. package/dist/api/datasources.d.ts +2 -20
  13. package/dist/api/datasources.js +7 -86
  14. package/dist/api/model-invocation.d.ts +58 -0
  15. package/dist/api/model-invocation.js +203 -0
  16. package/dist/api/models.d.ts +79 -0
  17. package/dist/api/models.js +183 -0
  18. package/dist/api/ontology-query-metrics.d.ts +14 -0
  19. package/dist/api/ontology-query-metrics.js +30 -0
  20. package/dist/api/trace.d.ts +44 -0
  21. package/dist/api/trace.js +81 -0
  22. package/dist/api/vega.d.ts +53 -0
  23. package/dist/api/vega.js +144 -0
  24. package/dist/bundled-model-templates.d.ts +17 -0
  25. package/dist/bundled-model-templates.js +24 -0
  26. package/dist/cli.js +15 -0
  27. package/dist/client.d.ts +3 -0
  28. package/dist/client.js +5 -0
  29. package/dist/commands/agent.d.ts +7 -1
  30. package/dist/commands/agent.js +75 -21
  31. package/dist/commands/bkn-metric.d.ts +1 -0
  32. package/dist/commands/bkn-metric.js +406 -0
  33. package/dist/commands/bkn-ops.js +28 -16
  34. package/dist/commands/bkn-utils.d.ts +38 -0
  35. package/dist/commands/bkn-utils.js +54 -0
  36. package/dist/commands/bkn.js +4 -0
  37. package/dist/commands/ds.js +14 -3
  38. package/dist/commands/explore-chat.js +2 -2
  39. package/dist/commands/model.d.ts +72 -0
  40. package/dist/commands/model.js +1315 -0
  41. package/dist/commands/trace.d.ts +14 -0
  42. package/dist/commands/trace.js +168 -0
  43. package/dist/index.d.ts +9 -0
  44. package/dist/index.js +5 -0
  45. package/dist/resources/datasources.js +2 -1
  46. package/dist/resources/models.d.ts +40 -0
  47. package/dist/resources/models.js +88 -0
  48. package/dist/templates/model/llm-basic.json +13 -0
  49. package/dist/templates/model/manifest.json +16 -0
  50. package/dist/templates/model/small-basic.json +6 -0
  51. package/dist/trace-core/diagnose/builtin-rules/excessive-tool-calls-per-turn.d.ts +2 -0
  52. package/dist/trace-core/diagnose/builtin-rules/excessive-tool-calls-per-turn.js +15 -0
  53. package/dist/trace-core/diagnose/builtin-rules/excessive-tool-calls-per-turn.yaml +16 -0
  54. package/dist/trace-core/diagnose/builtin-rules/llm-response-truncated-no-continue.d.ts +2 -0
  55. package/dist/trace-core/diagnose/builtin-rules/llm-response-truncated-no-continue.js +44 -0
  56. package/dist/trace-core/diagnose/builtin-rules/llm-response-truncated-no-continue.yaml +15 -0
  57. package/dist/trace-core/diagnose/builtin-rules/register.d.ts +1 -0
  58. package/dist/trace-core/diagnose/builtin-rules/register.js +11 -0
  59. package/dist/trace-core/diagnose/builtin-rules/retrieval-empty-no-fallback.d.ts +2 -0
  60. package/dist/trace-core/diagnose/builtin-rules/retrieval-empty-no-fallback.js +29 -0
  61. package/dist/trace-core/diagnose/builtin-rules/retrieval-empty-no-fallback.yaml +15 -0
  62. package/dist/trace-core/diagnose/builtin-rules/tool-error-swallowed.d.ts +2 -0
  63. package/dist/trace-core/diagnose/builtin-rules/tool-error-swallowed.js +45 -0
  64. package/dist/trace-core/diagnose/builtin-rules/tool-error-swallowed.yaml +15 -0
  65. package/dist/trace-core/diagnose/builtin-rules/tool-loop-no-state-change.d.ts +2 -0
  66. package/dist/trace-core/diagnose/builtin-rules/tool-loop-no-state-change.js +38 -0
  67. package/dist/trace-core/diagnose/builtin-rules/tool-loop-no-state-change.yaml +16 -0
  68. package/dist/trace-core/diagnose/index.d.ts +9 -0
  69. package/dist/trace-core/diagnose/index.js +104 -0
  70. package/dist/trace-core/diagnose/predicate-registry.d.ts +7 -0
  71. package/dist/trace-core/diagnose/predicate-registry.js +30 -0
  72. package/dist/trace-core/diagnose/report-assembler.d.ts +12 -0
  73. package/dist/trace-core/diagnose/report-assembler.js +90 -0
  74. package/dist/trace-core/diagnose/rule-loader.d.ts +11 -0
  75. package/dist/trace-core/diagnose/rule-loader.js +86 -0
  76. package/dist/trace-core/diagnose/schemas.d.ts +109 -0
  77. package/dist/trace-core/diagnose/schemas.js +94 -0
  78. package/dist/trace-core/diagnose/signal-probe.d.ts +5 -0
  79. package/dist/trace-core/diagnose/signal-probe.js +21 -0
  80. package/dist/trace-core/diagnose/synthesizer-template.d.ts +2 -0
  81. package/dist/trace-core/diagnose/synthesizer-template.js +49 -0
  82. package/dist/trace-core/diagnose/trace-shaper.d.ts +3 -0
  83. package/dist/trace-core/diagnose/trace-shaper.js +72 -0
  84. package/dist/trace-core/diagnose/types.d.ts +124 -0
  85. package/dist/trace-core/diagnose/types.js +1 -0
  86. package/dist/utils/trace-views.d.ts +44 -0
  87. package/dist/utils/trace-views.js +425 -0
  88. package/package.json +15 -5
@@ -0,0 +1,49 @@
1
+ const SEVERITY_RANK = { high: 3, medium: 2, low: 1 };
2
+ function overlapRatio(a, b) {
3
+ if (a.length === 0 || b.length === 0)
4
+ return 0;
5
+ const setA = new Set(a);
6
+ const intersect = b.filter((x) => setA.has(x)).length;
7
+ const smaller = Math.min(a.length, b.length);
8
+ return intersect / smaller;
9
+ }
10
+ export function templateSynthesize(findings) {
11
+ if (findings.length === 0) {
12
+ return {
13
+ headline: "No findings",
14
+ primaryRootCause: null,
15
+ fixPriority: [],
16
+ crossFindingLinks: [],
17
+ };
18
+ }
19
+ // Sort indices by severity desc, stable on original index (so same input → same output).
20
+ const indices = findings.map((_, i) => i);
21
+ indices.sort((i, j) => {
22
+ const r = SEVERITY_RANK[findings[j].severity] - SEVERITY_RANK[findings[i].severity];
23
+ return r !== 0 ? r : i - j;
24
+ });
25
+ const topIdx = indices[0];
26
+ const top = findings[topIdx];
27
+ const headline = `see findings[${topIdx}]: ${top.symptom}`;
28
+ const primaryRootCause = {
29
+ findingIds: [topIdx],
30
+ description: `Top-severity finding from rule '${top.ruleId}': ${top.symptom}`,
31
+ targetForFix: top.suggestedFix.target,
32
+ };
33
+ const fixPriority = indices.map((i) => ({
34
+ findingId: i,
35
+ reason: `severity=${findings[i].severity}`,
36
+ }));
37
+ const crossFindingLinks = [];
38
+ for (let i = 0; i < findings.length; i++) {
39
+ for (let j = i + 1; j < findings.length; j++) {
40
+ if (overlapRatio(findings[i].evidence.spans, findings[j].evidence.spans) >= 0.5) {
41
+ crossFindingLinks.push({
42
+ findingIds: [i, j],
43
+ relation: "overlapping_evidence_spans",
44
+ });
45
+ }
46
+ }
47
+ }
48
+ return { headline, primaryRootCause, fixPriority, crossFindingLinks };
49
+ }
@@ -0,0 +1,3 @@
1
+ import type { TraceTree } from "./types.js";
2
+ import type { RawSpan } from "../../api/trace.js";
3
+ export declare function assembleTraceTree(traceId: string, raw: RawSpan[]): TraceTree;
@@ -0,0 +1,72 @@
1
+ // Map from OTel GenAI `gen_ai.operation.name` (the cross-runtime standard) to
2
+ // the diagnostic SpanKind buckets rules filter on. `agent.trace.type` is kept
3
+ // as a fallback for runtimes/fixtures that pre-tag spans with our own taxonomy.
4
+ const KIND_MAP = {
5
+ // OTel GenAI semconv operation names
6
+ chat: "llm",
7
+ text_completion: "llm",
8
+ embeddings: "retrieval",
9
+ execute_tool: "tool",
10
+ // pre-existing custom taxonomy (synthetic fixtures, optional runtime tag)
11
+ model: "llm",
12
+ llm: "llm",
13
+ tool: "tool",
14
+ retrieval: "retrieval",
15
+ reasoning: "reasoning",
16
+ };
17
+ function deriveKind(attrs) {
18
+ const op = attrs["gen_ai.operation.name"];
19
+ if (typeof op === "string" && op in KIND_MAP)
20
+ return KIND_MAP[op];
21
+ const t = attrs["agent.trace.type"];
22
+ if (typeof t === "string" && t in KIND_MAP)
23
+ return KIND_MAP[t];
24
+ return "unknown";
25
+ }
26
+ function deriveStatus(raw) {
27
+ const code = raw?.code?.toUpperCase();
28
+ if (code === "OK")
29
+ return "ok";
30
+ if (code === "ERROR")
31
+ return "error";
32
+ return "unset";
33
+ }
34
+ function durationMs(start, end) {
35
+ if (!start || !end)
36
+ return 0;
37
+ // string nanos → BigInt to avoid precision loss, then convert to ms.
38
+ const s = BigInt(start);
39
+ const e = BigInt(end);
40
+ return Number((e - s) / 1000000n);
41
+ }
42
+ export function assembleTraceTree(traceId, raw) {
43
+ const spans = raw.map((r) => {
44
+ const attrs = r.attributes ?? {};
45
+ return {
46
+ spanId: r.spanId,
47
+ parentSpanId: r.parentSpanId ?? null,
48
+ name: r.name ?? "",
49
+ kind: deriveKind(attrs),
50
+ startTimeUnixNano: r.startTimeUnixNano ?? "0",
51
+ endTimeUnixNano: r.endTimeUnixNano ?? "0",
52
+ durationMs: durationMs(r.startTimeUnixNano, r.endTimeUnixNano),
53
+ status: deriveStatus(r.status),
54
+ attributes: attrs,
55
+ };
56
+ });
57
+ const byId = new Map();
58
+ const parentToChildren = new Map();
59
+ const byKind = new Map();
60
+ for (const s of spans) {
61
+ byId.set(s.spanId, s);
62
+ const arr = parentToChildren.get(s.parentSpanId) ?? [];
63
+ arr.push(s);
64
+ parentToChildren.set(s.parentSpanId, arr);
65
+ const kindArr = byKind.get(s.kind) ?? [];
66
+ kindArr.push(s);
67
+ byKind.set(s.kind, kindArr);
68
+ }
69
+ const roots = parentToChildren.get(null) ?? [];
70
+ const root = roots.length > 0 ? roots[0] : null;
71
+ return { traceId, spans, byId, parentToChildren, byKind, root };
72
+ }
@@ -0,0 +1,124 @@
1
+ export interface SpanAttributes {
2
+ [key: string]: unknown;
3
+ }
4
+ export interface Span {
5
+ spanId: string;
6
+ parentSpanId: string | null;
7
+ name: string;
8
+ kind: SpanKind;
9
+ startTimeUnixNano: string;
10
+ endTimeUnixNano: string;
11
+ durationMs: number;
12
+ status: 'ok' | 'error' | 'unset';
13
+ attributes: SpanAttributes;
14
+ }
15
+ export type SpanKind = 'tool' | 'llm' | 'retrieval' | 'reasoning' | 'unknown';
16
+ export interface TraceTree {
17
+ traceId: string;
18
+ spans: Span[];
19
+ byId: Map<string, Span>;
20
+ parentToChildren: Map<string | null, Span[]>;
21
+ byKind: Map<SpanKind, Span[]>;
22
+ root: Span | null;
23
+ }
24
+ export interface RuleTaxonomy {
25
+ signalsAxis: 'interaction' | 'execution' | 'environment';
26
+ msClass: 'retry_loop' | 'tool_misuse' | 'context_loss' | 'goal_drift' | 'cascading_error' | 'silent_quality_degradation';
27
+ }
28
+ export interface Rule {
29
+ schemaVersion: 'diagnosis-rule/v1';
30
+ id: string;
31
+ severity: 'low' | 'medium' | 'high';
32
+ symptom: string;
33
+ taxonomy: RuleTaxonomy;
34
+ suggestedFix: {
35
+ target: string;
36
+ changeTemplate: string;
37
+ };
38
+ verifyWith: {
39
+ assertionTemplates: string[];
40
+ };
41
+ predicateRef: string;
42
+ params: Record<string, unknown>;
43
+ sourcePath: string;
44
+ }
45
+ export interface Hit {
46
+ evidenceSpans: string[];
47
+ excerpt: string;
48
+ bindings: Record<string, unknown>;
49
+ }
50
+ export type Predicate = (trace: TraceTree, params: Record<string, unknown>) => Hit[];
51
+ export interface Finding {
52
+ ruleId: string;
53
+ judgmentKind: 'symbolic';
54
+ severity: 'low' | 'medium' | 'high';
55
+ symptom: string;
56
+ likelyCause: string;
57
+ evidence: {
58
+ spans: string[];
59
+ excerpt: string;
60
+ };
61
+ suggestedFix: {
62
+ target: string;
63
+ change: string;
64
+ };
65
+ confidence: 'low';
66
+ verifyWith: {
67
+ suggestedEvalCase: {
68
+ queryId: string | null;
69
+ query: string | null;
70
+ assertions: string[];
71
+ };
72
+ };
73
+ }
74
+ export interface SummaryRootCause {
75
+ findingIds: number[];
76
+ description: string;
77
+ targetForFix: string;
78
+ }
79
+ export interface SummaryFixPriority {
80
+ findingId: number;
81
+ reason: string;
82
+ }
83
+ export interface SummaryCrossLink {
84
+ findingIds: number[];
85
+ relation: string;
86
+ }
87
+ export interface Summary {
88
+ headline: string;
89
+ primaryRootCause: SummaryRootCause | null;
90
+ fixPriority: SummaryFixPriority[];
91
+ crossFindingLinks: SummaryCrossLink[];
92
+ }
93
+ export interface Report {
94
+ schemaVersion: 'trace-diagnose-report/v1';
95
+ trace: {
96
+ traceId: string;
97
+ agentId: string | null;
98
+ tenant: string | null;
99
+ };
100
+ run: {
101
+ diagnosedAt: string;
102
+ cliVersion: string;
103
+ mode: 'symbolic-only';
104
+ rulesApplied: string[];
105
+ rulesSkipped: {
106
+ ruleId: string;
107
+ reason: string;
108
+ }[];
109
+ synthesizerMode: 'template';
110
+ };
111
+ summary: Summary;
112
+ findings: Finding[];
113
+ }
114
+ export interface DiagnoseOpts {
115
+ out: string | null;
116
+ rulesDir: string | null;
117
+ noBuiltin: boolean;
118
+ noLlm: true;
119
+ agentProvider: string | null;
120
+ timeoutMs: number;
121
+ baseUrl: string;
122
+ token: string;
123
+ businessDomain: string;
124
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,44 @@
1
+ import type { TraceSpan, TracesByConversationResult } from "../api/conversations.js";
2
+ export interface SpanNode {
3
+ span: TraceSpan;
4
+ children: SpanNode[];
5
+ }
6
+ /**
7
+ * Build a parent-child forest from a flat span array.
8
+ *
9
+ * Spans whose parentSpanId is missing or points to a span that is not in the set
10
+ * are treated as roots. Multiple traceIds in one conversation produce multiple roots.
11
+ * Children are sorted by startTime ascending.
12
+ */
13
+ export declare function buildSpanTree(spans: TraceSpan[]): SpanNode[];
14
+ /**
15
+ * Tree view: indented call topology with duration, status, service, and inline metadata.
16
+ */
17
+ export declare function formatTreeView(spans: TraceSpan[]): string;
18
+ /**
19
+ * Perf view: aggregated duration and call count per category.
20
+ */
21
+ export declare function formatPerfView(spans: TraceSpan[]): string;
22
+ /**
23
+ * Evidence view: ordered chain of execute_tool spans with arguments, hit count, and score.
24
+ */
25
+ export declare function formatEvidenceView(spans: TraceSpan[]): string;
26
+ export interface ReasoningOptions {
27
+ /** When true, print every message at full length. Default truncates large bodies. */
28
+ full?: boolean;
29
+ }
30
+ /**
31
+ * Reasoning view: walks the LLM's chat history (input messages of the chat span +
32
+ * the assistant's final output) so the user can read the agent's actual thought
33
+ * process — system / user / assistant / tool_call / tool result / final answer.
34
+ */
35
+ export declare function formatReasoningView(spans: TraceSpan[], opts?: ReasoningOptions): string;
36
+ export type TraceView = "tree" | "perf" | "evidence" | "reasoning" | "all";
37
+ export interface FormatOptions {
38
+ /** Pass through to formatReasoningView — disables per-message truncation. */
39
+ full?: boolean;
40
+ }
41
+ /**
42
+ * Render one or more views over a trace result. Returns concatenated text suitable for stdout.
43
+ */
44
+ export declare function formatTraceResult(result: TracesByConversationResult, view: TraceView, opts?: FormatOptions): string;