@eduardo191labs/opencode-free-pi 0.7.26 → 0.7.29

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.
Files changed (48) hide show
  1. package/README.md +60 -105
  2. package/dist/catalog/live.js +4 -1
  3. package/dist/catalog/models.d.ts +1 -1
  4. package/dist/catalog/models.js +14 -3
  5. package/dist/constants.d.ts +15 -35
  6. package/dist/constants.js +15 -35
  7. package/dist/index.d.ts +1 -1
  8. package/dist/plugin.js +14 -46
  9. package/dist/session/events.js +5 -12
  10. package/dist/session/hooks.js +83 -348
  11. package/dist/session/overflow.d.ts +7 -28
  12. package/dist/session/overflow.js +7 -73
  13. package/dist/session/replay.d.ts +8 -41
  14. package/dist/session/replay.js +9 -148
  15. package/dist/session/tools.d.ts +2 -7
  16. package/dist/session/tools.js +3 -18
  17. package/dist/session/trace.js +0 -1
  18. package/dist/session/track.d.ts +2 -0
  19. package/dist/session/track.js +5 -0
  20. package/dist/state.d.ts +1 -6
  21. package/dist/state.js +0 -2
  22. package/dist/tools/vision.js +1 -0
  23. package/dist/types.d.ts +0 -1
  24. package/package.json +2 -2
  25. package/src/catalog/live.ts +4 -1
  26. package/src/catalog/models.ts +20 -3
  27. package/src/constants.ts +16 -38
  28. package/src/index.ts +0 -1
  29. package/src/plugin.ts +12 -52
  30. package/src/session/events.ts +5 -12
  31. package/src/session/hooks.ts +109 -431
  32. package/src/session/overflow.ts +7 -77
  33. package/src/session/replay.ts +12 -188
  34. package/src/session/tools.ts +1 -22
  35. package/src/session/trace.ts +0 -1
  36. package/src/session/track.ts +11 -0
  37. package/src/state.ts +0 -8
  38. package/src/tools/vision.ts +2 -0
  39. package/src/types.ts +0 -1
  40. package/dist/session/diet.d.ts +0 -23
  41. package/dist/session/diet.js +0 -176
  42. package/dist/session/title.d.ts +0 -18
  43. package/dist/session/title.js +0 -81
  44. package/dist/session/usage.d.ts +0 -5
  45. package/dist/session/usage.js +0 -32
  46. package/src/session/diet.ts +0 -201
  47. package/src/session/title.ts +0 -90
  48. package/src/session/usage.ts +0 -46
package/src/index.ts CHANGED
@@ -17,7 +17,6 @@ export type {
17
17
  ModelsDevProvider,
18
18
  ModelRegistry,
19
19
  QuotaSnapshot,
20
- RequestUsage,
21
20
  } from "./types.js";
22
21
  export { createLogger } from "./logging.js";
23
22
  export type { Logger, LogLevel } from "./logging.js";
package/src/plugin.ts CHANGED
@@ -16,12 +16,6 @@ import { closeFreePiSession } from "./api/session.js";
16
16
  import { readCredentialsToken } from "./auth/credentials.js";
17
17
  import { tokenFromCredential } from "./auth/credential.js";
18
18
  import { fetchCatalog } from "./catalog/catalog.js";
19
- import {
20
- currentLiveLimits,
21
- loadLiveLimits,
22
- shouldVerifyWindow,
23
- verifyGatewayWindow,
24
- } from "./catalog/live.js";
25
19
  import {
26
20
  accountAccess,
27
21
  buildModels,
@@ -36,6 +30,7 @@ import {
36
30
  CATALOG_RECOVERY_DELAYS_MS,
37
31
  CATALOG_RECOVERY_INTERVAL_MS,
38
32
  CATALOG_RECOVERY_LOG_EVERY,
33
+ DEFAULT_FREE_MODEL_ID,
39
34
  DEFENSE_PROVIDER_ID,
40
35
  PROVIDER_ID,
41
36
  } from "./constants.js";
@@ -197,10 +192,8 @@ async function setup(ctx: Plugin.Context): Promise<() => Promise<void>> {
197
192
  let currentMain = filterAccessibleModels(catalogMain, access);
198
193
  let currentDefense = filterAccessibleModels(catalogDefense, access);
199
194
  const candidates = modelRegistryCandidates(registry);
200
- const live = await loadLiveLimits();
201
195
  const configuredEffort = ctx.options?.reasoningEffort;
202
196
  const buildOptions = {
203
- live,
204
197
  ...(configuredEffort === false
205
198
  ? { reasoningEffort: false as const }
206
199
  : isReasoningEffort(configuredEffort)
@@ -220,38 +213,28 @@ async function setup(ctx: Plugin.Context): Promise<() => Promise<void>> {
220
213
  return kept;
221
214
  };
222
215
 
223
- // Rebuilt on every call so a learned context ceiling or a recovered catalog
224
- // applies without restarting OpenCode.
216
+ // Rebuilt on every call so a recovered catalog or a refreshed quota applies
217
+ // without restarting OpenCode.
225
218
  const buildMain = () =>
226
219
  withoutDenied(
227
220
  PROVIDER_ID,
228
- buildModels(currentMain, candidates, {
229
- ...buildOptions,
230
- live: currentLiveLimits() ?? live,
231
- }),
221
+ buildModels(currentMain, candidates, buildOptions),
232
222
  );
233
223
  const buildDefense = () =>
234
224
  withoutDenied(
235
225
  DEFENSE_PROVIDER_ID,
236
- buildModels(currentDefense, candidates, {
237
- ...buildOptions,
238
- live: currentLiveLimits() ?? live,
239
- }),
226
+ buildModels(currentDefense, candidates, buildOptions),
240
227
  );
241
228
 
242
229
  function indexModelState(): void {
243
230
  const mainNow = buildMain();
244
231
  const defenseNow = buildDefense();
245
232
  for (const [id, model] of Object.entries(mainNow)) {
246
- state.outputLimits.set(`${PROVIDER_ID}/${id}`, model.limit.output);
247
- state.contextLimits.set(`${PROVIDER_ID}/${id}`, model.limit.context);
248
233
  if (model.modalities.input.includes("image")) {
249
234
  state.imageModels.add(`${PROVIDER_ID}/${id}`);
250
235
  }
251
236
  }
252
237
  for (const [id, model] of Object.entries(defenseNow)) {
253
- state.outputLimits.set(`${DEFENSE_PROVIDER_ID}/${id}`, model.limit.output);
254
- state.contextLimits.set(`${DEFENSE_PROVIDER_ID}/${id}`, model.limit.context);
255
238
  if (model.modalities.input.includes("image")) {
256
239
  state.imageModels.add(`${DEFENSE_PROVIDER_ID}/${id}`);
257
240
  }
@@ -267,7 +250,7 @@ async function setup(ctx: Plugin.Context): Promise<() => Promise<void>> {
267
250
 
268
251
  await ctx.provider.transform((editor) => {
269
252
  // Re-index on every transform: a learned context/output ceiling changes the
270
- // built model, and the diet and maxTokens cap read these maps. Refreshing
253
+ // built model, and the maxTokens cap reads these maps. Refreshing
271
254
  // only at setup would keep budgeting against the old window after a reload.
272
255
  indexModelState();
273
256
  applyProvider(
@@ -288,41 +271,18 @@ async function setup(ctx: Plugin.Context): Promise<() => Promise<void>> {
288
271
  }
289
272
  });
290
273
 
291
- // The gateway does not publish its context ceiling. Verify the compiled
292
- // window against it once a week with a free model, in the background, and
293
- // persist the result for the next catalog build. Prefer the free model with
294
- // the largest prompt already accepted in real traffic: it has the most
295
- // evidence behind it, so the growth probes describe the window sessions use.
296
- if (ctx.options?.liveLimits !== false && shouldVerifyWindow(live)) {
297
- const free = currentMain.filter(
298
- (model) => model.free === true && typeof model.id === "string",
299
- );
300
- const probe =
301
- free
302
- .map((model) => ({
303
- id: model.id,
304
- seen: live?.models[model.id ?? ""]?.maxPromptTokens ?? 0,
305
- }))
306
- .sort((a, b) => b.seen - a.seen)[0]?.id ??
307
- (currentCatalog?.default_model &&
308
- free.some((model) => model.id === currentCatalog?.default_model)
309
- ? currentCatalog.default_model
310
- : undefined) ??
311
- free[0]?.id ??
312
- Object.keys(buildMain())[0];
313
- if (probe) {
314
- void verifyGatewayWindow(deps, { model: probe }).catch(() => undefined);
315
- }
316
- }
317
-
318
- // V1 selected the catalog default when the user had no model configured.
274
+ // A fresh session gets the model that reasons longest of the free ones, not
275
+ // the catalog's phone-app default (`particle`, the fastest cheap model). The
276
+ // catalog default stays the fallback for the day this id disappears.
319
277
  await ctx.model.transform((editor) => {
320
278
  if (editor.default.get()) return;
321
279
  const models = buildMain();
322
280
  const preferred =
281
+ (models[DEFAULT_FREE_MODEL_ID] ? DEFAULT_FREE_MODEL_ID : undefined) ??
323
282
  (currentCatalog?.default_model && models[currentCatalog.default_model]
324
283
  ? currentCatalog.default_model
325
- : undefined) ?? Object.keys(models)[0];
284
+ : undefined) ??
285
+ Object.keys(models)[0];
326
286
  if (preferred) editor.default.set(PROVIDER_ID, preferred);
327
287
  });
328
288
 
@@ -1,6 +1,7 @@
1
1
  /** Subscribes to core events that affect free-pi state. */
2
2
 
3
3
  import type { Plugin } from "@opencode/plugin";
4
+ import { fetchQuota } from "../api/quota.js";
4
5
  import { closeFreePiSession } from "../api/session.js";
5
6
  import { readCredentialsToken } from "../auth/credentials.js";
6
7
  import { tokenFromCredential } from "../auth/credential.js";
@@ -21,7 +22,6 @@ import {
21
22
  import {
22
23
  markWireSessionDirty,
23
24
  } from "./wire.js";
24
- import { recordUsage } from "./usage.js";
25
25
 
26
26
  function isAbortLike(error: any): boolean {
27
27
  if (!error) return false;
@@ -39,7 +39,6 @@ const EVENT_TYPES = [
39
39
  "session.execution.succeeded",
40
40
  "session.execution.failed",
41
41
  "session.execution.interrupted",
42
- "session.usage.updated",
43
42
  "session.step.ended",
44
43
  "session.deleted",
45
44
  "location.shutdown",
@@ -124,6 +123,9 @@ async function handleEvent(
124
123
  }
125
124
  case "session.execution.succeeded": {
126
125
  endExecution(deps, event.data?.sessionID);
126
+ // Usage and cost are OpenCode's; the free-pi account quota is not, so a
127
+ // finished turn is the moment to refresh the picker's access filter.
128
+ void fetchQuota(deps).catch(() => undefined);
127
129
  return;
128
130
  }
129
131
  case "session.execution.interrupted": {
@@ -134,21 +136,12 @@ async function handleEvent(
134
136
  endExecution(deps, sessionID);
135
137
  return;
136
138
  }
137
- case "session.usage.updated": {
138
- const sessionID = event.data?.sessionID;
139
- if (typeof sessionID !== "string" || !isFreePiSession(deps, sessionID)) {
140
- return;
141
- }
142
- recordUsage(deps, sessionID, event.data?.cost, event.data?.tokens);
143
- return;
144
- }
145
139
  case "session.step.ended": {
146
140
  const sessionID = event.data?.sessionID;
147
141
  if (typeof sessionID !== "string" || !isFreePiSession(deps, sessionID)) {
148
142
  return;
149
143
  }
150
- recordUsage(deps, sessionID, event.data?.cost, event.data?.tokens);
151
- // Step tokens are per request; usage.updated is cumulative.
144
+ // Step tokens are per request; OpenCode owns cumulative usage.
152
145
  noteStepTokens(
153
146
  deps,
154
147
  deps.state.trackedModels.get(sessionID)?.modelID ?? "",