pantheon-opencode 1.6.2-beta.8 → 1.6.2
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/CHANGELOG.md +121 -3
- package/README.md +34 -60
- package/README.pt-BR.md +10 -12
- package/bin/pantheon-init.mjs +30 -32
- package/package.json +3 -3
- package/plugin.json +1 -1
- package/pyproject.toml +1 -1
- package/scripts/decision-router.mjs +41 -56
- package/scripts/install/decision-router-config.mjs +67 -1
- package/scripts/install/npx-cache-sweep.mjs +103 -0
- package/scripts/install/opencode.mjs +7 -7
- package/scripts/install/plugin.mjs +4 -3
- package/scripts/install/strings.mjs +4 -0
- package/scripts/release-gates.mjs +6 -82
- package/scripts/sync-tui.mjs +3 -3
- package/src/agents/aphrodite.md +1 -1
- package/src/agents/apollo.md +1 -0
- package/src/agents/athena.md +1 -0
- package/src/agents/demeter.md +1 -1
- package/src/agents/gaia.md +1 -0
- package/src/agents/hephaestus.md +1 -1
- package/src/agents/hermes.md +1 -1
- package/src/agents/iris.md +1 -1
- package/src/agents/mnemosyne.md +1 -0
- package/src/agents/nyx.md +1 -1
- package/src/agents/prometheus.md +1 -1
- package/src/agents/talos.md +1 -1
- package/src/agents/themis.md +1 -1
- package/src/agents/zeus.md +5 -2
- package/src/pantheon/jev-decision-tool.ts +366 -114
- package/src/pantheon/jev-decisions.ts +11 -13
- package/src/pantheon/jev-escalation.ts +639 -0
- package/src/pantheon/jev-quality-score.ts +432 -0
- package/src/pantheon/jev-risk-gate.ts +479 -0
- package/src/pantheon/local-decision-prompt.ts +12 -18
- package/src/pantheon/v2-decision-telemetry.ts +14 -4
- package/src/pantheon/v2-native-decisions.ts +1 -1
- package/src/plugin-v2.ts +201 -24
- package/src/plugins/tui/dist/server-v2.d.ts +19 -11
- package/src/plugins/tui/dist/server-v2.d.ts.map +1 -1
- package/src/plugins/tui/dist/server-v2.js +19 -789
- package/src/plugins/tui/dist/server-v2.js.map +1 -1
- package/src/plugins/tui/dist/tui-v2.d.ts +12 -331
- package/src/plugins/tui/dist/tui-v2.d.ts.map +1 -1
- package/src/plugins/tui/dist/tui-v2.js +257 -5370
- package/src/plugins/tui/dist/tui-v2.js.map +1 -1
- package/src/plugins/tui/dist/tui.js +1 -1
- package/src/plugins/tui/package-lock.json +2 -2
- package/src/plugins/tui/package.json +1 -1
- package/src/plugins/tui/src/index-v2.tsx +213 -86
- package/src/plugins/tui/src/server-v2.ts +18 -982
- package/src/plugins/tui/tsconfig.typecheck.json +1 -6
- package/src/plugins/tui/tsdown.config.ts +1 -11
- package/src/plugins/tui/dist/cost-data-CCZUs1aJ.js +0 -749
- package/src/plugins/tui/dist/cost-data-CCZUs1aJ.js.map +0 -1
- package/src/plugins/tui/dist/quota-adapters.d.ts +0 -89
- package/src/plugins/tui/dist/quota-adapters.d.ts.map +0 -1
- package/src/plugins/tui/dist/quota-adapters.js +0 -1142
- package/src/plugins/tui/dist/quota-adapters.js.map +0 -1
- package/src/plugins/tui/dist/quota-cli.d.ts +0 -17
- package/src/plugins/tui/dist/quota-cli.d.ts.map +0 -1
- package/src/plugins/tui/dist/quota-cli.js +0 -184
- package/src/plugins/tui/dist/quota-cli.js.map +0 -1
- package/src/plugins/tui/dist/quota-rpc-DMmXBhBe.js +0 -684
- package/src/plugins/tui/dist/quota-rpc-DMmXBhBe.js.map +0 -1
- package/src/plugins/tui/dist/usage-history-contract-nt1rnBPE.js +0 -118
- package/src/plugins/tui/dist/usage-history-contract-nt1rnBPE.js.map +0 -1
- package/src/plugins/tui/dist/usage-history-worker.js +0 -25
- package/src/plugins/tui/dist/usage-history-worker.js.map +0 -1
- package/src/plugins/tui/src/go-key-diagnostic-ui.ts +0 -100
- package/src/plugins/tui/src/go-key-diagnostic.ts +0 -125
- package/src/plugins/tui/src/quota-adapters.ts +0 -1598
- package/src/plugins/tui/src/quota-cli.ts +0 -237
- package/src/plugins/tui/src/quota-rpc.ts +0 -373
- package/src/plugins/tui/src/quota-v2-model.ts +0 -1032
- package/src/plugins/tui/src/quota-v2.tsx +0 -1238
- package/src/plugins/tui/src/usage-history-contract.ts +0 -217
- package/src/plugins/tui/src/usage-history-validation.ts +0 -104
- package/src/plugins/tui/src/usage-history-view.tsx +0 -236
- package/src/plugins/tui/src/usage-history-worker.ts +0 -25
- package/src/plugins/tui/src/usage-view-model.ts +0 -272
|
@@ -1,217 +0,0 @@
|
|
|
1
|
-
export const MAX_HISTORY_BUCKETS = 366
|
|
2
|
-
export const MAX_HISTORY_MODELS = 64
|
|
3
|
-
export const MAX_HISTORY_QUERY_MS = 5_000
|
|
4
|
-
|
|
5
|
-
export type UsageHistoryRpcPeriod = '7d' | '15d' | '30d' | 'total'
|
|
6
|
-
|
|
7
|
-
interface UsageHistorySourceTotals {
|
|
8
|
-
input: number
|
|
9
|
-
output: number
|
|
10
|
-
cacheRead: number
|
|
11
|
-
cacheWrite: number
|
|
12
|
-
total: number
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
interface UsageHistorySourceBucket extends UsageHistorySourceTotals {
|
|
16
|
-
date: string
|
|
17
|
-
estimatedUsd?: number
|
|
18
|
-
pricingComplete: boolean
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
interface UsageHistorySourceModel {
|
|
22
|
-
providerID: string
|
|
23
|
-
modelID: string
|
|
24
|
-
totals: UsageHistorySourceTotals
|
|
25
|
-
buckets: UsageHistorySourceBucket[]
|
|
26
|
-
estimatedUsd?: number
|
|
27
|
-
pricingComplete: boolean
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
interface UsageHistorySourceResult {
|
|
31
|
-
period: UsageHistoryRpcPeriod
|
|
32
|
-
coverageStart: string | null
|
|
33
|
-
partial: boolean
|
|
34
|
-
rows: number
|
|
35
|
-
buckets: UsageHistorySourceBucket[]
|
|
36
|
-
totals: UsageHistorySourceTotals
|
|
37
|
-
models: UsageHistorySourceModel[]
|
|
38
|
-
pricing: {
|
|
39
|
-
status: 'empty' | 'unavailable' | 'partial' | 'complete'
|
|
40
|
-
complete: boolean
|
|
41
|
-
estimatedUsd?: number
|
|
42
|
-
unpricedModels: number
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export interface UsageHistoryRpcTotals {
|
|
47
|
-
input: number
|
|
48
|
-
output: number
|
|
49
|
-
cacheRead: number
|
|
50
|
-
cacheWrite: number
|
|
51
|
-
total: number
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export interface UsageHistoryRpcBucket extends UsageHistoryRpcTotals {
|
|
55
|
-
date: string
|
|
56
|
-
estimatedUsd?: number
|
|
57
|
-
pricingComplete: boolean
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export interface UsageHistoryRpcModel {
|
|
61
|
-
providerID: string
|
|
62
|
-
modelID: string
|
|
63
|
-
totals: UsageHistoryRpcTotals
|
|
64
|
-
estimatedUsd?: number
|
|
65
|
-
pricingComplete: boolean
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
export interface UsageHistoryRpcAvailable {
|
|
69
|
-
status: 'available' | 'partial'
|
|
70
|
-
period: UsageHistoryRpcPeriod
|
|
71
|
-
coverageStart: string | null
|
|
72
|
-
partial: boolean
|
|
73
|
-
truncated: boolean
|
|
74
|
-
rows: number
|
|
75
|
-
buckets: UsageHistoryRpcBucket[]
|
|
76
|
-
totals: UsageHistoryRpcTotals
|
|
77
|
-
models: UsageHistoryRpcModel[]
|
|
78
|
-
pricing: {
|
|
79
|
-
status: 'empty' | 'unavailable' | 'partial' | 'complete'
|
|
80
|
-
complete: boolean
|
|
81
|
-
billed: false
|
|
82
|
-
estimatedUsd?: number
|
|
83
|
-
unpricedModels: number
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
export interface UsageHistoryRpcUnavailable {
|
|
88
|
-
status: 'unavailable'
|
|
89
|
-
reason: 'invalid_period' | 'history_unavailable' | 'history_timeout'
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
export type UsageHistoryRpcResult = UsageHistoryRpcAvailable | UsageHistoryRpcUnavailable
|
|
93
|
-
|
|
94
|
-
export const USAGE_HISTORY_PERIODS: readonly UsageHistoryRpcPeriod[] = ['7d', '15d', '30d', 'total']
|
|
95
|
-
|
|
96
|
-
const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER
|
|
97
|
-
const SAFE_LABEL = /^[A-Za-z0-9][A-Za-z0-9._:/-]{0,127}$/
|
|
98
|
-
const ISO_DATE = /^\d{4}-\d{2}-\d{2}$/
|
|
99
|
-
|
|
100
|
-
function safeCount(value: number, markTruncated: () => void): number {
|
|
101
|
-
if (!Number.isFinite(value) || value < 0) {
|
|
102
|
-
markTruncated()
|
|
103
|
-
return 0
|
|
104
|
-
}
|
|
105
|
-
if (!Number.isSafeInteger(value)) {
|
|
106
|
-
markTruncated()
|
|
107
|
-
return Math.min(Math.floor(value), MAX_SAFE_INTEGER)
|
|
108
|
-
}
|
|
109
|
-
return value
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
function safeEstimate(value: number | undefined, markTruncated: () => void): number | undefined {
|
|
113
|
-
if (value === undefined) return undefined
|
|
114
|
-
if (!Number.isFinite(value) || value < 0) {
|
|
115
|
-
markTruncated()
|
|
116
|
-
return undefined
|
|
117
|
-
}
|
|
118
|
-
const rounded = Math.round(value * 1e8) / 1e8
|
|
119
|
-
if (!Number.isFinite(rounded)) {
|
|
120
|
-
markTruncated()
|
|
121
|
-
return undefined
|
|
122
|
-
}
|
|
123
|
-
return rounded
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
function safeLabel(value: string, markTruncated: () => void): string {
|
|
127
|
-
if (!SAFE_LABEL.test(value)) {
|
|
128
|
-
markTruncated()
|
|
129
|
-
return 'unknown'
|
|
130
|
-
}
|
|
131
|
-
return value
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
function projectTotals(
|
|
135
|
-
value: UsageHistorySourceTotals,
|
|
136
|
-
markTruncated: () => void,
|
|
137
|
-
): UsageHistoryRpcTotals {
|
|
138
|
-
return {
|
|
139
|
-
input: safeCount(value.input, markTruncated),
|
|
140
|
-
output: safeCount(value.output, markTruncated),
|
|
141
|
-
cacheRead: safeCount(value.cacheRead, markTruncated),
|
|
142
|
-
cacheWrite: safeCount(value.cacheWrite, markTruncated),
|
|
143
|
-
total: safeCount(value.total, markTruncated),
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/** Project cost-command output to a bounded, non-identifying TUI contract. */
|
|
148
|
-
export function projectUsageHistoryResult(
|
|
149
|
-
result: UsageHistorySourceResult,
|
|
150
|
-
): UsageHistoryRpcAvailable {
|
|
151
|
-
let unsafeOrTruncated = false
|
|
152
|
-
const markTruncated = (): void => {
|
|
153
|
-
unsafeOrTruncated = true
|
|
154
|
-
}
|
|
155
|
-
const sourceBuckets = result.buckets
|
|
156
|
-
const sourceModels = result.models
|
|
157
|
-
if (sourceBuckets.length > MAX_HISTORY_BUCKETS || sourceModels.length > MAX_HISTORY_MODELS) {
|
|
158
|
-
markTruncated()
|
|
159
|
-
}
|
|
160
|
-
const buckets = sourceBuckets.slice(-MAX_HISTORY_BUCKETS).flatMap((bucket) => {
|
|
161
|
-
if (!ISO_DATE.test(bucket.date)) {
|
|
162
|
-
markTruncated()
|
|
163
|
-
return []
|
|
164
|
-
}
|
|
165
|
-
const estimatedUsd = safeEstimate(bucket.estimatedUsd, markTruncated)
|
|
166
|
-
return [
|
|
167
|
-
{
|
|
168
|
-
date: bucket.date,
|
|
169
|
-
...projectTotals(bucket, markTruncated),
|
|
170
|
-
...(estimatedUsd === undefined ? {} : { estimatedUsd }),
|
|
171
|
-
pricingComplete: bucket.pricingComplete === true,
|
|
172
|
-
},
|
|
173
|
-
]
|
|
174
|
-
})
|
|
175
|
-
const models = sourceModels.slice(0, MAX_HISTORY_MODELS).map((model) => {
|
|
176
|
-
const estimatedUsd = safeEstimate(model.estimatedUsd, markTruncated)
|
|
177
|
-
return {
|
|
178
|
-
providerID: safeLabel(model.providerID, markTruncated),
|
|
179
|
-
modelID: safeLabel(model.modelID, markTruncated),
|
|
180
|
-
totals: projectTotals(model.totals, markTruncated),
|
|
181
|
-
...(estimatedUsd === undefined ? {} : { estimatedUsd }),
|
|
182
|
-
pricingComplete: model.pricingComplete === true,
|
|
183
|
-
}
|
|
184
|
-
})
|
|
185
|
-
let coverageStart = result.coverageStart
|
|
186
|
-
if (coverageStart !== null && !ISO_DATE.test(coverageStart)) {
|
|
187
|
-
markTruncated()
|
|
188
|
-
coverageStart = null
|
|
189
|
-
}
|
|
190
|
-
const pricingEstimate = safeEstimate(result.pricing.estimatedUsd, markTruncated)
|
|
191
|
-
const rows = safeCount(result.rows, markTruncated)
|
|
192
|
-
const totals = projectTotals(result.totals, markTruncated)
|
|
193
|
-
const unpricedModels = safeCount(result.pricing.unpricedModels, markTruncated)
|
|
194
|
-
const truncated = unsafeOrTruncated
|
|
195
|
-
return {
|
|
196
|
-
status: result.partial || truncated ? 'partial' : 'available',
|
|
197
|
-
period: result.period,
|
|
198
|
-
coverageStart,
|
|
199
|
-
partial: result.partial,
|
|
200
|
-
truncated,
|
|
201
|
-
rows,
|
|
202
|
-
buckets,
|
|
203
|
-
totals,
|
|
204
|
-
models,
|
|
205
|
-
pricing: {
|
|
206
|
-
status: result.pricing.status,
|
|
207
|
-
complete: result.pricing.complete === true,
|
|
208
|
-
billed: false,
|
|
209
|
-
...(pricingEstimate === undefined ? {} : { estimatedUsd: pricingEstimate }),
|
|
210
|
-
unpricedModels,
|
|
211
|
-
},
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
export function isUsageHistoryPeriod(value: unknown): value is UsageHistoryRpcPeriod {
|
|
216
|
-
return typeof value === 'string' && USAGE_HISTORY_PERIODS.includes(value as UsageHistoryRpcPeriod)
|
|
217
|
-
}
|
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
UsageHistoryRpcBucket,
|
|
3
|
-
UsageHistoryRpcModel,
|
|
4
|
-
UsageHistoryRpcResult,
|
|
5
|
-
UsageHistoryRpcTotals,
|
|
6
|
-
} from './usage-history-contract'
|
|
7
|
-
import type { ActiveContextCostResult } from './usage-view-model'
|
|
8
|
-
|
|
9
|
-
function asRecord(value: unknown): Record<string, unknown> | null {
|
|
10
|
-
return typeof value === 'object' && value !== null ? (value as Record<string, unknown>) : null
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function isTotals(value: unknown): value is UsageHistoryRpcTotals {
|
|
14
|
-
const totals = asRecord(value)
|
|
15
|
-
return (
|
|
16
|
-
totals !== null &&
|
|
17
|
-
['input', 'output', 'cacheRead', 'cacheWrite', 'total'].every(
|
|
18
|
-
(key) =>
|
|
19
|
-
typeof totals[key] === 'number' && Number.isSafeInteger(totals[key]) && totals[key] >= 0,
|
|
20
|
-
)
|
|
21
|
-
)
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function isHistoryBucket(value: unknown): value is UsageHistoryRpcBucket {
|
|
25
|
-
const bucket = asRecord(value)
|
|
26
|
-
return (
|
|
27
|
-
bucket !== null &&
|
|
28
|
-
typeof bucket.date === 'string' &&
|
|
29
|
-
/^\d{4}-\d{2}-\d{2}$/.test(bucket.date) &&
|
|
30
|
-
isTotals(bucket) &&
|
|
31
|
-
typeof bucket.pricingComplete === 'boolean' &&
|
|
32
|
-
(bucket.estimatedUsd === undefined ||
|
|
33
|
-
(typeof bucket.estimatedUsd === 'number' && Number.isFinite(bucket.estimatedUsd)))
|
|
34
|
-
)
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function isHistoryModel(value: unknown): value is UsageHistoryRpcModel {
|
|
38
|
-
const model = asRecord(value)
|
|
39
|
-
return (
|
|
40
|
-
model !== null &&
|
|
41
|
-
typeof model.providerID === 'string' &&
|
|
42
|
-
typeof model.modelID === 'string' &&
|
|
43
|
-
isTotals(model.totals) &&
|
|
44
|
-
typeof model.pricingComplete === 'boolean' &&
|
|
45
|
-
(model.estimatedUsd === undefined ||
|
|
46
|
-
(typeof model.estimatedUsd === 'number' && Number.isFinite(model.estimatedUsd)))
|
|
47
|
-
)
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export function isUsageHistoryRpcResult(value: unknown): value is UsageHistoryRpcResult {
|
|
51
|
-
const result = asRecord(value)
|
|
52
|
-
if (!result) return false
|
|
53
|
-
if (result.status === 'unavailable') {
|
|
54
|
-
return ['invalid_period', 'history_unavailable', 'history_timeout'].includes(
|
|
55
|
-
String(result.reason),
|
|
56
|
-
)
|
|
57
|
-
}
|
|
58
|
-
if (result.status !== 'available' && result.status !== 'partial') return false
|
|
59
|
-
const pricing = asRecord(result.pricing)
|
|
60
|
-
return (
|
|
61
|
-
['7d', '15d', '30d', 'total'].includes(String(result.period)) &&
|
|
62
|
-
(result.coverageStart === null || typeof result.coverageStart === 'string') &&
|
|
63
|
-
typeof result.partial === 'boolean' &&
|
|
64
|
-
typeof result.truncated === 'boolean' &&
|
|
65
|
-
typeof result.rows === 'number' &&
|
|
66
|
-
Number.isSafeInteger(result.rows) &&
|
|
67
|
-
result.rows >= 0 &&
|
|
68
|
-
Array.isArray(result.buckets) &&
|
|
69
|
-
result.buckets.every(isHistoryBucket) &&
|
|
70
|
-
isTotals(result.totals) &&
|
|
71
|
-
Array.isArray(result.models) &&
|
|
72
|
-
result.models.every(isHistoryModel) &&
|
|
73
|
-
pricing !== null &&
|
|
74
|
-
['empty', 'unavailable', 'partial', 'complete'].includes(String(pricing.status)) &&
|
|
75
|
-
typeof pricing.complete === 'boolean' &&
|
|
76
|
-
pricing.billed === false &&
|
|
77
|
-
typeof pricing.unpricedModels === 'number' &&
|
|
78
|
-
Number.isSafeInteger(pricing.unpricedModels) &&
|
|
79
|
-
pricing.unpricedModels >= 0 &&
|
|
80
|
-
(pricing.estimatedUsd === undefined ||
|
|
81
|
-
(typeof pricing.estimatedUsd === 'number' && Number.isFinite(pricing.estimatedUsd)))
|
|
82
|
-
)
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export function isActiveContextCostResult(value: unknown): value is ActiveContextCostResult {
|
|
86
|
-
const result = asRecord(value)
|
|
87
|
-
if (result?.billed !== false) return false
|
|
88
|
-
if (result.cacheDiscountApplied !== false) return false
|
|
89
|
-
if (result.basis !== 'pre-cache-context-tokens-times-input-price') return false
|
|
90
|
-
if (result.status === 'estimated') {
|
|
91
|
-
return typeof result.estimatedUsd === 'number' && Number.isFinite(result.estimatedUsd)
|
|
92
|
-
}
|
|
93
|
-
return (
|
|
94
|
-
result.status === 'unavailable' &&
|
|
95
|
-
[
|
|
96
|
-
'invalid-context-token-count',
|
|
97
|
-
'missing-model-identity',
|
|
98
|
-
'no-pricing',
|
|
99
|
-
'unknown-model',
|
|
100
|
-
'ambiguous-model',
|
|
101
|
-
'missing-cache-price',
|
|
102
|
-
].includes(String(result.reason))
|
|
103
|
-
)
|
|
104
|
-
}
|
|
@@ -1,236 +0,0 @@
|
|
|
1
|
-
/** @jsxImportSource @opentui/solid */
|
|
2
|
-
|
|
3
|
-
import type { Context, KeymapLayer, PanelInput } from '@opencode/plugin/tui/context'
|
|
4
|
-
import { createEffect, createSignal, For, type JSX, onCleanup, Show } from 'solid-js'
|
|
5
|
-
import { quotaRpc } from './quota-rpc'
|
|
6
|
-
import type { UsageHistoryRpcPeriod, UsageHistoryRpcResult } from './usage-history-contract'
|
|
7
|
-
import { isActiveContextCostResult, isUsageHistoryRpcResult } from './usage-history-validation'
|
|
8
|
-
import {
|
|
9
|
-
type ActiveContextCostResult,
|
|
10
|
-
activeContextEstimateErrorLines,
|
|
11
|
-
activeContextEstimateLines,
|
|
12
|
-
activeContextEstimateMissingLines,
|
|
13
|
-
activeContextEstimatePendingLines,
|
|
14
|
-
USAGE_HISTORY_PERIOD_BINDINGS,
|
|
15
|
-
usageHistoryChartLines,
|
|
16
|
-
usageHistoryControlsLines,
|
|
17
|
-
usageHistoryCoverageLabel,
|
|
18
|
-
usageHistoryPartialWarning,
|
|
19
|
-
usageHistoryPricingLabel,
|
|
20
|
-
usageHistorySelectorLines,
|
|
21
|
-
usageHistoryTextLines,
|
|
22
|
-
usageHistoryUnavailableMessage,
|
|
23
|
-
} from './usage-view-model'
|
|
24
|
-
|
|
25
|
-
function location(context: Context): ReturnType<Context['data']['location']['default']> {
|
|
26
|
-
return context.location ?? context.data.location.default()
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export function ActiveContextEstimate(props: {
|
|
30
|
-
context: Context
|
|
31
|
-
providerID: string | null
|
|
32
|
-
modelID: string | null
|
|
33
|
-
contextTokens: number | null
|
|
34
|
-
width: number
|
|
35
|
-
}): JSX.Element {
|
|
36
|
-
const [result, setResult] = createSignal<ActiveContextCostResult | null>(null)
|
|
37
|
-
const [loading, setLoading] = createSignal(false)
|
|
38
|
-
const [failed, setFailed] = createSignal(false)
|
|
39
|
-
const client = props.context.client.rpc(quotaRpc)
|
|
40
|
-
|
|
41
|
-
createEffect(() => {
|
|
42
|
-
const providerID = props.providerID
|
|
43
|
-
const modelID = props.modelID
|
|
44
|
-
const contextTokens = props.contextTokens
|
|
45
|
-
if (contextTokens === null) {
|
|
46
|
-
setResult(null)
|
|
47
|
-
setFailed(false)
|
|
48
|
-
setLoading(false)
|
|
49
|
-
return
|
|
50
|
-
}
|
|
51
|
-
if (!providerID || !modelID) {
|
|
52
|
-
setResult({
|
|
53
|
-
status: 'unavailable',
|
|
54
|
-
billed: false,
|
|
55
|
-
basis: 'pre-cache-context-tokens-times-input-price',
|
|
56
|
-
cacheDiscountApplied: false,
|
|
57
|
-
reason: 'missing-model-identity',
|
|
58
|
-
})
|
|
59
|
-
setFailed(false)
|
|
60
|
-
setLoading(false)
|
|
61
|
-
return
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const controller = new AbortController()
|
|
65
|
-
let current = true
|
|
66
|
-
setResult(null)
|
|
67
|
-
setFailed(false)
|
|
68
|
-
setLoading(true)
|
|
69
|
-
try {
|
|
70
|
-
void client
|
|
71
|
-
.active_context_cost(
|
|
72
|
-
{ providerID, modelID, contextTokens },
|
|
73
|
-
{ signal: controller.signal, location: location(props.context) },
|
|
74
|
-
)
|
|
75
|
-
.then((response) => {
|
|
76
|
-
if (!current) return
|
|
77
|
-
if (isActiveContextCostResult(response)) setResult(response)
|
|
78
|
-
else setFailed(true)
|
|
79
|
-
})
|
|
80
|
-
.catch(() => {
|
|
81
|
-
if (current) setFailed(true)
|
|
82
|
-
})
|
|
83
|
-
.finally(() => {
|
|
84
|
-
if (current) setLoading(false)
|
|
85
|
-
})
|
|
86
|
-
} catch {
|
|
87
|
-
setFailed(true)
|
|
88
|
-
setLoading(false)
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
onCleanup(() => {
|
|
92
|
-
current = false
|
|
93
|
-
controller.abort()
|
|
94
|
-
})
|
|
95
|
-
})
|
|
96
|
-
|
|
97
|
-
const lines = (): string[] => {
|
|
98
|
-
if (loading()) return activeContextEstimatePendingLines(props.width)
|
|
99
|
-
if (failed()) return activeContextEstimateErrorLines(props.width)
|
|
100
|
-
const current = result()
|
|
101
|
-
if (current) return activeContextEstimateLines(current, props.width)
|
|
102
|
-
return activeContextEstimateMissingLines(props.width)
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
return (
|
|
106
|
-
<box flexDirection="column">
|
|
107
|
-
<For each={lines()}>{(line) => <text>{line}</text>}</For>
|
|
108
|
-
</box>
|
|
109
|
-
)
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export function UsageHistorySection(props: {
|
|
113
|
-
context: Context
|
|
114
|
-
panel: PanelInput
|
|
115
|
-
width: number
|
|
116
|
-
}): JSX.Element {
|
|
117
|
-
const [period, setPeriod] = createSignal<UsageHistoryRpcPeriod>('7d')
|
|
118
|
-
const [result, setResult] = createSignal<UsageHistoryRpcResult | null>(null)
|
|
119
|
-
const [loading, setLoading] = createSignal(true)
|
|
120
|
-
const [requestFailed, setRequestFailed] = createSignal(false)
|
|
121
|
-
const client = props.context.client.rpc(quotaRpc)
|
|
122
|
-
|
|
123
|
-
const keymap = (): KeymapLayer => ({
|
|
124
|
-
mode: 'global',
|
|
125
|
-
enabled: () => props.panel.focused === true,
|
|
126
|
-
commands: USAGE_HISTORY_PERIOD_BINDINGS.map(({ period: selected, bind, label }) => ({
|
|
127
|
-
id: `pantheon.quota.history.${selected}`,
|
|
128
|
-
title: `Histórico de uso: ${label}`,
|
|
129
|
-
bind,
|
|
130
|
-
run: () => {
|
|
131
|
-
if (props.panel.focused !== true) return false
|
|
132
|
-
setPeriod(selected)
|
|
133
|
-
return undefined
|
|
134
|
-
},
|
|
135
|
-
})),
|
|
136
|
-
})
|
|
137
|
-
props.context.keymap.layer(keymap)
|
|
138
|
-
|
|
139
|
-
createEffect(() => {
|
|
140
|
-
const selectedPeriod = period()
|
|
141
|
-
const controller = new AbortController()
|
|
142
|
-
let current = true
|
|
143
|
-
setLoading(true)
|
|
144
|
-
setRequestFailed(false)
|
|
145
|
-
setResult(null)
|
|
146
|
-
|
|
147
|
-
try {
|
|
148
|
-
void client
|
|
149
|
-
.usage_history(
|
|
150
|
-
{ period: selectedPeriod },
|
|
151
|
-
{ signal: controller.signal, location: location(props.context) },
|
|
152
|
-
)
|
|
153
|
-
.then((response) => {
|
|
154
|
-
if (!current) return
|
|
155
|
-
if (isUsageHistoryRpcResult(response)) setResult(response)
|
|
156
|
-
else setRequestFailed(true)
|
|
157
|
-
})
|
|
158
|
-
.catch(() => {
|
|
159
|
-
if (current) setRequestFailed(true)
|
|
160
|
-
})
|
|
161
|
-
.finally(() => {
|
|
162
|
-
if (current) setLoading(false)
|
|
163
|
-
})
|
|
164
|
-
} catch {
|
|
165
|
-
setRequestFailed(true)
|
|
166
|
-
setLoading(false)
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
onCleanup(() => {
|
|
170
|
-
current = false
|
|
171
|
-
controller.abort()
|
|
172
|
-
})
|
|
173
|
-
})
|
|
174
|
-
|
|
175
|
-
const chartLines = (): string[] => {
|
|
176
|
-
const current = result()
|
|
177
|
-
return current?.status === 'available' || current?.status === 'partial'
|
|
178
|
-
? usageHistoryChartLines(current, props.width)
|
|
179
|
-
: []
|
|
180
|
-
}
|
|
181
|
-
const unavailable = (): string | null => {
|
|
182
|
-
const current = result()
|
|
183
|
-
return current ? usageHistoryUnavailableMessage(current) : null
|
|
184
|
-
}
|
|
185
|
-
const available = ():
|
|
186
|
-
| Extract<UsageHistoryRpcResult, { status: 'available' | 'partial' }>
|
|
187
|
-
| undefined => {
|
|
188
|
-
const current = result()
|
|
189
|
-
return current?.status === 'available' || current?.status === 'partial' ? current : undefined
|
|
190
|
-
}
|
|
191
|
-
const warning = (): string[] => {
|
|
192
|
-
const current = result()
|
|
193
|
-
if (current?.status !== 'available' && current?.status !== 'partial') return []
|
|
194
|
-
const message = usageHistoryPartialWarning(current)
|
|
195
|
-
return message ? usageHistoryTextLines(message, props.width) : []
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
return (
|
|
199
|
-
<box flexDirection="column" paddingTop={1}>
|
|
200
|
-
<text attributes={1}>Histórico de uso</text>
|
|
201
|
-
<For each={usageHistorySelectorLines(period(), props.width)}>
|
|
202
|
-
{(line) => <text>{line}</text>}
|
|
203
|
-
</For>
|
|
204
|
-
<For each={usageHistoryControlsLines(props.width)}>{(line) => <text>{line}</text>}</For>
|
|
205
|
-
<Show when={loading()}>
|
|
206
|
-
<text>Carregando histórico…</text>
|
|
207
|
-
</Show>
|
|
208
|
-
<Show when={!loading() && requestFailed()}>
|
|
209
|
-
<For each={usageHistoryTextLines('Falha ao consultar o histórico.', props.width)}>
|
|
210
|
-
{(line) => <text>{line}</text>}
|
|
211
|
-
</For>
|
|
212
|
-
</Show>
|
|
213
|
-
<Show when={!loading() && unavailable()}>
|
|
214
|
-
{(message) => (
|
|
215
|
-
<For each={usageHistoryTextLines(message(), props.width)}>
|
|
216
|
-
{(line) => <text>{line}</text>}
|
|
217
|
-
</For>
|
|
218
|
-
)}
|
|
219
|
-
</Show>
|
|
220
|
-
<Show when={!loading() && !requestFailed() && available()}>
|
|
221
|
-
{(history) => (
|
|
222
|
-
<box flexDirection="column">
|
|
223
|
-
<For each={usageHistoryTextLines(usageHistoryCoverageLabel(history()), props.width)}>
|
|
224
|
-
{(line) => <text>{line}</text>}
|
|
225
|
-
</For>
|
|
226
|
-
<For each={warning()}>{(line) => <text>{line}</text>}</For>
|
|
227
|
-
<For each={usageHistoryTextLines(usageHistoryPricingLabel(history()), props.width)}>
|
|
228
|
-
{(line) => <text>{line}</text>}
|
|
229
|
-
</For>
|
|
230
|
-
<For each={chartLines()}>{(line) => <text>{line}</text>}</For>
|
|
231
|
-
</box>
|
|
232
|
-
)}
|
|
233
|
-
</Show>
|
|
234
|
-
</box>
|
|
235
|
-
)
|
|
236
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { parentPort, workerData } from 'node:worker_threads'
|
|
2
|
-
import { queryUsageHistory, resolveCostDbPath } from './cost-data'
|
|
3
|
-
import { isUsageHistoryPeriod, projectUsageHistoryResult } from './usage-history-contract'
|
|
4
|
-
|
|
5
|
-
interface UsageHistoryWorkerInput {
|
|
6
|
-
period?: unknown
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
const port = parentPort
|
|
10
|
-
if (port) {
|
|
11
|
-
void (async () => {
|
|
12
|
-
try {
|
|
13
|
-
const input = workerData as UsageHistoryWorkerInput
|
|
14
|
-
if (!isUsageHistoryPeriod(input.period)) {
|
|
15
|
-
port.postMessage({ ok: false })
|
|
16
|
-
return
|
|
17
|
-
}
|
|
18
|
-
const result = await queryUsageHistory(resolveCostDbPath(), input.period)
|
|
19
|
-
port.postMessage({ ok: true, result: projectUsageHistoryResult(result) })
|
|
20
|
-
} catch {
|
|
21
|
-
// Database errors may contain the local path, SQL, or private ledger data.
|
|
22
|
-
port.postMessage({ ok: false })
|
|
23
|
-
}
|
|
24
|
-
})()
|
|
25
|
-
}
|