Repository navigation
refactor(ai-gateway): share BYOK owner lookups and inline the direct BYOK dedupe - #8348
Conversation
getBYOKforUser/getBYOKforOrganization and the two provider-id lookups now delegate to one private query body parameterized by the owning column (kilo_user_id or organization_id). Contract unchanged: null for an empty provider list and for zero rows, created_at ordering, and the same exported signatures. The organization lookup still accepts a transaction. enhanceDirectByokModelList replaces the comma-operator dedupe with an explicit filter body; first occurrence still wins. Adds a DB-backed test covering owner scoping, created_at order, disabled keys, empty-provider and no-key null results, and the provider-id lookups.
Code Review SummaryStatus: No Issues Found | Recommendation: Merge Executive SummaryThe refactor is behavior-preserving for both BYOK owner lookups and the direct-BYOK dedupe, and the new DB-backed test verifies owner scoping, ordering, disabled-key exclusion, null results, and transaction support with high confidence. Files Reviewed (3 files)
Notes: verified the shared Reviewed by deepseek-v4.1-flash-1017 · Input: 65K · Output: 11.5K · Cached: 1.3M Review guidance: REVIEW.md from base branch |
What changed
packages/web-shared/src/lib/ai-gateway/byok/index.tsparameterized by the owning column (
byok_api_keys.kilo_user_idorbyok_api_keys.organization_id):getByokRowsForOwnerfor decrypted rows andgetEnabledByokProviderIdsfor provider-id lists.and behavior:
nullfor an empty provider list and for zero rows, ascendingcreated_atorder,is_enabled = true, and the same provider-id parsing.getBYOKforUserstays typedtypeof db;getBYOKforOrganizationkeepsaccepting a transaction.
packages/web-shared/src/lib/ai-gateway/providers/direct-byok/model-list.tsif (seenIds.has(id)) return false; seenIds.add(id); return true;) instead of a comma-operator side effect insidea ternary. First occurrence still wins; the recommended flag merge and the
reasoning_optionsfallback are unchanged.Test added
packages/web-shared/src/lib/ai-gateway/byok/index.test.ts(DB-backed, runsunder the apps/web Jest config). It seeds two users and two organizations with
distinct
created_atvalues and asserts, for both owner kinds:list,
null,getBYOKforOrganizationworks inside a transaction.End-to-end evidence
Local PostgreSQL from
dev/docker-compose.yml(pnpm test:db, migrationsapplied), real queries through the changed helpers, no mocks of the database:
The change is server-side query plumbing with no user-visible surface, so there
is no screenshot to publish. Behavior is observed through the real database
queries above, including the direct-byok merge output pinned by
model-list.test.ts(order, flags, reasoning options).Proxy answers
The owner's proxy gave these answers in the owner's place.
typeof dbwhile getBYOKforOrganization acceptstypeof db | DrizzleTransaction— the accidental divergence the finding names. How should the shared helper's signatures settle?Answer: Keep the user path narrow (
typeof db)Confidence: 0.60 · Precedent: No profile rule decides this. The work item says to keep the exported signatures, so share only the query body and leave getBYOKforUser typed
typeof db. Nearest rule, 'Put each separate concern in its own PR', keeps the widening out of this refactor.Answer: Add a permanent DB-backed test for the shared owner lookups
Confidence: 0.55 · Precedent: Rule 'If you see a gap or an error, fix it'. Rule 'Keep CI checks that catch real errors in PRs'. No unmocked test covers the org path, and this refactor touches that path.