build-sdk.mjs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/env node
  2. // 构建业务平台使用的 SDK 包(docs/integration.md):@agent-server/shared、@agent-server/client、@agent-server/chat-ui。
  3. // 输出 dist-sdk/<包名>-<版本>.tgz 与 dist-sdk/index.json,由服务在 /sdk/ 下公开提供(不含任何密钥)。
  4. // 包之间是对等依赖:业务平台用 URL 同时安装三个包,pnpm 按包名把它们互相关联,不会去 npm 仓库查找。
  5. import { execFileSync } from 'node:child_process'
  6. import fs from 'node:fs'
  7. import path from 'node:path'
  8. import { fileURLToPath } from 'node:url'
  9. const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..')
  10. const out = path.join(root, 'dist-sdk')
  11. const stage = path.join(out, 'stage')
  12. const run = (cmd, args, cwd) => execFileSync(cmd, args, { cwd, stdio: ['ignore', 'inherit', 'inherit'] })
  13. const pkgDir = name => path.join(root, 'packages', name)
  14. const readPkg = name => JSON.parse(fs.readFileSync(path.join(pkgDir(name), 'package.json'), 'utf8'))
  15. fs.rmSync(out, { recursive: true, force: true })
  16. fs.mkdirSync(stage, { recursive: true })
  17. run('npx', ['tsup', '--silent'], pkgDir('shared'))
  18. run('npx', ['tsup', '--silent'], pkgDir('client'))
  19. run('npx', ['vite', 'build', '--logLevel', 'warn'], pkgDir('chat-ui'))
  20. run('npx', ['vue-tsc', '-p', 'tsconfig.build.json'], pkgDir('chat-ui'))
  21. const version = readPkg('client').version
  22. const peer = { '@agent-server/shared': version }
  23. const packages = [
  24. {
  25. dir: 'shared', files: { 'dist/index.js': 'index.js', 'dist/index.d.ts': 'index.d.ts' },
  26. manifest: { exports: { '.': { types: './index.d.ts', import: './index.js' } }, types: './index.d.ts' },
  27. },
  28. {
  29. dir: 'client', files: { 'dist/index.js': 'index.js', 'dist/index.d.ts': 'index.d.ts' },
  30. manifest: { exports: { '.': { types: './index.d.ts', import: './index.js' } }, types: './index.d.ts', peerDependencies: peer },
  31. },
  32. {
  33. dir: 'chat-ui', files: { 'dist/index.js': 'index.js', 'dist/style.css': 'style.css', 'dist/types': 'types' },
  34. manifest: {
  35. exports: { '.': { types: './types/index.d.ts', import: './index.js' }, './style.css': './style.css' },
  36. types: './types/index.d.ts',
  37. sideEffects: ['*.css'],
  38. dependencies: { 'markdown-it': readPkg('chat-ui').dependencies['markdown-it'] },
  39. peerDependencies: { ...peer, '@agent-server/client': version, vue: readPkg('chat-ui').peerDependencies.vue },
  40. },
  41. },
  42. ]
  43. const index = []
  44. for (const p of packages) {
  45. const src = readPkg(p.dir)
  46. const dir = path.join(stage, p.dir)
  47. for (const [from, to] of Object.entries(p.files)) fs.cpSync(path.join(pkgDir(p.dir), from), path.join(dir, to), { recursive: true })
  48. const manifest = { name: src.name, version, type: 'module', description: `AgentServer SDK:${p.dir}`, ...p.manifest }
  49. fs.writeFileSync(path.join(dir, 'package.json'), JSON.stringify(manifest, null, 2))
  50. const file = execFileSync('npm', ['pack', '--silent', '--pack-destination', out], { cwd: dir, encoding: 'utf8' }).trim().split('\n').pop()
  51. index.push({ name: src.name, version, file })
  52. }
  53. fs.rmSync(stage, { recursive: true, force: true })
  54. fs.writeFileSync(path.join(out, 'index.json'), JSON.stringify({ version, packages: index }, null, 2))
  55. console.log(`SDK ${version}:${index.map(p => p.file).join('、')}`)