129 lines
3.4 KiB
TypeScript
129 lines
3.4 KiB
TypeScript
// @ts-nocheck
|
|
import { mutation, query } from "./_generated/server";
|
|
import { v } from "convex/values";
|
|
|
|
export const getByRoom = query({
|
|
args: {
|
|
roomName: v.string(),
|
|
},
|
|
handler: async (ctx, args) => {
|
|
return await ctx.db
|
|
.query("voiceSessions")
|
|
.withIndex("by_roomName", (q) => q.eq("roomName", args.roomName))
|
|
.unique();
|
|
},
|
|
});
|
|
|
|
export const createSession = mutation({
|
|
args: {
|
|
roomName: v.string(),
|
|
participantIdentity: v.string(),
|
|
siteUrl: v.optional(v.string()),
|
|
pathname: v.optional(v.string()),
|
|
pageUrl: v.optional(v.string()),
|
|
source: v.optional(v.string()),
|
|
metadata: v.optional(v.string()),
|
|
startedAt: v.optional(v.number()),
|
|
recordingDisclosureAt: v.optional(v.number()),
|
|
recordingStatus: v.optional(
|
|
v.union(
|
|
v.literal("pending"),
|
|
v.literal("starting"),
|
|
v.literal("recording"),
|
|
v.literal("completed"),
|
|
v.literal("failed"),
|
|
),
|
|
),
|
|
},
|
|
handler: async (ctx, args) => {
|
|
const now = args.startedAt ?? Date.now();
|
|
return await ctx.db.insert("voiceSessions", {
|
|
...args,
|
|
startedAt: now,
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
});
|
|
},
|
|
});
|
|
|
|
export const addTranscriptTurn = mutation({
|
|
args: {
|
|
sessionId: v.id("voiceSessions"),
|
|
roomName: v.string(),
|
|
participantIdentity: v.string(),
|
|
role: v.union(v.literal("user"), v.literal("assistant"), v.literal("system")),
|
|
text: v.string(),
|
|
kind: v.optional(v.string()),
|
|
isFinal: v.optional(v.boolean()),
|
|
language: v.optional(v.string()),
|
|
source: v.optional(v.string()),
|
|
createdAt: v.optional(v.number()),
|
|
},
|
|
handler: async (ctx, args) => {
|
|
const createdAt = args.createdAt ?? Date.now();
|
|
return await ctx.db.insert("voiceTranscriptTurns", {
|
|
...args,
|
|
text: args.text.trim(),
|
|
createdAt,
|
|
});
|
|
},
|
|
});
|
|
|
|
export const updateRecording = mutation({
|
|
args: {
|
|
sessionId: v.id("voiceSessions"),
|
|
recordingStatus: v.optional(
|
|
v.union(
|
|
v.literal("pending"),
|
|
v.literal("starting"),
|
|
v.literal("recording"),
|
|
v.literal("completed"),
|
|
v.literal("failed"),
|
|
),
|
|
),
|
|
recordingId: v.optional(v.string()),
|
|
recordingUrl: v.optional(v.string()),
|
|
recordingError: v.optional(v.string()),
|
|
},
|
|
handler: async (ctx, args) => {
|
|
await ctx.db.patch(args.sessionId, {
|
|
recordingStatus: args.recordingStatus,
|
|
recordingId: args.recordingId,
|
|
recordingUrl: args.recordingUrl,
|
|
recordingError: args.recordingError,
|
|
updatedAt: Date.now(),
|
|
});
|
|
return await ctx.db.get(args.sessionId);
|
|
},
|
|
});
|
|
|
|
export const completeSession = mutation({
|
|
args: {
|
|
sessionId: v.id("voiceSessions"),
|
|
endedAt: v.optional(v.number()),
|
|
recordingStatus: v.optional(
|
|
v.union(
|
|
v.literal("pending"),
|
|
v.literal("starting"),
|
|
v.literal("recording"),
|
|
v.literal("completed"),
|
|
v.literal("failed"),
|
|
),
|
|
),
|
|
recordingId: v.optional(v.string()),
|
|
recordingUrl: v.optional(v.string()),
|
|
recordingError: v.optional(v.string()),
|
|
},
|
|
handler: async (ctx, args) => {
|
|
const endedAt = args.endedAt ?? Date.now();
|
|
await ctx.db.patch(args.sessionId, {
|
|
endedAt,
|
|
recordingStatus: args.recordingStatus,
|
|
recordingId: args.recordingId,
|
|
recordingUrl: args.recordingUrl,
|
|
recordingError: args.recordingError,
|
|
updatedAt: endedAt,
|
|
});
|
|
return await ctx.db.get(args.sessionId);
|
|
},
|
|
});
|