@kwirthmagnify/kwirth-common-ai 0.5.27 → 0.5.28

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/dist/back.d.ts CHANGED
@@ -7,12 +7,19 @@ export declare const loadModels: (providers: ILlmProvider[], log: IBackChannelOb
7
7
  export { generateText, Output, stepCountIs, tool } from 'ai';
8
8
  export { z } from 'zod';
9
9
  export declare const zodFromExample: (example: Record<string, unknown>) => z.ZodObject<Record<string, z.ZodTypeAny>>;
10
+ export interface ISourceRepoCred {
11
+ host: string;
12
+ type: 'github' | 'gitlab';
13
+ token: string;
14
+ apiBaseUrl?: string;
15
+ }
10
16
  export interface IToolContext {
11
17
  origin: string;
12
18
  nodes: Map<string, any>;
13
19
  clusterInfo: any;
14
20
  clusterMetrics: any[];
15
21
  clusterEvents?: any[];
22
+ sourceRepos?: ISourceRepoCred[];
16
23
  trace: (toolName: string, args: Record<string, unknown>) => void;
17
24
  }
18
25
  export declare enum EToolEffect {
@@ -369,6 +376,10 @@ export declare const tools: {
369
376
  reason: any;
370
377
  message: any;
371
378
  controlledBy: any;
379
+ source: {
380
+ repo: any;
381
+ revision: any;
382
+ } | undefined;
372
383
  conditions: any;
373
384
  containers: any;
374
385
  initContainers: any;
@@ -387,6 +398,7 @@ export declare const tools: {
387
398
  reason?: undefined;
388
399
  message?: undefined;
389
400
  controlledBy?: undefined;
401
+ source?: undefined;
390
402
  conditions?: undefined;
391
403
  containers?: undefined;
392
404
  initContainers?: undefined;
@@ -416,6 +428,25 @@ export declare const tools: {
416
428
  revisionCount?: undefined;
417
429
  revisions?: undefined;
418
430
  }>;
431
+ readonly get_source_file: import("ai").Tool<{
432
+ repo: string;
433
+ ref: string;
434
+ path: string;
435
+ }, {
436
+ repo: string;
437
+ ref: string;
438
+ path: string;
439
+ truncated: boolean;
440
+ content: string;
441
+ error?: undefined;
442
+ } | {
443
+ error: any;
444
+ repo?: undefined;
445
+ ref?: undefined;
446
+ path?: undefined;
447
+ truncated?: undefined;
448
+ content?: undefined;
449
+ }>;
419
450
  };
420
451
  export declare const toolInfoList: IToolInfo[];
421
452
  export interface IAgentRunResult {
package/dist/back.js CHANGED
@@ -174,6 +174,37 @@ const zodFromExample = (example) => {
174
174
  return zod_1.z.object(shape);
175
175
  };
176
176
  exports.zodFromExample = zodFromExample;
177
+ // Parse a repo reference (full URL or 'owner/name'; bare form assumes github.com). Owner keeps GitLab subgroups.
178
+ const parseRepoRef = (repo) => {
179
+ let host = 'github.com';
180
+ let pathPart = repo.trim();
181
+ const m = pathPart.match(/^(?:git@|https?:\/\/)?([^/:]+)[/:](.+)$/);
182
+ if (m && m[1].includes('.')) {
183
+ host = m[1];
184
+ pathPart = m[2];
185
+ }
186
+ const projectPath = pathPart.replace(/\.git$/, '').replace(/^\/+|\/+$/g, '');
187
+ return { host, projectPath };
188
+ };
189
+ // Fetch one file's raw content at a ref from GitHub or GitLab (cloud or on-prem), using the matching cred.
190
+ const fetchSourceFile = async (cred, projectPath, ref, path) => {
191
+ var _a, _b;
192
+ const cleanPath = path.replace(/^\/+/, '');
193
+ if (cred.type === 'github') {
194
+ const base = (_a = cred.apiBaseUrl) !== null && _a !== void 0 ? _a : 'https://api.github.com';
195
+ const url = `${base}/repos/${projectPath}/contents/${cleanPath.split('/').map(encodeURIComponent).join('/')}?ref=${encodeURIComponent(ref)}`;
196
+ const resp = await fetch(url, { headers: { Authorization: `Bearer ${cred.token}`, Accept: 'application/vnd.github.raw', 'User-Agent': 'kwirth-agora' } });
197
+ if (!resp.ok)
198
+ throw new Error(`GitHub ${resp.status} for ${projectPath}/${cleanPath}@${ref}`);
199
+ return await resp.text();
200
+ }
201
+ const base = (_b = cred.apiBaseUrl) !== null && _b !== void 0 ? _b : `https://${cred.host}/api/v4`;
202
+ const url = `${base}/projects/${encodeURIComponent(projectPath)}/repository/files/${encodeURIComponent(cleanPath)}/raw?ref=${encodeURIComponent(ref)}`;
203
+ const resp = await fetch(url, { headers: { 'PRIVATE-TOKEN': cred.token, 'User-Agent': 'kwirth-agora' } });
204
+ if (!resp.ok)
205
+ throw new Error(`GitLab ${resp.status} for ${projectPath}/${cleanPath}@${ref}`);
206
+ return await resp.text();
207
+ };
177
208
  // Effect of a tool: READ (safe, informational) or WRITE (has side effects on the cluster).
178
209
  // Dual purpose: hints the LLM, and gates authorization (Agora/readOnly filter out WRITE).
179
210
  var EToolEffect;
@@ -786,7 +817,7 @@ exports.tools = {
786
817
  name: zod_1.z.string().describe('Name of the pod')
787
818
  }),
788
819
  execute: async ({ namespace, name }) => {
789
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
820
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
790
821
  ctx().trace('describe_pod', { namespace, name });
791
822
  try {
792
823
  const c = ctx().clusterInfo;
@@ -813,24 +844,30 @@ exports.tools = {
813
844
  const rsOwner = ((_f = (_e = rs.metadata) === null || _e === void 0 ? void 0 : _e.ownerReferences) !== null && _f !== void 0 ? _f : [])[0];
814
845
  controlledBy = rsOwner ? { kind: rsOwner.kind, name: rsOwner.name } : { kind: 'ReplicaSet', name: podOwner.name };
815
846
  }
816
- catch (_m) {
847
+ catch (_r) {
817
848
  controlledBy = { kind: 'ReplicaSet', name: podOwner.name };
818
849
  }
819
850
  }
820
851
  else if (podOwner) {
821
852
  controlledBy = { kind: podOwner.kind, name: podOwner.name }; // StatefulSet/DaemonSet/Job own pods directly
822
853
  }
854
+ // Source provenance: OCI standard annotations (org.opencontainers.image.*) with a kwirth.io fallback →
855
+ // the repo + commit the image was built from, so the bot can read the actual source (get_source_file).
856
+ const ann = (_h = (_g = pod.metadata) === null || _g === void 0 ? void 0 : _g.annotations) !== null && _h !== void 0 ? _h : {};
857
+ const sourceRepo = (_j = ann['org.opencontainers.image.source']) !== null && _j !== void 0 ? _j : ann['kwirth.io/source-repo'];
858
+ const source = sourceRepo ? { repo: sourceRepo, revision: (_k = ann['org.opencontainers.image.revision']) !== null && _k !== void 0 ? _k : ann['kwirth.io/source-ref'] } : undefined;
823
859
  return {
824
860
  name, namespace, phase: status.phase, node: spec.nodeName, startTime: status.startTime, reason: status.reason, message: status.message,
825
861
  controlledBy, // e.g. { kind: 'Deployment', name: 'montag-agent' } → feed to get_rollout_history
826
- conditions: ((_g = status.conditions) !== null && _g !== void 0 ? _g : []).map((c) => ({ type: c.type, status: c.status, reason: c.reason })),
827
- containers: ((_h = status.containerStatuses) !== null && _h !== void 0 ? _h : []).map(containerStatus),
828
- initContainers: ((_j = status.initContainerStatuses) !== null && _j !== void 0 ? _j : []).map(containerStatus),
829
- spec: { containers: ((_k = spec.containers) !== null && _k !== void 0 ? _k : []).map(specContainer), restartPolicy: spec.restartPolicy }
862
+ source, // { repo, revision } from image annotations → feed to get_source_file
863
+ conditions: ((_l = status.conditions) !== null && _l !== void 0 ? _l : []).map((c) => ({ type: c.type, status: c.status, reason: c.reason })),
864
+ containers: ((_m = status.containerStatuses) !== null && _m !== void 0 ? _m : []).map(containerStatus),
865
+ initContainers: ((_o = status.initContainerStatuses) !== null && _o !== void 0 ? _o : []).map(containerStatus),
866
+ spec: { containers: ((_p = spec.containers) !== null && _p !== void 0 ? _p : []).map(specContainer), restartPolicy: spec.restartPolicy }
830
867
  };
831
868
  }
832
869
  catch (err) {
833
- return { error: (_l = err.message) !== null && _l !== void 0 ? _l : String(err) };
870
+ return { error: (_q = err.message) !== null && _q !== void 0 ? _q : String(err) };
834
871
  }
835
872
  }
836
873
  }),
@@ -902,6 +939,30 @@ exports.tools = {
902
939
  }
903
940
  }
