agent-ticketing 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -59,10 +59,18 @@ NOT keep the Durable Object hot, so pick the mode by latency preference, not cos
59
59
  the thinking, the ticket is completed with its output.
60
60
 
61
61
  ```bash
62
+ # persistent service (recommended): starts now, restarts on crash, survives reboots
62
63
  MAILBOX_URL=https://… MAILBOX_API_KEY=mbx_live_… \
63
- npx -y -p agent-ticketing mailbox-claude-runner # add --interval 5m for lazy polling
64
+ npx -y -p agent-ticketing mailbox-claude-runner --install-service
65
+
66
+ # one-off foreground run: drop --install-service (add --interval 5m for lazy polling)
67
+ # remove the service later: mailbox-claude-runner --uninstall-service
64
68
  ```
65
69
 
70
+ `--install-service` writes a launchd LaunchAgent (macOS) or a systemd user unit
71
+ (Linux) with your env baked in. Linux + reboot-without-login also needs
72
+ `loginctl enable-linger $USER` once.
73
+
66
74
  Runners are agent-specific — `mailbox-claude-runner` launches headless **Claude Code**
67
75
  sessions. Runners for Codex and daemon-style agents (Hermes, OpenClaw) are planned;
68
76
  for a custom harness, embed the SDK instead (below).
package/dist/index.d.ts CHANGED
@@ -143,6 +143,12 @@ export declare class MailboxAgent {
143
143
  }): Promise<Message>;
144
144
  complete(ticketId: string, summary?: string): Promise<Ticket>;
145
145
  release(ticketId: string, reason?: string): Promise<Ticket>;
146
+ /** Download an attachment's bytes (any attachment in your mailbox's tickets). */
147
+ downloadAttachment(attachmentId: string): Promise<{
148
+ filename: string;
149
+ contentType: string;
150
+ data: Uint8Array;
151
+ }>;
146
152
  uploadAttachment(ticketId: string, file: {
147
153
  filename: string;
148
154
  contentType?: string;
package/dist/index.js CHANGED
@@ -87,6 +87,21 @@ export class MailboxAgent {
87
87
  });
88
88
  return (await res.json()).ticket;
89
89
  }
