UNPKG

@tanstack/ai

Version:

Type-safe TypeScript AI SDK for streaming chat, tool calling, agents, structured outputs, and multimodal generation.

82 lines (81 loc) • 2.85 kB
//#region src/reasoning.ts /** Every level, from least to most thinking. */ var REASONING_LEVELS = [ "off", "minimal", "low", "medium", "high", "xhigh", "max" ]; /** A level or an object, as the user passed it, into the one shape adapters read. */ function normalizeReasoning(option) { if (option === void 0) return void 0; if (typeof option === "string") return { level: option, summary: true }; return { level: option.level, summary: option.summary ?? true, ...option.budgetTokens !== void 0 ? { budgetTokens: option.budgetTokens } : {} }; } /** * The levels a model supports, from least to most thinking: the levels in its * data. A model that does not reason, or has no data, supports only `off`. */ function supportedReasoningLevels(reasoning) { if (!reasoning) return ["off"]; return REASONING_LEVELS.filter((level) => level in reasoning.levels); } /** * A level the model supports: the level itself, else the nearest supported * level above it, else the nearest below it, else `off`. The same rule as pi. */ function clampReasoningLevel(reasoning, level) { const supported = supportedReasoningLevels(reasoning); if (supported.includes(level)) return level; const index = REASONING_LEVELS.indexOf(level); const above = REASONING_LEVELS.slice(index + 1).find((candidate) => supported.includes(candidate)); if (above) return above; return REASONING_LEVELS.slice(0, index).reverse().find((candidate) => supported.includes(candidate)) ?? supported[0] ?? "off"; } /** * pi's thinking budgets for budget-based models. `xhigh` and `max` get the * `high` budget: budget-based models have no higher level. */ var DEFAULT_REASONING_BUDGETS = { minimal: 1024, low: 2048, medium: 8192, high: 16384, xhigh: 16384, max: 16384 }; /** The thinking budget for a request: `budgetTokens`, else pi's table. `0` for `off`. */ function reasoningBudget(request) { if (request.budgetTokens !== void 0) return request.budgetTokens; return request.level === "off" ? 0 : DEFAULT_REASONING_BUDGETS[request.level]; } /** * Resolve `request` for a model: clamp the level to the model's levels, and * look up its provider value. `undefined` when there is nothing to send: no * request, or a model that does not reason, has no data, or has no levels. */ function resolveReasoning(request, reasoning) { if (!request || !reasoning) return void 0; const level = clampReasoningLevel(reasoning, request.level); const value = reasoning.levels[level]; if (value === void 0) return void 0; return { level, value, summary: request.summary, ...request.budgetTokens !== void 0 ? { budgetTokens: request.budgetTokens } : {} }; } //#endregion export { DEFAULT_REASONING_BUDGETS, REASONING_LEVELS, clampReasoningLevel, normalizeReasoning, reasoningBudget, resolveReasoning, supportedReasoningLevels }; //# sourceMappingURL=reasoning.js.map