@eduardo191labs/opencode-free-pi 0.6.3 → 0.6.5
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 +62 -26
- package/dist/agents/definitions.d.ts +19 -1
- package/dist/agents/definitions.js +36 -1
- package/dist/catalog/models.js +11 -4
- package/dist/constants.d.ts +1 -1
- package/dist/constants.js +3 -0
- package/dist/plugin.js +25 -3
- package/dist/session/events.js +31 -15
- package/dist/session/hooks.js +48 -7
- package/dist/session/tools.d.ts +12 -0
- package/dist/session/tools.js +23 -4
- package/dist/state.d.ts +2 -0
- package/dist/state.js +1 -0
- package/dist/tools/image-generate.d.ts +4 -3
- package/dist/tools/image-generate.js +6 -5
- package/dist/tools/vision.d.ts +5 -4
- package/dist/tools/vision.js +6 -5
- package/package.json +1 -1
- package/src/agents/definitions.ts +47 -1
- package/src/catalog/models.ts +10 -4
- package/src/constants.ts +3 -0
- package/src/plugin.ts +28 -2
- package/src/session/events.ts +31 -26
- package/src/session/hooks.ts +59 -6
- package/src/session/tools.ts +26 -5
- package/src/state.ts +3 -0
- package/src/tools/image-generate.ts +6 -5
- package/src/tools/vision.ts +6 -5
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* `generate_image`, the free-pi text-to-image tool.
|
|
3
3
|
*
|
|
4
|
-
* The
|
|
5
|
-
*
|
|
6
|
-
* limits and error wording as the provider
|
|
4
|
+
* The tool writes the server's image into `images/` inside the project and
|
|
5
|
+
* returns the relative path, so the result is a file even when the running
|
|
6
|
+
* model accepts images. Same endpoint, limits and error wording as the provider
|
|
7
|
+
* CLI.
|
|
7
8
|
*/
|
|
8
9
|
import type { Plugin } from "@opencode/plugin";
|
|
9
10
|
import type { Deps } from "../deps.js";
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* `generate_image`, the free-pi text-to-image tool.
|
|
3
3
|
*
|
|
4
|
-
* The
|
|
5
|
-
*
|
|
6
|
-
* limits and error wording as the provider
|
|
4
|
+
* The tool writes the server's image into `images/` inside the project and
|
|
5
|
+
* returns the relative path, so the result is a file even when the running
|
|
6
|
+
* model accepts images. Same endpoint, limits and error wording as the provider
|
|
7
|
+
* CLI.
|
|
7
8
|
*/
|
|
8
9
|
import { randomBytes } from "node:crypto";
|
|
9
10
|
import { mkdir, open } from "node:fs/promises";
|
|
@@ -123,7 +124,7 @@ async function executeGenerateImage(ctx, deps, raw, context) {
|
|
|
123
124
|
mediaType: data.media_type,
|
|
124
125
|
model: data.model,
|
|
125
126
|
});
|
|
126
|
-
return done(`Saved image to ${relative} (${ext}, ${bytes.length} bytes).
|
|
127
|
+
return done(`Saved image to ${relative} (${ext}, ${bytes.length} bytes). To check how it looks, read that file when this model accepts image input, or call look_at_image with its path. If you cannot inspect it, say so instead of describing it.`, {
|
|
127
128
|
ok: true,
|
|
128
129
|
path: relative,
|
|
129
130
|
...(typeof data.cost_usd === "number" ? { cost_usd: data.cost_usd } : {}),
|
|
@@ -134,7 +135,7 @@ export async function registerGenerateImageTool(ctx, deps) {
|
|
|
134
135
|
return ctx.tool.transform((editor) => {
|
|
135
136
|
editor.add({
|
|
136
137
|
name: "generate_image",
|
|
137
|
-
description: "Generate one image from a text prompt and save it as a file under images/ in the project. Returns the file path;
|
|
138
|
+
description: "Generate one image from a text prompt and save it as a file under images/ in the project. Returns the file path; read it or call look_at_image to check the result. Use it only when the user asks for an image, mockup, icon or illustration. Each call uses part of the user's daily allowance.",
|
|
138
139
|
input: IMAGE_INPUT,
|
|
139
140
|
options: { codemode: false },
|
|
140
141
|
execute: (input, context) => executeGenerateImage(ctx, deps, input, context),
|
package/dist/tools/vision.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* `look_at_image`, the free-pi vision tool.
|
|
3
3
|
*
|
|
4
|
-
* free-pi models
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* against the user's daily
|
|
4
|
+
* Image-capable free-pi models see attached images natively, but a file on disk
|
|
5
|
+
* still has to be attached (and its pixels stay in the context). This tool asks
|
|
6
|
+
* the server's vision model one question about the file and returns text only,
|
|
7
|
+
* which also covers text-only models. Each call counts against the user's daily
|
|
8
|
+
* allowance.
|
|
8
9
|
*/
|
|
9
10
|
import type { Plugin } from "@opencode/plugin";
|
|
10
11
|
import type { Deps } from "../deps.js";
|
package/dist/tools/vision.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* `look_at_image`, the free-pi vision tool.
|
|
3
3
|
*
|
|
4
|
-
* free-pi models
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* against the user's daily
|
|
4
|
+
* Image-capable free-pi models see attached images natively, but a file on disk
|
|
5
|
+
* still has to be attached (and its pixels stay in the context). This tool asks
|
|
6
|
+
* the server's vision model one question about the file and returns text only,
|
|
7
|
+
* which also covers text-only models. Each call counts against the user's daily
|
|
8
|
+
* allowance.
|
|
8
9
|
*/
|
|
9
10
|
import { resolveToken } from "../auth/token.js";
|
|
10
11
|
import { isFreePiSession } from "../session/tools.js";
|
|
@@ -246,7 +247,7 @@ export async function registerVisionTool(ctx, deps) {
|
|
|
246
247
|
return ctx.tool.transform((editor) => {
|
|
247
248
|
editor.add({
|
|
248
249
|
name: TOOL_NAME,
|
|
249
|
-
description: "Ask a vision model one question about a PNG, JPEG or WebP image inside the project folder (for example a screenshot) and get a text answer.
|
|
250
|
+
description: "Ask a vision model one question about a PNG, JPEG or WebP image inside the project folder (for example a screenshot) and get a text answer. Use it when the running model takes text only, or when you want a targeted answer without attaching the image. Each call uses part of the user's daily allowance.",
|
|
250
251
|
input: INPUT,
|
|
251
252
|
options: { codemode: false },
|
|
252
253
|
execute: (input, context) => executeVision(ctx, deps, input, context),
|
package/package.json
CHANGED
|
@@ -49,7 +49,8 @@ it.
|
|
|
49
49
|
- \`free_pi_usage\` shows today's remaining allowance, credits, token counts and
|
|
50
50
|
the account tier. Read-only.
|
|
51
51
|
- \`look_at_image\` asks a vision model about a PNG, JPEG or WebP inside the
|
|
52
|
-
project.
|
|
52
|
+
project. Use it when the current model takes text only, or when the image is a
|
|
53
|
+
file on disk and you want a targeted answer instead of attaching it.
|
|
53
54
|
- \`generate_image\` draws one image from a prompt and saves it under \`images/\`.
|
|
54
55
|
- \`web_search\` searches the web for current information.
|
|
55
56
|
- \`docs_lookup\` looks up current library and framework documentation.
|
|
@@ -88,6 +89,20 @@ OpenCode as usual; free-pi does not replace them. Prefer those native features
|
|
|
88
89
|
over running an external CLI.
|
|
89
90
|
`;
|
|
90
91
|
|
|
92
|
+
/**
|
|
93
|
+
* The same skill as a native `SKILL.md`. OpenCode discovers it from the global
|
|
94
|
+
* skills directory, so the file, its ID, its permissions and the `skill` tool
|
|
95
|
+
* all belong to OpenCode instead of a runtime registration.
|
|
96
|
+
*/
|
|
97
|
+
export const FREE_PI_SKILL_FILE = `---
|
|
98
|
+
name: ${FREE_PI_SKILL_NAME}
|
|
99
|
+
description: ${FREE_PI_SKILL_DESCRIPTION}
|
|
100
|
+
---
|
|
101
|
+
|
|
102
|
+
${MARKER}
|
|
103
|
+
|
|
104
|
+
${FREE_PI_SKILL}`;
|
|
105
|
+
|
|
91
106
|
const SCOUT = `---
|
|
92
107
|
description: Read-only recon. Locates code, traces how pieces connect, and returns structured findings the lead can use without re-reading the files.
|
|
93
108
|
mode: all
|
|
@@ -254,6 +269,10 @@ export function freePiAgentPath(configDir: string, id: FreePiAgentID): string {
|
|
|
254
269
|
return join(configDir, "agents", ...id.split("/")) + ".md";
|
|
255
270
|
}
|
|
256
271
|
|
|
272
|
+
export function freePiSkillPath(configDir: string): string {
|
|
273
|
+
return join(configDir, "skills", FREE_PI_SKILL_ID, "SKILL.md");
|
|
274
|
+
}
|
|
275
|
+
|
|
257
276
|
async function fileState(
|
|
258
277
|
path: string,
|
|
259
278
|
content: string,
|
|
@@ -300,3 +319,30 @@ export async function installFreePiAgents(
|
|
|
300
319
|
|
|
301
320
|
return { installed, skipped, changed: installed.length > 0 };
|
|
302
321
|
}
|
|
322
|
+
|
|
323
|
+
export type SkillInstallResult = {
|
|
324
|
+
readonly installed: boolean;
|
|
325
|
+
readonly skipped: boolean;
|
|
326
|
+
readonly changed: boolean;
|
|
327
|
+
};
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Writes the free-pi skill as `skills/free-pi/SKILL.md`, the layout OpenCode's
|
|
331
|
+
* own skill loader reads. A file without our marker is user-owned and stays
|
|
332
|
+
* untouched, exactly like the agent files.
|
|
333
|
+
*/
|
|
334
|
+
export async function installFreePiSkill(
|
|
335
|
+
configDir = openCodeConfigDir(),
|
|
336
|
+
): Promise<SkillInstallResult> {
|
|
337
|
+
const path = freePiSkillPath(configDir);
|
|
338
|
+
const state = await fileState(path, FREE_PI_SKILL_FILE);
|
|
339
|
+
if (state === "same") {
|
|
340
|
+
return { installed: false, skipped: false, changed: false };
|
|
341
|
+
}
|
|
342
|
+
if (state === "protected") {
|
|
343
|
+
return { installed: false, skipped: true, changed: false };
|
|
344
|
+
}
|
|
345
|
+
await mkdir(dirname(path), { recursive: true });
|
|
346
|
+
await writeFile(path, FREE_PI_SKILL_FILE, "utf8");
|
|
347
|
+
return { installed: true, skipped: false, changed: true };
|
|
348
|
+
}
|
package/src/catalog/models.ts
CHANGED
|
@@ -329,9 +329,10 @@ const KNOWN_INPUT_MODALITIES = new Set([
|
|
|
329
329
|
/**
|
|
330
330
|
* Native modalities for the chat route. models.dev describes the upstream
|
|
331
331
|
* model, and the gateway forwards image parts to multimodal upstreams, so an
|
|
332
|
-
* image-capable model is advertised as such.
|
|
333
|
-
*
|
|
334
|
-
*
|
|
332
|
+
* image-capable model is advertised as such. free-pi's free models were probed
|
|
333
|
+
* with `curl_cffi` and all of them transcribe an attached screenshot correctly
|
|
334
|
+
* even when models.dev has no entry for the alias, so free models always accept
|
|
335
|
+
* images. Output is always text: free-pi has no image results on the chat route.
|
|
335
336
|
*/
|
|
336
337
|
function modelModalities(
|
|
337
338
|
model: CatalogModel,
|
|
@@ -341,6 +342,7 @@ function modelModalities(
|
|
|
341
342
|
const input = (declared ?? ["text"]).filter((value) =>
|
|
342
343
|
KNOWN_INPUT_MODALITIES.has(value),
|
|
343
344
|
);
|
|
345
|
+
if (model.free === true && !input.includes("image")) input.push("image");
|
|
344
346
|
if (input.length === 0) input.push("text");
|
|
345
347
|
if (!input.includes("text")) input.unshift("text");
|
|
346
348
|
return { input, output: ["text"] };
|
|
@@ -422,7 +424,11 @@ export function buildModels(
|
|
|
422
424
|
...(defaultEffort ? { settings: { reasoningEffort: defaultEffort } } : {}),
|
|
423
425
|
enabled: model.unavailable !== true,
|
|
424
426
|
temperature: model.temperature ?? capability?.temperature ?? true,
|
|
425
|
-
|
|
427
|
+
// All free models were probed against the gateway and call tools, so a
|
|
428
|
+
// models.dev entry that marks the matched upstream as text-only must not
|
|
429
|
+
// take away their tools. That would also hide OpenCode's native
|
|
430
|
+
// `subagent` tool. A `tool_call` value in free-pi's own catalog still wins.
|
|
431
|
+
tool_call: model.tool_call ?? (model.free === true ? true : capability?.tool_call ?? true),
|
|
426
432
|
attachment:
|
|
427
433
|
model.attachment ??
|
|
428
434
|
capability?.attachment ??
|
package/src/constants.ts
CHANGED
|
@@ -77,6 +77,9 @@ export const FREE_PI_GUIDANCE = `# free-pi working rules
|
|
|
77
77
|
- Read the files involved before editing and run the project's checks afterwards. Never say a test, build or command passed unless you ran it and saw it pass.
|
|
78
78
|
- Treat the conversation above as memory: reuse earlier decisions, file paths, identifiers and results. If something is genuinely missing, say what is missing instead of guessing.
|
|
79
79
|
- Use the available tools (read, grep, glob, bash, edit) for real work; do not describe hypothetical commands as if they ran.
|
|
80
|
+
- Delegate large or self-contained work with OpenCode's native \`subagent\` tool: \`free-pi/scout\` and \`free-pi/planner\` for read-only recon and planning, \`free-pi/worker\` for a full task, \`free-pi/reviewer\` for review. A subagent runs in its own context and reports back; never launch a subagent from inside one.
|
|
81
|
+
- Load a skill with the native \`skill\` tool when its description matches the task.
|
|
82
|
+
- \`AGENTS.md\` and the files listed under \`instructions\` are the project's memory. Follow them; do not restate or replace them.
|
|
80
83
|
- Never claim to have compacted, summarized or cleared context unless that actually happened.
|
|
81
84
|
- Continue until the request is finished, then report concisely what changed and what you verified.`;
|
|
82
85
|
export const FREE_PI_GUIDANCE_MARKER = "# free-pi working rules";
|
package/src/plugin.ts
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
/** OpenCode V2 plugin entry point for free-pi. */
|
|
2
2
|
|
|
3
3
|
import { Plugin } from "@opencode/plugin";
|
|
4
|
-
import { join } from "node:path";
|
|
5
4
|
import {
|
|
6
5
|
FREE_PI_SKILL,
|
|
7
6
|
FREE_PI_SKILL_DESCRIPTION,
|
|
8
7
|
FREE_PI_SKILL_ID,
|
|
9
8
|
FREE_PI_SKILL_NAME,
|
|
9
|
+
freePiSkillPath,
|
|
10
10
|
installFreePiAgents,
|
|
11
|
+
installFreePiSkill,
|
|
11
12
|
openCodeConfigDir,
|
|
12
13
|
} from "./agents/definitions.js";
|
|
13
14
|
import { fetchQuota } from "./api/quota.js";
|
|
@@ -100,6 +101,25 @@ async function setup(ctx: Plugin.Context): Promise<() => Promise<void>> {
|
|
|
100
101
|
} catch (error) {
|
|
101
102
|
log.warn("Could not install the free-pi subagents", error);
|
|
102
103
|
}
|
|
104
|
+
// OpenCode's own skill loader reads `skills/free-pi/SKILL.md`. Install the
|
|
105
|
+
// file first so the skill has a real path, supporting files and permissions
|
|
106
|
+
// like any other native skill; the transform below only fills the gap when
|
|
107
|
+
// the config directory is not writable.
|
|
108
|
+
try {
|
|
109
|
+
const skill = await installFreePiSkill();
|
|
110
|
+
if (skill.changed) {
|
|
111
|
+
await ctx.skill.reload();
|
|
112
|
+
log.info("Installed the native free-pi skill");
|
|
113
|
+
}
|
|
114
|
+
if (skill.skipped) {
|
|
115
|
+
log.warn(
|
|
116
|
+
"Left the user-owned free-pi skill file untouched",
|
|
117
|
+
freePiSkillPath(openCodeConfigDir()),
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
} catch (error) {
|
|
121
|
+
log.warn("Could not install the free-pi skill file", error);
|
|
122
|
+
}
|
|
103
123
|
try {
|
|
104
124
|
nativeRegistrations.push(
|
|
105
125
|
await ctx.skill.transform((editor) => {
|
|
@@ -108,7 +128,7 @@ async function setup(ctx: Plugin.Context): Promise<() => Promise<void>> {
|
|
|
108
128
|
id: FREE_PI_SKILL_ID,
|
|
109
129
|
name: FREE_PI_SKILL_NAME,
|
|
110
130
|
description: FREE_PI_SKILL_DESCRIPTION,
|
|
111
|
-
path:
|
|
131
|
+
path: freePiSkillPath(openCodeConfigDir()),
|
|
112
132
|
content: FREE_PI_SKILL,
|
|
113
133
|
} as any);
|
|
114
134
|
}),
|
|
@@ -150,9 +170,15 @@ async function setup(ctx: Plugin.Context): Promise<() => Promise<void>> {
|
|
|
150
170
|
|
|
151
171
|
for (const [id, model] of Object.entries(mainModels)) {
|
|
152
172
|
state.outputLimits.set(`${PROVIDER_ID}/${id}`, model.limit.output);
|
|
173
|
+
if (model.modalities.input.includes("image")) {
|
|
174
|
+
state.imageModels.add(`${PROVIDER_ID}/${id}`);
|
|
175
|
+
}
|
|
153
176
|
}
|
|
154
177
|
for (const [id, model] of Object.entries(defenseModels)) {
|
|
155
178
|
state.outputLimits.set(`${DEFENSE_PROVIDER_ID}/${id}`, model.limit.output);
|
|
179
|
+
if (model.modalities.input.includes("image")) {
|
|
180
|
+
state.imageModels.add(`${DEFENSE_PROVIDER_ID}/${id}`);
|
|
181
|
+
}
|
|
156
182
|
}
|
|
157
183
|
|
|
158
184
|
const credential = await deps.activeCredential().catch(() => undefined);
|
package/src/session/events.ts
CHANGED
|
@@ -7,7 +7,7 @@ import { tokenFromCredential } from "../auth/credential.js";
|
|
|
7
7
|
import type { Deps } from "../deps.js";
|
|
8
8
|
import { handleFreePiError } from "../errors/handle.js";
|
|
9
9
|
import { noteStepTokens } from "../catalog/live.js";
|
|
10
|
-
import { forgetToolStats } from "./tools.js";
|
|
10
|
+
import { forgetToolStats, isFreePiSession } from "./tools.js";
|
|
11
11
|
import {
|
|
12
12
|
beginExecution,
|
|
13
13
|
cancelAllFinalizeTimers,
|
|
@@ -69,21 +69,25 @@ async function handleEvent(
|
|
|
69
69
|
return;
|
|
70
70
|
}
|
|
71
71
|
case "session.execution.started": {
|
|
72
|
-
|
|
72
|
+
const sessionID = event.data?.sessionID;
|
|
73
|
+
if (typeof sessionID === "string" && isFreePiSession(deps, sessionID)) {
|
|
74
|
+
beginExecution(deps, sessionID);
|
|
75
|
+
}
|
|
73
76
|
return;
|
|
74
77
|
}
|
|
75
78
|
case "session.execution.failed": {
|
|
76
79
|
const sessionID = event.data?.sessionID;
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
80
|
+
if (typeof sessionID === "string" && isFreePiSession(deps, sessionID)) {
|
|
81
|
+
// A request that dies mid-stream can leave its slot locked on the
|
|
82
|
+
// server. The next turn will use a fresh wire id instead of waiting on
|
|
83
|
+
// a retry.
|
|
84
|
+
if (isAbortLike(event.data?.error)) {
|
|
85
|
+
markWireSessionDirty(deps, sessionID);
|
|
86
|
+
}
|
|
87
|
+
await handleFreePiError(deps, event.data?.error, {
|
|
88
|
+
allowRaw: true,
|
|
89
|
+
}).catch(() => undefined);
|
|
81
90
|
}
|
|
82
|
-
await handleFreePiError(deps, event.data?.error, {
|
|
83
|
-
allowRaw:
|
|
84
|
-
typeof sessionID === "string" &&
|
|
85
|
-
deps.state.freePiSessions.has(sessionID),
|
|
86
|
-
}).catch(() => undefined);
|
|
87
91
|
endExecution(deps, sessionID);
|
|
88
92
|
return;
|
|
89
93
|
}
|
|
@@ -92,30 +96,31 @@ async function handleEvent(
|
|
|
92
96
|
return;
|
|
93
97
|
}
|
|
94
98
|
case "session.execution.interrupted": {
|
|
95
|
-
|
|
96
|
-
|
|
99
|
+
const sessionID = event.data?.sessionID;
|
|
100
|
+
if (typeof sessionID === "string" && isFreePiSession(deps, sessionID)) {
|
|
101
|
+
markWireSessionDirty(deps, sessionID);
|
|
102
|
+
}
|
|
103
|
+
endExecution(deps, sessionID);
|
|
97
104
|
return;
|
|
98
105
|
}
|
|
99
106
|
case "session.usage.updated": {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
);
|
|
107
|
+
const sessionID = event.data?.sessionID;
|
|
108
|
+
if (typeof sessionID !== "string" || !isFreePiSession(deps, sessionID)) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
recordUsage(deps, sessionID, event.data?.cost, event.data?.tokens);
|
|
106
112
|
return;
|
|
107
113
|
}
|
|
108
114
|
case "session.step.ended": {
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
);
|
|
115
|
+
const sessionID = event.data?.sessionID;
|
|
116
|
+
if (typeof sessionID !== "string" || !isFreePiSession(deps, sessionID)) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
recordUsage(deps, sessionID, event.data?.cost, event.data?.tokens);
|
|
115
120
|
// Step tokens are per request; usage.updated is cumulative.
|
|
116
121
|
noteStepTokens(
|
|
117
122
|
deps,
|
|
118
|
-
deps.state.trackedModels.get(
|
|
123
|
+
deps.state.trackedModels.get(sessionID)?.modelID ?? "",
|
|
119
124
|
event.data?.tokens,
|
|
120
125
|
);
|
|
121
126
|
return;
|
package/src/session/hooks.ts
CHANGED
|
@@ -55,6 +55,7 @@ import {
|
|
|
55
55
|
wireSessionID,
|
|
56
56
|
} from "./wire.js";
|
|
57
57
|
import {
|
|
58
|
+
FREE_PI_TOOL_NAMES,
|
|
58
59
|
isFreePiSession,
|
|
59
60
|
recordCompaction,
|
|
60
61
|
recordToolEnd,
|
|
@@ -181,7 +182,6 @@ async function applyModelRequest(deps: Deps, event: any): Promise<void> {
|
|
|
181
182
|
const providerID = event.model?.providerID;
|
|
182
183
|
if (!isFreePiProvider(providerID)) return;
|
|
183
184
|
rememberFreePiSession(deps, event.sessionID);
|
|
184
|
-
rememberTrackedModel(deps, event.sessionID, providerID, event.model?.id ?? "");
|
|
185
185
|
|
|
186
186
|
const agent = event.agent ?? (event.kind === "title" ? "title" : undefined);
|
|
187
187
|
event.headers ??= {};
|
|
@@ -368,6 +368,22 @@ function applyRetryPolicy(deps: Deps, event: any): void {
|
|
|
368
368
|
);
|
|
369
369
|
}
|
|
370
370
|
|
|
371
|
+
/**
|
|
372
|
+
* Removes this plugin's tools from a request served by another provider. The
|
|
373
|
+
* tool editor is global, so without this every model would see `look_at_image`,
|
|
374
|
+
* `web_search` and the rest in its prompt even though their execution refuses
|
|
375
|
+
* non-free-pi sessions. The session's own (not another provider's) tool list is
|
|
376
|
+
* what the model receives, so this keeps free-pi's tools to free-pi models.
|
|
377
|
+
*/
|
|
378
|
+
function hideFreePiTools(event: any): void {
|
|
379
|
+
if (isFreePiProvider(event.model?.providerID)) return;
|
|
380
|
+
const tools = event.tools;
|
|
381
|
+
if (!tools || typeof tools !== "object") return;
|
|
382
|
+
for (const name of FREE_PI_TOOL_NAMES) {
|
|
383
|
+
if (Object.hasOwn(tools, name)) delete tools[name];
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
371
387
|
async function registerToolHooks(
|
|
372
388
|
ctx: Plugin.Context,
|
|
373
389
|
deps: Deps,
|
|
@@ -393,14 +409,22 @@ async function registerToolHooks(
|
|
|
393
409
|
}
|
|
394
410
|
|
|
395
411
|
/**
|
|
396
|
-
*
|
|
397
|
-
*
|
|
398
|
-
*
|
|
399
|
-
*
|
|
412
|
+
* Models whose upstream accepts images get them natively, so a `read` that
|
|
413
|
+
* returns a picture must keep the file part. Text-only models would turn it
|
|
414
|
+
* into an error, so the image is swapped for a pointer to `look_at_image` when
|
|
415
|
+
* the tool settles: the base64 payload never enters the session (or a later
|
|
416
|
+
* compaction) instead of being stored only to be discarded at request time.
|
|
400
417
|
*/
|
|
401
418
|
function replaceUnsupportedImages(deps: Deps, event: any): void {
|
|
402
419
|
if (event.status !== "completed") return;
|
|
403
420
|
if (!isFreePiSession(deps, event.sessionID)) return;
|
|
421
|
+
const tracked = deps.state.trackedModels.get(event.sessionID);
|
|
422
|
+
if (
|
|
423
|
+
tracked &&
|
|
424
|
+
deps.state.imageModels.has(`${tracked.providerID}/${tracked.modelID}`)
|
|
425
|
+
) {
|
|
426
|
+
return;
|
|
427
|
+
}
|
|
404
428
|
const content = event.result?.content;
|
|
405
429
|
if (!Array.isArray(content)) return;
|
|
406
430
|
const replaced = content.map((part: any) => {
|
|
@@ -411,7 +435,7 @@ function replaceUnsupportedImages(deps: Deps, event: any): void {
|
|
|
411
435
|
typeof event.input?.path === "string" ? event.input.path : name;
|
|
412
436
|
return {
|
|
413
437
|
type: "text",
|
|
414
|
-
text: `ERROR: Cannot read "${name}" (free-pi
|
|
438
|
+
text: `ERROR: Cannot read "${name}" (this free-pi model takes text only). If you need its content, call look_at_image with path "${requested}" and a specific question, or switch to a model that accepts image input.`,
|
|
415
439
|
};
|
|
416
440
|
});
|
|
417
441
|
if (replaced.every((part, index) => part === content[index])) return;
|
|
@@ -450,6 +474,35 @@ export async function registerSessionHooks(
|
|
|
450
474
|
);
|
|
451
475
|
await hookForProviders(ctx, "generate", generation("generate"), registrations);
|
|
452
476
|
await hookForProviders(ctx, "title", generation("title"), registrations);
|
|
477
|
+
// The generation hooks above only run for free-pi providers. These unscoped
|
|
478
|
+
// ones strip the plugin's tools from every other provider's requests.
|
|
479
|
+
for (const name of ["context", "compaction", "generate"] as const) {
|
|
480
|
+
registrations.push(
|
|
481
|
+
await ctx.session.hook(name as any, ((event: any) => {
|
|
482
|
+
hideFreePiTools(event);
|
|
483
|
+
}) as any),
|
|
484
|
+
);
|
|
485
|
+
}
|
|
486
|
+
// Track the model of every request, not only free-pi's: the tool gate and
|
|
487
|
+
// the usage handler must know when a session switches to another provider,
|
|
488
|
+
// otherwise a session that used free-pi once would keep its tools armed.
|
|
489
|
+
registrations.push(
|
|
490
|
+
await ctx.session.hook("model.request" as any, ((event: any) => {
|
|
491
|
+
const providerID = event.model?.providerID;
|
|
492
|
+
if (
|
|
493
|
+
typeof providerID === "string" &&
|
|
494
|
+
typeof event.sessionID === "string" &&
|
|
495
|
+
event.sessionID
|
|
496
|
+
) {
|
|
497
|
+
rememberTrackedModel(
|
|
498
|
+
deps,
|
|
499
|
+
event.sessionID,
|
|
500
|
+
providerID,
|
|
501
|
+
event.model?.id ?? "",
|
|
502
|
+
);
|
|
503
|
+
}
|
|
504
|
+
}) as any),
|
|
505
|
+
);
|
|
453
506
|
await hookForProviders(
|
|
454
507
|
ctx,
|
|
455
508
|
"model.request",
|
package/src/session/tools.ts
CHANGED
|
@@ -24,6 +24,20 @@ const ID_PATTERN = /^[\w.:-]{1,100}$/;
|
|
|
24
24
|
const MAX_ENTRY_LENGTH = 150;
|
|
25
25
|
const MAX_STARTED = 200;
|
|
26
26
|
|
|
27
|
+
/**
|
|
28
|
+
* Every tool this plugin adds. They are registered globally because OpenCode's
|
|
29
|
+
* tool editor has no provider filter, so the plugin hides them from requests
|
|
30
|
+
* served by another provider (see `hideFreePiTools`) and refuses to run them
|
|
31
|
+
* outside a free-pi session.
|
|
32
|
+
*/
|
|
33
|
+
export const FREE_PI_TOOL_NAMES = [
|
|
34
|
+
"look_at_image",
|
|
35
|
+
"free_pi_usage",
|
|
36
|
+
"web_search",
|
|
37
|
+
"docs_lookup",
|
|
38
|
+
"generate_image",
|
|
39
|
+
] as const;
|
|
40
|
+
|
|
27
41
|
type ToolResultLike = {
|
|
28
42
|
metadata?: Record<string, unknown>;
|
|
29
43
|
};
|
|
@@ -35,13 +49,20 @@ type ToolEnd = {
|
|
|
35
49
|
result?: ToolResultLike;
|
|
36
50
|
};
|
|
37
51
|
|
|
52
|
+
/**
|
|
53
|
+
* Whether this session is currently served by free-pi. The last request's
|
|
54
|
+
* model wins over the historical set, so a session that switches to another
|
|
55
|
+
* provider stops seeing free-pi's tools and headers immediately.
|
|
56
|
+
*/
|
|
38
57
|
export function isFreePiSession(deps: Deps, sessionID: string): boolean {
|
|
39
|
-
if (deps.state.freePiSessions.has(sessionID)) return true;
|
|
40
58
|
const tracked = deps.state.trackedModels.get(sessionID);
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
59
|
+
if (tracked) {
|
|
60
|
+
return (
|
|
61
|
+
tracked.providerID === PROVIDER_ID ||
|
|
62
|
+
tracked.providerID === DEFENSE_PROVIDER_ID
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
return deps.state.freePiSessions.has(sessionID);
|
|
45
66
|
}
|
|
46
67
|
|
|
47
68
|
function statsFor(deps: Deps, sessionID: string): ToolStats {
|
package/src/state.ts
CHANGED
|
@@ -29,6 +29,8 @@ export type RuntimeState = {
|
|
|
29
29
|
lastQuotaNotice?: string;
|
|
30
30
|
catalogModels: Catalog;
|
|
31
31
|
outputLimits: Map<string, number>;
|
|
32
|
+
/** `provider/model` ids whose upstream accepts image input. */
|
|
33
|
+
imageModels: Set<string>;
|
|
32
34
|
|
|
33
35
|
quotaCache?: QuotaSnapshot;
|
|
34
36
|
quotaPromise?: Promise<QuotaSnapshot>;
|
|
@@ -74,6 +76,7 @@ export function createRuntimeState(): RuntimeState {
|
|
|
74
76
|
registryAttemptAt: 0,
|
|
75
77
|
catalogModels: {},
|
|
76
78
|
outputLimits: new Map(),
|
|
79
|
+
imageModels: new Set(),
|
|
77
80
|
lastQuotaRefreshAt: 0,
|
|
78
81
|
lastErrorLogAt: 0,
|
|
79
82
|
tokenChecks: new Map(),
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* `generate_image`, the free-pi text-to-image tool.
|
|
3
3
|
*
|
|
4
|
-
* The
|
|
5
|
-
*
|
|
6
|
-
* limits and error wording as the provider
|
|
4
|
+
* The tool writes the server's image into `images/` inside the project and
|
|
5
|
+
* returns the relative path, so the result is a file even when the running
|
|
6
|
+
* model accepts images. Same endpoint, limits and error wording as the provider
|
|
7
|
+
* CLI.
|
|
7
8
|
*/
|
|
8
9
|
|
|
9
10
|
import { randomBytes } from "node:crypto";
|
|
@@ -167,7 +168,7 @@ async function executeGenerateImage(
|
|
|
167
168
|
model: data.model,
|
|
168
169
|
});
|
|
169
170
|
return done(
|
|
170
|
-
`Saved image to ${relative} (${ext}, ${bytes.length} bytes).
|
|
171
|
+
`Saved image to ${relative} (${ext}, ${bytes.length} bytes). To check how it looks, read that file when this model accepts image input, or call look_at_image with its path. If you cannot inspect it, say so instead of describing it.`,
|
|
171
172
|
{
|
|
172
173
|
ok: true,
|
|
173
174
|
path: relative,
|
|
@@ -185,7 +186,7 @@ export async function registerGenerateImageTool(
|
|
|
185
186
|
editor.add({
|
|
186
187
|
name: "generate_image",
|
|
187
188
|
description:
|
|
188
|
-
"Generate one image from a text prompt and save it as a file under images/ in the project. Returns the file path;
|
|
189
|
+
"Generate one image from a text prompt and save it as a file under images/ in the project. Returns the file path; read it or call look_at_image to check the result. Use it only when the user asks for an image, mockup, icon or illustration. Each call uses part of the user's daily allowance.",
|
|
189
190
|
input: IMAGE_INPUT,
|
|
190
191
|
options: { codemode: false },
|
|
191
192
|
execute: (input: unknown, context: any) =>
|
package/src/tools/vision.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* `look_at_image`, the free-pi vision tool.
|
|
3
3
|
*
|
|
4
|
-
* free-pi models
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* against the user's daily
|
|
4
|
+
* Image-capable free-pi models see attached images natively, but a file on disk
|
|
5
|
+
* still has to be attached (and its pixels stay in the context). This tool asks
|
|
6
|
+
* the server's vision model one question about the file and returns text only,
|
|
7
|
+
* which also covers text-only models. Each call counts against the user's daily
|
|
8
|
+
* allowance.
|
|
8
9
|
*/
|
|
9
10
|
|
|
10
11
|
import type { Plugin } from "@opencode/plugin";
|
|
@@ -314,7 +315,7 @@ export async function registerVisionTool(
|
|
|
314
315
|
editor.add({
|
|
315
316
|
name: TOOL_NAME,
|
|
316
317
|
description:
|
|
317
|
-
"Ask a vision model one question about a PNG, JPEG or WebP image inside the project folder (for example a screenshot) and get a text answer.
|
|
318
|
+
"Ask a vision model one question about a PNG, JPEG or WebP image inside the project folder (for example a screenshot) and get a text answer. Use it when the running model takes text only, or when you want a targeted answer without attaching the image. Each call uses part of the user's daily allowance.",
|
|
318
319
|
input: INPUT,
|
|
319
320
|
options: { codemode: false },
|
|
320
321
|
execute: (
|