@mxalbert/context-mode 2.0.0
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 +33 -0
- package/.claude-plugin/plugin.json +31 -0
- package/.codex-plugin/hooks.json +65 -0
- package/.codex-plugin/mcp.json +13 -0
- package/.codex-plugin/plugin.json +31 -0
- package/.openclaw-plugin/index.ts +12 -0
- package/.openclaw-plugin/openclaw.plugin.json +38 -0
- package/.openclaw-plugin/package.json +28 -0
- package/LICENSE +94 -0
- package/README.md +1619 -0
- package/bin/statusline.mjs +378 -0
- package/build/adapters/antigravity/index.d.ts +51 -0
- package/build/adapters/antigravity/index.js +169 -0
- package/build/adapters/antigravity-cli/index.d.ts +51 -0
- package/build/adapters/antigravity-cli/index.js +342 -0
- package/build/adapters/base.d.ts +92 -0
- package/build/adapters/base.js +135 -0
- package/build/adapters/claude-code/config.d.ts +8 -0
- package/build/adapters/claude-code/config.js +8 -0
- package/build/adapters/claude-code/hooks.d.ts +106 -0
- package/build/adapters/claude-code/hooks.js +183 -0
- package/build/adapters/claude-code/index.d.ts +80 -0
- package/build/adapters/claude-code/index.js +569 -0
- package/build/adapters/claude-code-base.d.ts +49 -0
- package/build/adapters/claude-code-base.js +113 -0
- package/build/adapters/client-map.d.ts +10 -0
- package/build/adapters/client-map.js +42 -0
- package/build/adapters/codex/config.d.ts +8 -0
- package/build/adapters/codex/config.js +8 -0
- package/build/adapters/codex/hooks.d.ts +55 -0
- package/build/adapters/codex/hooks.js +64 -0
- package/build/adapters/codex/index.d.ts +87 -0
- package/build/adapters/codex/index.js +995 -0
- package/build/adapters/codex/paths.d.ts +1 -0
- package/build/adapters/codex/paths.js +12 -0
- package/build/adapters/codex/usage.d.ts +107 -0
- package/build/adapters/codex/usage.js +227 -0
- package/build/adapters/copilot-base.d.ts +94 -0
- package/build/adapters/copilot-base.js +297 -0
- package/build/adapters/copilot-cli/hooks.d.ts +33 -0
- package/build/adapters/copilot-cli/hooks.js +64 -0
- package/build/adapters/copilot-cli/index.d.ts +48 -0
- package/build/adapters/copilot-cli/index.js +341 -0
- package/build/adapters/cursor/config.d.ts +4 -0
- package/build/adapters/cursor/config.js +4 -0
- package/build/adapters/cursor/hooks.d.ts +55 -0
- package/build/adapters/cursor/hooks.js +82 -0
- package/build/adapters/cursor/index.d.ts +64 -0
- package/build/adapters/cursor/index.js +517 -0
- package/build/adapters/detect.d.ts +136 -0
- package/build/adapters/detect.js +623 -0
- package/build/adapters/gemini-cli/config.d.ts +8 -0
- package/build/adapters/gemini-cli/config.js +8 -0
- package/build/adapters/gemini-cli/hooks.d.ts +83 -0
- package/build/adapters/gemini-cli/hooks.js +115 -0
- package/build/adapters/gemini-cli/index.d.ts +80 -0
- package/build/adapters/gemini-cli/index.js +483 -0
- package/build/adapters/jetbrains-copilot/config.d.ts +8 -0
- package/build/adapters/jetbrains-copilot/config.js +8 -0
- package/build/adapters/jetbrains-copilot/hooks.d.ts +59 -0
- package/build/adapters/jetbrains-copilot/hooks.js +87 -0
- package/build/adapters/jetbrains-copilot/index.d.ts +31 -0
- package/build/adapters/jetbrains-copilot/index.js +131 -0
- 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 +21 -0
- package/build/adapters/kimi/paths.js +52 -0
- package/build/adapters/kimi/usage.d.ts +82 -0
- package/build/adapters/kimi/usage.js +217 -0
- package/build/adapters/kiro/hooks.d.ts +61 -0
- package/build/adapters/kiro/hooks.js +94 -0
- package/build/adapters/kiro/index.d.ts +61 -0
- package/build/adapters/kiro/index.js +301 -0
- package/build/adapters/omp/index.d.ts +65 -0
- package/build/adapters/omp/index.js +182 -0
- package/build/adapters/omp/plugin.d.ts +81 -0
- package/build/adapters/omp/plugin.js +331 -0
- package/build/adapters/omp/usage.d.ts +49 -0
- package/build/adapters/omp/usage.js +110 -0
- package/build/adapters/openclaw/config.d.ts +8 -0
- package/build/adapters/openclaw/config.js +8 -0
- package/build/adapters/openclaw/hooks.d.ts +50 -0
- package/build/adapters/openclaw/hooks.js +61 -0
- package/build/adapters/openclaw/index.d.ts +74 -0
- package/build/adapters/openclaw/index.js +439 -0
- package/build/adapters/openclaw/mcp-tools.d.ts +54 -0
- package/build/adapters/openclaw/mcp-tools.js +207 -0
- package/build/adapters/openclaw/plugin.d.ts +141 -0
- package/build/adapters/openclaw/plugin.js +818 -0
- package/build/adapters/openclaw/session-db.d.ts +55 -0
- package/build/adapters/openclaw/session-db.js +88 -0
- package/build/adapters/openclaw/usage.d.ts +34 -0
- package/build/adapters/openclaw/usage.js +52 -0
- package/build/adapters/openclaw/workspace-router.d.ts +29 -0
- package/build/adapters/openclaw/workspace-router.js +64 -0
- package/build/adapters/opencode/config.d.ts +8 -0
- package/build/adapters/opencode/config.js +8 -0
- package/build/adapters/opencode/hooks.d.ts +38 -0
- package/build/adapters/opencode/hooks.js +50 -0
- package/build/adapters/opencode/index.d.ts +144 -0
- package/build/adapters/opencode/index.js +638 -0
- package/build/adapters/opencode/plugin.d.ts +249 -0
- package/build/adapters/opencode/plugin.js +1536 -0
- package/build/adapters/opencode/v2.d.ts +117 -0
- package/build/adapters/opencode/v2.js +224 -0
- package/build/adapters/opencode/zod3tov4.d.ts +1 -0
- package/build/adapters/opencode/zod3tov4.js +132 -0
- package/build/adapters/pi/extension.d.ts +81 -0
- package/build/adapters/pi/extension.js +861 -0
- package/build/adapters/pi/index.d.ts +57 -0
- package/build/adapters/pi/index.js +173 -0
- package/build/adapters/pi/mcp-bridge.d.ts +268 -0
- package/build/adapters/pi/mcp-bridge.js +894 -0
- package/build/adapters/qwen-code/hooks.d.ts +26 -0
- package/build/adapters/qwen-code/hooks.js +29 -0
- package/build/adapters/qwen-code/index.d.ts +40 -0
- package/build/adapters/qwen-code/index.js +361 -0
- package/build/adapters/qwen-code/usage.d.ts +90 -0
- package/build/adapters/qwen-code/usage.js +222 -0
- package/build/adapters/types.d.ts +360 -0
- package/build/adapters/types.js +118 -0
- package/build/adapters/vscode-copilot/config.d.ts +8 -0
- package/build/adapters/vscode-copilot/config.js +8 -0
- package/build/adapters/vscode-copilot/hooks.d.ts +71 -0
- package/build/adapters/vscode-copilot/hooks.js +98 -0
- package/build/adapters/vscode-copilot/index.d.ts +32 -0
- package/build/adapters/vscode-copilot/index.js +222 -0
- package/build/adapters/zed/index.d.ts +40 -0
- package/build/adapters/zed/index.js +163 -0
- package/build/cli.d.ts +34 -0
- package/build/cli.js +1828 -0
- package/build/db-base.d.ts +250 -0
- package/build/db-base.js +756 -0
- package/build/executor.d.ts +72 -0
- package/build/executor.js +678 -0
- package/build/exit-classify.d.ts +19 -0
- package/build/exit-classify.js +12 -0
- package/build/fetch/blocks.d.ts +145 -0
- package/build/fetch/blocks.js +244 -0
- package/build/fetch/extract.d.ts +68 -0
- package/build/fetch/extract.js +123 -0
- package/build/fetch/page-store.d.ts +66 -0
- package/build/fetch/page-store.js +174 -0
- package/build/fetch-cache.d.ts +13 -0
- package/build/fetch-cache.js +15 -0
- package/build/lifecycle.d.ts +124 -0
- package/build/lifecycle.js +278 -0
- package/build/package-identity.d.ts +26 -0
- package/build/package-identity.js +28 -0
- package/build/runPool.d.ts +36 -0
- package/build/runPool.js +51 -0
- package/build/runtime.d.ts +71 -0
- package/build/runtime.js +659 -0
- package/build/search/auto-memory.d.ts +47 -0
- package/build/search/auto-memory.js +170 -0
- package/build/search/ctx-search-schema.d.ts +90 -0
- package/build/search/ctx-search-schema.js +135 -0
- package/build/search/flood-guard.d.ts +57 -0
- package/build/search/flood-guard.js +80 -0
- package/build/search/unified.d.ts +56 -0
- package/build/search/unified.js +104 -0
- package/build/security.d.ts +206 -0
- package/build/security.js +780 -0
- package/build/server.d.ts +240 -0
- package/build/server.js +5034 -0
- package/build/session/analytics.d.ts +773 -0
- package/build/session/analytics.js +2420 -0
- package/build/session/db.d.ts +468 -0
- package/build/session/db.js +1330 -0
- package/build/session/error-classifier.d.ts +87 -0
- package/build/session/error-classifier.js +303 -0
- package/build/session/event-emit.d.ts +48 -0
- package/build/session/event-emit.js +101 -0
- package/build/session/extract.d.ts +333 -0
- package/build/session/extract.js +2688 -0
- package/build/session/model-prices.json +429 -0
- package/build/session/persist-tool-calls.d.ts +54 -0
- package/build/session/persist-tool-calls.js +105 -0
- package/build/session/pricing.d.ts +64 -0
- package/build/session/pricing.js +151 -0
- package/build/session/project-attribution.d.ts +73 -0
- package/build/session/project-attribution.js +245 -0
- package/build/session/purge.d.ts +138 -0
- package/build/session/purge.js +240 -0
- package/build/session/retrieval-marker.d.ts +39 -0
- package/build/session/retrieval-marker.js +65 -0
- package/build/session/snapshot.d.ts +46 -0
- package/build/session/snapshot.js +532 -0
- package/build/store-directory.d.ts +56 -0
- package/build/store-directory.js +254 -0
- package/build/store.d.ts +147 -0
- package/build/store.js +1714 -0
- package/build/truncate.d.ts +57 -0
- package/build/truncate.js +142 -0
- package/build/types.d.ts +117 -0
- package/build/types.js +20 -0
- package/build/util/claude-config.d.ts +32 -0
- package/build/util/claude-config.js +84 -0
- package/build/util/hook-config.d.ts +27 -0
- package/build/util/hook-config.js +76 -0
- package/build/util/jsonc.d.ts +14 -0
- package/build/util/jsonc.js +104 -0
- package/build/util/plugin-cache-integrity.d.ts +51 -0
- package/build/util/plugin-cache-integrity.js +146 -0
- package/build/util/project-dir.d.ts +149 -0
- package/build/util/project-dir.js +340 -0
- package/build/util/sibling-mcp.d.ts +79 -0
- package/build/util/sibling-mcp.js +181 -0
- package/cli.bundle.mjs +1495 -0
- package/configs/antigravity/GEMINI.md +68 -0
- package/configs/antigravity/mcp_config.json +7 -0
- package/configs/antigravity-cli/hooks/hooks.json +37 -0
- package/configs/antigravity-cli/hooks.json +37 -0
- package/configs/antigravity-cli/mcp_config.json +10 -0
- package/configs/antigravity-cli/plugin.json +14 -0
- package/configs/antigravity-cli/rules/context-mode.md +77 -0
- package/configs/antigravity-cli/skills/context-mode/SKILL.md +77 -0
- package/configs/claude-code/CLAUDE.md +91 -0
- package/configs/codex/AGENTS.md +99 -0
- package/configs/codex/config.toml +8 -0
- package/configs/codex/hooks.json +47 -0
- package/configs/copilot-cli/.github/plugin/plugin.json +23 -0
- package/configs/copilot-cli/.mcp.json +12 -0
- package/configs/copilot-cli/README.md +47 -0
- package/configs/copilot-cli/hooks.json +41 -0
- package/configs/copilot-cli/skills/context-mode/SKILL.md +38 -0
- package/configs/cursor/context-mode.mdc +76 -0
- package/configs/cursor/hooks.json +21 -0
- package/configs/cursor/mcp.json +7 -0
- package/configs/gemini-cli/GEMINI.md +88 -0
- package/configs/gemini-cli/mcp.json +7 -0
- package/configs/gemini-cli/settings.json +60 -0
- package/configs/jetbrains-copilot/copilot-instructions.md +80 -0
- package/configs/jetbrains-copilot/hooks.json +16 -0
- package/configs/jetbrains-copilot/mcp.json +7 -0
- package/configs/kilo/AGENTS.md +84 -0
- package/configs/kilo/kilo.json +6 -0
- package/configs/kimi/hooks.json +54 -0
- package/configs/kiro/KIRO.md +84 -0
- package/configs/kiro/agent.json +18 -0
- package/configs/kiro/mcp.json +7 -0
- package/configs/omp/SYSTEM.md +85 -0
- package/configs/omp/mcp.json +7 -0
- package/configs/openclaw/AGENTS.md +87 -0
- package/configs/openclaw/openclaw.json +13 -0
- package/configs/opencode/AGENTS.md +84 -0
- package/configs/opencode/opencode.json +6 -0
- package/configs/pi/AGENTS.md +3 -0
- package/configs/qwen-code/QWEN.md +91 -0
- package/configs/vscode-copilot/copilot-instructions.md +80 -0
- package/configs/vscode-copilot/hooks.json +16 -0
- package/configs/vscode-copilot/mcp.json +7 -0
- package/configs/zed/AGENTS.md +68 -0
- package/hooks/antigravity-cli/payload.mjs +98 -0
- package/hooks/antigravity-cli/posttooluse.mjs +138 -0
- package/hooks/antigravity-cli/pretooluse.mjs +78 -0
- package/hooks/antigravity-cli/stop.mjs +58 -0
- package/hooks/auto-injection.mjs +102 -0
- package/hooks/cache-heal-utils.mjs +379 -0
- package/hooks/codex/platform.mjs +1 -0
- package/hooks/codex/posttooluse.mjs +66 -0
- package/hooks/codex/precompact.mjs +70 -0
- package/hooks/codex/pretooluse.mjs +42 -0
- package/hooks/codex/sessionstart.mjs +121 -0
- package/hooks/codex/stop.mjs +138 -0
- package/hooks/codex/userpromptsubmit.mjs +75 -0
- package/hooks/copilot-cli/posttooluse.mjs +79 -0
- package/hooks/copilot-cli/precompact.mjs +66 -0
- package/hooks/copilot-cli/pretooluse.mjs +41 -0
- package/hooks/copilot-cli/sessionstart.mjs +121 -0
- package/hooks/copilot-cli/stop.mjs +59 -0
- package/hooks/copilot-cli/userpromptsubmit.mjs +77 -0
- package/hooks/core/codex-caps.mjs +112 -0
- package/hooks/core/formatters.mjs +356 -0
- package/hooks/core/mcp-ready.mjs +109 -0
- package/hooks/core/platform-detect.mjs +49 -0
- package/hooks/core/routing.mjs +1050 -0
- package/hooks/core/stdin.mjs +90 -0
- package/hooks/core/tool-naming.mjs +57 -0
- package/hooks/cursor/afteragentresponse.mjs +74 -0
- package/hooks/cursor/hooks.json +31 -0
- package/hooks/cursor/posttooluse.mjs +72 -0
- package/hooks/cursor/pretooluse.mjs +26 -0
- package/hooks/cursor/sessionstart.mjs +97 -0
- package/hooks/cursor/stop.mjs +48 -0
- package/hooks/ensure-deps.mjs +254 -0
- package/hooks/formatters/claude-code.mjs +113 -0
- package/hooks/formatters/cursor.mjs +37 -0
- package/hooks/formatters/gemini-cli.mjs +55 -0
- package/hooks/formatters/vscode-copilot.mjs +55 -0
- package/hooks/gemini-cli/aftermodel.mjs +70 -0
- package/hooks/gemini-cli/aftertool.mjs +61 -0
- package/hooks/gemini-cli/beforeagent.mjs +99 -0
- package/hooks/gemini-cli/beforetool.mjs +27 -0
- package/hooks/gemini-cli/precompress.mjs +55 -0
- package/hooks/gemini-cli/sessionstart.mjs +137 -0
- package/hooks/heal-partial-install.mjs +712 -0
- package/hooks/hooks.json +143 -0
- package/hooks/jetbrains-copilot/posttooluse.mjs +61 -0
- package/hooks/jetbrains-copilot/precompact.mjs +55 -0
- package/hooks/jetbrains-copilot/pretooluse.mjs +27 -0
- package/hooks/jetbrains-copilot/sessionstart.mjs +124 -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 +132 -0
- package/hooks/kimi/userpromptsubmit.mjs +90 -0
- package/hooks/kiro/agentspawn.mjs +97 -0
- package/hooks/kiro/posttooluse.mjs +51 -0
- package/hooks/kiro/pretooluse.mjs +64 -0
- package/hooks/kiro/userpromptsubmit.mjs +88 -0
- package/hooks/normalize-hooks.mjs +323 -0
- package/hooks/platform-bridge.mjs +340 -0
- package/hooks/posttooluse.mjs +225 -0
- package/hooks/precompact.mjs +100 -0
- package/hooks/pretooluse.mjs +227 -0
- package/hooks/qwen-code/platform.mjs +1 -0
- package/hooks/qwen-code/stop.mjs +168 -0
- package/hooks/routing-block.mjs +105 -0
- package/hooks/run-hook.mjs +95 -0
- package/hooks/security.bundle.mjs +2 -0
- package/hooks/session-attribution.bundle.mjs +1 -0
- package/hooks/session-db.bundle.mjs +145 -0
- package/hooks/session-directive.mjs +520 -0
- package/hooks/session-extract.bundle.mjs +3 -0
- package/hooks/session-helpers.mjs +428 -0
- package/hooks/session-loaders.mjs +448 -0
- package/hooks/session-snapshot.bundle.mjs +32 -0
- package/hooks/sessionstart.mjs +464 -0
- package/hooks/stop.mjs +82 -0
- package/hooks/suppress-stderr.mjs +25 -0
- package/hooks/userpromptsubmit.mjs +108 -0
- package/hooks/vscode-copilot/posttooluse.mjs +61 -0
- package/hooks/vscode-copilot/precompact.mjs +55 -0
- package/hooks/vscode-copilot/pretooluse.mjs +27 -0
- package/hooks/vscode-copilot/sessionstart.mjs +129 -0
- package/openclaw.plugin.json +38 -0
- package/package.json +132 -0
- package/scripts/heal-better-sqlite3.mjs +430 -0
- package/scripts/heal-installed-plugins.mjs +635 -0
- package/scripts/plugin-cache-integrity.mjs +248 -0
- package/scripts/postinstall.mjs +397 -0
- package/server.bundle.mjs +1421 -0
- package/server.js +5 -0
- package/skills/.ignore +7 -0
- package/skills/context-mode/SKILL.md +300 -0
- package/skills/context-mode/references/anti-patterns.md +283 -0
- package/skills/context-mode/references/patterns-javascript.md +298 -0
- package/skills/context-mode/references/patterns-python.md +304 -0
- package/skills/context-mode/references/patterns-shell.md +277 -0
- package/skills/ctx-doctor/SKILL.md +22 -0
- package/skills/ctx-index/SKILL.md +46 -0
- package/skills/ctx-insight/SKILL.md +24 -0
- package/skills/ctx-purge/SKILL.md +49 -0
- package/skills/ctx-search/SKILL.md +35 -0
- package/skills/ctx-stats/SKILL.md +26 -0
- package/skills/ctx-upgrade/SKILL.md +31 -0
- package/start.mjs +616 -0
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import "./platform.mjs";
|
|
3
|
+
import "../suppress-stderr.mjs";
|
|
4
|
+
import "../ensure-deps.mjs";
|
|
5
|
+
/**
|
|
6
|
+
* Kimi Code CLI sessionStart hook for context-mode.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { createRoutingBlock } from "../routing-block.mjs";
|
|
10
|
+
import { createToolNamer } from "../core/tool-naming.mjs";
|
|
11
|
+
|
|
12
|
+
const toolNamer = createToolNamer("kimi");
|
|
13
|
+
const ROUTING_BLOCK = createRoutingBlock(toolNamer);
|
|
14
|
+
import {
|
|
15
|
+
writeSessionEventsFile,
|
|
16
|
+
buildSessionDirective,
|
|
17
|
+
getSessionEvents,
|
|
18
|
+
} from "../session-directive.mjs";
|
|
19
|
+
import {
|
|
20
|
+
readStdin,
|
|
21
|
+
parseStdin,
|
|
22
|
+
getSessionId,
|
|
23
|
+
getSessionDBPath,
|
|
24
|
+
getSessionEventsPath,
|
|
25
|
+
getCleanupFlagPath,
|
|
26
|
+
getInputProjectDir,
|
|
27
|
+
resolveConfigDir,
|
|
28
|
+
KIMI_OPTS,
|
|
29
|
+
} from "../session-helpers.mjs";
|
|
30
|
+
import { createSessionLoaders } from "../session-loaders.mjs";
|
|
31
|
+
import { existsSync, readFileSync, unlinkSync } from "node:fs";
|
|
32
|
+
import { join } from "node:path";
|
|
33
|
+
import { fileURLToPath } from "node:url";
|
|
34
|
+
|
|
35
|
+
const HOOK_DIR = fileURLToPath(new URL(".", import.meta.url));
|
|
36
|
+
const { loadSessionDB } = createSessionLoaders(HOOK_DIR);
|
|
37
|
+
const OPTS = KIMI_OPTS;
|
|
38
|
+
|
|
39
|
+
let additionalContext = ROUTING_BLOCK;
|
|
40
|
+
|
|
41
|
+
function captureKimiInstructionRules(db, sessionId, projectDir) {
|
|
42
|
+
const paths = [];
|
|
43
|
+
for (const baseDir of [resolveConfigDir(OPTS), projectDir]) {
|
|
44
|
+
paths.push(join(baseDir, "AGENTS.md"));
|
|
45
|
+
paths.push(join(baseDir, "AGENTS.override.md"));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
for (const p of [...new Set(paths)]) {
|
|
49
|
+
try {
|
|
50
|
+
if (!existsSync(p)) continue;
|
|
51
|
+
const content = readFileSync(p, "utf8");
|
|
52
|
+
db.insertEvent(sessionId, { type: "rule", category: "rule", data: p, priority: 1 });
|
|
53
|
+
db.insertEvent(sessionId, { type: "rule_content", category: "rule", data: content, priority: 1 });
|
|
54
|
+
} catch {
|
|
55
|
+
// Missing or unreadable rule files should never break SessionStart.
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
try {
|
|
61
|
+
const raw = await readStdin();
|
|
62
|
+
const input = parseStdin(raw);
|
|
63
|
+
// Kimi Code emits ONLY 'startup' | 'resume' for SessionStart.source:
|
|
64
|
+
// refs/platforms/kimi-code/.../session/index.ts:153,181,495
|
|
65
|
+
// (triggerSessionStart signature is `source: 'startup' | 'resume'`)
|
|
66
|
+
// refs/platforms/kimi-cli/src/kimi_cli/cli/__init__.py:642
|
|
67
|
+
// (`_session_source = "resume" if resumed else "startup"`)
|
|
68
|
+
// Default unknown values to 'startup' rather than branching on a
|
|
69
|
+
// 'compact' path that the host never reaches.
|
|
70
|
+
const source = input.source === "resume" ? "resume" : "startup";
|
|
71
|
+
const projectDir = getInputProjectDir(input, KIMI_OPTS);
|
|
72
|
+
|
|
73
|
+
if (source === "resume") {
|
|
74
|
+
const { SessionDB } = await loadSessionDB();
|
|
75
|
+
const dbPath = getSessionDBPath(OPTS, projectDir);
|
|
76
|
+
const db = new SessionDB({ dbPath });
|
|
77
|
+
const sessionId = getSessionId(input, OPTS);
|
|
78
|
+
|
|
79
|
+
try { unlinkSync(getCleanupFlagPath(OPTS, projectDir)); } catch { /* no flag */ }
|
|
80
|
+
|
|
81
|
+
const events = sessionId ? getSessionEvents(db, sessionId) : [];
|
|
82
|
+
if (events.length > 0) {
|
|
83
|
+
const eventMeta = writeSessionEventsFile(events, getSessionEventsPath(OPTS, projectDir));
|
|
84
|
+
additionalContext += buildSessionDirective(source, eventMeta, toolNamer);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
db.close();
|
|
88
|
+
} else {
|
|
89
|
+
// source === "startup"
|
|
90
|
+
const { SessionDB } = await loadSessionDB();
|
|
91
|
+
const dbPath = getSessionDBPath(OPTS, projectDir);
|
|
92
|
+
const db = new SessionDB({ dbPath });
|
|
93
|
+
try { unlinkSync(getSessionEventsPath(OPTS, projectDir)); } catch { /* no stale file */ }
|
|
94
|
+
|
|
95
|
+
db.cleanupOldSessions(7);
|
|
96
|
+
// Single source of truth lives in SessionDB. Reaching through `db.db.exec`
|
|
97
|
+
// duplicated schema knowledge in the hook and would silently drift if
|
|
98
|
+
// `session_events` ever renamed its FK column.
|
|
99
|
+
db.pruneOrphanedEvents();
|
|
100
|
+
|
|
101
|
+
const sessionId = getSessionId(input, OPTS);
|
|
102
|
+
db.ensureSession(sessionId, projectDir);
|
|
103
|
+
captureKimiInstructionRules(db, sessionId, projectDir);
|
|
104
|
+
|
|
105
|
+
db.close();
|
|
106
|
+
}
|
|
107
|
+
} catch {
|
|
108
|
+
// Swallow errors — hook must not fail
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
process.stdout.write(JSON.stringify({
|
|
112
|
+
hookSpecificOutput: { hookEventName: "SessionStart", additionalContext },
|
|
113
|
+
}) + "\n");
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import "./platform.mjs";
|
|
3
|
+
import "../suppress-stderr.mjs";
|
|
4
|
+
import "../ensure-deps.mjs";
|
|
5
|
+
/**
|
|
6
|
+
* Kimi Code CLI Stop hook — record turn-end state for continuity.
|
|
7
|
+
*
|
|
8
|
+
* Stop fires at the END OF EACH ASSISTANT TURN, not at session close.
|
|
9
|
+
* Kimi Code emits a distinct `SessionEnd` event for genuine session
|
|
10
|
+
* shutdown (refs/platforms/kimi-code/.../session/index.ts:192,502 —
|
|
11
|
+
* `triggerSessionEnd('exit')`); the matching `hooks/kimi/sessionend.mjs`
|
|
12
|
+
* owns the `session_end` SessionDB row. Writing `session_end` here would
|
|
13
|
+
* have produced one such row per turn.
|
|
14
|
+
* Cross-reference: refs/platforms/kimi-cli/src/kimi_cli/hooks/events.py:
|
|
15
|
+
* 99-114 — `session_start` and `session_end` are distinct emitters.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { readStdin, parseStdin, getSessionId, getSessionDBPath, getInputProjectDir, KIMI_OPTS } from "../session-helpers.mjs";
|
|
19
|
+
import { createSessionLoaders, attributeAndInsertEvents } from "../session-loaders.mjs";
|
|
20
|
+
import { dirname, join, resolve } from "node:path";
|
|
21
|
+
import { fileURLToPath } from "node:url";
|
|
22
|
+
import { homedir } from "node:os";
|
|
23
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
24
|
+
|
|
25
|
+
const HOOK_DIR = dirname(fileURLToPath(import.meta.url));
|
|
26
|
+
const { loadSessionDB, loadExtract, loadProjectAttribution } = createSessionLoaders(HOOK_DIR);
|
|
27
|
+
const OPTS = KIMI_OPTS;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Resolve <kimiConfigDir> (mirrors src/adapters/kimi/paths.ts:resolveKimiConfigDir
|
|
31
|
+
* — hooks cannot import the TS module, so the logic is duplicated here).
|
|
32
|
+
*/
|
|
33
|
+
function resolveKimiConfigDir() {
|
|
34
|
+
const envVal = process.env.KIMI_CODE_HOME;
|
|
35
|
+
if (envVal) {
|
|
36
|
+
if (envVal.startsWith("~")) return resolve(homedir(), envVal.replace(/^~[/\\]?/, ""));
|
|
37
|
+
return resolve(envVal);
|
|
38
|
+
}
|
|
39
|
+
return resolve(homedir(), ".kimi-code");
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Best-effort <sessionDir>/wire.jsonl resolution keyed by session id.
|
|
44
|
+
*
|
|
45
|
+
* NOTE / WIRE GAP: adapter-matrix/kimi.md confirms usage lands at
|
|
46
|
+
* <sessionDir>/wire.jsonl (agent/index.ts:142), but the exact session_id ->
|
|
47
|
+
* sessionDir directory layout is not carried in the hook stdin payload and the
|
|
48
|
+
* kimi-code refs are not checked out in this worktree to pin it. We probe the
|
|
49
|
+
* documented candidate layouts and return the first whose wire.jsonl exists,
|
|
50
|
+
* else null -> the cost block degrades to a no-op rather than guessing wrong.
|
|
51
|
+
*/
|
|
52
|
+
function resolveKimiWireJsonlPath(sessionId) {
|
|
53
|
+
if (typeof sessionId !== "string" || sessionId.length === 0) return null;
|
|
54
|
+
const configDir = resolveKimiConfigDir();
|
|
55
|
+
const candidates = [
|
|
56
|
+
join(configDir, "sessions", sessionId, "wire.jsonl"),
|
|
57
|
+
join(configDir, "agents", sessionId, "wire.jsonl"),
|
|
58
|
+
join(configDir, sessionId, "wire.jsonl"),
|
|
59
|
+
];
|
|
60
|
+
for (const candidate of candidates) {
|
|
61
|
+
try { if (existsSync(candidate)) return candidate; } catch { /* try next */ }
|
|
62
|
+
}
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
try {
|
|
67
|
+
const raw = await readStdin();
|
|
68
|
+
const input = parseStdin(raw);
|
|
69
|
+
const projectDir = getInputProjectDir(input, OPTS);
|
|
70
|
+
|
|
71
|
+
const { SessionDB } = await loadSessionDB();
|
|
72
|
+
const dbPath = getSessionDBPath(OPTS, projectDir);
|
|
73
|
+
const db = new SessionDB({ dbPath });
|
|
74
|
+
const sessionId = getSessionId(input, OPTS);
|
|
75
|
+
|
|
76
|
+
db.ensureSession(sessionId, projectDir);
|
|
77
|
+
// SessionEvent contract (src/types.ts:33-47) requires `type`, `category`,
|
|
78
|
+
// `data`, `priority`. SessionDB.insertEvent hashes `event.data` for the
|
|
79
|
+
// dedup key — passing `undefined` throws inside the wrapping try and the
|
|
80
|
+
// row silently never lands. Encode the turn snapshot into `data` so the
|
|
81
|
+
// hash is stable and the row actually persists.
|
|
82
|
+
const payload = {
|
|
83
|
+
stop_hook_active: input.stop_hook_active ?? false,
|
|
84
|
+
last_assistant_message: typeof input.last_assistant_message === "string"
|
|
85
|
+
? input.last_assistant_message.slice(0, 2000)
|
|
86
|
+
: null,
|
|
87
|
+
};
|
|
88
|
+
db.insertEvent(sessionId, {
|
|
89
|
+
type: "turn_end",
|
|
90
|
+
category: "session",
|
|
91
|
+
data: JSON.stringify(payload),
|
|
92
|
+
priority: 1,
|
|
93
|
+
}, "Stop");
|
|
94
|
+
|
|
95
|
+
// ─── kimi-code per-turn cost capture (cursor-gated, no double-count) ───
|
|
96
|
+
// Usage lives ONLY on <sessionDir>/wire.jsonl `usage.record` lines, never in
|
|
97
|
+
// hook stdin (adapter-matrix/kimi.md). Tail the file, sum NEW delta lines
|
|
98
|
+
// since a per-session high-water cursor, and forward. Best-effort — a missing
|
|
99
|
+
// wire file or read/extract failure must never block the turn_end write or
|
|
100
|
+
// the session, so the whole block is wrapped and swallowed.
|
|
101
|
+
try {
|
|
102
|
+
const wirePath = resolveKimiWireJsonlPath(sessionId);
|
|
103
|
+
if (wirePath) {
|
|
104
|
+
let wireText = null;
|
|
105
|
+
try {
|
|
106
|
+
wireText = readFileSync(wirePath, "utf-8");
|
|
107
|
+
} catch {
|
|
108
|
+
// unreadable/missing wire.jsonl — skip capture this turn.
|
|
109
|
+
}
|
|
110
|
+
if (wireText) {
|
|
111
|
+
const { extractKimiUsageSince } = await loadExtract();
|
|
112
|
+
const { resolveProjectAttributions } = await loadProjectAttribution();
|
|
113
|
+
const cursor = db.getUsageCursor(sessionId);
|
|
114
|
+
const { events, cursor: next } = extractKimiUsageSince(wireText, cursor);
|
|
115
|
+
if (events.length > 0) {
|
|
116
|
+
// attributeAndInsertEvents both INSERTS locally and FORWARDS to the
|
|
117
|
+
// platform (gated on ~/.context-mode/platform.json).
|
|
118
|
+
attributeAndInsertEvents(db, sessionId, events, input, projectDir, "Stop", resolveProjectAttributions);
|
|
119
|
+
}
|
|
120
|
+
if (next) db.setUsageCursor(sessionId, next);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
} catch {
|
|
124
|
+
// Best-effort cost capture — never block the session on failure.
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
db.close();
|
|
128
|
+
} catch {
|
|
129
|
+
// Kimi Code hooks must not block the session.
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
process.stdout.write("{}\n");
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import "./platform.mjs";
|
|
3
|
+
import "../suppress-stderr.mjs";
|
|
4
|
+
import "../ensure-deps.mjs";
|
|
5
|
+
/**
|
|
6
|
+
* Kimi Code CLI UserPromptSubmit hook — capture user prompts for continuity.
|
|
7
|
+
*
|
|
8
|
+
* Kimi Code sends `prompt` as a ContentPart[] array. We normalize it to
|
|
9
|
+
* a single string so downstream extractors work correctly.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { readStdin, parseStdin, getSessionId, getSessionDBPath, getInputProjectDir, KIMI_OPTS } from "../session-helpers.mjs";
|
|
13
|
+
import { createSessionLoaders, attributeAndInsertEvents } from "../session-loaders.mjs";
|
|
14
|
+
import { dirname } from "node:path";
|
|
15
|
+
import { fileURLToPath } from "node:url";
|
|
16
|
+
|
|
17
|
+
const HOOK_DIR = dirname(fileURLToPath(import.meta.url));
|
|
18
|
+
const { loadSessionDB, loadExtract, loadProjectAttribution } = createSessionLoaders(HOOK_DIR);
|
|
19
|
+
const OPTS = KIMI_OPTS;
|
|
20
|
+
|
|
21
|
+
function extractPromptText(input) {
|
|
22
|
+
const raw = input.prompt ?? input.message ?? "";
|
|
23
|
+
if (Array.isArray(raw)) {
|
|
24
|
+
// ContentPart[] — Kimi Code sends { type: "text", text: "..." } objects
|
|
25
|
+
return raw
|
|
26
|
+
.filter((p) => p && (p.type === "text" || typeof p.text === "string"))
|
|
27
|
+
.map((p) => p.text)
|
|
28
|
+
.join("\n");
|
|
29
|
+
}
|
|
30
|
+
return String(raw);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
try {
|
|
34
|
+
const raw = await readStdin();
|
|
35
|
+
const input = parseStdin(raw);
|
|
36
|
+
const projectDir = getInputProjectDir(input, OPTS);
|
|
37
|
+
|
|
38
|
+
const prompt = extractPromptText(input);
|
|
39
|
+
const trimmed = (prompt || "").trim();
|
|
40
|
+
|
|
41
|
+
const isSystemMessage = trimmed.startsWith("<task-notification>")
|
|
42
|
+
|| trimmed.startsWith("<system-reminder>")
|
|
43
|
+
|| trimmed.startsWith("<context_guidance>")
|
|
44
|
+
|| trimmed.startsWith("<tool-result>");
|
|
45
|
+
|
|
46
|
+
if (trimmed.length > 0 && !isSystemMessage) {
|
|
47
|
+
const { SessionDB } = await loadSessionDB();
|
|
48
|
+
const { extractUserEvents } = await loadExtract();
|
|
49
|
+
const { resolveProjectAttributions } = await loadProjectAttribution();
|
|
50
|
+
const dbPath = getSessionDBPath(OPTS, projectDir);
|
|
51
|
+
const db = new SessionDB({ dbPath });
|
|
52
|
+
const sessionId = getSessionId(input, OPTS);
|
|
53
|
+
|
|
54
|
+
db.ensureSession(sessionId, projectDir);
|
|
55
|
+
|
|
56
|
+
const promptEvent = {
|
|
57
|
+
type: "user_prompt",
|
|
58
|
+
category: "user-prompt",
|
|
59
|
+
data: prompt,
|
|
60
|
+
priority: 1,
|
|
61
|
+
};
|
|
62
|
+
const promptAttributions = attributeAndInsertEvents(
|
|
63
|
+
db, sessionId, [promptEvent], input, projectDir, "UserPromptSubmit", resolveProjectAttributions,
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
const userEvents = extractUserEvents(trimmed);
|
|
67
|
+
const savedLastKnown = promptAttributions[0]?.projectDir || null;
|
|
68
|
+
const sessionStats = db.getSessionStats(sessionId);
|
|
69
|
+
const lastKnownProjectDir = typeof db.getLatestAttributedProjectDir === "function"
|
|
70
|
+
? db.getLatestAttributedProjectDir(sessionId)
|
|
71
|
+
: null;
|
|
72
|
+
const userAttributions = resolveProjectAttributions(userEvents, {
|
|
73
|
+
sessionOriginDir: sessionStats?.project_dir || projectDir,
|
|
74
|
+
inputProjectDir: projectDir,
|
|
75
|
+
workspaceRoots: Array.isArray(input.workspace_roots) ? input.workspace_roots : [],
|
|
76
|
+
lastKnownProjectDir: savedLastKnown || lastKnownProjectDir,
|
|
77
|
+
});
|
|
78
|
+
for (let i = 0; i < userEvents.length; i++) {
|
|
79
|
+
db.insertEvent(sessionId, userEvents[i], "UserPromptSubmit", userAttributions[i]);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
db.close();
|
|
83
|
+
}
|
|
84
|
+
} catch {
|
|
85
|
+
// Kimi Code hooks must not block the session.
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
process.stdout.write(JSON.stringify({
|
|
89
|
+
hookSpecificOutput: { hookEventName: "UserPromptSubmit", additionalContext: "" },
|
|
90
|
+
}) + "\n");
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import "../suppress-stderr.mjs";
|
|
3
|
+
import "../ensure-deps.mjs";
|
|
4
|
+
/**
|
|
5
|
+
* Kiro CLI agentSpawn hook for context-mode.
|
|
6
|
+
*
|
|
7
|
+
* agentSpawn is the Kiro semantic equivalent of Claude Code's SessionStart:
|
|
8
|
+
* fires once when an agent loads. We use it to inject the routing block and
|
|
9
|
+
* (on resume/compact) the session-resume directive.
|
|
10
|
+
*
|
|
11
|
+
* Source: https://kiro.dev/docs/cli/hooks
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { createRoutingBlock } from "../routing-block.mjs";
|
|
15
|
+
import { createToolNamer } from "../core/tool-naming.mjs";
|
|
16
|
+
|
|
17
|
+
const toolNamer = createToolNamer("kiro");
|
|
18
|
+
const ROUTING_BLOCK = createRoutingBlock(toolNamer);
|
|
19
|
+
import {
|
|
20
|
+
writeSessionEventsFile,
|
|
21
|
+
buildSessionDirective,
|
|
22
|
+
getSessionEvents,
|
|
23
|
+
} from "../session-directive.mjs";
|
|
24
|
+
import {
|
|
25
|
+
readStdin,
|
|
26
|
+
parseStdin,
|
|
27
|
+
getSessionId,
|
|
28
|
+
getSessionDBPath,
|
|
29
|
+
getSessionEventsPath,
|
|
30
|
+
getCleanupFlagPath,
|
|
31
|
+
getInputProjectDir,
|
|
32
|
+
KIRO_OPTS,
|
|
33
|
+
} from "../session-helpers.mjs";
|
|
34
|
+
import { createSessionLoaders } from "../session-loaders.mjs";
|
|
35
|
+
import { unlinkSync } from "node:fs";
|
|
36
|
+
import { fileURLToPath } from "node:url";
|
|
37
|
+
|
|
38
|
+
const HOOK_DIR = fileURLToPath(new URL(".", import.meta.url));
|
|
39
|
+
const { loadSessionDB } = createSessionLoaders(HOOK_DIR);
|
|
40
|
+
const OPTS = KIRO_OPTS;
|
|
41
|
+
|
|
42
|
+
let additionalContext = ROUTING_BLOCK;
|
|
43
|
+
|
|
44
|
+
try {
|
|
45
|
+
const raw = await readStdin();
|
|
46
|
+
const input = parseStdin(raw);
|
|
47
|
+
// Kiro stdin shape mirrors codex/CC: { source, cwd, ... }. Default startup
|
|
48
|
+
// when no source provided (single-shot agent spawn).
|
|
49
|
+
const source = input.source ?? "startup";
|
|
50
|
+
const projectDir = getInputProjectDir(input, OPTS);
|
|
51
|
+
|
|
52
|
+
if (source === "compact" || source === "resume") {
|
|
53
|
+
const { SessionDB } = await loadSessionDB();
|
|
54
|
+
const dbPath = getSessionDBPath(OPTS, projectDir);
|
|
55
|
+
const db = new SessionDB({ dbPath });
|
|
56
|
+
|
|
57
|
+
if (source === "compact") {
|
|
58
|
+
const sessionId = getSessionId(input, OPTS);
|
|
59
|
+
const resume = db.getResume(sessionId);
|
|
60
|
+
if (resume && !resume.consumed) {
|
|
61
|
+
db.markResumeConsumed(sessionId);
|
|
62
|
+
}
|
|
63
|
+
} else {
|
|
64
|
+
try { unlinkSync(getCleanupFlagPath(OPTS, projectDir)); } catch { /* no flag */ }
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const sessionId = getSessionId(input, OPTS);
|
|
68
|
+
const events = sessionId ? getSessionEvents(db, sessionId) : [];
|
|
69
|
+
if (events.length > 0) {
|
|
70
|
+
const eventMeta = writeSessionEventsFile(events, getSessionEventsPath(OPTS, projectDir));
|
|
71
|
+
additionalContext += buildSessionDirective(source, eventMeta, toolNamer);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
db.close();
|
|
75
|
+
} else if (source === "startup") {
|
|
76
|
+
const { SessionDB } = await loadSessionDB();
|
|
77
|
+
const dbPath = getSessionDBPath(OPTS, projectDir);
|
|
78
|
+
const db = new SessionDB({ dbPath });
|
|
79
|
+
try { unlinkSync(getSessionEventsPath(OPTS, projectDir)); } catch { /* no stale file */ }
|
|
80
|
+
|
|
81
|
+
db.cleanupOldSessions(7);
|
|
82
|
+
db.db.exec(`DELETE FROM session_events WHERE session_id NOT IN (SELECT session_id FROM session_meta)`);
|
|
83
|
+
|
|
84
|
+
const sessionId = getSessionId(input, OPTS);
|
|
85
|
+
db.ensureSession(sessionId, projectDir);
|
|
86
|
+
|
|
87
|
+
db.close();
|
|
88
|
+
}
|
|
89
|
+
// clear => routing block only
|
|
90
|
+
} catch {
|
|
91
|
+
// Swallow errors — hook must not fail
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Kiro CLI agentSpawn JSON output shape mirrors CC SessionStart.
|
|
95
|
+
process.stdout.write(JSON.stringify({
|
|
96
|
+
hookSpecificOutput: { hookEventName: "agentSpawn", additionalContext },
|
|
97
|
+
}) + "\n");
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import "../suppress-stderr.mjs";
|
|
3
|
+
import "../ensure-deps.mjs";
|
|
4
|
+
/**
|
|
5
|
+
* Kiro CLI PostToolUse hook — session event capture.
|
|
6
|
+
* Must be fast (<20ms). No network, no LLM, just SQLite writes.
|
|
7
|
+
*
|
|
8
|
+
* Source: https://kiro.dev/docs/cli/hooks/
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { readStdin, parseStdin, getSessionId, getSessionDBPath, getInputProjectDir, KIRO_OPTS } from "../session-helpers.mjs";
|
|
12
|
+
import { createSessionLoaders, attributeAndInsertEvents } from "../session-loaders.mjs";
|
|
13
|
+
import { dirname } from "node:path";
|
|
14
|
+
import { fileURLToPath } from "node:url";
|
|
15
|
+
|
|
16
|
+
const HOOK_DIR = dirname(fileURLToPath(import.meta.url));
|
|
17
|
+
const { loadSessionDB, loadExtract, loadProjectAttribution } = createSessionLoaders(HOOK_DIR);
|
|
18
|
+
const OPTS = KIRO_OPTS;
|
|
19
|
+
|
|
20
|
+
try {
|
|
21
|
+
const raw = await readStdin();
|
|
22
|
+
const input = parseStdin(raw);
|
|
23
|
+
|
|
24
|
+
const { extractEvents } = await loadExtract();
|
|
25
|
+
const { resolveProjectAttributions } = await loadProjectAttribution();
|
|
26
|
+
const { SessionDB } = await loadSessionDB();
|
|
27
|
+
|
|
28
|
+
const sessionId = getSessionId(input, OPTS);
|
|
29
|
+
const projectDir = getInputProjectDir(input, OPTS);
|
|
30
|
+
const dbPath = getSessionDBPath(OPTS, projectDir);
|
|
31
|
+
const db = new SessionDB({ dbPath });
|
|
32
|
+
|
|
33
|
+
db.ensureSession(sessionId, projectDir);
|
|
34
|
+
|
|
35
|
+
const events = extractEvents({
|
|
36
|
+
tool_name: input.tool_name,
|
|
37
|
+
tool_input: input.tool_input ?? {},
|
|
38
|
+
tool_response: typeof input.tool_response === "string"
|
|
39
|
+
? input.tool_response
|
|
40
|
+
: JSON.stringify(input.tool_response ?? ""),
|
|
41
|
+
tool_output: input.tool_output,
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
attributeAndInsertEvents(db, sessionId, events, input, projectDir, "PostToolUse", resolveProjectAttributions);
|
|
45
|
+
|
|
46
|
+
db.close();
|
|
47
|
+
} catch {
|
|
48
|
+
// Non-blocking — swallow errors silently
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// PostToolUse is non-blocking — no stdout output
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import "../suppress-stderr.mjs";
|
|
3
|
+
/**
|
|
4
|
+
* Kiro CLI PreToolUse hook for context-mode.
|
|
5
|
+
* Uses exit codes instead of JSON stdout:
|
|
6
|
+
* - Exit 0: allow (stdout → agent context)
|
|
7
|
+
* - Exit 2: block (stderr → agent error)
|
|
8
|
+
*
|
|
9
|
+
* Source: https://kiro.dev/docs/cli/hooks/
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { dirname, resolve } from "node:path";
|
|
13
|
+
import { fileURLToPath } from "node:url";
|
|
14
|
+
import { readStdin } from "../core/stdin.mjs";
|
|
15
|
+
import { routePreToolUse, initSecurity } from "../core/routing.mjs";
|
|
16
|
+
import { parseStdin, getSessionId, KIRO_OPTS } from "../session-helpers.mjs";
|
|
17
|
+
|
|
18
|
+
const __hookDir = dirname(fileURLToPath(import.meta.url));
|
|
19
|
+
await initSecurity(resolve(__hookDir, "..", "..", "build"));
|
|
20
|
+
|
|
21
|
+
const raw = await readStdin();
|
|
22
|
+
const input = parseStdin(raw);
|
|
23
|
+
// Kiro stdin: { hook_event_name, cwd, tool_name, tool_input }
|
|
24
|
+
const tool = input.tool_name ?? "";
|
|
25
|
+
const toolInput = input.tool_input ?? {};
|
|
26
|
+
const projectDir = input.cwd ?? process.cwd();
|
|
27
|
+
|
|
28
|
+
const decision = routePreToolUse(tool, toolInput, projectDir, "kiro", getSessionId(input, KIRO_OPTS));
|
|
29
|
+
|
|
30
|
+
if (!decision) process.exit(0);
|
|
31
|
+
|
|
32
|
+
switch (decision.action) {
|
|
33
|
+
case "deny":
|
|
34
|
+
process.stderr.write(decision.reason ?? "Blocked by context-mode");
|
|
35
|
+
process.exit(2);
|
|
36
|
+
break;
|
|
37
|
+
|
|
38
|
+
case "modify":
|
|
39
|
+
// Kiro CLI cannot modify tool input — deny with redirect message
|
|
40
|
+
// The updatedInput.command contains an echo "..." wrapper — extract inner message
|
|
41
|
+
if (typeof decision.updatedInput?.command === "string") {
|
|
42
|
+
const msg = decision.updatedInput.command
|
|
43
|
+
.replace(/^echo\s+"?/, "")
|
|
44
|
+
.replace(/"?\s*$/, "");
|
|
45
|
+
process.stderr.write(msg);
|
|
46
|
+
} else {
|
|
47
|
+
process.stderr.write(decision.reason ?? "Blocked by context-mode routing");
|
|
48
|
+
}
|
|
49
|
+
process.exit(2);
|
|
50
|
+
break;
|
|
51
|
+
|
|
52
|
+
case "context":
|
|
53
|
+
process.stdout.write(decision.additionalContext ?? "");
|
|
54
|
+
process.exit(0);
|
|
55
|
+
break;
|
|
56
|
+
|
|
57
|
+
case "ask":
|
|
58
|
+
// Kiro CLI has no "ask" concept — passthrough
|
|
59
|
+
process.exit(0);
|
|
60
|
+
break;
|
|
61
|
+
|
|
62
|
+
default:
|
|
63
|
+
process.exit(0);
|
|
64
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import "../suppress-stderr.mjs";
|
|
3
|
+
import "../ensure-deps.mjs";
|
|
4
|
+
/**
|
|
5
|
+
* Kiro CLI UserPromptSubmit hook — capture user prompts for continuity.
|
|
6
|
+
*
|
|
7
|
+
* Mirrors hooks/codex/userpromptsubmit.mjs (same json-stdio paradigm).
|
|
8
|
+
* Kiro stdin: { hook_event_name: "userPromptSubmit", cwd, prompt }
|
|
9
|
+
*
|
|
10
|
+
* Source: https://kiro.dev/docs/cli/hooks
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import {
|
|
14
|
+
readStdin,
|
|
15
|
+
parseStdin,
|
|
16
|
+
getSessionId,
|
|
17
|
+
getSessionDBPath,
|
|
18
|
+
getInputProjectDir,
|
|
19
|
+
KIRO_OPTS,
|
|
20
|
+
} from "../session-helpers.mjs";
|
|
21
|
+
import { createSessionLoaders, attributeAndInsertEvents } from "../session-loaders.mjs";
|
|
22
|
+
import { dirname } from "node:path";
|
|
23
|
+
import { fileURLToPath } from "node:url";
|
|
24
|
+
|
|
25
|
+
const HOOK_DIR = dirname(fileURLToPath(import.meta.url));
|
|
26
|
+
const { loadSessionDB, loadExtract, loadProjectAttribution } = createSessionLoaders(HOOK_DIR);
|
|
27
|
+
const OPTS = KIRO_OPTS;
|
|
28
|
+
|
|
29
|
+
try {
|
|
30
|
+
const raw = await readStdin();
|
|
31
|
+
const input = parseStdin(raw);
|
|
32
|
+
const projectDir = getInputProjectDir(input, OPTS);
|
|
33
|
+
|
|
34
|
+
const prompt = input.prompt ?? input.message ?? "";
|
|
35
|
+
const trimmed = (prompt || "").trim();
|
|
36
|
+
|
|
37
|
+
const isSystemMessage = trimmed.startsWith("<task-notification>")
|
|
38
|
+
|| trimmed.startsWith("<system-reminder>")
|
|
39
|
+
|| trimmed.startsWith("<context_guidance>")
|
|
40
|
+
|| trimmed.startsWith("<tool-result>");
|
|
41
|
+
|
|
42
|
+
if (trimmed.length > 0 && !isSystemMessage) {
|
|
43
|
+
const { SessionDB } = await loadSessionDB();
|
|
44
|
+
const { extractUserEvents } = await loadExtract();
|
|
45
|
+
const { resolveProjectAttributions } = await loadProjectAttribution();
|
|
46
|
+
const dbPath = getSessionDBPath(OPTS, projectDir);
|
|
47
|
+
const db = new SessionDB({ dbPath });
|
|
48
|
+
const sessionId = getSessionId(input, OPTS);
|
|
49
|
+
|
|
50
|
+
db.ensureSession(sessionId, projectDir);
|
|
51
|
+
|
|
52
|
+
const promptEvent = {
|
|
53
|
+
type: "user_prompt",
|
|
54
|
+
category: "user-prompt",
|
|
55
|
+
data: prompt,
|
|
56
|
+
priority: 1,
|
|
57
|
+
};
|
|
58
|
+
const promptAttributions = attributeAndInsertEvents(
|
|
59
|
+
db, sessionId, [promptEvent], input, projectDir, "UserPromptSubmit", resolveProjectAttributions,
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
const userEvents = extractUserEvents(trimmed);
|
|
63
|
+
const savedLastKnown = promptAttributions[0]?.projectDir || null;
|
|
64
|
+
const sessionStats = db.getSessionStats(sessionId);
|
|
65
|
+
const lastKnownProjectDir = typeof db.getLatestAttributedProjectDir === "function"
|
|
66
|
+
? db.getLatestAttributedProjectDir(sessionId)
|
|
67
|
+
: null;
|
|
68
|
+
const userAttributions = resolveProjectAttributions(userEvents, {
|
|
69
|
+
sessionOriginDir: sessionStats?.project_dir || projectDir,
|
|
70
|
+
inputProjectDir: projectDir,
|
|
71
|
+
workspaceRoots: Array.isArray(input.workspace_roots) ? input.workspace_roots : [],
|
|
72
|
+
lastKnownProjectDir: savedLastKnown || lastKnownProjectDir,
|
|
73
|
+
});
|
|
74
|
+
for (let i = 0; i < userEvents.length; i++) {
|
|
75
|
+
db.insertEvent(sessionId, userEvents[i], "UserPromptSubmit", userAttributions[i]);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
db.close();
|
|
79
|
+
}
|
|
80
|
+
} catch {
|
|
81
|
+
// Kiro hooks must not block the session.
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Kiro CLI userPromptSubmit accepts JSON output for additionalContext (see docs).
|
|
85
|
+
// We don't inject context here — that's the agentSpawn hook's job.
|
|
86
|
+
process.stdout.write(JSON.stringify({
|
|
87
|
+
hookSpecificOutput: { hookEventName: "userPromptSubmit", additionalContext: "" },
|
|
88
|
+
}) + "\n");
|