minicode-ai 0.14.1 → 0.14.2

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.
Files changed (79) hide show
  1. package/README.md +1 -1
  2. package/cli/auto-update.ts +32 -2
  3. package/cli/index.ts +10 -2
  4. package/cli/setup.ts +8 -3
  5. package/cli/tui.ts +18 -15
  6. package/docs/OUTPUT_PROTOCOL_SPEC.md +6 -0
  7. package/docs/OUTPUT_RENDERING_SPEC.md +28 -4
  8. package/docs/README.md +2 -2
  9. package/docs/TERMINAL_CONTRACT.md +52 -26
  10. package/docs/getting-started.md +14 -12
  11. package/docs/glossary.md +1 -1
  12. package/docs/mcp-lsp.md +1 -1
  13. package/docs/policy-sandbox.md +7 -1
  14. package/docs/security-model.md +2 -2
  15. package/docs/security.md +7 -1
  16. package/docs/tools.md +4 -4
  17. package/docs/verify-benchmark.md +16 -0
  18. package/package.json +1 -3
  19. package/src/app/mentions.ts +4 -3
  20. package/src/app/session.ts +13 -9
  21. package/src/app/tool-layer.ts +6 -0
  22. package/src/constants.ts +17 -0
  23. package/src/lib/git-hardening.ts +27 -22
  24. package/src/lib/safe-open.ts +73 -19
  25. package/src/lib/trusted-exec.ts +7 -4
  26. package/src/mcp/client.ts +34 -9
  27. package/src/mcp/transport.ts +96 -42
  28. package/src/memory/files.ts +15 -4
  29. package/src/memory/vector.ts +5 -1
  30. package/src/policy/context.ts +21 -13
  31. package/src/policy/tool-contract.ts +4 -10
  32. package/src/policy/update-check.ts +83 -6
  33. package/src/policy/usage.ts +17 -7
  34. package/src/presentation/adapter.ts +64 -2
  35. package/src/presentation/events.ts +6 -0
  36. package/src/presentation/model.ts +1 -0
  37. package/src/presentation/projection.ts +2 -0
  38. package/src/presentation/reducer.ts +1 -0
  39. package/src/providers/anthropic.ts +52 -19
  40. package/src/providers/guards.ts +98 -13
  41. package/src/providers/responses.ts +61 -23
  42. package/src/providers/sse-bounds.ts +170 -0
  43. package/src/repo/repomap.ts +10 -11
  44. package/src/runtime/execution-backend.ts +7 -8
  45. package/src/sandbox/docker.ts +113 -9
  46. package/src/session/checkpoint.ts +9 -2
  47. package/src/session/persistence.ts +5 -2
  48. package/src/session/verification.ts +55 -16
  49. package/src/skills/loader.ts +16 -3
  50. package/src/tools/bash.ts +6 -3
  51. package/src/tools/code_run.ts +6 -5
  52. package/src/tools/context.ts +8 -0
  53. package/src/tools/delete_file.ts +2 -1
  54. package/src/tools/edit.ts +2 -1
  55. package/src/tools/git.ts +128 -54
  56. package/src/tools/glob.ts +2 -1
  57. package/src/tools/grep.ts +31 -6
  58. package/src/tools/lsp.ts +5 -4
  59. package/src/tools/mcp_call.ts +3 -1
  60. package/src/tools/memory.ts +175 -180
  61. package/src/tools/move_file.ts +2 -1
  62. package/src/tools/patch.ts +2 -1
  63. package/src/tools/read_file.ts +94 -1
  64. package/src/tools/read_image.ts +2 -1
  65. package/src/tools/task.ts +2 -1
  66. package/src/tools/todo.ts +4 -5
  67. package/src/tools/web_fetch.ts +28 -12
  68. package/src/tools/web_search.ts +28 -9
  69. package/src/tools/write_file.ts +2 -1
  70. package/src/ui/assistant/simple.ts +72 -3
  71. package/src/ui/i18n/en.ts +3 -0
  72. package/src/ui/i18n/id.ts +3 -0
  73. package/src/ui/render/width.ts +107 -79
  74. package/src/ui/runtime/screen.ts +49 -37
  75. package/src/ui/tui/app.ts +255 -205
  76. package/src/ui/tui/transcript.ts +39 -0
  77. package/CHANGELOG.md +0 -1185
  78. package/docs/PLAN_UIUX_V6.md +0 -209
  79. package/docs/changelog.md +0 -3
