mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 00:41:25 +00:00
* fix(plugins): apply before_agent_start hook systemPrompt to session (#14583) * fix(plugins): apply legacy systemPrompt override and add changelog credit --------- Co-authored-by: yinghaosang <yinghaosang@users.noreply.github.com> Co-authored-by: Vincent Koc <vincentkoc@ieee.org>
This commit is contained in:
@@ -560,7 +560,7 @@ export async function runEmbeddedAttempt(
|
||||
tools,
|
||||
});
|
||||
const systemPromptOverride = createSystemPromptOverride(appendPrompt);
|
||||
const systemPromptText = systemPromptOverride();
|
||||
let systemPromptText = systemPromptOverride();
|
||||
|
||||
const sessionLock = await acquireSessionWriteLock({
|
||||
sessionFile: params.sessionFile,
|
||||
@@ -1038,6 +1038,13 @@ export async function runEmbeddedAttempt(
|
||||
`hooks: prepended context to prompt (${hookResult.prependContext.length} chars)`,
|
||||
);
|
||||
}
|
||||
const legacySystemPrompt =
|
||||
typeof hookResult?.systemPrompt === "string" ? hookResult.systemPrompt.trim() : "";
|
||||
if (legacySystemPrompt) {
|
||||
applySystemPromptOverrideToSession(activeSession, legacySystemPrompt);
|
||||
systemPromptText = legacySystemPrompt;
|
||||
log.debug(`hooks: applied systemPrompt override (${legacySystemPrompt.length} chars)`);
|
||||
}
|
||||
}
|
||||
|
||||
log.debug(`embedded run prompt start: runId=${params.runId} sessionId=${params.sessionId}`);
|
||||
|
||||
51
src/agents/pi-embedded-runner/system-prompt.test.ts
Normal file
51
src/agents/pi-embedded-runner/system-prompt.test.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import type { AgentSession } from "@mariozechner/pi-coding-agent";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { applySystemPromptOverrideToSession, createSystemPromptOverride } from "./system-prompt.js";
|
||||
|
||||
function createMockSession() {
|
||||
const setSystemPrompt = vi.fn();
|
||||
const session = {
|
||||
agent: { setSystemPrompt },
|
||||
} as unknown as AgentSession;
|
||||
return { session, setSystemPrompt };
|
||||
}
|
||||
|
||||
describe("applySystemPromptOverrideToSession", () => {
|
||||
it("applies a string override to the session system prompt", () => {
|
||||
const { session, setSystemPrompt } = createMockSession();
|
||||
const prompt = "You are a helpful assistant with custom context.";
|
||||
|
||||
applySystemPromptOverrideToSession(session, prompt);
|
||||
|
||||
expect(setSystemPrompt).toHaveBeenCalledWith(prompt);
|
||||
const mutable = session as unknown as { _baseSystemPrompt?: string };
|
||||
expect(mutable._baseSystemPrompt).toBe(prompt);
|
||||
});
|
||||
|
||||
it("trims whitespace from string overrides", () => {
|
||||
const { session, setSystemPrompt } = createMockSession();
|
||||
|
||||
applySystemPromptOverrideToSession(session, " padded prompt ");
|
||||
|
||||
expect(setSystemPrompt).toHaveBeenCalledWith("padded prompt");
|
||||
});
|
||||
|
||||
it("applies a function override to the session system prompt", () => {
|
||||
const { session, setSystemPrompt } = createMockSession();
|
||||
const override = createSystemPromptOverride("function-based prompt");
|
||||
|
||||
applySystemPromptOverrideToSession(session, override);
|
||||
|
||||
expect(setSystemPrompt).toHaveBeenCalledWith("function-based prompt");
|
||||
});
|
||||
|
||||
it("sets _rebuildSystemPrompt that returns the override", () => {
|
||||
const { session } = createMockSession();
|
||||
applySystemPromptOverrideToSession(session, "rebuild test");
|
||||
|
||||
const mutable = session as unknown as {
|
||||
_rebuildSystemPrompt?: (toolNames: string[]) => string;
|
||||
};
|
||||
expect(mutable._rebuildSystemPrompt?.(["tool1"])).toBe("rebuild test");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user