@shuind/dsh-codex-harness 0.3.1 → 0.3.3

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 (86) hide show
  1. package/CHANGELOG.md +29 -0
  2. package/README.md +21 -43
  3. package/docs/preset-library.md +2 -2
  4. package/docs/tool-policy.md +1 -1
  5. package/lib/client.js +1738 -327
  6. package/lib/compact-command.js +7 -1
  7. package/lib/compaction.js +7 -1
  8. package/lib/hybrid-executor.js +23 -12
  9. package/lib/index.js +1266 -36
  10. package/lib/preferences.js +21 -2
  11. package/lib/tool-policy.js +11 -2
  12. package/lib/typert.js +42 -1
  13. package/lib/types/client/desktop.d.ts +20 -42
  14. package/lib/types/client/desktop.js +191 -126
  15. package/lib/types/client/index.d.ts +104 -28
  16. package/lib/types/client/index.js +97 -15
  17. package/lib/types/client/policy-drafts.d.ts +18 -0
  18. package/lib/types/client/policy-drafts.js +27 -0
  19. package/lib/types/client/policy-locale.d.ts +80 -20
  20. package/lib/types/client/policy-locale.js +197 -80
  21. package/lib/types/client/preset-manager.d.ts +25 -2
  22. package/lib/types/client/preset-manager.js +89 -5
  23. package/lib/types/client/preset-requests.d.ts +44 -1
  24. package/lib/types/client/preset-requests.js +150 -0
  25. package/lib/types/client/tool-policy.d.ts +7 -2
  26. package/lib/types/client/tool-policy.js +48 -6
  27. package/lib/types/contracts/preset-editor-schema.d.ts +35 -0
  28. package/lib/types/contracts/preset-editor-schema.js +39 -0
  29. package/lib/types/contracts/settings.d.ts +12 -0
  30. package/lib/types/contracts/settings.js +10 -1
  31. package/lib/types/contracts/ui-contracts.d.ts +52 -0
  32. package/lib/types/contracts/ui-contracts.js +58 -0
  33. package/lib/types/contracts/update.d.ts +73 -0
  34. package/lib/types/contracts/update.js +291 -0
  35. package/lib/types/entries/index.js +20 -2
  36. package/lib/types/entries/preferences.d.ts +2 -0
  37. package/lib/types/entries/preferences.js +9 -0
  38. package/lib/types/hybrid/hybrid-sdk.d.ts +1 -1
  39. package/lib/types/hybrid/hybrid-sdk.js +27 -13
  40. package/lib/types/presets/preset-editor.d.ts +13 -1
  41. package/lib/types/presets/preset-editor.js +24 -1
  42. package/lib/types/presets/pro-mode-store.d.ts +13 -0
  43. package/lib/types/presets/pro-mode-store.js +69 -0
  44. package/lib/types/responses/background-sse-codec.d.ts +23 -0
  45. package/lib/types/responses/background-sse-codec.js +230 -0
  46. package/lib/types/responses/background-stream.d.ts +28 -0
  47. package/lib/types/responses/background-stream.js +351 -0
  48. package/lib/types/responses/compact-checkpoint.js +3 -10
  49. package/lib/types/responses/fetch-transport.js +24 -9
  50. package/lib/types/responses/remote-compact.js +39 -11
  51. package/lib/types/responses/wire-rewrite.d.ts +4 -0
  52. package/lib/types/responses/wire-rewrite.js +30 -4
  53. package/lib/types/tools/codex-subagent.d.ts +14 -0
  54. package/lib/types/tools/codex-subagent.js +269 -0
  55. package/lib/types/update/checker.d.ts +14 -0
  56. package/lib/types/update/checker.js +166 -0
  57. package/locale/en.json +1 -1
  58. package/locale/hybrid-executor/en.json +1 -1
  59. package/locale/hybrid-executor/zh.json +1 -1
  60. package/locale/zh.json +1 -1
  61. package/package.json +6 -2
  62. package/src/client/desktop.tsx +370 -153
  63. package/src/client/index.tsx +122 -22
  64. package/src/client/policy-drafts.ts +45 -0
  65. package/src/client/policy-locale.ts +197 -80
  66. package/src/client/preset-manager.tsx +125 -4
  67. package/src/client/preset-requests.ts +185 -1
  68. package/src/client/tool-policy.tsx +63 -9
  69. package/src/contracts/preset-editor-schema.ts +44 -0
  70. package/src/contracts/settings.ts +20 -1
  71. package/src/contracts/ui-contracts.ts +91 -0
  72. package/src/contracts/update.ts +307 -0
  73. package/src/entries/index.ts +25 -2
  74. package/src/entries/preferences.ts +9 -0
  75. package/src/hybrid/hybrid-sdk.ts +28 -12
  76. package/src/presets/preset-editor.ts +31 -3
  77. package/src/presets/pro-mode-store.ts +80 -0
  78. package/src/responses/background-sse-codec.ts +237 -0
  79. package/src/responses/background-stream.ts +371 -0
  80. package/src/responses/compact-checkpoint.ts +2 -8
  81. package/src/responses/fetch-transport.ts +28 -9
  82. package/src/responses/remote-compact.ts +41 -11
  83. package/src/responses/wire-rewrite.ts +34 -3
  84. package/src/tools/codex-subagent.ts +290 -0
  85. package/src/update/checker.ts +193 -0
  86. package/tsdown.config.ts +4 -0
