Web Search Agent: Autonomous Researcher

Turn scales beautifully beyond local script boundaries. This is the code for the Autonomous Web Search Agent, showcasing how to seamlessly combine the HTTP standard library, Concurrency via Actor Models, and Stochastic native types.

This script executes an end-to-end operation by using a Master LLM to formulate 3 independent, distinct search trajectories, executes them synchronously using background actors, extracts the JSON HTTP responses, correctly models the text strings via semantic context.append, and constructs a structured analytical Markdown report summarizing the findings.

deep_research.tn
// ============================================================================
// Turn Language: Autonomous Deep Research Agent
// ============================================================================
use "std/http";
use "std/json";
use "std/fs";

struct SearchPlan {
  rationale: Str,
  query_1: Str,
  query_2: Str,
  query_3: Str
};

struct ResearchReport {
  title: Str,
  executive_summary: Str,
  key_findings: Str,
  conclusion: Str,
  confidence_score: Num
};

let search_knowledge_base = tool turn(query: Str) -> Str {
  call("echo", "[Search Agent] Executing remote GET request for: '" + query + "'");
  let endpoint = "https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=" + query + "&utf8=&format=json";
  let raw_response = call("http_get", endpoint);
  let parsed = call("json_parse", raw_response);
  
  let result_array = parsed["query"]["search"];
  if call("is_null", result_array) { return "No results found for query: " + query; }
  
  let obj = result_array[0]; let snippet1 = obj["snippet"];
  let obj2 = result_array[1]; let snippet2 = obj2["snippet"];
  return snippet1 + " " + snippet2;
};

let run_deep_research = tool turn(objective: Str) -> ResearchReport {
  let plan: SearchPlan = with budget(tokens: 1000) {
      infer SearchPlan {
          "You are an expert autonomous research planner.
           The user wants to know about: " + objective + "
           Break this down into exactly 3 highly specific search terms.
           CRITICAL REQUIREMENT: The queries MUST BE URL-encoded for an HTTP GET. 
           DO NOT INCLUDE SPACES. REPLACE ALL SPACES WITH %20.";
      };
  };
  
  let pid_1 = spawn linked turn() { return search_knowledge_base(plan.query_1); };
  let pid_2 = spawn linked turn() { return search_knowledge_base(plan.query_2); };
  let pid_3 = spawn linked turn() { return search_knowledge_base(plan.query_3); };
  
  let data_1 = await receive;
  let data_2 = await receive;
  let data_3 = await receive;
  
  let ans_1 = data_1["reason"];
  context.append(ans_1);
  let ans_2 = data_2["reason"];
  context.append(ans_2);
  let ans_3 = data_3["reason"];
  context.append(ans_3);
  
  let report: ResearchReport = with budget(tokens: 2000, time: 60) {
      infer ResearchReport {
          "You are a stellar analytical agent. Review the raw search snippets currently 
           in your context window regarding the objective: " + objective + "
           Synthesize a comprehensive, totally objective Markdown report. 
           Rate your confidence score from 0-100 based on data quality.";
      };
  };
  
  return report;
};