Skip to content

Commit 7b6c683

Browse files
authored
refactor(types): remove forced tokenlens and cache casts
Remove forced tokenlens SourceModel casts and replace the links null cache cast with an honest symbol sentinel.
1 parent 58876a2 commit 7b6c683

3 files changed

Lines changed: 22 additions & 24 deletions

File tree

apps/dashboard/components/ai-elements/context.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ import { computeTokenCostsForModel } from "tokenlens/helpers";
1313
import { vercelModels } from "tokenlens/providers/vercel";
1414
import { Button, Progress } from "@databuddy/ui";
1515

16-
type VercelModelId = keyof typeof vercelModels.models;
17-
1816
const lookupModel = (modelId: string): SourceModel | undefined => {
19-
const model = vercelModels.models[modelId as VercelModelId];
17+
const model =
18+
vercelModels.models[modelId as keyof typeof vercelModels.models];
2019
return model
21-
? ({ canonical_id: model.id, ...model } as unknown as SourceModel)
20+
? ({ canonical_id: model.id, ...model } satisfies SourceModel)
2221
: undefined;
2322
};
2423

apps/links/src/routes/redirect.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ const EXPIRED_URL = `${config.urls.dashboard}/dby/expired`;
2323
const NOT_FOUND_URL = `${config.urls.dashboard}/dby/not-found`;
2424
const OG_PROXY_URL = `${config.urls.dashboard}/dby/l`;
2525

26-
const NULL_SENTINEL = Object.freeze({ __null: true }) as unknown as CachedLink;
27-
const linkCache = new LRUCache<string, CachedLink>({ max: 1000, ttl: 5000 });
26+
const NULL_SENTINEL = Symbol("null");
27+
const linkCache = new LRUCache<string, CachedLink | typeof NULL_SENTINEL>({
28+
max: 1000,
29+
ttl: 5000,
30+
});
2831
const etagCache = new LRUCache<string, string>({ max: 1000, ttl: 60_000 });
2932
const dedupCache = new LRUCache<string, true>({ max: 10_000, ttl: 300_000 });
3033
const botCache = new LRUCache<string, { isBot: boolean; isSocial: boolean }>({

packages/ai/src/lib/usage-telemetry.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,48 +9,44 @@ import type { SourceModel } from "tokenlens";
99
import { computeTokenCostsForModel } from "tokenlens/helpers";
1010
import { vercelModels } from "tokenlens/providers/vercel";
1111

12-
type VercelModelId = keyof typeof vercelModels.models;
13-
1412
interface CostModel {
1513
fallback: boolean;
1614
id: string;
1715
model: SourceModel;
1816
}
1917

20-
function createSourceModel(
21-
id: string,
22-
cost: AgentModelCostUsdPerMillion
23-
): SourceModel {
24-
return { canonical_id: id, cost, id } as unknown as SourceModel;
25-
}
26-
2718
function createCostModel(
2819
id: string,
2920
cost: AgentModelCostUsdPerMillion,
3021
fallback = false
3122
): CostModel {
32-
return { fallback, id, model: createSourceModel(id, cost) };
33-
}
34-
35-
function lookupConfiguredModel(modelId: string): CostModel | null {
36-
const resolved = lookupAgentModelCost(modelId);
37-
return resolved ? createCostModel(resolved.id, resolved.cost) : null;
23+
return {
24+
fallback,
25+
id,
26+
model: { canonical_id: id, cost, id, name: id } satisfies SourceModel,
27+
};
3828
}
3929

4030
function lookupTokenlensModel(modelId: string): CostModel | null {
41-
const model = vercelModels.models[modelId as VercelModelId];
31+
const model =
32+
vercelModels.models[modelId as keyof typeof vercelModels.models];
4233
if (!model?.cost) {
4334
return null;
4435
}
4536
return {
4637
fallback: false,
4738
id: model.id,
48-
model: { canonical_id: model.id, ...model } as unknown as SourceModel,
39+
model: { canonical_id: model.id, ...model } satisfies SourceModel,
4940
};
5041
}
5142

5243
function resolveCostModel(modelId: string): CostModel {
53-
const model = lookupConfiguredModel(modelId) ?? lookupTokenlensModel(modelId);
44+
const configured = lookupAgentModelCost(modelId);
45+
if (configured) {
46+
return createCostModel(configured.id, configured.cost);
47+
}
48+
49+
const model = lookupTokenlensModel(modelId);
5450
if (model) {
5551
return model;
5652
}

0 commit comments

Comments
 (0)