okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-027-gh-app: GitHub app and Terminal items for 0.2.73cabb9e3f

main
3.4 KiB73 linesraw
1import { clampThinkingLevel } from "../models.js";
2import { estimateContextTokens } from "../utils/estimate.js";
3const CONTEXT_SAFETY_TOKENS = 4096;
4const MIN_MAX_TOKENS = 1;
5export function clampMaxTokensToContext(model, context, maxTokens) {
6    if (model.contextWindow <= 0)
7        return Math.max(MIN_MAX_TOKENS, maxTokens);
8    const available = model.contextWindow - estimateContextTokens(context).tokens - CONTEXT_SAFETY_TOKENS;
9    return Math.min(maxTokens, Math.max(MIN_MAX_TOKENS, available));
10}
11export function resolveSamplingParams(model, thinkingLevel, requestParams) {
12    const effectiveThinkingLevel = clampThinkingLevel(model, thinkingLevel);
13    const thinkingLevelParams = model.samplingParamsByThinkingLevel?.[effectiveThinkingLevel];
14    return model.samplingParams || thinkingLevelParams || requestParams
15        ? { ...model.samplingParams, ...thinkingLevelParams, ...requestParams }
16        : undefined;
17}
18export function buildBaseOptions(model, context, options, apiKey) {
19    const samplingParams = resolveSamplingParams(model, options?.reasoning ?? "off", options?.samplingParams);
20    return {
21        temperature: options?.temperature,
22        samplingParams,
23        maxTokens: clampMaxTokensToContext(model, context, options?.maxTokens ?? model.maxTokens),
24        signal: options?.signal,
25        telemetryContext: options?.telemetryContext,
26        apiKey: apiKey || options?.apiKey,
27        fetch: options?.fetch,
28        transport: options?.transport,
29        cacheRetention: options?.cacheRetention,
30        sessionId: options?.sessionId,
31        headers: options?.headers,
32        onPayload: options?.onPayload,
33        onResponse: options?.onResponse,
34        onProviderStreamEvent: options?.onProviderStreamEvent,
35        timeoutMs: options?.timeoutMs,
36        websocketConnectTimeoutMs: options?.websocketConnectTimeoutMs,
37        maxRetries: options?.maxRetries,
38        maxRetryDelayMs: options?.maxRetryDelayMs,
39        metadata: options?.metadata,
40        env: options?.env,
41    };
42}
43/** Tokens always left for the answer when a thinking budget shares the response ceiling. */
44export const MIN_ANSWER_TOKENS = 1024;
45export const DEFAULT_THINKING_BUDGETS = {
46    minimal: 1024,
47    low: 2048,
48    medium: 8192,
49    high: 16384,
50};
51export function clampReasoning(effort) {
52    return effort === "xhigh" || effort === "max" ? "high" : effort;
53}
54export function thinkingBudgetForLevel(reasoningLevel, customBudgets) {
55    const budgets = { ...DEFAULT_THINKING_BUDGETS, ...customBudgets };
56    const level = clampReasoning(reasoningLevel);
57    return budgets[level];
58}
59/** Cap a thinking budget so at least MIN_ANSWER_TOKENS remain under a shared response ceiling. */
60export function clampThinkingBudgetToAnswerRoom(thinkingBudget, ceiling) {
61    return Math.min(thinkingBudget, Math.max(0, ceiling - MIN_ANSWER_TOKENS));
62}
63export function adjustMaxTokensForThinking(
64// Undefined means no explicit caller cap. Use the model cap and fit thinking inside it.
65baseMaxTokens, modelMaxTokens, reasoningLevel, customBudgets) {
66    let thinkingBudget = thinkingBudgetForLevel(reasoningLevel, customBudgets);
67    const maxTokens = baseMaxTokens === undefined ? modelMaxTokens : Math.min(baseMaxTokens + thinkingBudget, modelMaxTokens);
68    if (maxTokens <= thinkingBudget) {
69        thinkingBudget = clampThinkingBudgetToAnswerRoom(thinkingBudget, maxTokens);
70    }
71    return { maxTokens, thinkingBudget };
72}
73//# sourceMappingURL=simple-options.js.map