@eduardo191labs/opencode-free-pi 0.2.5 → 0.2.6

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 CHANGED
@@ -42,6 +42,15 @@ La lista debe mostrar `free-pi` como `active`, con la versión del paquete. Si
42
42
  aparece `failed`, ejecuta `opencode --print-logs` y revisa el `cause=` del log;
43
43
  el `ref=` solo no contiene el motivo.
44
44
 
45
+ `opencode plugin add` acumula versiones exactas en `plugins`. Si antes
46
+ instalaste otra versión, la entrada vieja queda como `failed` y no muestra
47
+ `id` ni `version`. Elimínala y deja solo una:
48
+
49
+ ```bash
50
+ opencode plugin remove @eduardo191labs/opencode-free-pi@0.2.3
51
+ opencode plugin list
52
+ ```
53
+
45
54
  El `postinstall` antiguo copiaba `dist/index.js` a
46
55
  `~/.config/opencode/plugins/opencode-free-pi.ts`. Ese archivo local no tiene
47
56
  versión de paquete y puede quedar desactualizado; en OpenCode 2 bórralo para
@@ -55,10 +64,16 @@ Remove-Item "$env:USERPROFILE\.config\opencode\plugins\opencode-free-pi.ts" -For
55
64
  rm -f ~/.config/opencode/plugins/opencode-free-pi.ts
56
65
  ```
57
66
 
58
- En OpenCode 2 el proveedor y las herramientas se registran igual. Las
59
- credenciales se leen de `FREEPI_JWT` o de
60
- `~/.free-pi/agent/credentials.json`; el flujo interactivo
61
- `opencode auth login -p free-pi` corresponde a OpenCode 1.x.
67
+ En OpenCode 2 el proveedor y las herramientas se registran igual. El plugin
68
+ también registra la integración `free-pi`, así que el inicio de sesión
69
+ interactivo funciona en OpenCode 2:
70
+
71
+ ```bash
72
+ opencode auth login free-pi
73
+ ```
74
+
75
+ Puedes elegir GitHub o pegar un JWT. Las credenciales también se leen de
76
+ `FREEPI_JWT` o de `~/.free-pi/agent/credentials.json`.
62
77
 
63
78
  ## Instalación rápida con script
64
79
 
@@ -97,6 +112,15 @@ pkg install curl
97
112
 
98
113
  ## Después de instalar
99
114
 
115
+ OpenCode 2:
116
+
117
+ ```bash
118
+ opencode auth login free-pi
119
+ opencode
120
+ ```
121
+
122
+ OpenCode 1.x:
123
+
100
124
  ```bash
101
125
  opencode auth login -p free-pi
102
126
  opencode
