veryfront 0.1.903 → 0.1.905
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/deno.js +1 -1
- package/esm/src/platform/adapters/veryfront-api-client/operations.js +2 -2
- package/esm/src/provider/types.d.ts +1 -0
- package/esm/src/provider/types.d.ts.map +1 -1
- package/esm/src/provider/veryfront-cloud/provider.d.ts.map +1 -1
- package/esm/src/provider/veryfront-cloud/provider.js +11 -8
- package/esm/src/runtime/runtime-bridge.d.ts.map +1 -1
- package/esm/src/runtime/runtime-bridge.js +157 -44
- package/esm/src/server/handlers/request/api/api-handler-wrapper.d.ts.map +1 -1
- package/esm/src/server/handlers/request/api/api-handler-wrapper.js +2 -0
- package/esm/src/utils/version-constant.d.ts +1 -1
- package/esm/src/utils/version-constant.js +1 -1
- package/package.json +1 -1
package/esm/deno.js
CHANGED
|
@@ -117,7 +117,7 @@ export class VeryfrontAPIOperations {
|
|
|
117
117
|
return getProjectSchema().parse(raw);
|
|
118
118
|
}
|
|
119
119
|
async listBranchFiles(projectRef, branchRef = "main", options = {}) {
|
|
120
|
-
const params = buildListParams(options);
|
|
120
|
+
const params = addRuntimeServerFunctionAccess(buildListParams(options));
|
|
121
121
|
params.set("branch", branchRef);
|
|
122
122
|
const url = `/projects/${encodeURIComponent(projectRef)}/files?${params}`;
|
|
123
123
|
logger.debug("listBranchFiles", { projectRef, branchRef, pattern: options.pattern });
|
|
@@ -143,7 +143,7 @@ export class VeryfrontAPIOperations {
|
|
|
143
143
|
}
|
|
144
144
|
getBranchFile(projectRef, branchRef, pathOrId) {
|
|
145
145
|
return withSpan(SpanNames.API_GET_FILE, async () => {
|
|
146
|
-
const params = new URLSearchParams({ branch: branchRef });
|
|
146
|
+
const params = addRuntimeServerFunctionAccess(new URLSearchParams({ branch: branchRef }));
|
|
147
147
|
const url = `/projects/${encodeURIComponent(projectRef)}/files/${encodeURIComponent(pathOrId)}?${params}`;
|
|
148
148
|
logger.debug("getBranchFile", { projectRef, branchRef, pathOrId });
|
|
149
149
|
const raw = await this.request(url);
|
|
@@ -17,6 +17,7 @@ export interface ModelRuntimeStreamResult {
|
|
|
17
17
|
/** Public API contract for model runtime. */
|
|
18
18
|
export interface ModelRuntime extends RuntimeMetadata {
|
|
19
19
|
readonly _isVfLocalModel?: boolean;
|
|
20
|
+
readonly _generateViaStream?: boolean;
|
|
20
21
|
doGenerate(options: unknown): PromiseLike<ModelRuntimeGenerateResult>;
|
|
21
22
|
doStream(options: unknown): PromiseLike<ModelRuntimeStreamResult>;
|
|
22
23
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/src/provider/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,oBAAoB,CAAC,EAAE,MAAM,CAAC;IACvC,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAC1B,QAAQ,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACjC;AAED,MAAM,WAAW,0BAA0B;IACzC,OAAO,CAAC,EAAE,OAAO,EAAE,CAAC;IACpB,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,QAAQ,CAAC,EAAE,OAAO,EAAE,CAAC;CACtB;AAED,MAAM,WAAW,wBAAwB;IACvC,MAAM,EAAE,cAAc,CAAC,OAAO,CAAC,CAAC;IAChC,QAAQ,CAAC,EAAE,OAAO,EAAE,CAAC;CACtB;AAED,6CAA6C;AAC7C,MAAM,WAAW,YAAa,SAAQ,eAAe;IACnD,QAAQ,CAAC,eAAe,CAAC,EAAE,OAAO,CAAC;IACnC,UAAU,CAAC,OAAO,EAAE,OAAO,GAAG,WAAW,CAAC,0BAA0B,CAAC,CAAC;IACtE,QAAQ,CAAC,OAAO,EAAE,OAAO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AAE,CAAC;CACtB;AAED,6CAA6C;AAC7C,MAAM,WAAW,YAAa,SAAQ,eAAe;IACnD,QAAQ,CAAC,eAAe,CAAC,EAAE,OAAO,CAAC;IACnC,QAAQ,CAAC,kBAAkB,CAAC,EAAE,OAAO,CAAC;IACtC,UAAU,CAAC,OAAO,EAAE,OAAO,GAAG,WAAW,CAAC,0BAA0B,CAAC,CAAC;IACtE,QAAQ,CAAC,OAAO,EAAE,OAAO,GAAG,WAAW,CAAC,wBAAwB,CAAC,CAAC;CACnE;AAED,MAAM,WAAW,gBAAiB,SAAQ,eAAe;IACvD,QAAQ,CAAC,oBAAoB,CAAC,EAAE,MAAM,GAAG,WAAW,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC;IACzE,QAAQ,CAAC,qBAAqB,CAAC,EAAE,OAAO,GAAG,WAAW,CAAC,OAAO,GAAG,SAAS,CAAC,CAAC;IAC5E,OAAO,CAAC,OAAO,EAAE;QACf,MAAM,EAAE,MAAM,EAAE,CAAC;QACjB,WAAW,CAAC,EAAE,WAAW,CAAC;KAC3B,GAAG,WAAW,CAAC;QACd,UAAU,EAAE,MAAM,EAAE,EAAE,CAAC;QACvB,KAAK,CAAC,EAAE;YACN,MAAM,CAAC,EAAE,MAAM,CAAC;SACjB,CAAC;QACF,WAAW,CAAC,EAAE,OAAO,CAAC;QACtB,QAAQ,CAAC,EAAE,OAAO,EAAE,CAAC;KACtB,CAAC,CAAC;CACJ"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../../../../src/src/provider/veryfront-cloud/provider.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../../../../src/src/provider/veryfront-cloud/provider.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAahD,wBAAgB,yBAAyB,CAAC,OAAO,EAAE,MAAM,GAAG,YAAY,CAuEvE"}
|
|
@@ -2,6 +2,9 @@ import { createError, toError } from "../../errors/veryfront-error.js";
|
|
|
2
2
|
import { ensureBuiltinLLMProviders } from "../../extensions/builtin-extensions.js";
|
|
3
3
|
import { createVeryfrontCloudFetch, getVeryfrontCloudGatewayBaseUrl, parseVeryfrontCloudModelId, requireVeryfrontCloudBootstrap, } from "./shared.js";
|
|
4
4
|
import { createVeryfrontCloudOpenAIModel } from "./openai.js";
|
|
5
|
+
function preferStreamedGenerate(model) {
|
|
6
|
+
return Object.assign(model, { _generateViaStream: true });
|
|
7
|
+
}
|
|
5
8
|
export function createVeryfrontCloudModel(modelId) {
|
|
6
9
|
const { provider, modelId: upstreamModelId } = parseVeryfrontCloudModelId(modelId, "language");
|
|
7
10
|
const { apiBaseUrl, apiToken, projectSlug } = requireVeryfrontCloudBootstrap();
|
|
@@ -12,25 +15,25 @@ export function createVeryfrontCloudModel(modelId) {
|
|
|
12
15
|
case "anthropic": {
|
|
13
16
|
const anthropic = registry.get("anthropic");
|
|
14
17
|
if (anthropic) {
|
|
15
|
-
return anthropic.createModel(upstreamModelId, {
|
|
18
|
+
return preferStreamedGenerate(anthropic.createModel(upstreamModelId, {
|
|
16
19
|
credential: apiToken,
|
|
17
20
|
authToken: apiToken,
|
|
18
21
|
baseURL,
|
|
19
22
|
name: "veryfront-cloud",
|
|
20
23
|
fetch,
|
|
21
|
-
});
|
|
24
|
+
}));
|
|
22
25
|
}
|
|
23
26
|
break;
|
|
24
27
|
}
|
|
25
28
|
case "google": {
|
|
26
29
|
const google = registry.get("google");
|
|
27
30
|
if (google) {
|
|
28
|
-
return google.createModel(upstreamModelId, {
|
|
31
|
+
return preferStreamedGenerate(google.createModel(upstreamModelId, {
|
|
29
32
|
credential: apiToken,
|
|
30
33
|
baseURL,
|
|
31
34
|
name: "veryfront-cloud",
|
|
32
35
|
fetch,
|
|
33
|
-
});
|
|
36
|
+
}));
|
|
34
37
|
}
|
|
35
38
|
break;
|
|
36
39
|
}
|
|
@@ -39,18 +42,18 @@ export function createVeryfrontCloudModel(modelId) {
|
|
|
39
42
|
case "moonshotai": {
|
|
40
43
|
const openai = registry.get("openai");
|
|
41
44
|
if (openai) {
|
|
42
|
-
return openai.createModel(upstreamModelId, {
|
|
45
|
+
return preferStreamedGenerate(openai.createModel(upstreamModelId, {
|
|
43
46
|
credential: apiToken,
|
|
44
47
|
baseURL,
|
|
45
48
|
name: "veryfront-cloud",
|
|
46
49
|
fetch,
|
|
47
|
-
});
|
|
50
|
+
}));
|
|
48
51
|
}
|
|
49
|
-
return createVeryfrontCloudOpenAIModel(upstreamModelId, {
|
|
52
|
+
return preferStreamedGenerate(createVeryfrontCloudOpenAIModel(upstreamModelId, {
|
|
50
53
|
apiToken,
|
|
51
54
|
baseURL,
|
|
52
55
|
fetch,
|
|
53
|
-
});
|
|
56
|
+
}));
|
|
54
57
|
}
|
|
55
58
|
default: {
|
|
56
59
|
const _exhaustive = provider;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runtime-bridge.d.ts","sourceRoot":"","sources":["../../../src/src/runtime/runtime-bridge.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,KAAK,EAAE,4BAA4B,EAAE,MAAM,2DAA2D,CAAC;AAC9G,OAAO,KAAK,EACV,yBAAyB,
|
|
1
|
+
{"version":3,"file":"runtime-bridge.d.ts","sourceRoot":"","sources":["../../../src/src/runtime/runtime-bridge.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,KAAK,EAAE,4BAA4B,EAAE,MAAM,2DAA2D,CAAC;AAC9G,OAAO,KAAK,EACV,yBAAyB,EAEzB,mBAAmB,EACnB,6BAA6B,EAC7B,cAAc,EACf,MAAM,wCAAwC,CAAC;AAChD,OAAO,KAAK,EACV,gBAAgB,EAChB,YAAY,EAEb,MAAM,sBAAsB,CAAC;AAE9B,KAAK,mBAAmB,GAAG;IACzB,K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CAAC;IACtB,eAAe,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC1C,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B,WAAW,CAAC,EAAE,WAAW,CAAC;CAC3B,CAAC;AAEF,KAAK,YAAY,GAAG;IAClB,KAAK,EAAE,gBAAgB,CAAC;IACxB,KAAK,EAAE,MAAM,CAAC;IACd,WAAW,CAAC,EAAE,WAAW,CAAC;CAC3B,CAAC;AAEF,KAAK,gBAAgB,GAAG;IACtB,KAAK,EAAE,gBAAgB,CAAC;IACxB,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,WAAW,CAAC,EAAE,WAAW,CAAC;CAC3B,CAAC;AAuqBF,wBAAgB,YAAY,CAAC,OAAO,EAAE,mBAAmB,GAAG,WAAW,CAAC,yBAAyB,CAAC,CAWjG;AAED,wBAAgB,UAAU,CAAC,OAAO,EAAE,iBAAiB,GAAG,mBAAmB,CAgB1E;AAED,wBAAgB,KAAK,CAAC,OAAO,EAAE,YAAY;;;;;;;;GAW1C;AAED,wBAAgB,SAAS,CAAC,OAAO,EAAE,gBAAgB;;;;;;;GAUlD;AACD,6DAA6D;AAC7D,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,MAAM,EAAE,EAAE,CAAC,EAAE,MAAM,EAAE,GAAG,MAAM,CAsBjE"}
|
|
@@ -131,6 +131,9 @@ function normalizeFinishReason(finishReason) {
|
|
|
131
131
|
}
|
|
132
132
|
return null;
|
|
133
133
|
}
|
|
134
|
+
function shouldGenerateViaStream(model) {
|
|
135
|
+
return model._generateViaStream === true;
|
|
136
|
+
}
|
|
134
137
|
function parseToolCallInput(input) {
|
|
135
138
|
if (typeof input !== "string") {
|
|
136
139
|
return input;
|
|
@@ -192,6 +195,29 @@ async function resolveDirectTools(tools) {
|
|
|
192
195
|
}
|
|
193
196
|
return resolvedTools.length > 0 ? resolvedTools : undefined;
|
|
194
197
|
}
|
|
198
|
+
function buildDirectModelOptions(options, tools) {
|
|
199
|
+
return {
|
|
200
|
+
prompt: toRuntimePrompt(normalizeSystemPrompt(options.system), getProviderRequestMessages(options.messages)),
|
|
201
|
+
maxOutputTokens: options.maxOutputTokens,
|
|
202
|
+
...(options.temperature === undefined ? {} : { temperature: options.temperature }),
|
|
203
|
+
topP: options.topP,
|
|
204
|
+
topK: options.topK,
|
|
205
|
+
stopSequences: options.stopSequences,
|
|
206
|
+
...(tools ? { tools } : {}),
|
|
207
|
+
...(options.toolChoice ? { toolChoice: options.toolChoice } : {}),
|
|
208
|
+
...(options.seed !== undefined ? { seed: options.seed } : {}),
|
|
209
|
+
...(options.presencePenalty !== undefined ? { presencePenalty: options.presencePenalty } : {}),
|
|
210
|
+
...(options.frequencyPenalty !== undefined
|
|
211
|
+
? { frequencyPenalty: options.frequencyPenalty }
|
|
212
|
+
: {}),
|
|
213
|
+
...(options.headers ? { headers: options.headers } : {}),
|
|
214
|
+
...(options.providerOptions ? { providerOptions: options.providerOptions } : {}),
|
|
215
|
+
...("includeRawChunks" in options && options.includeRawChunks !== undefined
|
|
216
|
+
? { includeRawChunks: options.includeRawChunks }
|
|
217
|
+
: {}),
|
|
218
|
+
abortSignal: options.abortSignal,
|
|
219
|
+
};
|
|
220
|
+
}
|
|
195
221
|
function isDirectToolCallPart(part) {
|
|
196
222
|
return !!part &&
|
|
197
223
|
typeof part === "object" &&
|
|
@@ -254,6 +280,128 @@ function buildDirectGenerateResult(result) {
|
|
|
254
280
|
finishReason: normalizeFinishReason(result.finishReason),
|
|
255
281
|
};
|
|
256
282
|
}
|
|
283
|
+
function streamUsageToGenerateUsage(totalUsage) {
|
|
284
|
+
if (!totalUsage) {
|
|
285
|
+
return undefined;
|
|
286
|
+
}
|
|
287
|
+
const inputTokens = totalUsage.inputTokens;
|
|
288
|
+
const outputTokens = totalUsage.outputTokens;
|
|
289
|
+
const totalTokens = totalUsage.totalTokens ??
|
|
290
|
+
(inputTokens !== undefined || outputTokens !== undefined
|
|
291
|
+
? (inputTokens ?? 0) + (outputTokens ?? 0)
|
|
292
|
+
: undefined);
|
|
293
|
+
return {
|
|
294
|
+
inputTokens,
|
|
295
|
+
outputTokens,
|
|
296
|
+
...(totalTokens !== undefined ? { totalTokens } : {}),
|
|
297
|
+
...(totalUsage.cacheCreationInputTokens !== undefined
|
|
298
|
+
? { cacheCreationInputTokens: totalUsage.cacheCreationInputTokens }
|
|
299
|
+
: {}),
|
|
300
|
+
...(totalUsage.cacheReadInputTokens !== undefined
|
|
301
|
+
? { cacheReadInputTokens: totalUsage.cacheReadInputTokens }
|
|
302
|
+
: {}),
|
|
303
|
+
...(totalUsage.cachedInputTokens !== undefined
|
|
304
|
+
? { cachedInputTokens: totalUsage.cachedInputTokens }
|
|
305
|
+
: {}),
|
|
306
|
+
...(totalUsage.reasoningTokens !== undefined
|
|
307
|
+
? { reasoningTokens: totalUsage.reasoningTokens }
|
|
308
|
+
: {}),
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
async function buildGenerateResultFromStream(stream) {
|
|
312
|
+
let text = "";
|
|
313
|
+
let usage;
|
|
314
|
+
let finishReason = null;
|
|
315
|
+
const toolCalls = new Map();
|
|
316
|
+
const toolInputs = new Map();
|
|
317
|
+
const toolResults = [];
|
|
318
|
+
for await (const rawPart of mapReadableStream(stream)) {
|
|
319
|
+
if (!rawPart || typeof rawPart !== "object" || !("type" in rawPart)) {
|
|
320
|
+
continue;
|
|
321
|
+
}
|
|
322
|
+
const part = rawPart;
|
|
323
|
+
switch (part.type) {
|
|
324
|
+
case "text-delta":
|
|
325
|
+
text += part.text;
|
|
326
|
+
break;
|
|
327
|
+
case "tool-input-start":
|
|
328
|
+
toolInputs.set(part.id, {
|
|
329
|
+
toolCallId: part.id,
|
|
330
|
+
toolName: part.toolName,
|
|
331
|
+
input: "",
|
|
332
|
+
});
|
|
333
|
+
break;
|
|
334
|
+
case "tool-input-delta": {
|
|
335
|
+
const input = toolInputs.get(part.id);
|
|
336
|
+
if (input) {
|
|
337
|
+
input.input += part.delta;
|
|
338
|
+
}
|
|
339
|
+
break;
|
|
340
|
+
}
|
|
341
|
+
case "tool-input-end": {
|
|
342
|
+
const input = toolInputs.get(part.id);
|
|
343
|
+
if (input) {
|
|
344
|
+
toolCalls.set(input.toolCallId, {
|
|
345
|
+
toolCallId: input.toolCallId,
|
|
346
|
+
toolName: input.toolName,
|
|
347
|
+
input: parseToolCallInput(input.input),
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
break;
|
|
351
|
+
}
|
|
352
|
+
case "tool-input-available": {
|
|
353
|
+
const toolCallId = part.toolCallId ?? part.id;
|
|
354
|
+
if (toolCallId) {
|
|
355
|
+
toolCalls.set(toolCallId, {
|
|
356
|
+
toolCallId,
|
|
357
|
+
toolName: part.toolName,
|
|
358
|
+
input: parseToolCallInput(part.input),
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
break;
|
|
362
|
+
}
|
|
363
|
+
case "tool-call":
|
|
364
|
+
toolCalls.set(part.toolCallId, {
|
|
365
|
+
toolCallId: part.toolCallId,
|
|
366
|
+
toolName: part.toolName,
|
|
367
|
+
input: parseToolCallInput(part.input),
|
|
368
|
+
});
|
|
369
|
+
break;
|
|
370
|
+
case "tool-result": {
|
|
371
|
+
const result = part.result ?? part.output ?? part.error;
|
|
372
|
+
toolResults.push({
|
|
373
|
+
toolCallId: part.toolCallId,
|
|
374
|
+
toolName: part.toolName,
|
|
375
|
+
result,
|
|
376
|
+
...(part.isError === true || part.error !== undefined ? { isError: true } : {}),
|
|
377
|
+
...(part.providerExecuted === true ? { providerExecuted: true } : {}),
|
|
378
|
+
});
|
|
379
|
+
break;
|
|
380
|
+
}
|
|
381
|
+
case "tool-error":
|
|
382
|
+
toolResults.push({
|
|
383
|
+
toolCallId: part.toolCallId,
|
|
384
|
+
toolName: part.toolName,
|
|
385
|
+
result: part.error,
|
|
386
|
+
isError: true,
|
|
387
|
+
...(part.providerExecuted === true ? { providerExecuted: true } : {}),
|
|
388
|
+
});
|
|
389
|
+
break;
|
|
390
|
+
case "finish":
|
|
391
|
+
finishReason = part.finishReason ?? null;
|
|
392
|
+
usage = streamUsageToGenerateUsage(part.totalUsage);
|
|
393
|
+
break;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
const finalToolCalls = [...toolCalls.values()];
|
|
397
|
+
return {
|
|
398
|
+
text,
|
|
399
|
+
...(finalToolCalls.length > 0 ? { toolCalls: finalToolCalls } : {}),
|
|
400
|
+
...(toolResults.length > 0 ? { toolResults } : {}),
|
|
401
|
+
usage,
|
|
402
|
+
finishReason,
|
|
403
|
+
};
|
|
404
|
+
}
|
|
257
405
|
function normalizeStreamPart(part) {
|
|
258
406
|
if (!part || typeof part !== "object" || !("type" in part)) {
|
|
259
407
|
return part;
|
|
@@ -271,7 +419,7 @@ function normalizeStreamPart(part) {
|
|
|
271
419
|
return part;
|
|
272
420
|
}
|
|
273
421
|
const finishPart = part;
|
|
274
|
-
const usage = normalizeUsage(finishPart.usage);
|
|
422
|
+
const usage = normalizeUsage(finishPart.usage) ?? normalizeUsage(finishPart.totalUsage);
|
|
275
423
|
const recomputedTotal = usage ? (usage.inputTokens ?? 0) + (usage.outputTokens ?? 0) : undefined;
|
|
276
424
|
return {
|
|
277
425
|
type: "finish",
|
|
@@ -321,51 +469,16 @@ async function* textDeltasFromStream(stream) {
|
|
|
321
469
|
}
|
|
322
470
|
}
|
|
323
471
|
export function generateText(options) {
|
|
324
|
-
return resolveDirectTools(options.tools).then((tools) =>
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
...(tools ? { tools } : {}),
|
|
332
|
-
...(options.toolChoice ? { toolChoice: options.toolChoice } : {}),
|
|
333
|
-
...(options.seed !== undefined ? { seed: options.seed } : {}),
|
|
334
|
-
...(options.presencePenalty !== undefined
|
|
335
|
-
? { presencePenalty: options.presencePenalty }
|
|
336
|
-
: {}),
|
|
337
|
-
...(options.frequencyPenalty !== undefined
|
|
338
|
-
? { frequencyPenalty: options.frequencyPenalty }
|
|
339
|
-
: {}),
|
|
340
|
-
...(options.headers ? { headers: options.headers } : {}),
|
|
341
|
-
...(options.providerOptions ? { providerOptions: options.providerOptions } : {}),
|
|
342
|
-
abortSignal: options.abortSignal,
|
|
343
|
-
}).then(buildDirectGenerateResult));
|
|
472
|
+
return resolveDirectTools(options.tools).then((tools) => {
|
|
473
|
+
const directOptions = buildDirectModelOptions(options, tools);
|
|
474
|
+
if (shouldGenerateViaStream(options.model)) {
|
|
475
|
+
return options.model.doStream(directOptions).then(({ stream }) => buildGenerateResultFromStream(stream));
|
|
476
|
+
}
|
|
477
|
+
return options.model.doGenerate(directOptions).then(buildDirectGenerateResult);
|
|
478
|
+
});
|
|
344
479
|
}
|
|
345
480
|
export function streamText(options) {
|
|
346
|
-
const directResultPromise = resolveDirectTools(options.tools).then((tools) => options.model.doStream(
|
|
347
|
-
prompt: toRuntimePrompt(normalizeSystemPrompt(options.system), getProviderRequestMessages(options.messages)),
|
|
348
|
-
maxOutputTokens: options.maxOutputTokens,
|
|
349
|
-
...(options.temperature === undefined ? {} : { temperature: options.temperature }),
|
|
350
|
-
topP: options.topP,
|
|
351
|
-
topK: options.topK,
|
|
352
|
-
stopSequences: options.stopSequences,
|
|
353
|
-
...(tools ? { tools } : {}),
|
|
354
|
-
...(options.toolChoice ? { toolChoice: options.toolChoice } : {}),
|
|
355
|
-
...(options.seed !== undefined ? { seed: options.seed } : {}),
|
|
356
|
-
...(options.presencePenalty !== undefined
|
|
357
|
-
? { presencePenalty: options.presencePenalty }
|
|
358
|
-
: {}),
|
|
359
|
-
...(options.frequencyPenalty !== undefined
|
|
360
|
-
? { frequencyPenalty: options.frequencyPenalty }
|
|
361
|
-
: {}),
|
|
362
|
-
...(options.headers ? { headers: options.headers } : {}),
|
|
363
|
-
...(options.providerOptions ? { providerOptions: options.providerOptions } : {}),
|
|
364
|
-
...(options.includeRawChunks !== undefined
|
|
365
|
-
? { includeRawChunks: options.includeRawChunks }
|
|
366
|
-
: {}),
|
|
367
|
-
abortSignal: options.abortSignal,
|
|
368
|
-
}));
|
|
481
|
+
const directResultPromise = resolveDirectTools(options.tools).then((tools) => options.model.doStream(buildDirectModelOptions(options, tools)));
|
|
369
482
|
const branchedStreamsPromise = directResultPromise.then(({ stream }) => stream.tee());
|
|
370
483
|
return {
|
|
371
484
|
fullStream: (async function* () {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"api-handler-wrapper.d.ts","sourceRoot":"","sources":["../../../../../../src/src/server/handlers/request/api/api-handler-wrapper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AACrD,OAAO,KAAK,EACV,cAAc,EACd,eAAe,EAEf,aAAa,EACd,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"api-handler-wrapper.d.ts","sourceRoot":"","sources":["../../../../../../src/src/server/handlers/request/api/api-handler-wrapper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AACrD,OAAO,KAAK,EACV,cAAc,EACd,eAAe,EAEf,aAAa,EACd,MAAM,gBAAgB,CAAC;AAsBxB,qBAAa,iBAAkB,SAAQ,WAAW;IAChD,OAAO,CAAC,UAAU,CAAS;IAC3B,OAAO,CAAC,OAAO,CAAiE;IAChF,OAAO,CAAC,WAAW,CAA8B;IAEjD,QAAQ,EAAE,eAAe,CAGvB;gBAGA,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,OAAO,uCAAuC,EAAE,cAAc;IAOnE,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAW3B,MAAM,CAAC,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,cAAc,GAAG,OAAO,CAAC,aAAa,CAAC;IAmDvE,OAAO,CAAC,iBAAiB;CA4D1B"}
|
|
@@ -49,6 +49,8 @@ export class ApiHandlerWrapper extends BaseHandler {
|
|
|
49
49
|
return fsWrapper.runWithContext(ctx.projectSlug, ctx.proxyToken ?? "", () => this.handleWithContext(req, ctx, pathname), ctx.projectId, {
|
|
50
50
|
productionMode: isProduction,
|
|
51
51
|
releaseId: ctx.releaseId,
|
|
52
|
+
branch: isProduction ? null : ctx.requestContext?.branch ?? ctx.parsedDomain?.branch ??
|
|
53
|
+
null,
|
|
52
54
|
environmentName: ctx.environmentName,
|
|
53
55
|
});
|
|
54
56
|
}
|