refactor: share reply prefix context
This commit is contained in:
41
src/channels/reply-prefix.ts
Normal file
41
src/channels/reply-prefix.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { resolveEffectiveMessagesConfig, resolveIdentityName } from "../agents/identity.js";
|
||||
import type { ClawdbotConfig } from "../config/config.js";
|
||||
import type { GetReplyOptions } from "../auto-reply/types.js";
|
||||
import {
|
||||
extractShortModelName,
|
||||
type ResponsePrefixContext,
|
||||
} from "../auto-reply/reply/response-prefix-template.js";
|
||||
|
||||
type ModelSelectionContext = Parameters<NonNullable<GetReplyOptions["onModelSelected"]>>[0];
|
||||
|
||||
export type ReplyPrefixContextBundle = {
|
||||
prefixContext: ResponsePrefixContext;
|
||||
responsePrefix?: string;
|
||||
responsePrefixContextProvider: () => ResponsePrefixContext;
|
||||
onModelSelected: (ctx: ModelSelectionContext) => void;
|
||||
};
|
||||
|
||||
export function createReplyPrefixContext(params: {
|
||||
cfg: ClawdbotConfig;
|
||||
agentId: 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).responsePrefix,
|
||||
responsePrefixContextProvider: () => prefixContext,
|
||||
onModelSelected,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user