* fix(mobile): show attached images in native (rich) chat Attaching an image in the mobile native chat did nothing visible — it reused the terminal attach flow, which pastes a bracketed host path into the hidden terminal, so there was no composer preview and nothing in the transcript. Give native chat the desktop model instead: - pick + upload shows a removable thumbnail chip in the composer (no early paste) - on submit, images ride along: Ctrl+U clear -> bracketed paste(s) -> settle -> text + Enter (idempotent on retry) - the optimistic echo carries the local preview URIs and the message renderer draws image-ref blocks as real thumbnails when the URI is loadable, so the sent photo appears in the conversation immediately - image-only echoes reconcile by ordinal against user turns after their tail (ignores agent replies / paginated history / the 'unknown' ack-loss path) Terminal chat attach is unchanged (both flows consolidated behind useMobileSessionImageAttachments). Adds unit coverage for pick+upload, the ride-along byte order, chip render/remove, and echo reconciliation. * test(mobile): interactive native-chat image proof (real hooks, click-driven) Replace the hand-fed component render with an interactive harness that mounts the real MobileNativeChatComposer/Message + useMobileNativeChatImageAttachments + drafts under react-native-web and drives the actual flow via clicks. Only the two OS boundaries are faked: the photo picker and the paired-host RPC socket. Screenshots (mobile/docs/native-chat-image-attachment/) are produced by real clicks, not props: - attach -> real upload pipeline -> chip appears, nothing pasted yet - send -> real ride-along emits Ctrl+U clear, bracketed image paste, text+Enter (shown in the live byte trace) and the sent bubble renders the photo thumbnail * fix(mobile): scope native-chat image attachments by active tab Images are now scoped to the tab that initiated the pick, so switching tabs during upload cannot ride an image into another terminal. Chips stay with their original tab, and only the active scope's images send with text. Improved error handling with user-facing toast messages for disconnection and send failures. * test(mobile): add image attachment tab-scoping and error tests Add comprehensive test coverage for tab-scoped attachment behavior, error handling when transport fails or lease is gated, and edge cases like attaching images during an in-flight send. Extract baseArgs and update helpers to reduce boilerplate across test cases. * fix(mobile): show attached images in native rich chat Images attached in the mobile native (rich) chat now display as: - Removable composer chips while composing - Thumbnails in the sent user bubble after sending (desktop parity) Implements proper image echo reconciliation by distinguishing image-source marker turns from text echoes, so an image send isn't cleared by an unrelated text echo. Adds scope isolation to prevent chips and drafts from leaking between tabs, and detects tab switches during the image-paste settle window to abort the send. Fixes Android tap-target positioning for the image removal badge and clears stale terminal input after failed pastes to avoid gluing fragments onto the next message. * rm stubs --------- Co-authored-by: Jinjing <6427696+AmethystLiang@users.noreply.github.com>
132 lines
4.9 KiB
TypeScript
132 lines
4.9 KiB
TypeScript
import { formatAgentTypeLabel } from '../../../src/shared/agent-type-label'
|
|
import {
|
|
formatNativeChatEmptyStateCopy,
|
|
type NativeChatEmptyStateCopy
|
|
} from '../../../src/shared/native-chat-empty-state'
|
|
import type { NativeChatMessage } from '../../../src/shared/native-chat-types'
|
|
import { foldToolMessages } from './mobile-native-chat-blocks'
|
|
import { normalizeImageTranscriptMessages } from './mobile-native-chat-image-transcript-markers'
|
|
import { stripNoiseMessages } from './mobile-native-chat-noise'
|
|
import type { MobileNativeChatStatus } from './use-mobile-native-chat-session'
|
|
|
|
/** The centered empty-state copy for a chat with no messages, mirroring the
|
|
* desktop `NativeChatEmptyState` (shared copy + agent label) so the two surfaces
|
|
* stay in lockstep. Returns null when the list should stay bare (idle, or the
|
|
* loading spinner owns the view). */
|
|
export function mobileNativeChatEmptyState(
|
|
status: MobileNativeChatStatus,
|
|
agent: string | null,
|
|
error?: string
|
|
): NativeChatEmptyStateCopy | null {
|
|
const agentLabel = agent ? formatAgentTypeLabel(agent) : 'the agent'
|
|
switch (status) {
|
|
// A live agent with no transcript yet — and a loaded-but-empty transcript —
|
|
// are both "start a chat"; invite the first message instead of implying the
|
|
// agent is still starting up.
|
|
case 'waiting-session':
|
|
case 'ready':
|
|
return formatNativeChatEmptyStateCopy('empty', agentLabel)
|
|
case 'error': {
|
|
const copy = formatNativeChatEmptyStateCopy('error', agentLabel)
|
|
return error ? { ...copy, subtitle: error } : copy
|
|
}
|
|
default:
|
|
return null
|
|
}
|
|
}
|
|
|
|
/** An optimistic user echo: the text and/or the local preview URIs of any images
|
|
* ridden along on the send, shown until the transcript catches up. */
|
|
export type MobileNativeChatPendingItem = {
|
|
id: string
|
|
text: string
|
|
images?: string[]
|
|
}
|
|
|
|
/** Derive the list data from the raw transcript: fold tool turns into the
|
|
* assistant turn, optionally append a synthetic streaming bubble, then the
|
|
* route-owned optimistic "queued" messages at the tail. Returns the
|
|
* intermediate `folded`/`streaming` so the caller can memoize on them. */
|
|
export function buildMobileNativeChatData({
|
|
messages,
|
|
streamingText,
|
|
pending
|
|
}: {
|
|
messages: NativeChatMessage[]
|
|
streamingText?: string
|
|
pending: MobileNativeChatPendingItem[]
|
|
}): { folded: NativeChatMessage[]; streaming: string | null; data: NativeChatMessage[] } {
|
|
const folded = foldMobileNativeChatMessages(messages)
|
|
return buildMobileNativeChatTransientData({ folded, streamingText, pending })
|
|
}
|
|
|
|
export function foldMobileNativeChatMessages(messages: NativeChatMessage[]): NativeChatMessage[] {
|
|
// Normalize first (desktop assembler parity): image marker turns fold into
|
|
// image-ref blocks instead of rendering as raw `[Image: …]` text.
|
|
return foldToolMessages(stripNoiseMessages(normalizeImageTranscriptMessages(messages)))
|
|
}
|
|
|
|
export function buildMobileNativeChatTransientData({
|
|
folded,
|
|
streamingText,
|
|
pending
|
|
}: {
|
|
folded: NativeChatMessage[]
|
|
streamingText?: string
|
|
pending: MobileNativeChatPendingItem[]
|
|
}): { folded: NativeChatMessage[]; streaming: string | null; data: NativeChatMessage[] } {
|
|
// Only show the streaming bubble while its text leads the transcript — once the
|
|
// real assistant turn lands with the same text, drop the synthetic one.
|
|
const streaming = deriveStreaming(folded, streamingText)
|
|
const data: NativeChatMessage[] = [
|
|
...folded,
|
|
...(streaming
|
|
? [
|
|
{
|
|
id: 'streaming',
|
|
role: 'assistant' as const,
|
|
blocks: [{ type: 'text' as const, text: streaming }],
|
|
timestamp: null,
|
|
source: 'hook' as const
|
|
}
|
|
]
|
|
: []),
|
|
...pending.map((p) => ({
|
|
id: p.id,
|
|
role: 'user' as const,
|
|
// Text first (when present), then a thumbnail per ridden-along image so the
|
|
// sent photo shows immediately, before the transcript echo lands.
|
|
blocks: [
|
|
...(p.text ? [{ type: 'text' as const, text: p.text }] : []),
|
|
...(p.images ?? []).map((uri) => ({ type: 'image-ref' as const, url: uri }))
|
|
],
|
|
timestamp: null,
|
|
source: 'transcript' as const
|
|
}))
|
|
]
|
|
return { folded, streaming, data }
|
|
}
|
|
|
|
function deriveStreaming(folded: NativeChatMessage[], streamingText?: string): string | null {
|
|
const text = streamingText?.trim()
|
|
if (!text) {
|
|
return null
|
|
}
|
|
const last = folded[folded.length - 1]
|
|
const lastText =
|
|
last?.role === 'assistant'
|
|
? last.blocks
|
|
.filter((b) => b.type === 'text')
|
|
.map((b) => (b.type === 'text' ? b.text : ''))
|
|
.join('')
|
|
.trim()
|
|
: ''
|
|
// Hide the synthetic bubble only once the real turn has landed leading with the
|
|
// streamed text. A bare length compare would suppress a short new reply behind a
|
|
// longer previous turn; a completed prior turn won't start with the new prefix.
|
|
if (lastText.startsWith(text)) {
|
|
return null
|
|
}
|
|
return text
|
|
}
|