From 27f02b7460f8be2fbd1b38eaa4a65500d3eb1be0 Mon Sep 17 00:00:00 2001 From: owo <17620121969@163.com> Date: Fri, 14 Aug 2026 16:30:48 +0800 Subject: [PATCH] feat: wire Cordis apply lifecycle with soft-fail token check Co-authored-by: Cursor --- src/bridge.ts | 8 ++++---- src/index.ts | 32 ++++++++++++++++++++++++++++++-- tests/apply.test.ts | 21 +++++++++++++++++++++ 3 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 tests/apply.test.ts diff --git a/src/bridge.ts b/src/bridge.ts index bbf130a..fdf7389 100644 --- a/src/bridge.ts +++ b/src/bridge.ts @@ -3,10 +3,10 @@ import type { AgentHandle } from '@deepseek-ai/dsh-agent' import { createUserMessage } from '@deepseek-ai/dsh-llm/message' import type { ContentBlock, TextBlock } from '@deepseek-ai/dsh-llm/types' import { SessionId, type SessionEvent } from '@deepseek-ai/dsh-session/types' -import { isAuthorized } from './auth.ts' -import { TelegramClient, type TelegramClientLike, type TelegramUpdate } from './client.ts' -import { MSG, parseCommand } from './commands.ts' -import { markdownToHtml, splitMessage } from './format.ts' +import { isAuthorized } from './auth.js' +import { TelegramClient, type TelegramClientLike, type TelegramUpdate } from './client.js' +import { MSG, parseCommand } from './commands.js' +import { markdownToHtml, splitMessage } from './format.js' export interface TelegramBridgeOptions { token: string diff --git a/src/index.ts b/src/index.ts index e148d26..6b2b5a7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ import type { Context } from '@deepseek-ai/cordis' import Schema from '@deepseek-ai/schemastery' +import { TelegramBridge } from './bridge.js' export const name = 'dsh-telegram-channel' export const inject = ['agents'] @@ -26,6 +27,33 @@ export const Config: Schema = Schema.object({ cwd: Schema.string(), }) -export function apply(_ctx: Context, _config: TelegramChannelConfig): void { - // Task 6 fills this in +export function apply(ctx: Context, config: TelegramChannelConfig): void { + const token = (config.token && config.token.length > 0) + ? config.token + : (process.env.DSH_TELEGRAM_TOKEN ?? '') + if (!token) { + ctx.logger.error( + 'dsh-telegram-channel: missing bot token (set config.token or DSH_TELEGRAM_TOKEN); polling not started', + ) + return + } + const bridge = new TelegramBridge(ctx, { + token, + allowedUserIds: config.allowedUserIds ?? [], + allowAllUsers: config.allowAllUsers ?? false, + provider: config.provider ?? 'deepseek-official', + model: config.model ?? 'deepseek-v4-flash', + maxMessageLength: config.maxMessageLength ?? 4096, + cwd: config.cwd ?? process.cwd(), + }) + ctx.effect(() => { + bridge.start() + return () => { void bridge.stop() } + }, 'dsh-telegram-channel.serve') } + +export * from './format.js' +export * from './client.js' +export * from './auth.js' +export * from './commands.js' +export * from './bridge.js' diff --git a/tests/apply.test.ts b/tests/apply.test.ts new file mode 100644 index 0000000..67d7f92 --- /dev/null +++ b/tests/apply.test.ts @@ -0,0 +1,21 @@ +import assert from 'node:assert/strict' +import test from 'node:test' +import { apply } from '../src/index.ts' + +test('apply without token does not throw and does not start effect', () => { + const effects: string[] = [] + const ctx = { + effect(_fn: () => () => void, name: string) { + effects.push(name) + return () => {} + }, + logger: { error() {}, warn() {} }, + agents: {}, + } as any + const prev = process.env.DSH_TELEGRAM_TOKEN + delete process.env.DSH_TELEGRAM_TOKEN + assert.doesNotThrow(() => apply(ctx, { token: '', allowedUserIds: [1] })) + assert.deepEqual(effects, []) + if (prev !== undefined) process.env.DSH_TELEGRAM_TOKEN = prev + else delete process.env.DSH_TELEGRAM_TOKEN +})