# Demonstrates: Custom tools with statement composition, web fetching tools, inline tool definitions,
# match statements with stubbed data, LLM tool integration, template variables, UI hints
shared:
  data:
    metacritic_stub_data:
      "Soul": |
        Soul
        
        movie
        2020
        83
        
        The Story of Souleyman
        
        movie
        2024
        81
        
        Summer of Soul (...Or, When the Revolution Could Not Be Televised)
        
        movie
        2021
        96
        
        Lost Soulz
        
        movie
        2023
        74
        
        Dead Souls
        
        movie
        2018
        89

      "Wallace & Gromit: The Curse of the Were-Rabbit": |
        Wallace & Gromit: The Curse of the Were-Rabbit
        
          movie
          2005
          87
        
        Pirates of the Caribbean: The Curse of the Black Pearl
        
          movie
          2003
          63
        
        Wallace & Gromit: Vengeance Most Fowl
        
          movie
          2024
          83

      "The Incredibles": |
        The Incredibles
        
        movie
        2004
        90
        
        Incredibles 2
        
        movie
        2018
        80
        
        The Squid and the Whale
        
        movie
        2005
        82
        
        The Witcher: Nightmare of the Wolf
        
        movie
        2021
        67

  tools:
    # Stub for debugging without hammering actual Metacritics site
    - name: _search_movie_on_metacritic_stub
      description: >
        (debug) Searches for the Metacritic score for a given movie title. Returns text from Metacritic web site,
        containing the list of possible matches (movie title, release year, score).
      input:
        - name: movie_title
          description: Movie title
          type: str
      tools:
        - import_tool: llm_workers.tools.misc.user_input
      do:
        starlark: |-
          if movie_title in metacritic_stub_data:
              result = metacritic_stub_data[movie_title]
          else:
              result = user_input(prompt = f"Movie '{movie_title}' not found in stub data. Please provide mock Metacritic search results (movie titles, years, and scores):")
#        if: ${movie_title in metacritic_stub_data}
#        then:
#          eval: ${metacritic_stub_data[movie_title]}
#        else:
#          call: user_input
#          params:
#            prompt: "Movie '${movie_title}' not found in stub data. Please provide mock Metacritic search results (movie titles, years, and scores):"

    - name: metacritic_monkey
      description: >
        Finds the Metacritic score for a given movie title and year. Returns either a single number or "N/A" if the movie is not found.
      input:
        - name: movie_title
          description: Movie title
          type: str
        - name: movie_year
          description: Movie release year
          type: int
      ui_hint: Looking up Metacritic score for movie "${movie_title}" (${movie_year})
      tools:
        - import_tool: llm_workers.tools.llm_tool.build_llm_tool
          name: llm_tool
          config:
            model_ref: fast
          ui_hint: Extracting Metacritic score from search results
      do:
        #        - call:
        #            import_tool: llm_workers.tools.fetch.fetch_page_text
        #          params:
        #            url: "https://www.metacritic.com/search/${movie_title}/?page=1&category=2"
        #            xpath: "//*[@class=\"c-pageSiteSearch-results\"]"
        # replace block above with commented out block below for debugging with stub data
        - call: _search_movie_on_metacritic_stub
          params:
            movie_title: "${movie_title}"
        - call: llm_tool
          params:
            prompt: >
              Find Metacritic score for movie "${movie_title}" released in ${movie_year}.
              To do so:
                - From the list of possible matches, chose the one matching movie title and year and return metacritic score as single number
                - If no matching movie is found, respond with just "N/A" (without quotes)
                - DO NOT provide any additional information in the response

              Possible matches:
              ${_}

chat:
  default_prompt: Please find metacritic score for the movie "Soul" (2020).
  tools:
    - metacritic_monkey

cli:
  process_input: one_by_one
  tools:
    - metacritic_monkey
  do:
    - eval: "${parse_json(input)}"
    - call: metacritic_monkey
      params:
        movie_title: "${_.title}"
        movie_year: "${_.year}"
    
