@ductape/cli 0.6.0 → 0.6.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 +0 -43
- package/dist/commands/apps.d.ts +0 -10
- package/dist/commands/apps.js +5 -24
- package/dist/commands/deploy-builds.js +14 -1
- package/dist/commands/deploy-remote.js +13 -0
- package/dist/commands/deploy-worker.js +5 -0
- package/dist/commands/secrets.d.ts +1 -0
- package/dist/commands/secrets.js +9 -1
- package/dist/commands/work-hooks.d.ts +64 -0
- package/dist/commands/work-hooks.js +335 -0
- package/dist/commands/work-test.d.ts +37 -0
- package/dist/commands/work-test.js +71 -0
- package/dist/commands/work.d.ts +19 -0
- package/dist/commands/work.js +520 -0
- package/dist/index.js +8 -29
- package/dist/lib/api-client.js +2 -4
- package/dist/lib/cloud-connection.js +0 -3
- package/dist/lib/forms/enums.d.ts +1 -1
- package/dist/lib/forms/enums.js +1 -1
- package/dist/lib/forms/prompt.d.ts +1 -0
- package/dist/lib/forms/prompt.js +17 -27
- package/dist/lib/forms/schemas.js +2 -2
- package/dist/lib/forms/types.d.ts +2 -1
- package/dist/lib/platform-api.d.ts +0 -1
- package/dist/lib/platform-api.js +9 -6
- package/dist/lib/read-body.d.ts +2 -0
- package/dist/lib/read-body.js +1 -1
- package/dist/lib/test-summary.d.ts +12 -0
- package/dist/lib/test-summary.js +50 -0
- package/dist/lib/work-delivery.d.ts +37 -0
- package/dist/lib/work-delivery.js +61 -0
- package/dist/lib/work-paths.d.ts +6 -0
- package/dist/lib/work-paths.js +16 -0
- package/dist/lib/work.d.ts +68 -0
- package/dist/lib/work.js +217 -0
- package/package.json +2 -3
package/dist/lib/work.js
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import readline from 'node:readline';
|
|
6
|
+
import { ApiClient } from './api-client.js';
|
|
7
|
+
import { GLOBAL_DIR, findProjectConfig } from './config.js';
|
|
8
|
+
import { requireWorkspaceContext } from './workspace-context.js';
|
|
9
|
+
/**
|
|
10
|
+
* Client for the work tracker (/tickets/v1/work). Every call carries the signed-in person's credentials and the
|
|
11
|
+
* active workspace. Agent calls also carry a session id: the agent's session is remembered per repo and client in
|
|
12
|
+
* ~/.ductape/work-sessions.json, so a later command in the same repo picks it up.
|
|
13
|
+
*/
|
|
14
|
+
export const AGENT_CLIENTS = ['claude-code', 'codex', 'cursor', 'other'];
|
|
15
|
+
const SESSIONS_PATH = path.join(GLOBAL_DIR, 'work-sessions.json');
|
|
16
|
+
function git(args, cwd) {
|
|
17
|
+
try {
|
|
18
|
+
return execFileSync('git', args, { cwd, stdio: ['ignore', 'pipe', 'ignore'], encoding: 'utf8' }).trim() || undefined;
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/** Normalises a git remote to host/owner/name, so SSH and HTTPS remotes of one repo match. */
|
|
25
|
+
export function normaliseRemote(remote) {
|
|
26
|
+
let r = remote.trim().replace(/\.git$/, '').replace(/\/$/, '');
|
|
27
|
+
const scp = /^[\w.-]+@([\w.-]+):(.+)$/.exec(r);
|
|
28
|
+
if (scp)
|
|
29
|
+
r = `${scp[1]}/${scp[2]}`;
|
|
30
|
+
r = r.replace(/^[a-z+]+:\/\//i, '').replace(/^[^@/]+@/, '');
|
|
31
|
+
return r.toLowerCase();
|
|
32
|
+
}
|
|
33
|
+
/** The repo the command runs in: its origin remote (or folder name), branch and root. */
|
|
34
|
+
export function detectRepo(cwd = process.cwd()) {
|
|
35
|
+
const root = git(['rev-parse', '--show-toplevel'], cwd);
|
|
36
|
+
if (!root)
|
|
37
|
+
return { repo: '' };
|
|
38
|
+
const remote = git(['config', '--get', 'remote.origin.url'], root);
|
|
39
|
+
const branch = git(['rev-parse', '--abbrev-ref', 'HEAD'], root);
|
|
40
|
+
return { repo: remote ? normaliseRemote(remote) : `local/${path.basename(root)}`, branch: branch === 'HEAD' ? undefined : branch, root };
|
|
41
|
+
}
|
|
42
|
+
/** The agent client this command speaks for: --client, then DUCTAPE_AGENT_CLIENT. None means a person. */
|
|
43
|
+
export function resolveClient(flag) {
|
|
44
|
+
const value = (flag ?? process.env.DUCTAPE_AGENT_CLIENT ?? '').trim().toLowerCase();
|
|
45
|
+
if (!value)
|
|
46
|
+
return undefined;
|
|
47
|
+
if (!AGENT_CLIENTS.includes(value)) {
|
|
48
|
+
throw new Error(`Unknown agent client "${value}". Use one of: ${AGENT_CLIENTS.join(', ')}.`);
|
|
49
|
+
}
|
|
50
|
+
return value;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Sessions are remembered per workspace, repo and client, and, when the agent's own conversation id is known (hooks
|
|
54
|
+
* receive it), per conversation too: two windows of one agent in one repo then keep separate work sessions.
|
|
55
|
+
*/
|
|
56
|
+
const sessionKey = (workspaceId, repo, client, conversation) => `${workspaceId}|${repo}|${client}${conversation ? `|${conversation}` : ''}`;
|
|
57
|
+
const KEEP_MS = 14 * 24 * 60 * 60 * 1000;
|
|
58
|
+
function readSessions() {
|
|
59
|
+
try {
|
|
60
|
+
return JSON.parse(fs.readFileSync(SESSIONS_PATH, 'utf8'));
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
return {};
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
function writeSessions(map) {
|
|
67
|
+
// Conversations come and go; entries older than two weeks are dropped so the file does not grow forever.
|
|
68
|
+
const cutoff = Date.now() - KEEP_MS;
|
|
69
|
+
const kept = Object.fromEntries(Object.entries(map).filter(([, v]) => !(Date.parse(v.started_at) < cutoff)));
|
|
70
|
+
fs.mkdirSync(GLOBAL_DIR, { recursive: true });
|
|
71
|
+
fs.writeFileSync(SESSIONS_PATH, JSON.stringify(kept, null, 2), { mode: 0o600 });
|
|
72
|
+
}
|
|
73
|
+
export function rememberSession(workspaceId, repo, client, session, conversation) {
|
|
74
|
+
const map = readSessions();
|
|
75
|
+
const entry = { session_id: session.id, agent_id: session.agent_id, workspace_id: workspaceId, started_at: session.started_at };
|
|
76
|
+
map[sessionKey(workspaceId, repo, client)] = entry;
|
|
77
|
+
if (conversation)
|
|
78
|
+
map[sessionKey(workspaceId, repo, client, conversation)] = entry;
|
|
79
|
+
writeSessions(map);
|
|
80
|
+
}
|
|
81
|
+
export function forgetSession(workspaceId, repo, client, conversation) {
|
|
82
|
+
const map = readSessions();
|
|
83
|
+
const base = sessionKey(workspaceId, repo, client);
|
|
84
|
+
if (conversation) {
|
|
85
|
+
const own = map[sessionKey(workspaceId, repo, client, conversation)]?.session_id;
|
|
86
|
+
delete map[sessionKey(workspaceId, repo, client, conversation)];
|
|
87
|
+
// The repo-wide entry goes too only when it is this conversation's session.
|
|
88
|
+
if (own && map[base]?.session_id === own)
|
|
89
|
+
delete map[base];
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
delete map[base];
|
|
93
|
+
}
|
|
94
|
+
writeSessions(map);
|
|
95
|
+
}
|
|
96
|
+
/** The remembered session. With a conversation id, only that conversation's: never another window's. */
|
|
97
|
+
export function savedSession(workspaceId, repo, client, conversation) {
|
|
98
|
+
return readSessions()[sessionKey(workspaceId, repo, client, conversation)]?.session_id;
|
|
99
|
+
}
|
|
100
|
+
export class WorkClient {
|
|
101
|
+
ctx;
|
|
102
|
+
repo;
|
|
103
|
+
client;
|
|
104
|
+
api;
|
|
105
|
+
session;
|
|
106
|
+
conversation;
|
|
107
|
+
constructor(opts = {}, ctx = requireWorkspaceContext()) {
|
|
108
|
+
this.ctx = ctx;
|
|
109
|
+
this.repo = detectRepo();
|
|
110
|
+
this.client = opts.client;
|
|
111
|
+
this.api = new ApiClient(ctx.apiUrl, ctx.credentials.auth_token);
|
|
112
|
+
this.conversation = opts.conversation;
|
|
113
|
+
this.session = opts.session ?? process.env.DUCTAPE_WORK_SESSION ?? (opts.client ? savedSession(ctx.workspaceId, this.repo.repo, opts.client, opts.conversation) : undefined);
|
|
114
|
+
}
|
|
115
|
+
/** The product of the linked Ductape project (ductape/config.json), used when a command names none. */
|
|
116
|
+
get linkedProduct() {
|
|
117
|
+
try {
|
|
118
|
+
return findProjectConfig()?.config?.product_tag || undefined;
|
|
119
|
+
}
|
|
120
|
+
catch {
|
|
121
|
+
return undefined;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
/** The product a command is for: the one it names, or the linked project's. */
|
|
125
|
+
product(flag) {
|
|
126
|
+
return flag || this.linkedProduct;
|
|
127
|
+
}
|
|
128
|
+
get sessionId() {
|
|
129
|
+
return this.session;
|
|
130
|
+
}
|
|
131
|
+
auth() {
|
|
132
|
+
return { user_id: this.ctx.credentials.user_id, public_key: this.ctx.credentials.public_key, workspace_id: this.ctx.workspaceId };
|
|
133
|
+
}
|
|
134
|
+
headers(withSession) {
|
|
135
|
+
return withSession && this.session ? { 'x-ductape-session': this.session } : {};
|
|
136
|
+
}
|
|
137
|
+
async get(p, query = {}, withSession = true) {
|
|
138
|
+
const params = new URLSearchParams();
|
|
139
|
+
for (const [k, v] of Object.entries({ ...query, ...this.auth() }))
|
|
140
|
+
if (v !== undefined && v !== null && v !== '')
|
|
141
|
+
params.set(k, String(v));
|
|
142
|
+
return this.api.request('GET', `/tickets/v1/work${p}?${params}`, undefined, this.headers(withSession));
|
|
143
|
+
}
|
|
144
|
+
async send(method, p, body = {}, withSession = true) {
|
|
145
|
+
const call = () => this.api.request(method, `/tickets/v1/work${p}`, { ...body, ...this.auth() }, { ...this.headers(withSession), ...(this.grant ? { 'x-ductape-grant': this.grant } : {}) });
|
|
146
|
+
try {
|
|
147
|
+
return await call();
|
|
148
|
+
}
|
|
149
|
+
catch (error) {
|
|
150
|
+
if (!isStepUpRequired(error) || this.grant)
|
|
151
|
+
throw error;
|
|
152
|
+
// A change only a person may make: prove it is the person with the code Ductape emails them, then try again.
|
|
153
|
+
// The grant lives only in this process. An agent's shell is not interactive and cannot read the email.
|
|
154
|
+
this.grant = await this.stepUp();
|
|
155
|
+
return call();
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
grant;
|
|
159
|
+
async stepUp() {
|
|
160
|
+
if (!process.stdin.isTTY || !process.stderr.isTTY) {
|
|
161
|
+
throw new Error('This change needs a code from your email, so an agent using your sign-in cannot make it. Run the command yourself in your own terminal, or make the change in the Workbench.');
|
|
162
|
+
}
|
|
163
|
+
const started = await this.api.request('POST', '/tickets/v1/work/step-up/start', this.auth(), {});
|
|
164
|
+
if (started?.required === false)
|
|
165
|
+
throw new Error('The server did not ask for a code but refused the change; try again.');
|
|
166
|
+
const code = await promptHidden(`A code was sent to ${started.sent_to}. Enter it to confirm it is you: `);
|
|
167
|
+
const verified = await this.api.request('POST', '/tickets/v1/work/step-up/verify', { ...this.auth(), code: code.trim() }, {});
|
|
168
|
+
process.stderr.write('Confirmed for 15 minutes in this command.\n');
|
|
169
|
+
return verified.grant;
|
|
170
|
+
}
|
|
171
|
+
/** Starts an agent session for this repo and remembers it for later commands. */
|
|
172
|
+
async startSession(input) {
|
|
173
|
+
const res = await this.send('POST', '/sessions', {
|
|
174
|
+
client: input.client,
|
|
175
|
+
// How the agent reached Ductape, and the name its client gave (set by the MCP server from its handshake).
|
|
176
|
+
via: input.via ?? process.env.DUCTAPE_AGENT_VIA ?? 'cli',
|
|
177
|
+
client_name: process.env.DUCTAPE_AGENT_CLIENT_NAME || undefined,
|
|
178
|
+
name: input.name,
|
|
179
|
+
product: this.product(input.product),
|
|
180
|
+
machine: input.machine ?? os.hostname(),
|
|
181
|
+
repo: this.repo.repo,
|
|
182
|
+
branch: this.repo.branch,
|
|
183
|
+
}, false);
|
|
184
|
+
rememberSession(this.ctx.workspaceId, this.repo.repo, input.client, res.session, this.conversation);
|
|
185
|
+
return res;
|
|
186
|
+
}
|
|
187
|
+
async endSession(summary) {
|
|
188
|
+
if (!this.session)
|
|
189
|
+
throw new Error('No agent session to end. Pass --session, or --client for the session started in this repo.');
|
|
190
|
+
const res = await this.send('POST', '/sessions/end', { summary });
|
|
191
|
+
if (this.client)
|
|
192
|
+
forgetSession(this.ctx.workspaceId, this.repo.repo, this.client, this.conversation);
|
|
193
|
+
return res;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
/** Whether a refusal asks for a step-up code. */
|
|
197
|
+
export function isStepUpRequired(error) {
|
|
198
|
+
const e = error;
|
|
199
|
+
return /STEP_UP_REQUIRED/.test(String(e?.message ?? '')) || e?.details?.code === 'STEP_UP_REQUIRED';
|
|
200
|
+
}
|
|
201
|
+
/** Reads one line from the terminal. */
|
|
202
|
+
function promptHidden(question) {
|
|
203
|
+
return new Promise((resolve) => {
|
|
204
|
+
const rl = readline.createInterface({ input: process.stdin, output: process.stderr, terminal: true });
|
|
205
|
+
rl.question(question, (answer) => {
|
|
206
|
+
rl.close();
|
|
207
|
+
resolve(answer);
|
|
208
|
+
});
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
/** Splits a comma-separated flag into a list; undefined stays undefined. */
|
|
212
|
+
export const list = (value) => {
|
|
213
|
+
if (value === undefined)
|
|
214
|
+
return undefined;
|
|
215
|
+
const parts = (Array.isArray(value) ? value : [value]).flatMap((v) => v.split(','));
|
|
216
|
+
return parts.map((s) => s.trim()).filter(Boolean);
|
|
217
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ductape/cli",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.2",
|
|
4
4
|
"description": "Ductape CLI — local platform, login, link projects, and manage resources via the proxy (Workbench-compatible)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -18,8 +18,7 @@
|
|
|
18
18
|
"build": "tsc",
|
|
19
19
|
"dev": "tsx src/index.ts",
|
|
20
20
|
"test": "npm run build && node --import tsx --test test/**/*.test.ts",
|
|
21
|
-
"prepublishOnly": "npm run build"
|
|
22
|
-
"test:ui:e2e": "node --import tsx test/e2e/ui-slice.ts"
|
|
21
|
+
"prepublishOnly": "npm run build"
|
|
23
22
|
},
|
|
24
23
|
"keywords": [
|
|
25
24
|
"ductape",
|