@vikrant82/opencode-cache-keepalive 0.1.5 → 0.1.6
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 +4 -1
- package/dist/index.js +65 -26
- package/dist/index.js.map +1 -1
- package/dist/lib/control.d.ts +34 -1
- package/dist/lib/control.d.ts.map +1 -1
- package/dist/lib/keepalive.d.ts +3 -0
- package/dist/lib/keepalive.d.ts.map +1 -1
- package/dist/lib/state.d.ts +3 -0
- package/dist/lib/state.d.ts.map +1 -1
- package/dist/lib/tui/commands.d.ts +7 -0
- package/dist/lib/tui/commands.d.ts.map +1 -1
- package/dist/lib/tui/footer.d.ts.map +1 -1
- package/dist/lib/tui/format.d.ts +8 -0
- package/dist/lib/tui/format.d.ts.map +1 -1
- package/lib/control.ts +96 -11
- package/lib/keepalive.ts +49 -20
- package/lib/state.ts +9 -2
- package/lib/tui/commands.tsx +184 -0
- package/lib/tui/footer.tsx +33 -6
- package/lib/tui/format.ts +36 -0
- package/package.json +1 -1
- package/lib/tui/commands.ts +0 -59
package/lib/tui/format.ts
CHANGED
|
@@ -11,3 +11,39 @@ export function kfmt(n: number): string {
|
|
|
11
11
|
const k = n / 1000
|
|
12
12
|
return `${k >= 10 ? Math.round(k) : k.toFixed(1)}k`
|
|
13
13
|
}
|
|
14
|
+
|
|
15
|
+
const UNIT_MS: Record<string, number> = { h: 3_600_000, m: 60_000, s: 1_000 }
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Parse a human duration such as "4m30s", "4.5m", "270s", "1h" or "28m 20s".
|
|
19
|
+
* A bare number means minutes. Returns undefined for anything unparseable;
|
|
20
|
+
* range checks are the caller's concern.
|
|
21
|
+
*/
|
|
22
|
+
export function parseDuration(input: string): number | undefined {
|
|
23
|
+
const text = input.trim().toLowerCase()
|
|
24
|
+
if (!text) return undefined
|
|
25
|
+
if (/^\d+(?:\.\d+)?$/.test(text)) return Math.round(Number(text) * UNIT_MS.m)
|
|
26
|
+
|
|
27
|
+
const segment = /(\d+(?:\.\d+)?)\s*(hours?|hrs?|h|minutes?|mins?|m|seconds?|secs?|s)\s*/y
|
|
28
|
+
let total = 0
|
|
29
|
+
while (segment.lastIndex < text.length) {
|
|
30
|
+
const match = segment.exec(text)
|
|
31
|
+
if (!match) return undefined
|
|
32
|
+
total += Number(match[1]) * UNIT_MS[match[2][0]]
|
|
33
|
+
}
|
|
34
|
+
return Math.round(total)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Compact duration label, e.g. 270000 -> "4m 30s", 3600000 -> "1h". */
|
|
38
|
+
export function formatDuration(ms: number): string {
|
|
39
|
+
const total = Math.max(0, Math.round(ms / 1000))
|
|
40
|
+
const hours = Math.floor(total / 3600)
|
|
41
|
+
const minutes = Math.floor((total % 3600) / 60)
|
|
42
|
+
const seconds = total % 60
|
|
43
|
+
const parts = [
|
|
44
|
+
hours ? `${hours}h` : "",
|
|
45
|
+
minutes ? `${minutes}m` : "",
|
|
46
|
+
seconds ? `${seconds}s` : "",
|
|
47
|
+
].filter(Boolean)
|
|
48
|
+
return parts.length ? parts.join(" ") : "0s"
|
|
49
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package.json",
|
|
3
3
|
"name": "@vikrant82/opencode-cache-keepalive",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.6",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "OpenCode plugin that keeps provider prompt caches warm by sending invisible keepalive pings, with a live TUI readout",
|
|
7
7
|
"main": "./dist/index.js",
|
package/lib/tui/commands.ts
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import type { TuiPluginApi } from "@opencode-ai/plugin/tui"
|
|
2
|
-
import { readControl, writeControl } from "../control"
|
|
3
|
-
|
|
4
|
-
export function registerKeepaliveCommands(api: TuiPluginApi): void {
|
|
5
|
-
// The control file is scoped per directory, so toggling keepalive here only
|
|
6
|
-
// affects this project's server plugin — not other open opencode sessions.
|
|
7
|
-
// Read lazily: state paths may not be populated at registration time.
|
|
8
|
-
const directory = () => api.state.path.directory
|
|
9
|
-
|
|
10
|
-
const setEnabled = async (enabled: boolean) => {
|
|
11
|
-
try {
|
|
12
|
-
await writeControl(enabled, directory())
|
|
13
|
-
api.ui.toast({
|
|
14
|
-
variant: enabled ? "success" : "warning",
|
|
15
|
-
title: "Cache keepalive",
|
|
16
|
-
message: enabled ? "Enabled" : "Disabled",
|
|
17
|
-
})
|
|
18
|
-
} catch (error) {
|
|
19
|
-
api.ui.toast({
|
|
20
|
-
variant: "error",
|
|
21
|
-
title: "Cache keepalive",
|
|
22
|
-
message: error instanceof Error ? error.message : String(error),
|
|
23
|
-
})
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
api.keymap.registerLayer({
|
|
28
|
-
commands: [
|
|
29
|
-
{
|
|
30
|
-
name: "keepalive.toggle",
|
|
31
|
-
title: "Toggle cache keepalive",
|
|
32
|
-
category: "Keepalive",
|
|
33
|
-
namespace: "palette",
|
|
34
|
-
slashName: "keepalive-toggle",
|
|
35
|
-
run: () => setEnabled(!currentEnabled(directory())),
|
|
36
|
-
},
|
|
37
|
-
{
|
|
38
|
-
name: "keepalive.on",
|
|
39
|
-
title: "Enable cache keepalive",
|
|
40
|
-
category: "Keepalive",
|
|
41
|
-
namespace: "palette",
|
|
42
|
-
slashName: "keepalive-on",
|
|
43
|
-
run: () => setEnabled(true),
|
|
44
|
-
},
|
|
45
|
-
{
|
|
46
|
-
name: "keepalive.off",
|
|
47
|
-
title: "Disable cache keepalive",
|
|
48
|
-
category: "Keepalive",
|
|
49
|
-
namespace: "palette",
|
|
50
|
-
slashName: "keepalive-off",
|
|
51
|
-
run: () => setEnabled(false),
|
|
52
|
-
},
|
|
53
|
-
],
|
|
54
|
-
})
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function currentEnabled(directory: string): boolean {
|
|
58
|
-
return readControl(directory)?.enabled ?? true
|
|
59
|
-
}
|