salmon-loop 0.2.13 → 0.3.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 (224) hide show
  1. package/dist/cli/argv/headless-detection.js +27 -0
  2. package/dist/cli/chat-flow.js +11 -0
  3. package/dist/cli/chat.js +160 -24
  4. package/dist/cli/commands/chat.js +14 -7
  5. package/dist/cli/commands/flow-mode.js +63 -0
  6. package/dist/cli/commands/registry.js +2 -0
  7. package/dist/cli/commands/run/benchmark-artifacts.js +41 -0
  8. package/dist/cli/commands/run/early-errors.js +23 -0
  9. package/dist/cli/commands/run/handler.js +115 -27
  10. package/dist/cli/commands/run/headless-error-writer.js +8 -0
  11. package/dist/cli/commands/run/loop-params.js +2 -0
  12. package/dist/cli/commands/run/mode.js +2 -5
  13. package/dist/cli/commands/run/parse-options.js +16 -0
  14. package/dist/cli/commands/run/persist-session.js +10 -1
  15. package/dist/cli/commands/run/preflight.js +10 -0
  16. package/dist/cli/commands/run/reporter-factory.js +4 -0
  17. package/dist/cli/commands/run/runtime-llm.js +38 -11
  18. package/dist/cli/commands/run/runtime-options.js +2 -2
  19. package/dist/cli/commands/serve.js +97 -77
  20. package/dist/cli/commands/tool-names.js +78 -78
  21. package/dist/cli/headless/anthropic-stream-normalized-encoder.js +6 -1
  22. package/dist/cli/headless/json-protocol.js +37 -0
  23. package/dist/cli/headless/native-stream-normalized-encoder.js +6 -1
  24. package/dist/cli/headless/protocol-metadata.js +22 -0
  25. package/dist/cli/headless/stream-json-protocol.js +34 -1
  26. package/dist/cli/index.js +6 -4
  27. package/dist/cli/locales/en.js +30 -6
  28. package/dist/cli/program-bootstrap.js +10 -5
  29. package/dist/cli/program-commands.js +5 -1
  30. package/dist/cli/reporters/anthropic-stream.js +7 -1
  31. package/dist/cli/reporters/json.js +4 -0
  32. package/dist/cli/reporters/stream-json.js +17 -2
  33. package/dist/cli/run-cli.js +5 -3
  34. package/dist/cli/slash/runtime.js +27 -12
  35. package/dist/cli/ui/components/CommandInput.js +7 -3
  36. package/dist/cli/ui/components/CommandSuggestionList.js +1 -1
  37. package/dist/cli/utils/command-option-source.js +13 -0
  38. package/dist/cli/utils/verify-resolver.js +8 -4
  39. package/dist/cli/utils/worktree-prepare-resolver.js +7 -3
  40. package/dist/core/adapters/fs/file-adapter.js +6 -0
  41. package/dist/core/adapters/fs/filesystem.js +2 -1
  42. package/dist/core/adapters/git/git-adapter.js +78 -1
  43. package/dist/core/backends/salmon-loop/task-executor.js +1 -0
  44. package/dist/core/benchmark/patch-artifact.js +124 -0
  45. package/dist/core/benchmark/swe-bench.js +25 -0
  46. package/dist/core/config/load.js +18 -11
  47. package/dist/core/config/resolve-llm.js +12 -0
  48. package/dist/core/config/resolvers/server.js +0 -6
  49. package/dist/core/config/validate.js +73 -21
  50. package/dist/core/context/gatherers/metadata-gatherer.js +1 -0
  51. package/dist/core/context/gatherers/ripgrep-gatherer.js +84 -2
  52. package/dist/core/context/keywords.js +18 -4
  53. package/dist/core/context/service-deps.js +2 -2
  54. package/dist/core/context/service.js +8 -0
  55. package/dist/core/context/steps/context-gather.js +38 -0
  56. package/dist/core/context/summarization/summarizer.js +55 -12
  57. package/dist/core/context/targeting/target-resolver.js +4 -4
  58. package/dist/core/extensions/index.js +23 -5
  59. package/dist/core/extensions/merge.js +14 -0
  60. package/dist/core/extensions/paths.js +31 -0
  61. package/dist/core/extensions/schemas.js +8 -5
  62. package/dist/core/facades/cli-chat.js +6 -2
  63. package/dist/core/facades/cli-command-chat.js +1 -0
  64. package/dist/core/facades/cli-command-tool-names.js +2 -0
  65. package/dist/core/facades/cli-observability.js +1 -1
  66. package/dist/core/facades/cli-program-bootstrap.js +1 -0
  67. package/dist/core/facades/cli-run-handler.js +4 -2
  68. package/dist/core/facades/cli-run-persist-session.js +1 -0
  69. package/dist/core/facades/cli-serve.js +4 -4
  70. package/dist/core/facades/cli-utils-worktree.js +1 -1
  71. package/dist/core/failure/diagnostics.js +53 -1
  72. package/dist/core/grizzco/dsl/llm-strategy.js +4 -1
  73. package/dist/core/grizzco/engine/outcome/loop-result-mapper.js +67 -9
  74. package/dist/core/grizzco/engine/pipeline/pipeline.js +6 -2
  75. package/dist/core/grizzco/engine/transaction/attempt-failure.js +90 -15
  76. package/dist/core/grizzco/engine/transaction/report-mapper.js +17 -3
  77. package/dist/core/grizzco/engine/transaction/transaction-runner.js +165 -7
  78. package/dist/core/grizzco/flows/AutopilotFlow.js +18 -0
  79. package/dist/core/grizzco/flows/flow-dispatch.js +11 -0
  80. package/dist/core/grizzco/steps/answer.js +13 -14
  81. package/dist/core/grizzco/steps/autopilot.js +396 -0
  82. package/dist/core/grizzco/steps/cache-sharing.js +29 -0
  83. package/dist/core/grizzco/steps/explore.js +37 -21
  84. package/dist/core/grizzco/steps/generateReview.js +2 -5
  85. package/dist/core/grizzco/steps/patch/apply-check.js +10 -0
  86. package/dist/core/grizzco/steps/patch/diff-normalization.js +70 -0
  87. package/dist/core/grizzco/steps/patch/diff-salvage.js +46 -0
  88. package/dist/core/grizzco/steps/patch/prompt-input.js +42 -0
  89. package/dist/core/grizzco/steps/patch.js +105 -146
  90. package/dist/core/grizzco/steps/plan.js +101 -25
  91. package/dist/core/grizzco/steps/preflight.js +5 -6
  92. package/dist/core/grizzco/steps/request-assembly.js +78 -0
  93. package/dist/core/grizzco/steps/research.js +39 -36
  94. package/dist/core/grizzco/steps/tool-runtime.js +47 -0
  95. package/dist/core/grizzco/steps/verify-shared.js +23 -0
  96. package/dist/core/grizzco/steps/verify.js +13 -21
  97. package/dist/core/interaction/orchestration/facade.js +1 -1
  98. package/dist/core/llm/ai-sdk/chat-executor.js +2 -0
  99. package/dist/core/llm/ai-sdk/high-level-phase-specs.js +63 -0
  100. package/dist/core/llm/ai-sdk/message-mapper.js +40 -10
  101. package/dist/core/llm/ai-sdk/provider-factory.js +14 -0
  102. package/dist/core/llm/ai-sdk/request-params.js +113 -1
  103. package/dist/core/llm/ai-sdk/result-mapper.js +16 -0
  104. package/dist/core/llm/ai-sdk.js +112 -27
  105. package/dist/core/llm/capabilities.js +12 -0
  106. package/dist/core/llm/contracts/repair.js +36 -30
  107. package/dist/core/llm/errors.js +83 -2
  108. package/dist/core/llm/message-composition.js +7 -22
  109. package/dist/core/llm/phase-router.js +29 -10
  110. package/dist/core/llm/redact.js +28 -3
  111. package/dist/core/llm/registry.js +2 -0
  112. package/dist/core/llm/request-augmentation.js +55 -0
  113. package/dist/core/llm/request-envelope.js +334 -0
  114. package/dist/core/llm/shared-request-assembly.js +35 -0
  115. package/dist/core/llm/stream-utils.js +13 -4
  116. package/dist/core/llm/utils.js +18 -29
  117. package/dist/core/memory/relevant-retrieval.js +144 -0
  118. package/dist/core/observability/logger.js +11 -2
  119. package/dist/core/patch/diff.js +1 -0
  120. package/dist/core/prompts/registry.js +39 -2
  121. package/dist/core/prompts/runtime.js +50 -12
  122. package/dist/core/prompts/templates/phases/patch_user.hbs +2 -5
  123. package/dist/core/prompts/templates/phases/research_user.hbs +11 -0
  124. package/dist/core/prompts/templates/phases/review_user.hbs +3 -0
  125. package/dist/core/prompts/templates/system/answer_system.hbs +5 -0
  126. package/dist/core/prompts/templates/system/autopilot_system.hbs +11 -0
  127. package/dist/core/prompts/templates/system/explore_system.hbs +14 -23
  128. package/dist/core/prompts/templates/system/main_system.hbs +4 -16
  129. package/dist/core/prompts/templates/system/patch_system.hbs +39 -8
  130. package/dist/core/prompts/templates/system/plan_system.hbs +86 -1
  131. package/dist/core/prompts/templates/system/research_system.hbs +2 -0
  132. package/dist/core/protocols/a2a/agent-card.js +5 -3
  133. package/dist/core/protocols/a2a/sdk/executor.js +2 -1
  134. package/dist/core/protocols/a2a/sdk/server.js +0 -1
  135. package/dist/core/protocols/acp/formal-agent.js +300 -58
  136. package/dist/core/protocols/acp/handlers.js +5 -1
  137. package/dist/core/protocols/acp/permission-provider.js +1 -1
  138. package/dist/core/protocols/shared/flow-mode-mapping.js +23 -0
  139. package/dist/core/public-capabilities/flow-mode-metadata.js +39 -0
  140. package/dist/core/public-capabilities/projections.js +29 -0
  141. package/dist/core/public-capabilities/registry.js +26 -0
  142. package/dist/core/public-capabilities/types.js +2 -0
  143. package/dist/core/runtime/agent-server-runtime.js +47 -43
  144. package/dist/core/runtime/execution-profile.js +67 -0
  145. package/dist/core/session/artifact-state.js +160 -0
  146. package/dist/core/session/compaction/index.js +183 -0
  147. package/dist/core/session/compaction/microcompact.js +78 -0
  148. package/dist/core/session/compaction/tracking.js +48 -0
  149. package/dist/core/session/compaction/types.js +11 -0
  150. package/dist/core/session/compression.js +8 -0
  151. package/dist/core/session/manager.js +244 -8
  152. package/dist/core/session/pruning-strategy.js +55 -9
  153. package/dist/core/session/replacement-preview-provider.js +24 -0
  154. package/dist/core/session/replacement-state.js +131 -0
  155. package/dist/core/session/resume-repair/pipeline.js +79 -0
  156. package/dist/core/session/resume-repair/stages/load-raw-archive-state.js +40 -0
  157. package/dist/core/session/resume-repair/stages/reattach-runtime-state.js +8 -0
  158. package/dist/core/session/resume-repair/stages/recover-orphaned-branches.js +10 -0
  159. package/dist/core/session/resume-repair/stages/relink-boundary-and-tail.js +36 -0
  160. package/dist/core/session/resume-repair/stages/replay-startup-hooks.js +23 -0
  161. package/dist/core/session/resume-repair/stages/rescue-stale-metadata.js +17 -0
  162. package/dist/core/session/resume-repair/types.js +2 -0
  163. package/dist/core/session/summary-sync.js +164 -13
  164. package/dist/core/session/token-tracker.js +6 -0
  165. package/dist/core/skills/audit.js +34 -0
  166. package/dist/core/skills/bridge.js +84 -7
  167. package/dist/core/skills/discovery.js +94 -0
  168. package/dist/core/skills/feature-flags.js +52 -0
  169. package/dist/core/skills/index.js +1 -1
  170. package/dist/core/skills/loader.js +195 -20
  171. package/dist/core/skills/parser.js +296 -24
  172. package/dist/core/skills/permissions.js +117 -0
  173. package/dist/core/skills/runtime/MicroTaskRunner.js +10 -4
  174. package/dist/core/skills/runtime/SkillRunner.js +240 -61
  175. package/dist/core/strata/layers/shadow-driver/shadow-driver.js +37 -7
  176. package/dist/core/strata/layers/worktree.js +67 -10
  177. package/dist/core/strata/runtime/synchronizer.js +29 -2
  178. package/dist/core/streaming/stream-assembler.js +75 -31
  179. package/dist/core/sub-agent/context-snapshot.js +156 -0
  180. package/dist/core/sub-agent/core/loop.js +1 -1
  181. package/dist/core/sub-agent/core/manager.js +119 -20
  182. package/dist/core/sub-agent/dispatch-policy.js +29 -0
  183. package/dist/core/sub-agent/prefix-consistency.js +48 -0
  184. package/dist/core/sub-agent/registry-defaults.js +4 -0
  185. package/dist/core/sub-agent/tools/task-spawn.js +79 -2
  186. package/dist/core/sub-agent/types.js +134 -5
  187. package/dist/core/tools/audit.js +13 -4
  188. package/dist/core/tools/builtin/ast-grep.js +1 -1
  189. package/dist/core/tools/builtin/ast.js +1 -1
  190. package/dist/core/tools/builtin/benchmark.js +360 -0
  191. package/dist/core/tools/builtin/code-search/backends/rg.js +2 -1
  192. package/dist/core/tools/builtin/code-search/executor.js +6 -1
  193. package/dist/core/tools/builtin/code-search/spec.js +26 -2
  194. package/dist/core/tools/builtin/fs.js +256 -23
  195. package/dist/core/tools/builtin/git.js +2 -2
  196. package/dist/core/tools/builtin/index.js +51 -2
  197. package/dist/core/tools/builtin/interaction.js +8 -1
  198. package/dist/core/tools/builtin/plan.js +37 -15
  199. package/dist/core/tools/builtin/shell.js +1 -1
  200. package/dist/core/tools/loader.js +39 -16
  201. package/dist/core/tools/mapper.js +17 -3
  202. package/dist/core/tools/mcp/client.js +2 -1
  203. package/dist/core/tools/parallel/scheduler.js +35 -4
  204. package/dist/core/tools/permissions/permission-rules.js +5 -10
  205. package/dist/core/tools/policy.js +6 -1
  206. package/dist/core/tools/recoverable-tool-errors.js +10 -0
  207. package/dist/core/tools/router.js +24 -6
  208. package/dist/core/tools/session.js +458 -48
  209. package/dist/core/tools/tool-visibility.js +62 -0
  210. package/dist/core/tools/types.js +9 -1
  211. package/dist/core/types/execution.js +4 -0
  212. package/dist/core/types/flow-mode.js +8 -0
  213. package/dist/core/utils/path.js +52 -0
  214. package/dist/core/verification/runner.js +4 -1
  215. package/dist/core/version.js +17 -0
  216. package/dist/languages/typescript/index.js +4 -1
  217. package/dist/locales/en.js +35 -2
  218. package/dist/utils/eol.js +1 -1
  219. package/package.json +14 -7
  220. package/scripts/fix-es-abstract-compat.js +77 -0
  221. package/dist/core/runtime/fastify-server-bundle.js +0 -26
  222. package/dist/core/runtime/sidecar-fastify-plugin.js +0 -35
  223. package/dist/core/runtime/sidecar-paths.js +0 -47
  224. package/dist/core/runtime/sidecar-route-catalog.js +0 -103
