Files
Pulse/internal/api/chat_service_adapter.go
rcourtman 70eb2262a9 Ask once for a finding decision when an alert run records none
A scoped run on a resource with an active alert sometimes ends with
the fault described in prose and nothing recorded. On a rig one model
recorded a finding on one of three manual runs on a crashed worker and
wrote the fault in prose the other two times, so the same fault went
from fixed to not looked at between two runs of one build.

Pulse cannot tell that prose from a considered "nothing to do", and a
finished job that a person rechecks must stay quiet, so Pulse does not
file anything itself for these runs. The model is asked instead, once
and in the same conversation: record what you described, or say that
nothing needs recording.

Patrol hands the chat loop the question for a scoped run whose
resources have an active alert. The loop asks it only when a Watch run
ends a turn with text, no tool call and no finding decision so far. The
turn that answers is the existing final decision turn: report and
assess tools only. It is never asked twice, never after a turn that was
already decision-only, and never in a separate model session.

Cost: one more provider call for such a run when it recorded nothing,
and one closing call more when the answer is a recorded finding. A run
that records a finding by itself, and every run on a resource with no
active alert, costs what it did before.

Contract-Neutral: agent-lifecycle and storage-recovery name internal/api/ as an extension point. This change adds one internal field to the Patrol chat bridge and touches nothing those two contracts describe. ai-runtime.md and api-contracts.md carry the contract notes, and the bridge contract test pins the field.
2026-11-10 22:27:45 +00:00

132 lines
4.4 KiB
Go

package api
import (
"context"
"encoding/json"
"github.com/rcourtman/pulse-go-rewrite/internal/ai"
"github.com/rcourtman/pulse-go-rewrite/internal/ai/chat"
"github.com/rcourtman/pulse-go-rewrite/internal/ai/providers"
"github.com/rcourtman/pulse-go-rewrite/internal/ai/tools"
"github.com/rcourtman/pulse-go-rewrite/internal/config"
)
// chatServiceAdapter wraps chat.Service to implement ai.ChatServiceProvider.
// This bridges the chat package (concrete) to the ai package (interface) without
// creating an import cycle.
type chatServiceAdapter struct {
svc *chat.Service
}
func (a *chatServiceAdapter) CreateSession(ctx context.Context) (*ai.ChatSession, error) {
session, err := a.svc.CreateSession(ctx)
if err != nil {
return nil, err
}
return &ai.ChatSession{ID: session.ID}, nil
}
func (a *chatServiceAdapter) ExecuteStream(ctx context.Context, req ai.ChatExecuteRequest, callback ai.ChatStreamCallback) error {
return a.svc.ExecuteStream(ctx, chat.ExecuteRequest{
Prompt: req.Prompt,
SessionID: req.SessionID,
}, adaptCallback(callback))
}
func (a *chatServiceAdapter) ExecutePatrolStream(ctx context.Context, req ai.PatrolExecuteRequest, callback ai.ChatStreamCallback) (*ai.PatrolStreamResponse, error) {
resp, err := a.svc.ExecutePatrolStream(ctx, adaptPatrolExecuteRequest(req), adaptCallback(callback))
if err != nil {
if resp != nil {
return &ai.PatrolStreamResponse{
Content: resp.Content,
InputTokens: resp.InputTokens,
OutputTokens: resp.OutputTokens,
CacheCreationInputTokens: resp.CacheCreationInputTokens,
CacheReadInputTokens: resp.CacheReadInputTokens,
}, err
}
return nil, err
}
return &ai.PatrolStreamResponse{
Content: resp.Content,
InputTokens: resp.InputTokens,
OutputTokens: resp.OutputTokens,
CacheCreationInputTokens: resp.CacheCreationInputTokens,
CacheReadInputTokens: resp.CacheReadInputTokens,
}, nil
}
func adaptPatrolExecuteRequest(req ai.PatrolExecuteRequest) chat.PatrolRequest {
return chat.PatrolRequest{
Prompt: req.Prompt,
SystemPrompt: req.SystemPrompt,
SessionID: req.SessionID,
ExecutionID: req.ExecutionID,
UseCase: req.UseCase,
MaxTurns: req.MaxTurns,
AllowedToolNames: append([]string(nil), req.AllowedToolNames...),
FindingDecisionPrompt: req.FindingDecisionPrompt,
}
}
//nolint:dupl // mirrors orchestratorChatAdapter.GetMessages: same source messages mapped onto a deliberately separate output contract that may diverge
func (a *chatServiceAdapter) GetMessages(ctx context.Context, sessionID string) ([]ai.ChatMessage, error) {
messages, err := a.svc.GetMessages(ctx, sessionID)
if err != nil {
return nil, err
}
result := make([]ai.ChatMessage, len(messages))
for i, m := range messages {
result[i] = adaptChatMessage(m)
}
return result, nil
}
func adaptChatMessage(m chat.Message) ai.ChatMessage {
msg := ai.ChatMessage{
ID: m.ID,
Role: m.Role,
Content: m.Content,
ReasoningContent: m.ReasoningContent,
Timestamp: m.Timestamp,
}
for _, tc := range m.ToolCalls {
msg.ToolCalls = append(msg.ToolCalls, tc.NormalizeCollections())
}
if m.ToolResult != nil {
toolResult := *m.ToolResult
msg.ToolResult = &toolResult
}
return msg.NormalizeCollections()
}
func (a *chatServiceAdapter) DeleteSession(ctx context.Context, sessionID string) error {
return a.svc.DeleteSession(ctx, sessionID)
}
func (a *chatServiceAdapter) ReloadConfig(ctx context.Context, cfg *config.AIConfig) error {
return a.svc.Restart(ctx, cfg)
}
func (a *chatServiceAdapter) SetPatrolProviderFactory(factory func(string) (providers.StreamingProvider, error)) {
a.svc.SetPatrolProviderFactory(factory)
}
// GetExecutor exposes the underlying chat service's tool executor so that
// patrol can set the finding creator. This satisfies the
// chatServiceExecutorAccessor interface in the ai package.
func (a *chatServiceAdapter) GetExecutor() *tools.PulseToolExecutor {
return a.svc.GetExecutor()
}
// adaptCallback converts an ai.ChatStreamCallback to a chat.StreamCallback.
// The ai package uses []byte for event data, while the chat package uses json.RawMessage.
func adaptCallback(callback ai.ChatStreamCallback) chat.StreamCallback {
return func(event chat.StreamEvent) {
callback(ai.ChatStreamEvent{
Type: event.Type,
Data: json.RawMessage(event.Data),
})
}
}