-
Notifications
You must be signed in to change notification settings - Fork 519
Expand file tree
/
Copy pathagent-definition.ts
More file actions
348 lines (298 loc) · 10.4 KB
/
agent-definition.ts
File metadata and controls
348 lines (298 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/**
* Codebuff Agent Type Definitions
*
* This file provides TypeScript type definitions for creating custom Codebuff agents.
* Import these types in your agent files to get full type safety and IntelliSense.
*
* Usage in .agents/your-agent.ts:
* import { AgentDefinition, ToolName, ModelName } from './types/agent-definition'
*
* const definition: AgentDefinition = {
* // ... your agent configuration with full type safety ...
* }
*
* export default definition
*/
import type * as Tools from './tools'
import type { Message, ToolResultOutput, JsonObjectSchema } from './util-types'
type ToolName = Tools.ToolName
// ============================================================================
// Logger Interface
// ============================================================================
export interface Logger {
debug: (data: any, msg?: string) => void
info: (data: any, msg?: string) => void
warn: (data: any, msg?: string) => void
error: (data: any, msg?: string) => void
}
// ============================================================================
// Agent Definition and Utility Types
// ============================================================================
export interface AgentDefinition {
/** Unique identifier for this agent. Must contain only lowercase letters, numbers, and hyphens, e.g. 'code-reviewer' */
id: string
/** Version string (if not provided, will default to '0.0.1' and be bumped on each publish) */
version?: string
/** Publisher ID for the agent. Must be provided if you want to publish the agent. */
publisher?: string
/** Human-readable name for the agent */
displayName: string
/** AI model to use for this agent. Can be any model in OpenRouter: https://openrouter.ai/models */
model: ModelName
/**
* https://openrouter.ai/docs/use-cases/reasoning-tokens
* One of `max_tokens` or `effort` is required.
* If `exclude` is true, reasoning will be removed from the response. Default is false.
*/
reasoningOptions?: {
enabled?: boolean
exclude?: boolean
} & (
| {
max_tokens: number
}
| {
effort: 'high' | 'medium' | 'low'
}
)
// ============================================================================
// Tools and Subagents
// ============================================================================
/** Tools this agent can use. */
toolNames?: (ToolName | (string & {}))[]
/** Other agents this agent can spawn, like 'codebuff/file-picker@0.0.1'.
*
* Use the fully qualified agent id from the agent store, including publisher and version: 'codebuff/file-picker@0.0.1'
* (publisher and version are required!)
*
* Or, use the agent id from a local agent file in your .agents directory: 'file-picker'.
*/
spawnableAgents?: string[]
// ============================================================================
// Input and Output
// ============================================================================
/** The input schema required to spawn the agent. Provide a prompt string and/or a params object or none.
* 80% of the time you want just a prompt string with a description:
* inputSchema: {
* prompt: { type: 'string', description: 'A description of what info would be helpful to the agent' }
* }
*/
inputSchema?: {
prompt?: { type: 'string'; description?: string }
params?: JsonObjectSchema
}
/** Whether to include conversation history from the parent agent in context.
*
* Defaults to false.
* Use this if the agent needs to know all the previous messages in the conversation.
*/
includeMessageHistory?: boolean
/** How the agent should output a response to its parent (defaults to 'last_message')
*
* last_message: The last message from the agent, typically after using tools.
*
* all_messages: All messages from the agent, including tool calls and results.
*
* structured_output: Make the agent output a JSON object. Can be used with outputSchema or without if you want freeform json output.
*/
outputMode?: 'last_message' | 'all_messages' | 'structured_output'
/** JSON schema for structured output (when outputMode is 'structured_output') */
outputSchema?: JsonObjectSchema
// ============================================================================
// Prompts
// ============================================================================
/** Prompt for when and why to spawn this agent. Include the main purpose and use cases.
*
* This field is key if the agent is intended to be spawned by other agents. */
spawnerPrompt?: string
/** Background information for the agent. Fairly optional. Prefer using instructionsPrompt for agent instructions. */
systemPrompt?: string
/** Instructions for the agent.
*
* IMPORTANT: Updating this prompt is the best way to shape the agent's behavior.
* This prompt is inserted after each user input. */
instructionsPrompt?: string
/** Prompt inserted at each agent step.
*
* Powerful for changing the agent's behavior, but usually not necessary for smart models.
* Prefer instructionsPrompt for most instructions. */
stepPrompt?: string
// ============================================================================
// Handle Steps
// ============================================================================
/** Programmatically step the agent forward and run tools.
*
* You can either yield:
* - A tool call object with toolName and input properties.
* - 'STEP' to run agent's model and generate one assistant message.
* - 'STEP_ALL' to run the agent's model until it uses the end_turn tool or stops includes no tool calls in a message.
*
* Or use 'return' to end the turn.
*
* Example 1:
* function* handleSteps({ agentState, prompt, params, logger }) {
* logger.info('Starting file read process')
* const { toolResult } = yield {
* toolName: 'read_files',
* input: { paths: ['file1.txt', 'file2.txt'] }
* }
* yield 'STEP_ALL'
*
* // Optionally do a post-processing step here...
* logger.info('Files read successfully, setting output')
* yield {
* toolName: 'set_output',
* input: {
* output: 'The files were read successfully.',
* },
* }
* }
*
* Example 2:
* handleSteps: function* ({ agentState, prompt, params, logger }) {
* while (true) {
* logger.debug('Spawning thinker agent')
* yield {
* toolName: 'spawn_agents',
* input: {
* agents: [
* {
* agent_type: 'thinker',
* prompt: 'Think deeply about the user request',
* },
* ],
* },
* }
* const { stepsComplete } = yield 'STEP'
* if (stepsComplete) break
* }
* }
*/
handleSteps?: (context: AgentStepContext) => Generator<
ToolCall | 'STEP' | 'STEP_ALL',
void,
{
agentState: AgentState
toolResult: ToolResultOutput[] | undefined
stepsComplete: boolean
}
>
}
// ============================================================================
// Supporting Types
// ============================================================================
export interface AgentState {
agentId: string
runId: string
parentId: string | undefined
/** The agent's conversation history: messages from the user and the assistant. */
messageHistory: Message[]
/** The last value set by the set_output tool. This is a plain object or undefined if not set. */
output: Record<string, any> | undefined
}
/**
* Context provided to handleSteps generator function
*/
export interface AgentStepContext {
agentState: AgentState
prompt?: string
params?: Record<string, any>
logger: Logger
}
/**
* Tool call object for handleSteps generator
*/
export type ToolCall<T extends ToolName = ToolName> = {
[K in T]: {
toolName: K
input: Tools.GetToolParams<K>
includeToolCall?: boolean
}
}[T]
// ============================================================================
// Available Tools
// ============================================================================
/**
* File operation tools
*/
export type FileTools =
| 'read_files'
| 'write_file'
| 'str_replace'
| 'find_files'
/**
* Code analysis tools
*/
export type CodeAnalysisTools = 'code_search' | 'find_files'
/**
* Terminal and system tools
*/
export type TerminalTools = 'run_terminal_command' | 'run_file_change_hooks'
/**
* Web and browser tools
*/
export type WebTools = 'web_search' | 'read_docs'
/**
* Agent management tools
*/
export type AgentTools = 'spawn_agents' | 'set_messages' | 'add_message'
/**
* Planning and organization tools
*/
export type PlanningTools = 'think_deeply'
/**
* Output and control tools
*/
export type OutputTools = 'set_output' | 'end_turn'
/**
* Common tool combinations for convenience
*/
export type FileEditingTools = FileTools | 'end_turn'
export type ResearchTools = WebTools | 'write_file' | 'end_turn'
export type CodeAnalysisToolSet = FileTools | CodeAnalysisTools | 'end_turn'
// ============================================================================
// Available Models (see: https://openrouter.ai/models)
// ============================================================================
/**
* AI models available for agents. Pick from our selection of recommended models or choose any model in OpenRouter.
*
* See available models at https://openrouter.ai/models
*/
export type ModelName =
// Recommended Models
// OpenAI
| 'openai/gpt-5'
| 'openai/gpt-5-chat'
| 'openai/gpt-5-mini'
| 'openai/gpt-5-nano'
// Anthropic
| 'anthropic/claude-4-sonnet-20250522'
| 'anthropic/claude-opus-4.1'
// Gemini
| 'google/gemini-2.5-pro'
| 'google/gemini-2.5-flash'
| 'google/gemini-2.5-flash-lite'
// X-AI
| 'x-ai/grok-4-07-09'
| 'x-ai/grok-code-fast-1'
// Qwen
| 'qwen/qwen3-coder'
| 'qwen/qwen3-coder:nitro'
| 'qwen/qwen3-235b-a22b-2507'
| 'qwen/qwen3-235b-a22b-2507:nitro'
| 'qwen/qwen3-235b-a22b-thinking-2507'
| 'qwen/qwen3-235b-a22b-thinking-2507:nitro'
| 'qwen/qwen3-30b-a3b'
| 'qwen/qwen3-30b-a3b:nitro'
// DeepSeek
| 'deepseek/deepseek-chat-v3-0324'
| 'deepseek/deepseek-chat-v3-0324:nitro'
| 'deepseek/deepseek-r1-0528'
| 'deepseek/deepseek-r1-0528:nitro'
// Other open source models
| 'moonshotai/kimi-k2'
| 'moonshotai/kimi-k2:nitro'
| 'z-ai/glm-4.5'
| 'z-ai/glm-4.5:nitro'
| (string & {})
export type { Tools }