/** * `/login` `/logout` 命令解析(纯函数,与 `ui/advisorCommand.ts`/`ui/computerCommand.ts` * 同构,单独导出可测)。 * * 原来这套能力叫 `/team`(login/logout/status/models/usage/tools)。改名理由: * 登录本身就是登录,不登录自然用不了团队在线模型和协作,不需要在命令名里 * 反复强调"team"——多一个词只会让用户敲错、也让它看起来像一个跟本机模型 * 无关的独立子系统(其实完全相反:不登录时本机模型照常可用,`/login` 只影响 * "要不要多一路在线模型可选")。`/login`/`/logout` 与 Claude Code 的顶层命令 * 保持同名,降低用户的记忆负担。 * * 普通用户不需要也不应该每次输入服务器地址。服务器地址由发行配置解析, * 只有明确输入 http(s) 地址时才保留开发/自托管兼容入口。 */ export type LoginCommand = 'status' | 'help' | 'login' | 'models' | 'usage' | 'tools'; export type LoginCommandParseResult = | { matched: false } | { matched: true; command: 'status' | 'help' | 'models' | 'usage' } | { matched: true; command: 'login'; server?: string } /** value: '' 或 'list' = 查看当前设置;其余按逗号/空格拆分成类别列表。 */ | { matched: true; command: 'tools'; value: string }; export function parseLoginCommand(input: string): LoginCommandParseResult { const trimmed = input.trim(); // 裸 `/login` = 使用内置/已配置地址发起登录;纯查询使用 `/login status`。 if (trimmed === '/login') return { matched: true, command: 'login' }; const match = /^\/login\s+([\s\S]+)$/.exec(trimmed); if (!match) return { matched: false }; const rest = match[1]!.trim(); if (rest === 'status') return { matched: true, command: 'status' }; if (rest === 'help') return { matched: true, command: 'help' }; if (rest === 'models' || rest === 'refresh') return { matched: true, command: 'models' }; if (rest === 'usage') return { matched: true, command: 'usage' }; if (rest === 'tools' || rest.startsWith('tools ')) { return { matched: true, command: 'tools', value: rest.slice('tools'.length).trim() }; } // 仅保留显式 URL 作为开发/自托管兼容入口,避免把拼写错误发到网络。 if (/^https?:\/\//i.test(rest)) return { matched: true, command: 'login', server: rest }; return { matched: true, command: 'help' }; } /** `/logout` 精确匹配,不接受多余文字(与 `/advisor` 等硬拦截命令同规则)。 */ export function isLogoutCommand(input: string): boolean { return input.trim() === '/logout'; } /** `/login help` 的文案(纯数据,测试可断言它覆盖了全部子命令)。 */ export function loginHelpText(): string { return [ '[/login] 团队在线模型登录 —— 不登录不影响本机模型正常使用。', ' /login 使用已配置的服务端登录', ' /login status 只查看状态,不打开浏览器', ' /login models 列出可用在线模型', ' /login usage 查看订阅与用量', ' /login tools [list|workspace,web,…] 查看/设置当前 Session 的在线工具类别', ' /login help 显示本说明', ' /logout 登出并撤销本机设备授权', ].join('\n'); }