1
0

start-study-check.mjs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import * as fs from 'node:fs'
  2. import * as path from 'node:path'
  3. import * as os from 'node:os'
  4. import { fileURLToPath, pathToFileURL } from 'node:url'
  5. import { spawn } from 'node:child_process'
  6. const packageRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..')
  7. const evidence = path.resolve(process.argv[2])
  8. const cli = path.resolve(process.argv[3])
  9. const port = Number(process.argv[4] ?? 6096)
  10. const indexCheck = process.argv.includes('--index')
  11. if (!fs.existsSync(cli) || !Number.isInteger(port) || port < 1024) throw new Error('Supply the installed DSH CLI entry and an available port')
  12. const root = fs.mkdtempSync(path.join(os.tmpdir(), 'webnovel-browser-'))
  13. const profile = path.join(root, 'profiles', 'web')
  14. const workspaceArg = process.argv.indexOf('--workspace')
  15. if (workspaceArg >= 0 && !process.argv[workspaceArg + 1]) throw new Error('--workspace requires a path')
  16. const workspace = workspaceArg >= 0 ? path.resolve(process.argv[workspaceArg + 1]) : path.join(root, 'workspace')
  17. fs.mkdirSync(profile, { recursive: true }); fs.mkdirSync(workspace, { recursive: true }); fs.mkdirSync(evidence, { recursive: true })
  18. fs.writeFileSync(path.join(profile, 'package.json'), JSON.stringify({ name: 'webnovel-acceptance-profile', private: true, type: 'module', dsh: { profile: { bundles: ['@deepseek-ai/dsh-base', '@deepseek-ai/dsh-web-app'], patchReload: 'live' } } }, null, 2))
  19. fs.writeFileSync(path.join(profile, 'cordis.yml'), '[]\n')
  20. fs.writeFileSync(path.join(profile, 'cordis.patch.yml'), JSON.stringify([
  21. { id: 'agent-default-model', config: { provider: 'webnovel-acceptance', model: 'fixture' } },
  22. { insert: [{ id: 'webnovel', name: pathToFileURL(path.join(packageRoot, 'lib/index.js')).href },
  23. ...(indexCheck ? [{ id: 'embedding', name: pathToFileURL(path.join(packageRoot, '../embedding-provider/lib/index.js')).href }] : []),
  24. { id: 'webnovel-browser-acceptance', name: pathToFileURL(path.join(packageRoot, 'tests/fixtures/study-browser-host.mjs')).href }] },
  25. ], null, 2))
  26. const stdout = fs.openSync(path.join(root, 'stdout.log'), 'a')
  27. const stderr = fs.openSync(path.join(root, 'stderr.log'), 'a')
  28. const child = spawn(process.execPath, [cli, '--profile', 'web', '--host', '127.0.0.1', '--port', String(port), '--no-open'], {
  29. cwd: packageRoot, env: { ...process.env, DSH_HOME: root, ...(indexCheck ? { WEBNOVEL_INDEX_CHECK: '1' } : {}), WEBNOVEL_BROWSER_WORKSPACE: workspace, WEBNOVEL_BROWSER_REPORT: path.join(evidence, 'browser-host-report.json'), DSH_TELEMETRY_DISABLED: '1' },
  30. detached: true, windowsHide: true, stdio: ['ignore', stdout, stderr],
  31. })
  32. child.unref(); fs.closeSync(stdout); fs.closeSync(stderr)
  33. fs.writeFileSync(path.join(evidence, 'browser-runtime.json'), JSON.stringify({ root, workspace, port, processId: child.pid }, null, 2) + '\n')
  34. console.log(JSON.stringify({ processId: child.pid, port, root }))