zhuye 0.1.0 → 0.1.5

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/README.md CHANGED
@@ -8,7 +8,16 @@
8
8
  npm install zhuye
9
9
  ```
10
10
 
11
- 需要本机有 `zhuye` 二进制在 PATH(或通过 `ZHUYE_BIN` 指向)。从 [GitHub Releases](https://github.com/Asketisch/ZhuYe/releases) 下载,或从本仓库 `make build` 产出 `target/release/zhuye`。
11
+ 安装即用:主包通过 optional dependencies 自动安装当前平台的平台子包
12
+ (`zhuye-darwin-arm64` / `zhuye-darwin-x64` / `zhuye-linux-x64` /
13
+ `zhuye-linux-arm64` / `zhuye-win32-x64`),内嵌对应平台的 `zhuye`
14
+ 二进制,SDK 优先解析它 —— 无需自装二进制、无需配环境变量。
15
+
16
+ 二进制解析优先级:显式 `options.bin` → `ZHUYE_BIN` → 平台子包内嵌
17
+ 二进制 → PATH 上的 `zhuye`。当前平台不在支持矩阵(如 musl/Alpine)
18
+ 时回退 PATH:从 [GitHub Releases](https://github.com/Asketisch/ZhuYe/releases)
19
+ 下载,或从本仓库 `make build` 产出 `target/release/zhuye` 后以
20
+ `ZHUYE_BIN` 指向。
12
21
 
13
22
  ## 用法
14
23
 
@@ -62,7 +71,7 @@ const handshake = await agent.nextGlobalEvent({ timeoutMs: 5000 });
62
71
 
63
72
  | 变量 | 作用 |
64
73
  |------|------|
65
- | `ZHUYE_BIN` | `zhuye` 二进制路径(默认搜索 PATH)。 |
74
+ | `ZHUYE_BIN` | `zhuye` 二进制路径(最高优先级覆盖;缺省用平台子包内嵌二进制)。 |
66
75
  | `ZHUYE_MODEL` | 模型名(spec,例如 `mock/mock-1` 用于离线 SDK 测试)。 |
67
76
  | `ZHUYE_REMOTE_TOOL_TIMEOUT_SECS` | 远程工具等待客户端回执的上限,默认 120s。 |
68
77
 
package/dist/agent.d.ts CHANGED
@@ -5,7 +5,11 @@ import { Event, Op, ReviewDecision, ToolOutput } from './protocol.js';
5
5
  export type ToolHandler = (args: Record<string, unknown>) => Promise<ToolOutput> | ToolOutput;
6
6
  /** ZhuyeAgent 启动选项。 */
7
7
  export interface ZhuyeAgentOptions {
8
- /** `zhuye` 二进制路径。默认读 env `ZHUYE_BIN` → PATH 上的 `zhuye`。 */
8
+ /**
9
+ * `zhuye` 二进制路径。缺省按优先级解析:env `ZHUYE_BIN` → 随包安装
10
+ * 的平台子包内嵌二进制(`zhuye-<平台>` optional dependency)→
11
+ * PATH 上的 `zhuye`。
12
+ */
9
13
  bin?: string;
10
14
  /** 工作目录(传给 serve 的 env)。 */
11
15
  cwd?: string;
package/dist/agent.js CHANGED
@@ -13,6 +13,7 @@
13
13
  //! ```
14
14
  import { spawn } from 'node:child_process';
15
15
  import { randomUUID } from 'node:crypto';
16
+ import { resolveBundledBinary } from './binary.js';
16
17
  const ZHUYE_BIN_ENV = 'ZHUYE_BIN';
17
18
  const DEFAULT_BIN = 'zhuye';
18
19
  const READLINE_LIMIT = 16 * 1024 * 1024;
