refactor(types): remove forced tokenlens and cache casts#517
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
1 Skipped Deployment
|
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Repository UI Review profile: ASSERTIVE Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
The latest updates on your projects. Learn more about Unkey Deploy
|
Greptile SummaryThis PR is a type-safety refactor across three files, removing
Confidence Score: 5/5Safe to merge — all three files make purely type-level improvements with no runtime logic changes, and the one implicit behavioural fix (adding the missing The redirect sentinel swap from a frozen object to a Symbol is handled correctly at every call site — the cache type is widened, the equality check is preserved, and TypeScript now prevents accidentally passing the sentinel where a real No files require special attention. Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A["resolveCostModel(modelId)"] --> B{lookupAgentModelCost}
B -- "found" --> C["createCostModel(id, cost)\n{ canonical_id, cost, id, name } satisfies SourceModel"]
B -- "not found" --> D{lookupTokenlensModel}
D -- "found & has cost" --> E["{ canonical_id, ...model } satisfies SourceModel"]
D -- "not found" --> F["resolveAgentModelCost (fallback)\ncreateCostModel(fallback.id, fallback.cost, true)"]
C --> G[computeTokenCostsForModel]
E --> G
F --> G
subgraph "redirect.ts lookupLink(slug)"
H["linkCache.get(slug)"] --> I{undefined?}
I -- "yes" --> J[check Redis / DB]
I -- "no" --> K{=== NULL_SENTINEL Symbol?}
K -- "yes" --> L["return { link: null }"]
K -- "no" --> M["return { link: CachedLink }"]
J -- "DB miss" --> N["linkCache.set(slug, NULL_SENTINEL)"]
J -- "DB hit" --> O["linkCache.set(slug, link)"]
end
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
flowchart TD
A["resolveCostModel(modelId)"] --> B{lookupAgentModelCost}
B -- "found" --> C["createCostModel(id, cost)\n{ canonical_id, cost, id, name } satisfies SourceModel"]
B -- "not found" --> D{lookupTokenlensModel}
D -- "found & has cost" --> E["{ canonical_id, ...model } satisfies SourceModel"]
D -- "not found" --> F["resolveAgentModelCost (fallback)\ncreateCostModel(fallback.id, fallback.cost, true)"]
C --> G[computeTokenCostsForModel]
E --> G
F --> G
subgraph "redirect.ts lookupLink(slug)"
H["linkCache.get(slug)"] --> I{undefined?}
I -- "yes" --> J[check Redis / DB]
I -- "no" --> K{=== NULL_SENTINEL Symbol?}
K -- "yes" --> L["return { link: null }"]
K -- "no" --> M["return { link: CachedLink }"]
J -- "DB miss" --> N["linkCache.set(slug, NULL_SENTINEL)"]
J -- "DB hit" --> O["linkCache.set(slug, link)"]
end
Reviews (1): Last reviewed commit: "refactor(types): remove forced tokenlens..." | Re-trigger Greptile |
There was a problem hiding this comment.
No issues found across 3 files
Confidence score: 5/5
- Automated review surfaced no issues in the provided summaries.
- No files require special attention.
Architecture diagram
sequenceDiagram
participant UsageTelemetry as "Usage Telemetry"
participant Config as "AgentModelCost Config"
participant Tokenlens as "tokenlens / vercelModels"
participant Dashboard as "Dashboard Context"
participant LinkCache as "LRU Cache (linkCache)"
participant LinksRedirect as "Links Redirect"
Note over UsageTelemetry, Tokenlens: AI Usage – Cost Model Resolution
UsageTelemetry->>UsageTelemetry: resolveCostModel(modelId)
UsageTelemetry->>Config: lookupAgentModelCost(modelId)
alt Configured cost found
Config-->>UsageTelemetry: resolved cost + id
UsageTelemetry->>UsageTelemetry: createCostModel(id, cost, fallback=false)
Note right of UsageTelemetry: NEW: construct SourceModel\nwith satisfies (no cast)
else No configured cost
UsageTelemetry->>Tokenlens: vercelModels.models[id]
Tokenlens-->>UsageTelemetry: model entry (or undefined)
alt Tokenlens model exists
UsageTelemetry->>UsageTelemetry: construct CostModel with satisfies
else Tokenlens model not found
UsageTelemetry->>UsageTelemetry: create fallback cost model
end
end
UsageTelemetry-->>UsageTelemetry: return CostModel
Note over Dashboard, Tokenlens: Dashboard – Model Lookup
Dashboard->>Dashboard: lookupModel(modelId)
Dashboard->>Tokenlens: vercelModels.models[id]
Tokenlens-->>Dashboard: model entry
alt Model found
Dashboard->>Dashboard: return SourceModel with satisfies
else Not found
Dashboard-->>Dashboard: return undefined
end
Note over LinksRedirect, LinkCache: Redirect – Link Cache
LinksRedirect->>LinkCache: get(linkKey)
alt Cache hit
LinkCache-->>LinksRedirect: CachedLink or NULL_SENTINEL
Note right of LinksRedirect: CHANGED: sentinel is\nSymbol("null") instead of cast object
alt Value is NULL_SENTINEL
LinksRedirect->>LinksRedirect: treat as not found / expired
else Real cached link
LinksRedirect->>LinksRedirect: use cached link data
end
else Cache miss
LinkCache-->>LinksRedirect: undefined
LinksRedirect->>LinksRedirect: fetch link from DB / API
LinksRedirect->>LinkCache: set(key, link) or set(key, NULL_SENTINEL) for miss
LinkCache-->>LinksRedirect: stored
end
LinksRedirect-->>LinksRedirect: proceed with redirect logic
Shadow auto-approve: would auto-approve. Type-safety refactor: replace unsafe casts with satisfies and use Symbol sentinel for cache null. No behavior changes.
Re-trigger cubic
Summary
as unknown as SourceModelcasts from AI usage telemetry and dashboard token cost lookupSymbolsentinel and honest cache union typeVerification
bun run check-typesbun run lintbun run testcd packages/ai && bun test src/lib/usage-telemetry.test.tscd apps/links && bun test srcbunx ultracite check packages/ai/src/lib/usage-telemetry.ts apps/dashboard/components/ai-elements/context.tsx apps/links/src/routes/redirect.tsAI Disclosure
Summary by cubic
Remove unsafe casts in model cost lookups and caches to improve type safety, with no behavior changes. This makes
tokenlensintegrations and link caching clearer and safer.SourceModelwithsatisfies, resolve costs via configuredlookupAgentModelCostfirst, then fall back totokenlensmodels.satisfiesforSourceModel.Symbol("null")sentinel and a precise union type inLRUCache.Written for commit 0f9ac32. Summary will update on new commits.