feat(plugins): add modelOverride/providerOverride to before_agent_start hook

Enable plugins to override the model and provider for agent runs by
returning modelOverride/providerOverride from the before_agent_start
hook. The hook is now invoked early in run.ts (before resolveModel)
so overrides take effect. The result is passed to attempt.ts via
earlyHookResult to prevent double-firing.

This enables security-critical use cases like routing PII-containing
prompts to local models instead of cloud providers.
This commit is contained in:
Nate Fikru
2026-02-15 12:05:29 -05:00
committed by Peter Steinberger
parent 15dd2cda20
commit b90eb51520
5 changed files with 73 additions and 25 deletions

View File

@@ -850,31 +850,37 @@ export async function runEmbeddedAttempt(
try {
const promptStartedAt = Date.now();
// Run before_agent_start hooks to allow plugins to inject context
// Run before_agent_start hooks to allow plugins to inject context.
// If run.ts already fired the hook (for model override), reuse its result.
let effectivePrompt = params.prompt;
if (hookRunner?.hasHooks("before_agent_start")) {
try {
const hookResult = await hookRunner.runBeforeAgentStart(
{
prompt: params.prompt,
messages: activeSession.messages,
},
{
agentId: hookAgentId,
sessionKey: params.sessionKey,
sessionId: params.sessionId,
workspaceDir: params.workspaceDir,
messageProvider: params.messageProvider ?? undefined,
},
const hookResult =
params.earlyHookResult ??
(hookRunner?.hasHooks("before_agent_start")
? await hookRunner
.runBeforeAgentStart(
{
prompt: params.prompt,
messages: activeSession.messages,
},
{
agentId: hookAgentId,
sessionKey: params.sessionKey,
sessionId: params.sessionId,
workspaceDir: params.workspaceDir,
messageProvider: params.messageProvider ?? undefined,
},
)
.catch((hookErr: unknown) => {
log.warn(`before_agent_start hook failed: ${String(hookErr)}`);
return undefined;
})
: undefined);
{
if (hookResult?.prependContext) {
effectivePrompt = `${hookResult.prependContext}\n\n${params.prompt}`;
log.debug(
`hooks: prepended context to prompt (${hookResult.prependContext.length} chars)`,
);
if (hookResult?.prependContext) {
effectivePrompt = `${hookResult.prependContext}\n\n${params.prompt}`;
log.debug(
`hooks: prepended context to prompt (${hookResult.prependContext.length} chars)`,
);
}
} catch (hookErr) {
log.warn(`before_agent_start hook failed: ${String(hookErr)}`);
}
}