mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-19 11:38:38 +00:00
refactor: centralize legacy hook prompt field stripping
This commit is contained in:
@@ -12,7 +12,11 @@ import { resolveUserPath } from "../utils.js";
|
|||||||
import { registerPluginCommand } from "./commands.js";
|
import { registerPluginCommand } from "./commands.js";
|
||||||
import { normalizePluginHttpPath } from "./http-path.js";
|
import { normalizePluginHttpPath } from "./http-path.js";
|
||||||
import type { PluginRuntime } from "./runtime/types.js";
|
import type { PluginRuntime } from "./runtime/types.js";
|
||||||
import { isPluginHookName, isPromptInjectionHookName } from "./types.js";
|
import {
|
||||||
|
isPluginHookName,
|
||||||
|
isPromptInjectionHookName,
|
||||||
|
stripPromptMutationFieldsFromLegacyHookResult,
|
||||||
|
} from "./types.js";
|
||||||
import type {
|
import type {
|
||||||
OpenClawPluginApi,
|
OpenClawPluginApi,
|
||||||
OpenClawPluginChannelRegistration,
|
OpenClawPluginChannelRegistration,
|
||||||
@@ -32,7 +36,6 @@ import type {
|
|||||||
PluginLogger,
|
PluginLogger,
|
||||||
PluginOrigin,
|
PluginOrigin,
|
||||||
PluginKind,
|
PluginKind,
|
||||||
PluginHookBeforeAgentStartResult,
|
|
||||||
PluginHookName,
|
PluginHookName,
|
||||||
PluginHookHandlerMap,
|
PluginHookHandlerMap,
|
||||||
PluginHookRegistration as TypedPluginHookRegistration,
|
PluginHookRegistration as TypedPluginHookRegistration,
|
||||||
@@ -146,24 +149,6 @@ type PluginTypedHookPolicy = {
|
|||||||
allowPromptInjection?: boolean;
|
allowPromptInjection?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
const stripPromptMutationFieldsFromLegacyHookResult = (
|
|
||||||
result: PluginHookBeforeAgentStartResult | void,
|
|
||||||
): PluginHookBeforeAgentStartResult | void => {
|
|
||||||
if (!result || typeof result !== "object") {
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
const {
|
|
||||||
systemPrompt: _systemPrompt,
|
|
||||||
prependContext: _prependContext,
|
|
||||||
prependSystemContext: _prependSystemContext,
|
|
||||||
appendSystemContext: _appendSystemContext,
|
|
||||||
...remaining
|
|
||||||
} = result;
|
|
||||||
return Object.keys(remaining).length > 0
|
|
||||||
? (remaining as PluginHookBeforeAgentStartResult)
|
|
||||||
: undefined;
|
|
||||||
};
|
|
||||||
|
|
||||||
const constrainLegacyPromptInjectionHook = (
|
const constrainLegacyPromptInjectionHook = (
|
||||||
handler: PluginHookHandlerMap["before_agent_start"],
|
handler: PluginHookHandlerMap["before_agent_start"],
|
||||||
): PluginHookHandlerMap["before_agent_start"] => {
|
): PluginHookHandlerMap["before_agent_start"] => {
|
||||||
|
|||||||
@@ -430,6 +430,22 @@ export type PluginHookBeforePromptBuildResult = {
|
|||||||
appendSystemContext?: string;
|
appendSystemContext?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const PLUGIN_PROMPT_MUTATION_RESULT_FIELDS = [
|
||||||
|
"systemPrompt",
|
||||||
|
"prependContext",
|
||||||
|
"prependSystemContext",
|
||||||
|
"appendSystemContext",
|
||||||
|
] as const satisfies readonly (keyof PluginHookBeforePromptBuildResult)[];
|
||||||
|
|
||||||
|
type MissingPluginPromptMutationResultFields = Exclude<
|
||||||
|
keyof PluginHookBeforePromptBuildResult,
|
||||||
|
(typeof PLUGIN_PROMPT_MUTATION_RESULT_FIELDS)[number]
|
||||||
|
>;
|
||||||
|
type AssertAllPluginPromptMutationResultFieldsListed =
|
||||||
|
MissingPluginPromptMutationResultFields extends never ? true : never;
|
||||||
|
const assertAllPluginPromptMutationResultFieldsListed: AssertAllPluginPromptMutationResultFieldsListed = true;
|
||||||
|
void assertAllPluginPromptMutationResultFieldsListed;
|
||||||
|
|
||||||
// before_agent_start hook (legacy compatibility: combines both phases)
|
// before_agent_start hook (legacy compatibility: combines both phases)
|
||||||
export type PluginHookBeforeAgentStartEvent = {
|
export type PluginHookBeforeAgentStartEvent = {
|
||||||
prompt: string;
|
prompt: string;
|
||||||
@@ -440,6 +456,26 @@ export type PluginHookBeforeAgentStartEvent = {
|
|||||||
export type PluginHookBeforeAgentStartResult = PluginHookBeforePromptBuildResult &
|
export type PluginHookBeforeAgentStartResult = PluginHookBeforePromptBuildResult &
|
||||||
PluginHookBeforeModelResolveResult;
|
PluginHookBeforeModelResolveResult;
|
||||||
|
|
||||||
|
export type PluginHookBeforeAgentStartOverrideResult = Omit<
|
||||||
|
PluginHookBeforeAgentStartResult,
|
||||||
|
keyof PluginHookBeforePromptBuildResult
|
||||||
|
>;
|
||||||
|
|
||||||
|
export const stripPromptMutationFieldsFromLegacyHookResult = (
|
||||||
|
result: PluginHookBeforeAgentStartResult | void,
|
||||||
|
): PluginHookBeforeAgentStartOverrideResult | void => {
|
||||||
|
if (!result || typeof result !== "object") {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
const remaining: Partial<PluginHookBeforeAgentStartResult> = { ...result };
|
||||||
|
for (const field of PLUGIN_PROMPT_MUTATION_RESULT_FIELDS) {
|
||||||
|
delete remaining[field];
|
||||||
|
}
|
||||||
|
return Object.keys(remaining).length > 0
|
||||||
|
? (remaining as PluginHookBeforeAgentStartOverrideResult)
|
||||||
|
: undefined;
|
||||||
|
};
|
||||||
|
|
||||||
// llm_input hook
|
// llm_input hook
|
||||||
export type PluginHookLlmInputEvent = {
|
export type PluginHookLlmInputEvent = {
|
||||||
runId: string;
|
runId: string;
|
||||||
|
|||||||
Reference in New Issue
Block a user