@lunora/solid 1.0.0-alpha.23 → 1.0.0-alpha.24
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 +6 -0
- 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/createStream-D9ONbGAw.mjs +70 -0
- package/dist/packem_shared/createVoiceAgent-hu59SaDl.mjs +418 -0
- package/package.json +1 -1
|
@@ -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 };
|
|
@@ -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 };
|