Repository navigation
Expand file tree
/
Copy pathmodel.ts
More file actions
292 lines (291 loc) · 10.3 KB
/
Copy pathmodel.ts
File metadata and controls
292 lines (291 loc) · 10.3 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
import {
createAIModel,
modelConnectionError,
modelConnectionReason,
promptCacheOptions,
} from "./providers.js";
import type { DB } from "@db/index.js";
import { AppError, fail } from "@core/shared/errors.js";
import { requireInferenceModel } from "@core/modules/ai/config.js";
import {
hoistToolImages,
bindToolImageFrames,
transmittedToolImageFrames,
type ToolImageSelection,
type TransmittedToolImage,
} from "./tool-media.js";
import { modelPromptImages } from "./model-image.js";
import { beginCall, settleCall } from "@core/modules/ai/usage.js";
import {
MODEL_INPUT_BYTE_FACTOR,
fitPromptToModelInput,
promptPayloadBytes,
} from "./context-budget.js";
export function usageOf(usage: any) {
const raw = usage?.raw;
if (
raw?.prompt_tokens !== undefined &&
(!Number.isSafeInteger(raw.prompt_tokens) ||
!Number.isSafeInteger(raw.completion_tokens))
)
return null;
const input =
typeof usage?.inputTokens === "number"
? usage.inputTokens
: usage?.inputTokens?.total;
const output =
typeof usage?.outputTokens === "number"
? usage.outputTokens
: usage?.outputTokens?.total;
if (
!Number.isSafeInteger(input) ||
!Number.isSafeInteger(output) ||
input < 0 ||
output < 0
)
return null;
const cached =
raw?.prompt_cache_hit_tokens ??
raw?.prompt_tokens_details?.cached_tokens ??
raw?.input_tokens_details?.cached_tokens ??
raw?.cache_read_input_tokens ??
usage.inputTokens?.cacheRead ??
usage.cachedInputTokens ??
0;
if (!Number.isSafeInteger(cached) || cached < 0) return null;
return {
input,
output,
cached: Math.min(input, cached),
raw: usage,
};
}
export type ScopedModelPrompt = {
prompt: any[];
protectedPrefix: number;
};
export async function meteredModel(
db: DB,
userId: string,
modelId: string,
jobId: string | null,
fetcher?: typeof fetch,
beforeCall?: () => Promise<void>,
cacheKey?: string,
scopePrompt?: (prompt: any[]) => ScopedModelPrompt,
scopeTools?: (tools: any) => any,
preparedPrompt?: (
prompt: any[],
frames: TransmittedToolImage[],
) => { complete(): void; abort(): void },
normalizePromptImages: (prompt: any[]) => Promise<any[]> = modelPromptImages,
) {
const { model } = await requireInferenceModel(db, userId, modelId);
const base = createAIModel(model, fetcher);
return new Proxy(base, {
get(target, key) {
if (key !== "doGenerate" && key !== "doStream") {
const v = Reflect.get(target, key);
return typeof v === "function" ? v.bind(target) : v;
}
return async (options: any) => {
options = promptCacheOptions(model, options, cacheKey);
if (scopeTools)
options = { ...options, tools: scopeTools(options.tools) };
await beforeCall?.();
const current = await requireInferenceModel(db, userId, modelId);
if (
current.model.model !== model.model ||
current.model.baseUrl !== model.baseUrl ||
current.model.provider !== model.provider ||
current.model.apiMode !== model.apiMode ||
current.model.apiVersion !== model.apiVersion ||
current.model.apiKey !== model.apiKey ||
current.model.vision !== model.vision ||
current.model.pdf !== model.pdf ||
current.model.maxInput !== model.maxInput ||
current.model.maxOutput !== model.maxOutput
)
fail(409, "模型配置已变化,请重新发起任务", {
code: "model_config_changed",
});
let transmittedFrames: TransmittedToolImage[] = [];
if (Array.isArray(options.prompt)) {
const selected: ToolImageSelection[] = [];
const scoped = scopePrompt
? scopePrompt(options.prompt)
: { prompt: options.prompt, protectedPrefix: 0 };
const hoisted = hoistToolImages(scoped.prompt, 4, (frame) =>
selected.push(frame),
);
const normalized = normalizePromptImages === modelPromptImages
? await modelPromptImages(hoisted, new Set(selected
.filter(frame => frame.toolName === "image_mask_region_view" || frame.toolName === "image_candidate_region_view")
.map(frame => frame.part.data)))
: await normalizePromptImages(hoisted);
const bound = bindToolImageFrames(selected, normalized);
options = {
...options,
prompt: fitPromptToModelInput(
normalized,
model.maxInput,
options.tools,
scoped.protectedPrefix,
),
};
transmittedFrames = transmittedToolImageFrames(options.prompt, bound);
}
const bytes = promptPayloadBytes(options.prompt, options.tools);
if (bytes > model.maxInput * MODEL_INPUT_BYTE_FACTOR)
fail(413, "当前上下文过长,请减少引用或新建会话", {
code: "model_context_large",
});
// UTF-8 bytes bound common byte-tokenizers conservatively; reserve against enforced output limit.
const media = (Array.isArray(options.prompt) ? options.prompt : [])
.flatMap((m: any) => (Array.isArray(m.content) ? m.content : []))
.filter((p: any) => p.type === "file" || p.type === "image");
const mediaBudget = media.reduce(
(sum: number, p: any) =>
sum + (p.mediaType === "application/pdf" ? model.maxInput : 8192),
0,
);
const inputUpper = bytes + 512 + mediaBudget;
const maxOutput = Math.min(
options.maxOutputTokens ?? model.maxOutput,
model.maxOutput,
);
const call = await beginCall(
db,
userId,
modelId,
jobId,
inputUpper,
maxOutput,
);
let observation:
ReturnType<NonNullable<typeof preparedPrompt>> | undefined;
let failed = false;
const abortObservation = () => {
failed = true;
observation?.abort();
};
const confirm = (finishReason: any) => {
const reason =
typeof finishReason === "string"
? finishReason
: finishReason?.unified;
if (
failed ||
options.abortSignal?.aborted ||
!["stop", "tool-calls", "length"].includes(reason)
)
abortObservation();
else observation?.complete();
};
try {
// No private frame identifiers are added to options or provider JSON.
// Confirmation is committed only after a successful response finishes.
observation = preparedPrompt?.(options.prompt, transmittedFrames);
if (key === "doGenerate") {
const result = await target.doGenerate({
...options,
maxOutputTokens: maxOutput,
});
await settleCall(db, call.id, usageOf(result.usage));
confirm(result.finishReason);
return result;
}
const result = await target.doStream({
...options,
maxOutputTokens: maxOutput,
});
let ended = false;
let finishReason: any;
const held: any[] = [];
const reader = result.stream.getReader();
const stream = new ReadableStream({
async pull(controller) {
try {
// Mastra synthesizes a complete tool-call at tool-input-end.
// Retain either executable boundary and its tail until success.
while (true) {
const next = await reader.read();
if (next.done) {
if (!ended) {
abortObservation();
await settleCall(db, call.id, null);
}
if (observation && ended) {
confirm(finishReason);
for (const value of held)
if (!failed || value.type === "finish")
controller.enqueue(value);
}
controller.close();
return;
}
if (next.value.type === "error") {
abortObservation();
held.length = 0;
}
if (next.value.type === "finish") {
await settleCall(db, call.id, usageOf(next.value.usage));
ended = true;
finishReason = next.value.finishReason;
if (observation) {
held.push(next.value);
continue;
}
controller.enqueue(next.value);
return;
}
if (
observation &&
(held.length ||
next.value.type === "tool-input-end" ||
next.value.type === "tool-call")
) {
if (!failed) held.push(next.value);
continue;
}
controller.enqueue(next.value);
return;
}
} catch (e) {
abortObservation();
await settleCall(db, call.id, null);
controller.error(e);
}
},
async cancel(reason) {
abortObservation();
await reader.cancel(reason);
if (!ended) await settleCall(db, call.id, null);
},
});
return { ...result, stream };
} catch (e) {
abortObservation();
const status = (e as any)?.statusCode;
await settleCall(
db,
call.id,
[400, 401, 403, 404, 413, 422, 429].includes(status)
? { input: 0, output: 0 }
: null,
"failed",
);
// Our own guarded errors carry a safe message already; only
// provider/network failures need translation. Never forward provider
// bodies, request headers or real model IDs to a user.
if (e instanceof AppError) throw e;
fail(
[413, 429].includes(status) ? status : 502,
modelConnectionError(e),
modelConnectionReason(e),
);
}
};
},
});
}