@lumenflow/cli 3.17.6 → 3.18.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.
Files changed (62) hide show
  1. package/dist/init-detection.js +5 -3
  2. package/dist/init-detection.js.map +1 -1
  3. package/dist/init-docs-scaffolder.js +96 -0
  4. package/dist/init-docs-scaffolder.js.map +1 -0
  5. package/dist/init-package-config.js +135 -0
  6. package/dist/init-package-config.js.map +1 -0
  7. package/dist/init-safety-scripts.js +129 -0
  8. package/dist/init-safety-scripts.js.map +1 -0
  9. package/dist/init-templates.js +4 -4
  10. package/dist/init-templates.js.map +1 -1
  11. package/dist/init.js +13 -302
  12. package/dist/init.js.map +1 -1
  13. package/dist/initiative-plan.js +1 -1
  14. package/dist/initiative-plan.js.map +1 -1
  15. package/dist/onboarding-template-paths.js +0 -1
  16. package/dist/onboarding-template-paths.js.map +1 -1
  17. package/dist/pre-commit-check.js +1 -1
  18. package/dist/pre-commit-check.js.map +1 -1
  19. package/dist/wu-done.js +389 -423
  20. package/dist/wu-done.js.map +1 -1
  21. package/dist/wu-edit-operations.js +4 -0
  22. package/dist/wu-edit-operations.js.map +1 -1
  23. package/dist/wu-edit-validators.js +4 -0
  24. package/dist/wu-edit-validators.js.map +1 -1
  25. package/dist/wu-edit.js +11 -0
  26. package/dist/wu-edit.js.map +1 -1
  27. package/dist/wu-spawn-strategy-resolver.js +13 -1
  28. package/dist/wu-spawn-strategy-resolver.js.map +1 -1
  29. package/package.json +8 -8
  30. package/packs/agent-runtime/.turbo/turbo-build.log +4 -0
  31. package/packs/agent-runtime/README.md +147 -0
  32. package/packs/agent-runtime/capability-factory.ts +104 -0
  33. package/packs/agent-runtime/config.schema.json +87 -0
  34. package/packs/agent-runtime/constants.ts +21 -0
  35. package/packs/agent-runtime/index.ts +11 -0
  36. package/packs/agent-runtime/manifest.ts +207 -0
  37. package/packs/agent-runtime/manifest.yaml +193 -0
  38. package/packs/agent-runtime/orchestration.ts +1787 -0
  39. package/packs/agent-runtime/pack-registration.ts +110 -0
  40. package/packs/agent-runtime/package.json +57 -0
  41. package/packs/agent-runtime/policy-factory.ts +165 -0
  42. package/packs/agent-runtime/tool-impl/agent-turn-tools.ts +793 -0
  43. package/packs/agent-runtime/tool-impl/index.ts +5 -0
  44. package/packs/agent-runtime/tool-impl/provider-adapters.ts +1245 -0
  45. package/packs/agent-runtime/tools/index.ts +4 -0
  46. package/packs/agent-runtime/tools/types.ts +47 -0
  47. package/packs/agent-runtime/tsconfig.json +20 -0
  48. package/packs/agent-runtime/types.ts +128 -0
  49. package/packs/agent-runtime/vitest.config.ts +11 -0
  50. package/packs/sidekick/.turbo/turbo-build.log +1 -1
  51. package/packs/sidekick/package.json +1 -1
  52. package/packs/sidekick/vitest.config.ts +11 -0
  53. package/packs/software-delivery/.turbo/turbo-build.log +1 -1
  54. package/packs/software-delivery/package.json +1 -1
  55. package/packs/software-delivery/vitest.config.ts +11 -0
  56. package/templates/core/.lumenflow/rules/wu-workflow.md.template +1 -1
  57. package/templates/core/LUMENFLOW.md.template +2 -2
  58. package/templates/core/ai/onboarding/first-wu-mistakes.md.template +2 -2
  59. package/templates/core/ai/onboarding/quick-ref-commands.md.template +1 -1
  60. package/templates/core/ai/onboarding/starting-prompt.md.template +1 -1
  61. package/templates/vendors/claude/.claude/skills/frontend-design/SKILL.md.template +1 -1
  62. package/templates/core/ai/onboarding/wu-sizing-guide.md.template +0 -84
