Sessions
Connect model, lesson and practice adapters and handle bounded advisory results.
Use runInstruction(host, request, ports, options) for advisory assistance. The request includes assistance, learner text, optional intent, history and narrower limits. Host options include limits, requiredLesson, summary authorization and an abort signal.
Connect a model
A ModelPort identifies its provider and model and implements asynchronous generate(input, signal). The input separates policy, untrusted learner text, history, tool results and optional committed summary context. It includes allowed operation descriptors and remaining limits. Keep credentials and execution functions inside the host.
This offline fixture demonstrates the adapter contract. It returns a constant response; replace the implementation with your own provider integration.
// preceptor-docs: runnable
import { POLICY_REVISION, runInstruction } from "@0xbuff/preceptor";
const host = {
policyRevision: POLICY_REVISION,
sessionId: "example",
mode: "guided_learning",
grants: [],
consent: [],
installed: [],
};
const model = {
provider: "example",
model: "fixture",
generate: async () => ({
kind: "final",
attribution: { provider: "example", model: "fixture" },
text: "Explain the principle in your own words, then compare it with an example.",
}),
};
const result = await runInstruction(
host,
{ assistance: "generate", text: "Help me study" },
{ model },
);
if (result.status !== "completed" || result.usage.modelRequests !== 1 || !result.advisory)
throw new Error("Expected an attributed advisory result");
console.log(result.status);Expected output: completed.
Model responses carry matching provider/model attribution and optional input/output token counts.
| Kind | Payload |
|---|---|
| final | Advisory text |
| tools | Bounded list of typed requests with unique call IDs |
| unavailable | transient or unavailable reason |
Reported token counts are informational. The host controls remote token limits, costs, quotas and transport. Preceptor does not execute arbitrary provider tool calls.
Lesson and practice adapters
SessionPorts.lesson(reference, maxBytes, signal) receives the exact host reference. Return an excerpt with matching documentRef and revision, text, actual UTF-8 bytes and completeness (complete or truncated). Alternatively return kind unavailable with reason not_found, denied, unavailable or transient. The host authenticates and reauthorizes the source; do not fall back to another revision.
Set options.requiredLesson to true when generation requires complete lesson context. For optional lessons the model can request lesson.read with empty arguments. SessionPorts.practice(request, signal) receives a topic and objective and returns suggestions or a typed unavailable response.
Dispatch and cancellation
The engine validates each request before dispatch and executes calls sequentially. Unknown, denied, invalid and duplicate calls consume an attempt. A valid call ID is reserved for the run even when its envelope is rejected. Only explicit transient model responses and idempotent lesson reads may be retried within the shared budgets. Practice and memory mutations are not retried automatically.
Pass an AbortSignal to cancel a run. One monotonic deadline limits local waiting, including noncooperative asynchronous calls. Late results are discarded. Synchronous adapter code cannot be preempted; remote cancellation and mutation reconciliation remain host duties.
Handle results
Inspect status before reading plan or advisory text. Sessions report bounded usage, provenance and tool outcomes. An unavailable required lesson stops generation; optional incomplete context can accompany advisory text. Earlier memory admissions remain committed if later generation fails.
See contracts and limits, memory and committed summaries.