Files
accounted/app/api/agent/onboarding/stream/route.ts
T
Jakob WennbergandClaude Opus 4.7 20989379bb feat(sandbox,branding): prod-parity demo with AI gating + accounted rebrand (#585)
* feat(sandbox,branding): prod-parity demo with AI gating + accounted rebrand

Sandbox now ships with seeded suppliers, supplier invoices, an asset,
a verified agent_profile, and pending operations so the demo company
exercises every prod surface. Server-side `guardSandbox()` short-
circuits any AI or paid-external API call (Bedrock chat/composer,
Resend invoice send, Riksbanken FX, VIES, etc.) and the AgentSheet
swaps in a SandboxAgentPreview that explains what's gated and offers
a register CTA. DashboardContent no longer mounts the
NewUserChecklist when the agent is already built, fixing the path
that let sandbox users still trigger /onboarding/agent.

Visible branding flips from Gnubok to Accounted: new BrandWordmark
component (Hedvig Letters Serif 700), new app/icon.png + PWA icons
generated from the accounted icon, default appName updated. URLs,
header names, API key prefixes, hostnames, and event/cookie/
localStorage keys keep `gnubok` — the rebrand is visual only.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(sandbox): hardcode supplier-invoice arrival numbers in seed

get_next_arrival_number is MAX(arrival_number) + 1 against the same
table we're about to insert into. Calling it twice before either row
lands made both calls return 1, which then violated the
(company_id, arrival_number) unique index — POST /api/sandbox/seed
500'd on first sandbox start.

The seeded company is brand new in this branch so 1 and 2 are
guaranteed unused; hardcoding side-steps the race entirely.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(sandbox): set paid_amount=0 on unpaid supplier invoice

PostgREST normalizes the column set across rows in a bulk insert, so
the second supplier invoice (Espresso House, status=registered) was
being sent with paid_amount=null because the first row (Telia, paid)
set it. supplier_invoices.paid_amount is NOT NULL DEFAULT 0; the
default only kicks in when the column is *absent* from the payload,
not when it's explicitly null. Set it inline to side-step the
normalization.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(sandbox): set actor_type=agent_chat on seeded pending_operations

pending_operations only allows user-scoped INSERTs via the
`pending_operations_chat_insert` policy, which requires
actor_type='agent_chat' alongside auth.uid()=user_id +
company membership. The seed was inserting with the default
actor_type='user', tripping the RLS check.

Also lift risk_level from preview_data (where it was unused) onto
the row itself, matching the column added in
20260430120000_pending_operations_actor_and_risk.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(pr-review): address PR #585 review feedback

Fixes called out by the core-only CI check, Greptile, and the
compliance + Swedish-accounting bots:

- AGI Programnamn pinned back to 'gnubok' (CI blocker). The XML
  Skatteverket receives must keep the stable software identifier
  regardless of the visual rebrand — same rule as the v1 health
  endpoint's `service: 'gnubok'` literal.
- handleCreateAccount in SandboxAgentPreview + ChatEmptyState now
  wraps signOut() in try/catch so a transient Supabase failure
  doesn't strand the user on a dead button (greptile P2 × 2).
- /api/currency/rate hard-fails on missing companyId instead of
  conditionally skipping the sandbox guard (greptile P2 / compliance
  V8.2.1).
- topUpSandboxAdditions now delegates to ensureSandboxAgentProfile;
  the assistant persona lives in exactly one place across the seed,
  layout backfills, and top-up path (greptile P2 outside-diff /
  compliance SOC2 CC6.1).
- ensureSandboxAgentProfile drops the userId param and sets
  verified_by_user_id to NULL — synthetic seed data should not
  attribute verification to a real user (compliance V8.2.1 /
  GDPR Art. 25(2)). Errors now logged via the structured logger
  instead of being silently swallowed (V16).
- Sandbox seed swaps real-world company names (Telia, Espresso
  House) for clearly-synthetic Demo-prefixed brands using the
  5559... documentation org-number range (compliance A.8.33).
  Asset cost bumped 24 000 → 35 000 SEK so the demo clears the
  förbrukningsinventarier threshold and illustrates capitalization
  unambiguously (swedish-asset-accounting).
- Representation pending-operation preview corrected: VAT label
  fixed from 6% → 12%, and input VAT split between the avdragsgill
  (2641) and ej-avdragsgill (5811) portions to match
  swedish-vat / ML 8 kap rules (swedish-vat).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(pr-review): seed preview consistency + AGI Programnamn constant

Two last review-bot items before merge:

- Sandbox seed: the representation pending-operation preview was
  splitting the 240 SEK café meal 60/180 between 5810 and 5811,
  which is wrong for a single attendee under the 300 SEK / person
  avdragsgill cap (ML 8 kap) — the entire amount is fully
  avdragsgill in that case. Collapse the preview to a single 5810
  + 2641 + 2440 entry so it matches the supplier_invoice_items row
  1:1 and stops teaching demo users an incorrect bookkeeping
  pattern.
- Hoist the AGI Programnamn 'gnubok' literal into a named constant
  with a comment pointing to potential future Skatteverket vendor
  registration (per the swedish-compliance bot's nit).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(sandbox): avoid BFL duplicate-verification on pending op + fix VAT cap comment

Swedish-compliance bot caught two final nits:

- The pending operation for the Demokafé representation was using the
  same supplier_invoice_number as the already-seeded supplier_invoices
  row (88245). If the sandbox user approved the staged operation, the
  insert would have created (or attempted) a duplicate verification —
  BFL 5 kap. requires each affärshändelse be recorded exactly once.
  Swap the staged operation's invoice number to a distinct value
  (INKOMMANDE-2026-001) so approval cleanly creates a new row.
- The preview comment described the 300 SEK threshold as an
  "avdragsgill cap". The actual rule (ML 8 kap. 9 §) caps the
  deductible VAT at 25 % × 300 SEK × antal_personer = 75 SEK per
  person — the 300 SEK is the tax base, not the total. Math here is
  correct either way, but the comment now states the correct formula
  so future seed edits don't propagate the wrong understanding.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-28 13:19:41 +02:00

305 lines
12 KiB
TypeScript

import { createClient } from '@/lib/supabase/server'
import { NextResponse } from 'next/server'
import { z } from 'zod'
import { getActiveCompanyId } from '@/lib/company/context'
import { guardSandbox } from '@/lib/sandbox/guard'
import { checkAgentRateLimit, agentRateLimitResponseBody } from '@/lib/rate-limits/agent'
import { gatherComposerInputs, inputsToSourceSignals } from '@/lib/agent/composer/inputs'
import { selectAtoms } from '@/lib/agent/composer/atom-selection'
import { writeNarrative } from '@/lib/agent/composer/narrative'
import { fallbackAtomSelection, fallbackNarrative } from '@/lib/agent/composer/fallback'
import { filterRedundantQuestions } from '@/lib/agent/composer/atom-selection'
import { preWarmAtomCache } from '@/lib/agent/composer/prewarm'
import { OPUS_MODEL } from '@/lib/agent/composer/client'
import { ensureTicSnapshot } from '@/lib/agent/composer/tic-fetch'
import type { AtomSelection } from '@/lib/agent/composer/schemas'
const BodySchema = z.object({
company_id: z.string().uuid().optional(),
})
// 10s — the user is on a wait-screen with visible progress, so we can afford
// the longer budget. The prior 5s clipped legitimate fetches (TIC fans out to
// ~13 Lens calls upstream) into the fallback bucket while still burning the
// in-flight upstream calls against quota. See actions.ts:182-189 for the
// May 2026 incident context.
const TIC_BUDGET_MS = 10_000
// Bedrock cold-starts can take 2-3s before the first token, plus the actual
// Opus selection call typically lands at 10-14s. 15s was too tight and put
// real Opus calls into the fallback bucket on the first turn of the day.
const SELECT_BUDGET_MS = 25_000
const NARRATIVE_BUDGET_MS = 8_000
// Per-step event shape streamed as NDJSON. Each line is one JSON object.
type Step = 'tic' | 'select' | 'narrative' | 'finalize' | 'prewarm'
type Status = 'in_progress' | 'success' | 'fallback' | 'skipped' | 'error'
type StreamEvent =
| { step: Step; status: Status }
| { step: 'select'; status: 'success' | 'fallback'; selection: AtomSelection }
| { step: 'narrative'; status: 'success' | 'fallback'; narrative: string }
| { step: 'finalize'; status: 'success'; profile: ProfilePayload }
| { step: 'error'; status: 'error'; message: string }
interface ProfilePayload {
company_id: string
horizontal_atoms: string[]
vertical_atoms: string[]
modifier_atoms: string[]
is_multi_vertical: boolean
profile_summary: string
verification_questions: string[]
uncertainty_notes: string[]
composer_model: string
composed_at: string
}
// POST /api/agent/onboarding/stream
//
// Streams real-timed progress for the agent build sequence (plan §7 Phase A).
// Each step runs on its actual latency — no artificial delays. On timeout or
// failure, the step emits `fallback` and the pipeline continues with a
// deterministic default so the user always reaches Phase B.
//
// Response: application/x-ndjson — one JSON event per line.
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 reload-spam of the onboarding build
// (each run fires 2 LLM calls). 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(() => ({})))
} catch (err) {
return NextResponse.json(
{ error: err instanceof Error ? err.message : 'Invalid body' },
{ 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: 'Not a member of this company' }, { status: 403 })
}
// No live composer run for sandbox companies — they ship with a pre-built
// verified agent_profile so the chrome is visible without burning Bedrock.
const blocked = await guardSandbox(supabase, companyId)
if (blocked) return blocked
const stream = new ReadableStream<Uint8Array>({
async start(controller) {
const encoder = new TextEncoder()
const send = (event: StreamEvent) => {
try {
controller.enqueue(encoder.encode(JSON.stringify(event) + '\n'))
} catch {
// Stream was cancelled (user navigated away). Subsequent enqueues
// would throw — we just stop emitting.
}
}
try {
// Step 1 — TIC: read companies.tic_snapshot; if missing or stale,
// live-fetch from the TIC extension (cookies forwarded from the
// incoming request) and persist. Falls through gracefully when TIC is
// disabled, the company has no org_number, or the request times out.
send({ step: 'tic', status: 'in_progress' })
const cookieHeader = request.headers.get('cookie') ?? ''
// SSRF guard: derive origin from NEXT_PUBLIC_APP_URL (a required env
// var per CLAUDE.md) instead of request.headers.host. On self-hosted
// Docker the Host header can be attacker-controlled and would
// otherwise let an attacker redirect the cookie-bearing TIC fetch to
// a host they control.
const appUrl = process.env.NEXT_PUBLIC_APP_URL?.replace(/\/+$/, '') ?? ''
const fallbackHost = request.headers.get('host') ?? 'localhost:3000'
const fallbackProto =
request.headers.get('x-forwarded-proto') ??
(fallbackHost.startsWith('localhost') ? 'http' : 'https')
const origin = appUrl || `${fallbackProto}://${fallbackHost}`
const ticResult = await withTimeout(
// upgradeV1: agent build is the consumer of the v2-only sections;
// bounded to companies actively creating an agent, so the TIC
// budget stays safe even when upgrading pre-v2 snapshots.
// timeoutMs: lift the internal fetch signal to match the outer
// budget — otherwise the 5s default fires first and we get the
// pre-fix behavior even with the longer outer budget.
ensureTicSnapshot({
supabase,
companyId,
cookieHeader,
origin,
upgradeV1: true,
timeoutMs: TIC_BUDGET_MS,
}),
TIC_BUDGET_MS,
).catch(() => ({ snapshot: null, source: 'fallback' as const }))
send({
step: 'tic',
status: ticResult.snapshot ? 'success' : 'fallback',
})
// Gather inputs once — used by select + narrative + persistence.
const inputs = await gatherComposerInputs(supabase, companyId)
// Step 2 — Opus atom selection with timeout + deterministic fallback.
send({ step: 'select', status: 'in_progress' })
let selection: AtomSelection
try {
selection = await withTimeout(selectAtoms(inputs), SELECT_BUDGET_MS)
send({ step: 'select', status: 'success', selection })
} catch {
selection = fallbackAtomSelection(inputs)
send({ step: 'select', status: 'fallback', selection })
}
// Filter verification questions deterministically regardless of which
// path produced the selection. fallbackAtomSelection generates a
// generic template that doesn't know about KÄNDA FAKTA; the Opus
// path is also re-filtered in case the model strayed. Cheap belt-
// and-braces — same function used for both.
selection.verification_questions = filterRedundantQuestions(
selection.verification_questions,
inputs,
selection.modifier_atoms,
)
// Step 3 — Sonnet narrative with timeout + plain fallback.
send({ step: 'narrative', status: 'in_progress' })
let narrative: string
try {
narrative = await withTimeout(writeNarrative(inputs, selection), NARRATIVE_BUDGET_MS)
send({ step: 'narrative', status: 'success', narrative })
} catch {
narrative = fallbackNarrative(inputs)
send({ step: 'narrative', status: 'fallback', narrative })
}
// Step 4 — Persist the profile so Phase B has a row to edit.
const composedAt = new Date().toISOString()
const sourceSignals = inputsToSourceSignals(inputs)
const { error: upsertErr } = await supabase
.from('agent_profiles')
.upsert(
{
company_id: companyId,
horizontal_atoms: selection.horizontal_atoms,
vertical_atoms: selection.vertical_atoms,
modifier_atoms: selection.modifier_atoms,
profile_summary: narrative,
source_signals: sourceSignals,
// Persist so the Phase C intake agent can read them server-
// side when the chat opens. Plan §7 Phase C.
verification_questions: selection.verification_questions,
composed_at: composedAt,
composer_model: OPUS_MODEL,
composer_version: 1,
},
{ onConflict: 'company_id' },
)
if (upsertErr) {
send({ step: 'error', status: 'error', message: upsertErr.message })
return
}
send({
step: 'finalize',
status: 'success',
profile: {
company_id: companyId,
horizontal_atoms: selection.horizontal_atoms,
vertical_atoms: selection.vertical_atoms,
modifier_atoms: selection.modifier_atoms,
is_multi_vertical: selection.is_multi_vertical,
profile_summary: narrative,
verification_questions: selection.verification_questions,
uncertainty_notes: selection.uncertainty_notes,
composer_model: OPUS_MODEL,
composed_at: composedAt,
},
})
// Step 5 — fire-and-forget cache pre-warm. The client renders the
// review card already; pre-warm just buys a faster first chat turn.
send({ step: 'prewarm', status: 'in_progress' })
const allIds = [
...selection.horizontal_atoms,
...selection.vertical_atoms,
...selection.modifier_atoms,
]
if (allIds.length > 0) {
const { data: rows } = await supabase
.from('agent_atom_registry')
.select('id, body')
.in('id', allIds)
const bodies = (rows ?? [])
.map((r: { body: string | null }) => r.body ?? '')
.filter((b: string) => b.length > 0)
void preWarmAtomCache({ atomBodies: bodies })
}
send({ step: 'prewarm', status: 'success' })
} catch (err) {
send({
step: 'error',
status: 'error',
message: err instanceof Error ? err.message : 'Composer pipeline failed',
})
} 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',
},
})
}
// Run a promise against a wall-clock budget. The underlying work continues to
// completion on the server when the budget elapses — we just stop waiting for
// it. For Anthropic calls that's fine: a slow Opus turn finishing later still
// warms its own cache.
function withTimeout<T>(promise: Promise<T>, ms: number): Promise<T> {
return new Promise<T>((resolve, reject) => {
const timer = setTimeout(() => reject(new Error(`Timeout after ${ms}ms`)), ms)
promise.then(
(v) => {
clearTimeout(timer)
resolve(v)
},
(e) => {
clearTimeout(timer)
reject(e)
},
)
})
}