> ## Documentation Index
> Fetch the complete documentation index at: https://docs.avibe.bot/llms.txt
> Use this file to discover all available pages before exploring further.

# Source filter

> Run your own Python scripts on Vibey's web search and web fetch: rewrite what goes out, and decide what comes back.

**Source filter** is an experimental feature. It puts your own scripts around two of Vibey's tools, **web search** (`web_search`) and **web fetch** (`web_fetch`). Before a call goes out, a script can rewrite the query or URL, answer the call itself, or block it. When the result comes back, a script can keep it, replace it entirely, or block it, before Vibey ever reads it.

Use it to keep Vibey to sources you trust, drop sites you don't, add a page you always want considered, or point reads at a mirror.

## Turn it on

Open **Settings › Experimental › Source filter** and switch on **Use source filter**. Only the Owner of the Avibe can change it. It applies from Vibey's next turn; turning it off restores the earlier behavior from the next turn.

While it is on:

* Vibey no longer uses a model's built-in web search; every search and every page read goes through Avibe's tools, and so through your scripts.
* Search services, fallback order, and billing work as before. When a search falls back to another provider, your scripts still run once per call, not once per attempt.
* It covers Vibey's web search and web fetch only. Other tools, MCP servers, the shell, and other Agents reach the web without passing through it.

## Where scripts go

Put your scripts in the hooks folder of your Avibe home (by default `~/.avibe/hooks/source_filter/`; the settings page shows the exact path):

| File | Runs |
| - | - |
| `before_search.py` | before a web search is sent |
| `after_search.py` | after a web search returns, before Vibey reads it |
| `before_fetch.py` | before a page is read |
| `after_fetch.py` | after a page is read, before Vibey reads it |

Every file is optional. A missing file is skipped, and with none of them the tools run as usual. Avibe looks for each file when its step comes, so adding or removing a script takes effect from the next call. The settings page shows which scripts it found, and the last run that failed.

## Python and dependencies

Scripts run with Python, without a shell, in the hooks folder.

* A script that uses only the standard library needs nothing installed: Avibe runs it with its own Python.
* For third-party packages, make the hooks folder a [uv](https://docs.astral.sh/uv/) project. All four scripts share it:

  ```bash theme={null}
  cd ~/.avibe/hooks/source_filter
  uv init --bare
  uv add httpx
  uv sync --locked
  ```

  When the folder has a `.venv`, Avibe runs the scripts with `.venv/bin/python` (`.venv\Scripts\python.exe` on Windows). Avibe never installs packages during a search; run `uv sync --locked` again after changing dependencies.

## The protocol

Each run reads one JSON request on standard input and writes one JSON response on standard output, both UTF-8. Write anything else, such as debug output, to standard error. A run has 600 seconds.

### Before a call

The request carries what Vibey asked for:

```json theme={null}
{"v": 1, "hook": "before_search", "args": {"query": "rust async runtimes"}}
```

`before_fetch` gets `{"url": "..."}` in `args`. Respond with one of:

| Response | Effect |
| - | - |
| `{"action": "pass"}` | send the call as it is |
| `{"action": "replace", "args": {"query": "..."}}` | send these arguments instead |
| `{"action": "respond", "output": {...}}` | answer the call yourself; nothing is sent |
| `{"action": "block", "reason": "..."}` | stop the call; Vibey sees that it was blocked, and why |

Arguments you write still go through the tool's usual checks when they are sent, including the page reader's network safety rules.

### After a call

The request adds what came back, as `output`, and where it came from, as `origin` (`"gateway"`, or `"before_hook"` when your before script answered):

```json theme={null}
{
  "v": 1,
  "hook": "after_search",
  "args": {"query": "rust async runtimes"},
  "origin": "gateway",
  "output": {"ok": true, "result": {"results": [
    {"url": "https://example.org/a", "title": "A", "snippet": "..."}
  ]}}
}
```

A failed call has `{"ok": false, "error": {"code": "...", "message": "..."}}` instead; a search that ended early also keeps the sources it found as `result`. Respond with `{"action": "pass"}`, `{"action": "replace", "output": {...}}`, or `{"action": "block", "reason": "..."}`.

### Output shapes

| Tool | `result` |
| - | - |
| web search | `{"results": [{"url", "title", "snippet", "page_age"}]}`, up to 50 results |
| web fetch | `{"url", "final_url", "title", "content"}` |

`url` is required for each search result, and `content` for a page; the other fields are optional. Your output can name any source and any content, and can turn a failure into a success or the reverse. Fields not listed here are not passed to Vibey.

## Call a model from a script

A script can ask one of your models for help, for example to judge which results look reliable. Every run gets two environment variables:

* `AVIBE_GATEWAY_URL`: the address of Avibe's local model gateway, OpenAI-compatible.
* `AVIBE_GATEWAY_KEY`: a key that works only while this run lasts.

Send a request to `$AVIBE_GATEWAY_URL/chat/completions` with the key as a bearer token, and name the model in the request: any model from Vibey's model menu. The call uses your model quota like any other.

The key only answers chat completions. A model called this way cannot search or read pages itself, so your scripts are never bypassed or called again from inside. Don't print the key, even to standard error.

Leave your HTTP call a shorter timeout than the 600-second run, so the script can still answer.

## When a script fails

If a script exits with an error, runs past 600 seconds, or writes something that is not a valid response, the call is blocked: Vibey is told the source filter blocked it, and never sees the unfiltered result. Nothing is retried. If you would rather let results through when your own code fails, catch the error in your script and respond `{"action": "pass"}`.

Whatever a script returns, Avibe still records what really ran upstream and what it cost.

## Examples

Drop some domains from search results, and always add one trusted page (`after_search.py`):

```python theme={null}
import json, sys
from urllib.parse import urlsplit

BLOCKED = {"spam.example", "contentfarm.example"}
PINNED = {"url": "https://docs.example/handbook", "title": "Team handbook"}

req = json.load(sys.stdin)
out = req["output"]
# A failed search may still carry the sources it found, and Vibey reads those too.
found = (out.get("result") or {}).get("results", [])
kept = [r for r in found if (urlsplit(r["url"]).hostname or "") not in BLOCKED]
if out["ok"]:
    kept = [PINNED, *kept]
out["result"] = {"results": kept[:50]}
json.dump({"action": "replace", "output": out}, sys.stdout)
```

Refuse to read internal addresses (`before_fetch.py`):

```python theme={null}
import json, sys
from urllib.parse import urlsplit

req = json.load(sys.stdin)
host = urlsplit(req["args"]["url"]).hostname or ""
if host.endswith(".internal.example"):
    json.dump({"action": "block", "reason": "internal address"}, sys.stdout)
else:
    json.dump({"action": "pass"}, sys.stdout)
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.