create-better-t-stack 3.10.0 → 3.11.0

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.
@@ -0,0 +1,17 @@
1
+ import { defineApp } from "convex/server";
2
+ {{#if (eq auth "better-auth")}}
3
+ import betterAuth from "@convex-dev/better-auth/convex.config";
4
+ {{/if}}
5
+ {{#if (includes examples "ai")}}
6
+ import agent from "@convex-dev/agent/convex.config";
7
+ {{/if}}
8
+
9
+ const app = defineApp();
10
+ {{#if (eq auth "better-auth")}}
11
+ app.use(betterAuth);
12
+ {{/if}}
13
+ {{#if (includes examples "ai")}}
14
+ app.use(agent);
15
+ {{/if}}
16
+
17
+ export default app;
@@ -0,0 +1,9 @@
1
+ import { Agent } from "@convex-dev/agent";
2
+ import { google } from "@ai-sdk/google";
3
+ import { components } from "./_generated/api";
4
+
5
+ export const chatAgent = new Agent(components.agent, {
6
+ name: "Chat Agent",
7
+ languageModel: google("gemini-2.5-flash"),
8
+ instructions: "You are a helpful AI assistant. Be concise and friendly in your responses.",
9
+ });
@@ -0,0 +1,67 @@
1
+ import {
2
+ createThread,
3
+ listUIMessages,
4
+ saveMessage,
5
+ syncStreams,
6
+ vStreamArgs,
7
+ } from "@convex-dev/agent";
8
+ import { paginationOptsValidator } from "convex/server";
9
+ import { v } from "convex/values";
10
+
11
+ import { components, internal } from "./_generated/api";
12
+ import { internalAction, mutation, query } from "./_generated/server";
13
+ import { chatAgent } from "./agent";
14
+
15
+ export const createNewThread = mutation({
16
+ args: {},
17
+ handler: async (ctx) => {
18
+ const threadId = await createThread(ctx, components.agent, {});
19
+ return threadId;
20
+ },
21
+ });
22
+
23
+ export const listMessages = query({
24
+ args: {
25
+ threadId: v.string(),
26
+ paginationOpts: paginationOptsValidator,
27
+ streamArgs: vStreamArgs,
28
+ },
29
+ handler: async (ctx, args) => {
30
+ const paginated = await listUIMessages(ctx, components.agent, args);
31
+ const streams = await syncStreams(ctx, components.agent, args);
32
+ return { ...paginated, streams };
33
+ },
34
+ });
35
+
36
+ export const sendMessage = mutation({
37
+ args: {
38
+ threadId: v.string(),
39
+ prompt: v.string(),
40
+ },
41
+ handler: async (ctx, { threadId, prompt }) => {
42
+ const { messageId } = await saveMessage(ctx, components.agent, {
43
+ threadId,
44
+ prompt,
45
+ });
46
+ await ctx.scheduler.runAfter(0, internal.chat.generateResponseAsync, {
47
+ threadId,
48
+ promptMessageId: messageId,
49
+ });
50
+ return messageId;
51
+ },
52
+ });
53
+
54
+ export const generateResponseAsync = internalAction({
55
+ args: {
56
+ threadId: v.string(),
57
+ promptMessageId: v.string(),
58
+ },
59
+ handler: async (ctx, { threadId, promptMessageId }) => {
60
+ await chatAgent.streamText(
61
+ ctx,
62
+ { threadId },
63
+ { promptMessageId },
64
+ { saveStreamDeltas: true },
65
+ );
66
+ },
67
+ });
@@ -1,3 +1,301 @@
1
+ {{#if (eq backend "convex")}}
2
+ import { Ionicons } from "@expo/vector-icons";
3
+ import {
4
+ useUIMessages,
5
+ useSmoothText,
6
+ type UIMessage,
7
+ } from "@convex-dev/agent/react";
8
+ import { api } from "@{{projectName}}/backend/convex/_generated/api";
9
+ import { useMutation } from "convex/react";
10
+ import { useRef, useEffect, useState } from "react";
11
+ import {
12
+ View,
13
+ Text,
14
+ TextInput,
15
+ TouchableOpacity,
16
+ ScrollView,
17
+ KeyboardAvoidingView,
18
+ Platform,
19
+ StyleSheet,
20
+ ActivityIndicator,
21
+ } from "react-native";
22
+
23
+ import { Container } from "@/components/container";
24
+ import { useColorScheme } from "@/lib/use-color-scheme";
25
+ import { NAV_THEME } from "@/lib/constants";
26
+
27
+ function MessageContent({
28
+ text,
29
+ isStreaming,
30
+ textColor,
31
+ }: {
32
+ text: string;
33
+ isStreaming: boolean;
34
+ textColor: string;
35
+ }) {
36
+ const [visibleText] = useSmoothText(text, {
37
+ startStreaming: isStreaming,
38
+ });
39
+ return <Text style={[styles.messageText, { color: textColor }]}>{visibleText}</Text>;
40
+ }
41
+
42
+ export default function AIScreen() {
43
+ const { colorScheme } = useColorScheme();
44
+ const theme = colorScheme === "dark" ? NAV_THEME.dark : NAV_THEME.light;
45
+ const [input, setInput] = useState("");
46
+ const [threadId, setThreadId] = useState<string | null>(null);
47
+ const [isLoading, setIsLoading] = useState(false);
48
+ const scrollViewRef = useRef<ScrollView>(null);
49
+
50
+ const createThread = useMutation(api.chat.createNewThread);
51
+ const sendMessage = useMutation(api.chat.sendMessage);
52
+
53
+ const { results: messages } = useUIMessages(
54
+ api.chat.listMessages,
55
+ threadId ? { threadId } : "skip",
56
+ { initialNumItems: 50, stream: true },
57
+ );
58
+
59
+ const hasStreamingMessage = messages?.some(
60
+ (m: UIMessage) => m.status === "streaming",
61
+ );
62
+
63
+ useEffect(() => {
64
+ scrollViewRef.current?.scrollToEnd({ animated: true });
65
+ }, [messages]);
66
+
67
+ async function onSubmit() {
68
+ const value = input.trim();
69
+ if (!value || isLoading) return;
70
+
71
+ setIsLoading(true);
72
+ setInput("");
73
+
74
+ try {
75
+ let currentThreadId = threadId;
76
+ if (!currentThreadId) {
77
+ currentThreadId = await createThread();
78
+ setThreadId(currentThreadId);
79
+ }
80
+
81
+ await sendMessage({ threadId: currentThreadId, prompt: value });
82
+ } catch (error) {
83
+ console.error("Failed to send message:", error);
84
+ } finally {
85
+ setIsLoading(false);
86
+ }
87
+ }
88
+
89
+ return (
90
+ <Container>
91
+ <KeyboardAvoidingView
92
+ style={styles.container}
93
+ behavior={Platform.OS === "ios" ? "padding" : "height"}
94
+ >
95
+ <View style={styles.content}>
96
+ <View style={styles.header}>
97
+ <Text style={[styles.headerTitle, { color: theme.text }]}>
98
+ AI Chat
99
+ </Text>
100
+ <Text style={[styles.headerSubtitle, { color: theme.text, opacity: 0.7 }]}>
101
+ Chat with our AI assistant
102
+ </Text>
103
+ </View>
104
+ <ScrollView
105
+ ref={scrollViewRef}
106
+ style={styles.scrollView}
107
+ showsVerticalScrollIndicator={false}
108
+ >
109
+ {!messages || messages.length === 0 ? (
110
+ <View style={styles.emptyContainer}>
111
+ <Text style={[styles.emptyText, { color: theme.text, opacity: 0.7 }]}>
112
+ Ask me anything to get started!
113
+ </Text>
114
+ </View>
115
+ ) : (
116
+ <View style={styles.messagesList}>
117
+ {messages.map((message: UIMessage) => (
118
+ <View
119
+ key={message.key}
120
+ style={[
121
+ styles.messageCard,
122
+ {
123
+ backgroundColor: message.role === "user"
124
+ ? theme.primary + "20"
125
+ : theme.card,
126
+ borderColor: theme.border,
127
+ alignSelf: message.role === "user" ? "flex-end" : "flex-start",
128
+ marginLeft: message.role === "user" ? 32 : 0,
129
+ marginRight: message.role === "user" ? 0 : 32,
130
+ },
131
+ ]}
132
+ >
133
+ <Text style={[styles.messageRole, { color: theme.text }]}>
134
+ {message.role === "user" ? "You" : "AI Assistant"}
135
+ </Text>
136
+ <MessageContent
137
+ text={message.text ?? ""}
138
+ isStreaming={message.status === "streaming"}
139
+ textColor={theme.text}
140
+ />
141
+ </View>
142
+ ))}
143
+ {isLoading && !hasStreamingMessage && (
144
+ <View
145
+ style={[
146
+ styles.messageCard,
147
+ {
148
+ backgroundColor: theme.card,
149
+ borderColor: theme.border,
150
+ alignSelf: "flex-start",
151
+ marginRight: 32,
152
+ },
153
+ ]}
154
+ >
155
+ <Text style={[styles.messageRole, { color: theme.text }]}>
156
+ AI Assistant
157
+ </Text>
158
+ <View style={styles.loadingContainer}>
159
+ <ActivityIndicator size="small" color={theme.primary} />
160
+ <Text style={[styles.loadingText, { color: theme.text, opacity: 0.7 }]}>
161
+ Thinking...
162
+ </Text>
163
+ </View>
164
+ </View>
165
+ )}
166
+ </View>
167
+ )}
168
+ </ScrollView>
169
+ <View style={[styles.inputContainer, { borderTopColor: theme.border }]}>
170
+ <View style={styles.inputRow}>
171
+ <TextInput
172
+ value={input}
173
+ onChangeText={setInput}
174
+ placeholder="Type your message..."
175
+ placeholderTextColor={theme.text}
176
+ style={[
177
+ styles.input,
178
+ {
179
+ color: theme.text,
180
+ borderColor: theme.border,
181
+ backgroundColor: theme.background,
182
+ },
183
+ ]}
184
+ onSubmitEditing={(e) => {
185
+ e.preventDefault();
186
+ onSubmit();
187
+ }}
188
+ editable={!isLoading}
189
+ autoFocus={true}
190
+ multiline
191
+ />
192
+ <TouchableOpacity
193
+ onPress={onSubmit}
194
+ disabled={!input.trim() || isLoading}
195
+ style={[
196
+ styles.sendButton,
197
+ {
198
+ backgroundColor: input.trim() && !isLoading ? theme.primary : theme.border,
199
+ opacity: input.trim() && !isLoading ? 1 : 0.5,
200
+ },
201
+ ]}
202
+ >
203
+ <Ionicons
204
+ name="send"
205
+ size={20}
206
+ color="#ffffff"
207
+ />
208
+ </TouchableOpacity>
209
+ </View>
210
+ </View>
211
+ </View>
212
+ </KeyboardAvoidingView>
213
+ </Container>
214
+ );
215
+ }
216
+
217
+ const styles = StyleSheet.create({
218
+ container: {
219
+ flex: 1,
220
+ },
221
+ content: {
222
+ flex: 1,
223
+ padding: 16,
224
+ },
225
+ header: {
226
+ marginBottom: 16,
227
+ },
228
+ headerTitle: {
229
+ fontSize: 20,
230
+ fontWeight: "bold",
231
+ marginBottom: 4,
232
+ },
233
+ headerSubtitle: {
234
+ fontSize: 14,
235
+ },
236
+ scrollView: {
237
+ flex: 1,
238
+ marginBottom: 16,
239
+ },
240
+ emptyContainer: {
241
+ flex: 1,
242
+ justifyContent: "center",
243
+ alignItems: "center",
244
+ },
245
+ emptyText: {
246
+ fontSize: 16,
247
+ textAlign: "center",
248
+ },
249
+ messagesList: {
250
+ gap: 8,
251
+ paddingBottom: 16,
252
+ },
253
+ messageCard: {
254
+ borderWidth: 1,
255
+ padding: 12,
256
+ maxWidth: "80%",
257
+ },
258
+ messageRole: {
259
+ fontSize: 12,
260
+ fontWeight: "bold",
261
+ marginBottom: 4,
262
+ },
263
+ messageText: {
264
+ fontSize: 14,
265
+ lineHeight: 20,
266
+ },
267
+ loadingContainer: {
268
+ flexDirection: "row",
269
+ alignItems: "center",
270
+ gap: 8,
271
+ },
272
+ loadingText: {
273
+ fontSize: 14,
274
+ },
275
+ inputContainer: {
276
+ borderTopWidth: 1,
277
+ paddingTop: 12,
278
+ },
279
+ inputRow: {
280
+ flexDirection: "row",
281
+ alignItems: "flex-end",
282
+ gap: 8,
283
+ },
284
+ input: {
285
+ flex: 1,
286
+ borderWidth: 1,
287
+ padding: 8,
288
+ fontSize: 14,
289
+ minHeight: 36,
290
+ maxHeight: 100,
291
+ },
292
+ sendButton: {
293
+ padding: 8,
294
+ justifyContent: "center",
295
+ alignItems: "center",
296
+ },
297
+ });
298
+ {{else}}
1
299
  import { useRef, useEffect, useState } from "react";
2
300
  import {
3
301
  View,
@@ -104,8 +402,8 @@ export default function AIScreen() {
104
402
  style={[
105
403
  styles.messageCard,
106
404
  {
107
- backgroundColor: message.role === "user"
108
- ? theme.primary + "20"
405
+ backgroundColor: message.role === "user"
406
+ ? theme.primary + "20"
109
407
  : theme.card,
110
408
  borderColor: theme.border,
111
409
  alignSelf: message.role === "user" ? "flex-end" : "flex-start",
@@ -284,4 +582,4 @@ const styles = StyleSheet.create({
284
582
  textAlign: "center",
285
583
  },
286
584
  });
287
-
585
+ {{/if}}