@hexdecimal16/nexo 0.4.16 → 0.4.18
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/core/src/providers/agy-p.js +159 -18
- package/package.json +1 -1
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { spawn } from "node:child_process";
|
|
2
|
-
import { rmSync } from "node:fs";
|
|
2
|
+
import { existsSync, mkdirSync, readdirSync, rmSync, statSync, symlinkSync, writeFileSync } from "node:fs";
|
|
3
3
|
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
4
|
-
import { tmpdir } from "node:os";
|
|
4
|
+
import { homedir, tmpdir } from "node:os";
|
|
5
5
|
import { join } from "node:path";
|
|
6
6
|
import { createInterface } from "node:readline";
|
|
7
7
|
import { createAssistantMessageEventStream, createProvider, getCurrentSystemPrompt, getCurrentTools, } from "@earendil-works/pi-ai";
|
|
8
8
|
import { getBuiltinModels } from "@earendil-works/pi-ai/providers/all";
|
|
9
|
-
import { renderTranscriptParts } from "./claude-p.js";
|
|
9
|
+
import { NEXT_TURN_NOTE, renderEntryParts, renderTranscriptParts } from "./claude-p.js";
|
|
10
10
|
import { parseReply } from "./kiro-p.js";
|
|
11
11
|
import { ReplyTextExtractor } from "./reply-text.js";
|
|
12
12
|
/** Provider id: models are referenced as `agy-p/<model>`, e.g. `agy-p/gemini-4-neon` or `agy-p/claude-sonnet-5-5`. */
|
|
@@ -152,12 +152,78 @@ export function agyToolsSection(tools) {
|
|
|
152
152
|
"",
|
|
153
153
|
'Every reply must be exactly one JSON object and nothing else (no prose around it, no code fence): {"text": string, "tool_calls": [{"name": string, "arguments": object}]}.',
|
|
154
154
|
"`text` is what you say to the user (may be empty). `tool_calls` lists actions for the caller to run now, with arguments matching the action's parameters.",
|
|
155
|
-
"The actions below are not tools you can invoke: never call them directly and never invoke any built-in tool (
|
|
155
|
+
"The actions below are not tools you can invoke: never call them directly and never invoke any native or built-in tool (`call_mcp_tool`, `list_resources`, `read_resource`, `manage_task`, etc.). These actions are NOT MCP tools — only list them in the JSON `tool_calls` array. The caller runs them and sends the results back in the next turn as <tool_result> entries. When you are finished, leave `tool_calls` empty and put the final answer in `text`.",
|
|
156
|
+
"When you list `tool_calls`, use `text` for one short sentence: what the earlier results showed and what these calls will check.",
|
|
157
|
+
"Earlier <assistant> entries may open with <notes>: your own reasoning from that turn, carried over because each turn starts fresh. Build on it, never write <notes> yourself, and do not repeat a command or read whose <tool_result> already answers the question.",
|
|
156
158
|
"",
|
|
157
159
|
"Available actions, one JSON definition per line:",
|
|
158
160
|
defs,
|
|
159
161
|
].join("\n");
|
|
160
162
|
}
|
|
163
|
+
/** How many of the latest assistant turns carry their reasoning forward, and how much of each (its end, where conclusions are). */
|
|
164
|
+
const NOTES_TURNS = 20;
|
|
165
|
+
const NOTES_CHARS = 1_500;
|
|
166
|
+
const NEW_MESSAGE_NOTE = "The last <user> entry is the user's newest message: follow it, even where it changes or stops the earlier work.";
|
|
167
|
+
function withNotes(message) {
|
|
168
|
+
if (message.role !== "assistant")
|
|
169
|
+
return message;
|
|
170
|
+
const reasoning = message.content
|
|
171
|
+
.map((b) => (b.type === "thinking" && !b.redacted ? b.thinking.trim() : ""))
|
|
172
|
+
.filter((t) => t.length > 0)
|
|
173
|
+
.join("\n");
|
|
174
|
+
if (!reasoning)
|
|
175
|
+
return message;
|
|
176
|
+
const kept = reasoning.length > NOTES_CHARS ? `…${reasoning.slice(-NOTES_CHARS)}` : reasoning;
|
|
177
|
+
return { ...message, content: [{ type: "text", text: `<notes>\n${kept}\n</notes>` }, ...message.content] };
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* The conversation as `agy acp` sees it. Each turn is a fresh session, so the model's reasoning from earlier turns
|
|
181
|
+
* would otherwise be lost: Gemini thinks in thinking blocks and leaves `text` empty on tool turns, then re-derives (and
|
|
182
|
+
* re-runs) what it already found. The latest turns' reasoning goes back in as <notes>, and a new user message that
|
|
183
|
+
* ends the transcript is called out so it is not mistaken for context.
|
|
184
|
+
*/
|
|
185
|
+
export function agyTranscriptParts(messages) {
|
|
186
|
+
const turns = messages.filter((m) => m.role !== "system");
|
|
187
|
+
if (turns.length === 1 && turns[0]?.role === "user")
|
|
188
|
+
return renderTranscriptParts(turns);
|
|
189
|
+
const assistants = turns.flatMap((m, i) => (m.role === "assistant" ? [i] : []));
|
|
190
|
+
const noted = new Set(assistants.slice(-NOTES_TURNS));
|
|
191
|
+
const parts = renderEntryParts(turns.map((m, i) => (noted.has(i) ? withNotes(m) : m)));
|
|
192
|
+
const note = turns.at(-1)?.role === "user" ? `${NEXT_TURN_NOTE} ${NEW_MESSAGE_NOTE}` : NEXT_TURN_NOTE;
|
|
193
|
+
parts.push({ type: "text", text: `\n\n${note}` });
|
|
194
|
+
return parts;
|
|
195
|
+
}
|
|
196
|
+
/** MCP config agy reads from `<gemini dir>/config/`: the mirror swaps in an empty one. */
|
|
197
|
+
const MCP_CONFIG = "mcp_config.json";
|
|
198
|
+
/**
|
|
199
|
+
* Builds a copy of the Antigravity data directory under `workspace` whose entries are symlinks to the real ones (login,
|
|
200
|
+
* settings and projects included), except `config/mcp_config.json`, which lists no servers. `agy acp` offers the model
|
|
201
|
+
* every configured MCP server regardless of the agent's `tools: []` and the session's `mcpServers: []`, and those tools
|
|
202
|
+
* are what the model reaches for instead of the JSON reply. Returns undefined when there is nothing to mirror or the
|
|
203
|
+
* mirror cannot be made; the server then runs against the real directory.
|
|
204
|
+
*/
|
|
205
|
+
export function mirrorGeminiDir(source, workspace) {
|
|
206
|
+
try {
|
|
207
|
+
if (!statSync(source).isDirectory())
|
|
208
|
+
return undefined;
|
|
209
|
+
const target = join(workspace, ".gemini-home");
|
|
210
|
+
mkdirSync(join(target, "config"), { recursive: true });
|
|
211
|
+
for (const name of readdirSync(source))
|
|
212
|
+
if (name !== "config")
|
|
213
|
+
symlinkSync(join(source, name), join(target, name));
|
|
214
|
+
const config = join(source, "config");
|
|
215
|
+
if (existsSync(config)) {
|
|
216
|
+
for (const name of readdirSync(config))
|
|
217
|
+
if (name !== MCP_CONFIG)
|
|
218
|
+
symlinkSync(join(config, name), join(target, "config", name));
|
|
219
|
+
}
|
|
220
|
+
writeFileSync(join(target, "config", MCP_CONFIG), '{"mcpServers":{}}\n', "utf8");
|
|
221
|
+
return target;
|
|
222
|
+
}
|
|
223
|
+
catch {
|
|
224
|
+
return undefined;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
161
227
|
let callSeq = 0;
|
|
162
228
|
function toolCallId() {
|
|
163
229
|
callSeq = (callSeq + 1) % 1_000_000;
|
|
@@ -257,6 +323,40 @@ function rpcErrorMessage(error) {
|
|
|
257
323
|
}
|
|
258
324
|
return detail && !base.includes(detail) ? `${base}: ${detail}` : base;
|
|
259
325
|
}
|
|
326
|
+
function parseObjectArgs(value) {
|
|
327
|
+
if (value === undefined || value === null)
|
|
328
|
+
return {};
|
|
329
|
+
let parsed = value;
|
|
330
|
+
if (typeof value === "string") {
|
|
331
|
+
try {
|
|
332
|
+
parsed = JSON.parse(value);
|
|
333
|
+
}
|
|
334
|
+
catch {
|
|
335
|
+
return undefined;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
return typeof parsed === "object" && parsed !== null && !Array.isArray(parsed) ? parsed : undefined;
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Extracts a nexo `ToolCall` when a model invokes Antigravity's residual `call_mcp_tool` targeting one of nexo's own
|
|
342
|
+
* declared actions (e.g. `ToolName: "bash"`). Returns undefined for any non-nexo tool or malformed arguments so real
|
|
343
|
+
* MCP tools and other native tools are always rejected.
|
|
344
|
+
*/
|
|
345
|
+
export function extractMcpToolCall(update, knownTools) {
|
|
346
|
+
if (update.name !== "call_mcp_tool")
|
|
347
|
+
return undefined;
|
|
348
|
+
const raw = update.args ?? update.rawInput;
|
|
349
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw))
|
|
350
|
+
return undefined;
|
|
351
|
+
const obj = raw;
|
|
352
|
+
const toolName = typeof obj.ToolName === "string" ? obj.ToolName : typeof obj.toolName === "string" ? obj.toolName : undefined;
|
|
353
|
+
if (!toolName || !knownTools.has(toolName))
|
|
354
|
+
return undefined;
|
|
355
|
+
const args = parseObjectArgs(obj.Arguments ?? obj.arguments);
|
|
356
|
+
if (!args)
|
|
357
|
+
return undefined;
|
|
358
|
+
return { type: "toolCall", id: toolCallId(), name: toolName, arguments: args };
|
|
359
|
+
}
|
|
260
360
|
const AGENT_FILE = [
|
|
261
361
|
"---",
|
|
262
362
|
`name: ${AGENT_NAME}`,
|
|
@@ -264,7 +364,7 @@ const AGENT_FILE = [
|
|
|
264
364
|
"tools: []",
|
|
265
365
|
"mainAgent: true",
|
|
266
366
|
"---",
|
|
267
|
-
"Follow the instructions provided for this session.
|
|
367
|
+
"Follow the instructions provided for this session. Never invoke any native or built-in tools (including `call_mcp_tool`, `list_resources`, `read_resource`, and `manage_task`).",
|
|
268
368
|
"Your own working directory is an empty scratch directory unrelated to the caller's. Never resolve or link file paths against it: use relative paths as given, or only absolute paths named in the conversation.",
|
|
269
369
|
"",
|
|
270
370
|
].join("\n");
|
|
@@ -288,14 +388,17 @@ class AcpConnection {
|
|
|
288
388
|
this.onClose = onClose;
|
|
289
389
|
}
|
|
290
390
|
/** Starts the server and completes the `initialize` handshake. */
|
|
291
|
-
static async open(run, onClose) {
|
|
391
|
+
static async open(run, onClose, geminiDir) {
|
|
292
392
|
registerExitHandler();
|
|
293
393
|
// Throwaway workspace holding the no-tools custom agent under ./.agents/agents, with no project files in reach.
|
|
294
394
|
const dir = await mkdtemp(join(tmpdir(), "nexo-agy-p-"));
|
|
295
395
|
liveDirs.add(dir);
|
|
296
396
|
await mkdir(join(dir, ".agents", "agents"), { recursive: true });
|
|
297
397
|
await writeFile(join(dir, ".agents", "agents", `${AGENT_NAME}.md`), AGENT_FILE, "utf8");
|
|
298
|
-
|
|
398
|
+
// Removing the workspace later unlinks the mirror's symlinks without touching what they point at.
|
|
399
|
+
const mirror = geminiDir === null ? undefined : mirrorGeminiDir(geminiDir, dir);
|
|
400
|
+
const args = ["acp", "--agent", AGENT_NAME, "--include-default-customizations=false", ...(mirror ? ["--gemini-dir", mirror] : [])];
|
|
401
|
+
const proc = run({ args, cwd: dir });
|
|
299
402
|
const conn = new AcpConnection(proc, dir, onClose);
|
|
300
403
|
void conn.pump();
|
|
301
404
|
void proc.exit.then((exit) => conn.shutdown(new Error(exit.stderr.trim().split("\n").at(-1) || `agy exited with code ${exit.code ?? "null"}`)));
|
|
@@ -423,6 +526,9 @@ function looksLikeReplyAttempt(text) {
|
|
|
423
526
|
return /"tool_calls"\s*:/.test(text) || /^\s*(```|\{)/.test(text);
|
|
424
527
|
}
|
|
425
528
|
const RETRY_NOTE = "Your previous reply was not a single valid JSON object in the required format. Reply again with only that JSON object.";
|
|
529
|
+
function nativeRetryNote(call) {
|
|
530
|
+
return `The tool "${call}" is not available: you have no native, built-in or MCP tools. Reply again with only the JSON object, listing actions in its \`tool_calls\` array.`;
|
|
531
|
+
}
|
|
426
532
|
function raceAbort(promise, signal) {
|
|
427
533
|
if (!signal)
|
|
428
534
|
return promise;
|
|
@@ -469,6 +575,7 @@ function streamAgyP(getConn, model, context, options) {
|
|
|
469
575
|
return fail("aborted", "Request was aborted");
|
|
470
576
|
const tools = getCurrentTools(context.messages);
|
|
471
577
|
const useTools = tools.length > 0 && options?.toolChoice !== "none";
|
|
578
|
+
const knownTools = useTools ? new Set(tools.map((t) => t.name)) : new Set();
|
|
472
579
|
const systemPrompt = [getCurrentSystemPrompt(context.messages), useTools ? agyToolsSection(tools) : ""].filter((s) => s.length > 0).join("\n\n");
|
|
473
580
|
const idleMs = parseIdleMs(process.env[AGY_P_IDLE_ENV]);
|
|
474
581
|
const closeSession = (session) => {
|
|
@@ -545,10 +652,12 @@ function streamAgyP(getConn, model, context, options) {
|
|
|
545
652
|
? [{ type: "text", text: input }]
|
|
546
653
|
: input.map((p) => (p.type === "text" ? { type: "text", text: p.text } : { type: "image", data: p.data, mimeType: p.mimeType }));
|
|
547
654
|
const { conn, id } = session;
|
|
548
|
-
const outcome = { text: "", usage: emptyUsage() };
|
|
655
|
+
const outcome = { text: "", recoveredCalls: [], usage: emptyUsage() };
|
|
549
656
|
const replyText = useTools ? new ReplyTextExtractor() : undefined;
|
|
550
|
-
//
|
|
657
|
+
// Any native tool call cancels the turn; `call_mcp_tool` targeting a known nexo action is recovered as a nexo ToolCall.
|
|
551
658
|
let nativeCall;
|
|
659
|
+
let cancelledForTool = false;
|
|
660
|
+
const seenCallIds = new Set();
|
|
552
661
|
let stalled = false;
|
|
553
662
|
let open;
|
|
554
663
|
let idle;
|
|
@@ -575,8 +684,20 @@ function streamAgyP(getConn, model, context, options) {
|
|
|
575
684
|
conn.listen(id, (u) => {
|
|
576
685
|
arm();
|
|
577
686
|
if (u.sessionUpdate === "tool_call") {
|
|
578
|
-
if (
|
|
579
|
-
|
|
687
|
+
if (typeof u.toolCallId === "string") {
|
|
688
|
+
if (seenCallIds.has(u.toolCallId))
|
|
689
|
+
return;
|
|
690
|
+
seenCallIds.add(u.toolCallId);
|
|
691
|
+
}
|
|
692
|
+
const recovered = useTools ? extractMcpToolCall(u, knownTools) : undefined;
|
|
693
|
+
if (recovered) {
|
|
694
|
+
outcome.recoveredCalls.push(recovered);
|
|
695
|
+
}
|
|
696
|
+
else if (!nativeCall) {
|
|
697
|
+
nativeCall = u.title ?? u.name ?? "tool";
|
|
698
|
+
}
|
|
699
|
+
if (!cancelledForTool) {
|
|
700
|
+
cancelledForTool = true;
|
|
580
701
|
cancel();
|
|
581
702
|
}
|
|
582
703
|
return;
|
|
@@ -623,8 +744,14 @@ function streamAgyP(getConn, model, context, options) {
|
|
|
623
744
|
outcome.aborted = true;
|
|
624
745
|
else if (stalled)
|
|
625
746
|
outcome.error = `no output for ${Math.round(idleMs / 1000)}s`;
|
|
626
|
-
else if (nativeCall)
|
|
747
|
+
else if (nativeCall) {
|
|
748
|
+
outcome.nativeCall = nativeCall;
|
|
627
749
|
outcome.error = `stopped: native tool call "${nativeCall}" (the nexo agent offers no tools)`;
|
|
750
|
+
}
|
|
751
|
+
else if (outcome.recoveredCalls.length > 0) {
|
|
752
|
+
if (!("e" in settled))
|
|
753
|
+
outcome.usage = toUsage(settled.r?.usage);
|
|
754
|
+
}
|
|
628
755
|
else if ("e" in settled) {
|
|
629
756
|
outcome.error = settled.e.message;
|
|
630
757
|
outcome.lost = conn.closed;
|
|
@@ -647,7 +774,7 @@ function streamAgyP(getConn, model, context, options) {
|
|
|
647
774
|
let session;
|
|
648
775
|
try {
|
|
649
776
|
stream.push({ type: "start", partial: snapshot() });
|
|
650
|
-
const prompt =
|
|
777
|
+
const prompt = agyTranscriptParts(context.messages);
|
|
651
778
|
const opened = await startSession();
|
|
652
779
|
if (opened === "aborted" || signal?.aborted)
|
|
653
780
|
return fail("aborted", "Request was aborted");
|
|
@@ -664,11 +791,22 @@ function streamAgyP(getConn, model, context, options) {
|
|
|
664
791
|
let usage = outcome.usage;
|
|
665
792
|
if (outcome.aborted || signal?.aborted)
|
|
666
793
|
return fail("aborted", "Request was aborted", usage);
|
|
794
|
+
// A native tool call: correct it once in the same session (it keeps the transcript and its cache) rather
|
|
795
|
+
// than failing back to the engine, which would resend the whole transcript to a fresh session.
|
|
796
|
+
if (useTools && outcome.nativeCall) {
|
|
797
|
+
outcome = await runOnce(session, nativeRetryNote(outcome.nativeCall), !partial.content.some((b) => b.type === "text"));
|
|
798
|
+
usage = addUsage(usage, outcome.usage);
|
|
799
|
+
if (outcome.aborted || signal?.aborted)
|
|
800
|
+
return fail("aborted", "Request was aborted", usage);
|
|
801
|
+
}
|
|
667
802
|
if (outcome.error)
|
|
668
803
|
return fail("error", `agy failed: ${outcome.error}`, usage);
|
|
669
804
|
let stopReason = outcome.stopReason === "max_tokens" ? "length" : "stop";
|
|
670
805
|
if (useTools) {
|
|
671
|
-
|
|
806
|
+
const fromOutcome = (o) => o.recoveredCalls.length > 0
|
|
807
|
+
? { text: looksLikeReplyAttempt(o.text) ? "" : o.text.trim(), calls: o.recoveredCalls }
|
|
808
|
+
: parseAgyReply(o.text, tools);
|
|
809
|
+
let reply = fromOutcome(outcome);
|
|
672
810
|
if (!reply) {
|
|
673
811
|
// Same session, so the model still has the transcript: only the correction is sent. Text the first
|
|
674
812
|
// attempt already streamed is not streamed again.
|
|
@@ -678,7 +816,7 @@ function streamAgyP(getConn, model, context, options) {
|
|
|
678
816
|
return fail("aborted", "Request was aborted", usage);
|
|
679
817
|
if (outcome.error)
|
|
680
818
|
return fail("error", `agy failed: ${outcome.error}`, usage);
|
|
681
|
-
reply =
|
|
819
|
+
reply = fromOutcome(outcome);
|
|
682
820
|
}
|
|
683
821
|
// Still not the format. A botched attempt at it (JSON naming an unknown action, a truncated object) is an
|
|
684
822
|
// error the engine retries; plain prose is kept.
|
|
@@ -726,14 +864,17 @@ function streamAgyP(getConn, model, context, options) {
|
|
|
726
864
|
/**
|
|
727
865
|
* Runs models through one long-lived Antigravity CLI ACP server (`agy acp`), authenticated by the CLI's own login.
|
|
728
866
|
* The server starts on the first call and is shared by every call after it, avoiding per-call language-server start-up.
|
|
729
|
-
* It runs a minimal custom agent with `tools: []` in a throwaway workspace
|
|
867
|
+
* It runs a minimal custom agent with `tools: []` in a throwaway workspace, against a mirror of the Antigravity data
|
|
868
|
+
* directory with no MCP servers (`mirrorGeminiDir`); each turn opens a fresh session carrying
|
|
730
869
|
* nexo's system prompt via `_meta.antigravity.systemPrompt.append`, sets the base model and `thought_level` from
|
|
731
|
-
* reasoning effort, and closes the session afterwards.
|
|
870
|
+
* reasoning effort, and closes the session afterwards. The transcript carries the latest turns' reasoning as <notes>
|
|
871
|
+
* (`agyTranscriptParts`). Any native tool call cancels the turn and is corrected once in the same session. Nexo tools are described
|
|
732
872
|
* in the prompt and parsed as `{ text, tool_calls }` (one correction retry when it does not parse); `text` streams as
|
|
733
873
|
* it is written. Token counts are reported from ACP usage while cost stays $0.
|
|
734
874
|
*/
|
|
735
875
|
export function agyPProvider(options = {}) {
|
|
736
876
|
const run = options.run ?? spawnRunner(options.command ?? process.env[AGY_P_COMMAND_ENV] ?? "agy");
|
|
877
|
+
const geminiDir = options.geminiDir === undefined ? join(homedir(), ".gemini") : options.geminiDir;
|
|
737
878
|
let current;
|
|
738
879
|
const getConn = () => {
|
|
739
880
|
if (current)
|
|
@@ -741,7 +882,7 @@ export function agyPProvider(options = {}) {
|
|
|
741
882
|
const opened = AcpConnection.open(run, () => {
|
|
742
883
|
if (current === opened)
|
|
743
884
|
current = undefined;
|
|
744
|
-
});
|
|
885
|
+
}, geminiDir);
|
|
745
886
|
current = opened;
|
|
746
887
|
opened.catch(() => {
|
|
747
888
|
if (current === opened)
|