@codemem/pi-extension

中文文档
0.49.1发布于 2026/11/16MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 2184

CodeMem extension for the pi coding agent — ingest, injection, and native memory tools

原始使用文档
文档目录6 节
  1. Install
  2. What it does
  3. Observer derivation (v1)
  4. Configuration
  5. Lifecycle
  6. Peer dependencies

@codemem/pi-extension

Pi coding-agent extension for codemem: session ingest and turn-local memory injection.

Install

Requires Pi 1.1.0 or newer, below 2.0. Upgrade an older host with pi update (pi update --extensions reconciles installed packages, not the host).

npm i -g codemem
codemem setup --pi-only

Update the CLI and Pi extension together, then rerun setup and restart Pi. The extension's CLI fallback uses the grouped memory and session commands. An older CLI may not support those paths if the extension is upgraded separately.

Restart Pi after setup. codemem setup --pi-only configures only Pi. A fresh setup adds npm:@codemem/pi-extension@<version> to settings.json packages (PI_CODING_AGENT_DIR when set, otherwise ~/.pi/agent); an existing configured dev path may be retained. Useful flags:

FlagPurpose
--pi-onlyOnly configure pi
--pi-mcpOpt into MCP via third-party pi-mcp-adapter (writes mcp.json only when detected)
--pi-extension-path <path>Dev: write a local-path packages entry instead of the npm pin

Dev / local-path install (equivalent to --pi-extension-path):

{
  "packages": ["/absolute/path/to/codemem/packages/pi-extension"]
}

Build first so dist/index.js exists (pnpm --filter @codemem/pi-extension build).

Uninstall: remove the @codemem/pi-extension packages entry and restart pi. The shared memory store is left intact.

