refactor(channels): share threading tool context

This commit is contained in:
Peter Steinberger
2026-02-15 18:30:34 +00:00
parent b2d8b95906
commit d9c891eb90

View File

@@ -8,7 +8,9 @@ import type {
ChannelAgentPromptAdapter,
ChannelMentionAdapter,
ChannelPlugin,
ChannelThreadingContext,
ChannelThreadingAdapter,
ChannelThreadingToolContext,
} from "./plugins/types.js";
import {
resolveChannelGroupRequireMention,
@@ -79,6 +81,21 @@ const formatLower = (allowFrom: Array<string | number>) =>
.map((entry) => String(entry).trim())
.filter(Boolean)
.map((entry) => entry.toLowerCase());
function buildDirectOrGroupThreadToolContext(params: {
context: ChannelThreadingContext;
hasRepliedRef: ChannelThreadingToolContext["hasRepliedRef"];
}): ChannelThreadingToolContext {
const isDirect = params.context.ChatType?.toLowerCase() === "direct";
const channelId =
(isDirect ? (params.context.From ?? params.context.To) : params.context.To)?.trim() ||
undefined;
return {
currentChannelId: channelId,
currentThreadTs: params.context.ReplyToId,
hasRepliedRef: params.hasRepliedRef,
};
}
// Channel docks: lightweight channel metadata/behavior for shared code paths.
//
// Rules:
@@ -404,16 +421,8 @@ const DOCKS: Record<ChatChannelId, ChannelDock> = {
.filter(Boolean),
},
threading: {
buildToolContext: ({ context, hasRepliedRef }) => {
const isDirect = context.ChatType?.toLowerCase() === "direct";
const channelId =
(isDirect ? (context.From ?? context.To) : context.To)?.trim() || undefined;
return {
currentChannelId: channelId,
currentThreadTs: context.ReplyToId,
hasRepliedRef,
};
},
buildToolContext: ({ context, hasRepliedRef }) =>
buildDirectOrGroupThreadToolContext({ context, hasRepliedRef }),
},
},
imessage: {
@@ -437,16 +446,8 @@ const DOCKS: Record<ChatChannelId, ChannelDock> = {
resolveToolPolicy: resolveIMessageGroupToolPolicy,
},
threading: {
buildToolContext: ({ context, hasRepliedRef }) => {
const isDirect = context.ChatType?.toLowerCase() === "direct";
const channelId =
(isDirect ? (context.From ?? context.To) : context.To)?.trim() || undefined;
return {
currentChannelId: channelId,
currentThreadTs: context.ReplyToId,
hasRepliedRef,
};
},
buildToolContext: ({ context, hasRepliedRef }) =>
buildDirectOrGroupThreadToolContext({ context, hasRepliedRef }),
},
},
};