Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

19.7 KB
Raw Blame
# Generated from protocol/rpc-schema.json by packages/adk/scripts/generate.mjs.
# Do not edit by hand: run `node packages/adk/scripts/generate.mjs` after
# changing the Rust dispatcher, and commit the regenerated output with it.

from __future__ import annotations

from typing import Any, Literal, NotRequired, TypedDict, Union

PROTOCOL_VERSION = 1

#: The JSON-RPC methods this protocol accepts, in schema order.
RPC_METHODS: tuple[str, ...] = (
    "initialize",
    "session/list",
    "session/start",
    "session/resume",
    "session/fork",
    "session/archive",
    "turn/start",
    "turn/steer",
    "turn/interrupt",
    "permission/respond",
    "tool/respond",
    "session/tools/get",
    "session/tools/set",
    "session/model/set",
    "session/reset",
)

#: The ``type`` tag of an event, which is also its ``item/<tag>`` method name.
EVENT_TAGS: tuple[str, ...] = (
    "token",
    "reasoning",
    "step",
    "tool_call_started",
    "tool_call_args_delta",
    "tool_call",
    "tool_output_delta",
    "tool_result",
    "subagent_start",
    "subagent_queued",
    "subagent_end",
    "subagent_plan",
    "subagent",
    "notice",
    "agent_message",
    "compaction",
    "retry",
    "monitors",
    "parked",
    "messages_updated",
    "ask_request",
    "ask_resolved",
    "todo_update",
    "turn_usage",
    "done",
    "error",
    "permission_request",
    "tool_request",
    "tool_request_cancelled",
    "tool_details",
    "request_provenance",
)

RpcMethod = Literal[
    "initialize",
    "session/list",
    "session/start",
    "session/resume",
    "session/fork",
    "session/archive",
    "turn/start",
    "turn/steer",
    "turn/interrupt",
    "permission/respond",
    "tool/respond",
    "session/tools/get",
    "session/tools/set",
    "session/model/set",
    "session/reset",
]

EventTag = Literal[
    "token",
    "reasoning",
    "step",
    "tool_call_started",
    "tool_call_args_delta",
    "tool_call",
    "tool_output_delta",
    "tool_result",
    "subagent_start",
    "subagent_queued",
    "subagent_end",
    "subagent_plan",
    "subagent",
    "notice",
    "agent_message",
    "compaction",
    "retry",
    "monitors",
    "parked",
    "messages_updated",
    "ask_request",
    "ask_resolved",
    "todo_update",
    "turn_usage",
    "done",
    "error",
    "permission_request",
    "tool_request",
    "tool_request_cancelled",
    "tool_details",
    "request_provenance",
]

# -------------------------------------------------------------------------
# Shared definitions
# -------------------------------------------------------------------------

class AskRequest(TypedDict):
    questions: list[Question]

class ClientInfo(TypedDict):
    name: str
    version: str

# `Deserialize` as well as `Serialize`: a consumer validates what it received against these shapes, and `JsonSchema` is what `jan cli agent schema` publishes them as -- a consumer generates its own types from `protocol/schema.json` rather than copying this declaration. The wire has to survive a round trip, not just a write, so `#[serde(tag = "type")]` keeps the tag on both sides. Where a [`StreamEvent::Compaction`] is in its round trip.
CompactionPhase = Union[Literal["started", "finished"], Literal["failed"]]

# Which path asked for a [`StreamEvent::Compaction`].
CompactionReason = Union[Literal["preflight"], Literal["context_overflow"], Literal["session_budget"]]

# What a finished compaction did, carried on [`StreamEvent::Compaction`] so a consumer can show the summarizer as an agent of its own: what it read, which model wrote the summary, what it cost and what it wrote.
class CompactionReport(TypedDict):
    # The model that wrote the summary: the `smol` model when it answered, else the run's own. `None` when no model answered and the history was folded into Jan's fallback note instead.
    model: NotRequired[Union[str, None]]
    # Estimated tokens of the span that was folded, before it was clamped for the summarizer: how much conversation the summary stands in for.
    input_tokens: int
    # The summarizer's own reported usage, every request it took included.
    usage: NotRequired[Union[Usage, None]]
    # The summary that now stands in for the folded span.
    summary: str

