context-mode 1.0.150 → 1.0.152
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/.claude-plugin/marketplace.json +2 -2
- package/.claude-plugin/plugin.json +1 -1
- package/.codex-plugin/mcp.json +5 -1
- package/.codex-plugin/plugin.json +1 -1
- package/.openclaw-plugin/openclaw.plugin.json +16 -1
- package/.openclaw-plugin/package.json +1 -1
- package/README.md +89 -3
- package/build/adapters/claude-code/hooks.js +2 -2
- package/build/adapters/claude-code/index.js +14 -13
- package/build/adapters/client-map.js +3 -0
- package/build/adapters/detect.js +13 -1
- package/build/adapters/gemini-cli/hooks.d.ts +10 -0
- package/build/adapters/gemini-cli/hooks.js +12 -2
- package/build/adapters/gemini-cli/index.d.ts +21 -1
- package/build/adapters/gemini-cli/index.js +37 -1
- package/build/adapters/kimi/config.d.ts +8 -0
- package/build/adapters/kimi/config.js +8 -0
- package/build/adapters/kimi/hooks.d.ts +28 -0
- package/build/adapters/kimi/hooks.js +34 -0
- package/build/adapters/kimi/index.d.ts +66 -0
- package/build/adapters/kimi/index.js +537 -0
- package/build/adapters/kimi/paths.d.ts +1 -0
- package/build/adapters/kimi/paths.js +12 -0
- package/build/adapters/kiro/hooks.js +2 -2
- package/build/adapters/openclaw/plugin.d.ts +14 -13
- package/build/adapters/openclaw/plugin.js +140 -40
- package/build/adapters/opencode/plugin.js +4 -3
- package/build/adapters/opencode/zod3tov4.js +8 -8
- package/build/adapters/pi/extension.js +9 -24
- package/build/adapters/pi/mcp-bridge.js +37 -0
- package/build/adapters/qwen-code/index.js +7 -7
- package/build/adapters/types.d.ts +39 -2
- package/build/adapters/types.js +55 -2
- package/build/adapters/vscode-copilot/index.js +13 -1
- package/build/cli.js +433 -25
- package/build/executor.js +6 -3
- package/build/runtime.d.ts +81 -1
- package/build/runtime.js +195 -9
- package/build/search/ctx-search-schema.d.ts +90 -0
- package/build/search/ctx-search-schema.js +135 -0
- package/build/search/unified.d.ts +12 -0
- package/build/search/unified.js +17 -2
- package/build/server.d.ts +2 -1
- package/build/server.js +378 -97
- package/build/session/analytics.d.ts +36 -13
- package/build/session/analytics.js +123 -26
- package/build/session/db.d.ts +24 -0
- package/build/session/db.js +41 -0
- package/build/session/extract.js +30 -0
- package/build/session/snapshot.js +24 -0
- package/build/store.d.ts +12 -1
- package/build/store.js +72 -20
- package/build/types.d.ts +7 -0
- package/build/util/project-dir.d.ts +19 -16
- package/build/util/project-dir.js +80 -45
- package/cli.bundle.mjs +371 -320
- package/configs/kimi/hooks.json +54 -0
- package/configs/pi/AGENTS.md +3 -85
- package/hooks/cache-heal-utils.mjs +148 -0
- package/hooks/core/formatters.mjs +26 -0
- package/hooks/core/routing.mjs +9 -1
- package/hooks/core/stdin.mjs +74 -3
- package/hooks/core/tool-naming.mjs +1 -0
- package/hooks/heal-partial-install.mjs +712 -0
- package/hooks/kimi/platform.mjs +1 -0
- package/hooks/kimi/posttooluse.mjs +72 -0
- package/hooks/kimi/precompact.mjs +80 -0
- package/hooks/kimi/pretooluse.mjs +42 -0
- package/hooks/kimi/sessionend.mjs +61 -0
- package/hooks/kimi/sessionstart.mjs +113 -0
- package/hooks/kimi/stop.mjs +61 -0
- package/hooks/kimi/userpromptsubmit.mjs +90 -0
- package/hooks/normalize-hooks.mjs +66 -12
- package/hooks/routing-block.mjs +8 -2
- package/hooks/security.bundle.mjs +1 -1
- package/hooks/session-db.bundle.mjs +6 -4
- package/hooks/session-extract.bundle.mjs +2 -2
- package/hooks/session-helpers.mjs +93 -3
- package/hooks/session-snapshot.bundle.mjs +20 -19
- package/hooks/sessionstart.mjs +64 -0
- package/insight/server.mjs +15 -3
- package/openclaw.plugin.json +16 -1
- package/package.json +1 -1
- package/scripts/heal-installed-plugins.mjs +31 -10
- package/scripts/postinstall.mjs +10 -0
- package/server.bundle.mjs +206 -157
- package/skills/ctx-index/SKILL.md +46 -0
- package/skills/ctx-search/SKILL.md +35 -0
- package/start.mjs +84 -11
- package/build/cache-heal.d.ts +0 -48
- package/build/cache-heal.js +0 -150
- package/build/concurrency/runPool.d.ts +0 -36
- package/build/concurrency/runPool.js +0 -51
- package/build/openclaw/mcp-tools.d.ts +0 -54
- package/build/openclaw/mcp-tools.js +0 -198
- package/build/openclaw/workspace-router.d.ts +0 -29
- package/build/openclaw/workspace-router.js +0 -64
- package/build/openclaw-plugin.d.ts +0 -130
- package/build/openclaw-plugin.js +0 -626
- package/build/opencode-plugin.d.ts +0 -122
- package/build/opencode-plugin.js +0 -375
- package/build/pi-extension.d.ts +0 -14
- package/build/pi-extension.js +0 -451
- package/build/routing-block.d.ts +0 -8
- package/build/routing-block.js +0 -86
- package/build/tool-naming.d.ts +0 -4
- package/build/tool-naming.js +0 -24
package/build/pi-extension.js
DELETED
|
@@ -1,451 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Pi coding agent extension for context-mode.
|
|
3
|
-
*
|
|
4
|
-
* Follows the OpenClaw adapter pattern: imports shared session modules,
|
|
5
|
-
* registers Pi-specific hooks. NO copy-paste of session logic.
|
|
6
|
-
* NO external npm dependencies beyond what Pi runtime provides.
|
|
7
|
-
*
|
|
8
|
-
* Entry point: `export default function(pi: ExtensionAPI) { ... }`
|
|
9
|
-
*
|
|
10
|
-
* Lifecycle: session_start, tool_call, tool_result, before_agent_start,
|
|
11
|
-
* session_before_compact, session_compact, session_shutdown.
|
|
12
|
-
*/
|
|
13
|
-
import { createHash } from "node:crypto";
|
|
14
|
-
import { existsSync, mkdirSync } from "node:fs";
|
|
15
|
-
import { homedir } from "node:os";
|
|
16
|
-
import { join, resolve, dirname } from "node:path";
|
|
17
|
-
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
18
|
-
import { SessionDB } from "./session/db.js";
|
|
19
|
-
import { extractEvents, extractUserEvents } from "./session/extract.js";
|
|
20
|
-
import { buildResumeSnapshot } from "./session/snapshot.js";
|
|
21
|
-
// ── Pi Tool Name Mapping ─────────────────────────────────
|
|
22
|
-
// Pi uses lowercase; shared extractors expect PascalCase (Claude Code convention).
|
|
23
|
-
const PI_TOOL_MAP = {
|
|
24
|
-
bash: "Bash",
|
|
25
|
-
read: "Read",
|
|
26
|
-
write: "Write",
|
|
27
|
-
edit: "Edit",
|
|
28
|
-
grep: "Grep",
|
|
29
|
-
find: "Glob",
|
|
30
|
-
ls: "Glob",
|
|
31
|
-
};
|
|
32
|
-
// ── Routing patterns ─────────────────────────────────────
|
|
33
|
-
// Inline HTTP client patterns to block in bash — self-contained, no routing module needed.
|
|
34
|
-
const BLOCKED_BASH_PATTERNS = [
|
|
35
|
-
/\bcurl\s/,
|
|
36
|
-
/\bwget\s/,
|
|
37
|
-
/\bfetch\s*\(/,
|
|
38
|
-
/\brequests\.get\s*\(/,
|
|
39
|
-
/\brequests\.post\s*\(/,
|
|
40
|
-
/\bhttp\.get\s*\(/,
|
|
41
|
-
/\bhttp\.request\s*\(/,
|
|
42
|
-
/\burllib\.request/,
|
|
43
|
-
/\bInvoke-WebRequest\b/,
|
|
44
|
-
];
|
|
45
|
-
// ── Module-level DB singleton ────────────────────────────
|
|
46
|
-
let _db = null;
|
|
47
|
-
let _sessionId = "";
|
|
48
|
-
// Per-session gate: routing block injected at most once per session_id.
|
|
49
|
-
const _routingInjected = new Set();
|
|
50
|
-
// Cached routing-block string (built once per process from hooks/routing-block.mjs).
|
|
51
|
-
let _routingBlock = null;
|
|
52
|
-
async function getRoutingBlock(pluginRoot) {
|
|
53
|
-
if (_routingBlock !== null)
|
|
54
|
-
return _routingBlock;
|
|
55
|
-
try {
|
|
56
|
-
const routingMod = await import(pathToFileURL(join(pluginRoot, "hooks", "routing-block.mjs")).href);
|
|
57
|
-
const namingMod = await import(pathToFileURL(join(pluginRoot, "hooks", "core", "tool-naming.mjs")).href);
|
|
58
|
-
const t = namingMod.createToolNamer("pi");
|
|
59
|
-
_routingBlock = String(routingMod.createRoutingBlock(t));
|
|
60
|
-
}
|
|
61
|
-
catch {
|
|
62
|
-
_routingBlock = "";
|
|
63
|
-
}
|
|
64
|
-
return _routingBlock;
|
|
65
|
-
}
|
|
66
|
-
// Cached buildAutoInjection (500-token cap, prioritized).
|
|
67
|
-
let _buildAutoInjection = undefined;
|
|
68
|
-
async function getAutoInjection(pluginRoot) {
|
|
69
|
-
if (_buildAutoInjection !== undefined)
|
|
70
|
-
return _buildAutoInjection;
|
|
71
|
-
try {
|
|
72
|
-
const mod = await import(pathToFileURL(join(pluginRoot, "hooks", "auto-injection.mjs")).href);
|
|
73
|
-
_buildAutoInjection = mod.buildAutoInjection;
|
|
74
|
-
}
|
|
75
|
-
catch {
|
|
76
|
-
_buildAutoInjection = null;
|
|
77
|
-
}
|
|
78
|
-
return _buildAutoInjection ?? null;
|
|
79
|
-
}
|
|
80
|
-
// ── Helpers ──────────────────────────────────────────────
|
|
81
|
-
function getSessionDir() {
|
|
82
|
-
const dir = join(homedir(), ".pi", "context-mode", "sessions");
|
|
83
|
-
mkdirSync(dir, { recursive: true });
|
|
84
|
-
return dir;
|
|
85
|
-
}
|
|
86
|
-
function getDBPath() {
|
|
87
|
-
return join(getSessionDir(), "context-mode.db");
|
|
88
|
-
}
|
|
89
|
-
function getOrCreateDB() {
|
|
90
|
-
if (!_db) {
|
|
91
|
-
_db = new SessionDB({ dbPath: getDBPath() });
|
|
92
|
-
}
|
|
93
|
-
return _db;
|
|
94
|
-
}
|
|
95
|
-
/** Derive a stable session ID from Pi's session file path (SHA256, 16 hex chars). */
|
|
96
|
-
function deriveSessionId(ctx) {
|
|
97
|
-
try {
|
|
98
|
-
const sessionManager = ctx.sessionManager;
|
|
99
|
-
const sessionFile = sessionManager?.getSessionFile?.();
|
|
100
|
-
if (sessionFile && typeof sessionFile === "string") {
|
|
101
|
-
return createHash("sha256").update(sessionFile).digest("hex").slice(0, 16);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
catch {
|
|
105
|
-
// best effort
|
|
106
|
-
}
|
|
107
|
-
return `pi-${Date.now()}`;
|
|
108
|
-
}
|
|
109
|
-
/** Build stats text for the /ctx-stats command. */
|
|
110
|
-
function buildStatsText(db, sessionId) {
|
|
111
|
-
try {
|
|
112
|
-
const events = db.getEvents(sessionId);
|
|
113
|
-
const stats = db.getSessionStats(sessionId);
|
|
114
|
-
const lines = [
|
|
115
|
-
"## context-mode stats (Pi)",
|
|
116
|
-
"",
|
|
117
|
-
`- Session: \`${sessionId.slice(0, 8)}...\``,
|
|
118
|
-
`- Events captured: ${events.length}`,
|
|
119
|
-
`- Compactions: ${stats?.compact_count ?? 0}`,
|
|
120
|
-
];
|
|
121
|
-
// Event breakdown by category
|
|
122
|
-
const byCategory = {};
|
|
123
|
-
for (const ev of events) {
|
|
124
|
-
const key = ev.category ?? "unknown";
|
|
125
|
-
byCategory[key] = (byCategory[key] ?? 0) + 1;
|
|
126
|
-
}
|
|
127
|
-
if (Object.keys(byCategory).length > 0) {
|
|
128
|
-
lines.push("- Event breakdown:");
|
|
129
|
-
for (const [category, count] of Object.entries(byCategory)) {
|
|
130
|
-
lines.push(` - ${category}: ${count}`);
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
// Session age
|
|
134
|
-
if (stats?.started_at) {
|
|
135
|
-
const startedMs = new Date(stats.started_at).getTime();
|
|
136
|
-
const ageMinutes = Math.round((Date.now() - startedMs) / 60_000);
|
|
137
|
-
lines.push(`- Session age: ${ageMinutes}m`);
|
|
138
|
-
}
|
|
139
|
-
return lines.join("\n");
|
|
140
|
-
}
|
|
141
|
-
catch {
|
|
142
|
-
return "context-mode stats unavailable (session DB error)";
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
function resolveCommandContext(argsOrCtx, ctx) {
|
|
146
|
-
if (ctx !== undefined)
|
|
147
|
-
return ctx;
|
|
148
|
-
if (argsOrCtx && typeof argsOrCtx === "object")
|
|
149
|
-
return argsOrCtx;
|
|
150
|
-
return undefined;
|
|
151
|
-
}
|
|
152
|
-
function handleCommandText(text, ctx) {
|
|
153
|
-
if (ctx?.hasUI) {
|
|
154
|
-
ctx.ui.notify(text, "info");
|
|
155
|
-
return;
|
|
156
|
-
}
|
|
157
|
-
return { text };
|
|
158
|
-
}
|
|
159
|
-
// ── Extension entry point ────────────────────────────────
|
|
160
|
-
/** Pi extension default export. Called once by Pi runtime with the extension API. */
|
|
161
|
-
export default function piExtension(pi) {
|
|
162
|
-
const buildDir = dirname(fileURLToPath(import.meta.url));
|
|
163
|
-
const pluginRoot = resolve(buildDir, "..");
|
|
164
|
-
const projectDir = process.env.PI_PROJECT_DIR || process.cwd();
|
|
165
|
-
const db = getOrCreateDB();
|
|
166
|
-
// ── 1. session_start — Initialize session ──────────────
|
|
167
|
-
pi.on("session_start", (ctx) => {
|
|
168
|
-
try {
|
|
169
|
-
_sessionId = deriveSessionId(ctx ?? {});
|
|
170
|
-
db.ensureSession(_sessionId, projectDir);
|
|
171
|
-
db.cleanupOldSessions(7);
|
|
172
|
-
}
|
|
173
|
-
catch {
|
|
174
|
-
// best effort — never break session start
|
|
175
|
-
if (!_sessionId) {
|
|
176
|
-
_sessionId = `pi-${Date.now()}`;
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
});
|
|
180
|
-
// ── 2. tool_call — PreToolUse routing enforcement ──────
|
|
181
|
-
// Block bash commands that contain curl/wget/fetch/requests patterns.
|
|
182
|
-
pi.on("tool_call", (event) => {
|
|
183
|
-
try {
|
|
184
|
-
const toolName = String(event?.toolName ?? "").toLowerCase();
|
|
185
|
-
if (toolName !== "bash")
|
|
186
|
-
return;
|
|
187
|
-
const command = String(event?.input?.command ?? "");
|
|
188
|
-
if (!command)
|
|
189
|
-
return;
|
|
190
|
-
const isBlocked = BLOCKED_BASH_PATTERNS.some((p) => p.test(command));
|
|
191
|
-
if (isBlocked) {
|
|
192
|
-
return {
|
|
193
|
-
block: true,
|
|
194
|
-
reason: "Use context-mode MCP tools (execute, fetch_and_index) instead of inline HTTP clients. " +
|
|
195
|
-
"Raw curl/wget/fetch output floods the context window.",
|
|
196
|
-
};
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
catch {
|
|
200
|
-
// Routing failure — allow passthrough
|
|
201
|
-
}
|
|
202
|
-
});
|
|
203
|
-
// ── 3. tool_result — PostToolUse event capture ─────────
|
|
204
|
-
pi.on("tool_result", (event) => {
|
|
205
|
-
try {
|
|
206
|
-
if (!_sessionId)
|
|
207
|
-
return;
|
|
208
|
-
const rawToolName = String(event?.toolName ?? event?.tool_name ?? "");
|
|
209
|
-
const mappedToolName = PI_TOOL_MAP[rawToolName.toLowerCase()] ?? rawToolName;
|
|
210
|
-
// Normalize result to string
|
|
211
|
-
const rawResult = event?.result ?? event?.output;
|
|
212
|
-
const resultStr = typeof rawResult === "string"
|
|
213
|
-
? rawResult
|
|
214
|
-
: rawResult != null
|
|
215
|
-
? JSON.stringify(rawResult)
|
|
216
|
-
: undefined;
|
|
217
|
-
// Detect errors
|
|
218
|
-
const hasError = Boolean(event?.error || event?.isError);
|
|
219
|
-
const hookInput = {
|
|
220
|
-
tool_name: mappedToolName,
|
|
221
|
-
tool_input: event?.params ?? event?.input ?? {},
|
|
222
|
-
tool_response: resultStr,
|
|
223
|
-
tool_output: hasError ? { isError: true } : undefined,
|
|
224
|
-
};
|
|
225
|
-
const events = extractEvents(hookInput);
|
|
226
|
-
if (events.length > 0) {
|
|
227
|
-
for (const ev of events) {
|
|
228
|
-
db.insertEvent(_sessionId, ev, "PostToolUse");
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
else if (rawToolName) {
|
|
232
|
-
// Fallback: record unrecognized tool call as generic event
|
|
233
|
-
const data = JSON.stringify({
|
|
234
|
-
tool: rawToolName,
|
|
235
|
-
params: event?.params ?? event?.input,
|
|
236
|
-
});
|
|
237
|
-
db.insertEvent(_sessionId, {
|
|
238
|
-
type: "tool_call",
|
|
239
|
-
category: "pi",
|
|
240
|
-
data,
|
|
241
|
-
priority: 1,
|
|
242
|
-
data_hash: createHash("sha256")
|
|
243
|
-
.update(data)
|
|
244
|
-
.digest("hex")
|
|
245
|
-
.slice(0, 16),
|
|
246
|
-
}, "PostToolUse");
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
catch {
|
|
250
|
-
// Silent — session capture must never break the tool call
|
|
251
|
-
}
|
|
252
|
-
});
|
|
253
|
-
// ── 4. before_agent_start — Routing + active_memory + resume injection ─
|
|
254
|
-
pi.on("before_agent_start", async (event) => {
|
|
255
|
-
try {
|
|
256
|
-
if (!_sessionId)
|
|
257
|
-
return;
|
|
258
|
-
const prompt = String(event?.prompt ?? "");
|
|
259
|
-
// Extract user events from the prompt text
|
|
260
|
-
if (prompt) {
|
|
261
|
-
const userEvents = extractUserEvents(prompt);
|
|
262
|
-
for (const ev of userEvents) {
|
|
263
|
-
db.insertEvent(_sessionId, ev, "UserPromptSubmit");
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
const existingPrompt = String(event?.systemPrompt ?? "");
|
|
267
|
-
const parts = [];
|
|
268
|
-
if (existingPrompt)
|
|
269
|
-
parts.push(existingPrompt);
|
|
270
|
-
// Pi-1: Inject routing block once per session (gated by _routingInjected).
|
|
271
|
-
// v1.0.107 — visible marker so Pi users can verify the routing block
|
|
272
|
-
// reached the model (Mickey-class verification path; mirrors OpenCode).
|
|
273
|
-
if (!_routingInjected.has(_sessionId)) {
|
|
274
|
-
const routingBlock = await getRoutingBlock(pluginRoot);
|
|
275
|
-
if (routingBlock) {
|
|
276
|
-
const marker = `<!-- context-mode: routing block injected (sessionID=${String(_sessionId).slice(0, 8)}) -->`;
|
|
277
|
-
parts.push(marker + "\n" + routingBlock);
|
|
278
|
-
_routingInjected.add(_sessionId);
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
// Pi-3 + Pi-4: Always build active_memory (not just post-compact),
|
|
282
|
-
// capped at 500 tokens via buildAutoInjection. Falls back to inline
|
|
283
|
-
// budget loop if the helper is unavailable.
|
|
284
|
-
const activeEvents = db.getEvents(_sessionId, {
|
|
285
|
-
minPriority: 3,
|
|
286
|
-
limit: 50,
|
|
287
|
-
});
|
|
288
|
-
if (activeEvents.length > 0) {
|
|
289
|
-
const buildAuto = await getAutoInjection(pluginRoot);
|
|
290
|
-
let memoryContext = "";
|
|
291
|
-
if (buildAuto) {
|
|
292
|
-
memoryContext = buildAuto(activeEvents.map((e) => ({
|
|
293
|
-
category: String(e.category ?? ""),
|
|
294
|
-
data: String(e.data ?? ""),
|
|
295
|
-
})));
|
|
296
|
-
}
|
|
297
|
-
// Fallback (or if helper produced empty output): inline 500-token cap.
|
|
298
|
-
if (!memoryContext) {
|
|
299
|
-
const memoryLines = ["<active_memory>"];
|
|
300
|
-
let budget = 2000; // ~500 tokens at 4 chars/token
|
|
301
|
-
for (const ev of activeEvents) {
|
|
302
|
-
const line = ` <event type="${ev.type}" category="${ev.category}">${ev.data}</event>`;
|
|
303
|
-
if (line.length > budget)
|
|
304
|
-
break;
|
|
305
|
-
memoryLines.push(line);
|
|
306
|
-
budget -= line.length;
|
|
307
|
-
}
|
|
308
|
-
memoryLines.push("</active_memory>");
|
|
309
|
-
if (memoryLines.length > 2)
|
|
310
|
-
memoryContext = memoryLines.join("\n");
|
|
311
|
-
}
|
|
312
|
-
if (memoryContext)
|
|
313
|
-
parts.push(memoryContext);
|
|
314
|
-
}
|
|
315
|
-
// Resume snapshot (only when present and unconsumed).
|
|
316
|
-
const resume = db.getResume(_sessionId);
|
|
317
|
-
if (resume && !resume.consumed && resume.snapshot) {
|
|
318
|
-
parts.push(resume.snapshot);
|
|
319
|
-
db.markResumeConsumed(_sessionId);
|
|
320
|
-
}
|
|
321
|
-
// Return modified systemPrompt only if we added something beyond existing.
|
|
322
|
-
const baseLen = existingPrompt ? 1 : 0;
|
|
323
|
-
if (parts.length > baseLen) {
|
|
324
|
-
return { systemPrompt: parts.join("\n\n") };
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
catch {
|
|
328
|
-
// best effort — never break agent start
|
|
329
|
-
}
|
|
330
|
-
});
|
|
331
|
-
// ── 4b. before_provider_response — capture response metadata ───
|
|
332
|
-
// Pi-2: Register the missing event so providers can record latency,
|
|
333
|
-
// model, and token usage when Pi exposes them. Best-effort only;
|
|
334
|
-
// the handler must never throw or modify the response.
|
|
335
|
-
pi.on("before_provider_response", (event) => {
|
|
336
|
-
try {
|
|
337
|
-
if (!_sessionId)
|
|
338
|
-
return;
|
|
339
|
-
const meta = {
|
|
340
|
-
model: event?.model ?? event?.providerModel,
|
|
341
|
-
provider: event?.provider,
|
|
342
|
-
latencyMs: event?.latencyMs ?? event?.latency,
|
|
343
|
-
tokens: event?.usage ?? event?.tokens,
|
|
344
|
-
};
|
|
345
|
-
// Skip when Pi gives us nothing useful — avoids noise in the DB.
|
|
346
|
-
if (meta.model == null &&
|
|
347
|
-
meta.provider == null &&
|
|
348
|
-
meta.latencyMs == null &&
|
|
349
|
-
meta.tokens == null) {
|
|
350
|
-
return;
|
|
351
|
-
}
|
|
352
|
-
const data = JSON.stringify(meta);
|
|
353
|
-
db.insertEvent(_sessionId, {
|
|
354
|
-
type: "provider_response",
|
|
355
|
-
category: "pi",
|
|
356
|
-
data,
|
|
357
|
-
priority: 1,
|
|
358
|
-
data_hash: createHash("sha256").update(data).digest("hex").slice(0, 16),
|
|
359
|
-
}, "PostToolUse");
|
|
360
|
-
}
|
|
361
|
-
catch {
|
|
362
|
-
// best effort — never break provider response
|
|
363
|
-
}
|
|
364
|
-
});
|
|
365
|
-
// ── 5. session_before_compact — Build resume snapshot ──
|
|
366
|
-
pi.on("session_before_compact", () => {
|
|
367
|
-
try {
|
|
368
|
-
if (!_sessionId)
|
|
369
|
-
return;
|
|
370
|
-
const allEvents = db.getEvents(_sessionId);
|
|
371
|
-
if (allEvents.length === 0)
|
|
372
|
-
return;
|
|
373
|
-
const stats = db.getSessionStats(_sessionId);
|
|
374
|
-
const snapshot = buildResumeSnapshot(allEvents, {
|
|
375
|
-
compactCount: (stats?.compact_count ?? 0) + 1,
|
|
376
|
-
});
|
|
377
|
-
db.upsertResume(_sessionId, snapshot, allEvents.length);
|
|
378
|
-
}
|
|
379
|
-
catch {
|
|
380
|
-
// best effort — never break compaction
|
|
381
|
-
}
|
|
382
|
-
});
|
|
383
|
-
// ── 6. session_compact — Increment compact counter ─────
|
|
384
|
-
pi.on("session_compact", () => {
|
|
385
|
-
try {
|
|
386
|
-
if (!_sessionId)
|
|
387
|
-
return;
|
|
388
|
-
db.incrementCompactCount(_sessionId);
|
|
389
|
-
}
|
|
390
|
-
catch {
|
|
391
|
-
// best effort
|
|
392
|
-
}
|
|
393
|
-
});
|
|
394
|
-
// ── 7. session_shutdown — Cleanup old sessions ─────────
|
|
395
|
-
pi.on("session_shutdown", () => {
|
|
396
|
-
try {
|
|
397
|
-
if (_db) {
|
|
398
|
-
_db.cleanupOldSessions(7);
|
|
399
|
-
}
|
|
400
|
-
_db = null;
|
|
401
|
-
_routingInjected.clear();
|
|
402
|
-
_sessionId = "";
|
|
403
|
-
}
|
|
404
|
-
catch {
|
|
405
|
-
// best effort — never throw during shutdown
|
|
406
|
-
}
|
|
407
|
-
});
|
|
408
|
-
// ── 8. Slash commands ──────────────────────────────────
|
|
409
|
-
pi.registerCommand("ctx-stats", {
|
|
410
|
-
description: "Show context-mode session statistics",
|
|
411
|
-
handler: async (argsOrCtx, maybeCtx) => {
|
|
412
|
-
const ctx = resolveCommandContext(argsOrCtx, maybeCtx);
|
|
413
|
-
const text = !_db || !_sessionId
|
|
414
|
-
? "context-mode: no active session"
|
|
415
|
-
: buildStatsText(_db, _sessionId);
|
|
416
|
-
return handleCommandText(text, ctx);
|
|
417
|
-
},
|
|
418
|
-
});
|
|
419
|
-
pi.registerCommand("ctx-doctor", {
|
|
420
|
-
description: "Run context-mode diagnostics",
|
|
421
|
-
handler: async (argsOrCtx, maybeCtx) => {
|
|
422
|
-
const ctx = resolveCommandContext(argsOrCtx, maybeCtx);
|
|
423
|
-
const dbPath = getDBPath();
|
|
424
|
-
const dbExists = existsSync(dbPath);
|
|
425
|
-
const lines = [
|
|
426
|
-
"## ctx-doctor (Pi)",
|
|
427
|
-
"",
|
|
428
|
-
`- DB path: \`${dbPath}\``,
|
|
429
|
-
`- DB exists: ${dbExists}`,
|
|
430
|
-
`- Session ID: \`${_sessionId ? _sessionId.slice(0, 8) + "..." : "none"}\``,
|
|
431
|
-
`- Plugin root: \`${pluginRoot}\``,
|
|
432
|
-
`- Project dir: \`${projectDir}\``,
|
|
433
|
-
];
|
|
434
|
-
if (_db && _sessionId) {
|
|
435
|
-
try {
|
|
436
|
-
const stats = _db.getSessionStats(_sessionId);
|
|
437
|
-
const eventCount = _db.getEventCount(_sessionId);
|
|
438
|
-
lines.push(`- Events: ${eventCount}`);
|
|
439
|
-
lines.push(`- Compactions: ${stats?.compact_count ?? 0}`);
|
|
440
|
-
const resume = _db.getResume(_sessionId);
|
|
441
|
-
lines.push(`- Resume snapshot: ${resume ? (resume.consumed ? "consumed" : "available") : "none"}`);
|
|
442
|
-
}
|
|
443
|
-
catch {
|
|
444
|
-
lines.push("- DB query error");
|
|
445
|
-
}
|
|
446
|
-
}
|
|
447
|
-
const text = lines.join("\n");
|
|
448
|
-
return handleCommandText(text, ctx);
|
|
449
|
-
},
|
|
450
|
-
});
|
|
451
|
-
}
|
package/build/routing-block.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { ToolNamer } from "./tool-naming.js";
|
|
2
|
-
export interface RoutingBlockOptions {
|
|
3
|
-
includeCommands?: boolean;
|
|
4
|
-
}
|
|
5
|
-
export declare function createRoutingBlock(t: ToolNamer, options?: RoutingBlockOptions): string;
|
|
6
|
-
export declare function createReadGuidance(t: ToolNamer): string;
|
|
7
|
-
export declare function createGrepGuidance(t: ToolNamer): string;
|
|
8
|
-
export declare function createBashGuidance(t: ToolNamer): string;
|
package/build/routing-block.js
DELETED
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
export function createRoutingBlock(t, options = {}) {
|
|
2
|
-
const { includeCommands = true } = options;
|
|
3
|
-
return `
|
|
4
|
-
<context_window_protection>
|
|
5
|
-
<priority_instructions>
|
|
6
|
-
Raw tool output floods context window. MUST use context-mode MCP tools. Keep raw data in sandbox.
|
|
7
|
-
</priority_instructions>
|
|
8
|
-
|
|
9
|
-
<tool_selection_hierarchy>
|
|
10
|
-
0. MEMORY: ${t("ctx_search")}(sort: "timeline")
|
|
11
|
-
- After resume, check prior context before asking user.
|
|
12
|
-
1. GATHER: ${t("ctx_batch_execute")}(commands, queries)
|
|
13
|
-
- Primary research tool. Runs commands, auto-indexes, searches. ONE call replaces many steps.
|
|
14
|
-
- Each command: {label: "section header", command: "shell command"}
|
|
15
|
-
- label becomes FTS5 chunk title — descriptive labels improve search.
|
|
16
|
-
2. FOLLOW-UP: ${t("ctx_search")}(queries: ["q1", "q2", ...])
|
|
17
|
-
- All follow-up questions. ONE call, many queries (default relevance mode).
|
|
18
|
-
3. PROCESSING: ${t("ctx_execute")}(language, code) | ${t("ctx_execute_file")}(path, language, code)
|
|
19
|
-
- API calls, log analysis, data processing.
|
|
20
|
-
</tool_selection_hierarchy>
|
|
21
|
-
|
|
22
|
-
<forbidden_actions>
|
|
23
|
-
- NO Bash for commands producing >20 lines output.
|
|
24
|
-
- NO Read for analysis — use execute_file. Read IS correct for files you intend to Edit.
|
|
25
|
-
- NO WebFetch — use ${t("ctx_fetch_and_index")}.
|
|
26
|
-
- Bash ONLY for git/mkdir/rm/mv/navigation.
|
|
27
|
-
- NO ${t("ctx_execute")} or ${t("ctx_execute_file")} for file creation/modification.
|
|
28
|
-
ctx_execute is for analysis, processing, computation only.
|
|
29
|
-
</forbidden_actions>
|
|
30
|
-
|
|
31
|
-
<file_writing_policy>
|
|
32
|
-
ALWAYS use native Write/Edit tools for file creation/modification.
|
|
33
|
-
NEVER use ${t("ctx_execute")}, ${t("ctx_execute_file")}, or Bash to write files.
|
|
34
|
-
Applies to all file types: code, configs, plans, specs, YAML, JSON, markdown.
|
|
35
|
-
</file_writing_policy>
|
|
36
|
-
|
|
37
|
-
<output_constraints>
|
|
38
|
-
<communication_style>
|
|
39
|
-
Terse like caveman. Technical substance exact. Only fluff die.
|
|
40
|
-
Use fragments when clear. Short synonyms (fix not "implement a solution for").
|
|
41
|
-
Technical terms exact. Code blocks unchanged.
|
|
42
|
-
Auto-expand for: security warnings, irreversible actions, user confusion.
|
|
43
|
-
</communication_style>
|
|
44
|
-
<artifact_policy>
|
|
45
|
-
Write artifacts (code, configs, PRDs) to FILES. NEVER inline.
|
|
46
|
-
Return only: file path + 1-line description.
|
|
47
|
-
</artifact_policy>
|
|
48
|
-
<response_format>
|
|
49
|
-
Concise summary:
|
|
50
|
-
- Actions taken (2-3 bullets)
|
|
51
|
-
- File paths created/modified
|
|
52
|
-
- Key findings
|
|
53
|
-
</response_format>
|
|
54
|
-
</output_constraints>
|
|
55
|
-
<session_continuity>
|
|
56
|
-
Skills, roles, and decisions set during this session remain active until the user revokes them.
|
|
57
|
-
Do not drop behavioral directives as context grows.
|
|
58
|
-
</session_continuity>
|
|
59
|
-
${includeCommands ? `
|
|
60
|
-
<ctx_commands>
|
|
61
|
-
"ctx stats" | "ctx-stats" | "/ctx-stats" | context savings question
|
|
62
|
-
→ Call stats MCP tool, display full output verbatim.
|
|
63
|
-
|
|
64
|
-
"ctx doctor" | "ctx-doctor" | "/ctx-doctor" | diagnose context-mode
|
|
65
|
-
→ Call doctor MCP tool, run returned shell command, display as checklist.
|
|
66
|
-
|
|
67
|
-
"ctx upgrade" | "ctx-upgrade" | "/ctx-upgrade" | update context-mode
|
|
68
|
-
→ Call upgrade MCP tool, run returned shell command, display as checklist.
|
|
69
|
-
|
|
70
|
-
"ctx purge" | "ctx-purge" | "/ctx-purge" | wipe/reset knowledge base
|
|
71
|
-
→ Call purge MCP tool with confirm: true. Warn: irreversible.
|
|
72
|
-
|
|
73
|
-
After /clear or /compact: knowledge base preserved. Tell user: "context-mode knowledge base preserved. Use \`ctx purge\` to start fresh."
|
|
74
|
-
</ctx_commands>
|
|
75
|
-
` : ''}
|
|
76
|
-
</context_window_protection>`;
|
|
77
|
-
}
|
|
78
|
-
export function createReadGuidance(t) {
|
|
79
|
-
return '<context_guidance>\n <tip>\n Reading to Edit? Read is correct — Edit needs content in context.\n Reading to analyze/explore? Use ' + t("ctx_execute_file") + '(path, language, code) — only printed summary enters context.\n </tip>\n</context_guidance>';
|
|
80
|
-
}
|
|
81
|
-
export function createGrepGuidance(t) {
|
|
82
|
-
return '<context_guidance>\n <tip>\n May flood context. Use ' + t("ctx_execute") + '(language: "shell", code: "...") to run searches in sandbox. Only printed summary enters context.\n </tip>\n</context_guidance>';
|
|
83
|
-
}
|
|
84
|
-
export function createBashGuidance(t) {
|
|
85
|
-
return '<context_guidance>\n <tip>\n May produce large output. Use ' + t("ctx_batch_execute") + '(commands, queries) for multiple commands, ' + t("ctx_execute") + '(language: "shell", code: "...") for single. Only printed summary enters context. Bash only for: git, mkdir, rm, mv, navigation.\n </tip>\n</context_guidance>';
|
|
86
|
-
}
|
package/build/tool-naming.d.ts
DELETED
package/build/tool-naming.js
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
const TOOL_PREFIXES = {
|
|
2
|
-
"claude-code": (tool) => `mcp__plugin_context-mode_context-mode__${tool}`,
|
|
3
|
-
"gemini-cli": (tool) => `mcp__context-mode__${tool}`,
|
|
4
|
-
"antigravity": (tool) => `mcp__context-mode__${tool}`,
|
|
5
|
-
"opencode": (tool) => `context-mode_${tool}`,
|
|
6
|
-
"kilo": (tool) => `context-mode_${tool}`,
|
|
7
|
-
"vscode-copilot": (tool) => `context-mode_${tool}`,
|
|
8
|
-
"jetbrains-copilot": (tool) => `context-mode_${tool}`,
|
|
9
|
-
"kiro": (tool) => `@context-mode/${tool}`,
|
|
10
|
-
"zed": (tool) => `mcp:context-mode:${tool}`,
|
|
11
|
-
"cursor": (tool) => tool,
|
|
12
|
-
"codex": (tool) => tool,
|
|
13
|
-
"openclaw": (tool) => tool,
|
|
14
|
-
"pi": (tool) => tool,
|
|
15
|
-
"qwen-code": (tool) => `mcp__context-mode__${tool}`,
|
|
16
|
-
};
|
|
17
|
-
export function getToolName(platform, bareTool) {
|
|
18
|
-
const fn = TOOL_PREFIXES[platform] || TOOL_PREFIXES["claude-code"];
|
|
19
|
-
return fn(bareTool);
|
|
20
|
-
}
|
|
21
|
-
export function createToolNamer(platform) {
|
|
22
|
-
return (bareTool) => getToolName(platform, bareTool);
|
|
23
|
-
}
|
|
24
|
-
export const KNOWN_PLATFORMS = Object.keys(TOOL_PREFIXES);
|