Skip to Content
Motoko

The call protocol

A call is a client's request of one instance. callSchemas defines each call once with zod: what it takes (params), what it answers (result), and whether it changes the instance's sessions (writes). Every transport carries the same calls, as @chia1104/motoko-cloudflare does over a WebSocket to a Durable Object, and @chia1104/motoko-node over HTTP, and @chia1104/motoko-client makes them over either. A transport frames them in its own envelope and adds what only it has, such as following several sessions on one socket.

methodparamsresultwrites
createSessionrequestId?a sessionyes
listSessions{}[session], oldest firstno
submitsessionId, text, requestId, whenBusy?, replyTo?{ submissionId, title? }yes
continuesessionId, requestId{ submissionId }yes
approvesessionId, decisions: [{ toolCallId, approved, comment? }][{ toolCallId, outcome }]yes
replysessionId, toolCallId, token, text, isError?{ outcome }yes
abortsessionIdnullyes
compactsessionId, instructions?nullyes
withdrawsessionId, submissionId{ outcome }yes
renameSessionsessionId, title: string | nulla sessionyes
deleteSessionsessionIdnullyes

A session, as these answer it, is { id, createdAt, updatedAt, title?, metadata? }, what a list of sessions shows. createdAt and updatedAt are epoch ms; updatedAt is its creation, or the last submit or continue it admitted, an ask's aside. title is the name renameSession gave it, or else the start of the first message submitted to it, absent before either: titleOf(text) makes one, each run of whitespace one space, trimmed and cut to MAX_TITLE_LENGTH (100) characters. renameSession with title: null puts the first message's back, and refuses a title that is blank or longer than that (invalid_request). metadata is a JSON object the server gave the session, such as the project it is filed under; no call of a client sets it.

submit's whenBusy is queue (default), steer or reject (see Steering and the queue), and its submissionId is what withdraw takes back while the input waits. Its title is the session's title once the input was admitted, as listSessions lists it then, absent for an ask. withdraw's outcome is withdrawn, already_placed when a run took the input first, or settled when it was answered, withdrawn or dropped already, so it may be sent again. abort drops every input waiting too. compact asks for a compaction, with instructions for what its summary should keep, and is answered once it is admitted; the stream shows it go (see Compactions).

continue starts a run on the session's context as it is, without a new message: it carries on a run that ended without an answer, such as one at its turn limit (run:end with code: "turn_limit") or one whose model request found no credentials (code: "no_credentials", once the app holds the key again), from where it stopped. Inputs the failed run left waiting join it, so its first user event is theirs; otherwise it shows run:start and the model's reply, with no user event. It is refused while a run is going (session_busy), and a context with nothing to answer ends its run at once (run:end with code: "nothing_to_answer"). Its requestId, like submit's, makes a retry return the first continue.

submit's replyTo makes the submission an ask: the asked instance delivers the answer of the run it starts to that reply address (replyAddressSchema: { scope, sessionId, toolCallId, token }), as a reply call to the asker. An ask always queues: it takes no whenBusy: "reject" or "steer", and it cannot be withdrawn, since its asker waits for its answer. reply answers a deferred call with the token of its address. Its outcome is applied, or already_replied for a call that has its reply, so a delivery may be sent again.

CallParams<M> is what a sender writes, CallResult<M> what a receiver reads, and Call<M> a call of method M with its params parsed, { method, params }, as the server runs it. Call alone is a call of any method. A readonly caller may make only the calls whose writes is false. @chia1104/motoko-core's runCall runs a Call on an instance, and its callErrorOf maps what the instance threw to a code.

A call that fails answers a CallError, { code, message } (callErrorSchema):

codeWhen
invalid_requestA call that does not parse, or core's InvalidRequestError (a refused replyTo or title, or a withdraw of an ask's input or of one the session does not hold).
session_not_foundCore's SessionNotFoundError; also the end of a follow whose session was deleted.
session_busyCore's SessionBusyError: submit with whenBusy: "reject", or continue, while a run is going.
session_ownedCore's SessionOwnedError: submit, continue, withdraw, compact, renameSession or deleteSession of a child session.
approval_not_pendingCore's ApprovalNotPendingError; no decision of the batch was applied.
reply_not_pendingCore's ReplyNotPendingError: no such call waits for this reply, or its token is wrong.
invalid_cursorA cursor of another session, or core's InvalidCursorError.
instance_closedCore's InstanceClosedError: the instance lost its lease or its storage failed.
lease_heldCore's LeaseHeldError: another writer holds the instance.
forbiddenA readonly caller's call that writes, or one the app's authorize refused; nothing ran.
internalAnything else; the message says nothing, and the server reports the error.

A client that follows a session gets each event as { sessionId, cursor, event } (followedEventSchema), and { sessionId, error } (followEndedSchema) when the server stops following it: the session was deleted, or it cannot be read. sessionIdSchema is the session id every call and frame carries.