@magpie-community/opencode-trae-auth 0.2.11 → 0.2.12

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 (3) hide show
  1. package/README.md +4 -0
  2. package/index.mjs +26 -4
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -107,6 +107,10 @@ agent's `POST https://trae-api-cn.mchost.guru/api/agent/v3/llm_utils_chat`:
107
107
  Text under `message`, `delta` or an event with no name is the answer's
108
108
  too.
109
109
  - `token_usage`, `done` and `error` mark usage, the end and failures.
110
+ What `token_usage` says the prompt cache read and wrote
111
+ (`cache_read_input_tokens`, `cache_creation_input_tokens`) goes on under
112
+ those names, and `reasoning_tokens` as
113
+ `completion_tokens_details.reasoning_tokens`.
110
114
  `done`'s `finish_reason` `length`/`max_tokens` is OpenAI's `length`.
111
115
  - Queue events are ignored.
112
116
  - A `{"reasoning_content": …}` object written into the text is reasoning,
package/index.mjs CHANGED
@@ -992,13 +992,35 @@ const eventName = (s) =>
992
992
  .toLowerCase()
993
993
  .replace(/^_|_$/g, "")
994
994
 
995
- // usageOf maps Trae's token counts to OpenAI's
995
+ // tokensOf maps Trae's token counts to OpenAI's. Trae's token_usage event
996
+ // (TRAE SOLO CN's ai-agent, TokenUsageEvent: name, prompt_tokens,
997
+ // completion_tokens, total_tokens, reasoning_tokens,
998
+ // cache_creation_input_tokens, cache_read_input_tokens,
999
+ // prompt_tokens_total, completion_tokens_total) says what the prompt cache
1000
+ // read and wrote under Anthropic's names. They go on under those names,
1001
+ // which magpie reads beside prompt_tokens as it reads a relay's in front of
1002
+ // Claude (whether prompt_tokens counts them too is judged there, by
1003
+ // whether it is at least as big); without them every request read as no
1004
+ // cache hit (01huadalang on magpie's Discord: K3, 53 calls, all 0)
996
1005
  function tokensOf(u) {
997
1006
  if (!u || typeof u !== "object") return null
998
- const p = Number(u.prompt_tokens ?? u.input_tokens ?? u.promptTokens ?? u.inputTokens ?? 0) || 0
999
- const c = Number(u.completion_tokens ?? u.output_tokens ?? u.completionTokens ?? u.outputTokens ?? 0) || 0
1007
+ const n = (...v) => Number(v.find((x) => x != null) ?? 0) || 0
1008
+ const p = n(u.prompt_tokens, u.input_tokens, u.promptTokens, u.inputTokens)
1009
+ const c = n(u.completion_tokens, u.output_tokens, u.completionTokens, u.outputTokens)
1000
1010
  if (!p && !c) return null
1001
- return { prompt_tokens: p, completion_tokens: c, total_tokens: Number(u.total_tokens ?? u.totalTokens) || p + c }
1011
+ const out = { prompt_tokens: p, completion_tokens: c, total_tokens: n(u.total_tokens, u.totalTokens) || p + c }
1012
+ const read = n(u.prompt_tokens_details?.cached_tokens)
1013
+ const write = n(u.prompt_tokens_details?.cache_write_tokens)
1014
+ if (read || write) out.prompt_tokens_details = { cached_tokens: read, ...(write ? { cache_write_tokens: write } : {}) }
1015
+ else {
1016
+ const r = n(u.cache_read_input_tokens, u.cacheReadInputTokens)
1017
+ const w = n(u.cache_creation_input_tokens, u.cacheCreationInputTokens)
1018
+ if (r) out.cache_read_input_tokens = r
1019
+ if (w) out.cache_creation_input_tokens = w
1020
+ }
1021
+ const thought = n(u.reasoning_tokens, u.completion_tokens_details?.reasoning_tokens)
1022
+ if (thought) out.completion_tokens_details = { reasoning_tokens: thought }
1023
+ return out
1002
1024
  }
1003
1025
 
1004
1026
  let callSeq = 0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-trae-auth",
3
- "version": "0.2.11",
3
+ "version": "0.2.12",
4
4
  "description": "OpenCode provider plugins for Trae: Trae CN (trae.cn) and Trae Global (trae.ai), free tier included, with Trae's own browser sign-in",
5
5
  "type": "module",
6
6
  "main": "./index.mjs",