opencode-devin 0.1.1 → 0.2.1
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 +105 -18
- package/dist/auth/browser.d.ts +7 -0
- package/dist/auth/browser.js +18 -0
- package/dist/auth/login.d.ts +15 -0
- package/dist/auth/login.js +51 -0
- package/dist/auth/loopback.d.ts +1 -1
- package/dist/auth/loopback.js +38 -2
- package/dist/credentials.js +5 -1
- package/dist/index.js +15 -39
- package/dist/protocol/auth.d.ts +7 -1
- package/dist/protocol/auth.js +26 -4
- package/dist/protocol/catalog.d.ts +18 -5
- package/dist/protocol/catalog.js +53 -30
- package/dist/protocol/chat.js +42 -11
- package/dist/protocol/effort.d.ts +20 -0
- package/dist/protocol/effort.js +31 -0
- package/dist/protocol/model.d.ts +29 -12
- package/dist/protocol/model.js +300 -138
- package/package.json +9 -6
package/dist/protocol/model.js
CHANGED
|
@@ -9,37 +9,29 @@
|
|
|
9
9
|
* Originally ported from ai-sdk-devin / pi-devin-auth (MIT).
|
|
10
10
|
*/
|
|
11
11
|
import { streamChatEvents } from "./chat.js";
|
|
12
|
-
import { getCachedCatalog } from "./catalog.js";
|
|
12
|
+
import { getCachedCatalog, ModelNotAvailableError, } from "./catalog.js";
|
|
13
|
+
import { splitEffortSuffix, toEffortLevel, withEffortSuffix } from "./effort.js";
|
|
13
14
|
import { DEFAULT_API_SERVER } from "../constants.js";
|
|
14
|
-
/** Valid reasoning effort levels (encoded as UID suffixes in the catalog). */
|
|
15
|
-
const EFFORT_LEVELS = ["none", "low", "medium", "high", "xhigh", "max"];
|
|
16
15
|
/**
|
|
17
16
|
* Resolve the actual model UID to send upstream, applying an optional
|
|
18
17
|
* reasoning-effort override from providerOptions: swap an existing effort
|
|
19
18
|
* suffix, or append one when the catalog lists that variant.
|
|
19
|
+
*
|
|
20
|
+
* Exported for tests: this is what the catalog lookup exists for, so it needs
|
|
21
|
+
* to stay covered independently of the network.
|
|
20
22
|
*/
|
|
21
|
-
function resolveEffortUid(modelId, catalog, requestedEffort) {
|
|
22
|
-
const effort =
|
|
23
|
-
if (!
|
|
23
|
+
export function resolveEffortUid(modelId, catalog, requestedEffort) {
|
|
24
|
+
const effort = toEffortLevel(requestedEffort);
|
|
25
|
+
if (!effort)
|
|
24
26
|
return modelId;
|
|
25
|
-
const
|
|
26
|
-
if (
|
|
27
|
-
return
|
|
27
|
+
const split = splitEffortSuffix(modelId);
|
|
28
|
+
if (split.effortLevel)
|
|
29
|
+
return withEffortSuffix(split.baseModelUid, effort);
|
|
28
30
|
if (catalog?.some((entry) => entry.baseModelUid === modelId && entry.effortLevel === effort)) {
|
|
29
|
-
return modelId
|
|
31
|
+
return withEffortSuffix(modelId, effort);
|
|
30
32
|
}
|
|
31
33
|
return modelId;
|
|
32
34
|
}
|
|
33
|
-
/** Calculate the dollar cost of a request from token usage and model pricing. */
|
|
34
|
-
export function calculateCost(usage, pricing) {
|
|
35
|
-
if (!pricing)
|
|
36
|
-
return undefined;
|
|
37
|
-
const input = ((usage.inputTokens ?? 0) / 1_000_000) * pricing.input;
|
|
38
|
-
const cached = ((usage.cachedInputTokens ?? 0) / 1_000_000) * pricing.cachedInput;
|
|
39
|
-
const output = ((usage.outputTokens ?? 0) / 1_000_000) * pricing.output;
|
|
40
|
-
const total = input + cached + output;
|
|
41
|
-
return total > 0 ? Math.round(total * 1_000_000) / 1_000_000 : 0;
|
|
42
|
-
}
|
|
43
35
|
// --- Prompt conversion: LanguageModelV3Prompt → ChatHistoryItem[] ---
|
|
44
36
|
function convertPrompt(prompt) {
|
|
45
37
|
const items = [];
|
|
@@ -114,105 +106,149 @@ const DEFAULT_USAGE = {
|
|
|
114
106
|
inputTokens: { total: 0, noCache: undefined, cacheRead: undefined, cacheWrite: undefined },
|
|
115
107
|
outputTokens: { total: 0, text: undefined, reasoning: undefined },
|
|
116
108
|
};
|
|
117
|
-
|
|
109
|
+
/** Sentinel distinguishing a timer wake-up from an upstream event. */
|
|
110
|
+
const TICK = Symbol("coalesce-tick");
|
|
111
|
+
/**
|
|
112
|
+
* Exported for tests: the Cascade event -> AI SDK stream translation is pure,
|
|
113
|
+
* and interleaved parallel tool calls are the case most likely to regress.
|
|
114
|
+
*/
|
|
115
|
+
export async function* convertStreamEvents(events, generateId) {
|
|
118
116
|
let textId = "";
|
|
119
117
|
let reasoningId = "";
|
|
120
118
|
let textOpen = false;
|
|
121
119
|
let reasoningOpen = false;
|
|
122
|
-
let currentToolId = "";
|
|
123
|
-
let currentToolName = "";
|
|
124
|
-
let toolInputOpen = false;
|
|
125
|
-
let pendingToolArgs = "";
|
|
126
120
|
let finishReason = "stop";
|
|
127
121
|
let usage;
|
|
128
122
|
let textBuf = "";
|
|
129
123
|
let textLastFlush = 0;
|
|
130
124
|
let reasoningBuf = "";
|
|
131
125
|
let reasoningLastFlush = 0;
|
|
132
|
-
|
|
133
|
-
|
|
126
|
+
// Cascade interleaves argument deltas across parallel tool calls, so pending
|
|
127
|
+
// arguments are buffered per call id. A single shared buffer would splice two
|
|
128
|
+
// calls' JSON into one unparsable string.
|
|
129
|
+
const openTools = new Map();
|
|
130
|
+
// Deltas that carry no id belong to the most recently opened call.
|
|
131
|
+
let lastToolId = "";
|
|
132
|
+
const pendingDeltas = () => {
|
|
133
|
+
const parts = [];
|
|
134
|
+
if (textBuf) {
|
|
135
|
+
parts.push({ type: "text-delta", id: textId, delta: textBuf });
|
|
136
|
+
textBuf = "";
|
|
137
|
+
}
|
|
138
|
+
if (reasoningBuf) {
|
|
139
|
+
parts.push({ type: "reasoning-delta", id: reasoningId, delta: reasoningBuf });
|
|
140
|
+
reasoningBuf = "";
|
|
141
|
+
}
|
|
142
|
+
return parts;
|
|
143
|
+
};
|
|
144
|
+
const closeBlocks = () => {
|
|
145
|
+
const parts = [];
|
|
146
|
+
if (textOpen) {
|
|
147
|
+
parts.push({ type: "text-end", id: textId });
|
|
148
|
+
textOpen = false;
|
|
149
|
+
}
|
|
150
|
+
if (reasoningOpen) {
|
|
151
|
+
parts.push({ type: "reasoning-end", id: reasoningId });
|
|
152
|
+
reasoningOpen = false;
|
|
153
|
+
}
|
|
154
|
+
return parts;
|
|
155
|
+
};
|
|
156
|
+
// The protocol has no end-of-tool-call marker, so a call cannot be completed
|
|
157
|
+
// until the stream ends. They are emitted in the order they were started.
|
|
158
|
+
const completedToolCalls = () => {
|
|
159
|
+
const parts = [];
|
|
160
|
+
for (const [id, tool] of openTools) {
|
|
161
|
+
parts.push({ type: "tool-call", toolCallId: id, toolName: tool.name, input: tool.args });
|
|
162
|
+
}
|
|
163
|
+
openTools.clear();
|
|
164
|
+
return parts;
|
|
165
|
+
};
|
|
166
|
+
/** Flush only the buffers that have been waiting longer than the interval. */
|
|
167
|
+
const staleDeltas = () => {
|
|
168
|
+
const now = Date.now();
|
|
169
|
+
const parts = [];
|
|
170
|
+
if (textBuf && now - textLastFlush >= COALESCE_INTERVAL_MS) {
|
|
171
|
+
parts.push({ type: "text-delta", id: textId, delta: textBuf });
|
|
172
|
+
textBuf = "";
|
|
173
|
+
textLastFlush = now;
|
|
174
|
+
}
|
|
175
|
+
if (reasoningBuf && now - reasoningLastFlush >= COALESCE_INTERVAL_MS) {
|
|
176
|
+
parts.push({ type: "reasoning-delta", id: reasoningId, delta: reasoningBuf });
|
|
177
|
+
reasoningBuf = "";
|
|
178
|
+
reasoningLastFlush = now;
|
|
179
|
+
}
|
|
180
|
+
return parts;
|
|
181
|
+
};
|
|
182
|
+
const handleEvent = (event) => {
|
|
183
|
+
const parts = [];
|
|
134
184
|
switch (event.kind) {
|
|
135
185
|
case "text": {
|
|
186
|
+
// Flush the other block before opening this one, so the parts stay in
|
|
187
|
+
// causal order. Without this, text buffered right before the model
|
|
188
|
+
// starts thinking would stay invisible until more text, a tool call, or
|
|
189
|
+
// the end of the stream.
|
|
190
|
+
if (reasoningOpen && reasoningBuf) {
|
|
191
|
+
parts.push({ type: "reasoning-delta", id: reasoningId, delta: reasoningBuf });
|
|
192
|
+
reasoningBuf = "";
|
|
193
|
+
reasoningLastFlush = Date.now();
|
|
194
|
+
}
|
|
136
195
|
if (!textOpen) {
|
|
137
196
|
textId = generateId();
|
|
138
197
|
textOpen = true;
|
|
139
198
|
textLastFlush = Date.now();
|
|
140
|
-
|
|
199
|
+
parts.push({ type: "text-start", id: textId });
|
|
141
200
|
}
|
|
142
201
|
textBuf += event.text;
|
|
143
202
|
if (textBuf.length >= COALESCE_MAX_BYTES || Date.now() - textLastFlush >= COALESCE_INTERVAL_MS) {
|
|
144
|
-
|
|
203
|
+
parts.push({ type: "text-delta", id: textId, delta: textBuf });
|
|
145
204
|
textBuf = "";
|
|
146
205
|
textLastFlush = Date.now();
|
|
147
206
|
}
|
|
148
207
|
break;
|
|
149
208
|
}
|
|
150
209
|
case "reasoning": {
|
|
210
|
+
if (textOpen && textBuf) {
|
|
211
|
+
parts.push({ type: "text-delta", id: textId, delta: textBuf });
|
|
212
|
+
textBuf = "";
|
|
213
|
+
textLastFlush = Date.now();
|
|
214
|
+
}
|
|
151
215
|
if (!reasoningOpen) {
|
|
152
216
|
reasoningId = generateId();
|
|
153
217
|
reasoningOpen = true;
|
|
154
218
|
reasoningLastFlush = Date.now();
|
|
155
|
-
|
|
219
|
+
parts.push({ type: "reasoning-start", id: reasoningId });
|
|
156
220
|
}
|
|
157
221
|
reasoningBuf += event.text;
|
|
158
222
|
if (reasoningBuf.length >= COALESCE_MAX_BYTES || Date.now() - reasoningLastFlush >= COALESCE_INTERVAL_MS) {
|
|
159
|
-
|
|
223
|
+
parts.push({ type: "reasoning-delta", id: reasoningId, delta: reasoningBuf });
|
|
160
224
|
reasoningBuf = "";
|
|
161
225
|
reasoningLastFlush = Date.now();
|
|
162
226
|
}
|
|
163
227
|
break;
|
|
164
228
|
}
|
|
165
229
|
case "tool_call_start": {
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
if (
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
yield { type: "text-end", id: textId };
|
|
176
|
-
textOpen = false;
|
|
177
|
-
}
|
|
178
|
-
if (reasoningOpen) {
|
|
179
|
-
yield { type: "reasoning-end", id: reasoningId };
|
|
180
|
-
reasoningOpen = false;
|
|
181
|
-
}
|
|
182
|
-
if (toolInputOpen) {
|
|
183
|
-
yield { type: "tool-call", toolCallId: currentToolId, toolName: currentToolName, input: pendingToolArgs };
|
|
184
|
-
}
|
|
185
|
-
currentToolId = event.id || generateId();
|
|
186
|
-
currentToolName = event.name;
|
|
187
|
-
toolInputOpen = true;
|
|
188
|
-
pendingToolArgs = "";
|
|
230
|
+
parts.push(...pendingDeltas());
|
|
231
|
+
parts.push(...closeBlocks());
|
|
232
|
+
const id = event.id || generateId();
|
|
233
|
+
const open = openTools.get(id);
|
|
234
|
+
if (open)
|
|
235
|
+
open.name = event.name;
|
|
236
|
+
else
|
|
237
|
+
openTools.set(id, { name: event.name, args: "" });
|
|
238
|
+
lastToolId = id;
|
|
189
239
|
break;
|
|
190
240
|
}
|
|
191
241
|
case "tool_call_args": {
|
|
192
|
-
|
|
242
|
+
const open = openTools.get(event.id ?? lastToolId);
|
|
243
|
+
// An orphan delta has no call to attach to; dropping it beats inventing
|
|
244
|
+
// a nameless tool call.
|
|
245
|
+
if (open)
|
|
246
|
+
open.args += event.argsDelta;
|
|
193
247
|
break;
|
|
194
248
|
}
|
|
195
249
|
case "finish": {
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
textBuf = "";
|
|
199
|
-
}
|
|
200
|
-
if (reasoningBuf) {
|
|
201
|
-
yield { type: "reasoning-delta", id: reasoningId, delta: reasoningBuf };
|
|
202
|
-
reasoningBuf = "";
|
|
203
|
-
}
|
|
204
|
-
if (textOpen) {
|
|
205
|
-
yield { type: "text-end", id: textId };
|
|
206
|
-
textOpen = false;
|
|
207
|
-
}
|
|
208
|
-
if (reasoningOpen) {
|
|
209
|
-
yield { type: "reasoning-end", id: reasoningId };
|
|
210
|
-
reasoningOpen = false;
|
|
211
|
-
}
|
|
212
|
-
if (toolInputOpen) {
|
|
213
|
-
yield { type: "tool-call", toolCallId: currentToolId, toolName: currentToolName, input: pendingToolArgs };
|
|
214
|
-
toolInputOpen = false;
|
|
215
|
-
}
|
|
250
|
+
parts.push(...pendingDeltas());
|
|
251
|
+
parts.push(...closeBlocks());
|
|
216
252
|
finishReason = event.reason;
|
|
217
253
|
break;
|
|
218
254
|
}
|
|
@@ -237,34 +273,77 @@ async function* convertStreamEvents(events, generateId, pricing) {
|
|
|
237
273
|
break;
|
|
238
274
|
}
|
|
239
275
|
}
|
|
276
|
+
return parts;
|
|
277
|
+
};
|
|
278
|
+
// The loop is woken by a timer as well as by upstream events, so text that
|
|
279
|
+
// arrives and is then followed by silence is still delivered promptly instead
|
|
280
|
+
// of waiting for the next event or the end of the stream.
|
|
281
|
+
//
|
|
282
|
+
// A single `next()` is kept in flight across iterations: when the timer wins
|
|
283
|
+
// the race the pending read is handed to the next iteration rather than
|
|
284
|
+
// dropped, which is what keeps this from losing events.
|
|
285
|
+
const upstream = events[Symbol.asyncIterator]();
|
|
286
|
+
let inFlight = null;
|
|
287
|
+
const nextEvent = () => (inFlight ??= upstream.next());
|
|
288
|
+
let wake;
|
|
289
|
+
const ticker = setInterval(() => wake?.(), COALESCE_INTERVAL_MS);
|
|
290
|
+
const tick = () => new Promise((resolve) => {
|
|
291
|
+
wake = () => {
|
|
292
|
+
wake = undefined;
|
|
293
|
+
resolve(TICK);
|
|
294
|
+
};
|
|
295
|
+
});
|
|
296
|
+
// With nothing buffered there is nothing to flush, so the timer race — and its
|
|
297
|
+
// allocation — is skipped entirely for the common fast-streaming path.
|
|
298
|
+
const nextStep = () => textBuf || reasoningBuf ? Promise.race([nextEvent(), tick()]) : nextEvent();
|
|
299
|
+
yield { type: "stream-start", warnings: [] };
|
|
300
|
+
try {
|
|
301
|
+
for (;;) {
|
|
302
|
+
const step = await nextStep();
|
|
303
|
+
if (step === TICK) {
|
|
304
|
+
// The upstream read is still in flight; keep it for the next iteration.
|
|
305
|
+
yield* staleDeltas();
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
// Consume the read. Without clearing it here, the same settled promise
|
|
309
|
+
// would be handed back on the next iteration and the same event replayed
|
|
310
|
+
// forever.
|
|
311
|
+
inFlight = null;
|
|
312
|
+
if (step.done)
|
|
313
|
+
break;
|
|
314
|
+
yield* handleEvent(step.value);
|
|
315
|
+
}
|
|
240
316
|
}
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
if (textOpen)
|
|
247
|
-
yield { type: "text-end", id: textId };
|
|
248
|
-
if (reasoningOpen)
|
|
249
|
-
yield { type: "reasoning-end", id: reasoningId };
|
|
250
|
-
if (toolInputOpen) {
|
|
251
|
-
yield { type: "tool-call", toolCallId: currentToolId, toolName: currentToolName, input: pendingToolArgs };
|
|
317
|
+
finally {
|
|
318
|
+
clearInterval(ticker);
|
|
319
|
+
inFlight = null;
|
|
320
|
+
// Let the upstream generator finish so its own cleanup runs.
|
|
321
|
+
void upstream.return?.(undefined);
|
|
252
322
|
}
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
323
|
+
// Final flush
|
|
324
|
+
yield* pendingDeltas();
|
|
325
|
+
yield* closeBlocks();
|
|
326
|
+
yield* completedToolCalls();
|
|
257
327
|
const rawReason = FINISH_REASON_MAP[finishReason] ?? "stop";
|
|
258
328
|
yield {
|
|
259
329
|
type: "finish",
|
|
260
330
|
finishReason: { unified: rawReason, raw: finishReason },
|
|
261
331
|
usage: usage ?? DEFAULT_USAGE,
|
|
262
|
-
...(cost !== undefined ? { providerMetadata: { devin: { cost } } } : {}),
|
|
263
332
|
};
|
|
264
333
|
}
|
|
265
|
-
|
|
334
|
+
/** Exported for tests; mirrors `convertStreamEvents` for the non-streaming path. */
|
|
335
|
+
export async function collectEvents(events) {
|
|
266
336
|
const result = { text: "", reasoning: "", toolCalls: [], finishReason: "stop" };
|
|
267
|
-
|
|
337
|
+
// Keyed by call id for the same reason as the streaming path: parallel tool
|
|
338
|
+
// calls interleave their argument deltas.
|
|
339
|
+
const openTools = new Map();
|
|
340
|
+
let lastToolId = "";
|
|
341
|
+
let anonymous = 0;
|
|
342
|
+
const commit = () => {
|
|
343
|
+
for (const tool of openTools.values())
|
|
344
|
+
result.toolCalls.push(tool);
|
|
345
|
+
openTools.clear();
|
|
346
|
+
};
|
|
268
347
|
for await (const event of events) {
|
|
269
348
|
switch (event.kind) {
|
|
270
349
|
case "text":
|
|
@@ -273,20 +352,25 @@ async function collectEvents(events) {
|
|
|
273
352
|
case "reasoning":
|
|
274
353
|
result.reasoning += event.text;
|
|
275
354
|
break;
|
|
276
|
-
case "tool_call_start":
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
355
|
+
case "tool_call_start": {
|
|
356
|
+
// A start without an id still needs its own buffer, otherwise two
|
|
357
|
+
// anonymous calls would share one.
|
|
358
|
+
const id = event.id ?? `__anonymous_${anonymous++}`;
|
|
359
|
+
const open = openTools.get(id);
|
|
360
|
+
if (open)
|
|
361
|
+
open.name = event.name;
|
|
362
|
+
else
|
|
363
|
+
openTools.set(id, { id: event.id ?? id, name: event.name, args: "" });
|
|
364
|
+
lastToolId = id;
|
|
280
365
|
break;
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
366
|
+
}
|
|
367
|
+
case "tool_call_args": {
|
|
368
|
+
const open = openTools.get(event.id ?? lastToolId);
|
|
369
|
+
if (open)
|
|
370
|
+
open.args += event.argsDelta;
|
|
284
371
|
break;
|
|
372
|
+
}
|
|
285
373
|
case "finish":
|
|
286
|
-
if (currentTool) {
|
|
287
|
-
result.toolCalls.push(currentTool);
|
|
288
|
-
currentTool = null;
|
|
289
|
-
}
|
|
290
374
|
result.finishReason = event.reason;
|
|
291
375
|
break;
|
|
292
376
|
case "usage":
|
|
@@ -302,18 +386,65 @@ async function collectEvents(events) {
|
|
|
302
386
|
break;
|
|
303
387
|
}
|
|
304
388
|
}
|
|
305
|
-
|
|
306
|
-
result.toolCalls.push(currentTool);
|
|
389
|
+
commit();
|
|
307
390
|
return result;
|
|
308
391
|
}
|
|
392
|
+
/**
|
|
393
|
+
* Refuse a request the catalog says cannot succeed, and warn about one it cannot
|
|
394
|
+
* vouch for.
|
|
395
|
+
*
|
|
396
|
+
* Disabled is a hard stop because the account cannot use it. Absent is only a
|
|
397
|
+
* warning: the catalog is not a contract that it enumerates every uid Cascade
|
|
398
|
+
* accepts, so the upstream response stays the authority.
|
|
399
|
+
*/
|
|
400
|
+
function checkModelUsable(modelId, catalog) {
|
|
401
|
+
if (!catalog)
|
|
402
|
+
return;
|
|
403
|
+
const entry = catalog.entries.find((candidate) => candidate.modelUid === modelId);
|
|
404
|
+
if (!entry) {
|
|
405
|
+
console.warn(`[opencode-devin] model "${modelId}" is not in the Cascade catalog for this account; sending the request anyway`);
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
if (entry.disabled)
|
|
409
|
+
throw new ModelNotAvailableError(modelId, entry.label);
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* Whether the catalog vouches for this model.
|
|
413
|
+
*
|
|
414
|
+
* `true` when the account's catalog lists it, `false` when the catalog loaded
|
|
415
|
+
* and does not mention it, `undefined` when no catalog was available at all.
|
|
416
|
+
*/
|
|
417
|
+
function isKnownToCatalog(modelId, catalog) {
|
|
418
|
+
if (!catalog)
|
|
419
|
+
return undefined;
|
|
420
|
+
return catalog.entries.some((entry) => entry.modelUid === modelId);
|
|
421
|
+
}
|
|
422
|
+
/**
|
|
423
|
+
* Add the likely cause to a failure for a model the catalog never listed.
|
|
424
|
+
*
|
|
425
|
+
* Refusing such a model up front would be wrong: the catalog is not a contract
|
|
426
|
+
* that it enumerates every uid Cascade accepts, so a false rejection would break
|
|
427
|
+
* a model that does work. Instead the request is allowed to fail the way
|
|
428
|
+
* upstream makes it fail, and the probable reason is attached to that error —
|
|
429
|
+
* otherwise the user has to correlate a log warning with an HTTP failure.
|
|
430
|
+
*/
|
|
431
|
+
function hintUnlistedModel(cause, modelId, known) {
|
|
432
|
+
if (known !== false || !(cause instanceof Error))
|
|
433
|
+
return cause;
|
|
434
|
+
cause.message +=
|
|
435
|
+
` — model "${modelId}" is not listed in your Cognition catalog, which is the most likely reason.` +
|
|
436
|
+
` If it should be available, run /connect to refresh the catalog, or pick a model from /models.`;
|
|
437
|
+
return cause;
|
|
438
|
+
}
|
|
309
439
|
function createLanguageModel(options) {
|
|
310
|
-
const run = (callOptions) => {
|
|
440
|
+
const run = async (callOptions, signal) => {
|
|
441
|
+
const catalog = await options.loadCatalog();
|
|
442
|
+
checkModelUsable(options.modelId, catalog);
|
|
311
443
|
const effortOverride = callOptions.providerOptions?.devin?.reasoningEffort;
|
|
312
|
-
const resolvedModelId = resolveEffortUid(options.modelId,
|
|
313
|
-
const pricing = options.catalogMap?.get(resolvedModelId)?.pricing;
|
|
444
|
+
const resolvedModelId = resolveEffortUid(options.modelId, catalog?.enabled, effortOverride);
|
|
314
445
|
return {
|
|
315
446
|
resolvedModelId,
|
|
316
|
-
|
|
447
|
+
knownToCatalog: isKnownToCatalog(options.modelId, catalog),
|
|
317
448
|
events: streamChatEvents({
|
|
318
449
|
apiKey: options.apiKey,
|
|
319
450
|
apiServerUrl: options.apiServerUrl,
|
|
@@ -326,7 +457,7 @@ function createLanguageModel(options) {
|
|
|
326
457
|
topP: callOptions.topP,
|
|
327
458
|
topK: callOptions.topK,
|
|
328
459
|
},
|
|
329
|
-
signal
|
|
460
|
+
signal,
|
|
330
461
|
}),
|
|
331
462
|
};
|
|
332
463
|
};
|
|
@@ -336,8 +467,10 @@ function createLanguageModel(options) {
|
|
|
336
467
|
modelId: options.modelId,
|
|
337
468
|
supportedUrls: {},
|
|
338
469
|
async doGenerate(callOptions) {
|
|
339
|
-
const {
|
|
340
|
-
const collected = await collectEvents(events)
|
|
470
|
+
const { events, knownToCatalog } = await run(callOptions, callOptions.abortSignal);
|
|
471
|
+
const collected = await collectEvents(events).catch((cause) => {
|
|
472
|
+
throw hintUnlistedModel(cause, options.modelId, knownToCatalog);
|
|
473
|
+
});
|
|
341
474
|
const content = [];
|
|
342
475
|
if (collected.reasoning)
|
|
343
476
|
content.push({ type: "reasoning", text: collected.reasoning });
|
|
@@ -346,11 +479,6 @@ function createLanguageModel(options) {
|
|
|
346
479
|
for (const call of collected.toolCalls) {
|
|
347
480
|
content.push({ type: "tool-call", toolCallId: call.id, toolName: call.name, input: call.args });
|
|
348
481
|
}
|
|
349
|
-
const cost = calculateCost({
|
|
350
|
-
inputTokens: collected.usage?.inputTokens?.total,
|
|
351
|
-
cachedInputTokens: collected.usage?.inputTokens?.cacheRead,
|
|
352
|
-
outputTokens: collected.usage?.outputTokens?.total,
|
|
353
|
-
}, pricing);
|
|
354
482
|
return {
|
|
355
483
|
content,
|
|
356
484
|
finishReason: {
|
|
@@ -359,25 +487,44 @@ function createLanguageModel(options) {
|
|
|
359
487
|
},
|
|
360
488
|
usage: collected.usage ?? DEFAULT_USAGE,
|
|
361
489
|
warnings: [],
|
|
362
|
-
...(cost !== undefined ? { providerMetadata: { devin: { cost } } } : {}),
|
|
363
490
|
};
|
|
364
491
|
},
|
|
365
492
|
async doStream(callOptions) {
|
|
366
|
-
|
|
493
|
+
// Owned so that cancelling the stream can tear the request down; the
|
|
494
|
+
// caller's signal is honoured alongside it.
|
|
495
|
+
const abort = new AbortController();
|
|
496
|
+
const signal = callOptions.abortSignal
|
|
497
|
+
? AbortSignal.any([callOptions.abortSignal, abort.signal])
|
|
498
|
+
: abort.signal;
|
|
499
|
+
const { events, knownToCatalog } = await run(callOptions, signal);
|
|
367
500
|
const generateId = () => crypto.randomUUID();
|
|
368
|
-
const generator = convertStreamEvents(events, generateId
|
|
501
|
+
const generator = convertStreamEvents(events, generateId);
|
|
502
|
+
const modelId = options.modelId;
|
|
503
|
+
let cancelled = false;
|
|
369
504
|
const stream = new ReadableStream({
|
|
370
505
|
async start(controller) {
|
|
371
506
|
try {
|
|
372
507
|
for await (const chunk of generator) {
|
|
508
|
+
if (cancelled)
|
|
509
|
+
break;
|
|
373
510
|
controller.enqueue(chunk);
|
|
374
511
|
}
|
|
375
|
-
|
|
512
|
+
if (!cancelled)
|
|
513
|
+
controller.close();
|
|
376
514
|
}
|
|
377
515
|
catch (cause) {
|
|
378
|
-
|
|
516
|
+
if (!cancelled)
|
|
517
|
+
controller.error(hintUnlistedModel(cause, modelId, knownToCatalog));
|
|
379
518
|
}
|
|
380
519
|
},
|
|
520
|
+
async cancel() {
|
|
521
|
+
cancelled = true;
|
|
522
|
+
// Aborting is what actually releases the connection. `generator.return()`
|
|
523
|
+
// alone is not enough: it is queued behind the in-flight read, so the
|
|
524
|
+
// request would stay open until the idle timeout fired.
|
|
525
|
+
abort.abort(new Error("Stream cancelled by the consumer"));
|
|
526
|
+
await generator.return(undefined).catch(() => { });
|
|
527
|
+
},
|
|
381
528
|
});
|
|
382
529
|
return { stream };
|
|
383
530
|
},
|
|
@@ -385,28 +532,43 @@ function createLanguageModel(options) {
|
|
|
385
532
|
}
|
|
386
533
|
export function createDevin(options) {
|
|
387
534
|
const apiKey = options.apiKey ?? "";
|
|
388
|
-
let
|
|
389
|
-
let
|
|
535
|
+
let catalog;
|
|
536
|
+
let pending;
|
|
537
|
+
/**
|
|
538
|
+
* Per-account catalog, memoized for the life of the provider.
|
|
539
|
+
*
|
|
540
|
+
* `getCachedCatalog` already dedupes concurrent fetches and caches the result
|
|
541
|
+
* for 10 minutes, so this is normally a cache hit — the plugin's `publish()`
|
|
542
|
+
* warms the same module-level cache before any chat happens. On failure it
|
|
543
|
+
* resolves `undefined`, and the request proceeds uncatalogued rather than being
|
|
544
|
+
* rejected on a guess.
|
|
545
|
+
*/
|
|
546
|
+
const loadCatalog = () => {
|
|
547
|
+
if (!apiKey)
|
|
548
|
+
return Promise.resolve(undefined);
|
|
549
|
+
if (catalog)
|
|
550
|
+
return Promise.resolve(catalog);
|
|
551
|
+
pending ??= getCachedCatalog(apiKey, options.baseURL)
|
|
552
|
+
.then((result) => {
|
|
553
|
+
if (result) {
|
|
554
|
+
const entries = Array.from(result.byUid.values());
|
|
555
|
+
catalog = { entries, enabled: entries.filter((entry) => !entry.disabled) };
|
|
556
|
+
}
|
|
557
|
+
return catalog;
|
|
558
|
+
})
|
|
559
|
+
.finally(() => {
|
|
560
|
+
pending = undefined;
|
|
561
|
+
});
|
|
562
|
+
return pending;
|
|
563
|
+
};
|
|
390
564
|
return {
|
|
391
565
|
languageModel(modelId) {
|
|
392
566
|
return createLanguageModel({
|
|
393
567
|
modelId,
|
|
394
568
|
apiKey,
|
|
395
569
|
apiServerUrl: options.baseURL,
|
|
396
|
-
|
|
397
|
-
catalogMap,
|
|
570
|
+
loadCatalog,
|
|
398
571
|
});
|
|
399
572
|
},
|
|
400
|
-
async models() {
|
|
401
|
-
if (!apiKey)
|
|
402
|
-
return [];
|
|
403
|
-
const catalog = await getCachedCatalog(apiKey, options.baseURL);
|
|
404
|
-
if (!catalog)
|
|
405
|
-
return [];
|
|
406
|
-
const models = Array.from(catalog.byUid.values()).filter((entry) => !entry.disabled);
|
|
407
|
-
cachedModels = models;
|
|
408
|
-
catalogMap = new Map(models.map((entry) => [entry.modelUid, entry]));
|
|
409
|
-
return models;
|
|
410
|
-
},
|
|
411
573
|
};
|
|
412
574
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package.json",
|
|
3
3
|
"name": "opencode-devin",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.2.1",
|
|
5
5
|
"description": "OpenCode v2 plugin: connect your Devin subscription to opencode with native /connect login and devin/* models",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "./dist/index.js",
|
|
@@ -19,9 +19,11 @@
|
|
|
19
19
|
"dist"
|
|
20
20
|
],
|
|
21
21
|
"scripts": {
|
|
22
|
-
"build": "node
|
|
23
|
-
"
|
|
24
|
-
"
|
|
22
|
+
"build": "node scripts/build.mjs",
|
|
23
|
+
"build:clean": "node -e \"fs.rmSync('dist',{recursive:true,force:true})\" && tsc",
|
|
24
|
+
"typecheck": "tsc --noEmit && tsc --noEmit -p tsconfig.test.json",
|
|
25
|
+
"test": "bun test",
|
|
26
|
+
"prepublishOnly": "npm run build:clean"
|
|
25
27
|
},
|
|
26
28
|
"keywords": [
|
|
27
29
|
"opencode",
|
|
@@ -47,7 +49,8 @@
|
|
|
47
49
|
"@opencode/plugin": "2.0.16"
|
|
48
50
|
},
|
|
49
51
|
"devDependencies": {
|
|
50
|
-
"@types/bun": "1.4.
|
|
51
|
-
"
|
|
52
|
+
"@types/bun": "1.4.4",
|
|
53
|
+
"protobufjs": "^8.8.1",
|
|
54
|
+
"typescript": "7.0.2"
|
|
52
55
|
}
|
|
53
56
|
}
|