@oh-my-pi/pi-ai 17.1.7 → 17.2.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 +64 -0
- package/dist/types/auth-broker/server.d.ts +5 -0
- package/dist/types/auth-broker/types.d.ts +4 -0
- package/dist/types/auth-storage.d.ts +26 -1
- package/dist/types/error/classes.d.ts +5 -1
- package/dist/types/error/rate-limit.d.ts +5 -4
- package/dist/types/providers/anthropic-client.d.ts +13 -1
- package/dist/types/providers/anthropic.d.ts +9 -1
- package/dist/types/providers/cursor/exec-modern.d.ts +98 -0
- package/dist/types/providers/cursor-pi-args.d.ts +99 -0
- package/dist/types/providers/cursor.d.ts +53 -3
- package/dist/types/providers/error-message.d.ts +2 -4
- package/dist/types/providers/openai-codex-responses.d.ts +10 -0
- package/dist/types/providers/openai-responses-wire.d.ts +8 -0
- package/dist/types/providers/openai-shared.d.ts +13 -1
- package/dist/types/registry/exa.d.ts +8 -0
- package/dist/types/registry/registry.d.ts +7 -1
- package/dist/types/registry/xai.d.ts +4 -1
- package/dist/types/stream.d.ts +2 -0
- package/dist/types/types.d.ts +101 -1
- package/dist/types/usage/umans.d.ts +2 -0
- package/dist/types/utils/block-symbols.d.ts +11 -0
- package/dist/types/utils/harmony-leak.d.ts +15 -7
- package/dist/types/utils/schema/normalize.d.ts +1 -0
- package/dist/types/utils.d.ts +8 -0
- package/package.json +4 -4
- package/src/auth-broker/client.ts +5 -1
- package/src/auth-broker/server.ts +103 -9
- package/src/auth-broker/snapshot-cache.ts +12 -3
- package/src/auth-broker/types.ts +6 -0
- package/src/auth-storage.ts +447 -28
- package/src/error/classes.ts +98 -3
- package/src/error/flags.ts +6 -1
- package/src/error/rate-limit.ts +18 -12
- package/src/providers/amazon-bedrock.ts +3 -3
- package/src/providers/anthropic-client.ts +24 -2
- package/src/providers/anthropic.ts +31 -6
- package/src/providers/cursor/exec-modern.ts +495 -0
- package/src/providers/cursor/proto/agent.proto +1007 -0
- package/src/providers/cursor-pi-args.ts +135 -0
- package/src/providers/cursor.ts +1138 -66
- package/src/providers/devin.ts +2 -1
- package/src/providers/error-message.ts +3 -1
- package/src/providers/google-vertex.ts +3 -5
- package/src/providers/openai-codex-responses.ts +109 -27
- package/src/providers/openai-completions.ts +19 -4
- package/src/providers/openai-responses-wire.ts +8 -0
- package/src/providers/openai-responses.ts +12 -1
- package/src/providers/openai-shared.ts +83 -14
- package/src/registry/api-key-validation.ts +18 -34
- package/src/registry/exa.ts +19 -0
- package/src/registry/novita.ts +6 -3
- package/src/registry/registry.ts +3 -1
- package/src/registry/xai.ts +17 -1
- package/src/stream.ts +3 -4
- package/src/types.ts +115 -0
- package/src/usage/umans.ts +192 -0
- package/src/utils/block-symbols.ts +12 -0
- package/src/utils/harmony-leak.ts +32 -0
- package/src/utils/idle-iterator.ts +2 -2
- package/src/utils/schema/normalize.ts +140 -38
- package/src/utils.ts +173 -3
|
@@ -0,0 +1,495 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Proto builders for the modern Cursor CLI exec frames (`ExecServerMessage`
|
|
3
|
+
* 27-31, 36-38, 40-55).
|
|
4
|
+
*
|
|
5
|
+
* Split out of `cursor.ts` because these are pure `create(...)` shapes with no
|
|
6
|
+
* transport, stream, or block-state coupling: the dispatcher decides *which*
|
|
7
|
+
* answer a frame gets, this module knows *what* that answer looks like on the
|
|
8
|
+
* wire. Every builder returns a result whose oneof case is set — an
|
|
9
|
+
* `ExecClientMessage` carrying a result with an unset oneof is a fake success
|
|
10
|
+
* the server reads as "the tool ran and produced nothing".
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { create } from "@bufbuild/protobuf";
|
|
14
|
+
import {
|
|
15
|
+
AfterAgentResponseRequestResponseSchema,
|
|
16
|
+
AfterAgentThoughtRequestResponseSchema,
|
|
17
|
+
BeforeSubmitPromptRequestResponseSchema,
|
|
18
|
+
type ExecuteHookRequest,
|
|
19
|
+
type ExecuteHookResponse,
|
|
20
|
+
ExecuteHookResponseSchema,
|
|
21
|
+
type ExecuteHookResult,
|
|
22
|
+
ExecuteHookResultSchema,
|
|
23
|
+
type McpStateExecResult,
|
|
24
|
+
McpStateExecResultSchema,
|
|
25
|
+
McpStateServerSchema,
|
|
26
|
+
McpStateSuccessSchema,
|
|
27
|
+
type McpToolDefinition,
|
|
28
|
+
PiBashExecErrorSchema,
|
|
29
|
+
type PiBashExecResult,
|
|
30
|
+
PiBashExecResultSchema,
|
|
31
|
+
PiBashExecSuccessSchema,
|
|
32
|
+
PiEditExecErrorSchema,
|
|
33
|
+
PiEditExecRejectedSchema,
|
|
34
|
+
type PiEditExecResult,
|
|
35
|
+
PiEditExecResultSchema,
|
|
36
|
+
PiEditExecSuccessSchema,
|
|
37
|
+
PiFindExecErrorSchema,
|
|
38
|
+
type PiFindExecResult,
|
|
39
|
+
PiFindExecResultSchema,
|
|
40
|
+
PiFindExecSuccessSchema,
|
|
41
|
+
PiGrepExecErrorSchema,
|
|
42
|
+
type PiGrepExecResult,
|
|
43
|
+
PiGrepExecResultSchema,
|
|
44
|
+
PiGrepExecSuccessSchema,
|
|
45
|
+
PiLsExecErrorSchema,
|
|
46
|
+
type PiLsExecResult,
|
|
47
|
+
PiLsExecResultSchema,
|
|
48
|
+
PiLsExecSuccessSchema,
|
|
49
|
+
PiReadExecErrorSchema,
|
|
50
|
+
type PiReadExecResult,
|
|
51
|
+
PiReadExecResultSchema,
|
|
52
|
+
PiReadExecSuccessSchema,
|
|
53
|
+
type PiTruncation,
|
|
54
|
+
PiTruncationSchema,
|
|
55
|
+
PiWriteExecErrorSchema,
|
|
56
|
+
PiWriteExecRejectedSchema,
|
|
57
|
+
type PiWriteExecResult,
|
|
58
|
+
PiWriteExecResultSchema,
|
|
59
|
+
PiWriteExecSuccessSchema,
|
|
60
|
+
PostToolUseFailureRequestResponseSchema,
|
|
61
|
+
PostToolUseRequestResponseSchema,
|
|
62
|
+
PreCompactRequestResponseSchema,
|
|
63
|
+
PreToolUseRequestResponseSchema,
|
|
64
|
+
StopRequestResponseSchema,
|
|
65
|
+
SubagentStartRequestResponseSchema,
|
|
66
|
+
SubagentStopRequestResponseSchema,
|
|
67
|
+
} from "@oh-my-pi/pi-catalog/discovery/cursor-gen/agent_pb";
|
|
68
|
+
import type { ToolResultMessage } from "../../types";
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The pure arg translation lives in `../cursor-pi-args` so the legacy pi shim
|
|
72
|
+
* can share it without pulling this module's protobuf graph into the bundled
|
|
73
|
+
* virtual registry. Re-exported here because this is where the frame builders
|
|
74
|
+
* and their translation are consumed together.
|
|
75
|
+
*/
|
|
76
|
+
export {
|
|
77
|
+
piEscapeRegexLiteral,
|
|
78
|
+
piGrepSkip,
|
|
79
|
+
piJoinPath,
|
|
80
|
+
piLimit,
|
|
81
|
+
piLsPath,
|
|
82
|
+
piReadDisplayPath,
|
|
83
|
+
piReadPath,
|
|
84
|
+
piTimeout,
|
|
85
|
+
} from "../cursor-pi-args";
|
|
86
|
+
|
|
87
|
+
/** Flatten a tool result's content into the single `output` string the Pi frames carry. */
|
|
88
|
+
export function piOutputText(toolResult: ToolResultMessage): string {
|
|
89
|
+
return toolResult.content.map(item => (item.type === "text" ? item.text : `[${item.mimeType} image]`)).join("\n");
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Read one field off a tool result's `details` bag, or off a nested object
|
|
94
|
+
* inside it.
|
|
95
|
+
*
|
|
96
|
+
* `details` is `unknown` by design — every tool ships its own shape — so each
|
|
97
|
+
* read is narrowed rather than asserted, and the caller decides what a value of
|
|
98
|
+
* the wrong type means.
|
|
99
|
+
*/
|
|
100
|
+
function bagValue(bag: unknown, key: string): unknown {
|
|
101
|
+
if (!bag || typeof bag !== "object" || !(key in bag)) return undefined;
|
|
102
|
+
return Reflect.get(bag, key);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* A positive integer count from `details`, or `undefined`.
|
|
107
|
+
*
|
|
108
|
+
* The Pi frames model their limit counters as `optional uint32`: zero means
|
|
109
|
+
* "the limit was reached at zero results", so a missing, non-numeric, or
|
|
110
|
+
* non-positive value must stay unset rather than be sent as 0.
|
|
111
|
+
*/
|
|
112
|
+
function detailCount(toolResult: ToolResultMessage, key: string): number | undefined {
|
|
113
|
+
const value = bagValue(toolResult.details, key);
|
|
114
|
+
return positiveCount(value);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function positiveCount(value: unknown): number | undefined {
|
|
118
|
+
return typeof value === "number" && Number.isFinite(value) && value > 0 ? Math.floor(value) : undefined;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* The entry cap a listing hit, from either shape a local tool records it in.
|
|
123
|
+
*
|
|
124
|
+
* `glob` sets a flat `details.resultLimitReached` alongside the structured
|
|
125
|
+
* meta; `read` — which serves `pi_ls` — records the cap only through
|
|
126
|
+
* `OutputMeta` at `details.meta.limits.resultLimit.reached`. Reading just the
|
|
127
|
+
* flat field dropped `entry_limit_reached` for every real listing, so Cursor
|
|
128
|
+
* received clipped output with no incompleteness signal.
|
|
129
|
+
*/
|
|
130
|
+
function resultLimitReached(toolResult: ToolResultMessage): number | undefined {
|
|
131
|
+
const flat = detailCount(toolResult, "resultLimitReached");
|
|
132
|
+
if (flat !== undefined) return flat;
|
|
133
|
+
const limits = bagValue(bagValue(toolResult.details, "meta"), "limits");
|
|
134
|
+
return positiveCount(bagValue(bagValue(limits, "resultLimit"), "reached"));
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Translate a local tool's truncation summary into `PiTruncation`.
|
|
139
|
+
*
|
|
140
|
+
* Two shapes reach here. `read`/`grep` set `details.truncation`
|
|
141
|
+
* (`TruncationResult`), which carries an explicit `truncated` boolean. `bash`
|
|
142
|
+
* sets `details.meta.truncation` (`TruncationMeta`), which has **no** such
|
|
143
|
+
* flag — its presence *is* the signal, and requiring the boolean silently
|
|
144
|
+
* dropped every Bash truncation, handing Cursor clipped output with no notice
|
|
145
|
+
* that it was clipped.
|
|
146
|
+
*
|
|
147
|
+
* Returns `undefined` when nothing was truncated: the field is `optional` on
|
|
148
|
+
* every Pi success message, and emitting a zeroed `PiTruncation` would tell the
|
|
149
|
+
* server the output was trimmed to nothing.
|
|
150
|
+
*/
|
|
151
|
+
export function piTruncation(toolResult: ToolResultMessage): PiTruncation | undefined {
|
|
152
|
+
const direct = bagValue(toolResult.details, "truncation");
|
|
153
|
+
// `TruncationResult` is authoritative when present and explicitly false.
|
|
154
|
+
const truncation = direct !== undefined ? direct : bagValue(bagValue(toolResult.details, "meta"), "truncation");
|
|
155
|
+
if (truncation === undefined || truncation === null) return undefined;
|
|
156
|
+
// `TruncationResult` gates on its flag; `TruncationMeta` has none and is
|
|
157
|
+
// only ever attached when output was actually trimmed.
|
|
158
|
+
const flag = bagValue(truncation, "truncated");
|
|
159
|
+
if (flag !== undefined && flag !== true) return undefined;
|
|
160
|
+
const truncatedBy = bagValue(truncation, "truncatedBy");
|
|
161
|
+
const totalLines = bagValue(truncation, "totalLines");
|
|
162
|
+
const outputLines = bagValue(truncation, "outputLines");
|
|
163
|
+
const outputBytes = bagValue(truncation, "outputBytes");
|
|
164
|
+
return create(PiTruncationSchema, {
|
|
165
|
+
truncated: true,
|
|
166
|
+
truncatedBy: typeof truncatedBy === "string" ? truncatedBy : "",
|
|
167
|
+
totalLines: typeof totalLines === "number" ? totalLines : 0,
|
|
168
|
+
outputLines: typeof outputLines === "number" ? outputLines : 0,
|
|
169
|
+
outputBytes: typeof outputBytes === "number" ? outputBytes : 0,
|
|
170
|
+
firstLineExceedsLimit: bagValue(truncation, "firstLineExceedsLimit") === true,
|
|
171
|
+
lastLinePartial: bagValue(truncation, "lastLinePartial") === true,
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export function buildPiReadResult(toolResult: ToolResultMessage): PiReadExecResult {
|
|
176
|
+
const text = piOutputText(toolResult);
|
|
177
|
+
if (toolResult.isError) return buildPiReadError(text || "Read failed");
|
|
178
|
+
return create(PiReadExecResultSchema, {
|
|
179
|
+
result: {
|
|
180
|
+
case: "success",
|
|
181
|
+
value: create(PiReadExecSuccessSchema, { output: text, truncation: piTruncation(toolResult) }),
|
|
182
|
+
},
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export function buildPiReadError(error: string): PiReadExecResult {
|
|
187
|
+
return create(PiReadExecResultSchema, {
|
|
188
|
+
result: { case: "error", value: create(PiReadExecErrorSchema, { error }) },
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export function buildPiBashResult(toolResult: ToolResultMessage): PiBashExecResult {
|
|
193
|
+
const text = piOutputText(toolResult);
|
|
194
|
+
const truncation = piTruncation(toolResult);
|
|
195
|
+
if (toolResult.isError) {
|
|
196
|
+
return create(PiBashExecResultSchema, {
|
|
197
|
+
result: {
|
|
198
|
+
case: "error",
|
|
199
|
+
value: create(PiBashExecErrorSchema, { error: text || "Command failed", truncation }),
|
|
200
|
+
},
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
return create(PiBashExecResultSchema, {
|
|
204
|
+
result: {
|
|
205
|
+
case: "success",
|
|
206
|
+
value: create(PiBashExecSuccessSchema, { output: text, truncation }),
|
|
207
|
+
},
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export function buildPiBashError(error: string): PiBashExecResult {
|
|
212
|
+
return create(PiBashExecResultSchema, {
|
|
213
|
+
result: { case: "error", value: create(PiBashExecErrorSchema, { error }) },
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* `PiEditExecSuccess` requires `diff` and `patch` alongside `output`. The local
|
|
219
|
+
* `edit` tool reports them under `details`; when it does not, the strings stay
|
|
220
|
+
* empty rather than being faked from the output text.
|
|
221
|
+
*/
|
|
222
|
+
export function buildPiEditResult(toolResult: ToolResultMessage): PiEditExecResult {
|
|
223
|
+
const text = piOutputText(toolResult);
|
|
224
|
+
if (toolResult.isError) return buildPiEditError(text || "Edit failed");
|
|
225
|
+
const diff = bagValue(toolResult.details, "diff");
|
|
226
|
+
const patch = bagValue(toolResult.details, "patch");
|
|
227
|
+
return create(PiEditExecResultSchema, {
|
|
228
|
+
result: {
|
|
229
|
+
case: "success",
|
|
230
|
+
value: create(PiEditExecSuccessSchema, {
|
|
231
|
+
output: text,
|
|
232
|
+
diff: typeof diff === "string" ? diff : "",
|
|
233
|
+
patch: typeof patch === "string" ? patch : "",
|
|
234
|
+
firstChangedLine: detailCount(toolResult, "firstChangedLine"),
|
|
235
|
+
}),
|
|
236
|
+
},
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
export function buildPiEditError(error: string): PiEditExecResult {
|
|
241
|
+
return create(PiEditExecResultSchema, {
|
|
242
|
+
result: { case: "error", value: create(PiEditExecErrorSchema, { error }) },
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* A refusal is not an execution failure: `PiEditExecResult` models them as
|
|
248
|
+
* separate variants, and answering a denied call with `error` reads as "the
|
|
249
|
+
* edit ran and broke", which invites a retry of an operation that was never
|
|
250
|
+
* permitted.
|
|
251
|
+
*/
|
|
252
|
+
export function buildPiEditRejected(reason: string): PiEditExecResult {
|
|
253
|
+
return create(PiEditExecResultSchema, {
|
|
254
|
+
result: { case: "rejected", value: create(PiEditExecRejectedSchema, { reason }) },
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
export function buildPiWriteResult(toolResult: ToolResultMessage): PiWriteExecResult {
|
|
259
|
+
const text = piOutputText(toolResult);
|
|
260
|
+
if (toolResult.isError) return buildPiWriteError(text || "Write failed");
|
|
261
|
+
return create(PiWriteExecResultSchema, {
|
|
262
|
+
result: { case: "success", value: create(PiWriteExecSuccessSchema, { output: text }) },
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
export function buildPiWriteError(error: string): PiWriteExecResult {
|
|
267
|
+
return create(PiWriteExecResultSchema, {
|
|
268
|
+
result: { case: "error", value: create(PiWriteExecErrorSchema, { error }) },
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** Same variant split as {@link buildPiEditRejected}. */
|
|
273
|
+
export function buildPiWriteRejected(reason: string): PiWriteExecResult {
|
|
274
|
+
return create(PiWriteExecResultSchema, {
|
|
275
|
+
result: { case: "rejected", value: create(PiWriteExecRejectedSchema, { reason }) },
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
export function buildPiGrepResult(toolResult: ToolResultMessage): PiGrepExecResult {
|
|
280
|
+
const text = piOutputText(toolResult);
|
|
281
|
+
if (toolResult.isError) return buildPiGrepError(text || "Grep failed");
|
|
282
|
+
const matchLimitReached = detailCount(toolResult, "perFileLimitReached");
|
|
283
|
+
return create(PiGrepExecResultSchema, {
|
|
284
|
+
result: {
|
|
285
|
+
case: "success",
|
|
286
|
+
value: create(PiGrepExecSuccessSchema, {
|
|
287
|
+
output: text,
|
|
288
|
+
truncation: piTruncation(toolResult) ?? grepInternalCapTruncation(toolResult, text, matchLimitReached),
|
|
289
|
+
matchLimitReached,
|
|
290
|
+
linesTruncated: bagValue(toolResult.details, "linesTruncated") === true,
|
|
291
|
+
}),
|
|
292
|
+
},
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* The one grep cap that reaches Cursor through no other field.
|
|
298
|
+
*
|
|
299
|
+
* `GrepTool` folds every cap into the flat `details.truncated`, but only
|
|
300
|
+
* populates `details.truncation` when the output text itself was trimmed and
|
|
301
|
+
* `perFileLimitReached` when a per-file or explicit total cap fired. Its native
|
|
302
|
+
* backend's own ceiling (`INTERNAL_TOTAL_CAP`) sets neither, so a search that
|
|
303
|
+
* hit it answered as an unqualified success over incomplete results — the one
|
|
304
|
+
* failure mode a caller cannot detect and cannot retry around.
|
|
305
|
+
*
|
|
306
|
+
* Only consulted once the specific signals came back empty, so a cap that did
|
|
307
|
+
* report itself is never restated. `totalLines` stays 0: the flat flag says
|
|
308
|
+
* that results were dropped, not how many there were.
|
|
309
|
+
*/
|
|
310
|
+
function grepInternalCapTruncation(
|
|
311
|
+
toolResult: ToolResultMessage,
|
|
312
|
+
text: string,
|
|
313
|
+
matchLimitReached: number | undefined,
|
|
314
|
+
): PiTruncation | undefined {
|
|
315
|
+
if (matchLimitReached !== undefined) return undefined;
|
|
316
|
+
if (bagValue(toolResult.details, "truncated") !== true) return undefined;
|
|
317
|
+
return create(PiTruncationSchema, {
|
|
318
|
+
truncated: true,
|
|
319
|
+
truncatedBy: "matches",
|
|
320
|
+
totalLines: 0,
|
|
321
|
+
outputLines: text ? text.split("\n").length : 0,
|
|
322
|
+
outputBytes: Buffer.byteLength(text, "utf-8"),
|
|
323
|
+
firstLineExceedsLimit: false,
|
|
324
|
+
lastLinePartial: false,
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
export function buildPiGrepError(error: string): PiGrepExecResult {
|
|
329
|
+
return create(PiGrepExecResultSchema, {
|
|
330
|
+
result: { case: "error", value: create(PiGrepExecErrorSchema, { error }) },
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
export function buildPiFindResult(toolResult: ToolResultMessage): PiFindExecResult {
|
|
335
|
+
const text = piOutputText(toolResult);
|
|
336
|
+
if (toolResult.isError) return buildPiFindError(text || "Find failed");
|
|
337
|
+
return create(PiFindExecResultSchema, {
|
|
338
|
+
result: {
|
|
339
|
+
case: "success",
|
|
340
|
+
value: create(PiFindExecSuccessSchema, {
|
|
341
|
+
output: text,
|
|
342
|
+
truncation: piTruncation(toolResult),
|
|
343
|
+
resultLimitReached: resultLimitReached(toolResult),
|
|
344
|
+
}),
|
|
345
|
+
},
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
export function buildPiFindError(error: string): PiFindExecResult {
|
|
350
|
+
return create(PiFindExecResultSchema, {
|
|
351
|
+
result: { case: "error", value: create(PiFindExecErrorSchema, { error }) },
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
export function buildPiLsResult(toolResult: ToolResultMessage): PiLsExecResult {
|
|
356
|
+
const text = piOutputText(toolResult);
|
|
357
|
+
if (toolResult.isError) return buildPiLsError(text || "Ls failed");
|
|
358
|
+
return create(PiLsExecResultSchema, {
|
|
359
|
+
result: {
|
|
360
|
+
case: "success",
|
|
361
|
+
value: create(PiLsExecSuccessSchema, {
|
|
362
|
+
output: text,
|
|
363
|
+
truncation: piTruncation(toolResult),
|
|
364
|
+
entryLimitReached: resultLimitReached(toolResult),
|
|
365
|
+
}),
|
|
366
|
+
},
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
export function buildPiLsError(error: string): PiLsExecResult {
|
|
371
|
+
return create(PiLsExecResultSchema, {
|
|
372
|
+
result: { case: "error", value: create(PiLsExecErrorSchema, { error }) },
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* Answer `mcpStateExecArgs` (frame 36) from the catalog already advertised in
|
|
378
|
+
* `RequestContext.tools`.
|
|
379
|
+
*
|
|
380
|
+
* This client hosts no MCP servers of its own: every forwarded tool is a local
|
|
381
|
+
* pi-agent tool published under a synthetic `providerIdentifier`. Regrouping
|
|
382
|
+
* the same list keeps the server's view of "which servers exist and what do
|
|
383
|
+
* they expose" consistent with what it was told at context time, instead of
|
|
384
|
+
* claiming zero servers while tool calls for them keep arriving.
|
|
385
|
+
*
|
|
386
|
+
* `serverIdentifiers` filters the answer when the server asks about specific
|
|
387
|
+
* servers. `kickOnly` is a restart request — there is nothing to restart, so it
|
|
388
|
+
* is answered with the same state rather than an error.
|
|
389
|
+
*/
|
|
390
|
+
export function buildMcpStateResult(
|
|
391
|
+
tools: McpToolDefinition[],
|
|
392
|
+
serverIdentifiers: readonly string[],
|
|
393
|
+
): McpStateExecResult {
|
|
394
|
+
const byProvider = new Map<string, McpToolDefinition[]>();
|
|
395
|
+
for (const tool of tools) {
|
|
396
|
+
const identifier = tool.providerIdentifier;
|
|
397
|
+
const existing = byProvider.get(identifier);
|
|
398
|
+
if (existing) existing.push(tool);
|
|
399
|
+
else byProvider.set(identifier, [tool]);
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
const wanted = serverIdentifiers.length > 0 ? new Set(serverIdentifiers) : undefined;
|
|
403
|
+
const servers = [];
|
|
404
|
+
for (const [identifier, serverTools] of byProvider) {
|
|
405
|
+
if (wanted && !wanted.has(identifier)) continue;
|
|
406
|
+
servers.push(
|
|
407
|
+
create(McpStateServerSchema, {
|
|
408
|
+
serverName: identifier,
|
|
409
|
+
serverIdentifier: identifier,
|
|
410
|
+
tools: serverTools,
|
|
411
|
+
status: "connected",
|
|
412
|
+
}),
|
|
413
|
+
);
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
return create(McpStateExecResultSchema, {
|
|
417
|
+
result: { case: "success", value: create(McpStateSuccessSchema, { servers }) },
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* Build the neutral response for a hook query: the matching response case with
|
|
423
|
+
* every field unset.
|
|
424
|
+
*
|
|
425
|
+
* This client runs no Cursor hooks, and every field of every response variant
|
|
426
|
+
* is `optional` — so an empty response of the right case means "no hook had
|
|
427
|
+
* anything to say", which is exactly true. It is NOT the unset-oneof fake
|
|
428
|
+
* success: the case itself is set, only the payload is empty.
|
|
429
|
+
*
|
|
430
|
+
* `ExecuteHookRequest` and `ExecuteHookResponse` are parallel oneofs whose case
|
|
431
|
+
* names line up, but the two unions are unrelated to the compiler: a `switch`
|
|
432
|
+
* is what makes each pairing individually type-checked, and it forces a
|
|
433
|
+
* deliberate branch when a future regen adds a request case.
|
|
434
|
+
*
|
|
435
|
+
* Returns `null` for a request case this build does not model, which the
|
|
436
|
+
* dispatcher answers with `ExecClientThrow` rather than guessing a case.
|
|
437
|
+
*/
|
|
438
|
+
export function buildNeutralHookResult(request: ExecuteHookRequest | undefined): ExecuteHookResult | null {
|
|
439
|
+
let response: ExecuteHookResponse;
|
|
440
|
+
switch (request?.request.case) {
|
|
441
|
+
case "preCompact":
|
|
442
|
+
response = create(ExecuteHookResponseSchema, {
|
|
443
|
+
response: { case: "preCompact", value: create(PreCompactRequestResponseSchema, {}) },
|
|
444
|
+
});
|
|
445
|
+
break;
|
|
446
|
+
case "subagentStart":
|
|
447
|
+
response = create(ExecuteHookResponseSchema, {
|
|
448
|
+
response: { case: "subagentStart", value: create(SubagentStartRequestResponseSchema, {}) },
|
|
449
|
+
});
|
|
450
|
+
break;
|
|
451
|
+
case "subagentStop":
|
|
452
|
+
response = create(ExecuteHookResponseSchema, {
|
|
453
|
+
response: { case: "subagentStop", value: create(SubagentStopRequestResponseSchema, {}) },
|
|
454
|
+
});
|
|
455
|
+
break;
|
|
456
|
+
case "preToolUse":
|
|
457
|
+
response = create(ExecuteHookResponseSchema, {
|
|
458
|
+
response: { case: "preToolUse", value: create(PreToolUseRequestResponseSchema, {}) },
|
|
459
|
+
});
|
|
460
|
+
break;
|
|
461
|
+
case "postToolUse":
|
|
462
|
+
response = create(ExecuteHookResponseSchema, {
|
|
463
|
+
response: { case: "postToolUse", value: create(PostToolUseRequestResponseSchema, {}) },
|
|
464
|
+
});
|
|
465
|
+
break;
|
|
466
|
+
case "postToolUseFailure":
|
|
467
|
+
response = create(ExecuteHookResponseSchema, {
|
|
468
|
+
response: { case: "postToolUseFailure", value: create(PostToolUseFailureRequestResponseSchema, {}) },
|
|
469
|
+
});
|
|
470
|
+
break;
|
|
471
|
+
case "beforeSubmitPrompt":
|
|
472
|
+
response = create(ExecuteHookResponseSchema, {
|
|
473
|
+
response: { case: "beforeSubmitPrompt", value: create(BeforeSubmitPromptRequestResponseSchema, {}) },
|
|
474
|
+
});
|
|
475
|
+
break;
|
|
476
|
+
case "afterAgentResponse":
|
|
477
|
+
response = create(ExecuteHookResponseSchema, {
|
|
478
|
+
response: { case: "afterAgentResponse", value: create(AfterAgentResponseRequestResponseSchema, {}) },
|
|
479
|
+
});
|
|
480
|
+
break;
|
|
481
|
+
case "afterAgentThought":
|
|
482
|
+
response = create(ExecuteHookResponseSchema, {
|
|
483
|
+
response: { case: "afterAgentThought", value: create(AfterAgentThoughtRequestResponseSchema, {}) },
|
|
484
|
+
});
|
|
485
|
+
break;
|
|
486
|
+
case "stop":
|
|
487
|
+
response = create(ExecuteHookResponseSchema, {
|
|
488
|
+
response: { case: "stop", value: create(StopRequestResponseSchema, {}) },
|
|
489
|
+
});
|
|
490
|
+
break;
|
|
491
|
+
default:
|
|
492
|
+
return null;
|
|
493
|
+
}
|
|
494
|
+
return create(ExecuteHookResultSchema, { response });
|
|
495
|
+
}
|