cursor-opencode-provider 0.2.9 → 0.3.1
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 +139 -10
- package/dist/agent-url.js +2 -0
- package/dist/auth.d.ts +7 -0
- package/dist/auth.js +9 -0
- package/dist/compaction-marker.d.ts +14 -0
- package/dist/compaction-marker.js +36 -0
- package/dist/context/env.js +6 -2
- package/dist/context/paths.js +3 -2
- package/dist/index.d.ts +1 -3
- package/dist/index.js +2 -8
- package/dist/language-model.js +51 -35
- package/dist/model-config.d.ts +15 -0
- package/dist/model-config.js +206 -0
- package/dist/opencode2/catalog.d.ts +33 -0
- package/dist/opencode2/catalog.js +85 -0
- package/dist/opencode2/integration.d.ts +23 -0
- package/dist/opencode2/integration.js +124 -0
- package/dist/opencode2/types.d.ts +316 -0
- package/dist/opencode2/types.js +18 -0
- package/dist/plugin-core.d.ts +27 -0
- package/dist/plugin-core.js +31 -0
- package/dist/plugin-opencode2.d.ts +3 -0
- package/dist/plugin-opencode2.js +293 -0
- package/dist/plugin-v2.d.ts +7 -0
- package/dist/plugin-v2.js +1 -15
- package/dist/plugin.d.ts +1 -15
- package/dist/plugin.js +11 -191
- package/dist/protocol/apply-patch.d.ts +109 -0
- package/dist/protocol/apply-patch.js +163 -0
- package/dist/protocol/messages.d.ts +10 -0
- package/dist/protocol/messages.js +83 -1
- package/dist/protocol/tools.d.ts +24 -1
- package/dist/protocol/tools.js +262 -19
- package/dist/session-directory.d.ts +17 -0
- package/dist/session-directory.js +35 -0
- package/dist/session.d.ts +13 -1
- package/dist/session.js +70 -1
- package/dist/shell-timeout.d.ts +3 -1
- package/dist/shell-timeout.js +12 -4
- package/dist/usage.d.ts +30 -0
- package/dist/usage.js +77 -0
- package/dist/web-search-tool.d.ts +33 -0
- package/dist/web-search-tool.js +22 -0
- package/dist/web-tools.d.ts +9 -25
- package/dist/web-tools.js +34 -36
- package/package.json +14 -9
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Synthesis of OpenCode `apply_patch` envelopes from Cursor's native write/edit
|
|
3
|
+
* exec requests.
|
|
4
|
+
*
|
|
5
|
+
* ## Why this exists
|
|
6
|
+
*
|
|
7
|
+
* OpenCode 1.x does not merely *add* `apply_patch` for GPT models — it removes
|
|
8
|
+
* `edit` and `write` from the catalog in exchange. From
|
|
9
|
+
* `packages/opencode/src/tool/registry.ts` (`ToolRegistry.tools`):
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* // use apply tool in same format as codex
|
|
13
|
+
* const usePatch =
|
|
14
|
+
* input.modelID.includes("gpt-") && !input.modelID.includes("oss") && !input.modelID.includes("gpt-4")
|
|
15
|
+
* if (tool.id === ApplyPatchTool.id) return usePatch
|
|
16
|
+
* if (tool.id === EditTool.id || tool.id === WriteTool.id) return !usePatch
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* The predicate runs on the *API* model id, so every Cursor `gpt-5*` model
|
|
20
|
+
* matches (and, as a quirk of substring matching, `gpt-3.5-*` does too while
|
|
21
|
+
* `gpt-4o` does not).
|
|
22
|
+
*
|
|
23
|
+
* Upstream context, in case this ever needs revisiting:
|
|
24
|
+
*
|
|
25
|
+
* - Introduced by opencode PR #9127 ("apply_patch tool for openai models"). The
|
|
26
|
+
* intent is to hand GPT/Codex models the tool surface they were post-trained
|
|
27
|
+
* on; the same commit tells the codex system prompt to prefer `apply_patch`.
|
|
28
|
+
* - The *exclusivity* is deliberate. On opencode issue #34491 a maintainer
|
|
29
|
+
* describes the gate as a "band-aid", "intentionally minimal", whose purpose
|
|
30
|
+
* is that "GPT no longer receives edit + write + apply_patch simultaneously",
|
|
31
|
+
* and notes that a plugin-side opt-out "isn't feasible today".
|
|
32
|
+
* - opencode issue #19942 reported exactly this hazard — model-conditional tool
|
|
33
|
+
* substitution makes model-agnostic integrations impossible — and was closed
|
|
34
|
+
* `not_planned` by a stale bot.
|
|
35
|
+
* - opencode issue #35408 (open, milestone 2.0) proposes a
|
|
36
|
+
* `ctx.session.context(...)` hook exposing a mutable tool roster, and lists
|
|
37
|
+
* "Select `apply_patch` versus `edit` + `write`" as work to move out of the
|
|
38
|
+
* registry. Once that lands, a host-side opt-out replaces this shim.
|
|
39
|
+
* - OpenCode 2.0 (`packages/core`) has **no** such gate: `apply_patch`, `edit`
|
|
40
|
+
* and `write` are all registered unconditionally in `tool/builtins.ts`. This
|
|
41
|
+
* translation is therefore 1.x-only in practice, but it is keyed off the
|
|
42
|
+
* advertised set rather than a version check, so it simply never fires on 2.0.
|
|
43
|
+
*
|
|
44
|
+
* ## Why translation, not advertisement
|
|
45
|
+
*
|
|
46
|
+
* `toolsToDescriptors` forwards every advertised host tool to Cursor verbatim,
|
|
47
|
+
* so `opencode-apply_patch` already appears in Cursor's catalog whenever
|
|
48
|
+
* OpenCode advertises it, and an `mcp_args` call naming it already executes.
|
|
49
|
+
* What breaks is Cursor's *native* write/edit channel (`write_args`,
|
|
50
|
+
* `pi_edit_args`), which this provider resolves to the fixed host names
|
|
51
|
+
* `write`/`edit`.
|
|
52
|
+
*
|
|
53
|
+
* Translating on our side is the only option: Cursor's agent protocol has no
|
|
54
|
+
* patch-format edit tool at all (its `ToolCall` oneof offers whole-file
|
|
55
|
+
* `edit_tool_call`/`write_args` and substring `pi_edit_tool_call`/`pi_edit_args`;
|
|
56
|
+
* `apply_agent_diff_tool_call` is unrelated — it applies a cloud agent's diff by
|
|
57
|
+
* `agent_id`). Cursor's CLI also contains no model-conditional branching
|
|
58
|
+
* whatsoever, and `ModelDetails` carries no tool-capability flags, so the same
|
|
59
|
+
* native requests arrive for every model. Those names are absent from
|
|
60
|
+
* the catalog, so the request is refused before it reaches OpenCode.
|
|
61
|
+
*
|
|
62
|
+
* ## Format notes (verified against `packages/opencode/src/patch/index.ts`)
|
|
63
|
+
*
|
|
64
|
+
* - `*** Add File:` **overwrites an existing file** — the tool's `add` case uses
|
|
65
|
+
* `oldContent = ""` and never stats the target (upstream test: "adds file
|
|
66
|
+
* overwriting existing file"). A whole-file write therefore needs no diff and
|
|
67
|
+
* no existence check.
|
|
68
|
+
* - An empty `@@` header is a valid chunk start: the parser stores
|
|
69
|
+
* `change_context: contextLine || undefined`, and a missing context simply
|
|
70
|
+
* means "match `old_lines` from the current scan position".
|
|
71
|
+
* - Every payload line is prefixed with `+`, `-`, or a space, and the parser
|
|
72
|
+
* only breaks on *unprefixed* `***` / `@@`. Content lines that themselves
|
|
73
|
+
* begin with `***` or `@@` therefore survive a round trip unharmed.
|
|
74
|
+
* - `Add File` content is rejoined with `\n` and normalized to exactly one
|
|
75
|
+
* trailing newline. A write of content with no trailing newline gains one.
|
|
76
|
+
* - Update chunks match **whole lines** via `seekSequence`, unlike OpenCode
|
|
77
|
+
* `edit`, which is substring-based. Substring edits must be expanded to line
|
|
78
|
+
* boundaries before they can be expressed as a patch — see `planSubstringEdit`.
|
|
79
|
+
* - Renames are rejected by OpenCode 2.0's implementation ("apply_patch moves
|
|
80
|
+
* are not supported yet"), so `*** Move to:` is never emitted here.
|
|
81
|
+
*/
|
|
82
|
+
export declare const APPLY_PATCH_TOOL = "apply_patch";
|
|
83
|
+
/** One `@@` chunk: replace `oldLines` with `newLines`. */
|
|
84
|
+
export type UpdateChunk = {
|
|
85
|
+
oldLines: string[];
|
|
86
|
+
newLines: string[];
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* Whole-file write as `*** Add File:`. Valid whether or not the target exists —
|
|
90
|
+
* OpenCode's add case overwrites unconditionally.
|
|
91
|
+
*/
|
|
92
|
+
export declare function buildAddFilePatch(filePath: string, content: string): string;
|
|
93
|
+
/** Targeted replacement as `*** Update File:` with one `@@` chunk per change. */
|
|
94
|
+
export declare function buildUpdateFilePatch(filePath: string, chunks: UpdateChunk[]): string;
|
|
95
|
+
export type SubstringEditPlan = {
|
|
96
|
+
ok: true;
|
|
97
|
+
chunks: UpdateChunk[];
|
|
98
|
+
} | {
|
|
99
|
+
ok: false;
|
|
100
|
+
reason: string;
|
|
101
|
+
};
|
|
102
|
+
/**
|
|
103
|
+
* Expand a substring replacement to the whole lines it touches, because
|
|
104
|
+
* `apply_patch` matches line sequences rather than substrings.
|
|
105
|
+
*
|
|
106
|
+
* Refuses rather than guessing: an absent match, or an ambiguous one without
|
|
107
|
+
* `replaceAll`, would otherwise be silently applied to the wrong region.
|
|
108
|
+
*/
|
|
109
|
+
export declare function planSubstringEdit(source: string, oldString: string, newString: string, replaceAll?: boolean): SubstringEditPlan;
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Synthesis of OpenCode `apply_patch` envelopes from Cursor's native write/edit
|
|
3
|
+
* exec requests.
|
|
4
|
+
*
|
|
5
|
+
* ## Why this exists
|
|
6
|
+
*
|
|
7
|
+
* OpenCode 1.x does not merely *add* `apply_patch` for GPT models — it removes
|
|
8
|
+
* `edit` and `write` from the catalog in exchange. From
|
|
9
|
+
* `packages/opencode/src/tool/registry.ts` (`ToolRegistry.tools`):
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* // use apply tool in same format as codex
|
|
13
|
+
* const usePatch =
|
|
14
|
+
* input.modelID.includes("gpt-") && !input.modelID.includes("oss") && !input.modelID.includes("gpt-4")
|
|
15
|
+
* if (tool.id === ApplyPatchTool.id) return usePatch
|
|
16
|
+
* if (tool.id === EditTool.id || tool.id === WriteTool.id) return !usePatch
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* The predicate runs on the *API* model id, so every Cursor `gpt-5*` model
|
|
20
|
+
* matches (and, as a quirk of substring matching, `gpt-3.5-*` does too while
|
|
21
|
+
* `gpt-4o` does not).
|
|
22
|
+
*
|
|
23
|
+
* Upstream context, in case this ever needs revisiting:
|
|
24
|
+
*
|
|
25
|
+
* - Introduced by opencode PR #9127 ("apply_patch tool for openai models"). The
|
|
26
|
+
* intent is to hand GPT/Codex models the tool surface they were post-trained
|
|
27
|
+
* on; the same commit tells the codex system prompt to prefer `apply_patch`.
|
|
28
|
+
* - The *exclusivity* is deliberate. On opencode issue #34491 a maintainer
|
|
29
|
+
* describes the gate as a "band-aid", "intentionally minimal", whose purpose
|
|
30
|
+
* is that "GPT no longer receives edit + write + apply_patch simultaneously",
|
|
31
|
+
* and notes that a plugin-side opt-out "isn't feasible today".
|
|
32
|
+
* - opencode issue #19942 reported exactly this hazard — model-conditional tool
|
|
33
|
+
* substitution makes model-agnostic integrations impossible — and was closed
|
|
34
|
+
* `not_planned` by a stale bot.
|
|
35
|
+
* - opencode issue #35408 (open, milestone 2.0) proposes a
|
|
36
|
+
* `ctx.session.context(...)` hook exposing a mutable tool roster, and lists
|
|
37
|
+
* "Select `apply_patch` versus `edit` + `write`" as work to move out of the
|
|
38
|
+
* registry. Once that lands, a host-side opt-out replaces this shim.
|
|
39
|
+
* - OpenCode 2.0 (`packages/core`) has **no** such gate: `apply_patch`, `edit`
|
|
40
|
+
* and `write` are all registered unconditionally in `tool/builtins.ts`. This
|
|
41
|
+
* translation is therefore 1.x-only in practice, but it is keyed off the
|
|
42
|
+
* advertised set rather than a version check, so it simply never fires on 2.0.
|
|
43
|
+
*
|
|
44
|
+
* ## Why translation, not advertisement
|
|
45
|
+
*
|
|
46
|
+
* `toolsToDescriptors` forwards every advertised host tool to Cursor verbatim,
|
|
47
|
+
* so `opencode-apply_patch` already appears in Cursor's catalog whenever
|
|
48
|
+
* OpenCode advertises it, and an `mcp_args` call naming it already executes.
|
|
49
|
+
* What breaks is Cursor's *native* write/edit channel (`write_args`,
|
|
50
|
+
* `pi_edit_args`), which this provider resolves to the fixed host names
|
|
51
|
+
* `write`/`edit`.
|
|
52
|
+
*
|
|
53
|
+
* Translating on our side is the only option: Cursor's agent protocol has no
|
|
54
|
+
* patch-format edit tool at all (its `ToolCall` oneof offers whole-file
|
|
55
|
+
* `edit_tool_call`/`write_args` and substring `pi_edit_tool_call`/`pi_edit_args`;
|
|
56
|
+
* `apply_agent_diff_tool_call` is unrelated — it applies a cloud agent's diff by
|
|
57
|
+
* `agent_id`). Cursor's CLI also contains no model-conditional branching
|
|
58
|
+
* whatsoever, and `ModelDetails` carries no tool-capability flags, so the same
|
|
59
|
+
* native requests arrive for every model. Those names are absent from
|
|
60
|
+
* the catalog, so the request is refused before it reaches OpenCode.
|
|
61
|
+
*
|
|
62
|
+
* ## Format notes (verified against `packages/opencode/src/patch/index.ts`)
|
|
63
|
+
*
|
|
64
|
+
* - `*** Add File:` **overwrites an existing file** — the tool's `add` case uses
|
|
65
|
+
* `oldContent = ""` and never stats the target (upstream test: "adds file
|
|
66
|
+
* overwriting existing file"). A whole-file write therefore needs no diff and
|
|
67
|
+
* no existence check.
|
|
68
|
+
* - An empty `@@` header is a valid chunk start: the parser stores
|
|
69
|
+
* `change_context: contextLine || undefined`, and a missing context simply
|
|
70
|
+
* means "match `old_lines` from the current scan position".
|
|
71
|
+
* - Every payload line is prefixed with `+`, `-`, or a space, and the parser
|
|
72
|
+
* only breaks on *unprefixed* `***` / `@@`. Content lines that themselves
|
|
73
|
+
* begin with `***` or `@@` therefore survive a round trip unharmed.
|
|
74
|
+
* - `Add File` content is rejoined with `\n` and normalized to exactly one
|
|
75
|
+
* trailing newline. A write of content with no trailing newline gains one.
|
|
76
|
+
* - Update chunks match **whole lines** via `seekSequence`, unlike OpenCode
|
|
77
|
+
* `edit`, which is substring-based. Substring edits must be expanded to line
|
|
78
|
+
* boundaries before they can be expressed as a patch — see `planSubstringEdit`.
|
|
79
|
+
* - Renames are rejected by OpenCode 2.0's implementation ("apply_patch moves
|
|
80
|
+
* are not supported yet"), so `*** Move to:` is never emitted here.
|
|
81
|
+
*/
|
|
82
|
+
export const APPLY_PATCH_TOOL = "apply_patch";
|
|
83
|
+
const BEGIN = "*** Begin Patch";
|
|
84
|
+
const END = "*** End Patch";
|
|
85
|
+
/**
|
|
86
|
+
* Whole-file write as `*** Add File:`. Valid whether or not the target exists —
|
|
87
|
+
* OpenCode's add case overwrites unconditionally.
|
|
88
|
+
*/
|
|
89
|
+
export function buildAddFilePatch(filePath, content) {
|
|
90
|
+
const lines = splitLines(content);
|
|
91
|
+
return [BEGIN, `*** Add File: ${filePath}`, ...lines.map((line) => `+${line}`), END].join("\n");
|
|
92
|
+
}
|
|
93
|
+
/** Targeted replacement as `*** Update File:` with one `@@` chunk per change. */
|
|
94
|
+
export function buildUpdateFilePatch(filePath, chunks) {
|
|
95
|
+
const body = [];
|
|
96
|
+
for (const chunk of chunks) {
|
|
97
|
+
body.push("@@");
|
|
98
|
+
for (const line of chunk.oldLines)
|
|
99
|
+
body.push(`-${line}`);
|
|
100
|
+
for (const line of chunk.newLines)
|
|
101
|
+
body.push(`+${line}`);
|
|
102
|
+
}
|
|
103
|
+
return [BEGIN, `*** Update File: ${filePath}`, ...body, END].join("\n");
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Expand a substring replacement to the whole lines it touches, because
|
|
107
|
+
* `apply_patch` matches line sequences rather than substrings.
|
|
108
|
+
*
|
|
109
|
+
* Refuses rather than guessing: an absent match, or an ambiguous one without
|
|
110
|
+
* `replaceAll`, would otherwise be silently applied to the wrong region.
|
|
111
|
+
*/
|
|
112
|
+
export function planSubstringEdit(source, oldString, newString, replaceAll = false) {
|
|
113
|
+
if (oldString === "")
|
|
114
|
+
return { ok: false, reason: "the text to replace is empty" };
|
|
115
|
+
const offsets = [];
|
|
116
|
+
for (let at = source.indexOf(oldString); at !== -1; at = source.indexOf(oldString, at + oldString.length)) {
|
|
117
|
+
offsets.push(at);
|
|
118
|
+
}
|
|
119
|
+
if (offsets.length === 0)
|
|
120
|
+
return { ok: false, reason: "the text to replace was not found in the file" };
|
|
121
|
+
if (offsets.length > 1 && !replaceAll) {
|
|
122
|
+
return {
|
|
123
|
+
ok: false,
|
|
124
|
+
reason: `the text to replace appears ${offsets.length} times; it must be unique`,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
const targets = replaceAll ? offsets : offsets.slice(0, 1);
|
|
128
|
+
const chunks = [];
|
|
129
|
+
let previousEnd = -1;
|
|
130
|
+
for (const offset of targets) {
|
|
131
|
+
const start = lineStart(source, offset);
|
|
132
|
+
const end = lineEnd(source, offset + oldString.length);
|
|
133
|
+
// Overlapping expansions would emit the same original lines twice, and the
|
|
134
|
+
// second chunk could never match after the first replaced them.
|
|
135
|
+
if (start < previousEnd) {
|
|
136
|
+
return { ok: false, reason: "overlapping replacements cannot be expressed as a patch" };
|
|
137
|
+
}
|
|
138
|
+
previousEnd = end;
|
|
139
|
+
const before = source.slice(start, offset);
|
|
140
|
+
const after = source.slice(offset + oldString.length, end);
|
|
141
|
+
chunks.push({
|
|
142
|
+
oldLines: splitLines(source.slice(start, end)),
|
|
143
|
+
newLines: splitLines(before + newString + after),
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
return { ok: true, chunks };
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Split into patch payload lines. A trailing newline is dropped so it is not
|
|
150
|
+
* emitted as a spurious empty line — OpenCode re-adds exactly one on apply.
|
|
151
|
+
*/
|
|
152
|
+
function splitLines(text) {
|
|
153
|
+
const normalized = text.endsWith("\n") ? text.slice(0, -1) : text;
|
|
154
|
+
return normalized.split("\n");
|
|
155
|
+
}
|
|
156
|
+
function lineStart(source, offset) {
|
|
157
|
+
const at = source.lastIndexOf("\n", offset - 1);
|
|
158
|
+
return at === -1 ? 0 : at + 1;
|
|
159
|
+
}
|
|
160
|
+
function lineEnd(source, offset) {
|
|
161
|
+
const at = source.indexOf("\n", offset);
|
|
162
|
+
return at === -1 ? source.length : at;
|
|
163
|
+
}
|
|
@@ -3,6 +3,16 @@ export declare function createMessageTypes(): protobuf.Root;
|
|
|
3
3
|
export declare function getMessageTypes(): protobuf.Root;
|
|
4
4
|
export declare function encodeMessage(typeName: string, message: Record<string, unknown>): Uint8Array;
|
|
5
5
|
export declare function decodeMessage<T = Record<string, unknown>>(typeName: string, data: Uint8Array): T;
|
|
6
|
+
/**
|
|
7
|
+
* Diagnostic-only: walk AgentServerMessage.interaction_update(1).turn_ended(14)
|
|
8
|
+
* without going through our hand-written TurnEnded schema, so a `cache_write`/
|
|
9
|
+
* `reasoning_tokens` question can be checked against the raw wire instead of
|
|
10
|
+
* guessed. Confirmed field ids 1-5 (input/output/cache_read/cache_write/
|
|
11
|
+
* reasoning) decode correctly. Live captures routinely include `f4` explicitly
|
|
12
|
+
* as `0` while `f3` (cache_read) and `f5` (reasoning) are non-zero — Cursor's
|
|
13
|
+
* agent TurnEnded populates the field but does not report write counts here.
|
|
14
|
+
*/
|
|
15
|
+
export declare function debugWalkTurnEnded(payload: Uint8Array): string;
|
|
6
16
|
/**
|
|
7
17
|
* Decode a protobuf sub-message from a frame payload that wraps it in
|
|
8
18
|
* a top-level field key + length varint. Skips the outer wrapper and
|
|
@@ -434,15 +434,22 @@ export function createMessageTypes() {
|
|
|
434
434
|
{ id: 1, name: "success", type: "GrepSuccess" },
|
|
435
435
|
{ id: 2, name: "error", type: "GrepError" },
|
|
436
436
|
], [{ name: "result", fields: ["success", "error"] }]);
|
|
437
|
+
// agent.v1.WriteArgs. Cursor's own LocalWriteExecutor reads `file_bytes`
|
|
438
|
+
// first and only falls back to `file_text`, so a write whose content arrives
|
|
439
|
+
// as bytes decodes to an empty string unless field 5 is declared here.
|
|
437
440
|
addType(root, "WriteArgs", [
|
|
438
441
|
{ id: 1, name: "path", type: "string" },
|
|
439
442
|
{ id: 2, name: "file_text", type: "string" },
|
|
440
443
|
{ id: 3, name: "tool_call_id", type: "string" },
|
|
444
|
+
{ id: 4, name: "return_file_content_after_write", type: "bool" },
|
|
445
|
+
{ id: 5, name: "file_bytes", type: "bytes" },
|
|
446
|
+
{ id: 6, name: "encoding_hint", type: "string" },
|
|
441
447
|
]);
|
|
442
448
|
addType(root, "WriteSuccess", [
|
|
443
449
|
{ id: 1, name: "path", type: "string" },
|
|
444
450
|
{ id: 2, name: "lines_created", type: "int32" },
|
|
445
451
|
{ id: 3, name: "file_size", type: "int32" },
|
|
452
|
+
{ id: 4, name: "file_content_after_write", type: "string" },
|
|
446
453
|
]);
|
|
447
454
|
addType(root, "WriteError", [
|
|
448
455
|
{ id: 1, name: "path", type: "string" },
|
|
@@ -476,7 +483,26 @@ export function createMessageTypes() {
|
|
|
476
483
|
{ id: 2, name: "error", type: "PiWriteError" },
|
|
477
484
|
{ id: 3, name: "rejected", type: "PiWriteRejected" },
|
|
478
485
|
], [{ name: "result", fields: ["success", "error", "rejected"] }]);
|
|
479
|
-
|
|
486
|
+
// agent.v1.PiTruncation. Every Pi *ExecSuccess shares output(1)+truncation(2)
|
|
487
|
+
// and only diverges at field 3, so the shared PiOutputSuccess can carry it.
|
|
488
|
+
// Cursor's own CLI always reports truncation rather than silently shortening
|
|
489
|
+
// a payload; emitting this keeps a capped read distinguishable from a
|
|
490
|
+
// complete one.
|
|
491
|
+
addType(root, "PiTruncation", [
|
|
492
|
+
{ id: 1, name: "truncated", type: "bool" },
|
|
493
|
+
{ id: 2, name: "truncated_by", type: "string" },
|
|
494
|
+
{ id: 3, name: "total_lines", type: "uint32" },
|
|
495
|
+
{ id: 4, name: "output_lines", type: "uint32" },
|
|
496
|
+
{ id: 5, name: "output_bytes", type: "uint32" },
|
|
497
|
+
{ id: 6, name: "max_lines", type: "uint32" },
|
|
498
|
+
{ id: 7, name: "max_bytes", type: "uint32" },
|
|
499
|
+
{ id: 8, name: "first_line_exceeds_limit", type: "bool" },
|
|
500
|
+
{ id: 9, name: "last_line_partial", type: "bool" },
|
|
501
|
+
]);
|
|
502
|
+
addType(root, "PiOutputSuccess", [
|
|
503
|
+
{ id: 1, name: "output", type: "string" },
|
|
504
|
+
{ id: 2, name: "truncation", type: "PiTruncation" },
|
|
505
|
+
]);
|
|
480
506
|
addType(root, "PiExecError", [{ id: 1, name: "error", type: "string" }]);
|
|
481
507
|
addType(root, "PiExecRejected", [{ id: 1, name: "reason", type: "string" }]);
|
|
482
508
|
for (const resultType of [
|
|
@@ -1209,6 +1235,62 @@ export function decodeMessage(typeName, data) {
|
|
|
1209
1235
|
const decoded = type.decode(data);
|
|
1210
1236
|
return type.toObject(decoded, { defaults: true, json: true, longs: Number });
|
|
1211
1237
|
}
|
|
1238
|
+
/** Schema-free walk of one message's top-level wire fields (no nested descent). */
|
|
1239
|
+
function readRawFields(buf) {
|
|
1240
|
+
const reader = protobuf.Reader.create(buf);
|
|
1241
|
+
const fields = [];
|
|
1242
|
+
while (reader.pos < reader.len) {
|
|
1243
|
+
const tag = reader.uint32();
|
|
1244
|
+
const field = tag >>> 3;
|
|
1245
|
+
const wireType = tag & 7;
|
|
1246
|
+
switch (wireType) {
|
|
1247
|
+
case 0:
|
|
1248
|
+
fields.push({ field, wireType, varint: reader.uint64().toString() });
|
|
1249
|
+
break;
|
|
1250
|
+
case 1:
|
|
1251
|
+
fields.push({ field, wireType, fixed: reader.fixed64().toString() });
|
|
1252
|
+
break;
|
|
1253
|
+
case 2:
|
|
1254
|
+
fields.push({ field, wireType, bytes: reader.bytes() });
|
|
1255
|
+
break;
|
|
1256
|
+
case 5:
|
|
1257
|
+
fields.push({ field, wireType, fixed: reader.fixed32() });
|
|
1258
|
+
break;
|
|
1259
|
+
default:
|
|
1260
|
+
// Groups (3/4) aren't used on this wire; bail rather than desync the reader.
|
|
1261
|
+
return fields;
|
|
1262
|
+
}
|
|
1263
|
+
}
|
|
1264
|
+
return fields;
|
|
1265
|
+
}
|
|
1266
|
+
/**
|
|
1267
|
+
* Diagnostic-only: walk AgentServerMessage.interaction_update(1).turn_ended(14)
|
|
1268
|
+
* without going through our hand-written TurnEnded schema, so a `cache_write`/
|
|
1269
|
+
* `reasoning_tokens` question can be checked against the raw wire instead of
|
|
1270
|
+
* guessed. Confirmed field ids 1-5 (input/output/cache_read/cache_write/
|
|
1271
|
+
* reasoning) decode correctly. Live captures routinely include `f4` explicitly
|
|
1272
|
+
* as `0` while `f3` (cache_read) and `f5` (reasoning) are non-zero — Cursor's
|
|
1273
|
+
* agent TurnEnded populates the field but does not report write counts here.
|
|
1274
|
+
*/
|
|
1275
|
+
export function debugWalkTurnEnded(payload) {
|
|
1276
|
+
try {
|
|
1277
|
+
const top = readRawFields(payload);
|
|
1278
|
+
const iuField = top.find((f) => f.field === 1 && f.wireType === 2);
|
|
1279
|
+
if (!iuField?.bytes)
|
|
1280
|
+
return "(no interaction_update field)";
|
|
1281
|
+
const iu = readRawFields(iuField.bytes);
|
|
1282
|
+
const teField = iu.find((f) => f.field === 14 && f.wireType === 2);
|
|
1283
|
+
if (!teField?.bytes)
|
|
1284
|
+
return "(no turn_ended field in interaction_update)";
|
|
1285
|
+
const te = readRawFields(teField.bytes);
|
|
1286
|
+
return te
|
|
1287
|
+
.map((f) => `f${f.field}:wt${f.wireType}=${f.varint ?? f.fixed ?? (f.bytes ? `bytes[${f.bytes.length}]` : "?")}`)
|
|
1288
|
+
.join(" ");
|
|
1289
|
+
}
|
|
1290
|
+
catch (e) {
|
|
1291
|
+
return `(walk failed: ${e.message})`;
|
|
1292
|
+
}
|
|
1293
|
+
}
|
|
1212
1294
|
/**
|
|
1213
1295
|
* Decode a protobuf sub-message from a frame payload that wraps it in
|
|
1214
1296
|
* a top-level field key + length varint. Skips the outer wrapper and
|
package/dist/protocol/tools.d.ts
CHANGED
|
@@ -87,6 +87,22 @@ export declare function resolveCursorSubagentType(subagentType: string, catalog?
|
|
|
87
87
|
export declare function mapCursorSubagentTypeToOpenCode(subagentType: string): string;
|
|
88
88
|
/** Resolve a native Cursor subagent request to this turn's executor and catalog. */
|
|
89
89
|
export declare function remapNativeSubagentForCatalog(parsed: ParsedExecRequest, advertisedToolNames: Iterable<string>, catalog?: HostSubagentCatalog): void;
|
|
90
|
+
/**
|
|
91
|
+
* Express a native Cursor write/edit as `apply_patch` when the host swapped the
|
|
92
|
+
* edit-tool family out from under us.
|
|
93
|
+
*
|
|
94
|
+
* OpenCode 1.x removes `edit` and `write` from the catalog for GPT models and
|
|
95
|
+
* advertises `apply_patch` in their place — see the module comment in
|
|
96
|
+
* `./apply-patch.ts` for the upstream reasoning and citations. Cursor keeps
|
|
97
|
+
* using its native write/edit exec channel regardless, so without this the
|
|
98
|
+
* request is refused as an unavailable tool and the model loses the ability to
|
|
99
|
+
* change files at all.
|
|
100
|
+
*
|
|
101
|
+
* Keyed purely off the advertised set: this is inert whenever the host offers
|
|
102
|
+
* `write`/`edit` normally, and equally inert when it offers neither those nor
|
|
103
|
+
* `apply_patch` (the caller's unavailable-tool rejection still applies).
|
|
104
|
+
*/
|
|
105
|
+
export declare function remapEditToolsForCatalog(parsed: ParsedExecRequest, advertisedToolNames: Iterable<string>, workspaceRoot?: string): void;
|
|
90
106
|
export type ParsedExecRequest = {
|
|
91
107
|
id: number;
|
|
92
108
|
execId: string;
|
|
@@ -158,6 +174,13 @@ export type ToolResultInput = {
|
|
|
158
174
|
resultMetadata?: Record<string, unknown>;
|
|
159
175
|
/** Structured shell completion captured by the OpenCode plugin hook. */
|
|
160
176
|
shellOutcome?: CursorShellOutcome;
|
|
177
|
+
/**
|
|
178
|
+
* Workspace root for result shapes that must name a directory
|
|
179
|
+
* (grep_result workspace_results keys, ls_result directory_tree_root).
|
|
180
|
+
* Must not fall back to process.cwd() — the OpenCode 2.0 daemon's cwd is
|
|
181
|
+
* often $HOME and would re-advertise the wrong folder to the model.
|
|
182
|
+
*/
|
|
183
|
+
workspaceRoot?: string;
|
|
161
184
|
};
|
|
162
185
|
/**
|
|
163
186
|
* Build one or more ExecClientMessage frames for a tool result.
|
|
@@ -195,7 +218,7 @@ export declare function unwrapReadOutput(output: string): string;
|
|
|
195
218
|
* OpenCode returns free-form text; we wrap it in the minimal success shape the
|
|
196
219
|
* server accepts (verified against agent.v1 wire captures).
|
|
197
220
|
*/
|
|
198
|
-
export declare function buildTypedExecResult(resultField: string, output: string, error?: string, toolName?: string, resultMetadata?: Record<string, unknown>, shellOutcome?: CursorShellOutcome): Record<string, unknown>;
|
|
221
|
+
export declare function buildTypedExecResult(resultField: string, output: string, error?: string, toolName?: string, resultMetadata?: Record<string, unknown>, shellOutcome?: CursorShellOutcome, workspaceRoot?: string): Record<string, unknown>;
|
|
199
222
|
export declare function buildToolCallPart(execMsg: ParsedExecRequest, sessionId: string): {
|
|
200
223
|
toolCallId: string;
|
|
201
224
|
toolName: string;
|