@qfei-design/make-ai-assistant 0.1.0
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 +120 -0
- package/README.md +125 -0
- package/artifact-v1.schema.json +318 -0
- package/capabilities.json +60 -0
- package/dist/conversation-B5kECJz8.js +595 -0
- package/dist/conversation-DpYszfVd.cjs +1 -0
- package/dist/core/conversation.d.ts +72 -0
- package/dist/core/index.d.ts +4 -0
- package/dist/core/registry.d.ts +19 -0
- package/dist/core/sse-transport.d.ts +40 -0
- package/dist/core/text.d.ts +1 -0
- package/dist/core/types.d.ts +167 -0
- package/dist/core/validation.d.ts +14 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.mjs +64 -0
- package/dist/react/artifact-renderer.d.ts +12 -0
- package/dist/react/artifact-templates.d.ts +11 -0
- package/dist/react/format.d.ts +2 -0
- package/dist/react/index.d.ts +5 -0
- package/dist/react/make-ai-assistant.d.ts +27 -0
- package/dist/react/scale.d.ts +5 -0
- package/dist/react.cjs +1 -0
- package/dist/react.mjs +661 -0
- package/dist/sse.cjs +8 -0
- package/dist/sse.d.ts +1 -0
- package/dist/sse.mjs +157 -0
- package/dist/styles.css +615 -0
- package/dist/styles.css.d.ts +2 -0
- package/dist/testing/fixtures.d.ts +10 -0
- package/dist/testing/index.d.ts +1 -0
- package/dist/testing.cjs +1 -0
- package/dist/testing.mjs +132 -0
- package/docs/artifact-v1.md +101 -0
- package/docs/backend-contract.md +94 -0
- package/package.ai.json +52 -0
- package/package.json +115 -0
- package/recipes.json +81 -0
package/dist/sse.mjs
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { p as b } from "./conversation-B5kECJz8.js";
|
|
2
|
+
const S = 1e6, u = (e) => typeof e == "object" && e !== null && !Array.isArray(e), E = (e, t, r) => {
|
|
3
|
+
const a = Object.keys(e).find(
|
|
4
|
+
(s) => s.toLowerCase() === t.toLowerCase()
|
|
5
|
+
);
|
|
6
|
+
a && delete e[a], e[t] = r;
|
|
7
|
+
}, A = (e) => {
|
|
8
|
+
const t = {};
|
|
9
|
+
return Array.isArray(e) ? e.forEach(([r, a]) => E(t, r, a)) : e && typeof e.forEach == "function" ? e.forEach(
|
|
10
|
+
(r, a) => E(t, a, r)
|
|
11
|
+
) : e && Object.entries(e).forEach(
|
|
12
|
+
([r, a]) => E(t, r, a)
|
|
13
|
+
), E(t, "Accept", "text/event-stream"), E(t, "Content-Type", "application/json"), t;
|
|
14
|
+
}, I = (e, t) => {
|
|
15
|
+
if (!u(t)) throw new Error("Invalid assistant SSE event: data");
|
|
16
|
+
if (Object.prototype.hasOwnProperty.call(t, "type"))
|
|
17
|
+
throw new Error("Invalid assistant SSE event: unsupported field");
|
|
18
|
+
try {
|
|
19
|
+
return b({ ...t, type: e });
|
|
20
|
+
} catch (r) {
|
|
21
|
+
throw r instanceof Error && r.message.startsWith("Invalid assistant event") ? new Error(r.message.replace("Invalid assistant event", "Invalid assistant SSE event")) : r;
|
|
22
|
+
}
|
|
23
|
+
}, w = (e) => {
|
|
24
|
+
if (!e.trim()) return;
|
|
25
|
+
const t = e.split(`
|
|
26
|
+
`), r = t.find((n) => n.startsWith("event:"))?.slice(6).trim(), a = t.filter((n) => n.startsWith("data:")).map((n) => n.slice(5).trimStart()).join(`
|
|
27
|
+
`);
|
|
28
|
+
if (t.some(
|
|
29
|
+
(n) => n.startsWith("event:") || n.startsWith("data:")
|
|
30
|
+
)) {
|
|
31
|
+
if (!r || !a)
|
|
32
|
+
throw new Error("Invalid assistant SSE event: missing event or data");
|
|
33
|
+
try {
|
|
34
|
+
return I(r, JSON.parse(a));
|
|
35
|
+
} catch (n) {
|
|
36
|
+
throw n instanceof Error && n.message.startsWith("Invalid assistant SSE event") ? n : new Error("Invalid assistant SSE event: JSON");
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
async function* x(e, t, r = S) {
|
|
41
|
+
if (!e.body) throw new Error("当前运行环境无法读取 AI 助手流式响应");
|
|
42
|
+
const a = e.body.getReader(), s = new TextDecoder();
|
|
43
|
+
let n = "", d = !1, h = !1, l;
|
|
44
|
+
const c = () => (l ??= a.cancel().catch((o) => {
|
|
45
|
+
console.error("[make-ai-assistant] SSE reader cancellation failed", {
|
|
46
|
+
errorType: o instanceof Error ? o.name : typeof o
|
|
47
|
+
});
|
|
48
|
+
}), l), f = () => {
|
|
49
|
+
c();
|
|
50
|
+
};
|
|
51
|
+
t?.addEventListener("abort", f, { once: !0 });
|
|
52
|
+
const m = (o) => {
|
|
53
|
+
const i = o.type === "error" || o.type === "run.cancelled" || o.type === "run.complete";
|
|
54
|
+
return h ||= i, i;
|
|
55
|
+
};
|
|
56
|
+
try {
|
|
57
|
+
for (; ; ) {
|
|
58
|
+
if (t?.aborted) throw new DOMException("Aborted", "AbortError");
|
|
59
|
+
const o = await a.read();
|
|
60
|
+
if (t?.aborted) throw new DOMException("Aborted", "AbortError");
|
|
61
|
+
let i = s.decode(o.value, { stream: !o.done });
|
|
62
|
+
d && (i = `\r${i}`, d = !1), !o.done && i.endsWith("\r") && (i = i.slice(0, -1), d = !0), o.done && d && (i += "\r", d = !1), n += i.replace(/\r\n|\r/g, `
|
|
63
|
+
`);
|
|
64
|
+
let p = n.indexOf(`
|
|
65
|
+
|
|
66
|
+
`);
|
|
67
|
+
for (; p >= 0; ) {
|
|
68
|
+
if (p > r)
|
|
69
|
+
throw new Error("AI 助手 SSE 事件超过大小限制");
|
|
70
|
+
const y = w(n.slice(0, p));
|
|
71
|
+
if (n = n.slice(p + 2), y) {
|
|
72
|
+
const v = m(y);
|
|
73
|
+
if (yield y, v) return;
|
|
74
|
+
}
|
|
75
|
+
p = n.indexOf(`
|
|
76
|
+
|
|
77
|
+
`);
|
|
78
|
+
}
|
|
79
|
+
if (n.length > r)
|
|
80
|
+
throw new Error("AI 助手 SSE 事件超过大小限制");
|
|
81
|
+
if (o.done) break;
|
|
82
|
+
}
|
|
83
|
+
if (n.trim()) {
|
|
84
|
+
const o = w(n);
|
|
85
|
+
if (o) {
|
|
86
|
+
const i = m(o);
|
|
87
|
+
if (yield o, i) return;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
if (!h)
|
|
91
|
+
throw new Error("AI 助手流式响应在终止事件前结束");
|
|
92
|
+
} finally {
|
|
93
|
+
t?.removeEventListener("abort", f), await c(), a.releaseLock();
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
const T = (e) => {
|
|
97
|
+
const t = e.maxEventCharacters ?? S;
|
|
98
|
+
if (!Number.isInteger(t) || t <= 0)
|
|
99
|
+
throw new Error("maxEventCharacters must be a positive integer");
|
|
100
|
+
return {
|
|
101
|
+
async *run(r, a) {
|
|
102
|
+
console.info("[make-ai-assistant] SSE run start", {
|
|
103
|
+
appId: r.context.app.id
|
|
104
|
+
});
|
|
105
|
+
try {
|
|
106
|
+
if (a?.signal?.aborted)
|
|
107
|
+
throw new DOMException("Aborted", "AbortError");
|
|
108
|
+
let s;
|
|
109
|
+
const n = e.fetch ?? globalThis.fetch, d = typeof e.endpoint == "function" ? e.endpoint(r) : e.endpoint, h = typeof e.headers == "function" ? await e.headers() : e.headers, l = await n(d, {
|
|
110
|
+
method: "POST",
|
|
111
|
+
body: JSON.stringify(r),
|
|
112
|
+
credentials: e.credentials ?? "include",
|
|
113
|
+
headers: A(h),
|
|
114
|
+
signal: a?.signal
|
|
115
|
+
});
|
|
116
|
+
if (!l.ok) {
|
|
117
|
+
if (console.error("[make-ai-assistant] SSE run rejected", {
|
|
118
|
+
appId: r.context.app.id,
|
|
119
|
+
status: l.status
|
|
120
|
+
}), l.body) {
|
|
121
|
+
const c = l.body.getReader();
|
|
122
|
+
try {
|
|
123
|
+
await c.cancel();
|
|
124
|
+
} catch (f) {
|
|
125
|
+
console.error("[make-ai-assistant] SSE reader cancellation failed", {
|
|
126
|
+
errorType: f instanceof Error ? f.name : typeof f
|
|
127
|
+
});
|
|
128
|
+
} finally {
|
|
129
|
+
c.releaseLock();
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
throw new Error(`AI 助手请求失败 [${l.status}]`);
|
|
133
|
+
}
|
|
134
|
+
for await (const c of x(
|
|
135
|
+
l,
|
|
136
|
+
a?.signal,
|
|
137
|
+
t
|
|
138
|
+
))
|
|
139
|
+
(c.type === "error" || c.type === "run.cancelled" || c.type === "run.complete") && (s = c.type, console.info("[make-ai-assistant] SSE run terminal event", {
|
|
140
|
+
appId: r.context.app.id,
|
|
141
|
+
terminalType: s
|
|
142
|
+
})), yield c;
|
|
143
|
+
} catch (s) {
|
|
144
|
+
throw a?.signal?.aborted ? console.info("[make-ai-assistant] SSE run cancelled", {
|
|
145
|
+
appId: r.context.app.id
|
|
146
|
+
}) : console.error("[make-ai-assistant] SSE run failed", {
|
|
147
|
+
appId: r.context.app.id,
|
|
148
|
+
errorType: s instanceof Error ? s.name : typeof s
|
|
149
|
+
}), s;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
};
|
|
154
|
+
export {
|
|
155
|
+
S as DEFAULT_MAX_SSE_EVENT_CHARACTERS,
|
|
156
|
+
T as createSseAssistantTransport
|
|
157
|
+
};
|