# Link router

> Make Flow your default browser and route every web link by rules to a Flow tab, a PR review session, another browser or app, or a chooser.

The link router sends each web link to the right place. Set Flow as your Mac's default browser, and every http(s) link you click in Slack, Mail or any other app is checked against your rules, top to bottom. The first rule that matches decides where it opens: a Flow web tab, a PR review session, another browser or app, a custom target (Claude, ChatGPT, Obsidian…), or a menu that lets you pick. Links you open inside Flow can go through the same rules.

> **Note:** The link router is an experiment (ALPHA). Turn it on in Settings › Experiments (`features.link_router`).

While the experiment is off:

- Settings has no Link router section, and its settings don't show up in search.
- A link sent to Flow opens as a web tab, as before. With web tabs off and a fallback browser set (`link_router.fallback`), it opens in that browser. With web tabs off and no fallback, Flow shows an error toast.
- Links inside Flow open the way `links.open_in` says (see [Links you open inside Flow](#links-you-open-inside-flow)).

Your rules file and the `link_router.*` settings are kept either way.

## Make Flow the default browser

1. Turn on the experiment in Settings › Experiments.
2. Open Settings › Link router. The **Default browser** row shows which app opens links now.
3. Click **Make Flow the default browser**. macOS asks you to confirm.
4. If you had no fallback browser set, the browser you replaced becomes the fallback, so links no rule takes still open there.

Only the installed `Flow.app` can be the default browser. A development build run from a terminal has no bundle id, so the button is disabled. Try your rules with `flow-cli route-url` instead (see [Test a link](#test-a-link)).

## Rules

You edit rules in Settings › Link router › Rules. They live in `~/.flow/link_router.json`, one file for the whole Mac. Rules run top to bottom, and the first **enabled** rule whose pattern and source apps both match wins. Use ↑ and ↓ to reorder a rule, ✕ to delete it, its switch to turn it off, and **Add rule** to add one.

Each rule has:

- **Name**: a label for you.
- **Pattern**: which URLs it matches (see [Pattern syntax](#pattern-syntax)).
- **From apps**: bundle ids of the apps the link may come from, comma-separated (`com.tinyspeck.slackmacgap, com.apple.mail`). Leave it empty for any app. `flow` means links you open inside Flow.
- **Destination**: where a matching link opens.

| Destination | Opens the link | Skipped (next rule tried) when |
| --- | --- | --- |
| Flow tab | In a Flow web tab | Web tabs are off |
| PR review session | As a PR review session in Flow | The link isn't a GitHub pull request, or it's the pull request the surface you clicked already shows |
| An installed browser or app | In that app. Settings lists the apps that open https links; in the file, any bundle id | The rule names no app |
| Target: NAME | At the URL a [target](#targets) builds | The target is missing, or its extract pattern doesn't match |
| Fallback browser | In the fallback browser | Never |
| Ask (the chooser) | Shows the [chooser](#the-chooser) | Never |

Until the file exists, Flow starts with one rule, *GitHub pull requests* (`github.com/*/*/pull/*` → PR review session), and three targets.

### Pattern syntax

A pattern is a glob over `host[:port]/path[?query]`, without the scheme, or a regular expression between slashes.

| Pattern | Matches |
| --- | --- |
| `github.com/*/*/pull/*` | Host and path; `*` is any run of characters |
| `zoom.us` | A pattern with no `/` or `?` matches the host, with any path |
| `*.zoom.us` | Any subdomain, and `zoom.us` itself |
| `www.figma.com` | A leading `www.` is ignored on both sides, so this is `figma.com` |
| `localhost:3000` | A port, when the pattern names one |
| `example.com?tab=*` | A `?` right after the host: any path with that query |
| `/^https://docs\.rs\//` | `/…/` is a regular expression on the full URL |

- The host part of a glob is matched against the URL's host alone, so a `*` there never reaches into the path. A port counts only when the pattern names one.
- Hosts compare case-insensitively; paths and queries compare exactly.
- A glob never sees the fragment (`#…`). A `/regex/` runs on the whole URL, fragment included.
- A regular expression that doesn't compile never matches, and Settings shows why under the rule. An empty pattern matches nothing.

### When nothing matches

The link opens in the **fallback browser** (`link_router.fallback`). If that is empty, or names Flow itself, Flow uses Safari.

Flow never hands a link back to itself through macOS: a rule or target that names Flow's own app opens a Flow tab instead.

## Targets

A target turns the link into another URL and opens it. Edit them in Settings › Link router › Targets: a name, a URL template, **Open with**, and an optional **Extract** pattern. The template takes these placeholders:

| Placeholder | Value for `https://www.example.com:8080/a/b?x=1` |
| --- | --- |
| `{url}` | The link as is |
| `{url_encoded}` | The link percent-encoded for a query (`https%3A%2F%2F…`); only `A–Z a–z 0–9 - _ . ~` are kept |
| `{host}` | `www.example.com` (lowercase, no port) |
| `{path}` | `/a/b` (no query) |
| `{query}` | `x=1` (no `?`) |

An unknown placeholder is left as typed. The result must be a URL (`scheme:…`). If it isn't, the link opens in the fallback browser, and the Test trace says why.

**Open with** picks the app for the result. *By link* opens an http(s) result in the fallback browser and any other scheme (`obsidian://…`) with that scheme's app. Or pick an installed app.

The seeded targets are:

| Target | Template |
| --- | --- |
| Claude | `https://claude.ai/new?q={url_encoded}` |
| ChatGPT | `https://chatgpt.com/?q={url_encoded}` |
| Obsidian | `obsidian://new?content={url_encoded}` |

### Extract parts of a link

A target's **Extract** field (`extract` in the file) takes a regular expression that runs on the full link. Write it plain, without the `/…/` a rule's pattern uses. Its capture groups become placeholders, so the target can build a new link out of pieces of the old one.

To open GitHub pull requests in Graphite:

| Field | Value |
| --- | --- |
| Extract | `github\.com/([^/]+)/([^/]+)/pull/(\d+)` |
| Template | `https://app.graphite.dev/github/pr/{1}/{2}/{3}` |

`{1}`, `{2}`… are the numbered groups (`{0}` never fills, and a group that took no part is empty). Named groups read better:

| Field | Value |
| --- | --- |
| Extract | `github\.com/(?<owner>[^/]+)/(?<repo>[^/]+)/pull/(?<n>\d+)` |
| Template | `https://app.graphite.dev/github/pr/{owner}/{repo}/{n}` |

Either way, `https://github.com/acme/app/pull/12/files` opens `https://app.graphite.dev/github/pr/acme/app/12`. Point a rule at the target (pattern `github.com/*/*/pull/*`, destination *Target: Graphite*), or pick it from the chooser.

- A group's text goes in exactly as it appears in the link. It isn't percent-encoded.
- A group wins over a built-in placeholder of the same name, so a group called `host` replaces `{host}` in that target.
- A target with an extract pattern applies only to links the pattern matches. A rule pointing at it falls through to the next rule for other links, and the chooser lists the target only for links it matches.
- An extract pattern that doesn't compile never matches, and Settings shows why under the target.

[Go links](https://flow.worldbreakstudios.com/docs/features/golinks.md) use the same extract syntax.

## The chooser

Hold the **ask modifier** while you click a link, and Flow shows a menu instead of following the rules. The modifier is `link_router.ask_modifier`: ⌥ Option by default, or Control, fn or None. A rule whose destination is *Ask* shows the same menu.

- **A link from another app:** Flow reads the modifier the moment the link arrives and opens the menu at the pointer.
- **A terminal link in Flow:** hold the ask modifier as you ⌘-click it (`⌥⌘`-click by default). The menu opens at the click.

The chooser lists, in its default order: Flow tab (with web tabs on), PR review session (for a pull request link), each installed browser (the fallback first), each target that fits the link, and **Copy link** last. Esc or a click away opens nothing. Two installed apps with the same name are told apart by their bundle id, for example *ChatGPT Atlas (com.openai.atlas)*.

For a link from another app: picking a Flow tab or PR review session brings Flow's main window forward, and Esc or Copy link hands focus back to the app you clicked in.

### Customize the chooser menu

Settings › Link router › Menu sets what the chooser lists, and in what order.

- **The general menu** is the one every link shows unless a rule has its own. Pick *General menu* in *Menu for*, move entries with ↑ ↓, and turn *Show* off for the ones you don't want. Your first move pins the current order; an entry that appears later (a new browser or target) goes at the end. An app you uninstall drops out.
- **A rule's own menu:** pick the rule in *Menu for* and turn on *Own menu for this rule*. It starts as a copy of the general menu, then keeps its own order and hidden entries. Each link uses the menu of the first enabled rule that matches it and has its own menu, whatever that rule's destination. If none does, it uses the general menu.
- **Hide everywhere** hides an entry in every menu until you bring it back from *Hidden everywhere*.
- **Reset order** puts the selected menu back to the default order with nothing hidden. Entries hidden everywhere stay hidden.

A given link shows only the entries that fit it: the PR review session only on pull request links, a target only when its template (and extract pattern, if any) fits. *Copy link* is always there, even when you hide everything else.

## Links you open inside Flow

`links.open_in` decides where Flow opens a web link you click inside Flow: `browser` (the default: your browser) or `app` (a Flow web tab). Holding ⇧ flips it for one click:

| Where | Opens the other way |
| --- | --- |
| A terminal link | `⇧⌘`-click |
| A link on the PR review page or in Notes | ⇧-click |
| A go link in the command palette | ⇧↩ |

Set it in Settings › General › Tabs › **Open links in** (Browser or Flow tab). The row is there only while the Web tabs experiment is on; with web tabs off, every link opens in the browser and ⇧ flips nothing. See [Web tabs](https://flow.worldbreakstudios.com/docs/workspace/webview-tabs.md).

With the link router on, the browser side of that choice goes through your rules, with the source app `flow`. So you can name `flow` in a rule's *From apps* to treat Flow's own links differently, for example to send Flow's GitHub links to Chrome but Slack's to a PR review session. `links.open_in = app` and ⇧ still open a Flow tab directly.

## Ask the decisions model

With the Decisions experiment on and a decisions model set up in Settings › Decisions, **Ask the decisions model** (`link_router.model_fallback`) lets that model pick where an unmatched link goes. It is offered the destinations your enabled rules and targets name, plus the fallback. It must answer within 3 seconds with at least 50% confidence; otherwise the link opens in the fallback browser.

> **Warning:** The link's URL, and the bundle id of the app it came from, go to the decisions model you configured. That is a hosted vendor if that is what you set up. Flow never fetches the page.

## Test a link

Settings › Link router › Test: type a URL, and optionally the bundle id of the app it comes from. You see the rule that takes it, where it opens, a step-by-step trace, and the menu the chooser would show for it, along with which menu decided it (*General menu* or *Rule: …*). The trace names hosts, rules, targets and apps, never a query string.

From a terminal:

```sh
# Report the route without opening anything
flow-cli read link-route https://github.com/acme/app/pull/12 --from com.tinyspeck.slackmacgap

# Route the link exactly as an incoming one
flow-cli route-url https://zoom.us/j/123 --from com.tinyspeck.slackmacgap

# As if the ask modifier were held: shows the chooser
flow-cli route-url https://example.com --ask
```

`read link-route` replies with JSON: `rule`, `rule_name`, `action`, `url`, `trace`, `would_ask_model`, `enabled`, `fallback`, and `menu` (`{"scope": "general" | "rule:ID", "items": [{"key", "label"}]}`). See [flow-cli](https://flow.worldbreakstudios.com/docs/cli/flow-cli.md).

## The rules file

`~/.flow/link_router.json` holds `rules` and `targets` in order, plus the chooser's `menu` and `hidden` entries once you customize them. A destination is an object with a `kind`: `flow_tab`, `pr_session`, `app` (with `bundle_id`), `target` (with `id`), `fallback` or `ask`.

```json
{
  "rules": [
    {
      "id": "github-prs",
      "name": "GitHub pull requests",
      "enabled": true,
      "pattern": "github.com/*/*/pull/*",
      "from_apps": [],
      "destination": { "kind": "pr_session" }
    },
    {
      "id": "docs-in-chrome",
      "name": "Docs from Flow in Chrome",
      "enabled": true,
      "pattern": "docs.rs",
      "from_apps": ["flow"],
      "destination": { "kind": "app", "bundle_id": "com.google.Chrome" }
    }
  ],
  "targets": [
    {
      "id": "claude",
      "name": "Claude",
      "template": "https://claude.ai/new?q={url_encoded}"
    }
  ]
}
```

- A target may also have `extract` (a regular expression) and `app` (a bundle id to open the result with).
- Menu entry keys are `flow_tab`, `pr_session`, `app:BUNDLE_ID` and `target:ID`. `menu` is `{"order": [...], "hidden": [...]}`; a rule can have its own `menu`.
- Flow reads the file once, when it starts, and saves your Settings edits to it. Quit Flow before you edit the file by hand.
- A file that doesn't parse shows as an error in Settings, and Flow treats it as empty. The next save first keeps a copy as `link_router.json.bad`.
- An empty or repeated rule or target id is replaced with a fresh one.

## Settings

| Key | Type | Default | Effect |
| --- | --- | --- | --- |
| `features.link_router` | bool | `false` | Turns the link router on |
| `link_router.fallback` | string (bundle id) | empty (Safari) | The browser for links no rule takes. Empty, or Flow itself, means Safari |
| `link_router.ask_modifier` | `option`, `control`, `fn` or `none` | `option` | The key you hold while clicking a link to get the chooser |
| `link_router.model_fallback` | bool | `false` | Ask the decisions model when no rule matches (needs the Decisions experiment) |
| `links.open_in` | `browser` or `app` | `browser` | Where links you open inside Flow go; ⇧ flips it (needs web tabs) |

All of them are app-wide, the same in every checkout. See [Settings](https://flow.worldbreakstudios.com/docs/configuration/settings.md) and [Settings scopes](https://flow.worldbreakstudios.com/docs/configuration/settings-scopes.md).

## Limits

- Routing reads the URL only. Flow never fetches a page to decide where it goes, because fetching can use up one-time sign-in links, password resets, email confirmations and unsubscribe links.
- Only the installed `Flow.app` can be your default browser.
- Without a license, while the paywall is up, every link opens in the fallback browser: no rules, no chooser.
- If an app or browser fails to open a link, Flow shows a toast and opens the link in the fallback browser (Safari, if the fallback itself failed). A link is never silently dropped.
