@denizokcu/haze 0.10.0 → 0.11.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 (169) hide show
  1. package/CHANGELOG.md +77 -1
  2. package/README.md +66 -25
  3. package/SECURITY.md +22 -0
  4. package/bin/haze.js +102 -1
  5. package/dist/buildInfo.json +7 -0
  6. package/dist/cli/chat/TaskBar.d.ts +2 -1
  7. package/dist/cli/chat/TaskBar.js +7 -4
  8. package/dist/cli/chat/contextReport.js +2 -0
  9. package/dist/cli/chat/fileMentionSuggestions.js +3 -2
  10. package/dist/cli/chat/liveRegion.d.ts +25 -0
  11. package/dist/cli/chat/liveRegion.js +48 -0
  12. package/dist/cli/chat/messages.d.ts +33 -2
  13. package/dist/cli/chat/messages.js +216 -33
  14. package/dist/cli/chat/sessionLifecycle.d.ts +9 -0
  15. package/dist/cli/chat/sessionLifecycle.js +64 -14
  16. package/dist/cli/chat/sessionRecorder.js +75 -7
  17. package/dist/cli/chat/startupInfo.d.ts +0 -1
  18. package/dist/cli/chat/startupInfo.js +11 -15
  19. package/dist/cli/chat/wizardDispatch.js +111 -12
  20. package/dist/cli/commands/chat.d.ts +5 -0
  21. package/dist/cli/commands/chat.js +203 -101
  22. package/dist/cli/commands/commands.d.ts +2 -0
  23. package/dist/cli/commands/commands.js +10 -5
  24. package/dist/cli/commands/doctor.d.ts +19 -0
  25. package/dist/cli/commands/doctor.js +76 -0
  26. package/dist/cli/commands/formatters.js +2 -0
  27. package/dist/cli/commands/ndjsonSink.d.ts +23 -0
  28. package/dist/cli/commands/ndjsonSink.js +38 -0
  29. package/dist/cli/commands/providerWizard.d.ts +9 -2
  30. package/dist/cli/commands/providerWizard.js +37 -4
  31. package/dist/cli/commands/runCommand.d.ts +3 -0
  32. package/dist/cli/commands/runCommand.js +117 -11
  33. package/dist/cli/commands/streaming/abortCause.d.ts +24 -0
  34. package/dist/cli/commands/streaming/abortCause.js +16 -0
  35. package/dist/cli/commands/streaming/assistantText.js +9 -2
  36. package/dist/cli/commands/streaming/goalCheckpoint.d.ts +65 -0
  37. package/dist/cli/commands/streaming/goalCheckpoint.js +23 -0
  38. package/dist/cli/commands/streaming/goalSupervisor.d.ts +64 -0
  39. package/dist/cli/commands/streaming/goalSupervisor.js +127 -0
  40. package/dist/cli/commands/streaming/toolCallRecovery.d.ts +2 -0
  41. package/dist/cli/commands/streaming/toolCallRecovery.js +24 -0
  42. package/dist/cli/commands/streaming/toolGroupRenderer.d.ts +29 -0
  43. package/dist/cli/commands/streaming/toolGroupRenderer.js +50 -4
  44. package/dist/cli/commands/streaming/toolResultState.d.ts +6 -1
  45. package/dist/cli/commands/streaming/toolResultState.js +29 -7
  46. package/dist/cli/commands/streaming/turnOutcome.d.ts +17 -0
  47. package/dist/cli/commands/streaming/turnOutcome.js +27 -11
  48. package/dist/cli/commands/streaming.d.ts +70 -3
  49. package/dist/cli/commands/streaming.js +491 -77
  50. package/dist/cli/commands/wizardActions.d.ts +1 -0
  51. package/dist/cli/commands/wizardActions.js +1 -0
  52. package/dist/cli/commands/wizardPrompts.js +7 -1
  53. package/dist/cli/commands/wizardSuggestions.js +5 -3
  54. package/dist/cli/index.js +68 -22
  55. package/dist/cli/terminalTitle.js +1 -1
  56. package/dist/config/modelDiscovery.d.ts +51 -0
  57. package/dist/config/modelDiscovery.js +164 -1
  58. package/dist/config/privateStorage.js +28 -2
  59. package/dist/config/providerAuth.d.ts +14 -0
  60. package/dist/config/providerAuth.js +49 -0
  61. package/dist/config/providerPresets.d.ts +39 -7
  62. package/dist/config/providerPresets.js +371 -100
  63. package/dist/config/providers.js +2 -0
  64. package/dist/config/settings.d.ts +36 -0
  65. package/dist/config/settings.js +9 -0
  66. package/dist/core/agent/budgets.d.ts +10 -0
  67. package/dist/core/agent/budgets.js +10 -0
  68. package/dist/core/agent/compaction.d.ts +38 -0
  69. package/dist/core/agent/compaction.js +92 -17
  70. package/dist/core/agent/completionController.d.ts +230 -0
  71. package/dist/core/agent/completionController.js +287 -0
  72. package/dist/core/agent/contextBudget.d.ts +38 -0
  73. package/dist/core/agent/contextBudget.js +60 -7
  74. package/dist/core/agent/events.d.ts +74 -0
  75. package/dist/core/agent/reasoningPolicy.d.ts +31 -0
  76. package/dist/core/agent/reasoningPolicy.js +27 -0
  77. package/dist/core/agent/toolCapabilities.d.ts +27 -0
  78. package/dist/core/agent/toolCapabilities.js +58 -0
  79. package/dist/core/agent/toolExecutionBudget.d.ts +35 -0
  80. package/dist/core/agent/toolExecutionBudget.js +49 -0
  81. package/dist/core/agent/toolOutputStore.d.ts +8 -0
  82. package/dist/core/agent/toolOutputStore.js +10 -10
  83. package/dist/core/agent/toolResults.d.ts +12 -0
  84. package/dist/core/agent/toolResults.js +38 -0
  85. package/dist/core/agent/turnBudget.d.ts +35 -0
  86. package/dist/core/agent/turnBudget.js +27 -0
  87. package/dist/core/agent/turnPolicy.d.ts +11 -0
  88. package/dist/core/agent/turnPolicy.js +19 -0
  89. package/dist/core/agent/workState.d.ts +75 -0
  90. package/dist/core/agent/workState.js +120 -9
  91. package/dist/core/deadline.d.ts +33 -0
  92. package/dist/core/deadline.js +74 -0
  93. package/dist/core/goal/completionPolicy.d.ts +27 -0
  94. package/dist/core/goal/completionPolicy.js +37 -1
  95. package/dist/core/limits/byteBudgets.d.ts +2 -0
  96. package/dist/core/limits/byteBudgets.js +2 -0
  97. package/dist/core/limits/textBudgets.d.ts +2 -0
  98. package/dist/core/limits/textBudgets.js +2 -0
  99. package/dist/core/log/llmLog.js +7 -0
  100. package/dist/core/persistence/orderedFileWriter.d.ts +10 -0
  101. package/dist/core/persistence/orderedFileWriter.js +16 -2
  102. package/dist/core/process/backgroundRegistry.js +8 -7
  103. package/dist/core/process/runBoundedProcess.d.ts +8 -2
  104. package/dist/core/process/runBoundedProcess.js +15 -6
  105. package/dist/core/safety/missingExecutable.d.ts +42 -0
  106. package/dist/core/safety/missingExecutable.js +77 -0
  107. package/dist/core/session/sessionStore.d.ts +37 -0
  108. package/dist/core/session/sessionStore.js +215 -51
  109. package/dist/core/subagent/contracts.d.ts +4 -0
  110. package/dist/core/subagent/contracts.js +1 -0
  111. package/dist/core/subagent/subagentCoordinator.d.ts +4 -0
  112. package/dist/core/subagent/subagentCoordinator.js +32 -2
  113. package/dist/core/subagent/subagentRunner.js +17 -37
  114. package/dist/llm/client.d.ts +17 -1
  115. package/dist/llm/client.js +82 -14
  116. package/dist/llm/hazeTools.d.ts +554 -80
  117. package/dist/llm/hazeTools.js +103 -24
  118. package/dist/llm/lsp.d.ts +24 -4
  119. package/dist/llm/lsp.js +72 -11
  120. package/dist/llm/lspTools.d.ts +2366 -0
  121. package/dist/llm/lspTools.js +87 -79
  122. package/dist/llm/openaiCodex.d.ts +3 -0
  123. package/dist/llm/openaiCodex.js +47 -0
  124. package/dist/llm/openaiCodexOAuth.d.ts +54 -0
  125. package/dist/llm/openaiCodexOAuth.js +214 -0
  126. package/dist/llm/requestContext.d.ts +3 -0
  127. package/dist/llm/requestContext.js +7 -4
  128. package/dist/llm/systemPrompt.d.ts +11 -0
  129. package/dist/llm/systemPrompt.js +12 -6
  130. package/dist/llm/toolResultTypes.d.ts +8 -1
  131. package/dist/llm/tools/bashTool.d.ts +48 -16
  132. package/dist/llm/tools/bashTool.js +5 -0
  133. package/dist/llm/tools/editMatch.d.ts +14 -0
  134. package/dist/llm/tools/editMatch.js +72 -0
  135. package/dist/llm/tools/failures.d.ts +2 -0
  136. package/dist/llm/tools/failures.js +1 -0
  137. package/dist/llm/tools/fetchTool.d.ts +16 -0
  138. package/dist/llm/tools/fileToolShared.d.ts +4 -3
  139. package/dist/llm/tools/fileToolShared.js +16 -21
  140. package/dist/llm/tools/gitIgnore.d.ts +49 -0
  141. package/dist/llm/tools/gitIgnore.js +129 -0
  142. package/dist/llm/tools/readRecovery.d.ts +10 -0
  143. package/dist/llm/tools/readRecovery.js +135 -0
  144. package/dist/llm/tools/toolContext.d.ts +1 -1
  145. package/dist/llm/tools/toolContext.js +61 -32
  146. package/dist/llm/webFetch.d.ts +17 -0
  147. package/dist/llm/webFetch.js +64 -97
  148. package/dist/ui/codeHighlight.d.ts +4 -0
  149. package/dist/ui/codeHighlight.js +31 -0
  150. package/dist/ui/components/Header.d.ts +1 -1
  151. package/dist/ui/components/MarkdownText.d.ts +16 -2
  152. package/dist/ui/components/MarkdownText.js +244 -34
  153. package/dist/ui/components/TextInput.d.ts +8 -1
  154. package/dist/ui/components/TextInput.js +6 -2
  155. package/dist/ui/theme.d.ts +4 -3
  156. package/dist/ui/theme.js +5 -4
  157. package/dist/utils/buildInfo.d.ts +104 -0
  158. package/dist/utils/buildInfo.js +253 -0
  159. package/dist/utils/fs.d.ts +9 -0
  160. package/dist/utils/fs.js +87 -25
  161. package/dist/utils/openPath.d.ts +2 -2
  162. package/dist/utils/openPath.js +19 -6
  163. package/dist/utils/path.d.ts +2 -0
  164. package/dist/utils/path.js +5 -1
  165. package/dist/utils/utf8.d.ts +22 -0
  166. package/dist/utils/utf8.js +54 -0
  167. package/dist/utils/version.d.ts +9 -3
  168. package/dist/utils/version.js +28 -2
  169. package/package.json +24 -19
