mirror of
https://github.com/anthropics/claude-plugins-official.git
synced 2026-03-21 11:53:08 +00:00
Compare commits
2 Commits
kenneth/te
...
kenneth/te
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3d8042f259 | ||
|
|
1daff5f224 |
@@ -15,7 +15,7 @@ import {
|
|||||||
ListToolsRequestSchema,
|
ListToolsRequestSchema,
|
||||||
CallToolRequestSchema,
|
CallToolRequestSchema,
|
||||||
} from '@modelcontextprotocol/sdk/types.js'
|
} from '@modelcontextprotocol/sdk/types.js'
|
||||||
import { Bot, InputFile, type Context } from 'grammy'
|
import { Bot, GrammyError, InputFile, type Context } from 'grammy'
|
||||||
import type { ReactionTypeEmoji } from 'grammy/types'
|
import type { ReactionTypeEmoji } from 'grammy/types'
|
||||||
import { randomBytes } from 'crypto'
|
import { randomBytes } from 'crypto'
|
||||||
import { readFileSync, writeFileSync, mkdirSync, readdirSync, rmSync, statSync, renameSync, realpathSync, chmodSync } from 'fs'
|
import { readFileSync, writeFileSync, mkdirSync, readdirSync, rmSync, statSync, renameSync, realpathSync, chmodSync } from 'fs'
|
||||||
@@ -304,7 +304,7 @@ function checkApprovals(): void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!STATIC) setInterval(checkApprovals, 5000).unref()
|
if (!STATIC) setInterval(checkApprovals, 5000)
|
||||||
|
|
||||||
// Telegram caps messages at 4096 chars. Split long replies, preferring
|
// Telegram caps messages at 4096 chars. Split long replies, preferring
|
||||||
// paragraph boundaries when chunkMode is 'newline'.
|
// paragraph boundaries when chunkMode is 'newline'.
|
||||||
@@ -507,24 +507,6 @@ mcp.setRequestHandler(CallToolRequestSchema, async req => {
|
|||||||
|
|
||||||
await mcp.connect(new StdioServerTransport())
|
await mcp.connect(new StdioServerTransport())
|
||||||
|
|
||||||
// When Claude Code closes the MCP connection, stdin gets EOF. Without this
|
|
||||||
// the bot keeps polling forever as a zombie, holding the token and blocking
|
|
||||||
// the next session with 409 Conflict.
|
|
||||||
let shuttingDown = false
|
|
||||||
function shutdown(): void {
|
|
||||||
if (shuttingDown) return
|
|
||||||
shuttingDown = true
|
|
||||||
process.stderr.write('telegram channel: shutting down\n')
|
|
||||||
// bot.stop() signals the poll loop to end; the current getUpdates request
|
|
||||||
// may take up to its long-poll timeout to return. Force-exit after 2s.
|
|
||||||
setTimeout(() => process.exit(0), 2000)
|
|
||||||
void Promise.resolve(bot.stop()).finally(() => process.exit(0))
|
|
||||||
}
|
|
||||||
process.stdin.on('end', shutdown)
|
|
||||||
process.stdin.on('close', shutdown)
|
|
||||||
process.on('SIGTERM', shutdown)
|
|
||||||
process.on('SIGINT', shutdown)
|
|
||||||
|
|
||||||
bot.on('message:text', async ctx => {
|
bot.on('message:text', async ctx => {
|
||||||
await handleInbound(ctx, ctx.message.text, undefined)
|
await handleInbound(ctx, ctx.message.text, undefined)
|
||||||
})
|
})
|
||||||
@@ -611,9 +593,35 @@ async function handleInbound(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
void bot.start({
|
// 409 Conflict = another getUpdates consumer is still active (zombie from a
|
||||||
onStart: info => {
|
// previous session, or a second Claude Code instance). Retry with backoff
|
||||||
botUsername = info.username
|
// until the slot frees up instead of crashing on the first rejection.
|
||||||
process.stderr.write(`telegram channel: polling as @${info.username}\n`)
|
void (async () => {
|
||||||
},
|
for (let attempt = 1; ; attempt++) {
|
||||||
})
|
try {
|
||||||
|
await bot.start({
|
||||||
|
onStart: info => {
|
||||||
|
botUsername = info.username
|
||||||
|
process.stderr.write(`telegram channel: polling as @${info.username}\n`)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return // bot.stop() was called — clean exit from the loop
|
||||||
|
} catch (err) {
|
||||||
|
if (err instanceof GrammyError && err.error_code === 409) {
|
||||||
|
const delay = Math.min(1000 * attempt, 15000)
|
||||||
|
const detail = attempt === 1
|
||||||
|
? ' — another instance is polling (zombie session, or a second Claude Code running?)'
|
||||||
|
: ''
|
||||||
|
process.stderr.write(
|
||||||
|
`telegram channel: 409 Conflict${detail}, retrying in ${delay / 1000}s\n`,
|
||||||
|
)
|
||||||
|
await new Promise(r => setTimeout(r, delay))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// bot.stop() mid-setup rejects with grammy's "Aborted delay" — expected, not an error.
|
||||||
|
if (err instanceof Error && err.message === 'Aborted delay') return
|
||||||
|
process.stderr.write(`telegram channel: polling failed: ${err}\n`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})()
|
||||||
|
|||||||
Reference in New Issue
Block a user