@lunora/solid 1.0.0-alpha.23 → 1.0.0-alpha.25
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/index.d.mts +583 -3
- package/dist/index.d.ts +583 -3
- package/dist/index.mjs +8 -2
- package/dist/packem_shared/createAgent-D7EZBeql.mjs +55 -0
- package/dist/packem_shared/createAgentChat-CsoyE3RE.mjs +150 -0
- package/dist/packem_shared/createAgentState-DckYu3G8.mjs +21 -0
- package/dist/packem_shared/createAgentToolEvents-UavTO16u.mjs +99 -0
- package/dist/packem_shared/{createFlag-Bu9YfzcJ.mjs → createFlag-DQoGdUMB.mjs} +1 -1
- package/dist/packem_shared/{createInfiniteQuery-Dy9k7Px2.mjs → createInfiniteQuery-DwajCO2G.mjs} +100 -2
- package/dist/packem_shared/createStream-D9ONbGAw.mjs +70 -0
- package/dist/packem_shared/createVoiceAgent-hu59SaDl.mjs +418 -0
- package/dist/packem_shared/{stable-key-CGp4e2Ux.mjs → stable-key-DePnevIy.mjs} +2 -2
- package/package.json +4 -4
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { createMemo } from 'solid-js';
|
|
2
|
+
import { createMutation } from './createMutation-LkrbhItI.mjs';
|
|
3
|
+
import { createSubscription } from './createSubscription-D3HbChGe.mjs';
|
|
4
|
+
|
|
5
|
+
const NO_MUTATION_REF = {
|
|
6
|
+
__lunoraRef: ""
|
|
7
|
+
};
|
|
8
|
+
const resolveMaybe = (value) => typeof value === "function" ? value() : value;
|
|
9
|
+
const createAgent = (options) => {
|
|
10
|
+
const {
|
|
11
|
+
api,
|
|
12
|
+
cancel: cancelReference,
|
|
13
|
+
run: runReference,
|
|
14
|
+
runArgs,
|
|
15
|
+
threadKey
|
|
16
|
+
} = options;
|
|
17
|
+
const runMutation = createMutation(runReference);
|
|
18
|
+
const cancelMutation = createMutation(cancelReference ?? NO_MUTATION_REF);
|
|
19
|
+
const {
|
|
20
|
+
data: threadData
|
|
21
|
+
} = createSubscription(api.agents.agentThread, () => {
|
|
22
|
+
return {
|
|
23
|
+
key: resolveMaybe(threadKey)
|
|
24
|
+
};
|
|
25
|
+
});
|
|
26
|
+
const thread = createMemo(() => threadData());
|
|
27
|
+
const status = createMemo(() => thread()?.status);
|
|
28
|
+
const run = async (input, arguments_) => {
|
|
29
|
+
await runMutation.mutate({
|
|
30
|
+
input,
|
|
31
|
+
threadKey: resolveMaybe(threadKey),
|
|
32
|
+
...runArgs,
|
|
33
|
+
...arguments_
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
const cancel = async () => {
|
|
37
|
+
const instanceId = thread()?.instanceId;
|
|
38
|
+
if (cancelReference === void 0 || instanceId === void 0) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
await cancelMutation.mutate({
|
|
42
|
+
instanceId,
|
|
43
|
+
threadKey: resolveMaybe(threadKey)
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
return {
|
|
47
|
+
cancel,
|
|
48
|
+
pending: runMutation.pending,
|
|
49
|
+
run,
|
|
50
|
+
status,
|
|
51
|
+
thread
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export { NO_MUTATION_REF, createAgent, resolveMaybe };
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { createSignal, createMemo } from 'solid-js';
|
|
2
|
+
import { resolveMaybe, NO_MUTATION_REF } from './createAgent-D7EZBeql.mjs';
|
|
3
|
+
import { createMutation } from './createMutation-LkrbhItI.mjs';
|
|
4
|
+
import { createStream } from './createStream-D9ONbGAw.mjs';
|
|
5
|
+
import { createSubscription } from './createSubscription-D3HbChGe.mjs';
|
|
6
|
+
|
|
7
|
+
const NO_STREAM_REF = {
|
|
8
|
+
__lunoraRef: ""
|
|
9
|
+
};
|
|
10
|
+
const reconcileOptimistic = (optimistic, durable) => {
|
|
11
|
+
const pool = durable.filter((message) => message.role === "user").map((message) => message.content);
|
|
12
|
+
return optimistic.filter((pending) => {
|
|
13
|
+
const index = pool.indexOf(pending.content);
|
|
14
|
+
if (index !== -1) {
|
|
15
|
+
pool.splice(index, 1);
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
return true;
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
const createAgentChat = (options) => {
|
|
22
|
+
const {
|
|
23
|
+
api,
|
|
24
|
+
cancel: cancelReference,
|
|
25
|
+
limit,
|
|
26
|
+
send: sendReference,
|
|
27
|
+
sendArgs,
|
|
28
|
+
stream: streamReference,
|
|
29
|
+
threadKey
|
|
30
|
+
} = options;
|
|
31
|
+
const {
|
|
32
|
+
data: history
|
|
33
|
+
} = createSubscription(api.agents.agentMessages, () => {
|
|
34
|
+
const key = resolveMaybe(threadKey);
|
|
35
|
+
return limit === void 0 ? {
|
|
36
|
+
key
|
|
37
|
+
} : {
|
|
38
|
+
key,
|
|
39
|
+
limit
|
|
40
|
+
};
|
|
41
|
+
});
|
|
42
|
+
const {
|
|
43
|
+
data: threadData
|
|
44
|
+
} = createSubscription(api.agents.agentThread, () => {
|
|
45
|
+
return {
|
|
46
|
+
key: resolveMaybe(threadKey)
|
|
47
|
+
};
|
|
48
|
+
});
|
|
49
|
+
const streamArguments = streamReference === void 0 ? "skip" : () => {
|
|
50
|
+
return {
|
|
51
|
+
key: resolveMaybe(threadKey)
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
const {
|
|
55
|
+
chunks
|
|
56
|
+
} = createStream(streamReference ?? NO_STREAM_REF, streamArguments);
|
|
57
|
+
const sendMutation = createMutation(sendReference);
|
|
58
|
+
const cancelMutation = createMutation(cancelReference ?? NO_MUTATION_REF);
|
|
59
|
+
const approvalMutation = createMutation(api.agents.agentResolveApproval);
|
|
60
|
+
const [optimistic, setOptimistic] = createSignal([]);
|
|
61
|
+
let nextId = 0;
|
|
62
|
+
const thread = createMemo(() => threadData());
|
|
63
|
+
const status = createMemo(() => thread()?.status);
|
|
64
|
+
const durable = createMemo(() => history() ?? []);
|
|
65
|
+
const messages = createMemo(() => {
|
|
66
|
+
const rows = durable();
|
|
67
|
+
const visible = reconcileOptimistic(optimistic(), rows);
|
|
68
|
+
if (visible.length === 0) {
|
|
69
|
+
return rows;
|
|
70
|
+
}
|
|
71
|
+
return [...rows, ...visible.map((pending, index) => {
|
|
72
|
+
return {
|
|
73
|
+
content: pending.content,
|
|
74
|
+
optimistic: true,
|
|
75
|
+
role: "user",
|
|
76
|
+
seq: rows.length + index
|
|
77
|
+
};
|
|
78
|
+
})];
|
|
79
|
+
});
|
|
80
|
+
const streamingText = createMemo(() => {
|
|
81
|
+
const key = resolveMaybe(threadKey);
|
|
82
|
+
const assistantCount = durable().filter((message) => message.role === "assistant").length;
|
|
83
|
+
return chunks().filter((event) => event.kind !== "progress" && event.threadKey === key && event.turn >= assistantCount).map((delta) => delta.text).join("");
|
|
84
|
+
});
|
|
85
|
+
const send = async (input, arguments_) => {
|
|
86
|
+
const id = nextId;
|
|
87
|
+
nextId += 1;
|
|
88
|
+
setOptimistic((previous) => [...reconcileOptimistic(previous, durable()), {
|
|
89
|
+
content: input,
|
|
90
|
+
id
|
|
91
|
+
}]);
|
|
92
|
+
await sendMutation.mutate({
|
|
93
|
+
input,
|
|
94
|
+
threadKey: resolveMaybe(threadKey),
|
|
95
|
+
...sendArgs,
|
|
96
|
+
...arguments_
|
|
97
|
+
});
|
|
98
|
+
};
|
|
99
|
+
const approve = async (toolCallId, note) => {
|
|
100
|
+
const instanceId = thread()?.instanceId;
|
|
101
|
+
if (instanceId === void 0) {
|
|
102
|
+
throw new Error("createAgentChat: cannot approve — no in-flight run (thread has no instanceId)");
|
|
103
|
+
}
|
|
104
|
+
await approvalMutation.mutate({
|
|
105
|
+
decision: "approve",
|
|
106
|
+
instanceId,
|
|
107
|
+
threadKey: resolveMaybe(threadKey),
|
|
108
|
+
toolCallId,
|
|
109
|
+
...note === void 0 ? {} : {
|
|
110
|
+
note
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
};
|
|
114
|
+
const reject = async (toolCallId, note) => {
|
|
115
|
+
const instanceId = thread()?.instanceId;
|
|
116
|
+
if (instanceId === void 0) {
|
|
117
|
+
throw new Error("createAgentChat: cannot reject — no in-flight run (thread has no instanceId)");
|
|
118
|
+
}
|
|
119
|
+
await approvalMutation.mutate({
|
|
120
|
+
decision: "reject",
|
|
121
|
+
instanceId,
|
|
122
|
+
threadKey: resolveMaybe(threadKey),
|
|
123
|
+
toolCallId,
|
|
124
|
+
...note === void 0 ? {} : {
|
|
125
|
+
note
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
};
|
|
129
|
+
const cancel = async () => {
|
|
130
|
+
const instanceId = thread()?.instanceId;
|
|
131
|
+
if (cancelReference === void 0 || instanceId === void 0) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
await cancelMutation.mutate({
|
|
135
|
+
instanceId,
|
|
136
|
+
threadKey: resolveMaybe(threadKey)
|
|
137
|
+
});
|
|
138
|
+
};
|
|
139
|
+
return {
|
|
140
|
+
approve,
|
|
141
|
+
cancel,
|
|
142
|
+
messages,
|
|
143
|
+
reject,
|
|
144
|
+
send,
|
|
145
|
+
status,
|
|
146
|
+
streamingText
|
|
147
|
+
};
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
export { createAgentChat };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { createMemo } from 'solid-js';
|
|
2
|
+
import { resolveMaybe } from './createAgent-D7EZBeql.mjs';
|
|
3
|
+
import { createSubscription } from './createSubscription-D3HbChGe.mjs';
|
|
4
|
+
|
|
5
|
+
const createAgentState = (options) => {
|
|
6
|
+
const {
|
|
7
|
+
data,
|
|
8
|
+
error
|
|
9
|
+
} = createSubscription(options.api.agents.agentState, () => {
|
|
10
|
+
return {
|
|
11
|
+
key: resolveMaybe(options.threadKey)
|
|
12
|
+
};
|
|
13
|
+
});
|
|
14
|
+
const state = createMemo(() => data());
|
|
15
|
+
return {
|
|
16
|
+
error,
|
|
17
|
+
state
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export { createAgentState };
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { createMemo } from 'solid-js';
|
|
2
|
+
import { resolveMaybe } from './createAgent-D7EZBeql.mjs';
|
|
3
|
+
import { createStream } from './createStream-D9ONbGAw.mjs';
|
|
4
|
+
import { createSubscription } from './createSubscription-D3HbChGe.mjs';
|
|
5
|
+
|
|
6
|
+
const NO_STREAM_REF = {
|
|
7
|
+
__lunoraRef: ""
|
|
8
|
+
};
|
|
9
|
+
const EMPTY_MESSAGES = [];
|
|
10
|
+
const toDurableEvent = (message) => {
|
|
11
|
+
if (message.role === "assistant" && message.toolCalls) {
|
|
12
|
+
return message.toolCalls.map((call) => {
|
|
13
|
+
return {
|
|
14
|
+
input: call.input,
|
|
15
|
+
seq: message.seq,
|
|
16
|
+
toolCallId: call.id,
|
|
17
|
+
toolName: call.name,
|
|
18
|
+
type: "call"
|
|
19
|
+
};
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
if (message.role !== "tool") {
|
|
23
|
+
return void 0;
|
|
24
|
+
}
|
|
25
|
+
if (message.status === "awaiting_approval") {
|
|
26
|
+
return [{
|
|
27
|
+
seq: message.seq,
|
|
28
|
+
type: "awaiting-approval",
|
|
29
|
+
...message.toolCallId === void 0 ? {} : {
|
|
30
|
+
toolCallId: message.toolCallId
|
|
31
|
+
},
|
|
32
|
+
...message.toolName === void 0 ? {} : {
|
|
33
|
+
toolName: message.toolName
|
|
34
|
+
}
|
|
35
|
+
}];
|
|
36
|
+
}
|
|
37
|
+
return [{
|
|
38
|
+
output: message.content,
|
|
39
|
+
seq: message.seq,
|
|
40
|
+
type: "result",
|
|
41
|
+
...message.status === "approved" || message.status === "rejected" ? {
|
|
42
|
+
status: message.status
|
|
43
|
+
} : {},
|
|
44
|
+
...message.toolCallId === void 0 ? {} : {
|
|
45
|
+
toolCallId: message.toolCallId
|
|
46
|
+
},
|
|
47
|
+
...message.toolName === void 0 ? {} : {
|
|
48
|
+
toolName: message.toolName
|
|
49
|
+
}
|
|
50
|
+
}];
|
|
51
|
+
};
|
|
52
|
+
const createAgentToolEvents = (options) => {
|
|
53
|
+
const {
|
|
54
|
+
api,
|
|
55
|
+
limit,
|
|
56
|
+
stream: streamReference,
|
|
57
|
+
threadKey
|
|
58
|
+
} = options;
|
|
59
|
+
const messagesArguments = () => {
|
|
60
|
+
const key = resolveMaybe(threadKey);
|
|
61
|
+
return limit === void 0 ? {
|
|
62
|
+
key
|
|
63
|
+
} : {
|
|
64
|
+
key,
|
|
65
|
+
limit
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
const {
|
|
69
|
+
data: history
|
|
70
|
+
} = createSubscription(api.agents.agentMessages, messagesArguments);
|
|
71
|
+
const streamArguments = streamReference === void 0 ? "skip" : () => {
|
|
72
|
+
return {
|
|
73
|
+
key: resolveMaybe(threadKey)
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
const {
|
|
77
|
+
chunks
|
|
78
|
+
} = createStream(streamReference ?? NO_STREAM_REF, streamArguments);
|
|
79
|
+
const events = createMemo(() => {
|
|
80
|
+
const key = resolveMaybe(threadKey);
|
|
81
|
+
const durable = history() ?? EMPTY_MESSAGES;
|
|
82
|
+
const derived = durable.flatMap((message) => toDurableEvent(message) ?? []);
|
|
83
|
+
for (const event of chunks()) {
|
|
84
|
+
if (event.kind === "progress" && event.threadKey === key) {
|
|
85
|
+
derived.push({
|
|
86
|
+
data: event.data,
|
|
87
|
+
toolCallId: event.toolCallId,
|
|
88
|
+
type: "progress"
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return derived;
|
|
93
|
+
});
|
|
94
|
+
return {
|
|
95
|
+
events
|
|
96
|
+
};
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
export { createAgentToolEvents };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createSignal, createEffect, on, onCleanup } from 'solid-js';
|
|
2
|
-
import { s as stableStringify } from './stable-key-
|
|
2
|
+
import { s as stableStringify } from './stable-key-DePnevIy.mjs';
|
|
3
3
|
import { useLunora } from './LunoraContext-C59PzHhN.mjs';
|
|
4
4
|
|
|
5
5
|
const FLAGS_EVAL_PATH = "__lunora_flags__:eval";
|
package/dist/packem_shared/{createInfiniteQuery-Dy9k7Px2.mjs → createInfiniteQuery-DwajCO2G.mjs}
RENAMED
|
@@ -1,8 +1,106 @@
|
|
|
1
1
|
import { initialPages, derivePaginationStatus, rebalance, applyLoadMore } from '@lunora/client/pagination';
|
|
2
2
|
import { createMemo, createSignal, createEffect, on, onCleanup } from 'solid-js';
|
|
3
|
-
import { s as stableStringify } from './stable-key-
|
|
3
|
+
import { s as stableStringify } from './stable-key-DePnevIy.mjs';
|
|
4
4
|
import { useLunora } from './LunoraContext-C59PzHhN.mjs';
|
|
5
5
|
|
|
6
|
+
const toBase64 = (bytes) => {
|
|
7
|
+
let binary = "";
|
|
8
|
+
const chunk = 32768;
|
|
9
|
+
for (let index = 0; index < bytes.length; index += chunk) {
|
|
10
|
+
binary += String.fromCharCode(...bytes.subarray(index, index + chunk));
|
|
11
|
+
}
|
|
12
|
+
return btoa(binary);
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const TAG = "$lunora.wire$";
|
|
16
|
+
const MAX_DEPTH = 64;
|
|
17
|
+
const encodeWire = (value, depth = 0) => {
|
|
18
|
+
if (depth > MAX_DEPTH) {
|
|
19
|
+
throw new RangeError(`wire-codec: value nesting exceeds the ${MAX_DEPTH}-level limit`);
|
|
20
|
+
}
|
|
21
|
+
if (value === void 0) {
|
|
22
|
+
return [TAG, "undefined"];
|
|
23
|
+
}
|
|
24
|
+
if (value === null) {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
const kind = typeof value;
|
|
28
|
+
if (kind === "bigint") {
|
|
29
|
+
return [TAG, "bigint", value.toString()];
|
|
30
|
+
}
|
|
31
|
+
if (kind === "number") {
|
|
32
|
+
const numeric = value;
|
|
33
|
+
if (Number.isNaN(numeric)) {
|
|
34
|
+
return [TAG, "nan"];
|
|
35
|
+
}
|
|
36
|
+
if (numeric === Infinity) {
|
|
37
|
+
return [TAG, "inf"];
|
|
38
|
+
}
|
|
39
|
+
if (numeric === -Infinity) {
|
|
40
|
+
return [TAG, "-inf"];
|
|
41
|
+
}
|
|
42
|
+
return numeric;
|
|
43
|
+
}
|
|
44
|
+
if (kind !== "object") {
|
|
45
|
+
return value;
|
|
46
|
+
}
|
|
47
|
+
if (value instanceof Date) {
|
|
48
|
+
return [TAG, "date", encodeWire(value.getTime(), depth + 1)];
|
|
49
|
+
}
|
|
50
|
+
if (value instanceof Error) {
|
|
51
|
+
const error = value;
|
|
52
|
+
const properties = {};
|
|
53
|
+
for (const key of Object.keys(error)) {
|
|
54
|
+
if (error[key] !== void 0) {
|
|
55
|
+
properties[key] = encodeWire(error[key], depth + 1);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
const encodedError = [TAG, "error", error.name, error.message, properties];
|
|
59
|
+
if (error.cause !== void 0) {
|
|
60
|
+
encodedError.push(encodeWire(error.cause, depth + 1));
|
|
61
|
+
}
|
|
62
|
+
return encodedError;
|
|
63
|
+
}
|
|
64
|
+
if (value instanceof URL) {
|
|
65
|
+
return [TAG, "url", value.href];
|
|
66
|
+
}
|
|
67
|
+
if (value instanceof Map) {
|
|
68
|
+
return [TAG, "map", [...value.entries()].map(([k, v]) => [encodeWire(k, depth + 1), encodeWire(v, depth + 1)])];
|
|
69
|
+
}
|
|
70
|
+
if (value instanceof Set) {
|
|
71
|
+
return [TAG, "set", [...value].map((item) => encodeWire(item, depth + 1))];
|
|
72
|
+
}
|
|
73
|
+
if (value instanceof ArrayBuffer) {
|
|
74
|
+
return [TAG, "bytes", toBase64(new Uint8Array(value)), "ArrayBuffer"];
|
|
75
|
+
}
|
|
76
|
+
if (ArrayBuffer.isView(value)) {
|
|
77
|
+
const view = value;
|
|
78
|
+
const ctorName = view.constructor.name;
|
|
79
|
+
const bytes = new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
|
|
80
|
+
return ctorName === "Uint8Array" ? [TAG, "bytes", toBase64(bytes)] : [TAG, "bytes", toBase64(bytes), ctorName];
|
|
81
|
+
}
|
|
82
|
+
if (Array.isArray(value)) {
|
|
83
|
+
const encoded = value.map((item) => encodeWire(item, depth + 1));
|
|
84
|
+
return encoded.length > 0 && encoded[0] === TAG ? [TAG, "arr", encoded] : encoded;
|
|
85
|
+
}
|
|
86
|
+
const proto = Object.getPrototypeOf(value);
|
|
87
|
+
if (proto !== null && proto !== Object.prototype) {
|
|
88
|
+
const name = value.constructor?.name ?? "value";
|
|
89
|
+
throw new TypeError(`wire-codec: cannot encode a ${name} over the Lunora wire — only plain objects, arrays, and the supported built-ins (Date, Error, URL, Map, Set, ArrayBuffer/typed arrays, bigint) round-trip`);
|
|
90
|
+
}
|
|
91
|
+
const source = value;
|
|
92
|
+
const result = {};
|
|
93
|
+
for (const key of Object.keys(source)) {
|
|
94
|
+
const field = source[key];
|
|
95
|
+
if (field !== void 0) {
|
|
96
|
+
result[key] = encodeWire(field, depth + 1);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return result;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
const stableWireKey = (value) => stableStringify(encodeWire(value));
|
|
103
|
+
|
|
6
104
|
const buildPageArgs = (page, baseArgs) => {
|
|
7
105
|
return {
|
|
8
106
|
...baseArgs,
|
|
@@ -13,7 +111,7 @@ const buildPageArgs = (page, baseArgs) => {
|
|
|
13
111
|
}
|
|
14
112
|
};
|
|
15
113
|
};
|
|
16
|
-
const buildPageKey = (functionPath, pageArgs) => `${functionPath}::${
|
|
114
|
+
const buildPageKey = (functionPath, pageArgs) => `${functionPath}::${stableWireKey(pageArgs)}`;
|
|
17
115
|
const createPaginatedCore = (function_, args, options) => {
|
|
18
116
|
const client = useLunora();
|
|
19
117
|
const {
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { createSignal, createEffect, on, onCleanup } from 'solid-js';
|
|
2
|
+
import { useLunora } from './LunoraContext-C59PzHhN.mjs';
|
|
3
|
+
|
|
4
|
+
const createStream = (function_, args, options = {}) => {
|
|
5
|
+
const client = useLunora();
|
|
6
|
+
const [chunks, setChunks] = createSignal([]);
|
|
7
|
+
const [error, setError] = createSignal(void 0);
|
|
8
|
+
const [status, setStatus] = createSignal("idle");
|
|
9
|
+
const resolveArgs = typeof args === "function" ? args : () => args;
|
|
10
|
+
let cancelCurrent;
|
|
11
|
+
const cancel = () => {
|
|
12
|
+
cancelCurrent?.();
|
|
13
|
+
};
|
|
14
|
+
createEffect(on(resolveArgs, (currentArgs) => {
|
|
15
|
+
setChunks(() => []);
|
|
16
|
+
setError(() => void 0);
|
|
17
|
+
if (currentArgs === "skip") {
|
|
18
|
+
setStatus("idle");
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
setStatus("streaming");
|
|
22
|
+
let active = true;
|
|
23
|
+
const iterable = client.stream(function_, currentArgs, {
|
|
24
|
+
maxBuffer: options.maxBuffer,
|
|
25
|
+
shardKey: options.shardKey
|
|
26
|
+
});
|
|
27
|
+
const cancelIterable = () => {
|
|
28
|
+
iterable.cancel();
|
|
29
|
+
};
|
|
30
|
+
cancelCurrent = cancelIterable;
|
|
31
|
+
(async () => {
|
|
32
|
+
try {
|
|
33
|
+
for await (const chunk of iterable) {
|
|
34
|
+
if (!active) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
setChunks((previous) => [...previous, chunk]);
|
|
38
|
+
}
|
|
39
|
+
if (active) {
|
|
40
|
+
setStatus("complete");
|
|
41
|
+
}
|
|
42
|
+
} catch (streamError) {
|
|
43
|
+
if (!active) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
setError(() => streamError instanceof Error ? streamError : new Error(String(streamError)));
|
|
47
|
+
setStatus("error");
|
|
48
|
+
}
|
|
49
|
+
})().catch(() => {
|
|
50
|
+
});
|
|
51
|
+
onCleanup(() => {
|
|
52
|
+
active = false;
|
|
53
|
+
cancelIterable();
|
|
54
|
+
if (cancelCurrent === cancelIterable) {
|
|
55
|
+
cancelCurrent = void 0;
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}));
|
|
59
|
+
onCleanup(() => {
|
|
60
|
+
cancel();
|
|
61
|
+
});
|
|
62
|
+
return {
|
|
63
|
+
cancel,
|
|
64
|
+
chunks,
|
|
65
|
+
error,
|
|
66
|
+
status
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
export { createStream };
|