catalyst-ai 0.1.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.
- package/README.md +30 -0
- package/android/.gradle/8.9/checksums/checksums.lock +0 -0
- package/android/.gradle/8.9/dependencies-accessors/gc.properties +0 -0
- package/android/.gradle/8.9/fileChanges/last-build.bin +0 -0
- package/android/.gradle/8.9/fileHashes/fileHashes.lock +0 -0
- package/android/.gradle/8.9/gc.properties +0 -0
- package/android/.gradle/9.2.0/checksums/checksums.lock +0 -0
- package/android/.gradle/9.2.0/fileChanges/last-build.bin +0 -0
- package/android/.gradle/9.2.0/fileHashes/fileHashes.bin +0 -0
- package/android/.gradle/9.2.0/fileHashes/fileHashes.lock +0 -0
- package/android/.gradle/9.2.0/gc.properties +0 -0
- package/android/.gradle/buildOutputCleanup/buildOutputCleanup.lock +0 -0
- package/android/.gradle/buildOutputCleanup/cache.properties +2 -0
- package/android/.gradle/vcs-1/gc.properties +0 -0
- package/android/build.gradle.kts +47 -0
- package/android/src/main/AndroidManifest.xml +3 -0
- package/android/src/main/java/io/catalyst/nativeai/AIBridge.kt +24 -0
- package/android/src/main/java/io/catalyst/nativeai/CatalystAIBridge.kt +79 -0
- package/android/src/main/java/io/catalyst/nativeai/NativeBridgeAI.kt +293 -0
- package/android/src/main/resources/META-INF/services/io.catalyst.nativeai.AIBridge +1 -0
- package/package.json +27 -0
- package/scripts/check-peer.js +11 -0
- package/src/buildAttachmentSystemPrompt.js +19 -0
- package/src/index.js +4 -0
- package/src/metrics.js +210 -0
- package/src/pricing.js +14 -0
- package/src/route.js +529 -0
- package/src/useCloudAI.js +315 -0
- package/src/useNativeAI.js +308 -0
- package/src/useWebAI.js +385 -0
package/src/metrics.js
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
// Hardcoded USD price per 1M tokens — mirrors pricing.js (server-side, CJS).
|
|
2
|
+
// Duplicated here (rather than imported) because this file ships to the
|
|
3
|
+
// browser bundle and pricing.js is a Node/CJS-only module used by route.js.
|
|
4
|
+
const PRICING = {
|
|
5
|
+
"gpt-4o-mini": { input: 0.15, cachedInput: 0.075, output: 0.60 },
|
|
6
|
+
"gpt-5": { input: 1.25, cachedInput: 0.125, output: 10.00 },
|
|
7
|
+
"gemini-3.5-flash": { input: 1.50, cachedInput: 0.15, output: 9.00 },
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const DEFAULT_PRICING = { input: 0, cachedInput: 0, output: 0 }
|
|
11
|
+
|
|
12
|
+
function getPricing(model) {
|
|
13
|
+
return PRICING[model] ?? DEFAULT_PRICING
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// usage: normalized { model, promptTokens, cachedTokens, completionTokens, reasoningTokens }
|
|
17
|
+
// (completionTokens is visible-output-only; reasoningTokens is additive — see route.js normalizers)
|
|
18
|
+
// timing: { ttftMs, genMs }
|
|
19
|
+
export function computeMetrics(usage, timing = {}) {
|
|
20
|
+
if (!usage) return null
|
|
21
|
+
|
|
22
|
+
const price = getPricing(usage.model)
|
|
23
|
+
const billedInputTokens = Math.max(0, usage.promptTokens - usage.cachedTokens)
|
|
24
|
+
const billedOutputTokens = usage.completionTokens + usage.reasoningTokens
|
|
25
|
+
const totalTokens = usage.promptTokens + usage.completionTokens + usage.reasoningTokens
|
|
26
|
+
|
|
27
|
+
const cost =
|
|
28
|
+
(billedInputTokens / 1e6) * price.input +
|
|
29
|
+
(usage.cachedTokens / 1e6) * price.cachedInput +
|
|
30
|
+
(billedOutputTokens / 1e6) * price.output
|
|
31
|
+
|
|
32
|
+
const cacheSavings = (usage.cachedTokens / 1e6) * (price.input - price.cachedInput)
|
|
33
|
+
|
|
34
|
+
const tps = billedOutputTokens > 0 && timing.genMs > 0
|
|
35
|
+
? parseFloat((billedOutputTokens / (timing.genMs / 1000)).toFixed(1))
|
|
36
|
+
: null
|
|
37
|
+
|
|
38
|
+
return {
|
|
39
|
+
provider: usage.provider ?? null,
|
|
40
|
+
model: usage.model ?? null,
|
|
41
|
+
ttftMs: timing.ttftMs ?? null,
|
|
42
|
+
genMs: timing.genMs ?? null,
|
|
43
|
+
tps,
|
|
44
|
+
promptTokens: usage.promptTokens,
|
|
45
|
+
cachedTokens: usage.cachedTokens,
|
|
46
|
+
completionTokens: usage.completionTokens,
|
|
47
|
+
reasoningTokens: usage.reasoningTokens,
|
|
48
|
+
totalTokens,
|
|
49
|
+
cost,
|
|
50
|
+
cacheSavings,
|
|
51
|
+
device: null,
|
|
52
|
+
dtype: null,
|
|
53
|
+
loadMs: null,
|
|
54
|
+
downloadBytes: null,
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// history: array of computeMetrics() results (or the token-count-only fallback shape)
|
|
59
|
+
// accumulated across a session. Returns null when history is empty.
|
|
60
|
+
export function aggregateSessionMetrics(history) {
|
|
61
|
+
if (!history || history.length === 0) return null
|
|
62
|
+
|
|
63
|
+
const generationCount = history.length
|
|
64
|
+
let totalCost = 0
|
|
65
|
+
let totalTokens = 0
|
|
66
|
+
let totalCachedTokens = 0
|
|
67
|
+
let totalCacheSavings = 0
|
|
68
|
+
let ttftSum = 0
|
|
69
|
+
let ttftCount = 0
|
|
70
|
+
let tpsSum = 0
|
|
71
|
+
let tpsCount = 0
|
|
72
|
+
let minTps = null
|
|
73
|
+
let maxTps = null
|
|
74
|
+
const byProvider = {}
|
|
75
|
+
|
|
76
|
+
for (const m of history) {
|
|
77
|
+
totalCost += m.cost ?? 0
|
|
78
|
+
totalTokens += m.totalTokens ?? 0
|
|
79
|
+
totalCachedTokens += m.cachedTokens ?? 0
|
|
80
|
+
totalCacheSavings += m.cacheSavings ?? 0
|
|
81
|
+
|
|
82
|
+
if (typeof m.ttftMs === "number") {
|
|
83
|
+
ttftSum += m.ttftMs
|
|
84
|
+
ttftCount++
|
|
85
|
+
}
|
|
86
|
+
if (typeof m.tps === "number") {
|
|
87
|
+
tpsSum += m.tps
|
|
88
|
+
tpsCount++
|
|
89
|
+
minTps = minTps === null ? m.tps : Math.min(minTps, m.tps)
|
|
90
|
+
maxTps = maxTps === null ? m.tps : Math.max(maxTps, m.tps)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const providerKey = m.provider ?? "unknown"
|
|
94
|
+
if (!byProvider[providerKey]) {
|
|
95
|
+
byProvider[providerKey] = { generationCount: 0, totalCost: 0, totalTokens: 0 }
|
|
96
|
+
}
|
|
97
|
+
byProvider[providerKey].generationCount++
|
|
98
|
+
byProvider[providerKey].totalCost += m.cost ?? 0
|
|
99
|
+
byProvider[providerKey].totalTokens += m.totalTokens ?? 0
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return {
|
|
103
|
+
generationCount,
|
|
104
|
+
totalCost,
|
|
105
|
+
totalTokens,
|
|
106
|
+
totalCachedTokens,
|
|
107
|
+
totalCacheSavings,
|
|
108
|
+
avgTtftMs: ttftCount > 0 ? Math.round(ttftSum / ttftCount) : null,
|
|
109
|
+
avgTps: tpsCount > 0 ? parseFloat((tpsSum / tpsCount).toFixed(1)) : null,
|
|
110
|
+
minTps,
|
|
111
|
+
maxTps,
|
|
112
|
+
avgCostPerGeneration: generationCount > 0 ? totalCost / generationCount : 0,
|
|
113
|
+
byProvider,
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// history: array of native per-generation metrics objects ({ device, ttftMs, tps, totalTokens, genMs })
|
|
118
|
+
// accumulated across a session. No cost/cachedTokens/cacheSavings/byProvider — native has no
|
|
119
|
+
// billing and only one local model, so those fields don't apply. Returns null when history is empty.
|
|
120
|
+
export function aggregateNativeSessionMetrics(history) {
|
|
121
|
+
if (!history || history.length === 0) return null
|
|
122
|
+
|
|
123
|
+
const generationCount = history.length
|
|
124
|
+
let totalTokens = 0
|
|
125
|
+
let totalGenMs = 0
|
|
126
|
+
let ttftSum = 0
|
|
127
|
+
let ttftCount = 0
|
|
128
|
+
let tpsSum = 0
|
|
129
|
+
let tpsCount = 0
|
|
130
|
+
let minTps = null
|
|
131
|
+
let maxTps = null
|
|
132
|
+
|
|
133
|
+
for (const m of history) {
|
|
134
|
+
totalTokens += m.totalTokens ?? 0
|
|
135
|
+
totalGenMs += m.genMs ?? 0
|
|
136
|
+
|
|
137
|
+
if (typeof m.ttftMs === "number") {
|
|
138
|
+
ttftSum += m.ttftMs
|
|
139
|
+
ttftCount++
|
|
140
|
+
}
|
|
141
|
+
if (typeof m.tps === "number") {
|
|
142
|
+
tpsSum += m.tps
|
|
143
|
+
tpsCount++
|
|
144
|
+
minTps = minTps === null ? m.tps : Math.min(minTps, m.tps)
|
|
145
|
+
maxTps = maxTps === null ? m.tps : Math.max(maxTps, m.tps)
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return {
|
|
150
|
+
generationCount,
|
|
151
|
+
totalTokens,
|
|
152
|
+
totalGenMs,
|
|
153
|
+
avgTtftMs: ttftCount > 0 ? Math.round(ttftSum / ttftCount) : null,
|
|
154
|
+
avgTps: tpsCount > 0 ? parseFloat((tpsSum / tpsCount).toFixed(1)) : null,
|
|
155
|
+
minTps,
|
|
156
|
+
maxTps,
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// EXPERIMENTAL (see useWebAI.js) — metrics reflect a provider whose generation quality/backend
|
|
161
|
+
// selection isn't yet reliable, so tps/ttft numbers here are informative, not a guarantee.
|
|
162
|
+
//
|
|
163
|
+
// history: array of web per-generation metrics objects ({ device, dtype, ttftMs, tps, totalTokens, genMs })
|
|
164
|
+
// accumulated across a browser session. No cost/cachedTokens/cacheSavings/byProvider — web has no
|
|
165
|
+
// billing and one pipeline per session. device/dtype are taken from the most recent entry rather
|
|
166
|
+
// than aggregated, since they describe the loaded model/backend, not a per-generation value.
|
|
167
|
+
// Returns null when history is empty.
|
|
168
|
+
export function aggregateWebSessionMetrics(history) {
|
|
169
|
+
if (!history || history.length === 0) return null
|
|
170
|
+
|
|
171
|
+
const generationCount = history.length
|
|
172
|
+
let totalTokens = 0
|
|
173
|
+
let totalGenMs = 0
|
|
174
|
+
let ttftSum = 0
|
|
175
|
+
let ttftCount = 0
|
|
176
|
+
let tpsSum = 0
|
|
177
|
+
let tpsCount = 0
|
|
178
|
+
let minTps = null
|
|
179
|
+
let maxTps = null
|
|
180
|
+
|
|
181
|
+
for (const m of history) {
|
|
182
|
+
totalTokens += m.totalTokens ?? 0
|
|
183
|
+
totalGenMs += m.genMs ?? 0
|
|
184
|
+
|
|
185
|
+
if (typeof m.ttftMs === "number") {
|
|
186
|
+
ttftSum += m.ttftMs
|
|
187
|
+
ttftCount++
|
|
188
|
+
}
|
|
189
|
+
if (typeof m.tps === "number") {
|
|
190
|
+
tpsSum += m.tps
|
|
191
|
+
tpsCount++
|
|
192
|
+
minTps = minTps === null ? m.tps : Math.min(minTps, m.tps)
|
|
193
|
+
maxTps = maxTps === null ? m.tps : Math.max(maxTps, m.tps)
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const last = history[history.length - 1]
|
|
198
|
+
|
|
199
|
+
return {
|
|
200
|
+
generationCount,
|
|
201
|
+
totalTokens,
|
|
202
|
+
totalGenMs,
|
|
203
|
+
avgTtftMs: ttftCount > 0 ? Math.round(ttftSum / ttftCount) : null,
|
|
204
|
+
avgTps: tpsCount > 0 ? parseFloat((tpsSum / tpsCount).toFixed(1)) : null,
|
|
205
|
+
minTps,
|
|
206
|
+
maxTps,
|
|
207
|
+
device: last?.device ?? null,
|
|
208
|
+
dtype: last?.dtype ?? null,
|
|
209
|
+
}
|
|
210
|
+
}
|
package/src/pricing.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Hardcoded USD price per 1M tokens. Update manually when providers change pricing.
|
|
2
|
+
const PRICING = {
|
|
3
|
+
"gpt-4o-mini": { input: 0.15, cachedInput: 0.075, output: 0.60 },
|
|
4
|
+
"gpt-5": { input: 1.25, cachedInput: 0.125, output: 10.00 },
|
|
5
|
+
"gemini-3.5-flash": { input: 1.50, cachedInput: 0.15, output: 9.00 },
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const DEFAULT_PRICING = { input: 0, cachedInput: 0, output: 0 }
|
|
9
|
+
|
|
10
|
+
function getPricing(model) {
|
|
11
|
+
return PRICING[model] ?? DEFAULT_PRICING
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
module.exports = { PRICING, getPricing }
|