Journal API Contract

The Pydantic models of the envelope and the error body of every journaled response, and of a journal entry. See Smarter Journal.

Pydantic models of the Smarter Journal api contract.

They describe the envelope that smarter.lib.journal.http.SmarterJournaledJsonResponse adds to every journaled response, the error body of smarter.lib.journal.http.SmarterJournaledJsonErrorResponse, and a journal entry, a smarter.lib.journal.models.SAMJournal row.

These models are the contract’s single source of truth:

  • the journaled responses are built from them, by smarter.lib.journal.http,

  • the JSON Schema in journal-contract.schema.json is generated from them (manage.py export_journal_contract_schema), for the clients that read journaled responses, such as the smarter cli.

The envelope and the error body allow extra fields, because each view adds its own data beside them. The model of a journal entry (SAMJournalApiOrmModel) has exactly the row’s fields. Adding a field is backward compatible. Removing or renaming one that is declared here is not.

smarter.lib.journal.contract.models.CONTRACT_MODELS: tuple[type[BaseModel], ...] = (<class 'smarter.lib.journal.contract.models.SAMJournalApiEnvelope'>, <class 'smarter.lib.journal.contract.models.SAMJournalApiErrorResponse'>, <class 'smarter.lib.journal.contract.models.SAMJournalApiResponse'>, <class 'smarter.lib.journal.contract.models.SAMJournalApiOrmModel'>)

The models whose JSON Schema is exported.

Their dependencies are exported with them.

(
    <class 'smarter.lib.journal.contract.models.SAMJournalApiEnvelope'>,
    <class 'smarter.lib.journal.contract.models.SAMJournalApiErrorResponse'>,
    <class 'smarter.lib.journal.contract.models.SAMJournalApiResponse'>,
    <class 'smarter.lib.journal.contract.models.SAMJournalApiOrmModel'>,
)
smarter.lib.journal.contract.models.JOURNAL_API_MODELS: tuple[type[BaseModel], ...] = (<class 'smarter.lib.journal.contract.models.SAMJournalApiEntry'>, <class 'smarter.lib.journal.contract.models.SAMJournalApiPage'>)

The models whose JSON Schemas the Smarter api’s OpenAPI document includes.

(
    <class 'smarter.lib.journal.contract.models.SAMJournalApiEntry'>,
    <class 'smarter.lib.journal.contract.models.SAMJournalApiPage'>,
)
smarter.lib.journal.contract.models.SAMJournalApiCommand

the choices of SAMJournal.command.

See smarter.lib.journal.enum.SmarterJournalCliCommands.choices().

Type:

A journal entry’s command

alias of Literal[‘apply’, ‘prompt’, ‘chat_config’, ‘delete’, ‘deploy’, ‘describe’, ‘get’, ‘journal’, ‘logs’, ‘example_manifest’, ‘status’, ‘json_schema’, ‘version’, ‘undeploy’, ‘validate’, ‘whoami’, ‘resources’, ‘documents’]

class smarter.lib.journal.contract.models.SAMJournalApiEntry(*, key, createdAt=None, user=None, thing, command, statusCode, request, response)[source]

Bases: SAMJournalApiEntrySummary

A journal entry, as the journal’s REST api returns it by its key: with its request and response documents.

It is the output of smarter.lib.journal.serializers.SAMJournalSerializer, in camelCase. The request document holds the request’s url, an allowlist of its headers, and its body, with its credentials redacted: never the caller’s cookies or api key. See HttpAnonymousRequestSerializer.

classmethod from_model(journal)[source]

The REST api’s model of a journal entry.

Parameters:

journal (SAMJournal) – The journal entry, whose user should be fetched with it.

Return type:

SAMJournalApiEntry

Returns:

Its model.

model_config = {'alias_generator': <function to_camel>, 'extra': 'forbid', 'serialize_by_alias': True, 'validate_by_alias': True, 'validate_by_name': True}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class smarter.lib.journal.contract.models.SAMJournalApiEntrySummary(*, key, createdAt=None, user=None, thing, command, statusCode)[source]

Bases: BaseModel

A journal entry, as the journal’s list returns it: without its request and response documents.

It is the output of smarter.lib.journal.serializers.SAMJournalMiniSerializer, in camelCase. Unlike SAMJournalApiOrmModel, its user is the username.

classmethod from_model(journal)[source]

The REST api’s summary of a journal entry.

Parameters:

journal (SAMJournal) – The journal entry, whose user should be fetched with it.

Return type:

SAMJournalApiEntrySummary

Returns:

Its model.

model_config = {'alias_generator': <function to_camel>, 'extra': 'forbid', 'serialize_by_alias': True, 'validate_by_alias': True, 'validate_by_name': True}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

classmethod summary_fields(journal)[source]

Return the fields of a journal entry that both of the REST api’s models have.

Parameters:

journal (SAMJournal) – The journal entry, whose user should be fetched with it.

Return type:

dict[str, Any]

Returns:

The fields, by name.

class smarter.lib.journal.contract.models.SAMJournalApiEnvelope(*, api='smarter.sh/v1', metadata, **extra_data)[source]

Bases: SAMJournalApiModel

The fields that every journaled response adds beside the view’s own data.

classmethod build(command, thing)[source]

The envelope of a response, without a journal key.