@@ -750,6 +750,7 @@ export function reduce(
750
750
  category: event.category,
751
751
  severity: event.severity,
752
752
  message: event.message,
753
+ ...(event.attemptId !== undefined ? { attemptId: event.attemptId } : {}),
753
754
  ...(event.cause ? { cause: event.cause } : {}),
754
755
  ...(event.action ? { action: event.action } : {}),
755
756
  }
@@ -5,6 +5,12 @@ import type { ToolSchema } from "#minicore/core/tool.ts"
5
5
  import type { Content, Message } from "#minicore/core/types.ts"
6
6
  import { LIMITS } from "../constants.ts"
7
7
  import { thinkingFamily } from "./effort.ts"
8
+ import {
9
+ appendToolArgument,
10
+ boundedSseBody,
11
+ parseProviderToolArguments,
12
+ readProviderErrorBody,
13
+ } from "./sse-bounds.ts"
8
14
 
9
15
  export interface AnthropicConfig {
10
16
  id?: string
@@ -128,13 +134,17 @@ export function createAnthropicProvider(config: AnthropicConfig): ModelProvider
128
134
  throw new ProviderError("network", e instanceof Error ? e.message : String(e))
129
135
  }
130
136
  if (!response.ok) {
131
- const txt = await response.text().catch(() => "")
137
+ const txt = await readProviderErrorBody(response).catch(() => "")
132
138
  throw toAnthropicError(response.status, txt, response.headers)
133
139
  }
134
140
  if (!response.body) throw new ProviderError("network", "empty response body")
135
141
 
136
142
  // buffer for tool inputs across deltas — per-stream (was global, now isolated)
137
- const pendingTools = new Map<number, { id: string; name: string; args: string }>()
143
+ const pendingTools = new Map<
144
+ number,
145
+ { id: string; name: string; args: string; argsBytes: number }
146
+ >()
147
+ let pendingToolArgBytes = 0
138
148
 
139
149
  // Anthropic SSE: event: ...\ndata: {...}
140
150
  let currentEvent = ""
@@ -204,7 +214,12 @@ export function createAnthropicProvider(config: AnthropicConfig): ModelProvider
204
214
  // [ST-5] Non-null guard: blok malformed tanpa id/name dikunci, bukan
205
215
  // dipaksa `block.id!`/`block.name!` yang bisa menyimpan undefined.
206
216
  if (typeof block.id !== "string" || typeof block.name !== "string") continue
207
- pendingTools.set(raw.index ?? 0, { id: block.id, name: block.name, args: "" })
217
+ pendingTools.set(raw.index ?? 0, {
218
+ id: block.id,
219
+ name: block.name,
220
+ args: "",
221
+ argsBytes: 0,
222
+ })
208
223
  }
209
224
  }
210
225
  if (evType === "content_block_delta") {
@@ -212,7 +227,18 @@ export function createAnthropicProvider(config: AnthropicConfig): ModelProvider
212
227
  if (d?.type === "input_json_delta") {
213
228
  const idx = raw.index ?? 0
214
229
  const p = pendingTools.get(idx)
215
- if (p) p.args += d.partial_json ?? ""
230
+ if (p) {
231
+ const next = appendToolArgument(
232
+ p.args,
233
+ p.argsBytes,
234
+ d.partial_json ?? "",
235
+ pendingToolArgBytes,
236
+ "Anthropic",
237
+ )
238
+ p.args = next.value
239
+ p.argsBytes = next.bytes
240
+ pendingToolArgBytes = next.pendingBytes
241
+ }
216
242
  }
217
243
  if (d?.type === "thinking_delta" && typeof d.thinking === "string") {
218
244
  yield { type: "extension", kind: "reasoning", data: { text: d.thinking } }
@@ -222,17 +248,18 @@ export function createAnthropicProvider(config: AnthropicConfig): ModelProvider
222
248
  const idx = raw.index ?? 0
223
249
  const p = pendingTools.get(idx)
224
250
  if (p) {
225
- let args: unknown = p.args
226
- try {
227
- args = p.args ? JSON.parse(p.args) : {}
228
- } catch {
229
- args = { raw: p.args }
230
- }
251
+ const args = parseProviderToolArguments(p.args, "Anthropic")
252
+ pendingToolArgBytes -= p.argsBytes
231
253
  yield { type: "tool_call", id: p.id, name: p.name, args }
232
254
  pendingTools.delete(idx)
233
255
  }
234
256
  }
235
257
  }
258
+ if (pendingTools.size > 0)
259
+ throw new ProviderError(
260
+ "invalid_request",
261
+ "Anthropic stream ended with incomplete tool arguments",
262
+ )
236
263
  },
