@goodandready/dsh-subscriptions 0.5.3 → 0.5.4

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.
@@ -0,0 +1,66 @@
1
+ /**
2
+ * Session Cache & Token Analyzer.
3
+ * Parses Harness turn usage and reports cache efficiency.
4
+ */
5
+
6
+ export function analyzeSessionEvents(events = []) {
7
+ let promptTokens = 0
8
+ let cachedTokens = 0
9
+ let completionTokens = 0
10
+ let totalCalls = 0
11
+ const callRecords = []
12
+
13
+ let prevPrompt = 0
14
+ for (const ev of events) {
15
+ if (!ev) continue
16
+ const usage = ev.usage || (ev.type === 'usage' ? ev.usage : null) || (ev.data && ev.data.usage)
17
+ if (!usage) continue
18
+
19
+ const p = Number(usage.prompt_tokens || usage.input_tokens || usage.promptTokens || 0)
20
+ const c = Number(usage.prompt_tokens_details?.cached_tokens || usage.cache_read_input_tokens || usage.cached_tokens || usage.cachedTokens || 0)
21
+ const comp = Number(usage.completion_tokens || usage.output_tokens || usage.completionTokens || 0)
22
+
23
+ if (p <= 0 && comp <= 0) continue
24
+
25
+ totalCalls++
26
+ promptTokens += p
27
+ cachedTokens += c
28
+ completionTokens += comp
29
+
30
+ let classification = 'cold_start'
31
+ if (totalCalls > 1) {
32
+ if (c > 0 && p > 0 && (c / p) >= 0.5) {
33
+ classification = 'cache_hit'
34
+ } else if (p > prevPrompt) {
35
+ classification = 'delta'
36
+ } else {
37
+ classification = 'affinity_miss'
38
+ }
39
+ }
40
+ prevPrompt = p
41
+
42
+ callRecords.push({
43
+ call: totalCalls,
44
+ prompt: p,
45
+ cached: c,
46
+ completion: comp,
47
+ classification,
48
+ hitRate: p > 0 ? Math.round((c / p) * 100) : 0,
49
+ })
50
+ }
51
+
52
+ const weightedCacheHitPercent = promptTokens > 0
53
+ ? Math.round((cachedTokens / promptTokens) * 1000) / 10
54
+ : 0
55
+
56
+ return {
57
+ totalCalls,
58
+ promptTokens,
59
+ cachedTokens,
60
+ completionTokens,
61
+ totalTokens: promptTokens + completionTokens,
62
+ weightedCacheHitPercent,
63
+ savedTokens: cachedTokens,
64
+ calls: callRecords,
65
+ }
66
+ }
package/lib/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { analyzeSessionEvents } from './analyze-session.js'
1
2
  import { discoverLocalCliSessions, loadLocalCliBlob } from './import-auth.js'
2
3
  import { readFileSync } from 'node:fs'
3
4
  import z from '@deepseek-ai/schemastery'
@@ -1170,6 +1171,26 @@ export function apply(ctx, config) {
1170
1171
  },
1171
1172
  }), 'dsh-subscriptions: /import-local')
1172
1173
 
1174
+ ctx.effect(() => ctx.webServer.register({
1175
+ kind: 'exact',
1176
+ path: '/dsh-subscriptions/analyze-session',
1177
+ handler: async (req, res) => {
1178
+ if (req.method !== 'POST') {
1179
+ writeJson(res, 405, { ok: false, error: { code: 'method', message: 'POST only' } })
1180
+ return
1181
+ }
1182
+ try {
1183
+ const body = await readBody(req).catch(() => ({}))
1184
+ const events = Array.isArray(body && body.events) ? body.events : []
1185
+ const analysis = analyzeSessionEvents(events)
1186
+ writeJson(res, 200, { ok: true, analysis })
1187
+ } catch (e) {
1188
+ writeJson(res, 500, { ok: false, error: { message: String(e && e.message || e) } })
1189
+ }
1190
+ },
1191
+ }), 'dsh-subscriptions: /analyze-session')
1192
+
1193
+
1173
1194
 
1174
1195
  // HTTP-прокси к API провайдера через subscriptions.request.
1175
1196
  // Same-origin only, allowlist путей, ротация и квота как у моделей.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@goodandready/dsh-subscriptions",
3
- "version": "0.5.3",
3
+ "version": "0.5.4",
4
4
  "description": "Use ChatGPT Codex, Claude, Grok, and Antigravity subscriptions as DeepSeek Harness LLM providers via OAuth.",
5
5
  "license": "MIT",
6
6
  "type": "module",