Skip to content

Commit a199546

Browse files
steipeteedenfunf
andcommitted
refactor(line): keep card registration in the plugin entry
Load only the card handler at runtime, removing duplicate metadata and the captured registration API. Retain LINE-only command scope and cover the real lazy handler. Co-authored-by: xuyuanhao <[email protected]>
1 parent 6973849 commit a199546

7 files changed

Lines changed: 189 additions & 295 deletions

‎extensions/line/card-command-metadata.ts‎

Lines changed: 0 additions & 22 deletions
This file was deleted.

‎extensions/line/card-command-registration.test.ts‎

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,19 @@ import type { OpenClawPluginCommandDefinition } from "openclaw/plugin-sdk/plugin
33
import { createTestPluginApi } from "openclaw/plugin-sdk/plugin-test-api";
44
import { describe, expect, it } from "vitest";
55
import lineEntry from "./index.js";
6-
import { registerLineCardCommand } from "./src/card-command.js";
76

8-
/**
9-
* Collect what a registration actually hands the host.
10-
*
11-
* The bundled entry registers `/card` eagerly and loads its handler lazily, so
12-
* the registration the host sees is the one the entry declares, not the one
13-
* inside the handler module.
14-
*/
15-
function registeredCommands(register: (api: ReturnType<typeof createTestPluginApi>) => void) {
7+
function registeredCardCommand(): OpenClawPluginCommandDefinition {
168
const commands: OpenClawPluginCommandDefinition[] = [];
17-
register(
9+
lineEntry.register(
1810
createTestPluginApi({
19-
// Runs the entry's command registration without loading the channel plugin.
11+
// Exercise the shipped registration without loading the full channel runtime.
2012
registrationMode: "tool-discovery",
2113
registerCommand(command) {
2214
commands.push(command);
2315
},
2416
}),
2517
);
26-
return commands;
27-
}
28-
29-
function cardCommandFrom(
30-
register: (api: ReturnType<typeof createTestPluginApi>) => void,
31-
): OpenClawPluginCommandDefinition {
32-
const card = registeredCommands(register).find((command) => command.name === "card");
18+
const card = commands.find((command) => command.name === "card");
3319
if (!card) {
3420
throw new Error("LINE did not register a /card command");
3521
}
@@ -38,21 +24,32 @@ function cardCommandFrom(
3824

3925
describe("line /card registration", () => {
4026
it("offers the command on LINE only", () => {
41-
// Flex cards are a LINE transport feature. Core turns this declaration into
42-
// the actual gating for listings, native command menus, text matching and
43-
// execution; see src/plugins/commands.test.ts for that behavior.
44-
expect(cardCommandFrom((api) => lineEntry.register(api)).channels).toEqual(["line"]);
27+
expect(registeredCardCommand()).toMatchObject({
28+
channels: ["line"],
29+
acceptsArgs: true,
30+
requireAuth: false,
31+
});
4532
});
4633

47-
it("keeps the handler module's declaration identical to the shipped one", () => {
48-
// Two copies of this metadata would let the shipped scope drift away from
49-
// the one the handler module declares, with nothing reporting the gap.
50-
const shipped = cardCommandFrom((api) => lineEntry.register(api));
51-
const local = cardCommandFrom(registerLineCardCommand);
52-
expect(local.channels).toEqual(shipped.channels);
53-
expect(local.name).toBe(shipped.name);
54-
expect(local.description).toBe(shipped.description);
55-
expect(local.acceptsArgs).toBe(shipped.acceptsArgs);
56-
expect(local.requireAuth).toBe(shipped.requireAuth);
34+
it("renders a card through the registered lazy handler", async () => {
35+
const args = 'info "Welcome" "Thanks for joining!"';
36+
await expect(
37+
registeredCardCommand().handler({
38+
channel: "line",
39+
isAuthorizedSender: false,
40+
commandBody: `/card ${args}`,
41+
args,
42+
config: {},
43+
requestConversationBinding: async () => ({ status: "error", message: "unused" }),
44+
detachConversationBinding: async () => ({ removed: false }),
45+
getCurrentConversationBinding: async () => null,
46+
}),
47+
).resolves.toMatchObject({
48+
channelData: {
49+
line: {
50+
flexMessage: { altText: "Welcome: Thanks for joining!", contents: { type: "bubble" } },
51+
},
52+
},
53+
});
5754
});
5855
});

‎extensions/line/index.ts‎

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,8 @@
11
// Line plugin entrypoint registers its OpenClaw integration.
2-
import {
3-
defineBundledChannelEntry,
4-
type OpenClawPluginCommandDefinition,
5-
type OpenClawPluginApi,
6-
} from "openclaw/plugin-sdk/channel-entry-contract";
2+
import { defineBundledChannelEntry } from "openclaw/plugin-sdk/channel-entry-contract";
73
import { createLazyRuntimeModule } from "openclaw/plugin-sdk/lazy-runtime";
8-
import { LINE_CARD_COMMAND_METADATA } from "./card-command-metadata.js";
94

10-
type RegisteredLineCardCommand = OpenClawPluginCommandDefinition;
11-
12-
function createLineCardCommandLoader(api: OpenClawPluginApi) {
13-
return createLazyRuntimeModule<RegisteredLineCardCommand>(async () => {
14-
let registered: RegisteredLineCardCommand | null = null;
15-
const { registerLineCardCommand } = await import("./src/card-command.js");
16-
registerLineCardCommand({
17-
...api,
18-
registerCommand(command: RegisteredLineCardCommand) {
19-
registered = command;
20-
},
21-
});
22-
if (!registered) {
23-
throw new Error("LINE card command registration unavailable");
24-
}
25-
return registered;
26-
});
27-
}
5+
const loadLineCardCommand = createLazyRuntimeModule(() => import("./src/card-command.js"));
286

297
export default defineBundledChannelEntry({
308
id: "line",
@@ -40,12 +18,15 @@ export default defineBundledChannelEntry({
4018
exportName: "setLineRuntime",
4119
},
4220
registerFull(api) {
43-
const loadLineCardCommand = createLineCardCommandLoader(api);
4421
api.registerCommand({
45-
...LINE_CARD_COMMAND_METADATA,
22+
name: "card",
23+
description: "Send a rich card message.",
24+
channels: ["line"],
25+
acceptsArgs: true,
26+
requireAuth: false,
4627
async handler(ctx) {
47-
const command = await loadLineCardCommand();
48-
return await command.handler(ctx);
28+
const { handleLineCardCommand } = await loadLineCardCommand();
29+
return await handleLineCardCommand(ctx.args);
4930
},
5031
});
5132
},

‎extensions/line/src/card-command.blank-arguments.test.ts‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,12 @@
22
import type { messagingApi } from "@line/bot-sdk";
33
import { expectDefined } from "@openclaw/normalization-core";
44
import { describe, expect, it } from "vitest";
5-
import { registerLineCardCommand } from "./card-command.js";
5+
import { handleLineCardCommand } from "./card-command.js";
66
import { buildTemplateMessageFromPayload } from "./template-messages.js";
77
import type { LineChannelData } from "./types.js";
88

99
async function runCardCommand(args: string): Promise<LineChannelData> {
10-
let result: unknown;
11-
registerLineCardCommand({
12-
registerCommand(command: unknown) {
13-
const { handler } = command as {
14-
handler: (ctx: { args: string; channel: string }) => Promise<unknown>;
15-
};
16-
result = handler({ channel: "line", args });
17-
},
18-
} as never);
19-
const payload = (await result) as { channelData: { line: LineChannelData } };
10+
const payload = (await handleLineCardCommand(args)) as { channelData: { line: LineChannelData } };
2011
return payload.channelData.line;
2112
}
2213

‎extensions/line/src/card-command.escapes.test.ts‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,12 @@
22
import type { messagingApi } from "@line/bot-sdk";
33
import { expectDefined } from "@openclaw/normalization-core";
44
import { describe, expect, it } from "vitest";
5-
import { registerLineCardCommand } from "./card-command.js";
5+
import { handleLineCardCommand } from "./card-command.js";
66
import { buildTemplateMessageFromPayload } from "./template-messages.js";
77
import type { LineChannelData } from "./types.js";
88

99
async function runCardCommand(args: string): Promise<LineChannelData> {
10-
let result: unknown;
11-
registerLineCardCommand({
12-
registerCommand(command: unknown) {
13-
const { handler } = command as {
14-
handler: (ctx: { args: string; channel: string }) => Promise<unknown>;
15-
};
16-
result = handler({ channel: "line", args });
17-
},
18-
} as never);
19-
const payload = (await result) as { channelData: { line: LineChannelData } };
10+
const payload = (await handleLineCardCommand(args)) as { channelData: { line: LineChannelData } };
2011
return payload.channelData.line;
2112
}
2213

0 commit comments

Comments
 (0)