Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/fold-core/src/HookRunner/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ export const PostToolUseHookInput = Schema.Struct({
parentAgentId: Schema.NullOr(AgentId),
toolCallId: ToolCallId,
toolName: Schema.String,
originalInput: Schema.Unknown,
executedInput: Schema.Unknown,
handlerResult: Schema.Unknown,
result: Schema.Unknown,
isFailure: Schema.Boolean,
}).annotate({ identifier: 'PostToolUseHookInput' })
Expand Down
7 changes: 7 additions & 0 deletions packages/fold-core/src/ToolRuntime/ToolRuntimeLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,8 @@ const finalOutputAfterPostToolHooks = (input: {
readonly parentAgentId: AgentId | null
readonly toolCallId: ToolCallId
readonly toolName: string
readonly originalInput: unknown
readonly executedInput: unknown
readonly output: FinalToolOutput
}): Effect.Effect<FinalToolOutput, HookExecutionError, HookRunner | StopController> =>
Effect.gen(function* () {
Expand All @@ -360,6 +362,9 @@ const finalOutputAfterPostToolHooks = (input: {
parentAgentId: input.parentAgentId,
toolCallId: input.toolCallId,
toolName: input.toolName,
originalInput: input.originalInput,
executedInput: input.executedInput,
handlerResult: input.output.result,
result: input.output.result,
isFailure: input.output.isFailure,
})
Expand Down Expand Up @@ -458,6 +463,8 @@ const settlePreparedToolCall = (input: {
parentAgentId: input.parentAgentId,
toolCallId,
toolName,
originalInput: input.prepared.original.params,
executedInput: input.prepared.params,
output: handlerOutput,
})

Expand Down
3 changes: 3 additions & 0 deletions packages/fold-core/test/HookRunner/PassThrough.vi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ it.effect('layerNoHooks returns pass-through decisions', () =>
parentAgentId: null,
toolCallId,
toolName: 'echo',
originalInput: params,
executedInput: params,
handlerResult: { echoed: 'hello' },
result: { echoed: 'hello' },
isFailure: false,
})
Expand Down
9 changes: 9 additions & 0 deletions packages/fold-core/test/HookRunner/PostToolUseHook.vi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ describe('HookRunner postToolUse hooks', () => {
parentAgentId: null,
toolCallId: ToolCallId.create(),
toolName: 'echo',
originalInput: { text: 'original' },
executedInput: { text: 'executed' },
handlerResult: { step: 0 },
result: { step: 0 },
isFailure: false,
})
Expand Down Expand Up @@ -71,6 +74,9 @@ describe('HookRunner postToolUse hooks', () => {
parentAgentId: null,
toolCallId: ToolCallId.create(),
toolName: 'echo',
originalInput: { text: 'original' },
executedInput: { text: 'executed' },
handlerResult: { original: true },
result: { original: true },
isFailure: false,
})
Expand Down Expand Up @@ -112,6 +118,9 @@ describe('HookRunner postToolUse hooks', () => {
parentAgentId: null,
toolCallId: ToolCallId.create(),
toolName: 'echo',
originalInput: { text: 'original' },
executedInput: { text: 'executed' },
handlerResult: { original: true },
result: { original: true },
isFailure: false,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,62 @@ it.effect('live preToolUse hook can update execution params without changing pro
expect(projectedToolResult).not.toHaveProperty('executedInput')
}),
)

it.effect('postToolUse receives original, executed, and handler values while hooks compose in order', () =>
Effect.gen(function* () {
const recorder = yield* makeEchoRecorder()
const observed = yield* Ref.make<ReadonlyArray<unknown>>([])
const hookLayer = makeHookRunner({
preToolUse: [
{
name: 'mutate-echo',
tools: ['echo'],
handler: () => Effect.succeed({ _tag: 'continue' as const, params: { text: 'executed' } }),
},
],
postToolUse: [
{
name: 'decorate-echo',
tools: ['echo'],
handler: (input) =>
Ref.update(observed, (values) => [...values, input]).pipe(
Effect.as({ _tag: 'replace' as const, result: { decorated: true }, isFailure: false }),
),
},
{
name: 'observe-decorated-echo',
tools: ['echo'],
handler: (input) =>
Ref.update(observed, (values) => [...values, input]).pipe(Effect.as({ _tag: 'keep' as const })),
},
],
})
const layer = toolRuntimeBaseLayer(hookLayer, layerEchoTool(recorder))

const settlement = yield* Effect.gen(function* () {
const runtime = yield* ToolRuntime
return yield* runtime.settle({
agentId,
parentAgentId: null,
assistantMessage: makeAssistantToolCall({ text: 'original' }),
})
}).pipe(Effect.provide(layer))

const calls = yield* Ref.get(observed)
expect(calls).toHaveLength(2)
expect(calls[0]).toMatchObject({
originalInput: { text: 'original' },
executedInput: { text: 'executed' },
handlerResult: { echoed: 'executed' },
result: { echoed: 'executed' },
})
expect(calls[1]).toMatchObject({
originalInput: { text: 'original' },
executedInput: { text: 'executed' },
handlerResult: { echoed: 'executed' },
result: { decorated: true },
})
expect(settlement.toolResults[0]?.executedInput).toEqual({ text: 'executed' })
expect(settlement.toolResults[0]?.message.content[0]).toMatchObject({ result: { decorated: true } })
}),
)
Loading