@lcas58/esmi-api-types 1.0.23 → 1.0.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.
|
@@ -6,3 +6,4 @@ export declare const discover: AppRouteHandler<DiscoverRoute>;
|
|
|
6
6
|
export declare const discoverExternal: AppRouteHandler<DiscoverExternalRoute>;
|
|
7
7
|
export declare const saveExternal: AppRouteHandler<SaveExternalRoute>;
|
|
8
8
|
export declare const chat: AppRouteHandler<ChatRoute>;
|
|
9
|
+
export declare const chatStream: (c: Parameters<AppRouteHandler<ChatRoute>>[0]) => Promise<Response>;
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { and, eq, gte, ilike, inArray, isNotNull, lte } from "drizzle-orm";
|
|
2
|
+
import { streamSSE } from "hono/streaming";
|
|
2
3
|
import * as HttpStatusCodes from "stoker/http-status-codes";
|
|
3
4
|
import * as HttpStatusPhrases from "stoker/http-status-phrases";
|
|
4
|
-
import { agentEventsToRaw, runChatAgent, runExternalDiscoveryAgent } from "../../agent/runner.js";
|
|
5
|
+
import { agentEventsToRaw, runChatAgent, runExternalDiscoveryAgent, runStreamingChatAgent } from "../../agent/runner.js";
|
|
5
6
|
import { getKnownExternalUrls, saveEvents, searchLocalEvents } from "../../agent/tools.js";
|
|
6
7
|
import { createDb } from "../../db/index.js";
|
|
7
8
|
import { event, location } from "../../db/schema/index.js";
|
|
8
9
|
import { buildCacheKey, buildSeenKey, getCachedDiscovery, markUrlsSeen, setCachedDiscovery } from "../../lib/discovery-cache.js";
|
|
10
|
+
import { chatRequestSchema } from "./events.routes.js";
|
|
9
11
|
export const list = async (c) => {
|
|
10
12
|
const { db } = createDb(c.env);
|
|
11
13
|
const query = c.req.valid("query");
|
|
@@ -210,3 +212,34 @@ export const chat = async (c) => {
|
|
|
210
212
|
});
|
|
211
213
|
return c.json(result, HttpStatusCodes.OK);
|
|
212
214
|
};
|
|
215
|
+
export const chatStream = async (c) => {
|
|
216
|
+
const rawBody = await c.req.json();
|
|
217
|
+
const parsed = chatRequestSchema.safeParse(rawBody);
|
|
218
|
+
if (!parsed.success) {
|
|
219
|
+
return c.json({ message: "Invalid request body" }, HttpStatusCodes.UNPROCESSABLE_ENTITY);
|
|
220
|
+
}
|
|
221
|
+
const user = c.get("user");
|
|
222
|
+
if (!user) {
|
|
223
|
+
return c.json({ message: "Unauthorized" }, HttpStatusCodes.UNAUTHORIZED);
|
|
224
|
+
}
|
|
225
|
+
const { db } = createDb(c.env);
|
|
226
|
+
const body = parsed.data;
|
|
227
|
+
return streamSSE(c, async (stream) => {
|
|
228
|
+
const writer = async (event) => {
|
|
229
|
+
await stream.writeSSE({ data: JSON.stringify(event) });
|
|
230
|
+
};
|
|
231
|
+
try {
|
|
232
|
+
await runStreamingChatAgent(c.env, db, {
|
|
233
|
+
message: body.message,
|
|
234
|
+
history: body.history,
|
|
235
|
+
city: body.city,
|
|
236
|
+
state: body.state,
|
|
237
|
+
}, writer);
|
|
238
|
+
}
|
|
239
|
+
catch (err) {
|
|
240
|
+
console.error("[chatStream] error:", err);
|
|
241
|
+
await stream.writeSSE({ data: JSON.stringify({ type: "error", message: "Stream failed" }) });
|
|
242
|
+
await stream.writeSSE({ data: JSON.stringify({ type: "done" }) });
|
|
243
|
+
}
|
|
244
|
+
});
|
|
245
|
+
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { createRouter } from "../../lib/create-app.js";
|
|
2
|
+
import { isAuth } from "../../middlewares/is-auth.js";
|
|
2
3
|
import * as handlers from "./events.handlers.js";
|
|
3
4
|
import * as routes from "./events.routes.js";
|
|
4
5
|
const router = createRouter()
|
|
@@ -8,4 +9,6 @@ const router = createRouter()
|
|
|
8
9
|
.openapi(routes.discoverExternal, handlers.discoverExternal)
|
|
9
10
|
.openapi(routes.saveExternal, handlers.saveExternal)
|
|
10
11
|
.openapi(routes.chat, handlers.chat);
|
|
12
|
+
// SSE streaming route (not in OpenAPI spec — SSE responses can't be modeled in JSON schema)
|
|
13
|
+
router.post("/events/chat/stream", isAuth(), handlers.chatStream);
|
|
11
14
|
export default router;
|