llm.js
9.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import { randomUUID } from 'node:crypto'
import * as v from 'valibot'
import { LLMError } from './errors.js'
import { EMPTY_USAGE } from './base.js'
import { loadRegistry, registeredProviders } from './registry.js'
import { noopNotifyPort, noopTracePort } from './ports/no-op.js'
const NOTIFY_FAIL_THRESHOLD = 3
const KeyProviderShape = v.object({
acquire: v.function(),
report_usage: v.function(),
report_rate_limited: v.function(),
report_error: v.function(),
})
const ChatRequestSchema = v.object({
messages: v.pipe(
v.array(v.object({
role: v.picklist(['system', 'user', 'assistant', 'tool']),
content: v.any(),
prompt_cache_breakpoint: v.optional(v.boolean()),
})),
v.minLength(1),
),
model: v.optional(v.string()),
temperature: v.optional(v.number()),
max_tokens: v.optional(v.number()),
response_format: v.optional(v.any()),
tools: v.optional(v.array(v.any())),
thinking: v.optional(v.any()),
images: v.optional(v.array(v.any())),
videos: v.optional(v.array(v.any())),
prompt_cache_options: v.optional(v.strictObject({
mode: v.picklist(['implicit', 'explicit']),
ttl: v.optional(v.literal('30m')),
})),
})
/**
* provider 调用、供应商成本、调用日志、积分结算的统一编排入口。
* costPort/billingPort/auditPort 要么三者全部注入,要么全部省略用于运营探针。
*/
export function createLLM({
keyProvider,
provider = 'openai',
vendor = provider,
model,
costPort,
billingPort,
notifyPort,
tracePort,
auditPort,
nowMs,
newId,
retryPolicy,
}) {
assertKeyProvider(keyProvider, provider)
const accounting = accountingPorts({ costPort, billingPort, auditPort })
const notify = notifyPort ?? noopNotifyPort
const trace = tracePort ?? noopTracePort
const now = nowMs ?? Date.now
const genId = newId ?? randomUUID
const table = loadRegistry()
if (!table[provider]) {
throw new LLMError({
provider,
reason: 'badRequest',
retryable: false,
message: `createLLM: unknown provider '${provider}'. Registered providers: [${registeredProviders().join(', ')}]`,
})
}
let instance
let consecutiveFailures = 0
async function ensureInstance() {
if (instance) return instance
const module = await table[provider]()
instance = new module.default({ keyProvider, provider, vendor, model, tracePort: trace, retryPolicy, nowMs: now })
return instance
}
async function chat(req, opts = {}) {
const parsed = parseChatRequest(req, provider)
const impl = await ensureInstance()
const usedModel = parsed.model ?? model ?? ''
const requestId = genId()
const billingOperationId = opts.billing_ctx?.idempotency_key ?? requestId
const startedAt = now()
const sessionId = opts.trace_ctx?.session_id ?? null
let failureUsage = EMPTY_USAGE
let failureTtftMs = null
let keyId = null
const implOpts = { ...opts, on_key_lease: (id) => { keyId = id } }
let result
try {
result = await impl.chat(parsed, implOpts)
failureUsage = result.usage
failureTtftMs = result.timings?.ttft_ms ?? null
consecutiveFailures = 0
} catch (error) {
const llmError = normalizeLlmError(error, provider)
const { provider_request_id } = llmError
if (accounting) {
accounting.auditPort.record({
provider: vendor,
model: usedModel,
request_id: requestId,
billing_operation_id: billingOperationId,
session_id: sessionId,
user_id: opts.billing_ctx?.user_id ?? null,
key_id: keyId,
created_at: new Date(now()),
status: llmError.reason === 'aborted' ? 'aborted' : 'error',
ttft_ms: failureTtftMs,
duration_ms: now() - startedAt,
usage: failureUsage,
provider_request_id,
service_tier: null,
pricing_snapshot: null,
supplier_cost_micros: null,
currency: null,
billing_mode: null,
billing_policy_snapshot: null,
charged_points: null,
ledger_id: null,
error_reason: llmError.reason,
error_message: llmError.message,
error_detail: llmError.detail,
messages: parsed.messages,
response_text: null,
reasoning_text: null,
mode: opts.mode ?? null,
correlation_id: opts.correlation_id ?? null,
attempt_index: opts.attempt_index ?? null,
})
}
if (llmError.reason !== 'aborted') {
consecutiveFailures += 1
if (consecutiveFailures >= NOTIFY_FAIL_THRESHOLD) {
consecutiveFailures = 0
await notify.notify(
{ kind: 'llm.consecutiveFailures', provider, reason: llmError.reason },
opts.billing_ctx,
)
}
}
throw llmError
}
const {
content,
reasoning,
tool_calls,
usage,
provider_request_id,
service_tier,
timings = { ttft_ms: null, duration_ms: now() - startedAt },
} = result
const metered_usage = { kind: 'token', ...usage }
const out = {
content,
usage,
metered_usage,
provider: vendor,
model: usedModel,
timings,
request_id: requestId,
billing_operation_id: billingOperationId,
provider_request_id,
service_tier,
}
if (reasoning) out.reasoning = reasoning
if (tool_calls) out.tool_calls = tool_calls
if (!accounting) return out
const calculated = await accounting.costPort.calculate({ provider: vendor, model: usedModel, usage: metered_usage })
await accounting.auditPort.recordProviderSuccess({
provider: vendor,
model: usedModel,
request_id: requestId,
billing_operation_id: billingOperationId,
session_id: sessionId,
user_id: opts.billing_ctx?.user_id ?? null,
key_id: keyId,
created_at: new Date(now()),
status: 'ok',
...timings,
usage,
provider_request_id,
service_tier,
pricing_snapshot: calculated.pricing_snapshot,
supplier_cost_micros: calculated.supplier_cost_micros,
currency: calculated.currency,
billing_mode: null,
billing_policy_snapshot: null,
charged_points: null,
ledger_id: null,
error_reason: null,
error_message: null,
error_detail: null,
messages: parsed.messages,
response_text: content,
reasoning_text: reasoning ?? null,
mode: opts.mode ?? null,
correlation_id: opts.correlation_id ?? null,
attempt_index: opts.attempt_index ?? null,
})
const settlement = await accounting.billingPort.settle({
call_id: requestId,
user_id: opts.billing_ctx?.user_id,
call_ref: { type: 'llm_operation', id: billingOperationId },
supplier_cost: {
supplier_cost_micros: calculated.supplier_cost_micros,
currency: calculated.currency,
},
policy: opts.billing_ctx?.policy,
idempotency_key: billingOperationId,
})
out.accounting = {
call_ref: { type: 'llm_operation', id: billingOperationId },
usage: metered_usage,
pricing_snapshot: calculated.pricing_snapshot,
supplier_cost_micros: calculated.supplier_cost_micros,
currency: calculated.currency,
billing_mode: settlement.billing_mode,
billing_policy_snapshot: settlement.billing_policy_snapshot,
charged_points: settlement.charged_points,
ledger_id: settlement.ledger_id,
}
return out
}
return { chat }
}
function parseChatRequest(req, provider) {
try {
return v.parse(ChatRequestSchema, req)
} catch (cause) {
throw new LLMError({
provider,
reason: 'badRequest',
retryable: false,
cause,
message: 'createLLM.chat: invalid ChatRequest',
})
}
}
function assertKeyProvider(keyProvider, provider) {
if (!keyProvider) {
throw new LLMError({ provider, reason: 'badRequest', retryable: false, message: 'createLLM: keyProvider is required' })
}
try {
v.parse(KeyProviderShape, keyProvider)
} catch (cause) {
throw new LLMError({
provider,
reason: 'badRequest',
retryable: false,
cause,
message: 'createLLM: keyProvider does not satisfy the KeyProvider port shape',
})
}
}
function accountingPorts({ costPort, billingPort, auditPort }) {
const ports = [costPort, billingPort, auditPort]
const present = ports.filter(Boolean).length
if (present === 0) return null
if (present !== ports.length) {
throw new Error('createLLM: costPort、billingPort、auditPort 必须同时注入')
}
if (typeof costPort.calculate !== 'function') throw new Error('createLLM: costPort.calculate 必须是函数')
if (typeof billingPort.settle !== 'function') throw new Error('createLLM: billingPort.settle 必须是函数')
if (
typeof auditPort.record !== 'function' ||
typeof auditPort.recordProviderSuccess !== 'function'
) {
throw new Error('createLLM: auditPort 缺少 record/recordProviderSuccess')
}
return { costPort, billingPort, auditPort }
}
function normalizeLlmError(error, provider) {
if (error instanceof LLMError) return error
if (!(error instanceof Error)) throw new Error(`[${provider}] provider 抛出了非 Error 值`)
return new LLMError({
provider,
reason: 'unknown',
retryable: false,
cause: error,
detail: { type: 'runtime', name: error.name, message: error.message },
})
}
export function createStaticKeyProvider({ api_key, base_url }) {
if (!api_key) {
throw new LLMError({
provider: 'static',
reason: 'badRequest',
retryable: false,
message: 'createStaticKeyProvider: api_key is required',
})
}
return {
async acquire() {
return { api_key, base_url, account_id: null, key_id: null, release() {} }
},
report_usage() {},
report_rate_limited() {},
report_error() {},
}
}