904
941
  }),
942
+ get_source_file: (0, ai_1.tool)({
943
+ description: 'Fetches the contents of a single source file from a Git repository (GitHub or GitLab) at a specific ref. Use to inspect the actual source behind a crash: read the stack-trace file:line from the logs, then fetch that file at the image build revision (describe_pod.source). Call repeatedly to follow the trace across files until you find the bug.',
944
+ inputSchema: zod_1.z.object({
945
+ repo: zod_1.z.string().describe('Repository URL or owner/name (e.g. https://github.com/org/app or org/app) — from describe_pod.source.repo'),
946
+ ref: zod_1.z.string().describe('Commit SHA, branch or tag — use the build revision (describe_pod.source.revision) so you read the code actually running'),
947
+ path: zod_1.z.string().describe('File path within the repo (e.g. src/regex/loader.go)')
948
+ }),
949
+ execute: async ({ repo, ref, path }) => {
950
+ var _a, _b;
951
+ ctx().trace('get_source_file', { repo, ref, path });
952
+ try {
953
+ const { host, projectPath } = parseRepoRef(repo);
954
+ const cred = ((_a = ctx().sourceRepos) !== null && _a !== void 0 ? _a : []).find(r => r.host === host);
955
+ if (!cred)
956
+ return { error: `No source-repo credentials configured for host '${host}'. Add it in Agora → Source repos.` };
957
+ const text = await fetchSourceFile(cred, projectPath, ref, path);
958
+ const truncated = text.length > 20000;
959
+ return { repo: projectPath, ref, path, truncated, content: truncated ? text.slice(0, 20000) : text };
960
+ }
961
+ catch (err) {
962
+ return { error: (_b = err.message) !== null && _b !== void 0 ? _b : String(err) };
963
+ }
964
+ }
965
+ }),
905
966
  };
