feat: add OpenClaw interactive-handler adapter / 新增 OpenClaw interactive-handler adapter
This commit is contained in:
40
src/adapters/openclaw-interactive-handler.ts
Normal file
40
src/adapters/openclaw-interactive-handler.ts
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
import { persistOpenClawReplyEndState } from "./openclaw-state-file.ts"
|
||||||
|
import { buildTelegramCallbackResolution } from "../runtime/openclaw-telegram-callback-actions.ts"
|
||||||
|
import { normalizeTelegramCallback } from "../core/callback-contract.ts"
|
||||||
|
|
||||||
|
export type OpenClawTelegramInteractiveContext = {
|
||||||
|
callback: {
|
||||||
|
data: string
|
||||||
|
messageId: string
|
||||||
|
chatId: string
|
||||||
|
}
|
||||||
|
conversationId: string
|
||||||
|
sessionKey: string | null
|
||||||
|
callbackId: string
|
||||||
|
respond: {
|
||||||
|
editButtons: (input: { buttons: { text: string; callback_data: string }[][] }) => Promise<void> | void
|
||||||
|
reply: (input: { text: string }) => Promise<void> | void
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function handleOpenClawTelegramReplyEndInteraction(baseDir: string, ctx: OpenClawTelegramInteractiveContext): Promise<boolean> {
|
||||||
|
const normalized = normalizeTelegramCallback({
|
||||||
|
callbackData: ctx.callback.data,
|
||||||
|
conversationId: ctx.conversationId,
|
||||||
|
sessionKey: ctx.sessionKey,
|
||||||
|
sourceMessageId: ctx.callback.messageId,
|
||||||
|
sourceCallbackId: ctx.callbackId,
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!normalized) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
persistOpenClawReplyEndState(baseDir, normalized)
|
||||||
|
|
||||||
|
const resolution = buildTelegramCallbackResolution(normalized.choice)
|
||||||
|
await ctx.respond.editButtons({ buttons: resolution.buttons })
|
||||||
|
await ctx.respond.reply({ text: resolution.acknowledgement })
|
||||||
|
return true
|
||||||
|
}
|
||||||
58
tests/openclaw-interactive-handler.test.mjs
Normal file
58
tests/openclaw-interactive-handler.test.mjs
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
import test from "node:test"
|
||||||
|
import assert from "node:assert/strict"
|
||||||
|
import fs from "node:fs"
|
||||||
|
import os from "node:os"
|
||||||
|
import path from "node:path"
|
||||||
|
|
||||||
|
import { handleOpenClawTelegramReplyEndInteraction } from "../src/adapters/openclaw-interactive-handler.ts"
|
||||||
|
|
||||||
|
test("openclaw interactive handler persists state and emits response actions", async () => {
|
||||||
|
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "reply-end-interactive-"))
|
||||||
|
const calls = []
|
||||||
|
|
||||||
|
const handled = await handleOpenClawTelegramReplyEndInteraction(tempDir, {
|
||||||
|
callback: {
|
||||||
|
data: "rec:continue",
|
||||||
|
messageId: "77",
|
||||||
|
chatId: "864811879",
|
||||||
|
},
|
||||||
|
conversationId: "864811879",
|
||||||
|
sessionKey: "agent:main:main",
|
||||||
|
callbackId: "cb-77",
|
||||||
|
respond: {
|
||||||
|
editButtons: async (input) => {
|
||||||
|
calls.push(["editButtons", input])
|
||||||
|
},
|
||||||
|
reply: async (input) => {
|
||||||
|
calls.push(["reply", input])
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
assert.equal(handled, true)
|
||||||
|
const statePath = path.join(tempDir, "reply-end-controls.json")
|
||||||
|
const stored = JSON.parse(fs.readFileSync(statePath, "utf-8"))
|
||||||
|
assert.equal(stored["864811879"].lastChoice, "continue")
|
||||||
|
assert.equal(calls[0][0], "editButtons")
|
||||||
|
assert.equal(calls[1][0], "reply")
|
||||||
|
})
|
||||||
|
|
||||||
|
test("openclaw interactive handler ignores unrelated callback data", async () => {
|
||||||
|
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "reply-end-interactive-"))
|
||||||
|
const handled = await handleOpenClawTelegramReplyEndInteraction(tempDir, {
|
||||||
|
callback: {
|
||||||
|
data: "other:value",
|
||||||
|
messageId: "78",
|
||||||
|
chatId: "864811879",
|
||||||
|
},
|
||||||
|
conversationId: "864811879",
|
||||||
|
sessionKey: "agent:main:main",
|
||||||
|
callbackId: "cb-78",
|
||||||
|
respond: {
|
||||||
|
editButtons: async () => {},
|
||||||
|
reply: async () => {},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
assert.equal(handled, false)
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user