Behaviour for URL-specific content extraction strategies.
A strategy controls how a URL is fetched and how the response is
converted to text for LLM consumption. Strategies are matched against
URLs in order — the first strategy whose match?/2 returns true
handles the request.
Callbacks
match?/2(required) — returnstrueif this strategy handles the given URI.request/2(optional) — modifies theReq.Requestbefore execution (URL rewriting, custom headers, etc.).extract/2(required) — converts theReq.Responseinto a content string.
Example
defmodule MyApp.WikiStrategy do
@behaviour Omni.Tools.WebFetch.Strategy
@impl true
def match?(uri, _opts), do: uri.host == "en.wikipedia.org"
@impl true
def request(req, _opts) do
# Use the mobile API for cleaner content
Req.merge(req, headers: [{"accept", "text/html"}])
end
@impl true
def extract(response, _opts) do
Html2Markdown.convert(response.body)
end
endUsage
Pass strategies to Omni.Tools.WebFetch.new/1:
Omni.Tools.WebFetch.new(
strategies: [
{MyApp.WikiStrategy, []},
MyApp.AnotherStrategy
]
)
Summary
Callbacks
Extracts content from the response as a string.
Returns true if this strategy handles the given URI.
Modifies the Req.Request before execution.
Functions
Finds the first strategy matching the given URI.
Normalizes a list of strategy specs into {module, opts} tuples.
Callbacks
@callback extract(Req.Response.t(), opts :: keyword()) :: String.t()
Extracts content from the response as a string.
Returns true if this strategy handles the given URI.
@callback request(Req.Request.t(), opts :: keyword()) :: Req.Request.t()
Modifies the Req.Request before execution.
Functions
Finds the first strategy matching the given URI.
Returns {module, opts} for the first strategy whose match?/2
returns true, or nil if none match.
Normalizes a list of strategy specs into {module, opts} tuples.
Accepts bare modules or {module, opts} tuples. Validates that each
module implements the required callbacks. Raises ArgumentError on
invalid input.
Strategy.resolve([MyStrategy, {OtherStrategy, key: "val"}])
#=> [{MyStrategy, []}, {OtherStrategy, [key: "val"]}]