feat(goal): TG /goal 指令进入目标模式

- 复用宿主命令运行时(commands.execute,与 Web 端 /goal 同通道),不依赖
  GUI 连接:command-goal 在宿主侧操作 session-log 支撑的 goals 域
- 语法与 Web 完全一致:/goal <目标>、/goal(查看)、clear/edit/pause/resume
- 目标状态变更(blocked/complete)实时推送 TG 通知,手机可感知目标模式进展
- 配置侧启用 command-goal(plugin_toggle,已 live 生效)
This commit is contained in:
root
2026-09-17 12:05:26 +08:00
parent 85a42f28a6
commit 36e95c2631
6 changed files with 130 additions and 0 deletions
+56
View File
@@ -194,6 +194,7 @@ export class TelegramBridge {
{ command: 'model', description: '切换当前绑定会话的模型' },
{ command: 'status', description: '会话状态:模型/上下文/统计(首token/速率/tokens)' },
{ command: 'compact', description: '压缩当前会话历史(缩短上下文,空闲时执行)' },
{ command: 'goal', description: '目标模式:/goal <目标>(clear/edit/pause/resume 管理)' },
{ command: 'rich', description: '渲染:on 富文本 / off HTML 兼容(本聊天,需新版客户端)' },
{ command: 'unbind', description: '断开手机绑定(不关闭本机会话)' },
{ command: 'stop', description: '中止当前正在运行的任务' },
@@ -309,6 +310,9 @@ export class TelegramBridge {
case 'compact':
await this.requestCompact(chatId);
return;
case 'goal':
await this.requestGoal(chatId, parsed.arg);
return;
case 'rich':
await this.handleRichCommand(chatId, parsed.arg);
return;
@@ -895,6 +899,41 @@ export class TelegramBridge {
this.ctx.logger.error(`dsh-telegram-channel: /compact runner crashed: ${this.redact(err)}`);
});
}
/**
* TG /goal —— 走宿主命令运行时(commands.execute),与 Web 端 /goal 完全同一条
* 通道:command-goal 在宿主侧操作 session-log 支撑的 goals 域,不依赖 GUI 连接。
*/
async requestGoal(chatId, arg) {
const binding = this.bindings.get(String(chatId));
if (!binding) {
await this.client.sendMessage(chatId, MSG.NEED_BIND);
return;
}
const agent = await this.ensureLiveAgent(binding.sessionId);
if (!agent) {
this.bindings.delete(String(chatId));
await this.client.sendMessage(chatId, MSG.GONE);
return;
}
const commands = this.serviceOf('commands');
if (!commands?.execute || typeof commands.find !== 'function' || !commands.find(agent, 'goal')) {
await this.client.sendMessage(chatId, '当前环境不支持 /goal(command-goal 未装配)');
return;
}
const line = `/goal${arg ? ` ${arg}` : ''}`;
try {
const execution = await commands.execute(agent, line, [], new AbortController().signal);
if (execution === undefined) {
await this.client.sendMessage(chatId, '/goal 未执行:命令在运行时已注销');
return;
}
const text = execution.result?.text ?? (execution.result?.kind === 'success' ? '完成' : '失败');
await this.client.sendMessage(chatId, text.slice(0, 3500));
}
catch (err) {
await this.client.sendMessage(chatId, `/goal 执行失败:${this.redact(err).slice(0, 200)}`);
}
}
/** 等待压缩落定并汇报结果(不阻塞轮询循环)。 */
async runCompaction(chatId, sessionId, operation, controller) {
try {
@@ -1204,6 +1243,23 @@ export class TelegramBridge {
}
return;
}
// 'goal/changed' 不在本插件的 SessionEvent 类型联合内,宽松断言读取。
const eventType = event.type;
if (eventType === 'goal/changed') {
// Goal mode lives on the phone too: surface phase transitions (blocked/complete
// especially) without waiting for the next assistant text.
const data = event.data;
const phase = data?.phase;
if (phase === 'blocked' || phase === 'complete') {
const reason = data?.blockedReason;
const label = phase === 'blocked'
? `目标被阻塞${reason ? `:${reason.code ?? ''} ${reason.message ?? ''}`.slice(0, 120) : ''}`
: '目标已达成 ✓';
for (const b of targets)
this.enqueueNotice(b.chatId, `> ${label}`);
}
return;
}
if (event.type === 'todo/write') {
const todos = Array.isArray(event.data?.todos) ? event.data.todos : [];
this.lastTodos.set(id, todos);