906
967
  exports.toolInfoList = [
907
968
  { name: 'list_namespaces', effect: EToolEffect.READ, description: 'Lists all namespaces in the cluster with their status and labels.' },
@@ -936,6 +997,7 @@ exports.toolInfoList = [
936
997
  { name: 'get_pod_yaml', effect: EToolEffect.READ, description: 'Full Pod manifest (kubectl get pod -o yaml): spec (env, volumes, resources, probes) and status.' },
937
998
  { name: 'get_deployment_yaml', effect: EToolEffect.READ, description: 'Full Deployment manifest (kubectl get deployment -o yaml): pod template (image, env, resources, probes) and strategy.' },
938
999
  { name: 'get_rollout_history', effect: EToolEffect.READ, description: 'Rollout revisions of a Deployment (via ReplicaSets): image + template per revision, to see what changed (new image/env/resource) that may have broken the pods.' },
1000
+ { name: 'get_source_file', effect: EToolEffect.READ, description: 'Fetches a file from a Git repo (GitHub/GitLab) at a ref — inspect the source behind a crash by following the stack trace to the offending file:line.' },
939
1001
  ];
940
1002
  // Resolves the tool names an agent may use: autoTools = full catalog, otherwise its own list
941
1003
  // (intersected with the catalog); readOnly filters out WRITE-effect tools. Pure (no LLM) → unit-testable.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kwirthmagnify/kwirth-common-ai",
3
- "version": "0.5.27",
3
+ "version": "0.5.28",
4
4
  "description": "Shared AI/LLM types and utilities for Kwirth AI plugins",
5
5
  "scripts": {
6
6
  "build": "del .\\dist\\* /s /q 2>nul & tsc",