Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion bin/claude-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ import { startProxyServer } from "../src/proxy/server"

const port = parseInt(process.env.CLAUDE_PROXY_PORT || "3456", 10)
const host = process.env.CLAUDE_PROXY_HOST || "127.0.0.1"
const idleTimeoutSeconds = parseInt(process.env.CLAUDE_PROXY_IDLE_TIMEOUT_SECONDS || "120", 10)

await startProxyServer({ port, host })
await startProxyServer({ port, host, idleTimeoutSeconds })
74 changes: 68 additions & 6 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,72 @@
import { AsyncLocalStorage } from "node:async_hooks"

type LogFields = Record<string, unknown>

const contextStore = new AsyncLocalStorage<LogFields>()

const shouldLog = () => process.env["OPENCODE_CLAUDE_PROVIDER_DEBUG"]
const shouldLogStreamDebug = () => process.env["OPENCODE_CLAUDE_PROVIDER_STREAM_DEBUG"]

export const claudeLog = (message: string, extra?: Record<string, unknown>) => {
if (!shouldLog()) return
const parts = ["[opencode-claude-code-provider]", message]
if (extra && Object.keys(extra).length > 0) {
parts.push(JSON.stringify(extra))
const isVerboseStreamEvent = (event: string): boolean => {
return event.startsWith("stream.") || event === "response.empty_stream"
}

const REDACTED_KEYS = new Set([
"authorization",
"cookie",
"x-api-key",
"apiKey",
"apikey",
"prompt",
"messages",
"content"
])

const sanitize = (value: unknown): unknown => {
if (value === null || value === undefined) return value

if (typeof value === "string") {
if (value.length > 512) {
return `${value.slice(0, 512)}... [truncated=${value.length}]`
}
return value
}

if (Array.isArray(value)) {
return value.map(sanitize)
}
console.debug(parts.join(" "))

if (typeof value === "object") {
const out: Record<string, unknown> = {}
for (const [k, v] of Object.entries(value as Record<string, unknown>)) {
if (REDACTED_KEYS.has(k)) {
if (typeof v === "string") {
out[k] = `[redacted len=${v.length}]`
} else if (Array.isArray(v)) {
out[k] = `[redacted array len=${v.length}]`
} else {
out[k] = "[redacted]"
}
} else {
out[k] = sanitize(v)
}
}
return out
}

return value
}

export const withClaudeLogContext = <T>(context: LogFields, fn: () => T): T => {
return contextStore.run(context, fn)
}

export const claudeLog = (event: string, extra?: LogFields) => {
if (!shouldLog()) return
if (isVerboseStreamEvent(event) && !shouldLogStreamDebug()) return

const context = contextStore.getStore() || {}
const payload = sanitize({ ts: new Date().toISOString(), event, ...context, ...(extra || {}) })

console.debug(`[opencode-claude-code-provider] ${JSON.stringify(payload)}`)
}
Loading