@trim21/personal-pi-extensions 0.1.516 → 0.1.517

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trim21/personal-pi-extensions",
3
- "version": "0.1.516",
3
+ "version": "0.1.517",
4
4
  "type": "module",
5
5
  "description": "Custom pi coding-agent extensions: bwrap sandbox, workspace guard, opencode edit, and more",
6
6
  "keywords": [
package/src/aft/bridge.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  /**
2
2
  * AFT bridge 管理:二进制解析、transport pool 生命周期、工具调用封装。
3
3
  *
4
- * 依赖 @cortexkit/aft-bridge(官方协议的 JS 客户端):findBinary 的解析顺序是
5
- * 缓存 → npm 平台包(@cortexkit/aft-<platform>,随 npm 镜像分发,无运行时网络)
6
- * → PATH → cargo → GitHub release 兜底;内网部署只要保证平台包版本与
4
+ * 依赖 @cortexkit/aft-bridge(官方协议的 JS 客户端):二进制解析由入口在
5
+ * session_start 用 findBinary 完成(缓存 → npm 平台包 PATH → cargo →
6
+ * GitHub release 兜底),本模块只接收解析结果;内网部署只要保证平台包版本与
7
7
  * aft-bridge 锁一致就不会走到最后的网络下载。
8
8
  */
9
9
 
@@ -12,7 +12,6 @@ import {
12
12
  type AftTransportPool,
13
13
  type BridgeRequestOptions,
14
14
  createAftTransportPool,
15
- findBinary,
16
15
  inlineUserConfigTier,
17
16
  readConfigTiers,
18
17
  resolveCortexKitConfigPaths,
@@ -25,6 +24,8 @@ import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
25
24
  import type { SemanticRemote } from "./config.js";
26
25
  import { type AftLogger, createAftLogger } from "./logger.js";
27
26
 
27
+ export { findBinary } from "@cortexkit/aft-bridge";
28
+
28
29
  /** Pi 会话 ID:Rust 侧用它做 session 作用域(undo/checkpoint),感知工具可留空。 */
29
30
  export function resolveSessionId(extCtx: ExtensionContext): string | undefined {
30
31
  const manager = (extCtx as unknown as { sessionManager?: { getSessionId?: () => string } })
@@ -33,22 +34,6 @@ export function resolveSessionId(extCtx: ExtensionContext): string | undefined {
33
34
  return typeof id === "string" && id.length > 0 ? id : undefined;
34
35
  }
35
36
 
36
- /**
37
- * 解析 aft 二进制;失败时抛出(调用方决定是否降级不注册工具)。
38
- * 不带版本参数:findBinary 内部用 @cortexkit/aft-bridge 自身版本作为匹配基准,
39
- * 与 npm 平台包(@cortexkit/aft-<platform>)精确对齐,避免手动读 package.json
40
- * (其 exports 不暴露 ./package.json)。
41
- */
42
- export async function resolveAftBinary(): Promise<string> {
43
- const path = await findBinary();
44
- if (!path) {
45
- throw new Error(
46
- "AFT binary not found. Install via npm platform package (@cortexkit/aft-<platform>), cargo install agent-file-tools, or place `aft` on PATH.",
47
- );
48
- }
49
- return path;
50
- }
51
-
52
37
  export interface AftPool {
53
38
  pool: AftTransportPool;
54
39
  /** 当前项目根(process.cwd),供 bridge 查询。 */
@@ -65,10 +50,11 @@ export interface AftState {
65
50
  export async function createAftState(
66
51
  cwd: string,
67
52
  sessionId: string | undefined,
53
+ binaryPath: string,
68
54
  semantic?: SemanticRemote,
69
55
  ): Promise<AftState> {
70
56
  const logger = createAftLogger(sessionId);
71
- const pool = await createAftPool(cwd, logger, semantic);
57
+ const pool = await createAftPool(cwd, logger, binaryPath, semantic);
72
58
  return { logger, pool };
73
59
  }
74
60
 
@@ -91,6 +77,7 @@ export const SEMANTIC_API_KEY_ENV = "AFT_SEMANTIC_API_KEY";
91
77
 
92
78
  /**
93
79
  * 创建 transport pool。每个项目根一个常驻 aft 进程,跨 session 共享。
80
+ * `binaryPath` 由入口在 session_start 解析好传入(含 auto-download 兜底)。
94
81
  *
95
82
  * `semantic` 决定 embedding 密钥如何送达子进程:aft 只从配置里读 `api_key_env`
96
83
  * 这个「变量名」,再自己 `env::var` 取值,所以提供值时必须连带把名字告诉它。
@@ -99,12 +86,12 @@ export const SEMANTIC_API_KEY_ENV = "AFT_SEMANTIC_API_KEY";
99
86
  export async function createAftPool(
100
87
  cwd: string,
101
88
  logger: AftLogger,
89
+ binaryPath: string,
102
90
  semantic?: SemanticRemote,
103
91
  ): Promise<AftPool> {
104
92
  // 必须在任何 bridge 代码运行前注册:不设 logger 时 aft-bridge 会把 child
105
93
  // stderr / 生命周期日志 fallback 到 console.error,raw 输出打进 pi 的 stderr 破坏 TUI。
106
94
  setActiveLogger(logger);
107
- const binaryPath = await resolveAftBinary();
108
95
  const paths = resolveCortexKitConfigPaths(cwd);
109
96
  const childEnv: Record<string, string> = {
110
97
  // Rust 侧 semantic_search 在索引 Building 时阻塞等待构建完成
package/src/aft/index.ts CHANGED
@@ -9,9 +9,11 @@
9
9
  * fastembed 后端不使用。
10
10
  *
11
11
  * bridge 状态(日志 + 常驻 aft 子进程)的生命周期跟 session 走:session_start
12
- * 时用当次 session id 创建(日志落在 tmp/{sessionId}/aft-plugin.log),
13
- * session_shutdown / 进程退出时释放。工具实现经 getState() 取状态,
14
- * session 未初始化时抛错。
12
+ * 时先解析 aft 二进制(含 GitHub release auto-download 兜底)——找不到就
13
+ * notify warning 且不注册任何 aft 工具,避免模型看到只会抛 "not initialized"
14
+ * 的死工具;找到则注册工具并创建 bridge 状态(日志落在
15
+ * tmp/{sessionId}/aft-plugin.log),session_shutdown / 进程退出时释放。
16
+ * 工具实现经 getState() 取状态。
15
17
  *
16
18
  * Usage:
17
19
  * pi -e ./aft/index.ts
@@ -20,7 +22,7 @@
20
22
  import { resolveCortexKitConfigPaths } from "@cortexkit/aft-bridge";
21
23
  import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
22
24
 
23
- import { createAftState, resolveSessionId, shutdownAftPool } from "./bridge.js";
25
+ import { createAftState, findBinary, resolveSessionId, shutdownAftPool } from "./bridge.js";
24
26
  import { loadAftConfig } from "./config.js";
25
27
  import {
26
28
  registerCallgraphTool,
@@ -37,12 +39,6 @@ export default function aftReadTools(pi: ExtensionAPI): void {
37
39
  // bridge 状态跟 session 生命周期走,作用域就是本工厂闭包,不落到模块级。
38
40
  let state: Awaited<ReturnType<typeof createAftState>> | null = null;
39
41
 
40
- // 预热:提前解析二进制并拉起 bridge 子进程;失败直接抛给 pi(runner 捕获
41
- // 后上报 ExtensionError),工具调用侧经 getState() 抛未初始化错误。
42
- pi.on("session_start", async (_event, ctx) => {
43
- state = await createAftState(cwd, resolveSessionId(ctx), cfg.semanticRemote);
44
- });
45
-
46
42
  const getState = (): Awaited<ReturnType<typeof createAftState>> => {
47
43
  if (!state) {
48
44
  throw new Error(
@@ -53,22 +49,38 @@ export default function aftReadTools(pi: ExtensionAPI): void {
53
49
  };
54
50
 
55
51
  const toolCtx = { cwd, getState };
56
- registerOutlineTool(pi, toolCtx);
57
- registerZoomTool(pi, toolCtx);
58
- registerCallgraphTool(pi, toolCtx);
59
- if (cfg.semanticSearch) {
60
- if (cfg.semanticRemote) {
61
- registerSearchTool(pi, toolCtx);
62
- } else {
63
- // 只开了开关、没配外部 embedding 后端:与其静默不注册,不如说明缺什么。
64
- pi.on("session_start", (_event, ctx) => {
52
+
53
+ // 工具注册延迟到 session_start:先确认二进制可用再决定注册面。pi 允许在
54
+ // session_start 里 registerTool(工具表按 name 覆盖,跨 session 重复注册幂等)。
55
+ pi.on("session_start", async (_event, ctx) => {
56
+ const binaryPath = await findBinary();
57
+ if (!binaryPath) {
58
+ ctx.ui.notify(
59
+ "AFT binary not found: aft_outline / aft_zoom / aft_callgraph are not registered. " +
60
+ "Install the npm platform package (@cortexkit/aft-<platform>), run `cargo install agent-file-tools`, " +
61
+ "or place `aft` on PATH, then restart pi.",
62
+ "warning",
63
+ );
64
+ return;
65
+ }
66
+
67
+ registerOutlineTool(pi, toolCtx);
68
+ registerZoomTool(pi, toolCtx);
69
+ registerCallgraphTool(pi, toolCtx);
70
+ if (cfg.semanticSearch) {
71
+ if (cfg.semanticRemote) {
72
+ registerSearchTool(pi, toolCtx);
73
+ } else {
74
+ // 只开了开关、没配外部 embedding 后端:与其静默不注册,不如说明缺什么。
65
75
  ctx.ui.notify(
66
76
  "aft_search is not registered: semantic_search needs an external embedding backend (aft.jsonc semantic.backend = openai_compatible | ollama, plus base_url). The local ONNX fastembed default is not used here.",
67
77
  "warning",
68
78
  );
69
- });
79
+ }
70
80
  }
71
- }
81
+
82
+ state = await createAftState(cwd, resolveSessionId(ctx), binaryPath, cfg.semanticRemote);
83
+ });
72
84
 
73
85
  // 释放当前 session 的 bridge 状态。session_shutdown 是 pi 的正常生命周期;
74
86
  // beforeExit 兜底进程自然退出(不能注册 SIGINT/SIGTERM——那会吞掉 pi 主进程