@agentrq/agentrq-ws 0.18.4 → 0.18.6
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 +4 -0
- package/package.json +1 -1
- package/src/attachments.js +40 -0
- package/src/commands.js +17 -2
package/README.md
CHANGED
|
@@ -95,6 +95,10 @@ agentrq-ws reply 0isnjTCkpW5 "Build is green — log attached" --attach ./run.lo
|
|
|
95
95
|
agentrq-ws task create "Review the design" --body @brief.md --attach ./mock.png
|
|
96
96
|
```
|
|
97
97
|
|
|
98
|
+
The server takes at most 4 MiB per request, and base64 adds a third, so a file
|
|
99
|
+
over about 3 MiB is refused before anything is sent: gzip it first. Files that
|
|
100
|
+
fit only on their own go one `reply` each.
|
|
101
|
+
|
|
98
102
|
**Downloading** writes a file and prints where it went. With no `--out` it lands
|
|
99
103
|
in the OS temp directory:
|
|
100
104
|
|
package/package.json
CHANGED
package/src/attachments.js
CHANGED
|
@@ -81,6 +81,46 @@ export function readAttachment(filePath, { readFile = readFileSync, stat = statS
|
|
|
81
81
|
}
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
+
/** The workspace server refuses a request body over 4 MiB, with HTTP 413. */
|
|
85
|
+
export const MAX_REQUEST_BYTES = 4 << 20
|
|
86
|
+
|
|
87
|
+
/** Room kept for the JSON-RPC envelope, the tool's name and the ids around the payload. */
|
|
88
|
+
const ENVELOPE_BYTES = 16 << 10
|
|
89
|
+
|
|
90
|
+
const mib = (bytes) => `${(bytes / (1 << 20)).toFixed(1)} MiB`
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Split attachments into groups that each fit one request beside `reservedBytes`
|
|
94
|
+
* of other payload, in the order given. Base64 grows a file by a third, so a
|
|
95
|
+
* file over about 3 MiB fits no request: it is refused here, naming the file,
|
|
96
|
+
* rather than sent to come back as a bare 413.
|
|
97
|
+
*/
|
|
98
|
+
export function batchAttachments(attachments, reservedBytes = 0, limit = MAX_REQUEST_BYTES) {
|
|
99
|
+
const room = limit - ENVELOPE_BYTES - reservedBytes
|
|
100
|
+
const batches = []
|
|
101
|
+
let current = []
|
|
102
|
+
let used = 0
|
|
103
|
+
for (const attachment of attachments) {
|
|
104
|
+
const size = Buffer.byteLength(JSON.stringify(attachment))
|
|
105
|
+
if (size > room) {
|
|
106
|
+
const raw = Buffer.byteLength(attachment.data, 'base64')
|
|
107
|
+
throw new UserError(
|
|
108
|
+
`cannot attach ${attachment.filename}: ${mib(raw)} is ${mib(size)} once base64-encoded, ` +
|
|
109
|
+
`over the workspace server's ${mib(limit)} request limit; compress it (gzip) or split it`,
|
|
110
|
+
)
|
|
111
|
+
}
|
|
112
|
+
if (current.length > 0 && used + size > room) {
|
|
113
|
+
batches.push(current)
|
|
114
|
+
current = []
|
|
115
|
+
used = 0
|
|
116
|
+
}
|
|
117
|
+
current.push(attachment)
|
|
118
|
+
used += size
|
|
119
|
+
}
|
|
120
|
+
if (current.length > 0) batches.push(current)
|
|
121
|
+
return batches
|
|
122
|
+
}
|
|
123
|
+
|
|
84
124
|
/**
|
|
85
125
|
* Work out where a downloaded attachment should land.
|
|
86
126
|
*
|
package/src/commands.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
import { readFileSync } from 'node:fs'
|
|
5
5
|
import { basename, join } from 'node:path'
|
|
6
6
|
|
|
7
|
-
import { readAttachment, resolveOutputPath, writeAttachment } from './attachments.js'
|
|
7
|
+
import { batchAttachments, readAttachment, resolveOutputPath, writeAttachment } from './attachments.js'
|
|
8
8
|
import { UserError } from './errors.js'
|
|
9
9
|
|
|
10
10
|
/** Statuses the workspace accepts, mirrored from isValidTaskStatus on the backend. */
|
|
@@ -300,6 +300,10 @@ export const COMMANDS = [
|
|
|
300
300
|
clearContext: ctx.values['clear-context'] || undefined,
|
|
301
301
|
attachments: collectAttachments(ctx.values),
|
|
302
302
|
}
|
|
303
|
+
const others = Buffer.byteLength(JSON.stringify({ ...payload, attachments: undefined }))
|
|
304
|
+
if (payload.attachments && batchAttachments(payload.attachments, others).length > 1) {
|
|
305
|
+
throw new UserError('the attachments do not fit one request together: attach fewer here and send the rest with reply')
|
|
306
|
+
}
|
|
303
307
|
return ctx.client.callAction('task', 'create', payload, ['createTask', payload])
|
|
304
308
|
},
|
|
305
309
|
},
|
|
@@ -328,7 +332,18 @@ export const COMMANDS = [
|
|
|
328
332
|
const raw = requirePositional(ctx.positionals, 1, 'text')
|
|
329
333
|
const text = await resolveText(raw, { stdin: ctx.stdin, what: 'the reply text' })
|
|
330
334
|
const attachments = collectAttachments(ctx.values)
|
|
331
|
-
return ctx.client.callAction('task', 'reply', { taskId, text
|
|
335
|
+
if (!attachments) return ctx.client.callAction('task', 'reply', { taskId, text }, ['reply', { chatId: taskId, text }])
|
|
336
|
+
// Files too large to share one request go one reply per group, the text
|
|
337
|
+
// with the first and the rest labelled by their position.
|
|
338
|
+
const batches = batchAttachments(attachments, Buffer.byteLength(JSON.stringify(text)))
|
|
339
|
+
const outcomes = []
|
|
340
|
+
for (const [i, group] of batches.entries()) {
|
|
341
|
+
const body = i === 0 ? text : `Attachments ${i + 1}/${batches.length}: ${group.map((a) => a.filename).join(', ')}`
|
|
342
|
+
const payload = { taskId, text: body, attachments: group }
|
|
343
|
+
outcomes.push(await ctx.client.callAction('task', 'reply', payload, ['reply', { chatId: taskId, text: body, attachments: group }]))
|
|
344
|
+
}
|
|
345
|
+
if (outcomes.length === 1) return outcomes[0]
|
|
346
|
+
return { text: outcomes.map((o) => o.text).join('\n'), result: outcomes.at(-1).result }
|
|
332
347
|
},
|
|
333
348
|
},
|
|
334
349
|
{
|