feat: add reusable Telegram PoC helper module / 新增可重用 Telegram PoC helper 模組
This commit is contained in:
36
src/runtime/openclaw-telegram-poc.ts
Normal file
36
src/runtime/openclaw-telegram-poc.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import type { ReplyEndChoice, ReplyEndState, TelegramInlineButton } from "../types.js"
|
||||
|
||||
export function buildDefaultReplyEndButtons(): TelegramInlineButton[][] {
|
||||
return [[
|
||||
{ text: "A. 繼續", callback_data: "rec:continue" },
|
||||
{ text: "B. 就這樣吧,不需要額外處理", callback_data: "rec:stop" },
|
||||
]]
|
||||
}
|
||||
|
||||
export function buildResolvedReplyEndButtons(choice: ReplyEndChoice): TelegramInlineButton[][] {
|
||||
if (choice === "continue") {
|
||||
return [[
|
||||
{ text: "✅ A. 繼續", callback_data: "rec:continue" },
|
||||
{ text: "B. 就這樣吧,不需要額外處理", callback_data: "rec:stop" },
|
||||
]]
|
||||
}
|
||||
|
||||
return [[
|
||||
{ text: "A. 繼續", callback_data: "rec:continue" },
|
||||
{ text: "⏹ B. 就這樣吧,不需要額外處理", callback_data: "rec:stop" },
|
||||
]]
|
||||
}
|
||||
|
||||
export function buildReplyEndAcknowledgement(choice: ReplyEndChoice): string {
|
||||
return choice === "continue"
|
||||
? "reply-end-controls: continue received"
|
||||
: "reply-end-controls: stop received"
|
||||
}
|
||||
|
||||
export function applyStopStateToInboundText(text: string, state: ReplyEndState | null): string {
|
||||
if (!state || state.lastChoice !== "stop" || state.active !== true) {
|
||||
return text
|
||||
}
|
||||
|
||||
return `${text}\n\n[System: The user previously pressed reply-end-controls Stop for this conversation. Treat the previous thread as closed. Unless the user's new message contains a clear new instruction or explicit request for more work, you must NOT proactively continue, must NOT add extra checks, must NOT propose next steps, must NOT extend the task, and should answer briefly and close cleanly.]`.trim()
|
||||
}
|
||||
58
tests/runtime-helper.test.mjs
Normal file
58
tests/runtime-helper.test.mjs
Normal file
@@ -0,0 +1,58 @@
|
||||
import test from "node:test"
|
||||
import assert from "node:assert/strict"
|
||||
|
||||
import {
|
||||
applyStopStateToInboundText,
|
||||
buildDefaultReplyEndButtons,
|
||||
buildReplyEndAcknowledgement,
|
||||
buildResolvedReplyEndButtons,
|
||||
} from "../src/runtime/openclaw-telegram-poc.ts"
|
||||
|
||||
test("default reply-end buttons contain continue and stop", () => {
|
||||
const buttons = buildDefaultReplyEndButtons()
|
||||
assert.equal(buttons[0][0].callback_data, "rec:continue")
|
||||
assert.equal(buttons[0][1].callback_data, "rec:stop")
|
||||
})
|
||||
|
||||
test("resolved buttons mark continue selection", () => {
|
||||
const buttons = buildResolvedReplyEndButtons("continue")
|
||||
assert.match(buttons[0][0].text, /^✅/)
|
||||
assert.doesNotMatch(buttons[0][1].text, /^⏹/)
|
||||
})
|
||||
|
||||
test("resolved buttons mark stop selection", () => {
|
||||
const buttons = buildResolvedReplyEndButtons("stop")
|
||||
assert.match(buttons[0][1].text, /^⏹/)
|
||||
assert.doesNotMatch(buttons[0][0].text, /^✅/)
|
||||
})
|
||||
|
||||
test("acknowledgement text matches choice", () => {
|
||||
assert.equal(buildReplyEndAcknowledgement("continue"), "reply-end-controls: continue received")
|
||||
assert.equal(buildReplyEndAcknowledgement("stop"), "reply-end-controls: stop received")
|
||||
})
|
||||
|
||||
test("stop state injects control text into inbound message", () => {
|
||||
const result = applyStopStateToInboundText("我先這樣", {
|
||||
lastChoice: "stop",
|
||||
lastChoiceAt: "2026-05-13T00:00:00Z",
|
||||
sourceMessageId: "1",
|
||||
sourceCallbackId: "cb-1",
|
||||
active: true,
|
||||
})
|
||||
assert.match(result, /reply-end-controls Stop/)
|
||||
})
|
||||
|
||||
test("continue or null state leaves inbound text unchanged", () => {
|
||||
const base = "我先這樣"
|
||||
assert.equal(applyStopStateToInboundText(base, null), base)
|
||||
assert.equal(
|
||||
applyStopStateToInboundText(base, {
|
||||
lastChoice: "continue",
|
||||
lastChoiceAt: "2026-05-13T00:00:00Z",
|
||||
sourceMessageId: "1",
|
||||
sourceCallbackId: "cb-1",
|
||||
active: true,
|
||||
}),
|
||||
base
|
||||
)
|
||||
})
|
||||
Reference in New Issue
Block a user