opencode-subagent-tools 0.1.0 → 0.2.0

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/src/tokens.ts CHANGED
@@ -4,35 +4,35 @@ import {
4
4
  type ModelRefLike,
5
5
  type TokenUsageLike,
6
6
  type UsageMessage,
7
- } from "./session"
7
+ } from "./session";
8
8
 
9
9
  export interface TokenUsage {
10
- input: number
11
- output: number
12
- reasoning: number
13
- cache: { read: number; write: number }
10
+ input: number;
11
+ output: number;
12
+ reasoning: number;
13
+ cache: { read: number; write: number };
14
14
  }
15
15
 
16
16
  export interface LastRequestUsage {
17
- input: number
18
- output: number
19
- reasoning: number
20
- cacheRead: number
21
- cacheWrite: number
22
- total: number
17
+ input: number;
18
+ output: number;
19
+ reasoning: number;
20
+ cacheRead: number;
21
+ cacheWrite: number;
22
+ total: number;
23
23
  }
24
24
 
25
25
  const isNumber = (value: unknown): value is number =>
26
- typeof value === "number" && Number.isFinite(value)
26
+ typeof value === "number" && Number.isFinite(value);
27
27
 
28
28
  export const normalizeTokens = (value: unknown): TokenUsage | undefined => {
29
- if (!value || typeof value !== "object") return undefined
30
- const tokens = value as TokenUsageLike
31
- const input = isNumber(tokens.input) ? tokens.input : undefined
32
- const output = isNumber(tokens.output) ? tokens.output : undefined
33
- const reasoning = isNumber(tokens.reasoning) ? tokens.reasoning : undefined
34
- const cacheRead = isNumber(tokens.cache?.read) ? tokens.cache.read : undefined
35
- const cacheWrite = isNumber(tokens.cache?.write) ? tokens.cache.write : undefined
29
+ if (!value || typeof value !== "object") return undefined;
30
+ const tokens = value as TokenUsageLike;
31
+ const input = isNumber(tokens.input) ? tokens.input : undefined;
32
+ const output = isNumber(tokens.output) ? tokens.output : undefined;
33
+ const reasoning = isNumber(tokens.reasoning) ? tokens.reasoning : undefined;
34
+ const cacheRead = isNumber(tokens.cache?.read) ? tokens.cache.read : undefined;
35
+ const cacheWrite = isNumber(tokens.cache?.write) ? tokens.cache.write : undefined;
36
36
 
37
37
  // An object with no numeric token fields is missing data, not zero usage.
38
38
  if (
@@ -42,7 +42,7 @@ export const normalizeTokens = (value: unknown): TokenUsage | undefined => {
42
42
  cacheRead === undefined &&
43
43
  cacheWrite === undefined
44
44
  ) {
45
- return undefined
45
+ return undefined;
46
46
  }
47
47
 
48
48
  return {
@@ -53,21 +53,21 @@ export const normalizeTokens = (value: unknown): TokenUsage | undefined => {
53
53
  read: cacheRead ?? 0,
54
54
  write: cacheWrite ?? 0,
55
55
  },
56
- }
57
- }
56
+ };
57
+ };
58
58
 
59
59
  export const tokensUsed = (tokens: TokenUsage): number =>
60
- tokens.input + tokens.output + tokens.reasoning
60
+ tokens.input + tokens.output + tokens.reasoning;
61
61
 
62
62
  export const tokensTotal = (tokens: TokenUsage): number =>
63
- tokensUsed(tokens) + tokens.cache.read + tokens.cache.write
63
+ tokensUsed(tokens) + tokens.cache.read + tokens.cache.write;
64
64
 
