-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathchainloop-trace.ts
More file actions
52 lines (48 loc) · 1.9 KB
/
Copy pathchainloop-trace.ts
File metadata and controls
52 lines (48 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import type { Plugin } from "@opencode-ai/plugin"
export const ChainloopTrace: Plugin = async ({ $ }) => {
// The commit-msg hook links sessions to commits by cross-referencing
// staged files against AI line attributions recorded by post-tool-use.
// If no file-writing tools (edit, write, apply_patch) are invoked during
// the session, there will be no attributions and the commit will not be
// marked as AI-assisted.
const fileWritingTools = ["edit","write","apply_patch"]
function filePathFromArgs(args: any): string {
if (args?.filePath) return args.filePath
if (args?.path) return args.path
return ""
}
async function fire(event: string, payload: Record<string, any>) {
const json = JSON.stringify(payload)
await $`echo ${json} | chainloop trace hook opencode ${event}`
}
return {
event: async ({ event }) => {
if (event.type === "session.created") {
const sessionID = event.properties?.info?.id ?? ""
await fire("session-start", { session_id: sessionID, hook_event_name: "session.created" })
}
if (event.type === "session.deleted") {
const sessionID = event.properties?.info?.id ?? ""
await fire("session-end", { session_id: sessionID, hook_event_name: "session.deleted" })
}
},
"tool.execute.before": async (input, output) => {
if (!fileWritingTools.includes(input.tool)) return
await fire("pre-tool-use", {
session_id: input.sessionID,
hook_event_name: "tool.execute.before",
tool_name: input.tool,
file_path: filePathFromArgs(output.args),
})
},
"tool.execute.after": async (input) => {
if (!fileWritingTools.includes(input.tool)) return
await fire("post-tool-use", {
session_id: input.sessionID,
hook_event_name: "tool.execute.after",
tool_name: input.tool,
file_path: filePathFromArgs(input.args),
})
},
}
}