# What a host says a tool does, which decides how the loop treats it. Absent means opaque: prompted unless `auto_approve`, sequential, withheld in Plan mode -- the plugin/MCP default.
HostCapability = Union[Literal["read"], Literal["actuator"]]

# The declaration shape, for the document only: the dispatcher parses the real `HostToolDecl`, which lives outside this module and carries no schema.
class HostToolDeclSchema(TypedDict):
    name: str
    description: NotRequired[str]
    # JSON Schema for the arguments, advertised to the model verbatim.
    parameters: NotRequired[Union[dict[str, Any], None]]
    capability: NotRequired[Union[HostCapability, None]]

# Display-only view of one active monitor, for a status panel.
class MonitorSnapshot(TypedDict):
    monitorId: str
    name: str
    script: str
    polls: int

class OptionItem(TypedDict):
    label: str
    description: NotRequired[Union[str, None]]

# A subagent in a not-yet-started phase of a phased dispatch: its name (unique across the plan, and its blackboard file) and 1-based phase number. Carried by [`StreamEvent::SubagentPlan`] so a consumer can show it waiting on the phase before it.
class PendingSubagent(TypedDict):
    name: str
    phase: int

# Who gates host tool calls. `jan` prompts through `permission_request` the way any opaque tool is prompted; `host` means the host's own callback is the gate, so Jan never asks about a host tool.
PermissionOwner = Literal["jan", "host"]

# One image in an outbound request, as [`StreamEvent::RequestProvenance`] reports it: identity, not content.
class ProvenanceImage(TypedDict):
    # SHA-256 over the image's decoded bytes.
    sha256: str
    mime_type: str
    # Decoded length in bytes.
    bytes: int
    # The host tool call whose result carried it, when one did. `None` for an image the user attached.
    tool_call_id: NotRequired[Union[str, None]]

class Question(TypedDict):
    id: str
    question: str
    options: list[OptionItem]
    multi: NotRequired[bool]
    recommended: NotRequired[Union[int, None]]

class TodoItem(TypedDict):
    content: str
    status: TodoStatus

class TodoList(TypedDict):
    phases: list[TodoPhase]

class TodoPhase(TypedDict):
    name: str
    tasks: list[TodoItem]

TodoStatus = Literal["pending", "in_progress", "completed", "abandoned"]

# A host tool's answer: text, or OpenAI content parts (`text` / `image_url` with a base64 `data:` URL) under the same caps as a user message.
ToolResultContent = Union[str, list[Any]]

class Usage(TypedDict):
    prompt_tokens: NotRequired[Union[int, None]]
    completion_tokens: NotRequired[Union[int, None]]
    total_tokens: NotRequired[Union[int, None]]
    # Prompt tokens served from the provider's prompt cache (a read/hit). OpenAI reports it under `prompt_tokens_details.cached_tokens`; Anthropic under `cache_read_input_tokens`. A thrashing cache shows here as a low value against a high `prompt_tokens`.
    cached_tokens: NotRequired[Union[int, None]]
    # Prompt tokens written into the provider cache this request (a write). Only Anthropic bills this separately (`cache_creation_input_tokens`); absent for providers that do not distinguish reads from writes.
    cache_write_tokens: NotRequired[Union[int, None]]

# -------------------------------------------------------------------------
# Request parameters, one type per Rust params struct
# -------------------------------------------------------------------------

class InitializeParams(TypedDict):
    """`initialize`"""

    protocolVersion: int
    clientInfo: ClientInfo
    capabilities: NotRequired[Any]

class SessionListParams(TypedDict):
    """`session/list`"""
    pass

