Skip to content

Commit 4114a37

Browse files
committed
Prompt the agent with the output schema
1 parent 81eeae2 commit 4114a37

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

backend/src/run-agent-step.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ export const runAgentStep = async (
484484
(toolCalls.length === 0 && toolResults.length === 0)
485485

486486
const newAgentState = {
487-
...agentState, // This now includes any updates from tool execution (like output)
487+
...agentState,
488488
messageHistory: finalMessageHistoryWithToolResults,
489489
stepsRemaining: agentState.stepsRemaining - 1,
490490
agentContext: newAgentContext,

backend/src/templates/strings.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { z } from 'zod/v4'
12
import { CodebuffConfigSchema } from '@codebuff/common/json-config/constants'
23
import { stringifySchema } from '@codebuff/common/json-config/stringify-schema'
34
import {
@@ -146,24 +147,46 @@ export async function getAgentPrompt<T extends StringField | RequirePrompt>(
146147

147148
// Add parent instructions for userInputPrompt
148149
if (promptType.type === 'userInputPrompt' && agentState.agentType) {
149-
const parentInstructions = await collectParentInstructions(
150-
agentState.agentType,
151-
agentRegistry
152-
)
153-
154150
addendum +=
155151
'\n\n' +
156152
getShortToolInstructions(
157153
agentTemplate.toolNames,
158154
agentTemplate.spawnableAgents
159155
)
160156

157+
const parentInstructions = await collectParentInstructions(
158+
agentState.agentType,
159+
agentRegistry
160+
)
161+
161162
if (parentInstructions.length > 0) {
162163
addendum += '\n\n## Additional Instructions for Spawning Agents\n\n'
163164
addendum += parentInstructions
164165
.map((instruction) => `- ${instruction}`)
165166
.join('\n')
166167
}
168+
169+
// Add output schema information if defined
170+
if (agentTemplate.outputSchema) {
171+
addendum += '\n\n## Output Schema\n\n'
172+
addendum +=
173+
'When using the set_output tool, your output must conform to this schema:\n\n'
174+
addendum += '```json\n'
175+
try {
176+
// Convert Zod schema to JSON schema for display
177+
const jsonSchema = z.toJSONSchema(agentTemplate.outputSchema)
178+
delete jsonSchema['$schema'] // Remove the $schema field for cleaner display
179+
addendum += JSON.stringify(jsonSchema, null, 2)
180+
} catch {
181+
// Fallback to a simple description
182+
addendum += JSON.stringify(
183+
{ type: 'object', description: 'Output schema validation enabled' },
184+
null,
185+
2
186+
)
187+
}
188+
addendum += '\n```'
189+
}
167190
}
168191

169192
return prompt + addendum

0 commit comments

Comments
 (0)