What it does

  • Ingest — ordinary session events (session_start, user/assistant messages, tool calls/results) prefer POST /api/pi-hooks (source: "pi"), with codemem pi-hook-ingest CLI fallback (spool on failure). Boundary events (session_before_compact, session_shutdown) always attempt the CLI path so a flush can run; failure is fail-open.
  • Injection — on pi's context event, appends a ## codemem memories block to the latest user message of the request copy (never the system prompt, never the saved session). Older user messages replay the exact bytes already injected for them, and a new pack is fetched only for the latest undecided message, so the provider's prefix cache stays stable. Already-shown items are cut from new packs when the pack response carries renderer spans. A contract-valid HTTP or memory pack --json response, including a zero-item pack, ends that fetch; {} and error-shaped bodies fall through. Those span-bearing successes log inject.pack.ok source=pi to the plugin log, the same line pi-hook-inject writes when it is the fallback. Decisions live in memory: a restart fetches again.
  • File context — after a successful built-in read, related memories may be appended to that tool result. Failed reads and other tools are skipped.
  • Tools — registers the 15 memory_* tools natively (HTTP preferred, CLI fallback), including memory_session_search over stored Pi session text. Every HTTP operation first proves the viewer is target-aware (GET /api/prompt-pack-profile must report this process's db_path + identity_target) and re-sends the target on the query, so a foreign or stale older viewer on the port is never trusted (409 or unproven → targeted CLI path). Non-idempotent memory_remember does NOT CLI-replay on ambiguous HTTP failures (timeout/5xx/reset after send). No pi-mcp-adapter needed. Skipped when pi.tools_mode is mcp-adapter.
  • Compaction — session_before_compact attempts a flush only and never returns a custom summary. session_compact skips one new fetch only when willRetry is set and reason is not manual. Failed compaction, agent_settled, manual compaction, and a later user message clear that skip. A threshold compact skips only if that same guard matches.
  • Fork/resume — re-keys stream identity on every session_start. Ingest cursor metadata persists via pi.appendEntry (codemem.cursor). Recall decisions stay in memory and are cleared on session_start; a restart refetches.
  • Cross-agent — one shared, project-scoped store. Memories from OpenCode/Claude/Codex inject into pi and the reverse.
  • Dashboard — pi rows appear in the source-agnostic feed/sessions/projects tabs with no extra setup.

Tool purposes and troubleshooting: Pi extension.

Observer derivation (v1)

Setup can fill unset observer_* keys from pi's API-key providers (cheap-model-first). Credentials stay in memory only and are never written to codemem config. unconfigured (oauth-only) is an auto-derivation status when only OAuth credentials are available, not the state of every observer connection. Configure observer credentials or the connection separately, with provider or model overrides as needed (observer auth). Explicit observer_* config/env always wins.

Configuration

Read from CODEMEM_CONFIG when set, otherwise $XDG_CONFIG_HOME/codemem or ~/.config/codemem, using an existing config.json then config.jsonc. Pi keys live under the pi object, with CODEMEM_PI_* env overrides.

Key / envDefaultMeaning
pi.tools_mode / CODEMEM_PI_TOOLS_MODEnativenative registers tools here; mcp-adapter skips native registration; configure MCP separately
pi.inject_prompts / CODEMEM_PI_INJECT_PROMPTStrueAttach the memory pack to the latest user message on each model request
pi.file_context / CODEMEM_PI_FILE_CONTEXTtrueAppend related memories to successful built-in read results when available

Shared viewer / inject knobs (same as other clients):

EnvDefaultMeaning
CODEMEM_VIEWER_HOST127.0.0.1Viewer host
CODEMEM_VIEWER_PORT38888Viewer port
CODEMEM_VIEWER1Enable viewer use
CODEMEM_VIEWER_AUTO1Auto-start codemem serve start when needed
CODEMEM_RAW_EVENTS_BACKOFF_MS10000Backoff after HTTP stream failure before retrying
CODEMEM_INJECT_LIMIT8Pack item limit
CODEMEM_INJECT_TOKEN_BUDGET800Pack token budget
CODEMEM_INJECT_MAX_CHARS16000Max injection block chars
CODEMEM_INJECT_RETAINED_TOKEN_BUDGETuncappedOptional approximate token ceiling for blocks already injected in the session (estimated as ceil(chars / 4)). Unset, zero, negative, and invalid values mean no cap; when enabled, a full ceiling attaches nothing new, fetches nothing, and never removes or truncates blocks already attached
CODEMEM_PI_HOOK_HTTP_TIMEOUT_MS5000Ingest and native-tool HTTP timeout (ms). Does not set the pack-fetch timeout

Example config:

{
  "pi": {
    "tools_mode": "native",
    "inject_prompts": true,
    "file_context": true
  }
}

Lifecycle

Per pi extension rules: the factory only wires handlers. Viewer auto-start and other session resources begin on session_start and clean up idempotently on session_shutdown. Session state is re-keyed from ctx.sessionManager.getSessionId() on every session_start; durable ingest cursors persist via pi.appendEntry (codemem.cursor). Recall decisions are not session entries.

Peer dependencies

  • @earendil-works/pi-coding-agent >=1.1.0 <2 (declared host peer)
  • typebox (tool parameter schemas)

No @codemem/core or native modules load inside the pi process.

版本与发布记录

@codemem/pi-extension 共发布 30 个版本,首次发布于 2026/09/21;过去 12 个月发布了 30 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.49.1latest2026/11/16今天
0.49.02026/11/15今天
0.49.0-beta.7beta2026/11/141 天前
0.49.0-beta.52026/11/142 天前
0.49.0-beta.42026/11/132 天前
0.49.0-beta.32026/11/133 天前

其他发布通道:bootstrap → 0.0.0-alpha.0,rc → 0.46.0-rc.4,beta → 0.49.0-beta.7。

关于 @codemem/pi-extension 的常见问题

@codemem/pi-extension 是什么?

CodeMem extension for the pi coding agent — ingest, injection, and native memory tools

如何安装 @codemem/pi-extension?

在项目目录运行 npm install @codemem/pi-extension,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:typebox(*)、@earendil-works/pi-coding-agent(>=1.1.0 <2)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@codemem/pi-extension 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@codemem/pi-extension 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@codemem/pi-extension 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=24。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@codemem/pi-extension 可以商用吗?

@codemem/pi-extension 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@codemem/pi-extension 的下载量有多少?

根据 npm 官方下载统计,@codemem/pi-extension 在 2026/10/10 – 2026/11/08 这 30 天内被下载 2,184 次,日均约 73 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@codemem/pi-extension 还在维护吗?

最新版本 0.49.1 发布于 2026/11/16(今天);至今共发布 30 个版本,其中过去 12 个月发布了 30 个;首次发布于 2026/09/21。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。