@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
package/lib/types/index.js
DELETED
|
@@ -1,738 +0,0 @@
|
|
|
1
|
-
/** Codex-compatible prompt overlay and core tools for a dsh agent preset. */
|
|
2
|
-
import z from '@deepseek-ai/schemastery';
|
|
3
|
-
import { defineTool } from '@deepseek-ai/dsh-tools';
|
|
4
|
-
import { APPLY_PATCH_DESCRIPTION, applyPatchHunks, parsePatch } from "./patch.js";
|
|
5
|
-
import { renderExecResult, runExecCommand, runWriteStdin } from "./exec.js";
|
|
6
|
-
import { hostedWebSearchStream, installHostedWebSearch, remoteCompactStream, repairLegacyPiAiContextOverflow, } from "./remote.js";
|
|
7
|
-
import { registerCodexActivityProjection } from "./activity.js";
|
|
8
|
-
import { DEFAULT_CODEX_PERSONA, DEFAULT_CODEX_SYSTEM_PROMPT, DEFAULT_DSH_CORE_SOURCE_PROMPT, DEFAULT_DSH_CORE_WEB_PROMPT, resolveCodexSystemPrompt, } from "./prompt.js";
|
|
9
|
-
import { CODEX_SETTINGS_ENTRY, CODEX_SETTINGS_NAMESPACE, CODEX_SETTINGS_SCHEMA, } from "./settings.js";
|
|
10
|
-
import { codexCompactionMode } from "./compaction.js";
|
|
11
|
-
import { CodexPresetEditor } from "./preset-editor.js";
|
|
12
|
-
import { applyPromptSections, Config as PromptSectionsConfig } from "./prompt-sections.js";
|
|
13
|
-
export { DEFAULT_CODEX_PERSONA, DEFAULT_CODEX_SYSTEM_PROMPT, DEFAULT_DSH_CORE_SOURCE_PROMPT, DEFAULT_DSH_CORE_WEB_PROMPT, } from "./prompt.js";
|
|
14
|
-
export { CODEX_SETTINGS_NAMESPACE, CODEX_SETTINGS_SCHEMA } from "./settings.js";
|
|
15
|
-
export const name = 'codex';
|
|
16
|
-
export const inject = ['tools', 'systemPrompt', 'shell', 'fs', 'llm', 'credentials', 'settings'];
|
|
17
|
-
const DEFAULT_MAX_OUTPUT_BYTES = 1024 * 1024;
|
|
18
|
-
/** Add the preset editor when DSH's optional user-preset service is available. */
|
|
19
|
-
function installPresetEditor(ctx) {
|
|
20
|
-
// `agentPresets` is supplied by dsh-agent-presets and may be installed after
|
|
21
|
-
// this plugin. Waiting through inject keeps the Codex core usable on hosts
|
|
22
|
-
// without that optional service while still mounting the Remote as soon as
|
|
23
|
-
// the Web preset catalog becomes available.
|
|
24
|
-
ctx.inject(['agentPresets'], presetCtx => {
|
|
25
|
-
void (async () => {
|
|
26
|
-
try {
|
|
27
|
-
await presetCtx.plugin(CodexPresetEditor);
|
|
28
|
-
}
|
|
29
|
-
catch (error) {
|
|
30
|
-
ctx.logger.warn('codex: preset editor is unavailable on this host');
|
|
31
|
-
ctx.logger.warn(error);
|
|
32
|
-
}
|
|
33
|
-
})();
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
/** Runtime configuration schema for the Codex tool bridge. */
|
|
37
|
-
export const Config = z.object({
|
|
38
|
-
globalEnhancements: z.boolean().default(true),
|
|
39
|
-
codexCore: z.boolean().default(true),
|
|
40
|
-
defaultYieldTimeMs: z.number().step(1).min(0).default(10_000),
|
|
41
|
-
pollYieldTimeMs: z.number().step(1).min(0).default(5_000),
|
|
42
|
-
writeYieldTimeMs: z.number().step(1).min(0).default(250),
|
|
43
|
-
maxOutputBytes: z.number().step(1).min(1).default(DEFAULT_MAX_OUTPUT_BYTES),
|
|
44
|
-
hostedWebSearch: z.boolean().default(true),
|
|
45
|
-
remoteCompact: z.boolean().default(true),
|
|
46
|
-
systemPrompt: z.string().default(DEFAULT_CODEX_SYSTEM_PROMPT),
|
|
47
|
-
presetPrompt: z.string(),
|
|
48
|
-
presetSections: PromptSectionsConfig,
|
|
49
|
-
promptEnabled: z.boolean(),
|
|
50
|
-
terminalToolsEnabled: z.boolean(),
|
|
51
|
-
patchToolEnabled: z.boolean(),
|
|
52
|
-
planToolEnabled: z.boolean(),
|
|
53
|
-
hostedWebSearchEnabled: z.boolean(),
|
|
54
|
-
remoteCompactionEnabled: z.boolean(),
|
|
55
|
-
persona: z.string().default(DEFAULT_CODEX_PERSONA),
|
|
56
|
-
harnessSourcePrompt: z.string().default(DEFAULT_DSH_CORE_SOURCE_PROMPT),
|
|
57
|
-
webSurfacePrompt: z.string().default(DEFAULT_DSH_CORE_WEB_PROMPT),
|
|
58
|
-
});
|
|
59
|
-
const LLM_PI_AI_SETTINGS = 'llm-pi-ai';
|
|
60
|
-
const GPT_REASONING_EFFORTS = {
|
|
61
|
-
low: 'low',
|
|
62
|
-
medium: 'medium',
|
|
63
|
-
high: 'high',
|
|
64
|
-
xhigh: 'xhigh',
|
|
65
|
-
max: 'max',
|
|
66
|
-
};
|
|
67
|
-
function codexReasoningEfforts(configured) {
|
|
68
|
-
if (configured === false)
|
|
69
|
-
return false;
|
|
70
|
-
const filtered = configured === undefined
|
|
71
|
-
? {}
|
|
72
|
-
: Object.fromEntries(Object.entries(configured).filter(([level]) => level !== 'off' && level !== 'minimal'));
|
|
73
|
-
return Object.keys(filtered).length === 0 ? GPT_REASONING_EFFORTS : filtered;
|
|
74
|
-
}
|
|
75
|
-
/** GPT model ids are the only models whose relay capabilities Codex fills in. */
|
|
76
|
-
function isGptModel(id) {
|
|
77
|
-
return /(?:^|\/)(?:gpt|chatgpt)(?:[-_.]|\d|$)/i.test(id);
|
|
78
|
-
}
|
|
79
|
-
/** Add Codex defaults without overwriting explicit user capabilities. */
|
|
80
|
-
export function enrichCodexModel(model) {
|
|
81
|
-
if (!isGptModel(model.id))
|
|
82
|
-
return model;
|
|
83
|
-
const input = model.input === undefined || model.input.length === 0
|
|
84
|
-
? ['text', 'image']
|
|
85
|
-
: model.input;
|
|
86
|
-
const efforts = codexReasoningEfforts(model.reasoningEfforts);
|
|
87
|
-
if (input === model.input && efforts === model.reasoningEfforts)
|
|
88
|
-
return model;
|
|
89
|
-
return { ...model, input, reasoningEfforts: efforts };
|
|
90
|
-
}
|
|
91
|
-
/** The settings schema keys modelOverrides by id, so its values must not carry an id field. */
|
|
92
|
-
function enrichCodexOverride(id, model) {
|
|
93
|
-
if (!isGptModel(id))
|
|
94
|
-
return model;
|
|
95
|
-
const enriched = enrichCodexModel({ ...model, id });
|
|
96
|
-
const { id: _id, ...withoutId } = enriched;
|
|
97
|
-
const inputMissing = model.input === undefined || model.input.length === 0;
|
|
98
|
-
const nextEfforts = codexReasoningEfforts(model.reasoningEfforts);
|
|
99
|
-
const reasoningChanged = JSON.stringify(nextEfforts) !== JSON.stringify(model.reasoningEfforts);
|
|
100
|
-
if (!inputMissing && !reasoningChanged)
|
|
101
|
-
return model;
|
|
102
|
-
return { ...withoutId, reasoningEfforts: nextEfforts };
|
|
103
|
-
}
|
|
104
|
-
/** Persist only missing GPT capabilities into the user's existing pi-ai model config. */
|
|
105
|
-
async function enrichConfiguredGptModels(ctx) {
|
|
106
|
-
const settings = ctx.get('settings');
|
|
107
|
-
if (settings === undefined)
|
|
108
|
-
return;
|
|
109
|
-
const current = settings.get(LLM_PI_AI_SETTINGS);
|
|
110
|
-
if (current?.providers === undefined)
|
|
111
|
-
return;
|
|
112
|
-
const providers = {};
|
|
113
|
-
let changed = false;
|
|
114
|
-
for (const [provider, profile] of Object.entries(current.providers)) {
|
|
115
|
-
const models = profile.models;
|
|
116
|
-
const overrides = profile.modelOverrides;
|
|
117
|
-
const nextModels = Array.isArray(models) ? models.map(enrichCodexModel) : undefined;
|
|
118
|
-
const nextOverrides = overrides === undefined
|
|
119
|
-
? undefined
|
|
120
|
-
: Object.fromEntries(Object.entries(overrides).map(([id, model]) => [id, enrichCodexOverride(id, model)]));
|
|
121
|
-
const modelsChanged = nextModels !== undefined && nextModels.some((model, index) => model !== models?.[index]);
|
|
122
|
-
const overridesChanged = nextOverrides !== undefined
|
|
123
|
-
&& overrides !== undefined
|
|
124
|
-
&& Object.entries(nextOverrides).some(([id, model]) => model !== overrides[id]);
|
|
125
|
-
if (modelsChanged || overridesChanged) {
|
|
126
|
-
changed = true;
|
|
127
|
-
providers[provider] = {
|
|
128
|
-
...modelsChanged ? { models: nextModels } : {},
|
|
129
|
-
...overridesChanged ? { modelOverrides: nextOverrides } : {},
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
if (changed)
|
|
134
|
-
await settings.update(LLM_PI_AI_SETTINGS, { providers });
|
|
135
|
-
}
|
|
136
|
-
/** Keep newly edited GPT model entries enriched while Codex mode is mounted. */
|
|
137
|
-
function watchConfiguredGptModels(ctx) {
|
|
138
|
-
let tail = Promise.resolve();
|
|
139
|
-
const schedule = () => {
|
|
140
|
-
tail = tail.then(() => enrichConfiguredGptModels(ctx)).catch(error => {
|
|
141
|
-
ctx.logger.warn('codex: could not enrich configured GPT model capabilities');
|
|
142
|
-
ctx.logger.warn(error);
|
|
143
|
-
});
|
|
144
|
-
};
|
|
145
|
-
ctx.on('settings/document-updated', (ns) => {
|
|
146
|
-
if (ns === LLM_PI_AI_SETTINGS)
|
|
147
|
-
schedule();
|
|
148
|
-
});
|
|
149
|
-
schedule();
|
|
150
|
-
}
|
|
151
|
-
/** Install the optional settings source used by requests and prompt assembly. */
|
|
152
|
-
function installCodexSettings(ctx, systemPrompt, persona, harnessSourcePrompt, webSurfacePrompt) {
|
|
153
|
-
const entry = {
|
|
154
|
-
...CODEX_SETTINGS_ENTRY,
|
|
155
|
-
systemPrompt,
|
|
156
|
-
persona,
|
|
157
|
-
harnessSourcePrompt,
|
|
158
|
-
webSurfacePrompt,
|
|
159
|
-
};
|
|
160
|
-
const withDefaults = (value) => ({
|
|
161
|
-
...entry,
|
|
162
|
-
...value,
|
|
163
|
-
});
|
|
164
|
-
let source = () => entry;
|
|
165
|
-
let attached = false;
|
|
166
|
-
const attachSettings = (candidate) => {
|
|
167
|
-
if (attached || candidate === undefined || candidate === null)
|
|
168
|
-
return attached;
|
|
169
|
-
const settings = candidate;
|
|
170
|
-
if (typeof settings.get === 'function') {
|
|
171
|
-
const get = settings.get.bind(settings);
|
|
172
|
-
if (get(CODEX_SETTINGS_NAMESPACE) !== undefined) {
|
|
173
|
-
source = () => get(CODEX_SETTINGS_NAMESPACE);
|
|
174
|
-
attached = true;
|
|
175
|
-
return true;
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
if (typeof settings.installSection === 'function') {
|
|
179
|
-
settings.installSection.call(settings, ctx, CODEX_SETTINGS_NAMESPACE, CODEX_SETTINGS_SCHEMA, entry, {
|
|
180
|
-
setSource: (current) => { source = current; },
|
|
181
|
-
onChange: () => { },
|
|
182
|
-
});
|
|
183
|
-
attached = true;
|
|
184
|
-
return true;
|
|
185
|
-
}
|
|
186
|
-
if (typeof settings.register === 'function') {
|
|
187
|
-
const scope = settings.register.call(settings, CODEX_SETTINGS_NAMESPACE, CODEX_SETTINGS_SCHEMA, { base: entry });
|
|
188
|
-
source = () => scope.get();
|
|
189
|
-
attached = true;
|
|
190
|
-
return true;
|
|
191
|
-
}
|
|
192
|
-
return false;
|
|
193
|
-
};
|
|
194
|
-
if (!attachSettings(ctx.get('settings'))) {
|
|
195
|
-
ctx.inject(['settings'], (settingsCtx) => {
|
|
196
|
-
attachSettings(settingsCtx.settings);
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
|
-
return { current: () => withDefaults(source()) };
|
|
200
|
-
}
|
|
201
|
-
/** Read the event snapshot across the pre- and post-0.1.2 DSH session APIs. */
|
|
202
|
-
function sessionEvents(session) {
|
|
203
|
-
const compatible = session;
|
|
204
|
-
if (typeof compatible.snapshotEvents === 'function')
|
|
205
|
-
return compatible.snapshotEvents();
|
|
206
|
-
return compatible.events ?? [];
|
|
207
|
-
}
|
|
208
|
-
function hasOpenTurn(session) {
|
|
209
|
-
const events = sessionEvents(session);
|
|
210
|
-
const start = events.findLast(event => event.type === 'turn/start');
|
|
211
|
-
const end = events.findLast(event => event.type === 'turn/end');
|
|
212
|
-
return start !== undefined && (end === undefined || start.seq > end.seq);
|
|
213
|
-
}
|
|
214
|
-
/**
|
|
215
|
-
* Pair the live Codex capacity with the token-meter context projection.
|
|
216
|
-
*
|
|
217
|
-
* The core loop records adapter metadata in `request/context`, while Codex's
|
|
218
|
-
* setting is a request-scoped LLM capacity. Appending the override after the
|
|
219
|
-
* loop's metadata event keeps the official ContextMeter and compaction engine
|
|
220
|
-
* on the same effective capacity.
|
|
221
|
-
*/
|
|
222
|
-
export function syncCodexContextWindow(session, contextWindow) {
|
|
223
|
-
if (contextWindow === undefined
|
|
224
|
-
|| !Number.isSafeInteger(contextWindow)
|
|
225
|
-
|| contextWindow <= 0
|
|
226
|
-
|| !hasOpenTurn(session))
|
|
227
|
-
return;
|
|
228
|
-
const config = session.requestHeader()?.config;
|
|
229
|
-
if (typeof config?.provider !== 'string'
|
|
230
|
-
|| typeof config.model !== 'string'
|
|
231
|
-
|| !isGptModel(config.model))
|
|
232
|
-
return;
|
|
233
|
-
const current = session.requestContext();
|
|
234
|
-
if (current?.provider === config.provider
|
|
235
|
-
&& current.model === config.model
|
|
236
|
-
&& current.contextWindow === contextWindow)
|
|
237
|
-
return;
|
|
238
|
-
session.append('request/context', {
|
|
239
|
-
provider: config.provider,
|
|
240
|
-
model: config.model,
|
|
241
|
-
contextWindow,
|
|
242
|
-
});
|
|
243
|
-
}
|
|
244
|
-
/** Keep the official context meter synchronized with a live Codex override. */
|
|
245
|
-
function installCodexContextProjection(ctx, current) {
|
|
246
|
-
const sync = (session, contextWindow = current().contextWindow) => {
|
|
247
|
-
syncCodexContextWindow(session, contextWindow);
|
|
248
|
-
};
|
|
249
|
-
const pending = new WeakSet();
|
|
250
|
-
const syncAfterPublication = (session) => {
|
|
251
|
-
if (pending.has(session))
|
|
252
|
-
return;
|
|
253
|
-
pending.add(session);
|
|
254
|
-
queueMicrotask(() => {
|
|
255
|
-
try {
|
|
256
|
-
sync(session);
|
|
257
|
-
}
|
|
258
|
-
finally {
|
|
259
|
-
pending.delete(session);
|
|
260
|
-
}
|
|
261
|
-
});
|
|
262
|
-
};
|
|
263
|
-
const syncAll = (contextWindow = current().contextWindow) => {
|
|
264
|
-
const sessions = ctx.get('sessions');
|
|
265
|
-
for (const session of sessions?.list?.() ?? [])
|
|
266
|
-
sync(session, contextWindow);
|
|
267
|
-
};
|
|
268
|
-
ctx.on('session/event', (session, event) => {
|
|
269
|
-
// Session.append() rejects nested appends until every observer returns.
|
|
270
|
-
if (event.type === 'request/header' || event.type === 'request/context')
|
|
271
|
-
syncAfterPublication(session);
|
|
272
|
-
});
|
|
273
|
-
// Restored sessions may already contain their request/header seed, so no
|
|
274
|
-
// session/event is emitted for the initial capacity. Catch them at the
|
|
275
|
-
// store publication boundary as well.
|
|
276
|
-
ctx.on('session/created', (session) => { sync(session); });
|
|
277
|
-
// Use the resolved value carried by the consumer-facing event. This avoids
|
|
278
|
-
// waiting for the settings source closure to refresh before synchronizing
|
|
279
|
-
// already-open sessions.
|
|
280
|
-
ctx.on('settings/updated', (ns, next) => {
|
|
281
|
-
if (ns === CODEX_SETTINGS_NAMESPACE) {
|
|
282
|
-
syncAll(next.contextWindow);
|
|
283
|
-
}
|
|
284
|
-
});
|
|
285
|
-
ctx.on('settings/document-updated', ns => {
|
|
286
|
-
if (ns === CODEX_SETTINGS_NAMESPACE)
|
|
287
|
-
syncAll();
|
|
288
|
-
});
|
|
289
|
-
syncAll();
|
|
290
|
-
}
|
|
291
|
-
/** Apply the live Codex controls to one agent request without leaking them to other routes. */
|
|
292
|
-
export function applyCodexRequestSettings(request, settings) {
|
|
293
|
-
const { contextWindow: _inheritedContextWindow, serviceTier: _inheritedServiceTier, ...withoutCodexControls } = request;
|
|
294
|
-
if (!isGptModel(request.model))
|
|
295
|
-
return withoutCodexControls;
|
|
296
|
-
return {
|
|
297
|
-
...withoutCodexControls,
|
|
298
|
-
...settings.contextWindow === undefined ? {} : { contextWindow: settings.contextWindow },
|
|
299
|
-
...settings.fast ? { serviceTier: 'priority' } : {},
|
|
300
|
-
};
|
|
301
|
-
}
|
|
302
|
-
/** Put the agent persona first and omit the generic Harness identity opener. */
|
|
303
|
-
export function normalizeCodexPromptAssembly(assembly) {
|
|
304
|
-
const sections = assembly.sections.filter(section => section.name !== 'harness:identity');
|
|
305
|
-
const persona = sections.find(section => section.name === 'deployment:persona');
|
|
306
|
-
if (persona === undefined)
|
|
307
|
-
return { ...assembly, sections };
|
|
308
|
-
return {
|
|
309
|
-
...assembly,
|
|
310
|
-
sections: [persona, ...sections.filter(section => section.name !== 'deployment:persona')],
|
|
311
|
-
};
|
|
312
|
-
}
|
|
313
|
-
function sectionText(assembly, name) {
|
|
314
|
-
const section = assembly.sections.find(candidate => candidate.name === name);
|
|
315
|
-
return section?.text;
|
|
316
|
-
}
|
|
317
|
-
function promptVariables(assembly) {
|
|
318
|
-
const variables = { ...assembly.variables };
|
|
319
|
-
if (variables.sourceRoot === undefined) {
|
|
320
|
-
const source = sectionText(assembly, 'harness:source');
|
|
321
|
-
const match = source?.match(/^The DeepSeek Harness implementation checkout is at (.+?)\. The checkout location/u);
|
|
322
|
-
if (match?.[1] !== undefined)
|
|
323
|
-
variables.sourceRoot = match[1];
|
|
324
|
-
}
|
|
325
|
-
if (variables.webUrl === undefined) {
|
|
326
|
-
const web = sectionText(assembly, 'app:web-surface');
|
|
327
|
-
const match = web?.match(/^You are interacting with the user through the DeepSeek Harness Web GUI at (.+?)\. When/u);
|
|
328
|
-
if (match?.[1] !== undefined)
|
|
329
|
-
variables.webUrl = match[1];
|
|
330
|
-
}
|
|
331
|
-
return variables;
|
|
332
|
-
}
|
|
333
|
-
function interpolateCodexPrompt(text, variables) {
|
|
334
|
-
return text.replace(/\{\{([\w.-]+)\}\}/g, (whole, key) => variables[key] ?? whole);
|
|
335
|
-
}
|
|
336
|
-
/** Apply user-editable Persona and DSH Core runtime prompt templates. */
|
|
337
|
-
function applyCodexPromptOverrides(assembly, settings) {
|
|
338
|
-
const overrides = {
|
|
339
|
-
'deployment:persona': settings.persona,
|
|
340
|
-
'harness:source': settings.harnessSourcePrompt,
|
|
341
|
-
'app:web-surface': settings.webSurfacePrompt,
|
|
342
|
-
};
|
|
343
|
-
const defaults = {
|
|
344
|
-
'deployment:persona': DEFAULT_CODEX_PERSONA,
|
|
345
|
-
'harness:source': DEFAULT_DSH_CORE_SOURCE_PROMPT,
|
|
346
|
-
'app:web-surface': DEFAULT_DSH_CORE_WEB_PROMPT,
|
|
347
|
-
};
|
|
348
|
-
const hasCustomOverride = Object.entries(overrides).some(([name, text]) => (text !== undefined && text !== defaults[name]));
|
|
349
|
-
if (!hasCustomOverride)
|
|
350
|
-
return assembly;
|
|
351
|
-
const variables = promptVariables(assembly);
|
|
352
|
-
const sections = assembly.sections.map(section => {
|
|
353
|
-
const template = overrides[section.name];
|
|
354
|
-
return template === undefined || template === defaults[section.name]
|
|
355
|
-
? section
|
|
356
|
-
: { ...section, text: interpolateCodexPrompt(template, variables) };
|
|
357
|
-
});
|
|
358
|
-
return { ...assembly, sections, variables };
|
|
359
|
-
}
|
|
360
|
-
const CODEX_TOOL_NAMES = {
|
|
361
|
-
terminalToolsEnabled: new Set(['exec_command', 'write_stdin']),
|
|
362
|
-
patchToolEnabled: new Set(['apply_patch']),
|
|
363
|
-
planToolEnabled: new Set(['update_plan']),
|
|
364
|
-
};
|
|
365
|
-
/** Remove disabled Codex capabilities from the next model-visible assembly. */
|
|
366
|
-
export function applyCodexCapabilitySettings(assembly, settings) {
|
|
367
|
-
const disabled = new Set();
|
|
368
|
-
for (const [setting, names] of Object.entries(CODEX_TOOL_NAMES)) {
|
|
369
|
-
if (settings[setting])
|
|
370
|
-
continue;
|
|
371
|
-
for (const name of names)
|
|
372
|
-
disabled.add(name);
|
|
373
|
-
}
|
|
374
|
-
const filtered = disabled.size === 0
|
|
375
|
-
? assembly
|
|
376
|
-
: { ...assembly, tools: assembly.tools.filter(tool => !disabled.has(tool.name)) };
|
|
377
|
-
const withOverrides = applyCodexPromptOverrides(filtered, settings);
|
|
378
|
-
return settings.promptEnabled ? normalizeCodexPromptAssembly(withOverrides) : withOverrides;
|
|
379
|
-
}
|
|
380
|
-
/** Build the complete plugin-owned prompt, preserving an intentional empty value. */
|
|
381
|
-
export function buildCodexSystemPrompt(config = {}) {
|
|
382
|
-
return resolveCodexSystemPrompt(config.systemPrompt);
|
|
383
|
-
}
|
|
384
|
-
const EXEC_COMMAND_DESCRIPTION = 'Runs a shell command. By default it uses pipes: a running session can be polled for output but does not accept stdin. Set tty=true before starting a command that needs interactive input; background jobs use pipes.';
|
|
385
|
-
const WRITE_STDIN_DESCRIPTION = 'Polls output from an existing unified exec session. Non-empty chars can be sent only to a session created with tty=true; pipe-backed sessions accept empty chars for polling only.';
|
|
386
|
-
const UPDATE_PLAN_DESCRIPTION = 'Updates the task plan.\nProvide an optional explanation and a list of plan items, each with a step and status.\nAt most one step can be in_progress at a time.';
|
|
387
|
-
const PLAN_STATUSES = ['pending', 'in_progress', 'completed'];
|
|
388
|
-
function assertCapabilityEnabled(current, setting, toolName) {
|
|
389
|
-
if (!current()[setting])
|
|
390
|
-
throw new Error(`${toolName}: disabled in Codex Harness plugin settings`);
|
|
391
|
-
}
|
|
392
|
-
function sessionCwd(exec) {
|
|
393
|
-
return exec.agent?.session.header.cwd;
|
|
394
|
-
}
|
|
395
|
-
function resolvePolicy(ctx, exec) {
|
|
396
|
-
const policy = ctx.get('sandboxPolicy');
|
|
397
|
-
return policy?.resolve(exec.agent === undefined ? {} : { session: exec.agent.session });
|
|
398
|
-
}
|
|
399
|
-
async function resolveTarget(ctx, path, exec) {
|
|
400
|
-
const cwd = sessionCwd(exec);
|
|
401
|
-
return ctx.fs.resolve(path, cwd === undefined ? { signal: exec.signal } : { cwd, signal: exec.signal });
|
|
402
|
-
}
|
|
403
|
-
async function observedTarget(ctx, target, exec) {
|
|
404
|
-
const info = await ctx.fs.stat(target, exec.signal);
|
|
405
|
-
if (info === undefined)
|
|
406
|
-
throw new Error(`apply_patch: file not found: ${target.displayPath}`);
|
|
407
|
-
if (info.type !== 'file')
|
|
408
|
-
throw new Error(`apply_patch: not a regular file: ${target.displayPath}`);
|
|
409
|
-
ctx.emit('fs/observed', target, { kind: 'present', version: info.version }, exec);
|
|
410
|
-
return info;
|
|
411
|
-
}
|
|
412
|
-
async function writePatchedFile(ctx, target, content, fallback, exec, policy) {
|
|
413
|
-
const intent = await ctx.waterfall('fs/write-intent', target, exec, () => fallback);
|
|
414
|
-
const outcome = await ctx.fs.writeText(target, content, intent, exec.signal, policy);
|
|
415
|
-
ctx.emit('fs/observed', target, { kind: 'present', version: outcome.version }, exec);
|
|
416
|
-
return outcome.operation === 'create' ? 'created' : 'updated';
|
|
417
|
-
}
|
|
418
|
-
async function deletePatchedFile(ctx, target, version, exec, policy) {
|
|
419
|
-
const remove = ctx.fs.remove;
|
|
420
|
-
if (typeof remove !== 'function') {
|
|
421
|
-
throw new Error('apply_patch: the configured dsh filesystem does not support file deletion');
|
|
422
|
-
}
|
|
423
|
-
await remove.call(ctx.fs, target, { version }, exec.signal, policy);
|
|
424
|
-
ctx.emit('fs/observed', target, { kind: 'absent' }, exec);
|
|
425
|
-
}
|
|
426
|
-
async function applyOnePatch(ctx, file, exec, policy) {
|
|
427
|
-
const target = await resolveTarget(ctx, file.path, exec);
|
|
428
|
-
if (file.kind === 'add') {
|
|
429
|
-
const existing = await ctx.fs.stat(target, exec.signal);
|
|
430
|
-
if (existing !== undefined)
|
|
431
|
-
throw new Error(`apply_patch: file already exists: ${target.displayPath}`);
|
|
432
|
-
ctx.emit('fs/observed', target, { kind: 'absent' }, exec);
|
|
433
|
-
await writePatchedFile(ctx, target, file.content, { kind: 'createIfAbsent' }, exec, policy);
|
|
434
|
-
return { path: file.path, operation: 'created' };
|
|
435
|
-
}
|
|
436
|
-
const sourceInfo = await observedTarget(ctx, target, exec);
|
|
437
|
-
const original = await ctx.fs.readText(target, exec.signal);
|
|
438
|
-
const updated = file.kind === 'delete' ? undefined : applyPatchHunks(original, file.hunks, file.path);
|
|
439
|
-
if (file.kind === 'delete') {
|
|
440
|
-
await deletePatchedFile(ctx, target, sourceInfo.version, exec, policy);
|
|
441
|
-
return { path: file.path, operation: 'deleted' };
|
|
442
|
-
}
|
|
443
|
-
if (file.moveTo === undefined) {
|
|
444
|
-
await writePatchedFile(ctx, target, updated, { kind: 'replaceIfVersion', version: sourceInfo.version }, exec, policy);
|
|
445
|
-
return { path: file.path, operation: 'updated' };
|
|
446
|
-
}
|
|
447
|
-
const destination = await resolveTarget(ctx, file.moveTo, exec);
|
|
448
|
-
if (destination.targetKey === target.targetKey) {
|
|
449
|
-
await writePatchedFile(ctx, target, updated, { kind: 'replaceIfVersion', version: sourceInfo.version }, exec, policy);
|
|
450
|
-
return { path: file.path, operation: 'updated', moveTo: file.moveTo };
|
|
451
|
-
}
|
|
452
|
-
const destinationInfo = await ctx.fs.stat(destination, exec.signal);
|
|
453
|
-
if (destinationInfo !== undefined)
|
|
454
|
-
throw new Error(`apply_patch: move destination already exists: ${destination.displayPath}`);
|
|
455
|
-
ctx.emit('fs/observed', destination, { kind: 'absent' }, exec);
|
|
456
|
-
await writePatchedFile(ctx, destination, updated, { kind: 'createIfAbsent' }, exec, policy);
|
|
457
|
-
await deletePatchedFile(ctx, target, sourceInfo.version, exec, policy);
|
|
458
|
-
return { path: file.path, operation: 'moved', moveTo: file.moveTo };
|
|
459
|
-
}
|
|
460
|
-
function patchSummary(value) {
|
|
461
|
-
const letter = (operation) => {
|
|
462
|
-
switch (operation) {
|
|
463
|
-
case 'created': return 'A';
|
|
464
|
-
case 'updated': return 'M';
|
|
465
|
-
case 'deleted': return 'D';
|
|
466
|
-
case 'moved': return 'M';
|
|
467
|
-
default: return operation;
|
|
468
|
-
}
|
|
469
|
-
};
|
|
470
|
-
return `Success. Updated the following files:\n${value.files.map(file => `${letter(file.operation)} ${file.operation === 'moved' ? file.moveTo : file.path}`).join('\n')}\n`;
|
|
471
|
-
}
|
|
472
|
-
function planTodos(args) {
|
|
473
|
-
const seen = new Set();
|
|
474
|
-
let active = 0;
|
|
475
|
-
const todos = [];
|
|
476
|
-
for (const item of args.plan) {
|
|
477
|
-
const content = item.step.trim();
|
|
478
|
-
if (content.length === 0)
|
|
479
|
-
throw new Error('update_plan: every step must be non-empty');
|
|
480
|
-
if (seen.has(content))
|
|
481
|
-
throw new Error(`update_plan: duplicate step ${JSON.stringify(content)}`);
|
|
482
|
-
seen.add(content);
|
|
483
|
-
if (item.status === 'in_progress')
|
|
484
|
-
active++;
|
|
485
|
-
todos.push({ content, status: item.status });
|
|
486
|
-
}
|
|
487
|
-
if (active > 1)
|
|
488
|
-
throw new Error('update_plan: at most one step may be in_progress');
|
|
489
|
-
return todos;
|
|
490
|
-
}
|
|
491
|
-
function registerExecTools(ctx, config) {
|
|
492
|
-
ctx.tools.register(defineTool({
|
|
493
|
-
name: 'exec_command',
|
|
494
|
-
description: EXEC_COMMAND_DESCRIPTION,
|
|
495
|
-
parameters: {
|
|
496
|
-
cmd: { type: 'string', required: true, description: 'Shell command to execute.' },
|
|
497
|
-
workdir: { type: 'string', description: 'Working directory for the command. Defaults to the turn cwd.' },
|
|
498
|
-
tty: { type: 'boolean', description: 'True allocates a PTY for the command; false or omitted uses plain pipes.' },
|
|
499
|
-
run_in_background: { type: 'boolean', description: 'Run as a DSH background job and return its job id immediately; collect output with job_output and stop it with job_kill. Only supported for pipe-backed commands.' },
|
|
500
|
-
yield_time_ms: { type: 'number', description: 'Wait before yielding output. Defaults to 10000 ms; effective range is 250-30000 ms.' },
|
|
501
|
-
max_output_tokens: { type: 'number', description: 'Output token budget. Defaults to 10000 tokens; larger requests may be capped by policy.' },
|
|
502
|
-
shell: { type: 'string', description: "Shell binary to launch. Defaults to the user's default shell." },
|
|
503
|
-
login: { type: 'boolean', description: 'True runs the shell with -l/-i semantics; false disables them. Defaults to true.' },
|
|
504
|
-
},
|
|
505
|
-
output: {
|
|
506
|
-
schema: {
|
|
507
|
-
type: 'object',
|
|
508
|
-
additionalProperties: false,
|
|
509
|
-
properties: {
|
|
510
|
-
chunk_id: { type: 'string' },
|
|
511
|
-
wall_time_seconds: { type: 'number', required: true },
|
|
512
|
-
exit_code: { type: 'number' },
|
|
513
|
-
session_id: { type: 'number' },
|
|
514
|
-
job_id: { type: 'string' },
|
|
515
|
-
original_token_count: { type: 'number' },
|
|
516
|
-
output: { type: 'string', required: true },
|
|
517
|
-
},
|
|
518
|
-
},
|
|
519
|
-
render: (_args, value) => [{ type: 'text', text: renderExecResult(value) }],
|
|
520
|
-
},
|
|
521
|
-
async execute(args, exec) {
|
|
522
|
-
assertCapabilityEnabled(config.currentSettings, 'terminalToolsEnabled', 'exec_command');
|
|
523
|
-
return runExecCommand(ctx, args, exec, config);
|
|
524
|
-
},
|
|
525
|
-
presentCall: args => ({
|
|
526
|
-
card: 'terminal',
|
|
527
|
-
title: args.cmd,
|
|
528
|
-
...args.workdir === undefined ? {} : { cwd: args.workdir },
|
|
529
|
-
}),
|
|
530
|
-
}));
|
|
531
|
-
ctx.tools.register(defineTool({
|
|
532
|
-
name: 'write_stdin',
|
|
533
|
-
description: WRITE_STDIN_DESCRIPTION,
|
|
534
|
-
parameters: {
|
|
535
|
-
session_id: { type: 'number', required: true, description: 'Identifier of the running unified exec session.' },
|
|
536
|
-
chars: { type: 'string', description: 'Characters to send to a PTY session created with tty=true. Leave empty to poll output without writing; pipe-backed sessions do not accept input.' },
|
|
537
|
-
yield_time_ms: { type: 'number', description: 'Wait before yielding output. Non-empty writes default to 250 ms and cap at 30000 ms; empty polls wait 5000-300000 ms by default.' },
|
|
538
|
-
max_output_tokens: { type: 'number', description: 'Output token budget. Defaults to 10000 tokens; larger requests may be capped by policy.' },
|
|
539
|
-
},
|
|
540
|
-
output: {
|
|
541
|
-
schema: {
|
|
542
|
-
type: 'object',
|
|
543
|
-
additionalProperties: false,
|
|
544
|
-
properties: {
|
|
545
|
-
chunk_id: { type: 'string' },
|
|
546
|
-
wall_time_seconds: { type: 'number', required: true },
|
|
547
|
-
exit_code: { type: 'number' },
|
|
548
|
-
session_id: { type: 'number' },
|
|
549
|
-
original_token_count: { type: 'number' },
|
|
550
|
-
output: { type: 'string', required: true },
|
|
551
|
-
},
|
|
552
|
-
},
|
|
553
|
-
render: (_args, value) => [{ type: 'text', text: renderExecResult(value) }],
|
|
554
|
-
},
|
|
555
|
-
async execute(args, exec) {
|
|
556
|
-
assertCapabilityEnabled(config.currentSettings, 'terminalToolsEnabled', 'write_stdin');
|
|
557
|
-
return runWriteStdin(ctx, args, exec, config);
|
|
558
|
-
},
|
|
559
|
-
}));
|
|
560
|
-
}
|
|
561
|
-
function registerPatchTool(ctx, currentSettings) {
|
|
562
|
-
ctx.tools.register(defineTool({
|
|
563
|
-
name: 'apply_patch',
|
|
564
|
-
description: APPLY_PATCH_DESCRIPTION,
|
|
565
|
-
parameters: {
|
|
566
|
-
input: { type: 'string', required: true, description: 'The complete patch text.' },
|
|
567
|
-
},
|
|
568
|
-
output: {
|
|
569
|
-
schema: {
|
|
570
|
-
type: 'object',
|
|
571
|
-
additionalProperties: false,
|
|
572
|
-
properties: {
|
|
573
|
-
files: {
|
|
574
|
-
type: 'array',
|
|
575
|
-
required: true,
|
|
576
|
-
items: {
|
|
577
|
-
type: 'object',
|
|
578
|
-
additionalProperties: false,
|
|
579
|
-
properties: {
|
|
580
|
-
path: { type: 'string', required: true },
|
|
581
|
-
operation: { type: 'string', required: true, enum: ['created', 'updated', 'deleted', 'moved'] },
|
|
582
|
-
moveTo: { type: 'string' },
|
|
583
|
-
},
|
|
584
|
-
},
|
|
585
|
-
},
|
|
586
|
-
},
|
|
587
|
-
},
|
|
588
|
-
render: (_args, value) => [{ type: 'text', text: patchSummary(value) }],
|
|
589
|
-
},
|
|
590
|
-
async execute(args, exec) {
|
|
591
|
-
assertCapabilityEnabled(currentSettings, 'patchToolEnabled', 'apply_patch');
|
|
592
|
-
const files = parsePatch(args.input);
|
|
593
|
-
const policy = resolvePolicy(ctx, exec);
|
|
594
|
-
const applied = [];
|
|
595
|
-
for (const file of files)
|
|
596
|
-
applied.push(await applyOnePatch(ctx, file, exec, policy));
|
|
597
|
-
return { files: applied };
|
|
598
|
-
},
|
|
599
|
-
presentCall(args) {
|
|
600
|
-
return {
|
|
601
|
-
card: 'generic',
|
|
602
|
-
title: 'Apply patch',
|
|
603
|
-
kind: 'edit',
|
|
604
|
-
rawInput: args.input,
|
|
605
|
-
};
|
|
606
|
-
},
|
|
607
|
-
}));
|
|
608
|
-
}
|
|
609
|
-
function registerPlanTool(ctx, currentSettings) {
|
|
610
|
-
ctx.tools.register(defineTool({
|
|
611
|
-
name: 'update_plan',
|
|
612
|
-
description: UPDATE_PLAN_DESCRIPTION,
|
|
613
|
-
parameters: {
|
|
614
|
-
explanation: { type: 'string', description: 'Optional explanation for this plan update.' },
|
|
615
|
-
plan: {
|
|
616
|
-
type: 'array',
|
|
617
|
-
required: true,
|
|
618
|
-
description: 'The list of steps',
|
|
619
|
-
items: {
|
|
620
|
-
type: 'object',
|
|
621
|
-
additionalProperties: false,
|
|
622
|
-
properties: {
|
|
623
|
-
step: { type: 'string', required: true, description: 'Task step text.' },
|
|
624
|
-
status: { type: 'string', required: true, enum: [...PLAN_STATUSES], description: 'Step status.' },
|
|
625
|
-
},
|
|
626
|
-
},
|
|
627
|
-
},
|
|
628
|
-
},
|
|
629
|
-
output: {
|
|
630
|
-
schema: { type: 'object', additionalProperties: false, properties: {} },
|
|
631
|
-
render: () => [{ type: 'text', text: 'Plan updated' }],
|
|
632
|
-
},
|
|
633
|
-
execute(args, exec) {
|
|
634
|
-
assertCapabilityEnabled(currentSettings, 'planToolEnabled', 'update_plan');
|
|
635
|
-
const agent = exec.agent;
|
|
636
|
-
if (agent === undefined)
|
|
637
|
-
throw new Error('update_plan requires an owning agent session');
|
|
638
|
-
agent.session.append('todo/write', { todos: planTodos(args) });
|
|
639
|
-
return Promise.resolve({});
|
|
640
|
-
},
|
|
641
|
-
}));
|
|
642
|
-
}
|
|
643
|
-
/** Explicit preset instructions win without changing shared tools or permissions. */
|
|
644
|
-
export function withPresetPrompt(settings, presetPrompt) {
|
|
645
|
-
return presetPrompt === undefined ? settings : { ...settings, systemPrompt: presetPrompt };
|
|
646
|
-
}
|
|
647
|
-
/** Mount the Codex prompt/tool layer inside one fixed agent preset. */
|
|
648
|
-
export function apply(ctx, config = {}) {
|
|
649
|
-
installPresetEditor(ctx);
|
|
650
|
-
const resolved = {
|
|
651
|
-
globalEnhancements: config.globalEnhancements ?? true,
|
|
652
|
-
codexCore: config.codexCore ?? true,
|
|
653
|
-
defaultYieldTimeMs: config.defaultYieldTimeMs ?? 10_000,
|
|
654
|
-
pollYieldTimeMs: config.pollYieldTimeMs ?? 5_000,
|
|
655
|
-
writeYieldTimeMs: config.writeYieldTimeMs ?? 250,
|
|
656
|
-
maxOutputBytes: config.maxOutputBytes ?? DEFAULT_MAX_OUTPUT_BYTES,
|
|
657
|
-
hostedWebSearch: config.hostedWebSearch ?? true,
|
|
658
|
-
remoteCompact: config.remoteCompact ?? true,
|
|
659
|
-
systemPrompt: config.systemPrompt ?? DEFAULT_CODEX_SYSTEM_PROMPT,
|
|
660
|
-
persona: config.persona ?? DEFAULT_CODEX_PERSONA,
|
|
661
|
-
harnessSourcePrompt: config.harnessSourcePrompt ?? DEFAULT_DSH_CORE_SOURCE_PROMPT,
|
|
662
|
-
webSurfacePrompt: config.webSurfacePrompt ?? DEFAULT_DSH_CORE_WEB_PROMPT,
|
|
663
|
-
};
|
|
664
|
-
if (resolved.codexCore
|
|
665
|
-
&& ctx.fs.sandboxMode !== undefined
|
|
666
|
-
&& ctx.get('sandboxPolicy') === undefined) {
|
|
667
|
-
throw new Error('codex: a sandboxing filesystem requires ctx.sandboxPolicy');
|
|
668
|
-
}
|
|
669
|
-
const sharedSettings = installCodexSettings(ctx, resolved.systemPrompt, resolved.persona, resolved.harnessSourcePrompt, resolved.webSurfacePrompt);
|
|
670
|
-
const codexSettings = { current: () => {
|
|
671
|
-
const shared = withPresetPrompt(sharedSettings.current(), config.presetPrompt);
|
|
672
|
-
return {
|
|
673
|
-
...shared,
|
|
674
|
-
...(config.promptEnabled === undefined ? {} : { promptEnabled: config.promptEnabled }),
|
|
675
|
-
...(config.terminalToolsEnabled === undefined ? {} : { terminalToolsEnabled: config.terminalToolsEnabled }),
|
|
676
|
-
...(config.patchToolEnabled === undefined ? {} : { patchToolEnabled: config.patchToolEnabled }),
|
|
677
|
-
...(config.planToolEnabled === undefined ? {} : { planToolEnabled: config.planToolEnabled }),
|
|
678
|
-
...(config.hostedWebSearchEnabled === undefined ? {} : { hostedWebSearchEnabled: config.hostedWebSearchEnabled }),
|
|
679
|
-
...(config.remoteCompactionEnabled === undefined ? {} : { remoteCompactionEnabled: config.remoteCompactionEnabled }),
|
|
680
|
-
};
|
|
681
|
-
} };
|
|
682
|
-
const execConfig = { ...resolved, currentSettings: codexSettings.current };
|
|
683
|
-
if (resolved.globalEnhancements) {
|
|
684
|
-
// The generic pi-ai plugin remains the owner of provider routes. This
|
|
685
|
-
// layer adds only request metadata and remote-first transport behavior.
|
|
686
|
-
watchConfiguredGptModels(ctx);
|
|
687
|
-
registerCodexActivityProjection(ctx);
|
|
688
|
-
installCodexContextProjection(ctx, codexSettings.current);
|
|
689
|
-
ctx.on('agent/request', async (_payload, next) => {
|
|
690
|
-
const request = await next();
|
|
691
|
-
return applyCodexRequestSettings(request, codexSettings.current());
|
|
692
|
-
});
|
|
693
|
-
if (typeof ctx.effect === 'function') {
|
|
694
|
-
const disposeHostedWebSearch = installHostedWebSearch();
|
|
695
|
-
ctx.effect(() => disposeHostedWebSearch, 'codex: Responses transport wrapper');
|
|
696
|
-
}
|
|
697
|
-
ctx.on('llm/stream', ((options, next) => {
|
|
698
|
-
const settings = codexSettings.current();
|
|
699
|
-
const contextWindow = options.contextWindow ?? settings.contextWindow;
|
|
700
|
-
const compactionMode = codexCompactionMode();
|
|
701
|
-
const legacySafeNext = () => repairLegacyPiAiContextOverflow(next(), options.model, contextWindow);
|
|
702
|
-
if (options.purpose === 'compaction' && isGptModel(options.model)) {
|
|
703
|
-
const useRemoteCompaction = compactionMode === 'remote'
|
|
704
|
-
|| (compactionMode !== 'local' && resolved.remoteCompact && settings.remoteCompactionEnabled);
|
|
705
|
-
if (useRemoteCompaction) {
|
|
706
|
-
return remoteCompactStream(ctx, options, legacySafeNext, {
|
|
707
|
-
hostedWebSearch: resolved.hostedWebSearch && settings.hostedWebSearchEnabled,
|
|
708
|
-
customApplyPatch: settings.patchToolEnabled,
|
|
709
|
-
});
|
|
710
|
-
}
|
|
711
|
-
return legacySafeNext();
|
|
712
|
-
}
|
|
713
|
-
if (options.purpose === undefined && isGptModel(options.model)) {
|
|
714
|
-
return repairLegacyPiAiContextOverflow(hostedWebSearchStream(next, {
|
|
715
|
-
serviceTier: options.serviceTier,
|
|
716
|
-
hostedWebSearch: resolved.hostedWebSearch && settings.hostedWebSearchEnabled,
|
|
717
|
-
customApplyPatch: settings.patchToolEnabled,
|
|
718
|
-
}), options.model, contextWindow);
|
|
719
|
-
}
|
|
720
|
-
return next();
|
|
721
|
-
}));
|
|
722
|
-
}
|
|
723
|
-
if (resolved.codexCore) {
|
|
724
|
-
ctx.on('system-prompt/assemble', async (_assembly, _context, next) => applyPromptSections(applyCodexCapabilitySettings(await next(), codexSettings.current()), config.presetSections ?? {}));
|
|
725
|
-
ctx.systemPrompt.section({
|
|
726
|
-
name: 'codex:base',
|
|
727
|
-
order: 10,
|
|
728
|
-
text: () => codexSettings.current().promptEnabled
|
|
729
|
-
? resolveCodexSystemPrompt(codexSettings.current().systemPrompt)
|
|
730
|
-
: '',
|
|
731
|
-
});
|
|
732
|
-
registerExecTools(ctx, execConfig);
|
|
733
|
-
registerPatchTool(ctx, codexSettings.current);
|
|
734
|
-
registerPlanTool(ctx, codexSettings.current);
|
|
735
|
-
}
|
|
736
|
-
}
|
|
737
|
-
export default { name, inject, Config, apply };
|
|
738
|
-
//# sourceMappingURL=index.js.map
|