@@ -46,7 +47,12 @@ export class ZhuyeAgent {
46
47
  // config 目录都定位不到),options.env 只做增量覆盖。
47
48
  const env = { ...process.env, ...options.env };
48
49
  const spawnOpts = {
49
- bin: options.bin ?? process.env[ZHUYE_BIN_ENV] ?? DEFAULT_BIN,
50
+ // 解析优先级:显式指定 → 环境变量 → 随平台子包内嵌的二进制 →
51
+ // PATH 上的 `zhuye`。内嵌优先于 PATH:它与 SDK 版本严格配套。
52
+ bin: options.bin ??
53
+ process.env[ZHUYE_BIN_ENV] ??
54
+ resolveBundledBinary() ??
55
+ DEFAULT_BIN,
50
56
  args: ['serve'],
51
57
  cwd: options.cwd,
52
58
  env,
@@ -0,0 +1,16 @@
1
+ /** 当前平台对应的 npm 平台子包名;不在支持矩阵时返回 null。 */
2
+ export declare function platformPackageName(platform?: string, arch?: string): string | null;
3
+ /** 由平台子包目录推导二进制路径(纯函数拆分,便于单测)。 */
4
+ export declare function binaryPathFromPackageDir(pkgDir: string): string;
5
+ /**
6
+ * 解析随包安装的内嵌 `zhuye` 二进制的绝对路径。
7
+ *
8
+ * 平台子包未落地(不支持的平台,或 optionalDependencies 安装失败 ——
9
+ * npm 对 optional 依赖失败静默容忍)时返回 null,由 spawn 链回退到
10
+ * PATH 上的 `zhuye`。
11
+ *
12
+ * 用 createRequire 而非 import.meta.resolve:后者在旧 Node 需要
13
+ * experimental 标志;createRequire 自 Node 12.2 稳定,且天然解析
14
+ * node_modules 的真实落盘路径。
15
+ */
16
+ export declare function resolveBundledBinary(): string | null;
package/dist/binary.js ADDED
@@ -0,0 +1,49 @@
1
+ //! 内嵌二进制解析:定位随平台子包(optionalDependencies)安装的
2
+ //! `zhuye` 可执行文件,让 `npm install zhuye` 后开箱即用。
3
+ //!
4
+ //! 解析优先级见 `agent.ts` 的 spawn 链:options.bin → ZHUYE_BIN →
5
+ //! 本模块 → PATH 上的 `zhuye`。
6
+ import { accessSync, constants } from 'node:fs';
7
+ import { createRequire } from 'node:module';
8
+ import { join } from 'node:path';
9
+ /** process.platform → process.arch → npm 平台子包名(与发布矩阵一一对应)。 */
10
+ const PLATFORM_PACKAGES = {
11
+ darwin: { arm64: 'zhuye-darwin-arm64', x64: 'zhuye-darwin-x64' },
12
+ linux: { arm64: 'zhuye-linux-arm64', x64: 'zhuye-linux-x64' },
13
+ win32: { x64: 'zhuye-win32-x64' },
14
+ };
15
+ const EXE_SUFFIX = process.platform === 'win32' ? '.exe' : '';
16
+ /** 当前平台对应的 npm 平台子包名;不在支持矩阵时返回 null。 */
17
+ export function platformPackageName(platform = process.platform, arch = process.arch) {
18
+ return PLATFORM_PACKAGES[platform]?.[arch] ?? null;
19
+ }
20
+ /** 由平台子包目录推导二进制路径(纯函数拆分,便于单测)。 */
21
+ export function binaryPathFromPackageDir(pkgDir) {
22
+ return join(pkgDir, 'bin', `zhuye${EXE_SUFFIX}`);
23
+ }
24
+ /**
25
+ * 解析随包安装的内嵌 `zhuye` 二进制的绝对路径。
26
+ *
27
+ * 平台子包未落地(不支持的平台,或 optionalDependencies 安装失败 ——
28
+ * npm 对 optional 依赖失败静默容忍)时返回 null,由 spawn 链回退到
29
+ * PATH 上的 `zhuye`。
30
+ *
31
+ * 用 createRequire 而非 import.meta.resolve:后者在旧 Node 需要
32
+ * experimental 标志;createRequire 自 Node 12.2 稳定,且天然解析
33
+ * node_modules 的真实落盘路径。
34
+ */
35
+ export function resolveBundledBinary() {
36
+ const pkg = platformPackageName();
37
+ if (pkg === null)
38
+ return null;
39
+ try {
40
+ const require = createRequire(import.meta.url);
41
+ const manifest = require.resolve(`${pkg}/package.json`);
42
+ const bin = binaryPathFromPackageDir(join(manifest, '..'));
43
+ accessSync(bin, constants.X_OK);
44
+ return bin;
45
+ }
46
+ catch {
47
+ return null;
48
+ }
49
+ }
package/dist/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './protocol.js';
2
2
  export { ZhuyeAgent, ToolHandler, ZhuyeAgentOptions, } from './agent.js';
3
+ export { platformPackageName, resolveBundledBinary, } from './binary.js';
package/dist/index.js CHANGED
@@ -1,3 +1,4 @@
1
1
  //! `zhuye` —— TS SDK 公共入口。
2
2
  export * from './protocol.js';
3
3
  export { ZhuyeAgent, } from './agent.js';
4
+ export { platformPackageName, resolveBundledBinary, } from './binary.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zhuye",
3
- "version": "0.1.0",
3
+ "version": "0.1.5",
4
4
  "description": "ZhuYe Agent SDK for TypeScript: spawn `zhuye serve` and drive it over JSONL stdio.",
5
5
  "author": "ZhuYe Agent Contributors",
6
6
  "homepage": "https://github.com/Asketisch/ZhuYe#readme",
@@ -35,5 +35,12 @@
35
35
  "@types/node": "^20.11.0",
36
36
  "typescript": "^5.4.0",
37
37
  "vitest": "^1.6.0"
38
+ },
39
+ "optionalDependencies": {
40
+ "zhuye-darwin-arm64": "0.1.5",
41
+ "zhuye-darwin-x64": "0.1.5",
42
+ "zhuye-linux-x64": "0.1.5",
43
+ "zhuye-linux-arm64": "0.1.5",
44
+ "zhuye-win32-x64": "0.1.5"
38
45
  }
39
- }
46
+ }