@@ -129,12 +129,32 @@ export function toAiSdkMessages(messages) {
129
129
  ],
130
130
  };
131
131
  }
132
- if (m.role === 'assistant' && Array.isArray(m.tool_calls) && m.tool_calls.length > 0) {
132
+ if (m.role === 'assistant') {
133
+ const hasToolCalls = Array.isArray(m.tool_calls) && m.tool_calls.length > 0;
134
+ const reasoningContent = typeof m.reasoning_content === 'string' && m.reasoning_content.length > 0
135
+ ? m.reasoning_content
136
+ : undefined;
137
+ if (!hasToolCalls && !reasoningContent) {
138
+ let content = m.content;
139
+ if (content === undefined || content === null) {
140
+ content = '';
141
+ }
142
+ if (typeof content !== 'string') {
143
+ content = JSON.stringify(content);
144
+ }
145
+ return {
146
+ role: m.role,
147
+ content: content,
148
+ };
149
+ }
133
150
  const parts = [];
151
+ if (reasoningContent) {
152
+ parts.push({ type: 'reasoning', text: reasoningContent });
153
+ }
134
154
  if (m.content && typeof m.content === 'string') {
135
155
  parts.push({ type: 'text', text: m.content });
136
156
  }
137
- for (const call of m.tool_calls) {
157
+ for (const call of hasToolCalls ? m.tool_calls || [] : []) {
138
158
  const toolCallId = call?.id || 'unknown';
139
159
  const toolName = call?.function?.name || call?.name || 'unknown';
140
160
  const rawArgs = call?.function?.arguments;
@@ -143,11 +163,15 @@ export function toAiSdkMessages(messages) {
143
163
  ? safeParseJsonObject(rawArgs)
144
164
  : {}
145
165
  : (call?.input ?? call?.args ?? {});
166
+ const providerOptions = isObjectRecord(call?.providerMetadata)
167
+ ? deepCloneJson(call.providerMetadata, {})
168
+ : undefined;
146
169
  parts.push({
147
170
  type: 'tool-call',
148
171
  toolCallId,
149
172
  toolName,
150
173
  input: deepCloneJson(input, {}),
174
+ ...(providerOptions ? { providerOptions } : {}),
151
175
  });
152
176
  }
153
177
  return {
@@ -228,13 +252,19 @@ export function toOpenAiToolCalls(toolCalls) {
228
252
  return raw;
229
253
  }
230
254
  };
231
- return toolCalls.map((c) => ({
232
- id: c?.toolCallId || c?.id || 'unknown',
233
- type: 'function',
234
- function: {
235
- name: c?.toolName || c?.name || 'unknown',
236
- arguments: JSON.stringify(normalizeToolInput(c?.input ?? c?.args ?? {})),
237
- },
238
- }));
255
+ return toolCalls.map((c) => {
256
+ const providerMetadata = isObjectRecord(c?.providerMetadata)
257
+ ? deepCloneJson(c.providerMetadata, {})
258
+ : undefined;
259
+ return {
260
+ id: c?.toolCallId || c?.id || 'unknown',
261
+ type: 'function',
262
+ function: {
263
+ name: c?.toolName || c?.name || 'unknown',
264
+ arguments: JSON.stringify(normalizeToolInput(c?.input ?? c?.args ?? {})),
265
+ },
266
+ ...(providerMetadata ? { providerMetadata } : {}),
267
+ };
268
+ });
239
269
  }
240
270
  //# sourceMappingURL=message-mapper.js.map
@@ -1,9 +1,23 @@
1
1
  import { createOpenAI } from '@ai-sdk/openai';
2
2
  import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
3
3
  import { resolveBaseUrl } from '../base-url.js';
4
+ function toProviderOptionsKey(value) {
5
+ const normalized = value.trim();
6
+ if (!normalized)
7
+ return 'openaiCompatible';
8
+ return normalized
9
+ .replace(/[-_\s]+(.)?/g, (_, char) => (char ? char.toUpperCase() : ''))
10
+ .replace(/^[A-Z]/, (char) => char.toLowerCase());
11
+ }
4
12
  export function resolveAiSdkModelId(modelId) {
5
13
  return modelId || process.env.SALMONLOOP_MODEL || process.env.S8P_MODEL || 'gpt-4o';
6
14
  }
15
+ export function resolveAiSdkProviderOptionsKey(cfg) {
16
+ if (cfg.clientPackage === '@ai-sdk/openai') {
17
+ return 'openai';
18
+ }
19
+ return toProviderOptionsKey(cfg.providerName || 'openaiCompatible');
20
+ }
7
21
  export function createAiSdkChatModel(cfg, modelId) {
8
22
  if (cfg.clientPackage === '@ai-sdk/openai') {
9
23
  const provider = createOpenAI({
@@ -1,16 +1,128 @@
1
+ function isRecord(value) {
2
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
3
+ }
4
+ function isJsonValue(value) {
5
+ if (value === null ||
6
+ typeof value === 'string' ||
7
+ typeof value === 'number' ||
8
+ typeof value === 'boolean') {
9
+ return true;
10
+ }
11
+ if (Array.isArray(value)) {
12
+ return value.every((item) => isJsonValue(item));
13
+ }
14
+ if (!isRecord(value)) {
15
+ return false;
16
+ }
17
+ return Object.values(value).every((item) => isJsonValue(item));
18
+ }
19
+ function toJsonObject(value) {
20
+ if (!isRecord(value))
21
+ return undefined;
22
+ const out = {};
23
+ for (const [key, entry] of Object.entries(value)) {
24
+ if (!isJsonValue(entry))
25
+ return undefined;
26
+ out[key] = entry;
27
+ }
28
+ return out;
29
+ }
30
+ function buildOpenAICacheHintFromPolicy(policy) {
31
+ if (!policy || policy.eligibility !== 'eligible')
32
+ return undefined;
33
+ if (!policy.contextHash || !policy.cacheSafeFingerprint)
34
+ return undefined;
35
+ const namespace = typeof policy.namespace === 'string' && policy.namespace.trim()
36
+ ? policy.namespace
37
+ : 'request-envelope';
38
+ const components = [policy.contextHash, `stable:${policy.cacheSafeFingerprint}`];
39
+ if (policy.mode === 'strict_full_prompt' && policy.lateInjectionFingerprint) {
40
+ components.push(`late:${policy.lateInjectionFingerprint}`);
41
+ }
42
+ return `cache:${JSON.stringify({ namespace, components })}`;
43
+ }
44
+ function mergeProviderOptions(params) {
45
+ const merged = isRecord(params.providerOptions)
46
+ ? { ...params.providerOptions }
47
+ : {};
48
+ const cacheHint = params.providerHints?.openAICacheHint ??
49
+ buildOpenAICacheHintFromPolicy(params.providerHints?.openAICachePolicy);
50
+ if (cacheHint) {
51
+ const existing = toJsonObject(merged[params.providerOptionsKey]) ?? {};
52
+ if (typeof existing.user !== 'string' || !existing.user.trim()) {
53
+ existing.user = cacheHint;
54
+ }
55
+ merged[params.providerOptionsKey] = existing;
56
+ }
57
+ return Object.keys(merged).length > 0 ? merged : undefined;
58
+ }
59
+ function resolveResponseFormat(options) {
60
+ if (options.responseFormat === 'json_object') {
61
+ return options.responseFormatJsonObjectSupported === false ? undefined : { type: 'json' };
62
+ }
63
+ if (options.responseFormat === 'text') {
64
+ return { type: 'text' };
65
+ }
66
+ return undefined;
67
+ }
68
+ function stringifySystemContent(content) {
69
+ if (typeof content === 'string')
70
+ return content;
71
+ if (content === undefined || content === null)
72
+ return '';
73
+ if (Array.isArray(content)) {
74
+ return content
75
+ .map((part) => {
76
+ if (typeof part === 'string')
77
+ return part;
78
+ if (isRecord(part) && typeof part.text === 'string')
79
+ return part.text;
80
+ return '';
81
+ })
82
+ .filter((part) => part.length > 0)
83
+ .join('\n');
84
+ }
85
+ return String(content);
86
+ }
87
+ function splitSystemMessages(messages) {
88
+ const systemParts = [];
89
+ const conversationMessages = [];
90
+ for (const message of messages) {
91
+ if (isRecord(message) && message.role === 'system') {
92
+ const content = stringifySystemContent(message.content).trim();
93
+ if (content) {
94
+ systemParts.push(content);
95
+ }
96
+ continue;
97
+ }
98
+ conversationMessages.push(message);
99
+ }
100
+ return {
101
+ system: systemParts.length > 0 ? systemParts.join('\n\n') : undefined,
102
+ messages: conversationMessages,
103
+ };
104
+ }
1
105
  export function buildAiSdkRequestParams(params) {
106
+ const splitMessages = splitSystemMessages(params.messages);
2
107
  return {
3
108
  model: params.model,
4
- messages: params.messages,
109
+ system: splitMessages.system,
110
+ messages: splitMessages.messages,
5
111
  tools: params.tools,
6
112
  temperature: params.options.temperature,
7
113
  maxOutputTokens: params.options.maxTokens != null ? Number(params.options.maxTokens) : undefined,
8
114
  stopSequences: params.options.stop,
115
+ responseFormat: resolveResponseFormat(params.options),
9
116
  toolChoice: (params.options.toolChoice === 'none'
10
117
  ? 'none'
11
118
  : params.tools
12
119
  ? 'auto'
13
120
  : undefined),
121
+ providerOptions: mergeProviderOptions({
122
+ providerOptions: params.options.providerOptions,
123
+ providerHints: params.options.providerHints,
124
+ providerOptionsKey: params.providerOptionsKey,
125
+ }),
14
126
  headers: params.headers,
15
127
  abortSignal: params.abortSignal,
16
128
  };
@@ -1,9 +1,25 @@
1
1
  import { mapAiSdkStreamPartToChunk } from '../stream-utils.js';
2
2
  import { toOpenAiToolCalls } from './message-mapper.js';
3
+ function extractReasoningContent(result) {
4
+ if (typeof result?.reasoningText === 'string' && result.reasoningText.length > 0) {
5
+ return result.reasoningText;
6
+ }
7
+ const reasoningParts = Array.isArray(result?.reasoning)
8
+ ? result.reasoning
9
+ : Array.isArray(result?.content)
10
+ ? result.content.filter((part) => part?.type === 'reasoning')
11
+ : [];
12
+ const text = reasoningParts
13
+ .map((part) => (typeof part?.text === 'string' ? part.text : ''))
14
+ .join('');
15
+ return text.length > 0 ? text : undefined;
16
+ }
3
17
  export function mapAiSdkGenerateResultToMessage(result) {
18
+ const reasoningContent = extractReasoningContent(result);
4
19
  return {
5
20
  role: 'assistant',
6
21
  content: result?.text || '',
22
+ ...(reasoningContent ? { reasoning_content: reasoningContent } : {}),
7
23
  tool_calls: toOpenAiToolCalls(result?.toolCalls),
8
24
  };
9
25
  }
@@ -1,59 +1,101 @@
1
1
  import { randomUUID } from 'crypto';
2
- import { LIMITS } from '../config/limits.js';
3
- import { getPatchPrompt, getPlanPrompt } from '../prompts/runtime.js';
4
2
  import { executeAiSdkChatRequest, executeAiSdkChatStreamRequest } from './ai-sdk/chat-executor.js';
3
+ import { HIGH_LEVEL_PHASE_SPECS, } from './ai-sdk/high-level-phase-specs.js';
5
4
  import { toAiSdkMessages, toAiSdkToolSet } from './ai-sdk/message-mapper.js';
6
5
  import { withAuditObservationName } from './ai-sdk/observation-context.js';
7
- import { createAiSdkChatModel, resolveAiSdkModelId } from './ai-sdk/provider-factory.js';
8
- import { wrapPlanEmpty, sanitizeError, LlmError } from './errors.js';
9
- import { extractUnifiedDiffFromLLMContent, formatContextForPrompt, parsePlanFromLLMContent, } from './utils.js';
6
+ import { createAiSdkChatModel, resolveAiSdkModelId, resolveAiSdkProviderOptionsKey, } from './ai-sdk/provider-factory.js';
7
+ import { repairToJsonObject } from './contracts/repair.js';
8
+ import { buildSharedRequestEnvelope } from './shared-request-assembly.js';
9
+ import { formatContextForPrompt } from './utils.js';
10
10
  export class AiSdkLLM {
11
11
  cfg;
12
12
  model;
13
13
  modelId;
14
+ providerOptionsKey;
14
15
  timeoutMs;
15
16
  constructor(cfg) {
16
17
  this.cfg = cfg;
17
18
  this.modelId = resolveAiSdkModelId(cfg.modelId);
19
+ this.providerOptionsKey = resolveAiSdkProviderOptionsKey(cfg);
18
20
  this.timeoutMs = cfg.timeoutMs;
19
21
  this.model = createAiSdkChatModel(cfg, this.modelId);
20
22
  }
21
23
  getModelId() {
22
24
  return this.modelId;
23
25
  }
24
- getCapabilities() {
26
+ getCapabilities(_options) {
25
27
  return {
26
28
  toolCalling: true,
27
29
  responseFormatJsonObject: true,
28
30
  streaming: true,
31
+ ...this.cfg.capabilities,
29
32
  };
30
33
  }
34
+ applyCapabilityOptions(options = {}) {
35
+ const capabilities = this.getCapabilities({ phase: options.phase });
36
+ const requestOptions = {
37
+ ...options,
38
+ responseFormatJsonObjectSupported: capabilities.responseFormatJsonObject !== false,
39
+ };
40
+ if (capabilities.toolCalling === false) {
41
+ requestOptions.tools = undefined;
42
+ requestOptions.toolSpecs = undefined;
43
+ requestOptions.toolChoice = 'none';
44
+ }
45
+ return requestOptions;
46
+ }
47
+ async *chatStreamFromChat(messages, options = {}) {
48
+ const response = await this.chat(messages, options);
49
+ if (response.reasoning_content) {
50
+ yield {
51
+ role: 'assistant',
52
+ source: 'synthesized',
53
+ reasoningDelta: response.reasoning_content,
54
+ };
55
+ }
56
+ if (response.content) {
57
+ yield { role: 'assistant', source: 'synthesized', contentDelta: response.content };
58
+ }
59
+ if (Array.isArray(response.tool_calls) && response.tool_calls.length > 0) {
60
+ yield { role: 'assistant', source: 'synthesized', tool_calls: response.tool_calls };
61
+ }
62
+ yield { role: 'assistant', source: 'synthesized', done: true, finishReason: 'stop' };
63
+ }
31
64
  async chat(messages, options = {}) {
32
65
  const aiMessages = toAiSdkMessages(messages);
33
- const tools = toAiSdkToolSet(options.tools, options.toolSpecs);
66
+ const requestOptions = this.applyCapabilityOptions(options);
67
+ const tools = toAiSdkToolSet(requestOptions.tools, requestOptions.toolSpecs);
34
68
  return executeAiSdkChatRequest({
35
69
  model: this.model,
36
70
  modelId: this.modelId,
71
+ providerOptionsKey: this.providerOptionsKey,
37
72
  timeoutMs: this.timeoutMs,
38
73
  langfuseEnabled: Boolean(this.cfg.langfuseEnabled),
39
74
  requestId: randomUUID(),
40
75
  messages: aiMessages,
41
76
  tools,
42
- options,
77
+ options: requestOptions,
43
78
  });
44
79
  }
45
80
  async *chatStream(messages, options = {}) {
81
+ const requestOptions = this.applyCapabilityOptions(options);
82
+ const capabilities = this.getCapabilities({ phase: requestOptions.phase });
83
+ if (capabilities.streaming === false) {
84
+ yield* this.chatStreamFromChat(messages, requestOptions);
85
+ return;
86
+ }
46
87
  const aiMessages = toAiSdkMessages(messages);
47
- const tools = toAiSdkToolSet(options.tools, options.toolSpecs);
88
+ const tools = toAiSdkToolSet(requestOptions.tools, requestOptions.toolSpecs);
48
89
  yield* executeAiSdkChatStreamRequest({
49
90
  model: this.model,
50
91
  modelId: this.modelId,
92
+ providerOptionsKey: this.providerOptionsKey,
51
93
  timeoutMs: this.timeoutMs,
52
94
  langfuseEnabled: Boolean(this.cfg.langfuseEnabled),
53
95
  requestId: randomUUID(),
54
96
  messages: aiMessages,
55
97
  tools,
56
- options,
98
+ options: requestOptions,
57
99
  });
58
100
  }
59
101
  /**
@@ -66,27 +108,70 @@ export class AiSdkLLM {
66
108
  yield* this.chatStream(messages, options);
67
109
  }
68
110
  async createPlan(context, instruction, lastError, signal) {
69
- const prompt = await getPlanPrompt(formatContextForPrompt(context), instruction, LIMITS.maxFilesChanged, lastError);
70
- const response = await withAuditObservationName('PLAN:plan-json', async () => this.chat([{ role: 'user', content: prompt }], { signal }));
71
- const content = response.content;
72
- if (!content) {
73
- throw wrapPlanEmpty();
111
+ return this.runHighLevelPhase(HIGH_LEVEL_PHASE_SPECS.plan, {
112
+ context,
113
+ instruction,
114
+ lastError,
115
+ signal,
116
+ });
117
+ }
118
+ async createPatch(context, plan, lastError, signal) {
119
+ const planStr = JSON.stringify(plan, null, 2);
120
+ return this.runHighLevelPhase(HIGH_LEVEL_PHASE_SPECS.patch, {
121
+ context,
122
+ planStr,
123
+ lastError,
124
+ signal,
125
+ });
126
+ }
127
+ async runHighLevelPhase(spec, input) {
128
+ const contextPrompt = formatContextForPrompt(input.context);
129
+ const userPrompt = await spec.buildPrompt({ ...input, contextPrompt });
130
+ const attachments = spec.buildAttachments({ ...input, contextPrompt });
131
+ const content = await this.executeHighLevelPrompt({
132
+ phase: spec.name,
133
+ defaultNamespace: spec.namespace,
134
+ contextHash: input.context.contextHash,
135
+ userPrompt,
136
+ attachments,
137
+ observationName: spec.observationName,
138
+ signal: input.signal,
139
+ });
140
+ return spec.parseResult(content);
141
+ }
142
+ async executeHighLevelPrompt(params) {
143
+ const sharedEnvelope = buildSharedRequestEnvelope({
144
+ defaultNamespace: params.defaultNamespace,
145
+ contextHash: params.contextHash,
146
+ systemPrompt: '',
147
+ userPrompt: params.userPrompt,
148
+ attachments: params.attachments,
149
+ });
150
+ const response = await withAuditObservationName(params.observationName, async () => this.chat(sharedEnvelope.baseMessages, {
151
+ providerHints: sharedEnvelope.envelope.providerHints,
152
+ responseFormat: params.phase === 'plan' ? 'json_object' : undefined,
153
+ signal: params.signal,
154
+ }));
155
+ if (params.phase !== 'plan') {
156
+ return response.content;
74
157
  }
75
158
  try {
76
- return parsePlanFromLLMContent(content);
159
+ HIGH_LEVEL_PHASE_SPECS.plan.parseResult(response.content);
160
+ return response.content;
77
161
  }
78
- catch (e) {
79
- throw new LlmError('LLM plan parsing failed', 'LLM_PLAN_INVALID_JSON', {
80
- causeMessage: sanitizeError(e),
81
- });
162
+ catch (error) {
163
+ const repair = await withAuditObservationName('PLAN:plan-json-repair', async () => repairToJsonObject({
164
+ llm: this,
165
+ baseMessages: sharedEnvelope.baseMessages,
166
+ chatOptions: {
167
+ providerHints: sharedEnvelope.envelope.providerHints,
168
+ signal: params.signal,
169
+ },
170
+ badContent: response.content ?? '',
171
+ reason: error instanceof Error ? error.message : String(error),
172
+ }));
173
+ return repair.content;
82
174
  }
83
175
  }
84
- async createPatch(context, plan, lastError, signal) {
85
- const planStr = JSON.stringify(plan, null, 2);
86
- const formattedContext = formatContextForPrompt(context);
87
- const prompt = await getPatchPrompt(planStr, formattedContext, LIMITS.maxFilesChanged, LIMITS.maxDiffLines, lastError);
88
- const response = await withAuditObservationName('PATCH:unified-diff', async () => this.chat([{ role: 'user', content: prompt }], { signal }));
89
- return extractUnifiedDiffFromLLMContent(response.content || '');
90
- }
91
176
  }
92
177
  //# sourceMappingURL=ai-sdk.js.map
@@ -0,0 +1,12 @@
1
+ export function resolveLlmCapabilities(llm, phase) {
2
+ return llm.getCapabilities?.({ phase }) ?? {};
3
+ }
4
+ export function supportsLlmStreaming(llm, phase) {
5
+ const capabilities = resolveLlmCapabilities(llm, phase);
6
+ if (capabilities.streaming === false)
7
+ return false;
8
+ if (capabilities.streaming === true)
9
+ return typeof llm.chatStream === 'function';
10
+ return typeof llm.chatStream === 'function';
11
+ }
12
+ //# sourceMappingURL=capabilities.js.map
@@ -13,10 +13,11 @@ export async function repairToJsonObject(args) {
13
13
  'Your previous response did not satisfy the contract.',
14
14
  `Reason: ${reason}`,
15
15
  '',
16
- 'Return ONLY a single JSON object.',
17
- '- No Markdown fences.',
18
- '- No commentary.',
19
- '- No leading/trailing text.',
16
+ 'Return exactly one JSON object and nothing else.',
17
+ 'The first non-whitespace character must be {.',
18
+ 'The last non-whitespace character must be }.',
19
+ '',
20
+ 'Forbidden: Markdown fences, commentary, labels, multiple objects, or any leading/trailing text.',
20
21
  '',
21
22
  'The JSON object MUST include keys: goal, files, changes, verify.',
22
23
  '',
@@ -38,31 +39,36 @@ export async function repairToJsonObject(args) {
38
39
  });
39
40
  }
40
41
  export async function repairToUnifiedDiff(args) {
41
- const { llm, baseMessages, chatOptions, badContent, reason } = args;
42
- const prompt = [
43
- 'Your previous response did not satisfy the contract.',
44
- `Reason: ${reason}`,
45
- '',
46
- 'Return ONLY a standard git unified diff patch.',
47
- '- It MUST start with `diff --git`.',
48
- '- No Markdown fences.',
49
- '- No commentary.',
50
- '- Exactly one final patch block (no multiple alternatives).',
51
- '',
52
- 'Previous response (truncated):',
53
- truncateForPrompt(badContent, Math.min(1200, Math.max(400, LIMITS.maxContextChars / 100))),
54
- ].join('\n');
55
- return llm.chat([
56
- ...baseMessages,
57
- { role: 'assistant', content: badContent || '' },
58
- { role: 'user', content: prompt },
59
- ], {
60
- ...chatOptions,
61
- responseFormat: 'text',
62
- tools: undefined,
63
- toolSpecs: undefined,
64
- toolChoice: undefined,
65
- temperature: 0,
66
- });
42
+ const { badContent } = args;
43
+ const extractCanonicalDiff = (input) => {
44
+ if (!input)
45
+ return '';
46
+ const fromText = (value) => {
47
+ const start = value.search(/^\s*diff --git /m);
48
+ if (start === -1)
49
+ return '';
50
+ const section = value.slice(start).trim();
51
+ const fenceClose = section.search(/\n```/);
52
+ if (fenceClose !== -1)
53
+ return section.slice(0, fenceClose).trim();
54
+ return section;
55
+ };
56
+ const fencedBlocks = [];
57
+ const fenceRegex = /```(?:diff)?\s*\n([\s\S]*?)\n```/gi;
58
+ let match = null;
59
+ while ((match = fenceRegex.exec(input)) !== null) {
60
+ const block = match[1];
61
+ const extracted = fromText(block);
62
+ if (extracted)
63
+ fencedBlocks.push(extracted);
64
+ }
65
+ if (fencedBlocks.length > 0)
66
+ return fencedBlocks[fencedBlocks.length - 1];
67
+ return fromText(input);
68
+ };
69
+ return {
70
+ role: 'assistant',
71
+ content: extractCanonicalDiff(badContent || ''),
72
+ };
67
73
  }
68
74
  //# sourceMappingURL=repair.js.map
@@ -26,6 +26,11 @@ function extractProviderDetails(err) {
26
26
  if (typeof candidate.statusCode === 'number') {
27
27
  details.statusCode = candidate.statusCode;
28
28
  }
29
+ // Align with AI SDK error shapes that store HTTP status in response.status
30
+ const response = candidate.response;
31
+ if (typeof details.statusCode !== 'number' && response && typeof response.status === 'number') {
32
+ details.statusCode = response.status;
33
+ }
29
34
  if (typeof candidate.responseBody === 'string') {
30
35
  // Apply sanitization to responseBody immediately after truncation
31
36
  details.responseBody = sanitizeError(truncate(candidate.responseBody));
@@ -75,6 +80,42 @@ function extractProviderDetails(err) {
75
80
  }
76
81
  return details;
77
82
  }
83
+ function extractNetworkCode(err) {
84
+ if (!err || typeof err !== 'object')
85
+ return undefined;
86
+ const candidate = err;
87
+ const direct = candidate.code;
88
+ if (typeof direct === 'string' && direct.trim())
89
+ return direct;
90
+ const cause = candidate.cause;
91
+ if (cause && typeof cause === 'object' && typeof cause.code === 'string') {
92
+ const code = String(cause.code);
93
+ return code.trim() ? code : undefined;
94
+ }
95
+ return undefined;
96
+ }
97
+ function isAuthenticationFailure(input) {
98
+ if (input.statusCode === 401)
99
+ return true;
100
+ const lower = `${input.message} ${input.providerMessage ?? ''} ${input.sanitizedMessage}`.toLowerCase();
101
+ const authHints = [
102
+ 'unauthorized',
103
+ 'forbidden',
104
+ 'authentication failed',
105
+ 'auth failed',
106
+ 'invalid api key',
107
+ 'invalid api-key',
108
+ 'access denied',
109
+ 'permission denied',
110
+ 'credential',
111
+ 'appidnoautherror',
112
+ 'noautherror',
113
+ ];
114
+ if (authHints.some((hint) => lower.includes(hint))) {
115
+ return true;
116
+ }
117
+ return input.statusCode === 403 && /auth|access|permission|credential|forbidden/i.test(lower);
118
+ }
78
119
  /**
79
120
  * Sanitizes an error message using the shared utility to prevent leakage
80
121
  * of sensitive technical data.
@@ -83,8 +124,16 @@ export function sanitizeError(err) {
83
124
  return sanitizeErrorMessage(err);
84
125
  }
85
126
  export function toLlmError(err, provider) {
86
- let name = err instanceof Error ? err.name : 'UnknownError';
87
- let message = err instanceof Error ? err.message : String(err);
127
+ let name = err instanceof Error
128
+ ? err.name
129
+ : typeof err?.name === 'string'
130
+ ? String(err.name)
131
+ : 'UnknownError';
132
+ let message = err instanceof Error
133
+ ? err.message
134
+ : typeof err?.message === 'string'
135
+ ? String(err.message)
136
+ : String(err);
88
137
  // Unwrap RetryError to get the last error's message if available
89
138
  if (name === 'AI_RetryError' || err?.lastError) {
90
139
  const lastError = err.lastError;
@@ -137,6 +186,38 @@ export function toLlmError(err, provider) {
137
186
  })) {
138
187
  return new LlmError('LLM context length exceeded', 'LLM_CONTEXT_LENGTH_EXCEEDED', meta);
139
188
  }
189
+ const lower = `${message} ${meta.providerMessage ?? ''} ${sanitizedMessage}`.toLowerCase();
190
+ const statusCode = meta.statusCode;
191
+ const networkCode = extractNetworkCode(err)?.toUpperCase();
192
+ if (isAuthenticationFailure({
193
+ statusCode,
194
+ message,
195
+ providerMessage: meta.providerMessage,
196
+ sanitizedMessage,
197
+ })) {
198
+ return new LlmError('LLM authentication failed', 'LLM_AUTHENTICATION_FAILED', meta);
199
+ }
200
+ if (statusCode === 429 || lower.includes('rate limit') || lower.includes('too many requests')) {
201
+ return new LlmError('LLM rate limited', 'LLM_RATE_LIMITED', meta);
202
+ }
203
+ if (statusCode === 408 || lower.includes('timeout') || networkCode === 'ETIMEDOUT') {
204
+ return new LlmError('LLM request timed out', 'LLM_REQUEST_TIMEOUT', meta);
205
+ }
206
+ if (typeof statusCode === 'number' && statusCode >= 500 && statusCode < 600) {
207
+ return new LlmError('LLM upstream server error', 'LLM_UPSTREAM_5XX', meta);
208
+ }
209
+ if (typeof networkCode === 'string') {
210
+ const unreachable = new Set([
211
+ 'ECONNRESET',
212
+ 'ETIMEDOUT',
213
+ 'EAI_AGAIN',
214
+ 'ENOTFOUND',
215
+ 'ECONNREFUSED',
216
+ ]);
217
+ if (unreachable.has(networkCode)) {
218
+ return new LlmError('LLM network request failed', 'LLM_NETWORK_UNREACHABLE', meta);
219
+ }
220
+ }
140
221
  return new LlmError('LLM request failed', 'LLM_HTTP_REQUEST_FAILED', meta);
141
222
  }
142
223
  function isContextLengthExceeded(input) {