Obtain the function name, parameters, callId before the tool call, as well as the tool name, parameters, and tool execution result after the tool call in the final return result?

Comment From: zhoumiv

You can register an ObservationHandler that observes ToolCallingObservationContext into the Spring container:

@Slf4j
@Component
public class ToolCallingObservationHandler implements ObservationHandler<ToolCallingObservationContext> {

    @Override
    public void onStart(ToolCallingObservationContext context) {
        ToolDefinition toolDefinition = context.getToolDefinition();
        String toolName = toolDefinition.name();
        String arguments = context.getToolCallArguments();

        log.info("ToolCalling#onStart toolName: {}, args: {}", toolName, arguments);
    }

    @Override
    public void onStop(ToolCallingObservationContext context) {
        ToolDefinition toolDefinition = context.getToolDefinition();
        String toolName = toolDefinition.name();
        String result = context.getToolCallResult();

        log.info("ToolCalling#onStop toolName: {}, result: {}", toolName, result);
    }

    @Override
    public void onError(ToolCallingObservationContext context) {
        ToolDefinition toolDefinition = context.getToolDefinition();
        String toolName = toolDefinition.name();
        Throwable error = context.getError();
        log.error("ToolCalling#onError toolName: {}", toolName, error);
    }

    @Override
    public boolean supportsContext(@NonNull Observation.Context context) {
        return context instanceof ToolCallingObservationContext;
    }
}