@absolutejs/ai 0.0.18 → 0.0.20
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/ai/index.js +65 -1
- package/dist/ai/index.js.map +3 -3
- package/dist/src/ai/client/actions.d.ts +1 -1
- package/dist/src/ai/generateAI.d.ts +23 -1
- package/dist/src/ai/index.d.ts +2 -2
- package/dist/types/ai.d.ts +74 -4398
- package/package.json +6 -2
package/dist/ai/index.js
CHANGED
|
@@ -2977,6 +2977,69 @@ var generateAI = async (options) => {
|
|
|
2977
2977
|
}
|
|
2978
2978
|
return { text, toolCalls, usage };
|
|
2979
2979
|
};
|
|
2980
|
+
var DEFAULT_TOOL_MAX_TURNS = 6;
|
|
2981
|
+
var mergeUsage = (left, right) => {
|
|
2982
|
+
if (!left)
|
|
2983
|
+
return right;
|
|
2984
|
+
if (!right)
|
|
2985
|
+
return left;
|
|
2986
|
+
const add = (a, b) => a === undefined && b === undefined ? undefined : (a ?? 0) + (b ?? 0);
|
|
2987
|
+
return {
|
|
2988
|
+
cacheReadInputTokens: add(left.cacheReadInputTokens, right.cacheReadInputTokens),
|
|
2989
|
+
cacheWriteInputTokens: add(left.cacheWriteInputTokens, right.cacheWriteInputTokens),
|
|
2990
|
+
inputTokens: (left.inputTokens ?? 0) + (right.inputTokens ?? 0),
|
|
2991
|
+
outputTokens: (left.outputTokens ?? 0) + (right.outputTokens ?? 0)
|
|
2992
|
+
};
|
|
2993
|
+
};
|
|
2994
|
+
var toProviderTools = (tools) => Object.entries(tools).map(([name, definition]) => ({
|
|
2995
|
+
description: definition.description,
|
|
2996
|
+
input_schema: definition.input,
|
|
2997
|
+
name
|
|
2998
|
+
}));
|
|
2999
|
+
var generateAIWithTools = async (options) => {
|
|
3000
|
+
const { maxTurns = DEFAULT_TOOL_MAX_TURNS, onToolUse, tools, ...base } = options;
|
|
3001
|
+
const providerTools = toProviderTools(tools);
|
|
3002
|
+
const toolCalls = [];
|
|
3003
|
+
let usage;
|
|
3004
|
+
const runTurn = async (messages, turnsLeft) => {
|
|
3005
|
+
const result = await generateAI({
|
|
3006
|
+
...base,
|
|
3007
|
+
messages,
|
|
3008
|
+
toolChoice: "auto",
|
|
3009
|
+
tools: providerTools
|
|
3010
|
+
});
|
|
3011
|
+
usage = mergeUsage(usage, result.usage);
|
|
3012
|
+
if (result.toolCalls.length === 0 || turnsLeft <= 1) {
|
|
3013
|
+
return { messages, text: result.text, toolCalls, usage };
|
|
3014
|
+
}
|
|
3015
|
+
toolCalls.push(...result.toolCalls);
|
|
3016
|
+
const assistantBlocks = [
|
|
3017
|
+
...result.text ? [{ content: result.text, type: "text" }] : [],
|
|
3018
|
+
...result.toolCalls.map((call) => ({
|
|
3019
|
+
id: call.id,
|
|
3020
|
+
input: call.input,
|
|
3021
|
+
name: call.name,
|
|
3022
|
+
type: "tool_use"
|
|
3023
|
+
}))
|
|
3024
|
+
];
|
|
3025
|
+
const resultBlocks = await Promise.all(result.toolCalls.map(async (call) => {
|
|
3026
|
+
const definition = tools[call.name];
|
|
3027
|
+
const output = definition ? await Promise.resolve(definition.handler(call.input)).catch((err) => `Error: ${err instanceof Error ? err.message : String(err)}`) : `Error: unknown tool "${call.name}"`;
|
|
3028
|
+
onToolUse?.(call.name, call.input, output);
|
|
3029
|
+
return {
|
|
3030
|
+
content: output,
|
|
3031
|
+
tool_use_id: call.id,
|
|
3032
|
+
type: "tool_result"
|
|
3033
|
+
};
|
|
3034
|
+
}));
|
|
3035
|
+
return runTurn([
|
|
3036
|
+
...messages,
|
|
3037
|
+
{ content: assistantBlocks, role: "assistant" },
|
|
3038
|
+
{ content: resultBlocks, role: "user" }
|
|
3039
|
+
], turnsLeft - 1);
|
|
3040
|
+
};
|
|
3041
|
+
return runTurn(options.messages, maxTurns);
|
|
3042
|
+
};
|
|
2980
3043
|
var generateObjectAI = async (options) => {
|
|
2981
3044
|
const toolName = options.toolName ?? DEFAULT_OBJECT_TOOL_NAME;
|
|
2982
3045
|
const tool = {
|
|
@@ -3843,6 +3906,7 @@ export {
|
|
|
3843
3906
|
google,
|
|
3844
3907
|
generateObjectAI,
|
|
3845
3908
|
generateId,
|
|
3909
|
+
generateAIWithTools,
|
|
3846
3910
|
generateAI,
|
|
3847
3911
|
gemini,
|
|
3848
3912
|
deepseek,
|
|
@@ -3857,5 +3921,5 @@ export {
|
|
|
3857
3921
|
aiChat
|
|
3858
3922
|
};
|
|
3859
3923
|
|
|
3860
|
-
//# debugId=
|
|
3924
|
+
//# debugId=3AB7E01A7CCE98C464756E2164756E21
|
|
3861
3925
|
//# sourceMappingURL=index.js.map
|