mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-16 04:01:56 +00:00
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:
parent
a2515cf43f
commit
204d27581b
13 changed files with 62 additions and 106 deletions
|
|
@ -14,7 +14,7 @@
|
|||
*/
|
||||
|
||||
import type { Agent, AgentEvent, AgentState, AppMessage, Attachment, ThinkingLevel } from "@mariozechner/pi-agent-core";
|
||||
import type { AssistantMessage, ImageContent, Message, Model, TextContent } from "@mariozechner/pi-ai";
|
||||
import type { AssistantMessage, Message, Model, TextContent } from "@mariozechner/pi-ai";
|
||||
import { isContextOverflow, modelsAreEqual, supportsXhigh } from "@mariozechner/pi-ai";
|
||||
import { getAuthPath } from "../config.js";
|
||||
import { type BashResult, executeBash as executeBashCommand } from "./bash-executor.js";
|
||||
|
|
@ -29,13 +29,12 @@ import type { LoadedCustomTool, SessionEvent as ToolSessionEvent } from "./custo
|
|||
import { exportSessionToHtml } from "./export-html.js";
|
||||
import type {
|
||||
HookCommandContext,
|
||||
HookMessage,
|
||||
HookRunner,
|
||||
SessionEventResult,
|
||||
TurnEndEvent,
|
||||
TurnStartEvent,
|
||||
} from "./hooks/index.js";
|
||||
import { type BashExecutionMessage, type HookAppMessage, isHookAppMessage } from "./messages.js";
|
||||
import { type BashExecutionMessage, type HookMessage, isHookMessage } from "./messages.js";
|
||||
import type { ModelRegistry } from "./model-registry.js";
|
||||
import type { CompactionEntry, SessionManager } from "./session-manager.js";
|
||||
import type { SettingsManager, SkillsSettings } from "./settings-manager.js";
|
||||
|
|
@ -220,7 +219,7 @@ export class AgentSession {
|
|||
// Handle session persistence
|
||||
if (event.type === "message_end") {
|
||||
// Check if this is a hook message (has _hookData marker)
|
||||
if (isHookAppMessage(event.message)) {
|
||||
if (isHookMessage(event.message)) {
|
||||
// Persist as CustomMessageEntry
|
||||
this.sessionManager.appendCustomMessageEntry(
|
||||
event.message.customType,
|
||||
|
|
@ -557,21 +556,18 @@ export class AgentSession {
|
|||
* @param message Hook message with customType, content, display, details
|
||||
* @param triggerTurn If true and not streaming, triggers a new LLM turn
|
||||
*/
|
||||
async sendHookMessage<T = unknown>(message: HookMessage<T>, triggerTurn?: boolean): Promise<void> {
|
||||
// Normalize content to array format for the AppMessage
|
||||
const content: (TextContent | ImageContent)[] =
|
||||
typeof message.content === "string" ? [{ type: "text", text: message.content }] : message.content;
|
||||
|
||||
// Create HookAppMessage with proper role for type-safe handling
|
||||
const appMessage: HookAppMessage = {
|
||||
role: "hookMessage",
|
||||
async sendHookMessage<T = unknown>(
|
||||
message: Pick<HookMessage<T>, "customType" | "content" | "display" | "details">,
|
||||
triggerTurn?: boolean,
|
||||
): Promise<void> {
|
||||
const appMessage = {
|
||||
role: "hookMessage" as const,
|
||||
customType: message.customType,
|
||||
content,
|
||||
content: message.content,
|
||||
display: message.display,
|
||||
details: message.details,
|
||||
timestamp: Date.now(),
|
||||
};
|
||||
|
||||
} satisfies HookMessage<T>;
|
||||
if (this.isStreaming) {
|
||||
// Queue for processing by agent loop
|
||||
await this.agent.queueMessage(appMessage);
|
||||
|
|
|
|||
|
|
@ -27,7 +27,6 @@ export type {
|
|||
HookEvent,
|
||||
HookEventContext,
|
||||
HookFactory,
|
||||
HookMessage,
|
||||
HookMessageRenderer,
|
||||
HookMessageRenderOptions,
|
||||
HookUIContext,
|
||||
|
|
|
|||
|
|
@ -9,15 +9,9 @@ import * as path from "node:path";
|
|||
import { fileURLToPath } from "node:url";
|
||||
import { createJiti } from "jiti";
|
||||
import { getAgentDir } from "../../config.js";
|
||||
import type { HookMessage } from "../messages.js";
|
||||
import { execCommand } from "./runner.js";
|
||||
import type {
|
||||
ExecOptions,
|
||||
HookAPI,
|
||||
HookFactory,
|
||||
HookMessage,
|
||||
HookMessageRenderer,
|
||||
RegisteredCommand,
|
||||
} from "./types.js";
|
||||
import type { ExecOptions, HookAPI, HookFactory, HookMessageRenderer, RegisteredCommand } from "./types.js";
|
||||
|
||||
// Create require function to resolve module paths at runtime
|
||||
const require = createRequire(import.meta.url);
|
||||
|
|
@ -56,7 +50,10 @@ type HandlerFn = (...args: unknown[]) => Promise<unknown>;
|
|||
/**
|
||||
* Send message handler type for pi.sendMessage().
|
||||
*/
|
||||
export type SendMessageHandler = <T = unknown>(message: HookMessage<T>, triggerTurn?: boolean) => void;
|
||||
export type SendMessageHandler = <T = unknown>(
|
||||
message: Pick<HookMessage<T>, "customType" | "content" | "display" | "details">,
|
||||
triggerTurn?: boolean,
|
||||
) => void;
|
||||
|
||||
/**
|
||||
* Append entry handler type for pi.appendEntry().
|
||||
|
|
|
|||
|
|
@ -11,8 +11,9 @@ import type { Component } from "@mariozechner/pi-tui";
|
|||
import type { Theme } from "../../modes/interactive/theme/theme.js";
|
||||
import type { CompactionPreparation, CompactionResult } from "../compaction.js";
|
||||
import type { ExecOptions, ExecResult } from "../exec.js";
|
||||
import type { HookMessage } from "../messages.js";
|
||||
import type { ModelRegistry } from "../model-registry.js";
|
||||
import type { CompactionEntry, CustomMessageEntry, SessionManager } from "../session-manager.js";
|
||||
import type { CompactionEntry, SessionManager } from "../session-manager.js";
|
||||
import type { EditToolDetails } from "../tools/edit.js";
|
||||
import type {
|
||||
BashToolDetails,
|
||||
|
|
@ -380,14 +381,6 @@ export interface SessionEventResult {
|
|||
*/
|
||||
export type HookHandler<E, R = void> = (event: E, ctx: HookEventContext) => Promise<R>;
|
||||
|
||||
/**
|
||||
* Options passed to custom message renderers.
|
||||
*/
|
||||
/**
|
||||
* Message type for hooks to send. Creates CustomMessageEntry in the session.
|
||||
*/
|
||||
export type HookMessage<T = unknown> = Pick<CustomMessageEntry<T>, "customType" | "content" | "display" | "details">;
|
||||
|
||||
export interface HookMessageRenderOptions {
|
||||
/** Whether the view is expanded */
|
||||
expanded: boolean;
|
||||
|
|
@ -463,7 +456,10 @@ export interface HookAPI {
|
|||
* @param triggerTurn - If true and agent is idle, triggers a new LLM turn. Default: false.
|
||||
* If agent is streaming, message is queued and triggerTurn is ignored.
|
||||
*/
|
||||
sendMessage<T = unknown>(message: HookMessage<T>, triggerTurn?: boolean): void;
|
||||
sendMessage<T = unknown>(
|
||||
message: Pick<HookMessage<T>, "customType" | "content" | "display" | "details">,
|
||||
triggerTurn?: boolean,
|
||||
): void;
|
||||
|
||||
/**
|
||||
* Append a custom entry to the session for hook state persistence.
|
||||
|
|
|
|||
|
|
@ -32,10 +32,10 @@ import type { ImageContent, TextContent } from "@mariozechner/pi-ai";
|
|||
* Message type for hook-injected messages via sendMessage().
|
||||
* These are custom messages that hooks can inject into the conversation.
|
||||
*/
|
||||
export interface HookAppMessage<T = unknown> {
|
||||
export interface HookMessage<T = unknown> {
|
||||
role: "hookMessage";
|
||||
customType: string;
|
||||
content: (TextContent | ImageContent)[];
|
||||
content: string | (TextContent | ImageContent)[];
|
||||
display: boolean;
|
||||
details?: T;
|
||||
timestamp: number;
|
||||
|
|
@ -45,7 +45,7 @@ export interface HookAppMessage<T = unknown> {
|
|||
declare module "@mariozechner/pi-agent-core" {
|
||||
interface CustomMessages {
|
||||
bashExecution: BashExecutionMessage;
|
||||
hookMessage: HookAppMessage;
|
||||
hookMessage: HookMessage;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -63,8 +63,8 @@ export function isBashExecutionMessage(msg: AppMessage | Message): msg is BashEx
|
|||
/**
|
||||
* Type guard for HookAppMessage.
|
||||
*/
|
||||
export function isHookAppMessage(msg: AppMessage | Message): msg is HookAppMessage {
|
||||
return (msg as HookAppMessage).role === "hookMessage";
|
||||
export function isHookMessage(msg: AppMessage | Message): msg is HookMessage {
|
||||
return (msg as HookMessage).role === "hookMessage";
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
|
|
@ -114,7 +114,7 @@ export function messageTransformer(messages: AppMessage[]): Message[] {
|
|||
timestamp: m.timestamp,
|
||||
};
|
||||
}
|
||||
if (isHookAppMessage(m)) {
|
||||
if (isHookMessage(m)) {
|
||||
// Convert hook message to user message for LLM
|
||||
return {
|
||||
role: "user",
|
||||
|
|
|
|||
|
|
@ -131,8 +131,6 @@ export interface SessionTreeNode {
|
|||
|
||||
export interface SessionContext {
|
||||
messages: AppMessage[];
|
||||
/** Entries in the current path (root to leaf). Use to identify custom_message entries for rendering. */
|
||||
entries: SessionEntry[];
|
||||
thinkingLevel: string;
|
||||
model: { provider: string; modelId: string } | null;
|
||||
}
|
||||
|
|
@ -292,7 +290,7 @@ export function buildSessionContext(
|
|||
}
|
||||
|
||||
if (!leaf) {
|
||||
return { messages: [], entries: [], thinkingLevel: "off", model: null };
|
||||
return { messages: [], thinkingLevel: "off", model: null };
|
||||
}
|
||||
|
||||
// Walk from leaf to root, collecting path
|
||||
|
|
@ -326,12 +324,10 @@ export function buildSessionContext(
|
|||
// 2. Emit kept messages (from firstKeptEntryId up to compaction)
|
||||
// 3. Emit messages after compaction
|
||||
const messages: AppMessage[] = [];
|
||||
const contextEntries: SessionEntry[] = [];
|
||||
|
||||
if (compaction) {
|
||||
// Emit summary first
|
||||
messages.push(createSummaryMessage(compaction.summary, compaction.timestamp));
|
||||
contextEntries.push(compaction);
|
||||
|
||||
// Find compaction index in path
|
||||
const compactionIdx = path.findIndex((e) => e.type === "compaction" && e.id === compaction.id);
|
||||
|
|
@ -346,13 +342,10 @@ export function buildSessionContext(
|
|||
if (foundFirstKept) {
|
||||
if (entry.type === "message") {
|
||||
messages.push(entry.message);
|
||||
contextEntries.push(entry);
|
||||
} else if (entry.type === "custom_message") {
|
||||
messages.push(createCustomMessage(entry));
|
||||
contextEntries.push(entry);
|
||||
} else if (entry.type === "branch_summary") {
|
||||
messages.push(createSummaryMessage(entry.summary, entry.timestamp));
|
||||
contextEntries.push(entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -362,13 +355,10 @@ export function buildSessionContext(
|
|||
const entry = path[i];
|
||||
if (entry.type === "message") {
|
||||
messages.push(entry.message);
|
||||
contextEntries.push(entry);
|
||||
} else if (entry.type === "custom_message") {
|
||||
messages.push(createCustomMessage(entry));
|
||||
contextEntries.push(entry);
|
||||
} else if (entry.type === "branch_summary") {
|
||||
messages.push(createSummaryMessage(entry.summary, entry.timestamp));
|
||||
contextEntries.push(entry);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
|
@ -376,18 +366,15 @@ export function buildSessionContext(
|
|||
for (const entry of path) {
|
||||
if (entry.type === "message") {
|
||||
messages.push(entry.message);
|
||||
contextEntries.push(entry);
|
||||
} else if (entry.type === "custom_message") {
|
||||
messages.push(createCustomMessage(entry));
|
||||
contextEntries.push(entry);
|
||||
} else if (entry.type === "branch_summary") {
|
||||
messages.push(createSummaryMessage(entry.summary, entry.timestamp));
|
||||
contextEntries.push(entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { messages, entries: contextEntries, thinkingLevel, model };
|
||||
return { messages, thinkingLevel, model };
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue