Replace per-turn detached workers with a file-backed queue consumed by
a single long-running batch worker (30s interval, 0-10s jitter).
- queue.ts, batchWorker.ts: file queue with pid-based lock for worker
singleton; tasks deduped by sessionID:messageID before processing
- worker.ts: retry with backoff (5s, 10s) for 429 and network errors;
update commit state per upload so partial failures resume cleanly;
export auth, loadSessionMessages, getSessionDirectory and upload*
helpers for batchWorker reuse
- git.ts: cap commit log at 50 entries within last 7 days; pause 500ms
every 10 commits to spread load
- worker.ts: resolve session jsonl via ~/.claude/projects/{normalized}
with fallbacks, scanning for the file containing the target session
- logger.ts: file + stderr logger gated by CSC_RAW_DUMP_DEBUG, default
silent
- sessionDataUploader.ts: implement createSessionTurnUploader to pick
the last assistant message; query.ts fires uploadSessionTurn after
query_api_streaming_end (non-blocking)
Signed-off-by: 林凯90331 <90331@sangfor.com>
Co-authored-by: CoStrict <zgsm@sangfor.com.cn>
40 lines
1.2 KiB
TypeScript
40 lines
1.2 KiB
TypeScript
/**
|
|
* Raw Dump 日志模块
|
|
* 通过环境变量开关控制,默认关闭,与业务逻辑完全解耦
|
|
*/
|
|
|
|
import { appendFileSync } from 'node:fs'
|
|
import os from 'node:os'
|
|
import path from 'node:path'
|
|
|
|
const LOG_FILE = path.join(os.homedir(), '.claude', 'csc-raw-dump.log')
|
|
|
|
function isDebugEnabled(): boolean {
|
|
const v = process.env.CSC_RAW_DUMP_DEBUG
|
|
return v === '1' || v === 'true'
|
|
}
|
|
|
|
export function createLogger(prefix: string) {
|
|
const enabled = isDebugEnabled()
|
|
|
|
function write(level: string, msg: string, meta?: Record<string, unknown>) {
|
|
if (!enabled) return
|
|
const timestamp = new Date().toISOString()
|
|
const metaStr = meta ? ` ${JSON.stringify(meta)}` : ''
|
|
const line = `[${timestamp}] [${prefix}:${level}] ${msg}${metaStr}\n`
|
|
console.error(line.trimEnd())
|
|
try {
|
|
appendFileSync(LOG_FILE, line)
|
|
} catch {
|
|
// ignore
|
|
}
|
|
}
|
|
|
|
return {
|
|
debug: (msg: string, meta?: Record<string, unknown>) => write('debug', msg, meta),
|
|
info: (msg: string, meta?: Record<string, unknown>) => write('info', msg, meta),
|
|
warn: (msg: string, meta?: Record<string, unknown>) => write('warn', msg, meta),
|
|
error: (msg: string, meta?: Record<string, unknown>) => write('error', msg, meta),
|
|
}
|
|
}
|