@shuind/dsh-codex-harness 0.2.3 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,49 +1,49 @@
1
- # @shuind/dsh-codex-harness
2
-
3
- 为 DSH 提供可自定义提示词与 Codex 工具的编码模式。
4
-
5
- ## 功能
6
-
7
- - 使用精简codex提示词;对gpt过度拉屎做了少量提示词约束。
8
- - 在 **设置 → 插件 → 插件配置 → Codex Harness** 中查看、编辑或恢复完整提示词。
9
- - 提供 `exec_command`、`write_stdin`、`apply_patch` 和 `update_plan`。
10
- - 支持 Fast 请求、GPT 图片与思考强度和上下文容量。
11
- - 实时显示请求模型、模型回复和上下文压缩状态及耗时。
12
- - Responses 请求优先使用原生 `apply_patch`、hosted `web_search` 和远程压缩,失败时回退到 DSH 实现。
13
-
14
- ## 安装
15
-
16
- ```sh
17
- dsh plugin --profile web add @shuind/dsh-codex-harness@0.2.1
18
- ```
19
-
20
- 重启 Web,在 **Agent 预设** 中选择 **Codex 模式**,再创建会话。
21
-
22
- ## 配置
23
-
24
- ### 模型
25
-
26
- 在 DSH 的 Models 中配置支持 OpenAI Responses 的 provider、endpoint、API key 和 GPT 模型。例如:
27
-
28
- ```yaml
29
- api: openai-responses
30
- baseURL: https://your-responses-endpoint.example.com
31
- apiKeyEnv: OPENAI_API_KEY
32
- ```
33
-
34
- 也可以配合 [dsh-codex-connect](https://github.com/shuind/dsh-codex-connect) 使用 ChatGPT 订阅模型。
35
-
36
- ### 提示词
37
-
38
- 打开 **设置 → 插件 → 插件配置 → Codex Harness**。编辑器展示本插件实际注入的完整提示词;保存后从下一次请求开始生效,点击“恢复默认”可回到随包模板。
39
-
40
- 身份、模型、工作目录、工具说明和 DSH 运行时上下文由对应插件动态提供,不会写死在可编辑模板中。
41
-
42
- ### Fast 与上下文
43
-
44
- - **Fast**:在 Codex 模式的模型菜单中开启,向 Responses 请求发送 `service_tier: "priority"`。
45
- - **上下文容量**:点击输入栏旁的上下文用量指示器,可设置 `1K`–`1000K` tokens;下一次请求和自动压缩使用该值。
46
-
47
- ## License
48
-
49
- MIT
1
+ # @shuind/dsh-codex-harness
2
+
3
+ 为 DSH 提供可自定义提示词与 Codex 工具的编码模式。
4
+
5
+ ## 功能
6
+
7
+ - 使用精简codex提示词;对gpt过度拉屎做了少量提示词约束。
8
+ - 在 **设置 → 插件 → 插件配置 → Codex Harness** 中查看、编辑或恢复完整提示词。
9
+ - 提供 `exec_command`、`write_stdin`、`apply_patch` 和 `update_plan`。
10
+ - 支持 Fast 请求、GPT 图片与思考强度和上下文容量。
11
+ - 实时显示请求模型、模型回复和上下文压缩状态及耗时。
12
+ - Responses 请求优先使用原生 `apply_patch`、hosted `web_search` 和远程压缩,失败时回退到 DSH 实现。
13
+
14
+ ## 安装
15
+
16
+ ```sh
17
+ dsh plugin --profile web add @shuind/dsh-codex-harness@0.2.1
18
+ ```
19
+
20
+ 重启 Web,在 **Agent 预设** 中选择 **Codex 模式**,再创建会话。
21
+
22
+ ## 配置
23
+
24
+ ### 模型
25
+
26
+ 在 DSH 的 Models 中配置支持 OpenAI Responses 的 provider、endpoint、API key 和 GPT 模型。例如:
27
+
28
+ ```yaml
29
+ api: openai-responses
30
+ baseURL: https://your-responses-endpoint.example.com
31
+ apiKeyEnv: OPENAI_API_KEY
32
+ ```
33
+
34
+ 也可以配合 [dsh-codex-connect](https://github.com/shuind/dsh-codex-connect) 使用 ChatGPT 订阅模型。
35
+
36
+ ### 提示词
37
+
38
+ 打开 **设置 → 插件 → 插件配置 → Codex Harness**。编辑器展示本插件实际注入的完整提示词;保存后从下一次请求开始生效,点击“恢复默认”可回到随包模板。
39
+
40
+ 身份、模型、工作目录、工具说明和 DSH 运行时上下文由对应插件动态提供,不会写死在可编辑模板中。
41
+
42
+ ### Fast 与上下文
43
+
44
+ - **Fast**:在 Codex 模式的模型菜单中开启,向 Responses 请求发送 `service_tier: "priority"`。
45
+ - **上下文容量**:点击输入栏旁的上下文用量指示器,可设置 `1K`–`1000K` tokens;下一次请求和自动压缩使用该值。
46
+
47
+ ## License
48
+
49
+ MIT
package/cordis.patch.yml CHANGED
@@ -1,11 +1,11 @@
1
- - insert:
2
- # Keep preset installation and settings registration on the host plane.
3
- - id: codex-preset-installer
4
- name: '@shuind/dsh-codex-harness/installer'
5
- # This row owns the global capability layer and also advertises the
6
- # browser face. Codex prompt/tools remain agent-preset scoped.
7
- - id: codex-client
8
- name: '@shuind/dsh-codex-harness'
9
- config:
10
- globalEnhancements: true
11
- codexCore: false
1
+ - insert:
2
+ # Keep preset installation and settings registration on the host plane.
3
+ - id: codex-preset-installer
4
+ name: '@shuind/dsh-codex-harness/installer'
5
+ # This row owns the global capability layer and also advertises the
6
+ # browser face. Codex prompt/tools remain agent-preset scoped.
7
+ - id: codex-client
8
+ name: '@shuind/dsh-codex-harness'
9
+ config:
10
+ globalEnhancements: true
11
+ codexCore: false
package/lib/index.js CHANGED
@@ -526,6 +526,7 @@ async function runWriteStdin(ctx, args, exec, config) {
526
526
  const SETTINGS_NAMESPACE = "llm-pi-ai";
527
527
  const REMOTE_COMPACTION_OPEN = "<codex-remote-compaction>";
528
528
  const REMOTE_COMPACTION_CLOSE = "</codex-remote-compaction>";
529
+ const CONTEXT_WINDOW_EXCEEDED_CODE = "CONTEXT_WINDOW_EXCEEDED";
529
530
  const CODEX_ERROR_DIRECTORY = ["diagnostics", "openai-codex-errors"];
530
531
  const HOSTED_REQUESTS = new AsyncLocalStorage();
531
532
  let hostedPatchUsers = 0;
@@ -908,6 +909,38 @@ function hostedWebSearchStream(next, options = {}) {
908
909
  }
909
910
  })();
910
911
  }
