@hexdecimal16/nexo 0.4.16 → 0.4.17

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.
@@ -152,7 +152,7 @@ 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 (such as `manage_task`), only list actions in `tool_calls`. 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`.",
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
156
  "",
157
157
  "Available actions, one JSON definition per line:",
158
158
  defs,
@@ -257,6 +257,40 @@ function rpcErrorMessage(error) {
257
257
  }
258
258
  return detail && !base.includes(detail) ? `${base}: ${detail}` : base;
259
259
  }
260
+ function parseObjectArgs(value) {
261
+ if (value === undefined || value === null)
262
+ return {};
263
+ let parsed = value;
264
+ if (typeof value === "string") {
265
+ try {
266
+ parsed = JSON.parse(value);
267
+ }
268
+ catch {
269
+ return undefined;
270
+ }
271
+ }
272
+ return typeof parsed === "object" && parsed !== null && !Array.isArray(parsed) ? parsed : undefined;
273
+ }
274
+ /**
275
+ * Extracts a nexo `ToolCall` when a model invokes Antigravity's residual `call_mcp_tool` targeting one of nexo's own
276
+ * declared actions (e.g. `ToolName: "bash"`). Returns undefined for any non-nexo tool or malformed arguments so real
277
+ * MCP tools and other native tools are always rejected.
278
+ */
279
+ export function extractMcpToolCall(update, knownTools) {
280
+ if (update.name !== "call_mcp_tool")
281
+ return undefined;
282
+ const raw = update.args ?? update.rawInput;
283
+ if (!raw || typeof raw !== "object" || Array.isArray(raw))
284
+ return undefined;
285
+ const obj = raw;
286
+ const toolName = typeof obj.ToolName === "string" ? obj.ToolName : typeof obj.toolName === "string" ? obj.toolName : undefined;
287
+ if (!toolName || !knownTools.has(toolName))
288
+ return undefined;
289
+ const args = parseObjectArgs(obj.Arguments ?? obj.arguments);
290
+ if (!args)
291
+ return undefined;
292
+ return { type: "toolCall", id: toolCallId(), name: toolName, arguments: args };
293
+ }
260
294
  const AGENT_FILE = [
261
295
  "---",
262
296
  `name: ${AGENT_NAME}`,
@@ -264,7 +298,7 @@ const AGENT_FILE = [
264
298
  "tools: []",
265
299
  "mainAgent: true",
266
300
  "---",
267
- "Follow the instructions provided for this session. Do not invoke any built-in tools.",
301
+ "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
302
  "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
303
  "",
270
304
  ].join("\n");
@@ -469,6 +503,7 @@ function streamAgyP(getConn, model, context, options) {
469
503
  return fail("aborted", "Request was aborted");
470
504
  const tools = getCurrentTools(context.messages);
471
505
  const useTools = tools.length > 0 && options?.toolChoice !== "none";
506
+ const knownTools = useTools ? new Set(tools.map((t) => t.name)) : new Set();
472
507
  const systemPrompt = [getCurrentSystemPrompt(context.messages), useTools ? agyToolsSection(tools) : ""].filter((s) => s.length > 0).join("\n\n");
473
508
  const idleMs = parseIdleMs(process.env[AGY_P_IDLE_ENV]);
474
509
  const closeSession = (session) => {
@@ -545,10 +580,12 @@ function streamAgyP(getConn, model, context, options) {
545
580
  ? [{ type: "text", text: input }]
546
581
  : input.map((p) => (p.type === "text" ? { type: "text", text: p.text } : { type: "image", data: p.data, mimeType: p.mimeType }));
547
582
  const { conn, id } = session;
548
- const outcome = { text: "", usage: emptyUsage() };
583
+ const outcome = { text: "", recoveredCalls: [], usage: emptyUsage() };
549
584
  const replyText = useTools ? new ReplyTextExtractor() : undefined;
550
- // A native tool call cancels the turn so the engine retries cleanly.
585
+ // Any native tool call cancels the turn; `call_mcp_tool` targeting a known nexo action is recovered as a nexo ToolCall.
551
586
  let nativeCall;
587
+ let cancelledForTool = false;
588
+ const seenCallIds = new Set();
552
589
  let stalled = false;
553
590
  let open;
554
591
  let idle;
@@ -575,8 +612,20 @@ function streamAgyP(getConn, model, context, options) {
575
612
  conn.listen(id, (u) => {
576
613
  arm();
577
614
  if (u.sessionUpdate === "tool_call") {
578
- if (!nativeCall) {
579
- nativeCall = u.title ?? "tool";
615
+ if (typeof u.toolCallId === "string") {
616
+ if (seenCallIds.has(u.toolCallId))
617
+ return;
618
+ seenCallIds.add(u.toolCallId);
619
+ }
620
+ const recovered = useTools ? extractMcpToolCall(u, knownTools) : undefined;
621
+ if (recovered) {
622
+ outcome.recoveredCalls.push(recovered);
623
+ }
624
+ else if (!nativeCall) {
625
+ nativeCall = u.title ?? u.name ?? "tool";
626
+ }
627
+ if (!cancelledForTool) {
628
+ cancelledForTool = true;
580
629
  cancel();
581
630
  }
582
631
  return;
@@ -625,6 +674,10 @@ function streamAgyP(getConn, model, context, options) {
625
674
  outcome.error = `no output for ${Math.round(idleMs / 1000)}s`;
626
675
  else if (nativeCall)
627
676
  outcome.error = `stopped: native tool call "${nativeCall}" (the nexo agent offers no tools)`;
677
+ else if (outcome.recoveredCalls.length > 0) {
678
+ if (!("e" in settled))
679
+ outcome.usage = toUsage(settled.r?.usage);
680
+ }
628
681
  else if ("e" in settled) {
629
682
  outcome.error = settled.e.message;
630
683
  outcome.lost = conn.closed;
@@ -668,7 +721,10 @@ function streamAgyP(getConn, model, context, options) {
668
721
  return fail("error", `agy failed: ${outcome.error}`, usage);
669
722
  let stopReason = outcome.stopReason === "max_tokens" ? "length" : "stop";
670
723
  if (useTools) {
671
- let reply = parseAgyReply(outcome.text, tools);
724
+ const fromOutcome = (o) => o.recoveredCalls.length > 0
725
+ ? { text: looksLikeReplyAttempt(o.text) ? "" : o.text.trim(), calls: o.recoveredCalls }
726
+ : parseAgyReply(o.text, tools);
727
+ let reply = fromOutcome(outcome);
672
728
  if (!reply) {
673
729
  // Same session, so the model still has the transcript: only the correction is sent. Text the first
674
730
  // attempt already streamed is not streamed again.
@@ -678,7 +734,7 @@ function streamAgyP(getConn, model, context, options) {
678
734
  return fail("aborted", "Request was aborted", usage);
679
735
  if (outcome.error)
680
736
  return fail("error", `agy failed: ${outcome.error}`, usage);
681
- reply = parseAgyReply(outcome.text, tools);
737
+ reply = fromOutcome(outcome);
682
738
  }
683
739
  // Still not the format. A botched attempt at it (JSON naming an unknown action, a truncated object) is an
684
740
  // error the engine retries; plain prose is kept.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hexdecimal16/nexo",
3
- "version": "0.4.16",
3
+ "version": "0.4.17",
4
4
  "description": "nexo agent engine",
5
5
  "license": "PolyForm-Noncommercial-1.0.0",
6
6
  "repository": {