@anuma/sdk 10.9.0 → 10.10.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.
@@ -5395,7 +5395,7 @@ async function updateMessageErrorOp(ctx, uniqueId, error) {
5395
5395
  }
5396
5396
  await ctx.database.write(async () => {
5397
5397
  await message.update((msg) => {
5398
- msg._setRaw("error", error);
5398
+ msg._setRaw("error", error ?? "");
5399
5399
  });
5400
5400
  });
5401
5401
  return messageToStored(message, ctx.walletAddress, ctx.signMessage, ctx.embeddedWalletSigner);
@@ -8354,6 +8354,67 @@ async function openInboxItem(item, keyPair) {
8354
8354
  }
8355
8355
  return opened;
8356
8356
  }
8357
+ function inboxReplaces(row, opened) {
8358
+ if (row.error) return true;
8359
+ return row.wasStopped === true && opened.status === "completed";
8360
+ }
8361
+ async function clearTurnErrors(backend, answer) {
8362
+ const parentId = answer.parentMessageId;
8363
+ if (parentId) {
8364
+ const [parent] = await backend.getMessagesByIds([parentId]);
8365
+ if (parent?.error) await backend.updateMessageError(parentId, null);
8366
+ }
8367
+ if (!answer.error) return answer;
8368
+ return await backend.updateMessageError(answer.uniqueId, null) ?? answer;
8369
+ }
8370
+ async function deliverInboxAnswers(options) {
8371
+ const { inbox, keyPair, backend, store } = options;
8372
+ const results = [];
8373
+ for (const item of await inbox.list()) {
8374
+ let opened;
8375
+ try {
8376
+ opened = await openInboxItem(item, keyPair);
8377
+ } catch (err) {
8378
+ results.push({
8379
+ id: item.id,
8380
+ conversationId: null,
8381
+ status: "unreadable",
8382
+ assistantMessage: null,
8383
+ error: err instanceof Error ? err.message : String(err)
8384
+ });
8385
+ continue;
8386
+ }
8387
+ const base = { id: item.id, conversationId: opened.conversation_id };
8388
+ try {
8389
+ const messageId = opened.assistant_message_id ?? `msg_bg_${opened.inference_id}`;
8390
+ const [found] = await backend.getMessagesByIds([messageId]);
8391
+ const existing = found && inboxReplaces(found, opened) ? void 0 : found;
8392
+ if (!found) {
8393
+ const parent = opened.parent_message_id;
8394
+ const ownsTurn = parent ? (await backend.getMessagesByIds([parent])).length > 0 : await backend.getConversation(opened.conversation_id) !== null;
8395
+ if (!ownsTurn) {
8396
+ results.push({ ...base, status: "elsewhere", assistantMessage: null });
8397
+ continue;
8398
+ }
8399
+ }
8400
+ const stored2 = existing ?? await store(opened, messageId);
8401
+ await inbox.remove(item.id);
8402
+ results.push({
8403
+ ...base,
8404
+ status: existing ? "delivered" : "stored",
8405
+ assistantMessage: stored2
8406
+ });
8407
+ } catch (err) {
8408
+ results.push({
8409
+ ...base,
8410
+ status: "retry",
8411
+ assistantMessage: null,
8412
+ error: err instanceof Error ? err.message : String(err)
8413
+ });
8414
+ }
8415
+ }
8416
+ return results;
8417
+ }
8357
8418
 
8358
8419
  // src/lib/chat/reservationReceipts.ts
