MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / AtomicHooksPlugin

Function AtomicHooksPlugin

packages/opencode-atomic-hooks/src/index.ts:59–794  ·  view source on GitHub ↗
({ $, directory })

Source from the content-addressed store, hash-verified

57}
58
59export const AtomicHooksPlugin: Plugin = async ({ $, directory }) => {
60 const ATOMIC = "atomic";
61 const atomicDir = join(directory, ".atomic");
62 const logFile = join(atomicDir, "plugin.log");
63 const debugFile = join(atomicDir, "plugin-debug.log");
64 try {
65 mkdirSync(atomicDir, { recursive: true });
66 } catch {}
67
68 // Set of messageIDs that belong to user messages (from chat.message hook).
69 // Used to distinguish user text parts from assistant text parts in the event stream.
70 const userMessageIDs = new Set<string>();
71
72 const log = (msg: string) => {
73 const line = `${new Date().toISOString()} ${msg}\n`;
74 try {
75 appendFileSync(logFile, line);
76 } catch {}
77 };
78
79 // DEBUG: Separate debug log with full object inspection
80 const debug = (label: string, data?: unknown) => {
81 const ts = new Date().toISOString();
82 const body = data !== undefined ? `\n${inspect(data)}` : "";
83 try {
84 appendFileSync(debugFile, `${ts} [${label}]${body}\n---\n`);
85 } catch {}
86 };
87
88 const hook = async (
89 sessionID: string,
90 verb: string,
91 extra: Record<string, unknown> = {},
92 ) => {
93 const payload = JSON.stringify({
94 session_id: sessionID,
95 cwd: directory,
96 timestamp: new Date().toISOString(),
97 ...extra,
98 });
99 try {
100 const result =
101 await $`echo ${payload} | ${ATOMIC} agent hooks opencode ${verb}`
102 .cwd(directory)
103 .quiet()
104 .nothrow();
105 const ok = result.exitCode === 0;
106 const stderr = result.stderr.toString().trim();
107 log(
108 `${verb} session=${sessionID} exit=${result.exitCode}${stderr ? " stderr=" + stderr : ""}`,
109 );
110 return ok;
111 } catch (err) {
112 log(`${verb} session=${sessionID} error=${err}`);
113 return false;
114 }
115 };
116

Callers

nothing calls this directly

Calls 14

createShellHandlerFunction · 0.90
debugFunction · 0.85
ensureFunction · 0.85
recordTurnFunction · 0.85
logFunction · 0.70
hookFunction · 0.70
clearMethod · 0.65
hasMethod · 0.65
getMethod · 0.65
joinFunction · 0.50
deleteMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected