Compare commits
2 Commits
513a394b93
...
codex/team
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
efa0b79c4c | ||
|
|
a8db021597 |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -3,9 +3,10 @@ node_modules
|
|||||||
.nuxt
|
.nuxt
|
||||||
.output
|
.output
|
||||||
.data
|
.data
|
||||||
|
.env
|
||||||
|
Frontend/.data
|
||||||
dist
|
dist
|
||||||
coverage
|
coverage
|
||||||
npm-debug.log*
|
npm-debug.log*
|
||||||
pnpm-lock.yaml
|
pnpm-lock.yaml
|
||||||
yarn.lock
|
yarn.lock
|
||||||
|
|
||||||
|
|||||||
12
Frontend/.env.example
Normal file
12
Frontend/.env.example
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
DATABASE_URL="file:../../.data/clientsflow-dev.db"
|
||||||
|
REDIS_URL="redis://localhost:6379"
|
||||||
|
|
||||||
|
# Agent (LangGraph + OpenAI)
|
||||||
|
OPENAI_API_KEY=""
|
||||||
|
OPENAI_MODEL="gpt-4o-mini"
|
||||||
|
# "langgraph" (default) or "rule"
|
||||||
|
CF_AGENT_MODE="langgraph"
|
||||||
|
|
||||||
|
TELEGRAM_BOT_TOKEN=""
|
||||||
|
TELEGRAM_WEBHOOK_SECRET=""
|
||||||
|
TELEGRAM_DEFAULT_TEAM_ID="demo-team"
|
||||||
603
Frontend/app.vue
603
Frontend/app.vue
@@ -1,4 +1,5 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
|
import { onMounted } from "vue";
|
||||||
type TabId = "communications" | "documents";
|
type TabId = "communications" | "documents";
|
||||||
type CalendarView = "day" | "week" | "month" | "year" | "agenda";
|
type CalendarView = "day" | "week" | "month" | "year" | "agenda";
|
||||||
type SortMode = "name" | "lastContact";
|
type SortMode = "name" | "lastContact";
|
||||||
@@ -147,372 +148,129 @@ function endAfter(startIso: string, minutes: number) {
|
|||||||
return d.toISOString();
|
return d.toISOString();
|
||||||
}
|
}
|
||||||
|
|
||||||
const feedCards = ref<FeedCard[]>([
|
const feedCards = ref<FeedCard[]>([]);
|
||||||
{
|
|
||||||
id: "f1",
|
|
||||||
at: atOffset(0, 9, 35),
|
|
||||||
contact: "Anna Meyer",
|
|
||||||
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.",
|
|
||||||
proposal: {
|
|
||||||
title: "Add event to calendar",
|
|
||||||
details: [
|
|
||||||
"Contact: Anna Meyer",
|
|
||||||
"Start: 30 minutes from now",
|
|
||||||
"Duration: 30 minutes",
|
|
||||||
],
|
|
||||||
key: "create_followup",
|
|
||||||
},
|
|
||||||
decision: "pending",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "f2",
|
|
||||||
at: atOffset(0, 10, 8),
|
|
||||||
contact: "Murat Ali",
|
|
||||||
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.",
|
|
||||||
proposal: {
|
|
||||||
title: "Start a call and open chat",
|
|
||||||
details: [
|
|
||||||
"Contact: Murat Ali",
|
|
||||||
"Channel: Phone",
|
|
||||||
"After action: open the communication thread for this contact",
|
|
||||||
],
|
|
||||||
key: "call",
|
|
||||||
},
|
|
||||||
decision: "pending",
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
|
|
||||||
const contacts = ref<Contact[]>([
|
const contacts = ref<Contact[]>([]);
|
||||||
{
|
|
||||||
id: "c1",
|
|
||||||
name: "Anna Meyer",
|
|
||||||
avatar: "https://randomuser.me/api/portraits/women/44.jpg",
|
|
||||||
company: "Nordline GmbH",
|
|
||||||
country: "Germany",
|
|
||||||
location: "Berlin",
|
|
||||||
channels: ["Telegram", "Phone", "Email"],
|
|
||||||
lastContactAt: atOffset(-0, 10, 20),
|
|
||||||
description: "Decision owner for procurement. Prefers short, concrete updates with clear deadlines.\n\nBest pattern: one clear question per message, then one explicit next step.\n\nRisk: if timeline question is delayed, decision date keeps slipping.",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "c2",
|
|
||||||
name: "Murat Ali",
|
|
||||||
avatar: "https://randomuser.me/api/portraits/men/32.jpg",
|
|
||||||
company: "Connect FZCO",
|
|
||||||
country: "UAE",
|
|
||||||
location: "Dubai",
|
|
||||||
channels: ["WhatsApp", "Instagram", "Phone", "Email"],
|
|
||||||
lastContactAt: atOffset(-1, 18, 10),
|
|
||||||
description: "High activity in chat, fast response cycle.\n\nNeeds legal path clarity and explicit owner from his side.\n\nBest move: in next call, lock legal owner and target signature date.",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "c3",
|
|
||||||
name: "Ilya Petroff",
|
|
||||||
avatar: "https://randomuser.me/api/portraits/men/18.jpg",
|
|
||||||
company: "Volta Tech",
|
|
||||||
country: "Armenia",
|
|
||||||
location: "Yerevan",
|
|
||||||
channels: ["Email", "Phone"],
|
|
||||||
lastContactAt: atOffset(-1, 11, 12),
|
|
||||||
description: "Early-stage lead. Interested, but wants structured onboarding before commercial details.\n\nBest move: send concise onboarding plan + 2 time slots for intro session.",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "c4",
|
|
||||||
name: "Carlos Rivera",
|
|
||||||
avatar: "https://randomuser.me/api/portraits/men/65.jpg",
|
|
||||||
company: "BluePort",
|
|
||||||
country: "Spain",
|
|
||||||
location: "Barcelona",
|
|
||||||
channels: ["WhatsApp", "Email"],
|
|
||||||
lastContactAt: atOffset(-3, 14, 35),
|
|
||||||
description: "Pilot interest for two teams. Focus on rollout timeline and ownership per team.\n\nBest move: propose phased pilot with weekly checkpoints.",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "c5",
|
|
||||||
name: "Daria Ivanova",
|
|
||||||
avatar: "https://randomuser.me/api/portraits/women/22.jpg",
|
|
||||||
company: "Skyline Trade",
|
|
||||||
country: "Kazakhstan",
|
|
||||||
location: "Almaty",
|
|
||||||
channels: ["Telegram", "Phone"],
|
|
||||||
lastContactAt: atOffset(-4, 16, 10),
|
|
||||||
description: "Commercial discussion is blocked by ROI framing.\n\nBest move: provide short ROI model with three measurable outcomes.",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "c6",
|
|
||||||
name: "Ethan Moore",
|
|
||||||
avatar: "https://randomuser.me/api/portraits/men/76.jpg",
|
|
||||||
company: "NorthBridge",
|
|
||||||
country: "USA",
|
|
||||||
location: "Austin",
|
|
||||||
channels: ["Email", "Phone"],
|
|
||||||
lastContactAt: atOffset(-5, 12, 45),
|
|
||||||
description: "Prefers stable weekly cadence and structured updates.\n\nBest move: fixed weekly summary + one priority ask per cycle.",
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
|
|
||||||
const calendarEvents = ref<CalendarEvent[]>([
|
const calendarEvents = ref<CalendarEvent[]>([]);
|
||||||
{
|
|
||||||
id: "e1",
|
|
||||||
title: "Anna follow-up",
|
|
||||||
start: inMinutes(12),
|
|
||||||
end: endAfter(inMinutes(12), 30),
|
|
||||||
contact: "Anna Meyer",
|
|
||||||
note: "Confirm decision date and next owner.",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "e2",
|
|
||||||
title: "Murat contract call",
|
|
||||||
start: atOffset(2, 16, 30),
|
|
||||||
end: endAfter(atOffset(2, 16, 30), 30),
|
|
||||||
contact: "Murat Ali",
|
|
||||||
note: "Lock legal owner and signing target.",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "e3",
|
|
||||||
title: "Ilya discovery",
|
|
||||||
start: atOffset(3, 11, 0),
|
|
||||||
end: endAfter(atOffset(3, 11, 0), 30),
|
|
||||||
contact: "Ilya Petroff",
|
|
||||||
note: "Qualify onboarding readiness.",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "e4",
|
|
||||||
title: "Internal sync",
|
|
||||||
start: atOffset(0, 9, 0),
|
|
||||||
end: endAfter(atOffset(0, 9, 0), 30),
|
|
||||||
contact: "Team",
|
|
||||||
note: "Align next actions across accounts.",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "e5",
|
|
||||||
title: "BluePort intro",
|
|
||||||
start: atOffset(4, 13, 0),
|
|
||||||
end: endAfter(atOffset(4, 13, 0), 30),
|
|
||||||
contact: "Carlos Rivera",
|
|
||||||
note: "Check pilot scope and timeline.",
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
|
|
||||||
const commItems = ref<CommItem[]>([
|
const commItems = ref<CommItem[]>([]);
|
||||||
{
|
|
||||||
id: "m1",
|
|
||||||
at: atOffset(0, 9, 20),
|
|
||||||
contact: "Anna Meyer",
|
|
||||||
channel: "Telegram",
|
|
||||||
kind: "message",
|
|
||||||
direction: "in",
|
|
||||||
text: "Can you share final pricing today?",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "m2",
|
|
||||||
at: atOffset(0, 9, 22),
|
|
||||||
contact: "Anna Meyer",
|
|
||||||
channel: "Telegram",
|
|
||||||
kind: "message",
|
|
||||||
direction: "out",
|
|
||||||
text: "Yes, sending after demo.",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "m3",
|
|
||||||
at: atOffset(0, 10, 0),
|
|
||||||
contact: "Anna Meyer",
|
|
||||||
channel: "Phone",
|
|
||||||
kind: "call",
|
|
||||||
direction: "out",
|
|
||||||
text: "Quick sync call",
|
|
||||||
duration: "14m",
|
|
||||||
transcript: [
|
|
||||||
"Anna: We can review pricing today, but I need one clear next step.",
|
|
||||||
"You: Agreed. I will send the final version right after this call.",
|
|
||||||
"Anna: Perfect. Add a follow-up tomorrow morning.",
|
|
||||||
],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "m4",
|
|
||||||
at: atOffset(-1, 15, 5),
|
|
||||||
contact: "Murat Ali",
|
|
||||||
channel: "WhatsApp",
|
|
||||||
kind: "message",
|
|
||||||
direction: "in",
|
|
||||||
text: "Need to shift call by one day.",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "m5",
|
|
||||||
at: atOffset(-1, 15, 8),
|
|
||||||
contact: "Murat Ali",
|
|
||||||
channel: "WhatsApp",
|
|
||||||
kind: "message",
|
|
||||||
direction: "out",
|
|
||||||
text: "Works. Let us do 12:00 tomorrow.",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "m6",
|
|
||||||
at: atOffset(-1, 18, 10),
|
|
||||||
contact: "Murat Ali",
|
|
||||||
channel: "Phone",
|
|
||||||
kind: "call",
|
|
||||||
direction: "in",
|
|
||||||
text: "Contract call",
|
|
||||||
duration: "9m",
|
|
||||||
transcript: [
|
|
||||||
"Murat: We can move forward if legal owner is confirmed today.",
|
|
||||||
"You: Understood. I will lock owner and send the timeline.",
|
|
||||||
"Murat: Then we target signature this week.",
|
|
||||||
],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "m9",
|
|
||||||
at: atOffset(-1, 13, 40),
|
|
||||||
contact: "Murat Ali",
|
|
||||||
channel: "Instagram",
|
|
||||||
kind: "message",
|
|
||||||
direction: "in",
|
|
||||||
text: "Sent details in DM, can we align tomorrow?",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "m7",
|
|
||||||
at: atOffset(-1, 11, 12),
|
|
||||||
contact: "Ilya Petroff",
|
|
||||||
channel: "Email",
|
|
||||||
kind: "message",
|
|
||||||
direction: "in",
|
|
||||||
text: "Interested in pilot, what are next steps?",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "m8",
|
|
||||||
at: atOffset(-1, 12, 0),
|
|
||||||
contact: "Ilya Petroff",
|
|
||||||
channel: "Phone",
|
|
||||||
kind: "call",
|
|
||||||
direction: "out",
|
|
||||||
text: "Missed callback",
|
|
||||||
duration: "-",
|
|
||||||
transcript: [
|
|
||||||
"No full transcript available. Call was not connected.",
|
|
||||||
],
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
|
|
||||||
const commPins = ref<CommPin[]>([
|
const commPins = ref<CommPin[]>([]);
|
||||||
{
|
|
||||||
id: "cp-1",
|
|
||||||
contact: "Anna Meyer",
|
|
||||||
text: "First lock the decision date, then send the final offer.",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "cp-2",
|
|
||||||
contact: "Anna Meyer",
|
|
||||||
text: "A short follow-up is needed no later than 30 minutes after the demo.",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "cp-3",
|
|
||||||
contact: "Murat Ali",
|
|
||||||
text: "In every update, confirm the legal owner on the client side.",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "cp-4",
|
|
||||||
contact: "Ilya Petroff",
|
|
||||||
text: "Work through a structured onboarding plan, not pricing first.",
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
|
|
||||||
const deals = ref<Deal[]>([
|
const deals = ref<Deal[]>([]);
|
||||||
{
|
|
||||||
id: "d1",
|
|
||||||
contact: "Anna Meyer",
|
|
||||||
title: "Nordline pilot",
|
|
||||||
company: "Nordline GmbH",
|
|
||||||
stage: "Negotiation",
|
|
||||||
amount: "$28,000",
|
|
||||||
nextStep: "Send post-demo offer within 30 minutes",
|
|
||||||
summary: "Active thread, fast feedback, high close probability this week.",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "d2",
|
|
||||||
contact: "Murat Ali",
|
|
||||||
title: "Connect annual",
|
|
||||||
company: "Connect FZCO",
|
|
||||||
stage: "Legal review",
|
|
||||||
amount: "$74,000",
|
|
||||||
nextStep: "Lock legal owner and expected sign date",
|
|
||||||
summary: "Strong engagement in chat and calls, pending legal alignment.",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "d3",
|
|
||||||
contact: "Ilya Petroff",
|
|
||||||
title: "Volta onboarding",
|
|
||||||
company: "Volta Tech",
|
|
||||||
stage: "Discovery",
|
|
||||||
amount: "$12,000",
|
|
||||||
nextStep: "Offer two onboarding slots for tomorrow",
|
|
||||||
summary: "Early stage interest, needs concise onboarding plan.",
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
|
|
||||||
const documents = ref<WorkspaceDocument[]>([
|
const documents = ref<WorkspaceDocument[]>([]);
|
||||||
{
|
|
||||||
id: "doc-1",
|
|
||||||
title: "Outbound cadence v1",
|
|
||||||
type: "Regulation",
|
|
||||||
owner: "Revenue Ops",
|
|
||||||
scope: "All B2B accounts",
|
|
||||||
updatedAt: atOffset(-1, 10, 0),
|
|
||||||
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.",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "doc-2",
|
|
||||||
title: "Discovery call playbook",
|
|
||||||
type: "Playbook",
|
|
||||||
owner: "Sales Lead",
|
|
||||||
scope: "Discovery calls",
|
|
||||||
updatedAt: atOffset(-2, 12, 15),
|
|
||||||
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.",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "doc-3",
|
|
||||||
title: "AI assistant operating policy",
|
|
||||||
type: "Policy",
|
|
||||||
owner: "Founders",
|
|
||||||
scope: "AI recommendations and automations",
|
|
||||||
updatedAt: atOffset(-3, 9, 40),
|
|
||||||
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.",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "doc-4",
|
|
||||||
title: "Post-call follow-up template",
|
|
||||||
type: "Template",
|
|
||||||
owner: "Enablement",
|
|
||||||
scope: "Any completed client call",
|
|
||||||
updatedAt: atOffset(-4, 16, 20),
|
|
||||||
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.",
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
|
|
||||||
const pilotMessages = ref([
|
type PilotMessage = {
|
||||||
{ id: "p1", role: "assistant", text: "I monitor calendar, contacts, and communications in one flow." },
|
id: string;
|
||||||
{ id: "p2", role: "user", text: "What is critical to do today?" },
|
role: "user" | "assistant" | "system";
|
||||||
{ id: "p3", role: "assistant", text: "First Anna after demo, then Murat on legal owner." },
|
text: string;
|
||||||
]);
|
plan?: string[] | null;
|
||||||
|
tools?: string[] | null;
|
||||||
|
createdAt?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
const pilotMessages = ref<PilotMessage[]>([]);
|
||||||
const pilotInput = ref("");
|
const pilotInput = ref("");
|
||||||
|
const pilotSending = ref(false);
|
||||||
|
const authMe = ref<{ user: { id: string; email: string; name: string }; team: { id: string; name: string } } | null>(
|
||||||
|
null,
|
||||||
|
);
|
||||||
|
const loginEmail = ref("");
|
||||||
|
const loginName = ref("");
|
||||||
|
const loginTeamName = ref("My Team");
|
||||||
|
const loginError = ref<string | null>(null);
|
||||||
|
const loginBusy = ref(false);
|
||||||
|
|
||||||
function sendPilotMessage() {
|
async function loadPilotMessages() {
|
||||||
const text = pilotInput.value.trim();
|
const res = await $fetch<{ items: PilotMessage[] }>("/api/chat");
|
||||||
if (!text) return;
|
pilotMessages.value = res.items ?? [];
|
||||||
|
|
||||||
pilotMessages.value.push({ id: `p-${Date.now()}`, role: "user", text });
|
|
||||||
pilotMessages.value.push({
|
|
||||||
id: `p-${Date.now() + 1}`,
|
|
||||||
role: "assistant",
|
|
||||||
text: "Got it. Added to context and will use it in the next recommendations.",
|
|
||||||
});
|
|
||||||
pilotInput.value = "";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function loadMe() {
|
||||||
|
authMe.value = await $fetch("/api/auth/me");
|
||||||
|
}
|
||||||
|
|
||||||
|
async function login() {
|
||||||
|
loginError.value = null;
|
||||||
|
loginBusy.value = true;
|
||||||
|
try {
|
||||||
|
await $fetch("/api/auth/login", {
|
||||||
|
method: "POST",
|
||||||
|
body: { email: loginEmail.value, name: loginName.value, teamName: loginTeamName.value },
|
||||||
|
});
|
||||||
|
await loadMe();
|
||||||
|
await Promise.all([loadPilotMessages(), refreshCrmData()]);
|
||||||
|
} catch (e: any) {
|
||||||
|
loginError.value = e?.data?.message || e?.message || "Login failed";
|
||||||
|
} finally {
|
||||||
|
loginBusy.value = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function logout() {
|
||||||
|
await $fetch("/api/auth/logout", { method: "POST" });
|
||||||
|
authMe.value = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function useDemo() {
|
||||||
|
await $fetch("/api/auth/demo", { method: "POST" });
|
||||||
|
await loadMe();
|
||||||
|
await Promise.all([loadPilotMessages(), refreshCrmData()]);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function refreshCrmData() {
|
||||||
|
const [contactsRes, commRes, calRes, dealsRes, feedRes, pinsRes, docsRes] = await Promise.all([
|
||||||
|
$fetch<{ items: Contact[] }>("/api/contacts"),
|
||||||
|
$fetch<{ items: CommItem[] }>("/api/communications"),
|
||||||
|
$fetch<{ items: CalendarEvent[] }>("/api/calendar"),
|
||||||
|
$fetch<{ items: Deal[] }>("/api/deals"),
|
||||||
|
$fetch<{ items: FeedCard[] }>("/api/feed"),
|
||||||
|
$fetch<{ items: CommPin[] }>("/api/pins"),
|
||||||
|
$fetch<{ items: WorkspaceDocument[] }>("/api/documents"),
|
||||||
|
]);
|
||||||
|
|
||||||
|
contacts.value = contactsRes.items ?? [];
|
||||||
|
commItems.value = commRes.items ?? [];
|
||||||
|
calendarEvents.value = calRes.items ?? [];
|
||||||
|
deals.value = dealsRes.items ?? [];
|
||||||
|
feedCards.value = feedRes.items ?? [];
|
||||||
|
commPins.value = pinsRes.items ?? [];
|
||||||
|
documents.value = docsRes.items ?? [];
|
||||||
|
|
||||||
|
// Derive channels per contact from communication items.
|
||||||
|
const byName = new Map<string, Set<string>>();
|
||||||
|
for (const item of commItems.value) {
|
||||||
|
if (!byName.has(item.contact)) byName.set(item.contact, new Set());
|
||||||
|
byName.get(item.contact)?.add(item.channel);
|
||||||
|
}
|
||||||
|
contacts.value = contacts.value.map((c) => ({
|
||||||
|
...c,
|
||||||
|
channels: Array.from(byName.get(c.name) ?? []),
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
async function sendPilotMessage() {
|
||||||
|
const text = pilotInput.value.trim();
|
||||||
|
if (!text || pilotSending.value) return;
|
||||||
|
|
||||||
|
pilotSending.value = true;
|
||||||
|
try {
|
||||||
|
await $fetch("/api/chat", { method: "POST", body: { text } });
|
||||||
|
pilotInput.value = "";
|
||||||
|
await loadPilotMessages();
|
||||||
|
} finally {
|
||||||
|
pilotSending.value = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
loadMe()
|
||||||
|
.then(() => Promise.all([loadPilotMessages(), refreshCrmData()]))
|
||||||
|
.catch(() => {});
|
||||||
|
});
|
||||||
|
|
||||||
const calendarView = ref<CalendarView>("month");
|
const calendarView = ref<CalendarView>("month");
|
||||||
const calendarCursor = ref(new Date(new Date().getFullYear(), new Date().getMonth(), 1));
|
const calendarCursor = ref(new Date(new Date().getFullYear(), new Date().getMonth(), 1));
|
||||||
const selectedDateKey = ref(dayKey(new Date()));
|
const selectedDateKey = ref(dayKey(new Date()));
|
||||||
@@ -1117,7 +875,10 @@ function makeId(prefix: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function pushPilotNote(text: string) {
|
function pushPilotNote(text: string) {
|
||||||
pilotMessages.value.push({ id: makeId("p"), role: "assistant", text });
|
// Fire-and-forget: log assistant note to the same conversation.
|
||||||
|
$fetch("/api/chat/log", { method: "POST", body: { text } })
|
||||||
|
.then(loadPilotMessages)
|
||||||
|
.catch(() => {});
|
||||||
}
|
}
|
||||||
|
|
||||||
function openCommunicationThread(contact: string) {
|
function openCommunicationThread(contact: string) {
|
||||||
@@ -1159,7 +920,8 @@ function openMessageFromContact(channel: CommItem["channel"]) {
|
|||||||
selectedCommChannel.value = channel;
|
selectedCommChannel.value = channel;
|
||||||
}
|
}
|
||||||
|
|
||||||
function executeFeedAction(key: FeedCard["proposal"]["key"]) {
|
async function executeFeedAction(card: FeedCard) {
|
||||||
|
const key = card.proposal.key;
|
||||||
if (key === "create_followup") {
|
if (key === "create_followup") {
|
||||||
const start = new Date();
|
const start = new Date();
|
||||||
start.setMinutes(start.getMinutes() + 30);
|
start.setMinutes(start.getMinutes() + 30);
|
||||||
@@ -1167,54 +929,62 @@ function executeFeedAction(key: FeedCard["proposal"]["key"]) {
|
|||||||
const end = new Date(start);
|
const end = new Date(start);
|
||||||
end.setMinutes(end.getMinutes() + 30);
|
end.setMinutes(end.getMinutes() + 30);
|
||||||
|
|
||||||
calendarEvents.value.unshift({
|
const res = await $fetch<{ item: CalendarEvent }>("/api/calendar", {
|
||||||
id: makeId("e"),
|
method: "POST",
|
||||||
title: "Follow-up: Anna",
|
body: {
|
||||||
|
title: `Follow-up: ${card.contact.split(" ")[0] ?? "Contact"}`,
|
||||||
start: start.toISOString(),
|
start: start.toISOString(),
|
||||||
end: end.toISOString(),
|
end: end.toISOString(),
|
||||||
contact: "Anna Meyer",
|
contact: card.contact,
|
||||||
note: "Created from feed action.",
|
note: "Created from feed action.",
|
||||||
|
status: "planned",
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
calendarEvents.value = [res.item, ...calendarEvents.value];
|
||||||
|
|
||||||
selectedDateKey.value = dayKey(start);
|
selectedDateKey.value = dayKey(start);
|
||||||
calendarCursor.value = new Date(start.getFullYear(), start.getMonth(), 1);
|
calendarCursor.value = new Date(start.getFullYear(), start.getMonth(), 1);
|
||||||
selectedTab.value = "communications";
|
selectedTab.value = "communications";
|
||||||
peopleLeftMode.value = "calendar";
|
peopleLeftMode.value = "calendar";
|
||||||
return `Event created: Follow-up: Anna · ${formatDay(start.toISOString())} ${formatTime(start.toISOString())}`;
|
return `Event created: Follow-up · ${formatDay(start.toISOString())} ${formatTime(start.toISOString())} · ${card.contact}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (key === "open_comm") {
|
if (key === "open_comm") {
|
||||||
openCommunicationThread("Murat Ali");
|
openCommunicationThread(card.contact);
|
||||||
return "Opened Murat Ali communication thread.";
|
return `Opened ${card.contact} communication thread.`;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (key === "call") {
|
if (key === "call") {
|
||||||
commItems.value.push({
|
await $fetch("/api/communications", {
|
||||||
id: makeId("m"),
|
method: "POST",
|
||||||
at: new Date().toISOString(),
|
body: {
|
||||||
contact: "Murat Ali",
|
contact: card.contact,
|
||||||
channel: "Phone",
|
channel: "Phone",
|
||||||
kind: "call",
|
kind: "call",
|
||||||
direction: "out",
|
direction: "out",
|
||||||
text: "Call started from feed",
|
text: "Call started from feed",
|
||||||
duration: "00:00",
|
durationSec: 0,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
openCommunicationThread("Murat Ali");
|
await refreshCrmData();
|
||||||
return "Call event created and Murat Ali chat opened.";
|
openCommunicationThread(card.contact);
|
||||||
|
return `Call event created and ${card.contact} chat opened.`;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (key === "draft_message") {
|
if (key === "draft_message") {
|
||||||
commItems.value.push({
|
await $fetch("/api/communications", {
|
||||||
id: makeId("m"),
|
method: "POST",
|
||||||
at: new Date().toISOString(),
|
body: {
|
||||||
contact: "Ilya Petroff",
|
contact: card.contact,
|
||||||
channel: "Email",
|
channel: "Email",
|
||||||
kind: "message",
|
kind: "message",
|
||||||
direction: "out",
|
direction: "out",
|
||||||
text: "Draft: onboarding plan + two slots for tomorrow.",
|
text: "Draft: onboarding plan + two slots for tomorrow.",
|
||||||
|
},
|
||||||
});
|
});
|
||||||
openCommunicationThread("Ilya Petroff");
|
await refreshCrmData();
|
||||||
return "Draft message added to Ilya Petroff communications.";
|
openCommunicationThread(card.contact);
|
||||||
|
return `Draft message added to ${card.contact} communications.`;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (key === "run_summary") {
|
if (key === "run_summary") {
|
||||||
@@ -1222,32 +992,38 @@ function executeFeedAction(key: FeedCard["proposal"]["key"]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (key === "prepare_question") {
|
if (key === "prepare_question") {
|
||||||
commItems.value.push({
|
await $fetch("/api/communications", {
|
||||||
id: makeId("m"),
|
method: "POST",
|
||||||
at: new Date().toISOString(),
|
body: {
|
||||||
contact: "Anna Meyer",
|
contact: card.contact,
|
||||||
channel: "Telegram",
|
channel: "Telegram",
|
||||||
kind: "message",
|
kind: "message",
|
||||||
direction: "out",
|
direction: "out",
|
||||||
text: "Draft: can you confirm your decision date for this cycle?",
|
text: "Draft: can you confirm your decision date for this cycle?",
|
||||||
|
},
|
||||||
});
|
});
|
||||||
openCommunicationThread("Anna Meyer");
|
await refreshCrmData();
|
||||||
return "Question about decision date added to Anna Meyer chat.";
|
openCommunicationThread(card.contact);
|
||||||
|
return `Question about decision date added to ${card.contact} chat.`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return "Action completed.";
|
return "Action completed.";
|
||||||
}
|
}
|
||||||
|
|
||||||
function decideFeedCard(card: FeedCard, decision: "accepted" | "rejected") {
|
async function decideFeedCard(card: FeedCard, decision: "accepted" | "rejected") {
|
||||||
card.decision = decision;
|
card.decision = decision;
|
||||||
|
|
||||||
if (decision === "rejected") {
|
if (decision === "rejected") {
|
||||||
card.decisionNote = "Rejected. Nothing created.";
|
const note = "Rejected. Nothing created.";
|
||||||
|
card.decisionNote = note;
|
||||||
|
await $fetch(`/api/feed/${card.id}`, { method: "PUT", body: { decision: "rejected", decisionNote: note } });
|
||||||
pushPilotNote(`[${card.contact}] recommendation rejected: ${card.proposal.title}`);
|
pushPilotNote(`[${card.contact}] recommendation rejected: ${card.proposal.title}`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = executeFeedAction(card.proposal.key);
|
const result = await executeFeedAction(card);
|
||||||
card.decisionNote = result;
|
card.decisionNote = result;
|
||||||
|
await $fetch(`/api/feed/${card.id}`, { method: "PUT", body: { decision: "accepted", decisionNote: result } });
|
||||||
pushPilotNote(`[${card.contact}] ${result}`);
|
pushPilotNote(`[${card.contact}] ${result}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1258,10 +1034,37 @@ function decideFeedCard(card: FeedCard, decision: "accepted" | "rejected") {
|
|||||||
<div class="grid gap-3 lg:grid-cols-12 lg:gap-4">
|
<div class="grid gap-3 lg:grid-cols-12 lg:gap-4">
|
||||||
<aside class="card min-h-0 border border-base-300 bg-base-100 shadow-sm lg:col-span-3 lg:sticky lg:top-5 lg:h-[calc(100vh-2.5rem)]">
|
<aside class="card min-h-0 border border-base-300 bg-base-100 shadow-sm lg:col-span-3 lg:sticky lg:top-5 lg:h-[calc(100vh-2.5rem)]">
|
||||||
<div class="card-body h-full min-h-0 p-3 md:p-4">
|
<div class="card-body h-full min-h-0 p-3 md:p-4">
|
||||||
<div class="mb-2">
|
<div class="mb-2 flex items-start justify-between gap-2">
|
||||||
|
<div>
|
||||||
<h2 class="text-sm font-semibold uppercase tracking-wide text-base-content/70">Pilot</h2>
|
<h2 class="text-sm font-semibold uppercase tracking-wide text-base-content/70">Pilot</h2>
|
||||||
|
<p v-if="authMe" class="mt-1 text-xs text-base-content/60">
|
||||||
|
{{ authMe.team.name }} · {{ authMe.user.name }}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<button v-if="authMe" class="btn btn-ghost btn-xs" @click="logout">Logout</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div v-if="!authMe" class="space-y-3">
|
||||||
|
<div class="rounded-xl border border-base-300 bg-base-100 p-3">
|
||||||
|
<p class="text-sm font-semibold">Login</p>
|
||||||
|
<p class="mt-1 text-xs text-base-content/60">MVP: email + name. Потом подключим нормальную auth.</p>
|
||||||
|
|
||||||
|
<div class="mt-3 space-y-2">
|
||||||
|
<input v-model="loginEmail" type="email" class="input input-bordered input-sm w-full" placeholder="Email" />
|
||||||
|
<input v-model="loginName" type="text" class="input input-bordered input-sm w-full" placeholder="Name" />
|
||||||
|
<input v-model="loginTeamName" type="text" class="input input-bordered input-sm w-full" placeholder="Team name" />
|
||||||
|
<p v-if="loginError" class="text-xs text-error">{{ loginError }}</p>
|
||||||
|
<button class="btn btn-sm w-full" :disabled="loginBusy" @click="login">
|
||||||
|
{{ loginBusy ? "Logging in..." : "Login" }}
|
||||||
|
</button>
|
||||||
|
<button class="btn btn-ghost btn-sm w-full" :disabled="loginBusy" @click="useDemo">
|
||||||
|
Use demo
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<template v-else>
|
||||||
<div class="min-h-0 flex-1 space-y-2 overflow-y-auto rounded-xl border border-base-300 p-2">
|
<div class="min-h-0 flex-1 space-y-2 overflow-y-auto rounded-xl border border-base-300 p-2">
|
||||||
<div
|
<div
|
||||||
v-for="message in pilotMessages"
|
v-for="message in pilotMessages"
|
||||||
@@ -1269,9 +1072,32 @@ function decideFeedCard(card: FeedCard, decision: "accepted" | "rejected") {
|
|||||||
class="chat"
|
class="chat"
|
||||||
:class="message.role === 'assistant' ? 'chat-start' : 'chat-end'"
|
:class="message.role === 'assistant' ? 'chat-start' : 'chat-end'"
|
||||||
>
|
>
|
||||||
|
<div class="space-y-2">
|
||||||
<div class="chat-bubble text-sm" :class="message.role === 'assistant' ? 'chat-bubble-neutral' : ''">
|
<div class="chat-bubble text-sm" :class="message.role === 'assistant' ? 'chat-bubble-neutral' : ''">
|
||||||
{{ message.text }}
|
{{ message.text }}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<details
|
||||||
|
v-if="message.role === 'assistant' && ((message.plan && message.plan.length) || (message.tools && message.tools.length))"
|
||||||
|
class="rounded-lg border border-base-300 bg-base-100 p-2 text-xs"
|
||||||
|
>
|
||||||
|
<summary class="cursor-pointer select-none font-semibold text-base-content/70">Plan & tools</summary>
|
||||||
|
<div class="mt-2 space-y-2">
|
||||||
|
<div v-if="message.plan && message.plan.length">
|
||||||
|
<div class="font-semibold text-base-content/70">Plan</div>
|
||||||
|
<ul class="list-disc pl-4">
|
||||||
|
<li v-for="(step, idx) in message.plan" :key="`plan-${message.id}-${idx}`">{{ step }}</li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
<div v-if="message.tools && message.tools.length">
|
||||||
|
<div class="font-semibold text-base-content/70">Tools</div>
|
||||||
|
<ul class="list-disc pl-4">
|
||||||
|
<li v-for="(tool, idx) in message.tools" :key="`tools-${message.id}-${idx}`">{{ tool }}</li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</details>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -1283,8 +1109,9 @@ function decideFeedCard(card: FeedCard, decision: "accepted" | "rejected") {
|
|||||||
placeholder="Write a message..."
|
placeholder="Write a message..."
|
||||||
@keyup.enter="sendPilotMessage"
|
@keyup.enter="sendPilotMessage"
|
||||||
>
|
>
|
||||||
<button class="btn btn-sm" @click="sendPilotMessage">Send</button>
|
<button class="btn btn-sm" :disabled="pilotSending" @click="sendPilotMessage">Send</button>
|
||||||
</div>
|
</div>
|
||||||
|
</template>
|
||||||
</div>
|
</div>
|
||||||
</aside>
|
</aside>
|
||||||
|
|
||||||
|
|||||||
940
Frontend/package-lock.json
generated
940
Frontend/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -4,12 +4,22 @@
|
|||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "nuxt build",
|
"build": "nuxt build",
|
||||||
|
"db:generate": "prisma generate",
|
||||||
|
"db:migrate": "prisma migrate dev",
|
||||||
|
"db:push": "prisma db push",
|
||||||
|
"db:seed": "node prisma/seed.mjs",
|
||||||
|
"dataset:export": "node scripts/export-dataset.mjs",
|
||||||
"dev": "nuxt dev",
|
"dev": "nuxt dev",
|
||||||
"generate": "nuxt generate",
|
"generate": "nuxt generate",
|
||||||
|
"postinstall": "nuxt prepare && prisma generate",
|
||||||
"preview": "nuxt preview",
|
"preview": "nuxt preview",
|
||||||
"postinstall": "nuxt prepare"
|
"typecheck": "nuxt typecheck"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@prisma/client": "^6.16.1",
|
||||||
|
"@langchain/core": "^0.3.77",
|
||||||
|
"@langchain/langgraph": "^0.2.74",
|
||||||
|
"@langchain/openai": "^0.6.9",
|
||||||
"@tailwindcss/vite": "^4.1.18",
|
"@tailwindcss/vite": "^4.1.18",
|
||||||
"@tiptap/extension-collaboration": "^2.27.2",
|
"@tiptap/extension-collaboration": "^2.27.2",
|
||||||
"@tiptap/extension-collaboration-cursor": "^2.27.2",
|
"@tiptap/extension-collaboration-cursor": "^2.27.2",
|
||||||
@@ -17,10 +27,19 @@
|
|||||||
"@tiptap/starter-kit": "^2.27.2",
|
"@tiptap/starter-kit": "^2.27.2",
|
||||||
"@tiptap/vue-3": "^2.27.2",
|
"@tiptap/vue-3": "^2.27.2",
|
||||||
"daisyui": "^5.5.18",
|
"daisyui": "^5.5.18",
|
||||||
|
"bullmq": "^5.58.2",
|
||||||
|
"ioredis": "^5.7.0",
|
||||||
"nuxt": "^4.3.1",
|
"nuxt": "^4.3.1",
|
||||||
"tailwindcss": "^4.1.18",
|
"tailwindcss": "^4.1.18",
|
||||||
"vue": "^3.5.27",
|
"vue": "^3.5.27",
|
||||||
"y-webrtc": "^10.3.0",
|
"y-webrtc": "^10.3.0",
|
||||||
"yjs": "^13.6.29"
|
"yjs": "^13.6.29",
|
||||||
|
"zod": "^4.1.5"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"prisma": "^6.16.1"
|
||||||
|
},
|
||||||
|
"prisma": {
|
||||||
|
"seed": "node prisma/seed.mjs"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
366
Frontend/prisma/schema.prisma
Normal file
366
Frontend/prisma/schema.prisma
Normal file
@@ -0,0 +1,366 @@
|
|||||||
|
generator client {
|
||||||
|
provider = "prisma-client-js"
|
||||||
|
}
|
||||||
|
|
||||||
|
datasource db {
|
||||||
|
provider = "sqlite"
|
||||||
|
url = env("DATABASE_URL")
|
||||||
|
}
|
||||||
|
|
||||||
|
enum TeamRole {
|
||||||
|
OWNER
|
||||||
|
MEMBER
|
||||||
|
}
|
||||||
|
|
||||||
|
enum MessageDirection {
|
||||||
|
IN
|
||||||
|
OUT
|
||||||
|
}
|
||||||
|
|
||||||
|
enum MessageChannel {
|
||||||
|
TELEGRAM
|
||||||
|
WHATSAPP
|
||||||
|
INSTAGRAM
|
||||||
|
PHONE
|
||||||
|
EMAIL
|
||||||
|
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
|
||||||
|
createdAt DateTime @default(now())
|
||||||
|
updatedAt DateTime @updatedAt
|
||||||
|
|
||||||
|
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 {
|
||||||
|
id String @id @default(cuid())
|
||||||
|
email String @unique
|
||||||
|
name String
|
||||||
|
createdAt DateTime @default(now())
|
||||||
|
updatedAt DateTime @updatedAt
|
||||||
|
|
||||||
|
memberships TeamMember[]
|
||||||
|
conversations ChatConversation[] @relation("ConversationCreator")
|
||||||
|
chatMessages ChatMessage[] @relation("ChatAuthor")
|
||||||
|
}
|
||||||
|
|
||||||
|
model TeamMember {
|
||||||
|
id String @id @default(cuid())
|
||||||
|
teamId String
|
||||||
|
userId String
|
||||||
|
role TeamRole @default(MEMBER)
|
||||||
|
createdAt DateTime @default(now())
|
||||||
|
|
||||||
|
team Team @relation(fields: [teamId], references: [id], onDelete: Cascade)
|
||||||
|
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||||
|
|
||||||
|
@@unique([teamId, userId])
|
||||||
|
@@index([userId])
|
||||||
|
}
|
||||||
|
|
||||||
|
model Contact {
|
||||||
|
id String @id @default(cuid())
|
||||||
|
teamId String
|
||||||
|
name String
|
||||||
|
company String?
|
||||||
|
country String?
|
||||||
|
location String?
|
||||||
|
avatarUrl String?
|
||||||
|
email String?
|
||||||
|
phone String?
|
||||||
|
createdAt DateTime @default(now())
|
||||||
|
updatedAt DateTime @updatedAt
|
||||||
|
|
||||||
|
team Team @relation(fields: [teamId], references: [id], onDelete: Cascade)
|
||||||
|
note ContactNote?
|
||||||
|
messages ContactMessage[]
|
||||||
|
events CalendarEvent[]
|
||||||
|
deals Deal[]
|
||||||
|
feedCards FeedCard[]
|
||||||
|
pins ContactPin[]
|
||||||
|
|
||||||
|
omniThreads OmniThread[]
|
||||||
|
omniMessages OmniMessage[]
|
||||||
|
omniIdentities OmniContactIdentity[]
|
||||||
|
|
||||||
|
@@index([teamId, updatedAt])
|
||||||
|
}
|
||||||
|
|
||||||
|
model ContactNote {
|
||||||
|
id String @id @default(cuid())
|
||||||
|
contactId String @unique
|
||||||
|
content String
|
||||||
|
createdAt DateTime @default(now())
|
||||||
|
updatedAt DateTime @updatedAt
|
||||||
|
|
||||||
|
contact Contact @relation(fields: [contactId], references: [id], onDelete: Cascade)
|
||||||
|
}
|
||||||
|
|
||||||
|
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())
|
||||||
|
|
||||||
|
contact Contact @relation(fields: [contactId], references: [id], onDelete: Cascade)
|
||||||
|
|
||||||
|
@@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
|
||||||
|
contactId String?
|
||||||
|
title String
|
||||||
|
startsAt DateTime
|
||||||
|
endsAt DateTime?
|
||||||
|
note String?
|
||||||
|
status 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([startsAt])
|
||||||
|
@@index([contactId, startsAt])
|
||||||
|
@@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
|
||||||
|
createdByUserId String
|
||||||
|
title String?
|
||||||
|
createdAt DateTime @default(now())
|
||||||
|
updatedAt DateTime @updatedAt
|
||||||
|
|
||||||
|
team Team @relation(fields: [teamId], references: [id], onDelete: Cascade)
|
||||||
|
createdByUser User @relation("ConversationCreator", fields: [createdByUserId], references: [id], onDelete: Cascade)
|
||||||
|
messages ChatMessage[]
|
||||||
|
|
||||||
|
@@index([teamId, updatedAt])
|
||||||
|
@@index([createdByUserId])
|
||||||
|
}
|
||||||
|
|
||||||
|
model ChatMessage {
|
||||||
|
id String @id @default(cuid())
|
||||||
|
teamId String
|
||||||
|
conversationId String
|
||||||
|
authorUserId String?
|
||||||
|
role ChatRole
|
||||||
|
text String
|
||||||
|
planJson Json?
|
||||||
|
createdAt DateTime @default(now())
|
||||||
|
|
||||||
|
team Team @relation(fields: [teamId], references: [id], onDelete: Cascade)
|
||||||
|
conversation ChatConversation @relation(fields: [conversationId], references: [id], onDelete: Cascade)
|
||||||
|
authorUser User? @relation("ChatAuthor", fields: [authorUserId], references: [id], onDelete: SetNull)
|
||||||
|
|
||||||
|
@@index([createdAt])
|
||||||
|
@@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])
|
||||||
|
}
|
||||||
353
Frontend/prisma/seed.mjs
Normal file
353
Frontend/prisma/seed.mjs
Normal file
@@ -0,0 +1,353 @@
|
|||||||
|
import { PrismaClient } from "@prisma/client";
|
||||||
|
import fs from "node:fs";
|
||||||
|
import path from "node:path";
|
||||||
|
|
||||||
|
function loadEnvFromDotEnv() {
|
||||||
|
const p = path.resolve(process.cwd(), ".env");
|
||||||
|
if (!fs.existsSync(p)) return;
|
||||||
|
const raw = fs.readFileSync(p, "utf8");
|
||||||
|
for (const line of raw.split("\n")) {
|
||||||
|
const trimmed = line.trim();
|
||||||
|
if (!trimmed || trimmed.startsWith("#")) continue;
|
||||||
|
const idx = trimmed.indexOf("=");
|
||||||
|
if (idx === -1) continue;
|
||||||
|
const key = trimmed.slice(0, idx).trim();
|
||||||
|
let val = trimmed.slice(idx + 1).trim();
|
||||||
|
if ((val.startsWith('"') && val.endsWith('"')) || (val.startsWith("'") && val.endsWith("'"))) {
|
||||||
|
val = val.slice(1, -1);
|
||||||
|
}
|
||||||
|
if (!key) continue;
|
||||||
|
// Force DATABASE_URL from local .env for scripts, to avoid inheriting a stale shell env.
|
||||||
|
if (key === "DATABASE_URL") {
|
||||||
|
process.env[key] = val;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!process.env[key]) process.env[key] = val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
loadEnvFromDotEnv();
|
||||||
|
|
||||||
|
const prisma = new PrismaClient();
|
||||||
|
|
||||||
|
function atOffset(days, hour, minute) {
|
||||||
|
const d = new Date();
|
||||||
|
d.setDate(d.getDate() + days);
|
||||||
|
d.setHours(hour, minute, 0, 0);
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function main() {
|
||||||
|
// Create default team/user for dev.
|
||||||
|
const user = await prisma.user.upsert({
|
||||||
|
where: { id: "demo-user" },
|
||||||
|
update: { email: "demo@clientsflow.local", name: "Demo User" },
|
||||||
|
create: { id: "demo-user", email: "demo@clientsflow.local", name: "Demo User" },
|
||||||
|
});
|
||||||
|
|
||||||
|
const team = await prisma.team.upsert({
|
||||||
|
where: { id: "demo-team" },
|
||||||
|
update: { name: "Demo Team" },
|
||||||
|
create: { id: "demo-team", name: "Demo Team" },
|
||||||
|
});
|
||||||
|
|
||||||
|
await prisma.teamMember.upsert({
|
||||||
|
where: { teamId_userId: { teamId: team.id, userId: user.id } },
|
||||||
|
update: {},
|
||||||
|
create: { teamId: team.id, userId: user.id, role: "OWNER" },
|
||||||
|
});
|
||||||
|
|
||||||
|
// Idempotent-ish seed per team: if we already have contacts in this team, do nothing.
|
||||||
|
const existing = await prisma.contact.count({ where: { teamId: team.id } });
|
||||||
|
if (existing > 0) return;
|
||||||
|
|
||||||
|
const contacts = await prisma.contact.createManyAndReturn({
|
||||||
|
data: [
|
||||||
|
{
|
||||||
|
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",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
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",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
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",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
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",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
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",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
const byName = Object.fromEntries(contacts.map((c) => [c.name, c]));
|
||||||
|
|
||||||
|
await prisma.contactNote.createMany({
|
||||||
|
data: [
|
||||||
|
{
|
||||||
|
contactId: byName["Anna Meyer"].id,
|
||||||
|
content:
|
||||||
|
"Decision owner. Prefers short, concrete updates with a clear next step.\nRisk: decision date slips if we don't lock timeline.",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
contactId: byName["Murat Ali"].id,
|
||||||
|
content:
|
||||||
|
"High activity. Needs legal path clarity and an explicit owner on their side.\nBest move: lock legal owner + target signature date.",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
contactId: byName["Ilya Petroff"].id,
|
||||||
|
content:
|
||||||
|
"Early-stage. Wants structured onboarding before commercial details.\nBest move: onboarding plan + 2 time slots.",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
await prisma.contactMessage.createMany({
|
||||||
|
data: [
|
||||||
|
{
|
||||||
|
contactId: byName["Anna Meyer"].id,
|
||||||
|
kind: "MESSAGE",
|
||||||
|
direction: "IN",
|
||||||
|
channel: "TELEGRAM",
|
||||||
|
content: "Thanks for the demo. Can you send 2 pricing options?",
|
||||||
|
occurredAt: atOffset(0, 10, 20),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
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?",
|
||||||
|
occurredAt: atOffset(0, 10, 35),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
contactId: byName["Murat Ali"].id,
|
||||||
|
kind: "MESSAGE",
|
||||||
|
direction: "IN",
|
||||||
|
channel: "WHATSAPP",
|
||||||
|
content: "Let's do a quick call. Need to clarify legal owner.",
|
||||||
|
occurredAt: atOffset(-1, 18, 10),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
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),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
await prisma.calendarEvent.createMany({
|
||||||
|
data: [
|
||||||
|
{
|
||||||
|
teamId: team.id,
|
||||||
|
contactId: byName["Anna Meyer"].id,
|
||||||
|
title: "Follow-up: Anna",
|
||||||
|
startsAt: atOffset(0, 12, 30),
|
||||||
|
endsAt: atOffset(0, 13, 0),
|
||||||
|
note: "Lock decision date + confirm option A/B.",
|
||||||
|
status: "planned",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
teamId: team.id,
|
||||||
|
contactId: byName["Murat Ali"].id,
|
||||||
|
title: "Call: Murat (legal owner)",
|
||||||
|
startsAt: atOffset(0, 15, 0),
|
||||||
|
endsAt: atOffset(0, 15, 20),
|
||||||
|
note: "Confirm legal owner + target signature date.",
|
||||||
|
status: "planned",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
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: {},
|
||||||
|
create: {
|
||||||
|
id: `pilot-${team.id}`,
|
||||||
|
teamId: team.id,
|
||||||
|
createdByUserId: user.id,
|
||||||
|
title: "Pilot",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
await prisma.chatMessage.createMany({
|
||||||
|
data: [
|
||||||
|
{
|
||||||
|
teamId: team.id,
|
||||||
|
conversationId: `pilot-${team.id}`,
|
||||||
|
authorUserId: null,
|
||||||
|
role: "ASSISTANT",
|
||||||
|
text:
|
||||||
|
"Я смотрю календарь, контакты и переписки как один поток. Спроси: \"чем заняться сегодня\" или \"покажи 10 лучших клиентов\".",
|
||||||
|
planJson: { steps: ["Скажи задачу", "Я соберу срез данных", "Предложу план и действия"], tools: ["read index/contacts.json"] },
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
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()
|
||||||
|
.catch((e) => {
|
||||||
|
console.error(e);
|
||||||
|
process.exitCode = 1;
|
||||||
|
})
|
||||||
|
.finally(async () => {
|
||||||
|
await prisma.$disconnect();
|
||||||
|
});
|
||||||
182
Frontend/scripts/export-dataset.mjs
Normal file
182
Frontend/scripts/export-dataset.mjs
Normal file
@@ -0,0 +1,182 @@
|
|||||||
|
import fs from "node:fs/promises";
|
||||||
|
import fsSync from "node:fs";
|
||||||
|
import path from "node:path";
|
||||||
|
import { PrismaClient } from "@prisma/client";
|
||||||
|
|
||||||
|
function loadEnvFromDotEnv() {
|
||||||
|
const p = path.resolve(process.cwd(), ".env");
|
||||||
|
if (!fsSync.existsSync(p)) return;
|
||||||
|
const raw = fsSync.readFileSync(p, "utf8");
|
||||||
|
for (const line of raw.split("\n")) {
|
||||||
|
const trimmed = line.trim();
|
||||||
|
if (!trimmed || trimmed.startsWith("#")) continue;
|
||||||
|
const idx = trimmed.indexOf("=");
|
||||||
|
if (idx === -1) continue;
|
||||||
|
const key = trimmed.slice(0, idx).trim();
|
||||||
|
let val = trimmed.slice(idx + 1).trim();
|
||||||
|
if ((val.startsWith('"') && val.endsWith('"')) || (val.startsWith("'") && val.endsWith("'"))) {
|
||||||
|
val = val.slice(1, -1);
|
||||||
|
}
|
||||||
|
if (!key) continue;
|
||||||
|
if (key === "DATABASE_URL") {
|
||||||
|
process.env[key] = val;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!process.env[key]) process.env[key] = val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
loadEnvFromDotEnv();
|
||||||
|
|
||||||
|
const prisma = new PrismaClient();
|
||||||
|
|
||||||
|
function datasetRoot() {
|
||||||
|
const teamId = process.env.TEAM_ID || "demo-team";
|
||||||
|
const userId = process.env.USER_ID || "demo-user";
|
||||||
|
return path.resolve(process.cwd(), "..", ".data", "crmfs", "teams", teamId, "users", userId);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function ensureDir(p) {
|
||||||
|
await fs.mkdir(p, { recursive: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
async function writeJson(p, value) {
|
||||||
|
await fs.writeFile(p, JSON.stringify(value, null, 2) + "\n", "utf8");
|
||||||
|
}
|
||||||
|
|
||||||
|
function jsonlLine(value) {
|
||||||
|
return JSON.stringify(value) + "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
async function main() {
|
||||||
|
const root = datasetRoot();
|
||||||
|
const tmp = root + ".tmp";
|
||||||
|
|
||||||
|
await fs.rm(tmp, { recursive: true, force: true });
|
||||||
|
await ensureDir(tmp);
|
||||||
|
|
||||||
|
const contactsDir = path.join(tmp, "contacts");
|
||||||
|
const notesDir = path.join(tmp, "notes");
|
||||||
|
const messagesDir = path.join(tmp, "messages");
|
||||||
|
const eventsDir = path.join(tmp, "events");
|
||||||
|
const indexDir = path.join(tmp, "index");
|
||||||
|
await Promise.all([
|
||||||
|
ensureDir(contactsDir),
|
||||||
|
ensureDir(notesDir),
|
||||||
|
ensureDir(messagesDir),
|
||||||
|
ensureDir(eventsDir),
|
||||||
|
ensureDir(indexDir),
|
||||||
|
]);
|
||||||
|
|
||||||
|
const teamId = process.env.TEAM_ID || "demo-team";
|
||||||
|
const contacts = await prisma.contact.findMany({
|
||||||
|
where: { teamId },
|
||||||
|
orderBy: { updatedAt: "desc" },
|
||||||
|
include: {
|
||||||
|
note: { select: { content: true, updatedAt: true } },
|
||||||
|
messages: {
|
||||||
|
select: {
|
||||||
|
kind: true,
|
||||||
|
direction: true,
|
||||||
|
channel: true,
|
||||||
|
content: true,
|
||||||
|
durationSec: true,
|
||||||
|
transcriptJson: true,
|
||||||
|
occurredAt: true,
|
||||||
|
},
|
||||||
|
orderBy: { occurredAt: "asc" },
|
||||||
|
},
|
||||||
|
events: {
|
||||||
|
select: { title: true, startsAt: true, endsAt: true, status: true, note: true },
|
||||||
|
orderBy: { startsAt: "asc" },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
take: 5000,
|
||||||
|
});
|
||||||
|
|
||||||
|
const contactIndex = [];
|
||||||
|
for (const c of contacts) {
|
||||||
|
await writeJson(path.join(contactsDir, `${c.id}.json`), {
|
||||||
|
id: c.id,
|
||||||
|
teamId: c.teamId,
|
||||||
|
name: c.name,
|
||||||
|
company: c.company ?? null,
|
||||||
|
country: c.country ?? null,
|
||||||
|
location: c.location ?? null,
|
||||||
|
avatarUrl: c.avatarUrl ?? null,
|
||||||
|
email: c.email ?? null,
|
||||||
|
phone: c.phone ?? null,
|
||||||
|
createdAt: c.createdAt,
|
||||||
|
updatedAt: c.updatedAt,
|
||||||
|
});
|
||||||
|
|
||||||
|
await fs.writeFile(
|
||||||
|
path.join(notesDir, `${c.id}.md`),
|
||||||
|
(c.note?.content?.trim() ? c.note.content.trim() : "") + "\n",
|
||||||
|
"utf8",
|
||||||
|
);
|
||||||
|
|
||||||
|
await fs.writeFile(
|
||||||
|
path.join(messagesDir, `${c.id}.jsonl`),
|
||||||
|
c.messages
|
||||||
|
.map((m) =>
|
||||||
|
jsonlLine({
|
||||||
|
kind: m.kind,
|
||||||
|
direction: m.direction,
|
||||||
|
channel: m.channel,
|
||||||
|
occurredAt: m.occurredAt,
|
||||||
|
content: m.content,
|
||||||
|
durationSec: m.durationSec ?? null,
|
||||||
|
transcript: m.transcriptJson ?? null,
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
.join(""),
|
||||||
|
"utf8",
|
||||||
|
);
|
||||||
|
|
||||||
|
await fs.writeFile(
|
||||||
|
path.join(eventsDir, `${c.id}.jsonl`),
|
||||||
|
c.events
|
||||||
|
.map((e) =>
|
||||||
|
jsonlLine({
|
||||||
|
title: e.title,
|
||||||
|
startsAt: e.startsAt,
|
||||||
|
endsAt: e.endsAt,
|
||||||
|
status: e.status ?? null,
|
||||||
|
note: e.note ?? null,
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
.join(""),
|
||||||
|
"utf8",
|
||||||
|
);
|
||||||
|
|
||||||
|
const lastMessageAt = c.messages.length ? c.messages[c.messages.length - 1].occurredAt : null;
|
||||||
|
const nextEventAt = c.events.find((e) => new Date(e.startsAt).getTime() >= Date.now())?.startsAt ?? null;
|
||||||
|
contactIndex.push({
|
||||||
|
id: c.id,
|
||||||
|
name: c.name,
|
||||||
|
company: c.company ?? null,
|
||||||
|
lastMessageAt,
|
||||||
|
nextEventAt,
|
||||||
|
updatedAt: c.updatedAt,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
await writeJson(path.join(indexDir, "contacts.json"), contactIndex);
|
||||||
|
await writeJson(path.join(tmp, "meta.json"), { exportedAt: new Date().toISOString(), version: 1 });
|
||||||
|
|
||||||
|
await ensureDir(path.dirname(root));
|
||||||
|
await fs.rm(root, { recursive: true, force: true });
|
||||||
|
await fs.rename(tmp, root);
|
||||||
|
|
||||||
|
console.log("exported", root);
|
||||||
|
}
|
||||||
|
|
||||||
|
await main()
|
||||||
|
.catch((e) => {
|
||||||
|
console.error(e);
|
||||||
|
process.exitCode = 1;
|
||||||
|
})
|
||||||
|
.finally(async () => {
|
||||||
|
await prisma.$disconnect();
|
||||||
|
});
|
||||||
201
Frontend/server/agent/crmAgent.ts
Normal file
201
Frontend/server/agent/crmAgent.ts
Normal file
@@ -0,0 +1,201 @@
|
|||||||
|
import fs from "node:fs/promises";
|
||||||
|
import path from "node:path";
|
||||||
|
import type { ChatRole, Prisma } from "@prisma/client";
|
||||||
|
import { prisma } from "../utils/prisma";
|
||||||
|
import { datasetRoot } from "../dataset/paths";
|
||||||
|
import { ensureDataset } from "../dataset/exporter";
|
||||||
|
import { runLangGraphCrmAgentFor } from "./langgraphCrmAgent";
|
||||||
|
|
||||||
|
type ContactIndexRow = {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
company: string | null;
|
||||||
|
lastMessageAt: string | null;
|
||||||
|
nextEventAt: string | null;
|
||||||
|
updatedAt: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type AgentReply = {
|
||||||
|
text: string;
|
||||||
|
plan: string[];
|
||||||
|
tools: string[];
|
||||||
|
dbWrites?: Array<{ kind: string; detail: string }>;
|
||||||
|
};
|
||||||
|
|
||||||
|
function normalize(s: string) {
|
||||||
|
return s.trim().toLowerCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
function isToday(date: Date) {
|
||||||
|
const now = new Date();
|
||||||
|
return (
|
||||||
|
date.getFullYear() === now.getFullYear() &&
|
||||||
|
date.getMonth() === now.getMonth() &&
|
||||||
|
date.getDate() === now.getDate()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function readContactIndex(): Promise<ContactIndexRow[]> {
|
||||||
|
throw new Error("readContactIndex now requires dataset root");
|
||||||
|
}
|
||||||
|
|
||||||
|
async function readContactIndexFrom(root: string): Promise<ContactIndexRow[]> {
|
||||||
|
const p = path.join(root, "index", "contacts.json");
|
||||||
|
const raw = await fs.readFile(p, "utf8");
|
||||||
|
return JSON.parse(raw);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function countJsonlLines(p: string): Promise<number> {
|
||||||
|
const raw = await fs.readFile(p, "utf8");
|
||||||
|
if (!raw.trim()) return 0;
|
||||||
|
// cheap line count (JSONL is 1 item per line)
|
||||||
|
return raw.trimEnd().split("\n").length;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function readJsonl(p: string): Promise<any[]> {
|
||||||
|
const raw = await fs.readFile(p, "utf8");
|
||||||
|
if (!raw.trim()) return [];
|
||||||
|
return raw
|
||||||
|
.trimEnd()
|
||||||
|
.split("\n")
|
||||||
|
.filter(Boolean)
|
||||||
|
.map((line) => JSON.parse(line));
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatContactLine(c: ContactIndexRow) {
|
||||||
|
const company = c.company ? ` (${c.company})` : "";
|
||||||
|
const lastAt = c.lastMessageAt ? new Date(c.lastMessageAt).toLocaleString("ru-RU") : "нет";
|
||||||
|
return `- ${c.name}${company} · последнее: ${lastAt}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function runCrmAgent(userText: string): Promise<AgentReply> {
|
||||||
|
throw new Error("runCrmAgent now requires auth context");
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function runCrmAgentFor(
|
||||||
|
input: { teamId: string; userId: string; userText: string },
|
||||||
|
): Promise<AgentReply> {
|
||||||
|
const mode = (process.env.CF_AGENT_MODE ?? "langgraph").toLowerCase();
|
||||||
|
if (mode !== "rule" && process.env.OPENAI_API_KEY) {
|
||||||
|
return runLangGraphCrmAgentFor(input);
|
||||||
|
}
|
||||||
|
|
||||||
|
await ensureDataset({ teamId: input.teamId, userId: input.userId });
|
||||||
|
const q = normalize(input.userText);
|
||||||
|
const root = datasetRoot({ teamId: input.teamId, userId: input.userId });
|
||||||
|
const contacts = await readContactIndexFrom(root);
|
||||||
|
|
||||||
|
// "10 лучших клиентов"
|
||||||
|
if (q.includes("10 лучших") || (q.includes("топ") && q.includes("клиент"))) {
|
||||||
|
const ranked = await Promise.all(
|
||||||
|
contacts.map(async (c) => {
|
||||||
|
const msgPath = path.join(root, "messages", `${c.id}.jsonl`);
|
||||||
|
const evPath = path.join(root, "events", `${c.id}.jsonl`);
|
||||||
|
const msgCount = await countJsonlLines(msgPath).catch(() => 0);
|
||||||
|
const ev = await readJsonl(evPath).catch(() => []);
|
||||||
|
const todayEvCount = ev.filter((e) => (e?.startsAt ? isToday(new Date(e.startsAt)) : false)).length;
|
||||||
|
const score = msgCount * 2 + todayEvCount * 3;
|
||||||
|
return { c, score };
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
ranked.sort((a, b) => b.score - a.score);
|
||||||
|
const top = ranked.slice(0, 10).map((x) => x.c);
|
||||||
|
|
||||||
|
return {
|
||||||
|
plan: [
|
||||||
|
"Загрузить индекс контактов из файлового датасета",
|
||||||
|
"Посчитать активность по JSONL (сообщения/события сегодня)",
|
||||||
|
"Отсортировать и показать топ",
|
||||||
|
],
|
||||||
|
tools: ["read index/contacts.json", "read messages/{contactId}.jsonl", "read events/{contactId}.jsonl"],
|
||||||
|
text:
|
||||||
|
`Топ-10 по активности (сообщения + события):\n` +
|
||||||
|
top.map(formatContactLine).join("\n") +
|
||||||
|
`\n\nЕсли хочешь, скажи критерий "лучший" (выручка/стадия/вероятность/давность) и я пересчитаю.`,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// "чем заняться сегодня"
|
||||||
|
if (q.includes("чем") && (q.includes("сегодня") || q.includes("заняться"))) {
|
||||||
|
const todayEvents: Array<{ who: string; title: string; at: Date; note?: string | null }> = [];
|
||||||
|
|
||||||
|
for (const c of contacts) {
|
||||||
|
const evPath = path.join(root, "events", `${c.id}.jsonl`);
|
||||||
|
const ev = await readJsonl(evPath).catch(() => []);
|
||||||
|
for (const e of ev) {
|
||||||
|
if (!e?.startsAt) continue;
|
||||||
|
const at = new Date(e.startsAt);
|
||||||
|
if (!isToday(at)) continue;
|
||||||
|
todayEvents.push({ who: c.name, title: e.title ?? "Event", at, note: e.note ?? null });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
todayEvents.sort((a, b) => a.at.getTime() - b.at.getTime());
|
||||||
|
|
||||||
|
const followups = [...contacts]
|
||||||
|
.map((c) => ({ c, last: c.lastMessageAt ? new Date(c.lastMessageAt).getTime() : 0 }))
|
||||||
|
.sort((a, b) => a.last - b.last)
|
||||||
|
.slice(0, 3)
|
||||||
|
.map((x) => x.c);
|
||||||
|
|
||||||
|
const lines: string[] = [];
|
||||||
|
if (todayEvents.length > 0) {
|
||||||
|
lines.push("Сегодня по календарю:");
|
||||||
|
for (const e of todayEvents) {
|
||||||
|
const hhmm = e.at.toLocaleTimeString("ru-RU", { hour: "2-digit", minute: "2-digit" });
|
||||||
|
lines.push(`- ${hhmm} · ${e.title} · ${e.who}${e.note ? ` · ${e.note}` : ""}`);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
lines.push("Сегодня нет запланированных событий в календаре.");
|
||||||
|
}
|
||||||
|
|
||||||
|
lines.push("");
|
||||||
|
lines.push("Фокус дня (если нужно добить прогресс):");
|
||||||
|
for (const c of followups) {
|
||||||
|
lines.push(`- Написать follow-up: ${c.name}${c.company ? ` (${c.company})` : ""}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
plan: [
|
||||||
|
"Прочитать события на сегодня из файлового датасета",
|
||||||
|
"Найти контакты без свежего касания (по lastMessageAt)",
|
||||||
|
"Сформировать короткий список действий",
|
||||||
|
],
|
||||||
|
tools: ["read index/contacts.json", "read events/{contactId}.jsonl"],
|
||||||
|
text: lines.join("\n"),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Default: keep it simple, ask for intent + show what the agent can do.
|
||||||
|
return {
|
||||||
|
plan: ["Уточнить цель", "Выбрать данные для анализа", "Предложить план действий и, если нужно, изменения в CRM"],
|
||||||
|
tools: ["read index/contacts.json (по необходимости)", "search messages/events (по необходимости)"],
|
||||||
|
text:
|
||||||
|
"Ок. Скажи, что нужно сделать.\n" +
|
||||||
|
"Примеры:\n" +
|
||||||
|
"- \"покажи 10 лучших клиентов\"\n" +
|
||||||
|
"- \"чем мне сегодня заняться\"\n" +
|
||||||
|
"- \"составь план касаний на неделю\"\n",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function persistChatMessage(input: {
|
||||||
|
role: ChatRole;
|
||||||
|
text: string;
|
||||||
|
plan?: string[];
|
||||||
|
tools?: string[];
|
||||||
|
teamId: string;
|
||||||
|
conversationId: string;
|
||||||
|
authorUserId?: string | null;
|
||||||
|
}) {
|
||||||
|
const data: Prisma.ChatMessageCreateInput = {
|
||||||
|
team: { connect: { id: input.teamId } },
|
||||||
|
conversation: { connect: { id: input.conversationId } },
|
||||||
|
authorUser: input.authorUserId ? { connect: { id: input.authorUserId } } : undefined,
|
||||||
|
role: input.role,
|
||||||
|
text: input.text,
|
||||||
|
planJson: input.plan || input.tools ? ({ steps: input.plan ?? [], tools: input.tools ?? [] } as any) : undefined,
|
||||||
|
};
|
||||||
|
return prisma.chatMessage.create({ data });
|
||||||
|
}
|
||||||
375
Frontend/server/agent/langgraphCrmAgent.ts
Normal file
375
Frontend/server/agent/langgraphCrmAgent.ts
Normal file
@@ -0,0 +1,375 @@
|
|||||||
|
import type { AgentReply } from "./crmAgent";
|
||||||
|
import { prisma } from "../utils/prisma";
|
||||||
|
import { ensureDataset } from "../dataset/exporter";
|
||||||
|
import { createReactAgent } from "@langchain/langgraph/prebuilt";
|
||||||
|
import { ChatOpenAI } from "@langchain/openai";
|
||||||
|
import { tool } from "@langchain/core/tools";
|
||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
|
function iso(d: Date) {
|
||||||
|
return d.toISOString();
|
||||||
|
}
|
||||||
|
|
||||||
|
async function buildCrmSnapshot(input: { teamId: string }) {
|
||||||
|
const now = new Date();
|
||||||
|
const in7 = new Date(now.getTime() + 7 * 24 * 60 * 60 * 1000);
|
||||||
|
|
||||||
|
const [contacts, upcoming, deals] = await Promise.all([
|
||||||
|
prisma.contact.findMany({
|
||||||
|
where: { teamId: input.teamId },
|
||||||
|
orderBy: { updatedAt: "desc" },
|
||||||
|
take: 25,
|
||||||
|
include: {
|
||||||
|
messages: { select: { occurredAt: true, channel: true, direction: true }, orderBy: { occurredAt: "desc" }, take: 1 },
|
||||||
|
deals: { select: { stage: true, amount: true, updatedAt: true }, orderBy: { updatedAt: "desc" }, take: 1 },
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
prisma.calendarEvent.findMany({
|
||||||
|
where: { teamId: input.teamId, startsAt: { gte: now, lte: in7 } },
|
||||||
|
orderBy: { startsAt: "asc" },
|
||||||
|
take: 20,
|
||||||
|
include: { contact: { select: { name: true } } },
|
||||||
|
}),
|
||||||
|
prisma.deal.findMany({
|
||||||
|
where: { teamId: input.teamId },
|
||||||
|
orderBy: { updatedAt: "desc" },
|
||||||
|
take: 20,
|
||||||
|
include: { contact: { select: { name: true, company: true } } },
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
|
||||||
|
const byStage = new Map<string, number>();
|
||||||
|
for (const d of deals) byStage.set(d.stage, (byStage.get(d.stage) ?? 0) + 1);
|
||||||
|
|
||||||
|
const lines: string[] = [];
|
||||||
|
lines.push(`Snapshot time: ${iso(now)}`);
|
||||||
|
lines.push(`Contacts: ${await prisma.contact.count({ where: { teamId: input.teamId } })}`);
|
||||||
|
lines.push(`Deals: ${await prisma.deal.count({ where: { teamId: input.teamId } })}`);
|
||||||
|
lines.push(`Upcoming events (7d): ${upcoming.length}`);
|
||||||
|
lines.push("");
|
||||||
|
|
||||||
|
if (upcoming.length) {
|
||||||
|
lines.push("Upcoming events:");
|
||||||
|
for (const e of upcoming) {
|
||||||
|
lines.push(`- ${e.startsAt.toISOString()} · ${e.title} · ${e.contact?.name ?? "No contact"}`);
|
||||||
|
}
|
||||||
|
lines.push("");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (byStage.size) {
|
||||||
|
lines.push("Deals by stage:");
|
||||||
|
for (const [stage, n] of [...byStage.entries()].sort((a, b) => b[1] - a[1])) {
|
||||||
|
lines.push(`- ${stage}: ${n}`);
|
||||||
|
}
|
||||||
|
lines.push("");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (contacts.length) {
|
||||||
|
lines.push("Recently updated contacts:");
|
||||||
|
for (const c of contacts.slice(0, 12)) {
|
||||||
|
const last = c.messages[0]?.occurredAt ? c.messages[0].occurredAt.toISOString() : c.updatedAt.toISOString();
|
||||||
|
const deal = c.deals[0] ? `${c.deals[0].stage}${c.deals[0].amount ? ` $${c.deals[0].amount}` : ""}` : "no deal";
|
||||||
|
lines.push(`- ${c.name}${c.company ? ` (${c.company})` : ""} · last touch ${last} · ${deal}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return lines.join("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function runLangGraphCrmAgentFor(input: {
|
||||||
|
teamId: string;
|
||||||
|
userId: string;
|
||||||
|
userText: string;
|
||||||
|
}): Promise<AgentReply> {
|
||||||
|
if (!process.env.OPENAI_API_KEY) {
|
||||||
|
return {
|
||||||
|
text: "OPENAI_API_KEY не задан. Сейчас включен fallback-агент без LLM.",
|
||||||
|
plan: ["Проверить .env", "Добавить OPENAI_API_KEY", "Перезапустить dev-сервер"],
|
||||||
|
tools: [],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Keep the dataset fresh so the "CRM filesystem" stays in sync with DB.
|
||||||
|
await ensureDataset({ teamId: input.teamId, userId: input.userId });
|
||||||
|
|
||||||
|
const toolsUsed: string[] = [];
|
||||||
|
const dbWrites: Array<{ kind: string; detail: string }> = [];
|
||||||
|
|
||||||
|
const CrmToolSchema = z.object({
|
||||||
|
action: z.enum([
|
||||||
|
"query_contacts",
|
||||||
|
"query_deals",
|
||||||
|
"query_events",
|
||||||
|
"update_contact_note",
|
||||||
|
"create_event",
|
||||||
|
"create_message",
|
||||||
|
"update_deal_stage",
|
||||||
|
]),
|
||||||
|
// queries
|
||||||
|
query: z.string().optional(),
|
||||||
|
stage: z.string().optional(),
|
||||||
|
from: z.string().optional(),
|
||||||
|
to: z.string().optional(),
|
||||||
|
limit: z.number().int().optional(),
|
||||||
|
// writes
|
||||||
|
contact: z.string().optional(),
|
||||||
|
content: z.string().optional(),
|
||||||
|
title: z.string().optional(),
|
||||||
|
start: z.string().optional(),
|
||||||
|
end: z.string().optional(),
|
||||||
|
note: z.string().optional(),
|
||||||
|
status: z.string().optional(),
|
||||||
|
channel: z.enum(["Telegram", "WhatsApp", "Instagram", "Phone", "Email"]).optional(),
|
||||||
|
kind: z.enum(["message", "call"]).optional(),
|
||||||
|
direction: z.enum(["in", "out"]).optional(),
|
||||||
|
text: z.string().optional(),
|
||||||
|
at: z.string().optional(),
|
||||||
|
durationSec: z.number().int().optional(),
|
||||||
|
transcript: z.array(z.string()).optional(),
|
||||||
|
dealId: z.string().optional(),
|
||||||
|
});
|
||||||
|
|
||||||
|
const crmTool = tool(
|
||||||
|
async (raw: z.infer<typeof CrmToolSchema>) => {
|
||||||
|
toolsUsed.push(`crm:${raw.action}`);
|
||||||
|
|
||||||
|
if (raw.action === "query_contacts") {
|
||||||
|
const q = (raw.query ?? "").trim();
|
||||||
|
const items = await prisma.contact.findMany({
|
||||||
|
where: {
|
||||||
|
teamId: input.teamId,
|
||||||
|
...(q
|
||||||
|
? {
|
||||||
|
OR: [
|
||||||
|
{ name: { contains: q } },
|
||||||
|
{ company: { contains: q } },
|
||||||
|
{ email: { contains: q } },
|
||||||
|
{ phone: { contains: q } },
|
||||||
|
],
|
||||||
|
}
|
||||||
|
: {}),
|
||||||
|
},
|
||||||
|
orderBy: { updatedAt: "desc" },
|
||||||
|
take: Math.max(1, Math.min(raw.limit ?? 20, 100)),
|
||||||
|
include: { note: { select: { content: true, updatedAt: true } } },
|
||||||
|
});
|
||||||
|
return JSON.stringify(
|
||||||
|
items.map((c) => ({
|
||||||
|
id: c.id,
|
||||||
|
name: c.name,
|
||||||
|
company: c.company,
|
||||||
|
country: c.country,
|
||||||
|
location: c.location,
|
||||||
|
email: c.email,
|
||||||
|
phone: c.phone,
|
||||||
|
note: c.note?.content ?? null,
|
||||||
|
})),
|
||||||
|
null,
|
||||||
|
2,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (raw.action === "query_deals") {
|
||||||
|
const items = await prisma.deal.findMany({
|
||||||
|
where: { teamId: input.teamId, ...(raw.stage ? { stage: raw.stage } : {}) },
|
||||||
|
orderBy: { updatedAt: "desc" },
|
||||||
|
take: Math.max(1, Math.min(raw.limit ?? 20, 100)),
|
||||||
|
include: { contact: { select: { name: true, company: true } } },
|
||||||
|
});
|
||||||
|
return JSON.stringify(
|
||||||
|
items.map((d) => ({
|
||||||
|
id: d.id,
|
||||||
|
title: d.title,
|
||||||
|
stage: d.stage,
|
||||||
|
amount: d.amount,
|
||||||
|
nextStep: d.nextStep,
|
||||||
|
summary: d.summary,
|
||||||
|
contact: d.contact.name,
|
||||||
|
company: d.contact.company,
|
||||||
|
})),
|
||||||
|
null,
|
||||||
|
2,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (raw.action === "query_events") {
|
||||||
|
const from = raw.from ? new Date(raw.from) : new Date(Date.now() - 30 * 24 * 60 * 60 * 1000);
|
||||||
|
const to = raw.to ? new Date(raw.to) : new Date(Date.now() + 60 * 24 * 60 * 60 * 1000);
|
||||||
|
const items = await prisma.calendarEvent.findMany({
|
||||||
|
where: { teamId: input.teamId, startsAt: { gte: from, lte: to } },
|
||||||
|
orderBy: { startsAt: "asc" },
|
||||||
|
take: Math.max(1, Math.min(raw.limit ?? 100, 500)),
|
||||||
|
include: { contact: { select: { name: true } } },
|
||||||
|
});
|
||||||
|
return JSON.stringify(
|
||||||
|
items.map((e) => ({
|
||||||
|
id: e.id,
|
||||||
|
title: e.title,
|
||||||
|
startsAt: e.startsAt.toISOString(),
|
||||||
|
endsAt: (e.endsAt ?? e.startsAt).toISOString(),
|
||||||
|
note: e.note,
|
||||||
|
contact: e.contact?.name ?? null,
|
||||||
|
})),
|
||||||
|
null,
|
||||||
|
2,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (raw.action === "update_contact_note") {
|
||||||
|
const contactName = (raw.contact ?? "").trim();
|
||||||
|
const content = (raw.content ?? "").trim();
|
||||||
|
if (!contactName) throw new Error("contact is required");
|
||||||
|
if (!content) throw new Error("content is required");
|
||||||
|
|
||||||
|
const contact = await prisma.contact.findFirst({
|
||||||
|
where: { teamId: input.teamId, name: contactName },
|
||||||
|
select: { id: true },
|
||||||
|
});
|
||||||
|
if (!contact) throw new Error("contact not found");
|
||||||
|
|
||||||
|
await prisma.contactNote.upsert({
|
||||||
|
where: { contactId: contact.id },
|
||||||
|
update: { content },
|
||||||
|
create: { contactId: contact.id, content },
|
||||||
|
});
|
||||||
|
dbWrites.push({ kind: "contact_note", detail: `${contactName}: updated` });
|
||||||
|
return JSON.stringify({ ok: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (raw.action === "create_event") {
|
||||||
|
const title = (raw.title ?? "").trim();
|
||||||
|
const start = raw.start ? new Date(raw.start) : null;
|
||||||
|
if (!title) throw new Error("title is required");
|
||||||
|
if (!start || Number.isNaN(start.getTime())) throw new Error("start is invalid");
|
||||||
|
|
||||||
|
const end = raw.end ? new Date(raw.end) : null;
|
||||||
|
const contactName = (raw.contact ?? "").trim();
|
||||||
|
const contact = contactName
|
||||||
|
? await prisma.contact.findFirst({ where: { teamId: input.teamId, name: contactName }, select: { id: true } })
|
||||||
|
: null;
|
||||||
|
|
||||||
|
const created = await prisma.calendarEvent.create({
|
||||||
|
data: {
|
||||||
|
teamId: input.teamId,
|
||||||
|
contactId: contact?.id ?? null,
|
||||||
|
title,
|
||||||
|
startsAt: start,
|
||||||
|
endsAt: end && !Number.isNaN(end.getTime()) ? end : null,
|
||||||
|
note: (raw.note ?? "").trim() || null,
|
||||||
|
status: (raw.status ?? "").trim() || null,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
dbWrites.push({ kind: "calendar_event", detail: `created ${created.id}` });
|
||||||
|
return JSON.stringify({ ok: true, id: created.id });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (raw.action === "create_message") {
|
||||||
|
const contactName = (raw.contact ?? "").trim();
|
||||||
|
const text = (raw.text ?? "").trim();
|
||||||
|
if (!contactName) throw new Error("contact is required");
|
||||||
|
if (!text) throw new Error("text is required");
|
||||||
|
|
||||||
|
const contact = await prisma.contact.findFirst({
|
||||||
|
where: { teamId: input.teamId, name: contactName },
|
||||||
|
select: { id: true },
|
||||||
|
});
|
||||||
|
if (!contact) throw new Error("contact not found");
|
||||||
|
|
||||||
|
const occurredAt = raw.at ? new Date(raw.at) : new Date();
|
||||||
|
if (Number.isNaN(occurredAt.getTime())) throw new Error("at is invalid");
|
||||||
|
|
||||||
|
const created = await prisma.contactMessage.create({
|
||||||
|
data: {
|
||||||
|
contactId: contact.id,
|
||||||
|
kind: raw.kind === "call" ? "CALL" : "MESSAGE",
|
||||||
|
direction: raw.direction === "in" ? "IN" : "OUT",
|
||||||
|
channel:
|
||||||
|
raw.channel === "Telegram"
|
||||||
|
? "TELEGRAM"
|
||||||
|
: raw.channel === "WhatsApp"
|
||||||
|
? "WHATSAPP"
|
||||||
|
: raw.channel === "Instagram"
|
||||||
|
? "INSTAGRAM"
|
||||||
|
: raw.channel === "Email"
|
||||||
|
? "EMAIL"
|
||||||
|
: "PHONE",
|
||||||
|
content: text,
|
||||||
|
durationSec: typeof raw.durationSec === "number" ? raw.durationSec : null,
|
||||||
|
transcriptJson: Array.isArray(raw.transcript) ? raw.transcript : null,
|
||||||
|
occurredAt,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
dbWrites.push({ kind: "contact_message", detail: `created ${created.id}` });
|
||||||
|
return JSON.stringify({ ok: true, id: created.id });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (raw.action === "update_deal_stage") {
|
||||||
|
const dealId = (raw.dealId ?? "").trim();
|
||||||
|
const stage = (raw.stage ?? "").trim();
|
||||||
|
if (!dealId) throw new Error("dealId is required");
|
||||||
|
if (!stage) throw new Error("stage is required");
|
||||||
|
|
||||||
|
const updated = await prisma.deal.updateMany({
|
||||||
|
where: { id: dealId, teamId: input.teamId },
|
||||||
|
data: { stage },
|
||||||
|
});
|
||||||
|
if (updated.count === 0) throw new Error("deal not found");
|
||||||
|
dbWrites.push({ kind: "deal", detail: `updated stage for ${dealId}` });
|
||||||
|
return JSON.stringify({ ok: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
return JSON.stringify({ ok: false, error: "unknown action" });
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "crm",
|
||||||
|
description:
|
||||||
|
"Query and update CRM data (contacts, deals, events, communications). Use this tool for any data you need beyond the snapshot.",
|
||||||
|
schema: CrmToolSchema,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
const snapshot = await buildCrmSnapshot({ teamId: input.teamId });
|
||||||
|
|
||||||
|
const model = new ChatOpenAI({
|
||||||
|
apiKey: process.env.OPENAI_API_KEY,
|
||||||
|
model: process.env.OPENAI_MODEL || "gpt-4o-mini",
|
||||||
|
temperature: 0.2,
|
||||||
|
});
|
||||||
|
|
||||||
|
const agent = createReactAgent({
|
||||||
|
llm: model,
|
||||||
|
tools: [crmTool],
|
||||||
|
responseFormat: z.object({
|
||||||
|
answer: z.string().describe("Final assistant answer for the user."),
|
||||||
|
plan: z.array(z.string()).min(1).max(10).describe("Short plan (3-8 steps)."),
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
const system = [
|
||||||
|
"You are Pilot, a CRM assistant.",
|
||||||
|
"Rules:",
|
||||||
|
"- Be concrete and concise.",
|
||||||
|
"- If you need data beyond the snapshot, call the crm tool.",
|
||||||
|
"- If user asks to change CRM, you may do it via the crm tool and then report what changed.",
|
||||||
|
"- Do not claim you sent an external message; you can only create draft messages/events/notes in CRM.",
|
||||||
|
"",
|
||||||
|
"CRM Snapshot:",
|
||||||
|
snapshot,
|
||||||
|
].join("\n");
|
||||||
|
|
||||||
|
const res: any = await agent.invoke(
|
||||||
|
{
|
||||||
|
messages: [
|
||||||
|
{ role: "system", content: system },
|
||||||
|
{ role: "user", content: input.userText },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{ recursionLimit: 30 },
|
||||||
|
);
|
||||||
|
|
||||||
|
const structured = res?.structuredResponse as { answer?: string; plan?: string[] } | undefined;
|
||||||
|
const text = structured?.answer?.trim() || "Готово.";
|
||||||
|
const plan = Array.isArray(structured?.plan) ? structured!.plan : ["Собрать данные", "Сформировать ответ"];
|
||||||
|
|
||||||
|
return { text, plan, tools: toolsUsed, dbWrites: dbWrites.length ? dbWrites : undefined };
|
||||||
|
}
|
||||||
8
Frontend/server/api/auth/demo.post.ts
Normal file
8
Frontend/server/api/auth/demo.post.ts
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
import { ensureDemoAuth, setSession } from "../../utils/auth";
|
||||||
|
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
const demo = await ensureDemoAuth();
|
||||||
|
setSession(event, demo);
|
||||||
|
return { ok: true };
|
||||||
|
});
|
||||||
|
|
||||||
35
Frontend/server/api/auth/login.post.ts
Normal file
35
Frontend/server/api/auth/login.post.ts
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
import { readBody } from "h3";
|
||||||
|
import { prisma } from "../../utils/prisma";
|
||||||
|
import { setSession } from "../../utils/auth";
|
||||||
|
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
const body = await readBody<{ email?: string; name?: string; teamName?: string }>(event);
|
||||||
|
const email = (body?.email ?? "").trim().toLowerCase();
|
||||||
|
const name = (body?.name ?? "").trim();
|
||||||
|
const teamName = (body?.teamName ?? "").trim() || "My Team";
|
||||||
|
|
||||||
|
if (!email || !email.includes("@")) {
|
||||||
|
throw createError({ statusCode: 400, statusMessage: "valid email is required" });
|
||||||
|
}
|
||||||
|
if (!name) {
|
||||||
|
throw createError({ statusCode: 400, statusMessage: "name is required" });
|
||||||
|
}
|
||||||
|
|
||||||
|
const user = await prisma.user.upsert({
|
||||||
|
where: { email },
|
||||||
|
update: { name },
|
||||||
|
create: { email, name },
|
||||||
|
});
|
||||||
|
|
||||||
|
// For MVP: 1 user -> 1 team (created if missing)
|
||||||
|
const team = await prisma.team.create({ data: { name: teamName } });
|
||||||
|
await prisma.teamMember.create({ data: { teamId: team.id, userId: user.id, role: "OWNER" } });
|
||||||
|
|
||||||
|
const conversation = await prisma.chatConversation.create({
|
||||||
|
data: { teamId: team.id, createdByUserId: user.id, title: "Pilot" },
|
||||||
|
});
|
||||||
|
|
||||||
|
setSession(event, { teamId: team.id, userId: user.id, conversationId: conversation.id });
|
||||||
|
return { ok: true };
|
||||||
|
});
|
||||||
|
|
||||||
6
Frontend/server/api/auth/logout.post.ts
Normal file
6
Frontend/server/api/auth/logout.post.ts
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
import { clearAuthSession } from "../../utils/auth";
|
||||||
|
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
clearAuthSession(event);
|
||||||
|
return { ok: true };
|
||||||
|
});
|
||||||
17
Frontend/server/api/auth/me.get.ts
Normal file
17
Frontend/server/api/auth/me.get.ts
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
import { getAuthContext } from "../../utils/auth";
|
||||||
|
import { prisma } from "../../utils/prisma";
|
||||||
|
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
try {
|
||||||
|
const auth = await getAuthContext(event);
|
||||||
|
const [user, team, conv] = await Promise.all([
|
||||||
|
prisma.user.findUnique({ where: { id: auth.userId } }),
|
||||||
|
prisma.team.findUnique({ where: { id: auth.teamId } }),
|
||||||
|
prisma.chatConversation.findUnique({ where: { id: auth.conversationId } }),
|
||||||
|
]);
|
||||||
|
if (!user || !team || !conv) throw new Error("unauth");
|
||||||
|
return { user: { id: user.id, email: user.email, name: user.name }, team: { id: team.id, name: team.name }, conversation: { id: conv.id, title: conv.title } };
|
||||||
|
} catch {
|
||||||
|
throw createError({ statusCode: 401, statusMessage: "Unauthorized" });
|
||||||
|
}
|
||||||
|
});
|
||||||
29
Frontend/server/api/calendar.get.ts
Normal file
29
Frontend/server/api/calendar.get.ts
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import { prisma } from "../utils/prisma";
|
||||||
|
import { getAuthContext } from "../utils/auth";
|
||||||
|
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
const auth = await getAuthContext(event);
|
||||||
|
const query = getQuery(event) as any;
|
||||||
|
|
||||||
|
const from = query.from ? new Date(String(query.from)) : new Date(Date.now() - 1000 * 60 * 60 * 24 * 30);
|
||||||
|
const to = query.to ? new Date(String(query.to)) : new Date(Date.now() + 1000 * 60 * 60 * 24 * 60);
|
||||||
|
|
||||||
|
const items = await prisma.calendarEvent.findMany({
|
||||||
|
where: { teamId: auth.teamId, startsAt: { gte: from, lte: to } },
|
||||||
|
include: { contact: { select: { name: true } } },
|
||||||
|
orderBy: { startsAt: "asc" },
|
||||||
|
take: 500,
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
items: items.map((e) => ({
|
||||||
|
id: e.id,
|
||||||
|
title: e.title,
|
||||||
|
start: e.startsAt.toISOString(),
|
||||||
|
end: (e.endsAt ?? e.startsAt).toISOString(),
|
||||||
|
contact: e.contact?.name ?? "",
|
||||||
|
note: e.note ?? "",
|
||||||
|
})),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
51
Frontend/server/api/calendar.post.ts
Normal file
51
Frontend/server/api/calendar.post.ts
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
import { readBody } from "h3";
|
||||||
|
import { prisma } from "../utils/prisma";
|
||||||
|
import { getAuthContext } from "../utils/auth";
|
||||||
|
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
const auth = await getAuthContext(event);
|
||||||
|
const body = await readBody<{
|
||||||
|
title?: string;
|
||||||
|
start?: string;
|
||||||
|
end?: string;
|
||||||
|
contact?: string;
|
||||||
|
note?: string;
|
||||||
|
status?: string;
|
||||||
|
}>(event);
|
||||||
|
|
||||||
|
const title = (body?.title ?? "").trim();
|
||||||
|
const start = body?.start ? new Date(body.start) : null;
|
||||||
|
const end = body?.end ? new Date(body.end) : null;
|
||||||
|
if (!title) throw createError({ statusCode: 400, statusMessage: "title is required" });
|
||||||
|
if (!start || Number.isNaN(start.getTime())) throw createError({ statusCode: 400, statusMessage: "start is invalid" });
|
||||||
|
|
||||||
|
const contactName = (body?.contact ?? "").trim();
|
||||||
|
const contact = contactName
|
||||||
|
? await prisma.contact.findFirst({ where: { teamId: auth.teamId, name: contactName }, select: { id: true, name: true } })
|
||||||
|
: null;
|
||||||
|
|
||||||
|
const created = await prisma.calendarEvent.create({
|
||||||
|
data: {
|
||||||
|
teamId: auth.teamId,
|
||||||
|
contactId: contact?.id ?? null,
|
||||||
|
title,
|
||||||
|
startsAt: start,
|
||||||
|
endsAt: end && !Number.isNaN(end.getTime()) ? end : null,
|
||||||
|
note: (body?.note ?? "").trim() || null,
|
||||||
|
status: (body?.status ?? "").trim() || null,
|
||||||
|
},
|
||||||
|
include: { contact: { select: { name: true } } },
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
item: {
|
||||||
|
id: created.id,
|
||||||
|
title: created.title,
|
||||||
|
start: created.startsAt.toISOString(),
|
||||||
|
end: (created.endsAt ?? created.startsAt).toISOString(),
|
||||||
|
contact: created.contact?.name ?? "",
|
||||||
|
note: created.note ?? "",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
22
Frontend/server/api/chat.get.ts
Normal file
22
Frontend/server/api/chat.get.ts
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
import { prisma } from "../utils/prisma";
|
||||||
|
import { getAuthContext } from "../utils/auth";
|
||||||
|
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
const auth = await getAuthContext(event);
|
||||||
|
const items = await prisma.chatMessage.findMany({
|
||||||
|
where: { teamId: auth.teamId, conversationId: auth.conversationId },
|
||||||
|
orderBy: { createdAt: "asc" },
|
||||||
|
take: 200,
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
items: items.map((m) => ({
|
||||||
|
id: m.id,
|
||||||
|
role: m.role === "USER" ? "user" : m.role === "ASSISTANT" ? "assistant" : "system",
|
||||||
|
text: m.text,
|
||||||
|
plan: (m.planJson as any)?.steps ?? null,
|
||||||
|
tools: (m.planJson as any)?.tools ?? null,
|
||||||
|
createdAt: m.createdAt,
|
||||||
|
})),
|
||||||
|
};
|
||||||
|
});
|
||||||
33
Frontend/server/api/chat.post.ts
Normal file
33
Frontend/server/api/chat.post.ts
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import { readBody } from "h3";
|
||||||
|
import { persistChatMessage, runCrmAgentFor } from "../agent/crmAgent";
|
||||||
|
import { getAuthContext } from "../utils/auth";
|
||||||
|
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
const body = await readBody<{ text?: string }>(event);
|
||||||
|
const text = (body?.text ?? "").trim();
|
||||||
|
if (!text) {
|
||||||
|
throw createError({ statusCode: 400, statusMessage: "text is required" });
|
||||||
|
}
|
||||||
|
|
||||||
|
const auth = await getAuthContext(event);
|
||||||
|
await persistChatMessage({
|
||||||
|
teamId: auth.teamId,
|
||||||
|
conversationId: auth.conversationId,
|
||||||
|
authorUserId: auth.userId,
|
||||||
|
role: "USER",
|
||||||
|
text,
|
||||||
|
});
|
||||||
|
|
||||||
|
const reply = await runCrmAgentFor({ teamId: auth.teamId, userId: auth.userId, userText: text });
|
||||||
|
await persistChatMessage({
|
||||||
|
teamId: auth.teamId,
|
||||||
|
conversationId: auth.conversationId,
|
||||||
|
authorUserId: null,
|
||||||
|
role: "ASSISTANT",
|
||||||
|
text: reply.text,
|
||||||
|
plan: reply.plan,
|
||||||
|
tools: reply.tools,
|
||||||
|
});
|
||||||
|
|
||||||
|
return { ok: true };
|
||||||
|
});
|
||||||
25
Frontend/server/api/chat/log.post.ts
Normal file
25
Frontend/server/api/chat/log.post.ts
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
import { readBody } from "h3";
|
||||||
|
import { persistChatMessage } from "../../agent/crmAgent";
|
||||||
|
import { getAuthContext } from "../../utils/auth";
|
||||||
|
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
const body = await readBody<{ text?: string }>(event);
|
||||||
|
const text = (body?.text ?? "").trim();
|
||||||
|
if (!text) {
|
||||||
|
throw createError({ statusCode: 400, statusMessage: "text is required" });
|
||||||
|
}
|
||||||
|
|
||||||
|
const auth = await getAuthContext(event);
|
||||||
|
await persistChatMessage({
|
||||||
|
teamId: auth.teamId,
|
||||||
|
conversationId: auth.conversationId,
|
||||||
|
authorUserId: null,
|
||||||
|
role: "ASSISTANT",
|
||||||
|
text,
|
||||||
|
plan: [],
|
||||||
|
tools: [],
|
||||||
|
});
|
||||||
|
|
||||||
|
return { ok: true };
|
||||||
|
});
|
||||||
|
|
||||||
40
Frontend/server/api/communications.get.ts
Normal file
40
Frontend/server/api/communications.get.ts
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
import { prisma } from "../utils/prisma";
|
||||||
|
import { getAuthContext } from "../utils/auth";
|
||||||
|
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
const auth = await getAuthContext(event);
|
||||||
|
|
||||||
|
const items = await prisma.contactMessage.findMany({
|
||||||
|
where: { contact: { teamId: auth.teamId } },
|
||||||
|
orderBy: { occurredAt: "asc" },
|
||||||
|
take: 2000,
|
||||||
|
include: {
|
||||||
|
contact: { select: { id: true, name: true } },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
items: items.map((m) => ({
|
||||||
|
id: m.id,
|
||||||
|
at: m.occurredAt.toISOString(),
|
||||||
|
contactId: m.contactId,
|
||||||
|
contact: m.contact.name,
|
||||||
|
channel:
|
||||||
|
m.channel === "TELEGRAM"
|
||||||
|
? "Telegram"
|
||||||
|
: m.channel === "WHATSAPP"
|
||||||
|
? "WhatsApp"
|
||||||
|
: m.channel === "INSTAGRAM"
|
||||||
|
? "Instagram"
|
||||||
|
: m.channel === "EMAIL"
|
||||||
|
? "Email"
|
||||||
|
: "Phone",
|
||||||
|
kind: m.kind === "CALL" ? "call" : "message",
|
||||||
|
direction: m.direction === "IN" ? "in" : "out",
|
||||||
|
text: m.content,
|
||||||
|
duration: m.durationSec ? new Date(m.durationSec * 1000).toISOString().slice(14, 19) : undefined,
|
||||||
|
transcript: Array.isArray(m.transcriptJson) ? (m.transcriptJson as any) : undefined,
|
||||||
|
})),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
53
Frontend/server/api/communications.post.ts
Normal file
53
Frontend/server/api/communications.post.ts
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
import { readBody } from "h3";
|
||||||
|
import { prisma } from "../utils/prisma";
|
||||||
|
import { getAuthContext } from "../utils/auth";
|
||||||
|
|
||||||
|
function toDbChannel(channel: string) {
|
||||||
|
const c = channel.toLowerCase();
|
||||||
|
if (c === "telegram") return "TELEGRAM";
|
||||||
|
if (c === "whatsapp") return "WHATSAPP";
|
||||||
|
if (c === "instagram") return "INSTAGRAM";
|
||||||
|
if (c === "email") return "EMAIL";
|
||||||
|
return "PHONE";
|
||||||
|
}
|
||||||
|
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
const auth = await getAuthContext(event);
|
||||||
|
const body = await readBody<{
|
||||||
|
contact?: string;
|
||||||
|
channel?: string;
|
||||||
|
kind?: "message" | "call";
|
||||||
|
direction?: "in" | "out";
|
||||||
|
text?: string;
|
||||||
|
at?: string;
|
||||||
|
durationSec?: number;
|
||||||
|
transcript?: string[];
|
||||||
|
}>(event);
|
||||||
|
|
||||||
|
const contactName = (body?.contact ?? "").trim();
|
||||||
|
if (!contactName) throw createError({ statusCode: 400, statusMessage: "contact is required" });
|
||||||
|
|
||||||
|
const contact = await prisma.contact.findFirst({
|
||||||
|
where: { teamId: auth.teamId, name: contactName },
|
||||||
|
select: { id: true, name: true },
|
||||||
|
});
|
||||||
|
if (!contact) throw createError({ statusCode: 404, statusMessage: "contact not found" });
|
||||||
|
|
||||||
|
const occurredAt = body?.at ? new Date(body.at) : new Date();
|
||||||
|
if (Number.isNaN(occurredAt.getTime())) throw createError({ statusCode: 400, statusMessage: "at is invalid" });
|
||||||
|
|
||||||
|
const created = await prisma.contactMessage.create({
|
||||||
|
data: {
|
||||||
|
contactId: contact.id,
|
||||||
|
kind: body?.kind === "call" ? "CALL" : "MESSAGE",
|
||||||
|
direction: body?.direction === "in" ? "IN" : "OUT",
|
||||||
|
channel: toDbChannel(body?.channel ?? "Phone") as any,
|
||||||
|
content: (body?.text ?? "").trim(),
|
||||||
|
durationSec: typeof body?.durationSec === "number" ? body.durationSec : null,
|
||||||
|
transcriptJson: Array.isArray(body?.transcript) ? body.transcript : undefined,
|
||||||
|
occurredAt,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return { ok: true, id: created.id };
|
||||||
|
});
|
||||||
30
Frontend/server/api/contacts.get.ts
Normal file
30
Frontend/server/api/contacts.get.ts
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
import { prisma } from "../utils/prisma";
|
||||||
|
import { getAuthContext } from "../utils/auth";
|
||||||
|
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
const auth = await getAuthContext(event);
|
||||||
|
const items = await prisma.contact.findMany({
|
||||||
|
where: { teamId: auth.teamId },
|
||||||
|
include: {
|
||||||
|
note: { select: { content: true, updatedAt: true } },
|
||||||
|
messages: { select: { occurredAt: true }, orderBy: { occurredAt: "desc" }, take: 1 },
|
||||||
|
},
|
||||||
|
orderBy: { updatedAt: "desc" },
|
||||||
|
take: 500,
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
items: items.map((c) => ({
|
||||||
|
id: c.id,
|
||||||
|
name: c.name,
|
||||||
|
avatar: c.avatarUrl ?? "",
|
||||||
|
company: c.company ?? "",
|
||||||
|
country: c.country ?? "",
|
||||||
|
location: c.location ?? "",
|
||||||
|
channels: [], // derived client-side from comm list for now
|
||||||
|
lastContactAt: c.messages[0]?.occurredAt?.toISOString?.() ?? c.updatedAt.toISOString(),
|
||||||
|
description: c.note?.content ?? "",
|
||||||
|
})),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
27
Frontend/server/api/contacts/[id].get.ts
Normal file
27
Frontend/server/api/contacts/[id].get.ts
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
import { prisma } from "../../utils/prisma";
|
||||||
|
import { getAuthContext } from "../../utils/auth";
|
||||||
|
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
const auth = await getAuthContext(event);
|
||||||
|
const id = getRouterParam(event, "id");
|
||||||
|
if (!id) throw createError({ statusCode: 400, statusMessage: "id is required" });
|
||||||
|
|
||||||
|
const contact = await prisma.contact.findFirst({
|
||||||
|
where: { id, teamId: auth.teamId },
|
||||||
|
include: { note: { select: { content: true } } },
|
||||||
|
});
|
||||||
|
if (!contact) throw createError({ statusCode: 404, statusMessage: "not found" });
|
||||||
|
|
||||||
|
return {
|
||||||
|
id: contact.id,
|
||||||
|
name: contact.name,
|
||||||
|
avatar: contact.avatarUrl ?? "",
|
||||||
|
company: contact.company ?? "",
|
||||||
|
country: contact.country ?? "",
|
||||||
|
location: contact.location ?? "",
|
||||||
|
email: contact.email ?? "",
|
||||||
|
phone: contact.phone ?? "",
|
||||||
|
description: contact.note?.content ?? "",
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
23
Frontend/server/api/contacts/[id]/note.put.ts
Normal file
23
Frontend/server/api/contacts/[id]/note.put.ts
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
import { readBody } from "h3";
|
||||||
|
import { prisma } from "../../../utils/prisma";
|
||||||
|
import { getAuthContext } from "../../../utils/auth";
|
||||||
|
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
const auth = await getAuthContext(event);
|
||||||
|
const id = getRouterParam(event, "id");
|
||||||
|
if (!id) throw createError({ statusCode: 400, statusMessage: "id is required" });
|
||||||
|
const body = await readBody<{ content?: string }>(event);
|
||||||
|
const content = (body?.content ?? "").toString();
|
||||||
|
|
||||||
|
const contact = await prisma.contact.findFirst({ where: { id, teamId: auth.teamId } });
|
||||||
|
if (!contact) throw createError({ statusCode: 404, statusMessage: "not found" });
|
||||||
|
|
||||||
|
await prisma.contactNote.upsert({
|
||||||
|
where: { contactId: id },
|
||||||
|
update: { content },
|
||||||
|
create: { contactId: id, content },
|
||||||
|
});
|
||||||
|
|
||||||
|
return { ok: true };
|
||||||
|
});
|
||||||
|
|
||||||
8
Frontend/server/api/dataset/export.post.ts
Normal file
8
Frontend/server/api/dataset/export.post.ts
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
import { exportDatasetFromPrismaFor } from "../../dataset/exporter";
|
||||||
|
import { getAuthContext } from "../../utils/auth";
|
||||||
|
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
const auth = await getAuthContext(event);
|
||||||
|
await exportDatasetFromPrismaFor({ teamId: auth.teamId, userId: auth.userId });
|
||||||
|
return { ok: true };
|
||||||
|
});
|
||||||
27
Frontend/server/api/deals.get.ts
Normal file
27
Frontend/server/api/deals.get.ts
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
import { prisma } from "../utils/prisma";
|
||||||
|
import { getAuthContext } from "../utils/auth";
|
||||||
|
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
const auth = await getAuthContext(event);
|
||||||
|
|
||||||
|
const items = await prisma.deal.findMany({
|
||||||
|
where: { teamId: auth.teamId },
|
||||||
|
include: { contact: { select: { name: true, company: true } } },
|
||||||
|
orderBy: { updatedAt: "desc" },
|
||||||
|
take: 500,
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
items: items.map((d) => ({
|
||||||
|
id: d.id,
|
||||||
|
contact: d.contact.name,
|
||||||
|
title: d.title,
|
||||||
|
company: d.contact.company ?? "",
|
||||||
|
stage: d.stage,
|
||||||
|
amount: d.amount ? String(d.amount) : "",
|
||||||
|
nextStep: d.nextStep ?? "",
|
||||||
|
summary: d.summary ?? "",
|
||||||
|
})),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
26
Frontend/server/api/documents.get.ts
Normal file
26
Frontend/server/api/documents.get.ts
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
import { prisma } from "../utils/prisma";
|
||||||
|
import { getAuthContext } from "../utils/auth";
|
||||||
|
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
const auth = await getAuthContext(event);
|
||||||
|
|
||||||
|
const items = await prisma.workspaceDocument.findMany({
|
||||||
|
where: { teamId: auth.teamId },
|
||||||
|
orderBy: { updatedAt: "desc" },
|
||||||
|
take: 200,
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
items: items.map((d) => ({
|
||||||
|
id: d.id,
|
||||||
|
title: d.title,
|
||||||
|
type: d.type,
|
||||||
|
owner: d.owner,
|
||||||
|
scope: d.scope,
|
||||||
|
updatedAt: d.updatedAt.toISOString(),
|
||||||
|
summary: d.summary,
|
||||||
|
body: d.body,
|
||||||
|
})),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
27
Frontend/server/api/feed.get.ts
Normal file
27
Frontend/server/api/feed.get.ts
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
import { prisma } from "../utils/prisma";
|
||||||
|
import { getAuthContext } from "../utils/auth";
|
||||||
|
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
const auth = await getAuthContext(event);
|
||||||
|
|
||||||
|
const items = await prisma.feedCard.findMany({
|
||||||
|
where: { teamId: auth.teamId },
|
||||||
|
include: { contact: { select: { name: true } } },
|
||||||
|
orderBy: { happenedAt: "desc" },
|
||||||
|
take: 200,
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
items: items.map((c) => ({
|
||||||
|
id: c.id,
|
||||||
|
at: c.happenedAt.toISOString(),
|
||||||
|
contact: c.contact?.name ?? "",
|
||||||
|
text: c.text,
|
||||||
|
proposal: c.proposalJson as any,
|
||||||
|
decision:
|
||||||
|
c.decision === "ACCEPTED" ? "accepted" : c.decision === "REJECTED" ? "rejected" : ("pending" as const),
|
||||||
|
decisionNote: c.decisionNote ?? undefined,
|
||||||
|
})),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
23
Frontend/server/api/feed/[id].put.ts
Normal file
23
Frontend/server/api/feed/[id].put.ts
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
import { readBody } from "h3";
|
||||||
|
import { prisma } from "../../utils/prisma";
|
||||||
|
import { getAuthContext } from "../../utils/auth";
|
||||||
|
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
const auth = await getAuthContext(event);
|
||||||
|
const id = String(getRouterParam(event, "id") ?? "");
|
||||||
|
if (!id) throw createError({ statusCode: 400, statusMessage: "id is required" });
|
||||||
|
|
||||||
|
const body = await readBody<{ decision?: "accepted" | "rejected" | "pending"; decisionNote?: string }>(event);
|
||||||
|
const decision = body?.decision;
|
||||||
|
if (!decision) throw createError({ statusCode: 400, statusMessage: "decision is required" });
|
||||||
|
|
||||||
|
const nextDecision = decision === "accepted" ? "ACCEPTED" : decision === "rejected" ? "REJECTED" : "PENDING";
|
||||||
|
|
||||||
|
const res = await prisma.feedCard.updateMany({
|
||||||
|
where: { id, teamId: auth.teamId },
|
||||||
|
data: { decision: nextDecision, decisionNote: body?.decisionNote ?? null },
|
||||||
|
});
|
||||||
|
if (res.count === 0) throw createError({ statusCode: 404, statusMessage: "feed card not found" });
|
||||||
|
|
||||||
|
return { ok: true, id };
|
||||||
|
});
|
||||||
22
Frontend/server/api/pins.get.ts
Normal file
22
Frontend/server/api/pins.get.ts
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
import { prisma } from "../utils/prisma";
|
||||||
|
import { getAuthContext } from "../utils/auth";
|
||||||
|
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
const auth = await getAuthContext(event);
|
||||||
|
|
||||||
|
const items = await prisma.contactPin.findMany({
|
||||||
|
where: { teamId: auth.teamId },
|
||||||
|
include: { contact: { select: { name: true } } },
|
||||||
|
orderBy: { updatedAt: "desc" },
|
||||||
|
take: 500,
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
items: items.map((p) => ({
|
||||||
|
id: p.id,
|
||||||
|
contact: p.contact.name,
|
||||||
|
text: p.text,
|
||||||
|
})),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
41
Frontend/server/api/telegram/messages.get.ts
Normal file
41
Frontend/server/api/telegram/messages.get.ts
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
import { getQuery } from "h3";
|
||||||
|
import { prisma } from "../../utils/prisma";
|
||||||
|
import { getAuthContext } from "../../utils/auth";
|
||||||
|
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
const auth = await getAuthContext(event);
|
||||||
|
const q = getQuery(event);
|
||||||
|
const threadId = typeof q.threadId === "string" ? q.threadId : "";
|
||||||
|
if (!threadId) throw createError({ statusCode: 400, statusMessage: "threadId is required" });
|
||||||
|
|
||||||
|
const thread = await prisma.omniThread.findFirst({
|
||||||
|
where: { id: threadId, teamId: auth.teamId, channel: "TELEGRAM" },
|
||||||
|
});
|
||||||
|
if (!thread) throw createError({ statusCode: 404, statusMessage: "thread not found" });
|
||||||
|
|
||||||
|
const items = await prisma.omniMessage.findMany({
|
||||||
|
where: { teamId: auth.teamId, threadId: thread.id, channel: "TELEGRAM" },
|
||||||
|
orderBy: { occurredAt: "asc" },
|
||||||
|
take: 200,
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
thread: {
|
||||||
|
id: thread.id,
|
||||||
|
contactId: thread.contactId,
|
||||||
|
externalChatId: thread.externalChatId,
|
||||||
|
businessConnectionId: thread.businessConnectionId,
|
||||||
|
title: thread.title,
|
||||||
|
updatedAt: thread.updatedAt,
|
||||||
|
},
|
||||||
|
items: items.map((m) => ({
|
||||||
|
id: m.id,
|
||||||
|
direction: m.direction,
|
||||||
|
status: m.status,
|
||||||
|
text: m.text,
|
||||||
|
providerMessageId: m.providerMessageId,
|
||||||
|
occurredAt: m.occurredAt,
|
||||||
|
})),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
36
Frontend/server/api/telegram/send.post.ts
Normal file
36
Frontend/server/api/telegram/send.post.ts
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
import { readBody } from "h3";
|
||||||
|
import { prisma } from "../../utils/prisma";
|
||||||
|
import { getAuthContext } from "../../utils/auth";
|
||||||
|
import { enqueueTelegramSend } from "../../queues/telegramSend";
|
||||||
|
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
const auth = await getAuthContext(event);
|
||||||
|
const body = await readBody<{ threadId?: string; text?: string }>(event);
|
||||||
|
|
||||||
|
const threadId = (body?.threadId || "").trim();
|
||||||
|
const text = (body?.text || "").trim();
|
||||||
|
if (!threadId) throw createError({ statusCode: 400, statusMessage: "threadId is required" });
|
||||||
|
if (!text) throw createError({ statusCode: 400, statusMessage: "text is required" });
|
||||||
|
|
||||||
|
const thread = await prisma.omniThread.findFirst({
|
||||||
|
where: { id: threadId, teamId: auth.teamId, channel: "TELEGRAM" },
|
||||||
|
});
|
||||||
|
if (!thread) throw createError({ statusCode: 404, statusMessage: "thread not found" });
|
||||||
|
|
||||||
|
const msg = await prisma.omniMessage.create({
|
||||||
|
data: {
|
||||||
|
teamId: auth.teamId,
|
||||||
|
contactId: thread.contactId,
|
||||||
|
threadId: thread.id,
|
||||||
|
direction: "OUT",
|
||||||
|
channel: "TELEGRAM",
|
||||||
|
status: "PENDING",
|
||||||
|
text,
|
||||||
|
occurredAt: new Date(),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
await enqueueTelegramSend({ omniMessageId: msg.id });
|
||||||
|
return { ok: true, messageId: msg.id };
|
||||||
|
});
|
||||||
|
|
||||||
37
Frontend/server/api/telegram/threads.get.ts
Normal file
37
Frontend/server/api/telegram/threads.get.ts
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
import { prisma } from "../../utils/prisma";
|
||||||
|
import { getAuthContext } from "../../utils/auth";
|
||||||
|
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
const auth = await getAuthContext(event);
|
||||||
|
|
||||||
|
const threads = await prisma.omniThread.findMany({
|
||||||
|
where: { teamId: auth.teamId, channel: "TELEGRAM" },
|
||||||
|
orderBy: { updatedAt: "desc" },
|
||||||
|
take: 50,
|
||||||
|
include: {
|
||||||
|
contact: true,
|
||||||
|
messages: { orderBy: { occurredAt: "desc" }, take: 1 },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
items: threads.map((t) => ({
|
||||||
|
id: t.id,
|
||||||
|
contact: { id: t.contact.id, name: t.contact.name },
|
||||||
|
externalChatId: t.externalChatId,
|
||||||
|
businessConnectionId: t.businessConnectionId,
|
||||||
|
title: t.title,
|
||||||
|
updatedAt: t.updatedAt,
|
||||||
|
lastMessage: t.messages[0]
|
||||||
|
? {
|
||||||
|
id: t.messages[0].id,
|
||||||
|
direction: t.messages[0].direction,
|
||||||
|
status: t.messages[0].status,
|
||||||
|
text: t.messages[0].text,
|
||||||
|
occurredAt: t.messages[0].occurredAt,
|
||||||
|
}
|
||||||
|
: null,
|
||||||
|
})),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
163
Frontend/server/api/telegram/webhook.post.ts
Normal file
163
Frontend/server/api/telegram/webhook.post.ts
Normal file
@@ -0,0 +1,163 @@
|
|||||||
|
import { readBody, getQuery, getHeader } from "h3";
|
||||||
|
import { prisma } from "../../utils/prisma";
|
||||||
|
|
||||||
|
function teamIdFromWebhook(event: any) {
|
||||||
|
const q = getQuery(event);
|
||||||
|
const fromQuery = typeof q.teamId === "string" ? q.teamId : null;
|
||||||
|
return fromQuery || process.env.TELEGRAM_DEFAULT_TEAM_ID || "demo-team";
|
||||||
|
}
|
||||||
|
|
||||||
|
function assertSecret(event: any) {
|
||||||
|
const expected = process.env.TELEGRAM_WEBHOOK_SECRET;
|
||||||
|
if (!expected) return;
|
||||||
|
|
||||||
|
const got = getHeader(event, "x-telegram-bot-api-secret-token");
|
||||||
|
if (!got || got !== expected) {
|
||||||
|
throw createError({ statusCode: 401, statusMessage: "invalid telegram secret token" });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function displayNameFromTelegram(obj: any) {
|
||||||
|
const first = obj?.first_name || "";
|
||||||
|
const last = obj?.last_name || "";
|
||||||
|
const u = obj?.username ? `@${obj.username}` : "";
|
||||||
|
const full = `${first} ${last}`.trim();
|
||||||
|
return (full || u || "Telegram user").trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
async function upsertBusinessConnection(teamId: string, bc: any) {
|
||||||
|
if (!bc?.id) return;
|
||||||
|
const businessConnectionId = String(bc.id);
|
||||||
|
|
||||||
|
await prisma.telegramBusinessConnection.upsert({
|
||||||
|
where: { teamId_businessConnectionId: { teamId, businessConnectionId } },
|
||||||
|
update: {
|
||||||
|
isEnabled: typeof bc.is_enabled === "boolean" ? bc.is_enabled : undefined,
|
||||||
|
canReply: typeof bc.can_reply === "boolean" ? bc.can_reply : undefined,
|
||||||
|
rawJson: bc,
|
||||||
|
},
|
||||||
|
create: {
|
||||||
|
teamId,
|
||||||
|
businessConnectionId,
|
||||||
|
isEnabled: typeof bc.is_enabled === "boolean" ? bc.is_enabled : null,
|
||||||
|
canReply: typeof bc.can_reply === "boolean" ? bc.can_reply : null,
|
||||||
|
rawJson: bc,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function ensureContactForTelegramChat(teamId: string, externalChatId: string, tgUser: any) {
|
||||||
|
const existing = await prisma.omniContactIdentity.findUnique({
|
||||||
|
where: { teamId_channel_externalId: { teamId, channel: "TELEGRAM", externalId: externalChatId } },
|
||||||
|
include: { contact: true },
|
||||||
|
});
|
||||||
|
if (existing) return existing.contact;
|
||||||
|
|
||||||
|
const contact = await prisma.contact.create({
|
||||||
|
data: {
|
||||||
|
teamId,
|
||||||
|
name: displayNameFromTelegram(tgUser),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
await prisma.omniContactIdentity.create({
|
||||||
|
data: {
|
||||||
|
teamId,
|
||||||
|
contactId: contact.id,
|
||||||
|
channel: "TELEGRAM",
|
||||||
|
externalId: externalChatId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return contact;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function ensureThread(input: {
|
||||||
|
teamId: string;
|
||||||
|
contactId: string;
|
||||||
|
externalChatId: string;
|
||||||
|
businessConnectionId?: string | null;
|
||||||
|
title?: string | null;
|
||||||
|
}) {
|
||||||
|
return prisma.omniThread.upsert({
|
||||||
|
where: {
|
||||||
|
teamId_channel_externalChatId_businessConnectionId: {
|
||||||
|
teamId: input.teamId,
|
||||||
|
channel: "TELEGRAM",
|
||||||
|
externalChatId: input.externalChatId,
|
||||||
|
businessConnectionId: input.businessConnectionId ?? null,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
update: {
|
||||||
|
contactId: input.contactId,
|
||||||
|
title: input.title ?? undefined,
|
||||||
|
},
|
||||||
|
create: {
|
||||||
|
teamId: input.teamId,
|
||||||
|
contactId: input.contactId,
|
||||||
|
channel: "TELEGRAM",
|
||||||
|
externalChatId: input.externalChatId,
|
||||||
|
businessConnectionId: input.businessConnectionId ?? null,
|
||||||
|
title: input.title ?? null,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
assertSecret(event);
|
||||||
|
const teamId = teamIdFromWebhook(event);
|
||||||
|
|
||||||
|
const update = (await readBody<any>(event)) || {};
|
||||||
|
|
||||||
|
// business_connection updates (user connected/disconnected bot)
|
||||||
|
if (update.business_connection) {
|
||||||
|
await upsertBusinessConnection(teamId, update.business_connection);
|
||||||
|
return { ok: true };
|
||||||
|
}
|
||||||
|
|
||||||
|
const msg = update.business_message || update.edited_business_message;
|
||||||
|
if (!msg) return { ok: true };
|
||||||
|
|
||||||
|
const businessConnectionId = msg.business_connection_id ? String(msg.business_connection_id) : null;
|
||||||
|
const chatId = msg.chat?.id != null ? String(msg.chat.id) : null;
|
||||||
|
const providerMessageId = msg.message_id != null ? String(msg.message_id) : null;
|
||||||
|
|
||||||
|
if (!chatId || !providerMessageId) return { ok: true };
|
||||||
|
|
||||||
|
const text = typeof msg.text === "string" ? msg.text : typeof msg.caption === "string" ? msg.caption : "";
|
||||||
|
const occurredAt = msg.date ? new Date(Number(msg.date) * 1000) : new Date();
|
||||||
|
|
||||||
|
const contact = await ensureContactForTelegramChat(teamId, chatId, msg.from || msg.chat);
|
||||||
|
const thread = await ensureThread({
|
||||||
|
teamId,
|
||||||
|
contactId: contact.id,
|
||||||
|
externalChatId: chatId,
|
||||||
|
businessConnectionId,
|
||||||
|
title: msg.chat?.title ? String(msg.chat.title) : null,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Dedupe on (threadId, providerMessageId). If duplicate, ignore.
|
||||||
|
try {
|
||||||
|
await prisma.omniMessage.create({
|
||||||
|
data: {
|
||||||
|
teamId,
|
||||||
|
contactId: contact.id,
|
||||||
|
threadId: thread.id,
|
||||||
|
direction: "IN",
|
||||||
|
channel: "TELEGRAM",
|
||||||
|
status: "DELIVERED",
|
||||||
|
text: text || "",
|
||||||
|
providerMessageId,
|
||||||
|
providerUpdateId: update.update_id != null ? String(update.update_id) : null,
|
||||||
|
rawJson: update,
|
||||||
|
occurredAt,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} catch (e: any) {
|
||||||
|
// Prisma unique constraint violation => duplicate delivery
|
||||||
|
if (e?.code !== "P2002") throw e;
|
||||||
|
}
|
||||||
|
|
||||||
|
return { ok: true };
|
||||||
|
});
|
||||||
|
|
||||||
156
Frontend/server/dataset/exporter.ts
Normal file
156
Frontend/server/dataset/exporter.ts
Normal file
@@ -0,0 +1,156 @@
|
|||||||
|
import fs from "node:fs/promises";
|
||||||
|
import path from "node:path";
|
||||||
|
import { prisma } from "../utils/prisma";
|
||||||
|
import { datasetRoot } from "./paths";
|
||||||
|
|
||||||
|
type ExportMeta = {
|
||||||
|
exportedAt: string;
|
||||||
|
version: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
async function ensureDir(p: string) {
|
||||||
|
await fs.mkdir(p, { recursive: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
async function writeJson(p: string, value: unknown) {
|
||||||
|
await fs.writeFile(p, JSON.stringify(value, null, 2) + "\n", "utf8");
|
||||||
|
}
|
||||||
|
|
||||||
|
function jsonlLine(value: unknown) {
|
||||||
|
return JSON.stringify(value) + "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function exportDatasetFromPrisma() {
|
||||||
|
throw new Error("exportDatasetFromPrisma now requires { teamId, userId }");
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function exportDatasetFromPrismaFor(input: { teamId: string; userId: string }) {
|
||||||
|
const root = datasetRoot(input);
|
||||||
|
const tmp = root + ".tmp";
|
||||||
|
|
||||||
|
await fs.rm(tmp, { recursive: true, force: true });
|
||||||
|
await ensureDir(tmp);
|
||||||
|
|
||||||
|
const contactsDir = path.join(tmp, "contacts");
|
||||||
|
const notesDir = path.join(tmp, "notes");
|
||||||
|
const messagesDir = path.join(tmp, "messages");
|
||||||
|
const eventsDir = path.join(tmp, "events");
|
||||||
|
const indexDir = path.join(tmp, "index");
|
||||||
|
await Promise.all([
|
||||||
|
ensureDir(contactsDir),
|
||||||
|
ensureDir(notesDir),
|
||||||
|
ensureDir(messagesDir),
|
||||||
|
ensureDir(eventsDir),
|
||||||
|
ensureDir(indexDir),
|
||||||
|
]);
|
||||||
|
|
||||||
|
const contacts = await prisma.contact.findMany({
|
||||||
|
where: { teamId: input.teamId },
|
||||||
|
orderBy: { updatedAt: "desc" },
|
||||||
|
include: {
|
||||||
|
note: { select: { content: true, updatedAt: true } },
|
||||||
|
messages: {
|
||||||
|
select: {
|
||||||
|
kind: true,
|
||||||
|
direction: true,
|
||||||
|
channel: true,
|
||||||
|
content: true,
|
||||||
|
durationSec: true,
|
||||||
|
transcriptJson: true,
|
||||||
|
occurredAt: true,
|
||||||
|
},
|
||||||
|
orderBy: { occurredAt: "asc" },
|
||||||
|
},
|
||||||
|
events: {
|
||||||
|
select: { title: true, startsAt: true, endsAt: true, status: true, note: true },
|
||||||
|
orderBy: { startsAt: "asc" },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
take: 5000,
|
||||||
|
});
|
||||||
|
|
||||||
|
const contactIndex = [];
|
||||||
|
|
||||||
|
for (const c of contacts) {
|
||||||
|
const contactFile = path.join(contactsDir, `${c.id}.json`);
|
||||||
|
await writeJson(contactFile, {
|
||||||
|
id: c.id,
|
||||||
|
teamId: c.teamId,
|
||||||
|
name: c.name,
|
||||||
|
company: c.company ?? null,
|
||||||
|
country: c.country ?? null,
|
||||||
|
location: c.location ?? null,
|
||||||
|
avatarUrl: c.avatarUrl ?? null,
|
||||||
|
email: c.email ?? null,
|
||||||
|
phone: c.phone ?? null,
|
||||||
|
createdAt: c.createdAt,
|
||||||
|
updatedAt: c.updatedAt,
|
||||||
|
});
|
||||||
|
|
||||||
|
const noteFile = path.join(notesDir, `${c.id}.md`);
|
||||||
|
await fs.writeFile(
|
||||||
|
noteFile,
|
||||||
|
(c.note?.content?.trim() ? c.note.content.trim() : "") + "\n",
|
||||||
|
"utf8",
|
||||||
|
);
|
||||||
|
|
||||||
|
const msgFile = path.join(messagesDir, `${c.id}.jsonl`);
|
||||||
|
const msgLines = c.messages.map((m) =>
|
||||||
|
jsonlLine({
|
||||||
|
kind: m.kind,
|
||||||
|
direction: m.direction,
|
||||||
|
channel: m.channel,
|
||||||
|
occurredAt: m.occurredAt,
|
||||||
|
content: m.content,
|
||||||
|
durationSec: m.durationSec ?? null,
|
||||||
|
transcript: m.transcriptJson ?? null,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
await fs.writeFile(msgFile, msgLines.join(""), "utf8");
|
||||||
|
|
||||||
|
const evFile = path.join(eventsDir, `${c.id}.jsonl`);
|
||||||
|
const evLines = c.events.map((e) =>
|
||||||
|
jsonlLine({
|
||||||
|
title: e.title,
|
||||||
|
startsAt: e.startsAt,
|
||||||
|
endsAt: e.endsAt,
|
||||||
|
status: e.status ?? null,
|
||||||
|
note: e.note ?? null,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
await fs.writeFile(evFile, evLines.join(""), "utf8");
|
||||||
|
|
||||||
|
const lastMessageAt = c.messages.length ? c.messages[c.messages.length - 1].occurredAt : null;
|
||||||
|
const nextEventAt = c.events.find((e) => new Date(e.startsAt).getTime() >= Date.now())?.startsAt ?? null;
|
||||||
|
|
||||||
|
contactIndex.push({
|
||||||
|
id: c.id,
|
||||||
|
name: c.name,
|
||||||
|
company: c.company ?? null,
|
||||||
|
lastMessageAt,
|
||||||
|
nextEventAt,
|
||||||
|
updatedAt: c.updatedAt,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
await writeJson(path.join(indexDir, "contacts.json"), contactIndex);
|
||||||
|
|
||||||
|
const meta: ExportMeta = { exportedAt: new Date().toISOString(), version: 1 };
|
||||||
|
await writeJson(path.join(tmp, "meta.json"), meta);
|
||||||
|
|
||||||
|
await ensureDir(path.dirname(root));
|
||||||
|
await fs.rm(root, { recursive: true, force: true });
|
||||||
|
await fs.rename(tmp, root);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function ensureDataset(input: { teamId: string; userId: string }) {
|
||||||
|
const root = datasetRoot(input);
|
||||||
|
try {
|
||||||
|
const metaPath = path.join(root, "meta.json");
|
||||||
|
await fs.access(metaPath);
|
||||||
|
return;
|
||||||
|
} catch {
|
||||||
|
// fallthrough
|
||||||
|
}
|
||||||
|
await exportDatasetFromPrismaFor(input);
|
||||||
|
}
|
||||||
6
Frontend/server/dataset/paths.ts
Normal file
6
Frontend/server/dataset/paths.ts
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
import path from "node:path";
|
||||||
|
|
||||||
|
export function datasetRoot(input: { teamId: string; userId: string }) {
|
||||||
|
// Keep it outside Frontend so it can be easily ignored and shared.
|
||||||
|
return path.resolve(process.cwd(), "..", ".data", "crmfs", "teams", input.teamId, "users", input.userId);
|
||||||
|
}
|
||||||
9
Frontend/server/plugins/queues.ts
Normal file
9
Frontend/server/plugins/queues.ts
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
import { startTelegramSendWorker } from "../queues/telegramSend";
|
||||||
|
|
||||||
|
export default defineNitroPlugin(() => {
|
||||||
|
// Keep API nodes and worker nodes separate: start only when explicitly enabled.
|
||||||
|
if (process.env.RUN_QUEUE_WORKER !== "1") return;
|
||||||
|
|
||||||
|
startTelegramSendWorker();
|
||||||
|
});
|
||||||
|
|
||||||
92
Frontend/server/queues/telegramSend.ts
Normal file
92
Frontend/server/queues/telegramSend.ts
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
import { Queue, Worker, JobsOptions } from "bullmq";
|
||||||
|
import { getRedis } from "../utils/redis";
|
||||||
|
import { prisma } from "../utils/prisma";
|
||||||
|
import { telegramBotApi } from "../utils/telegram";
|
||||||
|
|
||||||
|
export const TELEGRAM_SEND_QUEUE_NAME = "telegram:send";
|
||||||
|
|
||||||
|
type TelegramSendJob = {
|
||||||
|
omniMessageId: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export function telegramSendQueue() {
|
||||||
|
return new Queue<TelegramSendJob>(TELEGRAM_SEND_QUEUE_NAME, {
|
||||||
|
connection: getRedis(),
|
||||||
|
defaultJobOptions: {
|
||||||
|
removeOnComplete: { count: 1000 },
|
||||||
|
removeOnFail: { count: 5000 },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function enqueueTelegramSend(input: TelegramSendJob, opts?: JobsOptions) {
|
||||||
|
const q = telegramSendQueue();
|
||||||
|
return q.add("send", input, {
|
||||||
|
jobId: input.omniMessageId, // idempotency
|
||||||
|
attempts: 10,
|
||||||
|
backoff: { type: "exponential", delay: 1000 },
|
||||||
|
...opts,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function startTelegramSendWorker() {
|
||||||
|
return new Worker<TelegramSendJob>(
|
||||||
|
TELEGRAM_SEND_QUEUE_NAME,
|
||||||
|
async (job) => {
|
||||||
|
const msg = await prisma.omniMessage.findUnique({
|
||||||
|
where: { id: job.data.omniMessageId },
|
||||||
|
include: { thread: true },
|
||||||
|
});
|
||||||
|
if (!msg) return;
|
||||||
|
|
||||||
|
// Idempotency: if we already sent it, don't send twice.
|
||||||
|
if (msg.status === "SENT" && msg.providerMessageId) return;
|
||||||
|
|
||||||
|
if (msg.channel !== "TELEGRAM" || msg.direction !== "OUT") {
|
||||||
|
throw new Error(`Invalid omni message for telegram send: ${msg.id}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const thread = msg.thread;
|
||||||
|
const chatId = thread.externalChatId;
|
||||||
|
const businessConnectionId = thread.businessConnectionId || undefined;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const result = await telegramBotApi<any>("sendMessage", {
|
||||||
|
chat_id: chatId,
|
||||||
|
text: msg.text,
|
||||||
|
...(businessConnectionId ? { business_connection_id: businessConnectionId } : {}),
|
||||||
|
});
|
||||||
|
|
||||||
|
const providerMessageId = result?.message_id != null ? String(result.message_id) : null;
|
||||||
|
await prisma.omniMessage.update({
|
||||||
|
where: { id: msg.id },
|
||||||
|
data: {
|
||||||
|
status: "SENT",
|
||||||
|
providerMessageId: providerMessageId,
|
||||||
|
rawJson: result,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} catch (e: any) {
|
||||||
|
const isLastAttempt =
|
||||||
|
typeof job.opts.attempts === "number" && job.attemptsMade + 1 >= job.opts.attempts;
|
||||||
|
|
||||||
|
if (isLastAttempt) {
|
||||||
|
await prisma.omniMessage.update({
|
||||||
|
where: { id: msg.id },
|
||||||
|
data: {
|
||||||
|
status: "FAILED",
|
||||||
|
rawJson: {
|
||||||
|
error: String(e?.message || e),
|
||||||
|
attemptsMade: job.attemptsMade + 1,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{ connection: getRedis() },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
92
Frontend/server/utils/auth.ts
Normal file
92
Frontend/server/utils/auth.ts
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
import type { H3Event } from "h3";
|
||||||
|
import { getCookie, setCookie, deleteCookie, getHeader } from "h3";
|
||||||
|
import { prisma } from "./prisma";
|
||||||
|
|
||||||
|
export type AuthContext = {
|
||||||
|
teamId: string;
|
||||||
|
userId: string;
|
||||||
|
conversationId: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
const COOKIE_USER = "cf_user";
|
||||||
|
const COOKIE_TEAM = "cf_team";
|
||||||
|
const COOKIE_CONV = "cf_conv";
|
||||||
|
|
||||||
|
function cookieOpts() {
|
||||||
|
return {
|
||||||
|
httpOnly: true,
|
||||||
|
sameSite: "lax" as const,
|
||||||
|
path: "/",
|
||||||
|
secure: process.env.NODE_ENV === "production",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function clearAuthSession(event: H3Event) {
|
||||||
|
deleteCookie(event, COOKIE_USER, { path: "/" });
|
||||||
|
deleteCookie(event, COOKIE_TEAM, { path: "/" });
|
||||||
|
deleteCookie(event, COOKIE_CONV, { path: "/" });
|
||||||
|
}
|
||||||
|
|
||||||
|
export function setSession(event: H3Event, ctx: AuthContext) {
|
||||||
|
setCookie(event, COOKIE_USER, ctx.userId, cookieOpts());
|
||||||
|
setCookie(event, COOKIE_TEAM, ctx.teamId, cookieOpts());
|
||||||
|
setCookie(event, COOKIE_CONV, ctx.conversationId, cookieOpts());
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getAuthContext(event: H3Event): Promise<AuthContext> {
|
||||||
|
const cookieUser = getCookie(event, COOKIE_USER)?.trim();
|
||||||
|
const cookieTeam = getCookie(event, COOKIE_TEAM)?.trim();
|
||||||
|
const cookieConv = getCookie(event, COOKIE_CONV)?.trim();
|
||||||
|
|
||||||
|
// Temporary compatibility: allow passing via headers for debugging/dev tools.
|
||||||
|
const hdrTeam = getHeader(event, "x-team-id")?.trim();
|
||||||
|
const hdrUser = getHeader(event, "x-user-id")?.trim();
|
||||||
|
const hdrConv = getHeader(event, "x-conversation-id")?.trim();
|
||||||
|
|
||||||
|
const hasAnySession = Boolean(cookieUser || cookieTeam || cookieConv || hdrTeam || hdrUser || hdrConv);
|
||||||
|
if (!hasAnySession) {
|
||||||
|
throw createError({ statusCode: 401, statusMessage: "Unauthorized" });
|
||||||
|
}
|
||||||
|
|
||||||
|
const userId = cookieUser || hdrUser;
|
||||||
|
const teamId = cookieTeam || hdrTeam;
|
||||||
|
const conversationId = cookieConv || hdrConv;
|
||||||
|
|
||||||
|
if (!userId || !teamId || !conversationId) {
|
||||||
|
throw createError({ statusCode: 401, statusMessage: "Unauthorized" });
|
||||||
|
}
|
||||||
|
|
||||||
|
const user = await prisma.user.findUnique({ where: { id: userId } });
|
||||||
|
const team = await prisma.team.findUnique({ where: { id: teamId } });
|
||||||
|
const conv = await prisma.chatConversation.findUnique({ where: { id: conversationId } });
|
||||||
|
|
||||||
|
if (!user || !team || !conv) {
|
||||||
|
throw createError({ statusCode: 401, statusMessage: "Unauthorized" });
|
||||||
|
}
|
||||||
|
|
||||||
|
return { teamId: team.id, userId: user.id, conversationId: conv.id };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function ensureDemoAuth() {
|
||||||
|
const user = await prisma.user.upsert({
|
||||||
|
where: { id: "demo-user" },
|
||||||
|
update: { email: "demo@clientsflow.local", name: "Demo User" },
|
||||||
|
create: { id: "demo-user", email: "demo@clientsflow.local", name: "Demo User" },
|
||||||
|
});
|
||||||
|
const team = await prisma.team.upsert({
|
||||||
|
where: { id: "demo-team" },
|
||||||
|
update: { name: "Demo Team" },
|
||||||
|
create: { id: "demo-team", name: "Demo Team" },
|
||||||
|
});
|
||||||
|
await prisma.teamMember.upsert({
|
||||||
|
where: { teamId_userId: { teamId: team.id, userId: user.id } },
|
||||||
|
update: {},
|
||||||
|
create: { teamId: team.id, userId: user.id, role: "OWNER" },
|
||||||
|
});
|
||||||
|
const conv = await prisma.chatConversation.upsert({
|
||||||
|
where: { id: `pilot-${team.id}` },
|
||||||
|
update: {},
|
||||||
|
create: { id: `pilot-${team.id}`, teamId: team.id, createdByUserId: user.id, title: "Pilot" },
|
||||||
|
});
|
||||||
|
return { teamId: team.id, userId: user.id, conversationId: conv.id };
|
||||||
|
}
|
||||||
17
Frontend/server/utils/prisma.ts
Normal file
17
Frontend/server/utils/prisma.ts
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
import { PrismaClient } from "@prisma/client";
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
// eslint-disable-next-line no-var
|
||||||
|
var __prisma: PrismaClient | undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const prisma =
|
||||||
|
globalThis.__prisma ??
|
||||||
|
new PrismaClient({
|
||||||
|
log: ["error", "warn"],
|
||||||
|
});
|
||||||
|
|
||||||
|
if (process.env.NODE_ENV !== "production") {
|
||||||
|
globalThis.__prisma = prisma;
|
||||||
|
}
|
||||||
|
|
||||||
22
Frontend/server/utils/redis.ts
Normal file
22
Frontend/server/utils/redis.ts
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
import Redis from "ioredis";
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
// eslint-disable-next-line no-var
|
||||||
|
var __redis: Redis | undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getRedis() {
|
||||||
|
if (globalThis.__redis) return globalThis.__redis;
|
||||||
|
|
||||||
|
const url = process.env.REDIS_URL || "redis://localhost:6379";
|
||||||
|
const client = new Redis(url, {
|
||||||
|
maxRetriesPerRequest: null, // recommended for BullMQ
|
||||||
|
});
|
||||||
|
|
||||||
|
if (process.env.NODE_ENV !== "production") {
|
||||||
|
globalThis.__redis = client;
|
||||||
|
}
|
||||||
|
|
||||||
|
return client;
|
||||||
|
}
|
||||||
|
|
||||||
29
Frontend/server/utils/telegram.ts
Normal file
29
Frontend/server/utils/telegram.ts
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
export type TelegramUpdate = Record<string, any>;
|
||||||
|
|
||||||
|
export function telegramApiBase() {
|
||||||
|
return process.env.TELEGRAM_API_BASE || "https://api.telegram.org";
|
||||||
|
}
|
||||||
|
|
||||||
|
export function requireTelegramBotToken() {
|
||||||
|
const token = process.env.TELEGRAM_BOT_TOKEN;
|
||||||
|
if (!token) throw new Error("TELEGRAM_BOT_TOKEN is required");
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function telegramBotApi<T>(method: string, body: unknown): Promise<T> {
|
||||||
|
const token = requireTelegramBotToken();
|
||||||
|
const res = await fetch(`${telegramApiBase()}/bot${token}/${method}`, {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "content-type": "application/json" },
|
||||||
|
body: JSON.stringify(body),
|
||||||
|
});
|
||||||
|
|
||||||
|
const json = (await res.json().catch(() => null)) as any;
|
||||||
|
if (!res.ok || !json?.ok) {
|
||||||
|
const desc = json?.description || `HTTP ${res.status}`;
|
||||||
|
throw new Error(`Telegram API ${method} failed: ${desc}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return json.result as T;
|
||||||
|
}
|
||||||
|
|
||||||
4
Frontend/tsconfig.json
Normal file
4
Frontend/tsconfig.json
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"extends": "./.nuxt/tsconfig.json"
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user