@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
package/src/constants.ts
CHANGED
|
@@ -11,8 +11,24 @@ export const DEFAULT_BASE_URL = "https://api.freepi.ai";
|
|
|
11
11
|
export const DEFAULT_CLIENT_VERSION = "0.6.25";
|
|
12
12
|
export const CONSENT_VERSION = "4";
|
|
13
13
|
|
|
14
|
+
/**
|
|
15
|
+
* The provider CLI's window. The gateway enforces its own ceiling but does not
|
|
16
|
+
* publish it, so this is the starting point; `verifyGatewayWindow` replaces it
|
|
17
|
+
* with what the server actually accepts.
|
|
18
|
+
*/
|
|
14
19
|
export const CONTEXT_WINDOW = 200_000;
|
|
20
|
+
/** Largest window the plugin will ever advertise after measuring the gateway. */
|
|
21
|
+
export const MAX_CONTEXT_WINDOW = 1_000_000;
|
|
15
22
|
export const MAX_OUTPUT_TOKENS = 32_768;
|
|
23
|
+
/**
|
|
24
|
+
* Upper bound for a model that publishes a larger output budget. Reasoning
|
|
25
|
+
* tokens share this budget, so a model that can emit more must not be pinned to
|
|
26
|
+
* the CLI's 32k: a long chain of thought would consume it and leave `content`
|
|
27
|
+
* empty.
|
|
28
|
+
*/
|
|
29
|
+
export const MAX_OUTPUT_TOKENS_CEILING = 131_072;
|
|
30
|
+
/** How long a measured gateway window is trusted before checking again. */
|
|
31
|
+
export const VERIFY_LIMITS_TTL_MS = 7 * 24 * 60 * 60 * 1_000;
|
|
16
32
|
|
|
17
33
|
export const AD_CONTEXT_HEADER = "x-ad-context";
|
|
18
34
|
/**
|
|
@@ -21,18 +37,17 @@ export const AD_CONTEXT_HEADER = "x-ad-context";
|
|
|
21
37
|
*/
|
|
22
38
|
export const AD_CONTEXT_OPT_OUT = "none";
|
|
23
39
|
export const SUBAGENT_HEADER = "x-freepi-subagent";
|
|
24
|
-
export const COST_EXPERIMENT_HEADER = "x-freepi-cost-exp";
|
|
25
|
-
export const EFFICIENCY_RULE =
|
|
26
|
-
"- When several independent reads or searches are needed, issue them in the same turn.";
|
|
27
40
|
|
|
28
41
|
/**
|
|
29
42
|
* The provider CLI starts with thinking off, but leaving `reasoning_effort`
|
|
30
43
|
* unset makes the gateway answer in a hurry: the free models spend only a few
|
|
31
|
-
* dozen reasoning tokens even on tasks that need planning. The
|
|
32
|
-
*
|
|
33
|
-
*
|
|
44
|
+
* dozen reasoning tokens even on tasks that need planning. The gateway accepts
|
|
45
|
+
* every level (measured with curl_cffi) but currently does not let the level
|
|
46
|
+
* change how long the upstream thinks, so the plugin also asks for explicit
|
|
47
|
+
* step-by-step checking in the prompt and defaults to `high`. `none`/`minimal`
|
|
48
|
+
* stay one pick away when speed matters more than care.
|
|
34
49
|
*/
|
|
35
|
-
export const DEFAULT_REASONING_EFFORT = "
|
|
50
|
+
export const DEFAULT_REASONING_EFFORT = "high";
|
|
36
51
|
export const REASONING_EFFORTS = [
|
|
37
52
|
"none",
|
|
38
53
|
"minimal",
|
|
@@ -47,6 +62,7 @@ export const DEFAULT_THINKING_LEVELS = [
|
|
|
47
62
|
"low",
|
|
48
63
|
"medium",
|
|
49
64
|
"high",
|
|
65
|
+
"max",
|
|
50
66
|
] as const;
|
|
51
67
|
|
|
52
68
|
/**
|
|
@@ -57,6 +73,7 @@ export const DEFAULT_THINKING_LEVELS = [
|
|
|
57
73
|
*/
|
|
58
74
|
export const FREE_PI_GUIDANCE = `# free-pi working rules
|
|
59
75
|
- Answer the user's latest request and keep their constraints; do not switch to a different task.
|
|
76
|
+
- Slow down on multi-step work (arithmetic, counting, logic, dates, units, planning, debugging): write out each step, recompute the result a second way, and only then answer. On those tasks a correct answer after a dozen explicit steps beats a fast guess; do not stop at the first plausible number.
|
|
60
77
|
- Read the files involved before editing and run the project's checks afterwards. Never say a test, build or command passed unless you ran it and saw it pass.
|
|
61
78
|
- Treat the conversation above as memory: reuse earlier decisions, file paths, identifiers and results. If something is genuinely missing, say what is missing instead of guessing.
|
|
62
79
|
- Use the available tools (read, grep, glob, bash, edit) for real work; do not describe hypothetical commands as if they ran.
|
|
@@ -94,11 +111,6 @@ export const CONCURRENT_RETRY_MAX = 6;
|
|
|
94
111
|
export const CONCURRENT_RETRY_BASE_MS = 1_000;
|
|
95
112
|
export const CONCURRENT_RETRY_MAX_DELAY_MS = 15_000;
|
|
96
113
|
|
|
97
|
-
/** pi assumes a 5 minute prompt-cache TTL when explaining a miss. */
|
|
98
|
-
export const CACHE_TTL_MS = 5 * 60_000;
|
|
99
|
-
export const CACHE_MISS_NOISE_FLOOR_TOKENS = 1_024;
|
|
100
|
-
export const CACHE_MISS_NOTICE_TOKENS = 20_000;
|
|
101
|
-
|
|
102
114
|
/** Tool outcome headers emitted by the free-pi provider CLI. */
|
|
103
115
|
export const TOOL_META_HEADER = "x-freepi-tool-meta";
|
|
104
116
|
export const TOOL_ERRORS_HEADER = "x-freepi-tool-errors";
|
|
@@ -106,14 +118,6 @@ export const COMPACTED_HEADER = "x-freepi-compacted";
|
|
|
106
118
|
export const TOOL_META_BUDGET = 1_500;
|
|
107
119
|
export const TOOL_ID_LIMIT = 50;
|
|
108
120
|
|
|
109
|
-
/**
|
|
110
|
-
* The `compact100` cost experiment reserves 100k tokens, so pi compacts when
|
|
111
|
-
* the prompt reaches 100k. OpenCode compacts at `window - max(15%, 16k)`;
|
|
112
|
-
* 117_647 is the smallest window that keeps that trigger at 100k.
|
|
113
|
-
*/
|
|
114
|
-
export const COMPACT100_TRIGGER_TOKENS = 100_000;
|
|
115
|
-
export const COMPACT100_CONTEXT_WINDOW = 117_647;
|
|
116
|
-
|
|
117
121
|
export const SESSION_TRACK_LIMIT = 200;
|
|
118
122
|
export const AUTH_SKEW_MS = 60_000;
|
|
119
123
|
export const AUTH_TIMEOUT_MS = 15 * 60_000;
|
|
@@ -130,10 +134,8 @@ export const AD_IMPRESSION_LIMIT = 200;
|
|
|
130
134
|
export const CREDITS_PER_USD = 40_000 / 3;
|
|
131
135
|
|
|
132
136
|
export const CATALOG_TTL_MS = 10 * 60_000;
|
|
133
|
-
export const CATALOG_DISK_TTL_MS = 24 * 60 * 60_000;
|
|
134
137
|
export const CATALOG_TIMEOUT_MS = 8_000;
|
|
135
138
|
export const REGISTRY_TTL_MS = 5 * 60_000;
|
|
136
|
-
export const REGISTRY_DISK_TTL_MS = 7 * 24 * 60 * 60_000;
|
|
137
139
|
export const REGISTRY_RETRY_MS = 60_000;
|
|
138
140
|
export const REGISTRY_TIMEOUT_MS = 3_500;
|
|
139
141
|
export const REGISTRY_ATTEMPTS = 2;
|
package/src/logging.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
import { appendFileSync, mkdirSync, renameSync, statSync } from "node:fs";
|
|
4
4
|
import { dirname, join } from "node:path";
|
|
5
|
-
import {
|
|
5
|
+
import { dataDir } from "./utils/data-dir.js";
|
|
6
6
|
|
|
7
7
|
export type LogLevel = "info" | "warn" | "error" | "debug";
|
|
8
8
|
|
|
@@ -19,7 +19,7 @@ let fileFailureReported = false;
|
|
|
19
19
|
|
|
20
20
|
function logFile(): string {
|
|
21
21
|
const configured = process.env.FREE_PI_LOG_FILE?.trim();
|
|
22
|
-
return configured || join(
|
|
22
|
+
return configured || join(dataDir(), "plugin.log");
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
function rotate(file: string): void {
|
package/src/plugin.ts
CHANGED
|
@@ -15,6 +15,11 @@ import { closeFreePiSession } from "./api/session.js";
|
|
|
15
15
|
import { readCredentialsToken } from "./auth/credentials.js";
|
|
16
16
|
import { tokenFromCredential } from "./auth/credential.js";
|
|
17
17
|
import { fetchCatalog } from "./catalog/catalog.js";
|
|
18
|
+
import {
|
|
19
|
+
loadLiveLimits,
|
|
20
|
+
shouldVerifyWindow,
|
|
21
|
+
verifyGatewayWindow,
|
|
22
|
+
} from "./catalog/live.js";
|
|
18
23
|
import {
|
|
19
24
|
accountAccess,
|
|
20
25
|
buildModels,
|
|
@@ -130,9 +135,10 @@ async function setup(ctx: Plugin.Context): Promise<() => Promise<void>> {
|
|
|
130
135
|
const main = filterAccessibleModels(catalogMain, access);
|
|
131
136
|
const defense = filterAccessibleModels(catalogDefense, access);
|
|
132
137
|
const candidates = modelRegistryCandidates(registry);
|
|
138
|
+
const live = await loadLiveLimits();
|
|
133
139
|
const configuredEffort = ctx.options?.reasoningEffort;
|
|
134
140
|
const buildOptions = {
|
|
135
|
-
|
|
141
|
+
live,
|
|
136
142
|
...(configuredEffort === false
|
|
137
143
|
? { reasoningEffort: false as const }
|
|
138
144
|
: isReasoningEffort(configuredEffort)
|
|
@@ -174,6 +180,33 @@ async function setup(ctx: Plugin.Context): Promise<() => Promise<void>> {
|
|
|
174
180
|
}
|
|
175
181
|
});
|
|
176
182
|
|
|
183
|
+
// The gateway does not publish its context ceiling. Verify the compiled
|
|
184
|
+
// window against it once a week with a free model, in the background, and
|
|
185
|
+
// persist the result for the next catalog build. Prefer the free model with
|
|
186
|
+
// the largest prompt already accepted in real traffic: it has the most
|
|
187
|
+
// evidence behind it, so the growth probes describe the window sessions use.
|
|
188
|
+
if (ctx.options?.liveLimits !== false && shouldVerifyWindow(live)) {
|
|
189
|
+
const free = main.filter(
|
|
190
|
+
(model) => model.free === true && typeof model.id === "string",
|
|
191
|
+
);
|
|
192
|
+
const probe =
|
|
193
|
+
free
|
|
194
|
+
.map((model) => ({
|
|
195
|
+
id: model.id,
|
|
196
|
+
seen: live?.models[model.id ?? ""]?.maxPromptTokens ?? 0,
|
|
197
|
+
}))
|
|
198
|
+
.sort((a, b) => b.seen - a.seen)[0]?.id ??
|
|
199
|
+
(catalog?.default_model &&
|
|
200
|
+
free.some((model) => model.id === catalog.default_model)
|
|
201
|
+
? catalog.default_model
|
|
202
|
+
: undefined) ??
|
|
203
|
+
free[0]?.id ??
|
|
204
|
+
Object.keys(mainModels)[0];
|
|
205
|
+
if (probe) {
|
|
206
|
+
void verifyGatewayWindow(deps, { model: probe }).catch(() => undefined);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
177
210
|
// V1 selected the catalog default when the user had no model configured.
|
|
178
211
|
await ctx.model.transform((editor) => {
|
|
179
212
|
if (editor.default.get()) return;
|
|
@@ -211,7 +244,6 @@ async function setup(ctx: Plugin.Context): Promise<() => Promise<void>> {
|
|
|
211
244
|
defaultModel: catalog?.default_model,
|
|
212
245
|
clientVersion: state.latestClientVersion,
|
|
213
246
|
capabilities: candidates.length,
|
|
214
|
-
costArm: state.costExperimentArm,
|
|
215
247
|
models: main.map((model) => model.id),
|
|
216
248
|
},
|
|
217
249
|
);
|
package/src/session/events.ts
CHANGED
|
@@ -6,7 +6,7 @@ import { readCredentialsToken } from "../auth/credentials.js";
|
|
|
6
6
|
import { tokenFromCredential } from "../auth/credential.js";
|
|
7
7
|
import type { Deps } from "../deps.js";
|
|
8
8
|
import { handleFreePiError } from "../errors/handle.js";
|
|
9
|
-
import {
|
|
9
|
+
import { noteStepTokens } from "../catalog/live.js";
|
|
10
10
|
import { forgetToolStats } from "./tools.js";
|
|
11
11
|
import {
|
|
12
12
|
beginExecution,
|
|
@@ -113,7 +113,11 @@ async function handleEvent(
|
|
|
113
113
|
event.data?.tokens,
|
|
114
114
|
);
|
|
115
115
|
// Step tokens are per request; usage.updated is cumulative.
|
|
116
|
-
|
|
116
|
+
noteStepTokens(
|
|
117
|
+
deps,
|
|
118
|
+
deps.state.trackedModels.get(event.data?.sessionID)?.modelID ?? "",
|
|
119
|
+
event.data?.tokens,
|
|
120
|
+
);
|
|
117
121
|
return;
|
|
118
122
|
}
|
|
119
123
|
case "session.deleted": {
|
|
@@ -121,7 +125,6 @@ async function handleEvent(
|
|
|
121
125
|
if (typeof sessionID !== "string") return;
|
|
122
126
|
await finalizeFreePiSession(deps, sessionID);
|
|
123
127
|
forgetFreePiSession(deps, sessionID);
|
|
124
|
-
forgetCacheMiss(deps, sessionID);
|
|
125
128
|
forgetToolStats(deps, sessionID);
|
|
126
129
|
deps.state.subagentSessions.delete(sessionID);
|
|
127
130
|
return;
|
package/src/session/hooks.ts
CHANGED
|
@@ -8,17 +8,16 @@ import { resolveToken } from "../auth/token.js";
|
|
|
8
8
|
import {
|
|
9
9
|
AD_CONTEXT_HEADER,
|
|
10
10
|
AD_CONTEXT_OPT_OUT,
|
|
11
|
-
COST_EXPERIMENT_HEADER,
|
|
12
11
|
CONCURRENT_RETRY_BASE_MS,
|
|
13
12
|
CONCURRENT_RETRY_MAX,
|
|
14
13
|
CONCURRENT_RETRY_MAX_DELAY_MS,
|
|
15
14
|
DEFENSE_PROVIDER_ID,
|
|
16
|
-
EFFICIENCY_RULE,
|
|
17
15
|
FREE_PI_COMPACTION_GUIDANCE,
|
|
18
16
|
FREE_PI_COMPACTION_MARKER,
|
|
19
17
|
FREE_PI_GUIDANCE,
|
|
20
18
|
FREE_PI_GUIDANCE_MARKER,
|
|
21
19
|
MAX_OUTPUT_TOKENS,
|
|
20
|
+
MAX_OUTPUT_TOKENS_CEILING,
|
|
22
21
|
PROVIDER_ID,
|
|
23
22
|
RETRY_BASE_DELAY_MS,
|
|
24
23
|
RETRY_MAX_DELAY_MS,
|
|
@@ -138,7 +137,9 @@ async function applyGeneration(
|
|
|
138
137
|
const limit = deps.state.outputLimits.get(
|
|
139
138
|
`${event.model.providerID}/${modelID}`,
|
|
140
139
|
);
|
|
141
|
-
|
|
140
|
+
// Reasoning shares the output budget; a model that publishes more than the
|
|
141
|
+
// CLI's 32k keeps that larger budget (up to the plugin ceiling).
|
|
142
|
+
const cap = Math.min(limit ?? MAX_OUTPUT_TOKENS, MAX_OUTPUT_TOKENS_CEILING);
|
|
142
143
|
if (event.options) {
|
|
143
144
|
const current = event.options.maxTokens;
|
|
144
145
|
if (current === undefined || current > cap) event.options.maxTokens = cap;
|
|
@@ -163,18 +164,6 @@ async function applyGeneration(
|
|
|
163
164
|
appendSystemGuidance(event, FREE_PI_GUIDANCE, FREE_PI_GUIDANCE_MARKER);
|
|
164
165
|
}
|
|
165
166
|
|
|
166
|
-
if (deps.state.costExperimentArm === "efficiency") {
|
|
167
|
-
const system = Array.isArray(event.system) ? event.system : [];
|
|
168
|
-
if (
|
|
169
|
-
!system.some(
|
|
170
|
-
(part: any) => part?.type === "text" && part.text === EFFICIENCY_RULE,
|
|
171
|
-
)
|
|
172
|
-
) {
|
|
173
|
-
system.push({ type: "text", text: EFFICIENCY_RULE });
|
|
174
|
-
event.system = system;
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
|
|
178
167
|
const agent = event.agent;
|
|
179
168
|
const turn = await resolveAdTurn(deps, {
|
|
180
169
|
sessionID: event.sessionID,
|
|
@@ -222,9 +211,6 @@ async function applyModelRequest(deps: Deps, event: any): Promise<void> {
|
|
|
222
211
|
if (deps.state.subagentSessions.has(event.sessionID)) {
|
|
223
212
|
event.headers[SUBAGENT_HEADER] = "1";
|
|
224
213
|
}
|
|
225
|
-
if (deps.state.costExperimentArm) {
|
|
226
|
-
event.headers[COST_EXPERIMENT_HEADER] = deps.state.costExperimentArm;
|
|
227
|
-
}
|
|
228
214
|
|
|
229
215
|
// Title requests are suppressed by the title hook, so an ad for them would be
|
|
230
216
|
// an impression the user never sees.
|
package/src/state.ts
CHANGED
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
import { DEFAULT_CLIENT_VERSION } from "./constants.js";
|
|
4
4
|
import type {
|
|
5
5
|
AdTurn,
|
|
6
|
-
CacheMissState,
|
|
7
6
|
Catalog,
|
|
8
7
|
FreePiError,
|
|
9
8
|
ModelRegistry,
|
|
@@ -26,7 +25,6 @@ export type RuntimeState = {
|
|
|
26
25
|
cachedRegistryAt: number;
|
|
27
26
|
registryAttemptAt: number;
|
|
28
27
|
registryPromise?: Promise<ModelRegistry | undefined>;
|
|
29
|
-
costExperimentArm?: string;
|
|
30
28
|
lastCatalogNotice?: string;
|
|
31
29
|
lastQuotaNotice?: string;
|
|
32
30
|
catalogModels: Catalog;
|
|
@@ -64,20 +62,7 @@ export type RuntimeState = {
|
|
|
64
62
|
pendingAdTurns: Map<string, AdTurn>;
|
|
65
63
|
adImpressions: Map<string, Promise<void>>;
|
|
66
64
|
trackedModels: Map<string, TrackedModel>;
|
|
67
|
-
cacheMisses: Map<string, CacheMissState>;
|
|
68
65
|
toolStats: Map<string, ToolStats>;
|
|
69
|
-
/** Successful paid-tool answers, keyed by canonical input, to avoid rebilling. */
|
|
70
|
-
toolSpend: Map<
|
|
71
|
-
string,
|
|
72
|
-
{
|
|
73
|
-
at: number;
|
|
74
|
-
output: {
|
|
75
|
-
content: ReadonlyArray<{ type: "text"; text: string }>;
|
|
76
|
-
metadata: Record<string, unknown>;
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
>;
|
|
80
|
-
toolSpendPending: Map<string, Promise<any>>;
|
|
81
66
|
subagentSessions: Set<string>;
|
|
82
67
|
};
|
|
83
68
|
|
|
@@ -108,10 +93,7 @@ export function createRuntimeState(): RuntimeState {
|
|
|
108
93
|
pendingAdTurns: new Map(),
|
|
109
94
|
adImpressions: new Map(),
|
|
110
95
|
trackedModels: new Map(),
|
|
111
|
-
cacheMisses: new Map(),
|
|
112
96
|
toolStats: new Map(),
|
|
113
|
-
toolSpend: new Map(),
|
|
114
|
-
toolSpendPending: new Map(),
|
|
115
97
|
subagentSessions: new Set(),
|
|
116
98
|
};
|
|
117
99
|
}
|
|
@@ -4,14 +4,14 @@
|
|
|
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
|
|
|
10
10
|
import { randomUUID } from "node:crypto";
|
|
11
11
|
import { mkdir, readFile, rm, stat, writeFile } from "node:fs/promises";
|
|
12
12
|
import { join } from "node:path";
|
|
13
13
|
import type { Logger } from "../logging.js";
|
|
14
|
-
import {
|
|
14
|
+
import { dataDir } from "../utils/data-dir.js";
|
|
15
15
|
|
|
16
16
|
type Owner = { pid: number; startedAt: number; id: string };
|
|
17
17
|
|
|
@@ -24,7 +24,7 @@ const MAX_WAIT_MS = 120_000;
|
|
|
24
24
|
let chain: Promise<void> = Promise.resolve();
|
|
25
25
|
|
|
26
26
|
function lockDir(): string {
|
|
27
|
-
return join(
|
|
27
|
+
return join(dataDir(), "vision.lock");
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
function pidAlive(pid: number): boolean {
|
|
@@ -87,9 +87,9 @@ async function acquire(log: Logger): Promise<Slot | undefined> {
|
|
|
87
87
|
const startedAt = Date.now();
|
|
88
88
|
|
|
89
89
|
try {
|
|
90
|
-
await mkdir(
|
|
90
|
+
await mkdir(dataDir(), { recursive: true });
|
|
91
91
|
} catch {
|
|
92
|
-
// Without a
|
|
92
|
+
// Without a data directory the file lock is unavailable; the in-process
|
|
93
93
|
// queue still serializes calls, so keep going.
|
|
94
94
|
}
|
|
95
95
|
|
package/src/tools/vision.ts
CHANGED
|
@@ -8,13 +8,11 @@
|
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
import type { Plugin } from "@opencode/plugin";
|
|
11
|
-
import { createHash } from "node:crypto";
|
|
12
11
|
import { resolveToken } from "../auth/token.js";
|
|
13
12
|
import type { Deps } from "../deps.js";
|
|
14
13
|
import { isFreePiSession } from "../session/tools.js";
|
|
15
14
|
import { resolveBaseUrl } from "../utils/urls.js";
|
|
16
15
|
import { loadImage, prepareImage, type VisionMime } from "./image.js";
|
|
17
|
-
import { spendOnce } from "./spend-cache.js";
|
|
18
16
|
import { withVisionSlot } from "./vision-queue.js";
|
|
19
17
|
|
|
20
18
|
type Registration = { dispose: () => Promise<void> };
|
|
@@ -273,44 +271,34 @@ async function executeVision(
|
|
|
273
271
|
);
|
|
274
272
|
}
|
|
275
273
|
|
|
276
|
-
const
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
.digest("hex");
|
|
283
|
-
|
|
284
|
-
return spendOnce(deps, `vision:${imageKey}`, async () => {
|
|
285
|
-
const queued = await withVisionSlot(deps.log, () =>
|
|
286
|
-
askVision(deps, token, prepared.data, prepared.mime, question, context.signal),
|
|
274
|
+
const queued = await withVisionSlot(deps.log, () =>
|
|
275
|
+
askVision(deps, token, prepared.data, prepared.mime, question, context.signal),
|
|
276
|
+
);
|
|
277
|
+
if (!queued.ok) {
|
|
278
|
+
return fail(
|
|
279
|
+
"Another image question is still running. Wait a moment and call look_at_image again.",
|
|
287
280
|
);
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
"Another image question is still running. Wait a moment and call look_at_image again.",
|
|
291
|
-
);
|
|
292
|
-
}
|
|
293
|
-
if (!queued.value.ok) return fail(queued.value.text);
|
|
281
|
+
}
|
|
282
|
+
if (!queued.value.ok) return fail(queued.value.text);
|
|
294
283
|
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
});
|
|
284
|
+
const { answer, attempts } = queued.value;
|
|
285
|
+
const elapsed = Date.now() - startedAt;
|
|
286
|
+
deps.log.info("look_at_image answered", {
|
|
287
|
+
path,
|
|
288
|
+
ms: elapsed,
|
|
289
|
+
attempts,
|
|
290
|
+
resized: prepared.resized,
|
|
291
|
+
bytes: prepared.bytes,
|
|
292
|
+
size: `${prepared.width}x${prepared.height}`,
|
|
293
|
+
mime: prepared.mime,
|
|
294
|
+
model: answer.model,
|
|
295
|
+
});
|
|
296
|
+
return done(questionText(path, answer.text), {
|
|
297
|
+
ok: true,
|
|
298
|
+
...(answer.cost_usd !== undefined ? { cost_usd: answer.cost_usd } : {}),
|
|
299
|
+
...(answer.model !== undefined ? { model: answer.model } : {}),
|
|
300
|
+
elapsed_ms: elapsed,
|
|
301
|
+
attempts,
|
|
314
302
|
});
|
|
315
303
|
}
|
|
316
304
|
|
package/src/tools/web.ts
CHANGED
|
@@ -15,7 +15,6 @@ import {
|
|
|
15
15
|
isGatedSession,
|
|
16
16
|
mappedText,
|
|
17
17
|
} from "./api.js";
|
|
18
|
-
import { spendOnce } from "./spend-cache.js";
|
|
19
18
|
|
|
20
19
|
const ERRORS: Record<string, string> = {
|
|
21
20
|
daily_cap:
|
|
@@ -115,30 +114,28 @@ async function executeWebSearch(
|
|
|
115
114
|
const num =
|
|
116
115
|
typeof input?.num_results === "number" ? input.num_results : undefined;
|
|
117
116
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
if (!result.ok) return fail(mappedText(ERRORS, result));
|
|
117
|
+
const result = await freePiRequest(deps, {
|
|
118
|
+
path: "/tools/web-search",
|
|
119
|
+
method: "POST",
|
|
120
|
+
body: { query, ...(num !== undefined ? { num_results: num } : {}) },
|
|
121
|
+
timeoutMs: 30_000,
|
|
122
|
+
signal: context.signal,
|
|
123
|
+
label: "The web search",
|
|
124
|
+
});
|
|
125
|
+
if (!result.ok) return fail(mappedText(ERRORS, result));
|
|
128
126
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
});
|
|
127
|
+
deps.log.info("web_search answered", {
|
|
128
|
+
query,
|
|
129
|
+
results: Array.isArray(result.data?.results)
|
|
130
|
+
? result.data.results.length
|
|
131
|
+
: 0,
|
|
132
|
+
costUsd: result.data?.cost_usd,
|
|
133
|
+
});
|
|
134
|
+
return done(formatWebResults(query, result.data?.results ?? []), {
|
|
135
|
+
ok: true,
|
|
136
|
+
...(typeof result.data?.cost_usd === "number"
|
|
137
|
+
? { cost_usd: result.data.cost_usd }
|
|
138
|
+
: {}),
|
|
142
139
|
});
|
|
143
140
|
}
|
|
144
141
|
|
|
@@ -155,42 +152,35 @@ async function executeDocsLookup(
|
|
|
155
152
|
if (!library) return fail("docs_lookup needs a library name.");
|
|
156
153
|
if (!query) return fail("docs_lookup needs a specific question.");
|
|
157
154
|
|
|
158
|
-
const
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
...(typeof data.cost_usd === "number"
|
|
187
|
-
? { cost_usd: data.cost_usd }
|
|
188
|
-
: {}),
|
|
189
|
-
},
|
|
190
|
-
);
|
|
191
|
-
}),
|
|
155
|
+
const lookup = await freePiRequest(deps, {
|
|
156
|
+
path: "/tools/docs",
|
|
157
|
+
method: "POST",
|
|
158
|
+
body: {
|
|
159
|
+
library,
|
|
160
|
+
query,
|
|
161
|
+
...(libraryId ? { library_id: libraryId } : {}),
|
|
162
|
+
},
|
|
163
|
+
timeoutMs: 30_000,
|
|
164
|
+
signal: context.signal,
|
|
165
|
+
label: "The documentation lookup",
|
|
166
|
+
});
|
|
167
|
+
if (!lookup.ok) return fail(mappedText(ERRORS, lookup, library));
|
|
168
|
+
const data = lookup.data ?? {};
|
|
169
|
+
deps.log.info("docs_lookup answered", {
|
|
170
|
+
library,
|
|
171
|
+
libraryID: data.library_id,
|
|
172
|
+
costUsd: data.cost_usd,
|
|
173
|
+
});
|
|
174
|
+
return done(
|
|
175
|
+
`${data.title ?? library} (library_id: ${data.library_id ?? "unknown"})\n\n${data.docs ?? ""}`,
|
|
176
|
+
{
|
|
177
|
+
ok: true,
|
|
178
|
+
library_id: data.library_id,
|
|
179
|
+
...(typeof data.cost_usd === "number"
|
|
180
|
+
? { cost_usd: data.cost_usd }
|
|
181
|
+
: {}),
|
|
182
|
+
},
|
|
192
183
|
);
|
|
193
|
-
return result;
|
|
194
184
|
}
|
|
195
185
|
|
|
196
186
|
export async function registerWebTools(
|
package/src/types.ts
CHANGED
|
@@ -76,7 +76,6 @@ export type Catalog = {
|
|
|
76
76
|
default_model?: string;
|
|
77
77
|
notice?: string;
|
|
78
78
|
models?: CatalogModel[];
|
|
79
|
-
cost_experiment?: { arm?: string };
|
|
80
79
|
};
|
|
81
80
|
|
|
82
81
|
/** Intermediate, SDK-agnostic model shape used while the catalog is built. */
|
|
@@ -115,6 +114,11 @@ export type FreePiOptions = {
|
|
|
115
114
|
* models plan before answering. Set `false` to keep the gateway default.
|
|
116
115
|
*/
|
|
117
116
|
reasoningEffort?: string | false;
|
|
117
|
+
/**
|
|
118
|
+
* Verify the gateway's real context window once a week and reuse the measured
|
|
119
|
+
* value. Defaults to true; set `false` to stay on the compiled window.
|
|
120
|
+
*/
|
|
121
|
+
liveLimits?: boolean;
|
|
118
122
|
};
|
|
119
123
|
|
|
120
124
|
export type MeResponse = {
|
|
@@ -243,13 +247,6 @@ export type TrackedModel = {
|
|
|
243
247
|
modelID: string;
|
|
244
248
|
};
|
|
245
249
|
|
|
246
|
-
export type CacheMissState = {
|
|
247
|
-
promptTokens: number;
|
|
248
|
-
at: number;
|
|
249
|
-
modelKey: string;
|
|
250
|
-
reportedCache: boolean;
|
|
251
|
-
};
|
|
252
|
-
|
|
253
250
|
export type ToolStats = {
|
|
254
251
|
meta: string[];
|
|
255
252
|
metaBytes: number;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** Per-user directory for the files the plugin keeps between runs. */
|
|
2
|
+
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
|
|
6
|
+
export function dataDir(): string {
|
|
7
|
+
const configured = process.env.FREE_PI_DATA_DIR?.trim();
|
|
8
|
+
if (configured) return configured;
|
|
9
|
+
return join(homedir(), ".cache", "opencode", "free-pi");
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function dataPath(name: string): string {
|
|
13
|
+
return join(dataDir(), name);
|
|
14
|
+
}
|
package/dist/session/cache.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Prompt-cache miss notices, mirroring pi's `cache-stats` detector.
|
|
3
|
-
*
|
|
4
|
-
* free-pi reports no per-token cost, so only the token threshold applies. The
|
|
5
|
-
* notices are opt-in because OpenCode has no toast surface for them.
|
|
6
|
-
*/
|
|
7
|
-
import type { Deps } from "../deps.js";
|
|
8
|
-
import type { TokenUsageLike } from "../types.js";
|
|
9
|
-
/**
|
|
10
|
-
* Records one step's usage and logs a miss when the prompt was re-billed.
|
|
11
|
-
* `session.step.ended` carries per-step tokens; `session.usage.updated` is a
|
|
12
|
-
* cumulative total and must not be fed here.
|
|
13
|
-
*/
|
|
14
|
-
export declare function observeStepUsage(deps: Deps, sessionID: string | undefined, tokens: TokenUsageLike | undefined): void;
|
|
15
|
-
export declare function forgetCacheMiss(deps: Deps, sessionID: string): void;
|