Files
openclaw/src/channels/reply-prefix.ts
Peter Steinberger 00d8d7ead0 refactor: extract normalization core package
Extract shared normalization/coercion helpers into private @openclaw/normalization-core workspace package while preserving existing plugin SDK helper subpaths.\n\nAlso keeps direct normalization-core imports internal, wires UI/build/loader resolution, and replaces the slow PR network CodeQL lane with a fast added-line boundary scan while retaining full CodeQL for scheduled/manual runs.\n\nVerification: local moved tests, plugin SDK boundary tests, extension loader tests, agents-support shard, UI build/test, build artifacts, lint, workflow guards, autoreview, and GitHub CI passed on PR head 963d893715.
2026-05-31 01:33:00 +01:00

68 lines
2.2 KiB
TypeScript

import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce";
import { resolveAgentIdentity, resolveEffectiveMessagesConfig } from "../agents/identity.js";
import type { GetReplyOptions } from "../auto-reply/get-reply-options.types.js";
import {
extractShortModelName,
type ResponsePrefixContext,
} from "../auto-reply/reply/response-prefix-template.js";
import type { OpenClawConfig } from "../config/types.openclaw.js";
type ModelSelectionContext = Parameters<NonNullable<GetReplyOptions["onModelSelected"]>>[0];
export type ReplyPrefixContextBundle = {
prefixContext: ResponsePrefixContext;
responsePrefix?: string;
responsePrefixContextProvider: () => ResponsePrefixContext;
onModelSelected: (ctx: ModelSelectionContext) => void;
};
export type ReplyPrefixOptions = Pick<
ReplyPrefixContextBundle,
"responsePrefix" | "responsePrefixContextProvider" | "onModelSelected"
>;
export function createReplyPrefixContext(params: {
cfg: OpenClawConfig;
agentId: string;
channel?: string;
accountId?: string;
}): ReplyPrefixContextBundle {
const { cfg, agentId } = params;
const prefixContext: ResponsePrefixContext = {
identityName: normalizeOptionalString(resolveAgentIdentity(cfg, agentId)?.name),
};
const onModelSelected = (ctx: ModelSelectionContext) => {
// Mutate the object directly instead of reassigning to ensure closures see updates.
prefixContext.provider = ctx.provider;
prefixContext.model = extractShortModelName(ctx.model);
prefixContext.modelFull = `${ctx.provider}/${ctx.model}`;
prefixContext.thinkingLevel = ctx.thinkLevel ?? "off";
};
return {
prefixContext,
responsePrefix: resolveEffectiveMessagesConfig(cfg, agentId, {
channel: params.channel,
accountId: params.accountId,
}).responsePrefix,
responsePrefixContextProvider: () => prefixContext,
onModelSelected,
};
}
export function createReplyPrefixOptions(params: {
cfg: OpenClawConfig;
agentId: string;
channel?: string;
accountId?: string;
}): ReplyPrefixOptions {
const { responsePrefix, responsePrefixContextProvider, onModelSelected } =
createReplyPrefixContext(params);
return {
responsePrefix,
responsePrefixContextProvider,
onModelSelected,
};
}