feat: stream chat and settle metered CNY usage atomically
This commit is contained in:
1 parent
7360cf3ebe
commit
b46ee72f2d
28 files changed
+1098
-86
No files matched your search
@@ -28,7 +28,7 @@ export interface CatalogModel {
|
||||
inputTypes: string[]
|
||||
outputTypes: string[]
|
||||
capabilities: Record<string, unknown>
|
||||
pricing?: { input?: number; output?: number; unit?: string }
|
||||
pricing?: { input?: number; output?: number; cachedInput?: number; unit?: string }
|
||||
priority?: number
|
||||
maxConcurrency?: number
|
||||
status: 'active' | 'disabled'
|
||||
@@ -51,7 +51,7 @@ export interface WalletEntry {
|
||||
amount: number
|
||||
balanceAfter: number
|
||||
balanceBefore: number
|
||||
operation: 'increase' | 'decrease' | 'set'
|
||||
operation: 'increase' | 'decrease' | 'set' | 'usage'
|
||||
currency: 'CNY'
|
||||
reason: string
|
||||
createdAt: string
|
||||
@@ -92,6 +92,9 @@ export interface PlatformOverview {
|
||||
callsThisMonth: number
|
||||
averageLatencyMs?: number
|
||||
availableCredit?: number
|
||||
balance?: number
|
||||
reservedCredit?: number
|
||||
costThisMonth?: number
|
||||
availabilityPercent?: number
|
||||
asyncTasks: number
|
||||
asyncSuccessRate?: number
|
||||
@@ -287,14 +290,29 @@ class HttpCatalogClient {
|
||||
return response.blob()
|
||||
}
|
||||
|
||||
async chatCompletion(apiKey: string, input: { model: string; prompt: string }) {
|
||||
async chatCompletion(apiKey: string, input: { model: string; prompt: string; maxTokens?: number }) {
|
||||
return this.request<ChatCompletionResult>('/v1/chat/completions', {
|
||||
method: 'POST',
|
||||
headers: { Authorization: `Bearer ${apiKey}`, 'X-Moyi-Source': 'playground' },
|
||||
body: JSON.stringify({ model: input.model, messages: [{ role: 'user', content: input.prompt }] }),
|
||||
headers: { Authorization: `Bearer ${apiKey}`, 'X-Moyi-Source': 'playground', 'Idempotency-Key': crypto.randomUUID() },
|
||||
body: JSON.stringify({ model: input.model, messages: [{ role: 'user', content: input.prompt }], max_tokens: input.maxTokens || 4096 }),
|
||||
})
|
||||
}
|
||||
|
||||
async streamChat(apiKey: string, input: { model: string; prompt: string; maxTokens: number }, onDelta: (content: string, reasoning: string) => void, signal: AbortSignal) {
|
||||
const response = await fetch(`${this.baseUrl}/v1/chat/completions`, {
|
||||
method: 'POST', credentials: 'include', signal,
|
||||
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${apiKey}`, 'X-Moyi-Source': 'playground', 'Idempotency-Key': crypto.randomUUID() },
|
||||
body: JSON.stringify({ model: input.model, messages: [{ role: 'user', content: input.prompt }], stream: true, max_tokens: input.maxTokens }),
|
||||
})
|
||||
if (!response.ok) {
|
||||
const error = await response.json().catch(() => ({}))
|
||||
throw new Error(error.error?.message || `调用失败 (${response.status})`)
|
||||
}
|
||||
if (!response.body || !response.headers.get('content-type')?.includes('text/event-stream')) throw new Error('服务未返回流式响应')
|
||||
const { readChatStream } = await import('./chat-stream')
|
||||
await readChatStream(response.body, onDelta)
|
||||
}
|
||||
|
||||
async createMediaTask(apiKey: string, input: { model: string; prompt: string; mediaType: '3d' | 'video' }) {
|
||||
const payload = await this.request<{ task: AsyncTaskResult }>(input.mediaType === 'video' ? '/v1/video/tasks' : '/v1/3d/tasks', {
|
||||
method: 'POST',
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
/** Parse incrementally: a UTF-8 character or SSE frame may span several network chunks. */
|
||||
export async function readChatStream(body: ReadableStream<Uint8Array>, onDelta: (content: string, reasoning: string) => void) {
|
||||
const reader = body.getReader()
|
||||
const decoder = new TextDecoder()
|
||||
let buffer = '', data: string[] = [], done = false
|
||||
const line = (value: string) => {
|
||||
if (value.endsWith('\r')) value = value.slice(0, -1)
|
||||
if (value === '') {
|
||||
if (!data.length) return
|
||||
const frame = data.join('\n'); data = []
|
||||
if (frame === '[DONE]') { done = true; return }
|
||||
const event = JSON.parse(frame)
|
||||
if (event.error) throw new Error(event.error.message || '流式调用失败')
|
||||
const delta = event.choices?.[0]?.delta
|
||||
if (delta) onDelta(typeof delta.content === 'string' ? delta.content : '', typeof delta.reasoning_content === 'string' ? delta.reasoning_content : '')
|
||||
} else if (value.startsWith('data:')) data.push(value.slice(5).replace(/^ /, ''))
|
||||
}
|
||||
try {
|
||||
while (!done) {
|
||||
const part = await reader.read()
|
||||
buffer += decoder.decode(part.value, { stream: !part.done })
|
||||
let pos: number
|
||||
while ((pos = buffer.indexOf('\n')) >= 0) {
|
||||
line(buffer.slice(0, pos)); buffer = buffer.slice(pos + 1)
|
||||
if (done) break
|
||||
}
|
||||
if (part.done) break
|
||||
if (buffer.length > 262144 || data.join('\n').length > 262144) throw new Error('流式响应超过限制')
|
||||
}
|
||||
if (!done) throw new Error('连接提前结束,请到使用记录中确认调用与扣费状态')
|
||||
} finally {
|
||||
await reader.cancel().catch(() => {})
|
||||
reader.releaseLock()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user