@connextar/house 0.23.1 → 0.23.3
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 +22 -3
- package/dist/analytics/browser.d.ts +1 -0
- package/dist/analytics/browser.d.ts.map +1 -1
- package/dist/analytics/browser.js +20 -3
- package/dist/analytics/browser.js.map +1 -1
- package/dist/analytics/config.d.ts +5 -0
- package/dist/analytics/config.d.ts.map +1 -1
- package/dist/analytics/config.js +4 -1
- package/dist/analytics/config.js.map +1 -1
- package/dist/analytics/react.d.ts.map +1 -1
- package/dist/analytics/react.js +4 -2
- package/dist/analytics/react.js.map +1 -1
- package/dist/analytics/redact.d.ts.map +1 -1
- package/dist/analytics/redact.js +6 -1
- package/dist/analytics/redact.js.map +1 -1
- package/package.json +5 -2
- package/src/account/data-contract.ts +112 -0
- package/src/account/index.ts +11 -0
- package/src/admin/access.ts +267 -0
- package/src/admin/audit.ts +238 -0
- package/src/admin/bootstrap.ts +220 -0
- package/src/admin/client-index.ts +19 -0
- package/src/admin/client.tsx +187 -0
- package/src/admin/index.ts +91 -0
- package/src/admin/proof.ts +155 -0
- package/src/admin/second-factor-index.ts +16 -0
- package/src/admin/second-factor.ts +403 -0
- package/src/admin/testing-index.ts +13 -0
- package/src/admin/testing.ts +299 -0
- package/src/admin/view-as.ts +511 -0
- package/src/ai/config-index.ts +40 -0
- package/src/ai/config.ts +176 -0
- package/src/ai/images.ts +18 -0
- package/src/ai/index.ts +21 -0
- package/src/ai/pricing.ts +107 -0
- package/src/ai/provider.ts +403 -0
- package/src/analytics/browser.ts +400 -0
- package/src/analytics/client-index.ts +12 -0
- package/src/analytics/config.ts +89 -0
- package/src/analytics/index.ts +17 -0
- package/src/analytics/ingest.ts +81 -0
- package/src/analytics/react-index.ts +16 -0
- package/src/analytics/react.tsx +241 -0
- package/src/analytics/redact.ts +146 -0
- package/src/analytics/server-index.ts +17 -0
- package/src/analytics/server.ts +277 -0
- package/src/auth/index.ts +30 -0
- package/src/auth/second-factor-index.ts +16 -0
- package/src/auth/second-factor-next.ts +136 -0
- package/src/auth/second-factor-routes.ts +40 -0
- package/src/auth/second-factor.ts +226 -0
- package/src/auth/step-up-client-index.ts +13 -0
- package/src/auth/step-up-client.ts +23 -0
- package/src/auth/step-up-index.ts +19 -0
- package/src/auth/step-up-routes.ts +99 -0
- package/src/auth/step-up.ts +432 -0
- package/src/auth/two-factor-policy.ts +112 -0
- package/src/changelog/changelog.tsx +367 -0
- package/src/changelog/index.ts +12 -0
- package/src/editor/block-handle.tsx +312 -0
- package/src/editor/blocks.ts +169 -0
- package/src/editor/bubble-toolbar.tsx +208 -0
- package/src/editor/collapsible.ts +159 -0
- package/src/editor/content-index.ts +1 -0
- package/src/editor/content.tsx +343 -0
- package/src/editor/editor.tsx +333 -0
- package/src/editor/emoji-index.ts +1 -0
- package/src/editor/emoji-picker.tsx +114 -0
- package/src/editor/emoji.ts +436 -0
- package/src/editor/index.ts +6 -0
- package/src/editor/sections.ts +83 -0
- package/src/editor/suggestion-popup.tsx +136 -0
- package/src/editor/suggestions.ts +309 -0
- package/src/editor/ui.tsx +171 -0
- package/src/errors/error-shell.tsx +72 -0
- package/src/errors/global-error-shell.tsx +116 -0
- package/src/errors/index.ts +2 -0
- package/src/grid/columns.ts +48 -0
- package/src/grid/compare.ts +35 -0
- package/src/grid/hidden-columns.ts +95 -0
- package/src/grid/index.ts +27 -0
- package/src/grid/use-grid.ts +282 -0
- package/src/grid/views.ts +70 -0
- package/src/guides/guide-article.tsx +92 -0
- package/src/guides/guide-figure-zoom.tsx +533 -0
- package/src/guides/guide-figure.tsx +68 -0
- package/src/guides/guide-toc.tsx +133 -0
- package/src/guides/guides-landing.tsx +57 -0
- package/src/guides/guides-search.tsx +160 -0
- package/src/guides/guides-shell.tsx +60 -0
- package/src/guides/guides-sidebar.tsx +93 -0
- package/src/guides/index.ts +28 -0
- package/src/guides/library.ts +165 -0
- package/src/health/health.ts +115 -0
- package/src/health/index.ts +11 -0
- package/src/internal/cx.ts +4 -0
- package/src/internal/next-link.ts +23 -0
- package/src/jobs/counts.ts +151 -0
- package/src/jobs/cron-auth.ts +60 -0
- package/src/jobs/health.ts +315 -0
- package/src/jobs/idle.ts +86 -0
- package/src/jobs/index.ts +120 -0
- package/src/jobs/job-health-list.tsx +316 -0
- package/src/jobs/next-index.ts +4 -0
- package/src/jobs/next.ts +65 -0
- package/src/jobs/prisma-index.ts +12 -0
- package/src/jobs/prisma.ts +170 -0
- package/src/jobs/prune.ts +79 -0
- package/src/jobs/react-index.ts +35 -0
- package/src/jobs/registry.ts +189 -0
- package/src/jobs/scheduler.ts +437 -0
- package/src/jobs/store.ts +212 -0
- package/src/jobs/windows.ts +157 -0
- package/src/maintenance/core.ts +270 -0
- package/src/maintenance/gate.ts +171 -0
- package/src/maintenance/index.ts +40 -0
- package/src/maintenance/page.ts +76 -0
- package/src/maintenance/proxy.ts +88 -0
- package/src/maintenance/react-index.ts +7 -0
- package/src/maintenance/react.tsx +190 -0
- package/src/maintenance/route.ts +82 -0
- package/src/maintenance/unavailable.ts +61 -0
- package/src/markdown/figure.tsx +132 -0
- package/src/markdown/figures.ts +237 -0
- package/src/markdown/guide.tsx +536 -0
- package/src/markdown/index.ts +53 -0
- package/src/markdown/table-view.tsx +74 -0
- package/src/markdown/table.ts +90 -0
- package/src/password/common.ts +58 -0
- package/src/password/estimate-index.ts +18 -0
- package/src/password/estimate.ts +715 -0
- package/src/password/index.ts +40 -0
- package/src/password/password-input.tsx +116 -0
- package/src/password/password-strength.tsx +157 -0
- package/src/password/reveal.ts +151 -0
- package/src/rate-limit/better-auth.ts +94 -0
- package/src/rate-limit/index.ts +32 -0
- package/src/rate-limit/keys.ts +110 -0
- package/src/rate-limit/limiter.ts +276 -0
- package/src/rate-limit/prisma-index.ts +12 -0
- package/src/rate-limit/prisma.ts +157 -0
- package/src/rate-limit/store.ts +47 -0
- package/src/release/changelog.ts +364 -0
- package/src/release/index.ts +32 -0
- package/src/release/version.ts +64 -0
- package/src/setup-guide/bits.tsx +420 -0
- package/src/setup-guide/card.tsx +358 -0
- package/src/setup-guide/class-names.ts +144 -0
- package/src/setup-guide/context.tsx +239 -0
- package/src/setup-guide/guide.ts +271 -0
- package/src/setup-guide/index.ts +76 -0
- package/src/setup-guide/launcher.tsx +207 -0
- package/src/setup-guide/strip.tsx +127 -0
- package/src/styles/house.css +384 -0
- package/src/trail/index.ts +12 -0
- package/src/trail/rules.ts +84 -0
- package/src/trail/trail.tsx +367 -0
- package/src/ux/errors.ts +32 -0
- package/src/ux/feedback.tsx +41 -0
- package/src/ux/index.ts +44 -0
- package/src/ux/navigation-index.ts +16 -0
- package/src/ux/navigation-progress.tsx +270 -0
- package/src/ux/optimistic.ts +415 -0
- package/src/ux/use-action.ts +155 -0
- package/src/ux/use-optimistic-list.ts +227 -0
- package/src/ux/use-optimistic-value.ts +136 -0
- package/src/web-search/index.ts +13 -0
- package/src/web-search/providers.ts +177 -0
- package/src/web-search/types.ts +98 -0
- package/src/whats-new/body.tsx +190 -0
- package/src/whats-new/index.ts +43 -0
- package/src/whats-new/seen.ts +40 -0
- package/src/whats-new/select.ts +230 -0
- package/src/whats-new/use-whats-new.ts +109 -0
- package/src/wizard/index.ts +61 -0
- package/src/wizard/invalid.ts +152 -0
- package/src/wizard/react-hook-form-index.ts +11 -0
- package/src/wizard/rhf-adapter.ts +67 -0
- package/src/wizard/rules.ts +137 -0
- package/src/wizard/wizard.tsx +442 -0
package/src/ai/config.ts
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which model provider an instance uses, and which model it uses for which job.
|
|
3
|
+
*
|
|
4
|
+
* Extracted from AltEd and PharmaLine, which had each written this file and were
|
|
5
|
+
* about to be joined by a third. Two rules shape it, and both are why it is
|
|
6
|
+
* shared rather than copied.
|
|
7
|
+
*
|
|
8
|
+
* **Naming a provider is a decision, not a preference.** If `AI_PROVIDER` names
|
|
9
|
+
* a provider whose key is missing, the feature is *off* and says why — it does
|
|
10
|
+
* not quietly send a person's records to the other one. Silently switching
|
|
11
|
+
* processors would break the promise a privacy policy makes by name.
|
|
12
|
+
*
|
|
13
|
+
* **Jobs, not one model.** Sorting a bank line wants a small fast model; writing
|
|
14
|
+
* a report wants a capable one. The app names its jobs and, for each, the
|
|
15
|
+
* environment variable an operator sets per provider and a working default.
|
|
16
|
+
* The model name is provider-specific, so pointing `AI_PROVIDER` at OpenAI can
|
|
17
|
+
* never send a Claude model name to OpenAI.
|
|
18
|
+
*
|
|
19
|
+
* Deliberately free of `server-only` and of anything Next-specific: PharmaLine
|
|
20
|
+
* reads it from a plain Node service. An app binds it from a server-only
|
|
21
|
+
* module of its own, because the provider half reads API keys.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
export const AI_PROVIDERS = ["ANTHROPIC", "OPENAI"] as const;
|
|
25
|
+
export type AiProviderName = (typeof AI_PROVIDERS)[number];
|
|
26
|
+
|
|
27
|
+
/** How each provider is named to a person — in a privacy policy, in settings, in logs. */
|
|
28
|
+
export const PROVIDER_LABELS: Record<AiProviderName, string> = {
|
|
29
|
+
ANTHROPIC: "Anthropic",
|
|
30
|
+
OPENAI: "OpenAI",
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/** The environment variable holding each provider's key. */
|
|
34
|
+
export const PROVIDER_KEY_VARS: Record<AiProviderName, string> = {
|
|
35
|
+
ANTHROPIC: "ANTHROPIC_API_KEY",
|
|
36
|
+
OPENAI: "OPENAI_API_KEY",
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/** One provider's model for one job: the variable an operator sets, and the default when they don't. */
|
|
40
|
+
export interface AiModelSpec {
|
|
41
|
+
modelVar: string;
|
|
42
|
+
/** A working starting point. Every one is overridable; none is load-bearing. */
|
|
43
|
+
defaultModel: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** One job the app gives a model. */
|
|
47
|
+
export interface AiTaskSpec {
|
|
48
|
+
ANTHROPIC: AiModelSpec;
|
|
49
|
+
OPENAI: AiModelSpec;
|
|
50
|
+
/**
|
|
51
|
+
* Whether structured calls for this job ask Anthropic for adaptive thinking.
|
|
52
|
+
* Off unless the job says so: the small fast models (Haiku 4.5) reject the
|
|
53
|
+
* setting outright with a 400, and a job's default model is exactly where
|
|
54
|
+
* that knowledge belongs. A call can still override it.
|
|
55
|
+
*/
|
|
56
|
+
thinking?: boolean;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The app's jobs, by name. */
|
|
60
|
+
export type AiTaskTable<Task extends string> = Record<Task, AiTaskSpec>;
|
|
61
|
+
|
|
62
|
+
/** Just the parts of the environment this module reads — so tests need no globals. */
|
|
63
|
+
export type AiEnv = Record<string, string | undefined>;
|
|
64
|
+
|
|
65
|
+
const read = (env: AiEnv, name: string): string => (env[name] ?? "").trim();
|
|
66
|
+
|
|
67
|
+
/** Providers with a key set, in preference order. */
|
|
68
|
+
export function configuredProviders(env: AiEnv): AiProviderName[] {
|
|
69
|
+
return AI_PROVIDERS.filter((provider) => read(env, PROVIDER_KEY_VARS[provider]).length > 0);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export interface ProviderChoice {
|
|
73
|
+
provider: AiProviderName | null;
|
|
74
|
+
apiKey: string;
|
|
75
|
+
/** Set when the configuration is contradictory, for an operator to read. */
|
|
76
|
+
problem: string | null;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* The provider this instance will use.
|
|
81
|
+
*
|
|
82
|
+
* `AI_PROVIDER` unset means "whichever is configured", preferring Anthropic when
|
|
83
|
+
* both are — an arbitrary but stable order, so adding a second key never silently
|
|
84
|
+
* moves an instance to a different processor. Nothing configured at all is not a
|
|
85
|
+
* problem, just a feature that is off.
|
|
86
|
+
*/
|
|
87
|
+
export function chooseProvider(env: AiEnv): ProviderChoice {
|
|
88
|
+
const requested = read(env, "AI_PROVIDER").toUpperCase();
|
|
89
|
+
|
|
90
|
+
if (requested) {
|
|
91
|
+
const named = AI_PROVIDERS.find((provider) => provider === requested);
|
|
92
|
+
if (!named) {
|
|
93
|
+
return {
|
|
94
|
+
provider: null,
|
|
95
|
+
apiKey: "",
|
|
96
|
+
problem: `AI_PROVIDER is set to “${read(env, "AI_PROVIDER")}” — expected ${AI_PROVIDERS.map((p) => p.toLowerCase()).join(" or ")}.`,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
const apiKey = read(env, PROVIDER_KEY_VARS[named]);
|
|
100
|
+
if (!apiKey) {
|
|
101
|
+
return {
|
|
102
|
+
provider: null,
|
|
103
|
+
apiKey: "",
|
|
104
|
+
problem: `AI_PROVIDER names ${PROVIDER_LABELS[named]}, but ${PROVIDER_KEY_VARS[named]} is empty.`,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
return { provider: named, apiKey, problem: null };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const [first] = configuredProviders(env);
|
|
111
|
+
if (!first) return { provider: null, apiKey: "", problem: null };
|
|
112
|
+
return { provider: first, apiKey: read(env, PROVIDER_KEY_VARS[first]), problem: null };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** The model this provider uses for this job — the operator's, or the default. */
|
|
116
|
+
export function modelFor<Task extends string>(
|
|
117
|
+
tasks: AiTaskTable<Task>,
|
|
118
|
+
provider: AiProviderName,
|
|
119
|
+
task: Task,
|
|
120
|
+
env: AiEnv,
|
|
121
|
+
): string {
|
|
122
|
+
const spec = tasks[task][provider];
|
|
123
|
+
return read(env, spec.modelVar) || spec.defaultModel;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export interface ResolvedAi {
|
|
127
|
+
provider: AiProviderName;
|
|
128
|
+
apiKey: string;
|
|
129
|
+
model: string;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** Everything one call needs, or null when the feature is off. */
|
|
133
|
+
export function resolveAi<Task extends string>(tasks: AiTaskTable<Task>, task: Task, env: AiEnv): ResolvedAi | null {
|
|
134
|
+
const choice = chooseProvider(env);
|
|
135
|
+
if (!choice.provider) return null;
|
|
136
|
+
return { provider: choice.provider, apiKey: choice.apiKey, model: modelFor(tasks, choice.provider, task, env) };
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export interface AiStatus<Task extends string = string> {
|
|
140
|
+
enabled: boolean;
|
|
141
|
+
provider: AiProviderName | null;
|
|
142
|
+
/** "Anthropic" / "OpenAI" — the name to show a person, and to name in a policy. */
|
|
143
|
+
providerLabel: string | null;
|
|
144
|
+
/** The model per job, when enabled. */
|
|
145
|
+
models: Record<Task, string> | null;
|
|
146
|
+
/** A misconfiguration worth telling an operator about, when there is one. */
|
|
147
|
+
problem: string | null;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** What the settings and operator screens show. */
|
|
151
|
+
export function aiStatus<Task extends string>(tasks: AiTaskTable<Task>, env: AiEnv): AiStatus<Task> {
|
|
152
|
+
const choice = chooseProvider(env);
|
|
153
|
+
if (!choice.provider) {
|
|
154
|
+
return { enabled: false, provider: null, providerLabel: null, models: null, problem: choice.problem };
|
|
155
|
+
}
|
|
156
|
+
const provider = choice.provider;
|
|
157
|
+
const models = Object.fromEntries(
|
|
158
|
+
(Object.keys(tasks) as Task[]).map((task) => [task, modelFor(tasks, provider, task, env)]),
|
|
159
|
+
) as Record<Task, string>;
|
|
160
|
+
return { enabled: true, provider, providerLabel: PROVIDER_LABELS[provider], models, problem: null };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* What a *person* may be told: everything but the misconfiguration.
|
|
165
|
+
*
|
|
166
|
+
* `problem` names environment variables and echoes the operator's configured
|
|
167
|
+
* provider — deployment detail, never a secret, but it has no business in a
|
|
168
|
+
* payload sent to every signed-in account (AltEd audit round 6). The operator
|
|
169
|
+
* screen, which is where somebody would act on it, still gets the full shape.
|
|
170
|
+
*/
|
|
171
|
+
export type PublicAiStatus<Task extends string = string> = Omit<AiStatus<Task>, "problem">;
|
|
172
|
+
|
|
173
|
+
export function publicAiStatus<Task extends string>(tasks: AiTaskTable<Task>, env: AiEnv): PublicAiStatus<Task> {
|
|
174
|
+
const { enabled, provider, providerLabel, models } = aiStatus(tasks, env);
|
|
175
|
+
return { enabled, provider, providerLabel, models };
|
|
176
|
+
}
|
package/src/ai/images.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The pictures a structured call can carry. Their own module, free of either SDK,
|
|
3
|
+
* so an upload form can check a file's type from `@connextar/house/ai/config`.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/** The image types both providers accept inline. */
|
|
7
|
+
export const AI_IMAGE_TYPES = ["image/jpeg", "image/png", "image/gif", "image/webp"] as const;
|
|
8
|
+
export type AiImageType = (typeof AI_IMAGE_TYPES)[number];
|
|
9
|
+
|
|
10
|
+
export function isAiImageType(value: string): value is AiImageType {
|
|
11
|
+
return (AI_IMAGE_TYPES as readonly string[]).includes(value);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface AiImage {
|
|
15
|
+
mediaType: AiImageType;
|
|
16
|
+
/** Base64, no data-URL prefix. */
|
|
17
|
+
base64: string;
|
|
18
|
+
}
|
package/src/ai/index.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@connextar/house/ai` — the harness, and everything it is configured with.
|
|
3
|
+
* Server-only in practice: it carries both provider SDKs. A client component
|
|
4
|
+
* imports `@connextar/house/ai/config` instead.
|
|
5
|
+
*/
|
|
6
|
+
export * from "./config-index.js";
|
|
7
|
+
export {
|
|
8
|
+
AiServiceError,
|
|
9
|
+
AiUnavailableError,
|
|
10
|
+
AiUnusableAnswerError,
|
|
11
|
+
OPENAI_MIN_OUTPUT_TOKENS,
|
|
12
|
+
createAiHarness,
|
|
13
|
+
takesReasoningEffort,
|
|
14
|
+
type AiCallResult,
|
|
15
|
+
type AiHarness,
|
|
16
|
+
type AiHarnessOptions,
|
|
17
|
+
type AiStructuredCall,
|
|
18
|
+
type AiTextCall,
|
|
19
|
+
type AiUnusableReason,
|
|
20
|
+
type OpenAiReasoningEffort,
|
|
21
|
+
} from "./provider.js";
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type { AiProviderName } from "./config.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* What a model call costs, at the provider's published rates.
|
|
5
|
+
*
|
|
6
|
+
* The arithmetic and the starting rates only. Where the rates are *kept* is each
|
|
7
|
+
* app's decision — AltEd and PharmaLine keep a dated price table an operator can
|
|
8
|
+
* correct; a smaller app can price straight from `DEFAULT_PRICES` — so nothing
|
|
9
|
+
* here touches a database. Nothing here is the source of truth for a price
|
|
10
|
+
* either: the provider's pricing page is, which is why every row records where
|
|
11
|
+
* it came from and when.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
export interface PriceRates {
|
|
15
|
+
inputPerMillionUsd: number;
|
|
16
|
+
outputPerMillionUsd: number;
|
|
17
|
+
cacheReadPerMillionUsd: number;
|
|
18
|
+
cacheWritePerMillionUsd: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface ModelPrice extends PriceRates {
|
|
22
|
+
provider: AiProviderName;
|
|
23
|
+
model: string;
|
|
24
|
+
source: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const ANTHROPIC_SOURCE = "Anthropic API pricing, checked 2026-06-24 — cache reads 10% of input, cache writes 125%";
|
|
28
|
+
const OPENAI_SOURCE = "OpenAI API pricing, checked 2026-09-07 — cached input 10% of input, no write premium";
|
|
29
|
+
|
|
30
|
+
/** Per million tokens, USD. Cache multipliers are each provider's published rule. */
|
|
31
|
+
const anthropic = (model: string, input: number, output: number): ModelPrice => ({
|
|
32
|
+
provider: "ANTHROPIC",
|
|
33
|
+
model,
|
|
34
|
+
inputPerMillionUsd: input,
|
|
35
|
+
outputPerMillionUsd: output,
|
|
36
|
+
cacheReadPerMillionUsd: input * 0.1,
|
|
37
|
+
cacheWritePerMillionUsd: input * 1.25,
|
|
38
|
+
source: ANTHROPIC_SOURCE,
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const openai = (model: string, input: number, output: number): ModelPrice => ({
|
|
42
|
+
provider: "OPENAI",
|
|
43
|
+
model,
|
|
44
|
+
inputPerMillionUsd: input,
|
|
45
|
+
outputPerMillionUsd: output,
|
|
46
|
+
cacheReadPerMillionUsd: input * 0.1,
|
|
47
|
+
cacheWritePerMillionUsd: input,
|
|
48
|
+
source: OPENAI_SOURCE,
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The rates the portfolio's apps start from — the union of AltEd's and
|
|
53
|
+
* PharmaLine's seeded tables. A model missing from here is unpriced, not free:
|
|
54
|
+
* `findPrice` returns null and the caller records the tokens without a cost.
|
|
55
|
+
*/
|
|
56
|
+
export const DEFAULT_PRICES: readonly ModelPrice[] = Object.freeze([
|
|
57
|
+
anthropic("claude-opus-5", 5, 25),
|
|
58
|
+
anthropic("claude-sonnet-5", 2, 10),
|
|
59
|
+
anthropic("claude-haiku-4-5", 1, 5),
|
|
60
|
+
anthropic("claude-opus-4-8", 5, 25),
|
|
61
|
+
anthropic("claude-sonnet-4-6", 3, 15),
|
|
62
|
+
openai("gpt-5", 1.25, 10),
|
|
63
|
+
openai("gpt-5-mini", 0.25, 2),
|
|
64
|
+
]);
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The family a reported model id belongs to.
|
|
68
|
+
*
|
|
69
|
+
* Providers report dated snapshots (`claude-haiku-4-5-20251001`) and priced
|
|
70
|
+
* variants (`gpt-5-2026-03-01`) of a family that is priced once. The date
|
|
71
|
+
* suffix is dropped for the lookup, so a row for the family covers them all —
|
|
72
|
+
* and an exact row, when one exists, still wins.
|
|
73
|
+
*/
|
|
74
|
+
export function modelFamily(model: string): string {
|
|
75
|
+
return model.replace(/[-_]?\d{4}-?\d{2}-?\d{2}$/, "");
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** The rate for a model from a list: an exact row first, then its family's. Null when none is known. */
|
|
79
|
+
export function findPrice<P extends { provider: AiProviderName; model: string }>(
|
|
80
|
+
prices: readonly P[],
|
|
81
|
+
provider: AiProviderName,
|
|
82
|
+
model: string,
|
|
83
|
+
): P | null {
|
|
84
|
+
for (const candidate of [...new Set([model, modelFamily(model)])]) {
|
|
85
|
+
const row = prices.find((price) => price.provider === provider && price.model === candidate);
|
|
86
|
+
if (row) return row;
|
|
87
|
+
}
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export interface TokenCounts {
|
|
92
|
+
inputTokens: number;
|
|
93
|
+
outputTokens: number;
|
|
94
|
+
cacheReadTokens?: number;
|
|
95
|
+
cacheWriteTokens?: number;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** The cost of a call at a rate, in USD. Six decimals: a Haiku call is fractions of a cent. */
|
|
99
|
+
export function costUsd(tokens: TokenCounts, rates: PriceRates): number {
|
|
100
|
+
const per = (count: number, rate: number) => (count / 1_000_000) * rate;
|
|
101
|
+
const total =
|
|
102
|
+
per(tokens.inputTokens, rates.inputPerMillionUsd) +
|
|
103
|
+
per(tokens.outputTokens, rates.outputPerMillionUsd) +
|
|
104
|
+
per(tokens.cacheReadTokens ?? 0, rates.cacheReadPerMillionUsd) +
|
|
105
|
+
per(tokens.cacheWriteTokens ?? 0, rates.cacheWritePerMillionUsd);
|
|
106
|
+
return Math.round(total * 1_000_000) / 1_000_000;
|
|
107
|
+
}
|