Files
accounted/app/api/agent/knowledge/route.ts
T
Jakob Wennberg dcd33997b7 feat(agent): move 'Vad din agent vet' into settings (Assistenten -> Kunskap) (#1008)
Relocates the ledger-knowledge surface off the top nav and into the
assistant settings hub as a third tab (Minne / Kompetens / Kunskap), per
the code's own "minne + kunskap under Assistenten" intent and the #935
flag that this was an easy call to change.

Because both settings surfaces (the full-page rail and the intercepting
settings modal) mount each section as a propless component via
SETTINGS_SECTIONS, the knowledge data must be fetched client-side rather
than passed as a server prop:

- New GET /api/agent/knowledge aggregates buildLedgerContext +
  buildDeepEntities + buildAgentCompetence + company name (read-only,
  company-scoped via withRouteContext).
- AgentKnowledgeView + AgentCompetenceSections converted from async
  server components to client components (getTranslations ->
  useTranslations; no other server-only usage).
- New AgentKnowledgePanel client wrapper lazy-fetches the payload when
  the Kunskap tab opens (Radix unmounts inactive tabs), with Skeleton and
  error states matching the memory/skills panels.
- Removed the Brain/agent-knowledge entry (and its now-unused import)
  from the Analys nav group.
- /agent-knowledge kept as a redirect to /settings/assistant?view=knowledge
  so old links/bookmarks resolve.

Tests: new route test (auth 401, no-company 400, happy-path aggregation).
i18n: load_error_* keys added to both locales (parity kept).

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-12 22:21:08 +02:00

31 lines
1.4 KiB
TypeScript

import { NextResponse } from 'next/server'
import { withRouteContext } from '@/lib/api/with-route-context'
import { getCompanyDisplayName } from '@/lib/company/context'
import { buildLedgerContext } from '@/lib/agent-context/ledger-context'
import { buildDeepEntities } from '@/lib/agent-context/ledger-deep'
import { buildAgentCompetence } from '@/lib/agent-context/agent-competence'
// GET /api/agent/knowledge
//
// Read-only transparency surface for "Vad din agent vet": the exact ledger
// context the AI agent reads before booking, plus the deep entity-resolved
// profile and the agent's competence. Powers the Kunskap tab in the assistant
// settings hub (client-fetched so it renders identically in the full-page
// settings rail and the routed settings modal, which both mount the same
// propless content component). Derived per request from live bookings; never
// cached, so a fixed profile can't go stale.
export const GET = withRouteContext('agent.knowledge.get', async (_request, ctx) => {
const { supabase, companyId } = ctx
const [context, deep, competence, companyName] = await Promise.all([
buildLedgerContext(supabase, companyId),
buildDeepEntities(supabase, companyId),
buildAgentCompetence(supabase, companyId),
getCompanyDisplayName(supabase, companyId),
])
return NextResponse.json({
data: { context, deep, competence, companyName: companyName ?? '' },
})
})