@yuridulin/pointer 0.0.0-stage → 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/README.md +54 -2
- package/package.json +51 -4
- package/server/acp/catalog.ts +111 -0
- package/server/acp/connection.ts +233 -0
- package/server/acp/fake-agent.ts +408 -0
- package/server/acp/protocol.ts +167 -0
- package/server/acp/run.ts +139 -0
- package/server/acp/session.ts +1249 -0
- package/server/attachments.ts +115 -0
- package/server/changes.ts +239 -0
- package/server/cli.ts +362 -0
- package/server/compact.ts +361 -0
- package/server/config.ts +176 -0
- package/server/db.ts +462 -0
- package/server/dev.ts +8 -0
- package/server/diff.ts +190 -0
- package/server/engines.ts +63 -0
- package/server/files.ts +254 -0
- package/server/git.ts +319 -0
- package/server/history.ts +425 -0
- package/server/http/attachments.ts +63 -0
- package/server/http/browse.ts +108 -0
- package/server/http/chats.ts +627 -0
- package/server/http/closing.ts +14 -0
- package/server/http/files.ts +115 -0
- package/server/http/git.ts +84 -0
- package/server/http/preview.ts +21 -0
- package/server/http/providers.ts +200 -0
- package/server/http/reply.ts +38 -0
- package/server/hub.ts +521 -0
- package/server/install.ts +169 -0
- package/server/jobs.ts +664 -0
- package/server/linkPreview.ts +172 -0
- package/server/loop.ts +328 -0
- package/server/main.ts +353 -0
- package/server/probe.ts +34 -0
- package/server/process.ts +333 -0
- package/server/prompt.ts +196 -0
- package/server/providers/fake.ts +100 -0
- package/server/providers/opencode.ts +468 -0
- package/server/providers/registry.ts +149 -0
- package/server/providers/store.ts +210 -0
- package/server/segments.ts +248 -0
- package/server/sse.ts +93 -0
- package/server/tools/bash.ts +222 -0
- package/server/tools/edit.ts +254 -0
- package/server/tools/glob.ts +63 -0
- package/server/tools/grep.ts +122 -0
- package/server/tools/ignore.ts +54 -0
- package/server/tools/index.ts +129 -0
- package/server/tools/jail.ts +85 -0
- package/server/tools/jobs.ts +293 -0
- package/server/tools/read.ts +248 -0
- package/server/tools/write.ts +70 -0
- package/server/tray.ps1 +191 -0
- package/server/turn.ts +663 -0
- package/server/watch.ts +94 -0
- package/shared/acpConfig.ts +113 -0
- package/shared/links.ts +122 -0
- package/shared/types.ts +392 -0
- package/web/dist/app.css +1 -0
- package/web/dist/assets/codicon-mxdk0fnd.ttf +0 -0
- package/web/dist/index.html +28 -0
- package/web/dist/main.css +1 -0
- package/web/dist/main.js +1366 -0
- package/web/dist/tokens.css +1 -0
- package/web/dist/workers/css.worker.js +97 -0
- package/web/dist/workers/editor.worker.js +30 -0
- package/web/dist/workers/html.worker.js +300 -0
- package/web/dist/workers/json.worker.js +62 -0
- package/web/dist/workers/ts.worker.js +67733 -0
|
@@ -0,0 +1,468 @@
|
|
|
1
|
+
import { readSSE } from '../sse'
|
|
2
|
+
import type {
|
|
3
|
+
AssistantMessage,
|
|
4
|
+
Block,
|
|
5
|
+
ModelMessage,
|
|
6
|
+
StopReason,
|
|
7
|
+
StreamEvent,
|
|
8
|
+
StreamLLMInput,
|
|
9
|
+
TextBlock,
|
|
10
|
+
ThinkingBlock,
|
|
11
|
+
ToolCallBlock,
|
|
12
|
+
ToolDef,
|
|
13
|
+
Usage,
|
|
14
|
+
} from '../../shared/types'
|
|
15
|
+
|
|
16
|
+
const DEFAULT_BASE_URL = 'https://opencode.ai/inference/go'
|
|
17
|
+
const MAX_ATTEMPTS = 4
|
|
18
|
+
|
|
19
|
+
export type FetchLike = (input: string | URL | Request, init?: RequestInit) => Promise<Response>
|
|
20
|
+
|
|
21
|
+
/** `opencode` posts to `{base}/openai/v1/...`. `openai` posts to `{base}/v1/...`, or `{base}/...` when base already ends in `/v1`. */
|
|
22
|
+
export type ChatLayout = 'opencode' | 'openai'
|
|
23
|
+
|
|
24
|
+
export type StreamDeps = {
|
|
25
|
+
fetch?: FetchLike
|
|
26
|
+
pause?: (attempt: number) => Promise<void>
|
|
27
|
+
baseUrl?: string
|
|
28
|
+
onRetry?: (attempt: number) => void
|
|
29
|
+
apiKey?: string
|
|
30
|
+
layout?: ChatLayout
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
type ToolAcc = { id: string; name: string; args: string; started: boolean }
|
|
34
|
+
|
|
35
|
+
type Acc = {
|
|
36
|
+
text: string
|
|
37
|
+
thinking: string
|
|
38
|
+
tools: Map<number, ToolAcc>
|
|
39
|
+
finish: string | null
|
|
40
|
+
usage: Usage
|
|
41
|
+
checkpoint: string | null
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
type ChatChunk = {
|
|
45
|
+
model?: string
|
|
46
|
+
system_fingerprint?: string
|
|
47
|
+
choices?: Array<{
|
|
48
|
+
delta?: {
|
|
49
|
+
content?: string | null
|
|
50
|
+
reasoning_content?: string | null
|
|
51
|
+
reasoning?: string | null
|
|
52
|
+
tool_calls?: Array<{
|
|
53
|
+
index?: number
|
|
54
|
+
id?: string
|
|
55
|
+
function?: { name?: string; arguments?: string }
|
|
56
|
+
}>
|
|
57
|
+
}
|
|
58
|
+
finish_reason?: string | null
|
|
59
|
+
}>
|
|
60
|
+
usage?: {
|
|
61
|
+
prompt_tokens?: number
|
|
62
|
+
completion_tokens?: number
|
|
63
|
+
prompt_tokens_details?: { cached_tokens?: number }
|
|
64
|
+
}
|
|
65
|
+
error?: { message?: string } | string
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export async function* streamLLM(input: StreamLLMInput, deps?: StreamDeps): AsyncGenerator<StreamEvent> {
|
|
69
|
+
const acc = emptyAcc()
|
|
70
|
+
const fetchImpl = deps?.fetch ?? fetch
|
|
71
|
+
const session = input.sessionId ?? crypto.randomUUID()
|
|
72
|
+
let response: Response | undefined
|
|
73
|
+
let failure = ''
|
|
74
|
+
let sawDone = false
|
|
75
|
+
|
|
76
|
+
for (let attempt = 0; attempt < MAX_ATTEMPTS; attempt++) {
|
|
77
|
+
if (input.signal?.aborted) {
|
|
78
|
+
yield done(acc, 'aborted')
|
|
79
|
+
return
|
|
80
|
+
}
|
|
81
|
+
if (attempt > 0) {
|
|
82
|
+
deps?.onRetry?.(attempt)
|
|
83
|
+
try {
|
|
84
|
+
await pauseFor(attempt - 1, input.signal, deps)
|
|
85
|
+
} catch (error) {
|
|
86
|
+
if (isAbort(error) || input.signal?.aborted) {
|
|
87
|
+
yield done(acc, 'aborted')
|
|
88
|
+
return
|
|
89
|
+
}
|
|
90
|
+
throw error
|
|
91
|
+
}
|
|
92
|
+
if (input.signal?.aborted) {
|
|
93
|
+
yield done(acc, 'aborted')
|
|
94
|
+
return
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
try {
|
|
99
|
+
response = await fetchImpl(chatCompletionsUrl(deps?.baseUrl, deps?.layout), {
|
|
100
|
+
method: 'POST',
|
|
101
|
+
headers: {
|
|
102
|
+
Authorization: `Bearer ${deps?.apiKey ?? process.env.OPENCODE_API_KEY ?? ''}`,
|
|
103
|
+
'Content-Type': 'application/json',
|
|
104
|
+
'User-Agent': 'pointer/0.1',
|
|
105
|
+
'x-opencode-session': session,
|
|
106
|
+
},
|
|
107
|
+
body: JSON.stringify(requestBody(input)),
|
|
108
|
+
signal: input.signal,
|
|
109
|
+
})
|
|
110
|
+
} catch (error) {
|
|
111
|
+
if (isAbort(error) || input.signal?.aborted) {
|
|
112
|
+
yield done(acc, 'aborted')
|
|
113
|
+
return
|
|
114
|
+
}
|
|
115
|
+
failure = error instanceof Error ? error.message : String(error)
|
|
116
|
+
response = undefined
|
|
117
|
+
if (attempt === MAX_ATTEMPTS - 1) break
|
|
118
|
+
continue
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (response.ok) break
|
|
122
|
+
failure = clip(await response.text())
|
|
123
|
+
if (!retryable(response.status, failure) || attempt === MAX_ATTEMPTS - 1) {
|
|
124
|
+
yield* fail(acc, failure || `HTTP ${response.status}`)
|
|
125
|
+
return
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (!response?.ok || !response.body) {
|
|
130
|
+
yield* fail(acc, failure || 'request failed')
|
|
131
|
+
return
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const upstreamModel = response.headers.get('x-opencode-upstream-model-id')?.trim()
|
|
135
|
+
if (upstreamModel && upstreamModel !== input.model && upstreamModel !== `deepseek/${input.model}`) {
|
|
136
|
+
acc.checkpoint = upstreamModel
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
try {
|
|
140
|
+
for await (const data of readSSE(response.body, input.signal)) {
|
|
141
|
+
if (input.signal?.aborted) {
|
|
142
|
+
yield done(acc, 'aborted')
|
|
143
|
+
return
|
|
144
|
+
}
|
|
145
|
+
if (data === '[DONE]') {
|
|
146
|
+
sawDone = true
|
|
147
|
+
break
|
|
148
|
+
}
|
|
149
|
+
let json: unknown
|
|
150
|
+
try {
|
|
151
|
+
json = JSON.parse(data)
|
|
152
|
+
} catch {
|
|
153
|
+
yield done(acc, 'error', 'Провайдер прислал некорректный JSON')
|
|
154
|
+
return
|
|
155
|
+
}
|
|
156
|
+
if (!isRecord(json)) {
|
|
157
|
+
yield done(acc, 'error', 'Провайдер прислал некорректный ответ')
|
|
158
|
+
return
|
|
159
|
+
}
|
|
160
|
+
const chunk = json as ChatChunk
|
|
161
|
+
if (chunk.error !== undefined) {
|
|
162
|
+
const message = clip(errorText(chunk.error))
|
|
163
|
+
yield done(acc, 'error', message)
|
|
164
|
+
return
|
|
165
|
+
}
|
|
166
|
+
for (const event of consume(acc, chunk, input.model)) yield event
|
|
167
|
+
}
|
|
168
|
+
} catch (error) {
|
|
169
|
+
if (isAbort(error) || input.signal?.aborted) {
|
|
170
|
+
yield done(acc, 'aborted')
|
|
171
|
+
return
|
|
172
|
+
}
|
|
173
|
+
yield done(acc, 'error', error instanceof Error ? error.message : String(error))
|
|
174
|
+
return
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (input.signal?.aborted) {
|
|
178
|
+
yield done(acc, 'aborted')
|
|
179
|
+
return
|
|
180
|
+
}
|
|
181
|
+
if (!sawDone && acc.finish === null) {
|
|
182
|
+
yield done(acc, 'error', 'Поток завершился без [DONE] и без finish_reason')
|
|
183
|
+
return
|
|
184
|
+
}
|
|
185
|
+
yield done(acc, mapStop(acc.finish))
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function requestBody(input: StreamLLMInput): Record<string, unknown> {
|
|
189
|
+
const body: Record<string, unknown> = {
|
|
190
|
+
model: input.model,
|
|
191
|
+
messages: toApiMessages(input.system, input.messages),
|
|
192
|
+
stream: true,
|
|
193
|
+
stream_options: { include_usage: true },
|
|
194
|
+
}
|
|
195
|
+
const tools = toApiTools(input.tools)
|
|
196
|
+
if (tools) body.tools = tools
|
|
197
|
+
if (input.reasoning) {
|
|
198
|
+
body.reasoning_effort = input.reasoning
|
|
199
|
+
body.thinking = { type: 'enabled' }
|
|
200
|
+
}
|
|
201
|
+
return body
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function toApiMessages(system: string | undefined, messages: ModelMessage[]): Record<string, unknown>[] {
|
|
205
|
+
const out: Record<string, unknown>[] = []
|
|
206
|
+
if (system) out.push({ role: 'system', content: system })
|
|
207
|
+
for (const message of messages) {
|
|
208
|
+
if (message.role === 'user') {
|
|
209
|
+
out.push({ role: 'user', content: message.content.text })
|
|
210
|
+
continue
|
|
211
|
+
}
|
|
212
|
+
if (message.role === 'tool') {
|
|
213
|
+
out.push({
|
|
214
|
+
role: 'tool',
|
|
215
|
+
tool_call_id: message.content.toolCallId,
|
|
216
|
+
content: message.content.text,
|
|
217
|
+
})
|
|
218
|
+
continue
|
|
219
|
+
}
|
|
220
|
+
out.push(assistantMessage(message))
|
|
221
|
+
}
|
|
222
|
+
return out
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export function chatCompletionsUrl(baseUrl?: string, layout: ChatLayout = 'opencode'): string {
|
|
226
|
+
return endpointUrl(baseUrl, layout, 'chat')
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export function modelsUrl(baseUrl: string | undefined, layout: ChatLayout): string {
|
|
230
|
+
return endpointUrl(baseUrl, layout, 'models')
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function endpointUrl(baseUrl: string | undefined, layout: ChatLayout, leaf: 'chat' | 'models'): string {
|
|
234
|
+
const base = (baseUrl ?? DEFAULT_BASE_URL).replace(/\/+$/, '')
|
|
235
|
+
if (layout === 'opencode') {
|
|
236
|
+
return leaf === 'chat' ? `${base}/openai/v1/chat/completions` : `${base}/v1/models`
|
|
237
|
+
}
|
|
238
|
+
const root = base.endsWith('/v1') ? base : `${base}/v1`
|
|
239
|
+
return leaf === 'chat' ? `${root}/chat/completions` : `${root}/models`
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function assistantMessage(message: AssistantMessage): Record<string, unknown> {
|
|
243
|
+
const text = message.content.blocks
|
|
244
|
+
.filter((block): block is TextBlock => block.type === 'text')
|
|
245
|
+
.map((block) => block.text)
|
|
246
|
+
.join('')
|
|
247
|
+
const thinking = message.content.blocks
|
|
248
|
+
.filter((block): block is ThinkingBlock => block.type === 'thinking')
|
|
249
|
+
.map((block) => block.text)
|
|
250
|
+
.join('')
|
|
251
|
+
const toolCalls = message.content.blocks.filter((block): block is ToolCallBlock => block.type === 'tool_call')
|
|
252
|
+
const assistant: Record<string, unknown> = { role: 'assistant', content: text }
|
|
253
|
+
if (thinking) assistant.reasoning_content = thinking
|
|
254
|
+
if (toolCalls.length > 0) {
|
|
255
|
+
assistant.tool_calls = toolCalls.map((call) => ({
|
|
256
|
+
id: call.id,
|
|
257
|
+
type: 'function',
|
|
258
|
+
function: { name: call.name, arguments: JSON.stringify(call.args ?? {}) },
|
|
259
|
+
}))
|
|
260
|
+
}
|
|
261
|
+
return assistant
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function toApiTools(tools: ToolDef[] | undefined): Record<string, unknown>[] | undefined {
|
|
265
|
+
if (!tools?.length) return undefined
|
|
266
|
+
return tools.map((tool) => ({
|
|
267
|
+
type: 'function',
|
|
268
|
+
function: {
|
|
269
|
+
name: tool.name,
|
|
270
|
+
description: tool.description,
|
|
271
|
+
parameters: tool.parameters,
|
|
272
|
+
},
|
|
273
|
+
}))
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function consume(acc: Acc, chunk: ChatChunk, requestedModel: string): StreamEvent[] {
|
|
277
|
+
const events: StreamEvent[] = []
|
|
278
|
+
if (typeof chunk.system_fingerprint === 'string' && chunk.system_fingerprint) {
|
|
279
|
+
acc.checkpoint = chunk.system_fingerprint
|
|
280
|
+
} else if (
|
|
281
|
+
typeof chunk.model === 'string' &&
|
|
282
|
+
chunk.model &&
|
|
283
|
+
chunk.model !== requestedModel &&
|
|
284
|
+
chunk.model !== `deepseek/${requestedModel}`
|
|
285
|
+
) {
|
|
286
|
+
acc.checkpoint = chunk.model
|
|
287
|
+
}
|
|
288
|
+
if (chunk.usage) acc.usage = mapUsage(chunk.usage)
|
|
289
|
+
const choice = chunk.choices?.[0]
|
|
290
|
+
const delta = choice?.delta
|
|
291
|
+
if (typeof delta?.content === 'string' && delta.content) {
|
|
292
|
+
acc.text += delta.content
|
|
293
|
+
events.push({ type: 'text_delta', text: delta.content })
|
|
294
|
+
}
|
|
295
|
+
const thinking =
|
|
296
|
+
typeof delta?.reasoning_content === 'string'
|
|
297
|
+
? delta.reasoning_content
|
|
298
|
+
: typeof delta?.reasoning === 'string'
|
|
299
|
+
? delta.reasoning
|
|
300
|
+
: ''
|
|
301
|
+
if (thinking) {
|
|
302
|
+
acc.thinking += thinking
|
|
303
|
+
events.push({ type: 'thinking_delta', text: thinking })
|
|
304
|
+
}
|
|
305
|
+
if (Array.isArray(delta?.tool_calls)) {
|
|
306
|
+
for (const part of delta.tool_calls) {
|
|
307
|
+
const index = typeof part.index === 'number' ? part.index : 0
|
|
308
|
+
let tool = acc.tools.get(index)
|
|
309
|
+
if (!tool) {
|
|
310
|
+
tool = { id: '', name: '', args: '', started: false }
|
|
311
|
+
acc.tools.set(index, tool)
|
|
312
|
+
}
|
|
313
|
+
if (typeof part.id === 'string' && part.id) tool.id = part.id
|
|
314
|
+
if (typeof part.function?.name === 'string' && part.function.name) tool.name = part.function.name
|
|
315
|
+
if (typeof part.function?.arguments === 'string') tool.args += part.function.arguments
|
|
316
|
+
if (!tool.started && tool.id && tool.name) {
|
|
317
|
+
tool.started = true
|
|
318
|
+
events.push({ type: 'tool_call_started', id: tool.id, name: tool.name })
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
if (typeof choice?.finish_reason === 'string' && choice.finish_reason) acc.finish = choice.finish_reason
|
|
323
|
+
return events
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
function done(acc: Acc, stopReason: StopReason, error?: string): StreamEvent {
|
|
327
|
+
return {
|
|
328
|
+
type: 'done',
|
|
329
|
+
blocks: blocksFrom(acc, stopReason),
|
|
330
|
+
stopReason,
|
|
331
|
+
usage: acc.usage,
|
|
332
|
+
...(error ? { error } : {}),
|
|
333
|
+
...(acc.checkpoint ? { servedCheckpoint: acc.checkpoint } : {}),
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
function* fail(acc: Acc, message: string): Generator<StreamEvent> {
|
|
338
|
+
yield done(acc, 'error', message || 'request failed')
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
function blocksFrom(acc: Acc, stopReason: StopReason): Block[] {
|
|
342
|
+
const blocks: Block[] = []
|
|
343
|
+
if (acc.thinking) blocks.push({ type: 'thinking', text: acc.thinking })
|
|
344
|
+
if (acc.text) blocks.push({ type: 'text', text: acc.text })
|
|
345
|
+
for (const index of [...acc.tools.keys()].sort((a, b) => a - b)) {
|
|
346
|
+
const tool = acc.tools.get(index)
|
|
347
|
+
if (!tool?.id || !tool.name) continue
|
|
348
|
+
const args = parseToolArgs(tool.args)
|
|
349
|
+
if (stopReason === 'max_tokens') {
|
|
350
|
+
blocks.push({
|
|
351
|
+
type: 'tool_call',
|
|
352
|
+
id: tool.id,
|
|
353
|
+
name: tool.name,
|
|
354
|
+
args: args.ok ? args.value : {},
|
|
355
|
+
error:
|
|
356
|
+
'Вызов не исполнялся: ответ обрезан по лимиту токенов, аргументы могли быть неполными. Переотправь вызов целиком',
|
|
357
|
+
})
|
|
358
|
+
continue
|
|
359
|
+
}
|
|
360
|
+
if (args.ok) {
|
|
361
|
+
blocks.push({ type: 'tool_call', id: tool.id, name: tool.name, args: args.value })
|
|
362
|
+
continue
|
|
363
|
+
}
|
|
364
|
+
// The call is kept with an error instead of being dropped: the model emitted it, so it
|
|
365
|
+
// must learn that nothing ran and why.
|
|
366
|
+
blocks.push({
|
|
367
|
+
type: 'tool_call',
|
|
368
|
+
id: tool.id,
|
|
369
|
+
name: tool.name,
|
|
370
|
+
args: {},
|
|
371
|
+
error: `Вызов не исполнялся: аргументы не разбираются как JSON (${args.error}). Переотправь вызов с корректным JSON`,
|
|
372
|
+
})
|
|
373
|
+
}
|
|
374
|
+
return blocks
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
type ParsedArgs = { ok: true; value: unknown } | { ok: false; error: string }
|
|
378
|
+
|
|
379
|
+
function parseToolArgs(raw: string): ParsedArgs {
|
|
380
|
+
const trimmed = raw.trim()
|
|
381
|
+
if (!trimmed) return { ok: true, value: {} }
|
|
382
|
+
try {
|
|
383
|
+
return { ok: true, value: JSON.parse(trimmed) }
|
|
384
|
+
} catch (error) {
|
|
385
|
+
return { ok: false, error: error instanceof Error ? error.message : String(error) }
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
function mapStop(finish: string | null): StopReason {
|
|
390
|
+
if (finish === 'stop' || finish == null) return 'end'
|
|
391
|
+
if (finish === 'tool_calls') return 'tool_use'
|
|
392
|
+
if (finish === 'length' || finish === 'max_tokens') return 'max_tokens'
|
|
393
|
+
return 'error'
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
function mapUsage(usage: NonNullable<ChatChunk['usage']>): Usage {
|
|
397
|
+
const mapped: Usage = {
|
|
398
|
+
input: usage.prompt_tokens ?? 0,
|
|
399
|
+
output: usage.completion_tokens ?? 0,
|
|
400
|
+
}
|
|
401
|
+
const cached = usage.prompt_tokens_details?.cached_tokens
|
|
402
|
+
if (typeof cached === 'number') mapped.cacheRead = cached
|
|
403
|
+
return mapped
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
function emptyAcc(): Acc {
|
|
407
|
+
return {
|
|
408
|
+
text: '',
|
|
409
|
+
thinking: '',
|
|
410
|
+
tools: new Map(),
|
|
411
|
+
finish: null,
|
|
412
|
+
usage: { input: 0, output: 0 },
|
|
413
|
+
checkpoint: null,
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
function retryable(status: number, body: string): boolean {
|
|
418
|
+
if (status === 429 || status >= 500) return true
|
|
419
|
+
return /overloaded|перегружено/i.test(body)
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
async function pauseFor(attempt: number, signal: AbortSignal | undefined, deps?: StreamDeps): Promise<void> {
|
|
423
|
+
if (deps?.pause) {
|
|
424
|
+
await deps.pause(attempt)
|
|
425
|
+
return
|
|
426
|
+
}
|
|
427
|
+
await sleep(250 * 2 ** attempt, signal)
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
function sleep(ms: number, signal?: AbortSignal): Promise<void> {
|
|
431
|
+
return new Promise((resolve, reject) => {
|
|
432
|
+
if (signal?.aborted) {
|
|
433
|
+
reject(abortError())
|
|
434
|
+
return
|
|
435
|
+
}
|
|
436
|
+
const onAbort = () => {
|
|
437
|
+
clearTimeout(timer)
|
|
438
|
+
reject(abortError())
|
|
439
|
+
}
|
|
440
|
+
const timer = setTimeout(() => {
|
|
441
|
+
signal?.removeEventListener('abort', onAbort)
|
|
442
|
+
resolve()
|
|
443
|
+
}, ms)
|
|
444
|
+
signal?.addEventListener('abort', onAbort, { once: true })
|
|
445
|
+
})
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
function abortError(): DOMException {
|
|
449
|
+
return new DOMException('The operation was aborted.', 'AbortError')
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
function isAbort(error: unknown): boolean {
|
|
453
|
+
return error instanceof Error && error.name === 'AbortError'
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
function errorText(error: { message?: string } | string): string {
|
|
457
|
+
if (typeof error === 'string') return error
|
|
458
|
+
return error.message ?? 'provider error'
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
function clip(text: string): string {
|
|
462
|
+
const trimmed = text.trim()
|
|
463
|
+
return trimmed.length > 2000 ? trimmed.slice(0, 2000) : trimmed
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
467
|
+
return typeof value === 'object' && value !== null
|
|
468
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import type { Database } from 'bun:sqlite'
|
|
2
|
+
import type { StreamEvent, StreamLLMInput } from '../../shared/types'
|
|
3
|
+
import type { Config } from '../config'
|
|
4
|
+
import { streamLLM, type ChatLayout, type FetchLike } from './opencode'
|
|
5
|
+
import { getProvider, providerEnabled, type ProviderRow } from './store'
|
|
6
|
+
|
|
7
|
+
export type ProviderDeps = {
|
|
8
|
+
baseUrl?: string
|
|
9
|
+
onRetry?: (attempt: number) => void
|
|
10
|
+
fetch?: FetchLike
|
|
11
|
+
apiKey?: string
|
|
12
|
+
layout?: ChatLayout
|
|
13
|
+
db?: Database
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type ProviderSpec = {
|
|
17
|
+
id: string
|
|
18
|
+
label: string
|
|
19
|
+
defaultBaseUrl: string
|
|
20
|
+
apiKeyEnv?: string
|
|
21
|
+
chatLayout: ChatLayout
|
|
22
|
+
stream: (input: StreamLLMInput, deps?: ProviderDeps) => AsyncGenerator<StreamEvent>
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const PROVIDERS: readonly ProviderSpec[] = [
|
|
26
|
+
{
|
|
27
|
+
id: 'opencode-go',
|
|
28
|
+
label: 'OpenCode Go',
|
|
29
|
+
defaultBaseUrl: 'https://opencode.ai/inference/go',
|
|
30
|
+
apiKeyEnv: 'OPENCODE_API_KEY',
|
|
31
|
+
chatLayout: 'opencode',
|
|
32
|
+
stream: streamLLM,
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
id: 'openai',
|
|
36
|
+
label: 'OpenAI-compatible',
|
|
37
|
+
defaultBaseUrl: 'https://api.openai.com/v1',
|
|
38
|
+
apiKeyEnv: 'OPENAI_API_KEY',
|
|
39
|
+
chatLayout: 'openai',
|
|
40
|
+
stream: streamLLM,
|
|
41
|
+
},
|
|
42
|
+
]
|
|
43
|
+
|
|
44
|
+
const FALLBACK_PROVIDER = 'opencode-go'
|
|
45
|
+
|
|
46
|
+
export function providerById(id: string): ProviderSpec | null {
|
|
47
|
+
return PROVIDERS.find((provider) => provider.id === id) ?? null
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** The provider named by `model` when its prefix is known, otherwise the default adapter. */
|
|
51
|
+
export function defaultProviderId(config: Config): string {
|
|
52
|
+
const slash = config.model.indexOf('/')
|
|
53
|
+
if (slash > 0) {
|
|
54
|
+
const prefix = config.model.slice(0, slash)
|
|
55
|
+
if (providerById(prefix)) return prefix
|
|
56
|
+
}
|
|
57
|
+
return FALLBACK_PROVIDER
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function resolveModel(
|
|
61
|
+
model: string,
|
|
62
|
+
fallbackProvider: string,
|
|
63
|
+
): { provider: ProviderSpec; model: string } | null {
|
|
64
|
+
const trimmed = model.trim()
|
|
65
|
+
if (!trimmed) return null
|
|
66
|
+
const slash = trimmed.indexOf('/')
|
|
67
|
+
if (slash === -1) {
|
|
68
|
+
const provider = providerById(fallbackProvider)
|
|
69
|
+
return provider ? { provider, model: trimmed } : null
|
|
70
|
+
}
|
|
71
|
+
const provider = providerById(trimmed.slice(0, slash))
|
|
72
|
+
if (!provider) return null
|
|
73
|
+
const bare = trimmed.slice(slash + 1)
|
|
74
|
+
return bare ? { provider, model: bare } : null
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function baseUrlFor(
|
|
78
|
+
config: Config,
|
|
79
|
+
provider: ProviderSpec,
|
|
80
|
+
slot?: Pick<ProviderRow, 'base_url'> | null,
|
|
81
|
+
): string {
|
|
82
|
+
// OpenCode Go has one known address. The page does not store a base URL for it.
|
|
83
|
+
if (provider.id !== 'opencode-go' && slot?.base_url) return slot.base_url
|
|
84
|
+
const configured = config.providers?.[provider.id]?.baseUrl
|
|
85
|
+
if (configured) return configured
|
|
86
|
+
if (provider.id === defaultProviderId(config)) return config.baseUrl
|
|
87
|
+
return provider.defaultBaseUrl
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** A key saved on the slot wins. The environment variable is the fallback until the page stores one. */
|
|
91
|
+
export function apiKeyFor(provider: ProviderSpec, slot?: Pick<ProviderRow, 'api_key'> | null): string | null {
|
|
92
|
+
if (slot?.api_key) return slot.api_key
|
|
93
|
+
if (!provider.apiKeyEnv) return null
|
|
94
|
+
const env = process.env[provider.apiKeyEnv]
|
|
95
|
+
return env ? env : null
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export async function* streamForModel(
|
|
99
|
+
model: string,
|
|
100
|
+
config: Config,
|
|
101
|
+
input: StreamLLMInput,
|
|
102
|
+
deps?: ProviderDeps,
|
|
103
|
+
): AsyncGenerator<StreamEvent> {
|
|
104
|
+
const resolved = resolveModel(model, defaultProviderId(config))
|
|
105
|
+
if (!resolved) {
|
|
106
|
+
const id = model.includes('/') ? model.slice(0, model.indexOf('/')) : model
|
|
107
|
+
yield {
|
|
108
|
+
type: 'done',
|
|
109
|
+
blocks: [],
|
|
110
|
+
stopReason: 'error',
|
|
111
|
+
usage: { input: 0, output: 0 },
|
|
112
|
+
error: `Неизвестный провайдер: ${id}. Доступны: ${PROVIDERS.map((provider) => provider.id).join(', ')}`,
|
|
113
|
+
}
|
|
114
|
+
return
|
|
115
|
+
}
|
|
116
|
+
const { provider } = resolved
|
|
117
|
+
const slot = deps?.db ? getProvider(deps.db, provider.id) : null
|
|
118
|
+
if (slot && !providerEnabled(slot)) {
|
|
119
|
+
yield {
|
|
120
|
+
type: 'done',
|
|
121
|
+
blocks: [],
|
|
122
|
+
stopReason: 'error',
|
|
123
|
+
usage: { input: 0, output: 0 },
|
|
124
|
+
error: `Провайдер выключен: ${provider.label}`,
|
|
125
|
+
}
|
|
126
|
+
return
|
|
127
|
+
}
|
|
128
|
+
const apiKey = apiKeyFor(provider, slot)
|
|
129
|
+
if (!apiKey) {
|
|
130
|
+
yield {
|
|
131
|
+
type: 'done',
|
|
132
|
+
blocks: [],
|
|
133
|
+
stopReason: 'error',
|
|
134
|
+
usage: { input: 0, output: 0 },
|
|
135
|
+
error: `Не задан ${provider.apiKeyEnv ?? 'ключ'}`,
|
|
136
|
+
}
|
|
137
|
+
return
|
|
138
|
+
}
|
|
139
|
+
yield* provider.stream(
|
|
140
|
+
{ ...input, model: resolved.model },
|
|
141
|
+
{
|
|
142
|
+
fetch: deps?.fetch,
|
|
143
|
+
onRetry: deps?.onRetry,
|
|
144
|
+
baseUrl: baseUrlFor(config, provider, slot),
|
|
145
|
+
apiKey,
|
|
146
|
+
layout: provider.chatLayout,
|
|
147
|
+
},
|
|
148
|
+
)
|
|
149
|
+
}
|