237
264
  }
238
265
  return provider
@@ -392,15 +419,19 @@ async function* sseAnthropic(
392
419
  body: ReadableStream<Uint8Array>,
393
420
  signal: AbortSignal,
394
421
  ): AsyncGenerator<{ event: string; data: unknown }> {
395
- const reader = body.getReader()
422
+ const boundedBody = boundedSseBody(body, signal, "Anthropic")
423
+ const reader = boundedBody.getReader()
396
424
  const decoder = new TextDecoder()
397
425
  let buffer = ""
398
426
  let evt = ""
427
+ let reachedEof = false
399
428
  try {
400
429
  while (true) {
401
- if (signal.aborted) throw new DOMException("Aborted", "AbortError")
402
430
  const { done, value } = await reader.read()
403
- if (done) break
431
+ if (done) {
432
+ reachedEof = true
433
+ break
434
+ }
404
435
  buffer += decoder.decode(value, { stream: true })
405
436
  let idx = buffer.indexOf("\n")
406
437
  while (idx >= 0) {
@@ -410,18 +441,20 @@ async function* sseAnthropic(
410
441
  else if (line.startsWith("data:")) {
411
442
  const payload = line.slice(5).trim()
412
443
  if (payload) {
444
+ let data: unknown
413
445
  try {
414
- const data = JSON.parse(payload)
415
- yield { event: evt, data }
416
- } catch {}
446
+ data = JSON.parse(payload)
447
+ } catch {
448
+ throw new ProviderError("invalid_request", "Anthropic returned malformed SSE JSON")
449
+ }
450
+ yield { event: evt, data }
417
451
  }
418
- } else if (line === "") {
419
- evt = ""
420
- }
452
+ } else if (line === "") evt = ""
421
453
  idx = buffer.indexOf("\n")
422
454
  }
423
455
  }
424
456
  } finally {
457
+ if (!reachedEof) await reader.cancel().catch(() => {})
425
458
  reader.releaseLock()
426
459
  }
427
460
  }
@@ -10,19 +10,30 @@
10
10
  // Abort parent diteruskan apa adanya (tak pernah ditelan/dikonversi); hanya
11
11
  // timeout MILIK guard yang diklasifikasikan ulang menjadi ProviderError
12
12
  // "server" agar tak dikira network-putus atau abort-user.
13
+ import { Buffer } from "node:buffer"
13
14
  import { abortError, ProviderError } from "#minicore/core/errors.ts"
14
15
  import type { ModelProvider, ProviderEvent, StreamRequest } from "#minicore/core/provider.ts"
15
16
  import { LIMITS } from "../constants.ts"
16
17
 
