@crewai-ts/core 0.1.6 → 0.1.10
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/dist/index.cjs +346 -8
- package/dist/index.js +346 -8
- package/dist/openai-completion.d.ts +3 -0
- package/dist/provider-completions.d.ts +3 -0
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -30289,6 +30289,28 @@ function createLLM(llmValue = null, env = process.env) {
|
|
|
30289
30289
|
if (apiBase !== void 0) {
|
|
30290
30290
|
options.api_base = apiBase;
|
|
30291
30291
|
}
|
|
30292
|
+
const providerOptions = options;
|
|
30293
|
+
const llmOptions = llmValue;
|
|
30294
|
+
for (const key of [
|
|
30295
|
+
"api",
|
|
30296
|
+
"instructions",
|
|
30297
|
+
"store",
|
|
30298
|
+
"previousResponseId",
|
|
30299
|
+
"previous_response_id",
|
|
30300
|
+
"include",
|
|
30301
|
+
"builtinTools",
|
|
30302
|
+
"builtin_tools",
|
|
30303
|
+
"parseToolOutputs",
|
|
30304
|
+
"parse_tool_outputs",
|
|
30305
|
+
"autoChain",
|
|
30306
|
+
"auto_chain",
|
|
30307
|
+
"autoChainReasoning",
|
|
30308
|
+
"auto_chain_reasoning"
|
|
30309
|
+
]) {
|
|
30310
|
+
if (llmOptions[key] !== void 0) {
|
|
30311
|
+
providerOptions[key] = llmOptions[key];
|
|
30312
|
+
}
|
|
30313
|
+
}
|
|
30292
30314
|
return createConfiguredLLM(options);
|
|
30293
30315
|
}
|
|
30294
30316
|
var create_llm = createLLM;
|
|
@@ -45517,8 +45539,13 @@ var OpenAICompletion = class _OpenAICompletion extends ConfiguredLLM {
|
|
|
45517
45539
|
this.responseChainId = this.previousResponseId;
|
|
45518
45540
|
this.reasoningChainItems = null;
|
|
45519
45541
|
}
|
|
45520
|
-
call(messages, options) {
|
|
45521
|
-
|
|
45542
|
+
async call(messages, options) {
|
|
45543
|
+
if (this.stream) {
|
|
45544
|
+
throw new Error("OpenAI streaming responses are not supported by the built-in fetch transport yet.");
|
|
45545
|
+
}
|
|
45546
|
+
const tools = options?.tools ?? null;
|
|
45547
|
+
const responseModel = options?.responseModel ?? null;
|
|
45548
|
+
return this.api === "responses" ? await this.callResponses(messages, tools, responseModel) : await this.callChatCompletions(messages, tools);
|
|
45522
45549
|
}
|
|
45523
45550
|
async acall(messages, options) {
|
|
45524
45551
|
return await super.acall(messages, options);
|
|
@@ -45655,6 +45682,79 @@ ${message.content}` : message.content;
|
|
|
45655
45682
|
_prepare_responses_params(messages, tools = null, responseModel = null) {
|
|
45656
45683
|
return this.prepareResponsesParams(messages, tools, responseModel);
|
|
45657
45684
|
}
|
|
45685
|
+
async callChatCompletions(messages, tools) {
|
|
45686
|
+
const params = this.prepareCompletionParams(this.formatMessages(messages), tools);
|
|
45687
|
+
const response = await this.fetchOpenAI("chat/completions", params);
|
|
45688
|
+
const usage = this.extractOpenAITokenUsage(response);
|
|
45689
|
+
if (usage.total_tokens !== 0) {
|
|
45690
|
+
this.trackTokenUsageInternal(usage);
|
|
45691
|
+
}
|
|
45692
|
+
const choices = readObject(response).choices;
|
|
45693
|
+
const firstChoice = Array.isArray(choices) ? readObject(choices[0]) : {};
|
|
45694
|
+
const message = readObject(firstChoice.message);
|
|
45695
|
+
const toolCalls = Array.isArray(message.tool_calls) ? message.tool_calls : [];
|
|
45696
|
+
if (toolCalls.length > 0) {
|
|
45697
|
+
return toolCalls;
|
|
45698
|
+
}
|
|
45699
|
+
return typeof message.content === "string" ? message.content : "";
|
|
45700
|
+
}
|
|
45701
|
+
async callResponses(messages, tools, responseModel) {
|
|
45702
|
+
const params = this.prepareResponsesParams(this.formatMessages(messages), tools, responseModel);
|
|
45703
|
+
const response = await this.fetchOpenAI("responses", params);
|
|
45704
|
+
const usage = this.extractResponsesTokenUsage(response);
|
|
45705
|
+
if (usage.total_tokens !== 0) {
|
|
45706
|
+
this.trackTokenUsageInternal(usage);
|
|
45707
|
+
}
|
|
45708
|
+
const responseId = stringOrNull2(readObject(response).id);
|
|
45709
|
+
if (this.autoChain && responseId) {
|
|
45710
|
+
this.responseChainId = responseId;
|
|
45711
|
+
}
|
|
45712
|
+
const reasoningItems = this.extractReasoningItems(response);
|
|
45713
|
+
if (this.autoChainReasoning && reasoningItems.length > 0) {
|
|
45714
|
+
this.reasoningChainItems = reasoningItems;
|
|
45715
|
+
}
|
|
45716
|
+
const result = this.extractBuiltinToolOutputs(response);
|
|
45717
|
+
if (result.function_calls.length > 0 || result.hasToolOutputs() || result.hasReasoning()) {
|
|
45718
|
+
return result;
|
|
45719
|
+
}
|
|
45720
|
+
return result.text;
|
|
45721
|
+
}
|
|
45722
|
+
async fetchOpenAI(path, body) {
|
|
45723
|
+
const clientParams = this.getClientParams();
|
|
45724
|
+
const apiKey = stringOrNull2(clientParams.api_key);
|
|
45725
|
+
if (!apiKey) {
|
|
45726
|
+
throw new Error("OpenAI API key is required. Pass api_key when constructing OpenAICompletion.");
|
|
45727
|
+
}
|
|
45728
|
+
const baseUrl = (stringOrNull2(clientParams.base_url) ?? "https://api.openai.com/v1").replace(/\/$/, "");
|
|
45729
|
+
const defaultQuery = readObject(clientParams.default_query);
|
|
45730
|
+
const query = new URLSearchParams();
|
|
45731
|
+
for (const [key, value] of Object.entries(defaultQuery)) {
|
|
45732
|
+
if (value !== void 0 && value !== null) {
|
|
45733
|
+
query.set(key, typeof value === "string" || typeof value === "number" || typeof value === "boolean" ? String(value) : JSON.stringify(value));
|
|
45734
|
+
}
|
|
45735
|
+
}
|
|
45736
|
+
const url = `${baseUrl}/${path}${query.size > 0 ? `?${query.toString()}` : ""}`;
|
|
45737
|
+
const defaultHeaders = Object.fromEntries(
|
|
45738
|
+
Object.entries(readObject(clientParams.default_headers)).filter(([, value]) => typeof value === "string")
|
|
45739
|
+
);
|
|
45740
|
+
const response = await fetch(url, {
|
|
45741
|
+
method: "POST",
|
|
45742
|
+
headers: {
|
|
45743
|
+
...defaultHeaders,
|
|
45744
|
+
Authorization: `Bearer ${apiKey}`,
|
|
45745
|
+
"Content-Type": "application/json",
|
|
45746
|
+
...typeof clientParams.organization === "string" ? { "OpenAI-Organization": clientParams.organization } : {},
|
|
45747
|
+
...typeof clientParams.project === "string" ? { "OpenAI-Project": clientParams.project } : {}
|
|
45748
|
+
},
|
|
45749
|
+
body: JSON.stringify(body)
|
|
45750
|
+
});
|
|
45751
|
+
const payload = await response.json().catch(() => ({}));
|
|
45752
|
+
if (!response.ok) {
|
|
45753
|
+
const error = readObject(readObject(payload).error);
|
|
45754
|
+
throw new Error(stringOrNull2(error.message) ?? `OpenAI request failed with HTTP ${response.status.toString()}.`);
|
|
45755
|
+
}
|
|
45756
|
+
return payload;
|
|
45757
|
+
}
|
|
45658
45758
|
convertToolsForInterference(tools) {
|
|
45659
45759
|
return convertToolsToOpenAISchema(tools)[0];
|
|
45660
45760
|
}
|
|
@@ -45854,6 +45954,18 @@ ${message.content}` : message.content;
|
|
|
45854
45954
|
name: item.name,
|
|
45855
45955
|
arguments: item.arguments
|
|
45856
45956
|
});
|
|
45957
|
+
} else if (type2 === "message") {
|
|
45958
|
+
const content = Array.isArray(item.content) ? item.content : [];
|
|
45959
|
+
const textParts = [];
|
|
45960
|
+
for (const rawContent of content) {
|
|
45961
|
+
const contentItem = readObject(rawContent);
|
|
45962
|
+
if (contentItem.type === "output_text" && typeof contentItem.text === "string") {
|
|
45963
|
+
textParts.push(contentItem.text);
|
|
45964
|
+
}
|
|
45965
|
+
}
|
|
45966
|
+
if (!result.text && textParts.length > 0) {
|
|
45967
|
+
result.text = textParts.join("");
|
|
45968
|
+
}
|
|
45857
45969
|
}
|
|
45858
45970
|
}
|
|
45859
45971
|
return result;
|
|
@@ -46135,6 +46247,7 @@ var OpenAICompatibleCompletion = class _OpenAICompatibleCompletion extends OpenA
|
|
|
46135
46247
|
return _OpenAICompatibleCompletion.resolveHeaders(headers, config);
|
|
46136
46248
|
}
|
|
46137
46249
|
};
|
|
46250
|
+
registerLLMProviderFactory("openai", (options) => new OpenAICompletion(options));
|
|
46138
46251
|
for (const provider of Object.keys(OPENAI_COMPATIBLE_PROVIDERS)) {
|
|
46139
46252
|
registerLLMProviderFactory(provider, (options) => {
|
|
46140
46253
|
const { logprobs: _logprobs, ...compatibleOptions } = options;
|
|
@@ -46300,6 +46413,8 @@ var AnthropicCompletion = class _AnthropicCompletion extends ConfiguredLLM {
|
|
|
46300
46413
|
stop: options.stop,
|
|
46301
46414
|
stopSequences: options.stopSequences,
|
|
46302
46415
|
stop_sequences: options.stop_sequences,
|
|
46416
|
+
responseFormat: options.responseFormat,
|
|
46417
|
+
response_format: options.response_format,
|
|
46303
46418
|
maxTokens: options.maxTokens ?? options.max_tokens ?? 4096,
|
|
46304
46419
|
timeout: options.timeout ?? null
|
|
46305
46420
|
}));
|
|
@@ -46324,8 +46439,71 @@ var AnthropicCompletion = class _AnthropicCompletion extends ConfiguredLLM {
|
|
|
46324
46439
|
this.previousThinkingBlocks = [];
|
|
46325
46440
|
this._previous_thinking_blocks = this.previousThinkingBlocks;
|
|
46326
46441
|
}
|
|
46327
|
-
call(messages, options) {
|
|
46328
|
-
|
|
46442
|
+
async call(messages, options) {
|
|
46443
|
+
const apiKey = this.apiKey;
|
|
46444
|
+
if (!apiKey) {
|
|
46445
|
+
throw new Error("Anthropic API key required. Pass api_key when constructing AnthropicCompletion.");
|
|
46446
|
+
}
|
|
46447
|
+
if (this.stream) {
|
|
46448
|
+
throw new Error("Anthropic streaming responses are not supported by the built-in HTTP transport yet.");
|
|
46449
|
+
}
|
|
46450
|
+
const [formattedMessages, systemMessage] = this.formatMessagesForAnthropic(messages);
|
|
46451
|
+
const availableFunctions = options?.availableFunctions ?? options?.available_functions ?? null;
|
|
46452
|
+
const params = this.prepareCompletionParams(
|
|
46453
|
+
formattedMessages,
|
|
46454
|
+
systemMessage,
|
|
46455
|
+
options?.tools ?? null,
|
|
46456
|
+
availableFunctions
|
|
46457
|
+
);
|
|
46458
|
+
const schema = anthropicResponseSchema(options?.responseModel ?? this.responseFormat);
|
|
46459
|
+
if (schema) {
|
|
46460
|
+
const tools = Array.isArray(params.tools) ? params.tools.filter((tool2) => typeof tool2 === "object" && tool2 !== null) : [];
|
|
46461
|
+
tools.push({
|
|
46462
|
+
name: STRUCTURED_OUTPUT_TOOL_NAME,
|
|
46463
|
+
description: "Use this tool to provide your final structured response in the required JSON shape.",
|
|
46464
|
+
input_schema: sanitizeToolParamsForAnthropicStrict(structuredClone(schema)),
|
|
46465
|
+
strict: true
|
|
46466
|
+
});
|
|
46467
|
+
params.tools = tools;
|
|
46468
|
+
params.tool_choice = { type: "tool", name: STRUCTURED_OUTPUT_TOOL_NAME };
|
|
46469
|
+
}
|
|
46470
|
+
const baseUrl = this.baseUrl ?? "https://api.anthropic.com";
|
|
46471
|
+
const response = await fetch(`${baseUrl.replace(/\/$/, "")}/v1/messages`, {
|
|
46472
|
+
method: "POST",
|
|
46473
|
+
headers: {
|
|
46474
|
+
"content-type": "application/json",
|
|
46475
|
+
"x-api-key": apiKey,
|
|
46476
|
+
"anthropic-version": "2023-06-01"
|
|
46477
|
+
},
|
|
46478
|
+
body: JSON.stringify(params),
|
|
46479
|
+
...options?.signal ? { signal: options.signal } : {}
|
|
46480
|
+
});
|
|
46481
|
+
const body = await response.json().catch(() => ({}));
|
|
46482
|
+
if (!response.ok) {
|
|
46483
|
+
const error = readObject2(readObject2(body).error);
|
|
46484
|
+
throw new Error(scalarToString(error.message) ?? `Anthropic request failed with HTTP ${response.status.toString()}.`);
|
|
46485
|
+
}
|
|
46486
|
+
const usage = this.extractAnthropicTokenUsage(body);
|
|
46487
|
+
if (usage.total_tokens !== 0) {
|
|
46488
|
+
this.trackTokenUsageInternal(usage);
|
|
46489
|
+
}
|
|
46490
|
+
const thinkingBlocks = _AnthropicCompletion.extractThinkingBlocksFromResponse(body);
|
|
46491
|
+
if (thinkingBlocks.length > 0) {
|
|
46492
|
+
this.previousThinkingBlocks = thinkingBlocks;
|
|
46493
|
+
this._previous_thinking_blocks = this.previousThinkingBlocks;
|
|
46494
|
+
}
|
|
46495
|
+
const structuredOutput = _AnthropicCompletion.extractStructuredOutputFromResponse(body);
|
|
46496
|
+
if (structuredOutput) {
|
|
46497
|
+
return structuredOutput;
|
|
46498
|
+
}
|
|
46499
|
+
const toolUses = _AnthropicCompletion.extractToolUsesFromResponse(body);
|
|
46500
|
+
if (toolUses.length > 0) {
|
|
46501
|
+
if (availableFunctions && Object.keys(availableFunctions).length > 0) {
|
|
46502
|
+
return await this.executeFirstTool(toolUses, availableFunctions);
|
|
46503
|
+
}
|
|
46504
|
+
return toolUses;
|
|
46505
|
+
}
|
|
46506
|
+
return this.applyStopWords(anthropicResponseText(body));
|
|
46329
46507
|
}
|
|
46330
46508
|
async acall(messages, options) {
|
|
46331
46509
|
return await super.acall(messages, options);
|
|
@@ -46839,8 +47017,32 @@ var BedrockCompletion = class _BedrockCompletion extends ConfiguredLLM {
|
|
|
46839
47017
|
this.additional_model_response_field_paths = this.additionalModelResponseFieldPaths;
|
|
46840
47018
|
this.interceptor = null;
|
|
46841
47019
|
}
|
|
46842
|
-
call(messages, options) {
|
|
46843
|
-
|
|
47020
|
+
async call(messages, options) {
|
|
47021
|
+
if (this.stream) {
|
|
47022
|
+
throw new Error("Bedrock streaming responses are not supported by the built-in client transport yet.");
|
|
47023
|
+
}
|
|
47024
|
+
const client = this.getConverseClient();
|
|
47025
|
+
const { messages: formattedMessages, body } = this.prepareConverseRequestBody(
|
|
47026
|
+
messages,
|
|
47027
|
+
options?.tools ?? null
|
|
47028
|
+
);
|
|
47029
|
+
const request = {
|
|
47030
|
+
modelId: this.model,
|
|
47031
|
+
messages: formattedMessages,
|
|
47032
|
+
...body
|
|
47033
|
+
};
|
|
47034
|
+
const response = await client.converse(request);
|
|
47035
|
+
this.trackTokenUsageInternal(readObject2(readObject2(response).usage));
|
|
47036
|
+
const toolUses = _BedrockCompletion.extractToolUsesFromResponse(response);
|
|
47037
|
+
const availableFunctions = options?.availableFunctions ?? options?.available_functions ?? null;
|
|
47038
|
+
if (toolUses.length > 0) {
|
|
47039
|
+
if (availableFunctions && Object.keys(availableFunctions).length > 0) {
|
|
47040
|
+
const executed = await this.executeToolUseAndPrepareMessages(formattedMessages, toolUses[0], availableFunctions);
|
|
47041
|
+
return executed.result;
|
|
47042
|
+
}
|
|
47043
|
+
return toolUses;
|
|
47044
|
+
}
|
|
47045
|
+
return bedrockResponseText(response);
|
|
46844
47046
|
}
|
|
46845
47047
|
async acall(messages, options) {
|
|
46846
47048
|
return await super.acall(messages, options);
|
|
@@ -47263,6 +47465,24 @@ ${content}` : content;
|
|
|
47263
47465
|
const model = this.model.toLowerCase();
|
|
47264
47466
|
return model.includes("anthropic") || model.includes("claude");
|
|
47265
47467
|
}
|
|
47468
|
+
getConverseClient() {
|
|
47469
|
+
const direct = readObject2(this.session).converse;
|
|
47470
|
+
if (typeof direct === "function") {
|
|
47471
|
+
return { converse: (request) => Reflect.apply(direct, this.session, [request]) };
|
|
47472
|
+
}
|
|
47473
|
+
const clientFactory = readObject2(this.session).client;
|
|
47474
|
+
if (typeof clientFactory === "function") {
|
|
47475
|
+
const client = Reflect.apply(clientFactory, this.session, [
|
|
47476
|
+
"bedrock-runtime",
|
|
47477
|
+
this.regionName ? { region: this.regionName } : void 0
|
|
47478
|
+
]);
|
|
47479
|
+
const converse = readObject2(client).converse;
|
|
47480
|
+
if (typeof converse === "function") {
|
|
47481
|
+
return { converse: (request) => Reflect.apply(converse, client, [request]) };
|
|
47482
|
+
}
|
|
47483
|
+
}
|
|
47484
|
+
throw new Error("Bedrock live calls require a session/client with a converse(request) method.");
|
|
47485
|
+
}
|
|
47266
47486
|
};
|
|
47267
47487
|
var GeminiCompletion = class _GeminiCompletion extends ConfiguredLLM {
|
|
47268
47488
|
project;
|
|
@@ -47340,7 +47560,7 @@ var GeminiCompletion = class _GeminiCompletion extends ConfiguredLLM {
|
|
|
47340
47560
|
}
|
|
47341
47561
|
call(messages, options) {
|
|
47342
47562
|
if (this.useVertexai) {
|
|
47343
|
-
return
|
|
47563
|
+
return this.callVertexAI(messages, options);
|
|
47344
47564
|
}
|
|
47345
47565
|
const apiKey = this.apiKey ?? process.env.GOOGLE_API_KEY ?? process.env.GEMINI_API_KEY ?? null;
|
|
47346
47566
|
if (!apiKey) {
|
|
@@ -47394,6 +47614,58 @@ var GeminiCompletion = class _GeminiCompletion extends ConfiguredLLM {
|
|
|
47394
47614
|
);
|
|
47395
47615
|
});
|
|
47396
47616
|
}
|
|
47617
|
+
async callVertexAI(messages, options) {
|
|
47618
|
+
if (!this.project) {
|
|
47619
|
+
throw new Error("Vertex AI Gemini calls require a project.");
|
|
47620
|
+
}
|
|
47621
|
+
const clientParams = readObject2(this.clientParams);
|
|
47622
|
+
const accessToken = scalarToString(clientParams.access_token ?? clientParams.accessToken ?? this.apiKey);
|
|
47623
|
+
if (!accessToken) {
|
|
47624
|
+
throw new Error("Vertex AI Gemini calls require client_params.access_token or api_key.");
|
|
47625
|
+
}
|
|
47626
|
+
const [contents, systemInstruction] = this.formatMessagesForGemini(messages);
|
|
47627
|
+
const tools = options?.tools ?? null;
|
|
47628
|
+
const generationConfig = this.prepareGenerationConfig(
|
|
47629
|
+
systemInstruction,
|
|
47630
|
+
tools,
|
|
47631
|
+
options?.responseModel ?? null
|
|
47632
|
+
);
|
|
47633
|
+
const requestBody = readObject2(generationConfig);
|
|
47634
|
+
const generationConfigBody = { ...generationConfig };
|
|
47635
|
+
delete generationConfigBody.system_instruction;
|
|
47636
|
+
delete generationConfigBody.tools;
|
|
47637
|
+
delete generationConfigBody.safety_settings;
|
|
47638
|
+
const model = this.model.replace(/^(?:gemini|google)\//, "");
|
|
47639
|
+
const baseUrl = this.baseUrl ?? `https://${this.location}-aiplatform.googleapis.com/v1`;
|
|
47640
|
+
const url = `${baseUrl.replace(/\/$/, "")}/projects/${encodeURIComponent(this.project)}/locations/${encodeURIComponent(this.location)}/publishers/google/models/${encodeURIComponent(model)}:generateContent`;
|
|
47641
|
+
const response = await fetch(url, {
|
|
47642
|
+
method: "POST",
|
|
47643
|
+
headers: {
|
|
47644
|
+
Authorization: `Bearer ${accessToken}`,
|
|
47645
|
+
"Content-Type": "application/json"
|
|
47646
|
+
},
|
|
47647
|
+
body: JSON.stringify({
|
|
47648
|
+
contents,
|
|
47649
|
+
...Object.keys(generationConfigBody).length > 0 ? { generationConfig: generationConfigBody } : {},
|
|
47650
|
+
..."system_instruction" in requestBody ? { systemInstruction: requestBody.system_instruction } : {},
|
|
47651
|
+
..."tools" in requestBody ? { tools: requestBody.tools } : {},
|
|
47652
|
+
..."safety_settings" in requestBody ? { safetySettings: requestBody.safety_settings } : {}
|
|
47653
|
+
}),
|
|
47654
|
+
...options?.signal ? { signal: options.signal } : {}
|
|
47655
|
+
});
|
|
47656
|
+
const body = await response.json().catch(() => ({}));
|
|
47657
|
+
if (!response.ok) {
|
|
47658
|
+
const error = readObject2(readObject2(body).error);
|
|
47659
|
+
throw new Error(scalarToString(error.message) ?? `Vertex AI Gemini request failed with HTTP ${response.status.toString()}.`);
|
|
47660
|
+
}
|
|
47661
|
+
return await this.processResponseWithTools(
|
|
47662
|
+
body,
|
|
47663
|
+
contents,
|
|
47664
|
+
options?.availableFunctions ?? options?.available_functions ?? null,
|
|
47665
|
+
null,
|
|
47666
|
+
geminiMaxToolRounds(options)
|
|
47667
|
+
);
|
|
47668
|
+
}
|
|
47397
47669
|
async acall(messages, options) {
|
|
47398
47670
|
return await this.call(this.formatMessages(messages), options);
|
|
47399
47671
|
}
|
|
@@ -48401,7 +48673,7 @@ var AzureCompletion = class _AzureCompletion extends ConfiguredLLM {
|
|
|
48401
48673
|
if (this._responses_delegate) {
|
|
48402
48674
|
return this._responses_delegate.call(messages, options);
|
|
48403
48675
|
}
|
|
48404
|
-
return
|
|
48676
|
+
return this.callChatCompletions(messages, options);
|
|
48405
48677
|
}
|
|
48406
48678
|
async acall(messages, options) {
|
|
48407
48679
|
if (this._responses_delegate) {
|
|
@@ -48615,6 +48887,50 @@ var AzureCompletion = class _AzureCompletion extends ConfiguredLLM {
|
|
|
48615
48887
|
_prepare_completion_params(messages, tools = null) {
|
|
48616
48888
|
return this.prepareCompletionParams(messages, tools);
|
|
48617
48889
|
}
|
|
48890
|
+
async callChatCompletions(messages, options) {
|
|
48891
|
+
if (this.stream) {
|
|
48892
|
+
throw new Error("Azure streaming responses are not supported by the built-in fetch transport yet.");
|
|
48893
|
+
}
|
|
48894
|
+
const client = this.getSyncClient();
|
|
48895
|
+
const endpoint = scalarToString(client.endpoint);
|
|
48896
|
+
const apiKey = scalarToString(client.api_key);
|
|
48897
|
+
if (!endpoint) {
|
|
48898
|
+
throw new Error("Azure endpoint is required");
|
|
48899
|
+
}
|
|
48900
|
+
if (!apiKey) {
|
|
48901
|
+
throw new Error("Azure API key is required");
|
|
48902
|
+
}
|
|
48903
|
+
const params = this.prepareCompletionParams(
|
|
48904
|
+
this.formatMessages(messages),
|
|
48905
|
+
options?.tools ?? null
|
|
48906
|
+
);
|
|
48907
|
+
const url = azureChatCompletionsUrl(endpoint, this.model, this.apiVersion);
|
|
48908
|
+
const response = await fetch(url, {
|
|
48909
|
+
method: "POST",
|
|
48910
|
+
headers: {
|
|
48911
|
+
"Content-Type": "application/json",
|
|
48912
|
+
"api-key": apiKey
|
|
48913
|
+
},
|
|
48914
|
+
body: JSON.stringify(params)
|
|
48915
|
+
});
|
|
48916
|
+
const payload = await response.json().catch(() => ({}));
|
|
48917
|
+
if (!response.ok) {
|
|
48918
|
+
const error = readObject2(readObject2(payload).error);
|
|
48919
|
+
throw new Error(scalarToString(error.message) ?? `Azure completion request failed with HTTP ${response.status.toString()}.`);
|
|
48920
|
+
}
|
|
48921
|
+
const usage = this.extractAzureTokenUsage(payload);
|
|
48922
|
+
if (usage.total_tokens !== 0) {
|
|
48923
|
+
this.trackTokenUsageInternal(usage);
|
|
48924
|
+
}
|
|
48925
|
+
const choices = readObject2(payload).choices;
|
|
48926
|
+
const firstChoice = Array.isArray(choices) ? readObject2(choices[0]) : {};
|
|
48927
|
+
const message = readObject2(firstChoice.message);
|
|
48928
|
+
const toolCalls = Array.isArray(message.tool_calls) ? message.tool_calls : [];
|
|
48929
|
+
if (toolCalls.length > 0) {
|
|
48930
|
+
return toolCalls;
|
|
48931
|
+
}
|
|
48932
|
+
return typeof message.content === "string" ? message.content : "";
|
|
48933
|
+
}
|
|
48618
48934
|
prepareResponsesParams(messages, tools = null, responseModel = null) {
|
|
48619
48935
|
if (!this._responses_delegate) {
|
|
48620
48936
|
throw new Error("Azure Responses API is only available when api is set to 'responses'.");
|
|
@@ -48912,15 +49228,29 @@ function geminiResponseSchema(value) {
|
|
|
48912
49228
|
const schema = schemaProvider.model_json_schema?.() ?? schemaProvider.modelJsonSchema?.() ?? schemaProvider.schema;
|
|
48913
49229
|
return schema && typeof schema === "object" && !Array.isArray(schema) ? schema : null;
|
|
48914
49230
|
}
|
|
49231
|
+
function anthropicResponseSchema(value) {
|
|
49232
|
+
if (!value || typeof value !== "object") {
|
|
49233
|
+
return null;
|
|
49234
|
+
}
|
|
49235
|
+
const schemaProvider = value;
|
|
49236
|
+
const schema = schemaProvider.model_json_schema?.() ?? schemaProvider.modelJsonSchema?.() ?? schemaProvider.schema;
|
|
49237
|
+
return schema && typeof schema === "object" && !Array.isArray(schema) ? schema : null;
|
|
49238
|
+
}
|
|
48915
49239
|
function anthropicResponseContent(response) {
|
|
48916
49240
|
const content = readObject2(response).content;
|
|
48917
49241
|
return Array.isArray(content) ? content : [];
|
|
48918
49242
|
}
|
|
49243
|
+
function anthropicResponseText(response) {
|
|
49244
|
+
return anthropicResponseContent(response).map((block) => scalarToString(readObject2(block).text)).filter((text) => text !== null).join("");
|
|
49245
|
+
}
|
|
48919
49246
|
function bedrockResponseContent(response) {
|
|
48920
49247
|
const output4 = readObject2(readObject2(response).output);
|
|
48921
49248
|
const message = readObject2(output4.message);
|
|
48922
49249
|
return Array.isArray(message.content) ? message.content : [];
|
|
48923
49250
|
}
|
|
49251
|
+
function bedrockResponseText(response) {
|
|
49252
|
+
return bedrockResponseContent(response).map((block) => scalarToString(readObject2(block).text)).filter((text) => text !== null).join("");
|
|
49253
|
+
}
|
|
48924
49254
|
function isAzureOpenAIEndpoint(endpoint) {
|
|
48925
49255
|
if (!endpoint) {
|
|
48926
49256
|
return false;
|
|
@@ -48951,6 +49281,14 @@ function azureResponsesBaseUrl(endpoint) {
|
|
|
48951
49281
|
return `${trimmed.replace(/\/openai(?:\/v1)?$/i, "")}/openai/v1/`;
|
|
48952
49282
|
}
|
|
48953
49283
|
}
|
|
49284
|
+
function azureChatCompletionsUrl(endpoint, model, apiVersion) {
|
|
49285
|
+
const version2 = encodeURIComponent(apiVersion ?? "2024-06-01");
|
|
49286
|
+
const trimmed = endpoint.replace(/\/+$/, "");
|
|
49287
|
+
const deploymentPath = "/openai/deployments/";
|
|
49288
|
+
const lower = trimmed.toLowerCase();
|
|
49289
|
+
const base = lower.includes(deploymentPath) ? trimmed : `${trimmed.replace(/\/openai(?:\/v1)?$/i, "")}${deploymentPath}${encodeURIComponent(azureResponsesModelName(model))}`;
|
|
49290
|
+
return `${base}/chat/completions?api-version=${version2}`;
|
|
49291
|
+
}
|
|
48954
49292
|
function geminiContextWindowSize(model) {
|
|
48955
49293
|
const windows = {
|
|
48956
49294
|
"gemini-3-pro-preview": 1048576,
|
package/dist/index.js
CHANGED
|
@@ -14006,6 +14006,28 @@ function createLLM(llmValue = null, env = process.env) {
|
|
|
14006
14006
|
if (apiBase !== void 0) {
|
|
14007
14007
|
options.api_base = apiBase;
|
|
14008
14008
|
}
|
|
14009
|
+
const providerOptions = options;
|
|
14010
|
+
const llmOptions = llmValue;
|
|
14011
|
+
for (const key of [
|
|
14012
|
+
"api",
|
|
14013
|
+
"instructions",
|
|
14014
|
+
"store",
|
|
14015
|
+
"previousResponseId",
|
|
14016
|
+
"previous_response_id",
|
|
14017
|
+
"include",
|
|
14018
|
+
"builtinTools",
|
|
14019
|
+
"builtin_tools",
|
|
14020
|
+
"parseToolOutputs",
|
|
14021
|
+
"parse_tool_outputs",
|
|
14022
|
+
"autoChain",
|
|
14023
|
+
"auto_chain",
|
|
14024
|
+
"autoChainReasoning",
|
|
14025
|
+
"auto_chain_reasoning"
|
|
14026
|
+
]) {
|
|
14027
|
+
if (llmOptions[key] !== void 0) {
|
|
14028
|
+
providerOptions[key] = llmOptions[key];
|
|
14029
|
+
}
|
|
14030
|
+
}
|
|
14009
14031
|
return createConfiguredLLM(options);
|
|
14010
14032
|
}
|
|
14011
14033
|
var create_llm = createLLM;
|
|
@@ -29180,8 +29202,13 @@ var OpenAICompletion = class _OpenAICompletion extends ConfiguredLLM {
|
|
|
29180
29202
|
this.responseChainId = this.previousResponseId;
|
|
29181
29203
|
this.reasoningChainItems = null;
|
|
29182
29204
|
}
|
|
29183
|
-
call(messages, options) {
|
|
29184
|
-
|
|
29205
|
+
async call(messages, options) {
|
|
29206
|
+
if (this.stream) {
|
|
29207
|
+
throw new Error("OpenAI streaming responses are not supported by the built-in fetch transport yet.");
|
|
29208
|
+
}
|
|
29209
|
+
const tools = options?.tools ?? null;
|
|
29210
|
+
const responseModel = options?.responseModel ?? null;
|
|
29211
|
+
return this.api === "responses" ? await this.callResponses(messages, tools, responseModel) : await this.callChatCompletions(messages, tools);
|
|
29185
29212
|
}
|
|
29186
29213
|
async acall(messages, options) {
|
|
29187
29214
|
return await super.acall(messages, options);
|
|
@@ -29318,6 +29345,79 @@ ${message.content}` : message.content;
|
|
|
29318
29345
|
_prepare_responses_params(messages, tools = null, responseModel = null) {
|
|
29319
29346
|
return this.prepareResponsesParams(messages, tools, responseModel);
|
|
29320
29347
|
}
|
|
29348
|
+
async callChatCompletions(messages, tools) {
|
|
29349
|
+
const params = this.prepareCompletionParams(this.formatMessages(messages), tools);
|
|
29350
|
+
const response = await this.fetchOpenAI("chat/completions", params);
|
|
29351
|
+
const usage = this.extractOpenAITokenUsage(response);
|
|
29352
|
+
if (usage.total_tokens !== 0) {
|
|
29353
|
+
this.trackTokenUsageInternal(usage);
|
|
29354
|
+
}
|
|
29355
|
+
const choices = readObject(response).choices;
|
|
29356
|
+
const firstChoice = Array.isArray(choices) ? readObject(choices[0]) : {};
|
|
29357
|
+
const message = readObject(firstChoice.message);
|
|
29358
|
+
const toolCalls = Array.isArray(message.tool_calls) ? message.tool_calls : [];
|
|
29359
|
+
if (toolCalls.length > 0) {
|
|
29360
|
+
return toolCalls;
|
|
29361
|
+
}
|
|
29362
|
+
return typeof message.content === "string" ? message.content : "";
|
|
29363
|
+
}
|
|
29364
|
+
async callResponses(messages, tools, responseModel) {
|
|
29365
|
+
const params = this.prepareResponsesParams(this.formatMessages(messages), tools, responseModel);
|
|
29366
|
+
const response = await this.fetchOpenAI("responses", params);
|
|
29367
|
+
const usage = this.extractResponsesTokenUsage(response);
|
|
29368
|
+
if (usage.total_tokens !== 0) {
|
|
29369
|
+
this.trackTokenUsageInternal(usage);
|
|
29370
|
+
}
|
|
29371
|
+
const responseId = stringOrNull2(readObject(response).id);
|
|
29372
|
+
if (this.autoChain && responseId) {
|
|
29373
|
+
this.responseChainId = responseId;
|
|
29374
|
+
}
|
|
29375
|
+
const reasoningItems = this.extractReasoningItems(response);
|
|
29376
|
+
if (this.autoChainReasoning && reasoningItems.length > 0) {
|
|
29377
|
+
this.reasoningChainItems = reasoningItems;
|
|
29378
|
+
}
|
|
29379
|
+
const result = this.extractBuiltinToolOutputs(response);
|
|
29380
|
+
if (result.function_calls.length > 0 || result.hasToolOutputs() || result.hasReasoning()) {
|
|
29381
|
+
return result;
|
|
29382
|
+
}
|
|
29383
|
+
return result.text;
|
|
29384
|
+
}
|
|
29385
|
+
async fetchOpenAI(path, body) {
|
|
29386
|
+
const clientParams = this.getClientParams();
|
|
29387
|
+
const apiKey = stringOrNull2(clientParams.api_key);
|
|
29388
|
+
if (!apiKey) {
|
|
29389
|
+
throw new Error("OpenAI API key is required. Pass api_key when constructing OpenAICompletion.");
|
|
29390
|
+
}
|
|
29391
|
+
const baseUrl = (stringOrNull2(clientParams.base_url) ?? "https://api.openai.com/v1").replace(/\/$/, "");
|
|
29392
|
+
const defaultQuery = readObject(clientParams.default_query);
|
|
29393
|
+
const query = new URLSearchParams();
|
|
29394
|
+
for (const [key, value] of Object.entries(defaultQuery)) {
|
|
29395
|
+
if (value !== void 0 && value !== null) {
|
|
29396
|
+
query.set(key, typeof value === "string" || typeof value === "number" || typeof value === "boolean" ? String(value) : JSON.stringify(value));
|
|
29397
|
+
}
|
|
29398
|
+
}
|
|
29399
|
+
const url = `${baseUrl}/${path}${query.size > 0 ? `?${query.toString()}` : ""}`;
|
|
29400
|
+
const defaultHeaders = Object.fromEntries(
|
|
29401
|
+
Object.entries(readObject(clientParams.default_headers)).filter(([, value]) => typeof value === "string")
|
|
29402
|
+
);
|
|
29403
|
+
const response = await fetch(url, {
|
|
29404
|
+
method: "POST",
|
|
29405
|
+
headers: {
|
|
29406
|
+
...defaultHeaders,
|
|
29407
|
+
Authorization: `Bearer ${apiKey}`,
|
|
29408
|
+
"Content-Type": "application/json",
|
|
29409
|
+
...typeof clientParams.organization === "string" ? { "OpenAI-Organization": clientParams.organization } : {},
|
|
29410
|
+
...typeof clientParams.project === "string" ? { "OpenAI-Project": clientParams.project } : {}
|
|
29411
|
+
},
|
|
29412
|
+
body: JSON.stringify(body)
|
|
29413
|
+
});
|
|
29414
|
+
const payload = await response.json().catch(() => ({}));
|
|
29415
|
+
if (!response.ok) {
|
|
29416
|
+
const error = readObject(readObject(payload).error);
|
|
29417
|
+
throw new Error(stringOrNull2(error.message) ?? `OpenAI request failed with HTTP ${response.status.toString()}.`);
|
|
29418
|
+
}
|
|
29419
|
+
return payload;
|
|
29420
|
+
}
|
|
29321
29421
|
convertToolsForInterference(tools) {
|
|
29322
29422
|
return convertToolsToOpenAISchema(tools)[0];
|
|
29323
29423
|
}
|
|
@@ -29517,6 +29617,18 @@ ${message.content}` : message.content;
|
|
|
29517
29617
|
name: item.name,
|
|
29518
29618
|
arguments: item.arguments
|
|
29519
29619
|
});
|
|
29620
|
+
} else if (type === "message") {
|
|
29621
|
+
const content = Array.isArray(item.content) ? item.content : [];
|
|
29622
|
+
const textParts = [];
|
|
29623
|
+
for (const rawContent of content) {
|
|
29624
|
+
const contentItem = readObject(rawContent);
|
|
29625
|
+
if (contentItem.type === "output_text" && typeof contentItem.text === "string") {
|
|
29626
|
+
textParts.push(contentItem.text);
|
|
29627
|
+
}
|
|
29628
|
+
}
|
|
29629
|
+
if (!result.text && textParts.length > 0) {
|
|
29630
|
+
result.text = textParts.join("");
|
|
29631
|
+
}
|
|
29520
29632
|
}
|
|
29521
29633
|
}
|
|
29522
29634
|
return result;
|
|
@@ -29798,6 +29910,7 @@ var OpenAICompatibleCompletion = class _OpenAICompatibleCompletion extends OpenA
|
|
|
29798
29910
|
return _OpenAICompatibleCompletion.resolveHeaders(headers, config);
|
|
29799
29911
|
}
|
|
29800
29912
|
};
|
|
29913
|
+
registerLLMProviderFactory("openai", (options) => new OpenAICompletion(options));
|
|
29801
29914
|
for (const provider of Object.keys(OPENAI_COMPATIBLE_PROVIDERS)) {
|
|
29802
29915
|
registerLLMProviderFactory(provider, (options) => {
|
|
29803
29916
|
const { logprobs: _logprobs, ...compatibleOptions } = options;
|
|
@@ -29961,6 +30074,8 @@ var AnthropicCompletion = class _AnthropicCompletion extends ConfiguredLLM {
|
|
|
29961
30074
|
stop: options.stop,
|
|
29962
30075
|
stopSequences: options.stopSequences,
|
|
29963
30076
|
stop_sequences: options.stop_sequences,
|
|
30077
|
+
responseFormat: options.responseFormat,
|
|
30078
|
+
response_format: options.response_format,
|
|
29964
30079
|
maxTokens: options.maxTokens ?? options.max_tokens ?? 4096,
|
|
29965
30080
|
timeout: options.timeout ?? null
|
|
29966
30081
|
}));
|
|
@@ -29985,8 +30100,71 @@ var AnthropicCompletion = class _AnthropicCompletion extends ConfiguredLLM {
|
|
|
29985
30100
|
this.previousThinkingBlocks = [];
|
|
29986
30101
|
this._previous_thinking_blocks = this.previousThinkingBlocks;
|
|
29987
30102
|
}
|
|
29988
|
-
call(messages, options) {
|
|
29989
|
-
|
|
30103
|
+
async call(messages, options) {
|
|
30104
|
+
const apiKey = this.apiKey;
|
|
30105
|
+
if (!apiKey) {
|
|
30106
|
+
throw new Error("Anthropic API key required. Pass api_key when constructing AnthropicCompletion.");
|
|
30107
|
+
}
|
|
30108
|
+
if (this.stream) {
|
|
30109
|
+
throw new Error("Anthropic streaming responses are not supported by the built-in HTTP transport yet.");
|
|
30110
|
+
}
|
|
30111
|
+
const [formattedMessages, systemMessage] = this.formatMessagesForAnthropic(messages);
|
|
30112
|
+
const availableFunctions = options?.availableFunctions ?? options?.available_functions ?? null;
|
|
30113
|
+
const params = this.prepareCompletionParams(
|
|
30114
|
+
formattedMessages,
|
|
30115
|
+
systemMessage,
|
|
30116
|
+
options?.tools ?? null,
|
|
30117
|
+
availableFunctions
|
|
30118
|
+
);
|
|
30119
|
+
const schema = anthropicResponseSchema(options?.responseModel ?? this.responseFormat);
|
|
30120
|
+
if (schema) {
|
|
30121
|
+
const tools = Array.isArray(params.tools) ? params.tools.filter((tool2) => typeof tool2 === "object" && tool2 !== null) : [];
|
|
30122
|
+
tools.push({
|
|
30123
|
+
name: STRUCTURED_OUTPUT_TOOL_NAME,
|
|
30124
|
+
description: "Use this tool to provide your final structured response in the required JSON shape.",
|
|
30125
|
+
input_schema: sanitizeToolParamsForAnthropicStrict(structuredClone(schema)),
|
|
30126
|
+
strict: true
|
|
30127
|
+
});
|
|
30128
|
+
params.tools = tools;
|
|
30129
|
+
params.tool_choice = { type: "tool", name: STRUCTURED_OUTPUT_TOOL_NAME };
|
|
30130
|
+
}
|
|
30131
|
+
const baseUrl = this.baseUrl ?? "https://api.anthropic.com";
|
|
30132
|
+
const response = await fetch(`${baseUrl.replace(/\/$/, "")}/v1/messages`, {
|
|
30133
|
+
method: "POST",
|
|
30134
|
+
headers: {
|
|
30135
|
+
"content-type": "application/json",
|
|
30136
|
+
"x-api-key": apiKey,
|
|
30137
|
+
"anthropic-version": "2023-06-01"
|
|
30138
|
+
},
|
|
30139
|
+
body: JSON.stringify(params),
|
|
30140
|
+
...options?.signal ? { signal: options.signal } : {}
|
|
30141
|
+
});
|
|
30142
|
+
const body = await response.json().catch(() => ({}));
|
|
30143
|
+
if (!response.ok) {
|
|
30144
|
+
const error = readObject2(readObject2(body).error);
|
|
30145
|
+
throw new Error(scalarToString(error.message) ?? `Anthropic request failed with HTTP ${response.status.toString()}.`);
|
|
30146
|
+
}
|
|
30147
|
+
const usage = this.extractAnthropicTokenUsage(body);
|
|
30148
|
+
if (usage.total_tokens !== 0) {
|
|
30149
|
+
this.trackTokenUsageInternal(usage);
|
|
30150
|
+
}
|
|
30151
|
+
const thinkingBlocks = _AnthropicCompletion.extractThinkingBlocksFromResponse(body);
|
|
30152
|
+
if (thinkingBlocks.length > 0) {
|
|
30153
|
+
this.previousThinkingBlocks = thinkingBlocks;
|
|
30154
|
+
this._previous_thinking_blocks = this.previousThinkingBlocks;
|
|
30155
|
+
}
|
|
30156
|
+
const structuredOutput = _AnthropicCompletion.extractStructuredOutputFromResponse(body);
|
|
30157
|
+
if (structuredOutput) {
|
|
30158
|
+
return structuredOutput;
|
|
30159
|
+
}
|
|
30160
|
+
const toolUses = _AnthropicCompletion.extractToolUsesFromResponse(body);
|
|
30161
|
+
if (toolUses.length > 0) {
|
|
30162
|
+
if (availableFunctions && Object.keys(availableFunctions).length > 0) {
|
|
30163
|
+
return await this.executeFirstTool(toolUses, availableFunctions);
|
|
30164
|
+
}
|
|
30165
|
+
return toolUses;
|
|
30166
|
+
}
|
|
30167
|
+
return this.applyStopWords(anthropicResponseText(body));
|
|
29990
30168
|
}
|
|
29991
30169
|
async acall(messages, options) {
|
|
29992
30170
|
return await super.acall(messages, options);
|
|
@@ -30500,8 +30678,32 @@ var BedrockCompletion = class _BedrockCompletion extends ConfiguredLLM {
|
|
|
30500
30678
|
this.additional_model_response_field_paths = this.additionalModelResponseFieldPaths;
|
|
30501
30679
|
this.interceptor = null;
|
|
30502
30680
|
}
|
|
30503
|
-
call(messages, options) {
|
|
30504
|
-
|
|
30681
|
+
async call(messages, options) {
|
|
30682
|
+
if (this.stream) {
|
|
30683
|
+
throw new Error("Bedrock streaming responses are not supported by the built-in client transport yet.");
|
|
30684
|
+
}
|
|
30685
|
+
const client = this.getConverseClient();
|
|
30686
|
+
const { messages: formattedMessages, body } = this.prepareConverseRequestBody(
|
|
30687
|
+
messages,
|
|
30688
|
+
options?.tools ?? null
|
|
30689
|
+
);
|
|
30690
|
+
const request = {
|
|
30691
|
+
modelId: this.model,
|
|
30692
|
+
messages: formattedMessages,
|
|
30693
|
+
...body
|
|
30694
|
+
};
|
|
30695
|
+
const response = await client.converse(request);
|
|
30696
|
+
this.trackTokenUsageInternal(readObject2(readObject2(response).usage));
|
|
30697
|
+
const toolUses = _BedrockCompletion.extractToolUsesFromResponse(response);
|
|
30698
|
+
const availableFunctions = options?.availableFunctions ?? options?.available_functions ?? null;
|
|
30699
|
+
if (toolUses.length > 0) {
|
|
30700
|
+
if (availableFunctions && Object.keys(availableFunctions).length > 0) {
|
|
30701
|
+
const executed = await this.executeToolUseAndPrepareMessages(formattedMessages, toolUses[0], availableFunctions);
|
|
30702
|
+
return executed.result;
|
|
30703
|
+
}
|
|
30704
|
+
return toolUses;
|
|
30705
|
+
}
|
|
30706
|
+
return bedrockResponseText(response);
|
|
30505
30707
|
}
|
|
30506
30708
|
async acall(messages, options) {
|
|
30507
30709
|
return await super.acall(messages, options);
|
|
@@ -30924,6 +31126,24 @@ ${content}` : content;
|
|
|
30924
31126
|
const model = this.model.toLowerCase();
|
|
30925
31127
|
return model.includes("anthropic") || model.includes("claude");
|
|
30926
31128
|
}
|
|
31129
|
+
getConverseClient() {
|
|
31130
|
+
const direct = readObject2(this.session).converse;
|
|
31131
|
+
if (typeof direct === "function") {
|
|
31132
|
+
return { converse: (request) => Reflect.apply(direct, this.session, [request]) };
|
|
31133
|
+
}
|
|
31134
|
+
const clientFactory = readObject2(this.session).client;
|
|
31135
|
+
if (typeof clientFactory === "function") {
|
|
31136
|
+
const client = Reflect.apply(clientFactory, this.session, [
|
|
31137
|
+
"bedrock-runtime",
|
|
31138
|
+
this.regionName ? { region: this.regionName } : void 0
|
|
31139
|
+
]);
|
|
31140
|
+
const converse = readObject2(client).converse;
|
|
31141
|
+
if (typeof converse === "function") {
|
|
31142
|
+
return { converse: (request) => Reflect.apply(converse, client, [request]) };
|
|
31143
|
+
}
|
|
31144
|
+
}
|
|
31145
|
+
throw new Error("Bedrock live calls require a session/client with a converse(request) method.");
|
|
31146
|
+
}
|
|
30927
31147
|
};
|
|
30928
31148
|
var GeminiCompletion = class _GeminiCompletion extends ConfiguredLLM {
|
|
30929
31149
|
project;
|
|
@@ -31001,7 +31221,7 @@ var GeminiCompletion = class _GeminiCompletion extends ConfiguredLLM {
|
|
|
31001
31221
|
}
|
|
31002
31222
|
call(messages, options) {
|
|
31003
31223
|
if (this.useVertexai) {
|
|
31004
|
-
return
|
|
31224
|
+
return this.callVertexAI(messages, options);
|
|
31005
31225
|
}
|
|
31006
31226
|
const apiKey = this.apiKey ?? process.env.GOOGLE_API_KEY ?? process.env.GEMINI_API_KEY ?? null;
|
|
31007
31227
|
if (!apiKey) {
|
|
@@ -31055,6 +31275,58 @@ var GeminiCompletion = class _GeminiCompletion extends ConfiguredLLM {
|
|
|
31055
31275
|
);
|
|
31056
31276
|
});
|
|
31057
31277
|
}
|
|
31278
|
+
async callVertexAI(messages, options) {
|
|
31279
|
+
if (!this.project) {
|
|
31280
|
+
throw new Error("Vertex AI Gemini calls require a project.");
|
|
31281
|
+
}
|
|
31282
|
+
const clientParams = readObject2(this.clientParams);
|
|
31283
|
+
const accessToken = scalarToString(clientParams.access_token ?? clientParams.accessToken ?? this.apiKey);
|
|
31284
|
+
if (!accessToken) {
|
|
31285
|
+
throw new Error("Vertex AI Gemini calls require client_params.access_token or api_key.");
|
|
31286
|
+
}
|
|
31287
|
+
const [contents, systemInstruction] = this.formatMessagesForGemini(messages);
|
|
31288
|
+
const tools = options?.tools ?? null;
|
|
31289
|
+
const generationConfig = this.prepareGenerationConfig(
|
|
31290
|
+
systemInstruction,
|
|
31291
|
+
tools,
|
|
31292
|
+
options?.responseModel ?? null
|
|
31293
|
+
);
|
|
31294
|
+
const requestBody = readObject2(generationConfig);
|
|
31295
|
+
const generationConfigBody = { ...generationConfig };
|
|
31296
|
+
delete generationConfigBody.system_instruction;
|
|
31297
|
+
delete generationConfigBody.tools;
|
|
31298
|
+
delete generationConfigBody.safety_settings;
|
|
31299
|
+
const model = this.model.replace(/^(?:gemini|google)\//, "");
|
|
31300
|
+
const baseUrl = this.baseUrl ?? `https://${this.location}-aiplatform.googleapis.com/v1`;
|
|
31301
|
+
const url = `${baseUrl.replace(/\/$/, "")}/projects/${encodeURIComponent(this.project)}/locations/${encodeURIComponent(this.location)}/publishers/google/models/${encodeURIComponent(model)}:generateContent`;
|
|
31302
|
+
const response = await fetch(url, {
|
|
31303
|
+
method: "POST",
|
|
31304
|
+
headers: {
|
|
31305
|
+
Authorization: `Bearer ${accessToken}`,
|
|
31306
|
+
"Content-Type": "application/json"
|
|
31307
|
+
},
|
|
31308
|
+
body: JSON.stringify({
|
|
31309
|
+
contents,
|
|
31310
|
+
...Object.keys(generationConfigBody).length > 0 ? { generationConfig: generationConfigBody } : {},
|
|
31311
|
+
..."system_instruction" in requestBody ? { systemInstruction: requestBody.system_instruction } : {},
|
|
31312
|
+
..."tools" in requestBody ? { tools: requestBody.tools } : {},
|
|
31313
|
+
..."safety_settings" in requestBody ? { safetySettings: requestBody.safety_settings } : {}
|
|
31314
|
+
}),
|
|
31315
|
+
...options?.signal ? { signal: options.signal } : {}
|
|
31316
|
+
});
|
|
31317
|
+
const body = await response.json().catch(() => ({}));
|
|
31318
|
+
if (!response.ok) {
|
|
31319
|
+
const error = readObject2(readObject2(body).error);
|
|
31320
|
+
throw new Error(scalarToString(error.message) ?? `Vertex AI Gemini request failed with HTTP ${response.status.toString()}.`);
|
|
31321
|
+
}
|
|
31322
|
+
return await this.processResponseWithTools(
|
|
31323
|
+
body,
|
|
31324
|
+
contents,
|
|
31325
|
+
options?.availableFunctions ?? options?.available_functions ?? null,
|
|
31326
|
+
null,
|
|
31327
|
+
geminiMaxToolRounds(options)
|
|
31328
|
+
);
|
|
31329
|
+
}
|
|
31058
31330
|
async acall(messages, options) {
|
|
31059
31331
|
return await this.call(this.formatMessages(messages), options);
|
|
31060
31332
|
}
|
|
@@ -32062,7 +32334,7 @@ var AzureCompletion = class _AzureCompletion extends ConfiguredLLM {
|
|
|
32062
32334
|
if (this._responses_delegate) {
|
|
32063
32335
|
return this._responses_delegate.call(messages, options);
|
|
32064
32336
|
}
|
|
32065
|
-
return
|
|
32337
|
+
return this.callChatCompletions(messages, options);
|
|
32066
32338
|
}
|
|
32067
32339
|
async acall(messages, options) {
|
|
32068
32340
|
if (this._responses_delegate) {
|
|
@@ -32276,6 +32548,50 @@ var AzureCompletion = class _AzureCompletion extends ConfiguredLLM {
|
|
|
32276
32548
|
_prepare_completion_params(messages, tools = null) {
|
|
32277
32549
|
return this.prepareCompletionParams(messages, tools);
|
|
32278
32550
|
}
|
|
32551
|
+
async callChatCompletions(messages, options) {
|
|
32552
|
+
if (this.stream) {
|
|
32553
|
+
throw new Error("Azure streaming responses are not supported by the built-in fetch transport yet.");
|
|
32554
|
+
}
|
|
32555
|
+
const client = this.getSyncClient();
|
|
32556
|
+
const endpoint = scalarToString(client.endpoint);
|
|
32557
|
+
const apiKey = scalarToString(client.api_key);
|
|
32558
|
+
if (!endpoint) {
|
|
32559
|
+
throw new Error("Azure endpoint is required");
|
|
32560
|
+
}
|
|
32561
|
+
if (!apiKey) {
|
|
32562
|
+
throw new Error("Azure API key is required");
|
|
32563
|
+
}
|
|
32564
|
+
const params = this.prepareCompletionParams(
|
|
32565
|
+
this.formatMessages(messages),
|
|
32566
|
+
options?.tools ?? null
|
|
32567
|
+
);
|
|
32568
|
+
const url = azureChatCompletionsUrl(endpoint, this.model, this.apiVersion);
|
|
32569
|
+
const response = await fetch(url, {
|
|
32570
|
+
method: "POST",
|
|
32571
|
+
headers: {
|
|
32572
|
+
"Content-Type": "application/json",
|
|
32573
|
+
"api-key": apiKey
|
|
32574
|
+
},
|
|
32575
|
+
body: JSON.stringify(params)
|
|
32576
|
+
});
|
|
32577
|
+
const payload = await response.json().catch(() => ({}));
|
|
32578
|
+
if (!response.ok) {
|
|
32579
|
+
const error = readObject2(readObject2(payload).error);
|
|
32580
|
+
throw new Error(scalarToString(error.message) ?? `Azure completion request failed with HTTP ${response.status.toString()}.`);
|
|
32581
|
+
}
|
|
32582
|
+
const usage = this.extractAzureTokenUsage(payload);
|
|
32583
|
+
if (usage.total_tokens !== 0) {
|
|
32584
|
+
this.trackTokenUsageInternal(usage);
|
|
32585
|
+
}
|
|
32586
|
+
const choices = readObject2(payload).choices;
|
|
32587
|
+
const firstChoice = Array.isArray(choices) ? readObject2(choices[0]) : {};
|
|
32588
|
+
const message = readObject2(firstChoice.message);
|
|
32589
|
+
const toolCalls = Array.isArray(message.tool_calls) ? message.tool_calls : [];
|
|
32590
|
+
if (toolCalls.length > 0) {
|
|
32591
|
+
return toolCalls;
|
|
32592
|
+
}
|
|
32593
|
+
return typeof message.content === "string" ? message.content : "";
|
|
32594
|
+
}
|
|
32279
32595
|
prepareResponsesParams(messages, tools = null, responseModel = null) {
|
|
32280
32596
|
if (!this._responses_delegate) {
|
|
32281
32597
|
throw new Error("Azure Responses API is only available when api is set to 'responses'.");
|
|
@@ -32573,15 +32889,29 @@ function geminiResponseSchema(value) {
|
|
|
32573
32889
|
const schema = schemaProvider.model_json_schema?.() ?? schemaProvider.modelJsonSchema?.() ?? schemaProvider.schema;
|
|
32574
32890
|
return schema && typeof schema === "object" && !Array.isArray(schema) ? schema : null;
|
|
32575
32891
|
}
|
|
32892
|
+
function anthropicResponseSchema(value) {
|
|
32893
|
+
if (!value || typeof value !== "object") {
|
|
32894
|
+
return null;
|
|
32895
|
+
}
|
|
32896
|
+
const schemaProvider = value;
|
|
32897
|
+
const schema = schemaProvider.model_json_schema?.() ?? schemaProvider.modelJsonSchema?.() ?? schemaProvider.schema;
|
|
32898
|
+
return schema && typeof schema === "object" && !Array.isArray(schema) ? schema : null;
|
|
32899
|
+
}
|
|
32576
32900
|
function anthropicResponseContent(response) {
|
|
32577
32901
|
const content = readObject2(response).content;
|
|
32578
32902
|
return Array.isArray(content) ? content : [];
|
|
32579
32903
|
}
|
|
32904
|
+
function anthropicResponseText(response) {
|
|
32905
|
+
return anthropicResponseContent(response).map((block) => scalarToString(readObject2(block).text)).filter((text) => text !== null).join("");
|
|
32906
|
+
}
|
|
32580
32907
|
function bedrockResponseContent(response) {
|
|
32581
32908
|
const output4 = readObject2(readObject2(response).output);
|
|
32582
32909
|
const message = readObject2(output4.message);
|
|
32583
32910
|
return Array.isArray(message.content) ? message.content : [];
|
|
32584
32911
|
}
|
|
32912
|
+
function bedrockResponseText(response) {
|
|
32913
|
+
return bedrockResponseContent(response).map((block) => scalarToString(readObject2(block).text)).filter((text) => text !== null).join("");
|
|
32914
|
+
}
|
|
32585
32915
|
function isAzureOpenAIEndpoint(endpoint) {
|
|
32586
32916
|
if (!endpoint) {
|
|
32587
32917
|
return false;
|
|
@@ -32612,6 +32942,14 @@ function azureResponsesBaseUrl(endpoint) {
|
|
|
32612
32942
|
return `${trimmed.replace(/\/openai(?:\/v1)?$/i, "")}/openai/v1/`;
|
|
32613
32943
|
}
|
|
32614
32944
|
}
|
|
32945
|
+
function azureChatCompletionsUrl(endpoint, model, apiVersion) {
|
|
32946
|
+
const version2 = encodeURIComponent(apiVersion ?? "2024-06-01");
|
|
32947
|
+
const trimmed = endpoint.replace(/\/+$/, "");
|
|
32948
|
+
const deploymentPath = "/openai/deployments/";
|
|
32949
|
+
const lower = trimmed.toLowerCase();
|
|
32950
|
+
const base = lower.includes(deploymentPath) ? trimmed : `${trimmed.replace(/\/openai(?:\/v1)?$/i, "")}${deploymentPath}${encodeURIComponent(azureResponsesModelName(model))}`;
|
|
32951
|
+
return `${base}/chat/completions?api-version=${version2}`;
|
|
32952
|
+
}
|
|
32615
32953
|
function geminiContextWindowSize(model) {
|
|
32616
32954
|
const windows = {
|
|
32617
32955
|
"gemini-3-pro-preview": 1048576,
|
|
@@ -219,6 +219,9 @@ export declare class OpenAICompletion extends ConfiguredLLM {
|
|
|
219
219
|
_prepare_completion_params(messages: readonly LLMMessage[], tools?: readonly Tool[] | null): Record<string, unknown>;
|
|
220
220
|
prepareResponsesParams(messages: readonly LLMMessage[], tools?: readonly Tool[] | null, responseModel?: unknown): Record<string, unknown>;
|
|
221
221
|
_prepare_responses_params(messages: readonly LLMMessage[], tools?: readonly Tool[] | null, responseModel?: unknown): Record<string, unknown>;
|
|
222
|
+
private callChatCompletions;
|
|
223
|
+
private callResponses;
|
|
224
|
+
private fetchOpenAI;
|
|
222
225
|
convertToolsForInterference(tools: readonly Tool[]): Record<string, unknown>[];
|
|
223
226
|
_convert_tools_for_interference(tools: readonly Tool[]): Record<string, unknown>[];
|
|
224
227
|
convertToolsForResponses(tools: readonly Tool[]): Record<string, unknown>[];
|
|
@@ -268,6 +268,7 @@ export declare class BedrockCompletion extends ConfiguredLLM {
|
|
|
268
268
|
trackTokenUsageInternal(usageData: Record<string, unknown>): void;
|
|
269
269
|
_track_token_usage_internal(usageData: Record<string, unknown>): void;
|
|
270
270
|
private isClaudeModel;
|
|
271
|
+
private getConverseClient;
|
|
271
272
|
}
|
|
272
273
|
export type GeminiCompletionOptions = BaseLLMOptions & {
|
|
273
274
|
project?: string | null;
|
|
@@ -322,6 +323,7 @@ export declare class GeminiCompletion extends ConfiguredLLM {
|
|
|
322
323
|
tools: readonly Tool[] | null;
|
|
323
324
|
constructor(options?: GeminiCompletionOptions);
|
|
324
325
|
call(messages: readonly LLMMessage[], options?: LLMCallOptions): Promise<LLMResponse>;
|
|
326
|
+
private callVertexAI;
|
|
325
327
|
acall(messages: LLMMessageInput, options?: LLMCallOptions): Promise<LLMResponse>;
|
|
326
328
|
supportsFunctionCalling(): boolean;
|
|
327
329
|
supports_function_calling(): boolean;
|
|
@@ -579,6 +581,7 @@ export declare class AzureCompletion extends ConfiguredLLM {
|
|
|
579
581
|
_get_sync_client(env?: NodeJS.ProcessEnv): Record<string, unknown>;
|
|
580
582
|
prepareCompletionParams(messages: readonly LLMMessage[], tools?: readonly Tool[] | null): AzureCompletionParams;
|
|
581
583
|
_prepare_completion_params(messages: readonly LLMMessage[], tools?: readonly Tool[] | null): AzureCompletionParams;
|
|
584
|
+
private callChatCompletions;
|
|
582
585
|
prepareResponsesParams(messages: readonly LLMMessage[], tools?: readonly Tool[] | null, responseModel?: unknown): Record<string, unknown>;
|
|
583
586
|
_prepare_responses_params(messages: readonly LLMMessage[], tools?: readonly Tool[] | null, responseModel?: unknown): Record<string, unknown>;
|
|
584
587
|
convertToolsForInterference(tools: readonly Tool[]): Record<string, unknown>[];
|