class SessionStartParams(TypedDict):
    """`session/start`"""

    cwd: str
    model: NotRequired[Union[str, None]]
    ephemeral: NotRequired[bool]
    # Host tools this session may call. Kept as raw values until declaration so a malformed entry is reported as `invalid_tools` with the reason, rather than as a generic params error that names nothing.
    tools: NotRequired[list[HostToolDeclSchema]]
    # `false` advertises only the host tools: no built-ins, MCP, plugin, `ask`, `todo` or monitor tools. `subagents: true` adds back only `dispatch_subagent`, `list_subagents`, `message_subagent` and `stop_subagent`.
    builtins: NotRequired[bool]
    # Whether the session may delegate to subagents; defaults to `builtins`. With `builtins: false` it adds only `dispatch_subagent`, `list_subagents`, `message_subagent` and `stop_subagent`, and every child is held to the session's host tools. `false` withholds subagent tools even from a session with built-ins.
    subagents: NotRequired[Union[bool, None]]
    # The session's whole system prompt, sent byte for byte in place of the one Jan composes: no Jan identity, guides, environment, date, git state or recalled project memory. Its answers are still indexed for later sessions unless it is `ephemeral`. Jan may still append runtime notices to the conversation. Children keep their own prompts.
    systemPrompt: NotRequired[Union[str, None]]
    permissions: NotRequired[PermissionOwner]

class SessionIdParams(TypedDict):
    """`session/resume`, `session/fork`, `session/archive`, `turn/interrupt`, `session/tools/get`, `session/reset`"""

    sessionId: str

class TurnStartParams(TypedDict):
    """`turn/start`"""

    sessionId: str
    input: Any

class TurnSteerParams(TypedDict):
    """`turn/steer`"""

    sessionId: str
    input: str

class PermissionResponseParams(TypedDict):
    """`permission/respond`"""

    requestId: str
    decision: str

class ToolRespondParams(TypedDict):
    """`tool/respond`"""

    requestId: str
    content: ToolResultContent
    isError: NotRequired[bool]
    # Host/UI-only data, echoed as `item/tool_details` and never sent to the model.
    details: NotRequired[Union[dict[str, Any], None]]

class SessionToolsSetParams(TypedDict):
    """`session/tools/set`"""

    sessionId: str
    tools: list[HostToolDeclSchema]

class SessionModelSetParams(TypedDict):
    """`session/model/set`"""

    sessionId: str
    model: str

# -------------------------------------------------------------------------
# Events: the payload of an ``item/<tag>`` notification
# -------------------------------------------------------------------------

class TokenEvent(TypedDict):
    """`item/token`"""

    text: str
    type: Literal["token"]

class ReasoningEvent(TypedDict):
    """`item/reasoning`"""

    text: str
    type: Literal["reasoning"]

class StepEvent(TypedDict):
    """`item/step`"""

    index: int
    max: int
    type: Literal["step"]

class ToolCallStartedEvent(TypedDict):
    """`item/tool_call_started`"""

    id: str
    name: str
    type: Literal["tool_call_started"]

class ToolCallArgsDeltaEvent(TypedDict):
    """`item/tool_call_args_delta`"""

    id: str
    delta: str
    type: Literal["tool_call_args_delta"]

class ToolCallEvent(TypedDict):
    """`item/tool_call`"""

    id: str
    name: str
    args: Any
    type: Literal["tool_call"]

class ToolOutputDeltaEvent(TypedDict):
    """`item/tool_output_delta`"""

    id: str
    delta: str
    type: Literal["tool_output_delta"]

class ToolResultEvent(TypedDict):
    """`item/tool_result`"""

    id: str
    content: str
    is_error: bool
    diff: NotRequired[Union[str, None]]
    type: Literal["tool_result"]

class SubagentStartEvent(TypedDict):
    """`item/subagent_start`"""

    run_id: str
    name: str
    # The task the child was dispatched with -- its sole user message. Carried on the event rather than left for consumers to correlate back to the `dispatch_subagent` call: two dispatches can share a `subagent_name`, so matching on name alone is ambiguous.
    task: NotRequired[Union[str, None]]
    # Where the child works: its own worktree and branch, or the checkout it was given, marked when read-only.
    workspace: NotRequired[Union[str, None]]
    # An agent Jan runs for itself (memory tidy) rather than one the model dispatched. A consumer lists it with the other subagents, marked as Jan's own, and keeps its run out of the conversation.
    internal: NotRequired[bool]
    type: Literal["subagent_start"]