912
+ function contextTokens(usage) {
913
+ const fields = [
914
+ usage.inputTokens,
915
+ usage.outputTokens,
916
+ usage.cacheReadTokens ?? 0,
917
+ usage.cacheWriteTokens ?? 0
918
+ ];
919
+ if (fields.some((value) => !Number.isSafeInteger(value) || value < 0)) return void 0;
920
+ const total = fields.reduce((sum, value) => sum + value, 0);
921
+ return Number.isSafeInteger(total) ? total : void 0;
922
+ }
923
+ /**
924
+ * Correct the usage-only overflow verdict emitted by pi-ai adapters that do
925
+ * not yet consume the request-scoped Codex capacity.
926
+ */
927
+ function repairLegacyPiAiContextOverflow(stream, model, contextWindow) {
928
+ return (async function* () {
929
+ let usage;
930
+ for await (const chunk of stream) {
931
+ if (chunk.type === "usage") usage = chunk.usage;
932
+ const usedTokens = usage === void 0 ? void 0 : contextTokens(usage);
933
+ if (chunk.type === "finish" && chunk.reason.kind === "error" && chunk.reason.failure.code === CONTEXT_WINDOW_EXCEEDED_CODE && chunk.reason.failure.message === `pi-ai detected context overflow for model "${model}"` && typeof contextWindow === "number" && Number.isSafeInteger(contextWindow) && contextWindow > 0 && usage !== void 0 && usage.outputTokens > 0 && usedTokens !== void 0 && usedTokens <= contextWindow) {
934
+ yield {
935
+ ...chunk,
936
+ reason: { kind: "stop" }
937
+ };
938
+ continue;
939
+ }
940
+ yield chunk;
941
+ }
942
+ })();
943
+ }
911
944
  function textOf(message) {
912
945
  return message.content.filter((block) => block.type === "text").map((block) => block.text).join("");
913
946
  }
