mirror of
https://github.com/NanmiCoder/claude-code-haha.git
synced 2026-10-10 11:53:10 +08:00
814c02a786
Connecting Feishu meant creating a bot by hand on the open platform and pasting an App ID and App Secret back. Feishu also exposes an RFC 8628 device-authorization flow, so the desktop can now render a QR code, and confirming it in the app creates the bot and stores its credentials directly. `adapters/feishu/registration.ts` implements that protocol rather than importing `registerApp` from `@larksuiteoapi/node-sdk@1.73`: the repository pins 1.60 for the chat client, and the SDK runs the whole poll inside one un-cancellable promise where the desktop needs the stateless begin/poll pair the DingTalk registration already uses. The scan is create-only, so it can never rewrite the configuration of a bot the user already runs, and it pre-fills exactly the scopes, events and callbacks this adapter calls. International tenants finish on Lark's domain, which is now persisted and honoured by the client. WeCom, QQ and Slack join the same session model. WeCom and QQ bind by scanning; Slack has no scan flow, so it uses an app manifest that pre-fills the scopes and Socket Mode. All three run over long connections, so no public callback URL is needed, and all three accept private chats only — pairing authorizes one person, and answering in a group would extend that authorization to everyone else in the room. They are built on a new `adapters/common/chat-runtime.ts` instead of a fourth copy of the loop the five existing adapters each carry. A platform supplies a `ChatPort` — how to say something, how to open a streaming reply, optionally how to send an image — and the runtime owns pairing, command routing, session restore, permission bookkeeping and the translation of the server's stream. The existing five are deliberately left on their own copies; migrating them is a separate change with its own regression surface. Attachments are downloaded through a deferred loader that runs after the pairing gate and inside the per-chat queue. Resolving them eagerly would let an unpaired stranger make the adapter fetch bytes and write them under ~/.claude/im-downloads — on Slack with the bot token attached — and would let a slow attachment overtake a text message sent after it. The sidecar launcher's per-adapter branches become one table. It is declared above the mode dispatch on purpose: `runAdapters` is hoisted and runs at module top level, so a table declared below it is still in its temporal dead zone when the adapters mode reads it — which type checks, lints and unit tests all miss, and only the compiled binary reveals. Verified with the checks `check:impact` selects: adapters, server, desktop, electron, policy, chat-contract, agent-flow, docs, native (sidecar compile, packaging and an adapters-mode smoke against the real binary) and coverage. The scan flows themselves are not verified against live platforms — that needs real WeCom, QQ and Slack accounts and would create real bots. Claude-Session: https://claude.ai/code/session_01CCGoP316AK7wdQG3Ms6Uwq
104 lines
3.2 KiB
TypeScript
104 lines
3.2 KiB
TypeScript
/**
|
|
* Normalize one inbound Enterprise WeChat message body.
|
|
*
|
|
* The WebSocket channel delivers six message shapes (text / image / mixed /
|
|
* voice / file / video) that all carry their text in a different place. This
|
|
* module flattens them into the fields the shared chat runtime needs, and is
|
|
* the only place that knows those shapes.
|
|
*/
|
|
|
|
export type WecomInboundBody = {
|
|
msgid?: string
|
|
aibotid?: string
|
|
chatid?: string
|
|
chattype?: 'single' | 'group' | string
|
|
msgtype?: string
|
|
from?: { userid?: string }
|
|
create_time?: number
|
|
text?: { content?: string }
|
|
voice?: { content?: string }
|
|
mixed?: { msg_item?: Array<{ msgtype?: string; text?: { content?: string } }> }
|
|
quote?: {
|
|
msgtype?: string
|
|
text?: { content?: string }
|
|
voice?: { content?: string }
|
|
mixed?: { msg_item?: Array<{ msgtype?: string; text?: { content?: string } }> }
|
|
}
|
|
}
|
|
|
|
export type WecomInboundPayload = {
|
|
/** Conversation key: the sender's userid, since only 1:1 chats are routed. */
|
|
chatId: string
|
|
userId: string
|
|
text: string
|
|
dedupKey: string
|
|
}
|
|
|
|
function cleanText(value: unknown): string {
|
|
return typeof value === 'string' ? value.trim() : ''
|
|
}
|
|
|
|
function mixedText(
|
|
mixed: { msg_item?: Array<{ msgtype?: string; text?: { content?: string } }> } | undefined,
|
|
): string {
|
|
return (mixed?.msg_item ?? [])
|
|
.filter((item) => item.msgtype === 'text')
|
|
.map((item) => cleanText(item.text?.content))
|
|
.filter(Boolean)
|
|
.join('\n')
|
|
}
|
|
|
|
/** The user-visible text of a message, whatever shape carried it. */
|
|
export function extractWecomText(body: WecomInboundBody | undefined): string {
|
|
if (!body) return ''
|
|
const parts = [
|
|
cleanText(body.text?.content),
|
|
// WeCom transcribes voice server-side, so a voice note is plain text here.
|
|
cleanText(body.voice?.content),
|
|
mixedText(body.mixed),
|
|
].filter(Boolean)
|
|
return parts.join('\n').trim()
|
|
}
|
|
|
|
/** The text of a quoted message, used as context for the Agent. */
|
|
export function extractWecomQuoteText(body: WecomInboundBody | undefined): string {
|
|
const quote = body?.quote
|
|
if (!quote) return ''
|
|
const parts = [
|
|
cleanText(quote.text?.content),
|
|
cleanText(quote.voice?.content),
|
|
mixedText(quote.mixed),
|
|
].filter(Boolean)
|
|
return parts.join('\n').trim()
|
|
}
|
|
|
|
/**
|
|
* Flatten an inbound body, or return null when it cannot be routed.
|
|
*
|
|
* A message with no sender userid has no identity to authorize, and a group
|
|
* message would answer in a room where pairing authorized only one member —
|
|
* both are dropped here rather than downstream, so the runtime only ever sees
|
|
* routable private messages.
|
|
*/
|
|
export function extractWecomPayload(
|
|
body: WecomInboundBody | undefined,
|
|
): WecomInboundPayload | null {
|
|
if (!body) return null
|
|
const userId = cleanText(body.from?.userid)
|
|
if (!userId) return null
|
|
if (body.chattype === 'group') return null
|
|
|
|
const chatId = userId
|
|
|
|
const quoted = extractWecomQuoteText(body)
|
|
const body_text = extractWecomText(body)
|
|
const text = quoted ? `> ${quoted.replace(/\n/g, '\n> ')}\n\n${body_text}`.trim() : body_text
|
|
|
|
return {
|
|
chatId,
|
|
userId,
|
|
text,
|
|
dedupKey: cleanText(body.msgid) || `${chatId}:${body.create_time ?? ''}:${body_text.slice(0, 32)}`,
|
|
}
|
|
}
|