Recording what people ask
A bot answers a question and forgets it. The question repository is where it keeps them instead, so whoever maintains the knowledge base can see what is asked most — and what is asked and not answered.
- One row per question. The same question asked again is counted on the row it repeats, not added as another.
- The answer is kept with it, exactly as the bot gave it the first time. Correct it there if you like: a repeat never overwrites it.
- Nothing about who asked is recorded — no name, no channel.
- It cannot slow down or break an answer. Recording happens after the reply and a failure is only a line in the log.
Recording from a bot
One call, after the reply has been sent:
import { recordQuestion } from "@wingman/question-log";
await conversation.replyInThread(answer);
await recordQuestion(conversation, { question, answer });
Leave answer out for a question the bot had no written answer for; the row is
still created and counted, with an empty page. On a deployment that has not
configured a repository the call does nothing.
There are two places to keep them, and a deployment uses one: the bot's own database, which also gives it a public page of questions, or Notion. A bot with its own database bound does not write to Notion.
Keeping them in the bot's own database
The bot's own database is a Cloudflare D1
database bound to the Worker as QUESTIONS_DB. There is no credential and
nothing to prepare — the bot creates its tables the first time it uses them.
-
Create the database:
wrangler d1 create <your-bot>-questions -
Put what that prints into the
d1_databasesblock ofwrangler.jsonc:"d1_databases": [ { "binding": "QUESTIONS_DB", "database_name": "<your-bot>-questions", "database_id": "<the id it printed>" } ] -
Deploy.
It holds the same things the Notion databases do — the question, how often it was asked, when first and last, the answer, and every wording exactly as it was typed — and each question gets a random identifier of its own. The count is exact here: two people asking the same new question at the same moment land on one record.
To curate the questions, sign in to the bot's settings — the questions page has a Sign in link under it — and open a question. Two buttons appear under its heading:
- Edit opens the question's wording and its answer. Reword a question to cover the general case — "How do I deploy the bot?" rather than the one customer's project it was first asked about; the words people really used stay listed under Asked as. The answer editor takes Markdown — it shows the formatting as you type and has a toolbar for bold, lists, links and the rest. Save changes what the page shows and what the bot answers with from then on; save the answer empty to remove it. If another question already has the wording you typed, nothing is saved and the screen links to that question. Part of files the question under a broader one — see Sub-questions.
- Delete asks first, then removes the question, its answer and the wordings kept under it. It cannot be undone; asked again, it is recorded as a new question. Its sub-questions move up to the question it was part of.
The bot reads a question as it stands, so every change shows at once.
The questions page
A bot that keeps its questions itself can publish them. Add one line where the bot's screens are mounted:
import {
MAX_ANSWER_CHARACTERS, MAX_QUESTION_CHARACTERS, publishedQuestionsFromEnv,
} from "@wingman/question-log";
admin: adminSite({
questions: env => publishedQuestionsFromEnv(env),
maxAnswerCharacters: MAX_ANSWER_CHARACTERS,
maxQuestionCharacters: MAX_QUESTION_CHARACTERS,
// …
}),
The two limits tell the editor how long an answer and a question may be — the same numbers the bot's own store keeps, so a correction the editor accepts is one the store saves.
and the bot serves /questions, with a Questions tab in the bar beside
the knowledge base and the chat:
- The navigation lists the questions, most asked first — one line per question, however many ways it was put — with each sub-question under the question it is part of.
- Opening one shows its answer and, under Asked as, the exact words people used to ask it.
- Every question has an address of its own,
/questions/<identifier>, which stays the same for as long as the question exists.
Three things to do with it:
- Add
"/questions", "/questions/*"toassets.run_worker_firstinwrangler.jsonc, or the address answers with the documentation's 404 page — see the website. - Add the tab to the documentation pages' own bar, in
knowledge/_heading.md:<a class="ui-nav-link" href="/questions">Questions</a>. - Decide whether it should be public. The page is readable by anybody, like the documentation, and it shows what people typed. Nothing about who asked is on it, but a question can carry a name or an order number of its own. Leave the line out and the questions are still recorded — only the page is gone.
On a deployment with no database the questions come from the Notion database the bot records in, if it has one, and can only be read there — correct them in Notion. With neither, the page says the bot keeps no questions.
The journal
To learn why a question was or was not answered from the records, add a
second line beside questions:
import { answerJournalFromEnv } from "@wingman/question-log";
admin: adminSite({
questions: env => publishedQuestionsFromEnv(env),
journal: env => answerJournalFromEnv(env),
}),
Whoever is signed in then has a Journal tab: every reply the bot gave, newest first. Opening one shows how it was reached —
- what the decision model thought of the recorded questions closest to the message, each with its probability and the threshold it had to reach, so a question that came close and fell short is there to see;
- whether the written answer was kept, as the review judged it;
- whether a new question was recorded, or which one it was counted on;
- which broader question a new one was filed under, with every candidate it was compared with — or why it stayed at the top level.
A question's page links to the replies counted on it, for whoever is signed in. The journal is private, lives in the same database as the questions, and keeps the newest thousand replies.
Sub-questions
A question can be a sub-question of another: a more specific question within that one's broader topic. Where do I get a token to set up Slack? is part of How do I set up Slack?, and Which scopes does the token need? is part of that.
- On the questions page the navigation is a tree. A question with sub-questions has a "2 more specific questions" line under it that opens them, and the way to the question you are reading is already open. The question's own page says what it is part of and lists its more specific questions.
- The bot files new questions itself when the deployment has a decision model (see below): a question asked for the first time is compared with the most-asked questions sharing its words, and filed under the one the model is sure it belongs to. Not sure, or no decision model, and it is a question of its own.
- You file them on the editor, under Part of: every other question, in its place in the tree, or Nothing — a question of its own. A question cannot be filed under itself or one of its own sub-questions.
Starting with questions of your own
A new bot's repository is empty, so nothing can be answered from the record
until people have asked. questions.md, at the root of the bot's package,
is the set it starts with instead — the bot-scaffolding skill writes it from the
knowledge base:
## How do I set up Slack?
Create a Slack app, add the scopes, install it and set the three secrets.
[Connecting Slack]
### Where do I find SLACK_BOT_USER_ID?
Ask Slack's once the token exists; is the value.
[Connecting Slack]
## is a broad question and ### a sub-question of it; the text under each is
the answer, sent as written. A link to knowledge/… becomes that article's
address on the bot's own website. Plant it from the bot after replying:
import { questionSeed, seedQuestions } from "@wingman/question-log";
import seedMarkdown from "../questions.md";
const seed = questionSeed(seedMarkdown); // once; seed.problems lists mistakes
// …after the reply:
await seedQuestions(conversation, seed, { links });
- It is planted once per version of the file, ten questions per message,
and remembered in the bot's store (
BOT_STORE). A new version adds only the questions that are new in it. - Nothing is overwritten. A question already on record keeps its answer, and one a person deleted is not added back — so correct a planted answer on the questions page, not in the file.
- Planted questions are not asked yet: their count starts at 0, and the page says so.
- A file with a mistake in it — a question with no answer, the same question
twice — plants nothing; test that
seed.problemsis empty. - Have the decision model read it before it ships. In a checkout of the
SDK,
pnpm review-questions bots/<your-bot>lists the file's mistakes and then, withSYSTEM_ONE_URLin the bot's.dev.vars, what the model finds: a heading that asks two things, a sub-question it would file under another broader one, two questions that are the same. Whether each answer answers its question is still yours to read. Each finding names its line. It is a reason to read that question again, not a verdict. SEED_QUESTIONS=off(Starter questions on the settings screen) keeps the repository empty.
This site's bot starts with its own questions.md. Ask it one:
Which chat systems does a Wingman bot work in? — then find it
under Questions in the bar above.
Setting up Notion
- Create an integration at notion.so/profile/integrations and copy its secret.
- Create a database in Notion. An empty one is enough — the bot uses whatever the title column is called and adds the columns it needs itself.
- Share the database with the integration: ••• on the database page, then Connections. Skip this and Notion answers as though the database did not exist.
- Give the bot both values:
| Variable | Meaning |
|---|---|
NOTION_TOKEN |
The integration's secret. A secret — wrangler secret put NOTION_TOKEN, or the Services row on the bot's settings screen. |
NOTION_QUESTIONS_DATABASE |
The database, as its address copied from the browser or as its id. A plain variable, also on the settings screen. |
With only one of the two set, nothing is recorded and the log says which one is missing.
Every wording, as typed
The row of a question keeps the wording it was first asked in. To keep every asking, name a second database:
| Variable | Meaning |
|---|---|
NOTION_ASKED_QUESTIONS_DATABASE |
A second database, shared with the same integration. Optional. |
Each question is then also written there exactly as it was typed, with the date and a link to the row it was counted on — and that row gains an Asked as column listing them.
Creating both databases with one command
In a checkout of the SDK, instead of creating them by hand:
NOTION_TOKEN=ntn_… pnpm question-databases --page <address of a Notion page>
It creates the two databases inside that page with every column, and prints the two variables to set. The page has to be shared with the integration. Running it again changes nothing: it only ever adds what is missing.
Bringing existing databases up to date
A new version of the bot may read a column your databases were made without —
Parent question arrived with sub-questions. The bot adds it
itself the first time it records a question after the update, and you can do it
beforehand, against the databases your bot is configured with:
pnpm question-databases --env-file bots/<your-bot>/.dev.vars --check # what is missing; changes nothing
pnpm question-databases --env-file bots/<your-bot>/.dev.vars # adds it
It only ever adds columns: nothing is renamed, removed or created.
What the database looks like
| Column | Type | Holds |
|---|---|---|
| the title column | Title | The question, as it was first asked. |
Count |
Number | How many times it has been asked. Sort by this. |
First asked, Last asked |
Date | When. |
Key |
Text | The normalised wording the bot matches on. For the bot; hide it from your views. |
Parent question |
Relation | The broader question this one is a sub-question of. Set it by hand in Notion; the bot sets it for a new question when it has a decision model. |
The count is a guide rather than a ledger: Notion cannot increment a number, so two people asking the same thing in the same instant can be counted once.
Which questions count as the same
Two steps, the cheap one first.
The same question, typed differently — always on. Capitals, punctuation,
accents and filler words (please, the, can I) are ignored, so How do I
reset my password? and how do i reset my password are one row. Word order and
the words that change an answer (not, why, when) are kept.
The same question, in other words — when the deployment has a Vectorize
index for it and Workers AI. I forgot my password, what now? then lands on the
row above. Create an index of its own — never the knowledge base's — and
bind it as QUESTION_INDEX:
wrangler vectorize create my-bot-questions --dimensions=768 --metric=cosine
"vectorize": [{ "binding": "QUESTION_INDEX", "index_name": "my-bot-questions" }]
768 is the width of the default embedding model; a different AI_EMBEDDING_MODEL
needs the matching width (see AI and tools). The bot is deliberately
cautious here: two rows a person can merge are better than two questions merged
where nobody can see it.
Answering a question that was answered before
The answers in the database are not only a record. With a decision model configured, the bot looks there first:
| Variable | Meaning |
|---|---|
SYSTEM_ONE_URL |
The address of a System One endpoint, ending in /v1/systemone. |
SYSTEM_ONE_API_KEY |
Its key, when the server asks for one. A secret. |
import { replyMode } from "@wingman/core";
import { planAnswer } from "@wingman/question-log";
const mode = replyMode(event, conversation.env); // "direct", "listen" or null
if (!mode) return;
const plan = await planAnswer(conversation, question, { mode, topics: TOPICS });
if (plan.action === "Silent") return;
if (plan.recall) {
await conversation.replyInThread(plan.recall.answer);
await plan.finish(); // counted on the records it came from
return;
}
const answer = await responder.answer({ question }, conversation); // the model, as before
await conversation.replyInThread(answer.text);
await plan.finish(answer); // after the reply, with the documents it used
A decision model does not write anything. Everything it is asked about a message is asked at once, in one request: does the message ask anything at all, and — the message split into the questions it asks — which of the recorded questions would, answered, fully answer each of them. Only when every question in the message has an answer on record is the reply taken from the database — one answer as it stands, several put together under the question each belongs to, with no AI. Where a question matches both a broad question and one of its more specific ones, the more specific one answers it. Otherwise the bot answers the way it always did; a message that is half on record is never half answered.
The answer is read from the record as it is now. Correct an answer on the questions page, or a page in Notion, and that is what the next person asking is told.
It needs no AI provider, so a bot with AI_SERVICE unset still answers what it
has answered before.
What happens to an answer the AI wrote
After the reply, the decision model reads the question and the answer the AI gave, again in one request:
- Does it answer everything that was asked? If not — "I have asked a person to help you" is the usual case — the question is recorded without an answer, so nobody is later told that from the record. The next time it is asked and the AI's answer does cover it, that answer is kept — only ever on a question with no answer yet, never over one somebody wrote.
- Does it carry more than the question needs? Then the AI is asked once more, for a shorter answer and the more specific questions the rest of it answers. The question is kept with the shorter answer, and the more specific ones are filed under it, each with its own answer, Not asked yet. The next person who asks one of those gets exactly that part.
None of it delays the reply, and a failure anywhere leaves the question recorded the way it was before.
Listening in a channel
In a shared channel a bot answers only when it is mentioned. Set
LISTEN_IN_CHANNELS to on (Listen in channels on the settings screen) and it
also answers questions it overhears — only those on its own topics:
- a message that asks nothing, or asks about something else, gets no reply;
- a question already on record is answered from the record;
- any other question on its topics is answered by the AI from the documentation — and when the documentation does not cover it, the bot says nothing rather than "I do not know". No menu, no placeholder, no hand-off to a person: nobody asked it.
Listening needs the decision model: without one the bot cannot tell a question
on its topics from chatter, and stays silent as before. Slack delivers channel
messages to the bot (message.channels); Google Chat and Teams deliver only
the messages that mention it.
Asking the bot to stay out
Anyone who would rather talk to people than to an AI puts 🙊 in their message,
and the bot does not answer it — not when it overhears it, not when it is
mentioned in it, not in a direct message. The sign is a setting:
DO_NOT_ENGAGE_MARKERS (Signs that keep the bot out of a message on the settings screen) takes a
comma-separated list of emoji or words, and off turns it off. Slack sends an
emoji in a message as its :name:, so a custom emoji is listed in both
spellings — 🚫, :no_entry_sign:.
When the AI cannot answer
A provider that is down or timing out does not have to end the conversation. The database is searched by key words, and the questions closest to the one just asked are offered as buttons:
import { offeredAnswer, offerRecordedQuestions } from "@wingman/question-log";
const offer = await offerRecordedQuestions(conversation, question); // three at most, or null
// "Sorry — I could not write an answer just now. Are you asking one of these?"
// …one button per `offer.records[n]`, its id carrying `offer.id` and `n`.
// When one is pressed:
const pressed = await offeredAnswer(conversation, offerId, n);
if (pressed?.answer) await conversation.replyInThread(pressed.answer);
await pressed?.count(); // counted on that question, once
Only questions that hold an answer are offered, and the answer is read when the button is pressed, so a corrected page is what the person sees. This needs no decision model: nothing decides for the person, they choose.
The button names the offer, not the page. The offer is kept for an hour under an id nobody can guess, so a press can only ever read an answer the bot itself put on a button.
The same decision model keeps the database to questions: "thanks", "ok" and "good morning" are answered and not recorded. It is deliberately cautious — it leaves out only what it is nearly sure asks nothing, so the odd remark still gets a row and no real question is lost.
Keeping questions somewhere else
Both stores are implementations of an interface with three methods (and a few
optional ones — keeping the wordings, or sub-questions with
setParent):
import { questionLog, type QuestionRepository } from "@wingman/question-log";
const repository: QuestionRepository = {
async findByKey(key) { /* the record stored under this key, or null */ },
async create(question) { /* a new record, counted once, holding the answer */ },
async recordRepeat(id, askedAt) { /* count one more; null if the record is gone */ },
};
await questionLog({ repository, logger: conversation.logger }).record({ question, answer });
A repository stores the key it is given and finds a record by it again; deciding
which questions are the same stays with questionLog, so every repository
agrees about it. memoryQuestionRepository() is a complete one to read, and to
use in tests.