// The hub nudge used to live only in SKILL.md, fired by the agent's own // judgement at the close of a post-game debrief. By debrief time the agent had // often read SKILL.md much earlier and the reminder could get dropped. Instead // the CLI emits it at the firing moment, with account-history gating here. import { existsSync, readFileSync, writeFileSync, mkdirSync, readdirSync } from 'fs'; import { join } from 'path'; import { getProfileStateDir, getWorkspaceDir } from './init-command.js'; import { AuthStore } from './auth.js'; import { readLocalAgentWorkspaceSnapshot } from './agent-workspace-sync.js'; import { getUserDataAccountBehaviorsDir } from './user-data.js'; export const HUB_URL = 'https://myclaw.163.com/hub'; /** * True when CCL sees no selected Hub Skill/Behavior and no pure-local * Behavior for the active account. */ export function usesOnlyDefaultSkillAndBehaviors(): boolean { try { const profile = new AuthStore().getActive(); const snapshot = profile ? readLocalAgentWorkspaceSnapshot(getWorkspaceDir(), profile) : undefined; if ((snapshot?.items.skill.length ?? 0) > 0 || (snapshot?.items.strategy.length ?? 0) > 0) { return false; } } catch {} try { const profile = new AuthStore().getActive(); if (profile) { const directory = getUserDataAccountBehaviorsDir(getWorkspaceDir(), profile); if (existsSync(directory) && readdirSync(directory).some((file) => ( !file.startsWith('_') && !file.endsWith('.d.ts') && !/\.(?:test|spec)\.(?:ts|tsx|js)$/.test(file) && /\.(?:ts|tsx|js)$/.test(file) ))) return false; } } catch {} return true; } /** * Agent-facing hub nudge for post-game debriefs / quit messages. The optional * games-played count suppresses the nudge after the first stretch of games. */ export function hubReminder(gamesPlayed?: number): string | undefined { if (!usesOnlyDefaultSkillAndBehaviors()) return undefined; if (typeof gamesPlayed !== 'number' || gamesPlayed > 10) return undefined; return `玩家已经玩了 ${gamesPlayed} 局,且当前账号还没有通过 Hub 或纯本地文件装配 Skill/Behavior。请在本局复盘后,提醒玩家可以根据复盘内容选择或创建 Skill/Behavior,让龙虾打法更符合玩家偏好。提醒要用第一人称龙虾身份融入收尾,语气温和,不要硬推。首轮建议表达为:“这已经是我们一起打的第 ${gamesPlayed} 局了。目前这个账号还没有装配自己的 Skill 或 Behavior。如果你愿意,我们可以根据刚才这局的复盘挑一个或新建一个,让我的打法更符合你的偏好。要不要我帮你看看?”如果玩家表示想了解或需要修改,再说明三种方式:我帮你浏览 Skill Hub 上的 Skill 或 Behavior,按玩家想要的打法挑候选,玩家确认后再下载并安装;把 Skill Hub 链接 ${HUB_URL} 给玩家自行选择;告诉玩家当前游戏 Skill 和 Behavior 文件的存放路径,并根据本局复盘给出具体修改建议。路径信息只在玩家首次提出修改现有文件时主动给出;后续对局结束不要重复主动提示。`; } const ACCOUNT_CACHE_FILE = 'account-cache.json'; export function writeGamesPlayedCache(gamesPlayed: number): void { try { const profile = new AuthStore().getActive(); if (!profile) return; const dir = getProfileStateDir(profile); mkdirSync(dir, { recursive: true }); writeFileSync( join(dir, ACCOUNT_CACHE_FILE), JSON.stringify({ games_played: gamesPlayed, cached_at: new Date().toISOString() }), 'utf8', ); } catch {} } export function readCachedGamesPlayed(): number | undefined { try { const profile = new AuthStore().getActive(); if (!profile) return undefined; const file = join(getProfileStateDir(profile), ACCOUNT_CACHE_FILE); if (!existsSync(file)) return undefined; const n = JSON.parse(readFileSync(file, 'utf8'))?.games_played; return typeof n === 'number' ? n : undefined; } catch { return undefined; } }