sekura 0.4.0 → 0.4.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 +8 -5
- package/dist/auth.js +137 -19
- package/dist/catalogue.js +53 -9
- package/dist/commands/billing.js +25 -3
- package/dist/commands/doctor.js +37 -0
- package/dist/commands/findings.js +38 -0
- package/dist/commands/init.js +20 -9
- package/dist/commands/login.js +2 -2
- package/dist/commands/pay.js +5 -2
- package/dist/commands/plans.js +263 -0
- package/dist/commands/report.js +33 -0
- package/dist/commands/repos.js +55 -0
- package/dist/commands/scan.js +100 -0
- package/dist/commands/scans.js +32 -0
- package/dist/commands/shared.js +236 -0
- package/dist/commands/status.js +14 -2
- package/dist/commands/terms.js +78 -0
- package/dist/commands/uninstall.js +38 -12
- package/dist/device.js +6 -6
- package/dist/errors.js +17 -0
- package/dist/exit.js +46 -0
- package/dist/flow.js +218 -0
- package/dist/guide.js +26 -11
- package/dist/open.js +68 -0
- package/dist/origin.js +66 -0
- package/dist/payment.js +32 -0
- package/dist/plan.js +15 -0
- package/dist/session.js +182 -0
- package/dist/state.js +72 -0
- package/dist/steps.js +330 -0
- package/dist/terms.js +81 -0
- package/dist/verify.js +5 -1
- package/dist/writers.js +76 -10
- package/package.json +2 -2
- package/skills/sekura/SKILL.md +33 -21
package/dist/flow.js
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { confirm, select } from '@inquirer/prompts';
|
|
2
|
+
import { randomBytes } from 'node:crypto';
|
|
3
|
+
import { readKey } from './auth.js';
|
|
4
|
+
import { call, getPlans, paymentRequiredOf, tools } from './api.js';
|
|
5
|
+
import { EXIT, Exit, SIGN_IN_HINT } from './exit.js';
|
|
6
|
+
import { openLink } from './open.js';
|
|
7
|
+
import { openableLink, paymentState } from './payment.js';
|
|
8
|
+
import { dropQuote, findQuote, saveQuote } from './state.js';
|
|
9
|
+
import { dot, interactive, printJson, say, tick, waitFor } from './ui.js';
|
|
10
|
+
export function requireKey() {
|
|
11
|
+
const key = readKey();
|
|
12
|
+
if (!key)
|
|
13
|
+
throw new Exit(EXIT.notSignedIn, `Not signed in here: ${SIGN_IN_HINT} first.`);
|
|
14
|
+
return key;
|
|
15
|
+
}
|
|
16
|
+
const usd = (cents) => `$${(cents / 100).toFixed(cents % 100 ? 2 : 0)}`;
|
|
17
|
+
export const amountOf = (q) => typeof q.amountCents === 'number' && Number.isFinite(q.amountCents) ? q.amountCents : null;
|
|
18
|
+
export const needsAgreement = (q) => !!q.disclosure || (amountOf(q) ?? 0) > 0;
|
|
19
|
+
export function maxPriceCents(raw) {
|
|
20
|
+
if (raw === undefined || raw === null || raw === '')
|
|
21
|
+
return null;
|
|
22
|
+
const n = Number(String(raw).replace(/^\$/, ''));
|
|
23
|
+
if (!Number.isFinite(n) || n < 0)
|
|
24
|
+
throw new Exit(EXIT.error, `--max-price takes an amount in US dollars, such as 10; ${raw} is not one.`);
|
|
25
|
+
return Math.round(n * 100);
|
|
26
|
+
}
|
|
27
|
+
const payable = (o) => !!o.payment && (o.method === 'saved_card' || o.method === 'subscription');
|
|
28
|
+
function paymentFromFlag(q, card) {
|
|
29
|
+
const options = q.paymentOptions ?? [];
|
|
30
|
+
const subscription = options.find((o) => o.method === 'subscription' && o.payment);
|
|
31
|
+
if (subscription?.payment)
|
|
32
|
+
return subscription.payment;
|
|
33
|
+
if (!card)
|
|
34
|
+
return undefined;
|
|
35
|
+
if (card === 'default') {
|
|
36
|
+
const d = options.find((o) => o.method === 'saved_card' && o.isDefault) ?? options.find((o) => o.method === 'saved_card');
|
|
37
|
+
if (!d?.payment)
|
|
38
|
+
throw new Exit(EXIT.error, 'There is no saved card to charge; leave --card out to pay on a Stripe Checkout page.');
|
|
39
|
+
return d.payment;
|
|
40
|
+
}
|
|
41
|
+
const named = options.find((o) => o.method === 'saved_card' && (o.card === card || o.last4 === card));
|
|
42
|
+
if (!named?.payment)
|
|
43
|
+
throw new Exit(EXIT.error, `${card} is not one of the saved cards this quote lists.`);
|
|
44
|
+
return named.payment;
|
|
45
|
+
}
|
|
46
|
+
async function askPayment(q, flagged) {
|
|
47
|
+
const fromFlag = paymentFromFlag(q, flagged);
|
|
48
|
+
if (fromFlag)
|
|
49
|
+
return fromFlag;
|
|
50
|
+
const options = (q.paymentOptions ?? []).filter((o) => payable(o) || o.method === 'checkout');
|
|
51
|
+
if (!options.some(payable))
|
|
52
|
+
return undefined;
|
|
53
|
+
const picked = await select({
|
|
54
|
+
message: 'How do you want to pay?',
|
|
55
|
+
choices: options.map((o) => ({
|
|
56
|
+
name: payable(o) ? `${o.masked ?? o.label ?? 'the saved card'}${o.isDefault ? ' (default)' : ''}` : 'Pay on a Stripe Checkout page in your browser',
|
|
57
|
+
value: payable(o) ? o.payment : null,
|
|
58
|
+
})),
|
|
59
|
+
});
|
|
60
|
+
return picked ?? undefined;
|
|
61
|
+
}
|
|
62
|
+
const withPayment = (args, payment) => (payment ? { ...args, payment } : args);
|
|
63
|
+
function cacheQuote(req, q) {
|
|
64
|
+
return saveQuote({
|
|
65
|
+
quoteId: typeof q.quoteId === 'string' && q.quoteId.length <= 64 ? q.quoteId : `q_${randomBytes(9).toString('base64url')}`,
|
|
66
|
+
op: req.op,
|
|
67
|
+
args: req.args,
|
|
68
|
+
confirmOperation: q.confirmOperation,
|
|
69
|
+
confirmArguments: q.confirmArguments,
|
|
70
|
+
amountCents: amountOf(q),
|
|
71
|
+
price: typeof q.price === 'string' ? q.price : null,
|
|
72
|
+
text: q.text,
|
|
73
|
+
...(typeof q.quoteExpiresAt === 'string' ? { expiresAt: q.quoteExpiresAt } : {}),
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
export async function confirmQuote(req) {
|
|
77
|
+
const flags = req.flags ?? {};
|
|
78
|
+
const again = req.command ? `${req.command} --quote` : 'the same command with --quote';
|
|
79
|
+
if (flags.confirm) {
|
|
80
|
+
const found = findQuote(flags.confirm);
|
|
81
|
+
if (found.status === 'unknown')
|
|
82
|
+
throw new Exit(EXIT.error, `There is no quote ${flags.confirm} saved here. Run ${again} to get one.`);
|
|
83
|
+
if (found.status === 'expired')
|
|
84
|
+
throw new Exit(EXIT.error, `Quote ${flags.confirm} expired at ${found.quote.expiresAt}; a quote holds for 15 minutes. Run ${again} for a new one. Nothing was started.`);
|
|
85
|
+
if (found.quote.op !== req.op)
|
|
86
|
+
throw new Exit(EXIT.error, `Quote ${flags.confirm} is for ${found.quote.op}, not ${req.op}. Nothing was started.`);
|
|
87
|
+
const max = maxPriceCents(flags.maxPrice);
|
|
88
|
+
if (max !== null && (found.quote.amountCents ?? 0) > max)
|
|
89
|
+
throw new Exit(EXIT.declined, `Quote ${flags.confirm} is ${usd(found.quote.amountCents ?? 0)}, above --max-price ${usd(max)}. Nothing was started.`);
|
|
90
|
+
return { kind: 'agreed', how: 'confirm-flag', confirmOperation: found.quote.confirmOperation, confirmArguments: found.quote.confirmArguments, amountCents: found.quote.amountCents, quoteId: found.quote.quoteId, quote: null };
|
|
91
|
+
}
|
|
92
|
+
const { data: q } = await req.fetch();
|
|
93
|
+
const amount = amountOf(q);
|
|
94
|
+
const show = req.sayQuote ?? ((text) => { say(); for (const l of text.split('\n'))
|
|
95
|
+
say(` ${l}`); say(); });
|
|
96
|
+
if (flags.quote) {
|
|
97
|
+
const payment = paymentFromFlag(q, flags.card);
|
|
98
|
+
const cached = cacheQuote(req, { ...q, confirmArguments: withPayment(q.confirmArguments, payment) });
|
|
99
|
+
if (flags.json)
|
|
100
|
+
printJson({ quoteId: cached.quoteId, op: cached.op, confirmOperation: cached.confirmOperation, confirmArguments: cached.confirmArguments, amountCents: cached.amountCents, price: cached.price, expiresAt: cached.expiresAt, text: cached.text });
|
|
101
|
+
else {
|
|
102
|
+
show(q.text);
|
|
103
|
+
say(`${dot} Quote ${cached.quoteId}, good until ${cached.expiresAt}. Nothing was started or charged.`);
|
|
104
|
+
say(` To start exactly this: ${req.command ?? 'the same command'} --confirm ${cached.quoteId}`);
|
|
105
|
+
}
|
|
106
|
+
return { kind: 'quoted', cached };
|
|
107
|
+
}
|
|
108
|
+
show(q.text);
|
|
109
|
+
if (flags.yes) {
|
|
110
|
+
const max = maxPriceCents(flags.maxPrice);
|
|
111
|
+
if (needsAgreement(q)) {
|
|
112
|
+
if (max === null)
|
|
113
|
+
throw new Exit(EXIT.agreementRequired, `--yes alone does not start anything paid or authorized. Read the quote above, then run ${again} and --confirm <quoteId>, or add --max-price <usd> to --yes. Nothing was started.`);
|
|
114
|
+
if ((amount ?? 0) > max)
|
|
115
|
+
throw new Exit(EXIT.declined, `The price is ${usd(amount ?? 0)}, above --max-price ${usd(max)}. Nothing was started.`);
|
|
116
|
+
}
|
|
117
|
+
return { kind: 'agreed', how: 'yes-flag', confirmOperation: q.confirmOperation, confirmArguments: withPayment(q.confirmArguments, paymentFromFlag(q, flags.card)), amountCents: amount, quoteId: null, quote: q };
|
|
118
|
+
}
|
|
119
|
+
if (!interactive())
|
|
120
|
+
throw new Exit(EXIT.agreementRequired, `There is no terminal to agree in, so nothing was started. Run ${again}, show the quote to the person, then --confirm <quoteId>.`);
|
|
121
|
+
const payment = await askPayment(q, flags.card);
|
|
122
|
+
const agreed = await confirm({ message: req.question ?? 'Do you agree?', default: false });
|
|
123
|
+
if (!agreed)
|
|
124
|
+
throw new Exit(EXIT.declined, 'Nothing was started or charged.');
|
|
125
|
+
return { kind: 'agreed', how: 'asked', confirmOperation: q.confirmOperation, confirmArguments: withPayment(q.confirmArguments, payment), amountCents: amount, quoteId: null, quote: q };
|
|
126
|
+
}
|
|
127
|
+
export async function planById(key, planId) {
|
|
128
|
+
return (await getPlans(key)).plans.find((p) => p.id === planId);
|
|
129
|
+
}
|
|
130
|
+
export async function waitForPayment(key, planId, start, opts = {}) {
|
|
131
|
+
let last = start;
|
|
132
|
+
let state = { kind: 'waiting' };
|
|
133
|
+
let cancelledSeen = 0;
|
|
134
|
+
const result = await waitFor(opts.message ?? 'Waiting for the payment to complete in your browser (Enter to stop waiting)', async () => {
|
|
135
|
+
last = await planById(key, planId);
|
|
136
|
+
state = paymentState(start, last);
|
|
137
|
+
if (state.kind === 'cancelled')
|
|
138
|
+
return ++cancelledSeen >= 2;
|
|
139
|
+
cancelledSeen = 0;
|
|
140
|
+
return state.kind === 'started';
|
|
141
|
+
}, { ...(opts.timeoutMs ? { timeoutMs: opts.timeoutMs } : {}), ...(opts.intervalMs ? { intervalMs: opts.intervalMs } : {}) });
|
|
142
|
+
return { result, state: state, plan: last };
|
|
143
|
+
}
|
|
144
|
+
const sayCheckout = (opened, url) => {
|
|
145
|
+
if (opened.opened)
|
|
146
|
+
say(`${tick} Opened Stripe Checkout in your browser: ${url.href}`);
|
|
147
|
+
else
|
|
148
|
+
say(`${dot} Open this link in a browser to pay: ${url.href}`);
|
|
149
|
+
};
|
|
150
|
+
export async function startWithPayment(key, op, args, opts = {}) {
|
|
151
|
+
let reply;
|
|
152
|
+
try {
|
|
153
|
+
reply = await call(key, op, args);
|
|
154
|
+
}
|
|
155
|
+
catch (e) {
|
|
156
|
+
const due = paymentRequiredOf(e);
|
|
157
|
+
if (!due)
|
|
158
|
+
throw e;
|
|
159
|
+
if (opts.quoteId)
|
|
160
|
+
dropQuote(opts.quoteId);
|
|
161
|
+
const url = openableLink(due.checkoutUrl);
|
|
162
|
+
if (!url)
|
|
163
|
+
throw new Exit(EXIT.error, `Sekura answered with a payment link this CLI does not open: ${due.checkoutUrl}`);
|
|
164
|
+
const planId = typeof due.planId === 'string' && due.planId ? due.planId : null;
|
|
165
|
+
const opened = openLink(url.href, { browser: opts.browser });
|
|
166
|
+
if (!opened.opened && !interactive()) {
|
|
167
|
+
throw new Exit(EXIT.paymentNeedsBrowser, `Payment needs a browser. Open this link to pay; the scan starts once it is paid: ${url.href}${planId ? `\n To wait for it here: npx sekura@latest pay ${url.href} --plan ${planId} --no-open` : ''}`, { checkoutUrl: url.href, planId });
|
|
168
|
+
}
|
|
169
|
+
;
|
|
170
|
+
(opts.sayLink ?? sayCheckout)(opened, url);
|
|
171
|
+
if (!planId)
|
|
172
|
+
return { kind: 'payment-pending', why: 'no-plan', planId: null, checkoutUrl: url.href };
|
|
173
|
+
let start;
|
|
174
|
+
try {
|
|
175
|
+
start = await planById(key, planId);
|
|
176
|
+
}
|
|
177
|
+
catch {
|
|
178
|
+
start = undefined;
|
|
179
|
+
}
|
|
180
|
+
const w = await waitForPayment(key, planId, start, opts);
|
|
181
|
+
if (w.state.kind === 'started')
|
|
182
|
+
return { kind: 'paid-started', scanId: w.state.scanId, scanStatus: w.state.scanStatus, plan: w.plan, planId, checkoutUrl: url.href };
|
|
183
|
+
if (w.result === 'done' && w.state.kind === 'cancelled')
|
|
184
|
+
return { kind: 'payment-cancelled', plan: w.plan, planId, checkoutUrl: url.href };
|
|
185
|
+
if (w.state.kind === 'paid')
|
|
186
|
+
return { kind: 'paid', plan: w.plan, planId, checkoutUrl: url.href };
|
|
187
|
+
return { kind: 'payment-pending', why: w.result === 'skipped' ? 'skipped' : 'timeout', planId, checkoutUrl: url.href };
|
|
188
|
+
}
|
|
189
|
+
if (opts.quoteId)
|
|
190
|
+
dropQuote(opts.quoteId);
|
|
191
|
+
const { data } = reply;
|
|
192
|
+
return { kind: 'started', text: reply.text, data, scanId: typeof data.scanId === 'string' ? data.scanId : null, planId: typeof data.planId === 'string' ? data.planId : null };
|
|
193
|
+
}
|
|
194
|
+
export function exitCodeOfStart(o) {
|
|
195
|
+
switch (o.kind) {
|
|
196
|
+
case 'started':
|
|
197
|
+
case 'paid-started':
|
|
198
|
+
case 'paid': return EXIT.done;
|
|
199
|
+
case 'payment-cancelled': return EXIT.declined;
|
|
200
|
+
case 'payment-pending': return EXIT.stoppedWaiting;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
export const SCAN_TERMINAL = new Set(['done', 'failed', 'cancelled', 'expired']);
|
|
204
|
+
export const scanFinished = (status) => !!status && SCAN_TERMINAL.has(status);
|
|
205
|
+
export async function watchScan(key, scanId, opts = {}) {
|
|
206
|
+
let detail = null;
|
|
207
|
+
let seen = null;
|
|
208
|
+
const result = await waitFor(opts.message ?? `Scan ${scanId} is running (Enter stops watching; the scan carries on)`, async () => {
|
|
209
|
+
detail = (await tools.getScan(key, { scanId })).data;
|
|
210
|
+
const status = String(detail.scan?.status ?? '');
|
|
211
|
+
if (status && status !== seen) {
|
|
212
|
+
seen = status;
|
|
213
|
+
opts.onStatus?.(status, detail);
|
|
214
|
+
}
|
|
215
|
+
return scanFinished(status);
|
|
216
|
+
}, { timeoutMs: opts.timeoutMs ?? 60 * 60_000, intervalMs: opts.intervalMs ?? 5000 });
|
|
217
|
+
return { result, detail: detail, status: seen };
|
|
218
|
+
}
|
package/dist/guide.js
CHANGED
|
@@ -6,13 +6,14 @@
|
|
|
6
6
|
// rather than waiting for Enter.
|
|
7
7
|
import { confirm, select } from '@inquirer/prompts';
|
|
8
8
|
import { spawnSync } from 'node:child_process';
|
|
9
|
-
import { forgetKey, openBrowser, readKey, redact, saveKey, signIn } from './auth.js';
|
|
9
|
+
import { adoptSavedKey, forgetKey, openBrowser, readKey, redact, saveKey, signIn } from './auth.js';
|
|
10
10
|
import { getAccount, listRepositories, SignedOut } from './api.js';
|
|
11
|
-
import { carriesKey, nextStep, onPath, SERVER_NAME } from './catalogue.js';
|
|
11
|
+
import { carriesKey, HOST, nextStep, onPath, ORIGIN, SERVER_NAME } from './catalogue.js';
|
|
12
|
+
import { signedInByTool } from './writers.js';
|
|
12
13
|
import { currentGithubRepo } from './repo.js';
|
|
13
14
|
import { cross, dot, say, tick, waitFor } from './ui.js';
|
|
14
|
-
const START =
|
|
15
|
-
const DASHBOARD =
|
|
15
|
+
const START = `${ORIGIN}/app/start`;
|
|
16
|
+
const DASHBOARD = `${ORIGIN}/app`;
|
|
16
17
|
/**
|
|
17
18
|
* The key already here: SEKURA_API_KEY or the stored one, when Sekura still
|
|
18
19
|
* takes it. A revoked stored key is forgotten. Null when there is none.
|
|
@@ -24,6 +25,8 @@ export async function existingSession() {
|
|
|
24
25
|
return null;
|
|
25
26
|
try {
|
|
26
27
|
const account = await getAccount(existing);
|
|
28
|
+
if (!fromEnv)
|
|
29
|
+
adoptSavedKey();
|
|
27
30
|
say(`${tick} Signed in to Sekura as ${account.email ?? account.name ?? redact(existing)}${fromEnv ? ' (SEKURA_API_KEY)' : ''}`);
|
|
28
31
|
return { key: existing, account };
|
|
29
32
|
}
|
|
@@ -58,7 +61,7 @@ export async function signInHere(opts = {}) {
|
|
|
58
61
|
if (c.complete)
|
|
59
62
|
say(` Or open ${c.complete}`);
|
|
60
63
|
say();
|
|
61
|
-
say(
|
|
64
|
+
say(` Waiting for you to approve on ${HOST}…`);
|
|
62
65
|
},
|
|
63
66
|
});
|
|
64
67
|
let account = null;
|
|
@@ -85,7 +88,7 @@ export async function obtainKey(opts) {
|
|
|
85
88
|
const accountReady = (a) => a.termsAccepted !== false && a.emailVerified !== false;
|
|
86
89
|
/** Asks Sekura until the Terms are accepted and the email verified. */
|
|
87
90
|
export async function waitForAccount(s, wait = {}) {
|
|
88
|
-
const r = await waitFor(
|
|
91
|
+
const r = await waitFor(`Waiting for the Terms and your email on ${HOST}… press Enter to skip`, async () => {
|
|
89
92
|
const a = await getAccount(s.key);
|
|
90
93
|
s.account = a;
|
|
91
94
|
return accountReady(a);
|
|
@@ -105,8 +108,8 @@ export async function checkAccount(s, interactive, wait = {}) {
|
|
|
105
108
|
if (!todo.length)
|
|
106
109
|
return;
|
|
107
110
|
const page = a.links?.dashboard ?? DASHBOARD;
|
|
108
|
-
say(`${dot} Before a scan can start, ${todo.join(' and ')} on
|
|
109
|
-
if (!interactive || !(await confirm({ message:
|
|
111
|
+
say(`${dot} Before a scan can start, ${todo.join(' and ')} on ${HOST}: ${page}`);
|
|
112
|
+
if (!interactive || !(await confirm({ message: `Open ${HOST} to do that now?`, default: true })))
|
|
110
113
|
return;
|
|
111
114
|
openBrowser(page);
|
|
112
115
|
if (await waitForAccount(s, wait))
|
|
@@ -166,7 +169,7 @@ export async function pickRepository(s, cwd, interactive, wait = {}) {
|
|
|
166
169
|
const go = await select({
|
|
167
170
|
message: repos.length ? `Give Sekura access to ${here}?` : 'Install the Sekura GitHub App?',
|
|
168
171
|
choices: [
|
|
169
|
-
{ name:
|
|
172
|
+
{ name: `Yes, open ${HOST} (the GitHub App asks which repositories)`, value: true },
|
|
170
173
|
{ name: repos.length ? 'No, pick from the repositories Sekura can already scan' : 'Not now', value: false },
|
|
171
174
|
],
|
|
172
175
|
});
|
|
@@ -220,7 +223,10 @@ export function toolLogin(s) {
|
|
|
220
223
|
* Runs the tool's own sign-in for each written entry that could not carry
|
|
221
224
|
* the key. The browser is already signed in to sekura.ai, so each is one
|
|
222
225
|
* Approve. `ask: false` when the person already agreed to sign in (init's
|
|
223
|
-
* one question).
|
|
226
|
+
* one question). A tool that already shows itself signed in is left alone:
|
|
227
|
+
* each `mcp login` mints a new key on Sekura, and the one it replaces stays
|
|
228
|
+
* live, because the tool keeps it and the CLI never sees it. Returns the
|
|
229
|
+
* names of the tools signed in.
|
|
224
230
|
*/
|
|
225
231
|
export async function runToolLogins(steps, interactive, ask = true) {
|
|
226
232
|
const done = new Set();
|
|
@@ -230,7 +236,16 @@ export async function runToolLogins(steps, interactive, ask = true) {
|
|
|
230
236
|
const cmd = toolLogin(s);
|
|
231
237
|
if (!cmd || s.kind !== 'tool')
|
|
232
238
|
continue;
|
|
233
|
-
if (
|
|
239
|
+
if (signedInByTool(s.target)) {
|
|
240
|
+
done.add(s.tool.name);
|
|
241
|
+
say(`${tick} ${s.tool.name} is already signed in to Sekura, so no new key was made`);
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
244
|
+
// One line per question, the detail above it (see init.ts: a wrapped
|
|
245
|
+
// question leaves the cursor mid-word).
|
|
246
|
+
if (ask)
|
|
247
|
+
say(` ${s.tool.name} signs in with ${cmd}; you approve in the browser.`);
|
|
248
|
+
if (ask && !(await confirm({ message: `Sign ${s.tool.name} in now?`, default: true })))
|
|
234
249
|
continue;
|
|
235
250
|
if (!ask)
|
|
236
251
|
say(` ${s.tool.name}: running ${cmd}; approve in the browser`);
|
package/dist/open.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { spawn, spawnSync } from 'node:child_process';
|
|
2
|
+
import { platform } from 'node:os';
|
|
3
|
+
import { onPath } from './catalogue.js';
|
|
4
|
+
import { remoteSession } from './device.js';
|
|
5
|
+
/** Whether this machine has a browser to open. On Linux that takes a
|
|
6
|
+
* display and xdg-open; a bare server or container has neither, and the
|
|
7
|
+
* spawn's failure would only arrive after the URL was promised. */
|
|
8
|
+
export function hasBrowser(env = process.env) {
|
|
9
|
+
if (platform() === 'darwin' || platform() === 'win32')
|
|
10
|
+
return true;
|
|
11
|
+
return !!(env.DISPLAY || env.WAYLAND_DISPLAY) && onPath('xdg-open');
|
|
12
|
+
}
|
|
13
|
+
/** Opens a URL in the default browser; false when there is none to ask. */
|
|
14
|
+
export function openBrowser(url) {
|
|
15
|
+
if (!hasBrowser())
|
|
16
|
+
return false;
|
|
17
|
+
const [cmd, args] = platform() === 'darwin' ? ['open', [url]]
|
|
18
|
+
: platform() === 'win32' ? ['cmd', ['/c', 'start', '""', url.replace(/&/g, '^&')]]
|
|
19
|
+
: ['xdg-open', [url]];
|
|
20
|
+
try {
|
|
21
|
+
const child = spawn(cmd, args, { stdio: 'ignore', detached: true });
|
|
22
|
+
child.on('error', () => { });
|
|
23
|
+
child.unref();
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
export function canOpen(opts = {}) {
|
|
31
|
+
const env = opts.env ?? process.env;
|
|
32
|
+
if (opts.browser === false)
|
|
33
|
+
return 'disabled';
|
|
34
|
+
if (remoteSession(env))
|
|
35
|
+
return 'remote';
|
|
36
|
+
return hasBrowser(env) ? true : 'no-browser';
|
|
37
|
+
}
|
|
38
|
+
export function openLink(url, opts = {}) {
|
|
39
|
+
const env = opts.env ?? process.env;
|
|
40
|
+
if (opts.browser === false)
|
|
41
|
+
return { opened: false, url, why: 'disabled' };
|
|
42
|
+
const remote = remoteSession(env);
|
|
43
|
+
if (remote)
|
|
44
|
+
return { opened: false, url, why: 'remote', remote };
|
|
45
|
+
if (!hasBrowser(env))
|
|
46
|
+
return { opened: false, url, why: 'no-browser' };
|
|
47
|
+
return openBrowser(url) ? { opened: true, url } : { opened: false, url, why: 'failed' };
|
|
48
|
+
}
|
|
49
|
+
export function copyToClipboard(text, env = process.env) {
|
|
50
|
+
if (remoteSession(env))
|
|
51
|
+
return false;
|
|
52
|
+
const candidates = platform() === 'darwin' ? [['pbcopy', []]]
|
|
53
|
+
: platform() === 'win32' ? [['clip', []]]
|
|
54
|
+
: [['wl-copy', []], ['xclip', ['-selection', 'clipboard']], ['xsel', ['--clipboard', '--input']]];
|
|
55
|
+
for (const [bin, args] of candidates) {
|
|
56
|
+
if (platform() !== 'win32' && !onPath(bin))
|
|
57
|
+
continue;
|
|
58
|
+
try {
|
|
59
|
+
const r = spawnSync(bin, args, { input: text, stdio: ['pipe', 'ignore', 'ignore'], timeout: 3000 });
|
|
60
|
+
if (!r.error && r.status === 0)
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return false;
|
|
68
|
+
}
|
package/dist/origin.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
export const DEFAULT_ORIGIN = 'https://sekura.ai';
|
|
2
|
+
export const ENDPOINT_VAR = 'SEKURA_ENDPOINT';
|
|
3
|
+
export const ENDPOINT_FLAG = '--endpoint';
|
|
4
|
+
const LOOPBACK = new Set(['127.0.0.1', 'localhost', '[::1]']);
|
|
5
|
+
export class BadEndpoint extends Error {
|
|
6
|
+
constructor(value, why) {
|
|
7
|
+
super(`${value} is not a Sekura server address: ${why}. Use https://…, or http://127.0.0.1:<port> for a server on this machine.`);
|
|
8
|
+
this.name = 'BadEndpoint';
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export function parseOrigin(value) {
|
|
12
|
+
const raw = value.trim();
|
|
13
|
+
let u;
|
|
14
|
+
try {
|
|
15
|
+
u = new URL(raw);
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
throw new BadEndpoint(raw, 'it is not a URL');
|
|
19
|
+
}
|
|
20
|
+
if (u.protocol !== 'https:' && u.protocol !== 'http:')
|
|
21
|
+
throw new BadEndpoint(raw, `${u.protocol} is not https`);
|
|
22
|
+
if (u.protocol === 'http:' && !LOOPBACK.has(u.hostname))
|
|
23
|
+
throw new BadEndpoint(raw, 'http is allowed only for 127.0.0.1 or localhost');
|
|
24
|
+
if (u.username || u.password)
|
|
25
|
+
throw new BadEndpoint(raw, 'it carries a user name or password');
|
|
26
|
+
if (u.search || u.hash)
|
|
27
|
+
throw new BadEndpoint(raw, 'it carries a query or a fragment');
|
|
28
|
+
if (!['', '/', '/mcp', '/mcp/'].includes(u.pathname))
|
|
29
|
+
throw new BadEndpoint(raw, `the path ${u.pathname} is not / or /mcp`);
|
|
30
|
+
return u.origin;
|
|
31
|
+
}
|
|
32
|
+
export function endpointFromArgv(argv) {
|
|
33
|
+
for (let i = 0; i < argv.length; i++) {
|
|
34
|
+
const a = argv[i];
|
|
35
|
+
if (a === '--')
|
|
36
|
+
return null;
|
|
37
|
+
if (a === ENDPOINT_FLAG)
|
|
38
|
+
return argv[i + 1] ?? '';
|
|
39
|
+
if (a.startsWith(`${ENDPOINT_FLAG}=`))
|
|
40
|
+
return a.slice(ENDPOINT_FLAG.length + 1);
|
|
41
|
+
}
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
export const COMMANDS = [
|
|
45
|
+
'init', 'login', 'logout', 'status', 'account', 'doctor', 'terms', 'repos',
|
|
46
|
+
'scan', 'scans', 'findings', 'report', 'plans', 'plan', 'billing', 'pay', 'uninstall', 'help',
|
|
47
|
+
];
|
|
48
|
+
const VALUE_OPTIONS = new Set([
|
|
49
|
+
ENDPOINT_FLAG, '--tools', '--scope', '--plan', '--timeout', '--terms-version', '--text-hash',
|
|
50
|
+
'--confirm', '--max-price', '--card', '--output', '-o', '--limit', '--tier', '--weekday', '--every', '--time', '--tz',
|
|
51
|
+
]);
|
|
52
|
+
export function unknownCommand(argv) {
|
|
53
|
+
for (let i = 0; i < argv.length; i++) {
|
|
54
|
+
const a = argv[i];
|
|
55
|
+
if (a === '--')
|
|
56
|
+
return null;
|
|
57
|
+
if (VALUE_OPTIONS.has(a)) {
|
|
58
|
+
i++;
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if (a.startsWith('-'))
|
|
62
|
+
continue;
|
|
63
|
+
return COMMANDS.includes(a) ? null : a;
|
|
64
|
+
}
|
|
65
|
+
return null;
|
|
66
|
+
}
|
package/dist/payment.js
CHANGED
|
@@ -35,3 +35,35 @@ export function paymentState(start, now) {
|
|
|
35
35
|
}
|
|
36
36
|
export const cardLines = (b) => (b.cards ?? []).map((c) => `${c.masked}${c.isDefault ? ' (default)' : ''}`);
|
|
37
37
|
export const cardsFingerprint = (b) => (b.cards ?? []).map((c) => `${c.masked}:${c.isDefault ? 1 : 0}`).join('|');
|
|
38
|
+
// The names a user reads for a plan, keyed by the tier id the server sends,
|
|
39
|
+
// which stays as it is (26 November 2026 pricing). 'plus' is retired but
|
|
40
|
+
// still held by some accounts, so it reads as "Your monthly plan" rather than by
|
|
41
|
+
// its old name. A row's `plan` is 'always' for every monthly tier, so the
|
|
42
|
+
// tier is read first: by `plan` alone a More frequently plan reads as Weekly.
|
|
43
|
+
const TIER_NAMES = { once: 'One-time scan', free: 'One-time scan', always: 'Weekly', pro: 'More frequently', plus: 'Your monthly plan' };
|
|
44
|
+
/** A monthly plan's name, or undefined for a one-time scan. */
|
|
45
|
+
export function monthlyName(p) {
|
|
46
|
+
if (!p)
|
|
47
|
+
return undefined;
|
|
48
|
+
const tier = p.tier ?? p.plan;
|
|
49
|
+
if (tier && TIER_NAMES[tier] && tier !== 'once' && tier !== 'free')
|
|
50
|
+
return TIER_NAMES[tier];
|
|
51
|
+
return p.plan === 'always' ? 'Your monthly plan' : undefined;
|
|
52
|
+
}
|
|
53
|
+
const day = (iso) => {
|
|
54
|
+
const d = new Date(iso);
|
|
55
|
+
return Number.isNaN(d.getTime()) ? iso : d.toLocaleDateString('en-GB', { day: 'numeric', month: 'long', year: 'numeric', timeZone: 'UTC' });
|
|
56
|
+
};
|
|
57
|
+
/** One line per active plan: "owner/repo · Weekly · on demand". */
|
|
58
|
+
export function planLines(plans) {
|
|
59
|
+
return plans.filter((p) => p.status === 'active').map((p) => {
|
|
60
|
+
const repo = p.owner && p.name ? `${p.owner}/${p.name}` : p.id;
|
|
61
|
+
const monthly = monthlyName(p);
|
|
62
|
+
const parts = [repo, monthly ?? 'One-time scan'];
|
|
63
|
+
if (monthly && p.schedule?.description)
|
|
64
|
+
parts.push(p.schedule.description);
|
|
65
|
+
if (p.canceledAt)
|
|
66
|
+
parts.push(p.currentPeriodEnd ? `cancelled, ends ${day(p.currentPeriodEnd)}` : 'cancelled');
|
|
67
|
+
return parts.join(' · ');
|
|
68
|
+
});
|
|
69
|
+
}
|
package/dist/plan.js
CHANGED
|
@@ -51,6 +51,21 @@ export function describeStep(s, dryRun = false) {
|
|
|
51
51
|
case 'guidance': return `guidance: ${verb}add a marked Sekura section to ${s.path}`;
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
|
+
/** What uninstall will take out, as the person reads it before agreeing:
|
|
55
|
+
* the file or the tool's command, and each skill file by its full path.
|
|
56
|
+
* Until 0.4.2 this was describeStep cut at its first colon, which left the
|
|
57
|
+
* two skill files reading "skill" and "skill" with no path. */
|
|
58
|
+
export function removalLabel(s) {
|
|
59
|
+
switch (s.kind) {
|
|
60
|
+
case 'tool': {
|
|
61
|
+
const t = s.target;
|
|
62
|
+
const where = t.kind === 'command' ? `runs ${t.bin} ${t.remove.join(' ')}` : t.kind === 'link' ? 'its command is not on PATH, so nothing is removed here' : t.path;
|
|
63
|
+
return `${s.tool.name}: ${where}`;
|
|
64
|
+
}
|
|
65
|
+
case 'skill': return `skill: ${s.path}`;
|
|
66
|
+
case 'guidance': return `guidance: the marked Sekura section in ${s.path}`;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
54
69
|
export function applyStep(s) {
|
|
55
70
|
switch (s.kind) {
|
|
56
71
|
case 'tool': return apply(s.target);
|