I've been trying to implement a custom strategy th...
# koog-agentic-framework
p
I've been trying to implement a custom strategy that requires user approval before executing any tool call. The user can either approve the tool call, or reject it and provide feedback. This feedback is then passed back to the LLM to guide its next step. Approval is working, but rejection is not with the following error:
Copy code
java.lang.IllegalStateException: Error from OpenAILLMClient API: 400 Bad Request: {
  "error": {
    "message": "An assistant message with 'tool_calls' must be followed by tool messages responding to each 'tool_call_id'. The following tool_call_ids did not have response messages: call_AiXXX",
    "type": "invalid_request_error",
    "param": "messages.[5].role",
    "code": null
  }
}
Here is a snippet of the code:
Copy code
edge(nodeStart forwardTo nodeCallLLM)
edge((nodeCallLLM forwardTo askUserPermissionForToolCall) onToolCall { true })
edge((askUserPermissionForToolCall forwardTo nodeExecuteTool) onToolCallApproved { true })
edge((askUserPermissionForToolCall forwardTo nodeCallLLM) onToolCallRejected { true })

@EdgeTransformationDslMarker
infix fun <IncomingOutput, IntermediateOutput, OutgoingInput> AIAgentEdgeBuilderIntermediate<IncomingOutput, IntermediateOutput, OutgoingInput>.onToolCallRejected(
    block: suspend (ToolExecutionConsent.Rejected) -> Boolean
): AIAgentEdgeBuilderIntermediate<IncomingOutput, String, OutgoingInput> {
    return onIsInstance(ToolExecutionConsent.Rejected::class)
        .onCondition { signature -> block(signature) }
        .transformed { it.feedback }
}
My intuition is that I need to return a
ReceivedToolResult
with the rejection message (instead of just the feedback) and forward it to
nodeLLMSendToolResult()
instead of
nodeLLMRequest()
. Wondering if anyone tried to implement something similar and how to make it work.
d
Yeah, that's exactly what I am doing: Whether or not the call was approved, return a ReceivedToolResult If it is a rejection, explain it is the ReceivedToolResult
@Pedro Ribeiro this is my custom node for executing some vetted tool calls, if that can inspire you: https://github.com/ptitjes/konvo/blob/develop/konvo-core%2Fsrc%2FcommonMain%2Fkotlin%2Fio%2Fgithub%2Fptitjes%2Fkonvo%2Fcore%2Fagents%2Ftoolkit%2FNodes.kt#L38 The VettedToolCall objects contain the tool call information and the result of the vetting process. That node
nodeExecuteVettedToolCalls
is handling parallel tool calls too.
👀 2
p
Nice, thanks 👍
a
A more idiomatic approach would be to ask for approval from the user inside the tool, and throw an error in case when the user did not approve:
Copy code
@Tool
fun myTool(args: Args): Result {
    if (!askForApproval()) error("User did not approve the tool call")
    // you tool code
}
Then koog will handle the error and generate the tool result with the error message as the result.
v
@Antonii Belyshev will it? I didn’t know we have this thing already 😅
p
Thanks for the reply. I prefer @Didier Villevalois's approach, as it allows me to add approvals for imported tools like
ReadFileTool
without overriding them.