ref: add session resolving and socketio

This commit is contained in:
Steve Korshakov 2025-07-12 20:45:15 -07:00
parent d70ec6c720
commit e2322b7e2d
3 changed files with 61 additions and 1 deletions

View File

@ -0,0 +1,12 @@
/*
Warnings:
- A unique constraint covering the columns `[accountId,tag]` on the table `Session` will be added. If there are existing duplicate values, this will fail.
- Added the required column `tag` to the `Session` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "Session" ADD COLUMN "tag" TEXT NOT NULL;
-- CreateIndex
CREATE UNIQUE INDEX "Session_accountId_tag_key" ON "Session"("accountId", "tag");

View File

@ -34,12 +34,14 @@ model Account {
model Session {
id String @id @default(cuid())
tag String
accountId String
account Account @relation(fields: [accountId], references: [id])
seq Int @default(0)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
messages SessionMessage[]
@@unique([accountId, tag])
}
model SessionMessage {

View File

@ -124,6 +124,7 @@ export async function startApi() {
take: 150,
select: {
id: true,
tag: true,
seq: true,
createdAt: true,
updatedAt: true
@ -133,6 +134,7 @@ export async function startApi() {
return reply.send({
sessions: sessions.map((v) => ({
id: v.id,
tag: v.tag,
seq: v.seq,
createdAt: v.createdAt.getTime(),
updatedAt: v.updatedAt.getTime()
@ -140,6 +142,50 @@ export async function startApi() {
});
});
// Create or load session by tag
typed.post('/v1/sessions', {
schema: {
body: z.object({
tag: z.string()
})
},
preHandler: app.authenticate
}, async (request, reply) => {
const userId = request.user.id;
const { tag } = request.body;
const session = await db.session.upsert({
where: {
accountId_tag: {
accountId: userId,
tag: tag
}
},
update: {},
create: {
accountId: userId,
tag: tag
},
select: {
id: true,
tag: true,
seq: true,
createdAt: true,
updatedAt: true
}
});
return reply.send({
session: {
id: session.id,
tag: session.tag,
seq: session.seq,
createdAt: session.createdAt.getTime(),
updatedAt: session.updatedAt.getTime()
}
});
});
// Messages API
typed.get('/v1/sessions/:sessionId/messages', {
schema: {
@ -311,7 +357,7 @@ export async function startApi() {
transports: ['websocket'],
pingTimeout: 45000,
pingInterval: 15000,
path: '/session/stream/',
path: '/v1/updates',
allowUpgrades: true,
upgradeTimeout: 10000,
connectTimeout: 20000