'use strict'; var chunkFEUXFWF5_cjs = require('./chunk-FEUXFWF5.cjs'); var zod = require('zod'); function convertToGroqChatMessages(prompt) { 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) => { var _a; switch (part.type) { case "text": { return { type: "text", text: part.text }; } case "image": { return { type: "image_url", image_url: { url: part.image instanceof URL ? part.image.toString() : `data:${(_a = part.mimeType) != null ? _a : "image/jpeg"};base64,${chunkFEUXFWF5_cjs.convertUint8ArrayToBase64(part.image)}` } }; } case "file": { throw new chunkFEUXFWF5_cjs.UnsupportedFunctionalityError({ functionality: "File content parts in user messages" }); } } }) }); break; } case "assistant": { let text = ""; const toolCalls = []; for (const part of content) { switch (part.type) { case "text": { text += part.text; break; } case "tool-call": { toolCalls.push({ id: part.toolCallId, type: "function", function: { name: part.toolName, arguments: JSON.stringify(part.args) } }); break; } } } messages.push({ role: "assistant", content: text, tool_calls: toolCalls.length > 0 ? toolCalls : void 0 }); break; } case "tool": { for (const toolResponse of content) { messages.push({ role: "tool", tool_call_id: toolResponse.toolCallId, content: JSON.stringify(toolResponse.result) }); } break; } default: { const _exhaustiveCheck = role; throw new Error(`Unsupported role: ${_exhaustiveCheck}`); } } } return messages; } 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 }; } var groqErrorDataSchema = zod.z.object({ error: zod.z.object({ message: zod.z.string(), type: zod.z.string() }) }); var groqFailedResponseHandler = chunkFEUXFWF5_cjs.createJsonErrorResponseHandler({ errorSchema: groqErrorDataSchema, errorToMessage: (data) => data.error.message }); function prepareTools({ mode }) { var _a; const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0; const toolWarnings = []; if (tools == null) { return { tools: void 0, tool_choice: void 0, toolWarnings }; } const toolChoice = mode.toolChoice; const groqTools = []; for (const tool of tools) { if (tool.type === "provider-defined") { toolWarnings.push({ type: "unsupported-tool", tool }); } else { groqTools.push({ type: "function", function: { name: tool.name, description: tool.description, parameters: tool.parameters } }); } } if (toolChoice == null) { return { tools: groqTools, tool_choice: void 0, toolWarnings }; } const type = toolChoice.type; switch (type) { case "auto": case "none": case "required": return { tools: groqTools, tool_choice: type, toolWarnings }; case "tool": return { tools: groqTools, tool_choice: { type: "function", function: { name: toolChoice.toolName } }, toolWarnings }; default: { const _exhaustiveCheck = type; throw new chunkFEUXFWF5_cjs.UnsupportedFunctionalityError({ functionality: `Unsupported 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 "unknown"; } } var GroqChatLanguageModel = class { constructor(modelId, settings, config) { this.specificationVersion = "v1"; this.supportsStructuredOutputs = false; this.defaultObjectGenerationMode = "json"; this.modelId = modelId; this.settings = settings; this.config = config; } get provider() { return this.config.provider; } get supportsImageUrls() { return !this.settings.downloadImages; } getArgs({ mode, prompt, maxTokens, temperature, topP, topK, frequencyPenalty, presencePenalty, stopSequences, responseFormat, seed, stream, providerMetadata }) { const type = mode.type; const warnings = []; if (topK != null) { warnings.push({ type: "unsupported-setting", setting: "topK" }); } if (responseFormat != null && responseFormat.type === "json" && responseFormat.schema != null) { warnings.push({ type: "unsupported-setting", setting: "responseFormat", details: "JSON response format schema is not supported" }); } const groqOptions = chunkFEUXFWF5_cjs.parseProviderOptions({ provider: "groq", providerOptions: providerMetadata, schema: zod.z.object({ reasoningFormat: zod.z.enum(["parsed", "raw", "hidden"]).nullish() }) }); const baseArgs = { // model id: model: this.modelId, // model specific settings: user: this.settings.user, parallel_tool_calls: this.settings.parallelToolCalls, // standardized settings: max_tokens: maxTokens, temperature, top_p: topP, frequency_penalty: frequencyPenalty, presence_penalty: presencePenalty, stop: stopSequences, seed, // response format: response_format: ( // json object response format is not supported for streaming: stream === false && (responseFormat == null ? void 0 : responseFormat.type) === "json" ? { type: "json_object" } : void 0 ), // provider options: reasoning_format: groqOptions == null ? void 0 : groqOptions.reasoningFormat, // messages: messages: convertToGroqChatMessages(prompt) }; switch (type) { case "regular": { const { tools, tool_choice, toolWarnings } = prepareTools({ mode }); return { args: { ...baseArgs, tools, tool_choice }, warnings: [...warnings, ...toolWarnings] }; } case "object-json": { return { args: { ...baseArgs, response_format: ( // json object response format is not supported for streaming: stream === false ? { type: "json_object" } : void 0 ) }, warnings }; } case "object-tool": { return { args: { ...baseArgs, tool_choice: { type: "function", function: { name: mode.tool.name } }, tools: [ { type: "function", function: { name: mode.tool.name, description: mode.tool.description, parameters: mode.tool.parameters } } ] }, warnings }; } default: { const _exhaustiveCheck = type; throw new Error(`Unsupported type: ${_exhaustiveCheck}`); } } } async doGenerate(options) { var _a, _b, _c, _d, _e, _f, _g; const { args, warnings } = this.getArgs({ ...options, stream: false }); const body = JSON.stringify(args); const { responseHeaders, value: response, rawValue: rawResponse } = await chunkFEUXFWF5_cjs.postJsonToApi({ url: this.config.url({ path: "/chat/completions", modelId: this.modelId }), headers: chunkFEUXFWF5_cjs.combineHeaders(this.config.headers(), options.headers), body: args, failedResponseHandler: groqFailedResponseHandler, successfulResponseHandler: chunkFEUXFWF5_cjs.createJsonResponseHandler( groqChatResponseSchema ), abortSignal: options.abortSignal, fetch: this.config.fetch }); const { messages: rawPrompt, ...rawSettings } = args; const choice = response.choices[0]; return { text: (_a = choice.message.content) != null ? _a : void 0, reasoning: (_b = choice.message.reasoning) != null ? _b : void 0, toolCalls: (_c = choice.message.tool_calls) == null ? void 0 : _c.map((toolCall) => { var _a2; return { toolCallType: "function", toolCallId: (_a2 = toolCall.id) != null ? _a2 : chunkFEUXFWF5_cjs.generateId(), toolName: toolCall.function.name, args: toolCall.function.arguments }; }), finishReason: mapGroqFinishReason(choice.finish_reason), usage: { promptTokens: (_e = (_d = response.usage) == null ? void 0 : _d.prompt_tokens) != null ? _e : NaN, completionTokens: (_g = (_f = response.usage) == null ? void 0 : _f.completion_tokens) != null ? _g : NaN }, rawCall: { rawPrompt, rawSettings }, rawResponse: { headers: responseHeaders, body: rawResponse }, response: getResponseMetadata(response), warnings, request: { body } }; } async doStream(options) { const { args, warnings } = this.getArgs({ ...options, stream: true }); const body = JSON.stringify({ ...args, stream: true }); const { responseHeaders, value: response } = await chunkFEUXFWF5_cjs.postJsonToApi({ url: this.config.url({ path: "/chat/completions", modelId: this.modelId }), headers: chunkFEUXFWF5_cjs.combineHeaders(this.config.headers(), options.headers), body: { ...args, stream: true }, failedResponseHandler: groqFailedResponseHandler, successfulResponseHandler: chunkFEUXFWF5_cjs.createEventSourceResponseHandler(groqChatChunkSchema), abortSignal: options.abortSignal, fetch: this.config.fetch }); const { messages: rawPrompt, ...rawSettings } = args; const toolCalls = []; let finishReason = "unknown"; let usage = { promptTokens: void 0, completionTokens: void 0 }; let isFirstChunk = true; return { stream: response.pipeThrough( new TransformStream({ transform(chunk, controller) { var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o; 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 }); return; } if (isFirstChunk) { isFirstChunk = false; controller.enqueue({ type: "response-metadata", ...getResponseMetadata(value) }); } if (((_a = value.x_groq) == null ? void 0 : _a.usage) != null) { usage = { promptTokens: (_b = value.x_groq.usage.prompt_tokens) != null ? _b : void 0, completionTokens: (_c = value.x_groq.usage.completion_tokens) != null ? _c : void 0 }; } const choice = value.choices[0]; if ((choice == null ? void 0 : choice.finish_reason) != null) { finishReason = mapGroqFinishReason(choice.finish_reason); } if ((choice == null ? void 0 : choice.delta) == null) { return; } const delta = choice.delta; if (delta.reasoning != null && delta.reasoning.length > 0) { controller.enqueue({ type: "reasoning", textDelta: delta.reasoning }); } if (delta.content != null && delta.content.length > 0) { controller.enqueue({ type: "text-delta", textDelta: delta.content }); } if (delta.tool_calls != null) { for (const toolCallDelta of delta.tool_calls) { const index = toolCallDelta.index; if (toolCalls[index] == null) { if (toolCallDelta.type !== "function") { throw new chunkFEUXFWF5_cjs.InvalidResponseDataError({ data: toolCallDelta, message: `Expected 'function' type.` }); } if (toolCallDelta.id == null) { throw new chunkFEUXFWF5_cjs.InvalidResponseDataError({ data: toolCallDelta, message: `Expected 'id' to be a string.` }); } if (((_d = toolCallDelta.function) == null ? void 0 : _d.name) == null) { throw new chunkFEUXFWF5_cjs.InvalidResponseDataError({ data: toolCallDelta, message: `Expected 'function.name' to be a string.` }); } toolCalls[index] = { id: toolCallDelta.id, type: "function", function: { name: toolCallDelta.function.name, arguments: (_e = toolCallDelta.function.arguments) != null ? _e : "" }, hasFinished: false }; const toolCall2 = toolCalls[index]; if (((_f = toolCall2.function) == null ? void 0 : _f.name) != null && ((_g = toolCall2.function) == null ? void 0 : _g.arguments) != null) { if (toolCall2.function.arguments.length > 0) { controller.enqueue({ type: "tool-call-delta", toolCallType: "function", toolCallId: toolCall2.id, toolName: toolCall2.function.name, argsTextDelta: toolCall2.function.arguments }); } if (chunkFEUXFWF5_cjs.isParsableJson(toolCall2.function.arguments)) { controller.enqueue({ type: "tool-call", toolCallType: "function", toolCallId: (_h = toolCall2.id) != null ? _h : chunkFEUXFWF5_cjs.generateId(), toolName: toolCall2.function.name, args: toolCall2.function.arguments }); toolCall2.hasFinished = true; } } continue; } const toolCall = toolCalls[index]; if (toolCall.hasFinished) { continue; } if (((_i = toolCallDelta.function) == null ? void 0 : _i.arguments) != null) { toolCall.function.arguments += (_k = (_j = toolCallDelta.function) == null ? void 0 : _j.arguments) != null ? _k : ""; } controller.enqueue({ type: "tool-call-delta", toolCallType: "function", toolCallId: toolCall.id, toolName: toolCall.function.name, argsTextDelta: (_l = toolCallDelta.function.arguments) != null ? _l : "" }); if (((_m = toolCall.function) == null ? void 0 : _m.name) != null && ((_n = toolCall.function) == null ? void 0 : _n.arguments) != null && chunkFEUXFWF5_cjs.isParsableJson(toolCall.function.arguments)) { controller.enqueue({ type: "tool-call", toolCallType: "function", toolCallId: (_o = toolCall.id) != null ? _o : chunkFEUXFWF5_cjs.generateId(), toolName: toolCall.function.name, args: toolCall.function.arguments }); toolCall.hasFinished = true; } } } }, flush(controller) { var _a, _b; controller.enqueue({ type: "finish", finishReason, usage: { promptTokens: (_a = usage.promptTokens) != null ? _a : NaN, completionTokens: (_b = usage.completionTokens) != null ? _b : NaN }, ...{} }); } }) ), rawCall: { rawPrompt, rawSettings }, rawResponse: { headers: responseHeaders }, warnings, request: { body } }; } }; var groqChatResponseSchema = zod.z.object({ id: zod.z.string().nullish(), created: zod.z.number().nullish(), model: zod.z.string().nullish(), choices: zod.z.array( zod.z.object({ message: zod.z.object({ content: zod.z.string().nullish(), reasoning: zod.z.string().nullish(), tool_calls: zod.z.array( zod.z.object({ id: zod.z.string().nullish(), type: zod.z.literal("function"), function: zod.z.object({ name: zod.z.string(), arguments: zod.z.string() }) }) ).nullish() }), index: zod.z.number(), finish_reason: zod.z.string().nullish() }) ), usage: zod.z.object({ prompt_tokens: zod.z.number().nullish(), completion_tokens: zod.z.number().nullish() }).nullish() }); var groqChatChunkSchema = zod.z.union([ zod.z.object({ id: zod.z.string().nullish(), created: zod.z.number().nullish(), model: zod.z.string().nullish(), choices: zod.z.array( zod.z.object({ delta: zod.z.object({ content: zod.z.string().nullish(), reasoning: zod.z.string().nullish(), tool_calls: zod.z.array( zod.z.object({ index: zod.z.number(), id: zod.z.string().nullish(), type: zod.z.literal("function").optional(), function: zod.z.object({ name: zod.z.string().nullish(), arguments: zod.z.string().nullish() }) }) ).nullish() }).nullish(), finish_reason: zod.z.string().nullable().optional(), index: zod.z.number() }) ), x_groq: zod.z.object({ usage: zod.z.object({ prompt_tokens: zod.z.number().nullish(), completion_tokens: zod.z.number().nullish() }).nullish() }).nullish() }), groqErrorDataSchema ]); var groqProviderOptionsSchema = zod.z.object({ language: zod.z.string().nullish(), prompt: zod.z.string().nullish(), responseFormat: zod.z.string().nullish(), temperature: zod.z.number().min(0).max(1).nullish(), timestampGranularities: zod.z.array(zod.z.string()).nullish() }); var GroqTranscriptionModel = class { constructor(modelId, config) { this.modelId = modelId; this.config = config; this.specificationVersion = "v1"; } get provider() { return this.config.provider; } getArgs({ audio, mediaType, providerOptions }) { var _a, _b, _c, _d, _e; const warnings = []; const groqOptions = chunkFEUXFWF5_cjs.parseProviderOptions({ provider: "groq", providerOptions, schema: groqProviderOptionsSchema }); const formData = new FormData(); const blob = audio instanceof Uint8Array ? new Blob([audio]) : new Blob([chunkFEUXFWF5_cjs.convertBase64ToUint8Array(audio)]); formData.append("model", this.modelId); formData.append("file", new File([blob], "audio", { type: mediaType })); if (groqOptions) { const transcriptionModelOptions = { language: (_a = groqOptions.language) != null ? _a : void 0, prompt: (_b = groqOptions.prompt) != null ? _b : void 0, response_format: (_c = groqOptions.responseFormat) != null ? _c : void 0, temperature: (_d = groqOptions.temperature) != null ? _d : void 0, timestamp_granularities: (_e = groqOptions.timestampGranularities) != null ? _e : void 0 }; for (const key in transcriptionModelOptions) { const value = transcriptionModelOptions[key]; if (value !== void 0) { formData.append(key, String(value)); } } } return { formData, warnings }; } async doGenerate(options) { var _a, _b, _c, _d, _e; const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date(); const { formData, warnings } = this.getArgs(options); const { value: response, responseHeaders, rawValue: rawResponse } = await chunkFEUXFWF5_cjs.postFormDataToApi({ url: this.config.url({ path: "/audio/transcriptions", modelId: this.modelId }), headers: chunkFEUXFWF5_cjs.combineHeaders(this.config.headers(), options.headers), formData, failedResponseHandler: groqFailedResponseHandler, successfulResponseHandler: chunkFEUXFWF5_cjs.createJsonResponseHandler( groqTranscriptionResponseSchema ), abortSignal: options.abortSignal, fetch: this.config.fetch }); return { text: response.text, segments: (_e = (_d = response.segments) == null ? void 0 : _d.map((segment) => ({ text: segment.text, startSecond: segment.start, endSecond: segment.end }))) != null ? _e : [], language: response.language, durationInSeconds: response.duration, warnings, response: { timestamp: currentDate, modelId: this.modelId, headers: responseHeaders, body: rawResponse } }; } }; var groqTranscriptionResponseSchema = zod.z.object({ task: zod.z.string(), language: zod.z.string(), duration: zod.z.number(), text: zod.z.string(), segments: zod.z.array( zod.z.object({ id: zod.z.number(), seek: zod.z.number(), start: zod.z.number(), end: zod.z.number(), text: zod.z.string(), tokens: zod.z.array(zod.z.number()), temperature: zod.z.number(), avg_logprob: zod.z.number(), compression_ratio: zod.z.number(), no_speech_prob: zod.z.number() }) ), x_groq: zod.z.object({ id: zod.z.string() }) }); function createGroq(options = {}) { var _a; const baseURL = (_a = chunkFEUXFWF5_cjs.withoutTrailingSlash(options.baseURL)) != null ? _a : "https://api.groq.com/openai/v1"; const getHeaders = () => ({ Authorization: `Bearer ${chunkFEUXFWF5_cjs.loadApiKey({ apiKey: options.apiKey, environmentVariableName: "GROQ_API_KEY", description: "Groq" })}`, ...options.headers }); const createChatModel = (modelId, settings = {}) => new GroqChatLanguageModel(modelId, settings, { provider: "groq.chat", url: ({ path }) => `${baseURL}${path}`, headers: getHeaders, fetch: options.fetch }); const createLanguageModel = (modelId, settings) => { if (new.target) { throw new Error( "The Groq model function cannot be called with the new keyword." ); } return createChatModel(modelId, settings); }; const createTranscriptionModel = (modelId) => { return new GroqTranscriptionModel(modelId, { provider: "groq.transcription", url: ({ path }) => `${baseURL}${path}`, headers: getHeaders, fetch: options.fetch }); }; const provider = function(modelId, settings) { return createLanguageModel(modelId, settings); }; provider.languageModel = createLanguageModel; provider.chat = createChatModel; provider.textEmbeddingModel = (modelId) => { throw new chunkFEUXFWF5_cjs.NoSuchModelError({ modelId, modelType: "textEmbeddingModel" }); }; provider.transcription = createTranscriptionModel; return provider; } var groq = createGroq(); exports.createGroq = createGroq; exports.groq = groq; //# sourceMappingURL=dist-6VCC76Q3.cjs.map //# sourceMappingURL=dist-6VCC76Q3.cjs.map