@commonlyai/cli 0.1.214 → 0.1.215
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/package.json +1 -1
- package/src/commands/task.js +278 -0
- package/src/index.js +4 -0
- package/src/lib/task.js +234 -0
package/package.json
CHANGED
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* commonly task <subcommand>
|
|
3
|
+
*
|
|
4
|
+
* list — the pod's board, optionally narrowed by status, or to rows this identity holds
|
|
5
|
+
* create — a new row; --notes is a second request, because create does not take notes
|
|
6
|
+
* claim — take the lease on a row
|
|
7
|
+
* update — change a row's fields, and/or append a history note
|
|
8
|
+
* close — mark a row done, as its creator or its holder
|
|
9
|
+
*
|
|
10
|
+
* Identity and base URL come from ../lib/task.js. Every subcommand goes through
|
|
11
|
+
* connect() below, so none can grow its own fallback to the saved login. Flags are
|
|
12
|
+
* checked before the first request, so a refusal costs no network round trip.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { TOKEN_FILE_ENV } from '../lib/inbox.js';
|
|
16
|
+
import {
|
|
17
|
+
TASK_PRIORITIES,
|
|
18
|
+
TASK_STATUSES,
|
|
19
|
+
TASK_TOKEN_ENV,
|
|
20
|
+
TaskRefusal,
|
|
21
|
+
UPDATE_STATUSES,
|
|
22
|
+
checkPriority,
|
|
23
|
+
checkStatusFilter,
|
|
24
|
+
checkTaskId,
|
|
25
|
+
checkUpdateStatus,
|
|
26
|
+
createTaskClient,
|
|
27
|
+
describeFailure,
|
|
28
|
+
formatTaskLine,
|
|
29
|
+
requireText,
|
|
30
|
+
resolveTaskBaseUrl,
|
|
31
|
+
resolveTaskToken,
|
|
32
|
+
taskPath,
|
|
33
|
+
} from '../lib/task.js';
|
|
34
|
+
|
|
35
|
+
export const TASK_TOKEN_HELP = `Agent token file (or ${TOKEN_FILE_ENV}, or ${TASK_TOKEN_ENV})`;
|
|
36
|
+
|
|
37
|
+
const connect = (opts, env, fetchImpl) => createTaskClient({
|
|
38
|
+
baseUrl: resolveTaskBaseUrl(opts, env),
|
|
39
|
+
token: resolveTaskToken(opts, env),
|
|
40
|
+
fetchImpl,
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const podOf = (opts) => requireText(opts.pod, '--pod');
|
|
44
|
+
|
|
45
|
+
const report = (err, error) => {
|
|
46
|
+
error(err instanceof TaskRefusal ? `refusing: ${err.message}` : describeFailure(err));
|
|
47
|
+
return 1;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
// A row goes out as JSON for agents, and as the line a person scans otherwise.
|
|
51
|
+
const emitRow = (opts, log, task) => {
|
|
52
|
+
log(opts.json ? JSON.stringify(task, null, 2) : formatTaskLine(task));
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export const runTaskList = async (opts = {}, deps = {}) => {
|
|
56
|
+
const {
|
|
57
|
+
env = process.env, fetchImpl = fetch, log = console.log, error = console.error,
|
|
58
|
+
} = deps;
|
|
59
|
+
try {
|
|
60
|
+
const pod = podOf(opts);
|
|
61
|
+
const status = opts.status === undefined ? undefined : checkStatusFilter(opts.status);
|
|
62
|
+
const { tasks } = await connect(opts, env, fetchImpl).get(taskPath(pod), { status });
|
|
63
|
+
// The server marks each row claimedByMe for this identity, so --mine needs no second request.
|
|
64
|
+
const rows = opts.mine ? tasks.filter((task) => task.claimedByMe) : tasks;
|
|
65
|
+
if (opts.json) {
|
|
66
|
+
log(JSON.stringify(rows, null, 2));
|
|
67
|
+
} else if (rows.length === 0) {
|
|
68
|
+
log('(no tasks match)');
|
|
69
|
+
} else {
|
|
70
|
+
for (const task of rows) log(formatTaskLine(task));
|
|
71
|
+
}
|
|
72
|
+
return 0;
|
|
73
|
+
} catch (err) {
|
|
74
|
+
return report(err, error);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
export const runTaskCreate = async (opts = {}, deps = {}) => {
|
|
79
|
+
const {
|
|
80
|
+
env = process.env, fetchImpl = fetch, log = console.log, error = console.error,
|
|
81
|
+
} = deps;
|
|
82
|
+
try {
|
|
83
|
+
const pod = podOf(opts);
|
|
84
|
+
const body = { title: requireText(opts.title, '--title') };
|
|
85
|
+
if (opts.priority !== undefined) body.priority = checkPriority(opts.priority);
|
|
86
|
+
if (opts.assignee !== undefined) body.assignee = requireText(opts.assignee, '--assignee');
|
|
87
|
+
if (opts.dep !== undefined) body.dep = checkTaskId(opts.dep);
|
|
88
|
+
const notes = opts.notes === undefined ? undefined : requireText(opts.notes, '--notes');
|
|
89
|
+
const client = connect(opts, env, fetchImpl);
|
|
90
|
+
const { task } = await client.post(taskPath(pod), body);
|
|
91
|
+
if (notes === undefined) {
|
|
92
|
+
emitRow(opts, log, task);
|
|
93
|
+
return 0;
|
|
94
|
+
}
|
|
95
|
+
// The row exists once the create returns. If the notes fail, say so, so nobody
|
|
96
|
+
// retries the create and makes a twin.
|
|
97
|
+
try {
|
|
98
|
+
const { task: noted } = await client.patch(taskPath(pod, task.taskId), { notes });
|
|
99
|
+
emitRow(opts, log, noted);
|
|
100
|
+
return 0;
|
|
101
|
+
} catch (err) {
|
|
102
|
+
error(`Created ${task.taskId}, but --notes did not save. ${describeFailure(err)}`);
|
|
103
|
+
return 1;
|
|
104
|
+
}
|
|
105
|
+
} catch (err) {
|
|
106
|
+
return report(err, error);
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
export const runTaskClaim = async (taskArg, opts = {}, deps = {}) => {
|
|
111
|
+
const {
|
|
112
|
+
env = process.env, fetchImpl = fetch, log = console.log, error = console.error,
|
|
113
|
+
} = deps;
|
|
114
|
+
try {
|
|
115
|
+
const pod = podOf(opts);
|
|
116
|
+
const taskId = checkTaskId(taskArg);
|
|
117
|
+
const body = opts.continueAnyway ? { continueAnyway: true } : {};
|
|
118
|
+
const { task } = await connect(opts, env, fetchImpl).post(`${taskPath(pod, taskId)}/claim`, body);
|
|
119
|
+
emitRow(opts, log, task);
|
|
120
|
+
return 0;
|
|
121
|
+
} catch (err) {
|
|
122
|
+
return report(err, error);
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
export const runTaskUpdate = async (taskArg, opts = {}, deps = {}) => {
|
|
127
|
+
const {
|
|
128
|
+
env = process.env, fetchImpl = fetch, log = console.log, error = console.error,
|
|
129
|
+
} = deps;
|
|
130
|
+
try {
|
|
131
|
+
const pod = podOf(opts);
|
|
132
|
+
const taskId = checkTaskId(taskArg);
|
|
133
|
+
const fields = {};
|
|
134
|
+
if (opts.status !== undefined) fields.status = checkUpdateStatus(opts.status);
|
|
135
|
+
// An empty value unassigns. The server stores that as null, not as '', so the row
|
|
136
|
+
// reads as unassigned to the router.
|
|
137
|
+
if (opts.assignee !== undefined) fields.assignee = String(opts.assignee).trim();
|
|
138
|
+
if (opts.title !== undefined) fields.title = requireText(opts.title, '--title');
|
|
139
|
+
if (opts.priority !== undefined) fields.priority = checkPriority(opts.priority);
|
|
140
|
+
// "none" clears the dependency. Sending '' would store a dependency that names nothing.
|
|
141
|
+
if (opts.dep !== undefined) fields.dep = opts.dep === 'none' ? null : checkTaskId(opts.dep);
|
|
142
|
+
const note = opts.note === undefined ? undefined : requireText(opts.note, '--note');
|
|
143
|
+
if (Object.keys(fields).length === 0 && note === undefined) {
|
|
144
|
+
throw new TaskRefusal('nothing to change. Pass --status, --assignee, --title, --priority, --dep or --note.');
|
|
145
|
+
}
|
|
146
|
+
const client = connect(opts, env, fetchImpl);
|
|
147
|
+
const path = taskPath(pod, taskId);
|
|
148
|
+
let task = null;
|
|
149
|
+
if (Object.keys(fields).length > 0) ({ task } = await client.patch(path, fields));
|
|
150
|
+
if (note !== undefined) {
|
|
151
|
+
try {
|
|
152
|
+
const body = opts.continueAnyway ? { text: note, continueAnyway: true } : { text: note };
|
|
153
|
+
({ task } = await client.post(`${path}/updates`, body));
|
|
154
|
+
} catch (err) {
|
|
155
|
+
// With no field change before it, a failed note is the whole failure.
|
|
156
|
+
if (task === null) throw err;
|
|
157
|
+
error(`Updated ${taskId}, but --note did not post. ${describeFailure(err)}`);
|
|
158
|
+
return 1;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
emitRow(opts, log, task);
|
|
162
|
+
return 0;
|
|
163
|
+
} catch (err) {
|
|
164
|
+
return report(err, error);
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
export const runTaskClose = async (taskArg, opts = {}, deps = {}) => {
|
|
169
|
+
const {
|
|
170
|
+
env = process.env, fetchImpl = fetch, log = console.log, error = console.error,
|
|
171
|
+
} = deps;
|
|
172
|
+
try {
|
|
173
|
+
const pod = podOf(opts);
|
|
174
|
+
const taskId = checkTaskId(taskArg);
|
|
175
|
+
const prUrl = opts.pr === undefined ? undefined : requireText(opts.pr, '--pr');
|
|
176
|
+
// No notes key here. /complete replaces the row's notes whenever one is sent.
|
|
177
|
+
const body = prUrl === undefined ? {} : { prUrl };
|
|
178
|
+
const { task } = await connect(opts, env, fetchImpl).post(`${taskPath(pod, taskId)}/complete`, body);
|
|
179
|
+
emitRow(opts, log, task);
|
|
180
|
+
return 0;
|
|
181
|
+
} catch (err) {
|
|
182
|
+
return report(err, error);
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
export const registerTask = (program) => {
|
|
187
|
+
const task = program
|
|
188
|
+
.command('task')
|
|
189
|
+
.description('Read and write the task board as an agent (list, create, claim, update, close)');
|
|
190
|
+
|
|
191
|
+
task.addHelpText('after', `
|
|
192
|
+
The board is read and written as the identity in a token file, and nowhere else:
|
|
193
|
+
|
|
194
|
+
$ commonly task list --pod <podId> --token-file <path>
|
|
195
|
+
$ COMMONLY_AGENT_TOKEN=<token> commonly task claim TASK-123 --pod <podId>
|
|
196
|
+
|
|
197
|
+
The identity is --token-file, then COMMONLY_TOKEN_FILE, then COMMONLY_AGENT_TOKEN.
|
|
198
|
+
The saved login and COMMONLY_TOKEN are never read: a write made under an
|
|
199
|
+
operator's login lands on the row under that operator's name.
|
|
200
|
+
|
|
201
|
+
Each verb does what it says. \`claim\` takes the lease. \`close\` stamps the
|
|
202
|
+
completion time and the PR. \`update --status\` sets only pending or blocked, so it
|
|
203
|
+
cannot claim or close a row. \`update --note\` appends a history line. The row's
|
|
204
|
+
notes are set by \`create --notes\` and are not editable here.
|
|
205
|
+
|
|
206
|
+
A claim or a note can refuse with unseen messages in the pod. Read them and retry,
|
|
207
|
+
or pass --continue-anyway to go ahead without reading them.
|
|
208
|
+
|
|
209
|
+
Examples:
|
|
210
|
+
$ commonly task list --pod <podId> --status pending,claimed
|
|
211
|
+
$ commonly task list --pod <podId> --mine --json
|
|
212
|
+
$ commonly task create --pod <podId> --title "Fix the board" --priority P1 --notes "Repro in staging"
|
|
213
|
+
$ commonly task claim TASK-123 --pod <podId>
|
|
214
|
+
$ commonly task update TASK-123 --pod <podId> --note "PR is up"
|
|
215
|
+
$ commonly task close TASK-123 --pod <podId> --pr https://github.com/Team-Commonly/commonly/pull/<n>
|
|
216
|
+
`);
|
|
217
|
+
|
|
218
|
+
task
|
|
219
|
+
.command('list')
|
|
220
|
+
.description("List a pod's task board")
|
|
221
|
+
.requiredOption('--pod <podId>', 'Pod whose board to read')
|
|
222
|
+
.option('--status <list>', `Only these statuses, comma-separated (${TASK_STATUSES.join('|')})`)
|
|
223
|
+
.option('--mine', 'Only rows this identity holds a claim on')
|
|
224
|
+
.option('--token-file <path>', TASK_TOKEN_HELP)
|
|
225
|
+
.option('--instance <url>', 'Target Commonly instance')
|
|
226
|
+
.option('--json', 'Emit the rows as JSON')
|
|
227
|
+
.action(async (opts) => { process.exit(await runTaskList(opts)); });
|
|
228
|
+
|
|
229
|
+
task
|
|
230
|
+
.command('create')
|
|
231
|
+
.description('Create a row, then set its --notes (create does not take notes)')
|
|
232
|
+
.requiredOption('--pod <podId>', 'Pod to create the row in')
|
|
233
|
+
.requiredOption('--title <text>', 'Row title')
|
|
234
|
+
.option('--notes <text>', 'Notes to set once the row exists')
|
|
235
|
+
.option('--priority <P>', `${TASK_PRIORITIES.join('|')} (default P2)`)
|
|
236
|
+
.option('--assignee <name>', 'Assign the row at creation')
|
|
237
|
+
.option('--dep <TASK-n>', 'Row this one waits on')
|
|
238
|
+
.option('--token-file <path>', TASK_TOKEN_HELP)
|
|
239
|
+
.option('--instance <url>', 'Target Commonly instance')
|
|
240
|
+
.option('--json', 'Emit the row as JSON')
|
|
241
|
+
.action(async (opts) => { process.exit(await runTaskCreate(opts)); });
|
|
242
|
+
|
|
243
|
+
task
|
|
244
|
+
.command('claim <taskId>')
|
|
245
|
+
.description('Take the lease on a row (TASK-n)')
|
|
246
|
+
.requiredOption('--pod <podId>', 'Pod the row is in')
|
|
247
|
+
.option('--continue-anyway', 'Claim even with unseen messages in the pod')
|
|
248
|
+
.option('--token-file <path>', TASK_TOKEN_HELP)
|
|
249
|
+
.option('--instance <url>', 'Target Commonly instance')
|
|
250
|
+
.option('--json', 'Emit the row as JSON')
|
|
251
|
+
.action(async (taskId, opts) => { process.exit(await runTaskClaim(taskId, opts)); });
|
|
252
|
+
|
|
253
|
+
task
|
|
254
|
+
.command('update <taskId>')
|
|
255
|
+
.description("Change a row's fields, and/or append a history note")
|
|
256
|
+
.requiredOption('--pod <podId>', 'Pod the row is in')
|
|
257
|
+
.option('--status <status>', `${UPDATE_STATUSES.join('|')} (claim and close have their own commands)`)
|
|
258
|
+
.option('--assignee <name>', 'Reassign the row; an empty value unassigns it')
|
|
259
|
+
.option('--title <text>', 'New title')
|
|
260
|
+
.option('--priority <P>', TASK_PRIORITIES.join('|'))
|
|
261
|
+
.option('--dep <TASK-n>', 'Row this one waits on; "none" clears it')
|
|
262
|
+
.option('--note <text>', 'Append a history note')
|
|
263
|
+
.option('--continue-anyway', 'Post the note even with unseen messages in the pod')
|
|
264
|
+
.option('--token-file <path>', TASK_TOKEN_HELP)
|
|
265
|
+
.option('--instance <url>', 'Target Commonly instance')
|
|
266
|
+
.option('--json', 'Emit the row as JSON')
|
|
267
|
+
.action(async (taskId, opts) => { process.exit(await runTaskUpdate(taskId, opts)); });
|
|
268
|
+
|
|
269
|
+
task
|
|
270
|
+
.command('close <taskId>')
|
|
271
|
+
.description('Mark a row done, as its creator or its holder')
|
|
272
|
+
.requiredOption('--pod <podId>', 'Pod the row is in')
|
|
273
|
+
.option('--pr <url>', 'Pull request that delivered the row')
|
|
274
|
+
.option('--token-file <path>', TASK_TOKEN_HELP)
|
|
275
|
+
.option('--instance <url>', 'Target Commonly instance')
|
|
276
|
+
.option('--json', 'Emit the row as JSON')
|
|
277
|
+
.action(async (taskId, opts) => { process.exit(await runTaskClose(taskId, opts)); });
|
|
278
|
+
};
|
package/src/index.js
CHANGED
|
@@ -23,6 +23,7 @@ import { registerDev } from './commands/dev.js';
|
|
|
23
23
|
import { registerVerify } from './commands/verify.js';
|
|
24
24
|
import { registerDoctor } from './commands/doctor.js';
|
|
25
25
|
import { registerMcp } from './commands/mcp.js';
|
|
26
|
+
import { registerTask } from './commands/task.js';
|
|
26
27
|
|
|
27
28
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
28
29
|
const pkg = JSON.parse(readFileSync(join(__dirname, '../package.json'), 'utf8'));
|
|
@@ -60,6 +61,9 @@ registerVerify(program);
|
|
|
60
61
|
registerDoctor(program);
|
|
61
62
|
registerMcp(program);
|
|
62
63
|
|
|
64
|
+
// The task board as an agent: list, create, claim, update, close (TASK-703)
|
|
65
|
+
registerTask(program);
|
|
66
|
+
|
|
63
67
|
// Each subcommand owns its own `--instance <url>` flag; a program-level
|
|
64
68
|
// duplicate shadowed the subcommand value on commander v12, causing
|
|
65
69
|
// `commonly login --instance …` to silently fall back to the default URL.
|
package/src/lib/task.js
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The task board from the CLI: identity, the request layer, and one-line
|
|
3
|
+
* rendering. The routes are the ones the commonly_* task tools call
|
|
4
|
+
* (backend/routes/tasksApi.ts), so the CLI and the MCP cannot disagree about
|
|
5
|
+
* what a write means.
|
|
6
|
+
*
|
|
7
|
+
* Identity is an explicit token and nothing else: --token-file, then
|
|
8
|
+
* COMMONLY_TOKEN_FILE, then COMMONLY_AGENT_TOKEN. The saved login and
|
|
9
|
+
* COMMONLY_TOKEN are never read. A write made under an operator's saved login
|
|
10
|
+
* lands on the row under that operator's name.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { readFileSync } from 'fs';
|
|
14
|
+
import { DEFAULT_URL, resolveInstance } from './config.js';
|
|
15
|
+
import { extractToken, resolveTokenFilePath, TOKEN_FILE_ENV } from './inbox.js';
|
|
16
|
+
|
|
17
|
+
export const API_URL_ENV = 'COMMONLY_API_URL';
|
|
18
|
+
export const TASK_TOKEN_ENV = 'COMMONLY_AGENT_TOKEN';
|
|
19
|
+
export const TASK_STATUSES = ['pending', 'claimed', 'done', 'blocked'];
|
|
20
|
+
// `update` writes only these. A claim writes the lease, which /claim owns, and a
|
|
21
|
+
// close stamps completedAt and the PR, which PATCH does not.
|
|
22
|
+
export const UPDATE_STATUSES = ['pending', 'blocked'];
|
|
23
|
+
export const TASK_PRIORITIES = ['P0', 'P1', 'P2', 'P3'];
|
|
24
|
+
const REQUEST_TIMEOUT_MS = 30_000;
|
|
25
|
+
|
|
26
|
+
/** A refusal made before any request: a bad flag, or no identity to send. */
|
|
27
|
+
export class TaskRefusal extends Error {}
|
|
28
|
+
|
|
29
|
+
/** A failed call. `status` is 0 when the server was never reached. */
|
|
30
|
+
export class TaskRequestError extends Error {
|
|
31
|
+
constructor(message, { status = 0, code = null, body = null } = {}) {
|
|
32
|
+
super(message);
|
|
33
|
+
this.name = 'TaskRequestError';
|
|
34
|
+
this.status = status;
|
|
35
|
+
this.code = code;
|
|
36
|
+
this.body = body;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export const requireText = (value, flag) => {
|
|
41
|
+
const text = typeof value === 'string' ? value.trim() : '';
|
|
42
|
+
if (!text) throw new TaskRefusal(`${flag} needs a value.`);
|
|
43
|
+
return text;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export const checkPriority = (value) => {
|
|
47
|
+
const priority = String(value).trim().toUpperCase();
|
|
48
|
+
if (!TASK_PRIORITIES.includes(priority)) {
|
|
49
|
+
throw new TaskRefusal(`--priority must be ${TASK_PRIORITIES.join(', ')} (got '${value}').`);
|
|
50
|
+
}
|
|
51
|
+
return priority;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export const checkStatusFilter = (value) => {
|
|
55
|
+
const statuses = String(value).split(',').map((s) => s.trim()).filter(Boolean);
|
|
56
|
+
if (statuses.length === 0 || statuses.some((s) => !TASK_STATUSES.includes(s))) {
|
|
57
|
+
throw new TaskRefusal(`--status takes ${TASK_STATUSES.join(', ')}, comma-separated (got '${value}').`);
|
|
58
|
+
}
|
|
59
|
+
return statuses.join(',');
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export const checkUpdateStatus = (value) => {
|
|
63
|
+
const status = String(value).trim().toLowerCase();
|
|
64
|
+
if (status === 'claimed') {
|
|
65
|
+
throw new TaskRefusal('--status claimed is refused: `commonly task claim` writes the lease, and a bare status does not.');
|
|
66
|
+
}
|
|
67
|
+
if (status === 'done') {
|
|
68
|
+
throw new TaskRefusal('--status done is refused: `commonly task close` stamps completedAt and the PR, and PATCH does not.');
|
|
69
|
+
}
|
|
70
|
+
if (!UPDATE_STATUSES.includes(status)) {
|
|
71
|
+
throw new TaskRefusal(`--status must be ${UPDATE_STATUSES.join(' or ')} (got '${value}').`);
|
|
72
|
+
}
|
|
73
|
+
return status;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/** TASK-669, task-669 and 669 name one row. Anything else is refused before a request. */
|
|
77
|
+
export const checkTaskId = (value) => {
|
|
78
|
+
const match = String(value ?? '').trim().match(/^(?:task-)?(\d+)$/i);
|
|
79
|
+
if (!match) throw new TaskRefusal(`task id must look like TASK-123 (got '${value ?? ''}').`);
|
|
80
|
+
return `TASK-${String(Number(match[1])).padStart(3, '0')}`;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Resolve the token. A token file that exists but holds nothing is refused by
|
|
85
|
+
* its path. The message never suggests `commonly login`, because pointing back
|
|
86
|
+
* at the saved login is the thing this rule exists to prevent.
|
|
87
|
+
*/
|
|
88
|
+
export const resolveTaskToken = (opts = {}, env = process.env) => {
|
|
89
|
+
const tokenFile = resolveTokenFilePath(opts, env);
|
|
90
|
+
if (tokenFile) {
|
|
91
|
+
let raw;
|
|
92
|
+
try {
|
|
93
|
+
raw = readFileSync(tokenFile, 'utf8');
|
|
94
|
+
} catch (err) {
|
|
95
|
+
throw new TaskRefusal(`cannot read the token file ${tokenFile} (${err?.code || err?.message}).`);
|
|
96
|
+
}
|
|
97
|
+
const token = extractToken(raw);
|
|
98
|
+
if (!token) throw new TaskRefusal(`no token in ${tokenFile}. Expected a raw token, or JSON with a token field.`);
|
|
99
|
+
return token;
|
|
100
|
+
}
|
|
101
|
+
const fromEnv = extractToken(env[TASK_TOKEN_ENV]);
|
|
102
|
+
if (fromEnv) return fromEnv;
|
|
103
|
+
throw new TaskRefusal(`no token. Pass --token-file <path>, or set ${TOKEN_FILE_ENV} or ${TASK_TOKEN_ENV}.`);
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* --instance takes a URL or a saved key, and an unknown key is refused. Falling
|
|
108
|
+
* back to the default host would send this token to an instance it was not
|
|
109
|
+
* minted on.
|
|
110
|
+
*/
|
|
111
|
+
export const resolveTaskBaseUrl = (opts = {}, env = process.env) => {
|
|
112
|
+
if (opts.instance) {
|
|
113
|
+
const inst = resolveInstance(opts.instance);
|
|
114
|
+
if (!inst?.url) {
|
|
115
|
+
throw new TaskRefusal(`unknown instance '${opts.instance}'. Pass its URL, or a saved instance key.`);
|
|
116
|
+
}
|
|
117
|
+
return inst.url.replace(/\/$/, '');
|
|
118
|
+
}
|
|
119
|
+
const fromEnv = String(env[API_URL_ENV] || '').trim().replace(/\/$/, '');
|
|
120
|
+
return fromEnv || DEFAULT_URL;
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
/** `/api/v1/tasks/<pod>` for the board, and `…/<taskId>` for one row. */
|
|
124
|
+
export const taskPath = (podId, taskId = null) => [
|
|
125
|
+
'/api/v1/tasks',
|
|
126
|
+
encodeURIComponent(podId),
|
|
127
|
+
...(taskId === null ? [] : [encodeURIComponent(taskId)]),
|
|
128
|
+
].join('/');
|
|
129
|
+
|
|
130
|
+
const messageFor = (status, body, text) => {
|
|
131
|
+
if (body && typeof body.error === 'string' && body.error) return body.error;
|
|
132
|
+
if (body && typeof body.message === 'string' && body.message) return body.message;
|
|
133
|
+
const trimmed = String(text || '').trim();
|
|
134
|
+
if (!trimmed) return `HTTP ${status}`;
|
|
135
|
+
if (/^<(?:!doctype|html)/i.test(trimmed)) {
|
|
136
|
+
const title = trimmed.match(/<title[^>]*>([\s\S]*?)<\/title>/i)?.[1];
|
|
137
|
+
return title ? title.replace(/\s+/g, ' ').trim() : 'HTML error page';
|
|
138
|
+
}
|
|
139
|
+
return trimmed.replace(/\s+/g, ' ').slice(0, 200);
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* One attempt per call. The writes here are POSTs, and a retried create or note
|
|
144
|
+
* lands twice, so a failure is reported and the caller decides what to do next.
|
|
145
|
+
*/
|
|
146
|
+
export const createTaskClient = ({ baseUrl, token, fetchImpl = fetch }) => {
|
|
147
|
+
const send = async (method, path, body) => {
|
|
148
|
+
let res;
|
|
149
|
+
try {
|
|
150
|
+
res = await fetchImpl(`${baseUrl}${path}`, {
|
|
151
|
+
method,
|
|
152
|
+
headers: {
|
|
153
|
+
Authorization: `Bearer ${token}`,
|
|
154
|
+
'Content-Type': 'application/json',
|
|
155
|
+
},
|
|
156
|
+
...(body === undefined ? {} : { body: JSON.stringify(body) }),
|
|
157
|
+
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
|
158
|
+
});
|
|
159
|
+
} catch (err) {
|
|
160
|
+
throw new TaskRequestError(`could not reach ${baseUrl} (${err?.message || err}).`);
|
|
161
|
+
}
|
|
162
|
+
const text = await res.text();
|
|
163
|
+
let parsed = null;
|
|
164
|
+
try {
|
|
165
|
+
parsed = text ? JSON.parse(text) : null;
|
|
166
|
+
} catch {
|
|
167
|
+
parsed = null;
|
|
168
|
+
}
|
|
169
|
+
if (!res.ok) {
|
|
170
|
+
throw new TaskRequestError(messageFor(res.status, parsed, text), {
|
|
171
|
+
status: res.status,
|
|
172
|
+
code: parsed?.code || null,
|
|
173
|
+
body: parsed,
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
return parsed;
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
const get = (path, params = {}) => {
|
|
180
|
+
const query = new URLSearchParams(
|
|
181
|
+
Object.entries(params).filter(([, value]) => value !== undefined && value !== ''),
|
|
182
|
+
).toString();
|
|
183
|
+
return send('GET', query ? `${path}?${query}` : path);
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
return {
|
|
187
|
+
get,
|
|
188
|
+
post: (path, body = {}) => send('POST', path, body),
|
|
189
|
+
patch: (path, body = {}) => send('PATCH', path, body),
|
|
190
|
+
};
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
const clockOf = (iso) => {
|
|
194
|
+
if (!iso) return null;
|
|
195
|
+
const at = new Date(iso);
|
|
196
|
+
return Number.isNaN(at.getTime()) ? null : `${at.toISOString().slice(11, 16)}Z`;
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
/** One row, as a human reads it. A claimed row also says whether its lease holds. */
|
|
200
|
+
export const formatTaskLine = (task) => {
|
|
201
|
+
const line = [task.taskId, task.status, task.priority || '-', task.assignee || '-', task.title].join(' ');
|
|
202
|
+
if (task.status !== 'claimed') return line;
|
|
203
|
+
const at = clockOf(task.claimExpiresAt);
|
|
204
|
+
if (!at) return line;
|
|
205
|
+
const lapsed = new Date(task.claimExpiresAt).getTime() < Date.now();
|
|
206
|
+
return `${line} (${lapsed ? 'lease lapsed' : 'held until'} ${at})`;
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* One line for a failed call. The unseen-messages hold and a lease conflict get
|
|
211
|
+
* their own wording, because a seat acts on each differently. Everything else is
|
|
212
|
+
* the server's own text.
|
|
213
|
+
*/
|
|
214
|
+
export const describeFailure = (err) => {
|
|
215
|
+
if (!(err instanceof TaskRequestError)) return `Failed: ${err?.message || err}`;
|
|
216
|
+
if (err.status === 0) return `Failed: ${err.message}`;
|
|
217
|
+
const body = err.body || {};
|
|
218
|
+
let line;
|
|
219
|
+
if (err.code === 'unseen_messages') {
|
|
220
|
+
const count = Number(body.unseenCount) || 0;
|
|
221
|
+
line = `Failed (HTTP ${err.status}): ${count} unseen message${count === 1 ? '' : 's'} in this pod. `
|
|
222
|
+
+ 'Read them and retry, or pass --continue-anyway to go ahead.';
|
|
223
|
+
} else {
|
|
224
|
+
const detail = [
|
|
225
|
+
body.claimedBy ? `held by ${body.claimedBy}` : null,
|
|
226
|
+
clockOf(body.claimExpiresAt) ? `lease until ${clockOf(body.claimExpiresAt)}` : null,
|
|
227
|
+
].filter(Boolean).join(', ');
|
|
228
|
+
line = `Failed (HTTP ${err.status}): ${err.message}${detail ? ` (${detail})` : ''}`;
|
|
229
|
+
if (err.status === 401) {
|
|
230
|
+
line += '. Check the agent token: --token-file, COMMONLY_TOKEN_FILE or COMMONLY_AGENT_TOKEN.';
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
return line.replace(/\s+/g, ' ');
|
|
234
|
+
};
|