@arcote.tech/arc-chat 0.4.9 → 0.5.1
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/package.json +6 -6
- package/src/aggregates/message.ts +68 -78
- package/src/chat-builder.ts +75 -51
- package/src/index.ts +22 -4
- package/src/listeners/ai-generation-listener.ts +293 -0
- package/src/react/index.ts +4 -0
- package/src/react/use-chat.ts +260 -0
- package/src/routes/chat-stream-route.ts +31 -0
- package/src/routes/tool-results-route.ts +49 -0
- package/src/streaming/stream-registry.ts +146 -0
- package/src/aggregates/conversation.ts +0 -151
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import type { ChatStreamEvent, ToolResult } from "@arcote.tech/arc-ai";
|
|
2
|
+
|
|
3
|
+
// ─── Stream Session ─────────────────────────────────────────────
|
|
4
|
+
|
|
5
|
+
export interface StreamSession {
|
|
6
|
+
readonly sessionId: string;
|
|
7
|
+
push(event: ChatStreamEvent): void;
|
|
8
|
+
createReadableStream(): ReadableStream<Uint8Array>;
|
|
9
|
+
waitForClientToolResults(timeoutMs?: number): Promise<ToolResult[]>;
|
|
10
|
+
resolveClientToolResults(results: ToolResult[]): void;
|
|
11
|
+
close(): void;
|
|
12
|
+
isClosed(): boolean;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// ─── Registry ───────────────────────────────────────────────────
|
|
16
|
+
|
|
17
|
+
const sessions = new Map<string, StreamSession>();
|
|
18
|
+
|
|
19
|
+
export function createStreamSession(sessionId: string): StreamSession {
|
|
20
|
+
const existing = sessions.get(sessionId);
|
|
21
|
+
if (existing) return existing;
|
|
22
|
+
|
|
23
|
+
let controller: ReadableStreamDefaultController<Uint8Array> | null = null;
|
|
24
|
+
let closed = false;
|
|
25
|
+
const encoder = new TextEncoder();
|
|
26
|
+
const buffer: ChatStreamEvent[] = [];
|
|
27
|
+
|
|
28
|
+
// Client tool results coordination
|
|
29
|
+
let toolResultsResolve: ((results: ToolResult[]) => void) | null = null;
|
|
30
|
+
|
|
31
|
+
// Keep-alive interval
|
|
32
|
+
let keepAliveInterval: ReturnType<typeof setInterval> | null = null;
|
|
33
|
+
|
|
34
|
+
function encode(event: ChatStreamEvent): Uint8Array {
|
|
35
|
+
return encoder.encode(`data: ${JSON.stringify(event)}\n\n`);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function startKeepAlive() {
|
|
39
|
+
keepAliveInterval = setInterval(() => {
|
|
40
|
+
if (controller && !closed) {
|
|
41
|
+
try {
|
|
42
|
+
controller.enqueue(encoder.encode(`: ping\n\n`));
|
|
43
|
+
} catch {
|
|
44
|
+
// Controller closed, clean up
|
|
45
|
+
cleanup();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}, 5000);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function cleanup() {
|
|
52
|
+
if (keepAliveInterval) {
|
|
53
|
+
clearInterval(keepAliveInterval);
|
|
54
|
+
keepAliveInterval = null;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const session: StreamSession = {
|
|
59
|
+
sessionId,
|
|
60
|
+
|
|
61
|
+
push(event: ChatStreamEvent) {
|
|
62
|
+
if (closed) return;
|
|
63
|
+
buffer.push(event);
|
|
64
|
+
if (controller) {
|
|
65
|
+
try {
|
|
66
|
+
controller.enqueue(encode(event));
|
|
67
|
+
} catch {
|
|
68
|
+
// Client disconnected, ignore
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
|
|
73
|
+
createReadableStream(): ReadableStream<Uint8Array> {
|
|
74
|
+
return new ReadableStream<Uint8Array>({
|
|
75
|
+
start(ctrl) {
|
|
76
|
+
controller = ctrl;
|
|
77
|
+
|
|
78
|
+
// Replay buffered events for late-connecting clients
|
|
79
|
+
for (const event of buffer) {
|
|
80
|
+
ctrl.enqueue(encode(event));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
startKeepAlive();
|
|
84
|
+
},
|
|
85
|
+
cancel() {
|
|
86
|
+
controller = null;
|
|
87
|
+
cleanup();
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
},
|
|
91
|
+
|
|
92
|
+
waitForClientToolResults(timeoutMs = 60_000): Promise<ToolResult[]> {
|
|
93
|
+
return new Promise<ToolResult[]>((resolve, reject) => {
|
|
94
|
+
const timer = setTimeout(() => {
|
|
95
|
+
toolResultsResolve = null;
|
|
96
|
+
reject(new Error("Client tool results timeout"));
|
|
97
|
+
}, timeoutMs);
|
|
98
|
+
|
|
99
|
+
toolResultsResolve = (results) => {
|
|
100
|
+
clearTimeout(timer);
|
|
101
|
+
toolResultsResolve = null;
|
|
102
|
+
resolve(results);
|
|
103
|
+
};
|
|
104
|
+
});
|
|
105
|
+
},
|
|
106
|
+
|
|
107
|
+
resolveClientToolResults(results: ToolResult[]) {
|
|
108
|
+
if (toolResultsResolve) {
|
|
109
|
+
toolResultsResolve(results);
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
|
|
113
|
+
close() {
|
|
114
|
+
if (closed) return;
|
|
115
|
+
closed = true;
|
|
116
|
+
cleanup();
|
|
117
|
+
if (controller) {
|
|
118
|
+
try {
|
|
119
|
+
controller.close();
|
|
120
|
+
} catch {
|
|
121
|
+
// Already closed
|
|
122
|
+
}
|
|
123
|
+
controller = null;
|
|
124
|
+
}
|
|
125
|
+
},
|
|
126
|
+
|
|
127
|
+
isClosed() {
|
|
128
|
+
return closed;
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
sessions.set(sessionId, session);
|
|
133
|
+
return session;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function getStreamSession(sessionId: string): StreamSession | undefined {
|
|
137
|
+
return sessions.get(sessionId);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export function deleteStreamSession(sessionId: string): void {
|
|
141
|
+
const session = sessions.get(sessionId);
|
|
142
|
+
if (session) {
|
|
143
|
+
session.close();
|
|
144
|
+
sessions.delete(sessionId);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
@@ -1,151 +0,0 @@
|
|
|
1
|
-
/// <reference path="../arc.d.ts" />
|
|
2
|
-
import {
|
|
3
|
-
aggregate,
|
|
4
|
-
date,
|
|
5
|
-
id,
|
|
6
|
-
number,
|
|
7
|
-
string,
|
|
8
|
-
type ArcId,
|
|
9
|
-
} from "@arcote.tech/arc";
|
|
10
|
-
import type { Token } from "@arcote.tech/arc-auth";
|
|
11
|
-
|
|
12
|
-
// ─── ID ──────────────────────────────────────────────────────────
|
|
13
|
-
|
|
14
|
-
export const createConversationId = <const Name extends string>(data: {
|
|
15
|
-
name: Name;
|
|
16
|
-
}) => id(`${data.name}Conversation`);
|
|
17
|
-
|
|
18
|
-
export type ConversationId<Name extends string = string> = ReturnType<
|
|
19
|
-
typeof createConversationId<Name>
|
|
20
|
-
>;
|
|
21
|
-
|
|
22
|
-
// ─── Aggregate ───────────────────────────────────────────────────
|
|
23
|
-
|
|
24
|
-
export type ConversationAggregateData = {
|
|
25
|
-
name: string;
|
|
26
|
-
conversationId: ArcId<any>;
|
|
27
|
-
accountId: ArcId<any>;
|
|
28
|
-
userToken: Token;
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
export const createConversationAggregate = <
|
|
32
|
-
const Data extends ConversationAggregateData,
|
|
33
|
-
>(
|
|
34
|
-
data: Data,
|
|
35
|
-
) => {
|
|
36
|
-
const { conversationId, accountId, userToken } = data;
|
|
37
|
-
|
|
38
|
-
return aggregate(
|
|
39
|
-
`${data.name}Conversations`,
|
|
40
|
-
conversationId,
|
|
41
|
-
{
|
|
42
|
-
accountId,
|
|
43
|
-
title: string(),
|
|
44
|
-
model: string(),
|
|
45
|
-
createdAt: date(),
|
|
46
|
-
lastMessageAt: date().optional(),
|
|
47
|
-
messageCount: number(),
|
|
48
|
-
},
|
|
49
|
-
)
|
|
50
|
-
.publicEvent(
|
|
51
|
-
"conversationCreated",
|
|
52
|
-
{
|
|
53
|
-
conversationId,
|
|
54
|
-
accountId,
|
|
55
|
-
title: string(),
|
|
56
|
-
model: string(),
|
|
57
|
-
},
|
|
58
|
-
async (ctx, event) => {
|
|
59
|
-
const p = event.payload;
|
|
60
|
-
await ctx.set(p.conversationId, {
|
|
61
|
-
accountId: p.accountId,
|
|
62
|
-
title: p.title,
|
|
63
|
-
model: p.model,
|
|
64
|
-
createdAt: event.createdAt,
|
|
65
|
-
messageCount: 0,
|
|
66
|
-
});
|
|
67
|
-
},
|
|
68
|
-
)
|
|
69
|
-
|
|
70
|
-
.publicEvent(
|
|
71
|
-
"conversationRenamed",
|
|
72
|
-
{
|
|
73
|
-
conversationId,
|
|
74
|
-
title: string(),
|
|
75
|
-
},
|
|
76
|
-
async (ctx, event) => {
|
|
77
|
-
await ctx.modify(event.payload.conversationId, {
|
|
78
|
-
title: event.payload.title,
|
|
79
|
-
});
|
|
80
|
-
},
|
|
81
|
-
)
|
|
82
|
-
|
|
83
|
-
.publicEvent(
|
|
84
|
-
"conversationUpdated",
|
|
85
|
-
{
|
|
86
|
-
conversationId,
|
|
87
|
-
messageCount: number(),
|
|
88
|
-
},
|
|
89
|
-
async (ctx, event) => {
|
|
90
|
-
await ctx.modify(event.payload.conversationId, {
|
|
91
|
-
lastMessageAt: event.createdAt,
|
|
92
|
-
messageCount: event.payload.messageCount,
|
|
93
|
-
});
|
|
94
|
-
},
|
|
95
|
-
)
|
|
96
|
-
|
|
97
|
-
.mutateMethod(
|
|
98
|
-
"create",
|
|
99
|
-
{
|
|
100
|
-
params: {
|
|
101
|
-
title: string().optional(),
|
|
102
|
-
model: string(),
|
|
103
|
-
},
|
|
104
|
-
},
|
|
105
|
-
ONLY_SERVER &&
|
|
106
|
-
(async (ctx, params) => {
|
|
107
|
-
const cId = conversationId.generate();
|
|
108
|
-
const aId = ctx.$auth.params.accountId;
|
|
109
|
-
|
|
110
|
-
await ctx.conversationCreated.emit({
|
|
111
|
-
conversationId: cId,
|
|
112
|
-
accountId: accountId.parse(aId),
|
|
113
|
-
title: params.title ?? "New conversation",
|
|
114
|
-
model: params.model,
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
return { conversationId: cId };
|
|
118
|
-
}),
|
|
119
|
-
)
|
|
120
|
-
|
|
121
|
-
.mutateMethod(
|
|
122
|
-
"rename",
|
|
123
|
-
{
|
|
124
|
-
params: {
|
|
125
|
-
_id: conversationId,
|
|
126
|
-
title: string().minLength(1),
|
|
127
|
-
},
|
|
128
|
-
},
|
|
129
|
-
ONLY_SERVER &&
|
|
130
|
-
(async (ctx, params) => {
|
|
131
|
-
await ctx.conversationRenamed.emit({
|
|
132
|
-
conversationId: params._id,
|
|
133
|
-
title: params.title,
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
return { success: true };
|
|
137
|
-
}),
|
|
138
|
-
)
|
|
139
|
-
|
|
140
|
-
.clientQuery("getAll", async (ctx) => ctx.$query.find({}))
|
|
141
|
-
.clientQuery(
|
|
142
|
-
"getById",
|
|
143
|
-
async (ctx, params: { _id: string }) =>
|
|
144
|
-
ctx.$query.findOne({ _id: params._id }),
|
|
145
|
-
)
|
|
146
|
-
|
|
147
|
-
.protectBy(userToken, (p) => ({ accountId: p.accountId }))
|
|
148
|
-
;
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
export type ConversationAggregate = ReturnType<typeof createConversationAggregate>;
|