8359
8420
  var ACTIONS = [
@@ -9845,6 +9906,13 @@ function createErrorResult(message, onError) {
9845
9906
  function isAbortError(err) {
9846
9907
  return err instanceof Error && err.name === "AbortError";
9847
9908
  }
9909
+ var ProviderStreamError = class extends Error {
9910
+ constructor(message, code) {
9911
+ super(message);
9912
+ this.name = "ProviderStreamError";
9913
+ this.code = code;
9914
+ }
9915
+ };
9848
9916
  function getInStreamErrorMessage(chunk) {
9849
9917
  if (!chunk || typeof chunk !== "object") return null;
9850
9918
  const obj = chunk;
@@ -10131,7 +10199,7 @@ var CompletionsStrategy = class {
10131
10199
  processStreamChunk(chunk, accumulator) {
10132
10200
  const result = { content: null, thinking: null };
10133
10201
  const inStreamErr = getInStreamErrorMessage(chunk);
10134
- if (inStreamErr) throw new Error(inStreamErr);
10202
+ if (inStreamErr) throw new ProviderStreamError(inStreamErr);
10135
10203
  const rawChunk = chunk;
10136
10204
  const typedChunk = rawChunk.response && rawChunk.type === "response" ? rawChunk.response : chunk;
10137
10205
  if (typedChunk.id && !accumulator.responseId) {
@@ -10982,7 +11050,7 @@ var ResponsesStrategy = class {
10982
11050
  const result = { content: null, thinking: null };
10983
11051
  const typedChunk = chunk;
10984
11052
  const inStreamErr = getInStreamErrorMessage(chunk);
10985
- if (inStreamErr) throw new Error(inStreamErr);
11053
+ if (inStreamErr) throw new ProviderStreamError(inStreamErr);
10986
11054
  if (typedChunk.type === "response.failed") {
10987
11055
  const resp = typedChunk.response;
10988
11056
  const err = resp?.error;
@@ -10999,9 +11067,12 @@ var ResponsesStrategy = class {
10999
11067
  }
11000
11068
  }
11001
11069
  const label = code ? `[${code}] ` : "";
11002
- throw new Error(`${label}${message || "Upstream request failed"}`);
11070
+ throw new ProviderStreamError(
11071
+ `${label}${message || "Upstream request failed"}`,
11072
+ code || void 0
11073
+ );
11003
11074
  }
11004
- throw new Error("Upstream request failed (response.failed)");
11075
+ throw new ProviderStreamError("Upstream request failed (response.failed)");
11005
11076
  }
11006
11077
  if (typedChunk.type === "response" && typedChunk.response) {
11007
11078
  const resp = typedChunk.response;
@@ -11625,13 +11696,6 @@ function isRetriableStreamError(err) {
11625
11696
  if (msg.includes("connection refused") || msg.includes("connect error")) return true;
11626
11697
  return false;
11627
11698
  }
11628
- var ProviderStreamError = class extends Error {
11629
- constructor(message, code) {
11630
- super(message);
11631
- this.name = "ProviderStreamError";
11632
- this.code = code;
11633
- }
11634
- };
11635
11699
  function extractProviderStreamError(chunk) {
11636
11700
  if (!chunk || typeof chunk !== "object") return null;
11637
11701
  const errField = chunk.error;
@@ -12012,6 +12076,7 @@ async function runToolLoop(options) {
12012
12076
  thinking: redactor.deAnonymize(acc.thinking)
12013
12077
  }) : strategy.buildFinalResponse(acc);
12014
12078
  const finalContentText = (acc) => redactor ? redactor.deAnonymize(acc.content) : acc.content;
12079
+ let streamingAccumulator = null;
12015
12080
  try {
12016
12081
  let sseError = null;
12017
12082
  let apiTools = toolsToApiFormat(tools, resolved);
@@ -12163,6 +12228,7 @@ async function runToolLoop(options) {
12163
12228
  }
12164
12229
  });
12165
12230
  accumulator = createStreamAccumulator(model || void 0);
12231
+ streamingAccumulator = accumulator;
12166
12232
  contentSmoother = new StreamSmoother((text16) => {
12167
12233
  if (emitData) emitData(text16);
12168
12234
  }, smoothing);
@@ -12673,6 +12739,7 @@ Executing tool: ${toolInfo}
12673
12739
  }
12674
12740
  });
12675
12741
  currentAccumulator = createStreamAccumulator(model || void 0);
12742
+ streamingAccumulator = currentAccumulator;
12676
12743
  contContentSmoother = new StreamSmoother((text16) => {
12677
12744
  if (emitData) emitData(text16);
12678
12745
  }, smoothing);
@@ -12870,7 +12937,7 @@ Executing tool: ${toolInfo}
12870
12937
  const statusCode = err instanceof Error && "statusCode" in err ? err.statusCode : void 0;
12871
12938
  const connectionLost = !(err instanceof ProviderStreamError) && !(err instanceof SseError);
12872
12939
  return {
12873
- data: null,
12940
+ data: connectionLost && streamingAccumulator ? buildResponseFinal(streamingAccumulator) : null,
12874
12941
  error: errorMsg,
12875
12942
  statusCode,
12876
12943
  ...connectionLost ? { connectionLost: true } : {}
@@ -40184,82 +40251,45 @@ function useChatStorage(options) {
40184
40251
  await inbox.putPublicKey(publicKey);
40185
40252
  registeredInboxKeyRef.current = { inbox, publicKey };
40186
40253
  }
40187
- const results = [];
40188
- for (const item of await inbox.list()) {
40189
- let opened;
40190
- try {
40191
- opened = await openInboxItem(item, keyPair);
40192
- } catch (err) {
40193
- results.push({
40194
- id: item.id,
40195
- conversationId: null,
40196
- status: "unreadable",
40197
- assistantMessage: null,
40198
- error: err instanceof Error ? err.message : String(err)
40254
+ return deliverInboxAnswers({
40255
+ inbox,
40256
+ keyPair,
40257
+ backend,
40258
+ store: async (opened, messageId) => {
40259
+ const { redactor } = resolveCallPii(
40260
+ void 0,
40261
+ piiRedaction,
40262
+ () => getConversationRedactor(opened.conversation_id, nerDetectorRef.current)
40263
+ );
40264
+ const knownToolCallEventIds = await backend.getToolCallEventIds(opened.conversation_id);
40265
+ const { fields, fileIds } = await toAssistantMessageFields(
40266
+ opened.response,
40267
+ opened.conversation_id,
40268
+ knownToolCallEventIds
40269
+ );
40270
+ const upserted = await backend.upsertMessage({
40271
+ ...fields,
40272
+ content: redactor ? redactor.deAnonymize(fields.content) : fields.content,
40273
+ thinking: redactor && fields.thinking ? redactor.deAnonymize(fields.thinking) : fields.thinking,
40274
+ conversationId: opened.conversation_id,
40275
+ role: "assistant",
40276
+ model: opened.model,
40277
+ parentMessageId: opened.parent_message_id ?? void 0,
40278
+ wasStopped: opened.status === "tool_request",
40279
+ uniqueId: messageId
40199
40280
  });
40200
- continue;
40201
- }
40202
- const base = { id: item.id, conversationId: opened.conversation_id };
40203
- try {
40204
- const messageId = opened.assistant_message_id ?? `msg_bg_${opened.inference_id}`;
40205
- const [existing] = await backend.getMessagesByIds([messageId]);
40206
- if (!existing) {
40207
- const parent = opened.parent_message_id;
40208
- const ownsTurn = parent ? (await backend.getMessagesByIds([parent])).length > 0 : await backend.getConversation(opened.conversation_id) !== null;
40209
- if (!ownsTurn) {
40210
- results.push({ ...base, status: "elsewhere", assistantMessage: null });
40211
- continue;
40212
- }
40213
- }
40214
- let stored2 = existing;
40215
- if (!stored2) {
40216
- const { redactor } = resolveCallPii(
40217
- void 0,
40218
- piiRedaction,
40219
- () => getConversationRedactor(opened.conversation_id, nerDetectorRef.current)
40220
- );
40221
- const knownToolCallEventIds = await backend.getToolCallEventIds(opened.conversation_id);
40222
- const { fields, fileIds } = await toAssistantMessageFields(
40223
- opened.response,
40224
- opened.conversation_id,
40225
- knownToolCallEventIds
40226
- );
40227
- stored2 = await backend.createMessage({
40228
- ...fields,
40229
- content: redactor ? redactor.deAnonymize(fields.content) : fields.content,
40230
- thinking: redactor && fields.thinking ? redactor.deAnonymize(fields.thinking) : fields.thinking,
40231
- conversationId: opened.conversation_id,
40232
- role: "assistant",
40233
- model: opened.model,
40234
- parentMessageId: opened.parent_message_id ?? void 0,
40235
- wasStopped: opened.status === "tool_request" ? true : void 0,
40236
- uniqueId: messageId
40237
- });
40238
- if (fileIds.length > 0) {
40239
- try {
40240
- await updateMediaMessageIdBatchOp(mediaCtx, fileIds, stored2.uniqueId);
40241
- } catch (err) {
40242
- getLogger().warn("[useChatStorage] Failed to link inbox media:", err);
40243
- }
40281
+ const stored2 = await clearTurnErrors(backend, upserted);
40282
+ if (fileIds.length > 0) {
40283
+ try {
40284
+ await updateMediaMessageIdBatchOp(mediaCtx, fileIds, stored2.uniqueId);
40285
+ } catch (err) {
40286
+ getLogger().warn("[useChatStorage] Failed to link inbox media:", err);
40244
40287
  }
40245
- void embedMessageAsync(stored2, (text16) => redactor ? redactor.maskText(text16) : text16);
40246
40288
  }
40247
- await inbox.remove(item.id);
40248
- results.push({
40249
- ...base,
40250
- status: existing ? "delivered" : "stored",
40251
- assistantMessage: stored2
40252
- });
40253
- } catch (err) {
40254
- results.push({
40255
- ...base,
40256
- status: "retry",
40257
- assistantMessage: null,
40258
- error: err instanceof Error ? err.message : String(err)
40259
- });
40289
+ void embedMessageAsync(stored2, (text16) => redactor ? redactor.maskText(text16) : text16);
40290
+ return stored2;
40260
40291
  }
40261
- }
40262
- return results;
40292
+ });
40263
40293
  }, [
40264
40294
  inbox,
40265
40295
  walletAddress,