@samrahimi/smol-js 0.1.0 → 0.2.0

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
@@ -767,6 +767,65 @@ declare class UserInputTool extends Tool {
767
767
  }
768
768
  declare const finalAnswerTool: FinalAnswerTool;
769
769
 
770
+ /**
771
+ * AgentTool - Wraps a CodeAgent as a Tool for use by other agents
772
+ *
773
+ * This enables nested/hierarchical agent architectures where a "manager" agent
774
+ * can delegate tasks to specialized "worker" agents.
775
+ *
776
+ * Based on the ManagedAgent pattern from Python smolagents.
777
+ */
778
+
779
+ interface AgentToolConfig {
780
+ /**
781
+ * The agent to wrap as a tool
782
+ */
783
+ agent: Agent;
784
+ /**
785
+ * Name for the tool (defaults to agent's name or "managed_agent")
786
+ */
787
+ name?: string;
788
+ /**
789
+ * Description of what this agent does (used in parent agent's prompt)
790
+ */
791
+ description?: string;
792
+ /**
793
+ * Additional context to provide to the agent with each task
794
+ */
795
+ additionalContext?: string;
796
+ /**
797
+ * Whether to provide the full result object or just the output
798
+ * @default false
799
+ */
800
+ returnFullResult?: boolean;
801
+ }
802
+ declare class AgentTool extends Tool {
803
+ readonly name: string;
804
+ readonly description: string;
805
+ readonly inputs: ToolInputs;
806
+ readonly outputType = "string";
807
+ private agent;
808
+ private additionalContext?;
809
+ private returnFullResult;
810
+ constructor(config: AgentToolConfig);
811
+ /**
812
+ * Generate a default description based on the agent's configuration
813
+ */
814
+ private generateDescription;
815
+ /**
816
+ * Execute the agent with the given task
817
+ */
818
+ execute(args: Record<string, unknown>): Promise<unknown>;
819
+ /**
820
+ * Override toCodePrompt to provide a cleaner signature for nested agents
821
+ */
822
+ toCodePrompt(): string;
823
+ }
824
+ /**
825
+ * Helper function to quickly wrap an agent as a tool
826
+ */
827
+ declare function agentAsTool(agent: Agent, options?: Omit<AgentToolConfig, 'agent'>): AgentTool;
828
+
770
829
  /**
771
830
  * System prompts for CodeAgent
772
831
  *
@@ -790,4 +849,4 @@ declare const FINAL_ANSWER_PROMPT = "Based on the steps you've taken so far, pro
790
849
  */
791
850
  declare function getErrorRecoveryPrompt(error: string): string;
792
851
 
793
- export { type ActionOutput, type ActionStep, Agent, type AgentConfig, type AgentConfig$1 as AgentConfigType, AgentLogger, AgentMemory, type ChatMessage, CodeAgent, type CodeAgentConfig, type CodeExecutionOutput, type ExecutorConfig, FINAL_ANSWER_PROMPT, type FinalAnswerStep, FinalAnswerTool, type GenerateOptions, LocalExecutor, LogLevel, type MemoryStep, type MessageRole, Model, type ModelConfig, OpenAIModel, type OpenAIModelConfig, type PromptVariables, type RunResult, type StreamEvent, type SystemPromptStep, type TaskStep, type Timing, type TokenUsage, Tool, type ToolCall, type ToolInput, type ToolInputType, type ToolInputs, UserInputTool, createTool, finalAnswerTool, generateSystemPrompt, getErrorRecoveryPrompt };
852
+ 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, type ExecutorConfig, FINAL_ANSWER_PROMPT, type FinalAnswerStep, FinalAnswerTool, type GenerateOptions, LocalExecutor, LogLevel, type MemoryStep, type MessageRole, Model, type ModelConfig, OpenAIModel, type OpenAIModelConfig, type PromptVariables, type RunResult, type StreamEvent, type SystemPromptStep, type TaskStep, type Timing, type TokenUsage, Tool, type ToolCall, type ToolInput, type ToolInputType, type ToolInputs, UserInputTool, agentAsTool, createTool, finalAnswerTool, generateSystemPrompt, getErrorRecoveryPrompt };
package/dist/index.d.ts CHANGED
@@ -767,6 +767,65 @@ declare class UserInputTool extends Tool {
767
767
  }
