Skip to content

Commit 99c6575

Browse files
committed
update test config
1 parent 6b2ea36 commit 99c6575

File tree

11 files changed

+350
-295
lines changed

11 files changed

+350
-295
lines changed

.cursor/rules/convex_rules.mdc

Lines changed: 195 additions & 179 deletions
Large diffs are not rendered by default.

example/convex/messages.ts

Lines changed: 72 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,72 @@
1-
import { query, mutation, internalQuery } from "./_generated/server";
2-
import { StreamId } from "@convex-dev/persistent-text-streaming";
3-
import { v } from "convex/values";
4-
import { streamingComponent } from "./streaming";
5-
6-
export const listMessages = query({
7-
args: {},
8-
handler: async (ctx) => {
9-
return await ctx.db.query("userMessages").collect();
10-
},
11-
});
12-
13-
export const clearMessages = mutation({
14-
args: {},
15-
handler: async (ctx) => {
16-
const chats = await ctx.db.query("userMessages").collect();
17-
await Promise.all(chats.map((chat) => ctx.db.delete(chat._id)));
18-
},
19-
});
20-
21-
export const sendMessage = mutation({
22-
args: {
23-
prompt: v.string(),
24-
},
25-
handler: async (ctx, args) => {
26-
const responseStreamId = await streamingComponent.createStream(ctx);
27-
const chatId = await ctx.db.insert("userMessages", {
28-
prompt: args.prompt,
29-
responseStreamId,
30-
});
31-
return chatId;
32-
},
33-
});
34-
35-
export const getHistory = internalQuery({
36-
args: {},
37-
handler: async (ctx) => {
38-
// Grab all the user messages
39-
const allMessages = await ctx.db.query("userMessages").collect();
40-
41-
// Lets join the user messages with the assistant messages
42-
const joinedResponses = await Promise.all(
43-
allMessages.map(async (userMessage) => {
44-
return {
45-
userMessage,
46-
responseMessage: await streamingComponent.getStreamBody(
47-
ctx,
48-
userMessage.responseStreamId as StreamId
49-
),
50-
};
51-
})
52-
);
53-
54-
return joinedResponses.flatMap((joined) => {
55-
const user = {
56-
role: "user" as const,
57-
content: joined.userMessage.prompt,
58-
};
59-
60-
const assistant = {
61-
role: "assistant" as const,
62-
content: joined.responseMessage.text,
63-
};
64-
65-
// If the assistant message is empty, its probably because we have not
66-
// started streaming yet so lets not include it in the history
67-
if (!assistant.content) return [user];
68-
69-
return [user, assistant];
70-
});
71-
},
72-
});
1+
import { query, mutation, internalQuery } from "./_generated/server";
2+
import { StreamId } from "@convex-dev/persistent-text-streaming";
3+
import { v } from "convex/values";
4+
import { streamingComponent } from "./streaming";
5+
6+
export const listMessages = query({
7+
args: {},
8+
handler: async (ctx) => {
9+
return await ctx.db.query("userMessages").collect();
10+
},
11+
});
12+
13+
export const clearMessages = mutation({
14+
args: {},
15+
handler: async (ctx) => {
16+
const chats = await ctx.db.query("userMessages").collect();
17+
await Promise.all(chats.map((chat) => ctx.db.delete(chat._id)));
18+
},
19+
});
20+
21+
export const sendMessage = mutation({
22+
args: {
23+
prompt: v.string(),
24+
},
25+
handler: async (ctx, args) => {
26+
const responseStreamId = await streamingComponent.createStream(ctx);
27+
const chatId = await ctx.db.insert("userMessages", {
28+
prompt: args.prompt,
29+
responseStreamId,
30+
});
31+
return chatId;
32+
},
33+
});
34+
35+
export const getHistory = internalQuery({
36+
args: {},
37+
handler: async (ctx) => {
38+
// Grab all the user messages
39+
const allMessages = await ctx.db.query("userMessages").collect();
40+
41+
// Lets join the user messages with the assistant messages
42+
const joinedResponses = await Promise.all(
43+
allMessages.map(async (userMessage) => {
44+
return {
45+
userMessage,
46+
responseMessage: await streamingComponent.getStreamBody(
47+
ctx,
48+
userMessage.responseStreamId as StreamId
49+
),
50+
};
51+
})
52+
);
53+
54+
return joinedResponses.flatMap((joined) => {
55+
const user = {
56+
role: "user" as const,
57+
content: joined.userMessage.prompt,
58+
};
59+
60+
const assistant = {
61+
role: "assistant" as const,
62+
content: joined.responseMessage.text,
63+
};
64+
65+
// If the assistant message is empty, its probably because we have not
66+
// started streaming yet so lets not include it in the history
67+
if (!assistant.content) return [user];
68+
69+
return [user, assistant];
70+
});
71+
},
72+
});

