- Streaming AI endpoint at /campanha/api/ai-chat with DeepSeek V4 Flash - 8 tools: calculate, getMunicipalityVotes, getTopDeputies, getDobradinhas, getMunicipalityOverview, getLeaderships, getOrganizations, searchEntities - In-memory rate limiter (50 msgs / 15 min per user) - Desktop: resizable right sidebar via react-resizable-panels - Mobile: full-screen Drawer opened by FAB (2/3 size of quick-actions FAB) - Markdown rendering with react-markdown + remark-gfm (tables, bold, lists) - Session-bound context (clean slate each open) - Auth via existing campaign-token cookie, tools respect user RBAC - Architecture doc: docs/plans/ai-chat-sollinha.md - E2E tests: campaignAiChat.e2e.spec.ts (mocked endpoint)
136 lines
4 KiB
TypeScript
136 lines
4 KiB
TypeScript
import { tool } from 'ai'
|
|
import { z } from 'zod'
|
|
|
|
import type { AIToolContext } from '@/lib/ai/types'
|
|
|
|
type SearchHit = {
|
|
collection: string
|
|
label: string
|
|
description: string
|
|
href?: string
|
|
}
|
|
|
|
export const searchEntities = (ctx: AIToolContext) =>
|
|
tool({
|
|
description:
|
|
'Fuzzy search across municipalities, leaderships, organizations, and state deputies. ' +
|
|
'Use this FIRST when the user mentions an entity name and you are not sure which tool to call. ' +
|
|
'Returns categorized results that help you decide which specific tool to use next.',
|
|
inputSchema: z.object({
|
|
query: z
|
|
.string()
|
|
.describe('Search term (municipality name, person name, organization name, etc.).'),
|
|
}),
|
|
execute: async ({ query }) => {
|
|
const { payload } = ctx
|
|
|
|
const hits: SearchHit[] = []
|
|
|
|
// Search municipalities
|
|
const munResult = await payload.find({
|
|
collection: 'municipality',
|
|
where: {
|
|
or: [{ name: { like: query } }, { city: { like: query } }, { region: { like: query } }],
|
|
},
|
|
depth: 0,
|
|
limit: 5,
|
|
pagination: false,
|
|
select: { name: true, slug: true, city: true, region: true },
|
|
sort: 'name',
|
|
overrideAccess: false,
|
|
user: ctx.user,
|
|
})
|
|
|
|
for (const doc of munResult.docs as Array<Record<string, unknown>>) {
|
|
hits.push({
|
|
collection: 'municipality',
|
|
label: doc.name as string,
|
|
description: `Município — ${doc.city} (${doc.region})`,
|
|
href: `/campanha/municipios/${doc.slug}`,
|
|
})
|
|
}
|
|
|
|
// Search leaderships
|
|
const leadResult = await payload.find({
|
|
collection: 'leadership',
|
|
where: {
|
|
or: [{ 'contact.name': { like: query } }, { 'municipalities.name': { like: query } }],
|
|
},
|
|
depth: 1,
|
|
limit: 5,
|
|
pagination: false,
|
|
select: { contact: true, municipalities: true },
|
|
sort: '-updatedAt',
|
|
overrideAccess: false,
|
|
user: ctx.user,
|
|
})
|
|
|
|
for (const doc of leadResult.docs as Array<Record<string, unknown>>) {
|
|
const contact = doc.contact as { name?: string } | undefined
|
|
const municipalities = (doc.municipalities as Array<{ name?: string }>) ?? []
|
|
hits.push({
|
|
collection: 'leadership',
|
|
label: contact?.name ?? 'Liderança',
|
|
description: `Liderança — ${municipalities.map((m) => m.name).join(', ')}`,
|
|
})
|
|
}
|
|
|
|
// Search organizations
|
|
const orgResult = await payload.find({
|
|
collection: 'organization',
|
|
where: {
|
|
or: [{ name: { like: query } }, { 'municipalities.name': { like: query } }],
|
|
},
|
|
depth: 0,
|
|
limit: 5,
|
|
pagination: false,
|
|
select: { name: true, kind: true },
|
|
sort: 'name',
|
|
overrideAccess: false,
|
|
user: ctx.user,
|
|
})
|
|
|
|
for (const doc of orgResult.docs as Array<Record<string, unknown>>) {
|
|
hits.push({
|
|
collection: 'organization',
|
|
label: doc.name as string,
|
|
description: `Organização — ${doc.kind as string}`,
|
|
})
|
|
}
|
|
|
|
// Search state deputies
|
|
const depResult = await payload.find({
|
|
collection: 'stateDeputy',
|
|
where: {
|
|
name: { like: query },
|
|
},
|
|
depth: 0,
|
|
limit: 5,
|
|
pagination: false,
|
|
select: { name: true, party: true },
|
|
sort: 'name',
|
|
overrideAccess: false,
|
|
user: ctx.user,
|
|
})
|
|
|
|
for (const doc of depResult.docs as Array<Record<string, unknown>>) {
|
|
hits.push({
|
|
collection: 'stateDeputy',
|
|
label: doc.name as string,
|
|
description: `Dobradinha — ${(doc.party as string) ?? 'sem partido'}`,
|
|
})
|
|
}
|
|
|
|
if (hits.length === 0) {
|
|
return {
|
|
message: `Nenhum resultado encontrado para "${query}". Tente um termo diferente.`,
|
|
}
|
|
}
|
|
|
|
return {
|
|
termo: query,
|
|
total: hits.length,
|
|
resultados: hits,
|
|
}
|
|
},
|
|
})
|