Agent v1 bundle: TIC v2 onboarding, in-app assistant gating, sidebar nav, MCP fixes (#584)
* fix(sie-import): accept tab as field separator (Bollbok exports) The SIE 4 spec allows either space or tab between fields, but splitSIELine() only treated space (0x20) as a separator. Bollbok exports tab-separated lines for every record except #RAR, which silently swallowed all #IB / #UB / #KONTO / #KTYP / #VER / #TRANS records — imports appeared empty even though the file was well-formed. Also adds a parser-side diagnostic that emits a warning when raw #IB or #VER lines are present in the input but parsing produced none. The previous silent failure is how this bug stayed hidden; the warning gives the import preview something visible to surface next time. Verified against two real reproducer files (Sean / Erik Hellqvist): erik h 2025.SE (UTF-8): 166 accounts, 66 IB, 4 UB, 11 RES, 95 vouchers, 198 TRANS. erik h 2026.SE (CP437): 166 accounts, 66 IB, 4 UB, 0 vouchers. Both now parse with zero warnings/errors. Tests: + 8 Bollbok-shape tab-separated fixtures (2025 + 2026 quoting variants). + 4 silent-failure diagnostic-warning tests. All 74 sie-parser tests pass; 155/155 in lib/import; 64/64 downstream callers. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * fix(sie-import): address PR #513 review — strip #KTYP quotes, suppress redundant aggregate warning Two non-blocking P2 findings from Greptile review on PR #513: 1. #KTYP handler stored fields[2] directly, so Bollbok 2026 exports (#KTYP\t1510\t"T") stored '"T"' with literal quotes instead of 'T'. Latent defect — accountType is unused downstream today, but my tab- separator fix made the quoted-value path reachable. Now routes through parseStringField so both Bollbok 2025 (unquoted T) and 2026 (quoted "T") land as 'T'. 2. The aggregate "kontrollera fältavskiljare och teckenkodning" warning fired alongside per-record 'error'-severity issues for malformed #IB / #VER records, producing a misleading hint when the parser had already pinpointed the structural problem. Now suppressed when an error-severity issue with the same tag already exists. Test coverage: + accountType asserted to be 'T' (not '"T"') in both 2025 + 2026 shapes. + VER aggregate-warning test now uses #VER lines without { } blocks (silent loss, no per-record error) — the canonical case the diagnostic is designed for. + New suppression test: bare #VER produces per-record errors AND the aggregate warning is absent. 75/75 sie-parser tests pass; 156/156 in lib/import. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * wip: agent chat + composer + memory + document extraction In-progress work on this branch beyond the SIE-import fixes: - Specialized accountant agent (composer + intents + chat loop) - Persistent agent_conversations/messages, agent_profiles, agent_memory - /chat surface + /onboarding/agent + /settings/agent-memory - document-extraction extension with status hooks - MCP server staging refactor + new skills (atoms, bank reconciliation, customer onboarding, kreditfaktura) - pending_operations rejection feedback (category + reason) + realtime - TIC company profile cached snapshot on companies - 17 migrations (all additive — see prior conversation analysis) Parked while branch waits for review/merge. Migrations are already applied to prod. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * refactor(tic): migrate company-data client from api-core v1 to Lens v2 Swaps the seven TIC company-data endpoints we call from the api-core paths (`/datasets/companies/{companyId}/...`, `/search/companies`) to the Lens equivalents (`/companies/{id}/...`, `/search-public/companies`). Hard cutover; proxy pattern preserved. Schema shifts handled inside the extension so consumers (TicWorkspace, Step2CompanyDetails) don't need changes: - `/companies/{id}/bank-accounts` now returns Bankgirot only — map to the existing `{ type, accountNumber, bic }` shape, drop terminated. - `/companies/{id}/industries` returns a discriminated array — filter to `companyIndustryCodeType === 'sni2007'` to preserve v1 behavior. - `/companies/{id}/phone-numbers` renamed the field to `phoneNumberFormatted` (fall back to `e164PhoneNumber`). - `/companies/{id}/documents` replaces `/financial-report-summaries`; filter `type === 'annualReport'` and read nested `financialReportMetadata` to rebuild the legacy summary shape. - `isCeased` is now a top-level boolean; `activityStatus` is an enum. Translate enum -> 'ceased' for the workspace's existing check. BankID identity flow (id.tic.io) is untouched — separate TIC product. Note: deploy gated on the TIC proxy being flipped to lens-api.tic.io with an `x-api-key` Lens key. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * feat(tic): expose v2 onboarding & workspace data Adds six new Lens (v2) fetchers on top of the migration that already landed in this branch, surfacing the data through /lookup and /profile. New fetchers in lib/tic-client.ts: - getFiscalYears /companies/{id}/fiscal-years - getAccountingPeriods /companies/{id}/accounting-periods - getPayrolls /companies/{id}/payrolls - getSignatory /companies/{id}/signatory - getRepresentatives /companies/{id}/representatives - getCompanyStatus /companies/{id}/status /lookup gains a fiscalYear field (current fiscal-year configuration) so onboarding Step 2 can skip manual MM-DD entry. CompanyLookupResult extended with optional fiscalYear; consumers without it keep working. /profile gains five new sections on TICCompanyProfile: - fiscalYear + fiscalYearHistory current + deduped period list - signatory firmateckning descriptions - board + representatives board-composition summary + active officers (positionEnd in future) - payrolls payroll2 array newest-first, with deviation vs annual-report - statuses current+historical status entries with red/yellow/green/neutral color TicWorkspace renders the new data as four cards (Status, Fiscal year + Signatory, Board + Representatives, Payroll history) plus a Badge mapping for the traffic-light status color. Tests: 52 -> 60 passing. Added unit tests for the new fetchers' v2 paths, fiscal-year auto-fill in /lookup, and full v2 profile coverage. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * feat(onboarding,agent): lean on TIC v2 to skip Steps 1 & 3 and sharpen Opus Three small wins that unlock more of the v2 cutover. No new endpoints — the data was already in the snapshot, just not flowing where it should. Step 1 (entity_type) — deep-link path only: - /lookup now returns `legalEntityType` and `registrationDate` (added to CompanyLookupResult). - /onboarding/page.tsx does a server-side /lookup prefetch when ?org_number= is present (BankID picker path), maps "AB"/"EF" to the EntityType enum, and seeds Step 1's radio. Falls through silently for unsupported codes (HB, KB, …) and on TIC errors. - WelcomeOnboarding hydrates ticLookup state from the server prefetch so Step 2's debounced client fetch and Step 3's first-year inference both have data on first render — no flash. Step 3 (is_first_fiscal_year) — every path: - deriveFirstYearDefaults() parses ticLookup.registrationDate and returns { isFirstFiscalYear, firstYearStart } when registered <12 months ago. Step 3's initialData picks it up; the user only confirms the end date. - Settings value wins when present so existing users with a saved choice don't get overridden. Composer prompt: - redactTic allowlist was the bottleneck — it stripped beneficialOwners, signatory, board, representatives, payrolls, statuses, fiscalYear before Opus ever saw the JSON. Existing filterRedundantQuestions ownership logic was effectively dead because the data path was severed. Expanded allowlist to include those v2 sections; kept bankAccounts/ email/phone/fiscalYearHistory/financialReports out (token cost > signal). - SYSTEM_PROMPT now documents each v2 section and the rules Opus should apply: payroll signal switches from "registration.payroll" to "actual payrolls[] filings" (kills the false-positive swedish-payroll selection for newly registered employers); beneficialOwners[] becomes the authoritative ownership source (single owner → FMB modifier; multiple → multi-owner); statuses[] isCeased/red triggers an uncertainty_note. Tests: 4112 unchanged. Build: green. No schema or migration changes. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * fix(agent): onboarding polish + composer signal fixes from first-run feedback UX: - AgentOnboarding: drop the 10s "Hoppa över — fortsätt med standardval" escape hatch. The fallback path runs automatically on timeout; the manual skip just teased users into a degraded build. - ReviewCard step 2 title: "Stämma av detaljerna" → "Stäm av detaljerna" (imperative form matches the rest of the steps). - Drop em-dashes from user-visible Swedish strings in AgentOnboarding + ReviewCard (fallback labels, subtitles, placeholder, error message, final CTA). Em-dashes survive in code comments only. - "Fråga min revisor" → "Fråga min assistent" everywhere it surfaced: AgentTrigger, AgentSparkleButton, ReviewCard preview, ReviewCard fallback comment, general.help intent buttonLabel + prompt text. - AgentTrigger / AgentSparkleButton / EmptyState.AgentHelpLink / TransactionInboxCard ask-button all gated on identity.isVerified. Pre-onboarding users no longer see the floating FAB or per-page Sparkle buttons. AgentSheetProvider.identity gained an isVerified field; (dashboard)/layout.tsx selects agent_profiles.verified_at and passes it through. TIC verksamhetsbeskrivning: - tic/index.ts /profile: /companies/{id}/purposes returns every historical verksamhetsföremål filing. Picking [0] was returning the oldest "äga och förvalta" holding-company boilerplate for companies whose later filings narrowed the purpose ("tillhandahålla företagskrediter och finansiella teknologilösningar"). Sort the array by lastUpdatedAtUtc desc and take the most recent non-empty purpose. Composer banking signal: - loadBankingSummary now reads journal_entry_id alongside description/amount/date and returns per-counterparty `direction` ('in' | 'out' | 'mixed') and `has_unbooked` (any row not yet booked). Aggregate `unbooked_count` accompanies the rollup. - buildUserPrompt emits each counterparty as `Name: 12 345 kr (ut, OBOKFÖRD)` so Opus can tell income from cost on sight and tell which counterparties are still open questions. - SYSTEM_PROMPT now explicitly forbids verification questions about counterparties whose direction is unambiguous AND status is 'bokförd'. Should kill the regressions from the first agent build: * "Konsult, J 98 565 kr — intäkt eller kostnad?" when the amount is clearly negative. * "ALMI AB 493 000 kr — lån eller bidrag?" when the transaction is already categorized. Tests: 4112 unchanged. Build: green. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * fix(agent,ui): representation needs deltagare+syfte, drop duplicate doc icon Representation booking: - transaction-categorization prompt now requires the agent to capture participants (name + company) AND purpose before staging a representation categorization. SKV's representationsregler + ML 8 kap require the verifikation to document who attended and what the meeting was about; without that the avdrag is denied and the post should be booked as non-deductible / personalkostnad. - The agent confirms back in plain text (audit trail in the chat), writes the deltagare + syfte to gnubok_remember_fact (long-term), THEN stages. Saknas deltagare/syfte: explicitly tell the user the avdrag won't go through and offer the non-deductible alternative. - Known gap (followup, not this commit): the staged op's journal entry description doesn't yet carry the deltagare text. Until we add a `notes` field to gnubok_categorize_transaction, the audit trail lives in chat + agent_memory only. TransactionInboxCard duplicate attachment indicator: - Drop the FileCheck2 "open document" button from the trailing slot. TransactionAttachmentIndicator (Paperclip) next to the description already opens the underlag on click. Two icons doing the same thing was noise. Cleaned up the unused state (isOpeningDoc, hasAttachment, handleOpenAttachment) and dropped now-unused imports (FileCheck2, useToast). Tests: 4112. Build: green. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * feat(agent,nav): notes on verifikation + redesigned sidebar Audit-trail notes for representation: - gnubok_categorize_transaction gains an optional `notes` string. Threaded through stagePendingOperation → commitCategorizeTransaction → createTransactionJournalEntry, which now appends notes to the entry's description (capped at 500 chars). The verifikation an external auditor reads now carries deltagare + syfte directly — not just chat history / agent_memory. - transaction-categorization prompt updated: representation flow now REQUIRES the agent to pass deltagare+syfte via the notes parameter. Without it the booking is non-deductible / personalkostnad per SKV. DashboardNav redesign: - Top section: flat, no header — Hem (/chat), Underlag (was Dokumentinkorg), Transaktioner, Granskning. Always visible; the inline badge on /pending shows the count when there are pending ops. - Mid section: four collapsible dropdowns (Försäljning, Inköp, Redovisning, Personal). Each auto-expands when the active route lives inside it. KPI moved from main to Redovisning. Extension nav items (TIC workspace, etc.) fold into Redovisning. - Bottom-left: new account popover (DropdownMenu, opens upward) holding CompanySwitcher, Inställningar, Hjälp, Support, Logga ut. Replaces the old top company-switcher card + the bottom Support/Logout block. - Mobile drawer mirrors the new structure: top items as flat list, same four dropdown groups, separate "Tillägg" section when extensions exist, "Mitt konto" section at the bottom. - i18n: invoice_inbox label renamed "Dokumentinkorg" → "Underlag" ("Documents" in en). New keys: mitt_konto, group_extensions. Tests: 4112. Build: green. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * fix(nav): unhide Leverantörer under Inköp The /suppliers entry existed in navItems but was marked hidden — leftover from when the supplier list lived elsewhere in the IA. Removing the hidden flag puts Leverantörer in the Inköp dropdown alongside Leverantörsfakturor. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * fix(nav): CompanySwitcher back to top-left, user account moves bottom-left The previous pass collapsed both concepts into the bottom popover. They mean different things: the company is the org context everything below operates against (top-of-sidebar, scannable); the user is the account-holder (bottom-of-sidebar, where settings/logout live). - (dashboard)/layout.tsx: fetch profiles.full_name alongside the existing identity queries; pass userName + userEmail into DashboardNav. - DashboardNav: restore CompanySwitcher at the top of the sidebar (pre-redesign placement). Bottom-left popover trigger now shows the signed-in user's name + single-letter initial (accountInitial helper falls back to email's first char, then "?"). Popover header carries full name + email; items unchanged (Inställningar, Hjälp, Support, Logga ut). CompanySwitcher removed from inside the popover — nested dropdowns were awkward and the top placement is where it belongs. Tests: 4112. Build: green. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * fix(pending): trim the agent context strip The row-level AgentContextStrip on /pending was rendering the model name (eu.anthropic.claude-sonnet-4-6) and the full atoms array (horizontal/swedish-vat, vertical/konsult-it, …) inline, which made each row 60–80 chars of mostly-the-same metadata. Reviewers never scan that text; they scan amounts and decide approve/reject. Now the strip shows only the conversation deep-link (Konversation #<short id>) — the one piece that's actually useful for diving into context. Model + atoms remain available in agent_metadata for debugging surfaces; they're just not in the list view. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * fix(agent): shared ground rules + paragraph breaks after tool calls Two regressions surfaced in real usage. Both are systemic. Shared agent ground rules: - /chat surface (general.help) was happily inventing four-digit BAS account numbers ("Debet 6212 - Molntjänster…", "Kredit 2614 - Ingående moms…") and proposing booking decisions on invoices it had never seen, with no follow-up questions about currency/scope/etc. - transaction-categorization had those rules baked into its prompt; general-help / bokslut-step / invoice-draft / supplier-invoice-review / verifikation-draft / vat-review never inherited them. - Extracted lib/agent/intents/shared-rules.ts with five cross-cutting rules: underlag first (check inbox + ask user to upload to Dokumentinkorgen when missing), ask follow-ups when ambiguous, never write four-digit BAS account numbers in chat (category names only), cite atoms / load skills (don't guess), check counterparty history before proposing. - Injected renderAgentGroundRules() into all six intents above. transaction-categorization left alone — it has more detailed inline rules tied to its specific underlag-flow. Paragraph break after tool calls: - text_delta from the model often resumes after a tool call without a leading newline ("kategoriseras." → gnubok_query_journal runs → "Inget historik hittades…" appended directly). Markdown rendered the concatenation as one paragraph. - AgentChat text_delta handler now inserts \n\n when (a) the buffer ends with text content, (b) the incoming delta starts with text content, (c) at least one tool call has run, and (d) the buffer doesn't already end with a blank line. Tests: 4112. Build: green. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * fix(nav): default-open dropdown groups; closing is per-user Dropdowns started collapsed which meant first-time users had to open each group to discover what's inside. Inverted the state: default open, user can collapse, active route still forces a group open. - manualExpanded → manualCollapsed (semantics flip) - toggleGroup unchanged externally; flips the bit - isGroupExpanded returns !manualCollapsed[g] || hasActiveChild Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * feat(agent): rate-safe v1→v2 TIC upgrade, counterparty defaults, profile settings Three pre-ship quality wins. Rate-limit-safe TIC v2 upgrade: - The /profile endpoint fans out to ~13 Lens calls; the account has a ~3000/mo ceiling. Force-refreshing every pre-v2 (v1) snapshot across the customer base would blow the budget. - ensureTicSnapshot gains an `upgradeV1` flag. A cached snapshot still inside the 7-day window is re-fetched only when (a) the caller passes upgradeV1 AND (b) the snapshot is v1-shaped (missing the v2-only `statuses` key). Gated to the two agent-onboarding call sites — a deliberate, once-per-company action and the only consumer of the v2 sections. Workspace + signup keep the natural 7-day staleness, so the v1→v2 migration is lazy and bounded to companies actually building an agent. Known-counterparty defaults (shared-rules): - Agent now proposes a sensible default for well-known counterparties instead of asking the same question monthly: Almi → lån, Tillväxtverket/ Vinnova/EU-stöd → bidrag, Skatteverket → skatt/avgift or återbäring, Bolagsverket → avgift, Försäkringskassan → ersättning, EF private withdrawal → eget uttag. Stated as an assumption the user can correct, not a hard rule — underlag/history still wins. Företagsprofil settings page: - New /settings/agent-profile (Företagsprofil / "Company profile"): view + edit the agent's company profile after onboarding — assistant name + avatar, the profile summary the agent reasons from, and a read-only chip view of loaded specialities (atoms). Backed by the existing GET/PATCH /api/agent/profile. - New GET /api/agent/atom-titles?ids= resolves atom slugs → human titles for the chips (registry is globally-readable reference data). - Added to SettingsSidebar; i18n keys agent_profile (sv "Företagsprofil" / en "Company profile"). Note: /chat already redirects unverified users to / (chat layout guard), and / renders WelcomeGate → /onboarding/agent. No redirect work needed. AgentSetupBanner.tsx is orphaned dead code (WelcomeGate superseded it). Tests: 4112. Build: green. Both new routes compile. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * feat(nav,agent): Hem=Översikt + separate Assistent button; memory dedup Nav restructure: - "Hem" now points to / (Översikt dashboard) again, not /chat. The agent chat gets its own top-level nav entry "Assistent" (Sparkles icon) → /chat. Mobile bottom nav mirrors this (Hem / Assistent / Transaktioner). - / restored to render DashboardContent (the Översikt) for built-agent users instead of redirecting to /chat. Users who haven't built their assistant yet still get WelcomeGate (the build-agent checklist); once verified, / shows the dashboard. Chat is reachable anytime via its nav entry. Restored main's dashboard data-fetch; added an agent_profiles verified_at probe to drive the WelcomeGate branch. - i18n: nav.assistant ("Assistent" / "Assistant"). agent_memory dedup (gnubok_remember_fact): - The agent re-remembers the same fact constantly (e.g. "Vercel = omvänd skattskyldighet" on every Vercel categorization), which would bloat agent_memory with paraphrases over months. - Before insert, compare the incoming fact against the 300 most-recent active memories by word-set Jaccard similarity (lowercased, punctuation- stripped, stopwords dropped). A near-duplicate (≥0.82) is treated as already-known: bump its relevance toward the new score + refresh updated_at instead of writing a new row. Embedding-free, zero added latency beyond one bounded SELECT. Tests: 4112. Build: green. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * fix(agent,nav): företagsprofil=Bolagsuppgifter, avatar nav icon, dedupe greeting Företagsprofil settings page (the right content this time): - Replaced the agent atoms/summary panel with CompanyProfileView — a read-only "Bolagsuppgifter" view of the cached TIC company snapshot (name, org-nr, form, address, F-skatt/Moms/Arbetsgivare, SNI, bank, verksamhet, employees, latest financials, status traffic-lights, fiscal year, firmateckning, företrädare). Server component reads the companies.tic_snapshot column directly — no extension import, stays inside the core-build boundary. - Route renamed /settings/agent-profile → /settings/company-profile. Removed the old AgentProfilePanel + the now-unused /api/agent/atom-titles endpoint. "Assistent" nav icon = the agent's chosen avatar: - DashboardNav reads agent identity from AgentSheetProvider and renders the onboarding-chosen avatar for the /chat ("Assistent") entry across desktop sidebar, mobile drawer, and mobile bottom nav. Falls back to the Sparkles glyph pre-onboarding (no avatar yet). Nav cleanup: - Dropped the beta badge from Underlag. - Filtered the TIC workspace (/e/general/tic, "Företagsprofil") out of the nav — the same Bolagsuppgifter now lives under Inställningar → Företagsprofil, so it shouldn't appear in two places. Doubled intake greeting fix: - /chat/intake fires an invoke with no conversation_id, then swaps the URL to /chat/[id] the instant the `conversation` event lands — which can beat the greeting being persisted. /chat/[id] then hydrated with 0 messages and, because the auto-fire guard keyed on (id && messages>0), fired a SECOND invoke on the same conversation → two greetings. Guard now keys on conversation-id presence alone: a set id means resume, never bootstrap. Closes the race. Tests: 4112. Build: green. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * fix(agent): paragraph-break-after-tool split words mid-stream The earlier "insert \n\n when text resumes after a tool call" heuristic re-evaluated on EVERY text_delta (any delta not starting/ending with whitespace, once a tool had run). Streaming deltas arrive in sub-word chunks, so it injected breaks between fragments of the same word: "minnes\n\nno\n\nterna", "kund\n\nrep\n\nresentation". Replace the per-delta heuristic with a consume-once ref: - tool_use sets breakBeforeNextTextRef = true - the next text_delta consumes it: prepends \n\n exactly once (only when the buffer has content, doesn't already end in whitespace, and the delta doesn't start with whitespace), then clears the flag So the break fires once per tool→text resume, never mid-word. Build: green. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * fix(agent): much shorter replies, representation headcount + VAT cap, dot separator Brevity (system-prompt Svarsformat — affects every reply): - Hard "korthet är regel nummer ett": aim for 2-4 sentences, lead with the answer/action, no warm-up ("Här är vad som gäller…"), don't derive VAT in prose, don't restate what the approval card shows, one question at a time. The agent was writing textbook-length essays. Representation rule now in shared-rules (so verifikation-draft, vat-review, etc. all get it — previously only transaction-categorization had it, which is why the verifikation flow guessed 25% VAT and skipped the cap): - Require ANTAL deltagare (headcount), not just one name — the moms deduction is per person (underlag cap 300 kr/person ex moms). - Use the receipt's ACTUAL VAT rate (usually 12% on food), never assume 25%. - Meal representation isn't income-tax deductible (post-2017); whole cost booked as non-deductible representation. Verifikation description separator: - createTransactionJournalEntry appended notes with an em-dash ("Utlägg Eatnam — Deltagare:…"), violating house style. Switched to a middle dot " · ". journal_entries has no separate notes column — the description IS the BFL verifikationstext / audit field, so deltagare + syfte correctly live there. Tests: 4112. Build: green. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * fix(settings): tidy Bolagsuppgifter — no status colours, clean firmateckning From first-look feedback on the Företagsprofil page: - Status: dropped the coloured traffic-light badges (red/yellow/green). Per the design system semantic colour is data-only, never chrome, so status now renders as plain label + date. Also filtered to dated entries only — Bolagsverket emits flags like "Har aldrig varit verksam" with no date that read as noise next to the real status. Ceased status gets muted destructive text (the one chrome colour the system keeps). - Firmateckning: the source text carries ">" list markers and crams several rules onto one line, and repeats "Firman tecknas av styrelsen" across rows. cleanSignatory() strips the markers, normalises whitespace, splits run-on "Firman tecknas …" clauses onto separate lines, and the render dedupes — so each rule reads as its own sentence. Build: green. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * fix(mcp): inbox items expose all terminal links + processed flag The Eatnam receipt was booked against its bank transaction (so the inbox row had matched_transaction_id + created_journal_entry_id set), yet the agent reported it as loose/unmatched and a duplicate risk. Root cause: gnubok_list_inbox_items only selected and returned matched_supplier_id + created_supplier_invoice_id — the supplier-invoice path. The transaction-match and direct-journal-entry paths were invisible, so any receipt cleared via /transactions looked unprocessed. - list_inbox_items now selects + returns matched_transaction_id and created_journal_entry_id alongside the supplier fields, plus a derived `processed` boolean (true when ANY of the three terminal links is set). - New unprocessed_only=true input filters to items with no terminal link — the "what still needs handling" view that prevents the agent from flagging already-booked docs as duplicates. (Fetches a wider window then filters client-side so limit applies post-filter.) - Description updated to document the processed semantics, within the 280-char tool-description budget. The DB linkage itself already worked: /transactions attach-document sets matched_transaction_id, and commitCategorizeTransaction stamps created_journal_entry_id. This was purely a read/surface gap. Tests: 4112 (+ MCP description guard). Build: green. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * fix(mcp): repair stage-but-never-commit tools + consolidate tool surface - post_annual_depreciation AND reverse_entry were never in the pending_operations operation_type CHECK, so both staged then died with check_violation at INSERT. Add the CHECK migration, a commitPostAnnualDepreciation executor (reusing commitAnnualPostings), risk tier, and the PendingOperationType union member. - Salary tools de-risked: calculate_salary_run calls runSalaryCalculation() directly (no self-fetch/forged cookie); create_salary_run uses a transactional create-run helper with compensating delete; generate_agi actually generates + persists the declaration. - import_sie parses + validates at stage time with a content-rich preview (company, fiscal year, voucher/account counts, balance) instead of a blind byte count. - batch-match-invoices passed user.id where companyId was expected (silently matched zero). - VAT report+widget merged behind render_ui; gnubok_search_tools ranks by relevance; gnubok_feedback readOnlyHint corrected; tools/list instruction text fixed; income decision-tree + GL/query_journal cross-refs added. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * feat(agent): load skill atom bodies from the DB so they survive the build Skill bodies were read from disk at runtime (.claude/skills/**/SKILL.md); on Vercel the dynamic readFile path isn't traced into the lambda and on Docker .claude/ is excluded, so atoms loaded EMPTY in production — a despecialized agent. Inline the bodies into agent_atom_registry instead: - Migration adds body + mcp_exposed columns; a build-time generator (scripts/generate-skill-bodies.ts) emits a deterministic dollar-quoted seed migration with a content-hash manifest + --check CI guard. - Read sites (mcp-server atoms.ts, chat system-prompt.ts, composer prewarm) read body from the DB, with a dev-only disk fallback. mcp_exposed curates which atoms the MCP exposes (swarm-* never become atoms). - The seed script + generator share scripts/lib/atom-discovery.ts; estimated_tokens now reflects SKILL.md only. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * feat(agent): safe the in-app assistant — gating, FAB de-confliction, rate limit, friendly errors - Hide all agent entry points until verified_at: the Assistent nav tab (sidebar + mobile) and the agent-memory settings tab now match the floating FAB's gate. - FAB de-confliction: /kpi -> kpi.explain and /bookkeeping/year-end -> bokslut.step so the floating button opens the SAME assistant as the page button (no two-agents-on-one-page). - Generous per-user rate limit (30/min, 1000/day) on /api/agent/invoke, /onboarding/stream, /composer via a new agent_rate_counters table + check_and_increment_agent_quota RPC; fails open. Bounds runaway Bedrock spend without touching normal users. - Friendly errors: Bedrock 429/timeout/5xx normalized to Swedish (friendlyModelError) in run-turn + the invoke route; the chat client surfaces the server's friendly message instead of a raw HTTP status. - /chat/new validates ?intent= against the registry so bad deep-links fall back to general.help instead of rendering a broken-looking error. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * fix(agent): keep /chat read-only — redirect categorization + swap the "categorize" suggestion for a VAT-report question general.help (the /chat assistant) is read-only, but it still gave per-transaction bokföringsförslag in prose and asked "godkänner du dessa?" — an analysis the user can't act on (no write tool, no per-tx underlag). Strengthen the prompt to redirect categorization/bokföring to the per-transaction flow (open the transaction -> "Fråga om denna transaktion", where the agent sees the underlag and stages a real ApprovalCard); a short overview is still allowed. Add a guard test locking in no-write-tools + the redirect language. Swap the /chat empty-state "Hjälp mig kategorisera" chip (which lured users into exactly this dead-end) for a VAT-report question the read-only assistant can actually answer. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * refactor(pending): declutter the review queue rows + header Fold the conversation deep-link onto the actor label (drop the separate "Konversation #xxxx" strip and its icon), hide the quick-pick when there's only one operation type (it duplicated "Markera alla"), and drop the "(0)" from the disabled bulk-approve button. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * feat(vat): enhance VAT handling by integrating document validation and improving error messaging * feat(settings): add assistant knowledge surface + consolidate settings tabs Expose the agent's skill atoms (agent_atom_registry) in a read-only surface beside the existing memory view, and tighten the settings tab bar from 14 to 10 tabs. - New GET /api/agent/skills + AgentSkillsPanel: lists active, mcp_exposed atoms grouped by tier (Kärnkompetens / bransch / bolagssituation), flags which are active for the company from agent_profiles, and lazy-loads each SKILL.md body on expand. - New /settings/assistant tab with a Minne/Kompetens toggle (?view=skills); /settings/agent-memory and /settings/agent-skills redirect into it. - Merge Företagsprofil (TIC snapshot) into the Företag tab via CompanyProfileSection; /settings/company-profile redirects. - Merge Skatteverket-anslutningen into the Skatt tab — OAuth returnTo and the callback toast now target /settings/tax; /settings/skatteverket redirects. - Drop the Säkerhetsbackup tab (already under Importera/Exportera). Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * fix(inbox): keep booked underlag out of the unmatched queue + widen match window - categorize: after booking an inbox underlag onto a verifikat, backfill the inbox row's matched_transaction_id + created_journal_entry_id so it stops showing as unmatched (mirrors the /attach-document paperclip path). - TransactionMatchPicker: bias the candidate window forward (60d before → 180d after the invoice date) so late payments aren't dropped before scoring, and widen the ranking date tolerance to 120d so the true match floats to the top instead of collapsing to "Svag match". Fix "okatigoriserade" typo. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * wip: bundle in-progress branch work + agent onboarding chat optimizations Captures the uncommitted work-in-progress on this branch so it lives on the remote. Heterogeneous changeset — bundled as one commit since the work was already entangled across files. Headline change in this commit (from this session): - Remove the double interview in agent onboarding. Phase B's verification- question form stepper is gone — the Phase C chat (onboarding.intake) now owns the entire interview and reads the composer's verification_questions server-side as its question bank. - ReviewCard collapses from 3 steps to 2 (meet → review-and-confirm) with value-first ordering: profile + "vad jag kan hjälpa dig med" + facts + optional seed note. CTA reads "Möt {namn}" to signal the chat follows. - ChatIntakeStarter handoff subcopy updated to match reality (assistant greets first; user can leave anytime). - Stamp agent_profiles.intake_completed_at server-side in app/api/agent/invoke/route.ts on the first user-typed reply in any onboarding.intake conversation (idempotent IS NULL guard, best-effort). Closes the previously dead-write column and unlocks the opportunistic- follow-up hook the migration anticipated. Plus in-progress branch work being carried forward (not introduced here): agent runtime + intent prompts, composer + atom-discovery scripts, MCP server skills surface, onboarding flow components, dashboard/inbox tweaks, two new agent_atom_registry migrations, additional agent-chat tests. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * refactor(agent): drop inline "Fråga assistenten" affordances — rely on the FAB The bottom-right "Fråga {namn}" FAB (AgentTrigger) is already route-aware and picks the right intent per page, so duplicating it as inline page- header buttons and empty-state links is noise. Removed: - EmptyState `agentHelp` link ("Eller fråga {namn} hur du kommer igång") + the AgentHelpLink component + agent_default_name/agent_ask_link i18n keys + the agentHelp props on EmptyInvoices/EmptyCustomers/EmptyTransactions. - AgentSparkleButton on /bookkeeping (verifikation.draft) and /kpi (kpi.explain) page headers. The FAB stays — when verified, it appears on those routes and routes to the right intent automatically. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * fix(agent): gate the last two ungated "Fråga assistenten" affordances Both surfaces previously called useAgentSheet directly without checking identity.isVerified, so they appeared pre-onboarding (everywhere else the FAB / sparkle buttons / /chat / Assistent nav are all gated on verified_at). - Settings page header: remove the "Fråga {namn}" pill entirely. The FAB covers /settings routes route-aware (settings.help) — no need for a duplicate inline trigger. - Invoice inbox transaction picker: hide the "Fråga assistenten" button when the agent isn't built. Done at the parent (InvoiceInboxWorkspace) by passing onAskAssistant only when identity.isVerified is true; the child renders the button only when the callback is present. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * feat(tic,onboarding,agent): single-call TIC lookup + director-aware narrative voice - TIC: collapse the company lookup from 6 endpoint calls to 1 (search-public already exposes sniCodes, bank accounts, emails, phones, and registration flags). Derive fiscal-year MM-DD from mostRecentFinancialSummary; newly-registered companies fall through to the client's first-year defaults. - Onboarding: BankID picker no longer auto-provisions companies. Every pick routes through the wizard with orgnr (and entity_type via the CompanyRoles match) prefilled; F-skatt/VAT/address get confirmed in steps 2-4 instead of being auto-fetched. createCompanyFromOnboarding reuses CompanyLookupResult and adds a defensive top-level catch so server-action errors surface to the UI instead of being redacted. - Agent composer: loadUserDirectorship() checks BankID CompanyRoles for a director-like position (ceo/boardMember/chairman/externalSignatory, active) before the narrative uses second-person ownership voice ("Du driver…"); unknown users get neutral third-person voice so we never put ownership words in the user's mouth. Tests cover loadUserDirectorship, narrative voice, tic-fetch path, onboarding page, and updated TIC client + lookup/profile suites. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * fix(tic): extend agent-onboarding TIC budget to 10s + backfill stranded org_numbers The 5s TIC fetch timeout aborted client-side before the upstream Lens fan-out (~13 calls) could complete, but the in-flight upstream calls still counted against quota — actions.ts already documents ~530 wasted calls from this in May. Same bug still applied to the agent-onboarding stream path. Adds an optional `timeoutMs` to `ensureTicSnapshot` so deliberate wait-screen callers (agent onboarding stream) can run with 10s while background/dev callers stay on the conservative 5s default. Page-level server fetch (page.tsx) intentionally stays at 5s to avoid blocking TTFB without a visible progress affordance. Backfill migration mirrors `company_settings.org_number` to `companies.org_number` for the 105 cases where it's safe (after dedup + conflict filtering). 56 of those are on active companies — unblocks duplicate guards, SIE/SRU exports, and TIC fallback chain. Zero TIC API calls — pure data move. Idempotent. Also sweeps a pre-existing SSRF guard on the stream route's origin derivation that was sitting unstaged in the working tree — it lives in the same diff hunks as the TIC budget change and couldn't be split cleanly. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * wip: bundle in-progress branch work Sweep up uncommitted agent/MCP/RLS work-in-progress so the branch is fully backed up to origin. Not reviewed in detail — committed as-is to preserve working state alongside the TIC fixes in the previous commit. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * fix(agent): tag the "Bygg din bokföringsassistent" CTA as Beta Adds a Beta badge next to the assistant-setup heading on the dashboard banner, dashboard inline card, and onboarding checklist row. Also drops the stale "Gratis i 30 dagar" subline from the dashboard card. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * fix(build,migrations): PendingOperationType salary ops + resolve migration version collisions PR #584 went red on three things: 1. core-only build / Vercel: `lib/pending-operations/commit.ts:2666` switched on 'create_salary_run' and 'generate_agi' but `PendingOperationType` was missing both literals. Add them to the union. 2. Supabase preview: migration version 20260526120000 collided with main's newly-merged 20260526120000_fix_replace_sie_import_hard_delete.sql. Bump the branch's pair to 20260526120050 / 20260526120051 — still ahead of 20260526120100_restvardeavskrivning so ordering is preserved. 3. 20260527170000 was used twice on this branch (_agent_rls_with_check + _journal_entry_no_doc_required). Bump the second to 20260527170100 so the pair stays orderable and Supabase doesn't choke on the duplicate schema_migrations PK. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * fix(ci): reword comment so core-only guard stops flagging it The "Check no core imports from extensions" step greps for the literal \`from '@/extensions/\` across lib/, app/api/, components/. A comment in lib/agent/composer/tic-fetch.ts quoted the exact pattern verbatim to explain *why* the file does a self-fetch instead of importing the TIC extension directly — which the grep matched even though no actual import exists. Rewrite the line to keep the same meaning without the literal pattern. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> --------- Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]> Co-authored-by: Emil <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 4.7
Emil
parent
a9b43ebeb7
commit
f53725b20a
@@ -0,0 +1,24 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { swedishToday } from '../utils'
|
||||
|
||||
describe('swedishToday', () => {
|
||||
it('formats the date as ISO yyyy-MM-dd with a Swedish weekday', () => {
|
||||
// 2026-01-01 is a Thursday → "torsdag". Noon UTC keeps us clear of any
|
||||
// midnight boundary so the assertion is timezone-stable.
|
||||
expect(swedishToday(new Date('2026-01-01T12:00:00Z'))).toBe('2026-01-01 (torsdag)')
|
||||
})
|
||||
|
||||
it('reports the date in Europe/Stockholm, not UTC', () => {
|
||||
// 23:30 UTC on 2026-05-26 is already 01:30 on 2026-05-27 in Stockholm
|
||||
// (CEST, UTC+2). A naive UTC date would read the day before — the off-by-one
|
||||
// we explicitly format around for users near midnight.
|
||||
expect(swedishToday(new Date('2026-05-26T23:30:00Z'))).toBe('2026-05-27 (onsdag)')
|
||||
})
|
||||
|
||||
it('omits clock time so the cached prompt prefix stays stable across a day', () => {
|
||||
const morning = swedishToday(new Date('2026-05-27T06:00:00Z'))
|
||||
const evening = swedishToday(new Date('2026-05-27T18:00:00Z'))
|
||||
expect(morning).toBe(evening)
|
||||
expect(morning).not.toMatch(/\d{2}:\d{2}/)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,30 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { friendlyModelError } from '../run-turn'
|
||||
|
||||
describe('friendlyModelError', () => {
|
||||
it('429 status → "Anna är upptagen"', () => {
|
||||
expect(friendlyModelError({ status: 429, message: 'Too Many Requests' })).toMatch(/upptagen/i)
|
||||
})
|
||||
|
||||
it('throttling message → busy', () => {
|
||||
expect(friendlyModelError(new Error('ThrottlingException: Rate exceeded'))).toMatch(/upptagen/i)
|
||||
})
|
||||
|
||||
it('timeout / dropped connection → "Anslutningen ... bröts"', () => {
|
||||
expect(friendlyModelError(new Error('socket hang up ETIMEDOUT'))).toMatch(/anslutningen/i)
|
||||
})
|
||||
|
||||
it('5xx → temporary service error', () => {
|
||||
expect(friendlyModelError({ status: 503, message: 'Service Unavailable' })).toMatch(/tillfälligt fel/i)
|
||||
})
|
||||
|
||||
it('unknown error → generic Swedish line', () => {
|
||||
expect(friendlyModelError(new Error('weird'))).toMatch(/något gick fel/i)
|
||||
})
|
||||
|
||||
it('never leaks the raw English SDK message to the user', () => {
|
||||
const out = friendlyModelError(new Error('ValidationException: model id invalid'))
|
||||
expect(out).not.toContain('ValidationException')
|
||||
expect(out).not.toContain('model id')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,322 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import type { AgentIntent } from '@/lib/agent/intents/types'
|
||||
import type { AgentTool } from '@/lib/agent/tools/types'
|
||||
import type { StreamEvent } from '../run-turn'
|
||||
|
||||
// Anthropic client mock — returns a single round trip: one tool_use turn,
|
||||
// then a final text-only turn (so the loop terminates). run-turn now uses
|
||||
// `messages.stream()` for token-level streaming, so we expose a stream
|
||||
// adapter that delegates `finalMessage()` to the same queued mock.
|
||||
const messagesCreate = vi.fn()
|
||||
vi.mock('@/lib/agent/composer/client', () => ({
|
||||
getAnthropic: () => ({
|
||||
messages: {
|
||||
create: messagesCreate,
|
||||
stream: (args: unknown) => {
|
||||
const stream = {
|
||||
on: () => stream,
|
||||
finalMessage: () => messagesCreate(args),
|
||||
}
|
||||
return stream
|
||||
},
|
||||
},
|
||||
}),
|
||||
SONNET_MODEL: 'claude-sonnet-4-6',
|
||||
}))
|
||||
|
||||
// system-prompt builder — return a minimal valid shape.
|
||||
vi.mock('../system-prompt', () => ({
|
||||
buildSystemPrompt: vi.fn().mockResolvedValue({
|
||||
blocks: [],
|
||||
promptHash: 'sha256:test',
|
||||
atomsLoaded: [],
|
||||
}),
|
||||
}))
|
||||
|
||||
// Tool registry — return a controllable tool list. Tests overwrite per-case.
|
||||
const getMock = vi.fn()
|
||||
const getManyMock = vi.fn()
|
||||
vi.mock('@/lib/agent/tools/registry', () => ({
|
||||
agentToolRegistry: {
|
||||
get: (...args: unknown[]) => getMock(...args),
|
||||
getMany: (...args: unknown[]) => getManyMock(...args),
|
||||
},
|
||||
}))
|
||||
|
||||
import { runChatTurn } from '../run-turn'
|
||||
|
||||
function fakeSupabase() {
|
||||
// Every chain method is a no-op that resolves to empty.
|
||||
const passthrough: Record<string, unknown> = {}
|
||||
const proxy: unknown = new Proxy(passthrough, {
|
||||
get(_t, prop) {
|
||||
if (prop === 'then') {
|
||||
return (resolve: (v: unknown) => void) => resolve({ data: null, error: null })
|
||||
}
|
||||
return () => proxy
|
||||
},
|
||||
})
|
||||
return proxy as unknown as Parameters<typeof runChatTurn>[0]['supabase']
|
||||
}
|
||||
|
||||
function makeIntent(): AgentIntent {
|
||||
return {
|
||||
id: 'general.help',
|
||||
buttonLabel: 'x',
|
||||
sheetTitle: 'x',
|
||||
atoms: { mode: 'progressive', horizontal: [], includeCompanyVertical: false, includeCompanyModifiers: false },
|
||||
tools: ['gnubok_remember_fact'],
|
||||
model: 'claude-sonnet-4-6',
|
||||
capture: async () => ({}),
|
||||
promptTemplate: () => '',
|
||||
}
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
describe('runChatTurn — memory_captured emission', () => {
|
||||
it('emits memory_captured after a successful remember_fact tool call', async () => {
|
||||
// First response: model issues a remember_fact tool_use.
|
||||
// Second response: model finishes with text (no more tools → loop ends).
|
||||
messagesCreate
|
||||
.mockResolvedValueOnce({
|
||||
content: [
|
||||
{
|
||||
type: 'tool_use',
|
||||
id: 'tu_1',
|
||||
name: 'gnubok_remember_fact',
|
||||
input: { content: 'Hyresfaktura kommer 25:e varje månad', kind: 'pattern' },
|
||||
},
|
||||
],
|
||||
stop_reason: 'tool_use',
|
||||
})
|
||||
.mockResolvedValueOnce({
|
||||
content: [{ type: 'text', text: 'Klart.' }],
|
||||
stop_reason: 'end_turn',
|
||||
})
|
||||
|
||||
const rememberTool: AgentTool = {
|
||||
name: 'gnubok_remember_fact',
|
||||
description: '',
|
||||
inputSchema: { type: 'object', additionalProperties: false, properties: {} },
|
||||
execute: vi.fn().mockResolvedValue({
|
||||
id: 'mem-abc',
|
||||
kind: 'pattern',
|
||||
content: 'Hyresfaktura kommer 25:e varje månad',
|
||||
created_at: '2026-05-18T10:00:00Z',
|
||||
}),
|
||||
}
|
||||
getMock.mockReturnValue(rememberTool)
|
||||
getManyMock.mockResolvedValue([rememberTool])
|
||||
|
||||
const events: StreamEvent[] = []
|
||||
await runChatTurn({
|
||||
supabase: fakeSupabase(),
|
||||
userId: 'user-1',
|
||||
companyId: 'company-1',
|
||||
companyName: 'Acme AB',
|
||||
firstName: 'Anna',
|
||||
intent: makeIntent(),
|
||||
conversationId: 'conv-1',
|
||||
userMessage: 'kom ihåg det här',
|
||||
persist: false,
|
||||
emit: (e) => {
|
||||
events.push(e)
|
||||
return true
|
||||
},
|
||||
})
|
||||
|
||||
const memEvent = events.find((e) => e.kind === 'memory_captured')
|
||||
expect(memEvent).toBeDefined()
|
||||
expect(memEvent).toMatchObject({
|
||||
kind: 'memory_captured',
|
||||
tool_use_id: 'tu_1',
|
||||
action: 'remembered',
|
||||
memory_id: 'mem-abc',
|
||||
memory_kind: 'pattern',
|
||||
content: 'Hyresfaktura kommer 25:e varje månad',
|
||||
})
|
||||
})
|
||||
|
||||
it('emits memory_captured with action=forgotten for forget_fact', async () => {
|
||||
messagesCreate
|
||||
.mockResolvedValueOnce({
|
||||
content: [
|
||||
{
|
||||
type: 'tool_use',
|
||||
id: 'tu_2',
|
||||
name: 'gnubok_forget_fact',
|
||||
input: { id: 'mem-old', is_active: false },
|
||||
},
|
||||
],
|
||||
stop_reason: 'tool_use',
|
||||
})
|
||||
.mockResolvedValueOnce({
|
||||
content: [{ type: 'text', text: 'Glömt.' }],
|
||||
stop_reason: 'end_turn',
|
||||
})
|
||||
|
||||
const forgetTool: AgentTool = {
|
||||
name: 'gnubok_forget_fact',
|
||||
description: '',
|
||||
inputSchema: { type: 'object', additionalProperties: false, properties: {} },
|
||||
execute: vi.fn().mockResolvedValue({ id: 'mem-old', is_active: false }),
|
||||
}
|
||||
getMock.mockReturnValue(forgetTool)
|
||||
getManyMock.mockResolvedValue([forgetTool])
|
||||
|
||||
const events: StreamEvent[] = []
|
||||
await runChatTurn({
|
||||
supabase: fakeSupabase(),
|
||||
userId: 'user-1',
|
||||
companyId: 'company-1',
|
||||
companyName: 'Acme AB',
|
||||
firstName: 'Anna',
|
||||
intent: { ...makeIntent(), tools: ['gnubok_forget_fact'] },
|
||||
conversationId: 'conv-1',
|
||||
userMessage: 'glöm det där',
|
||||
persist: false,
|
||||
emit: (e) => {
|
||||
events.push(e)
|
||||
return true
|
||||
},
|
||||
})
|
||||
|
||||
const memEvent = events.find((e) => e.kind === 'memory_captured')
|
||||
expect(memEvent).toMatchObject({
|
||||
kind: 'memory_captured',
|
||||
action: 'forgotten',
|
||||
memory_id: 'mem-old',
|
||||
})
|
||||
})
|
||||
|
||||
it('bumps last_accessed_at for the memories included in the turn', async () => {
|
||||
// Single-shot text response — no tool use, simplest path.
|
||||
messagesCreate.mockResolvedValueOnce({
|
||||
content: [{ type: 'text', text: 'OK.' }],
|
||||
stop_reason: 'end_turn',
|
||||
})
|
||||
getManyMock.mockResolvedValue([])
|
||||
|
||||
// Intercept supabase to capture the .in() call on agent_memory.
|
||||
const inSpy = vi.fn().mockResolvedValue({ data: null, error: null })
|
||||
const updateChain = { eq: vi.fn().mockResolvedValue({ data: null, error: null }), in: inSpy }
|
||||
const memoryRows = [
|
||||
{ id: 'mem-A', content: 'X', kind: 'fact', relevance_score: 1, last_accessed_at: null, is_pinned: false },
|
||||
{ id: 'mem-B', content: 'Y', kind: 'preference', relevance_score: 0.8, last_accessed_at: null, is_pinned: false },
|
||||
]
|
||||
const memoryQueryChain = {
|
||||
select: () => memoryQueryChain,
|
||||
eq: () => memoryQueryChain,
|
||||
order: () => memoryQueryChain,
|
||||
limit: () => Promise.resolve({ data: memoryRows, error: null }),
|
||||
}
|
||||
const messagesQueryChain = {
|
||||
select: () => messagesQueryChain,
|
||||
eq: () => messagesQueryChain,
|
||||
order: () => Promise.resolve({ data: [], error: null }),
|
||||
}
|
||||
const profileChain = {
|
||||
select: () => profileChain,
|
||||
eq: () => profileChain,
|
||||
maybeSingle: () => Promise.resolve({ data: null }),
|
||||
}
|
||||
|
||||
let bumpCalled: string[] | null = null
|
||||
const supabase = {
|
||||
auth: { getUser: vi.fn() },
|
||||
from: vi.fn((table: string) => {
|
||||
if (table === 'agent_profiles') return profileChain
|
||||
if (table === 'agent_memory') {
|
||||
return {
|
||||
...memoryQueryChain,
|
||||
update: () => ({
|
||||
in: (_col: string, ids: string[]) => {
|
||||
bumpCalled = ids
|
||||
return Promise.resolve({ data: null, error: null })
|
||||
},
|
||||
}),
|
||||
}
|
||||
}
|
||||
if (table === 'agent_messages') {
|
||||
return {
|
||||
...messagesQueryChain,
|
||||
insert: () => Promise.resolve({ data: null, error: null }),
|
||||
}
|
||||
}
|
||||
if (table === 'agent_conversations') {
|
||||
return {
|
||||
update: () => updateChain,
|
||||
insert: () => Promise.resolve({ data: null, error: null }),
|
||||
}
|
||||
}
|
||||
return memoryQueryChain
|
||||
}),
|
||||
}
|
||||
|
||||
await runChatTurn({
|
||||
supabase: supabase as unknown as Parameters<typeof runChatTurn>[0]['supabase'],
|
||||
userId: 'user-1',
|
||||
companyId: 'company-1',
|
||||
companyName: 'Acme AB',
|
||||
firstName: 'Anna',
|
||||
intent: makeIntent(),
|
||||
conversationId: 'conv-1',
|
||||
userMessage: 'hej',
|
||||
persist: true,
|
||||
emit: () => true,
|
||||
})
|
||||
|
||||
expect(bumpCalled).not.toBeNull()
|
||||
expect(bumpCalled).toEqual(expect.arrayContaining(['mem-A', 'mem-B']))
|
||||
})
|
||||
|
||||
it('does NOT emit memory_captured for unrelated tools', async () => {
|
||||
messagesCreate
|
||||
.mockResolvedValueOnce({
|
||||
content: [
|
||||
{
|
||||
type: 'tool_use',
|
||||
id: 'tu_3',
|
||||
name: 'gnubok_list_uncategorized_transactions',
|
||||
input: {},
|
||||
},
|
||||
],
|
||||
stop_reason: 'tool_use',
|
||||
})
|
||||
.mockResolvedValueOnce({
|
||||
content: [{ type: 'text', text: 'Done.' }],
|
||||
stop_reason: 'end_turn',
|
||||
})
|
||||
|
||||
const listTool: AgentTool = {
|
||||
name: 'gnubok_list_uncategorized_transactions',
|
||||
description: '',
|
||||
inputSchema: { type: 'object', additionalProperties: false, properties: {} },
|
||||
execute: vi.fn().mockResolvedValue({ data: [] }),
|
||||
}
|
||||
getMock.mockReturnValue(listTool)
|
||||
getManyMock.mockResolvedValue([listTool])
|
||||
|
||||
const events: StreamEvent[] = []
|
||||
await runChatTurn({
|
||||
supabase: fakeSupabase(),
|
||||
userId: 'user-1',
|
||||
companyId: 'company-1',
|
||||
companyName: 'Acme AB',
|
||||
firstName: 'Anna',
|
||||
intent: { ...makeIntent(), tools: ['gnubok_list_uncategorized_transactions'] },
|
||||
conversationId: 'conv-1',
|
||||
userMessage: 'hi',
|
||||
persist: false,
|
||||
emit: (e) => {
|
||||
events.push(e)
|
||||
return true
|
||||
},
|
||||
})
|
||||
|
||||
expect(events.find((e) => e.kind === 'memory_captured')).toBeUndefined()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,127 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import type { AgentIntent } from '@/lib/agent/intents/types'
|
||||
|
||||
// Verifies the extended-thinking ("tänka längre") wiring: an opted-in intent
|
||||
// gets a thinking config + bumped max_tokens on the model call, an intent
|
||||
// without it gets neither; and thinking blocks are stripped before persistence.
|
||||
//
|
||||
// The Anthropic client mock mirrors run-turn-memory.test.ts: stream().on() is a
|
||||
// chainable no-op and finalMessage() delegates to a queued mock that records
|
||||
// the args the stream was called with.
|
||||
const messagesCreate = vi.fn()
|
||||
vi.mock('@/lib/agent/composer/client', () => ({
|
||||
getAnthropic: () => ({
|
||||
messages: {
|
||||
stream: (args: unknown) => {
|
||||
const stream = { on: () => stream, finalMessage: () => messagesCreate(args) }
|
||||
return stream
|
||||
},
|
||||
},
|
||||
}),
|
||||
SONNET_MODEL: 'claude-sonnet-4-6',
|
||||
}))
|
||||
|
||||
vi.mock('../system-prompt', () => ({
|
||||
buildSystemPrompt: vi.fn().mockResolvedValue({
|
||||
blocks: [],
|
||||
promptHash: 'sha256:test',
|
||||
atomsLoaded: [],
|
||||
}),
|
||||
}))
|
||||
|
||||
const getManyMock = vi.fn()
|
||||
vi.mock('@/lib/agent/tools/registry', () => ({
|
||||
agentToolRegistry: {
|
||||
get: () => undefined,
|
||||
getMany: (...args: unknown[]) => getManyMock(...args),
|
||||
},
|
||||
}))
|
||||
|
||||
import { runChatTurn, stripThinking } from '../run-turn'
|
||||
|
||||
function fakeSupabase() {
|
||||
const passthrough: Record<string, unknown> = {}
|
||||
const proxy: unknown = new Proxy(passthrough, {
|
||||
get(_t, prop) {
|
||||
if (prop === 'then') {
|
||||
return (resolve: (v: unknown) => void) => resolve({ data: null, error: null })
|
||||
}
|
||||
return () => proxy
|
||||
},
|
||||
})
|
||||
return proxy as unknown as Parameters<typeof runChatTurn>[0]['supabase']
|
||||
}
|
||||
|
||||
function baseIntent(): AgentIntent {
|
||||
return {
|
||||
id: 'general.help',
|
||||
buttonLabel: 'x',
|
||||
sheetTitle: 'x',
|
||||
atoms: { mode: 'progressive', horizontal: [], includeCompanyVertical: false, includeCompanyModifiers: false },
|
||||
tools: [],
|
||||
model: 'claude-sonnet-4-6',
|
||||
capture: async () => ({}),
|
||||
promptTemplate: () => '',
|
||||
}
|
||||
}
|
||||
|
||||
async function runWith(intent: AgentIntent) {
|
||||
messagesCreate.mockResolvedValueOnce({
|
||||
content: [{ type: 'text', text: 'ok' }],
|
||||
stop_reason: 'end_turn',
|
||||
})
|
||||
getManyMock.mockResolvedValue([])
|
||||
await runChatTurn({
|
||||
supabase: fakeSupabase(),
|
||||
userId: 'u',
|
||||
companyId: 'c',
|
||||
companyName: 'X',
|
||||
firstName: 'A',
|
||||
intent,
|
||||
conversationId: 'conv',
|
||||
userMessage: 'hej',
|
||||
persist: false,
|
||||
emit: () => true,
|
||||
})
|
||||
// The args object the stream was invoked with.
|
||||
return messagesCreate.mock.calls[0][0] as { thinking?: unknown; max_tokens?: number }
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
describe('runChatTurn — extended thinking wiring', () => {
|
||||
it('passes a thinking config and bumps max_tokens when the intent opts in', async () => {
|
||||
const args = await runWith({ ...baseIntent(), thinking: { budgetTokens: 2000 } })
|
||||
expect(args.thinking).toEqual({ type: 'enabled', budget_tokens: 2000 })
|
||||
// budget must be strictly below max_tokens — we add the normal output budget.
|
||||
expect(args.max_tokens).toBe(2000 + 4096)
|
||||
})
|
||||
|
||||
it('omits thinking and keeps the default budget when the intent does not opt in', async () => {
|
||||
const args = await runWith(baseIntent())
|
||||
expect(args.thinking).toBeUndefined()
|
||||
expect(args.max_tokens).toBe(4096)
|
||||
})
|
||||
})
|
||||
|
||||
describe('stripThinking', () => {
|
||||
it('drops thinking and redacted_thinking blocks but keeps text and tool_use', () => {
|
||||
const blocks = [
|
||||
{ type: 'thinking', thinking: 'raw chain of thought', signature: 'sig' },
|
||||
{ type: 'redacted_thinking', data: 'xxx' },
|
||||
{ type: 'text', text: 'svar' },
|
||||
{ type: 'tool_use', id: 't1', name: 'gnubok_load_skill', input: {} },
|
||||
]
|
||||
expect(stripThinking(blocks)).toEqual([
|
||||
{ type: 'text', text: 'svar' },
|
||||
{ type: 'tool_use', id: 't1', name: 'gnubok_load_skill', input: {} },
|
||||
])
|
||||
})
|
||||
|
||||
it('is a no-op when there are no thinking blocks', () => {
|
||||
const blocks = [{ type: 'text', text: 'x' }]
|
||||
expect(stripThinking(blocks)).toEqual(blocks)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,32 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { boundToolResultText, MAX_TOOL_RESULT_CHARS } from '../run-turn'
|
||||
|
||||
// Tool results re-enter the model context on every later loop iteration and are
|
||||
// persisted as a 'tool' message that loadConversationMessages replays on every
|
||||
// future turn. An unbounded read (gnubok_get_document_content returns full
|
||||
// OCR/PDF text) would therefore re-introduce the context rot we keep out of the
|
||||
// system prompt. boundToolResultText caps the serialized payload.
|
||||
|
||||
describe('boundToolResultText — tool-return discipline', () => {
|
||||
it('passes small results through unchanged', () => {
|
||||
const small = JSON.stringify({ rows: [{ account: '1930', amount: 1000 }] })
|
||||
expect(boundToolResultText(small)).toBe(small)
|
||||
})
|
||||
|
||||
it('passes a result exactly at the ceiling through unchanged', () => {
|
||||
const exact = 'x'.repeat(MAX_TOOL_RESULT_CHARS)
|
||||
expect(boundToolResultText(exact)).toBe(exact)
|
||||
})
|
||||
|
||||
it('truncates an oversized result to the ceiling and appends a steer', () => {
|
||||
const huge = 'x'.repeat(MAX_TOOL_RESULT_CHARS + 50_000)
|
||||
const out = boundToolResultText(huge)
|
||||
// The body is capped at the ceiling…
|
||||
expect(out.startsWith('x'.repeat(MAX_TOOL_RESULT_CHARS))).toBe(true)
|
||||
expect(out.length).toBeLessThan(huge.length)
|
||||
// …and the marker tells the model it was cut and how to narrow.
|
||||
expect(out).toContain('avkortat')
|
||||
expect(out).toContain(String(huge.length))
|
||||
expect(out.toLowerCase()).toMatch(/smalare|limit|datumintervall/)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,100 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import type { SupabaseClient } from '@supabase/supabase-js'
|
||||
import type { AgentIntent } from '@/lib/agent/intents/types'
|
||||
import { buildIdentityBlock } from '../system-prompt'
|
||||
|
||||
// buildIdentityBlock is the always-on Block 2 of the chat system prompt. Unlike
|
||||
// the per-intent ground rules (which sit only in the first user message and
|
||||
// fall out of salience deep in a conversation), this block is re-sent on every
|
||||
// turn. These guards lock in the epistemics rules added after the agent
|
||||
// confidently answered "matvaror är 12 %" from stale training memory — it
|
||||
// dropped to 6 % in April 2026 — and invented a "ränteintäkter från ALMI"
|
||||
// concern by inferring a lending business from an SNI code.
|
||||
|
||||
type VatStatus = Parameters<typeof buildIdentityBlock>[0]['vatStatus']
|
||||
|
||||
// Minimal base-typed intent — buildIdentityBlock only reads id, sheetTitle and
|
||||
// atoms.mode. (The concrete intents have narrow capture/template generics that
|
||||
// don't unify with the base AgentIntent the builder expects; the real call site
|
||||
// resolves intents through the registry as base-typed.)
|
||||
const intent: AgentIntent = {
|
||||
id: 'general.help',
|
||||
buttonLabel: 'x',
|
||||
sheetTitle: 'Fråga din assistent',
|
||||
atoms: { mode: 'progressive', horizontal: [], includeCompanyVertical: false, includeCompanyModifiers: false },
|
||||
tools: [],
|
||||
model: 'claude-sonnet-4-6',
|
||||
capture: async () => ({}),
|
||||
promptTemplate: () => '',
|
||||
}
|
||||
|
||||
function block(vatStatus: VatStatus): string {
|
||||
return buildIdentityBlock({
|
||||
intent,
|
||||
companyId: 'c1',
|
||||
companyName: 'Testbolaget AB',
|
||||
firstName: 'Jakob',
|
||||
profileSummary: null,
|
||||
rankedMemory: [],
|
||||
vatStatus,
|
||||
today: '2026-01-01 (torsdag)',
|
||||
// buildIdentityBlock never touches supabase; it's a pure render of args.
|
||||
supabase: {} as unknown as SupabaseClient,
|
||||
})
|
||||
}
|
||||
|
||||
const VAT_STATES: VatStatus[] = [
|
||||
null,
|
||||
{ vat_registered: true, vat_number: 'SE556677889901' },
|
||||
{ vat_registered: false, vat_number: null },
|
||||
]
|
||||
|
||||
describe('chat system prompt — always-on epistemics rules', () => {
|
||||
it('forces load-before-answer for regulatory figures, on every VAT status', () => {
|
||||
for (const vs of VAT_STATES) {
|
||||
const out = block(vs)
|
||||
expect(out).toContain('# Säkerhet i sak — ladda reglerna, gissa aldrig från minnet')
|
||||
// Must point at the load tool and demand reading before answering.
|
||||
expect(out).toContain('gnubok_load_skill')
|
||||
// The canonical staleness trap must be named so the rule is concrete,
|
||||
// not abstract: a model answering food VAT "12 %" from memory is wrong.
|
||||
expect(out).toContain('12 %→6 %')
|
||||
}
|
||||
})
|
||||
|
||||
it('kills the "I am sure" escape hatch and turns "are you sure?" into a verify signal', () => {
|
||||
const out = block(null)
|
||||
expect(out).toContain('ja, jag är säker')
|
||||
expect(out).toContain('är du säker?')
|
||||
// The instruction must be to load/verify, not to repeat the prior answer.
|
||||
expect(out.toLowerCase()).toContain('upprepa')
|
||||
})
|
||||
|
||||
it('forbids inferring the business from weak signals like SNI codes', () => {
|
||||
const out = block(null)
|
||||
expect(out).toContain('# Påstå inget om bolaget du inte grundat i data')
|
||||
expect(out).toContain('SNI-kod')
|
||||
// Resolve real uncertainty by reading data or asking — not by speculating.
|
||||
expect(out).toMatch(/läsverktyg|fråga/i)
|
||||
})
|
||||
|
||||
it('anchors relative-time reasoning to the supplied current date', () => {
|
||||
// Without an explicit "today" the model dates "förra månaden" / overdue
|
||||
// invoices / the current VAT period off its training cutoff. The date the
|
||||
// caller passes must land verbatim in the always-on block.
|
||||
const out = block(null)
|
||||
expect(out).toContain('# Dagens datum')
|
||||
expect(out).toContain('Idag är 2026-01-01 (torsdag).')
|
||||
// Must tell the model to trust this over its own sense of "now".
|
||||
expect(out).toContain('träningsdata')
|
||||
})
|
||||
|
||||
it('lets the agent read a pre-loaded atom directly instead of re-loading it', () => {
|
||||
// Declarative intents pre-load swedish-vat etc. into Block 1, so the rule
|
||||
// must not force a redundant gnubok_load_skill when the owning atom is
|
||||
// already present. This nuance used to live only in the per-intent KÄLLOR
|
||||
// line; it now lives here, in the single canonical epistemics home.
|
||||
const out = block(null)
|
||||
expect(out).toContain('redan laddad')
|
||||
})
|
||||
})
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
@@ -0,0 +1,174 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { loadUserDirectorship } from '../inputs'
|
||||
|
||||
// The composer can only put "Du driver…" in the user's mouth when we have
|
||||
// evidence the user actually directs this company. The signal: BankID
|
||||
// CompanyRoles for the active user, matched against this company's orgnr,
|
||||
// with a director-like positionType still active. These tests pin every
|
||||
// branch — false positives here means we'd narrate ownership for an
|
||||
// accountant or employee, which is the exact UX bug we just fixed.
|
||||
|
||||
function buildSupabase(opts: {
|
||||
companyOrgNumber?: string | null
|
||||
enrichment?: unknown
|
||||
}) {
|
||||
return {
|
||||
from: vi.fn().mockImplementation((table: string) => {
|
||||
const chain: Record<string, unknown> = {}
|
||||
const methods = ['select', 'eq', 'maybeSingle', 'single']
|
||||
for (const m of methods) {
|
||||
chain[m] = () => {
|
||||
if (m === 'single' && table === 'companies') {
|
||||
return Promise.resolve({
|
||||
data: { org_number: opts.companyOrgNumber ?? null },
|
||||
error: null,
|
||||
})
|
||||
}
|
||||
if (m === 'maybeSingle' && table === 'bankid_enrichment') {
|
||||
return Promise.resolve({
|
||||
data: opts.enrichment ?? null,
|
||||
error: null,
|
||||
})
|
||||
}
|
||||
return chain
|
||||
}
|
||||
}
|
||||
return chain
|
||||
}),
|
||||
} as never
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
describe('loadUserDirectorship', () => {
|
||||
it('returns confirmedDirector=true when user has a boardMember position at this orgnr', async () => {
|
||||
const supabase = buildSupabase({
|
||||
companyOrgNumber: '5560125790',
|
||||
enrichment: {
|
||||
company_roles: [
|
||||
{
|
||||
companyRegistrationNumber: '5560125790',
|
||||
positionTypes: ['boardMember'],
|
||||
positionEnd: null,
|
||||
},
|
||||
],
|
||||
},
|
||||
})
|
||||
|
||||
const result = await loadUserDirectorship(supabase, 'co-1')
|
||||
expect(result.confirmedDirector).toBe(true)
|
||||
})
|
||||
|
||||
it('returns confirmedDirector=true for ceo role', async () => {
|
||||
const supabase = buildSupabase({
|
||||
companyOrgNumber: '5560125790',
|
||||
enrichment: {
|
||||
company_roles: [
|
||||
{
|
||||
companyRegistrationNumber: '5560125790',
|
||||
positionTypes: ['ceo'],
|
||||
positionEnd: null,
|
||||
},
|
||||
],
|
||||
},
|
||||
})
|
||||
|
||||
const result = await loadUserDirectorship(supabase, 'co-1')
|
||||
expect(result.confirmedDirector).toBe(true)
|
||||
})
|
||||
|
||||
it('matches even when orgnr is hyphen-formatted in CompanyRoles', async () => {
|
||||
const supabase = buildSupabase({
|
||||
companyOrgNumber: '5560125790',
|
||||
enrichment: {
|
||||
company_roles: [
|
||||
{
|
||||
companyRegistrationNumber: '556012-5790',
|
||||
positionTypes: ['chairman'],
|
||||
positionEnd: null,
|
||||
},
|
||||
],
|
||||
},
|
||||
})
|
||||
|
||||
const result = await loadUserDirectorship(supabase, 'co-1')
|
||||
expect(result.confirmedDirector).toBe(true)
|
||||
})
|
||||
|
||||
it('returns confirmedDirector=false when company has no org_number (manual-name signup)', async () => {
|
||||
const supabase = buildSupabase({
|
||||
companyOrgNumber: null,
|
||||
enrichment: { company_roles: [] },
|
||||
})
|
||||
|
||||
const result = await loadUserDirectorship(supabase, 'co-1')
|
||||
expect(result.confirmedDirector).toBe(false)
|
||||
})
|
||||
|
||||
it('returns confirmedDirector=false when user has no BankID enrichment (email signup)', async () => {
|
||||
const supabase = buildSupabase({
|
||||
companyOrgNumber: '5560125790',
|
||||
enrichment: null,
|
||||
})
|
||||
|
||||
const result = await loadUserDirectorship(supabase, 'co-1')
|
||||
expect(result.confirmedDirector).toBe(false)
|
||||
})
|
||||
|
||||
it('returns confirmedDirector=false when CompanyRoles has no match for this orgnr', async () => {
|
||||
const supabase = buildSupabase({
|
||||
companyOrgNumber: '5560125790',
|
||||
enrichment: {
|
||||
company_roles: [
|
||||
{
|
||||
companyRegistrationNumber: '5567890123', // different company
|
||||
positionTypes: ['ceo'],
|
||||
positionEnd: null,
|
||||
},
|
||||
],
|
||||
},
|
||||
})
|
||||
|
||||
const result = await loadUserDirectorship(supabase, 'co-1')
|
||||
expect(result.confirmedDirector).toBe(false)
|
||||
})
|
||||
|
||||
it('returns confirmedDirector=false when position has already ended', async () => {
|
||||
const supabase = buildSupabase({
|
||||
companyOrgNumber: '5560125790',
|
||||
enrichment: {
|
||||
company_roles: [
|
||||
{
|
||||
companyRegistrationNumber: '5560125790',
|
||||
positionTypes: ['boardMember'],
|
||||
// 1 year ago
|
||||
positionEnd: new Date(Date.now() - 365 * 24 * 3600_000).toISOString(),
|
||||
},
|
||||
],
|
||||
},
|
||||
})
|
||||
|
||||
const result = await loadUserDirectorship(supabase, 'co-1')
|
||||
expect(result.confirmedDirector).toBe(false)
|
||||
})
|
||||
|
||||
it('returns confirmedDirector=false for non-director positions (deputyBoardMember, auditor)', async () => {
|
||||
const supabase = buildSupabase({
|
||||
companyOrgNumber: '5560125790',
|
||||
enrichment: {
|
||||
company_roles: [
|
||||
{
|
||||
companyRegistrationNumber: '5560125790',
|
||||
positionTypes: ['deputyBoardMember', 'auditor'],
|
||||
positionEnd: null,
|
||||
},
|
||||
],
|
||||
},
|
||||
})
|
||||
|
||||
const result = await loadUserDirectorship(supabase, 'co-1')
|
||||
expect(result.confirmedDirector).toBe(false)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,82 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { fallbackNarrative } from '../fallback'
|
||||
import type { ComposerInputs } from '../inputs'
|
||||
|
||||
// The agent profile narrative has two voices depending on whether the user
|
||||
// is a verified director at the company:
|
||||
//
|
||||
// - confirmed director → "Du driver Coredination AB..."
|
||||
// - everyone else → "Coredination AB är..."
|
||||
//
|
||||
// The Sonnet path injects this via the system prompt (covered manually in
|
||||
// system-prompt eval), so these tests target the deterministic fallback —
|
||||
// which is what ships when Sonnet times out or the API key is missing.
|
||||
// The fallback is the worst-case render and must never put presumptive
|
||||
// ownership words in a non-director user's mouth.
|
||||
|
||||
function makeInputs(overrides: Partial<ComposerInputs>): ComposerInputs {
|
||||
return {
|
||||
companyId: 'co-1',
|
||||
companyName: 'Coredination AB',
|
||||
entityType: 'aktiebolag',
|
||||
ticSnapshot: null,
|
||||
ticFetchedAt: null,
|
||||
companySettings: null,
|
||||
sieSummary: null,
|
||||
bankingSummary: null,
|
||||
atomIndex: [],
|
||||
userIsConfirmedDirector: false,
|
||||
...overrides,
|
||||
}
|
||||
}
|
||||
|
||||
describe('fallbackNarrative voice branching', () => {
|
||||
it('uses second-person "Du driver" when user is a confirmed director', () => {
|
||||
const text = fallbackNarrative(makeInputs({ userIsConfirmedDirector: true }))
|
||||
expect(text).toMatch(/Du driver Coredination AB som aktiebolag\./)
|
||||
expect(text).toMatch(/din verksamhet/i)
|
||||
})
|
||||
|
||||
it('uses neutral third-person when user is NOT a confirmed director', () => {
|
||||
const text = fallbackNarrative(makeInputs({ userIsConfirmedDirector: false }))
|
||||
expect(text).toMatch(/Coredination AB är ett aktiebolag\./)
|
||||
// Critical: must not assume the user owns or runs the company.
|
||||
expect(text).not.toMatch(/\bDu driver\b/)
|
||||
expect(text).not.toMatch(/\bdin verksamhet\b/i)
|
||||
})
|
||||
|
||||
it('keeps neutral voice for enskild firma when not confirmed', () => {
|
||||
const text = fallbackNarrative(
|
||||
makeInputs({
|
||||
entityType: 'enskild_firma',
|
||||
companyName: 'Anna Andersson',
|
||||
userIsConfirmedDirector: false,
|
||||
}),
|
||||
)
|
||||
expect(text).toMatch(/Anna Andersson är en enskild firma\./)
|
||||
expect(text).not.toMatch(/Du driver/)
|
||||
})
|
||||
|
||||
it('uses second-person for enskild firma when director is confirmed', () => {
|
||||
const text = fallbackNarrative(
|
||||
makeInputs({
|
||||
entityType: 'enskild_firma',
|
||||
companyName: 'Anna Andersson',
|
||||
userIsConfirmedDirector: true,
|
||||
}),
|
||||
)
|
||||
expect(text).toMatch(/Du driver Anna Andersson som enskild firma\./)
|
||||
})
|
||||
|
||||
it('falls back gracefully when entityType is unknown', () => {
|
||||
const text = fallbackNarrative(
|
||||
makeInputs({
|
||||
entityType: 'handelsbolag',
|
||||
userIsConfirmedDirector: false,
|
||||
}),
|
||||
)
|
||||
// Generic neutral form — still no "Du driver".
|
||||
expect(text).toMatch(/Coredination AB är ett företag/)
|
||||
expect(text).not.toMatch(/Du driver/)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,54 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { AtomSelectionSchema, ATOM_SELECTION_TOOL_SCHEMA } from '../schemas'
|
||||
|
||||
describe('AtomSelectionSchema', () => {
|
||||
it('accepts a minimal valid selection', () => {
|
||||
const result = AtomSelectionSchema.safeParse({
|
||||
horizontal_atoms: ['horizontal/swedish-vat'],
|
||||
vertical_atoms: [],
|
||||
modifier_atoms: [],
|
||||
is_multi_vertical: false,
|
||||
verification_questions: [],
|
||||
uncertainty_notes: [],
|
||||
})
|
||||
expect(result.success).toBe(true)
|
||||
})
|
||||
|
||||
it('rejects missing fields', () => {
|
||||
const result = AtomSelectionSchema.safeParse({
|
||||
horizontal_atoms: ['horizontal/swedish-vat'],
|
||||
})
|
||||
expect(result.success).toBe(false)
|
||||
})
|
||||
|
||||
it('rejects wrong types', () => {
|
||||
const result = AtomSelectionSchema.safeParse({
|
||||
horizontal_atoms: 'not-an-array',
|
||||
vertical_atoms: [],
|
||||
modifier_atoms: [],
|
||||
is_multi_vertical: false,
|
||||
verification_questions: [],
|
||||
uncertainty_notes: [],
|
||||
})
|
||||
expect(result.success).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('ATOM_SELECTION_TOOL_SCHEMA', () => {
|
||||
it('declares the same required fields as the Zod schema', () => {
|
||||
// Sanity: both schemas must list the same required keys, or atom selection
|
||||
// requests will silently drop fields the Zod parser then rejects.
|
||||
expect(ATOM_SELECTION_TOOL_SCHEMA.required).toEqual([
|
||||
'horizontal_atoms',
|
||||
'vertical_atoms',
|
||||
'modifier_atoms',
|
||||
'is_multi_vertical',
|
||||
'verification_questions',
|
||||
'uncertainty_notes',
|
||||
])
|
||||
})
|
||||
|
||||
it('forbids additional properties so hallucinated keys fail loudly', () => {
|
||||
expect(ATOM_SELECTION_TOOL_SCHEMA.additionalProperties).toBe(false)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,314 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||
import { ensureTicSnapshot } from '../tic-fetch'
|
||||
|
||||
// `ensureTicSnapshot` is the single chokepoint between the agent build path
|
||||
// and the TIC /profile endpoint. Every TIC call from agent onboarding goes
|
||||
// through here, and the 3000/mo Lens budget makes the cache / fallback
|
||||
// branches load-bearing. These tests cover:
|
||||
//
|
||||
// - cache hit (fresh): no /profile call
|
||||
// - cache miss (no snapshot): /profile fetched + persisted
|
||||
// - cache hit but stale (>7d): refetch + persist
|
||||
// - cache hit but v1 shape + upgradeV1=true: refetch + persist
|
||||
// - cache hit but v1 shape + upgradeV1=false: stays v1 (budget protection)
|
||||
// - /profile fetch fails: returns existing snapshot (degraded, doesn't crash)
|
||||
// - company has no org_number anywhere: returns fallback null
|
||||
|
||||
const ORIGIN = 'http://localhost:3000'
|
||||
const COMPANY_ID = 'company-uuid'
|
||||
|
||||
function buildSupabase(
|
||||
selectResult: { data: unknown; error: unknown } = { data: null, error: null },
|
||||
settingsSelectResult: { data: unknown; error: unknown } = { data: null, error: null },
|
||||
updateResult: { error: unknown } = { error: null },
|
||||
) {
|
||||
const updateCalls: unknown[][] = []
|
||||
const fromCalls: string[] = []
|
||||
const from = vi.fn().mockImplementation((table: string) => {
|
||||
fromCalls.push(table)
|
||||
// Build a chain that resolves to whichever select result matches the
|
||||
// table. The function under test queries `companies` first, then
|
||||
// optionally `company_settings`, then `companies.update`.
|
||||
const chain: Record<string, unknown> = {}
|
||||
const methods = ['select', 'eq', 'limit', 'maybeSingle', 'single']
|
||||
for (const m of methods) {
|
||||
chain[m] = () => {
|
||||
if (m === 'single') {
|
||||
return Promise.resolve(table === 'companies' ? selectResult : settingsSelectResult)
|
||||
}
|
||||
if (m === 'maybeSingle') {
|
||||
return Promise.resolve(table === 'companies' ? selectResult : settingsSelectResult)
|
||||
}
|
||||
return chain
|
||||
}
|
||||
}
|
||||
chain.update = (payload: unknown) => {
|
||||
updateCalls.push([payload])
|
||||
const updateChain: Record<string, unknown> = {}
|
||||
updateChain.eq = () => Promise.resolve(updateResult)
|
||||
return updateChain
|
||||
}
|
||||
return chain
|
||||
})
|
||||
return {
|
||||
supabase: { from } as never,
|
||||
fromCalls,
|
||||
updateCalls,
|
||||
}
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.stubGlobal('fetch', vi.fn())
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
describe('ensureTicSnapshot — cache hit', () => {
|
||||
it('returns cached snapshot without hitting TIC when the row is fresh and v2-shaped', async () => {
|
||||
const fetchedAt = new Date(Date.now() - 60_000).toISOString() // 1 min ago
|
||||
const cached = { statuses: [], companyName: 'Cached AB' }
|
||||
const { supabase, fromCalls } = buildSupabase({
|
||||
data: { org_number: '5560125790', tic_snapshot: cached, tic_snapshot_fetched_at: fetchedAt },
|
||||
error: null,
|
||||
})
|
||||
|
||||
const result = await ensureTicSnapshot({
|
||||
supabase,
|
||||
companyId: COMPANY_ID,
|
||||
cookieHeader: '',
|
||||
origin: ORIGIN,
|
||||
})
|
||||
|
||||
expect(result.source).toBe('cached')
|
||||
expect(result.snapshot).toEqual(cached)
|
||||
expect(fetch).not.toHaveBeenCalled()
|
||||
// Only the companies SELECT should have run — no profile fetch, no update.
|
||||
expect(fromCalls).toEqual(['companies'])
|
||||
})
|
||||
|
||||
it('does NOT call /profile when cache is fresh and upgradeV1=false, even if snapshot is v1', async () => {
|
||||
// V1 snapshot = no `statuses` key. Without upgradeV1=true, we accept it
|
||||
// as-is to protect the TIC budget across the customer base.
|
||||
const v1Snapshot = { companyName: 'V1 AB' /* no `statuses` */ }
|
||||
const fetchedAt = new Date(Date.now() - 60_000).toISOString()
|
||||
const { supabase } = buildSupabase({
|
||||
data: { org_number: '5560125790', tic_snapshot: v1Snapshot, tic_snapshot_fetched_at: fetchedAt },
|
||||
error: null,
|
||||
})
|
||||
|
||||
const result = await ensureTicSnapshot({
|
||||
supabase,
|
||||
companyId: COMPANY_ID,
|
||||
cookieHeader: '',
|
||||
origin: ORIGIN,
|
||||
// upgradeV1 omitted -> defaults to false
|
||||
})
|
||||
|
||||
expect(result.source).toBe('cached')
|
||||
expect(result.snapshot).toEqual(v1Snapshot)
|
||||
expect(fetch).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
describe('ensureTicSnapshot — cache miss & refetch', () => {
|
||||
it('fetches /profile and persists when no snapshot exists', async () => {
|
||||
const profile = { statuses: [], companyName: 'Fresh AB' }
|
||||
vi.mocked(fetch).mockResolvedValue(
|
||||
new Response(JSON.stringify({ data: profile }), { status: 200 }),
|
||||
)
|
||||
|
||||
const { supabase, updateCalls } = buildSupabase({
|
||||
data: { org_number: '5560125790', tic_snapshot: null, tic_snapshot_fetched_at: null },
|
||||
error: null,
|
||||
})
|
||||
|
||||
const result = await ensureTicSnapshot({
|
||||
supabase,
|
||||
companyId: COMPANY_ID,
|
||||
cookieHeader: 'sb-auth=abc',
|
||||
origin: ORIGIN,
|
||||
})
|
||||
|
||||
expect(result.source).toBe('fetched')
|
||||
expect(result.snapshot).toEqual(profile)
|
||||
// Hit the /profile endpoint with cookie forwarded
|
||||
expect(fetch).toHaveBeenCalledTimes(1)
|
||||
const fetchUrl = vi.mocked(fetch).mock.calls[0][0] as string
|
||||
expect(fetchUrl).toContain('/api/extensions/ext/tic/profile')
|
||||
expect(fetchUrl).toContain('org_number=5560125790')
|
||||
// Persisted via UPDATE
|
||||
expect(updateCalls).toHaveLength(1)
|
||||
const persisted = updateCalls[0][0] as Record<string, unknown>
|
||||
expect(persisted.tic_snapshot).toEqual(profile)
|
||||
expect(persisted.tic_snapshot_fetched_at).toBeDefined()
|
||||
})
|
||||
|
||||
it('refetches when cached snapshot is stale (>7 days)', async () => {
|
||||
const eightDaysAgo = new Date(Date.now() - 8 * 24 * 3600_000).toISOString()
|
||||
const oldSnapshot = { statuses: [], companyName: 'Old AB' }
|
||||
const freshProfile = { statuses: [], companyName: 'Fresh AB' }
|
||||
vi.mocked(fetch).mockResolvedValue(
|
||||
new Response(JSON.stringify({ data: freshProfile }), { status: 200 }),
|
||||
)
|
||||
|
||||
const { supabase } = buildSupabase({
|
||||
data: {
|
||||
org_number: '5560125790',
|
||||
tic_snapshot: oldSnapshot,
|
||||
tic_snapshot_fetched_at: eightDaysAgo,
|
||||
},
|
||||
error: null,
|
||||
})
|
||||
|
||||
const result = await ensureTicSnapshot({
|
||||
supabase,
|
||||
companyId: COMPANY_ID,
|
||||
cookieHeader: '',
|
||||
origin: ORIGIN,
|
||||
})
|
||||
|
||||
expect(result.source).toBe('fetched')
|
||||
expect(result.snapshot).toEqual(freshProfile)
|
||||
expect(fetch).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it('refetches v1 snapshot when upgradeV1=true, even if still fresh', async () => {
|
||||
const v1Snapshot = { companyName: 'V1 AB' /* no statuses */ }
|
||||
const v2Profile = { statuses: [], companyName: 'V2 AB' }
|
||||
vi.mocked(fetch).mockResolvedValue(
|
||||
new Response(JSON.stringify({ data: v2Profile }), { status: 200 }),
|
||||
)
|
||||
|
||||
const { supabase } = buildSupabase({
|
||||
data: {
|
||||
org_number: '5560125790',
|
||||
tic_snapshot: v1Snapshot,
|
||||
tic_snapshot_fetched_at: new Date().toISOString(),
|
||||
},
|
||||
error: null,
|
||||
})
|
||||
|
||||
const result = await ensureTicSnapshot({
|
||||
supabase,
|
||||
companyId: COMPANY_ID,
|
||||
cookieHeader: '',
|
||||
origin: ORIGIN,
|
||||
upgradeV1: true,
|
||||
})
|
||||
|
||||
expect(result.source).toBe('fetched')
|
||||
expect(result.snapshot).toEqual(v2Profile)
|
||||
expect(fetch).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
})
|
||||
|
||||
describe('ensureTicSnapshot — degraded paths', () => {
|
||||
it('returns fallback null when companies row does not exist', async () => {
|
||||
const { supabase } = buildSupabase({ data: null, error: null })
|
||||
|
||||
const result = await ensureTicSnapshot({
|
||||
supabase,
|
||||
companyId: COMPANY_ID,
|
||||
cookieHeader: '',
|
||||
origin: ORIGIN,
|
||||
})
|
||||
|
||||
expect(result.source).toBe('fallback')
|
||||
expect(result.snapshot).toBeNull()
|
||||
expect(fetch).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('falls back to company_settings.org_number when companies.org_number is null', async () => {
|
||||
const profile = { statuses: [], companyName: 'EF Person' }
|
||||
vi.mocked(fetch).mockResolvedValue(
|
||||
new Response(JSON.stringify({ data: profile }), { status: 200 }),
|
||||
)
|
||||
|
||||
const { supabase } = buildSupabase(
|
||||
{ data: { org_number: null, tic_snapshot: null, tic_snapshot_fetched_at: null }, error: null },
|
||||
{ data: { org_number: '8001011231' }, error: null },
|
||||
)
|
||||
|
||||
const result = await ensureTicSnapshot({
|
||||
supabase,
|
||||
companyId: COMPANY_ID,
|
||||
cookieHeader: '',
|
||||
origin: ORIGIN,
|
||||
})
|
||||
|
||||
expect(result.source).toBe('fetched')
|
||||
// Org number from company_settings flowed into the profile URL
|
||||
const fetchUrl = vi.mocked(fetch).mock.calls[0][0] as string
|
||||
expect(fetchUrl).toContain('org_number=8001011231')
|
||||
})
|
||||
|
||||
it('returns fallback null when no org_number is available anywhere', async () => {
|
||||
const { supabase } = buildSupabase(
|
||||
{ data: { org_number: null, tic_snapshot: null, tic_snapshot_fetched_at: null }, error: null },
|
||||
{ data: { org_number: null }, error: null },
|
||||
)
|
||||
|
||||
const result = await ensureTicSnapshot({
|
||||
supabase,
|
||||
companyId: COMPANY_ID,
|
||||
cookieHeader: '',
|
||||
origin: ORIGIN,
|
||||
})
|
||||
|
||||
expect(result.source).toBe('fallback')
|
||||
expect(result.snapshot).toBeNull()
|
||||
expect(fetch).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('returns existing (stale) snapshot when /profile fetch fails', async () => {
|
||||
const staleSnapshot = { companyName: 'Stale AB' }
|
||||
vi.mocked(fetch).mockResolvedValue(new Response('upstream down', { status: 502 }))
|
||||
|
||||
const { supabase } = buildSupabase({
|
||||
data: {
|
||||
org_number: '5560125790',
|
||||
tic_snapshot: staleSnapshot,
|
||||
tic_snapshot_fetched_at: new Date(Date.now() - 8 * 24 * 3600_000).toISOString(), // forces refetch
|
||||
},
|
||||
error: null,
|
||||
})
|
||||
|
||||
const result = await ensureTicSnapshot({
|
||||
supabase,
|
||||
companyId: COMPANY_ID,
|
||||
cookieHeader: '',
|
||||
origin: ORIGIN,
|
||||
})
|
||||
|
||||
expect(result.source).toBe('fallback')
|
||||
// Degrade to the stale snapshot rather than crash — the agent build path
|
||||
// depends on this contract so a TIC outage never blocks onboarding.
|
||||
expect(result.snapshot).toEqual(staleSnapshot)
|
||||
})
|
||||
|
||||
it('returns existing snapshot when /profile fetch throws (network error / timeout)', async () => {
|
||||
const staleSnapshot = { companyName: 'Stale AB' }
|
||||
vi.mocked(fetch).mockRejectedValue(new Error('ECONNREFUSED'))
|
||||
|
||||
const { supabase } = buildSupabase({
|
||||
data: {
|
||||
org_number: '5560125790',
|
||||
tic_snapshot: staleSnapshot,
|
||||
tic_snapshot_fetched_at: new Date(Date.now() - 8 * 24 * 3600_000).toISOString(),
|
||||
},
|
||||
error: null,
|
||||
})
|
||||
|
||||
const result = await ensureTicSnapshot({
|
||||
supabase,
|
||||
companyId: COMPANY_ID,
|
||||
cookieHeader: '',
|
||||
origin: ORIGIN,
|
||||
})
|
||||
|
||||
expect(result.source).toBe('fallback')
|
||||
expect(result.snapshot).toEqual(staleSnapshot)
|
||||
})
|
||||
})
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,59 @@
|
||||
import AnthropicBedrock from '@anthropic-ai/bedrock-sdk'
|
||||
|
||||
let cached: AnthropicBedrock | null = null
|
||||
|
||||
// Single AnthropicBedrock client for the agent composer + chat loop. Matches
|
||||
// the credential surface the rest of the codebase already uses (see
|
||||
// extensions/general/invoice-inbox/lib/extract-invoice-fields.ts) so:
|
||||
//
|
||||
// 1. There's no separate ANTHROPIC_API_KEY to provision and rotate.
|
||||
// 2. All Claude traffic stays in eu-north-1 — important for Swedish
|
||||
// accounting data under BFL retention.
|
||||
// 3. Failures and quotas show up in one AWS surface, not two.
|
||||
//
|
||||
// Trade-off vs. the direct Anthropic API: Bedrock's prompt-cache TTL is
|
||||
// 5 minutes (default) rather than the 1h the plan §10 specifies. We still
|
||||
// pass `cache_control: { type: 'ephemeral', ttl: '1h' }` in the system
|
||||
// prompt assembly — Bedrock currently ignores the explicit TTL and uses 5m.
|
||||
// Cache effectiveness drops on multi-minute gaps but the loop still works.
|
||||
// Revisit if/when Bedrock exposes longer TTLs or if cost forces the direct
|
||||
// API.
|
||||
export function getAnthropic(): AnthropicBedrock {
|
||||
if (cached) return cached
|
||||
const awsRegion = process.env.AWS_REGION || 'eu-north-1'
|
||||
const awsAccessKey = process.env.AWS_ACCESS_KEY_ID
|
||||
const awsSecretKey = process.env.AWS_SECRET_ACCESS_KEY
|
||||
// When both static keys are present, pass them. Otherwise omit them so the
|
||||
// SDK falls back to the AWS credential provider chain (instance profile,
|
||||
// IRSA, EKS pod identity, ...). The two-overload SDK refuses a mix.
|
||||
cached =
|
||||
awsAccessKey && awsSecretKey
|
||||
? new AnthropicBedrock({ awsRegion, awsAccessKey, awsSecretKey })
|
||||
: new AnthropicBedrock({ awsRegion })
|
||||
return cached
|
||||
}
|
||||
|
||||
// Bedrock model IDs. Region prefix `eu.` keeps inference inside eu-north-1.
|
||||
// Both are env-overridable so ops can swap models without a code deploy.
|
||||
//
|
||||
// Per plan §14 the composer's atom-selection call should run on Opus 4.7 for
|
||||
// the higher-stakes selection reasoning. Opus 4.7 is not yet enabled on this
|
||||
// AWS Bedrock account (403 "not available for this account" — request access
|
||||
// on the AWS console under Bedrock → Model access). For now we point OPUS at
|
||||
// Sonnet 4.6 so the composer still works; atom selection on Sonnet is still
|
||||
// good — it's a structured-output call via tool_use forcing, not deep
|
||||
// reasoning. Flip BEDROCK_OPUS_MODEL_ID back to eu.anthropic.claude-opus-4-7
|
||||
// once Opus access lands.
|
||||
export const OPUS_MODEL = process.env.BEDROCK_OPUS_MODEL_ID || 'eu.anthropic.claude-sonnet-4-6'
|
||||
export const SONNET_MODEL = process.env.BEDROCK_SONNET_MODEL_ID || 'eu.anthropic.claude-sonnet-4-6'
|
||||
|
||||
// Extended-thinking budgets (budget_tokens) for the chat intents. These are
|
||||
// ceilings, not floors: the model spends only what a turn needs, so a generous
|
||||
// cap improves hard turns (multi-source VAT synthesis, anomaly detection)
|
||||
// without taxing simple ones. run-turn derives max_tokens = budget + 4096, so
|
||||
// raising these is safe — no manual max_tokens bookkeeping. Tiered to match the
|
||||
// model split: DEEP for the Opus / heavy-reasoning intents, STANDARD for the
|
||||
// rest. Early-stage default favours reasoning quality over token cost; dial
|
||||
// down here in one place if latency/cost ever bites.
|
||||
export const THINKING_BUDGET_STANDARD = 6000
|
||||
export const THINKING_BUDGET_DEEP = 12000
|
||||
@@ -0,0 +1,137 @@
|
||||
import type { ComposerInputs } from './inputs'
|
||||
import type { AtomSelection } from './schemas'
|
||||
|
||||
// Deterministic atom selection used when the Opus call times out or fails.
|
||||
//
|
||||
// Per plan §7 Phase A: "Fall back to a default vertical from SNI prefix and
|
||||
// a generic horizontal set (vat, invoice, year-end)". The result is good
|
||||
// enough that the user can proceed; they can refine atom selection in Phase B
|
||||
// or rebuild later.
|
||||
//
|
||||
// We pick deliberately conservatively — better to load a few extra horizontals
|
||||
// than to miss one. The agent loop pays for cache, not for content; an extra
|
||||
// 8k tokens of swedish-financial-reporting on a sole-trader profile is cheap
|
||||
// noise, while missing swedish-vat on any Swedish company is a correctness bug.
|
||||
export function fallbackAtomSelection(inputs: ComposerInputs): AtomSelection {
|
||||
const knownIds = new Set(inputs.atomIndex.map((a) => a.id))
|
||||
const has = (id: string) => knownIds.has(id)
|
||||
|
||||
const isAB = inputs.entityType === 'aktiebolag'
|
||||
const isEF = inputs.entityType === 'enskild_firma'
|
||||
|
||||
const tic = inputs.ticSnapshot as
|
||||
| {
|
||||
registration?: { payroll?: boolean }
|
||||
sniCodes?: { code: string; name: string }[]
|
||||
employeeRange?: string | null
|
||||
}
|
||||
| null
|
||||
const isEmployer = Boolean(tic?.registration?.payroll)
|
||||
|
||||
const horizontal: string[] = []
|
||||
// These three apply to every Swedish business.
|
||||
pushIfKnown(horizontal, 'horizontal/swedish-vat', has)
|
||||
pushIfKnown(horizontal, 'horizontal/swedish-invoice-compliance', has)
|
||||
pushIfKnown(horizontal, 'horizontal/swedish-year-end-closing', has)
|
||||
pushIfKnown(horizontal, 'horizontal/swedish-accounting-compliance', has)
|
||||
// SIE and assets are common needs across both entity types.
|
||||
pushIfKnown(horizontal, 'horizontal/swedish-sie-import-export', has)
|
||||
pushIfKnown(horizontal, 'horizontal/swedish-asset-accounting', has)
|
||||
|
||||
if (isAB) {
|
||||
pushIfKnown(horizontal, 'horizontal/swedish-financial-reporting', has)
|
||||
pushIfKnown(horizontal, 'horizontal/swedish-sru-filing', has)
|
||||
pushIfKnown(horizontal, 'horizontal/swedish-tax-planning', has)
|
||||
}
|
||||
|
||||
if (isEmployer) {
|
||||
pushIfKnown(horizontal, 'horizontal/swedish-payroll', has)
|
||||
}
|
||||
|
||||
// Vertical fallback: best-effort SNI-prefix match. Empty list is acceptable
|
||||
// — vertical atoms are not yet authored (Phase 3).
|
||||
const verticals: string[] = []
|
||||
const sniCodes = tic?.sniCodes ?? []
|
||||
if (sniCodes.length > 0) {
|
||||
for (const atom of inputs.atomIndex) {
|
||||
if (atom.tier !== 'vertical') continue
|
||||
const matches = sniCodes.some((sni) =>
|
||||
atom.sni_prefixes.some((prefix) => sni.code.startsWith(prefix)),
|
||||
)
|
||||
if (matches) verticals.push(atom.id)
|
||||
}
|
||||
}
|
||||
|
||||
// Modifier fallback: pick what we can derive from entity_type + employer flag.
|
||||
const modifiers: string[] = []
|
||||
if (isAB) {
|
||||
pushIfKnown(modifiers, 'modifier/single-shareholder-ab-fmb', has)
|
||||
}
|
||||
if (isEF) {
|
||||
pushIfKnown(modifiers, 'modifier/enskild-firma', has)
|
||||
}
|
||||
if (isEmployer) {
|
||||
pushIfKnown(modifiers, 'modifier/small-employer', has)
|
||||
}
|
||||
|
||||
return {
|
||||
horizontal_atoms: horizontal,
|
||||
vertical_atoms: verticals,
|
||||
modifier_atoms: modifiers,
|
||||
is_multi_vertical: verticals.length > 1,
|
||||
verification_questions: buildFallbackQuestions(inputs),
|
||||
uncertainty_notes: ['Selection produced by deterministic fallback — Opus call failed or was skipped.'],
|
||||
}
|
||||
}
|
||||
|
||||
function pushIfKnown(arr: string[], id: string, has: (id: string) => boolean) {
|
||||
if (has(id)) arr.push(id)
|
||||
}
|
||||
|
||||
function buildFallbackQuestions(inputs: ComposerInputs): string[] {
|
||||
const qs: string[] = []
|
||||
if (!inputs.ticSnapshot) {
|
||||
qs.push('Vad är din huvudsakliga verksamhet? (några ord räcker)')
|
||||
}
|
||||
if (inputs.entityType === 'aktiebolag') {
|
||||
qs.push('Är du ensamägare till bolaget?')
|
||||
qs.push('Har bolaget anställda förutom dig?')
|
||||
}
|
||||
qs.push('Vilken momsperiod använder ni — månad, kvartal eller år?')
|
||||
return qs
|
||||
}
|
||||
|
||||
// Build a minimal Swedish narrative for the fallback path so Phase B has
|
||||
// something to render even when the Sonnet call also failed. Mirrors the
|
||||
// Sonnet prompt's voice-branching: second-person only when the user is a
|
||||
// confirmed director, neutral otherwise.
|
||||
export function fallbackNarrative(inputs: ComposerInputs): string {
|
||||
const parts: string[] = []
|
||||
const name = inputs.companyName || 'företaget'
|
||||
const isAB = inputs.entityType === 'aktiebolag'
|
||||
const isEF = inputs.entityType === 'enskild_firma'
|
||||
const form = isAB ? 'aktiebolag' : isEF ? 'enskild firma' : null
|
||||
|
||||
if (inputs.userIsConfirmedDirector) {
|
||||
if (form) {
|
||||
parts.push(`Du driver ${name} som ${form}.`)
|
||||
} else {
|
||||
parts.push(`Du driver ${name}.`)
|
||||
}
|
||||
} else {
|
||||
if (form) {
|
||||
parts.push(`${name} är ${form === 'enskild firma' ? 'en enskild firma' : 'ett aktiebolag'}.`)
|
||||
} else {
|
||||
parts.push(`${name} är ett företag i gnubok.`)
|
||||
}
|
||||
}
|
||||
parts.push(
|
||||
'Jag har laddat de svenska reglerna som gäller bredast — moms, fakturering, bokslut och årsavslutning.',
|
||||
)
|
||||
parts.push(
|
||||
inputs.userIsConfirmedDirector
|
||||
? 'Berätta gärna lite mer om din verksamhet i nästa steg så kan jag skräddarsy stöden mer.'
|
||||
: 'Berätta gärna lite mer om verksamheten i nästa steg så kan jag skräddarsy stöden mer.',
|
||||
)
|
||||
return parts.join(' ')
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
import type { SupabaseClient } from '@supabase/supabase-js'
|
||||
import { gatherComposerInputs, inputsToSourceSignals } from './inputs'
|
||||
import { selectAtoms } from './atom-selection'
|
||||
import { writeNarrative } from './narrative'
|
||||
import { preWarmAtomCache } from './prewarm'
|
||||
import { OPUS_MODEL } from './client'
|
||||
import type { ComposedProfile } from './schemas'
|
||||
|
||||
interface ComposeOptions {
|
||||
// When true, runs the selection + narrative + pre-warm but does not write
|
||||
// to agent_profiles. Useful for evaluating composer output before commit.
|
||||
dryRun?: boolean
|
||||
// When true, skips cache pre-warm. Tests and CI typically set this.
|
||||
skipPrewarm?: boolean
|
||||
}
|
||||
|
||||
// Top-level composer: gather inputs → select atoms (Opus) → write narrative
|
||||
// (Sonnet) → persist agent_profiles row → fire-and-forget cache pre-warm.
|
||||
//
|
||||
// See dev_docs/specialized-agent-plan.md §6.
|
||||
export async function composeAgentProfile(
|
||||
supabase: SupabaseClient,
|
||||
companyId: string,
|
||||
options: ComposeOptions = {},
|
||||
): Promise<ComposedProfile> {
|
||||
const inputs = await gatherComposerInputs(supabase, companyId)
|
||||
|
||||
const selection = await selectAtoms(inputs)
|
||||
const profileSummary = await writeNarrative(inputs, selection)
|
||||
|
||||
const sourceSignals = inputsToSourceSignals(inputs)
|
||||
const composedAt = new Date().toISOString()
|
||||
const composerModel = OPUS_MODEL
|
||||
|
||||
if (!options.dryRun) {
|
||||
const { error } = 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: profileSummary,
|
||||
source_signals: sourceSignals,
|
||||
composed_at: composedAt,
|
||||
composer_model: composerModel,
|
||||
composer_version: 1,
|
||||
},
|
||||
{ onConflict: 'company_id' },
|
||||
)
|
||||
if (error) throw new Error(`Failed to upsert agent_profiles: ${error.message}`)
|
||||
}
|
||||
|
||||
if (!options.skipPrewarm && !options.dryRun) {
|
||||
// Resolve atom body paths from the registry and fire pre-warm.
|
||||
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)
|
||||
// Intentionally not awaited — pre-warm must not block the response.
|
||||
void preWarmAtomCache({ atomBodies: bodies })
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
companyId,
|
||||
horizontalAtoms: selection.horizontal_atoms,
|
||||
verticalAtoms: selection.vertical_atoms,
|
||||
modifierAtoms: selection.modifier_atoms,
|
||||
isMultiVertical: selection.is_multi_vertical,
|
||||
verificationQuestions: selection.verification_questions,
|
||||
uncertaintyNotes: selection.uncertainty_notes,
|
||||
profileSummary,
|
||||
sourceSignals,
|
||||
composerModel,
|
||||
composedAt,
|
||||
}
|
||||
}
|
||||
|
||||
export type { ComposedProfile } from './schemas'
|
||||
@@ -0,0 +1,405 @@
|
||||
import type { SupabaseClient } from '@supabase/supabase-js'
|
||||
import type { SourceSignals } from './schemas'
|
||||
|
||||
// Atom registry index row — the metadata-only shape the composer sees when
|
||||
// picking a loadout. We never send full atom bodies to the Opus call;
|
||||
// metadata is enough for selection.
|
||||
export interface AtomRegistryIndexRow {
|
||||
id: string
|
||||
tier: 'horizontal' | 'vertical' | 'modifier'
|
||||
title: string
|
||||
description: string
|
||||
sni_prefixes: string[]
|
||||
trigger_signals: Record<string, unknown>
|
||||
estimated_tokens: number
|
||||
version: number
|
||||
}
|
||||
|
||||
export async function loadAtomRegistryIndex(
|
||||
supabase: SupabaseClient,
|
||||
): Promise<AtomRegistryIndexRow[]> {
|
||||
const { data, error } = await supabase
|
||||
.from('agent_atom_registry')
|
||||
.select('id, tier, title, description, sni_prefixes, trigger_signals, estimated_tokens, version')
|
||||
.eq('is_active', true)
|
||||
.is('parent_atom_id', null) // top-level skills only; reference children are load-on-demand
|
||||
.order('id')
|
||||
if (error) throw new Error(`Failed to load agent_atom_registry: ${error.message}`)
|
||||
return (data ?? []) as AtomRegistryIndexRow[]
|
||||
}
|
||||
|
||||
export async function loadCompanyTicSnapshot(
|
||||
supabase: SupabaseClient,
|
||||
companyId: string,
|
||||
): Promise<{ snapshot: Record<string, unknown> | null; fetchedAt: string | null; name: string; entityType: string }> {
|
||||
const { data, error } = await supabase
|
||||
.from('companies')
|
||||
.select('name, entity_type, tic_snapshot, tic_snapshot_fetched_at')
|
||||
.eq('id', companyId)
|
||||
.single()
|
||||
if (error) throw new Error(`Failed to load company ${companyId}: ${error.message}`)
|
||||
return {
|
||||
snapshot: (data?.tic_snapshot as Record<string, unknown> | null) ?? null,
|
||||
fetchedAt: (data?.tic_snapshot_fetched_at as string | null) ?? null,
|
||||
name: data?.name ?? '',
|
||||
entityType: data?.entity_type ?? '',
|
||||
}
|
||||
}
|
||||
|
||||
// Known facts from `company_settings` — the onboarding form persists these
|
||||
// (moms_period, fiscal_year_start_month, f_skatt, employees, city). Composer
|
||||
// uses them as KNOWN inputs so it stops generating verification questions
|
||||
// about already-settled values.
|
||||
export interface CompanySettingsForComposer {
|
||||
city: string | null
|
||||
moms_period: string | null
|
||||
fiscal_year_start_month: number | null
|
||||
f_skatt: boolean | null
|
||||
vat_registered: boolean | null
|
||||
employee_count: number | null
|
||||
has_employees: boolean | null
|
||||
pays_salaries: boolean | null
|
||||
accounting_method: string | null
|
||||
}
|
||||
|
||||
export async function loadCompanySettings(
|
||||
supabase: SupabaseClient,
|
||||
companyId: string,
|
||||
): Promise<CompanySettingsForComposer | null> {
|
||||
const { data } = await supabase
|
||||
.from('company_settings')
|
||||
.select(
|
||||
'city, moms_period, fiscal_year_start_month, f_skatt, vat_registered, employee_count, has_employees, pays_salaries, accounting_method',
|
||||
)
|
||||
.eq('company_id', companyId)
|
||||
.maybeSingle()
|
||||
return (data ?? null) as CompanySettingsForComposer | null
|
||||
}
|
||||
|
||||
// Whether the currently-onboarding user is a confirmed director / signatory
|
||||
// at this company per BankID CompanyRoles. When true, the narrative is safe
|
||||
// to use second-person ownership voice ("Du driver…"); when false (manual-
|
||||
// orgnr signup, accountant-on-behalf-of, etc.) the narrative falls back to
|
||||
// neutral third-person ("Coredination AB är…") so we don't put words about
|
||||
// ownership in the user's mouth.
|
||||
//
|
||||
// We match the user's enrichment row against this company's org_number.
|
||||
// `companyId` is the gnubok UUID — we need to read the orgnr from
|
||||
// `companies` to do the match. Cheap (single SELECT each) and only runs once
|
||||
// per agent build.
|
||||
//
|
||||
// Director-like positions per Bolagsverket: 'ceo', 'boardMember', 'chairman',
|
||||
// 'externalSignatory'. Deputy positions ('deputyBoardMember') and external
|
||||
// auditors are intentionally excluded — they don't run the company day-to-day.
|
||||
const DIRECTOR_POSITION_TYPES = new Set([
|
||||
'ceo',
|
||||
'boardMember',
|
||||
'chairman',
|
||||
'externalSignatory',
|
||||
// Lowercase variants in case TIC normalises differently
|
||||
'CEO',
|
||||
'BoardMember',
|
||||
'Chairman',
|
||||
'ExternalSignatory',
|
||||
])
|
||||
|
||||
export async function loadUserDirectorship(
|
||||
supabase: SupabaseClient,
|
||||
companyId: string,
|
||||
): Promise<{ confirmedDirector: boolean }> {
|
||||
// Read this company's org_number — the BankID CompanyRoles row keys on
|
||||
// companyRegistrationNumber, not the gnubok company UUID.
|
||||
const { data: companyRow } = await supabase
|
||||
.from('companies')
|
||||
.select('org_number')
|
||||
.eq('id', companyId)
|
||||
.single()
|
||||
const orgNumber = (companyRow?.org_number as string | null)?.replace(/[\s-]/g, '')
|
||||
if (!orgNumber) return { confirmedDirector: false }
|
||||
|
||||
// Read the active user's enrichment row. Composer runs inside the user's
|
||||
// request context (RLS-scoped client), so .maybeSingle() only sees the row
|
||||
// for the authenticated user — no need to join through company_members.
|
||||
const { data: enrichmentRow } = await supabase
|
||||
.from('bankid_enrichment')
|
||||
.select('company_roles')
|
||||
.maybeSingle()
|
||||
const roles = (enrichmentRow?.company_roles ?? []) as Array<{
|
||||
companyRegistrationNumber?: string
|
||||
positionTypes?: string[]
|
||||
positionEnd?: string | null
|
||||
}>
|
||||
if (!Array.isArray(roles) || roles.length === 0) return { confirmedDirector: false }
|
||||
|
||||
const match = roles.find(
|
||||
(r) => r.companyRegistrationNumber?.replace(/[\s-]/g, '') === orgNumber,
|
||||
)
|
||||
if (!match) return { confirmedDirector: false }
|
||||
|
||||
// Position must be a director-type AND not already ended.
|
||||
const nowIso = new Date().toISOString()
|
||||
if (match.positionEnd && match.positionEnd < nowIso) return { confirmedDirector: false }
|
||||
const positions = match.positionTypes ?? []
|
||||
const isDirector = positions.some((p) => DIRECTOR_POSITION_TYPES.has(p))
|
||||
return { confirmedDirector: isDirector }
|
||||
}
|
||||
|
||||
interface SieSummary {
|
||||
top_accounts: { account: string; abs_amount: number }[]
|
||||
top_counterparties: { name: string; abs_amount: number }[]
|
||||
year_count: number
|
||||
}
|
||||
|
||||
// Build a coarse SIE summary from the most-recent imported SIE for the
|
||||
// company. Used by the composer as a verticality signal (e.g. a top-spend
|
||||
// account 1465 — alcohol inventory — strongly suggests restaurang).
|
||||
//
|
||||
// Returns null when no SIE has been imported. The composer must still work
|
||||
// without SIE data; TIC sniCodes carry most of the signal on their own.
|
||||
export async function loadSieSummary(
|
||||
supabase: SupabaseClient,
|
||||
companyId: string,
|
||||
): Promise<SieSummary | null> {
|
||||
const { data: imports, error: importsErr } = await supabase
|
||||
.from('sie_imports')
|
||||
.select('id, fiscal_year_start, fiscal_year_end')
|
||||
.eq('company_id', companyId)
|
||||
.eq('status', 'completed')
|
||||
.order('created_at', { ascending: false })
|
||||
.limit(20)
|
||||
if (importsErr) return null
|
||||
if (!imports || imports.length === 0) return null
|
||||
|
||||
// Fiscal-year span across all completed imports (approximate).
|
||||
const years = new Set(
|
||||
imports.map((r: { fiscal_year_start: string | null }) => {
|
||||
const v = r.fiscal_year_start
|
||||
return v ? v.slice(0, 4) : ''
|
||||
}),
|
||||
)
|
||||
years.delete('')
|
||||
|
||||
// Top-20 account magnitudes across journal_entry_lines for the company.
|
||||
// Cheaper than aggregating SIE line-items directly because the lines have
|
||||
// already landed in journal_entry_lines after import.
|
||||
const { data: lines, error: linesErr } = await supabase.rpc('agent_top_accounts_for_company', {
|
||||
p_company_id: companyId,
|
||||
p_limit: 20,
|
||||
})
|
||||
|
||||
// RPC is optional — if it doesn't exist yet, fall back to an inline group-by.
|
||||
// Either way, we tolerate missing data and return what we have.
|
||||
let topAccounts: { account: string; abs_amount: number }[] = []
|
||||
if (!linesErr && Array.isArray(lines)) {
|
||||
topAccounts = (lines as { account_number: string; abs_amount: number }[]).map((l) => ({
|
||||
account: l.account_number,
|
||||
abs_amount: Number(l.abs_amount) || 0,
|
||||
}))
|
||||
}
|
||||
|
||||
// Coarse counterparty rollup off the bank-statement description string.
|
||||
// `transactions.description` is the raw text from the bank — not
|
||||
// normalized — so this is a noisy signal. The composer treats it as a hint
|
||||
// alongside TIC sniCodes, which carry the strong industry signal.
|
||||
const { data: tx } = await supabase
|
||||
.from('transactions')
|
||||
.select('description, amount')
|
||||
.eq('company_id', companyId)
|
||||
.limit(2000)
|
||||
|
||||
const cpAgg = new Map<string, number>()
|
||||
if (Array.isArray(tx)) {
|
||||
for (const t of tx as { description: string | null; amount: number | string | null }[]) {
|
||||
const name = normalizeCounterparty(t.description)
|
||||
if (!name) continue
|
||||
const amt = Math.abs(Number(t.amount) || 0)
|
||||
cpAgg.set(name, (cpAgg.get(name) ?? 0) + amt)
|
||||
}
|
||||
}
|
||||
const topCounterparties = Array.from(cpAgg.entries())
|
||||
.sort((a, b) => b[1] - a[1])
|
||||
.slice(0, 10)
|
||||
.map(([name, abs_amount]) => ({ name, abs_amount }))
|
||||
|
||||
return {
|
||||
top_accounts: topAccounts,
|
||||
top_counterparties: topCounterparties,
|
||||
year_count: years.size,
|
||||
}
|
||||
}
|
||||
|
||||
interface BankingCounterparty {
|
||||
name: string
|
||||
abs_amount: number
|
||||
// 'in' → money coming in (income / refund / loan disbursement)
|
||||
// 'out' → money going out (cost / supplier payment / repayment)
|
||||
// 'mixed' → both directions present (rare — typically transfers or
|
||||
// returns). The composer should not assume a category from
|
||||
// mixed counterparties.
|
||||
direction: 'in' | 'out' | 'mixed'
|
||||
// True when at least one transaction for this counterparty still has
|
||||
// journal_entry_id IS NULL. Composer should only generate verification
|
||||
// questions about counterparties where this is true — the others are
|
||||
// already settled and re-asking wastes the user's time.
|
||||
has_unbooked: boolean
|
||||
}
|
||||
|
||||
interface BankingSummary {
|
||||
top_counterparties: BankingCounterparty[]
|
||||
monthly_volume: number | null
|
||||
unbooked_count: number
|
||||
}
|
||||
|
||||
// POC: re-use transactions table for banking counterparties. A first-class
|
||||
// Enable Banking summary lives behind the enable-banking extension and is
|
||||
// post-POC.
|
||||
//
|
||||
// Booking-aware: each rolled-up counterparty carries `direction` (sign of
|
||||
// the transactions) and `has_unbooked` (any row without journal_entry_id).
|
||||
// Both signals exist to keep the composer from asking dumb questions —
|
||||
// "is this a cost or an income?" when the sign is clearly negative,
|
||||
// "how should this be booked?" when there's no unbooked transaction left.
|
||||
export async function loadBankingSummary(
|
||||
supabase: SupabaseClient,
|
||||
companyId: string,
|
||||
): Promise<BankingSummary | null> {
|
||||
const { data, error } = await supabase
|
||||
.from('transactions')
|
||||
.select('description, amount, date, journal_entry_id')
|
||||
.eq('company_id', companyId)
|
||||
.gte('date', oneYearAgo())
|
||||
.order('date', { ascending: false })
|
||||
.limit(5000)
|
||||
if (error || !Array.isArray(data) || data.length === 0) return null
|
||||
|
||||
interface Bucket {
|
||||
absAmount: number
|
||||
hasInflow: boolean
|
||||
hasOutflow: boolean
|
||||
hasUnbooked: boolean
|
||||
}
|
||||
const cpAgg = new Map<string, Bucket>()
|
||||
let totalVolume = 0
|
||||
let unbookedCount = 0
|
||||
for (const t of data as {
|
||||
description: string | null
|
||||
amount: number | string | null
|
||||
journal_entry_id: string | null
|
||||
}[]) {
|
||||
const signedAmt = Number(t.amount) || 0
|
||||
const absAmt = Math.abs(signedAmt)
|
||||
totalVolume += absAmt
|
||||
if (!t.journal_entry_id) unbookedCount++
|
||||
|
||||
const name = normalizeCounterparty(t.description)
|
||||
if (!name) continue
|
||||
const prev = cpAgg.get(name) ?? {
|
||||
absAmount: 0,
|
||||
hasInflow: false,
|
||||
hasOutflow: false,
|
||||
hasUnbooked: false,
|
||||
}
|
||||
prev.absAmount += absAmt
|
||||
if (signedAmt > 0) prev.hasInflow = true
|
||||
if (signedAmt < 0) prev.hasOutflow = true
|
||||
if (!t.journal_entry_id) prev.hasUnbooked = true
|
||||
cpAgg.set(name, prev)
|
||||
}
|
||||
const top: BankingCounterparty[] = Array.from(cpAgg.entries())
|
||||
.sort((a, b) => b[1].absAmount - a[1].absAmount)
|
||||
.slice(0, 20)
|
||||
.map(([name, b]) => ({
|
||||
name,
|
||||
abs_amount: b.absAmount,
|
||||
direction: b.hasInflow && b.hasOutflow ? 'mixed' : b.hasInflow ? 'in' : 'out',
|
||||
has_unbooked: b.hasUnbooked,
|
||||
}))
|
||||
|
||||
return {
|
||||
top_counterparties: top,
|
||||
monthly_volume: totalVolume > 0 ? Math.round(totalVolume / 12) : null,
|
||||
unbooked_count: unbookedCount,
|
||||
}
|
||||
}
|
||||
|
||||
function oneYearAgo(): string {
|
||||
const d = new Date()
|
||||
d.setFullYear(d.getFullYear() - 1)
|
||||
return d.toISOString().slice(0, 10)
|
||||
}
|
||||
|
||||
// Cheap counterparty extraction from a bank-statement description. Strips
|
||||
// reference numbers, dates, and common suffixes; truncates to ~40 chars.
|
||||
// Post-POC: replace with the matcher in lib/transactions/.
|
||||
function normalizeCounterparty(raw: string | null | undefined): string | null {
|
||||
if (!raw) return null
|
||||
const cleaned = raw
|
||||
.replace(/\b\d{4,}\b/g, ' ') // strip long digit runs (refs)
|
||||
.replace(/[/*:|]/g, ' ')
|
||||
.replace(/\s+/g, ' ')
|
||||
.trim()
|
||||
.slice(0, 40)
|
||||
return cleaned.length >= 3 ? cleaned : null
|
||||
}
|
||||
|
||||
// Composite input for the Opus selection call.
|
||||
export interface ComposerInputs {
|
||||
companyId: string
|
||||
companyName: string
|
||||
entityType: string
|
||||
ticSnapshot: Record<string, unknown> | null
|
||||
ticFetchedAt: string | null
|
||||
companySettings: CompanySettingsForComposer | null
|
||||
sieSummary: SieSummary | null
|
||||
bankingSummary: BankingSummary | null
|
||||
atomIndex: AtomRegistryIndexRow[]
|
||||
// True when BankID CompanyRoles confirms the active user holds a
|
||||
// director-like position at this company. Controls whether the narrative
|
||||
// uses second-person ownership voice ("Du driver…") or neutral
|
||||
// third-person ("Coredination AB är…"). Default false so unknown users
|
||||
// never get the presumptive voice.
|
||||
userIsConfirmedDirector: boolean
|
||||
}
|
||||
|
||||
export async function gatherComposerInputs(
|
||||
supabase: SupabaseClient,
|
||||
companyId: string,
|
||||
): Promise<ComposerInputs> {
|
||||
const [
|
||||
{ snapshot, fetchedAt, name, entityType },
|
||||
atomIndex,
|
||||
sieSummary,
|
||||
bankingSummary,
|
||||
companySettings,
|
||||
directorship,
|
||||
] = await Promise.all([
|
||||
loadCompanyTicSnapshot(supabase, companyId),
|
||||
loadAtomRegistryIndex(supabase),
|
||||
loadSieSummary(supabase, companyId).catch(() => null),
|
||||
loadBankingSummary(supabase, companyId).catch(() => null),
|
||||
loadCompanySettings(supabase, companyId).catch(() => null),
|
||||
loadUserDirectorship(supabase, companyId).catch(() => ({ confirmedDirector: false })),
|
||||
])
|
||||
|
||||
return {
|
||||
companyId,
|
||||
companyName: name,
|
||||
entityType,
|
||||
ticSnapshot: snapshot,
|
||||
ticFetchedAt: fetchedAt,
|
||||
companySettings,
|
||||
sieSummary,
|
||||
bankingSummary,
|
||||
atomIndex,
|
||||
userIsConfirmedDirector: directorship.confirmedDirector,
|
||||
}
|
||||
}
|
||||
|
||||
export function inputsToSourceSignals(inputs: ComposerInputs): SourceSignals {
|
||||
return {
|
||||
tic: inputs.ticSnapshot,
|
||||
sie_summary: inputs.sieSummary,
|
||||
banking_summary: inputs.bankingSummary,
|
||||
atom_registry_version: inputs.atomIndex.reduce((acc, a) => acc + a.version, 0),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
import { getAnthropic, SONNET_MODEL } from './client'
|
||||
import type { AtomSelection } from './schemas'
|
||||
import type { ComposerInputs } from './inputs'
|
||||
|
||||
// Two voices: second-person ownership ("Du driver…") only when BankID
|
||||
// CompanyRoles has confirmed the active user holds a director-like position
|
||||
// at this company. Otherwise we use neutral third-person ("Coredination AB
|
||||
// är…") so manual-orgnr signups (accountant-on-behalf-of, employees,
|
||||
// family members setting up a parent's company) don't get a narrative
|
||||
// presuming they personally own the company. Same content, different voice.
|
||||
const SHARED_PROMPT_HEADER = `Du skriver en kort, saklig profil av ett företag. Profilen visas för användaren under rubriken "Profil" och används som bakgrund åt en bokföringsassistent. Den är INTE en hälsning och INTE ett chattmeddelande.
|
||||
|
||||
Stil:
|
||||
- Max 80 ord, två till tre meningar.
|
||||
- Saklig och konkret. Inga floskler, inga utropstecken, inga emoji, ingen avslutande fråga ("Stämmer det?").
|
||||
- Skriv ALDRIG i jag-form ("Jag ser att…"). Det är en beskrivning, inte assistenten som talar.
|
||||
- Använd ALDRIG tankstreck (— eller –). Använd kommatecken, punkt eller skriv "till" för intervall ("2,5 till 5 miljoner"). Hård regel.
|
||||
|
||||
Innehåll:
|
||||
1. Beskriv verksamheten med egna ord utifrån SNI-koder och verksamhetsbeskrivning: vad företaget gör, juridisk form och ägarbild om den är känd. Återge inte verksamhetsbeskrivningen ordagrant — den visas redan separat under "Verksamhet".
|
||||
2. Avsluta med en mening om vad assistenten är inställd på att hjälpa till med för den här typen av verksamhet, utifrån de valda specialiteterna.
|
||||
|
||||
Skriv endast själva profiltexten. Ingen rubrik, inga punktlistor.`
|
||||
|
||||
const VOICE_DIRECTOR = `\n\nRöst: Andra person, ägar-/ledningsperspektiv. "Du driver…", "Din verksamhet…". Användaren är verifierad styrelseledamot eller firmatecknare i bolaget.`
|
||||
|
||||
const VOICE_NEUTRAL = `\n\nRöst: Tredje person, neutral. "Coredination AB är…", "Bolaget bedriver…". Användaren kan vara ägare, anställd eller redovisningskonsult — vi vet inte. Skriv ALDRIG "Du driver…", "Din verksamhet…" eller andra formuleringar som antar att användaren själv äger eller leder bolaget. Använd företagsnamnet eller "Bolaget".`
|
||||
|
||||
function systemPromptFor(userIsConfirmedDirector: boolean): string {
|
||||
return SHARED_PROMPT_HEADER + (userIsConfirmedDirector ? VOICE_DIRECTOR : VOICE_NEUTRAL)
|
||||
}
|
||||
|
||||
export async function writeNarrative(
|
||||
inputs: ComposerInputs,
|
||||
selection: AtomSelection,
|
||||
): Promise<string> {
|
||||
const anthropic = getAnthropic()
|
||||
|
||||
const response = await anthropic.messages.create({
|
||||
model: SONNET_MODEL,
|
||||
max_tokens: 400,
|
||||
system: systemPromptFor(inputs.userIsConfirmedDirector),
|
||||
messages: [
|
||||
{
|
||||
role: 'user',
|
||||
content: buildUserPrompt(inputs, selection),
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
const text = response.content
|
||||
.filter((b) => b.type === 'text')
|
||||
.map((b) => (b as { type: 'text'; text: string }).text)
|
||||
.join('')
|
||||
.trim()
|
||||
|
||||
return text
|
||||
}
|
||||
|
||||
function buildUserPrompt(inputs: ComposerInputs, selection: AtomSelection): string {
|
||||
const lines: string[] = []
|
||||
lines.push(`Företag: ${inputs.companyName}`)
|
||||
lines.push(`Juridisk form: ${inputs.entityType}`)
|
||||
|
||||
if (inputs.ticSnapshot) {
|
||||
const tic = inputs.ticSnapshot as Record<string, unknown>
|
||||
const sni = (tic.sniCodes as { code: string; name: string }[] | undefined) ?? []
|
||||
if (sni.length > 0) {
|
||||
lines.push(`SNI: ${sni.map((s) => `${s.code} ${s.name}`).join('; ')}`)
|
||||
}
|
||||
if (typeof tic.purpose === 'string' && tic.purpose.trim().length > 0) {
|
||||
// Verksamhetsbeskrivning is the most important signal for the
|
||||
// confirming voice — pass it verbatim so the model can paraphrase.
|
||||
lines.push(`Verksamhetsbeskrivning (Bolagsverket): ${tic.purpose as string}`)
|
||||
}
|
||||
const reg = tic.registration as { fTax?: boolean; vat?: boolean; payroll?: boolean } | undefined
|
||||
if (reg) {
|
||||
const flags = [
|
||||
reg.fTax ? 'F-skatt' : null,
|
||||
reg.vat ? 'momsregistrerad' : null,
|
||||
reg.payroll ? 'arbetsgivare' : null,
|
||||
].filter(Boolean)
|
||||
if (flags.length > 0) lines.push(`Registreringar: ${flags.join(', ')}`)
|
||||
}
|
||||
if (tic.employeeRange) lines.push(`Anställda: ${tic.employeeRange as string}`)
|
||||
if (tic.turnoverRange) lines.push(`Omsättning: ${tic.turnoverRange as string}`)
|
||||
const owners = tic.beneficialOwners as
|
||||
| { name: string; extentDescription?: string | null }[]
|
||||
| undefined
|
||||
if (Array.isArray(owners) && owners.length > 0) {
|
||||
const names = owners.map((o) => o.name).join(', ')
|
||||
lines.push(
|
||||
`Verkliga huvudmän: ${names}${owners.length === 1 ? ' (ensam ägare)' : ''}`,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if (inputs.sieSummary && inputs.sieSummary.top_accounts.length > 0) {
|
||||
const top = inputs.sieSummary.top_accounts.slice(0, 5)
|
||||
lines.push(`Topp-konton i SIE: ${top.map((a) => a.account).join(', ')}`)
|
||||
}
|
||||
|
||||
lines.push('')
|
||||
lines.push(`Valda horizontals: ${selection.horizontal_atoms.join(', ') || '(inga)'}`)
|
||||
lines.push(`Valda verticals: ${selection.vertical_atoms.join(', ') || '(inga)'}`)
|
||||
lines.push(`Valda modifiers: ${selection.modifier_atoms.join(', ') || '(inga)'}`)
|
||||
|
||||
return lines.join('\n')
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
import { getAnthropic, SONNET_MODEL } from './client'
|
||||
|
||||
// Cache pre-warm after composition: fire a max_tokens: 1 request with the
|
||||
// assembled atom bodies so the Block 1 cache prefix lands warm before the
|
||||
// user's first chat turn. Best-effort — if this fails, the loop still works,
|
||||
// just with a cold first turn.
|
||||
//
|
||||
// Bodies come from the DB (agent_atom_registry.body), not disk — so pre-warm
|
||||
// no longer depends on .claude/skills being present at runtime. In dev before
|
||||
// `npm run skills:generate` has seeded bodies, the list is empty and pre-warm
|
||||
// simply no-ops (a cold first turn, which is acceptable for a dev convenience).
|
||||
//
|
||||
// Note: we use max_tokens: 1 (not 0). The Anthropic API requires at least
|
||||
// 1 output token. Pre-warm cost is dominated by input processing, so a
|
||||
// single output token is negligible.
|
||||
//
|
||||
// Plan ref: §6 (cache pre-warming), §10 (caching strategy).
|
||||
|
||||
export async function preWarmAtomCache(opts: {
|
||||
atomBodies: string[]
|
||||
ttl?: '5m' | '1h'
|
||||
}): Promise<void> {
|
||||
const { atomBodies, ttl = '1h' } = opts
|
||||
|
||||
const bodies = atomBodies.filter((b) => b && b.length > 0)
|
||||
if (bodies.length === 0) return
|
||||
|
||||
const anthropic = getAnthropic()
|
||||
try {
|
||||
await anthropic.messages.create({
|
||||
model: SONNET_MODEL,
|
||||
max_tokens: 1,
|
||||
system: [
|
||||
{
|
||||
type: 'text',
|
||||
text: bodies.join('\n\n---\n\n'),
|
||||
cache_control: { type: 'ephemeral', ttl },
|
||||
},
|
||||
],
|
||||
messages: [{ role: 'user', content: 'warmup' }],
|
||||
})
|
||||
} catch {
|
||||
// Fire-and-forget — pre-warm failure must never block the composer.
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
import { z } from 'zod'
|
||||
|
||||
// Atom selection output. The Opus call returns this via tool_use forcing —
|
||||
// the model is forced to invoke `compose_agent_profile(...)` once, which
|
||||
// gives us Zod-validatable structured output instead of free-text JSON.
|
||||
export const AtomSelectionSchema = z.object({
|
||||
horizontal_atoms: z
|
||||
.array(z.string())
|
||||
.describe('Atom IDs of horizontal regulatory skills to load (e.g. "horizontal/swedish-vat").'),
|
||||
vertical_atoms: z
|
||||
.array(z.string())
|
||||
.describe(
|
||||
'Atom IDs of industry/vertical atoms (e.g. "vertical/konsult-it"). Empty array if no vertical fits.',
|
||||
),
|
||||
modifier_atoms: z
|
||||
.array(z.string())
|
||||
.describe(
|
||||
'Atom IDs of cross-cutting modifier atoms (e.g. "modifier/single-shareholder-ab-fmb").',
|
||||
),
|
||||
is_multi_vertical: z
|
||||
.boolean()
|
||||
.describe('True when the company genuinely spans more than one industry.'),
|
||||
verification_questions: z
|
||||
.array(z.string())
|
||||
.describe(
|
||||
'Short Swedish questions the user must confirm during Phase B verification. Highest-leverage uncertainties only.',
|
||||
),
|
||||
uncertainty_notes: z
|
||||
.array(z.string())
|
||||
.describe('Free-form notes the composer wants to surface to a developer reviewing the selection.'),
|
||||
})
|
||||
|
||||
export type AtomSelection = z.infer<typeof AtomSelectionSchema>
|
||||
|
||||
// JSON-Schema for the tool_use forcing. Anthropic's API requires the tool
|
||||
// schema in plain JSON Schema (not Zod). Kept in sync with AtomSelectionSchema
|
||||
// by convention; the response is re-validated through Zod after parsing.
|
||||
// Typed as the SDK's InputSchema shape (mutable) so it satisfies the
|
||||
// Tool.input_schema parameter.
|
||||
export const ATOM_SELECTION_TOOL_SCHEMA: {
|
||||
type: 'object'
|
||||
properties: Record<string, unknown>
|
||||
required: string[]
|
||||
additionalProperties: boolean
|
||||
} = {
|
||||
type: 'object',
|
||||
properties: {
|
||||
horizontal_atoms: { type: 'array', items: { type: 'string' } },
|
||||
vertical_atoms: { type: 'array', items: { type: 'string' } },
|
||||
modifier_atoms: { type: 'array', items: { type: 'string' } },
|
||||
is_multi_vertical: { type: 'boolean' },
|
||||
verification_questions: { type: 'array', items: { type: 'string' } },
|
||||
uncertainty_notes: { type: 'array', items: { type: 'string' } },
|
||||
},
|
||||
required: [
|
||||
'horizontal_atoms',
|
||||
'vertical_atoms',
|
||||
'modifier_atoms',
|
||||
'is_multi_vertical',
|
||||
'verification_questions',
|
||||
'uncertainty_notes',
|
||||
],
|
||||
additionalProperties: false,
|
||||
}
|
||||
|
||||
// Source signals snapshot. Persisted in agent_profiles.source_signals so the
|
||||
// selection is reconstructable later even if upstream data (TIC, SIE) changes.
|
||||
export const SourceSignalsSchema = z.object({
|
||||
tic: z.record(z.string(), z.unknown()).nullable(),
|
||||
sie_summary: z
|
||||
.object({
|
||||
top_accounts: z.array(z.object({ account: z.string(), abs_amount: z.number() })),
|
||||
top_counterparties: z.array(z.object({ name: z.string(), abs_amount: z.number() })),
|
||||
year_count: z.number(),
|
||||
})
|
||||
.nullable(),
|
||||
banking_summary: z
|
||||
.object({
|
||||
top_counterparties: z.array(z.object({ name: z.string(), abs_amount: z.number() })),
|
||||
monthly_volume: z.number().nullable(),
|
||||
})
|
||||
.nullable(),
|
||||
atom_registry_version: z.number(),
|
||||
})
|
||||
|
||||
export type SourceSignals = z.infer<typeof SourceSignalsSchema>
|
||||
|
||||
// Output of the full composer pipeline.
|
||||
export interface ComposedProfile {
|
||||
companyId: string
|
||||
horizontalAtoms: string[]
|
||||
verticalAtoms: string[]
|
||||
modifierAtoms: string[]
|
||||
isMultiVertical: boolean
|
||||
verificationQuestions: string[]
|
||||
uncertaintyNotes: string[]
|
||||
profileSummary: string
|
||||
sourceSignals: SourceSignals
|
||||
composerModel: string
|
||||
composedAt: string
|
||||
}
|
||||
@@ -0,0 +1,184 @@
|
||||
import type { SupabaseClient } from '@supabase/supabase-js'
|
||||
import { createLogger } from '@/lib/logger'
|
||||
|
||||
const log = createLogger('agent.composer.tic-fetch')
|
||||
|
||||
// Live-fetch the TIC company profile via the existing extension HTTP route
|
||||
// and cache it on `companies.tic_snapshot`. Used by the agent onboarding
|
||||
// stream (Phase A step 1) and the /onboarding/agent server component so the
|
||||
// review card has SNI, verksamhetsbeskrivning, address, and recent financials
|
||||
// without requiring the user to have visited the TIC workspace beforehand.
|
||||
//
|
||||
// Why HTTP self-fetch rather than a direct import: core-build CI forbids
|
||||
// imports from @/extensions/ in lib/agent/*. Going through the extension's
|
||||
// public HTTP surface keeps the boundary intact and works the same in dev
|
||||
// and on Vercel. The TIC handler already accepts cookie-auth, so we just
|
||||
// forward the user's session cookie.
|
||||
//
|
||||
// Stale-cache policy: anything cached within the last 7 days is reused
|
||||
// verbatim. TIC data is slow-changing (sniCodes, registration, address
|
||||
// rarely flip) so this avoids re-hitting TIC on every page load.
|
||||
//
|
||||
// Rate budget: the /profile endpoint fans out to ~13 TIC (Lens) calls and
|
||||
// the account has a ~3000/mo ceiling. So we DON'T eagerly re-fetch every
|
||||
// pre-v2 (v1) snapshot — that would blow the budget across the customer
|
||||
// base. Instead, v1 snapshots upgrade to v2 lazily: only when a caller
|
||||
// that actually consumes the v2 sections passes `upgradeV1: true` (today
|
||||
// just the agent-onboarding paths, a deliberate once-per-company action).
|
||||
|
||||
const STALE_AFTER_MS = 7 * 24 * 60 * 60 * 1000 // 7 days
|
||||
// Default fetch timeout. The agent-onboarding callers override with a longer
|
||||
// budget (10s) since the user is on a wait-screen with visible progress and
|
||||
// the prior 5s default killed every signup-time fetch in May (~530 wasted
|
||||
// upstream Lens calls — the abort fired client-side but the upstream calls
|
||||
// kept running and counted against quota). Other callers (background jobs,
|
||||
// dev tooling) stay on the conservative default.
|
||||
const FETCH_TIMEOUT_MS = 5_000
|
||||
|
||||
export interface TicSnapshotResult {
|
||||
snapshot: Record<string, unknown> | null
|
||||
source: 'cached' | 'fetched' | 'fallback'
|
||||
}
|
||||
|
||||
// A snapshot written before the TIC v2 migration lacks the v2-only
|
||||
// `statuses` section. v2 always includes the key (possibly an empty
|
||||
// array), so its absence is a reliable "this is a v1 snapshot" signal.
|
||||
function isV1Snapshot(snapshot: Record<string, unknown> | null): boolean {
|
||||
return snapshot != null && !('statuses' in snapshot)
|
||||
}
|
||||
|
||||
export async function ensureTicSnapshot(opts: {
|
||||
supabase: SupabaseClient
|
||||
companyId: string
|
||||
cookieHeader: string
|
||||
// Origin to use for the internal self-fetch. The caller derives this from
|
||||
// the incoming request's host header so dev (localhost:3000), preview
|
||||
// (vercel.app), and production all reach their own instance of the TIC
|
||||
// route. Falls back to NEXT_PUBLIC_APP_URL when not supplied — fine for
|
||||
// background jobs but wrong for request-scoped paths because that env var
|
||||
// is the production canonical URL even in dev.
|
||||
origin?: string
|
||||
// When true, a cached snapshot still inside the 7-day window is
|
||||
// re-fetched if it's a pre-v2 (v1) shape. Gated to deliberate, bounded
|
||||
// callers (agent onboarding) so the v1→v2 upgrade doesn't fan out across
|
||||
// every company and exhaust the monthly TIC budget.
|
||||
upgradeV1?: boolean
|
||||
// Override the default 5s fetch timeout. Use when the caller has a UI
|
||||
// affordance for waiting (agent onboarding wait-screen) so legitimate
|
||||
// fetches don't get aborted before the ~13-call Lens fan-out completes —
|
||||
// which was the root cause of the May 2026 quota-burn incident.
|
||||
timeoutMs?: number
|
||||
}): Promise<TicSnapshotResult> {
|
||||
const {
|
||||
supabase,
|
||||
companyId,
|
||||
cookieHeader,
|
||||
origin,
|
||||
upgradeV1 = false,
|
||||
timeoutMs = FETCH_TIMEOUT_MS,
|
||||
} = opts
|
||||
|
||||
const { data: companyRow } = await supabase
|
||||
.from('companies')
|
||||
.select('org_number, tic_snapshot, tic_snapshot_fetched_at')
|
||||
.eq('id', companyId)
|
||||
.single()
|
||||
|
||||
if (!companyRow) return { snapshot: null, source: 'fallback' }
|
||||
|
||||
const cachedSnapshot = companyRow.tic_snapshot as Record<string, unknown> | null
|
||||
const needsV2Upgrade = upgradeV1 && isV1Snapshot(cachedSnapshot)
|
||||
|
||||
// Fresh cache hit — nothing to do. (Unless the caller needs v2 fields and
|
||||
// the cache is still v1, in which case we fall through to a refetch.)
|
||||
if (
|
||||
cachedSnapshot &&
|
||||
!isStale(companyRow.tic_snapshot_fetched_at as string | null) &&
|
||||
!needsV2Upgrade
|
||||
) {
|
||||
return { snapshot: cachedSnapshot, source: 'cached' }
|
||||
}
|
||||
|
||||
// Org number drifts: some onboarding flows persist it on company_settings
|
||||
// only (TicWorkspace reads from there). Prefer companies.org_number but
|
||||
// fall back to company_settings.org_number so existing companies aren't
|
||||
// permanently blocked from TIC enrichment.
|
||||
let orgNumber = (companyRow.org_number as string | null) ?? null
|
||||
if (!orgNumber) {
|
||||
const { data: settingsRow } = await supabase
|
||||
.from('company_settings')
|
||||
.select('org_number')
|
||||
.eq('company_id', companyId)
|
||||
.maybeSingle()
|
||||
orgNumber = (settingsRow?.org_number as string | null) ?? null
|
||||
}
|
||||
if (!orgNumber) {
|
||||
return { snapshot: (companyRow.tic_snapshot as Record<string, unknown> | null) ?? null, source: 'fallback' }
|
||||
}
|
||||
|
||||
const profile = await fetchTicProfile(orgNumber, cookieHeader, origin, timeoutMs)
|
||||
if (!profile) {
|
||||
// Fall through with whatever (possibly stale) snapshot we already have.
|
||||
return {
|
||||
snapshot: (companyRow.tic_snapshot as Record<string, unknown> | null) ?? null,
|
||||
source: 'fallback',
|
||||
}
|
||||
}
|
||||
|
||||
// Persist. Best-effort — if the update fails, we still return the profile
|
||||
// we just fetched so the current request can use it.
|
||||
const { error } = await supabase
|
||||
.from('companies')
|
||||
.update({
|
||||
tic_snapshot: profile,
|
||||
tic_snapshot_fetched_at: new Date().toISOString(),
|
||||
})
|
||||
.eq('id', companyId)
|
||||
if (error) {
|
||||
// Stale data is fine for the current request — but a silent write
|
||||
// failure means the next caller re-fetches TIC unnecessarily and the
|
||||
// monthly TIC budget bleeds. Surface it via the structured logger.
|
||||
log.warn('tic snapshot persist failed', { error: error.message, companyId })
|
||||
}
|
||||
|
||||
return { snapshot: profile, source: 'fetched' }
|
||||
}
|
||||
|
||||
function isStale(fetchedAt: string | null): boolean {
|
||||
if (!fetchedAt) return true
|
||||
const ts = Date.parse(fetchedAt)
|
||||
if (Number.isNaN(ts)) return true
|
||||
return Date.now() - ts > STALE_AFTER_MS
|
||||
}
|
||||
|
||||
async function fetchTicProfile(
|
||||
orgNumber: string,
|
||||
cookieHeader: string,
|
||||
origin: string | undefined,
|
||||
timeoutMs: number,
|
||||
): Promise<Record<string, unknown> | null> {
|
||||
const baseUrl = origin || process.env.NEXT_PUBLIC_APP_URL || 'http://localhost:3000'
|
||||
const url = `${baseUrl}/api/extensions/ext/tic/profile?org_number=${encodeURIComponent(orgNumber)}`
|
||||
|
||||
try {
|
||||
const res = await fetch(url, {
|
||||
headers: cookieHeader ? { cookie: cookieHeader } : undefined,
|
||||
signal: AbortSignal.timeout(timeoutMs),
|
||||
})
|
||||
if (!res.ok) {
|
||||
log.warn('tic profile non-ok', { url, status: res.status })
|
||||
return null
|
||||
}
|
||||
const body = (await res.json()) as { data?: Record<string, unknown> }
|
||||
return body.data ?? null
|
||||
} catch (err) {
|
||||
// Network error, timeout, TIC extension disabled, TIC API misconfigured.
|
||||
// Any of these is a normal fallback — return null so the caller can
|
||||
// degrade gracefully.
|
||||
log.warn('tic profile fetch failed', {
|
||||
url,
|
||||
error: err instanceof Error ? err.message : String(err),
|
||||
})
|
||||
return null
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { generalHelp } from '../general-help'
|
||||
|
||||
// general.help is the read-only /chat assistant. These guards lock in:
|
||||
// 1. no write tools reach this intent (structural read-only), and
|
||||
// 2. the prompt redirects categorization/bokföring to the per-transaction
|
||||
// flow instead of giving unactionable prose proposals + a fake "godkänner du?".
|
||||
// If a future edit reintroduces a write tool or softens the redirect, this fails.
|
||||
|
||||
const WRITE_TOOLS = [
|
||||
'gnubok_categorize_transaction',
|
||||
'gnubok_create_invoice',
|
||||
'gnubok_create_voucher',
|
||||
'gnubok_correct_entry',
|
||||
'gnubok_reverse_journal_entry',
|
||||
'gnubok_approve_supplier_invoice',
|
||||
'gnubok_mark_invoice_as_paid',
|
||||
'gnubok_run_year_end',
|
||||
'gnubok_match_transaction_to_invoice',
|
||||
]
|
||||
|
||||
function renderPrompt() {
|
||||
return generalHelp.promptTemplate({
|
||||
captured: { route: '/transactions' },
|
||||
profileSummary: null,
|
||||
activeMemory: [],
|
||||
})
|
||||
}
|
||||
|
||||
describe('general.help — the /chat read-only assistant', () => {
|
||||
it('exposes no write tools (so /chat cannot stage a booking)', () => {
|
||||
for (const t of WRITE_TOOLS) {
|
||||
expect(generalHelp.tools).not.toContain(t)
|
||||
}
|
||||
})
|
||||
|
||||
it('redirects categorization to the Dokumentinkorgen flow instead of proposing in prose', () => {
|
||||
const out = renderPrompt()
|
||||
// Per-transaction agent help now lives in Dokumentinkorgen (match the
|
||||
// underlag to the transaction, then ask there) — not a transactions-page row
|
||||
// button, which was removed.
|
||||
expect(out).toContain('Dokumentinkorgen')
|
||||
expect(out).toContain('matcha det mot transaktionen')
|
||||
// Must explicitly forbid per-transaction prose proposals + the fake "approve?" prompt.
|
||||
expect(out).toContain('INTE per-transaktions-bokföringsförslag')
|
||||
expect(out.toLowerCase()).toContain('godkänner du dessa')
|
||||
})
|
||||
|
||||
it('forbids fabricating that it staged anything', () => {
|
||||
expect(renderPrompt()).toMatch(/ALDRIG fabricera/i)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,112 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { routeToIntent } from '../route-mapping'
|
||||
|
||||
describe('routeToIntent', () => {
|
||||
it('falls back to general.help when pathname is null/undefined/empty', () => {
|
||||
for (const input of [null, undefined, '']) {
|
||||
const out = routeToIntent(input as string | null | undefined)
|
||||
expect(out.intentId).toBe('general.help')
|
||||
expect(out.labelSuffix).toBeNull()
|
||||
}
|
||||
})
|
||||
|
||||
it('routes the root and list pages to general.help', () => {
|
||||
for (const route of ['/', '/transactions', '/invoices', '/customers', '/reports']) {
|
||||
const out = routeToIntent(route)
|
||||
expect(out.intentId).toBe('general.help')
|
||||
expect(out.intentArgs.route).toBe(route)
|
||||
expect(out.labelSuffix).toBeNull()
|
||||
expect(out.contextRef).toBeUndefined()
|
||||
}
|
||||
})
|
||||
|
||||
it('routes /invoices/new to invoice.draft without an id', () => {
|
||||
const out = routeToIntent('/invoices/new')
|
||||
expect(out.intentId).toBe('invoice.draft')
|
||||
expect(out.intentArgs).toEqual({})
|
||||
expect(out.contextRef).toBeUndefined()
|
||||
expect(out.labelSuffix).toBe('om denna faktura')
|
||||
})
|
||||
|
||||
it('routes /invoices/[id] to invoice.draft with the id', () => {
|
||||
const out = routeToIntent('/invoices/abc-123')
|
||||
expect(out.intentId).toBe('invoice.draft')
|
||||
expect(out.intentArgs).toEqual({ invoice_id: 'abc-123' })
|
||||
expect(out.contextRef).toBe('invoice:abc-123')
|
||||
expect(out.labelSuffix).toBe('om denna faktura')
|
||||
})
|
||||
|
||||
it('routes /invoices/[id]/credit to invoice.draft with the parent id', () => {
|
||||
// The credit-note form is still an invoice context — same intent, same
|
||||
// captured entity. The :credit suffix isn't its own intent.
|
||||
const out = routeToIntent('/invoices/abc-123/credit')
|
||||
expect(out.intentId).toBe('invoice.draft')
|
||||
expect(out.intentArgs).toEqual({ invoice_id: 'abc-123' })
|
||||
expect(out.contextRef).toBe('invoice:abc-123')
|
||||
})
|
||||
|
||||
it('routes /supplier-invoices/[id] to supplier_invoice.review', () => {
|
||||
const out = routeToIntent('/supplier-invoices/sup-1')
|
||||
expect(out.intentId).toBe('supplier_invoice.review')
|
||||
expect(out.intentArgs).toEqual({ supplier_invoice_id: 'sup-1' })
|
||||
expect(out.contextRef).toBe('supplier_invoice:sup-1')
|
||||
expect(out.labelSuffix).toBe('om denna leverantörsfaktura')
|
||||
})
|
||||
|
||||
it('does NOT route /supplier-invoices/new to supplier_invoice.review (no entity yet)', () => {
|
||||
// There's no invoice to review yet — fall through so the Opus intent
|
||||
// doesn't fire on an empty capture.
|
||||
const out = routeToIntent('/supplier-invoices/new')
|
||||
expect(out.intentId).toBe('general.help')
|
||||
})
|
||||
|
||||
it('falls through /bookkeeping/[id] to general.help (FAB is suppressed on the verifikation editor)', () => {
|
||||
// The verifikation editor is a dense regulatory surface; AgentTrigger
|
||||
// hides the FAB on /bookkeeping/[id] entirely. routeToIntent still
|
||||
// returns a sensible default in case anything else queries it.
|
||||
const out = routeToIntent('/bookkeeping/je-7')
|
||||
expect(out.intentId).toBe('general.help')
|
||||
expect(out.intentArgs).toEqual({ route: '/bookkeeping/je-7' })
|
||||
expect(out.labelSuffix).toBeNull()
|
||||
expect(out.contextRef).toBeUndefined()
|
||||
})
|
||||
|
||||
it('routes /bookkeeping/year-end to bokslut.step (matches the page button — no two-agents-on-one-page)', () => {
|
||||
const out = routeToIntent('/bookkeeping/year-end')
|
||||
expect(out.intentId).toBe('bokslut.step')
|
||||
expect(out.intentArgs).toEqual({ step_id: null })
|
||||
expect(out.contextRef).toBe('bokslut:overview')
|
||||
expect(out.labelSuffix).toBe('om bokslutet')
|
||||
})
|
||||
|
||||
it('routes /kpi to kpi.explain (matches the page button)', () => {
|
||||
const out = routeToIntent('/kpi')
|
||||
expect(out.intentId).toBe('kpi.explain')
|
||||
expect(out.intentArgs).toEqual({ kpi_key: 'översikt' })
|
||||
expect(out.contextRef).toBe('kpi:översikt')
|
||||
expect(out.labelSuffix).toBe('om nyckeltalen')
|
||||
})
|
||||
|
||||
it('does NOT route bare /bookkeeping list page to verifikation.draft', () => {
|
||||
const out = routeToIntent('/bookkeeping')
|
||||
expect(out.intentId).toBe('general.help')
|
||||
})
|
||||
|
||||
it('routes /settings/<panel> to settings.help with the panel slug', () => {
|
||||
const out = routeToIntent('/settings/invoicing')
|
||||
expect(out.intentId).toBe('settings.help')
|
||||
expect(out.intentArgs).toEqual({ panel: 'invoicing' })
|
||||
expect(out.labelSuffix).toBeNull()
|
||||
})
|
||||
|
||||
it('takes the first segment after /settings as the panel slug (ignores deeper paths)', () => {
|
||||
const out = routeToIntent('/settings/invoicing/templates/new')
|
||||
expect(out.intentId).toBe('settings.help')
|
||||
expect(out.intentArgs).toEqual({ panel: 'invoicing' })
|
||||
})
|
||||
|
||||
it('routes bare /settings without a panel slug to general.help', () => {
|
||||
const out = routeToIntent('/settings')
|
||||
expect(out.intentId).toBe('general.help')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,42 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { renderAgentGroundRules, AGENT_GROUND_RULES } from '../shared-rules'
|
||||
|
||||
// AGENT_GROUND_RULES is rendered into the first user message of the bookkeeping
|
||||
// intents that inject it (general.help, vat-review, invoice-draft,
|
||||
// supplier_invoice.review, bokslut.step, verifikation.draft). It owns the
|
||||
// bookkeeping-specific HEURISTICS: underlag-first, no BAS numbers in chat,
|
||||
// counterparty history, representation, known-counterparty defaults.
|
||||
//
|
||||
// The cross-cutting EPISTEMICS rules (load before quoting a rate; don't infer
|
||||
// the business from an SNI code) deliberately do NOT live here anymore. They
|
||||
// live exactly once, in the always-on system prompt (buildIdentityBlock — see
|
||||
// system-prompt.test.ts), which is re-sent every turn in the high-salience
|
||||
// system position. This file guards both: that the heuristics stay, and that
|
||||
// the epistemics are not re-duplicated back into the first user message.
|
||||
|
||||
const text = renderAgentGroundRules()
|
||||
|
||||
describe('agent ground rules — bookkeeping heuristics it owns', () => {
|
||||
it('keeps underlag-first, no-BAS-in-chat, history, representation, known counterparties', () => {
|
||||
expect(text).toContain('UNDERLAG FÖRST')
|
||||
expect(text).toContain('INGA BAS-KONTONUMMER')
|
||||
expect(text).toContain('KOLLA HISTORIK FÖRST')
|
||||
expect(text).toContain('REPRESENTATION')
|
||||
expect(text).toContain('KÄNDA MOTPARTER')
|
||||
})
|
||||
|
||||
it('still renders as a non-trivial joined block', () => {
|
||||
expect(AGENT_GROUND_RULES.length).toBeGreaterThan(10)
|
||||
expect(text.split('\n').length).toBeGreaterThan(10)
|
||||
})
|
||||
})
|
||||
|
||||
describe('agent ground rules — epistemics live in the system prompt, not here', () => {
|
||||
it('does not re-duplicate the always-on epistemics / anti-speculation rules', () => {
|
||||
// These moved to buildIdentityBlock (always-on Block 2). Re-adding them here
|
||||
// restores the triplication this cleanup removed.
|
||||
expect(text).not.toContain('12 %→6 %')
|
||||
expect(text).not.toContain('GISSA INTE BOLAGETS VERKSAMHET')
|
||||
expect(text).not.toContain('är du säker?')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,185 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { transactionCategorization } from '../transaction-categorization'
|
||||
|
||||
// Locks in the prose-drift fix from /Users/jakobwennberg/.claude/plans/.
|
||||
// The promptTemplate must instruct the agent to narrate using CATEGORY
|
||||
// LABELS, never four-digit BAS account numbers. If a future edit
|
||||
// reintroduces a "föreslå BAS-konto"-style instruction, this test fails.
|
||||
|
||||
const TX_ID = '11111111-1111-1111-1111-111111111111'
|
||||
|
||||
function stripUuidsAndDates(text: string): string {
|
||||
return text
|
||||
// RFC 4122 UUIDs
|
||||
.replace(/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/gi, '')
|
||||
// ISO dates yyyy-MM-dd
|
||||
.replace(/\d{4}-\d{2}-\d{2}/g, '')
|
||||
// Swedish law references — "ML 2023:200", "BFNAR 2013:2", etc. The year
|
||||
// half can collide with the BAS class-2 range, so strip the whole
|
||||
// reference before scanning for stray BAS numbers.
|
||||
.replace(/\b\d{4}:\d{1,3}\b/g, '')
|
||||
}
|
||||
|
||||
function renderPrompt(opts: {
|
||||
hasUnderlag: boolean
|
||||
profileSummary?: string | null
|
||||
}) {
|
||||
const captured = {
|
||||
transaction: {
|
||||
id: TX_ID,
|
||||
date: '2026-05-12',
|
||||
description: 'Supabase Pte. Ltd.',
|
||||
amount: -810,
|
||||
currency: 'USD',
|
||||
counterparty_name: null,
|
||||
},
|
||||
underlag: opts.hasUnderlag
|
||||
? [
|
||||
{
|
||||
kind: 'receipt' as const,
|
||||
document_id: 'doc-1',
|
||||
merchant_name: 'Supabase Pte. Ltd.',
|
||||
receipt_date: '2026-05-12',
|
||||
total_amount: 810,
|
||||
vat_amount: 0,
|
||||
currency: 'USD',
|
||||
is_restaurant: null,
|
||||
is_systembolaget: null,
|
||||
raw_extraction: null,
|
||||
},
|
||||
]
|
||||
: [],
|
||||
}
|
||||
return transactionCategorization.promptTemplate({
|
||||
captured,
|
||||
profileSummary: opts.profileSummary ?? null,
|
||||
activeMemory: [],
|
||||
})
|
||||
}
|
||||
|
||||
describe('transaction.categorization prompt template', () => {
|
||||
it('includes the transaction UUID', () => {
|
||||
const out = renderPrompt({ hasUnderlag: true })
|
||||
expect(out).toContain(`transaction_id: ${TX_ID}`)
|
||||
})
|
||||
|
||||
it('instructs the agent to narrate with category names, not BAS numbers', () => {
|
||||
const out = renderPrompt({ hasUnderlag: true })
|
||||
expect(out).toContain('kategori-namn')
|
||||
expect(out).toContain('ALDRIG ett BAS-kontonummer')
|
||||
})
|
||||
|
||||
it('does not embed a four-digit BAS account number in the prompt body', () => {
|
||||
// Bare four-digit BAS like "5420", "6540", "1930" must not appear in the
|
||||
// prompt — only category labels. Strip UUIDs and ISO dates first so
|
||||
// their digit fragments don't trigger false positives.
|
||||
const out = renderPrompt({ hasUnderlag: true })
|
||||
const stripped = stripUuidsAndDates(out)
|
||||
const bareFourDigits = stripped.match(/\b[12345678]\d{3}\b/g) ?? []
|
||||
expect(bareFourDigits).toEqual([])
|
||||
})
|
||||
|
||||
it('still tells the agent to use category labels even with no underlag', () => {
|
||||
const out = renderPrompt({ hasUnderlag: false })
|
||||
const stripped = stripUuidsAndDates(out)
|
||||
const bareFourDigits = stripped.match(/\b[12345678]\d{3}\b/g) ?? []
|
||||
expect(bareFourDigits).toEqual([])
|
||||
})
|
||||
|
||||
it('instructs the agent to use the enum from the tool schema', () => {
|
||||
const out = renderPrompt({ hasUnderlag: true })
|
||||
expect(out).toContain('Välj kategori från enum-listan')
|
||||
})
|
||||
|
||||
it('instructs the agent to ask follow-up questions when the underlag is unclear', () => {
|
||||
const out = renderPrompt({ hasUnderlag: true })
|
||||
expect(out).toContain('FRÅGA användaren först')
|
||||
expect(out).toContain('oklart eller motsägelsefullt')
|
||||
})
|
||||
|
||||
it('instructs the agent to ask about purpose for context-dependent categories', () => {
|
||||
// Even when extraction is complete, classification depends on context
|
||||
// the human knows: restaurant rep vs intern måltid, Systembolaget rep
|
||||
// vs gift, ICA office vs private, etc. The prompt must lock in a
|
||||
// mandatory follow-up question for these categories.
|
||||
const out = renderPrompt({ hasUnderlag: true })
|
||||
expect(out.toLowerCase()).toContain('systembolaget')
|
||||
expect(out.toLowerCase()).toContain('restaurang')
|
||||
expect(out).toContain('STÄLL en kort följdfråga')
|
||||
expect(out.toLowerCase()).toContain('hellre en fråga än en felaktig bokning')
|
||||
})
|
||||
|
||||
it('points at the loaded atoms as primary source, without re-stating the system-prompt epistemics', () => {
|
||||
// Compliance content lives in the loaded atoms (swedish-vat,
|
||||
// swedish-accounting-compliance, ...). The intent prompt points at them as
|
||||
// the primary source to cite from…
|
||||
const out = renderPrompt({ hasUnderlag: true })
|
||||
expect(out.toLowerCase()).toContain('atomerna')
|
||||
expect(out).toMatch(/swedish-(vat|accounting-compliance|invoice-compliance)/)
|
||||
// …but it must NOT re-duplicate the load-before-answer epistemics rule. That
|
||||
// rule now lives once in the always-on system prompt (Block 2); re-adding it
|
||||
// here restores the triplication this cleanup removed.
|
||||
expect(out).not.toContain('12 %→6 %')
|
||||
expect(out.toLowerCase()).not.toContain('träningsdata')
|
||||
})
|
||||
|
||||
it('instructs the agent to check counterparty history before proposing', () => {
|
||||
// Past bookings for the same counterparty are a stronger signal than
|
||||
// the LLM's guess. Lock in the tool call. We query the actual journal
|
||||
// (gnubok_query_journal) rather than the lossy categorization_templates
|
||||
// summary, so the agent sees full verifikat — accounts, VAT, line text.
|
||||
const out = renderPrompt({ hasUnderlag: true })
|
||||
expect(out).toContain('gnubok_query_journal')
|
||||
expect(out.toLowerCase()).toContain('så har du gjort förut')
|
||||
})
|
||||
|
||||
it('instructs the agent to persist user answers via memory tool', () => {
|
||||
// Follow-up answers (rep vs intern, kund vs anställd) should be saved
|
||||
// so the agent doesn't re-ask next time a similar counterparty
|
||||
// appears.
|
||||
const out = renderPrompt({ hasUnderlag: true })
|
||||
expect(out).toContain('gnubok_remember_fact')
|
||||
})
|
||||
|
||||
it('forbids redundant staging narration that the ApprovalCard already shows', () => {
|
||||
// The card renders directly below the agent's response with the risk,
|
||||
// category, BAS account, VAT lines, and Godkänn/Avslå buttons. Echoing
|
||||
// "stagear nu" / "stageat" / "godkänn i appen" duplicates the card and
|
||||
// leaves cramped run-on text. Lock the no-narration rule in.
|
||||
const out = renderPrompt({ hasUnderlag: true })
|
||||
// The prompt names the phrases the agent must avoid: "stagear nu",
|
||||
// "godkänna i appen". It also tells the agent to skip repeating the
|
||||
// card's contents.
|
||||
expect(out.toLowerCase()).toContain('stagear nu')
|
||||
expect(out.toLowerCase()).toContain('godkänna i appen')
|
||||
expect(out.toLowerCase()).toContain('godkännandekortet')
|
||||
expect(out).toMatch(/[Bb]erätta INTE för användaren/)
|
||||
})
|
||||
|
||||
it('directs the user to Dokumentinkorgen when underlag is missing', () => {
|
||||
// The chat sheet no longer accepts file uploads. The agent must not
|
||||
// tell users to "drop the file in chat" or "click the paperclip" —
|
||||
// those affordances were removed in v5. Documents go through the
|
||||
// Dokumentinkorgen workspace.
|
||||
const out = renderPrompt({ hasUnderlag: false })
|
||||
expect(out).toContain('Dokumentinkorgen')
|
||||
expect(out).toContain('Matcha mot transaktion')
|
||||
expect(out.toLowerCase()).not.toContain('gem-ikon')
|
||||
expect(out.toLowerCase()).not.toContain('släpp filen här i chattfönstret')
|
||||
expect(out.toLowerCase()).not.toContain('chattfönstret')
|
||||
})
|
||||
|
||||
it('routes post-booking underlag to the verifikation, not back to Dokumentinkorgen', () => {
|
||||
// After a verifikation has been staged, the user must not be sent back
|
||||
// to Dokumentinkorgen — the doc belongs ON the verifikation (under
|
||||
// Bokföring). Inbox is for unbooked documents only.
|
||||
const noUnderlag = renderPrompt({ hasUnderlag: false })
|
||||
expect(noUnderlag).toContain('bifogas TILL VERIFIKATIONEN')
|
||||
expect(noUnderlag).toContain('öppna verifikationen i Bokföring')
|
||||
// The "Arbetssätt" rule that forbids repeating the upload reminder
|
||||
// after staging must also exist.
|
||||
const withUnderlag = renderPrompt({ hasUnderlag: true })
|
||||
expect(withUnderlag).toContain('Upprepa INTE underlag-uppmaningen efter stagning')
|
||||
expect(withUnderlag).toMatch(/bifogas till\s+VERIFIKATIONEN/)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,130 @@
|
||||
import { defineAgentIntent } from './types'
|
||||
import { OPUS_MODEL } from '@/lib/agent/composer/client'
|
||||
import { renderAgentGroundRules } from './shared-rules'
|
||||
|
||||
// bokslut.step — "Fråga [namn]" inside the year-end (bokslut) wizard.
|
||||
//
|
||||
// Bokslut is where users feel the most stress: many decisions (periodisering,
|
||||
// avskrivningar, dispositioner, tax provision), each with K2/K3 implications,
|
||||
// and irreversible once locked. The agent explains the current step, the
|
||||
// state, and what's recommended given the company's signals.
|
||||
//
|
||||
// Declarative atoms: year-end-closing + financial-reporting + tax-planning +
|
||||
// asset-accounting. Heavy load by design; this is when the user wants the
|
||||
// full reasoning depth.
|
||||
//
|
||||
// Opus per plan §8 V1 #6 — multi-step reasoning across rules + balances.
|
||||
|
||||
interface BokslutStepArgs {
|
||||
// The bokslut wizard's step id, e.g. 'accruals', 'depreciation',
|
||||
// 'dispositioner', 'tax-provision', 'arsredovisning'. Empty = overview.
|
||||
step_id?: string | null
|
||||
fiscal_year_end?: string | null
|
||||
}
|
||||
|
||||
interface CapturedBokslutStep {
|
||||
step_id: string | null
|
||||
fiscal_period: {
|
||||
id: string | null
|
||||
period_start: string | null
|
||||
period_end: string | null
|
||||
status: string | null
|
||||
} | null
|
||||
entity_type: string | null
|
||||
}
|
||||
|
||||
export const bokslutStep = defineAgentIntent<BokslutStepArgs, CapturedBokslutStep>({
|
||||
id: 'bokslut.step',
|
||||
buttonLabel: 'Fråga om detta steg',
|
||||
sheetTitle: 'Hjälp med bokslut',
|
||||
|
||||
atoms: {
|
||||
mode: 'declarative',
|
||||
horizontal: [
|
||||
'swedish-year-end-closing',
|
||||
'swedish-financial-reporting',
|
||||
'swedish-tax-planning',
|
||||
'swedish-asset-accounting',
|
||||
'swedish-accounting-compliance',
|
||||
],
|
||||
includeCompanyVertical: true,
|
||||
includeCompanyModifiers: true,
|
||||
},
|
||||
|
||||
tools: [
|
||||
'gnubok_year_end_readiness',
|
||||
'gnubok_propose_accruals',
|
||||
'gnubok_propose_annual_depreciation',
|
||||
'gnubok_propose_dispositioner',
|
||||
'gnubok_preview_arsredovisning',
|
||||
'gnubok_preview_ef_declaration',
|
||||
'gnubok_get_trial_balance',
|
||||
'gnubok_get_balance_sheet',
|
||||
'gnubok_get_income_statement',
|
||||
'gnubok_load_skill',
|
||||
'gnubok_search_tools',
|
||||
'gnubok_remember_fact',
|
||||
'gnubok_forget_fact',
|
||||
],
|
||||
|
||||
model: OPUS_MODEL,
|
||||
|
||||
capture: async ({ step_id, fiscal_year_end }, { supabase, companyId }) => {
|
||||
// Find the latest non-locked fiscal period (the one being closed) — or
|
||||
// the one matching fiscal_year_end if supplied.
|
||||
let query = supabase
|
||||
.from('fiscal_periods')
|
||||
.select('id, period_start, period_end, status')
|
||||
.eq('company_id', companyId)
|
||||
if (fiscal_year_end) query = query.eq('period_end', fiscal_year_end)
|
||||
query = query.order('period_end', { ascending: false }).limit(1)
|
||||
const { data: period } = await query.maybeSingle()
|
||||
|
||||
const { data: company } = await supabase
|
||||
.from('companies')
|
||||
.select('entity_type')
|
||||
.eq('id', companyId)
|
||||
.maybeSingle()
|
||||
|
||||
return {
|
||||
step_id: step_id ?? null,
|
||||
fiscal_period: period
|
||||
? {
|
||||
id: (period as { id: string }).id,
|
||||
period_start: ((period as { period_start?: string | null }).period_start) ?? null,
|
||||
period_end: ((period as { period_end?: string | null }).period_end) ?? null,
|
||||
status: ((period as { status?: string | null }).status) ?? null,
|
||||
}
|
||||
: null,
|
||||
entity_type: ((company as { entity_type?: string | null } | null)?.entity_type) ?? null,
|
||||
}
|
||||
},
|
||||
|
||||
promptTemplate: ({ captured, profileSummary }) => {
|
||||
const lines: string[] = []
|
||||
if (profileSummary) lines.push(`Företagets profil: ${profileSummary}`, '')
|
||||
|
||||
lines.push('Användaren är i bokslutsguiden och behöver hjälp.')
|
||||
if (captured.step_id) lines.push(`Aktivt steg: ${captured.step_id}`)
|
||||
if (captured.fiscal_period) {
|
||||
lines.push(
|
||||
`Räkenskapsår: ${captured.fiscal_period.period_start ?? '?'} → ${captured.fiscal_period.period_end ?? '?'} (status: ${captured.fiscal_period.status ?? '?'})`,
|
||||
)
|
||||
}
|
||||
if (captured.entity_type) lines.push(`Företagsform: ${captured.entity_type}`)
|
||||
lines.push('')
|
||||
lines.push(renderAgentGroundRules())
|
||||
lines.push('')
|
||||
lines.push('Arbetssätt — hjälp användaren genom STEGET de står i:')
|
||||
lines.push('1. Kör gnubok_year_end_readiness för att se vad som saknas.')
|
||||
lines.push('2. Om steget är "accruals": använd gnubok_propose_accruals för periodiseringar och förklara varje förslag (när påverkar det BR/RR, varför detta belopp?).')
|
||||
lines.push('3. Om steget är "depreciation": gnubok_propose_annual_depreciation. Förklara planenlig vs. överavskrivning, K2 schablonregler vs. K3 individual.')
|
||||
lines.push('4. Om steget är "dispositioner": gnubok_propose_dispositioner. Periodiseringsfond, koncernbidrag (om holding), årets skatt.')
|
||||
lines.push('5. Om steget är "arsredovisning": preview via gnubok_preview_arsredovisning, granska noter, förvaltningsberättelse, underskrifter, deadline.')
|
||||
lines.push('6. Om EF: använd gnubok_preview_ef_declaration. Räntefördelning, expansionsfond, NE-bilaga.')
|
||||
lines.push('')
|
||||
lines.push('Var BFL-rigorös — bokslut är irreversibelt när det låses. Peka på risker innan du föreslår staging av en operation.')
|
||||
lines.push('Svara på svenska. Ditt första svar är det första användaren ser — gå rakt på sak.')
|
||||
return lines.join('\n')
|
||||
},
|
||||
})
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,248 @@
|
||||
import { defineAgentIntent } from './types'
|
||||
import { SONNET_MODEL, THINKING_BUDGET_STANDARD } from '@/lib/agent/composer/client'
|
||||
import { renderAgentGroundRules } from './shared-rules'
|
||||
|
||||
// invoice.draft — "Fråga om denna faktura" from the invoice form.
|
||||
//
|
||||
// Declarative atom mode: loads VAT + invoice compliance + e-invoicing
|
||||
// upfront, plus the company's vertical + modifier atoms. The agent helps
|
||||
// with VAT treatment (25/12/6 % or reverse charge), payment terms,
|
||||
// kreditfaktura mechanics, OCR/Bankgiro on the invoice, and EU-customer
|
||||
// edge cases.
|
||||
//
|
||||
// The user does the actual drafting in the form; this intent advises.
|
||||
// gnubok_create_invoice / send_invoice are NOT in the tool list because
|
||||
// the form already submits to those endpoints — the agent shouldn't race
|
||||
// the form.
|
||||
//
|
||||
// Plan ref: dev_docs/specialized-agent-plan.md §8 (V1 intent #2).
|
||||
|
||||
interface InvoiceDraftArgs {
|
||||
// null when the user opened the agent before picking a customer.
|
||||
customer_id?: string | null
|
||||
// Set when editing an existing draft (route /invoices/[id]). null for new.
|
||||
invoice_id?: string | null
|
||||
}
|
||||
|
||||
interface CapturedInvoiceDraft {
|
||||
customer: {
|
||||
id: string
|
||||
name: string | null
|
||||
customer_type: string | null
|
||||
country: string | null
|
||||
vat_number: string | null
|
||||
vat_number_validated: boolean | null
|
||||
org_number: string | null
|
||||
} | null
|
||||
recent_invoices: {
|
||||
invoice_number: string | null
|
||||
invoice_date: string | null
|
||||
status: string | null
|
||||
total: number | null
|
||||
currency: string | null
|
||||
}[]
|
||||
invoice: {
|
||||
id: string
|
||||
invoice_number: string | null
|
||||
status: string | null
|
||||
total: number | null
|
||||
currency: string | null
|
||||
} | null
|
||||
// Compact subset of company_settings relevant to invoice drafting.
|
||||
company_invoice_context: {
|
||||
moms_period: string | null
|
||||
vat_registered: boolean | null
|
||||
accounting_method: string | null
|
||||
invoice_default_days: number | null
|
||||
} | null
|
||||
}
|
||||
|
||||
export const invoiceDraft = defineAgentIntent<InvoiceDraftArgs, CapturedInvoiceDraft>({
|
||||
id: 'invoice.draft',
|
||||
buttonLabel: 'Fråga om denna faktura',
|
||||
sheetTitle: 'Hjälp med faktura',
|
||||
|
||||
atoms: {
|
||||
mode: 'declarative',
|
||||
horizontal: ['swedish-vat', 'swedish-invoice-compliance', 'swedish-e-invoicing'],
|
||||
includeCompanyVertical: true,
|
||||
includeCompanyModifiers: true,
|
||||
},
|
||||
|
||||
tools: [
|
||||
'gnubok_list_customers',
|
||||
'gnubok_create_customer',
|
||||
'gnubok_load_skill',
|
||||
'gnubok_search_tools',
|
||||
'gnubok_remember_fact',
|
||||
'gnubok_forget_fact',
|
||||
],
|
||||
|
||||
model: SONNET_MODEL,
|
||||
|
||||
// Draft the invoice lines + VAT in the thinking channel, so the visible reply
|
||||
// is one short confirmation after staging rather than a play-by-play that
|
||||
// repeats once before the tool call and once after it.
|
||||
thinking: { budgetTokens: THINKING_BUDGET_STANDARD },
|
||||
|
||||
capture: async ({ customer_id, invoice_id }, { supabase, companyId }) => {
|
||||
// Resolve the effective customer_id. When the FAB lands here from
|
||||
// /invoices/[id] it only knows invoice_id — read customer_id off the
|
||||
// invoice row so the customer section of the prompt isn't empty.
|
||||
type InvoiceRow = {
|
||||
id: string
|
||||
invoice_number?: string | null
|
||||
status?: string | null
|
||||
total?: number | null
|
||||
currency?: string | null
|
||||
customer_id?: string | null
|
||||
}
|
||||
let invoice: InvoiceRow | null = null
|
||||
if (invoice_id) {
|
||||
const { data } = await supabase
|
||||
.from('invoices')
|
||||
.select('id, invoice_number, status, total, currency, customer_id')
|
||||
.eq('id', invoice_id)
|
||||
.eq('company_id', companyId)
|
||||
.maybeSingle()
|
||||
invoice = (data as InvoiceRow | null) ?? null
|
||||
}
|
||||
const effectiveCustomerId = customer_id ?? invoice?.customer_id ?? null
|
||||
|
||||
const [{ data: customer }, { data: recent }, { data: settings }] = await Promise.all([
|
||||
effectiveCustomerId
|
||||
? supabase
|
||||
.from('customers')
|
||||
.select('id, name, customer_type, country, vat_number, vat_number_validated, org_number')
|
||||
.eq('id', effectiveCustomerId)
|
||||
.eq('company_id', companyId)
|
||||
.maybeSingle()
|
||||
: Promise.resolve({ data: null }),
|
||||
effectiveCustomerId
|
||||
? supabase
|
||||
.from('invoices')
|
||||
.select('invoice_number, invoice_date, status, total, currency')
|
||||
.eq('customer_id', effectiveCustomerId)
|
||||
.eq('company_id', companyId)
|
||||
.order('invoice_date', { ascending: false })
|
||||
.limit(5)
|
||||
: Promise.resolve({ data: [] }),
|
||||
supabase
|
||||
.from('company_settings')
|
||||
.select('moms_period, vat_registered, accounting_method, invoice_default_days')
|
||||
.eq('company_id', companyId)
|
||||
.maybeSingle(),
|
||||
])
|
||||
|
||||
return {
|
||||
customer: customer
|
||||
? {
|
||||
id: (customer as { id: string }).id,
|
||||
name: ((customer as { name?: string | null }).name) ?? null,
|
||||
customer_type: ((customer as { customer_type?: string | null }).customer_type) ?? null,
|
||||
country: ((customer as { country?: string | null }).country) ?? null,
|
||||
vat_number: ((customer as { vat_number?: string | null }).vat_number) ?? null,
|
||||
vat_number_validated:
|
||||
((customer as { vat_number_validated?: boolean | null }).vat_number_validated) ?? null,
|
||||
org_number: ((customer as { org_number?: string | null }).org_number) ?? null,
|
||||
}
|
||||
: null,
|
||||
recent_invoices: ((recent ?? []) as {
|
||||
invoice_number: string | null
|
||||
invoice_date: string | null
|
||||
status: string | null
|
||||
total: number | null
|
||||
currency: string | null
|
||||
}[]).map((r) => ({
|
||||
invoice_number: r.invoice_number,
|
||||
invoice_date: r.invoice_date,
|
||||
status: r.status,
|
||||
total: r.total,
|
||||
currency: r.currency,
|
||||
})),
|
||||
invoice: invoice
|
||||
? {
|
||||
id: (invoice as { id: string }).id,
|
||||
invoice_number: ((invoice as { invoice_number?: string | null }).invoice_number) ?? null,
|
||||
status: ((invoice as { status?: string | null }).status) ?? null,
|
||||
total: ((invoice as { total?: number | null }).total) ?? null,
|
||||
currency: ((invoice as { currency?: string | null }).currency) ?? null,
|
||||
}
|
||||
: null,
|
||||
company_invoice_context: settings
|
||||
? {
|
||||
moms_period: (settings as { moms_period?: string | null }).moms_period ?? null,
|
||||
vat_registered: (settings as { vat_registered?: boolean | null }).vat_registered ?? null,
|
||||
accounting_method:
|
||||
(settings as { accounting_method?: string | null }).accounting_method ?? null,
|
||||
invoice_default_days:
|
||||
(settings as { invoice_default_days?: number | null }).invoice_default_days ?? null,
|
||||
}
|
||||
: null,
|
||||
}
|
||||
},
|
||||
|
||||
promptTemplate: ({ captured, profileSummary }) => {
|
||||
const lines: string[] = []
|
||||
if (profileSummary) lines.push(`Företagets profil: ${profileSummary}`, '')
|
||||
|
||||
lines.push('Användaren håller på att skriva en faktura. Hjälp dem komma rätt.')
|
||||
lines.push('')
|
||||
lines.push(renderAgentGroundRules())
|
||||
lines.push('')
|
||||
|
||||
if (captured.customer) {
|
||||
const c = captured.customer
|
||||
lines.push('KUND (vald):')
|
||||
lines.push(`- Namn: ${c.name ?? '(saknas)'}`)
|
||||
lines.push(`- Typ: ${c.customer_type ?? '(saknas)'}`)
|
||||
lines.push(`- Land: ${c.country ?? 'SE'}`)
|
||||
if (c.vat_number) {
|
||||
lines.push(
|
||||
`- VAT-nummer: ${c.vat_number}${c.vat_number_validated ? ' (validerat via VIES)' : ' (ej validerat)'}`,
|
||||
)
|
||||
}
|
||||
if (c.org_number) lines.push(`- Org.nr: ${c.org_number}`)
|
||||
lines.push('')
|
||||
|
||||
if (captured.recent_invoices.length > 0) {
|
||||
lines.push('Senaste fakturor till denna kund:')
|
||||
for (const r of captured.recent_invoices) {
|
||||
const amt = r.total != null ? `${r.total.toLocaleString('sv-SE')} ${r.currency ?? 'SEK'}` : '?'
|
||||
lines.push(` • ${r.invoice_number ?? '?'} (${r.invoice_date ?? '?'}, ${r.status ?? '?'}) — ${amt}`)
|
||||
}
|
||||
lines.push('')
|
||||
}
|
||||
} else {
|
||||
lines.push('Ingen kund vald ännu. Be användaren välja eller skapa en kund först om de behöver hjälp med momsbehandling — momskod beror på kundens land och typ.')
|
||||
lines.push('')
|
||||
}
|
||||
|
||||
if (captured.company_invoice_context) {
|
||||
const s = captured.company_invoice_context
|
||||
const known: string[] = []
|
||||
if (s.moms_period) known.push(`Momsperiod: ${s.moms_period}`)
|
||||
if (s.vat_registered != null) known.push(`Momsregistrerad: ${s.vat_registered ? 'ja' : 'nej'}`)
|
||||
if (s.accounting_method) known.push(`Bokföringsmetod: ${s.accounting_method}`)
|
||||
if (s.invoice_default_days != null) known.push(`Standardbetalningsvillkor: ${s.invoice_default_days} dagar`)
|
||||
if (known.length > 0) {
|
||||
lines.push('KÄNDA FAKTA (fråga inte om dessa):')
|
||||
for (const k of known) lines.push(`- ${k}`)
|
||||
lines.push('')
|
||||
}
|
||||
}
|
||||
|
||||
lines.push('Arbetssätt: hämta information via verktygsanrop FÖRST (tyst — statusraderna visar att du söker, och ditt resonemang sker i tankekanalen), föreslå sedan. Skriv din förklaring EN gång efteråt, inte i flera block runt anropen.')
|
||||
lines.push('- Hjälp användaren välja rätt momsbehandling baserat på kundens land + typ + VAT-validering:')
|
||||
lines.push(' · SE-kund: 25/12/6 % beroende på vara/tjänst.')
|
||||
lines.push(' · EU näringsidkare med validerat VAT-nr: omvänd skattskyldighet (reverse charge) på tjänster.')
|
||||
lines.push(' · EU privatperson: SE-moms (eller OSS-tröskel om varor).')
|
||||
lines.push(' · Utanför EU: export, 0 %.')
|
||||
lines.push('- Föreslå betalningsvillkor, OCR/Bankgiro-uppgifter, eventuell ROT/RUT, EU-text på fakturan vid reverse charge.')
|
||||
lines.push('- Du SKAPAR INTE fakturan — användaren gör det själv i formuläret. Du rådger.')
|
||||
lines.push('- Om kunden saknar VAT-nummer men är EU-näringsidkare, säg till — VIES-validering krävs för reverse charge.')
|
||||
lines.push('')
|
||||
lines.push('Svara på svenska och var direkt — ditt första svar är det första användaren ser.')
|
||||
return lines.join('\n')
|
||||
},
|
||||
})
|
||||
@@ -0,0 +1,81 @@
|
||||
import { defineAgentIntent } from './types'
|
||||
import { SONNET_MODEL } from '@/lib/agent/composer/client'
|
||||
|
||||
// kpi.explain — "Förklara siffran" on a KPI card / nyckeltal.
|
||||
//
|
||||
// The user sees a number ("rörelsemarginal 12 %") and wants to know what
|
||||
// drove it, how it compares to last period, and whether it's healthy for
|
||||
// their type of business. The agent reads the trial balance / income
|
||||
// statement, surfaces the underlying accounts, and contextualizes.
|
||||
//
|
||||
// Light-touch intent — captures only what the user looked at. The agent
|
||||
// fetches the rest via tools.
|
||||
|
||||
interface KpiExplainArgs {
|
||||
kpi_key: string
|
||||
value?: number | null
|
||||
period_label?: string | null
|
||||
trend?: string | null
|
||||
}
|
||||
|
||||
interface CapturedKpiExplain {
|
||||
kpi_key: string
|
||||
value: number | null
|
||||
period_label: string | null
|
||||
trend: string | null
|
||||
}
|
||||
|
||||
export const kpiExplain = defineAgentIntent<KpiExplainArgs, CapturedKpiExplain>({
|
||||
id: 'kpi.explain',
|
||||
buttonLabel: 'Förklara denna siffra',
|
||||
sheetTitle: 'Förklara nyckeltalet',
|
||||
|
||||
atoms: {
|
||||
mode: 'declarative',
|
||||
horizontal: ['swedish-financial-reporting'],
|
||||
includeCompanyVertical: true,
|
||||
includeCompanyModifiers: false,
|
||||
},
|
||||
|
||||
tools: [
|
||||
'gnubok_get_kpi_report',
|
||||
'gnubok_get_income_statement',
|
||||
'gnubok_get_balance_sheet',
|
||||
'gnubok_get_general_ledger',
|
||||
'gnubok_query_journal',
|
||||
'gnubok_load_skill',
|
||||
'gnubok_search_tools',
|
||||
'gnubok_remember_fact',
|
||||
'gnubok_forget_fact',
|
||||
],
|
||||
|
||||
model: SONNET_MODEL,
|
||||
|
||||
capture: async ({ kpi_key, value, period_label, trend }) => ({
|
||||
kpi_key,
|
||||
value: value ?? null,
|
||||
period_label: period_label ?? null,
|
||||
trend: trend ?? null,
|
||||
}),
|
||||
|
||||
promptTemplate: ({ captured, profileSummary }) => {
|
||||
const lines: string[] = []
|
||||
if (profileSummary) lines.push(`Företagets profil: ${profileSummary}`, '')
|
||||
|
||||
lines.push(`Användaren tittar på nyckeltalet "${captured.kpi_key}".`)
|
||||
if (captured.value != null) {
|
||||
lines.push(`Visat värde: ${captured.value.toLocaleString('sv-SE')}`)
|
||||
}
|
||||
if (captured.period_label) lines.push(`Period: ${captured.period_label}`)
|
||||
if (captured.trend) lines.push(`Trend: ${captured.trend}`)
|
||||
lines.push('')
|
||||
lines.push('Arbetssätt:')
|
||||
lines.push('1. Förklara KORT vad nyckeltalet mäter och hur det räknas ut (formel + ingående konton).')
|
||||
lines.push('2. Hämta gnubok_get_kpi_report / gnubok_get_income_statement för att se vad som driver siffran just nu.')
|
||||
lines.push('3. Peka på vad användaren kan göra om siffran är låg eller hög — utan att moralisera.')
|
||||
lines.push('4. Om det är ovanligt för deras bransch eller jämförelseperiod, säg det.')
|
||||
lines.push('')
|
||||
lines.push('Svara på svenska, max 4–5 korta stycken. Ditt första svar är det första användaren ser.')
|
||||
return lines.join('\n')
|
||||
},
|
||||
})
|
||||
@@ -0,0 +1,79 @@
|
||||
import { defineAgentIntent } from './types'
|
||||
import { SONNET_MODEL } from '@/lib/agent/composer/client'
|
||||
|
||||
// onboarding.empty — "Hjälp mig komma igång" on an empty-state page
|
||||
// (no transactions, no customers, no invoices, etc.).
|
||||
//
|
||||
// Progressive atom mode — keeps the prompt small so the agent can fan out
|
||||
// to whichever horizontal skill matches the empty area. Captures the route
|
||||
// + subject so the agent knows whether to talk about banking connection,
|
||||
// invoice creation, customer setup, etc.
|
||||
|
||||
interface OnboardingEmptyArgs {
|
||||
// The route the user is on, e.g. '/transactions', '/customers'. Optional.
|
||||
route?: string | null
|
||||
// The subject of the empty state, e.g. 'transactions', 'customers',
|
||||
// 'invoices'. Optional — derived from route when absent.
|
||||
subject?: string | null
|
||||
}
|
||||
|
||||
interface CapturedOnboardingEmpty {
|
||||
route: string | null
|
||||
subject: string | null
|
||||
}
|
||||
|
||||
export const onboardingEmpty = defineAgentIntent<OnboardingEmptyArgs, CapturedOnboardingEmpty>({
|
||||
id: 'onboarding.empty',
|
||||
buttonLabel: 'Visa mig hur jag kommer igång',
|
||||
sheetTitle: 'Komma igång',
|
||||
|
||||
atoms: {
|
||||
mode: 'progressive',
|
||||
horizontal: [],
|
||||
includeCompanyVertical: false,
|
||||
includeCompanyModifiers: false,
|
||||
},
|
||||
|
||||
tools: [
|
||||
'gnubok_search_tools',
|
||||
'gnubok_list_skills',
|
||||
'gnubok_load_skill',
|
||||
'gnubok_remember_fact',
|
||||
'gnubok_forget_fact',
|
||||
],
|
||||
|
||||
model: SONNET_MODEL,
|
||||
|
||||
capture: async ({ route, subject }) => ({
|
||||
route: route ?? null,
|
||||
subject: subject ?? deriveSubject(route ?? null),
|
||||
}),
|
||||
|
||||
promptTemplate: ({ captured, profileSummary }) => {
|
||||
const lines: string[] = []
|
||||
if (profileSummary) lines.push(`Företagets profil: ${profileSummary}`, '')
|
||||
|
||||
lines.push(
|
||||
`Användaren är på en tom sida${captured.subject ? ` för ${captured.subject}` : ''} och vill komma igång.`,
|
||||
)
|
||||
if (captured.route) lines.push(`Route: ${captured.route}`)
|
||||
lines.push('')
|
||||
lines.push(
|
||||
'Förklara kort vad sidan är till för och vilka 1–2 nästa steg som ger mest värde för just denna användare. Var konkret. Svara på svenska.',
|
||||
)
|
||||
return lines.join('\n')
|
||||
},
|
||||
})
|
||||
|
||||
function deriveSubject(route: string | null): string | null {
|
||||
if (!route) return null
|
||||
if (route.startsWith('/transactions')) return 'transaktioner'
|
||||
if (route.startsWith('/customers')) return 'kunder'
|
||||
if (route.startsWith('/invoices')) return 'kundfakturor'
|
||||
if (route.startsWith('/supplier-invoices')) return 'leverantörsfakturor'
|
||||
if (route.startsWith('/bookkeeping')) return 'bokföring'
|
||||
if (route.startsWith('/assets')) return 'anläggningstillgångar'
|
||||
if (route.startsWith('/reports')) return 'rapporter'
|
||||
if (route.startsWith('/salary')) return 'löner'
|
||||
return null
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user