usegrunt 0.6.2 → 0.6.3
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 +9 -2
- package/dist/local/consent-source.js +27 -0
- package/dist/local/data-sharing.js +100 -0
- package/dist/local/deepseek-response.js +16 -0
- package/dist/local/fallback-setup.js +52 -3
- package/dist/local/free-api.js +137 -0
- package/dist/local/main.js +33 -9
- package/dist/local/managed-router.js +32 -0
- package/dist/local/nvidia.js +116 -0
- package/dist/local/opencode.js +100 -7
- package/dist/local/registry-source.js +26 -0
- package/dist/local/setup.js +269 -70
- package/dist/local/tokenharbor.js +64 -0
- package/dist/local/wizard.js +46 -9
- package/dist/local/zro.js +79 -0
- package/dist/registry/adapters.json +32 -4
- package/dist/registry.js +6 -4
- package/package.json +1 -1
package/dist/local/opencode.js
CHANGED
|
@@ -6,12 +6,21 @@ import { userTurns } from './harness.js';
|
|
|
6
6
|
import { executable } from './multiplexer.js';
|
|
7
7
|
import { loopback } from './freellmapi.js';
|
|
8
8
|
import { SourceError } from './source.js';
|
|
9
|
+
export const DEFAULT_OPENCODE_MODELS = ['deepseek-v4.1-flash-free', 'glm-5.3-flash-free', 'big-pickle'];
|
|
10
|
+
const MODEL_QUOTA_COOLDOWN_MS = 60000;
|
|
11
|
+
class UnavailableModel extends SourceError {
|
|
12
|
+
constructor() { super('upstream', 'OpenCode free model is unavailable'); }
|
|
13
|
+
}
|
|
9
14
|
export class OpenCode {
|
|
10
15
|
options;
|
|
11
16
|
id = 'opencode';
|
|
12
17
|
child;
|
|
13
18
|
starting;
|
|
14
19
|
sessions = new Map();
|
|
20
|
+
sessionModels = new Map();
|
|
21
|
+
sessionParents = new Map();
|
|
22
|
+
modelCooldowns = new Map();
|
|
23
|
+
nextModel = 0;
|
|
15
24
|
prepared = new Map();
|
|
16
25
|
busy = new Set();
|
|
17
26
|
shutdown = new AbortController();
|
|
@@ -64,21 +73,70 @@ export class OpenCode {
|
|
|
64
73
|
resolve(address);
|
|
65
74
|
}
|
|
66
75
|
});
|
|
67
|
-
const failed = () => { clearTimeout(timer); this.starting = undefined; this.sessions.clear(); this.prepared.clear(); reject(new SourceError('upstream', 'OpenCode server stopped; run opencode serve to check installation')); };
|
|
76
|
+
const failed = () => { clearTimeout(timer); this.starting = undefined; this.sessions.clear(); this.sessionModels.clear(); this.sessionParents.clear(); this.prepared.clear(); reject(new SourceError('upstream', 'OpenCode server stopped; run opencode serve to check installation')); };
|
|
68
77
|
child.once('error', failed);
|
|
69
78
|
child.once('exit', failed);
|
|
70
79
|
});
|
|
71
80
|
return this.starting;
|
|
72
81
|
}
|
|
73
82
|
async complete(request, caller) {
|
|
83
|
+
const signal = AbortSignal.any([caller, this.shutdown.signal, ...(this.options.timeout ? [AbortSignal.timeout(this.options.timeout)] : [])]);
|
|
84
|
+
signal.throwIfAborted();
|
|
85
|
+
const scalar = typeof this.options.model === 'function' ? this.options.model() : this.options.model;
|
|
86
|
+
const configured = typeof this.options.models === 'function' ? this.options.models() : this.options.models;
|
|
87
|
+
const models = [...new Set(scalar === undefined ? configured ?? DEFAULT_OPENCODE_MODELS : [scalar])];
|
|
88
|
+
if (!models.length || models.some(model => typeof model !== 'string' || !model.trim()))
|
|
89
|
+
throw new SourceError('upstream', 'OpenCode model pool is empty or invalid');
|
|
90
|
+
const turns = userTurns(request), cwd = await realpath(request.cwd);
|
|
91
|
+
const key = (history) => createHash('sha256').update(JSON.stringify([cwd, history])).digest('hex');
|
|
92
|
+
const prior = this.sessions.get(key(turns)) ?? (turns.length > 1 ? this.sessions.get(key(turns.slice(0, -1))) : undefined);
|
|
93
|
+
const sticky = prior && !this.busy.has(prior) ? this.sessionModels.get(prior) : undefined;
|
|
94
|
+
const start = sticky && models.includes(sticky) ? models.indexOf(sticky) : this.nextModel % models.length;
|
|
95
|
+
const candidates = [...models.slice(start), ...models.slice(0, start)];
|
|
96
|
+
const attempt = { retryable: false };
|
|
97
|
+
let last;
|
|
98
|
+
try {
|
|
99
|
+
for (const model of candidates) {
|
|
100
|
+
if ((this.modelCooldowns.get(model) ?? 0) > Date.now())
|
|
101
|
+
continue;
|
|
102
|
+
signal.throwIfAborted();
|
|
103
|
+
if (model !== sticky)
|
|
104
|
+
this.nextModel = (models.indexOf(model) + 1) % models.length;
|
|
105
|
+
try {
|
|
106
|
+
return await this.completeModel(request, caller, model, signal, attempt);
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
if (signal.aborted || !(error instanceof SourceError) || (error.code !== 'quota' && !(error instanceof UnavailableModel)))
|
|
110
|
+
throw error;
|
|
111
|
+
last = error;
|
|
112
|
+
this.modelCooldowns.set(model, Date.now() + (error.retryAfter === undefined ? MODEL_QUOTA_COOLDOWN_MS : error.retryAfter * 1000));
|
|
113
|
+
if (!attempt.retryable)
|
|
114
|
+
throw error;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
const retryAfter = Math.max(1, Math.ceil((Math.min(...models.map(model => this.modelCooldowns.get(model) ?? Date.now())) - Date.now()) / 1000));
|
|
118
|
+
throw new SourceError('quota', 'OpenCode free models are temporarily exhausted or unavailable', retryAfter, last?.output);
|
|
119
|
+
}
|
|
120
|
+
finally {
|
|
121
|
+
if (attempt.session)
|
|
122
|
+
this.busy.delete(attempt.session);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
async completeModel(request, caller, modelID, signal, attempt) {
|
|
74
126
|
const turns = userTurns(request), prompt = request.handoffPrompt ?? turns.at(-1);
|
|
75
127
|
if (!prompt)
|
|
76
128
|
throw new SourceError('upstream', 'No user text in this request');
|
|
77
129
|
const cwd = await realpath(request.cwd);
|
|
78
130
|
const key = (history) => createHash('sha256').update(JSON.stringify([cwd, history])).digest('hex');
|
|
79
131
|
const prior = this.sessions.get(key(turns)) ?? (turns.length > 1 ? this.sessions.get(key(turns.slice(0, -1))) : undefined);
|
|
80
|
-
const continuation = prior && !this.busy.has(prior) ? prior : undefined;
|
|
81
|
-
|
|
132
|
+
const continuation = attempt.session ?? (prior && !this.busy.has(prior) ? prior : undefined);
|
|
133
|
+
attempt.retryable = false;
|
|
134
|
+
// Reserve a continuation before any asynchronous server work, and keep the
|
|
135
|
+
// reservation across quota retries of this same user turn.
|
|
136
|
+
if (continuation) {
|
|
137
|
+
attempt.session = continuation;
|
|
138
|
+
this.busy.add(continuation);
|
|
139
|
+
}
|
|
82
140
|
let session, base;
|
|
83
141
|
const events = new AbortController();
|
|
84
142
|
let reader;
|
|
@@ -142,6 +200,7 @@ export class OpenCode {
|
|
|
142
200
|
session = continuation ?? (await call('/session', { title: 'grunt' })).id;
|
|
143
201
|
if (!session || !/^[a-zA-Z0-9_-]+$/.test(session))
|
|
144
202
|
throw new SourceError('upstream', 'OpenCode returned an invalid session');
|
|
203
|
+
attempt.session = session;
|
|
145
204
|
this.busy.add(session);
|
|
146
205
|
// OpenCode 1.18.31: subscribe before sending the message so a fast
|
|
147
206
|
// permission request cannot be missed, including on an existing server.
|
|
@@ -181,8 +240,10 @@ export class OpenCode {
|
|
|
181
240
|
}
|
|
182
241
|
}
|
|
183
242
|
})().catch(error => { guardFailed = true; throw error; });
|
|
184
|
-
const
|
|
185
|
-
|
|
243
|
+
const reply = await Promise.race([watching, call(`/session/${session}/message`, {
|
|
244
|
+
...(attempt.messageID ? { messageID: attempt.messageID } : {}),
|
|
245
|
+
model: { providerID: 'opencode', modelID }, parts: [{ ...(attempt.partID ? { id: attempt.partID } : {}), type: 'text', text: prompt }]
|
|
246
|
+
})]);
|
|
186
247
|
const parts = reply.parts ?? [];
|
|
187
248
|
const text = parts.filter(p => p.type === 'text' && typeof p.text === 'string').map(p => p.text).join('\n');
|
|
188
249
|
if (reply.info?.error) {
|
|
@@ -191,11 +252,16 @@ export class OpenCode {
|
|
|
191
252
|
throw new SourceError('logged_out', 'OpenCode free model requires sign-in');
|
|
192
253
|
if (error.data?.statusCode === 429)
|
|
193
254
|
throw new SourceError('quota', 'OpenCode free model quota exhausted', undefined, text);
|
|
255
|
+
if (error.data?.statusCode === 404 || error.data?.code === 'model_not_found' || ['ModelNotFoundError', 'ProviderModelNotFoundError'].includes(error.name))
|
|
256
|
+
throw new UnavailableModel();
|
|
194
257
|
throw new SourceError('refused', 'OpenCode provider refused the request');
|
|
195
258
|
}
|
|
196
259
|
if (!text)
|
|
197
260
|
throw new SourceError('upstream', 'OpenCode returned no assistant text');
|
|
198
261
|
this.sessions.set(key(turns), session);
|
|
262
|
+
this.sessionModels.set(session, modelID);
|
|
263
|
+
if (typeof reply.info?.parentID === 'string')
|
|
264
|
+
this.sessionParents.set(session, reply.info.parentID);
|
|
199
265
|
return { text };
|
|
200
266
|
}
|
|
201
267
|
catch (error) {
|
|
@@ -207,6 +273,35 @@ export class OpenCode {
|
|
|
207
273
|
throw new SourceError('cancelled', 'OpenCode request cancelled during shutdown');
|
|
208
274
|
throw new SourceError('timeout', 'OpenCode request timed out');
|
|
209
275
|
}
|
|
276
|
+
if (error instanceof SourceError && (error.code === 'quota' || error instanceof UnavailableModel) && session && base) {
|
|
277
|
+
// A provider error can arrive after OpenCode has persisted the user
|
|
278
|
+
// prompt. Reuse that prompt's IDs rather than appending it again. Never
|
|
279
|
+
// replay a turn that already ran native tools, or whose state is unclear.
|
|
280
|
+
try {
|
|
281
|
+
const response = await this.fetcher(`${base}/session/${session}/message`, { headers, redirect: 'error', signal: turnSignal });
|
|
282
|
+
check(response);
|
|
283
|
+
const messages = await response.json();
|
|
284
|
+
if (Array.isArray(messages)) {
|
|
285
|
+
const userIndex = messages.findLastIndex(message => message.info?.role === 'user');
|
|
286
|
+
const user = messages[userIndex];
|
|
287
|
+
const usedTools = messages.slice(Math.max(0, userIndex)).some(message => message.parts?.some(part => part.type === 'tool'));
|
|
288
|
+
if (!usedTools && (messages.length === 0 || user?.info.id === this.sessionParents.get(session))) {
|
|
289
|
+
attempt.session = session;
|
|
290
|
+
delete attempt.messageID;
|
|
291
|
+
delete attempt.partID;
|
|
292
|
+
attempt.retryable = true;
|
|
293
|
+
}
|
|
294
|
+
else if (!usedTools && user?.parts?.length === 1 && user.parts[0]?.type === 'text' && user.parts[0].text === prompt
|
|
295
|
+
&& /^msg[a-zA-Z0-9_-]+$/.test(user.info.id) && /^prt[a-zA-Z0-9_-]+$/.test(user.parts[0].id)) {
|
|
296
|
+
attempt.session = session;
|
|
297
|
+
attempt.messageID = user.info.id;
|
|
298
|
+
attempt.partID = user.parts[0].id;
|
|
299
|
+
attempt.retryable = true;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
catch { /* Unknown persisted state is not safe to replay automatically. */ }
|
|
304
|
+
}
|
|
210
305
|
if (error instanceof SourceError)
|
|
211
306
|
throw error;
|
|
212
307
|
throw new SourceError('upstream', 'OpenCode request failed');
|
|
@@ -215,8 +310,6 @@ export class OpenCode {
|
|
|
215
310
|
events.abort();
|
|
216
311
|
await reader?.cancel().catch(() => { });
|
|
217
312
|
await watching?.catch(() => { });
|
|
218
|
-
if (session)
|
|
219
|
-
this.busy.delete(session);
|
|
220
313
|
}
|
|
221
314
|
}
|
|
222
315
|
async close() {
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { SourceError } from './source.js';
|
|
2
|
+
// Mixed legacy/custom source lists do not use StickyRouter. Keep the current
|
|
3
|
+
// registry's revocation boundary around every known source in that path too.
|
|
4
|
+
export class RegistrySource {
|
|
5
|
+
source;
|
|
6
|
+
registry;
|
|
7
|
+
id;
|
|
8
|
+
supportsFunctionTools;
|
|
9
|
+
constructor(source, registry) {
|
|
10
|
+
this.source = source;
|
|
11
|
+
this.registry = registry;
|
|
12
|
+
this.id = source.id;
|
|
13
|
+
if (source.supportsFunctionTools !== undefined)
|
|
14
|
+
this.supportsFunctionTools = source.supportsFunctionTools;
|
|
15
|
+
}
|
|
16
|
+
listed() { return this.registry().adapters.some(adapter => adapter.id === this.id); }
|
|
17
|
+
async available() { return this.listed() && await this.source.available(); }
|
|
18
|
+
async healthCheck() { return this.listed() && await (this.source.healthCheck?.() ?? this.source.available()); }
|
|
19
|
+
complete(request, signal) {
|
|
20
|
+
signal.throwIfAborted();
|
|
21
|
+
if (!this.listed())
|
|
22
|
+
throw new SourceError('refused', `${this.id} is not in the current adapter registry`);
|
|
23
|
+
return this.source.complete(request, signal);
|
|
24
|
+
}
|
|
25
|
+
async close() { await this.source.close?.(); }
|
|
26
|
+
}
|