@kweaver-ai/kweaver-sdk 0.7.3 → 0.7.4

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 (43) hide show
  1. package/README.md +29 -0
  2. package/README.zh.md +26 -0
  3. package/bin/kweaver.js +12 -11
  4. package/dist/api/bkn-backend.d.ts +1 -0
  5. package/dist/api/bkn-backend.js +1 -1
  6. package/dist/api/bkn-metrics.d.ts +59 -0
  7. package/dist/api/bkn-metrics.js +129 -0
  8. package/dist/api/conversations.d.ts +47 -2
  9. package/dist/api/conversations.js +113 -17
  10. package/dist/api/datasources.js +43 -6
  11. package/dist/api/model-invocation.d.ts +58 -0
  12. package/dist/api/model-invocation.js +203 -0
  13. package/dist/api/models.d.ts +79 -0
  14. package/dist/api/models.js +183 -0
  15. package/dist/api/ontology-query-metrics.d.ts +14 -0
  16. package/dist/api/ontology-query-metrics.js +30 -0
  17. package/dist/bundled-model-templates.d.ts +17 -0
  18. package/dist/bundled-model-templates.js +24 -0
  19. package/dist/cli.js +10 -0
  20. package/dist/client.d.ts +3 -0
  21. package/dist/client.js +5 -0
  22. package/dist/commands/agent.d.ts +7 -1
  23. package/dist/commands/agent.js +75 -21
  24. package/dist/commands/bkn-metric.d.ts +1 -0
  25. package/dist/commands/bkn-metric.js +406 -0
  26. package/dist/commands/bkn-ops.js +17 -11
  27. package/dist/commands/bkn-utils.d.ts +29 -0
  28. package/dist/commands/bkn-utils.js +37 -0
  29. package/dist/commands/bkn.js +4 -0
  30. package/dist/commands/ds.js +7 -1
  31. package/dist/commands/explore-chat.js +2 -2
  32. package/dist/commands/model.d.ts +72 -0
  33. package/dist/commands/model.js +1315 -0
  34. package/dist/index.d.ts +9 -0
  35. package/dist/index.js +5 -0
  36. package/dist/resources/models.d.ts +40 -0
  37. package/dist/resources/models.js +88 -0
  38. package/dist/templates/model/llm-basic.json +13 -0
  39. package/dist/templates/model/manifest.json +16 -0
  40. package/dist/templates/model/small-basic.json +6 -0
  41. package/dist/utils/trace-views.d.ts +44 -0
  42. package/dist/utils/trace-views.js +425 -0
  43. package/package.json +3 -3
@@ -43,6 +43,35 @@ export declare function detectPrimaryKey(table: {
43
43
  type: string;
44
44
  }>;
45
45
  }, rows?: Array<Record<string, string | null>>): PkDetectionResult;
46
+ export interface PkResolution {
47
+ /** Resolved PK column name, or null when caller must fail-fast. */
48
+ pk: string | null;
49
+ /** Origin of the resolution — used by callers for messaging and warnings. */
50
+ source: "override" | "schema" | "sample" | "ambiguous";
51
+ /** For 'sample' source: cardinality candidates from `detectPrimaryKey`. */
52
+ candidates?: PkCandidate[];
53
+ /** For 'sample' source: rows seen, propagated for error formatting. */
54
+ sampleSize?: number;
55
+ /** For 'ambiguous' source: schema-declared composite PK columns. */
56
+ ambiguous?: string[];
57
+ }
58
+ /**
59
+ * Resolve a single PK for a BKN object type, in priority order:
60
+ * 1. caller-provided override (e.g. --pk-map)
61
+ * 2. schema-declared single PK from datasource metadata
62
+ * 3. sample-based detection (CSV / schemaless sources)
63
+ * Composite SQL PKs intentionally surface as `source: "ambiguous"` — BKN
64
+ * object types take a single PK, so the caller must pick via --pk-map.
65
+ */
66
+ export declare function resolvePrimaryKey(table: {
67
+ name: string;
68
+ columns: Array<{
69
+ name: string;
70
+ type: string;
71
+ isPrimaryKey?: boolean;
72
+ }>;
73
+ primaryKeys?: string[];
74
+ }, sampleRows?: Array<Record<string, string | null>>, override?: string | null): PkResolution;
46
75
  /** Format a user-facing error message when PK auto-detection fails. */
47
76
  export declare function formatPkDetectionError(tableName: string, result: PkDetectionResult): string;
