@zhin.js/scaffold-wizard 0.1.5 → 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/adapter.d.ts.map +1 -1
- package/lib/adapter.js +2 -1
- package/lib/adapter.js.map +1 -1
- package/lib/ai.d.ts +6 -0
- package/lib/ai.d.ts.map +1 -1
- package/lib/ai.js +64 -34
- package/lib/ai.js.map +1 -1
- package/lib/apply.js +2 -2
- package/lib/apply.js.map +1 -1
- package/lib/index.d.ts +10 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +5 -1
- package/lib/index.js.map +1 -1
- package/lib/optional-peers.d.ts +21 -0
- package/lib/optional-peers.d.ts.map +1 -0
- package/lib/optional-peers.js +105 -0
- package/lib/optional-peers.js.map +1 -0
- package/lib/project-config-plan.d.ts +53 -0
- package/lib/project-config-plan.d.ts.map +1 -0
- package/lib/project-config-plan.js +337 -0
- package/lib/project-config-plan.js.map +1 -0
- package/lib/project-deps.d.ts +26 -3
- package/lib/project-deps.d.ts.map +1 -1
- package/lib/project-deps.js +211 -26
- package/lib/project-deps.js.map +1 -1
- package/lib/upgrade-l4.d.ts +15 -0
- package/lib/upgrade-l4.d.ts.map +1 -0
- package/lib/upgrade-l4.js +68 -0
- package/lib/upgrade-l4.js.map +1 -0
- package/lib/zhin-stack-deps.d.ts +66 -0
- package/lib/zhin-stack-deps.d.ts.map +1 -0
- package/lib/zhin-stack-deps.js +202 -0
- package/lib/zhin-stack-deps.js.map +1 -0
- package/package.json +7 -5
- package/src/adapter.ts +2 -1
- package/src/ai.ts +76 -32
- package/src/apply.ts +2 -2
- package/src/index.ts +53 -0
- package/src/optional-peers.ts +149 -0
- package/src/project-config-plan.ts +400 -0
- package/src/project-deps.ts +228 -27
- package/src/stack-versions.generated.json +48 -0
- package/src/upgrade-l4.ts +103 -0
- package/src/zhin-stack-deps.ts +252 -0
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import { createRequire } from 'node:module';
|
|
2
|
+
import fs from 'fs-extra';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { DATABASE_PACKAGES } from './types.js';
|
|
5
|
+
import type { PackageJsonLike } from './project-deps.js';
|
|
6
|
+
import {
|
|
7
|
+
findMissingPackageDependencies,
|
|
8
|
+
findUnresolvedPackageInstalls,
|
|
9
|
+
isAiEnabledInConfig,
|
|
10
|
+
} from './project-deps.js';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* User project dependency policy: scaffolded dependencies intentionally float to latest.
|
|
14
|
+
*/
|
|
15
|
+
export const ZHIN_STACK_VERSIONS = {
|
|
16
|
+
'zhin.js': 'latest',
|
|
17
|
+
'@zhin.js/cli': 'latest',
|
|
18
|
+
'@zhin.js/agent': 'latest',
|
|
19
|
+
'@zhin.js/database': 'latest',
|
|
20
|
+
'@zhin.js/host-router': 'latest',
|
|
21
|
+
'@zhin.js/host-api': 'latest',
|
|
22
|
+
'@zhin.js/client': 'latest',
|
|
23
|
+
'@zhin.js/contract': 'latest',
|
|
24
|
+
'@zhin.js/satori': 'latest',
|
|
25
|
+
'@zhin.js/speech': 'latest',
|
|
26
|
+
'@zhin.js/html-renderer': 'latest',
|
|
27
|
+
'@zhin.js/mcp': 'latest',
|
|
28
|
+
'@zhin.js/adapter-sandbox': 'latest',
|
|
29
|
+
'@zhin.js/adapter-dingtalk': 'latest',
|
|
30
|
+
'@zhin.js/adapter-discord': 'latest',
|
|
31
|
+
'@zhin.js/adapter-email': 'latest',
|
|
32
|
+
'@zhin.js/adapter-github': 'latest',
|
|
33
|
+
'@zhin.js/adapter-icqq': 'latest',
|
|
34
|
+
'@zhin.js/adapter-kook': 'latest',
|
|
35
|
+
'@zhin.js/adapter-lark': 'latest',
|
|
36
|
+
'@zhin.js/adapter-line': 'latest',
|
|
37
|
+
'@zhin.js/adapter-milky': 'latest',
|
|
38
|
+
'@zhin.js/adapter-napcat': 'latest',
|
|
39
|
+
'@zhin.js/adapter-onebot11': 'latest',
|
|
40
|
+
'@zhin.js/adapter-onebot12': 'latest',
|
|
41
|
+
'@zhin.js/adapter-qq': 'latest',
|
|
42
|
+
'@zhin.js/adapter-satori': 'latest',
|
|
43
|
+
'@zhin.js/adapter-slack': 'latest',
|
|
44
|
+
'@zhin.js/adapter-telegram': 'latest',
|
|
45
|
+
'@zhin.js/adapter-wechat-mp': 'latest',
|
|
46
|
+
'@zhin.js/adapter-wecom': 'latest',
|
|
47
|
+
'@zhin.js/adapter-weixin-ilink': 'latest',
|
|
48
|
+
} as const;
|
|
49
|
+
|
|
50
|
+
export type ZhinStackPackage = keyof typeof ZHIN_STACK_VERSIONS;
|
|
51
|
+
|
|
52
|
+
const HOST_CONSOLE_PLUGINS = new Set([
|
|
53
|
+
'@zhin.js/host-router',
|
|
54
|
+
'@zhin.js/host-api',
|
|
55
|
+
]);
|
|
56
|
+
|
|
57
|
+
function parseMajor(versionSpec: string | undefined): number | undefined {
|
|
58
|
+
if (!versionSpec?.trim()) return undefined;
|
|
59
|
+
if (versionSpec === 'workspace:*') return undefined;
|
|
60
|
+
const match = versionSpec.trim().replace(/^[\^~>=<]+/, '').match(/^(\d+)/);
|
|
61
|
+
return match ? Number(match[1]) : undefined;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function getDeclaredPackageVersion(pkg: PackageJsonLike, name: string): string | undefined {
|
|
65
|
+
return pkg.dependencies?.[name] ?? pkg.devDependencies?.[name];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function readInstalledMajor(cwd: string, packageName: string): number | undefined {
|
|
69
|
+
const pkgJson = path.join(cwd, 'package.json');
|
|
70
|
+
if (!fs.existsSync(pkgJson)) return undefined;
|
|
71
|
+
try {
|
|
72
|
+
const req = createRequire(pkgJson);
|
|
73
|
+
const installed = req(`${packageName}/package.json`) as { version?: string };
|
|
74
|
+
return parseMajor(installed.version);
|
|
75
|
+
} catch {
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function resolvePackageMajor(cwd: string, pkg: PackageJsonLike, packageName: string): number | undefined {
|
|
81
|
+
return readInstalledMajor(cwd, packageName) ?? parseMajor(getDeclaredPackageVersion(pkg, packageName));
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function resolveStackVersion(packageName: string): string {
|
|
85
|
+
return ZHIN_STACK_VERSIONS[packageName as ZhinStackPackage] ?? 'latest';
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function collectZhinPluginsFromConfig(config: Record<string, unknown>): string[] {
|
|
89
|
+
const plugins = Array.isArray(config.plugins)
|
|
90
|
+
? config.plugins.filter((p): p is string => typeof p === 'string')
|
|
91
|
+
: [];
|
|
92
|
+
return [...new Set(plugins.filter((p) => p.startsWith('@zhin.js/')))];
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** create-zhin / minimal-bot 默认 Host 端口(避免与常见 8086 占用冲突) */
|
|
96
|
+
export const DEFAULT_CREATE_BOT_HTTP_PORT = 8068;
|
|
97
|
+
|
|
98
|
+
/** create-zhin 生成项目的 .npmrc(避免全局 strict-peer-dependencies 阻断 AI 栈安装) */
|
|
99
|
+
export const CREATE_BOT_NPMRC = 'strict-peer-dependencies=false\n';
|
|
100
|
+
|
|
101
|
+
/** create-zhin 生成项目的 pnpm 配置 */
|
|
102
|
+
export function getCreateBotPnpmConfig(_aiEnabled?: boolean): Record<string, unknown> {
|
|
103
|
+
return {
|
|
104
|
+
onlyBuiltDependencies: ['esbuild'],
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** create-zhin-app 默认 bot 骨架依赖(不含适配器 / AI / 数据库驱动) */
|
|
109
|
+
export function getCreateBotBaseDependencies(): Record<string, string> {
|
|
110
|
+
return {
|
|
111
|
+
'zhin.js': ZHIN_STACK_VERSIONS['zhin.js'],
|
|
112
|
+
'@zhin.js/cli': ZHIN_STACK_VERSIONS['@zhin.js/cli'],
|
|
113
|
+
'@zhin.js/host-router': ZHIN_STACK_VERSIONS['@zhin.js/host-router'],
|
|
114
|
+
'@zhin.js/client': ZHIN_STACK_VERSIONS['@zhin.js/client'],
|
|
115
|
+
'@zhin.js/host-api': ZHIN_STACK_VERSIONS['@zhin.js/host-api'],
|
|
116
|
+
'@zhin.js/contract': ZHIN_STACK_VERSIONS['@zhin.js/contract'],
|
|
117
|
+
'@zhin.js/satori': ZHIN_STACK_VERSIONS['@zhin.js/satori'],
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** 根据 zhin.config 推导应声明的 @zhin.js/* / zhin.js 依赖 */
|
|
122
|
+
export function getRequiredZhinDependenciesForConfig(config: Record<string, unknown>): Record<string, string> {
|
|
123
|
+
const deps: Record<string, string> = {
|
|
124
|
+
'zhin.js': ZHIN_STACK_VERSIONS['zhin.js'],
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
for (const plugin of collectZhinPluginsFromConfig(config)) {
|
|
128
|
+
deps[plugin] = resolveStackVersion(plugin);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const database = config.database;
|
|
132
|
+
if (database && typeof database === 'object' && !Array.isArray(database)) {
|
|
133
|
+
deps['@zhin.js/database'] = ZHIN_STACK_VERSIONS['@zhin.js/database'];
|
|
134
|
+
const dialect = String((database as Record<string, unknown>).dialect ?? '');
|
|
135
|
+
const driver = DATABASE_PACKAGES[dialect as keyof typeof DATABASE_PACKAGES];
|
|
136
|
+
if (driver) {
|
|
137
|
+
deps[driver] = 'latest';
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const usesHost = collectZhinPluginsFromConfig(config).some((p) => HOST_CONSOLE_PLUGINS.has(p));
|
|
142
|
+
if (usesHost) {
|
|
143
|
+
for (const pkg of ['@zhin.js/host-router', '@zhin.js/host-api'] as const) {
|
|
144
|
+
deps[pkg] = ZHIN_STACK_VERSIONS[pkg];
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return deps;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export interface ZhinStackIncompatibility {
|
|
152
|
+
package: string;
|
|
153
|
+
reason: string;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export function findOutdatedZhinStackInPackageJson(
|
|
157
|
+
pkg: PackageJsonLike,
|
|
158
|
+
required: Record<string, string>,
|
|
159
|
+
): string[] {
|
|
160
|
+
const outdated: string[] = [];
|
|
161
|
+
for (const [name, minSpec] of Object.entries(required)) {
|
|
162
|
+
const declared = getDeclaredPackageVersion(pkg, name);
|
|
163
|
+
if (!declared || declared === 'workspace:*') continue;
|
|
164
|
+
const declaredMajor = parseMajor(declared);
|
|
165
|
+
const minMajor = parseMajor(minSpec);
|
|
166
|
+
if (declaredMajor != null && minMajor != null && declaredMajor < minMajor) {
|
|
167
|
+
outdated.push(name);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return outdated;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export function findInstalledZhinStackIncompatibilities(
|
|
174
|
+
cwd: string,
|
|
175
|
+
pkg: PackageJsonLike,
|
|
176
|
+
config: Record<string, unknown>,
|
|
177
|
+
): ZhinStackIncompatibility[] {
|
|
178
|
+
const issues: ZhinStackIncompatibility[] = [];
|
|
179
|
+
const zhinMajor = resolvePackageMajor(cwd, pkg, 'zhin.js');
|
|
180
|
+
const agentDeclared = getDeclaredPackageVersion(pkg, '@zhin.js/agent');
|
|
181
|
+
const aiEnabled = isAiEnabledInConfig(config);
|
|
182
|
+
|
|
183
|
+
if (zhinMajor != null && zhinMajor < 4 && (aiEnabled || agentDeclared)) {
|
|
184
|
+
issues.push({
|
|
185
|
+
package: 'zhin.js',
|
|
186
|
+
reason: 'zhin.js 4.x 才支持 @zhin.js/agent 与 AI 安装分层(ADR 0019),请升级 zhin.js',
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (aiEnabled && !agentDeclared) {
|
|
191
|
+
// missing 由 missingFromPackageJson 处理;此处只报版本组合问题
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const agentMajor = resolvePackageMajor(cwd, pkg, '@zhin.js/agent');
|
|
195
|
+
if (agentMajor != null && agentMajor < 1 && aiEnabled) {
|
|
196
|
+
issues.push({
|
|
197
|
+
package: '@zhin.js/agent',
|
|
198
|
+
reason: '@zhin.js/agent 1.x 与 zhin.js 4.x / AI SDK 7 配套,请升级 @zhin.js/agent',
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
return issues;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export interface ZhinStackDiagnosis {
|
|
206
|
+
required: Record<string, string>;
|
|
207
|
+
missingFromPackageJson: string[];
|
|
208
|
+
outdatedInPackageJson: string[];
|
|
209
|
+
incompatibleInstalled: ZhinStackIncompatibility[];
|
|
210
|
+
notInstalled: string[];
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export function packagesNeedingZhinStackFix(diagnosis: ZhinStackDiagnosis): string[] {
|
|
214
|
+
const names = new Set<string>([
|
|
215
|
+
...diagnosis.missingFromPackageJson,
|
|
216
|
+
...diagnosis.outdatedInPackageJson,
|
|
217
|
+
]);
|
|
218
|
+
for (const issue of diagnosis.incompatibleInstalled) {
|
|
219
|
+
names.add(issue.package);
|
|
220
|
+
}
|
|
221
|
+
return [...names];
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
export function diagnoseZhinStackDependencies(
|
|
225
|
+
cwd: string,
|
|
226
|
+
config: Record<string, unknown>,
|
|
227
|
+
pkg?: PackageJsonLike,
|
|
228
|
+
): ZhinStackDiagnosis {
|
|
229
|
+
const required = getRequiredZhinDependenciesForConfig(config);
|
|
230
|
+
const packageJsonPath = path.join(cwd, 'package.json');
|
|
231
|
+
const packageJson = pkg ?? (fs.existsSync(packageJsonPath)
|
|
232
|
+
? fs.readJsonSync(packageJsonPath) as PackageJsonLike
|
|
233
|
+
: {});
|
|
234
|
+
const missingFromPackageJson = findMissingPackageDependencies(packageJson, required);
|
|
235
|
+
const outdatedInPackageJson = findOutdatedZhinStackInPackageJson(packageJson, required);
|
|
236
|
+
const incompatibleInstalled = findInstalledZhinStackIncompatibilities(cwd, packageJson, config);
|
|
237
|
+
const declared = Object.keys(required).filter((name) => !missingFromPackageJson.includes(name));
|
|
238
|
+
const notInstalled = findUnresolvedPackageInstalls(cwd, declared);
|
|
239
|
+
return {
|
|
240
|
+
required,
|
|
241
|
+
missingFromPackageJson,
|
|
242
|
+
outdatedInPackageJson,
|
|
243
|
+
incompatibleInstalled,
|
|
244
|
+
notInstalled,
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
export function formatZhinStackFixCommand(packages: string[], required: Record<string, string>): string {
|
|
249
|
+
if (packages.length === 0) return 'pnpm install';
|
|
250
|
+
const specs = packages.map((name) => `${name}@${required[name] ?? 'latest'}`);
|
|
251
|
+
return `pnpm add ${specs.join(' ')}`;
|
|
252
|
+
}
|