@tarquinen/opencode-dcp 3.2.3-beta0 → 3.2.5-beta0

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/lib/logger.ts DELETED
@@ -1,235 +0,0 @@
1
- import { writeFile, mkdir } from "fs/promises"
2
- import { join } from "path"
3
- import { existsSync } from "fs"
4
- import { homedir } from "os"
5
-
6
- export class Logger {
7
- private logDir: string
8
- private scope?: string
9
- public enabled: boolean
10
-
11
- constructor(enabled: boolean, scope?: string) {
12
- this.enabled = enabled
13
- this.scope = scope?.replace(/[^A-Za-z0-9._-]/g, "_")
14
- const configHome = process.env.XDG_CONFIG_HOME || join(homedir(), ".config")
15
- this.logDir = this.scope
16
- ? join(configHome, "opencode", "logs", "dcp", this.scope)
17
- : join(configHome, "opencode", "logs", "dcp")
18
- }
19
-
20
- private async ensureLogDir() {
21
- if (!existsSync(this.logDir)) {
22
- await mkdir(this.logDir, { recursive: true })
23
- }
24
- }
25
-
26
- private formatData(data?: any): string {
27
- if (!data) return ""
28
-
29
- const parts: string[] = []
30
- for (const [key, value] of Object.entries(data)) {
31
- if (value === undefined || value === null) continue
32
-
33
- // Format arrays compactly
34
- if (Array.isArray(value)) {
35
- if (value.length === 0) continue
36
- parts.push(
37
- `${key}=[${value.slice(0, 3).join(",")}${value.length > 3 ? `...+${value.length - 3}` : ""}]`,
38
- )
39
- } else if (typeof value === "object") {
40
- const str = JSON.stringify(value)
41
- if (str.length < 50) {
42
- parts.push(`${key}=${str}`)
43
- }
44
- } else {
45
- parts.push(`${key}=${value}`)
46
- }
47
- }
48
- return parts.join(" ")
49
- }
50
-
51
- private getCallerFile(skipFrames: number = 3): string {
52
- const originalPrepareStackTrace = Error.prepareStackTrace
53
- try {
54
- const err = new Error()
55
- Error.prepareStackTrace = (_, stack) => stack
56
- const stack = err.stack as unknown as NodeJS.CallSite[]
57
- Error.prepareStackTrace = originalPrepareStackTrace
58
-
59
- // Skip specified number of frames to get to actual caller
60
- for (let i = skipFrames; i < stack.length; i++) {
61
- const filename = stack[i]?.getFileName()
62
- if (filename && !filename.includes("/logger.")) {
63
- // Extract just the filename without path and extension
64
- const match = filename.match(/([^/\\]+)\.[tj]s$/)
65
- return match ? match[1] : filename
66
- }
67
- }
68
- return "unknown"
69
- } catch {
70
- return "unknown"
71
- }
72
- }
73
-
74
- private async write(level: string, component: string, message: string, data?: any) {
75
- if (!this.enabled) return
76
-
77
- try {
78
- await this.ensureLogDir()
79
-
80
- const timestamp = new Date().toISOString()
81
- const dataStr = this.formatData(data)
82
-
83
- const logLine = `${timestamp} ${level.padEnd(5)} ${component}: ${message}${dataStr ? " | " + dataStr : ""}\n`
84
-
85
- const logFile = this.scope
86
- ? join(this.logDir, `${new Date().toISOString().split("T")[0]}.log`)
87
- : join(this.logDir, "daily", `${new Date().toISOString().split("T")[0]}.log`)
88
-
89
- if (!this.scope) {
90
- const dailyLogDir = join(this.logDir, "daily")
91
- if (!existsSync(dailyLogDir)) {
92
- await mkdir(dailyLogDir, { recursive: true })
93
- }
94
- }
95
-
96
- await writeFile(logFile, logLine, { flag: "a" })
97
- } catch (error) {}
98
- }
99
-
100
- info(message: string, data?: any) {
101
- const component = this.getCallerFile(2)
102
- return this.write("INFO", component, message, data)
103
- }
104
-
105
- debug(message: string, data?: any) {
106
- const component = this.getCallerFile(2)
107
- return this.write("DEBUG", component, message, data)
108
- }
109
-
110
- warn(message: string, data?: any) {
111
- const component = this.getCallerFile(2)
112
- return this.write("WARN", component, message, data)
113
- }
114
-
115
- error(message: string, data?: any) {
116
- const component = this.getCallerFile(2)
117
- return this.write("ERROR", component, message, data)
118
- }
119
-
120
- /**
121
- * Strips unnecessary metadata from messages for cleaner debug logs.
122
- *
123
- * Removed:
124
- * - All IDs (id, sessionID, messageID, parentID)
125
- * - summary, path, cost, model, agent, mode, finish, providerID, modelID
126
- * - step-start and step-finish parts entirely
127
- * - snapshot fields
128
- * - ignored text parts
129
- *
130
- * Kept:
131
- * - role, time (created only), tokens (input, output, reasoning, cache)
132
- * - text, reasoning, tool parts with content
133
- * - tool calls with: tool, callID, input, output, metadata
134
- */
135
- private minimizeForDebug(messages: any[]): any[] {
136
- return messages.map((msg) => {
137
- const minimized: any = {
138
- role: msg.info?.role,
139
- }
140
-
141
- if (msg.info?.time?.created) {
142
- minimized.time = msg.info.time.created
143
- }
144
-
145
- if (msg.info?.tokens) {
146
- minimized.tokens = {
147
- input: msg.info.tokens.input,
148
- output: msg.info.tokens.output,
149
- reasoning: msg.info.tokens.reasoning,
150
- cache: msg.info.tokens.cache,
151
- }
152
- }
153
-
154
- if (msg.parts) {
155
- minimized.parts = msg.parts
156
- .map((part: any) => {
157
- if (part.type === "step-start" || part.type === "step-finish") {
158
- return null
159
- }
160
-
161
- if (part.type === "text") {
162
- if (part.ignored) return null
163
- const textPart: any = { type: "text", text: part.text }
164
- if (part.metadata) textPart.metadata = part.metadata
165
- return textPart
166
- }
167
-
168
- if (part.type === "reasoning") {
169
- const reasoningPart: any = { type: "reasoning", text: part.text }
170
- if (part.metadata) reasoningPart.metadata = part.metadata
171
- return reasoningPart
172
- }
173
-
174
- if (part.type === "tool") {
175
- const toolPart: any = {
176
- type: "tool",
177
- tool: part.tool,
178
- callID: part.callID,
179
- }
180
-
181
- if (part.state?.status) {
182
- toolPart.status = part.state.status
183
- }
184
- if (part.state?.input) {
185
- toolPart.input = part.state.input
186
- }
187
- if (part.state?.output) {
188
- toolPart.output = part.state.output
189
- }
190
- if (part.state?.error) {
191
- toolPart.error = part.state.error
192
- }
193
- if (part.metadata) {
194
- toolPart.metadata = part.metadata
195
- }
196
- if (part.state?.metadata) {
197
- toolPart.metadata = {
198
- ...(toolPart.metadata || {}),
199
- ...part.state.metadata,
200
- }
201
- }
202
- if (part.state?.title) {
203
- toolPart.title = part.state.title
204
- }
205
-
206
- return toolPart
207
- }
208
-
209
- return null
210
- })
211
- .filter(Boolean)
212
- }
213
-
214
- return minimized
215
- })
216
- }
217
-
218
- async saveContext(sessionId: string, messages: any[]) {
219
- if (!this.enabled) return
220
-
221
- try {
222
- const contextDir = join(this.logDir, "context", sessionId)
223
- if (!existsSync(contextDir)) {
224
- await mkdir(contextDir, { recursive: true })
225
- }
226
-
227
- const minimized = this.minimizeForDebug(messages).filter(
228
- (msg) => msg.parts && msg.parts.length > 0,
229
- )
230
- const timestamp = new Date().toISOString().replace(/[:.]/g, "-")
231
- const contextFile = join(contextDir, `${timestamp}.json`)
232
- await writeFile(contextFile, JSON.stringify(minimized, null, 2))
233
- } catch (error) {}
234
- }
235
- }
@@ -1,56 +0,0 @@
1
- import type { PluginConfig } from "../config"
2
- import type { WithParts } from "../state"
3
-
4
- export const getLastUserMessage = (
5
- messages: WithParts[],
6
- startIndex?: number,
7
- ): WithParts | null => {
8
- const start = startIndex ?? messages.length - 1
9
- for (let i = start; i >= 0; i--) {
10
- const msg = messages[i]
11
- if (msg.info.role === "user" && !isIgnoredUserMessage(msg)) {
12
- return msg
13
- }
14
- }
15
- return null
16
- }
17
-
18
- export const messageHasCompress = (message: WithParts): boolean => {
19
- if (message.info.role !== "assistant") {
20
- return false
21
- }
22
-
23
- const parts = Array.isArray(message.parts) ? message.parts : []
24
- return parts.some(
25
- (part) =>
26
- part.type === "tool" && part.tool === "compress" && part.state?.status === "completed",
27
- )
28
- }
29
-
30
- export const isIgnoredUserMessage = (message: WithParts): boolean => {
31
- if (message.info.role !== "user") {
32
- return false
33
- }
34
-
35
- const parts = Array.isArray(message.parts) ? message.parts : []
36
- if (parts.length === 0) {
37
- return true
38
- }
39
-
40
- for (const part of parts) {
41
- if (!(part as any).ignored) {
42
- return false
43
- }
44
- }
45
-
46
- return true
47
- }
48
-
49
- export function isProtectedUserMessage(config: PluginConfig, message: WithParts): boolean {
50
- return (
51
- config.compress.mode === "message" &&
52
- config.compress.protectUserMessages &&
53
- message.info.role === "user" &&
54
- !isIgnoredUserMessage(message)
55
- )
56
- }
@@ -1,4 +0,0 @@
1
- export * from "./persistence"
2
- export * from "./types"
3
- export * from "./state"
4
- export * from "./tool-cache"
@@ -1,256 +0,0 @@
1
- /**
2
- * State persistence module for DCP plugin.
3
- * Persists pruned tool IDs across sessions so they survive OpenCode restarts.
4
- * Storage location: ~/.local/share/opencode/storage/plugin/dcp/{sessionId}.json
5
- */
6
-
7
- import * as fs from "fs/promises"
8
- import { existsSync } from "fs"
9
- import { homedir } from "os"
10
- import { join } from "path"
11
- import type { CompressionBlock, PrunedMessageEntry, SessionState, SessionStats } from "./types"
12
- import type { Logger } from "../logger"
13
- import { serializePruneMessagesState } from "./utils"
14
-
15
- /** Prune state as stored on disk */
16
- export interface PersistedPruneMessagesState {
17
- byMessageId: Record<string, PrunedMessageEntry>
18
- blocksById: Record<string, CompressionBlock>
19
- activeBlockIds: number[]
20
- activeByAnchorMessageId: Record<string, number>
21
- nextBlockId: number
22
- nextRunId: number
23
- }
24
-
25
- export interface PersistedPrune {
26
- tools?: Record<string, number>
27
- messages?: PersistedPruneMessagesState
28
- }
29
-
30
- export interface PersistedNudges {
31
- contextLimitAnchors: string[]
32
- turnNudgeAnchors?: string[]
33
- iterationNudgeAnchors?: string[]
34
- }
35
-
36
- export interface PersistedSessionState {
37
- sessionName?: string
38
- prune: PersistedPrune
39
- nudges: PersistedNudges
40
- stats: SessionStats
41
- lastUpdated: string
42
- }
43
-
44
- const STORAGE_DIR = join(
45
- process.env.XDG_DATA_HOME || join(homedir(), ".local", "share"),
46
- "opencode",
47
- "storage",
48
- "plugin",
49
- "dcp",
50
- )
51
-
52
- async function ensureStorageDir(): Promise<void> {
53
- if (!existsSync(STORAGE_DIR)) {
54
- await fs.mkdir(STORAGE_DIR, { recursive: true })
55
- }
56
- }
57
-
58
- function getSessionFilePath(sessionId: string): string {
59
- return join(STORAGE_DIR, `${sessionId}.json`)
60
- }
61
-
62
- async function writePersistedSessionState(
63
- sessionId: string,
64
- state: PersistedSessionState,
65
- logger: Logger,
66
- ): Promise<void> {
67
- await ensureStorageDir()
68
-
69
- const filePath = getSessionFilePath(sessionId)
70
- const content = JSON.stringify(state, null, 2)
71
- await fs.writeFile(filePath, content, "utf-8")
72
-
73
- logger.info("Saved session state to disk", {
74
- sessionId,
75
- totalTokensSaved: state.stats.totalPruneTokens,
76
- })
77
- }
78
-
79
- export async function saveSessionState(
80
- sessionState: SessionState,
81
- logger: Logger,
82
- sessionName?: string,
83
- ): Promise<void> {
84
- try {
85
- if (!sessionState.sessionId) {
86
- return
87
- }
88
-
89
- const state: PersistedSessionState = {
90
- sessionName: sessionName,
91
- prune: {
92
- tools: Object.fromEntries(sessionState.prune.tools),
93
- messages: serializePruneMessagesState(sessionState.prune.messages),
94
- },
95
- nudges: {
96
- contextLimitAnchors: Array.from(sessionState.nudges.contextLimitAnchors),
97
- turnNudgeAnchors: Array.from(sessionState.nudges.turnNudgeAnchors),
98
- iterationNudgeAnchors: Array.from(sessionState.nudges.iterationNudgeAnchors),
99
- },
100
- stats: sessionState.stats,
101
- lastUpdated: new Date().toISOString(),
102
- }
103
-
104
- await writePersistedSessionState(sessionState.sessionId, state, logger)
105
- } catch (error: any) {
106
- logger.error("Failed to save session state", {
107
- sessionId: sessionState.sessionId,
108
- error: error?.message,
109
- })
110
- }
111
- }
112
-
113
- export async function loadSessionState(
114
- sessionId: string,
115
- logger: Logger,
116
- ): Promise<PersistedSessionState | null> {
117
- try {
118
- const filePath = getSessionFilePath(sessionId)
119
-
120
- if (!existsSync(filePath)) {
121
- return null
122
- }
123
-
124
- const content = await fs.readFile(filePath, "utf-8")
125
- const state = JSON.parse(content) as PersistedSessionState
126
-
127
- const hasPruneTools = state?.prune?.tools && typeof state.prune.tools === "object"
128
- const hasPruneMessages = state?.prune?.messages && typeof state.prune.messages === "object"
129
- const hasNudgeFormat = state?.nudges && typeof state.nudges === "object"
130
- if (
131
- !state ||
132
- !state.prune ||
133
- !hasPruneTools ||
134
- !hasPruneMessages ||
135
- !state.stats ||
136
- !hasNudgeFormat
137
- ) {
138
- logger.warn("Invalid session state file, ignoring", {
139
- sessionId: sessionId,
140
- })
141
- return null
142
- }
143
-
144
- const rawContextLimitAnchors = Array.isArray(state.nudges.contextLimitAnchors)
145
- ? state.nudges.contextLimitAnchors
146
- : []
147
- const validAnchors = rawContextLimitAnchors.filter(
148
- (entry): entry is string => typeof entry === "string",
149
- )
150
- const dedupedAnchors = [...new Set(validAnchors)]
151
- if (validAnchors.length !== rawContextLimitAnchors.length) {
152
- logger.warn("Filtered out malformed contextLimitAnchors entries", {
153
- sessionId: sessionId,
154
- original: rawContextLimitAnchors.length,
155
- valid: validAnchors.length,
156
- })
157
- }
158
- state.nudges.contextLimitAnchors = dedupedAnchors
159
-
160
- const rawTurnNudgeAnchors = Array.isArray(state.nudges.turnNudgeAnchors)
161
- ? state.nudges.turnNudgeAnchors
162
- : []
163
- const validSoftAnchors = rawTurnNudgeAnchors.filter(
164
- (entry): entry is string => typeof entry === "string",
165
- )
166
- const dedupedSoftAnchors = [...new Set(validSoftAnchors)]
167
- if (validSoftAnchors.length !== rawTurnNudgeAnchors.length) {
168
- logger.warn("Filtered out malformed turnNudgeAnchors entries", {
169
- sessionId: sessionId,
170
- original: rawTurnNudgeAnchors.length,
171
- valid: validSoftAnchors.length,
172
- })
173
- }
174
- state.nudges.turnNudgeAnchors = dedupedSoftAnchors
175
-
176
- const rawIterationNudgeAnchors = Array.isArray(state.nudges.iterationNudgeAnchors)
177
- ? state.nudges.iterationNudgeAnchors
178
- : []
179
- const validIterationAnchors = rawIterationNudgeAnchors.filter(
180
- (entry): entry is string => typeof entry === "string",
181
- )
182
- const dedupedIterationAnchors = [...new Set(validIterationAnchors)]
183
- if (validIterationAnchors.length !== rawIterationNudgeAnchors.length) {
184
- logger.warn("Filtered out malformed iterationNudgeAnchors entries", {
185
- sessionId: sessionId,
186
- original: rawIterationNudgeAnchors.length,
187
- valid: validIterationAnchors.length,
188
- })
189
- }
190
- state.nudges.iterationNudgeAnchors = dedupedIterationAnchors
191
-
192
- logger.info("Loaded session state from disk", {
193
- sessionId: sessionId,
194
- })
195
-
196
- return state
197
- } catch (error: any) {
198
- logger.warn("Failed to load session state", {
199
- sessionId: sessionId,
200
- error: error?.message,
201
- })
202
- return null
203
- }
204
- }
205
-
206
- export interface AggregatedStats {
207
- totalTokens: number
208
- totalTools: number
209
- totalMessages: number
210
- sessionCount: number
211
- }
212
-
213
- export async function loadAllSessionStats(logger: Logger): Promise<AggregatedStats> {
214
- const result: AggregatedStats = {
215
- totalTokens: 0,
216
- totalTools: 0,
217
- totalMessages: 0,
218
- sessionCount: 0,
219
- }
220
-
221
- try {
222
- if (!existsSync(STORAGE_DIR)) {
223
- return result
224
- }
225
-
226
- const files = await fs.readdir(STORAGE_DIR)
227
- const jsonFiles = files.filter((f) => f.endsWith(".json"))
228
-
229
- for (const file of jsonFiles) {
230
- try {
231
- const filePath = join(STORAGE_DIR, file)
232
- const content = await fs.readFile(filePath, "utf-8")
233
- const state = JSON.parse(content) as PersistedSessionState
234
-
235
- if (state?.stats?.totalPruneTokens && state?.prune) {
236
- result.totalTokens += state.stats.totalPruneTokens
237
- result.totalTools += state.prune.tools
238
- ? Object.keys(state.prune.tools).length
239
- : 0
240
- result.totalMessages += state.prune.messages?.byMessageId
241
- ? Object.keys(state.prune.messages.byMessageId).length
242
- : 0
243
- result.sessionCount++
244
- }
245
- } catch {
246
- // Skip invalid files
247
- }
248
- }
249
-
250
- logger.debug("Loaded all-time stats", result)
251
- } catch (error: any) {
252
- logger.warn("Failed to load all-time stats", { error: error?.message })
253
- }
254
-
255
- return result
256
- }