@kealthas-dev/opencode-hook-logger 1.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.
Files changed (2) hide show
  1. package/hook-logger.ts +58 -0
  2. package/package.json +20 -0
package/hook-logger.ts ADDED
@@ -0,0 +1,58 @@
1
+ import { appendFile, mkdir } from "node:fs/promises";
2
+ import { existsSync } from "node:fs";
3
+ import { join } from "node:path";
4
+ import { homedir } from "node:os";
5
+ import type { Plugin } from "@opencode-ai/plugin";
6
+
7
+ const outDir = join(homedir(), "opencode-hook-output");
8
+
9
+ function safeStringify(value: unknown): string {
10
+ const seen = new WeakSet<object>();
11
+ return JSON.stringify(value, (_key, val) => {
12
+ if (typeof val === "object" && val !== null) {
13
+ if (seen.has(val)) return "[Circular]";
14
+ seen.add(val);
15
+ }
16
+ if (typeof val === "bigint") return val.toString();
17
+ return val;
18
+ });
19
+ }
20
+
21
+ async function logEvent(name: string, payload: Record<string, unknown>) {
22
+ const file = join(outDir, `${name}.jsonl`);
23
+ const line = safeStringify({ ts: new Date().toISOString(), ...payload }) + "\n";
24
+ try {
25
+ if (!existsSync(outDir)) await mkdir(outDir, { recursive: true });
26
+ await appendFile(file, line);
27
+ } catch (e) {
28
+ // silently fail, never block opencode over logging
29
+ }
30
+ }
31
+
32
+ export const HookLogger: Plugin = async () => {
33
+ return {
34
+ event: async ({ event }) => logEvent("event", { event }),
35
+ config: async (input) => logEvent("config", { input }),
36
+ "chat.message": async (input, output) => logEvent("chat.message", { input, output }),
37
+ "chat.params": async (input, output) => logEvent("chat.params", { input, output }),
38
+ "chat.headers": async (input, output) => logEvent("chat.headers", { input, output }),
39
+ "permission.ask": async (input, output) => logEvent("permission.ask", { input, output }),
40
+ "command.execute.before": async (input, output) => logEvent("command.execute.before", { input, output }),
41
+ "tool.execute.before": async (input, output) => logEvent("tool.execute.before", { input, output }),
42
+ "tool.execute.after": async (input, output) => logEvent("tool.execute.after", { input, output }),
43
+ "shell.env": async (input, output) => logEvent("shell.env", { input, output }),
44
+ "experimental.chat.messages.transform": async (input, output) =>
45
+ logEvent("experimental.chat.messages.transform", { input, output }),
46
+ "experimental.chat.system.transform": async (input, output) =>
47
+ logEvent("experimental.chat.system.transform", { input, output }),
48
+ "experimental.provider.small_model": async (input, output) =>
49
+ logEvent("experimental.provider.small_model", { input, output }),
50
+ "experimental.session.compacting": async (input, output) =>
51
+ logEvent("experimental.session.compacting", { input, output }),
52
+ "experimental.compaction.autocontinue": async (input, output) =>
53
+ logEvent("experimental.compaction.autocontinue", { input, output }),
54
+ "experimental.text.complete": async (input, output) =>
55
+ logEvent("experimental.text.complete", { input, output }),
56
+ "tool.definition": async (input, output) => logEvent("tool.definition", { input, output }),
57
+ };
58
+ };
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "@kealthas-dev/opencode-hook-logger",
3
+ "version": "1.0.0",
4
+ "type": "module",
5
+ "description": "hook-logger.ts opencode plugin (logs essentially every opencode hook event as JSONL under ~/opencode-hook-output/, for debugging/observability), published to npm so opencode's own npm-plugin loader can install it directly from a bare package name in opencode.json's plugin array - see CLAUDE.md. Written in TypeScript against @opencode-ai/plugin's Plugin type. Independently installable from llm-review-gate.ts - not bundled together.",
6
+ "main": "hook-logger.ts",
7
+ "files": ["hook-logger.ts"],
8
+ "publishConfig": {
9
+ "access": "public"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "https://github.com/Keal-thas/opencode-toolkit.git",
14
+ "directory": "plugins/hook-logger"
15
+ },
16
+ "devDependencies": {
17
+ "@opencode-ai/plugin": "^1.0.0",
18
+ "@types/node": "^22.0.0"
19
+ }
20
+ }