refactor: centralize target errors and cache lookups
This commit is contained in:
@@ -150,6 +150,36 @@ function applyCrossContextMessageDecoration({
|
||||
return applied.message;
|
||||
}
|
||||
|
||||
async function maybeApplyCrossContextMarker(params: {
|
||||
cfg: ClawdbotConfig;
|
||||
channel: ChannelId;
|
||||
action: ChannelMessageActionName;
|
||||
target: string;
|
||||
toolContext?: ChannelThreadingToolContext;
|
||||
accountId?: string | null;
|
||||
args: Record<string, unknown>;
|
||||
message: string;
|
||||
preferEmbeds: boolean;
|
||||
}): Promise<string> {
|
||||
if (!shouldApplyCrossContextMarker(params.action) || !params.toolContext) {
|
||||
return params.message;
|
||||
}
|
||||
const decoration = await buildCrossContextDecoration({
|
||||
cfg: params.cfg,
|
||||
channel: params.channel,
|
||||
target: params.target,
|
||||
toolContext: params.toolContext,
|
||||
accountId: params.accountId ?? undefined,
|
||||
});
|
||||
if (!decoration) return params.message;
|
||||
return applyCrossContextMessageDecoration({
|
||||
params: params.args,
|
||||
message: params.message,
|
||||
decoration,
|
||||
preferEmbeds: params.preferEmbeds,
|
||||
});
|
||||
}
|
||||
|
||||
function readBooleanParam(params: Record<string, unknown>, key: string): boolean | undefined {
|
||||
const raw = params[key];
|
||||
if (typeof raw === "boolean") return raw;
|
||||
@@ -339,24 +369,17 @@ async function handleSendAction(ctx: ResolvedActionContext): Promise<MessageActi
|
||||
params.media = parsed.mediaUrls?.[0] || parsed.mediaUrl || undefined;
|
||||
}
|
||||
|
||||
const decoration =
|
||||
shouldApplyCrossContextMarker(action) && input.toolContext
|
||||
? await buildCrossContextDecoration({
|
||||
cfg,
|
||||
channel,
|
||||
target: to,
|
||||
toolContext: input.toolContext,
|
||||
accountId: accountId ?? undefined,
|
||||
})
|
||||
: null;
|
||||
if (decoration) {
|
||||
message = applyCrossContextMessageDecoration({
|
||||
params,
|
||||
message,
|
||||
decoration,
|
||||
preferEmbeds: true,
|
||||
});
|
||||
}
|
||||
message = await maybeApplyCrossContextMarker({
|
||||
cfg,
|
||||
channel,
|
||||
action,
|
||||
target: to,
|
||||
toolContext: input.toolContext,
|
||||
accountId,
|
||||
args: params,
|
||||
message,
|
||||
preferEmbeds: true,
|
||||
});
|
||||
|
||||
const mediaUrl = readStringParam(params, "media", { trim: false });
|
||||
const gifPlayback = readBooleanParam(params, "gifPlayback") ?? false;
|
||||
@@ -415,25 +438,18 @@ async function handlePollAction(ctx: ResolvedActionContext): Promise<MessageActi
|
||||
integer: true,
|
||||
});
|
||||
const maxSelections = allowMultiselect ? Math.max(2, options.length) : 1;
|
||||
const decoration =
|
||||
shouldApplyCrossContextMarker(action) && input.toolContext
|
||||
? await buildCrossContextDecoration({
|
||||
cfg,
|
||||
channel,
|
||||
target: to,
|
||||
toolContext: input.toolContext,
|
||||
accountId: accountId ?? undefined,
|
||||
})
|
||||
: null;
|
||||
if (decoration) {
|
||||
const base = typeof params.message === "string" ? params.message : "";
|
||||
applyCrossContextMessageDecoration({
|
||||
params,
|
||||
message: base,
|
||||
decoration,
|
||||
preferEmbeds: true,
|
||||
});
|
||||
}
|
||||
const base = typeof params.message === "string" ? params.message : "";
|
||||
await maybeApplyCrossContextMarker({
|
||||
cfg,
|
||||
channel,
|
||||
action,
|
||||
target: to,
|
||||
toolContext: input.toolContext,
|
||||
accountId,
|
||||
args: params,
|
||||
message: base,
|
||||
preferEmbeds: true,
|
||||
});
|
||||
|
||||
const poll = await executePollAction({
|
||||
ctx: {
|
||||
|
||||
8
src/infra/outbound/target-errors.ts
Normal file
8
src/infra/outbound/target-errors.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
export function missingTargetMessage(provider: string, hint?: string): string {
|
||||
const suffix = hint ? ` ${hint}` : "";
|
||||
return `Delivering to ${provider} requires target${suffix}`;
|
||||
}
|
||||
|
||||
export function missingTargetError(provider: string, hint?: string): Error {
|
||||
return new Error(missingTargetMessage(provider, hint));
|
||||
}
|
||||
78
src/infra/outbound/target-resolver.test.ts
Normal file
78
src/infra/outbound/target-resolver.test.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import type { ChannelDirectoryEntry } from "../../channels/plugins/types.js";
|
||||
import type { ClawdbotConfig } from "../../config/config.js";
|
||||
import { resetDirectoryCache, resolveMessagingTarget } from "./target-resolver.js";
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
listGroups: vi.fn(),
|
||||
listGroupsLive: vi.fn(),
|
||||
getChannelPlugin: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("../../channels/plugins/index.js", () => ({
|
||||
getChannelPlugin: (...args: unknown[]) => mocks.getChannelPlugin(...args),
|
||||
}));
|
||||
|
||||
describe("resolveMessagingTarget (directory fallback)", () => {
|
||||
const cfg = {} as ClawdbotConfig;
|
||||
|
||||
beforeEach(() => {
|
||||
mocks.listGroups.mockReset();
|
||||
mocks.listGroupsLive.mockReset();
|
||||
mocks.getChannelPlugin.mockReset();
|
||||
resetDirectoryCache();
|
||||
mocks.getChannelPlugin.mockReturnValue({
|
||||
directory: {
|
||||
listGroups: mocks.listGroups,
|
||||
listGroupsLive: mocks.listGroupsLive,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("uses live directory fallback and caches the result", async () => {
|
||||
const entry: ChannelDirectoryEntry = { id: "123456789", name: "support" };
|
||||
mocks.listGroups.mockResolvedValue([]);
|
||||
mocks.listGroupsLive.mockResolvedValue([entry]);
|
||||
|
||||
const first = await resolveMessagingTarget({
|
||||
cfg,
|
||||
channel: "discord",
|
||||
input: "support",
|
||||
});
|
||||
|
||||
expect(first.ok).toBe(true);
|
||||
if (first.ok) {
|
||||
expect(first.target.source).toBe("directory");
|
||||
expect(first.target.to).toBe("123456789");
|
||||
}
|
||||
expect(mocks.listGroups).toHaveBeenCalledTimes(1);
|
||||
expect(mocks.listGroupsLive).toHaveBeenCalledTimes(1);
|
||||
|
||||
const second = await resolveMessagingTarget({
|
||||
cfg,
|
||||
channel: "discord",
|
||||
input: "support",
|
||||
});
|
||||
|
||||
expect(second.ok).toBe(true);
|
||||
expect(mocks.listGroups).toHaveBeenCalledTimes(1);
|
||||
expect(mocks.listGroupsLive).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("skips directory lookup for direct ids", async () => {
|
||||
const result = await resolveMessagingTarget({
|
||||
cfg,
|
||||
channel: "discord",
|
||||
input: "123456789",
|
||||
});
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
if (result.ok) {
|
||||
expect(result.target.source).toBe("normalized");
|
||||
expect(result.target.to).toBe("123456789");
|
||||
}
|
||||
expect(mocks.listGroups).not.toHaveBeenCalled();
|
||||
expect(mocks.listGroupsLive).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -227,6 +227,7 @@ async function getDirectoryEntries(params: {
|
||||
source: "live",
|
||||
});
|
||||
directoryCache.set(liveKey, liveEntries, params.cfg);
|
||||
directoryCache.set(cacheKey, liveEntries, params.cfg);
|
||||
return liveEntries;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user