promptlike 0.10.0 → 0.11.0
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/CHANGELOG.md +65 -0
- package/README.md +129 -4
- package/dist/commands/feedback.js +351 -0
- package/dist/commands/local.js +3 -1
- package/dist/commands/localHistory.js +1 -1
- package/dist/commands/save.js +72 -1
- package/dist/commands/search.js +4 -1
- package/dist/commands/suggest.js +134 -11
- package/dist/commands/whoami.js +80 -0
- package/dist/help.js +4 -2
- package/dist/index.js +5 -1
- package/dist/lib/accountSave.js +159 -0
- package/dist/lib/api-client.js +28 -0
- package/dist/lib/feedbackApi.js +134 -0
- package/dist/lib/feedbackHint.js +15 -0
- package/dist/lib/feedbackInput.js +182 -0
- package/dist/lib/feedbackQueue.js +219 -0
- package/dist/lib/feedbackSource.js +53 -0
- package/dist/lib/libraryCache.js +1 -0
- package/dist/lib/localStore.js +49 -10
- package/dist/lib/loopApi.js +38 -2
- package/dist/lib/promptResolver.js +51 -5
- package/dist/lib/rank.js +24 -6
- package/dist/lib/titleMatch.js +32 -0
- package/dist/program.js +147 -5
- package/package.json +1 -1
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* HTTP for `ple feedback`: POST /api/feedback (the session is optional) and the owner's
|
|
4
|
+
* GET /api/feedback · PATCH /api/feedback/{id}.
|
|
5
|
+
*
|
|
6
|
+
* Kept out of `api-client.ts` on purpose: a report must go out WITHOUT a session, a failed send is
|
|
7
|
+
* queued instead of thrown, and the status codes map to the feedback exit codes rather than to the
|
|
8
|
+
* generic 1/2. It reuses the same rules as the rest of the CLI: the base URL is validated before any
|
|
9
|
+
* request is built, every request has a timeout, redirects are never followed (a 3xx would replay the
|
|
10
|
+
* body, and the bearer token, at an address the server picked), and a token is only ever sent to the
|
|
11
|
+
* configured API origin. Nothing here throws; every outcome is a value.
|
|
12
|
+
*/
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.FEEDBACK_STATUSES = exports.OWNER_TIMEOUT_MS = exports.SUBMIT_TIMEOUT_MS = void 0;
|
|
15
|
+
exports.request = request;
|
|
16
|
+
exports.serverMessage = serverMessage;
|
|
17
|
+
exports.serverDetails = serverDetails;
|
|
18
|
+
exports.toSendResult = toSendResult;
|
|
19
|
+
exports.sendFeedback = sendFeedback;
|
|
20
|
+
const constants_1 = require("./constants");
|
|
21
|
+
const format_1 = require("./format");
|
|
22
|
+
exports.SUBMIT_TIMEOUT_MS = 15000;
|
|
23
|
+
exports.OWNER_TIMEOUT_MS = 15000;
|
|
24
|
+
exports.FEEDBACK_STATUSES = ['new', 'triaged', 'done', 'wontfix'];
|
|
25
|
+
const MAX_RETRY_AFTER_S = 86400;
|
|
26
|
+
function clampRetryAfter(value) {
|
|
27
|
+
const n = typeof value === 'number' ? value : typeof value === 'string' && /^\d{1,6}$/.test(value.trim()) ? Number(value) : NaN;
|
|
28
|
+
return Number.isFinite(n) && n >= 0 ? Math.min(Math.floor(n), MAX_RETRY_AFTER_S) : undefined;
|
|
29
|
+
}
|
|
30
|
+
function isTimeout(err, signal) {
|
|
31
|
+
if (signal.aborted)
|
|
32
|
+
return true;
|
|
33
|
+
const name = err?.name;
|
|
34
|
+
return name === 'TimeoutError' || name === 'AbortError';
|
|
35
|
+
}
|
|
36
|
+
async function request(input) {
|
|
37
|
+
if (constants_1.API_BASE_URL_ERROR)
|
|
38
|
+
return { kind: 'config', message: (0, format_1.safeText)(constants_1.API_BASE_URL_ERROR) };
|
|
39
|
+
const url = new URL(constants_1.API_BASE_URL + input.path);
|
|
40
|
+
for (const [k, v] of Object.entries(input.query ?? {})) {
|
|
41
|
+
if (v !== undefined && v !== '')
|
|
42
|
+
url.searchParams.set(k, String(v));
|
|
43
|
+
}
|
|
44
|
+
const headers = { Accept: 'application/json', 'X-CLI-Version': constants_1.CLI_VERSION };
|
|
45
|
+
if (input.body !== undefined)
|
|
46
|
+
headers['Content-Type'] = 'application/json';
|
|
47
|
+
if (input.token)
|
|
48
|
+
headers.Authorization = `Bearer ${input.token}`;
|
|
49
|
+
const signal = AbortSignal.timeout(Math.max(1, Math.floor(input.timeoutMs)));
|
|
50
|
+
let res;
|
|
51
|
+
try {
|
|
52
|
+
res = await (input.fetchImpl ?? fetch)(url.toString(), {
|
|
53
|
+
method: input.method,
|
|
54
|
+
headers,
|
|
55
|
+
body: input.body === undefined ? undefined : JSON.stringify(input.body),
|
|
56
|
+
signal,
|
|
57
|
+
redirect: 'manual',
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
catch (err) {
|
|
61
|
+
if (isTimeout(err, signal)) {
|
|
62
|
+
return { kind: 'unreachable', message: `No response from ${constants_1.API_BASE_URL} within ${Math.round(input.timeoutMs / 1000)}s.` };
|
|
63
|
+
}
|
|
64
|
+
return { kind: 'unreachable', message: (0, format_1.safeText)(`Cannot reach ${constants_1.API_BASE_URL}. ${err instanceof Error ? err.message : String(err)}`).slice(0, 300) };
|
|
65
|
+
}
|
|
66
|
+
if (res.status >= 300 && res.status < 400) {
|
|
67
|
+
return { kind: 'unreachable', message: `The server answered with a redirect (HTTP ${res.status}); the CLI does not follow redirects. Check PROMPTLIKE_API_URL (currently ${constants_1.API_BASE_URL}).` };
|
|
68
|
+
}
|
|
69
|
+
let body = {};
|
|
70
|
+
try {
|
|
71
|
+
const parsed = await res.json();
|
|
72
|
+
if (typeof parsed === 'object' && parsed !== null && !Array.isArray(parsed))
|
|
73
|
+
body = parsed;
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
// Not JSON, or cut off mid-body: the status alone decides.
|
|
77
|
+
}
|
|
78
|
+
const retryAfter = clampRetryAfter(body.retry_after) ?? clampRetryAfter(res.headers.get('retry-after') ?? undefined);
|
|
79
|
+
return { kind: 'response', status: res.status, body, retryAfter };
|
|
80
|
+
}
|
|
81
|
+
/** The server's `error` text, made terminal-safe and short; `fallback` when there is none. */
|
|
82
|
+
function serverMessage(body, fallback) {
|
|
83
|
+
const e = body.error;
|
|
84
|
+
return typeof e === 'string' && e.trim() ? (0, format_1.safeText)(e).trim().slice(0, 300) : fallback;
|
|
85
|
+
}
|
|
86
|
+
/** `details` of a 400, flattened to one safe line (it may be an array, an object or a string). */
|
|
87
|
+
function serverDetails(body) {
|
|
88
|
+
const d = body.details;
|
|
89
|
+
if (d === undefined || d === null)
|
|
90
|
+
return '';
|
|
91
|
+
let text;
|
|
92
|
+
try {
|
|
93
|
+
text = typeof d === 'string' ? d : JSON.stringify(d);
|
|
94
|
+
}
|
|
95
|
+
catch {
|
|
96
|
+
return '';
|
|
97
|
+
}
|
|
98
|
+
return (0, format_1.safeText)(text).slice(0, 300);
|
|
99
|
+
}
|
|
100
|
+
function toSendResult(out) {
|
|
101
|
+
if (out.kind === 'config')
|
|
102
|
+
return { kind: 'config', message: out.message };
|
|
103
|
+
if (out.kind === 'unreachable')
|
|
104
|
+
return { kind: 'unavailable', message: out.message };
|
|
105
|
+
const { status, body } = out;
|
|
106
|
+
if (status === 201 || status === 200) {
|
|
107
|
+
const id = typeof body.id === 'string' || typeof body.id === 'number' ? (0, format_1.safeText)(String(body.id)).slice(0, 64) : undefined;
|
|
108
|
+
return { kind: 'sent', id: id || undefined };
|
|
109
|
+
}
|
|
110
|
+
if (status === 400 || status === 413 || status === 422) {
|
|
111
|
+
const fallback = status === 413 ? 'The report is too large.' : 'The server rejected the report.';
|
|
112
|
+
const details = serverDetails(body);
|
|
113
|
+
return { kind: 'invalid', status, message: serverMessage(body, fallback) + (details ? ` (${details})` : '') };
|
|
114
|
+
}
|
|
115
|
+
if (status === 429)
|
|
116
|
+
return { kind: 'rate_limited', retryAfter: out.retryAfter };
|
|
117
|
+
if (status === 401 || status === 403)
|
|
118
|
+
return { kind: 'auth', status };
|
|
119
|
+
if (status >= 500 || status === 404 || status === 408) {
|
|
120
|
+
return { kind: 'unavailable', message: `The server answered HTTP ${status}.` };
|
|
121
|
+
}
|
|
122
|
+
return { kind: 'unexpected', status, message: serverMessage(body, `The server answered HTTP ${status}.`) };
|
|
123
|
+
}
|
|
124
|
+
/** POST one report. With a `token` the report is tied to the account; a 401/403 is returned for the caller to retry anonymously. */
|
|
125
|
+
async function sendFeedback(payload, opts = {}) {
|
|
126
|
+
return toSendResult(await request({
|
|
127
|
+
method: 'POST',
|
|
128
|
+
path: '/api/feedback',
|
|
129
|
+
body: payload,
|
|
130
|
+
token: opts.token,
|
|
131
|
+
timeoutMs: opts.timeoutMs ?? exports.SUBMIT_TIMEOUT_MS,
|
|
132
|
+
fetchImpl: opts.fetchImpl,
|
|
133
|
+
}));
|
|
134
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* The one-line pointer to `ple feedback`, shown when `ple search` or `ple get` comes up empty or fails.
|
|
4
|
+
*
|
|
5
|
+
* Always stderr (stdout stays the prompt text / the result list for pipes and agents), never part of a
|
|
6
|
+
* `--json` answer, and never repeated: one line per command run.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.FEEDBACK_HINT = void 0;
|
|
10
|
+
exports.printFeedbackHint = printFeedbackHint;
|
|
11
|
+
const format_1 = require("./format");
|
|
12
|
+
exports.FEEDBACK_HINT = 'Missing something? ple feedback --type missing "..."';
|
|
13
|
+
function printFeedbackHint() {
|
|
14
|
+
console.error(format_1.c.muted(exports.FEEDBACK_HINT));
|
|
15
|
+
}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Input rules for `ple feedback` — pure, no network, no disk.
|
|
4
|
+
*
|
|
5
|
+
* A report is a short note about the tool, never a vehicle for data. What is checked here:
|
|
6
|
+
* - sizes (they mirror the server: message 1..2000, prompt reference ≤200, agent name ≤64);
|
|
7
|
+
* - the message is one piece of plain text: terminal escapes and control characters are removed;
|
|
8
|
+
* - `--prompt` is a TITLE or an id. Anything that looks like prompt text (a line break, more than
|
|
9
|
+
* 200 characters) is refused, so a prompt body cannot ride along by accident;
|
|
10
|
+
* - the same secret patterns `ple import --scan` uses (see secretGuard.ts) REFUSE the report and
|
|
11
|
+
* name the kind, never the value. Refusing beats masking: a half-hidden key is still a leaked key.
|
|
12
|
+
*
|
|
13
|
+
* The context block is built from constants (version, platform, Node version, the command's NAME and
|
|
14
|
+
* the NAMES of the flags that were used). No argument value, no message, no path ever goes into it.
|
|
15
|
+
*/
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.MAX_CONTEXT_VALUE = exports.MAX_EMAIL = exports.MAX_AGENT_NAME = exports.MAX_PROMPT_REF = exports.MAX_MESSAGE = exports.DEFAULT_FEEDBACK_TYPE = exports.FEEDBACK_TYPES = void 0;
|
|
18
|
+
exports.scanForSecret = scanForSecret;
|
|
19
|
+
exports.cleanMessage = cleanMessage;
|
|
20
|
+
exports.cleanLine = cleanLine;
|
|
21
|
+
exports.isFeedbackType = isFeedbackType;
|
|
22
|
+
exports.sanitizeAgentName = sanitizeAgentName;
|
|
23
|
+
exports.isPlausibleEmail = isPlausibleEmail;
|
|
24
|
+
exports.osLabel = osLabel;
|
|
25
|
+
exports.buildContext = buildContext;
|
|
26
|
+
exports.checkFeedbackFields = checkFeedbackFields;
|
|
27
|
+
exports.parseStoredPayload = parseStoredPayload;
|
|
28
|
+
const format_1 = require("./format");
|
|
29
|
+
const secretGuard_1 = require("./secretGuard");
|
|
30
|
+
exports.FEEDBACK_TYPES = ['bug', 'missing', 'idea', 'prompt'];
|
|
31
|
+
exports.DEFAULT_FEEDBACK_TYPE = 'idea';
|
|
32
|
+
exports.MAX_MESSAGE = 2000;
|
|
33
|
+
exports.MAX_PROMPT_REF = 200;
|
|
34
|
+
exports.MAX_AGENT_NAME = 64;
|
|
35
|
+
exports.MAX_EMAIL = 254;
|
|
36
|
+
/** Every context value is a short string. */
|
|
37
|
+
exports.MAX_CONTEXT_VALUE = 64;
|
|
38
|
+
// Zero-width / formatting characters never change what a key looks like to a reader, but one in the middle
|
|
39
|
+
// of a token defeats a regex. Removed for MATCHING only (the text that is sent is not touched by this).
|
|
40
|
+
const INVISIBLE = /[\p{Cf}\u2028\u2029\u034F\u115F\u1160\u17B4\u17B5\u180B-\u180F\u3164\uFE00-\uFE0F\uFFA0]/gu;
|
|
41
|
+
/** Kind of secret found in `text` (never the value), or null. NFKC + invisible characters removed first. */
|
|
42
|
+
function scanForSecret(text) {
|
|
43
|
+
const scanned = text.normalize('NFKC').replace(INVISIBLE, '');
|
|
44
|
+
return (0, secretGuard_1.findSecret)(scanned) ?? (0, secretGuard_1.findSecret)(text);
|
|
45
|
+
}
|
|
46
|
+
/** Plain multi-line text: escapes and control characters out, line endings unified, ends trimmed. */
|
|
47
|
+
function cleanMessage(raw) {
|
|
48
|
+
return (0, format_1.safeText)(raw.replace(/\r\n?/g, '\n').replace(/[\u2028\u2029]/g, '\n'), true).trim();
|
|
49
|
+
}
|
|
50
|
+
/** One line: every run of whitespace/control characters becomes a single space. */
|
|
51
|
+
function cleanLine(raw) {
|
|
52
|
+
return (0, format_1.safeText)(raw.replace(/[\r\n\t\u2028\u2029]+/g, ' ')).replace(/\s{2,}/g, ' ').trim();
|
|
53
|
+
}
|
|
54
|
+
function isFeedbackType(value) {
|
|
55
|
+
return exports.FEEDBACK_TYPES.includes(value);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Agent names are labels: lowercase `a-z 0-9 . _ -`, at most 64. Returns '' when nothing is left, so the caller
|
|
59
|
+
* can tell "no name" from a name.
|
|
60
|
+
*/
|
|
61
|
+
function sanitizeAgentName(raw) {
|
|
62
|
+
return raw
|
|
63
|
+
.normalize('NFKC')
|
|
64
|
+
.toLowerCase()
|
|
65
|
+
.replace(INVISIBLE, '')
|
|
66
|
+
.replace(/[^a-z0-9._-]+/g, '-')
|
|
67
|
+
.replace(/-{2,}/g, '-')
|
|
68
|
+
.replace(/^[-._]+|[-._]+$/g, '')
|
|
69
|
+
.slice(0, exports.MAX_AGENT_NAME);
|
|
70
|
+
}
|
|
71
|
+
const EMAIL_RE = /^[^\s@<>()[\],;:"\\]{1,64}@[^\s@<>()[\],;:"\\]+\.[^\s@<>()[\],;:"\\]{2,}$/;
|
|
72
|
+
function isPlausibleEmail(value) {
|
|
73
|
+
return value.length <= exports.MAX_EMAIL && EMAIL_RE.test(value);
|
|
74
|
+
}
|
|
75
|
+
/** `os` for the context: platform and CPU architecture only (`darwin-arm64`). */
|
|
76
|
+
function osLabel(platform = process.platform, arch = process.arch) {
|
|
77
|
+
return `${platform}-${arch}`.slice(0, exports.MAX_CONTEXT_VALUE);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* The context block. `flagNames` are long option names that were passed (`--type`, `--prompt`); values and the
|
|
81
|
+
* message are never part of it. Names outside `--[a-z-]` are dropped, so nothing else can slip in through here.
|
|
82
|
+
*/
|
|
83
|
+
function buildContext(opts) {
|
|
84
|
+
const flags = (opts.flagNames ?? []).filter((f) => /^--[a-z][a-z-]{0,30}$/.test(f));
|
|
85
|
+
const command = [opts.commandName, ...flags].join(' ').slice(0, exports.MAX_CONTEXT_VALUE);
|
|
86
|
+
return {
|
|
87
|
+
cli_version: opts.cliVersion.slice(0, exports.MAX_CONTEXT_VALUE),
|
|
88
|
+
os: osLabel(opts.platform, opts.arch),
|
|
89
|
+
node: (opts.nodeVersion ?? process.versions.node).slice(0, exports.MAX_CONTEXT_VALUE),
|
|
90
|
+
command,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
/** Validate and normalise what the user typed. The error text says what to change and never echoes a secret. */
|
|
94
|
+
function checkFeedbackFields(input) {
|
|
95
|
+
const typeRaw = input.type === undefined ? exports.DEFAULT_FEEDBACK_TYPE : input.type.trim().toLowerCase();
|
|
96
|
+
if (!isFeedbackType(typeRaw)) {
|
|
97
|
+
return { ok: false, error: `--type must be one of: ${exports.FEEDBACK_TYPES.join(', ')} (got '${cleanLine(input.type ?? '').slice(0, 20)}').` };
|
|
98
|
+
}
|
|
99
|
+
const message = cleanMessage(input.message);
|
|
100
|
+
if (!message)
|
|
101
|
+
return { ok: false, error: 'The message is empty. Usage: ple feedback [--type bug|missing|idea|prompt] "<message>"' };
|
|
102
|
+
if (message.length > exports.MAX_MESSAGE) {
|
|
103
|
+
return { ok: false, error: `The message is too long (${message.length} characters, max ${exports.MAX_MESSAGE}). One short, factual sentence is enough.` };
|
|
104
|
+
}
|
|
105
|
+
const messageSecret = scanForSecret(input.message);
|
|
106
|
+
if (messageSecret) {
|
|
107
|
+
return {
|
|
108
|
+
ok: false,
|
|
109
|
+
error: `The message contains something that looks like a secret (${messageSecret}). Nothing was sent. Remove it and try again — feedback never needs secrets.`,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
const value = { type: typeRaw, message };
|
|
113
|
+
if (input.prompt !== undefined) {
|
|
114
|
+
if (/[\r\n\u2028\u2029]/.test(input.prompt)) {
|
|
115
|
+
return { ok: false, error: '--prompt takes a prompt title or id on one line, not prompt text. The body of a prompt is never sent.' };
|
|
116
|
+
}
|
|
117
|
+
const ref = cleanLine(input.prompt);
|
|
118
|
+
if (ref.length > exports.MAX_PROMPT_REF) {
|
|
119
|
+
return { ok: false, error: `--prompt is too long (max ${exports.MAX_PROMPT_REF} characters). Give the prompt's title or id, never its text.` };
|
|
120
|
+
}
|
|
121
|
+
const refSecret = scanForSecret(input.prompt);
|
|
122
|
+
if (refSecret)
|
|
123
|
+
return { ok: false, error: `--prompt contains something that looks like a secret (${refSecret}). Nothing was sent.` };
|
|
124
|
+
if (ref)
|
|
125
|
+
value.prompt_ref = ref;
|
|
126
|
+
}
|
|
127
|
+
if (input.email !== undefined) {
|
|
128
|
+
const email = input.email.trim();
|
|
129
|
+
if (!isPlausibleEmail(email))
|
|
130
|
+
return { ok: false, error: '--email is not a valid email address.' };
|
|
131
|
+
value.email = email;
|
|
132
|
+
}
|
|
133
|
+
return { ok: true, value };
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Re-validate a payload read back from disk (the queue file). Anything off — wrong shape, over a limit,
|
|
137
|
+
* a secret that was added by hand — returns null and the entry is dropped.
|
|
138
|
+
*/
|
|
139
|
+
function parseStoredPayload(raw) {
|
|
140
|
+
if (typeof raw !== 'object' || raw === null)
|
|
141
|
+
return null;
|
|
142
|
+
const o = raw;
|
|
143
|
+
if (typeof o.type !== 'string' || !isFeedbackType(o.type))
|
|
144
|
+
return null;
|
|
145
|
+
if (typeof o.message !== 'string' || o.message.length < 1 || o.message.length > exports.MAX_MESSAGE)
|
|
146
|
+
return null;
|
|
147
|
+
if (scanForSecret(o.message))
|
|
148
|
+
return null;
|
|
149
|
+
if (o.source !== 'human' && o.source !== 'agent')
|
|
150
|
+
return null;
|
|
151
|
+
if (o.client !== 'cli')
|
|
152
|
+
return null;
|
|
153
|
+
const ctx = o.context;
|
|
154
|
+
if (typeof ctx !== 'object' || ctx === null)
|
|
155
|
+
return null;
|
|
156
|
+
const short = (v) => typeof v === 'string' && v.length <= exports.MAX_CONTEXT_VALUE;
|
|
157
|
+
if (!short(ctx.cli_version) || !short(ctx.os) || !short(ctx.node) || !short(ctx.command))
|
|
158
|
+
return null;
|
|
159
|
+
const out = {
|
|
160
|
+
type: o.type,
|
|
161
|
+
message: o.message,
|
|
162
|
+
source: o.source,
|
|
163
|
+
client: 'cli',
|
|
164
|
+
context: { cli_version: ctx.cli_version, os: ctx.os, node: ctx.node, command: ctx.command },
|
|
165
|
+
};
|
|
166
|
+
if (o.prompt_ref !== undefined) {
|
|
167
|
+
if (typeof o.prompt_ref !== 'string' || o.prompt_ref.length > exports.MAX_PROMPT_REF || /[\r\n]/.test(o.prompt_ref))
|
|
168
|
+
return null;
|
|
169
|
+
out.prompt_ref = o.prompt_ref;
|
|
170
|
+
}
|
|
171
|
+
if (o.email !== undefined) {
|
|
172
|
+
if (typeof o.email !== 'string' || !isPlausibleEmail(o.email))
|
|
173
|
+
return null;
|
|
174
|
+
out.email = o.email;
|
|
175
|
+
}
|
|
176
|
+
if (o.agent_name !== undefined) {
|
|
177
|
+
if (typeof o.agent_name !== 'string' || o.agent_name.length > exports.MAX_AGENT_NAME)
|
|
178
|
+
return null;
|
|
179
|
+
out.agent_name = o.agent_name;
|
|
180
|
+
}
|
|
181
|
+
return out;
|
|
182
|
+
}
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Offline queue for `ple feedback` — `~/.promptlike/feedback/queue.json`.
|
|
4
|
+
*
|
|
5
|
+
* A report that could not be delivered (no network, timeout, server error) is written here, so an agent
|
|
6
|
+
* or a person on a bad connection does not lose it. The file follows the other local stores: mode 0600
|
|
7
|
+
* inside a 0700 folder, written atomically and never through a symbolic link (see fsSafe.ts), under
|
|
8
|
+
* `PROMPTLIKE_HOME` when that is set.
|
|
9
|
+
*
|
|
10
|
+
* Limits: at most {@link QUEUE_MAX} reports (the oldest one is dropped to make room) and at most
|
|
11
|
+
* {@link MAX_QUEUE_FILE_BYTES} on disk. A file that is unreadable, too big or not ours reads as empty and
|
|
12
|
+
* is replaced by the next write; a single entry that is off (wrong shape, over a limit) is dropped on its own.
|
|
13
|
+
*
|
|
14
|
+
* Sending later: after ANY other `ple` command, at most once every {@link RETRY_INTERVAL_MS}, within a
|
|
15
|
+
* {@link FLUSH_TIMEOUT_MS} budget, silently — no output, every error swallowed (the same idea as the update
|
|
16
|
+
* notice: stamp before the attempt, so a slow or failed one is not retried by the next command in a burst).
|
|
17
|
+
* It sends only what `ple feedback` queued, never anything else. Off with `PLE_FEEDBACK_QUEUE=off`.
|
|
18
|
+
*/
|
|
19
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
20
|
+
if (k2 === undefined) k2 = k;
|
|
21
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
22
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
23
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
24
|
+
}
|
|
25
|
+
Object.defineProperty(o, k2, desc);
|
|
26
|
+
}) : (function(o, m, k, k2) {
|
|
27
|
+
if (k2 === undefined) k2 = k;
|
|
28
|
+
o[k2] = m[k];
|
|
29
|
+
}));
|
|
30
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
31
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
32
|
+
}) : function(o, v) {
|
|
33
|
+
o["default"] = v;
|
|
34
|
+
});
|
|
35
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
36
|
+
var ownKeys = function(o) {
|
|
37
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
38
|
+
var ar = [];
|
|
39
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
40
|
+
return ar;
|
|
41
|
+
};
|
|
42
|
+
return ownKeys(o);
|
|
43
|
+
};
|
|
44
|
+
return function (mod) {
|
|
45
|
+
if (mod && mod.__esModule) return mod;
|
|
46
|
+
var result = {};
|
|
47
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
48
|
+
__setModuleDefault(result, mod);
|
|
49
|
+
return result;
|
|
50
|
+
};
|
|
51
|
+
})();
|
|
52
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
53
|
+
exports.FLUSH_TIMEOUT_MS = exports.RETRY_INTERVAL_MS = exports.MAX_QUEUE_FILE_BYTES = exports.QUEUE_MAX = void 0;
|
|
54
|
+
exports.queueFile = queueFile;
|
|
55
|
+
exports.readQueue = readQueue;
|
|
56
|
+
exports.writeQueue = writeQueue;
|
|
57
|
+
exports.enqueue = enqueue;
|
|
58
|
+
exports.queueEnabled = queueEnabled;
|
|
59
|
+
exports.isFlushDue = isFlushDue;
|
|
60
|
+
exports.flushQueue = flushQueue;
|
|
61
|
+
exports.flushAfterCommand = flushAfterCommand;
|
|
62
|
+
const crypto_1 = require("crypto");
|
|
63
|
+
const path = __importStar(require("path"));
|
|
64
|
+
const config_1 = require("./config");
|
|
65
|
+
const feedbackInput_1 = require("./feedbackInput");
|
|
66
|
+
const feedbackApi_1 = require("./feedbackApi");
|
|
67
|
+
const fsSafe_1 = require("./fsSafe");
|
|
68
|
+
const localStore_1 = require("./localStore");
|
|
69
|
+
const updateCheck_1 = require("./updateCheck");
|
|
70
|
+
exports.QUEUE_MAX = 50;
|
|
71
|
+
exports.MAX_QUEUE_FILE_BYTES = 512 * 1024;
|
|
72
|
+
exports.RETRY_INTERVAL_MS = 15 * 60 * 1000;
|
|
73
|
+
exports.FLUSH_TIMEOUT_MS = 2000;
|
|
74
|
+
function queueFile() {
|
|
75
|
+
return path.join((0, localStore_1.localRoot)(), 'feedback', 'queue.json');
|
|
76
|
+
}
|
|
77
|
+
const isTime = (v) => typeof v === 'number' && Number.isFinite(v) && v >= 0;
|
|
78
|
+
/** The queue, or an empty one. Never throws. */
|
|
79
|
+
function readQueue() {
|
|
80
|
+
const empty = { version: 1, last_attempt_at: 0, items: [] };
|
|
81
|
+
const raw = (0, fsSafe_1.readPrivateFile)(queueFile());
|
|
82
|
+
if (raw === null || raw.length > exports.MAX_QUEUE_FILE_BYTES)
|
|
83
|
+
return empty;
|
|
84
|
+
try {
|
|
85
|
+
const o = JSON.parse(raw);
|
|
86
|
+
if (typeof o !== 'object' || o === null || !Array.isArray(o.items))
|
|
87
|
+
return empty;
|
|
88
|
+
const items = [];
|
|
89
|
+
for (const entry of o.items) {
|
|
90
|
+
if (typeof entry !== 'object' || entry === null)
|
|
91
|
+
continue;
|
|
92
|
+
const e = entry;
|
|
93
|
+
const payload = (0, feedbackInput_1.parseStoredPayload)(e.payload);
|
|
94
|
+
if (!payload)
|
|
95
|
+
continue;
|
|
96
|
+
items.push({
|
|
97
|
+
qid: typeof e.qid === 'string' && /^[0-9a-f]{8,32}$/.test(e.qid) ? e.qid : newQid(),
|
|
98
|
+
queued_at: typeof e.queued_at === 'string' ? e.queued_at.slice(0, 40) : '',
|
|
99
|
+
payload,
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
return { version: 1, last_attempt_at: isTime(o.last_attempt_at) ? o.last_attempt_at : 0, items: items.slice(-exports.QUEUE_MAX) };
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
return empty;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
function newQid() {
|
|
109
|
+
return (0, crypto_1.randomBytes)(8).toString('hex');
|
|
110
|
+
}
|
|
111
|
+
/** Write the queue. Throws when the disk refuses — callers decide what that means. */
|
|
112
|
+
function writeQueue(state) {
|
|
113
|
+
const trimmed = { ...state, items: state.items.slice(-exports.QUEUE_MAX) };
|
|
114
|
+
(0, fsSafe_1.writePrivateFile)(queueFile(), JSON.stringify(trimmed) + '\n');
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Add one report. The retry timer starts now (`last_attempt_at`), so the very next command does not try
|
|
118
|
+
* again against a network that just failed. Throws when the file cannot be written.
|
|
119
|
+
*/
|
|
120
|
+
function enqueue(payload, now = Date.now()) {
|
|
121
|
+
const state = readQueue();
|
|
122
|
+
const droppedOldest = state.items.length >= exports.QUEUE_MAX;
|
|
123
|
+
const items = [...state.items, { qid: newQid(), queued_at: new Date(now).toISOString(), payload }].slice(-exports.QUEUE_MAX);
|
|
124
|
+
writeQueue({ version: 1, last_attempt_at: now, items });
|
|
125
|
+
return { size: items.length, droppedOldest };
|
|
126
|
+
}
|
|
127
|
+
const OFF_VALUES = new Set(['off', 'false', '0', 'no']);
|
|
128
|
+
/** On unless `PLE_FEEDBACK_QUEUE=off` (also `false`, `0`, `no`). */
|
|
129
|
+
function queueEnabled(env = process.env) {
|
|
130
|
+
const v = env.PLE_FEEDBACK_QUEUE?.trim().toLowerCase();
|
|
131
|
+
return !(v && OFF_VALUES.has(v));
|
|
132
|
+
}
|
|
133
|
+
function isFlushDue(state, now) {
|
|
134
|
+
if (state.items.length === 0)
|
|
135
|
+
return false;
|
|
136
|
+
const age = now - state.last_attempt_at;
|
|
137
|
+
return state.last_attempt_at === 0 || age < 0 || age >= exports.RETRY_INTERVAL_MS;
|
|
138
|
+
}
|
|
139
|
+
function sessionToken() {
|
|
140
|
+
try {
|
|
141
|
+
const config = (0, config_1.readConfig)();
|
|
142
|
+
return config && !(0, config_1.isTokenExpiringSoon)(config) ? config.access_token : undefined;
|
|
143
|
+
}
|
|
144
|
+
catch {
|
|
145
|
+
return undefined;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
/** Remove the finished entries from the CURRENT file (a concurrent `ple feedback` may have added some meanwhile). */
|
|
149
|
+
function removeFinished(done) {
|
|
150
|
+
const fresh = readQueue();
|
|
151
|
+
const items = fresh.items.filter((i) => !done.has(i.qid));
|
|
152
|
+
writeQueue({ ...fresh, items });
|
|
153
|
+
return items.length;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Try to deliver the queued reports. Silent and bounded; returns what it did, for tests. Never throws.
|
|
157
|
+
*
|
|
158
|
+
* Skipped (nothing read, nothing sent): switched off; no queue; not due; after a failed command; after `ple
|
|
159
|
+
* feedback` itself unless it succeeded; and after the commands documented as offline or as talking to one
|
|
160
|
+
* host only (the same list the update notice honors), so "no network access" stays true for them.
|
|
161
|
+
*/
|
|
162
|
+
async function flushQueue(deps = {}) {
|
|
163
|
+
const result = { attempted: false, sent: 0, dropped: 0, remaining: 0 };
|
|
164
|
+
try {
|
|
165
|
+
const env = deps.env ?? process.env;
|
|
166
|
+
if (!queueEnabled(env))
|
|
167
|
+
return result;
|
|
168
|
+
if (deps.command === undefined || updateCheck_1.SKIPPED_COMMANDS.has(deps.command))
|
|
169
|
+
return result;
|
|
170
|
+
if (deps.exitCode)
|
|
171
|
+
return result;
|
|
172
|
+
const now = deps.now ?? Date.now();
|
|
173
|
+
const state = readQueue();
|
|
174
|
+
result.remaining = state.items.length;
|
|
175
|
+
if (!isFlushDue(state, now))
|
|
176
|
+
return result;
|
|
177
|
+
// Stamp BEFORE sending: a hung or failed attempt is not repeated by the next command.
|
|
178
|
+
writeQueue({ ...state, last_attempt_at: now });
|
|
179
|
+
result.attempted = true;
|
|
180
|
+
const send = deps.send ?? ((p, o) => (0, feedbackApi_1.sendFeedback)(p, o));
|
|
181
|
+
const token = deps.token === undefined ? sessionToken() : deps.token ?? undefined;
|
|
182
|
+
const deadline = Date.now() + exports.FLUSH_TIMEOUT_MS;
|
|
183
|
+
const done = new Set();
|
|
184
|
+
for (const item of state.items) {
|
|
185
|
+
const left = deadline - Date.now();
|
|
186
|
+
if (left < 100)
|
|
187
|
+
break;
|
|
188
|
+
let out = await send(item.payload, { token, timeoutMs: left });
|
|
189
|
+
if (out.kind === 'auth' && token) {
|
|
190
|
+
const again = deadline - Date.now();
|
|
191
|
+
if (again < 100)
|
|
192
|
+
break;
|
|
193
|
+
out = await send(item.payload, { token: undefined, timeoutMs: again });
|
|
194
|
+
}
|
|
195
|
+
if (out.kind === 'sent') {
|
|
196
|
+
done.add(item.qid);
|
|
197
|
+
result.sent += 1;
|
|
198
|
+
}
|
|
199
|
+
else if (out.kind === 'invalid') {
|
|
200
|
+
// The server will never accept this text; keeping it only blocks the queue.
|
|
201
|
+
done.add(item.qid);
|
|
202
|
+
result.dropped += 1;
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
break; // rate limit, no network, server error: stop, the next due attempt continues here
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
if (done.size > 0)
|
|
209
|
+
result.remaining = removeFinished(done);
|
|
210
|
+
}
|
|
211
|
+
catch {
|
|
212
|
+
// A background send must never get in the way of a command.
|
|
213
|
+
}
|
|
214
|
+
return result;
|
|
215
|
+
}
|
|
216
|
+
/** What index.ts calls once after a command: figures out the command from argv and flushes. */
|
|
217
|
+
async function flushAfterCommand(argv = process.argv.slice(2)) {
|
|
218
|
+
return flushQueue({ command: (0, updateCheck_1.commandOf)(argv), exitCode: process.exitCode });
|
|
219
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Who is writing the report: a person at a terminal, or an AI agent running `ple` for someone?
|
|
4
|
+
*
|
|
5
|
+
* Decided from, in this order:
|
|
6
|
+
* 1. `--agent <name>` → agent, that name
|
|
7
|
+
* 2. `PLE_AGENT=<name>` → agent, that name (the explicit opt-in for any tool)
|
|
8
|
+
* 3. `AI_AGENT=<name>` → agent, that name (a bare `1`/`true` → `unknown`)
|
|
9
|
+
* 4. `CLAUDECODE` / `CLAUDE_CODE_ENTRYPOINT` → agent `claude-code`
|
|
10
|
+
* 5. any `CODEX_*` → agent `codex`
|
|
11
|
+
* 6. any `CURSOR_*` → agent `cursor`
|
|
12
|
+
* otherwise human.
|
|
13
|
+
*
|
|
14
|
+
* Whether stdin is a terminal is deliberately NOT a signal: people pipe into `ple` all the time, and an
|
|
15
|
+
* agent can have a pseudo-terminal. Only a name that was set, on purpose or by the agent's own host, counts.
|
|
16
|
+
*/
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.detectSource = detectSource;
|
|
19
|
+
const feedbackInput_1 = require("./feedbackInput");
|
|
20
|
+
const GENERIC_VALUES = new Set(['1', 'true', 'yes', 'on']);
|
|
21
|
+
function present(env, key) {
|
|
22
|
+
const v = env[key];
|
|
23
|
+
return typeof v === 'string' && v.trim() !== '' ? v.trim() : undefined;
|
|
24
|
+
}
|
|
25
|
+
function anyWithPrefix(env, prefix) {
|
|
26
|
+
return Object.keys(env).some((k) => k.startsWith(prefix) && present(env, k) !== undefined);
|
|
27
|
+
}
|
|
28
|
+
/** `flag` is the raw `--agent` value (already known to be non-empty after sanitising, see the command). */
|
|
29
|
+
function detectSource(env, flag) {
|
|
30
|
+
if (flag !== undefined) {
|
|
31
|
+
const name = (0, feedbackInput_1.sanitizeAgentName)(flag);
|
|
32
|
+
return { source: 'agent', agent_name: name || 'unknown' };
|
|
33
|
+
}
|
|
34
|
+
const ple = present(env, 'PLE_AGENT');
|
|
35
|
+
if (ple !== undefined) {
|
|
36
|
+
const name = (0, feedbackInput_1.sanitizeAgentName)(ple);
|
|
37
|
+
if (name)
|
|
38
|
+
return { source: 'agent', agent_name: name };
|
|
39
|
+
}
|
|
40
|
+
const generic = present(env, 'AI_AGENT');
|
|
41
|
+
if (generic !== undefined) {
|
|
42
|
+
const name = GENERIC_VALUES.has(generic.toLowerCase()) ? '' : (0, feedbackInput_1.sanitizeAgentName)(generic);
|
|
43
|
+
return { source: 'agent', agent_name: name || 'unknown' };
|
|
44
|
+
}
|
|
45
|
+
if (present(env, 'CLAUDECODE') !== undefined || present(env, 'CLAUDE_CODE_ENTRYPOINT') !== undefined) {
|
|
46
|
+
return { source: 'agent', agent_name: 'claude-code' };
|
|
47
|
+
}
|
|
48
|
+
if (anyWithPrefix(env, 'CODEX_'))
|
|
49
|
+
return { source: 'agent', agent_name: 'codex' };
|
|
50
|
+
if (anyWithPrefix(env, 'CURSOR_'))
|
|
51
|
+
return { source: 'agent', agent_name: 'cursor' };
|
|
52
|
+
return { source: 'human' };
|
|
53
|
+
}
|
package/dist/lib/libraryCache.js
CHANGED
|
@@ -113,6 +113,7 @@ function entriesFromAccount(prompts) {
|
|
|
113
113
|
body: (0, format_1.joinPromptParts)(p.current_version ?? {}).slice(0, rank_1.BODY_HEAD),
|
|
114
114
|
uses: typeof p.use_count === 'number' ? p.use_count : null,
|
|
115
115
|
repo: p.project_name ?? null,
|
|
116
|
+
repoId: p.project_id ?? null,
|
|
116
117
|
}));
|
|
117
118
|
}
|
|
118
119
|
function entriesFromLocal(prompts) {
|