@crewai-ts/core 0.1.5 → 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/agent.d.ts +1 -0
- package/dist/index.cjs +358 -8
- package/dist/index.js +358 -8
- package/dist/openai-completion.d.ts +3 -0
- package/dist/provider-completions.d.ts +3 -0
- package/package.json +1 -1
package/dist/agent.d.ts
CHANGED
|
@@ -427,6 +427,7 @@ export declare class Agent {
|
|
|
427
427
|
private callAndTrackLLM;
|
|
428
428
|
private messagesWithInputFilesForClient;
|
|
429
429
|
private clientSupportsMultimodal;
|
|
430
|
+
private clientSupportsFunctionCalling;
|
|
430
431
|
private modelNameForClient;
|
|
431
432
|
private applyGuardrail;
|
|
432
433
|
private emitStep;
|
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;
|
|
@@ -44139,8 +44161,13 @@ Result: ${output4}`, {
|
|
|
44139
44161
|
const beforeUsage = getLLMUsageMetrics(llmClient);
|
|
44140
44162
|
const model = this.modelNameForClient(llmClient);
|
|
44141
44163
|
const responseModel = responseModelFromOptions(options);
|
|
44164
|
+
const nativeToolOptions = tools.length > 0 && this.clientSupportsFunctionCalling(llmClient) ? (() => {
|
|
44165
|
+
const [, availableFunctions] = setupNativeTools(tools);
|
|
44166
|
+
return { availableFunctions, available_functions: availableFunctions };
|
|
44167
|
+
})() : {};
|
|
44142
44168
|
const result = await callLLM(llmClient, messagesForCall, {
|
|
44143
44169
|
tools,
|
|
44170
|
+
...nativeToolOptions,
|
|
44144
44171
|
...responseModel === void 0 ? {} : { responseModel },
|
|
44145
44172
|
metadata: {
|
|
44146
44173
|
agent: this,
|
|
@@ -44173,6 +44200,13 @@ Result: ${output4}`, {
|
|
|
44173
44200
|
}
|
|
44174
44201
|
return typeof candidate.supports_multimodal === "function" && candidate.supports_multimodal();
|
|
44175
44202
|
}
|
|
44203
|
+
clientSupportsFunctionCalling(llmClient) {
|
|
44204
|
+
const candidate = llmClient;
|
|
44205
|
+
if (typeof candidate.supportsFunctionCalling === "function" && candidate.supportsFunctionCalling()) {
|
|
44206
|
+
return true;
|
|
44207
|
+
}
|
|
44208
|
+
return typeof candidate.supports_function_calling === "function" && candidate.supports_function_calling();
|
|
44209
|
+
}
|
|
44176
44210
|
modelNameForClient(llmClient) {
|
|
44177
44211
|
if (llmClient === this.llmClient && typeof this.llm === "string") {
|
|
44178
44212
|
return this.llm;
|
|
@@ -45505,8 +45539,13 @@ var OpenAICompletion = class _OpenAICompletion extends ConfiguredLLM {
|
|
|
45505
45539
|
this.responseChainId = this.previousResponseId;
|
|
45506
45540
|
this.reasoningChainItems = null;
|
|
45507
45541
|
}
|
|
45508
|
-
call(messages, options) {
|
|
45509
|
-
|
|
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);
|
|
45510
45549
|
}
|
|
45511
45550
|
async acall(messages, options) {
|
|
45512
45551
|
return await super.acall(messages, options);
|
|
@@ -45643,6 +45682,79 @@ ${message.content}` : message.content;
|
|
|
45643
45682
|
_prepare_responses_params(messages, tools = null, responseModel = null) {
|
|
45644
45683
|
return this.prepareResponsesParams(messages, tools, responseModel);
|
|
45645
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
|
+
}
|
|
45646
45758
|
convertToolsForInterference(tools) {
|
|
45647
45759
|
return convertToolsToOpenAISchema(tools)[0];
|
|
45648
45760
|
}
|
|
@@ -45842,6 +45954,18 @@ ${message.content}` : message.content;
|
|
|
45842
45954
|
name: item.name,
|
|
45843
45955
|
arguments: item.arguments
|
|
45844
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
|
+
}
|
|
45845
45969
|
}
|
|
45846
45970
|
}
|
|
45847
45971
|
return result;
|
|
@@ -46123,6 +46247,7 @@ var OpenAICompatibleCompletion = class _OpenAICompatibleCompletion extends OpenA
|
|
|
46123
46247
|
return _OpenAICompatibleCompletion.resolveHeaders(headers, config);
|
|
46124
46248
|
}
|
|
46125
46249
|
};
|
|
46250
|
+
registerLLMProviderFactory("openai", (options) => new OpenAICompletion(options));
|
|
46126
46251
|
for (const provider of Object.keys(OPENAI_COMPATIBLE_PROVIDERS)) {
|
|
46127
46252
|
registerLLMProviderFactory(provider, (options) => {
|
|
46128
46253
|
const { logprobs: _logprobs, ...compatibleOptions } = options;
|
|
@@ -46288,6 +46413,8 @@ var AnthropicCompletion = class _AnthropicCompletion extends ConfiguredLLM {
|
|
|
46288
46413
|
stop: options.stop,
|
|
46289
46414
|
stopSequences: options.stopSequences,
|
|
46290
46415
|
stop_sequences: options.stop_sequences,
|
|
46416
|
+
responseFormat: options.responseFormat,
|
|
46417
|
+
response_format: options.response_format,
|
|
46291
46418
|
maxTokens: options.maxTokens ?? options.max_tokens ?? 4096,
|
|
46292
46419
|
timeout: options.timeout ?? null
|
|
46293
46420
|
}));
|
|
@@ -46312,8 +46439,71 @@ var AnthropicCompletion = class _AnthropicCompletion extends ConfiguredLLM {
|
|
|
46312
46439
|
this.previousThinkingBlocks = [];
|
|
46313
46440
|
this._previous_thinking_blocks = this.previousThinkingBlocks;
|
|
46314
46441
|
}
|
|
46315
|
-
call(messages, options) {
|
|
46316
|
-
|
|
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));
|
|
46317
46507
|
}
|
|
46318
46508
|
async acall(messages, options) {
|
|
46319
46509
|
return await super.acall(messages, options);
|
|
@@ -46827,8 +47017,32 @@ var BedrockCompletion = class _BedrockCompletion extends ConfiguredLLM {
|
|
|
46827
47017
|
this.additional_model_response_field_paths = this.additionalModelResponseFieldPaths;
|
|
46828
47018
|
this.interceptor = null;
|
|
46829
47019
|
}
|
|
46830
|
-
call(messages, options) {
|
|
46831
|
-
|
|
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);
|
|
46832
47046
|
}
|
|
46833
47047
|
async acall(messages, options) {
|
|
46834
47048
|
return await super.acall(messages, options);
|
|
@@ -47251,6 +47465,24 @@ ${content}` : content;
|
|
|
47251
47465
|
const model = this.model.toLowerCase();
|
|
47252
47466
|
return model.includes("anthropic") || model.includes("claude");
|
|
47253
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
|
+
}
|
|
47254
47486
|
};
|
|
47255
47487
|
var GeminiCompletion = class _GeminiCompletion extends ConfiguredLLM {
|
|
47256
47488
|
project;
|
|
@@ -47328,7 +47560,7 @@ var GeminiCompletion = class _GeminiCompletion extends ConfiguredLLM {
|
|
|
47328
47560
|
}
|
|
47329
47561
|
call(messages, options) {
|
|
47330
47562
|
if (this.useVertexai) {
|
|
47331
|
-
return
|
|
47563
|
+
return this.callVertexAI(messages, options);
|
|
47332
47564
|
}
|
|
47333
47565
|
const apiKey = this.apiKey ?? process.env.GOOGLE_API_KEY ?? process.env.GEMINI_API_KEY ?? null;
|
|
47334
47566
|
if (!apiKey) {
|
|
@@ -47382,6 +47614,58 @@ var GeminiCompletion = class _GeminiCompletion extends ConfiguredLLM {
|
|
|
47382
47614
|
);
|
|
47383
47615
|
});
|
|
47384
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
|
+
}
|
|
47385
47669
|
async acall(messages, options) {
|
|
47386
47670
|
return await this.call(this.formatMessages(messages), options);
|
|
47387
47671
|
}
|
|
@@ -48389,7 +48673,7 @@ var AzureCompletion = class _AzureCompletion extends ConfiguredLLM {
|
|
|
48389
48673
|
if (this._responses_delegate) {
|
|
48390
48674
|
return this._responses_delegate.call(messages, options);
|
|
48391
48675
|
}
|
|
48392
|
-
return
|
|
48676
|
+
return this.callChatCompletions(messages, options);
|
|
48393
48677
|
}
|
|
48394
48678
|
async acall(messages, options) {
|
|
48395
48679
|
if (this._responses_delegate) {
|
|
@@ -48603,6 +48887,50 @@ var AzureCompletion = class _AzureCompletion extends ConfiguredLLM {
|
|
|
48603
48887
|
_prepare_completion_params(messages, tools = null) {
|
|
48604
48888
|
return this.prepareCompletionParams(messages, tools);
|
|
48605
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
|
+
}
|
|
48606
48934
|
prepareResponsesParams(messages, tools = null, responseModel = null) {
|
|
48607
48935
|
if (!this._responses_delegate) {
|
|
48608
48936
|
throw new Error("Azure Responses API is only available when api is set to 'responses'.");
|
|
@@ -48900,15 +49228,29 @@ function geminiResponseSchema(value) {
|
|
|
48900
49228
|
const schema = schemaProvider.model_json_schema?.() ?? schemaProvider.modelJsonSchema?.() ?? schemaProvider.schema;
|
|
48901
49229
|
return schema && typeof schema === "object" && !Array.isArray(schema) ? schema : null;
|
|
48902
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
|
+
}
|
|
48903
49239
|
function anthropicResponseContent(response) {
|
|
48904
49240
|
const content = readObject2(response).content;
|
|
48905
49241
|
return Array.isArray(content) ? content : [];
|
|
48906
49242
|
}
|
|
49243
|
+
function anthropicResponseText(response) {
|
|
49244
|
+
return anthropicResponseContent(response).map((block) => scalarToString(readObject2(block).text)).filter((text) => text !== null).join("");
|
|
49245
|
+
}
|
|
48907
49246
|
function bedrockResponseContent(response) {
|
|
48908
49247
|
const output4 = readObject2(readObject2(response).output);
|
|
48909
49248
|
const message = readObject2(output4.message);
|
|
48910
49249
|
return Array.isArray(message.content) ? message.content : [];
|
|
48911
49250
|
}
|
|
49251
|
+
function bedrockResponseText(response) {
|
|
49252
|
+
return bedrockResponseContent(response).map((block) => scalarToString(readObject2(block).text)).filter((text) => text !== null).join("");
|
|
49253
|
+
}
|
|
48912
49254
|
function isAzureOpenAIEndpoint(endpoint) {
|
|
48913
49255
|
if (!endpoint) {
|
|
48914
49256
|
return false;
|
|
@@ -48939,6 +49281,14 @@ function azureResponsesBaseUrl(endpoint) {
|
|
|
48939
49281
|
return `${trimmed.replace(/\/openai(?:\/v1)?$/i, "")}/openai/v1/`;
|
|
48940
49282
|
}
|
|
48941
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
|
+
}
|
|
48942
49292
|
function geminiContextWindowSize(model) {
|
|
48943
49293
|
const windows = {
|
|
48944
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;
|
|
@@ -27809,8 +27831,13 @@ Result: ${output4}`, {
|
|
|
27809
27831
|
const beforeUsage = getLLMUsageMetrics(llmClient);
|
|
27810
27832
|
const model = this.modelNameForClient(llmClient);
|
|
27811
27833
|
const responseModel = responseModelFromOptions(options);
|
|
27834
|
+
const nativeToolOptions = tools.length > 0 && this.clientSupportsFunctionCalling(llmClient) ? (() => {
|
|
27835
|
+
const [, availableFunctions] = setupNativeTools(tools);
|
|
27836
|
+
return { availableFunctions, available_functions: availableFunctions };
|
|
27837
|
+
})() : {};
|
|
27812
27838
|
const result = await callLLM(llmClient, messagesForCall, {
|
|
27813
27839
|
tools,
|
|
27840
|
+
...nativeToolOptions,
|
|
27814
27841
|
...responseModel === void 0 ? {} : { responseModel },
|
|
27815
27842
|
metadata: {
|
|
27816
27843
|
agent: this,
|
|
@@ -27843,6 +27870,13 @@ Result: ${output4}`, {
|
|
|
27843
27870
|
}
|
|
27844
27871
|
return typeof candidate.supports_multimodal === "function" && candidate.supports_multimodal();
|
|
27845
27872
|
}
|
|
27873
|
+
clientSupportsFunctionCalling(llmClient) {
|
|
27874
|
+
const candidate = llmClient;
|
|
27875
|
+
if (typeof candidate.supportsFunctionCalling === "function" && candidate.supportsFunctionCalling()) {
|
|
27876
|
+
return true;
|
|
27877
|
+
}
|
|
27878
|
+
return typeof candidate.supports_function_calling === "function" && candidate.supports_function_calling();
|
|
27879
|
+
}
|
|
27846
27880
|
modelNameForClient(llmClient) {
|
|
27847
27881
|
if (llmClient === this.llmClient && typeof this.llm === "string") {
|
|
27848
27882
|
return this.llm;
|
|
@@ -29168,8 +29202,13 @@ var OpenAICompletion = class _OpenAICompletion extends ConfiguredLLM {
|
|
|
29168
29202
|
this.responseChainId = this.previousResponseId;
|
|
29169
29203
|
this.reasoningChainItems = null;
|
|
29170
29204
|
}
|
|
29171
|
-
call(messages, options) {
|
|
29172
|
-
|
|
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);
|
|
29173
29212
|
}
|
|
29174
29213
|
async acall(messages, options) {
|
|
29175
29214
|
return await super.acall(messages, options);
|
|
@@ -29306,6 +29345,79 @@ ${message.content}` : message.content;
|
|
|
29306
29345
|
_prepare_responses_params(messages, tools = null, responseModel = null) {
|
|
29307
29346
|
return this.prepareResponsesParams(messages, tools, responseModel);
|
|
29308
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
|
+
}
|
|
29309
29421
|
convertToolsForInterference(tools) {
|
|
29310
29422
|
return convertToolsToOpenAISchema(tools)[0];
|
|
29311
29423
|
}
|
|
@@ -29505,6 +29617,18 @@ ${message.content}` : message.content;
|
|
|
29505
29617
|
name: item.name,
|
|
29506
29618
|
arguments: item.arguments
|
|
29507
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
|
+
}
|
|
29508
29632
|
}
|
|
29509
29633
|
}
|
|
29510
29634
|
return result;
|
|
@@ -29786,6 +29910,7 @@ var OpenAICompatibleCompletion = class _OpenAICompatibleCompletion extends OpenA
|
|
|
29786
29910
|
return _OpenAICompatibleCompletion.resolveHeaders(headers, config);
|
|
29787
29911
|
}
|
|
29788
29912
|
};
|
|
29913
|
+
registerLLMProviderFactory("openai", (options) => new OpenAICompletion(options));
|
|
29789
29914
|
for (const provider of Object.keys(OPENAI_COMPATIBLE_PROVIDERS)) {
|
|
29790
29915
|
registerLLMProviderFactory(provider, (options) => {
|
|
29791
29916
|
const { logprobs: _logprobs, ...compatibleOptions } = options;
|
|
@@ -29949,6 +30074,8 @@ var AnthropicCompletion = class _AnthropicCompletion extends ConfiguredLLM {
|
|
|
29949
30074
|
stop: options.stop,
|
|
29950
30075
|
stopSequences: options.stopSequences,
|
|
29951
30076
|
stop_sequences: options.stop_sequences,
|
|
30077
|
+
responseFormat: options.responseFormat,
|
|
30078
|
+
response_format: options.response_format,
|
|
29952
30079
|
maxTokens: options.maxTokens ?? options.max_tokens ?? 4096,
|
|
29953
30080
|
timeout: options.timeout ?? null
|
|
29954
30081
|
}));
|
|
@@ -29973,8 +30100,71 @@ var AnthropicCompletion = class _AnthropicCompletion extends ConfiguredLLM {
|
|
|
29973
30100
|
this.previousThinkingBlocks = [];
|
|
29974
30101
|
this._previous_thinking_blocks = this.previousThinkingBlocks;
|
|
29975
30102
|
}
|
|
29976
|
-
call(messages, options) {
|
|
29977
|
-
|
|
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));
|
|
29978
30168
|
}
|
|
29979
30169
|
async acall(messages, options) {
|
|
29980
30170
|
return await super.acall(messages, options);
|
|
@@ -30488,8 +30678,32 @@ var BedrockCompletion = class _BedrockCompletion extends ConfiguredLLM {
|
|
|
30488
30678
|
this.additional_model_response_field_paths = this.additionalModelResponseFieldPaths;
|
|
30489
30679
|
this.interceptor = null;
|
|
30490
30680
|
}
|
|
30491
|
-
call(messages, options) {
|
|
30492
|
-
|
|
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);
|
|
30493
30707
|
}
|
|
30494
30708
|
async acall(messages, options) {
|
|
30495
30709
|
return await super.acall(messages, options);
|
|
@@ -30912,6 +31126,24 @@ ${content}` : content;
|
|
|
30912
31126
|
const model = this.model.toLowerCase();
|
|
30913
31127
|
return model.includes("anthropic") || model.includes("claude");
|
|
30914
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
|
+
}
|
|
30915
31147
|
};
|
|
30916
31148
|
var GeminiCompletion = class _GeminiCompletion extends ConfiguredLLM {
|
|
30917
31149
|
project;
|
|
@@ -30989,7 +31221,7 @@ var GeminiCompletion = class _GeminiCompletion extends ConfiguredLLM {
|
|
|
30989
31221
|
}
|
|
30990
31222
|
call(messages, options) {
|
|
30991
31223
|
if (this.useVertexai) {
|
|
30992
|
-
return
|
|
31224
|
+
return this.callVertexAI(messages, options);
|
|
30993
31225
|
}
|
|
30994
31226
|
const apiKey = this.apiKey ?? process.env.GOOGLE_API_KEY ?? process.env.GEMINI_API_KEY ?? null;
|
|
30995
31227
|
if (!apiKey) {
|
|
@@ -31043,6 +31275,58 @@ var GeminiCompletion = class _GeminiCompletion extends ConfiguredLLM {
|
|
|
31043
31275
|
);
|
|
31044
31276
|
});
|
|
31045
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
|
+
}
|
|
31046
31330
|
async acall(messages, options) {
|
|
31047
31331
|
return await this.call(this.formatMessages(messages), options);
|
|
31048
31332
|
}
|
|
@@ -32050,7 +32334,7 @@ var AzureCompletion = class _AzureCompletion extends ConfiguredLLM {
|
|
|
32050
32334
|
if (this._responses_delegate) {
|
|
32051
32335
|
return this._responses_delegate.call(messages, options);
|
|
32052
32336
|
}
|
|
32053
|
-
return
|
|
32337
|
+
return this.callChatCompletions(messages, options);
|
|
32054
32338
|
}
|
|
32055
32339
|
async acall(messages, options) {
|
|
32056
32340
|
if (this._responses_delegate) {
|
|
@@ -32264,6 +32548,50 @@ var AzureCompletion = class _AzureCompletion extends ConfiguredLLM {
|
|
|
32264
32548
|
_prepare_completion_params(messages, tools = null) {
|
|
32265
32549
|
return this.prepareCompletionParams(messages, tools);
|
|
32266
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
|
+
}
|
|
32267
32595
|
prepareResponsesParams(messages, tools = null, responseModel = null) {
|
|
32268
32596
|
if (!this._responses_delegate) {
|
|
32269
32597
|
throw new Error("Azure Responses API is only available when api is set to 'responses'.");
|
|
@@ -32561,15 +32889,29 @@ function geminiResponseSchema(value) {
|
|
|
32561
32889
|
const schema = schemaProvider.model_json_schema?.() ?? schemaProvider.modelJsonSchema?.() ?? schemaProvider.schema;
|
|
32562
32890
|
return schema && typeof schema === "object" && !Array.isArray(schema) ? schema : null;
|
|
32563
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
|
+
}
|
|
32564
32900
|
function anthropicResponseContent(response) {
|
|
32565
32901
|
const content = readObject2(response).content;
|
|
32566
32902
|
return Array.isArray(content) ? content : [];
|
|
32567
32903
|
}
|
|
32904
|
+
function anthropicResponseText(response) {
|
|
32905
|
+
return anthropicResponseContent(response).map((block) => scalarToString(readObject2(block).text)).filter((text) => text !== null).join("");
|
|
32906
|
+
}
|
|
32568
32907
|
function bedrockResponseContent(response) {
|
|
32569
32908
|
const output4 = readObject2(readObject2(response).output);
|
|
32570
32909
|
const message = readObject2(output4.message);
|
|
32571
32910
|
return Array.isArray(message.content) ? message.content : [];
|
|
32572
32911
|
}
|
|
32912
|
+
function bedrockResponseText(response) {
|
|
32913
|
+
return bedrockResponseContent(response).map((block) => scalarToString(readObject2(block).text)).filter((text) => text !== null).join("");
|
|
32914
|
+
}
|
|
32573
32915
|
function isAzureOpenAIEndpoint(endpoint) {
|
|
32574
32916
|
if (!endpoint) {
|
|
32575
32917
|
return false;
|
|
@@ -32600,6 +32942,14 @@ function azureResponsesBaseUrl(endpoint) {
|
|
|
32600
32942
|
return `${trimmed.replace(/\/openai(?:\/v1)?$/i, "")}/openai/v1/`;
|
|
32601
32943
|
}
|
|
32602
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
|
+
}
|
|
32603
32953
|
function geminiContextWindowSize(model) {
|
|
32604
32954
|
const windows = {
|
|
32605
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>[];
|