mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-25 05:07:05 +00:00
Adds an opt-in content search over session messages, alongside the existing metadata search. An FTS5 index covers visible user and assistant prose; the sidebar gains a scope toggle, and selecting a result opens the matching message rather than the session tail. Search scope is a two-state toggle rather than a dropdown: a 48x48 target matching the clear endcap, distinct glyphs for session details vs message text, tooltips naming the real scopes, and the active mode kept on the collapsed chip (tiann/hapi#2081). Dogfooded on a live 1178-session fleet. The scope toggle and reading forward from a search result were both click-tested on that install — the latter walking six bounded pages through a 1,507-message session and stopping correctly at the session head. The per-commit history does not survive this squash; the review trail — including which commit closed which finding — is on the PR threads. Fixes #1554 Fixes #2081
175 lines
7.3 KiB
TypeScript
175 lines
7.3 KiB
TypeScript
import { describe, expect, it, vi } from 'vitest';
|
|
import { wireTransportEvents } from './loop';
|
|
import type { PiTransport } from './piTransport';
|
|
import { PiSession } from './session';
|
|
import type { CodexMessage } from '@/agent/messageConverter';
|
|
|
|
/** 1x1 red PNG. */
|
|
const ONE_PX_PNG = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==';
|
|
|
|
function createHarness() {
|
|
let listener: ((event: Record<string, unknown>) => void) | null = null;
|
|
const order: string[] = [];
|
|
const messages: CodexMessage[] = [];
|
|
const transport = {
|
|
onEvent: vi.fn((handler: (event: Record<string, unknown>) => void) => { listener = handler; }),
|
|
send: vi.fn(),
|
|
} as unknown as PiTransport;
|
|
const client = {
|
|
keepAlive: vi.fn(),
|
|
updateMetadata: vi.fn(),
|
|
emitMessagesConsumed: vi.fn(),
|
|
sendSessionEvent: vi.fn(),
|
|
updateAgentState: vi.fn(),
|
|
emitSessionReady: vi.fn(),
|
|
getMetadata: vi.fn(() => null),
|
|
rpcHandlerManager: { registerHandler: vi.fn() },
|
|
};
|
|
const session = new PiSession({
|
|
api: {} as never,
|
|
client: {
|
|
...client,
|
|
sendAgentMessage: vi.fn((msg: CodexMessage) => {
|
|
order.push(`agent:${msg.type}`);
|
|
messages.push(msg);
|
|
}),
|
|
sendSessionEvent: vi.fn((msg: { type: string }) => { order.push(`session:${msg.type}`); }),
|
|
} as never,
|
|
path: '/tmp/test',
|
|
logPath: '/tmp/test.log',
|
|
startedBy: 'terminal',
|
|
startingMode: 'local',
|
|
model: 'deepseek-v4-flash-0731',
|
|
});
|
|
|
|
const controller = wireTransportEvents(transport, session, []);
|
|
|
|
return {
|
|
emit: (event: Record<string, unknown>) => listener?.(event),
|
|
order,
|
|
messages,
|
|
controller,
|
|
flush: () => new Promise((resolve) => setTimeout(resolve, 0)),
|
|
};
|
|
}
|
|
|
|
|
|
describe('Pi publish ordering (real converters)', () => {
|
|
it('publishes inline images before a same-chunk extension notification', () => {
|
|
const h = createHarness();
|
|
h.emit({
|
|
type: 'tool_execution_end', toolCallId: 'image', toolName: 'hapi_display_image',
|
|
result: { content: [{ type: 'image', data: ONE_PX_PNG, mimeType: 'image/png' }] },
|
|
isError: false,
|
|
});
|
|
h.emit({ type: 'extension_ui_request', id: 'notice', method: 'notify', message: 'Image ready', notifyType: 'info' });
|
|
expect(h.order).toEqual(['agent:tool-call-result', 'agent:generated-image', 'session:message']);
|
|
});
|
|
it('publishes a tool-execution result before a later retry notice from the same chunk', async () => {
|
|
const h = createHarness();
|
|
|
|
h.emit({
|
|
type: 'tool_execution_end',
|
|
toolCallId: 'tc-1',
|
|
toolName: 'bash',
|
|
result: { content: [{ type: 'text', text: 'ok' }], details: {} },
|
|
isError: false,
|
|
});
|
|
h.emit({ type: 'auto_retry_start', attempt: 1, maxAttempts: 3, delayMs: 10, errorMessage: '429' });
|
|
|
|
await h.flush();
|
|
|
|
// The tool result must reach the chat before the lifecycle notice.
|
|
const resultIdx = h.order.indexOf('agent:tool-call-result');
|
|
const noticeIdx = h.order.indexOf('session:message');
|
|
expect(resultIdx).toBeGreaterThanOrEqual(0);
|
|
expect(noticeIdx).toBeGreaterThan(resultIdx);
|
|
});
|
|
|
|
it('keeps image cards in event order ahead of a compaction notice', async () => {
|
|
const h = createHarness();
|
|
|
|
h.emit({
|
|
type: 'tool_execution_end',
|
|
toolCallId: 'tc-2',
|
|
toolName: 'hapi_display_image',
|
|
result: {
|
|
content: [
|
|
{ type: 'text', text: 'screenshot' },
|
|
{ type: 'image', data: ONE_PX_PNG, mimeType: 'image/png' },
|
|
],
|
|
details: {},
|
|
},
|
|
isError: false,
|
|
});
|
|
h.emit({ type: 'compaction_start', reason: 'threshold' });
|
|
|
|
await h.flush();
|
|
|
|
const resultIdx = h.order.indexOf('agent:tool-call-result');
|
|
const imageIdx = h.order.indexOf('agent:generated-image');
|
|
const noticeIdx = h.order.indexOf('session:message');
|
|
expect(resultIdx).toBeGreaterThanOrEqual(0);
|
|
expect(imageIdx).toBeGreaterThan(resultIdx);
|
|
expect(noticeIdx).toBeGreaterThan(imageIdx);
|
|
});
|
|
|
|
it('preserves assistant snapshots on both sides of an inline image result', async () => {
|
|
const h = createHarness();
|
|
const text = (delta: string) => h.emit({
|
|
type: 'message_update', assistantMessageEvent: { type: 'text_delta', delta },
|
|
});
|
|
h.emit({ type: 'message_start' });
|
|
text('before');
|
|
h.emit({ type: 'message_end' });
|
|
h.emit({
|
|
type: 'tool_execution_end', toolCallId: 'image', toolName: 'hapi_display_image',
|
|
result: { content: [{ type: 'image', data: ONE_PX_PNG, mimeType: 'image/png' }] },
|
|
isError: false,
|
|
});
|
|
h.emit({ type: 'message_start' });
|
|
text('after');
|
|
h.emit({ type: 'message_end' });
|
|
await h.flush();
|
|
// Each text segment publishes its live snapshot and then a terminal
|
|
// one. The terminal copy is deliberate: the hub skips live snapshots
|
|
// when building the content-search index, so without it a reply whose
|
|
// final text already arrived live would never be indexed (tiann/hapi#1554).
|
|
// Downstream both collapse into one block — the hub dedupes by render
|
|
// key and the web reducer merges blocks sharing a stream id.
|
|
//
|
|
// What this test guards is the ordering: assistant text on both sides
|
|
// of the inline image result, never reordered across it.
|
|
expect(h.messages.map((message) => message.type === 'message' ? message.message : message.type))
|
|
.toEqual(['before', 'before', 'tool-call-result', 'generated-image', 'after', 'after']);
|
|
});
|
|
|
|
it('retains queued assistant deltas when the prompt is rejected in the same chunk', async () => {
|
|
const h = createHarness();
|
|
h.controller.beginPromptLifecycle('prompt-rejected');
|
|
h.emit({ type: 'message_start' });
|
|
h.emit({ type: 'message_update', assistantMessageEvent: { type: 'text_delta', delta: 'before' } });
|
|
await h.flush();
|
|
|
|
h.emit({ type: 'message_update', assistantMessageEvent: { type: 'text_delta', delta: ' rejection' } });
|
|
h.emit({ type: 'response', command: 'prompt', id: 'prompt-rejected', success: false, error: 'rejected' });
|
|
await h.flush();
|
|
expect(h.messages.filter((message) => message.type === 'message').map((message) => message.message))
|
|
.toEqual(['before', 'before rejection']);
|
|
});
|
|
|
|
it('drains queued deltas before flushing a closed or failed transport', async () => {
|
|
const h = createHarness();
|
|
h.emit({ type: 'message_start' });
|
|
h.emit({ type: 'message_update', assistantMessageEvent: { type: 'text_delta', delta: 'before' } });
|
|
await h.flush();
|
|
|
|
h.emit({ type: 'message_update', assistantMessageEvent: { type: 'text_delta', delta: ' close' } });
|
|
await h.controller.flush();
|
|
await h.flush();
|
|
expect(h.messages.filter((message) => message.type === 'message').map((message) => message.message))
|
|
.toEqual(['before', 'before close']);
|
|
});
|
|
|
|
});
|