768
768
  declare const finalAnswerTool: FinalAnswerTool;
769
769
 
770
+ /**
771
+ * AgentTool - Wraps a CodeAgent as a Tool for use by other agents
772
+ *
773
+ * This enables nested/hierarchical agent architectures where a "manager" agent
774
+ * can delegate tasks to specialized "worker" agents.
775
+ *
776
+ * Based on the ManagedAgent pattern from Python smolagents.
777
+ */
778
+
779
+ interface AgentToolConfig {
780
+ /**
781
+ * The agent to wrap as a tool
782
+ */
783
+ agent: Agent;
784
+ /**
785
+ * Name for the tool (defaults to agent's name or "managed_agent")
786
+ */
787
+ name?: string;
788
+ /**
789
+ * Description of what this agent does (used in parent agent's prompt)
790
+ */
791
+ description?: string;
792
+ /**
793
+ * Additional context to provide to the agent with each task
794
+ */
795
+ additionalContext?: string;
796
+ /**
797
+ * Whether to provide the full result object or just the output
798
+ * @default false
799
+ */
800
+ returnFullResult?: boolean;
801
+ }
802
+ declare class AgentTool extends Tool {
803
+ readonly name: string;
804
+ readonly description: string;
805
+ readonly inputs: ToolInputs;
806
+ readonly outputType = "string";
807
+ private agent;
808
+ private additionalContext?;
809
+ private returnFullResult;
810
+ constructor(config: AgentToolConfig);
811
+ /**
812
+ * Generate a default description based on the agent's configuration
813
+ */
814
+ private generateDescription;
815
+ /**
816
+ * Execute the agent with the given task
817
+ */
818
+ execute(args: Record<string, unknown>): Promise<unknown>;
819
+ /**
820
+ * Override toCodePrompt to provide a cleaner signature for nested agents
821
+ */
822
+ toCodePrompt(): string;
823
+ }
824
+ /**
825
+ * Helper function to quickly wrap an agent as a tool
826
+ */
827
+ declare function agentAsTool(agent: Agent, options?: Omit<AgentToolConfig, 'agent'>): AgentTool;
828
+
770
829
  /**
771
830
  * System prompts for CodeAgent
772
831
  *
@@ -790,4 +849,4 @@ declare const FINAL_ANSWER_PROMPT = "Based on the steps you've taken so far, pro
790
849
  */
791
850
  declare function getErrorRecoveryPrompt(error: string): string;
792
851
 
