promptlike 0.10.1 → 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 +47 -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 +71 -0
- package/dist/commands/search.js +4 -1
- package/dist/commands/suggest.js +37 -4
- 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 +18 -8
- 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,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) {
|
package/dist/lib/localStore.js
CHANGED
|
@@ -83,6 +83,8 @@ const os = __importStar(require("os"));
|
|
|
83
83
|
const path = __importStar(require("path"));
|
|
84
84
|
const fsSafe_1 = require("./fsSafe");
|
|
85
85
|
const promptTemplate_1 = require("./promptTemplate");
|
|
86
|
+
const titleMatch_1 = require("./titleMatch");
|
|
87
|
+
const rank_1 = require("./rank");
|
|
86
88
|
/** Frontmatter key ↔ `Upstream` property. Order = file order. */
|
|
87
89
|
const UPSTREAM_KEYS = [
|
|
88
90
|
['repo', 'upstream_repo'],
|
|
@@ -152,8 +154,9 @@ function slugFor(title) {
|
|
|
152
154
|
return legacy !== slugify(title) && exists(legacy) ? legacy : slugify(title);
|
|
153
155
|
}
|
|
154
156
|
function escapeValue(v) {
|
|
155
|
-
// Frontmatter is one value per line; a newline would silently truncate it.
|
|
156
|
-
|
|
157
|
+
// Frontmatter is one value per line; a newline would silently truncate it. The reader's `m` flag
|
|
158
|
+
// also treats a lone \r, U+2028 and U+2029 as line ends, so an imported title could plant a field.
|
|
159
|
+
return v.replace(/[\r\n\u2028\u2029]+/g, ' ').trim();
|
|
157
160
|
}
|
|
158
161
|
function serialize(p) {
|
|
159
162
|
// Empty fields are written without a trailing space — these files are meant
|
|
@@ -312,8 +315,10 @@ function list() {
|
|
|
312
315
|
.sort((a, b) => (b.updated || '').localeCompare(a.updated || ''));
|
|
313
316
|
}
|
|
314
317
|
/**
|
|
315
|
-
* Resolve a user-typed name to one prompt: exact slug, then exact title,
|
|
316
|
-
*
|
|
318
|
+
* Resolve a user-typed name to one prompt: exact slug, then exact title, then the same name ignoring
|
|
319
|
+
* case, Turkish letters and accents ("hat baslangici" = "Hat başlangıcı"), then a unique partial of
|
|
320
|
+
* title or slug (same folding). A step with several hits reports them as candidates — ambiguity is
|
|
321
|
+
* reported, never guessed — and a later, looser step is never tried once an earlier one hit.
|
|
317
322
|
*/
|
|
318
323
|
function resolve(name) {
|
|
319
324
|
const all = list();
|
|
@@ -328,19 +333,24 @@ function resolve(name) {
|
|
|
328
333
|
return { prompt: byTitle[0] };
|
|
329
334
|
if (byTitle.length > 1)
|
|
330
335
|
return { candidates: byTitle };
|
|
331
|
-
const
|
|
336
|
+
const folded = all.filter((p) => (0, titleMatch_1.foldedEquals)(p.title, name) || (0, titleMatch_1.foldedEquals)(p.slug, name));
|
|
337
|
+
if (folded.length === 1)
|
|
338
|
+
return { prompt: folded[0] };
|
|
339
|
+
if (folded.length > 1)
|
|
340
|
+
return { candidates: folded };
|
|
341
|
+
const partial = all.filter((p) => (0, titleMatch_1.foldedIncludes)(p.slug, name) || (0, titleMatch_1.foldedIncludes)(p.title, name));
|
|
332
342
|
if (partial.length === 1)
|
|
333
343
|
return { prompt: partial[0] };
|
|
334
344
|
if (partial.length > 1)
|
|
335
345
|
return { candidates: partial };
|
|
336
346
|
return null;
|
|
337
347
|
}
|
|
338
|
-
/** Free-text search over title, description, tags and body. */
|
|
348
|
+
/** Free-text search over title, description, tags and body — case, Turkish letters and accents folded ("baslangic" finds "başlangıç"). */
|
|
339
349
|
function search(query) {
|
|
340
|
-
const needle =
|
|
350
|
+
const needle = (0, rank_1.foldText)(query.trim());
|
|
341
351
|
if (!needle)
|
|
342
352
|
return [];
|
|
343
|
-
return list().filter((p) => [p.title, p.description, p.tags.join(' '), p.body].join('\n')
|
|
353
|
+
return list().filter((p) => (0, rank_1.foldText)([p.title, p.description, p.tags.join(' '), p.body].join('\n')).includes(needle));
|
|
344
354
|
}
|
|
345
355
|
/** Fill `{{variables}}`; report the ones left unfilled rather than shipping them. */
|
|
346
356
|
function fillVariables(body, values) {
|
package/dist/lib/loopApi.js
CHANGED
|
@@ -15,6 +15,9 @@ exports.bearerGet = bearerGet;
|
|
|
15
15
|
exports.requestVariants = requestVariants;
|
|
16
16
|
exports.saveVersion = saveVersion;
|
|
17
17
|
exports.saveWinner = saveWinner;
|
|
18
|
+
exports.createPrompt = createPrompt;
|
|
19
|
+
exports.patchDescription = patchDescription;
|
|
20
|
+
exports.addTagByName = addTagByName;
|
|
18
21
|
const constants_1 = require("./constants");
|
|
19
22
|
const config_1 = require("./config");
|
|
20
23
|
/**
|
|
@@ -66,10 +69,10 @@ async function bearerRequest(method, path, body, timeoutMs, forbidden) {
|
|
|
66
69
|
res = await fetch(constants_1.API_BASE_URL + path, {
|
|
67
70
|
method,
|
|
68
71
|
headers: {
|
|
69
|
-
...(method
|
|
72
|
+
...(method !== 'GET' ? { 'Content-Type': 'application/json' } : {}),
|
|
70
73
|
Authorization: `Bearer ${config.access_token}`,
|
|
71
74
|
},
|
|
72
|
-
body: method
|
|
75
|
+
body: method !== 'GET' ? JSON.stringify(body) : undefined,
|
|
73
76
|
// Süre başlıkla sınırlı değil: gövde okuması da aynı sinyalle kesilir.
|
|
74
77
|
signal,
|
|
75
78
|
redirect: 'manual',
|
|
@@ -185,3 +188,36 @@ async function saveWinner(promptId, variant, changelog) {
|
|
|
185
188
|
source: 'cli',
|
|
186
189
|
}, exports.SAVE_TIMEOUT_MS);
|
|
187
190
|
}
|
|
191
|
+
const createForbidden = (serverError, body) => typeof body.upgradeUrl === 'string'
|
|
192
|
+
? new Error(`${serverError?.trim() || 'Prompt limit reached on this plan.'} — upgrade at ${constants_1.API_BASE_URL}/pricing`)
|
|
193
|
+
: new Error(serverError?.trim() || 'You are not allowed to add prompts to this repo.');
|
|
194
|
+
const metaForbidden = (serverError) => new Error(serverError?.trim() || 'You are not allowed to change this prompt.');
|
|
195
|
+
/**
|
|
196
|
+
* Create a prompt (with its first version) in a repo through the save endpoint
|
|
197
|
+
* (`isNew`). A single-body prompt goes into `contextText` — the slot `ple sync`
|
|
198
|
+
* and the import route use — so it reads back exactly as written.
|
|
199
|
+
*/
|
|
200
|
+
async function createPrompt(input) {
|
|
201
|
+
return bearerRequest('POST', '/api/prompts', {
|
|
202
|
+
isNew: true,
|
|
203
|
+
projectId: input.projectId,
|
|
204
|
+
title: input.title,
|
|
205
|
+
description: input.description ?? '',
|
|
206
|
+
roleText: '',
|
|
207
|
+
descriptionText: '',
|
|
208
|
+
contextText: input.body,
|
|
209
|
+
changelog: input.changelog,
|
|
210
|
+
source: 'cli',
|
|
211
|
+
}, exports.SAVE_TIMEOUT_MS, createForbidden);
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* PATCH /api/prompts/{id} — change the prompt-level description only. No version
|
|
215
|
+
* is created (the description is not part of a version) and nothing else moves.
|
|
216
|
+
*/
|
|
217
|
+
async function patchDescription(promptId, description) {
|
|
218
|
+
await bearerRequest('PATCH', `/api/prompts/${encodeURIComponent(promptId)}`, { description }, exports.SAVE_TIMEOUT_MS, metaForbidden);
|
|
219
|
+
}
|
|
220
|
+
/** POST /api/prompts/{id}/tags — attach a tag by name (found or created in the prompt's repo); already attached is a no-op. */
|
|
221
|
+
async function addTagByName(promptId, tagName) {
|
|
222
|
+
await bearerRequest('POST', `/api/prompts/${encodeURIComponent(promptId)}/tags`, { tagName }, exports.SAVE_TIMEOUT_MS, metaForbidden);
|
|
223
|
+
}
|