paths.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { existsSync } from 'node:fs'
  2. import { dirname, join, resolve } from 'node:path'
  3. import { fileURLToPath } from 'node:url'
  4. import { HttpError } from '../errors.js'
  5. /** Package root: works from src/server/hermes (tsx) and dist-server/server/hermes (build). */
  6. export const PACKAGE_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '../../..')
  7. export interface KernelPaths { root: string; python: string; worker: string; service: string }
  8. /** Source checkout, npm package, esbuild bundle (runtime/ beside the bundle) or an explicit root. */
  9. function kernelRoot(env: NodeJS.ProcessEnv): string {
  10. if (env.YAOYAO_KERNEL_ROOT) return env.YAOYAO_KERNEL_ROOT
  11. const candidates = [PACKAGE_ROOT, dirname(fileURLToPath(import.meta.url)), process.cwd()]
  12. return candidates.find(root => existsSync(join(root, 'runtime/worker.py'))) ?? PACKAGE_ROOT
  13. }
  14. export function kernelPaths(env: NodeJS.ProcessEnv = process.env): KernelPaths {
  15. const root = kernelRoot(env)
  16. const bundled = join(root, 'runtime/python/bin/python3')
  17. return {
  18. root,
  19. python: env.YAOYAO_KERNEL_PYTHON || (existsSync(bundled) ? bundled : join(env.YAOYAO_KERNEL_VENV || join(root, '.venv'), 'bin/python')),
  20. worker: env.YAOYAO_KERNEL_WORKER || join(root, 'runtime/worker.py'),
  21. service: env.YAOYAO_HERMES_SERVICE || join(root, 'runtime/hermes_service.py'),
  22. }
  23. }
  24. export function requireKernel(paths: KernelPaths, script: string): void {
  25. if (!existsSync(script) || !existsSync(paths.python))
  26. throw new HttpError(503, '内置 Hermes 内核尚未安装,请运行 yaoyao setup(源码目录使用 npm run kernel:setup)。', 'kernel_unavailable')
  27. }
  28. /** Isolated process environment: no ambient Hermes profile, only proxy/TLS settings pass through. */
  29. export function kernelEnv(home: string, source: NodeJS.ProcessEnv, systemHome?: string): NodeJS.ProcessEnv {
  30. const env: NodeJS.ProcessEnv = { PATH: source.PATH, HOME: home, HERMES_HOME: home, LANG: 'en_US.UTF-8', PYTHONUNBUFFERED: '1', PYTHONIOENCODING: 'utf-8', YAOVI_KERNEL: '1', HERMES_SKIP_UPDATE_CHECK: '1' }
  31. if (systemHome && systemHome !== home) env.YAOVI_HERMES_ROOT = systemHome
  32. for (const key of ['HTTP_PROXY', 'HTTPS_PROXY', 'ALL_PROXY', 'NO_PROXY', 'http_proxy', 'https_proxy', 'all_proxy', 'no_proxy', 'SSL_CERT_FILE', 'SSL_CERT_DIR', 'REQUESTS_CA_BUNDLE', 'TMPDIR'])
  33. if (source[key]) env[key] = source[key]
  34. for (const [key, value] of Object.entries(source)) if (key.startsWith('YAOVI_HERMES_TEST_')) env[key] = value
  35. return env
  36. }
  37. const SECRET = /\b(?:sk-[A-Za-z0-9_-]{16,}|gh[pousr]_[A-Za-z0-9]{20,}|xox[abpr]-[A-Za-z0-9-]{10,}|AKIA[0-9A-Z]{16}|eyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,})\b/g
  38. const ASSIGNMENT = /\b((?:api[_-]?key|token|secret|password|authorization)\s*[:=]\s*)(["']?)([^\s"']{6,})\2/gi
  39. export function scrubSecrets(text: string): string {
  40. return text.replace(SECRET, '«redacted»').replace(ASSIGNMENT, (_m, key: string, quote: string) => `${key}${quote}«redacted»${quote}`)
  41. }