Skip to content

Commit 385c242

Browse files
ampagentsteipete
andcommitted
perf(discord): reuse prepared outbound account
Co-authored-by: Peter Steinberger <[email protected]>
1 parent 24c4637 commit 385c242

6 files changed

Lines changed: 70 additions & 43 deletions

File tree

‎extensions/discord/src/client.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,34 @@ describe("createDiscordClient", () => {
3333
await expect(request(operation, "send")).resolves.toBe("sent");
3434
expect(operation).toHaveBeenCalledTimes(3);
3535
});
36+
37+
it("keeps explicit-token retries bound to the REST account", async () => {
38+
registerGateway("default", { isConnected: false } as GatewayPlugin);
39+
registerGateway("ops", { isConnected: true } as GatewayPlugin);
40+
const client = createDiscordClient({
41+
cfg: {
42+
channels: {
43+
discord: {
44+
defaultAccount: "ops",
45+
accounts: { ops: { token: "configured-token" } },
46+
},
47+
},
48+
},
49+
token: "explicit-token",
50+
rest: {} as RequestClient,
51+
retry: { attempts: 3, minDelayMs: 0, maxDelayMs: 0, jitter: 0 },
52+
});
53+
const operation = vi
54+
.fn()
55+
.mockRejectedValueOnce(new TypeError("fetch failed"))
56+
.mockRejectedValueOnce(new TypeError("fetch failed"))
57+
.mockRejectedValueOnce(new TypeError("fetch failed"))
58+
.mockResolvedValue("sent");
59+
60+
expect(client.account.accountId).toBe("ops");
61+
await expect(client.request(operation, "send")).resolves.toBe("sent");
62+
expect(operation).toHaveBeenCalledTimes(4);
63+
});
3664
});
3765

