opencode-codegraph 0.1.2 → 0.1.3

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 (3) hide show
  1. package/package.json +1 -1
  2. package/src/index.ts +50 -20
  3. package/src/util.ts +89 -8
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-codegraph",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "OpenCode plugin for CodeGraph CPG-powered code analysis",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
package/src/index.ts CHANGED
@@ -10,11 +10,17 @@
10
10
  * - Toast notifications on CPG update completion
11
11
  */
12
12
 
13
- import type { Plugin } from "@opencode-ai/plugin"
14
- import { tool } from "@opencode-ai/plugin/tool"
15
-
16
- import { CodeGraphAPI } from "./api"
17
- import { extractFileRefs, isGitCommit } from "./util"
13
+ import type { Plugin } from "@opencode-ai/plugin"
14
+ import { tool } from "@opencode-ai/plugin/tool"
15
+
16
+ import { CodeGraphAPI } from "./api"
17
+ import {
18
+ extractFileRefs,
19
+ formatReviewTraceSummary,
20
+ getHeadCommit,
21
+ isGitCommit,
22
+ readReviewTraceSnapshot,
23
+ } from "./util"
18
24
 
19
25
  const codegraphPlugin: Plugin = async (input) => {
20
26
  const { client, directory, $ } = input
@@ -66,21 +72,45 @@ const codegraphPlugin: Plugin = async (input) => {
66
72
  // -----------------------------------------------------------------
67
73
  // 3. Post-commit: trigger incremental CPG update
68
74
  // -----------------------------------------------------------------
69
- "tool.execute.after": async (inp, output) => {
70
- if (inp.tool !== "bash") return
71
- if (!isGitCommit(output.output)) return
72
-
73
- try {
74
- await api.triggerIncrementalUpdate(projectId, directory)
75
- // Notify user via output metadata (visible in OpenCode UI)
76
- output.title = "CodeGraph: CPG update triggered"
77
- output.metadata = {
78
- ...output.metadata,
79
- codegraph_cpg_update: "triggered",
80
- }
81
- } catch {
82
- // Best-effort — don't break the workflow
83
- }
75
+ "tool.execute.after": async (inp, output) => {
76
+ if (inp.tool !== "bash") return
77
+ if (!isGitCommit(output.output)) return
78
+
79
+ try {
80
+ const commit = await getHeadCommit($)
81
+ await api.triggerIncrementalUpdate(projectId, directory)
82
+
83
+ let traceSummary: string | null = null
84
+ if (commit) {
85
+ const traceSnapshot = await readReviewTraceSnapshot(directory, commit)
86
+ if (traceSnapshot) {
87
+ traceSummary = formatReviewTraceSummary(traceSnapshot)
88
+ output.metadata = {
89
+ ...output.metadata,
90
+ codegraph_review_trace_status: traceSnapshot.status || "unknown",
91
+ codegraph_review_trace_phase: traceSnapshot.phase || "unknown",
92
+ codegraph_review_trace_findings: traceSnapshot.review_findings_count ?? null,
93
+ codegraph_review_trace_recommendations:
94
+ traceSnapshot.review_recommendations?.slice(0, 3) || [],
95
+ }
96
+ }
97
+ }
98
+
99
+ // Notify user via output metadata (visible in OpenCode UI)
100
+ output.title = traceSummary
101
+ ? "CodeGraph: CPG update triggered + review trace found"
102
+ : "CodeGraph: CPG update triggered"
103
+ output.metadata = {
104
+ ...output.metadata,
105
+ codegraph_cpg_update: "triggered",
106
+ }
107
+ if (traceSummary) {
108
+ const existingOutput = output.output?.trimEnd() || ""
109
+ output.output = existingOutput ? `${existingOutput}\n\n${traceSummary}` : traceSummary
110
+ }
111
+ } catch {
112
+ // Best-effort — don't break the workflow
113
+ }
84
114
  },
85
115
 
86
116
  // -----------------------------------------------------------------
package/src/util.ts CHANGED
@@ -1,6 +1,20 @@
1
- /**
2
- * Utility functions for the CodeGraph OpenCode plugin.
3
- */
1
+ import { readFile } from "node:fs/promises"
2
+ import path from "node:path"
3
+
4
+ /**
5
+ * Utility functions for the CodeGraph OpenCode plugin.
6
+ */
7
+
8
+ export type ReviewTraceSnapshot = {
9
+ commit?: string
10
+ status?: string
11
+ phase?: string
12
+ updated_at?: string
13
+ review_findings_count?: number
14
+ review_severity_counts?: Record<string, number>
15
+ review_recommendations?: string[]
16
+ error?: string | null
17
+ }
4
18
 
5
19
  /**
6
20
  * Extract file references from message parts.
@@ -43,7 +57,7 @@ export function extractFileRefs(parts: Array<{ type: string; text?: string }>):
43
57
  /**
44
58
  * Check if a shell command output indicates a git commit was made.
45
59
  */
46
- export function isGitCommit(output: string): boolean {
60
+ export function isGitCommit(output: string): boolean {
47
61
  if (!output) return false
48
62
 
49
63
  // Common git commit success patterns
@@ -52,10 +66,77 @@ export function isGitCommit(output: string): boolean {
52
66
  /^\s*create mode/m.test(output) || // create mode 100644
53
67
  /^\s*\d+ files? changed/m.test(output) // 3 files changed, 42 insertions
54
68
  )
55
- }
56
-
57
- // File extensions recognized as source code
58
- const SOURCE_EXTENSIONS = new Set([
69
+ }
70
+
71
+ export async function getHeadCommit($: any): Promise<string | null> {
72
+ try {
73
+ const sha = (await $`git rev-parse HEAD`.quiet().text()).trim()
74
+ return sha || null
75
+ } catch {
76
+ return null
77
+ }
78
+ }
79
+
80
+ export async function readReviewTraceSnapshot(
81
+ directory: string,
82
+ commit: string,
83
+ attempts = 4,
84
+ delayMs = 500,
85
+ ): Promise<ReviewTraceSnapshot | null> {
86
+ const statusPath = path.join(directory, "data", "reviews", `${commit}.status.json`)
87
+
88
+ for (let attempt = 0; attempt < attempts; attempt += 1) {
89
+ try {
90
+ const raw = await readFile(statusPath, "utf-8")
91
+ return JSON.parse(raw) as ReviewTraceSnapshot
92
+ } catch {
93
+ if (attempt === attempts - 1) return null
94
+ await new Promise((resolve) => setTimeout(resolve, delayMs))
95
+ }
96
+ }
97
+
98
+ return null
99
+ }
100
+
101
+ export function formatReviewTraceSummary(snapshot: ReviewTraceSnapshot): string | null {
102
+ const status = snapshot.status || "unknown"
103
+ const phase = snapshot.phase || "unknown"
104
+ const findingsCount = snapshot.review_findings_count
105
+ const recommendations = Array.isArray(snapshot.review_recommendations)
106
+ ? snapshot.review_recommendations.filter(Boolean)
107
+ : []
108
+
109
+ const lines = ["## CodeGraph Review Trace", "", `- Status: ${status}`, `- Phase: ${phase}`]
110
+
111
+ if (typeof findingsCount === "number") {
112
+ lines.push(`- Findings: ${findingsCount}`)
113
+ }
114
+
115
+ const severityCounts = snapshot.review_severity_counts || {}
116
+ const severitySummary = Object.entries(severityCounts)
117
+ .filter(([, count]) => typeof count === "number" && count > 0)
118
+ .map(([severity, count]) => `${severity}:${count}`)
119
+ .join(", ")
120
+ if (severitySummary) {
121
+ lines.push(`- Severity: ${severitySummary}`)
122
+ }
123
+
124
+ if (snapshot.error) {
125
+ lines.push(`- Error: ${snapshot.error}`)
126
+ }
127
+
128
+ if (recommendations.length) {
129
+ lines.push("", "### Recommendations")
130
+ for (const recommendation of recommendations.slice(0, 3)) {
131
+ lines.push(`- ${recommendation}`)
132
+ }
133
+ }
134
+
135
+ return lines.length > 2 ? lines.join("\n") : null
136
+ }
137
+
138
+ // File extensions recognized as source code
139
+ const SOURCE_EXTENSIONS = new Set([
59
140
  "py",
60
141
  "go",
61
142
  "js",