mirror of https://github.com/openclaw/openclaw.git
83 lines
2.5 KiB
TypeScript
83 lines
2.5 KiB
TypeScript
import { resolveEffectiveMessagesConfig, resolveIdentityName } from "../agents/identity.js";
|
|
import {
|
|
extractShortModelName,
|
|
type ResponsePrefixContext,
|
|
} from "../auto-reply/reply/response-prefix-template.js";
|
|
import type { GetReplyOptions } from "../auto-reply/types.js";
|
|
import { getChannelPlugin } from "../channels/plugins/index.js";
|
|
import type { OpenClawConfig } from "../config/config.js";
|
|
|
|
type ModelSelectionContext = Parameters<NonNullable<GetReplyOptions["onModelSelected"]>>[0];
|
|
|
|
export type ReplyPrefixContextBundle = {
|
|
prefixContext: ResponsePrefixContext;
|
|
responsePrefix?: string;
|
|
enableSlackInteractiveReplies?: boolean;
|
|
responsePrefixContextProvider: () => ResponsePrefixContext;
|
|
onModelSelected: (ctx: ModelSelectionContext) => void;
|
|
};
|
|
|
|
export type ReplyPrefixOptions = Pick<
|
|
ReplyPrefixContextBundle,
|
|
| "responsePrefix"
|
|
| "enableSlackInteractiveReplies"
|
|
| "responsePrefixContextProvider"
|
|
| "onModelSelected"
|
|
>;
|
|
|
|
export function createReplyPrefixContext(params: {
|
|
cfg: OpenClawConfig;
|
|
agentId: string;
|
|
channel?: string;
|
|
accountId?: string;
|
|
}): ReplyPrefixContextBundle {
|
|
const { cfg, agentId } = params;
|
|
const prefixContext: ResponsePrefixContext = {
|
|
identityName: resolveIdentityName(cfg, agentId),
|
|
};
|
|
|
|
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,
|
|
enableSlackInteractiveReplies: params.channel
|
|
? (getChannelPlugin(params.channel)?.messaging?.enableInteractiveReplies?.({
|
|
cfg,
|
|
accountId: params.accountId,
|
|
}) ?? undefined)
|
|
: undefined,
|
|
responsePrefixContextProvider: () => prefixContext,
|
|
onModelSelected,
|
|
};
|
|
}
|
|
|
|
export function createReplyPrefixOptions(params: {
|
|
cfg: OpenClawConfig;
|
|
agentId: string;
|
|
channel?: string;
|
|
accountId?: string;
|
|
}): ReplyPrefixOptions {
|
|
const {
|
|
responsePrefix,
|
|
enableSlackInteractiveReplies,
|
|
responsePrefixContextProvider,
|
|
onModelSelected,
|
|
} = createReplyPrefixContext(params);
|
|
return {
|
|
responsePrefix,
|
|
enableSlackInteractiveReplies,
|
|
responsePrefixContextProvider,
|
|
onModelSelected,
|
|
};
|
|
}
|