-
Notifications
You must be signed in to change notification settings - Fork 518
Expand file tree
/
Copy pathagent-template.ts
More file actions
165 lines (151 loc) · 4.31 KB
/
agent-template.ts
File metadata and controls
165 lines (151 loc) · 4.31 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
/**
* Backend Agent Template Types
*
* This file provides backend-compatible agent template types with strict validation.
* It imports base types from the user-facing template to eliminate duplication.
*/
import type { MCPConfig } from './mcp'
import type { Model } from '../old-constants'
import type { ToolResultOutput } from './messages/content-part'
import type { AgentState, AgentTemplateType } from './session-state'
import type {
ToolCall,
AgentState as PublicAgentState,
} from '../templates/initial-agents-dir/types/agent-definition'
import type { Logger } from '../templates/initial-agents-dir/types/util-types'
import type { ToolName } from '../tools/constants'
import type { z } from 'zod/v4'
export type AgentId = `${string}/${string}@${number}.${number}.${number}`
export type OpenRouterReasoningOptions = {
/**
* 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.
*/
enabled?: boolean
exclude?: boolean
} & (
| {
max_tokens: number
}
| {
effort: 'high' | 'medium' | 'low'
}
)
export type OpenRouterProviderRoutingOptions = {
/**
* List of provider slugs to try in order (e.g. ["anthropic", "openai"])
*/
order?: string[]
/**
* Whether to allow backup providers when primary is unavailable (default: true)
*/
allow_fallbacks?: boolean
/**
* Only use providers that support all parameters in your request (default: false)
*/
require_parameters?: boolean
/**
* Control whether to use providers that may store data
*/
data_collection?: 'allow' | 'deny'
/**
* List of provider slugs to allow for this request
*/
only?: string[]
/**
* List of provider slugs to skip for this request
*/
ignore?: string[]
/**
* List of quantization levels to filter by (e.g. ["int4", "int8"])
*/
quantizations?: Array<
| 'int4'
| 'int8'
| 'fp4'
| 'fp6'
| 'fp8'
| 'fp16'
| 'bf16'
| 'fp32'
| 'unknown'
>
/**
* Sort providers by price, throughput, or latency
*/
sort?: 'price' | 'throughput' | 'latency'
/**
* Maximum pricing you want to pay for this request
*/
max_price?: {
prompt?: number | string
completion?: number | string
image?: number | string
audio?: number | string
request?: number | string
}
}
export type OpenRouterProviderOptions = {
models?: string[]
reasoning?: OpenRouterReasoningOptions
/**
* A unique identifier representing your end-user, which can
* help OpenRouter to monitor and detect abuse.
*/
user?: string
}
/**
* Backend agent template with strict validation and Zod schemas
* Extends the user-facing AgentDefinition but with backend-specific requirements
*/
export type AgentTemplate<
P = string | undefined,
T = Record<string, any> | undefined,
> = {
id: AgentTemplateType
displayName: string
model: Model
reasoningOptions?: OpenRouterReasoningOptions
providerOptions?: OpenRouterProviderRoutingOptions
mcpServers: Record<string, MCPConfig>
toolNames: (ToolName | (string & {}))[]
spawnableAgents: AgentTemplateType[]
spawnerPrompt?: string
systemPrompt: string
instructionsPrompt: string
stepPrompt: string
parentInstructions?: Record<string, string>
// Required parameters for spawning this agent.
inputSchema: {
prompt?: z.ZodSchema<P>
params?: z.ZodSchema<T>
}
includeMessageHistory: boolean
inheritParentSystemPrompt: boolean
outputMode: 'last_message' | 'all_messages' | 'structured_output'
outputSchema?: z.ZodSchema<any>
handleSteps?: StepHandler<P, T> | string // Function or string of the generator code for running in a sandbox
}
export type StepText = { type: 'STEP_TEXT'; text: string }
export type GenerateN = { type: 'GENERATE_N'; n: number }
export type StepGenerator = Generator<
Omit<ToolCall, 'toolCallId'> | 'STEP' | 'STEP_ALL' | StepText | GenerateN, // Generic tool call type
void,
{
agentState: PublicAgentState
toolResult: ToolResultOutput[]
stepsComplete: boolean
nResponses?: string[]
}
>
export type StepHandler<
P = string | undefined,
T = Record<string, any> | undefined,
> = (context: {
agentState: AgentState
prompt: P
params: T
logger: Logger
}) => StepGenerator
export { Logger, PublicAgentState }