@qfei-design/make-ai-assistant 0.2.13 → 0.2.15
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/PUBLIC_API.md +18 -19
- package/README.md +18 -12
- package/capabilities.json +2 -1
- package/dist/client-protocol.d.ts +24 -0
- package/dist/client-types.d.ts +420 -0
- package/dist/client.cjs +2 -0
- package/dist/client.d.ts +18 -0
- package/dist/client.mjs +1154 -0
- package/dist/conversation-BpmWa2VL.cjs +1 -0
- package/dist/conversation-DuqMadaQ.js +888 -0
- package/dist/conversation-management-DZROhXyK.js +229 -0
- package/dist/conversation-management-f1y0yRvt.cjs +1 -0
- package/dist/core/conversation-management.d.ts +15 -5
- package/dist/core/conversation.d.ts +6 -5
- package/dist/index.cjs +1 -1
- package/dist/index.mjs +4 -4
- package/dist/make-app.cjs +1 -1
- package/dist/make-app.d.ts +8 -69
- package/dist/make-app.mjs +236 -703
- package/dist/make-console.cjs +1 -1
- package/dist/make-console.mjs +1 -1
- package/dist/react/icons.d.ts +19 -0
- package/dist/react/make-ai-assistant.d.ts +1 -2
- package/dist/react/use-attachments.d.ts +2 -1
- package/dist/react/use-conversation-manager.d.ts +1 -0
- package/dist/react.cjs +4 -4
- package/dist/react.mjs +1619 -1346
- package/dist/sse.cjs +1 -1
- package/dist/sse.mjs +1 -1
- package/dist/styles.css +288 -105
- package/docs/backend-contract.md +3 -7
- package/docs/make-app-integration.md +23 -68
- package/examples/make-app/main.tsx +1 -1
- package/examples/make-app/transport.ts +36 -146
- package/package.ai.json +10 -7
- package/package.json +9 -3
- package/recipes.json +4 -3
- package/dist/conversation-CZfbgCXd.js +0 -1111
- package/dist/conversation-Cvh-IVRV.cjs +0 -1
- package/dist/make-app/protocol.d.ts +0 -27
- package/dist/make-app/stream.d.ts +0 -20
package/dist/client.mjs
ADDED
|
@@ -0,0 +1,1154 @@
|
|
|
1
|
+
import { b as _e, c as be, a as xe, p as ke, d as Se, e as Pe } from "./conversation-management-DZROhXyK.js";
|
|
2
|
+
const A = _e, g = ke, f = be, p = xe, G = Se, K = Pe;
|
|
3
|
+
function E(o, a) {
|
|
4
|
+
if (Object.keys(o).some((n) => !a.includes(n)))
|
|
5
|
+
throw new Error("Invalid request: unknown field");
|
|
6
|
+
}
|
|
7
|
+
function X(o) {
|
|
8
|
+
const a = f(o, 36);
|
|
9
|
+
if (!/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(
|
|
10
|
+
a
|
|
11
|
+
))
|
|
12
|
+
throw new Error("Invalid request: UUID");
|
|
13
|
+
return a;
|
|
14
|
+
}
|
|
15
|
+
function W(o, a = !1) {
|
|
16
|
+
return f(o, 8192, a);
|
|
17
|
+
}
|
|
18
|
+
const te = (o) => {
|
|
19
|
+
const a = f(o, 64);
|
|
20
|
+
if (!Number.isFinite(Date.parse(a)))
|
|
21
|
+
throw new Error("Invalid response: timestamp");
|
|
22
|
+
return a;
|
|
23
|
+
};
|
|
24
|
+
function me(o) {
|
|
25
|
+
const a = W(o.nextCursor, !0), n = G(o.hasMore);
|
|
26
|
+
if (n !== !!a)
|
|
27
|
+
throw new Error("Invalid response: page cursor");
|
|
28
|
+
return { nextCursor: a, hasMore: n };
|
|
29
|
+
}
|
|
30
|
+
function ne(o, a, n, l) {
|
|
31
|
+
const i = A(o);
|
|
32
|
+
if (i.appKey !== a || n && i.agentId !== n || l && i.chatId !== l || !["active", "deleted"].includes(String(i.status)))
|
|
33
|
+
throw new Error("Invalid response: chat scope");
|
|
34
|
+
return {
|
|
35
|
+
chatId: g(i.chatId),
|
|
36
|
+
appKey: a,
|
|
37
|
+
agentId: g(i.agentId),
|
|
38
|
+
title: f(i.title, 255, !0),
|
|
39
|
+
titlePending: i.titlePending === void 0 ? !1 : G(i.titlePending),
|
|
40
|
+
status: i.status,
|
|
41
|
+
revision: p(i.revision, 1),
|
|
42
|
+
pinned: G(i.pinned),
|
|
43
|
+
pinnedAt: i.pinnedAt == null ? null : te(i.pinnedAt),
|
|
44
|
+
createdAt: te(i.createdAt),
|
|
45
|
+
lastActivityAt: te(i.lastActivityAt)
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
function Me(o, a, n) {
|
|
49
|
+
const l = A(o), i = K(l.chats, 100).map((m) => ne(m, a, n));
|
|
50
|
+
if (new Set(i.map((m) => m.chatId)).size !== i.length)
|
|
51
|
+
throw new Error("Invalid response: duplicate chat");
|
|
52
|
+
return { chats: i, ...me(l) };
|
|
53
|
+
}
|
|
54
|
+
function Ce(o, a) {
|
|
55
|
+
const n = A(o);
|
|
56
|
+
return { agents: K(n.agents, 100).map((i) => {
|
|
57
|
+
const m = A(i);
|
|
58
|
+
if (m.appKey !== a) throw new Error("Invalid response: agent scope");
|
|
59
|
+
return { agentId: g(m.agentId), appKey: a, name: f(m.name, 255) };
|
|
60
|
+
}), ...me(n) };
|
|
61
|
+
}
|
|
62
|
+
function ge(o) {
|
|
63
|
+
const a = A(o), n = f(a.digestSha256, 64);
|
|
64
|
+
if (!/^[0-9a-f]{64}$/.test(n))
|
|
65
|
+
throw new Error("Invalid response: digest");
|
|
66
|
+
return {
|
|
67
|
+
contentRef: g(a.contentRef),
|
|
68
|
+
contentType: f(a.contentType, 256),
|
|
69
|
+
sizeBytes: p(a.sizeBytes),
|
|
70
|
+
digestSha256: n
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
function ve(o) {
|
|
74
|
+
return K(o, 32).map((a) => {
|
|
75
|
+
const n = A(a), l = f(n.kind, 128), i = n.id === void 0 ? {} : { id: g(n.id) };
|
|
76
|
+
return l === "text" ? { ...i, kind: l, text: f(n.text, 1e5, !0) } : ["image", "file", "audio", "video"].includes(l) ? {
|
|
77
|
+
...i,
|
|
78
|
+
kind: l,
|
|
79
|
+
...n.name === void 0 ? {} : { name: f(n.name, 512, !0) },
|
|
80
|
+
media: ge(n.media)
|
|
81
|
+
} : {
|
|
82
|
+
...i,
|
|
83
|
+
kind: "unknown",
|
|
84
|
+
originalKind: l,
|
|
85
|
+
...n.textFallback === void 0 ? {} : { textFallback: f(n.textFallback, 1e5, !0) }
|
|
86
|
+
};
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
function Ee(o) {
|
|
90
|
+
const a = A(o);
|
|
91
|
+
if (!["like", "dislike", "none"].includes(String(a.rating)))
|
|
92
|
+
throw new Error("Invalid response: feedback");
|
|
93
|
+
return {
|
|
94
|
+
id: g(a.id),
|
|
95
|
+
rating: a.rating,
|
|
96
|
+
revision: p(a.revision, 1),
|
|
97
|
+
...a.reason === void 0 ? {} : { reason: f(a.reason, 128, !0) },
|
|
98
|
+
...a.comment === void 0 ? {} : { comment: f(a.comment, 4e3, !0) }
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
function Le(o) {
|
|
102
|
+
const a = A(o);
|
|
103
|
+
let n = 0;
|
|
104
|
+
const l = K(a.messages, 200).map((i) => {
|
|
105
|
+
const m = A(i);
|
|
106
|
+
if (m.role !== "user" && m.role !== "assistant")
|
|
107
|
+
throw new Error("Invalid response: history role");
|
|
108
|
+
const P = f(m.text, 1e5, !0);
|
|
109
|
+
return n += [...P].length, {
|
|
110
|
+
messageId: g(m.messageId),
|
|
111
|
+
role: m.role,
|
|
112
|
+
cursor: W(m.cursor),
|
|
113
|
+
createdAt: te(m.createdAt),
|
|
114
|
+
text: P,
|
|
115
|
+
...m.responseId === void 0 ? {} : { responseId: g(m.responseId) },
|
|
116
|
+
...m.parts === void 0 ? {} : { parts: ve(m.parts) },
|
|
117
|
+
...m.feedback === void 0 ? {} : { feedback: Ee(m.feedback) }
|
|
118
|
+
};
|
|
119
|
+
});
|
|
120
|
+
if (n > 1e6 || new Set(l.map((i) => i.messageId)).size !== l.length)
|
|
121
|
+
throw new Error("Invalid response: history budget or duplicate identity");
|
|
122
|
+
return { messages: l, ...me(a) };
|
|
123
|
+
}
|
|
124
|
+
function le(o, a, n) {
|
|
125
|
+
const l = A(o);
|
|
126
|
+
if (l.chatId !== a || l.responseId !== n)
|
|
127
|
+
throw new Error("Invalid response: response scope");
|
|
128
|
+
const i = f(l.status, 64), m = G(l.terminal);
|
|
129
|
+
if (["completed", "failed", "cancelled"].includes(i) && !m || ["queued", "dispatched", "running"].includes(i) && m)
|
|
130
|
+
throw new Error("Invalid response: contradictory terminal");
|
|
131
|
+
const P = K(l.items, 20).map((k) => {
|
|
132
|
+
const _ = A(k);
|
|
133
|
+
return {
|
|
134
|
+
itemId: g(_.itemId),
|
|
135
|
+
partId: g(_.partId),
|
|
136
|
+
text: f(_.text, 1e5, !0),
|
|
137
|
+
final: G(_.final),
|
|
138
|
+
revision: p(_.revision, 1),
|
|
139
|
+
..._.parts === void 0 ? {} : { parts: ve(_.parts) }
|
|
140
|
+
};
|
|
141
|
+
});
|
|
142
|
+
if (P.reduce((k, _) => k + [..._.text].length, 0) > 1e5 || new Set(P.map((k) => k.itemId)).size !== P.length)
|
|
143
|
+
throw new Error("Invalid response: output budget or identity");
|
|
144
|
+
return {
|
|
145
|
+
chatId: a,
|
|
146
|
+
responseId: n,
|
|
147
|
+
status: i,
|
|
148
|
+
terminal: m,
|
|
149
|
+
snapshotRevision: p(l.snapshotRevision, 1),
|
|
150
|
+
items: P,
|
|
151
|
+
...l.cancelRequested === void 0 ? {} : { cancelRequested: G(l.cancelRequested) }
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
function ue(o, a) {
|
|
155
|
+
const n = A(o), l = g(n.id), i = f(n.status, 64), m = p(n.sizeBytes), P = p(n.partSize, 1);
|
|
156
|
+
if (a && l !== a)
|
|
157
|
+
throw new Error("Invalid response: upload identity");
|
|
158
|
+
const k = K(n.completedParts, 65536).map(
|
|
159
|
+
(M) => p(M, 0, Math.max(0, Math.ceil(m / P) - 1))
|
|
160
|
+
);
|
|
161
|
+
if (new Set(k).size !== k.length)
|
|
162
|
+
throw new Error("Invalid response: upload receipt");
|
|
163
|
+
const _ = {
|
|
164
|
+
id: l,
|
|
165
|
+
status: i,
|
|
166
|
+
name: f(n.name, 512),
|
|
167
|
+
contentType: f(n.contentType, 256),
|
|
168
|
+
sizeBytes: m,
|
|
169
|
+
partSize: P,
|
|
170
|
+
completedParts: k,
|
|
171
|
+
...n.expiresAt === void 0 ? {} : { expiresAt: te(n.expiresAt) }
|
|
172
|
+
};
|
|
173
|
+
if (i === "ready") {
|
|
174
|
+
const M = A(n.content);
|
|
175
|
+
if (M.kind !== "reference" || M.sizeBytes !== m || !/^[0-9a-f]{64}$/.test(String(M.digestSha256)))
|
|
176
|
+
throw new Error("Invalid response: upload content");
|
|
177
|
+
return {
|
|
178
|
+
..._,
|
|
179
|
+
content: {
|
|
180
|
+
kind: "reference",
|
|
181
|
+
contentRef: g(M.contentRef),
|
|
182
|
+
sizeBytes: m,
|
|
183
|
+
digestSha256: String(M.digestSha256)
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
if (i === "uploading" && !_.expiresAt)
|
|
188
|
+
throw new Error("Invalid response: upload expiry");
|
|
189
|
+
return _;
|
|
190
|
+
}
|
|
191
|
+
function Be(o, a, n) {
|
|
192
|
+
const l = A(o), i = A(l.limits);
|
|
193
|
+
if (l.apiVersion !== "v1" || l.appKey !== a || l.agentId !== n)
|
|
194
|
+
throw new Error("Invalid response: capability scope");
|
|
195
|
+
return {
|
|
196
|
+
apiVersion: "v1",
|
|
197
|
+
appKey: a,
|
|
198
|
+
agentId: n,
|
|
199
|
+
features: K(l.features, 256).map((m) => f(m, 128)),
|
|
200
|
+
modelInputKinds: K(l.modelInputKinds, 64).map((m) => f(m, 64)),
|
|
201
|
+
limits: {
|
|
202
|
+
maxInputTextBytes: p(i.maxInputTextBytes, 1),
|
|
203
|
+
maxMessageBytes: p(i.maxMessageBytes, 1),
|
|
204
|
+
maxInputParts: p(i.maxInputParts, 1),
|
|
205
|
+
maxUploadBytes: p(i.maxUploadBytes, 1),
|
|
206
|
+
uploadPartBytes: p(i.uploadPartBytes, 1),
|
|
207
|
+
maxConcurrentUploads: p(i.maxConcurrentUploads, 1),
|
|
208
|
+
maxSseEventBytes: p(i.maxSseEventBytes, 1)
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
class h extends Error {
|
|
213
|
+
status;
|
|
214
|
+
code;
|
|
215
|
+
requestId;
|
|
216
|
+
retryAfterMs;
|
|
217
|
+
outcome;
|
|
218
|
+
lastCursor;
|
|
219
|
+
constructor(a, n = {}) {
|
|
220
|
+
super(a), this.name = "ApiError", this.code = a, this.status = n.status, this.requestId = n.requestId, this.retryAfterMs = n.retryAfterMs, this.outcome = n.outcome ?? "known", this.lastCursor = n.lastCursor;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
const Z = new TextEncoder(), q = (o, a) => Object.entries(o.headers).find(
|
|
224
|
+
([n]) => n.toLowerCase() === a.toLowerCase()
|
|
225
|
+
)?.[1], H = (o) => new URLSearchParams(
|
|
226
|
+
Object.entries(o).filter(([, a]) => a !== void 0).map(([a, n]) => [a, String(n)])
|
|
227
|
+
).toString(), J = (o) => encodeURIComponent(g(o));
|
|
228
|
+
function ee(o, a) {
|
|
229
|
+
return new Promise((n, l) => {
|
|
230
|
+
const i = () => l(new h("AI_CLIENT_ABORTED"));
|
|
231
|
+
if (a.aborted) {
|
|
232
|
+
o.catch(() => {
|
|
233
|
+
}), i();
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
a.addEventListener("abort", i, { once: !0 }), o.then(n, l).finally(() => a.removeEventListener("abort", i));
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
async function* he(o, a) {
|
|
240
|
+
const n = o.body?.[Symbol.asyncIterator]();
|
|
241
|
+
if (n)
|
|
242
|
+
try {
|
|
243
|
+
for (; ; ) {
|
|
244
|
+
const l = await ee(n.next(), a);
|
|
245
|
+
if (l.done) return;
|
|
246
|
+
yield l.value;
|
|
247
|
+
}
|
|
248
|
+
} finally {
|
|
249
|
+
n.return?.().catch(() => {
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
function De(o) {
|
|
254
|
+
const a = o.apiVersion ?? "v1";
|
|
255
|
+
if (a !== "v1")
|
|
256
|
+
throw new h("AI_API_VERSION_UNSUPPORTED", { outcome: "notSent" });
|
|
257
|
+
if (!["sdk", "transport"].includes(o.transport.retryOwner))
|
|
258
|
+
throw new h("AI_BAD_REQUEST", { outcome: "notSent" });
|
|
259
|
+
const n = Object.freeze({
|
|
260
|
+
appKey: g(o.scope.appKey),
|
|
261
|
+
identityKey: f(o.scope.identityKey, 8192)
|
|
262
|
+
}), l = o.basePath ?? "/api/make/app/ai";
|
|
263
|
+
if (!/^\/(?!\/)[A-Za-z0-9/_-]+$/.test(l) || /\/v\d+\/?$/.test(l))
|
|
264
|
+
throw new Error("Invalid request: basePath");
|
|
265
|
+
if (o.transport.retryOwner === "transport" && o.retry)
|
|
266
|
+
throw new Error("Invalid request: multiple retry owners");
|
|
267
|
+
const i = p(o.retry?.maxAttempts ?? 3, 1, 5), m = p(
|
|
268
|
+
o.maxJsonBytes ?? 8 * 1024 * 1024,
|
|
269
|
+
1,
|
|
270
|
+
32 * 1024 * 1024
|
|
271
|
+
), P = p(
|
|
272
|
+
o.maxEventBytes ?? 1024 * 1024,
|
|
273
|
+
1,
|
|
274
|
+
4 * 1024 * 1024
|
|
275
|
+
), k = /* @__PURE__ */ new Set();
|
|
276
|
+
let _ = !1;
|
|
277
|
+
const M = (e, s, r, t) => {
|
|
278
|
+
try {
|
|
279
|
+
o.onDiagnostic?.({ operation: e, code: s, requestId: r, attempts: t });
|
|
280
|
+
} catch {
|
|
281
|
+
}
|
|
282
|
+
}, Y = (e, s) => {
|
|
283
|
+
if (_ || e?.signal?.aborted)
|
|
284
|
+
throw new h("AI_CLIENT_ABORTED", { outcome: "notSent" });
|
|
285
|
+
const r = p(
|
|
286
|
+
e?.timeoutMs ?? s,
|
|
287
|
+
1,
|
|
288
|
+
1440 * 60 * 1e3
|
|
289
|
+
), t = new AbortController();
|
|
290
|
+
k.add(t);
|
|
291
|
+
const c = () => t.abort();
|
|
292
|
+
e?.signal?.addEventListener("abort", c, { once: !0 });
|
|
293
|
+
let u = !1;
|
|
294
|
+
const d = setTimeout(() => {
|
|
295
|
+
u = !0, c();
|
|
296
|
+
}, r);
|
|
297
|
+
return {
|
|
298
|
+
controller: t,
|
|
299
|
+
deadline: Date.now() + r,
|
|
300
|
+
failure: (I = "known") => new h(u ? "AI_CLIENT_TIMEOUT" : "AI_CLIENT_ABORTED", {
|
|
301
|
+
outcome: I
|
|
302
|
+
}),
|
|
303
|
+
close: () => {
|
|
304
|
+
clearTimeout(d), e?.signal?.removeEventListener("abort", c), k.delete(t), t.abort();
|
|
305
|
+
}
|
|
306
|
+
};
|
|
307
|
+
}, se = async (e, s) => {
|
|
308
|
+
if (s.aborted) throw new h("AI_CLIENT_ABORTED");
|
|
309
|
+
await new Promise((r, t) => {
|
|
310
|
+
const u = setTimeout(() => {
|
|
311
|
+
s.removeEventListener("abort", d), r();
|
|
312
|
+
}, e), d = () => {
|
|
313
|
+
clearTimeout(u), s.removeEventListener("abort", d), t(new h("AI_CLIENT_ABORTED"));
|
|
314
|
+
};
|
|
315
|
+
s.addEventListener("abort", d, { once: !0 });
|
|
316
|
+
});
|
|
317
|
+
}, Ie = async (e, s, r) => {
|
|
318
|
+
let t = 0;
|
|
319
|
+
const c = [];
|
|
320
|
+
for await (const I of he(e, r)) {
|
|
321
|
+
if (t += I.byteLength, t > s)
|
|
322
|
+
throw new h("AI_RESPONSE_TOO_LARGE", {
|
|
323
|
+
status: e.status
|
|
324
|
+
});
|
|
325
|
+
c.push(I);
|
|
326
|
+
}
|
|
327
|
+
const u = new Uint8Array(t);
|
|
328
|
+
let d = 0;
|
|
329
|
+
for (const I of c)
|
|
330
|
+
u.set(I, d), d += I.length;
|
|
331
|
+
return u;
|
|
332
|
+
}, ae = async (e, s) => {
|
|
333
|
+
let r = "AI_HTTP_ERROR";
|
|
334
|
+
try {
|
|
335
|
+
const u = A(
|
|
336
|
+
JSON.parse(
|
|
337
|
+
new TextDecoder("utf-8", { fatal: !0 }).decode(
|
|
338
|
+
await Ie(e, m, s)
|
|
339
|
+
)
|
|
340
|
+
)
|
|
341
|
+
);
|
|
342
|
+
typeof u.code == "string" && u.code.length < 128 && (r = u.code);
|
|
343
|
+
} catch {
|
|
344
|
+
}
|
|
345
|
+
const t = q(e, "retry-after"), c = t === void 0 ? void 0 : /^\d+$/.test(t) ? Number(t) * 1e3 : Math.max(0, Date.parse(t) - Date.now());
|
|
346
|
+
return new h(r, {
|
|
347
|
+
status: e.status,
|
|
348
|
+
requestId: q(e, "x-request-id"),
|
|
349
|
+
retryAfterMs: Number.isFinite(c) ? c : void 0
|
|
350
|
+
});
|
|
351
|
+
}, O = (e, s, r, t) => ({
|
|
352
|
+
method: e,
|
|
353
|
+
path: l.replace(/\/$/, "") + "/" + a + s,
|
|
354
|
+
headers: {
|
|
355
|
+
accept: "application/json",
|
|
356
|
+
...r === void 0 && t === void 0 ? {} : {
|
|
357
|
+
"content-type": t ? "application/octet-stream" : "application/json"
|
|
358
|
+
}
|
|
359
|
+
},
|
|
360
|
+
...t === void 0 ? r === void 0 ? {} : { body: Z.encode(JSON.stringify(r)) } : { body: new Uint8Array(t) }
|
|
361
|
+
});
|
|
362
|
+
async function b(e, s, r, t, c = !0, u = [200]) {
|
|
363
|
+
const d = Y(t, o.timeoutMs ?? 3e4), I = c && o.transport.retryOwner === "sdk" ? i : 1;
|
|
364
|
+
let y = !1;
|
|
365
|
+
M(e), console.info("[make-ai-assistant] client operation start", { operation: e });
|
|
366
|
+
try {
|
|
367
|
+
for (let R = 1; R <= I; R++)
|
|
368
|
+
try {
|
|
369
|
+
if (d.controller.signal.aborted || Date.now() >= d.deadline)
|
|
370
|
+
throw new h("AI_CLIENT_TIMEOUT", {
|
|
371
|
+
outcome: y ? "unknown" : "notSent"
|
|
372
|
+
});
|
|
373
|
+
y = !0;
|
|
374
|
+
const w = await ee(
|
|
375
|
+
o.transport.request(s, {
|
|
376
|
+
signal: d.controller.signal,
|
|
377
|
+
timeoutMs: Math.max(1, d.deadline - Date.now())
|
|
378
|
+
}),
|
|
379
|
+
d.controller.signal
|
|
380
|
+
);
|
|
381
|
+
if (w.status < 200 || w.status >= 300)
|
|
382
|
+
throw await ae(w, d.controller.signal);
|
|
383
|
+
if (q(w, "make-ai-api-version") !== a || !u.includes(w.status))
|
|
384
|
+
throw new h("AI_PROTOCOL_ERROR", {
|
|
385
|
+
status: w.status
|
|
386
|
+
});
|
|
387
|
+
let v;
|
|
388
|
+
if (w.status !== 204) {
|
|
389
|
+
if (!q(w, "content-type")?.toLowerCase().includes("application/json"))
|
|
390
|
+
throw new h("AI_PROTOCOL_ERROR", {
|
|
391
|
+
status: w.status
|
|
392
|
+
});
|
|
393
|
+
try {
|
|
394
|
+
v = JSON.parse(
|
|
395
|
+
new TextDecoder("utf-8", { fatal: !0 }).decode(
|
|
396
|
+
await Ie(
|
|
397
|
+
w,
|
|
398
|
+
m,
|
|
399
|
+
d.controller.signal
|
|
400
|
+
)
|
|
401
|
+
)
|
|
402
|
+
);
|
|
403
|
+
} catch (S) {
|
|
404
|
+
throw S instanceof h ? S : new h("AI_PROTOCOL_ERROR", {
|
|
405
|
+
status: w.status
|
|
406
|
+
});
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
try {
|
|
410
|
+
const S = r(v);
|
|
411
|
+
return M(
|
|
412
|
+
e,
|
|
413
|
+
void 0,
|
|
414
|
+
q(w, "x-request-id"),
|
|
415
|
+
R
|
|
416
|
+
), S;
|
|
417
|
+
} catch {
|
|
418
|
+
throw new h("AI_PROTOCOL_ERROR", {
|
|
419
|
+
status: w.status,
|
|
420
|
+
requestId: q(w, "x-request-id")
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
} catch (w) {
|
|
424
|
+
if (d.controller.signal.aborted)
|
|
425
|
+
throw d.failure(y ? "unknown" : "notSent");
|
|
426
|
+
let v = w instanceof h ? w : new h("AI_TRANSPORT_ERROR", {
|
|
427
|
+
outcome: y ? "unknown" : "notSent"
|
|
428
|
+
});
|
|
429
|
+
s.method !== "GET" && y && ((v.status ?? 0) >= 500 || v.status === 408 || ["AI_PROTOCOL_ERROR", "AI_RESPONSE_TOO_LARGE"].includes(
|
|
430
|
+
v.code
|
|
431
|
+
)) && (v = new h(v.code, {
|
|
432
|
+
...v,
|
|
433
|
+
outcome: "unknown"
|
|
434
|
+
}));
|
|
435
|
+
const S = v.code === "AI_TRANSPORT_ERROR" || [429, 502, 503, 504].includes(v.status ?? 0) && v.code !== "AI_QUOTA_EXCEEDED", T = v.retryAfterMs ?? Math.min(
|
|
436
|
+
o.retry?.maxDelayMs ?? 5e3,
|
|
437
|
+
100 * 2 ** (R - 1) * (0.5 + Math.random())
|
|
438
|
+
);
|
|
439
|
+
if (!S || R === I || d.controller.signal.aborted || Date.now() + T >= d.deadline)
|
|
440
|
+
throw v;
|
|
441
|
+
await se(T, d.controller.signal);
|
|
442
|
+
}
|
|
443
|
+
throw new h("AI_TRANSPORT_ERROR", { outcome: "unknown" });
|
|
444
|
+
} catch (R) {
|
|
445
|
+
throw M(
|
|
446
|
+
e,
|
|
447
|
+
R instanceof h ? R.code : "AI_CLIENT_ERROR"
|
|
448
|
+
), console.warn("[make-ai-assistant] client operation failed", {
|
|
449
|
+
operation: e
|
|
450
|
+
}), R;
|
|
451
|
+
} finally {
|
|
452
|
+
d.close();
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
const C = (e) => "/chats/" + J(e), fe = (e) => C(e.chatId) + "/responses/" + J(e.responseId), oe = (e, s) => ({
|
|
456
|
+
...e.cursor === void 0 ? {} : { cursor: W(e.cursor) },
|
|
457
|
+
...e.limit === void 0 ? {} : { limit: p(e.limit, 1, s) }
|
|
458
|
+
}), Re = (e) => {
|
|
459
|
+
if (E(e, ["chatId", "messageId", "text", "parts"]), g(e.chatId), X(e.messageId), e.text === void 0 == (e.parts === void 0))
|
|
460
|
+
throw new Error("Invalid request: text and parts are exclusive");
|
|
461
|
+
let s = e.text ?? "";
|
|
462
|
+
if (e.parts !== void 0) {
|
|
463
|
+
const c = /* @__PURE__ */ new Set();
|
|
464
|
+
for (const u of K(e.parts, 32)) {
|
|
465
|
+
const d = A(u), I = X(d.id);
|
|
466
|
+
if (c.has(I))
|
|
467
|
+
throw new Error("Invalid request: duplicate part");
|
|
468
|
+
if (c.add(I), d.kind === "text")
|
|
469
|
+
E(d, ["id", "kind", "text"]), s += f(d.text, 8192, !0);
|
|
470
|
+
else {
|
|
471
|
+
if (E(d, ["id", "kind", "name", "media"]), !["image", "file", "audio", "video"].includes(String(d.kind)))
|
|
472
|
+
throw new Error("Invalid request: part kind");
|
|
473
|
+
E(A(d.media), [
|
|
474
|
+
"contentRef",
|
|
475
|
+
"contentType",
|
|
476
|
+
"sizeBytes",
|
|
477
|
+
"digestSha256"
|
|
478
|
+
]);
|
|
479
|
+
const y = ge(d.media);
|
|
480
|
+
if (d.name !== void 0 && /[\r\n/\\]/.test(f(d.name, 512)))
|
|
481
|
+
throw new h("AI_BAD_REQUEST", { outcome: "notSent" });
|
|
482
|
+
if (!/^[a-z0-9!#$&^_.+-]+\/[a-z0-9!#$&^_.+-]+$/.test(
|
|
483
|
+
y.contentType
|
|
484
|
+
) || d.kind !== "file" && (!y.contentType.startsWith(String(d.kind) + "/") || y.contentType === "image/svg+xml"))
|
|
485
|
+
throw new h("AI_BAD_REQUEST", { outcome: "notSent" });
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
if (!e.parts.length || !s.trim() && e.parts.every((u) => u.kind === "text"))
|
|
489
|
+
throw new Error("Invalid request: empty message");
|
|
490
|
+
} else f(e.text, 8192);
|
|
491
|
+
if (Z.encode(s).length > 8192)
|
|
492
|
+
throw new Error("Invalid request: text exceeds 8 KiB");
|
|
493
|
+
const { chatId: r, ...t } = e;
|
|
494
|
+
if (Z.encode(JSON.stringify(t)).length > 65536)
|
|
495
|
+
throw new Error("Invalid request: message too large");
|
|
496
|
+
return t;
|
|
497
|
+
}, ie = /* @__PURE__ */ new Map();
|
|
498
|
+
async function* pe(e, s, r, t) {
|
|
499
|
+
p(r, 1, 1e4), p(t, 1);
|
|
500
|
+
const c = /* @__PURE__ */ new Set();
|
|
501
|
+
let u = s, d = 0;
|
|
502
|
+
for (let I = 0; I < r; I++) {
|
|
503
|
+
const y = await e(u);
|
|
504
|
+
for (const R of y.values) {
|
|
505
|
+
if (++d > t)
|
|
506
|
+
throw new h("AI_ITERATION_LIMIT", { lastCursor: u });
|
|
507
|
+
yield R;
|
|
508
|
+
}
|
|
509
|
+
if (!y.hasMore) return;
|
|
510
|
+
if (y.nextCursor === u || c.has(y.nextCursor))
|
|
511
|
+
throw new h("AI_PROTOCOL_ERROR");
|
|
512
|
+
c.add(y.nextCursor), u = y.nextCursor;
|
|
513
|
+
}
|
|
514
|
+
throw new h("AI_ITERATION_LIMIT", { lastCursor: u });
|
|
515
|
+
}
|
|
516
|
+
const N = {
|
|
517
|
+
apiVersion: a,
|
|
518
|
+
scope: n,
|
|
519
|
+
capabilities: {
|
|
520
|
+
async get(e, s) {
|
|
521
|
+
E(e, ["agentId"]);
|
|
522
|
+
const r = g(e.agentId), t = ie.get(r);
|
|
523
|
+
if (!_ && !s?.signal?.aborted && t && t.expires > Date.now())
|
|
524
|
+
return t.value;
|
|
525
|
+
const c = await b(
|
|
526
|
+
"capabilities.get",
|
|
527
|
+
O(
|
|
528
|
+
"GET",
|
|
529
|
+
"/capabilities?" + H({ appKey: n.appKey, agentId: r })
|
|
530
|
+
),
|
|
531
|
+
(u) => Be(u, n.appKey, r),
|
|
532
|
+
s
|
|
533
|
+
);
|
|
534
|
+
return ie.set(r, {
|
|
535
|
+
value: c,
|
|
536
|
+
expires: Date.now() + 6e4
|
|
537
|
+
}), c;
|
|
538
|
+
}
|
|
539
|
+
},
|
|
540
|
+
agents: {
|
|
541
|
+
async list(e = {}, s) {
|
|
542
|
+
return E(e, ["cursor", "limit"]), b(
|
|
543
|
+
"agents.list",
|
|
544
|
+
O(
|
|
545
|
+
"GET",
|
|
546
|
+
"/agents?" + H({ appKey: n.appKey, ...oe(e, 100) })
|
|
547
|
+
),
|
|
548
|
+
(r) => Ce(r, n.appKey),
|
|
549
|
+
s
|
|
550
|
+
);
|
|
551
|
+
}
|
|
552
|
+
},
|
|
553
|
+
chats: {
|
|
554
|
+
async create(e, s) {
|
|
555
|
+
E(e, ["agentId", "requestId", "title"]);
|
|
556
|
+
const r = g(e.agentId);
|
|
557
|
+
return X(e.requestId), e.title !== void 0 && f(e.title, 255), b(
|
|
558
|
+
"chats.create",
|
|
559
|
+
O("POST", "/chats", { appKey: n.appKey, ...e }),
|
|
560
|
+
(t) => ne(t, n.appKey, r),
|
|
561
|
+
s,
|
|
562
|
+
!0,
|
|
563
|
+
[201]
|
|
564
|
+
);
|
|
565
|
+
},
|
|
566
|
+
async list(e, s) {
|
|
567
|
+
E(e, ["agentId", "cursor", "limit"]);
|
|
568
|
+
const r = g(e.agentId);
|
|
569
|
+
return b(
|
|
570
|
+
"chats.list",
|
|
571
|
+
O(
|
|
572
|
+
"GET",
|
|
573
|
+
"/chats?" + H({
|
|
574
|
+
appKey: n.appKey,
|
|
575
|
+
agentId: r,
|
|
576
|
+
...oe(e, 100)
|
|
577
|
+
})
|
|
578
|
+
),
|
|
579
|
+
(t) => Me(t, n.appKey, r),
|
|
580
|
+
s
|
|
581
|
+
);
|
|
582
|
+
},
|
|
583
|
+
async get(e, s) {
|
|
584
|
+
return E(e, ["chatId"]), b(
|
|
585
|
+
"chats.get",
|
|
586
|
+
O("GET", C(e.chatId)),
|
|
587
|
+
(r) => ne(r, n.appKey, void 0, e.chatId),
|
|
588
|
+
s
|
|
589
|
+
);
|
|
590
|
+
},
|
|
591
|
+
async update(e, s) {
|
|
592
|
+
if (E(e, [
|
|
593
|
+
"chatId",
|
|
594
|
+
"expectedRevision",
|
|
595
|
+
"title",
|
|
596
|
+
"pinned"
|
|
597
|
+
]), p(e.expectedRevision, 1), e.title === void 0 && e.pinned === void 0)
|
|
598
|
+
throw new Error("Invalid request: empty patch");
|
|
599
|
+
e.title !== void 0 && f(e.title, 255), e.pinned !== void 0 && G(e.pinned);
|
|
600
|
+
const { chatId: r, ...t } = e;
|
|
601
|
+
return b(
|
|
602
|
+
"chats.update",
|
|
603
|
+
O("PATCH", C(r), t),
|
|
604
|
+
(c) => ne(c, n.appKey, void 0, r),
|
|
605
|
+
s,
|
|
606
|
+
!1
|
|
607
|
+
);
|
|
608
|
+
},
|
|
609
|
+
async delete(e, s) {
|
|
610
|
+
E(e, ["chatId", "expectedRevision"]), p(e.expectedRevision, 1), await b(
|
|
611
|
+
"chats.delete",
|
|
612
|
+
O(
|
|
613
|
+
"DELETE",
|
|
614
|
+
C(e.chatId) + "?" + H({ expectedRevision: e.expectedRevision })
|
|
615
|
+
),
|
|
616
|
+
() => {
|
|
617
|
+
},
|
|
618
|
+
s,
|
|
619
|
+
!1,
|
|
620
|
+
[204]
|
|
621
|
+
);
|
|
622
|
+
},
|
|
623
|
+
iterate(e, s) {
|
|
624
|
+
return pe(
|
|
625
|
+
async (r) => {
|
|
626
|
+
const t = await N.chats.list({ ...e, cursor: r }, s);
|
|
627
|
+
return { ...t, values: t.chats };
|
|
628
|
+
},
|
|
629
|
+
e.cursor,
|
|
630
|
+
s.maxPages,
|
|
631
|
+
s.maxItems
|
|
632
|
+
);
|
|
633
|
+
}
|
|
634
|
+
},
|
|
635
|
+
messages: {
|
|
636
|
+
async send(e, s) {
|
|
637
|
+
const r = Re(e);
|
|
638
|
+
return b(
|
|
639
|
+
"messages.send",
|
|
640
|
+
O("POST", C(e.chatId) + "/messages", r),
|
|
641
|
+
(t) => {
|
|
642
|
+
const c = A(t);
|
|
643
|
+
if (c.chatId !== e.chatId || c.messageId !== e.messageId)
|
|
644
|
+
throw new Error("Invalid response: accepted scope");
|
|
645
|
+
return {
|
|
646
|
+
chatId: e.chatId,
|
|
647
|
+
messageId: e.messageId,
|
|
648
|
+
responseId: g(c.responseId),
|
|
649
|
+
...c.acceptedCursor === void 0 ? {} : { acceptedCursor: W(c.acceptedCursor) }
|
|
650
|
+
};
|
|
651
|
+
},
|
|
652
|
+
s,
|
|
653
|
+
!0,
|
|
654
|
+
[202]
|
|
655
|
+
);
|
|
656
|
+
},
|
|
657
|
+
async list(e, s) {
|
|
658
|
+
E(e, ["chatId", "cursor", "limit"]);
|
|
659
|
+
const r = H(oe(e, 200));
|
|
660
|
+
return b(
|
|
661
|
+
"messages.list",
|
|
662
|
+
O(
|
|
663
|
+
"GET",
|
|
664
|
+
C(e.chatId) + "/messages" + (r ? "?" + r : "")
|
|
665
|
+
),
|
|
666
|
+
Le,
|
|
667
|
+
s
|
|
668
|
+
);
|
|
669
|
+
},
|
|
670
|
+
iterate(e, s) {
|
|
671
|
+
return pe(
|
|
672
|
+
async (r) => {
|
|
673
|
+
const t = await N.messages.list({ ...e, cursor: r }, s);
|
|
674
|
+
return { ...t, values: t.messages };
|
|
675
|
+
},
|
|
676
|
+
e.cursor,
|
|
677
|
+
s.maxPages,
|
|
678
|
+
s.maxItems
|
|
679
|
+
);
|
|
680
|
+
},
|
|
681
|
+
async sendAndWatch(e, s) {
|
|
682
|
+
const r = Date.now(), t = await N.messages.send(e, s), c = (s?.timeoutMs ?? o.stream?.maxDurationMs ?? 6e5) - (Date.now() - r);
|
|
683
|
+
if (c <= 0)
|
|
684
|
+
throw new h("AI_CLIENT_TIMEOUT", { outcome: "known" });
|
|
685
|
+
return {
|
|
686
|
+
accepted: t,
|
|
687
|
+
events: N.responses.subscribe(
|
|
688
|
+
{ chatId: t.chatId, responseId: t.responseId },
|
|
689
|
+
{ ...s, timeoutMs: c }
|
|
690
|
+
)
|
|
691
|
+
};
|
|
692
|
+
}
|
|
693
|
+
},
|
|
694
|
+
responses: {
|
|
695
|
+
async get(e, s) {
|
|
696
|
+
return E(e, ["chatId", "responseId"]), b(
|
|
697
|
+
"responses.get",
|
|
698
|
+
O("GET", fe(e)),
|
|
699
|
+
(r) => le(r, e.chatId, e.responseId),
|
|
700
|
+
s
|
|
701
|
+
);
|
|
702
|
+
},
|
|
703
|
+
subscribe(e, s) {
|
|
704
|
+
return Te(e, s);
|
|
705
|
+
},
|
|
706
|
+
async cancel(e, s) {
|
|
707
|
+
return E(e, ["chatId", "responseId"]), b(
|
|
708
|
+
"responses.cancel",
|
|
709
|
+
O("POST", fe(e) + "/cancel", {}),
|
|
710
|
+
(r) => le(r, e.chatId, e.responseId),
|
|
711
|
+
s,
|
|
712
|
+
!0,
|
|
713
|
+
[200, 202]
|
|
714
|
+
);
|
|
715
|
+
},
|
|
716
|
+
async waitForTerminal(e, s) {
|
|
717
|
+
const r = Y(s, s.timeoutMs);
|
|
718
|
+
try {
|
|
719
|
+
for (; !r.controller.signal.aborted; ) {
|
|
720
|
+
const t = await N.responses.get(e, {
|
|
721
|
+
signal: r.controller.signal,
|
|
722
|
+
timeoutMs: Math.max(1, r.deadline - Date.now())
|
|
723
|
+
});
|
|
724
|
+
if (t.terminal) return t;
|
|
725
|
+
await se(s.pollIntervalMs ?? 1e3, r.controller.signal);
|
|
726
|
+
}
|
|
727
|
+
throw new h("AI_CLIENT_TIMEOUT");
|
|
728
|
+
} finally {
|
|
729
|
+
r.close();
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
},
|
|
733
|
+
feedback: {
|
|
734
|
+
async set(e, s) {
|
|
735
|
+
if (E(e, [
|
|
736
|
+
"chatId",
|
|
737
|
+
"messageId",
|
|
738
|
+
"requestId",
|
|
739
|
+
"rating",
|
|
740
|
+
"reason",
|
|
741
|
+
"comment"
|
|
742
|
+
]), X(e.requestId), !["like", "dislike", "none"].includes(e.rating))
|
|
743
|
+
throw new Error("Invalid request: rating");
|
|
744
|
+
e.reason !== void 0 && f(e.reason, 128, !0), e.comment !== void 0 && f(e.comment, 4e3, !0);
|
|
745
|
+
const { chatId: r, messageId: t, ...c } = e;
|
|
746
|
+
return b(
|
|
747
|
+
"feedback.set",
|
|
748
|
+
O(
|
|
749
|
+
"PUT",
|
|
750
|
+
C(r) + "/messages/" + J(t) + "/feedback",
|
|
751
|
+
c
|
|
752
|
+
),
|
|
753
|
+
Ee,
|
|
754
|
+
s
|
|
755
|
+
);
|
|
756
|
+
}
|
|
757
|
+
},
|
|
758
|
+
uploads: {
|
|
759
|
+
async start(e, s) {
|
|
760
|
+
E(e, [
|
|
761
|
+
"chatId",
|
|
762
|
+
"fileId",
|
|
763
|
+
"name",
|
|
764
|
+
"contentType",
|
|
765
|
+
"sizeBytes",
|
|
766
|
+
"digestSha256"
|
|
767
|
+
]), X(e.fileId), f(e.name, 512), f(e.contentType, 256), p(e.sizeBytes, 1);
|
|
768
|
+
const { chatId: r, ...t } = e;
|
|
769
|
+
return b(
|
|
770
|
+
"uploads.start",
|
|
771
|
+
O("POST", C(r) + "/uploads", t),
|
|
772
|
+
(c) => ue(c),
|
|
773
|
+
s
|
|
774
|
+
);
|
|
775
|
+
},
|
|
776
|
+
async get(e, s) {
|
|
777
|
+
return E(e, ["chatId", "uploadId"]), b(
|
|
778
|
+
"uploads.get",
|
|
779
|
+
O(
|
|
780
|
+
"GET",
|
|
781
|
+
C(e.chatId) + "/uploads/" + J(e.uploadId)
|
|
782
|
+
),
|
|
783
|
+
(r) => ue(r, e.uploadId),
|
|
784
|
+
s
|
|
785
|
+
);
|
|
786
|
+
},
|
|
787
|
+
async putPart(e, s) {
|
|
788
|
+
if (E(e, ["chatId", "uploadId", "part", "bytes"]), p(e.part), !(e.bytes instanceof Uint8Array) || e.bytes.length > m)
|
|
789
|
+
throw new Error("Invalid request: upload bytes");
|
|
790
|
+
await b(
|
|
791
|
+
"uploads.putPart",
|
|
792
|
+
O(
|
|
793
|
+
"PUT",
|
|
794
|
+
C(e.chatId) + "/uploads/" + J(e.uploadId) + "/parts/" + e.part,
|
|
795
|
+
void 0,
|
|
796
|
+
e.bytes
|
|
797
|
+
),
|
|
798
|
+
() => {
|
|
799
|
+
},
|
|
800
|
+
s,
|
|
801
|
+
!0,
|
|
802
|
+
[204]
|
|
803
|
+
);
|
|
804
|
+
},
|
|
805
|
+
async complete(e, s) {
|
|
806
|
+
return E(e, ["chatId", "uploadId"]), b(
|
|
807
|
+
"uploads.complete",
|
|
808
|
+
O(
|
|
809
|
+
"POST",
|
|
810
|
+
C(e.chatId) + "/uploads/" + J(e.uploadId) + "/complete",
|
|
811
|
+
{}
|
|
812
|
+
),
|
|
813
|
+
(r) => ue(r, e.uploadId),
|
|
814
|
+
s
|
|
815
|
+
);
|
|
816
|
+
},
|
|
817
|
+
async uploadFile(e, s) {
|
|
818
|
+
const r = p(s?.concurrency ?? 2, 1, 8), t = Y(s, o.timeoutMs ?? 3e5), c = () => ({
|
|
819
|
+
signal: t.controller.signal,
|
|
820
|
+
timeoutMs: Math.max(1, t.deadline - Date.now())
|
|
821
|
+
});
|
|
822
|
+
try {
|
|
823
|
+
const { source: u, ...d } = e, I = await N.uploads.start(
|
|
824
|
+
{ ...d, sizeBytes: u.sizeBytes },
|
|
825
|
+
c()
|
|
826
|
+
);
|
|
827
|
+
if (I.status === "ready") return I;
|
|
828
|
+
if (I.status !== "uploading" || I.partSize > m || I.sizeBytes !== u.sizeBytes)
|
|
829
|
+
throw new h("AI_UPLOAD_UNAVAILABLE");
|
|
830
|
+
const y = new Set(I.completedParts), R = Math.ceil(u.sizeBytes / I.partSize);
|
|
831
|
+
let w = 0, v = I.completedParts.reduce(
|
|
832
|
+
(T, L) => T + Math.min(
|
|
833
|
+
I.partSize,
|
|
834
|
+
u.sizeBytes - L * I.partSize
|
|
835
|
+
),
|
|
836
|
+
0
|
|
837
|
+
);
|
|
838
|
+
const S = Array.from({ length: r }, async () => {
|
|
839
|
+
for (; w < R; ) {
|
|
840
|
+
if (t.controller.signal.aborted) throw t.failure();
|
|
841
|
+
const T = w++;
|
|
842
|
+
if (y.has(T)) continue;
|
|
843
|
+
const L = Math.min(
|
|
844
|
+
I.partSize,
|
|
845
|
+
u.sizeBytes - T * I.partSize
|
|
846
|
+
), z = await ee(
|
|
847
|
+
u.read(T * I.partSize, L, c()),
|
|
848
|
+
t.controller.signal
|
|
849
|
+
);
|
|
850
|
+
if (!(z instanceof Uint8Array) || z.length !== L)
|
|
851
|
+
throw new h("AI_CONTENT_INTEGRITY_ERROR");
|
|
852
|
+
await N.uploads.putPart(
|
|
853
|
+
{ chatId: e.chatId, uploadId: I.id, part: T, bytes: z },
|
|
854
|
+
c()
|
|
855
|
+
), v += L;
|
|
856
|
+
try {
|
|
857
|
+
s?.onProgress?.(v);
|
|
858
|
+
} catch {
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
});
|
|
862
|
+
try {
|
|
863
|
+
await Promise.all(S);
|
|
864
|
+
} catch (T) {
|
|
865
|
+
throw t.controller.abort(), await Promise.allSettled(S), T;
|
|
866
|
+
}
|
|
867
|
+
return await N.uploads.complete(
|
|
868
|
+
{ chatId: e.chatId, uploadId: I.id },
|
|
869
|
+
c()
|
|
870
|
+
);
|
|
871
|
+
} finally {
|
|
872
|
+
t.close();
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
},
|
|
876
|
+
content: {
|
|
877
|
+
async read(e, s) {
|
|
878
|
+
M("content.read"), console.info("[make-ai-assistant] client operation start", {
|
|
879
|
+
operation: "content.read"
|
|
880
|
+
}), E(e, ["chatId", "contentRef", "maxBytes"]), p(e.maxBytes, 1);
|
|
881
|
+
const r = Y(s, o.timeoutMs ?? 3e4);
|
|
882
|
+
try {
|
|
883
|
+
const t = await ee(
|
|
884
|
+
o.transport.request(
|
|
885
|
+
O(
|
|
886
|
+
"GET",
|
|
887
|
+
C(e.chatId) + "/content/" + J(e.contentRef)
|
|
888
|
+
),
|
|
889
|
+
{ signal: r.controller.signal }
|
|
890
|
+
),
|
|
891
|
+
r.controller.signal
|
|
892
|
+
);
|
|
893
|
+
if (t.status !== 200)
|
|
894
|
+
throw await ae(t, r.controller.signal);
|
|
895
|
+
if (q(t, "make-ai-api-version") !== a)
|
|
896
|
+
throw new h("AI_PROTOCOL_ERROR");
|
|
897
|
+
const c = q(t, "content-length"), u = c === void 0 ? void 0 : Number(c);
|
|
898
|
+
if (u !== void 0 && (!Number.isSafeInteger(u) || u < 0 || u > e.maxBytes))
|
|
899
|
+
throw new h("AI_RESPONSE_TOO_LARGE");
|
|
900
|
+
return {
|
|
901
|
+
contentType: q(t, "content-type"),
|
|
902
|
+
contentLength: u,
|
|
903
|
+
body: (async function* () {
|
|
904
|
+
let d = 0;
|
|
905
|
+
try {
|
|
906
|
+
for await (const I of he(
|
|
907
|
+
t,
|
|
908
|
+
r.controller.signal
|
|
909
|
+
)) {
|
|
910
|
+
if (d += I.length, d > e.maxBytes)
|
|
911
|
+
throw new h("AI_RESPONSE_TOO_LARGE");
|
|
912
|
+
yield I;
|
|
913
|
+
}
|
|
914
|
+
if (u !== void 0 && u !== d)
|
|
915
|
+
throw new h("AI_CONTENT_INTEGRITY_ERROR");
|
|
916
|
+
} finally {
|
|
917
|
+
r.close();
|
|
918
|
+
}
|
|
919
|
+
})()
|
|
920
|
+
};
|
|
921
|
+
} catch (t) {
|
|
922
|
+
throw r.close(), t;
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
},
|
|
926
|
+
dispose() {
|
|
927
|
+
_ = !0, ie.clear();
|
|
928
|
+
for (const e of k) e.abort();
|
|
929
|
+
k.clear();
|
|
930
|
+
}
|
|
931
|
+
};
|
|
932
|
+
async function* Te(e, s) {
|
|
933
|
+
M("responses.subscribe"), console.info("[make-ai-assistant] client operation start", {
|
|
934
|
+
operation: "responses.subscribe"
|
|
935
|
+
}), E(e, ["chatId", "responseId", "cursor"]), g(e.chatId), g(e.responseId);
|
|
936
|
+
let r = e.cursor === void 0 ? void 0 : W(e.cursor);
|
|
937
|
+
const t = Y(s, o.stream?.maxDurationMs ?? 6e5), c = /* @__PURE__ */ new Map();
|
|
938
|
+
let u = 0, d = !1;
|
|
939
|
+
const I = o.transport.retryOwner === "sdk" ? i : 1;
|
|
940
|
+
try {
|
|
941
|
+
for (let y = 1; y <= I; y++) {
|
|
942
|
+
const R = new AbortController(), w = () => R.abort();
|
|
943
|
+
t.controller.signal.addEventListener("abort", w, {
|
|
944
|
+
once: !0
|
|
945
|
+
});
|
|
946
|
+
let v, S = !1;
|
|
947
|
+
try {
|
|
948
|
+
if (t.controller.signal.aborted) throw t.failure();
|
|
949
|
+
const T = O(
|
|
950
|
+
"GET",
|
|
951
|
+
C(e.chatId) + "/events?" + H({ responseId: e.responseId, cursor: r })
|
|
952
|
+
), L = setTimeout(
|
|
953
|
+
w,
|
|
954
|
+
o.stream?.connectTimeoutMs ?? 3e4
|
|
955
|
+
);
|
|
956
|
+
let z;
|
|
957
|
+
try {
|
|
958
|
+
z = await ee(
|
|
959
|
+
o.transport.request(
|
|
960
|
+
{
|
|
961
|
+
...T,
|
|
962
|
+
headers: { ...T.headers, accept: "text/event-stream" }
|
|
963
|
+
},
|
|
964
|
+
{ signal: R.signal }
|
|
965
|
+
),
|
|
966
|
+
R.signal
|
|
967
|
+
);
|
|
968
|
+
} finally {
|
|
969
|
+
clearTimeout(L);
|
|
970
|
+
}
|
|
971
|
+
if (z.status !== 200)
|
|
972
|
+
throw await ae(z, R.signal);
|
|
973
|
+
if (q(z, "make-ai-api-version") !== a || !q(z, "content-type")?.startsWith("text/event-stream"))
|
|
974
|
+
throw new h("AI_PROTOCOL_ERROR");
|
|
975
|
+
const we = new TextDecoder("utf-8", { fatal: !0 });
|
|
976
|
+
let V = "";
|
|
977
|
+
v = setTimeout(
|
|
978
|
+
w,
|
|
979
|
+
o.stream?.idleTimeoutMs ?? 6e4
|
|
980
|
+
);
|
|
981
|
+
for await (const Ae of he(z, R.signal)) {
|
|
982
|
+
if (clearTimeout(v), v = setTimeout(
|
|
983
|
+
w,
|
|
984
|
+
o.stream?.idleTimeoutMs ?? 6e4
|
|
985
|
+
), S = !0, V += we.decode(Ae, { stream: !0 }), Z.encode(V).length > P * 2)
|
|
986
|
+
throw new h("AI_RESPONSE_TOO_LARGE");
|
|
987
|
+
let re;
|
|
988
|
+
for (; re = /\r?\n\r?\n/.exec(V); ) {
|
|
989
|
+
const Oe = V.slice(0, re.index);
|
|
990
|
+
V = V.slice(re.index + re[0].length);
|
|
991
|
+
let $ = "message", U;
|
|
992
|
+
const ce = [];
|
|
993
|
+
for (const D of Oe.split(/\r?\n/)) {
|
|
994
|
+
const j = D.indexOf(":");
|
|
995
|
+
if (j < 0 || j === 0) continue;
|
|
996
|
+
const Q = D.slice(0, j), de = D.slice(j + 1).replace(/^ /, "");
|
|
997
|
+
Q === "event" && ($ = de), Q === "id" && (U = de || void 0), Q === "data" && ce.push(de);
|
|
998
|
+
}
|
|
999
|
+
if (!ce.length) continue;
|
|
1000
|
+
const ye = ce.join(`
|
|
1001
|
+
`);
|
|
1002
|
+
if (Z.encode(ye).length > P)
|
|
1003
|
+
throw new h("AI_RESPONSE_TOO_LARGE");
|
|
1004
|
+
U && W(U);
|
|
1005
|
+
const B = A(JSON.parse(ye));
|
|
1006
|
+
if (B.responseId !== e.responseId)
|
|
1007
|
+
throw new h("AI_PROTOCOL_ERROR");
|
|
1008
|
+
const F = {
|
|
1009
|
+
responseId: e.responseId,
|
|
1010
|
+
...U ? { cursor: U } : {}
|
|
1011
|
+
};
|
|
1012
|
+
let x;
|
|
1013
|
+
switch ($) {
|
|
1014
|
+
case "response.subscribed":
|
|
1015
|
+
x = { kind: "subscribed", ...F };
|
|
1016
|
+
break;
|
|
1017
|
+
case "response.state":
|
|
1018
|
+
x = {
|
|
1019
|
+
kind: "state",
|
|
1020
|
+
...F,
|
|
1021
|
+
status: f(B.status, 64)
|
|
1022
|
+
};
|
|
1023
|
+
break;
|
|
1024
|
+
case "response.reset":
|
|
1025
|
+
x = {
|
|
1026
|
+
kind: "reset",
|
|
1027
|
+
...F,
|
|
1028
|
+
reason: f(B.reason, 128)
|
|
1029
|
+
};
|
|
1030
|
+
break;
|
|
1031
|
+
case "response.snapshot":
|
|
1032
|
+
x = {
|
|
1033
|
+
kind: "snapshot",
|
|
1034
|
+
...F,
|
|
1035
|
+
snapshot: le(
|
|
1036
|
+
B,
|
|
1037
|
+
e.chatId,
|
|
1038
|
+
e.responseId
|
|
1039
|
+
)
|
|
1040
|
+
};
|
|
1041
|
+
break;
|
|
1042
|
+
case "response.completed":
|
|
1043
|
+
case "response.failed":
|
|
1044
|
+
case "response.cancelled":
|
|
1045
|
+
x = {
|
|
1046
|
+
kind: $.slice(9),
|
|
1047
|
+
...F,
|
|
1048
|
+
...B.code === void 0 ? {} : { code: f(B.code, 128) }
|
|
1049
|
+
};
|
|
1050
|
+
break;
|
|
1051
|
+
case "response.output_item.added":
|
|
1052
|
+
case "response.output_text.delta":
|
|
1053
|
+
case "response.output_item.done": {
|
|
1054
|
+
const D = {
|
|
1055
|
+
...F,
|
|
1056
|
+
itemId: g(B.itemId),
|
|
1057
|
+
partId: g(B.partId),
|
|
1058
|
+
sequence: p(B.sequence, 1)
|
|
1059
|
+
};
|
|
1060
|
+
x = $ === "response.output_item.added" ? { kind: "itemAdded", ...D } : $ === "response.output_text.delta" ? {
|
|
1061
|
+
kind: "textDelta",
|
|
1062
|
+
...D,
|
|
1063
|
+
delta: f(B.delta, 1e5, !0)
|
|
1064
|
+
} : {
|
|
1065
|
+
kind: "itemDone",
|
|
1066
|
+
...D,
|
|
1067
|
+
text: f(B.text, 1e5, !0),
|
|
1068
|
+
final: G(B.final)
|
|
1069
|
+
};
|
|
1070
|
+
break;
|
|
1071
|
+
}
|
|
1072
|
+
default:
|
|
1073
|
+
x = {
|
|
1074
|
+
kind: "unknown",
|
|
1075
|
+
...F,
|
|
1076
|
+
name: f($, 128)
|
|
1077
|
+
};
|
|
1078
|
+
}
|
|
1079
|
+
if (x.kind === "failed" && x.code === "AI_UPSTREAM_ERROR")
|
|
1080
|
+
throw new h("AI_STREAM_INTERRUPTED", {
|
|
1081
|
+
lastCursor: r
|
|
1082
|
+
});
|
|
1083
|
+
if ((x.kind === "reset" || x.kind === "snapshot") && (d = !0), U) {
|
|
1084
|
+
r = U;
|
|
1085
|
+
const D = $ + "\0" + JSON.stringify(x), j = c.get(U);
|
|
1086
|
+
if (j !== void 0) {
|
|
1087
|
+
if (j !== D)
|
|
1088
|
+
throw new h("AI_PROTOCOL_ERROR");
|
|
1089
|
+
continue;
|
|
1090
|
+
}
|
|
1091
|
+
for (c.set(U, D), u += D.length; c.size > 512 || u > 4e6; ) {
|
|
1092
|
+
const Q = c.keys().next().value;
|
|
1093
|
+
u -= c.get(Q).length, c.delete(Q);
|
|
1094
|
+
}
|
|
1095
|
+
}
|
|
1096
|
+
if (yield x, ["completed", "failed", "cancelled"].includes(x.kind) || x.kind === "snapshot" && x.snapshot.terminal)
|
|
1097
|
+
return;
|
|
1098
|
+
}
|
|
1099
|
+
S = !1;
|
|
1100
|
+
}
|
|
1101
|
+
throw S = !0, we.decode(), new h("AI_STREAM_INTERRUPTED", { lastCursor: r });
|
|
1102
|
+
} catch (T) {
|
|
1103
|
+
if (t.controller.signal.aborted) throw t.failure();
|
|
1104
|
+
if (!(T instanceof h) && S)
|
|
1105
|
+
throw new h("AI_PROTOCOL_ERROR", { lastCursor: r });
|
|
1106
|
+
if (T instanceof h && !["AI_STREAM_INTERRUPTED", "AI_CLIENT_ABORTED"].includes(
|
|
1107
|
+
T.code
|
|
1108
|
+
) && ![429, 502, 503, 504].includes(T.status ?? 0))
|
|
1109
|
+
throw T;
|
|
1110
|
+
if (d) {
|
|
1111
|
+
for (console.info(
|
|
1112
|
+
"[make-ai-assistant] stream recovery uses confirmed snapshots"
|
|
1113
|
+
); !t.controller.signal.aborted; ) {
|
|
1114
|
+
const L = await N.responses.get(
|
|
1115
|
+
{ chatId: e.chatId, responseId: e.responseId },
|
|
1116
|
+
{
|
|
1117
|
+
signal: t.controller.signal,
|
|
1118
|
+
timeoutMs: Math.max(1, t.deadline - Date.now())
|
|
1119
|
+
}
|
|
1120
|
+
);
|
|
1121
|
+
if (yield { kind: "snapshot", snapshot: L }, L.terminal) return;
|
|
1122
|
+
await se(1e3, t.controller.signal);
|
|
1123
|
+
}
|
|
1124
|
+
throw t.failure();
|
|
1125
|
+
}
|
|
1126
|
+
if (y === I) {
|
|
1127
|
+
const L = await N.responses.get(
|
|
1128
|
+
{ chatId: e.chatId, responseId: e.responseId },
|
|
1129
|
+
{
|
|
1130
|
+
signal: t.controller.signal,
|
|
1131
|
+
timeoutMs: Math.max(1, t.deadline - Date.now())
|
|
1132
|
+
}
|
|
1133
|
+
);
|
|
1134
|
+
if (yield { kind: "snapshot", snapshot: L }, L.terminal) return;
|
|
1135
|
+
throw new h("AI_STREAM_INTERRUPTED", { lastCursor: r });
|
|
1136
|
+
}
|
|
1137
|
+
await se(
|
|
1138
|
+
Math.min(o.retry?.maxDelayMs ?? 2e3, 500 * y),
|
|
1139
|
+
t.controller.signal
|
|
1140
|
+
);
|
|
1141
|
+
} finally {
|
|
1142
|
+
clearTimeout(v), t.controller.signal.removeEventListener("abort", w), R.abort();
|
|
1143
|
+
}
|
|
1144
|
+
}
|
|
1145
|
+
} finally {
|
|
1146
|
+
t.close();
|
|
1147
|
+
}
|
|
1148
|
+
}
|
|
1149
|
+
return N;
|
|
1150
|
+
}
|
|
1151
|
+
export {
|
|
1152
|
+
h as ApiError,
|
|
1153
|
+
De as createMakeAgentClient
|
|
1154
|
+
};
|