@@ -0,0 +1,193 @@
1
+ /** Host-side update checker: npm dist-tags first, GitHub Releases fallback. */
2
+
3
+ import { readFile } from 'node:fs/promises'
4
+ import { dirname, join } from 'node:path'
5
+ import { fileURLToPath } from 'node:url'
6
+ import {
7
+ CODEX_HARNESS_GITHUB_LATEST_RELEASE_URL,
8
+ CODEX_HARNESS_NPM_METADATA_URL,
9
+ CODEX_HARNESS_PACKAGE_NAME,
10
+ compareHarnessVersions,
11
+ harnessReleaseUrl,
12
+ normalizeHarnessVersion,
13
+ parseHarnessVersion,
14
+ } from '../contracts/update.ts'
15
+ import type { HarnessUpdateResult } from '../contracts/update.ts'
16
+
17
+ export const CODEX_HARNESS_UPDATE_TIMEOUT_MS = 8_000
18
+ export const CODEX_HARNESS_UPDATE_MAX_BYTES = 64 * 1024
19
+
20
+ const MODULE_DIR = dirname(fileURLToPath(import.meta.url))
21
+ const MAX_ROOT_WALK_DEPTH = 6
22
+
23
+ type FetchImpl = (input: string, init?: RequestInit) => Promise<Response>
24
+
25
+ export interface HarnessUpdateCheckOptions {
26
+ currentVersion?: string
27
+ fetchImpl?: FetchImpl
28
+ timeoutMs?: number
29
+ }
30
+
31
+ type SourceLookup =
32
+ | { ok: true; latestVersion: string }
33
+ | { ok: false; reason: 'registry-unavailable' | 'invalid-registry-response' }
34
+
35
+ let cachedPackageVersion: string | undefined
36
+
37
+ export async function readCurrentHarnessVersion(startDir = MODULE_DIR): Promise<string | undefined> {
38
+ if (startDir === MODULE_DIR && cachedPackageVersion !== undefined) return cachedPackageVersion
39
+ let current = startDir
40
+ for (let depth = 0; depth <= MAX_ROOT_WALK_DEPTH; depth += 1) {
41
+ try {
42
+ const raw = await readFile(join(current, 'package.json'), 'utf8')
43
+ const parsed: unknown = JSON.parse(raw)
44
+ if (typeof parsed === 'object' && parsed !== null && !Array.isArray(parsed)) {
45
+ const record = parsed as Record<string, unknown>
46
+ if (record['name'] === CODEX_HARNESS_PACKAGE_NAME && typeof record['version'] === 'string' && parseHarnessVersion(record['version']) !== undefined) {
47
+ const normalized = normalizeHarnessVersion(record['version'])
48
+ if (startDir === MODULE_DIR) cachedPackageVersion = normalized
49
+ return normalized
50
+ }
51
+ }
52
+ } catch {
53
+ // Keep walking upward.
54
+ }
55
+ const parent = dirname(current)
56
+ if (parent === current) break
57
+ current = parent
58
+ }
59
+ return undefined
60
+ }
61
+
62
+ async function readBoundedText(response: Response, maxBytes: number): Promise<string> {
63
+ if (response.body === null) {
64
+ const text = await response.text()
65
+ if (new TextEncoder().encode(text).byteLength > maxBytes) throw new RangeError('update response too large')
66
+ return text
67
+ }
68
+ const reader = response.body.getReader()
69
+ const decoder = new TextDecoder()
70
+ const chunks: string[] = []
71
+ let total = 0
72
+ try {
73
+ while (true) {
74
+ const next = await reader.read()
75
+ if (next.done) break
76
+ total += next.value.byteLength
77
+ if (total > maxBytes) {
78
+ await reader.cancel()
79
+ throw new RangeError('update response too large')
80
+ }
81
+ chunks.push(decoder.decode(next.value, { stream: true }))
82
+ }
83
+ chunks.push(decoder.decode())
84
+ return chunks.join('')
85
+ } finally {
86
+ reader.releaseLock()
87
+ }
88
+ }
89
+
90
+ async function fetchBounded(
91
+ fetchImpl: FetchImpl,
92
+ url: string,
93
+ timeoutMs: number,
94
+ headers: Record<string, string>,
95
+ ): Promise<{ response: Response; text: string }> {
96
+ const controller = new AbortController()
97
+ const timer = setTimeout(() => { controller.abort(new Error('update request timed out')) }, timeoutMs)
98
+ try {
99
+ const response = await fetchImpl(url, { headers, signal: controller.signal })
100
+ const text = await readBoundedText(response, CODEX_HARNESS_UPDATE_MAX_BYTES)
101
+ return { response, text }
102
+ } finally {
103
+ clearTimeout(timer)
104
+ }
105
+ }
106
+
107
+ function registryCandidates(value: unknown): string[] {
108
+ if (typeof value !== 'object' || value === null || Array.isArray(value)) return []
109
+ const record = value as Record<string, unknown>
110
+ const tags = record['dist-tags'] ?? record
111
+ if (typeof tags !== 'object' || tags === null || Array.isArray(tags)) return []
112
+ return ['latest', 'alpha']
113
+ .map(tag => (tags as Record<string, unknown>)[tag])
114
+ .filter((candidate): candidate is string => typeof candidate === 'string' && parseHarnessVersion(candidate) !== undefined)
115
+ .map(normalizeHarnessVersion)
116
+ }
117
+
118
+ async function lookupNpmLatest(fetchImpl: FetchImpl, timeoutMs: number): Promise<SourceLookup> {
119
+ try {
120
+ const { response, text } = await fetchBounded(fetchImpl, CODEX_HARNESS_NPM_METADATA_URL, timeoutMs, { accept: 'application/json' })
121
+ if (!response.ok) return { ok: false, reason: 'registry-unavailable' }
122
+ const candidates = registryCandidates(JSON.parse(text) as unknown)
123
+ if (candidates.length === 0) return { ok: false, reason: 'invalid-registry-response' }
124
+ const latestVersion = candidates.reduce((best, item) => compareHarnessVersions(item, best) > 0 ? item : best)
125
+ return { ok: true, latestVersion }
126
+ } catch (error: unknown) {
127
+ return {
128
+ ok: false,
129
+ reason: error instanceof SyntaxError || error instanceof RangeError ? 'invalid-registry-response' : 'registry-unavailable',
130
+ }
131
+ }
132
+ }
133
+
134
+ async function lookupGitHubLatest(fetchImpl: FetchImpl, timeoutMs: number): Promise<SourceLookup> {
135
+ try {
136
+ const { response, text } = await fetchBounded(
137
+ fetchImpl,
138
+ CODEX_HARNESS_GITHUB_LATEST_RELEASE_URL,
139
+ timeoutMs,
140
+ { accept: 'application/vnd.github+json' },
141
+ )
142
+ if (!response.ok) return { ok: false, reason: 'registry-unavailable' }
143
+ const value = JSON.parse(text) as unknown
144
+ if (typeof value !== 'object' || value === null || Array.isArray(value)) {
145
+ return { ok: false, reason: 'invalid-registry-response' }
146
+ }
147
+ const tag = (value as Record<string, unknown>)['tag_name']
148
+ if (typeof tag !== 'string' || parseHarnessVersion(tag) === undefined) {
149
+ return { ok: false, reason: 'invalid-registry-response' }
150
+ }
151
+ return { ok: true, latestVersion: normalizeHarnessVersion(tag) }
152
+ } catch (error: unknown) {
153
+ return {
154
+ ok: false,
155
+ reason: error instanceof SyntaxError || error instanceof RangeError ? 'invalid-registry-response' : 'registry-unavailable',
156
+ }
157
+ }
158
+ }
159
+
160
+ export async function checkForHarnessUpdate(options: HarnessUpdateCheckOptions = {}): Promise<HarnessUpdateResult> {
161
+ const rawCurrent = options.currentVersion ?? await readCurrentHarnessVersion()
162
+ if (rawCurrent === undefined || parseHarnessVersion(rawCurrent) === undefined) {
163
+ return { status: 'unavailable', currentVersion: rawCurrent ?? '0.0.0', reason: 'invalid-current-version' }
164
+ }
165
+ const currentVersion = normalizeHarnessVersion(rawCurrent)
166
+ const fetchImpl = options.fetchImpl ?? fetch
167
+ const timeoutMs = options.timeoutMs ?? CODEX_HARNESS_UPDATE_TIMEOUT_MS
168
+
169
+ const npmResult = await lookupNpmLatest(fetchImpl, timeoutMs)
170
+ let latestVersion: string
171
+ if (npmResult.ok) {
172
+ latestVersion = npmResult.latestVersion
173
+ } else {
174
+ const githubResult = await lookupGitHubLatest(fetchImpl, timeoutMs)
175
+ if (!githubResult.ok) {
176
+ const reason = githubResult.reason === 'invalid-registry-response' || npmResult.reason === 'invalid-registry-response'
177
+ ? 'invalid-registry-response'
178
+ : 'registry-unavailable'
179
+ return { status: 'unavailable', currentVersion, reason }
180
+ }
181
+ latestVersion = githubResult.latestVersion
182
+ }
183
+
184
+ if (compareHarnessVersions(latestVersion, currentVersion) <= 0) {
185
+ return { status: 'up-to-date', currentVersion, latestVersion: currentVersion }
186
+ }
187
+ return {
188
+ status: 'update-available',
189
+ currentVersion,
190
+ latestVersion,
191
+ releaseUrl: harnessReleaseUrl(latestVersion),
192
+ }
193
+ }
package/tsdown.config.ts CHANGED
@@ -1,5 +1,8 @@
1
+ import { readFileSync } from 'node:fs'
1
2
  import { defineConfig } from 'tsdown'
2
3
 
4
+ const PACKAGE_VERSION = (JSON.parse(readFileSync(new URL('./package.json', import.meta.url), 'utf8')) as { version: string }).version
5
+
3
6
  // DSH's browser module table owns these identities. Bundling either one would
4
7
  // give slot components a second React runtime and make their Hooks invalid in
5
8
  // the host renderer.
@@ -94,6 +97,7 @@ export default defineConfig([
94
97
  'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV ?? 'production'),
95
98
  'import.meta.env.MODE': JSON.stringify(process.env.NODE_ENV ?? 'production'),
96
99
  'import.meta.env': JSON.stringify({ MODE: process.env.NODE_ENV ?? 'production' }),
100
+ __CODEX_HARNESS_VERSION__: JSON.stringify(PACKAGE_VERSION),
97
101
  },
98
102
  deps: {
99
103
  neverBundle: (specifier: string) => CLIENT_EXTERNALS.has(specifier),