Skip to content

About

Dialog for AI agents: a program with its own Dialog account talks to people end-to-end encrypted and asks structured questions (approve / deny, choices, text). Rust, Python, Node.js.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

Repository files navigation

Dialog for agents

For programs (AI agents, internal platforms) that talk to people on Dialog as an account of their own, end-to-end encrypted, and ask them structured questions: approve / deny, one of a list, several of a list, free text. The person gets a notification, sees the question as a card with buttons in the chat, taps an answer, and the program receives which request was answered, with which options and text, or that it expired or was cancelled.

This is not the Bot API (github.com/DialogPM/dialog-python, dialog-js...): a bot hands the server plain text. An agent is an ordinary account; the server sees only ciphertext.

Folder What
crates/dialog-agent the Rust library and the dialog-agent program
python/ dialog_agent for Python (standard library only)
js/ dialog-agent for Node.js (JavaScript and TypeScript, no dependencies)

The Python and Node.js libraries start the dialog-agent program and talk to it over its standard input and output; the encryption runs in that program. Build it with cargo build --release -p dialog-agent and put it on PATH, or point DIALOG_AGENT_BIN at it.

An account for the agent

Every agent has its own account: a 16-digit number and a password.

export DIALOG_AGENT_SERVER=https://chat.dialog.pm
export DIALOG_AGENT_DATA=./agent-data        # keys and messages, locked with the password
export DIALOG_AGENT_PASSWORD='…'             # never on the command line
dialog-agent register --name "Deploy agent"

It prints the account number and the 24-word recovery phrase (once: keep it). An account created in the app works too: set DIALOG_AGENT_NUMBER to its number and the first run signs in to it as a new device. People reach the agent like any contact: by its number or by the @username it sets.

Asking

from dialog_agent import Agent

with Agent() as agent:                     # settings from DIALOG_AGENT_*
    for event in agent.events():
        if event["event"] == "message" and event["text"] == "deploy":
            outcome = agent.ask_and_wait(event["conversation_id"], "Deploy to production?", expires_in=600)
            agent.send(event["conversation_id"], "Deploying." if outcome.approved else "Not deploying.")
import { Agent } from "dialog-agent";

const agent = await Agent.start();
const outcome = await agent.askAndWait("@ana", {
  text: "Which servers?", kind: "multi", allowText: true,
  options: [{ id: "eu", label: "Europe" }, { id: "us", label: "America" }],
});
console.log(outcome.state, outcome.options, outcome.text);
await agent.close();
Kind The person sees The outcome's options
confirm (default) Approve / Deny (or your two labels, ids approve and deny) ["approve"] or ["deny"]
single a list, one choice one id
multi a list with checkboxes any number of ids
text a text field none; the answer is in text

allow_text adds a text field next to the options. expires_in (seconds, at most 30 days) closes the request by itself. An answer is final. state is open, answered, cancelled or expired.

Limits: 10 options, 64 characters per label, 2000 for the question, 1000 for a text answer, 20 open requests per conversation. Requests go to direct conversations only. A first message to someone who never wrote to the agent arrives as a message request, like from any stranger.

An app older than this feature shows the request as a text message (the question and the numbered options) and cannot press anything; what its user types arrives as an ordinary message event.

Controlling the agent from the chat

Who may command it. DIALOG_AGENT_ALLOWED (comma separated account numbers, @usernames or account ids; Python Agent(allowed=[...]), Node { allowed: [...] }, Rust Options.allowed). Resolved to account ids when the agent signs in (it refuses to start if one cannot be found); the ready line lists them. Messages and stops from anyone else never reach the program and are not marked read. Empty: anyone.

What it is doing. working(to, status) shows a status ("Running npm test", at most 120 characters) above the person's composer, with a Stop button; idle(to) removes it. The app lets it lapse after about 8 seconds, so send it again about every 5 seconds while working. Older apps show ordinary "typing…".

Stop. The person's Stop button arrives as {"event": "stop", "conversation_id": "…", "from": "…", "session_id": "…" | null} (Node: agent.on("stop", …)): end what you are doing for them, in that session.

