@eduardo191labs/opencode-free-pi 0.5.6 → 0.6.2
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 +64 -55
- package/dist/catalog/catalog.js +1 -21
- package/dist/catalog/live.d.ts +71 -0
- package/dist/catalog/live.js +301 -0
- package/dist/catalog/models.d.ts +13 -2
- package/dist/catalog/models.js +69 -21
- package/dist/catalog/registry.js +5 -15
- package/dist/catalog/v2.js +5 -7
- package/dist/constants.d.ts +24 -21
- package/dist/constants.js +24 -19
- package/dist/logging.js +2 -2
- package/dist/plugin.js +26 -2
- package/dist/session/events.js +2 -3
- package/dist/session/hooks.js +4 -12
- package/dist/state.d.ts +1 -15
- package/dist/state.js +0 -3
- package/dist/tools/vision-queue.d.ts +1 -1
- package/dist/tools/vision-queue.js +5 -5
- package/dist/tools/vision.js +24 -35
- package/dist/tools/web.js +39 -44
- package/dist/types.d.ts +5 -9
- package/dist/utils/data-dir.d.ts +3 -0
- package/dist/utils/data-dir.js +12 -0
- package/package.json +2 -2
- package/src/catalog/catalog.ts +0 -25
- package/src/catalog/live.ts +361 -0
- package/src/catalog/models.ts +92 -23
- package/src/catalog/registry.ts +6 -21
- package/src/catalog/v2.ts +5 -7
- package/src/constants.ts +24 -22
- package/src/logging.ts +2 -2
- package/src/plugin.ts +34 -2
- package/src/session/events.ts +6 -3
- package/src/session/hooks.ts +4 -18
- package/src/state.ts +0 -18
- package/src/tools/vision-queue.ts +5 -5
- package/src/tools/vision.ts +26 -38
- package/src/tools/web.ts +49 -59
- package/src/types.ts +5 -8
- package/src/utils/data-dir.ts +14 -0
- package/dist/session/cache.d.ts +0 -15
- package/dist/session/cache.js +0 -57
- package/dist/tools/spend-cache.d.ts +0 -15
- package/dist/tools/spend-cache.js +0 -64
- package/dist/utils/cache.d.ts +0 -7
- package/dist/utils/cache.js +0 -45
- package/src/session/cache.ts +0 -82
- package/src/tools/spend-cache.ts +0 -79
- package/src/utils/cache.ts +0 -49
|
@@ -4,12 +4,12 @@
|
|
|
4
4
|
* free-pi answers one `/tools/vision` request at a time: a second concurrent
|
|
5
5
|
* question gets `rate_limited`. OpenCode can run several sessions, and users can
|
|
6
6
|
* run several OpenCode processes, so calls are queued in-process and coordinated
|
|
7
|
-
* through a lock directory in the free-pi
|
|
7
|
+
* through a lock directory in the free-pi data directory.
|
|
8
8
|
*/
|
|
9
9
|
import { randomUUID } from "node:crypto";
|
|
10
10
|
import { mkdir, readFile, rm, stat, writeFile } from "node:fs/promises";
|
|
11
11
|
import { join } from "node:path";
|
|
12
|
-
import {
|
|
12
|
+
import { dataDir } from "../utils/data-dir.js";
|
|
13
13
|
const OWNER_FILE = "owner.json";
|
|
14
14
|
const POLL_MS = 350;
|
|
15
15
|
/** A live owner may legitimately hold the lock for a 90 s request. */
|
|
@@ -17,7 +17,7 @@ const STALE_MS = 10 * 60_000;
|
|
|
17
17
|
const MAX_WAIT_MS = 120_000;
|
|
18
18
|
let chain = Promise.resolve();
|
|
19
19
|
function lockDir() {
|
|
20
|
-
return join(
|
|
20
|
+
return join(dataDir(), "vision.lock");
|
|
21
21
|
}
|
|
22
22
|
function pidAlive(pid) {
|
|
23
23
|
try {
|
|
@@ -76,10 +76,10 @@ async function acquire(log) {
|
|
|
76
76
|
};
|
|
77
77
|
const startedAt = Date.now();
|
|
78
78
|
try {
|
|
79
|
-
await mkdir(
|
|
79
|
+
await mkdir(dataDir(), { recursive: true });
|
|
80
80
|
}
|
|
81
81
|
catch {
|
|
82
|
-
// Without a
|
|
82
|
+
// Without a data directory the file lock is unavailable; the in-process
|
|
83
83
|
// queue still serializes calls, so keep going.
|
|
84
84
|
}
|
|
85
85
|
while (Date.now() - startedAt < MAX_WAIT_MS) {
|
package/dist/tools/vision.js
CHANGED
|
@@ -6,12 +6,10 @@
|
|
|
6
6
|
* `/tools/vision` and returns the vision model's text answer. Each call counts
|
|
7
7
|
* against the user's daily allowance.
|
|
8
8
|
*/
|
|
9
|
-
import { createHash } from "node:crypto";
|
|
10
9
|
import { resolveToken } from "../auth/token.js";
|
|
11
10
|
import { isFreePiSession } from "../session/tools.js";
|
|
12
11
|
import { resolveBaseUrl } from "../utils/urls.js";
|
|
13
12
|
import { loadImage, prepareImage } from "./image.js";
|
|
14
|
-
import { spendOnce } from "./spend-cache.js";
|
|
15
13
|
import { withVisionSlot } from "./vision-queue.js";
|
|
16
14
|
const TOOL_NAME = "look_at_image";
|
|
17
15
|
const TIMEOUT_MS = 90_000;
|
|
@@ -214,39 +212,30 @@ async function executeVision(ctx, deps, raw, context) {
|
|
|
214
212
|
if (!token) {
|
|
215
213
|
return fail("look_at_image needs a free-pi sign-in. Do not retry; tell the user to connect the free-pi integration, and continue without this tool.");
|
|
216
214
|
}
|
|
217
|
-
const
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
.
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
model: answer.model,
|
|
242
|
-
});
|
|
243
|
-
return done(questionText(path, answer.text), {
|
|
244
|
-
ok: true,
|
|
245
|
-
...(answer.cost_usd !== undefined ? { cost_usd: answer.cost_usd } : {}),
|
|
246
|
-
...(answer.model !== undefined ? { model: answer.model } : {}),
|
|
247
|
-
elapsed_ms: elapsed,
|
|
248
|
-
attempts,
|
|
249
|
-
});
|
|
215
|
+
const queued = await withVisionSlot(deps.log, () => askVision(deps, token, prepared.data, prepared.mime, question, context.signal));
|
|
216
|
+
if (!queued.ok) {
|
|
217
|
+
return fail("Another image question is still running. Wait a moment and call look_at_image again.");
|
|
218
|
+
}
|
|
219
|
+
if (!queued.value.ok)
|
|
220
|
+
return fail(queued.value.text);
|
|
221
|
+
const { answer, attempts } = queued.value;
|
|
222
|
+
const elapsed = Date.now() - startedAt;
|
|
223
|
+
deps.log.info("look_at_image answered", {
|
|
224
|
+
path,
|
|
225
|
+
ms: elapsed,
|
|
226
|
+
attempts,
|
|
227
|
+
resized: prepared.resized,
|
|
228
|
+
bytes: prepared.bytes,
|
|
229
|
+
size: `${prepared.width}x${prepared.height}`,
|
|
230
|
+
mime: prepared.mime,
|
|
231
|
+
model: answer.model,
|
|
232
|
+
});
|
|
233
|
+
return done(questionText(path, answer.text), {
|
|
234
|
+
ok: true,
|
|
235
|
+
...(answer.cost_usd !== undefined ? { cost_usd: answer.cost_usd } : {}),
|
|
236
|
+
...(answer.model !== undefined ? { model: answer.model } : {}),
|
|
237
|
+
elapsed_ms: elapsed,
|
|
238
|
+
attempts,
|
|
250
239
|
});
|
|
251
240
|
}
|
|
252
241
|
/**
|
package/dist/tools/web.js
CHANGED
|
@@ -5,7 +5,6 @@
|
|
|
5
5
|
* CLI, and are only exposed inside free-pi sessions.
|
|
6
6
|
*/
|
|
7
7
|
import { done, fail, freePiRequest, GATED_TEXT, isGatedSession, mappedText, } from "./api.js";
|
|
8
|
-
import { spendOnce } from "./spend-cache.js";
|
|
9
8
|
const ERRORS = {
|
|
10
9
|
daily_cap: "The user's daily free usage is used up. Do not retry; tell the user and continue without this tool.",
|
|
11
10
|
rolling_cap: "The user's short-term usage limit is reached. Do not retry; continue without this tool.",
|
|
@@ -83,30 +82,28 @@ async function executeWebSearch(deps, raw, context) {
|
|
|
83
82
|
if (!query)
|
|
84
83
|
return fail("web_search needs a query.");
|
|
85
84
|
const num = typeof input?.num_results === "number" ? input.num_results : undefined;
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
: {}),
|
|
109
|
-
});
|
|
85
|
+
const result = await freePiRequest(deps, {
|
|
86
|
+
path: "/tools/web-search",
|
|
87
|
+
method: "POST",
|
|
88
|
+
body: { query, ...(num !== undefined ? { num_results: num } : {}) },
|
|
89
|
+
timeoutMs: 30_000,
|
|
90
|
+
signal: context.signal,
|
|
91
|
+
label: "The web search",
|
|
92
|
+
});
|
|
93
|
+
if (!result.ok)
|
|
94
|
+
return fail(mappedText(ERRORS, result));
|
|
95
|
+
deps.log.info("web_search answered", {
|
|
96
|
+
query,
|
|
97
|
+
results: Array.isArray(result.data?.results)
|
|
98
|
+
? result.data.results.length
|
|
99
|
+
: 0,
|
|
100
|
+
costUsd: result.data?.cost_usd,
|
|
101
|
+
});
|
|
102
|
+
return done(formatWebResults(query, result.data?.results ?? []), {
|
|
103
|
+
ok: true,
|
|
104
|
+
...(typeof result.data?.cost_usd === "number"
|
|
105
|
+
? { cost_usd: result.data.cost_usd }
|
|
106
|
+
: {}),
|
|
110
107
|
});
|
|
111
108
|
}
|
|
112
109
|
async function executeDocsLookup(deps, raw, context) {
|
|
@@ -120,7 +117,7 @@ async function executeDocsLookup(deps, raw, context) {
|
|
|
120
117
|
return fail("docs_lookup needs a library name.");
|
|
121
118
|
if (!query)
|
|
122
119
|
return fail("docs_lookup needs a specific question.");
|
|
123
|
-
const
|
|
120
|
+
const lookup = await freePiRequest(deps, {
|
|
124
121
|
path: "/tools/docs",
|
|
125
122
|
method: "POST",
|
|
126
123
|
body: {
|
|
@@ -131,24 +128,22 @@ async function executeDocsLookup(deps, raw, context) {
|
|
|
131
128
|
timeoutMs: 30_000,
|
|
132
129
|
signal: context.signal,
|
|
133
130
|
label: "The documentation lookup",
|
|
134
|
-
})
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
}));
|
|
151
|
-
return result;
|
|
131
|
+
});
|
|
132
|
+
if (!lookup.ok)
|
|
133
|
+
return fail(mappedText(ERRORS, lookup, library));
|
|
134
|
+
const data = lookup.data ?? {};
|
|
135
|
+
deps.log.info("docs_lookup answered", {
|
|
136
|
+
library,
|
|
137
|
+
libraryID: data.library_id,
|
|
138
|
+
costUsd: data.cost_usd,
|
|
139
|
+
});
|
|
140
|
+
return done(`${data.title ?? library} (library_id: ${data.library_id ?? "unknown"})\n\n${data.docs ?? ""}`, {
|
|
141
|
+
ok: true,
|
|
142
|
+
library_id: data.library_id,
|
|
143
|
+
...(typeof data.cost_usd === "number"
|
|
144
|
+
? { cost_usd: data.cost_usd }
|
|
145
|
+
: {}),
|
|
146
|
+
});
|
|
152
147
|
}
|
|
153
148
|
export async function registerWebTools(ctx, deps) {
|
|
154
149
|
return ctx.tool.transform((editor) => {
|
package/dist/types.d.ts
CHANGED
|
@@ -92,9 +92,6 @@ export type Catalog = {
|
|
|
92
92
|
default_model?: string;
|
|
93
93
|
notice?: string;
|
|
94
94
|
models?: CatalogModel[];
|
|
95
|
-
cost_experiment?: {
|
|
96
|
-
arm?: string;
|
|
97
|
-
};
|
|
98
95
|
};
|
|
99
96
|
/** Intermediate, SDK-agnostic model shape used while the catalog is built. */
|
|
100
97
|
export type ModelDefinition = {
|
|
@@ -138,6 +135,11 @@ export type FreePiOptions = {
|
|
|
138
135
|
* models plan before answering. Set `false` to keep the gateway default.
|
|
139
136
|
*/
|
|
140
137
|
reasoningEffort?: string | false;
|
|
138
|
+
/**
|
|
139
|
+
* Verify the gateway's real context window once a week and reuse the measured
|
|
140
|
+
* value. Defaults to true; set `false` to stay on the compiled window.
|
|
141
|
+
*/
|
|
142
|
+
liveLimits?: boolean;
|
|
141
143
|
};
|
|
142
144
|
export type MeResponse = {
|
|
143
145
|
handle?: string;
|
|
@@ -247,12 +249,6 @@ export type TrackedModel = {
|
|
|
247
249
|
providerID: string;
|
|
248
250
|
modelID: string;
|
|
249
251
|
};
|
|
250
|
-
export type CacheMissState = {
|
|
251
|
-
promptTokens: number;
|
|
252
|
-
at: number;
|
|
253
|
-
modelKey: string;
|
|
254
|
-
reportedCache: boolean;
|
|
255
|
-
};
|
|
256
252
|
export type ToolStats = {
|
|
257
253
|
meta: string[];
|
|
258
254
|
metaBytes: number;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** Per-user directory for the files the plugin keeps between runs. */
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
export function dataDir() {
|
|
5
|
+
const configured = process.env.FREE_PI_DATA_DIR?.trim();
|
|
6
|
+
if (configured)
|
|
7
|
+
return configured;
|
|
8
|
+
return join(homedir(), ".cache", "opencode", "free-pi");
|
|
9
|
+
}
|
|
10
|
+
export function dataPath(name) {
|
|
11
|
+
return join(dataDir(), name);
|
|
12
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eduardo191labs/opencode-free-pi",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.2",
|
|
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",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"scripts": {
|
|
25
25
|
"build": "node -e \"require('node:fs').rmSync('dist',{recursive:true,force:true})\" && tsc -p tsconfig.json",
|
|
26
26
|
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
27
|
-
"test": "npm run build && node test/smoke.mjs && node test/registry.mjs && node test/models.mjs && node test/
|
|
27
|
+
"test": "npm run build && node test/smoke.mjs && node test/registry.mjs && node test/models.mjs && node test/live.mjs && node test/hooks.mjs && node test/title.mjs && node test/errors.mjs && node test/vision.mjs && node test/agents.mjs && node test/wire.mjs && node test/tools.mjs",
|
|
28
28
|
"prepublishOnly": "npm run build",
|
|
29
29
|
"postinstall": "node scripts/postinstall.mjs"
|
|
30
30
|
},
|
package/src/catalog/catalog.ts
CHANGED
|
@@ -2,18 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
import { resolveToken } from "../auth/token.js";
|
|
4
4
|
import {
|
|
5
|
-
CATALOG_DISK_TTL_MS,
|
|
6
5
|
CATALOG_TIMEOUT_MS,
|
|
7
6
|
CATALOG_TTL_MS,
|
|
8
|
-
COST_EXPERIMENT_HEADER,
|
|
9
7
|
} from "../constants.js";
|
|
10
8
|
import type { Deps } from "../deps.js";
|
|
11
9
|
import type { Catalog } from "../types.js";
|
|
12
|
-
import { readCache, writeCache } from "../utils/cache.js";
|
|
13
10
|
import { resolveBaseUrl } from "../utils/urls.js";
|
|
14
11
|
|
|
15
|
-
const CACHE_FILE = "catalog.json";
|
|
16
|
-
|
|
17
12
|
function isCatalog(value: unknown): value is Catalog {
|
|
18
13
|
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
19
14
|
}
|
|
@@ -30,11 +25,6 @@ function applyCatalog(deps: Deps, catalog: Catalog): Catalog {
|
|
|
30
25
|
if (typeof catalog.latest === "string" && catalog.latest.length > 0) {
|
|
31
26
|
state.latestClientVersion = catalog.latest;
|
|
32
27
|
}
|
|
33
|
-
const arm = catalog.cost_experiment?.arm;
|
|
34
|
-
state.costExperimentArm =
|
|
35
|
-
typeof arm === "string" && /^[A-Za-z0-9_-]{1,40}$/.test(arm)
|
|
36
|
-
? arm
|
|
37
|
-
: undefined;
|
|
38
28
|
if (
|
|
39
29
|
typeof catalog.notice === "string" &&
|
|
40
30
|
catalog.notice &&
|
|
@@ -54,12 +44,9 @@ export function refreshCatalog(deps: Deps): Promise<Catalog | undefined> {
|
|
|
54
44
|
if (state.catalogPromise) return state.catalogPromise;
|
|
55
45
|
const pending = (async () => {
|
|
56
46
|
try {
|
|
57
|
-
// The CLI asks for the catalog with its JWT and the cost-experiment
|
|
58
|
-
// header; without both the server may omit `cost_experiment`.
|
|
59
47
|
const token = await resolveToken(deps).catch(() => undefined);
|
|
60
48
|
const headers: Record<string, string> = {
|
|
61
49
|
accept: "application/json",
|
|
62
|
-
[COST_EXPERIMENT_HEADER]: "1",
|
|
63
50
|
};
|
|
64
51
|
if (token) headers.authorization = `Bearer ${token}`;
|
|
65
52
|
const response = await fetch(`${resolveBaseUrl()}/client-version`, {
|
|
@@ -72,7 +59,6 @@ export function refreshCatalog(deps: Deps): Promise<Catalog | undefined> {
|
|
|
72
59
|
throw new Error("free-pi returned an unexpected catalog payload");
|
|
73
60
|
}
|
|
74
61
|
applyCatalog(deps, catalog);
|
|
75
|
-
void writeCache(CACHE_FILE, catalog);
|
|
76
62
|
return catalog;
|
|
77
63
|
} catch (error) {
|
|
78
64
|
const cached = state.cachedCatalog;
|
|
@@ -97,16 +83,5 @@ export async function fetchCatalog(deps: Deps): Promise<Catalog | undefined> {
|
|
|
97
83
|
if (state.cachedCatalog && Date.now() - state.cachedCatalogAt < CATALOG_TTL_MS) {
|
|
98
84
|
return state.cachedCatalog;
|
|
99
85
|
}
|
|
100
|
-
|
|
101
|
-
if (!state.cachedCatalog) {
|
|
102
|
-
const disk = await readCache<Catalog>(CACHE_FILE, CATALOG_DISK_TTL_MS);
|
|
103
|
-
if (isCatalog(disk)) {
|
|
104
|
-
applyCatalog(deps, disk);
|
|
105
|
-
// Serve the cached copy immediately and refresh it in the background.
|
|
106
|
-
void refreshCatalog(deps);
|
|
107
|
-
return disk;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
86
|
return refreshCatalog(deps);
|
|
112
87
|
}
|