#!/usr/bin/env node // 构建业务平台使用的 SDK 包(docs/integration.md):@agent-server/shared、@agent-server/client、@agent-server/chat-ui, // 以及技能推送工具 agent-skills(docs/design/19-skill-cli.md)。 // 输出 dist-sdk/<包名>-<版本>.tgz,部署到 app/sdk(保留旧版本),由服务的 npm 仓库 /npm/ 公开提供(不含任何密钥)。 // 包之间是对等依赖;业务平台用 @agent-server:registry=<服务器>/npm/ 安装,其余依赖仍从 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')) run('npx', ['tsup', '--silent'], pkgDir('skills-cli')) 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 }) } // 推送工具:单文件、不带依赖,版本独立 { const src = readPkg('skills-cli') const dir = path.join(stage, 'skills-cli') fs.mkdirSync(dir, { recursive: true }) fs.copyFileSync(path.join(pkgDir('skills-cli'), 'dist/agent-skills.mjs'), path.join(dir, 'agent-skills.mjs')) fs.chmodSync(path.join(dir, 'agent-skills.mjs'), 0o755) const manifest = { name: src.name, version: src.version, type: 'module', description: src.description, bin: { 'agent-skills': './agent-skills.mjs' }, engines: { node: '>=18' } } 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: src.version, file }) } fs.rmSync(stage, { recursive: true, force: true }) console.log(`SDK ${version}:${index.map(p => p.file).join('、')}`)