@wenbin_wb/dsh-bridge 1.2.1 → 1.2.3
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/lib/wechat/node.js +47 -21
- package/package.json +1 -1
package/lib/wechat/node.js
CHANGED
|
@@ -449,20 +449,7 @@ export class WechatConversationNode {
|
|
|
449
449
|
let agent = this.activeAgent()
|
|
450
450
|
if (!agent && this.activeSessionId) {
|
|
451
451
|
// agent 不在内存(DSH 重启后或切换到持久化会话)→ re-attach。
|
|
452
|
-
// agents.create(sessionId) 对已持久化的 session 会自动采纳其历史,对不存在的则新建。
|
|
453
452
|
try {
|
|
454
|
-
// 读取持久化会话的 cwd,避免 cwd 冲突
|
|
455
|
-
let sessionCwd = this.config.cwd || process.cwd()
|
|
456
|
-
try {
|
|
457
|
-
const headers = await this.ctx.sessionPersistence?.list?.()
|
|
458
|
-
const header = headers?.find?.((h) => h?.id === this.activeSessionId)
|
|
459
|
-
if (header?.cwd) sessionCwd = header.cwd
|
|
460
|
-
} catch { /* 读取失败则用 fallback */ }
|
|
461
|
-
|
|
462
|
-
const meta = {
|
|
463
|
-
cwd: sessionCwd,
|
|
464
|
-
agentPreset: this.config.agentPreset || 'routing-suite',
|
|
465
|
-
}
|
|
466
453
|
const agentOptions = {}
|
|
467
454
|
if (this.config.agentProvider) agentOptions.provider = this.config.agentProvider
|
|
468
455
|
if (this.config.agentModel) agentOptions.model = this.config.agentModel
|
|
@@ -475,15 +462,40 @@ export class WechatConversationNode {
|
|
|
475
462
|
}
|
|
476
463
|
} catch { /* ignore */ }
|
|
477
464
|
}
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
465
|
+
|
|
466
|
+
// 判断该 session 是否已持久化:已持久化 → agents.resume(从持久化加载历史恢复,
|
|
467
|
+
// 避免 agents.create 用空 seed 与已持久化事件冲突);未持久化 → agents.create。
|
|
468
|
+
let persisted = false
|
|
469
|
+
try {
|
|
470
|
+
const headers = await this.ctx.sessionPersistence?.list?.()
|
|
471
|
+
persisted = Array.isArray(headers) && headers.some((h) => h?.id === this.activeSessionId)
|
|
472
|
+
} catch { /* 读取失败则按未持久化处理 */ }
|
|
473
|
+
|
|
474
|
+
let handle
|
|
475
|
+
if (persisted) {
|
|
476
|
+
handle = await this.ctx.agents.resume({
|
|
477
|
+
resumeSessionId: this.activeSessionId,
|
|
478
|
+
agentOptions,
|
|
479
|
+
})
|
|
480
|
+
} else {
|
|
481
|
+
// 读取持久化会话的 cwd 做 fallback(新建会话时用)
|
|
482
|
+
let sessionCwd = this.config.cwd || process.cwd()
|
|
483
|
+
const meta = {
|
|
484
|
+
cwd: sessionCwd,
|
|
485
|
+
agentPreset: this.config.agentPreset || 'routing-suite',
|
|
486
|
+
}
|
|
487
|
+
handle = await this.ctx.agents.create({
|
|
488
|
+
sessionId: this.activeSessionId,
|
|
489
|
+
meta,
|
|
490
|
+
agentOptions,
|
|
491
|
+
})
|
|
492
|
+
}
|
|
483
493
|
agent = handle.agent
|
|
484
|
-
this.logger?.info?.(`[dsh-bridge wechat] re-attached agent to session ${this.activeSessionId}`)
|
|
494
|
+
this.logger?.info?.(`[dsh-bridge wechat] re-attached agent to session ${this.activeSessionId} (${persisted ? 'resume' : 'create'})`)
|
|
485
495
|
} catch (err) {
|
|
486
|
-
|
|
496
|
+
const reason = err instanceof Error ? err.message : String(err)
|
|
497
|
+
this.logger?.warn?.(`[dsh-bridge wechat] failed to re-attach agent: ${reason}`)
|
|
498
|
+
await sendTextToPeer(this, `${MARK.err} 恢复会话失败: ${reason}。发送 /new <提示词> 新建一个会话。`)
|
|
487
499
|
}
|
|
488
500
|
}
|
|
489
501
|
if (!agent) {
|
|
@@ -886,7 +898,7 @@ async function routeCommand(node, text) {
|
|
|
886
898
|
return true
|
|
887
899
|
case 'use': {
|
|
888
900
|
const index = Number(rest[0])
|
|
889
|
-
const sessions = await listSessions(node)
|
|
901
|
+
const sessions = sessionsInDisplayOrder(await listSessions(node))
|
|
890
902
|
if (!Number.isInteger(index) || index < 1 || index > sessions.length) {
|
|
891
903
|
await sendTextToPeer(node, `${MARK.err} 无效编号。可用: 1–${sessions.length}(/sessions 查看列表)`)
|
|
892
904
|
return true
|
|
@@ -1005,6 +1017,19 @@ async function renderSessions(node) {
|
|
|
1005
1017
|
return `${MARK.list}\n${parts.join('\n')}`
|
|
1006
1018
|
}
|
|
1007
1019
|
|
|
1020
|
+
// 与 renderSessions 完全一致的显示顺序:按工作区字母序分组、组内保持 listSessions 顺序。
|
|
1021
|
+
// /use N 用这个数组索引,保证显示的编号 N 与切换的会话一一对应。
|
|
1022
|
+
function sessionsInDisplayOrder(all) {
|
|
1023
|
+
const groups = new Map()
|
|
1024
|
+
for (const s of all) {
|
|
1025
|
+
const key = s.cwd || '(未指定)'
|
|
1026
|
+
if (!groups.has(key)) groups.set(key, [])
|
|
1027
|
+
groups.get(key).push(s)
|
|
1028
|
+
}
|
|
1029
|
+
const sortedGroups = [...groups.entries()].sort((a, b) => String(a[0]).localeCompare(String(b[0])))
|
|
1030
|
+
return sortedGroups.flatMap(([, sessions]) => sessions)
|
|
1031
|
+
}
|
|
1032
|
+
|
|
1008
1033
|
// 时间戳 → 可读时间
|
|
1009
1034
|
function fmtTime(ms) {
|
|
1010
1035
|
try {
|
|
@@ -1044,6 +1069,7 @@ export const wechatNodeHelpers = {
|
|
|
1044
1069
|
extractText,
|
|
1045
1070
|
isGroupMessage,
|
|
1046
1071
|
listSessions,
|
|
1072
|
+
sessionsInDisplayOrder,
|
|
1047
1073
|
}
|
|
1048
1074
|
|
|
1049
1075
|
function sleep(ms) {
|
package/package.json
CHANGED