@gentiq/core 1.0.0-alpha.9 → 1.0.0-beta.2

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,19 +1,19 @@
1
- import { generateId as z, readUIMessageStream as st } from "ai";
2
- import { nanoid as at } from "nanoid";
3
- import { isGentiqError as ot, GentiqErrorCode as U, GentiqError as it } from "../errors.es.js";
4
- import { isAwaitingOutput as dt, isAwaitingApproval as ut, resolveMessageId as N, messageFeedback as B, isAwaitingUser as lt, attachmentFilePart as J } from "../protocol/messages.es.js";
1
+ import { generateId as z, readUIMessageStream as at } from "ai";
2
+ import { nanoid as it } from "nanoid";
3
+ import { isGentiqError as $, GentiqErrorCode as C, GentiqError as ot } from "../errors.es.js";
4
+ import { isAwaitingOutput as ut, isAwaitingApproval as dt, resolveMessageId as U, messageFeedback as B, isAwaitingUser as lt, attachmentFilePart as J } from "../protocol/messages.es.js";
5
5
  import { toGentiqMessages as K } from "../protocol/storedMessages.es.js";
6
6
  import { logDisconnected as ct, logChatError as ft } from "./chatDiagnostics.es.js";
7
7
  import { createChatTransport as Q } from "./chatTransport.es.js";
8
8
  import { shareUnchanged as pt } from "./shareUnchanged.es.js";
9
9
  import { isTurnActive as yt, newThreadState as mt, storedThreadState as gt, threadReducer as ht } from "./threadReducer.es.js";
10
10
  const V = 50, W = [250, 500, 1e3, 2e3, 4e3, 8e3];
