ref: remove updates
This commit is contained in:
parent
6c0428c9d3
commit
b656562e56
@ -0,0 +1,11 @@
|
|||||||
|
/*
|
||||||
|
Warnings:
|
||||||
|
|
||||||
|
- You are about to drop the `Update` table. If the table is not empty, all the data it contains will be lost.
|
||||||
|
|
||||||
|
*/
|
||||||
|
-- DropForeignKey
|
||||||
|
ALTER TABLE "Update" DROP CONSTRAINT "Update_accountId_fkey";
|
||||||
|
|
||||||
|
-- DropTable
|
||||||
|
DROP TABLE "Update";
|
||||||
@ -25,7 +25,6 @@ model Account {
|
|||||||
createdAt DateTime @default(now())
|
createdAt DateTime @default(now())
|
||||||
updatedAt DateTime @updatedAt
|
updatedAt DateTime @updatedAt
|
||||||
Session Session[]
|
Session Session[]
|
||||||
Update Update[]
|
|
||||||
AccountPushToken AccountPushToken[]
|
AccountPushToken AccountPushToken[]
|
||||||
TerminalAuthRequest TerminalAuthRequest[]
|
TerminalAuthRequest TerminalAuthRequest[]
|
||||||
UsageReport UsageReport[]
|
UsageReport UsageReport[]
|
||||||
@ -90,17 +89,6 @@ model SessionMessage {
|
|||||||
@@unique([sessionId, localId])
|
@@unique([sessionId, localId])
|
||||||
}
|
}
|
||||||
|
|
||||||
model Update {
|
|
||||||
id String @id @default(cuid())
|
|
||||||
accountId String
|
|
||||||
account Account @relation(fields: [accountId], references: [id])
|
|
||||||
seq Int
|
|
||||||
/// [UpdateBody]
|
|
||||||
content Json
|
|
||||||
createdAt DateTime @default(now())
|
|
||||||
updatedAt DateTime @updatedAt
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// Utility
|
// Utility
|
||||||
//
|
//
|
||||||
|
|||||||
@ -6,10 +6,9 @@ import { z } from "zod";
|
|||||||
import * as privacyKit from "privacy-kit";
|
import * as privacyKit from "privacy-kit";
|
||||||
import * as tweetnacl from "tweetnacl";
|
import * as tweetnacl from "tweetnacl";
|
||||||
import { db } from "@/storage/db";
|
import { db } from "@/storage/db";
|
||||||
import { Account, Update } from "@prisma/client";
|
import { Account } from "@prisma/client";
|
||||||
import { onShutdown } from "@/utils/shutdown";
|
import { onShutdown } from "@/utils/shutdown";
|
||||||
import { allocateSessionSeq, allocateUserSeq } from "@/services/seq";
|
import { allocateSessionSeq, allocateUserSeq } from "@/services/seq";
|
||||||
import { randomKey } from "@/utils/randomKey";
|
|
||||||
import { randomKeyNaked } from "@/utils/randomKeyNaked";
|
import { randomKeyNaked } from "@/utils/randomKeyNaked";
|
||||||
|
|
||||||
// Connection metadata types
|
// Connection metadata types
|
||||||
@ -334,31 +333,49 @@ export async function startApi(): Promise<{ app: FastifyInstance; io: Server }>
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// Create new session with update
|
|
||||||
const result = await db.$transaction(async (tx) => {
|
|
||||||
// Get user for update sequence
|
|
||||||
const user = await tx.account.findUnique({
|
|
||||||
where: { id: userId }
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!user) {
|
// Resolve seq
|
||||||
throw new Error('User not found');
|
const updSeq = await allocateUserSeq(userId);
|
||||||
|
|
||||||
|
// Create session
|
||||||
|
const session = await db.session.create({
|
||||||
|
data: {
|
||||||
|
accountId: userId,
|
||||||
|
tag: tag,
|
||||||
|
metadata: metadata
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
const updSeq = user.seq + 1;
|
// Create update
|
||||||
|
const updContent: PrismaJson.UpdateBody = {
|
||||||
|
t: 'new-session',
|
||||||
|
id: session.id,
|
||||||
|
seq: session.seq,
|
||||||
|
metadata: session.metadata,
|
||||||
|
metadataVersion: session.metadataVersion,
|
||||||
|
agentState: session.agentState,
|
||||||
|
agentStateVersion: session.agentStateVersion,
|
||||||
|
active: session.active,
|
||||||
|
activeAt: session.lastActiveAt.getTime(),
|
||||||
|
createdAt: session.createdAt.getTime(),
|
||||||
|
updatedAt: session.updatedAt.getTime()
|
||||||
|
};
|
||||||
|
|
||||||
// Create session
|
// Emit update to connected sockets
|
||||||
const session = await tx.session.create({
|
emitUpdateToInterestedClients({
|
||||||
data: {
|
event: 'update',
|
||||||
accountId: userId,
|
userId,
|
||||||
tag: tag,
|
sessionId: session.id,
|
||||||
metadata: metadata
|
payload: {
|
||||||
}
|
id: randomKeyNaked(12),
|
||||||
});
|
seq: updSeq,
|
||||||
|
body: updContent,
|
||||||
|
createdAt: Date.now()
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// Create update
|
return reply.send({
|
||||||
const updContent: PrismaJson.UpdateBody = {
|
session: {
|
||||||
t: 'new-session',
|
|
||||||
id: session.id,
|
id: session.id,
|
||||||
seq: session.seq,
|
seq: session.seq,
|
||||||
metadata: session.metadata,
|
metadata: session.metadata,
|
||||||
@ -369,50 +386,6 @@ export async function startApi(): Promise<{ app: FastifyInstance; io: Server }>
|
|||||||
activeAt: session.lastActiveAt.getTime(),
|
activeAt: session.lastActiveAt.getTime(),
|
||||||
createdAt: session.createdAt.getTime(),
|
createdAt: session.createdAt.getTime(),
|
||||||
updatedAt: session.updatedAt.getTime()
|
updatedAt: session.updatedAt.getTime()
|
||||||
};
|
|
||||||
|
|
||||||
const update = await tx.update.create({
|
|
||||||
data: {
|
|
||||||
accountId: userId,
|
|
||||||
seq: updSeq,
|
|
||||||
content: updContent
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Update user sequence
|
|
||||||
await tx.account.update({
|
|
||||||
where: { id: userId },
|
|
||||||
data: { seq: updSeq }
|
|
||||||
});
|
|
||||||
|
|
||||||
return { session, update };
|
|
||||||
});
|
|
||||||
|
|
||||||
// Emit update to connected sockets
|
|
||||||
emitUpdateToInterestedClients({
|
|
||||||
event: 'update',
|
|
||||||
userId,
|
|
||||||
sessionId: result.session.id,
|
|
||||||
payload: {
|
|
||||||
id: result.update.id,
|
|
||||||
seq: result.update.seq,
|
|
||||||
body: result.update.content,
|
|
||||||
createdAt: result.update.createdAt.getTime()
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return reply.send({
|
|
||||||
session: {
|
|
||||||
id: result.session.id,
|
|
||||||
seq: result.session.seq,
|
|
||||||
metadata: result.session.metadata,
|
|
||||||
metadataVersion: result.session.metadataVersion,
|
|
||||||
agentState: result.session.agentState,
|
|
||||||
agentStateVersion: result.session.agentStateVersion,
|
|
||||||
active: result.session.active,
|
|
||||||
activeAt: result.session.lastActiveAt.getTime(),
|
|
||||||
createdAt: result.session.createdAt.getTime(),
|
|
||||||
updatedAt: result.session.updatedAt.getTime()
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,8 +1,12 @@
|
|||||||
import { EventEmitter } from 'events';
|
import { EventEmitter } from 'events';
|
||||||
import { Update } from '@prisma/client';
|
|
||||||
|
|
||||||
export interface PubSubEvents {
|
export interface PubSubEvents {
|
||||||
'update': (accountId: string, update: Update) => void;
|
'update': (accountId: string, update: {
|
||||||
|
id: string,
|
||||||
|
seq: number,
|
||||||
|
body: any,
|
||||||
|
createdAt: number
|
||||||
|
}) => void;
|
||||||
'update-ephemeral': (accountId: string, update: { type: 'activity', id: string, active: boolean, activeAt: number, thinking: boolean }) => void;
|
'update-ephemeral': (accountId: string, update: { type: 'activity', id: string, active: boolean, activeAt: number, thinking: boolean }) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user