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