zapagent 1.14.21 → 1.14.22
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.
- package/README.md +1 -1
- package/package.json +1 -1
- package/src/agent-factory.ts +1 -1
- package/src/codex-provider.ts +3 -2
- package/src/config.ts +25 -3
- package/src/provider-catalog.ts +7 -2
- package/src/provider-metadata.ts +10 -1
- package/src/ui/dialog-config.tsx +53 -32
- package/zapsdk/core/providers/openai-responses.ts +12 -0
- package/zapsdk/core/reasoning-effort.ts +48 -0
package/README.md
CHANGED
|
@@ -97,7 +97,7 @@ Git workspaces also require [Git](https://git-scm.com).
|
|
|
97
97
|
| Linux | [ARM64](https://github.com/JackCaow/zapagent/releases/latest/download/zap-linux-arm64) · [x64](https://github.com/JackCaow/zapagent/releases/latest/download/zap-linux-x64) |
|
|
98
98
|
| Windows | [x64](https://github.com/JackCaow/zapagent/releases/latest/download/zap-windows-x64.exe) |
|
|
99
99
|
|
|
100
|
-
**Model configuration** — use `/model` to choose a model and `/effort` to adjust supported reasoning settings. For custom services, enter the Base URL and API key in `/connect` and select the service's API protocol; choose **OpenAI Responses** for a Responses endpoint. [Responses configuration](docs/usage.md#openai-responses-api).
|
|
100
|
+
**Model configuration** — use `/model` to choose a model and `/effort` to adjust supported reasoning settings. For custom services, enter the Base URL and API key in `/connect` and select the service's API protocol; choose **OpenAI Responses** for a Responses endpoint. [Responses configuration](docs/usage.md#openai-responses-api). Codex OAuth reads reasoning levels, descriptions and the default from the current model’s authenticated catalog. Choices are saved per model; **default** (or `/effort off`) lets the server choose. Saved capabilities remain available offline. [Terminal recording](docs/recordings/model-effort.cast).
|
|
101
101
|
|
|
102
102
|
**From source** — the repository pins Bun 1.3.14:
|
|
103
103
|
|
package/package.json
CHANGED
package/src/agent-factory.ts
CHANGED
|
@@ -120,7 +120,7 @@ export function buildAgentObjects(
|
|
|
120
120
|
} as unknown as Record<string, string>
|
|
121
121
|
: undefined
|
|
122
122
|
const rawProvider = active?.authMode === "codex-oauth"
|
|
123
|
-
? new OpenAIResponsesProvider({ model, codex: { getCredentials: getCodexCredentials },
|
|
123
|
+
? new OpenAIResponsesProvider({ model, codex: { getCredentials: getCodexCredentials, reasoningByModel: active.reasoningByModel, effortByModel: active.effortByModel },
|
|
124
124
|
canonicalModelIds: modelCanonicalIds(active), supportsVision: resolveVisionSupport(active, model),
|
|
125
125
|
extraRequestParams: active.extraRequestParams, maxRequestBytes: active.maxRequestBytes })
|
|
126
126
|
: isAnthropic
|
package/src/codex-provider.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { CODEX_BASE_URL, getCodexCredentials } from "./codex-auth.js"
|
|
2
|
+
import { normalizeModelReasoning, type ModelReasoning } from "../zapsdk/core/reasoning-effort.js"
|
|
2
3
|
import { VERSION } from "./version.js"
|
|
3
4
|
import { setActive, upsertProvider, type AppConfig, type Provider, type ProviderModelMetadata } from "./config.js"
|
|
4
5
|
|
|
@@ -14,7 +15,7 @@ export function connectCodexProvider(config: AppConfig): { config: AppConfig; pr
|
|
|
14
15
|
return { config: next, provider }
|
|
15
16
|
}
|
|
16
17
|
|
|
17
|
-
export async function discoverCodexModels(signal: AbortSignal): Promise<Array<{ id: string; metadata: ProviderModelMetadata }>> {
|
|
18
|
+
export async function discoverCodexModels(signal: AbortSignal): Promise<Array<{ id: string; metadata: ProviderModelMetadata; reasoning?: ModelReasoning }>> {
|
|
18
19
|
const credentials = await getCodexCredentials(signal)
|
|
19
20
|
const response = await fetch(`${CODEX_BASE_URL}/models?client_version=${encodeURIComponent(VERSION)}`, {
|
|
20
21
|
headers: { Authorization: `Bearer ${credentials.accessToken}`, originator: "zap",
|
|
@@ -27,7 +28,7 @@ export async function discoverCodexModels(signal: AbortSignal): Promise<Array<{
|
|
|
27
28
|
return data.models.flatMap(model => {
|
|
28
29
|
// supported_in_api only gates API-key accounts; Codex OAuth can use backend-only models.
|
|
29
30
|
if (typeof model.slug !== "string" || !model.slug || model.visibility === "hide" || model.visibility === "hidden") return []
|
|
30
|
-
return [{ id: model.slug, metadata: {
|
|
31
|
+
return [{ id: model.slug, reasoning: normalizeModelReasoning(model.supported_reasoning_levels, model.default_reasoning_level), metadata: {
|
|
31
32
|
name: typeof model.display_name === "string" ? model.display_name : undefined,
|
|
32
33
|
contextWindow: typeof model.context_window === "number" && model.context_window > 0 ? model.context_window : undefined,
|
|
33
34
|
supportsVision: Array.isArray(model.input_modalities) ? model.input_modalities.includes("image") : undefined,
|
package/src/config.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { modelReasoningFor, normalizeReasoningMap, resolveModelEffort, effortName, type ModelReasoning } from "../zapsdk/core/reasoning-effort.js"
|
|
1
2
|
import { deriveCompactionThresholds } from "../zapsdk/context/compact-budget.js"
|
|
2
3
|
import { canonicalModelId } from "../zapsdk/core/model-identity.js"
|
|
3
4
|
import { existsSync, readFileSync } from "node:fs"
|
|
@@ -117,6 +118,10 @@ export interface Provider {
|
|
|
117
118
|
* hints apply; unknown capability preserves images. The host can persist
|
|
118
119
|
* LoopAgent's bounded image-rejection fallback here for subsequent runs. */
|
|
119
120
|
visionByModel?: Record<string, boolean>
|
|
121
|
+
/** Authenticated capabilities, independent of public catalog alias metadata. */
|
|
122
|
+
reasoningByModel?: Record<string, ModelReasoning>
|
|
123
|
+
/** Codex model preferences; null explicitly uses the server default. */
|
|
124
|
+
effortByModel?: Record<string, string | null>
|
|
120
125
|
}
|
|
121
126
|
|
|
122
127
|
export interface CompactionConfig {
|
|
@@ -461,6 +466,9 @@ function normalizeProvider(value: unknown, index: number): Provider | null {
|
|
|
461
466
|
: [],
|
|
462
467
|
protocol: value.protocol === "anthropic" || value.protocol === "openai" || value.protocol === "openai-responses" ? value.protocol : undefined,
|
|
463
468
|
modelMetadata,
|
|
469
|
+
reasoningByModel: normalizeReasoningMap(value.reasoningByModel),
|
|
470
|
+
effortByModel: isRecord(value.effortByModel) ? Object.fromEntries(Object.entries(value.effortByModel)
|
|
471
|
+
.filter(([model, effort]) => !!model && model.length <= 256 && !/[\x00-\x1f\x7f]/.test(model) && (effort === null || effortName(effort)))) : undefined,
|
|
464
472
|
} as Provider
|
|
465
473
|
provider.contextWindowMode = value.contextWindowMode === "auto" || value.contextWindowMode === "manual"
|
|
466
474
|
? value.contextWindowMode
|
|
@@ -813,11 +821,16 @@ export function removeModelFromProvider(config: AppConfig, providerId: string, m
|
|
|
813
821
|
// reasoning_effort`; "unset" = key absent, server uses its own default.
|
|
814
822
|
// The Responses adapter maps this setting to `reasoning.effort`.
|
|
815
823
|
|
|
816
|
-
/**
|
|
817
|
-
|
|
818
|
-
|
|
824
|
+
/** The request-body model override also controls the effort capability lookup. */
|
|
825
|
+
export function effortModel(config: AppConfig, provider: Provider): string {
|
|
826
|
+
return typeof provider.extraRequestParams?.model === "string" ? provider.extraRequestParams.model
|
|
827
|
+
: config.active.providerId === provider.id ? config.active.model : provider.models[0] ?? ""
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
/** OAuth uses exact model capabilities; other providers keep preset/passthrough behavior. */
|
|
819
831
|
export function getEffortValuesForProvider(config: AppConfig, providerId: string): readonly string[] {
|
|
820
832
|
const p = config.providers.find(x => x.id === providerId)
|
|
833
|
+
if (p?.authMode === "codex-oauth") return modelReasoningFor(p.reasoningByModel, effortModel(config, p))?.levels.map(level => level.effort) ?? []
|
|
821
834
|
const preset = p?.sourcePresetId ? presetMap.get(p.sourcePresetId) : undefined
|
|
822
835
|
return preset?.effortValues ?? DEFAULT_EFFORT_VALUES
|
|
823
836
|
}
|
|
@@ -830,6 +843,7 @@ export function getEffortValuesForProvider(config: AppConfig, providerId: string
|
|
|
830
843
|
* upstream server enforce its own rules. */
|
|
831
844
|
export function hasDeclaredEffortValues(config: AppConfig, providerId: string): boolean {
|
|
832
845
|
const p = config.providers.find(x => x.id === providerId)
|
|
846
|
+
if (p?.authMode === "codex-oauth") return !!modelReasoningFor(p.reasoningByModel, effortModel(config, p))
|
|
833
847
|
const preset = p?.sourcePresetId ? presetMap.get(p.sourcePresetId) : undefined
|
|
834
848
|
return preset?.effortValues !== undefined
|
|
835
849
|
}
|
|
@@ -861,6 +875,13 @@ export function resolveEffortInput(
|
|
|
861
875
|
/** Current effort, or undefined if unset (= use server default). */
|
|
862
876
|
export function getEffort(config: AppConfig, providerId: string): string | undefined {
|
|
863
877
|
const p = config.providers.find(x => x.id === providerId)
|
|
878
|
+
if (p?.authMode === "codex-oauth") {
|
|
879
|
+
const model = effortModel(config, p)
|
|
880
|
+
const nested = p.extraRequestParams?.reasoning as { effort?: unknown } | undefined
|
|
881
|
+
const value = Object.hasOwn(p.effortByModel ?? {}, model) ? p.effortByModel![model]
|
|
882
|
+
: typeof p.extraRequestParams?.reasoning_effort === "string" ? p.extraRequestParams.reasoning_effort : nested?.effort
|
|
883
|
+
return resolveModelEffort(modelReasoningFor(p.reasoningByModel, model), value)
|
|
884
|
+
}
|
|
864
885
|
const v = p?.extraRequestParams?.reasoning_effort
|
|
865
886
|
return typeof v === "string" ? v : undefined
|
|
866
887
|
}
|
|
@@ -872,6 +893,7 @@ export function setEffort(config: AppConfig, providerId: string, value: string |
|
|
|
872
893
|
const providers = config.providers.map(p => {
|
|
873
894
|
if (p.id !== providerId) return p
|
|
874
895
|
const extra = { ...(p.extraRequestParams ?? {}) }
|
|
896
|
+
if (p.authMode === "codex-oauth") return { ...p, effortByModel: { ...p.effortByModel, [effortModel(config, p)]: value } }
|
|
875
897
|
if (value === null) delete extra.reasoning_effort
|
|
876
898
|
else extra.reasoning_effort = value
|
|
877
899
|
return { ...p, extraRequestParams: extra }
|
package/src/provider-catalog.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ModelReasoning } from "../zapsdk/core/reasoning-effort.js"
|
|
1
2
|
import { listModels } from "../zapsdk/core/list-models.js"
|
|
2
3
|
import { getModelsDevProvider, suggestedModelsDevIds, type ModelsDevProvider } from "../zapsdk/core/models-dev.js"
|
|
3
4
|
import { resolveCatalogProviderId, upsertProvider, setActive, type AppConfig, type Provider, type ProviderModelMetadata } from "./config.js"
|
|
@@ -5,7 +6,7 @@ import { enrichProviderMetadata, mergeModelMetadata, modelsDevMetadata, pickMode
|
|
|
5
6
|
import { discoverCodexModels } from "./codex-provider.js"
|
|
6
7
|
|
|
7
8
|
export type ModelSource = "saved" | "provider" | "models.dev" | "manual"
|
|
8
|
-
export interface CatalogRow { id: string; source: ModelSource; metadata?: ProviderModelMetadata }
|
|
9
|
+
export interface CatalogRow { id: string; source: ModelSource; metadata?: ProviderModelMetadata; reasoning?: ModelReasoning }
|
|
9
10
|
export interface DiscoveredCatalog { rows: CatalogRow[]; error?: string }
|
|
10
11
|
|
|
11
12
|
/** User-selected metadata for a gateway alias, independent of transport identity. */
|
|
@@ -73,7 +74,11 @@ export function selectCatalogModel(config: AppConfig, providerId: string, row: C
|
|
|
73
74
|
const models = [...new Set([...provider.models, row.id])]
|
|
74
75
|
const modelMetadata = pickModelMetadata(mergeModelMetadata(provider.modelMetadata ?? {},
|
|
75
76
|
row.metadata ? { [row.id]: row.metadata } : {}), models)
|
|
76
|
-
|
|
77
|
+
const liveReasoning = provider.authMode === "codex-oauth" && row.source === "provider"
|
|
78
|
+
const reasoningByModel = { ...provider.reasoningByModel,
|
|
79
|
+
...(liveReasoning && row.reasoning ? { [row.id]: row.reasoning } : {}) }
|
|
80
|
+
if (liveReasoning && !row.reasoning) delete reasoningByModel[row.id]
|
|
81
|
+
return setActive(upsertProvider(config, { ...provider, models, modelMetadata, reasoningByModel }), providerId, row.id)
|
|
77
82
|
}
|
|
78
83
|
|
|
79
84
|
export function describeCatalogRow(row: CatalogRow): string {
|
package/src/provider-metadata.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { discoverCodexModels } from "./codex-provider.js"
|
|
1
2
|
import type { ImageTokenContext } from "../zapsdk/context/image-tokens.js"
|
|
2
3
|
import { canonicalModelId } from "../zapsdk/core/model-identity.js"
|
|
3
4
|
import {
|
|
@@ -83,6 +84,14 @@ export function enrichProviderMetadata(provider: Provider, catalog: ModelsDevPro
|
|
|
83
84
|
export async function refreshModelsDevMetadata(config: AppConfig): Promise<AppConfig> {
|
|
84
85
|
const providers = await Promise.all(config.providers.map(async provider => {
|
|
85
86
|
if (provider.models.length === 0) return provider
|
|
87
|
+
if (provider.authMode === "codex-oauth") {
|
|
88
|
+
try {
|
|
89
|
+
const rows = await discoverCodexModels(AbortSignal.timeout(6500))
|
|
90
|
+
const reasoningByModel = Object.fromEntries(rows.flatMap(row => row.reasoning ? [[row.id, row.reasoning]] : []))
|
|
91
|
+
return JSON.stringify(reasoningByModel) === JSON.stringify(provider.reasoningByModel ?? {})
|
|
92
|
+
? provider : { ...provider, reasoningByModel }
|
|
93
|
+
} catch { return provider }
|
|
94
|
+
}
|
|
86
95
|
const ids = new Set([resolveCatalogProviderId(provider), ...provider.models.map(id =>
|
|
87
96
|
provider.modelMetadata?.[id]?.catalogRef?.providerId)].filter((id): id is string => !!id))
|
|
88
97
|
const catalogs = await Promise.all([...ids].map(id => getModelsDevProvider(id).catch(() => null)))
|
|
@@ -124,7 +133,7 @@ export function applyMetadataRefresh(current: AppConfig, initial: AppConfig, ref
|
|
|
124
133
|
const after = refreshed.providers.find(item => item.id === provider.id)
|
|
125
134
|
if (!before || !after || before === after || provider !== before) return provider
|
|
126
135
|
return { ...provider, contextWindow: after.contextWindow,
|
|
127
|
-
contextWindowMode: after.contextWindowMode, modelMetadata: after.modelMetadata }
|
|
136
|
+
contextWindowMode: after.contextWindowMode, modelMetadata: after.modelMetadata, reasoningByModel: after.reasoningByModel }
|
|
128
137
|
})
|
|
129
138
|
return providers.every((provider, index) => provider === current.providers[index])
|
|
130
139
|
? current : { ...current, providers }
|
package/src/ui/dialog-config.tsx
CHANGED
|
@@ -3,7 +3,12 @@
|
|
|
3
3
|
// dialogs: Model, Mode, Effort. Kept in one file because they share the
|
|
4
4
|
// same 30-line shape — caller hands in `{config, onCommit, onCancel}`,
|
|
5
5
|
// the dialog wires options + commit semantics around PickerDialog.
|
|
6
|
+
import { modelReasoningFor } from "../../zapsdk/core/reasoning-effort.js"
|
|
7
|
+
import { useEffect, useState } from "react"
|
|
8
|
+
import { discoverCodexModels } from "../codex-provider.js"
|
|
6
9
|
import {
|
|
10
|
+
effortModel,
|
|
11
|
+
upsertProvider,
|
|
7
12
|
getActiveProvider,
|
|
8
13
|
getEffort,
|
|
9
14
|
setEffort,
|
|
@@ -48,37 +53,53 @@ export function ModeDialog({ config, onCommit, onCancel }: DialogProps) {
|
|
|
48
53
|
// ── Effort picker ────────────────────────────────────────────────────────
|
|
49
54
|
export function EffortDialog({ config, onCommit, onCancel }: DialogProps) {
|
|
50
55
|
const provider = getActiveProvider(config)
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
56
|
+
// A changed connection remounts the fetch and picker, including its cursor.
|
|
57
|
+
return <ModelEffortDialog key={provider ? `${provider.id}:${effortModel(config, provider)}` : "none"}
|
|
58
|
+
config={config} onCommit={onCommit} onCancel={onCancel} />
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function ModelEffortDialog({ config, onCommit, onCancel }: DialogProps) {
|
|
62
|
+
const provider = getActiveProvider(config)
|
|
63
|
+
const [snapshot, setSnapshot] = useState<{ provider: typeof provider; rows: Awaited<ReturnType<typeof discoverCodexModels>> }>()
|
|
64
|
+
const [status, setStatus] = useState("Loading model capabilities…")
|
|
65
|
+
useEffect(() => {
|
|
66
|
+
if (provider?.authMode !== "codex-oauth") return
|
|
67
|
+
const controller = new AbortController()
|
|
68
|
+
setStatus("Loading model capabilities…")
|
|
69
|
+
discoverCodexModels(controller.signal).then(rows => {
|
|
70
|
+
if (controller.signal.aborted) return
|
|
71
|
+
setSnapshot({ provider, rows })
|
|
72
|
+
setStatus("Model has not advertised reasoning levels")
|
|
73
|
+
}, () => {
|
|
74
|
+
if (!controller.signal.aborted) setStatus("Discovery unavailable; using saved model capabilities")
|
|
75
|
+
})
|
|
76
|
+
return () => controller.abort()
|
|
77
|
+
}, [provider])
|
|
78
|
+
let latest = config
|
|
79
|
+
if (provider && snapshot?.provider === provider) {
|
|
80
|
+
const reasoningByModel = Object.fromEntries(snapshot.rows.flatMap(row => row.reasoning ? [[row.id, row.reasoning]] : []))
|
|
81
|
+
latest = upsertProvider(config, { ...provider, reasoningByModel })
|
|
61
82
|
}
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
const
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
83
|
+
const active = getActiveProvider(latest)
|
|
84
|
+
const codex = active?.authMode === "codex-oauth"
|
|
85
|
+
const values = active ? getEffortValuesForProvider(latest, active.id) : []
|
|
86
|
+
const model = active ? effortModel(latest, active) : ""
|
|
87
|
+
const profile = modelReasoningFor(active?.reasoningByModel, model)
|
|
88
|
+
if (!active || (!codex && values.length === 0)) {
|
|
89
|
+
return <PickerDialog title="Reasoning effort"
|
|
90
|
+
options={[{ id: "__none", label: "Active provider does not expose reasoning_effort", disabled: true }]}
|
|
91
|
+
onCancel={onCancel} onSelect={onCancel} />
|
|
92
|
+
}
|
|
93
|
+
const current = getEffort(latest, active.id)
|
|
94
|
+
const options: PickerOption[] = [{ id: "__default", label: codex ? "default" : "off",
|
|
95
|
+
meta: `${codex ? `server default${profile?.defaultEffort ? ` (${profile.defaultEffort})` : ""}` : "server default"}${!current ? " (current)" : ""}` },
|
|
96
|
+
...values.map(value => ({ id: `level:${value}`, label: value,
|
|
97
|
+
meta: [profile?.levels.find(level => level.effort === value)?.description,
|
|
98
|
+
current === value ? "(current)" : undefined].filter(Boolean).join(" · ") }))]
|
|
99
|
+
if (codex && !profile) options.push({ id: "__status", label: status, disabled: true })
|
|
100
|
+
if (codex && profile?.levels.length === 0) options.push({ id: "__none", label: "No adjustable reasoning levels", disabled: true })
|
|
101
|
+
return <PickerDialog key={`${model}:${values.join(",")}:${current ?? "default"}`}
|
|
102
|
+
title={`Reasoning effort — ${codex ? model : active.name}`}
|
|
103
|
+
options={options} initialIndex={current ? Math.max(0, values.indexOf(current) + 1) : 0}
|
|
104
|
+
onCancel={onCancel} onSelect={id => onCommit(setEffort(latest, active.id, id === "__default" ? null : id.slice(6)))} />
|
|
84
105
|
}
|
|
@@ -7,6 +7,7 @@ import { canonicalizeJsonSchema, zodToJsonSchema } from "../../tools/schema.js";
|
|
|
7
7
|
import { ProviderRequest } from "../provider-request.js";
|
|
8
8
|
import { getStreamIdleMs, withStreamIdleTimeout } from "../stream-idle.js";
|
|
9
9
|
import { OpenAICompatibleProvider, type OpenAICompatibleProviderOptions } from "./openai-compatible.js";
|
|
10
|
+
import { modelReasoningFor, resolveModelEffort, type ModelReasoning } from "../reasoning-effort.js";
|
|
10
11
|
import { stripBoundary } from "../prompt-boundary.js";
|
|
11
12
|
|
|
12
13
|
export const CODEX_RESPONSES_BASE_URL = "https://chatgpt.com/backend-api/codex";
|
|
@@ -15,6 +16,8 @@ export interface OpenAIResponsesProviderOptions extends OpenAICompatibleProvider
|
|
|
15
16
|
codex?: {
|
|
16
17
|
getCredentials: (signal?: AbortSignal) => Promise<{ accessToken: string; accountId?: string }>;
|
|
17
18
|
fetch?: typeof globalThis.fetch;
|
|
19
|
+
reasoningByModel?: Record<string, ModelReasoning>;
|
|
20
|
+
effortByModel?: Record<string, string | null>;
|
|
18
21
|
};
|
|
19
22
|
}
|
|
20
23
|
type LayoutItem =
|
|
@@ -128,6 +131,15 @@ export class OpenAIResponsesProvider implements LLMProvider {
|
|
|
128
131
|
stream,
|
|
129
132
|
} as ResponseCreateParams;
|
|
130
133
|
if (this.options.codex) {
|
|
134
|
+
const { reasoningByModel, effortByModel } = this.options.codex;
|
|
135
|
+
const model = body.model ?? "";
|
|
136
|
+
const reasoning = { ...body.reasoning };
|
|
137
|
+
const value = Object.hasOwn(effortByModel ?? {}, model) ? effortByModel![model] : reasoning.effort;
|
|
138
|
+
const effort = resolveModelEffort(modelReasoningFor(reasoningByModel, model), value);
|
|
139
|
+
if (effort === undefined) delete reasoning.effort;
|
|
140
|
+
else reasoning.effort = effort as typeof reasoning.effort;
|
|
141
|
+
if (Object.keys(reasoning).length) body.reasoning = reasoning;
|
|
142
|
+
else delete body.reasoning;
|
|
131
143
|
let leadingSystemEnd = 0;
|
|
132
144
|
while (messages[leadingSystemEnd]?.role === "system") leadingSystemEnd++;
|
|
133
145
|
body.instructions = messages.slice(0, leadingSystemEnd).map(message => stripBoundary(message.content)).join("\n\n");
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/** Account-advertised capabilities for an exact request model ID. */
|
|
2
|
+
export interface ModelReasoning {
|
|
3
|
+
levels: Array<{ effort: string; description?: string }>;
|
|
4
|
+
defaultEffort?: string;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function effortName(value: unknown): value is string {
|
|
8
|
+
return typeof value === "string" && /^[a-zA-Z0-9_-]{1,64}$/.test(value);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** Missing/malformed is unknown; an explicit empty array means no override. */
|
|
12
|
+
export function normalizeModelReasoning(levels: unknown, defaultEffort?: unknown): ModelReasoning | undefined {
|
|
13
|
+
if (!Array.isArray(levels)) return undefined;
|
|
14
|
+
const result: ModelReasoning = { levels: [] };
|
|
15
|
+
const seen = new Set<string>();
|
|
16
|
+
for (const level of levels) {
|
|
17
|
+
if (!level || typeof level !== "object" || !effortName(level.effort)) return undefined;
|
|
18
|
+
const key = level.effort.toLowerCase();
|
|
19
|
+
if (seen.has(key)) continue;
|
|
20
|
+
seen.add(key);
|
|
21
|
+
const description = typeof level.description === "string"
|
|
22
|
+
? level.description.replace(/[\x00-\x1f\x7f-\x9f]/g, " ").slice(0, 256) : undefined;
|
|
23
|
+
result.levels.push({ effort: level.effort, ...(description ? { description } : {}) });
|
|
24
|
+
}
|
|
25
|
+
const canonical = typeof defaultEffort === "string"
|
|
26
|
+
? result.levels.find(level => level.effort.toLowerCase() === defaultEffort.toLowerCase())?.effort : undefined;
|
|
27
|
+
if (canonical) result.defaultEffort = canonical;
|
|
28
|
+
return result;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function resolveModelEffort(profile: ModelReasoning | undefined, value: unknown): string | undefined {
|
|
32
|
+
if (typeof value !== "string") return undefined;
|
|
33
|
+
return profile ? profile.levels.find(level => level.effort.toLowerCase() === value.toLowerCase())?.effort : value;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function normalizeReasoningMap(value: unknown): Record<string, ModelReasoning> | undefined {
|
|
37
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return undefined;
|
|
38
|
+
const entries = Object.entries(value).flatMap(([model, raw]) => {
|
|
39
|
+
if (!model || model.length > 256 || /[\x00-\x1f\x7f]/.test(model) || !raw || typeof raw !== "object") return [];
|
|
40
|
+
const profile = normalizeModelReasoning(raw.levels, raw.defaultEffort);
|
|
41
|
+
return profile ? [[model, profile]] : [];
|
|
42
|
+
});
|
|
43
|
+
return entries.length ? Object.fromEntries(entries) : undefined;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function modelReasoningFor(profiles: Record<string, ModelReasoning> | undefined, model: string): ModelReasoning | undefined {
|
|
47
|
+
return profiles && Object.hasOwn(profiles, model) ? profiles[model] : undefined;
|
|
48
|
+
}
|