4f0a7b1db0
* feat(entitlements): capability-grant gate substrate (paywall + modularity) Two-axis capability primitive behind the SaaS paywall and the per-tenant modularity/marketplace vision: - migration: capability_grants (entitlement axis, polymorphic company/firm scope), company_capability_config (enablement axis), metered_events (append-only), company_has_capability() RPC reusing the 20260619130100 tenant guard; SELECT-only RLS (writes service-role only, no self-grant). - lib/entitlements: hasCapability/requireCapability gate (mirrors guardSandbox, fail-closed, NEXT_PUBLIC_SELF_HOSTED bypass), capability key namespace, metering helper. - unit (11) + pg-real tests (RPC/RLS/tenant-guard incl. no-self-grant). Gate not yet wired into call sites (follow-up commit). Paid keys: ai, bank_sync, skatteverket, email_send. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(entitlements): enforce capability gate at paid external-service chokepoints Wire the gate into the paid surfaces (keys: ai, email_send, bank_sync, skatteverket): - AI routes (agent invoke/composer/onboarding stream): requireCapability(ai) - Invoice send (web + v1): requireCapability(email_send) - document-extraction event handler: skip Bedrock extract if ai not entitled - enable-banking + skatteverket crons: per-company hasCapability skip in loop - colocated send-route test mocks updated (requireCapability -> null) Free per founder decision: TIC org lookup, VIES VAT validation, FX auto-fetch, cloud backup, BankID login, all internal bookkeeping. DEPLOY ORDER: fail-closed by design — do NOT deploy before trial/comp grant seeding lands, or companies without grants lose these features. Seeding + Stripe checkout/webhook are the next steps. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(entitlements): seed trial + comp capability grants Makes the fail-closed gate safely deployable — nobody is locked out at cutover: - AFTER INSERT trigger on companies grants every NEW company a 30-day trial on the PAID keys (ai, bank_sync, skatteverket, email_send), on ALL creation paths (RPC/MCP/direct) — so a new signup can use onboarding AI immediately. - one-time backfill for EXISTING companies: created <=2026-06-07 -> trial ends 2026-07-07; created later -> created_at + 30 days. - permanent comp grants for Arcim/Mattsson (matched by name, no hardcoded UUIDs). - pg tests: clearGrants() for controlled resolver tests + trigger coverage. Trigger fn is SECURITY DEFINER so it writes grants regardless of caller RLS (table has no INSERT policy for authenticated — no self-grant). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(entitlements): client capability visibility + billing page Non-payers get a clean upsell instead of broken/empty features: - CompanyContext gains capabilities[] + useCapability(key); resolved once server-side in the dashboard layout via getCompanyCapabilities (batched, 2 queries), all three provider branches wired. - /settings/billing upgrade page — the destination upsells point to (Stripe Payment Link via NEXT_PUBLIC_STRIPE_PAYMENT_LINK; degrades to 'coming soon' until automated checkout lands). - ChatEmptyState: non-payer sees an Uppgradera CTA (mirrors the sandbox state). - SendInvoiceDialog: email send disabled + upsell note when email_send missing (extends the existing sandbox-disable pattern). Fast-follow: chat input/FAB + document-inbox empty state + bank/skatteverket/ AI-suggest buttons + a shared capability_blocked->toast backstop. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(entitlements): gate remaining paid UI surfaces with upsell (fast-follow) disable-with-upsell across the rest of the paid surfaces (keys: bank_sync, skatteverket, ai): - BankSyncNowButton: sync/reconnect disabled + note when !bank_sync (CSV/SIE stays free) - AGIPanel: AGI submit-to-Skatteverket disabled + note when !skatteverket - SkatteverketConnectPanel: BankID connect/reconnect disabled + upsell - ApprovalCard: AI re-propose (correction) gated; manual approve/reject stay free - InvoiceInboxWorkspace: upsell when extraction empty AND !ai (deterministic parse + manual entry unaffected) - AgentTrigger FAB: routes to /settings/billing when !ai (no dead chat) - settings nav: 'Abonnemang'/'Subscription' link to /settings/billing (sv/en) TaxPaymentPanel + TransactionInboxCard intentionally untouched — only local/ deterministic actions there, nothing paid+external to gate. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(entitlements): automated Stripe subscription checkout + webhook Self-serve revenue wired to the same capability-grant primitive: - migration: company_subscriptions (company<->Stripe link/status) + stripe_webhook_events (idempotency) - lib/stripe: getStripe singleton, plan->price mapping, subscription-sync (statusGrantsAccess / subscriptionToState / applySubscriptionState / handleStripeEvent). Active sub -> upsert source='stripe' grants for PAID keys (expiry = period_end + 3d grace); canceled/unpaid -> remove ONLY stripe grants (freeze-and-retain). - routes: POST /api/billing/checkout (hosted subscription Checkout, company_id metadata), POST /api/billing/portal (Customer Portal), POST /api/stripe/webhook (raw-body signature verify, event-id dedup; handles checkout.session.completed + customer.subscription.*) - billing page: real plan-toggle Checkout CTA / manage-subscription portal, gated on isStripeConfigured() - adds stripe@22; unit tests for sync logic Provisioning is webhook-driven (never trusts the success redirect). Needs env: STRIPE_SECRET_KEY, STRIPE_WEBHOOK_SECRET, STRIPE_PRICE_MONTHLY, STRIPE_PRICE_YEARLY. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(entitlements): validate UUIDs in capability filter + log webhook errors Addresses PR review (Superagent Security / PR Agent): - has-capability.ts: validate companyId/teamId as UUIDs before interpolating into the PostgREST .or() filter (fail-closed) — removes the latent injection vector flagged in the entitlement gate. Unit tests updated to use UUIDs. - stripe/webhook: log processing failures with event id + type before the generic 500, so a failing webhook is visible to operators. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(salary): always-free AGI XML download for manual filing; only direct API submit is paid Per founder decision on the swedish-compliance-review finding: AGI is a mandatory statutory filing, so producing/downloading the AGI XML must never be paywalled. Adds a free 'Ladda ner AGI-fil' button (generates + downloads the XML for manual upload to Skatteverket's e-service) on all tiers; the gated 'Skicka in underlag' stays the paid convenience (direct API submission — which also requires the paid BankID connection). Upsell reworded to point to the manual path. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(entitlements): harden comp-grant match after prod verification Verified Arcim/Mattsson in prod (pwxtzglxptnnvjrpixpg): the name match was case-sensitive (missed the active 'Arcim technology AB' lowercase variant) and would have granted 3 archived dupes. Now match by org_number (5595386219 / 5595719864) OR case-insensitive name, active companies only — hits exactly the 3 active comp companies, excludes archived dupes and the unrelated 'Amnäs Mattsson, Emil' enskild firma. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
304 lines
11 KiB
TypeScript
304 lines
11 KiB
TypeScript
import { createClient } from '@/lib/supabase/server'
|
|
import { NextResponse } from 'next/server'
|
|
import { z } from 'zod'
|
|
import { ensureInitialized } from '@/lib/init'
|
|
import { getActiveCompanyId } from '@/lib/company/context'
|
|
import { getIntent } from '@/lib/agent/intents/registry'
|
|
import { checkAgentRateLimit, agentRateLimitResponseBody } from '@/lib/rate-limits/agent'
|
|
import { runChatTurn, friendlyModelError } from '@/lib/agent/chat/run-turn'
|
|
import { guardSandbox } from '@/lib/sandbox/guard'
|
|
import { requireCapability } from '@/lib/entitlements/has-capability'
|
|
import { CAPABILITY } from '@/lib/entitlements/keys'
|
|
|
|
// Make sure extensions are loaded — the chat loop dispatches against the
|
|
// agent tool registry which is populated by the mcp-server extension at load.
|
|
ensureInitialized()
|
|
|
|
// Hard cap on the per-turn user input. Generous for a chat composer (about
|
|
// 5k words / 20 pages) but bounds Bedrock token cost if the rate limiter is
|
|
// ever fail-open and a client floods large payloads.
|
|
const MAX_USER_MESSAGE_LEN = 20_000
|
|
|
|
const BodySchema = z.object({
|
|
intent_id: z.string().min(1).max(200),
|
|
// Existing conversation to resume; if omitted, the route creates one. The
|
|
// chat sheet's React state holds the conversation id as `string | null`
|
|
// and serializes `null` on the first turn, so accept null alongside
|
|
// undefined and treat both as "no existing conversation".
|
|
conversation_id: z.string().uuid().nullable().optional(),
|
|
// Optional company override; defaults to active_company_id.
|
|
company_id: z.string().uuid().nullable().optional(),
|
|
// The user's message (or, on the first turn, this is empty and we send the
|
|
// intent's prompt template instead). Capped to bound LLM cost.
|
|
user_message: z.string().max(MAX_USER_MESSAGE_LEN).nullable().optional(),
|
|
// Intent-specific capture args (e.g. { transaction_id: '...' } for
|
|
// transaction.categorization). Used only on the first turn to build the
|
|
// prompt template. Each value is bounded so capture inputs can't be a
|
|
// megabyte each; the dispatcher rejects oversize values upfront.
|
|
intent_args: z
|
|
.record(z.string().max(120), z.unknown())
|
|
.nullable()
|
|
.optional()
|
|
.refine(
|
|
(v) => {
|
|
if (!v) return true
|
|
try {
|
|
return JSON.stringify(v).length <= MAX_USER_MESSAGE_LEN
|
|
} catch {
|
|
return false
|
|
}
|
|
},
|
|
{ message: 'intent_args too large' },
|
|
),
|
|
// Optional context_ref for the conversation row, e.g. 'transaction:<id>'.
|
|
context_ref: z.string().max(200).nullable().optional(),
|
|
// When true (and user_message is provided), persist the turn but flag it
|
|
// hidden so it doesn't render as a user bubble on resume. Used by the chat's
|
|
// rejection-correction flow (ApprovalCard → AgentChat) to feed the agent a
|
|
// synthetic correction without showing it as something the user typed.
|
|
user_message_hidden: z.boolean().nullable().optional(),
|
|
})
|
|
|
|
// POST /api/agent/invoke
|
|
//
|
|
// Streams NDJSON events from the chat loop. Each line is a JSON object whose
|
|
// `kind` identifies the event type — see lib/agent/chat/run-turn.ts StreamEvent.
|
|
//
|
|
// Auth: the user must be a member of the resolved company.
|
|
//
|
|
// Plan ref: dev_docs/specialized-agent-plan.md §9 (chat loop).
|
|
export async function POST(request: Request) {
|
|
const supabase = await createClient()
|
|
const { data: { user } } = await supabase.auth.getUser()
|
|
if (!user) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
|
|
// Generous per-user rate limit — bounds runaway Bedrock spend (loop-firing
|
|
// sessions). Fails open on infra error.
|
|
const rate = await checkAgentRateLimit(supabase, user.id)
|
|
if (!rate.ok) {
|
|
return NextResponse.json(agentRateLimitResponseBody(rate), {
|
|
status: 429,
|
|
headers: rate.retryAfterSec ? { 'Retry-After': String(rate.retryAfterSec) } : undefined,
|
|
})
|
|
}
|
|
|
|
let body: z.infer<typeof BodySchema>
|
|
try {
|
|
body = BodySchema.parse(await request.json())
|
|
} catch (err) {
|
|
return NextResponse.json(
|
|
{ error: err instanceof Error ? err.message : 'Invalid body' },
|
|
{ status: 400 },
|
|
)
|
|
}
|
|
|
|
const intent = getIntent(body.intent_id)
|
|
if (!intent) {
|
|
return NextResponse.json({ error: `Unknown intent: ${body.intent_id}` }, { status: 400 })
|
|
}
|
|
|
|
const companyId = body.company_id ?? (await getActiveCompanyId(supabase, user.id))
|
|
if (!companyId) return NextResponse.json({ error: 'No active company' }, { status: 400 })
|
|
|
|
const { data: membership } = await supabase
|
|
.from('company_members')
|
|
.select('role')
|
|
.eq('company_id', companyId)
|
|
.eq('user_id', user.id)
|
|
.maybeSingle()
|
|
if (!membership) return NextResponse.json({ error: 'Forbidden' }, { status: 403 })
|
|
|
|
// No Anthropic Bedrock calls in the sandbox — the demo runs entirely on
|
|
// seed data and the assistant is gated to a "look, don't touch" preview.
|
|
const blocked = await guardSandbox(supabase, companyId)
|
|
if (blocked) return blocked
|
|
|
|
const capBlocked = await requireCapability(supabase, companyId, CAPABILITY.ai)
|
|
if (capBlocked) return capBlocked
|
|
|
|
// onboarding.intake completion signal — once the user has actually
|
|
// engaged (typed a real reply, not the auto-fired greeting prompt that
|
|
// mounts the chat), stamp intake_completed_at on the profile so re-entry
|
|
// logic and opportunistic follow-up logic in other intents can tell the
|
|
// intake happened. Idempotent: the IS NULL guard ensures we never
|
|
// overwrite the first engagement timestamp. Best-effort — failure here
|
|
// doesn't break the chat; the next user turn retries.
|
|
if (
|
|
body.intent_id === 'onboarding.intake' &&
|
|
typeof body.user_message === 'string' &&
|
|
body.user_message.trim().length > 0 &&
|
|
body.user_message_hidden !== true
|
|
) {
|
|
try {
|
|
await supabase
|
|
.from('agent_profiles')
|
|
.update({ intake_completed_at: new Date().toISOString() })
|
|
.eq('company_id', companyId)
|
|
.is('intake_completed_at', null)
|
|
} catch {
|
|
// ignored — see comment above
|
|
}
|
|
}
|
|
|
|
// Load lightweight company + user signals for the system prompt.
|
|
const [{ data: company }, { data: profile }] = await Promise.all([
|
|
supabase.from('companies').select('name').eq('id', companyId).single(),
|
|
supabase.from('profiles').select('full_name').eq('id', user.id).single(),
|
|
])
|
|
const companyName = company?.name ?? ''
|
|
const firstName = profile?.full_name?.split(' ')[0] ?? null
|
|
|
|
// Resolve / create the conversation row.
|
|
let conversationId = body.conversation_id ?? null
|
|
if (!conversationId) {
|
|
const { data: newConv, error: convErr } = await supabase
|
|
.from('agent_conversations')
|
|
.insert({
|
|
company_id: companyId,
|
|
user_id: user.id,
|
|
intent_id: body.intent_id,
|
|
context_ref: body.context_ref ?? null,
|
|
title: intent.sheetTitle,
|
|
})
|
|
.select('id')
|
|
.single()
|
|
if (convErr || !newConv) {
|
|
return NextResponse.json(
|
|
{ error: convErr?.message ?? 'Failed to create conversation' },
|
|
{ status: 500 },
|
|
)
|
|
}
|
|
conversationId = newConv.id as string
|
|
}
|
|
|
|
// Compute the user message to send to Anthropic. On the first turn (no
|
|
// user_message provided), we run the intent's capture + promptTemplate
|
|
// pipeline so the prompt is anchored on the page context the user
|
|
// clicked from.
|
|
let effectiveUserMessage = body.user_message ?? ''
|
|
// When the caller didn't supply a user_message, we synthesize one from the
|
|
// intent's promptTemplate. Mark that synthetic turn hidden so the UI
|
|
// doesn't render the template scaffolding as a user bubble on resume. The
|
|
// client can also explicitly request a hidden turn (rejection correction)
|
|
// even when it DID supply a user_message.
|
|
let userMessageHidden = body.user_message_hidden === true
|
|
if (!effectiveUserMessage) {
|
|
try {
|
|
const captured = await intent.capture(body.intent_args ?? {}, {
|
|
supabase,
|
|
userId: user.id,
|
|
companyId,
|
|
})
|
|
const profileSummary = await loadProfileSummary(supabase, companyId)
|
|
const memory = await loadRankedMemory(supabase, companyId, 30)
|
|
effectiveUserMessage = intent.promptTemplate({
|
|
captured,
|
|
profileSummary,
|
|
activeMemory: memory,
|
|
})
|
|
userMessageHidden = true
|
|
} catch (err) {
|
|
return NextResponse.json(
|
|
{
|
|
error:
|
|
err instanceof Error
|
|
? `Capture failed: ${err.message}`
|
|
: 'Capture failed',
|
|
},
|
|
{ status: 500 },
|
|
)
|
|
}
|
|
}
|
|
|
|
// Stream — NDJSON events from the chat loop.
|
|
const encoder = new TextEncoder()
|
|
// Conversation id is set above; capture into a non-null local for the
|
|
// streaming closure's first emission.
|
|
const convId: string = conversationId
|
|
|
|
const stream = new ReadableStream<Uint8Array>({
|
|
async start(controller) {
|
|
const emit = (event: unknown): boolean => {
|
|
try {
|
|
controller.enqueue(encoder.encode(JSON.stringify(event) + '\n'))
|
|
return true
|
|
} catch {
|
|
return false
|
|
}
|
|
}
|
|
|
|
// Surface the conversation id so the client can resume with it.
|
|
emit({ kind: 'conversation', conversation_id: convId })
|
|
|
|
try {
|
|
await runChatTurn({
|
|
supabase,
|
|
userId: user.id,
|
|
companyId,
|
|
companyName,
|
|
firstName,
|
|
intent,
|
|
conversationId: convId,
|
|
userMessage: effectiveUserMessage,
|
|
userMessageHidden,
|
|
persist: true,
|
|
emit: (event) => emit(event),
|
|
})
|
|
} catch (err) {
|
|
// run-turn already emitted a friendly error before re-throwing; emit a
|
|
// normalized one here too so this outer catch never overwrites it with a
|
|
// raw AWS SDK string.
|
|
emit({
|
|
kind: 'error',
|
|
message: friendlyModelError(err),
|
|
})
|
|
} finally {
|
|
try {
|
|
controller.close()
|
|
} catch {
|
|
// Already closed
|
|
}
|
|
}
|
|
},
|
|
})
|
|
|
|
return new Response(stream, {
|
|
headers: {
|
|
'Content-Type': 'application/x-ndjson; charset=utf-8',
|
|
'Cache-Control': 'no-store',
|
|
'X-Accel-Buffering': 'no',
|
|
},
|
|
})
|
|
}
|
|
|
|
async function loadProfileSummary(
|
|
supabase: Awaited<ReturnType<typeof createClient>>,
|
|
companyId: string,
|
|
): Promise<string | null> {
|
|
const { data } = await supabase
|
|
.from('agent_profiles')
|
|
.select('profile_summary')
|
|
.eq('company_id', companyId)
|
|
.maybeSingle()
|
|
return (data?.profile_summary as string | null) ?? null
|
|
}
|
|
|
|
async function loadRankedMemory(
|
|
supabase: Awaited<ReturnType<typeof createClient>>,
|
|
companyId: string,
|
|
cap: number,
|
|
): Promise<{ content: string; kind: string }[]> {
|
|
const { data } = await supabase
|
|
.from('agent_memory')
|
|
.select('content, kind, relevance_score, last_accessed_at')
|
|
.eq('company_id', companyId)
|
|
.eq('is_active', true)
|
|
.order('relevance_score', { ascending: false })
|
|
.order('last_accessed_at', { ascending: false, nullsFirst: false })
|
|
.limit(cap)
|
|
return (data ?? []).map((r: { content: string; kind: string }) => ({
|
|
content: r.content,
|
|
kind: r.kind,
|
|
}))
|
|
}
|