(
params: {
action: ClientAction<'prompt'>
promptId: string
sendAction: SendActionFn
logger: Logger
signal: AbortSignal
} & ParamsExcluding<
typeof mainPrompt,
'localAgentTemplates' | 'onResponseChunk'
>,
)
| 154 | } |
| 155 | |
| 156 | export 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, |
no test coverage detected