#!/usr/bin/env node // 构建业务平台使用的 SDK 包(docs/integration.md):@agent-server/shared、@agent-server/client、@agent-server/chat-ui。 // 输出 dist-sdk/<包名>-<版本>.tgz 与 dist-sdk/index.json,由服务在 /sdk/ 下公开提供(不含任何密钥)。 // 包之间是对等依赖:业务平台用 URL 同时安装三个包,pnpm 按包名把它们互相关联,不会去 npm 仓库查找。 import { execFileSync } from 'node:child_process' import fs from 'node:fs' import path from 'node:path' import { fileURLToPath } from 'node:url' const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..') const out = path.join(root, 'dist-sdk') const stage = path.join(out, 'stage') const run = (cmd, args, cwd) => execFileSync(cmd, args, { cwd, stdio: ['ignore', 'inherit', 'inherit'] }) const pkgDir = name => path.join(root, 'packages', name) const readPkg = name => JSON.parse(fs.readFileSync(path.join(pkgDir(name), 'package.json'), 'utf8')) fs.rmSync(out, { recursive: true, force: true }) fs.mkdirSync(stage, { recursive: true }) run('npx', ['tsup', '--silent'], pkgDir('shared')) run('npx', ['tsup', '--silent'], pkgDir('client')) run('npx', ['vite', 'build', '--logLevel', 'warn'], pkgDir('chat-ui')) run('npx', ['vue-tsc', '-p', 'tsconfig.build.json'], pkgDir('chat-ui')) const version = readPkg('client').version const peer = { '@agent-server/shared': version } const packages = [ { dir: 'shared', files: { 'dist/index.js': 'index.js', 'dist/index.d.ts': 'index.d.ts' }, manifest: { exports: { '.': { types: './index.d.ts', import: './index.js' } }, types: './index.d.ts' }, }, { dir: 'client', files: { 'dist/index.js': 'index.js', 'dist/index.d.ts': 'index.d.ts' }, manifest: { exports: { '.': { types: './index.d.ts', import: './index.js' } }, types: './index.d.ts', peerDependencies: peer }, }, { dir: 'chat-ui', files: { 'dist/index.js': 'index.js', 'dist/style.css': 'style.css', 'dist/types': 'types' }, manifest: { exports: { '.': { types: './types/index.d.ts', import: './index.js' }, './style.css': './style.css' }, types: './types/index.d.ts', sideEffects: ['*.css'], dependencies: { 'markdown-it': readPkg('chat-ui').dependencies['markdown-it'] }, peerDependencies: { ...peer, '@agent-server/client': version, vue: readPkg('chat-ui').peerDependencies.vue }, }, }, ] const index = [] for (const p of packages) { const src = readPkg(p.dir) const dir = path.join(stage, p.dir) for (const [from, to] of Object.entries(p.files)) fs.cpSync(path.join(pkgDir(p.dir), from), path.join(dir, to), { recursive: true }) const manifest = { name: src.name, version, type: 'module', description: `AgentServer SDK:${p.dir}`, ...p.manifest } fs.writeFileSync(path.join(dir, 'package.json'), JSON.stringify(manifest, null, 2)) const file = execFileSync('npm', ['pack', '--silent', '--pack-destination', out], { cwd: dir, encoding: 'utf8' }).trim().split('\n').pop() index.push({ name: src.name, version, file }) } fs.rmSync(stage, { recursive: true, force: true }) fs.writeFileSync(path.join(out, 'index.json'), JSON.stringify({ version, packages: index }, null, 2)) console.log(`SDK ${version}:${index.map(p => p.file).join('、')}`)