refactor(slack): dedupe modal lifecycle interaction handlers

This commit is contained in:
Peter Steinberger
2026-02-21 22:02:43 +00:00
parent 9d17a30643
commit 474ba45a2f
2 changed files with 77 additions and 39 deletions

View File

@@ -1115,5 +1115,35 @@ describe("registerSlackInteractionEvents", () => {
);
expect(options.sessionKey).toBe("agent:main:slack:channel:C99");
});
it("defaults modal close isCleared to false when Slack omits the flag", async () => {
enqueueSystemEventMock.mockReset();
const { ctx, getViewClosedHandler } = createContext();
registerSlackInteractionEvents({ ctx: ctx as never });
const viewClosedHandler = getViewClosedHandler();
expect(viewClosedHandler).toBeTruthy();
const ack = vi.fn().mockResolvedValue(undefined);
await viewClosedHandler!({
ack,
body: {
user: { id: "U901" },
view: {
id: "V901",
callback_id: "openclaw:deploy_form",
},
},
});
expect(ack).toHaveBeenCalled();
expect(enqueueSystemEventMock).toHaveBeenCalledTimes(1);
const [eventText] = enqueueSystemEventMock.mock.calls[0] as [string];
const payload = JSON.parse(eventText.replace("Slack interaction: ", "")) as {
interactionType: string;
isCleared?: boolean;
};
expect(payload.interactionType).toBe("view_closed");
expect(payload.isCleared).toBe(false);
});
});
const selectedDateTimeEpoch = 1_771_632_300;

View File

@@ -98,6 +98,8 @@ type SlackModalEventBase = {
};
};
type SlackModalInteractionKind = "view_submission" | "view_closed";
function readOptionValues(options: unknown): string[] | undefined {
if (!Array.isArray(options)) {
return undefined;
@@ -442,6 +444,45 @@ function resolveSlackModalEventBase(params: {
};
}
function emitSlackModalLifecycleEvent(params: {
ctx: SlackMonitorContext;
body: SlackModalBody;
interactionType: SlackModalInteractionKind;
contextPrefix: "slack:interaction:view" | "slack:interaction:view-closed";
}): void {
const { callbackId, userId, viewId, sessionRouting, payload } = resolveSlackModalEventBase({
ctx: params.ctx,
body: params.body,
});
const isViewClosed = params.interactionType === "view_closed";
const isCleared = params.body.is_cleared === true;
const eventPayload = isViewClosed
? {
interactionType: params.interactionType,
...payload,
isCleared,
}
: {
interactionType: params.interactionType,
...payload,
};
if (isViewClosed) {
params.ctx.runtime.log?.(
`slack:interaction view_closed callback=${callbackId} user=${userId} cleared=${isCleared}`,
);
} else {
params.ctx.runtime.log?.(
`slack:interaction view_submission callback=${callbackId} user=${userId} inputs=${payload.inputs.length}`,
);
}
enqueueSystemEvent(`Slack interaction: ${JSON.stringify(eventPayload)}`, {
sessionKey: sessionRouting.sessionKey,
contextKey: [params.contextPrefix, callbackId, viewId, userId].filter(Boolean).join(":"),
});
}
export function registerSlackInteractionEvents(params: { ctx: SlackMonitorContext }) {
const { ctx } = params;
if (typeof ctx.app.action !== "function") {
@@ -611,26 +652,11 @@ export function registerSlackInteractionEvents(params: { ctx: SlackMonitorContex
new RegExp(`^${OPENCLAW_ACTION_PREFIX}`),
async ({ ack, body }: { ack: () => Promise<void>; body: unknown }) => {
await ack();
const modalBody = body as SlackModalBody;
const { callbackId, userId, viewId, sessionRouting, payload } = resolveSlackModalEventBase({
emitSlackModalLifecycleEvent({
ctx,
body: modalBody,
});
const eventPayload = {
body: body as SlackModalBody,
interactionType: "view_submission",
...payload,
};
ctx.runtime.log?.(
`slack:interaction view_submission callback=${callbackId} user=${userId} inputs=${payload.inputs.length}`,
);
enqueueSystemEvent(`Slack interaction: ${JSON.stringify(eventPayload)}`, {
sessionKey: sessionRouting.sessionKey,
contextKey: ["slack:interaction:view", callbackId, viewId, userId]
.filter(Boolean)
.join(":"),
contextPrefix: "slack:interaction:view",
});
},
);
@@ -652,29 +678,11 @@ export function registerSlackInteractionEvents(params: { ctx: SlackMonitorContex
new RegExp(`^${OPENCLAW_ACTION_PREFIX}`),
async ({ ack, body }: { ack: () => Promise<void>; body: unknown }) => {
await ack();
const modalBody = body as SlackModalBody;
const { callbackId, userId, viewId, sessionRouting, payload } = resolveSlackModalEventBase({
emitSlackModalLifecycleEvent({
ctx,
body: modalBody,
});
const eventPayload = {
body: body as SlackModalBody,
interactionType: "view_closed",
...payload,
isCleared: modalBody.is_cleared === true,
};
ctx.runtime.log?.(
`slack:interaction view_closed callback=${callbackId} user=${userId} cleared=${
modalBody.is_cleared === true
}`,
);
enqueueSystemEvent(`Slack interaction: ${JSON.stringify(eventPayload)}`, {
sessionKey: sessionRouting.sessionKey,
contextKey: ["slack:interaction:view-closed", callbackId, viewId, userId]
.filter(Boolean)
.join(":"),
contextPrefix: "slack:interaction:view-closed",
});
},
);