@lumenflow/cli 5.5.0 → 5.7.14

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 (229) hide show
  1. package/README.md +41 -40
  2. package/dist/db-journal-recover.js +400 -0
  3. package/dist/db-journal-recover.js.map +1 -0
  4. package/dist/docs-sync.js +8 -3
  5. package/dist/docs-sync.js.map +1 -1
  6. package/dist/doctor.js +11 -0
  7. package/dist/doctor.js.map +1 -1
  8. package/dist/gate-defaults.js +37 -0
  9. package/dist/gate-defaults.js.map +1 -1
  10. package/dist/gates/monolithic-file-contention-guard.js +167 -0
  11. package/dist/gates/monolithic-file-contention-guard.js.map +1 -0
  12. package/dist/gates/prod-migration-drift.js +207 -0
  13. package/dist/gates/prod-migration-drift.js.map +1 -0
  14. package/dist/gates/test-over-deletion-guard.js +280 -0
  15. package/dist/gates/test-over-deletion-guard.js.map +1 -0
  16. package/dist/gates-runners.js +44 -3
  17. package/dist/gates-runners.js.map +1 -1
  18. package/dist/gates.js +3 -2
  19. package/dist/gates.js.map +1 -1
  20. package/dist/hooks/config-resolver.js +16 -1
  21. package/dist/hooks/config-resolver.js.map +1 -1
  22. package/dist/hooks/dirty-guard.js +43 -2
  23. package/dist/hooks/dirty-guard.js.map +1 -1
  24. package/dist/hooks/git-status-parser.js +22 -8
  25. package/dist/hooks/git-status-parser.js.map +1 -1
  26. package/dist/init-templates.js +241 -0
  27. package/dist/init-templates.js.map +1 -1
  28. package/dist/init.js +122 -16
  29. package/dist/init.js.map +1 -1
  30. package/dist/lumenflow-setup.js +144 -0
  31. package/dist/lumenflow-setup.js.map +1 -0
  32. package/dist/lumenflow-upgrade.js +43 -1
  33. package/dist/lumenflow-upgrade.js.map +1 -1
  34. package/dist/mem-create.js +10 -1
  35. package/dist/mem-create.js.map +1 -1
  36. package/dist/mem-signal.js +21 -4
  37. package/dist/mem-signal.js.map +1 -1
  38. package/dist/orchestrate-initiative.js +28 -3
  39. package/dist/orchestrate-initiative.js.map +1 -1
  40. package/dist/public-manifest.js +17 -7
  41. package/dist/public-manifest.js.map +1 -1
  42. package/dist/release.js +53 -18
  43. package/dist/release.js.map +1 -1
  44. package/dist/wu-done-gates.js +13 -9
  45. package/dist/wu-done-gates.js.map +1 -1
  46. package/dist/wu-done.js +14 -2
  47. package/dist/wu-done.js.map +1 -1
  48. package/dist/wu-edit-operations.js +74 -0
  49. package/dist/wu-edit-operations.js.map +1 -1
  50. package/dist/wu-edit-validators.js +58 -0
  51. package/dist/wu-edit-validators.js.map +1 -1
  52. package/dist/wu-edit.js +106 -4
  53. package/dist/wu-edit.js.map +1 -1
  54. package/dist/wu-prep.js +57 -9
  55. package/dist/wu-prep.js.map +1 -1
  56. package/dist/wu-recover.js +6 -0
  57. package/dist/wu-recover.js.map +1 -1
  58. package/dist/wu-release.js +120 -2
  59. package/dist/wu-release.js.map +1 -1
  60. package/dist/wu-sizing-validation.js +47 -17
  61. package/dist/wu-sizing-validation.js.map +1 -1
  62. package/dist/wu-status.js +33 -0
  63. package/dist/wu-status.js.map +1 -1
  64. package/package.json +13 -12
  65. package/packs/agent-runtime/package.json +1 -1
  66. package/packs/sidekick/package.json +1 -1
  67. package/packs/software-delivery/package.json +1 -1
  68. package/templates/core/AGENTS.md.template +67 -3
  69. package/templates/core/LUMENFLOW.md.template +196 -47
  70. package/dist/distribution-preflight.js +0 -230
  71. package/dist/distribution-preflight.js.map +0 -1
  72. package/packs/agent-runtime/agent-heartbeat.ts +0 -163
  73. package/packs/agent-runtime/auto-session-integration.ts +0 -888
  74. package/packs/agent-runtime/capability-factory.ts +0 -104
  75. package/packs/agent-runtime/constants.ts +0 -21
  76. package/packs/agent-runtime/delegation-registry-schema.ts +0 -220
  77. package/packs/agent-runtime/delegation-registry-store.ts +0 -269
  78. package/packs/agent-runtime/delegation-tree.ts +0 -328
  79. package/packs/agent-runtime/index.ts +0 -20
  80. package/packs/agent-runtime/manifest.ts +0 -348
  81. package/packs/agent-runtime/memory-coordination-contract.ts +0 -86
  82. package/packs/agent-runtime/orchestration.ts +0 -2027
  83. package/packs/agent-runtime/pack-registration.ts +0 -110
  84. package/packs/agent-runtime/policy-factory.ts +0 -165
  85. package/packs/agent-runtime/remote-controls/index.ts +0 -7
  86. package/packs/agent-runtime/remote-controls/operations.ts +0 -405
  87. package/packs/agent-runtime/remote-controls/port.ts +0 -48
  88. package/packs/agent-runtime/remote-controls/state-store.ts +0 -258
  89. package/packs/agent-runtime/remote-controls/types.ts +0 -105
  90. package/packs/agent-runtime/session-schema.ts +0 -467
  91. package/packs/agent-runtime/tool-impl/agent-turn-tools.ts +0 -793
  92. package/packs/agent-runtime/tool-impl/index.ts +0 -6
  93. package/packs/agent-runtime/tool-impl/provider-adapters.ts +0 -1245
  94. package/packs/agent-runtime/tool-impl/remote-controls.mock.ts +0 -256
  95. package/packs/agent-runtime/tool-impl/remote-controls.ts +0 -273
  96. package/packs/agent-runtime/tools/index.ts +0 -4
  97. package/packs/agent-runtime/tools/types.ts +0 -47
  98. package/packs/agent-runtime/turn-lifecycle-events.ts +0 -590
  99. package/packs/agent-runtime/types.ts +0 -128
  100. package/packs/agent-runtime/vitest.config.ts +0 -11
  101. package/packs/sidekick/channel-ingress.ts +0 -137
  102. package/packs/sidekick/constants.ts +0 -10
  103. package/packs/sidekick/index.ts +0 -8
  104. package/packs/sidekick/manifest-schema.ts +0 -49
  105. package/packs/sidekick/manifest.ts +0 -512
  106. package/packs/sidekick/pack-registration.ts +0 -110
  107. package/packs/sidekick/policy-factory.ts +0 -38
  108. package/packs/sidekick/sidekick-events.ts +0 -694
  109. package/packs/sidekick/src/adapters/cloud-queue.ts +0 -101
  110. package/packs/sidekick/src/adapters/control-plane-bridge.adapter.ts +0 -386
  111. package/packs/sidekick/src/adapters/filesystem-bridge.adapter.ts +0 -228
  112. package/packs/sidekick/src/domain/channel.types.ts +0 -64
  113. package/packs/sidekick/src/ports/channel-bridge.port.ts +0 -92
  114. package/packs/sidekick/src/routines/commit.ts +0 -74
  115. package/packs/sidekick/tool-impl/channel-tools.ts +0 -577
  116. package/packs/sidekick/tool-impl/channel-transports.ts +0 -75
  117. package/packs/sidekick/tool-impl/index.ts +0 -29
  118. package/packs/sidekick/tool-impl/memory-tools.ts +0 -290
  119. package/packs/sidekick/tool-impl/routine-commit.ts +0 -102
  120. package/packs/sidekick/tool-impl/routine-tools.ts +0 -440
  121. package/packs/sidekick/tool-impl/runtime-context.ts +0 -28
  122. package/packs/sidekick/tool-impl/shared.ts +0 -125
  123. package/packs/sidekick/tool-impl/storage.ts +0 -325
  124. package/packs/sidekick/tool-impl/system-tools.ts +0 -160
  125. package/packs/sidekick/tool-impl/task-tools.ts +0 -506
  126. package/packs/sidekick/tools/channel-tools.ts +0 -53
  127. package/packs/sidekick/tools/index.ts +0 -9
  128. package/packs/sidekick/tools/memory-tools.ts +0 -53
  129. package/packs/sidekick/tools/routine-tools.ts +0 -53
  130. package/packs/sidekick/tools/system-tools.ts +0 -47
  131. package/packs/sidekick/tools/task-tools.ts +0 -61
  132. package/packs/sidekick/tools/types.ts +0 -57
  133. package/packs/sidekick/vitest.config.ts +0 -11
  134. package/packs/software-delivery/constants.ts +0 -10
  135. package/packs/software-delivery/extensions.ts +0 -140
  136. package/packs/software-delivery/gate-policies.ts +0 -134
  137. package/packs/software-delivery/index.ts +0 -8
  138. package/packs/software-delivery/manifest-schema.ts +0 -268
  139. package/packs/software-delivery/manifest.ts +0 -657
  140. package/packs/software-delivery/pack-registration.ts +0 -113
  141. package/packs/software-delivery/src/commands/index.ts +0 -5
  142. package/packs/software-delivery/src/config/delivery-review-contract.ts +0 -256
  143. package/packs/software-delivery/src/config/env-accessors.ts +0 -66
  144. package/packs/software-delivery/src/config/index.ts +0 -8
  145. package/packs/software-delivery/src/config/normalize-config-keys.ts +0 -9
  146. package/packs/software-delivery/src/config/schemas/lumenflow-config-schema-types.ts +0 -460
  147. package/packs/software-delivery/src/config/workspace-reader.ts +0 -375
  148. package/packs/software-delivery/src/constants/backlog-patterns.ts +0 -31
  149. package/packs/software-delivery/src/constants/client-ids.ts +0 -19
  150. package/packs/software-delivery/src/constants/config-contract.ts +0 -7
  151. package/packs/software-delivery/src/constants/docs-layout-presets.ts +0 -50
  152. package/packs/software-delivery/src/constants/duration-constants.ts +0 -20
  153. package/packs/software-delivery/src/constants/gate-constants.ts +0 -32
  154. package/packs/software-delivery/src/constants/index.ts +0 -29
  155. package/packs/software-delivery/src/constants/lock-constants.ts +0 -35
  156. package/packs/software-delivery/src/constants/object-guards.ts +0 -12
  157. package/packs/software-delivery/src/constants/section-headings.ts +0 -107
  158. package/packs/software-delivery/src/constants/wu-cli-constants.ts +0 -500
  159. package/packs/software-delivery/src/constants/wu-domain-constants.ts +0 -466
  160. package/packs/software-delivery/src/constants/wu-git-constants.ts +0 -7
  161. package/packs/software-delivery/src/constants/wu-id-format.ts +0 -327
  162. package/packs/software-delivery/src/constants/wu-paths-constants.ts +0 -384
  163. package/packs/software-delivery/src/constants/wu-statuses.ts +0 -287
  164. package/packs/software-delivery/src/constants/wu-type-helpers.ts +0 -67
  165. package/packs/software-delivery/src/constants/wu-ui-constants.ts +0 -267
  166. package/packs/software-delivery/src/constants/wu-validation-constants.ts +0 -73
  167. package/packs/software-delivery/src/domain/index.ts +0 -5
  168. package/packs/software-delivery/src/domain/orchestration.constants.ts +0 -166
  169. package/packs/software-delivery/src/domain/orchestration.schemas.ts +0 -238
  170. package/packs/software-delivery/src/domain/orchestration.types.ts +0 -176
  171. package/packs/software-delivery/src/methodology/incremental-test.ts +0 -122
  172. package/packs/software-delivery/src/methodology/index.ts +0 -6
  173. package/packs/software-delivery/src/methodology/manual-test-validator.ts +0 -292
  174. package/packs/software-delivery/src/policy/coverage-gate.ts +0 -270
  175. package/packs/software-delivery/src/policy/gates-agent-mode.ts +0 -223
  176. package/packs/software-delivery/src/policy/gates-config-internal.ts +0 -121
  177. package/packs/software-delivery/src/policy/gates-config.ts +0 -300
  178. package/packs/software-delivery/src/policy/gates-coverage.ts +0 -356
  179. package/packs/software-delivery/src/policy/gates-presets.ts +0 -134
  180. package/packs/software-delivery/src/policy/gates-schemas.ts +0 -173
  181. package/packs/software-delivery/src/policy/index.ts +0 -22
  182. package/packs/software-delivery/src/policy/package-manager-resolver.ts +0 -319
  183. package/packs/software-delivery/src/policy/resolve-policy.ts +0 -601
  184. package/packs/software-delivery/src/ports/config.ports.ts +0 -90
  185. package/packs/software-delivery/src/ports/dashboard-renderer.port.ts +0 -125
  186. package/packs/software-delivery/src/ports/index.ts +0 -10
  187. package/packs/software-delivery/src/ports/sync-validator.ports.ts +0 -59
  188. package/packs/software-delivery/src/ports/wu-helpers.ports.ts +0 -168
  189. package/packs/software-delivery/src/ports/wu-state.ports.ts +0 -241
  190. package/packs/software-delivery/src/primitives/index.ts +0 -5
  191. package/packs/software-delivery/src/runtime/index.ts +0 -6
  192. package/packs/software-delivery/src/runtime/work-classifier.ts +0 -561
  193. package/packs/software-delivery/src/sandbox/index.ts +0 -10
  194. package/packs/software-delivery/src/sandbox/sandbox-allowlist.ts +0 -118
  195. package/packs/software-delivery/src/sandbox/sandbox-backend-linux.ts +0 -88
  196. package/packs/software-delivery/src/sandbox/sandbox-backend-macos.ts +0 -154
  197. package/packs/software-delivery/src/sandbox/sandbox-backend-windows.ts +0 -47
  198. package/packs/software-delivery/src/sandbox/sandbox-profile.ts +0 -153
  199. package/packs/software-delivery/src/schemas/index.ts +0 -5
  200. package/packs/software-delivery/src/state/date-utils.ts +0 -158
  201. package/packs/software-delivery/src/state/index.ts +0 -15
  202. package/packs/software-delivery/src/state/state-machine.ts +0 -119
  203. package/packs/software-delivery/src/state/wu-doc-types.ts +0 -51
  204. package/packs/software-delivery/src/state/wu-paths.ts +0 -381
  205. package/packs/software-delivery/src/state/wu-schema.ts +0 -1139
  206. package/packs/software-delivery/src/state/wu-state-schema.ts +0 -255
  207. package/packs/software-delivery/src/state/wu-yaml.ts +0 -338
  208. package/packs/software-delivery/tool-impl/agent-tools.ts +0 -263
  209. package/packs/software-delivery/tool-impl/delegation-tools.ts +0 -66
  210. package/packs/software-delivery/tool-impl/flow-metrics-tools.ts +0 -219
  211. package/packs/software-delivery/tool-impl/git-runner.ts +0 -113
  212. package/packs/software-delivery/tool-impl/git-tools.ts +0 -316
  213. package/packs/software-delivery/tool-impl/index.ts +0 -15
  214. package/packs/software-delivery/tool-impl/initiative-orchestration-tools.ts +0 -720
  215. package/packs/software-delivery/tool-impl/lane-lock.ts +0 -246
  216. package/packs/software-delivery/tool-impl/memory-tools.ts +0 -470
  217. package/packs/software-delivery/tool-impl/pending-runtime-tools.ts +0 -21
  218. package/packs/software-delivery/tool-impl/runtime-cli-adapter.ts +0 -329
  219. package/packs/software-delivery/tool-impl/runtime-native-tools.ts +0 -687
  220. package/packs/software-delivery/tool-impl/worker-loader.ts +0 -52
  221. package/packs/software-delivery/tool-impl/worktree-tools.ts +0 -46
  222. package/packs/software-delivery/tool-impl/wu-lifecycle-tools.ts +0 -807
  223. package/packs/software-delivery/tools/delegation-tools.ts +0 -23
  224. package/packs/software-delivery/tools/git-tools.ts +0 -55
  225. package/packs/software-delivery/tools/index.ts +0 -8
  226. package/packs/software-delivery/tools/lane-lock-tool.ts +0 -37
  227. package/packs/software-delivery/tools/types.ts +0 -71
  228. package/packs/software-delivery/tools/worktree-tools.ts +0 -49
  229. package/packs/software-delivery/vitest.config.ts +0 -11
@@ -1,1245 +0,0 @@
1
- // Copyright (c) 2026 Hellmai Ltd
2
- // SPDX-License-Identifier: LicenseRef-LumenFlow-Proprietary
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
- }