Skip to content

Commit 080887d

Browse files
authored
refactor(types): discriminated unions for internal contracts — pilot lane A (#124845)
1 parent fb84199 commit 080887d

22 files changed

Lines changed: 143 additions & 110 deletions

‎config/assertion-safety-baseline.txt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ extensions/discord/src/internal/gateway-dispatch.ts 7
371371
extensions/discord/src/internal/gateway-payload.ts 1
372372
extensions/discord/src/internal/gateway-voice-state-cache.ts 4
373373
extensions/discord/src/internal/gateway.ts 9
374-
extensions/discord/src/internal/interaction-dispatch.ts 14
374+
extensions/discord/src/internal/interaction-dispatch.ts 12
375375
extensions/discord/src/internal/interaction-options.ts 2
376376
extensions/discord/src/internal/interaction-response.ts 2
377377
extensions/discord/src/internal/interactions.ts 8
@@ -2898,7 +2898,7 @@ src/flows/doctor-core-checks.ts 4
28982898
src/flows/doctor-health-contributions-final.ts 1
28992899
src/flows/doctor-health-contributions.ts 2
29002900
src/flows/doctor-removed-workspaces-state-check.ts 2
2901-
src/flows/health-check-adapter.ts 2
2901+
src/flows/health-check-adapter.ts 1
29022902
src/flows/search-setup.ts 6
29032903
src/gateway/agent-runtime-identity-token.ts 1
29042904
src/gateway/agent-turn/agent-dedupe.ts 3

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { createDeferred } from "openclaw/plugin-sdk/extension-shared";
44
import { MAX_TIMER_TIMEOUT_MS } from "openclaw/plugin-sdk/number-runtime";
55
import { afterEach, describe, expect, it, vi } from "vitest";
66
import { Client } from "./client.js";
7-
import { BaseCommand } from "./commands.js";
7+
import { Command, type CommandOptions, type DiscordCommand } from "./commands.js";
88
import { ComponentRegistry } from "./component-registry.js";
99
import { Button, StringSelectMenu, parseCustomId } from "./components.js";
1010
import { DiscordError } from "./rest.js";
@@ -20,16 +20,14 @@ afterEach(() => {
2020
function createTestCommand(params: {
2121
name: string;
2222
guildIds?: string[];
23-
options?: unknown[];
24-
}): BaseCommand {
25-
return new (class extends BaseCommand {
23+
options?: CommandOptions;
24+
}): DiscordCommand {
25+
return new (class extends Command {
2626
name = params.name;
2727
override description = `${params.name} command`;
28-
type = ApplicationCommandType.ChatInput;
2928
override guildIds = params.guildIds;
30-
serializeOptions() {
31-
return params.options;
32-
}
29+
override options = params.options;
30+
run() {}
3331
})();
3432
}
3533

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import type { APIInteraction } from "discord-api-types/v10";
33
import type { DiscordCommandDeployHashStore } from "../command-deploy-store.js";
44
import { DiscordCommandDeployer, type DeployCommandOptions } from "./command-deploy.js";
5-
import type { BaseCommand } from "./commands.js";
5+
import type { DiscordCommand } from "./commands.js";
66
import { ComponentRegistry } from "./component-registry.js";
77
import { BaseMessageInteractiveComponent, type Modal } from "./components.js";
88
import { DiscordEntityCache } from "./entity-cache.js";
@@ -57,7 +57,7 @@ export class Client {
5757
routes: Route[] = [];
5858
plugins: Array<{ id: string; plugin: Plugin }> = [];
5959
options: ClientOptions;
60-
commands: BaseCommand[];
60+
commands: DiscordCommand[];
6161
listeners: AnyListener[];
6262
rest: RequestClient;
6363
componentHandler = new ComponentRegistry<BaseMessageInteractiveComponent>();
@@ -71,7 +71,7 @@ export class Client {
7171
constructor(
7272
options: ClientOptions,
7373
handlers: {
74-
commands?: BaseCommand[];
74+
commands?: DiscordCommand[];
7575
listeners?: AnyListener[];
7676
components?: BaseMessageInteractiveComponent[];
7777
modals?: Modal[];

‎extensions/discord/src/internal/command-deploy.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ describe("commandsEqual", () => {
213213
*/
214214
describe("DiscordCommandDeployer SQLite cache", () => {
215215
class StaticCommand extends BaseCommand {
216+
readonly commandKind = "leaf";
216217
name: string;
217218
override description = "ping the bot";
218219
type = ApplicationCommandType.ChatInput;

‎extensions/discord/src/internal/commands.ts‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type CommandOption = Record<string, unknown> & {
1919
autocomplete?: boolean | ((interaction: AutocompleteInteraction) => Promise<void>);
2020
};
2121
export type CommandOptions = CommandOption[];
22+
export type DiscordCommand = Command | CommandWithSubcommands;
2223

2324
type RawSubcommandOption = {
2425
name?: unknown;
@@ -72,28 +73,21 @@ function findCommandOption(
7273
return options?.find((option) => option.name === name);
7374
}
7475

75-
function hasCommandOptions(
76-
command: BaseCommand,
77-
): command is BaseCommand & { options?: CommandOptions } {
78-
return "options" in command;
79-
}
80-
8176
export function resolveFocusedCommandOptionAutocompleteHandler(
82-
command: BaseCommand,
77+
command: DiscordCommand,
8378
interaction: AutocompleteInteraction,
8479
): ((interaction: AutocompleteInteraction) => Promise<void>) | undefined {
8580
const focusedName = interaction.options.getFocused()?.name;
8681
const options =
87-
"subcommands" in command && Array.isArray(command.subcommands)
82+
command.commandKind === "group"
8883
? findSelectedSubcommand(command.subcommands, interaction)?.options
89-
: hasCommandOptions(command)
90-
? command.options
91-
: undefined;
84+
: command.options;
9285
const autocomplete = findCommandOption(options, focusedName)?.autocomplete;
9386
return typeof autocomplete === "function" ? autocomplete : undefined;
9487
}
9588

9689
export abstract class BaseCommand {
90+
abstract readonly commandKind: "leaf" | "group";
9791
id?: string;
9892
abstract name: string;
9993
description?: string;
@@ -133,6 +127,7 @@ export abstract class BaseCommand {
133127
}
134128

135129
export abstract class Command extends BaseCommand {
130+
readonly commandKind = "leaf";
136131
options?: CommandOptions;
137132
type = ApplicationCommandType.ChatInput;
138133
abstract run(interaction: unknown): unknown;
@@ -153,6 +148,7 @@ export abstract class Command extends BaseCommand {
153148
}
154149

155150
export abstract class CommandWithSubcommands extends BaseCommand {
151+
readonly commandKind = "group";
156152
type = ApplicationCommandType.ChatInput;
157153
abstract subcommands: Command[];
158154
async run(interaction: CommandInteraction): Promise<unknown> {

‎extensions/discord/src/internal/interaction-dispatch.ts‎

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Discord plugin module implements interaction dispatch behavior.
22
import { InteractionType, type APIInteraction } from "discord-api-types/v10";
33
import {
4-
type BaseCommand,
4+
type DiscordCommand,
55
deferCommandInteractionIfNeeded,
66
resolveFocusedCommandOptionAutocompleteHandler,
77
} from "./commands.js";
@@ -28,7 +28,7 @@ type DispatchModal = {
2828
};
2929

3030
type DispatchClient = Parameters<typeof createInteraction>[0] & {
31-
commands: BaseCommand[];
31+
commands: DiscordCommand[];
3232
componentHandler: {
3333
resolve(customId: string, options?: { componentType?: number }): DispatchComponent | undefined;
3434
resolveOneOffComponent(params: {
@@ -60,20 +60,16 @@ export async function dispatchInteraction(
6060
await optionAutocomplete(autocompleteInteraction);
6161
return;
6262
}
63-
if ("autocomplete" in command) {
64-
await (
65-
command as { autocomplete: (interaction: AutocompleteInteraction) => Promise<void> }
66-
).autocomplete(autocompleteInteraction);
63+
if (command.commandKind === "leaf") {
64+
await command.autocomplete(autocompleteInteraction);
6765
}
6866
return;
6967
}
7068
if (rawData.type === InteractionType.ApplicationCommand) {
7169
const command = client.commands.find((entry) => entry.name === readInteractionName(rawData));
72-
if (command && "run" in command) {
70+
if (command) {
7371
await deferCommandInteractionIfNeeded(command, interaction as CommandInteraction);
74-
await (command as { run: (interaction: CommandInteraction) => Promise<void> }).run(
75-
interaction as CommandInteraction,
76-
);
72+
await command.run(interaction as CommandInteraction);
7773
}
7874
return;
7975
}

‎extensions/discord/src/internal/test-builders.test-support.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { ComponentType, InteractionType } from "discord-api-types/v10";
33
import { vi, type Mock } from "vitest";
44
import { Client } from "./client.js";
5-
import type { BaseCommand } from "./commands.js";
5+
import type { DiscordCommand } from "./commands.js";
66
import type { RawInteraction } from "./interactions.js";
77
import type { RequestClient, RequestData } from "./rest.js";
88

@@ -55,7 +55,7 @@ export function createAbortableFetchMock() {
5555
}
5656

5757
export function createInternalTestClient(
58-
commands: BaseCommand[] = [],
58+
commands: DiscordCommand[] = [],
5959
options?: Partial<ClientOptions>,
6060
): Client {
6161
return new Client(

‎extensions/discord/src/monitor/provider.interactions.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import {
99
getDiscordExecApprovalApprovers,
1010
isDiscordExecApprovalClientEnabled,
1111
} from "../exec-approvals.js";
12-
import type { BaseCommand, BaseMessageInteractiveComponent, Modal } from "../internal/discord.js";
12+
import type {
13+
BaseMessageInteractiveComponent,
14+
DiscordCommand,
15+
Modal,
16+
} from "../internal/discord.js";
1317
import { createDiscordVoiceCommand, DISCORD_VOICE_COMMAND_SPEC } from "../voice/command.js";
1418
import {
1519
createAgentComponentControls,
@@ -55,12 +59,12 @@ export function createDiscordProviderInteractionSurface(params: {
5559
abortSignal?: AbortSignal;
5660
createNativeCommand?: typeof createDiscordNativeCommand;
5761
}): {
58-
commands: BaseCommand[];
62+
commands: DiscordCommand[];
5963
components: BaseMessageInteractiveComponent[];
6064
modals: Modal[];
6165
} {
6266
const createNativeCommand = params.createNativeCommand ?? createDiscordNativeCommand;
63-
const commands: BaseCommand[] = params.commandSpecs.map((spec) => {
67+
const commands: DiscordCommand[] = params.commandSpecs.map((spec) => {
6468
if (
6569
params.nativeEnabled &&
6670
params.voiceEnabled &&

‎extensions/discord/src/monitor/provider.startup.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import type { DiscordCommandDeployHashStore } from "../command-deploy-store.js";
88
import {
99
Client,
1010
ReadyListener,
11-
type BaseCommand,
1211
type BaseMessageInteractiveComponent,
12+
type DiscordCommand,
1313
type Modal,
1414
type Plugin,
1515
} from "../internal/discord.js";
@@ -93,7 +93,7 @@ export async function createDiscordMonitorClient(params: {
9393
applicationId: string;
9494
token: string;
9595
restFetch?: typeof fetch;
96-
commands: BaseCommand[];
96+
commands: DiscordCommand[];
9797
components: BaseMessageInteractiveComponent[];
9898
modals: Modal[];
9999
voiceEnabled: boolean;

‎src/config/sessions/session-accessor.sqlite-message-cut.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import {
4949
import type { InternalSessionEntry as SessionEntry } from "./types.js";
5050

5151
type MessageCut = {
52+
status: "cut";
5253
editorText?: string;
5354
editorAttachments?: Array<{ mimeType: string; data: string }>;
5455
editorMediaRefs?: Array<{ path: string; contentType: string }>;
@@ -290,7 +291,7 @@ function mutateSqliteSessionAtMessageInTransaction(
290291
}
291292
const events = loadTranscriptEventsFromDatabase(database, currentEntry.sessionId);
292293
const cut = params.mode === "switch" ? undefined : resolveMessageCut(events, params.entryId);
293-
if (cut && "status" in cut) {
294+
if (cut && cut.status !== "cut") {
294295
return cut;
295296
}
296297
if (params.mode === "switch") {
@@ -311,7 +312,7 @@ function mutateSqliteSessionAtMessageInTransaction(
311312
sessionId: nextSessionId,
312313
});
313314
const nextEvents =
314-
params.mode === "fork" && cut && !("status" in cut)
315+
params.mode === "fork" && cut?.status === "cut"
315316
? [header, ...cut.prefix]
316317
: [
317318
header,
@@ -354,11 +355,11 @@ function mutateSqliteSessionAtMessageInTransaction(
354355
status: "created",
355356
key: params.targetKey,
356357
entry: nextEntry,
357-
...(cut && !("status" in cut) && cut.editorText ? { editorText: cut.editorText } : {}),
358-
...(cut && !("status" in cut) && cut.editorAttachments
358+
...(cut?.status === "cut" && cut.editorText ? { editorText: cut.editorText } : {}),
359+
...(cut?.status === "cut" && cut.editorAttachments
359360
? { editorAttachments: cut.editorAttachments }
360361
: {}),
361-
...(cut && !("status" in cut) && cut.editorMediaRefs
362+
...(cut?.status === "cut" && cut.editorMediaRefs
362363
? { editorMediaRefs: cut.editorMediaRefs }
363364
: {}),
364365
};
@@ -482,6 +483,7 @@ function resolveMessageCut(
482483
const editorAttachments = extractEditorAttachments(message.content);
483484
const editorMediaRefs = extractEditorMediaRefs(message);
484485
return {
486+
status: "cut",
485487
editorText: extractEditorText(message.content),
486488
...(editorAttachments ? { editorAttachments } : {}),
487489
...(editorMediaRefs ? { editorMediaRefs } : {}),

0 commit comments

Comments
 (0)