17
18
  export function withStreamGuards(
18
19
  provider: ModelProvider,
19
- opts: { timeoutMs?: number; maxTextChars?: number; maxReasonChars?: number } = {},
20
+ opts: {
21
+ timeoutMs?: number
22
+ maxTextChars?: number
23
+ maxReasonChars?: number
24
+ maxToolArgBytes?: number
25
+ maxTotalToolArgBytes?: number
26
+ } = {},
20
27
  ): ModelProvider {
21
28
  const timeoutMs = opts.timeoutMs ?? LIMITS.PROVIDER_REQUEST_TIMEOUT_MS
22
29
  const maxTextChars = opts.maxTextChars ?? LIMITS.PROVIDER_TEXT_MAX_CHARS
23
30
  // Reasoning = bukan bagian jawaban; cap agresif (2MB logis ≈ 500k token —
24
31
  // jauh di atas reasoning sah mana pun, jauh di bawah OOM).
25
32
  const maxReasonChars = opts.maxReasonChars ?? LIMITS.PROVIDER_REASON_MAX_CHARS
33
+ const maxToolArgBytes = opts.maxToolArgBytes ?? LIMITS.PROVIDER_TOOL_ARGS_MAX_BYTES
34
+ const maxTotalToolArgBytes =
35
+ opts.maxTotalToolArgBytes ?? LIMITS.PROVIDER_TOOL_ARGS_PENDING_MAX_BYTES
36
+ let nextAttemptId = 0
26
37
  return {
27
38
  ...provider,
28
39
  id: provider.id,
@@ -31,27 +42,76 @@ export function withStreamGuards(
31
42
  },
32
43
  async *stream(request: StreamRequest, signal: AbortSignal): AsyncIterable<ProviderEvent> {
33
44
  if (signal.aborted) throw abortError(signal)
45
+ const attemptId = ++nextAttemptId
46
+ const attemptEvent = (
47
+ phase: "started" | "completed" | "invalidated",
48
+ fields: { hadVisibleOutput?: boolean; reason?: string } = {},
49
+ ): ProviderEvent => ({
50
+ type: "extension",
51
+ kind: "minicode:attempt",
52
+ data: { phase, attemptId, ...fields },
53
+ })
54
+ yield attemptEvent("started")
34
55
  const ctl = new AbortController()
35
56
  const onParentAbort = () => ctl.abort(signal.reason)
36
57
  if (signal.aborted) ctl.abort(signal.reason)
37
58
  else signal.addEventListener("abort", onParentAbort, { once: true })
38
- const timer = setTimeout(
39
- () => ctl.abort(new Error(`provider request timed out after ${timeoutMs}ms`)),
40
- timeoutMs,
41
- )
59
+ let timedOut = false
60
+ const timer = setTimeout(() => {
61
+ timedOut = true
62
+ ctl.abort(new Error(`provider request timed out after ${timeoutMs}ms`))
63
+ }, timeoutMs)
42
64
  // Jangan menahan exit proses bila turn sudah selesai dari sisi lain.
43
65
  ;(timer as unknown as { unref?: () => void }).unref?.()
44
- let timedOut = false
45
- const onLinkedAbort = () => {
46
- if (!signal.aborted) timedOut = true
47
- }
48
- ctl.signal.addEventListener("abort", onLinkedAbort, { once: true })
49
66
  let textChars = 0
50
67
  let reasonChars = 0
68
+ let toolArgumentBytes = 0
51
69
  let marked = false
52
70
  let reasonMarked = false
71
+ let finishReason: "stop" | "tool_calls" | "length" | "abort" | "error" | undefined
72
+ let hadVisibleOutput = false
73
+ let invalidated = false
53
74
  try {
54
75
  for await (const ev of provider.stream(request, ctl.signal)) {
76
+ if (
77
+ finishReason !== undefined &&
78
+ (ev.type === "text" || ev.type === "tool_call" || ev.type === "finish")
79
+ ) {
80
+ throw new ProviderError("network", "provider emitted content after finish")
81
+ }
82
+ if (ev.type === "finish") {
83
+ finishReason = ev.reason
84
+ if (ev.reason !== "stop" && ev.reason !== "tool_calls") {
85
+ invalidated = true
86
+ yield attemptEvent("invalidated", {
87
+ hadVisibleOutput,
88
+ reason: `finish:${ev.reason}`,
89
+ })
90
+ }
91
+ yield ev
92
+ continue
93
+ }
94
+ if (ev.type === "tool_call") {
95
+ let serialized: string
96
+ try {
97
+ serialized = JSON.stringify(ev.args ?? {}) ?? ""
98
+ } catch {
99
+ throw new ProviderError(
100
+ "invalid_request",
101
+ "provider returned unserializable tool arguments",
102
+ )
103
+ }
104
+ const argBytes = Buffer.byteLength(serialized, "utf8")
105
+ toolArgumentBytes += argBytes
106
+ if (argBytes > maxToolArgBytes || toolArgumentBytes > maxTotalToolArgBytes) {
107
+ const error = new ProviderError(
108
+ "invalid_request",
109
+ "provider tool arguments exceed configured byte limits",
110
+ )
111
+ ctl.abort(error)
112
+ throw error
113
+ }
114
+ }
55
115
  // F-16 diperluas (investigasi Phase 5): extension "reasoning" juga
56
116
  // diakumulasi kernel (loop `reasoning += ev.data.text` TANPA cap) —
57
117
  // dulu hanya ev.type "text" yang dipotong, sehingga reasoning stream
@@ -64,13 +124,18 @@ export function withStreamGuards(
64
124
  textChars += ev.text.length
65
125
  if (textChars > maxTextChars) {
66
126
  const keep = ev.text.slice(0, ev.text.length - (textChars - maxTextChars))
67
- if (keep) yield { type: "text", text: keep }
127
+ if (keep) {
128
+ hadVisibleOutput = true
129
+ yield { type: "text", text: keep }
130
+ }
68
131
  if (!marked) {
69
132
  marked = true
133
+ hadVisibleOutput = true
70
134
  yield { type: "text", text: "\n… [provider text truncated: turn cap exceeded]" }
71
135
  }
72
136
  continue
73
137
  }
138
+ hadVisibleOutput = true
74
139
  } else if (ev.type === "extension" && ev.kind === "reasoning") {
75
140
  const d = ev.data as { text?: unknown } | undefined
76
141
  if (typeof d?.text === "string" && d.text) {
@@ -78,9 +143,13 @@ export function withStreamGuards(
78
143
  reasonChars += d.text.length
79
144
  if (reasonChars > maxReasonChars) {
80
145
  const keep = d.text.slice(0, d.text.length - (reasonChars - maxReasonChars))
81
- if (keep) yield { type: "extension", kind: "reasoning", data: { text: keep } }
146
+ if (keep) {
147
+ hadVisibleOutput = true
148
+ yield { type: "extension", kind: "reasoning", data: { text: keep } }
149
+ }
82
150
  if (!reasonMarked) {
83
151
  reasonMarked = true
152
+ hadVisibleOutput = true
84
153
  yield {
85
154
  type: "extension",
86
155
  kind: "reasoning",
@@ -89,18 +158,34 @@ export function withStreamGuards(
89
158
  }
90
159
  continue
91
160
  }
161
+ hadVisibleOutput = true
92
162
  }
93
163
  }
94
164
  yield ev
95
165
  }
166
+ if (finishReason === "stop" || finishReason === "tool_calls") {
167
+ yield attemptEvent("completed", { hadVisibleOutput })
168
+ } else if (!invalidated) {
169
+ invalidated = true
170
+ yield attemptEvent("invalidated", {
171
+ hadVisibleOutput,
172
+ reason: "missing-finish",
173
+ })
174
+ }
96
175
  } catch (e) {
176
+ if (!invalidated) {
177
+ invalidated = true
178
+ yield attemptEvent("invalidated", {
179
+ hadVisibleOutput,
180
+ reason: timedOut ? "timeout" : "stream-error",
181
+ })
182
+ }
97
183
  if (timedOut)
98
184
  throw new ProviderError("server", `provider request timed out after ${timeoutMs}ms`)
99
185
  throw e
100
186
  } finally {
101
187
  clearTimeout(timer)
102
188
  signal.removeEventListener("abort", onParentAbort)
103
- ctl.signal.removeEventListener("abort", onLinkedAbort)
104
189
  }
105
190
  },
106
191
  }
@@ -1,6 +1,12 @@
1
1
  import { ProviderError } from "#minicore/core/errors.ts"
2
2
  import type { ModelProvider, ProviderEvent, StreamRequest } from "#minicore/core/provider.ts"
3
3
  import { allowedEfforts, thinkingFamily } from "./effort.ts"
4
+ import {
5
+ appendToolArgument,
6
+ boundedSseBody,
7
+ parseProviderToolArguments,
8
+ readProviderErrorBody,
9
+ } from "./sse-bounds.ts"
4
10
 
5
11
  export interface ResponsesConfig {
6
12
  id?: string
@@ -131,7 +137,7 @@ export function createResponsesProvider(config: ResponsesConfig): ModelProvider
131
137
  throw new ProviderError("network", e instanceof Error ? e.message : String(e))
132
138
  }
133
139
  if (!res.ok) {
134
- const txt = await res.text().catch(() => "")
140
+ const txt = await readProviderErrorBody(res).catch(() => "")
135
141
  const retryAfter = res.headers.get("retry-after")
136
142
  if (res.status === 429) {
137
143
  const ms = retryAfter ? Number(retryAfter) * 1000 : undefined
@@ -169,7 +175,8 @@ export function createResponsesProvider(config: ResponsesConfig): ModelProvider
169
175
  }
170
176
  if (!res.body) throw new ProviderError("network", "empty response body")
171
177
  // Simplified SSE: forward text deltas
172
- const reader = res.body.getReader()
178
+ const boundedBody = boundedSseBody(res.body, signal, "Responses")
179
+ const reader = boundedBody.getReader()
173
180
  const decoder = new TextDecoder()
174
181
  let buf = ""
175
182
  // F-01: akumulasi function_call per item (Responses tidak punya delta
@@ -177,16 +184,12 @@ export function createResponsesProvider(config: ResponsesConfig): ModelProvider
177
184
  // response.function_call_arguments.delta lalu item final via
178
185
  // response.output_item.done). Tanpa ini tool yang dideklarasikan di
179
186
  // request tidak pernah dieksekusi — agen tampak selesai padahal buta.
180
- const pendingCalls = new Map<string, { id: string; name: string; args: string }>()
187
+ const pendingCalls = new Map<
188
+ string,
189
+ { id: string; name: string; args: string; argsBytes: number }
190
+ >()
191
+ let pendingToolArgBytes = 0
181
192
  let finished = false
182
- const parseCallArgs = (raw: string): unknown => {
183
- if (!raw) return {}
184
- try {
185
- return JSON.parse(raw)
186
- } catch {
187
- return { raw }
188
- }
189
- }
190
193
  const yieldCall = function* (
191
194
  id: string,
192
195
  name: string,
@@ -197,13 +200,22 @@ export function createResponsesProvider(config: ResponsesConfig): ModelProvider
197
200
  // eksekusi ganda diam-diam — gagal keras sebagai network retryable.
198
201
  throw new ProviderError("network", "tool call after finish")
199
202
  }
200
- yield { type: "tool_call", id: id || name, name, args: parseCallArgs(argsRaw) }
203
+ yield {
204
+ type: "tool_call",
205
+ id: id || name,
206
+ name,
207
+ args: parseProviderToolArguments(argsRaw, "Responses"),
208
+ }
201
209
  }
210
+ let reachedEof = false
202
211
  try {
203
212
  while (true) {
204
213
  if (signal.aborted) throw new DOMException("Aborted", "AbortError")
205
214
  const { done, value } = await reader.read()
206
- if (done) break
215
+ if (done) {
216
+ reachedEof = true
217
+ break
218
+ }
207
219
  buf += decoder.decode(value, { stream: true })
208
220
  let idx = buf.indexOf("\n")
209
221
  while (idx >= 0) {
@@ -219,20 +231,22 @@ export function createResponsesProvider(config: ResponsesConfig): ModelProvider
219
231
  yield* yieldCall(acc.id, acc.name, acc.args)
220
232
  }
221
233
  pendingCalls.clear()
234
+ pendingToolArgBytes = 0
222
235
  if (!finished) {
223
236
  finished = true
224
237
  yield { type: "finish", reason: "stop" }
225
238
  }
226
239
  return
227
240
  }
228
- // Hanya JSON rusak yang di-skip di sini: error dari yieldCall
229
- // (mis. tool-setelah-finish) HARUS merambat ke kernel, jangan
230
- // sampai tertelan catch di bawah.
241
+ if (!payload) {
242
+ idx = buf.indexOf("\n")
243
+ continue
244
+ }
231
245
  let data: Record<string, unknown>
232
246
  try {
233
247
  data = JSON.parse(payload) as Record<string, unknown>
234
248
  } catch {
235
- continue
249
+ throw new ProviderError("invalid_request", "Responses returned malformed SSE JSON")
236
250
  }
237
251
  // response.completed → simpan id untuk chaining turn berikut.
238
252
  // Format nyata: {type:"response.completed", response:{id:"resp_…"}}.
@@ -300,10 +314,22 @@ export function createResponsesProvider(config: ResponsesConfig): ModelProvider
300
314
  ? item.call_id
301
315
  : `item_${pendingCalls.size}`
302
316
  const cid = typeof item.call_id === "string" && item.call_id ? item.call_id : key
317
+ const prior = pendingCalls.get(key)
318
+ if (prior) pendingToolArgBytes -= prior.argsBytes
319
+ const initialArgs = typeof item.arguments === "string" ? item.arguments : ""
320
+ const boundedArgs = appendToolArgument(
321
+ "",
322
+ 0,
323
+ initialArgs,
324
+ pendingToolArgBytes,
325
+ "Responses",
326
+ )
327
+ pendingToolArgBytes = boundedArgs.pendingBytes
303
328
  pendingCalls.set(key, {
304
329
  id: cid,
305
330
  name: typeof item.name === "string" ? item.name : "",
306
- args: typeof item.arguments === "string" ? item.arguments : "",
331
+ args: boundedArgs.value,
332
+ argsBytes: boundedArgs.bytes,
307
333
  })
308
334
  } else if (dtype === "response.function_call_arguments.delta") {
309
335
  const key =
@@ -311,7 +337,18 @@ export function createResponsesProvider(config: ResponsesConfig): ModelProvider
311
337
  ? data.item_id
312
338
  : ([...pendingCalls.keys()].pop() ?? "")
313
339
  const acc = pendingCalls.get(key)
314
- if (acc && typeof data.delta === "string") acc.args += data.delta
340
+ if (acc && typeof data.delta === "string") {
341
+ const boundedArgs = appendToolArgument(
342
+ acc.args,
343
+ acc.argsBytes,
344
+ data.delta,
345
+ pendingToolArgBytes,
346
+ "Responses",
347
+ )
348
+ acc.args = boundedArgs.value
349
+ acc.argsBytes = boundedArgs.bytes
350
+ pendingToolArgBytes = boundedArgs.pendingBytes
351
+ }
315
352
  } else if (dtype === "response.output_item.done" && item?.type === "function_call") {
316
353
  const key =
317
354
  typeof item.id === "string" && pendingCalls.has(item.id)
@@ -323,6 +360,7 @@ export function createResponsesProvider(config: ResponsesConfig): ModelProvider
323
360
  const argsRaw =
324
361
  (typeof item.arguments === "string" && item.arguments) || acc?.args || ""
325
362
  pendingCalls.delete(key)
363
+ if (acc) pendingToolArgBytes -= acc.argsBytes
326
364
  if (name) yield* yieldCall(id, name, argsRaw)
327
365
  } else if (data.type === "function_call" && typeof data.name === "string") {
328
366
  const id =
@@ -364,16 +402,16 @@ export function createResponsesProvider(config: ResponsesConfig): ModelProvider
364
402
  }
365
403
  }
366
404
  } finally {
405
+ if (!reachedEof) await reader.cancel().catch(() => {})
367
406
  reader.releaseLock()
368
407
  }
369
- // Provider quirky bisa berakhir tanpa finish eksplisit: flush sisa
370
- // function_call yang terakumulasi (argumen parsial → {raw}, ditolak
371
- // validateArgs sebagai observasi error — aman), lalu finish stop agar
372
- // kernel tidak menganggap stream terputus.
408
+ // Gateway yang menyelesaikan stream tanpa output_item.done tetap flush
409
+ // item tertahan; parser sudah membatasi dan memvalidasi JSON argumennya.
373
410
  for (const [, acc] of pendingCalls) {
374
411
  yield* yieldCall(acc.id, acc.name, acc.args)
375
412
  }
376
413
  pendingCalls.clear()
414
+ pendingToolArgBytes = 0
377
415
  if (!finished) yield { type: "finish", reason: "stop" }
378
416
  },
379
417
  }
