mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 09:02:45 +00:00
refactor(plugins): split before-agent hooks by model and prompt phases
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import type { AgentMessage } from "@mariozechner/pi-agent-core";
|
||||
import type { ImageContent } from "@mariozechner/pi-ai";
|
||||
import { streamSimple } from "@mariozechner/pi-ai";
|
||||
import { createAgentSession, SessionManager, SettingsManager } from "@mariozechner/pi-coding-agent";
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import type { EmbeddedRunAttemptParams, EmbeddedRunAttemptResult } from "./types.js";
|
||||
import { resolveHeartbeatPrompt } from "../../../auto-reply/heartbeat.js";
|
||||
import { resolveChannelCapabilities } from "../../../config/channel-capabilities.js";
|
||||
import { getMachineDisplayName } from "../../../infra/machine-name.js";
|
||||
@@ -103,7 +104,6 @@ import {
|
||||
shouldFlagCompactionTimeout,
|
||||
} from "./compaction-timeout.js";
|
||||
import { detectAndLoadPromptImages } from "./images.js";
|
||||
import type { EmbeddedRunAttemptParams, EmbeddedRunAttemptResult } from "./types.js";
|
||||
|
||||
export function injectHistoryImagesIntoMessages(
|
||||
messages: AgentMessage[],
|
||||
@@ -863,31 +863,52 @@ export async function runEmbeddedAttempt(
|
||||
try {
|
||||
const promptStartedAt = Date.now();
|
||||
|
||||
// Run before_agent_start hooks to allow plugins to inject context.
|
||||
// If run.ts already fired the hook (for model override), reuse its result.
|
||||
// Run before_prompt_build hooks to allow plugins to inject prompt context.
|
||||
// Legacy compatibility: before_agent_start is also checked for context fields.
|
||||
let effectivePrompt = params.prompt;
|
||||
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);
|
||||
const hookCtx = {
|
||||
agentId: hookAgentId,
|
||||
sessionKey: params.sessionKey,
|
||||
sessionId: params.sessionId,
|
||||
workspaceDir: params.workspaceDir,
|
||||
messageProvider: params.messageProvider ?? undefined,
|
||||
};
|
||||
const promptBuildResult = hookRunner?.hasHooks("before_prompt_build")
|
||||
? await hookRunner
|
||||
.runBeforePromptBuild(
|
||||
{
|
||||
prompt: params.prompt,
|
||||
messages: activeSession.messages,
|
||||
},
|
||||
hookCtx,
|
||||
)
|
||||
.catch((hookErr: unknown) => {
|
||||
log.warn(`before_prompt_build hook failed: ${String(hookErr)}`);
|
||||
return undefined;
|
||||
})
|
||||
: undefined;
|
||||
const legacyResult = hookRunner?.hasHooks("before_agent_start")
|
||||
? await hookRunner
|
||||
.runBeforeAgentStart(
|
||||
{
|
||||
prompt: params.prompt,
|
||||
messages: activeSession.messages,
|
||||
},
|
||||
hookCtx,
|
||||
)
|
||||
.catch((hookErr: unknown) => {
|
||||
log.warn(
|
||||
`before_agent_start hook (legacy prompt build path) failed: ${String(hookErr)}`,
|
||||
);
|
||||
return undefined;
|
||||
})
|
||||
: undefined;
|
||||
const hookResult = {
|
||||
systemPrompt: promptBuildResult?.systemPrompt ?? legacyResult?.systemPrompt,
|
||||
prependContext: [promptBuildResult?.prependContext, legacyResult?.prependContext]
|
||||
.filter((value): value is string => Boolean(value))
|
||||
.join("\n\n"),
|
||||
};
|
||||
{
|
||||
if (hookResult?.prependContext) {
|
||||
effectivePrompt = `${hookResult.prependContext}\n\n${params.prompt}`;
|
||||
|
||||
@@ -2,7 +2,6 @@ import type { AgentMessage } from "@mariozechner/pi-agent-core";
|
||||
import type { Api, AssistantMessage, Model } from "@mariozechner/pi-ai";
|
||||
import type { ThinkLevel } from "../../../auto-reply/thinking.js";
|
||||
import type { SessionSystemPromptReport } from "../../../config/sessions/types.js";
|
||||
import type { PluginHookBeforeAgentStartResult } from "../../../plugins/types.js";
|
||||
import type { MessagingToolSend } from "../../pi-embedded-messaging.js";
|
||||
import type { AuthStorage, ModelRegistry } from "../../pi-model-discovery.js";
|
||||
import type { NormalizedUsage } from "../../usage.js";
|
||||
@@ -20,8 +19,6 @@ export type EmbeddedRunAttemptParams = EmbeddedRunAttemptBase & {
|
||||
authStorage: AuthStorage;
|
||||
modelRegistry: ModelRegistry;
|
||||
thinkLevel: ThinkLevel;
|
||||
/** Pre-computed hook result from run.ts to avoid double-firing before_agent_start. */
|
||||
earlyHookResult?: PluginHookBeforeAgentStartResult;
|
||||
};
|
||||
|
||||
export type EmbeddedRunAttemptResult = {
|
||||
|
||||
Reference in New Issue
Block a user