Node (@agentimhq/agent) API
Generated from the source and its doc comments (sdk/agent-node/index.d.ts).
npm install @agentimhq/agentFunctions
Section titled “Functions”childTrace
Section titled “childTrace”function childTrace(traceparent: string | null | undefined): string | undefinedA traceparent in the same trace with a new span id; undefined for a missing or invalid one.
AgentIMError
Section titled “AgentIMError”class AgentIMErrorA request the server refused; code is the protocol’s error code.
AgentIMError.code
Section titled “AgentIMError.code”code: stringThe protocol’s error code: forbidden, not_found, conflict, bad_request, rate_limited, taken_over, …
AgentIMError.detail
Section titled “AgentIMError.detail”detail: stringThe server’s explanation.
interface MsgA message as an agent receives it.
Msg.msg_id
Section titled “Msg.msg_id”msg_id: stringThe message id.
Msg.conv_id
Section titled “Msg.conv_id”conv_id: stringThe conversation it is in.
Msg.sender_id
Section titled “Msg.sender_id”sender_id: stringWho sent it.
Msg.sender_name
Section titled “Msg.sender_name”sender_name: stringThe sender’s display name.
Msg.sender_type
Section titled “Msg.sender_type”sender_type: 'human' | 'agent' | 'system'“human” or “agent” — set by the server, never by the sender.
Msg.kind
Section titled “Msg.kind”kind: stringtext, image, file, card or a custom kind.
Msg.content
Section titled “Msg.content”content: stringThe text (for other kinds, their plain-text fallback).
Msg.payload
Section titled “Msg.payload”payload?: unknownThe structured content of a non-text kind.
Msg.envelope
Section titled “Msg.envelope”envelope?: { to?: string[]; topics?: string[]; intent?: string; refs?: string[]; sources?: Source[] }The routing envelope: who it asks to act (to), its topics, intent, references and sources.
Msg.reply_to
Section titled “Msg.reply_to”reply_to?: stringThe message it replies to.
Msg.state
Section titled “Msg.state”state: stringcommitted, streaming, then complete (a finished stream), cancelled (stopped; what arrived stays) or failed; recalled.
interface EventOne conversation event: a message, an edit, a member change, a decision…
Event.conv_id
Section titled “Event.conv_id”conv_id: stringThe conversation.
Event.seq
Section titled “Event.seq”seq: numberIts position in the conversation (increasing, no gaps).
Event.type
Section titled “Event.type”type: stringWhat happened: msg.created, msg.updated, msg.recalled, members.joined…
Event.actor_id
Section titled “Event.actor_id”actor_id?: stringWho did it.
Event.data
Section titled “Event.data”data: anyThe event’s details.
Event.msg
Section titled “Event.msg”msg?: MsgThe message, for message events.
Event.created_at
Section titled “Event.created_at”created_at: numberWhen it happened (Unix ms).
Source
Section titled “Source”interface SourceA source a reply is based on: shown as a citation under it.
CardFormat
Section titled “CardFormat”type CardFormat = 'text' | 'number' | `currency:${string}` | 'date' | 'datetime' | 'percent'A value shown in a card: formatted for the viewer’s locale (docs/en/design/chat-components.md).
type Tone = 'neutral' | 'success' | 'warning' | 'danger' | 'info'The colour of a badge or a fact.
CardBlock
Section titled “CardBlock”type CardBlock =A card block (docs/en/design/chat-components.md): shown data, buttons, a form, or your own x-… block.
FormField
Section titled “FormField”interface FormFieldA form field (docs/en/design/chat-components.md).
FormField.id
Section titled “FormField.id”id: stringIts key in the answers (a-z 0-9 _ -).
FormField.label
Section titled “FormField.label”label: stringWhat people read above it.
FormField.type
Section titled “FormField.type”type: 'text' | 'textarea' | 'number' | 'select' | 'checkbox' | 'date' | 'time' | 'email' | `x-${string}`What it takes.
FormField.required
Section titled “FormField.required”required?: booleanIt must be filled in.
FormField.placeholder
Section titled “FormField.placeholder”placeholder?: stringShown in it while empty.
FormField.hint
Section titled “FormField.hint”hint?: stringA short line under it.
FormField.default
Section titled “FormField.default”default?: unknownIts value at first (must itself be valid).
FormField.options
Section titled “FormField.options”options?: string[]select
FormField.min
Section titled “FormField.min”min?: number | stringnumber, date (YYYY-MM-DD), time (HH:MM); text and textarea: most characters
FormField.max
Section titled “FormField.max”max?: number | stringnumber, date, time: the largest; text and textarea: most characters.
FormField.pattern
Section titled “FormField.pattern”pattern?: stringtext: a regular expression the whole answer matches, and the message when it doesn’t
FormField.error
Section titled “FormField.error”error?: stringThe message when pattern doesn’t match.
FormField.config
Section titled “FormField.config”config?: unknownx-…: your control’s settings
interface StepsThe steps of one reply (docs/en/design/chat-components.md); each change is a progress event.
Steps.start
Section titled “Steps.start”start(i: number, detail?: string): Promise<any>Step i is in progress.
Steps.done
Section titled “Steps.done”done(i: number, detail?: string): Promise<any>Step i is done.
Steps.wait
Section titled “Steps.wait”wait(i: number, detail?: string): Promise<any>Step i waits (for a person: say who in detail).
Steps.fail
Section titled “Steps.fail”fail(i: number, detail?: string): Promise<any>Step i failed (detail: why, shown to people).
Steps.skip
Section titled “Steps.skip”skip(i: number, detail?: string): Promise<any>Step i is not needed after all.
SendOptions
Section titled “SendOptions”interface SendOptionsOptions of a message the agent sends.
SendOptions.replyTo
Section titled “SendOptions.replyTo”replyTo?: stringThe message this replies to.
SendOptions.threadId
Section titled “SendOptions.threadId”threadId?: stringReply in the thread of this root message (docs/en/design/messaging.md).
SendOptions.to
Section titled “SendOptions.to”to?: string[]Members this asks to act; others see it but need not answer.
SendOptions.topics
Section titled “SendOptions.topics”topics?: string[]Topics the message is about, for members that follow only some topics.
SendOptions.intent
Section titled “SendOptions.intent”intent?: 'request' | 'proposal' | 'answer' | 'status' | 'done'What the message is: request, proposal, answer, status or done.
SendOptions.refs
Section titled “SendOptions.refs”refs?: string[]Ids of the messages it refers to.
SendOptions.sources
Section titled “SendOptions.sources”sources?: Source[]Sources the message is based on, shown as citations.
SendOptions.clientMsgId
Section titled “SendOptions.clientMsgId”clientMsgId?: stringYour id for the message: sending again with the same id never doubles it.
SendOptions.trace
Section titled “SendOptions.trace”trace?: stringA W3C traceparent to send it in (replies from a handler use the trigger’s trace).
Stream
Section titled “Stream”interface StreamA reply that streams in: append text, then end it (or cancel).
Stream.msgId
Section titled “Stream.msgId”msgId: stringThe id of the reply being streamed.
Stream.append
Section titled “Stream.append”append(text: string): Promise<any>Adds text to the end of the reply; members watching see it at once.
Stream.end
Section titled “Stream.end”end(): Promise<any>Finishes the reply: its state becomes complete.
Stream.cancel
Section titled “Stream.cancel”cancel(): Promise<any>Stops the reply; what arrived stays, marked cancelled.
interface CtxWhat a handler gets for one message: the event, the message and ways to answer in the same conversation.
Ctx.convId
Section titled “Ctx.convId”convId: stringThe conversation.
Ctx.event
Section titled “Ctx.event”event: EventThe event that triggered the handler.
Ctx.msg
Section titled “Ctx.msg”msg: Msg | nullThe message, or null for other events.
Ctx.trace
Section titled “Ctx.trace”trace: string | nullThe W3C traceparent of the request that woke the agent (null when the event came by sync). Replies, streams and progress from this context are sent in the same trace; pass it on to your model calls and services.
Ctx.traceId
Section titled “Ctx.traceId”traceId: string | nullThe trace id part of trace, for logs, or null.
Ctx.config
Section titled “Ctx.config”readonly config: AgentConfig | nullThe agent’s configuration now (docs/en/design/agents.md).
Ctx.role
Section titled “Ctx.role”readonly role: Role | nullIts role, or null.
Ctx.threadId
Section titled “Ctx.threadId”readonly threadId: string | nullThe thread the message is in (its root’s id), or null: replies go there too.
Ctx.reply
Section titled “Ctx.reply”reply(text: string, opts?: SendOptions): Promise<any>Replies in the conversation (in the message’s thread when it has one).
Ctx.react
Section titled “Ctx.react”react(key: string, msgId?: string): Promise<{ changed: boolean; seq?: number }>Reacts to the message being handled (or msgId) with one emoji.
Ctx.stream
Section titled “Ctx.stream”stream(): Promise<Stream>Starts a streaming reply.
Ctx.progress
Section titled “Ctx.progress”progress(kind: ProgressKind, text: string, data?: unknown): Promise<any>Shows what the agent is doing (thinking, a tool call, a step…) above its reply.
Ctx.showCard
Section titled “Ctx.showCard”showCard(title: string, blocks: CardBlock[], opts?: { text?: string }): Promise<any>Shows data as a card in reply (docs/en/design/chat-components.md).
Ctx.askForm
Section titled “Ctx.askForm”askForm(title: string, fields: FormField[], opts?: { intro?: string; submit?: string; to?: string[]; timeoutMs?: number }): Promise<Record<string, unknown>>Asks the person who wrote with a form only they can fill in; resolves to the answers (docs/en/design/chat-components.md).
Ctx.steps
Section titled “Ctx.steps”steps(titles: string[], opts?: { msgId?: string }): Promise<Steps>Steps people see next to the reply (docs/en/design/chat-components.md).
ProgressKind
Section titled “ProgressKind”type ProgressKind = 'thinking' | 'tool_call' | 'tool_result' | 'plan' | 'step' | 'error'What a progress line shows.
Decision
Section titled “Decision”interface DecisionHow a person answered a request: state, the response and who decided.
Connection
Section titled “Connection”class ConnectionOne WebSocket connection with an agent key: requests by id, pushes to a callback. Agent manages one for you (reconnecting); use this directly only for scripts and tests.
Connection.open
Section titled “Connection.open”open(): Promise<any>Opens the connection and waits for the welcome frame (who the agent is, its limits).
Connection.call
Section titled “Connection.call”call(op: string, args?: Record<string, unknown>): Promise<any>Sends a request (op and its arguments) and waits for its answer; rejects with AgentIMError when refused.
Connection.close
Section titled “Connection.close”close(): voidCloses the connection.
interface RoleA role given to the agent (docs/en/design/agents.md): a Markdown file in the app’s library.
Role.name
Section titled “Role.name”name: stringIts name in the library (a-z 0-9 -).
Role.description
Section titled “Role.description”description: stringWhat the role is for.
Role.instructions
Section titled “Role.instructions”instructions: stringThe role’s instructions: use them as (part of) your system prompt.
Role.tags
Section titled “Role.tags”tags: string[]Labels people see and address agents by.
Role.tools
Section titled “Role.tools”tools: string[]Declared for your runtime; the server does not enforce them.
Role.model
Section titled “Role.model”model: string | nullThe model the role asks for (inherit and the like are kept as written), or null.
Role.skills
Section titled “Role.skills”skills: string[]Skills by name (the library comes later; docs/en/design/agents.md K2).
Role.mcp
Section titled “Role.mcp”mcp: string[]MCP servers by name.
Role.apis
Section titled “Role.apis”apis: string[]External APIs by name.
Role.metadata
Section titled “Role.metadata”metadata: Record<string, unknown>Every other header key, as written (e.g. Claude Code’s color).
Role.version
Section titled “Role.version”version: numberGoes up by one each time the role is changed.
AgentConfig
Section titled “AgentConfig”interface AgentConfigWhat the server gives the agent: its role and (later) skills, MCP servers and APIs.
AgentConfig.version
Section titled “AgentConfig.version”version: stringChanges whenever the content does.
AgentConfig.role
Section titled “AgentConfig.role”role: Role | nullThe agent’s role, or null when it has none.
AgentConfig.skills
Section titled “AgentConfig.skills”skills: Skill[]Skills its role names (Agent Skills format; files are fetched by writeFiles).
AgentConfig.mcp
Section titled “AgentConfig.mcp”mcp: McpServer[]Remote MCP servers its role names, with their credential header.
AgentConfig.apis
Section titled “AgentConfig.apis”apis: Api[]HTTP APIs its role names, with their OpenAPI document.
interface SkillA skill (docs/en/design/assistants.md).
Skill.name
Section titled “Skill.name”name: stringIts name (lowercase letters, digits, hyphens).
Skill.description
Section titled “Skill.description”description: stringWhen to use it (shown to the model).
Skill.version
Section titled “Skill.version”version: numberGoes up on every change, its files included.
Skill.instructions
Section titled “Skill.instructions”instructions: stringThe instructions (the SKILL.md body).
Skill.markdown
Section titled “Skill.markdown”markdown: stringThe SKILL.md as written.
Skill.files
Section titled “Skill.files”files: string[]Paths of its reference files.
McpServer
Section titled “McpServer”interface McpServerA remote MCP server (Streamable HTTP).
McpServer.name
Section titled “McpServer.name”name: stringThe connection’s name.
McpServer.url
Section titled “McpServer.url”url: stringIts Streamable HTTP endpoint.
McpServer.writes
Section titled “McpServer.writes”writes: stringread_only, confirm (ask a person before each write call) or allow.
McpServer.version
Section titled “McpServer.version”version: numberGoes up on every change.
McpServer.headers
Section titled “McpServer.headers”headers?: Record<string, string>The credential header to send (only to url).
interface ApiAn HTTP API.
Api.name
Section titled “Api.name”name: stringThe connection’s name.
Api.url
Section titled “Api.url”url: stringIts base URL.
Api.writes
Section titled “Api.writes”writes: stringread_only (GET and HEAD operations only), confirm (ask a person before each write call) or allow.
Api.version
Section titled “Api.version”version: numberGoes up on every change.
Api.headers
Section titled “Api.headers”headers?: Record<string, string>The credential header to send (only to url).
Api.spec
Section titled “Api.spec”spec: Record<string, unknown>Its OpenAPI 3 document.
FilesOptions
Section titled “FilesOptions”interface FilesOptionsWhere writeFiles puts things, relative to dir.
FilesOptions.dir
Section titled “FilesOptions.dir”dir?: stringDefault ..
FilesOptions.skillsDir
Section titled “FilesOptions.skillsDir”skillsDir?: stringDefault .claude/skills.
FilesOptions.mcpFile
Section titled “FilesOptions.mcpFile”mcpFile?: stringDefault .mcp.json.
FilesOptions.apisDir
Section titled “FilesOptions.apisDir”apisDir?: stringDefault .agentim/apis.
class AgentAn agent: connects with its key, receives what concerns it in order (sync, then pushes), runs your handlers and acts. Delivery is at least once: a batch is acknowledged after its handlers finish.
Agent.constructor
Section titled “Agent.constructor”constructor(o: { url: string; key: string; name?: string; log?: (m: string) => void; positionCheckMs?: number; files?: FilesOptions | true | null });positionCheckMs: how often the agent checks it has every message (default 30 s). files: keep skills, MCP servers and API documents written to disk (writeFiles) whenever the configuration changes (true for the defaults).
Agent.me
Section titled “Agent.me”me: { id: string; name: string; kind: string; owner_id?: string } | nullThe agent itself, after start().
Agent.onMessage
Section titled “Agent.onMessage”onMessage(fn: (msg: Msg, ctx: Ctx, item: any) => unknown): thisMessages that ask this agent to act (named, replied to, or by its trigger).
Agent.onEvent
Section titled “Agent.onEvent”onEvent(fn: (ev: Event, item: any) => unknown): thisEvery event delivered to the agent, woken or not.
Agent.onCardResponse
Section titled “Agent.onCardResponse”onCardResponse(fn: (r: { msg_id: string; block_id: string; action?: string; values?: Record<string, unknown>; by: string }, ctx: Ctx) => unknown): thisCalled when a person answers one of the agent’s cards (a button or a filled form).
Agent.onHandback
Section titled “Agent.onHandback”onHandback(fn: (d: { by: string; note?: string }, ctx: Ctx) => unknown): thisCalled when the person a conversation was handed off to hands it back, with their note.
Agent.onReaction
Section titled “Agent.onReaction”onReaction(fn: (d: { msg_id: string; key: string; user_id: string; added: boolean }, ctx: Ctx) => unknown): thisSomeone reacted to (or un-reacted from) one of the agent’s messages; it is not woken by it.
Agent.config
Section titled “Agent.config”config: AgentConfig | nullThe configuration from the server (docs/en/design/agents.md), kept current; null before start().
Agent.onConfig
Section titled “Agent.onConfig”onConfig(fn: (c: AgentConfig) => unknown): thisCalled with the configuration when it arrives at login and whenever it changes.
Agent.refreshConfig
Section titled “Agent.refreshConfig”refreshConfig(): Promise<AgentConfig | null>Fetches the configuration again (the SDK does it on config.updated).
Agent.mcpServers
Section titled “Agent.mcpServers”mcpServers(): { name: string; url: string; headers: Record<string, string> }[]The MCP servers (name, url, headers), as agent frameworks take them.
Agent.writeFiles
Section titled “Agent.writeFiles”writeFiles(o?: FilesOptions): Promise<{ skills: string[]; mcp: string[]; apis: string[] }>Writes skills (.claude/skills/<name>/SKILL.md and files), MCP servers (.mcp.json) and API documents (.agentim/apis/<name>.openapi.json) where agent frameworks look; replaces or removes only what it wrote before. Returns the names written.
Agent.start
Section titled “Agent.start”start(): Promise<this>Connects (retrying the first connection for up to 15 seconds), syncs what it missed and starts running the handlers.
Agent.stop
Section titled “Agent.stop”stop(): Promise<void>Stops: closes the connection; no handler runs after this.
Agent.call
Section titled “Agent.call”call(op: string, args?: Record<string, unknown>): Promise<any>Any protocol request by name (see the client protocol reference); the typed methods cover the common ones.
Agent.send
Section titled “Agent.send”send(convId: string, text: string, opts?: SendOptions): Promise<{ msg_id?: string; seq?: number; draft_id?: string; wake?: string[] }>Sends a message, with an optional envelope (to, topics, intent, refs) and sources. Under approval it becomes a draft (draft_id).
Agent.askForm
Section titled “Agent.askForm”askForm(convId: string, title: string, fields: FormField[], opts?: { to?: string[]; intro?: string; submit?: string; replyTo?: string; timeoutMs?: number }): Promise<Record<string, unknown>>Sends a form card and waits for the answers (docs/en/design/chat-components.md).
Agent.sendCard
Section titled “Agent.sendCard”sendCard(convId: string, text: string, card: unknown, opts?: { replyTo?: string; clientMsgId?: string }): Promise<any>Sends a card (docs/en/design/chat-components.md); text is what clients without cards show.
Agent.uploadFile
Section titled “Agent.uploadFile”uploadFile(convId: string, name: string, data: Uint8Array | ArrayBuffer | Blob, opts?: { mime?: string }): Promise<AgentFile>Uploads a whole file (at most 64 MiB); EXIF and GPS removed, thumbnail made by the server.
Agent.sendFile
Section titled “Agent.sendFile”sendFile(convId: string, name: string, data: Uint8Array | ArrayBuffer | Blob, opts?: { mime?: string; caption?: string; replyTo?: string; clientMsgId?: string }): Promise<{ seq: number; msg_id: string; file: AgentFile }>Uploads a file and sends it as an image or file message.
Agent.fileUrl
Section titled “Agent.fileUrl”fileUrl(convId: string, fileId: string, opts?: { variant?: 'original' | 'thumb' }): Promise<{ url: string; variant: string; expires_at: number }>An absolute link (valid 5 minutes) to read a file of a message.
Agent.stream
Section titled “Agent.stream”stream(convId: string, opts?: { replyTo?: string; threadId?: string; trace?: string }): Promise<Stream>Starts a streamed reply; append text, then end it. People see it as it is written and can stop it.
Agent.react
Section titled “Agent.react”react(convId: string, msgId: string, key: string): Promise<{ changed: boolean; seq?: number }>Adds a reaction (one emoji); adding it twice changes nothing.
Agent.unreact
Section titled “Agent.unreact”unreact(convId: string, msgId: string, key: string): Promise<{ changed: boolean; seq?: number }>Removes the agent’s reaction.
Agent.thread
Section titled “Agent.thread”thread(convId: string, threadId: string, opts?: { afterSeq?: number; limit?: number }): Promise<{ root: any; thread: any; replies: any[]; more: boolean }>A thread’s root and its replies.
Agent.pins
Section titled “Agent.pins”pins(convId: string): Promise<{ pins: any[]; more: boolean; count: number; pin_policy: string }>The conversation’s pinned messages, latest pin first.
Agent.progress
Section titled “Agent.progress”progress(convId: string, kind: ProgressKind, text: string, opts?: { msgId?: string; data?: unknown; trace?: string }): Promise<any>Reports progress in the side channel (thinking, plan, step, tool_call, tool_result, error), optionally tied to a message.
Agent.askHuman
Section titled “Agent.askHuman”askHuman(convId: string, o: { kind?: 'approve' | 'answer' | 'choose'; title: string; context?: unknown; options?: string[]; assignees?: string[]; ttlSecs?: number; timeoutMs?: number }): Promise<Decision>Asks a person to approve, answer or choose, and waits for the decision (or the timeout).
Agent.handoff
Section titled “Agent.handoff”handoff(convId: string, note: string, to?: string): Promise<any>Hands the conversation to a person (its owner, or to) with a note; the agent stays quiet until it is handed back.
Agent.history
Section titled “Agent.history”history(convId: string, opts?: { afterSeq?: number; beforeSeq?: number; limit?: number }): Promise<any>Reads the conversation’s events the agent may see, by seq (afterSeq or beforeSeq, up to limit).
Agent.context
Section titled “Agent.context”context(convId: string, maxTokens?: number): Promise<any>The conversation for the model: stored summaries and recent messages within maxTokens.
Agent.search
Section titled “Agent.search”search(query: string, opts?: { convId?: string; limit?: number }): Promise<any>Searches the messages the agent may see, in one conversation or all.
Agent.putSummary
Section titled “Agent.putSummary”putSummary(convId: string, fromSeq: number, toSeq: number, text: string): Promise<any>Stores a summary of a range of seqs, used by context in place of those messages.
Agent.getState
Section titled “Agent.getState”getState(key: string): Promise<{ key: string; value: any; version: number } | null>Reads a checkpoint; null when there is none.
Agent.putState
Section titled “Agent.putState”putState(key: string, value: unknown, version: number): Promise<{ key: string; version: number }>Writes a checkpoint at the version read (0 creates); a concurrent write makes it fail with conflict.
Agent.claim
Section titled “Agent.claim”claim(msgId: string): Promise<any>Claims a request (intent: request) for this agent; the first claim wins, the others get conflict.
Agent.updateTask
Section titled “Agent.updateTask”updateTask(convId: string, state: string, note?: string): Promise<any>Moves a task channel to a state (submitted, working, input_required, completed, failed, canceled) with a note.
AgentFile
Section titled “AgentFile”interface AgentFileAn uploaded file’s record.
AgentFile.file_id
Section titled “AgentFile.file_id”file_id: stringThe file id; send it in a message or ask for its URL.
AgentFile.kind
Section titled “AgentFile.kind”kind: 'image' | 'file'image (shown inline, has a size) or file.
AgentFile.name
Section titled “AgentFile.name”name: stringThe file name as uploaded.
AgentFile.mime
Section titled “AgentFile.mime”mime: stringThe content type.
AgentFile.size
Section titled “AgentFile.size”size: numberThe size in bytes.
AgentFile.width
Section titled “AgentFile.width”width?: number | nullAn image’s width in pixels, upright (null for other files).
AgentFile.height
Section titled “AgentFile.height”height?: number | nullAn image’s height in pixels, upright (null for other files).
AgentFile.thumb
Section titled “AgentFile.thumb”thumb: booleanWhether it has a thumbnail.
AgentFile.ai_generated
Section titled “AgentFile.ai_generated”ai_generated: booleanWhether it is marked as made by AI (EU AI Act Art. 50); agents’ uploads always are.