@heyramzi/utils 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +112 -2
- 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 +201 -4
- package/src/ai/image-agnes.test.ts +130 -0
- package/src/ai/image.ts +435 -0
- package/src/ai/models.test.ts +98 -0
- package/src/ai/models.ts +450 -0
- package/src/analytics/client.test.ts +149 -0
- package/src/analytics/client.ts +129 -0
- package/src/analytics/server.test.ts +165 -0
- package/src/analytics/server.ts +88 -0
- package/src/analytics/svelte.test.ts +105 -0
- package/src/analytics/svelte.ts +58 -0
- package/src/analytics/types.ts +26 -0
- package/src/analytics/umami/protocol.test.ts +18 -0
- package/src/analytics/umami/protocol.ts +104 -0
- package/src/analytics/umami/reporting.test.ts +391 -0
- package/src/analytics/umami/reporting.ts +627 -0
- package/src/auth/config/otp.ts +8 -0
- package/src/auth/config/rate-limits.ts +11 -0
- package/src/auth/config/trusted-origins.ts +32 -0
- package/src/auth/email/index.ts +7 -0
- package/src/auth/email/layout.test.ts +73 -0
- package/src/auth/email/layout.ts +122 -0
- package/src/auth/email/resend.test.ts +35 -0
- package/src/auth/email/resend.ts +26 -0
- package/src/auth/email/template.ts +77 -0
- package/src/auth/email/types.ts +74 -0
- package/src/auth/index.ts +5 -0
- package/src/cache.test.ts +67 -0
- package/src/cache.ts +5 -0
- package/src/cn.test.ts +52 -0
- package/src/cn.ts +16 -0
- package/src/collection.test.ts +66 -0
- package/src/collection.ts +8 -0
- package/src/crypto.test.ts +86 -0
- package/src/crypto.ts +157 -0
- package/src/error.test.ts +79 -0
- package/src/error.ts +16 -0
- package/src/errors/sentry/index.ts +20 -0
- package/src/errors/sentry/protocol.test.ts +85 -0
- package/src/errors/sentry/protocol.ts +144 -0
- package/src/errors/sentry/sveltekit.test.ts +149 -0
- package/src/errors/sentry/sveltekit.ts +226 -0
- package/src/format.test.ts +225 -0
- package/src/format.ts +103 -0
- package/src/grab.d.ts +2 -0
- package/src/grab.js +112 -0
- package/src/index.ts +38 -0
- package/src/logger-types.ts +14 -0
- package/src/logger.ts +24 -0
- package/src/math.test.ts +41 -0
- package/src/math.ts +3 -0
- package/src/og/fonts.ts +69 -0
- package/src/og/index.ts +5 -0
- package/src/og/markup.test.ts +73 -0
- package/src/og/markup.ts +90 -0
- package/src/og/renderer.ts +42 -0
- package/src/og/types.ts +26 -0
- package/src/og/wawoff2.d.ts +6 -0
- package/src/package-imports.test.mjs +22 -0
- package/src/result.test.ts +149 -0
- package/src/result.ts +29 -0
- package/src/string.test.ts +205 -0
- package/src/string.ts +71 -0
- package/src/testimonials/render-content.ts +21 -0
- package/src/testimonials/server.ts +47 -0
- package/src/testimonials/types.ts +35 -0
- package/src/timing.test.ts +166 -0
- package/src/timing.ts +28 -0
- package/src/vite.d.ts +12 -0
- package/src/vite.js +265 -0
- package/src/vite.test.js +235 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ramzi
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,113 @@
|
|
|
1
|
-
|
|
1
|
+
> [!IMPORTANT]
|
|
2
|
+
> This submodule is originally created and maintained in `Studio/vibe-kit/modules`. Changes should be made and pushed from there. We are the creators and maintainers of this submodule.
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
# @heyramzi/utils
|
|
5
|
+
|
|
6
|
+
Shared utility functions used across all workspace projects.
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
pnpm add @heyramzi/utils
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
In the workspace, use `"@heyramzi/utils": "workspace:*"` in your `package.json`.
|
|
15
|
+
|
|
16
|
+
## Exports
|
|
17
|
+
|
|
18
|
+
### Main entry (`.`)
|
|
19
|
+
|
|
20
|
+
All utilities re-exported from a single barrel:
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import { cn, formatDate, debounce, groupBy } from "@heyramzi/utils";
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
### Subpath exports
|
|
27
|
+
|
|
28
|
+
Some modules are also a direct import for tree-shaking. The rest come from the barrel only.
|
|
29
|
+
|
|
30
|
+
| Subpath | Exports |
|
|
31
|
+
| --- | --- |
|
|
32
|
+
| `@heyramzi/utils/cn` | `cn`, `WithElementRef`, `WithoutChild`, `WithoutChildren`, `WithoutChildrenOrChild` |
|
|
33
|
+
| `@heyramzi/utils/format` | `formatDate`, `formatCurrency`, `formatAmount`, `formatMillisecondsToHours`, `formatRelativeTime` |
|
|
34
|
+
| `@heyramzi/utils/string` | `slugify`, `generateSlug`, `buildSearchParams`, `parseJSON`, `parseCommaSeparatedList`, `matchesQuery` |
|
|
35
|
+
| `@heyramzi/utils/timing` | `debounce`, `throttle`, `sleep` |
|
|
36
|
+
| `@heyramzi/utils/math` | `clamp` |
|
|
37
|
+
| `@heyramzi/utils/collection` | `groupBy` |
|
|
38
|
+
| `@heyramzi/utils/result` | `Ok`, `Err`, `tryCatch`, `tryCatchAsync`, `Result` |
|
|
39
|
+
| `@heyramzi/utils/crypto` | `encryptSecret`, `decryptSecret`, `encryptSecretWithKey`, `decryptSecretWithKey`, `packSecret`, `unpackSecret`, `SecretCiphertext` |
|
|
40
|
+
| `@heyramzi/utils/vite` | `packageImportsOverKitAlias`, `reclaimPort` |
|
|
41
|
+
| `@heyramzi/utils/grab` | Side effect only: `if (dev) import("@heyramzi/utils/grab")`, then Alt+Click copies an element's HTML and Svelte component stack |
|
|
42
|
+
|
|
43
|
+
Barrel only: `fetchApi`, `isCacheExpired`, `setLogger`, `log`.
|
|
44
|
+
|
|
45
|
+
## Usage in projects
|
|
46
|
+
|
|
47
|
+
Projects re-export what they need from their own `src/lib/utils.ts`:
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
// src/lib/utils.ts
|
|
51
|
+
export { cn, formatDate, formatRelativeTime, debounce } from "@heyramzi/utils";
|
|
52
|
+
export type { WithoutChild, WithoutChildren, WithElementRef } from "@heyramzi/utils";
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
This keeps the public API stable per-project while sourcing from a single implementation.
|
|
56
|
+
|
|
57
|
+
## Logger
|
|
58
|
+
|
|
59
|
+
`log()` does nothing until an app calls `setLogger()` once at startup with an implementation of the `Logger` interface. It never throws.
|
|
60
|
+
|
|
61
|
+
## Secret Encryption
|
|
62
|
+
|
|
63
|
+
`@heyramzi/utils/crypto` exists for reversible encryption of OAuth tokens and API keys at rest in app backends. It does not store anything for you and it does not implement provider-specific auth flows.
|
|
64
|
+
|
|
65
|
+
Import it as a server-only subpath:
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
import { decryptSecret, encryptSecret } from "@heyramzi/utils/crypto";
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
It uses AES-256-GCM with:
|
|
72
|
+
|
|
73
|
+
- a 32-byte master key from `HEYRAMZI_UTILS_CRYPTO_KEY`
|
|
74
|
+
- a fresh random 12-byte IV for every encryption
|
|
75
|
+
- base64 output for both `ciphertext` and `iv`
|
|
76
|
+
|
|
77
|
+
`ciphertext` includes the GCM auth tag, so consumers only need to persist `ciphertext` and `iv`. Use this helper only when the secret must be recovered later. It is not for password storage or hashing.
|
|
78
|
+
|
|
79
|
+
If an app already has its own env var name or stores encrypted secrets in a single database field, use the additive helpers:
|
|
80
|
+
|
|
81
|
+
- `encryptSecretWithKey` / `decryptSecretWithKey` to pass a raw 32-byte key directly
|
|
82
|
+
- `packSecret` / `unpackSecret` to bridge `{ ciphertext, iv }` with a single base64 payload
|
|
83
|
+
|
|
84
|
+
## Dependencies
|
|
85
|
+
|
|
86
|
+
- `clsx` + `tailwind-merge` (for `cn`)
|
|
87
|
+
- `vite` (optional peer, only for `@heyramzi/utils/vite`)
|
|
88
|
+
|
|
89
|
+
## Tooling scripts
|
|
90
|
+
|
|
91
|
+
This package hosts two code-intelligence CLIs as devDependencies, invoked from the utils package so they have a stable home. They are not part of the runtime library surface.
|
|
92
|
+
|
|
93
|
+
| Script | Command | What it does |
|
|
94
|
+
| -------------- | --------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
95
|
+
| `pnpm fallow` | [`fallow`][fallow] | Static analysis for TypeScript/JavaScript: unused files/exports/deps, circular references, copy-pasted blocks, complexity hotspots, architecture boundary drift. |
|
|
96
|
+
| `pnpm shannon` | [`@keygraph/shannon`][shan] | Autonomous AI pentester for web apps and APIs. Reads a source checkout, runs exploits against a live URL, reports only verified vulnerabilities. Requires Docker and Node 18+. |
|
|
97
|
+
|
|
98
|
+
Run from `packages/utils`:
|
|
99
|
+
|
|
100
|
+
```bash
|
|
101
|
+
pnpm fallow # summary across the codebase
|
|
102
|
+
pnpm fallow dead-code # just unused code
|
|
103
|
+
pnpm fallow audit --format json
|
|
104
|
+
pnpm shannon setup
|
|
105
|
+
pnpm shannon start -u https://example.com -r /path/to/repo
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
[fallow]: https://github.com/fallow-rs/fallow
|
|
109
|
+
[shan]: https://github.com/KeygraphHQ/shannon
|
|
110
|
+
|
|
111
|
+
## Removed
|
|
112
|
+
|
|
113
|
+
The `error-reporter`, `image` and `sveltekit` modules are gone, along with `getErrorMessage`, `CACHE_TTL`, `getCacheExpiry`, `getLogger` and `formatTime`. Nothing imported them, and the `sharp` and SvelteKit peers went with them.
|
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
|
+
}
|