@moondeck/station 1.0.12 → 1.0.13
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/lib/agent-cli-issue-attachments.ts +117 -0
- package/lib/agent-cli-issue-comments.ts +103 -0
- package/lib/agent-cli-issues.ts +416 -0
- package/lib/agent-cli.ts +94 -7
- package/lib/issue-markdown.generated.ts +658 -0
- package/lib/orchestrator.ts +6 -0
- package/package.json +1 -1
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/** Standalone Issue attachment commands. All references resolve on the server. */
|
|
2
|
+
export const ISSUE_ATTACHMENTS_CLI_SOURCE = String.raw`
|
|
3
|
+
async function issueAttachmentGet(args) {
|
|
4
|
+
if (args[0] !== "get") issueUsage("attachment get KEY-N ATTACHMENT_ID [-o PATH] [--json]");
|
|
5
|
+
const { flags, positional } = issueFlags(args.slice(1).map(arg => arg === "-o" ? "--output" : arg), ["output", "json"]);
|
|
6
|
+
const key = issueKey(positional.slice(0, 1)), id = positional[1];
|
|
7
|
+
if (positional.length !== 2 || !id || id.length > 128 || id.includes(String.fromCharCode(0)))
|
|
8
|
+
issueUsage("attachment get needs an Issue key and attachment ID");
|
|
9
|
+
const resolved = await api("POST", "/api/agent/issues/" + encodeURIComponent(key) + "/attachments/resolve",
|
|
10
|
+
{ attachmentIds: [id] }, { attachmentAction: "get" });
|
|
11
|
+
const item = resolved.attachments.find(item => item.id === id);
|
|
12
|
+
if (!item || item.state === "no_access") issueUsage("attachment no_access: a human must grant Files read access within the owner's rights; no file downloaded");
|
|
13
|
+
if (item.state === "deleted") issueUsage("attachment file was deleted; no file downloaded");
|
|
14
|
+
if (item.state === "unavailable") issueUsage("attachment unavailable (retryable); retry later. No file downloaded");
|
|
15
|
+
if (item.state !== "available") issueUsage("unknown attachment state; no file downloaded");
|
|
16
|
+
const file = issueAttachmentFile(item.scope + "/" + item.path);
|
|
17
|
+
const dest = flags.output || defaultFilesDest(file.scope, file.path);
|
|
18
|
+
// Reuse Files' fresh authorization, ETag verification, bounded streaming,
|
|
19
|
+
// destination containment and atomic write. Never send the Task token to S3.
|
|
20
|
+
await downloadFile(file.scope, file.path, dest, flags.output === undefined, { remaining: FILES_CAP_BYTES });
|
|
21
|
+
out(flags.json ? { attachmentId: id, path: dest, downloaded: true } : "Downloaded " + id + " to " + dest);
|
|
22
|
+
}
|
|
23
|
+
function issueAttachmentFile(raw) {
|
|
24
|
+
const file = filesTarget(raw);
|
|
25
|
+
if (!file || !file.path || file.path.length > 900 || file.path.includes("\\") || file.path.includes(String.fromCharCode(0)) ||
|
|
26
|
+
file.path.split("/").some((part) => !part || part === "." || part === ".."))
|
|
27
|
+
issueUsage("Files paths must be org/relative/path or me/relative/path (no empty, dot or parent segments)");
|
|
28
|
+
return file;
|
|
29
|
+
}
|
|
30
|
+
async function issueUploadData(path) {
|
|
31
|
+
const limit = 200 * 1024 * 1024;
|
|
32
|
+
let file;
|
|
33
|
+
try {
|
|
34
|
+
file = await open(path, constants.O_RDONLY | constants.O_NONBLOCK);
|
|
35
|
+
const stat = await file.stat();
|
|
36
|
+
if (!stat.isFile()) issueUsage("--upload needs a regular local file");
|
|
37
|
+
if (stat.size > limit) issueUsage("upload exceeds the 200 MiB Files limit");
|
|
38
|
+
const chunks = []; let size = 0;
|
|
39
|
+
for await (const chunk of file.createReadStream({ start: 0, end: limit, autoClose: false })) {
|
|
40
|
+
size += chunk.length;
|
|
41
|
+
if (size > limit) issueUsage("upload exceeds the 200 MiB Files limit");
|
|
42
|
+
chunks.push(chunk);
|
|
43
|
+
}
|
|
44
|
+
return Buffer.concat(chunks);
|
|
45
|
+
} catch (error) {
|
|
46
|
+
issueUsage("cannot upload local file: " + error.message);
|
|
47
|
+
} finally { if (file) await file.close(); }
|
|
48
|
+
}
|
|
49
|
+
function issueAttachmentLine(item, key) {
|
|
50
|
+
if (item.state === "readable") return item.id + " readable (download: issue attachment get " + key + " " + item.id + ") " + item.reference;
|
|
51
|
+
if (item.state === "available") return item.id + " available " + item.scope + "/" + item.path + " " + item.size + " bytes " + item.reference;
|
|
52
|
+
if (item.state === "deleted") return item.id + " deleted " + item.name + " " + item.reference;
|
|
53
|
+
if (item.state === "unavailable") return item.id + " unavailable (retryable; rerun issue attachments later) " + item.reference;
|
|
54
|
+
return item.id + " no_access " + item.reference;
|
|
55
|
+
}
|
|
56
|
+
async function issueAttachmentsCommand(action, args) {
|
|
57
|
+
const { flags, positional } = issueFlags(args, action === "attachments" ? ["json"]
|
|
58
|
+
: action === "attach" ? ["upload", "expected-revision", "json"] : ["expected-revision", "json"]);
|
|
59
|
+
const key = issueKey(positional.slice(0, 1));
|
|
60
|
+
const expected = issueExpectedRevision(flags);
|
|
61
|
+
const upload = flags.upload !== undefined;
|
|
62
|
+
let files, bytes;
|
|
63
|
+
if (action === "attachments" && positional.length !== 1) issueUsage("attachments takes one Issue key");
|
|
64
|
+
if (action === "detach" && (positional.length !== 2 || !positional[1] || positional[1].length > 128 || positional[1].includes(String.fromCharCode(0))))
|
|
65
|
+
issueUsage("detach needs one Issue key and attachment ID");
|
|
66
|
+
if (action === "attach") {
|
|
67
|
+
if (upload && positional.length !== 1) issueUsage("--upload cannot be combined with existing Files paths");
|
|
68
|
+
if (!upload && (positional.length < 2 || positional.length > 21)) issueUsage("attach needs 1–20 Files paths, or --upload LOCAL_PATH");
|
|
69
|
+
if (!upload) {
|
|
70
|
+
files = positional.slice(1).map(issueAttachmentFile);
|
|
71
|
+
if (new Set(files.map(file => JSON.stringify(file))).size !== files.length) issueUsage("duplicate Files paths");
|
|
72
|
+
} else bytes = await issueUploadData(flags.upload);
|
|
73
|
+
}
|
|
74
|
+
const path = "/api/agent/issues/" + encodeURIComponent(key) + "/attachments";
|
|
75
|
+
const context = { attachmentAction: action };
|
|
76
|
+
// This also checks live Issue write and Files admission before uploading.
|
|
77
|
+
const current = await api("GET", path, undefined, context);
|
|
78
|
+
if (action === "attachments") {
|
|
79
|
+
if (current.attachments.length > 100) issueUsage("Cannot resolve more than 100 attachment references per request; an Issue supports at most 100. Rerun issue show to inspect its current revision");
|
|
80
|
+
const resolved = current.attachments.length ? await api("POST", path + "/resolve", {
|
|
81
|
+
attachmentIds: current.attachments.map(item => item.id),
|
|
82
|
+
}, context) : { issueId: current.issueId, revision: current.revision, attachments: [] };
|
|
83
|
+
const references = new Map(current.attachments.map(item => [item.id, item.reference]));
|
|
84
|
+
const result = { ...resolved, attachments: resolved.attachments.map(item => ({ ...item, reference: references.get(item.id) })) };
|
|
85
|
+
if (flags.json) return out(result);
|
|
86
|
+
out("Attachment references for " + key + " (revision " + result.revision + "):");
|
|
87
|
+
return out(result.attachments.length ? result.attachments.map(item => issueAttachmentLine(item, key)).join(NL) : "No attachments.");
|
|
88
|
+
}
|
|
89
|
+
const expectedRevision = expected ?? current.revision;
|
|
90
|
+
if (action === "detach") {
|
|
91
|
+
const result = await api("DELETE", path, { expectedRevision, attachmentIds: [positional[1]] }, context);
|
|
92
|
+
return out(flags.json ? { ...result, fileDeleted: false } : "Detached " + positional[1] + " from " + key + " (unlinked only; file not deleted). Revision: " + result.revision);
|
|
93
|
+
}
|
|
94
|
+
let uploadedFile;
|
|
95
|
+
if (upload) {
|
|
96
|
+
const detail = await api("GET", "/api/agent/issues/" + encodeURIComponent(key), undefined, context);
|
|
97
|
+
if (detail.issue.revision !== expectedRevision) issueUsage("The Issue changed since revision " + expectedRevision + "; nothing uploaded or attached. Rerun issue show and retry with --expected-revision");
|
|
98
|
+
const canonicalKey = issueKey([detail.issue.identifier]);
|
|
99
|
+
const folder = "Issues/" + canonicalKey;
|
|
100
|
+
// Validate org read before writing; attaching needs it even with files.write.
|
|
101
|
+
await api("GET", filesQuery("org", folder), undefined, context);
|
|
102
|
+
// A unique basename avoids replacing an existing Files object or changing
|
|
103
|
+
// the contents of somebody else's reference to the same scoped key.
|
|
104
|
+
uploadedFile = issueAttachmentFile("org/" + folder + "/" + randomUUID() + "-" + basename(flags.upload));
|
|
105
|
+
const uploadedPath = "org/" + uploadedFile.path;
|
|
106
|
+
await api("PUT", "/api/agent/files/content?scope=org&path=" + encodeURIComponent(uploadedFile.path), bytes,
|
|
107
|
+
{ raw: true, uploadPath: uploadedPath });
|
|
108
|
+
files = [uploadedFile];
|
|
109
|
+
context.uploadedPath = uploadedPath;
|
|
110
|
+
}
|
|
111
|
+
const result = await api("POST", path, { expectedRevision, files }, context);
|
|
112
|
+
if (flags.json) return out(uploadedFile ? { ...result, uploadedFile } : result);
|
|
113
|
+
if (uploadedFile) out("Uploaded to Files: org/" + uploadedFile.path);
|
|
114
|
+
out((result.changed ? "Attached " : "Already attached: ") + files.length + " file reference(s) to " + key + ". Revision: " + result.revision);
|
|
115
|
+
for (const item of result.attachments) out(item.id + " " + item.reference);
|
|
116
|
+
}
|
|
117
|
+
`;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/** Embedded alongside the Issue commands; all authority stays on the server. */
|
|
2
|
+
export const ISSUE_COMMENTS_CLI_SOURCE = String.raw`
|
|
3
|
+
function issueCommentText(flags) {
|
|
4
|
+
if ((flags.body === undefined) === (flags["body-file"] === undefined))
|
|
5
|
+
issueUsage("comment needs exactly one of --body TEXT or --body-file PATH (use - for stdin)");
|
|
6
|
+
if (flags["body-file"] === "-" && process.stdin.isTTY)
|
|
7
|
+
issueUsage("comment stdin is a terminal; pipe text or use --body-file PATH");
|
|
8
|
+
const text = (flags.body !== undefined ? flags.body : readFlagFile(flags["body-file"])).trim();
|
|
9
|
+
if (!text || text.length > 4000 || text.includes(String.fromCharCode(0)))
|
|
10
|
+
issueUsage("comment body must contain 1–4000 UTF-16 code units after trimming, without NUL; nothing was sent");
|
|
11
|
+
return text;
|
|
12
|
+
}
|
|
13
|
+
async function issueMentionIds(names) {
|
|
14
|
+
if (!names.length) return [];
|
|
15
|
+
const normalize = value => value.trim().replace(/\s+/g, " ").toLowerCase();
|
|
16
|
+
if (names.some(name => !normalize(name))) issueUsage("--mention needs a nonempty member name");
|
|
17
|
+
if (names.length > 50) issueUsage("at most 50 --mention values are allowed");
|
|
18
|
+
const members = new Map(), cursors = new Set();
|
|
19
|
+
let cursor;
|
|
20
|
+
for (let pages = 0; pages < 50; pages++) {
|
|
21
|
+
if (cursor && cursors.has(cursor)) issueUsage("member lookup repeated a cursor; nothing changed. Retry the lookup later");
|
|
22
|
+
if (cursor) cursors.add(cursor);
|
|
23
|
+
const query = new URLSearchParams({ limit: "100" });
|
|
24
|
+
if (cursor) query.set("cursor", cursor);
|
|
25
|
+
const page = await api("GET", "/api/agent/issues/members?" + query);
|
|
26
|
+
for (const member of page.members) members.set(member.userId, member);
|
|
27
|
+
cursor = page.nextCursor;
|
|
28
|
+
if (!cursor) break;
|
|
29
|
+
}
|
|
30
|
+
if (cursor) issueUsage("member lookup exceeded 50 pages; cannot safely resolve names. Nothing was changed");
|
|
31
|
+
return [...new Set(names.map(name => {
|
|
32
|
+
const normalized = normalize(name);
|
|
33
|
+
const exactId = members.get(name.trim());
|
|
34
|
+
const matches = exactId ? [exactId] : [...members.values()].filter(member => normalize(member.name) === normalized);
|
|
35
|
+
if (!matches.length) issueUsage("unknown or unreadable workspace member " + JSON.stringify(name) + "; use a current human member's display name or user ID, not an agent handle. Nothing was changed");
|
|
36
|
+
if (matches.length > 1) issueUsage("ambiguous member " + JSON.stringify(name) + "; use a user ID: " + matches.map(member => member.userId).join(", ") + ". Nothing was changed");
|
|
37
|
+
return matches[0].userId;
|
|
38
|
+
}))];
|
|
39
|
+
}
|
|
40
|
+
async function issueFindComment(key, id) {
|
|
41
|
+
let cursor;
|
|
42
|
+
const cursors = new Set();
|
|
43
|
+
for (let pages = 0; pages < 50; pages++) {
|
|
44
|
+
if (cursor && cursors.has(cursor)) issueUsage("comment history repeated a cursor; rerun issue comments before changing anything");
|
|
45
|
+
if (cursor) cursors.add(cursor);
|
|
46
|
+
const query = new URLSearchParams({ limit: "100" });
|
|
47
|
+
if (cursor) query.set("beforeSeq", String(cursor));
|
|
48
|
+
const page = await api("GET", "/api/agent/issues/" + encodeURIComponent(key) + "/chat?" + query);
|
|
49
|
+
const comment = page.messages.find(message => message.kind === "comment" && message.id === id);
|
|
50
|
+
if (comment) return comment;
|
|
51
|
+
cursor = page.nextBeforeSeq;
|
|
52
|
+
if (cursor == null) return null;
|
|
53
|
+
}
|
|
54
|
+
issueUsage("comment not found in 50 history pages; inspect issue comments before retrying");
|
|
55
|
+
}
|
|
56
|
+
async function issueCommentCommand(args) {
|
|
57
|
+
const action = ["post", "edit", "delete"].includes(args[0]) ? args.shift() : "post";
|
|
58
|
+
const writing = action !== "delete";
|
|
59
|
+
const allowed = ["json", ...(writing ? ["body", "body-file", "mention"] : []),
|
|
60
|
+
...(action !== "post" ? ["expected-revision"] : []), ...(action === "edit" ? ["clear-mentions"] : [])];
|
|
61
|
+
const { flags, positional } = issueFlags(args, allowed, ["json", "clear-mentions"]);
|
|
62
|
+
const key = issueKey(positional.slice(0, 1));
|
|
63
|
+
if (positional.length !== (action === "post" ? 1 : 2)) issueUsage("comment " + action + " needs an Issue key" + (action === "post" ? "" : " and comment ID"));
|
|
64
|
+
const id = positional[1];
|
|
65
|
+
if (id !== undefined && (!id.trim() || id.length > 128 || /[\s/\x00]/.test(id))) issueUsage("invalid comment ID");
|
|
66
|
+
const expected = issueExpectedRevision(flags);
|
|
67
|
+
if (expected !== undefined && expected < 1) issueUsage("comment --expected-revision must be positive (use the comment revision)");
|
|
68
|
+
const text = writing ? issueCommentText(flags) : undefined;
|
|
69
|
+
if (flags["clear-mentions"] && flags.mention) issueUsage("--clear-mentions cannot be combined with --mention");
|
|
70
|
+
const path = "/api/agent/issues/" + encodeURIComponent(key) + "/chat/comments";
|
|
71
|
+
let current;
|
|
72
|
+
if (action !== "post" && (expected === undefined || (action === "edit" && !flags.mention && !flags["clear-mentions"]))) {
|
|
73
|
+
current = await issueFindComment(key, id);
|
|
74
|
+
if (!current) issueUsage("comment not found; nothing changed. Run issue comments --json");
|
|
75
|
+
if (current[action === "edit" ? "canEdit" : "canDelete"] !== true)
|
|
76
|
+
issueUsage("cannot " + action + " this comment: issues.read + issues.write and the same owner, Task and agent are required. A human must grant missing issues.write; removed comments cannot be edited");
|
|
77
|
+
}
|
|
78
|
+
const body = writing ? { text, mentionedUserIds: flags.mention ? await issueMentionIds(flags.mention)
|
|
79
|
+
: action === "edit" && !flags["clear-mentions"] ? current.mentionedUserIds || [] : [] } : {};
|
|
80
|
+
const context = { commentAction: action };
|
|
81
|
+
if (action === "post") {
|
|
82
|
+
body.clientRequestId = randomUUID();
|
|
83
|
+
context.commentRequestId = body.clientRequestId;
|
|
84
|
+
} else {
|
|
85
|
+
body.expectedRevision = expected ?? current.revision;
|
|
86
|
+
context.commentRevision = body.expectedRevision;
|
|
87
|
+
}
|
|
88
|
+
const result = await api(action === "post" ? "POST" : action === "edit" ? "PATCH" : "DELETE",
|
|
89
|
+
path + (id === undefined ? "" : "/" + encodeURIComponent(id)), body, context);
|
|
90
|
+
if (context.transportRetries) {
|
|
91
|
+
// A replay receipt is the original admission, even if it was edited/deleted
|
|
92
|
+
// since. Refresh rather than claiming that original body is still current.
|
|
93
|
+
console.error("Comment accepted (clientRequestId " + body.clientRequestId + "). Refreshing history after the transport retry; receipt revision may be historical.");
|
|
94
|
+
result.currentComment = await issueFindComment(key, result.id);
|
|
95
|
+
if (!result.currentComment) console.error("Accepted comment is absent from current history; inspect issue comments --json before taking further action.");
|
|
96
|
+
}
|
|
97
|
+
if (flags.json) return out(result);
|
|
98
|
+
out((action === "post" ? "Comment accepted: " : action === "edit" ? "Comment edited: " : "Comment removed (placeholder retained): ") + result.id +
|
|
99
|
+
" (comment revision " + (result.currentComment?.revision ?? result.revision) + ")" + (result.clientRequestId ? "; clientRequestId " + result.clientRequestId : ""));
|
|
100
|
+
if (result.currentComment) out("Current: " + result.currentComment.actor.name + " — " +
|
|
101
|
+
(result.currentComment.deletedAt != null ? "Message removed" : result.currentComment.body));
|
|
102
|
+
}
|
|
103
|
+
`;
|