@michengai/dsh-im-connect 1.0.2 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/CHANGELOG.zh-CN.md +16 -0
- package/README.md +6 -5
- package/README.zh-CN.md +7 -6
- package/client.js +90 -16
- package/lib/channels/qq.d.ts +2 -0
- package/lib/channels/qq.d.ts.map +1 -1
- package/lib/channels/qq.js +56 -2
- package/lib/channels/qq.js.map +1 -1
- package/lib/channels/telegram.d.ts +14 -0
- package/lib/channels/telegram.d.ts.map +1 -1
- package/lib/channels/telegram.js +73 -18
- package/lib/channels/telegram.js.map +1 -1
- package/lib/client.js +90 -16
- package/lib/engine/file-delivery.d.ts +2 -0
- package/lib/engine/file-delivery.d.ts.map +1 -1
- package/lib/engine/file-delivery.js +16 -2
- package/lib/engine/file-delivery.js.map +1 -1
- package/lib/engine/gateway.d.ts.map +1 -1
- package/lib/engine/gateway.js +5 -3
- package/lib/engine/gateway.js.map +1 -1
- package/lib/engine/router.d.ts +6 -2
- package/lib/engine/router.d.ts.map +1 -1
- package/lib/engine/router.js +9 -1
- package/lib/engine/router.js.map +1 -1
- package/lib/engine/types.d.ts +6 -0
- package/lib/engine/types.d.ts.map +1 -1
- package/lib/manager.d.ts +4 -1
- package/lib/manager.d.ts.map +1 -1
- package/lib/manager.js +13 -2
- package/lib/manager.js.map +1 -1
- package/package.json +2 -2
- package/src/channels/qq.ts +51 -2
- package/src/channels/telegram.ts +86 -18
- package/src/engine/file-delivery.ts +15 -2
- package/src/engine/gateway.ts +7 -5
- package/src/engine/router.ts +11 -3
- package/src/engine/types.ts +7 -0
- package/src/manager.ts +19 -4
package/src/channels/telegram.ts
CHANGED
|
@@ -16,7 +16,16 @@ export interface TelegramConfig {
|
|
|
16
16
|
|
|
17
17
|
interface TgUpdate {
|
|
18
18
|
update_id: number
|
|
19
|
-
callback_query?: {
|
|
19
|
+
callback_query?: {
|
|
20
|
+
id: string
|
|
21
|
+
data?: string
|
|
22
|
+
from: { id: number }
|
|
23
|
+
message?: {
|
|
24
|
+
message_id: number
|
|
25
|
+
chat: { id: number; type: string }
|
|
26
|
+
message_thread_id?: number
|
|
27
|
+
}
|
|
28
|
+
}
|
|
20
29
|
message?: {
|
|
21
30
|
message_id: number
|
|
22
31
|
chat: { id: number; type: string }
|
|
@@ -29,11 +38,40 @@ interface TgUpdate {
|
|
|
29
38
|
entities?: Array<{ type: string; offset: number; length: number }>
|
|
30
39
|
caption_entities?: Array<{ type: string; offset: number; length: number }>
|
|
31
40
|
reply_to_message?: { from?: { id: number } }
|
|
41
|
+
message_thread_id?: number
|
|
32
42
|
}
|
|
33
43
|
}
|
|
34
44
|
|
|
35
45
|
const API = 'https://api.telegram.org'
|
|
36
46
|
|
|
47
|
+
/**
|
|
48
|
+
* 编码 Telegram 会话键:
|
|
49
|
+
* 普通私聊 / 群聊保持 "<chatId>";
|
|
50
|
+
* 论坛 / 话题群组(Forum / Topics)携带 threadId 时编码为 "<chatId>:<threadId>"。
|
|
51
|
+
*/
|
|
52
|
+
export function telegramChatId(chatId: string | number, threadId?: number): string {
|
|
53
|
+
return threadId !== undefined ? `${chatId}:${threadId}` : String(chatId)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 解码 Telegram 会话键:
|
|
58
|
+
* 分割出基础 chat_id 以及可选的 message_thread_id。
|
|
59
|
+
*/
|
|
60
|
+
export function parseTelegramChatId(chatId: string): { chatId: number | string; threadId?: number } {
|
|
61
|
+
const index = chatId.indexOf(':')
|
|
62
|
+
if (index < 0) {
|
|
63
|
+
const num = Number(chatId)
|
|
64
|
+
return { chatId: Number.isSafeInteger(num) ? num : chatId, threadId: undefined }
|
|
65
|
+
}
|
|
66
|
+
const idPart = chatId.slice(0, index)
|
|
67
|
+
const num = Number(idPart)
|
|
68
|
+
const rawThread = chatId.slice(index + 1)
|
|
69
|
+
return {
|
|
70
|
+
chatId: Number.isSafeInteger(num) ? num : idPart,
|
|
71
|
+
threadId: /^[1-9]\d*$/.test(rawThread) ? Number(rawThread) : undefined,
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
37
75
|
const FENCE_LINE = /^\s{0,3}(?:`{3,}|~{3,})/
|
|
38
76
|
/** 块级语法行:列表、标题、引用、表格都不需要也不应该补硬换行。 */
|
|
39
77
|
const BLOCK_LINE = /^\s{0,3}(?:#{1,6}\s|[-*+]\s|\d{1,9}[.)]\s|>|\|)/
|
|
@@ -104,10 +142,12 @@ export function createTelegramChannel(config: TelegramConfig, log: (line: string
|
|
|
104
142
|
}
|
|
105
143
|
|
|
106
144
|
async function sendMessageText(chatId: string, text: string): Promise<void> {
|
|
145
|
+
const route = parseTelegramChatId(chatId)
|
|
146
|
+
const threadParam = route.threadId !== undefined ? { message_thread_id: route.threadId } : {}
|
|
107
147
|
const markdown = richDisabled ? undefined : toTelegramRichMarkdown(text)
|
|
108
148
|
if (markdown !== undefined) {
|
|
109
149
|
try {
|
|
110
|
-
await api('sendRichMessage', { chat_id:
|
|
150
|
+
await api('sendRichMessage', { chat_id: route.chatId, ...threadParam, rich_message: { markdown } })
|
|
111
151
|
return
|
|
112
152
|
} catch (error) {
|
|
113
153
|
// 响应超时或连接中断时,原消息可能已经送达,不能再发一份。
|
|
@@ -117,7 +157,7 @@ export function createTelegramChannel(config: TelegramConfig, log: (line: string
|
|
|
117
157
|
if (richUnsupported(error)) richDisabled = true
|
|
118
158
|
}
|
|
119
159
|
}
|
|
120
|
-
await api('sendMessage', { chat_id:
|
|
160
|
+
await api('sendMessage', { chat_id: route.chatId, ...threadParam, text })
|
|
121
161
|
}
|
|
122
162
|
|
|
123
163
|
async function api<T>(method: string, body: Record<string, unknown>, timeoutMs = 30_000): Promise<T> {
|
|
@@ -201,8 +241,15 @@ export function createTelegramChannel(config: TelegramConfig, log: (line: string
|
|
|
201
241
|
if (update.callback_query) {
|
|
202
242
|
const callback = update.callback_query
|
|
203
243
|
await api('answerCallbackQuery', { callback_query_id: callback.id }).catch(() => log('[telegram] 按钮回执失败'))
|
|
204
|
-
if (callback.data && callback.message) await handler?.({
|
|
205
|
-
|
|
244
|
+
if (callback.data && callback.message) await handler?.({
|
|
245
|
+
chatId: telegramChatId(callback.message.chat.id, callback.message.message_thread_id),
|
|
246
|
+
userId: String(callback.from.id),
|
|
247
|
+
kind: callback.message.chat.type === 'private' ? 'dm' : 'group',
|
|
248
|
+
addressed: true,
|
|
249
|
+
text: '',
|
|
250
|
+
actionToken: callback.data,
|
|
251
|
+
messageId: `callback:${callback.id}`,
|
|
252
|
+
})
|
|
206
253
|
continue
|
|
207
254
|
}
|
|
208
255
|
const message = update.message
|
|
@@ -227,7 +274,10 @@ export function createTelegramChannel(config: TelegramConfig, log: (line: string
|
|
|
227
274
|
addressed = album.addressed
|
|
228
275
|
if (++album.count > 20) {
|
|
229
276
|
album.pending = []
|
|
230
|
-
if (addressed)
|
|
277
|
+
if (addressed) {
|
|
278
|
+
const threadParam = message.message_thread_id !== undefined ? { message_thread_id: message.message_thread_id } : {}
|
|
279
|
+
await api('sendMessage', { chat_id: message.chat.id, ...threadParam, text: config.host ? notice(config.host, '相册图片过多,请分批发送。') : '相册图片过多,请分批发送。' }).catch(() => undefined)
|
|
280
|
+
}
|
|
231
281
|
continue
|
|
232
282
|
}
|
|
233
283
|
if (!addressed) { album.pending.push(update); continue }
|
|
@@ -246,14 +296,15 @@ export function createTelegramChannel(config: TelegramConfig, log: (line: string
|
|
|
246
296
|
if (stopped) break
|
|
247
297
|
// Do not expose a token-bearing download URL through error messages.
|
|
248
298
|
log('[telegram] 图片接收失败')
|
|
249
|
-
|
|
299
|
+
const threadParam = message.message_thread_id !== undefined ? { message_thread_id: message.message_thread_id } : {}
|
|
300
|
+
await api('sendMessage', { chat_id: message.chat.id, ...threadParam, text: config.host ? notice(config.host, '图片接收失败,请检查图片格式和大小后重新发送完整消息。') : '图片接收失败,请检查图片格式和大小后重新发送完整消息。' }).catch(() => log('[telegram] 图片失败提示发送失败'))
|
|
250
301
|
// A failed image caption must never turn into a text command.
|
|
251
302
|
continue
|
|
252
303
|
}
|
|
253
304
|
}
|
|
254
305
|
if (stopped) break
|
|
255
306
|
void handler?.({
|
|
256
|
-
chatId:
|
|
307
|
+
chatId: telegramChatId(message.chat.id, message.message_thread_id),
|
|
257
308
|
userId: message.from ? String(message.from.id) : undefined,
|
|
258
309
|
username: message.from?.username ?? message.from?.first_name,
|
|
259
310
|
text,
|
|
@@ -306,38 +357,55 @@ export function createTelegramChannel(config: TelegramConfig, log: (line: string
|
|
|
306
357
|
await sendMessageText(chatId, text)
|
|
307
358
|
},
|
|
308
359
|
async sendFile(chatId, file, signal) {
|
|
360
|
+
const route = parseTelegramChatId(chatId)
|
|
309
361
|
const form = fileForm(file, 'document')
|
|
310
|
-
form.append('chat_id', chatId)
|
|
362
|
+
form.append('chat_id', String(route.chatId))
|
|
363
|
+
if (route.threadId !== undefined) {
|
|
364
|
+
form.append('message_thread_id', String(route.threadId))
|
|
365
|
+
}
|
|
311
366
|
await fileRequest(`${API}/bot${token}/sendDocument`, { method: 'POST', body: form, signal: timeoutSignal(120_000, AbortSignal.any([...(signal ? [signal] : []), ...(lifecycle ? [lifecycle.signal] : [])])) })
|
|
312
367
|
},
|
|
313
368
|
async sendChoices(message, text, buttons) {
|
|
314
|
-
const
|
|
369
|
+
const route = parseTelegramChatId(message.chatId)
|
|
370
|
+
const threadParam = route.threadId !== undefined ? { message_thread_id: route.threadId } : {}
|
|
371
|
+
const sent = await api<{ message_id: number }>('sendMessage', {
|
|
372
|
+
chat_id: route.chatId,
|
|
373
|
+
...threadParam,
|
|
374
|
+
text,
|
|
375
|
+
reply_markup: { inline_keyboard: buttons.map(button => [{ text: button.label.slice(0, 60), callback_data: button.token }]) },
|
|
376
|
+
}).catch(error => { throw choiceSendError(error) })
|
|
315
377
|
if (!Number.isSafeInteger(sent?.message_id)) return
|
|
316
378
|
return { close: async (status: string) => {
|
|
317
|
-
await api('editMessageText', { chat_id:
|
|
379
|
+
await api('editMessageText', { chat_id: route.chatId, message_id: sent.message_id, text: `${text}\n\n${status}`, reply_markup: { inline_keyboard: [] } })
|
|
318
380
|
} }
|
|
319
381
|
},
|
|
320
382
|
typingIntervalMs: 5000,
|
|
321
383
|
async addStatusReaction(message, state, _label, signal) {
|
|
322
384
|
if (!message.messageId || state === 'cancelled') return
|
|
385
|
+
const route = parseTelegramChatId(message.chatId)
|
|
323
386
|
const emoji = (state === 'success' || state === 'ended') ? '👍' : state === 'error' ? '👎' : state === 'waiting' ? '🤔' : '👀'
|
|
324
387
|
await fileRequest(`${API}/bot${token}/setMessageReaction`, {
|
|
325
388
|
method: 'POST', headers: { 'content-type': 'application/json' }, signal,
|
|
326
|
-
body: JSON.stringify({ chat_id:
|
|
389
|
+
body: JSON.stringify({ chat_id: route.chatId, message_id: Number(message.messageId), reaction: [{ type: 'emoji', emoji }] }),
|
|
327
390
|
})
|
|
328
391
|
return emoji
|
|
329
392
|
},
|
|
330
393
|
async removeStatusReaction(message, _reaction, signal) {
|
|
394
|
+
const route = parseTelegramChatId(message.chatId)
|
|
331
395
|
await fileRequest(`${API}/bot${token}/setMessageReaction`, {
|
|
332
396
|
method: 'POST', headers: { 'content-type': 'application/json' }, signal,
|
|
333
|
-
body: JSON.stringify({ chat_id:
|
|
397
|
+
body: JSON.stringify({ chat_id: route.chatId, message_id: Number(message.messageId), reaction: [] }),
|
|
334
398
|
})
|
|
335
399
|
},
|
|
336
400
|
async sendAction(chatId) {
|
|
337
|
-
|
|
401
|
+
const route = parseTelegramChatId(chatId)
|
|
402
|
+
const threadParam = route.threadId !== undefined ? { message_thread_id: route.threadId } : {}
|
|
403
|
+
await api('sendChatAction', { chat_id: route.chatId, ...threadParam, action: 'typing' }).catch(() => undefined)
|
|
338
404
|
},
|
|
339
405
|
async beginReply(chatId): Promise<ReplyStream> {
|
|
340
|
-
const
|
|
406
|
+
const route = parseTelegramChatId(chatId)
|
|
407
|
+
const threadParam = route.threadId !== undefined ? { message_thread_id: route.threadId } : {}
|
|
408
|
+
const first = await api<{ message_id: number }>('sendMessage', { chat_id: route.chatId, ...threadParam, text: '…' })
|
|
341
409
|
let last = '…'
|
|
342
410
|
let timer: ReturnType<typeof setTimeout> | undefined
|
|
343
411
|
let pending: string | undefined
|
|
@@ -351,20 +419,20 @@ export function createTelegramChannel(config: TelegramConfig, log: (line: string
|
|
|
351
419
|
const markdown = richDisabled ? undefined : toTelegramRichMarkdown(next)
|
|
352
420
|
if (markdown !== undefined) {
|
|
353
421
|
try {
|
|
354
|
-
await api('editMessageText', { chat_id:
|
|
422
|
+
await api('editMessageText', { chat_id: route.chatId, message_id: first.message_id, rich_message: { markdown } })
|
|
355
423
|
last = next
|
|
356
424
|
return
|
|
357
425
|
} catch (error) {
|
|
358
426
|
// 富文本被拒就退回纯文本编辑;确认方法不可用后不再重试。
|
|
359
427
|
if (richUnsupported(error)) richDisabled = true
|
|
360
428
|
else if (!(error instanceof Error && error.message.includes('message is not modified'))) {
|
|
361
|
-
await api('editMessageText', { chat_id:
|
|
429
|
+
await api('editMessageText', { chat_id: route.chatId, message_id: first.message_id, text: next })
|
|
362
430
|
last = next
|
|
363
431
|
return
|
|
364
432
|
}
|
|
365
433
|
}
|
|
366
434
|
}
|
|
367
|
-
await api('editMessageText', { chat_id:
|
|
435
|
+
await api('editMessageText', { chat_id: route.chatId, message_id: first.message_id, text: next })
|
|
368
436
|
last = next
|
|
369
437
|
} catch (error) {
|
|
370
438
|
const detail = error instanceof Error ? error.message : String(error)
|
|
@@ -62,6 +62,17 @@ export function toolResultFailed(data: any): boolean {
|
|
|
62
62
|
return data?.message?.content?.[0]?.isError === true
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
+
/** 文件在投递前已被删除(例如同一回合写入后又被清理)不是投递失败:静默跳过,不打扰用户。 */
|
|
66
|
+
export function missingFileError(error: unknown): boolean {
|
|
67
|
+
const code = (error as { code?: unknown } | null)?.code
|
|
68
|
+
if (typeof code === 'string') {
|
|
69
|
+
const tail = code.slice(code.lastIndexOf('/') + 1)
|
|
70
|
+
if (tail === 'not-found') return true
|
|
71
|
+
}
|
|
72
|
+
const message = error instanceof Error ? error.message : ''
|
|
73
|
+
return message === 'file-not-found'
|
|
74
|
+
}
|
|
75
|
+
|
|
65
76
|
/** 复用 Chat 完整文件读取服务,宿主负责路径、文件类型和大小限制。 */
|
|
66
77
|
export class FileDelivery {
|
|
67
78
|
private readonly queue = new KeyedSerialQueue()
|
|
@@ -106,9 +117,11 @@ export class FileDelivery {
|
|
|
106
117
|
await latest.channel.sendFile!(latest.chatId, { name, data: Buffer.from(result.data, 'base64') }, signal)
|
|
107
118
|
sent.add(identity)
|
|
108
119
|
} catch (error) {
|
|
109
|
-
ok = false
|
|
110
120
|
if (this.lifetime.signal.aborted) { ok = false; return }
|
|
111
|
-
|
|
121
|
+
if (missingFileError(error)) continue
|
|
122
|
+
ok = false
|
|
123
|
+
const code = typeof (error as { code?: unknown })?.code === 'string' ? ` code=${(error as { code: string }).code}` : ''
|
|
124
|
+
this.log(`[${initial.channel.id}] 文件回传失败(${error instanceof Error ? error.name : 'Error'}${code};敏感详情已隐藏)`)
|
|
112
125
|
const latest = target()
|
|
113
126
|
if (!latest || latest.channel !== initial.channel || latest.chatId !== initial.chatId) { ok = false; return }
|
|
114
127
|
const text = withReplyLocale(this.host, () => replyText('文件“{0}”未能发送,请在网页 Chat 中打开该文件;也可让助手重新交付。', name))
|
package/src/engine/gateway.ts
CHANGED
|
@@ -29,7 +29,7 @@ import {
|
|
|
29
29
|
type UserQuestionAnswer,
|
|
30
30
|
type UserQuestionItem,
|
|
31
31
|
} from './question.js'
|
|
32
|
-
import type { ChannelAdapter, EngineConfig, ImMessage } from './types.js'
|
|
32
|
+
import type { ChannelAdapter, EngineConfig, ImMessage, SendMode } from './types.js'
|
|
33
33
|
|
|
34
34
|
/** 分片发送的退避表(毫秒):第一次立即,随后 1s/3s/8s/20s,累计约 32s。 */
|
|
35
35
|
const SEND_RETRY_DELAYS_MS = [0, 1_000, 3_000, 8_000, 20_000] as const
|
|
@@ -597,7 +597,9 @@ export class ImEngine {
|
|
|
597
597
|
let scope = this.inputScopes.get(channel.id)
|
|
598
598
|
if (!scope) this.inputScopes.set(channel.id, scope = new AbortController())
|
|
599
599
|
const { signal } = scope
|
|
600
|
-
|
|
600
|
+
// 账号级发送方式:steer 作为补充指令进入下一步边界,不再显示「排队中」。
|
|
601
|
+
const sendMode: SendMode = this.resolveConfig(channel.id).sendMode === 'steer' ? 'steer' : 'queue'
|
|
602
|
+
const items = sources.map(source => new MessageProgress(channel, source, this.ctx, this.log, sendMode === 'steer' ? 'processing' : 'queued'))
|
|
601
603
|
let accepted = false
|
|
602
604
|
let submitted = false
|
|
603
605
|
let requestId: string | undefined
|
|
@@ -631,11 +633,11 @@ export class ImEngine {
|
|
|
631
633
|
// Optional lookup preserves text-only operation on older Hosts. Keep
|
|
632
634
|
// strict lookup so a pending or unloading provider is never invoked.
|
|
633
635
|
const controller = (this.ctx as unknown as { get(name: string): unknown }).get('sessionController') as {
|
|
634
|
-
prompt(request: { requestId: string; sessionId: string; mode:
|
|
636
|
+
prompt(request: { requestId: string; sessionId: string; mode: SendMode; content: Array<Record<string, unknown>> }, signal: AbortSignal): Promise<unknown>
|
|
635
637
|
} | undefined
|
|
636
638
|
if (!controller?.prompt) throw new ImageInputError('当前 Host 不支持 Chat 图片输入,请升级 DeepSeek Harness。')
|
|
637
639
|
submitted = true
|
|
638
|
-
await controller.prompt({ requestId, sessionId: binding.sessionId, mode:
|
|
640
|
+
await controller.prompt({ requestId, sessionId: binding.sessionId, mode: sendMode, content }, signal)
|
|
639
641
|
} else {
|
|
640
642
|
submitted = true
|
|
641
643
|
this.router.followup(binding, {
|
|
@@ -643,7 +645,7 @@ export class ImEngine {
|
|
|
643
645
|
role: 'user',
|
|
644
646
|
content,
|
|
645
647
|
source: { kind: 'user', rpcId: requestId },
|
|
646
|
-
})
|
|
648
|
+
}, sendMode)
|
|
647
649
|
}
|
|
648
650
|
accepted = true
|
|
649
651
|
this.log(`[${channel.id}] 已注入 ${binding.sessionId}`)
|
package/src/engine/router.ts
CHANGED
|
@@ -3,7 +3,7 @@ import type { ChannelInstanceId, ChatKind, SessionRecord } from './session-id.js
|
|
|
3
3
|
import { createImSessionId, parseImSessionId, sessionKeyOf } from './session-id.js'
|
|
4
4
|
import { SessionMapStore } from './session-store.js'
|
|
5
5
|
import { readHostDefaultModel, resolveImAgentOptions } from './agent-options.js'
|
|
6
|
-
import type { EngineConfig } from './types.js'
|
|
6
|
+
import type { EngineConfig, SendMode } from './types.js'
|
|
7
7
|
import { KeyedSerialQueue } from './keyed-queue.js'
|
|
8
8
|
import { sameWorkspacePath } from './workspace-path.js'
|
|
9
9
|
import { replyText } from './command-locale.js'
|
|
@@ -361,10 +361,18 @@ export class SessionRouter {
|
|
|
361
361
|
return this.channelOperations.run(record.channel, () => this.removeFromChannel(sessionId, false))
|
|
362
362
|
}
|
|
363
363
|
|
|
364
|
-
|
|
364
|
+
/**
|
|
365
|
+
* 投递一条普通用户消息。`steer` 模式在宿主 Agent 提供 next-step 注入时作为补充指令
|
|
366
|
+
* 立即进入运行中的任务;旧宿主没有 `steer` 时保持排队投递,消息不丢失、语义可预期。
|
|
367
|
+
*/
|
|
368
|
+
followup(binding: ChatBinding, message: unknown, mode: SendMode = 'queue'): void {
|
|
365
369
|
const raw = binding.handle?.agent ?? this.ctx.agents?.get?.(binding.sessionId)
|
|
366
|
-
const agent = raw as { followup?: (message: unknown) => void } | undefined
|
|
370
|
+
const agent = raw as { followup?: (message: unknown) => void; steer?: (message: unknown) => void } | undefined
|
|
367
371
|
if (!agent?.followup) throw new Error(`会话 ${binding.sessionId} 当前没有运行中的 agent`)
|
|
372
|
+
if (mode === 'steer' && typeof agent.steer === 'function') {
|
|
373
|
+
agent.steer(message)
|
|
374
|
+
return
|
|
375
|
+
}
|
|
368
376
|
agent.followup(message)
|
|
369
377
|
}
|
|
370
378
|
|
package/src/engine/types.ts
CHANGED
|
@@ -67,6 +67,12 @@ export interface ChannelAdapter {
|
|
|
67
67
|
skipMerge?: boolean
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
+
/**
|
|
71
|
+
* 普通消息的默认发送方式。`queue` 排队等待当前任务结束;`steer` 作为补充指令
|
|
72
|
+
* 在下一步边界注入运行中的任务(宿主 Agent 的 next-step 收件箱)。不提供自动切换。
|
|
73
|
+
*/
|
|
74
|
+
export type SendMode = 'queue' | 'steer'
|
|
75
|
+
|
|
70
76
|
export interface EngineConfig {
|
|
71
77
|
cwd: string
|
|
72
78
|
provider: string
|
|
@@ -75,4 +81,5 @@ export interface EngineConfig {
|
|
|
75
81
|
mergeTimeoutSecs: number
|
|
76
82
|
permissionPreset: string
|
|
77
83
|
reasoningEffort?: string
|
|
84
|
+
sendMode?: SendMode
|
|
78
85
|
}
|
package/src/manager.ts
CHANGED
|
@@ -18,7 +18,7 @@ import { ImEngine } from './engine/gateway.js'
|
|
|
18
18
|
import { SeenStore } from './engine/seen-store.js'
|
|
19
19
|
import { clearWeixinLogin, persistWeixinLogin, readLegacyWeixinBotToken, readWeixinAllowedUserId } from './channels/weixin.js'
|
|
20
20
|
import { normalizeAssistantModel, normalizePermission, normalizeWorkspacePath, type AssistantModel, type PermissionPreset } from './engine/assistant-settings.js'
|
|
21
|
-
import type { ChannelAdapter, EngineConfig, ImMessage } from './engine/types.js'
|
|
21
|
+
import type { ChannelAdapter, EngineConfig, ImMessage, SendMode } from './engine/types.js'
|
|
22
22
|
import { KeyedSerialQueue } from './engine/keyed-queue.js'
|
|
23
23
|
import { backupCorruptFileSync, writeFileAtomicSync } from './engine/atomic-file.js'
|
|
24
24
|
import { sameWorkspacePath } from './engine/workspace-path.js'
|
|
@@ -27,6 +27,11 @@ export const API_CLIENT_HEADER = 'x-dsh-im-connect-client'
|
|
|
27
27
|
const MAX_API_BODY_BYTES = 1024 * 1024
|
|
28
28
|
const ACCOUNT_REMARK_LIMIT = 40
|
|
29
29
|
|
|
30
|
+
/** 消息发送方式只有排队与插话两档;缺失或未知值一律回到排队,保持旧配置语义。 */
|
|
31
|
+
function normalizeSendMode(input: unknown): SendMode {
|
|
32
|
+
return input === 'steer' ? 'steer' : 'queue'
|
|
33
|
+
}
|
|
34
|
+
|
|
30
35
|
function channelAutoPrefix(platform: ChannelId): string {
|
|
31
36
|
return `${CHANNEL_META[platform].label}账号`
|
|
32
37
|
}
|
|
@@ -145,6 +150,8 @@ export interface ChannelState {
|
|
|
145
150
|
permission?: PermissionPreset
|
|
146
151
|
commandPermissions?: CommandPermissions
|
|
147
152
|
privateAccess?: 'approved' | 'all'
|
|
153
|
+
/** 普通消息的默认发送方式;缺省为排队发送。 */
|
|
154
|
+
sendMode?: SendMode
|
|
148
155
|
lastCheckedAt?: string
|
|
149
156
|
}
|
|
150
157
|
|
|
@@ -166,6 +173,7 @@ export interface AccountView {
|
|
|
166
173
|
permission: PermissionPreset
|
|
167
174
|
commandPermissions: CommandPermissions
|
|
168
175
|
privateAccess: 'approved' | 'all'
|
|
176
|
+
sendMode: SendMode
|
|
169
177
|
lastCheckedAt?: string
|
|
170
178
|
}
|
|
171
179
|
|
|
@@ -340,6 +348,7 @@ export class ChannelManager {
|
|
|
340
348
|
agentPreset: state.agentPreset || this.engineConfig.agentPreset || 'standard',
|
|
341
349
|
commandPermissions: normalizeCommandPermissions(state.commandPermissions),
|
|
342
350
|
privateAccess: state.privateAccess === 'all' ? 'all' : 'approved',
|
|
351
|
+
sendMode: normalizeSendMode(state.sendMode),
|
|
343
352
|
lastCheckedAt: state.lastCheckedAt,
|
|
344
353
|
}
|
|
345
354
|
}
|
|
@@ -413,6 +422,7 @@ export class ChannelManager {
|
|
|
413
422
|
agentPreset: normalized.settings.agentPreset,
|
|
414
423
|
commandPermissions: normalized.settings.commandPermissions,
|
|
415
424
|
privateAccess: normalized.settings.privateAccess,
|
|
425
|
+
sendMode: normalized.settings.sendMode,
|
|
416
426
|
enabled: true,
|
|
417
427
|
receiveEnabled: true,
|
|
418
428
|
config: nextConfig,
|
|
@@ -808,6 +818,8 @@ export class ChannelManager {
|
|
|
808
818
|
state.permission = normalized.settings.permission
|
|
809
819
|
state.commandPermissions = normalized.settings.commandPermissions
|
|
810
820
|
state.privateAccess = normalized.settings.privateAccess
|
|
821
|
+
// 发送方式按注入时机读取,不需要重载会话。
|
|
822
|
+
state.sendMode = normalized.settings.sendMode
|
|
811
823
|
this.flush()
|
|
812
824
|
if (reloadSessions) await this.engine.reloadChannel(accountId, { resetSessions })
|
|
813
825
|
return { ok: true, account: this.accountView(accountId, state) }
|
|
@@ -1177,6 +1189,7 @@ export class ChannelManager {
|
|
|
1177
1189
|
state.cwd = normalizeWorkspacePath(state.cwd) ?? cwd
|
|
1178
1190
|
state.permission = normalizePermission(state.permission, this.permissionPresets().names) ?? permission
|
|
1179
1191
|
state.privateAccess = state.privateAccess === 'all' ? 'all' : 'approved'
|
|
1192
|
+
state.sendMode = normalizeSendMode(state.sendMode)
|
|
1180
1193
|
}
|
|
1181
1194
|
this.store.version = 2
|
|
1182
1195
|
}
|
|
@@ -1219,7 +1232,7 @@ export class ChannelManager {
|
|
|
1219
1232
|
return ordinal
|
|
1220
1233
|
}
|
|
1221
1234
|
|
|
1222
|
-
private async normalizeAccountSettings(platform: ChannelId, input: Record<string, unknown>, previous: ChannelState): Promise<{ ok: true; settings: { name: string; nameOrdinal: number; assistant: AssistantModel; agentPreset: string; cwd: string; permission: PermissionPreset; commandPermissions: CommandPermissions; privateAccess: 'approved' | 'all' } } | { ok: false; error: string }> {
|
|
1235
|
+
private async normalizeAccountSettings(platform: ChannelId, input: Record<string, unknown>, previous: ChannelState): Promise<{ ok: true; settings: { name: string; nameOrdinal: number; assistant: AssistantModel; agentPreset: string; cwd: string; permission: PermissionPreset; commandPermissions: CommandPermissions; privateAccess: 'approved' | 'all'; sendMode: SendMode } } | { ok: false; error: string }> {
|
|
1223
1236
|
const agentPreset = input.agentPreset ?? previous.agentPreset ?? this.engineConfig.agentPreset ?? 'standard'
|
|
1224
1237
|
if (typeof agentPreset !== 'string' || !agentPreset.trim()) return { ok: false, error: '请选择 Agent 预设' }
|
|
1225
1238
|
if (input.agentPreset !== undefined) {
|
|
@@ -1241,6 +1254,7 @@ export class ChannelManager {
|
|
|
1241
1254
|
const permission = normalizePermission(input.permission ?? previous.permission ?? this.currentPermission(), this.permissionPresets().names)
|
|
1242
1255
|
if (!permission) return { ok: false, error: '请选择权限' }
|
|
1243
1256
|
const privateAccess = input.privateAccess === 'all' || (input.privateAccess === undefined && previous.privateAccess === 'all') ? 'all' : 'approved'
|
|
1257
|
+
const sendMode = normalizeSendMode(input.sendMode === undefined ? previous.sendMode : input.sendMode)
|
|
1244
1258
|
let commandPermissions: CommandPermissions
|
|
1245
1259
|
try { commandPermissions = normalizeCommandPermissions(input.commandPermissions === undefined ? previous.commandPermissions : input.commandPermissions) }
|
|
1246
1260
|
catch { return { ok: false, error: '命令权限配置无效' } }
|
|
@@ -1250,7 +1264,7 @@ export class ChannelManager {
|
|
|
1250
1264
|
if ([...trimmed].length > ACCOUNT_REMARK_LIMIT) return { ok: false, error: `账号备注不能超过 ${ACCOUNT_REMARK_LIMIT} 个字符` }
|
|
1251
1265
|
if (/[\u0000-\u001F\u007F]/.test(trimmed)) return { ok: false, error: '账号备注不能包含换行或控制字符' }
|
|
1252
1266
|
const name = trimmed || autoAccountName(platform, nameOrdinal)
|
|
1253
|
-
return { ok: true, settings: { name, nameOrdinal, assistant, agentPreset, cwd, permission, privateAccess, commandPermissions } }
|
|
1267
|
+
return { ok: true, settings: { name, nameOrdinal, assistant, agentPreset, cwd, permission, privateAccess, sendMode, commandPermissions } }
|
|
1254
1268
|
}
|
|
1255
1269
|
|
|
1256
1270
|
private accountIdFor(platform: ChannelId, config: Record<string, string>): string {
|
|
@@ -1304,6 +1318,7 @@ export class ChannelManager {
|
|
|
1304
1318
|
model: assistant?.model ?? this.engineConfig.model,
|
|
1305
1319
|
reasoningEffort: assistant?.reasoningEffort,
|
|
1306
1320
|
permissionPreset: normalizePermission(state.permission, this.permissionPresets().names) ?? this.engineConfig.permissionPreset,
|
|
1321
|
+
sendMode: normalizeSendMode(state.sendMode),
|
|
1307
1322
|
}
|
|
1308
1323
|
}
|
|
1309
1324
|
|
|
@@ -1362,7 +1377,7 @@ function pairingSettings(input?: Record<string, unknown>): Record<string, string
|
|
|
1362
1377
|
if (!input) return {}
|
|
1363
1378
|
const out: Record<string, string> = {}
|
|
1364
1379
|
if (input.commandPermissions !== undefined) out.__setting_commandPermissions = JSON.stringify(normalizeCommandPermissions(input.commandPermissions))
|
|
1365
|
-
for (const key of ['name', 'provider', 'model', 'reasoningEffort', 'cwd', 'permission', 'privateAccess', 'agentPreset']) {
|
|
1380
|
+
for (const key of ['name', 'provider', 'model', 'reasoningEffort', 'cwd', 'permission', 'privateAccess', 'sendMode', 'agentPreset']) {
|
|
1366
1381
|
const value = input[key]
|
|
1367
1382
|
if (value !== undefined && value !== null) out[`__setting_${key}`] = String(value)
|
|
1368
1383
|
}
|