| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- /**
- * patch.yml 生成
- * =============
- * 桌面端由 electron/patch.js 生成同一份文件(--patch 交给引擎)。Web 版不能直接
- * 复用那个模块:它读 Electron 的 store、依赖 i18n 与运行时探测,在服务端跑不起来。
- * 所以这里重写一份**只包含 Web 部署需要的那几行**的生成器,结构和取值对齐桌面端:
- *
- * 品牌 / 账号线 / 遥测 / 官方 provider 全部下线
- * 模型只留可可乐博网关(key 走环境变量,不落盘)
- * 人设换成教师端(文案从 persona.zh.txt 读,和桌面端保持一份)
- * 业务插件 cocorobo(apiBase / authFile / bridgeFile / workspaceDir)
- * 技能目录只认我们给的(includeDefaultRoots: false)
- */
- import fs from 'node:fs'
- import path from 'node:path'
- import { fileURLToPath } from 'node:url'
- const here = path.dirname(fileURLToPath(import.meta.url))
- /** JSON 字符串是合法 YAML,引号/冒号/中文都不用手工转义 */
- const y = (value) => JSON.stringify(String(value))
- /**
- * 引擎加载插件走 ESM 的 import(),**目录不被接受**(ERR_UNSUPPORTED_DIR_IMPORT)。
- * 桌面端的 plugins.json 里写的是入口文件路径,这里保持一致:
- * 给目录就自己找入口(index.mjs 优先 —— cocorobo 插件就是 .mjs)。
- */
- function entryOf (dirOrFile) {
- const value = String(dirOrFile || '')
- if (!value) return value
- if (/\.(m?js|cjs)$/i.test(value)) return value
- const dir = value.replace(/[\\/]+$/, '')
- for (const name of ['index.mjs', 'index.js', 'index.cjs']) {
- if (fs.existsSync(path.join(dir, name))) return path.join(dir, name)
- }
- try {
- const pkg = JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf8'))
- const main = typeof pkg.main === 'string' ? pkg.main : (pkg.exports && pkg.exports['.'])
- if (typeof main === 'string') return path.resolve(dir, main)
- } catch { /* 没有 package.json 或读不了 */ }
- return path.join(dir, 'index.js')
- }
- const DISABLED_ENTRIES = [
- 'deepseek-account',
- 'ui-settings-account',
- 'session-log-deepseek',
- 'ui-settings-session-log',
- 'session-telemetry-otel',
- 'desktop-product-telemetry',
- 'product-analytics',
- 'llm-deepseek',
- 'llm-deepseek-account'
- ]
- /** 模型可见但教师端用不到的工具:每个 schema 每轮都占上下文,去掉更省心 */
- const DISABLED_TOOLS = [
- 'mcp-resources',
- 'tool-plugin-manager',
- 'tool-subagent-control',
- 'tool-subagent-fork',
- 'tool-workflow',
- 'tool-goal',
- 'tool-agent-team',
- 'ui-agent-team',
- 'ui-account-notifications',
- 'harness-dev-context'
- ]
- const MEDIA_RE = /(^|[-_/])(image|img|video|tts|audio|music|embed|rerank)([-_/]|$)/i
- export function isMediaModel (model) {
- const id = String((model && model.id) || '')
- return MEDIA_RE.test(id)
- }
- function persona (locale) {
- const base = fs.readFileSync(path.join(here, '..', 'persona.zh.txt'), 'utf8')
- .split('\n').map((s) => s.trim()).filter(Boolean)
- if (locale === 'en') {
- base.push('The teacher is using the English interface. Always reply in English and produce documents, slides and messages in English, even though the skill notes you read may be written in Chinese — translate their structure, headings and template wording into natural English instead of copying the Chinese headings.')
- } else if (locale === 'zh-HK') {
- base.push('老師用的是繁體中文介面(香港用語)。一律用繁體中文回覆,產出的文件、簡報、訊息也用繁體中文;技能裡的簡體標題要轉成繁體,不要照抄簡體字。')
- }
- return base
- }
- /**
- * @param {object} input
- * @param {string} input.workspaceDir
- * @param {string} input.authFile
- * @param {string} input.bridgeFile
- * @param {string} input.apiBase 平台地址(按区)
- * @param {string} input.pluginEntry cocorobo 插件目录
- * @param {string} input.brandEntry 品牌插件目录
- * @param {string} input.skillsDir 技能根目录
- * @param {object} input.llm {baseURL, model, models, protocol, userId, orgId}
- * @param {string} input.locale zh-CN / zh-HK / en
- * @param {number} [input.waitMs]
- */
- export function buildPatch (input) {
- const lines = []
- const row = (id) => lines.push(`- id: ${id}`)
- const disabled = (id) => { row(id); lines.push(' disabled: true') }
- // 0) 首启的"预览版说明"会把底层产品名讲给老师听,直接标记为已确认
- row('ui-settings-general')
- lines.push(' config:')
- lines.push(` welcomeNoticeVersion: ${y('2026-11-20')}`)
- // 0.1) 默认工作目录:老师的产物必须落在自己的目录里
- row('working-directory')
- lines.push(' config:')
- lines.push(` defaultDirectory: ${y(input.workspaceDir)}`)
- // 1) 品牌:官方品牌包只在 official 构建里生效,换成我们自己的
- disabled('ui-brand-official')
- lines.push('- insert:')
- lines.push(' - id: ui-brand-cocorobo')
- lines.push(` name: ${y(entryOf(input.brandEntry))}`)
- // 2) 账号线:平台登录在 Web 服务这层做,引擎里不需要官方账号入口
- for (const id of DISABLED_ENTRIES) disabled(id)
- // 3) 模型:只留一个 provider,key 由进程环境变量注入
- const llm = input.llm || {}
- const models = (llm.models && llm.models.length) ? llm.models : [{ id: llm.model, name: llm.model }]
- const chat = models.filter((m) => !isMediaModel(m))
- const usable = chat.length ? chat : models.slice(0, 1)
- let defaultModel = llm.model || usable[0].id
- if (isMediaModel(defaultModel)) defaultModel = usable[0].id
- row('llm-pi-ai')
- lines.push(' config:')
- lines.push(' providers:')
- lines.push(' cocorobo:')
- lines.push(' displayName: CocoBuddy 模型服务')
- lines.push(' apiKeyEnv: CC_LLM_KEY')
- lines.push(` api: ${llm.protocol || 'openai-completions'}`)
- lines.push(` baseURL: ${y(llm.baseURL)}`)
- if (llm.userId || llm.orgId) {
- lines.push(' headers:')
- if (llm.userId) lines.push(` X-Cocorobo-User-Id: ${y(llm.userId)}`)
- if (llm.orgId) lines.push(` X-Cocorobo-Org-Id: ${y(llm.orgId)}`)
- }
- // 断网/断流重试:桌面端踩过的坑(SSE 断开但网关不报错)在这儿同样存在
- lines.push(' retryPolicy:')
- lines.push(' mode: normal')
- lines.push(' maxRetries: 30')
- lines.push(' retryableCodes: [TRANSPORT, STREAM_CLOSED, TIMEOUT, SERVER, RATE_LIMIT, EMPTY_RESPONSE, PI_AI_ERROR]')
- lines.push(' backoff:')
- lines.push(' initialDelayMs: 500')
- lines.push(' maxDelayMs: 5000')
- lines.push(' jitterRatio: 0.1')
- lines.push(' streamIdleTimeoutMs: 180000')
- lines.push(' models:')
- for (const m of usable) {
- lines.push(` - id: ${y(m.id)}`)
- if (m.name && m.name !== m.id) lines.push(` name: ${y(m.name)}`)
- lines.push(' input: [text, image]')
- }
- row('agent-default-model')
- lines.push(' config:')
- lines.push(' provider: cocorobo')
- lines.push(` model: ${y(defaultModel)}`)
- // 4) 人设
- row('system-prompt')
- lines.push(' config:')
- lines.push(' includeHarnessIdentity: false')
- lines.push(' personaPrefix: |')
- for (const sentence of persona(input.locale)) lines.push(` ${sentence}`)
- for (const id of DISABLED_TOOLS) disabled(id)
- lines.push('- id: repeat-tool-reminder')
- lines.push(' config:')
- lines.push(' thresholds: [2, 3, 5]')
- // 5) 业务插件
- lines.push('- insert:')
- lines.push(' - id: cocorobo')
- lines.push(` name: ${y(entryOf(input.pluginEntry))}`)
- lines.push(' config:')
- lines.push(` apiBase: ${y(input.apiBase)}`)
- lines.push(` authFile: ${y(input.authFile)}`)
- lines.push(` bridgeFile: ${y(input.bridgeFile)}`)
- lines.push(` llmBaseURL: ${y(llm.baseURL)}`)
- lines.push(` workspaceDir: ${y(input.workspaceDir)}`)
- lines.push(' mock: false')
- // 班级列表 / 课件状态 / 发布这些运维型能力,Web 版和桌面端一样可开
- lines.push(' extras: true')
- if (input.waitMs) lines.push(` waitMs: ${Number(input.waitMs)}`)
- // 6) 技能:只认我们给的目录(不吃用户目录里的野技能)
- row('skill-filesystem')
- lines.push(' disabled: false')
- lines.push(' config:')
- lines.push(' providerName: local')
- lines.push(' includeDefaultRoots: false')
- lines.push(' customSkillDirs:')
- for (const dir of input.skillDirs || [input.skillsDir]) lines.push(` - ${y(dir)}`)
- // 7) 右侧内嵌浏览器:web 档位默认关着,插件要用页签类型
- row('ui-sidebar-browser')
- lines.push(' disabled: false')
- return lines.join('\n') + '\n'
- }
|