@juanbenjumea/pi-agy 0.7.7 → 0.7.8
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/CHANGELOG.md +6 -0
- package/README.md +5 -6
- package/extensions/command-info.ts +113 -0
- package/extensions/command-run.ts +199 -0
- package/extensions/commands.ts +20 -317
- package/extensions/index.ts +5 -566
- package/extensions/lib/args.ts +20 -6
- package/extensions/lib/context.ts +12 -0
- package/extensions/lib/preflight.ts +42 -15
- package/extensions/lib/provider-prompt.ts +260 -0
- package/extensions/lib/provider-tools.ts +82 -0
- package/extensions/lib/provider.ts +33 -345
- package/extensions/lib/verify.ts +30 -1
- package/extensions/tools.ts +558 -0
- package/package.json +6 -2
- package/skills/agy/SKILL.md +11 -94
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import * as path from "node:path";
|
|
2
|
+
|
|
1
3
|
import {
|
|
2
4
|
checkAgyConnectivity,
|
|
3
5
|
checkAgyHealth,
|
|
@@ -8,9 +10,32 @@ import {
|
|
|
8
10
|
const TTL_MS = 5 * 60 * 1000;
|
|
9
11
|
const USAGE_TTL_MS = 60 * 1000;
|
|
10
12
|
const USAGE_FAILURE_TTL_MS = 5 * 1000;
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
|
|
14
|
+
interface PreflightEntry {
|
|
15
|
+
healthAt: number;
|
|
16
|
+
usageAt: number;
|
|
17
|
+
usage?: AgyUsageSnapshot;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Per-directory: health/quota answers depend on the working directory, so a
|
|
21
|
+
// single global entry would serve dir B dir A's snapshot. Bounded so long
|
|
22
|
+
// sessions across many checkouts cannot grow the map without limit.
|
|
23
|
+
const MAX_CACHED_DIRS = 32;
|
|
24
|
+
const entries = new Map<string, PreflightEntry>();
|
|
25
|
+
|
|
26
|
+
function entryFor(cwd: string): PreflightEntry {
|
|
27
|
+
const key = path.resolve(cwd);
|
|
28
|
+
let entry = entries.get(key);
|
|
29
|
+
if (!entry) {
|
|
30
|
+
entry = { healthAt: 0, usageAt: 0, usage: undefined };
|
|
31
|
+
entries.set(key, entry);
|
|
32
|
+
if (entries.size > MAX_CACHED_DIRS) {
|
|
33
|
+
const oldest = entries.keys().next();
|
|
34
|
+
if (!oldest.done) entries.delete(oldest.value);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return entry;
|
|
38
|
+
}
|
|
14
39
|
|
|
15
40
|
/** Run cached health/model checks and a shorter-lived model quota refresh. */
|
|
16
41
|
export async function runPreflight(
|
|
@@ -18,11 +43,13 @@ export async function runPreflight(
|
|
|
18
43
|
signal?: AbortSignal,
|
|
19
44
|
timeoutMs?: number,
|
|
20
45
|
): Promise<AgyUsageSnapshot | undefined> {
|
|
46
|
+
const entry = entryFor(cwd);
|
|
21
47
|
const now = Date.now();
|
|
22
|
-
const healthFresh =
|
|
23
|
-
const usageTtl =
|
|
24
|
-
|
|
25
|
-
|
|
48
|
+
const healthFresh = entry.healthAt > 0 && now - entry.healthAt < TTL_MS;
|
|
49
|
+
const usageTtl =
|
|
50
|
+
entry.usage?.error || !entry.usage ? USAGE_FAILURE_TTL_MS : USAGE_TTL_MS;
|
|
51
|
+
const usageFresh = entry.usageAt > 0 && now - entry.usageAt < usageTtl;
|
|
52
|
+
if (healthFresh && usageFresh) return entry.usage;
|
|
26
53
|
if (timeoutMs !== undefined && timeoutMs <= 0) {
|
|
27
54
|
throw new Error("agy timed out before preflight");
|
|
28
55
|
}
|
|
@@ -30,14 +57,16 @@ export async function runPreflight(
|
|
|
30
57
|
const health = healthFresh
|
|
31
58
|
? Promise.resolve()
|
|
32
59
|
: runHealthWithRetry(cwd, signal, timeoutMs).then(() => undefined);
|
|
33
|
-
const usage = usageFresh
|
|
60
|
+
const usage = usageFresh
|
|
61
|
+
? Promise.resolve(entry.usage)
|
|
62
|
+
: checkAgyUsage(cwd, signal, timeoutMs);
|
|
34
63
|
const [, refreshedUsage] = await Promise.all([health, usage]);
|
|
35
|
-
if (!healthFresh)
|
|
64
|
+
if (!healthFresh) entry.healthAt = Date.now();
|
|
36
65
|
if (!usageFresh) {
|
|
37
|
-
|
|
38
|
-
|
|
66
|
+
entry.usage = refreshedUsage;
|
|
67
|
+
entry.usageAt = Date.now();
|
|
39
68
|
}
|
|
40
|
-
return
|
|
69
|
+
return entry.usage;
|
|
41
70
|
}
|
|
42
71
|
|
|
43
72
|
/**
|
|
@@ -73,7 +102,5 @@ async function runHealthWithRetry(
|
|
|
73
102
|
|
|
74
103
|
/** Test helper — reset the in-process cache. */
|
|
75
104
|
export function resetPreflightCache(): void {
|
|
76
|
-
|
|
77
|
-
cachedUsageAt = 0;
|
|
78
|
-
cachedUsage = undefined;
|
|
105
|
+
entries.clear();
|
|
79
106
|
}
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { chmod, mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
3
|
+
import * as os from "node:os";
|
|
4
|
+
import * as path from "node:path";
|
|
5
|
+
|
|
6
|
+
/** Bounds for transcript rendering into a single turn prompt. */
|
|
7
|
+
const MAX_HISTORY_CHARS = 24_000;
|
|
8
|
+
const MAX_SYSTEM_CHARS = 8_000;
|
|
9
|
+
const MAX_MESSAGE_CHARS = 6_000;
|
|
10
|
+
|
|
11
|
+
/** Minimal structural transcript message (subset of Pi's Message). */
|
|
12
|
+
export interface ProviderTranscriptMessage {
|
|
13
|
+
role: string;
|
|
14
|
+
content?: unknown;
|
|
15
|
+
toolName?: string;
|
|
16
|
+
toolCallId?: string;
|
|
17
|
+
isError?: boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function textOfContent(content: unknown, imagePath?: (block: object) => string | undefined): string {
|
|
21
|
+
if (typeof content === "string") return content;
|
|
22
|
+
if (Array.isArray(content)) {
|
|
23
|
+
const parts: string[] = [];
|
|
24
|
+
for (const block of content) {
|
|
25
|
+
if (typeof block === "string") {
|
|
26
|
+
parts.push(block);
|
|
27
|
+
} else if (block && typeof block === "object") {
|
|
28
|
+
const record = block as Record<string, unknown>;
|
|
29
|
+
if (record.type === "text" && typeof record.text === "string") {
|
|
30
|
+
parts.push(record.text);
|
|
31
|
+
} else if (record.type === "image") {
|
|
32
|
+
const staged = imagePath?.(block);
|
|
33
|
+
parts.push(
|
|
34
|
+
staged ?? "[image omitted: agy provider turns are text-only]",
|
|
35
|
+
);
|
|
36
|
+
} else if (record.type === "toolCall") {
|
|
37
|
+
parts.push(`[tool call: ${String(record.name ?? "unknown")}]`);
|
|
38
|
+
} else if (record.type === "thinking" && typeof record.thinking === "string") {
|
|
39
|
+
parts.push(record.thinking);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return parts.join("\n");
|
|
44
|
+
}
|
|
45
|
+
return "";
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function clip(text: string, max: number): string {
|
|
49
|
+
if (text.length <= max) return text;
|
|
50
|
+
return text.slice(0, max - 1) + "…";
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface PromptImageBlock {
|
|
54
|
+
/** Identity of the transcript block (lookup key, never serialized). */
|
|
55
|
+
block: object;
|
|
56
|
+
data: string;
|
|
57
|
+
mimeType: string;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface StagedImages {
|
|
61
|
+
/** Per-turn subdir, undefined when nothing stageable. */
|
|
62
|
+
dir: string | undefined;
|
|
63
|
+
/** Block identity → absolute path (order-independent lookup). */
|
|
64
|
+
paths: Map<object, string>;
|
|
65
|
+
/** Remove this staging (idempotent, never throws). */
|
|
66
|
+
cleanup: () => Promise<void>;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Cap staged images per invocation (count and per-file size). */
|
|
70
|
+
export const MAX_STAGED_IMAGES = 8;
|
|
71
|
+
export const MAX_IMAGE_BYTES = 8 * 1024 * 1024;
|
|
72
|
+
|
|
73
|
+
const IMAGE_EXTENSIONS: Record<string, string> = {
|
|
74
|
+
"image/png": "png",
|
|
75
|
+
"image/jpeg": "jpg",
|
|
76
|
+
"image/webp": "webp",
|
|
77
|
+
"image/gif": "gif",
|
|
78
|
+
"image/bmp": "bmp",
|
|
79
|
+
"image/svg+xml": "svg",
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
export function imageExtension(mimeType: string): string {
|
|
83
|
+
return IMAGE_EXTENSIONS[mimeType.toLowerCase()] ?? "bin";
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Collect image blocks in transcript order, optionally only one tool result. */
|
|
87
|
+
export function collectImageBlocks(
|
|
88
|
+
messages: ProviderTranscriptMessage[],
|
|
89
|
+
onlyToolCallId?: string,
|
|
90
|
+
): PromptImageBlock[] {
|
|
91
|
+
const blocks: PromptImageBlock[] = [];
|
|
92
|
+
for (const message of messages) {
|
|
93
|
+
if (onlyToolCallId !== undefined) {
|
|
94
|
+
if (message.role !== "toolResult" || message.toolCallId !== onlyToolCallId) continue;
|
|
95
|
+
}
|
|
96
|
+
const content = message.content;
|
|
97
|
+
if (!Array.isArray(content)) continue;
|
|
98
|
+
for (const block of content) {
|
|
99
|
+
if (typeof block !== "object" || block === null) continue;
|
|
100
|
+
const record = block as Record<string, unknown>;
|
|
101
|
+
if (record.type !== "image") continue;
|
|
102
|
+
if (typeof record.data !== "string" || typeof record.mimeType !== "string") continue;
|
|
103
|
+
blocks.push({ block, data: record.data, mimeType: record.mimeType });
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return blocks;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Strict base64 (Node's decoder silently strips non-alphabet chars). */
|
|
110
|
+
const STRICT_BASE64_RE = /^[A-Za-z0-9+/]*={0,2}$/;
|
|
111
|
+
|
|
112
|
+
function decodeImageData(data: string): Buffer | undefined {
|
|
113
|
+
if (typeof data !== "string" || data.length === 0 || data.length % 4 !== 0) return undefined;
|
|
114
|
+
if (!STRICT_BASE64_RE.test(data)) return undefined;
|
|
115
|
+
try {
|
|
116
|
+
const bytes = Buffer.from(data, "base64");
|
|
117
|
+
if (bytes.length === 0 || bytes.length > MAX_IMAGE_BYTES) return undefined;
|
|
118
|
+
return bytes;
|
|
119
|
+
} catch {
|
|
120
|
+
return undefined;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** Process-stable staging root (0700): per-turn subdirs, one --add-dir. */
|
|
125
|
+
let imageStagingRoot: string | undefined;
|
|
126
|
+
|
|
127
|
+
/** Live staging dirs (teardown sweep; per-turn cleanup disposes handles). */
|
|
128
|
+
const liveStagingDirs = new Set<string>();
|
|
129
|
+
|
|
130
|
+
export function getLiveImageStagingCount(): number {
|
|
131
|
+
return liveStagingDirs.size;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Resolve (creating once) the process staging root for agy --add-dir. */
|
|
135
|
+
export async function ensureImageStagingRoot(): Promise<string> {
|
|
136
|
+
if (!imageStagingRoot) {
|
|
137
|
+
// mkdtemp (not join+mkdir): atomic, unique, 0700 — no pre-creation or
|
|
138
|
+
// symlink race between check and creation.
|
|
139
|
+
imageStagingRoot = await mkdtemp(path.join(os.tmpdir(), "pi-agy-images-"));
|
|
140
|
+
try {
|
|
141
|
+
await chmod(imageStagingRoot, 0o700);
|
|
142
|
+
} catch {
|
|
143
|
+
// mkdtemp already guarantees 0700; best effort.
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return imageStagingRoot;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Materialize image blocks as files agy's tools can open. Newest transcript
|
|
151
|
+
* images win (oldest starve first) so a fresh attachment is never dropped
|
|
152
|
+
* behind history. Failures degrade to omitted notes, never to turn
|
|
153
|
+
* failures. Each call owns its subdir; dispose via the returned cleanup.
|
|
154
|
+
*/
|
|
155
|
+
export async function stageImages(blocks: PromptImageBlock[]): Promise<StagedImages> {
|
|
156
|
+
const paths = new Map<object, string>();
|
|
157
|
+
const noop = async (): Promise<void> => undefined;
|
|
158
|
+
const bounded = blocks.slice(-MAX_STAGED_IMAGES);
|
|
159
|
+
if (bounded.length === 0) return { dir: undefined, paths, cleanup: noop };
|
|
160
|
+
let root: string;
|
|
161
|
+
try {
|
|
162
|
+
root = await ensureImageStagingRoot();
|
|
163
|
+
} catch {
|
|
164
|
+
return { dir: undefined, paths, cleanup: noop };
|
|
165
|
+
}
|
|
166
|
+
const dir = path.join(root, randomBytes(8).toString("hex"));
|
|
167
|
+
try {
|
|
168
|
+
await mkdir(dir, { mode: 0o700 });
|
|
169
|
+
} catch {
|
|
170
|
+
return { dir: undefined, paths, cleanup: noop };
|
|
171
|
+
}
|
|
172
|
+
liveStagingDirs.add(dir);
|
|
173
|
+
let cleaned = false;
|
|
174
|
+
const cleanup = async (): Promise<void> => {
|
|
175
|
+
if (cleaned) return;
|
|
176
|
+
cleaned = true;
|
|
177
|
+
liveStagingDirs.delete(dir);
|
|
178
|
+
await rm(dir, { recursive: true, force: true }).catch(() => undefined);
|
|
179
|
+
};
|
|
180
|
+
for (let index = 0; index < bounded.length; index++) {
|
|
181
|
+
const block = bounded[index];
|
|
182
|
+
const bytes = decodeImageData(block.data);
|
|
183
|
+
if (!bytes) continue;
|
|
184
|
+
const file = path.join(dir, `image-${index}.${imageExtension(block.mimeType)}`);
|
|
185
|
+
try {
|
|
186
|
+
await writeFile(file, bytes, { mode: 0o600 });
|
|
187
|
+
await chmod(file, 0o600);
|
|
188
|
+
} catch {
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
paths.set(block.block, file);
|
|
192
|
+
}
|
|
193
|
+
if (paths.size === 0) {
|
|
194
|
+
await cleanup();
|
|
195
|
+
return { dir: undefined, paths, cleanup: noop };
|
|
196
|
+
}
|
|
197
|
+
return { dir, paths, cleanup };
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** Remove every staging dir and the root (teardown, tests). */
|
|
201
|
+
export async function clearImageStaging(): Promise<void> {
|
|
202
|
+
const dirs = [...liveStagingDirs];
|
|
203
|
+
liveStagingDirs.clear();
|
|
204
|
+
await Promise.all(dirs.map((dir) => rm(dir, { recursive: true, force: true }).catch(() => undefined)));
|
|
205
|
+
if (imageStagingRoot) {
|
|
206
|
+
const root = imageStagingRoot;
|
|
207
|
+
imageStagingRoot = undefined;
|
|
208
|
+
await rm(root, { recursive: true, force: true }).catch(() => undefined);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Render Pi transcript messages into a single agy `-p` prompt. The latest
|
|
214
|
+
* user message is authoritative; earlier history is bounded reference. Pi
|
|
215
|
+
* tool definitions are intentionally NOT translated — agy runs its own tool
|
|
216
|
+
* loop (see the module wall note).
|
|
217
|
+
*/
|
|
218
|
+
export function renderProviderPrompt(
|
|
219
|
+
messages: ProviderTranscriptMessage[],
|
|
220
|
+
systemPrompt?: string,
|
|
221
|
+
imagePaths?: Map<object, string>,
|
|
222
|
+
): string {
|
|
223
|
+
const imageRef = (block: object): string | undefined => {
|
|
224
|
+
const file = imagePaths?.get(block);
|
|
225
|
+
return file
|
|
226
|
+
? `[attached image saved at ${file} — open it with your file tools to view it]`
|
|
227
|
+
: undefined;
|
|
228
|
+
};
|
|
229
|
+
const lines: string[] = [];
|
|
230
|
+
if (systemPrompt?.trim()) {
|
|
231
|
+
lines.push("System instructions:", clip(systemPrompt.trim(), MAX_SYSTEM_CHARS), "");
|
|
232
|
+
}
|
|
233
|
+
const history = messages.slice(0, -1);
|
|
234
|
+
const rendered: string[] = [];
|
|
235
|
+
let budget = MAX_HISTORY_CHARS;
|
|
236
|
+
for (let i = history.length - 1; i >= 0 && budget > 0; i--) {
|
|
237
|
+
const message = history[i];
|
|
238
|
+
const text = clip(textOfContent(message.content, imageRef).trim(), MAX_MESSAGE_CHARS);
|
|
239
|
+
if (!text) continue;
|
|
240
|
+
let label: string;
|
|
241
|
+
if (message.role === "user") label = "User";
|
|
242
|
+
else if (message.role === "assistant") label = "Assistant (previous turn)";
|
|
243
|
+
else if (message.role === "toolResult") {
|
|
244
|
+
label = `Tool result (${message.toolName ?? "tool"}${message.isError ? ", error" : ""})`;
|
|
245
|
+
} else if (message.role === "system") label = "System";
|
|
246
|
+
else label = message.role;
|
|
247
|
+
const entry = `${label}: ${text}`;
|
|
248
|
+
if (entry.length > budget) break;
|
|
249
|
+
budget -= entry.length;
|
|
250
|
+
rendered.unshift(entry);
|
|
251
|
+
}
|
|
252
|
+
if (rendered.length > 0) {
|
|
253
|
+
lines.push("Conversation history (reference only; the current message is authoritative):", ...rendered, "");
|
|
254
|
+
}
|
|
255
|
+
const last = messages[messages.length - 1];
|
|
256
|
+
const current = last ? textOfContent(last.content, imageRef).trim() : "";
|
|
257
|
+
const currentLabel = last && last.role !== "user" ? `Current message (${last.role}):` : "Current message:";
|
|
258
|
+
lines.push(currentLabel, current || "(empty — respond with a brief acknowledgement)");
|
|
259
|
+
return lines.join("\n");
|
|
260
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { toSafeBridgeName, type BridgeToolDef } from "./provider-mcp.js";
|
|
2
|
+
|
|
3
|
+
/** Pi tool source shape (structural subset of Pi's ToolInfo). */
|
|
4
|
+
export interface ProviderToolSource {
|
|
5
|
+
name: string;
|
|
6
|
+
description?: string;
|
|
7
|
+
parameters?: unknown;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Tools that must never be bridged back into agy: self-delegation loops
|
|
12
|
+
* burn quota and nest conversations, they are not capabilities agy lacks.
|
|
13
|
+
*/
|
|
14
|
+
const BRIDGE_EXCLUDED_TOOL_NAMES = new Set(["askantigravity"]);
|
|
15
|
+
const MAX_BRIDGED_TOOLS = 100;
|
|
16
|
+
|
|
17
|
+
function isBridgableToolName(name: string): boolean {
|
|
18
|
+
const lowered = name.toLowerCase();
|
|
19
|
+
if (lowered === "agy" || lowered.startsWith("agy_") || lowered.startsWith("agy-")) return false;
|
|
20
|
+
return !BRIDGE_EXCLUDED_TOOL_NAMES.has(lowered);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function jsonClone(value: unknown): Record<string, unknown> | undefined {
|
|
24
|
+
try {
|
|
25
|
+
const cloned: unknown = JSON.parse(JSON.stringify(value));
|
|
26
|
+
if (cloned && typeof cloned === "object" && !Array.isArray(cloned)) {
|
|
27
|
+
return cloned as Record<string, unknown>;
|
|
28
|
+
}
|
|
29
|
+
return undefined;
|
|
30
|
+
} catch {
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Project Pi tools into bridge definitions: exclude self-delegation,
|
|
37
|
+
* sanitize MCP names, drop unserializable schemas. First-seen wins on
|
|
38
|
+
* collisions so output is deterministic.
|
|
39
|
+
*/
|
|
40
|
+
export function projectBridgeTools(sources: ProviderToolSource[]): BridgeToolDef[] {
|
|
41
|
+
const defs: BridgeToolDef[] = [];
|
|
42
|
+
const seenNames = new Set<string>();
|
|
43
|
+
const seenPiNames = new Set<string>();
|
|
44
|
+
for (const source of sources) {
|
|
45
|
+
if (defs.length >= MAX_BRIDGED_TOOLS) break;
|
|
46
|
+
if (typeof source.name !== "string" || !isBridgableToolName(source.name)) continue;
|
|
47
|
+
if (seenPiNames.has(source.name)) continue;
|
|
48
|
+
const name = toSafeBridgeName(source.name);
|
|
49
|
+
if (seenNames.has(name)) continue;
|
|
50
|
+
// MCP requires an object root; anything else degrades to no-arg form
|
|
51
|
+
// instead of shipping a schema agy cannot validate against.
|
|
52
|
+
const cloned = jsonClone(source.parameters);
|
|
53
|
+
const inputSchema =
|
|
54
|
+
cloned && (cloned as { type?: unknown }).type === "object" && typeof (cloned as { properties?: unknown }).properties === "object"
|
|
55
|
+
? (cloned as Record<string, unknown>)
|
|
56
|
+
: { type: "object", properties: {} };
|
|
57
|
+
seenPiNames.add(source.name);
|
|
58
|
+
seenNames.add(name);
|
|
59
|
+
defs.push({
|
|
60
|
+
name,
|
|
61
|
+
piName: source.name,
|
|
62
|
+
description:
|
|
63
|
+
typeof source.description === "string" && source.description.trim()
|
|
64
|
+
? source.description.trim().slice(0, 500)
|
|
65
|
+
: `Pi tool ${source.name}`,
|
|
66
|
+
inputSchema,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
return defs;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Fingerprint the bridged catalog: changes recycle the driver (fresh ListTools). */
|
|
73
|
+
export function bridgeToolsFingerprint(tools: BridgeToolDef[]): string {
|
|
74
|
+
const canonical = JSON.stringify(
|
|
75
|
+
tools.map((tool) => [tool.name, tool.piName, tool.description, tool.inputSchema]),
|
|
76
|
+
);
|
|
77
|
+
let hash = 5381;
|
|
78
|
+
for (let i = 0; i < canonical.length; i++) {
|
|
79
|
+
hash = ((hash << 5) + hash + canonical.charCodeAt(i)) | 0;
|
|
80
|
+
}
|
|
81
|
+
return `n${tools.length}-h${(hash >>> 0).toString(16)}`;
|
|
82
|
+
}
|