@eduardo191labs/opencode-free-pi 0.3.21 → 0.4.0
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/README.md +64 -0
- package/dist/plugin.js +36 -5
- package/dist/session/hooks.d.ts +3 -1
- package/dist/session/hooks.js +28 -1
- package/dist/state.d.ts +1 -0
- package/dist/state.js +1 -0
- package/dist/tools/account.d.ts +10 -0
- package/dist/tools/account.js +103 -0
- package/dist/tools/api.d.ts +54 -0
- package/dist/tools/api.js +120 -0
- package/dist/tools/fusion.d.ts +45 -0
- package/dist/tools/fusion.js +162 -0
- package/dist/tools/image-generate.d.ts +13 -0
- package/dist/tools/image-generate.js +143 -0
- package/dist/tools/usage.d.ts +12 -0
- package/dist/tools/usage.js +125 -0
- package/dist/tools/vision-queue.d.ts +19 -0
- package/dist/tools/vision-queue.js +149 -0
- package/dist/tools/vision.js +150 -53
- package/dist/tools/web.d.ts +12 -0
- package/dist/tools/web.js +165 -0
- package/dist/types.d.ts +4 -0
- package/package.json +2 -2
- package/src/plugin.ts +50 -5
- package/src/session/hooks.ts +39 -0
- package/src/state.ts +2 -0
- package/src/tools/account.ts +135 -0
- package/src/tools/api.ts +178 -0
- package/src/tools/fusion.ts +221 -0
- package/src/tools/image-generate.ts +195 -0
- package/src/tools/usage.ts +163 -0
- package/src/tools/vision-queue.ts +161 -0
- package/src/tools/vision.ts +180 -56
- package/src/tools/web.ts +212 -0
- package/src/types.ts +1 -0
package/src/tools/vision.ts
CHANGED
|
@@ -12,12 +12,17 @@ import { resolveToken } from "../auth/token.js";
|
|
|
12
12
|
import type { Deps } from "../deps.js";
|
|
13
13
|
import { isFreePiSession } from "../session/tools.js";
|
|
14
14
|
import { resolveBaseUrl } from "../utils/urls.js";
|
|
15
|
-
import { loadImage, prepareImage } from "./image.js";
|
|
15
|
+
import { loadImage, prepareImage, type VisionMime } from "./image.js";
|
|
16
|
+
import { withVisionSlot } from "./vision-queue.js";
|
|
16
17
|
|
|
17
18
|
type Registration = { dispose: () => Promise<void> };
|
|
18
19
|
|
|
19
20
|
const TOOL_NAME = "look_at_image";
|
|
20
21
|
const TIMEOUT_MS = 90_000;
|
|
22
|
+
/** Waits used when the server says another image question is still running. */
|
|
23
|
+
const RETRY_DELAYS_MS = [2_000, 6_000, 12_000] as const;
|
|
24
|
+
const MAX_ATTEMPTS = RETRY_DELAYS_MS.length + 1;
|
|
25
|
+
const MAX_RETRY_DELAY_MS = 30_000;
|
|
21
26
|
|
|
22
27
|
const INPUT = {
|
|
23
28
|
type: "object",
|
|
@@ -78,6 +83,152 @@ function questionText(path: string, answer: string): string {
|
|
|
78
83
|
${answer}`;
|
|
79
84
|
}
|
|
80
85
|
|
|
86
|
+
type VisionAnswer = {
|
|
87
|
+
readonly text: string;
|
|
88
|
+
readonly model?: string;
|
|
89
|
+
readonly cost_usd?: number;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
type VisionAttempt =
|
|
93
|
+
| { readonly ok: true; readonly answer: VisionAnswer; readonly attempts: number }
|
|
94
|
+
| { readonly ok: false; readonly text: string };
|
|
95
|
+
|
|
96
|
+
function retryDelayMs(response: Response, body: any, attempt: number): number {
|
|
97
|
+
const header = Number(response.headers.get("retry-after"));
|
|
98
|
+
if (Number.isFinite(header) && header > 0) {
|
|
99
|
+
return Math.min(Math.ceil(header * 1_000), MAX_RETRY_DELAY_MS);
|
|
100
|
+
}
|
|
101
|
+
const seconds = Number(body?.retry_after_seconds);
|
|
102
|
+
if (Number.isFinite(seconds) && seconds > 0) {
|
|
103
|
+
return Math.min(Math.ceil(seconds * 1_000), MAX_RETRY_DELAY_MS);
|
|
104
|
+
}
|
|
105
|
+
return RETRY_DELAYS_MS[
|
|
106
|
+
Math.min(attempt, RETRY_DELAYS_MS.length - 1)
|
|
107
|
+
]!;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function wait(ms: number, signal: AbortSignal): Promise<void> {
|
|
111
|
+
return new Promise((resolve, reject) => {
|
|
112
|
+
const onAbort = () => {
|
|
113
|
+
clearTimeout(timer);
|
|
114
|
+
reject(signal.reason ?? new Error("aborted"));
|
|
115
|
+
};
|
|
116
|
+
const timer = setTimeout(() => {
|
|
117
|
+
signal.removeEventListener("abort", onAbort);
|
|
118
|
+
resolve();
|
|
119
|
+
}, ms);
|
|
120
|
+
if (signal.aborted) {
|
|
121
|
+
onAbort();
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* POSTs one image and retries when free-pi reports that another image question
|
|
130
|
+
* is still running. The plugin already queues local calls; retries cover CLI
|
|
131
|
+
* users, other machines and the server's short cooldown between questions.
|
|
132
|
+
*/
|
|
133
|
+
async function askVision(
|
|
134
|
+
deps: Deps,
|
|
135
|
+
token: string,
|
|
136
|
+
data: string,
|
|
137
|
+
mime: VisionMime,
|
|
138
|
+
question: string,
|
|
139
|
+
signal: AbortSignal,
|
|
140
|
+
): Promise<VisionAttempt> {
|
|
141
|
+
for (let attempt = 0; attempt < MAX_ATTEMPTS; attempt += 1) {
|
|
142
|
+
const timeout = AbortSignal.timeout(TIMEOUT_MS);
|
|
143
|
+
const requestSignal = signal
|
|
144
|
+
? AbortSignal.any([signal, timeout])
|
|
145
|
+
: timeout;
|
|
146
|
+
|
|
147
|
+
let response: Response;
|
|
148
|
+
try {
|
|
149
|
+
response = await fetch(`${resolveBaseUrl()}/tools/vision`, {
|
|
150
|
+
method: "POST",
|
|
151
|
+
headers: {
|
|
152
|
+
authorization: `Bearer ${token}`,
|
|
153
|
+
"content-type": "application/json",
|
|
154
|
+
},
|
|
155
|
+
body: JSON.stringify({
|
|
156
|
+
image_b64: data,
|
|
157
|
+
media_type: mime,
|
|
158
|
+
question,
|
|
159
|
+
}),
|
|
160
|
+
signal: requestSignal,
|
|
161
|
+
});
|
|
162
|
+
} catch (error) {
|
|
163
|
+
if (signal.aborted) {
|
|
164
|
+
return { ok: false, text: "The vision request was cancelled." };
|
|
165
|
+
}
|
|
166
|
+
if (error instanceof Error && error.name === "TimeoutError") {
|
|
167
|
+
return {
|
|
168
|
+
ok: false,
|
|
169
|
+
text: "The vision request timed out after 90 seconds.",
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
173
|
+
return { ok: false, text: "The vision request was cancelled." };
|
|
174
|
+
}
|
|
175
|
+
return { ok: false, text: "The vision request failed." };
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (response.ok) {
|
|
179
|
+
const body = (await response.json().catch(() => undefined)) as any;
|
|
180
|
+
if (typeof body?.text !== "string" || body.text === "") {
|
|
181
|
+
return { ok: false, text: "The vision model returned no answer." };
|
|
182
|
+
}
|
|
183
|
+
return {
|
|
184
|
+
ok: true,
|
|
185
|
+
attempts: attempt + 1,
|
|
186
|
+
answer: {
|
|
187
|
+
text: body.text,
|
|
188
|
+
...(typeof body.cost_usd === "number"
|
|
189
|
+
? { cost_usd: body.cost_usd }
|
|
190
|
+
: {}),
|
|
191
|
+
...(typeof body.model === "string" ? { model: body.model } : {}),
|
|
192
|
+
},
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const body = (await response.json().catch(() => ({}))) as any;
|
|
197
|
+
const code = String(body?.error?.code ?? body?.code ?? "");
|
|
198
|
+
if (code === "rate_limited") {
|
|
199
|
+
if (attempt + 1 >= MAX_ATTEMPTS) {
|
|
200
|
+
return {
|
|
201
|
+
ok: false,
|
|
202
|
+
text: "Another image question is still running. Wait a moment and call look_at_image again.",
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
const delay = retryDelayMs(response, body, attempt);
|
|
206
|
+
deps.log.info("look_at_image is waiting for the vision slot", {
|
|
207
|
+
attempt: attempt + 1,
|
|
208
|
+
delayMs: delay,
|
|
209
|
+
status: response.status,
|
|
210
|
+
});
|
|
211
|
+
try {
|
|
212
|
+
await wait(delay, signal);
|
|
213
|
+
} catch {
|
|
214
|
+
return { ok: false, text: "The vision request was cancelled." };
|
|
215
|
+
}
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const mapped = visionErrorText(code);
|
|
220
|
+
return {
|
|
221
|
+
ok: false,
|
|
222
|
+
text: mapped ?? `The vision request failed (HTTP ${response.status}).`,
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
return {
|
|
227
|
+
ok: false,
|
|
228
|
+
text: "Another image question is still running. Wait a moment and call look_at_image again.",
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
|
|
81
232
|
async function executeVision(
|
|
82
233
|
ctx: Plugin.Context,
|
|
83
234
|
deps: Deps,
|
|
@@ -120,62 +271,35 @@ async function executeVision(
|
|
|
120
271
|
);
|
|
121
272
|
}
|
|
122
273
|
|
|
123
|
-
const
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
method: "POST",
|
|
131
|
-
headers: {
|
|
132
|
-
authorization: `Bearer ${token}`,
|
|
133
|
-
"content-type": "application/json",
|
|
134
|
-
},
|
|
135
|
-
body: JSON.stringify({
|
|
136
|
-
image_b64: prepared.data,
|
|
137
|
-
media_type: prepared.mime,
|
|
138
|
-
question,
|
|
139
|
-
}),
|
|
140
|
-
signal,
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
if (!response.ok) {
|
|
144
|
-
const body = (await response.json().catch(() => ({}))) as any;
|
|
145
|
-
const code = body?.error?.code ?? body?.code ?? "";
|
|
146
|
-
const mapped = visionErrorText(String(code));
|
|
147
|
-
return fail(mapped ?? `The vision request failed (HTTP ${response.status}).`);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
const data = (await response.json()) as any;
|
|
151
|
-
if (typeof data?.text !== "string" || data.text === "") {
|
|
152
|
-
return fail("The vision model returned no answer.");
|
|
153
|
-
}
|
|
154
|
-
const elapsed = Date.now() - startedAt;
|
|
155
|
-
deps.log.info("look_at_image answered", {
|
|
156
|
-
path,
|
|
157
|
-
ms: elapsed,
|
|
158
|
-
resized: prepared.resized,
|
|
159
|
-
bytes: prepared.bytes,
|
|
160
|
-
size: `${prepared.width}x${prepared.height}`,
|
|
161
|
-
mime: prepared.mime,
|
|
162
|
-
model: data.model,
|
|
163
|
-
});
|
|
164
|
-
return done(questionText(path, data.text), {
|
|
165
|
-
ok: true,
|
|
166
|
-
...(typeof data.cost_usd === "number" ? { cost_usd: data.cost_usd } : {}),
|
|
167
|
-
...(typeof data.model === "string" ? { model: data.model } : {}),
|
|
168
|
-
elapsed_ms: elapsed,
|
|
169
|
-
});
|
|
170
|
-
} catch (error) {
|
|
171
|
-
if (error instanceof Error && error.name === "TimeoutError") {
|
|
172
|
-
return fail("The vision request timed out after 90 seconds.");
|
|
173
|
-
}
|
|
174
|
-
if (error instanceof Error && error.name === "AbortError") {
|
|
175
|
-
return fail("The vision request was cancelled.");
|
|
176
|
-
}
|
|
177
|
-
return fail("The vision request failed.");
|
|
274
|
+
const queued = await withVisionSlot(deps.log, () =>
|
|
275
|
+
askVision(deps, token, prepared.data, prepared.mime, question, context.signal),
|
|
276
|
+
);
|
|
277
|
+
if (!queued.ok) {
|
|
278
|
+
return fail(
|
|
279
|
+
"Another image question is still running. Wait a moment and call look_at_image again.",
|
|
280
|
+
);
|
|
178
281
|
}
|
|
282
|
+
if (!queued.value.ok) return fail(queued.value.text);
|
|
283
|
+
|
|
284
|
+
const { answer, attempts } = queued.value;
|
|
285
|
+
const elapsed = Date.now() - startedAt;
|
|
286
|
+
deps.log.info("look_at_image answered", {
|
|
287
|
+
path,
|
|
288
|
+
ms: elapsed,
|
|
289
|
+
attempts,
|
|
290
|
+
resized: prepared.resized,
|
|
291
|
+
bytes: prepared.bytes,
|
|
292
|
+
size: `${prepared.width}x${prepared.height}`,
|
|
293
|
+
mime: prepared.mime,
|
|
294
|
+
model: answer.model,
|
|
295
|
+
});
|
|
296
|
+
return done(questionText(path, answer.text), {
|
|
297
|
+
ok: true,
|
|
298
|
+
...(answer.cost_usd !== undefined ? { cost_usd: answer.cost_usd } : {}),
|
|
299
|
+
...(answer.model !== undefined ? { model: answer.model } : {}),
|
|
300
|
+
elapsed_ms: elapsed,
|
|
301
|
+
attempts,
|
|
302
|
+
});
|
|
179
303
|
}
|
|
180
304
|
|
|
181
305
|
/**
|
package/src/tools/web.ts
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `web_search` and `docs_lookup`, backed by free-pi's `/tools/*` endpoints.
|
|
3
|
+
*
|
|
4
|
+
* Both spend part of the user's daily allowance, exactly like the provider
|
|
5
|
+
* CLI, and are only exposed inside free-pi sessions.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { Plugin } from "@opencode/plugin";
|
|
9
|
+
import type { Deps } from "../deps.js";
|
|
10
|
+
import {
|
|
11
|
+
done,
|
|
12
|
+
fail,
|
|
13
|
+
freePiRequest,
|
|
14
|
+
GATED_TEXT,
|
|
15
|
+
isGatedSession,
|
|
16
|
+
mappedText,
|
|
17
|
+
} from "./api.js";
|
|
18
|
+
|
|
19
|
+
const ERRORS: Record<string, string> = {
|
|
20
|
+
daily_cap:
|
|
21
|
+
"The user's daily free usage is used up. Do not retry; tell the user and continue without this tool.",
|
|
22
|
+
rolling_cap:
|
|
23
|
+
"The user's short-term usage limit is reached. Do not retry; continue without this tool.",
|
|
24
|
+
rate_limited:
|
|
25
|
+
"Too many calls right now. Wait for the current call to finish, and do not call again in this turn unless needed.",
|
|
26
|
+
not_found:
|
|
27
|
+
'No documentation library matched the requested name. Retry once with the package\'s exact name, or use web_search.',
|
|
28
|
+
not_configured:
|
|
29
|
+
"This tool is not available on this server. Do not call it again in this session.",
|
|
30
|
+
upstream_error: "The search provider failed. You may retry once.",
|
|
31
|
+
guest_login_required:
|
|
32
|
+
"This tool needs a GitHub sign-in; the guest trial cannot use it. Do not retry; tell the user to sign in, and continue without this tool.",
|
|
33
|
+
guest_unavailable:
|
|
34
|
+
"The guest trial is not available right now. Do not retry; tell the user to sign in with GitHub, and continue without this tool.",
|
|
35
|
+
invalid_request:
|
|
36
|
+
"The server rejected this request. Do not retry it unchanged.",
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const WEB_INPUT = {
|
|
40
|
+
type: "object",
|
|
41
|
+
additionalProperties: false,
|
|
42
|
+
required: ["query"],
|
|
43
|
+
properties: {
|
|
44
|
+
query: {
|
|
45
|
+
type: "string",
|
|
46
|
+
minLength: 1,
|
|
47
|
+
maxLength: 500,
|
|
48
|
+
description:
|
|
49
|
+
'Specific query as you would type it into a search engine, e.g. "vite 6 release notes breaking changes"',
|
|
50
|
+
},
|
|
51
|
+
num_results: {
|
|
52
|
+
type: "integer",
|
|
53
|
+
minimum: 1,
|
|
54
|
+
maximum: 10,
|
|
55
|
+
description: "1-10, default 5; use 3 for a quick fact, 8-10 for a survey",
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
} as const;
|
|
59
|
+
|
|
60
|
+
const DOCS_INPUT = {
|
|
61
|
+
type: "object",
|
|
62
|
+
additionalProperties: false,
|
|
63
|
+
required: ["library", "query"],
|
|
64
|
+
properties: {
|
|
65
|
+
library: {
|
|
66
|
+
type: "string",
|
|
67
|
+
minLength: 1,
|
|
68
|
+
maxLength: 200,
|
|
69
|
+
description:
|
|
70
|
+
'Package or library name, e.g. "next.js", "prisma", "@tanstack/react-query"',
|
|
71
|
+
},
|
|
72
|
+
query: {
|
|
73
|
+
type: "string",
|
|
74
|
+
minLength: 1,
|
|
75
|
+
maxLength: 500,
|
|
76
|
+
description:
|
|
77
|
+
'One specific question, e.g. "configure middleware matcher for API routes"',
|
|
78
|
+
},
|
|
79
|
+
library_id: {
|
|
80
|
+
type: "string",
|
|
81
|
+
description:
|
|
82
|
+
"The library_id printed by an earlier docs_lookup, e.g. /vercel/next.js; skips the name search",
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
} as const;
|
|
86
|
+
|
|
87
|
+
function text(value: unknown): string {
|
|
88
|
+
return typeof value === "string" ? value.trim() : "";
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function formatWebResults(query: string, results: any[]): string {
|
|
92
|
+
if (!Array.isArray(results) || results.length === 0) {
|
|
93
|
+
return `No results for "${query}". Try a broader or differently worded query.`;
|
|
94
|
+
}
|
|
95
|
+
return `Web results for "${query}":\n\n${results
|
|
96
|
+
.map((item, index) => {
|
|
97
|
+
const published = item?.published_date
|
|
98
|
+
? `\nPublished: ${item.published_date}`
|
|
99
|
+
: "";
|
|
100
|
+
return `${index + 1}. ${item?.title ?? ""}\n${item?.url ?? ""}${published}\n${item?.text ?? item?.content ?? ""}`;
|
|
101
|
+
})
|
|
102
|
+
.join("\n\n")}`;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async function executeWebSearch(
|
|
106
|
+
deps: Deps,
|
|
107
|
+
raw: unknown,
|
|
108
|
+
context: { readonly sessionID: string; readonly signal: AbortSignal },
|
|
109
|
+
) {
|
|
110
|
+
if (!isGatedSession(deps, context.sessionID)) return fail(GATED_TEXT);
|
|
111
|
+
const input = raw as any;
|
|
112
|
+
const query = text(input?.query);
|
|
113
|
+
if (!query) return fail("web_search needs a query.");
|
|
114
|
+
const num =
|
|
115
|
+
typeof input?.num_results === "number" ? input.num_results : undefined;
|
|
116
|
+
|
|
117
|
+
const result = await freePiRequest(deps, {
|
|
118
|
+
path: "/tools/web-search",
|
|
119
|
+
method: "POST",
|
|
120
|
+
body: { query, ...(num !== undefined ? { num_results: num } : {}) },
|
|
121
|
+
timeoutMs: 30_000,
|
|
122
|
+
signal: context.signal,
|
|
123
|
+
label: "The web search",
|
|
124
|
+
});
|
|
125
|
+
if (!result.ok) return fail(mappedText(ERRORS, result));
|
|
126
|
+
|
|
127
|
+
deps.log.info("web_search answered", {
|
|
128
|
+
query,
|
|
129
|
+
results: Array.isArray(result.data?.results)
|
|
130
|
+
? result.data.results.length
|
|
131
|
+
: 0,
|
|
132
|
+
costUsd: result.data?.cost_usd,
|
|
133
|
+
});
|
|
134
|
+
return done(formatWebResults(query, result.data?.results ?? []), {
|
|
135
|
+
ok: true,
|
|
136
|
+
...(typeof result.data?.cost_usd === "number"
|
|
137
|
+
? { cost_usd: result.data.cost_usd }
|
|
138
|
+
: {}),
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
async function executeDocsLookup(
|
|
143
|
+
deps: Deps,
|
|
144
|
+
raw: unknown,
|
|
145
|
+
context: { readonly sessionID: string; readonly signal: AbortSignal },
|
|
146
|
+
) {
|
|
147
|
+
if (!isGatedSession(deps, context.sessionID)) return fail(GATED_TEXT);
|
|
148
|
+
const input = raw as any;
|
|
149
|
+
const library = text(input?.library);
|
|
150
|
+
const query = text(input?.query);
|
|
151
|
+
const libraryId = text(input?.library_id);
|
|
152
|
+
if (!library) return fail("docs_lookup needs a library name.");
|
|
153
|
+
if (!query) return fail("docs_lookup needs a specific question.");
|
|
154
|
+
|
|
155
|
+
const result = await freePiRequest(deps, {
|
|
156
|
+
path: "/tools/docs",
|
|
157
|
+
method: "POST",
|
|
158
|
+
body: {
|
|
159
|
+
library,
|
|
160
|
+
query,
|
|
161
|
+
...(libraryId ? { library_id: libraryId } : {}),
|
|
162
|
+
},
|
|
163
|
+
timeoutMs: 30_000,
|
|
164
|
+
signal: context.signal,
|
|
165
|
+
label: "The documentation lookup",
|
|
166
|
+
});
|
|
167
|
+
if (!result.ok) return fail(mappedText(ERRORS, result, library));
|
|
168
|
+
|
|
169
|
+
const data = result.data ?? {};
|
|
170
|
+
deps.log.info("docs_lookup answered", {
|
|
171
|
+
library,
|
|
172
|
+
libraryID: data.library_id,
|
|
173
|
+
costUsd: data.cost_usd,
|
|
174
|
+
});
|
|
175
|
+
return done(
|
|
176
|
+
`${data.title ?? library} (library_id: ${data.library_id ?? "unknown"})\n\n${data.docs ?? ""}`,
|
|
177
|
+
{
|
|
178
|
+
ok: true,
|
|
179
|
+
library_id: data.library_id,
|
|
180
|
+
...(typeof data.cost_usd === "number"
|
|
181
|
+
? { cost_usd: data.cost_usd }
|
|
182
|
+
: {}),
|
|
183
|
+
},
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export async function registerWebTools(
|
|
188
|
+
ctx: Plugin.Context,
|
|
189
|
+
deps: Deps,
|
|
190
|
+
): Promise<{ dispose: () => Promise<void> }> {
|
|
191
|
+
return ctx.tool.transform((editor) => {
|
|
192
|
+
editor.add({
|
|
193
|
+
name: "web_search",
|
|
194
|
+
description:
|
|
195
|
+
"Search the web and return a numbered list of results, each with title, URL, publish date and page text. Use it for current or recent information that your training data may lack. Each call uses part of the user's daily allowance.",
|
|
196
|
+
input: WEB_INPUT,
|
|
197
|
+
options: { codemode: false },
|
|
198
|
+
execute: (input: unknown, context: any) =>
|
|
199
|
+
executeWebSearch(deps, input, context),
|
|
200
|
+
} as any);
|
|
201
|
+
|
|
202
|
+
editor.add({
|
|
203
|
+
name: "docs_lookup",
|
|
204
|
+
description:
|
|
205
|
+
"Look up current documentation for a third-party library, framework or API. Returns the matching library's title and library_id, then documentation text relevant to your query. Use it instead of relying on memory for exact signatures and options. Each call uses part of the user's daily allowance.",
|
|
206
|
+
input: DOCS_INPUT,
|
|
207
|
+
options: { codemode: false },
|
|
208
|
+
execute: (input: unknown, context: any) =>
|
|
209
|
+
executeDocsLookup(deps, input, context),
|
|
210
|
+
} as any);
|
|
211
|
+
});
|
|
212
|
+
}
|
package/src/types.ts
CHANGED