Skip to content

Commit af3d796

Browse files
authored
refactor(discord): trim internal exports (#107714)
* refactor(discord): trim internal exports * refactor(discord): use canonical approval helper * chore(deadcode): refresh export baseline
1 parent e63e49b commit af3d796

59 files changed

Lines changed: 373 additions & 506 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

extensions/discord/src/approval-native.test.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
import fs from "node:fs";
33
import os from "node:os";
44
import path from "node:path";
5+
import { splitChannelApprovalCapability } from "openclaw/plugin-sdk/approval-delivery-runtime";
56
import { clearSessionStoreCacheForTest } from "openclaw/plugin-sdk/session-store-runtime";
67
import { describe, expect, it } from "vitest";
7-
import {
8-
createDiscordNativeApprovalAdapter,
9-
getDiscordApprovalCapability,
10-
} from "./approval-native.js";
8+
import { getDiscordApprovalCapability } from "./approval-native.js";
119
import { shouldHandleDiscordApprovalRequest } from "./approval-shared.js";
1210

1311
const STORE_PATH = path.join(os.tmpdir(), "openclaw-discord-approval-native-test.json");
@@ -27,29 +25,41 @@ const NATIVE_DELIVERY_CFG = {
2725
},
2826
} as const;
2927

28+
function createDiscordNativeApprovalAdapter() {
29+
return splitChannelApprovalCapability(getDiscordApprovalCapability());
30+
}
31+
3032
function writeStore(store: Record<string, unknown>) {
3133
fs.writeFileSync(STORE_PATH, `${JSON.stringify(store, null, 2)}\n`, "utf8");
3234
clearSessionStoreCacheForTest();
3335
}
3436