@@ -1853,15 +1886,16 @@ function apply(ctx, config = {}) {
1853
1886
  }
1854
1887
  ctx.on("llm/stream", ((options, next) => {
1855
1888
  const settings = codexSettings.current();
1856
- if (resolved.remoteCompact && settings.remoteCompactionEnabled && options.purpose === "compaction" && isGptModel(options.model)) return remoteCompactStream(ctx, options, next, {
1889
+ const legacySafeNext = () => repairLegacyPiAiContextOverflow(next(), options.model, options.contextWindow);
1890
+ if (resolved.remoteCompact && settings.remoteCompactionEnabled && options.purpose === "compaction" && isGptModel(options.model)) return remoteCompactStream(ctx, options, legacySafeNext, {
1857
1891
  hostedWebSearch: resolved.hostedWebSearch && settings.hostedWebSearchEnabled,
1858
1892
  customApplyPatch: settings.patchToolEnabled
1859
1893
  });
1860
- if (options.purpose === void 0 && isGptModel(options.model)) return hostedWebSearchStream(next, {
1894
+ if (options.purpose === void 0 && isGptModel(options.model)) return repairLegacyPiAiContextOverflow(hostedWebSearchStream(next, {
1861
1895
  serviceTier: options.serviceTier,
1862
1896
  hostedWebSearch: resolved.hostedWebSearch && settings.hostedWebSearchEnabled,
1863
1897
  customApplyPatch: settings.patchToolEnabled
1864
- });
1898
+ }), options.model, options.contextWindow);
1865
1899
  return next();
1866
1900
  }));
1867
1901
  }
@@ -4,7 +4,7 @@ import { installSettingsSection, settingsNamespace } from '@deepseek-ai/dsh-sett
4
4
  import { defineTool } from '@deepseek-ai/dsh-tools';
5
5
  import { APPLY_PATCH_DESCRIPTION, applyPatchHunks, parsePatch } from "./patch.js";
6
6
  import { renderExecResult, runExecCommand, runWriteStdin } from "./exec.js";
7
- import { hostedWebSearchStream, installHostedWebSearch, remoteCompactStream } from "./remote.js";
7
+ import { hostedWebSearchStream, installHostedWebSearch, remoteCompactStream, repairLegacyPiAiContextOverflow, } from "./remote.js";
8
8
  import { registerCodexActivityProjection } from "./activity.js";
9
9
  import { DEFAULT_CODEX_SYSTEM_PROMPT, resolveCodexSystemPrompt } from "./prompt.js";
10
10
  import { CODEX_SETTINGS_ENTRY, CODEX_SETTINGS_NAMESPACE, CODEX_SETTINGS_SCHEMA, } from "./settings.js";
@@ -559,21 +559,22 @@ export function apply(ctx, config = {}) {
559
559
  }
560
560
  ctx.on('llm/stream', ((options, next) => {
561
561
  const settings = codexSettings.current();
562
+ const legacySafeNext = () => repairLegacyPiAiContextOverflow(next(), options.model, options.contextWindow);
562
563
  if (resolved.remoteCompact
563
564
  && settings.remoteCompactionEnabled
564
565
  && options.purpose === 'compaction'
565
566
  && isGptModel(options.model)) {
566
- return remoteCompactStream(ctx, options, next, {
567
+ return remoteCompactStream(ctx, options, legacySafeNext, {
567
568
  hostedWebSearch: resolved.hostedWebSearch && settings.hostedWebSearchEnabled,
568
569
  customApplyPatch: settings.patchToolEnabled,
569
570
  });
570
571
  }
571
572
  if (options.purpose === undefined && isGptModel(options.model)) {
572
- return hostedWebSearchStream(next, {
573
+ return repairLegacyPiAiContextOverflow(hostedWebSearchStream(next, {
573
574
  serviceTier: options.serviceTier,
574
575
  hostedWebSearch: resolved.hostedWebSearch && settings.hostedWebSearchEnabled,
575
576
  customApplyPatch: settings.patchToolEnabled,
576
- });
577
+ }), options.model, options.contextWindow);
577
578
  }
578
579
  return next();
579
580
  }));
@@ -1,21 +1,21 @@
1
1
  /** Parser and line-oriented applicator for Codex's `apply_patch` language. */
2
2
  export const APPLY_PATCH_DESCRIPTION = 'Edits files using Codex patch syntax with Begin/End Patch markers and file update directives. In hunk lines, the first character is the operation marker; repeat a source-leading marker when the source line itself starts with one.';
3
3
  /** The grammar sent to providers that support OpenAI custom grammar tools. */
4
- export const APPLY_PATCH_GRAMMAR = String.raw `start: begin_patch hunk+ end_patch
5
- begin_patch: "*** Begin Patch" LF
6
- end_patch: "*** End Patch" LF?
7
- hunk: add_hunk | delete_hunk | update_hunk
8
- add_hunk: "*** Add File: " filename LF add_line+
9
- delete_hunk: "*** Delete File: " filename LF
10
- update_hunk: "*** Update File: " filename LF change_move? change?
11
- filename: /(.+)/
12
- add_line: "+" /(.*)/ LF -> line
13
- change_move: "*** Move to: " filename LF
14
- change: (change_context | change_line)+ eof_line?
15
- change_context: ("@@" | "@@ " /(.+)/) LF
16
- change_line: ("+" | "-" | " ") /(.*)/ LF
17
- eof_line: "*** End of File" LF
18
- %import common.LF
4
+ export const APPLY_PATCH_GRAMMAR = String.raw `start: begin_patch hunk+ end_patch
5
+ begin_patch: "*** Begin Patch" LF
6
+ end_patch: "*** End Patch" LF?
7
+ hunk: add_hunk | delete_hunk | update_hunk
8
+ add_hunk: "*** Add File: " filename LF add_line+
9
+ delete_hunk: "*** Delete File: " filename LF
10
+ update_hunk: "*** Update File: " filename LF change_move? change?
11
+ filename: /(.+)/
12
+ add_line: "+" /(.*)/ LF -> line
13
+ change_move: "*** Move to: " filename LF
14
+ change: (change_context | change_line)+ eof_line?
15
+ change_context: ("@@" | "@@ " /(.+)/) LF
16
+ change_line: ("+" | "-" | " ") /(.*)/ LF
17
+ eof_line: "*** End of File" LF
18
+ %import common.LF
19
19
  `;
20
20
  function invalid(message) {
21
21
  throw new Error(`apply_patch: ${message}`);
@@ -1,39 +1,39 @@
1
1
  /** Default plugin-owned Codex operating prompt. */
2
- export const DEFAULT_CODEX_SYSTEM_PROMPT = String.raw `## General
3
-
4
- - When searching for text or files, prefer using rg or rg --files respectively because rg is much faster than alternatives like grep. If rg is not available, use the next best alternative.
5
-
6
- ## Editing constraints
7
-
8
- - Default to ASCII when editing or creating files. Only introduce non-ASCII or other Unicode characters when there is a clear justification and the file already uses them.
9
- - Add succinct code comments that explain non-obvious code. Do not add comments that merely narrate assignments or control flow.
10
- - You may be in a dirty git worktree. Never revert existing changes you did not make unless the user explicitly requests it. If unrelated files are changed, leave them alone.
11
-
12
- ## Planning
13
-
14
- - Use update_plan for work with multiple meaningful steps. Keep the plan current as the task progresses.
15
- - Do not use a plan for a trivial one-step request.
16
-
17
- ## dsh session
18
-
19
- - The user and you share one workspace. Inspect the repository and every applicable AGENTS.md before editing.
20
-
21
- ## Task execution
22
-
23
- - Keep the user informed with concise progress updates and lead with the result.
24
-
25
- ## Presenting your work
26
-
27
- - Be concise, direct, friendly, and actionable.
28
- - For substantial work, explain what changed and why, then briefly note how the work was verified and what comes next.
29
- - Do not dump large files into the conversation; refer to their paths.
30
- - Use plain text with short sections only when they improve scanability.
31
-
32
- ## Working principles
33
-
34
- - Ask, align, and clarify first. Gather enough context from the user, then align the approach to achieve the user's goal through the clearest, most effective path.
35
- - Keep only the essential logic and core actions. Don't explain or test what was removed or why something wasn't done, especially when writing documentation or communicating. Convey enough valuable information with as few words as possible.
36
- - Solve problems by thinking from first principles and at a higher level.
2
+ export const DEFAULT_CODEX_SYSTEM_PROMPT = String.raw `## General
3
+
4
+ - When searching for text or files, prefer using rg or rg --files respectively because rg is much faster than alternatives like grep. If rg is not available, use the next best alternative.
5
+
6
+ ## Editing constraints
7
+
8
+ - Default to ASCII when editing or creating files. Only introduce non-ASCII or other Unicode characters when there is a clear justification and the file already uses them.
9
+ - Add succinct code comments that explain non-obvious code. Do not add comments that merely narrate assignments or control flow.
10
+ - You may be in a dirty git worktree. Never revert existing changes you did not make unless the user explicitly requests it. If unrelated files are changed, leave them alone.
11
+
12
+ ## Planning
13
+
14
+ - Use update_plan for work with multiple meaningful steps. Keep the plan current as the task progresses.
15
+ - Do not use a plan for a trivial one-step request.
16
+
17
+ ## dsh session
18
+
19
+ - The user and you share one workspace. Inspect the repository and every applicable AGENTS.md before editing.
20
+
21
+ ## Task execution
22
+
23
+ - Keep the user informed with concise progress updates and lead with the result.
24
+
25
+ ## Presenting your work
26
+
27
+ - Be concise, direct, friendly, and actionable.
28
+ - For substantial work, explain what changed and why, then briefly note how the work was verified and what comes next.
29
+ - Do not dump large files into the conversation; refer to their paths.
30
+ - Use plain text with short sections only when they improve scanability.
31
+
32
+ ## Working principles
33
+
34
+ - Ask, align, and clarify first. Gather enough context from the user, then align the approach to achieve the user's goal through the clearest, most effective path.
35
+ - Keep only the essential logic and core actions. Don't explain or test what was removed or why something wasn't done, especially when writing documentation or communicating. Convey enough valuable information with as few words as possible.
36
+ - Solve problems by thinking from first principles and at a higher level.
37
37
  `;
38
38
  /** Resolve the full plugin-owned prompt, preserving intentional empty overrides. */
39
39
  export function resolveCodexSystemPrompt(systemPrompt) {
@@ -29,6 +29,11 @@ export declare function replaceRemoteCompactions(body: JsonObject): JsonObject;
29
29
  export declare function installHostedWebSearch(): () => void;
30
30
  /** Iterate an existing DSH stream with the hosted-request context installed. */
31
31
  export declare function hostedWebSearchStream(next: () => AsyncIterable<StreamChunk>, options?: CodexRequestWireOptions): AsyncIterable<StreamChunk>;
32
+ /**
33
+ * Correct the usage-only overflow verdict emitted by pi-ai adapters that do
34
+ * not yet consume the request-scoped Codex capacity.
35
+ */
36
+ export declare function repairLegacyPiAiContextOverflow(stream: AsyncIterable<StreamChunk>, model: string, contextWindow: unknown): AsyncIterable<StreamChunk>;
32
37
  /** Remote-first compaction waterfall with the existing DSH path as fallback. */
33
38
  export declare function remoteCompactStream(ctx: Context, options: GenerateOptions, next: () => AsyncIterable<StreamChunk>, wireOptions?: CodexRequestWireOptions): AsyncIterable<StreamChunk>;
34
39
  export {};
@@ -8,6 +8,7 @@ import { APPLY_PATCH_DESCRIPTION, APPLY_PATCH_GRAMMAR } from "./patch.js";
8
8
  const SETTINGS_NAMESPACE = 'llm-pi-ai';
9
9
  const REMOTE_COMPACTION_OPEN = '<codex-remote-compaction>';
10
10
  const REMOTE_COMPACTION_CLOSE = '</codex-remote-compaction>';
11
+ const CONTEXT_WINDOW_EXCEEDED_CODE = 'CONTEXT_WINDOW_EXCEEDED';
11
12
  const CODEX_ERROR_DIRECTORY = ['diagnostics', 'openai-codex-errors'];
12
13
  const HOSTED_REQUESTS = new AsyncLocalStorage();
13
14
  let hostedPatchUsers = 0;
@@ -469,6 +470,47 @@ export function hostedWebSearchStream(next, options = {}) {
469
470
  }
470
471
  })();
471
472
  }
473
+ function contextTokens(usage) {
474
+ const fields = [
475
+ usage.inputTokens,
476
+ usage.outputTokens,
477
+ usage.cacheReadTokens ?? 0,
478
+ usage.cacheWriteTokens ?? 0,
479
+ ];
480
+ if (fields.some(value => !Number.isSafeInteger(value) || value < 0))
481
+ return undefined;
482
+ const total = fields.reduce((sum, value) => sum + value, 0);
483
+ return Number.isSafeInteger(total) ? total : undefined;
484
+ }
485
+ /**
486
+ * Correct the usage-only overflow verdict emitted by pi-ai adapters that do
487
+ * not yet consume the request-scoped Codex capacity.
488
+ */
489
+ export function repairLegacyPiAiContextOverflow(stream, model, contextWindow) {
490
+ return (async function* () {
491
+ let usage;
492
+ for await (const chunk of stream) {
493
+ if (chunk.type === 'usage')
494
+ usage = chunk.usage;
495
+ const usedTokens = usage === undefined ? undefined : contextTokens(usage);
496
+ if (chunk.type === 'finish'
497
+ && chunk.reason.kind === 'error'
498
+ && chunk.reason.failure.code === CONTEXT_WINDOW_EXCEEDED_CODE
499
+ && chunk.reason.failure.message === `pi-ai detected context overflow for model "${model}"`
500
+ && typeof contextWindow === 'number'
501
+ && Number.isSafeInteger(contextWindow)
502
+ && contextWindow > 0
503
+ && usage !== undefined
504
+ && usage.outputTokens > 0
505
+ && usedTokens !== undefined
506
+ && usedTokens <= contextWindow) {
507
+ yield { ...chunk, reason: { kind: 'stop' } };
508
+ continue;
509
+ }
510
+ yield chunk;
511
+ }
512
+ })();
513
+ }
472
514
  function textOf(message) {
473
515
  return message.content
474
516
  .filter(block => block.type === 'text')