@mastra/core
Version:
Mastra is a framework for building AI-powered applications and agents with a modern TypeScript stack.
1,519 lines (1,516 loc) • 308 kB
JavaScript
'use strict';
var chunkP247YTIF_cjs = require('./chunk-P247YTIF.cjs');
var chunkXDKZ76SF_cjs = require('./chunk-XDKZ76SF.cjs');
var v4 = require('zod/v4');
var VERSION = "1.0.44" ;
var cerebrasErrorSchema = v4.z.object({
message: v4.z.string(),
type: v4.z.string(),
param: v4.z.string(),
code: v4.z.string()
});
var cerebrasErrorStructure = {
errorSchema: cerebrasErrorSchema,
errorToMessage: (data) => data.message
};
function createCerebras(options = {}) {
var _a15;
const baseURL = chunkP247YTIF_cjs.withoutTrailingSlash(
(_a15 = options.baseURL) != null ? _a15 : "https://api.cerebras.ai/v1"
);
const getHeaders = () => chunkP247YTIF_cjs.withUserAgentSuffix(
{
Authorization: `Bearer ${chunkP247YTIF_cjs.loadApiKey({
apiKey: options.apiKey,
environmentVariableName: "CEREBRAS_API_KEY",
description: "Cerebras API key"
})}`,
...options.headers
},
`ai-sdk/cerebras/${VERSION}`
);
const createLanguageModel = (modelId) => {
return new chunkP247YTIF_cjs.OpenAICompatibleChatLanguageModel(modelId, {
provider: `cerebras.chat`,
url: ({ path }) => `${baseURL}${path}`,
headers: getHeaders,
fetch: options.fetch,
errorStructure: cerebrasErrorStructure,
supportsStructuredOutputs: true
});
};
const provider = (modelId) => createLanguageModel(modelId);
provider.languageModel = createLanguageModel;
provider.chat = createLanguageModel;
provider.textEmbeddingModel = (modelId) => {
throw new chunkP247YTIF_cjs.NoSuchModelError({ modelId, modelType: "textEmbeddingModel" });
};
provider.imageModel = (modelId) => {
throw new chunkP247YTIF_cjs.NoSuchModelError({ modelId, modelType: "imageModel" });
};
return provider;
}
createCerebras();
var DeepInfraImageModel = class {
constructor(modelId, config) {
this.modelId = modelId;
this.config = config;
this.specificationVersion = "v2";
this.maxImagesPerCall = 1;
}
get provider() {
return this.config.provider;
}
async doGenerate({
prompt,
n,
size,
aspectRatio,
seed,
providerOptions,
headers,
abortSignal
}) {
var _a15, _b, _c, _d;
const warnings = [];
const splitSize = size == null ? void 0 : size.split("x");
const currentDate = (_c = (_b = (_a15 = this.config._internal) == null ? void 0 : _a15.currentDate) == null ? void 0 : _b.call(_a15)) != null ? _c : /* @__PURE__ */ new Date();
const { value: response, responseHeaders } = await chunkP247YTIF_cjs.postJsonToApi({
url: `${this.config.baseURL}/${this.modelId}`,
headers: chunkP247YTIF_cjs.combineHeaders(this.config.headers(), headers),
body: {
prompt,
num_images: n,
...aspectRatio && { aspect_ratio: aspectRatio },
...splitSize && { width: splitSize[0], height: splitSize[1] },
...seed != null && { seed },
...(_d = providerOptions.deepinfra) != null ? _d : {}
},
failedResponseHandler: chunkP247YTIF_cjs.createJsonErrorResponseHandler({
errorSchema: deepInfraErrorSchema,
errorToMessage: (error) => error.detail.error
}),
successfulResponseHandler: chunkP247YTIF_cjs.createJsonResponseHandler(
deepInfraImageResponseSchema
),
abortSignal,
fetch: this.config.fetch
});
return {
images: response.images.map(
(image) => image.replace(/^data:image\/\w+;base64,/, "")
),
warnings,
response: {
timestamp: currentDate,
modelId: this.modelId,
headers: responseHeaders
}
};
}
};
var deepInfraErrorSchema = v4.z.object({
detail: v4.z.object({
error: v4.z.string()
})
});
var deepInfraImageResponseSchema = v4.z.object({
images: v4.z.array(v4.z.string())
});
var DeepInfraChatLanguageModel = class extends chunkP247YTIF_cjs.OpenAICompatibleChatLanguageModel {
constructor(modelId, config) {
super(modelId, config);
}
fixUsage(usage) {
var _a15, _b;
const outputTokens = (_a15 = usage.outputTokens) != null ? _a15 : 0;
const reasoningTokens = (_b = usage.reasoningTokens) != null ? _b : 0;
if (reasoningTokens > outputTokens) {
const correctedOutputTokens = outputTokens + reasoningTokens;
return {
...usage,
outputTokens: correctedOutputTokens,
totalTokens: usage.totalTokens != null ? usage.totalTokens + reasoningTokens : void 0
};
}
return usage;
}
async doGenerate(options) {
const result = await super.doGenerate(options);
return {
...result,
usage: this.fixUsage(result.usage)
};
}
async doStream(options) {
const result = await super.doStream(options);
const fixUsage = this.fixUsage.bind(this);
const transformedStream = result.stream.pipeThrough(
new TransformStream({
transform(chunk, controller) {
if (chunk.type === "finish") {
controller.enqueue({
...chunk,
usage: fixUsage(chunk.usage)
});
} else {
controller.enqueue(chunk);
}
}
})
);
return {
...result,
stream: transformedStream
};
}
};
var VERSION2 = "1.0.42" ;
function createDeepInfra(options = {}) {
var _a15;
const baseURL = chunkP247YTIF_cjs.withoutTrailingSlash(
(_a15 = options.baseURL) != null ? _a15 : "https://api.deepinfra.com/v1"
);
const getHeaders = () => chunkP247YTIF_cjs.withUserAgentSuffix(
{
Authorization: `Bearer ${chunkP247YTIF_cjs.loadApiKey({
apiKey: options.apiKey,
environmentVariableName: "DEEPINFRA_API_KEY",
description: "DeepInfra's API key"
})}`,
...options.headers
},
`ai-sdk/deepinfra/${VERSION2}`
);
const getCommonModelConfig = (modelType) => ({
provider: `deepinfra.${modelType}`,
url: ({ path }) => `${baseURL}/openai${path}`,
headers: getHeaders,
fetch: options.fetch
});
const createChatModel = (modelId) => {
return new DeepInfraChatLanguageModel(
modelId,
getCommonModelConfig("chat")
);
};
const createCompletionModel = (modelId) => new chunkP247YTIF_cjs.OpenAICompatibleCompletionLanguageModel(
modelId,
getCommonModelConfig("completion")
);
const createTextEmbeddingModel = (modelId) => new chunkP247YTIF_cjs.OpenAICompatibleEmbeddingModel(
modelId,
getCommonModelConfig("embedding")
);
const createImageModel = (modelId) => new DeepInfraImageModel(modelId, {
...getCommonModelConfig("image"),
baseURL: baseURL ? `${baseURL}/inference` : "https://api.deepinfra.com/v1/inference"
});
const provider = (modelId) => createChatModel(modelId);
provider.completionModel = createCompletionModel;
provider.chatModel = createChatModel;
provider.image = createImageModel;
provider.imageModel = createImageModel;
provider.languageModel = createChatModel;
provider.textEmbeddingModel = createTextEmbeddingModel;
return provider;
}
createDeepInfra();
function convertToDeepSeekChatMessages({
prompt,
responseFormat,
modelId
}) {
const isDeepSeekV4 = modelId.includes("deepseek-v4");
const messages = [];
const warnings = [];
if ((responseFormat == null ? void 0 : responseFormat.type) === "json") {
if (responseFormat.schema == null) {
messages.push({
role: "system",
content: "Return JSON."
});
} else {
messages.push({
role: "system",
content: "Return JSON that conforms to the following schema: " + JSON.stringify(responseFormat.schema)
});
}
}
let lastUserMessageIndex = -1;
for (let i = prompt.length - 1; i >= 0; i--) {
if (prompt[i].role === "user") {
lastUserMessageIndex = i;
break;
}
}
let index = -1;
for (const { role, content } of prompt) {
index++;
switch (role) {
case "system": {
messages.push({ role: "system", content });
break;
}
case "user": {
let userContent = "";
for (const part of content) {
if (part.type === "text") {
userContent += part.text;
} else {
warnings.push({
type: "other",
message: `Unsupported user message part type: ${part.type}`
});
}
}
messages.push({
role: "user",
content: userContent
});
break;
}
case "assistant": {
let text = "";
let reasoning;
const toolCalls = [];
for (const part of content) {
switch (part.type) {
case "text": {
text += part.text;
break;
}
case "reasoning": {
if (index <= lastUserMessageIndex && !isDeepSeekV4) {
break;
}
if (reasoning == null) {
reasoning = part.text;
} else {
reasoning += part.text;
}
break;
}
case "tool-call": {
toolCalls.push({
id: part.toolCallId,
type: "function",
function: {
name: part.toolName,
arguments: JSON.stringify(part.input)
}
});
break;
}
}
}
messages.push({
role: "assistant",
content: text,
reasoning_content: reasoning != null ? reasoning : isDeepSeekV4 ? "" : void 0,
tool_calls: toolCalls.length > 0 ? toolCalls : void 0
});
break;
}
case "tool": {
for (const toolResponse of content) {
const output = toolResponse.output;
let contentValue;
switch (output.type) {
case "text":
case "error-text":
contentValue = output.value;
break;
case "content":
case "json":
case "error-json":
contentValue = JSON.stringify(output.value);
break;
}
messages.push({
role: "tool",
tool_call_id: toolResponse.toolCallId,
content: contentValue
});
}
break;
}
default: {
warnings.push({
type: "other",
message: `Unsupported message role: ${role}`
});
break;
}
}
}
return { messages, warnings };
}
var tokenUsageSchema = v4.z.object({
prompt_tokens: v4.z.number().nullish(),
completion_tokens: v4.z.number().nullish(),
prompt_cache_hit_tokens: v4.z.number().nullish(),
prompt_cache_miss_tokens: v4.z.number().nullish(),
total_tokens: v4.z.number().nullish(),
completion_tokens_details: v4.z.object({
reasoning_tokens: v4.z.number().nullish()
}).nullish()
}).nullish();
var deepSeekErrorSchema = v4.z.object({
error: v4.z.object({
message: v4.z.string(),
type: v4.z.string().nullish(),
param: v4.z.any().nullish(),
code: v4.z.union([v4.z.string(), v4.z.number()]).nullish()
})
});
var deepseekChatResponseSchema = v4.z.object({
id: v4.z.string().nullish(),
created: v4.z.number().nullish(),
model: v4.z.string().nullish(),
choices: v4.z.array(
v4.z.object({
message: v4.z.object({
role: v4.z.literal("assistant").nullish(),
content: v4.z.string().nullish(),
reasoning_content: v4.z.string().nullish(),
tool_calls: v4.z.array(
v4.z.object({
id: v4.z.string().nullish(),
function: v4.z.object({
name: v4.z.string(),
arguments: v4.z.string()
})
})
).nullish()
}),
finish_reason: v4.z.string().nullish()
})
),
usage: tokenUsageSchema
});
var deepseekChatChunkSchema = chunkP247YTIF_cjs.lazySchema(
() => chunkP247YTIF_cjs.zodSchema(
v4.z.union([
v4.z.object({
id: v4.z.string().nullish(),
created: v4.z.number().nullish(),
model: v4.z.string().nullish(),
choices: v4.z.array(
v4.z.object({
delta: v4.z.object({
role: v4.z.enum(["assistant"]).nullish(),
content: v4.z.string().nullish(),
reasoning_content: v4.z.string().nullish(),
tool_calls: v4.z.array(
v4.z.object({
index: v4.z.number(),
id: v4.z.string().nullish(),
function: v4.z.object({
name: v4.z.string().nullish(),
arguments: v4.z.string().nullish()
})
})
).nullish()
}).nullish(),
finish_reason: v4.z.string().nullish()
})
),
usage: tokenUsageSchema
}),
deepSeekErrorSchema
])
)
);
var deepseekChatOptions = v4.z.object({
/**
* Type of thinking to use. Defaults to `enabled`.
*/
thinking: v4.z.object({
type: v4.z.enum(["enabled", "disabled"]).optional()
}).optional(),
/**
* Controls the thinking strength for DeepSeek V4 reasoning models.
*/
reasoningEffort: v4.z.enum(["high", "max"]).optional()
});
function prepareTools({
tools,
toolChoice
}) {
tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
const toolWarnings = [];
if (tools == null) {
return { tools: void 0, toolChoice: void 0, toolWarnings };
}
const deepseekTools = [];
for (const tool of tools) {
if (tool.type === "provider-defined") {
toolWarnings.push({
type: "unsupported-tool",
tool
});
} else {
deepseekTools.push({
type: "function",
function: {
name: tool.name,
description: tool.description,
parameters: tool.inputSchema
}
});
}
}
if (toolChoice == null) {
return { tools: deepseekTools, toolChoice: void 0, toolWarnings };
}
const type = toolChoice.type;
switch (type) {
case "auto":
case "none":
case "required":
return { tools: deepseekTools, toolChoice: type, toolWarnings };
case "tool":
return {
tools: deepseekTools,
toolChoice: {
type: "function",
function: { name: toolChoice.toolName }
},
toolWarnings
};
default: {
const _exhaustiveCheck = type;
throw new chunkP247YTIF_cjs.UnsupportedFunctionalityError({
functionality: `tool choice type: ${_exhaustiveCheck}`
});
}
}
}
function getResponseMetadata({
id,
model,
created
}) {
return {
id: id != null ? id : void 0,
modelId: model != null ? model : void 0,
timestamp: created != null ? new Date(created * 1e3) : void 0
};
}
function mapDeepSeekFinishReason(finishReason) {
switch (finishReason) {
case "stop":
return "stop";
case "length":
return "length";
case "content_filter":
return "content-filter";
case "tool_calls":
return "tool-calls";
case "insufficient_system_resource":
return "error";
default:
return "unknown";
}
}
var DeepSeekChatLanguageModel = class {
constructor(modelId, config) {
this.specificationVersion = "v2";
this.supportedUrls = {};
this.modelId = modelId;
this.config = config;
this.failedResponseHandler = chunkP247YTIF_cjs.createJsonErrorResponseHandler({
errorSchema: deepSeekErrorSchema,
errorToMessage: (error) => error.error.message
});
}
get provider() {
return this.config.provider;
}
get providerOptionsName() {
return this.config.provider.split(".")[0].trim();
}
async getArgs({
prompt,
maxOutputTokens,
temperature,
topP,
topK,
frequencyPenalty,
presencePenalty,
providerOptions,
stopSequences,
responseFormat,
tools,
toolChoice,
seed
}) {
var _a15, _b;
const deepseekOptions = (_a15 = await chunkP247YTIF_cjs.parseProviderOptions({
provider: this.providerOptionsName,
providerOptions,
schema: deepseekChatOptions
})) != null ? _a15 : {};
const { messages, warnings } = convertToDeepSeekChatMessages({
prompt,
responseFormat,
modelId: this.modelId
});
if (topK != null) {
warnings.push({ type: "unsupported-setting", setting: "topK" });
}
if (seed != null) {
warnings.push({ type: "unsupported-setting", setting: "seed" });
}
const {
tools: deepseekTools,
toolChoice: deepseekToolChoices,
toolWarnings
} = prepareTools({
tools,
toolChoice
});
const thinking = ((_b = deepseekOptions.thinking) == null ? void 0 : _b.type) != null ? { type: deepseekOptions.thinking.type } : void 0;
return {
args: {
model: this.modelId,
max_tokens: maxOutputTokens,
temperature,
top_p: topP,
frequency_penalty: frequencyPenalty,
presence_penalty: presencePenalty,
response_format: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? { type: "json_object" } : void 0,
stop: stopSequences,
messages,
tools: deepseekTools,
tool_choice: deepseekToolChoices,
thinking,
...(thinking == null ? void 0 : thinking.type) !== "disabled" && deepseekOptions.reasoningEffort != null && {
reasoning_effort: deepseekOptions.reasoningEffort
}
},
warnings: [...warnings, ...toolWarnings]
};
}
async doGenerate(options) {
var _a15, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
const { args, warnings } = await this.getArgs({ ...options });
const {
responseHeaders,
value: responseBody,
rawValue: rawResponse
} = await chunkP247YTIF_cjs.postJsonToApi({
url: this.config.url({
path: "/chat/completions",
modelId: this.modelId
}),
headers: chunkP247YTIF_cjs.combineHeaders(this.config.headers(), options.headers),
body: args,
failedResponseHandler: this.failedResponseHandler,
successfulResponseHandler: chunkP247YTIF_cjs.createJsonResponseHandler(
deepseekChatResponseSchema
),
abortSignal: options.abortSignal,
fetch: this.config.fetch
});
const choice = responseBody.choices[0];
const content = [];
const reasoning = choice.message.reasoning_content;
if (reasoning != null && reasoning.length > 0) {
content.push({
type: "reasoning",
text: reasoning
});
}
if (choice.message.tool_calls != null) {
for (const toolCall of choice.message.tool_calls) {
content.push({
type: "tool-call",
toolCallId: (_a15 = toolCall.id) != null ? _a15 : chunkP247YTIF_cjs.generateId(),
toolName: toolCall.function.name,
input: toolCall.function.arguments
});
}
}
const text = choice.message.content;
if (text != null && text.length > 0) {
content.push({ type: "text", text });
}
return {
content,
finishReason: mapDeepSeekFinishReason(choice.finish_reason),
usage: {
inputTokens: (_c = (_b = responseBody.usage) == null ? void 0 : _b.prompt_tokens) != null ? _c : void 0,
outputTokens: (_e = (_d = responseBody.usage) == null ? void 0 : _d.completion_tokens) != null ? _e : void 0,
totalTokens: (_g = (_f = responseBody.usage) == null ? void 0 : _f.total_tokens) != null ? _g : void 0,
reasoningTokens: (_j = (_i = (_h = responseBody.usage) == null ? void 0 : _h.completion_tokens_details) == null ? void 0 : _i.reasoning_tokens) != null ? _j : void 0,
cachedInputTokens: (_l = (_k = responseBody.usage) == null ? void 0 : _k.prompt_cache_hit_tokens) != null ? _l : void 0
},
providerMetadata: {
[this.providerOptionsName]: {
promptCacheHitTokens: (_n = (_m = responseBody.usage) == null ? void 0 : _m.prompt_cache_hit_tokens) != null ? _n : null,
promptCacheMissTokens: (_p = (_o = responseBody.usage) == null ? void 0 : _o.prompt_cache_miss_tokens) != null ? _p : null
}
},
request: { body: args },
response: {
...getResponseMetadata(responseBody),
headers: responseHeaders,
body: rawResponse
},
warnings
};
}
async doStream(options) {
const { args, warnings } = await this.getArgs({ ...options });
const body = {
...args,
stream: true,
stream_options: { include_usage: true }
};
const { responseHeaders, value: response } = await chunkP247YTIF_cjs.postJsonToApi({
url: this.config.url({
path: "/chat/completions",
modelId: this.modelId
}),
headers: chunkP247YTIF_cjs.combineHeaders(this.config.headers(), options.headers),
body,
failedResponseHandler: this.failedResponseHandler,
successfulResponseHandler: chunkP247YTIF_cjs.createEventSourceResponseHandler(
deepseekChatChunkSchema
),
abortSignal: options.abortSignal,
fetch: this.config.fetch
});
const toolCalls = [];
let finishReason = "unknown";
let usage = void 0;
let isFirstChunk = true;
const providerOptionsName = this.providerOptionsName;
let isActiveReasoning = false;
let isActiveText = false;
return {
stream: response.pipeThrough(
new TransformStream({
start(controller) {
controller.enqueue({ type: "stream-start", warnings });
},
transform(chunk, controller) {
var _a15, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
if (options.includeRawChunks) {
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
}
if (!chunk.success) {
finishReason = "error";
controller.enqueue({ type: "error", error: chunk.error });
return;
}
const value = chunk.value;
if ("error" in value) {
finishReason = "error";
controller.enqueue({ type: "error", error: value.error.message });
return;
}
if (isFirstChunk) {
isFirstChunk = false;
controller.enqueue({
type: "response-metadata",
...getResponseMetadata(value)
});
}
if (value.usage != null) {
usage = value.usage;
}
const choice = value.choices[0];
if ((choice == null ? void 0 : choice.finish_reason) != null) {
finishReason = mapDeepSeekFinishReason(choice.finish_reason);
}
if ((choice == null ? void 0 : choice.delta) == null) {
return;
}
const delta = choice.delta;
const reasoningContent = delta.reasoning_content;
if (reasoningContent) {
if (!isActiveReasoning) {
controller.enqueue({
type: "reasoning-start",
id: "reasoning-0"
});
isActiveReasoning = true;
}
controller.enqueue({
type: "reasoning-delta",
id: "reasoning-0",
delta: reasoningContent
});
}
if (delta.content) {
if (!isActiveText) {
controller.enqueue({ type: "text-start", id: "txt-0" });
isActiveText = true;
}
if (isActiveReasoning) {
controller.enqueue({
type: "reasoning-end",
id: "reasoning-0"
});
isActiveReasoning = false;
}
controller.enqueue({
type: "text-delta",
id: "txt-0",
delta: delta.content
});
}
if (delta.tool_calls != null) {
if (isActiveReasoning) {
controller.enqueue({
type: "reasoning-end",
id: "reasoning-0"
});
isActiveReasoning = false;
}
for (const toolCallDelta of delta.tool_calls) {
const index = toolCallDelta.index;
if (toolCalls[index] == null) {
if (toolCallDelta.id == null) {
throw new chunkP247YTIF_cjs.InvalidResponseDataError({
data: toolCallDelta,
message: `Expected 'id' to be a string.`
});
}
if (((_a15 = toolCallDelta.function) == null ? void 0 : _a15.name) == null) {
throw new chunkP247YTIF_cjs.InvalidResponseDataError({
data: toolCallDelta,
message: `Expected 'function.name' to be a string.`
});
}
controller.enqueue({
type: "tool-input-start",
id: toolCallDelta.id,
toolName: toolCallDelta.function.name
});
toolCalls[index] = {
id: toolCallDelta.id,
type: "function",
function: {
name: toolCallDelta.function.name,
arguments: (_b = toolCallDelta.function.arguments) != null ? _b : ""
},
hasFinished: false
};
const toolCall2 = toolCalls[index];
if (((_c = toolCall2.function) == null ? void 0 : _c.name) != null && ((_d = toolCall2.function) == null ? void 0 : _d.arguments) != null) {
if (toolCall2.function.arguments.length > 0) {
controller.enqueue({
type: "tool-input-delta",
id: toolCall2.id,
delta: toolCall2.function.arguments
});
}
if (chunkP247YTIF_cjs.isParsableJson(toolCall2.function.arguments)) {
controller.enqueue({
type: "tool-input-end",
id: toolCall2.id
});
controller.enqueue({
type: "tool-call",
toolCallId: (_e = toolCall2.id) != null ? _e : chunkP247YTIF_cjs.generateId(),
toolName: toolCall2.function.name,
input: toolCall2.function.arguments
});
toolCall2.hasFinished = true;
}
}
continue;
}
const toolCall = toolCalls[index];
if (toolCall.hasFinished) {
continue;
}
if (((_f = toolCallDelta.function) == null ? void 0 : _f.arguments) != null) {
toolCall.function.arguments += (_h = (_g = toolCallDelta.function) == null ? void 0 : _g.arguments) != null ? _h : "";
}
controller.enqueue({
type: "tool-input-delta",
id: toolCall.id,
delta: (_i = toolCallDelta.function.arguments) != null ? _i : ""
});
if (((_j = toolCall.function) == null ? void 0 : _j.name) != null && ((_k = toolCall.function) == null ? void 0 : _k.arguments) != null && chunkP247YTIF_cjs.isParsableJson(toolCall.function.arguments)) {
controller.enqueue({
type: "tool-input-end",
id: toolCall.id
});
controller.enqueue({
type: "tool-call",
toolCallId: (_l = toolCall.id) != null ? _l : chunkP247YTIF_cjs.generateId(),
toolName: toolCall.function.name,
input: toolCall.function.arguments
});
toolCall.hasFinished = true;
}
}
}
},
flush(controller) {
var _a15, _b, _c, _d, _e, _f, _g, _h, _i;
if (isActiveReasoning) {
controller.enqueue({ type: "reasoning-end", id: "reasoning-0" });
}
if (isActiveText) {
controller.enqueue({ type: "text-end", id: "txt-0" });
}
for (const toolCall of toolCalls.filter(
(toolCall2) => !toolCall2.hasFinished
)) {
controller.enqueue({
type: "tool-input-end",
id: toolCall.id
});
controller.enqueue({
type: "tool-call",
toolCallId: (_a15 = toolCall.id) != null ? _a15 : chunkP247YTIF_cjs.generateId(),
toolName: toolCall.function.name,
input: toolCall.function.arguments
});
}
controller.enqueue({
type: "finish",
finishReason,
usage: {
inputTokens: (_b = usage == null ? void 0 : usage.prompt_tokens) != null ? _b : void 0,
outputTokens: (_c = usage == null ? void 0 : usage.completion_tokens) != null ? _c : void 0,
totalTokens: (_d = usage == null ? void 0 : usage.total_tokens) != null ? _d : void 0,
reasoningTokens: (_f = (_e = usage == null ? void 0 : usage.completion_tokens_details) == null ? void 0 : _e.reasoning_tokens) != null ? _f : void 0,
cachedInputTokens: (_g = usage == null ? void 0 : usage.prompt_cache_hit_tokens) != null ? _g : void 0
},
providerMetadata: {
[providerOptionsName]: {
promptCacheHitTokens: (_h = usage == null ? void 0 : usage.prompt_cache_hit_tokens) != null ? _h : null,
promptCacheMissTokens: (_i = usage == null ? void 0 : usage.prompt_cache_miss_tokens) != null ? _i : null
}
}
});
}
})
),
request: { body },
response: { headers: responseHeaders }
};
}
};
var VERSION3 = "1.0.40" ;
function createDeepSeek(options = {}) {
var _a15;
const baseURL = chunkP247YTIF_cjs.withoutTrailingSlash(
(_a15 = options.baseURL) != null ? _a15 : "https://api.deepseek.com"
);
const getHeaders = () => chunkP247YTIF_cjs.withUserAgentSuffix(
{
Authorization: `Bearer ${chunkP247YTIF_cjs.loadApiKey({
apiKey: options.apiKey,
environmentVariableName: "DEEPSEEK_API_KEY",
description: "DeepSeek API key"
})}`,
...options.headers
},
`ai-sdk/deepseek/${VERSION3}`
);
const createLanguageModel = (modelId) => {
return new DeepSeekChatLanguageModel(modelId, {
provider: `deepseek.chat`,
url: ({ path }) => `${baseURL}${path}`,
headers: getHeaders,
fetch: options.fetch
});
};
const provider = (modelId) => createLanguageModel(modelId);
provider.languageModel = createLanguageModel;
provider.chat = createLanguageModel;
provider.textEmbeddingModel = (modelId) => {
throw new chunkP247YTIF_cjs.NoSuchModelError({ modelId, modelType: "textEmbeddingModel" });
};
provider.imageModel = (modelId) => {
throw new chunkP247YTIF_cjs.NoSuchModelError({ modelId, modelType: "imageModel" });
};
return provider;
}
createDeepSeek();
function convertGroqUsage(usage) {
var _a15, _b, _c, _d;
if (usage == null) {
return {
inputTokens: {
total: void 0,
noCache: void 0,
cacheRead: void 0,
cacheWrite: void 0
},
outputTokens: {
total: void 0,
text: void 0,
reasoning: void 0
},
raw: void 0
};
}
const promptTokens = (_a15 = usage.prompt_tokens) != null ? _a15 : 0;
const completionTokens = (_b = usage.completion_tokens) != null ? _b : 0;
const reasoningTokens = (_d = (_c = usage.completion_tokens_details) == null ? void 0 : _c.reasoning_tokens) != null ? _d : void 0;
const textTokens = reasoningTokens != null ? completionTokens - reasoningTokens : completionTokens;
return {
inputTokens: {
total: promptTokens,
noCache: promptTokens,
cacheRead: void 0,
cacheWrite: void 0
},
outputTokens: {
total: completionTokens,
text: textTokens,
reasoning: reasoningTokens
},
raw: usage
};
}
function convertToGroqChatMessages(prompt) {
var _a15;
const messages = [];
for (const { role, content } of prompt) {
switch (role) {
case "system": {
messages.push({ role: "system", content });
break;
}
case "user": {
if (content.length === 1 && content[0].type === "text") {
messages.push({ role: "user", content: content[0].text });
break;
}
messages.push({
role: "user",
content: content.map((part) => {
switch (part.type) {
case "text": {
return { type: "text", text: part.text };
}
case "file": {
if (!part.mediaType.startsWith("image/")) {
throw new chunkP247YTIF_cjs.UnsupportedFunctionalityError2({
functionality: "Non-image file content parts"
});
}
const mediaType = part.mediaType === "image/*" ? "image/jpeg" : part.mediaType;
return {
type: "image_url",
image_url: {
url: part.data instanceof URL ? part.data.toString() : `data:${mediaType};base64,${chunkP247YTIF_cjs.convertToBase642(part.data)}`
}
};
}
}
})
});
break;
}
case "assistant": {
let text = "";
let reasoning = "";
const toolCalls = [];
for (const part of content) {
switch (part.type) {
// groq supports reasoning for tool-calls in multi-turn conversations
// https://github.com/vercel/ai/issues/7860
case "reasoning": {
reasoning += part.text;
break;
}
case "text": {
text += part.text;
break;
}
case "tool-call": {
toolCalls.push({
id: part.toolCallId,
type: "function",
function: {
name: part.toolName,
arguments: JSON.stringify(part.input)
}
});
break;
}
}
}
messages.push({
role: "assistant",
content: text,
...reasoning.length > 0 ? { reasoning } : null,
...toolCalls.length > 0 ? { tool_calls: toolCalls } : null
});
break;
}
case "tool": {
for (const toolResponse of content) {
if (toolResponse.type === "tool-approval-response") {
continue;
}
const output = toolResponse.output;
let contentValue;
switch (output.type) {
case "text":
case "error-text":
contentValue = output.value;
break;
case "execution-denied":
contentValue = (_a15 = output.reason) != null ? _a15 : "Tool execution denied.";
break;
case "content":
case "json":
case "error-json":
contentValue = JSON.stringify(output.value);
break;
}
messages.push({
role: "tool",
tool_call_id: toolResponse.toolCallId,
content: contentValue
});
}
break;
}
default: {
const _exhaustiveCheck = role;
throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
}
}
}
return messages;
}
function getResponseMetadata2({
id,
model,
created
}) {
return {
id: id != null ? id : void 0,
modelId: model != null ? model : void 0,
timestamp: created != null ? new Date(created * 1e3) : void 0
};
}
var groqLanguageModelOptions = v4.z.object({
reasoningFormat: v4.z.enum(["parsed", "raw", "hidden"]).optional(),
/**
* Specifies the reasoning effort level for model inference.
* @see https://console.groq.com/docs/reasoning#reasoning-effort
*/
reasoningEffort: v4.z.enum(["none", "default", "low", "medium", "high"]).optional(),
/**
* Whether to enable parallel function calling during tool use. Default to true.
*/
parallelToolCalls: v4.z.boolean().optional(),
/**
* A unique identifier representing your end-user, which can help OpenAI to
* monitor and detect abuse. Learn more.
*/
user: v4.z.string().optional(),
/**
* Whether to use structured outputs.
*
* @default true
*/
structuredOutputs: v4.z.boolean().optional(),
/**
* Whether to use strict JSON schema validation.
* When true, the model uses constrained decoding to guarantee schema compliance.
* Only used when structured outputs are enabled and a schema is provided.
*
* @default true
*/
strictJsonSchema: v4.z.boolean().optional(),
/**
* Service tier for the request.
* - 'on_demand': Default tier with consistent performance and fairness
* - 'performance': Prioritized tier for latency-sensitive workloads
* - 'flex': Higher throughput tier optimized for workloads that can handle occasional request failures
* - 'auto': Uses on_demand rate limits, then falls back to flex tier if exceeded
*
* @default 'on_demand'
*/
serviceTier: v4.z.enum(["on_demand", "performance", "flex", "auto"]).optional()
});
var groqErrorDataSchema = v4.z.object({
error: v4.z.object({
message: v4.z.string(),
type: v4.z.string()
})
});
var groqFailedResponseHandler = chunkP247YTIF_cjs.createJsonErrorResponseHandler2({
errorSchema: groqErrorDataSchema,
errorToMessage: (data) => data.error.message
});
var BROWSER_SEARCH_SUPPORTED_MODELS = [
"openai/gpt-oss-20b",
"openai/gpt-oss-120b"
];
function isBrowserSearchSupportedModel(modelId) {
return BROWSER_SEARCH_SUPPORTED_MODELS.includes(modelId);
}
function getSupportedModelsString() {
return BROWSER_SEARCH_SUPPORTED_MODELS.join(", ");
}
function prepareTools2({
tools,
toolChoice,
modelId
}) {
tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
const toolWarnings = [];
if (tools == null) {
return { tools: void 0, toolChoice: void 0, toolWarnings };
}
const groqTools2 = [];
for (const tool of tools) {
if (tool.type === "provider") {
if (tool.id === "groq.browser_search") {
if (!isBrowserSearchSupportedModel(modelId)) {
toolWarnings.push({
type: "unsupported",
feature: `provider-defined tool ${tool.id}`,
details: `Browser search is only supported on the following models: ${getSupportedModelsString()}. Current model: ${modelId}`
});
} else {
groqTools2.push({
type: "browser_search"
});
}
} else {
toolWarnings.push({
type: "unsupported",
feature: `provider-defined tool ${tool.id}`
});
}
} else {
groqTools2.push({
type: "function",
function: {
name: tool.name,
description: tool.description,
parameters: tool.inputSchema,
...tool.strict != null ? { strict: tool.strict } : {}
}
});
}
}
if (toolChoice == null) {
return { tools: groqTools2, toolChoice: void 0, toolWarnings };
}
const type = toolChoice.type;
switch (type) {
case "auto":
case "none":
case "required":
return { tools: groqTools2, toolChoice: type, toolWarnings };
case "tool":
return {
tools: groqTools2,
toolChoice: {
type: "function",
function: {
name: toolChoice.toolName
}
},
toolWarnings
};
default: {
const _exhaustiveCheck = type;
throw new chunkP247YTIF_cjs.UnsupportedFunctionalityError2({
functionality: `tool choice type: ${_exhaustiveCheck}`
});
}
}
}
function mapGroqFinishReason(finishReason) {
switch (finishReason) {
case "stop":
return "stop";
case "length":
return "length";
case "content_filter":
return "content-filter";
case "function_call":
case "tool_calls":
return "tool-calls";
default:
return "other";
}
}
var GroqChatLanguageModel = class {
constructor(modelId, config) {
this.specificationVersion = "v3";
this.supportedUrls = {
"image/*": [/^https?:\/\/.*$/]
};
this.modelId = modelId;
this.config = config;
}
get provider() {
return this.config.provider;
}
async getArgs({
prompt,
maxOutputTokens,
temperature,
topP,
topK,
frequencyPenalty,
presencePenalty,
stopSequences,
responseFormat,
seed,
stream,
tools,
toolChoice,
providerOptions
}) {
var _a15, _b, _c;
const warnings = [];
const groqOptions = await chunkP247YTIF_cjs.parseProviderOptions2({
provider: "groq",
providerOptions,
schema: groqLanguageModelOptions
});
const structuredOutputs = (_a15 = groqOptions == null ? void 0 : groqOptions.structuredOutputs) != null ? _a15 : true;
const strictJsonSchema = (_b = groqOptions == null ? void 0 : groqOptions.strictJsonSchema) != null ? _b : true;
if (topK != null) {
warnings.push({ type: "unsupported", feature: "topK" });
}
if ((responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && !structuredOutputs) {
warnings.push({
type: "unsupported",
feature: "responseFormat",
details: "JSON response format schema is only supported with structuredOutputs"
});
}
const {
tools: groqTools2,
toolChoice: groqToolChoice,
toolWarnings
} = prepareTools2({ tools, toolChoice, modelId: this.modelId });
return {
args: {
// model id:
model: this.modelId,
// model specific settings:
user: groqOptions == null ? void 0 : groqOptions.user,
parallel_tool_calls: groqOptions == null ? void 0 : groqOptions.parallelToolCalls,
// standardized settings:
max_tokens: maxOutputTokens,
temperature,
top_p: topP,
frequency_penalty: frequencyPenalty,
presence_penalty: presencePenalty,
stop: stopSequences,
seed,
// response format:
response_format: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? structuredOutputs && responseFormat.schema != null ? {
type: "json_schema",
json_schema: {
schema: responseFormat.schema,
strict: strictJsonSchema,
name: (_c = responseFormat.name) != null ? _c : "response",
description: responseFormat.description
}
} : { type: "json_object" } : void 0,
// provider options:
reasoning_format: groqOptions == null ? void 0 : groqOptions.reasoningFormat,
reasoning_effort: groqOptions == null ? void 0 : groqOptions.reasoningEffort,
service_tier: groqOptions == null ? void 0 : groqOptions.serviceTier,
// messages:
messages: convertToGroqChatMessages(prompt),
// tools:
tools: groqTools2,
tool_choice: groqToolChoice
},
warnings: [...warnings, ...toolWarnings]
};
}
async doGenerate(options) {
var _a15, _b;
const { args, warnings } = await this.getArgs({
...options,
stream: false
});
const body = JSON.stringify(args);
const {
responseHeaders,
value: response,
rawValue: rawResponse
} = await chunkP247YTIF_cjs.postJsonToApi2({
url: this.config.url({
path: "/chat/completions",
modelId: this.modelId
}),
headers: chunkP247YTIF_cjs.combineHeaders2(this.config.headers(), options.headers),
body: args,
failedResponseHandler: groqFailedResponseHandler,
successfulResponseHandler: chunkP247YTIF_cjs.createJsonResponseHandler2(
groqChatResponseSchema
),
abortSignal: options.abortSignal,
fetch: this.config.fetch
});
const choice = response.choices[0];
const content = [];
const text = choice.message.content;
if (text != null && text.length > 0) {
content.push({ type: "text", text });
}
const reasoning = choice.message.reasoning;
if (reasoning != null && reasoning.length > 0) {
content.push({
type: "reasoning",
text: reasoning
});
}
if (choice.message.tool_calls != null) {
for (const toolCall of choice.message.tool_calls) {
content.push({
type: "tool-call",
toolCallId: (_a15 = toolCall.id) != null ? _a15 : chunkP247YTIF_cjs.generateId2(),
toolName: toolCall.function.name,
input: toolCall.function.arguments
});
}
}
return {
content,
finishReason: {
unified: mapGroqFinishReason(choice.finish_reason),
raw: (_b = choice.finish_reason) != null ? _b : void 0
},
usage: convertGroqUsage(response.usage),
response: {
...getResponseMetadata2(response),
headers: responseHeaders,
body: rawResponse
},
warnings,
request: { body }
};
}
async doStream(options) {
const { args, warnings } = await this.getArgs({ ...options, stream: true });
const body = JSON.stringify({ ...args, stream: true });
const { responseHeaders, value: response } = await chunkP247YTIF_cjs.postJsonToApi2({
url: this.config.url({
path: "/chat/completions",
modelId: this.modelId
}),
headers: chunkP247YTIF_cjs.combineHeaders2(this.config.headers(), options.headers),
body: {
...args,
stream: true
},
failedResponseHandler: groqFailedResponseHandler,
successfulResponseHandler: chunkP247YTIF_cjs.createEventSourceResponseHandler2(groqChatChunkSchema),
abortSignal: options.abortSignal,
fetch: this.config.fetch
});
const toolCalls = [];
let finishReason = {
unified: "other",
raw: void 0
};
let usage = void 0;
let isFirstChunk = true;
let isActiveText = false;
let isActiveReasoning = false;
return {
stream: response.pipeThrough(
new TransformStream({
start(controller) {
controller.enqueue({ type: "stream-start", warnings });
},
transform(chunk, controller) {
var _a15, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
if (options.includeRawChunks) {
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
}
if (!chunk.success) {
finishReason = {
unified: "error",
raw: void 0
};
controller.enqueue({ type: "error", error: chunk.error });
return;
}
const value = chunk.value;
if ("error" in value) {
finishReason = {
unified: "error",
raw: void 0
};
controller.enqueue({ type: "error", error: value.error });
return;
}
if (isFirstChunk) {
isFirstChunk = false;
controller.enqueue({
type: "response-metadata",
...getResponseMetadata2(value)
});
}
if (((_a15 = value.x_groq) == null ? void 0 : _a15.usage) != null) {
usage = value.x_groq.usage;
}
const choice = value.choices[0];
if ((choice == null ? void 0 : choice.finish_reason) != null) {
finishReason = {
unified: mapGroqFinishReason(choice.finish_reason),
raw: choice.finish_reason
};
}
if ((choice == null ? void 0 : choice.delta) == null) {
return;
}
const delta = choice.delta;
if (delta.reasoning != null && delta.reasoning.length > 0) {
if (!isActiveReasoning) {
controller.enqueue({
type: "reasoning-start",
id: "reasoning-0"
});
isActiveReasoning = true;
}
controller.enqueue({
type: "reasoning-delta",
id: "reasoning-0",
delta: delta.reasoning
});
}
if (delta.content != null && delta.content.length > 0) {
if (isActiveReasoning) {
controller.enqueue({
type: "reasoning-end",
id: "reasoning-0"
});
isActiveReasoning = false;
}
if (!isActiveText) {
controller.enqueue({ type: "text-start", id: "txt-0" });
isActiveText = true;
}
controller.enqueue({
type: "text-delta",
id: "txt-0",
delta: delta.content
});
}
if (delta.tool_calls != null) {
if (isActiveReasoning) {
controller.enqueue({
type: "reasoning-end",
id: "reasoning-0"
});