Extract agent and tools from tracker mapping worker into dedicated files
Split tracker_mapping_worker.go: agent construction, prompts, and structured output type move to tracker_mapping_agent.go; each tool gets its own *_tool.go file. Update naming conventions (worker, agent, tool file patterns, AgentResult suffix, RunTyped preference). Signed-off-by: Émile Ré <emile@probo.com>
This commit is contained in:
@@ -16,45 +16,26 @@ package cookiebanner
|
||||
|
||||
import (
|
||||
"context"
|
||||
_ "embed"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"go.gearno.de/kit/log"
|
||||
"go.gearno.de/kit/pg"
|
||||
"go.gearno.de/kit/worker"
|
||||
"go.probo.inc/probo/pkg/agent"
|
||||
"go.probo.inc/probo/pkg/agent/tools/search"
|
||||
"go.probo.inc/probo/pkg/coredata"
|
||||
"go.probo.inc/probo/pkg/gid"
|
||||
"go.probo.inc/probo/pkg/llm"
|
||||
"go.probo.inc/probo/pkg/slug"
|
||||
)
|
||||
|
||||
const (
|
||||
agentTimeout = 60 * time.Second
|
||||
agentMaxTurns = 5
|
||||
agentConfidenceThreshold = 0.6
|
||||
agentMaxPatternConfidence = 0.8
|
||||
)
|
||||
|
||||
//go:embed prompts/tracker_identification.txt.tmpl
|
||||
var trackerIdentificationPrompt string
|
||||
|
||||
type trackerMappingHandler struct {
|
||||
pg *pg.Client
|
||||
logger *log.Logger
|
||||
agent *agent.Agent
|
||||
}
|
||||
|
||||
type TrackerMappingConfig struct {
|
||||
LLMClient *llm.Client
|
||||
Model string
|
||||
FirecrawlAPIKey string
|
||||
}
|
||||
|
||||
func NewTrackerMappingWorker(
|
||||
pgClient *pg.Client,
|
||||
logger *log.Logger,
|
||||
@@ -78,52 +59,6 @@ func NewTrackerMappingWorker(
|
||||
)
|
||||
}
|
||||
|
||||
func buildTrackerMappingAgent(
|
||||
cfg TrackerMappingConfig,
|
||||
pgClient *pg.Client,
|
||||
logger *log.Logger,
|
||||
) *agent.Agent {
|
||||
tools := []agent.Tool{
|
||||
searchTrackerPatternsTool(pgClient),
|
||||
searchThirdPartiesTool(pgClient),
|
||||
}
|
||||
|
||||
if cfg.FirecrawlAPIKey != "" {
|
||||
tools = append(tools, search.FirecrawlSearchTool(cfg.FirecrawlAPIKey))
|
||||
}
|
||||
|
||||
outputType, err := agent.NewOutputType[TrackerIdentification]("tracker_identification")
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("cookiebanner: cannot build tracker identification output type: %s", err))
|
||||
}
|
||||
|
||||
return agent.New(
|
||||
"tracker-mapping",
|
||||
cfg.LLMClient,
|
||||
agent.WithInstructionsFunc(trackerMappingInstructions),
|
||||
agent.WithModel(cfg.Model),
|
||||
agent.WithTools(tools...),
|
||||
agent.WithOutputType(outputType),
|
||||
agent.WithMaxTurns(agentMaxTurns),
|
||||
agent.WithLogger(logger),
|
||||
)
|
||||
}
|
||||
|
||||
func trackerMappingInstructions(_ context.Context, _ *agent.Agent) string {
|
||||
categories := coredata.ThirdPartyCategories()
|
||||
parts := make([]string, len(categories))
|
||||
for i, c := range categories {
|
||||
parts[i] = string(c)
|
||||
}
|
||||
|
||||
return strings.Replace(
|
||||
trackerIdentificationPrompt,
|
||||
"{{.Categories}}",
|
||||
strings.Join(parts, ", "),
|
||||
1,
|
||||
)
|
||||
}
|
||||
|
||||
func (h *trackerMappingHandler) Claim(ctx context.Context) (coredata.TrackerPattern, error) {
|
||||
var tp coredata.TrackerPattern
|
||||
|
||||
@@ -292,7 +227,7 @@ func (h *trackerMappingHandler) identifyWithAgent(
|
||||
agentCtx, cancel := context.WithTimeout(ctx, agentTimeout)
|
||||
defer cancel()
|
||||
|
||||
result, err := agent.RunTyped[TrackerIdentification](
|
||||
result, err := agent.RunTyped[TrackerMappingAgentResult](
|
||||
agentCtx,
|
||||
h.agent,
|
||||
[]llm.Message{
|
||||
@@ -379,7 +314,7 @@ func (h *trackerMappingHandler) identifyWithAgent(
|
||||
func (h *trackerMappingHandler) resolveOrCreateCommonThirdParty(
|
||||
ctx context.Context,
|
||||
tx pg.Tx,
|
||||
identification TrackerIdentification,
|
||||
identification TrackerMappingAgentResult,
|
||||
domains []string,
|
||||
) (*gid.GID, error) {
|
||||
var party coredata.CommonThirdParty
|
||||
@@ -490,28 +425,3 @@ func (h *trackerMappingHandler) resolveThirdParty(
|
||||
|
||||
return &t.ID, nil
|
||||
}
|
||||
|
||||
func buildAgentPrompt(tp coredata.TrackerPattern, domains []string) string {
|
||||
maxAge := "session"
|
||||
if tp.MaxAgeSeconds != nil {
|
||||
maxAge = fmt.Sprintf("%d seconds", *tp.MaxAgeSeconds)
|
||||
}
|
||||
|
||||
prompt := fmt.Sprintf(
|
||||
"Identify the following tracker:\n\n"+
|
||||
"<pattern> %s </pattern>\n"+
|
||||
"<type> %s </type>\n"+
|
||||
"<match_type> %s </match_type>\n"+
|
||||
"<max_age> %s </max_age>\n",
|
||||
tp.Pattern,
|
||||
tp.TrackerType,
|
||||
tp.MatchType,
|
||||
maxAge,
|
||||
)
|
||||
|
||||
if len(domains) > 0 {
|
||||
prompt += fmt.Sprintf("<observed_domains> %s </observed_domains>\n", strings.Join(domains, ", "))
|
||||
}
|
||||
|
||||
return prompt
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user