@shuind/dsh-codex-harness 0.2.21 → 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.
- package/CHANGELOG.md +185 -0
- package/README.md +38 -50
- package/RELEASE.md +61 -0
- package/assets/codex.svg +5 -0
- package/cordis.patch.yml +181 -6
- package/docs/preset-library.md +33 -23
- package/docs/tool-policy-benchmark.json +310 -0
- package/docs/tool-policy.md +148 -0
- package/lib/client.js +4774 -2416
- package/lib/compact-command.js +416 -0
- package/lib/compaction.js +141 -12
- package/lib/hybrid-executor.js +1196 -0
- package/lib/index.js +3658 -2334
- package/lib/preferences.js +117 -0
- package/lib/tool-policy.js +2422 -0
- package/lib/typert.js +86 -79
- package/lib/types/client/activity.d.ts +33 -9
- package/lib/types/client/activity.js +4 -3
- package/lib/types/client/desktop.d.ts +67 -0
- package/lib/types/client/desktop.js +229 -0
- package/lib/types/client/index.d.ts +207 -110
- package/lib/types/client/index.js +99 -492
- package/lib/types/client/policy-drafts.d.ts +25 -0
- package/lib/types/client/policy-drafts.js +47 -0
- package/lib/types/client/policy-locale.d.ts +260 -0
- package/lib/types/client/policy-locale.js +357 -0
- package/lib/types/client/preset-manager.d.ts +38 -10
- package/lib/types/client/preset-manager.js +235 -185
- package/lib/types/client/preset-requests.d.ts +12 -0
- package/lib/types/client/preset-requests.js +101 -0
- package/lib/types/client/tool-policy.d.ts +33 -0
- package/lib/types/client/tool-policy.js +336 -0
- package/lib/types/contracts/preset-editor-schema.d.ts +78 -0
- package/lib/types/contracts/preset-editor-schema.js +95 -0
- package/lib/types/{prompt.js → contracts/prompt.js} +13 -7
- package/lib/types/{settings.d.ts → contracts/settings.d.ts} +11 -4
- package/lib/types/{settings.js → contracts/settings.js} +22 -6
- package/lib/types/contracts/tool-policy-contract.d.ts +63 -0
- package/lib/types/contracts/tool-policy-contract.js +33 -0
- package/lib/types/contracts/ui-contracts.d.ts +92 -0
- package/lib/types/contracts/ui-contracts.js +83 -0
- package/lib/types/entries/client.d.ts +10 -0
- package/lib/types/entries/client.js +10 -0
- package/lib/types/{compact-command.d.ts → entries/compact-command.d.ts} +2 -0
- package/lib/types/entries/compact-command.js +176 -0
- package/lib/types/{compaction.d.ts → entries/compaction.d.ts} +15 -3
- package/lib/types/{compaction.js → entries/compaction.js} +47 -11
- package/lib/types/entries/hybrid-executor.d.ts +19 -0
- package/lib/types/entries/hybrid-executor.js +57 -0
- package/lib/types/entries/index.d.ts +66 -0
- package/lib/types/entries/index.js +192 -0
- package/lib/types/entries/preferences.d.ts +11 -0
- package/lib/types/entries/preferences.js +16 -0
- package/lib/types/entries/tool-policy.d.ts +97 -0
- package/lib/types/entries/tool-policy.js +158 -0
- package/lib/types/entries/typert.host.d.ts +23 -0
- package/lib/types/entries/typert.host.js +24 -0
- package/lib/types/hybrid/codex-hybrid-executor.d.ts +44 -0
- package/lib/types/hybrid/codex-hybrid-executor.js +257 -0
- package/lib/types/hybrid/hybrid-sdk.d.ts +13 -0
- package/lib/types/hybrid/hybrid-sdk.js +196 -0
- package/lib/types/hybrid/program-calls.d.ts +28 -0
- package/lib/types/hybrid/program-calls.js +193 -0
- package/lib/types/hybrid/subcall-scheduler.d.ts +90 -0
- package/lib/types/hybrid/subcall-scheduler.js +343 -0
- package/lib/types/policy/deferred-tools.d.ts +42 -0
- package/lib/types/policy/deferred-tools.js +264 -0
- package/lib/types/policy/overlay-installer.d.ts +12 -0
- package/lib/types/policy/overlay-installer.js +179 -0
- package/lib/types/policy/rule-resolver.d.ts +94 -0
- package/lib/types/policy/rule-resolver.js +114 -0
- package/lib/types/policy/tool-capabilities.d.ts +33 -0
- package/lib/types/policy/tool-capabilities.js +46 -0
- package/lib/types/{activity.d.ts → presets/activity.d.ts} +3 -5
- package/lib/types/{activity.js → presets/activity.js} +51 -27
- package/lib/types/presets/desktop-preset.d.ts +22 -0
- package/lib/types/presets/desktop-preset.js +43 -0
- package/lib/types/presets/preset-editor.d.ts +47 -0
- package/lib/types/presets/preset-editor.js +81 -0
- package/lib/types/presets/preset-prompt-catalog.d.ts +23 -0
- package/lib/types/presets/preset-prompt-catalog.js +215 -0
- package/lib/types/presets/preset-tool-catalog.d.ts +5 -0
- package/lib/types/presets/preset-tool-catalog.js +58 -0
- package/lib/types/presets/runtime-enhancements.d.ts +45 -0
- package/lib/types/presets/runtime-enhancements.js +349 -0
- package/lib/types/presets/small-window-compaction.d.ts +15 -0
- package/lib/types/presets/small-window-compaction.js +121 -0
- package/lib/types/responses/compact-checkpoint.d.ts +27 -0
- package/lib/types/responses/compact-checkpoint.js +257 -0
- package/lib/types/responses/fetch-transport.d.ts +16 -0
- package/lib/types/responses/fetch-transport.js +257 -0
- package/lib/types/responses/remote-compact.d.ts +13 -0
- package/lib/types/responses/remote-compact.js +319 -0
- package/lib/types/responses/sse-diagnostics.d.ts +9 -0
- package/lib/types/responses/sse-diagnostics.js +157 -0
- package/lib/types/responses/wire-rewrite.d.ts +35 -0
- package/lib/types/responses/wire-rewrite.js +237 -0
- package/lib/types/tools/exec-output.d.ts +58 -0
- package/lib/types/tools/exec-output.js +173 -0
- package/lib/types/tools/exec-plan-tools.d.ts +37 -0
- package/lib/types/tools/exec-plan-tools.js +265 -0
- package/lib/types/tools/exec-runner.d.ts +21 -0
- package/lib/types/tools/exec-runner.js +385 -0
- package/lib/types/tools/patch-tool.d.ts +16 -0
- package/lib/types/tools/patch-tool.js +258 -0
- package/lib/types/{patch.d.ts → tools/patch.d.ts} +1 -1
- package/lib/types/{patch.js → tools/patch.js} +96 -21
- package/locale/en.json +6 -0
- package/locale/hybrid-executor/en.json +6 -0
- package/locale/hybrid-executor/zh.json +6 -0
- package/locale/tool-policy/en.json +6 -0
- package/locale/tool-policy/zh.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +297 -299
- package/presets/{codex-collaboration → codex-hybrid}/agent.cordis.yml +45 -48
- package/presets/codex-hybrid/preset.yml +3 -0
- package/scripts/benchmark-tool-policy.mjs +186 -0
- package/scripts/build-preset.mjs +19 -0
- package/scripts/clean-build.mjs +11 -0
- package/src/client/activity.ts +83 -0
- package/src/client/desktop.tsx +276 -0
- package/src/client/index.tsx +391 -0
- package/src/client/policy-drafts.ts +56 -0
- package/src/client/policy-locale.ts +362 -0
- package/src/client/preset-manager.tsx +376 -0
- package/src/client/preset-requests.ts +113 -0
- package/src/client/tool-policy.tsx +402 -0
- package/src/contracts/preset-editor-schema.ts +110 -0
- package/src/contracts/prompt.ts +66 -0
- package/src/contracts/settings.ts +103 -0
- package/src/contracts/tool-policy-contract.ts +93 -0
- package/src/contracts/ui-contracts.ts +173 -0
- package/src/entries/client.tsx +14 -0
- package/src/entries/compact-command.ts +256 -0
- package/src/entries/compaction.ts +218 -0
- package/src/entries/hybrid-executor.ts +76 -0
- package/src/entries/index.ts +298 -0
- package/src/entries/preferences.ts +17 -0
- package/src/entries/tool-policy.ts +162 -0
- package/src/entries/typert.host.ts +29 -0
- package/src/hybrid/codex-hybrid-executor.ts +310 -0
- package/src/hybrid/hybrid-sdk.ts +217 -0
- package/src/hybrid/program-calls.ts +202 -0
- package/src/hybrid/subcall-scheduler.ts +446 -0
- package/src/policy/deferred-tools.ts +331 -0
- package/src/policy/overlay-installer.ts +191 -0
- package/src/policy/rule-resolver.ts +128 -0
- package/src/policy/tool-capabilities.ts +88 -0
- package/src/presets/activity.ts +146 -0
- package/src/presets/desktop-preset.ts +60 -0
- package/src/presets/preset-editor.ts +101 -0
- package/src/presets/preset-prompt-catalog.ts +221 -0
- package/src/presets/preset-tool-catalog.ts +58 -0
- package/src/presets/runtime-enhancements.ts +415 -0
- package/src/presets/small-window-compaction.ts +166 -0
- package/src/responses/compact-checkpoint.ts +264 -0
- package/src/responses/fetch-transport.ts +290 -0
- package/src/responses/remote-compact.ts +393 -0
- package/src/responses/sse-diagnostics.ts +156 -0
- package/src/responses/wire-rewrite.ts +261 -0
- package/src/tools/exec-output.ts +211 -0
- package/src/tools/exec-plan-tools.ts +307 -0
- package/src/tools/exec-runner.ts +445 -0
- package/src/tools/patch-tool.ts +331 -0
- package/src/tools/patch.ts +328 -0
- package/tsconfig.json +29 -0
- package/tsdown.config.ts +110 -0
- package/lib/installer.js +0 -237
- package/lib/invariant.js +0 -23
- package/lib/types/activity-types.d.ts +0 -6
- package/lib/types/activity-types.js +0 -3
- package/lib/types/client/preset-copy.d.ts +0 -42
- package/lib/types/client/preset-copy.js +0 -81
- package/lib/types/compact-command.js +0 -79
- package/lib/types/context.d.ts +0 -8
- package/lib/types/context.js +0 -14
- package/lib/types/exec.d.ts +0 -46
- package/lib/types/exec.js +0 -365
- package/lib/types/index.d.ts +0 -95
- package/lib/types/index.js +0 -738
- package/lib/types/installer.d.ts +0 -27
- package/lib/types/installer.js +0 -146
- package/lib/types/invariant.d.ts +0 -16
- package/lib/types/invariant.js +0 -22
- package/lib/types/preset-contract.d.ts +0 -69
- package/lib/types/preset-contract.js +0 -22
- package/lib/types/preset-editor-remote.d.ts +0 -30
- package/lib/types/preset-editor-remote.js +0 -37
- package/lib/types/preset-editor.d.ts +0 -69
- package/lib/types/preset-editor.js +0 -557
- package/lib/types/preset-templates.d.ts +0 -36
- package/lib/types/preset-templates.js +0 -12
- package/lib/types/prompt-sections.d.ts +0 -15
- package/lib/types/prompt-sections.js +0 -23
- package/lib/types/remote.d.ts +0 -40
- package/lib/types/remote.js +0 -683
- package/lib/types/typert.host.d.ts +0 -47
- package/lib/types/typert.host.js +0 -99
- package/presets/codex-collaboration/preset.yml +0 -3
- /package/lib/types/{prompt.d.ts → contracts/prompt.d.ts} +0 -0
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
/** Read-only preset definition viewer and composer context-window control for Harness 0.2.2+. */
|
|
2
|
+
import { useCallback, useEffect, useId, useRef, useState, useSyncExternalStore } from 'react'
|
|
3
|
+
import { createPortal } from 'react-dom'
|
|
4
|
+
import type { PropsLocale } from '@deepseek-ai/dsh-client-ui-slots'
|
|
5
|
+
import type { RemoteResult, TypertClientRemote } from '@deepseek-ai/dsh-typert-protocol'
|
|
6
|
+
import {
|
|
7
|
+
contextPresetsForBounds,
|
|
8
|
+
formatCompactTokens,
|
|
9
|
+
OPENAI_CODEX_MODEL_CATALOG_PATH,
|
|
10
|
+
OPENAI_CODEX_MODEL_LIMITS,
|
|
11
|
+
OPENAI_CODEX_PROVIDER,
|
|
12
|
+
parseCustomContextInput,
|
|
13
|
+
resolveContextWindowBounds,
|
|
14
|
+
} from '../contracts/ui-contracts.ts'
|
|
15
|
+
import type {
|
|
16
|
+
CodexConnectScope,
|
|
17
|
+
ModelDirectorySnapshot,
|
|
18
|
+
ModelDirectoryStore,
|
|
19
|
+
PresetCatalog,
|
|
20
|
+
PresetDefinitionDocument,
|
|
21
|
+
} from '../contracts/ui-contracts.ts'
|
|
22
|
+
import type { PresetPromptCatalog, PresetToolCatalog } from '../contracts/tool-policy-contract.ts'
|
|
23
|
+
import { readPresetWithRetry } from './preset-requests.ts'
|
|
24
|
+
|
|
25
|
+
export interface PresetClientRemote extends TypertClientRemote {
|
|
26
|
+
codexPresetEditor: {
|
|
27
|
+
catalog(): Promise<RemoteResult<PresetCatalog>>
|
|
28
|
+
tools?(id: string): Promise<RemoteResult<PresetToolCatalog>>
|
|
29
|
+
prompts?(id: string): Promise<RemoteResult<PresetPromptCatalog>>
|
|
30
|
+
read(id: string): Promise<RemoteResult<PresetDefinitionDocument>>
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface PresetManagerActions {
|
|
35
|
+
remote: PresetClientRemote
|
|
36
|
+
ready: Promise<void>
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const documentCache = new WeakMap<PresetManagerActions, Map<string, { value: PresetDefinitionDocument; time: number }>>()
|
|
40
|
+
|
|
41
|
+
function unwrap<T>(result: RemoteResult<T>): T {
|
|
42
|
+
if (result.ok) return result.value
|
|
43
|
+
throw new Error((result.error as unknown as { message?: string }).message ?? 'remote-request-failed')
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const styles = `
|
|
47
|
+
.codex-preset-editor { display:grid; gap:10px; min-width:0; font-size:13px; border:0.5px solid var(--dsw-alias-settings-card-stroke, var(--dsw-alias-border-l2)); border-radius:var(--dsw-radius-xl, 12px); background:var(--dsw-alias-settings-card-fill, var(--dsw-alias-bg-layer-1)); padding:12px 14px; }
|
|
48
|
+
.codex-preset-editor .codex-preset-editor-head { display:flex; align-items:center; justify-content:space-between; gap:8px; }
|
|
49
|
+
.codex-preset-editor button { font:inherit; font-size:12px; height:30px; padding:0 11px; border:0.5px solid var(--dsw-alias-border-l3, var(--dsw-alias-border-l2)); border-radius:var(--dsw-radius-md, 8px); background:var(--dsw-alias-bg-layer-2); color:inherit; cursor:pointer; }
|
|
50
|
+
.codex-preset-editor button:disabled { opacity:.5; cursor:default; }
|
|
51
|
+
.codex-preset-editor p { margin:0; font-size:12px; line-height:1.55; color:var(--dsw-alias-label-secondary); }
|
|
52
|
+
.codex-preset-editor pre { white-space:pre-wrap; overflow-wrap:anywhere; margin:0; padding:10px 12px; border-radius:var(--dsw-radius-md, 8px); background:var(--dsw-alias-bg-layer-2); font-size:12px; line-height:1.5; max-height:480px; overflow:auto; }
|
|
53
|
+
`
|
|
54
|
+
|
|
55
|
+
/** A fixed-ID declaration viewer: no second picker and no write/copy actions. */
|
|
56
|
+
export function PresetDefinitionCard({
|
|
57
|
+
presetManager,
|
|
58
|
+
id,
|
|
59
|
+
t,
|
|
60
|
+
}: { presetManager: PresetManagerActions; id: string } & PropsLocale<'codex'>) {
|
|
61
|
+
const [value, setValue] = useState<PresetDefinitionDocument>()
|
|
62
|
+
const [loading, setLoading] = useState(true)
|
|
63
|
+
const [error, setError] = useState('')
|
|
64
|
+
const request = useRef(0)
|
|
65
|
+
const alive = useRef(true)
|
|
66
|
+
|
|
67
|
+
async function load(refresh = false): Promise<void> {
|
|
68
|
+
const ticket = ++request.current
|
|
69
|
+
setLoading(true)
|
|
70
|
+
setError('')
|
|
71
|
+
try {
|
|
72
|
+
await presetManager.ready
|
|
73
|
+
const cache = documentCache.get(presetManager) ?? new Map<string, { value: PresetDefinitionDocument; time: number }>()
|
|
74
|
+
documentCache.set(presetManager, cache)
|
|
75
|
+
const cached = cache.get(id)
|
|
76
|
+
const document = !refresh && cached && Date.now() - cached.time < 60_000
|
|
77
|
+
? cached.value
|
|
78
|
+
: await readPresetWithRetry(
|
|
79
|
+
() => presetManager.remote.codexPresetEditor.read(id).then(unwrap),
|
|
80
|
+
() => alive.current && ticket === request.current,
|
|
81
|
+
)
|
|
82
|
+
if (document.id !== id) throw new Error('preset-document-mismatch')
|
|
83
|
+
cache.set(id, { value: document, time: Date.now() })
|
|
84
|
+
if (alive.current && ticket === request.current) setValue(document)
|
|
85
|
+
} catch (cause) {
|
|
86
|
+
if (alive.current && ticket === request.current) {
|
|
87
|
+
setError(cause instanceof Error ? cause.message : String(cause))
|
|
88
|
+
}
|
|
89
|
+
} finally {
|
|
90
|
+
if (alive.current && ticket === request.current) setLoading(false)
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
useEffect(() => {
|
|
95
|
+
alive.current = true
|
|
96
|
+
void load()
|
|
97
|
+
return () => {
|
|
98
|
+
alive.current = false
|
|
99
|
+
request.current++
|
|
100
|
+
}
|
|
101
|
+
}, [presetManager, id])
|
|
102
|
+
|
|
103
|
+
const shown = value?.id === id ? value : undefined
|
|
104
|
+
return <section className="codex-preset-editor" aria-label={t('settingsDefinition')} aria-busy={loading}>
|
|
105
|
+
<style>{styles}</style>
|
|
106
|
+
<h3>{t('settingsDefinition')}</h3><code>{id}</code>
|
|
107
|
+
<button type="button" disabled={loading} onClick={() => { void load(true) }}>{t('pmRefresh')}</button>
|
|
108
|
+
{loading && !shown && <p role="status">{t('peLoading')}</p>}
|
|
109
|
+
{error && <p role="alert">{t('peReadError')}</p>}
|
|
110
|
+
{shown && <>
|
|
111
|
+
<strong>{shown.name}</strong>
|
|
112
|
+
<p>{shown.description}</p>
|
|
113
|
+
<pre style={{ whiteSpace: 'pre-wrap', overflowWrap: 'anywhere', margin: 0 }}>{shown.composition}</pre>
|
|
114
|
+
</>}
|
|
115
|
+
</section>
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const NOOP_SUBSCRIBE = () => () => {}
|
|
119
|
+
|
|
120
|
+
export const isGptModelName = (model: unknown): model is string =>
|
|
121
|
+
typeof model === 'string' && /(?:^|\/)(?:gpt|chatgpt)(?:[-_.]|\d|$)/i.test(model)
|
|
122
|
+
|
|
123
|
+
export function useModelDirectory(modelDirectory: ModelDirectoryStore | undefined): ModelDirectorySnapshot | undefined {
|
|
124
|
+
const subscribe = useCallback(
|
|
125
|
+
(listener: () => void) => modelDirectory !== undefined ? modelDirectory.subscribe(listener) : NOOP_SUBSCRIBE(),
|
|
126
|
+
[modelDirectory],
|
|
127
|
+
)
|
|
128
|
+
const getSnapshot = useCallback(() => modelDirectory?.getSnapshot(), [modelDirectory])
|
|
129
|
+
return useSyncExternalStore(subscribe, getSnapshot, getSnapshot)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export function pillStyle(active: boolean, writable: boolean) {
|
|
133
|
+
return {
|
|
134
|
+
boxSizing: 'border-box' as const,
|
|
135
|
+
display: 'inline-flex',
|
|
136
|
+
alignItems: 'center',
|
|
137
|
+
gap: 5,
|
|
138
|
+
height: 28,
|
|
139
|
+
border: 0,
|
|
140
|
+
borderRadius: 7,
|
|
141
|
+
padding: '0 6px',
|
|
142
|
+
font: 'inherit',
|
|
143
|
+
fontSize: 12,
|
|
144
|
+
lineHeight: '20px',
|
|
145
|
+
whiteSpace: 'nowrap' as const,
|
|
146
|
+
color: active ? 'var(--dsw-static-blue-500)' : 'var(--dsw-alias-label-secondary)',
|
|
147
|
+
background: active ? 'var(--dsw-alias-interactive-bg-selected)' : 'transparent',
|
|
148
|
+
cursor: writable ? 'pointer' : 'default',
|
|
149
|
+
fontWeight: active ? 600 : 400,
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
type DynamicCatalogMap = Readonly<Record<string, { readonly defaultTokens: number; readonly maxTokens: number }>>
|
|
154
|
+
let cachedConnectCatalog: DynamicCatalogMap | undefined
|
|
155
|
+
let catalogFetchPromise: Promise<DynamicCatalogMap | undefined> | undefined
|
|
156
|
+
|
|
157
|
+
async function loadConnectCatalog(): Promise<DynamicCatalogMap | undefined> {
|
|
158
|
+
if (cachedConnectCatalog !== undefined) return cachedConnectCatalog
|
|
159
|
+
if (typeof fetch !== 'function') return undefined
|
|
160
|
+
return catalogFetchPromise ??= fetch(OPENAI_CODEX_MODEL_CATALOG_PATH, {
|
|
161
|
+
headers: { accept: 'application/json' },
|
|
162
|
+
credentials: 'same-origin',
|
|
163
|
+
}).then(async res => {
|
|
164
|
+
if (!res.ok) return undefined
|
|
165
|
+
const rows = await res.json() as unknown
|
|
166
|
+
if (!Array.isArray(rows)) return undefined
|
|
167
|
+
const map: Record<string, { defaultTokens: number; maxTokens: number }> = {}
|
|
168
|
+
for (const row of rows) {
|
|
169
|
+
const rec = typeof row === 'object' && row !== null ? row as Record<string, unknown> : undefined
|
|
170
|
+
if (rec !== undefined && typeof rec.id === 'string' && typeof rec.contextWindow === 'number' && typeof rec.maxContextWindow === 'number') {
|
|
171
|
+
map[rec.id] = { defaultTokens: rec.contextWindow, maxTokens: rec.maxContextWindow }
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return cachedConnectCatalog = map
|
|
175
|
+
}).catch(() => undefined).finally(() => { catalogFetchPromise = undefined })
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const contextMenuCss = `
|
|
179
|
+
.codex-ctx-menu { position:fixed; z-index:1200; width:196px; padding:6px; border-radius:10px; font-size:12px; color:var(--dsw-alias-label-primary); background:var(--dsw-alias-bg-layer-1, #1f2329); border:0.5px solid var(--dsw-alias-border-l2); box-shadow:var(--dsw-shadow-lv2, 0 6px 20px rgb(0 0 0 / 16%)); box-sizing:border-box; }
|
|
180
|
+
.codex-ctx-head { padding:4px 8px 6px; font-size:11px; color:var(--dsw-alias-label-secondary); border-bottom:0.5px solid var(--dsw-alias-border-l2); margin-bottom:4px; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }
|
|
181
|
+
.codex-ctx-item { display:flex; align-items:center; gap:6px; width:100%; border:0; border-radius:6px; padding:5px 8px; background:transparent; color:inherit; font:inherit; text-align:left; cursor:pointer; }
|
|
182
|
+
.codex-ctx-item:hover:not(:disabled) { background:var(--dsw-alias-interactive-bg-hover, rgb(128 128 128 / 12%)); }
|
|
183
|
+
.codex-ctx-item[aria-checked=true] { color:var(--dsw-static-blue-500); font-weight:600; }
|
|
184
|
+
.codex-ctx-item:disabled { opacity:.5; cursor:default; }
|
|
185
|
+
.codex-ctx-form { display:flex; gap:4px; margin-top:4px; padding-top:6px; border-top:0.5px solid var(--dsw-alias-border-l2); }
|
|
186
|
+
.codex-ctx-form input { flex:1; min-width:0; height:26px; padding:0 7px; border-radius:6px; border:0.5px solid var(--dsw-alias-border-l3, var(--dsw-alias-border-l2)); background:var(--dsw-alias-bg-layer-2); color:inherit; font:inherit; font-variant-numeric:tabular-nums; }
|
|
187
|
+
.codex-ctx-form input[aria-invalid=true] { border-color:var(--dsw-alias-state-error-primary, #e5484d); }
|
|
188
|
+
.codex-ctx-form button { height:26px; padding:0 9px; border-radius:6px; border:0.5px solid var(--dsw-alias-border-l3, var(--dsw-alias-border-l2)); background:var(--dsw-alias-bg-layer-2); color:inherit; font:inherit; cursor:pointer; }
|
|
189
|
+
`
|
|
190
|
+
|
|
191
|
+
export interface ContextControlProps extends PropsLocale<'codex'> {
|
|
192
|
+
useSettings: (select: (state: unknown) => unknown) => unknown
|
|
193
|
+
setSetting: (field: string, value: unknown) => Promise<void>
|
|
194
|
+
unsetSetting: (field: string) => Promise<void>
|
|
195
|
+
modelDirectory?: ModelDirectoryStore | undefined
|
|
196
|
+
getCodexConnectScope?: (() => CodexConnectScope | undefined) | undefined
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export function ContextWindowControl(props: ContextControlProps) {
|
|
200
|
+
const { useSettings, setSetting, unsetSetting, modelDirectory, getCodexConnectScope, t } = props
|
|
201
|
+
const dirSnapshot = useModelDirectory(modelDirectory)
|
|
202
|
+
const settings = useSettings(state => state) as {
|
|
203
|
+
value?: { contextWindow?: number; contextWindowControlEnabled?: boolean }
|
|
204
|
+
writable: boolean
|
|
205
|
+
}
|
|
206
|
+
const hasDirectory = modelDirectory !== undefined
|
|
207
|
+
const provider = dirSnapshot?.current?.provider
|
|
208
|
+
const currentModel = dirSnapshot?.current?.model
|
|
209
|
+
const eligible = hasDirectory ? dirSnapshot?.status === 'ready' && isGptModelName(currentModel) : true
|
|
210
|
+
const isCodexConnect = hasDirectory && eligible && provider === OPENAI_CODEX_PROVIDER
|
|
211
|
+
const connectScope = isCodexConnect ? getCodexConnectScope?.() : undefined
|
|
212
|
+
const subscribeConnect = useCallback(
|
|
213
|
+
(listener: () => void) => connectScope !== undefined ? connectScope.subscribe(listener) : NOOP_SUBSCRIBE(),
|
|
214
|
+
[connectScope],
|
|
215
|
+
)
|
|
216
|
+
const getConnectSnapshot = useCallback(() => connectScope?.getSnapshot(), [connectScope])
|
|
217
|
+
const connectSnap = useSyncExternalStore(subscribeConnect, getConnectSnapshot, getConnectSnapshot)
|
|
218
|
+
|
|
219
|
+
const [open, setOpen] = useState(false)
|
|
220
|
+
const [customInput, setCustomInput] = useState('')
|
|
221
|
+
const [customInvalid, setCustomInvalid] = useState(false)
|
|
222
|
+
const [anchorRect, setAnchorRect] = useState<{ bottom: number; right: number }>()
|
|
223
|
+
const [dynamicCatalog, setDynamicCatalog] = useState<DynamicCatalogMap | undefined>(() => cachedConnectCatalog)
|
|
224
|
+
const fetchedModelsRef = useRef<Set<string>>(new Set())
|
|
225
|
+
const buttonRef = useRef<HTMLButtonElement>(null)
|
|
226
|
+
const menuRef = useRef<HTMLDivElement>(null)
|
|
227
|
+
const menuId = useId()
|
|
228
|
+
|
|
229
|
+
useEffect(() => {
|
|
230
|
+
if (!isCodexConnect || currentModel === undefined) return
|
|
231
|
+
if (OPENAI_CODEX_MODEL_LIMITS[currentModel] !== undefined || dynamicCatalog?.[currentModel] !== undefined) return
|
|
232
|
+
if (fetchedModelsRef.current.has(currentModel)) return
|
|
233
|
+
fetchedModelsRef.current.add(currentModel)
|
|
234
|
+
let cancelled = false
|
|
235
|
+
void loadConnectCatalog().then(catalog => {
|
|
236
|
+
if (!cancelled && catalog !== undefined) setDynamicCatalog(catalog)
|
|
237
|
+
})
|
|
238
|
+
return () => { cancelled = true }
|
|
239
|
+
}, [isCodexConnect, currentModel, dynamicCatalog])
|
|
240
|
+
|
|
241
|
+
useEffect(() => { setOpen(false); setCustomInvalid(false); setCustomInput('') }, [provider, currentModel])
|
|
242
|
+
|
|
243
|
+
useEffect(() => {
|
|
244
|
+
if (!open) return undefined
|
|
245
|
+
const updateAnchor = () => {
|
|
246
|
+
const rect = buttonRef.current?.getBoundingClientRect()
|
|
247
|
+
if (rect === undefined) return
|
|
248
|
+
setAnchorRect({
|
|
249
|
+
bottom: Math.max(8, window.innerHeight - rect.top + 6),
|
|
250
|
+
right: Math.max(8, window.innerWidth - rect.right),
|
|
251
|
+
})
|
|
252
|
+
}
|
|
253
|
+
const onKeyDown = (event: KeyboardEvent) => {
|
|
254
|
+
if (event.key !== 'Escape') return
|
|
255
|
+
setOpen(false); setCustomInvalid(false); buttonRef.current?.focus()
|
|
256
|
+
}
|
|
257
|
+
const onPointerDown = (event: PointerEvent) => {
|
|
258
|
+
const target = event.target as Node | null
|
|
259
|
+
if (target !== null && (buttonRef.current?.contains(target) || menuRef.current?.contains(target))) return
|
|
260
|
+
setOpen(false); setCustomInvalid(false)
|
|
261
|
+
}
|
|
262
|
+
updateAnchor()
|
|
263
|
+
window.addEventListener('resize', updateAnchor)
|
|
264
|
+
window.addEventListener('scroll', updateAnchor, true)
|
|
265
|
+
window.addEventListener('keydown', onKeyDown)
|
|
266
|
+
document.addEventListener('pointerdown', onPointerDown)
|
|
267
|
+
return () => {
|
|
268
|
+
window.removeEventListener('resize', updateAnchor)
|
|
269
|
+
window.removeEventListener('scroll', updateAnchor, true)
|
|
270
|
+
window.removeEventListener('keydown', onKeyDown)
|
|
271
|
+
document.removeEventListener('pointerdown', onPointerDown)
|
|
272
|
+
}
|
|
273
|
+
}, [open])
|
|
274
|
+
|
|
275
|
+
if (!(settings.value?.contextWindowControlEnabled ?? true) || !eligible) return null
|
|
276
|
+
if (isCodexConnect && connectSnap?.status === 'unavailable') return null
|
|
277
|
+
|
|
278
|
+
const bounds = resolveContextWindowBounds(provider, currentModel, dynamicCatalog)
|
|
279
|
+
const rawConnectOverride = isCodexConnect && typeof currentModel === 'string'
|
|
280
|
+
? connectSnap?.overrides?.[currentModel]
|
|
281
|
+
: undefined
|
|
282
|
+
const overrideTokens = isCodexConnect
|
|
283
|
+
? (typeof rawConnectOverride === 'number' && rawConnectOverride > 0 ? rawConnectOverride : undefined)
|
|
284
|
+
: settings.value?.contextWindow
|
|
285
|
+
const effectiveTokens = overrideTokens ?? bounds.defaultTokens
|
|
286
|
+
const writable = isCodexConnect ? connectSnap?.writable !== false : settings.writable !== false
|
|
287
|
+
const label = effectiveTokens !== undefined ? formatCompactTokens(effectiveTokens) : t('contextShortDefault')
|
|
288
|
+
const fullLabel = `${t('contextSize')}: ${effectiveTokens !== undefined ? `${effectiveTokens.toLocaleString()} tokens` : t('contextDefaultOption')}`
|
|
289
|
+
|
|
290
|
+
const applyBudget = (tokens: number | undefined): void => {
|
|
291
|
+
if (!writable) return
|
|
292
|
+
if (isCodexConnect) {
|
|
293
|
+
if (connectScope !== undefined && typeof currentModel === 'string') {
|
|
294
|
+
void connectScope.setModelOverride(currentModel, tokens).catch(() => {})
|
|
295
|
+
}
|
|
296
|
+
} else if (tokens === undefined) {
|
|
297
|
+
void unsetSetting('contextWindow').catch(() => {})
|
|
298
|
+
} else {
|
|
299
|
+
void setSetting('contextWindow', tokens).catch(() => {})
|
|
300
|
+
}
|
|
301
|
+
setOpen(false); setCustomInvalid(false); setCustomInput(''); buttonRef.current?.focus()
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
return <>
|
|
305
|
+
<button
|
|
306
|
+
ref={buttonRef}
|
|
307
|
+
type="button"
|
|
308
|
+
data-codex-context-control={overrideTokens !== undefined ? 'override' : 'default'}
|
|
309
|
+
aria-haspopup="menu"
|
|
310
|
+
aria-expanded={open}
|
|
311
|
+
aria-controls={open ? menuId : undefined}
|
|
312
|
+
aria-label={fullLabel}
|
|
313
|
+
title={fullLabel}
|
|
314
|
+
disabled={!writable}
|
|
315
|
+
onClick={() => {
|
|
316
|
+
if (!writable) return
|
|
317
|
+
if (!open && typeof window !== 'undefined') {
|
|
318
|
+
const rect = buttonRef.current?.getBoundingClientRect()
|
|
319
|
+
if (rect !== undefined) {
|
|
320
|
+
setAnchorRect({
|
|
321
|
+
bottom: Math.max(8, window.innerHeight - rect.top + 6),
|
|
322
|
+
right: Math.max(8, window.innerWidth - rect.right),
|
|
323
|
+
})
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
setOpen(current => !current); setCustomInvalid(false)
|
|
327
|
+
}}
|
|
328
|
+
style={{ ...pillStyle(overrideTokens !== undefined, writable), fontVariantNumeric: 'tabular-nums' }}
|
|
329
|
+
>{label}</button>
|
|
330
|
+
{open && typeof document !== 'undefined' ? createPortal(
|
|
331
|
+
<div
|
|
332
|
+
id={menuId}
|
|
333
|
+
ref={menuRef}
|
|
334
|
+
role="menu"
|
|
335
|
+
aria-label={t('contextSize')}
|
|
336
|
+
data-codex-context-menu
|
|
337
|
+
className="codex-ctx-menu"
|
|
338
|
+
style={{ bottom: anchorRect?.bottom ?? 48, right: anchorRect?.right ?? 16 }}
|
|
339
|
+
>
|
|
340
|
+
<style>{contextMenuCss}</style>
|
|
341
|
+
<div className="codex-ctx-head">{`${currentModel ?? 'GPT'} · ≤${formatCompactTokens(bounds.maxTokens)}`}</div>
|
|
342
|
+
<button
|
|
343
|
+
type="button" role="menuitemradio" className="codex-ctx-item"
|
|
344
|
+
aria-checked={overrideTokens === undefined} disabled={!writable} onClick={() => applyBudget(undefined)}
|
|
345
|
+
>
|
|
346
|
+
<span aria-hidden="true" style={{ width: 14, textAlign: 'center' }}>{overrideTokens === undefined ? '✓' : ''}</span>
|
|
347
|
+
<span>{bounds.defaultTokens !== undefined ? `${t('contextDefaultOption')} (${formatCompactTokens(bounds.defaultTokens)})` : t('contextDefaultOption')}</span>
|
|
348
|
+
</button>
|
|
349
|
+
{contextPresetsForBounds(bounds).map(tier => <button
|
|
350
|
+
key={tier}
|
|
351
|
+
type="button" role="menuitemradio" className="codex-ctx-item"
|
|
352
|
+
aria-checked={overrideTokens === tier} disabled={!writable} onClick={() => applyBudget(tier)}
|
|
353
|
+
style={{ fontVariantNumeric: 'tabular-nums' }}
|
|
354
|
+
>
|
|
355
|
+
<span aria-hidden="true" style={{ width: 14, textAlign: 'center' }}>{overrideTokens === tier ? '✓' : ''}</span>
|
|
356
|
+
<span>{formatCompactTokens(tier)}</span>
|
|
357
|
+
</button>)}
|
|
358
|
+
<form className="codex-ctx-form" onSubmit={event => {
|
|
359
|
+
event.preventDefault()
|
|
360
|
+
const parsed = parseCustomContextInput(customInput, bounds.maxTokens)
|
|
361
|
+
if (parsed === undefined) { setCustomInvalid(true); return }
|
|
362
|
+
applyBudget(parsed)
|
|
363
|
+
}}>
|
|
364
|
+
<input
|
|
365
|
+
type="text" inputMode="numeric"
|
|
366
|
+
placeholder={t('contextCustomPlaceholder')} aria-label={t('contextCustomPlaceholder')}
|
|
367
|
+
aria-invalid={customInvalid} value={customInput}
|
|
368
|
+
onChange={event => { setCustomInput(event.currentTarget.value); if (customInvalid) setCustomInvalid(false) }}
|
|
369
|
+
/>
|
|
370
|
+
<button type="submit" disabled={!writable}>{t('contextApply')}</button>
|
|
371
|
+
</form>
|
|
372
|
+
</div>,
|
|
373
|
+
document.body,
|
|
374
|
+
) : null}
|
|
375
|
+
</>
|
|
376
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import type { PresetCatalog } from '../contracts/ui-contracts.ts'
|
|
2
|
+
import type { PresetManagerActions } from './preset-manager.tsx'
|
|
3
|
+
import type { PresetPromptCatalog, PresetToolCatalog } from '../contracts/tool-policy-contract.ts'
|
|
4
|
+
|
|
5
|
+
const catalogs = new WeakMap<PresetManagerActions, PresetCatalog>()
|
|
6
|
+
const catalogRequests = new WeakMap<PresetManagerActions, Promise<PresetCatalog>>()
|
|
7
|
+
|
|
8
|
+
export function cachedPresetCatalog(manager: PresetManagerActions): PresetCatalog | undefined {
|
|
9
|
+
return catalogs.get(manager)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function cachePresetCatalog(manager: PresetManagerActions, catalog: PresetCatalog): void {
|
|
13
|
+
catalogs.set(manager, catalog)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** One cache and one in-flight catalog read for every surface using this manager. */
|
|
17
|
+
export function requestPresetCatalog(manager: PresetManagerActions, refresh = false): Promise<PresetCatalog> {
|
|
18
|
+
const pending = catalogRequests.get(manager)
|
|
19
|
+
if (pending !== undefined) return pending
|
|
20
|
+
const cached = catalogs.get(manager)
|
|
21
|
+
if (!refresh && cached !== undefined) return Promise.resolve(cached)
|
|
22
|
+
const request = (async () => {
|
|
23
|
+
await manager.ready
|
|
24
|
+
const result = await manager.remote.codexPresetEditor.catalog()
|
|
25
|
+
if (!result.ok) throw new Error(result.error.message ?? 'preset-catalog-unavailable')
|
|
26
|
+
catalogs.set(manager, result.value)
|
|
27
|
+
return result.value
|
|
28
|
+
})()
|
|
29
|
+
catalogRequests.set(manager, request)
|
|
30
|
+
void request.finally(() => {
|
|
31
|
+
if (catalogRequests.get(manager) === request) catalogRequests.delete(manager)
|
|
32
|
+
}).catch(() => {})
|
|
33
|
+
return request
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const toolCatalogs = new WeakMap<PresetManagerActions, Map<string, PresetToolCatalog>>()
|
|
37
|
+
const toolRequests = new WeakMap<PresetManagerActions, Map<string, Promise<PresetToolCatalog>>>()
|
|
38
|
+
|
|
39
|
+
export function cachedPresetTools(manager: PresetManagerActions, id: string): PresetToolCatalog | undefined {
|
|
40
|
+
return toolCatalogs.get(manager)?.get(id)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** The requested preset owns this snapshot; never substitute current-Agent tools. */
|
|
44
|
+
export function requestPresetTools(manager: PresetManagerActions, id: string, refresh = false): Promise<PresetToolCatalog> {
|
|
45
|
+
const pending = toolRequests.get(manager)?.get(id)
|
|
46
|
+
if (pending !== undefined) return pending
|
|
47
|
+
const cached = cachedPresetTools(manager, id)
|
|
48
|
+
if (!refresh && cached !== undefined) return Promise.resolve(cached)
|
|
49
|
+
const requests = toolRequests.get(manager) ?? new Map<string, Promise<PresetToolCatalog>>()
|
|
50
|
+
toolRequests.set(manager, requests)
|
|
51
|
+
const request = (async () => {
|
|
52
|
+
await manager.ready
|
|
53
|
+
const remote = manager.remote.codexPresetEditor
|
|
54
|
+
if (typeof remote.tools !== 'function') throw new Error('preset-tools-unavailable')
|
|
55
|
+
const result = await remote.tools(id)
|
|
56
|
+
if (!result.ok) throw new Error(result.error.message ?? 'preset-tools-unavailable')
|
|
57
|
+
if (result.value.preset !== id || result.value.registeredOnly !== true || !Array.isArray(result.value.tools)) {
|
|
58
|
+
throw new Error('preset-tools-mismatched-snapshot')
|
|
59
|
+
}
|
|
60
|
+
const catalogs = toolCatalogs.get(manager) ?? new Map<string, PresetToolCatalog>()
|
|
61
|
+
catalogs.set(id, result.value); toolCatalogs.set(manager, catalogs)
|
|
62
|
+
return result.value
|
|
63
|
+
})()
|
|
64
|
+
requests.set(id, request)
|
|
65
|
+
void request.finally(() => { if (requests.get(id) === request) requests.delete(id) }).catch(() => {})
|
|
66
|
+
return request
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const promptCatalogs = new WeakMap<PresetManagerActions, Map<string, PresetPromptCatalog>>()
|
|
70
|
+
const promptRequests = new WeakMap<PresetManagerActions, Map<string, Promise<PresetPromptCatalog>>>()
|
|
71
|
+
|
|
72
|
+
export function cachedPresetPrompts(manager: PresetManagerActions, id: string): PresetPromptCatalog | undefined {
|
|
73
|
+
return promptCatalogs.get(manager)?.get(id)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function requestPresetPrompts(manager: PresetManagerActions, id: string, refresh = false): Promise<PresetPromptCatalog> {
|
|
77
|
+
const pending = promptRequests.get(manager)?.get(id)
|
|
78
|
+
if (pending !== undefined) return pending
|
|
79
|
+
const cached = cachedPresetPrompts(manager, id)
|
|
80
|
+
if (!refresh && cached !== undefined) return Promise.resolve(cached)
|
|
81
|
+
const requests = promptRequests.get(manager) ?? new Map<string, Promise<PresetPromptCatalog>>()
|
|
82
|
+
promptRequests.set(manager, requests)
|
|
83
|
+
const request = (async () => {
|
|
84
|
+
await manager.ready
|
|
85
|
+
const remote = manager.remote.codexPresetEditor
|
|
86
|
+
if (typeof remote.prompts !== 'function') throw new Error('preset-prompts-unavailable')
|
|
87
|
+
const result = await remote.prompts(id)
|
|
88
|
+
if (!result.ok) throw new Error(result.error.message ?? 'preset-prompts-unavailable')
|
|
89
|
+
if (result.value.preset !== id || !Array.isArray(result.value.sections)) throw new Error('preset-prompts-mismatched-snapshot')
|
|
90
|
+
const catalogs = promptCatalogs.get(manager) ?? new Map<string, PresetPromptCatalog>()
|
|
91
|
+
catalogs.set(id, result.value); promptCatalogs.set(manager, catalogs)
|
|
92
|
+
return result.value
|
|
93
|
+
})()
|
|
94
|
+
requests.set(id, request)
|
|
95
|
+
void request.finally(() => { if (requests.get(id) === request) requests.delete(id) }).catch(() => {})
|
|
96
|
+
return request
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Only transport failures from a read can be retried without duplicating writes. */
|
|
100
|
+
export function isPresetConnectionError(error: unknown): boolean {
|
|
101
|
+
const message = error instanceof Error ? error.message : String(error)
|
|
102
|
+
return /Failed to fetch|NetworkError|Load failed|network request failed|no active Connection|connection.*(?:closed|offline)/i.test(message)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export async function readPresetWithRetry<T>(read: () => Promise<T>, active: () => boolean): Promise<T> {
|
|
106
|
+
try { return await read() }
|
|
107
|
+
catch (error) {
|
|
108
|
+
if (!isPresetConnectionError(error) || !active()) throw error
|
|
109
|
+
await new Promise(resolve => setTimeout(resolve, 300))
|
|
110
|
+
if (!active()) throw error
|
|
111
|
+
return read()
|
|
112
|
+
}
|
|
113
|
+
}
|