-
Notifications
You must be signed in to change notification settings - Fork 1
Add AI Gateway WebSocket client with reconnect and draining support #1609
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bf795c5
Add reconnecting AI Gateway WebSocket client with v1 protocol support.
jhaynie d2a023c
Hand off to a new WebSocket while retiring socket finishes in-flight …
jhaynie 4806ebb
Fix aigateway dist imports and require orgId only for CLI keys.
jhaynie ad2e4c2
fixes
jhaynie 53168af
fix(aigateway): resolve all 9 inline/nitpick findings
jhaynie bf55858
fix(aigateway): add @agentuity/core to declared dependencies
jhaynie 53d1e0b
fix(aigateway): add ../core to tsconfig project references
jhaynie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import { z } from 'zod'; | ||
|
|
||
| export function isCliApiKey(apiKey: string): boolean { | ||
| return apiKey.startsWith('ck_'); | ||
| } | ||
|
|
||
| export const AIGatewayWSFrameType = { | ||
| request: 'request', | ||
| response: 'response', | ||
| error: 'error', | ||
| cancel: 'cancel', | ||
| draining: 'draining', | ||
| } as const; | ||
|
|
||
| export const AIGatewayWSResponseStatus = { | ||
| complete: 'complete', | ||
| delta: 'delta', | ||
| thinkingDelta: 'thinking_delta', | ||
| } as const; | ||
|
|
||
| export const AIGatewayWSUsageSchema = z.object({ | ||
| prompt: z.number(), | ||
| completion: z.number(), | ||
| total: z.number(), | ||
| cached: z.number().optional(), | ||
| }); | ||
|
|
||
| export type AIGatewayWSUsage = z.infer<typeof AIGatewayWSUsageSchema>; | ||
|
|
||
| export const AIGatewayWSServerResponseSchema = z.looseObject({ | ||
| type: z.literal(AIGatewayWSFrameType.response), | ||
| id: z.string(), | ||
| compact: z.boolean().optional(), | ||
| status: z.string(), | ||
| status_code: z.number().optional(), | ||
| content: z.string().optional(), | ||
| delta: z.string().optional(), | ||
| thinking: z.string().optional(), | ||
| usage: AIGatewayWSUsageSchema.optional(), | ||
| cost: z.number().optional(), | ||
| unit: z.string().optional(), | ||
| input_qty: z.number().optional(), | ||
| output_qty: z.number().optional(), | ||
| event: z.string().optional(), | ||
| data: z.unknown().optional(), | ||
| }); | ||
|
|
||
| export type AIGatewayWSServerResponse = z.infer<typeof AIGatewayWSServerResponseSchema>; | ||
|
|
||
| export const AIGatewayWSServerErrorSchema = z.object({ | ||
| type: z.literal(AIGatewayWSFrameType.error), | ||
| id: z.string().optional(), | ||
| status_code: z.number(), | ||
| message: z.string(), | ||
| }); | ||
|
|
||
| export type AIGatewayWSServerError = z.infer<typeof AIGatewayWSServerErrorSchema>; | ||
|
|
||
| export const AIGatewayWSDrainingSchema = z.object({ | ||
| type: z.literal(AIGatewayWSFrameType.draining), | ||
| message: z.string().optional(), | ||
| }); | ||
|
|
||
| export type AIGatewayWSDraining = z.infer<typeof AIGatewayWSDrainingSchema>; | ||
|
|
||
| export type AIGatewayWSServerFrame = | ||
| | AIGatewayWSServerResponse | ||
| | AIGatewayWSServerError | ||
| | AIGatewayWSDraining; | ||
|
|
||
| export function parseAIGatewayWSServerFrame(raw: unknown): AIGatewayWSServerFrame | null { | ||
| if (!raw || typeof raw !== 'object') { | ||
| return null; | ||
| } | ||
| const frame = raw as Record<string, unknown>; | ||
| const type = frame.type; | ||
| if (type === AIGatewayWSFrameType.draining) { | ||
| const parsed = AIGatewayWSDrainingSchema.safeParse(raw); | ||
| return parsed.success ? parsed.data : null; | ||
| } | ||
| if (type === AIGatewayWSFrameType.error) { | ||
| const parsed = AIGatewayWSServerErrorSchema.safeParse(raw); | ||
| return parsed.success ? parsed.data : null; | ||
| } | ||
| if (type === AIGatewayWSFrameType.response) { | ||
| const parsed = AIGatewayWSServerResponseSchema.safeParse(raw); | ||
| return parsed.success ? parsed.data : null; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| export function buildAIGatewayWebSocketUrl(baseUrl: string): string { | ||
| const trimmed = baseUrl.trim().replace(/\/$/, ''); | ||
| const wsBase = trimmed.replace(/^https:\/\//, 'wss://').replace(/^http:\/\//, 'ws://'); | ||
| return `${wsBase}/v1/ws`; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.