feat: establish gateway adapter baseline
This commit is contained in:
1 parent
bebff16a2d
commit
06cfbbacc3
4 files changed
+308
-3
No files matched your search
@@ -0,0 +1,160 @@
|
||||
export type GatewayStatus = 'healthy' | 'degraded' | 'unavailable'
|
||||
|
||||
export interface GatewayHealth {
|
||||
status: GatewayStatus
|
||||
checkedAt: string
|
||||
source: 'demo' | 'otari'
|
||||
}
|
||||
|
||||
export interface GatewayModel {
|
||||
id: string
|
||||
provider?: string
|
||||
ownedBy?: string
|
||||
}
|
||||
|
||||
export interface GatewayUsageQuery {
|
||||
from?: string
|
||||
to?: string
|
||||
keyId?: string
|
||||
model?: string
|
||||
}
|
||||
|
||||
export interface GatewayUsageRecord {
|
||||
requestId: string
|
||||
model: string
|
||||
inputUnits: number
|
||||
outputUnits: number
|
||||
cost?: number
|
||||
createdAt: string
|
||||
}
|
||||
|
||||
export interface GatewayPage<T> {
|
||||
data: T[]
|
||||
nextCursor?: string
|
||||
}
|
||||
|
||||
export interface GatewayClient {
|
||||
health(): Promise<GatewayHealth>
|
||||
listModels(): Promise<GatewayModel[]>
|
||||
listUsage(query?: GatewayUsageQuery): Promise<GatewayPage<GatewayUsageRecord>>
|
||||
}
|
||||
|
||||
export interface OtariGatewayConfig {
|
||||
baseUrl: string
|
||||
accessToken: string
|
||||
timeoutMs?: number
|
||||
}
|
||||
|
||||
interface OtariModelResponse {
|
||||
data?: Array<{
|
||||
id: string
|
||||
provider?: string
|
||||
owned_by?: string
|
||||
}>
|
||||
}
|
||||
|
||||
interface OtariUsageResponse {
|
||||
data?: Array<{
|
||||
request_id?: string
|
||||
id?: string
|
||||
model?: string
|
||||
input_tokens?: number
|
||||
output_tokens?: number
|
||||
cost?: number
|
||||
created_at?: string
|
||||
}>
|
||||
next_cursor?: string
|
||||
}
|
||||
|
||||
export class OtariGatewayClient implements GatewayClient {
|
||||
private readonly baseUrl: string
|
||||
private readonly accessToken: string
|
||||
private readonly timeoutMs: number
|
||||
|
||||
constructor(config: OtariGatewayConfig) {
|
||||
this.baseUrl = config.baseUrl.replace(/\/$/, '')
|
||||
this.accessToken = config.accessToken
|
||||
this.timeoutMs = config.timeoutMs ?? 8000
|
||||
}
|
||||
|
||||
async health(): Promise<GatewayHealth> {
|
||||
const response = await this.request<{ status?: string }>('/health', false)
|
||||
return {
|
||||
status: response.status === 'healthy' ? 'healthy' : 'degraded',
|
||||
checkedAt: new Date().toISOString(),
|
||||
source: 'otari',
|
||||
}
|
||||
}
|
||||
|
||||
async listModels(): Promise<GatewayModel[]> {
|
||||
const response = await this.request<OtariModelResponse>('/v1/models')
|
||||
return (response.data ?? []).map((model) => ({
|
||||
id: model.id,
|
||||
provider: model.provider,
|
||||
ownedBy: model.owned_by,
|
||||
}))
|
||||
}
|
||||
|
||||
async listUsage(query: GatewayUsageQuery = {}): Promise<GatewayPage<GatewayUsageRecord>> {
|
||||
const search = new URLSearchParams()
|
||||
if (query.from) search.set('from', query.from)
|
||||
if (query.to) search.set('to', query.to)
|
||||
if (query.keyId) search.set('key_id', query.keyId)
|
||||
if (query.model) search.set('model', query.model)
|
||||
const suffix = search.size ? `?${search.toString()}` : ''
|
||||
const response = await this.request<OtariUsageResponse>(`/v1/usage${suffix}`)
|
||||
return {
|
||||
data: (response.data ?? []).map((record) => ({
|
||||
requestId: record.request_id ?? record.id ?? 'unknown',
|
||||
model: record.model ?? 'unknown',
|
||||
inputUnits: record.input_tokens ?? 0,
|
||||
outputUnits: record.output_tokens ?? 0,
|
||||
cost: record.cost,
|
||||
createdAt: record.created_at ?? new Date(0).toISOString(),
|
||||
})),
|
||||
nextCursor: response.next_cursor,
|
||||
}
|
||||
}
|
||||
|
||||
private async request<T>(path: string, authenticated = true): Promise<T> {
|
||||
const controller = new AbortController()
|
||||
const timeout = window.setTimeout(() => controller.abort(), this.timeoutMs)
|
||||
|
||||
try {
|
||||
const headers = new Headers({ Accept: 'application/json' })
|
||||
if (authenticated) headers.set('Authorization', `Bearer ${this.accessToken}`)
|
||||
const response = await fetch(`${this.baseUrl}${path}`, {
|
||||
headers,
|
||||
signal: controller.signal,
|
||||
})
|
||||
if (!response.ok) throw new Error(`Gateway request failed with status ${response.status}`)
|
||||
return await response.json() as T
|
||||
} finally {
|
||||
window.clearTimeout(timeout)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class DemoGatewayClient implements GatewayClient {
|
||||
async health(): Promise<GatewayHealth> {
|
||||
return {
|
||||
status: 'healthy',
|
||||
checkedAt: new Date().toISOString(),
|
||||
source: 'demo',
|
||||
}
|
||||
}
|
||||
|
||||
async listModels(): Promise<GatewayModel[]> {
|
||||
return [
|
||||
{ id: 'moyi-text-pro', provider: '供应商 A', ownedBy: 'moyi' },
|
||||
{ id: 'moyi-vision', provider: '供应商 A', ownedBy: 'moyi' },
|
||||
{ id: 'aisoulmate-3d', provider: '模灵科技', ownedBy: 'moyi' },
|
||||
]
|
||||
}
|
||||
|
||||
async listUsage(): Promise<GatewayPage<GatewayUsageRecord>> {
|
||||
return { data: [] }
|
||||
}
|
||||
}
|
||||
|
||||
export const gatewayClient: GatewayClient = new DemoGatewayClient()
|
||||
Reference in new issue
Block a user