@hexdecimal16/nexo 0.4.17 → 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.
@@ -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`. */
@@ -153,11 +153,77 @@ export function agyToolsSection(tools) {
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
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;
@@ -322,14 +388,17 @@ class AcpConnection {
322
388
  this.onClose = onClose;
323
389
  }
324
390
  /** Starts the server and completes the `initialize` handshake. */
325
- static async open(run, onClose) {
391
+ static async open(run, onClose, geminiDir) {
326
392
  registerExitHandler();
327
393
  // Throwaway workspace holding the no-tools custom agent under ./.agents/agents, with no project files in reach.
328
394
  const dir = await mkdtemp(join(tmpdir(), "nexo-agy-p-"));
329
395
  liveDirs.add(dir);
330
396
  await mkdir(join(dir, ".agents", "agents"), { recursive: true });
331
397
  await writeFile(join(dir, ".agents", "agents", `${AGENT_NAME}.md`), AGENT_FILE, "utf8");
332
- const proc = run({ args: ["acp", "--agent", AGENT_NAME, "--include-default-customizations=false"], cwd: dir });
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 });
333
402
  const conn = new AcpConnection(proc, dir, onClose);
334
403
  void conn.pump();
335
404
  void proc.exit.then((exit) => conn.shutdown(new Error(exit.stderr.trim().split("\n").at(-1) || `agy exited with code ${exit.code ?? "null"}`)));
@@ -457,6 +526,9 @@ function looksLikeReplyAttempt(text) {
457
526
  return /"tool_calls"\s*:/.test(text) || /^\s*(```|\{)/.test(text);
458
527
  }
459
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
+ }
460
532
  function raceAbort(promise, signal) {
461
533
  if (!signal)
462
534
  return promise;
@@ -672,8 +744,10 @@ function streamAgyP(getConn, model, context, options) {
672
744
  outcome.aborted = true;
673
745
  else if (stalled)
674
746
  outcome.error = `no output for ${Math.round(idleMs / 1000)}s`;
675
- else if (nativeCall)
747
+ else if (nativeCall) {
748
+ outcome.nativeCall = nativeCall;
676
749
  outcome.error = `stopped: native tool call "${nativeCall}" (the nexo agent offers no tools)`;
750
+ }
677
751
  else if (outcome.recoveredCalls.length > 0) {
678
752
  if (!("e" in settled))
679
753
  outcome.usage = toUsage(settled.r?.usage);
@@ -700,7 +774,7 @@ function streamAgyP(getConn, model, context, options) {
700
774
  let session;
701
775
  try {
702
776
  stream.push({ type: "start", partial: snapshot() });
703
- const prompt = renderTranscriptParts(context.messages);
777
+ const prompt = agyTranscriptParts(context.messages);
704
778
  const opened = await startSession();
705
779
  if (opened === "aborted" || signal?.aborted)
706
780
  return fail("aborted", "Request was aborted");
@@ -717,6 +791,14 @@ function streamAgyP(getConn, model, context, options) {
717
791
  let usage = outcome.usage;
718
792
  if (outcome.aborted || signal?.aborted)
719
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
+ }
720
802
  if (outcome.error)
721
803
  return fail("error", `agy failed: ${outcome.error}`, usage);
722
804
  let stopReason = outcome.stopReason === "max_tokens" ? "length" : "stop";
@@ -782,14 +864,17 @@ function streamAgyP(getConn, model, context, options) {
782
864
  /**
783
865
  * Runs models through one long-lived Antigravity CLI ACP server (`agy acp`), authenticated by the CLI's own login.
784
866
  * The server starts on the first call and is shared by every call after it, avoiding per-call language-server start-up.
785
- * It runs a minimal custom agent with `tools: []` in a throwaway workspace; each turn opens a fresh session carrying
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
786
869
  * nexo's system prompt via `_meta.antigravity.systemPrompt.append`, sets the base model and `thought_level` from
787
- * reasoning effort, and closes the session afterwards. Any native tool call cancels the turn. Nexo tools are described
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
788
872
  * in the prompt and parsed as `{ text, tool_calls }` (one correction retry when it does not parse); `text` streams as
789
873
  * it is written. Token counts are reported from ACP usage while cost stays $0.
790
874
  */
791
875
  export function agyPProvider(options = {}) {
792
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;
793
878
  let current;
794
879
  const getConn = () => {
795
880
  if (current)
@@ -797,7 +882,7 @@ export function agyPProvider(options = {}) {
797
882
  const opened = AcpConnection.open(run, () => {
798
883
  if (current === opened)
799
884
  current = undefined;
800
- });
885
+ }, geminiDir);
801
886
  current = opened;
802
887
  opened.catch(() => {
803
888
  if (current === opened)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hexdecimal16/nexo",
3
- "version": "0.4.17",
3
+ "version": "0.4.18",
4
4
  "description": "nexo agent engine",
5
5
  "license": "PolyForm-Noncommercial-1.0.0",
6
6
  "repository": {