@eduardo191labs/opencode-free-pi 0.2.4 → 0.2.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 +20 -4
- package/dist/index.js +89 -58
- package/package.json +1 -1
- package/scripts/postinstall.mjs +28 -3
- package/src/index.ts +93 -62
package/README.md
CHANGED
|
@@ -33,10 +33,26 @@ OpenCode 2 usa la entrada `plugins` (plural) y carga el paquete desde su
|
|
|
33
33
|
`exports`. Este paquete exporta las dos formas: el `server` de OpenCode 1.x y
|
|
34
34
|
el `setup` de OpenCode 2, así que el mismo paquete funciona en ambos.
|
|
35
35
|
|
|
36
|
-
```
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
36
|
+
```bash
|
|
37
|
+
opencode plugin add @eduardo191labs/opencode-free-pi@latest
|
|
38
|
+
opencode plugin list
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
La lista debe mostrar `free-pi` como `active`, con la versión del paquete. Si
|
|
42
|
+
aparece `failed`, ejecuta `opencode --print-logs` y revisa el `cause=` del log;
|
|
43
|
+
el `ref=` solo no contiene el motivo.
|
|
44
|
+
|
|
45
|
+
El `postinstall` antiguo copiaba `dist/index.js` a
|
|
46
|
+
`~/.config/opencode/plugins/opencode-free-pi.ts`. Ese archivo local no tiene
|
|
47
|
+
versión de paquete y puede quedar desactualizado; en OpenCode 2 bórralo para
|
|
48
|
+
que no compita con el paquete:
|
|
49
|
+
|
|
50
|
+
```powershell
|
|
51
|
+
Remove-Item "$env:USERPROFILE\.config\opencode\plugins\opencode-free-pi.ts" -Force -ErrorAction SilentlyContinue
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
rm -f ~/.config/opencode/plugins/opencode-free-pi.ts
|
|
40
56
|
```
|
|
41
57
|
|
|
42
58
|
En OpenCode 2 el proveedor y las herramientas se registran igual. Las
|
package/dist/index.js
CHANGED
|
@@ -3136,11 +3136,17 @@ function v2Client(ctx) {
|
|
|
3136
3136
|
},
|
|
3137
3137
|
};
|
|
3138
3138
|
}
|
|
3139
|
+
function settle(promise, fallback) {
|
|
3140
|
+
return Promise.race([
|
|
3141
|
+
promise,
|
|
3142
|
+
new Promise((resolve) => setTimeout(() => resolve(fallback), 6000)),
|
|
3143
|
+
]);
|
|
3144
|
+
}
|
|
3139
3145
|
async function FreePiV2Setup(ctx) {
|
|
3140
3146
|
const includeDefense = ctx.options?.includeDefense !== false;
|
|
3141
3147
|
const [catalog, registry] = await Promise.all([
|
|
3142
|
-
fetchCatalog(),
|
|
3143
|
-
fetchModelsRegistry(),
|
|
3148
|
+
settle(fetchCatalog(), undefined),
|
|
3149
|
+
settle(fetchModelsRegistry(), undefined),
|
|
3144
3150
|
]);
|
|
3145
3151
|
const { main, defense } = partitionModels(catalog);
|
|
3146
3152
|
const candidates = modelRegistryCandidates(registry);
|
|
@@ -3154,35 +3160,50 @@ async function FreePiV2Setup(ctx) {
|
|
|
3154
3160
|
editor.add(v2Provider(DEFENSE_PROVIDER_ID, "Free Pi Defense", buildModels(defense, candidates), token));
|
|
3155
3161
|
}
|
|
3156
3162
|
});
|
|
3157
|
-
|
|
3158
|
-
|
|
3159
|
-
const
|
|
3160
|
-
|
|
3161
|
-
|
|
3162
|
-
|
|
3163
|
-
|
|
3164
|
-
|
|
3165
|
-
|
|
3166
|
-
|
|
3167
|
-
|
|
3168
|
-
|
|
3169
|
-
|
|
3170
|
-
|
|
3171
|
-
|
|
3172
|
-
|
|
3173
|
-
|
|
3174
|
-
|
|
3175
|
-
|
|
3176
|
-
|
|
3177
|
-
|
|
3178
|
-
|
|
3179
|
-
|
|
3180
|
-
|
|
3181
|
-
|
|
3182
|
-
|
|
3183
|
-
|
|
3184
|
-
|
|
3185
|
-
|
|
3163
|
+
try {
|
|
3164
|
+
await ctx.tool.transform((editor) => {
|
|
3165
|
+
for (const [name, definition] of Object.entries(hooks.tool ?? {})) {
|
|
3166
|
+
const args = definition.args && typeof definition.args === "object"
|
|
3167
|
+
? definition.args
|
|
3168
|
+
: {};
|
|
3169
|
+
let input = {
|
|
3170
|
+
type: "object",
|
|
3171
|
+
properties: {},
|
|
3172
|
+
additionalProperties: true,
|
|
3173
|
+
};
|
|
3174
|
+
try {
|
|
3175
|
+
input = tool.schema.toJSONSchema(tool.schema.object(args));
|
|
3176
|
+
delete input.$schema;
|
|
3177
|
+
}
|
|
3178
|
+
catch (error) {
|
|
3179
|
+
console.error(`[free-pi] could not build the schema for ${name}; using a permissive schema`, error);
|
|
3180
|
+
}
|
|
3181
|
+
editor.add({
|
|
3182
|
+
name,
|
|
3183
|
+
description: definition.description ?? name,
|
|
3184
|
+
input,
|
|
3185
|
+
execute: async (value, context) => {
|
|
3186
|
+
const result = await definition.execute(value, {
|
|
3187
|
+
sessionID: context.sessionID,
|
|
3188
|
+
messageID: context.messageID,
|
|
3189
|
+
agent: context.agent,
|
|
3190
|
+
id: context.id,
|
|
3191
|
+
directory: ctx.location?.directory ?? process.cwd(),
|
|
3192
|
+
worktree: ctx.location?.directory ?? process.cwd(),
|
|
3193
|
+
abort: context.signal,
|
|
3194
|
+
});
|
|
3195
|
+
return {
|
|
3196
|
+
content: result?.output ?? result?.title ?? "",
|
|
3197
|
+
metadata: result?.metadata,
|
|
3198
|
+
};
|
|
3199
|
+
},
|
|
3200
|
+
});
|
|
3201
|
+
}
|
|
3202
|
+
});
|
|
3203
|
+
}
|
|
3204
|
+
catch (error) {
|
|
3205
|
+
console.error("[free-pi] could not register tools in OpenCode 2", error);
|
|
3206
|
+
}
|
|
3186
3207
|
const applyRequest = async (event) => {
|
|
3187
3208
|
const providerID = event.model?.providerID;
|
|
3188
3209
|
if (providerID !== PROVIDER_ID && providerID !== DEFENSE_PROVIDER_ID)
|
|
@@ -3204,34 +3225,44 @@ async function FreePiV2Setup(ctx) {
|
|
|
3204
3225
|
}
|
|
3205
3226
|
};
|
|
3206
3227
|
for (const kind of ["context", "compaction", "generate", "title"]) {
|
|
3207
|
-
|
|
3228
|
+
try {
|
|
3229
|
+
registrations.push(await ctx.session.hook(kind, applyRequest));
|
|
3230
|
+
}
|
|
3231
|
+
catch (error) {
|
|
3232
|
+
console.error(`[free-pi] could not register the ${kind} hook`, error);
|
|
3233
|
+
}
|
|
3234
|
+
}
|
|
3235
|
+
try {
|
|
3236
|
+
registrations.push(await ctx.session.hook("model.request", async (event) => {
|
|
3237
|
+
const providerID = event.model?.providerID;
|
|
3238
|
+
if (providerID !== PROVIDER_ID && providerID !== DEFENSE_PROVIDER_ID)
|
|
3239
|
+
return;
|
|
3240
|
+
event.headers ??= {};
|
|
3241
|
+
const current = await resolveToken(client);
|
|
3242
|
+
if (current)
|
|
3243
|
+
event.headers.authorization = `Bearer ${current}`;
|
|
3244
|
+
event.headers["x-session-id"] = event.sessionID;
|
|
3245
|
+
event.headers["x-client-version"] = latestClientVersion;
|
|
3246
|
+
if (costExperimentArm)
|
|
3247
|
+
event.headers[COST_EXPERIMENT_HEADER] = costExperimentArm;
|
|
3248
|
+
const messages = Array.isArray(event.messages) ? event.messages : [];
|
|
3249
|
+
const lastUser = [...messages]
|
|
3250
|
+
.reverse()
|
|
3251
|
+
.find((message) => (message?.info?.role ?? message?.role) === "user");
|
|
3252
|
+
const messageID = lastUser?.info?.id ?? lastUser?.id;
|
|
3253
|
+
const input = {
|
|
3254
|
+
sessionID: event.sessionID,
|
|
3255
|
+
model: event.model,
|
|
3256
|
+
agent: event.agent,
|
|
3257
|
+
message: messageID ? { id: messageID } : undefined,
|
|
3258
|
+
};
|
|
3259
|
+
const output = { headers: event.headers };
|
|
3260
|
+
await hooks["chat.headers"]?.(input, output);
|
|
3261
|
+
}));
|
|
3262
|
+
}
|
|
3263
|
+
catch (error) {
|
|
3264
|
+
console.error("[free-pi] could not register the request hook", error);
|
|
3208
3265
|
}
|
|
3209
|
-
registrations.push(await ctx.session.hook("model.request", async (event) => {
|
|
3210
|
-
const providerID = event.model?.providerID;
|
|
3211
|
-
if (providerID !== PROVIDER_ID && providerID !== DEFENSE_PROVIDER_ID)
|
|
3212
|
-
return;
|
|
3213
|
-
event.headers ??= {};
|
|
3214
|
-
const current = await resolveToken(client);
|
|
3215
|
-
if (current)
|
|
3216
|
-
event.headers.authorization = `Bearer ${current}`;
|
|
3217
|
-
event.headers["x-session-id"] = event.sessionID;
|
|
3218
|
-
event.headers["x-client-version"] = latestClientVersion;
|
|
3219
|
-
if (costExperimentArm)
|
|
3220
|
-
event.headers[COST_EXPERIMENT_HEADER] = costExperimentArm;
|
|
3221
|
-
const messages = Array.isArray(event.messages) ? event.messages : [];
|
|
3222
|
-
const lastUser = [...messages]
|
|
3223
|
-
.reverse()
|
|
3224
|
-
.find((message) => (message?.info?.role ?? message?.role) === "user");
|
|
3225
|
-
const messageID = lastUser?.info?.id ?? lastUser?.id;
|
|
3226
|
-
const input = {
|
|
3227
|
-
sessionID: event.sessionID,
|
|
3228
|
-
model: event.model,
|
|
3229
|
-
agent: event.agent,
|
|
3230
|
-
message: messageID ? { id: messageID } : undefined,
|
|
3231
|
-
};
|
|
3232
|
-
const output = { headers: event.headers };
|
|
3233
|
-
await hooks["chat.headers"]?.(input, output);
|
|
3234
|
-
}));
|
|
3235
3266
|
return async () => {
|
|
3236
3267
|
for (const registration of registrations.reverse()) {
|
|
3237
3268
|
await registration.dispose?.().catch(() => undefined);
|
package/package.json
CHANGED
package/scripts/postinstall.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { copyFile, mkdir } from "node:fs/promises";
|
|
1
|
+
import { copyFile, mkdir, readFile, rm } from "node:fs/promises";
|
|
2
2
|
import { existsSync } from "node:fs";
|
|
3
3
|
import { homedir } from "node:os";
|
|
4
4
|
import { dirname, join, resolve } from "node:path";
|
|
@@ -9,7 +9,9 @@ const source = join(packageRoot, "dist", "index.js");
|
|
|
9
9
|
const isGlobal = process.env.npm_config_global === "true";
|
|
10
10
|
|
|
11
11
|
if (!isGlobal && process.env.FREEPI_CONFIGURE !== "1") {
|
|
12
|
-
console.log(
|
|
12
|
+
console.log(
|
|
13
|
+
"free-pi: instalación local; usa `npm i -g` o FREEPI_CONFIGURE=1 para registrar el plugin.",
|
|
14
|
+
);
|
|
13
15
|
process.exit(0);
|
|
14
16
|
}
|
|
15
17
|
|
|
@@ -32,8 +34,31 @@ function display(file) {
|
|
|
32
34
|
return file.replace(home, "~");
|
|
33
35
|
}
|
|
34
36
|
|
|
37
|
+
const configNames = ["opencode.json", "opencode.jsonc", "opencode.json5"];
|
|
38
|
+
let configured = false;
|
|
39
|
+
for (const name of configNames) {
|
|
40
|
+
const file = join(configDir, name);
|
|
41
|
+
if (!existsSync(file)) continue;
|
|
42
|
+
try {
|
|
43
|
+
const text = await readFile(file, "utf8");
|
|
44
|
+
configured = /@eduardo191labs\/opencode-free-pi/.test(text);
|
|
45
|
+
} catch {}
|
|
46
|
+
if (configured) break;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (configured) {
|
|
50
|
+
await rm(target, { force: true });
|
|
51
|
+
console.log(
|
|
52
|
+
"free-pi: el paquete ya está configurado en OpenCode; se omitió el plugin local.",
|
|
53
|
+
);
|
|
54
|
+
console.log("free-pi: reinicia OpenCode y ejecuta `opencode plugin list`.");
|
|
55
|
+
process.exit(0);
|
|
56
|
+
}
|
|
57
|
+
|
|
35
58
|
await mkdir(pluginDir, { recursive: true });
|
|
36
59
|
await copyFile(source, target);
|
|
37
60
|
|
|
38
61
|
console.log(`free-pi: plugin instalado en ${display(target)}`);
|
|
39
|
-
console.log(
|
|
62
|
+
console.log(
|
|
63
|
+
"free-pi: reinicia OpenCode y ejecuta `opencode auth login -p free-pi`.",
|
|
64
|
+
);
|
package/src/index.ts
CHANGED
|
@@ -3772,11 +3772,18 @@ function v2Client(ctx: any): any {
|
|
|
3772
3772
|
};
|
|
3773
3773
|
}
|
|
3774
3774
|
|
|
3775
|
+
function settle<T>(promise: Promise<T>, fallback: T): Promise<T> {
|
|
3776
|
+
return Promise.race([
|
|
3777
|
+
promise,
|
|
3778
|
+
new Promise<T>((resolve) => setTimeout(() => resolve(fallback), 6000)),
|
|
3779
|
+
]);
|
|
3780
|
+
}
|
|
3781
|
+
|
|
3775
3782
|
async function FreePiV2Setup(ctx: any): Promise<() => Promise<void>> {
|
|
3776
3783
|
const includeDefense = ctx.options?.includeDefense !== false;
|
|
3777
3784
|
const [catalog, registry] = await Promise.all([
|
|
3778
|
-
fetchCatalog(),
|
|
3779
|
-
fetchModelsRegistry(),
|
|
3785
|
+
settle(fetchCatalog(), undefined),
|
|
3786
|
+
settle(fetchModelsRegistry(), undefined),
|
|
3780
3787
|
]);
|
|
3781
3788
|
const { main, defense } = partitionModels(catalog);
|
|
3782
3789
|
const candidates = modelRegistryCandidates(registry);
|
|
@@ -3801,36 +3808,52 @@ async function FreePiV2Setup(ctx: any): Promise<() => Promise<void>> {
|
|
|
3801
3808
|
}
|
|
3802
3809
|
});
|
|
3803
3810
|
|
|
3804
|
-
|
|
3805
|
-
|
|
3806
|
-
const
|
|
3807
|
-
|
|
3808
|
-
|
|
3809
|
-
|
|
3810
|
-
|
|
3811
|
-
|
|
3812
|
-
|
|
3813
|
-
|
|
3814
|
-
|
|
3815
|
-
|
|
3816
|
-
|
|
3817
|
-
|
|
3818
|
-
|
|
3819
|
-
|
|
3820
|
-
|
|
3821
|
-
|
|
3822
|
-
|
|
3823
|
-
|
|
3824
|
-
|
|
3825
|
-
|
|
3826
|
-
|
|
3827
|
-
|
|
3828
|
-
|
|
3829
|
-
|
|
3830
|
-
|
|
3831
|
-
|
|
3832
|
-
|
|
3833
|
-
|
|
3811
|
+
try {
|
|
3812
|
+
await ctx.tool.transform((editor: any) => {
|
|
3813
|
+
for (const [name, definition] of Object.entries<any>(hooks.tool ?? {})) {
|
|
3814
|
+
const args =
|
|
3815
|
+
definition.args && typeof definition.args === "object"
|
|
3816
|
+
? definition.args
|
|
3817
|
+
: {};
|
|
3818
|
+
let input: any = {
|
|
3819
|
+
type: "object",
|
|
3820
|
+
properties: {},
|
|
3821
|
+
additionalProperties: true,
|
|
3822
|
+
};
|
|
3823
|
+
try {
|
|
3824
|
+
input = (tool.schema as any).toJSONSchema(tool.schema.object(args));
|
|
3825
|
+
delete input.$schema;
|
|
3826
|
+
} catch (error) {
|
|
3827
|
+
console.error(
|
|
3828
|
+
`[free-pi] could not build the schema for ${name}; using a permissive schema`,
|
|
3829
|
+
error,
|
|
3830
|
+
);
|
|
3831
|
+
}
|
|
3832
|
+
editor.add({
|
|
3833
|
+
name,
|
|
3834
|
+
description: definition.description ?? name,
|
|
3835
|
+
input,
|
|
3836
|
+
execute: async (value: any, context: any) => {
|
|
3837
|
+
const result = await definition.execute(value, {
|
|
3838
|
+
sessionID: context.sessionID,
|
|
3839
|
+
messageID: context.messageID,
|
|
3840
|
+
agent: context.agent,
|
|
3841
|
+
id: context.id,
|
|
3842
|
+
directory: ctx.location?.directory ?? process.cwd(),
|
|
3843
|
+
worktree: ctx.location?.directory ?? process.cwd(),
|
|
3844
|
+
abort: context.signal,
|
|
3845
|
+
});
|
|
3846
|
+
return {
|
|
3847
|
+
content: result?.output ?? result?.title ?? "",
|
|
3848
|
+
metadata: result?.metadata,
|
|
3849
|
+
};
|
|
3850
|
+
},
|
|
3851
|
+
});
|
|
3852
|
+
}
|
|
3853
|
+
});
|
|
3854
|
+
} catch (error) {
|
|
3855
|
+
console.error("[free-pi] could not register tools in OpenCode 2", error);
|
|
3856
|
+
}
|
|
3834
3857
|
|
|
3835
3858
|
const applyRequest = async (event: any) => {
|
|
3836
3859
|
const providerID = event.model?.providerID;
|
|
@@ -3861,38 +3884,46 @@ async function FreePiV2Setup(ctx: any): Promise<() => Promise<void>> {
|
|
|
3861
3884
|
};
|
|
3862
3885
|
|
|
3863
3886
|
for (const kind of ["context", "compaction", "generate", "title"]) {
|
|
3864
|
-
|
|
3887
|
+
try {
|
|
3888
|
+
registrations.push(await ctx.session.hook(kind, applyRequest));
|
|
3889
|
+
} catch (error) {
|
|
3890
|
+
console.error(`[free-pi] could not register the ${kind} hook`, error);
|
|
3891
|
+
}
|
|
3865
3892
|
}
|
|
3866
3893
|
|
|
3867
|
-
|
|
3868
|
-
|
|
3869
|
-
|
|
3870
|
-
|
|
3871
|
-
|
|
3872
|
-
|
|
3873
|
-
|
|
3874
|
-
|
|
3875
|
-
|
|
3876
|
-
|
|
3877
|
-
|
|
3878
|
-
|
|
3879
|
-
|
|
3880
|
-
|
|
3881
|
-
|
|
3882
|
-
|
|
3883
|
-
(
|
|
3884
|
-
|
|
3885
|
-
|
|
3886
|
-
|
|
3887
|
-
|
|
3888
|
-
|
|
3889
|
-
|
|
3890
|
-
|
|
3891
|
-
|
|
3892
|
-
|
|
3893
|
-
|
|
3894
|
-
|
|
3895
|
-
|
|
3894
|
+
try {
|
|
3895
|
+
registrations.push(
|
|
3896
|
+
await ctx.session.hook("model.request", async (event: any) => {
|
|
3897
|
+
const providerID = event.model?.providerID;
|
|
3898
|
+
if (providerID !== PROVIDER_ID && providerID !== DEFENSE_PROVIDER_ID)
|
|
3899
|
+
return;
|
|
3900
|
+
event.headers ??= {};
|
|
3901
|
+
const current = await resolveToken(client);
|
|
3902
|
+
if (current) event.headers.authorization = `Bearer ${current}`;
|
|
3903
|
+
event.headers["x-session-id"] = event.sessionID;
|
|
3904
|
+
event.headers["x-client-version"] = latestClientVersion;
|
|
3905
|
+
if (costExperimentArm)
|
|
3906
|
+
event.headers[COST_EXPERIMENT_HEADER] = costExperimentArm;
|
|
3907
|
+
const messages = Array.isArray(event.messages) ? event.messages : [];
|
|
3908
|
+
const lastUser = [...messages]
|
|
3909
|
+
.reverse()
|
|
3910
|
+
.find(
|
|
3911
|
+
(message: any) => (message?.info?.role ?? message?.role) === "user",
|
|
3912
|
+
);
|
|
3913
|
+
const messageID = lastUser?.info?.id ?? lastUser?.id;
|
|
3914
|
+
const input = {
|
|
3915
|
+
sessionID: event.sessionID,
|
|
3916
|
+
model: event.model,
|
|
3917
|
+
agent: event.agent,
|
|
3918
|
+
message: messageID ? { id: messageID } : undefined,
|
|
3919
|
+
};
|
|
3920
|
+
const output = { headers: event.headers };
|
|
3921
|
+
await hooks["chat.headers"]?.(input, output);
|
|
3922
|
+
}),
|
|
3923
|
+
);
|
|
3924
|
+
} catch (error) {
|
|
3925
|
+
console.error("[free-pi] could not register the request hook", error);
|
|
3926
|
+
}
|
|
3896
3927
|
|
|
3897
3928
|
return async () => {
|
|
3898
3929
|
for (const registration of registrations.reverse()) {
|