@agentrq/agentrq-ws 0.8.2 → 0.9.1

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
@@ -20,7 +20,7 @@ npx -y @agentrq/agentrq-ws@latest workspace
20
20
 
21
21
  An agent talks to its workspace over MCP, which is the right shape for an agent
22
22
  and the wrong shape for a person: every call is a JSON envelope, and every
23
- answer is tokens the agent pays for. The same fifteen tools are underneath this
23
+ answer is tokens the agent pays for. The same seventeen tools are underneath this
24
24
  CLI, but a command is a line of shell and the answer is plain text.
25
25
 
26
26
  It is also the difference between reading an attachment and *handling* one — see
@@ -58,6 +58,8 @@ Node 20.6 or newer.
58
58
  | `skill delete <uri>` | Delete a skill (`skill://<name>`) or one of its files |
59
59
  | `event publish <name>` | Publish a named event |
60
60
  | `ask <taskId> <message>` | Ask the human a question and wait |
61
+ | `site-tools` | List the websites shared with this workspace from the Chrome extension, and their tools |
62
+ | `call-site-tool <site> <tool> --task <taskId>` | Run a shared website's tool in the human's Chrome, with `--args '{…}'` |
61
63
  | `tools` | List the tools this workspace server offers |
62
64
  | `call <tool> --args '{…}'` | Call any tool directly |
63
65
 
@@ -99,9 +101,10 @@ agentrq-ws attachment get 0isp9dJxr85 --task 0isnjTCkpW5 --out ~/Downloads
99
101
  agentrq-ws attachment get 0isp9dJxr85 --task 0isnjTCkpW5 --out ./report.pdf