example/convex/streaming.ts

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
import {
2-
PersistentTextStreaming,
3-
StreamId,
4-
StreamIdValidator,
5-
} from "@convex-dev/persistent-text-streaming";
6-
import { components } from "./_generated/api";
7-
import { query } from "./_generated/server";
8-
9-
export const streamingComponent = new PersistentTextStreaming(
10-
components.persistentTextStreaming
11-
);
12-
13-
export const getStreamBody = query({
14-
args: {
15-
streamId: StreamIdValidator,
16-
},
17-
handler: async (ctx, args) => {
18-
return await streamingComponent.getStreamBody(
19-
ctx,
20-
args.streamId as StreamId
21-
);
22-
},
23-
});
1+
import {
2+
PersistentTextStreaming,
3+
StreamId,
4+
StreamIdValidator,
5+
} from "@convex-dev/persistent-text-streaming";
6+
import { components } from "./_generated/api";
7+
import { query } from "./_generated/server";
8+
9+
export const streamingComponent = new PersistentTextStreaming(
10+
components.persistentTextStreaming
11+
);
12+
13+
export const getStreamBody = query({
14+
args: {
15+
streamId: StreamIdValidator,
16+
},
17+
handler: async (ctx, args) => {
18+
return await streamingComponent.getStreamBody(
19+
ctx,
20+
args.streamId as StreamId
21+
);
22+
},
23+
});

example/convex/tsconfig.json

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,5 @@
11
{
2-
"compilerOptions": {
3-
"allowJs": true,
4-
"strict": true,
5-
"skipLibCheck": true,
6-
"target": "ESNext",
7-
"lib": ["ES2021", "dom", "DOM.Iterable", "ESNext.Array"],
8-
"forceConsistentCasingInFileNames": true,
9-
"allowSyntheticDefaultImports": true,
10-
"verbatimModuleSyntax": true,
11-
"module": "ESNext",
12-
"moduleResolution": "Bundler",
13-
"isolatedModules": true,
14-
"noEmit": true,
15-
"jsx": "react-jsx"
16-
},
17-
"include": [".*"],
18-
"exclude": ["./_generated"]
2+
"extends": "../tsconfig.json",
3+
"include": ["."],
4+
"exclude": ["_generated"]
195
}

example/tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
"module": "ESNext",
1010
"moduleResolution": "Bundler",
1111
"resolveJsonModule": true,
12-
"exactOptionalPropertyTypes": true,
1312
"isolatedModules": true,
1413
"jsx": "react-jsx",
1514
"baseUrl": ".",

example/vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import tailwindcss from "@tailwindcss/vite";
44

55
// https://vitejs.dev/config/
66
export default defineConfig({
7+
envDir: "../",
78
plugins: [react(), tailwindcss()],
89
resolve: {
9-
conditions: ["@convex-dev/component-source"],
1010
alias: {
1111
"@": "/src",
1212
},

package-lock.json

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"react-dom": "~18.3.1 || ^19.0.0"
6565
},
6666
"devDependencies": {
67+
"@edge-runtime/vm": "^5.0.0",
6768
"@eslint/eslintrc": "3.3.1",
6869
"@eslint/js": "9.38.0",
6970
"@tailwindcss/vite": "4.1.16",

src/client/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ export class PersistentTextStreaming {
122122
}
123123
// Create a TransformStream to handle streaming data
124124
const { readable, writable } = new TransformStream();
125-
let writer = writable.getWriter() as WritableStreamDefaultWriter<Uint8Array> | null;
125+
let writer =
126+
writable.getWriter() as WritableStreamDefaultWriter<Uint8Array> | null;
126127
const textEncoder = new TextEncoder();
127128
let pending = "";
128129

@@ -134,7 +135,9 @@ export class PersistentTextStreaming {
134135
await writer.write(textEncoder.encode(text));
135136
} catch (e) {
136137
console.error("Error writing to stream", e);
137-
console.error("Will skip writing to stream but continue database updates");
138+
console.error(
139+
"Will skip writing to stream but continue database updates"
140+
);
138141
writer = null;
139142
}
140143
}

tsconfig.test.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"include": [
4+
"src/**/*.ts",
5+
"src/**/*.tsx",
6+
"example/src/**/*.ts",
7+
"example/src/**/*.tsx",
8+
"example/convex/**/*.ts"
9+
],
10+
"exclude": [
11+
"node_modules",
12+
"dist",
13+
"**/_generated"
14+
]
15+
}

0 commit comments

Comments
 (0)