pantheon-opencode 1.6.1 → 1.6.2-beta.10
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/AGENTS.global.md +11 -11
- package/AGENTS.md +2 -2
- package/CHANGELOG.md +179 -0
- package/README.md +268 -20
- package/README.pt-BR.md +34 -11
- package/bin/pantheon-init.mjs +301 -32
- package/package.json +13 -4
- package/plugin.json +1 -1
- package/prompts/README.md +58 -0
- package/prompts/debug-issue.prompt.md +46 -0
- package/prompts/dynamic/agora-generated.txt +38 -0
- package/prompts/dynamic/orchestrate-generated.txt +51 -0
- package/prompts/focus.prompt.md +42 -0
- package/prompts/implement-feature.prompt.md +39 -0
- package/prompts/optimize-database.prompt.md +58 -0
- package/prompts/orchestrate-with-zeus.prompt.md +441 -0
- package/prompts/plan-architecture.prompt.md +59 -0
- package/prompts/quick-discovery-large-codebase.prompt.md +294 -0
- package/prompts/quick-plan-large-feature.prompt.md +197 -0
- package/prompts/semantic-summarize.md +50 -0
- package/prompts/sketch.prompt.md +91 -0
- package/prompts/subtask.prompt.md +131 -0
- package/pyproject.toml +1 -1
- package/scripts/build-agents-md.mjs +1 -1
- package/scripts/decision-router-budget.mjs +780 -0
- package/scripts/decision-router-v2-budget.mjs +218 -0
- package/scripts/decision-router-v2-egress.mjs +272 -0
- package/scripts/decision-router.mjs +758 -0
- package/scripts/docs-check-changelog.mjs +5 -3
- package/scripts/doctor.mjs +213 -45
- package/scripts/install/agents-md.mjs +2 -1
- package/scripts/install/component-selection.mjs +9 -4
- package/scripts/install/config-migration.mjs +344 -393
- package/scripts/install/config-paths.mjs +15 -0
- package/scripts/install/decision-layer-preview.mjs +376 -0
- package/scripts/install/decision-router-config.mjs +560 -0
- package/scripts/install/health-check.mjs +20 -11
- package/scripts/install/interactive.mjs +103 -14
- package/scripts/install/interrupt.mjs +4 -3
- package/scripts/install/mcp-permissions.mjs +164 -0
- package/scripts/install/migrate.mjs +123 -72
- package/scripts/install/opencode-version.mjs +31 -13
- package/scripts/install/opencode-wildcard.mjs +106 -0
- package/scripts/install/opencode.mjs +411 -184
- package/scripts/install/plan-artifact.mjs +12 -1
- package/scripts/install/plan.mjs +8 -1
- package/scripts/install/plugin.mjs +18 -1
- package/scripts/install/state.mjs +12 -0
- package/scripts/install/strings.mjs +22 -0
- package/scripts/install/v2-capabilities.mjs +5 -6
- package/scripts/install/venv.mjs +34 -17
- package/scripts/mcp_resources.py +11 -9
- package/scripts/package-evidence.mjs +1 -0
- package/scripts/release-notes.mjs +30 -7
- package/scripts/restore.mjs +22 -10
- package/scripts/sandbox-env.sh +174 -0
- package/scripts/setup-v2-config.mjs +1703 -0
- package/scripts/setup.mjs +415 -0
- package/scripts/sync-tui.mjs +9 -1
- package/scripts/test-opencode-v2-sandbox.sh +448 -157
- package/scripts/update-skills-lock.mjs +30 -0
- package/scripts/versioning.mjs +41 -5
- package/skills-lock.json +10 -10
- package/src/agents/aphrodite.md +11 -3
- package/src/agents/apollo.md +6 -0
- package/src/agents/athena.md +7 -1
- package/src/agents/demeter.md +6 -2
- package/src/agents/gaia.md +5 -0
- package/src/agents/hephaestus.md +7 -2
- package/src/agents/hermes.md +14 -13
- package/src/agents/iris.md +6 -2
- package/src/agents/mnemosyne.md +21 -50
- package/src/agents/nyx.md +6 -2
- package/src/agents/prometheus.md +5 -1
- package/src/agents/talos.md +10 -1
- package/src/agents/themis.md +5 -1
- package/src/agents/zeus.md +61 -65
- package/src/instructions/agent-return-format.instructions.md +1 -1
- package/src/instructions/memory-protocol.instructions.md +3 -3
- package/src/instructions/test-economy.instructions.md +6 -6
- package/src/mcp/eval_store.py +11 -4
- package/src/mcp/mcp_resources.py +11 -9
- package/src/mcp/memory_mcp.py +293 -150
- package/src/mcp/pantheon_vision.py +37 -36
- package/src/pantheon/cost-command.ts +30 -133
- package/src/pantheon/jev-decision-tool.ts +347 -0
- package/src/pantheon/jev-decisions.ts +625 -0
- package/src/pantheon/local-decision-agents.ts +170 -0
- package/src/pantheon/local-decision-budget.ts +1046 -0
- package/src/pantheon/local-decision-egress-gate.ts +144 -0
- package/src/pantheon/local-decision-prompt.ts +256 -0
- package/src/pantheon/local-decision-provider-matrix.ts +44 -0
- package/src/pantheon/local-decision-router.ts +225 -0
- package/src/pantheon/local-decision-skills.ts +281 -0
- package/src/pantheon/local-decision-tool.ts +160 -0
- package/src/pantheon/local-decision-turn.ts +101 -0
- package/src/pantheon/local-decision-usage-evidence.internal.ts +168 -0
- package/src/pantheon/local-orchestration.ts +322 -0
- package/src/pantheon/model-pricing.ts +1 -536
- package/src/pantheon/pantheon-decision-catalog.ts +101 -0
- package/src/pantheon/v2-decision-budget.ts +456 -0
- package/src/pantheon/v2-decision-contracts.ts +300 -0
- package/src/pantheon/v2-decision-egress.ts +203 -0
- package/src/pantheon/v2-decision-events.ts +180 -0
- package/src/pantheon/v2-decision-heuristic.ts +121 -0
- package/src/pantheon/v2-decision-points/p1-agent.ts +91 -0
- package/src/pantheon/v2-decision-points/p10-claims.ts +243 -0
- package/src/pantheon/v2-decision-points/p11-catalog.ts +220 -0
- package/src/pantheon/v2-decision-points/p2-skill.ts +83 -0
- package/src/pantheon/v2-decision-points/p3-complexity.ts +108 -0
- package/src/pantheon/v2-decision-points/p4-memory.ts +137 -0
- package/src/pantheon/v2-decision-points/p5-themis-verify.ts +190 -0
- package/src/pantheon/v2-decision-points/p6-council.ts +161 -0
- package/src/pantheon/v2-decision-points/p7-loop.ts +263 -0
- package/src/pantheon/v2-decision-points/p8-guardrail.ts +367 -0
- package/src/pantheon/v2-decision-points/p9-memory-compaction.ts +304 -0
- package/src/pantheon/v2-decision-points.ts +242 -0
- package/src/pantheon/v2-decision-policy.ts +166 -0
- package/src/pantheon/v2-decision-premises.ts +146 -0
- package/src/pantheon/v2-decision-prompt-surface.ts +115 -0
- package/src/pantheon/v2-decision-rank.ts +91 -0
- package/src/pantheon/v2-decision-registry.ts +391 -0
- package/src/pantheon/v2-decision-resource-ids.ts +127 -0
- package/src/pantheon/v2-decision-shadow.ts +123 -0
- package/src/pantheon/v2-decision-telemetry.ts +214 -0
- package/src/pantheon/v2-decision-verify.ts +51 -0
- package/src/pantheon/v2-hook-engine.ts +307 -0
- package/src/pantheon/v2-native-decisions.ts +365 -0
- package/src/pantheon/v2-shell-hooks.ts +66 -2
- package/src/pantheon/v2-tools.ts +38 -15
- package/src/pantheon/v2-vision.ts +34 -29
- package/src/pantheon/vision.ts +43 -10
- package/src/plugin-v2.ts +449 -79
- package/src/plugin.ts +4 -0
- package/src/plugins/tui/THIRD_PARTY_NOTICES.md +24 -0
- package/src/plugins/tui/dist/server-v2.d.ts +22 -0
- package/src/plugins/tui/dist/server-v2.d.ts.map +1 -0
- package/src/plugins/tui/dist/server-v2.js +26 -0
- package/src/plugins/tui/dist/server-v2.js.map +1 -0
- package/src/plugins/tui/dist/tui-v2.d.ts +54 -51
- package/src/plugins/tui/dist/tui-v2.d.ts.map +1 -1
- package/src/plugins/tui/dist/tui-v2.js +762 -529
- package/src/plugins/tui/dist/tui-v2.js.map +1 -1
- package/src/plugins/tui/dist/tui.js +1 -1
- package/src/plugins/tui/index-v2.ts +3 -6
- package/src/plugins/tui/package-lock.json +17 -2
- package/src/plugins/tui/package.json +6 -3
- package/src/plugins/tui/src/context-gauge.ts +59 -0
- package/src/plugins/tui/src/cost-data.ts +607 -0
- package/src/plugins/tui/src/index-v2.tsx +360 -242
- package/src/plugins/tui/src/model-pricing.ts +536 -0
- package/src/plugins/tui/src/model-usage-limits.ts +4 -0
- package/src/plugins/tui/src/server-v2.ts +21 -0
- package/src/plugins/tui/src/sidebar-width.ts +129 -0
- package/src/plugins/tui/tsconfig.typecheck.json +1 -1
- package/src/plugins/tui/tsdown.config.ts +34 -5
- package/src/routing.yml +72 -13
- package/src/skills/README.md +4 -0
- package/src/skills/agent-coordination/SKILL.md +0 -3
- package/src/skills/artifact-management/SKILL.md +0 -3
- package/src/skills/auto-continue/SKILL.md +0 -3
- package/src/skills/code-review-checklist/SKILL.md +0 -3
- package/src/skills/context-compression/SKILL.md +0 -3
- package/src/skills/memory-bank/SKILL.md +6 -28
- package/src/skills/session-goal/SKILL.md +0 -3
- package/src/skills/simplify/SKILL.md +5 -4
- package/src/skills/tdd-with-agents/SKILL.md +0 -3
- package/src/skills/visual-review-pipeline/SKILL.md +0 -1
- package/src/pantheon/memory-context.ts +0 -1017
|
@@ -0,0 +1,536 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Model pricing — money for `pantheon_cost`.
|
|
3
|
+
*
|
|
4
|
+
* Prices come from the public catalog the OpenCode ecosystem already uses,
|
|
5
|
+
* `https://models.dev/api.json` (USD per 1M tokens). The rule that shapes this
|
|
6
|
+
* module is that a tool call must NEVER block on the network:
|
|
7
|
+
*
|
|
8
|
+
* - every call reads a normalized on-disk cache synchronously;
|
|
9
|
+
* - an explicit `refreshPricing` is the only AWAITED fetch (bounded timeout);
|
|
10
|
+
* - a missing or stale cache is refreshed in the BACKGROUND, with a short
|
|
11
|
+
* timeout, so the next call benefits without this one waiting;
|
|
12
|
+
* - every network/parse failure degrades to "pricing unavailable" and the
|
|
13
|
+
* token report keeps working.
|
|
14
|
+
*
|
|
15
|
+
* An unknown model is never priced at zero. `0` is a real catalog price (a free
|
|
16
|
+
* model), and conflating it with "no price" would report free usage for every
|
|
17
|
+
* model the catalog has never heard of — which is precisely the number a tier
|
|
18
|
+
* decision would be made on.
|
|
19
|
+
*
|
|
20
|
+
* Cache location follows the repository's existing config-dir convention
|
|
21
|
+
* (`PANTHEON_HOME` → `$XDG_CONFIG_HOME/opencode` → `~/.config/opencode`), so no
|
|
22
|
+
* new root is invented: `<configDir>/cache/models-dev.json`.
|
|
23
|
+
*
|
|
24
|
+
* @module model-pricing
|
|
25
|
+
*/
|
|
26
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs'
|
|
27
|
+
import { homedir } from 'node:os'
|
|
28
|
+
import { dirname, join } from 'node:path'
|
|
29
|
+
|
|
30
|
+
/** The catalog URL every OpenCode-adjacent tool prices against. */
|
|
31
|
+
const MODELS_DEV_URL = 'https://models.dev/api.json'
|
|
32
|
+
|
|
33
|
+
/** Cache payload version; a mismatch is treated as "no cache". */
|
|
34
|
+
const PRICING_CACHE_VERSION = 1
|
|
35
|
+
|
|
36
|
+
/** Cached prices are considered fresh for one day. */
|
|
37
|
+
const PRICING_TTL_MS = 24 * 60 * 60 * 1000
|
|
38
|
+
|
|
39
|
+
/** Upper bound for any catalog fetch, so no request can outlive a tool call. */
|
|
40
|
+
const PRICING_TIMEOUT_MS = 3000
|
|
41
|
+
|
|
42
|
+
/** One model's list price, in USD per 1M tokens. */
|
|
43
|
+
export interface ModelPrice {
|
|
44
|
+
inputPerMTok: number
|
|
45
|
+
outputPerMTok: number
|
|
46
|
+
/**
|
|
47
|
+
* Absent when the catalog publishes no cache-read price. Kept distinct from
|
|
48
|
+
* `0` so cache tokens can never be priced as free by accident.
|
|
49
|
+
*/
|
|
50
|
+
cacheReadPerMTok?: number
|
|
51
|
+
cacheWritePerMTok?: number
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Token counts a cost is computed from. */
|
|
55
|
+
export interface TokenAmounts {
|
|
56
|
+
input: number
|
|
57
|
+
output: number
|
|
58
|
+
cacheRead: number
|
|
59
|
+
cacheWrite: number
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Why a row could not be priced. */
|
|
63
|
+
export type UnpricedReason =
|
|
64
|
+
| 'no-pricing'
|
|
65
|
+
| 'unknown-model'
|
|
66
|
+
| 'ambiguous-model'
|
|
67
|
+
| 'missing-cache-price'
|
|
68
|
+
|
|
69
|
+
/** Resolved prices, ready for lookup. */
|
|
70
|
+
export interface PricingSnapshot {
|
|
71
|
+
/** `${providerId}/${modelId}`, lowercased. */
|
|
72
|
+
index: ReadonlyMap<string, ModelPrice>
|
|
73
|
+
/** Model id → every provider's price for it, for provider-less lookups. */
|
|
74
|
+
modelIndex: ReadonlyMap<string, readonly ModelPrice[]>
|
|
75
|
+
/** True when the cache is older than the TTL. */
|
|
76
|
+
stale: boolean
|
|
77
|
+
/** True when a background refresh is in flight. */
|
|
78
|
+
refreshing: boolean
|
|
79
|
+
/** Epoch ms of the cached catalog. */
|
|
80
|
+
fetchedAt?: number
|
|
81
|
+
/** Set when pricing cannot be used at all, or when a refresh failed. */
|
|
82
|
+
unavailableReason?: string
|
|
83
|
+
/**
|
|
84
|
+
* Set when a refresh was REFUSED rather than attempted (offline mode).
|
|
85
|
+
* Distinguished from a failed fetch so the report can say "no request was
|
|
86
|
+
* made" instead of implying the network was tried.
|
|
87
|
+
*/
|
|
88
|
+
refused?: boolean
|
|
89
|
+
/** True when offline mode was in effect for this snapshot. */
|
|
90
|
+
offline?: boolean
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Minimal fetch boundary, so tests never touch the network. */
|
|
94
|
+
export type FetchLike = (
|
|
95
|
+
url: string,
|
|
96
|
+
init: { signal?: AbortSignal; headers?: Record<string, string> },
|
|
97
|
+
) => Promise<{ ok: boolean; status: number; json(): Promise<unknown> }>
|
|
98
|
+
|
|
99
|
+
/** Injectable knobs (tests, air-gapped hosts, unusual installs). */
|
|
100
|
+
export interface PricingOptions {
|
|
101
|
+
/** Cache file. Default: `<configDir>/cache/models-dev.json`. */
|
|
102
|
+
cachePath?: string
|
|
103
|
+
/** Fetch boundary. Default: global `fetch`. */
|
|
104
|
+
fetchImpl?: FetchLike
|
|
105
|
+
/** Clock. Default: `Date.now`. */
|
|
106
|
+
now?: () => number
|
|
107
|
+
/** Freshness window. Default: {@link PRICING_TTL_MS}. */
|
|
108
|
+
ttlMs?: number
|
|
109
|
+
/** Environment used for cache-path resolution. Default: `process.env`. */
|
|
110
|
+
env?: NodeJS.ProcessEnv
|
|
111
|
+
/** Disable every network attempt (tests / air-gapped hosts). */
|
|
112
|
+
offline?: boolean
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
interface CachedCatalog {
|
|
116
|
+
version: number
|
|
117
|
+
fetchedAt: number
|
|
118
|
+
providers: Record<string, Record<string, ModelPrice>>
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
type ProviderPrices = Record<string, Record<string, ModelPrice>>
|
|
122
|
+
|
|
123
|
+
/** Resolve the OpenCode config dir, mirroring `_pantheon_paths.py`. */
|
|
124
|
+
function resolvePricingCacheDir(env: NodeJS.ProcessEnv = process.env): string {
|
|
125
|
+
const explicit = env.PANTHEON_HOME?.trim()
|
|
126
|
+
if (explicit !== undefined && explicit !== '') return explicit
|
|
127
|
+
const xdg = env.XDG_CONFIG_HOME?.trim()
|
|
128
|
+
const base = xdg !== undefined && xdg !== '' ? xdg : join(homedir(), '.config')
|
|
129
|
+
return join(base, 'opencode')
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** Cache file for the normalized catalog. */
|
|
133
|
+
function resolvePricingCachePath(env: NodeJS.ProcessEnv = process.env): string {
|
|
134
|
+
return join(resolvePricingCacheDir(env), 'cache', 'models-dev.json')
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
138
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function finiteNumber(value: unknown): number | undefined {
|
|
142
|
+
return typeof value === 'number' && Number.isFinite(value) ? value : undefined
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** Reject nonsense prices rather than reporting a $2B session. */
|
|
146
|
+
const MAX_SANE_PER_MTOK = 100_000
|
|
147
|
+
|
|
148
|
+
function nonNegativePrice(value: unknown): number | undefined {
|
|
149
|
+
const parsed = finiteNumber(value)
|
|
150
|
+
if (parsed === undefined || parsed < 0 || parsed > MAX_SANE_PER_MTOK) return undefined
|
|
151
|
+
return parsed
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** Validate one already-normalized price from the cache file. */
|
|
155
|
+
function buildPrice(
|
|
156
|
+
input: number | undefined,
|
|
157
|
+
output: number | undefined,
|
|
158
|
+
cacheRead: number | undefined,
|
|
159
|
+
cacheWrite: number | undefined,
|
|
160
|
+
): ModelPrice | undefined {
|
|
161
|
+
if (input === undefined || output === undefined) return undefined
|
|
162
|
+
return {
|
|
163
|
+
inputPerMTok: input,
|
|
164
|
+
outputPerMTok: output,
|
|
165
|
+
...(cacheRead === undefined ? {} : { cacheReadPerMTok: cacheRead }),
|
|
166
|
+
...(cacheWrite === undefined ? {} : { cacheWritePerMTok: cacheWrite }),
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function parseNormalizedPrice(value: unknown): ModelPrice | undefined {
|
|
171
|
+
if (!isRecord(value)) return undefined
|
|
172
|
+
return buildPrice(
|
|
173
|
+
nonNegativePrice(value.inputPerMTok),
|
|
174
|
+
nonNegativePrice(value.outputPerMTok),
|
|
175
|
+
nonNegativePrice(value.cacheReadPerMTok),
|
|
176
|
+
nonNegativePrice(value.cacheWritePerMTok),
|
|
177
|
+
)
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Validate the normalized `provider → model → price` shape.
|
|
182
|
+
*
|
|
183
|
+
* The cache stores the RESULT of {@link parseCatalogPayload}, not the raw
|
|
184
|
+
* models.dev payload, so it must not be re-parsed as one: a normalized entry has
|
|
185
|
+
* no `models` key, and treating it as a payload would silently read a valid
|
|
186
|
+
* cache as empty.
|
|
187
|
+
*/
|
|
188
|
+
function parseNormalizedProviders(value: unknown): ProviderPrices | undefined {
|
|
189
|
+
if (!isRecord(value)) return undefined
|
|
190
|
+
const providers: ProviderPrices = {}
|
|
191
|
+
for (const [providerId, models] of Object.entries(value)) {
|
|
192
|
+
if (!isRecord(models)) continue
|
|
193
|
+
const priced: Record<string, ModelPrice> = {}
|
|
194
|
+
for (const [modelId, price] of Object.entries(models)) {
|
|
195
|
+
const parsed = parseNormalizedPrice(price)
|
|
196
|
+
if (parsed !== undefined) priced[modelId.toLowerCase()] = parsed
|
|
197
|
+
}
|
|
198
|
+
if (Object.keys(priced).length > 0) providers[providerId.toLowerCase()] = priced
|
|
199
|
+
}
|
|
200
|
+
return Object.keys(providers).length === 0 ? undefined : providers
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** Parse one catalog model entry. Missing input/output cost ⇒ unpriced. */
|
|
204
|
+
function parseModelPrice(model: unknown): ModelPrice | undefined {
|
|
205
|
+
if (!isRecord(model)) return undefined
|
|
206
|
+
const cost = model.cost
|
|
207
|
+
if (!isRecord(cost)) return undefined
|
|
208
|
+
return buildPrice(
|
|
209
|
+
nonNegativePrice(cost.input),
|
|
210
|
+
nonNegativePrice(cost.output),
|
|
211
|
+
nonNegativePrice(cost.cache_read),
|
|
212
|
+
nonNegativePrice(cost.cache_write),
|
|
213
|
+
)
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Normalize a models.dev payload into `provider → model → price`.
|
|
218
|
+
*
|
|
219
|
+
* Provider and model ids are lowercased because the ledger stores whatever the
|
|
220
|
+
* host wrote (`opencode-go`) and the catalog keys are not case-normalized.
|
|
221
|
+
*/
|
|
222
|
+
export function parseCatalogPayload(payload: unknown): ProviderPrices | undefined {
|
|
223
|
+
if (!isRecord(payload)) return undefined
|
|
224
|
+
const providers: ProviderPrices = {}
|
|
225
|
+
for (const [providerId, provider] of Object.entries(payload)) {
|
|
226
|
+
if (!isRecord(provider)) continue
|
|
227
|
+
const models = provider.models
|
|
228
|
+
if (!isRecord(models)) continue
|
|
229
|
+
const priced: Record<string, ModelPrice> = {}
|
|
230
|
+
for (const [modelId, model] of Object.entries(models)) {
|
|
231
|
+
const price = parseModelPrice(model)
|
|
232
|
+
if (price !== undefined) priced[modelId.toLowerCase()] = price
|
|
233
|
+
}
|
|
234
|
+
if (Object.keys(priced).length > 0) providers[providerId.toLowerCase()] = priced
|
|
235
|
+
}
|
|
236
|
+
return Object.keys(providers).length === 0 ? undefined : providers
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function priceKey(price: ModelPrice): string {
|
|
240
|
+
return [
|
|
241
|
+
price.inputPerMTok,
|
|
242
|
+
price.outputPerMTok,
|
|
243
|
+
price.cacheReadPerMTok ?? '-',
|
|
244
|
+
price.cacheWritePerMTok ?? '-',
|
|
245
|
+
].join('/')
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function indexCatalog(providers: ProviderPrices): {
|
|
249
|
+
index: Map<string, ModelPrice>
|
|
250
|
+
modelIndex: Map<string, readonly ModelPrice[]>
|
|
251
|
+
} {
|
|
252
|
+
const index = new Map<string, ModelPrice>()
|
|
253
|
+
const modelIndex = new Map<string, ModelPrice[]>()
|
|
254
|
+
const seen = new Map<string, Set<string>>()
|
|
255
|
+
for (const [providerId, models] of Object.entries(providers)) {
|
|
256
|
+
for (const [modelId, price] of Object.entries(models)) {
|
|
257
|
+
index.set(`${providerId}/${modelId}`, price)
|
|
258
|
+
const bucket = modelIndex.get(modelId) ?? []
|
|
259
|
+
const keys = seen.get(modelId) ?? new Set<string>()
|
|
260
|
+
const key = priceKey(price)
|
|
261
|
+
if (!keys.has(key)) {
|
|
262
|
+
keys.add(key)
|
|
263
|
+
bucket.push(price)
|
|
264
|
+
seen.set(modelId, keys)
|
|
265
|
+
modelIndex.set(modelId, bucket)
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
return { index, modelIndex }
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function snapshotFromCatalog(
|
|
273
|
+
catalog: CachedCatalog,
|
|
274
|
+
now: number,
|
|
275
|
+
ttlMs: number,
|
|
276
|
+
refreshing: boolean,
|
|
277
|
+
): PricingSnapshot {
|
|
278
|
+
const { index, modelIndex } = indexCatalog(catalog.providers)
|
|
279
|
+
return {
|
|
280
|
+
index,
|
|
281
|
+
modelIndex,
|
|
282
|
+
fetchedAt: catalog.fetchedAt,
|
|
283
|
+
stale: Math.max(0, now - catalog.fetchedAt) > ttlMs,
|
|
284
|
+
refreshing,
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function unavailableSnapshot(reason: string): PricingSnapshot {
|
|
289
|
+
return {
|
|
290
|
+
index: new Map(),
|
|
291
|
+
modelIndex: new Map(),
|
|
292
|
+
stale: false,
|
|
293
|
+
refreshing: false,
|
|
294
|
+
unavailableReason: reason,
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
function parseCachedCatalog(raw: unknown): CachedCatalog | undefined {
|
|
299
|
+
if (!isRecord(raw)) return undefined
|
|
300
|
+
if (raw.version !== PRICING_CACHE_VERSION) return undefined
|
|
301
|
+
const fetchedAt = finiteNumber(raw.fetchedAt)
|
|
302
|
+
if (fetchedAt === undefined) return undefined
|
|
303
|
+
const providers = parseNormalizedProviders(raw.providers)
|
|
304
|
+
if (providers === undefined) return undefined
|
|
305
|
+
return { version: PRICING_CACHE_VERSION, fetchedAt, providers }
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/** Read the normalized cache. Any problem reads as "no cache", never as zero. */
|
|
309
|
+
function readPricingCache(cachePath: string): CachedCatalog | undefined {
|
|
310
|
+
try {
|
|
311
|
+
if (!existsSync(cachePath)) return undefined
|
|
312
|
+
return parseCachedCatalog(JSON.parse(readFileSync(cachePath, 'utf8')))
|
|
313
|
+
} catch {
|
|
314
|
+
return undefined
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/** Write atomically; a cache that cannot be written must not break a report. */
|
|
319
|
+
function writePricingCache(cachePath: string, catalog: CachedCatalog): boolean {
|
|
320
|
+
const temp = `${cachePath}.tmp-${process.pid}`
|
|
321
|
+
try {
|
|
322
|
+
mkdirSync(dirname(cachePath), { recursive: true })
|
|
323
|
+
writeFileSync(temp, `${JSON.stringify(catalog)}\n`, 'utf8')
|
|
324
|
+
renameSync(temp, cachePath)
|
|
325
|
+
return true
|
|
326
|
+
} catch {
|
|
327
|
+
return false
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
const inFlight = new Map<string, Promise<PricingSnapshot>>()
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Offline is a HOST-wide guarantee, not a hint.
|
|
335
|
+
*
|
|
336
|
+
* Both switches are honoured on every path that could open a socket, so the
|
|
337
|
+
* env var cannot be defeated by a caller that passes `refreshPricing` (or by a
|
|
338
|
+
* future code path that forgets to check). The reason names both switches,
|
|
339
|
+
* because a user reading the report needs to know which one to change.
|
|
340
|
+
*/
|
|
341
|
+
const OFFLINE_REFUSAL =
|
|
342
|
+
'refreshPricing cannot run: PANTHEON_COST_OFFLINE=1 (or the offline option) forbids every network request'
|
|
343
|
+
|
|
344
|
+
function isPricingOffline(options?: PricingOptions): boolean {
|
|
345
|
+
const env = options?.env ?? process.env
|
|
346
|
+
return options?.offline === true || env.PANTHEON_COST_OFFLINE === '1'
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* Fetch the catalog once per cache path at a time, bounded by a timeout.
|
|
351
|
+
*
|
|
352
|
+
* Failures are values, not throws: the caller reports "pricing unavailable" and
|
|
353
|
+
* keeps the token ledger it already read.
|
|
354
|
+
*/
|
|
355
|
+
function refreshPricingCatalog(options?: PricingOptions): Promise<PricingSnapshot> {
|
|
356
|
+
// The gate sits on the fetch itself, so no caller — background, forced, or a
|
|
357
|
+
// future one — can open a socket while offline.
|
|
358
|
+
if (isPricingOffline(options)) {
|
|
359
|
+
return Promise.resolve({
|
|
360
|
+
...unavailableSnapshot(OFFLINE_REFUSAL),
|
|
361
|
+
refused: true,
|
|
362
|
+
offline: true,
|
|
363
|
+
})
|
|
364
|
+
}
|
|
365
|
+
const env = options?.env ?? process.env
|
|
366
|
+
const cachePath = options?.cachePath ?? resolvePricingCachePath(env)
|
|
367
|
+
const existing = inFlight.get(cachePath)
|
|
368
|
+
if (existing !== undefined) return existing
|
|
369
|
+
|
|
370
|
+
const now = (options?.now ?? Date.now)()
|
|
371
|
+
const ttlMs = options?.ttlMs ?? PRICING_TTL_MS
|
|
372
|
+
const doFetch: FetchLike =
|
|
373
|
+
options?.fetchImpl ??
|
|
374
|
+
((url, init) =>
|
|
375
|
+
fetch(url, {
|
|
376
|
+
...(init.signal === undefined ? {} : { signal: init.signal }),
|
|
377
|
+
...(init.headers === undefined ? {} : { headers: init.headers }),
|
|
378
|
+
}))
|
|
379
|
+
|
|
380
|
+
const task = (async (): Promise<PricingSnapshot> => {
|
|
381
|
+
try {
|
|
382
|
+
const response = await doFetch(MODELS_DEV_URL, {
|
|
383
|
+
signal: AbortSignal.timeout(PRICING_TIMEOUT_MS),
|
|
384
|
+
headers: { accept: 'application/json' },
|
|
385
|
+
})
|
|
386
|
+
if (!response.ok) {
|
|
387
|
+
return unavailableSnapshot(`models.dev returned HTTP ${response.status}`)
|
|
388
|
+
}
|
|
389
|
+
const providers = parseCatalogPayload(await response.json())
|
|
390
|
+
if (providers === undefined) {
|
|
391
|
+
return unavailableSnapshot('models.dev payload carried no usable prices')
|
|
392
|
+
}
|
|
393
|
+
const catalog: CachedCatalog = { version: PRICING_CACHE_VERSION, fetchedAt: now, providers }
|
|
394
|
+
const cached = writePricingCache(cachePath, catalog)
|
|
395
|
+
const snapshot = snapshotFromCatalog(catalog, now, ttlMs, false)
|
|
396
|
+
return cached ? snapshot : { ...snapshot, unavailableReason: 'price cache is not writable' }
|
|
397
|
+
} catch (error) {
|
|
398
|
+
const detail = error instanceof Error ? error.message : String(error)
|
|
399
|
+
return unavailableSnapshot(`models.dev fetch failed: ${detail}`)
|
|
400
|
+
} finally {
|
|
401
|
+
inFlight.delete(cachePath)
|
|
402
|
+
}
|
|
403
|
+
})()
|
|
404
|
+
|
|
405
|
+
inFlight.set(cachePath, task)
|
|
406
|
+
return task
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* Load prices without blocking on the network.
|
|
411
|
+
*
|
|
412
|
+
* A stale cache is served immediately and refreshed in the background; with no
|
|
413
|
+
* cache there is nothing to serve, so the snapshot is explicitly unavailable
|
|
414
|
+
* and `pending` carries the opportunistic refresh for callers that want it.
|
|
415
|
+
*/
|
|
416
|
+
export function loadPricing(options?: PricingOptions): {
|
|
417
|
+
snapshot: PricingSnapshot
|
|
418
|
+
pending?: Promise<void>
|
|
419
|
+
} {
|
|
420
|
+
const env = options?.env ?? process.env
|
|
421
|
+
const cachePath = options?.cachePath ?? resolvePricingCachePath(env)
|
|
422
|
+
const now = (options?.now ?? Date.now)()
|
|
423
|
+
const ttlMs = options?.ttlMs ?? PRICING_TTL_MS
|
|
424
|
+
const catalog = readPricingCache(cachePath)
|
|
425
|
+
const offline = isPricingOffline(options)
|
|
426
|
+
|
|
427
|
+
if (catalog === undefined) {
|
|
428
|
+
if (offline) {
|
|
429
|
+
return {
|
|
430
|
+
snapshot: {
|
|
431
|
+
...unavailableSnapshot(
|
|
432
|
+
'no price cache, and pricing is offline (PANTHEON_COST_OFFLINE=1 or the offline option)',
|
|
433
|
+
),
|
|
434
|
+
offline: true,
|
|
435
|
+
},
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
const pending = refreshPricingCatalog(options)
|
|
439
|
+
return {
|
|
440
|
+
snapshot: unavailableSnapshot(
|
|
441
|
+
'no cached models.dev catalog yet; fetching one in the background',
|
|
442
|
+
),
|
|
443
|
+
pending: pending.then(() => undefined),
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
const snapshot = snapshotFromCatalog(catalog, now, ttlMs, false)
|
|
448
|
+
if (!snapshot.stale || offline) {
|
|
449
|
+
return { snapshot: offline ? { ...snapshot, offline: true } : snapshot }
|
|
450
|
+
}
|
|
451
|
+
// Stale prices are better than none: serve them and refresh behind the call.
|
|
452
|
+
const pending = refreshPricingCatalog(options)
|
|
453
|
+
return {
|
|
454
|
+
snapshot: { ...snapshot, refreshing: true },
|
|
455
|
+
pending: pending.then(() => undefined),
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* Await a refresh — only for an explicit `refreshPricing: true`.
|
|
461
|
+
*
|
|
462
|
+
* On failure the previously cached prices are still returned (marked stale), so
|
|
463
|
+
* a failed refresh does not erase the only prices the host has.
|
|
464
|
+
*/
|
|
465
|
+
export async function forceRefreshPricing(options?: PricingOptions): Promise<PricingSnapshot> {
|
|
466
|
+
const refreshed = await refreshPricingCatalog(options)
|
|
467
|
+
if (refreshed.unavailableReason === undefined) return refreshed
|
|
468
|
+
const env = options?.env ?? process.env
|
|
469
|
+
const cachePath = options?.cachePath ?? resolvePricingCachePath(env)
|
|
470
|
+
const catalog = readPricingCache(cachePath)
|
|
471
|
+
if (catalog === undefined) return refreshed
|
|
472
|
+
const now = (options?.now ?? Date.now)()
|
|
473
|
+
const cached = snapshotFromCatalog(catalog, now, options?.ttlMs ?? PRICING_TTL_MS, false)
|
|
474
|
+
return {
|
|
475
|
+
...cached,
|
|
476
|
+
unavailableReason: refreshed.unavailableReason,
|
|
477
|
+
...(refreshed.refused === true ? { refused: true } : {}),
|
|
478
|
+
...(refreshed.offline === true ? { offline: true } : {}),
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/** Result of a price lookup. */
|
|
483
|
+
export type PriceLookup = { ok: true; price: ModelPrice } | { ok: false; reason: UnpricedReason }
|
|
484
|
+
|
|
485
|
+
/**
|
|
486
|
+
* Resolve a price for one ledger model.
|
|
487
|
+
*
|
|
488
|
+
* Exact `provider/model` wins. A model that exists under exactly one provider
|
|
489
|
+
* price is accepted when the ledger's provider id is unknown to the catalog
|
|
490
|
+
* (real hosts name their own gateways, e.g. `opencode-go` serving a model the
|
|
491
|
+
* catalog lists under `opencode`). Two DIFFERENT prices for the same model id
|
|
492
|
+
* are ambiguous and stay unpriced rather than guessing a provider.
|
|
493
|
+
*/
|
|
494
|
+
export function lookupModelPrice(
|
|
495
|
+
snapshot: PricingSnapshot,
|
|
496
|
+
providerId: string | undefined,
|
|
497
|
+
modelId: string | undefined,
|
|
498
|
+
): PriceLookup {
|
|
499
|
+
if (snapshot.index.size === 0) return { ok: false, reason: 'no-pricing' }
|
|
500
|
+
const model = (modelId ?? '').trim().toLowerCase()
|
|
501
|
+
if (model === '') return { ok: false, reason: 'unknown-model' }
|
|
502
|
+
const provider = (providerId ?? '').trim().toLowerCase()
|
|
503
|
+
if (provider !== '') {
|
|
504
|
+
const exact = snapshot.index.get(`${provider}/${model}`)
|
|
505
|
+
if (exact !== undefined) return { ok: true, price: exact }
|
|
506
|
+
}
|
|
507
|
+
const candidates = snapshot.modelIndex.get(model) ?? []
|
|
508
|
+
const only = candidates.length === 1 ? candidates[0] : undefined
|
|
509
|
+
if (only !== undefined) return { ok: true, price: only }
|
|
510
|
+
if (candidates.length === 0) return { ok: false, reason: 'unknown-model' }
|
|
511
|
+
return { ok: false, reason: 'ambiguous-model' }
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
/** Result of the cost math. */
|
|
515
|
+
export type CostResult = { ok: true; usd: number } | { ok: false; reason: 'missing-cache-price' }
|
|
516
|
+
|
|
517
|
+
/**
|
|
518
|
+
* Cost of one model's token counts.
|
|
519
|
+
*
|
|
520
|
+
* Cache tokens are billed at their own rate; when the catalog has no cache rate
|
|
521
|
+
* the row is unpriced instead of being charged at the input rate or at zero.
|
|
522
|
+
*/
|
|
523
|
+
export function computeModelCost(price: ModelPrice, tokens: TokenAmounts): CostResult {
|
|
524
|
+
let usd = (tokens.input * price.inputPerMTok + tokens.output * price.outputPerMTok) / 1_000_000
|
|
525
|
+
if (tokens.cacheRead > 0) {
|
|
526
|
+
const rate = price.cacheReadPerMTok
|
|
527
|
+
if (rate === undefined) return { ok: false, reason: 'missing-cache-price' }
|
|
528
|
+
usd += (tokens.cacheRead * rate) / 1_000_000
|
|
529
|
+
}
|
|
530
|
+
if (tokens.cacheWrite > 0) {
|
|
531
|
+
const rate = price.cacheWritePerMTok
|
|
532
|
+
if (rate === undefined) return { ok: false, reason: 'missing-cache-price' }
|
|
533
|
+
usd += (tokens.cacheWrite * rate) / 1_000_000
|
|
534
|
+
}
|
|
535
|
+
return { ok: true, usd }
|
|
536
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* No-op server stub for the OpenCode V2 plugin loader.
|
|
3
|
+
*
|
|
4
|
+
* The OpenCode plugin host requires every plugin (TUI included) to expose a
|
|
5
|
+
* `server()` entry via `exports["./server"]` (or package.json `main`) — for the
|
|
6
|
+
* V2 copy this root `index-v2.ts` re-export resolves to this compiled entry —
|
|
7
|
+
* and it loads that server half only from a `config.plugins` entry (see
|
|
8
|
+
* `scripts/install/opencode.mjs`). When the entry is missing the host fails to
|
|
9
|
+
* attach the plugin's server half and the TUI runtime never wires the reactive
|
|
10
|
+
* channel — the initial JSX mounts but effects/refresh/events stay dead (the
|
|
11
|
+
* "mudo panel" symptom; same contract as the V1 `server.ts` stub).
|
|
12
|
+
*
|
|
13
|
+
* This stub satisfies the loader contract. The native quota RPC that used to
|
|
14
|
+
* live here was removed in 1.6.2 — provider quota/usage visibility is now the
|
|
15
|
+
* community plugin's job (`@slkiser/opencode-quota`,
|
|
16
|
+
* docs/tui-v2-quota-providers.md) — and pantheon-tui has no other background
|
|
17
|
+
* server work of its own.
|
|
18
|
+
*/
|
|
19
|
+
export default function server() {
|
|
20
|
+
return {}
|
|
21
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/** Conservative content budget; the host sidebar slot does not report its runtime width. */
|
|
2
|
+
export const SIDEBAR_CONTENT_WIDTH = 34
|
|
3
|
+
|
|
4
|
+
export const GRAPHEMES = new Intl.Segmenter(undefined, { granularity: 'grapheme' })
|
|
5
|
+
|
|
6
|
+
/** A grapheme the host draws two cells wide: emoji presentation, an extended
|
|
7
|
+
* pictograph (a bare `❤` counts too, exactly as the host's `emoji-regex`
|
|
8
|
+
* does) and a regional-indicator pair (a flag). */
|
|
9
|
+
const EMOJI_GLYPH = /\p{Emoji_Presentation}|\p{Extended_Pictographic}|\p{Regional_Indicator}/u
|
|
10
|
+
/** VS16 asks for emoji presentation; without it `1` is one cell, with it the
|
|
11
|
+
* keycap sequence is drawn as one two-cell glyph. */
|
|
12
|
+
const EMOJI_PRESENTATION_SELECTOR = '\uFE0F'
|
|
13
|
+
/** Unicode default-ignorable code points carry no width of their own. */
|
|
14
|
+
const IGNORABLE_GLYPH = /^\p{Default_Ignorable_Code_Point}$/u
|
|
15
|
+
/** East Asian Wide/Fullwidth ranges — two cells, never the ambiguous width
|
|
16
|
+
* (the host measures ambiguous as narrow, so `▸`/`✓`/`—` stay one cell).
|
|
17
|
+
* Generated from the host's own measurement (`string-width` over every code
|
|
18
|
+
* point, emoji excluded), so this table can be regenerated verbatim when the
|
|
19
|
+
* host upgrades its Unicode data instead of being hand-edited. */
|
|
20
|
+
const CELL_WIDE_RANGES: readonly (readonly [number, number])[] = [
|
|
21
|
+
[0x1100, 0x115e],
|
|
22
|
+
[0x2329, 0x232a],
|
|
23
|
+
[0x2630, 0x2637],
|
|
24
|
+
[0x268a, 0x268f],
|
|
25
|
+
[0x2e80, 0x2e99],
|
|
26
|
+
[0x2e9b, 0x2ef3],
|
|
27
|
+
[0x2f00, 0x2fd5],
|
|
28
|
+
[0x2ff0, 0x302f],
|
|
29
|
+
[0x3031, 0x303c],
|
|
30
|
+
[0x303e, 0x303e],
|
|
31
|
+
[0x3041, 0x3096],
|
|
32
|
+
[0x3099, 0x30ff],
|
|
33
|
+
[0x3105, 0x312f],
|
|
34
|
+
[0x3131, 0x3163],
|
|
35
|
+
[0x3165, 0x318e],
|
|
36
|
+
[0x3190, 0x31e5],
|
|
37
|
+
[0x31ef, 0x321e],
|
|
38
|
+
[0x3220, 0x3247],
|
|
39
|
+
[0x3250, 0x3296],
|
|
40
|
+
[0x3298, 0x3298],
|
|
41
|
+
[0x329a, 0xa48c],
|
|
42
|
+
[0xa490, 0xa4c6],
|
|
43
|
+
[0xa960, 0xa97c],
|
|
44
|
+
[0xac00, 0xd7a3],
|
|
45
|
+
[0xf900, 0xfaff],
|
|
46
|
+
[0xfe10, 0xfe19],
|
|
47
|
+
[0xfe30, 0xfe52],
|
|
48
|
+
[0xfe54, 0xfe66],
|
|
49
|
+
[0xfe68, 0xfe6b],
|
|
50
|
+
[0xff01, 0xff60],
|
|
51
|
+
[0xffe0, 0xffe6],
|
|
52
|
+
[0x16fe0, 0x16fe4],
|
|
53
|
+
[0x16ff0, 0x16ff6],
|
|
54
|
+
[0x17000, 0x18cda],
|
|
55
|
+
[0x18cff, 0x18d20],
|
|
56
|
+
[0x18d80, 0x18df2],
|
|
57
|
+
[0x18e00, 0x19191],
|
|
58
|
+
[0x191a0, 0x191d2],
|
|
59
|
+
[0x1aff0, 0x1aff3],
|
|
60
|
+
[0x1aff5, 0x1affb],
|
|
61
|
+
[0x1affd, 0x1affe],
|
|
62
|
+
[0x1b000, 0x1b128],
|
|
63
|
+
[0x1b132, 0x1b132],
|
|
64
|
+
[0x1b150, 0x1b152],
|
|
65
|
+
[0x1b155, 0x1b155],
|
|
66
|
+
[0x1b164, 0x1b168],
|
|
67
|
+
[0x1b170, 0x1b2fb],
|
|
68
|
+
[0x1d300, 0x1d356],
|
|
69
|
+
[0x1d360, 0x1d376],
|
|
70
|
+
[0x1f200, 0x1f200],
|
|
71
|
+
[0x1f210, 0x1f219],
|
|
72
|
+
[0x1f21b, 0x1f22e],
|
|
73
|
+
[0x1f230, 0x1f231],
|
|
74
|
+
[0x1f23b, 0x1f23b],
|
|
75
|
+
[0x1f240, 0x1f248],
|
|
76
|
+
[0x1f260, 0x1f265],
|
|
77
|
+
[0x20000, 0x2fffd],
|
|
78
|
+
[0x30000, 0x3fffd],
|
|
79
|
+
]
|
|
80
|
+
|
|
81
|
+
/** One code point the terminal draws no cell for: control characters, the
|
|
82
|
+
* zero-width joiners/marks, combining marks and variation selectors. */
|
|
83
|
+
function isZeroWidthCodePoint(code: number, glyph: string): boolean {
|
|
84
|
+
if (code <= 0x1f || (code >= 0x7f && code <= 0x9f)) return true
|
|
85
|
+
if (code >= 0x200b && code <= 0x200f) return true
|
|
86
|
+
if (code === 0xfeff) return true
|
|
87
|
+
if (code >= 0x300 && code <= 0x36f) return true
|
|
88
|
+
if (code >= 0x1ab0 && code <= 0x1aff) return true
|
|
89
|
+
if (code >= 0x1dc0 && code <= 0x1dff) return true
|
|
90
|
+
if (code >= 0x20d0 && code <= 0x20ff) return true
|
|
91
|
+
if (code >= 0xfe00 && code <= 0xfe0f) return true
|
|
92
|
+
if (code >= 0xfe20 && code <= 0xfe2f) return true
|
|
93
|
+
if (code >= 0xe0100 && code <= 0xe01ef) return true
|
|
94
|
+
if (code >= 0xd800 && code <= 0xdfff) return true // a lone surrogate
|
|
95
|
+
return IGNORABLE_GLYPH.test(glyph)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function isWideCodePoint(code: number): boolean {
|
|
99
|
+
for (const [low, high] of CELL_WIDE_RANGES) {
|
|
100
|
+
if (code < low) return false
|
|
101
|
+
if (code <= high) return true
|
|
102
|
+
}
|
|
103
|
+
return false
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Cells one grapheme occupies. */
|
|
107
|
+
function graphemeWidth(segment: string): number {
|
|
108
|
+
if (EMOJI_GLYPH.test(segment)) return 2
|
|
109
|
+
// A lone VS16 carries no width; after a base it asks for emoji presentation.
|
|
110
|
+
if (segment.length > 1 && segment.includes(EMOJI_PRESENTATION_SELECTOR)) return 2
|
|
111
|
+
let width = 0
|
|
112
|
+
for (const glyph of segment) {
|
|
113
|
+
const code = glyph.codePointAt(0) ?? 0
|
|
114
|
+
if (isZeroWidthCodePoint(code, glyph)) continue
|
|
115
|
+
width += isWideCodePoint(code) ? 2 : 1
|
|
116
|
+
}
|
|
117
|
+
return width
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Terminal cells `value` occupies — the unit every column in this plugin is
|
|
121
|
+
* measured in. Mirrors the host (that is how `@opentui/core` sizes a text
|
|
122
|
+
* run): emoji count as two cells, East Asian Wide/Fullwidth as two, East
|
|
123
|
+
* Asian Ambiguous as one, combining marks and variation selectors as none.
|
|
124
|
+
* Pure. */
|
|
125
|
+
export function cellWidth(value: string): number {
|
|
126
|
+
let width = 0
|
|
127
|
+
for (const { segment } of GRAPHEMES.segment(value)) width += graphemeWidth(segment)
|
|
128
|
+
return width
|
|
129
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
2
|
"//": "Typecheck gate for the V2 TUI entry only. `tsc --noEmit` at the package root was a no-op: with no tsconfig in this directory tsc walked up to the repo tsconfig.json, which excludes src/plugins/tui entirely — so nothing here was checked. tsdown bundles without type-checking, which is how an invalid event name (session.updated, absent from the OpenCode V2 event union) shipped in the V2 rewrite. This config points tsc at the V2 entry plus its ambient declarations so a type error in src/index-v2.tsx fails `npm run typecheck`. The V1 entry (src/index.tsx) is intentionally NOT included: it carries pre-existing type errors out of scope for the V2 gate. Keep this file strict JSON — the repo pre-commit `check-json` hook rejects comments.",
|
|
3
3
|
"extends": "./tsconfig.build.json",
|
|
4
|
-
"include": ["src/index-v2.tsx", "src/opencode-v2.d.ts"]
|
|
4
|
+
"include": ["src/index-v2.tsx", "src/server-v2.ts", "src/opencode-v2.d.ts"]
|
|
5
5
|
}
|