11
- function Nt({
11
+ function Ut({
12
12
  http: n,
13
- attachments: u,
13
+ attachments: o,
14
14
  threadId: b,
15
15
  throttleMs: f = 0,
16
- onStart: C,
16
+ onStart: O,
17
17
  onTurnEnd: x,
18
18
  getRunOptions: T
19
19
  }) {
@@ -22,7 +22,7 @@ function Nt({
22
22
  let A;
23
23
  const k = /* @__PURE__ */ new Set();
24
24
  let E, G = 0;
25
- const O = /* @__PURE__ */ new Map();
25
+ const P = /* @__PURE__ */ new Map();
26
26
  let c = !1, I, v;
27
27
  function l(t) {
28
28
  const e = ht(s, t);
@@ -33,32 +33,33 @@ function Nt({
33
33
  const { turn: t, message: e } = I;
34
34
  I = void 0, l({ type: "reply", turn: t, message: e });
35
35
  }
36
- function tt(t, e) {
36
+ function et(t, e) {
37
37
  if (f <= 0) return l({ type: "reply", turn: t, message: e });
38
38
  I = { turn: t, message: e }, v === void 0 && (S(), v = setTimeout(function r() {
39
39
  v = void 0, I && (S(), v = setTimeout(r, f));
40
40
  }, f));
41
41
  }
42
- async function L(t) {
43
- return _.push(t), u.acquire(t);
42
+ async function M(t) {
43
+ return _.push(t), o.acquire(t);
44
44
  }
45
- function M(t) {
46
- t.forEach((e) => u.release(e));
45
+ function L(t) {
46
+ t.forEach((e) => o.release(e));
47
47
  }
48
- async function et(t) {
49
- return t.file ? (_.push(t.id), J(t, u.adopt(t.id, t.file))) : J(t, await L(t.id));
48
+ async function rt(t) {
49
+ return t.file ? (_.push(t.id), J(t, o.adopt(t.id, t.file))) : J(t, await M(t.id));
50
50
  }
51
- function F(t, e, r, a) {
51
+ function F(t, e, r, a, i) {
52
52
  return Q(n, t, {
53
53
  lastRequest: a,
54
- resume: { turnId: e, after: r }
54
+ resume: { turnId: e, after: r },
55
+ abortSignal: i
55
56
  }).reconnectToStream({ chatId: t });
56
57
  }
57
- function rt(t, e, {
58
+ function nt(t, e, {
58
59
  turnId: r,
59
60
  signal: a,
60
- lastRequest: o,
61
- onTurn: i
61
+ lastRequest: i,
62
+ onTurn: u
62
63
  }) {
63
64
  let d = r, m = 0, p = e.getReader();
64
65
  return new ReadableStream({
@@ -67,16 +68,16 @@ function Nt({
67
68
  for (; ; )
68
69
  try {
69
70
  const { done: g, value: h } = await p.read();
70
- return g ? y.close() : (m += 1, h.type === "data-turn" && Et(h.data) && (d = h.data.turn_id, i(d)), y.enqueue(h));
71
+ return g ? y.close() : (m += 1, h.type === "data-turn" && Et(h.data) && (d = h.data.turn_id, u(d)), y.enqueue(h));
71
72
  } catch (g) {
72
73
  if (a.aborted || d === void 0 || !D(g) || w >= W.length) throw g;
73
74
  await It(W[w], a), w += 1;
74
75
  try {
75
- const h = await F(t, d, m, o);
76
+ const h = await F(t, d, m, i, a);
76
77
  if (!h) return y.close();
77
78
  p = h.getReader();
78
79
  } catch (h) {
79
- if (a.aborted || !D(h)) throw h;
80
+ if (a.aborted || !Rt(h)) throw h;
80
81
  }
81
82
  }
82
83
  },
@@ -86,18 +87,18 @@ function Nt({
86
87
  });
87
88
  }
88
89
  async function R(t, e, { start: r = !1, resume: a } = {}) {
89
- const o = a ? [] : [...O.values()];
90
- a || O.clear(), G += 1;
91
- const i = G, d = new AbortController(), m = { turn: i, abort: d, threadId: t, turnId: a };
90
+ const i = a ? [] : [...P.values()];
91
+ a || P.clear(), G += 1;
92
+ const u = G, d = new AbortController(), m = { turn: u, abort: d, threadId: t, turnId: a };
92
93
  E = m, l(
93
- r ? { type: "start", threadId: t, messages: e, turn: i } : { type: "send", messages: e, turn: i }
94
+ r ? { type: "start", threadId: t, messages: e, turn: u } : { type: "send", messages: e, turn: u }
94
95
  );
95
96
  const p = {};
96
97
  let y, w;
97
98
  try {
98
- const g = a ? await F(t, a, 0, p) : await Q(n, t, {
99
+ const g = a ? await F(t, a, 0, p, d.signal) : await Q(n, t, {
99
100
  lastRequest: p,
100
- toolOutputs: o,
101
+ toolOutputs: i,
101
102
  runOptions: T?.()
102
103
  }).sendMessages({
103
104
  trigger: "submit-message",
@@ -105,28 +106,28 @@ function Nt({
105
106
  messageId: void 0,
106
107
  messages: [...e],
107
108
  abortSignal: d.signal
108
- }), h = rt(t, g ?? Tt(), {
109
+ }), h = nt(t, g ?? Tt(), {
109
110
  turnId: a,
110
111
  signal: d.signal,
111
112
  lastRequest: p,
112
- onTurn: (P) => {
113
- m.turnId = P;
113
+ onTurn: (N) => {
114
+ m.turnId = N;
114
115
  }
115
- }), Y = e.at(-1), nt = st({
116
+ }), Y = e.at(-1), st = at({
116
117
  message: Y?.role === "assistant" ? wt(Y) : { id: z(), role: "assistant", parts: [] },
117
118
  stream: h,
118
119
  terminateOnError: !0
119
120
  });
120
- for await (const P of nt) {
121
- if (d.signal.aborted) break;
122
- const Z = pt(y, P);
123
- y = Z, tt(i, Z);
121
+ for await (const N of st) {
122
+ if (d.signal.aborted) continue;
123
+ const Z = pt(y, N);
124
+ y = Z, et(u, Z);
124
125
  }
125
126
  w = d.signal.aborted ? { outcome: "stopped" } : { outcome: "finished" };
126
127
  } catch (g) {
127
128
  d.signal.aborted || g instanceof Error && g.name === "AbortError" ? w = { outcome: "stopped" } : (D(g) && ct(p.trace), g instanceof Error && ft(p.trace, g), w = { outcome: "failed", error: vt(g) });
128
129
  }
129
- E?.turn === i && (E = void 0), I?.turn === i && S(), w.outcome === "failed" ? l({ type: "fail", turn: i, error: w.error }) : l({ type: w.outcome === "stopped" ? "stop" : "finish", turn: i }), c || x?.({ threadId: t, ...w, reply: y, ...Rt(y) });
130
+ E?.turn === u && (E = void 0), I?.turn === u && S(), w.outcome === "failed" ? l({ type: "fail", turn: u, error: w.error }) : l({ type: w.outcome === "stopped" ? "stop" : "finish", turn: u }), c || x?.({ threadId: t, ...w, reply: y, ..._t(y) });
130
131
  }
131
132
  async function j() {
132
133
  if (s.status !== "ready" && s.status !== "error") return;
@@ -152,15 +153,15 @@ function Nt({
152
153
  signal: e.signal
153
154
  });
154
155
  if (e.signal.aborted) return;
155
- const a = r.turn?.status === "failed" ? $(r.turn) : void 0;
156
+ const a = r.turn?.status === "failed" ? tt(r.turn) : void 0;
156
157
  l({
157
158
  type: "loaded",
158
159
  messages: K(r.items),
159
160
  cursor: r.next_cursor,
160
161
  ...a ? { error: a } : {}
161
162
  });
162
- const o = H.getState();
163
- r.turn?.status === "running" && o.status === "ready" && R(t, o.messages, { resume: r.turn.id });
163
+ const i = H.getState();
164
+ r.turn?.status === "running" && i.status === "ready" && R(t, i.messages, { resume: r.turn.id });
164
165
  } catch (r) {
165
166
  e.signal.aborted || l({ type: "load-failed", error: r });
166
167
  } finally {
@@ -186,17 +187,17 @@ function Nt({
186
187
  }
187
188
  },
188
189
  loadAttachment(t) {
189
- c || k.has(t) || (k.add(t), L(t).then((e) => {
190
+ c || k.has(t) || (k.add(t), M(t).then((e) => {
190
191
  !c && e && l({ type: "attachment", attachmentId: t, url: e }), e || k.delete(t);
191
192
  }));
192
193
  },
193
194
  async send({ text: t, attachments: e = [], references: r = [], voiceInputs: a = [] }) {
194
195
  if (!X(s) || c) return;
195
- const o = t.trim();
196
- if (!o && e.length === 0 && r.length === 0) return;
197
- const i = _.length, d = await Promise.all(e.map(et));
196
+ const i = t.trim();
197
+ if (!i && e.length === 0 && r.length === 0) return;
198
+ const u = _.length, d = await Promise.all(e.map(rt));
198
199
  if (!X(s) || c) {
199
- M(_.splice(i));
200
+ L(_.splice(u));
200
201
  return;
201
202
  }
202
203
  const m = {
@@ -208,91 +209,91 @@ function Nt({
208
209
  parts: [
209
210
  ...r.map((y) => ({ type: "data-reference", data: y })),
210
211
  ...d,
211
- { type: "text", text: o }
212
+ { type: "text", text: i }
212
213
  ],
213
214
  ...Object.keys(m).length > 0 ? { metadata: m } : {}
214
215
  };
215
216
  if (s.status === "new") {
216
- const y = at();
217
- return C?.(y, p), R(y, [p], { start: !0 });
217
+ const y = it();
218
+ return O?.(y, p), R(y, [p], { start: !0 });
218
219
  }
219
220
  return R(s.threadId, [...s.messages, p]);
220
221
  },
221
222
  async retry(t) {
222
223
  if (c || s.status !== "ready" && s.status !== "error") return;
223
224
  const { messages: e, threadId: r } = s, a = e.findIndex(
224
- (i) => i.role === "assistant" && (i.id === t || N(i) === t)
225
+ (u) => u.role === "assistant" && (u.id === t || U(u) === t)
225
226
  );
226
227
  if (a === -1) return;
227
- let o = a - 1;
228
- for (; o >= 0 && e[o].role !== "user"; ) o -= 1;
229
- if (o !== -1)
230
- return R(r, e.slice(0, o + 1));
228
+ let i = a - 1;
229
+ for (; i >= 0 && e[i].role !== "user"; ) i -= 1;
230
+ if (i !== -1)
231
+ return R(r, e.slice(0, i + 1));
231
232
  },
232
233
  async edit(t, { text: e }) {
233
234
  if (c || s.status !== "ready" && s.status !== "error") return;
234
- const { messages: r, threadId: a } = s, o = r.findIndex(
235
+ const { messages: r, threadId: a } = s, i = r.findIndex(
235
236
  (m) => m.role === "user" && m.id === t
236
237
  );
237
- if (o === -1) return;
238
- const i = bt(r[o].parts, e.trim());
239
- if (i.length === 0) return;
240
- const d = { ...r[o], parts: i };
241
- return R(a, [...r.slice(0, o), d]);
238
+ if (i === -1) return;
239
+ const u = bt(r[i].parts, e.trim());
240
+ if (u.length === 0) return;
241
+ const d = { ...r[i], parts: u };
242
+ return R(a, [...r.slice(0, i), d]);
242
243
  },
243
244
  async setFeedback(t, e) {
244
245
  const { threadId: r } = s;
245
246
  if (c || r === null) return;
246
247
  const a = s.messages.find(
247
- (d) => d.role === "assistant" && (d.id === t || N(d) === t)
248
+ (d) => d.role === "assistant" && (d.id === t || U(d) === t)
248
249
  );
249
250
  if (!a) return;
250
- const o = B(a), i = e ?? void 0;
251
- l({ type: "feedback", messageId: a.id, feedback: i });
251
+ const i = B(a), u = e ?? void 0;
252
+ l({ type: "feedback", messageId: a.id, feedback: u });
252
253
  try {
253
254
  await n.request("post", "/chat/feedback", {
254
- body: { thread_id: r, message_id: N(a), sentiment: e }
255
+ body: { thread_id: r, message_id: U(a), sentiment: e }
255
256
  });
256
257
  } catch (d) {
257
258
  const m = s.messages.find((p) => p.id === a.id);
258
- throw m && B(m) === i && l({ type: "feedback", messageId: a.id, feedback: o }), d;
259
+ throw m && B(m) === u && l({ type: "feedback", messageId: a.id, feedback: i }), d;
259
260
  }
260
261
  },
261
262
  async addToolApprovalResponse(t) {
262
- if (!(c || s.status !== "ready" && s.status !== "error" || !s.messages.at(-1)?.parts.some((r) => ut(r, t.id))))
263
+ if (!(c || s.status !== "ready" && s.status !== "error" || !s.messages.at(-1)?.parts.some((r) => dt(r, t.id))))
263
264
  return l({ type: "approval", response: t }), j();
264
265
  },
265
266
  async addToolOutput(t) {
266
- if (!(c || s.status !== "ready" && s.status !== "error" || !s.messages.at(-1)?.parts.some((r) => dt(r, t.toolCallId))))
267
- return O.set(t.toolCallId, t), l({ type: "tool-output", response: t }), j();
267
+ if (!(c || s.status !== "ready" && s.status !== "error" || !s.messages.at(-1)?.parts.some((r) => ut(r, t.toolCallId))))
268
+ return P.set(t.toolCallId, t), l({ type: "tool-output", response: t }), j();
268
269
  },
269
270
  stop() {
270
271
  if (!E || !yt(s)) return;
271
272
  const { turn: t, abort: e, threadId: r } = E;
272
273
  E = void 0, e.abort(), I?.turn === t && S(), l({ type: "stop", turn: t }), n.request("post", "/chat/threads/{thread_id}/stop", { path: { thread_id: r } }).catch((a) => {
273
- ot(a) && a.status === 404 || c || l({ type: "stop-failed", error: a });
274
+ $(a) && a.status === 404 || c || l({ type: "stop-failed", error: a });
274
275
  });
275
276
  },
276
277
  dismissError() {
277
278
  l({ type: "dismiss-error" });
278
279
  },
279
280
  dispose() {
280
- c || (c = !0, A?.abort(), E?.abort.abort(), E = void 0, clearTimeout(v), q.clear(), M(_.splice(0)));
281
+ c || (c = !0, A?.abort(), E?.abort.abort(), E = void 0, clearTimeout(v), q.clear(), L(_.splice(0)));
281
282
  }
282
283
  };
283
284
  return H;
284
285
  }
285
- function bt(n, u) {
286
+ function bt(n, o) {
286
287
  const b = n.findIndex((T) => T.type === "text"), f = n.filter((T) => T.type !== "text");
287
- if (!u) return f;
288
- const C = { type: "text", text: u }, x = b === -1 ? f.length : n.slice(0, b).filter((T) => T.type !== "text").length;
289
- return [...f.slice(0, x), C, ...f.slice(x)];
288
+ if (!o) return f;
289
+ const O = { type: "text", text: o }, x = b === -1 ? f.length : n.slice(0, b).filter((T) => T.type !== "text").length;
290
+ return [...f.slice(0, x), O, ...f.slice(x)];
290
291
  }
291
292
  function wt(n) {
292
293
  return {
293
294
  ...n,
294
295
  parts: n.parts.filter(
295
- (u) => u.type !== "data-chat-finished" && u.type !== "data-title"
296
+ (o) => o.type !== "data-chat-finished" && o.type !== "data-title"
296
297
  )
297
298
  };
298
299
  }
@@ -305,33 +306,36 @@ function Et(n) {
305
306
  function Tt() {
306
307
  return new ReadableStream({ start: (n) => n.close() });
307
308
  }
308
- function It(n, u) {
309
+ function It(n, o) {
309
310
  return new Promise((b) => {
310
311
  const f = setTimeout(b, n);
311
- u.addEventListener("abort", () => {
312
+ o.addEventListener("abort", () => {
312
313
  clearTimeout(f), b();
313
314
  });
314
315
  });
315
316
  }
316
317
  function vt(n) {
317
- return n instanceof Error && n.message === U.TURN_INTERRUPTED ? $({ error: U.TURN_INTERRUPTED }) : n;
318
+ return n instanceof Error && n.message === C.TURN_INTERRUPTED ? tt({ error: C.TURN_INTERRUPTED }) : n;
318
319
  }
319
- function $(n) {
320
- const u = n.error ?? U.INTERNAL_ERROR;
321
- return new it({ error: { code: u, message: "The turn broke off." } });
320
+ function tt(n) {
321
+ const o = n.error ?? C.INTERNAL_ERROR;
322
+ return new ot({ error: { code: o, message: "The turn broke off." } });
322
323
  }
323
324
  function D(n) {
324
325
  if (!(n instanceof TypeError)) return !1;
325
- const u = n.message.toLowerCase();
326
- return u.includes("fetch") || u.includes("network");
326
+ const o = n.message.toLowerCase();
327
+ return o.includes("fetch") || o.includes("network") || o.includes("load failed") || o.includes("stream");
327
328
  }
328
329
  function Rt(n) {
329
- let u, b = !1;
330
+ return D(n) ? !0 : $(n) ? n.code === C.TIMEOUT || n.status === 502 || n.status === 503 || n.status === 504 : !1;
331
+ }
332
+ function _t(n) {
333
+ let o, b = !1;
330
334
  for (const f of n?.parts ?? [])
331
- f.type === "data-title" && f.data.title && (u = f.data.title), f.type === "data-chat-finished" && f.data.title_pending && (b = !0);
332
- return { ...u ? { title: u } : {}, titlePending: b };
335
+ f.type === "data-title" && f.data.title && (o = f.data.title), f.type === "data-chat-finished" && f.data.title_pending && (b = !0);
336
+ return { ...o ? { title: o } : {}, titlePending: b };
333
337
  }
334
338
  export {
335
339
  V as THREAD_PAGE_SIZE,
336
- Nt as createThreadController
340
+ Ut as createThreadController
337
341
  };
@@ -1,31 +1,31 @@
1
- import { withToolOutput as b, withApprovalResponse as y, withFeedback as v } from "../protocol/messages.es.js";
2
- const o = [], c = { cursor: null, status: "idle" }, f = (r, e) => r.turn === e.turn, T = {
1
+ import { withFeedback as y, withApprovalResponse as v, withToolOutput as T } from "../protocol/messages.es.js";
2
+ const d = [], c = { cursor: null, status: "idle" }, f = (r, e) => r.turn === e.turn, A = {
3
3
  new: {
4
- start: (r, { threadId: e, messages: s, turn: n }) => ({
4
+ start: (r, { threadId: e, messages: s, turn: u }) => ({
5
5
  status: "submitted",
6
6
  threadId: e,
7
7
  messages: s,
8
- turn: n
8
+ turn: u
9
9
  })
10
10
  },
11
11
  unloaded: {
12
- load: ({ threadId: r }) => ({ status: "loading", threadId: r, messages: o })
12
+ load: ({ threadId: r }) => ({ status: "loading", threadId: r, messages: d })
13
13
  },
14
14
  loading: {
15
15
  loaded: ({ threadId: r }, { messages: e, error: s }) => s === void 0 ? { status: "ready", threadId: r, messages: e } : { status: "error", threadId: r, messages: e, error: s },
16
16
  "load-failed": ({ threadId: r }, { error: e }) => ({
17
17
  status: "load-failed",
18
18
  threadId: r,
19
- messages: o,
19
+ messages: d,
20
20
  error: e
21
21
  })
22
22
  },
23
23
  "load-failed": {
24
- load: ({ threadId: r }) => ({ status: "loading", threadId: r, messages: o })
24
+ load: ({ threadId: r }) => ({ status: "loading", threadId: r, messages: d })
25
25
  },
26
26
  ready: {
27
27
  send: ({ threadId: r }, { messages: e, turn: s }) => ({ status: "submitted", threadId: r, messages: e, turn: s }),
28
- feedback: d,
28
+ feedback: l,
29
29
  approval: g,
30
30
  "tool-output": p,
31
31
  "stop-failed": ({ threadId: r, messages: e }, { error: s }) => ({
@@ -37,27 +37,27 @@ const o = [], c = { cursor: null, status: "idle" }, f = (r, e) => r.turn === e.t
37
37
  },
38
38
  submitted: {
39
39
  reply: (r, e) => f(r, e) ? { ...r, status: "streaming", messages: h(r.messages, e.message) } : r,
40
- finish: l,
41
- stop: l,
40
+ finish: o,
41
+ stop: o,
42
42
  fail: m,
43
- feedback: d
43
+ feedback: l
44
44
  },
45
45
  streaming: {
46
46
  reply: (r, e) => f(r, e) ? { ...r, messages: h(r.messages, e.message) } : r,
47
- finish: l,
48
- stop: l,
47
+ finish: o,
48
+ stop: o,
49
49
  fail: m,
50
- feedback: d
50
+ feedback: l
51
51
  },
52
52
  error: {
53
53
  send: ({ threadId: r }, { messages: e, turn: s }) => ({ status: "submitted", threadId: r, messages: e, turn: s }),
54
54
  "dismiss-error": ({ threadId: r, messages: e }) => ({ status: "ready", threadId: r, messages: e }),
55
- feedback: d,
55
+ feedback: l,
56
56
  approval: g,
57
57
  "tool-output": p
58
58
  }
59
59
  };
60
- function l(r, e) {
60
+ function o(r, e) {
61
61
  return f(r, e) ? { status: "ready", threadId: r.threadId, messages: r.messages } : r;
62
62
  }
63
63
  function m(r, e) {
@@ -68,28 +68,28 @@ function m(r, e) {
68
68
  error: e.error
69
69
  } : r;
70
70
  }
71
- function d(r, { messageId: e, feedback: s }) {
72
- const n = r.messages.findIndex((u) => u.id === e);
73
- if (n === -1) return r;
71
+ function l(r, { messageId: e, feedback: s }) {
72
+ const u = r.messages.findIndex((n) => n.id === e);
73
+ if (u === -1) return r;
74
74
  const i = [...r.messages];
75
- return i[n] = v(i[n], s), { ...r, messages: i };
75
+ return i[u] = y(i[u], s), { ...r, messages: i };
76
76
  }
77
- function g(r, { response: e }) {
77
+ function w(r, e) {
78
78
  const s = r.messages.at(-1);
79
79
  if (!s || s.role !== "assistant") return r;
80
- const n = y(s, e);
81
- return n.parts.every((i, u) => i === s.parts[u]) ? r : { ...r, messages: [...r.messages.slice(0, -1), n] };
80
+ const u = e(s);
81
+ return u.parts.every((i, n) => i === s.parts[n]) ? r : { ...r, messages: [...r.messages.slice(0, -1), u] };
82
+ }
83
+ function g(r, { response: e }) {
84
+ return w(r, (s) => v(s, e));
82
85
  }
83
86
  function p(r, { response: e }) {
84
- const s = r.messages.at(-1);
85
- if (!s || s.role !== "assistant") return r;
86
- const n = b(s, e);
87
- return n.parts.every((i, u) => i === s.parts[u]) ? r : { ...r, messages: [...r.messages.slice(0, -1), n] };
87
+ return w(r, (s) => T(s, e));
88
88
  }
89
89
  function h(r, e) {
90
90
  return r.at(-1)?.id === e.id ? [...r.slice(0, -1), e] : [...r, e];
91
91
  }
92
- function I(r, e) {
92
+ function E(r, e) {
93
93
  switch (e.type) {
94
94
  case "earlier-load":
95
95
  return !t(r) || r.earlier.cursor === null || r.earlier.status === "loading" ? r : { ...r, earlier: { cursor: r.earlier.cursor, status: "loading" } };
@@ -104,42 +104,42 @@ function I(r, e) {
104
104
  case "attachment":
105
105
  return k(r, e);
106
106
  }
107
- const s = T[r.status][e.type];
107
+ const s = A[r.status][e.type];
108
108
  if (!s) return r;
109
- const n = s(r, e);
110
- return n === r ? r : e.type === "loaded" ? { ...n, earlier: { cursor: e.cursor, status: "idle" } } : { ...n, earlier: t(n) ? r.earlier : c };
109
+ const u = s(r, e);
110
+ return u === r ? r : e.type === "loaded" ? { ...u, earlier: { cursor: e.cursor, status: "idle" } } : { ...u, earlier: t(u) ? r.earlier : c };
111
111
  }
112
112
  function t(r) {
113
113
  return r.status === "ready" || r.status === "submitted" || r.status === "streaming" || r.status === "error";
114
114
  }
115
- function w(r, e) {
115
+ function b(r, e) {
116
116
  if (r.type !== "file") return !1;
117
117
  const s = r;
118
118
  return s.attachmentId === e && !s.url;
119
119
  }
120
120
  function k(r, { attachmentId: e, url: s }) {
121
121
  if (!t(r)) return r;
122
- let n = !1;
123
- const i = r.messages.map((u) => u.parts.some((a) => w(a, e)) ? (n = !0, {
124
- ...u,
125
- parts: u.parts.map(
126
- (a) => w(a, e) ? { ...a, url: s } : a
122
+ let u = !1;
123
+ const i = r.messages.map((n) => n.parts.some((a) => b(a, e)) ? (u = !0, {
124
+ ...n,
125
+ parts: n.parts.map(
126
+ (a) => b(a, e) ? { ...a, url: s } : a
127
127
  )
128
- }) : u);
129
- return n ? { ...r, messages: i } : r;
128
+ }) : n);
129
+ return u ? { ...r, messages: i } : r;
130
130
  }
131
- function E() {
132
- return { status: "new", threadId: null, messages: o, earlier: c };
133
- }
134
- function R(r) {
135
- return { status: "unloaded", threadId: r, messages: o, earlier: c };
131
+ function R() {
132
+ return { status: "new", threadId: null, messages: d, earlier: c };
136
133
  }
137
134
  function S(r) {
135
+ return { status: "unloaded", threadId: r, messages: d, earlier: c };
136
+ }
137
+ function x(r) {
138
138
  return r.status === "submitted" || r.status === "streaming";
139
139
  }
140
140
  export {
141
- S as isTurnActive,
142
- E as newThreadState,
143
- R as storedThreadState,
144
- I as threadReducer
141
+ x as isTurnActive,
142
+ R as newThreadState,
143
+ S as storedThreadState,
144
+ E as threadReducer
145
145
  };
package/package.json CHANGED
@@ -11,7 +11,8 @@
11
11
  "provenance": false
12
12
  },
13
13
  "description": "Framework-free core of the Gentiq SDK: the API client, the chat protocol and the thread controller.",
14
- "version": "1.0.0-alpha.9",
14
+ "version": "1.0.0-beta.2",
15
+ "license": "Apache-2.0",
15
16
  "type": "module",
16
17
  "files": [
17
18
  "dist"
@@ -61,7 +62,7 @@
61
62
  "test:watch": "vitest",
62
63
  "format": "prettier --write \"src/**/*.{ts,json}\"",
63
64
  "format:check": "prettier --check \"src/**/*.{ts,json}\"",
64
- "lint": "eslint src --max-warnings 8",
65
+ "lint": "eslint src --max-warnings 0",
65
66
  "build": "vite build",
66
67
  "build:watch": "vite build --watch"
67
68
  }