@absolutejs/ai 0.3.3 → 0.4.0

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.
@@ -2058,9 +2058,15 @@ var buildRequestBody2 = (params, configuredMax, configCaching) => {
2058
2058
  const options = requestOptionsFor(params);
2059
2059
  const messages = params.messages.filter((msg) => msg.role !== "system").map(mapMessage);
2060
2060
  if (caching && messages.length > 1) {
2061
- const last = messages[messages.length - 1];
2061
+ const lastIndex = messages.length - 1;
2062
+ const last = messages[lastIndex];
2062
2063
  if (last) {
2063
- messages[messages.length - 1] = cacheLastContentBlock(last);
2064
+ messages[lastIndex] = cacheLastContentBlock(last);
2065
+ }
2066
+ const previousUserIndex = messages.findLastIndex((message, index) => index < lastIndex - 1 && message.role === "user");
2067
+ const previous = messages[previousUserIndex];
2068
+ if (previous) {
2069
+ messages[previousUserIndex] = cacheLastContentBlock(previous);
2064
2070
  }
2065
2071
  }
2066
2072
  const max = typeof params.maxTokens === "number" ? params.maxTokens : configuredMax;
@@ -2195,14 +2201,79 @@ var getNumber = (obj, key) => {
2195
2201
  }
2196
2202
  return 0;
2197
2203
  };
2204
+ var TOOL_INPUT_PROGRESS_CHARS = 2048;
2205
+ var TOOL_INPUT_FIELD_MAX_CHARS = 200;
2206
+ var readJsonString = (json, start) => {
2207
+ for (let index = start + 1;index < json.length; index += 1) {
2208
+ if (json[index] === "\\")
2209
+ index += 1;
2210
+ else if (json[index] === '"')
2211
+ return { end: index + 1, raw: json.slice(start + 1, index) };
2212
+ }
2213
+ return;
2214
+ };
2215
+ var decodeJsonString = (raw) => {
2216
+ try {
2217
+ const value = JSON.parse(`"${raw}"`);
2218
+ return typeof value === "string" ? value : undefined;
2219
+ } catch {
2220
+ return;
2221
+ }
2222
+ };
2223
+ var completeStringFields = (partialJson) => {
2224
+ const fields = {};
2225
+ let depth = 0;
2226
+ let key;
2227
+ let index = 0;
2228
+ while (index < partialJson.length) {
2229
+ const character = partialJson[index];
2230
+ if (character === '"') {
2231
+ const text = readJsonString(partialJson, index);
2232
+ if (!text)
2233
+ break;
2234
+ if (depth === 1 && key === undefined) {
2235
+ const after = partialJson.slice(text.end).trimStart();
2236
+ if (after.startsWith(":"))
2237
+ key = text.raw;
2238
+ } else if (depth === 1 && key !== undefined) {
2239
+ const value = decodeJsonString(text.raw);
2240
+ if (value !== undefined && value.length <= TOOL_INPUT_FIELD_MAX_CHARS)
2241
+ fields[key] = value;
2242
+ key = undefined;
2243
+ }
2244
+ index = text.end;
2245
+ continue;
2246
+ }
2247
+ if (character === "{" || character === "[")
2248
+ depth += 1;
2249
+ else if (character === "}" || character === "]")
2250
+ depth -= 1;
2251
+ else if (character === "," && depth === 1)
2252
+ key = undefined;
2253
+ index += 1;
2254
+ }
2255
+ return fields;
2256
+ };
2257
+ var toolInputProgress = (state) => {
2258
+ state.toolInputReported = state.toolInputJson.length;
2259
+ return {
2260
+ fields: completeStringFields(state.toolInputJson),
2261
+ id: state.currentToolId,
2262
+ name: state.currentToolName,
2263
+ receivedChars: state.toolInputJson.length,
2264
+ type: "tool_input"
2265
+ };
2266
+ };
2198
2267
  var handleContentBlockStart = (parsed, state) => {
2199
2268
  const block = getRecord(parsed, "content_block");
2200
2269
  if (block && block.type === "tool_use") {
2201
2270
  state.currentToolId = getString(block, "id");
2202
2271
  state.currentToolName = getString(block, "name");
2203
2272
  state.toolInputJson = "";
2273
+ state.toolInputReported = 0;
2204
2274
  state.isThinkingBlock = false;
2205
2275
  state.currentProviderBlock = undefined;
2276
+ return toolInputProgress(state);
2206
2277
  } else if (block && block.type === "thinking") {
2207
2278
  state.isThinkingBlock = true;
2208
2279
  state.thinkingSignature = "";
@@ -2212,6 +2283,7 @@ var handleContentBlockStart = (parsed, state) => {
2212
2283
  state.currentProviderBlock = block && block.type !== "text" ? { ...block } : undefined;
2213
2284
  state.providerBlockInputJson = "";
2214
2285
  }
2286
+ return;
2215
2287
  };
2216
2288
  var handleContentBlockDelta = (parsed, state) => {
2217
2289
  const delta = getRecord(parsed, "delta");
@@ -2246,6 +2318,8 @@ var handleContentBlockDelta = (parsed, state) => {
2246
2318
  state.providerBlockInputJson += getString(delta, "partial_json");
2247
2319
  } else {
2248
2320
  state.toolInputJson += getString(delta, "partial_json");
2321
+ if (state.toolInputJson.length - state.toolInputReported >= TOOL_INPUT_PROGRESS_CHARS)
2322
+ return toolInputProgress(state);
2249
2323
  }
2250
2324
  }
2251
2325
  if (delta.type === "signature_delta") {
@@ -2365,8 +2439,7 @@ var handleError = (parsed, state) => {
2365
2439
  var processEvent = (eventType, parsed, state) => {
2366
2440
  switch (eventType) {
2367
2441
  case "content_block_start": {
2368
- handleContentBlockStart(parsed, state);
2369
- return;
2442
+ return handleContentBlockStart(parsed, state);
2370
2443
  }
2371
2444
  case "content_block_delta": {
2372
2445
  return handleContentBlockDelta(parsed, state);
@@ -2469,6 +2542,7 @@ async function* parseSSEStream3(body, providerName, signal) {
2469
2542
  stopReason: "",
2470
2543
  thinkingSignature: "",
2471
2544
  toolInputJson: "",
2545
+ toolInputReported: 0,
2472
2546
  usage: undefined,
2473
2547
  providerName,
2474
2548
  providerBlockInputJson: ""
@@ -3512,5 +3586,5 @@ export {
3512
3586
  verifyOpenRouterWebhookSignature
3513
3587
  };
3514
3588
 
3515
- //# debugId=B813A4B59FA2961264756E2164756E21
3589
+ //# debugId=28EECE403772185564756E2164756E21
3516
3590
  //# sourceMappingURL=openrouter.js.map