DB-backed workspace + LangGraph agent

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

View File

@@ -4,6 +4,7 @@ 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;
@@ -74,6 +75,11 @@ export async function runCrmAgent(userText: string): Promise<AgentReply> {
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 });

View 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 };
}

View 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 };
});

View 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 };
});

View File

@@ -0,0 +1,6 @@
import { clearAuthSession } from "../../utils/auth";
export default defineEventHandler(async (event) => {
clearAuthSession(event);
return { ok: true };
});

View 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" });
}
});

View 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 ?? "",
})),
};
});

View 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 ?? "",
},
};
});

View 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,
})),
};
});

View 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 };
});

View 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 ?? "",
})),
};
});

View 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 ?? "",
};
});

View 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 };
});

View 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 ?? "",
})),
};
});

View 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,
})),
};
});

View 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,
})),
};
});

View 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 };
});

View 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,
})),
};
});

View 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,
})),
};
});

View 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 };
});

View 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,
})),
};
});

View 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 };
});

View File

@@ -50,7 +50,15 @@ export async function exportDatasetFromPrismaFor(input: { teamId: string; userId
include: {
note: { select: { content: true, updatedAt: true } },
messages: {
select: { direction: true, channel: true, content: true, occurredAt: true },
select: {
kind: true,
direction: true,
channel: true,
content: true,
durationSec: true,
transcriptJson: true,
occurredAt: true,
},
orderBy: { occurredAt: "asc" },
},
events: {
@@ -70,6 +78,9 @@ export async function exportDatasetFromPrismaFor(input: { teamId: string; userId
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,
@@ -86,10 +97,13 @@ export async function exportDatasetFromPrismaFor(input: { teamId: string; userId
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");

View 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();
});

View 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() },
);
}

View File

@@ -1,5 +1,6 @@
import { prisma } from "./prisma";
import type { H3Event } from "h3";
import { getCookie, setCookie, deleteCookie, getHeader } from "h3";
import { prisma } from "./prisma";
export type AuthContext = {
teamId: string;
@@ -7,51 +8,85 @@ export type AuthContext = {
conversationId: string;
};
// Minimal temporary auth: pick from headers or auto-provision a default team/user.
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();
// Ensure default team/user exist.
const user =
(hdrUser ? await prisma.user.findUnique({ where: { id: hdrUser } }) : null) ??
(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 hasAnySession = Boolean(cookieUser || cookieTeam || cookieConv || hdrTeam || hdrUser || hdrConv);
if (!hasAnySession) {
throw createError({ statusCode: 401, statusMessage: "Unauthorized" });
}
const team =
(hdrTeam
? await prisma.team.findUnique({ where: { id: hdrTeam } })
: null) ??
(await prisma.team.upsert({
where: { id: "demo-team" },
update: { name: "Demo Team" },
create: { id: "demo-team", name: "Demo Team" },
}));
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 conversation =
(hdrConv
? await prisma.chatConversation.findUnique({ where: { id: hdrConv } })
: null) ??
(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: conversation.id };
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 };
}

View 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;
}

View 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;
}