@cortexkit/opencode-antigravity-auth 3.0.6 → 3.0.7

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 (164) hide show
  1. package/dist/cli.js +620 -120
  2. package/dist/cli.js.map +4 -4
  3. package/dist/cli.meta.json +128 -44
  4. package/dist/index.js +1469 -675
  5. package/dist/index.js.map +4 -4
  6. package/dist/index.meta.json +154 -73
  7. package/dist/server.js +1075 -280
  8. package/dist/server.js.map +4 -4
  9. package/dist/server.meta.json +1 -1
  10. package/dist/src/common-auth-embedded/claustrum/consumer.d.ts +11 -1
  11. package/dist/src/common-auth-embedded/claustrum/consumer.js +38 -1
  12. package/dist/src/common-auth-embedded/claustrum/custody.d.ts +29 -0
  13. package/dist/src/common-auth-embedded/claustrum/custody.js +17 -0
  14. package/dist/src/common-auth-embedded/claustrum/index.d.ts +1 -1
  15. package/dist/src/common-auth-embedded/fs/refresh-file-lock.d.ts +8 -2
  16. package/dist/src/common-auth-embedded/fs/refresh-file-lock.js +6 -1
  17. package/dist/src/common-auth-embedded/pi-slot/lock.js +7 -3
  18. package/dist/src/common-auth-embedded/quota/format.d.ts +14 -3
  19. package/dist/src/common-auth-embedded/quota/format.js +135 -18
  20. package/dist/src/common-auth-embedded/quota/index.d.ts +1 -1
  21. package/dist/src/common-auth-embedded/quota/index.js +1 -1
  22. package/dist/src/common-auth-embedded/setup/command-error.d.ts +32 -0
  23. package/dist/src/common-auth-embedded/setup/command-error.js +79 -0
  24. package/dist/src/common-auth-embedded/setup/command-runner.js +118 -24
  25. package/dist/src/common-auth-embedded/setup/command.js +75 -19
  26. package/dist/src/common-auth-embedded/setup/enroll.js +17 -5
  27. package/dist/src/common-auth-embedded/setup/index.d.ts +2 -1
  28. package/dist/src/common-auth-embedded/setup/index.js +1 -0
  29. package/dist/src/common-auth-embedded/setup/register.js +8 -2
  30. package/dist/src/common-auth-embedded/setup/types.d.ts +14 -0
  31. package/dist/src/common-auth-embedded/source-output.json +92 -76
  32. package/dist/src/common-auth-embedded/store/pool.d.ts +1 -1
  33. package/dist/src/common-auth-embedded/store/pool.js +0 -1
  34. package/dist/src/common-auth-embedded/store/refresh-lock.d.ts +0 -1
  35. package/dist/src/common-auth-embedded/store/refresh-lock.js +0 -1
  36. package/dist/src/ga/server/index.d.ts.map +1 -1
  37. package/dist/src/ga/server/index.js +4 -2
  38. package/dist/src/ga/server/index.js.map +1 -1
  39. package/dist/src/ga/tui/host-ga.d.ts.map +1 -1
  40. package/dist/src/ga/tui/host-ga.js +37 -3
  41. package/dist/src/ga/tui/host-ga.js.map +1 -1
  42. package/dist/src/plugin/config/schema.d.ts.map +1 -1
  43. package/dist/src/plugin/config/schema.js +2 -1
  44. package/dist/src/plugin/config/schema.js.map +1 -1
  45. package/dist/src/plugin/ga-location-services.d.ts.map +1 -1
  46. package/dist/src/plugin/ga-location-services.js +4 -3
  47. package/dist/src/plugin/ga-location-services.js.map +1 -1
  48. package/dist/src/plugin/gemini-dump.d.ts.map +1 -1
  49. package/dist/src/plugin/gemini-dump.js +2 -5
  50. package/dist/src/plugin/gemini-dump.js.map +1 -1
  51. package/dist/src/plugin/index.d.ts.map +1 -1
  52. package/dist/src/plugin/index.js +3 -3
  53. package/dist/src/plugin/index.js.map +1 -1
  54. package/dist/src/plugin/logging-utils.d.ts.map +1 -1
  55. package/dist/src/plugin/logging-utils.js +2 -5
  56. package/dist/src/plugin/logging-utils.js.map +1 -1
  57. package/dist/src/plugin/request.d.ts +1 -2
  58. package/dist/src/plugin/request.d.ts.map +1 -1
  59. package/dist/src/plugin/request.js +8 -12
  60. package/dist/src/plugin/request.js.map +1 -1
  61. package/dist/src/plugin/shared/request-services.d.ts +8 -1
  62. package/dist/src/plugin/shared/request-services.d.ts.map +1 -1
  63. package/dist/src/plugin/shared/request-services.js +140 -58
  64. package/dist/src/plugin/shared/request-services.js.map +1 -1
  65. package/dist/src/plugin/shared/vault-request-credentials.d.ts +12 -3
  66. package/dist/src/plugin/shared/vault-request-credentials.d.ts.map +1 -1
  67. package/dist/src/plugin/shared/vault-request-credentials.js +3 -0
  68. package/dist/src/plugin/shared/vault-request-credentials.js.map +1 -1
  69. package/dist/src/plugin/vault-custody.d.ts +14 -1
  70. package/dist/src/plugin/vault-custody.d.ts.map +1 -1
  71. package/dist/src/plugin/vault-custody.js +57 -10
  72. package/dist/src/plugin/vault-custody.js.map +1 -1
  73. package/dist/src/sidebar-projection.d.ts +4 -0
  74. package/dist/src/sidebar-projection.d.ts.map +1 -1
  75. package/dist/src/sidebar-projection.js +6 -2
  76. package/dist/src/sidebar-projection.js.map +1 -1
  77. package/dist/src/tui/quota-layout.d.ts +94 -0
  78. package/dist/src/tui/quota-layout.d.ts.map +1 -0
  79. package/dist/src/tui/quota-layout.js +139 -0
  80. package/dist/src/tui/quota-layout.js.map +1 -0
  81. package/dist/src/tui/route-layout.d.ts +8 -0
  82. package/dist/src/tui/route-layout.d.ts.map +1 -0
  83. package/dist/src/tui/route-layout.js +36 -0
  84. package/dist/src/tui/route-layout.js.map +1 -0
  85. package/dist/src/tui.d.ts.map +1 -1
  86. package/dist/src/tui.js +26 -128
  87. package/dist/src/tui.js.map +1 -1
  88. package/dist/tui-build-map.json +42 -13
  89. package/dist/tui-ga-build-map.json +46 -17
  90. package/dist/types/src/common-auth-embedded/claustrum/consumer.d.ts +11 -1
  91. package/dist/types/src/common-auth-embedded/claustrum/custody.d.ts +29 -0
  92. package/dist/types/src/common-auth-embedded/claustrum/index.d.ts +1 -1
  93. package/dist/types/src/common-auth-embedded/fs/refresh-file-lock.d.ts +8 -2
  94. package/dist/types/src/common-auth-embedded/quota/format.d.ts +14 -3
  95. package/dist/types/src/common-auth-embedded/quota/index.d.ts +1 -1
  96. package/dist/types/src/common-auth-embedded/setup/command-error.d.ts +32 -0
  97. package/dist/types/src/common-auth-embedded/setup/index.d.ts +2 -1
  98. package/dist/types/src/common-auth-embedded/setup/types.d.ts +14 -0
  99. package/dist/types/src/common-auth-embedded/store/pool.d.ts +1 -1
  100. package/dist/types/src/common-auth-embedded/store/refresh-lock.d.ts +0 -1
  101. package/dist/types/src/ga/server/index.d.ts.map +1 -1
  102. package/dist/types/src/plugin/config/schema.d.ts.map +1 -1
  103. package/dist/types/src/plugin/ga-location-services.d.ts.map +1 -1
  104. package/dist/types/src/plugin/gemini-dump.d.ts.map +1 -1
  105. package/dist/types/src/plugin/index.d.ts.map +1 -1
  106. package/dist/types/src/plugin/logging-utils.d.ts.map +1 -1
  107. package/dist/types/src/plugin/request.d.ts +1 -2
  108. package/dist/types/src/plugin/request.d.ts.map +1 -1
  109. package/dist/types/src/plugin/shared/request-services.d.ts +8 -1
  110. package/dist/types/src/plugin/shared/request-services.d.ts.map +1 -1
  111. package/dist/types/src/plugin/shared/vault-request-credentials.d.ts +12 -3
  112. package/dist/types/src/plugin/shared/vault-request-credentials.d.ts.map +1 -1
  113. package/dist/types/src/plugin/vault-custody.d.ts +14 -1
  114. package/dist/types/src/plugin/vault-custody.d.ts.map +1 -1
  115. package/package.json +3 -3
  116. package/src/common-auth-embedded/claustrum/consumer.d.ts +11 -1
  117. package/src/common-auth-embedded/claustrum/consumer.js +38 -1
  118. package/src/common-auth-embedded/claustrum/custody.d.ts +29 -0
  119. package/src/common-auth-embedded/claustrum/custody.js +17 -0
  120. package/src/common-auth-embedded/claustrum/index.d.ts +1 -1
  121. package/src/common-auth-embedded/fs/refresh-file-lock.d.ts +8 -2
  122. package/src/common-auth-embedded/fs/refresh-file-lock.js +6 -1
  123. package/src/common-auth-embedded/pi-slot/lock.js +7 -3
  124. package/src/common-auth-embedded/quota/format.d.ts +14 -3
  125. package/src/common-auth-embedded/quota/format.js +135 -18
  126. package/src/common-auth-embedded/quota/index.d.ts +1 -1
  127. package/src/common-auth-embedded/quota/index.js +1 -1
  128. package/src/common-auth-embedded/setup/command-error.d.ts +32 -0
  129. package/src/common-auth-embedded/setup/command-error.js +79 -0
  130. package/src/common-auth-embedded/setup/command-runner.js +118 -24
  131. package/src/common-auth-embedded/setup/command.js +75 -19
  132. package/src/common-auth-embedded/setup/enroll.js +17 -5
  133. package/src/common-auth-embedded/setup/index.d.ts +2 -1
  134. package/src/common-auth-embedded/setup/index.js +1 -0
  135. package/src/common-auth-embedded/setup/register.js +8 -2
  136. package/src/common-auth-embedded/setup/types.d.ts +14 -0
  137. package/src/common-auth-embedded/source-output.json +92 -76
  138. package/src/common-auth-embedded/store/pool.d.ts +1 -1
  139. package/src/common-auth-embedded/store/pool.js +0 -1
  140. package/src/common-auth-embedded/store/refresh-lock.d.ts +0 -1
  141. package/src/common-auth-embedded/store/refresh-lock.js +0 -1
  142. package/src/tui-compiled/common-auth-embedded/quota/format.js +135 -18
  143. package/src/tui-compiled/common-auth-embedded/quota/index.js +1 -1
  144. package/src/tui-compiled/tui/quota-layout.js +378 -0
  145. package/src/tui-compiled/tui/route-layout.js +30 -0
  146. package/src/tui-compiled/tui.js +145 -427
  147. package/src/tui-ga-compiled/host-ga.js +82 -52
  148. package/src/tui-ga-compiled/shared/6ca47cd4ec33fe72-route-layout.js +30 -0
  149. package/src/tui-ga-compiled/shared/8ae1927e5910ea81-sidebar-projection.js +2 -2
  150. package/src/tui-ga-compiled/shared/aef2402f8fc342f0-format.js +135 -18
  151. package/src/tui-ga-compiled/shared/bf17cbee28e58197-index.js +1 -1
  152. package/src/tui-ga-compiled/shared/dd4c9e1a907f8ef0-quota-layout.js +378 -0
  153. package/src/tui-ga-raw/host-ga.tsx +71 -30
  154. package/src/tui-ga-raw/shared/6ca47cd4ec33fe72-route-layout.js +30 -0
  155. package/src/tui-ga-raw/shared/8ae1927e5910ea81-sidebar-projection.js +2 -2
  156. package/src/tui-ga-raw/shared/aef2402f8fc342f0-format.js +135 -18
  157. package/src/tui-ga-raw/shared/bf17cbee28e58197-index.js +1 -1
  158. package/src/tui-ga-raw/shared/dd4c9e1a907f8ef0-quota-layout.tsx +391 -0
  159. package/src/tui-raw/common-auth-embedded/quota/format.js +135 -18
  160. package/src/tui-raw/common-auth-embedded/quota/index.js +1 -1
  161. package/src/tui-raw/tui/quota-layout.tsx +391 -0
  162. package/src/tui-raw/tui/route-layout.js +30 -0
  163. package/src/tui-raw/tui.tsx +50 -309
  164. package/src/tui.tsx +50 -309
