-
Notifications
You must be signed in to change notification settings - Fork 0
feat: conversation system #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| import { v } from "convex/values"; | ||
| import { mutation, query } from "./_generated/server"; | ||
|
|
||
| import { verifyAuth } from "./auth"; | ||
|
|
||
| export const create = mutation({ | ||
| args: { | ||
| projectId: v.id("projects"), | ||
| title: v.string(), | ||
| }, | ||
| handler: async (ctx, args) => { | ||
| const identity = await verifyAuth(ctx); | ||
|
|
||
| const project = await ctx.db.get("projects", args.projectId); | ||
|
|
||
| if (!project) { | ||
| throw new Error("Project not found"); | ||
| } | ||
|
|
||
| if (project.ownerId !== identity.subject) { | ||
| throw new Error("Unauthorized to access this project"); | ||
| } | ||
|
|
||
| const conversationId = await ctx.db.insert("conversations", { | ||
| projectId: args.projectId, | ||
| title: args.title, | ||
| updatedAt: Date.now(), | ||
| }); | ||
|
|
||
| return conversationId; | ||
| }, | ||
| }); | ||
|
|
||
| export const getById = query({ | ||
| args: { | ||
| id: v.id("conversations"), | ||
| }, | ||
| handler: async (ctx, args) => { | ||
| const identity = await verifyAuth(ctx); | ||
|
|
||
| const conversation = await ctx.db.get("conversations", args.id); | ||
|
|
||
| if (!conversation) { | ||
| throw new Error("Conversation not found"); | ||
| } | ||
|
|
||
| const project = await ctx.db.get("projects", conversation.projectId); | ||
|
|
||
| if (!project) { | ||
| throw new Error("Project not found"); | ||
| } | ||
|
|
||
| if (project.ownerId !== identity.subject) { | ||
| throw new Error("Unauthorized to access this project"); | ||
| } | ||
|
|
||
| return conversation; | ||
| }, | ||
| }); | ||
|
|
||
| export const getByProject = query({ | ||
| args: { | ||
| projectId: v.id("projects"), | ||
| }, | ||
| handler: async (ctx, args) => { | ||
| const identity = await verifyAuth(ctx); | ||
|
|
||
| const project = await ctx.db.get("projects", args.projectId); | ||
|
|
||
| if (!project) { | ||
| throw new Error("Project not found"); | ||
| } | ||
|
|
||
| if (project.ownerId !== identity.subject) { | ||
| throw new Error("Unauthorized to access this project"); | ||
| } | ||
|
|
||
| return await ctx.db | ||
| .query("conversations") | ||
| .withIndex("by_project", (q) => q.eq("projectId", args.projectId)) | ||
| .order("desc") | ||
| .collect(); | ||
| }, | ||
| }); | ||
|
|
||
| export const getMessages = query({ | ||
| args: { | ||
| conversationId: v.id("conversations"), | ||
| }, | ||
| handler: async (ctx, args) => { | ||
| const identity = await verifyAuth(ctx); | ||
|
|
||
| const conversation = await ctx.db.get("conversations", args.conversationId); | ||
|
|
||
| if (!conversation) { | ||
| throw new Error("Conversation not found"); | ||
| } | ||
|
|
||
| const project = await ctx.db.get("projects", conversation.projectId); | ||
|
|
||
| if (!project) { | ||
| throw new Error("Project not found"); | ||
| } | ||
|
|
||
| if (project.ownerId !== identity.subject) { | ||
| throw new Error("Unauthorized to access this project"); | ||
| } | ||
|
|
||
| return await ctx.db | ||
| .query("messages") | ||
| .withIndex("by_conversation", (q) => | ||
| q.eq("conversationId", args.conversationId) | ||
| ) | ||
| .order("asc") | ||
| .collect(); | ||
| }, | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import { v } from "convex/values"; | ||
|
|
||
| import { mutation, query } from "./_generated/server"; | ||
|
|
||
| const validateInternalKey = (key: string) => { | ||
| const internalKey = process.env.HEXSMITH_CONVEX_INTERNAL_KEY; | ||
|
|
||
| if (!internalKey) { | ||
| throw new Error("HEXSMITH_CONVEX_INTERNAL_KEY is not configured"); | ||
| } | ||
|
|
||
| if (key !== internalKey) { | ||
| throw new Error("Invalid internal key"); | ||
| } | ||
| }; | ||
|
|
||
| export const getConversationById = query({ | ||
| args: { | ||
| conversationId: v.id("conversations"), | ||
| internalKey: v.string(), | ||
| }, | ||
| handler: async (ctx, args) => { | ||
| validateInternalKey(args.internalKey); | ||
|
|
||
| return await ctx.db.get(args.conversationId); | ||
| }, | ||
| }); | ||
|
|
||
| export const createMessage = mutation({ | ||
| args: { | ||
| internalKey: v.string(), | ||
| conversationId: v.id("conversations"), | ||
| projectId: v.id("projects"), | ||
| role: v.union(v.literal("user"), v.literal("assistant")), | ||
| content: v.string(), | ||
| status: v.optional( | ||
| v.union( | ||
| v.literal("processing"), | ||
| v.literal("completed"), | ||
| v.literal("cancelled") | ||
| ) | ||
| ), | ||
| }, | ||
| handler: async (ctx, args) => { | ||
| validateInternalKey(args.internalKey); | ||
|
|
||
| const messageId = await ctx.db.insert("messages", { | ||
| conversationId: args.conversationId, | ||
| projectId: args.projectId, | ||
| role: args.role, | ||
| content: args.content, | ||
| status: args.status, | ||
| }); | ||
|
|
||
| // Update conversation's updatedAt | ||
| await ctx.db.patch(args.conversationId, { | ||
| updatedAt: Date.now(), | ||
| }); | ||
|
|
||
| return messageId; | ||
| }, | ||
| }); | ||
|
|
||
| export const updateMessageContent = mutation({ | ||
| args: { | ||
| internalKey: v.string(), | ||
| messageId: v.id("messages"), | ||
| content: v.string(), | ||
| }, | ||
| handler: async (ctx, args) => { | ||
| validateInternalKey(args.internalKey); | ||
|
|
||
| await ctx.db.patch(args.messageId, { | ||
| content: args.content, | ||
| status: "completed" as const, | ||
| }); | ||
| }, | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same issue: Fix all
ctx.db.getcalls throughout the file.Multiple occurrences of the incorrect two-argument
ctx.db.get()pattern exist in this file.🐛 Proposed fix for all occurrences
Also applies to: 47-47, 68-68, 93-93, 99-99
🤖 Prompt for AI Agents