@samrahimi/smol-js 0.7.0 → 0.7.2

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.d.mts CHANGED
@@ -165,7 +165,7 @@ interface GenerateOptions {
165
165
  }
166
166
  interface YAMLAgentDefinition {
167
167
  name: string;
168
- type: 'ToolUseAgent' | 'CodeAgent';
168
+ type: 'ToolUseAgent' | 'CodeAgent' | 'TerminalAgent';
169
169
  description?: string;
170
170
  model?: YAMLModelDefinition;
171
171
  tools?: string[];
@@ -926,6 +926,78 @@ declare class ToolUseAgent extends Agent {
926
926
  addTool(tool: Tool): void;
927
927
  }
928
928
 
929
+ /**
930
+ * TerminalAgent - Accomplishes tasks by reasoning about and executing shell commands.
931
+ *
932
+ * Unlike CodeAgent (sandboxed JS VM) or ToolUseAgent (structured function calls),
933
+ * TerminalAgent operates by having the LLM emit shell commands in fenced ```sh
934
+ * blocks. Each block is executed on the user's real macOS terminal with full
935
+ * stdout/stderr streaming.
936
+ *
937
+ * Key behaviours:
938
+ * - Verbose logging is always on. The user sees reasoning, pending commands,
939
+ * and live output.
940
+ * - A 5-second delay fires before every command batch so the user can read
941
+ * what is about to run and abort with Ctrl+C.
942
+ * - User-assigned tools are stripped. The agent always has final_answer and
943
+ * any AgentTools (sub-agent delegation) that were wired in via YAML.
944
+ * - Commands run via child_process with stdio piped so output is captured
945
+ * and streamed back to the agent as observations.
946
+ */
947
+
948
+ interface TerminalAgentConfig extends AgentConfig {
949
+ /**
950
+ * Seconds to wait before executing each command batch.
951
+ * Gives the user time to read pending commands and Ctrl+C.
952
+ * Default: 5
953
+ */
954
+ commandDelay?: number;
955
+ /**
956
+ * Maximum characters of command output to feed back to the LLM per batch.
957
+ * Very long outputs are truncated (head + tail preserved) to keep context
958
+ * manageable. Default: 8000
959
+ */
960
+ maxOutputLength?: number;
961
+ }
962
+ declare class TerminalAgent extends Agent {
963
+ private readonly commandDelay;
964
+ private readonly maxOutputLength;
965
+ constructor(config: TerminalAgentConfig);
966
+ /**
967
+ * Build the system prompt. Includes delegation info if sub-agents are present.
968
+ */
969
+ protected initializeSystemPrompt(): string;
970
+ /**
971
+ * Execute one step of the ReAct loop:
972
+ * 1. Send messages to LLM (with tool defs for final_answer / delegation)
973
+ * 2. Extract reasoning and ```sh blocks from the response
974
+ * 3. If tool calls present (final_answer or delegation), process them
975
+ * 4. Otherwise execute shell commands with the pre-execution delay
976
+ * 5. Feed stdout/stderr/exit-code back as observation
977
+ */
978
+ protected executeStep(memoryStep: ActionStep): Promise<ActionOutput>;
979
+ /**
980
+ * Run a single shell command, capture stdout/stderr, return structured result.
981
+ */
982
+ private runCommand;
983
+ /**
984
+ * Truncate long output, preserving head and tail so context stays useful.
985
+ */
986
+ private truncateOutput;
987
+ /**
988
+ * Generate response, with streaming if available.
989
+ */
990
+ private generateResponse;
991
+ /**
992
+ * Process tool calls (final_answer or AgentTool delegation).
993
+ */
994
+ private processToolCalls;
995
+ /**
996
+ * Override: force final answer via tool call format when max steps hit.
997
+ */
998
+ protected provideFinalAnswer(task: string): Promise<unknown>;
999
+ }
1000
+
929
1001
  /**
930
1002
  * OpenAI-compatible Model implementation
931
1003
  *
@@ -1191,27 +1263,31 @@ declare class ExaResearchTool extends Tool {
1191
1263
 
1192
1264
  /**
1193
1265
  * ProxyTool - Bridges the smol-js agent runtime to an external standalone tool
1194
- * executed in an isolated Bun process.
1266
+ * executed in an isolated Bun process via the toolHarness adapter.
1267
+ *
1268
+ * The tool file is never imported into the main Node.js process. When an agent
1269
+ * invokes this proxy, it spawns:
1270
+ *
1271
+ * bun run <toolHarness.ts> <toolPath> <argsJson>
1195
1272
  *
1196
- * The tool file is never imported into the main Node.js process. Instead, when
1197
- * an agent invokes this proxy, it spawns `bun run <toolPath>` and communicates
1198
- * via a lightweight stdin/stdout JSON protocol.
1273
+ * The harness is the ONLY place that speaks the stdout protocol. The tool
1274
+ * itself simply exports { TOOL_METADATA, execute } and knows nothing about
1275
+ * how it is being called.
1199
1276
  *
1200
- * Protocol (stdout of child):
1201
- * - Lines prefixed with `[TOOL_OUTPUT]` are streaming log lines from the tool.
1277
+ * Protocol (stdout of harness/child):
1202
1278
  * - A line prefixed with `[TOOL_RESULT]` contains the JSON-serialized return value.
1203
1279
  * - A line prefixed with `[TOOL_ERROR]` means the tool threw; payload is the message.
1204
- * - Any other stdout line is treated as a streaming log line.
1280
+ * - Any other stdout line (e.g. console.log inside execute()) is streaming output.
1205
1281
  *
1206
- * Extensibility note: The transport (child_process + stdio) is encapsulated here.
1207
- * A future subclass or factory could swap this for HTTP, gRPC, or IPC with no change
1208
- * to the ProxyTool interface visible to agents or YAML definitions.
1282
+ * Extensibility: The spawn+stdio transport is fully encapsulated here. A future
1283
+ * variant (HTTP, gRPC, IPC) only needs to replace execute() the tool file and
1284
+ * YAML definition stay identical.
1209
1285
  */
1210
1286
 
1211
1287
  interface ProxyToolConfig {
1212
1288
  /** Absolute path to the .ts or .js tool file */
1213
1289
  toolPath: string;
1214
- /** Tool name (must match the class name inside the file) */
1290
+ /** Tool name (must match the file's base name) */
1215
1291
  name: string;
1216
1292
  /** Human-readable description exposed to agents */
1217
1293
  description: string;
@@ -1230,14 +1306,16 @@ declare class ProxyTool extends Tool {
1230
1306
  private readonly toolPath;
1231
1307
  private readonly timeout;
1232
1308
  private bunPath;
1309
+ private harnessPath;
1233
1310
  constructor(config: ProxyToolConfig);
1234
1311
  /**
1235
- * Ensure Bun is available before first invocation.
1312
+ * Ensure Bun is available and locate the harness before first invocation.
1236
1313
  */
1237
1314
  setup(): Promise<void>;
1238
1315
  /**
1239
- * Spawn the tool in a Bun child process, pass serialized args via CLI,
1240
- * stream stdout back as log lines, and parse the final result.
1316
+ * Spawn the harness in a Bun child process. The harness imports the tool,
1317
+ * calls execute(args), and writes the protocol lines. Any console.log from
1318
+ * the tool flows through stdout as plain lines.
1241
1319
  */
1242
1320
  execute(args: Record<string, unknown>): Promise<unknown>;
1243
1321
  private processLine;
@@ -1508,4 +1586,4 @@ declare class Orchestrator {
1508
1586
  getRunId(): string | undefined;
1509
1587
  }
1510
1588
 
1511
- export { type ActionOutput, type ActionStep, Agent, type AgentConfig, type AgentConfig$1 as AgentConfigType, AgentLogger, AgentMemory, AgentTool, type AgentToolConfig, type ChatMessage, CodeAgent, type CodeAgentConfig, type CodeExecutionOutput, CurlTool, type CustomToolMetadata, type DiscoveredTool, ExaGetContentsTool, ExaResearchTool, ExaSearchTool, type ExecutorConfig, FINAL_ANSWER_PROMPT, type FinalAnswerStep, FinalAnswerTool, type GenerateOptions, type JSONAgentEndEvent, type JSONAgentObservationEvent, type JSONAgentStartEvent, type JSONAgentStepEvent, type JSONAgentThinkingEvent, type JSONAgentToolCallEvent, type JSONAgentToolResultEvent, type JSONErrorEvent, type JSONEvent, type JSONEventBase, type JSONEventType, type JSONLogEvent, type JSONOutputConfig, JSONOutputHandler, type JSONRunEndEvent, type JSONRunStartEvent, type JSONWorkflowLoadedEvent, type LoadedWorkflow, LocalExecutor, LogLevel, type MemoryStep, type MemoryStrategy, type MessageRole, Model, type ModelConfig, OpenAIModel, type OpenAIModelConfig, type OpenAIToolDefinition, Orchestrator, type OrchestratorConfig, type OrchestratorEvent, type OutputFormat, type PromptVariables, ProxyTool, type ProxyToolConfig, ReadFileTool, type RunResult, type StreamEvent, type SystemPromptStep, type TaskStep, type Timing, type TokenUsage, Tool, type ToolCall, type ToolCallResult, type ToolInput, type ToolInputType, type ToolInputs, ToolUseAgent, type ToolUseAgentConfig, type ToolUsePromptVariables, UserInputTool, WriteFileTool, type YAMLAgentDefinition, YAMLLoader, type YAMLModelDefinition, type YAMLToolDefinition, type YAMLWorkflowDefinition, agentAsTool, createTool, finalAnswerTool, formatToolDescriptions, generateSystemPrompt, generateToolUseSystemPrompt, getErrorRecoveryPrompt, loadCustomTools, scanCustomTools };
1589
+ export { type ActionOutput, type ActionStep, Agent, type AgentConfig, type AgentConfig$1 as AgentConfigType, AgentLogger, AgentMemory, AgentTool, type AgentToolConfig, type ChatMessage, CodeAgent, type CodeAgentConfig, type CodeExecutionOutput, CurlTool, type CustomToolMetadata, type DiscoveredTool, ExaGetContentsTool, ExaResearchTool, ExaSearchTool, type ExecutorConfig, FINAL_ANSWER_PROMPT, type FinalAnswerStep, FinalAnswerTool, type GenerateOptions, type JSONAgentEndEvent, type JSONAgentObservationEvent, type JSONAgentStartEvent, type JSONAgentStepEvent, type JSONAgentThinkingEvent, type JSONAgentToolCallEvent, type JSONAgentToolResultEvent, type JSONErrorEvent, type JSONEvent, type JSONEventBase, type JSONEventType, type JSONLogEvent, type JSONOutputConfig, JSONOutputHandler, type JSONRunEndEvent, type JSONRunStartEvent, type JSONWorkflowLoadedEvent, type LoadedWorkflow, LocalExecutor, LogLevel, type MemoryStep, type MemoryStrategy, type MessageRole, Model, type ModelConfig, OpenAIModel, type OpenAIModelConfig, type OpenAIToolDefinition, Orchestrator, type OrchestratorConfig, type OrchestratorEvent, type OutputFormat, type PromptVariables, ProxyTool, type ProxyToolConfig, ReadFileTool, type RunResult, type StreamEvent, type SystemPromptStep, type TaskStep, TerminalAgent, type TerminalAgentConfig, type Timing, type TokenUsage, Tool, type ToolCall, type ToolCallResult, type ToolInput, type ToolInputType, type ToolInputs, ToolUseAgent, type ToolUseAgentConfig, type ToolUsePromptVariables, UserInputTool, WriteFileTool, type YAMLAgentDefinition, YAMLLoader, type YAMLModelDefinition, type YAMLToolDefinition, type YAMLWorkflowDefinition, agentAsTool, createTool, finalAnswerTool, formatToolDescriptions, generateSystemPrompt, generateToolUseSystemPrompt, getErrorRecoveryPrompt, loadCustomTools, scanCustomTools };
package/dist/index.d.ts CHANGED
@@ -165,7 +165,7 @@ interface GenerateOptions {
165
165
  }
166
166
  interface YAMLAgentDefinition {
167
167
  name: string;
168
- type: 'ToolUseAgent' | 'CodeAgent';
168
+ type: 'ToolUseAgent' | 'CodeAgent' | 'TerminalAgent';
169
169
  description?: string;
170
170
  model?: YAMLModelDefinition;
171
171
  tools?: string[];
@@ -926,6 +926,78 @@ declare class ToolUseAgent extends Agent {
926
926
  addTool(tool: Tool): void;
927
927
  }
928
928
 
929
+ /**
930
+ * TerminalAgent - Accomplishes tasks by reasoning about and executing shell commands.
931
+ *
932
+ * Unlike CodeAgent (sandboxed JS VM) or ToolUseAgent (structured function calls),
933
+ * TerminalAgent operates by having the LLM emit shell commands in fenced ```sh
934
+ * blocks. Each block is executed on the user's real macOS terminal with full
935
+ * stdout/stderr streaming.
936
+ *
937
+ * Key behaviours:
938
+ * - Verbose logging is always on. The user sees reasoning, pending commands,
939
+ * and live output.
940
+ * - A 5-second delay fires before every command batch so the user can read
941
+ * what is about to run and abort with Ctrl+C.
942
+ * - User-assigned tools are stripped. The agent always has final_answer and
943
+ * any AgentTools (sub-agent delegation) that were wired in via YAML.
944
+ * - Commands run via child_process with stdio piped so output is captured
945
+ * and streamed back to the agent as observations.
946
+ */
947
+
948
+ interface TerminalAgentConfig extends AgentConfig {
949
+ /**
950
+ * Seconds to wait before executing each command batch.
951
+ * Gives the user time to read pending commands and Ctrl+C.
952
+ * Default: 5
953
+ */
954
+ commandDelay?: number;
955
+ /**
956
+ * Maximum characters of command output to feed back to the LLM per batch.
957
+ * Very long outputs are truncated (head + tail preserved) to keep context
958
+ * manageable. Default: 8000
959
+ */
960
+ maxOutputLength?: number;
961
+ }
962
+ declare class TerminalAgent extends Agent {
963
+ private readonly commandDelay;
964
+ private readonly maxOutputLength;
965
+ constructor(config: TerminalAgentConfig);
966
+ /**
967
+ * Build the system prompt. Includes delegation info if sub-agents are present.
968
+ */
969
+ protected initializeSystemPrompt(): string;
970
+ /**
971
+ * Execute one step of the ReAct loop:
972
+ * 1. Send messages to LLM (with tool defs for final_answer / delegation)
973
+ * 2. Extract reasoning and ```sh blocks from the response
974
+ * 3. If tool calls present (final_answer or delegation), process them
975
+ * 4. Otherwise execute shell commands with the pre-execution delay
976
+ * 5. Feed stdout/stderr/exit-code back as observation
977
+ */
978
+ protected executeStep(memoryStep: ActionStep): Promise<ActionOutput>;
979
+ /**
980
+ * Run a single shell command, capture stdout/stderr, return structured result.
981
+ */
982
+ private runCommand;
983
+ /**
984
+ * Truncate long output, preserving head and tail so context stays useful.
985
+ */
986
+ private truncateOutput;
987
+ /**
988
+ * Generate response, with streaming if available.
989
+ */
990
+ private generateResponse;
991
+ /**
992
+ * Process tool calls (final_answer or AgentTool delegation).
993
+ */
994
+ private processToolCalls;
995
+ /**
996
+ * Override: force final answer via tool call format when max steps hit.
997
+ */
998
+ protected provideFinalAnswer(task: string): Promise<unknown>;
999
+ }
1000
+
929
1001
  /**
930
1002
  * OpenAI-compatible Model implementation
931
1003
  *
@@ -1191,27 +1263,31 @@ declare class ExaResearchTool extends Tool {
1191
1263
 
1192
1264
  /**
1193
1265
  * ProxyTool - Bridges the smol-js agent runtime to an external standalone tool
1194
- * executed in an isolated Bun process.
1266
+ * executed in an isolated Bun process via the toolHarness adapter.
1267
+ *
1268
+ * The tool file is never imported into the main Node.js process. When an agent
1269
+ * invokes this proxy, it spawns:
1270
+ *
1271
+ * bun run <toolHarness.ts> <toolPath> <argsJson>
1195
1272
  *
1196
- * The tool file is never imported into the main Node.js process. Instead, when
1197
- * an agent invokes this proxy, it spawns `bun run <toolPath>` and communicates
1198
- * via a lightweight stdin/stdout JSON protocol.
1273
+ * The harness is the ONLY place that speaks the stdout protocol. The tool
1274
+ * itself simply exports { TOOL_METADATA, execute } and knows nothing about
1275
+ * how it is being called.
1199
1276
  *
1200
- * Protocol (stdout of child):
1201
- * - Lines prefixed with `[TOOL_OUTPUT]` are streaming log lines from the tool.
1277
+ * Protocol (stdout of harness/child):
1202
1278
  * - A line prefixed with `[TOOL_RESULT]` contains the JSON-serialized return value.
1203
1279
  * - A line prefixed with `[TOOL_ERROR]` means the tool threw; payload is the message.
1204
- * - Any other stdout line is treated as a streaming log line.
1280
+ * - Any other stdout line (e.g. console.log inside execute()) is streaming output.
1205
1281
  *
1206
- * Extensibility note: The transport (child_process + stdio) is encapsulated here.
1207
- * A future subclass or factory could swap this for HTTP, gRPC, or IPC with no change
1208
- * to the ProxyTool interface visible to agents or YAML definitions.
1282
+ * Extensibility: The spawn+stdio transport is fully encapsulated here. A future
1283
+ * variant (HTTP, gRPC, IPC) only needs to replace execute() the tool file and
1284
+ * YAML definition stay identical.
1209
1285
  */
1210
1286
 
1211
1287
  interface ProxyToolConfig {
1212
1288
  /** Absolute path to the .ts or .js tool file */
1213
1289
  toolPath: string;
1214
- /** Tool name (must match the class name inside the file) */
1290
+ /** Tool name (must match the file's base name) */
1215
1291
  name: string;
1216
1292
  /** Human-readable description exposed to agents */
1217
1293
  description: string;
@@ -1230,14 +1306,16 @@ declare class ProxyTool extends Tool {
1230
1306
  private readonly toolPath;
1231
1307
  private readonly timeout;
1232
1308
  private bunPath;
1309
+ private harnessPath;
1233
1310
  constructor(config: ProxyToolConfig);
1234
1311
  /**
1235
- * Ensure Bun is available before first invocation.
1312
+ * Ensure Bun is available and locate the harness before first invocation.
1236
1313
  */
1237
1314
  setup(): Promise<void>;
1238
1315
  /**
1239
- * Spawn the tool in a Bun child process, pass serialized args via CLI,
1240
- * stream stdout back as log lines, and parse the final result.
1316
+ * Spawn the harness in a Bun child process. The harness imports the tool,
1317
+ * calls execute(args), and writes the protocol lines. Any console.log from
1318
+ * the tool flows through stdout as plain lines.
1241
1319
  */
1242
1320
  execute(args: Record<string, unknown>): Promise<unknown>;
1243
1321
  private processLine;
@@ -1508,4 +1586,4 @@ declare class Orchestrator {
1508
1586
  getRunId(): string | undefined;
1509
1587
  }
1510
1588
 
1511
- export { type ActionOutput, type ActionStep, Agent, type AgentConfig, type AgentConfig$1 as AgentConfigType, AgentLogger, AgentMemory, AgentTool, type AgentToolConfig, type ChatMessage, CodeAgent, type CodeAgentConfig, type CodeExecutionOutput, CurlTool, type CustomToolMetadata, type DiscoveredTool, ExaGetContentsTool, ExaResearchTool, ExaSearchTool, type ExecutorConfig, FINAL_ANSWER_PROMPT, type FinalAnswerStep, FinalAnswerTool, type GenerateOptions, type JSONAgentEndEvent, type JSONAgentObservationEvent, type JSONAgentStartEvent, type JSONAgentStepEvent, type JSONAgentThinkingEvent, type JSONAgentToolCallEvent, type JSONAgentToolResultEvent, type JSONErrorEvent, type JSONEvent, type JSONEventBase, type JSONEventType, type JSONLogEvent, type JSONOutputConfig, JSONOutputHandler, type JSONRunEndEvent, type JSONRunStartEvent, type JSONWorkflowLoadedEvent, type LoadedWorkflow, LocalExecutor, LogLevel, type MemoryStep, type MemoryStrategy, type MessageRole, Model, type ModelConfig, OpenAIModel, type OpenAIModelConfig, type OpenAIToolDefinition, Orchestrator, type OrchestratorConfig, type OrchestratorEvent, type OutputFormat, type PromptVariables, ProxyTool, type ProxyToolConfig, ReadFileTool, type RunResult, type StreamEvent, type SystemPromptStep, type TaskStep, type Timing, type TokenUsage, Tool, type ToolCall, type ToolCallResult, type ToolInput, type ToolInputType, type ToolInputs, ToolUseAgent, type ToolUseAgentConfig, type ToolUsePromptVariables, UserInputTool, WriteFileTool, type YAMLAgentDefinition, YAMLLoader, type YAMLModelDefinition, type YAMLToolDefinition, type YAMLWorkflowDefinition, agentAsTool, createTool, finalAnswerTool, formatToolDescriptions, generateSystemPrompt, generateToolUseSystemPrompt, getErrorRecoveryPrompt, loadCustomTools, scanCustomTools };
1589
+ export { type ActionOutput, type ActionStep, Agent, type AgentConfig, type AgentConfig$1 as AgentConfigType, AgentLogger, AgentMemory, AgentTool, type AgentToolConfig, type ChatMessage, CodeAgent, type CodeAgentConfig, type CodeExecutionOutput, CurlTool, type CustomToolMetadata, type DiscoveredTool, ExaGetContentsTool, ExaResearchTool, ExaSearchTool, type ExecutorConfig, FINAL_ANSWER_PROMPT, type FinalAnswerStep, FinalAnswerTool, type GenerateOptions, type JSONAgentEndEvent, type JSONAgentObservationEvent, type JSONAgentStartEvent, type JSONAgentStepEvent, type JSONAgentThinkingEvent, type JSONAgentToolCallEvent, type JSONAgentToolResultEvent, type JSONErrorEvent, type JSONEvent, type JSONEventBase, type JSONEventType, type JSONLogEvent, type JSONOutputConfig, JSONOutputHandler, type JSONRunEndEvent, type JSONRunStartEvent, type JSONWorkflowLoadedEvent, type LoadedWorkflow, LocalExecutor, LogLevel, type MemoryStep, type MemoryStrategy, type MessageRole, Model, type ModelConfig, OpenAIModel, type OpenAIModelConfig, type OpenAIToolDefinition, Orchestrator, type OrchestratorConfig, type OrchestratorEvent, type OutputFormat, type PromptVariables, ProxyTool, type ProxyToolConfig, ReadFileTool, type RunResult, type StreamEvent, type SystemPromptStep, type TaskStep, TerminalAgent, type TerminalAgentConfig, type Timing, type TokenUsage, Tool, type ToolCall, type ToolCallResult, type ToolInput, type ToolInputType, type ToolInputs, ToolUseAgent, type ToolUseAgentConfig, type ToolUsePromptVariables, UserInputTool, WriteFileTool, type YAMLAgentDefinition, YAMLLoader, type YAMLModelDefinition, type YAMLToolDefinition, type YAMLWorkflowDefinition, agentAsTool, createTool, finalAnswerTool, formatToolDescriptions, generateSystemPrompt, generateToolUseSystemPrompt, getErrorRecoveryPrompt, loadCustomTools, scanCustomTools };