@span-io/agent-link 0.1.2 → 0.1.4

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/index.js CHANGED
@@ -161,35 +161,47 @@ function handleControl(message) {
161
161
  }
162
162
  }
163
163
  function setupAgentPiping(agentId, proc) {
164
+ const rawFlushSize = Number.parseInt(process.env.AGENT_LINK_STREAM_FLUSH_CHARS ?? "16384", 10);
165
+ const flushSize = Number.isFinite(rawFlushSize) && rawFlushSize > 0 ? rawFlushSize : 16384;
164
166
  const setupStream = (stream, name) => {
165
167
  if (!stream)
166
168
  return;
167
169
  let buffer = "";
168
170
  stream.setEncoding("utf8");
171
+ const flushChunk = (chunk) => {
172
+ if (chunk.length > 0) {
173
+ transport.sendLog(agentId, name, chunk);
174
+ }
175
+ };
169
176
  stream.on("data", (chunk) => {
170
177
  buffer += chunk;
171
178
  let index = buffer.indexOf("\n");
172
179
  while (index >= 0) {
173
180
  const line = buffer.slice(0, index + 1);
174
181
  buffer = buffer.slice(index + 1);
175
- transport.sendLog(agentId, name, line);
182
+ flushChunk(line);
176
183
  index = buffer.indexOf("\n");
177
184
  }
185
+ // Prevent unbounded memory growth when tools stream without newlines.
186
+ while (buffer.length >= flushSize) {
187
+ const part = buffer.slice(0, flushSize);
188
+ buffer = buffer.slice(flushSize);
189
+ flushChunk(part);
190
+ }
178
191
  });
179
192
  stream.on("end", () => {
180
- if (buffer.length > 0) {
181
- transport.sendLog(agentId, name, buffer);
182
- }
193
+ flushChunk(buffer);
194
+ buffer = "";
183
195
  });
184
196
  };
185
197
  setupStream(proc.child.stdout, "stdout");
186
198
  setupStream(proc.child.stderr, "stderr");
187
- proc.child.on("exit", (code, signal) => {
199
+ proc.child.on("exit", (_code, _signal) => {
188
200
  transport.sendStatus(agentId, "exited");
189
201
  activeAgents.delete(agentId);
190
202
  });
191
203
  proc.child.on("error", (error) => {
192
- transport.sendLog(agentId, "stderr", `Process error: ${error.message}\n`);
204
+ transport.sendLog(agentId, "stderr", "Process error: " + error.message + "\n");
193
205
  transport.sendStatus(agentId, "error");
194
206
  activeAgents.delete(agentId);
195
207
  });
@@ -1,3 +1,4 @@
1
+ import { mkdirSync } from "fs";
1
2
  import { spawn } from "child_process";
2
3
  const DEFAULT_CODEX_ARGS = "exec --skip-git-repo-check";
3
4
  const DEFAULT_GEMINI_ARGS = "";
@@ -12,6 +13,50 @@ function shellQuote(value) {
12
13
  // Simple single-quote wrapping for display purposes
13
14
  return `'${value.replace(/'/g, `'"'"'`)}'`;
14
15
  }
16
+ function collectDirectoriesFromArgs(args) {
17
+ const directories = [];
18
+ for (let i = 0; i < args.length; i += 1) {
19
+ const arg = args[i];
20
+ const next = args[i + 1];
21
+ if ((arg === "--cd" || arg === "-C" || arg === "--add-dir" || arg === "--include-directories") && typeof next === "string") {
22
+ directories.push(next);
23
+ i += 1;
24
+ continue;
25
+ }
26
+ if (arg.startsWith("--cd=")) {
27
+ directories.push(arg.slice("--cd=".length));
28
+ continue;
29
+ }
30
+ if (arg.startsWith("--add-dir=")) {
31
+ directories.push(arg.slice("--add-dir=".length));
32
+ continue;
33
+ }
34
+ if (arg.startsWith("--include-directories=")) {
35
+ const raw = arg.slice("--include-directories=".length);
36
+ raw
37
+ .split(",")
38
+ .map((entry) => entry.trim())
39
+ .filter(Boolean)
40
+ .forEach((entry) => directories.push(entry));
41
+ continue;
42
+ }
43
+ }
44
+ return directories;
45
+ }
46
+ function ensureDirectoriesExist(rawDirs) {
47
+ const deduped = new Set(rawDirs
48
+ .map((entry) => entry.trim())
49
+ .filter((entry) => entry.length > 0));
50
+ for (const dir of deduped) {
51
+ try {
52
+ mkdirSync(dir, { recursive: true });
53
+ }
54
+ catch (error) {
55
+ const message = error instanceof Error ? error.message : String(error);
56
+ throw new Error(`Failed to create working directory '${dir}': ${message}`);
57
+ }
58
+ }
59
+ }
15
60
  export function buildCommand({ agent, prompt, optionsArgs = [], executablePath }, promptModeOverride) {
16
61
  // 1. Gemini Models
17
62
  if (agent.model.startsWith("gemini-")) {
@@ -34,7 +79,13 @@ export function buildCommand({ agent, prompt, optionsArgs = [], executablePath }
34
79
  // 2. Claude Models
35
80
  if (agent.model.startsWith("claude-")) {
36
81
  const command = executablePath ?? process.env.CLAUDE_BIN ?? "claude";
37
- const args = ["-p", prompt, "--model", agent.model];
82
+ const args = ["-p", prompt];
83
+ if (optionsArgs.length > 0) {
84
+ args.push(...optionsArgs);
85
+ }
86
+ if (!args.includes("--model") && !args.includes("-m")) {
87
+ args.push("--model", agent.model);
88
+ }
38
89
  return { command, args, promptMode: "args" };
39
90
  }
40
91
  // 3. Generic Codex/Other Models
@@ -81,11 +132,11 @@ export function spawnAgentProcess(config) {
81
132
  const { agent, optionsArgs = [] } = config;
82
133
  const startedAt = new Date().toISOString();
83
134
  const isCodexModel = !agent.model.startsWith("gemini-") && !agent.model.startsWith("claude-");
84
- const sanitizedOptions = agent.model.startsWith("claude-")
85
- ? []
86
- : optionsArgs;
135
+ const sanitizedOptions = optionsArgs;
87
136
  const spawnWithMode = (promptModeOverride) => {
88
137
  const { command, args, promptMode } = buildCommand({ ...config, optionsArgs: sanitizedOptions }, promptModeOverride);
138
+ // Ensure requested project/working directories exist before launching CLI.
139
+ ensureDirectoriesExist(collectDirectoriesFromArgs(args));
89
140
  const ttyMode = process.env.CODEX_TTY_MODE ?? DEFAULT_TTY_MODE;
90
141
  const hasExec = args.includes("exec");
91
142
  const useScriptWrapper = ttyMode === "script" || (ttyMode === "auto" && hasExec);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@span-io/agent-link",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "Secure bridge between Span (AI control plane) and local agent CLI tools.",
5
5
  "type": "module",
6
6
  "bin": {