feat: wire Cordis apply lifecycle with soft-fail token check
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+4
-4
@@ -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
|
||||
|
||||
+30
-2
@@ -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<TelegramChannelConfig> = 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'
|
||||
|
||||
@@ -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
|
||||
})
|
||||
Reference in New Issue
Block a user