@floegence/floe-webapp-core 0.35.8 → 0.35.10

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.
@@ -1,79 +1,94 @@
1
- import { createComponent as X } from "solid-js/web";
2
- import { createMemo as b, createEffect as Y, on as Z, createSignal as C, useContext as $, createContext as ee, batch as E } from "solid-js";
3
- import { createStore as te, reconcile as v, produce as M } from "solid-js/store";
4
- import { deferNonBlocking as p } from "../../utils/defer.js";
1
+ import { createComponent as Y } from "solid-js/web";
2
+ import { createMemo as p, createEffect as Z, on as $, createSignal as b, useContext as ee, createContext as te, batch as F } from "solid-js";
3
+ import { createStore as oe, reconcile as T, produce as M } from "solid-js/store";
4
+ import { deferNonBlocking as k } from "../../utils/defer.js";
5
5
  import { DEFAULT_VIRTUAL_LIST_CONFIG as se } from "./types.js";
6
- const F = ee();
6
+ const z = te();
7
7
  function Ce() {
8
- const o = $(F);
9
- if (!o)
8
+ const n = ee(z);
9
+ if (!n)
10
10
  throw new Error("useChatContext must be used within a ChatProvider");
11
- return o;
11
+ return n;
12
12
  }
13
- const Me = (o) => {
14
- const S = b(() => ({
13
+ const xe = (n) => {
14
+ const C = p(() => ({
15
15
  placeholder: "Type a message...",
16
16
  allowAttachments: !0,
17
17
  maxAttachments: 10,
18
18
  maxAttachmentSize: 10485760,
19
19
  // 10MB
20
- ...o.config
21
- })), d = b(() => ({
20
+ ...n.config
21
+ })), u = p(() => ({
22
22
  ...se,
23
- ...o.config?.virtualList
24
- })), [c, u] = te(o.initialMessages || []), z = /* @__PURE__ */ new Map();
25
- Y(Z(() => o.initialMessages, (e) => {
26
- e && e.length > 0 && u(v(e));
23
+ ...n.config?.virtualList
24
+ })), [a, l] = oe(n.initialMessages || []), B = /* @__PURE__ */ new Map(), R = p(() => {
25
+ const e = /* @__PURE__ */ new Map();
26
+ for (let t = 0; t < a.length; t += 1) {
27
+ const o = a[t]?.id;
28
+ o && e.set(o, t);
29
+ }
30
+ return e;
31
+ }), x = (e) => {
32
+ const t = R().get(e);
33
+ if (t !== void 0) return t;
34
+ const o = a.findIndex((s) => s.id === e);
35
+ return o >= 0 ? o : void 0;
36
+ };
37
+ Z($(() => n.initialMessages, (e) => {
38
+ e && e.length > 0 && l(T(e));
27
39
  }, {
28
40
  defer: !0
29
41
  }));
30
- const [A, H] = C(!1), [P, R] = C(!0), [L, x] = C(null), [B, I] = C(0);
31
- let D = 0;
32
- const g = /* @__PURE__ */ new Set(), h = [], f = /* @__PURE__ */ new Map(), O = (e) => {
42
+ const [A, H] = b(!1), [P, D] = b(!0), [L, S] = b(null), [O, I] = b(0);
43
+ let j = 0;
44
+ const g = /* @__PURE__ */ new Set(), f = [], h = /* @__PURE__ */ new Map(), q = (e) => {
33
45
  if (!g.delete(e)) return;
34
- const t = h.indexOf(e);
35
- t >= 0 && h.splice(t, 1), I(g.size);
36
- }, j = () => {
37
- const e = ++D;
38
- return g.add(e), h.push(e), I(g.size), e;
39
- }, q = () => {
40
- for (; h.length > 0; ) {
41
- const e = h.shift();
46
+ const t = f.indexOf(e);
47
+ t >= 0 && f.splice(t, 1), I(g.size);
48
+ }, N = () => {
49
+ const e = ++j;
50
+ return g.add(e), f.push(e), I(g.size), e;
51
+ }, W = () => {
52
+ for (; f.length > 0; ) {
53
+ const e = f.shift();
42
54
  if (e === void 0) return;
43
55
  if (g.has(e)) {
44
56
  g.delete(e), I(g.size);
45
57
  return;
46
58
  }
47
59
  }
48
- }, U = b(() => B() > 0), N = b(() => U() || L() !== null), k = (e) => {
49
- u(M((t) => {
60
+ }, U = p(() => O() > 0), _ = p(() => U() || L() !== null), y = (e) => {
61
+ l(M((t) => {
50
62
  t.push(e);
51
63
  }));
52
- }, l = (e, t) => {
53
- u(M((s) => {
54
- const n = s.findIndex((a) => a.id === e);
55
- n !== -1 && (s[n] = t(s[n]));
56
- }));
57
- }, W = (e) => {
58
- u(M((t) => {
59
- const s = t.findIndex((n) => n.id === e);
60
- s !== -1 && t.splice(s, 1);
61
- })), f.delete(e);
62
- }, _ = () => {
63
- u(v([])), f.clear();
64
+ }, E = (e, t) => {
65
+ const o = x(e);
66
+ o !== void 0 && l(o, (s) => t(s));
64
67
  }, V = (e) => {
65
- u(v(e));
68
+ const t = x(e);
69
+ t !== void 0 && (l(M((o) => {
70
+ o.splice(t, 1);
71
+ })), h.delete(e));
72
+ }, G = () => {
73
+ l(T([])), h.clear();
74
+ }, Q = (e) => {
75
+ l(T(e));
76
+ }, i = (e, t) => {
77
+ const o = x(e);
78
+ o !== void 0 && l(o, M((s) => {
79
+ t(s);
80
+ }));
66
81
  };
67
- let T = [], w = null;
68
- const G = () => {
69
- const e = T;
70
- T = [], w = null, E(() => {
71
- e.forEach(Q);
82
+ let v = [], w = null;
83
+ const J = () => {
84
+ const e = v;
85
+ v = [], w = null, F(() => {
86
+ e.forEach(K);
72
87
  });
73
- }, Q = (e) => {
88
+ }, K = (e) => {
74
89
  switch (e.type) {
75
90
  case "message-start": {
76
- q();
91
+ W();
77
92
  const t = {
78
93
  id: e.messageId,
79
94
  role: "assistant",
@@ -81,105 +96,94 @@ const Me = (o) => {
81
96
  status: "streaming",
82
97
  timestamp: Date.now()
83
98
  };
84
- k(t), x(e.messageId);
99
+ y(t), S(e.messageId);
85
100
  break;
86
101
  }
87
102
  case "block-start": {
88
- l(e.messageId, (t) => ({
89
- ...t,
90
- blocks: [...t.blocks, ne(e.blockType)]
91
- }));
103
+ i(e.messageId, (t) => {
104
+ t.blocks.push(ne(e.blockType));
105
+ });
92
106
  break;
93
107
  }
94
108
  case "block-delta": {
95
- l(e.messageId, (t) => {
96
- const s = [...t.blocks], n = s[e.blockIndex];
97
- return n && "content" in n && typeof n.content == "string" && (n.content += e.delta), {
98
- ...t,
99
- blocks: s
100
- };
109
+ i(e.messageId, (t) => {
110
+ const o = t.blocks[e.blockIndex];
111
+ o && "content" in o && typeof o.content == "string" && (o.content += e.delta);
101
112
  });
102
113
  break;
103
114
  }
104
115
  case "block-set": {
105
- l(e.messageId, (t) => {
106
- const s = [...t.blocks];
107
- return e.blockIndex === s.length ? s.push(e.block) : e.blockIndex >= 0 && e.blockIndex < s.length && (s[e.blockIndex] = e.block), {
108
- ...t,
109
- blocks: s
110
- };
116
+ i(e.messageId, (t) => {
117
+ e.blockIndex === t.blocks.length ? t.blocks.push(e.block) : e.blockIndex >= 0 && e.blockIndex < t.blocks.length && (t.blocks[e.blockIndex] = e.block);
111
118
  });
112
119
  break;
113
120
  }
114
121
  case "block-end":
115
122
  break;
116
123
  case "message-end": {
117
- l(e.messageId, (t) => ({
118
- ...t,
119
- status: "complete"
120
- })), x(null);
124
+ i(e.messageId, (t) => {
125
+ t.status = "complete";
126
+ }), S(null);
121
127
  break;
122
128
  }
123
129
  case "error": {
124
- l(e.messageId, (t) => ({
125
- ...t,
126
- status: "error",
127
- error: e.error
128
- })), x(null);
130
+ i(e.messageId, (t) => {
131
+ t.status = "error", t.error = e.error;
132
+ }), S(null);
129
133
  break;
130
134
  }
131
135
  }
132
- }, J = {
133
- messages: () => c,
134
- coldMessages: z,
136
+ }, X = {
137
+ messages: () => a,
138
+ coldMessages: B,
135
139
  isLoadingHistory: A,
136
140
  hasMoreHistory: P,
137
141
  streamingMessageId: L,
138
142
  isPreparing: U,
139
- isWorking: N,
140
- config: S,
141
- virtualListConfig: d,
143
+ isWorking: _,
144
+ config: C,
145
+ virtualListConfig: u,
142
146
  sendMessage: async (e, t = []) => {
143
- const s = {
147
+ const o = {
144
148
  id: crypto.randomUUID(),
145
149
  role: "user",
146
- blocks: oe(e, t),
150
+ blocks: re(e, t),
147
151
  status: "sending",
148
152
  timestamp: Date.now()
149
153
  };
150
- E(() => {
151
- k(s), l(s.id, (i) => ({
152
- ...i,
154
+ F(() => {
155
+ y(o), E(o.id, (c) => ({
156
+ ...c,
153
157
  status: "complete"
154
158
  }));
155
159
  });
156
160
  try {
157
- o.callbacks?.onWillSend?.(e, t);
158
- } catch (i) {
159
- console.error("onWillSend error:", i);
161
+ n.callbacks?.onWillSend?.(e, t);
162
+ } catch (c) {
163
+ console.error("onWillSend error:", c);
160
164
  }
161
- const n = o.callbacks?.onSendMessage;
162
- if (!n) return;
163
- const a = j(), r = e, m = [...t];
164
- p(() => {
165
- Promise.resolve().then(() => n(r, m, k)).catch((i) => {
166
- console.error("Failed to send message:", i);
165
+ const s = n.callbacks?.onSendMessage;
166
+ if (!s) return;
167
+ const d = N(), r = e, m = [...t];
168
+ k(() => {
169
+ Promise.resolve().then(() => s(r, m, y)).catch((c) => {
170
+ console.error("Failed to send message:", c);
167
171
  }).finally(() => {
168
- O(a);
172
+ q(d);
169
173
  });
170
174
  });
171
175
  },
172
176
  loadMoreHistory: async () => {
173
177
  if (A() || !P()) return;
174
- const e = o.callbacks?.onLoadMore;
175
- e && (H(!0), p(() => {
178
+ const e = n.callbacks?.onLoadMore;
179
+ e && (H(!0), k(() => {
176
180
  Promise.resolve(e()).then((t) => {
177
181
  if (t.length === 0) {
178
- R(!1);
182
+ D(!1);
179
183
  return;
180
184
  }
181
- u(M((s) => {
182
- s.unshift(...t);
185
+ l(M((o) => {
186
+ o.unshift(...t);
183
187
  }));
184
188
  }).catch((t) => {
185
189
  console.error("Failed to load history:", t);
@@ -189,106 +193,87 @@ const Me = (o) => {
189
193
  }));
190
194
  },
191
195
  retryMessage: (e) => {
192
- const t = o.callbacks?.onRetry;
193
- t && p(() => {
196
+ const t = n.callbacks?.onRetry;
197
+ t && k(() => {
194
198
  try {
195
199
  t(e);
196
- } catch (s) {
197
- console.error("Failed to retry message:", s);
200
+ } catch (o) {
201
+ console.error("Failed to retry message:", o);
198
202
  }
199
203
  });
200
204
  },
201
- addMessage: k,
202
- updateMessage: l,
203
- deleteMessage: W,
204
- clearMessages: _,
205
- setMessages: V,
205
+ addMessage: y,
206
+ updateMessage: E,
207
+ deleteMessage: V,
208
+ clearMessages: G,
209
+ setMessages: Q,
206
210
  handleStreamEvent: (e) => {
207
- T.push(e), w || (w = requestAnimationFrame(G));
211
+ v.push(e), w || (w = requestAnimationFrame(J));
208
212
  },
209
213
  uploadAttachment: async (e) => {
210
- const t = o.callbacks?.onUploadAttachment;
214
+ const t = n.callbacks?.onUploadAttachment;
211
215
  return t ? await t(e) : URL.createObjectURL(e);
212
216
  },
213
217
  toggleToolCollapse: (e, t) => {
214
- l(e, (s) => ({
215
- ...s,
216
- blocks: s.blocks.map((n) => {
217
- if (n.type === "tool-call" && n.toolId === t) {
218
- const a = n.collapsed === void 0 ? !1 : !n.collapsed;
219
- return {
220
- ...n,
221
- collapsed: a
222
- };
218
+ i(e, (o) => {
219
+ for (const s of o.blocks)
220
+ if (s.type === "tool-call" && s.toolId === t) {
221
+ s.collapsed = s.collapsed === void 0 ? !1 : !s.collapsed;
222
+ break;
223
223
  }
224
- return n;
225
- })
226
- }));
224
+ });
227
225
  },
228
- approveToolCall: (e, t, s) => {
229
- l(e, (a) => ({
230
- ...a,
231
- blocks: a.blocks.map((r) => r.type !== "tool-call" || r.toolId !== t || r.requiresApproval !== !0 || r.approvalState !== "required" ? r : s ? {
232
- ...r,
233
- approvalState: "approved",
234
- status: "running"
235
- } : {
236
- ...r,
237
- approvalState: "rejected",
238
- status: "error",
239
- error: r.error || "Rejected by user"
240
- })
241
- }));
242
- const n = o.callbacks?.onToolApproval;
243
- n && p(() => {
244
- Promise.resolve(n(e, t, s)).catch((a) => {
245
- console.error("Failed to approve tool call:", a);
226
+ approveToolCall: (e, t, o) => {
227
+ i(e, (d) => {
228
+ for (const r of d.blocks)
229
+ if (!(r.type !== "tool-call" || r.toolId !== t) && !(r.requiresApproval !== !0 || r.approvalState !== "required")) {
230
+ o ? (r.approvalState = "approved", r.status = "running") : (r.approvalState = "rejected", r.status = "error", r.error = r.error || "Rejected by user");
231
+ break;
232
+ }
233
+ });
234
+ const s = n.callbacks?.onToolApproval;
235
+ s && k(() => {
236
+ Promise.resolve(s(e, t, o)).catch((d) => {
237
+ console.error("Failed to approve tool call:", d);
246
238
  });
247
239
  });
248
240
  },
249
- heightCache: f,
241
+ heightCache: h,
250
242
  setMessageHeight: (e, t) => {
251
- f.set(e, t);
243
+ h.set(e, t);
252
244
  },
253
- getMessageHeight: (e) => f.get(e) || d().defaultItemHeight,
254
- toggleChecklistItem: (e, t, s) => {
255
- let n = null;
256
- l(e, (r) => {
257
- const m = [...r.blocks], i = m[t];
258
- if (i && i.type === "checklist") {
259
- const K = i.items.map((y) => y.id === s ? (n = !y.checked, {
260
- ...y,
261
- checked: n
262
- }) : y);
263
- m[t] = {
264
- ...i,
265
- items: K
266
- };
245
+ getMessageHeight: (e) => h.get(e) || u().defaultItemHeight,
246
+ toggleChecklistItem: (e, t, o) => {
247
+ let s = null;
248
+ i(e, (r) => {
249
+ const m = r.blocks[t];
250
+ if (m && m.type === "checklist") {
251
+ for (const c of m.items)
252
+ if (c.id === o) {
253
+ s = !c.checked, c.checked = s;
254
+ break;
255
+ }
267
256
  }
268
- return {
269
- ...r,
270
- blocks: m
271
- };
272
257
  });
273
- const a = o.callbacks?.onChecklistChange;
274
- !a || n === null || p(() => {
258
+ const d = n.callbacks?.onChecklistChange;
259
+ !d || s === null || k(() => {
275
260
  try {
276
- a(e, t, s, n);
261
+ d(e, t, o, s);
277
262
  } catch (r) {
278
263
  console.error("Failed to handle checklist change:", r);
279
264
  }
280
265
  });
281
266
  }
282
267
  };
283
- return X(F.Provider, {
284
- value: J,
268
+ return Y(z.Provider, {
269
+ value: X,
285
270
  get children() {
286
- return o.children;
271
+ return n.children;
287
272
  }
288
273
  });
289
274
  };
290
- function ne(o) {
291
- switch (o) {
275
+ function ne(n) {
276
+ switch (n) {
292
277
  case "text":
293
278
  return {
294
279
  type: "text",
@@ -364,26 +349,26 @@ function ne(o) {
364
349
  };
365
350
  }
366
351
  }
367
- function oe(o, S) {
368
- const d = [];
369
- for (const c of S)
370
- c.type === "image" ? d.push({
352
+ function re(n, C) {
353
+ const u = [];
354
+ for (const a of C)
355
+ a.type === "image" ? u.push({
371
356
  type: "image",
372
- src: c.url || c.preview || "",
373
- alt: c.file.name
374
- }) : d.push({
357
+ src: a.url || a.preview || "",
358
+ alt: a.file.name
359
+ }) : u.push({
375
360
  type: "file",
376
- name: c.file.name,
377
- size: c.file.size,
378
- mimeType: c.file.type,
379
- url: c.url
361
+ name: a.file.name,
362
+ size: a.file.size,
363
+ mimeType: a.file.type,
364
+ url: a.url
380
365
  });
381
- return o.trim() && d.push({
366
+ return n.trim() && u.push({
382
367
  type: "text",
383
- content: o.trim()
384
- }), d;
368
+ content: n.trim()
369
+ }), u;
385
370
  }
386
371
  export {
387
- Me as ChatProvider,
372
+ xe as ChatProvider,
388
373
  Ce as useChatContext
389
374
  };