Creating Custom Tools
Relevant source files
The following files were used as context for generating this wiki page:
Creating custom tools within OS Chat involves leveraging the Vercel AI SDK to define executable functions that the AI can invoke to interact with external services, manage state, or perform complex delegated tasks. The system utilizes a combination of built-in search tools, persistence-focused memory tools, and a specialized create_agent tool that acts as a meta-tool for orchestrating multi-step workflows across various service connectors like Gmail, Notion, and Slack.
The architecture relies on Zod schemas for strict input validation and a factory-like pattern where tools are instantiated based on user context, such as session IDs or connected third-party integrations. These tools are then passed to the AI model's execution loop, allowing for autonomous operations within defined constraints.
Sources: src/lib/create-agent-tool.ts:16-33, convex/scheduled_ai.ts:153-189, README.md
Tool Definition and Validation
Tools in OS Chat are defined using the tool function from the AI SDK, which requires a descriptive purpose and a Zod-based input schema. This ensures that the AI provides the correct parameters before execution occurs.
Input Schema Structure
The create_agent tool, for instance, uses a specific schema to ensure delegated tasks have the necessary toolkit selection and context.
| Field | Type | Description | Constraints |
|---|---|---|---|
tool | string or string[] | One or more connector toolkit slugs (e.g., "GMAIL"). | At least one required. |
task | string | High-level goal for the delegated agent. | 1-2000 characters. |
context | string (optional) | Information from previous operations. | Max 5000 characters. |
Sources: src/lib/create-agent-tool.ts:16-33, src/lib/tests/create-agent-tool.test.ts:16-120
The Agent Orchestration Flow
The following diagram illustrates how the create_agent tool initializes a sub-agent to handle specific toolkit tasks.
The create_agent tool dynamically fetches tools from Composio based on the user's userId and the requested toolkits, then launches a secondary AI stream to process the specific task.
Sources: src/lib/create-agent-tool.ts:114-220
Memory and Persistence Tools
For scheduled tasks and background agents, specialized tools are implemented to maintain "Working Memory." This allows the AI to persist facts across different executions.
Scheduled Agent Memory
The ScheduledAgentMemory class interacts with Redis to store history and working memory. It provides a createUpdateTool which allows the AI to update its own persistent context.
Sources: src/lib/ai/scheduled-agent-memory.ts:35-71
The working memory is injected into the system prompt during the next execution via formatWorkingMemoryPrompt.
Sources: src/lib/ai/scheduled-agent-memory.ts:74-95, convex/scheduled_ai.ts:199-203
Tool Execution and Analysis
When a tool executes, particularly the create_agent sub-agent, the system performs a post-execution analysis to determine if the task was completed successfully or if it encountered issues like "stopped mid-execution" or "insufficient output."
Analysis Criteria
The analyzeSubAgentExecution function evaluates several factors:
- toolCallCount: Number of tools successfully invoked.
- finishReason: Whether the AI reached a "stop" state.
- finalText: Length and quality of the generated summary.
- subAgentError: Any caught exceptions during the stream.
Sources: src/lib/create-agent-tool.ts:54-94
Implementation Example: createAgentTool
// src/lib/create-agent-tool.ts:114-124
export const createAgentTool = ({
userId,
availableToolkits,
model,
systemPrompt,
writer,
}: CreateAgentToolOptions): Tool<CreateAgentInput, string> => {
return tool<CreateAgentInput, string>({
description: `Create a temporary agent that can use specific connectors...`,
inputSchema: createAgentInputSchema,
async execute(input) {
// Logic to fetch tools and stream sub-agent result
}
});
};
UI Integration and Tool Rendering
Custom tools must be handled in the frontend to provide visual feedback. OS Chat uses "Boundaries" to detect when the create_agent tool is running and renders a "Chain of Thought" (CoT) view.
Boundary Detection
The system injects start and end markers into the UI message stream:
data-agent-boundary(type: "start"): Marks the beginning of sub-agent work.data-agent-boundary(type: "end"): Includes the finalanalysisandtoolSummary.
Sources: src/lib/create-agent-tool.ts:225-270, src/components/chat/message-assistant.tsx:162-205
Visual Components
Tools are rendered using specialized components like ConnectorToolCall, which displays the request parameters, response data, and metadata in a collapsible card.
| Component | Purpose | Relevant Files |
|---|---|---|
UnifiedSearch | Renders web search results and query status. | src/components/chat/message-assistant.tsx |
ConnectorToolCall | Renders external service actions (Gmail, etc.) with JSON syntax highlighting. | src/components/tool/connector_tool_call.tsx |
ChainOfThought | Wraps multi-step agent executions in a "Thinking" UI. | src/components/chat/message-assistant.tsx |
Sources: src/components/tool/connector_tool_call.tsx:135-265, src/components/chat/message-assistant.tsx:550-610
Conclusion
Creating custom tools in OS Chat is a multi-layered process involving Zod validation, sub-agent orchestration via create_agent, and persistent state management through Redis-backed memory tools. This modular approach allows the assistant to scale from simple search tasks to complex, autonomous workflows while providing deep visibility into the AI's "thinking" and tool-calling process through the UI.