teodor-new-chat-ui 4.3.355 → 4.3.356

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.
@@ -72,4 +72,3 @@ export declare function useMessagesReducer(initial?: ChatMessage[]): {
72
72
  readonly state: MessagesState;
73
73
  readonly dispatch: import("react").Dispatch<MessagesAction>;
74
74
  };
75
- export declare function computeSuffix(existing: string, incoming: string): string;
@@ -0,0 +1,37 @@
1
+ import type { ChatMessage, MessagePart } from "@/types/api";
2
+ import type { ToolStreamEntry } from "@/types/toolStream";
3
+ export declare function sanitizeHistory(messages: ChatMessage[]): ChatMessage[];
4
+ export declare function replaceMessageAtIndex(messages: ChatMessage[], index: number, message: ChatMessage): ChatMessage[];
5
+ export declare function stripInterruptParts(messages: ChatMessage[]): ChatMessage[];
6
+ export declare function findLastAssistantId(messages: ChatMessage[]): string | null;
7
+ export declare function collectToolEventIds(ev: {
8
+ id?: unknown;
9
+ call_id?: unknown;
10
+ callId?: unknown;
11
+ }): string[];
12
+ export declare function gatherIdsForMapping(message: ChatMessage | null, candidates: string[]): string[];
13
+ export declare function mapToolIdsToMessage(toolIndex: Record<string, string>, messageId: string, ids: string[]): Record<string, string>;
14
+ export declare function mergeToolMetadata(existing: ChatMessage | undefined, toolCallIds: string[], toolName: string, toolArgs?: Record<string, unknown>): Record<string, unknown>;
15
+ export declare function appendToolTimelineEntry(messages: ChatMessage[], messageId: string, entry: Omit<ToolStreamEntry, "id" | "timestamp" | "idx">): ChatMessage[];
16
+ export declare function pushToolToStack(stack: string[], toolId: string): string[];
17
+ export declare function removeToolFromStack(stack: string[], toolId: string): string[];
18
+ export declare function syncToolTimelines(messages: ChatMessage[], existing: Record<string, ToolStreamEntry[]>): {
19
+ messages: ChatMessage[];
20
+ toolTimelines: Record<string, ToolStreamEntry[]>;
21
+ };
22
+ export declare function getFullTextFromParts(parts: MessagePart[]): string;
23
+ export declare function mergePartsWithMeta(baseParts: MessagePart[], deltaParts: MessagePart[], metaIn: {
24
+ lastSeq?: number | null;
25
+ lastText?: string;
26
+ }, seq: number | null): {
27
+ newParts: MessagePart[];
28
+ newMeta: {
29
+ lastSeq?: number | null;
30
+ lastText?: string;
31
+ };
32
+ changed: boolean;
33
+ };
34
+ export declare function computeSuffix(existing: string, incoming: string): string;
35
+ export declare function stampLastAssistantCheckpoint(messages: ChatMessage[], checkpointId: string, checkpointNs?: string | null): ChatMessage[];
36
+ export declare function nameFromModel(model?: string): string | null;
37
+ export declare function getToolProgress(message: ChatMessage): ToolStreamEntry[];
package/dist/index.esm.js CHANGED
@@ -3131,7 +3131,195 @@ function Xx(e, t, n) {
3131
3131
  function Jx(e, t, n) {
3132
3132
  return Nl.randomUUID && !e ? Nl.randomUUID() : Xx(e);
3133
3133
  }
3134
- const Zx = {
3134
+ function ri(e) {
3135
+ return e.filter((t) => {
3136
+ var s;
3137
+ if (t.role !== "assistant") return !0;
3138
+ const n = (t.content || []).some(
3139
+ (i) => {
3140
+ var a;
3141
+ return i && i.type === "text" && ((a = i.text) == null ? void 0 : a.trim());
3142
+ }
3143
+ ), r = (t.content || []).some((i) => i && i.type === "image_url"), o = !!((s = t.artifact) != null && s.plots && t.artifact.plots.length > 0);
3144
+ return !!(n || r || o);
3145
+ });
3146
+ }
3147
+ function Do(e, t, n) {
3148
+ return t < 0 || t >= e.length ? e : [...e.slice(0, t), n, ...e.slice(t + 1)];
3149
+ }
3150
+ function Iu(e) {
3151
+ let t = !1;
3152
+ const n = e.map((r) => {
3153
+ const o = (r.content ?? []).filter((s) => s.type !== "interrupt");
3154
+ return o.length !== r.content.length ? (t = !0, { ...r, content: o }) : r;
3155
+ });
3156
+ return t ? n : e;
3157
+ }
3158
+ function _l(e) {
3159
+ for (let t = e.length - 1; t >= 0; t--)
3160
+ if (e[t].role === "assistant") return e[t].id;
3161
+ return null;
3162
+ }
3163
+ function jl(e) {
3164
+ const t = /* @__PURE__ */ new Set(), n = (r) => {
3165
+ typeof r == "string" && r.trim() && t.add(r.trim());
3166
+ };
3167
+ return n(e.id), n(e == null ? void 0 : e.call_id), n(e == null ? void 0 : e.callId), Array.from(t);
3168
+ }
3169
+ function Zx(e, t) {
3170
+ const n = /* @__PURE__ */ new Set();
3171
+ for (const o of t)
3172
+ typeof o == "string" && o.trim() && n.add(o.trim());
3173
+ const r = sy(e);
3174
+ for (const o of r)
3175
+ n.add(o);
3176
+ return Array.from(n);
3177
+ }
3178
+ function Pl(e, t, n) {
3179
+ if (!n.length) return e;
3180
+ let r = !1;
3181
+ const o = { ...e };
3182
+ for (const s of n)
3183
+ s && o[s] !== t && (o[s] = t, r = !0);
3184
+ return r ? o : e;
3185
+ }
3186
+ function Qx(e, t, n, r) {
3187
+ const o = /* @__PURE__ */ new Set(), s = (l) => {
3188
+ typeof l == "string" && l.trim() && o.add(l.trim());
3189
+ };
3190
+ for (const l of t)
3191
+ s(l);
3192
+ const i = Array.from(o), a = {};
3193
+ if (i.length) {
3194
+ a.toolCallIds = i;
3195
+ const l = typeof a.toolCallId == "string" ? a.toolCallId.trim() : null;
3196
+ a.toolCallId = l && o.has(l) ? l : i[0];
3197
+ }
3198
+ return n && (a.toolName = n), r !== void 0 && (a.toolArgs = r), a;
3199
+ }
3200
+ function bo(e, t, n) {
3201
+ const r = e.findIndex((c) => c.id === t);
3202
+ if (r === -1) return e;
3203
+ const o = e[r], s = [...e], i = { ...o.additionalKwargs ?? {} }, a = Array.isArray(i.toolProgress) ? [...i.toolProgress] : [], l = {
3204
+ id: Jx(),
3205
+ idx: a.length + 1,
3206
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
3207
+ ...n
3208
+ };
3209
+ return a.push(l), Wx(o.id, a, {
3210
+ checkpointId: o.checkpointId ?? null,
3211
+ checkpointNs: o.checkpointNs ?? null
3212
+ }), i.toolProgress = a, s[r] = { ...o, additionalKwargs: i }, s;
3213
+ }
3214
+ function Ml(e, t) {
3215
+ return e.includes(t) ? e : [...e, t];
3216
+ }
3217
+ function ey(e, t) {
3218
+ const n = [...e], r = n.lastIndexOf(t);
3219
+ return r !== -1 && n.splice(r, 1), n;
3220
+ }
3221
+ function gr(e, t) {
3222
+ let n = !1;
3223
+ const r = {}, o = e.map((s) => {
3224
+ const i = iy(s);
3225
+ if (i.length)
3226
+ return r[s.id] = i, s;
3227
+ const a = t[s.id] ?? Vx(s.id);
3228
+ if (a != null && a.length) {
3229
+ const l = [...a];
3230
+ r[s.id] = l, n = !0;
3231
+ const c = { ...s.additionalKwargs ?? {}, toolProgress: l };
3232
+ return { ...s, additionalKwargs: c };
3233
+ }
3234
+ return s;
3235
+ });
3236
+ return {
3237
+ messages: n ? o : e,
3238
+ toolTimelines: r
3239
+ };
3240
+ }
3241
+ function Eu(e) {
3242
+ return !e || !e.length ? "" : e.filter((t) => t.type === "text").map((t) => t.text ?? "").join("");
3243
+ }
3244
+ function ty(e, t, n, r) {
3245
+ const o = { ...n };
3246
+ if (r !== null && typeof r == "number") {
3247
+ if (typeof o.lastSeq == "number" && r <= o.lastSeq)
3248
+ return { newParts: e, newMeta: o, changed: !1 };
3249
+ o.lastSeq = r;
3250
+ }
3251
+ const s = t.filter((f) => f.type === "text").map((f) => f.text ?? "").join(""), i = t.filter((f) => f.type !== "text"), a = o.lastText ?? Eu(e);
3252
+ let l = "", c = e.slice();
3253
+ if (s) {
3254
+ const f = ny(a, s);
3255
+ if (f === "" ? l = "" : f === s ? l = s : l = f, l) {
3256
+ const p = c[c.length - 1];
3257
+ if ((p == null ? void 0 : p.type) === "text") {
3258
+ const h = p.text ?? "", g = { ...p, text: h + l };
3259
+ c = [...c.slice(0, c.length - 1), g];
3260
+ } else
3261
+ c = [...c, { type: "text", text: l }];
3262
+ o.lastText = a + l;
3263
+ } else
3264
+ o.lastText = a;
3265
+ }
3266
+ i.length && (c = [...c, ...i]);
3267
+ const d = !!(l || i.length);
3268
+ return { newParts: c, newMeta: o, changed: d };
3269
+ }
3270
+ function Ol(e) {
3271
+ return e.replace(/\s+/g, " ").trim();
3272
+ }
3273
+ function ny(e, t) {
3274
+ if (!t) return "";
3275
+ if (!e) return t;
3276
+ if (e === t) return "";
3277
+ const n = Math.min(e.length, t.length);
3278
+ for (let i = n; i > 0; i--)
3279
+ if (e.slice(-i) === t.slice(0, i))
3280
+ return t.slice(i);
3281
+ const r = Ol(e), o = Ol(t);
3282
+ if (r === o) return "";
3283
+ const s = Math.min(r.length, o.length);
3284
+ for (let i = s; i > 0; i--)
3285
+ if (r.slice(-i) === o.slice(0, i))
3286
+ return t;
3287
+ return t;
3288
+ }
3289
+ function ry(e, t, n) {
3290
+ for (let r = e.length - 1; r >= 0; r--)
3291
+ if (e[r].role === "assistant") {
3292
+ const s = { ...e[r], checkpointId: t, checkpointNs: n ?? void 0 };
3293
+ return Do(e, r, s);
3294
+ }
3295
+ return e;
3296
+ }
3297
+ function oy(e) {
3298
+ if (!e) return null;
3299
+ const t = e.split("/");
3300
+ return t[t.length - 1] || null;
3301
+ }
3302
+ function sy(e) {
3303
+ if (!e) return [];
3304
+ const t = /* @__PURE__ */ new Set(), n = (o) => {
3305
+ typeof o == "string" && o.trim() && t.add(o.trim());
3306
+ }, r = e.additionalKwargs;
3307
+ if (r) {
3308
+ n(r.toolCallId);
3309
+ const o = r.toolCallIds;
3310
+ if (Array.isArray(o))
3311
+ for (const s of o)
3312
+ n(s);
3313
+ }
3314
+ for (const o of e.content ?? [])
3315
+ (o == null ? void 0 : o.type) === "tool_call" && n(o.id);
3316
+ return Array.from(t);
3317
+ }
3318
+ function iy(e) {
3319
+ const t = e.additionalKwargs ?? {};
3320
+ return Array.isArray(t.toolProgress) ? [...t.toolProgress] : [];
3321
+ }
3322
+ const ay = {
3135
3323
  messages: [],
3136
3324
  assemblingId: null,
3137
3325
  lastCheckpointId: null,
@@ -3143,7 +3331,7 @@ const Zx = {
3143
3331
  pendingInterrupt: null,
3144
3332
  toolTimelines: {}
3145
3333
  };
3146
- function Qx(e, t) {
3334
+ function ly(e, t) {
3147
3335
  var n;
3148
3336
  switch (t.type) {
3149
3337
  case "reset":
@@ -3188,15 +3376,11 @@ function Qx(e, t) {
3188
3376
  );
3189
3377
  return { ...e, messages: a, toolTimelines: l };
3190
3378
  }
3191
- case "user_message": {
3192
- const r = t.payload.message;
3193
- return e.messages.some((o) => o.id === r.id) ? e : { ...e, messages: [...e.messages, r] };
3194
- }
3195
3379
  case "event":
3196
- return _l(e, t.payload.ev);
3380
+ return Dl(e, t.payload.ev);
3197
3381
  case "batch": {
3198
3382
  const { events: r } = t.payload;
3199
- return r.length ? r.reduce((o, s) => _l(o, s), e) : e;
3383
+ return r.length ? r.reduce((o, s) => Dl(o, s), e) : e;
3200
3384
  }
3201
3385
  case "interrupt_response":
3202
3386
  return { ...e, messages: Iu(e.messages), pendingInterrupt: null };
@@ -3204,8 +3388,8 @@ function Qx(e, t) {
3204
3388
  return e;
3205
3389
  }
3206
3390
  }
3207
- function ey(e = []) {
3208
- const [t, n] = sm(Qx, { ...Zx, messages: e }), r = Oe([]), o = Oe(null), s = Oe(null), i = me(() => {
3391
+ function cy(e = []) {
3392
+ const [t, n] = sm(ly, { ...ay, messages: e }), r = Oe([]), o = Oe(null), s = Oe(null), i = me(() => {
3209
3393
  o.current !== null && typeof window < "u" && window.cancelAnimationFrame(o.current), s.current !== null && clearTimeout(s.current), o.current = null, s.current = null;
3210
3394
  const d = r.current;
3211
3395
  d.length && (r.current = [], n({ type: "batch", payload: { events: d } }));
@@ -3226,10 +3410,10 @@ function ey(e = []) {
3226
3410
  }), [l]);
3227
3411
  return { state: t, dispatch: n, ...c };
3228
3412
  }
3229
- function _l(e, t) {
3413
+ function Dl(e, t) {
3230
3414
  var n, r, o, s;
3231
3415
  if (t.type === "message.start" && t.role === "assistant") {
3232
- const i = t.id, a = cy(t.model);
3416
+ const i = t.id, a = oy(t.model);
3233
3417
  if (e.messages.some((d) => d.id === i)) {
3234
3418
  const d = e.messagesStreamMeta ?? {}, f = {
3235
3419
  ...d,
@@ -3248,20 +3432,20 @@ function _l(e, t) {
3248
3432
  return { ...e, assemblingId: i, messages: [...e.messages, c] };
3249
3433
  }
3250
3434
  if (t.type === "message.delta") {
3251
- const i = e.assemblingId ?? jl(e.messages);
3435
+ const i = e.assemblingId ?? _l(e.messages);
3252
3436
  if (!i) return e;
3253
3437
  const a = e.messages, l = a.findIndex((b) => b.id === i && b.role === "assistant");
3254
3438
  if (l === -1) return e;
3255
3439
  const c = a[l], d = e.messagesStreamMeta ?? {}, f = d[i] ?? { lastSeq: null, lastText: Eu(c.content) }, p = t.seq ?? null;
3256
3440
  if (p !== null && typeof p == "number" && typeof f.lastSeq == "number" && p <= f.lastSeq)
3257
3441
  return console.debug("[MessageReducer] skipping delta - seq <= lastSeq", { seq: p, lastSeq: f.lastSeq }), e;
3258
- const { newParts: h, newMeta: g, changed: m } = iy(c.content, t.delta, f, p);
3442
+ const { newParts: h, newMeta: g, changed: m } = ty(c.content, t.delta, f, p);
3259
3443
  if (!m) return e;
3260
3444
  const y = { ...c, content: h }, x = Do(a, l, y), w = { ...d, [i]: g };
3261
3445
  return { ...e, messages: x, messagesStreamMeta: w };
3262
3446
  }
3263
3447
  if (t.type === "message.end") {
3264
- const i = e.assemblingId ?? jl(e.messages);
3448
+ const i = e.assemblingId ?? _l(e.messages);
3265
3449
  if (!i) return { ...e, assemblingId: null };
3266
3450
  const a = e.messages, l = a.findIndex((c) => c.id === i && c.role === "assistant");
3267
3451
  if (l >= 0) {
@@ -3282,7 +3466,7 @@ function _l(e, t) {
3282
3466
  return e;
3283
3467
  }
3284
3468
  if (t.type === "tool.start") {
3285
- const i = Pl(t), a = i[0] ?? (typeof t.id == "string" ? t.id : null) ?? (typeof t.call_id == "string" ? t.call_id : null), l = t.name ?? t.tool ?? "tool";
3469
+ const i = jl(t), a = i[0] ?? (typeof t.id == "string" ? t.id : null) ?? (typeof t.call_id == "string" ? t.call_id : null), l = t.name ?? t.tool ?? "tool";
3286
3470
  !a && i.length === 0 && i.push(`${l}-${Date.now()}`);
3287
3471
  const c = i.length ? i : a ? [a] : [], d = c[0] ?? `${l}-${Date.now()}`, f = { type: "tool_call", id: d, name: l, args: t.args }, p = {
3288
3472
  type: e.toolStreamingMessageId ? "tool.call" : "tool.start",
@@ -3300,16 +3484,16 @@ function _l(e, t) {
3300
3484
  createdAt: (/* @__PURE__ */ new Date()).toISOString(),
3301
3485
  content: [f],
3302
3486
  additionalKwargs: {
3303
- ...ny(void 0, c.length ? c : [d], l, t.args),
3487
+ ...Qx(void 0, c.length ? c : [d], l, t.args),
3304
3488
  toolStreaming: !0
3305
3489
  }
3306
- }, b = ry(w, c.length ? c : [d]), T = [...e.messages, w], S = bo(T, w.id, p), { messages: C, toolTimelines: k } = gr(S, e.toolTimelines);
3490
+ }, b = Zx(w, c.length ? c : [d]), T = [...e.messages, w], S = bo(T, w.id, p), { messages: C, toolTimelines: k } = gr(S, e.toolTimelines);
3307
3491
  return {
3308
3492
  ...e,
3309
3493
  messages: C,
3310
- toolIndex: Ml(e.toolIndex, w.id, b),
3494
+ toolIndex: Pl(e.toolIndex, w.id, b),
3311
3495
  toolStreamingMessageId: w.id,
3312
- toolStreamingStack: Ol(e.toolStreamingStack ?? [], a ?? d),
3496
+ toolStreamingStack: Ml(e.toolStreamingStack ?? [], a ?? d),
3313
3497
  toolTimelines: k
3314
3498
  };
3315
3499
  }
@@ -3317,19 +3501,19 @@ function _l(e, t) {
3317
3501
  return {
3318
3502
  ...e,
3319
3503
  messages: m,
3320
- toolIndex: Ml(e.toolIndex, h, c),
3321
- toolStreamingStack: Ol(e.toolStreamingStack ?? [], a ?? d),
3504
+ toolIndex: Pl(e.toolIndex, h, c),
3505
+ toolStreamingStack: Ml(e.toolStreamingStack ?? [], a ?? d),
3322
3506
  toolTimelines: y
3323
3507
  };
3324
3508
  }
3325
3509
  if (t.type === "tool.end") {
3326
- const i = Pl(t);
3510
+ const i = jl(t);
3327
3511
  if (!i.length && !(typeof t.id == "string" && t.id))
3328
3512
  return e;
3329
3513
  const a = i[0] ?? (typeof t.id == "string" ? t.id : null);
3330
3514
  if (!a)
3331
3515
  return e;
3332
- const l = e.toolStreamingStack ?? [], c = l[0] ?? null, d = oy(l, a), f = d[0] ? e.toolIndex[d[0]] : null, p = e.toolStreamingMessageId, h = c === a, g = t.name ?? ((s = e.messages.find((S) => S.id === p)) == null ? void 0 : s.name) ?? "tool", m = typeof t.output == "string" ? t.output.trim() : JSON.stringify(t.output ?? ""), y = {
3516
+ const l = e.toolStreamingStack ?? [], c = l[0] ?? null, d = ey(l, a), f = d[0] ? e.toolIndex[d[0]] : null, p = e.toolStreamingMessageId, h = c === a, g = t.name ?? ((s = e.messages.find((S) => S.id === p)) == null ? void 0 : s.name) ?? "tool", m = typeof t.output == "string" ? t.output.trim() : JSON.stringify(t.output ?? ""), y = {
3333
3517
  type: "tool.end",
3334
3518
  toolId: a,
3335
3519
  toolName: g,
@@ -3387,7 +3571,7 @@ function _l(e, t) {
3387
3571
  return { ...e, messages: g, toolTimelines: m };
3388
3572
  }
3389
3573
  if (t.type === "checkpoint") {
3390
- const i = t.checkpointNs ?? e.lastCheckpointNs ?? null, a = ly(e.messages, t.checkpointId, i);
3574
+ const i = t.checkpointNs ?? e.lastCheckpointNs ?? null, a = ry(e.messages, t.checkpointId, i);
3391
3575
  return { ...e, lastCheckpointId: t.checkpointId, lastCheckpointNs: i, messages: a };
3392
3576
  }
3393
3577
  if (t.type === "error") {
@@ -3411,194 +3595,6 @@ function _l(e, t) {
3411
3595
  }
3412
3596
  return t.type === "thread_info", e;
3413
3597
  }
3414
- function ri(e) {
3415
- return e.filter((t) => {
3416
- var s;
3417
- if (t.role !== "assistant") return !0;
3418
- const n = (t.content || []).some(
3419
- (i) => {
3420
- var a;
3421
- return i && i.type === "text" && ((a = i.text) == null ? void 0 : a.trim());
3422
- }
3423
- ), r = (t.content || []).some((i) => i && i.type === "image_url"), o = !!((s = t.artifact) != null && s.plots && t.artifact.plots.length > 0);
3424
- return !!(n || r || o);
3425
- });
3426
- }
3427
- function Do(e, t, n) {
3428
- return t < 0 || t >= e.length ? e : [...e.slice(0, t), n, ...e.slice(t + 1)];
3429
- }
3430
- function Iu(e) {
3431
- let t = !1;
3432
- const n = e.map((r) => {
3433
- const o = (r.content ?? []).filter((s) => s.type !== "interrupt");
3434
- return o.length !== r.content.length ? (t = !0, { ...r, content: o }) : r;
3435
- });
3436
- return t ? n : e;
3437
- }
3438
- function jl(e) {
3439
- for (let t = e.length - 1; t >= 0; t--)
3440
- if (e[t].role === "assistant") return e[t].id;
3441
- return null;
3442
- }
3443
- function Pl(e) {
3444
- const t = /* @__PURE__ */ new Set(), n = (r) => {
3445
- typeof r == "string" && r.trim() && t.add(r.trim());
3446
- };
3447
- return n(e.id), n(e == null ? void 0 : e.call_id), n(e == null ? void 0 : e.callId), Array.from(t);
3448
- }
3449
- function ty(e) {
3450
- if (!e) return [];
3451
- const t = /* @__PURE__ */ new Set(), n = (o) => {
3452
- typeof o == "string" && o.trim() && t.add(o.trim());
3453
- }, r = e.additionalKwargs;
3454
- if (r) {
3455
- n(r.toolCallId);
3456
- const o = r.toolCallIds;
3457
- if (Array.isArray(o))
3458
- for (const s of o)
3459
- n(s);
3460
- }
3461
- for (const o of e.content ?? [])
3462
- (o == null ? void 0 : o.type) === "tool_call" && n(o.id);
3463
- return Array.from(t);
3464
- }
3465
- function ny(e, t, n, r) {
3466
- const o = /* @__PURE__ */ new Set(), s = (l) => {
3467
- typeof l == "string" && l.trim() && o.add(l.trim());
3468
- };
3469
- for (const l of t)
3470
- s(l);
3471
- const i = Array.from(o), a = {};
3472
- if (i.length) {
3473
- a.toolCallIds = i;
3474
- const l = typeof a.toolCallId == "string" ? a.toolCallId.trim() : null;
3475
- a.toolCallId = l && o.has(l) ? l : i[0];
3476
- }
3477
- return n && (a.toolName = n), r !== void 0 && (a.toolArgs = r), a;
3478
- }
3479
- function ry(e, t) {
3480
- const n = /* @__PURE__ */ new Set();
3481
- for (const o of t)
3482
- typeof o == "string" && o.trim() && n.add(o.trim());
3483
- const r = ty(e);
3484
- for (const o of r)
3485
- n.add(o);
3486
- return Array.from(n);
3487
- }
3488
- function Ml(e, t, n) {
3489
- if (!n.length) return e;
3490
- let r = !1;
3491
- const o = { ...e };
3492
- for (const s of n)
3493
- s && o[s] !== t && (o[s] = t, r = !0);
3494
- return r ? o : e;
3495
- }
3496
- function bo(e, t, n) {
3497
- const r = e.findIndex((c) => c.id === t);
3498
- if (r === -1) return e;
3499
- const o = e[r], s = [...e], i = { ...o.additionalKwargs ?? {} }, a = Array.isArray(i.toolProgress) ? [...i.toolProgress] : [], l = {
3500
- id: Jx(),
3501
- idx: a.length + 1,
3502
- timestamp: (/* @__PURE__ */ new Date()).toISOString(),
3503
- ...n
3504
- };
3505
- return a.push(l), Wx(o.id, a, {
3506
- checkpointId: o.checkpointId ?? null,
3507
- checkpointNs: o.checkpointNs ?? null
3508
- }), i.toolProgress = a, s[r] = { ...o, additionalKwargs: i }, s;
3509
- }
3510
- function Ol(e, t) {
3511
- return e.includes(t) ? e : [...e, t];
3512
- }
3513
- function oy(e, t) {
3514
- const n = [...e], r = n.lastIndexOf(t);
3515
- return r !== -1 && n.splice(r, 1), n;
3516
- }
3517
- function sy(e) {
3518
- const t = e.additionalKwargs ?? {};
3519
- return Array.isArray(t.toolProgress) ? [...t.toolProgress] : [];
3520
- }
3521
- function gr(e, t) {
3522
- let n = !1;
3523
- const r = {}, o = e.map((s) => {
3524
- const i = sy(s);
3525
- if (i.length)
3526
- return r[s.id] = i, s;
3527
- const a = t[s.id] ?? Vx(s.id);
3528
- if (a != null && a.length) {
3529
- const l = [...a];
3530
- r[s.id] = l, n = !0;
3531
- const c = { ...s.additionalKwargs ?? {}, toolProgress: l };
3532
- return { ...s, additionalKwargs: c };
3533
- }
3534
- return s;
3535
- });
3536
- return {
3537
- messages: n ? o : e,
3538
- toolTimelines: r
3539
- };
3540
- }
3541
- function Eu(e) {
3542
- return !e || !e.length ? "" : e.filter((t) => t.type === "text").map((t) => t.text ?? "").join("");
3543
- }
3544
- function iy(e, t, n, r) {
3545
- const o = { ...n };
3546
- if (r !== null && typeof r == "number") {
3547
- if (typeof o.lastSeq == "number" && r <= o.lastSeq)
3548
- return { newParts: e, newMeta: o, changed: !1 };
3549
- o.lastSeq = r;
3550
- }
3551
- const s = t.filter((f) => f.type === "text").map((f) => f.text ?? "").join(""), i = t.filter((f) => f.type !== "text"), a = o.lastText ?? Eu(e);
3552
- let l = "", c = e.slice();
3553
- if (s) {
3554
- const f = ay(a, s);
3555
- if (f === "" ? l = "" : f === s ? l = s : l = f, l) {
3556
- const p = c[c.length - 1];
3557
- if ((p == null ? void 0 : p.type) === "text") {
3558
- const h = p.text ?? "", g = { ...p, text: h + l };
3559
- c = [...c.slice(0, c.length - 1), g];
3560
- } else
3561
- c = [...c, { type: "text", text: l }];
3562
- o.lastText = a + l;
3563
- } else
3564
- o.lastText = a;
3565
- }
3566
- i.length && (c = [...c, ...i]);
3567
- const d = !!(l || i.length);
3568
- return { newParts: c, newMeta: o, changed: d };
3569
- }
3570
- function Dl(e) {
3571
- return e.replace(/\s+/g, " ").trim();
3572
- }
3573
- function ay(e, t) {
3574
- if (!t) return "";
3575
- if (!e) return t;
3576
- if (e === t) return "";
3577
- const n = Math.min(e.length, t.length);
3578
- for (let i = n; i > 0; i--)
3579
- if (e.slice(-i) === t.slice(0, i))
3580
- return t.slice(i);
3581
- const r = Dl(e), o = Dl(t);
3582
- if (r === o) return "";
3583
- const s = Math.min(r.length, o.length);
3584
- for (let i = s; i > 0; i--)
3585
- if (r.slice(-i) === o.slice(0, i))
3586
- return t;
3587
- return t;
3588
- }
3589
- function ly(e, t, n) {
3590
- for (let r = e.length - 1; r >= 0; r--)
3591
- if (e[r].role === "assistant") {
3592
- const s = { ...e[r], checkpointId: t, checkpointNs: n ?? void 0 };
3593
- return Do(e, r, s);
3594
- }
3595
- return e;
3596
- }
3597
- function cy(e) {
3598
- if (!e) return null;
3599
- const t = e.split("/");
3600
- return t[t.length - 1] || null;
3601
- }
3602
3598
  function uy(e = {}) {
3603
3599
  const t = Pe(() => An(e.baseUrl), [e.baseUrl]), n = e.streamPath ?? "/runs/stream", [r, o] = ve(e.token ?? null), s = Oe(e.headers ?? {}), i = e.credentials ?? "include", [a, l] = ve(!1), [c, d] = ve(null), [f, p] = ve(""), [h, g] = ve(null), [m, y] = ve(0), [x, w] = ve(!1), b = Oe(null), T = Oe(!0), S = Oe(0), C = Oe(null), k = Oe(null), R = Oe(null), A = Oe(null), O = me((j) => {
3604
3600
  o(j);
@@ -4102,7 +4098,7 @@ function by({
4102
4098
  onThreadChange: a,
4103
4099
  onConnectionError: l
4104
4100
  }) {
4105
- const { api: c, baseUrl: d } = vu(), f = Pe(() => An(d), [d]), [p, h] = ve([]), [g, m] = ve(t), [y, x] = ve(!1), [w, b] = ve(null), [T, S] = ve(!1), [C, k] = ve(null), { state: R, seed: A, prepend: O, pushUser: F, onEvent: q } = ey(), [W, j] = ve(!1), [_, E] = ve(!1), [$, D] = ve(null), [P, B] = ve([]), [Y, ne] = ve([]), [ye, Ce] = ve(!1), K = Pe(() => Hx(P), [P]), z = Oe(g), J = Oe(null), L = Oe(null), G = Oe(null), Z = Oe(null), ce = Oe(!1), oe = Oe(null), Ae = Oe(/* @__PURE__ */ new Map()), He = Oe(/* @__PURE__ */ new Map());
4101
+ const { api: c, baseUrl: d } = vu(), f = Pe(() => An(d), [d]), [p, h] = ve([]), [g, m] = ve(t), [y, x] = ve(!1), [w, b] = ve(null), [T, S] = ve(!1), [C, k] = ve(null), { state: R, seed: A, prepend: O, pushUser: F, onEvent: q } = cy(), [W, j] = ve(!1), [_, E] = ve(!1), [$, D] = ve(null), [P, B] = ve([]), [Y, ne] = ve([]), [ye, Ce] = ve(!1), K = Pe(() => Hx(P), [P]), z = Oe(g), J = Oe(null), L = Oe(null), G = Oe(null), Z = Oe(null), ce = Oe(!1), oe = Oe(null), Ae = Oe(/* @__PURE__ */ new Map()), He = Oe(/* @__PURE__ */ new Map());
4106
4102
  wt(() => {
4107
4103
  z.current = g;
4108
4104
  }, [g]);
@@ -18305,7 +18301,7 @@ export {
18305
18301
  ji as setChatToken,
18306
18302
  vu as useApi,
18307
18303
  uy as useChatStream,
18308
- ey as useMessagesReducer,
18304
+ cy as useMessagesReducer,
18309
18305
  Sy as useStream,
18310
18306
  ua as useThreadState,
18311
18307
  ca as useThreads