48
77
  /**
@@ -98,6 +98,43 @@ export function detectPrimaryKey(table, rows) {
98
98
  sampleSize: rows.length,
99
99
  };
100
100
  }
101
+ /**
102
+ * Resolve a single PK for a BKN object type, in priority order:
103
+ * 1. caller-provided override (e.g. --pk-map)
104
+ * 2. schema-declared single PK from datasource metadata
105
+ * 3. sample-based detection (CSV / schemaless sources)
106
+ * Composite SQL PKs intentionally surface as `source: "ambiguous"` — BKN
107
+ * object types take a single PK, so the caller must pick via --pk-map.
108
+ */
109
+ export function resolvePrimaryKey(table, sampleRows, override) {
110
+ if (override) {
111
+ return { pk: override, source: "override" };
112
+ }
113
+ const schemaPks = collectSchemaPks(table);
114
+ if (schemaPks.length === 1) {
115
+ return { pk: schemaPks[0], source: "schema" };
116
+ }
117
+ if (schemaPks.length > 1) {
118
+ return { pk: null, source: "ambiguous", ambiguous: schemaPks };
119
+ }
120
+ const sample = detectPrimaryKey(table, sampleRows);
121
+ return {
122
+ pk: sample.pk,
123
+ source: "sample",
124
+ candidates: sample.candidates,
125
+ sampleSize: sample.sampleSize,
126
+ };
127
+ }
128
+ function collectSchemaPks(table) {
129
+ // Filter against the actual column list — schema metadata can drift (stale
130
+ // catalog, post-rename) and an unusable PK should fall through cleanly to
131
+ // sample/fail rather than poison downstream object-type creation.
132
+ const colNames = new Set(table.columns.map((c) => c.name));
133
+ if (Array.isArray(table.primaryKeys) && table.primaryKeys.length > 0) {
134
+ return table.primaryKeys.filter((n) => colNames.has(n));
135
+ }
136
+ return table.columns.filter((c) => c.isPrimaryKey === true).map((c) => c.name);
137
+ }
101
138
  /** Format a user-facing error message when PK auto-detection fails. */
