feat(assistant): say what the conversation is anchored to (#1222)

* feat(assistant): say what the conversation is anchored to

agent_conversations.context_ref has been written since the first intents
shipped and read by nothing. The panel ignored it, so a thread resumed three
days later showed the messages with no indication of which invoice or which
bokslut it concerned, even though the row knew. /chat did worse: it printed the
ref raw, so the subtitle under someone's own conversation read
"invoice:5f3a-9c21-...", a database identifier shown to an accountant.

Both surfaces now render the same chip, which names the thing and links to it.
This matters more since the panel docks: sitting beside the page, "what is this
about" is a question the surface should answer rather than the user's memory.

The mapping is a data map in route-mapping.ts, not a switch in a component
(plan seam 8.5), so a flow run's ref renders in both surfaces with no change to
either. A ref it cannot read renders nothing rather than a broken chip.

Two refs deliberately have no link. There is no /transactions/[id] route, so a
transaction chip points at the list. The document inbox is an extension mounted
under /e/[sector], and core must not hardcode a path that exists only when the
extension is enabled, so that one is named without being linked.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* fix(assistant): review triage: make the colon test observable, drop an overclaim

The colon-splitting test asserted on a kpi ref, and kpi discards its id, so it
passed even with a parser that dropped everything after the second colon. Moved
to invoice:abc:2026, where the id reaches the href. Verified by switching
indexOf to lastIndexOf and confirming the test fails.

ContextChip's comment said a flow run's ref renders with no change to either
surface. It does not: an unknown kind maps to null and renders nothing until
the map gains an entry. The seam is that adding one is a single entry in one
file, which is what the comment now says.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Jakob Wennberg
2026-07-27 11:26:16 +02:00
committed by GitHub
co-authored by Claude Opus 5
parent 60cc51fe21
commit bcbe9b0903
5 changed files with 206 additions and 7 deletions
+25 -2
View File
@@ -1,7 +1,16 @@
'use client'
import { useEffect, useRef, useState } from 'react'
import { X, Expand, Shrink, PanelRightClose, Eraser, History, ChevronLeft, Loader2 } from 'lucide-react'
import {
X,
Expand,
Shrink,
PanelRightClose,
Eraser,
History,
ChevronLeft,
Loader2,
} from 'lucide-react'
import AgentChat, {
attachStagedOperations,
normalizeStoredMessages,
@@ -9,6 +18,7 @@ import AgentChat, {
} from './AgentChat'
import type { StoredStagedOperation } from '@/types'
import type { AgentStatusEvent } from './agent-status'
import ContextChip from './ContextChip'
import AgentAvatar from './AgentAvatar'
import AgentSessionList from './AgentSessionList'
import SandboxAgentPreview from './SandboxAgentPreview'
@@ -98,6 +108,10 @@ export default function AgentSheet({
const sheetTitle = intentToTitle(intentId, agentName)
const displayTitle = loaded ? (loaded.title ?? intentToTitle(loaded.intentId, agentName)) : sheetTitle
const activeConversationId = loaded?.id ?? conversationId
// A resumed thread's stored ref wins: it says what THAT conversation was
// about, which is the whole reason to show this. Falls back to the ref the
// panel was opened with for a thread that has not been persisted yet.
const activeContextRef = loaded ? loaded.contextRef : (contextRef ?? null)
const sheetRef = useRef<HTMLDivElement | null>(null)
// Monotonic counter so a slow conversation fetch can't clobber a newer pick.
const selectSeqRef = useRef(0)
@@ -257,7 +271,16 @@ export default function AgentSheet({
</button>
)}
<AgentAvatar avatarId={identity.avatarId} size="sm" alt={agentName ?? 'Assistent'} />
<h2 className="font-display text-lg tracking-tight truncate">{displayTitle}</h2>
<div className="min-w-0 flex-1">
<h2 className="font-display text-lg leading-tight tracking-tight truncate">
{displayTitle}
</h2>
{/* What this conversation is anchored to. context_ref has been
stored since the first intents shipped and read by nothing, so a
thread resumed days later gave no clue which invoice it was
about. Matters more now the panel sits BESIDE the page. */}
<ContextChip contextRef={activeContextRef} className="mt-0.5" />
</div>
<div className="ml-auto flex items-center gap-1">
{/* Grow/shrink the panel in place: NEVER navigates away, so the
user stays on the current page. Hidden on mobile where the sheet
+6 -4
View File
@@ -6,6 +6,7 @@ import { ArrowLeft } from 'lucide-react'
import AgentChat, { attachStagedOperations, normalizeStoredMessages } from './AgentChat'
import type { StoredStagedOperation } from '@/types'
import AgentAvatar from './AgentAvatar'
import ContextChip from './ContextChip'
import SandboxAgentPreview from './SandboxAgentPreview'
import { useAgentSheet } from './AgentSheetProvider'
import { useCompanyOptional } from '@/contexts/CompanyContext'
@@ -59,10 +60,11 @@ export default function ChatConversationView({
alt={identity.displayName ?? 'Assistent'}
/>
<div className="min-w-0">
<h1 className="font-display text-lg tracking-tight truncate">{title}</h1>
{contextRef && (
<p className="text-xs text-muted-foreground truncate">{contextRef}</p>
)}
<h1 className="font-display text-lg leading-tight tracking-tight truncate">{title}</h1>
{/* Was printing context_ref raw, so the subtitle read
"invoice:5f3a-9c21-...": a database identifier, shown to an
accountant, under the title of their own conversation. */}
<ContextChip contextRef={contextRef} className="mt-0.5" />
</div>
</header>
+59
View File
@@ -0,0 +1,59 @@
'use client'
import Link from 'next/link'
import { ArrowUpRight } from 'lucide-react'
import { contextRefToTarget } from '@/lib/agent/intents/route-mapping'
import { cn } from '@/lib/utils'
/**
* What a conversation is anchored to, as a chip.
*
* `agent_conversations.context_ref` has been written since the first intents
* shipped. The panel ignored it entirely, and /chat printed it raw, so a
* resumed thread's subtitle read "invoice:5f3a-9c21-...": a database
* identifier shown to an accountant. Both surfaces now render the same chip,
* which names the thing and links to it.
*
* Kind-agnostic on purpose (plan seam 8.4): the mapping lives in
* route-mapping.ts, so teaching every surface about a new ref kind is one
* entry in that map and no component change. A kind the map does not know
* yet, a flow run for instance, renders nothing rather than a broken link.
*/
export default function ContextChip({
contextRef,
className,
}: {
contextRef: string | null | undefined
className?: string
}) {
const target = contextRefToTarget(contextRef)
if (!target) return null
const shape =
'inline-flex max-w-full items-center gap-1 rounded-full border border-border px-2 py-0.5 text-xs text-muted-foreground'
// Not every context has a page to go to: the document inbox is an extension
// route, and core cannot hardcode a path that exists only when the extension
// is enabled. Naming it without linking beats a link that 404s.
if (!target.href) {
return (
<span className={cn(shape, className)}>
<span className="truncate">{target.label}</span>
</span>
)
}
return (
<Link
href={target.href}
className={cn(
shape,
'hover:bg-secondary hover:text-foreground transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring',
className,
)}
>
<span className="truncate">{target.label}</span>
<ArrowUpRight className="h-3 w-3 shrink-0" />
</Link>
)
}