793
- export { type ActionOutput, type ActionStep, Agent, type AgentConfig, type AgentConfig$1 as AgentConfigType, AgentLogger, AgentMemory, type ChatMessage, CodeAgent, type CodeAgentConfig, type CodeExecutionOutput, type ExecutorConfig, FINAL_ANSWER_PROMPT, type FinalAnswerStep, FinalAnswerTool, type GenerateOptions, LocalExecutor, LogLevel, type MemoryStep, type MessageRole, Model, type ModelConfig, OpenAIModel, type OpenAIModelConfig, type PromptVariables, type RunResult, type StreamEvent, type SystemPromptStep, type TaskStep, type Timing, type TokenUsage, Tool, type ToolCall, type ToolInput, type ToolInputType, type ToolInputs, UserInputTool, createTool, finalAnswerTool, generateSystemPrompt, getErrorRecoveryPrompt };
852
+ 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, type ExecutorConfig, FINAL_ANSWER_PROMPT, type FinalAnswerStep, FinalAnswerTool, type GenerateOptions, LocalExecutor, LogLevel, type MemoryStep, type MessageRole, Model, type ModelConfig, OpenAIModel, type OpenAIModelConfig, type PromptVariables, type RunResult, type StreamEvent, type SystemPromptStep, type TaskStep, type Timing, type TokenUsage, Tool, type ToolCall, type ToolInput, type ToolInputType, type ToolInputs, UserInputTool, agentAsTool, createTool, finalAnswerTool, generateSystemPrompt, getErrorRecoveryPrompt };
package/dist/index.js CHANGED
@@ -33,6 +33,7 @@ __export(index_exports, {
33
33
  Agent: () => Agent,
34
34
  AgentLogger: () => AgentLogger,
35
35
  AgentMemory: () => AgentMemory,
36
+ AgentTool: () => AgentTool,
36
37
  CodeAgent: () => CodeAgent,
37
38
  FINAL_ANSWER_PROMPT: () => FINAL_ANSWER_PROMPT,
38
39
  FinalAnswerTool: () => FinalAnswerTool,
@@ -42,6 +43,7 @@ __export(index_exports, {
42
43
  OpenAIModel: () => OpenAIModel,
43
44
  Tool: () => Tool,
44
45
  UserInputTool: () => UserInputTool,
46
+ agentAsTool: () => agentAsTool,
45
47
  createTool: () => createTool,
46
48
  finalAnswerTool: () => finalAnswerTool,
47
49
  generateSystemPrompt: () => generateSystemPrompt,
@@ -1749,11 +1751,87 @@ var OpenAIModel = class extends Model {
1749
1751
  });
1750
1752
  }
1751
1753
  };
1754
+
1755
+ // src/tools/AgentTool.ts
1756
+ var AgentTool = class extends Tool {
1757
+ name;
1758
+ description;
1759
+ inputs = {
1760
+ task: {
1761
+ type: "string",
1762
+ description: "The task or question to delegate to this agent",
1763
+ required: true
1764
+ }
1765
+ };
1766
+ outputType = "string";
1767
+ agent;
1768
+ additionalContext;
1769
+ returnFullResult;
1770
+ constructor(config) {
1771
+ super();
1772
+ this.agent = config.agent;
1773
+ this.name = config.name ?? "managed_agent";
1774
+ this.additionalContext = config.additionalContext;
1775
+ this.returnFullResult = config.returnFullResult ?? false;
1776
+ this.description = config.description ?? this.generateDescription();
1777
+ }
1778
+ /**
1779
+ * Generate a default description based on the agent's configuration
1780
+ */
1781
+ generateDescription() {
1782
+ return `Delegates a task to a specialized agent.
1783
+ This agent can help with complex sub-tasks that require multiple steps.
1784
+ Pass a clear, specific task description and the agent will work autonomously to solve it.
1785
+ Returns the agent's final answer as a string.`;
1786
+ }
1787
+ /**
1788
+ * Execute the agent with the given task
1789
+ */
1790
+ async execute(args) {
1791
+ let task = args.task;
1792
+ if (this.additionalContext) {
1793
+ task = `${this.additionalContext}
1794
+
1795
+ Task: ${task}`;
1796
+ }
1797
+ const result = await this.agent.run(task, true);
1798
+ if (this.returnFullResult) {
1799
+ return {
1800
+ output: result.output,
1801
+ steps: result.steps.length,
1802
+ duration: result.duration
1803
+ };
1804
+ }
1805
+ const output = result.output;
1806
+ if (typeof output === "string") {
1807
+ return output;
1808
+ }
1809
+ return JSON.stringify(output, null, 2);
1810
+ }
1811
+ /**
1812
+ * Override toCodePrompt to provide a cleaner signature for nested agents
1813
+ */
1814
+ toCodePrompt() {
1815
+ return `
1816
+ /**
1817
+ * ${this.description}
1818
+ *
1819
+ * @param task - The task or question to delegate to this specialized agent
1820
+ * @returns The agent's answer as a string
1821
+ */
1822
+ async function ${this.name}(task: string): Promise<string> { ... }
1823
+ `.trim();
1824
+ }
1825
+ };
1826
+ function agentAsTool(agent, options) {
1827
+ return new AgentTool({ agent, ...options });
1828
+ }
1752
1829
  // Annotate the CommonJS export names for ESM import in node:
1753
1830
  0 && (module.exports = {
1754
1831
  Agent,
1755
1832
  AgentLogger,
1756
1833
  AgentMemory,
1834
+ AgentTool,
1757
1835
  CodeAgent,
1758
1836
  FINAL_ANSWER_PROMPT,
1759
1837
  FinalAnswerTool,
@@ -1763,6 +1841,7 @@ var OpenAIModel = class extends Model {
1763
1841
  OpenAIModel,
1764
1842
  Tool,
1765
1843
  UserInputTool,
1844
+ agentAsTool,
1766
1845
  createTool,
1767
1846
  finalAnswerTool,
1768
1847
  generateSystemPrompt,