90
+ /** Download an attachment's bytes (any attachment in your mailbox's tickets). */
91
+ async downloadAttachment(attachmentId) {
92
+ const res = await fetch(`${this.baseUrl}/v1/attachments/${attachmentId}`, {
93
+ headers: { Authorization: `Bearer ${this.#apiKey}` },
94
+ });
95
+ if (!res.ok)
96
+ throw await toApiError(res);
97
+ const cd = res.headers.get("Content-Disposition") ?? "";
98
+ const filename = /filename="([^"]+)"/.exec(cd)?.[1] ?? attachmentId;
99
+ return {
100
+ filename,
101
+ contentType: res.headers.get("Content-Type") ?? "application/octet-stream",
102
+ data: new Uint8Array(await res.arrayBuffer()),
103
+ };
104
+ }
90
105
  async uploadAttachment(ticketId, file) {
91
106
  const params = new URLSearchParams({
92
107
  filename: file.filename,
package/dist/runner.js CHANGED
@@ -7,18 +7,141 @@
7
7
  * MAILBOX_URL=https://…workers.dev MAILBOX_API_KEY=mbx_live_… \
8
8
  * npx @mailbox/agent # or: mailbox-claude-runner [--interval 5m] [--once]
9
9
  */
10
- import { spawn } from "node:child_process";
11
- import { appendFileSync } from "node:fs";
10
+ import { execSync, spawn } from "node:child_process";
11
+ import { appendFileSync, mkdirSync, rmSync, writeFileSync } from "node:fs";
12
+ import { homedir, tmpdir } from "node:os";
13
+ import { basename, join } from "node:path";
12
14
  import { MailboxAgent } from "./index.js";
13
15
  const baseUrl = process.env.MAILBOX_URL;
14
16
  const apiKey = process.env.MAILBOX_API_KEY;
15
- if (!baseUrl || !apiKey) {
17
+ if ((!baseUrl || !apiKey) && !process.argv.includes("--uninstall-service")) {
16
18
  console.error("Set MAILBOX_URL and MAILBOX_API_KEY (Settings → Agents in the mailbox UI)");
17
19
  process.exit(1);
18
20
  }
19
21
  const args = process.argv.slice(2);
20
22
  const interval = args.includes("--interval") ? args[args.indexOf("--interval") + 1] : null;
21
23
  const once = args.includes("--once");
24
+ // ── --install-service / --uninstall-service: survive host reboots ────────────
25
+ // macOS → launchd LaunchAgent; Linux → systemd user unit. Env is baked in.
26
+ const SERVICE_LABEL = "com.agent-ticketing.runner";
27
+ const UNIT_NAME = "agent-ticketing-runner";
28
+ function serviceCommand() {
29
+ let npx = "npx";
30
+ try {
31
+ npx = execSync("command -v npx", { shell: "/bin/sh", encoding: "utf8" }).trim() || "npx";
32
+ }
33
+ catch {
34
+ // fall back to PATH lookup (we bake PATH into the service env anyway)
35
+ }
36
+ return `exec "${npx}" -y -p agent-ticketing mailbox-claude-runner${interval ? ` --interval ${interval}` : ""}`;
37
+ }
38
+ function xmlEscape(v) {
39
+ return v.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;");
40
+ }
41
+ function installService() {
42
+ const home = homedir();
43
+ const logDir = join(home, ".agent-ticketing");
44
+ mkdirSync(logDir, { recursive: true });
45
+ const envs = {
46
+ MAILBOX_URL: baseUrl ?? "",
47
+ MAILBOX_API_KEY: apiKey ?? "",
48
+ MAILBOX_LOG_FILE: process.env.MAILBOX_LOG_FILE ?? join(logDir, "events.ndjson"),
49
+ // Service shells get a bare PATH (no profile) — bake the installing shell's
50
+ // PATH so node/npx/claude resolve exactly as they did for the human.
51
+ PATH: process.env.PATH ?? "/usr/local/bin:/usr/bin:/bin",
52
+ };
53
+ if (process.env.CLAUDE_CMD)
54
+ envs.CLAUDE_CMD = process.env.CLAUDE_CMD;
55
+ if (process.platform === "darwin") {
56
+ const plist = join(home, "Library", "LaunchAgents", `${SERVICE_LABEL}.plist`);
57
+ mkdirSync(join(home, "Library", "LaunchAgents"), { recursive: true });
58
+ const envXml = Object.entries(envs)
59
+ .map(([k, v]) => ` <key>${k}</key><string>${xmlEscape(v)}</string>`)
60
+ .join("\n");
61
+ writeFileSync(plist, `<?xml version="1.0" encoding="UTF-8"?>
62
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
63
+ <plist version="1.0">
64
+ <dict>
65
+ <key>Label</key><string>${SERVICE_LABEL}</string>
66
+ <key>ProgramArguments</key>
67
+ <array><string>/bin/sh</string><string>-lc</string><string>${xmlEscape(serviceCommand())}</string></array>
68
+ <key>EnvironmentVariables</key>
69
+ <dict>
70
+ ${envXml}
71
+ </dict>
72
+ <key>RunAtLoad</key><true/>
73
+ <key>KeepAlive</key><true/>
74
+ <key>StandardOutPath</key><string>${join(logDir, "runner.log")}</string>
75
+ <key>StandardErrorPath</key><string>${join(logDir, "runner.log")}</string>
76
+ </dict>
77
+ </plist>
78
+ `);
79
+ execSync(`launchctl unload "${plist}" 2>/dev/null || true`, { shell: "/bin/sh" });
80
+ execSync(`launchctl load -w "${plist}"`, { shell: "/bin/sh" });
81
+ console.log(`installed launchd service ${SERVICE_LABEL}`);
82
+ console.log(` plist: ${plist}`);
83
+ console.log(` logs: ${join(logDir, "runner.log")}`);
84
+ console.log(`It starts now, restarts on crash, and survives reboots (starts at login).`);
85
+ }
86
+ else if (process.platform === "linux") {
87
+ const unitDir = join(home, ".config", "systemd", "user");
88
+ mkdirSync(unitDir, { recursive: true });
89
+ const unit = join(unitDir, `${UNIT_NAME}.service`);
90
+ const envLines = Object.entries(envs)
91
+ .map(([k, v]) => `Environment="${k}=${v}"`)
92
+ .join("\n");
93
+ writeFileSync(unit, `[Unit]
94
+ Description=Agent Ticketing runner (mailbox-claude-runner)
95
+ After=network-online.target
96
+
97
+ [Service]
98
+ ${envLines}
99
+ ExecStart=/bin/sh -lc '${serviceCommand()}'
100
+ Restart=always
101
+ RestartSec=10
102
+
103
+ [Install]
104
+ WantedBy=default.target
105
+ `);
106
+ execSync(`systemctl --user daemon-reload && systemctl --user enable --now ${UNIT_NAME}`, { shell: "/bin/sh" });
107
+ console.log(`installed systemd user service ${UNIT_NAME}`);
108
+ console.log(` unit: ${unit}`);
109
+ console.log(` logs: journalctl --user -u ${UNIT_NAME} -f (plus ${envs.MAILBOX_LOG_FILE})`);
110
+ console.log(`To also start WITHOUT anyone logging in after reboot, run once:`);
111
+ console.log(` loginctl enable-linger $USER`);
112
+ }
113
+ else {
114
+ console.error(`--install-service supports macOS (launchd) and Linux (systemd) — on ${process.platform}, run the runner under your own supervisor.`);
115
+ process.exit(1);
116
+ }
117
+ }
118
+ function uninstallService() {
119
+ const home = homedir();
120
+ if (process.platform === "darwin") {
121
+ const plist = join(home, "Library", "LaunchAgents", `${SERVICE_LABEL}.plist`);
122
+ execSync(`launchctl unload -w "${plist}" 2>/dev/null || true`, { shell: "/bin/sh" });
123
+ rmSync(plist, { force: true });
124
+ console.log(`removed launchd service ${SERVICE_LABEL}`);
125
+ }
126
+ else if (process.platform === "linux") {
127
+ execSync(`systemctl --user disable --now ${UNIT_NAME} 2>/dev/null || true`, { shell: "/bin/sh" });
128
+ rmSync(join(home, ".config", "systemd", "user", `${UNIT_NAME}.service`), { force: true });
129
+ execSync(`systemctl --user daemon-reload 2>/dev/null || true`, { shell: "/bin/sh" });
130
+ console.log(`removed systemd user service ${UNIT_NAME}`);
131
+ }
132
+ else {
133
+ console.error(`--uninstall-service supports macOS and Linux only.`);
134
+ process.exit(1);
135
+ }
136
+ }
137
+ if (args.includes("--uninstall-service")) {
138
+ uninstallService();
139
+ process.exit(0);
140
+ }
141
+ if (args.includes("--install-service")) {
142
+ installService();
143
+ process.exit(0);
144
+ }
22
145
  const claudeCmd = process.env.CLAUDE_CMD ?? "claude";
23
146
  // MAILBOX_LOG_FILE=agent.ndjson → one JSON event per line (ts, event, ticketId…)
24
147
  // for grep-able history that cross-references the server-side ticket threads.
@@ -33,12 +156,21 @@ const onEvent = logFile
33
156
  }
34
157
  }
35
158
  : undefined;
36
- const mailbox = new MailboxAgent({ baseUrl, apiKey });
37
- function promptFor(ticket, messages) {
159
+ // Non-null: every path reaching here passed the env guard (service handlers exit earlier).
160
+ const mailbox = new MailboxAgent({ baseUrl: baseUrl, apiKey: apiKey });
161
+ function promptFor(ticket, messages, attachmentPaths) {
38
162
  const thread = messages
39
163
  .filter((m) => m.kind === "message")
40
164
  .map((m) => `${m.author.type === "human" ? "REQUESTER" : m.author.type.toUpperCase()}: ${m.body}`)
41
165
  .join("\n\n");
166
+ const files = attachmentPaths.length > 0
167
+ ? [
168
+ ``,
169
+ `ATTACHMENTS — the requester attached these files; they are downloaded locally.`,
170
+ `Read them with your file tools (images can be viewed directly):`,
171
+ ...attachmentPaths.map((p) => `- ${p}`),
172
+ ]
173
+ : [];
42
174
  return [
43
175
  `You are working a ticket from a ticketing system. Do the task and reply with your final result.`,
44
176
  `Your reply is posted back to the requester as the ticket resolution — be complete but concise.`,
@@ -46,8 +178,30 @@ function promptFor(ticket, messages) {
46
178
  `TICKET: ${ticket.subject}`,
47
179
  ``,
48
180
  thread,
181
+ ...files,
49
182
  ].join("\n");
50
183
  }
184
+ /** Download the ticket's attachments so the headless session can open them. */
185
+ async function fetchAttachments(ticket, messages) {
186
+ const attachments = messages.flatMap((m) => m.attachments);
187
+ if (attachments.length === 0)
188
+ return [];
189
+ const dir = join(tmpdir(), "agent-ticketing", ticket.id);
190
+ mkdirSync(dir, { recursive: true });
191
+ const paths = [];
192
+ for (const a of attachments) {
193
+ try {
194
+ const file = await mailbox.downloadAttachment(a.id);
195
+ const path = join(dir, basename(a.filename) || a.id);
196
+ writeFileSync(path, file.data);
197
+ paths.push(path);
198
+ }
199
+ catch (e) {
200
+ console.error(`[mailbox] could not download attachment ${a.id} (${a.filename}): ${String(e)}`);
201
+ }
202
+ }
203
+ return paths;
204
+ }
51
205
  /**
52
206
  * Run `claude -p` with the prompt on STDIN (immune to ARG_MAX for long threads,
53
207
  * keeps ticket text out of `ps`). On failure, the error carries the exit code
@@ -90,8 +244,11 @@ function runClaude(prompt, signal) {
90
244
  }
91
245
  const controller = new AbortController();
92
246
  await mailbox.poll(async (ticket, ctx) => {
93
- await ctx.progress("launching headless Claude Code session");
94
- const stdout = await runClaude(promptFor(ticket, ctx.messages), ctx.signal);
247
+ const attachmentPaths = await fetchAttachments(ticket, ctx.messages);
248
+ await ctx.progress(attachmentPaths.length > 0
249
+ ? `launching headless Claude Code session (${attachmentPaths.length} attachment(s) downloaded)`
250
+ : "launching headless Claude Code session");
251
+ const stdout = await runClaude(promptFor(ticket, ctx.messages, attachmentPaths), ctx.signal);
95
252
  const result = stdout.trim();
96
253
  if (!result)
97
254
  throw new Error("claude produced no output");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent-ticketing",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Agent SDK for Agent Mailbox — poll tickets, heartbeat, ask the requester, complete. Zero dependencies.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",