DB-backed workspace + LangGraph agent

This commit is contained in:
Ruslan Bakiev
2026-02-18 13:56:35 +07:00
parent a8db021597
commit efa0b79c4c
36 changed files with 2125 additions and 468 deletions

View File

@@ -26,12 +26,38 @@ enum MessageChannel {
INTERNAL
}
enum ContactMessageKind {
MESSAGE
CALL
}
enum ChatRole {
USER
ASSISTANT
SYSTEM
}
enum OmniMessageStatus {
PENDING
SENT
FAILED
DELIVERED
READ
}
enum FeedCardDecision {
PENDING
ACCEPTED
REJECTED
}
enum WorkspaceDocumentType {
Regulation
Playbook
Policy
Template
}
model Team {
id String @id @default(cuid())
name String
@@ -41,8 +67,18 @@ model Team {
members TeamMember[]
contacts Contact[]
calendarEvents CalendarEvent[]
deals Deal[]
conversations ChatConversation[]
chatMessages ChatMessage[]
omniThreads OmniThread[]
omniMessages OmniMessage[]
omniIdentities OmniContactIdentity[]
telegramBusinessConnections TelegramBusinessConnection[]
feedCards FeedCard[]
contactPins ContactPin[]
documents WorkspaceDocument[]
}
model User {
@@ -76,6 +112,9 @@ model Contact {
teamId String
name String
company String?
country String?
location String?
avatarUrl String?
email String?
phone String?
createdAt DateTime @default(now())
@@ -85,6 +124,13 @@ model Contact {
note ContactNote?
messages ContactMessage[]
events CalendarEvent[]
deals Deal[]
feedCards FeedCard[]
pins ContactPin[]
omniThreads OmniThread[]
omniMessages OmniMessage[]
omniIdentities OmniContactIdentity[]
@@index([teamId, updatedAt])
}
@@ -102,9 +148,12 @@ model ContactNote {
model ContactMessage {
id String @id @default(cuid())
contactId String
kind ContactMessageKind @default(MESSAGE)
direction MessageDirection
channel MessageChannel
content String
durationSec Int?
transcriptJson Json?
occurredAt DateTime @default(now())
createdAt DateTime @default(now())
@@ -113,6 +162,84 @@ model ContactMessage {
@@index([contactId, occurredAt])
}
model OmniContactIdentity {
id String @id @default(cuid())
teamId String
contactId String
channel MessageChannel
externalId String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
team Team @relation(fields: [teamId], references: [id], onDelete: Cascade)
contact Contact @relation(fields: [contactId], references: [id], onDelete: Cascade)
@@unique([teamId, channel, externalId])
@@index([contactId])
@@index([teamId, updatedAt])
}
model OmniThread {
id String @id @default(cuid())
teamId String
contactId String
channel MessageChannel
externalChatId String
businessConnectionId String?
title String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
team Team @relation(fields: [teamId], references: [id], onDelete: Cascade)
contact Contact @relation(fields: [contactId], references: [id], onDelete: Cascade)
messages OmniMessage[]
@@unique([teamId, channel, externalChatId, businessConnectionId])
@@index([teamId, updatedAt])
@@index([contactId, updatedAt])
}
model OmniMessage {
id String @id @default(cuid())
teamId String
contactId String
threadId String
direction MessageDirection
channel MessageChannel
status OmniMessageStatus @default(PENDING)
text String
providerMessageId String?
providerUpdateId String?
rawJson Json?
occurredAt DateTime @default(now())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
team Team @relation(fields: [teamId], references: [id], onDelete: Cascade)
contact Contact @relation(fields: [contactId], references: [id], onDelete: Cascade)
thread OmniThread @relation(fields: [threadId], references: [id], onDelete: Cascade)
@@unique([threadId, providerMessageId])
@@index([teamId, occurredAt])
@@index([threadId, occurredAt])
}
model TelegramBusinessConnection {
id String @id @default(cuid())
teamId String
businessConnectionId String
isEnabled Boolean?
canReply Boolean?
rawJson Json?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
team Team @relation(fields: [teamId], references: [id], onDelete: Cascade)
@@unique([teamId, businessConnectionId])
@@index([teamId, updatedAt])
}
model CalendarEvent {
id String @id @default(cuid())
teamId String
@@ -133,6 +260,25 @@ model CalendarEvent {
@@index([teamId, startsAt])
}
model Deal {
id String @id @default(cuid())
teamId String
contactId String
title String
stage String
amount Int?
nextStep String?
summary String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
team Team @relation(fields: [teamId], references: [id], onDelete: Cascade)
contact Contact @relation(fields: [contactId], references: [id], onDelete: Cascade)
@@index([teamId, updatedAt])
@@index([contactId, updatedAt])
}
model ChatConversation {
id String @id @default(cuid())
teamId String
@@ -167,3 +313,54 @@ model ChatMessage {
@@index([teamId, createdAt])
@@index([conversationId, createdAt])
}
model FeedCard {
id String @id @default(cuid())
teamId String
contactId String?
happenedAt DateTime
text String
proposalJson Json
decision FeedCardDecision @default(PENDING)
decisionNote String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
team Team @relation(fields: [teamId], references: [id], onDelete: Cascade)
contact Contact? @relation(fields: [contactId], references: [id], onDelete: SetNull)
@@index([teamId, happenedAt])
@@index([contactId, happenedAt])
}
model ContactPin {
id String @id @default(cuid())
teamId String
contactId String
text String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
team Team @relation(fields: [teamId], references: [id], onDelete: Cascade)
contact Contact @relation(fields: [contactId], references: [id], onDelete: Cascade)
@@index([teamId, updatedAt])
@@index([contactId, updatedAt])
}
model WorkspaceDocument {
id String @id @default(cuid())
teamId String
title String
type WorkspaceDocumentType
owner String
scope String
summary String
body String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
team Team @relation(fields: [teamId], references: [id], onDelete: Cascade)
@@index([teamId, updatedAt])
}

View File

@@ -67,6 +67,9 @@ async function main() {
teamId: team.id,
name: "Anna Meyer",
company: "Nordline GmbH",
country: "Germany",
location: "Berlin",
avatarUrl: "https://randomuser.me/api/portraits/women/44.jpg",
email: "anna@nordline.example",
phone: "+49 30 123 45 67",
},
@@ -74,6 +77,9 @@ async function main() {
teamId: team.id,
name: "Murat Ali",
company: "Connect FZCO",
country: "UAE",
location: "Dubai",
avatarUrl: "https://randomuser.me/api/portraits/men/32.jpg",
email: "murat@connect.example",
phone: "+971 50 123 4567",
},
@@ -81,6 +87,9 @@ async function main() {
teamId: team.id,
name: "Ilya Petroff",
company: "Volta Tech",
country: "Armenia",
location: "Yerevan",
avatarUrl: "https://randomuser.me/api/portraits/men/18.jpg",
email: "ilya@volta.example",
phone: "+374 10 123 456",
},
@@ -88,6 +97,9 @@ async function main() {
teamId: team.id,
name: "Carlos Rivera",
company: "BluePort",
country: "Spain",
location: "Barcelona",
avatarUrl: "https://randomuser.me/api/portraits/men/65.jpg",
email: "carlos@blueport.example",
phone: "+34 600 123 456",
},
@@ -95,6 +107,9 @@ async function main() {
teamId: team.id,
name: "Daria Ivanova",
company: "Skyline Trade",
country: "Kazakhstan",
location: "Almaty",
avatarUrl: "https://randomuser.me/api/portraits/women/22.jpg",
email: "daria@skyline.example",
phone: "+7 777 123 45 67",
},
@@ -127,6 +142,7 @@ async function main() {
data: [
{
contactId: byName["Anna Meyer"].id,
kind: "MESSAGE",
direction: "IN",
channel: "TELEGRAM",
content: "Thanks for the demo. Can you send 2 pricing options?",
@@ -134,6 +150,7 @@ async function main() {
},
{
contactId: byName["Anna Meyer"].id,
kind: "MESSAGE",
direction: "OUT",
channel: "EMAIL",
content: "Sure. Option A/B attached. Can you confirm decision date for this cycle?",
@@ -141,6 +158,7 @@ async function main() {
},
{
contactId: byName["Murat Ali"].id,
kind: "MESSAGE",
direction: "IN",
channel: "WHATSAPP",
content: "Let's do a quick call. Need to clarify legal owner.",
@@ -148,11 +166,21 @@ async function main() {
},
{
contactId: byName["Ilya Petroff"].id,
kind: "MESSAGE",
direction: "OUT",
channel: "EMAIL",
content: "Draft: onboarding plan + two slots for tomorrow.",
occurredAt: atOffset(-1, 11, 12),
},
{
contactId: byName["Murat Ali"].id,
kind: "CALL",
direction: "OUT",
channel: "PHONE",
content: "Call started from CRM",
durationSec: 180,
occurredAt: atOffset(-1, 18, 30),
},
],
});
@@ -179,6 +207,29 @@ async function main() {
],
});
await prisma.deal.createMany({
data: [
{
teamId: team.id,
contactId: byName["Anna Meyer"].id,
title: "Nordline onboarding",
stage: "Proposal",
amount: 25000,
nextStep: "Lock decision date",
summary: "After demo: pricing options sent; waiting for decision date.",
},
{
teamId: team.id,
contactId: byName["Murat Ali"].id,
title: "Connect legal alignment",
stage: "Qualification",
amount: 18000,
nextStep: "Confirm legal owner",
summary: "High engagement; needs legal owner on their side.",
},
],
});
await prisma.chatConversation.upsert({
where: { id: `pilot-${team.id}` },
update: {},
@@ -203,6 +254,93 @@ async function main() {
},
],
});
await prisma.contactPin.createMany({
data: [
{ teamId: team.id, contactId: byName["Anna Meyer"].id, text: "First lock the decision date, then send the final offer." },
{ teamId: team.id, contactId: byName["Anna Meyer"].id, text: "A short follow-up is needed no later than 30 minutes after the demo." },
{ teamId: team.id, contactId: byName["Murat Ali"].id, text: "In every update, confirm the legal owner on the client side." },
{ teamId: team.id, contactId: byName["Ilya Petroff"].id, text: "Work through a structured onboarding plan, not pricing first." },
],
});
await prisma.workspaceDocument.createMany({
data: [
{
teamId: team.id,
title: "Outbound cadence v1",
type: "Regulation",
owner: "Revenue Ops",
scope: "All B2B accounts",
summary: "Unified sequence for first touch, follow-up, and qualification.",
body:
"## Goal\nMove a new contact to the first qualified call within 5 business days.\n\n## Base sequence\n- Day 0: first message in the primary channel.\n- Day 1: short follow-up with one clear ask.\n- Day 3: second follow-up + alternate channel.\n- Day 5: final ping and move to \"later\".\n\n## Rules\n- Always keep one explicit next step in each message.\n- Avoid long text walls.\n- After each reply, update context in the contact card.",
updatedAt: atOffset(-1, 10, 0),
},
{
teamId: team.id,
title: "Discovery call playbook",
type: "Playbook",
owner: "Sales Lead",
scope: "Discovery calls",
summary: "Call structure, mandatory questions, and outcome logging format.",
body:
"## Structure\n1. Context check (2 min)\n2. Current pain (8 min)\n3. Success criteria (6 min)\n4. Next step lock (4 min)\n\n## Mandatory outcomes\n- Confirmed business owner\n- Confirmed decision timeline\n- Confirmed next meeting date\n\n## Notes format\nAlways log: pain, impact, owner, ETA, risks.",
updatedAt: atOffset(-2, 12, 15),
},
{
teamId: team.id,
title: "AI assistant operating policy",
type: "Policy",
owner: "Founders",
scope: "AI recommendations and automations",
summary: "What actions AI can suggest and what requires explicit approval.",
body:
"## Allowed without approval\n- Draft message suggestions\n- Calendar proposal suggestions\n- Conversation summaries\n\n## Requires explicit approval\n- Sending a message to an external contact\n- Creating a calendar event\n- Changing deal stage\n\n## Logging\nEvery AI action must leave a short trace in the feed.",
updatedAt: atOffset(-3, 9, 40),
},
{
teamId: team.id,
title: "Post-call follow-up template",
type: "Template",
owner: "Enablement",
scope: "Any completed client call",
summary: "Template for short post-call follow-up with aligned actions.",
body:
"## Message template\nThanks for the call. Summary below:\n- What we aligned on\n- What remains open\n- Owner per action\n- Exact date for next sync\n\n## Quality bar\nThe client should understand the next step within 10 seconds.",
updatedAt: atOffset(-4, 16, 20),
},
],
});
await prisma.feedCard.createMany({
data: [
{
teamId: team.id,
contactId: byName["Anna Meyer"].id,
happenedAt: atOffset(0, 9, 35),
text:
"I analyzed Anna Meyer's latest activity: after a demo, the decision window is usually open for 1-2 hours. I suggest scheduling a follow-up immediately to keep momentum.",
proposalJson: {
title: "Add event to calendar",
details: ["Contact: Anna Meyer", "Start: 30 minutes from now", "Duration: 30 minutes"],
key: "create_followup",
},
},
{
teamId: team.id,
contactId: byName["Murat Ali"].id,
happenedAt: atOffset(0, 10, 8),
text:
"I found that Murat Ali gave 3 quick replies in a row over the last hour. I suggest moving to a short call now while engagement is high.",
proposalJson: {
title: "Start a call and open chat",
details: ["Contact: Murat Ali", "Channel: Phone", "After action: open the communication thread for this contact"],
key: "call",
},
},
],
});
}
main()