Skip to main content

@ai-coach/adapter-ollama

The local model, in every job the coach gives it. Everything the coach asks you while you spar, study, synthesize, elaborate, group or teach comes from a Gemma model running on your own machine through Ollama. Your notes never leave the computer. You change this package when you want the coach to word its questions differently, or to check a model's replies more strictly.

What it does not do​

  • No decisions about learning. What counts as a pass, a gap or a copied answer is a domain rule in @ai-coach/domain. This package asks the model and checks the reply's shape.
  • No network beyond your own machine. The host must be http on 127.0.0.1, localhost or ::1. Anything else stops with Tier 1 runs offline… (Tier 1 stays local).
  • No vault access and no online judge. Notes arrive through the application's ports; the judge lives in adapter-gemini.
  • Only zod and Node built-ins. Nx boundaries (adapter:ollama) forbid any other external import.

Architecture fit​

  • Scope: scope:coach
  • Style: architecture:hexagonal (type:adapter, adapter:ollama)
  • Depends on: @ai-coach/application (the ports it implements), @ai-coach/domain
  • Used by: @ai-coach/cli, which builds the partners in its composition root
[cli composition] ──▶ <stage>/Ollama…Partner ──▶ <stage>/<partner>-prompts.ts ──▶ shared/OllamaClient ──▶ Ollama on localhost
│ ▲
└── use cases call ──┘ (SparringPartner, StudyPartner, … ports)

Internal architecture & rules​

  • One folder per CLI stage, the same split as coach source, learn, exam and teach and as @ai-coach/domain: source/ (synthesis and structure partners), learn/ (elaboration and study partners), exam/ (sparring partner) and teach/ (teach partner). Each partner class sits next to its prompts (<partner>-prompts.ts) and its spec, re-exported through the folder's index.ts. Tuning how the coach talks in one job starts in that partner's prompts file.
  • shared/ holds what several stages use: OllamaClient, the one connection to Ollama; prompts.ts with the system and grader prompts and the helpers that wrap note text in tagged blocks; and the style classifier, which picks a coaching style for both spar and study. Each stage depends only on shared/, never on another stage.
  • The class is the only place that turns a model reply into the port's result.
  • Note text is data, never instructions. Notes, transcripts and marks are wrapped in tags with a random nonce and neutralized first. Keep every new prompt going through those wrappers.
  • The coach doesn't quote your note back. A question that copies the note is sent back to be rewritten; if that keeps happening, the sparring use case skips the note with ChallengeLeakError.
  • Every reply is checked. Shape, a real question where one is expected, no filler. A bad reply is retried once, then surfaces as UnusableReplyError so the use case can fall back.
  • Too long fails loudly. A note that won't fit the context window throws ContextTooSmallError rather than being cut off.
  • No comments in source code (DEC-AIC-0011).

Public surface​

  • OllamaSparringPartner — the opening challenge, follow-ups and the Tier 1 verdict, with each gap checked against what you said (coach spar).
  • OllamaStudyPartner — a guiding question per gap, clues on request, and whether your new explanation closes the gap (coach study).
  • OllamaStyleClassifier — picks the coaching style for a note when you haven't chosen one.
  • OllamaSynthesisPartner — groups your marks into ideas, asks gap questions, drafts core assertions and suggests buzzwords (coach synthesize).
  • OllamaElaborationPartner, OllamaStructurePartner, OllamaTeachPartner — the elaborate questions and follow-ups, structure questions about your groups, and the student's misconception (coach elaborate, group, teach).
  • OllamaClient — the shared connection, with retries and reply checks.
  • The prompt builders (focusMessages, hintMessages, ideasMessages, …) are exported for tests and tuning.

Model and host settings are in the configuration guide.

How to extend it​

  1. Add or change the port method in @ai-coach/application first, so the use case says what it needs.
  2. Write the prompt in the partner's <partner>-prompts.ts in its stage folder (a new partner goes in the stage whose command uses it), wrapping any note text with the helpers from shared/prompts.ts, and export it from the folder's index.ts.
  3. Call it from the partner class through OllamaClient, validate the reply with zod, and map bad replies to UnusableReplyError.
  4. Add a spec next to the partner and run npm exec nx test @ai-coach/adapter-ollama and npm exec nx lint @ai-coach/adapter-ollama.

Running & testing​

npm exec nx test @ai-coach/adapter-ollama
npm exec nx lint @ai-coach/adapter-ollama
npm exec nx typecheck @ai-coach/adapter-ollama
npm exec nx build @ai-coach/adapter-ollama

The tests start a tiny local HTTP server in place of Ollama, so they need neither Ollama nor a model.

Key ADRs​

  • DEC-AIC-0002 — Tier 1 runs on an offline Gemma model; this package is that model's adapter.
  • DEC-AIC-0014 — anchors sparring on a focus and keeps the note out of the dialogue.
  • DEC-AIC-0016 — every Tier 1 gap is checked against the transcript.
  • DEC-AIC-0013 — the context window is set explicitly and overflow fails loudly.
  • DEC-AIC-0008 — adapters implement application ports behind Nx boundaries.