MCPcopy Index your code
hub / github.com/CodebuffAI/codebuff / callMainPrompt

Function callMainPrompt

packages/agent-runtime/src/main-prompt.ts:156–254  ·  view source on GitHub ↗
(
  params: {
    action: ClientAction<'prompt'>
    promptId: string
    sendAction: SendActionFn
    logger: Logger
    signal: AbortSignal
  } & ParamsExcluding<
    typeof mainPrompt,
    'localAgentTemplates' | 'onResponseChunk'
  >,
)

Source from the content-addressed store, hash-verified

154}
155
156export async function callMainPrompt(
157 params: {
158 action: ClientAction<'prompt'>
159 promptId: string
160 sendAction: SendActionFn
161 logger: Logger
162 signal: AbortSignal
163 } & ParamsExcluding<
164 typeof mainPrompt,
165 'localAgentTemplates' | 'onResponseChunk'
166 >,
167) {
168 const { action, promptId, sendAction, logger } = params
169 const { fileContext } = action.sessionState
170
171 // Enforce server-side state authority: reset creditsUsed to 0
172 // The server controls cost tracking, clients cannot manipulate this value
173 action.sessionState.mainAgentState.creditsUsed = 0
174 action.sessionState.mainAgentState.directCreditsUsed = 0
175
176 // Add any extra tool results (e.g. from user-executed terminal commands) to message history
177 // This allows the AI to see context from commands run between prompts
178 if (action.toolResults && action.toolResults.length > 0) {
179 action.sessionState.mainAgentState.messageHistory.push(
180 ...action.toolResults,
181 )
182 }
183
184 // Assemble local agent templates from fileContext
185 const { agentTemplates: localAgentTemplates, validationErrors } =
186 assembleLocalAgentTemplates({ fileContext, logger })
187
188 if (validationErrors.length > 0) {
189 sendAction({
190 action: {
191 type: 'prompt-error',
192 message: `Invalid agent config: ${validationErrors.map((err) => err.message).join('\n')}`,
193 userInputId: promptId,
194 },
195 })
196 }
197
198 sendAction({
199 action: {
200 type: 'response-chunk',
201 userInputId: promptId,
202 chunk: {
203 type: 'start',
204 agentId: action.sessionState.mainAgentState.agentType ?? undefined,
205 messageHistoryLength:
206 action.sessionState.mainAgentState.messageHistory.length,
207 },
208 },
209 })
210
211 const result = await mainPrompt({
212 ...params,
213 localAgentTemplates,

Callers 1

runOnceFunction · 0.90

Calls 2

mainPromptFunction · 0.85

Tested by

no test coverage detected