@@ -4,6 +4,7 @@ import { SUBAGENT_MAX_STEPS, SUBAGENT_MIN_STEPS, SUBAGENT_SYNTHESIS_RESERVE, SUB
4
4
  import { storeToolOutput } from '../agent/toolOutputStore.js';
5
5
  import { withSyntheticControl } from '../agent/requestAssembly.js';
6
6
  import { toolOnlyStepCount } from '../agent/turnPolicy.js';
7
+ import { createToolExecutionBudget, isToolBudgetBlocked, withToolExecutionBudget } from '../agent/toolExecutionBudget.js';
7
8
  import { assembleWorkerContext, workerTaskMessage } from '../../llm/workerContext.js';
8
9
  import { buildSubagentPrompt, projectContextSection } from '../../llm/systemPrompt.js';
9
10
  import { hazeTools } from '../../llm/hazeTools.js';
@@ -13,28 +14,6 @@ import { COMPATIBILITY_PROFILE, MODE_TOOL_NAMES } from './executionProfiles.js';
13
14
  import { SubagentCoordinator } from './subagentCoordinator.js';
14
15
  import { resolveWorkspacePath, workspaceRelativePath } from '../../utils/path.js';
15
16
  const SYNTHESIS_DIRECTIVE = 'You have reached your tool/step budget. Stop calling tools. Return the requested self-contained deliverable now, including evidence, coverage gaps, changed paths, validation, and precise remaining work. A concise partial deliverable is mandatory and better than an empty response.';
16
- const TOOL_BUDGET_BLOCKED = '__hazeSubagentToolBudgetBlocked';
17
- function withToolExecutionBudget(tools, maxToolCalls, state) {
18
- return Object.fromEntries(Object.entries(tools).map(([name, definition]) => {
19
- if (typeof definition.execute !== 'function')
20
- return [name, definition];
21
- const execute = definition.execute;
22
- return [name, {
23
- ...definition,
24
- execute: (...args) => {
25
- // This check is at the actual execute boundary. Concurrent calls from
26
- // one emitted batch enter synchronously, so no more than the remaining
27
- // budget can reach an underlying tool implementation.
28
- if (state.started >= maxToolCalls) {
29
- state.exceeded = true;
30
- return { ok: false, [TOOL_BUDGET_BLOCKED]: true, error: `Subagent tool-call budget of ${maxToolCalls} exhausted; execution was blocked.` };
31
- }
32
- state.started++;
33
- return execute(...args);
34
- },
35
- }];
36
- }));
37
- }
38
17
  function toolSummary(output) {
39
18
  if (typeof output !== 'object' || output == null)
40
19
  return 'completed';
@@ -112,8 +91,8 @@ export async function runSubagent(taskInput, options) {
112
91
  const validation = [];
113
92
  let usageIn;
114
93
  let usageOut;
115
- const toolBudget = { started: 0, exceeded: false };
116
- const budgetedTools = withToolExecutionBudget(bundle.tools, profile.maxToolCalls, toolBudget);
94
+ const toolBudget = createToolExecutionBudget();
95
+ const budgetedTools = withToolExecutionBudget(bundle.tools, { state: toolBudget, limit: profile.maxToolCalls });
117
96
  const mutationPolicy = options.mutationPolicy;
118
97
  const mutationOwner = mutationPolicy?.createOwner();
119
98
  const toolExecutionContext = {
@@ -128,15 +107,16 @@ export async function runSubagent(taskInput, options) {
128
107
  if (mutationPolicy && mutationOwner && (task.mode === 'implement' || task.mode === 'validate') && !release)
129
108
  return terminalResult(task, runtime, profile, options.deadlineExpired?.() ? 'deadline_exceeded' : 'cancelled', 'Worker cancelled before workspace admission.', options.queueMs);
130
109
  try {
110
+ const { omitMaxOutputTokens, ...providerRequestOptions } = runtime.requestOptions;
131
111
  const result = await generateText({
132
112
  model: runtime.model,
133
113
  instructions: bundle.systemPrompt,
134
114
  messages: [{ role: 'user', content: workerTaskMessage(task) }],
135
115
  tools: budgetedTools,
136
116
  stopWhen: isStepCount(maxSteps),
137
- maxOutputTokens: profile.maxOutputTokens,
117
+ ...(!omitMaxOutputTokens ? { maxOutputTokens: profile.maxOutputTokens } : {}),
138
118
  maxRetries: profile.maxRetries,
139
- ...runtime.requestOptions,
119
+ ...providerRequestOptions,
140
120
  abortSignal: options.abortSignal,
141
121
  runtimeContext: toolExecutionContext,
142
122
  toolsContext: toolsContextFor(budgetedTools, toolExecutionContext),
@@ -156,17 +136,17 @@ export async function runSubagent(taskInput, options) {
156
136
  if (!event.toolCall)
157
137
  return;
158
138
  const output = event.toolOutput.type === 'tool-result' ? event.toolOutput.output : undefined;
159
- if (typeof output === 'object' && output != null && TOOL_BUDGET_BLOCKED in output)
160
- return;
161
- const toolError = 'error' in event.toolOutput ? event.toolOutput.error : 'tool execution failed';
162
- if (toolCallLog.length < profile.maxToolCalls)
163
- toolCallLog.push({ name: event.toolCall.toolName, summary: output === undefined ? `failed: ${String(toolError).slice(0, 120)}` : toolSummary(output), durationMs: event.toolExecutionMs });
164
- const changedPath = changedPathFromOutput(event.toolCall.toolName, output);
165
- if (changedPath)
166
- changedPaths.add(changedPath);
167
- const validationRecord = validationFromOutput(event.toolCall.toolName, output);
168
- if (validationRecord)
169
- validation.push(validationRecord);
139
+ if (!isToolBudgetBlocked(output)) {
140
+ const toolError = 'error' in event.toolOutput ? event.toolOutput.error : 'tool execution failed';
141
+ if (toolCallLog.length < profile.maxToolCalls)
142
+ toolCallLog.push({ name: event.toolCall.toolName, summary: output === undefined ? `failed: ${String(toolError).slice(0, 120)}` : toolSummary(output), durationMs: event.toolExecutionMs });
143
+ const changedPath = changedPathFromOutput(event.toolCall.toolName, output);
144
+ if (changedPath)
145
+ changedPaths.add(changedPath);
146
+ const validationRecord = validationFromOutput(event.toolCall.toolName, output);
147
+ if (validationRecord)
148
+ validation.push(validationRecord);
149
+ }
170
150
  },
171
151
  });
172
152
  usageIn = result.usage.inputTokens ?? usageIn;
@@ -1,5 +1,6 @@
1
- import { readSettings } from '../config/settings.js';
1
+ import { readSettings, type HazeProviderSettings } from '../config/settings.js';
2
2
  import type { ProviderCapabilities, ProviderRequestOptions, WorkerRuntime } from '../core/subagent/contracts.js';
3
+ import { type ResolvedReasoningPolicy } from '../core/agent/reasoningPolicy.js';
3
4
  export type { ProviderCapabilities, ProviderRequestOptions } from '../core/subagent/contracts.js';
4
5
  export interface ModelRuntimeSelection {
5
6
  model: WorkerRuntime['model'];
@@ -8,11 +9,26 @@ export interface ModelRuntimeSelection {
8
9
  }
9
10
  export interface ModelRuntimeConfig {
10
11
  providerName: string;
12
+ providerKind?: HazeProviderSettings['kind'];
11
13
  baseURL: string;
12
14
  modelName: string;
13
15
  cacheKey: string;
14
16
  capabilities: ProviderCapabilities;
17
+ /** Resolved reasoning policy (requested vs effective); observable, no secrets. */
18
+ reasoningPolicy: ResolvedReasoningPolicy;
19
+ /**
20
+ * Effective context window for request budgeting (RH-005). Always set: the
21
+ * user-configured value when present, otherwise a class-aware fallback
22
+ * (128K hosted / 32K local) so `contextWindowSource` can flag the guess.
23
+ */
24
+ contextWindowTokens: number;
25
+ /** Where contextWindowTokens came from: per-model settings, a user-set fallback, or the built-in default. */
26
+ contextWindowSource: 'settings' | 'user-fallback' | 'default-fallback';
27
+ /** Optional output-token limit metadata for request budgeting (RH-005). */
28
+ maxOutputTokens?: number;
15
29
  }
30
+ /** True for loopback/local inference servers, whose effective window is server-configured (often far below the model's). */
31
+ export declare function isLocalProviderUrl(url: string): boolean;
16
32
  export declare function modelWithConfig(session?: {
17
33
  cwd?: string;
18
34
  modelSelector?: string;
@@ -3,6 +3,9 @@ import crypto from 'node:crypto';
3
3
  import { readSettings } from '../config/settings.js';
4
4
  import { activeModel, modelSelector, resolveModelSelector } from '../config/providers.js';
5
5
  import { assertCredentialedEndpointSecure } from '../config/endpointSecurity.js';
6
+ import { resolveReasoningPolicy } from '../core/agent/reasoningPolicy.js';
7
+ import { FALLBACK_CONTEXT_WINDOW_TOKENS, FALLBACK_LOCAL_CONTEXT_TOKENS } from '../core/agent/contextBudget.js';
8
+ import { createChatGptCodexFetch } from './openaiCodex.js';
6
9
  const HAZE_SITE_URL = 'https://denizokcu.github.io/haze/';
7
10
  const HAZE_TITLE = 'Haze';
8
11
  function isOpenRouter(providerName, baseURL) {
@@ -16,8 +19,8 @@ function openRouterHeaders(providerName, baseURL) {
16
19
  'X-Title': HAZE_TITLE,
17
20
  };
18
21
  }
19
- function capabilities(providerName, baseURL) {
20
- const directOpenAI = providerName === 'openai' || /api\.openai\.com/i.test(baseURL);
22
+ function capabilities(providerName, baseURL, providerKind) {
23
+ const directOpenAI = providerKind !== 'chatgpt-codex' && (providerName === 'openai' || /api\.openai\.com/i.test(baseURL));
21
24
  const openRouter = isOpenRouter(providerName, baseURL);
22
25
  return {
23
26
  reportsCacheUsage: directOpenAI || openRouter,
@@ -26,22 +29,78 @@ function capabilities(providerName, baseURL) {
26
29
  supportsStickySessionId: openRouter,
27
30
  supportsServerCompaction: false,
28
31
  supportsTextVerbosity: directOpenAI,
32
+ supportsReasoningEffort: directOpenAI,
29
33
  };
30
34
  }
35
+ /** True for loopback/local inference servers, whose effective window is server-configured (often far below the model's). */
36
+ export function isLocalProviderUrl(url) {
37
+ try {
38
+ const hostname = new URL(url).hostname.toLowerCase().replace(/^\[|\]$/g, '');
39
+ return hostname === 'localhost' || hostname === '127.0.0.1' || hostname === '::1' || hostname === '0.0.0.0' || hostname.endsWith('.local');
40
+ }
41
+ catch {
42
+ return false;
43
+ }
44
+ }
31
45
  function runtimeForSelection(settings, selection, cwd) {
32
46
  const baseURL = selection.provider.url;
33
- const configuredKey = selection.provider.key ?? settings.apiKey;
47
+ const providerKind = selection.provider.kind;
48
+ const configuredKey = providerKind === 'chatgpt-codex' ? undefined : selection.provider.key ?? settings.apiKey;
34
49
  assertCredentialedEndpointSecure(baseURL, configuredKey);
35
50
  const apiKey = configuredKey ?? 'not-needed';
36
51
  const name = selection.model;
37
52
  const cacheSeed = cwd ?? process.cwd();
38
53
  const cacheKey = crypto.createHash('sha256').update(`${cacheSeed}\0${name}`).digest('hex').slice(0, 32);
54
+ const caps = capabilities(selection.provider.name, baseURL, providerKind);
55
+ const requestedReasoning = isReasoningLevel(settings.reasoning) ? settings.reasoning : undefined;
56
+ const reasoningPolicy = resolveReasoningPolicy({ requested: requestedReasoning, capabilities: caps });
57
+ const openai = providerKind === 'chatgpt-codex'
58
+ ? createOpenAI({ apiKey: 'haze-oauth-placeholder', baseURL, fetch: createChatGptCodexFetch(selection.provider.name) })
59
+ : createOpenAI({ apiKey, baseURL, headers: openRouterHeaders(selection.provider.name, baseURL) });
60
+ const limits = modelLimitsFor(selection.provider, name);
61
+ // Class-aware fallback so unknown local models (server-configured window,
62
+ // silent truncation) stay conservative while unknown hosted models get the
63
+ // modern 128K floor. A user-set fallback setting overrides the built-in
64
+ // default; the source tag distinguishes an intentional guess (no warning)
65
+ // from the default guess (warned once per session) (RH-005).
66
+ const isLocal = isLocalProviderUrl(baseURL);
67
+ const userFallback = isLocal ? settings.localContextWindowFallbackTokens : settings.contextWindowFallbackTokens;
68
+ const fallbackTokens = userFallback ?? (isLocal ? FALLBACK_LOCAL_CONTEXT_TOKENS : FALLBACK_CONTEXT_WINDOW_TOKENS);
69
+ const contextWindowSource = limits.contextWindowTokens !== undefined
70
+ ? 'settings'
71
+ : userFallback !== undefined ? 'user-fallback' : 'default-fallback';
39
72
  return {
40
- model: createOpenAI({ apiKey, baseURL, headers: openRouterHeaders(selection.provider.name, baseURL) }).chat(name),
73
+ model: providerKind === 'chatgpt-codex' ? openai.responses(name) : openai.chat(name),
41
74
  selector: modelSelector(selection.provider, name),
42
- config: { providerName: selection.provider.name, baseURL, modelName: name, cacheKey, capabilities: capabilities(selection.provider.name, baseURL) },
75
+ config: {
76
+ providerName: selection.provider.name,
77
+ providerKind,
78
+ baseURL,
79
+ modelName: name,
80
+ cacheKey,
81
+ capabilities: caps,
82
+ reasoningPolicy,
83
+ contextWindowTokens: limits.contextWindowTokens ?? fallbackTokens,
84
+ contextWindowSource,
85
+ ...(limits.maxOutputTokens !== undefined ? { maxOutputTokens: limits.maxOutputTokens } : {}),
86
+ },
43
87
  };
44
88
  }
89
+ /** Resolve optional context-window/output-token metadata for the selected model (RH-005). */
90
+ function modelLimitsFor(provider, modelName) {
91
+ const limits = provider.modelLimits?.[modelName];
92
+ if (!limits)
93
+ return {};
94
+ const out = {};
95
+ if (typeof limits.contextWindowTokens === 'number')
96
+ out.contextWindowTokens = limits.contextWindowTokens;
97
+ if (typeof limits.maxOutputTokens === 'number')
98
+ out.maxOutputTokens = limits.maxOutputTokens;
99
+ return out;
100
+ }
101
+ function isReasoningLevel(value) {
102
+ return value === 'low' || value === 'medium' || value === 'high';
103
+ }
45
104
  export async function modelWithConfig(session, settings) {
46
105
  // Callers may pass pre-read settings so a turn performs a single settings
47
106
  // read (CR-024); otherwise read fresh.
@@ -69,15 +128,24 @@ export async function resolveWorkerRuntime(input) {
69
128
  return { status: 'found', runtime: { model: selected.model, selector: selected.selector, providerName: selected.config.providerName, capabilities: selected.config.capabilities, requestOptions: providerRequestSettings(selected.config) } };
70
129
  }
71
130
  export function providerRequestSettings(config) {
131
+ if (config.providerKind === 'chatgpt-codex') {
132
+ return {
133
+ omitMaxOutputTokens: true,
134
+ providerOptions: { openai: { store: false, include: ['reasoning.encrypted_content'] } },
135
+ headers: { 'session-id': config.cacheKey },
136
+ };
137
+ }
138
+ const caps = config.capabilities;
139
+ const openaiOptions = {};
140
+ if (caps.supportsPromptCacheKey)
141
+ openaiOptions.promptCacheKey = config.cacheKey;
142
+ if (caps.supportsTextVerbosity)
143
+ openaiOptions.textVerbosity = 'low';
144
+ if (config.reasoningPolicy.effective !== 'disabled')
145
+ openaiOptions.reasoningEffort = config.reasoningPolicy.effective;
146
+ const providerOptions = Object.keys(openaiOptions).length > 0 ? { openai: openaiOptions } : undefined;
72
147
  return {
73
- ...(config.capabilities.supportsPromptCacheKey || config.capabilities.supportsTextVerbosity ? {
74
- providerOptions: {
75
- openai: {
76
- ...(config.capabilities.supportsPromptCacheKey ? { promptCacheKey: config.cacheKey } : {}),
77
- ...(config.capabilities.supportsTextVerbosity ? { textVerbosity: 'low' } : {}),
78
- },
79
- },
80
- } : {}),
81
- ...(config.capabilities.supportsStickySessionId ? { headers: { 'x-session-id': config.cacheKey } } : {}),
148
+ ...(providerOptions ? { providerOptions } : {}),
149
+ ...(caps.supportsStickySessionId ? { headers: { 'x-session-id': config.cacheKey } } : {}),
82
150
  };
83
151
  }