102
139
  export function formatPkDetectionError(tableName, result) {
103
140
  const lines = [`Cannot auto-detect primary key for table '${tableName}'.`];
@@ -7,6 +7,7 @@ import { resolveBusinessDomain } from "../config/store.js";
7
7
  import { runKnObjectTypeCommand, runKnRelationTypeCommand, runKnActionTypeCommand, runKnConceptGroupCommand, } from "./bkn-schema.js";
8
8
  import { runKnSubgraphCommand, runKnActionExecutionCommand, runKnActionLogCommand, runKnSearchCommand, runKnRelationTypePathsCommand, runKnResourcesCommand, } from "./bkn-query.js";
9
9
  import { runKnBuildCommand, runKnValidateCommand, runKnPushCommand, runKnPullCommand, runKnCreateFromDsCommand, runKnCreateFromCsvCommand, runKnActionScheduleCommand, runKnJobCommand, } from "./bkn-ops.js";
10
+ import { runKnMetricCommand } from "./bkn-metric.js";
10
11
  // Re-export shared utils for backward compatibility (tests import from bkn.js)
11
12
  export { pollWithBackoff, parseOntologyQueryFlags, parseJsonObject, parseSearchAfterArray, confirmYes, DISPLAY_HINTS, detectPrimaryKey, detectDisplayKey, } from "./bkn-utils.js";
12
13
  // Re-export schema types and parse functions for backward compatibility
@@ -404,6 +405,7 @@ Subcommands:
404
405
  job list|get|tasks|delete <kn-id> ...
405
406
  relation-type-paths <kn-id> '<json>' Query relation type paths between OTs
406
407
  resources List available resources
408
+ metric list|get|create|search|validate|update|delete|query|dry-run <kn-id> ... BKN metrics (definitions + data)
407
409
 
408
410
  Use 'kweaver bkn <subcommand> --help' for subcommand options.`;
409
411
  export async function runKnCommand(args) {
@@ -463,6 +465,8 @@ export async function runKnCommand(args) {
463
465
  return runKnRelationTypePathsCommand(rest);
464
466
  if (subcommand === "resources")
465
467
  return runKnResourcesCommand(rest);
468
+ if (subcommand === "metric")
469
+ return runKnMetricCommand(rest);
466
470
  return Promise.resolve(-1);
467
471
  };
468
472
  try {
@@ -389,7 +389,13 @@ async function printDsConnectOutput(base, dsId) {
389
389
  datasource_id: dsId,
390
390
  tables: tables.map((t) => ({
391
391
  name: t.name,
392
- columns: t.columns.map((c) => ({ name: c.name, type: c.type, comment: c.comment })),
392
+ ...(t.primaryKeys && t.primaryKeys.length > 0 ? { primary_keys: t.primaryKeys } : {}),
393
+ columns: t.columns.map((c) => ({
394
+ name: c.name,
395
+ type: c.type,
396
+ comment: c.comment,
397
+ ...(c.isPrimaryKey ? { is_primary_key: true } : {}),
398
+ })),
393
399
  })),
394
400
  };
395
401
  console.log(JSON.stringify(output, null, 2));
@@ -175,7 +175,7 @@ export function registerChatRoutes(getToken, businessDomain) {
175
175
  return;
176
176
  }
177
177
  const t = await getToken();
178
- const raw = await getTracesByConversation({
178
+ const result = await getTracesByConversation({
179
179
  baseUrl: t.baseUrl,
180
180
  accessToken: t.accessToken,
181
181
  agentId,
@@ -183,7 +183,7 @@ export function registerChatRoutes(getToken, businessDomain) {
183
183
  businessDomain,
184
184
  });
185
185
  res.writeHead(200, { "Content-Type": "application/json; charset=utf-8" });
186
- res.end(raw);
186
+ res.end(JSON.stringify(result));
187
187
  }
188
188
  catch (error) {
189
189
  handleApiError(res, error);
@@ -0,0 +1,72 @@
1
+ export interface ModelGlobalParse {
2
+ rest: string[];
3
+ businessDomain: string;
4
+ mfManagerBaseUrl?: string;
5
+ mfApiBaseUrl?: string;
6
+ pretty: boolean;
7
+ }
8
+ /** Strip global flags; fill default business domain. */
9
+ export declare function parseModelGlobalFlags(args: string[]): ModelGlobalParse;
10
+ /** Sparse flags for model llm edit (after leading model_id). Exported for unit tests. */
11
+ export interface ParsedLlmSparseEditFlags {
12
+ name?: string;
13
+ modelSeries?: string;
14
+ modelType?: string;
15
+ maxModelLen?: number;
16
+ quota?: boolean;
17
+ modelConfigFile?: string;
18
+ upstreamUrl?: string;
19
+ apiModel?: string;
20
+ apiKey?: string;
21
+ apiKeyFile?: string;
22
+ }
23
+ /** Exported for unit tests. */
24
+ export declare function parsedLlmSparseEditHasUpdates(p: ParsedLlmSparseEditFlags): boolean;
25
+ /** Exported for unit tests. */
26
+ export declare function parseLlmSparseEditFlags(tail: string[]): ParsedLlmSparseEditFlags;
27
+ /**
28
+ * Normalize GET /llm/get JSON into a body suitable for POST /llm/edit.
29
+ * Exported for unit tests.
30
+ */
31
+ export declare function llmModelGetToEditBase(raw: unknown): Record<string, unknown>;
32
+ /** Apply sparse llm edit flags onto a normalized record from GET. Exported for unit tests. */
33
+ export declare function mergeLlmEditOntoExistingBase(base: Record<string, unknown>, p: ParsedLlmSparseEditFlags): Promise<Record<string, unknown>>;
34
+ /**
35
+ * Returns registry **model_name** from ``GET /llm/get`` JSON when present. Exported for unit tests.
36
+ */
37
+ export declare function llmGetRecordModelName(raw: unknown): string;
38
+ /** Registry **model_name** from ``GET /small-model/get``. Exported for unit tests. */
39
+ export declare function smallGetRecordModelName(raw: unknown): string;
40
+ export interface ParsedSmallAddFlags {
41
+ name?: string;
42
+ modelType?: "embedding" | "reranker";
43
+ batchSize?: number;
44
+ maxTokens?: number;
45
+ embeddingDim?: number;
46
+ modelConfigFile?: string;
47
+ adapter?: boolean;
48
+ adapterCodeFile?: string;
49
+ bodyFile?: string;
50
+ /** Outbound HTTP API base or full path (stored in model_config.api_url). */
51
+ upstreamUrl?: string;
52
+ /** Third-party model id / deployment name (model_config.api_model). */
53
+ apiModel?: string;
54
+ /** Inline API secret — prefer --api-key-file (shell history risk). */
55
+ apiKey?: string;
56
+ apiKeyFile?: string;
57
+ }
58
+ /** True when sparse CLI flags should perform an edit (excluding --body-file). Exported for unit tests. */
59
+ export declare function parsedSmallFlagsHasEditUpdates(p: ParsedSmallAddFlags): boolean;
60
+ /**
61
+ * Normalize GET /small-model/get JSON into a body suitable for POST /small-model/edit.
62
+ * Exported for unit tests.
63
+ */
64
+ export declare function smallModelGetToEditBase(raw: unknown): Record<string, unknown>;
65
+ /**
66
+ * Apply sparse edit flags onto a normalized small-model record (from GET). Exported for unit tests.
67
+ */
68
+ export declare function mergeSmallEditOntoExistingBase(base: Record<string, unknown>, p: ParsedSmallAddFlags): Promise<Record<string, unknown>>;
69
+ /** Parse small-model add/edit flags from argv tail (after action). Exported for unit tests. */
70
+ export declare function parseSmallAddFlags(tail: string[]): ParsedSmallAddFlags;
71
+ export declare function buildSmallBodyFromFlags(p: ParsedSmallAddFlags, modelId?: string): Promise<Record<string, unknown>>;
72
+ export declare function runModelCommand(args: string[]): Promise<number>;