@cocorograph/hub-agent 0.6.7 → 0.6.8

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/usage.mjs +22 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocorograph/hub-agent",
3
- "version": "0.6.7",
3
+ "version": "0.6.8",
4
4
  "description": "Hub Hosted Cockpit のローカル常駐 agent。Hub と outbound WSS で接続し、ローカルの tmux/pty を中継する。",
5
5
  "type": "module",
6
6
  "license": "UNLICENSED",
package/src/usage.mjs CHANGED
@@ -245,6 +245,26 @@ async function statuslineCacheMtime() {
245
245
  }
246
246
  }
247
247
 
248
+ /**
249
+ * context 窓サイズ (トークン) を解決する。statusLine cache に context_window_size が
250
+ * あればそれを使う (1M ベータ等を自動反映)。無ければ HUB_CONTEXT_WINDOW / 200000。
251
+ * ※ cache が stale でも窓サイズ自体は不変なので利用できる。
252
+ */
253
+ async function contextWindowSize() {
254
+ const text = await readOrNull(statuslineCache())
255
+ if (text) {
256
+ try {
257
+ const j = JSON.parse(text)
258
+ const cw = j.context_window ?? j.contextWindow
259
+ const size = cw?.context_window_size ?? cw?.contextWindowSize
260
+ if (typeof size === "number" && size > 0) return size
261
+ } catch {
262
+ /* ignore */
263
+ }
264
+ }
265
+ return CONTEXT_WINDOW
266
+ }
267
+
248
268
  /**
249
269
  * 直近にアクティブな session jsonl の最後の assistant.usage から context% を
250
270
  * 推定する。チャットモード(SDK headless)は statusLine を書き出さないため、
@@ -279,6 +299,7 @@ async function latestJsonlContext(now) {
279
299
  }),
280
300
  )
281
301
  if (!best) return null
302
+ const windowSize = await contextWindowSize()
282
303
  const text = await readOrNull(best.fp)
283
304
  if (!text) return null
284
305
  const lines = text.split("\n")
@@ -301,7 +322,7 @@ async function latestJsonlContext(now) {
301
322
  (u.cache_creation_input_tokens || 0) +
302
323
  (u.output_tokens || 0)
303
324
  if (tokens <= 0) continue
304
- const percent = Math.min(100, Math.round((tokens / CONTEXT_WINDOW) * 1000) / 10)
325
+ const percent = Math.min(100, Math.round((tokens / windowSize) * 1000) / 10)
305
326
  return { percent, mtimeMs: best.mtimeMs }
306
327
  }
307
328
  return null