5.8 KiB
5.8 KiB
Scriptorium Architecture
1. Purpose and Non-Goals
Scriptorium is a prompt-definition execution engine.
It accepts named input artifacts, renders prompt templates, calls an LLM, validates output, optionally performs bounded structured-output repair, and returns an artifact with metadata.
Scriptorium is not an orchestrator. It must not own transcription, transcript merge/polish steps, notifications, or cross-step workflow control.
For the motivating D&D workflow:
- Narratio orchestrates.
- WhisperX transcribes.
- Seriatim merges transcripts.
- Audita polishes transcripts.
- Scriptorium generates final artifacts from prepared inputs.
Core Go code remains generic.
2. Current Architecture
Scriptorium uses a ports-and-adapters architecture to decouple the core execution logic from external dependencies.
Package Responsibilities
cmd/scriptorium: Binary entrypoint for CLI and HTTP server.internal/domain: Core domain contracts, includingPromptDefinition,ExecutionProfile, andRunResult.internal/usecase:Runnerorchestration, including the logic for profile selection, runtime override resolution, and bounded repair.internal/promptdef: Repository for loading and validating Prompt Definitions from the filesystem.internal/profile: Repository for loading Execution Profiles from the filesystem.internal/artifact: Input artifact resolution (inline,file).internal/prompt: Template rendering via Go templates.internal/llm: Provider-neutral client interface and OpenAI-compatible HTTP adapter.internal/validate: Output validation implementation (none/basic/json/json_schema).internal/adapter/cli: CLI flag parsing and output handling.internal/adapter/http: HTTP request/response mapping.
3. Run Data Flow
The Runner.Run flow executes the following steps:
- Load Prompt Definition: Retrieve the
PromptDefinitionby ID from the prompt repository. - Select Profile: Determine the
profile_idusing the precedence:- Explicit
profile_idinRunRequest. default_profilespecified in thePromptDefinition.- Error if neither is available.
- Explicit
- Load Execution Profile: Retrieve the
ExecutionProfilefrom the profile repository. - Resolve Runtime Overrides: Merge settings based on precedence (Highest to Lowest):
- Runtime overrides (CLI flags or HTTP
modelobject). - Execution Profile settings.
- Built-in application defaults.
- Runtime overrides (CLI flags or HTTP
- Resolve Artifacts: Load all named input artifacts defined in the request.
- Render Prompt: Apply template variables and input artifacts to the prompt templates.
- Call LLM: Execute the generation request using the resolved
ExecutionTarget. - Validate/Repair:
- Validate the model output against the output contract.
- If structured validation fails and
repair_attempts > 0, perform bounded repair and re-validate.
- Return Result: Produce a
RunResultcontaining the final artifact, metadata, and validation status.
4. Domain Model
Key domain types:
PromptDefinition: Defines the "what" (templates, inputs, validation contract, and an optionaldefault_profile).ExecutionProfile: Defines the "how" (endpoint, model, generation parameters, andapi_key_env).RunRequest: The intent to execute a prompt, includingprompt_id, optionalprofile_id, inputs, variables, and optional runtime overrides.RunResult: The outcome of a run, including the generatedArtifact,ValidationResult, and auditingRunMetadata.RunMetadata: Detailed tracing info:prompt_id,selected_profile_id, model params, usage tokens, and hashes.
5. Interfaces and Adapters
Primary Ports
promptdef.Repository: Lookup for prompt definitions.profile.Repository: Lookup for execution profiles.artifact.Reader: Loading of artifact content.prompt.Renderer: Template rendering.llm.Client: Model generation.validate.Validator: Output validation.
Current Adapters
- Repositories: Filesystem YAML loaders for both prompts and profiles.
- Artifact Reader: Composite reader supporting
fileandinline. - Prompt Renderer: Go templates with a custom
inputhelper. - LLM Client: OpenAI-compatible
/chat/completionsover HTTP. - Validator: Standard validator supporting
none,basic,json, andjson_schema.
6. Public Contracts
CLI
run: Executes a prompt. Uses flags like--prompt,--profile,--input, and various runtime overrides (e.g.,--model,--temperature).serve: Starts the HTTP API.
HTTP API
POST /v1/runs: AcceptsRunRequestJSON and returnsRunResponseJSON. No built-in auth.
YAML Shapes
- Prompt YAML: Includes
id,version,default_profile,inputs,templates, andvalidation. - Profile YAML: Includes
id,endpoint,model, generation params, andapi_key_env.
7. Guardrails
- Separation of Concerns: Prompt content must not belong in execution profiles; model/API settings must not belong in prompt definitions.
- Security: Raw API keys are unsupported in all configuration and transport layers. Only
api_key_envis used. - Path Resolution:
content_filepaths in prompt definitions resolve relative to the prompt YAML file. - Integrity: No silent prompt truncation or omission of content.
- Reliability: Repair loops are strictly bounded by
repair_attempts.
8. Extension Points
Future work should remain grounded in the current architecture:
- Artifacts: Add S3 artifact references via a new
artifact.Reader. - LLM: Implement additional provider adapters (e.g., Anthropic, Google).
- Execution: Add token budgeting, streaming generation, and batch execution capabilities.
- Repositories: Implement database-backed repositories for prompts and profiles.
- Profiles: Support more granular profile versioning and environment-specific profiles.