@@ -0,0 +1,1245 @@
1
+ // Copyright (c) 2026 Hellmai Ltd
2
+ // SPDX-License-Identifier: AGPL-3.0-only
3
+
4
+ import {
5
+ AGENT_RUNTIME_API_KEY_ENV,
6
+ AGENT_RUNTIME_BASE_URL_ENV,
7
+ AGENT_RUNTIME_STATIC_PROVIDER_ALLOWLIST,
8
+ AGENT_RUNTIME_STATIC_PROVIDER_URLS,
9
+ } from '../constants.js';
10
+ import {
11
+ AGENT_RUNTIME_PROVIDER_KINDS,
12
+ AGENT_RUNTIME_TURN_STATUSES,
13
+ type AgentRuntimeExecuteTurnOutput,
14
+ type AgentRuntimeIntentCatalogEntry,
15
+ type AgentRuntimeMessage,
16
+ type AgentRuntimeProviderKind,
17
+ type AgentRuntimeRequestedTool,
18
+ type AgentRuntimeStreamSnapshot,
19
+ type AgentRuntimeToolCatalogEntry,
20
+ type AgentRuntimeTurnStatus,
21
+ } from '../types.js';
22
+
23
+ const REQUEST_METHOD_POST = 'POST';
24
+ const HEADER_AUTHORIZATION = 'authorization';
25
+ const HEADER_CONTENT_TYPE = 'content-type';
26
+ const CONTENT_TYPE_JSON = 'application/json';
27
+ const HTTP_STATUS_UNAUTHORIZED = 401;
28
+ const HTTP_STATUS_RATE_LIMITED = 429;
29
+ const RESPONSE_FORMAT_TYPE = 'json_object';
30
+ const DEFAULT_FINISH_REASON = 'stop';
31
+ const DEFAULT_ASSISTANT_MESSAGE = '';
32
+ const RESPONSE_MODE_NON_STREAMING = 'non_streaming';
33
+ const RESPONSE_MODE_STREAMING = 'streaming';
34
+ const STREAM_DONE_SENTINEL = '[DONE]';
35
+ const STREAM_LINE_PREFIX = 'data:';
36
+ const STREAM_DELIMITER = '\n\n';
37
+ const FIXTURE_TOOL_NAME = 'calendar:create-event';
38
+ const FIXTURE_INPUT_TOKEN_KEY = 'prompt_tokens';
39
+ const FIXTURE_OUTPUT_TOKEN_KEY = 'completion_tokens';
40
+ const FIXTURE_TOTAL_TOKEN_KEY = 'total_tokens';
41
+ const MESSAGES_RESPONSE_KIND = 'message';
42
+ const MESSAGES_CONTENT_KIND_TEXT = 'text';
43
+ const MESSAGES_CONTENT_KIND_TOOL_USE = 'tool_use';
44
+
45
+ const PROVIDER_ERROR_CODES = {
46
+ AUTHENTICATION_FAILED: 'PROVIDER_AUTHENTICATION_FAILED',
47
+ HTTP_ERROR: 'PROVIDER_HTTP_ERROR',
48
+ MALFORMED_RESPONSE: 'PROVIDER_MALFORMED_RESPONSE',
49
+ RATE_LIMITED: 'PROVIDER_RATE_LIMITED',
50
+ TRANSPORT_ERROR: 'PROVIDER_TRANSPORT_ERROR',
51
+ UNSUPPORTED_PROVIDER: 'PROVIDER_UNSUPPORTED',
52
+ } as const;
53
+
54
+ const CONFORMANCE_SCENARIOS = {
55
+ SUCCESS: 'success',
56
+ TOOL_REQUEST_SHAPING: 'tool_request_shaping',
57
+ MALFORMED_RESPONSE: 'malformed_response',
58
+ NORMALIZED_ERROR: 'normalized_error',
59
+ } as const;
60
+
61
+ export interface ProviderCapabilityBaseline {
62
+ kind: AgentRuntimeProviderKind;
63
+ required_env: readonly string[];
64
+ network_allowlist: readonly string[];
65
+ allowed_urls: readonly string[];
66
+ }
67
+
68
+ export interface ProviderTurnRequest {
69
+ kind: AgentRuntimeProviderKind;
70
+ model: string;
71
+ url: string;
72
+ apiKey: string;
73
+ stream?: boolean;
74
+ messages: readonly AgentRuntimeMessage[];
75
+ toolCatalog: readonly AgentRuntimeToolCatalogEntry[];
76
+ intentCatalog: readonly AgentRuntimeIntentCatalogEntry[];
77
+ }
78
+
79
+ export interface ProviderTurnMetadata {
80
+ provider_kind: ProviderCapabilityBaseline['kind'];
81
+ request_url: string;
82
+ response_status?: number;
83
+ response_mode?: typeof RESPONSE_MODE_NON_STREAMING | typeof RESPONSE_MODE_STREAMING;
84
+ stream_snapshot_count?: number;
85
+ }
86
+
87
+ export interface ProviderTurnError {
88
+ code: string;
89
+ message: string;
90
+ details?: Record<string, unknown>;
91
+ }
92
+
93
+ export interface ProviderTurnSuccess {
94
+ ok: true;
95
+ output: AgentRuntimeExecuteTurnOutput;
96
+ metadata: ProviderTurnMetadata;
97
+ stream_snapshots?: readonly AgentRuntimeStreamSnapshot[];
98
+ }
99
+
100
+ export interface ProviderTurnFailure {
101
+ ok: false;
102
+ error: ProviderTurnError;
103
+ metadata: ProviderTurnMetadata;
104
+ }
105
+
106
+ export type ProviderTurnResult = ProviderTurnSuccess | ProviderTurnFailure;
107
+
108
+ export interface ProviderAdapterConformanceResult {
109
+ scenario: string;
110
+ passed: boolean;
111
+ normalized_output?: AgentRuntimeExecuteTurnOutput;
112
+ normalized_error?: ProviderTurnError;
113
+ }
114
+
115
+ export type ProviderTransport = (
116
+ url: string,
117
+ init: RequestInit,
118
+ ) => Promise<Pick<Response, 'ok' | 'status' | 'text' | 'body'>>;
119
+
120
+ export const STATIC_PROVIDER_CAPABILITY_BASELINE: ProviderCapabilityBaseline = {
121
+ kind: AGENT_RUNTIME_PROVIDER_KINDS.OPENAI_COMPATIBLE,
122
+ required_env: [AGENT_RUNTIME_API_KEY_ENV, AGENT_RUNTIME_BASE_URL_ENV],
123
+ network_allowlist: AGENT_RUNTIME_STATIC_PROVIDER_ALLOWLIST,
124
+ allowed_urls: AGENT_RUNTIME_STATIC_PROVIDER_URLS,
125
+ };
126
+
127
+ export function listStaticProviderCapabilityBaselines(): readonly ProviderCapabilityBaseline[] {
128
+ return [STATIC_PROVIDER_CAPABILITY_BASELINE];
129
+ }
130
+
131
+ export function validateNormalizedTurnOutput(
132
+ value: unknown,
133
+ ): { ok: true; value: AgentRuntimeExecuteTurnOutput } | { ok: false; error: ProviderTurnError } {
134
+ if (!isRecord(value)) {
135
+ return {
136
+ ok: false,
137
+ error: createMalformedResponseError(
138
+ 'Provider normalization did not produce an object. Ensure the adapter returns the governed turn shape.',
139
+ ),
140
+ };
141
+ }
142
+
143
+ const status = normalizeTurnStatus(value.status, value.requested_tool);
144
+ if (!status) {
145
+ return {
146
+ ok: false,
147
+ error: createMalformedResponseError(
148
+ 'Provider normalization returned an invalid status. Use reply, tool_request, complete, or escalate.',
149
+ ),
150
+ };
151
+ }
152
+
153
+ const intent = asNonEmptyString(value.intent);
154
+ if (!intent) {
155
+ return {
156
+ ok: false,
157
+ error: createMalformedResponseError(
158
+ 'Provider normalization returned no intent string. Ensure the model emits an intent field.',
159
+ ),
160
+ };
161
+ }
162
+
163
+ if (typeof value.assistant_message !== 'string') {
164
+ return {
165
+ ok: false,
166
+ error: createMalformedResponseError(
167
+ 'Provider normalization returned a non-string assistant_message. Ensure the model emits assistant_message as text.',
168
+ ),
169
+ };
170
+ }
171
+
172
+ const provider = normalizeProviderDescriptor(value.provider);
173
+ if (!provider) {
174
+ return {
175
+ ok: false,
176
+ error: createMalformedResponseError(
177
+ 'Provider normalization returned an invalid provider descriptor. Include kind and model strings.',
178
+ ),
179
+ };
180
+ }
181
+
182
+ const finishReason = asNonEmptyString(value.finish_reason);
183
+ if (!finishReason) {
184
+ return {
185
+ ok: false,
186
+ error: createMalformedResponseError(
187
+ 'Provider normalization returned no finish_reason. Include a non-empty provider finish reason.',
188
+ ),
189
+ };
190
+ }
191
+
192
+ const requestedTool = normalizeRequestedTool(value.requested_tool);
193
+ if (status === AGENT_RUNTIME_TURN_STATUSES.TOOL_REQUEST && !requestedTool) {
194
+ return {
195
+ ok: false,
196
+ error: createMalformedResponseError(
197
+ 'Provider normalization returned tool_request without a requested_tool payload.',
198
+ ),
199
+ };
200
+ }
201
+
202
+ const usage = normalizeUsage(value.usage);
203
+
204
+ return {
205
+ ok: true,
206
+ value: {
207
+ status,
208
+ intent,
209
+ assistant_message: value.assistant_message,
210
+ ...(requestedTool ? { requested_tool: requestedTool } : {}),
211
+ provider,
212
+ ...(usage ? { usage } : {}),
213
+ finish_reason: finishReason,
214
+ },
215
+ };
216
+ }
217
+
218
+ export async function executeProviderTurn(
219
+ request: ProviderTurnRequest,
220
+ options?: {
221
+ transport?: ProviderTransport;
222
+ },
223
+ ): Promise<ProviderTurnResult> {
224
+ const requestPayloadResult = buildProviderRequestPayload(request);
225
+ if (!requestPayloadResult.ok) {
226
+ return {
227
+ ok: false,
228
+ error: requestPayloadResult.error,
229
+ metadata: {
230
+ provider_kind: request.kind,
231
+ request_url: request.url,
232
+ response_mode: request.stream ? RESPONSE_MODE_STREAMING : RESPONSE_MODE_NON_STREAMING,
233
+ },
234
+ };
235
+ }
236
+
237
+ const transport = options?.transport ?? defaultTransport;
238
+
239
+ let response: Pick<Response, 'ok' | 'status' | 'text' | 'body'>;
240
+ try {
241
+ response = await transport(request.url, {
242
+ method: REQUEST_METHOD_POST,
243
+ headers: {
244
+ [HEADER_AUTHORIZATION]: `Bearer ${request.apiKey}`,
245
+ [HEADER_CONTENT_TYPE]: CONTENT_TYPE_JSON,
246
+ },
247
+ body: JSON.stringify(requestPayloadResult.value),
248
+ });
249
+ } catch (error) {
250
+ return {
251
+ ok: false,
252
+ error: createTransportError(request, error),
253
+ metadata: {
254
+ provider_kind: request.kind,
255
+ request_url: request.url,
256
+ response_mode: request.stream ? RESPONSE_MODE_STREAMING : RESPONSE_MODE_NON_STREAMING,
257
+ },
258
+ };
259
+ }
260
+
261
+ const metadata: ProviderTurnMetadata = {
262
+ provider_kind: request.kind,
263
+ request_url: request.url,
264
+ response_status: response.status,
265
+ response_mode: request.stream ? RESPONSE_MODE_STREAMING : RESPONSE_MODE_NON_STREAMING,
266
+ };
267
+
268
+ if (!response.ok) {
269
+ const responseText = await response.text();
270
+ return {
271
+ ok: false,
272
+ error: normalizeHttpError(request, response.status, responseText),
273
+ metadata,
274
+ };
275
+ }
276
+
277
+ if (request.stream && request.kind === AGENT_RUNTIME_PROVIDER_KINDS.OPENAI_COMPATIBLE) {
278
+ return executeStreamingProviderTurn(request, response, metadata);
279
+ }
280
+
281
+ const responseText = await response.text();
282
+
283
+ const parsedBody = parseJsonRecord(
284
+ responseText,
285
+ 'Provider returned a non-JSON success payload. Ensure the provider emits JSON and not plain text.',
286
+ );
287
+ if (!parsedBody.ok) {
288
+ return {
289
+ ok: false,
290
+ error: parsedBody.error,
291
+ metadata,
292
+ };
293
+ }
294
+
295
+ const normalizedOutput = normalizeProviderResponse(parsedBody.value, request);
296
+ if (!normalizedOutput.ok) {
297
+ return {
298
+ ok: false,
299
+ error: normalizedOutput.error,
300
+ metadata,
301
+ };
302
+ }
303
+
304
+ return {
305
+ ok: true,
306
+ output: normalizedOutput.value,
307
+ metadata,
308
+ };
309
+ }
310
+
311
+ export async function runProviderAdapterConformanceHarness(): Promise<
312
+ readonly ProviderAdapterConformanceResult[]
313
+ > {
314
+ const [staticProviderUrl] = AGENT_RUNTIME_STATIC_PROVIDER_URLS;
315
+ const baseRequest: ProviderTurnRequest = {
316
+ kind: AGENT_RUNTIME_PROVIDER_KINDS.OPENAI_COMPATIBLE,
317
+ model: 'fixture-model',
318
+ url: staticProviderUrl,
319
+ apiKey: 'fixture-token',
320
+ messages: [{ role: 'user', content: 'Create a reminder.' }],
321
+ toolCatalog: [
322
+ {
323
+ name: FIXTURE_TOOL_NAME,
324
+ description: 'Create a calendar event',
325
+ },
326
+ ],
327
+ intentCatalog: [
328
+ {
329
+ id: 'scheduling',
330
+ description: 'Schedule or reschedule work',
331
+ },
332
+ ],
333
+ };
334
+
335
+ const scenarios = [
336
+ {
337
+ scenario: CONFORMANCE_SCENARIOS.SUCCESS,
338
+ transport: async () =>
339
+ createJsonResponse({
340
+ model: baseRequest.model,
341
+ choices: [
342
+ {
343
+ finish_reason: DEFAULT_FINISH_REASON,
344
+ message: {
345
+ content: JSON.stringify({
346
+ status: AGENT_RUNTIME_TURN_STATUSES.REPLY,
347
+ intent: 'scheduling',
348
+ assistant_message: 'Scheduled.',
349
+ }),
350
+ },
351
+ },
352
+ ],
353
+ usage: {
354
+ [FIXTURE_INPUT_TOKEN_KEY]: 10,
355
+ [FIXTURE_OUTPUT_TOKEN_KEY]: 4,
356
+ [FIXTURE_TOTAL_TOKEN_KEY]: 14,
357
+ },
358
+ }),
359
+ verify: (result: ProviderTurnResult): ProviderAdapterConformanceResult => ({
360
+ scenario: CONFORMANCE_SCENARIOS.SUCCESS,
361
+ passed:
362
+ result.ok &&
363
+ result.output.status === AGENT_RUNTIME_TURN_STATUSES.REPLY &&
364
+ result.output.intent === 'scheduling',
365
+ ...(result.ok ? { normalized_output: result.output } : {}),
366
+ }),
367
+ },
368
+ {
369
+ scenario: CONFORMANCE_SCENARIOS.TOOL_REQUEST_SHAPING,
370
+ transport: async () =>
371
+ createJsonResponse({
372
+ model: baseRequest.model,
373
+ choices: [
374
+ {
375
+ finish_reason: 'tool_calls',
376
+ message: {
377
+ content: JSON.stringify({
378
+ status: AGENT_RUNTIME_TURN_STATUSES.TOOL_REQUEST,
379
+ intent: 'scheduling',
380
+ assistant_message: 'Creating the event now.',
381
+ }),
382
+ tool_calls: [
383
+ {
384
+ type: 'function',
385
+ function: {
386
+ name: FIXTURE_TOOL_NAME,
387
+ arguments: JSON.stringify({
388
+ title: 'Reminder',
389
+ }),
390
+ },
391
+ },
392
+ ],
393
+ },
394
+ },
395
+ ],
396
+ }),
397
+ verify: (result: ProviderTurnResult): ProviderAdapterConformanceResult => ({
398
+ scenario: CONFORMANCE_SCENARIOS.TOOL_REQUEST_SHAPING,
399
+ passed:
400
+ result.ok &&
401
+ result.output.status === AGENT_RUNTIME_TURN_STATUSES.TOOL_REQUEST &&
402
+ result.output.requested_tool?.name === FIXTURE_TOOL_NAME,
403
+ ...(result.ok ? { normalized_output: result.output } : {}),
404
+ }),
405
+ },
406
+ {
407
+ scenario: CONFORMANCE_SCENARIOS.MALFORMED_RESPONSE,
408
+ transport: async () =>
409
+ createJsonResponse({
410
+ model: baseRequest.model,
411
+ choices: [
412
+ {
413
+ finish_reason: DEFAULT_FINISH_REASON,
414
+ message: {
415
+ content: '{"intent":42}',
416
+ },
417
+ },
418
+ ],
419
+ }),
420
+ verify: (result: ProviderTurnResult): ProviderAdapterConformanceResult => ({
421
+ scenario: CONFORMANCE_SCENARIOS.MALFORMED_RESPONSE,
422
+ passed: !result.ok && result.error.code === PROVIDER_ERROR_CODES.MALFORMED_RESPONSE,
423
+ ...(!result.ok ? { normalized_error: result.error } : {}),
424
+ }),
425
+ },
426
+ {
427
+ scenario: CONFORMANCE_SCENARIOS.NORMALIZED_ERROR,
428
+ transport: async () =>
429
+ createJsonResponse(
430
+ {
431
+ error: {
432
+ message: 'Too many requests',
433
+ code: 'rate_limit',
434
+ },
435
+ },
436
+ HTTP_STATUS_RATE_LIMITED,
437
+ ),
438
+ verify: (result: ProviderTurnResult): ProviderAdapterConformanceResult => ({
439
+ scenario: CONFORMANCE_SCENARIOS.NORMALIZED_ERROR,
440
+ passed: !result.ok && result.error.code === PROVIDER_ERROR_CODES.RATE_LIMITED,
441
+ ...(!result.ok ? { normalized_error: result.error } : {}),
442
+ }),
443
+ },
444
+ ] as const;
445
+
446
+ const results: ProviderAdapterConformanceResult[] = [];
447
+ for (const scenario of scenarios) {
448
+ const result = await executeProviderTurn(baseRequest, {
449
+ transport: scenario.transport,
450
+ });
451
+ results.push(scenario.verify(result));
452
+ }
453
+ return results;
454
+ }
455
+
456
+ function buildProviderRequestPayload(
457
+ request: ProviderTurnRequest,
458
+ ): { ok: true; value: Record<string, unknown> } | { ok: false; error: ProviderTurnError } {
459
+ if (request.kind === AGENT_RUNTIME_PROVIDER_KINDS.OPENAI_COMPATIBLE) {
460
+ return {
461
+ ok: true,
462
+ value: buildOpenAiCompatibleRequestPayload(request),
463
+ };
464
+ }
465
+
466
+ if (request.kind === AGENT_RUNTIME_PROVIDER_KINDS.MESSAGES_COMPATIBLE) {
467
+ return {
468
+ ok: true,
469
+ value: buildMessagesCompatibleRequestPayload(request),
470
+ };
471
+ }
472
+
473
+ return {
474
+ ok: false,
475
+ error: {
476
+ code: PROVIDER_ERROR_CODES.UNSUPPORTED_PROVIDER,
477
+ message: `Provider kind "${request.kind}" is not supported by agent-runtime. Configure a supported provider family.`,
478
+ },
479
+ };
480
+ }
481
+
482
+ function buildOpenAiCompatibleRequestPayload(
483
+ request: ProviderTurnRequest,
484
+ ): Record<string, unknown> {
485
+ return {
486
+ model: request.model,
487
+ ...(request.stream ? { stream: true } : {}),
488
+ messages: [
489
+ {
490
+ role: 'system',
491
+ content: buildSystemInstruction(request.intentCatalog, request.toolCatalog),
492
+ },
493
+ ...request.messages.map((message) => normalizeOutboundMessage(message)),
494
+ ],
495
+ response_format: {
496
+ type: RESPONSE_FORMAT_TYPE,
497
+ },
498
+ };
499
+ }
500
+
501
+ function buildMessagesCompatibleRequestPayload(
502
+ request: ProviderTurnRequest,
503
+ ): Record<string, unknown> {
504
+ return {
505
+ model: request.model,
506
+ ...(request.stream ? { stream: true } : {}),
507
+ system: buildSystemInstruction(request.intentCatalog, request.toolCatalog),
508
+ messages: request.messages.map((message) => normalizeOutboundMessage(message)),
509
+ };
510
+ }
511
+
512
+ function buildSystemInstruction(
513
+ intentCatalog: readonly AgentRuntimeIntentCatalogEntry[],
514
+ toolCatalog: readonly AgentRuntimeToolCatalogEntry[],
515
+ ): string {
516
+ const intents =
517
+ intentCatalog.length === 0
518
+ ? 'No explicit intents were supplied.'
519
+ : intentCatalog.map((intent) => `- ${intent.id}: ${intent.description}`).join('\n');
520
+ const tools =
521
+ toolCatalog.length === 0
522
+ ? 'No tools are available.'
523
+ : toolCatalog.map((tool) => `- ${tool.name}: ${tool.description}`).join('\n');
524
+
525
+ return [
526
+ 'Return a single JSON object with keys: status, intent, assistant_message, and optional requested_tool.',
527
+ 'When a tool is needed, set status to "tool_request".',
528
+ 'Allowed intents:',
529
+ intents,
530
+ 'Available tools:',
531
+ tools,
532
+ ].join('\n');
533
+ }
534
+
535
+ function normalizeOutboundMessage(message: AgentRuntimeMessage): Record<string, unknown> {
536
+ const normalized: Record<string, unknown> = {
537
+ role: message.role,
538
+ content: message.content,
539
+ };
540
+
541
+ if (message.tool_name) {
542
+ normalized.tool_name = message.tool_name;
543
+ }
544
+ if (message.tool_call_id) {
545
+ normalized.tool_call_id = message.tool_call_id;
546
+ }
547
+
548
+ return normalized;
549
+ }
550
+
551
+ function normalizeProviderResponse(
552
+ payload: Record<string, unknown>,
553
+ request: ProviderTurnRequest,
554
+ ): { ok: true; value: AgentRuntimeExecuteTurnOutput } | { ok: false; error: ProviderTurnError } {
555
+ if (request.kind === AGENT_RUNTIME_PROVIDER_KINDS.OPENAI_COMPATIBLE) {
556
+ return normalizeOpenAiCompatibleResponse(payload, request);
557
+ }
558
+
559
+ if (request.kind === AGENT_RUNTIME_PROVIDER_KINDS.MESSAGES_COMPATIBLE) {
560
+ return normalizeMessagesCompatibleResponse(payload, request);
561
+ }
562
+
563
+ return {
564
+ ok: false,
565
+ error: {
566
+ code: PROVIDER_ERROR_CODES.UNSUPPORTED_PROVIDER,
567
+ message: `Provider kind "${request.kind}" is not supported by agent-runtime. Configure a supported provider family.`,
568
+ },
569
+ };
570
+ }
571
+
572
+ function normalizeOpenAiCompatibleResponse(
573
+ payload: Record<string, unknown>,
574
+ request: ProviderTurnRequest,
575
+ ): { ok: true; value: AgentRuntimeExecuteTurnOutput } | { ok: false; error: ProviderTurnError } {
576
+ const choices = asArray(payload.choices);
577
+ const [firstChoiceCandidate] = choices ?? [];
578
+ const firstChoice = asRecord(firstChoiceCandidate);
579
+ if (!firstChoice) {
580
+ return {
581
+ ok: false,
582
+ error: createMalformedResponseError(
583
+ 'Provider response did not include choices[0]. Ensure the provider returns a chat-completion-style payload.',
584
+ ),
585
+ };
586
+ }
587
+
588
+ const message = asRecord(firstChoice.message);
589
+ if (!message) {
590
+ return {
591
+ ok: false,
592
+ error: createMalformedResponseError(
593
+ 'Provider response did not include choices[0].message. Ensure the provider returns assistant message data.',
594
+ ),
595
+ };
596
+ }
597
+
598
+ const contentText = extractMessageContentText(message.content);
599
+ const contentRecord = parseJsonRecord(
600
+ contentText,
601
+ 'Provider assistant content was not valid JSON. Ensure the provider returns a JSON object that matches the governed turn schema.',
602
+ );
603
+ if (!contentRecord.ok) {
604
+ return {
605
+ ok: false,
606
+ error: contentRecord.error,
607
+ };
608
+ }
609
+
610
+ const shapedRequestedTool = shapeRequestedTool(message.tool_calls);
611
+ if (!shapedRequestedTool.ok) {
612
+ return {
613
+ ok: false,
614
+ error: shapedRequestedTool.error,
615
+ };
616
+ }
617
+
618
+ const rawOutput: Record<string, unknown> = {
619
+ status: contentRecord.value.status,
620
+ intent: contentRecord.value.intent,
621
+ assistant_message: contentRecord.value.assistant_message ?? DEFAULT_ASSISTANT_MESSAGE,
622
+ provider: {
623
+ kind: request.kind,
624
+ model: asNonEmptyString(payload.model) ?? request.model,
625
+ },
626
+ finish_reason: asNonEmptyString(firstChoice.finish_reason) ?? DEFAULT_FINISH_REASON,
627
+ };
628
+
629
+ const requestedTool = normalizeRequestedTool(contentRecord.value.requested_tool);
630
+ if (requestedTool) {
631
+ rawOutput.requested_tool = requestedTool;
632
+ } else if (shapedRequestedTool.value) {
633
+ rawOutput.requested_tool = shapedRequestedTool.value;
634
+ }
635
+
636
+ const usage = normalizeUsage(payload.usage);
637
+ if (usage) {
638
+ rawOutput.usage = usage;
639
+ }
640
+
641
+ return validateNormalizedTurnOutput(rawOutput);
642
+ }
643
+
644
+ function normalizeMessagesCompatibleResponse(
645
+ payload: Record<string, unknown>,
646
+ request: ProviderTurnRequest,
647
+ ): { ok: true; value: AgentRuntimeExecuteTurnOutput } | { ok: false; error: ProviderTurnError } {
648
+ const responseType = asNonEmptyString(payload.type);
649
+ if (responseType !== MESSAGES_RESPONSE_KIND) {
650
+ return {
651
+ ok: false,
652
+ error: createMalformedResponseError(
653
+ 'Provider response did not include type "message". Ensure the provider returns a message-style payload.',
654
+ ),
655
+ };
656
+ }
657
+
658
+ const contentBlocks = asArray(payload.content);
659
+ if (!contentBlocks || contentBlocks.length === 0) {
660
+ return {
661
+ ok: false,
662
+ error: createMalformedResponseError(
663
+ 'Provider response did not include content blocks. Ensure the provider returns message content.',
664
+ ),
665
+ };
666
+ }
667
+
668
+ let contentText = '';
669
+ let shapedRequestedTool: AgentRuntimeRequestedTool | undefined;
670
+ for (const blockCandidate of contentBlocks) {
671
+ const block = asRecord(blockCandidate);
672
+ if (!block) {
673
+ continue;
674
+ }
675
+
676
+ const blockType = asNonEmptyString(block.type);
677
+ if (blockType === MESSAGES_CONTENT_KIND_TEXT) {
678
+ const blockText = asNonEmptyString(block.text);
679
+ if (blockText) {
680
+ contentText += blockText;
681
+ }
682
+ continue;
683
+ }
684
+
685
+ if (blockType === MESSAGES_CONTENT_KIND_TOOL_USE) {
686
+ const name = asNonEmptyString(block.name);
687
+ const input = asRecord(block.input);
688
+ if (!name || !input) {
689
+ return {
690
+ ok: false,
691
+ error: createMalformedResponseError(
692
+ 'Provider tool_use content must include a non-empty name and object input payload.',
693
+ ),
694
+ };
695
+ }
696
+
697
+ shapedRequestedTool = {
698
+ name,
699
+ input,
700
+ };
701
+ }
702
+ }
703
+
704
+ const contentRecord = parseJsonRecord(
705
+ contentText,
706
+ 'Provider text content was not valid JSON. Ensure the provider returns a JSON object that matches the governed turn schema.',
707
+ );
708
+ if (!contentRecord.ok) {
709
+ return {
710
+ ok: false,
711
+ error: contentRecord.error,
712
+ };
713
+ }
714
+
715
+ const rawOutput: Record<string, unknown> = {
716
+ status: contentRecord.value.status,
717
+ intent: contentRecord.value.intent,
718
+ assistant_message: contentRecord.value.assistant_message ?? DEFAULT_ASSISTANT_MESSAGE,
719
+ provider: {
720
+ kind: request.kind,
721
+ model: asNonEmptyString(payload.model) ?? request.model,
722
+ },
723
+ finish_reason: asNonEmptyString(payload.stop_reason) ?? DEFAULT_FINISH_REASON,
724
+ };
725
+
726
+ const requestedTool = normalizeRequestedTool(contentRecord.value.requested_tool);
727
+ if (requestedTool) {
728
+ rawOutput.requested_tool = requestedTool;
729
+ } else if (shapedRequestedTool) {
730
+ rawOutput.requested_tool = shapedRequestedTool;
731
+ }
732
+
733
+ const usage = normalizeUsage(payload.usage);
734
+ if (usage) {
735
+ rawOutput.usage = usage;
736
+ }
737
+
738
+ return validateNormalizedTurnOutput(rawOutput);
739
+ }
740
+
741
+ async function executeStreamingProviderTurn(
742
+ request: ProviderTurnRequest,
743
+ response: Pick<Response, 'body'>,
744
+ metadata: ProviderTurnMetadata,
745
+ ): Promise<ProviderTurnResult> {
746
+ const streamParse = await parseOpenAiCompatibleStream(response.body, request);
747
+ if (!streamParse.ok) {
748
+ return {
749
+ ok: false,
750
+ error: streamParse.error,
751
+ metadata,
752
+ };
753
+ }
754
+
755
+ const normalizedOutput = normalizeOpenAiCompatibleResponse(streamParse.payload, request);
756
+ if (!normalizedOutput.ok) {
757
+ return {
758
+ ok: false,
759
+ error: normalizedOutput.error,
760
+ metadata,
761
+ };
762
+ }
763
+
764
+ return {
765
+ ok: true,
766
+ output: normalizedOutput.value,
767
+ metadata: {
768
+ ...metadata,
769
+ stream_snapshot_count: streamParse.snapshots.length + 1,
770
+ },
771
+ stream_snapshots: [
772
+ ...streamParse.snapshots,
773
+ {
774
+ sequence: streamParse.snapshots.length,
775
+ state: 'final',
776
+ data: {
777
+ ...normalizedOutput.value,
778
+ },
779
+ },
780
+ ],
781
+ };
782
+ }
783
+
784
+ async function parseOpenAiCompatibleStream(
785
+ body: ReadableStream<Uint8Array> | null | undefined,
786
+ request: ProviderTurnRequest,
787
+ ): Promise<
788
+ | {
789
+ ok: true;
790
+ payload: Record<string, unknown>;
791
+ snapshots: AgentRuntimeStreamSnapshot[];
792
+ }
793
+ | { ok: false; error: ProviderTurnError }
794
+ > {
795
+ const events = await readStreamEvents(body);
796
+ let accumulatedContent = '';
797
+ let finishReason: string | null = null;
798
+ let model = request.model;
799
+ let usage: AgentRuntimeExecuteTurnOutput['usage'];
800
+ const snapshots: AgentRuntimeStreamSnapshot[] = [];
801
+
802
+ for (const event of events) {
803
+ if (event === STREAM_DONE_SENTINEL) {
804
+ continue;
805
+ }
806
+
807
+ const parsedEvent = parseJsonRecord(
808
+ event,
809
+ 'Streaming provider event was not valid JSON. Ensure the provider emits JSON data events.',
810
+ );
811
+ if (!parsedEvent.ok) {
812
+ return parsedEvent;
813
+ }
814
+
815
+ const choice = asRecord(asArray(parsedEvent.value.choices)?.[0]);
816
+ if (!choice) {
817
+ return {
818
+ ok: false,
819
+ error: createMalformedResponseError(
820
+ 'Streaming provider event did not include choices[0]. Ensure the provider emits chat-completion-style stream chunks.',
821
+ ),
822
+ };
823
+ }
824
+
825
+ const streamedFinishReason = asNonEmptyString(choice.finish_reason);
826
+ const delta = asRecord(choice.delta);
827
+ const contentDelta = extractMessageContentText(delta?.content);
828
+ if (contentDelta.length > 0) {
829
+ accumulatedContent += contentDelta;
830
+ if (!streamedFinishReason) {
831
+ snapshots.push({
832
+ sequence: snapshots.length,
833
+ state: 'partial',
834
+ data: {
835
+ assistant_message: accumulatedContent,
836
+ provider: {
837
+ kind: request.kind,
838
+ model,
839
+ },
840
+ },
841
+ });
842
+ }
843
+ }
844
+
845
+ if (streamedFinishReason) {
846
+ finishReason = streamedFinishReason;
847
+ }
848
+
849
+ const streamedModel = asNonEmptyString(parsedEvent.value.model);
850
+ if (streamedModel) {
851
+ model = streamedModel;
852
+ }
853
+
854
+ const streamedUsage = normalizeUsage(parsedEvent.value.usage);
855
+ if (streamedUsage) {
856
+ usage = streamedUsage;
857
+ }
858
+ }
859
+
860
+ return {
861
+ ok: true,
862
+ payload: {
863
+ model,
864
+ choices: [
865
+ {
866
+ finish_reason: finishReason ?? DEFAULT_FINISH_REASON,
867
+ message: {
868
+ content: accumulatedContent,
869
+ },
870
+ },
871
+ ],
872
+ ...(usage ? { usage } : {}),
873
+ },
874
+ snapshots,
875
+ };
876
+ }
877
+
878
+ function normalizeTurnStatus(
879
+ statusValue: unknown,
880
+ requestedToolValue: unknown,
881
+ ): AgentRuntimeTurnStatus | null {
882
+ if (isTurnStatus(statusValue)) {
883
+ return requestedToolValue ? AGENT_RUNTIME_TURN_STATUSES.TOOL_REQUEST : statusValue;
884
+ }
885
+
886
+ if (requestedToolValue) {
887
+ return AGENT_RUNTIME_TURN_STATUSES.TOOL_REQUEST;
888
+ }
889
+
890
+ return AGENT_RUNTIME_TURN_STATUSES.REPLY;
891
+ }
892
+
893
+ function normalizeProviderDescriptor(
894
+ value: unknown,
895
+ ): AgentRuntimeExecuteTurnOutput['provider'] | null {
896
+ if (!isRecord(value)) {
897
+ return null;
898
+ }
899
+
900
+ const kind = asNonEmptyString(value.kind);
901
+ const model = asNonEmptyString(value.model);
902
+ if (!kind || !model) {
903
+ return null;
904
+ }
905
+
906
+ if (!isProviderKind(kind)) {
907
+ return null;
908
+ }
909
+
910
+ return {
911
+ kind,
912
+ model,
913
+ };
914
+ }
915
+
916
+ function normalizeUsage(value: unknown): AgentRuntimeExecuteTurnOutput['usage'] | undefined {
917
+ if (!isRecord(value)) {
918
+ return undefined;
919
+ }
920
+
921
+ const inputTokens = asInteger(value.input_tokens ?? value.prompt_tokens);
922
+ const outputTokens = asInteger(value.output_tokens ?? value.completion_tokens);
923
+ const totalTokens = asInteger(value.total_tokens ?? value.total_tokens);
924
+
925
+ if (inputTokens === null && outputTokens === null && totalTokens === null) {
926
+ return undefined;
927
+ }
928
+
929
+ return {
930
+ ...(inputTokens !== null ? { input_tokens: inputTokens } : {}),
931
+ ...(outputTokens !== null ? { output_tokens: outputTokens } : {}),
932
+ ...(totalTokens !== null ? { total_tokens: totalTokens } : {}),
933
+ };
934
+ }
935
+
936
+ function isProviderKind(value: string): value is AgentRuntimeProviderKind {
937
+ return (
938
+ value === AGENT_RUNTIME_PROVIDER_KINDS.OPENAI_COMPATIBLE ||
939
+ value === AGENT_RUNTIME_PROVIDER_KINDS.MESSAGES_COMPATIBLE
940
+ );
941
+ }
942
+
943
+ function normalizeRequestedTool(value: unknown): AgentRuntimeRequestedTool | null {
944
+ if (!isRecord(value)) {
945
+ return null;
946
+ }
947
+
948
+ const name = asNonEmptyString(value.name);
949
+ const input = asRecord(value.input);
950
+ if (!name || !input) {
951
+ return null;
952
+ }
953
+
954
+ return { name, input };
955
+ }
956
+
957
+ function shapeRequestedTool(
958
+ toolCallsValue: unknown,
959
+ ): { ok: true; value: AgentRuntimeRequestedTool | null } | { ok: false; error: ProviderTurnError } {
960
+ const toolCalls = asArray(toolCallsValue);
961
+ if (!toolCalls || toolCalls.length === 0) {
962
+ return { ok: true, value: null };
963
+ }
964
+
965
+ const [firstToolCallCandidate] = toolCalls;
966
+ const firstToolCall = asRecord(firstToolCallCandidate);
967
+ const functionRecord = firstToolCall ? asRecord(firstToolCall.function) : null;
968
+ const name = functionRecord ? asNonEmptyString(functionRecord.name) : null;
969
+ if (!functionRecord || !name) {
970
+ return {
971
+ ok: false,
972
+ error: createMalformedResponseError(
973
+ 'Provider tool call did not include function.name. Ensure tool calls include a named function payload.',
974
+ ),
975
+ };
976
+ }
977
+
978
+ const parsedArguments = parseRequestedToolArguments(functionRecord.arguments);
979
+ if (!parsedArguments.ok) {
980
+ return {
981
+ ok: false,
982
+ error: parsedArguments.error,
983
+ };
984
+ }
985
+
986
+ return {
987
+ ok: true,
988
+ value: {
989
+ name,
990
+ input: parsedArguments.value,
991
+ },
992
+ };
993
+ }
994
+
995
+ function parseRequestedToolArguments(
996
+ value: unknown,
997
+ ): { ok: true; value: Record<string, unknown> } | { ok: false; error: ProviderTurnError } {
998
+ if (isRecord(value)) {
999
+ return { ok: true, value };
1000
+ }
1001
+
1002
+ const argumentsText = asNonEmptyString(value);
1003
+ if (!argumentsText) {
1004
+ return {
1005
+ ok: false,
1006
+ error: createMalformedResponseError(
1007
+ 'Provider tool call arguments were missing. Ensure tool call arguments are a JSON object.',
1008
+ ),
1009
+ };
1010
+ }
1011
+
1012
+ return parseJsonRecord(
1013
+ argumentsText,
1014
+ 'Provider tool call arguments were not valid JSON. Ensure the provider encodes tool arguments as a JSON object.',
1015
+ );
1016
+ }
1017
+
1018
+ function extractMessageContentText(value: unknown): string {
1019
+ if (typeof value === 'string') {
1020
+ return value;
1021
+ }
1022
+
1023
+ const parts = asArray(value);
1024
+ if (!parts) {
1025
+ return DEFAULT_ASSISTANT_MESSAGE;
1026
+ }
1027
+
1028
+ const textParts: string[] = [];
1029
+ for (const part of parts) {
1030
+ const record = asRecord(part);
1031
+ if (!record) {
1032
+ continue;
1033
+ }
1034
+ const textValue = asNonEmptyString(record.text);
1035
+ if (textValue) {
1036
+ textParts.push(textValue);
1037
+ }
1038
+ }
1039
+
1040
+ return textParts.join('\n');
1041
+ }
1042
+
1043
+ async function readStreamEvents(
1044
+ body: ReadableStream<Uint8Array> | null | undefined,
1045
+ ): Promise<string[]> {
1046
+ if (!body) {
1047
+ return [];
1048
+ }
1049
+
1050
+ const reader = body.getReader();
1051
+ const decoder = new TextDecoder();
1052
+ const events: string[] = [];
1053
+ let buffer = '';
1054
+
1055
+ while (true) {
1056
+ const { done, value } = await reader.read();
1057
+ buffer += decoder.decode(value ?? new Uint8Array(), { stream: !done });
1058
+ buffer = buffer.replaceAll('\r\n', '\n');
1059
+
1060
+ let delimiterIndex = buffer.indexOf(STREAM_DELIMITER);
1061
+ while (delimiterIndex >= 0) {
1062
+ const event = parseStreamEvent(buffer.slice(0, delimiterIndex));
1063
+ if (event) {
1064
+ events.push(event);
1065
+ }
1066
+ buffer = buffer.slice(delimiterIndex + STREAM_DELIMITER.length);
1067
+ delimiterIndex = buffer.indexOf(STREAM_DELIMITER);
1068
+ }
1069
+
1070
+ if (done) {
1071
+ break;
1072
+ }
1073
+ }
1074
+
1075
+ const trailingEvent = parseStreamEvent(buffer);
1076
+ if (trailingEvent) {
1077
+ events.push(trailingEvent);
1078
+ }
1079
+
1080
+ return events;
1081
+ }
1082
+
1083
+ function parseStreamEvent(chunk: string): string | null {
1084
+ const dataLines = chunk
1085
+ .split('\n')
1086
+ .map((line) => line.trim())
1087
+ .filter((line) => line.startsWith(STREAM_LINE_PREFIX))
1088
+ .map((line) => line.slice(STREAM_LINE_PREFIX.length).trimStart());
1089
+
1090
+ if (dataLines.length === 0) {
1091
+ return null;
1092
+ }
1093
+
1094
+ return dataLines.join('\n');
1095
+ }
1096
+
1097
+ function normalizeHttpError(
1098
+ request: ProviderTurnRequest,
1099
+ status: number,
1100
+ responseText: string,
1101
+ ): ProviderTurnError {
1102
+ const parsedResponse = safeParseJsonRecord(responseText);
1103
+ const providerErrorRecord =
1104
+ parsedResponse && isRecord(parsedResponse.error) ? parsedResponse.error : null;
1105
+ const providerMessage = asNonEmptyString(providerErrorRecord?.message);
1106
+ const providerCode = asNonEmptyString(providerErrorRecord?.code);
1107
+
1108
+ if (status === HTTP_STATUS_UNAUTHORIZED) {
1109
+ return {
1110
+ code: PROVIDER_ERROR_CODES.AUTHENTICATION_FAILED,
1111
+ message: `Provider rejected the agent-runtime credentials with HTTP ${status}. Check ${AGENT_RUNTIME_API_KEY_ENV} and retry the turn.`,
1112
+ details: {
1113
+ provider_kind: request.kind,
1114
+ response_status: status,
1115
+ request_url: request.url,
1116
+ ...(providerCode ? { provider_code: providerCode } : {}),
1117
+ ...(providerMessage ? { provider_message: providerMessage } : {}),
1118
+ },
1119
+ };
1120
+ }
1121
+
1122
+ if (status === HTTP_STATUS_RATE_LIMITED) {
1123
+ return {
1124
+ code: PROVIDER_ERROR_CODES.RATE_LIMITED,
1125
+ message: `Provider rate limited the agent-runtime turn with HTTP ${status}. Retry later or reduce turn volume.`,
1126
+ details: {
1127
+ provider_kind: request.kind,
1128
+ response_status: status,
1129
+ request_url: request.url,
1130
+ ...(providerCode ? { provider_code: providerCode } : {}),
1131
+ ...(providerMessage ? { provider_message: providerMessage } : {}),
1132
+ },
1133
+ };
1134
+ }
1135
+
1136
+ return {
1137
+ code: PROVIDER_ERROR_CODES.HTTP_ERROR,
1138
+ message: `Provider request failed with HTTP ${status} while calling ${request.url}. Review provider availability and request formatting.`,
1139
+ details: {
1140
+ provider_kind: request.kind,
1141
+ response_status: status,
1142
+ request_url: request.url,
1143
+ ...(providerCode ? { provider_code: providerCode } : {}),
1144
+ ...(providerMessage ? { provider_message: providerMessage } : {}),
1145
+ },
1146
+ };
1147
+ }
1148
+
1149
+ function createMalformedResponseError(message: string): ProviderTurnError {
1150
+ return {
1151
+ code: PROVIDER_ERROR_CODES.MALFORMED_RESPONSE,
1152
+ message,
1153
+ };
1154
+ }
1155
+
1156
+ function createTransportError(request: ProviderTurnRequest, error: unknown): ProviderTurnError {
1157
+ const detail =
1158
+ error instanceof Error
1159
+ ? error.message
1160
+ : typeof error === 'string'
1161
+ ? error
1162
+ : 'Unknown transport failure';
1163
+
1164
+ return {
1165
+ code: PROVIDER_ERROR_CODES.TRANSPORT_ERROR,
1166
+ message: `Provider request failed before a response was received from ${request.url}: ${detail}`,
1167
+ details: {
1168
+ provider_kind: request.kind,
1169
+ request_url: request.url,
1170
+ },
1171
+ };
1172
+ }
1173
+
1174
+ function parseJsonRecord(
1175
+ text: string,
1176
+ message: string,
1177
+ ): { ok: true; value: Record<string, unknown> } | { ok: false; error: ProviderTurnError } {
1178
+ const parsed = safeParseJsonRecord(text);
1179
+ if (!parsed) {
1180
+ return {
1181
+ ok: false,
1182
+ error: createMalformedResponseError(message),
1183
+ };
1184
+ }
1185
+ return {
1186
+ ok: true,
1187
+ value: parsed,
1188
+ };
1189
+ }
1190
+
1191
+ function safeParseJsonRecord(text: string): Record<string, unknown> | null {
1192
+ try {
1193
+ const parsed = JSON.parse(text) as unknown;
1194
+ return isRecord(parsed) ? parsed : null;
1195
+ } catch {
1196
+ return null;
1197
+ }
1198
+ }
1199
+
1200
+ function isTurnStatus(value: unknown): value is AgentRuntimeTurnStatus {
1201
+ return Object.values(AGENT_RUNTIME_TURN_STATUSES).includes(value as AgentRuntimeTurnStatus);
1202
+ }
1203
+
1204
+ function isRecord(value: unknown): value is Record<string, unknown> {
1205
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
1206
+ }
1207
+
1208
+ function asRecord(value: unknown): Record<string, unknown> | null {
1209
+ return isRecord(value) ? value : null;
1210
+ }
1211
+
1212
+ function asArray(value: unknown): unknown[] | null {
1213
+ return Array.isArray(value) ? value : null;
1214
+ }
1215
+
1216
+ function asNonEmptyString(value: unknown): string | null {
1217
+ if (typeof value !== 'string') {
1218
+ return null;
1219
+ }
1220
+ const trimmed = value.trim();
1221
+ return trimmed.length > 0 ? trimmed : null;
1222
+ }
1223
+
1224
+ function asInteger(value: unknown): number | null {
1225
+ if (typeof value !== 'number' || !Number.isFinite(value) || value < 0) {
1226
+ return null;
1227
+ }
1228
+ return Math.trunc(value);
1229
+ }
1230
+
1231
+ async function defaultTransport(
1232
+ url: string,
1233
+ init: RequestInit,
1234
+ ): Promise<Pick<Response, 'ok' | 'status' | 'text' | 'body'>> {
1235
+ return globalThis.fetch(url, init);
1236
+ }
1237
+
1238
+ function createJsonResponse(body: unknown, status = 200): Response {
1239
+ return new Response(JSON.stringify(body), {
1240
+ status,
1241
+ headers: {
1242
+ [HEADER_CONTENT_TYPE]: CONTENT_TYPE_JSON,
1243
+ },
1244
+ });
1245
+ }