@cortexkit/opencode-anthropic-auth 2.8.0 → 2.9.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cortexkit/opencode-anthropic-auth",
3
- "version": "2.8.0",
3
+ "version": "2.9.1",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
@@ -77,7 +77,7 @@
77
77
  },
78
78
  "dependencies": {
79
79
  "@clack/prompts": "^1.6.0",
80
- "@cortexkit/anthropic-auth-core": "2.8.0",
80
+ "@cortexkit/anthropic-auth-core": "2.9.1",
81
81
  "@cortexkit/claustrum-client": "0.17.0",
82
82
  "@cortexkit/private-files": "0.2.2",
83
83
  "@cortexkit/subc-client": "^0.24",
@@ -89,6 +89,7 @@
89
89
  },
90
90
  "devDependencies": {
91
91
  "@ai-sdk/anthropic": "4.0.94",
92
+ "@cortexkit/opencode-sidebar": "0.1.0",
92
93
  "@opencode/plugin": "2.2.5"
93
94
  }
94
95
  }
@@ -5,6 +5,7 @@ import {
5
5
  type IncomingMessage,
6
6
  type ServerResponse,
7
7
  } from 'node:http'
8
+ import type { Socket } from 'node:net'
8
9
  import { join } from 'node:path'
9
10
  import {
10
11
  type CommandApplyRequest,
@@ -59,13 +60,43 @@ export async function startRpcServer(
59
60
  options: RpcServerOptions,
60
61
  ): Promise<RpcServerHandle> {
61
62
  const token = randomBytes(32).toString('hex')
63
+ const connections = new Set<Socket>()
64
+ let stopping = false
65
+ let closing: Promise<void> | undefined
62
66
  const server = createServer((req, res) => {
63
67
  void dispatch(req, res)
64
68
  })
69
+ server.on('connection', (socket) => {
70
+ connections.add(socket)
71
+ socket.once('close', () => connections.delete(socket))
72
+ })
73
+
74
+ function closeServer(): Promise<void> {
75
+ closing ??= (async () => {
76
+ stopping = true
77
+ await new Promise<void>((resolve) => server.close(() => resolve()))
78
+ // Bun closes connections that were idle when close() ran, but a request
79
+ // active at that point can leave its keep-alive socket open after replying.
80
+ // Wait for the replies first, then close every remaining owned socket.
81
+ await Promise.all(
82
+ [...connections].map(
83
+ (socket) =>
84
+ new Promise<void>((resolve) => {
85
+ socket.once('close', () => resolve())
86
+ socket.destroy()
87
+ }),
88
+ ),
89
+ )
90
+ })()
91
+ return closing
92
+ }
65
93
 
66
94
  async function dispatch(req: IncomingMessage, res: ServerResponse) {
67
95
  const json = (status: number, value: unknown) => {
68
- res.writeHead(status, { 'content-type': 'application/json' })
96
+ res.writeHead(status, {
97
+ 'content-type': 'application/json',
98
+ ...(stopping && { connection: 'close' }),
99
+ })
69
100
  res.end(JSON.stringify(value))
70
101
  }
71
102
  try {
@@ -129,7 +160,7 @@ export async function startRpcServer(
129
160
  try {
130
161
  await writePortFile(options.dir, { port, token, pid: process.pid })
131
162
  } catch (error) {
132
- await new Promise<void>((resolve) => server.close(() => resolve()))
163
+ await closeServer()
133
164
  throw error
134
165
  }
135
166
 
@@ -137,7 +168,7 @@ export async function startRpcServer(
137
168
  port,
138
169
  token,
139
170
  async stop() {
140
- await new Promise<void>((resolve) => server.close(() => resolve()))
171
+ await closeServer()
141
172
  try {
142
173
  const portFile = join(options.dir, `port-${process.pid}.json`)
143
174
  const current = JSON.parse(await readFile(portFile, 'utf8')) as {
@@ -1,5 +1,9 @@
1
1
  // @bun
2
2
  // ../../node_modules/@cortexkit/common-auth/src/quota-sidebar/index.tsx
3
+ import { createComponent as _$createComponent2 } from "@opentui/solid";
4
+ import { mergeProps as _$mergeProps } from "@opentui/solid";
5
+
6
+ // ../../node_modules/@cortexkit/opencode-sidebar/src/index.tsx
3
7
  import { createTextNode as _$createTextNode } from "@opentui/solid";
4
8
  import { createComponent as _$createComponent } from "@opentui/solid";
5
9
  import { use as _$use } from "@opentui/solid";
@@ -10,253 +14,6 @@ import { insert as _$insert } from "@opentui/solid";
10
14
  import { setProp as _$setProp } from "@opentui/solid";
11
15
  import { createElement as _$createElement } from "@opentui/solid";
12
16
  import { createContext, createMemo, createSignal, For, Index, mergeProps, Show, useContext } from "solid-js";
13
-
14
- // ../../node_modules/@cortexkit/common-auth/src/quota/map.ts
15
- var ALL_SCOPE = "all";
16
- var DEFAULT_REQUIRED_LABELS = Object.freeze([
17
- "primary"
18
- ]);
19
-
20
- // ../../node_modules/@cortexkit/common-auth/src/quota/format.ts
21
- var QUOTA_STALE_AFTER_MS = 15 * 60000;
22
- var NO_QUOTA_READING = "no quota reading yet";
23
- var NO_LIMITS_REPORTED = "no limits reported";
24
- function shortSpan(ms) {
25
- const minutes = Math.max(1, Math.floor(ms / 60000));
26
- if (minutes < 60)
27
- return `${minutes}m`;
28
- if (minutes < 1440) {
29
- const hours2 = Math.floor(minutes / 60);
30
- const rest = minutes % 60;
31
- return rest === 0 ? `${hours2}h` : `${hours2}h${rest}m`;
32
- }
33
- const days = Math.floor(minutes / 1440);
34
- const hours = Math.floor(minutes % 1440 / 60);
35
- return hours === 0 ? `${days}d` : `${days}d${hours}h`;
36
- }
37
- function lengthName(minutes) {
38
- if (minutes % 1440 === 0)
39
- return `${minutes / 1440}d`;
40
- if (minutes % 60 === 0)
41
- return `${minutes / 60}h`;
42
- return `${minutes}m`;
43
- }
44
- function quotaWindowName(limit) {
45
- if (limit.scope !== ALL_SCOPE || limit.windowMinutes === undefined)
46
- return limit.label;
47
- return lengthName(limit.windowMinutes);
48
- }
49
- function clampPercent(remaining) {
50
- return Math.min(100, Math.max(0, remaining));
51
- }
52
- function wholePercent(remaining) {
53
- return Math.round(clampPercent(remaining));
54
- }
55
- function percentLeft(remaining) {
56
- return `${wholePercent(remaining)}% left`;
57
- }
58
- function resetDetails(resetsAt, checkedAt, now) {
59
- const at = resetsAt === undefined ? Number.NaN : Date.parse(resetsAt);
60
- return {
61
- ...Number.isFinite(at) && at > now ? { resetIn: shortSpan(at - now) } : {},
62
- resetSinceReading: Number.isFinite(at) && at > checkedAt && at <= now
63
- };
64
- }
65
- function displayOrder(left, right) {
66
- const l = left.windowMinutes;
67
- const r = right.windowMinutes;
68
- if (l !== undefined && r !== undefined && l !== r)
69
- return l - r;
70
- if (l === undefined !== (r === undefined))
71
- return l === undefined ? 1 : -1;
72
- if (left.scope !== right.scope) {
73
- if (left.scope === ALL_SCOPE)
74
- return -1;
75
- if (right.scope === ALL_SCOPE)
76
- return 1;
77
- return left.scope < right.scope ? -1 : 1;
78
- }
79
- return left.label < right.label ? -1 : left.label > right.label ? 1 : 0;
80
- }
81
- function quotaWindowParts(quota, options) {
82
- const { windows, age } = scopedWindowParts(quota, options, true);
83
- const parts = windows.map(({ part }) => part);
84
- return age === undefined ? { windows: parts } : { windows: parts, age };
85
- }
86
- function withShortNames(windows) {
87
- const abbreviations = scopeAbbreviations(windows.filter(({ part, scope }) => part.kind === "window" && scope !== ALL_SCOPE).map(({ scope }) => scope));
88
- for (const window of windows) {
89
- window.part.short = window.part.kind === "budget" ? "cr" : window.scope === ALL_SCOPE ? window.part.name : abbreviations.get(window.scope) ?? window.scope;
90
- }
91
- return windows;
92
- }
93
- function hasWindowReading(limit) {
94
- return Number.isFinite(limit.remainingPercent) && Number.isFinite(limit.checkedAt);
95
- }
96
- function scopedWindowParts(quota, options, includeUnknown = false) {
97
- if (!quota)
98
- return { windows: [] };
99
- const { now } = options;
100
- const readings = quota.limits.filter((limit) => limit.kind === "reading" && (includeUnknown || hasWindowReading(limit))).sort(displayOrder);
101
- const windows = readings.map((limit) => {
102
- const part = hasWindowReading(limit) ? {
103
- kind: "window",
104
- reading: "known",
105
- name: quotaWindowName(limit),
106
- short: "",
107
- figure: percentLeft(limit.remainingPercent),
108
- remainingPercent: clampPercent(limit.remainingPercent),
109
- checkedAt: limit.checkedAt,
110
- ...resetDetails(limit.resetsAt, limit.checkedAt, now)
111
- } : {
112
- kind: "window",
113
- reading: "unknown",
114
- name: quotaWindowName(limit),
115
- short: "",
116
- figure: "no reading",
117
- resetSinceReading: false
118
- };
119
- return {
120
- scope: limit.scope,
121
- ...limit.windowMinutes === undefined ? {} : { windowMinutes: limit.windowMinutes },
122
- part
123
- };
124
- });
125
- const budget = quota.budget;
126
- if (budget) {
127
- const remainingPercent = budget.remainingPercent;
128
- const figure = budget.reached ? "credits spent" : remainingPercent !== undefined ? `credits ${percentLeft(remainingPercent)}` : "credits available";
129
- windows.push({
130
- scope: ALL_SCOPE,
131
- spent: budget.reached,
132
- part: {
133
- kind: "budget",
134
- reading: "known",
135
- name: "credits",
136
- short: "",
137
- figure,
138
- ...remainingPercent === undefined ? {} : { remainingPercent: clampPercent(remainingPercent) },
139
- checkedAt: budget.checkedAt,
140
- ...resetDetails(budget.resetsAt, budget.checkedAt, now)
141
- }
142
- });
143
- }
144
- withShortNames(windows);
145
- const readingTimes = windows.flatMap(({ part }) => part.reading === "known" && part.checkedAt !== undefined ? [part.checkedAt] : []);
146
- if (readingTimes.length > 0) {
147
- const age = now - Math.min(...readingTimes);
148
- if (age > (options.staleAfterMs ?? QUOTA_STALE_AFTER_MS)) {
149
- return { windows, age: `checked ${shortSpan(age)} ago` };
150
- }
151
- }
152
- return { windows };
153
- }
154
- function scopeDisplayName(scope) {
155
- const model = scope.slice(scope.indexOf(":") + 1);
156
- return model.charAt(0).toUpperCase() + model.slice(1);
157
- }
158
- function scopeAbbreviations(scopes) {
159
- const names = [...new Set(scopes.map(scopeDisplayName))];
160
- const lengths = new Map(names.map((name) => [name, 2]));
161
- const short = (name) => name.slice(0, lengths.get(name));
162
- for (;; ) {
163
- const byShort = new Map;
164
- for (const name of names) {
165
- byShort.set(short(name), [...byShort.get(short(name)) ?? [], name]);
166
- }
167
- let extended = false;
168
- for (const group of byShort.values()) {
169
- if (group.length < 2)
170
- continue;
171
- for (const name of group) {
172
- const length = lengths.get(name) ?? 2;
173
- if (length < name.length) {
174
- lengths.set(name, length + 1);
175
- extended = true;
176
- }
177
- }
178
- }
179
- if (!extended)
180
- break;
181
- }
182
- return new Map(scopes.map((scope) => [scope, short(scopeDisplayName(scope))]));
183
- }
184
- var NARROW_GROUP_LIMIT = 2;
185
- function groupShortNames(groups) {
186
- const labels = groups.map((group) => group.label.trim() || group.scope);
187
- const short = scopeAbbreviations(labels);
188
- return labels.map((label) => short.get(label) ?? label);
189
- }
190
- function groupedNarrowPieces(groups, windows) {
191
- if (!groups || groups.length === 0)
192
- return;
193
- const reported = windows.filter(({ part }) => part.kind === "window");
194
- if (reported.some(({ scope }) => scope === ALL_SCOPE))
195
- return;
196
- const candidates = groups.flatMap((group, listed) => {
197
- const inGroup = reported.filter(({ scope }) => scope === group.scope);
198
- const groupTightest = inGroup.reduce((least, window) => least === undefined || (window.part.remainingPercent ?? 0) < (least.part.remainingPercent ?? 0) ? window : least, undefined);
199
- return groupTightest ? [{ group, listed, window: groupTightest }] : [];
200
- });
201
- if (candidates.length === 0)
202
- return;
203
- const names = groupShortNames(groups);
204
- const left = (candidate) => candidate.window.part.remainingPercent ?? 0;
205
- const shown = [...candidates].sort((a, b) => left(a) - left(b) || a.listed - b.listed).slice(0, NARROW_GROUP_LIMIT).sort((a, b) => a.listed - b.listed);
206
- return shown.map((candidate, index) => {
207
- const { windowMinutes } = candidate.window;
208
- const piece = [
209
- names[candidate.listed] ?? candidate.group.scope,
210
- ...windowMinutes === undefined ? [] : [lengthName(windowMinutes)],
211
- `${wholePercent(left(candidate))}%`
212
- ].join(" ");
213
- return index === shown.length - 1 ? `${piece} left` : piece;
214
- });
215
- }
216
- function narrowTextParts(quota, options) {
217
- const { windows } = scopedWindowParts(quota, options);
218
- const grouped = groupedNarrowPieces(quota?.groups, windows);
219
- if (grouped)
220
- return grouped;
221
- const general = windows.filter(({ part, scope }) => part.kind === "window" && scope === ALL_SCOPE);
222
- const scoped = windows.filter(({ part, scope }) => part.kind === "window" && scope !== ALL_SCOPE);
223
- const budget = windows.find(({ part }) => part.kind === "budget");
224
- const budgetLeft = budget?.spent ? 0 : budget?.part.remainingPercent;
225
- const extras = [
226
- ...scoped.map(({ part }) => ({
227
- label: part.short,
228
- remaining: part.remainingPercent ?? 0
229
- })),
230
- ...budgetLeft === undefined || budget === undefined ? [] : [{ label: budget.part.short, remaining: budgetLeft }]
231
- ];
232
- const tightest = extras.reduce((best, item) => best === undefined || item.remaining < best.remaining ? item : best, undefined);
233
- const pieces = general.map(({ part }) => `${part.name} ${wholePercent(part.remainingPercent ?? 0)}%`);
234
- if (tightest) {
235
- pieces.push(`${tightest.label} ${wholePercent(tightest.remaining)}%`);
236
- }
237
- if (pieces.length === 0)
238
- return windows.map(({ part }) => part.figure);
239
- return pieces.map((piece, index) => index === pieces.length - 1 ? `${piece} left` : piece);
240
- }
241
- function quotaTextParts(quota, options) {
242
- if (options.form === "narrow")
243
- return narrowTextParts(quota, options);
244
- const { windows, age } = scopedWindowParts(quota, options);
245
- const full = (options.form ?? "full") === "full";
246
- const text = windows.map(({ part }) => {
247
- const reset = full && part.resetIn ? `, resets ${part.resetIn}` : "";
248
- return `${part.kind === "budget" ? "" : `${part.name} `}${part.figure}${reset}`;
249
- });
250
- return age === undefined ? text : [...text, age];
251
- }
252
- function formatQuota(quota, options) {
253
- const parts = quotaTextParts(quota, options);
254
- if (parts.length > 0)
255
- return parts.join(" \xB7 ");
256
- return quota && quota.limits.length > 0 ? NO_LIMITS_REPORTED : NO_QUOTA_READING;
257
- }
258
-
259
- // ../../node_modules/@cortexkit/common-auth/src/quota-sidebar/index.tsx
260
17
  var FrameContext = createContext();
261
18
  function frameContext() {
262
19
  const context = useContext(FrameContext);
@@ -559,13 +316,8 @@ function CollapsedAccountRow(props) {
559
316
  const context = frameContext();
560
317
  const [width, setWidth] = createSignal(0);
561
318
  let row;
562
- const hasQuota = () => quotaWindowParts(props.account.quota, {
563
- now: context.now
564
- }).windows.some((part) => part.reading === "known");
565
- const text = () => hasQuota() ? formatQuota(props.account.quota, {
566
- now: context.now,
567
- form: "narrow"
568
- }) : props.account.emptyQuotaText?.collapsed ?? "\u2014";
319
+ const hasQuota = () => props.account.hasQuota;
320
+ const text = () => hasQuota() ? props.account.collapsedQuotaText ?? "" : props.account.emptyQuotaText?.collapsed ?? "\u2014";
569
321
  const showQuota = () => width() <= 0 || displayWidth(text()) + 4 <= width();
570
322
  const label = () => {
571
323
  if (width() <= 0)
@@ -678,9 +430,7 @@ function AccountBlock(props) {
678
430
  const full = `${props.account.label}${props.account.titleSuffix ? ` \xB7 ${props.account.titleSuffix}` : ""}`;
679
431
  return columns() <= 0 ? full : fitText(full, columns() - displayWidth(props.account.status.text) - 1);
680
432
  };
681
- const age = () => quotaWindowParts(props.account.quota, {
682
- now: context.now
683
- }).age;
433
+ const age = () => props.account.quotaAge;
684
434
  const hasRows = () => props.account.groups.some((group) => group.rows.length > 0);
685
435
  return (() => {
686
436
  var _el$28 = _$createElement("box"), _el$29 = _$createElement("box"), _el$30 = _$createElement("text"), _el$31 = _$createElement("b"), _el$32 = _$createElement("text"), _el$33 = _$createElement("b");
@@ -901,9 +651,297 @@ function SidebarFrame(props) {
901
651
  }
902
652
  });
903
653
  }
654
+
655
+ // ../../node_modules/@cortexkit/common-auth/src/quota-sidebar/index.tsx
656
+ import { mapArray, mergeProps as mergeProps2 } from "solid-js";
657
+
658
+ // ../../node_modules/@cortexkit/common-auth/src/quota/map.ts
659
+ var ALL_SCOPE = "all";
660
+ var DEFAULT_REQUIRED_LABELS = Object.freeze([
661
+ "primary"
662
+ ]);
663
+
664
+ // ../../node_modules/@cortexkit/common-auth/src/quota/format.ts
665
+ var QUOTA_STALE_AFTER_MS = 15 * 60000;
666
+ var NO_QUOTA_READING = "no quota reading yet";
667
+ var NO_LIMITS_REPORTED = "no limits reported";
668
+ function shortSpan(ms) {
669
+ const minutes = Math.max(1, Math.floor(ms / 60000));
670
+ if (minutes < 60)
671
+ return `${minutes}m`;
672
+ if (minutes < 1440) {
673
+ const hours2 = Math.floor(minutes / 60);
674
+ const rest = minutes % 60;
675
+ return rest === 0 ? `${hours2}h` : `${hours2}h${rest}m`;
676
+ }
677
+ const days = Math.floor(minutes / 1440);
678
+ const hours = Math.floor(minutes % 1440 / 60);
679
+ return hours === 0 ? `${days}d` : `${days}d${hours}h`;
680
+ }
681
+ function lengthName(minutes) {
682
+ if (minutes % 1440 === 0)
683
+ return `${minutes / 1440}d`;
684
+ if (minutes % 60 === 0)
685
+ return `${minutes / 60}h`;
686
+ return `${minutes}m`;
687
+ }
688
+ function quotaWindowName(limit) {
689
+ if (limit.scope !== ALL_SCOPE || limit.windowMinutes === undefined)
690
+ return limit.label;
691
+ return lengthName(limit.windowMinutes);
692
+ }
693
+ function clampPercent(remaining) {
694
+ return Math.min(100, Math.max(0, remaining));
695
+ }
696
+ function wholePercent(remaining) {
697
+ return Math.round(clampPercent(remaining));
698
+ }
699
+ function percentLeft(remaining) {
700
+ return `${wholePercent(remaining)}% left`;
701
+ }
702
+ function resetDetails(resetsAt, checkedAt, now) {
703
+ const at = resetsAt === undefined ? Number.NaN : Date.parse(resetsAt);
704
+ return {
705
+ ...Number.isFinite(at) && at > now ? { resetIn: shortSpan(at - now) } : {},
706
+ resetSinceReading: Number.isFinite(at) && at > checkedAt && at <= now
707
+ };
708
+ }
709
+ function displayOrder(left, right) {
710
+ const l = left.windowMinutes;
711
+ const r = right.windowMinutes;
712
+ if (l !== undefined && r !== undefined && l !== r)
713
+ return l - r;
714
+ if (l === undefined !== (r === undefined))
715
+ return l === undefined ? 1 : -1;
716
+ if (left.scope !== right.scope) {
717
+ if (left.scope === ALL_SCOPE)
718
+ return -1;
719
+ if (right.scope === ALL_SCOPE)
720
+ return 1;
721
+ return left.scope < right.scope ? -1 : 1;
722
+ }
723
+ return left.label < right.label ? -1 : left.label > right.label ? 1 : 0;
724
+ }
725
+ function quotaWindowParts(quota, options) {
726
+ const { windows, age } = scopedWindowParts(quota, options, true);
727
+ const parts = windows.map(({ part }) => part);
728
+ return age === undefined ? { windows: parts } : { windows: parts, age };
729
+ }
730
+ function withShortNames(windows) {
731
+ const abbreviations = scopeAbbreviations(windows.filter(({ part, scope }) => part.kind === "window" && scope !== ALL_SCOPE).map(({ scope }) => scope));
732
+ for (const window of windows) {
733
+ window.part.short = window.part.kind === "budget" ? "cr" : window.scope === ALL_SCOPE ? window.part.name : abbreviations.get(window.scope) ?? window.scope;
734
+ }
735
+ return windows;
736
+ }
737
+ function hasWindowReading(limit) {
738
+ return Number.isFinite(limit.remainingPercent) && Number.isFinite(limit.checkedAt);
739
+ }
740
+ function scopedWindowParts(quota, options, includeUnknown = false) {
741
+ if (!quota)
742
+ return { windows: [] };
743
+ const { now } = options;
744
+ const readings = quota.limits.filter((limit) => limit.kind === "reading" && (includeUnknown || hasWindowReading(limit))).sort(displayOrder);
745
+ const windows = readings.map((limit) => {
746
+ const part = hasWindowReading(limit) ? {
747
+ kind: "window",
748
+ reading: "known",
749
+ name: quotaWindowName(limit),
750
+ short: "",
751
+ figure: percentLeft(limit.remainingPercent),
752
+ remainingPercent: clampPercent(limit.remainingPercent),
753
+ checkedAt: limit.checkedAt,
754
+ ...resetDetails(limit.resetsAt, limit.checkedAt, now)
755
+ } : {
756
+ kind: "window",
757
+ reading: "unknown",
758
+ name: quotaWindowName(limit),
759
+ short: "",
760
+ figure: "no reading",
761
+ resetSinceReading: false
762
+ };
763
+ return {
764
+ scope: limit.scope,
765
+ ...limit.windowMinutes === undefined ? {} : { windowMinutes: limit.windowMinutes },
766
+ part
767
+ };
768
+ });
769
+ const budget = quota.budget;
770
+ if (budget) {
771
+ const remainingPercent = budget.remainingPercent;
772
+ const figure = budget.reached ? "credits spent" : remainingPercent !== undefined ? `credits ${percentLeft(remainingPercent)}` : "credits available";
773
+ windows.push({
774
+ scope: ALL_SCOPE,
775
+ spent: budget.reached,
776
+ part: {
777
+ kind: "budget",
778
+ reading: "known",
779
+ name: "credits",
780
+ short: "",
781
+ figure,
782
+ ...remainingPercent === undefined ? {} : { remainingPercent: clampPercent(remainingPercent) },
783
+ checkedAt: budget.checkedAt,
784
+ ...resetDetails(budget.resetsAt, budget.checkedAt, now)
785
+ }
786
+ });
787
+ }
788
+ withShortNames(windows);
789
+ const readingTimes = windows.flatMap(({ part }) => part.reading === "known" && part.checkedAt !== undefined ? [part.checkedAt] : []);
790
+ if (readingTimes.length > 0) {
791
+ const age = now - Math.min(...readingTimes);
792
+ if (age > (options.staleAfterMs ?? QUOTA_STALE_AFTER_MS)) {
793
+ return { windows, age: `checked ${shortSpan(age)} ago` };
794
+ }
795
+ }
796
+ return { windows };
797
+ }
798
+ function scopeDisplayName(scope) {
799
+ const model = scope.slice(scope.indexOf(":") + 1);
800
+ return model.charAt(0).toUpperCase() + model.slice(1);
801
+ }
802
+ function scopeAbbreviations(scopes) {
803
+ const names = [...new Set(scopes.map(scopeDisplayName))];
804
+ const lengths = new Map(names.map((name) => [name, 2]));
805
+ const short = (name) => name.slice(0, lengths.get(name));
806
+ for (;; ) {
807
+ const byShort = new Map;
808
+ for (const name of names) {
809
+ byShort.set(short(name), [...byShort.get(short(name)) ?? [], name]);
810
+ }
811
+ let extended = false;
812
+ for (const group of byShort.values()) {
813
+ if (group.length < 2)
814
+ continue;
815
+ for (const name of group) {
816
+ const length = lengths.get(name) ?? 2;
817
+ if (length < name.length) {
818
+ lengths.set(name, length + 1);
819
+ extended = true;
820
+ }
821
+ }
822
+ }
823
+ if (!extended)
824
+ break;
825
+ }
826
+ return new Map(scopes.map((scope) => [scope, short(scopeDisplayName(scope))]));
827
+ }
828
+ var NARROW_GROUP_LIMIT = 2;
829
+ function groupShortNames(groups) {
830
+ const labels = groups.map((group) => group.label.trim() || group.scope);
831
+ const short = scopeAbbreviations(labels);
832
+ return labels.map((label) => short.get(label) ?? label);
833
+ }
834
+ function groupedNarrowPieces(groups, windows) {
835
+ if (!groups || groups.length === 0)
836
+ return;
837
+ const reported = windows.filter(({ part }) => part.kind === "window");
838
+ if (reported.some(({ scope }) => scope === ALL_SCOPE))
839
+ return;
840
+ const candidates = groups.flatMap((group, listed) => {
841
+ const inGroup = reported.filter(({ scope }) => scope === group.scope);
842
+ const groupTightest = inGroup.reduce((least, window) => least === undefined || (window.part.remainingPercent ?? 0) < (least.part.remainingPercent ?? 0) ? window : least, undefined);
843
+ return groupTightest ? [{ group, listed, window: groupTightest }] : [];
844
+ });
845
+ if (candidates.length === 0)
846
+ return;
847
+ const names = groupShortNames(groups);
848
+ const left = (candidate) => candidate.window.part.remainingPercent ?? 0;
849
+ const shown = [...candidates].sort((a, b) => left(a) - left(b) || a.listed - b.listed).slice(0, NARROW_GROUP_LIMIT).sort((a, b) => a.listed - b.listed);
850
+ return shown.map((candidate, index) => {
851
+ const { windowMinutes } = candidate.window;
852
+ const piece = [
853
+ names[candidate.listed] ?? candidate.group.scope,
854
+ ...windowMinutes === undefined ? [] : [lengthName(windowMinutes)],
855
+ `${wholePercent(left(candidate))}%`
856
+ ].join(" ");
857
+ return index === shown.length - 1 ? `${piece} left` : piece;
858
+ });
859
+ }
860
+ function narrowTextParts(quota, options) {
861
+ const { windows } = scopedWindowParts(quota, options);
862
+ const grouped = groupedNarrowPieces(quota?.groups, windows);
863
+ if (grouped)
864
+ return grouped;
865
+ const general = windows.filter(({ part, scope }) => part.kind === "window" && scope === ALL_SCOPE);
866
+ const scoped = windows.filter(({ part, scope }) => part.kind === "window" && scope !== ALL_SCOPE);
867
+ const budget = windows.find(({ part }) => part.kind === "budget");
868
+ const budgetLeft = budget?.spent ? 0 : budget?.part.remainingPercent;
869
+ const extras = [
870
+ ...scoped.map(({ part }) => ({
871
+ label: part.short,
872
+ remaining: part.remainingPercent ?? 0
873
+ })),
874
+ ...budgetLeft === undefined || budget === undefined ? [] : [{ label: budget.part.short, remaining: budgetLeft }]
875
+ ];
876
+ const tightest = extras.reduce((best, item) => best === undefined || item.remaining < best.remaining ? item : best, undefined);
877
+ const pieces = general.map(({ part }) => `${part.name} ${wholePercent(part.remainingPercent ?? 0)}%`);
878
+ if (tightest) {
879
+ pieces.push(`${tightest.label} ${wholePercent(tightest.remaining)}%`);
880
+ }
881
+ if (pieces.length === 0)
882
+ return windows.map(({ part }) => part.figure);
883
+ return pieces.map((piece, index) => index === pieces.length - 1 ? `${piece} left` : piece);
884
+ }
885
+ function quotaTextParts(quota, options) {
886
+ if (options.form === "narrow")
887
+ return narrowTextParts(quota, options);
888
+ const { windows, age } = scopedWindowParts(quota, options);
889
+ const full = (options.form ?? "full") === "full";
890
+ const text = windows.map(({ part }) => {
891
+ const reset = full && part.resetIn ? `, resets ${part.resetIn}` : "";
892
+ return `${part.kind === "budget" ? "" : `${part.name} `}${part.figure}${reset}`;
893
+ });
894
+ return age === undefined ? text : [...text, age];
895
+ }
896
+ function formatQuota(quota, options) {
897
+ const parts = quotaTextParts(quota, options);
898
+ if (parts.length > 0)
899
+ return parts.join(" \xB7 ");
900
+ return quota && quota.limits.length > 0 ? NO_LIMITS_REPORTED : NO_QUOTA_READING;
901
+ }
902
+
903
+ // ../../node_modules/@cortexkit/common-auth/src/quota-sidebar/index.tsx
904
+ function SidebarFrame2(props) {
905
+ const sources = new WeakMap;
906
+ const accounts = mapArray(() => props.accounts, (account) => {
907
+ const adapted = mergeProps2(account, {
908
+ get hasQuota() {
909
+ return quotaWindowParts(account.quota, {
910
+ now: props.now
911
+ }).windows.some((part) => part.reading === "known");
912
+ },
913
+ get collapsedQuotaText() {
914
+ return formatQuota(account.quota, {
915
+ now: props.now,
916
+ form: "narrow"
917
+ });
918
+ },
919
+ get quotaAge() {
920
+ return quotaWindowParts(account.quota, {
921
+ now: props.now
922
+ }).age;
923
+ }
924
+ });
925
+ sources.set(adapted, account);
926
+ return adapted;
927
+ });
928
+ return _$createComponent2(SidebarFrame, _$mergeProps(props, {
929
+ get accounts() {
930
+ return accounts();
931
+ },
932
+ beforeQuota: (account) => {
933
+ const source = sources.get(account);
934
+ return source && props.beforeQuota?.(source);
935
+ },
936
+ afterQuota: (account) => {
937
+ const source = sources.get(account);
938
+ return source && props.afterQuota?.(source);
939
+ }
940
+ }));
941
+ }
904
942
  export {
905
943
  SidebarTextRow,
906
944
  SidebarStatRow,
907
945
  SidebarSection,
908
- SidebarFrame
946
+ SidebarFrame2 as SidebarFrame
909
947
  };