@runappsai/agent 0.1.5 → 0.1.7

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/dist/agent.d.ts CHANGED
@@ -178,6 +178,7 @@ export declare class Agent {
178
178
  private callModel;
179
179
  private runBatch;
180
180
  private skipTool;
181
+ /** `parentId`: a call made from inside that tool call (ctx.callTool); its result goes back to the tool, not into the conversation. */
181
182
  private runTool;
182
183
  private permitted;
183
184
  /** After an abort, every open tool call gets a result so the transcript stays valid. */
package/dist/agent.js CHANGED
@@ -11,7 +11,7 @@ import { expandSlash } from "./templates.js";
11
11
  import { fileTools } from "./file-tools.js";
12
12
  import { validateSchema } from "./json-schema.js";
13
13
  import { sandboxTool } from "./sandbox.js";
14
- import { normalizeResult, textOf, toChatTool } from "./tool.js";
14
+ import { DEFAULT_MAX_RESULT_CHARS, normalizeResult, textOf, toChatTool, truncate } from "./tool.js";
15
15
  import { askUserTool, domUI } from "./dialogs.js";
16
16
  import { fileGuard } from "./file-guard.js";
17
17
  import { isMachineTitle, normalizePath, titleOf, UNTITLED } from "./workspace.js";
@@ -22,6 +22,8 @@ const MEMORY_PATH = "/memory/notes.md";
22
22
  const AGENTS_PATH = "/AGENTS.md";
23
23
  const MAX_NOTES_CHARS = 6000;
24
24
  const MAX_RETRIES = 3;
25
+ /** The longest result a tool called from another tool (ctx.callTool) hands back. */
26
+ const NESTED_RESULT_CHARS = 2_000_000;
25
27
  const LOOP_WARN = 3;
26
28
  const LOOP_STOP = 6;
27
29
  /**
@@ -118,6 +120,7 @@ function withoutMedia(messages, media, model) {
118
120
  const emptyUsage = () => ({ total_cost: 0, prompt_tokens: 0, completion_tokens: 0, calls: 0 });
119
121
  const abortedResult = () => ({ runId: "", reason: "aborted", text: "", usage: emptyUsage() });
120
122
  const SKIPPED_STOP = "Skipped: the user stopped the run.";
123
+ const FILE_TOOL_NAMES = new Set(["read_file", "list_files", "search_files", "write_file", "edit_file", "move_file", "copy_file", "delete_file"]);
121
124
  const balanceError = () => new ProviderError(402, "The reply was cut short because the balance could not cover a full answer; top up to continue", { code: "insufficient_balance" });
122
125
  /** Settles with `p`, or rejects as soon as `signal` aborts (the work itself may go on; its result is ignored). */
123
126
  function raceAbort(p, signal) {
@@ -170,7 +173,7 @@ export class Agent {
170
173
  this.ui = opts.ui ?? domUI();
171
174
  this.workspace = opts.workspace ?? null;
172
175
  const tools = [...(opts.tools ?? [])];
173
- // What the model has read, for the file tools and run_js alike; forgotten when the conversation is compacted or rewound.
176
+ // What the model has read, for the file tools; forgotten when the conversation is compacted or rewound.
174
177
  const guard = this.workspace ? fileGuard(this.workspace, { enabled: opts.readBeforeWrite !== false }) : undefined;
175
178
  if (guard) {
176
179
  const sync = () => guard.retain(this.filesInView());
@@ -184,7 +187,7 @@ export class Agent {
184
187
  if (opts.askUser !== false)
185
188
  tools.push(askUserTool());
186
189
  if (opts.sandbox !== false)
187
- tools.push(sandboxTool(this.workspace, { ...(typeof opts.sandbox === "object" ? opts.sandbox : {}), guard }));
190
+ tools.push(sandboxTool(typeof opts.sandbox === "object" ? opts.sandbox : {}));
188
191
  const seen = new Set();
189
192
  for (const t of tools) {
190
193
  if (seen.has(t.name))
@@ -793,7 +796,8 @@ export class Agent {
793
796
  appName: this.opts.appName,
794
797
  instructions: typeof this.opts.instructions === "function" ? this.opts.instructions() : this.opts.instructions,
795
798
  tools,
796
- workspaceDescription: ws ? ws.describe() : undefined,
799
+ // Without file tools the directory is only where the session is kept.
800
+ workspaceDescription: ws && tools.some((t) => FILE_TOOL_NAMES.has(t.name)) ? ws.describe() : undefined,
797
801
  skills: ws ? await ws.skills() : [],
798
802
  hasSandbox: tools.some((t) => t.name === "run_js"),
799
803
  hasAskUser: tools.some((t) => t.name === "ask_user"),
@@ -1077,18 +1081,22 @@ export class Agent {
1077
1081
  this.emit("tool_start", { runId, id, name, args: call.function.arguments });
1078
1082
  this.emit("tool_end", { runId, id, name, status: "skipped", result: why, durationMs: 0 });
1079
1083
  // A batch cut short by the user's new instruction: that is progress of its own.
1080
- return { message: { role: "tool", tool_call_id: id, content: why }, key: "", changed: false, progress: true };
1084
+ return { message: { role: "tool", tool_call_id: id, content: why }, key: "", changed: false, progress: true, status: "skipped" };
1081
1085
  }
1082
- async runTool(call, runId, signal, truncated) {
1086
+ /** `parentId`: a call made from inside that tool call (ctx.callTool); its result goes back to the tool, not into the conversation. */
1087
+ async runTool(call, runId, signal, truncated, parentId) {
1083
1088
  const id = call.id ?? "";
1089
+ const nested = parentId ? { parentId } : {};
1084
1090
  const name = call.function.name;
1085
1091
  const started = Date.now();
1086
1092
  let progress;
1087
1093
  let tool;
1088
1094
  const finish = (status, content, details) => {
1089
1095
  const result = textOf(content);
1090
- this.emit("tool_end", { runId, id, name, status, result, details, durationMs: Date.now() - started, ...(typeof content === "string" ? {} : { content }) });
1096
+ this.emit("tool_end", { runId, id, name, status, result: parentId ? truncate(result, DEFAULT_MAX_RESULT_CHARS).text : result, details, durationMs: Date.now() - started, ...nested, ...(typeof content === "string" ? {} : { content }) });
1091
1097
  return {
1098
+ status,
1099
+ details,
1092
1100
  message: { role: "tool", tool_call_id: id, content },
1093
1101
  // A read is judged by what it returned (another range of the same lines is nothing new); a change by what it was asked to do too.
1094
1102
  key: tool?.readOnly ? `${name}|${status}|${result.slice(0, 4000)}` : `${name}|${call.function.arguments.slice(0, 4000)}|${status}|${result.slice(0, 2000)}`,
@@ -1101,7 +1109,7 @@ export class Agent {
1101
1109
  return finish("skipped", SKIPPED_STOP);
1102
1110
  if (truncated) {
1103
1111
  // pi: nothing from a reply cut by the output limit runs.
1104
- this.emit("tool_start", { runId, id, name, args: call.function.arguments });
1112
+ this.emit("tool_start", { runId, id, name, args: call.function.arguments, ...nested });
1105
1113
  return finish("error", `Tool call "${name}" was not executed: the response hit the output token limit, so its arguments may be truncated. Re-issue the tool call with complete arguments.`);
1106
1114
  }
1107
1115
  const offered = await this.activeTools();
@@ -1111,10 +1119,10 @@ export class Agent {
1111
1119
  args = call.function.arguments.trim() ? JSON.parse(call.function.arguments) : {};
1112
1120
  }
1113
1121
  catch {
1114
- this.emit("tool_start", { runId, id, name, args: call.function.arguments });
1122
+ this.emit("tool_start", { runId, id, name, args: call.function.arguments, ...nested });
1115
1123
  return finish("error", "Error: the arguments are not valid JSON.");
1116
1124
  }
1117
- this.emit("tool_start", { runId, id, name, args });
1125
+ this.emit("tool_start", { runId, id, name, args, ...nested });
1118
1126
  if (!tool)
1119
1127
  return finish("error", `Error: there is no tool named "${name}". Available tools: ${offered.map((t) => t.name).join(", ")}.`);
1120
1128
  if (args === null || typeof args !== "object" || Array.isArray(args))
@@ -1157,13 +1165,14 @@ export class Agent {
1157
1165
  try {
1158
1166
  const own = tool.whenStopped?.(args);
1159
1167
  if (own !== undefined) {
1160
- const norm = normalizeResult(own, tool.maxResultChars);
1168
+ const norm = normalizeResult(own, parentId ? NESTED_RESULT_CHARS : tool.maxResultChars);
1161
1169
  return finish("skipped", norm.content, norm.details);
1162
1170
  }
1163
1171
  }
1164
1172
  catch { /* the default note */ }
1165
1173
  return finish("skipped", SKIPPED_STOP);
1166
1174
  };
1175
+ let inner = 0;
1167
1176
  try {
1168
1177
  const value = await raceAbort(Promise.resolve(tool.execute(args, {
1169
1178
  signal,
@@ -1171,12 +1180,17 @@ export class Agent {
1171
1180
  workspace: this.workspace,
1172
1181
  agent: this,
1173
1182
  callId: id,
1174
- update: (content) => this.emit("tool_update", { runId, id, name, content }),
1183
+ parentId,
1184
+ update: (content) => this.emit("tool_update", { runId, id, name, content, ...nested }),
1175
1185
  progress: (made) => { progress = made; },
1186
+ callTool: async (toolName, toolArgs, o) => {
1187
+ const out = await this.runTool({ id: `${id}.${++inner}`, type: "function", function: { name: toolName, arguments: JSON.stringify(toolArgs ?? {}) } }, runId, o?.signal ?? signal, false, id);
1188
+ return { status: out.status, text: textOf(out.message.content), details: out.details };
1189
+ },
1176
1190
  })), signal);
1177
1191
  if (signal.aborted)
1178
1192
  return stoppedIn();
1179
- const norm = normalizeResult(value, tool.maxResultChars);
1193
+ const norm = normalizeResult(value, parentId ? NESTED_RESULT_CHARS : tool.maxResultChars);
1180
1194
  return finish("ok", norm.content, norm.details);
1181
1195
  }
1182
1196
  catch (err) {