Skip to content

Commit 6eb04c8

Browse files
committed
perf(outbound): isolate id-like target resolution
1 parent 8628d05 commit 6eb04c8

5 files changed

Lines changed: 39 additions & 20 deletions

File tree

src/cron/isolated-agent/delivery-target.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ vi.mock("../../infra/outbound/channel-selection.runtime.js", () => ({
2323
.mockResolvedValue({ channel: "telegram", configured: ["telegram"] }),
2424
}));
2525

26-
vi.mock("../../infra/outbound/target-resolver.js", () => ({
26+
vi.mock("../../infra/outbound/target-id-resolution.js", () => ({
2727
maybeResolveIdLikeTarget: vi.fn(),
2828
}));
2929

@@ -40,13 +40,13 @@ const mockedModuleIds = [
4040
"../../config/sessions/store-load.js",
4141
"../../infra/outbound/channel-selection.runtime.js",
4242
"../../infra/outbound/targets.runtime.js",
43-
"../../infra/outbound/target-resolver.js",
43+
"../../infra/outbound/target-id-resolution.js",
4444
"../../pairing/pairing-store.js",
4545
];
4646

4747
import { loadSessionStore } from "../../config/sessions/store-load.js";
4848
import { resolveMessageChannelSelection } from "../../infra/outbound/channel-selection.runtime.js";
49-
import { maybeResolveIdLikeTarget } from "../../infra/outbound/target-resolver.js";
49+
import { maybeResolveIdLikeTarget } from "../../infra/outbound/target-id-resolution.js";
5050
import { resolveOutboundTarget } from "../../infra/outbound/targets.runtime.js";
5151
import { readChannelAllowFromStoreSync } from "../../pairing/pairing-store.js";
5252
import { resolveDeliveryTarget } from "./delivery-target.js";

src/cron/isolated-agent/delivery-target.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { resolveStorePath } from "../../config/sessions/paths.js";
55
import { loadSessionStore } from "../../config/sessions/store-load.js";
66
import type { OpenClawConfig } from "../../config/types.openclaw.js";
77
import { formatErrorMessage } from "../../infra/errors.js";
8-
import { maybeResolveIdLikeTarget } from "../../infra/outbound/target-resolver.js";
8+
import { maybeResolveIdLikeTarget } from "../../infra/outbound/target-id-resolution.js";
99
import { tryResolveLoadedOutboundTarget } from "../../infra/outbound/targets-loaded.js";
1010
import { resolveSessionDeliveryTarget } from "../../infra/outbound/targets-session.js";
1111
import type { OutboundChannel } from "../../infra/outbound/targets.js";
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import type { ChannelDirectoryEntryKind, ChannelId } from "../../channels/plugins/types.public.js";
2+
import type { OpenClawConfig } from "../../config/types.openclaw.js";
3+
import { maybeResolvePluginMessagingTarget } from "./target-normalization.js";
4+
5+
export type ResolvedIdLikeTarget = {
6+
to: string;
7+
kind: ChannelDirectoryEntryKind | "channel";
8+
display?: string;
9+
source: "normalized" | "directory";
10+
};
11+
12+
export async function maybeResolveIdLikeTarget(params: {
13+
cfg: OpenClawConfig;
14+
channel: ChannelId;
15+
input: string;
16+
accountId?: string | null;
17+
preferredKind?: ChannelDirectoryEntryKind | "channel";
18+
}): Promise<ResolvedIdLikeTarget | undefined> {
19+
const target = await maybeResolvePluginMessagingTarget({
20+
...params,
21+
requireIdLike: true,
22+
});
23+
if (!target) {
24+
return undefined;
25+
}
26+
return target;
27+
}

src/infra/outbound/target-resolver.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ vi.mock("../../channels/plugins/index.js", () => ({
2222
normalizeChannelId: (value: string) => value,
2323
}));
2424

25+
vi.mock("../../channels/plugins/registry-loaded-read.js", () => ({
26+
getLoadedChannelPluginForRead: (...args: unknown[]) => mocks.getChannelPlugin(...args),
27+
}));
28+
2529
vi.mock("../../plugins/runtime.js", () => ({
2630
getActivePluginChannelRegistry: () => null,
2731
getActivePluginRegistry: () => null,

src/infra/outbound/target-resolver.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { defaultRuntime, type RuntimeEnv } from "../../runtime.js";
99
import { normalizeLowercaseStringOrEmpty } from "../../shared/string-coerce.js";
1010
import { buildDirectoryCacheKey, DirectoryCache } from "./directory-cache.js";
1111
import { ambiguousTargetError, unknownTargetError } from "./target-errors.js";
12+
import { maybeResolveIdLikeTarget, type ResolvedIdLikeTarget } from "./target-id-resolution.js";
1213
import {
1314
buildTargetResolverSignature,
1415
looksLikeTargetId,
@@ -34,11 +35,13 @@ export type ResolveMessagingTargetResult =
3435
| { ok: false; error: Error; candidates?: ChannelDirectoryEntry[] };
3536

3637
function asResolvedMessagingTarget(
37-
target: Awaited<ReturnType<typeof maybeResolvePluginMessagingTarget>>,
38+
target: Awaited<ReturnType<typeof maybeResolvePluginMessagingTarget>> | ResolvedIdLikeTarget,
3839
): ResolvedMessagingTarget | undefined {
3940
return target;
4041
}
4142

43+
export { maybeResolveIdLikeTarget } from "./target-id-resolution.js";
44+
4245
export async function resolveChannelTarget(params: {
4346
cfg: OpenClawConfig;
4447
channel: ChannelId;
@@ -50,21 +53,6 @@ export async function resolveChannelTarget(params: {
5053
return resolveMessagingTarget(params);
5154
}
5255

53-
export async function maybeResolveIdLikeTarget(params: {
54-
cfg: OpenClawConfig;
55-
channel: ChannelId;
56-
input: string;
57-
accountId?: string | null;
58-
preferredKind?: TargetResolveKind;
59-
}): Promise<ResolvedMessagingTarget | undefined> {
60-
return asResolvedMessagingTarget(
61-
await maybeResolvePluginMessagingTarget({
62-
...params,
63-
requireIdLike: true,
64-
}),
65-
);
66-
}
67-
6856
const CACHE_TTL_MS = 30 * 60 * 1000;
6957
const directoryCache = new DirectoryCache<ChannelDirectoryEntry[]>(CACHE_TTL_MS);
7058

0 commit comments

Comments
 (0)