@namzu/openai 4.6.0 → 4.7.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.
@@ -0,0 +1,86 @@
1
+ /**
2
+ * Reading limits out of a model catalogue row, shared by the API-key client and the subscription
3
+ * route so the two spell the same fields the same way.
4
+ */
5
+
6
+ /** A number, or a string that holds one (some backends send counts as strings). */
7
+ function asNumber(value: unknown): number | undefined {
8
+ if (typeof value === 'number') return value
9
+ if (typeof value === 'string' && value.trim() !== '') return Number(value)
10
+ return undefined
11
+ }
12
+
13
+ /** The first of these fields of `row` that holds a positive safe integer. */
14
+ export function firstCount(
15
+ row: Record<string, unknown> | undefined,
16
+ wires: readonly string[],
17
+ ): number | undefined {
18
+ for (const wire of wires) {
19
+ const value = asNumber(row?.[wire])
20
+ if (value !== undefined && Number.isSafeInteger(value) && value > 0) return value
21
+ }
22
+ return undefined
23
+ }
24
+
25
+ /** The spellings catalogues use for the context window, in the order they are tried. */
26
+ export const CONTEXT_WINDOW_WIRES: readonly string[] = [
27
+ 'context_window_tokens',
28
+ 'context_window',
29
+ 'context_length',
30
+ 'max_model_len',
31
+ 'max_context_window',
32
+ ]
33
+
34
+ /** The window a catalogue row states, or `undefined` when it states none. */
35
+ export function declaredContextWindow(row: Record<string, unknown> | undefined) {
36
+ return firstCount(row, CONTEXT_WINDOW_WIRES)
37
+ }
38
+
39
+ /** The share of a window the subscription backend counts as usable when its row states none. */
40
+ export const SUBSCRIPTION_DEFAULT_USABLE_PERCENT = 95
41
+
42
+ /** The share of the resolved window the backend caps its auto-compact limit at. */
43
+ const SUBSCRIPTION_COMPACT_CAP_PERCENT = 90
44
+
45
+ /** What the subscription backend's model row says about the window and compaction. */
46
+ export interface SubscriptionLimits {
47
+ /** `context_window`, else `max_context_window`, scaled by the usable percent. */
48
+ readonly contextWindow?: number
49
+ /** The backend's compaction threshold: its limit held to 90% of the window, or 90% alone. */
50
+ readonly autoCompactTokenLimit?: number
51
+ }
52
+
53
+ /**
54
+ * Read one row of the subscription backend's `/models` answer the way the backend defines it:
55
+ *
56
+ * - resolved window = `context_window`, else `max_context_window`
57
+ * - usable window = resolved x `effective_context_window_percent` / 100, the percent being 95
58
+ * when the row omits it
59
+ * - auto-compact limit = `auto_compact_token_limit` held to 90% of the resolved window, or 90%
60
+ * of it alone when the row sends none, or the row's limit alone when there is no window
61
+ *
62
+ * Counts may arrive as numbers or numeric strings. Only this route applies the percent; the
63
+ * other compatible hosts go through `declaredContextWindow`.
64
+ */
65
+ export function subscriptionLimits(row: Record<string, unknown> | undefined): SubscriptionLimits {
66
+ const resolved = firstCount(row, ['context_window', 'max_context_window'])
67
+ const statedLimit = firstCount(row, ['auto_compact_token_limit'])
68
+ const percentValue = asNumber(row?.effective_context_window_percent)
69
+ const percent =
70
+ percentValue !== undefined && Number.isFinite(percentValue) && percentValue > 0
71
+ ? Math.min(percentValue, 100)
72
+ : SUBSCRIPTION_DEFAULT_USABLE_PERCENT
73
+ const contextWindow =
74
+ resolved === undefined ? undefined : Math.max(1, Math.floor((resolved * percent) / 100))
75
+ let autoCompactTokenLimit: number | undefined
76
+ if (resolved !== undefined) {
77
+ const cap = Math.max(1, Math.floor((resolved * SUBSCRIPTION_COMPACT_CAP_PERCENT) / 100))
78
+ autoCompactTokenLimit = statedLimit === undefined ? cap : Math.min(statedLimit, cap)
79
+ } else {
80
+ autoCompactTokenLimit = statedLimit
81
+ }
82
+ return {
83
+ ...(contextWindow !== undefined ? { contextWindow } : {}),
84
+ ...(autoCompactTokenLimit !== undefined ? { autoCompactTokenLimit } : {}),
85
+ }
86
+ }