@distlang/opencode-plugin 0.2.2 → 0.2.3

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/recorder.js +26 -4
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@distlang/opencode-plugin",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "description": "OpenCode plugin for Distlang Agent Debugger",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
package/src/recorder.js CHANGED
@@ -947,8 +947,13 @@ function assistantFinalState(info) {
947
947
 
948
948
  function tokensFromMessage(info) {
949
949
  const sources = tokenSources(info);
950
- const cacheValue = firstNumber(sources, ["cache", "cached", "cached_tokens", "cachedTokens", "cacheReadInputTokens", "cachedInput", "cachedInputTokens"]);
951
- const input = firstNumber(sources, ["input", "input_tokens", "inputTokens", "prompt", "prompt_tokens", "promptTokens"]);
950
+ const nestedCacheRead = firstNestedNumber(sources, ["cache", "read"]);
951
+ const nestedCacheWrite = firstNestedNumber(sources, ["cache", "write"]);
952
+ const flatCacheRead = firstNumber(sources, ["cached", "cached_tokens", "cachedTokens", "cacheReadInputTokens", "cachedInput", "cachedInputTokens"]);
953
+ const cacheRead = nestedCacheRead || flatCacheRead;
954
+ const cacheWrite = nestedCacheWrite;
955
+ const rawInput = firstNumber(sources, ["input", "input_tokens", "inputTokens", "prompt", "prompt_tokens", "promptTokens"]);
956
+ const input = rawInput + (nestedCacheRead > 0 || nestedCacheWrite > 0 ? cacheRead + cacheWrite : 0);
952
957
  const output = firstNumber(sources, ["output", "output_tokens", "outputTokens", "completion", "completion_tokens", "completionTokens"]);
953
958
  const reasoning = firstNumber(sources, ["reasoning", "reasoning_tokens", "reasoningTokens"]);
954
959
  const explicitContext = firstNumber(sources, ["context", "context_tokens", "contextTokens", "context_size", "contextSize", "context_size_tokens", "contextSizeTokens"]);
@@ -956,8 +961,8 @@ function tokensFromMessage(info) {
956
961
  input,
957
962
  output,
958
963
  reasoning,
959
- cached: cacheValue,
960
- context: explicitContext > 0 ? explicitContext : input + cacheValue + reasoning,
964
+ cached: cacheRead,
965
+ context: explicitContext > 0 ? explicitContext : rawInput + cacheRead + cacheWrite + reasoning,
961
966
  };
962
967
  }
963
968
 
@@ -995,6 +1000,23 @@ function firstNumber(sources, keys) {
995
1000
  return 0;
996
1001
  }
997
1002
 
1003
+ function firstNestedNumber(sources, path) {
1004
+ for (const source of sources) {
1005
+ let value = source;
1006
+ for (const key of path) {
1007
+ value = value && typeof value === "object" ? value[key] : undefined;
1008
+ if (typeof value === "undefined" || value === null) {
1009
+ break;
1010
+ }
1011
+ }
1012
+ const parsed = finiteNumber(value);
1013
+ if (parsed > 0) {
1014
+ return parsed;
1015
+ }
1016
+ }
1017
+ return 0;
1018
+ }
1019
+
998
1020
  function updateSessionTitle(recorder, info) {
999
1021
  const title = extractSessionTitle(info);
1000
1022
  if (title) {