@kkelly-offical/kkcode 0.1.3 → 0.1.7
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 +110 -172
- package/package.json +46 -46
- package/src/agent/agent.mjs +220 -170
- package/src/agent/prompt/bug-hunter.txt +90 -0
- package/src/agent/prompt/frontend-designer.txt +58 -0
- package/src/agent/prompt/longagent-blueprint-agent.txt +83 -0
- package/src/agent/prompt/longagent-coding-agent.txt +37 -0
- package/src/agent/prompt/longagent-debugging-agent.txt +46 -0
- package/src/agent/prompt/longagent-preview-agent.txt +63 -0
- package/src/config/defaults.mjs +260 -195
- package/src/config/schema.mjs +71 -6
- package/src/core/constants.mjs +91 -46
- package/src/index.mjs +1 -1
- package/src/knowledge/frontend-aesthetics.txt +39 -0
- package/src/knowledge/loader.mjs +2 -1
- package/src/knowledge/tailwind.txt +12 -3
- package/src/mcp/client-http.mjs +141 -157
- package/src/mcp/client-sse.mjs +288 -286
- package/src/mcp/client-stdio.mjs +533 -451
- package/src/mcp/constants.mjs +2 -0
- package/src/mcp/registry.mjs +479 -394
- package/src/mcp/stdio-framing.mjs +133 -127
- package/src/mcp/tool-result.mjs +24 -0
- package/src/observability/index.mjs +42 -0
- package/src/observability/metrics.mjs +137 -0
- package/src/observability/tracer.mjs +137 -0
- package/src/orchestration/background-manager.mjs +372 -358
- package/src/orchestration/background-worker.mjs +305 -245
- package/src/orchestration/longagent-manager.mjs +171 -116
- package/src/orchestration/stage-scheduler.mjs +728 -489
- package/src/permission/exec-policy.mjs +9 -11
- package/src/provider/anthropic.mjs +1 -0
- package/src/provider/openai.mjs +340 -339
- package/src/provider/retry-policy.mjs +68 -68
- package/src/provider/router.mjs +241 -228
- package/src/provider/sse.mjs +104 -91
- package/src/repl.mjs +59 -7
- package/src/session/checkpoint.mjs +66 -3
- package/src/session/compaction.mjs +298 -276
- package/src/session/engine.mjs +232 -225
- package/src/session/longagent-4stage.mjs +460 -0
- package/src/session/longagent-hybrid.mjs +1097 -0
- package/src/session/longagent-plan.mjs +365 -329
- package/src/session/longagent-project-memory.mjs +53 -0
- package/src/session/longagent-scaffold.mjs +291 -100
- package/src/session/longagent-task-bus.mjs +54 -0
- package/src/session/longagent-utils.mjs +472 -0
- package/src/session/longagent.mjs +900 -1462
- package/src/session/loop.mjs +65 -40
- package/src/session/project-context.mjs +30 -0
- package/src/session/prompt/agent.txt +25 -0
- package/src/session/prompt/plan.txt +31 -9
- package/src/session/rollback.mjs +196 -0
- package/src/session/store.mjs +519 -503
- package/src/session/system-prompt.mjs +273 -260
- package/src/session/task-validator.mjs +4 -3
- package/src/skill/builtin/design.mjs +76 -0
- package/src/skill/builtin/frontend.mjs +8 -0
- package/src/skill/registry.mjs +390 -336
- package/src/storage/ghost-commit-store.mjs +18 -8
- package/src/tool/executor.mjs +11 -0
- package/src/tool/git-auto.mjs +0 -19
- package/src/tool/question-prompt.mjs +93 -86
- package/src/tool/registry.mjs +71 -37
- package/src/ui/activity-renderer.mjs +664 -410
- package/src/util/git.mjs +23 -0
package/src/provider/openai.mjs
CHANGED
|
@@ -1,339 +1,340 @@
|
|
|
1
|
-
import { ProviderError } from "../core/errors.mjs"
|
|
2
|
-
import { requestWithRetry } from "./retry-policy.mjs"
|
|
3
|
-
import { parseSSE } from "./sse.mjs"
|
|
4
|
-
|
|
5
|
-
function sleep(ms) {
|
|
6
|
-
return new Promise((resolve) => setTimeout(resolve, ms))
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
function mapTools(tools) {
|
|
10
|
-
if (!tools || !tools.length) return undefined
|
|
11
|
-
return tools.map((tool) => ({
|
|
12
|
-
type: "function",
|
|
13
|
-
function: {
|
|
14
|
-
name: tool.name,
|
|
15
|
-
description: tool.description,
|
|
16
|
-
parameters: tool.inputSchema
|
|
17
|
-
}
|
|
18
|
-
}))
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function mapContentBlock(block) {
|
|
22
|
-
if (block.type === "image" && block.data) {
|
|
23
|
-
return {
|
|
24
|
-
type: "image_url",
|
|
25
|
-
image_url: {
|
|
26
|
-
url: `data:${block.mediaType || "image/png"};base64,${block.data}`
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
return { type: "text", text: String(block.text || block.content || "") }
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function mapMessages(messages) {
|
|
34
|
-
const mapped = []
|
|
35
|
-
for (const message of messages) {
|
|
36
|
-
const content = message.content
|
|
37
|
-
if (!Array.isArray(content)) {
|
|
38
|
-
mapped.push({ role: message.role, content: String(content || "") })
|
|
39
|
-
continue
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// Check for native tool_use blocks (assistant message with tool calls)
|
|
43
|
-
const toolUseBlocks = content.filter((b) => b.type === "tool_use")
|
|
44
|
-
if (toolUseBlocks.length > 0 && message.role === "assistant") {
|
|
45
|
-
const textParts = content.filter((b) => b.type === "text").map((b) => b.text || "").join("\n")
|
|
46
|
-
mapped.push({
|
|
47
|
-
role: "assistant",
|
|
48
|
-
content: textParts || null,
|
|
49
|
-
tool_calls: toolUseBlocks.map((b) => ({
|
|
50
|
-
id: b.id,
|
|
51
|
-
type: "function",
|
|
52
|
-
function: {
|
|
53
|
-
name: b.name,
|
|
54
|
-
arguments: JSON.stringify(b.input || {})
|
|
55
|
-
}
|
|
56
|
-
}))
|
|
57
|
-
})
|
|
58
|
-
continue
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// Check for tool_result blocks (user message with tool results)
|
|
62
|
-
const toolResultBlocks = content.filter((b) => b.type === "tool_result")
|
|
63
|
-
if (toolResultBlocks.length > 0) {
|
|
64
|
-
for (const result of toolResultBlocks) {
|
|
65
|
-
mapped.push({
|
|
66
|
-
role: "tool",
|
|
67
|
-
tool_call_id: result.tool_use_id,
|
|
68
|
-
content: String(result.content || "")
|
|
69
|
-
})
|
|
70
|
-
}
|
|
71
|
-
continue
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// Regular array content (images, text)
|
|
75
|
-
mapped.push({ role: message.role, content: content.map(mapContentBlock) })
|
|
76
|
-
}
|
|
77
|
-
return mapped
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
function parseToolCalls(message) {
|
|
81
|
-
if (!Array.isArray(message?.tool_calls)) return []
|
|
82
|
-
return message.tool_calls
|
|
83
|
-
.filter((call) => call?.function?.name)
|
|
84
|
-
.map((call) => {
|
|
85
|
-
const raw = call.function.arguments || "{}"
|
|
86
|
-
let args = {}
|
|
87
|
-
try {
|
|
88
|
-
args = JSON.parse(raw)
|
|
89
|
-
} catch (parseErr) {
|
|
90
|
-
console.error(`[openai] tool_call JSON parse failed for "${call.function.name}": ${parseErr.message} (${raw.length} chars, first 200: ${raw.slice(0, 200)})`)
|
|
91
|
-
args = { __parse_error: true, __raw_length: raw.length, __error: parseErr.message }
|
|
92
|
-
}
|
|
93
|
-
return {
|
|
94
|
-
id: call.id || `tc_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`,
|
|
95
|
-
name: call.function.name,
|
|
96
|
-
args
|
|
97
|
-
}
|
|
98
|
-
})
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
// Build system messages from structured blocks for optimal prefix caching.
|
|
102
|
-
// OpenAI auto-caches matching prefixes — stable content first, dynamic last.
|
|
103
|
-
function buildSystemMessages(system) {
|
|
104
|
-
if (!system) return []
|
|
105
|
-
if (system.blocks && Array.isArray(system.blocks)) {
|
|
106
|
-
const stable = []
|
|
107
|
-
const dynamic = []
|
|
108
|
-
for (const block of system.blocks) {
|
|
109
|
-
if (block.cacheable) stable.push(block.text)
|
|
110
|
-
else dynamic.push(block.text)
|
|
111
|
-
}
|
|
112
|
-
const msgs = []
|
|
113
|
-
if (stable.length) msgs.push({ role: "system", content: stable.join("\n\n") })
|
|
114
|
-
if (dynamic.length) msgs.push({ role: "system", content: dynamic.join("\n\n") })
|
|
115
|
-
return msgs
|
|
116
|
-
}
|
|
117
|
-
const text = typeof system === "string" ? system : system.text || String(system)
|
|
118
|
-
return text ? [{ role: "system", content: text }] : []
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
function timeoutSignal(ms, parentSignal = null) {
|
|
122
|
-
const own = AbortSignal.timeout(ms)
|
|
123
|
-
if (!parentSignal) return own
|
|
124
|
-
return AbortSignal.any([parentSignal, own])
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
export async function countTokensOpenAI(input) {
|
|
128
|
-
const { apiKey, baseUrl, model, system, messages, tools, timeoutMs = 10000 } = input
|
|
129
|
-
if (!apiKey) return null
|
|
130
|
-
const endpoint = `${baseUrl.replace(/\/$/, "")}/chat/completions`
|
|
131
|
-
const payload = {
|
|
132
|
-
model,
|
|
133
|
-
messages: [...buildSystemMessages(system), ...mapMessages(messages)],
|
|
134
|
-
tools: mapTools(tools),
|
|
135
|
-
max_tokens: 1,
|
|
136
|
-
stream: false
|
|
137
|
-
}
|
|
138
|
-
try {
|
|
139
|
-
const res = await fetch(endpoint, {
|
|
140
|
-
method: "POST",
|
|
141
|
-
headers: { "content-type": "application/json", authorization: `Bearer ${apiKey}` },
|
|
142
|
-
body: JSON.stringify(payload),
|
|
143
|
-
signal: AbortSignal.timeout(timeoutMs)
|
|
144
|
-
})
|
|
145
|
-
if (!res.ok) return null
|
|
146
|
-
const json = await res.json()
|
|
147
|
-
return json?.usage?.prompt_tokens ?? null
|
|
148
|
-
} catch {
|
|
149
|
-
return null
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
export async function requestOpenAI(input) {
|
|
154
|
-
const { apiKey, baseUrl, model, system, messages, tools, timeoutMs = 120000, maxTokens, retry = {}, signal = null } = input
|
|
155
|
-
if (!apiKey) {
|
|
156
|
-
throw new ProviderError(`missing API key for openai provider (env: ${input.apiKeyEnv || "unknown"})`, {
|
|
157
|
-
provider: "openai"
|
|
158
|
-
})
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
const payload = {
|
|
162
|
-
model,
|
|
163
|
-
messages: [...buildSystemMessages(system), ...mapMessages(messages)],
|
|
164
|
-
tools: mapTools(tools),
|
|
165
|
-
tool_choice: tools?.length ? "auto" : undefined,
|
|
166
|
-
...(maxTokens ? { max_tokens: maxTokens } : {})
|
|
167
|
-
}
|
|
168
|
-
const endpoint = `${baseUrl.replace(/\/$/, "")}/chat/completions`
|
|
169
|
-
|
|
170
|
-
return requestWithRetry({
|
|
171
|
-
attempts: Number(retry.attempts ?? 3),
|
|
172
|
-
baseDelayMs: Number(retry.baseDelayMs ?? 800),
|
|
173
|
-
signal,
|
|
174
|
-
execute: async () => {
|
|
175
|
-
const response = await fetch(endpoint, {
|
|
176
|
-
method: "POST",
|
|
177
|
-
headers: {
|
|
178
|
-
"content-type": "application/json",
|
|
179
|
-
authorization: `Bearer ${apiKey}`
|
|
180
|
-
},
|
|
181
|
-
body: JSON.stringify(payload),
|
|
182
|
-
signal: timeoutSignal(timeoutMs, signal)
|
|
183
|
-
})
|
|
184
|
-
|
|
185
|
-
if (!response.ok) {
|
|
186
|
-
const text = await response.text().catch(() => "")
|
|
187
|
-
const error = new ProviderError(`openai request failed: ${response.status} ${text}`, {
|
|
188
|
-
provider: "openai",
|
|
189
|
-
model,
|
|
190
|
-
endpoint
|
|
191
|
-
})
|
|
192
|
-
error.httpStatus = response.status
|
|
193
|
-
throw error
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
const json = await response.json()
|
|
197
|
-
const message = json?.choices?.[0]?.message ?? {}
|
|
198
|
-
const promptTokens = json?.usage?.prompt_tokens ?? 0
|
|
199
|
-
const cachedTokens = json?.usage?.prompt_tokens_details?.cached_tokens ?? 0
|
|
200
|
-
const usage = {
|
|
201
|
-
input: promptTokens - cachedTokens,
|
|
202
|
-
output: json?.usage?.completion_tokens ?? 0,
|
|
203
|
-
cacheRead: cachedTokens,
|
|
204
|
-
cacheWrite: 0
|
|
205
|
-
}
|
|
206
|
-
const toolCalls = parseToolCalls(message)
|
|
207
|
-
const text = typeof message.content === "string" ? message.content : ""
|
|
208
|
-
return { text, usage, toolCalls }
|
|
209
|
-
}
|
|
210
|
-
})
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
export async function* requestOpenAIStream(input) {
|
|
214
|
-
const { apiKey, baseUrl, model, system, messages, tools, timeoutMs = 120000, streamIdleTimeoutMs = 120000, maxTokens, retry = {}, signal = null } = input
|
|
215
|
-
if (!apiKey) {
|
|
216
|
-
throw new ProviderError(`missing API key for openai provider (env: ${input.apiKeyEnv || "unknown"})`, {
|
|
217
|
-
provider: "openai"
|
|
218
|
-
})
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
const payload = {
|
|
222
|
-
model,
|
|
223
|
-
messages: [...buildSystemMessages(system), ...mapMessages(messages)],
|
|
224
|
-
tools: mapTools(tools),
|
|
225
|
-
tool_choice: tools?.length ? "auto" : undefined,
|
|
226
|
-
...(maxTokens ? { max_tokens: maxTokens } : {}),
|
|
227
|
-
stream: true,
|
|
228
|
-
stream_options: { include_usage: true }
|
|
229
|
-
}
|
|
230
|
-
const endpoint = `${baseUrl.replace(/\/$/, "")}/chat/completions`
|
|
231
|
-
const attempts = Number(retry.attempts ?? 3)
|
|
232
|
-
const baseDelayMs = Number(retry.baseDelayMs ?? 800)
|
|
233
|
-
|
|
234
|
-
let response
|
|
235
|
-
for (let attempt = 1; attempt <= attempts; attempt++) {
|
|
236
|
-
try {
|
|
237
|
-
// Use a connection-only timeout for the initial fetch.
|
|
238
|
-
// Once headers arrive, clear it — the SSE idle timeout handles the streaming phase.
|
|
239
|
-
const connController = new AbortController()
|
|
240
|
-
const connTimer = setTimeout(() => connController.abort(), timeoutMs)
|
|
241
|
-
const fetchSignal = signal
|
|
242
|
-
? AbortSignal.any([signal, connController.signal])
|
|
243
|
-
: connController.signal
|
|
244
|
-
|
|
245
|
-
response = await fetch(endpoint, {
|
|
246
|
-
method: "POST",
|
|
247
|
-
headers: {
|
|
248
|
-
"content-type": "application/json",
|
|
249
|
-
authorization: `Bearer ${apiKey}`
|
|
250
|
-
},
|
|
251
|
-
body: JSON.stringify(payload),
|
|
252
|
-
signal: fetchSignal
|
|
253
|
-
})
|
|
254
|
-
clearTimeout(connTimer)
|
|
255
|
-
|
|
256
|
-
if (!response.ok) {
|
|
257
|
-
const text = await response.text().catch(() => "")
|
|
258
|
-
const error = new ProviderError(`openai stream failed: ${response.status} ${text}`, {
|
|
259
|
-
provider: "openai", model, endpoint
|
|
260
|
-
})
|
|
261
|
-
error.httpStatus = response.status
|
|
262
|
-
throw error
|
|
263
|
-
}
|
|
264
|
-
break
|
|
265
|
-
} catch (err) {
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
const
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
if (tc.
|
|
309
|
-
if (tc.function?.
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
: finishReason === "
|
|
337
|
-
: finishReason
|
|
338
|
-
|
|
339
|
-
}
|
|
1
|
+
import { ProviderError } from "../core/errors.mjs"
|
|
2
|
+
import { requestWithRetry } from "./retry-policy.mjs"
|
|
3
|
+
import { parseSSE } from "./sse.mjs"
|
|
4
|
+
|
|
5
|
+
function sleep(ms) {
|
|
6
|
+
return new Promise((resolve) => setTimeout(resolve, ms))
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function mapTools(tools) {
|
|
10
|
+
if (!tools || !tools.length) return undefined
|
|
11
|
+
return tools.map((tool) => ({
|
|
12
|
+
type: "function",
|
|
13
|
+
function: {
|
|
14
|
+
name: tool.name,
|
|
15
|
+
description: tool.description,
|
|
16
|
+
parameters: tool.inputSchema
|
|
17
|
+
}
|
|
18
|
+
}))
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function mapContentBlock(block) {
|
|
22
|
+
if (block.type === "image" && block.data) {
|
|
23
|
+
return {
|
|
24
|
+
type: "image_url",
|
|
25
|
+
image_url: {
|
|
26
|
+
url: `data:${block.mediaType || "image/png"};base64,${block.data}`
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return { type: "text", text: String(block.text || block.content || "") }
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function mapMessages(messages) {
|
|
34
|
+
const mapped = []
|
|
35
|
+
for (const message of messages) {
|
|
36
|
+
const content = message.content
|
|
37
|
+
if (!Array.isArray(content)) {
|
|
38
|
+
mapped.push({ role: message.role, content: String(content || "") })
|
|
39
|
+
continue
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Check for native tool_use blocks (assistant message with tool calls)
|
|
43
|
+
const toolUseBlocks = content.filter((b) => b.type === "tool_use")
|
|
44
|
+
if (toolUseBlocks.length > 0 && message.role === "assistant") {
|
|
45
|
+
const textParts = content.filter((b) => b.type === "text").map((b) => b.text || "").join("\n")
|
|
46
|
+
mapped.push({
|
|
47
|
+
role: "assistant",
|
|
48
|
+
content: textParts || null,
|
|
49
|
+
tool_calls: toolUseBlocks.map((b) => ({
|
|
50
|
+
id: b.id,
|
|
51
|
+
type: "function",
|
|
52
|
+
function: {
|
|
53
|
+
name: b.name,
|
|
54
|
+
arguments: JSON.stringify(b.input || {})
|
|
55
|
+
}
|
|
56
|
+
}))
|
|
57
|
+
})
|
|
58
|
+
continue
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Check for tool_result blocks (user message with tool results)
|
|
62
|
+
const toolResultBlocks = content.filter((b) => b.type === "tool_result")
|
|
63
|
+
if (toolResultBlocks.length > 0) {
|
|
64
|
+
for (const result of toolResultBlocks) {
|
|
65
|
+
mapped.push({
|
|
66
|
+
role: "tool",
|
|
67
|
+
tool_call_id: result.tool_use_id,
|
|
68
|
+
content: String(result.content || "")
|
|
69
|
+
})
|
|
70
|
+
}
|
|
71
|
+
continue
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Regular array content (images, text)
|
|
75
|
+
mapped.push({ role: message.role, content: content.map(mapContentBlock) })
|
|
76
|
+
}
|
|
77
|
+
return mapped
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function parseToolCalls(message) {
|
|
81
|
+
if (!Array.isArray(message?.tool_calls)) return []
|
|
82
|
+
return message.tool_calls
|
|
83
|
+
.filter((call) => call?.function?.name)
|
|
84
|
+
.map((call) => {
|
|
85
|
+
const raw = call.function.arguments || "{}"
|
|
86
|
+
let args = {}
|
|
87
|
+
try {
|
|
88
|
+
args = JSON.parse(raw)
|
|
89
|
+
} catch (parseErr) {
|
|
90
|
+
console.error(`[openai] tool_call JSON parse failed for "${call.function.name}": ${parseErr.message} (${raw.length} chars, first 200: ${raw.slice(0, 200)})`)
|
|
91
|
+
args = { __parse_error: true, __raw_length: raw.length, __error: parseErr.message }
|
|
92
|
+
}
|
|
93
|
+
return {
|
|
94
|
+
id: call.id || `tc_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`,
|
|
95
|
+
name: call.function.name,
|
|
96
|
+
args
|
|
97
|
+
}
|
|
98
|
+
})
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Build system messages from structured blocks for optimal prefix caching.
|
|
102
|
+
// OpenAI auto-caches matching prefixes — stable content first, dynamic last.
|
|
103
|
+
function buildSystemMessages(system) {
|
|
104
|
+
if (!system) return []
|
|
105
|
+
if (system.blocks && Array.isArray(system.blocks)) {
|
|
106
|
+
const stable = []
|
|
107
|
+
const dynamic = []
|
|
108
|
+
for (const block of system.blocks) {
|
|
109
|
+
if (block.cacheable) stable.push(block.text)
|
|
110
|
+
else dynamic.push(block.text)
|
|
111
|
+
}
|
|
112
|
+
const msgs = []
|
|
113
|
+
if (stable.length) msgs.push({ role: "system", content: stable.join("\n\n") })
|
|
114
|
+
if (dynamic.length) msgs.push({ role: "system", content: dynamic.join("\n\n") })
|
|
115
|
+
return msgs
|
|
116
|
+
}
|
|
117
|
+
const text = typeof system === "string" ? system : system.text || String(system)
|
|
118
|
+
return text ? [{ role: "system", content: text }] : []
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function timeoutSignal(ms, parentSignal = null) {
|
|
122
|
+
const own = AbortSignal.timeout(ms)
|
|
123
|
+
if (!parentSignal) return own
|
|
124
|
+
return AbortSignal.any([parentSignal, own])
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export async function countTokensOpenAI(input) {
|
|
128
|
+
const { apiKey, baseUrl, model, system, messages, tools, timeoutMs = 10000 } = input
|
|
129
|
+
if (!apiKey) return null
|
|
130
|
+
const endpoint = `${baseUrl.replace(/\/$/, "")}/chat/completions`
|
|
131
|
+
const payload = {
|
|
132
|
+
model,
|
|
133
|
+
messages: [...buildSystemMessages(system), ...mapMessages(messages)],
|
|
134
|
+
tools: mapTools(tools),
|
|
135
|
+
max_tokens: 1,
|
|
136
|
+
stream: false
|
|
137
|
+
}
|
|
138
|
+
try {
|
|
139
|
+
const res = await fetch(endpoint, {
|
|
140
|
+
method: "POST",
|
|
141
|
+
headers: { "content-type": "application/json", authorization: `Bearer ${apiKey}` },
|
|
142
|
+
body: JSON.stringify(payload),
|
|
143
|
+
signal: AbortSignal.timeout(timeoutMs)
|
|
144
|
+
})
|
|
145
|
+
if (!res.ok) return null
|
|
146
|
+
const json = await res.json()
|
|
147
|
+
return json?.usage?.prompt_tokens ?? null
|
|
148
|
+
} catch {
|
|
149
|
+
return null
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export async function requestOpenAI(input) {
|
|
154
|
+
const { apiKey, baseUrl, model, system, messages, tools, timeoutMs = 120000, maxTokens, retry = {}, signal = null } = input
|
|
155
|
+
if (!apiKey) {
|
|
156
|
+
throw new ProviderError(`missing API key for openai provider (env: ${input.apiKeyEnv || "unknown"})`, {
|
|
157
|
+
provider: "openai"
|
|
158
|
+
})
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const payload = {
|
|
162
|
+
model,
|
|
163
|
+
messages: [...buildSystemMessages(system), ...mapMessages(messages)],
|
|
164
|
+
tools: mapTools(tools),
|
|
165
|
+
tool_choice: tools?.length ? "auto" : undefined,
|
|
166
|
+
...(maxTokens ? { max_tokens: maxTokens } : {})
|
|
167
|
+
}
|
|
168
|
+
const endpoint = `${baseUrl.replace(/\/$/, "")}/chat/completions`
|
|
169
|
+
|
|
170
|
+
return requestWithRetry({
|
|
171
|
+
attempts: Number(retry.attempts ?? 3),
|
|
172
|
+
baseDelayMs: Number(retry.baseDelayMs ?? 800),
|
|
173
|
+
signal,
|
|
174
|
+
execute: async () => {
|
|
175
|
+
const response = await fetch(endpoint, {
|
|
176
|
+
method: "POST",
|
|
177
|
+
headers: {
|
|
178
|
+
"content-type": "application/json",
|
|
179
|
+
authorization: `Bearer ${apiKey}`
|
|
180
|
+
},
|
|
181
|
+
body: JSON.stringify(payload),
|
|
182
|
+
signal: timeoutSignal(timeoutMs, signal)
|
|
183
|
+
})
|
|
184
|
+
|
|
185
|
+
if (!response.ok) {
|
|
186
|
+
const text = await response.text().catch(() => "")
|
|
187
|
+
const error = new ProviderError(`openai request failed: ${response.status} ${text}`, {
|
|
188
|
+
provider: "openai",
|
|
189
|
+
model,
|
|
190
|
+
endpoint
|
|
191
|
+
})
|
|
192
|
+
error.httpStatus = response.status
|
|
193
|
+
throw error
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const json = await response.json()
|
|
197
|
+
const message = json?.choices?.[0]?.message ?? {}
|
|
198
|
+
const promptTokens = json?.usage?.prompt_tokens ?? 0
|
|
199
|
+
const cachedTokens = json?.usage?.prompt_tokens_details?.cached_tokens ?? 0
|
|
200
|
+
const usage = {
|
|
201
|
+
input: promptTokens - cachedTokens,
|
|
202
|
+
output: json?.usage?.completion_tokens ?? 0,
|
|
203
|
+
cacheRead: cachedTokens,
|
|
204
|
+
cacheWrite: 0
|
|
205
|
+
}
|
|
206
|
+
const toolCalls = parseToolCalls(message)
|
|
207
|
+
const text = typeof message.content === "string" ? message.content : ""
|
|
208
|
+
return { text, usage, toolCalls }
|
|
209
|
+
}
|
|
210
|
+
})
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export async function* requestOpenAIStream(input) {
|
|
214
|
+
const { apiKey, baseUrl, model, system, messages, tools, timeoutMs = 120000, streamIdleTimeoutMs = 120000, maxTokens, retry = {}, signal = null } = input
|
|
215
|
+
if (!apiKey) {
|
|
216
|
+
throw new ProviderError(`missing API key for openai provider (env: ${input.apiKeyEnv || "unknown"})`, {
|
|
217
|
+
provider: "openai"
|
|
218
|
+
})
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const payload = {
|
|
222
|
+
model,
|
|
223
|
+
messages: [...buildSystemMessages(system), ...mapMessages(messages)],
|
|
224
|
+
tools: mapTools(tools),
|
|
225
|
+
tool_choice: tools?.length ? "auto" : undefined,
|
|
226
|
+
...(maxTokens ? { max_tokens: maxTokens } : {}),
|
|
227
|
+
stream: true,
|
|
228
|
+
stream_options: { include_usage: true }
|
|
229
|
+
}
|
|
230
|
+
const endpoint = `${baseUrl.replace(/\/$/, "")}/chat/completions`
|
|
231
|
+
const attempts = Number(retry.attempts ?? 3)
|
|
232
|
+
const baseDelayMs = Number(retry.baseDelayMs ?? 800)
|
|
233
|
+
|
|
234
|
+
let response
|
|
235
|
+
for (let attempt = 1; attempt <= attempts; attempt++) {
|
|
236
|
+
try {
|
|
237
|
+
// Use a connection-only timeout for the initial fetch.
|
|
238
|
+
// Once headers arrive, clear it — the SSE idle timeout handles the streaming phase.
|
|
239
|
+
const connController = new AbortController()
|
|
240
|
+
const connTimer = setTimeout(() => connController.abort(), timeoutMs)
|
|
241
|
+
const fetchSignal = signal
|
|
242
|
+
? AbortSignal.any([signal, connController.signal])
|
|
243
|
+
: connController.signal
|
|
244
|
+
|
|
245
|
+
response = await fetch(endpoint, {
|
|
246
|
+
method: "POST",
|
|
247
|
+
headers: {
|
|
248
|
+
"content-type": "application/json",
|
|
249
|
+
authorization: `Bearer ${apiKey}`
|
|
250
|
+
},
|
|
251
|
+
body: JSON.stringify(payload),
|
|
252
|
+
signal: fetchSignal
|
|
253
|
+
})
|
|
254
|
+
clearTimeout(connTimer)
|
|
255
|
+
|
|
256
|
+
if (!response.ok) {
|
|
257
|
+
const text = await response.text().catch(() => "")
|
|
258
|
+
const error = new ProviderError(`openai stream failed: ${response.status} ${text}`, {
|
|
259
|
+
provider: "openai", model, endpoint
|
|
260
|
+
})
|
|
261
|
+
error.httpStatus = response.status
|
|
262
|
+
throw error
|
|
263
|
+
}
|
|
264
|
+
break
|
|
265
|
+
} catch (err) {
|
|
266
|
+
clearTimeout(connTimer)
|
|
267
|
+
if (signal?.aborted) throw err
|
|
268
|
+
const isNetwork = err?.code === "ETIMEDOUT" || err?.code === "ECONNRESET" || err?.name === "AbortError"
|
|
269
|
+
if (!isNetwork || attempt >= attempts) throw err
|
|
270
|
+
await sleep(baseDelayMs * Math.pow(2, attempt - 1))
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
const toolBuffers = new Map()
|
|
275
|
+
let finishReason = null
|
|
276
|
+
|
|
277
|
+
for await (const { data } of parseSSE(response.body, signal, { idleTimeoutMs: streamIdleTimeoutMs })) {
|
|
278
|
+
let json
|
|
279
|
+
try { json = JSON.parse(data) } catch { continue }
|
|
280
|
+
|
|
281
|
+
if (json.usage) {
|
|
282
|
+
const pt = json.usage.prompt_tokens ?? 0
|
|
283
|
+
const ct = json.usage.prompt_tokens_details?.cached_tokens ?? 0
|
|
284
|
+
yield {
|
|
285
|
+
type: "usage",
|
|
286
|
+
usage: { input: pt - ct, output: json.usage.completion_tokens ?? 0, cacheRead: ct, cacheWrite: 0 }
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
const choice = json.choices?.[0]
|
|
291
|
+
if (choice?.finish_reason) {
|
|
292
|
+
finishReason = choice.finish_reason
|
|
293
|
+
}
|
|
294
|
+
const delta = choice?.delta
|
|
295
|
+
if (!delta) continue
|
|
296
|
+
|
|
297
|
+
if (delta.content) {
|
|
298
|
+
yield { type: "text", content: delta.content }
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
if (delta.tool_calls) {
|
|
302
|
+
for (const tc of delta.tool_calls) {
|
|
303
|
+
const idx = tc.index ?? 0
|
|
304
|
+
if (!toolBuffers.has(idx)) {
|
|
305
|
+
toolBuffers.set(idx, { id: "", name: "", argsJson: "" })
|
|
306
|
+
}
|
|
307
|
+
const buf = toolBuffers.get(idx)
|
|
308
|
+
if (tc.id) buf.id = tc.id
|
|
309
|
+
if (tc.function?.name) buf.name = tc.function.name
|
|
310
|
+
if (tc.function?.arguments) buf.argsJson += tc.function.arguments
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
for (const [, buf] of toolBuffers) {
|
|
316
|
+
const raw = buf.argsJson || "{}"
|
|
317
|
+
let args = {}
|
|
318
|
+
try {
|
|
319
|
+
args = JSON.parse(raw)
|
|
320
|
+
} catch (parseErr) {
|
|
321
|
+
console.error(`[openai] tool_call JSON parse failed for "${buf.name}": ${parseErr.message} (${raw.length} chars, first 200: ${raw.slice(0, 200)})`)
|
|
322
|
+
args = { __parse_error: true, __raw_length: raw.length, __error: parseErr.message }
|
|
323
|
+
}
|
|
324
|
+
yield {
|
|
325
|
+
type: "tool_call",
|
|
326
|
+
call: {
|
|
327
|
+
id: buf.id || `tc_${Date.now()}`,
|
|
328
|
+
name: buf.name,
|
|
329
|
+
args
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// Normalize: "stop" → "end_turn", "length" → "max_tokens", "tool_calls" → "tool_use"
|
|
335
|
+
const normalizedReason = finishReason === "length" ? "max_tokens"
|
|
336
|
+
: finishReason === "tool_calls" ? "tool_use"
|
|
337
|
+
: finishReason === "stop" ? "end_turn"
|
|
338
|
+
: finishReason || "end_turn"
|
|
339
|
+
yield { type: "stop", reason: normalizedReason }
|
|
340
|
+
}
|