Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,32 @@ http.route({
method: "POST",
handler: streamChat,
});

// Handle CORS preflight requests so browsers will allow the POST above when
// your app is served from a different origin than your Convex deployment.
http.route({
path: "/chat-stream",
method: "OPTIONS",
handler: httpAction(async (_, request) => {
const headers = request.headers;
if (
headers.get("Origin") !== null &&
headers.get("Access-Control-Request-Method") !== null &&
headers.get("Access-Control-Request-Headers") !== null
) {
return new Response(null, {
headers: new Headers({
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "POST",
"Access-Control-Allow-Headers": "Content-Type, Digest, Authorization",
"Access-Control-Max-Age": "86400",
}),
});
} else {
return new Response();
}
}),
});
```

Finally, in your app, you can now create chats and them subscribe to them via
Expand Down
6 changes: 6 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import pluginJs from "@eslint/js";
import tseslint from "typescript-eslint";
import reactHooks from "eslint-plugin-react-hooks";
import reactRefresh from "eslint-plugin-react-refresh";
import convexPlugin from "@convex-dev/eslint-plugin";

export default [
{
Expand Down Expand Up @@ -36,7 +37,12 @@ export default [
languageOptions: {
globals: globals.worker,
},
plugins: {
"@convex-dev": convexPlugin,
},
rules: {
...convexPlugin.configs.recommended[0].rules,

"@typescript-eslint/no-floating-promises": "error",
"@typescript-eslint/no-explicit-any": "off",
"no-unused-vars": "off",
Expand Down
2 changes: 1 addition & 1 deletion example/convex/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const clearMessages = mutation({
args: {},
handler: async (ctx) => {
const chats = await ctx.db.query("userMessages").collect();
await Promise.all(chats.map((chat) => ctx.db.delete(chat._id)));
await Promise.all(chats.map((chat) => ctx.db.delete("userMessages", chat._id)));
},
});

Expand Down
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"react-dom": "~18.3.1 || ^19.0.0"
},
"devDependencies": {
"@convex-dev/eslint-plugin": "^2.0.0",
"@edge-runtime/vm": "5.0.0",
"@eslint/eslintrc": "3.3.5",
"@eslint/js": "9.39.4",
Expand Down
22 changes: 11 additions & 11 deletions src/component/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ export const addChunk = mutation({
final: v.boolean(),
},
handler: async (ctx, args) => {
const stream = await ctx.db.get(args.streamId);
const stream = await ctx.db.get("streams", args.streamId);
if (!stream) {
throw new Error("Stream not found");
}
if (stream.status === "pending") {
await ctx.db.patch(args.streamId, {
await ctx.db.patch("streams", args.streamId, {
status: "streaming",
});
} else if (stream.status !== "streaming") {
Expand All @@ -41,7 +41,7 @@ export const addChunk = mutation({
text: args.text,
});
if (args.final) {
await ctx.db.patch(args.streamId, {
await ctx.db.patch("streams", args.streamId, {
status: "done",
});
}
Expand All @@ -62,7 +62,7 @@ export const setStreamStatus = mutation({
),
},
handler: async (ctx, args) => {
const stream = await ctx.db.get(args.streamId);
const stream = await ctx.db.get("streams", args.streamId);
if (!stream) {
throw new Error("Stream not found");
}
Expand All @@ -73,7 +73,7 @@ export const setStreamStatus = mutation({
);
return;
}
await ctx.db.patch(args.streamId, {
await ctx.db.patch("streams", args.streamId, {
status: args.status,
});
},
Expand All @@ -86,7 +86,7 @@ export const getStreamStatus = query({
},
returns: streamStatusValidator,
handler: async (ctx, args) => {
const stream = await ctx.db.get(args.streamId);
const stream = await ctx.db.get("streams", args.streamId);
return stream?.status ?? "error";
},
});
Expand All @@ -102,7 +102,7 @@ export const getStreamText = query({
status: streamStatusValidator,
}),
handler: async (ctx, args) => {
const stream = await ctx.db.get(args.streamId);
const stream = await ctx.db.get("streams", args.streamId);
if (!stream) {
throw new Error("Stream not found");
}
Expand Down Expand Up @@ -133,11 +133,11 @@ export const deleteStream = mutation({
},
returns: v.null(),
handler: async (ctx, args) => {
const stream = await ctx.db.get(args.streamId);
const stream = await ctx.db.get("streams", args.streamId);
if (!stream) {
throw new Error(`Stream ${args.streamId} not found`);
}
await ctx.db.delete(args.streamId);
await ctx.db.delete("streams", args.streamId);
await ctx.scheduler.runAfter(0, internal.lib._deleteChunksPage, {
streamId: args.streamId,
cursor: null,
Expand All @@ -158,7 +158,7 @@ export const _deleteChunksPage = internalMutation({
.withIndex("byStream", (q) => q.eq("streamId", args.streamId))
.paginate({ cursor: args.cursor, numItems: DELETE_BATCH_SIZE });

await Promise.all(result.page.map((chunk) => ctx.db.delete(chunk._id)));
await Promise.all(result.page.map((chunk) => ctx.db.delete("chunks", chunk._id)));

if (!result.isDone) {
await ctx.scheduler.runAfter(0, internal.lib._deleteChunksPage, {
Expand Down Expand Up @@ -187,7 +187,7 @@ export const cleanupExpiredStreams = internalMutation({
for (const stream of [...pendingStreams, ...streamingStreams]) {
if (now - stream._creationTime > EXPIRATION_TIME) {
console.log("Cleaning up expired stream", stream._id);
await ctx.db.patch(stream._id, {
await ctx.db.patch("streams", stream._id, {
status: "timeout",
});
}
Expand Down
Loading