@tarquinen/opencode-dcp 3.2.4-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,172 +0,0 @@
1
- import type { SessionState, WithParts } from "./state"
2
- import { isIgnoredUserMessage } from "./messages/query"
3
-
4
- const MESSAGE_REF_REGEX = /^m(\d{4})$/
5
- const BLOCK_REF_REGEX = /^b([1-9]\d*)$/
6
- const MESSAGE_ID_TAG_NAME = "dcp-message-id"
7
-
8
- const MESSAGE_REF_WIDTH = 4
9
- const MESSAGE_REF_MIN_INDEX = 1
10
- export const MESSAGE_REF_MAX_INDEX = 9999
11
-
12
- export type ParsedBoundaryId =
13
- | {
14
- kind: "message"
15
- ref: string
16
- index: number
17
- }
18
- | {
19
- kind: "compressed-block"
20
- ref: string
21
- blockId: number
22
- }
23
-
24
- export function formatMessageRef(index: number): string {
25
- if (
26
- !Number.isInteger(index) ||
27
- index < MESSAGE_REF_MIN_INDEX ||
28
- index > MESSAGE_REF_MAX_INDEX
29
- ) {
30
- throw new Error(
31
- `Message ID index out of bounds: ${index}. Supported range is 0-${MESSAGE_REF_MAX_INDEX}.`,
32
- )
33
- }
34
- return `m${index.toString().padStart(MESSAGE_REF_WIDTH, "0")}`
35
- }
36
-
37
- export function formatBlockRef(blockId: number): string {
38
- if (!Number.isInteger(blockId) || blockId < 1) {
39
- throw new Error(`Invalid block ID: ${blockId}`)
40
- }
41
- return `b${blockId}`
42
- }
43
-
44
- export function parseMessageRef(ref: string): number | null {
45
- const normalized = ref.trim().toLowerCase()
46
- const match = normalized.match(MESSAGE_REF_REGEX)
47
- if (!match) {
48
- return null
49
- }
50
- const index = Number.parseInt(match[1], 10)
51
- if (!Number.isInteger(index)) {
52
- return null
53
- }
54
- if (index < MESSAGE_REF_MIN_INDEX || index > MESSAGE_REF_MAX_INDEX) {
55
- return null
56
- }
57
- return index
58
- }
59
-
60
- export function parseBlockRef(ref: string): number | null {
61
- const normalized = ref.trim().toLowerCase()
62
- const match = normalized.match(BLOCK_REF_REGEX)
63
- if (!match) {
64
- return null
65
- }
66
- const id = Number.parseInt(match[1], 10)
67
- return Number.isInteger(id) ? id : null
68
- }
69
-
70
- export function parseBoundaryId(id: string): ParsedBoundaryId | null {
71
- const normalized = id.trim().toLowerCase()
72
- const messageIndex = parseMessageRef(normalized)
73
- if (messageIndex !== null) {
74
- return {
75
- kind: "message",
76
- ref: formatMessageRef(messageIndex),
77
- index: messageIndex,
78
- }
79
- }
80
-
81
- const blockId = parseBlockRef(normalized)
82
- if (blockId !== null) {
83
- return {
84
- kind: "compressed-block",
85
- ref: formatBlockRef(blockId),
86
- blockId,
87
- }
88
- }
89
-
90
- return null
91
- }
92
-
93
- function escapeXmlAttribute(value: string): string {
94
- return value
95
- .replace(/&/g, "&amp;")
96
- .replace(/"/g, "&quot;")
97
- .replace(/</g, "&lt;")
98
- .replace(/>/g, "&gt;")
99
- }
100
-
101
- export function formatMessageIdTag(
102
- ref: string,
103
- attributes?: Record<string, string | undefined>,
104
- ): string {
105
- const serializedAttributes = Object.entries(attributes || {})
106
- .sort(([left], [right]) => left.localeCompare(right))
107
- .map(([name, value]) => {
108
- if (name.trim().length === 0 || typeof value !== "string" || value.length === 0) {
109
- return ""
110
- }
111
-
112
- return ` ${name}="${escapeXmlAttribute(value)}"`
113
- })
114
- .join("")
115
-
116
- return `\n<${MESSAGE_ID_TAG_NAME}${serializedAttributes}>${ref}</${MESSAGE_ID_TAG_NAME}>`
117
- }
118
-
119
- export function assignMessageRefs(state: SessionState, messages: WithParts[]): number {
120
- let assigned = 0
121
- let skippedSubAgentPrompt = false
122
-
123
- for (const message of messages) {
124
- if (isIgnoredUserMessage(message)) {
125
- continue
126
- }
127
-
128
- if (state.isSubAgent && !skippedSubAgentPrompt && message.info.role === "user") {
129
- skippedSubAgentPrompt = true
130
- continue
131
- }
132
-
133
- const rawMessageId = message.info.id
134
- if (typeof rawMessageId !== "string" || rawMessageId.length === 0) {
135
- continue
136
- }
137
-
138
- const existingRef = state.messageIds.byRawId.get(rawMessageId)
139
- if (existingRef) {
140
- if (state.messageIds.byRef.get(existingRef) !== rawMessageId) {
141
- state.messageIds.byRef.set(existingRef, rawMessageId)
142
- }
143
- continue
144
- }
145
-
146
- const ref = allocateNextMessageRef(state)
147
- state.messageIds.byRawId.set(rawMessageId, ref)
148
- state.messageIds.byRef.set(ref, rawMessageId)
149
- assigned++
150
- }
151
-
152
- return assigned
153
- }
154
-
155
- function allocateNextMessageRef(state: SessionState): string {
156
- let candidate = Number.isInteger(state.messageIds.nextRef)
157
- ? Math.max(MESSAGE_REF_MIN_INDEX, state.messageIds.nextRef)
158
- : MESSAGE_REF_MIN_INDEX
159
-
160
- while (candidate <= MESSAGE_REF_MAX_INDEX) {
161
- const ref = formatMessageRef(candidate)
162
- if (!state.messageIds.byRef.has(ref)) {
163
- state.messageIds.nextRef = candidate + 1
164
- return ref
165
- }
166
- candidate++
167
- }
168
-
169
- throw new Error(
170
- `Message ID alias capacity exceeded. Cannot allocate more than ${formatMessageRef(MESSAGE_REF_MAX_INDEX)} aliases in this session.`,
171
- )
172
- }
@@ -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"