← Catalog

@nicknisi/pi-answer

Extract questions from the last assistant reply into an interactive Q&A form and send the answers back

Package

Install

Install with PiG

pig install npm:@nicknisi/pi-answer

Install with Pi

pi install npm:@nicknisi/pi-answer
Review before installing

Packages run code with your permissions and influence agent behavior. Catalog inclusion is not a security audit. Review the source before installing a third-party package.

Pi manifest JSON

The pi field of package.json.

{
  "extensions": [
    "./index.ts"
  ]
}

README

Written by the package's publisher, shown as published in version 0.3.8 (README.md). PiG does not review it.

@nicknisi/pi-answer

Extracts unanswered questions from the last completed assistant message and presents them in an interactive Q&A overlay, then sends your answers back into the session as a single formatted user message. It exists for the common case where the agent ends a turn with several questions scattered through its output — instead of re-reading and composing a reply by hand, you tab through a focused form and fire the answers back in one shot.

What it adds

  • Slash command: /answer
  • Keybinding (shortcut): ctrl+. — same handler as the command
  • Overlays (via ctx.ui.custom):
    • A BorderedLoader while questions are being extracted by an LLM (abortable with Esc)
    • An AnswerComponent — a bordered, multi-question form with navigation, per-question status, and a submit confirmation step
  • Footer hint: ? ctrl+. to answer (status key answer-hint) after a reply that asks you questions, when a classifier is available
  • No tools, no widgets, no custom message/entry types. The submitted answers go out as a regular user message via pi.sendUserMessage.

How it works

  1. Walks ctx.sessionManager.getBranch() backwards to find the last assistant message with stopReason === "stop" and non-empty text. Incomplete (e.g. aborted or tool-loop) assistant messages are skipped, with a warning if any were skipped.

  2. Sends that message's text to an extraction model with a system prompt instructing it to return {"questions": [{question, context?}]} JSON — only questions that require user input, each self-contained.

  3. Parses the response leniently: tries the raw text, ```json fenced blocks, and the first {..} span. If parsing fails or the response is empty, falls back to a regex-based extractor that pulls ?-terminated sentences out of the original message (ignoring code blocks, inline code, and URLs). A failed extraction request (auth, rate limit, network) is reported as an error instead of falling back. Questions are trimmed and case-insensitively deduped.

  4. Opens the answer form. On submit, builds a message:

    Here are my answers to your questions:
    
    Q: <question>
    Context: <context, if any>
    A: <answer, or "(no answer)">
    
  5. If the session is idle (ctx.isIdle()), sends via pi.sendUserMessage(answers). If the agent is still running, sends with { deliverAs: "followUp" } and notifies that it was queued.

Answer hint

After each completed reply that contains a ?, a classifier (TypeSafe's Jev by default) judges whether the reply asks you questions that need answers. If it does (probability at least 0.5), the footer shows ? ctrl+. to answer. The hint clears on your next prompt or when you run /answer. A reply without a ? skips the classifier.

The hint is optional. With no credentials for any of the hintModels, an older pi without classifier support, a failed classifier call, or "hintModels": [], no hint shows and /answer works as before. The last 16,000 characters of the reply go to the classifier's provider. A missing classifier is looked up again on the next question-like reply, so logging in mid-session turns the hint on. A pending classifier call is aborted on your next prompt or session shutdown.

Usage

/answer

or press ctrl+. at the prompt.

Answer form keybindings

KeyAction
Tab / EnterNext question (on the last question: show submit confirmation)
Shift+TabPrevious question
Shift+EnterNewline inside the current answer
Up / DownPrevious/next question (only when the current answer box is empty)
Esc / Ctrl+CCancel (in confirmation: go back to the form)
Enter / y (in confirmation)Submit all answers
n (in confirmation)Go back to the form

The form renders up to 120 columns wide and shows: question counter (n/N), answered count, a per-question status strip (current highlighted, answered in success color, unanswered dim), the question text with optional Context: line, and the answer editor. Unanswered questions submit as (no answer) — the confirmation step tells you how many.

The extraction loader can be aborted with Esc, which cancels the whole flow.

Configuration

Optional global config: ~/.pi/agent/configs/answer.json. Changes take effect on the next session or after /reload.

{
  "extractionModels": ["anthropic/claude-haiku-4-5", "anthropic/claude-sonnet-5"],
  "hintModels": ["typesafe/jev-latest", "openrouter/typesafe/jev-1.14"]
}

extractionModels is a non-empty ordered list of provider/model-id strings. The first exact model with working credentials in its provider's available chat models is used, via selectSideModel from @nicknisi/pi-shared. Unavailable candidates and credential lookup failures continue in order. Model IDs may contain additional slashes; only the first slash separates the provider. Missing or invalid config falls back to the list above and invalid config emits a warning.

hintModels is an ordered list of classifier provider/model-id strings for the answer hint; the first with working credentials wins. It defaults to Jev on every provider pi lists it under. [] turns the hint off. An invalid list warns and keeps the default. See answer.example.json.

If no preferred extraction model is available, extraction falls back to the current session model (ctx.model). Selection is re-evaluated each time, with no cached/sticky fallback. /answer also works without a session model when a preferred model is usable. If neither is available it reports an error. If the selected model is found but the request fails (e.g. a deprecated model returning 410, an outage, or quota), it retries once on the current session model via withSessionFallback. Authentication comes from Pi's normal model runtime; classifier hint selection is unchanged. Older supported hosts without getAvailableOfType() check each preference with find() plus getApiKeyAndHeaders().

Dependencies

  • @earendil-works/pi-coding-agent (peer, >=0.86.0) — ExtensionAPI, ExtensionContext, BorderedLoader, Theme; extraction runs through ctx.modelRegistry.complete(model, { systemPrompt, messages }, { signal }), which applies models.json overrides, extension-registered providers, and request-time auth
  • @earendil-works/pi-tui — Component, Focusable, Editor, EditorTheme, Key, matchesKey, TUI, truncateToWidth, visibleWidth, wrapTextWithAnsi
  • @earendil-works/pi-ai — types only (UserMessage)
  • @nicknisi/pi-shared — selectSideModel for extraction; findClassifier and classifyBool for the answer hint

No third-party npm dependencies.

Caveats

  • Extraction-model availability. If no configured candidate is available with working authentication and no current session model exists, the command errors out listing the preferences it checked.
  • Pi internals: depends on ctx.sessionManager.getBranch() entry shapes (entry.type === "message", message.role, message.stopReason === "stop", content part { type: "text", text }), ctx.modelRegistry.getAvailableOfType/complete, ctx.ui.custom component contract, and pi.sendUserMessage(..., { deliverAs: "followUp" }). Any of these changing across pi versions will break it.
  • Editor render slicing. AnswerComponent.render strips the first and last lines of the embedded Editor.render() output (editorLines[1..len-2]) assuming the Editor wraps its content in a border frame. If pi-tui's Editor render shape changes, answers will render wrong.
  • Extraction is heuristic at the edges. The LLM prompt tries to keep questions self-contained, but the regex fallback (fallbackExtractQuestions) is naive — it splits on sentence boundaries ending in ? and can produce context-free or false-positive questions when extraction JSON parsing fails.
  • Multi-part assistant messages are flattened by joining all text parts with \n; non-text parts (tool calls, thinking) are ignored.
  • Requires the interactive TUI (ctx.mode === "tui"); in RPC mode it reports an error, and in print/JSON mode it does nothing. The answer hint is also TUI-only.

Install

pi install /Users/nicknisi/Developer/pi-extensions/packages/answer