Skip to content
Open
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
6 changes: 6 additions & 0 deletions lib/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,18 @@ export class Logger {
callID: part.callID,
}

if (part.state?.status) {
toolPart.status = part.state.status
}
if (part.state?.input) {
toolPart.input = part.state.input
}
if (part.state?.output) {
toolPart.output = part.state.output
}
if (part.state?.error) {
toolPart.error = part.state.error
}

return toolPart
}
Expand Down
4 changes: 3 additions & 1 deletion lib/state/tool-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ export async function syncToolCache(
turnProtectionTurns > 0 &&
state.currentTurn - turnCounter < turnProtectionTurns

state.lastToolPrune = part.tool === "discard" || part.tool === "extract"
state.lastToolPrune =
(part.tool === "discard" || part.tool === "extract") &&
part.state.status === "completed"

const allProtectedTools = config.tools.settings.protectedTools

Expand Down
30 changes: 22 additions & 8 deletions lib/strategies/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ async function executePruneOperation(

if (!ids || ids.length === 0) {
logger.debug(`${toolName} tool called but ids is empty or undefined`)
return `No IDs provided. Check the <prunable-tools> list for available IDs to ${toolName.toLowerCase()}.`
throw new Error(
`No IDs provided. Check the <prunable-tools> list for available IDs to ${toolName.toLowerCase()}.`,
)
}

const numericToolIds: number[] = ids
Expand All @@ -48,7 +50,7 @@ async function executePruneOperation(

if (numericToolIds.length === 0) {
logger.debug(`No numeric tool IDs provided for ${toolName}: ` + JSON.stringify(ids))
return "No numeric IDs provided. Format: ids: [id1, id2, ...]"
throw new Error("No numeric IDs provided. Format: ids: [id1, id2, ...]")
}

// Fetch messages to calculate tokens and find current agent
Expand All @@ -65,7 +67,9 @@ async function executePruneOperation(
// Validate that all numeric IDs are within bounds
if (numericToolIds.some((id) => id < 0 || id >= toolIdList.length)) {
logger.debug("Invalid tool IDs provided: " + numericToolIds.join(", "))
return "Invalid IDs provided. Only use numeric IDs from the <prunable-tools> list."
throw new Error(
"Invalid IDs provided. Only use numeric IDs from the <prunable-tools> list.",
)
}

// Validate that all IDs exist in cache and aren't protected
Expand All @@ -78,7 +82,9 @@ async function executePruneOperation(
"Rejecting prune request - ID not in cache (turn-protected or hallucinated)",
{ index, id },
)
return "Invalid IDs provided. Only use numeric IDs from the <prunable-tools> list."
throw new Error(
"Invalid IDs provided. Only use numeric IDs from the <prunable-tools> list.",
)
}
const allProtectedTools = config.tools.settings.protectedTools
if (allProtectedTools.includes(metadata.tool)) {
Expand All @@ -87,7 +93,9 @@ async function executePruneOperation(
id,
tool: metadata.tool,
})
return "Invalid IDs provided. Only use numeric IDs from the <prunable-tools> list."
throw new Error(
"Invalid IDs provided. Only use numeric IDs from the <prunable-tools> list.",
)
}

const filePath = getFilePathFromParameters(metadata.parameters)
Expand All @@ -98,7 +106,9 @@ async function executePruneOperation(
tool: metadata.tool,
filePath,
})
return "Invalid IDs provided. Only use numeric IDs from the <prunable-tools> list."
throw new Error(
"Invalid IDs provided. Only use numeric IDs from the <prunable-tools> list.",
)
}
}

Expand Down Expand Up @@ -158,7 +168,9 @@ export function createDiscardTool(ctx: PruneToolContext): ReturnType<typeof tool
const validReasons = ["completion", "noise"] as const
if (typeof reason !== "string" || !validReasons.includes(reason as any)) {
ctx.logger.debug("Invalid discard reason provided: " + reason)
return "No valid reason found. Use 'completion' or 'noise' as the first element."
throw new Error(
"No valid reason found. Use 'completion' or 'noise' as the first element.",
)
}

const numericIds = args.ids.slice(1)
Expand Down Expand Up @@ -186,7 +198,9 @@ export function createExtractTool(ctx: PruneToolContext): ReturnType<typeof tool
ctx.logger.debug(
"Extract tool called without distillation: " + JSON.stringify(args),
)
return "Missing distillation. You must provide a distillation string for each ID."
throw new Error(
"Missing distillation. You must provide a distillation string for each ID.",
)
}

// Log the distillation for debugging/analysis
Expand Down