@absolutejs/ai 0.0.27 → 0.0.29

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 CHANGED
@@ -1601,9 +1601,11 @@ var cacheLastContentBlock = (msg) => {
1601
1601
  };
1602
1602
  return { content: blocks, role: msg.role };
1603
1603
  };
1604
- var buildRequestBody4 = (params, configuredMax, promptCaching) => {
1604
+ var buildRequestBody4 = (params, configuredMax, configCaching) => {
1605
+ const caching = params.promptCaching ?? configCaching;
1606
+ const cacheSystem = params.cacheSystemPrompt ?? caching;
1605
1607
  const messages = params.messages.filter((msg) => msg.role !== "system").map(mapMessage);
1606
- if (promptCaching && messages.length > 1) {
1608
+ if (caching && messages.length > 1) {
1607
1609
  const last = messages[messages.length - 1];
1608
1610
  if (last) {
1609
1611
  messages[messages.length - 1] = cacheLastContentBlock(last);
@@ -1617,7 +1619,7 @@ var buildRequestBody4 = (params, configuredMax, promptCaching) => {
1617
1619
  stream: true
1618
1620
  };
1619
1621
  if (params.systemPrompt) {
1620
- body.system = promptCaching || params.cacheSystemPrompt ? [
1622
+ body.system = cacheSystem ? [
1621
1623
  {
1622
1624
  cache_control: { type: "ephemeral" },
1623
1625
  text: params.systemPrompt,
@@ -1627,7 +1629,7 @@ var buildRequestBody4 = (params, configuredMax, promptCaching) => {
1627
1629
  }
1628
1630
  if (params.tools && params.tools.length > 0) {
1629
1631
  const tools = params.tools.map(mapToolDefinition2);
1630
- if (promptCaching) {
1632
+ if (caching) {
1631
1633
  tools[tools.length - 1] = {
1632
1634
  ...tools[tools.length - 1],
1633
1635
  cache_control: { type: "ephemeral" }
@@ -2521,8 +2523,10 @@ var processToolTurn = async (socket, options, state, messageId, conversationId,
2521
2523
  });
2522
2524
  const toolDefs = options.tools ? buildToolDefinitions(options.tools) : undefined;
2523
2525
  const stream = options.provider.stream({
2526
+ cacheSystemPrompt: options.cacheSystemPrompt,
2524
2527
  messages: state.currentMessages,
2525
2528
  model: options.model,
2529
+ promptCaching: options.promptCaching,
2526
2530
  reasoning: options.reasoning,
2527
2531
  signal,
2528
2532
  systemPrompt: options.systemPrompt,
@@ -2605,8 +2609,10 @@ var processStreamTextChunk = async (chunk, options, socket, messageId, conversat
2605
2609
  var processStream = async (socket, options, messages, messageId, conversationId, signal, startTime) => {
2606
2610
  const toolDefs = options.tools ? buildToolDefinitions(options.tools) : undefined;
2607
2611
  const stream = options.provider.stream({
2612
+ cacheSystemPrompt: options.cacheSystemPrompt,
2608
2613
  messages,
2609
2614
  model: options.model,
2615
+ promptCaching: options.promptCaching,
2610
2616
  reasoning: options.reasoning,
2611
2617
  signal,
2612
2618
  systemPrompt: options.systemPrompt,
@@ -2954,9 +2960,11 @@ var streamTurns = async function* (options, renderers, messages, signal, startTi
2954
2960
  usage: undefined
2955
2961
  };
2956
2962
  const stream = options.provider.stream({
2963
+ cacheSystemPrompt: options.cacheSystemPrompt,
2957
2964
  maxTokens: options.maxTokens,
2958
2965
  messages: turnState.currentMessages,
2959
2966
  model: options.model,
2967
+ promptCaching: options.promptCaching,
2960
2968
  reasoning: options.reasoning,
2961
2969
  signal,
2962
2970
  systemPrompt: options.systemPrompt,
@@ -3331,6 +3339,7 @@ var generateAI = async (options) => {
3331
3339
  maxTokens: options.maxTokens,
3332
3340
  messages: options.messages,
3333
3341
  model: options.model,
3342
+ promptCaching: options.promptCaching,
3334
3343
  reasoning: options.reasoning,
3335
3344
  responseFormat: options.responseFormat,
3336
3345
  signal: options.signal,
@@ -3423,6 +3432,7 @@ var generateAIWithTools = async (options) => {
3423
3432
  };
3424
3433
  return runTurn(options.messages, maxTurns);
3425
3434
  };
3435
+ var DEFAULT_OBJECT_REPAIR_ATTEMPTS = 1;
3426
3436
  var generateObjectAI = async (options) => {
3427
3437
  const toolName = options.toolName ?? DEFAULT_OBJECT_TOOL_NAME;
3428
3438
  const tool = {
@@ -3430,25 +3440,66 @@ var generateObjectAI = async (options) => {
3430
3440
  input_schema: options.schema,
3431
3441
  name: toolName
3432
3442
  };
3433
- const { toolCalls, usage } = await generateAI({
3434
- cacheSystemPrompt: options.cacheSystemPrompt,
3435
- maxTokens: options.maxTokens,
3436
- messages: options.messages,
3437
- model: options.model,
3438
- provider: options.provider,
3439
- reasoning: options.reasoning,
3440
- signal: options.signal,
3441
- systemPrompt: options.systemPrompt,
3442
- temperature: options.temperature,
3443
- toolChoice: { name: toolName },
3444
- tools: [tool]
3445
- });
3446
- const call = toolCalls.find((toolCall) => toolCall.name === toolName);
3447
- if (!call) {
3448
- throw new Error(`generateObjectAI: model did not call the "${toolName}" tool`);
3443
+ const maxRepairAttempts = Math.max(0, options.maxRepairAttempts ?? DEFAULT_OBJECT_REPAIR_ATTEMPTS);
3444
+ const messages = [...options.messages];
3445
+ let usage;
3446
+ let lastError;
3447
+ for (let attempt = 0;attempt <= maxRepairAttempts; attempt += 1) {
3448
+ const result = await generateAI({
3449
+ cacheSystemPrompt: options.cacheSystemPrompt,
3450
+ maxTokens: options.maxTokens,
3451
+ messages,
3452
+ model: options.model,
3453
+ promptCaching: options.promptCaching,
3454
+ provider: options.provider,
3455
+ reasoning: options.reasoning,
3456
+ signal: options.signal,
3457
+ systemPrompt: options.systemPrompt,
3458
+ temperature: options.temperature,
3459
+ toolChoice: { name: toolName },
3460
+ tools: [tool]
3461
+ });
3462
+ usage = mergeUsage(usage, result.usage);
3463
+ const call = result.toolCalls.find((toolCall) => toolCall.name === toolName);
3464
+ let failure;
3465
+ let object;
3466
+ if (!call) {
3467
+ lastError = new Error(`generateObjectAI: model did not call the "${toolName}" tool`);
3468
+ failure = `You did not call the "${toolName}" tool. Call it exactly once with the structured result.`;
3469
+ } else {
3470
+ try {
3471
+ object = options.validate ? options.validate(call.input) : call.input;
3472
+ } catch (error) {
3473
+ lastError = error;
3474
+ failure = `Your "${toolName}" output failed validation: ${error instanceof Error ? error.message : String(error)}. Call "${toolName}" again with corrected output that satisfies the schema.`;
3475
+ }
3476
+ }
3477
+ if (failure === undefined)
3478
+ return { object, usage };
3479
+ if (attempt >= maxRepairAttempts)
3480
+ break;
3481
+ if (call) {
3482
+ messages.push({
3483
+ content: [
3484
+ {
3485
+ id: call.id,
3486
+ input: call.input && typeof call.input === "object" ? call.input : {},
3487
+ name: toolName,
3488
+ type: "tool_use"
3489
+ }
3490
+ ],
3491
+ role: "assistant"
3492
+ }, {
3493
+ content: [
3494
+ { content: failure, tool_use_id: call.id, type: "tool_result" }
3495
+ ],
3496
+ role: "user"
3497
+ });
3498
+ } else {
3499
+ messages.push({ content: failure, role: "user" });
3500
+ }
3449
3501
  }
3450
- const object = options.validate ? options.validate(call.input) : call.input;
3451
- return { object, usage };
3502
+ throw lastError instanceof Error ? lastError : new Error(`generateObjectAI: failed to produce valid output`);
3452
3503
  };
3453
3504
  // src/ai/conversationManager.ts
3454
3505
  var NOT_FOUND3 = -1;
@@ -4410,5 +4461,5 @@ export {
4410
4461
  PROVIDER_STATUS_PAGES
4411
4462
  };
4412
4463
 
4413
- //# debugId=4EA405589A58240564756E2164756E21
4464
+ //# debugId=AAB25EB53BF9BE9064756E2164756E21
4414
4465
  //# sourceMappingURL=index.js.map