@gentiq/core 1.0.0-alpha.11 → 1.0.0-alpha.13

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.
@@ -0,0 +1,104 @@
1
+ import { weekStart as m } from "./calendars.es.js";
2
+ import { createDateFormatter as f } from "./datetime.es.js";
3
+ function T() {
4
+ try {
5
+ const n = Intl.DateTimeFormat().resolvedOptions().timeZone;
6
+ return n && n.trim() ? n : "UTC";
7
+ } catch {
8
+ return "UTC";
9
+ }
10
+ }
11
+ function d(n) {
12
+ const t = /* @__PURE__ */ new Set();
13
+ for (const r of n) {
14
+ const e = Math.floor(r);
15
+ e === 7 ? t.add(0) : e >= 0 && e <= 6 && t.add(e);
16
+ }
17
+ return Array.from(t).sort((r, e) => r - e);
18
+ }
19
+ function D(n) {
20
+ const t = Math.max(0, Math.min(59, Math.floor(n.minute))), r = Math.max(0, Math.min(23, Math.floor(n.hour)));
21
+ if (n.frequency === "daily") return `${t} ${r} * * *`;
22
+ const e = d(n.daysOfWeek);
23
+ return e.length === 0 ? `${t} ${r} * * 1` : e.length === 7 ? `${t} ${r} * * *` : `${t} ${r} * * ${e.join(",")}`;
24
+ }
25
+ function l(n) {
26
+ const t = /* @__PURE__ */ new Set();
27
+ for (const r of n.split(",")) {
28
+ const e = r.trim();
29
+ if (/^[0-7]$/.test(e)) {
30
+ t.add(Number(e) === 7 ? 0 : Number(e));
31
+ continue;
32
+ }
33
+ const o = /^([0-7])-([0-7])$/.exec(e);
34
+ if (!o) return null;
35
+ const c = Number(o[1]), u = Number(o[2]);
36
+ if (c > u) return null;
37
+ for (let s = c; s <= u; s += 1)
38
+ t.add(s === 7 ? 0 : s);
39
+ }
40
+ return t.size > 0 ? Array.from(t).sort((r, e) => r - e) : null;
41
+ }
42
+ function y(n) {
43
+ if (!n) return null;
44
+ const t = n.trim().split(/\s+/);
45
+ if (t.length !== 5) return null;
46
+ const [r, e, o, c, u] = t;
47
+ if (o !== "*" || c !== "*" || !/^\d+$/.test(r) || !/^\d+$/.test(e)) return null;
48
+ const s = Number(r), i = Number(e);
49
+ if (s < 0 || s > 59 || i < 0 || i > 23) return null;
50
+ if (u === "*") return { frequency: "daily", hour: i, minute: s };
51
+ const a = l(u);
52
+ return a ? a.length === 7 ? { frequency: "daily", hour: i, minute: s } : { frequency: "weekly", hour: i, minute: s, daysOfWeek: a } : null;
53
+ }
54
+ function h(n, t, r) {
55
+ const e = new Date(Date.UTC(2026, 0, 1, n, t));
56
+ return f(r, {
57
+ hour: "2-digit",
58
+ minute: "2-digit",
59
+ timeZone: "UTC"
60
+ }).format(e);
61
+ }
62
+ function $(n, t, r) {
63
+ const e = m(r), o = [...n].sort((s, i) => (s - e + 7) % 7 - (i - e + 7) % 7), c = f(r, { weekday: "short", timeZone: "UTC" });
64
+ return o.map((s) => {
65
+ try {
66
+ return c.format(new Date(Date.UTC(2024, 0, 7 + s)));
67
+ } catch {
68
+ return t(`schedules:days.${s}`);
69
+ }
70
+ }).join(", ");
71
+ }
72
+ function p(n, t, r) {
73
+ if (n.kind === "once") {
74
+ if (!n.run_at) return t("schedules:cadence.once");
75
+ const o = f(r, {
76
+ dateStyle: "medium",
77
+ timeStyle: "short",
78
+ timeZone: n.timezone || "UTC"
79
+ }).format(new Date(n.run_at));
80
+ return t("schedules:cadence.once_at", { datetime: o });
81
+ }
82
+ if (n.kind === "interval") {
83
+ const o = n.interval_minutes ?? 60;
84
+ return o % 60 === 0 ? t("schedules:cadence.every_hours", { hours: o / 60 }) : t("schedules:cadence.every_minutes", { minutes: o });
85
+ }
86
+ const e = y(n.cron);
87
+ if (e) {
88
+ const o = h(e.hour, e.minute, r);
89
+ return e.frequency === "daily" ? t("schedules:cadence.daily", { time: o }) : e.daysOfWeek.length === 1 ? t("schedules:cadence.weekly", {
90
+ day: t(`schedules:days.${e.daysOfWeek[0]}`),
91
+ time: o
92
+ }) : t("schedules:cadence.weekly", {
93
+ day: $(e.daysOfWeek, t, r),
94
+ time: o
95
+ });
96
+ }
97
+ return n.cron ? t("schedules:cadence.custom", { cron: n.cron }) : t("schedules:cadence.custom_empty");
98
+ }
99
+ export {
100
+ y as cronToPreset,
101
+ p as describeSchedule,
102
+ T as detectLocalTimezone,
103
+ D as presetToCron
104
+ };
@@ -1,7 +1,7 @@
1
- import { createAttachmentCache as k } from "../attachments/attachmentCache.es.js";
1
+ import { createAttachmentCache as y } from "../attachments/attachmentCache.es.js";
2
2
  import { isTurnActive as T } from "./threadReducer.es.js";
3
3
  import { createThreadController as w } from "./threadController.es.js";
4
- const x = 3e4, C = 8, g = [];
4
+ const x = 3e4, C = 8, S = [];
5
5
  function N({
6
6
  http: A,
7
7
  resolveAttachment: D,
@@ -9,70 +9,70 @@ function N({
9
9
  getRunOptions: M,
10
10
  now: u = Date.now
11
11
  }) {
12
- const _ = k(D), c = /* @__PURE__ */ new Map(), i = /* @__PURE__ */ new Map(), l = /* @__PURE__ */ new Set(), d = /* @__PURE__ */ new Set();
13
- let o, a = g;
14
- const m = (e) => d.forEach((t) => t(e)), f = /* @__PURE__ */ new Set(), h = () => f.forEach((e) => e());
15
- function b() {
12
+ const _ = y(D), c = /* @__PURE__ */ new Map(), s = /* @__PURE__ */ new Map(), l = /* @__PURE__ */ new Set(), f = /* @__PURE__ */ new Set();
13
+ let o, a = S;
14
+ const m = (e) => f.forEach((t) => t(e)), h = /* @__PURE__ */ new Set(), d = () => h.forEach((e) => e());
15
+ function v() {
16
16
  const e = [...c].filter(([, t]) => T(t.controller.getState())).map(([t]) => t).sort();
17
- e.length === a.length && e.every((t, n) => t === a[n]) || (a = e.length === 0 ? g : e, l.forEach((t) => t()));
17
+ e.length === a.length && e.every((t, r) => t === a[r]) || (a = e.length === 0 ? S : e, l.forEach((t) => t()));
18
18
  }
19
- function v(e, t) {
20
- const n = {
19
+ function E(e, t) {
20
+ const r = {
21
21
  controller: t,
22
22
  idleSince: u(),
23
- unsubscribe: t.subscribe(b)
23
+ unsubscribe: t.subscribe(v)
24
24
  };
25
- return c.set(e, n), h(), b(), n;
25
+ return c.set(e, r), d(), v(), r;
26
26
  }
27
- function E(e) {
27
+ function b(e) {
28
28
  const t = w({
29
29
  http: A,
30
30
  attachments: _,
31
31
  threadId: e,
32
32
  throttleMs: L,
33
33
  getRunOptions: M,
34
- onStart: (n, r) => {
35
- o === t && (o = void 0), v(n, t), m({ type: "thread-started", threadId: n, message: r });
34
+ onStart: (r, n) => {
35
+ o === t && (o = void 0), E(r, t), m({ type: "thread-started", threadId: r, message: n });
36
36
  },
37
- onTurnEnd: (n) => {
38
- const r = c.get(n.threadId);
39
- r && (r.idleSince = u()), m({ type: "turn-ended", ...n }), S();
37
+ onTurnEnd: (r) => {
38
+ const n = c.get(r.threadId);
39
+ n && (n.idleSince = u()), m({ type: "turn-ended", ...r }), g();
40
40
  }
41
41
  });
42
42
  return t;
43
43
  }
44
- function y(e) {
45
- return (i.get(e.controller) ?? 0) === 0 && !T(e.controller.getState());
44
+ function k(e) {
45
+ return (s.get(e.controller) ?? 0) === 0 && !T(e.controller.getState());
46
46
  }
47
47
  function p(e, t) {
48
- c.delete(e), t.unsubscribe(), t.controller.dispose(), h();
48
+ c.delete(e), t.unsubscribe(), t.controller.dispose(), d();
49
49
  }
50
- function S() {
51
- const e = [...c].filter(([, r]) => y(r)), t = u() - x, n = e.filter(([r, s]) => s.idleSince > t ? !0 : (p(r, s), !1));
52
- n.sort(([, r], [, s]) => r.idleSince - s.idleSince).slice(0, Math.max(0, n.length - C)).forEach(([r, s]) => p(r, s));
50
+ function g() {
51
+ const e = [...c].filter(([, n]) => k(n)), t = u() - x, r = e.filter(([n, i]) => i.idleSince > t ? !0 : (p(n, i), !1));
52
+ r.sort(([, n], [, i]) => n.idleSince - i.idleSince).slice(0, Math.max(0, r.length - C)).forEach(([n, i]) => p(n, i));
53
53
  }
54
54
  return {
55
55
  thread(e) {
56
56
  if (e === null)
57
- return o || (o = E(null), h()), o;
58
- const t = c.get(e)?.controller ?? v(e, E(e)).controller;
59
- return S(), t;
57
+ return o || (o = b(null), d()), o;
58
+ const t = c.get(e)?.controller ?? E(e, b(e)).controller;
59
+ return g(), t;
60
60
  },
61
61
  peek: (e) => e === null ? o : c.get(e)?.controller,
62
62
  hold(e) {
63
- i.set(e, (i.get(e) ?? 0) + 1);
63
+ s.set(e, (s.get(e) ?? 0) + 1);
64
64
  let t = !1;
65
65
  return () => {
66
66
  if (t) return;
67
67
  t = !0;
68
- const n = (i.get(e) ?? 1) - 1;
69
- if (n > 0) {
70
- i.set(e, n);
68
+ const r = (s.get(e) ?? 1) - 1;
69
+ if (r > 0) {
70
+ s.set(e, r);
71
71
  return;
72
72
  }
73
- i.delete(e);
74
- const r = [...c.values()].find((s) => s.controller === e);
75
- r && (r.idleSince = u()), S();
73
+ s.delete(e);
74
+ const n = [...c.values()].find((i) => i.controller === e);
75
+ n && (n.idleSince = u()), g();
76
76
  };
77
77
  },
78
78
  getActiveThreadIds: () => a,
@@ -82,18 +82,22 @@ function N({
82
82
  };
83
83
  },
84
84
  subscribeThreads(e) {
85
- return f.add(e), () => {
86
- f.delete(e);
85
+ return h.add(e), () => {
86
+ h.delete(e);
87
87
  };
88
88
  },
89
89
  onEvent(e) {
90
- return d.add(e), () => {
91
- d.delete(e);
90
+ return f.add(e), () => {
91
+ f.delete(e);
92
92
  };
93
93
  },
94
+ reset() {
95
+ for (const [e, t] of c) p(e, t);
96
+ o?.dispose(), o = void 0, s.clear(), a = S, l.forEach((e) => e()), d();
97
+ },
94
98
  dispose() {
95
99
  for (const [e, t] of c) p(e, t);
96
- o?.dispose(), o = void 0, i.clear(), d.clear(), a = g, l.forEach((e) => e()), l.clear(), h(), f.clear();
100
+ o?.dispose(), o = void 0, s.clear(), f.clear(), a = S, l.forEach((e) => e()), l.clear(), d(), h.clear();
97
101
  }
98
102
  };
99
103
  }
@@ -1,48 +1,53 @@
1
- import { DefaultChatTransport as m } from "ai";
2
- import { errorFromResponse as d } from "../client/http.es.js";
3
- import { answersRequestBody as q, chatRequestBody as l } from "../protocol/chatRequest.es.js";
4
- import { startTrace as w, logRequestFailed as y, markReported as R, logRequestRefused as C, monitorChatResponse as T } from "./chatDiagnostics.es.js";
5
- function x(n, r, {
1
+ import { DefaultChatTransport as d } from "ai";
2
+ import { errorFromResponse as l } from "../client/http.es.js";
3
+ import { answersRequestBody as q, chatRequestBody as w } from "../protocol/chatRequest.es.js";
4
+ import { startTrace as y, logRequestFailed as g, markReported as R, logRequestRefused as C, monitorChatResponse as T } from "./chatDiagnostics.es.js";
5
+ function B(n, r, {
6
6
  lastRequest: c,
7
7
  toolOutputs: h = [],
8
8
  runOptions: u,
9
- resume: p
9
+ resume: p,
10
+ abortSignal: i
10
11
  }) {
11
- return new m({
12
+ return new d({
12
13
  api: "/chat",
13
14
  // The server holds the thread's history, so only the message being sent goes, or, when the
14
15
  // last message is an answer, the user's answers to the tool calls it waits on.
15
- prepareSendMessagesRequest: ({ messages: a }) => {
16
- const s = a.at(-1);
16
+ prepareSendMessagesRequest: ({ messages: s }) => {
17
+ const o = s.at(-1);
17
18
  return {
18
- body: s?.role === "assistant" ? q(r, s, { toolOutputs: h, options: u }) : l(r, a, u)
19
+ body: o?.role === "assistant" ? q(r, o, { toolOutputs: h, options: u }) : w(r, s, u)
19
20
  };
20
21
  },
21
22
  prepareReconnectToStreamRequest: () => ({
22
- api: p ? g(r, p.turnId, p.after) : "/chat"
23
+ api: p ? S(r, p.turnId, p.after) : "/chat"
23
24
  }),
24
25
  // The transport passes one of the paths above.
25
- fetch: async (a, s) => {
26
- const i = typeof a == "string" ? a : "/chat", f = new Headers(s?.headers), o = w(r);
27
- c.trace = o, f.set("X-Request-ID", o.requestId);
26
+ fetch: async (s, o) => {
27
+ const m = typeof s == "string" ? s : "/chat", f = new Headers(o?.headers), a = y(r);
28
+ c.trace = a, f.set("X-Request-ID", a.requestId);
28
29
  let t;
29
30
  try {
30
- t = await n.fetch(i, { ...s, headers: f });
31
+ t = await n.fetch(m, {
32
+ ...o,
33
+ signal: i ?? o?.signal,
34
+ headers: f
35
+ });
31
36
  } catch (e) {
32
- throw y(o, e), e instanceof Error ? R(e) : e;
37
+ throw g(a, e), e instanceof Error ? R(e) : e;
33
38
  }
34
39
  if (!t.ok) {
35
- o.status = t.status;
40
+ a.status = t.status;
36
41
  const e = await t.text().catch(() => "");
37
- throw C(o, t, e), R(d(t, e));
42
+ throw C(a, t, e), R(l(t, e));
38
43
  }
39
- return T(o, t);
44
+ return T(a, t);
40
45
  }
41
46
  });
42
47
  }
43
- function g(n, r, c) {
48
+ function S(n, r, c) {
44
49
  return `/chat/threads/${encodeURIComponent(n)}/turns/${encodeURIComponent(r)}/stream?after=${c}`;
45
50
  }
46
51
  export {
47
- x as createChatTransport
52
+ B as createChatTransport
48
53
  };