@engenty/local-models 0.0.0-stage → 0.20.6
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/LICENSE +21 -0
- package/README.md +41 -2
- package/dist/clients/chat.d.ts +89 -0
- package/dist/clients/chat.js +22 -0
- package/dist/clients/claude.d.ts +32 -0
- package/dist/clients/claude.js +238 -0
- package/dist/clients/client.d.ts +29 -0
- package/dist/clients/client.js +2 -0
- package/dist/clients/codex.d.ts +35 -0
- package/dist/clients/codex.js +253 -0
- package/dist/clients/cursor.d.ts +33 -0
- package/dist/clients/cursor.js +180 -0
- package/dist/clients/detect.d.ts +18 -0
- package/dist/clients/detect.js +34 -0
- package/dist/clients/env.d.ts +66 -0
- package/dist/clients/env.js +211 -0
- package/dist/clients/failure.d.ts +26 -0
- package/dist/clients/failure.js +35 -0
- package/dist/clients/index.d.ts +14 -0
- package/dist/clients/index.js +16 -0
- package/dist/clients/model.d.ts +126 -0
- package/dist/clients/model.js +225 -0
- package/dist/clients/prompt.d.ts +36 -0
- package/dist/clients/prompt.js +123 -0
- package/dist/clients/run.d.ts +15 -0
- package/dist/clients/run.js +41 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/ollama/api.d.ts +70 -0
- package/dist/ollama/api.js +164 -0
- package/dist/ollama/index.d.ts +2 -0
- package/dist/ollama/index.js +2 -0
- package/dist/ollama/model.d.ts +52 -0
- package/dist/ollama/model.js +302 -0
- package/dist/probe.d.ts +54 -0
- package/dist/probe.js +190 -0
- package/package.json +56 -4
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import { writeFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { issuesIn, parseLine, record } from "./chat.js";
|
|
4
|
+
import { probeVersion, resolveEnv, run } from "./env.js";
|
|
5
|
+
import { clientFailure } from "./failure.js";
|
|
6
|
+
import { ClientModel } from "./model.js";
|
|
7
|
+
import { runClient } from "./run.js";
|
|
8
|
+
/**
|
|
9
|
+
* Codex: `codex exec` answers on the ChatGPT sign-in kept in CODEX_HOME. The person's own
|
|
10
|
+
* config is left out so none of their MCP servers start for a call; tools go over MCP through
|
|
11
|
+
* a config override; structured output is validated by the client (`--output-schema`).
|
|
12
|
+
*/
|
|
13
|
+
export const CODEX_BIN = process.env.CODEX_BIN?.trim() || "codex";
|
|
14
|
+
const spec = {
|
|
15
|
+
id: "codex",
|
|
16
|
+
passThrough: /^CODEX_HOME$/,
|
|
17
|
+
keyVars: [],
|
|
18
|
+
async auth(env) {
|
|
19
|
+
try {
|
|
20
|
+
const { stdout, stderr } = await run(CODEX_BIN, ["login", "status"], {
|
|
21
|
+
timeout: 15_000,
|
|
22
|
+
env,
|
|
23
|
+
});
|
|
24
|
+
const text = `${stdout}\n${stderr}`;
|
|
25
|
+
if (/not logged in/i.test(text)) {
|
|
26
|
+
return "none";
|
|
27
|
+
}
|
|
28
|
+
if (/api key/i.test(text)) {
|
|
29
|
+
return "api_key";
|
|
30
|
+
}
|
|
31
|
+
return /logged in/i.test(text) ? "subscription" : "none";
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return "none";
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
/** The events of one run as the model call wants them: the last agent message and the usage. */
|
|
39
|
+
export function codexAnswer(stdout) {
|
|
40
|
+
let text = null;
|
|
41
|
+
let usage;
|
|
42
|
+
let failure = null;
|
|
43
|
+
let sessionId;
|
|
44
|
+
for (const line of stdout.split("\n")) {
|
|
45
|
+
let event;
|
|
46
|
+
try {
|
|
47
|
+
event = JSON.parse(line);
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
if (event.type === "thread.started") {
|
|
53
|
+
sessionId = event.thread_id;
|
|
54
|
+
}
|
|
55
|
+
else if (event.type === "item.completed" && event.item?.type === "agent_message") {
|
|
56
|
+
text = event.item.text ?? "";
|
|
57
|
+
}
|
|
58
|
+
else if (event.type === "turn.completed") {
|
|
59
|
+
const u = event.usage ?? {};
|
|
60
|
+
usage = {
|
|
61
|
+
input: u.input_tokens ?? 0,
|
|
62
|
+
cacheRead: u.cached_input_tokens ?? 0,
|
|
63
|
+
cacheWrite: u.cache_write_input_tokens ?? 0,
|
|
64
|
+
output: u.output_tokens ?? 0,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
else if (event.type === "turn.failed" || event.type === "error") {
|
|
68
|
+
failure =
|
|
69
|
+
typeof event.error === "string"
|
|
70
|
+
? event.error
|
|
71
|
+
: (event.error?.message ?? event.message ?? "error");
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return {
|
|
75
|
+
answer: text === null ? null : { text, usage, sessionId },
|
|
76
|
+
failure,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
/** The arguments every headless `codex exec` starts with: read-only, no approvals, nothing of the person's config. */
|
|
80
|
+
export function codexExecArgs(dir, webSearch) {
|
|
81
|
+
return [
|
|
82
|
+
"exec",
|
|
83
|
+
"-",
|
|
84
|
+
"--json",
|
|
85
|
+
"--ephemeral",
|
|
86
|
+
"--skip-git-repo-check",
|
|
87
|
+
"--ignore-user-config",
|
|
88
|
+
"-s",
|
|
89
|
+
"read-only",
|
|
90
|
+
"-C",
|
|
91
|
+
dir,
|
|
92
|
+
"-c",
|
|
93
|
+
'approval_policy="never"',
|
|
94
|
+
"-c",
|
|
95
|
+
`web_search="${webSearch ? "live" : "disabled"}"`,
|
|
96
|
+
];
|
|
97
|
+
}
|
|
98
|
+
class CodexModel extends ClientModel {
|
|
99
|
+
provider = "codex";
|
|
100
|
+
nativeSchema = true;
|
|
101
|
+
async invoke(call) {
|
|
102
|
+
const args = codexExecArgs(call.dir, call.webSearch);
|
|
103
|
+
if (this.modelId !== "default") {
|
|
104
|
+
args.push("-m", this.modelId);
|
|
105
|
+
}
|
|
106
|
+
if (call.effort) {
|
|
107
|
+
args.push("-c", `model_reasoning_effort="${call.effort}"`);
|
|
108
|
+
}
|
|
109
|
+
if (call.system) {
|
|
110
|
+
// A JSON string is a TOML basic string: the instructions travel as one config value.
|
|
111
|
+
args.push("-c", `developer_instructions=${JSON.stringify(call.system)}`);
|
|
112
|
+
}
|
|
113
|
+
if (call.bridgeUrl) {
|
|
114
|
+
args.push("-c", `mcp_servers.step.url="${call.bridgeUrl}"`, "-c", "mcp_servers.step.startup_timeout_sec=30", "-c", "mcp_servers.step.tool_timeout_sec=600",
|
|
115
|
+
// The tools are the host's own: they run without the client asking anyone.
|
|
116
|
+
"-c", 'mcp_servers.step.default_tools_approval_mode="approve"');
|
|
117
|
+
}
|
|
118
|
+
if (call.schema) {
|
|
119
|
+
const file = join(call.dir, "schema.json");
|
|
120
|
+
writeFileSync(file, JSON.stringify(call.schema));
|
|
121
|
+
args.push("--output-schema", file);
|
|
122
|
+
}
|
|
123
|
+
const { stdout, stderr, code } = await runClient(CODEX_BIN, args, {
|
|
124
|
+
cwd: call.dir,
|
|
125
|
+
env: (await resolveEnv(spec)).env,
|
|
126
|
+
signal: call.signal,
|
|
127
|
+
stdin: call.prompt,
|
|
128
|
+
});
|
|
129
|
+
const { answer, failure } = codexAnswer(stdout);
|
|
130
|
+
if (failure || !answer) {
|
|
131
|
+
throw clientFailure(this.client, failure ?? stderr.trim() ?? `exit ${code}`);
|
|
132
|
+
}
|
|
133
|
+
return answer;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
/** The Codex features that bring tools of their own; the chat works through MCP only. */
|
|
137
|
+
const CODEX_TOOLS_OFF = [
|
|
138
|
+
"shell_tool",
|
|
139
|
+
"unified_exec",
|
|
140
|
+
"apps",
|
|
141
|
+
"plugins",
|
|
142
|
+
"browser_use",
|
|
143
|
+
"in_app_browser",
|
|
144
|
+
"computer_use",
|
|
145
|
+
"image_generation",
|
|
146
|
+
"multi_agent",
|
|
147
|
+
"goals",
|
|
148
|
+
"sleep_tool",
|
|
149
|
+
"tool_suggest",
|
|
150
|
+
"skill_search",
|
|
151
|
+
"memories",
|
|
152
|
+
];
|
|
153
|
+
export const codex = {
|
|
154
|
+
...spec,
|
|
155
|
+
id: "codex",
|
|
156
|
+
name: "Codex",
|
|
157
|
+
bin: CODEX_BIN,
|
|
158
|
+
version: (env) => probeVersion(CODEX_BIN, env, (stdout) => {
|
|
159
|
+
const words = stdout.trim().split(/\s+/);
|
|
160
|
+
return words[words.length - 1] || null;
|
|
161
|
+
}),
|
|
162
|
+
login: { args: ["login"], interactive: false },
|
|
163
|
+
model: (alias, options) => new CodexModel(codex, alias || "default", options),
|
|
164
|
+
args(call) {
|
|
165
|
+
const mcp = call.mcp;
|
|
166
|
+
const options = [
|
|
167
|
+
"--json",
|
|
168
|
+
"--skip-git-repo-check",
|
|
169
|
+
...(call.model ? ["-m", call.model] : []),
|
|
170
|
+
// Only the host's tools: none of the person's own MCP servers start.
|
|
171
|
+
"--ignore-user-config",
|
|
172
|
+
"-c",
|
|
173
|
+
'sandbox_mode="read-only"',
|
|
174
|
+
"-c",
|
|
175
|
+
'approval_policy="never"',
|
|
176
|
+
"-c",
|
|
177
|
+
'web_search="disabled"',
|
|
178
|
+
// `-c features.…` rather than `--disable`: a feature a later Codex drops is only ignored.
|
|
179
|
+
...CODEX_TOOLS_OFF.flatMap((f) => ["-c", `features.${f}=false`]),
|
|
180
|
+
// A JSON string is a TOML basic string: the instructions travel as one config value.
|
|
181
|
+
"-c",
|
|
182
|
+
`developer_instructions=${JSON.stringify(call.system)}`,
|
|
183
|
+
...(mcp
|
|
184
|
+
? [
|
|
185
|
+
"-c",
|
|
186
|
+
`mcp_servers.${mcp.name}.url=${JSON.stringify(mcp.url)}`,
|
|
187
|
+
// The key comes from the environment, not the command line, where any process could read it.
|
|
188
|
+
"-c",
|
|
189
|
+
`mcp_servers.${mcp.name}.bearer_token_env_var="${mcp.keyVar}"`,
|
|
190
|
+
"-c",
|
|
191
|
+
`mcp_servers.${mcp.name}.startup_timeout_sec=30`,
|
|
192
|
+
"-c",
|
|
193
|
+
`mcp_servers.${mcp.name}.tool_timeout_sec=600`,
|
|
194
|
+
// The tools are the host's own: they run without the client asking anyone.
|
|
195
|
+
"-c",
|
|
196
|
+
`mcp_servers.${mcp.name}.default_tools_approval_mode="approve"`,
|
|
197
|
+
]
|
|
198
|
+
: []),
|
|
199
|
+
];
|
|
200
|
+
// `-i` takes several files, so the pictures come last, after the prompt ("-" = stdin).
|
|
201
|
+
const images = call.images.flatMap((i) => ["-i", i.file]);
|
|
202
|
+
return call.session
|
|
203
|
+
? ["exec", "resume", call.session, "-", ...options, ...images]
|
|
204
|
+
: ["exec", "-", ...options, ...images];
|
|
205
|
+
},
|
|
206
|
+
stdin: (call) => call.message,
|
|
207
|
+
events(line) {
|
|
208
|
+
const event = parseLine(line);
|
|
209
|
+
if (!event) {
|
|
210
|
+
return [];
|
|
211
|
+
}
|
|
212
|
+
const item = event.item ?? {};
|
|
213
|
+
if (event.type === "thread.started" && event.thread_id) {
|
|
214
|
+
return [{ kind: "session", id: String(event.thread_id) }];
|
|
215
|
+
}
|
|
216
|
+
if (item.type === "mcp_tool_call" && event.type === "item.started") {
|
|
217
|
+
return [
|
|
218
|
+
{
|
|
219
|
+
kind: "tool",
|
|
220
|
+
name: String(item.tool ?? ""),
|
|
221
|
+
args: record(item.arguments),
|
|
222
|
+
started: true,
|
|
223
|
+
},
|
|
224
|
+
];
|
|
225
|
+
}
|
|
226
|
+
if (item.type === "mcp_tool_call" && event.type === "item.completed") {
|
|
227
|
+
const text = (item.result?.content ?? []).find((c) => c.type === "text");
|
|
228
|
+
return [{ kind: "toolDone", issues: issuesIn(text?.text) }];
|
|
229
|
+
}
|
|
230
|
+
if (event.type === "item.completed" && item.type === "agent_message" && item.text?.trim()) {
|
|
231
|
+
// A message can end in a line break; the turn puts one blank line between messages.
|
|
232
|
+
return [{ kind: "text", text: String(item.text).trim() }];
|
|
233
|
+
}
|
|
234
|
+
if (event.type === "item.completed" && item.type === "reasoning" && item.text) {
|
|
235
|
+
return [
|
|
236
|
+
{ kind: "thinkingStart" },
|
|
237
|
+
{ kind: "thinking", text: String(item.text), whole: true },
|
|
238
|
+
];
|
|
239
|
+
}
|
|
240
|
+
if (event.type === "turn.failed") {
|
|
241
|
+
const error = event.error;
|
|
242
|
+
return [
|
|
243
|
+
{
|
|
244
|
+
kind: "failure",
|
|
245
|
+
detail: typeof error === "string" ? error : String(error?.message ?? "error"),
|
|
246
|
+
},
|
|
247
|
+
];
|
|
248
|
+
}
|
|
249
|
+
// A top-level `error` is often a retry ("Reconnecting... 1/5"); turn.failed ends the turn.
|
|
250
|
+
return [];
|
|
251
|
+
},
|
|
252
|
+
signedOut: (detail) => /\b401\b|unauthori[sz]ed|not logged in|codex login|authenticat|log ?in/i.test(detail),
|
|
253
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { Client } from "./client.js";
|
|
2
|
+
/**
|
|
3
|
+
* Cursor Agent: `cursor-agent -p` answers on the Cursor sign-in. The call's folder is its
|
|
4
|
+
* workspace: the instructions as AGENTS.md, the tools as an MCP server in .cursor/mcp.json.
|
|
5
|
+
* The answer is asked for as JSON where a schema is wanted.
|
|
6
|
+
*/
|
|
7
|
+
export declare const CURSOR_BIN: string;
|
|
8
|
+
/** What the client prints with `--output-format json`. */
|
|
9
|
+
export interface CursorResult {
|
|
10
|
+
type?: string;
|
|
11
|
+
subtype?: string;
|
|
12
|
+
is_error?: boolean;
|
|
13
|
+
result?: unknown;
|
|
14
|
+
session_id?: string;
|
|
15
|
+
duration_ms?: number;
|
|
16
|
+
}
|
|
17
|
+
export declare const cursor: Client;
|
|
18
|
+
/**
|
|
19
|
+
* Cursor Agent's workspace files for a chat: the instructions, the host's MCP server, and the
|
|
20
|
+
* permissions. The CLI also loads the servers of ~/.cursor/mcp.json and `--approve-mcps`
|
|
21
|
+
* approves them all, so each of those is denied by name; a deny wins over an allow.
|
|
22
|
+
*/
|
|
23
|
+
export declare function cursorWorkspace(input: {
|
|
24
|
+
system: string;
|
|
25
|
+
/** The host's MCP server, by the name the client knows it by. */
|
|
26
|
+
mcp: {
|
|
27
|
+
name: string;
|
|
28
|
+
url: string;
|
|
29
|
+
};
|
|
30
|
+
key: string;
|
|
31
|
+
/** The server names in the person's own ~/.cursor/mcp.json. */
|
|
32
|
+
ownServers: string[];
|
|
33
|
+
}): Record<string, string>;
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { mkdirSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { issuesIn, parseLine, record } from "./chat.js";
|
|
4
|
+
import { probeVersion, resolveEnv, run } from "./env.js";
|
|
5
|
+
import { clientFailure } from "./failure.js";
|
|
6
|
+
import { ClientModel } from "./model.js";
|
|
7
|
+
import { lastJson, runClient } from "./run.js";
|
|
8
|
+
/**
|
|
9
|
+
* Cursor Agent: `cursor-agent -p` answers on the Cursor sign-in. The call's folder is its
|
|
10
|
+
* workspace: the instructions as AGENTS.md, the tools as an MCP server in .cursor/mcp.json.
|
|
11
|
+
* The answer is asked for as JSON where a schema is wanted.
|
|
12
|
+
*/
|
|
13
|
+
export const CURSOR_BIN = process.env.CURSOR_AGENT_BIN?.trim() || "cursor-agent";
|
|
14
|
+
/** The escape sequences a terminal app draws with. */
|
|
15
|
+
const ANSI = new RegExp(`${String.fromCharCode(27)}\\[[0-9;]*[A-Za-z]`, "g");
|
|
16
|
+
const spec = {
|
|
17
|
+
id: "cursor",
|
|
18
|
+
passThrough: /^CURSOR_API_KEY$/,
|
|
19
|
+
keyVars: ["CURSOR_API_KEY"],
|
|
20
|
+
async auth(env) {
|
|
21
|
+
try {
|
|
22
|
+
const { stdout, stderr } = await run(CURSOR_BIN, ["status"], { timeout: 15_000, env });
|
|
23
|
+
// The client draws on the terminal even here: the control sequences are taken out.
|
|
24
|
+
const text = `${stdout}\n${stderr}`.replace(ANSI, "");
|
|
25
|
+
if (/not logged in/i.test(text) || !/logged in/i.test(text)) {
|
|
26
|
+
return "none";
|
|
27
|
+
}
|
|
28
|
+
return env.CURSOR_API_KEY ? "api_key" : "subscription";
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return "none";
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
class CursorModel extends ClientModel {
|
|
36
|
+
provider = "cursor-agent";
|
|
37
|
+
async invoke(call) {
|
|
38
|
+
if (call.system) {
|
|
39
|
+
writeFileSync(join(call.dir, "AGENTS.md"), call.system);
|
|
40
|
+
}
|
|
41
|
+
if (call.bridgeUrl) {
|
|
42
|
+
mkdirSync(join(call.dir, ".cursor"), { recursive: true });
|
|
43
|
+
writeFileSync(join(call.dir, ".cursor", "mcp.json"), JSON.stringify({ mcpServers: { step: { url: call.bridgeUrl } } }));
|
|
44
|
+
// The call's tools are all on the bridge: the client runs no commands and writes no files.
|
|
45
|
+
writeFileSync(join(call.dir, ".cursor", "cli.json"), JSON.stringify({
|
|
46
|
+
permissions: { allow: ["Mcp(step:*)"], deny: ["Shell(*)", "Write(**)"] },
|
|
47
|
+
}));
|
|
48
|
+
}
|
|
49
|
+
const args = ["-p", "--output-format", "json", "--workspace", call.dir, "--approve-mcps"];
|
|
50
|
+
if (this.modelId !== "auto") {
|
|
51
|
+
args.push("--model", this.modelId);
|
|
52
|
+
}
|
|
53
|
+
if (!call.bridgeUrl) {
|
|
54
|
+
// Nothing to work through: the client answers, it does not edit.
|
|
55
|
+
args.push("--mode", "ask");
|
|
56
|
+
}
|
|
57
|
+
const { stdout, stderr, code } = await runClient(CURSOR_BIN, args, {
|
|
58
|
+
cwd: call.dir,
|
|
59
|
+
env: (await resolveEnv(spec)).env,
|
|
60
|
+
signal: call.signal,
|
|
61
|
+
stdin: call.prompt,
|
|
62
|
+
});
|
|
63
|
+
const event = lastJson(stdout);
|
|
64
|
+
if (!event) {
|
|
65
|
+
throw clientFailure(this.client, stderr.trim() || `exit ${code}`);
|
|
66
|
+
}
|
|
67
|
+
if (event.is_error) {
|
|
68
|
+
throw clientFailure(this.client, String(event.result ?? "error"));
|
|
69
|
+
}
|
|
70
|
+
return {
|
|
71
|
+
text: typeof event.result === "string" ? event.result : JSON.stringify(event.result ?? ""),
|
|
72
|
+
sessionId: event.session_id,
|
|
73
|
+
meta: { durationMs: event.duration_ms ?? 0 },
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
export const cursor = {
|
|
78
|
+
...spec,
|
|
79
|
+
id: "cursor",
|
|
80
|
+
name: "Cursor Agent",
|
|
81
|
+
bin: CURSOR_BIN,
|
|
82
|
+
version: (env) => probeVersion(CURSOR_BIN, env, (stdout) => stdout.trim().split("\n").pop()?.trim() || null),
|
|
83
|
+
login: { args: ["login"], interactive: false },
|
|
84
|
+
model: (alias, options) => new CursorModel(cursor, alias || "auto", options),
|
|
85
|
+
// The workspace (call.dir) holds the instructions as AGENTS.md, the MCP server and the
|
|
86
|
+
// permissions in .cursor/: see cursorWorkspace().
|
|
87
|
+
args: (call) => [
|
|
88
|
+
"-p",
|
|
89
|
+
"--output-format",
|
|
90
|
+
"stream-json",
|
|
91
|
+
"--workspace",
|
|
92
|
+
call.dir,
|
|
93
|
+
...(call.mcp ? ["--approve-mcps"] : []),
|
|
94
|
+
...(call.model && call.model !== "auto" ? ["--model", call.model] : []),
|
|
95
|
+
...(call.session ? ["--resume", call.session] : []),
|
|
96
|
+
],
|
|
97
|
+
stdin: (call) => call.message,
|
|
98
|
+
events(line) {
|
|
99
|
+
const event = parseLine(line);
|
|
100
|
+
if (!event) {
|
|
101
|
+
return [];
|
|
102
|
+
}
|
|
103
|
+
const out = [];
|
|
104
|
+
if (event.type === "system" && event.session_id) {
|
|
105
|
+
out.push({ kind: "session", id: String(event.session_id) });
|
|
106
|
+
}
|
|
107
|
+
else if (event.type === "assistant") {
|
|
108
|
+
for (const block of event.message?.content ?? []) {
|
|
109
|
+
if (block.type === "text" && block.text) {
|
|
110
|
+
out.push({ kind: "text", text: block.text });
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
else if (event.type === "tool_call" && event.subtype === "started") {
|
|
115
|
+
const call = record(event.tool_call);
|
|
116
|
+
// The call as its protobuf JSON: { mcpToolCall: { args: { toolName, args, … } } }.
|
|
117
|
+
const mcp = record(record(call.mcpToolCall).args);
|
|
118
|
+
if (mcp.toolName || mcp.name) {
|
|
119
|
+
out.push({
|
|
120
|
+
kind: "tool",
|
|
121
|
+
name: String(mcp.toolName ?? mcp.name),
|
|
122
|
+
args: record(mcp.args),
|
|
123
|
+
started: true,
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
else if (event.type === "tool_call" && event.subtype === "completed") {
|
|
128
|
+
const call = record(record(event.tool_call).mcpToolCall);
|
|
129
|
+
const mcp = record(call.args);
|
|
130
|
+
if (mcp.toolName || mcp.name) {
|
|
131
|
+
const content = record(record(call.result).success).content;
|
|
132
|
+
const first = Array.isArray(content) ? record(content[0]) : {};
|
|
133
|
+
out.push({ kind: "toolDone", issues: issuesIn(record(first.text).text ?? first.text) });
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
else if (event.type === "thinking" && event.subtype === "delta") {
|
|
137
|
+
out.push({ kind: "thinking", text: String(event.text ?? "") });
|
|
138
|
+
}
|
|
139
|
+
else if (event.type === "result") {
|
|
140
|
+
if (event.session_id) {
|
|
141
|
+
out.push({ kind: "session", id: String(event.session_id) });
|
|
142
|
+
}
|
|
143
|
+
if (event.is_error) {
|
|
144
|
+
out.push({ kind: "failure", detail: String(event.result ?? "error") });
|
|
145
|
+
}
|
|
146
|
+
else if (typeof event.result === "string") {
|
|
147
|
+
out.push({ kind: "result", text: event.result });
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return out;
|
|
151
|
+
},
|
|
152
|
+
signedOut: (detail) => /authenticat|log ?in|CURSOR_API_KEY|unauthori[sz]ed|\b401\b/i.test(detail),
|
|
153
|
+
};
|
|
154
|
+
/**
|
|
155
|
+
* Cursor Agent's workspace files for a chat: the instructions, the host's MCP server, and the
|
|
156
|
+
* permissions. The CLI also loads the servers of ~/.cursor/mcp.json and `--approve-mcps`
|
|
157
|
+
* approves them all, so each of those is denied by name; a deny wins over an allow.
|
|
158
|
+
*/
|
|
159
|
+
export function cursorWorkspace(input) {
|
|
160
|
+
const { name, url } = input.mcp;
|
|
161
|
+
return {
|
|
162
|
+
"AGENTS.md": input.system,
|
|
163
|
+
".cursor/mcp.json": JSON.stringify({
|
|
164
|
+
mcpServers: {
|
|
165
|
+
[name]: { url, headers: { Authorization: `Bearer ${input.key}` } },
|
|
166
|
+
},
|
|
167
|
+
}),
|
|
168
|
+
".cursor/cli.json": JSON.stringify({
|
|
169
|
+
permissions: {
|
|
170
|
+
allow: [`Mcp(${name}:*)`],
|
|
171
|
+
deny: [
|
|
172
|
+
"Shell(*)",
|
|
173
|
+
"Read(**)",
|
|
174
|
+
"Write(**)",
|
|
175
|
+
...input.ownServers.filter((s) => s !== name).map((s) => `Mcp(${s}:*)`),
|
|
176
|
+
],
|
|
177
|
+
},
|
|
178
|
+
}),
|
|
179
|
+
};
|
|
180
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type ClientAuth, type EnvSpec } from "./env.js";
|
|
2
|
+
/** A client as detection needs it: how it signs in, and its version. */
|
|
3
|
+
export interface Detectable extends EnvSpec {
|
|
4
|
+
/** Null: not installed. Throws ProbeUndecided when the probe could not finish on a busy machine. */
|
|
5
|
+
version(env: NodeJS.ProcessEnv): Promise<string | null>;
|
|
6
|
+
}
|
|
7
|
+
/** A client on this computer as it is now. */
|
|
8
|
+
export interface ClientStatus {
|
|
9
|
+
/** Null: not installed. */
|
|
10
|
+
version: string | null;
|
|
11
|
+
auth: ClientAuth;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Whether a client is installed and signed in, asked again after a minute. `force` asks now and
|
|
15
|
+
* reads the login shell again: the person may just have signed in or set a key. A probe that
|
|
16
|
+
* could not finish on a busy machine keeps what the last look found.
|
|
17
|
+
*/
|
|
18
|
+
export declare function detectClient(client: Detectable, force?: boolean): Promise<ClientStatus>;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { forgetEnv, ProbeUndecided, resolveEnv } from "./env.js";
|
|
2
|
+
/** How long a look at a client holds: it may get installed or signed in meanwhile. */
|
|
3
|
+
const FRESH_MS = 60_000;
|
|
4
|
+
const detected = new Map();
|
|
5
|
+
/**
|
|
6
|
+
* Whether a client is installed and signed in, asked again after a minute. `force` asks now and
|
|
7
|
+
* reads the login shell again: the person may just have signed in or set a key. A probe that
|
|
8
|
+
* could not finish on a busy machine keeps what the last look found.
|
|
9
|
+
*/
|
|
10
|
+
export async function detectClient(client, force = false) {
|
|
11
|
+
const hit = detected.get(client.id);
|
|
12
|
+
if (!force && hit && Date.now() - hit.at < FRESH_MS) {
|
|
13
|
+
return hit.value;
|
|
14
|
+
}
|
|
15
|
+
if (force) {
|
|
16
|
+
forgetEnv(client.id);
|
|
17
|
+
}
|
|
18
|
+
let value = { version: null, auth: "none" };
|
|
19
|
+
try {
|
|
20
|
+
const { env, auth } = await resolveEnv(client);
|
|
21
|
+
const version = await client.version(env);
|
|
22
|
+
if (version) {
|
|
23
|
+
value = { version, auth };
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
catch (err) {
|
|
27
|
+
if (err instanceof ProbeUndecided && hit?.value.version) {
|
|
28
|
+
// The machine is busy (another client is working): the client was there, it still is.
|
|
29
|
+
value = hit.value;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
detected.set(client.id, { at: Date.now(), value });
|
|
33
|
+
return value;
|
|
34
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/** How a client is signed in: with the person's subscription, with a key from their shell, or not at all. */
|
|
2
|
+
export type ClientAuth = "subscription" | "api_key" | "none";
|
|
3
|
+
/** What the login shell decides about a client: its environment and its sign-in. */
|
|
4
|
+
export interface EnvSpec {
|
|
5
|
+
id: string;
|
|
6
|
+
/** Variables of the login shell the client may see: how it signs in and where it talks to. */
|
|
7
|
+
passThrough: RegExp;
|
|
8
|
+
/** Of those, the keys and tokens: held back while the client is signed in without them. */
|
|
9
|
+
keyVars: string[];
|
|
10
|
+
auth(env: NodeJS.ProcessEnv): Promise<ClientAuth>;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Runs a file and gives what it printed, like execFile: it rejects when the file is not there,
|
|
14
|
+
* ends with another code than 0, or takes longer than `timeout` (then with `killed` and `signal`
|
|
15
|
+
* set, as execFile does).
|
|
16
|
+
*/
|
|
17
|
+
export declare function run(file: string, args: string[], options?: {
|
|
18
|
+
env?: NodeJS.ProcessEnv;
|
|
19
|
+
cwd?: string;
|
|
20
|
+
timeout?: number;
|
|
21
|
+
}): Promise<{
|
|
22
|
+
stdout: string;
|
|
23
|
+
stderr: string;
|
|
24
|
+
}>;
|
|
25
|
+
/** A version probe that could not finish: the machine is busy, the client may well be there. */
|
|
26
|
+
export declare class ProbeUndecided extends Error {
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* `<bin> --version`, read by `pick`. Null: the client is not there or does not answer. A probe
|
|
30
|
+
* that ran out of time or could not start for want of resources throws ProbeUndecided: a client
|
|
31
|
+
* that starts slowly while another one is busy is still installed.
|
|
32
|
+
*/
|
|
33
|
+
export declare function probeVersion(bin: string, env: NodeJS.ProcessEnv, pick: (stdout: string) => string | null): Promise<string | null>;
|
|
34
|
+
/** The person's login shell, asked once: a desktop app and a bare dev server start without its variables. */
|
|
35
|
+
export declare function loginShellEnv(): Promise<Record<string, string>>;
|
|
36
|
+
/**
|
|
37
|
+
* The environment a client gets: its sign-in as the login shell sets it (else as this process
|
|
38
|
+
* has it), PATH as the shell has it so the client is found from a GUI app too, and nothing else —
|
|
39
|
+
* no key of the host's, no session of a parent client.
|
|
40
|
+
*/
|
|
41
|
+
export declare function clientEnv(shell: Record<string, string | undefined>, own: Record<string, string | undefined>, passThrough: RegExp,
|
|
42
|
+
/** Folders before the shell's PATH: the clients the host installed. */
|
|
43
|
+
first?: string[], platform?: NodeJS.Platform): NodeJS.ProcessEnv;
|
|
44
|
+
/** The environment without the named keys. */
|
|
45
|
+
export declare function stripKeys(env: NodeJS.ProcessEnv, keyVars: string[]): NodeJS.ProcessEnv;
|
|
46
|
+
/**
|
|
47
|
+
* Where the host installs clients, searched before the shell's PATH: a setup that installs a
|
|
48
|
+
* client into a folder of its own names the folder here, once, before any client runs.
|
|
49
|
+
*/
|
|
50
|
+
export declare function setClientFolders(list: () => string[]): void;
|
|
51
|
+
export interface ResolvedEnv {
|
|
52
|
+
env: NodeJS.ProcessEnv;
|
|
53
|
+
auth: ClientAuth;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* The environment a client runs in, decided once: the subscription sign-in comes first — it is
|
|
57
|
+
* what the person installed the client for — so a key the shell sets is held back while the
|
|
58
|
+
* client is signed in without it, and used only where it is the one way in.
|
|
59
|
+
*/
|
|
60
|
+
export declare function resolveEnv(spec: EnvSpec): Promise<ResolvedEnv>;
|
|
61
|
+
/** The environment of a sign-in: the client on its own, without a key of the shell, in a real terminal. */
|
|
62
|
+
export declare function loginEnv(spec: EnvSpec): Promise<NodeJS.ProcessEnv>;
|
|
63
|
+
/** The client's sign-in right now, on its own: asked again and again while the person signs in. */
|
|
64
|
+
export declare function freshAuth(spec: EnvSpec): Promise<ClientAuth>;
|
|
65
|
+
/** Forgets what was decided: the person may just have signed in or changed their shell. */
|
|
66
|
+
export declare function forgetEnv(id?: string): void;
|