feat(chat): add contact inbox sources with per-user hide filters
This commit is contained in:
@@ -97,6 +97,7 @@ type SnapshotOptions = {
|
||||
teamId: string;
|
||||
contact?: string;
|
||||
contactsLimit?: number;
|
||||
messageWhere?: any;
|
||||
};
|
||||
|
||||
function makeId(prefix: string) {
|
||||
@@ -136,6 +137,7 @@ async function buildCrmSnapshot(input: SnapshotOptions) {
|
||||
include: {
|
||||
note: { select: { content: true, updatedAt: true } },
|
||||
messages: {
|
||||
where: input.messageWhere,
|
||||
select: { id: true, occurredAt: true, channel: true, direction: true, kind: true, content: true },
|
||||
orderBy: { occurredAt: "desc" },
|
||||
take: 4,
|
||||
@@ -389,6 +391,23 @@ export async function runLangGraphCrmAgentFor(input: {
|
||||
|
||||
// Keep the dataset fresh so the "CRM filesystem" stays in sync with DB.
|
||||
await ensureDataset({ teamId: input.teamId, userId: input.userId });
|
||||
const hiddenInboxRows = await prisma.contactInboxPreference.findMany({
|
||||
where: {
|
||||
teamId: input.teamId,
|
||||
userId: input.userId,
|
||||
isHidden: true,
|
||||
},
|
||||
select: { contactInboxId: true },
|
||||
});
|
||||
const hiddenInboxIds = hiddenInboxRows.map((row) => row.contactInboxId);
|
||||
const visibleContactMessageWhere = hiddenInboxIds.length
|
||||
? {
|
||||
OR: [
|
||||
{ contactInboxId: null },
|
||||
{ contactInboxId: { notIn: hiddenInboxIds } },
|
||||
],
|
||||
}
|
||||
: undefined;
|
||||
|
||||
const toolsUsed: string[] = [];
|
||||
const dbWrites: Array<{ kind: string; detail: string }> = [];
|
||||
@@ -542,10 +561,11 @@ export async function runLangGraphCrmAgentFor(input: {
|
||||
take: limit,
|
||||
include: {
|
||||
note: { select: { content: true, updatedAt: true } },
|
||||
messages: {
|
||||
select: { occurredAt: true, channel: true, direction: true, kind: true, content: true },
|
||||
orderBy: { occurredAt: "desc" },
|
||||
take: 1,
|
||||
messages: {
|
||||
where: visibleContactMessageWhere,
|
||||
select: { occurredAt: true, channel: true, direction: true, kind: true, content: true },
|
||||
orderBy: { occurredAt: "desc" },
|
||||
take: 1,
|
||||
},
|
||||
events: {
|
||||
select: { id: true, title: true, startsAt: true, endsAt: true, isArchived: true },
|
||||
@@ -645,10 +665,11 @@ export async function runLangGraphCrmAgentFor(input: {
|
||||
where: { id: target.id, teamId: input.teamId },
|
||||
include: {
|
||||
note: { select: { content: true, updatedAt: true } },
|
||||
messages: {
|
||||
select: { id: true, occurredAt: true, channel: true, direction: true, kind: true, content: true, durationSec: true, transcriptJson: true },
|
||||
orderBy: { occurredAt: "desc" },
|
||||
take: messagesLimit,
|
||||
messages: {
|
||||
where: visibleContactMessageWhere,
|
||||
select: { id: true, occurredAt: true, channel: true, direction: true, kind: true, content: true, durationSec: true, transcriptJson: true },
|
||||
orderBy: { occurredAt: "desc" },
|
||||
take: messagesLimit,
|
||||
},
|
||||
events: {
|
||||
select: { id: true, title: true, startsAt: true, endsAt: true, note: true, isArchived: true },
|
||||
@@ -1098,6 +1119,7 @@ export async function runLangGraphCrmAgentFor(input: {
|
||||
|
||||
const snapshot = await buildCrmSnapshot({
|
||||
teamId: input.teamId,
|
||||
messageWhere: visibleContactMessageWhere,
|
||||
...(focusedContact ? { contact: focusedContact } : {}),
|
||||
});
|
||||
const snapshotJson = JSON.stringify(snapshot, null, 2);
|
||||
|
||||
Reference in New Issue
Block a user