Parameters:
  • command (object) – The command that the view passed, e.g. a SmarterJournalCliCommands, a string, or None.

  • thing (object) – The thing that the view passed, e.g. a SmarterJournalThings, a string, or None.

Return type:

SAMJournalApiEnvelope

Returns:

The envelope.

dump()[source]

The envelope as the JSON keys that a journaled response adds: api and metadata.

The metadata’s key is left out until the journal entry is saved.

Return type:

dict[str, JsonValue]

Returns:

The envelope’s keys.

model_config = {'extra': 'allow'}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class smarter.lib.journal.contract.models.SAMJournalApiErrorBody(*, errorClass, stacktrace=None, description=None, status, args, cause, context, thing, command, **extra_data)[source]

Bases: SAMJournalApiModel

The error of a journaled error response: the same shape for every error of the cli and the other journaled apis.

The field names are the values of smarter.lib.journal.enum.SmarterJournalApiResponseErrorKeys.

model_config = {'extra': 'allow'}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class smarter.lib.journal.contract.models.SAMJournalApiErrorResponse(*, error, **extra_data)[source]

Bases: SAMJournalApiModel

The body of a journaled error response.

SAMJournalApiEnvelope’s api and metadata are added beside error.

dump(include_stack_trace=True)[source]

The error response’s JSON.

Parameters:

include_stack_trace (bool) – Whether the error includes its stacktrace.

Return type:

dict[str, JsonValue]

Returns:

The response’s error key.

model_config = {'extra': 'allow'}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

type smarter.lib.journal.contract.models.SAMJournalApiJson = _AllowAnyJson]

A value that is stored, and returned, as arbitrary JSON, e.g. a journal entry’s request and response documents.

class smarter.lib.journal.contract.models.SAMJournalApiMetadata(*, command, thing, key=None, **extra_data)[source]

Bases: SAMJournalApiModel

The metadata of a journaled response: its command and thing, and, when the journal is enabled, the key of its journal entry.

command and thing are the strings of the values that the view passed, which may be "None" when it passed none, so they are not limited to the journal’s choices.

model_config = {'extra': 'allow'}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class smarter.lib.journal.contract.models.SAMJournalApiModel(**extra_data)[source]

Bases: BaseModel

Base class of the contract’s models of responses, which allow extra fields.

model_config = {'extra': 'allow'}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class smarter.lib.journal.contract.models.SAMJournalApiOrmModel(*, key, created_at=None, user=None, thing, command, request, response, status_code)[source]

Bases: BaseModel

A journal entry: a smarter.lib.journal.models.SAMJournal row.

Its fields are exactly the row’s fields, so extra fields are forbidden. It is built from the row by from_model().

classmethod from_model(journal)[source]

The model of a journal entry.

Parameters:

journal (SAMJournal) – The journal entry.

Return type:

SAMJournalApiOrmModel

Returns:

Its model.

model_config = {'extra': 'forbid'}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class smarter.lib.journal.contract.models.SAMJournalApiPage(*, objects, pagination)[source]

Bases: BaseModel

One page of the journal’s entries, newest first unless sorted, and a description of the page.

model_config = {'extra': 'forbid'}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class smarter.lib.journal.contract.models.SAMJournalApiResponse(*, api='smarter.sh/v1', metadata, error=None, **extra_data)[source]

Bases: SAMJournalApiEnvelope

A journaled response: the envelope, beside the view’s own data, e.g. data or error.

model_config = {'extra': 'allow'}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

smarter.lib.journal.contract.models.SAMJournalApiThing

the choices of SAMJournal.thing.

See smarter.lib.journal.enum.SmarterJournalThings.choices().

Type:

A journal entry’s thing

alias of Literal[‘Account’, ‘ApiConnection’, ‘ApiPlugin’, ‘SmarterAuthToken’, ‘Budget’, ‘CustomDomain’, ‘Guardrail’, ‘ImageSearchPlugin’, ‘LLMClient’, ‘MCPClient’, ‘Orchestrator’, ‘Prompt’, ‘ChatConfig’, ‘Proxy’, ‘Provider’, ‘Secret’, ‘SkillPlugin’, ‘SqlConnection’, ‘SqlPlugin’, ‘Plugin’, ‘User’, ‘Vectorsearch’, ‘Vectorstore’, ‘WebsearchPlugin’]

The JSON Schema of the Smarter Journal api contract, generated from its Pydantic models.

manage.py export_journal_contract_schema writes it to journal-contract.schema.json, beside this module, and a test fails if the committed file is out of date.

smarter.lib.journal.contract.schema.journal_contract_schema()[source]

The contract’s JSON Schema (draft 2020-12): one definition, in $defs, per model.

Return type:

dict[str, Any]

Returns:

The schema.

smarter.lib.journal.contract.schema.journal_contract_schema_json()[source]

The contract’s JSON Schema, as the text of journal-contract.schema.json.

Return type:

str

Returns:

The schema, as indented JSON, with a final newline.

smarter.lib.journal.contract.schema.write_journal_contract_schema(path='/home/runner/work/smarter/smarter/smarter/smarter/lib/journal/contract/journal-contract.schema.json')[source]

Write the contract’s JSON Schema.

Parameters:

path (str) – The file to write. Defaults to journal-contract.schema.json, beside this module.

Return type:

str

Returns:

The path that was written.