-
Notifications
You must be signed in to change notification settings - Fork 457
feat: [ENG-3548] Durable Object fallback for AI Gateway provider keys #5259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub. 3 Skipped Deployments
|
|
Claude finished @connortbot's task —— View job PR Review: Durable Object fallback for AI Gateway provider keysTodo List:
Score: 6/10 - Minor issues and improvements suggested, merge with considerationCritical Issues & Security Concerns:1. Type Safety Issue in ProviderKeyCache
|
Greptile Summary
Confidence Score: 3/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant User
participant Worker
participant KVCache
participant DOCache
participant Postgres
User->>Worker: Request with provider key needed
Worker->>KVCache: Get provider keys for org
alt KV Success
KVCache-->>Worker: Return cached keys
Worker-->>User: Process request with key
else KV Fails
KVCache--xWorker: Error/Timeout
Worker->>DOCache: Fallback: Get provider keys
alt DO Success
DOCache-->>Worker: Return cached keys
Worker-->>User: Process request with key
else DO Fails/Empty
DOCache--xWorker: Error/Empty
Worker->>Postgres: Fetch from database
Postgres-->>Worker: Return keys from DB
Worker->>DOCache: Update DO cache
Worker->>KVCache: Update KV cache (if not failed)
Worker-->>User: Process request with key
end
end
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
12 files reviewed, 2 comments
Edit Code Review Agent Settings | Greptile
React with 👍 or 👎 to share your feedback on this new summary format
| if (existingKeys) { | ||
| const existingKeysData = JSON.parse(existingKeys) as ProviderKey[]; | ||
| existingKeysData.push(...keysFromDb); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: After fetching from DB, keysFromDb is appended to existing KV cache, which can create duplicates if the same keys are already cached.
| if (existingKeys) { | |
| const existingKeysData = JSON.parse(existingKeys) as ProviderKey[]; | |
| existingKeysData.push(...keysFromDb); | |
| const existingKeysData = JSON.parse(existingKeys) as ProviderKey[]; | |
| // Merge keys, preferring fresh data from DB | |
| const keyMap = new Map(existingKeysData.map(k => [`${k.provider}-${k.cuid || ''}`, k])); | |
| keysFromDb.forEach(k => keyMap.set(`${k.provider}-${k.cuid || ''}`, k)); | |
| await storeInCache( | |
| `provider_keys_${orgId}`, | |
| JSON.stringify(Array.from(keyMap.values())), |
Prompt To Fix With AI
This is a comment left during a code review.
Path: worker/src/lib/managers/ProviderKeysManager.ts
Line: 162:164
Comment:
**logic:** After fetching from DB, `keysFromDb` is appended to existing KV cache, which can create duplicates if the same keys are already cached.
```suggestion
const existingKeysData = JSON.parse(existingKeys) as ProviderKey[];
// Merge keys, preferring fresh data from DB
const keyMap = new Map(existingKeysData.map(k => [`${k.provider}-${k.cuid || ''}`, k]));
keysFromDb.forEach(k => keyMap.set(`${k.provider}-${k.cuid || ''}`, k));
await storeInCache(
`provider_keys_${orgId}`,
JSON.stringify(Array.from(keyMap.values())),
```
How can I resolve this? If you propose a fix, please make it concise.| decrypted_provider_secret_key: row.decrypted_provider_secret_key, | ||
| auth_type: row.auth_type as "key" | "session_token", | ||
| byok_enabled: row.byok_enabled !== null ? row.byok_enabled === 1 : null, | ||
| config: row.config ? JSON.parse(row.config) : null, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: JSON.parse on potentially malformed config data lacks error handling - parsing failure will crash the DO
| config: row.config ? JSON.parse(row.config) : null, | |
| config: row.config ? (() => { try { return JSON.parse(row.config); } catch { return null; } })() : null, |
Prompt To Fix With AI
This is a comment left during a code review.
Path: worker/src/lib/durable-objects/ProviderKeyCache.ts
Line: 161:161
Comment:
**logic:** JSON.parse on potentially malformed config data lacks error handling - parsing failure will crash the DO
```suggestion
config: row.config ? (() => { try { return JSON.parse(row.config); } catch { return null; } })() : null,
```
How can I resolve this? If you propose a fix, please make it concise.
https://linear.app/helicone/issue/ENG-3548/fallback-for-keys-when-kvcache-dies
Affects Jawn + Worker
Context
In the past we've experienced times when KVCache would fail. Given the traffic of the AI Gateway these days and the way its trending, if this ever happens again our Postgres will die.
This adds another layer of caching for provider keys by falling back to a Durable Object namespace built for this use case.