@heyramzi/utils 1.12.0 → 1.13.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/dist/ai/image.js +220 -0
- package/dist/ai/models.js +261 -0
- package/dist/analytics/client.js +57 -0
- package/dist/analytics/server.js +54 -0
- package/dist/analytics/svelte.js +24 -0
- package/dist/analytics/types.js +0 -0
- package/dist/analytics/umami/protocol.js +35 -0
- package/dist/analytics/umami/reporting.js +189 -0
- package/dist/auth/config/otp.js +4 -0
- package/dist/auth/config/rate-limits.js +20 -0
- package/dist/auth/config/trusted-origins.js +15 -0
- package/dist/auth/email/index.js +3 -0
- package/dist/auth/email/layout.js +84 -0
- package/dist/auth/email/resend.js +30 -0
- package/dist/auth/email/template.js +57 -0
- package/dist/auth/email/types.js +0 -0
- package/dist/auth/index.js +3 -0
- package/dist/cache.js +4 -0
- package/dist/cn.js +5 -0
- package/dist/collection.js +8 -0
- package/dist/crypto.js +117 -0
- package/dist/error.js +15 -0
- package/dist/errors/sentry/index.js +15 -0
- package/dist/errors/sentry/protocol.js +130 -0
- package/dist/errors/sentry/sveltekit.js +145 -0
- package/dist/format.js +62 -0
- package/dist/grab.js +112 -0
- package/dist/index.js +10 -0
- package/dist/logger-types.js +0 -0
- package/dist/logger.js +22 -0
- package/dist/math.js +3 -0
- package/dist/og/fonts.js +46 -0
- package/dist/og/index.js +3 -0
- package/dist/og/markup.js +31 -0
- package/dist/og/renderer.js +29 -0
- package/dist/og/types.js +0 -0
- package/dist/result.js +26 -0
- package/dist/string.js +32 -0
- package/dist/testimonials/render-content.js +15 -0
- package/dist/testimonials/server.js +21 -0
- package/dist/testimonials/types.js +0 -0
- package/dist/timing.js +20 -0
- package/dist/vite.js +265 -0
- package/package.json +125 -27
- package/src/auth/email/template.ts +2 -2
- package/src/errors/sentry/sveltekit.test.ts +63 -2
- package/src/errors/sentry/sveltekit.ts +43 -5
package/dist/ai/image.js
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import { generateText } from "ai";
|
|
2
|
+
import { gatewayId, IMAGE_OUTPUT_TOKEN_PRICE_USD, resolveImageModelConfig } from "./models.js";
|
|
3
|
+
function providerOptions(config, aspectRatio) {
|
|
4
|
+
if (config.provider !== "google") {
|
|
5
|
+
throw new Error(`Provider "${config.provider}" has no image request shape in @heyramzi/utils.`);
|
|
6
|
+
}
|
|
7
|
+
return {
|
|
8
|
+
google: {
|
|
9
|
+
responseModalities: [
|
|
10
|
+
"IMAGE"
|
|
11
|
+
],
|
|
12
|
+
...aspectRatio ? {
|
|
13
|
+
imageConfig: {
|
|
14
|
+
aspectRatio
|
|
15
|
+
}
|
|
16
|
+
} : {}
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
function decodeBase64(value) {
|
|
21
|
+
const binary = atob(value);
|
|
22
|
+
const bytes = new Uint8Array(binary.length);
|
|
23
|
+
for(let index = 0; index < binary.length; index++)bytes[index] = binary.charCodeAt(index);
|
|
24
|
+
return bytes;
|
|
25
|
+
}
|
|
26
|
+
const OPENAI_QUALITY = {
|
|
27
|
+
small: "low",
|
|
28
|
+
medium: "medium",
|
|
29
|
+
large: "high"
|
|
30
|
+
};
|
|
31
|
+
const OPENAI_SIZE_BY_RATIO = {
|
|
32
|
+
"16:9": "1536x1024",
|
|
33
|
+
"3:2": "1536x1024",
|
|
34
|
+
"1:1": "1024x1024",
|
|
35
|
+
"2:3": "1024x1536",
|
|
36
|
+
"9:16": "1024x1536"
|
|
37
|
+
};
|
|
38
|
+
function openAiCost(config, outputTokens) {
|
|
39
|
+
const perToken = IMAGE_OUTPUT_TOKEN_PRICE_USD[config.modelId];
|
|
40
|
+
if (!perToken || !outputTokens) return config.pricePerImage;
|
|
41
|
+
return outputTokens * perToken;
|
|
42
|
+
}
|
|
43
|
+
function openAiKey(explicit) {
|
|
44
|
+
const env = globalThis.process?.env;
|
|
45
|
+
const apiKey = explicit ?? env?.OPENAI_API_KEY;
|
|
46
|
+
if (!apiKey) throw new Error("OPENAI_API_KEY is not set, so no OpenAI image can be drawn.");
|
|
47
|
+
return apiKey;
|
|
48
|
+
}
|
|
49
|
+
async function postOpenAi(path, body, headers, apiKey) {
|
|
50
|
+
const response = await fetch(`https://api.openai.com/v1/${path}`, {
|
|
51
|
+
method: "POST",
|
|
52
|
+
headers: {
|
|
53
|
+
Authorization: `Bearer ${apiKey}`,
|
|
54
|
+
...headers
|
|
55
|
+
},
|
|
56
|
+
body
|
|
57
|
+
});
|
|
58
|
+
const json = await response.json().catch(()=>({}));
|
|
59
|
+
return {
|
|
60
|
+
ok: response.ok,
|
|
61
|
+
status: response.status,
|
|
62
|
+
json
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
async function generateOpenAiImage(config, options, tier) {
|
|
66
|
+
const size = options.size ?? OPENAI_SIZE_BY_RATIO[options.aspectRatio ?? ""] ?? "auto";
|
|
67
|
+
const quality = OPENAI_QUALITY[tier];
|
|
68
|
+
const references = options.references ?? [];
|
|
69
|
+
const apiKey = openAiKey(options.apiKey);
|
|
70
|
+
async function call(withFidelity) {
|
|
71
|
+
if (references.length === 0) {
|
|
72
|
+
return postOpenAi("images/generations", JSON.stringify({
|
|
73
|
+
model: config.sdkId,
|
|
74
|
+
prompt: options.prompt,
|
|
75
|
+
size,
|
|
76
|
+
quality,
|
|
77
|
+
n: 1
|
|
78
|
+
}), {
|
|
79
|
+
"Content-Type": "application/json"
|
|
80
|
+
}, apiKey);
|
|
81
|
+
}
|
|
82
|
+
const form = new FormData();
|
|
83
|
+
form.set("model", config.sdkId);
|
|
84
|
+
form.set("prompt", options.prompt);
|
|
85
|
+
form.set("size", size);
|
|
86
|
+
form.set("quality", quality);
|
|
87
|
+
form.set("n", "1");
|
|
88
|
+
if (withFidelity) form.set("input_fidelity", "high");
|
|
89
|
+
for (const [index, reference] of references.entries()){
|
|
90
|
+
const extension = reference.mediaType.split("/")[1] ?? "png";
|
|
91
|
+
form.append("image[]", new Blob([
|
|
92
|
+
reference.bytes
|
|
93
|
+
], {
|
|
94
|
+
type: reference.mediaType
|
|
95
|
+
}), `reference-${index + 1}.${extension}`);
|
|
96
|
+
}
|
|
97
|
+
return postOpenAi("images/edits", form, {}, apiKey);
|
|
98
|
+
}
|
|
99
|
+
let result = await call(references.length > 0);
|
|
100
|
+
const unknownParam = !result.ok && (result.json.error?.message ?? "").includes("input_fidelity");
|
|
101
|
+
if (unknownParam) result = await call(false);
|
|
102
|
+
if (!result.ok) {
|
|
103
|
+
throw new Error(`${config.modelId} refused the request (${result.status}): ${result.json.error?.message ?? "no message"}`);
|
|
104
|
+
}
|
|
105
|
+
const base64 = result.json.data?.[0]?.b64_json;
|
|
106
|
+
if (!base64) throw new Error(`${config.modelId} returned no image data.`);
|
|
107
|
+
return {
|
|
108
|
+
bytes: decodeBase64(base64),
|
|
109
|
+
mediaType: "image/png",
|
|
110
|
+
config,
|
|
111
|
+
priceUsd: openAiCost(config, result.json.usage?.output_tokens),
|
|
112
|
+
attempts: 1
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
const AGNES_SIZE = {
|
|
116
|
+
small: "1K",
|
|
117
|
+
medium: "2K",
|
|
118
|
+
large: "4K"
|
|
119
|
+
};
|
|
120
|
+
const AGNES_BASE_URL = "https://apihub.agnes-ai.com/v1";
|
|
121
|
+
function agnesKey(explicit) {
|
|
122
|
+
const env = globalThis.process?.env;
|
|
123
|
+
const key = explicit ?? env?.AGNES_API_KEY;
|
|
124
|
+
if (!key) {
|
|
125
|
+
throw new Error("The agnes image lane needs an API key: pass `apiKey`, or set AGNES_API_KEY.");
|
|
126
|
+
}
|
|
127
|
+
return key;
|
|
128
|
+
}
|
|
129
|
+
async function generateAgnesImage(config, options, tier) {
|
|
130
|
+
const references = options.references ?? [];
|
|
131
|
+
if (references.length > 0) {
|
|
132
|
+
throw new Error(`${config.modelId} draws from a prompt only: it refuses a reference image with a 403 that blames the model. Draw through a gateway for anything conditioned on a frame.`);
|
|
133
|
+
}
|
|
134
|
+
const body = {
|
|
135
|
+
model: config.sdkId,
|
|
136
|
+
prompt: options.prompt,
|
|
137
|
+
size: options.size ?? AGNES_SIZE[tier],
|
|
138
|
+
ratio: options.aspectRatio ?? "1:1"
|
|
139
|
+
};
|
|
140
|
+
const response = await fetch(`${AGNES_BASE_URL}/images/generations`, {
|
|
141
|
+
method: "POST",
|
|
142
|
+
headers: {
|
|
143
|
+
Authorization: `Bearer ${agnesKey(options.apiKey)}`,
|
|
144
|
+
"Content-Type": "application/json"
|
|
145
|
+
},
|
|
146
|
+
body: JSON.stringify(body)
|
|
147
|
+
});
|
|
148
|
+
const json = await response.json().catch(()=>({}));
|
|
149
|
+
if (!response.ok) {
|
|
150
|
+
throw new Error(`${config.modelId} refused the request (${response.status}): ${json.error?.message ?? json.message ?? "no message"}`);
|
|
151
|
+
}
|
|
152
|
+
const first = json.data?.[0];
|
|
153
|
+
if (first?.b64_json) {
|
|
154
|
+
return {
|
|
155
|
+
bytes: decodeBase64(first.b64_json),
|
|
156
|
+
mediaType: "image/png",
|
|
157
|
+
config,
|
|
158
|
+
priceUsd: config.pricePerImage,
|
|
159
|
+
attempts: 1
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
if (first?.url) {
|
|
163
|
+
const image = await fetch(first.url);
|
|
164
|
+
if (!image.ok) {
|
|
165
|
+
throw new Error(`${config.modelId} returned a url that did not fetch (${image.status}).`);
|
|
166
|
+
}
|
|
167
|
+
return {
|
|
168
|
+
bytes: new Uint8Array(await image.arrayBuffer()),
|
|
169
|
+
mediaType: image.headers.get("content-type") ?? "image/png",
|
|
170
|
+
config,
|
|
171
|
+
priceUsd: config.pricePerImage,
|
|
172
|
+
attempts: 1
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
throw new Error(`${config.modelId} returned no image data.`);
|
|
176
|
+
}
|
|
177
|
+
export async function generateImage(options) {
|
|
178
|
+
const tier = options.tier ?? "medium";
|
|
179
|
+
const config = resolveImageModelConfig(tier, options.provider);
|
|
180
|
+
if (config.provider === "openai") return generateOpenAiImage(config, options, tier);
|
|
181
|
+
if (config.provider === "agnes") return generateAgnesImage(config, options, tier);
|
|
182
|
+
const id = gatewayId(config);
|
|
183
|
+
const model = options.gateway ? options.gateway(id) : id;
|
|
184
|
+
const attempts = options.attempts ?? 3;
|
|
185
|
+
let lastText = "";
|
|
186
|
+
for(let attempt = 1; attempt <= attempts; attempt++){
|
|
187
|
+
const result = await generateText({
|
|
188
|
+
model,
|
|
189
|
+
messages: [
|
|
190
|
+
{
|
|
191
|
+
role: "user",
|
|
192
|
+
content: [
|
|
193
|
+
{
|
|
194
|
+
type: "text",
|
|
195
|
+
text: options.prompt
|
|
196
|
+
},
|
|
197
|
+
...(options.references ?? []).map((reference)=>({
|
|
198
|
+
type: "file",
|
|
199
|
+
data: reference.bytes,
|
|
200
|
+
mediaType: reference.mediaType
|
|
201
|
+
}))
|
|
202
|
+
]
|
|
203
|
+
}
|
|
204
|
+
],
|
|
205
|
+
providerOptions: providerOptions(config, options.aspectRatio)
|
|
206
|
+
});
|
|
207
|
+
const file = result.files.find((entry)=>entry.mediaType.startsWith("image/"));
|
|
208
|
+
if (file) {
|
|
209
|
+
return {
|
|
210
|
+
bytes: file.uint8Array,
|
|
211
|
+
mediaType: file.mediaType,
|
|
212
|
+
config,
|
|
213
|
+
priceUsd: config.pricePerImage,
|
|
214
|
+
attempts: attempt
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
lastText = (result.text ?? "").slice(0, 160);
|
|
218
|
+
}
|
|
219
|
+
throw new Error(`${config.modelId} returned no image after ${attempts} attempts${lastText ? `: ${lastText}` : ""}`);
|
|
220
|
+
}
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
export const MODEL_IDS = {
|
|
2
|
+
xai: {
|
|
3
|
+
large: "grok-4.7",
|
|
4
|
+
medium: "grok-4.7",
|
|
5
|
+
small: "grok-4.7"
|
|
6
|
+
},
|
|
7
|
+
google: {
|
|
8
|
+
large: "gemini-3.8-flash",
|
|
9
|
+
medium: "gemini-3.8-flash",
|
|
10
|
+
small: "gemini-3.5-flash-lite"
|
|
11
|
+
},
|
|
12
|
+
anthropic: {
|
|
13
|
+
large: "claude-opus-5-5",
|
|
14
|
+
medium: "claude-sonnet-5-5",
|
|
15
|
+
small: "claude-haiku-5-5"
|
|
16
|
+
},
|
|
17
|
+
groq: {
|
|
18
|
+
large: "openai/gpt-oss-120b",
|
|
19
|
+
medium: "openai/gpt-oss-120b",
|
|
20
|
+
small: "openai/gpt-oss-20b"
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
export const MODEL_CATALOG = {
|
|
24
|
+
"grok-4.7": {
|
|
25
|
+
modelId: "grok-4.7",
|
|
26
|
+
sdkId: "grok-4.7",
|
|
27
|
+
provider: "xai",
|
|
28
|
+
tier: "large",
|
|
29
|
+
pricing: {
|
|
30
|
+
input: 2.0,
|
|
31
|
+
output: 6.0
|
|
32
|
+
},
|
|
33
|
+
gatewayId: "spacexai/grok-4.7"
|
|
34
|
+
},
|
|
35
|
+
"gemini-3.8-flash": {
|
|
36
|
+
modelId: "gemini-3.8-flash",
|
|
37
|
+
sdkId: "gemini-3.8-flash",
|
|
38
|
+
provider: "google",
|
|
39
|
+
tier: "medium",
|
|
40
|
+
pricing: {
|
|
41
|
+
input: 1.5,
|
|
42
|
+
output: 7.5
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
"gemini-3.5-flash-lite": {
|
|
46
|
+
modelId: "gemini-3.5-flash-lite",
|
|
47
|
+
sdkId: "gemini-3.5-flash-lite",
|
|
48
|
+
provider: "google",
|
|
49
|
+
tier: "small",
|
|
50
|
+
pricing: {
|
|
51
|
+
input: 0.3,
|
|
52
|
+
output: 2.5
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
"claude-opus-5-5": {
|
|
56
|
+
modelId: "claude-opus-5-5",
|
|
57
|
+
sdkId: "claude-opus-5-5",
|
|
58
|
+
gatewayId: "anthropic/claude-opus-5.5",
|
|
59
|
+
provider: "anthropic",
|
|
60
|
+
tier: "large",
|
|
61
|
+
pricing: {
|
|
62
|
+
input: 4.0,
|
|
63
|
+
output: 20.0
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
"claude-sonnet-5-5": {
|
|
67
|
+
modelId: "claude-sonnet-5-5",
|
|
68
|
+
sdkId: "claude-sonnet-5-5",
|
|
69
|
+
gatewayId: "anthropic/claude-sonnet-5.5",
|
|
70
|
+
provider: "anthropic",
|
|
71
|
+
tier: "medium",
|
|
72
|
+
pricing: {
|
|
73
|
+
input: 2.0,
|
|
74
|
+
output: 10.0
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
"claude-haiku-5-5": {
|
|
78
|
+
modelId: "claude-haiku-5-5",
|
|
79
|
+
sdkId: "claude-haiku-5-5",
|
|
80
|
+
gatewayId: "anthropic/claude-haiku-5.5",
|
|
81
|
+
provider: "anthropic",
|
|
82
|
+
tier: "small",
|
|
83
|
+
pricing: {
|
|
84
|
+
input: 0.1,
|
|
85
|
+
output: 0.5
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
"claude-fable-5-1": {
|
|
89
|
+
modelId: "claude-fable-5-1",
|
|
90
|
+
sdkId: "claude-fable-5-1",
|
|
91
|
+
gatewayId: "anthropic/claude-fable-5.1",
|
|
92
|
+
provider: "anthropic",
|
|
93
|
+
tier: "large",
|
|
94
|
+
pricing: {
|
|
95
|
+
input: 10.0,
|
|
96
|
+
output: 50.0
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
"gemini-3.6-flash": {
|
|
100
|
+
modelId: "gemini-3.6-flash",
|
|
101
|
+
sdkId: "gemini-3.6-flash",
|
|
102
|
+
provider: "google",
|
|
103
|
+
tier: "medium",
|
|
104
|
+
pricing: {
|
|
105
|
+
input: 1.5,
|
|
106
|
+
output: 7.5
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
"gpt-5.6-luna": {
|
|
110
|
+
modelId: "gpt-5.6-luna",
|
|
111
|
+
sdkId: "gpt-5.6-luna",
|
|
112
|
+
provider: "openai",
|
|
113
|
+
tier: "small",
|
|
114
|
+
pricing: {
|
|
115
|
+
input: 0.2,
|
|
116
|
+
output: 1.2
|
|
117
|
+
}
|
|
118
|
+
},
|
|
119
|
+
"gpt-6-luna": {
|
|
120
|
+
modelId: "gpt-6-luna",
|
|
121
|
+
sdkId: "gpt-6-luna",
|
|
122
|
+
provider: "openai",
|
|
123
|
+
tier: "small",
|
|
124
|
+
pricing: {
|
|
125
|
+
input: 0.1,
|
|
126
|
+
output: 0.5
|
|
127
|
+
}
|
|
128
|
+
},
|
|
129
|
+
"gpt-6.1-sol": {
|
|
130
|
+
modelId: "gpt-6.1-sol",
|
|
131
|
+
sdkId: "gpt-6.1-sol",
|
|
132
|
+
provider: "openai",
|
|
133
|
+
tier: "large",
|
|
134
|
+
pricing: {
|
|
135
|
+
input: 2.0,
|
|
136
|
+
output: 10.0
|
|
137
|
+
}
|
|
138
|
+
},
|
|
139
|
+
"openai/gpt-oss-120b": {
|
|
140
|
+
modelId: "openai/gpt-oss-120b",
|
|
141
|
+
sdkId: "openai/gpt-oss-120b",
|
|
142
|
+
gatewayId: "openai/gpt-oss-120b",
|
|
143
|
+
provider: "groq",
|
|
144
|
+
tier: "large",
|
|
145
|
+
pricing: {
|
|
146
|
+
input: 0.15,
|
|
147
|
+
output: 0.6
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
"openai/gpt-oss-20b": {
|
|
151
|
+
modelId: "openai/gpt-oss-20b",
|
|
152
|
+
sdkId: "openai/gpt-oss-20b",
|
|
153
|
+
gatewayId: "openai/gpt-oss-20b",
|
|
154
|
+
provider: "groq",
|
|
155
|
+
tier: "small",
|
|
156
|
+
pricing: {
|
|
157
|
+
input: 0.075,
|
|
158
|
+
output: 0.3
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
export const DEFAULT_PROVIDER = "xai";
|
|
163
|
+
export const EMBEDDING_MODEL = {
|
|
164
|
+
modelId: "gemini-embedding-2-preview",
|
|
165
|
+
gatewayId: "google/gemini-embedding-2",
|
|
166
|
+
provider: "google",
|
|
167
|
+
dimensions: 768
|
|
168
|
+
};
|
|
169
|
+
export const EMBEDDING_DIMENSIONS = EMBEDDING_MODEL.dimensions;
|
|
170
|
+
export function gatewayId(config) {
|
|
171
|
+
return config.gatewayId ?? `${config.provider}/${config.sdkId}`;
|
|
172
|
+
}
|
|
173
|
+
export function getModelConfig(id) {
|
|
174
|
+
const config = MODEL_CATALOG[id];
|
|
175
|
+
if (!config) {
|
|
176
|
+
throw new Error(`Missing model config for model "${id}".`);
|
|
177
|
+
}
|
|
178
|
+
return config;
|
|
179
|
+
}
|
|
180
|
+
export function resolveModelConfig(tier, provider = DEFAULT_PROVIDER) {
|
|
181
|
+
const ids = MODEL_IDS;
|
|
182
|
+
const modelId = ids[provider]?.[tier];
|
|
183
|
+
if (!modelId) {
|
|
184
|
+
throw new Error(`Missing model ID for provider "${provider}" and tier "${tier}".`);
|
|
185
|
+
}
|
|
186
|
+
return getModelConfig(modelId);
|
|
187
|
+
}
|
|
188
|
+
export function model(tier, provider = DEFAULT_PROVIDER) {
|
|
189
|
+
return gatewayId(resolveModelConfig(tier, provider));
|
|
190
|
+
}
|
|
191
|
+
export const IMAGE_MODEL_IDS = {
|
|
192
|
+
google: {
|
|
193
|
+
small: "gemini-3.1-flash-lite-image",
|
|
194
|
+
medium: "gemini-3.1-flash-lite-image",
|
|
195
|
+
large: "gemini-nano-banana-2.1"
|
|
196
|
+
},
|
|
197
|
+
openai: {
|
|
198
|
+
small: "gpt-image-2",
|
|
199
|
+
medium: "gpt-image-2",
|
|
200
|
+
large: "gpt-image-2"
|
|
201
|
+
},
|
|
202
|
+
agnes: {
|
|
203
|
+
small: "agnes-image-2.5-flash",
|
|
204
|
+
medium: "agnes-image-2.5-flash",
|
|
205
|
+
large: "agnes-image-2.5-flash"
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
export const IMAGE_MODEL_CATALOG = {
|
|
209
|
+
"gemini-3.1-flash-lite-image": {
|
|
210
|
+
modelId: "gemini-3.1-flash-lite-image",
|
|
211
|
+
sdkId: "gemini-3.1-flash-lite-image",
|
|
212
|
+
provider: "google",
|
|
213
|
+
tier: "small",
|
|
214
|
+
pricePerImage: 0.034
|
|
215
|
+
},
|
|
216
|
+
"gemini-nano-banana-2.1": {
|
|
217
|
+
modelId: "gemini-nano-banana-2.1",
|
|
218
|
+
sdkId: "gemini-nano-banana-2.1",
|
|
219
|
+
provider: "google",
|
|
220
|
+
tier: "large",
|
|
221
|
+
pricePerImage: 0.067
|
|
222
|
+
},
|
|
223
|
+
"gemini-3-pro-image": {
|
|
224
|
+
modelId: "gemini-3-pro-image",
|
|
225
|
+
sdkId: "gemini-3-pro-image",
|
|
226
|
+
provider: "google",
|
|
227
|
+
tier: "large",
|
|
228
|
+
pricePerImage: 0.134
|
|
229
|
+
},
|
|
230
|
+
"gpt-image-2": {
|
|
231
|
+
modelId: "gpt-image-2",
|
|
232
|
+
sdkId: "gpt-image-2",
|
|
233
|
+
provider: "openai",
|
|
234
|
+
tier: "medium",
|
|
235
|
+
pricePerImage: 0.032
|
|
236
|
+
},
|
|
237
|
+
"agnes-image-2.5-flash": {
|
|
238
|
+
modelId: "agnes-image-2.5-flash",
|
|
239
|
+
sdkId: "agnes-image-2.5-flash",
|
|
240
|
+
provider: "agnes",
|
|
241
|
+
tier: "medium",
|
|
242
|
+
pricePerImage: 0
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
export const IMAGE_OUTPUT_TOKEN_PRICE_USD = {
|
|
246
|
+
"gpt-image-2": 30 / 1_000_000
|
|
247
|
+
};
|
|
248
|
+
export const DEFAULT_IMAGE_PROVIDER = "google";
|
|
249
|
+
export function resolveImageModelConfig(tier, provider = DEFAULT_IMAGE_PROVIDER) {
|
|
250
|
+
const modelId = IMAGE_MODEL_IDS[provider]?.[tier];
|
|
251
|
+
const config = modelId ? IMAGE_MODEL_CATALOG[modelId] : undefined;
|
|
252
|
+
if (!config) {
|
|
253
|
+
throw new Error(`Missing image model for provider "${provider}" and tier "${tier}".`);
|
|
254
|
+
}
|
|
255
|
+
return config;
|
|
256
|
+
}
|
|
257
|
+
export function imagePriceUsd(modelId) {
|
|
258
|
+
const direct = IMAGE_MODEL_CATALOG[modelId];
|
|
259
|
+
if (direct) return direct.pricePerImage;
|
|
260
|
+
return Object.values(IMAGE_MODEL_CATALOG).find((config)=>config.sdkId === modelId)?.pricePerImage ?? null;
|
|
261
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import "./types.js";
|
|
2
|
+
let active = false;
|
|
3
|
+
export function initAnalytics(config) {
|
|
4
|
+
stop();
|
|
5
|
+
if (config.disabled || typeof window === "undefined") return stop;
|
|
6
|
+
active = true;
|
|
7
|
+
injectScript(config);
|
|
8
|
+
return stop;
|
|
9
|
+
}
|
|
10
|
+
function stop() {
|
|
11
|
+
active = false;
|
|
12
|
+
}
|
|
13
|
+
export function track(event, meta) {
|
|
14
|
+
if (!active || typeof window === "undefined" || !window.umami) return;
|
|
15
|
+
try {
|
|
16
|
+
if (meta) {
|
|
17
|
+
window.umami.track(event, meta);
|
|
18
|
+
} else {
|
|
19
|
+
window.umami.track(event);
|
|
20
|
+
}
|
|
21
|
+
} catch {}
|
|
22
|
+
}
|
|
23
|
+
export function trackRevenue(args) {
|
|
24
|
+
const meta = {
|
|
25
|
+
revenue: args.value,
|
|
26
|
+
currency: args.currency
|
|
27
|
+
};
|
|
28
|
+
if (args.plan) meta.plan = args.plan;
|
|
29
|
+
if (args.transactionId) meta.transaction_id = args.transactionId;
|
|
30
|
+
track(args.event, meta);
|
|
31
|
+
}
|
|
32
|
+
export function setProfileId(id) {
|
|
33
|
+
if (!active || typeof window === "undefined" || !window.umami?.identify) return;
|
|
34
|
+
try {
|
|
35
|
+
window.umami.identify(id ? {
|
|
36
|
+
profileId: id
|
|
37
|
+
} : {});
|
|
38
|
+
} catch {}
|
|
39
|
+
}
|
|
40
|
+
function injectScript(umami) {
|
|
41
|
+
if (typeof document === "undefined") return;
|
|
42
|
+
if (!(umami.autoInjectScript ?? true)) return;
|
|
43
|
+
if (document.querySelector(`script[data-website-id="${umami.websiteId}"]`)) return;
|
|
44
|
+
const script = document.createElement("script");
|
|
45
|
+
script.defer = true;
|
|
46
|
+
script.src = `${umami.host.replace(/\/+$/, "")}/script.js`;
|
|
47
|
+
script.dataset.websiteId = umami.websiteId;
|
|
48
|
+
if (umami.performance) script.dataset.performance = "true";
|
|
49
|
+
if (umami.tag) script.dataset.tag = umami.tag;
|
|
50
|
+
if (umami.beforeSend) script.dataset.beforeSend = umami.beforeSend;
|
|
51
|
+
if (umami.domains?.length) script.dataset.domains = umami.domains.join(",");
|
|
52
|
+
if (umami.doNotTrack) script.dataset.doNotTrack = "true";
|
|
53
|
+
if (umami.autoTrack === false) script.dataset.autoTrack = "false";
|
|
54
|
+
if (umami.excludeSearch) script.dataset.excludeSearch = "true";
|
|
55
|
+
if (umami.excludeHash) script.dataset.excludeHash = "true";
|
|
56
|
+
document.head.appendChild(script);
|
|
57
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { buildSendBody, UMAMI_ENDPOINTS } from "./umami/protocol.js";
|
|
2
|
+
const DEFAULT_USER_AGENT = "Mozilla/5.0 Marketing/1.0";
|
|
3
|
+
const TIMEOUT_MS = 3000;
|
|
4
|
+
let _config = null;
|
|
5
|
+
export function initServer(config) {
|
|
6
|
+
_config = config;
|
|
7
|
+
}
|
|
8
|
+
export async function trackServer(name, data, options) {
|
|
9
|
+
const { url, id } = typeof options === "string" ? {
|
|
10
|
+
url: options,
|
|
11
|
+
id: undefined
|
|
12
|
+
} : options ?? {};
|
|
13
|
+
try {
|
|
14
|
+
if (!_config) throw new Error("Call initServer() before tracking events");
|
|
15
|
+
const body = buildSendBody({
|
|
16
|
+
websiteId: _config.websiteId,
|
|
17
|
+
hostname: _config.hostname,
|
|
18
|
+
name,
|
|
19
|
+
url,
|
|
20
|
+
data
|
|
21
|
+
});
|
|
22
|
+
const payload = id ? {
|
|
23
|
+
...body.payload,
|
|
24
|
+
id
|
|
25
|
+
} : body.payload;
|
|
26
|
+
const response = await fetch(`${_config.hostUrl.replace(/\/$/, "")}${UMAMI_ENDPOINTS.send}`, {
|
|
27
|
+
method: "POST",
|
|
28
|
+
headers: {
|
|
29
|
+
"Content-Type": "application/json",
|
|
30
|
+
"User-Agent": _config.userAgent ?? DEFAULT_USER_AGENT
|
|
31
|
+
},
|
|
32
|
+
body: JSON.stringify({
|
|
33
|
+
...body,
|
|
34
|
+
payload
|
|
35
|
+
}),
|
|
36
|
+
signal: AbortSignal.timeout(TIMEOUT_MS)
|
|
37
|
+
});
|
|
38
|
+
const text = await response.text();
|
|
39
|
+
if (!response.ok || text.includes("beep")) {
|
|
40
|
+
throw new Error(`HTTP ${response.status}: ${text.slice(0, 120)}`);
|
|
41
|
+
}
|
|
42
|
+
return true;
|
|
43
|
+
} catch (err) {
|
|
44
|
+
console.error(`[analytics] Server track failed for ${name}:`, err);
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
export function trackServerRevenue(name, revenue, currency, data, options) {
|
|
49
|
+
return trackServer(name, {
|
|
50
|
+
...data,
|
|
51
|
+
revenue,
|
|
52
|
+
currency
|
|
53
|
+
}, options);
|
|
54
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const DEFAULT_HOST = "https://your-umami-instance.com";
|
|
2
|
+
export function umamiScriptAttrs(websiteId, options) {
|
|
3
|
+
const host = options?.hostUrl ?? DEFAULT_HOST;
|
|
4
|
+
const attrs = {
|
|
5
|
+
src: `${host}/script.js`,
|
|
6
|
+
"data-website-id": websiteId
|
|
7
|
+
};
|
|
8
|
+
if (options?.domains?.length) {
|
|
9
|
+
attrs["data-domains"] = options.domains.join(",");
|
|
10
|
+
}
|
|
11
|
+
if (options?.beforeSend) {
|
|
12
|
+
attrs["data-before-send"] = options.beforeSend;
|
|
13
|
+
}
|
|
14
|
+
if (options?.doNotTrack) {
|
|
15
|
+
attrs["data-do-not-track"] = "true";
|
|
16
|
+
}
|
|
17
|
+
if (options?.performance) {
|
|
18
|
+
attrs["data-performance"] = "true";
|
|
19
|
+
}
|
|
20
|
+
if (options?.tag) {
|
|
21
|
+
attrs["data-tag"] = options.tag;
|
|
22
|
+
}
|
|
23
|
+
return attrs;
|
|
24
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export const UMAMI_ENDPOINTS = {
|
|
2
|
+
send: "/api/send"
|
|
3
|
+
};
|
|
4
|
+
export function coerceData(data) {
|
|
5
|
+
if (!data) return undefined;
|
|
6
|
+
const out = {};
|
|
7
|
+
for (const [key, value] of Object.entries(data)){
|
|
8
|
+
if (value === undefined || value === null) continue;
|
|
9
|
+
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
|
10
|
+
out[key] = value;
|
|
11
|
+
} else {
|
|
12
|
+
out[key] = String(value);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
return Object.keys(out).length > 0 ? out : undefined;
|
|
16
|
+
}
|
|
17
|
+
export function buildSendBody(input) {
|
|
18
|
+
const data = coerceData(input.data);
|
|
19
|
+
const payload = {
|
|
20
|
+
website: input.websiteId,
|
|
21
|
+
url: input.url ?? "/"
|
|
22
|
+
};
|
|
23
|
+
if (input.name) payload.name = input.name;
|
|
24
|
+
if (input.hostname) payload.hostname = input.hostname;
|
|
25
|
+
if (input.title) payload.title = input.title;
|
|
26
|
+
if (input.referrer) payload.referrer = input.referrer;
|
|
27
|
+
if (input.language) payload.language = input.language;
|
|
28
|
+
if (input.screen) payload.screen = input.screen;
|
|
29
|
+
if (data) payload.data = data;
|
|
30
|
+
if (input.userAgent) payload.userAgent = input.userAgent;
|
|
31
|
+
return {
|
|
32
|
+
type: "event",
|
|
33
|
+
payload
|
|
34
|
+
};
|
|
35
|
+
}
|