@@ -0,0 +1,170 @@
1
+ import { Buffer } from "node:buffer"
2
+ import { abortError, ProviderError } from "#minicore/core/errors.ts"
3
+ import { LIMITS } from "../constants.ts"
4
+
5
+ export interface SseBounds {
6
+ maxFrameBytes?: number
7
+ maxTotalBytes?: number
8
+ }
9
+
10
+ export interface ToolArgumentBounds {
11
+ maxArgumentBytes?: number
12
+ maxPendingBytes?: number
13
+ }
14
+
15
+ /** Baca stream per chunk dengan batas byte sebelum parser SSE membangun buffer baris. */
16
+ export function boundedSseBody(
17
+ body: ReadableStream<Uint8Array>,
18
+ signal: AbortSignal,
19
+ provider: string,
20
+ bounds: SseBounds = {},
21
+ ): ReadableStream<Uint8Array> {
22
+ const maxFrameBytes = bounds.maxFrameBytes ?? LIMITS.PROVIDER_SSE_FRAME_MAX_BYTES
23
+ const maxTotalBytes = bounds.maxTotalBytes ?? LIMITS.PROVIDER_SSE_TOTAL_MAX_BYTES
24
+ if (signal.aborted) throw abortError(signal)
25
+ const reader = body.getReader()
26
+ let streamBytes = 0
27
+ let lineBytes = 0
28
+ let frameBytes = 0
29
+ let reachedEof = false
30
+ let released = false
31
+ const release = () => {
32
+ if (released) return
33
+ released = true
34
+ signal.removeEventListener("abort", onAbort)
35
+ reader.releaseLock()
36
+ }
37
+ const onAbort = () => {
38
+ void reader.cancel(signal.reason).catch(() => {})
39
+ }
40
+ signal.addEventListener("abort", onAbort, { once: true })
41
+ const cancelSource = async (reason?: unknown) => {
42
+ if (!reachedEof) await reader.cancel(reason).catch(() => {})
43
+ release()
44
+ }
45
+ return new ReadableStream<Uint8Array>({
46
+ async pull(controller) {
47
+ try {
48
+ if (signal.aborted) throw abortError(signal)
49
+ const { done, value } = await reader.read()
50
+ if (signal.aborted) throw abortError(signal)
51
+ if (done) {
52
+ reachedEof = true
53
+ if (lineBytes !== 0 || frameBytes !== 0) {
54
+ throw new ProviderError(
55
+ "invalid_request",
56
+ `${provider} returned an incomplete SSE frame`,
57
+ )
58
+ }
59
+ controller.close()
60
+ release()
61
+ return
62
+ }
63
+ streamBytes += value.byteLength
64
+ if (streamBytes > maxTotalBytes) {
65
+ throw new ProviderError(
66
+ "invalid_request",
67
+ `${provider} SSE stream exceeds ${maxTotalBytes} bytes`,
68
+ )
69
+ }
70
+ for (const byte of value) {
71
+ if (byte === 0x0a) {
72
+ frameBytes++
73
+ if (frameBytes > maxFrameBytes) {
74
+ throw new ProviderError(
75
+ "invalid_request",
76
+ `${provider} SSE frame exceeds ${maxFrameBytes} bytes`,
77
+ )
78
+ }
79
+ if (lineBytes === 0) frameBytes = 0
80
+ lineBytes = 0
81
+ } else {
82
+ lineBytes++
83
+ frameBytes++
84
+ if (lineBytes > maxFrameBytes || frameBytes > maxFrameBytes) {
85
+ throw new ProviderError(
86
+ "invalid_request",
87
+ `${provider} SSE frame exceeds ${maxFrameBytes} bytes`,
88
+ )
89
+ }
90
+ }
91
+ }
92
+ controller.enqueue(value)
93
+ } catch (error) {
94
+ await cancelSource(error)
95
+ controller.error(error)
96
+ }
97
+ },
98
+ cancel: cancelSource,
99
+ })
100
+ }
101
+
102
+ /** Body error dibaca secukupnya untuk kategori/pesan, lalu sisa stream dibuang. */
103
+ export async function readProviderErrorBody(response: Response): Promise<string> {
104
+ if (!response.body) return ""
105
+ const reader = response.body.getReader()
106
+ const chunks: Uint8Array[] = []
107
+ let size = 0
108
+ let reachedEof = false
109
+ try {
110
+ while (size < LIMITS.PROVIDER_ERROR_BODY_MAX_BYTES) {
111
+ const { done, value } = await reader.read()
112
+ if (done) {
113
+ reachedEof = true
114
+ break
115
+ }
116
+ const remaining = LIMITS.PROVIDER_ERROR_BODY_MAX_BYTES - size
117
+ const part = value.byteLength > remaining ? value.subarray(0, remaining) : value
118
+ chunks.push(part)
119
+ size += part.byteLength
120
+ if (part.byteLength < value.byteLength) break
121
+ }
122
+ return Buffer.concat(chunks.map((chunk) => Buffer.from(chunk))).toString("utf8")
123
+ } finally {
124
+ if (!reachedEof) await reader.cancel().catch(() => {})
125
+ reader.releaseLock()
126
+ }
127
+ }
128
+
129
+ export function appendToolArgument(
130
+ current: string,
131
+ currentBytes: number,
132
+ delta: string,
133
+ pendingBytes: number,
134
+ provider: string,
135
+ bounds: ToolArgumentBounds = {},
136
+ ): { value: string; bytes: number; pendingBytes: number } {
137
+ const deltaBytes = Buffer.byteLength(delta, "utf8")
138
+ const bytes = currentBytes + deltaBytes
139
+ const nextPendingBytes = pendingBytes + deltaBytes
140
+ const maxArgumentBytes = bounds.maxArgumentBytes ?? LIMITS.PROVIDER_TOOL_ARGS_MAX_BYTES
141
+ const maxPendingBytes = bounds.maxPendingBytes ?? LIMITS.PROVIDER_TOOL_ARGS_PENDING_MAX_BYTES
142
+ if (bytes > maxArgumentBytes) {
143
+ throw new ProviderError(
144
+ "invalid_request",
145
+ `${provider} tool arguments exceed ${maxArgumentBytes} bytes`,
146
+ )
147
+ }
148
+ if (nextPendingBytes > maxPendingBytes) {
149
+ throw new ProviderError(
150
+ "invalid_request",
151
+ `${provider} pending tool arguments exceed ${maxPendingBytes} bytes`,
152
+ )
153
+ }
154
+ return { value: current + delta, bytes, pendingBytes: nextPendingBytes }
155
+ }
156
+
157
+ export function parseProviderToolArguments(raw: string, provider: string): unknown {
158
+ if (Buffer.byteLength(raw, "utf8") > LIMITS.PROVIDER_TOOL_ARGS_MAX_BYTES) {
159
+ throw new ProviderError(
160
+ "invalid_request",
161
+ `${provider} tool arguments exceed ${LIMITS.PROVIDER_TOOL_ARGS_MAX_BYTES} bytes`,
162
+ )
163
+ }
164
+ if (!raw) return {}
165
+ try {
166
+ return JSON.parse(raw)
167
+ } catch {
168
+ throw new ProviderError("invalid_request", `${provider} returned malformed tool arguments`)
169
+ }
170
+ }