@absolutejs/ai 0.0.17 → 0.0.19
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 +162 -40
- package/dist/ai/index.js.map +11 -10
- package/dist/ai/providers/anthropic.js +128 -8
- package/dist/ai/providers/anthropic.js.map +5 -4
- package/dist/ai/providers/openai.js +124 -7
- package/dist/ai/providers/openai.js.map +5 -4
- package/dist/ai/providers/openaiCompatible.js +124 -7
- package/dist/ai/providers/openaiCompatible.js.map +5 -4
- package/dist/ai/providers/openaiResponses.js +114 -6
- package/dist/ai/providers/openaiResponses.js.map +5 -4
- package/dist/src/ai/client/actions.d.ts +1 -1
- package/dist/src/ai/generateAI.d.ts +5 -1
- package/dist/src/ai/providers/reasoning.d.ts +14 -0
- package/dist/types/ai.d.ts +104 -4410
- package/package.json +6 -2
package/dist/ai/index.js
CHANGED
|
@@ -43,6 +43,111 @@ async function* tapStream(source, params, providerName) {
|
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
+
// src/ai/providers/reasoning.ts
|
|
47
|
+
var EFFORT_ORDER = [
|
|
48
|
+
"minimal",
|
|
49
|
+
"low",
|
|
50
|
+
"medium",
|
|
51
|
+
"high",
|
|
52
|
+
"max"
|
|
53
|
+
];
|
|
54
|
+
var ANTHROPIC_EFFORT = [
|
|
55
|
+
/opus-4-[5-8]/,
|
|
56
|
+
/sonnet-4-6/,
|
|
57
|
+
/fable-5/,
|
|
58
|
+
/mythos-5/
|
|
59
|
+
];
|
|
60
|
+
var ANTHROPIC_ADAPTIVE_ONLY = [];
|
|
61
|
+
var ANTHROPIC_LEGACY_THINKING = [
|
|
62
|
+
/sonnet-4-5/,
|
|
63
|
+
/sonnet-4-0/,
|
|
64
|
+
/sonnet-4-2025/,
|
|
65
|
+
/opus-4-0/,
|
|
66
|
+
/opus-4-1/,
|
|
67
|
+
/opus-4-2025/,
|
|
68
|
+
/3-7-sonnet/
|
|
69
|
+
];
|
|
70
|
+
var ANTHROPIC_NO_SAMPLING = [/opus-4-[78]/, /fable-5/, /mythos-5/];
|
|
71
|
+
var ANTHROPIC_MAX_EFFORT = [
|
|
72
|
+
/opus-4-[678]/,
|
|
73
|
+
/sonnet-4-6/,
|
|
74
|
+
/fable-5/,
|
|
75
|
+
/mythos-5/
|
|
76
|
+
];
|
|
77
|
+
var OPENAI_REASONING = [/(^|[^a-z])o[1345](-|$)/, /gpt-5/];
|
|
78
|
+
var OPENAI_MINIMAL_EFFORT = [/gpt-5/];
|
|
79
|
+
var matches = (model, patterns) => patterns.some((pattern) => pattern.test(model));
|
|
80
|
+
var anthropicReasoningMode = (model) => {
|
|
81
|
+
if (matches(model, ANTHROPIC_EFFORT))
|
|
82
|
+
return "effort";
|
|
83
|
+
if (matches(model, ANTHROPIC_ADAPTIVE_ONLY))
|
|
84
|
+
return "adaptive";
|
|
85
|
+
if (matches(model, ANTHROPIC_LEGACY_THINKING))
|
|
86
|
+
return "legacy";
|
|
87
|
+
return "none";
|
|
88
|
+
};
|
|
89
|
+
var anthropicSupportsSampling = (model) => !matches(model, ANTHROPIC_NO_SAMPLING);
|
|
90
|
+
var isOpenAIReasoningModel = (model) => matches(model, OPENAI_REASONING);
|
|
91
|
+
var EFFORT_BUDGET = {
|
|
92
|
+
high: 16384,
|
|
93
|
+
low: 2048,
|
|
94
|
+
max: 32768,
|
|
95
|
+
medium: 8192,
|
|
96
|
+
minimal: 1024
|
|
97
|
+
};
|
|
98
|
+
var budgetToEffort = (budget) => {
|
|
99
|
+
if (budget <= 2048)
|
|
100
|
+
return "low";
|
|
101
|
+
if (budget <= 8192)
|
|
102
|
+
return "medium";
|
|
103
|
+
if (budget <= 16384)
|
|
104
|
+
return "high";
|
|
105
|
+
return "max";
|
|
106
|
+
};
|
|
107
|
+
var resolveEffort = (reasoning) => {
|
|
108
|
+
if (reasoning.effort)
|
|
109
|
+
return reasoning.effort;
|
|
110
|
+
if (typeof reasoning.budgetTokens === "number") {
|
|
111
|
+
return budgetToEffort(reasoning.budgetTokens);
|
|
112
|
+
}
|
|
113
|
+
return null;
|
|
114
|
+
};
|
|
115
|
+
var resolveBudgetTokens = (reasoning) => {
|
|
116
|
+
if (typeof reasoning.budgetTokens === "number")
|
|
117
|
+
return reasoning.budgetTokens;
|
|
118
|
+
if (reasoning.effort)
|
|
119
|
+
return EFFORT_BUDGET[reasoning.effort];
|
|
120
|
+
return null;
|
|
121
|
+
};
|
|
122
|
+
var clampEffort = (effort, allowed) => {
|
|
123
|
+
if (allowed.includes(effort))
|
|
124
|
+
return effort;
|
|
125
|
+
const idx = EFFORT_ORDER.indexOf(effort);
|
|
126
|
+
for (let lower = idx - 1;lower >= 0; lower -= 1) {
|
|
127
|
+
const candidate = EFFORT_ORDER[lower];
|
|
128
|
+
if (candidate && allowed.includes(candidate))
|
|
129
|
+
return candidate;
|
|
130
|
+
}
|
|
131
|
+
return allowed[0] ?? effort;
|
|
132
|
+
};
|
|
133
|
+
var anthropicEffortValue = (model, reasoning) => {
|
|
134
|
+
const effort = resolveEffort(reasoning);
|
|
135
|
+
if (!effort)
|
|
136
|
+
return null;
|
|
137
|
+
const allowed = matches(model, ANTHROPIC_MAX_EFFORT) ? ["low", "medium", "high", "max"] : ["low", "medium", "high"];
|
|
138
|
+
return clampEffort(effort, allowed);
|
|
139
|
+
};
|
|
140
|
+
var openaiEffortValue = (model, reasoning) => {
|
|
141
|
+
if (!isOpenAIReasoningModel(model))
|
|
142
|
+
return null;
|
|
143
|
+
const effort = resolveEffort(reasoning);
|
|
144
|
+
if (!effort)
|
|
145
|
+
return null;
|
|
146
|
+
const allowed = matches(model, OPENAI_MINIMAL_EFFORT) ? ["minimal", "low", "medium", "high"] : ["low", "medium", "high"];
|
|
147
|
+
const requested = effort === "max" ? "high" : effort;
|
|
148
|
+
return clampEffort(requested, allowed);
|
|
149
|
+
};
|
|
150
|
+
|
|
46
151
|
// src/ai/providers/openai.ts
|
|
47
152
|
var h2IfHttps = (url) => url.startsWith("https://") ? { protocol: "http2" } : {};
|
|
48
153
|
var DEFAULT_BASE_URL = "https://api.openai.com";
|
|
@@ -169,12 +274,24 @@ var buildRequestBody = (params) => {
|
|
|
169
274
|
body.parallel_tool_calls = params.parallelToolCalls;
|
|
170
275
|
}
|
|
171
276
|
}
|
|
172
|
-
if (
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
277
|
+
if (isOpenAIReasoningModel(params.model)) {
|
|
278
|
+
if (typeof params.maxTokens === "number") {
|
|
279
|
+
body.max_completion_tokens = params.maxTokens;
|
|
280
|
+
}
|
|
281
|
+
if (params.reasoning) {
|
|
282
|
+
const effort = openaiEffortValue(params.model, params.reasoning);
|
|
283
|
+
if (effort)
|
|
284
|
+
body.reasoning_effort = effort;
|
|
285
|
+
}
|
|
286
|
+
} else {
|
|
287
|
+
if (typeof params.temperature === "number") {
|
|
288
|
+
body.temperature = params.temperature;
|
|
289
|
+
}
|
|
290
|
+
if (typeof params.topP === "number")
|
|
291
|
+
body.top_p = params.topP;
|
|
292
|
+
if (typeof params.maxTokens === "number")
|
|
293
|
+
body.max_tokens = params.maxTokens;
|
|
294
|
+
}
|
|
178
295
|
if (params.stopSequences && params.stopSequences.length > 0)
|
|
179
296
|
body.stop = params.stopSequences;
|
|
180
297
|
if (typeof params.seed === "number")
|
|
@@ -591,11 +708,14 @@ var buildRequestBody2 = (params, isImageModel) => {
|
|
|
591
708
|
};
|
|
592
709
|
}
|
|
593
710
|
}
|
|
594
|
-
if (params.
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
711
|
+
if (params.reasoning && isOpenAIReasoningModel(params.model)) {
|
|
712
|
+
const effort = openaiEffortValue(params.model, params.reasoning);
|
|
713
|
+
if (effort) {
|
|
714
|
+
body.reasoning = {
|
|
715
|
+
effort,
|
|
716
|
+
summary: "auto"
|
|
717
|
+
};
|
|
718
|
+
}
|
|
599
719
|
}
|
|
600
720
|
return body;
|
|
601
721
|
};
|
|
@@ -1239,18 +1359,33 @@ var buildRequestBody4 = (params) => {
|
|
|
1239
1359
|
body.tool_choice = { name: params.toolChoice.name, type: "tool" };
|
|
1240
1360
|
}
|
|
1241
1361
|
}
|
|
1242
|
-
if (typeof params.temperature === "number")
|
|
1243
|
-
body.temperature = params.temperature;
|
|
1244
|
-
if (typeof params.topP === "number")
|
|
1245
|
-
body.top_p = params.topP;
|
|
1246
1362
|
if (typeof params.maxTokens === "number")
|
|
1247
1363
|
body.max_tokens = params.maxTokens;
|
|
1248
1364
|
if (params.stopSequences && params.stopSequences.length > 0) {
|
|
1249
1365
|
body.stop_sequences = params.stopSequences;
|
|
1250
1366
|
}
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1367
|
+
const mode = params.reasoning ? anthropicReasoningMode(params.model) : "none";
|
|
1368
|
+
const thinkingActive = mode !== "none";
|
|
1369
|
+
if (!thinkingActive && anthropicSupportsSampling(params.model)) {
|
|
1370
|
+
if (typeof params.temperature === "number") {
|
|
1371
|
+
body.temperature = params.temperature;
|
|
1372
|
+
}
|
|
1373
|
+
if (typeof params.topP === "number")
|
|
1374
|
+
body.top_p = params.topP;
|
|
1375
|
+
}
|
|
1376
|
+
if (mode === "effort" || mode === "adaptive") {
|
|
1377
|
+
body.thinking = { type: "adaptive" };
|
|
1378
|
+
if (mode === "effort" && params.reasoning) {
|
|
1379
|
+
const effort = anthropicEffortValue(params.model, params.reasoning);
|
|
1380
|
+
if (effort)
|
|
1381
|
+
body.output_config = { effort };
|
|
1382
|
+
}
|
|
1383
|
+
} else if (mode === "legacy" && params.reasoning) {
|
|
1384
|
+
const budget = resolveBudgetTokens(params.reasoning);
|
|
1385
|
+
if (budget) {
|
|
1386
|
+
body.thinking = { budget_tokens: budget, type: "enabled" };
|
|
1387
|
+
body.max_tokens = Math.max(typeof body.max_tokens === "number" ? body.max_tokens : MAX_TOKENS, budget + MAX_TOKENS);
|
|
1388
|
+
}
|
|
1254
1389
|
}
|
|
1255
1390
|
return body;
|
|
1256
1391
|
};
|
|
@@ -1901,7 +2036,6 @@ var WS_OPEN = 1;
|
|
|
1901
2036
|
var BACKPRESSURE_THRESHOLD = 1048576;
|
|
1902
2037
|
var BACKPRESSURE_DELAY = 10;
|
|
1903
2038
|
var DEFAULT_MAX_TURNS = 10;
|
|
1904
|
-
var DEFAULT_THINKING_BUDGET = 1e4;
|
|
1905
2039
|
var INITIAL_TURN = 0;
|
|
1906
2040
|
var delay = (milliseconds) => new Promise((resolve) => setTimeout(resolve, milliseconds));
|
|
1907
2041
|
var checkBackpressure = async (socket) => {
|
|
@@ -2083,16 +2217,12 @@ var processToolTurn = async (socket, options, state, messageId, conversationId,
|
|
|
2083
2217
|
role: "user"
|
|
2084
2218
|
});
|
|
2085
2219
|
const toolDefs = options.tools ? buildToolDefinitions(options.tools) : undefined;
|
|
2086
|
-
const thinkingConfig = options.thinking ? {
|
|
2087
|
-
budget_tokens: typeof options.thinking === "object" ? options.thinking.budgetTokens : DEFAULT_THINKING_BUDGET,
|
|
2088
|
-
type: "enabled"
|
|
2089
|
-
} : undefined;
|
|
2090
2220
|
const stream = options.provider.stream({
|
|
2091
2221
|
messages: state.currentMessages,
|
|
2092
2222
|
model: options.model,
|
|
2223
|
+
reasoning: options.reasoning,
|
|
2093
2224
|
signal,
|
|
2094
2225
|
systemPrompt: options.systemPrompt,
|
|
2095
|
-
thinking: thinkingConfig,
|
|
2096
2226
|
tools: toolDefs
|
|
2097
2227
|
});
|
|
2098
2228
|
await consumeToolStream(stream, state, options, socket, messageId, conversationId, signal);
|
|
@@ -2171,16 +2301,12 @@ var processStreamTextChunk = async (chunk, options, socket, messageId, conversat
|
|
|
2171
2301
|
};
|
|
2172
2302
|
var processStream = async (socket, options, messages, messageId, conversationId, signal, startTime) => {
|
|
2173
2303
|
const toolDefs = options.tools ? buildToolDefinitions(options.tools) : undefined;
|
|
2174
|
-
const thinkingConfig = options.thinking ? {
|
|
2175
|
-
budget_tokens: typeof options.thinking === "object" ? options.thinking.budgetTokens : DEFAULT_THINKING_BUDGET,
|
|
2176
|
-
type: "enabled"
|
|
2177
|
-
} : undefined;
|
|
2178
2304
|
const stream = options.provider.stream({
|
|
2179
2305
|
messages,
|
|
2180
2306
|
model: options.model,
|
|
2307
|
+
reasoning: options.reasoning,
|
|
2181
2308
|
signal,
|
|
2182
2309
|
systemPrompt: options.systemPrompt,
|
|
2183
|
-
thinking: thinkingConfig,
|
|
2184
2310
|
tools: toolDefs
|
|
2185
2311
|
});
|
|
2186
2312
|
const result = await consumeStream(stream, options, socket, messages, messageId, conversationId, signal);
|
|
@@ -2290,7 +2416,6 @@ var handleStreamError = async (socket, err, messageId, conversationId, signal, s
|
|
|
2290
2416
|
|
|
2291
2417
|
// src/ai/streamAIToSSE.ts
|
|
2292
2418
|
var DEFAULT_MAX_TURNS2 = 10;
|
|
2293
|
-
var DEFAULT_THINKING_BUDGET2 = 1e4;
|
|
2294
2419
|
var buildToolDefinitions2 = (tools) => Object.entries(tools).map(([name, def]) => ({
|
|
2295
2420
|
description: def.description,
|
|
2296
2421
|
input_schema: def.input,
|
|
@@ -2307,10 +2432,6 @@ var executeTool2 = async (options, toolName, toolInput) => {
|
|
|
2307
2432
|
return `Error: ${err instanceof Error ? err.message : String(err)}`;
|
|
2308
2433
|
}
|
|
2309
2434
|
};
|
|
2310
|
-
var buildThinkingConfig = (options) => options.thinking ? {
|
|
2311
|
-
budget_tokens: typeof options.thinking === "object" ? options.thinking.budgetTokens : DEFAULT_THINKING_BUDGET2,
|
|
2312
|
-
type: "enabled"
|
|
2313
|
-
} : undefined;
|
|
2314
2435
|
var serializeToolCall2 = (name, input) => `${name}:${JSON.stringify(input)}`;
|
|
2315
2436
|
var streamAIToSSE = async function* (conversationId, messageId, options, renderers) {
|
|
2316
2437
|
const signal = options.signal ?? new AbortController().signal;
|
|
@@ -2471,7 +2592,6 @@ var streamTurns = async function* (options, renderers, messages, signal, startTi
|
|
|
2471
2592
|
turn: 0
|
|
2472
2593
|
};
|
|
2473
2594
|
const toolDefs = options.tools ? buildToolDefinitions2(options.tools) : undefined;
|
|
2474
|
-
const thinkingConfig = buildThinkingConfig(options);
|
|
2475
2595
|
for (;turnState.turn <= maxTurns && !signal.aborted; turnState.turn++) {
|
|
2476
2596
|
const chunkState = {
|
|
2477
2597
|
contentBlocks: [],
|
|
@@ -2482,9 +2602,9 @@ var streamTurns = async function* (options, renderers, messages, signal, startTi
|
|
|
2482
2602
|
const stream = options.provider.stream({
|
|
2483
2603
|
messages: turnState.currentMessages,
|
|
2484
2604
|
model: options.model,
|
|
2605
|
+
reasoning: options.reasoning,
|
|
2485
2606
|
signal,
|
|
2486
2607
|
systemPrompt: options.systemPrompt,
|
|
2487
|
-
thinking: thinkingConfig,
|
|
2488
2608
|
tools: toolDefs
|
|
2489
2609
|
});
|
|
2490
2610
|
yield* consumeStream2(stream, chunkState, renderers, options, turnState, signal);
|
|
@@ -2640,7 +2760,7 @@ var resolveModel = (config, parsed) => {
|
|
|
2640
2760
|
return parsed.providerName;
|
|
2641
2761
|
};
|
|
2642
2762
|
var resolveTools = (config, providerName, model) => typeof config.tools === "function" ? config.tools(providerName, model) : config.tools;
|
|
2643
|
-
var
|
|
2763
|
+
var resolveReasoning = (config, providerName, model) => typeof config.reasoning === "function" ? config.reasoning(providerName, model) : config.reasoning;
|
|
2644
2764
|
var aiChat = (config) => {
|
|
2645
2765
|
const path = config.path ?? DEFAULT_PATH;
|
|
2646
2766
|
const store = config.store ?? createMemoryStore();
|
|
@@ -2691,7 +2811,7 @@ var aiChat = (config) => {
|
|
|
2691
2811
|
provider: config.provider(providerName),
|
|
2692
2812
|
signal: controller.signal,
|
|
2693
2813
|
systemPrompt: config.systemPrompt,
|
|
2694
|
-
|
|
2814
|
+
reasoning: resolveReasoning(config, providerName, model),
|
|
2695
2815
|
tools: resolveTools(config, providerName, model),
|
|
2696
2816
|
onComplete: async (fullResponse, usage) => {
|
|
2697
2817
|
const conv = await store.get(conversationId);
|
|
@@ -2765,7 +2885,7 @@ var aiChat = (config) => {
|
|
|
2765
2885
|
provider: config.provider(providerName),
|
|
2766
2886
|
signal: controller.signal,
|
|
2767
2887
|
systemPrompt: config.systemPrompt,
|
|
2768
|
-
|
|
2888
|
+
reasoning: resolveReasoning(config, providerName, model),
|
|
2769
2889
|
tools: resolveTools(config, providerName, model)
|
|
2770
2890
|
}, renderers);
|
|
2771
2891
|
for await (const event of sseStream) {
|
|
@@ -2833,6 +2953,7 @@ var generateAI = async (options) => {
|
|
|
2833
2953
|
maxTokens: options.maxTokens,
|
|
2834
2954
|
messages: options.messages,
|
|
2835
2955
|
model: options.model,
|
|
2956
|
+
reasoning: options.reasoning,
|
|
2836
2957
|
responseFormat: options.responseFormat,
|
|
2837
2958
|
signal: options.signal,
|
|
2838
2959
|
stopSequences: options.stopSequences,
|
|
@@ -2869,6 +2990,7 @@ var generateObjectAI = async (options) => {
|
|
|
2869
2990
|
messages: options.messages,
|
|
2870
2991
|
model: options.model,
|
|
2871
2992
|
provider: options.provider,
|
|
2993
|
+
reasoning: options.reasoning,
|
|
2872
2994
|
signal: options.signal,
|
|
2873
2995
|
systemPrompt: options.systemPrompt,
|
|
2874
2996
|
temperature: options.temperature,
|
|
@@ -3735,5 +3857,5 @@ export {
|
|
|
3735
3857
|
aiChat
|
|
3736
3858
|
};
|
|
3737
3859
|
|
|
3738
|
-
//# debugId=
|
|
3860
|
+
//# debugId=F448AA17322C183164756E2164756E21
|
|
3739
3861
|
//# sourceMappingURL=index.js.map
|