Always approve. ask(..., always=True) (Node { always: true }, Rust Request::confirm(..).with_always()) adds a third button to an Approve / Deny request. The outcome's option is approve, always or deny; approved is true for both of the first two, always tells them apart. Older apps see [Approve / Always approve / Deny] as text.

Files. send_file(to, path, caption?, session_id?) (Node sendFile) sends any file; pictures and videos show as such. A message event lists attachments (id, file_name, mime, size, kind); download(message_id, attachment_id) saves a decrypted copy under the agent's data directory (downloads/) and returns its path.

"/" commands. set_commands(to, [{command, description}]) (Node setCommands) fills the command menu of the person's composer (at most 50; 1-32 characters a-z, 0-9, _; an empty list removes them). Choosing one sends it as text, e.g. /stop.

Sessions

A person may turn on sessions in their conversation with the agent: one chat in their list, with separate threads inside it that they start, name and switch between. Each message event then names its session:

Field
session_id the session the message belongs to; null when the person does not use sessions
session_title its title, null while it has none
session_new true for the first message of that session the agent sees: start from scratch

Replies need nothing extra: what the agent sends goes to the session of the person's latest message. To answer in another one, name it: send(to, text, session_id=…) (Python), send(to, text, sessionId) (Node.js), Agent::send_in (Rust); questions take the same (session_id= / sessionId / Request::in_session). Only sessions the person used in that conversation can be named.

The agent may propose a title, set_session_title(to, session_id, title) (1 to 80 characters). The person's app applies it unless they typed a title themselves; they can rename the session at any time. Only the person starts, ends and names sessions.

for event in agent.events():
    if event["event"] == "message":
        if event["session_new"]:
            history = []                                  # a new session: a fresh context
        reply = think(history, event["text"])
        agent.send(event["conversation_id"], reply)       # into the same session
        if event["session_id"] and not event["session_title"]:
            agent.set_session_title(event["conversation_id"], event["session_id"], summarize(event["text"]))

An app without sessions, or with them turned off, shows one ordinary conversation.

The protocol of dialog-agent run

One JSON object per line.

→ {"id": 1, "method": "ask", "params": {"to": "@ana", "text": "Deploy?", "expires_in": 600}}
← {"id": 1, "result": {"request_id": "…"}}
→ {"id": 2, "method": "wait", "params": {"request_id": "…", "timeout": 30}}
← {"id": 2, "result": {"request_id": "…", "conversation_id": "…", "state": "answered", "options": ["approve"], "text": null, "answered_at": 1764590000000}}
← {"event": "message", "message_id": "…", "conversation_id": "…", "from": "…", "from_name": "…", "text": "hi", "kind": "text", "sent_at": 1764590000000, "session_id": null, "session_title": null, "session_new": false, "attachments": [{"id": "…", "file_name": "notes.txt", "mime": "text/plain", "size": 14, "kind": "file"}]}
← {"event": "stop", "conversation_id": "…", "from": "…", "session_id": null}
← {"event": "answer", "request_id": "…", "state": "answered", …}
← {"id": 3, "error": {"code": "invalid_input", "message": "…"}}

Methods: me, send {to, text, session_id?}, ask {to, text, kind?, options?, allow_text?, expires_in?, session_id?, always?}, wait {request_id, timeout?}, outcome {request_id}, cancel {request_id}, working {to, status}, idle {to}, send_file {to, path, caption?, session_id?} → {message_id}, download {message_id, attachment_id} → {path}, set_commands {to, commands: [{command, description}]}, set_session_title {to, session_id, title}, rpc {command, params} (any engine command), close. The first line is always {"event": "ready", "account": {…}, "allowed": [account ids]}. to is a conversation id, an account id, an account number or an @username.

This repository

crates/chat-core is Dialog's client engine (the same one the apps use: Signal Protocol, local storage), from github.com/DialogPM/desktop. Tests: cargo test -p dialog-agent --lib, (cd python && python3 -m unittest discover -s tests), (cd js && npm test), (cd claude-code && npm install && npm test). The end-to-end tests need a Dialog server and run from the main repository.

License

AGPL-3.0-only (see LICENSE), like the Dialog apps whose engine this includes.

About

Dialog for AI agents: a program with its own Dialog account talks to people end-to-end encrypted and asks structured questions (approve / deny, choices, text). Rust, Python, Node.js.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages