# Knowledge and Ask

A Fellow application can say what it knows about itself: the Markdown files of its `docs/` folder and, from the
contracts, every route and page with who may use it. That text has one format and three readers:

- **agents**, through the CLI (`fellow knowledge`), the MCP tool `fellow_knowledge`, and in the browser through
  WebMCP;
- **people**, through `<fellow-ask>`: a question box answered on their own device;
- **anything else** that reads Markdown, at `GET /__fellow/knowledge.txt`.

Nothing is sent to an AI service. The question is answered by the model built into the browser, from sections
found by a search that also runs in the browser. Where there is no built-in model, the person gets the sections
themselves, word for word.

## The two sources

| Source | Comes from | Good for |
|---|---|---|
| `docs` | `docs/**/*.md` at the project root. `README.md` or `index.md` comes first | Help inside the system: how the work is done, in the words of the business |
| `contracts` | `defineRoute`, `definePage` and the module manifests: method and path, who may use it, tenant, inputs, outputs, error codes, permissions and roles | Agents, and questions like "who can approve this?" |

Every `## ` heading starts one section. A document's title is carried into its headings
(`## Review — Deciding`), so a section still says where it comes from when it is read alone. One section is
written per module, per route and per page. Write `docs/` with that in mind: short sections, one subject each.

```bash
fellow knowledge                      # both sources, as Markdown
fellow knowledge --source docs        # only one
fellow knowledge --json               # { title, sources, sections: [{ title, body }] }
```

## Serving it

Off by default. Turn it on in `app.ts`:

```ts
import { defineApp } from '@fellowframework/core'

export default defineApp({
  knowledge: { sources: ['docs', 'contracts'], access: { permission: 'records.document.read' } },
})
```

| Option | Values | Default |
|---|---|---|
| `sources` | `'docs'`, `'contracts'` | both |
| `access` | `'public'`, `'authenticated'`, `{ permission }` | `'authenticated'` |

`GET /__fellow/knowledge.txt` then answers `text/markdown` to whoever `access` allows (session cookie or bearer
token), 401 or 403 to the rest, and 404 while `knowledge` is not set. The text is generated when the server is
built, so a change in `docs/` reaches production with the next build (and the dev server with a restart).

**`contracts` lists every route of the application**, with its permission. That is not secret (the pipeline
enforces access whatever a reader knows), but it is a map: keep `access` at `'authenticated'` or a permission
unless the application is public. For public help, serve only `docs`: `{ sources: ['docs'], access: 'public' }`.

## `<fellow-ask>`

`@fellowframework/ask` is a browser package with no dependencies. It ships compiled JavaScript, so it works in
any front end and bundler, or straight from a `<script type="module">`.

```bash
bun add @fellowframework/ask
```

```html
<script type="module">import '@fellowframework/ask/element'</script>
<fellow-ask subject="the records application" suggestions="Who can approve a document?|How do I send one for review?"></fellow-ask>
```

| Attribute | Meaning | Default |
|---|---|---|
| `source` | URL of the Markdown text | `/__fellow/knowledge.txt` |
| `subject` | What the text is about, told to the model | "this application" |
| `lang` | Language of the text and of the element's own words (`en`, `pt`, `es` built in) | `<html lang>` |
| `suggestions` | Questions offered as buttons, separated by `\|` | none |
| `placeholder`, `button` | Text of the field and of the button | from `lang` |
| `limit` | Sections used per answer | 3 |
| `tools` | `off` keeps the search from being offered to browser agents | on |
| `tool-name` | Name of the WebMCP tool | `search_knowledge` |

Children of the element are shown in the answer area until the first question.

Properties, set from JavaScript: `labels` (any text of the element) and `options` (search synonyms and stop
words, the instruction given to the model, and `fetch` for a source that needs a bearer token; by default the
request carries the session cookie). Events: `fellow-ask-question`, `fellow-ask-answer` (`detail.mode` is
`model`, `passages`, `none` or `unavailable`, with the sections used) and `fellow-ask-tool` (an agent used the
search). Style it with `::part(input)`, `::part(button)`, `::part(answer)`, `::part(suggestion)` and the custom
properties `--fellow-ask-accent`, `--fellow-ask-accent-text`, `--fellow-ask-border`, `--fellow-ask-field`,
`--fellow-ask-radius`.

In a Fellow page (Vue), import the element once and use the tag; the Fellow build leaves `fellow-*` tags to the
browser:

```vue
<script setup lang="ts">
import '@fellowframework/ask/element'
</script>

<template>
  <fellow-ask subject="the records application" />
</template>
```

In Astro, import it from a `<script>` in the page; in React, import it in a client component and write the tag.
https://fellow.run uses it on its home page, with `source="/llms-full.txt"`.

## Without the element

```ts
import { createAsk, createPageTranslator, registerSearchTool } from '@fellowframework/ask'

const ask = createAsk({ source: '/__fellow/knowledge.txt', subject: 'the records application', languages: ['pt'] })
const hits = await ask.search('who approves a document?')            // [{ title, body, score }]
const result = await ask.ask('who approves a document?', (status) => {}) // { mode, text, hits }
await registerSearchTool(ask)                                        // WebMCP, where the browser has it

const translator = createPageTranslator()                            // <html lang> → the browser's language
if (await translator.available()) await translator.translate()       // translator.restore() puts the text back
```

`source` may be any Markdown text with `## ` sections, not only the one Fellow generates: an `llms-full.txt`, a
handbook, a changelog.

## Limits

- **The built-in model exists in recent desktop Chrome.** Elsewhere the element shows the sections found. Both
  are useful; only one is a written answer.
- **It is a small model.** It answers what the sections say and is told to refuse the rest. Vague or missing
  documentation gives vague answers: the fix is in `docs/`.
- **Translation happens in the browser**, so search engines still see the original language.
- **WebMCP is a proposal** that few browsers ship; the tool is registered where it exists and ignored elsewhere.
