Phase 2 part 4: discovery per signal (Claude web search, explee.com)

Each signal's page can search for organizations that show it, by hand or
every 7, 14 or 30 days. Claude with the web_search_20260209 server tool
(pause_turn resumed) works from the signal's "How to spot it" lines and
the profile's targets; a company is kept only if its evidence URL was
among the pages searched. explee.com's company search runs the signal's
description in the target sizes, scored against the hypothesis, keeping
strong matches. Our sites, the inventory, existing prospects, CRM
Accounts and suppressed domains are left out. New companies go to Review
tagged with the signal. Schema 8 adds discovery_runs (counts, searches,
why companies were left out) and the signal's schedule and explee.com
description.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-21 18:27:35 -07:00
co-authored by Claude Opus 5.5
parent b025a97e4a
commit 157902f7e2
15 changed files with 1234 additions and 15 deletions
+37 -2
View File
@@ -26,14 +26,16 @@ const DefaultAnthropicModel = "claude-opus-5-5"
// Tasks the app asks a model to do. Each can use its own model.
const (
TaskProfile = "profile" // drafting the company profile, or an inventory entry, from a website
TaskContent = "content" // writing posts
TaskProfile = "profile" // drafting the company profile, or an inventory entry, from a website
TaskContent = "content" // writing posts
TaskProspects = "prospects" // finding companies that show a buying signal (with web search)
)
// Tasks lists the tasks with a description, for the settings page.
var Tasks = []struct{ ID, Label string }{
{TaskProfile, "Drafting the profile and filling in inventory entries"},
{TaskContent, "Writing posts"},
{TaskProspects, "Finding prospects for a buying signal (needs Claude, for web search)"},
}
// Config is the AI setting.
@@ -133,6 +135,39 @@ type Client interface {
Generate(ctx context.Context, req Request) (string, error)
}
// ResearchRequest is a prompt answered with web search.
type ResearchRequest struct {
Task string
System string
Prompt string
MaxTokens int
MaxSearches int // web searches allowed (each is billed)
BlockedDomains []string // never search these
}
// Source is a web page the model saw.
type Source struct{ URL, Title string }
// ResearchResult is the reply, the pages behind it, and how many searches
// it took.
type ResearchResult struct {
Text string
Sources []Source
Searches int
SearchErrors []string
}
// Researcher is a client that can search the web. Only Claude can.
type Researcher interface {
Research(ctx context.Context, req ResearchRequest) (*ResearchResult, error)
}
// ErrNoResearch means the AI provider can't search the web.
var ErrNoResearch = errors.New("web research needs Claude (Anthropic) as the AI provider; other servers can't search the web")
// DecodeJSON reads the outermost JSON object in a reply.
func DecodeJSON(text string, out any) error { return decodeJSON(text, out) }
// ErrNotConfigured means the provider can't be used yet (no key, no server).
var ErrNotConfigured = errors.New("AI provider not configured")
+104
View File
@@ -98,3 +98,107 @@ func (a *anthropicClient) Generate(ctx context.Context, req Request) (string, er
}
return text, nil
}
// Research runs a prompt with Claude's web search tool and returns the
// reply with every page the model saw (search results and citations), so
// callers can check that what it reports came from a real page.
func (a *anthropicClient) Research(ctx context.Context, req ResearchRequest) (*ResearchResult, error) {
model := a.cfg.ModelFor(req.Task)
maxTokens := req.MaxTokens
if maxTokens <= 0 {
maxTokens = 16000
}
maxSearches := req.MaxSearches
if maxSearches <= 0 {
maxSearches = 5
}
search := anthropic.BetaWebSearchTool20260209Param{MaxUses: anthropic.Int(int64(maxSearches))}
if len(req.BlockedDomains) > 0 {
search.BlockedDomains = req.BlockedDomains
}
params := anthropic.BetaMessageNewParams{
Model: anthropic.Model(model),
MaxTokens: int64(maxTokens),
Tools: []anthropic.BetaToolUnionParam{{OfWebSearchTool20260209: &search}},
Messages: []anthropic.BetaMessageParam{
anthropic.NewBetaUserMessage(anthropic.NewBetaTextBlock(req.Prompt)),
},
}
if req.System != "" {
params.System = []anthropic.BetaTextBlockParam{{Text: req.System}}
}
if effort := a.cfg.Effort; effort != "" {
params.OutputConfig.Effort = anthropic.BetaOutputConfigEffort(effort)
}
if fallbackModels[model] {
params.Betas = append(params.Betas, anthropic.AnthropicBetaServerSideFallback2026_07_01)
params.Fallbacks = anthropic.BetaFallbacksParamOfDefault()
}
res := &ResearchResult{}
seen := map[string]bool{}
addSource := func(url, title string) {
if url != "" && !seen[url] {
seen[url] = true
res.Sources = append(res.Sources, Source{URL: url, Title: title})
}
}
// The server runs the searches; a long run pauses (pause_turn) and is
// resumed by sending its reply back as it was.
for turn := 0; turn < 6; turn++ {
msg, err := a.client.Beta.Messages.New(ctx, params)
if err != nil {
var apierr *anthropic.Error
if errors.As(err, &apierr) {
switch apierr.StatusCode {
case 401:
return nil, fmt.Errorf("Anthropic rejected the API key (401); check it in Settings")
case 404:
return nil, fmt.Errorf("Anthropic doesn't know the model %q (404); choose another in Settings", model)
case 429:
return nil, fmt.Errorf("Anthropic rate limit reached (429); try again shortly")
}
}
return nil, fmt.Errorf("Anthropic: %w", err)
}
res.Searches += int(msg.Usage.ServerToolUse.WebSearchRequests)
var text strings.Builder
for _, block := range msg.Content {
switch b := block.AsAny().(type) {
case anthropic.BetaTextBlock:
text.WriteString(b.Text)
for _, c := range b.Citations {
if loc, ok := c.AsAny().(anthropic.BetaCitationsWebSearchResultLocation); ok {
addSource(loc.URL, loc.Title)
}
}
case anthropic.BetaWebSearchToolResultBlock:
// On success the content is a list of results; on failure
// it's an error object.
for _, r := range b.Content.OfBetaWebSearchResultBlockArray {
addSource(r.URL, r.Title)
}
if b.Content.ErrorCode != "" {
res.SearchErrors = append(res.SearchErrors, string(b.Content.ErrorCode))
}
}
}
if t := strings.TrimSpace(text.String()); t != "" {
res.Text = t // the last turn's text holds the answer
}
switch msg.StopReason {
case anthropic.BetaStopReasonPauseTurn:
params.Messages = append(params.Messages, msg.ToParam())
continue
case anthropic.BetaStopReasonRefusal:
return res, fmt.Errorf("the model declined this request (%s)", msg.StopDetails.Category)
case anthropic.BetaStopReasonMaxTokens:
return res, fmt.Errorf("the reply was cut off at %d tokens", maxTokens)
}
if res.Text == "" {
return res, fmt.Errorf("the model returned no text (stop reason %s)", msg.StopReason)
}
return res, nil
}
return res, errors.New("the research didn't finish after several continuations")
}