1- import type { ToolName } from '@codebuff/common/tools/constants'
2-
31import { CodebuffConfigSchema } from '@codebuff/common/json-config/constants'
42import { stringifySchema } from '@codebuff/common/json-config/stringify-schema'
5- import {
6- AgentState ,
7- AgentTemplateType ,
8- } from '@codebuff/common/types/session-state'
3+ import { renderToolResults } from '@codebuff/common/tools/utils'
4+ import { escapeString , generateCompactId } from '@codebuff/common/util/string'
95import { z } from 'zod/v4'
106
7+ import { agentTemplates } from './agent-list'
8+ import { type AgentRegistry } from './agent-registry'
9+ import { buildSubagentsDescription } from './prompts'
10+ import { PLACEHOLDER , placeholderValues } from './types'
1111import {
1212 getGitChangesPrompt ,
1313 getProjectFileTreePrompt ,
@@ -17,20 +17,15 @@ import {
1717 getShortToolInstructions ,
1818 getToolsInstructions ,
1919} from '../tools/prompts'
20-
21- import { renderToolResults } from '@codebuff/common/tools/utils'
22- import { ProjectFileContext } from '@codebuff/common/util/file'
23- import { escapeString , generateCompactId } from '@codebuff/common/util/string'
2420import { parseUserMessage } from '../util/messages'
25- import { agentTemplates } from './agent-list'
26- import { type AgentRegistry } from './agent-registry'
27- import { buildSubagentsDescription } from './prompts'
28- import {
29- AgentTemplate ,
30- PLACEHOLDER ,
31- PlaceholderValue ,
32- placeholderValues ,
33- } from './types'
21+
22+ import type { AgentTemplate , PlaceholderValue } from './types'
23+ import type { ToolName } from '@codebuff/common/tools/constants'
24+ import type {
25+ AgentState ,
26+ AgentTemplateType ,
27+ } from '@codebuff/common/types/session-state'
28+ import type { ProjectFileContext } from '@codebuff/common/util/file'
3429
3530export async function formatPrompt (
3631 prompt : string ,
@@ -39,14 +34,14 @@ export async function formatPrompt(
3934 tools : ToolName [ ] ,
4035 spawnableAgents : AgentTemplateType [ ] ,
4136 agentRegistry : AgentRegistry ,
42- intitialAgentPrompt ?: string
37+ intitialAgentPrompt ?: string ,
4338) : Promise < string > {
4439 const { messageHistory } = agentState
4540 const lastUserMessage = messageHistory . findLast (
4641 ( { role, content } ) =>
4742 role === 'user' &&
4843 typeof content === 'string' &&
49- parseUserMessage ( content )
44+ parseUserMessage ( content ) ,
5045 )
5146 const lastUserInput = lastUserMessage
5247 ? parseUserMessage ( lastUserMessage . content as string )
@@ -62,7 +57,7 @@ export async function formatPrompt(
6257 [ PLACEHOLDER . FILE_TREE_PROMPT ] : getProjectFileTreePrompt (
6358 fileContext ,
6459 20_000 ,
65- 'agent'
60+ 'agent' ,
6661 ) ,
6762 [ PLACEHOLDER . GIT_CHANGES_PROMPT ] : getGitChangesPrompt ( fileContext ) ,
6863 [ PLACEHOLDER . REMAINING_STEPS ] : `${ agentState . stepsRemaining ! } ` ,
@@ -71,7 +66,7 @@ export async function formatPrompt(
7166 [ PLACEHOLDER . TOOLS_PROMPT ] : getToolsInstructions ( tools ) ,
7267 [ PLACEHOLDER . AGENTS_PROMPT ] : buildSubagentsDescription (
7368 spawnableAgents ,
74- agentRegistry
69+ agentRegistry ,
7570 ) ,
7671 [ PLACEHOLDER . USER_CWD ] : fileContext . cwd ,
7772 [ PLACEHOLDER . USER_INPUT_PROMPT ] : escapeString ( lastUserInput ?? '' ) ,
@@ -86,16 +81,16 @@ export async function formatPrompt(
8681 'CLAUDE.md' ,
8782 'codebuff.json' ,
8883 'codebuff.jsonc' ,
89- ] . includes ( path )
84+ ] . includes ( path ) ,
9085 )
91- . map ( ( [ path , content ] ) => [ path , content . trim ( ) ] )
86+ . map ( ( [ path , content ] ) => [ path , content . trim ( ) ] ) ,
9287 ) ,
9388 ...fileContext . userKnowledgeFiles ,
9489 } ) . map ( ( [ path , content ] ) => ( {
9590 toolName : 'read_files' ,
9691 toolCallId : generateCompactId ( ) ,
9792 result : JSON . stringify ( { path, content } ) ,
98- } ) )
93+ } ) ) ,
9994 ) ,
10095 }
10196
@@ -111,7 +106,7 @@ type StringField = 'systemPrompt' | 'instructionsPrompt' | 'stepPrompt'
111106
112107export async function collectParentInstructions (
113108 agentType : string ,
114- agentRegistry : AgentRegistry
109+ agentRegistry : AgentRegistry ,
115110) : Promise < string [ ] > {
116111 const instructions : string [ ] = [ ]
117112
@@ -132,20 +127,31 @@ export async function getAgentPrompt<T extends StringField>(
132127 promptType : { type : T } ,
133128 fileContext : ProjectFileContext ,
134129 agentState : AgentState ,
135- agentRegistry : AgentRegistry
130+ agentRegistry : AgentRegistry ,
136131) : Promise < string | undefined > {
137- const promptValue = agentTemplate [ promptType . type ]
132+ let promptValue = agentTemplate [ promptType . type ]
133+ addToolsPromptToSystemPrompt: if ( promptType . type === 'systemPrompt' ) {
134+ if ( promptValue === undefined ) {
135+ promptValue = PLACEHOLDER . TOOLS_PROMPT
136+ break addToolsPromptToSystemPrompt
137+ }
138+ if ( ! promptValue . includes ( PLACEHOLDER . TOOLS_PROMPT ) ) {
139+ promptValue += `\n\n${ PLACEHOLDER . TOOLS_PROMPT } `
140+ }
141+ }
142+
138143 if ( promptValue === undefined ) {
139144 return undefined
140145 }
146+
141147 const prompt = await formatPrompt (
142148 promptValue ,
143149 fileContext ,
144150 agentState ,
145151 agentTemplate . toolNames ,
146152 agentTemplate . subagents ,
147153 agentRegistry ,
148- ''
154+ '' ,
149155 )
150156
151157 let addendum = ''
@@ -160,7 +166,7 @@ export async function getAgentPrompt<T extends StringField>(
160166
161167 const parentInstructions = await collectParentInstructions (
162168 agentState . agentType ,
163- agentRegistry
169+ agentRegistry ,
164170 )
165171
166172 if ( parentInstructions . length > 0 ) {
@@ -186,7 +192,7 @@ export async function getAgentPrompt<T extends StringField>(
186192 addendum += JSON . stringify (
187193 { type : 'object' , description : 'Output schema validation enabled' } ,
188194 null ,
189- 2
195+ 2 ,
190196 )
191197 }
192198 addendum += '\n```'
0 commit comments