3866
describe("createDiscordRestClient", () => {

‎extensions/discord/src/client.ts‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { RequestClient } from "./internal/discord.js";
1414
import { getGateway } from "./monitor/gateway-registry.js";
1515
import { resolveDiscordProxyFetchForAccount } from "./proxy-fetch.js";
1616
import { createDiscordRequestClient } from "./proxy-request-client.js";
17-
import { createDiscordRetryRunner, type DiscordRetryRunner } from "./retry.js";
17+
import { createDiscordRetryRunner } from "./retry.js";
1818
import type { DiscordRuntimeAccountContext } from "./send.types.js";
1919
import { normalizeDiscordToken } from "./token.js";
2020

@@ -139,21 +139,22 @@ export function createDiscordRestClient(opts: DiscordClientOpts) {
139139
return { token, rest, account };
140140
}
141141

142-
export function createDiscordClient(opts: DiscordClientOpts): {
143-
token: string;
144-
rest: RequestClient;
145-
request: DiscordRetryRunner;
146-
} {
147-
const { token, rest, account } = createDiscordRestClient(opts);
142+
export function createDiscordClient(opts: DiscordClientOpts) {
143+
const { token, rest, account: restAccount } = createDiscordRestClient(opts);
144+
const account = normalizeDiscordToken(opts.token, "channels.discord.token")
145+
? resolveDiscordAccount({ cfg: opts.cfg, accountId: opts.accountId })
146+
: restAccount;
147+
// Explicit-token REST clients retain their normalized gateway identity; outbound
148+
// projection consumers use the canonical configured account returned below.
148149
const request = createDiscordRetryRunner({
149150
retry: opts.retry,
150151
verbose: opts.verbose,
151152
isGatewayDisconnected: () => {
152-
const gateway = getGateway(account.accountId);
153+
const gateway = getGateway(restAccount.accountId);
153154
return gateway !== undefined && !gateway.isConnected;
154155
},
155156
});
156-
return { token, rest, request };
157+
return { token, rest, request, account };
157158
}
158159

159160
export function resolveDiscordRest(opts: DiscordClientOpts) {

‎extensions/discord/src/recipient-resolution.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,13 @@ export async function parseAndResolveRecipient(
4242
export async function parseAndResolveChannelRecipient(
4343
raw: string,
4444
cfg: OpenClawConfig,
45-
accountId?: string,
45+
accountId: string,
4646
): Promise<DiscordRecipient> {
47-
return await parseAndResolveRecipient(raw, cfg, accountId, {
48-
defaultKind: "channel",
49-
});
47+
const resolvedCfg = requireRuntimeConfig(cfg, "Discord recipient resolution");
48+
const resolved = await parseAndResolveDiscordTarget(
49+
raw,
50+
{ cfg: resolvedCfg, accountId },
51+
{ defaultKind: "channel" },
52+
);
53+
return { kind: resolved.kind, id: resolved.id };
5054
}

‎extensions/discord/src/send.components.ts‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import type { OutboundMediaAccess } from "openclaw/plugin-sdk/media-runtime";
77
import { requireRuntimeConfig } from "openclaw/plugin-sdk/plugin-config-runtime";
88
import type { ChunkMode } from "openclaw/plugin-sdk/reply-chunking";
99
import { uniqueStrings } from "openclaw/plugin-sdk/string-coerce-runtime";
10-
import { resolveDiscordAccount } from "./accounts.js";
1110
import { registerDiscordComponentEntries } from "./components-registry.js";
1211
import {
1312
buildDiscordComponentMessage,
@@ -300,9 +299,8 @@ export async function sendDiscordComponentMessage(
300299
}
301300

302301
const cfg = requireRuntimeConfig(opts.cfg, "Discord component send");
303-
const accountInfo = resolveDiscordAccount({ cfg, accountId: opts.accountId });
304-
const { token, rest, request } = createDiscordClient({ ...opts, cfg });
305-
const recipient = await parseAndResolveChannelRecipient(to, cfg, opts.accountId);
302+
const { token, rest, request, account: accountInfo } = createDiscordClient({ ...opts, cfg });
303+
const recipient = await parseAndResolveChannelRecipient(to, cfg, accountInfo.accountId);
306304
const { channelId } = await resolveChannelId(rest, recipient, request);
307305

308306
const channel = await resolveDiscordChannel(rest, channelId);
@@ -374,9 +372,8 @@ export async function editDiscordComponentMessage(
374372
opts: DiscordComponentSendOpts,
375373
): Promise<DiscordSendResult> {
376374
const cfg = requireRuntimeConfig(opts.cfg, "Discord component edit");
377-
const accountInfo = resolveDiscordAccount({ cfg, accountId: opts.accountId });
378-
const { token, rest, request } = createDiscordClient({ ...opts, cfg });
379-
const recipient = await parseAndResolveChannelRecipient(to, cfg, opts.accountId);
375+
const { token, rest, request, account: accountInfo } = createDiscordClient({ ...opts, cfg });
376+
const recipient = await parseAndResolveChannelRecipient(to, cfg, accountInfo.accountId);
380377
const { channelId } = await resolveChannelId(rest, recipient, request);
381378
const { body, buildResult } = await buildDiscordComponentPayload({
382379
spec,

‎extensions/discord/src/send.outbound.ts‎

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { resolveChunkMode, type ChunkMode } from "openclaw/plugin-sdk/reply-chun
1010
import type { RetryConfig } from "openclaw/plugin-sdk/retry-runtime";
1111
import { normalizeOptionalString } from "openclaw/plugin-sdk/string-coerce-runtime";
1212
import { truncateUtf16Safe } from "openclaw/plugin-sdk/text-utility-runtime";
13-
import { resolveDiscordAccount } from "./accounts.js";
1413
import { createChannelMessage, createThread, type RequestClient } from "./internal/discord.js";
1514
import { renderDiscordMarkdown } from "./markdown.js";
1615
import { rewriteDiscordKnownMentions } from "./mentions.js";
@@ -159,12 +158,17 @@ function toDiscordSendResult(
159158
async function resolveDiscordSendTarget(
160159
to: string,
161160
opts: DiscordSendOpts,
162-
): Promise<{ rest: RequestClient; request: DiscordClientRequest; channelId: string }> {
161+
): Promise<{
162+
rest: RequestClient;
163+
request: DiscordClientRequest;
164+
channelId: string;
165+
account: ReturnType<typeof createDiscordClient>["account"];
166+
}> {
163167
const cfg = requireRuntimeConfig(opts.cfg, "Discord send target resolution");
164-
const { rest, request } = createDiscordClient({ ...opts, cfg });
165-
const recipient = await parseAndResolveChannelRecipient(to, cfg, opts.accountId);
168+
const { rest, request, account } = createDiscordClient({ ...opts, cfg });
169+
const recipient = await parseAndResolveChannelRecipient(to, cfg, account.accountId);
166170
const { channelId } = await resolveChannelId(rest, recipient, request);
167-
return { rest, request, channelId };
171+
return { rest, request, channelId, account };
168172
}
169173

170174
export async function sendMessageDiscord(
@@ -173,10 +177,7 @@ export async function sendMessageDiscord(
173177
opts: DiscordSendOpts,
174178
): Promise<DiscordSendResult> {
175179
const cfg = requireRuntimeConfig(opts.cfg, "Discord send");
176-
const accountInfo = resolveDiscordAccount({
177-
cfg,
178-
accountId: opts.accountId,
179-
});
180+
const { token, rest, request, account: accountInfo } = createDiscordClient({ ...opts, cfg });
180181
const tableMode = resolveMarkdownTableMode({
181182
cfg,
182183
channel: "discord",
@@ -202,8 +203,7 @@ export async function sendMessageDiscord(
202203
accountId: accountInfo.accountId,
203204
mentionAliases: accountInfo.config.mentionAliases,
204205
});
205-
const { token, rest, request } = createDiscordClient({ ...opts, cfg });
206-
const recipient = await parseAndResolveChannelRecipient(to, cfg, opts.accountId);
206+
const recipient = await parseAndResolveChannelRecipient(to, cfg, accountInfo.accountId);
207207
const { channelId } = await resolveChannelId(rest, recipient, request);
208208

209209
// Forum/Media channels reject POST /messages; auto-create a thread post instead.
@@ -490,12 +490,13 @@ async function resolveDiscordStructuredSendContext(
490490
rewrittenContent?: string;
491491
suppressEmbeds: boolean;
492492
}> {
493-
const cfg = requireRuntimeConfig(opts.cfg, "Discord structured send");
494-
const accountInfo = resolveDiscordAccount({
495-
cfg,
496-
accountId: opts.accountId,
497-
});
498-
const { rest, request, channelId } = await resolveDiscordSendTarget(to, opts);
493+
requireRuntimeConfig(opts.cfg, "Discord structured send");
494+
const {
495+
rest,
496+
request,
497+
channelId,
498+
account: accountInfo,
499+
} = await resolveDiscordSendTarget(to, opts);
499500
const content = opts.content?.trim();
500501
const rewrittenContent = content
501502
? rewriteDiscordKnownMentions(content, {

‎extensions/discord/src/send.voice.ts‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
import { requireRuntimeConfig } from "openclaw/plugin-sdk/plugin-config-runtime";
1212
import { withTempWorkspace, resolvePreferredOpenClawTmpDir } from "openclaw/plugin-sdk/temp-path";
1313
import { loadWebMediaRaw } from "openclaw/plugin-sdk/web-media";
14-
import { resolveDiscordAccount } from "./accounts.js";
1514
import type { RequestClient } from "./internal/discord.js";
1615
import { parseAndResolveChannelRecipient } from "./recipient-resolution.js";
1716
import type { DiscordReplyReference } from "./reply-reference.js";
@@ -106,15 +105,12 @@ export async function sendVoiceMessageDiscord(
106105
let channelId: string | undefined;
107106

108107
try {
109-
const accountInfo = resolveDiscordAccount({
110-
cfg,
111-
accountId: opts.accountId,
112-
});
113108
const client = createDiscordClient({ ...opts, cfg });
114109
token = client.token;
115110
rest = client.rest;
116111
const request = client.request;
117-
const recipient = await parseAndResolveChannelRecipient(to, cfg, opts.accountId);
112+
const accountInfo = client.account;
113+
const recipient = await parseAndResolveChannelRecipient(to, cfg, accountInfo.accountId);
118114
channelId = (await resolveChannelId(rest, recipient, request)).channelId;
119115

120116
const ogg = await ensureOggOpus(localInputPath);

0 commit comments

Comments
 (0)