Cleanup: unify HookMessage naming and simplify SessionContext

- Rename HookAppMessage to HookMessage, isHookAppMessage to isHookMessage
- Remove entries array from SessionContext (use isHookMessage type guard instead)
- HookMessage.content now accepts string directly (not just array)
- Fix streamMessage type in AgentState (AppMessage, not Message)
- Rename CustomMessageComponent to HookMessageComponent
- Fix test hook to use pi.sendMessage
This commit is contained in:
Mario Zechner 2025-12-27 20:52:12 +01:00
parent a2515cf43f
commit 204d27581b
13 changed files with 62 additions and 106 deletions

View file

@ -1,22 +1,22 @@
import type { TextContent } from "@mariozechner/pi-ai";
import { Box, Container, Markdown, Spacer, Text } from "@mariozechner/pi-tui";
import type { HookMessage, HookMessageRenderer } from "../../../core/hooks/types.js";
import type { CustomMessageEntry } from "../../../core/session-manager.js";
import type { HookMessage } from "packages/coding-agent/src/core/messages.js";
import type { HookMessageRenderer } from "../../../core/hooks/types.js";
import { getMarkdownTheme, theme } from "../theme/theme.js";
/**
* Component that renders a custom message entry from hooks.
* Uses distinct styling to differentiate from user messages.
*/
export class CustomMessageComponent extends Container {
private entry: CustomMessageEntry;
export class HookMessageComponent extends Container {
private message: HookMessage<unknown>;
private customRenderer?: HookMessageRenderer;
private box: Box;
private _expanded = false;
constructor(entry: CustomMessageEntry, customRenderer?: HookMessageRenderer) {
constructor(message: HookMessage<unknown>, customRenderer?: HookMessageRenderer) {
super();
this.entry = entry;
this.message = message;
this.customRenderer = customRenderer;
this.addChild(new Spacer(1));
@ -38,18 +38,10 @@ export class CustomMessageComponent extends Container {
private rebuild(): void {
this.box.clear();
// Convert entry to HookMessage for renderer
const message: HookMessage = {
customType: this.entry.customType,
content: this.entry.content,
display: this.entry.display,
details: this.entry.details,
};
// Try custom renderer first
if (this.customRenderer) {
try {
const component = this.customRenderer(message, { expanded: this._expanded }, theme);
const component = this.customRenderer(this.message, { expanded: this._expanded }, theme);
if (component) {
this.box.addChild(component);
return;
@ -60,16 +52,16 @@ export class CustomMessageComponent extends Container {
}
// Default rendering: label + content
const label = theme.fg("customMessageLabel", `\x1b[1m[${this.entry.customType}]\x1b[22m`);
const label = theme.fg("customMessageLabel", `\x1b[1m[${this.message.customType}]\x1b[22m`);
this.box.addChild(new Text(label, 0, 0));
this.box.addChild(new Spacer(1));
// Extract text content
let text: string;
if (typeof this.entry.content === "string") {
text = this.entry.content;
if (typeof this.message.content === "string") {
text = this.message.content;
} else {
text = this.entry.content
text = this.message.content
.filter((c): c is TextContent => c.type === "text")
.map((c) => c.text)
.join("\n");

View file

@ -28,7 +28,7 @@ import { APP_NAME, getAuthPath, getDebugLogPath } from "../../config.js";
import type { AgentSession, AgentSessionEvent } from "../../core/agent-session.js";
import type { LoadedCustomTool, SessionEvent as ToolSessionEvent } from "../../core/custom-tools/index.js";
import type { HookUIContext } from "../../core/hooks/index.js";
import { isBashExecutionMessage, isHookAppMessage } from "../../core/messages.js";
import { isBashExecutionMessage, isHookMessage } from "../../core/messages.js";
import {
getLatestCompactionEntry,
type SessionContext,
@ -46,10 +46,10 @@ import { AssistantMessageComponent } from "./components/assistant-message.js";
import { BashExecutionComponent } from "./components/bash-execution.js";
import { CompactionComponent } from "./components/compaction.js";
import { CustomEditor } from "./components/custom-editor.js";
import { CustomMessageComponent } from "./components/custom-message.js";
import { DynamicBorder } from "./components/dynamic-border.js";
import { FooterComponent } from "./components/footer.js";
import { HookInputComponent } from "./components/hook-input.js";
import { HookMessageComponent } from "./components/hook-message.js";
import { HookSelectorComponent } from "./components/hook-selector.js";
import { ModelSelectorComponent } from "./components/model-selector.js";
import { OAuthSelectorComponent } from "./components/oauth-selector.js";
@ -817,7 +817,7 @@ export class InteractiveMode {
break;
case "message_start":
if (isHookAppMessage(event.message)) {
if (isHookMessage(event.message)) {
this.addMessageToChat(event.message);
this.ui.requestRender();
} else if (event.message.role === "user") {
@ -1051,7 +1051,7 @@ export class InteractiveMode {
this.ui.requestRender();
}
private addMessageToChat(message: Message | AppMessage): void {
private addMessageToChat(message: AppMessage): void {
if (isBashExecutionMessage(message)) {
const component = new BashExecutionComponent(message.command, this.ui);
if (message.output) {
@ -1067,20 +1067,11 @@ export class InteractiveMode {
return;
}
if (isHookAppMessage(message)) {
if (isHookMessage(message)) {
// Render as custom message if display is true
if (message.display) {
const entry = {
type: "custom_message" as const,
customType: message.customType,
content: message.content,
display: true,
id: "",
parentId: null,
timestamp: new Date().toISOString(),
};
const renderer = this.session.hookRunner?.getMessageRenderer(message.customType);
this.chatContainer.addChild(new CustomMessageComponent(entry, renderer));
this.chatContainer.addChild(new HookMessageComponent(message, renderer));
}
} else if (message.role === "user") {
const textContent = this.getUserMessageText(message);
@ -1114,11 +1105,9 @@ export class InteractiveMode {
}
const compactionEntry = getLatestCompactionEntry(this.sessionManager.getEntries());
const entries = sessionContext.entries;
for (let i = 0; i < sessionContext.messages.length; i++) {
const message = sessionContext.messages[i];
const entry = entries?.[i];
if (isBashExecutionMessage(message)) {
this.addMessageToChat(message);
@ -1126,10 +1115,10 @@ export class InteractiveMode {
}
// Check if this is a custom_message entry
if (entry?.type === "custom_message") {
if (entry.display) {
const renderer = this.session.hookRunner?.getMessageRenderer(entry.customType);
this.chatContainer.addChild(new CustomMessageComponent(entry, renderer));
if (isHookMessage(message)) {
if (message.display) {
const renderer = this.session.hookRunner?.getMessageRenderer(message.customType);
this.chatContainer.addChild(new HookMessageComponent(message, renderer));
}
continue;
}
@ -1322,7 +1311,7 @@ export class InteractiveMode {
child.setExpanded(this.toolOutputExpanded);
} else if (child instanceof BashExecutionComponent) {
child.setExpanded(this.toolOutputExpanded);
} else if (child instanceof CustomMessageComponent) {
} else if (child instanceof HookMessageComponent) {
child.setExpanded(this.toolOutputExpanded);
}
}