oc-tweaks 0.1.0
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/README.md +333 -0
- package/package.json +27 -0
- package/src/__tests__/.gitkeep +0 -0
- package/src/__tests__/background-subagent.test.ts +106 -0
- package/src/__tests__/cli-init.test.ts +70 -0
- package/src/__tests__/compaction.test.ts +113 -0
- package/src/__tests__/index.test.ts +180 -0
- package/src/__tests__/leaderboard.test.ts +244 -0
- package/src/__tests__/logger.test.ts +84 -0
- package/src/__tests__/notify.test.ts +318 -0
- package/src/__tests__/utils.test.ts +164 -0
- package/src/bun-test.d.ts +12 -0
- package/src/cli/init.ts +44 -0
- package/src/index.ts +4 -0
- package/src/plugins/.gitkeep +0 -0
- package/src/plugins/background-subagent.ts +59 -0
- package/src/plugins/compaction.ts +28 -0
- package/src/plugins/leaderboard.ts +184 -0
- package/src/plugins/notify.ts +383 -0
- package/src/types.ts +2 -0
- package/src/utils/.gitkeep +0 -0
- package/src/utils/config.ts +71 -0
- package/src/utils/index.ts +5 -0
- package/src/utils/logger.ts +52 -0
- package/src/utils/safe-hook.ts +16 -0
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
declare const Bun: any
|
|
2
|
+
|
|
3
|
+
import { mkdir } from "node:fs/promises"
|
|
4
|
+
import { dirname } from "node:path"
|
|
5
|
+
export interface LoggerConfig {
|
|
6
|
+
enabled?: boolean
|
|
7
|
+
maxLines?: number
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const DEFAULT_MAX_LINES = 100
|
|
11
|
+
|
|
12
|
+
function getHome(): string {
|
|
13
|
+
return Bun.env?.HOME ?? ((globalThis as any)?.process?.env?.HOME ?? "") ?? ""
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function getLogFilePath(): string {
|
|
17
|
+
return `${getHome()}/.config/opencode/plugins/oc-tweaks.log`
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export async function log(
|
|
21
|
+
config: LoggerConfig | undefined,
|
|
22
|
+
level: string,
|
|
23
|
+
message: string
|
|
24
|
+
): Promise<void> {
|
|
25
|
+
if (!config?.enabled) return
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
const logFile = getLogFilePath()
|
|
29
|
+
const file = Bun.file(logFile)
|
|
30
|
+
let content = ""
|
|
31
|
+
|
|
32
|
+
if (await file.exists()) {
|
|
33
|
+
content = await file.text()
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const line = `[${new Date().toISOString()}] [${level}] ${message}\n`
|
|
37
|
+
content += line
|
|
38
|
+
|
|
39
|
+
const maxLines = config.maxLines ?? DEFAULT_MAX_LINES
|
|
40
|
+
const keepLines = Math.floor(maxLines / 2)
|
|
41
|
+
const lines = content.split("\n").filter(Boolean)
|
|
42
|
+
|
|
43
|
+
if (lines.length > maxLines) {
|
|
44
|
+
content = lines.slice(-keepLines).join("\n") + "\n"
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
await mkdir(dirname(logFile), { recursive: true })
|
|
48
|
+
await Bun.write(logFile, content)
|
|
49
|
+
} catch {
|
|
50
|
+
// Never disrupt user workflow
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { log } from "./logger"
|
|
2
|
+
import type { LoggerConfig } from "./logger"
|
|
3
|
+
|
|
4
|
+
export function safeHook<T extends (...args: any[]) => any>(
|
|
5
|
+
name: string,
|
|
6
|
+
fn: T,
|
|
7
|
+
loggerConfig?: LoggerConfig
|
|
8
|
+
): T {
|
|
9
|
+
return (async (...args: any[]) => {
|
|
10
|
+
try {
|
|
11
|
+
return await fn(...args)
|
|
12
|
+
} catch (err) {
|
|
13
|
+
await log(loggerConfig, "ERROR", `[oc-tweaks] ${name}: ${err}`)
|
|
14
|
+
}
|
|
15
|
+
}) as unknown as T
|
|
16
|
+
}
|