Skip to content

Commit 6b9d883

Browse files
committed
refactor: centralize legacy hook prompt field stripping
1 parent 6aefd1c commit 6b9d883

File tree

2 files changed

+41
-20
lines changed

2 files changed

+41
-20
lines changed

src/plugins/registry.ts

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ import { resolveUserPath } from "../utils.js";
1212
import { registerPluginCommand } from "./commands.js";
1313
import { normalizePluginHttpPath } from "./http-path.js";
1414
import type { PluginRuntime } from "./runtime/types.js";
15-
import { isPluginHookName, isPromptInjectionHookName } from "./types.js";
15+
import {
16+
isPluginHookName,
17+
isPromptInjectionHookName,
18+
stripPromptMutationFieldsFromLegacyHookResult,
19+
} from "./types.js";
1620
import type {
1721
OpenClawPluginApi,
1822
OpenClawPluginChannelRegistration,
@@ -32,7 +36,6 @@ import type {
3236
PluginLogger,
3337
PluginOrigin,
3438
PluginKind,
35-
PluginHookBeforeAgentStartResult,
3639
PluginHookName,
3740
PluginHookHandlerMap,
3841
PluginHookRegistration as TypedPluginHookRegistration,
@@ -146,24 +149,6 @@ type PluginTypedHookPolicy = {
146149
allowPromptInjection?: boolean;
147150
};
148151

149-
const stripPromptMutationFieldsFromLegacyHookResult = (
150-
result: PluginHookBeforeAgentStartResult | void,
151-
): PluginHookBeforeAgentStartResult | void => {
152-
if (!result || typeof result !== "object") {
153-
return result;
154-
}
155-
const {
156-
systemPrompt: _systemPrompt,
157-
prependContext: _prependContext,
158-
prependSystemContext: _prependSystemContext,
159-
appendSystemContext: _appendSystemContext,
160-
...remaining
161-
} = result;
162-
return Object.keys(remaining).length > 0
163-
? (remaining as PluginHookBeforeAgentStartResult)
164-
: undefined;
165-
};
166-
167152
const constrainLegacyPromptInjectionHook = (
168153
handler: PluginHookHandlerMap["before_agent_start"],
169154
): PluginHookHandlerMap["before_agent_start"] => {

src/plugins/types.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,22 @@ export type PluginHookBeforePromptBuildResult = {
430430
appendSystemContext?: string;
431431
};
432432

433+
export const PLUGIN_PROMPT_MUTATION_RESULT_FIELDS = [
434+
"systemPrompt",
435+
"prependContext",
436+
"prependSystemContext",
437+
"appendSystemContext",
438+
] as const satisfies readonly (keyof PluginHookBeforePromptBuildResult)[];
439+
440+
type MissingPluginPromptMutationResultFields = Exclude<
441+
keyof PluginHookBeforePromptBuildResult,
442+
(typeof PLUGIN_PROMPT_MUTATION_RESULT_FIELDS)[number]
443+
>;
444+
type AssertAllPluginPromptMutationResultFieldsListed =
445+
MissingPluginPromptMutationResultFields extends never ? true : never;
446+
const assertAllPluginPromptMutationResultFieldsListed: AssertAllPluginPromptMutationResultFieldsListed = true;
447+
void assertAllPluginPromptMutationResultFieldsListed;
448+
433449
// before_agent_start hook (legacy compatibility: combines both phases)
434450
export type PluginHookBeforeAgentStartEvent = {
435451
prompt: string;
@@ -440,6 +456,26 @@ export type PluginHookBeforeAgentStartEvent = {
440456
export type PluginHookBeforeAgentStartResult = PluginHookBeforePromptBuildResult &
441457
PluginHookBeforeModelResolveResult;
442458

459+
export type PluginHookBeforeAgentStartOverrideResult = Omit<
460+
PluginHookBeforeAgentStartResult,
461+
keyof PluginHookBeforePromptBuildResult
462+
>;
463+
464+
export const stripPromptMutationFieldsFromLegacyHookResult = (
465+
result: PluginHookBeforeAgentStartResult | void,
466+
): PluginHookBeforeAgentStartOverrideResult | void => {
467+
if (!result || typeof result !== "object") {
468+
return result;
469+
}
470+
const remaining: Partial<PluginHookBeforeAgentStartResult> = { ...result };
471+
for (const field of PLUGIN_PROMPT_MUTATION_RESULT_FIELDS) {
472+
delete remaining[field];
473+
}
474+
return Object.keys(remaining).length > 0
475+
? (remaining as PluginHookBeforeAgentStartOverrideResult)
476+
: undefined;
477+
};
478+
443479
// llm_input hook
444480
export type PluginHookLlmInputEvent = {
445481
runId: string;

0 commit comments

Comments
 (0)