mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-19 11:03:41 +00:00
Add ReadonlySessionManager type for hooks
Hooks now receive ReadonlySessionManager in contexts, which only exposes read methods. Writes must go through pi.sendMessage() or pi.appendEntry().
This commit is contained in:
parent
26e1c9d91c
commit
38d65dfe59
3 changed files with 39 additions and 9 deletions
|
|
@ -2,7 +2,7 @@
|
||||||
* Test hook demonstrating custom commands, message rendering, and before_agent_start.
|
* Test hook demonstrating custom commands, message rendering, and before_agent_start.
|
||||||
*/
|
*/
|
||||||
import type { BeforeAgentStartEvent, HookAPI } from "@mariozechner/pi-coding-agent";
|
import type { BeforeAgentStartEvent, HookAPI } from "@mariozechner/pi-coding-agent";
|
||||||
import { Box, Text } from "@mariozechner/pi-tui";
|
import { Box, Spacer, Text } from "@mariozechner/pi-tui";
|
||||||
|
|
||||||
export default function (pi: HookAPI) {
|
export default function (pi: HookAPI) {
|
||||||
// Track whether injection is enabled
|
// Track whether injection is enabled
|
||||||
|
|
@ -10,20 +10,22 @@ export default function (pi: HookAPI) {
|
||||||
|
|
||||||
// Register a custom message renderer for our "test-info" type
|
// Register a custom message renderer for our "test-info" type
|
||||||
pi.registerMessageRenderer("test-info", (message, options, theme) => {
|
pi.registerMessageRenderer("test-info", (message, options, theme) => {
|
||||||
const box = new Box(0, 0, (t) => theme.bg("customMessageBg", t));
|
const box = new Box(1, 1, (t) => theme.bg("customMessageBg", t));
|
||||||
|
|
||||||
const label = theme.fg("success", "[TEST INFO]");
|
const label = theme.fg("success", "[TEST INFO]");
|
||||||
box.addChild(new Text(label, 0, 0));
|
box.addChild(new Text(label, 0, 0));
|
||||||
|
box.addChild(new Spacer(1));
|
||||||
|
|
||||||
const content =
|
const content =
|
||||||
typeof message.content === "string"
|
typeof message.content === "string"
|
||||||
? message.content
|
? message.content
|
||||||
: message.content.map((c) => (c.type === "text" ? c.text : "[image]")).join("");
|
: message.content.map((c) => (c.type === "text" ? c.text : "[image]")).join("");
|
||||||
|
|
||||||
box.addChild(new Text(theme.fg("text", content), 0, 1));
|
box.addChild(new Text(theme.fg("text", content), 0, 0));
|
||||||
|
|
||||||
if (options.expanded && message.details) {
|
if (options.expanded && message.details) {
|
||||||
box.addChild(new Text(theme.fg("dim", `Details: ${JSON.stringify(message.details)}`), 0, 2));
|
box.addChild(new Spacer(1));
|
||||||
|
box.addChild(new Text(theme.fg("dim", `Details: ${JSON.stringify(message.details)}`), 0, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
return box;
|
return box;
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,7 @@ export type {
|
||||||
HookMessageRenderOptions,
|
HookMessageRenderOptions,
|
||||||
HookUIContext,
|
HookUIContext,
|
||||||
LsToolResultEvent,
|
LsToolResultEvent,
|
||||||
|
ReadonlySessionManager,
|
||||||
ReadToolResultEvent,
|
ReadToolResultEvent,
|
||||||
RegisteredCommand,
|
RegisteredCommand,
|
||||||
SessionEvent,
|
SessionEvent,
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,34 @@ import type { CompactionPreparation, CompactionResult } from "../compaction.js";
|
||||||
import type { ExecOptions, ExecResult } from "../exec.js";
|
import type { ExecOptions, ExecResult } from "../exec.js";
|
||||||
import type { HookMessage } from "../messages.js";
|
import type { HookMessage } from "../messages.js";
|
||||||
import type { ModelRegistry } from "../model-registry.js";
|
import type { ModelRegistry } from "../model-registry.js";
|
||||||
import type { CompactionEntry, SessionManager } from "../session-manager.js";
|
import type {
|
||||||
|
CompactionEntry,
|
||||||
|
SessionEntry,
|
||||||
|
SessionHeader,
|
||||||
|
SessionManager,
|
||||||
|
SessionTreeNode,
|
||||||
|
} from "../session-manager.js";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read-only view of SessionManager for hooks.
|
||||||
|
* Hooks should use pi.sendMessage() and pi.appendEntry() for writes.
|
||||||
|
*/
|
||||||
|
export type ReadonlySessionManager = Pick<
|
||||||
|
SessionManager,
|
||||||
|
| "getCwd"
|
||||||
|
| "getSessionDir"
|
||||||
|
| "getSessionId"
|
||||||
|
| "getSessionFile"
|
||||||
|
| "getLeafUuid"
|
||||||
|
| "getLeafEntry"
|
||||||
|
| "getEntry"
|
||||||
|
| "getLabel"
|
||||||
|
| "getPath"
|
||||||
|
| "getHeader"
|
||||||
|
| "getEntries"
|
||||||
|
| "getTree"
|
||||||
|
>;
|
||||||
|
|
||||||
import type { EditToolDetails } from "../tools/edit.js";
|
import type { EditToolDetails } from "../tools/edit.js";
|
||||||
import type {
|
import type {
|
||||||
BashToolDetails,
|
BashToolDetails,
|
||||||
|
|
@ -76,8 +103,8 @@ export interface HookEventContext {
|
||||||
hasUI: boolean;
|
hasUI: boolean;
|
||||||
/** Current working directory */
|
/** Current working directory */
|
||||||
cwd: string;
|
cwd: string;
|
||||||
/** Session manager instance - use for entries, session file, etc. */
|
/** Session manager (read-only) - use pi.sendMessage()/pi.appendEntry() for writes */
|
||||||
sessionManager: SessionManager;
|
sessionManager: ReadonlySessionManager;
|
||||||
/** Model registry - use for API key resolution and model retrieval */
|
/** Model registry - use for API key resolution and model retrieval */
|
||||||
modelRegistry: ModelRegistry;
|
modelRegistry: ModelRegistry;
|
||||||
}
|
}
|
||||||
|
|
@ -430,8 +457,8 @@ export interface HookCommandContext {
|
||||||
hasUI: boolean;
|
hasUI: boolean;
|
||||||
/** Current working directory */
|
/** Current working directory */
|
||||||
cwd: string;
|
cwd: string;
|
||||||
/** Session manager for reading/writing session entries */
|
/** Session manager (read-only) - use pi.sendMessage()/pi.appendEntry() for writes */
|
||||||
sessionManager: SessionManager;
|
sessionManager: ReadonlySessionManager;
|
||||||
/** Model registry for API keys */
|
/** Model registry for API keys */
|
||||||
modelRegistry: ModelRegistry;
|
modelRegistry: ModelRegistry;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue