Move exec to HookAPI, sessionManager/modelRegistry to HookEventContext

Breaking changes:
- HookEventContext now has sessionManager and modelRegistry (moved from SessionEventBase)
- HookAPI now has exec() method (moved from HookEventContext/HookCommandContext)
- HookRunner constructor takes sessionManager and modelRegistry as required params
- Session events no longer include sessionManager/modelRegistry fields

Hook code migration:
- event.sessionManager -> ctx.sessionManager
- event.modelRegistry -> ctx.modelRegistry
- ctx.exec() -> pi.exec()

Updated:
- src/core/hooks/types.ts - type changes
- src/core/hooks/runner.ts - constructor, createContext
- src/core/hooks/loader.ts - add exec to HookAPI
- src/core/sdk.ts - pass sessionManager/modelRegistry to HookRunner
- src/core/agent-session.ts - remove sessionManager/modelRegistry from events
- src/modes/* - remove setSessionFile calls, update events
- examples/hooks/* - update to new API
This commit is contained in:
Mario Zechner 2025-12-27 02:43:36 +01:00
parent 7ed8e2e9fc
commit 29fec7848e
14 changed files with 78 additions and 90 deletions

View file

@ -27,15 +27,13 @@ import {
} from "./compaction.js";
import type { LoadedCustomTool, SessionEvent as ToolSessionEvent } from "./custom-tools/index.js";
import { exportSessionToHtml } from "./export-html.js";
import {
type ExecOptions,
execCommand,
type HookCommandContext,
type HookMessage,
type HookRunner,
type SessionEventResult,
type TurnEndEvent,
type TurnStartEvent,
import type {
HookCommandContext,
HookMessage,
HookRunner,
SessionEventResult,
TurnEndEvent,
TurnStartEvent,
} from "./hooks/index.js";
import { type BashExecutionMessage, type HookAppMessage, isHookAppMessage } from "./messages.js";
import type { ModelRegistry } from "./model-registry.js";
@ -519,7 +517,6 @@ export class AgentSession {
cwd,
sessionManager: this.sessionManager,
modelRegistry: this._modelRegistry,
exec: (cmd: string, cmdArgs: string[], options?: ExecOptions) => execCommand(cmd, cmdArgs, cwd, options),
};
try {
@ -640,8 +637,6 @@ export class AgentSession {
if (this._hookRunner?.hasHandlers("session")) {
const result = (await this._hookRunner.emit({
type: "session",
sessionManager: this.sessionManager,
modelRegistry: this._modelRegistry,
reason: "before_new",
})) as SessionEventResult | undefined;
@ -659,11 +654,8 @@ export class AgentSession {
// Emit session event with reason "new" to hooks
if (this._hookRunner) {
this._hookRunner.setSessionFile(this.sessionFile);
await this._hookRunner.emit({
type: "session",
sessionManager: this.sessionManager,
modelRegistry: this._modelRegistry,
reason: "new",
});
}
@ -888,8 +880,6 @@ export class AgentSession {
const result = (await this._hookRunner.emit({
type: "session",
sessionManager: this.sessionManager,
modelRegistry: this._modelRegistry,
reason: "before_compact",
preparation,
previousCompactions,
@ -952,8 +942,6 @@ export class AgentSession {
if (this._hookRunner && savedCompactionEntry) {
await this._hookRunner.emit({
type: "session",
sessionManager: this.sessionManager,
modelRegistry: this._modelRegistry,
reason: "compact",
compactionEntry: savedCompactionEntry,
fromHook,
@ -1060,8 +1048,6 @@ export class AgentSession {
const hookResult = (await this._hookRunner.emit({
type: "session",
sessionManager: this.sessionManager,
modelRegistry: this._modelRegistry,
reason: "before_compact",
preparation,
previousCompactions,
@ -1125,8 +1111,6 @@ export class AgentSession {
if (this._hookRunner && savedCompactionEntry) {
await this._hookRunner.emit({
type: "session",
sessionManager: this.sessionManager,
modelRegistry: this._modelRegistry,
reason: "compact",
compactionEntry: savedCompactionEntry,
fromHook,
@ -1431,8 +1415,6 @@ export class AgentSession {
if (this._hookRunner?.hasHandlers("session")) {
const result = (await this._hookRunner.emit({
type: "session",
sessionManager: this.sessionManager,
modelRegistry: this._modelRegistry,
reason: "before_switch",
targetSessionFile: sessionPath,
})) as SessionEventResult | undefined;
@ -1454,11 +1436,8 @@ export class AgentSession {
// Emit session event to hooks
if (this._hookRunner) {
this._hookRunner.setSessionFile(sessionPath);
await this._hookRunner.emit({
type: "session",
sessionManager: this.sessionManager,
modelRegistry: this._modelRegistry,
reason: "switch",
previousSessionFile,
});
@ -1515,8 +1494,6 @@ export class AgentSession {
if (this._hookRunner?.hasHandlers("session")) {
const result = (await this._hookRunner.emit({
type: "session",
sessionManager: this.sessionManager,
modelRegistry: this._modelRegistry,
reason: "before_branch",
targetTurnIndex: entryIndex,
})) as SessionEventResult | undefined;
@ -1544,11 +1521,8 @@ export class AgentSession {
// Emit branch event to hooks (after branch completes)
if (this._hookRunner) {
this._hookRunner.setSessionFile(newSessionFile);
await this._hookRunner.emit({
type: "session",
sessionManager: this.sessionManager,
modelRegistry: this._modelRegistry,
reason: "branch",
targetTurnIndex: entryIndex,
});