@qfei-design/make-ai-assistant 0.2.11 → 0.2.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.
package/dist/make-app.mjs CHANGED
@@ -1,19 +1,19 @@
1
- import { p as w, a as q, b as g, d as A, e as m, f as C, g as L, h as N, i as U, j as z, k as x, l as P, m as H } from "./conversation-CZfbgCXd.js";
2
- const b = (t) => {
3
- const o = g(t, 36);
1
+ import { p as w, a as B, b as g, d as b, e as v, f as R, g as H, h as j, i as P, j as $, k as K, l as O, m as J } from "./conversation-CZfbgCXd.js";
2
+ const M = (s) => {
3
+ const n = g(s, 36);
4
4
  if (!/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(
5
- o
5
+ n
6
6
  ))
7
7
  throw new Error("Invalid Make App request: UUID");
8
- return o;
9
- }, T = (t) => {
10
- const o = g(t, 512, !0);
11
- if (new TextEncoder().encode(o).length > 512 || /[\r\n/\\]/.test(o))
8
+ return n;
9
+ }, D = (s) => {
10
+ const n = g(s, 512, !0);
11
+ if (new TextEncoder().encode(n).length > 512 || /[\r\n/\\]/.test(n))
12
12
  throw new Error("Invalid Make App request: filename");
13
- return o;
14
- }, M = (t, o, a) => {
15
- const d = A(t);
16
- if (N(d, [
13
+ return n;
14
+ }, C = (s, n, a) => {
15
+ const i = b(s);
16
+ if (j(i, [
17
17
  "chatId",
18
18
  "appKey",
19
19
  "agentId",
@@ -24,177 +24,177 @@ const b = (t) => {
24
24
  "pinnedAt",
25
25
  "createdAt",
26
26
  "lastActivityAt"
27
- ]), d.appKey !== o.appKey || d.agentId !== o.agentId || a && d.chatId !== a || !["active", "deleted"].includes(String(d.status)))
27
+ ]), i.appKey !== n.appKey || i.agentId !== n.agentId || a && i.chatId !== a || !["active", "deleted"].includes(String(i.status)))
28
28
  throw new Error("Invalid Make App response: chat scope");
29
29
  return {
30
- chatId: w(d.chatId),
31
- appKey: o.appKey,
32
- agentId: o.agentId,
33
- title: g(d.title, 255, !0),
34
- status: d.status,
35
- revision: m(d.revision, 1),
36
- pinned: U(d.pinned),
37
- pinnedAt: d.pinnedAt == null ? null : g(d.pinnedAt, 64),
38
- createdAt: g(d.createdAt, 64),
39
- lastActivityAt: g(d.lastActivityAt, 64)
30
+ chatId: w(i.chatId),
31
+ appKey: n.appKey,
32
+ agentId: n.agentId,
33
+ title: g(i.title, 255, !0),
34
+ status: i.status,
35
+ revision: v(i.revision, 1),
36
+ pinned: P(i.pinned),
37
+ pinnedAt: i.pinnedAt == null ? null : g(i.pinnedAt, 64),
38
+ createdAt: g(i.createdAt, 64),
39
+ lastActivityAt: g(i.lastActivityAt, 64)
40
40
  };
41
- }, j = (t, o) => {
42
- const a = A(t), d = C(a.chats, 100).map(
43
- (h) => M(h, o)
41
+ }, Q = (s, n) => {
42
+ const a = b(s), i = R(a.chats, 100).map(
43
+ (f) => C(f, n)
44
44
  );
45
- if (new Set(d.map((h) => h.chatId)).size !== d.length)
45
+ if (new Set(i.map((f) => f.chatId)).size !== i.length)
46
46
  throw new Error("Invalid Make App response: duplicate chat");
47
- const c = g(a.nextCursor, 8192, !0), u = U(a.hasMore);
48
- if (u !== !!c)
47
+ const d = g(a.nextCursor, 8192, !0), l = P(a.hasMore);
48
+ if (l !== !!d)
49
49
  throw new Error("Invalid Make App response: chat cursor");
50
- return { chats: d, nextCursor: c, hasMore: u };
51
- }, J = (t, o, a) => {
52
- const d = A(t), c = m(d.nextCursor, -1), u = U(d.hasMore);
53
- if (c < a || u && c <= a)
50
+ return { chats: i, nextCursor: d, hasMore: l };
51
+ }, W = (s, n, a) => {
52
+ const i = b(s), d = v(i.nextCursor, -1), l = P(i.hasMore);
53
+ if (d < a || l && d <= a)
54
54
  throw new Error(
55
55
  "Invalid Make App response: history cursor did not advance"
56
56
  );
57
- let h = 0, p = a;
58
- const v = /* @__PURE__ */ new Set(), I = C(d.messages, 200).map((E) => {
59
- const y = A(E);
60
- if (y.role !== "user" && y.role !== "assistant")
57
+ let f = 0, c = a;
58
+ const h = /* @__PURE__ */ new Set(), u = R(i.messages, 200).map((A) => {
59
+ const p = b(A);
60
+ if (p.role !== "user" && p.role !== "assistant")
61
61
  throw new Error("Invalid Make App response: history role");
62
- const f = m(y.cursor);
63
- if (f <= p || f > c)
62
+ const I = v(p.cursor);
63
+ if (I <= c || I > d)
64
64
  throw new Error("Invalid Make App response: history order");
65
- p = f;
66
- const s = y.messageId !== void 0, e = s ? w(y.messageId) : `history-${o}-${f}`;
67
- if (v.has(e))
65
+ c = I;
66
+ const k = p.messageId !== void 0, E = k ? w(p.messageId) : `history-${n}-${I}`;
67
+ if (h.has(E))
68
68
  throw new Error("Invalid Make App response: duplicate message");
69
- v.add(e);
70
- const r = g(y.text, 1e5, !0);
71
- if (h += [...r].length, h > 1e6)
69
+ h.add(E);
70
+ const r = g(p.text, 1e5, !0);
71
+ if (f += [...r].length, f > 1e6)
72
72
  throw new Error("Invalid Make App response: history budget");
73
73
  return {
74
- id: e,
75
- role: y.role,
74
+ id: E,
75
+ role: p.role,
76
76
  content: r,
77
- persistent: s,
78
- cursor: f,
79
- createdAt: g(y.createdAt, 64),
80
- ...y.responseId === void 0 ? {} : { responseId: w(y.responseId) },
81
- ...y.parts === void 0 ? {} : { parts: q(y.parts) },
82
- ...y.feedback === void 0 ? {} : { feedback: z(y.feedback) }
77
+ persistent: k,
78
+ cursor: I,
79
+ createdAt: g(p.createdAt, 64),
80
+ ...p.responseId === void 0 ? {} : { responseId: w(p.responseId) },
81
+ ...p.parts === void 0 ? {} : { parts: B(p.parts) },
82
+ ...p.feedback === void 0 ? {} : { feedback: $(p.feedback) }
83
83
  };
84
84
  });
85
- return { threadId: o, messages: I, nextCursor: c, hasMore: u };
86
- }, K = (t, o) => {
87
- const a = L(t);
88
- if (a.chatId !== o.chatId || a.responseId !== o.responseId)
85
+ return { threadId: n, messages: u, nextCursor: d, hasMore: l };
86
+ }, U = (s, n) => {
87
+ const a = H(s);
88
+ if (a.chatId !== n.chatId || a.responseId !== n.responseId)
89
89
  throw new Error("Invalid Make App response: response scope");
90
90
  return a;
91
- }, S = (t, o) => {
92
- const a = A(t);
93
- if (!["uploading", "ready", "cancelled"].includes(String(a.status)) || o && a.id !== o)
91
+ }, _ = (s, n) => {
92
+ const a = b(s);
93
+ if (!["uploading", "ready", "cancelled"].includes(String(a.status)) || n && a.id !== n)
94
94
  throw new Error("Invalid Make App response: upload");
95
- const d = m(a.sizeBytes, 0, 64 << 20), c = m(a.partSize, 1 << 20, 1 << 20), u = C(a.completedParts, 64).map(
96
- (p) => m(p, 0, Math.max(0, Math.ceil(d / c) - 1))
95
+ const i = v(a.sizeBytes, 0, 64 << 20), d = v(a.partSize, 1 << 20, 1 << 20), l = R(a.completedParts, 64).map(
96
+ (c) => v(c, 0, Math.max(0, Math.ceil(i / d) - 1))
97
97
  );
98
- if (new Set(u).size !== u.length)
98
+ if (new Set(l).size !== l.length)
99
99
  throw new Error("Invalid Make App response: repeated upload part");
100
- const h = {
100
+ const f = {
101
101
  id: w(a.id),
102
102
  status: a.status,
103
- name: T(a.name),
103
+ name: D(a.name),
104
104
  contentType: g(a.contentType, 256),
105
- sizeBytes: d,
106
- partSize: c,
107
- completedParts: u
105
+ sizeBytes: i,
106
+ partSize: d,
107
+ completedParts: l
108
108
  };
109
- if (h.status === "ready") {
110
- const p = A(a.content);
111
- if (p.kind !== "reference" || p.sizeBytes !== d || !/^[0-9a-f]{64}$/.test(String(p.digestSha256)))
109
+ if (f.status === "ready") {
110
+ const c = b(a.content);
111
+ if (c.kind !== "reference" || c.sizeBytes !== i || !/^[0-9a-f]{64}$/.test(String(c.digestSha256)))
112
112
  throw new Error("Invalid Make App response: content receipt");
113
- h.content = {
113
+ f.content = {
114
114
  kind: "reference",
115
- contentRef: w(p.contentRef),
116
- sizeBytes: d,
117
- digestSha256: String(p.digestSha256)
115
+ contentRef: w(c.contentRef),
116
+ sizeBytes: i,
117
+ digestSha256: String(c.digestSha256)
118
118
  };
119
119
  }
120
- return h;
121
- }, Q = (t) => {
122
- const o = w(t.threadId), a = b(t.message.id);
123
- let d, c;
124
- if (t.message.parts !== void 0) {
125
- const u = q(t.message.parts);
126
- if (!u.length) throw new Error("Invalid Make App request: empty parts");
127
- const h = /* @__PURE__ */ new Set();
128
- for (const p of u) {
129
- const v = b(p.id);
130
- if (h.has(v))
120
+ return f;
121
+ }, X = (s) => {
122
+ const n = w(s.threadId), a = M(s.message.id);
123
+ let i, d;
124
+ if (s.message.parts !== void 0) {
125
+ const l = B(s.message.parts);
126
+ if (!l.length) throw new Error("Invalid Make App request: empty parts");
127
+ const f = /* @__PURE__ */ new Set();
128
+ for (const c of l) {
129
+ const h = M(c.id);
130
+ if (f.has(h))
131
131
  throw new Error("Invalid Make App request: duplicate part");
132
- if (h.add(v), p.kind !== "text") {
133
- p.name !== void 0 && T(p.name);
134
- const I = p.media.contentType;
135
- if (!/^[a-z0-9!#$&^_.+-]+\/[a-z0-9!#$&^_.+-]+$/.test(I) || p.kind !== "file" && (!I.startsWith(`${p.kind}/`) || I === "image/svg+xml"))
132
+ if (f.add(h), c.kind !== "text") {
133
+ c.name !== void 0 && D(c.name);
134
+ const u = c.media.contentType;
135
+ if (!/^[a-z0-9!#$&^_.+-]+\/[a-z0-9!#$&^_.+-]+$/.test(u) || c.kind !== "file" && (!u.startsWith(`${c.kind}/`) || u === "image/svg+xml"))
136
136
  throw new Error("Invalid Make App request: media type");
137
137
  }
138
138
  }
139
- if (c = u.filter((p) => p.kind === "text").map((p) => p.text).join(""), !c.trim() && u.every((p) => p.kind === "text"))
139
+ if (d = l.filter((c) => c.kind === "text").map((c) => c.text).join(""), !d.trim() && l.every((c) => c.kind === "text"))
140
140
  throw new Error("Invalid Make App request: empty input");
141
- d = { chatId: o, messageId: a, parts: u };
141
+ i = { chatId: n, messageId: a, parts: l };
142
142
  } else
143
- c = g(t.message.content, 8192), d = { chatId: o, messageId: a, text: c };
144
- if (new TextEncoder().encode(c).length > 8192 || new TextEncoder().encode(JSON.stringify(d)).length > 65536)
143
+ d = g(s.message.content, 8192), i = { chatId: n, messageId: a, text: d };
144
+ if (new TextEncoder().encode(d).length > 8192 || new TextEncoder().encode(JSON.stringify(i)).length > 65536)
145
145
  throw new Error(
146
146
  "输入内容过长,请控制在 8 KiB 文本和 32 个附件/文本片段以内"
147
147
  );
148
- return d;
149
- }, W = () => {
150
- const t = [];
151
- let o, a = !1, d;
148
+ return i;
149
+ }, G = () => {
150
+ const s = [];
151
+ let n, a = !1, i;
152
152
  return {
153
- push(c) {
153
+ push(d) {
154
154
  if (!a)
155
- if (o) {
156
- const u = o;
157
- o = void 0, u.resolve({ done: !1, value: c });
158
- } else t.length >= 4096 ? this.fail(new Error("响应事件积压超过限制,请恢复对话")) : t.push(c);
155
+ if (n) {
156
+ const l = n;
157
+ n = void 0, l.resolve({ done: !1, value: d });
158
+ } else s.length >= 4096 ? this.fail(new Error("响应事件积压超过限制,请恢复对话")) : s.push(d);
159
159
  },
160
- fail(c) {
161
- d = c, a = !0, t.length = 0, o?.reject(c), o = void 0;
160
+ fail(d) {
161
+ i = d, a = !0, s.length = 0, n?.reject(d), n = void 0;
162
162
  },
163
163
  close() {
164
- a = !0, t.length = 0, o?.resolve({ done: !0, value: void 0 }), o = void 0;
164
+ a = !0, s.length = 0, n?.resolve({ done: !0, value: void 0 }), n = void 0;
165
165
  },
166
166
  next() {
167
- return d ? Promise.reject(d) : t.length ? Promise.resolve({ done: !1, value: t.shift() }) : a ? Promise.resolve({ done: !0, value: void 0 }) : new Promise((c, u) => {
168
- o = { resolve: c, reject: u };
167
+ return i ? Promise.reject(i) : s.length ? Promise.resolve({ done: !1, value: s.shift() }) : a ? Promise.resolve({ done: !0, value: void 0 }) : new Promise((d, l) => {
168
+ n = { resolve: d, reject: l };
169
169
  });
170
170
  }
171
171
  };
172
- }, D = (t, o) => new Promise((a) => {
173
- if (o?.aborted) {
172
+ }, F = (s, n) => new Promise((a) => {
173
+ if (n?.aborted) {
174
174
  a();
175
175
  return;
176
176
  }
177
- const d = () => {
178
- clearTimeout(c), o?.removeEventListener("abort", d), a();
179
- }, c = setTimeout(d, t);
180
- o?.addEventListener("abort", d, { once: !0 });
181
- }), _ = (t) => {
182
- if (!/^\d{1,20}-\d{1,20}$/.test(t))
177
+ const i = () => {
178
+ clearTimeout(d), n?.removeEventListener("abort", i), a();
179
+ }, d = setTimeout(i, s);
180
+ n?.addEventListener("abort", i, { once: !0 });
181
+ }), T = (s) => {
182
+ if (!/^\d{1,20}-\d{1,20}$/.test(s))
183
183
  throw new Error("Invalid Make App response: stream cursor");
184
- return t.split("-").map(BigInt);
185
- }, X = (t, o) => {
186
- const [a, d] = _(t), [c, u] = _(o);
187
- return a > c || a === c && d > u;
188
- }, O = (t) => {
189
- const o = t;
190
- return [400, 401, 403, 404].includes(o?.status ?? o?.response?.status ?? 0) || [
184
+ return s.split("-").map(BigInt);
185
+ }, V = (s, n) => {
186
+ const [a, i] = T(s), [d, l] = T(n);
187
+ return a > d || a === d && i > l;
188
+ }, q = (s) => {
189
+ const n = s;
190
+ return [400, 401, 403, 404].includes(n?.status ?? n?.response?.status ?? 0) || [
191
191
  "AI_FORBIDDEN",
192
192
  "AI_AUTH_REQUIRED",
193
193
  "AI_AUTH_EXPIRED",
194
194
  "AI_NOT_FOUND",
195
195
  "AI_RESPONSE_NOT_FOUND"
196
- ].includes(o?.code ?? "") || t instanceof Error && t.message.startsWith("Invalid ");
197
- }, G = [
196
+ ].includes(n?.code ?? "") || s instanceof Error && s.message.startsWith("Invalid ");
197
+ }, Y = [
198
198
  "response.subscribed",
199
199
  "response.state",
200
200
  "response.output_item.added",
@@ -206,267 +206,267 @@ const b = (t) => {
206
206
  "response.failed",
207
207
  "response.cancelled"
208
208
  ];
209
- async function* V(t, o, a) {
209
+ async function* Z(s, n, a) {
210
210
  if (a?.aborted) return;
211
- let d = t.cursor ?? "0-0";
212
- _(d);
213
- let c = 0, u = !1, h = 0;
214
- const p = /* @__PURE__ */ new Map();
211
+ let i = s.cursor ?? "0-0";
212
+ T(i);
213
+ let d = 0, l = !1, f = 0;
214
+ const c = /* @__PURE__ */ new Map();
215
215
  yield {
216
216
  type: "response.state",
217
- chatId: t.chatId,
218
- responseId: t.responseId,
217
+ chatId: s.chatId,
218
+ responseId: s.responseId,
219
219
  status: "queued"
220
220
  };
221
- async function* v(I, E) {
222
- if (I === "completed") {
223
- let y = t.historyCursor ?? -1, f = 0;
224
- const s = [];
225
- let e = !0;
226
- for (; e && !a?.aborted; ) {
227
- if (++f > 200)
221
+ async function* h(u, A) {
222
+ if (u === "completed") {
223
+ let p = s.historyCursor ?? -1, I = 0;
224
+ const k = [];
225
+ let E = !0;
226
+ for (; E && !a?.aborted; ) {
227
+ if (++I > 200)
228
228
  throw new Error("历史恢复超过单次预算,请继续加载历史");
229
- const r = await o.loadHistory(t, y, a);
230
- for (const n of r.messages)
231
- if (n.responseId === t.responseId && n.role === "assistant" && n.persistent) {
232
- const { artifacts: i, ...l } = n;
233
- s.push(H(l));
229
+ const r = await n.loadHistory(s, p, a);
230
+ for (const e of r.messages)
231
+ if (e.responseId === s.responseId && e.role === "assistant" && e.persistent) {
232
+ const { artifacts: t, ...o } = e;
233
+ k.push(J(o));
234
234
  }
235
- if (e = r.hasMore ?? !1, e && (r.nextCursor ?? -1) <= y)
235
+ if (E = r.hasMore ?? !1, E && (r.nextCursor ?? -1) <= p)
236
236
  throw new Error("Invalid Make App response: history cursor");
237
- y = r.nextCursor ?? y;
237
+ p = r.nextCursor ?? p;
238
238
  }
239
239
  if (a?.aborted) return;
240
- if (!s.length)
240
+ if (!k.length)
241
241
  throw new Error("回答已完成,历史尚未同步,请恢复对话");
242
242
  yield {
243
243
  type: "response.reconciled",
244
- chatId: t.chatId,
245
- responseId: t.responseId,
246
- messages: s
244
+ chatId: s.chatId,
245
+ responseId: s.responseId,
246
+ messages: k
247
247
  };
248
248
  }
249
249
  yield {
250
250
  type: "response.terminal",
251
- chatId: t.chatId,
252
- responseId: t.responseId,
253
- status: I,
254
- ...E ? { code: E } : {}
251
+ chatId: s.chatId,
252
+ responseId: s.responseId,
253
+ status: u,
254
+ ...A ? { code: A } : {}
255
255
  };
256
256
  }
257
257
  for (; !a?.aborted; ) {
258
- if (c > 3) {
259
- if (++h > 600) throw new Error("响应连接中断,请恢复对话");
260
- let n;
258
+ if (d > 3) {
259
+ if (++f > 600) throw new Error("响应连接中断,请恢复对话");
260
+ let e;
261
261
  try {
262
- n = await o.getResponse(t, a);
263
- } catch (i) {
262
+ e = await n.getResponse(s, a);
263
+ } catch (t) {
264
264
  if (a?.aborted) return;
265
- if (O(i)) throw i;
265
+ if (q(t)) throw t;
266
266
  }
267
- if (n && (yield { type: "response.snapshot", snapshot: n }, x(n.status))) {
268
- yield* v(
269
- n.status
267
+ if (e && (yield { type: "response.snapshot", snapshot: e }, K(e.status))) {
268
+ yield* h(
269
+ e.status
270
270
  );
271
271
  return;
272
272
  }
273
- await D(Math.max(1e3, o.reconnectDelayMs), a);
273
+ await F(Math.max(1e3, n.reconnectDelayMs), a);
274
274
  continue;
275
275
  }
276
- const I = W();
277
- let E = 0, y = !1;
278
- const f = o.eventSourceFactory(
279
- { chatId: t.chatId, responseId: t.responseId, cursor: d },
276
+ const u = G();
277
+ let A = 0, p = !1;
278
+ const I = n.eventSourceFactory(
279
+ { chatId: s.chatId, responseId: s.responseId, cursor: i },
280
280
  { withCredentials: !0 }
281
- ), s = G.map((n) => {
282
- const i = (l) => {
283
- if (!(y || a?.aborted)) {
284
- if (l.data.length > o.maxEventCharacters * 2 || [...l.data].length > o.maxEventCharacters || E + l.data.length > 4e6) {
285
- y = !0, f.close(), I.fail(
281
+ ), k = Y.map((e) => {
282
+ const t = (o) => {
283
+ if (!(p || a?.aborted)) {
284
+ if (o.data.length > n.maxEventCharacters * 2 || [...o.data].length > n.maxEventCharacters || A + o.data.length > 4e6) {
285
+ p = !0, I.close(), u.fail(
286
286
  new Error(
287
287
  "Make App assistant SSE event exceeds maxEventCharacters"
288
288
  )
289
289
  );
290
290
  return;
291
291
  }
292
- E += l.data.length, I.push({ name: n, raw: l.data, id: l.lastEventId });
292
+ A += o.data.length, u.push({ name: e, raw: o.data, id: o.lastEventId });
293
293
  }
294
294
  };
295
- return f.addEventListener(n, i), { name: n, listener: i };
295
+ return I.addEventListener(e, t), { name: e, listener: t };
296
296
  });
297
- f.onerror = () => {
298
- !y && !a?.aborted && (y = !0, f.close(), I.push({ reconnect: !0 }));
297
+ I.onerror = () => {
298
+ !p && !a?.aborted && (p = !0, I.close(), u.push({ reconnect: !0 }));
299
299
  };
300
- const e = () => {
301
- y = !0, f.close(), I.close();
300
+ const E = () => {
301
+ p = !0, I.close(), u.close();
302
302
  };
303
- a?.addEventListener("abort", e, { once: !0 }), a?.aborted && e();
303
+ a?.addEventListener("abort", E, { once: !0 }), a?.aborted && E();
304
304
  try {
305
305
  for (; !a?.aborted; ) {
306
- const n = await I.next();
307
- if (n.done) return;
308
- const i = n.value;
309
- if ("reconnect" in i) break;
310
- if (E -= i.raw.length, i.id && !X(i.id, d)) {
311
- if (p.has(i.id) && p.get(i.id) !== i.raw)
306
+ const e = await u.next();
307
+ if (e.done) return;
308
+ const t = e.value;
309
+ if ("reconnect" in t) break;
310
+ if (A -= t.raw.length, t.id && !V(t.id, i)) {
311
+ if (c.has(t.id) && c.get(t.id) !== t.raw)
312
312
  throw new Error(
313
313
  "Invalid Make App response: conflicting stream replay"
314
314
  );
315
315
  continue;
316
316
  }
317
- const l = A(JSON.parse(i.raw));
318
- if (w(l.responseId) !== t.responseId)
317
+ const o = b(JSON.parse(t.raw));
318
+ if (w(o.responseId) !== s.responseId)
319
319
  throw new Error("Invalid Make App response: stream scope");
320
- if (i.name !== "response.subscribed") {
321
- if (i.name === "response.reset") {
322
- u = !0;
320
+ if (t.name !== "response.subscribed") {
321
+ if (t.name === "response.reset") {
322
+ l = !0;
323
323
  continue;
324
324
  }
325
- if (i.name === "response.snapshot") {
326
- const k = K(l, t);
327
- if (yield { type: "response.snapshot", snapshot: k }, x(k.status)) {
328
- yield* v(
329
- k.status
325
+ if (t.name === "response.snapshot") {
326
+ const y = U(o, s);
327
+ if (yield { type: "response.snapshot", snapshot: y }, K(y.status)) {
328
+ yield* h(
329
+ y.status
330
330
  );
331
331
  return;
332
332
  }
333
- } else if (i.name === "response.completed" || i.name === "response.cancelled") {
334
- yield* v(
335
- i.name === "response.completed" ? "completed" : "cancelled"
333
+ } else if (t.name === "response.completed" || t.name === "response.cancelled") {
334
+ yield* h(
335
+ t.name === "response.completed" ? "completed" : "cancelled"
336
336
  );
337
337
  return;
338
- } else if (i.name === "response.failed") {
339
- if (l.code === "AI_UPSTREAM_ERROR") {
340
- y = !0, f.close();
338
+ } else if (t.name === "response.failed") {
339
+ if (o.code === "AI_UPSTREAM_ERROR") {
340
+ p = !0, I.close();
341
341
  break;
342
342
  }
343
- if (l.code === "AI_FORBIDDEN" || l.code === "AI_AUTH_EXPIRED")
343
+ if (o.code === "AI_FORBIDDEN" || o.code === "AI_AUTH_EXPIRED")
344
344
  throw Object.assign(new Error("当前身份无法继续读取响应"), {
345
- code: l.code,
346
- status: l.code === "AI_FORBIDDEN" ? 403 : 401
345
+ code: o.code,
346
+ status: o.code === "AI_FORBIDDEN" ? 403 : 401
347
347
  });
348
- yield* v(
348
+ yield* h(
349
349
  "failed",
350
- typeof l.code == "string" ? l.code : void 0
350
+ typeof o.code == "string" ? o.code : void 0
351
351
  );
352
352
  return;
353
- } else i.name === "response.state" ? yield P({
354
- type: i.name,
355
- chatId: t.chatId,
356
- ...l
357
- }) : u || (yield P({ type: i.name, ...l }));
358
- i.id && (d = i.id, p.set(i.id, i.raw), p.size > 512 && p.delete(p.keys().next().value));
353
+ } else t.name === "response.state" ? yield O({
354
+ type: t.name,
355
+ chatId: s.chatId,
356
+ ...o
357
+ }) : l || (yield O({ type: t.name, ...o }));
358
+ t.id && (i = t.id, c.set(t.id, t.raw), c.size > 512 && c.delete(c.keys().next().value));
359
359
  }
360
360
  }
361
361
  } finally {
362
- f.onerror = null, f.close(), I.close();
363
- for (const { name: n, listener: i } of s)
364
- f.removeEventListener(n, i);
365
- a?.removeEventListener("abort", e);
362
+ I.onerror = null, I.close(), u.close();
363
+ for (const { name: e, listener: t } of k)
364
+ I.removeEventListener(e, t);
365
+ a?.removeEventListener("abort", E);
366
366
  }
367
- if (a?.aborted || (c += 1, console.info("[make-ai-assistant] response reconnect", {
368
- attempt: c
369
- }), await D(
370
- Math.min(o.reconnectDelayMs * 2 ** (c - 1), 5e3),
367
+ if (a?.aborted || (d += 1, console.info("[make-ai-assistant] response reconnect", {
368
+ attempt: d
369
+ }), await F(
370
+ Math.min(n.reconnectDelayMs * 2 ** (d - 1), 5e3),
371
371
  a
372
372
  ), a?.aborted)) return;
373
373
  let r;
374
374
  try {
375
- r = await o.getResponse(t, a);
376
- } catch (n) {
375
+ r = await n.getResponse(s, a);
376
+ } catch (e) {
377
377
  if (a?.aborted) return;
378
- if (O(n)) throw n;
378
+ if (q(e)) throw e;
379
379
  }
380
- if (r && x(r.status)) {
381
- yield { type: "response.snapshot", snapshot: r }, yield* v(r.status);
380
+ if (r && K(r.status)) {
381
+ yield { type: "response.snapshot", snapshot: r }, yield* h(r.status);
382
382
  return;
383
383
  }
384
- u && (c = 4);
384
+ l && (d = 4);
385
385
  }
386
386
  }
387
- const B = () => Object.assign(new Error("Operation aborted"), { name: "AbortError" }), F = (t) => {
388
- if (t?.aborted) throw B();
389
- }, Z = (t) => {
390
- const o = m(t.historyLimit ?? 100, 1, 200), a = m(
391
- t.maxEventCharacters ?? 1e6,
387
+ const L = () => Object.assign(new Error("Operation aborted"), { name: "AbortError" }), z = (s) => {
388
+ if (s?.aborted) throw L();
389
+ }, te = (s) => {
390
+ const n = v(s.historyLimit ?? 100, 1, 200), a = v(
391
+ s.maxEventCharacters ?? 1e6,
392
392
  1,
393
393
  4e6
394
- ), d = m(
395
- t.reconnectDelayMs ?? 500,
394
+ ), i = v(
395
+ s.reconnectDelayMs ?? 500,
396
396
  1,
397
397
  3e4
398
- ), c = (s) => {
399
- const e = t.resolveScope(s);
398
+ ), d = s.modelInputKinds ?? ["text"], l = s.readContent, f = (r) => {
399
+ const e = s.resolveScope(r);
400
400
  return {
401
401
  appKey: w(e.appKey),
402
402
  agentId: w(e.agentId),
403
403
  identityKey: w(e.identityKey)
404
404
  };
405
- }, u = (s) => {
406
- const e = c(s);
405
+ }, c = (r) => {
406
+ const e = f(r);
407
407
  return `${e.identityKey}\0${e.appKey}\0${e.agentId}`;
408
- }, h = async (s, e, r, n) => {
409
- F(n);
410
- const i = u(s);
408
+ }, h = async (r, e, t, o) => {
409
+ z(o);
410
+ const y = c(r);
411
411
  console.info("[make-ai-assistant] Make App operation start", { operation: e });
412
412
  try {
413
- const l = await r();
414
- if (F(n), i !== u(s)) throw B();
415
- return l;
416
- } catch (l) {
413
+ const m = await t();
414
+ if (z(o), y !== c(r)) throw L();
415
+ return m;
416
+ } catch (m) {
417
417
  throw console.warn("[make-ai-assistant] Make App operation failed", {
418
418
  operation: e,
419
- errorType: l instanceof Error ? l.name : typeof l
420
- }), l;
419
+ errorType: m instanceof Error ? m.name : typeof m
420
+ }), m;
421
421
  }
422
- }, p = (s) => ({
423
- chatId: w(s.chatId)
424
- }), v = (s) => ({
425
- ...p(s),
426
- responseId: w(s.responseId)
427
- }), I = (s) => ({
428
- ...p(s),
429
- uploadId: w(s.uploadId)
430
- }), E = async (s, e, r, n) => h(
431
- s,
422
+ }, u = (r) => ({
423
+ chatId: w(r.chatId)
424
+ }), A = (r) => ({
425
+ ...u(r),
426
+ responseId: w(r.responseId)
427
+ }), p = (r) => ({
428
+ ...u(r),
429
+ uploadId: w(r.uploadId)
430
+ }), I = async (r, e, t, o) => h(
431
+ r,
432
432
  "loadHistory",
433
- async () => J(
434
- await t.loadHistory(
433
+ async () => W(
434
+ await s.loadHistory(
435
435
  {
436
436
  chatId: w(e),
437
- cursor: m(r, -1),
438
- limit: o
437
+ cursor: v(t, -1),
438
+ limit: n
439
439
  },
440
- { signal: n }
440
+ { signal: o }
441
441
  ),
442
442
  e,
443
- r
443
+ t
444
444
  ),
445
- n
446
- ), y = async (s, e, r) => h(
447
- s,
445
+ o
446
+ ), k = async (r, e, t) => h(
447
+ r,
448
448
  "getResponse",
449
- async () => K(
450
- await t.getResponse(v(e), { signal: r }),
449
+ async () => U(
450
+ await s.getResponse(A(e), { signal: t }),
451
451
  e
452
452
  ),
453
- r
454
- ), f = (s, e, r) => {
455
- const n = u(s), i = r?.signal;
453
+ t
454
+ ), E = (r, e, t) => {
455
+ const o = c(r), y = t?.signal;
456
456
  return (async function* () {
457
- for await (const l of V(
457
+ for await (const m of Z(
458
458
  e,
459
459
  {
460
- eventSourceFactory: t.eventSourceFactory,
461
- getResponse: (k, R) => y(s, k, R),
462
- loadHistory: (k, R, $) => E(s, k.chatId, R, $),
460
+ eventSourceFactory: s.eventSourceFactory,
461
+ getResponse: (x, S) => k(r, x, S),
462
+ loadHistory: (x, S, N) => I(r, x.chatId, S, N),
463
463
  maxEventCharacters: a,
464
- reconnectDelayMs: d
464
+ reconnectDelayMs: i
465
465
  },
466
- i
466
+ y
467
467
  )) {
468
- if (i?.aborted || n !== u(s)) return;
469
- yield l;
468
+ if (y?.aborted || o !== c(r)) return;
469
+ yield m;
470
470
  }
471
471
  })();
472
472
  };
@@ -479,249 +479,251 @@ const B = () => Object.assign(new Error("Operation aborted"), { name: "AbortErro
479
479
  conversationManagement: !0,
480
480
  feedback: !0,
481
481
  attachments: !0,
482
- modelInputKinds: t.modelInputKinds ?? ["text"]
482
+ modelInputKinds: d
483
483
  },
484
- scopeKey: u,
485
- listAgents: (s, e) => h(
486
- s,
484
+ scopeKey: c,
485
+ listAgents: (r, e) => h(
486
+ r,
487
487
  "listAgents",
488
488
  async () => {
489
- const r = c(s), n = A(
490
- await t.listAgents(
491
- { appKey: r.appKey },
489
+ const t = f(r), o = b(
490
+ await s.listAgents(
491
+ { appKey: t.appKey },
492
492
  { signal: e?.signal }
493
493
  )
494
494
  );
495
- return C(n.agents, 1e3).map((i) => {
496
- const l = A(i);
497
- if (l.appKey !== r.appKey)
495
+ return R(o.agents, 1e3).map((y) => {
496
+ const m = b(y);
497
+ if (m.appKey !== t.appKey)
498
498
  throw new Error("Invalid Make App response: agent scope");
499
499
  return {
500
- agentId: w(l.agentId),
501
- appKey: r.appKey,
502
- name: g(l.name)
500
+ agentId: w(m.agentId),
501
+ appKey: t.appKey,
502
+ name: g(m.name)
503
503
  };
504
504
  });
505
505
  },
506
506
  e?.signal
507
507
  ),
508
- listConversations: (s, e, r) => h(
509
- s,
508
+ listConversations: (r, e, t) => h(
509
+ r,
510
510
  "listChats",
511
511
  async () => {
512
- const { appKey: n, agentId: i } = c(s);
513
- return j(
514
- await t.listChats(
512
+ const { appKey: o, agentId: y } = f(r);
513
+ return Q(
514
+ await s.listChats(
515
515
  {
516
- appKey: n,
517
- agentId: i,
516
+ appKey: o,
517
+ agentId: y,
518
518
  ...e?.cursor ? { cursor: g(e.cursor, 8192) } : {},
519
- limit: m(e?.limit ?? 50, 1, 100)
519
+ limit: v(e?.limit ?? 50, 1, 100)
520
520
  },
521
- { signal: r?.signal }
521
+ { signal: t?.signal }
522
522
  ),
523
- { appKey: n, agentId: i }
523
+ { appKey: o, agentId: y }
524
524
  );
525
525
  },
526
- r?.signal
526
+ t?.signal
527
527
  ),
528
- createConversation: (s, e, r) => h(
529
- s,
528
+ createConversation: (r, e, t) => h(
529
+ r,
530
530
  "createChat",
531
531
  async () => {
532
- const { appKey: n, agentId: i } = c(s), l = {
533
- appKey: n,
534
- agentId: i,
535
- requestId: b(e.requestId),
532
+ const { appKey: o, agentId: y } = f(r), m = {
533
+ appKey: o,
534
+ agentId: y,
535
+ requestId: M(e.requestId),
536
536
  ...e.title === void 0 ? {} : { title: g(e.title.trim(), 255) }
537
537
  };
538
- return M(
539
- await t.createChat(l, { signal: r?.signal }),
540
- { appKey: n, agentId: i }
538
+ return C(
539
+ await s.createChat(m, { signal: t?.signal }),
540
+ { appKey: o, agentId: y }
541
541
  );
542
542
  },
543
- r?.signal
543
+ t?.signal
544
544
  ),
545
- getConversation: (s, e, r) => h(
546
- s,
545
+ getConversation: (r, e, t) => h(
546
+ r,
547
547
  "getChat",
548
- async () => M(
549
- await t.getChat(p(e), { signal: r?.signal }),
550
- c(s),
548
+ async () => C(
549
+ await s.getChat(u(e), { signal: t?.signal }),
550
+ f(r),
551
551
  e.chatId
552
552
  ),
553
- r?.signal
553
+ t?.signal
554
554
  ),
555
- updateConversation: (s, e, r) => h(
556
- s,
555
+ updateConversation: (r, e, t) => h(
556
+ r,
557
557
  "updateChat",
558
558
  async () => {
559
- const n = {
560
- ...p(e),
561
- expectedRevision: m(e.expectedRevision, 1)
559
+ const o = {
560
+ ...u(e),
561
+ expectedRevision: v(e.expectedRevision, 1)
562
562
  };
563
- if (e.title !== void 0 && (n.title = g(e.title.trim(), 255)), e.pinned !== void 0) {
563
+ if (e.title !== void 0 && (o.title = g(e.title.trim(), 255)), e.pinned !== void 0) {
564
564
  if (typeof e.pinned != "boolean")
565
565
  throw new Error("Invalid Make App request: pinned");
566
- n.pinned = e.pinned;
566
+ o.pinned = e.pinned;
567
567
  }
568
- if (n.title === void 0 && n.pinned === void 0)
568
+ if (o.title === void 0 && o.pinned === void 0)
569
569
  throw new Error("Invalid Make App request: empty patch");
570
- return M(
571
- await t.updateChat(n, { signal: r?.signal }),
572
- c(s),
570
+ return C(
571
+ await s.updateChat(o, { signal: t?.signal }),
572
+ f(r),
573
573
  e.chatId
574
574
  );
575
575
  },
576
- r?.signal
576
+ t?.signal
577
577
  ),
578
- deleteConversation: (s, e, r) => h(
579
- s,
578
+ deleteConversation: (r, e, t) => h(
579
+ r,
580
580
  "deleteChat",
581
- () => t.deleteChat(
581
+ () => s.deleteChat(
582
582
  {
583
- ...p(e),
584
- expectedRevision: m(e.expectedRevision, 1)
583
+ ...u(e),
584
+ expectedRevision: v(e.expectedRevision, 1)
585
585
  },
586
- { signal: r?.signal }
586
+ { signal: t?.signal }
587
587
  ),
588
- r?.signal
588
+ t?.signal
589
589
  ),
590
- loadConversation: async (s, e) => e?.threadId ? E(s, e.threadId, e.cursor ?? -1, e.signal) : { messages: [] },
591
- getResponse: (s, e, r) => y(s, e, r?.signal),
592
- cancelResponse: (s, e, r) => h(
593
- s,
590
+ loadConversation: async (r, e) => e?.threadId ? I(r, e.threadId, e.cursor ?? -1, e.signal) : { messages: [] },
591
+ getResponse: (r, e, t) => k(r, e, t?.signal),
592
+ cancelResponse: (r, e, t) => h(
593
+ r,
594
594
  "cancelResponse",
595
- async () => K(
596
- await t.cancelResponse(v(e), {
597
- signal: r?.signal
595
+ async () => U(
596
+ await s.cancelResponse(A(e), {
597
+ signal: t?.signal
598
598
  }),
599
599
  e
600
600
  ),
601
- r?.signal
601
+ t?.signal
602
602
  ),
603
- setFeedback: (s, e, r) => h(
604
- s,
603
+ setFeedback: (r, e, t) => h(
604
+ r,
605
605
  "setFeedback",
606
606
  async () => {
607
607
  if (!["like", "dislike", "none"].includes(e.rating))
608
608
  throw new Error("Invalid Make App request: rating");
609
- const n = g(e.reason ?? "", 128, !0).trim(), i = g(e.comment ?? "", 4e3, !0).trim();
610
- if (e.rating !== "dislike" && (n || i))
609
+ const o = g(e.reason ?? "", 128, !0).trim(), y = g(e.comment ?? "", 4e3, !0).trim();
610
+ if (e.rating !== "dislike" && (o || y))
611
611
  throw new Error("Invalid Make App request: feedback detail");
612
- return z(
613
- await t.setFeedback(
612
+ return $(
613
+ await s.setFeedback(
614
614
  {
615
- ...p(e),
615
+ ...u(e),
616
616
  messageId: w(e.messageId),
617
- requestId: b(e.requestId),
617
+ requestId: M(e.requestId),
618
618
  rating: e.rating,
619
- ...n ? { reason: n } : {},
620
- ...i ? { comment: i } : {}
619
+ ...o ? { reason: o } : {},
620
+ ...y ? { comment: y } : {}
621
621
  },
622
- { signal: r?.signal }
622
+ { signal: t?.signal }
623
623
  )
624
624
  );
625
625
  },
626
- r?.signal
626
+ t?.signal
627
627
  ),
628
- startUpload: (s, e, r) => h(
629
- s,
628
+ startUpload: (r, e, t) => h(
629
+ r,
630
630
  "startUpload",
631
- async () => S(
632
- await t.startUpload(
631
+ async () => _(
632
+ await s.startUpload(
633
633
  {
634
- ...p(e),
635
- fileId: b(e.fileId),
636
- name: T(e.name),
634
+ ...u(e),
635
+ fileId: M(e.fileId),
636
+ name: D(e.name),
637
637
  contentType: g(e.contentType, 256),
638
- sizeBytes: m(e.sizeBytes, 0, 64 << 20),
638
+ sizeBytes: v(e.sizeBytes, 0, 64 << 20),
639
639
  ...e.digestSha256 ? { digestSha256: g(e.digestSha256, 64) } : {}
640
640
  },
641
- { signal: r?.signal }
641
+ { signal: t?.signal }
642
642
  )
643
643
  ),
644
- r?.signal
644
+ t?.signal
645
645
  ),
646
- getUpload: (s, e, r) => h(
647
- s,
646
+ getUpload: (r, e, t) => h(
647
+ r,
648
648
  "getUpload",
649
- async () => S(
650
- await t.getUpload(I(e), { signal: r?.signal }),
649
+ async () => _(
650
+ await s.getUpload(p(e), { signal: t?.signal }),
651
651
  e.uploadId
652
652
  ),
653
- r?.signal
653
+ t?.signal
654
654
  ),
655
- uploadPart: (s, e, r) => h(
656
- s,
655
+ uploadPart: (r, e, t) => h(
656
+ r,
657
657
  "uploadPart",
658
658
  () => {
659
659
  if (!(e.bytes instanceof Uint8Array) || e.bytes.byteLength > 1 << 20)
660
660
  throw new Error("Invalid Make App request: upload bytes");
661
- return t.uploadPart(
661
+ return s.uploadPart(
662
662
  {
663
- ...I(e),
664
- part: m(e.part, 0, 63),
663
+ ...p(e),
664
+ part: v(e.part, 0, 63),
665
665
  bytes: e.bytes
666
666
  },
667
- { signal: r?.signal }
667
+ { signal: t?.signal }
668
668
  );
669
669
  },
670
- r?.signal
670
+ t?.signal
671
671
  ),
672
- completeUpload: (s, e, r) => h(
673
- s,
672
+ completeUpload: (r, e, t) => h(
673
+ r,
674
674
  "completeUpload",
675
- async () => S(
676
- await t.completeUpload(I(e), {
677
- signal: r?.signal
675
+ async () => _(
676
+ await s.completeUpload(p(e), {
677
+ signal: t?.signal
678
678
  }),
679
679
  e.uploadId
680
680
  ),
681
- r?.signal
681
+ t?.signal
682
682
  ),
683
- readContent: (s, e, r) => h(
684
- s,
685
- "readContent",
686
- async () => {
687
- const n = await t.readContent(
688
- { ...p(e), contentRef: w(e.contentRef) },
689
- { signal: r?.signal }
690
- );
691
- if (!(n instanceof Uint8Array) || n.byteLength > 64 << 20)
692
- throw new Error("Invalid Make App response: content bytes");
693
- return n;
694
- },
695
- r?.signal
696
- ),
697
- watchResponse: f,
698
- async *run(s, e) {
683
+ ...l ? {
684
+ readContent: (r, e, t) => h(
685
+ r,
686
+ "readContent",
687
+ async () => {
688
+ const o = await l(
689
+ { ...u(e), contentRef: w(e.contentRef) },
690
+ { signal: t?.signal }
691
+ );
692
+ if (!(o instanceof Uint8Array) || o.byteLength > 64 << 20)
693
+ throw new Error("Invalid Make App response: content bytes");
694
+ return o;
695
+ },
696
+ t?.signal
697
+ )
698
+ } : {},
699
+ watchResponse: E,
700
+ async *run(r, e) {
699
701
  if (e?.signal?.aborted) return;
700
- const r = Q(s), n = await h(
701
- s.context,
702
+ const t = X(r), o = await h(
703
+ r.context,
702
704
  "sendMessage",
703
- async () => A(
704
- await t.sendMessage(r, { signal: e?.signal })
705
+ async () => b(
706
+ await s.sendMessage(t, { signal: e?.signal })
705
707
  ),
706
708
  e?.signal
707
709
  );
708
- if (n.chatId !== r.chatId)
710
+ if (o.chatId !== t.chatId)
709
711
  throw new Error("Invalid Make App response: accepted chat");
710
- const i = w(n.responseId), l = m(n.acceptedCursor);
712
+ const y = w(o.responseId), m = v(o.acceptedCursor);
711
713
  yield {
712
714
  type: "response.accepted",
713
- chatId: r.chatId,
714
- responseId: i,
715
- messageId: r.messageId,
716
- acceptedCursor: l
717
- }, yield* f(
718
- s.context,
719
- { chatId: r.chatId, responseId: i, historyCursor: l },
715
+ chatId: t.chatId,
716
+ responseId: y,
717
+ messageId: t.messageId,
718
+ acceptedCursor: m
719
+ }, yield* E(
720
+ r.context,
721
+ { chatId: t.chatId, responseId: y, historyCursor: m },
720
722
  e
721
723
  );
722
724
  }
723
725
  };
724
726
  };
725
727
  export {
726
- Z as createMakeAppAssistantTransport
728
+ te as createMakeAppAssistantTransport
727
729
  };