feat(audit): Phase 3 - Integrate audit logging with tools registry

Add audit logging to ExecuteWithContext:
- Log all tool executions with args, result, duration
- Log tool not found errors
- Include channel context for request tracing
- Works alongside existing structured logging

The audit logs capture:
- Tool name and ID
- Arguments (JSON-serializable)
- Result content
- Error status and async status
- Execution duration in milliseconds
This commit is contained in:
Vishnuvardhan Reddy 2026-03-01 15:23:48 +00:00
parent ecfe936b1b
commit 19018a3107

View file

@ -7,6 +7,7 @@ import (
"sync" "sync"
"time" "time"
"github.com/sipeed/picoclaw/pkg/audit"
"github.com/sipeed/picoclaw/pkg/logger" "github.com/sipeed/picoclaw/pkg/logger"
"github.com/sipeed/picoclaw/pkg/providers" "github.com/sipeed/picoclaw/pkg/providers"
) )
@ -61,6 +62,8 @@ func (r *ToolRegistry) ExecuteWithContext(
map[string]any{ map[string]any{
"tool": name, "tool": name,
}) })
// Audit the error
audit.LogError(ctx, "tool_not_found", fmt.Sprintf("tool %q not found", name), true)
return ErrorResult(fmt.Sprintf("tool %q not found", name)).WithError(fmt.Errorf("tool not found")) return ErrorResult(fmt.Sprintf("tool %q not found", name)).WithError(fmt.Errorf("tool not found"))
} }
@ -105,6 +108,17 @@ func (r *ToolRegistry) ExecuteWithContext(
}) })
} }
// Audit logging
ctx = audit.WithChannelContext(ctx, channel, chatID, "")
audit.LogToolCall(ctx, &audit.ToolCallData{
ToolID: name,
Name: name,
Arguments: args,
Result: result.ForLLM,
IsError: result.IsError,
IsAsync: result.Async,
}, duration.Milliseconds())
return result return result
} }