opencode-devin 0.1.0
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/LICENSE +21 -0
- package/README.md +107 -0
- package/dist/auth/exchange.d.ts +15 -0
- package/dist/auth/exchange.js +43 -0
- package/dist/auth/loopback.d.ts +15 -0
- package/dist/auth/loopback.js +57 -0
- package/dist/auth/pkce.d.ts +11 -0
- package/dist/auth/pkce.js +27 -0
- package/dist/auth/url.d.ts +12 -0
- package/dist/auth/url.js +17 -0
- package/dist/catalog.d.ts +11 -0
- package/dist/catalog.js +47 -0
- package/dist/constants.d.ts +19 -0
- package/dist/constants.js +19 -0
- package/dist/credentials.d.ts +23 -0
- package/dist/credentials.js +53 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +121 -0
- package/dist/protocol/auth.d.ts +20 -0
- package/dist/protocol/auth.js +97 -0
- package/dist/protocol/catalog.d.ts +49 -0
- package/dist/protocol/catalog.js +182 -0
- package/dist/protocol/chat.d.ts +92 -0
- package/dist/protocol/chat.js +465 -0
- package/dist/protocol/metadata.d.ts +17 -0
- package/dist/protocol/metadata.js +42 -0
- package/dist/protocol/model.d.ts +32 -0
- package/dist/protocol/model.js +412 -0
- package/dist/protocol/wire.d.ts +30 -0
- package/dist/protocol/wire.js +114 -0
- package/dist/provider.d.ts +18 -0
- package/dist/provider.js +20 -0
- package/package.json +53 -0
|
@@ -0,0 +1,412 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AI SDK `LanguageModelV3` facade over the Cascade chat stream.
|
|
3
|
+
*
|
|
4
|
+
* Models are discovered dynamically from the per-account catalog
|
|
5
|
+
* (GetCascadeModelConfigs). Auth: the long-lived session token
|
|
6
|
+
* (`devin-session-token$<JWT>`) as `apiKey`; a short-lived `user_jwt` is
|
|
7
|
+
* minted automatically per session.
|
|
8
|
+
*
|
|
9
|
+
* Originally ported from ai-sdk-devin / pi-devin-auth (MIT).
|
|
10
|
+
*/
|
|
11
|
+
import { streamChatEvents } from "./chat.js";
|
|
12
|
+
import { getCachedCatalog } from "./catalog.js";
|
|
13
|
+
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
|
+
/**
|
|
17
|
+
* Resolve the actual model UID to send upstream, applying an optional
|
|
18
|
+
* reasoning-effort override from providerOptions: swap an existing effort
|
|
19
|
+
* suffix, or append one when the catalog lists that variant.
|
|
20
|
+
*/
|
|
21
|
+
function resolveEffortUid(modelId, catalog, requestedEffort) {
|
|
22
|
+
const effort = typeof requestedEffort === "string" ? requestedEffort.toLowerCase() : "";
|
|
23
|
+
if (!EFFORT_LEVELS.includes(effort))
|
|
24
|
+
return modelId;
|
|
25
|
+
const effortMatch = modelId.match(/-(none|low|medium|high|xhigh|max)$/);
|
|
26
|
+
if (effortMatch?.[1])
|
|
27
|
+
return modelId.slice(0, -effortMatch[1].length - 1) + "-" + effort;
|
|
28
|
+
if (catalog?.some((entry) => entry.baseModelUid === modelId && entry.effortLevel === effort)) {
|
|
29
|
+
return modelId + "-" + effort;
|
|
30
|
+
}
|
|
31
|
+
return modelId;
|
|
32
|
+
}
|
|
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
|
+
// --- Prompt conversion: LanguageModelV3Prompt → ChatHistoryItem[] ---
|
|
44
|
+
function convertPrompt(prompt) {
|
|
45
|
+
const items = [];
|
|
46
|
+
for (const message of prompt) {
|
|
47
|
+
if (message.role === "system") {
|
|
48
|
+
items.push({ role: "system", content: message.content });
|
|
49
|
+
}
|
|
50
|
+
else if (message.role === "user") {
|
|
51
|
+
const parts = [];
|
|
52
|
+
for (const part of message.content) {
|
|
53
|
+
if (part.type === "text") {
|
|
54
|
+
parts.push({ type: "text", text: part.text });
|
|
55
|
+
}
|
|
56
|
+
else if (part.type === "file" && part.mediaType.startsWith("image/") && typeof part.data !== "object") {
|
|
57
|
+
const data = typeof part.data === "string" ? part.data : Buffer.from(part.data).toString("base64");
|
|
58
|
+
parts.push({ type: "image", mimeType: part.mediaType, base64Data: data });
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
items.push({ role: "user", content: parts });
|
|
62
|
+
}
|
|
63
|
+
else if (message.role === "assistant") {
|
|
64
|
+
const parts = [];
|
|
65
|
+
const toolCalls = [];
|
|
66
|
+
for (const part of message.content) {
|
|
67
|
+
if (part.type === "text") {
|
|
68
|
+
parts.push({ type: "text", text: part.text });
|
|
69
|
+
}
|
|
70
|
+
else if (part.type === "tool-call") {
|
|
71
|
+
toolCalls?.push({
|
|
72
|
+
id: part.toolCallId,
|
|
73
|
+
name: part.toolName,
|
|
74
|
+
arguments: typeof part.input === "string" ? part.input : JSON.stringify(part.input),
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
items.push({ role: "assistant", content: parts, tool_calls: toolCalls && toolCalls.length > 0 ? toolCalls : undefined });
|
|
79
|
+
}
|
|
80
|
+
else if (message.role === "tool") {
|
|
81
|
+
for (const part of message.content) {
|
|
82
|
+
if (part.type === "tool-result") {
|
|
83
|
+
const output = typeof part.output === "string" ? part.output : JSON.stringify(part.output);
|
|
84
|
+
items.push({ role: "tool", content: output, tool_call_id: part.toolCallId });
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return items;
|
|
90
|
+
}
|
|
91
|
+
function convertTools(tools) {
|
|
92
|
+
if (!tools || tools.length === 0)
|
|
93
|
+
return undefined;
|
|
94
|
+
const out = [];
|
|
95
|
+
for (const tool of tools) {
|
|
96
|
+
if (tool.type === "function") {
|
|
97
|
+
out.push({ name: tool.name, description: tool.description ?? "", parameters: tool.inputSchema });
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return out.length > 0 ? out : undefined;
|
|
101
|
+
}
|
|
102
|
+
// --- Stream event conversion ---
|
|
103
|
+
// Text/reasoning coalescing thresholds — reduces stream events from
|
|
104
|
+
// hundreds of 5-byte micro-deltas to a few dozen batched chunks.
|
|
105
|
+
const COALESCE_INTERVAL_MS = 32;
|
|
106
|
+
const COALESCE_MAX_BYTES = 128;
|
|
107
|
+
const FINISH_REASON_MAP = {
|
|
108
|
+
stop: "stop",
|
|
109
|
+
length: "length",
|
|
110
|
+
tool_calls: "tool-calls",
|
|
111
|
+
content_filter: "content-filter",
|
|
112
|
+
};
|
|
113
|
+
const DEFAULT_USAGE = {
|
|
114
|
+
inputTokens: { total: 0, noCache: undefined, cacheRead: undefined, cacheWrite: undefined },
|
|
115
|
+
outputTokens: { total: 0, text: undefined, reasoning: undefined },
|
|
116
|
+
};
|
|
117
|
+
async function* convertStreamEvents(events, generateId, pricing) {
|
|
118
|
+
let textId = "";
|
|
119
|
+
let reasoningId = "";
|
|
120
|
+
let textOpen = false;
|
|
121
|
+
let reasoningOpen = false;
|
|
122
|
+
let currentToolId = "";
|
|
123
|
+
let currentToolName = "";
|
|
124
|
+
let toolInputOpen = false;
|
|
125
|
+
let pendingToolArgs = "";
|
|
126
|
+
let finishReason = "stop";
|
|
127
|
+
let usage;
|
|
128
|
+
let textBuf = "";
|
|
129
|
+
let textLastFlush = 0;
|
|
130
|
+
let reasoningBuf = "";
|
|
131
|
+
let reasoningLastFlush = 0;
|
|
132
|
+
yield { type: "stream-start", warnings: [] };
|
|
133
|
+
for await (const event of events) {
|
|
134
|
+
switch (event.kind) {
|
|
135
|
+
case "text": {
|
|
136
|
+
if (!textOpen) {
|
|
137
|
+
textId = generateId();
|
|
138
|
+
textOpen = true;
|
|
139
|
+
textLastFlush = Date.now();
|
|
140
|
+
yield { type: "text-start", id: textId };
|
|
141
|
+
}
|
|
142
|
+
textBuf += event.text;
|
|
143
|
+
if (textBuf.length >= COALESCE_MAX_BYTES || Date.now() - textLastFlush >= COALESCE_INTERVAL_MS) {
|
|
144
|
+
yield { type: "text-delta", id: textId, delta: textBuf };
|
|
145
|
+
textBuf = "";
|
|
146
|
+
textLastFlush = Date.now();
|
|
147
|
+
}
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
case "reasoning": {
|
|
151
|
+
if (!reasoningOpen) {
|
|
152
|
+
reasoningId = generateId();
|
|
153
|
+
reasoningOpen = true;
|
|
154
|
+
reasoningLastFlush = Date.now();
|
|
155
|
+
yield { type: "reasoning-start", id: reasoningId };
|
|
156
|
+
}
|
|
157
|
+
reasoningBuf += event.text;
|
|
158
|
+
if (reasoningBuf.length >= COALESCE_MAX_BYTES || Date.now() - reasoningLastFlush >= COALESCE_INTERVAL_MS) {
|
|
159
|
+
yield { type: "reasoning-delta", id: reasoningId, delta: reasoningBuf };
|
|
160
|
+
reasoningBuf = "";
|
|
161
|
+
reasoningLastFlush = Date.now();
|
|
162
|
+
}
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
165
|
+
case "tool_call_start": {
|
|
166
|
+
if (textBuf) {
|
|
167
|
+
yield { type: "text-delta", id: textId, delta: textBuf };
|
|
168
|
+
textBuf = "";
|
|
169
|
+
}
|
|
170
|
+
if (reasoningBuf) {
|
|
171
|
+
yield { type: "reasoning-delta", id: reasoningId, delta: reasoningBuf };
|
|
172
|
+
reasoningBuf = "";
|
|
173
|
+
}
|
|
174
|
+
if (textOpen) {
|
|
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 = "";
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
case "tool_call_args": {
|
|
192
|
+
pendingToolArgs += event.argsDelta;
|
|
193
|
+
break;
|
|
194
|
+
}
|
|
195
|
+
case "finish": {
|
|
196
|
+
if (textBuf) {
|
|
197
|
+
yield { type: "text-delta", id: textId, delta: textBuf };
|
|
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
|
+
}
|
|
216
|
+
finishReason = event.reason;
|
|
217
|
+
break;
|
|
218
|
+
}
|
|
219
|
+
case "usage": {
|
|
220
|
+
usage = {
|
|
221
|
+
inputTokens: {
|
|
222
|
+
total: event.promptTokens,
|
|
223
|
+
noCache: event.promptTokens !== undefined && event.cachedInputTokens !== undefined
|
|
224
|
+
? event.promptTokens - event.cachedInputTokens
|
|
225
|
+
: undefined,
|
|
226
|
+
cacheRead: event.cachedInputTokens,
|
|
227
|
+
cacheWrite: event.cacheCreationInputTokens,
|
|
228
|
+
},
|
|
229
|
+
outputTokens: {
|
|
230
|
+
total: event.completionTokens,
|
|
231
|
+
text: event.completionTokens !== undefined && event.reasoningTokens !== undefined
|
|
232
|
+
? event.completionTokens - event.reasoningTokens
|
|
233
|
+
: undefined,
|
|
234
|
+
reasoning: event.reasoningTokens,
|
|
235
|
+
},
|
|
236
|
+
};
|
|
237
|
+
break;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
// Final flush
|
|
242
|
+
if (textBuf)
|
|
243
|
+
yield { type: "text-delta", id: textId, delta: textBuf };
|
|
244
|
+
if (reasoningBuf)
|
|
245
|
+
yield { type: "reasoning-delta", id: reasoningId, delta: reasoningBuf };
|
|
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 };
|
|
252
|
+
}
|
|
253
|
+
const inputTotal = usage?.inputTokens?.total;
|
|
254
|
+
const cachedRead = usage?.inputTokens?.cacheRead;
|
|
255
|
+
const outputTotal = usage?.outputTokens?.total;
|
|
256
|
+
const cost = calculateCost({ inputTokens: inputTotal, cachedInputTokens: cachedRead, outputTokens: outputTotal }, pricing);
|
|
257
|
+
const rawReason = FINISH_REASON_MAP[finishReason] ?? "stop";
|
|
258
|
+
yield {
|
|
259
|
+
type: "finish",
|
|
260
|
+
finishReason: { unified: rawReason, raw: finishReason },
|
|
261
|
+
usage: usage ?? DEFAULT_USAGE,
|
|
262
|
+
...(cost !== undefined ? { providerMetadata: { devin: { cost } } } : {}),
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
async function collectEvents(events) {
|
|
266
|
+
const result = { text: "", reasoning: "", toolCalls: [], finishReason: "stop" };
|
|
267
|
+
let currentTool = null;
|
|
268
|
+
for await (const event of events) {
|
|
269
|
+
switch (event.kind) {
|
|
270
|
+
case "text":
|
|
271
|
+
result.text += event.text;
|
|
272
|
+
break;
|
|
273
|
+
case "reasoning":
|
|
274
|
+
result.reasoning += event.text;
|
|
275
|
+
break;
|
|
276
|
+
case "tool_call_start":
|
|
277
|
+
if (currentTool)
|
|
278
|
+
result.toolCalls.push(currentTool);
|
|
279
|
+
currentTool = { id: event.id, name: event.name, args: "" };
|
|
280
|
+
break;
|
|
281
|
+
case "tool_call_args":
|
|
282
|
+
if (currentTool)
|
|
283
|
+
currentTool.args += event.argsDelta;
|
|
284
|
+
break;
|
|
285
|
+
case "finish":
|
|
286
|
+
if (currentTool) {
|
|
287
|
+
result.toolCalls.push(currentTool);
|
|
288
|
+
currentTool = null;
|
|
289
|
+
}
|
|
290
|
+
result.finishReason = event.reason;
|
|
291
|
+
break;
|
|
292
|
+
case "usage":
|
|
293
|
+
result.usage = {
|
|
294
|
+
inputTokens: {
|
|
295
|
+
total: event.promptTokens,
|
|
296
|
+
noCache: undefined,
|
|
297
|
+
cacheRead: event.cachedInputTokens,
|
|
298
|
+
cacheWrite: event.cacheCreationInputTokens,
|
|
299
|
+
},
|
|
300
|
+
outputTokens: { total: event.completionTokens, text: undefined, reasoning: event.reasoningTokens },
|
|
301
|
+
};
|
|
302
|
+
break;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
if (currentTool)
|
|
306
|
+
result.toolCalls.push(currentTool);
|
|
307
|
+
return result;
|
|
308
|
+
}
|
|
309
|
+
function createLanguageModel(options) {
|
|
310
|
+
const run = (callOptions) => {
|
|
311
|
+
const effortOverride = callOptions.providerOptions?.devin?.reasoningEffort;
|
|
312
|
+
const resolvedModelId = resolveEffortUid(options.modelId, options.catalog, effortOverride);
|
|
313
|
+
const pricing = options.catalogMap?.get(resolvedModelId)?.pricing;
|
|
314
|
+
return {
|
|
315
|
+
resolvedModelId,
|
|
316
|
+
pricing,
|
|
317
|
+
events: streamChatEvents({
|
|
318
|
+
apiKey: options.apiKey,
|
|
319
|
+
apiServerUrl: options.apiServerUrl,
|
|
320
|
+
modelUid: resolvedModelId,
|
|
321
|
+
messages: convertPrompt(callOptions.prompt),
|
|
322
|
+
tools: convertTools(callOptions.tools),
|
|
323
|
+
completionOpts: {
|
|
324
|
+
maxOutputTokens: callOptions.maxOutputTokens,
|
|
325
|
+
temperature: callOptions.temperature,
|
|
326
|
+
topP: callOptions.topP,
|
|
327
|
+
topK: callOptions.topK,
|
|
328
|
+
},
|
|
329
|
+
signal: callOptions.abortSignal,
|
|
330
|
+
}),
|
|
331
|
+
};
|
|
332
|
+
};
|
|
333
|
+
return {
|
|
334
|
+
specificationVersion: "v3",
|
|
335
|
+
provider: "devin",
|
|
336
|
+
modelId: options.modelId,
|
|
337
|
+
supportedUrls: {},
|
|
338
|
+
async doGenerate(callOptions) {
|
|
339
|
+
const { pricing, events } = run(callOptions);
|
|
340
|
+
const collected = await collectEvents(events);
|
|
341
|
+
const content = [];
|
|
342
|
+
if (collected.reasoning)
|
|
343
|
+
content.push({ type: "reasoning", text: collected.reasoning });
|
|
344
|
+
if (collected.text)
|
|
345
|
+
content.push({ type: "text", text: collected.text });
|
|
346
|
+
for (const call of collected.toolCalls) {
|
|
347
|
+
content.push({ type: "tool-call", toolCallId: call.id, toolName: call.name, input: call.args });
|
|
348
|
+
}
|
|
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
|
+
return {
|
|
355
|
+
content,
|
|
356
|
+
finishReason: {
|
|
357
|
+
unified: FINISH_REASON_MAP[collected.finishReason] ?? "stop",
|
|
358
|
+
raw: collected.finishReason,
|
|
359
|
+
},
|
|
360
|
+
usage: collected.usage ?? DEFAULT_USAGE,
|
|
361
|
+
warnings: [],
|
|
362
|
+
...(cost !== undefined ? { providerMetadata: { devin: { cost } } } : {}),
|
|
363
|
+
};
|
|
364
|
+
},
|
|
365
|
+
async doStream(callOptions) {
|
|
366
|
+
const { pricing, events } = run(callOptions);
|
|
367
|
+
const generateId = () => crypto.randomUUID();
|
|
368
|
+
const generator = convertStreamEvents(events, generateId, pricing);
|
|
369
|
+
const stream = new ReadableStream({
|
|
370
|
+
async start(controller) {
|
|
371
|
+
try {
|
|
372
|
+
for await (const chunk of generator) {
|
|
373
|
+
controller.enqueue(chunk);
|
|
374
|
+
}
|
|
375
|
+
controller.close();
|
|
376
|
+
}
|
|
377
|
+
catch (cause) {
|
|
378
|
+
controller.error(cause);
|
|
379
|
+
}
|
|
380
|
+
},
|
|
381
|
+
});
|
|
382
|
+
return { stream };
|
|
383
|
+
},
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
export function createDevin(options) {
|
|
387
|
+
const apiKey = options.apiKey ?? "";
|
|
388
|
+
let cachedModels;
|
|
389
|
+
let catalogMap;
|
|
390
|
+
return {
|
|
391
|
+
languageModel(modelId) {
|
|
392
|
+
return createLanguageModel({
|
|
393
|
+
modelId,
|
|
394
|
+
apiKey,
|
|
395
|
+
apiServerUrl: options.baseURL,
|
|
396
|
+
catalog: cachedModels,
|
|
397
|
+
catalogMap,
|
|
398
|
+
});
|
|
399
|
+
},
|
|
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
|
+
};
|
|
412
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Protobuf wire-format encoding and Connect-RPC streaming envelope.
|
|
3
|
+
*
|
|
4
|
+
* Connect-RPC streaming frame layout (HTTPS POST body):
|
|
5
|
+
*
|
|
6
|
+
* flags 1 byte | length 4 bytes BE | payload
|
|
7
|
+
*
|
|
8
|
+
* - flags bit 0x01: payload is gzip-compressed
|
|
9
|
+
* - flags bit 0x02: end-of-stream trailer frame (JSON `{error}` or empty)
|
|
10
|
+
*
|
|
11
|
+
* Protocol reverse-engineered from the Windsurf language_server and the Devin
|
|
12
|
+
* CLI; originally ported from pi-devin-auth (MIT, Copyright (c) 2026 nmzpy).
|
|
13
|
+
*/
|
|
14
|
+
export type WireType = 0 | 1 | 2 | 5;
|
|
15
|
+
export interface ProtoField {
|
|
16
|
+
readonly num: number;
|
|
17
|
+
readonly wire: WireType;
|
|
18
|
+
readonly value: bigint | Buffer;
|
|
19
|
+
}
|
|
20
|
+
export declare function encodeVarint(value: bigint | number): Buffer;
|
|
21
|
+
export declare function encodeTag(fieldNum: number, wire: WireType): Buffer;
|
|
22
|
+
export declare function encodeString(fieldNum: number, value: string): Buffer;
|
|
23
|
+
export declare function encodeMessage(fieldNum: number, body: Buffer): Buffer;
|
|
24
|
+
export declare function encodeVarintField(fieldNum: number, value: bigint | number): Buffer;
|
|
25
|
+
export declare function encodeTimestampBody(): Buffer;
|
|
26
|
+
export declare function decodeVarint(buf: Buffer, offset: number): [bigint, number];
|
|
27
|
+
/** Iterate the protobuf fields of a message in wire order. */
|
|
28
|
+
export declare function iterFields(buf: Buffer): Generator<ProtoField>;
|
|
29
|
+
/** Wrap a protobuf message into a Connect-RPC streaming request frame. */
|
|
30
|
+
export declare function frameConnectStream(body: Buffer, compress?: boolean): Buffer;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Protobuf wire-format encoding and Connect-RPC streaming envelope.
|
|
3
|
+
*
|
|
4
|
+
* Connect-RPC streaming frame layout (HTTPS POST body):
|
|
5
|
+
*
|
|
6
|
+
* flags 1 byte | length 4 bytes BE | payload
|
|
7
|
+
*
|
|
8
|
+
* - flags bit 0x01: payload is gzip-compressed
|
|
9
|
+
* - flags bit 0x02: end-of-stream trailer frame (JSON `{error}` or empty)
|
|
10
|
+
*
|
|
11
|
+
* Protocol reverse-engineered from the Windsurf language_server and the Devin
|
|
12
|
+
* CLI; originally ported from pi-devin-auth (MIT, Copyright (c) 2026 nmzpy).
|
|
13
|
+
*/
|
|
14
|
+
import * as zlib from "node:zlib";
|
|
15
|
+
export function encodeVarint(value) {
|
|
16
|
+
let v = BigInt(value);
|
|
17
|
+
if (v < 0n)
|
|
18
|
+
throw new RangeError(`encodeVarint: negative input not supported (got ${value})`);
|
|
19
|
+
const bytes = [];
|
|
20
|
+
while (v > 0x7fn) {
|
|
21
|
+
bytes.push(Number(v & 0x7fn) | 0x80);
|
|
22
|
+
v >>= 7n;
|
|
23
|
+
}
|
|
24
|
+
bytes.push(Number(v));
|
|
25
|
+
return Buffer.from(bytes);
|
|
26
|
+
}
|
|
27
|
+
export function encodeTag(fieldNum, wire) {
|
|
28
|
+
return encodeVarint((fieldNum << 3) | wire);
|
|
29
|
+
}
|
|
30
|
+
export function encodeString(fieldNum, value) {
|
|
31
|
+
const buf = Buffer.from(value, "utf8");
|
|
32
|
+
return Buffer.concat([encodeTag(fieldNum, 2), encodeVarint(buf.length), buf]);
|
|
33
|
+
}
|
|
34
|
+
export function encodeMessage(fieldNum, body) {
|
|
35
|
+
return Buffer.concat([encodeTag(fieldNum, 2), encodeVarint(body.length), body]);
|
|
36
|
+
}
|
|
37
|
+
export function encodeVarintField(fieldNum, value) {
|
|
38
|
+
return Buffer.concat([encodeTag(fieldNum, 0), encodeVarint(value)]);
|
|
39
|
+
}
|
|
40
|
+
export function encodeTimestampBody() {
|
|
41
|
+
const now = Date.now();
|
|
42
|
+
const seconds = Math.floor(now / 1000);
|
|
43
|
+
const nanos = (now % 1000) * 1_000_000;
|
|
44
|
+
return Buffer.concat([
|
|
45
|
+
encodeVarintField(1, seconds),
|
|
46
|
+
nanos > 0 ? encodeVarintField(2, nanos) : Buffer.alloc(0),
|
|
47
|
+
]);
|
|
48
|
+
}
|
|
49
|
+
export function decodeVarint(buf, offset) {
|
|
50
|
+
let result = 0n;
|
|
51
|
+
let shift = 0n;
|
|
52
|
+
let i = offset;
|
|
53
|
+
while (i < buf.length) {
|
|
54
|
+
const byte = buf[i++];
|
|
55
|
+
result |= BigInt(byte & 0x7f) << shift;
|
|
56
|
+
if (!(byte & 0x80))
|
|
57
|
+
return [result, i];
|
|
58
|
+
shift += 7n;
|
|
59
|
+
}
|
|
60
|
+
throw new Error("truncated varint");
|
|
61
|
+
}
|
|
62
|
+
/** Iterate the protobuf fields of a message in wire order. */
|
|
63
|
+
export function* iterFields(buf) {
|
|
64
|
+
let i = 0;
|
|
65
|
+
while (i < buf.length) {
|
|
66
|
+
const [tagBig, afterTag] = decodeVarint(buf, i);
|
|
67
|
+
i = afterTag;
|
|
68
|
+
const tag = Number(tagBig);
|
|
69
|
+
const num = tag >> 3;
|
|
70
|
+
const wire = (tag & 0x7);
|
|
71
|
+
if (wire === 0) {
|
|
72
|
+
const [value, afterValue] = decodeVarint(buf, i);
|
|
73
|
+
i = afterValue;
|
|
74
|
+
yield { num, wire, value };
|
|
75
|
+
}
|
|
76
|
+
else if (wire === 1) {
|
|
77
|
+
if (i + 8 > buf.length)
|
|
78
|
+
return;
|
|
79
|
+
yield { num, wire, value: buf.slice(i, i + 8) };
|
|
80
|
+
i += 8;
|
|
81
|
+
}
|
|
82
|
+
else if (wire === 2) {
|
|
83
|
+
const [lengthBig, afterLength] = decodeVarint(buf, i);
|
|
84
|
+
i = afterLength;
|
|
85
|
+
const length = Number(lengthBig);
|
|
86
|
+
if (length < 0 || i + length > buf.length)
|
|
87
|
+
return;
|
|
88
|
+
yield { num, wire, value: buf.slice(i, i + length) };
|
|
89
|
+
i += length;
|
|
90
|
+
}
|
|
91
|
+
else if (wire === 5) {
|
|
92
|
+
if (i + 4 > buf.length)
|
|
93
|
+
return;
|
|
94
|
+
yield { num, wire, value: buf.slice(i, i + 4) };
|
|
95
|
+
i += 4;
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/** Wrap a protobuf message into a Connect-RPC streaming request frame. */
|
|
103
|
+
export function frameConnectStream(body, compress = true) {
|
|
104
|
+
let payload = body;
|
|
105
|
+
let flags = 0;
|
|
106
|
+
if (compress) {
|
|
107
|
+
payload = zlib.gzipSync(body);
|
|
108
|
+
flags |= 0x01;
|
|
109
|
+
}
|
|
110
|
+
const header = Buffer.alloc(5);
|
|
111
|
+
header[0] = flags;
|
|
112
|
+
header.writeUInt32BE(payload.length, 1);
|
|
113
|
+
return Buffer.concat([header, payload]);
|
|
114
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provider package entrypoint for OpenCode v2.
|
|
3
|
+
*
|
|
4
|
+
* With the `aisdk:` package prefix, opencode's dynamic provider loader imports
|
|
5
|
+
* this module, calls the first `create*` export with the provider settings,
|
|
6
|
+
* and uses `sdk.languageModel(modelID)` as the AI SDK `LanguageModelV3`.
|
|
7
|
+
*
|
|
8
|
+
* The Cascade protocol implementation lives in `src/protocol/` — vendored,
|
|
9
|
+
* audited, and owned in this repository (see `src/protocol/ATTRIBUTION.md`).
|
|
10
|
+
*/
|
|
11
|
+
export interface DevinProviderSettings {
|
|
12
|
+
readonly apiKey?: string;
|
|
13
|
+
readonly baseURL?: string;
|
|
14
|
+
readonly name?: string;
|
|
15
|
+
}
|
|
16
|
+
export declare function createDevinProvider(options: DevinProviderSettings): {
|
|
17
|
+
languageModel: (modelID: string) => import("@ai-sdk/provider").LanguageModelV3;
|
|
18
|
+
};
|
package/dist/provider.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provider package entrypoint for OpenCode v2.
|
|
3
|
+
*
|
|
4
|
+
* With the `aisdk:` package prefix, opencode's dynamic provider loader imports
|
|
5
|
+
* this module, calls the first `create*` export with the provider settings,
|
|
6
|
+
* and uses `sdk.languageModel(modelID)` as the AI SDK `LanguageModelV3`.
|
|
7
|
+
*
|
|
8
|
+
* The Cascade protocol implementation lives in `src/protocol/` — vendored,
|
|
9
|
+
* audited, and owned in this repository (see `src/protocol/ATTRIBUTION.md`).
|
|
10
|
+
*/
|
|
11
|
+
import { createDevin } from "./protocol/model.js";
|
|
12
|
+
export function createDevinProvider(options) {
|
|
13
|
+
const provider = createDevin({
|
|
14
|
+
apiKey: options.apiKey ?? "",
|
|
15
|
+
baseURL: options.baseURL,
|
|
16
|
+
});
|
|
17
|
+
return {
|
|
18
|
+
languageModel: (modelID) => provider.languageModel(modelID),
|
|
19
|
+
};
|
|
20
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json.schemastore.org/package.json",
|
|
3
|
+
"name": "opencode-devin",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"description": "OpenCode v2 plugin: connect your Devin subscription to opencode with native /connect login and devin/* models",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./provider": {
|
|
14
|
+
"types": "./dist/provider.d.ts",
|
|
15
|
+
"default": "./dist/provider.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "node -e \"fs.rmSync('dist',{recursive:true,force:true})\" && tsc",
|
|
23
|
+
"typecheck": "tsc --noEmit",
|
|
24
|
+
"prepublishOnly": "npm run build"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"opencode",
|
|
28
|
+
"opencode-plugin",
|
|
29
|
+
"opencode-v2",
|
|
30
|
+
"devin",
|
|
31
|
+
"cognition",
|
|
32
|
+
"windsurf"
|
|
33
|
+
],
|
|
34
|
+
"license": "MIT",
|
|
35
|
+
"author": {
|
|
36
|
+
"name": "JoShMiQueL",
|
|
37
|
+
"email": "joshmiquel.cs@gmail.com"
|
|
38
|
+
},
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "git+https://github.com/JoShMiQueL/opencode-devin.git"
|
|
42
|
+
},
|
|
43
|
+
"homepage": "https://github.com/JoShMiQueL/opencode-devin#readme",
|
|
44
|
+
"bugs": "https://github.com/JoShMiQueL/opencode-devin/issues",
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"@ai-sdk/provider": "3.0.8",
|
|
47
|
+
"@opencode/plugin": "2.0.16"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@types/bun": "1.4.2",
|
|
51
|
+
"typescript": "5.9.3"
|
|
52
|
+
}
|
|
53
|
+
}
|