@eduardo191labs/opencode-free-pi 0.2.2 → 0.2.4
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 +33 -12
- package/dist/index.d.ts +2 -0
- package/dist/index.js +189 -1
- package/package.json +1 -1
- package/src/index.ts +225 -1
package/README.md
CHANGED
|
@@ -8,22 +8,42 @@ y Termux**.
|
|
|
8
8
|
|
|
9
9
|
```bash
|
|
10
10
|
npm i -g @eduardo191labs/opencode-free-pi
|
|
11
|
-
opencode plugin
|
|
11
|
+
opencode plugin --global @eduardo191labs/opencode-free-pi
|
|
12
12
|
```
|
|
13
13
|
|
|
14
|
-
El primer comando instala el paquete y sus dependencias. El segundo
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
El primer comando instala el paquete y sus dependencias. El segundo instala
|
|
15
|
+
las dependencias dentro de la configuración de OpenCode, escribe la entrada
|
|
16
|
+
`plugin` y es la forma recomendada, porque npm 12 bloquea los scripts de
|
|
17
|
+
instalación por defecto. En versiones nuevas de OpenCode el subcomando es
|
|
18
|
+
`opencode plugin add`.
|
|
17
19
|
|
|
18
20
|
El `postinstall` del paquete copia el plugin a
|
|
19
21
|
`~/.config/opencode/plugins/opencode-free-pi.ts` (en Windows,
|
|
20
22
|
`%USERPROFILE%\.config\opencode\plugins\opencode-free-pi.ts`) para que
|
|
21
23
|
OpenCode lo descubra automáticamente. Si tu npm bloquea scripts, ejecuta
|
|
22
|
-
también `opencode plugin
|
|
24
|
+
también `opencode plugin --global @eduardo191labs/opencode-free-pi` (o
|
|
25
|
+
`opencode plugin add ...` en versiones nuevas).
|
|
23
26
|
|
|
24
27
|
Para una instalación local sin `-g`, fuerza el registro con
|
|
25
28
|
`FREEPI_CONFIGURE=1 npm install`.
|
|
26
29
|
|
|
30
|
+
### OpenCode 2
|
|
31
|
+
|
|
32
|
+
OpenCode 2 usa la entrada `plugins` (plural) y carga el paquete desde su
|
|
33
|
+
`exports`. Este paquete exporta las dos formas: el `server` de OpenCode 1.x y
|
|
34
|
+
el `setup` de OpenCode 2, así que el mismo paquete funciona en ambos.
|
|
35
|
+
|
|
36
|
+
```jsonc
|
|
37
|
+
{
|
|
38
|
+
"plugins": ["@eduardo191labs/opencode-free-pi"],
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
En OpenCode 2 el proveedor y las herramientas se registran igual. Las
|
|
43
|
+
credenciales se leen de `FREEPI_JWT` o de
|
|
44
|
+
`~/.free-pi/agent/credentials.json`; el flujo interactivo
|
|
45
|
+
`opencode auth login -p free-pi` corresponde a OpenCode 1.x.
|
|
46
|
+
|
|
27
47
|
## Instalación rápida con script
|
|
28
48
|
|
|
29
49
|
> Pasa el cursor por el bloque de comandos y usa el icono de copiar de GitHub.
|
|
@@ -114,25 +134,26 @@ El paquete incluye `dist/index.js` ya compilado y tipos TypeScript:
|
|
|
114
134
|
|
|
115
135
|
```bash
|
|
116
136
|
npm pack
|
|
117
|
-
npm install -g ./eduardo191labs-opencode-free-pi
|
|
137
|
+
npm install -g ./eduardo191labs-opencode-free-pi-*.tgz
|
|
118
138
|
```
|
|
119
139
|
|
|
120
140
|
También puedes declararlo en OpenCode como plugin npm:
|
|
121
141
|
|
|
122
142
|
```json
|
|
123
143
|
{
|
|
124
|
-
"
|
|
144
|
+
"plugin": ["@eduardo191labs/opencode-free-pi"]
|
|
125
145
|
}
|
|
126
146
|
```
|
|
127
147
|
|
|
128
|
-
|
|
129
|
-
|
|
148
|
+
El comando `opencode plugin` es la instalación recomendada. El script sigue
|
|
149
|
+
siendo útil si prefieres copiar el archivo directamente a la carpeta de plugins.
|
|
130
150
|
|
|
131
151
|
## Publicación automática
|
|
132
152
|
|
|
133
|
-
Cada push a `main` ejecuta `.github/workflows/publish.yml`,
|
|
134
|
-
versión en npm, sube un patch si ya existe, publica el
|
|
135
|
-
`chore(release): vX.Y.Z [skip ci]` con el tag
|
|
153
|
+
Cada push a `main` ejecuta `.github/workflows/publish.yml`, compila, empaqueta,
|
|
154
|
+
comprueba la última versión en npm, sube un patch si ya existe, publica el
|
|
155
|
+
paquete y crea el commit `chore(release): vX.Y.Z [skip ci]` con el tag
|
|
156
|
+
`vX.Y.Z`. El workflow también actualiza `dist/` en ese commit.
|
|
136
157
|
|
|
137
158
|
El workflow usa el secret `NPM_TOKEN` del repositorio. Para una prueba sin
|
|
138
159
|
publicar, ejecuta el workflow manualmente con `publish = false`.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { type Plugin } from "@opencode-ai/plugin";
|
|
2
2
|
export declare const FreePiPlugin: Plugin;
|
|
3
|
+
declare function FreePiV2Setup(ctx: any): Promise<() => Promise<void>>;
|
|
3
4
|
declare const _default: {
|
|
4
5
|
id: string;
|
|
6
|
+
setup: typeof FreePiV2Setup;
|
|
5
7
|
server: Plugin;
|
|
6
8
|
};
|
|
7
9
|
export default _default;
|
package/dist/index.js
CHANGED
|
@@ -3055,4 +3055,192 @@ export const FreePiPlugin = async ({ client }, options) => {
|
|
|
3055
3055
|
},
|
|
3056
3056
|
};
|
|
3057
3057
|
};
|
|
3058
|
-
|
|
3058
|
+
function v2Models(providerID, models) {
|
|
3059
|
+
return Object.values(models).map((model) => {
|
|
3060
|
+
const cost = model.cost ?? {};
|
|
3061
|
+
const released = Date.parse(model.release_date ?? "");
|
|
3062
|
+
return {
|
|
3063
|
+
id: model.id,
|
|
3064
|
+
modelID: model.id,
|
|
3065
|
+
providerID,
|
|
3066
|
+
name: model.name ?? model.id,
|
|
3067
|
+
...(model.family ? { family: model.family } : {}),
|
|
3068
|
+
capabilities: {
|
|
3069
|
+
tools: model.tool_call !== false,
|
|
3070
|
+
input: model.modalities?.input ?? ["text"],
|
|
3071
|
+
output: model.modalities?.output ?? ["text"],
|
|
3072
|
+
...(model.temperature === undefined
|
|
3073
|
+
? {}
|
|
3074
|
+
: { temperature: model.temperature }),
|
|
3075
|
+
},
|
|
3076
|
+
variants: Object.entries(model.variants ?? {}).map(([id, settings]) => ({
|
|
3077
|
+
id,
|
|
3078
|
+
settings,
|
|
3079
|
+
})),
|
|
3080
|
+
time: { released: Number.isFinite(released) ? released : 0 },
|
|
3081
|
+
cost: [
|
|
3082
|
+
{
|
|
3083
|
+
input: cost.input ?? 0,
|
|
3084
|
+
output: cost.output ?? 0,
|
|
3085
|
+
cache: {
|
|
3086
|
+
read: cost.cache_read ?? cost.input ?? 0,
|
|
3087
|
+
write: cost.cache_write ?? cost.input ?? 0,
|
|
3088
|
+
},
|
|
3089
|
+
},
|
|
3090
|
+
],
|
|
3091
|
+
status: model.status === "deprecated" ? "deprecated" : "active",
|
|
3092
|
+
enabled: true,
|
|
3093
|
+
limit: {
|
|
3094
|
+
context: model.limit?.context ?? CONTEXT_WINDOW,
|
|
3095
|
+
output: model.limit?.output ?? MAX_OUTPUT_TOKENS,
|
|
3096
|
+
},
|
|
3097
|
+
};
|
|
3098
|
+
});
|
|
3099
|
+
}
|
|
3100
|
+
function v2Provider(providerID, name, models, token) {
|
|
3101
|
+
return {
|
|
3102
|
+
info: {
|
|
3103
|
+
id: providerID,
|
|
3104
|
+
name,
|
|
3105
|
+
activation: "enabled",
|
|
3106
|
+
package: "@opencode/ai/providers/openai-compatible",
|
|
3107
|
+
settings: {
|
|
3108
|
+
baseURL: completionsBaseUrl(),
|
|
3109
|
+
apiKey: token ?? "free-pi",
|
|
3110
|
+
},
|
|
3111
|
+
},
|
|
3112
|
+
models: v2Models(providerID, models),
|
|
3113
|
+
};
|
|
3114
|
+
}
|
|
3115
|
+
function v2Client(ctx) {
|
|
3116
|
+
const context = async (sessionID) => {
|
|
3117
|
+
try {
|
|
3118
|
+
return await ctx.session.context({ sessionID });
|
|
3119
|
+
}
|
|
3120
|
+
catch {
|
|
3121
|
+
return [];
|
|
3122
|
+
}
|
|
3123
|
+
};
|
|
3124
|
+
return {
|
|
3125
|
+
app: { log: async () => undefined },
|
|
3126
|
+
auth: { set: async () => undefined },
|
|
3127
|
+
tui: { showToast: async () => undefined },
|
|
3128
|
+
session: {
|
|
3129
|
+
messages: async ({ path }) => ({ data: await context(path.id) }),
|
|
3130
|
+
message: async ({ path }) => {
|
|
3131
|
+
const messages = await context(path.id);
|
|
3132
|
+
return {
|
|
3133
|
+
data: messages.find((message) => (message?.info?.id ?? message?.id) === path.messageID),
|
|
3134
|
+
};
|
|
3135
|
+
},
|
|
3136
|
+
},
|
|
3137
|
+
};
|
|
3138
|
+
}
|
|
3139
|
+
async function FreePiV2Setup(ctx) {
|
|
3140
|
+
const includeDefense = ctx.options?.includeDefense !== false;
|
|
3141
|
+
const [catalog, registry] = await Promise.all([
|
|
3142
|
+
fetchCatalog(),
|
|
3143
|
+
fetchModelsRegistry(),
|
|
3144
|
+
]);
|
|
3145
|
+
const { main, defense } = partitionModels(catalog);
|
|
3146
|
+
const candidates = modelRegistryCandidates(registry);
|
|
3147
|
+
const token = await readCredentialsToken();
|
|
3148
|
+
const client = v2Client(ctx);
|
|
3149
|
+
const hooks = await FreePiPlugin({ client }, ctx.options);
|
|
3150
|
+
const registrations = [];
|
|
3151
|
+
await ctx.provider.transform((editor) => {
|
|
3152
|
+
editor.add(v2Provider(PROVIDER_ID, "Free Pi", buildModels(main, candidates), token));
|
|
3153
|
+
if (includeDefense && defense.length > 0) {
|
|
3154
|
+
editor.add(v2Provider(DEFENSE_PROVIDER_ID, "Free Pi Defense", buildModels(defense, candidates), token));
|
|
3155
|
+
}
|
|
3156
|
+
});
|
|
3157
|
+
await ctx.tool.transform((editor) => {
|
|
3158
|
+
for (const [name, definition] of Object.entries(hooks.tool ?? {})) {
|
|
3159
|
+
const args = definition.args && typeof definition.args === "object"
|
|
3160
|
+
? definition.args
|
|
3161
|
+
: {};
|
|
3162
|
+
const input = tool.schema.toJSONSchema(tool.schema.object(args));
|
|
3163
|
+
delete input.$schema;
|
|
3164
|
+
editor.add({
|
|
3165
|
+
name,
|
|
3166
|
+
description: definition.description ?? name,
|
|
3167
|
+
input,
|
|
3168
|
+
execute: async (value, context) => {
|
|
3169
|
+
const result = await definition.execute(value, {
|
|
3170
|
+
sessionID: context.sessionID,
|
|
3171
|
+
messageID: context.messageID,
|
|
3172
|
+
agent: context.agent,
|
|
3173
|
+
id: context.id,
|
|
3174
|
+
directory: ctx.location?.directory ?? process.cwd(),
|
|
3175
|
+
worktree: ctx.location?.directory ?? process.cwd(),
|
|
3176
|
+
abort: context.signal,
|
|
3177
|
+
});
|
|
3178
|
+
return {
|
|
3179
|
+
content: result?.output ?? result?.title ?? "",
|
|
3180
|
+
metadata: result?.metadata,
|
|
3181
|
+
};
|
|
3182
|
+
},
|
|
3183
|
+
});
|
|
3184
|
+
}
|
|
3185
|
+
});
|
|
3186
|
+
const applyRequest = async (event) => {
|
|
3187
|
+
const providerID = event.model?.providerID;
|
|
3188
|
+
if (providerID !== PROVIDER_ID && providerID !== DEFENSE_PROVIDER_ID)
|
|
3189
|
+
return;
|
|
3190
|
+
const output = {
|
|
3191
|
+
maxOutputTokens: event.options?.maxTokens,
|
|
3192
|
+
headers: event.headers ?? {},
|
|
3193
|
+
};
|
|
3194
|
+
await hooks["chat.params"]?.({ sessionID: event.sessionID, model: event.model }, output);
|
|
3195
|
+
if (output.maxOutputTokens !== undefined) {
|
|
3196
|
+
event.options = {
|
|
3197
|
+
...(event.options ?? {}),
|
|
3198
|
+
maxTokens: output.maxOutputTokens,
|
|
3199
|
+
};
|
|
3200
|
+
}
|
|
3201
|
+
if (costExperimentArm === "efficiency" &&
|
|
3202
|
+
!event.system?.some((part) => part.type === "text" && part.text === EFFICIENCY_RULE)) {
|
|
3203
|
+
event.system?.push({ type: "text", text: EFFICIENCY_RULE });
|
|
3204
|
+
}
|
|
3205
|
+
};
|
|
3206
|
+
for (const kind of ["context", "compaction", "generate", "title"]) {
|
|
3207
|
+
registrations.push(await ctx.session.hook(kind, applyRequest));
|
|
3208
|
+
}
|
|
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
|
+
return async () => {
|
|
3236
|
+
for (const registration of registrations.reverse()) {
|
|
3237
|
+
await registration.dispose?.().catch(() => undefined);
|
|
3238
|
+
}
|
|
3239
|
+
await hooks.dispose?.().catch(() => undefined);
|
|
3240
|
+
};
|
|
3241
|
+
}
|
|
3242
|
+
export default {
|
|
3243
|
+
id: PROVIDER_ID,
|
|
3244
|
+
setup: FreePiV2Setup,
|
|
3245
|
+
server: FreePiPlugin,
|
|
3246
|
+
};
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -3682,4 +3682,228 @@ export const FreePiPlugin: Plugin = async ({ client }, options) => {
|
|
|
3682
3682
|
};
|
|
3683
3683
|
};
|
|
3684
3684
|
|
|
3685
|
-
|
|
3685
|
+
function v2Models(providerID: string, models: ModelMap): any[] {
|
|
3686
|
+
return Object.values(models).map((model: any) => {
|
|
3687
|
+
const cost = model.cost ?? {};
|
|
3688
|
+
const released = Date.parse(model.release_date ?? "");
|
|
3689
|
+
return {
|
|
3690
|
+
id: model.id,
|
|
3691
|
+
modelID: model.id,
|
|
3692
|
+
providerID,
|
|
3693
|
+
name: model.name ?? model.id,
|
|
3694
|
+
...(model.family ? { family: model.family } : {}),
|
|
3695
|
+
capabilities: {
|
|
3696
|
+
tools: model.tool_call !== false,
|
|
3697
|
+
input: model.modalities?.input ?? ["text"],
|
|
3698
|
+
output: model.modalities?.output ?? ["text"],
|
|
3699
|
+
...(model.temperature === undefined
|
|
3700
|
+
? {}
|
|
3701
|
+
: { temperature: model.temperature }),
|
|
3702
|
+
},
|
|
3703
|
+
variants: Object.entries(model.variants ?? {}).map(([id, settings]) => ({
|
|
3704
|
+
id,
|
|
3705
|
+
settings,
|
|
3706
|
+
})),
|
|
3707
|
+
time: { released: Number.isFinite(released) ? released : 0 },
|
|
3708
|
+
cost: [
|
|
3709
|
+
{
|
|
3710
|
+
input: cost.input ?? 0,
|
|
3711
|
+
output: cost.output ?? 0,
|
|
3712
|
+
cache: {
|
|
3713
|
+
read: cost.cache_read ?? cost.input ?? 0,
|
|
3714
|
+
write: cost.cache_write ?? cost.input ?? 0,
|
|
3715
|
+
},
|
|
3716
|
+
},
|
|
3717
|
+
],
|
|
3718
|
+
status: model.status === "deprecated" ? "deprecated" : "active",
|
|
3719
|
+
enabled: true,
|
|
3720
|
+
limit: {
|
|
3721
|
+
context: model.limit?.context ?? CONTEXT_WINDOW,
|
|
3722
|
+
output: model.limit?.output ?? MAX_OUTPUT_TOKENS,
|
|
3723
|
+
},
|
|
3724
|
+
};
|
|
3725
|
+
});
|
|
3726
|
+
}
|
|
3727
|
+
|
|
3728
|
+
function v2Provider(
|
|
3729
|
+
providerID: string,
|
|
3730
|
+
name: string,
|
|
3731
|
+
models: ModelMap,
|
|
3732
|
+
token: string | undefined,
|
|
3733
|
+
): any {
|
|
3734
|
+
return {
|
|
3735
|
+
info: {
|
|
3736
|
+
id: providerID,
|
|
3737
|
+
name,
|
|
3738
|
+
activation: "enabled",
|
|
3739
|
+
package: "@opencode/ai/providers/openai-compatible",
|
|
3740
|
+
settings: {
|
|
3741
|
+
baseURL: completionsBaseUrl(),
|
|
3742
|
+
apiKey: token ?? "free-pi",
|
|
3743
|
+
},
|
|
3744
|
+
},
|
|
3745
|
+
models: v2Models(providerID, models),
|
|
3746
|
+
};
|
|
3747
|
+
}
|
|
3748
|
+
|
|
3749
|
+
function v2Client(ctx: any): any {
|
|
3750
|
+
const context = async (sessionID: string) => {
|
|
3751
|
+
try {
|
|
3752
|
+
return await ctx.session.context({ sessionID });
|
|
3753
|
+
} catch {
|
|
3754
|
+
return [];
|
|
3755
|
+
}
|
|
3756
|
+
};
|
|
3757
|
+
return {
|
|
3758
|
+
app: { log: async () => undefined },
|
|
3759
|
+
auth: { set: async () => undefined },
|
|
3760
|
+
tui: { showToast: async () => undefined },
|
|
3761
|
+
session: {
|
|
3762
|
+
messages: async ({ path }: any) => ({ data: await context(path.id) }),
|
|
3763
|
+
message: async ({ path }: any) => {
|
|
3764
|
+
const messages = await context(path.id);
|
|
3765
|
+
return {
|
|
3766
|
+
data: (messages as any[]).find(
|
|
3767
|
+
(message) => (message?.info?.id ?? message?.id) === path.messageID,
|
|
3768
|
+
),
|
|
3769
|
+
};
|
|
3770
|
+
},
|
|
3771
|
+
},
|
|
3772
|
+
};
|
|
3773
|
+
}
|
|
3774
|
+
|
|
3775
|
+
async function FreePiV2Setup(ctx: any): Promise<() => Promise<void>> {
|
|
3776
|
+
const includeDefense = ctx.options?.includeDefense !== false;
|
|
3777
|
+
const [catalog, registry] = await Promise.all([
|
|
3778
|
+
fetchCatalog(),
|
|
3779
|
+
fetchModelsRegistry(),
|
|
3780
|
+
]);
|
|
3781
|
+
const { main, defense } = partitionModels(catalog);
|
|
3782
|
+
const candidates = modelRegistryCandidates(registry);
|
|
3783
|
+
const token = await readCredentialsToken();
|
|
3784
|
+
const client = v2Client(ctx);
|
|
3785
|
+
const hooks: any = await (FreePiPlugin as any)({ client }, ctx.options);
|
|
3786
|
+
const registrations: any[] = [];
|
|
3787
|
+
|
|
3788
|
+
await ctx.provider.transform((editor: any) => {
|
|
3789
|
+
editor.add(
|
|
3790
|
+
v2Provider(PROVIDER_ID, "Free Pi", buildModels(main, candidates), token),
|
|
3791
|
+
);
|
|
3792
|
+
if (includeDefense && defense.length > 0) {
|
|
3793
|
+
editor.add(
|
|
3794
|
+
v2Provider(
|
|
3795
|
+
DEFENSE_PROVIDER_ID,
|
|
3796
|
+
"Free Pi Defense",
|
|
3797
|
+
buildModels(defense, candidates),
|
|
3798
|
+
token,
|
|
3799
|
+
),
|
|
3800
|
+
);
|
|
3801
|
+
}
|
|
3802
|
+
});
|
|
3803
|
+
|
|
3804
|
+
await ctx.tool.transform((editor: any) => {
|
|
3805
|
+
for (const [name, definition] of Object.entries<any>(hooks.tool ?? {})) {
|
|
3806
|
+
const args =
|
|
3807
|
+
definition.args && typeof definition.args === "object"
|
|
3808
|
+
? definition.args
|
|
3809
|
+
: {};
|
|
3810
|
+
const input = (tool.schema as any).toJSONSchema(tool.schema.object(args));
|
|
3811
|
+
delete input.$schema;
|
|
3812
|
+
editor.add({
|
|
3813
|
+
name,
|
|
3814
|
+
description: definition.description ?? name,
|
|
3815
|
+
input,
|
|
3816
|
+
execute: async (value: any, context: any) => {
|
|
3817
|
+
const result = await definition.execute(value, {
|
|
3818
|
+
sessionID: context.sessionID,
|
|
3819
|
+
messageID: context.messageID,
|
|
3820
|
+
agent: context.agent,
|
|
3821
|
+
id: context.id,
|
|
3822
|
+
directory: ctx.location?.directory ?? process.cwd(),
|
|
3823
|
+
worktree: ctx.location?.directory ?? process.cwd(),
|
|
3824
|
+
abort: context.signal,
|
|
3825
|
+
});
|
|
3826
|
+
return {
|
|
3827
|
+
content: result?.output ?? result?.title ?? "",
|
|
3828
|
+
metadata: result?.metadata,
|
|
3829
|
+
};
|
|
3830
|
+
},
|
|
3831
|
+
});
|
|
3832
|
+
}
|
|
3833
|
+
});
|
|
3834
|
+
|
|
3835
|
+
const applyRequest = async (event: any) => {
|
|
3836
|
+
const providerID = event.model?.providerID;
|
|
3837
|
+
if (providerID !== PROVIDER_ID && providerID !== DEFENSE_PROVIDER_ID)
|
|
3838
|
+
return;
|
|
3839
|
+
const output: any = {
|
|
3840
|
+
maxOutputTokens: event.options?.maxTokens,
|
|
3841
|
+
headers: event.headers ?? {},
|
|
3842
|
+
};
|
|
3843
|
+
await hooks["chat.params"]?.(
|
|
3844
|
+
{ sessionID: event.sessionID, model: event.model },
|
|
3845
|
+
output,
|
|
3846
|
+
);
|
|
3847
|
+
if (output.maxOutputTokens !== undefined) {
|
|
3848
|
+
event.options = {
|
|
3849
|
+
...(event.options ?? {}),
|
|
3850
|
+
maxTokens: output.maxOutputTokens,
|
|
3851
|
+
};
|
|
3852
|
+
}
|
|
3853
|
+
if (
|
|
3854
|
+
costExperimentArm === "efficiency" &&
|
|
3855
|
+
!event.system?.some(
|
|
3856
|
+
(part: any) => part.type === "text" && part.text === EFFICIENCY_RULE,
|
|
3857
|
+
)
|
|
3858
|
+
) {
|
|
3859
|
+
event.system?.push({ type: "text", text: EFFICIENCY_RULE });
|
|
3860
|
+
}
|
|
3861
|
+
};
|
|
3862
|
+
|
|
3863
|
+
for (const kind of ["context", "compaction", "generate", "title"]) {
|
|
3864
|
+
registrations.push(await ctx.session.hook(kind, applyRequest));
|
|
3865
|
+
}
|
|
3866
|
+
|
|
3867
|
+
registrations.push(
|
|
3868
|
+
await ctx.session.hook("model.request", async (event: any) => {
|
|
3869
|
+
const providerID = event.model?.providerID;
|
|
3870
|
+
if (providerID !== PROVIDER_ID && providerID !== DEFENSE_PROVIDER_ID)
|
|
3871
|
+
return;
|
|
3872
|
+
event.headers ??= {};
|
|
3873
|
+
const current = await resolveToken(client);
|
|
3874
|
+
if (current) event.headers.authorization = `Bearer ${current}`;
|
|
3875
|
+
event.headers["x-session-id"] = event.sessionID;
|
|
3876
|
+
event.headers["x-client-version"] = latestClientVersion;
|
|
3877
|
+
if (costExperimentArm)
|
|
3878
|
+
event.headers[COST_EXPERIMENT_HEADER] = costExperimentArm;
|
|
3879
|
+
const messages = Array.isArray(event.messages) ? event.messages : [];
|
|
3880
|
+
const lastUser = [...messages]
|
|
3881
|
+
.reverse()
|
|
3882
|
+
.find(
|
|
3883
|
+
(message: any) => (message?.info?.role ?? message?.role) === "user",
|
|
3884
|
+
);
|
|
3885
|
+
const messageID = lastUser?.info?.id ?? lastUser?.id;
|
|
3886
|
+
const input = {
|
|
3887
|
+
sessionID: event.sessionID,
|
|
3888
|
+
model: event.model,
|
|
3889
|
+
agent: event.agent,
|
|
3890
|
+
message: messageID ? { id: messageID } : undefined,
|
|
3891
|
+
};
|
|
3892
|
+
const output = { headers: event.headers };
|
|
3893
|
+
await hooks["chat.headers"]?.(input, output);
|
|
3894
|
+
}),
|
|
3895
|
+
);
|
|
3896
|
+
|
|
3897
|
+
return async () => {
|
|
3898
|
+
for (const registration of registrations.reverse()) {
|
|
3899
|
+
await registration.dispose?.().catch(() => undefined);
|
|
3900
|
+
}
|
|
3901
|
+
await hooks.dispose?.().catch(() => undefined);
|
|
3902
|
+
};
|
|
3903
|
+
}
|
|
3904
|
+
|
|
3905
|
+
export default {
|
|
3906
|
+
id: PROVIDER_ID,
|
|
3907
|
+
setup: FreePiV2Setup,
|
|
3908
|
+
server: FreePiPlugin,
|
|
3909
|
+
};
|