class SubagentQueuedEvent(TypedDict):
    """`item/subagent_queued`"""

    run_id: str
    name: str
    task: NotRequired[Union[str, None]]
    waiting: int
    type: Literal["subagent_queued"]

class SubagentEndEvent(TypedDict):
    """`item/subagent_end`"""

    run_id: str
    name: str
    # Why the child failed, when it did: the same reason the parent's `<SYSTEM>` completion ping carries. Carried on the event because a background child's answer never reaches a consumer -- only the model reads it -- so without this a failed run is indistinguishable from a clean one on screen. `None` for a clean finish, and for a child cut off at parent teardown, which is not its own failure.
    error: NotRequired[Union[str, None]]
    type: Literal["subagent_end"]

class SubagentPlanEvent(TypedDict):
    """`item/subagent_plan`"""

    pending: list[PendingSubagent]
    type: Literal["subagent_plan"]

class SubagentEvent(TypedDict):
    """`item/subagent`"""

    run_id: str
    name: str
    event: Any
    type: Literal["subagent"]

class NoticeEvent(TypedDict):
    """`item/notice`"""

    text: str
    type: Literal["notice"]

# `item/agent_message`
AgentMessageEvent = TypedDict("AgentMessageEvent", {
    "from": str,
    "to": str,
    "text": str,
    "type": Literal["agent_message"],
})

class CompactionEvent(TypedDict):
    """`item/compaction`"""

    phase: CompactionPhase
    reason: CompactionReason
    messages: NotRequired[Union[int, None]]
    report: NotRequired[Union[CompactionReport, None]]
    type: Literal["compaction"]

class RetryEvent(TypedDict):
    """`item/retry`"""

    attempt: int
    max_attempts: int
    delay_ms: int
    reason: str
    type: Literal["retry"]

class MonitorsEvent(TypedDict):
    """`item/monitors`"""

    monitors: list[MonitorSnapshot]
    type: Literal["monitors"]

class ParkedEvent(TypedDict):
    """`item/parked`"""

    type: Literal["parked"]

class MessagesUpdatedEvent(TypedDict):
    """`item/messages_updated`"""

    messages: list[Any]
    type: Literal["messages_updated"]

class AskRequestEvent(TypedDict):
    """`item/ask_request`"""

    request_id: str
    request: AskRequest
    timeout_secs: NotRequired[Union[int, None]]
    type: Literal["ask_request"]

class AskResolvedEvent(TypedDict):
    """`item/ask_resolved`"""

    request_id: str
    type: Literal["ask_resolved"]

class TodoUpdateEvent(TypedDict):
    """`item/todo_update`"""

    list: TodoList
    type: Literal["todo_update"]

class TurnUsageEvent(TypedDict):
    """`item/turn_usage`"""

    usage: Usage
    # The provider's id for the execution that produced this usage, when it reported one. This is the handle a per-request billing lookup is keyed by, so a consumer can ask what this one request actually cost rather than only what it estimates. A sibling of `usage` rather than a field inside it, because it is a billing handle and not a token count. Absent on the default upstream path, which cannot see the response headers (see [`crate::core::agent::correlation`]); that is a limitation to report, not a reason to synthesize one.
    execution_id: NotRequired[Union[str, None]]
    type: Literal["turn_usage"]

class DoneEvent(TypedDict):
    """`item/done`"""

    stop_reason: str
    usage: NotRequired[Union[Usage, None]]
    type: Literal["done"]

class ErrorEvent(TypedDict):
    """`item/error`"""

    code: str
    message: str
    type: Literal["error"]