100
102
  ```
101
103
 
102
- The `--task` is needed because the download tool returns bytes and no filename —
103
- the task is where the name lives, so the CLI reads it first and your file
104
- arrives called what a human called it.
104
+ The `--task` is needed because an attachment belongs to a task. The CLI asks
105
+ for the attachment's public link, downloads it, and saves the file under the
106
+ name a human gave it; an older attachment with no link arrives the same way,
107
+ from its content.
105
108
 
106
109
  ## Long text
107
110
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agentrq/agentrq-ws",
3
- "version": "0.8.2",
3
+ "version": "0.9.1",
4
4
  "description": "AgentRQ workspace client \u2014 drive an AgentRQ workspace from the command line, using the workspace's own .mcp.json",
5
5
  "keywords": [
6
6
  "agentrq",
@@ -81,33 +81,6 @@ export function readAttachment(filePath, { readFile = readFileSync, stat = statS
81
81
  }
82
82
  }
83
83
 
84
- /**
85
- * Index the attachments mentioned in a task's text, id → {filename, mimeType}.
86
- *
87
- * `downloadAttachment` answers with base64 and nothing else — no name, no type
88
- * — so the only way to save a file under the name a human gave it is to read
89
- * the task first. Task-level attachments arrive as `- id=… name=… type=…`
90
- * lines and message-level ones inside the conversation JSON, so both shapes are
91
- * scanned.
92
- */
93
- export function indexAttachments(taskText) {
94
- const index = new Map()
95
- const text = String(taskText || '')
96
-
97
- const linePattern = /^\s*-\s*id=(\S+)\s+name=(.*?)\s+type=(\S+)\s*$/gm
98
- for (const match of text.matchAll(linePattern)) {
99
- index.set(match[1], { filename: match[2], mimeType: match[3] })
100
- }
101
-
102
- const jsonPattern = /"id"\s*:\s*"([^"]+)"\s*,\s*"filename"\s*:\s*"([^"]*)"\s*,\s*"mimeType"\s*:\s*"([^"]*)"/g
103
- for (const match of text.matchAll(jsonPattern)) {
104
- if (!index.has(match[1])) {
105
- index.set(match[1], { filename: match[2], mimeType: match[3] })
106
- }
107
- }
108
- return index
109
- }
110
-
111
84
  /**
112
85
  * Work out where a downloaded attachment should land.
113
86
  *
@@ -139,9 +112,10 @@ function isDirectory(path, stat) {
139
112
  }
140
113
  }
141
114
 
142
- /** Write base64 content to `path`, creating the directory if it is missing. */
143
- export function writeAttachment(path, base64, { write = writeFileSync, mkdir = mkdirSync } = {}) {
144
- const buffer = Buffer.from(String(base64 || '').trim(), 'base64')
115
+ /** Write content — bytes, or base64 text — to `path`, creating the directory if it is missing. */
116
+ export function writeAttachment(path, content, { write = writeFileSync, mkdir = mkdirSync } = {}) {
117
+ // Bytes as they are, or base64 text to decode.
118
+ const buffer = Buffer.isBuffer(content) ? content : Buffer.from(String(content || '').trim(), 'base64')
145
119
  try {
146
120
  mkdir(dirname(path), { recursive: true })
147
121
  write(path, buffer)
package/src/commands.js CHANGED
@@ -3,7 +3,7 @@
3
3
 
4
4
  import { readFileSync } from 'node:fs'
5
5
 
6
- import { indexAttachments, readAttachment, resolveOutputPath, writeAttachment } from './attachments.js'
6
+ import { readAttachment, resolveOutputPath, writeAttachment } from './attachments.js'
7
7
  import { UserError } from './errors.js'
8
8
 
9
9
  /** Statuses the workspace accepts, mirrored from isValidTaskStatus on the backend. */
@@ -93,26 +93,36 @@ function requirePositional(positionals, index, name) {
93
93
  }
94
94
 
95
95
  /**
96
- * `downloadAttachment` returns base64 and nothing else, so the filename has to
97
- * come from the task. A task whose text does not mention the id still
98
- * downloads — under the id as a name — because refusing would be worse than a
99
- * plainly-named file.
96
+ * `getAttachment` answers with the attachment's name and type, and its public
97
+ * link — the default — or, for one saved before attachments had links, its
98
+ * content as base64. A link is fetched, so the file arrives either way.
100
99
  */
101
100
  async function downloadAttachment(ctx, attachmentId, taskId, out) {
102
- const { text: taskText } = await ctx.client.callTool('getTask', {
103
- taskId,
104
- includeConversation: true,
105
- limit: 200,
106
- })
107
- const known = indexAttachments(taskText).get(attachmentId)
108
- const filename = (known && known.filename) || attachmentId
109
-
110
- const { text: base64 } = await ctx.client.callTool('downloadAttachment', { attachmentId, taskId })
111
- if (!base64.trim()) {
101
+ const { text } = await ctx.client.callTool('getAttachment', { attachmentId, taskId })
102
+ let attachment
103
+ try {
104
+ attachment = JSON.parse(text)
105
+ } catch {
106
+ throw new UserError(`unexpected answer for attachment ${attachmentId}: ${text}`)
107
+ }
108
+ const content = attachment.url ? await fetchAttachment(ctx, attachment.url) : attachment.data
109
+ if (!content || !content.length) {
112
110
  throw new UserError(`attachment ${attachmentId} is empty or was not found on task ${taskId}`)
113
111
  }
114
- const path = resolveOutputPath(filename, out, { cwd: ctx.cwd })
115
- return writeAttachment(path, base64)
112
+ const path = resolveOutputPath(attachment.filename || attachmentId, out, { cwd: ctx.cwd })
113
+ return writeAttachment(path, content)
114
+ }
115
+
116
+ async function fetchAttachment(ctx, url) {
117
+ const fetchImpl = ctx.fetch || globalThis.fetch
118
+ let response
119
+ try {
120
+ response = await fetchImpl(url)
121
+ } catch (err) {
122
+ throw new UserError(`cannot download ${url}: ${err.message}`)
123
+ }
124
+ if (!response.ok) throw new UserError(`cannot download ${url}: HTTP ${response.status}`)
125
+ return Buffer.from(await response.arrayBuffer())
116
126
  }
117
127
 
118
128
  export const COMMANDS = [
@@ -375,6 +385,34 @@ export const COMMANDS = [
375
385
  })
376
386
  },
377
387
  },
388
+ {
389
+ path: ['site-tools'],
390
+ summary: 'List the websites shared with this workspace and their tools',
391
+ usage: 'agentrq-ws site-tools',
392
+ async run(ctx) {
393
+ return ctx.client.callTool('listSiteTools', {})
394
+ },
395
+ },
396
+ {
397
+ path: ['call-site-tool'],
398
+ summary: "Run a shared website's tool in the human's Chrome",
399
+ usage: "agentrq-ws call-site-tool <site> <tool> --task <taskId> [--args '{\"k\":\"v\"}'|@file|-]",
400
+ options: {
401
+ task: { type: 'string', short: 't', description: 'The task you are working on; approvals are asked there' },
402
+ args: { type: 'string', short: 'a', description: 'Tool arguments as JSON (@file or - for stdin)' },
403
+ },
404
+ async run(ctx) {
405
+ const site = requirePositional(ctx.positionals, 0, 'site')
406
+ const tool = requirePositional(ctx.positionals, 1, 'tool')
407
+ if (!ctx.values.task) throw new UserError('--task <taskId> is required')
408
+ return ctx.client.callTool('callSiteTool', {
409
+ taskId: ctx.values.task,
410
+ site,
411
+ tool,
412
+ arguments: await readArgs(ctx),
413
+ })
414
+ },
415
+ },
378
416
  {
379
417
  path: ['tools'],
380
418
  summary: 'List the tools this workspace server offers',
@@ -398,20 +436,22 @@ export const COMMANDS = [
398
436
  // An escape hatch, so a tool added to the server tomorrow is reachable
399
437
  // today without waiting for this CLI to grow a verb for it.
400
438
  const name = requirePositional(ctx.positionals, 0, 'tool')
401
- const raw = await resolveText(ctx.values.args, { stdin: ctx.stdin, what: 'the arguments' })
402
- let args = {}
403
- if (raw !== undefined && String(raw).trim() !== '') {
404
- try {
405
- args = JSON.parse(raw)
406
- } catch (err) {
407
- throw new UserError(`--args is not valid JSON: ${err.message}`)
408
- }
409
- }
410
- return ctx.client.callTool(name, args)
439
+ return ctx.client.callTool(name, await readArgs(ctx))
411
440
  },
412
441
  },
413
442
  ]
414
443
 
444
+ /** --args as JSON, from inline, @file or stdin; nothing given is {}. */
445
+ async function readArgs(ctx) {
446
+ const raw = await resolveText(ctx.values.args, { stdin: ctx.stdin, what: 'the arguments' })
447
+ if (raw === undefined || String(raw).trim() === '') return {}
448
+ try {
449
+ return JSON.parse(raw)
450
+ } catch (err) {
451
+ throw new UserError(`--args is not valid JSON: ${err.message}`)
452
+ }
453
+ }
454
+
415
455
  /**
416
456
  * Match a leading path that names a family rather than a command — `task`,
417
457
  * `memory` — so it can be answered with that family's commands instead of