Skip to Content
Motoko

API

new MotokoCapability<Ports>({
  agent,                                     // the one agent definition the object runs
  models | modelsFor,                        // createAgentRuntime's: exactly one
  ports | portsFor,                          // createAgentRuntime's: exactly one
  closeIdleAfterMs?, defaultModel?, onReport?, settings?, deliverReply?,  // createAgentRuntime's
  store?,                                    // an AgentStore; default durableObjectStore(object's storage, scope)
  connections?,                              // MotokoConnections: the Agent itself, or a plain object's WebSockets
  authorize?,                                // (connection, call: MotokoCall) => boolean | Promise<boolean>
  sessionOptions?,                           // (connection) => what a session createSession makes runs with
})
capability.instance(): Promise<AgentInstance>  // waits for the object's startup; core's AgentInstance
capability.scope: StorageScope                 // { kind: agent.kind, name: <the object's name> }
capability.host: AgentHost                     // the store, with wake: { kind: "alarm", arm }
capability.dispose(): Promise<void>            // the Lifecycle's teardown: closes the runtime, keeps the data
durableObjectStore(storage, scope, { prefix? }): AgentStore
type MotokoConnections = { getConnections(): Iterable<Connection> }
  & ({ isConnectionReadonly(connection): boolean } | { isReadonly(connection): boolean })  // Agent's | WebSockets'
type MotokoCall = { method: "subscribe", params: { sessionId, from? } } | { method: "submit", params: ... } | ...
MAX_SUBSCRIPTIONS                              // 8: the sessions one connection may follow

The options are createAgentRuntime's for one agent, typed by @chia1104/motoko-core (AgentRuntimeModelsOption, AgentRuntimePortsOption), so modelsFor(scope) and portsFor({ scope, sessionId, ... }) work as there, with this object's scope. instance() resolves to core's AgentInstance: createSession, listSessions, session(id) with submit, events, approve, reply, abort, configure, rename and delete, as @chia1104/motoko-core's README describes them.

Install it on an Agent in the constructor, after super(): this.lifecycle.use(this.motoko), with connections: this. On a plain Durable Object, declare it before the lifecycle field, with the WebSockets capability as its connections:

readonly webSockets = new WebSockets({ handlers: { onMessage: (connection, message) => { /* the app's */ } } });
readonly motoko = new MotokoCapability({ agent, models, ports, connections: this.webSockets });
readonly lifecycle = Lifecycle.install(this).use(this.motoko).use(this.webSockets);

instance() waits for the Lifecycle's startup itself, so it may be called from an RPC method of either.

Its capability id is motoko and its job motoko-wake, in the SDK's cf_agents_jobs table; neither changes, so a pending wake-up outlives a deploy.

The client and the hook are @chia1104/motoko-client's: motokoClient(webSocketTransport(socket)) over the SDK's socket, and useMotokoSession(client, sessionId). Its README lists their API, and how the socket transport queues calls, fails the ones a close cut off, and follows again on every open.