From e74813254131201dd47ae1535bfad65a2b77edcc Mon Sep 17 00:00:00 2001 From: owo <17620121969@163.com> Date: Tue, 15 Sep 2026 21:37:16 +0800 Subject: [PATCH] fix: make optional services non-blocking --- lib/bridge.js | 20 +++++++++++++++++++- lib/index.d.ts | 2 +- lib/index.js | 4 ++-- src/bridge.ts | 20 +++++++++++++++++++- src/index.ts | 4 ++-- tests/apply.test.ts | 6 +++++- 6 files changed, 48 insertions(+), 8 deletions(-) diff --git a/lib/bridge.js b/lib/bridge.js index 8feece3..ba70c02 100644 --- a/lib/bridge.js +++ b/lib/bridge.js @@ -1345,7 +1345,25 @@ export class TelegramBridge { } // ── ask_user_question: TG answering via dual-path race with the UI ── userQuestions() { - return this.ctx.userQuestions; + const ctx = this.ctx; + if (typeof ctx.get === 'function') { + try { + const service = ctx.get('userQuestions'); + if (service) + return service; + } + catch { + // Continue to the plain-object fallback used by tests and simple hosts. + } + } + try { + return Object.prototype.hasOwnProperty.call(ctx, 'userQuestions') + ? ctx.userQuestions + : undefined; + } + catch { + return undefined; + } } /** * Wrap the UI provider's ask() so Telegram gets a parallel answer path. diff --git a/lib/index.d.ts b/lib/index.d.ts index d8b8769..b446764 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -1,7 +1,7 @@ import type { Context } from '@deepseek-ai/cordis'; import Schema from '@deepseek-ai/schemastery'; export declare const name = "dsh-telegram-channel"; -/** agents: followup; apiProxy: workspace/session catalog + /model; userQuestions: TG answer hook (Cordis requires inject). */ +/** agents: followup; apiProxy and userQuestions are optional runtime services. */ export declare const inject: string[]; export interface TelegramChannelConfig { token?: string; diff --git a/lib/index.js b/lib/index.js index cc2d308..e17195a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,8 +1,8 @@ import Schema from '@deepseek-ai/schemastery'; import { TelegramBridge } from './bridge.js'; export const name = 'dsh-telegram-channel'; -/** agents: followup; apiProxy: workspace/session catalog + /model; userQuestions: TG answer hook (Cordis requires inject). */ -export const inject = ['agents', 'apiProxy', 'userQuestions']; +/** agents: followup; apiProxy and userQuestions are optional runtime services. */ +export const inject = ['agents']; export const Config = Schema.object({ token: Schema.string().default(''), allowedUserIds: Schema.array(Schema.number()).default([]), diff --git a/src/bridge.ts b/src/bridge.ts index ee2a4f8..8210a2b 100644 --- a/src/bridge.ts +++ b/src/bridge.ts @@ -1521,7 +1521,25 @@ export class TelegramBridge { // ── ask_user_question: TG answering via dual-path race with the UI ── private userQuestions(): UserQuestionsServiceLike | undefined { - return (this.ctx as unknown as { userQuestions?: UserQuestionsServiceLike }).userQuestions + const ctx = this.ctx as Context & { + get?: (name: string, strict?: boolean) => unknown + userQuestions?: UserQuestionsServiceLike + } + if (typeof ctx.get === 'function') { + try { + const service = ctx.get('userQuestions') as UserQuestionsServiceLike | undefined + if (service) return service + } catch { + // Continue to the plain-object fallback used by tests and simple hosts. + } + } + try { + return Object.prototype.hasOwnProperty.call(ctx, 'userQuestions') + ? ctx.userQuestions + : undefined + } catch { + return undefined + } } /** diff --git a/src/index.ts b/src/index.ts index cb48213..1475d95 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,8 +3,8 @@ import Schema from '@deepseek-ai/schemastery' import { TelegramBridge } from './bridge.js' export const name = 'dsh-telegram-channel' -/** agents: followup; apiProxy: workspace/session catalog + /model; userQuestions: TG answer hook (Cordis requires inject). */ -export const inject = ['agents', 'apiProxy', 'userQuestions'] +/** agents: followup; apiProxy and userQuestions are optional runtime services. */ +export const inject = ['agents'] export interface TelegramChannelConfig { token?: string diff --git a/tests/apply.test.ts b/tests/apply.test.ts index 3f26bc6..350b8ec 100644 --- a/tests/apply.test.ts +++ b/tests/apply.test.ts @@ -1,6 +1,10 @@ import assert from 'node:assert/strict' import test from 'node:test' -import { apply } from '../src/index.ts' +import { apply, inject } from '../src/index.ts' + +test('optional host services are not hard Cordis dependencies', () => { + assert.deepEqual(inject, ['agents']) +}) test('apply without token does not throw and does not start effect', () => { const effects: string[] = []