65
65
  export const addTokens = (
66
66
  left?: TokenUsage,
67
67
  right?: TokenUsage,
68
68
  ): TokenUsage | undefined => {
69
- if (!left) return right
70
- if (!right) return left
69
+ if (!left) return right;
70
+ if (!right) return left;
71
71
  return {
72
72
  input: left.input + right.input,
73
73
  output: left.output + right.output,
@@ -76,24 +76,24 @@ export const addTokens = (
76
76
  read: left.cache.read + right.cache.read,
77
77
  write: left.cache.write + right.cache.write,
78
78
  },
79
- }
80
- }
79
+ };
80
+ };
81
81
 
82
82
  export const CONTEXT_USAGE = {
83
83
  usage: "usage",
84
84
  empty: "empty",
85
85
  missing: "missing",
86
- } as const
86
+ } as const;
87
87
 
88
88
  export type ContextUsage =
89
89
  | {
90
- kind: typeof CONTEXT_USAGE.usage
91
- total: number
92
- lastRequest: LastRequestUsage
93
- model?: ModelRefLike
90
+ kind: typeof CONTEXT_USAGE.usage;
91
+ total: number;
92
+ lastRequest: LastRequestUsage;
93
+ model?: ModelRefLike;
94
94
  }
95
95
  | { kind: typeof CONTEXT_USAGE.empty }
96
- | { kind: typeof CONTEXT_USAGE.missing }
96
+ | { kind: typeof CONTEXT_USAGE.missing };
97
97
 
98
98
  /**
99
99
  * Reproduce the UI's context number: the last assistant step after the last
@@ -108,31 +108,31 @@ export const contextFromMessages = (
108
108
  messages: readonly UsageMessage[],
109
109
  revertMessageID?: string,
110
110
  ): ContextUsage | undefined => {
111
- let end = messages.length
111
+ let end = messages.length;
112
112
  if (revertMessageID) {
113
- const boundary = messages.findIndex((message) => message?.id === revertMessageID)
114
- if (boundary === -1) return undefined
115
- end = boundary
113
+ const boundary = messages.findIndex((message) => message?.id === revertMessageID);
114
+ if (boundary === -1) return undefined;
115
+ end = boundary;
116
116
  }
117
117
 
118
- let compactionIndex = -1
118
+ let compactionIndex = -1;
119
119
  for (let index = end - 1; index >= 0; index--) {
120
- const message = messages[index]
121
- if (!message) continue
120
+ const message = messages[index];
121
+ if (!message) continue;
122
122
  if (
123
123
  message.type === MESSAGE_TYPE.compaction &&
124
124
  message.status === MESSAGE_STATUS.completed
125
125
  ) {
126
- compactionIndex = index
127
- break
126
+ compactionIndex = index;
127
+ break;
128
128
  }
129
129
  }
130
130
 
131
131
  for (let index = end - 1; index > compactionIndex; index--) {
132
- const message = messages[index]
133
- if (!message || message.type !== MESSAGE_TYPE.assistant) continue
134
- const tokens = normalizeTokens(message.tokens)
135
- if (!tokens) return { kind: CONTEXT_USAGE.missing }
132
+ const message = messages[index];
133
+ if (!message || message.type !== MESSAGE_TYPE.assistant) continue;
134
+ const tokens = normalizeTokens(message.tokens);
135
+ if (!tokens) return { kind: CONTEXT_USAGE.missing };
136
136
  return {
137
137
  kind: CONTEXT_USAGE.usage,
138
138
  total: tokensTotal(tokens),
@@ -145,10 +145,10 @@ export const contextFromMessages = (
145
145
  total: tokensTotal(tokens),
146
146
  },
147
147
  model: message.model,
148
- }
148
+ };
149
149
  }
150
- return { kind: CONTEXT_USAGE.empty }
151
- }
150
+ return { kind: CONTEXT_USAGE.empty };
151
+ };
152
152
 
153
153
  export const TOKEN_USAGE_SCHEMA = {
154
154
  type: "object",
@@ -168,4 +168,4 @@ export const TOKEN_USAGE_SCHEMA = {
168
168
  },
169
169
  required: ["input", "output", "reasoning", "cache"],
170
170
  additionalProperties: false,
171
- } as const
171
+ } as const;