opencode-codebuddy-quota 0.1.0 → 0.1.3

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.
@@ -1,51 +1,51 @@
1
- import { PACKAGE_NAME, PACKAGE_PREFIX, isVersion, packageSpec } from "./package-spec"
2
- import { timeoutSignal } from "./signal"
3
-
4
- /**
5
- * Looks up the newest `@ai-sdk/openai-compatible` build published under the
6
- * `ai-v6` dist-tag.
7
- *
8
- * OpenCode resolves a bare `@ai-sdk/*` spec through that tag, but it caches the
9
- * result under `<name>@<version>` and never re-resolves. Writing an explicit
10
- * version into the config is therefore the only way an upgrade takes effect,
11
- * which is why this exists rather than letting OpenCode follow the tag.
12
- */
13
-
14
- /** The dist-tag OpenCode's own `aisdk:` resolution points at. */
15
- export const DIST_TAG = "ai-v6"
16
- export const REGISTRY_URL = `https://registry.npmjs.org/${PACKAGE_NAME}`
17
-
18
- const DEFAULT_TIMEOUT_MS = 10_000
19
-
20
- export interface LatestVersionDeps {
21
- fetchImpl?: typeof fetch
22
- timeoutMs?: number
23
- }
24
-
25
- /** The version the tag points at, or null when the registry cannot be read. Never throws. */
26
- export async function latestTaggedVersion(deps: LatestVersionDeps = {}): Promise<string | null> {
27
- const fetchImpl = deps.fetchImpl ?? fetch
28
- try {
29
- const response = await fetchImpl(REGISTRY_URL, {
30
- // The abbreviated doc is enough: it carries dist-tags and is far smaller.
31
- headers: { accept: "application/vnd.npm.install-v1+json" },
32
- signal: timeoutSignal(deps.timeoutMs ?? DEFAULT_TIMEOUT_MS),
33
- })
34
- if (!response.ok) return null
35
- const body = (await response.json()) as { "dist-tags"?: Record<string, unknown> }
36
- const version = body?.["dist-tags"]?.[DIST_TAG]
37
- return isVersion(version) ? version : null
38
- } catch {
39
- return null
40
- }
41
- }
42
-
43
- /**
44
- * Rewrites the version inside an `aisdk:` provider package spec, or returns
45
- * null when the spec names some other package. A bare spec gains the version.
46
- */
47
- export function withVersion(spec: string, version: string): string | null {
48
- const prefix = `${PACKAGE_PREFIX}${PACKAGE_NAME}`
49
- if (spec !== prefix && !spec.startsWith(`${prefix}@`)) return null
50
- return packageSpec(version)
51
- }
1
+ import { PACKAGE_NAME, PACKAGE_PREFIX, isVersion, packageSpec } from "./package-spec"
2
+ import { timeoutSignal } from "./signal"
3
+
4
+ /**
5
+ * Looks up the newest `@ai-sdk/openai-compatible` build published under the
6
+ * `ai-v6` dist-tag.
7
+ *
8
+ * OpenCode resolves a bare `@ai-sdk/*` spec through that tag, but it caches the
9
+ * result under `<name>@<version>` and never re-resolves. Writing an explicit
10
+ * version into the config is therefore the only way an upgrade takes effect,
11
+ * which is why this exists rather than letting OpenCode follow the tag.
12
+ */
13
+
14
+ /** The dist-tag OpenCode's own `aisdk:` resolution points at. */
15
+ export const DIST_TAG = "ai-v6"
16
+ export const REGISTRY_URL = `https://registry.npmjs.org/${PACKAGE_NAME}`
17
+
18
+ const DEFAULT_TIMEOUT_MS = 10_000
19
+
20
+ export interface LatestVersionDeps {
21
+ fetchImpl?: typeof fetch
22
+ timeoutMs?: number
23
+ }
24
+
25
+ /** The version the tag points at, or null when the registry cannot be read. Never throws. */
26
+ export async function latestTaggedVersion(deps: LatestVersionDeps = {}): Promise<string | null> {
27
+ const fetchImpl = deps.fetchImpl ?? fetch
28
+ try {
29
+ const response = await fetchImpl(REGISTRY_URL, {
30
+ // The abbreviated doc is enough: it carries dist-tags and is far smaller.
31
+ headers: { accept: "application/vnd.npm.install-v1+json" },
32
+ signal: timeoutSignal(deps.timeoutMs ?? DEFAULT_TIMEOUT_MS),
33
+ })
34
+ if (!response.ok) return null
35
+ const body = (await response.json()) as { "dist-tags"?: Record<string, unknown> }
36
+ const version = body?.["dist-tags"]?.[DIST_TAG]
37
+ return isVersion(version) ? version : null
38
+ } catch {
39
+ return null
40
+ }
41
+ }
42
+
43
+ /**
44
+ * Rewrites the version inside an `aisdk:` provider package spec, or returns
45
+ * null when the spec names some other package. A bare spec gains the version.
46
+ */
47
+ export function withVersion(spec: string, version: string): string | null {
48
+ const prefix = `${PACKAGE_PREFIX}${PACKAGE_NAME}`
49
+ if (spec !== prefix && !spec.startsWith(`${prefix}@`)) return null
50
+ return packageSpec(version)
51
+ }
@@ -1,169 +1,169 @@
1
- import type { ModelMapping } from "./config"
2
-
3
- /**
4
- * Pricing and reasoning levels for a CodeBuddy model, taken from the models.dev
5
- * catalog that OpenCode keeps locally.
6
- *
7
- * Every lookup goes through the user's `models` map: a model is only offered
8
- * because someone wrote down where it lives, so there is nothing to guess. The
9
- * same model carries different prices and levels depending on who resells it,
10
- * which is exactly why the mapping names the provider rather than inferring it.
11
- */
12
-
13
- /** models.dev v3 sometimes declares cache.write as per-duration tiers, e.g. {"5m":3,"1h":6}. */
14
- export type CacheWritePrice = number | Record<string, number>
15
-
16
- export interface ModelsDevCost {
17
- text?: {
18
- input?: { price?: number; cache?: { read?: number; write?: CacheWritePrice } }
19
- output?: { price?: number }
20
- }
21
- }
22
-
23
- export interface ModelsDevEntry {
24
- name?: string
25
- cost?: ModelsDevCost
26
- capabilities?: {
27
- reasoning?: {
28
- supported?: boolean
29
- toggle?: boolean
30
- /** Selectable reasoning levels, least to most. */
31
- effort?: readonly string[]
32
- /** The level the vendor picks when the caller names none. */
33
- default?: { type?: string; value?: string } | string
34
- }
35
- }
36
- }
37
-
38
- /** `{ [providerID]: { models: { [modelID]: entry } } }`, as stored under `models-dev:v3:catalog`. */
39
- export type ModelsDevCatalog = Record<string, { models?: Record<string, ModelsDevEntry> }>
40
-
41
- export interface ModelCost {
42
- input: number
43
- output: number
44
- cache: { read: number; write: number }
45
- }
46
-
47
- /** What a lookup needs: the models.dev coordinates plus the credit multiplier. */
48
- export interface PricedModel {
49
- id: string
50
- /** Credit multiplier. `0` means the model is free; `null` means the server did not say. */
51
- credits: number | null
52
- }
53
-
54
- const ZERO_COST: ModelCost = { input: 0, output: 0, cache: { read: 0, write: 0 } }
55
-
56
- /** The models.dev entry a CodeBuddy model maps to, or undefined when unmapped or absent. */
57
- export function mappingEntry(
58
- catalog: ModelsDevCatalog | null,
59
- mapping: ModelMapping | undefined,
60
- ): ModelsDevEntry | undefined {
61
- if (!catalog || !mapping) return undefined
62
- return catalog[mapping.provider]?.models?.[mapping.model]
63
- }
64
-
65
- /**
66
- * Exact id first, then a case-insensitive pass — the server writes
67
- * `minimax-m3.1-flash` while models.dev writes `MiniMax-M3.1-Flash`.
68
- *
69
- * Nothing fuzzier than that: the catalog is full of `-free` siblings
70
- * (`step-5-preview-free`, `hy3-free`, …) that price at zero, so a prefix or
71
- * substring match would quietly report those instead of the paid model.
72
- */
73
- export function findEntry(
74
- catalog: ModelsDevCatalog,
75
- provider: string,
76
- modelID: string,
77
- ): ModelsDevEntry | undefined {
78
- const models = catalog[provider]?.models
79
- if (!models) return undefined
80
-
81
- const exact = models[modelID]
82
- if (exact) return exact
83
-
84
- const lowered = modelID.toLowerCase()
85
- for (const [id, entry] of Object.entries(models)) {
86
- if (id.toLowerCase() === lowered) return entry
87
- }
88
- return undefined
89
- }
90
-
91
- /** Only a finite number is a usable price; tier objects and strings fall back to zero. */
92
- function finitePrice(value: unknown): number {
93
- return typeof value === "number" && Number.isFinite(value) ? value : 0
94
- }
95
-
96
- function toCost(entry: ModelsDevEntry): ModelCost | null {
97
- const text = entry.cost?.text
98
- const input = text?.input?.price
99
- const output = text?.output?.price
100
- if (typeof input !== "number" || typeof output !== "number") return null
101
- return {
102
- input,
103
- output,
104
- // Most catalog entries omit cache.write; some v3 entries declare it as a
105
- // per-duration tier object. Neither is a usable scalar, so fall back to zero.
106
- cache: {
107
- read: finitePrice(text?.input?.cache?.read),
108
- write: finitePrice(text?.input?.cache?.write),
109
- },
110
- }
111
- }
112
-
113
- /**
114
- * Resolve a model's price, returning `[]` when no price can be trusted — an
115
- * unpriced model is better than a wrong one.
116
- */
117
- export function resolveCost(
118
- model: PricedModel,
119
- mapping: ModelMapping | undefined,
120
- catalog: ModelsDevCatalog | null,
121
- log: (message: string) => void,
122
- ): ModelCost[] {
123
- // Checked before any lookup: a free model needs no price, and looking one up
124
- // would contradict the credit factor the server reported.
125
- if (model.credits === 0) return [ZERO_COST]
126
-
127
- if (!catalog) {
128
- log(`models.dev catalog unavailable; ${model.id} stays unpriced`)
129
- return []
130
- }
131
- if (!mapping) {
132
- log(`${model.id} has no models.dev mapping; leaving it unpriced`)
133
- return []
134
- }
135
-
136
- const entry = findEntry(catalog, mapping.provider, mapping.model)
137
- if (!entry) {
138
- log(`${mapping.provider} has no entry for ${mapping.model} (${model.id}); leaving it unpriced`)
139
- return []
140
- }
141
- const cost = toCost(entry)
142
- if (!cost) {
143
- log(`${mapping.provider}/${mapping.model} has no text pricing; ${model.id} stays unpriced`)
144
- return []
145
- }
146
- return [cost]
147
- }
148
-
149
- /**
150
- * The vendor's own reasoning levels for a model, or null when models.dev has
151
- * none for it. CodeBuddy's gateway rewrites some of these (it swaps `max` for
152
- * `xhigh` on `deepseek-v4-pro`, for instance), but the vendor table is the
153
- * source of record here.
154
- */
155
- export function vendorEfforts(
156
- mapping: ModelMapping | undefined,
157
- catalog: ModelsDevCatalog | null,
158
- ): { efforts: string[]; defaultEffort: string | null } | null {
159
- const reasoning = mappingEntry(catalog, mapping)?.capabilities?.reasoning
160
- const efforts = reasoning?.effort
161
- if (!efforts || efforts.length === 0) return null
162
-
163
- const declared = typeof reasoning.default === "string" ? reasoning.default : reasoning.default?.value ?? null
164
- return {
165
- efforts: [...efforts],
166
- // Only honour a vendor default that is actually one of its levels.
167
- defaultEffort: declared && efforts.includes(declared) ? declared : null,
168
- }
169
- }
1
+ import type { ModelMapping } from "./config"
2
+
3
+ /**
4
+ * Pricing and reasoning levels for a CodeBuddy model, taken from the models.dev
5
+ * catalog that OpenCode keeps locally.
6
+ *
7
+ * Every lookup goes through the user's `models` map: a model is only offered
8
+ * because someone wrote down where it lives, so there is nothing to guess. The
9
+ * same model carries different prices and levels depending on who resells it,
10
+ * which is exactly why the mapping names the provider rather than inferring it.
11
+ */
12
+
13
+ /** models.dev v3 sometimes declares cache.write as per-duration tiers, e.g. {"5m":3,"1h":6}. */
14
+ export type CacheWritePrice = number | Record<string, number>
15
+
16
+ export interface ModelsDevCost {
17
+ text?: {
18
+ input?: { price?: number; cache?: { read?: number; write?: CacheWritePrice } }
19
+ output?: { price?: number }
20
+ }
21
+ }
22
+
23
+ export interface ModelsDevEntry {
24
+ name?: string
25
+ cost?: ModelsDevCost
26
+ capabilities?: {
27
+ reasoning?: {
28
+ supported?: boolean
29
+ toggle?: boolean
30
+ /** Selectable reasoning levels, least to most. */
31
+ effort?: readonly string[]
32
+ /** The level the vendor picks when the caller names none. */
33
+ default?: { type?: string; value?: string } | string
34
+ }
35
+ }
36
+ }
37
+
38
+ /** `{ [providerID]: { models: { [modelID]: entry } } }`, as stored under `models-dev:v3:catalog`. */
39
+ export type ModelsDevCatalog = Record<string, { models?: Record<string, ModelsDevEntry> }>
40
+
41
+ export interface ModelCost {
42
+ input: number
43
+ output: number
44
+ cache: { read: number; write: number }
45
+ }
46
+
47
+ /** What a lookup needs: the models.dev coordinates plus the credit multiplier. */
48
+ export interface PricedModel {
49
+ id: string
50
+ /** Credit multiplier. `0` means the model is free; `null` means the server did not say. */
51
+ credits: number | null
52
+ }
53
+
54
+ const ZERO_COST: ModelCost = { input: 0, output: 0, cache: { read: 0, write: 0 } }
55
+
56
+ /** The models.dev entry a CodeBuddy model maps to, or undefined when unmapped or absent. */
57
+ export function mappingEntry(
58
+ catalog: ModelsDevCatalog | null,
59
+ mapping: ModelMapping | undefined,
60
+ ): ModelsDevEntry | undefined {
61
+ if (!catalog || !mapping) return undefined
62
+ return catalog[mapping.provider]?.models?.[mapping.model]
63
+ }
64
+
65
+ /**
66
+ * Exact id first, then a case-insensitive pass — the server writes
67
+ * `minimax-m3.1-flash` while models.dev writes `MiniMax-M3.1-Flash`.
68
+ *
69
+ * Nothing fuzzier than that: the catalog is full of `-free` siblings
70
+ * (`step-5-preview-free`, `hy3-free`, …) that price at zero, so a prefix or
71
+ * substring match would quietly report those instead of the paid model.
72
+ */
73
+ export function findEntry(
74
+ catalog: ModelsDevCatalog,
75
+ provider: string,
76
+ modelID: string,
77
+ ): ModelsDevEntry | undefined {
78
+ const models = catalog[provider]?.models
79
+ if (!models) return undefined
80
+
81
+ const exact = models[modelID]
82
+ if (exact) return exact
83
+
84
+ const lowered = modelID.toLowerCase()
85
+ for (const [id, entry] of Object.entries(models)) {
86
+ if (id.toLowerCase() === lowered) return entry
87
+ }
88
+ return undefined
89
+ }
90
+
91
+ /** Only a finite number is a usable price; tier objects and strings fall back to zero. */
92
+ function finitePrice(value: unknown): number {
93
+ return typeof value === "number" && Number.isFinite(value) ? value : 0
94
+ }
95
+
96
+ function toCost(entry: ModelsDevEntry): ModelCost | null {
97
+ const text = entry.cost?.text
98
+ const input = text?.input?.price
99
+ const output = text?.output?.price
100
+ if (typeof input !== "number" || typeof output !== "number") return null
101
+ return {
102
+ input,
103
+ output,
104
+ // Most catalog entries omit cache.write; some v3 entries declare it as a
105
+ // per-duration tier object. Neither is a usable scalar, so fall back to zero.
106
+ cache: {
107
+ read: finitePrice(text?.input?.cache?.read),
108
+ write: finitePrice(text?.input?.cache?.write),
109
+ },
110
+ }
111
+ }
112
+
113
+ /**
114
+ * Resolve a model's price, returning `[]` when no price can be trusted — an
115
+ * unpriced model is better than a wrong one.
116
+ */
117
+ export function resolveCost(
118
+ model: PricedModel,
119
+ mapping: ModelMapping | undefined,
120
+ catalog: ModelsDevCatalog | null,
121
+ log: (message: string) => void,
122
+ ): ModelCost[] {
123
+ // Checked before any lookup: a free model needs no price, and looking one up
124
+ // would contradict the credit factor the server reported.
125
+ if (model.credits === 0) return [ZERO_COST]
126
+
127
+ if (!catalog) {
128
+ log(`models.dev catalog unavailable; ${model.id} stays unpriced`)
129
+ return []
130
+ }
131
+ if (!mapping) {
132
+ log(`${model.id} has no models.dev mapping; leaving it unpriced`)
133
+ return []
134
+ }
135
+
136
+ const entry = findEntry(catalog, mapping.provider, mapping.model)
137
+ if (!entry) {
138
+ log(`${mapping.provider} has no entry for ${mapping.model} (${model.id}); leaving it unpriced`)
139
+ return []
140
+ }
141
+ const cost = toCost(entry)
142
+ if (!cost) {
143
+ log(`${mapping.provider}/${mapping.model} has no text pricing; ${model.id} stays unpriced`)
144
+ return []
145
+ }
146
+ return [cost]
147
+ }
148
+
149
+ /**
150
+ * The vendor's own reasoning levels for a model, or null when models.dev has
151
+ * none for it. CodeBuddy's gateway rewrites some of these (it swaps `max` for
152
+ * `xhigh` on `deepseek-v4-pro`, for instance), but the vendor table is the
153
+ * source of record here.
154
+ */
155
+ export function vendorEfforts(
156
+ mapping: ModelMapping | undefined,
157
+ catalog: ModelsDevCatalog | null,
158
+ ): { efforts: string[]; defaultEffort: string | null } | null {
159
+ const reasoning = mappingEntry(catalog, mapping)?.capabilities?.reasoning
160
+ const efforts = reasoning?.effort
161
+ if (!efforts || efforts.length === 0) return null
162
+
163
+ const declared = typeof reasoning.default === "string" ? reasoning.default : reasoning.default?.value ?? null
164
+ return {
165
+ efforts: [...efforts],
166
+ // Only honour a vendor default that is actually one of its levels.
167
+ defaultEffort: declared && efforts.includes(declared) ? declared : null,
168
+ }
169
+ }