class PermissionRequestEvent(TypedDict):
    """`item/permission_request`"""

    request_id: str
    tool_name: str
    capability: str
    path: NotRequired[Union[str, None]]
    # The shell command for exec prompts (drives the command-scoped "allow always" grant); `None` for non-exec tools.
    command: NotRequired[Union[str, None]]
    # Focused diff preview for `write`/`edit` prompts so the user sees the change before approving; `None` for other tools.
    diff: NotRequired[Union[str, None]]
    prompt_kind: str
    offers_always: bool
    # The model's reason for an `escalate` prompt (running a shell command outside the sandbox), shown beside the command; `None` otherwise.
    justification: NotRequired[Union[str, None]]
    type: Literal["permission_request"]

class ToolRequestEvent(TypedDict):
    """`item/tool_request`"""

    request_id: str
    tool_name: str
    # The arguments the model produced, already parsed from the call's JSON string. Validated against nothing here -- the host owns the schema it declared and is the only party that can enforce it.
    args: Any
    # Which run raised the request: `None` for the main run, the child's run id for a subagent. Attribution only -- the host answers by `request_id` alone, and a child's request is emitted unwrapped at the top level so the same answer path serves both.
    run_id: NotRequired[Union[str, None]]
    type: Literal["tool_request"]

class ToolRequestCancelledEvent(TypedDict):
    """`item/tool_request_cancelled`"""

    request_id: str
    reason: str
    type: Literal["tool_request_cancelled"]

class ToolDetailsEvent(TypedDict):
    """`item/tool_details`"""

    id: str
    details: Any
    type: Literal["tool_details"]

class RequestProvenanceEvent(TypedDict):
    """`item/request_provenance`"""

    # Which run made the request: `None` for the main run, the child's run id for a subagent.
    run_id: NotRequired[Union[str, None]]
    # The session the request belongs to, as the run's handshake names it (the correlation id the request carries is derived from it).
    session_id: NotRequired[Union[str, None]]
    # The configured provider the model resolved to.
    provider: NotRequired[Union[str, None]]
    # The model id the upstream receives, without a `<provider>/` prefix.
    model: str
    # The wire API the request is built for (`anthropic`, `google`, `openai-responses`), absent for chat/completions.
    api_type: NotRequired[Union[str, None]]
    # SHA-256 of the request body as Jan built it, as canonical JSON: every object's keys sorted, recursively, so re-encoding the same members in another order gives the same digest. It is the value that makes two runs comparable even when a field this record does not itemize has changed. The body hashed is the one Jan built, before the provider adapter appends its transport fields (`stream`, `stream_options`), so it is not byte-for-byte what the provider received: a harness recomputes it by sorting keys and dropping those two fields.
    request_sha256: str
    # The canonical body's serialized length. Key order does not change it, so it describes the built body either way.
    body_bytes: int
    # SHA-256 of the `tools` array as sent, canonical JSON in the same sense, able to change while the model id does not.
    tools_sha256: NotRequired[Union[str, None]]
    # Every image in the body, in order, hashed over its decoded bytes so the host can hash the same frame it captured.
    images: NotRequired[list[ProvenanceImage]]
    type: Literal["request_provenance"]

#: Every event a session may report, discriminated on ``type``.
StreamEvent = Union[
    TokenEvent,
    ReasoningEvent,
    StepEvent,
    ToolCallStartedEvent,
    ToolCallArgsDeltaEvent,
    ToolCallEvent,
    ToolOutputDeltaEvent,
    ToolResultEvent,
    SubagentStartEvent,
    SubagentQueuedEvent,
    SubagentEndEvent,
    SubagentPlanEvent,
    SubagentEvent,
    NoticeEvent,
    AgentMessageEvent,
    CompactionEvent,
    RetryEvent,
    MonitorsEvent,
    ParkedEvent,
    MessagesUpdatedEvent,
    AskRequestEvent,
    AskResolvedEvent,
    TodoUpdateEvent,
    TurnUsageEvent,
    DoneEvent,
    ErrorEvent,
    PermissionRequestEvent,
    ToolRequestEvent,
    ToolRequestCancelledEvent,
    ToolDetailsEvent,
    RequestProvenanceEvent,
]