|
| 1 | +/** |
| 2 | + * Codebuff Agent Type Definitions |
| 3 | + * |
| 4 | + * This file provides TypeScript type definitions for creating custom Codebuff agents. |
| 5 | + * Import these types in your agent files to get full type safety and IntelliSense. |
| 6 | + * |
| 7 | + * Usage in .agents/your-agent.ts: |
| 8 | + * import { AgentConfig, ToolName, ModelName } from './types/agent-config' |
| 9 | + * |
| 10 | + * const config: AgentConfig = { |
| 11 | + * // ... your agent configuration with full type safety ... |
| 12 | + * } |
| 13 | + * |
| 14 | + * export default config |
| 15 | + */ |
| 16 | + |
| 17 | +// ============================================================================ |
| 18 | +// Core Agent Configuration Types |
| 19 | +// ============================================================================ |
| 20 | + |
| 21 | +export interface AgentConfig { |
| 22 | + /** Unique identifier for this agent. Must contain only lowercase letters, numbers, and hyphens, e.g. 'code-reviewer' */ |
| 23 | + id: string |
| 24 | + |
| 25 | + /** Version string (if not provided, will default to '0.0.1' and be bumped on each publish) */ |
| 26 | + version?: string |
| 27 | + |
| 28 | + /** Publisher ID for the agent. Must be provided if you want to publish the agent. */ |
| 29 | + publisher?: string |
| 30 | + |
| 31 | + /** Human-readable name for the agent */ |
| 32 | + displayName: string |
| 33 | + |
| 34 | + /** AI model to use for this agent. Can be any model in OpenRouter: https://openrouter.ai/models */ |
| 35 | + model: ModelName |
| 36 | + |
| 37 | + // ============================================================================ |
| 38 | + // Tools and Subagents |
| 39 | + // ============================================================================ |
| 40 | + |
| 41 | + /** Tools this agent can use. */ |
| 42 | + toolNames?: ToolName[] |
| 43 | + |
| 44 | + /** Other agents this agent can spawn. */ |
| 45 | + subagents?: SubagentName[] |
| 46 | + |
| 47 | + // ============================================================================ |
| 48 | + // Input and Output |
| 49 | + // ============================================================================ |
| 50 | + |
| 51 | + /** The input schema required to spawn the agent. Provide a prompt string and/or a params object or none. |
| 52 | + * 80% of the time you want just a prompt string with a description: |
| 53 | + * inputSchema: { |
| 54 | + * prompt: { type: 'string', description: 'A description of what info would be helpful to the agent' } |
| 55 | + * } |
| 56 | + */ |
| 57 | + inputSchema?: { |
| 58 | + prompt?: { type: 'string'; description?: string } |
| 59 | + params?: JsonSchema |
| 60 | + } |
| 61 | + |
| 62 | + /** Whether to include conversation history from the parent agent in context. |
| 63 | + * |
| 64 | + * Defaults to false. |
| 65 | + * Use this if the agent needs to know all the previous messages in the conversation. |
| 66 | + */ |
| 67 | + includeMessageHistory?: boolean |
| 68 | + |
| 69 | + /** How the agent should output a response to its parent (defaults to 'last_message') |
| 70 | + * |
| 71 | + * last_message: The last message from the agent, typcically after using tools. |
| 72 | + * |
| 73 | + * all_messages: All messages from the agent, including tool calls and results. |
| 74 | + * |
| 75 | + * json: Make the agent output a JSON object. Can be used with outputSchema or without if you want freeform json output. |
| 76 | + */ |
| 77 | + outputMode?: 'last_message' | 'all_messages' | 'json' |
| 78 | + |
| 79 | + /** JSON schema for structured output (when outputMode is 'json') */ |
| 80 | + outputSchema?: JsonSchema |
| 81 | + |
| 82 | + // ============================================================================ |
| 83 | + // Prompts |
| 84 | + // ============================================================================ |
| 85 | + |
| 86 | + /** Prompt for when to spawn this agent as a subagent. Include the main purpose and use cases. |
| 87 | + * |
| 88 | + * This field is key if the agent is a subagent and intended to be spawned. */ |
| 89 | + parentPrompt?: string |
| 90 | + |
| 91 | + /** Background information for the agent. Fairly optional. Prefer using instructionsPrompt for agent instructions. */ |
| 92 | + systemPrompt?: string |
| 93 | + |
| 94 | + /** Instructions for the agent. |
| 95 | + * |
| 96 | + * IMPORTANT: Updating this prompt is the best way to shape the agent's behavior. |
| 97 | + * This prompt is inserted after each user input. */ |
| 98 | + instructionsPrompt?: string |
| 99 | + |
| 100 | + /** Prompt inserted at each agent step. |
| 101 | + * |
| 102 | + * Powerful for changing the agent's behavior, but usually not necessary for smart models. |
| 103 | + * Prefer instructionsPrompt for most instructions. */ |
| 104 | + stepPrompt?: string |
| 105 | + |
| 106 | + // ============================================================================ |
| 107 | + // Handle Steps |
| 108 | + // ============================================================================ |
| 109 | + |
| 110 | + /** Programmatically step the agent forward and run tools. |
| 111 | + * |
| 112 | + * You can either yield: |
| 113 | + * - A tool call object with toolName and args properties. |
| 114 | + * - 'STEP' to run agent's model and generate one assistant message. |
| 115 | + * - 'STEP_ALL' to run the agent's model until it uses the end_turn tool or stops includes no tool calls in a message. |
| 116 | + * |
| 117 | + * Or use 'return' to end the turn. |
| 118 | + * |
| 119 | + * Example 1: |
| 120 | + * function* handleSteps({ agentStep, prompt, params}) { |
| 121 | + * const { toolResult } = yield { |
| 122 | + * toolName: 'read_files', |
| 123 | + * args: { paths: ['file1.txt', 'file2.txt'] } |
| 124 | + * } |
| 125 | + * yield 'STEP_ALL' |
| 126 | + * } |
| 127 | + * |
| 128 | + * Example 2: |
| 129 | + * handleSteps: function* ({ agentState, prompt, params }) { |
| 130 | + * while (true) { |
| 131 | + * yield { |
| 132 | + * toolName: 'spawn_agents', |
| 133 | + * args: { |
| 134 | + * agents: [ |
| 135 | + * { |
| 136 | + * agent_type: 'thinker', |
| 137 | + * prompt: 'Think deeply about the user request', |
| 138 | + * }, |
| 139 | + * ], |
| 140 | + * }, |
| 141 | + * } |
| 142 | + * yield 'STEP' |
| 143 | + * } |
| 144 | + * } |
| 145 | + */ |
| 146 | + handleSteps?: ( |
| 147 | + context: AgentStepContext, |
| 148 | + ) => Generator< |
| 149 | + ToolCall | 'STEP' | 'STEP_ALL', |
| 150 | + void, |
| 151 | + { agentState: AgentState; toolResult: ToolResult | undefined } |
| 152 | + > |
| 153 | +} |
| 154 | + |
| 155 | +// ============================================================================ |
| 156 | +// Supporting Types |
| 157 | +// ============================================================================ |
| 158 | + |
| 159 | +export interface AgentState { |
| 160 | + agentId: string |
| 161 | + parentId: string |
| 162 | + messageHistory: Message[] |
| 163 | +} |
| 164 | + |
| 165 | +/** |
| 166 | + * Message in conversation history |
| 167 | + */ |
| 168 | +export interface Message { |
| 169 | + role: 'user' | 'assistant' | 'system' |
| 170 | + content: string |
| 171 | + timestamp?: number |
| 172 | +} |
| 173 | + |
| 174 | +/** |
| 175 | + * Context provided to handleSteps generator function |
| 176 | + */ |
| 177 | +export interface AgentStepContext { |
| 178 | + agentState: AgentState |
| 179 | + prompt?: string |
| 180 | + params?: Record<string, any> |
| 181 | +} |
| 182 | + |
| 183 | +/** |
| 184 | + * Tool call object for handleSteps generator |
| 185 | + */ |
| 186 | +export type ToolCall<T extends ToolName = ToolName> = { |
| 187 | + [K in T]: { |
| 188 | + toolName: K |
| 189 | + args?: Tools.GetToolParams<K> |
| 190 | + } |
| 191 | +}[T] |
| 192 | + |
| 193 | +/** |
| 194 | + * Result from executing a tool |
| 195 | + */ |
| 196 | +export interface ToolResult { |
| 197 | + toolName: string |
| 198 | + toolCallId: string |
| 199 | + result: string |
| 200 | +} |
| 201 | + |
| 202 | +/** |
| 203 | + * JSON Schema definition (for prompt schema or output schema) |
| 204 | + */ |
| 205 | +export interface JsonSchema { |
| 206 | + type: string |
| 207 | + properties?: Record<string, any> |
| 208 | + required?: string[] |
| 209 | + [key: string]: any |
| 210 | +} |
| 211 | + |
| 212 | +// ============================================================================ |
| 213 | +// Available Tools |
| 214 | +// ============================================================================ |
| 215 | + |
| 216 | +/** |
| 217 | + * File operation tools |
| 218 | + */ |
| 219 | +export type FileTools = |
| 220 | + | 'read_files' |
| 221 | + | 'write_file' |
| 222 | + | 'str_replace' |
| 223 | + | 'find_files' |
| 224 | + |
| 225 | +/** |
| 226 | + * Code analysis tools |
| 227 | + */ |
| 228 | +export type CodeAnalysisTools = 'code_search' | 'find_files' |
| 229 | + |
| 230 | +/** |
| 231 | + * Terminal and system tools |
| 232 | + */ |
| 233 | +export type TerminalTools = 'run_terminal_command' | 'run_file_change_hooks' |
| 234 | + |
| 235 | +/** |
| 236 | + * Web and browser tools |
| 237 | + */ |
| 238 | +export type WebTools = 'browser_logs' | 'web_search' | 'read_docs' |
| 239 | + |
| 240 | +/** |
| 241 | + * Agent management tools |
| 242 | + */ |
| 243 | +export type AgentTools = |
| 244 | + | 'spawn_agents' |
| 245 | + | 'spawn_agents_async' |
| 246 | + | 'send_agent_message' |
| 247 | + | 'set_messages' |
| 248 | + | 'add_message' |
| 249 | + |
| 250 | +/** |
| 251 | + * Planning and organization tools |
| 252 | + */ |
| 253 | +export type PlanningTools = |
| 254 | + | 'think_deeply' |
| 255 | + | 'create_plan' |
| 256 | + | 'add_subgoal' |
| 257 | + | 'update_subgoal' |
| 258 | + |
| 259 | +/** |
| 260 | + * Output and control tools |
| 261 | + */ |
| 262 | +export type OutputTools = 'set_output' | 'end_turn' |
| 263 | + |
| 264 | +/** |
| 265 | + * Common tool combinations for convenience |
| 266 | + */ |
| 267 | +export type FileEditingTools = FileTools | 'end_turn' |
| 268 | +export type ResearchTools = WebTools | 'write_file' | 'end_turn' |
| 269 | +export type CodeAnalysisToolSet = FileTools | CodeAnalysisTools | 'end_turn' |
| 270 | + |
| 271 | +// ============================================================================ |
| 272 | +// Available Models (see: https://openrouter.ai/models) |
| 273 | +// ============================================================================ |
| 274 | + |
| 275 | +/** |
| 276 | + * AI models available for agents (all models in OpenRouter are supported) |
| 277 | + * |
| 278 | + * See available models at https://openrouter.ai/models |
| 279 | + */ |
| 280 | +export type ModelName = |
| 281 | + // Verified OpenRouter Models |
| 282 | + | 'anthropic/claude-4-sonnet-20250522' |
| 283 | + | 'anthropic/claude-opus-4.1' |
| 284 | + | 'anthropic/claude-3.5-haiku-20241022' |
| 285 | + | 'anthropic/claude-3.5-sonnet-20240620' |
| 286 | + | 'openai/gpt-4o-2024-11-20' |
| 287 | + | 'openai/gpt-4o-mini-2024-07-18' |
| 288 | + | 'openai/o3' |
| 289 | + | 'openai/o4-mini' |
| 290 | + | 'openai/o4-mini-high' |
| 291 | + | 'google/gemini-2.5-pro' |
| 292 | + | 'google/gemini-2.5-flash' |
| 293 | + | 'x-ai/grok-4-07-09' |
| 294 | + | (string & {}) |
| 295 | + |
| 296 | +// ============================================================================ |
| 297 | +// Spawnable Agents |
| 298 | +// ============================================================================ |
| 299 | + |
| 300 | +/** |
| 301 | + * Built-in agents that can be spawned by custom agents |
| 302 | + */ |
| 303 | +export type SubagentName = |
| 304 | + | 'file-picker' |
| 305 | + | 'file-explorer' |
| 306 | + | 'researcher' |
| 307 | + | 'thinker' |
| 308 | + | 'reviewer' |
| 309 | + | (string & {}) |
| 310 | + |
| 311 | +import type * as Tools from './tools' |
| 312 | +export type { Tools } |
| 313 | +type ToolName = Tools.ToolName |
0 commit comments