3537
describe("createDiscordNativeApprovalAdapter", () => {
3638
it("keeps approval availability enabled when approvers exist but native delivery is off", () => {
37-
const adapter = createDiscordNativeApprovalAdapter({
38-
enabled: false,
39-
approvers: ["555555555"],
40-
target: "channel",
41-
} as never);
39+
const adapter = createDiscordNativeApprovalAdapter();
40+
const cfg = {
41+
...NATIVE_APPROVAL_CFG,
42+
channels: {
43+
discord: {
44+
execApprovals: {
45+
enabled: false,
46+
approvers: ["555555555"],
47+
target: "channel",
48+
},
49+
},
50+
},
51+
} as const;
4252

4353
expect(
4454
adapter.auth?.getActionAvailabilityState?.({
45-
cfg: NATIVE_APPROVAL_CFG as never,
55+
cfg: cfg as never,
4656
accountId: "main",
4757
action: "approve",
4858
}),
4959
).toEqual({ kind: "enabled" });
5060
expect(
5161
adapter.native?.describeDeliveryCapabilities({
52-
cfg: NATIVE_APPROVAL_CFG as never,
62+
cfg: cfg as never,
5363
accountId: "main",
5464
approvalKind: "exec",
5565
request: {

extensions/discord/src/approval-native.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
createChannelApproverDmTargetResolver,
1414
createChannelNativeOriginTargetResolver,
1515
createApproverRestrictedNativeApprovalCapability,
16-
splitChannelApprovalCapability,
1716
} from "./approval-runtime.js";
1817
import { shouldHandleDiscordApprovalRequest } from "./approval-shared.js";
1918
import {
@@ -202,12 +201,6 @@ function createDiscordApprovalCapability(configOverride?: DiscordExecApprovalCon
202201
});
203202
}
204203

205-
export function createDiscordNativeApprovalAdapter(
206-
configOverride?: DiscordExecApprovalConfig | null,
207-
) {
208-
return splitChannelApprovalCapability(createDiscordApprovalCapability(configOverride));
209-
}
210-
211204
let cachedDiscordApprovalCapability: ReturnType<typeof createDiscordApprovalCapability> | undefined;
212205

213206
export function getDiscordApprovalCapability() {

extensions/discord/src/approval-runtime.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ export {
55
getExecApprovalReplyMetadata,
66
} from "openclaw/plugin-sdk/approval-client-runtime";
77
export { resolveApprovalApprovers } from "openclaw/plugin-sdk/approval-auth-runtime";
8-
export {
9-
createApproverRestrictedNativeApprovalCapability,
10-
splitChannelApprovalCapability,
11-
} from "openclaw/plugin-sdk/approval-delivery-runtime";
8+
export { createApproverRestrictedNativeApprovalCapability } from "openclaw/plugin-sdk/approval-delivery-runtime";
129
export {
1310
createChannelApproverDmTargetResolver,
1411
createChannelNativeOriginTargetResolver,

extensions/discord/src/audit-core.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ function listConfiguredGuildChannelKeys(
8383
return [...ids].toSorted((a, b) => a.localeCompare(b));
8484
}
8585

86-
export function collectDiscordAuditChannelIdsForGuilds(
86+
function collectDiscordAuditChannelIdsForGuilds(
8787
guilds: Record<string, DiscordGuildEntry> | undefined,
8888
) {
8989
const keys = listConfiguredGuildChannelKeys(guilds);

extensions/discord/src/audit.test.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
55
import {
66
auditDiscordChannelPermissionsWithFetcher,
77
collectDiscordAuditChannelIdsForAccount,
8-
collectDiscordAuditChannelIdsForGuilds,
98
} from "./audit-core.js";
109

1110
const fetchChannelPermissionsDiscordMock = vi.fn();
@@ -43,7 +42,7 @@ describe("discord audit", () => {
4342
},
4443
} as unknown as OpenClawConfig;
4544

46-
const collected = collectDiscordAuditChannelIdsForGuilds(readDiscordGuilds(cfg));
45+
const collected = collectDiscordAuditChannelIdsForAccount({ guilds: readDiscordGuilds(cfg) });
4746
expect(collected.channelIds).toEqual(["111", "222"]);
4847
expect(collected.unresolvedChannels).toBe(1);
4948

@@ -93,7 +92,7 @@ describe("discord audit", () => {
9392
},
9493
} as unknown as OpenClawConfig;
9594

96-
const collected = collectDiscordAuditChannelIdsForGuilds(readDiscordGuilds(cfg));
95+
const collected = collectDiscordAuditChannelIdsForAccount({ guilds: readDiscordGuilds(cfg) });
9796
expect(collected.channelIds).toEqual(["111"]);
9897
expect(collected.unresolvedChannels).toBe(0);
9998
});
@@ -116,7 +115,7 @@ describe("discord audit", () => {
116115
},
117116
} as unknown as OpenClawConfig;
118117

119-
const collected = collectDiscordAuditChannelIdsForGuilds(readDiscordGuilds(cfg));
118+
const collected = collectDiscordAuditChannelIdsForAccount({ guilds: readDiscordGuilds(cfg) });
120119
expect(collected.channelIds).toStrictEqual([]);
121120
expect(collected.unresolvedChannels).toBe(0);
122121
});
@@ -143,7 +142,7 @@ describe("discord audit", () => {
143142
},
144143
} as unknown as OpenClawConfig;
145144

146-
const collected = collectDiscordAuditChannelIdsForGuilds(readDiscordGuilds(cfg));
145+
const collected = collectDiscordAuditChannelIdsForAccount({ guilds: readDiscordGuilds(cfg) });
147146
expect(collected.channelIds).toEqual(["111"]);
148147
expect(collected.unresolvedChannels).toBe(1);
149148
});

extensions/discord/src/chunk.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22
import { expectDefined } from "@openclaw/normalization-core";
33
import { countLines, hasBalancedFences } from "openclaw/plugin-sdk/test-fixtures";
44
import { describe, expect, it } from "vitest";
5-
import { chunkDiscordText, chunkDiscordTextWithMode } from "./chunk.js";
5+
import { chunkDiscordTextWithMode } from "./chunk.js";
6+
7+
type ChunkOptions = Omit<Parameters<typeof chunkDiscordTextWithMode>[1], "chunkMode">;
8+
9+
function chunkDiscordText(text: string, options: ChunkOptions = {}) {
10+
return chunkDiscordTextWithMode(text, { ...options, chunkMode: "length" });
11+
}
612

713
describe("chunkDiscordText", () => {
814
it("splits tall messages even when under 2000 chars", () => {

extensions/discord/src/chunk.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ function splitLongLine(
156156
* Chunks outbound Discord text by both character count and (soft) line count,
157157
* while keeping fenced code blocks balanced across chunks.
158158
*/
159-
export function chunkDiscordText(text: string, opts: ChunkDiscordTextOpts = {}): string[] {
159+
function chunkDiscordText(text: string, opts: ChunkDiscordTextOpts = {}): string[] {
160160
const maxChars = resolveDiscordChunkLimit(opts.maxChars, DEFAULT_MAX_CHARS);
161161
const maxLines = resolveDiscordChunkLimit(opts.maxLines, DEFAULT_MAX_LINES);
162162

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import path from "node:path";
55
import { ApplicationCommandType, ComponentType, Routes } from "discord-api-types/v10";
66
import { MAX_TIMER_TIMEOUT_MS } from "openclaw/plugin-sdk/number-runtime";
77
import { afterEach, describe, expect, it, vi } from "vitest";
8-
import { Client, ComponentRegistry, type AnyListener } from "./client.js";
8+
import { Client, ComponentRegistry } from "./client.js";
99
import { BaseCommand } from "./commands.js";
1010
import { Button, StringSelectMenu, parseCustomId } from "./components.js";
1111
import { DiscordError } from "./rest.js";
1212
import { attachRestMock, createInternalTestClient } from "./test-builders.test-support.js";
1313

14+
type AnyListener = Parameters<Client["registerListener"]>[0];
15+
1416
function createDeferred<T = void>(): {
1517
promise: Promise<T>;
1618
resolve: (value: T | PromiseLike<T>) => void;

extensions/discord/src/internal/client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ export abstract class Plugin {
3030
onRequest?(req: Request, ctx: Context): Promise<Response | undefined> | Response | undefined;
3131
}
3232

33-
export type AnyListener = {
33+
type AnyListener = {
3434
type: string;
3535
handle(data: unknown, client: Client): Promise<void> | void;
3636
};
3737

38-
export interface ClientOptions {
38+
interface ClientOptions {
3939
baseUrl: string;
4040
clientId: string;
4141
deploySecret?: string;

extensions/discord/src/internal/interactions.test.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,7 @@ import {
88
} from "discord-api-types/v10";
99
import { describe, expect, it, vi } from "vitest";
1010
import { Container, TextDisplay } from "./components.js";
11-
import {
12-
BaseInteraction,
13-
ModalInteraction,
14-
createInteraction,
15-
type RawInteraction,
16-
} from "./interactions.js";
11+
import { ModalInteraction, createInteraction, type RawInteraction } from "./interactions.js";
1712
import { Message } from "./structures.js";
1813
import {
1914
attachRestMock,
@@ -29,7 +24,7 @@ describe("BaseInteraction", () => {
2924
const patch = vi.fn(async () => undefined);
3025
const client = createInternalTestClient();
3126
attachRestMock(client, { patch, post });
32-
const interaction = new BaseInteraction(
27+
const interaction = createInteraction(
3328
client,
3429
createInternalInteractionPayload({ id: "interaction1", token: "token1" }),
3530
);
@@ -52,7 +47,7 @@ describe("BaseInteraction", () => {
5247
const post = vi.fn(async () => undefined);
5348
const client = createInternalTestClient();
5449
attachRestMock(client, { post });
55-
const interaction = new BaseInteraction(
50+
const interaction = createInteraction(
5651
client,
5752
createInternalInteractionPayload({ id: "interaction1", token: "token1" }),
5853
);
@@ -85,7 +80,7 @@ describe("BaseInteraction", () => {
8580
const patch = vi.fn(async () => undefined);
8681
const client = createInternalTestClient();
8782
attachRestMock(client, { patch, post });
88-
const interaction = new BaseInteraction(
83+
const interaction = createInteraction(
8984
client,
9085
createInternalInteractionPayload({ id: "interaction1", token: "token1" }),
9186
);
@@ -199,7 +194,7 @@ describe("BaseInteraction", () => {
199194
const post = vi.fn(async () => undefined);
200195
const client = createInternalTestClient();
201196
attachRestMock(client, { get, post });
202-
const interaction = new BaseInteraction(
197+
const interaction = createInteraction(
203198
client,
204199
createInternalInteractionPayload({ id: "interaction1", token: "token1" }),
205200
);

0 commit comments

Comments
 (0)