cursor-opencode-provider 0.3.0 → 0.3.2
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 +13 -3
- package/dist/agent-url.js +2 -0
- package/dist/context/paths.js +3 -2
- package/dist/language-model.js +14 -2
- package/dist/model-config.js +25 -5
- package/dist/opencode2/catalog.js +2 -1
- package/dist/opencode2/types.d.ts +13 -1
- package/dist/pricing-data.d.ts +194 -0
- package/dist/pricing-data.js +194 -0
- package/dist/pricing.d.ts +62 -0
- package/dist/pricing.js +127 -0
- package/dist/protocol/apply-patch.d.ts +109 -0
- package/dist/protocol/apply-patch.js +163 -0
- package/dist/protocol/messages.js +27 -1
- package/dist/protocol/tools.d.ts +16 -0
- package/dist/protocol/tools.js +253 -15
- package/dist/shell-timeout.js +6 -2
- package/package.json +12 -9
package/dist/pricing.js
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cursor model token pricing for OpenCode cost reporting.
|
|
3
|
+
*
|
|
4
|
+
* Rates come from Cursor's public docs (Other Models pool = provider API price).
|
|
5
|
+
* Generated data lives in `pricing-data.ts`; regenerate with
|
|
6
|
+
* `bun run generate:pricing`. Unknown / unpublished models get no `cost`.
|
|
7
|
+
*/
|
|
8
|
+
import { CURSOR_MODEL_COSTS } from "./pricing-data.js";
|
|
9
|
+
/**
|
|
10
|
+
* Models we expose that Cursor does not currently publish numeric rates for
|
|
11
|
+
* on the Other Models table (Auto / first-party Cursor Models pool).
|
|
12
|
+
*/
|
|
13
|
+
export const CURSOR_UNPRICED_MODEL_IDS = ["default", "composer-2.5", "grok-4.5"];
|
|
14
|
+
const UNPRICED = new Set(CURSOR_UNPRICED_MODEL_IDS);
|
|
15
|
+
function isRecord(value) {
|
|
16
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
17
|
+
}
|
|
18
|
+
function isNonNegativeFiniteNumber(value) {
|
|
19
|
+
return typeof value === "number" && Number.isFinite(value) && value >= 0;
|
|
20
|
+
}
|
|
21
|
+
function collectCostValidationErrors(value, path, errors) {
|
|
22
|
+
if (!isRecord(value)) {
|
|
23
|
+
errors.push(`${path} must be an object`);
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
if (!isNonNegativeFiniteNumber(value.input)) {
|
|
27
|
+
errors.push(`${path}.input must be a non-negative finite number`);
|
|
28
|
+
}
|
|
29
|
+
if (!isNonNegativeFiniteNumber(value.output)) {
|
|
30
|
+
errors.push(`${path}.output must be a non-negative finite number`);
|
|
31
|
+
}
|
|
32
|
+
if (value.cache_read !== undefined && !isNonNegativeFiniteNumber(value.cache_read)) {
|
|
33
|
+
errors.push(`${path}.cache_read must be a non-negative finite number`);
|
|
34
|
+
}
|
|
35
|
+
if (value.cache_write !== undefined && !isNonNegativeFiniteNumber(value.cache_write)) {
|
|
36
|
+
errors.push(`${path}.cache_write must be a non-negative finite number`);
|
|
37
|
+
}
|
|
38
|
+
if (value.context_over_200k !== undefined) {
|
|
39
|
+
collectCostValidationErrors(value.context_over_200k, `${path}.context_over_200k`, errors);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
export function validateOpenCodeModelCost(value, path = "cost") {
|
|
43
|
+
const errors = [];
|
|
44
|
+
collectCostValidationErrors(value, path, errors);
|
|
45
|
+
return { valid: errors.length === 0, errors };
|
|
46
|
+
}
|
|
47
|
+
export function isOpenCodeModelCost(value) {
|
|
48
|
+
return validateOpenCodeModelCost(value).valid;
|
|
49
|
+
}
|
|
50
|
+
/** Strip synthetic OpenCode long-context suffix (`…-1m`, `…-1m-2`, …). */
|
|
51
|
+
export function wireModelIdForPricing(modelId) {
|
|
52
|
+
const match = /^(.*)-1m(?:-(\d+))?$/.exec(modelId);
|
|
53
|
+
if (!match)
|
|
54
|
+
return { baseId: modelId, longContextEntry: false };
|
|
55
|
+
return { baseId: match[1], longContextEntry: true };
|
|
56
|
+
}
|
|
57
|
+
function asMutableCost(value) {
|
|
58
|
+
const copy = { input: value.input, output: value.output };
|
|
59
|
+
if (value.cache_read !== undefined)
|
|
60
|
+
copy.cache_read = value.cache_read;
|
|
61
|
+
if (value.cache_write !== undefined)
|
|
62
|
+
copy.cache_write = value.cache_write;
|
|
63
|
+
if (value.context_over_200k) {
|
|
64
|
+
copy.context_over_200k = asMutableCost(value.context_over_200k);
|
|
65
|
+
}
|
|
66
|
+
return copy;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Look up classic OpenCode cost for a catalog / wire model id.
|
|
70
|
+
* Synthetic `-1m` entries retain both base and documented long-context rates.
|
|
71
|
+
*/
|
|
72
|
+
export function getCursorModelCost(modelId) {
|
|
73
|
+
const { baseId } = wireModelIdForPricing(modelId);
|
|
74
|
+
const found = CURSOR_MODEL_COSTS[baseId];
|
|
75
|
+
if (!found)
|
|
76
|
+
return undefined;
|
|
77
|
+
return asMutableCost(found);
|
|
78
|
+
}
|
|
79
|
+
export function applyCursorModelCost(modelId, entry) {
|
|
80
|
+
const modelCost = getCursorModelCost(modelId);
|
|
81
|
+
if (!modelCost)
|
|
82
|
+
return entry;
|
|
83
|
+
return { ...entry, cost: modelCost };
|
|
84
|
+
}
|
|
85
|
+
/** Convert classic cost → OpenCode 2.0 `Model.Info.cost` array. */
|
|
86
|
+
export function toOpenCode2Costs(cost) {
|
|
87
|
+
if (!cost)
|
|
88
|
+
return [];
|
|
89
|
+
const out = [
|
|
90
|
+
{
|
|
91
|
+
input: cost.input,
|
|
92
|
+
output: cost.output,
|
|
93
|
+
cache: {
|
|
94
|
+
read: cost.cache_read ?? 0,
|
|
95
|
+
write: cost.cache_write ?? 0,
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
];
|
|
99
|
+
if (cost.context_over_200k) {
|
|
100
|
+
out.push({
|
|
101
|
+
tier: { type: "context", size: 200_000 },
|
|
102
|
+
input: cost.context_over_200k.input,
|
|
103
|
+
output: cost.context_over_200k.output,
|
|
104
|
+
cache: {
|
|
105
|
+
read: cost.context_over_200k.cache_read ?? 0,
|
|
106
|
+
write: cost.context_over_200k.cache_write ?? 0,
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
return out;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Coverage helper for CI / maintainer checks.
|
|
114
|
+
* Intentionally unpriced first-party ids are counted as priced (covered).
|
|
115
|
+
*/
|
|
116
|
+
export function checkCursorPricingCoverage(modelIds) {
|
|
117
|
+
const priced = [];
|
|
118
|
+
const missing = [];
|
|
119
|
+
for (const modelId of modelIds) {
|
|
120
|
+
const { baseId } = wireModelIdForPricing(modelId);
|
|
121
|
+
if (UNPRICED.has(baseId) || getCursorModelCost(modelId))
|
|
122
|
+
priced.push(modelId);
|
|
123
|
+
else
|
|
124
|
+
missing.push(modelId);
|
|
125
|
+
}
|
|
126
|
+
return { priced, missing };
|
|
127
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -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 [
|
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;
|