package/dist/index.js CHANGED
@@ -3103,6 +3103,7 @@ function v2Provider(providerID, name, models, token) {
3103
3103
  id: providerID,
3104
3104
  name,
3105
3105
  activation: "enabled",
3106
+ integrationID: PROVIDER_ID,
3106
3107
  package: "@opencode/ai/providers/openai-compatible",
3107
3108
  settings: {
3108
3109
  baseURL: completionsBaseUrl(),
@@ -3112,6 +3113,31 @@ function v2Provider(providerID, name, models, token) {
3112
3113
  models: v2Models(providerID, models),
3113
3114
  };
3114
3115
  }
3116
+ async function v2ConnectionAuth(ctx) {
3117
+ try {
3118
+ const connection = await ctx.integration.connection.active(PROVIDER_ID);
3119
+ if (!connection)
3120
+ return undefined;
3121
+ const credential = await ctx.integration.connection.resolve(connection);
3122
+ if (!credential)
3123
+ return undefined;
3124
+ if (credential.type === "oauth")
3125
+ return {
3126
+ type: "oauth",
3127
+ refresh: credential.refresh,
3128
+ access: credential.access,
3129
+ expires: credential.expires,
3130
+ };
3131
+ if (credential.type === "key")
3132
+ return {
3133
+ type: "api",
3134
+ key: credential.key,
3135
+ metadata: credential.metadata,
3136
+ };
3137
+ }
3138
+ catch { }
3139
+ return undefined;
3140
+ }
3115
3141
  function v2Client(ctx) {
3116
3142
  const context = async (sessionID) => {
3117
3143
  try {
@@ -3150,10 +3176,82 @@ async function FreePiV2Setup(ctx) {
3150
3176
  ]);
3151
3177
  const { main, defense } = partitionModels(catalog);
3152
3178
  const candidates = modelRegistryCandidates(registry);
3153
- const token = await readCredentialsToken();
3154
3179
  const client = v2Client(ctx);
3180
+ const connectionAuth = await v2ConnectionAuth(ctx);
3181
+ let token = tokenFromAuth(connectionAuth) ?? (await readCredentialsToken());
3182
+ authResolver = async () => (await v2ConnectionAuth(ctx)) ?? undefined;
3155
3183
  const hooks = await FreePiPlugin({ client }, ctx.options);
3156
3184
  const registrations = [];
3185
+ const credentialsAbort = new AbortController();
3186
+ void (async () => {
3187
+ try {
3188
+ for await (const _event of ctx.event.subscribe({
3189
+ types: ["credential.updated", "credential.switched"],
3190
+ signal: credentialsAbort.signal,
3191
+ })) {
3192
+ const next = tokenFromAuth(await v2ConnectionAuth(ctx)) ??
3193
+ (await readCredentialsToken());
3194
+ if (next === token)
3195
+ continue;
3196
+ token = next;
3197
+ await ctx.provider.reload().catch(() => undefined);
3198
+ }
3199
+ }
3200
+ catch { }
3201
+ })();
3202
+ try {
3203
+ registrations.push(await ctx.integration.transform((editor) => {
3204
+ editor.update(PROVIDER_ID, (integration) => {
3205
+ integration.name = "Free Pi";
3206
+ });
3207
+ editor.method.update({
3208
+ integrationID: PROVIDER_ID,
3209
+ method: {
3210
+ id: "github",
3211
+ type: "oauth",
3212
+ label: "Sign in with GitHub (free-pi)",
3213
+ },
3214
+ authorize: async () => {
3215
+ const result = await authorizeFreePi(client);
3216
+ if (result.method !== "auto")
3217
+ throw new Error("free-pi sign-in did not start.");
3218
+ return {
3219
+ url: result.url,
3220
+ instructions: result.instructions,
3221
+ mode: "auto",
3222
+ callback: (async () => {
3223
+ const value = await result.callback();
3224
+ if (value.type !== "success")
3225
+ throw new Error("free-pi sign-in failed.");
3226
+ if ("key" in value)
3227
+ return {
3228
+ type: "key",
3229
+ key: value.key,
3230
+ metadata: value.metadata,
3231
+ };
3232
+ return {
3233
+ type: "oauth",
3234
+ methodID: "github",
3235
+ refresh: value.refresh,
3236
+ access: value.access,
3237
+ expires: value.expires,
3238
+ };
3239
+ })(),
3240
+ };
3241
+ },
3242
+ });
3243
+ editor.method.update({
3244
+ integrationID: PROVIDER_ID,
3245
+ method: {
3246
+ type: "key",
3247
+ label: "Paste a free-pi JWT",
3248
+ },
3249
+ });
3250
+ }));
3251
+ }
3252
+ catch (error) {
3253
+ console.error("[free-pi] could not register the OpenCode 2 integration", error);
3254
+ }
3157
3255
  await ctx.provider.transform((editor) => {
3158
3256
  editor.add(v2Provider(PROVIDER_ID, "Free Pi", buildModels(main, candidates), token));
3159
3257
  if (includeDefense && defense.length > 0) {
@@ -3264,6 +3362,7 @@ async function FreePiV2Setup(ctx) {
3264
3362
  console.error("[free-pi] could not register the request hook", error);
3265
3363
  }
3266
3364
  return async () => {
3365
+ credentialsAbort.abort();
3267
3366
  for (const registration of registrations.reverse()) {
3268
3367
  await registration.dispose?.().catch(() => undefined);
3269
3368
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eduardo191labs/opencode-free-pi",
3
- "version": "0.2.5",
3
+ "version": "0.2.6",
4
4
  "description": "Ad-supported free-pi provider plugin for OpenCode on Windows, Linux, macOS and Termux",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
package/src/index.ts CHANGED
@@ -3736,6 +3736,7 @@ function v2Provider(
3736
3736
  id: providerID,
3737
3737
  name,
3738
3738
  activation: "enabled",
3739
+ integrationID: PROVIDER_ID,
3739
3740
  package: "@opencode/ai/providers/openai-compatible",
3740
3741
  settings: {
3741
3742
  baseURL: completionsBaseUrl(),
@@ -3746,6 +3747,29 @@ function v2Provider(
3746
3747
  };
3747
3748
  }
3748
3749
 
3750
+ async function v2ConnectionAuth(ctx: any): Promise<any | undefined> {
3751
+ try {
3752
+ const connection = await ctx.integration.connection.active(PROVIDER_ID);
3753
+ if (!connection) return undefined;
3754
+ const credential = await ctx.integration.connection.resolve(connection);
3755
+ if (!credential) return undefined;
3756
+ if (credential.type === "oauth")
3757
+ return {
3758
+ type: "oauth",
3759
+ refresh: credential.refresh,
3760
+ access: credential.access,
3761
+ expires: credential.expires,
3762
+ };
3763
+ if (credential.type === "key")
3764
+ return {
3765
+ type: "api",
3766
+ key: credential.key,
3767
+ metadata: credential.metadata,
3768
+ };
3769
+ } catch {}
3770
+ return undefined;
3771
+ }
3772
+
3749
3773
  function v2Client(ctx: any): any {
3750
3774
  const context = async (sessionID: string) => {
3751
3775
  try {
@@ -3787,10 +3811,87 @@ async function FreePiV2Setup(ctx: any): Promise<() => Promise<void>> {
3787
3811
  ]);
3788
3812
  const { main, defense } = partitionModels(catalog);
3789
3813
  const candidates = modelRegistryCandidates(registry);
3790
- const token = await readCredentialsToken();
3791
3814
  const client = v2Client(ctx);
3815
+ const connectionAuth = await v2ConnectionAuth(ctx);
3816
+ let token = tokenFromAuth(connectionAuth) ?? (await readCredentialsToken());
3817
+ authResolver = async () => (await v2ConnectionAuth(ctx)) ?? undefined;
3792
3818
  const hooks: any = await (FreePiPlugin as any)({ client }, ctx.options);
3793
3819
  const registrations: any[] = [];
3820
+ const credentialsAbort = new AbortController();
3821
+
3822
+ void (async () => {
3823
+ try {
3824
+ for await (const _event of ctx.event.subscribe({
3825
+ types: ["credential.updated", "credential.switched"],
3826
+ signal: credentialsAbort.signal,
3827
+ })) {
3828
+ const next =
3829
+ tokenFromAuth(await v2ConnectionAuth(ctx)) ??
3830
+ (await readCredentialsToken());
3831
+ if (next === token) continue;
3832
+ token = next;
3833
+ await ctx.provider.reload().catch(() => undefined);
3834
+ }
3835
+ } catch {}
3836
+ })();
3837
+
3838
+ try {
3839
+ registrations.push(
3840
+ await ctx.integration.transform((editor: any) => {
3841
+ editor.update(PROVIDER_ID, (integration: any) => {
3842
+ integration.name = "Free Pi";
3843
+ });
3844
+ editor.method.update({
3845
+ integrationID: PROVIDER_ID,
3846
+ method: {
3847
+ id: "github",
3848
+ type: "oauth",
3849
+ label: "Sign in with GitHub (free-pi)",
3850
+ },
3851
+ authorize: async () => {
3852
+ const result = await authorizeFreePi(client);
3853
+ if (result.method !== "auto")
3854
+ throw new Error("free-pi sign-in did not start.");
3855
+ return {
3856
+ url: result.url,
3857
+ instructions: result.instructions,
3858
+ mode: "auto",
3859
+ callback: (async () => {
3860
+ const value = await result.callback();
3861
+ if (value.type !== "success")
3862
+ throw new Error("free-pi sign-in failed.");
3863
+ if ("key" in value)
3864
+ return {
3865
+ type: "key",
3866
+ key: value.key,
3867
+ metadata: value.metadata,
3868
+ };
3869
+ return {
3870
+ type: "oauth",
3871
+ methodID: "github",
3872
+ refresh: value.refresh,
3873
+ access: value.access,
3874
+ expires: value.expires,
3875
+ };
3876
+ })(),
3877
+ };
3878
+ },
3879
+ });
3880
+ editor.method.update({
3881
+ integrationID: PROVIDER_ID,
3882
+ method: {
3883
+ type: "key",
3884
+ label: "Paste a free-pi JWT",
3885
+ },
3886
+ });
3887
+ }),
3888
+ );
3889
+ } catch (error) {
3890
+ console.error(
3891
+ "[free-pi] could not register the OpenCode 2 integration",
3892
+ error,
3893
+ );
3894
+ }
3794
3895
 
3795
3896
  await ctx.provider.transform((editor: any) => {
3796
3897
  editor.add(
@@ -3926,6 +4027,7 @@ async function FreePiV2Setup(ctx: any): Promise<() => Promise<void>> {
3926
4027
  }
3927
4028
 
3928
4029
  return async () => {
4030
+ credentialsAbort.abort();
3929
4031
  for (const registration of registrations.reverse()) {
3930
4032
  await registration.dispose?.().catch(() => undefined);
3931
4033
  }