@@ -63,8 +63,12 @@ export function quotaWindowName(limit) {
63
63
  function clampPercent(remaining) {
64
64
  return Math.min(100, Math.max(0, remaining));
65
65
  }
66
+ /** A remaining percentage as the whole number every form shows. */
67
+ function wholePercent(remaining) {
68
+ return Math.round(clampPercent(remaining));
69
+ }
66
70
  function percentLeft(remaining) {
67
- return `${Math.round(clampPercent(remaining))}% left`;
71
+ return `${wholePercent(remaining)}% left`;
68
72
  }
69
73
  function resetDetails(resetsAt, checkedAt, now) {
70
74
  const at = resetsAt === undefined ? Number.NaN : Date.parse(resetsAt);
@@ -98,6 +102,16 @@ function displayOrder(left, right) {
98
102
  * and the optional age keep the shared formatter's wording.
99
103
  */
100
104
  export function quotaWindowParts(quota, options) {
105
+ const { windows, age } = scopedWindowParts(quota, options);
106
+ const parts = windows.map(({ part }) => part);
107
+ return age === undefined ? { windows: parts } : { windows: parts, age };
108
+ }
109
+ /**
110
+ * `quotaWindowParts` with each window's scope kept beside it. The narrow form
111
+ * needs the scope to name and pick model-scoped windows; the public parts do
112
+ * not carry it, so their shape stays as it was.
113
+ */
114
+ function scopedWindowParts(quota, options) {
101
115
  if (!quota)
102
116
  return { windows: [] };
103
117
  const { now } = options;
@@ -107,12 +121,15 @@ export function quotaWindowParts(quota, options) {
107
121
  const windows = readings.map((limit) => {
108
122
  const remainingPercent = clampPercent(limit.remainingPercent ?? 0);
109
123
  return {
110
- kind: 'window',
111
- name: quotaWindowName(limit),
112
- figure: percentLeft(limit.remainingPercent ?? 0),
113
- remainingPercent,
114
- checkedAt: limit.checkedAt,
115
- ...resetDetails(limit.resetsAt, limit.checkedAt, now),
124
+ scope: limit.scope,
125
+ part: {
126
+ kind: 'window',
127
+ name: quotaWindowName(limit),
128
+ figure: percentLeft(limit.remainingPercent ?? 0),
129
+ remainingPercent,
130
+ checkedAt: limit.checkedAt,
131
+ ...resetDetails(limit.resetsAt, limit.checkedAt, now),
132
+ },
116
133
  };
117
134
  });
118
135
  const budget = quota.budget;
@@ -124,32 +141,131 @@ export function quotaWindowParts(quota, options) {
124
141
  ? `credits ${percentLeft(remainingPercent)}`
125
142
  : 'credits available';
126
143
  windows.push({
127
- kind: 'budget',
128
- name: 'credits',
129
- figure,
130
- ...(remainingPercent === undefined
131
- ? {}
132
- : { remainingPercent: clampPercent(remainingPercent) }),
133
- checkedAt: budget.checkedAt,
134
- ...resetDetails(budget.resetsAt, budget.checkedAt, now),
144
+ scope: ALL_SCOPE,
145
+ spent: budget.reached,
146
+ part: {
147
+ kind: 'budget',
148
+ name: 'credits',
149
+ figure,
150
+ ...(remainingPercent === undefined
151
+ ? {}
152
+ : { remainingPercent: clampPercent(remainingPercent) }),
153
+ checkedAt: budget.checkedAt,
154
+ ...resetDetails(budget.resetsAt, budget.checkedAt, now),
155
+ },
135
156
  });
136
157
  }
137
158
  if (windows.length > 0) {
138
- const age = now - Math.min(...windows.map((part) => part.checkedAt));
159
+ const age = now - Math.min(...windows.map(({ part }) => part.checkedAt));
139
160
  if (age > (options.staleAfterMs ?? QUOTA_STALE_AFTER_MS)) {
140
161
  return { windows, age: `checked ${shortSpan(age)} ago` };
141
162
  }
142
163
  }
143
164
  return { windows };
144
165
  }
166
+ /**
167
+ * Whether the reading is stale: true exactly when the compact and full forms
168
+ * would end with `checked … ago`. The narrow form leaves that age out to stay
169
+ * short, so a caller drawing a narrow line uses this to dim it instead.
170
+ */
171
+ export function isQuotaStale(quota, options) {
172
+ return scopedWindowParts(quota, options).age !== undefined;
173
+ }
174
+ /**
175
+ * The model a scope names, as a display name: any namespace before a colon
176
+ * is dropped (`model:claude-opus-4` → `claude-opus-4`) and the first letter is
177
+ * capitalized (`fable` → `Fable`); the rest stays as written.
178
+ */
179
+ function scopeDisplayName(scope) {
180
+ const model = scope.slice(scope.indexOf(':') + 1);
181
+ return model.charAt(0).toUpperCase() + model.slice(1);
182
+ }
183
+ /**
184
+ * Short names for the model scopes in one row: the first two letters of each
185
+ * display name. Names that would read the same for different models are each
186
+ * extended one letter at a time until they differ (`Fable` and `Fabric` read
187
+ * `Fabl` and `Fabr`); a name that runs out of letters stays whole.
188
+ */
189
+ function scopeAbbreviations(scopes) {
190
+ const names = [...new Set(scopes.map(scopeDisplayName))];
191
+ const lengths = new Map(names.map((name) => [name, 2]));
192
+ const short = (name) => name.slice(0, lengths.get(name));
193
+ for (;;) {
194
+ const byShort = new Map();
195
+ for (const name of names) {
196
+ byShort.set(short(name), [...(byShort.get(short(name)) ?? []), name]);
197
+ }
198
+ let extended = false;
199
+ for (const group of byShort.values()) {
200
+ if (group.length < 2)
201
+ continue;
202
+ for (const name of group) {
203
+ const length = lengths.get(name) ?? 2;
204
+ if (length < name.length) {
205
+ lengths.set(name, length + 1);
206
+ extended = true;
207
+ }
208
+ }
209
+ }
210
+ if (!extended)
211
+ break;
212
+ }
213
+ return new Map(scopes.map((scope) => [scope, short(scopeDisplayName(scope))]));
214
+ }
215
+ /**
216
+ * The narrow form's pieces: every account-wide window as `5h 92%`, then at
217
+ * most one more item, with ` left` added once to the last piece. That item is
218
+ * the tightest (least remaining) of the model-scoped windows and the credit
219
+ * budget, so the line stays bounded whatever the account carries: a scoped
220
+ * window reads under its short model name (`Fa 98%`), the budget as `cr 75%`
221
+ * (`cr 0%` when spent). On a tie a scoped window wins over the budget, and
222
+ * among scoped windows the first in display order wins. The choice depends
223
+ * only on the reading, never on a width. A budget without a percentage cannot
224
+ * be chosen; when nothing at all is shown it keeps its compact wording
225
+ * (`credits available`). No age is added; `isQuotaStale` says whether the
226
+ * reading is stale. A window whose reset arrived after the reading keeps its
227
+ * figure, as in the other forms.
228
+ */
229
+ function narrowTextParts(quota, options) {
230
+ const { windows } = scopedWindowParts(quota, options);
231
+ const general = windows.filter(({ part, scope }) => part.kind === 'window' && scope === ALL_SCOPE);
232
+ const scoped = windows.filter(({ part, scope }) => part.kind === 'window' && scope !== ALL_SCOPE);
233
+ const abbreviations = scopeAbbreviations(scoped.map(({ scope }) => scope));
234
+ // The items competing for the one slot after the account-wide windows, in
235
+ // tie-break order: scoped windows in display order, then the budget. A
236
+ // spent budget counts as 0% left; one without a percentage cannot compete.
237
+ const budget = windows.find(({ part }) => part.kind === 'budget');
238
+ const budgetLeft = budget?.spent ? 0 : budget?.part.remainingPercent;
239
+ const extras = [
240
+ ...scoped.map(({ scope, part }) => ({
241
+ label: abbreviations.get(scope) ?? scope,
242
+ remaining: part.remainingPercent ?? 0,
243
+ })),
244
+ ...(budgetLeft === undefined
245
+ ? []
246
+ : [{ label: 'cr', remaining: budgetLeft }]),
247
+ ];
248
+ // Strictly lower wins, so on a tie the earlier item keeps the slot.
249
+ const tightest = extras.reduce((best, item) => best === undefined || item.remaining < best.remaining ? item : best, undefined);
250
+ const pieces = general.map(({ part }) => `${part.name} ${wholePercent(part.remainingPercent ?? 0)}%`);
251
+ if (tightest) {
252
+ pieces.push(`${tightest.label} ${wholePercent(tightest.remaining)}%`);
253
+ }
254
+ if (pieces.length === 0)
255
+ return windows.map(({ part }) => part.figure);
256
+ return pieces.map((piece, index) => index === pieces.length - 1 ? `${piece} left` : piece);
257
+ }
145
258
  /**
146
259
  * The pieces of a quota text, in order: one per reported window
147
260
  * (`5h 58% left, resets 2h`), then the credit budget when there is one, then
148
261
  * `checked 3h ago` when the oldest piece shown is stale. Empty when nothing
149
262
  * was reported; `formatQuota` turns that into a sentence. A caller printing
150
- * one line per window (the terminal auth menu) uses these directly.
263
+ * one line per window (the terminal auth menu) uses these directly. In the
264
+ * narrow form the pieces are the narrow line's (`5h 92%`, `7d 66% left`).
151
265
  */
152
266
  export function quotaTextParts(quota, options) {
267
+ if (options.form === 'narrow')
268
+ return narrowTextParts(quota, options);
153
269
  const parts = quotaWindowParts(quota, options);
154
270
  const full = (options.form ?? 'full') === 'full';
155
271
  const text = parts.windows.map((part) => {
@@ -160,7 +276,8 @@ export function quotaTextParts(quota, options) {
160
276
  }
161
277
  /**
162
278
  * A row's quota as one line: `5h 58% left · 7d 90% left` in the compact
163
- * form, with `, resets 2h` after each window in the full form. With nothing
279
+ * form, with `, resets 2h` after each window in the full form, and
280
+ * `5h 58% · 7d 90% left` in the narrow form. With nothing
164
281
  * reported it is `NO_QUOTA_READING`, or `NO_LIMITS_REPORTED` when the
165
282
  * provider answered without a limit for any window.
166
283
  */
@@ -1,5 +1,5 @@
1
1
  export { quotaCodec } from "./ca21dc417b36dc5d-codec.js";
2
- export { formatQuota, NO_LIMITS_REPORTED, NO_QUOTA_READING, QUOTA_STALE_AFTER_MS, quotaTextParts, quotaWindowName, quotaWindowParts, } from "./aef2402f8fc342f0-format.js";
2
+ export { formatQuota, isQuotaStale, NO_LIMITS_REPORTED, NO_QUOTA_READING, QUOTA_STALE_AFTER_MS, quotaTextParts, quotaWindowName, quotaWindowParts, } from "./aef2402f8fc342f0-format.js";
3
3
  export { ALL_SCOPE, DEFAULT_REQUIRED_LABELS, decodeQuotaMap, emptyQuotaMap, isQuotaMap, } from "./77138df80c7ae006-map.js";
4
4
  export { isQuotaObservation, mergeQuotaObservation, QuotaCodecError, } from "./ecc214300932dfb6-merge.js";
5
5
  export { budgetExhaustedResetAt, futureResetAt, projectQuota, readsExhausted, } from "./1ff34afa7c42d196-projection.js";
@@ -0,0 +1,391 @@
1
+ /** @jsxImportSource @opentui/solid */
2
+
3
+ /**
4
+ * Quota layout shared by the OpenCode 1 sidebar (`tui.tsx`) and the OpenCode 2
5
+ * sidebar (`ga/tui/host-ga.tsx`): one account block with its header line, the
6
+ * reading age once per account, plain-word group headers and one width-fitted
7
+ * row per quota window.
8
+ *
9
+ * Each renderer supplies its own data source and colors. This module imports
10
+ * only the pure quota presentation helpers, so it stays inside both TUI
11
+ * import graphs: no sidebar file, preferences file, RPC, account store,
12
+ * OAuth, token or host SDK module, and nothing that writes to the terminal.
13
+ */
14
+
15
+ import type { RGBA } from "@opentui/core"
16
+ import { For, type JSX, Show } from "solid-js"
17
+ import {
18
+ NO_QUOTA_READING,
19
+ type QuotaReading,
20
+ type QuotaWindowText,
21
+ quotaReadingAge,
22
+ quotaWindowText,
23
+ } from "./98cc0c186c46c42d-quota-presentation.js"
24
+
25
+ export type QuotaTone = 'ok' | 'warn' | 'err' | 'muted' | 'accent' | 'text'
26
+
27
+ /** A text color; `undefined` leaves the host's default color in place. */
28
+ export type QuotaColor = string | RGBA | undefined
29
+
30
+ /**
31
+ * How a renderer colors the quota layout. OpenCode 1 maps these onto its live
32
+ * host theme; a renderer without a theme returns `undefined` everywhere.
33
+ */
34
+ export interface QuotaPalette {
35
+ readonly text: () => QuotaColor
36
+ readonly muted: () => QuotaColor
37
+ readonly tone: (tone: QuotaTone) => QuotaColor
38
+ }
39
+
40
+ /** The bar and threshold settings the layout reads. */
41
+ export interface QuotaAppearance {
42
+ readonly barWidth: number
43
+ readonly barFilledChar: string
44
+ readonly barEmptyChar: string
45
+ readonly warnThreshold: number
46
+ readonly errorThreshold: number
47
+ }
48
+
49
+ /**
50
+ * The OpenCode 1 preference defaults for the quota bar, for a renderer that
51
+ * has no preferences file. A test keeps it equal to `DEFAULT_PREFS.appearance`.
52
+ */
53
+ export const DEFAULT_QUOTA_APPEARANCE: QuotaAppearance = {
54
+ barWidth: 10,
55
+ barFilledChar: '█',
56
+ barEmptyChar: '░',
57
+ warnThreshold: 50,
58
+ errorThreshold: 80,
59
+ }
60
+
61
+ export type QuotaGroupKey = 'gemini' | 'non-gemini'
62
+
63
+ export interface QuotaLayoutWindow {
64
+ readonly window: 'weekly' | '5h'
65
+ readonly remainingPercent: number
66
+ readonly resetAt?: number
67
+ readonly checkedAt?: number
68
+ }
69
+
70
+ export interface QuotaLayoutEntry {
71
+ readonly remainingPercent: number
72
+ readonly resetAt?: number
73
+ readonly checkedAt?: number
74
+ readonly windows?: readonly QuotaLayoutWindow[]
75
+ }
76
+
77
+ /** The account fields the quota layout reads, and nothing else. */
78
+ export interface QuotaLayoutAccount {
79
+ readonly label: string
80
+ readonly quota: {
81
+ readonly gemini?: QuotaLayoutEntry
82
+ readonly 'non-gemini'?: QuotaLayoutEntry
83
+ }
84
+ }
85
+
86
+ // Group headers name each quota pool in plain words.
87
+ export const GROUP_NAMES: Record<QuotaGroupKey, string> = {
88
+ gemini: 'Gemini',
89
+ 'non-gemini': 'Other models',
90
+ }
91
+
92
+ export const QUOTA_ORDER: readonly QuotaGroupKey[] = ['gemini', 'non-gemini']
93
+
94
+ // Shown instead of a stored figure once the window has reset since it was
95
+ // read: the stored figure describes the window before the reset.
96
+ export const RESET_SINCE_READING = 'reset since last check'
97
+
98
+ // Content width used until the first layout pass reports the real one. It
99
+ // fits the narrowest OpenCode sidebar.
100
+ export const ASSUMED_CONTENT_WIDTH = 28
101
+
102
+ // A window row is `<name> <bar> <figure> <reset>`. The bar shrinks to this
103
+ // before the reset column is dropped; below it the bar is dropped too.
104
+ const MIN_BAR_WIDTH = 4
105
+ // The widest figure the formatter writes: `100% left`.
106
+ const FIGURE_WIDTH = 9
107
+ // A window name (`5h`, `7d`) and the space after it.
108
+ const NAME_WIDTH = 3
109
+
110
+ export function quotaTone(
111
+ usedPct: number,
112
+ appearance: QuotaAppearance,
113
+ ): QuotaTone {
114
+ if (usedPct < appearance.warnThreshold) return 'ok'
115
+ if (usedPct < appearance.errorThreshold) return 'warn'
116
+ return 'err'
117
+ }
118
+
119
+ interface BarSegment {
120
+ text: string
121
+ tone: QuotaTone
122
+ }
123
+
124
+ function quotaBarSegments(
125
+ remainingPct: number,
126
+ width: number,
127
+ appearance: QuotaAppearance,
128
+ ): BarSegment[] {
129
+ const usedCells = Math.max(
130
+ 0,
131
+ Math.min(Math.round((remainingPct / 100) * width), width),
132
+ )
133
+ const tone = quotaTone(100 - remainingPct, appearance)
134
+ return [
135
+ { text: appearance.barFilledChar.repeat(usedCells), tone },
136
+ { text: appearance.barEmptyChar.repeat(width - usedCells), tone },
137
+ ].filter((segment) => segment.text.length > 0)
138
+ }
139
+
140
+ // --- Width-fitted quota rows ------------------------------------------------
141
+ //
142
+ // The host sidebars are about 20 to 48 columns wide. Every quota line is
143
+ // sized from the measured content width so that it fits on one line: text is
144
+ // never left to the renderer's wrapping, which breaks a row mid-word or down
145
+ // to one character per line once the right-hand column runs out of room.
146
+
147
+ export interface AccountWindow {
148
+ key: QuotaGroupKey
149
+ reading: QuotaReading
150
+ text: QuotaWindowText
151
+ }
152
+
153
+ // One entry per stored window, in group order. A pool stored before
154
+ // per-window readings existed contributes one window without a name.
155
+ export function accountWindows(
156
+ account: QuotaLayoutAccount,
157
+ now: number,
158
+ ): AccountWindow[] {
159
+ const result: AccountWindow[] = []
160
+ for (const key of QUOTA_ORDER) {
161
+ const entry = account.quota[key]
162
+ if (!entry) continue
163
+ const readings: QuotaReading[] =
164
+ entry.windows && entry.windows.length > 0
165
+ ? entry.windows.map((window) => ({
166
+ window: window.window,
167
+ remainingPercent: window.remainingPercent,
168
+ resetAt: window.resetAt,
169
+ checkedAt: window.checkedAt,
170
+ }))
171
+ : [
172
+ {
173
+ remainingPercent: entry.remainingPercent,
174
+ resetAt: entry.resetAt,
175
+ checkedAt: entry.checkedAt,
176
+ },
177
+ ]
178
+ for (const reading of readings)
179
+ result.push({ key, reading, text: quotaWindowText(reading, now) })
180
+ }
181
+ return result
182
+ }
183
+
184
+ // A window shows its figure only while no reset has happened since it was
185
+ // read and it has a read time at all.
186
+ export function showsFigure(window: AccountWindow): boolean {
187
+ return window.text.remaining != null && !window.text.resetSinceReading
188
+ }
189
+
190
+ interface WindowRowLayout {
191
+ bar: number
192
+ reset: boolean
193
+ }
194
+
195
+ // Column widths for one `<name> <bar> <figure> <reset>` row within `width`
196
+ // cells. The bar shrinks first, down to MIN_BAR_WIDTH; then the reset column
197
+ // is dropped; when even that leaves less than MIN_BAR_WIDTH cells for the
198
+ // bar, the bar is left out and the row is just the name and figure.
199
+ function windowRowLayout(
200
+ width: number,
201
+ named: boolean,
202
+ resetWidth: number,
203
+ maxBar: number,
204
+ ): WindowRowLayout {
205
+ const room = width - (named ? NAME_WIDTH : 0) - FIGURE_WIDTH
206
+ if (resetWidth > 0) {
207
+ const bar = Math.min(maxBar, room - resetWidth - 2)
208
+ if (bar >= MIN_BAR_WIDTH) return { bar, reset: true }
209
+ }
210
+ const bar = Math.min(maxBar, room - 1)
211
+ return { bar: bar >= MIN_BAR_WIDTH ? bar : 0, reset: false }
212
+ }
213
+
214
+ // Shorten a label to `max` cells, marking the cut with an ellipsis.
215
+ export function fitLabel(label: string, max: number): string {
216
+ if (label.length <= max) return label
217
+ if (max <= 0) return ''
218
+ if (max === 1) return '…'
219
+ return `${label.slice(0, max - 1)}…`
220
+ }
221
+
222
+ // One quota window on one line. The bar fills with remaining quota; the
223
+ // figure and reset come from the shared formatter. A window that reset since
224
+ // its reading shows that fact, dim, in place of the stale figure.
225
+ function WindowRow(props: {
226
+ palette: QuotaPalette
227
+ appearance: QuotaAppearance
228
+ window: AccountWindow
229
+ layout: WindowRowLayout
230
+ }): JSX.Element {
231
+ const text = () => props.window.text
232
+ const tone = () => quotaTone(100 - (text().remaining ?? 0), props.appearance)
233
+ return (
234
+ <box width='100%' flexDirection='row'>
235
+ <Show when={text().name !== ''}>
236
+ <text
237
+ width={NAME_WIDTH}
238
+ flexShrink={0}
239
+ wrapMode='none'
240
+ fg={props.palette.muted()}
241
+ >
242
+ {text().name}
243
+ </text>
244
+ </Show>
245
+ <Show
246
+ when={showsFigure(props.window)}
247
+ fallback={
248
+ <text flexShrink={1} wrapMode='none' fg={props.palette.muted()}>
249
+ {text().resetSinceReading ? RESET_SINCE_READING : text().figure}
250
+ </text>
251
+ }
252
+ >
253
+ <Show when={props.layout.bar > 0}>
254
+ <box width={props.layout.bar + 1} flexShrink={0} flexDirection='row'>
255
+ <For
256
+ each={quotaBarSegments(
257
+ text().remaining ?? 0,
258
+ props.layout.bar,
259
+ props.appearance,
260
+ )}
261
+ >
262
+ {(segment) => (
263
+ <text wrapMode='none' fg={props.palette.tone(segment.tone)}>
264
+ {segment.text}
265
+ </text>
266
+ )}
267
+ </For>
268
+ </box>
269
+ </Show>
270
+ <text flexShrink={0} wrapMode='none' fg={props.palette.tone(tone())}>
271
+ {text().figure}
272
+ </text>
273
+ <box flexGrow={1} />
274
+ <Show when={props.layout.reset && text().reset !== ''}>
275
+ <text flexShrink={0} wrapMode='none' fg={props.palette.muted()}>
276
+ {text().reset}
277
+ </text>
278
+ </Show>
279
+ </Show>
280
+ </box>
281
+ )
282
+ }
283
+
284
+ /**
285
+ * One account's quota block. The header line is the account label, the
286
+ * reading age when the oldest reading is stale, and the caller's status word
287
+ * right-aligned; the age moves to its own dim line when the three do not
288
+ * fit. Below it each quota group present gets a plain-word header and one
289
+ * row per window, or a single dim line when there is no reading at all.
290
+ * `children` render after the quota rows (the OpenCode 2 health line).
291
+ */
292
+ export function QuotaAccountBlock(props: {
293
+ palette: QuotaPalette
294
+ appearance: QuotaAppearance
295
+ account: QuotaLayoutAccount
296
+ status: string
297
+ statusTone: QuotaTone
298
+ now: () => number
299
+ width: () => number
300
+ marginTop?: number
301
+ children?: JSX.Element
302
+ }): JSX.Element {
303
+ const windows = () => accountWindows(props.account, props.now())
304
+ const age = () =>
305
+ quotaReadingAge(
306
+ windows().map((window) => window.reading),
307
+ props.now(),
308
+ )
309
+ const ageInline = () =>
310
+ age() !== '' &&
311
+ props.account.label.length + 3 + age().length + 1 + props.status.length <=
312
+ props.width()
313
+ const label = () =>
314
+ fitLabel(props.account.label, props.width() - 1 - props.status.length)
315
+ // One reset column width per account, so bars line up across its rows.
316
+ const resetWidth = () =>
317
+ Math.max(
318
+ 0,
319
+ ...windows()
320
+ .filter(showsFigure)
321
+ .map((window) => window.text.reset.length),
322
+ )
323
+ const layoutFor = (window: AccountWindow) =>
324
+ windowRowLayout(
325
+ props.width(),
326
+ window.text.name !== '',
327
+ resetWidth(),
328
+ props.appearance.barWidth,
329
+ )
330
+ const groups = () =>
331
+ QUOTA_ORDER.map((key) => ({
332
+ key,
333
+ windows: windows().filter((window) => window.key === key),
334
+ })).filter((group) => group.windows.length > 0)
335
+ return (
336
+ <box width='100%' flexDirection='column' marginTop={props.marginTop ?? 0}>
337
+ <box width='100%' flexDirection='row'>
338
+ <text flexShrink={0} wrapMode='none' fg={props.palette.text()}>
339
+ <b>{label()}</b>
340
+ </text>
341
+ <Show when={ageInline()}>
342
+ <text flexShrink={0} wrapMode='none' fg={props.palette.muted()}>
343
+ {` · ${age()}`}
344
+ </text>
345
+ </Show>
346
+ <box flexGrow={1} />
347
+ <text
348
+ flexShrink={0}
349
+ wrapMode='none'
350
+ fg={props.palette.tone(props.statusTone)}
351
+ >
352
+ <b>{props.status}</b>
353
+ </text>
354
+ </box>
355
+ <Show when={age() !== '' && !ageInline()}>
356
+ <text wrapMode='none' fg={props.palette.muted()}>
357
+ {age()}
358
+ </text>
359
+ </Show>
360
+ <Show
361
+ when={groups().length > 0}
362
+ fallback={
363
+ <text wrapMode='none' fg={props.palette.muted()}>
364
+ {NO_QUOTA_READING}
365
+ </text>
366
+ }
367
+ >
368
+ <For each={groups()}>
369
+ {(group) => (
370
+ <>
371
+ <text wrapMode='none' fg={props.palette.muted()}>
372
+ {GROUP_NAMES[group.key]}
373
+ </text>
374
+ <For each={group.windows}>
375
+ {(window) => (
376
+ <WindowRow
377
+ palette={props.palette}
378
+ appearance={props.appearance}
379
+ window={window}
380
+ layout={layoutFor(window)}
381
+ />
382
+ )}
383
+ </For>
384
+ </>
385
+ )}
386
+ </For>
387
+ </Show>
388
+ {props.children}
389
+ </box>
390
+ )
391
+ }