A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Add the Discord IM adapter
Fei2411d76fbe814917
diff --git a/ds-bot/src/host/im/format.js b/ds-bot/src/host/im/format.jsindex 848a1c52..ce020856 100644--- a/ds-bot/src/host/im/format.js+++ b/ds-bot/src/host/im/format.js@@ -137,6 +137,27 @@ function forceCut(block, limit) {// Chunks of one Markdown message, each under `limit` rendered characters. Block// boundaries are kept where possible; a fence is only split when it alone is too big.+// Plain markdown chunks at a byte-pair limit: paragraphs and lines first, a+// hard cut at the limit for one over-long run. For platforms that take+// markdown as-is (Discord, QQ, WeCom, DingTalk, Slack has its own converter).+export function chunkMarkdown(markdown, limit) {+ const chunks = []+ let rest = String(markdown ?? '')+ while (rest !== '') {+ if (points(rest) <= limit) { chunks.push(rest); break }+ let at = limit+ const newline = rest.lastIndexOf('\n', at)+ if (newline > limit / 2) at = newline+ else {+ const space = rest.lastIndexOf(' ', at)+ if (space > limit / 2) at = space+ }+ chunks.push(rest.slice(0, at))+ rest = rest.slice(at).replace(/^\s+/, '')+ }+ return chunks+}+export function chunkTelegram(markdown, limit = 4096) {const chunks = []let html = ''diff --git a/ds-bot/src/host/im/platforms/discord.js b/ds-bot/src/host/im/platforms/discord.jsnew file mode 100644index 00000000..cc8365d3--- /dev/null+++ b/ds-bot/src/host/im/platforms/discord.js@@ -0,0 +1,234 @@+// Discord: the Gateway over a WebSocket and the REST API for sends. DMs have no+// guild_id; a group message counts as a mention when the bot user is in+// `mentions`. Buttons are components; interactions come back as+// INTERACTION_CREATE and are answered with a deferred update (type 6).+// Wire details ported from dsh-im (MIT, see NOTICE).+import { chunkMarkdown, toPlain } from '../format.js'++const API = 'https://discord.com/api/v10'+const GATEWAY = 'wss://gateway.discord.gg/?v=10&encoding=json'+// guilds | guild messages | direct messages | message content+const INTENTS = (1 << 0) | (1 << 9) | (1 << 12) | (1 << 15)+const TEXT_LIMIT = 2000++const apiBase = env => String(env.config?.api ?? API).replace(/\/+$/, '')+const wsBase = env => String(env.config?.gateway ?? GATEWAY)++const errorOf = (body, status) => {+ const error = new Error(String(body?.message ?? `Discord answered ${status}`))+ error.status = status+ if (body?.errors?.retry_after !== undefined || body?.retry_after !== undefined) error.retryAfter = Number(body.errors?.retry_after ?? body.retry_after) * 1000 / 1000+ // Discord's retry_after is in seconds, and may be fractional.+ if (body?.retry_after !== undefined) error.retryAfter = Number(body.retry_after)+ error.permanent = status >= 400 && status < 500 && status !== 429+ return error+}++export default {+ id: 'discord',++ async check(fields, env) {+ const me = await request(env, fields, '/users/@me')+ if (me?.id === undefined || me.bot !== true) throw new Error('This token does not belong to a Discord bot')+ return { account: { id: String(me.id), name: me.username ? `@${me.username}` : String(me.id) } }+ },++ connect(fields, env, handlers) {+ const token = String(fields.token ?? '').trim()+ let stopped = false+ let socket+ let heartbeatTimer+ let heartbeatAcked = true+ let sequence+ let sessionId+ let resumeUrl+ let generation = 0+ let reconnectTimer+ const self = { id: undefined, name: '' }++ const rest = async (path, options = {}) => {+ const response = await env.fetch(`${apiBase(env)}${path}`, {+ method: options.method ?? 'POST',+ headers: { 'content-type': 'application/json', authorization: `Bot ${token}` },+ body: options.body === undefined ? undefined : JSON.stringify(options.body),+ })+ const body = await response.json().catch(() => ({}))+ if (!response.ok) throw errorOf(body, response.status)+ return body+ }++ const send = (packet) => {+ if (socket?.readyState === 1) socket.send(JSON.stringify(packet))+ }+ const heartbeat = () => send({ op: 1, d: sequence ?? null })+ const scheduleHeartbeat = (interval) => {+ clearTimeout(heartbeatTimer)+ heartbeatAcked = true+ const beat = () => {+ if (!heartbeatAcked) { socket?.close(4000, 'Heartbeat lost'); return }+ heartbeatAcked = false+ heartbeat()+ heartbeatTimer = setTimeout(beat, interval)+ }+ heartbeatTimer = setTimeout(beat, interval * Math.random())+ heartbeatTimer.unref?.()+ }++ const onMessageEvent = async (data) => {+ if (data.author?.bot === true) return+ const direct = data.guild_id === undefined+ const mentioned = direct ? false : (data.mentions ?? []).some(user => String(user.id) === self.id)+ const chat = {+ key: String(data.channel_id),+ kind: direct ? 'direct' : 'group',+ title: data.channel?.name ?? direct ? String(data.author?.username ?? '') : '',+ }+ const images = []+ const files = []+ for (const item of data.attachments ?? []) {+ try {+ const response = await env.fetch(item.url)+ const bytes = new Uint8Array(await response.arrayBuffer())+ const mediaType = String(item.content_type ?? '')+ if (mediaType.startsWith('image/')) images.push({ mediaType, data: Buffer.from(bytes).toString('base64') })+ else files.push({ name: String(item.filename ?? 'file'), data: bytes })+ } catch { /* an attachment that cannot be fetched just does not arrive */ }+ }+ const text = String(data.content ?? '')+ await handlers.onMessage({+ chat,+ sender: { id: String(data.author?.id ?? ''), name: String(data.author?.username ?? '') },+ text: mentioned && self.name ? text.replaceAll(`<@${self.id}>`, '').replaceAll(`<@!${self.id}>`, '').trim() : text,+ images, files, mentioned, messageId: String(data.id ?? ''),+ })+ }++ const onInteraction = async (data) => {+ // Type 3 is a component press: answer it with a deferred update and hand+ // the action to the core.+ if (data.type !== 3) return+ await rest(`/interactions/${data.id}/${data.token}/callback`, { body: { type: 6 } }).catch(() => {})+ const from = data.user ?? data.member?.user ?? {}+ const chat = { key: String(data.channel_id ?? data.channel?.id ?? ''), kind: data.guild_id === undefined ? 'direct' : 'group', title: '' }+ await handlers.onAction({+ chat,+ sender: { id: String(from.id ?? ''), name: String(from.username ?? '') },+ data: String(data.data?.custom_id ?? ''),+ messageId: String(data.message?.id ?? ''),+ })+ }++ let gatewayUrl+ const open = (resume) => {+ const gen = generation+ const url = resume && resumeUrl ? resumeUrl : gatewayUrl+ try {+ socket = new env.WebSocket(url)+ } catch (error) {+ handlers.onStatus({ state: 'error', error: `Discord could not open a socket: ${error?.message ?? error}` })+ return+ }+ socket.addEventListener('message', (event) => {+ if (stopped || generation !== gen) return+ let packet+ try { packet = JSON.parse(String(event.data)) } catch { return }+ if (Number.isSafeInteger(packet.s)) sequence = packet.s+ if (packet.op === 10) {+ scheduleHeartbeat(Number(packet.d?.heartbeat_interval) || 40_000)+ send(resume && sessionId+ ? { op: 6, d: { token, session_id: sessionId, seq: sequence ?? null } }+ : { op: 2, d: { token, intents: INTENTS, properties: { os: process.platform, browser: 'ds-bot', device: 'ds-bot' } } })+ return+ }+ if (packet.op === 11) { heartbeatAcked = true; return }+ if (packet.op === 1) { heartbeat(); return }+ if (packet.op === 7) { socket.close(4000, 'Reconnect requested'); return }+ if (packet.op === 9) {+ if (packet.d !== true) { sessionId = undefined; resumeUrl = undefined; sequence = undefined }+ socket.close(4000, 'Invalid session')+ return+ }+ if (packet.op !== 0) return+ if (packet.t === 'READY') {+ sessionId = packet.d?.session_id+ resumeUrl = packet.d?.resume_gateway_url+ self.id = String(packet.d?.user?.id ?? '')+ self.name = String(packet.d?.user?.username ?? '')+ handlers.onStatus({ state: 'ready', account: { id: self.id, name: self.name ? `@${self.name}` : self.id } })+ } else if (packet.t === 'RESUMED') {+ handlers.onStatus({ state: 'ready' })+ } else if (packet.t === 'MESSAGE_CREATE') {+ void onMessageEvent(packet.d).catch(error => env.log?.warn?.('ds-bot im: discord message failed: %s', error?.message ?? error))+ } else if (packet.t === 'INTERACTION_CREATE') {+ void onInteraction(packet.d).catch(error => env.log?.warn?.('ds-bot im: discord action failed: %s', error?.message ?? error))+ }+ })+ socket.addEventListener('close', () => {+ clearTimeout(heartbeatTimer)+ if (stopped || generation !== gen) return+ handlers.onStatus({ state: 'connecting' })+ // Resume keeps session_id when the close code allows it; a 4004 or an+ // invalid session forces a fresh identify.+ reconnectTimer = setTimeout(() => { if (!stopped) open(sessionId !== undefined) }, 2000)+ reconnectTimer.unref?.()+ })+ socket.addEventListener('error', () => {})+ }++ handlers.onStatus({ state: 'connecting' })+ const gatewaySocket = (base) => {+ try {+ const url = new URL(base)+ url.searchParams.set('v', '10')+ url.searchParams.set('encoding', 'json')+ return url.href+ } catch { return undefined }+ }+ void (async () => {+ try {+ const gateway = await rest('/gateway/bot', { method: 'GET' })+ gatewayUrl = gatewaySocket(gateway?.url) ?? gatewaySocket(wsBase(env))+ } catch {+ gatewayUrl = gatewaySocket(wsBase(env))+ }+ if (gatewayUrl === undefined) handlers.onStatus({ state: 'error', error: 'Discord gave no usable gateway URL' })+ else if (!stopped) open(false)+ })()++ return {+ send: async (chat, { text, buttons }) => {+ const chunks = chunkMarkdown(String(text ?? ''), TEXT_LIMIT)+ let messageId+ for (const [index, chunk] of chunks.entries()) {+ const components = index === chunks.length - 1 && buttons?.length > 0+ ? [{ type: 1, components: buttons.slice(0, 5).map(button => ({ type: 2, style: 1, label: button.label.slice(0, 80), custom_id: button.data })) }]+ : []+ const sent = await rest(`/channels/${chat.key}/messages`, { body: { content: chunk, components } })+ messageId = String(sent?.id ?? messageId ?? '')+ }+ return { messageId }+ },+ edit: async (chat, messageId, { text, buttons }) => {+ const components = buttons?.length > 0+ ? [{ type: 1, components: buttons.slice(0, 5).map(button => ({ type: 2, style: 1, label: button.label.slice(0, 80), custom_id: button.data })) }]+ : []+ await rest(`/channels/${chat.key}/messages/${messageId}`, { method: 'PATCH', body: { content: toPlain(text).slice(0, TEXT_LIMIT), components } })+ },+ typing: async (chat) => rest(`/channels/${chat.key}/typing`).then(() => {}, () => {}),+ stop: () => {+ stopped = true+ generation += 1+ clearTimeout(heartbeatTimer)+ clearTimeout(reconnectTimer)+ try { socket?.close(1000, 'stop') } catch { /* socket may not be open */ }+ },+ }+ },+}++async function request(env, fields, path) {+ const response = await env.fetch(`${apiBase(env)}${path}`, { headers: { authorization: `Bot ${String(fields.token ?? '').trim()}` } })+ const body = await response.json().catch(() => ({}))+ if (!response.ok) throw errorOf(body, response.status)+ return body+}diff --git a/ds-bot/src/host/im/platforms/index.js b/ds-bot/src/host/im/platforms/index.jsindex a4f040f6..f865d364 100644--- a/ds-bot/src/host/im/platforms/index.js+++ b/ds-bot/src/host/im/platforms/index.js@@ -1,5 +1,6 @@+import discord from './discord.js'import telegram from './telegram.js'// Every platform with an adapter. The rest of IM_PLATFORMS still show in settings as// coming soon — a connection can only be added for a platform listed here.-export const adapters = { telegram }+export const adapters = { telegram, discord }diff --git a/ds-bot/test/fake-ws.js b/ds-bot/test/fake-ws.jsnew file mode 100644index 00000000..37009232--- /dev/null+++ b/ds-bot/test/fake-ws.js@@ -0,0 +1,90 @@+import { createHash, randomBytes } from 'node:crypto'+import { createServer } from 'node:http'++// A minimal RFC 6455 server for adapter tests: the handshake, masked frame+// decode, unmasked send. Text only, no fragmentation beyond reassembly.+const GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'++const encode = (data, opcode = 0x81) => {+ const payload = Buffer.from(data)+ const length = payload.length+ const head = length < 126 ? 2 : length < 65536 ? 4 : 10+ const frame = Buffer.alloc(head + length)+ frame[0] = opcode+ if (length < 126) frame[1] = length+ else if (length < 65536) { frame[1] = 126; frame.writeUInt16BE(length, 2) }+ else { frame[1] = 127; frame.writeBigUInt64BE(BigInt(length), 2) }+ payload.copy(frame, head)+ return frame+}++// onSocket(socket) runs per connection; socket.send(text) / socket.close() and+// socket.on('message'|'close'). Returns { server, url, sockets } once listening.+export function fakeWsServer({ onSocket, onRequest } = {}) {+ const sockets = []+ const pending = onRequest ?? (() => undefined)+ const server = createServer((req, res) => {+ const answer = pending(req)+ if (answer === undefined) { res.writeHead(404); res.end(); return }+ res.writeHead(200, { 'content-type': 'application/json' })+ res.end(JSON.stringify(answer))+ })+ server.on('upgrade', (req, socket) => {+ const key = req.headers['sec-websocket-key'] ?? randomBytes(16).toString('base64')+ const accept = createHash('sha1').update(key + GUID).digest('base64')+ socket.write(`HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: ${accept}\r\n\r\n`)+ const ws = {+ send: text => socket.write(encode(text)),+ close: (code = 1000) => {+ try { socket.write(encode('', 0x88)) } catch { /* down */ }+ try { socket.destroy() } catch { /* down */ }+ },+ listeners: { message: [], close: [] },+ on(kind, fn) { this.listeners[kind].push(fn) },+ }+ sockets.push(ws)+ let buffer = Buffer.alloc(0)+ const pieces = []+ socket.on('data', (chunk) => {+ buffer = Buffer.concat([buffer, chunk])+ while (buffer.length >= 2) {+ const fin = (buffer[0] & 0x80) !== 0+ const opcode = buffer[0] & 0x0f+ const masked = (buffer[1] & 0x80) !== 0+ let length = buffer[1] & 0x7f+ let at = 2+ if (length === 126) { if (buffer.length < 4) break; length = buffer.readUInt16BE(2); at = 4 }+ else if (length === 127) { if (buffer.length < 10) break; length = Number(buffer.readBigUInt64BE(2)); at = 10 }+ const maskAt = at+ if (masked) at += 4+ if (buffer.length < at + length) break+ let payload = buffer.subarray(at, at + length)+ if (masked) {+ const mask = buffer.subarray(maskAt, maskAt + 4)+ payload = Buffer.from(payload)+ for (let index = 0; index < payload.length; index += 1) payload[index] ^= mask[index % 4]+ }+ buffer = buffer.subarray(at + length)+ if (opcode === 8) {+ socket.write(encode('', 0x88))+ socket.end()+ for (const fn of ws.listeners.close) fn()+ continue+ }+ if (opcode === 9) { socket.write(encode('', 0x8a)); continue }+ pieces.push(payload)+ if (!fin) continue+ const text = Buffer.concat(pieces).toString('utf8')+ pieces.length = 0+ for (const fn of ws.listeners.message) fn(text)+ }+ })+ socket.on('close', () => { for (const fn of ws.listeners.close) fn() })+ socket.on('error', () => {})+ onSocket?.(ws)+ })+ return new Promise(resolve => server.listen(0, '127.0.0.1', () => {+ const url = `ws://127.0.0.1:${server.address().port}`+ resolve({ server, url, sockets, close: () => { for (const ws of sockets) try { ws.close() } catch { /* down */ } server.closeAllConnections?.(); server.close() } })+ }))+}diff --git a/ds-bot/test/im-discord.test.js b/ds-bot/test/im-discord.test.jsnew file mode 100644index 00000000..95d244af--- /dev/null+++ b/ds-bot/test/im-discord.test.js@@ -0,0 +1,143 @@+import assert from 'node:assert/strict'+import { createServer } from 'node:http'+import { test } from 'node:test'+import discord from '../src/host/im/platforms/discord.js'+import { fakeWsServer } from './fake-ws.js'++// A fake Discord: REST answers from `replies`, the gateway sends an op-10 hello+// and READY on identify, and lets the test push dispatch packets.+async function fakeDiscord() {+ const calls = []+ const replies = {}+ const sockets = []+ const packets = []+ const http = createServer((req, res) => {+ let body = ''+ req.on('data', chunk => { body += chunk })+ req.on('end', () => {+ const params = JSON.parse(body || '{}')+ const call = { method: req.method, path: req.url, params }+ calls.push(call)+ let answer = replies[req.url]+ if (answer === undefined && req.url === '/api/v10/users/@me') answer = { result: { id: '81', username: 'fakebot', bot: true } }+ if (answer === undefined && req.url === '/api/v10/gateway/bot') answer = { result: { url: wss.url } }+ if (typeof answer === 'function') answer = answer(params)+ if (answer === undefined) answer = { result: { id: '900' } }+ if (answer.error) {+ res.writeHead(answer.error, { 'content-type': 'application/json' })+ res.end(JSON.stringify({ message: answer.description ?? 'failed', retry_after: answer.retry_after }))+ return+ }+ res.writeHead(200, { 'content-type': 'application/json' })+ res.end(JSON.stringify(answer.result))+ })+ })+ const wss = await fakeWsServer({+ onSocket(ws) {+ ws.on('message', (raw) => {+ const packet = JSON.parse(raw)+ packets.push(packet)+ if (packet.op === 2) {+ ws.send(JSON.stringify({ op: 0, t: 'READY', s: 1, d: { session_id: 's1', resume_gateway_url: wss.url, user: { id: '81', username: 'fakebot' } } }))+ }+ if (packet.op === 6) ws.send(JSON.stringify({ op: 0, t: 'RESUMED', s: 9, d: {} }))+ if (packet.op === 1) ws.send(JSON.stringify({ op: 11, d: null }))+ })+ ws.send(JSON.stringify({ op: 10, d: { heartbeat_interval: 30_000 } }))+ },+ })+ await new Promise(resolve => http.listen(0, '127.0.0.1', resolve))+ return {+ api: `http://127.0.0.1:${http.address().port}/api/v10`,+ gateway: wss.url,+ calls, replies, packets,+ sockets: wss.sockets,+ push: (ws, packet) => ws.send(JSON.stringify(packet)),+ close: () => { wss.close(); http.closeAllConnections?.(); http.close() },+ }+}++const env = (fake, runtime = {}) => ({+ fetch: globalThis.fetch,+ WebSocket: globalThis.WebSocket,+ config: { api: fake.api, gateway: fake.gateway },+ runtime: { get: () => runtime, set: patch => Object.assign(runtime, patch) },+ log: { warn: (...args) => console.error('[im-discord]', ...args) },+})++const until = async (fn, tries = 80) => {+ for (let at = 0; at < tries; at += 1) {+ const value = await fn()+ if (value !== undefined && value !== false) return value+ await new Promise(done => setTimeout(done, 50))+ }+ return undefined+}++test('check validates the bot token and names the account', async (t) => {+ const fake = await fakeDiscord()+ const { account } = await discord.check({ token: 'tok' }, env(fake))+ assert.deepEqual(account, { id: '81', name: '@fakebot' })+ assert.ok(fake.calls.some(call => call.path === '/api/v10/users/@me'))+ await fake.close()+})++test('a DM is direct, a guild message is a mention when the bot is in mentions', async (t) => {+ const fake = await fakeDiscord()+ const messages = []+ const handle = discord.connect({ token: 'tok' }, env(fake), { onMessage: async (msg) => { messages.push(msg) }, onAction: () => {}, onStatus: () => {} })+ t.after(() => { handle.stop(); fake.close() })+ const ws = await until(() => fake.sockets[0])+ await until(() => fake.packets.some(packet => packet.op === 2))+ fake.push(ws, { op: 0, t: 'MESSAGE_CREATE', s: 2, d: { id: '10', channel_id: 'dm1', content: 'hello', author: { id: 'u1', username: 'Fei' } } })+ fake.push(ws, { op: 0, t: 'MESSAGE_CREATE', s: 3, d: { id: '11', channel_id: 'g1', guild_id: 'w1', content: 'hey <@81> ping', author: { id: 'u2', username: 'Yan' }, mentions: [{ id: '81' }] } })+ fake.push(ws, { op: 0, t: 'MESSAGE_CREATE', s: 4, d: { id: '12', channel_id: 'g1', guild_id: 'w1', content: 'noise', author: { id: 'u2', username: 'Yan' }, mentions: [] } })+ await until(() => messages.length === 3)+ assert.equal(messages[0].chat.kind, 'direct')+ assert.equal(messages[1].chat.kind, 'group')+ assert.equal(messages[1].mentioned, true)+ assert.equal(messages[1].text, 'hey ping')+ assert.equal(messages[2].mentioned, false)+})++test('a component press is deferred and reports the action', async (t) => {+ const fake = await fakeDiscord()+ const actions = []+ const handle = discord.connect({ token: 'tok' }, env(fake), { onMessage: () => {}, onAction: async (action) => { actions.push(action) }, onStatus: () => {} })+ t.after(() => { handle.stop(); fake.close() })+ const ws = await until(() => fake.sockets[0])+ await until(() => fake.packets.some(packet => packet.op === 2))+ fake.push(ws, { op: 0, t: 'INTERACTION_CREATE', s: 2, d: { type: 3, id: 'i1', token: 'it', channel_id: 'dm1', user: { id: 'u1', username: 'Fei' }, data: { custom_id: 'q:q1:0' }, message: { id: '77' } } })+ await until(() => actions.length === 1)+ assert.equal(actions[0].data, 'q:q1:0')+ assert.ok(fake.calls.some(call => /interactions\/i1\/it\/callback/.test(call.path) && call.params.type === 6))+})++test('send chunks long text, buttons go on the last chunk, edit patches', async (t) => {+ const fake = await fakeDiscord()+ const handle = discord.connect({ token: 'tok' }, env(fake), { onMessage: () => {}, onAction: () => {}, onStatus: () => {} })+ t.after(() => { handle.stop(); fake.close() })+ await until(() => fake.sockets[0])+ const { messageId } = await handle.send({ key: 'dm1', kind: 'direct' }, { text: 'x'.repeat(4500), buttons: [{ data: 'q:q1:0', label: 'yes' }, { data: 'q:q1:1', label: 'no' }] })+ const posts = fake.calls.filter(call => /messages$/.test(call.path) && call.method === 'POST')+ assert.equal(posts.length, 3)+ assert.ok(posts.every(call => call.params.content.length <= 2000))+ assert.equal(posts[0].params.components.length, 0)+ assert.equal(posts[2].params.components[0].components.length, 2)+ assert.equal(messageId, '900')+ await handle.edit({ key: 'dm1', kind: 'direct' }, '55', { text: 'done', buttons: [] })+ assert.ok(fake.calls.some(call => call.method === 'PATCH' && /messages\/55$/.test(call.path)))+})++test('a dropped socket reconnects and resumes the session', async (t) => {+ const fake = await fakeDiscord()+ const statuses = []+ const handle = discord.connect({ token: 'tok' }, env(fake), { onMessage: () => {}, onAction: () => {}, onStatus: status => statuses.push(status) })+ t.after(() => { handle.stop(); fake.close() })+ const ws = await until(() => fake.sockets[0])+ await until(() => statuses.some(status => status.state === 'ready'))+ ws.close()+ await until(() => fake.sockets.length === 2)+ await until(() => fake.packets.some(packet => packet.op === 6))+ assert.ok(statuses.some(status => status.state === 'connecting'))+})