slashvibe-mcp 0.9.14 → 0.9.15
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 +5 -0
- package/draft-cli.js +4 -1
- package/package.json +1 -1
- package/store/api.js +15 -0
- package/tools/drafts.js +201 -5
package/README.md
CHANGED
|
@@ -60,6 +60,11 @@ was marked read, until you chose.
|
|
|
60
60
|
session, picks the person it is for, writes it, and shows you the exact message. Send,
|
|
61
61
|
Edit or Cancel. Or just say it: `message @sam: ship it`.
|
|
62
62
|
|
|
63
|
+
**Someone who is not here yet.** If the person you are writing to has no /vibe account, the
|
|
64
|
+
preview says so, and Keep holds your exact message for the GitHub account of that name for
|
|
65
|
+
30 days. You get a join link and your setup line to pass on; when they sign in to /vibe with
|
|
66
|
+
that account, it is your first message to them. Nothing reaches them before then.
|
|
67
|
+
|
|
63
68
|
## Who's here
|
|
64
69
|
|
|
65
70
|
When you open a session, one line tells you which of the people you know are around:
|
package/draft-cli.js
CHANGED
|
@@ -46,7 +46,10 @@ async function main() {
|
|
|
46
46
|
// do that under the lock first, so an abandoned draft is listed as
|
|
47
47
|
// 'unknown' (retry or discard) instead of hidden forever (codex r2).
|
|
48
48
|
moves.transact((drafts) => { for (const d of drafts) moves.reconcileAbandoned(d); return drafts; });
|
|
49
|
-
|
|
49
|
+
// A held draft (for someone not on /vibe yet) is decided only where its
|
|
50
|
+
// preview says so: a companion that renders it as an ordinary Send would
|
|
51
|
+
// describe a different act than the one it performs.
|
|
52
|
+
const mine = moves.loadDrafts().filter((d) => String(d.from || '').toLowerCase() === me && (d.status === 'previewed' || d.status === 'unknown') && !d.held);
|
|
50
53
|
// Exactly what the person would see in the terminal preview — and the rev
|
|
51
54
|
// that binds a Send to those bytes. Nothing private: context stays out.
|
|
52
55
|
const drafts = mine.map((d) => ({
|
package/package.json
CHANGED
package/store/api.js
CHANGED
|
@@ -907,6 +907,18 @@ async function markThreadRead(myHandle, theirHandle, lastMessageId = null, threa
|
|
|
907
907
|
|
|
908
908
|
// ============ INVITES ============
|
|
909
909
|
|
|
910
|
+
/**
|
|
911
|
+
* POST /api/invites — a held message for someone not on /vibe yet (a
|
|
912
|
+
* thought-bearing invitation named for one GitHub login). Sent ONCE, never
|
|
913
|
+
* retried here: the route has no idempotency key, so a transport retry after
|
|
914
|
+
* a lost response could mint a second invitation. The caller decides what an
|
|
915
|
+
* unanswered request means. `body.dry_run: true` is the platform's
|
|
916
|
+
* side-effect-free preview of the same request.
|
|
917
|
+
*/
|
|
918
|
+
async function createInvite(body) {
|
|
919
|
+
return request('POST', '/api/invites', body, { maxRetries: 0 });
|
|
920
|
+
}
|
|
921
|
+
|
|
910
922
|
// ============ AUTH ============
|
|
911
923
|
|
|
912
924
|
/**
|
|
@@ -1021,6 +1033,9 @@ module.exports = {
|
|
|
1021
1033
|
// Auth
|
|
1022
1034
|
verifyAuthToken,
|
|
1023
1035
|
|
|
1036
|
+
// Invites (a held message for someone not on /vibe yet)
|
|
1037
|
+
createInvite,
|
|
1038
|
+
|
|
1024
1039
|
// Hermetic request-count tests for the named-thread fast path.
|
|
1025
1040
|
_testing: { getThreadWithRequest, markThreadReadWithRequest },
|
|
1026
1041
|
};
|
package/tools/drafts.js
CHANGED
|
@@ -23,6 +23,12 @@
|
|
|
23
23
|
*
|
|
24
24
|
* Draft states: previewed → sending → sent
|
|
25
25
|
* ↘ cancelled ↘ unknown (retry/cancel only)
|
|
26
|
+
*
|
|
27
|
+
* Held drafts: a draft to a handle /vibe says does not exist (a definite 404,
|
|
28
|
+
* never a lookup that merely failed) and that is a GitHub login is HELD: Send
|
|
29
|
+
* creates a thought-bearing invitation named for that GitHub account instead
|
|
30
|
+
* of a message, so the exact approved text becomes the sender's first message
|
|
31
|
+
* when that person signs in. previewed → sending → held. It is never "sent".
|
|
26
32
|
*/
|
|
27
33
|
|
|
28
34
|
const fs = require('fs');
|
|
@@ -34,6 +40,7 @@ const { requireInit, normalizeHandle, isHereNow } = require('./_shared');
|
|
|
34
40
|
const { canonicalHandle, storedRecipientHandle } = require('../protocol/handle');
|
|
35
41
|
const { inertField } = require('../incoming');
|
|
36
42
|
const { deliver } = require('./deliver');
|
|
43
|
+
const invite = require('../invite');
|
|
37
44
|
|
|
38
45
|
const DRAFTS_FILE = path.join(config.VIBE_DIR, 'drafts.json');
|
|
39
46
|
const BINDINGS_FILE = path.join(config.VIBE_DIR, 'return-bindings.json');
|
|
@@ -194,7 +201,9 @@ function reconcileAbandoned(d) {
|
|
|
194
201
|
const newId = (prefix) => `${prefix}${crypto.randomBytes(4).toString('hex')}`;
|
|
195
202
|
// The reply target is part of what the person approves: answering a
|
|
196
203
|
// different message is a different act, so it changes the rev and the key.
|
|
197
|
-
|
|
204
|
+
// Holding a message for a newcomer is a different act from sending one, so
|
|
205
|
+
// it is part of the rev too (a held draft never shares a rev with a send).
|
|
206
|
+
const textHash = (d) => crypto.createHash('sha1').update(compose(d) + (d.replyTo ? `\n\u21a9${d.replyTo}` : '') + (d.held ? '\nheld-for-github' : '')).digest('hex');
|
|
198
207
|
const sendKey = (d) => `draft-${d.id}-${textHash(d).slice(0, 10)}`;
|
|
199
208
|
/**
|
|
200
209
|
* The #392 approval digest over the EXACT snapshot the person approved:
|
|
@@ -207,6 +216,29 @@ const approvedDigest = (to, message) => crypto.createHash('sha256').update(`${st
|
|
|
207
216
|
/** The preview revision: what the person SAW. Send must name it (codex P1). */
|
|
208
217
|
const revOf = (d) => textHash(d).slice(0, 8);
|
|
209
218
|
|
|
219
|
+
// ── held: a message kept for someone not on /vibe yet ─────────────────────────
|
|
220
|
+
// Platform contract: api/invites.js (POST /api/invites with first_thought +
|
|
221
|
+
// for_github), api/lib/invitation-email.js (APPROVAL_RULE, approvalInput).
|
|
222
|
+
|
|
223
|
+
const HELD_DAYS = 30;
|
|
224
|
+
/** The platform's for_github shape (api/invites.js), applied to the canonical handle. */
|
|
225
|
+
const GITHUB_LOGIN = /^[a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38}$/;
|
|
226
|
+
const isGithubLogin = (h) => GITHUB_LOGIN.test(String(h || ''));
|
|
227
|
+
/** generateCode(): 'VIBE-' + 4 of an unambiguous alphabet + '-' + the inviter handle's first 4. */
|
|
228
|
+
const INVITE_CODE = /^VIBE-[A-Z0-9]{4}-[A-Z0-9_-]{1,4}$/;
|
|
229
|
+
const LONE_SURROGATE = /[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/;
|
|
230
|
+
/**
|
|
231
|
+
* The platform's invitation approval digest (APPROVAL_RULE 'invitation-v1'):
|
|
232
|
+
* sha256 over [rule, for_email, subject, text, html, open_note, question,
|
|
233
|
+
* first_thought] joined by U+0000. A held draft carries only first_thought,
|
|
234
|
+
* so every other field is the empty string. Note: for_github is NOT covered
|
|
235
|
+
* by the platform digest; the recipient is bound here by the draft rev.
|
|
236
|
+
*/
|
|
237
|
+
const heldDigest = (firstThought) => crypto.createHash('sha256')
|
|
238
|
+
.update(['invitation-v1', '', '', '', '', '', '', firstThought].join('\u0000'), 'utf8').digest('hex');
|
|
239
|
+
/** The handle the platform gives a GitHub login on first sign-in (handles.js normalizeHandle folds '-' to '_'). */
|
|
240
|
+
const handleForLogin = (login) => String(login || '').replace(/-/g, '_');
|
|
241
|
+
|
|
210
242
|
// Field ceilings keep a draft inside the 2000-char message limit. A field
|
|
211
243
|
// over its ceiling is REPORTED, never silently cut: a draft that ends
|
|
212
244
|
// mid-word is not "the exact text you approve" (product-test finding —
|
|
@@ -235,7 +267,7 @@ function compose(d) { return `${d.body || ''}${refLines(d.refs)}`; }
|
|
|
235
267
|
|
|
236
268
|
const draftDefinition = {
|
|
237
269
|
name: 'vibe_prepare',
|
|
238
|
-
description: "Prepare a message to a person for review. Sends NOTHING. Use this whenever YOU wrote the words (from the work in this session, or as an answer to a message that arrived): pass handle + message, and reply_to when it answers a specific message. Returns the exact preview and the three choices: Send / Edit / Cancel. To edit, call again with the draft id and the new message.",
|
|
270
|
+
description: "Prepare a message to a person for review. Sends NOTHING. Use this whenever YOU wrote the words (from the work in this session, or as an answer to a message that arrived): pass handle + message, and reply_to when it answers a specific message. Returns the exact preview and the three choices: Send / Edit / Cancel. To edit, call again with the draft id and the new message. If the handle is not on /vibe yet, the preview says so and the message is held for that GitHub account instead.",
|
|
239
271
|
inputSchema: {
|
|
240
272
|
type: 'object',
|
|
241
273
|
properties: {
|
|
@@ -286,6 +318,21 @@ async function draftHandler(args) {
|
|
|
286
318
|
const message = typeof (args && args.message) === 'string' ? args.message.trim() : '';
|
|
287
319
|
const wantId = args && args.id;
|
|
288
320
|
|
|
321
|
+
// Is the person on /vibe at all? Asked only for a NEW draft to a handle
|
|
322
|
+
// shaped like a GitHub login. Only a definite "no such handle" (404) makes
|
|
323
|
+
// the draft held; a lookup that could not confirm keeps today's behaviour.
|
|
324
|
+
let absent = false;
|
|
325
|
+
if (!wantId && args && args.handle && message) {
|
|
326
|
+
const to = canonicalHandle(args.handle);
|
|
327
|
+
// Someone you already know is on /vibe: no lookup, so the ordinary
|
|
328
|
+
// preview costs no extra request and cannot be slowed by this one.
|
|
329
|
+
let known = false;
|
|
330
|
+
try { known = require('../reach').isKnown(to); } catch { known = false; }
|
|
331
|
+
if (to && to !== me && to !== 'echo' && !known && isGithubLogin(to)) {
|
|
332
|
+
try { absent = (await invite.lookup(to)).found === false; } catch { absent = false; }
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
289
336
|
const out = transact(drafts => {
|
|
290
337
|
let d = wantId ? drafts.find(x => x.id === wantId) : null;
|
|
291
338
|
if (wantId && !d) return { display: `No draft ${wantId} — it may have expired (drafts live 24h, locally). Prepare it again.` };
|
|
@@ -302,12 +349,16 @@ async function draftHandler(args) {
|
|
|
302
349
|
if (to.endsWith('/claude')) return { display: `Drafts go to a person's durable conversation, not a live session — use @${to.replace(/\/claude$/, '')} instead. Nothing drafted.` };
|
|
303
350
|
const project = typeof args.project === 'string' && args.project.trim() ? args.project.trim().slice(0, 60) : null;
|
|
304
351
|
d = { id: newId('w'), status: 'previewed', createdAt: Date.now(), from: me, to, body: message, replyTo: cleanId(args.reply_to), refs: cleanContext({ refs: args.refs }).refs, context: { project } };
|
|
352
|
+
// Nothing to answer in a conversation that does not exist yet.
|
|
353
|
+
if (absent) { d.held = true; d.replyTo = null; }
|
|
305
354
|
drafts.push(d);
|
|
306
355
|
} else {
|
|
307
356
|
// A finished draft stays finished; an unconfirmed one may only be
|
|
308
357
|
// retried as-is or cancelled (its idempotency key names THIS text).
|
|
309
358
|
if (d.from && d.from !== me) return { display: `Draft ${d.id} was prepared as @${d.from}; you are signed in as @${me}. Nothing sent — open a new draft as yourself.` };
|
|
310
359
|
reconcileAbandoned(d);
|
|
360
|
+
if (d.status === 'held') return { display: heldAlready(d) };
|
|
361
|
+
if (d.held && d.status === 'unknown') return { display: heldUnknown(d) };
|
|
311
362
|
if (d.status === 'sent') return { display: `Draft ${d.id} was already sent to @${d.to} — nothing changed. Open a new draft to say more.` };
|
|
312
363
|
if (d.status === 'cancelled') return { display: d.unconfirmed
|
|
313
364
|
? `Draft ${d.id} was cancelled after a Send to @${d.to} that did not confirm — it may or may not have reached them. Open a new draft to say more.`
|
|
@@ -323,7 +374,7 @@ async function draftHandler(args) {
|
|
|
323
374
|
d.edited = true;
|
|
324
375
|
}
|
|
325
376
|
if (args.refs) { d.refs = cleanContext({ refs: args.refs }).refs; }
|
|
326
|
-
if (args.reply_to !== undefined) d.replyTo = cleanId(args.reply_to);
|
|
377
|
+
if (args.reply_to !== undefined && !d.held) d.replyTo = cleanId(args.reply_to);
|
|
327
378
|
d.status = 'previewed';
|
|
328
379
|
}
|
|
329
380
|
}
|
|
@@ -331,9 +382,31 @@ async function draftHandler(args) {
|
|
|
331
382
|
return { draft: { ...d } };
|
|
332
383
|
});
|
|
333
384
|
if (out.display) return out;
|
|
385
|
+
if (out.draft.held) return heldPreview(out.draft);
|
|
334
386
|
return preview(out.draft, await findPerson(out.draft.to));
|
|
335
387
|
}
|
|
336
388
|
|
|
389
|
+
function heldPreview(d) {
|
|
390
|
+
const att = d.refs && d.refs.length ? d.refs.map(r => `${r.title}: ${r.url}`).join('\n') : 'none';
|
|
391
|
+
const message = compose(d);
|
|
392
|
+
const rev = revOf(d);
|
|
393
|
+
return {
|
|
394
|
+
display: `**To:** @${d.to} (not on /vibe yet)\n**Message (exact):**\n${message}\n**Attachments:** ${att}\n\n`
|
|
395
|
+
+ `@${d.to} is not on /vibe yet. If you keep it, this exact message is held for the GitHub account ${d.to} for ${HELD_DAYS} days and arrives as your first message when they sign in to /vibe with that account. You get a link to pass on to them.\n\n`
|
|
396
|
+
+ `Keep for @${d.to} · Edit · Cancel — nothing has been sent. _(draft ${d.id} · rev ${rev})_`,
|
|
397
|
+
data: {
|
|
398
|
+
draft: { id: d.id, to: d.to, body: d.body, message, refs: d.refs || [], status: d.status, rev, held: true, for_github: d.to },
|
|
399
|
+
actions: [{ label: `Keep for @${d.to}`, tool: 'vibe_send', args: { id: d.id, rev } }, { label: 'Edit', tool: 'vibe_prepare', args: { id: d.id, message: '<new body text — links are kept separately>' } }, { label: 'Cancel', tool: 'vibe_discard', args: { id: d.id } }],
|
|
400
|
+
},
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
function heldAlready(d) {
|
|
404
|
+
return `Draft ${d.id} is already held for @${d.to} — not creating a second invitation.${d.joinUrl ? ` Join link: ${d.joinUrl}` : ''}`;
|
|
405
|
+
}
|
|
406
|
+
function heldUnknown(d) {
|
|
407
|
+
return `Draft ${d.id}: the last Keep for @${d.to} did not confirm — the invitation may or may not have been created, and a second try could create two. Cancel it, then open a new draft if you still want to say it. Nothing more was done.`;
|
|
408
|
+
}
|
|
409
|
+
|
|
337
410
|
// ── vibe_discard ───────────────────────────────────────────────────────
|
|
338
411
|
|
|
339
412
|
const discardDefinition = {
|
|
@@ -349,12 +422,21 @@ async function discardHandler(args) {
|
|
|
349
422
|
if (!d) return { display: `No draft ${id} to cancel — nothing was sent either way.` };
|
|
350
423
|
reconcileAbandoned(d);
|
|
351
424
|
if (d.status === 'sending') return { display: `Draft ${d.id} is being sent to @${d.to} right now — it can't be cancelled at this point.`, data: { draft: { id: d.id, status: d.status } } };
|
|
425
|
+
if (d.status === 'held') return { display: `Draft ${d.id} is already held for @${d.to} — a held message can't be taken back from here.`, data: { draft: { id: d.id, status: d.status } } };
|
|
352
426
|
if (d.status === 'sent') return { display: `Draft ${d.id} was already sent to @${d.to} — a sent message can't be unsent.`, data: { draft: { id: d.id, status: d.status } } };
|
|
353
427
|
if (d.status === 'cancelled') return { display: d.unconfirmed
|
|
354
428
|
? `Draft ${d.id} is already cancelled — and the earlier Send to @${d.to} did not confirm, so it may or may not have reached them.`
|
|
355
429
|
: `Draft ${d.id} is already cancelled — nothing was sent.`, data: { draft: { id: d.id, status: d.status } } };
|
|
356
430
|
const wasUnknown = d.status === 'unknown' || Boolean(d.unconfirmed);
|
|
357
431
|
d.status = 'cancelled';
|
|
432
|
+
if (d.held) {
|
|
433
|
+
return {
|
|
434
|
+
display: wasUnknown
|
|
435
|
+
? `Cancelled — nothing more will be done for @${d.to}. Note: the earlier Keep did not confirm, so an invitation may or may not have been created.`
|
|
436
|
+
: `Cancelled — nothing kept for @${d.to}. The draft stays on your machine.`,
|
|
437
|
+
data: { draft: { id: d.id, status: 'cancelled' } },
|
|
438
|
+
};
|
|
439
|
+
}
|
|
358
440
|
return {
|
|
359
441
|
display: wasUnknown
|
|
360
442
|
? `Cancelled — no further send to @${d.to}. Note: the earlier attempt did not confirm, so it may or may not have reached them.`
|
|
@@ -407,6 +489,7 @@ async function sendHandler(args) {
|
|
|
407
489
|
// The approval was given as one account; it is not transferable to
|
|
408
490
|
// whoever is signed in now (codex P1).
|
|
409
491
|
if (d.from && d.from !== me) return { display: `Draft ${d.id} was prepared as @${d.from}; you are signed in as @${me}. Nothing sent — open a new draft as yourself.` };
|
|
492
|
+
if (d.status === 'held') return { display: heldAlready(d) };
|
|
410
493
|
if (d.status === 'sent') return { display: `Draft ${d.id} was already sent to @${d.to} — not sending it twice.` };
|
|
411
494
|
if (d.status === 'cancelled') return { display: `Draft ${d.id} was cancelled — nothing sent. Open a new draft if you want it back.` };
|
|
412
495
|
if (!rev) return { display: `Send needs the rev shown in the preview of draft ${d.id} — open it with vibe_prepare and send with that rev. Nothing sent.` };
|
|
@@ -417,6 +500,9 @@ async function sendHandler(args) {
|
|
|
417
500
|
// so a later definite refusal cannot erase it.
|
|
418
501
|
if (!reconcileAbandoned(d)) return { display: `Draft ${d.id} is already being sent — not sending it twice.` };
|
|
419
502
|
}
|
|
503
|
+
// POST /api/invites has no idempotency key: an unconfirmed Keep is never
|
|
504
|
+
// retried, because a retry could create a second invitation.
|
|
505
|
+
if (d.held && d.status === 'unknown') return { display: heldUnknown(d) };
|
|
420
506
|
if (d.status === 'unknown' && !transportDedupes()) {
|
|
421
507
|
// Without server-side deduplication a retry could deliver twice; the
|
|
422
508
|
// honest options are to cancel (with the warning) or write anew.
|
|
@@ -425,11 +511,12 @@ async function sendHandler(args) {
|
|
|
425
511
|
d.status = 'sending'; d.claimedAt = Date.now(); d.claimedBy = process.pid;
|
|
426
512
|
d.idempotencyKey = sendKey(d);
|
|
427
513
|
const message = compose(d).trim();
|
|
428
|
-
d.approvedSha256 = approvedDigest(d.to, message);
|
|
429
|
-
return { snapshot: { id: d.id, to: d.to, body: d.body, refs: d.refs, context: d.context, replyTo: d.replyTo || null, message, key: d.idempotencyKey, approvedSha256: d.approvedSha256 } };
|
|
514
|
+
d.approvedSha256 = d.held ? heldDigest(message) : approvedDigest(d.to, message);
|
|
515
|
+
return { snapshot: { id: d.id, to: d.to, body: d.body, refs: d.refs, context: d.context, replyTo: d.replyTo || null, message, key: d.idempotencyKey, approvedSha256: d.approvedSha256, held: Boolean(d.held) } };
|
|
430
516
|
});
|
|
431
517
|
if (claim.display) return claim;
|
|
432
518
|
const s = claim.snapshot;
|
|
519
|
+
if (s.held) return keepHeld(s, me);
|
|
433
520
|
|
|
434
521
|
// 2. Deliver, outside the lock.
|
|
435
522
|
let result;
|
|
@@ -470,6 +557,115 @@ async function sendHandler(args) {
|
|
|
470
557
|
return result;
|
|
471
558
|
}
|
|
472
559
|
|
|
560
|
+
// ── Keep: create the invitation that holds the approved message ───────────────
|
|
561
|
+
|
|
562
|
+
/**
|
|
563
|
+
* One fix per refusal, built from the status and the platform's stable error
|
|
564
|
+
* token only — server prose is never shown. Every refusal here is definite:
|
|
565
|
+
* it comes from a reply the platform sent before anything was minted.
|
|
566
|
+
*/
|
|
567
|
+
function heldRefusal(r) {
|
|
568
|
+
const status = r && r.statusCode;
|
|
569
|
+
const nothing = 'Nothing was kept.';
|
|
570
|
+
if (status === 401) return `${nothing} /vibe does not see you signed in. Run \`vibe init\` to sign in, then keep it again.`;
|
|
571
|
+
if (status === 403) return `${nothing} Holding a message for someone needs a fresh sign-in on this machine. Run \`vibe init\`, then keep it again.`;
|
|
572
|
+
if (status === 400 && r.error === 'No invite codes remaining') return `${nothing} You already have 3 unused invitations, the most /vibe allows at once. Pass on one of the links you already have; a slot opens when someone signs in with one or it expires after ${HELD_DAYS} days.`;
|
|
573
|
+
if (status === 400 && typeof r.error === 'string' && /^first_thought is \d+ chars/.test(r.error)) return `${nothing} The message is over the 2000-char limit. Edit it shorter, then keep it again.`;
|
|
574
|
+
if (status === 409 && r.error === 'approval_mismatch') return `${nothing} What /vibe would store is not what you approved. Open the draft again with vibe_prepare and approve what it shows now.`;
|
|
575
|
+
return `${nothing} /vibe refused the invitation${status ? ` (HTTP ${status})` : ''}. Try again in a moment.`;
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
async function keepHeld(s, me) {
|
|
579
|
+
let outcome; // { held: true, code, expiresAt } | { definite: bool, display, cancel? }
|
|
580
|
+
try {
|
|
581
|
+
outcome = await createHeld(s);
|
|
582
|
+
} catch (e) {
|
|
583
|
+
outcome = { definite: false, display: 'The invitation could not be confirmed — it may or may not have been created.' };
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
const at = Date.now();
|
|
587
|
+
const joinUrl = outcome.held ? `https://www.slashvibe.dev/join/${outcome.code}` : null;
|
|
588
|
+
transact(drafts => {
|
|
589
|
+
const cur = drafts.find(x => x.id === s.id);
|
|
590
|
+
if (!cur) return;
|
|
591
|
+
delete cur.claimedAt; delete cur.claimedBy;
|
|
592
|
+
if (outcome.held) { cur.status = 'held'; cur.heldAt = at; cur.joinUrl = joinUrl; cur.expiresAt = outcome.expiresAt || null; return; }
|
|
593
|
+
if (!outcome.definite) { cur.unconfirmed = true; cur.status = 'unknown'; return; }
|
|
594
|
+
cur.status = outcome.cancel ? 'cancelled' : (cur.unconfirmed ? 'unknown' : 'previewed');
|
|
595
|
+
});
|
|
596
|
+
|
|
597
|
+
if (!outcome.held) {
|
|
598
|
+
const tail = outcome.definite ? '' : `\n\n_Draft ${s.id} is kept as unconfirmed. Keeping it again could create a second invitation, so the only choice left is Cancel._`;
|
|
599
|
+
return { display: `${outcome.display}${tail}`, data: { sent: false, held: false, definite: Boolean(outcome.definite) } };
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
const until = outcome.expiresAt ? `, until ${outcome.expiresAt}` : '';
|
|
603
|
+
const pass = invite.line(me);
|
|
604
|
+
const lines = [
|
|
605
|
+
`Held for @${s.to}. /vibe created the invitation.`,
|
|
606
|
+
`**Join link:** ${joinUrl}`,
|
|
607
|
+
];
|
|
608
|
+
if (pass) lines.push(`**Pass on with it:** ${pass.command}`);
|
|
609
|
+
lines.push(`Your message is kept for the GitHub account ${s.to} for ${HELD_DAYS} days${until}. It arrives as your first message when they sign in to /vibe with that account; nothing reaches them before then.`);
|
|
610
|
+
|
|
611
|
+
// Writing to someone is what lets their answer reach you mid-work. Their
|
|
612
|
+
// handle does not exist yet: it will be their GitHub login with '-' folded
|
|
613
|
+
// to '_' (the platform's first-sign-in rule), so that is the one marked.
|
|
614
|
+
try { require('../reach').know(handleForLogin(s.to)); } catch (e) {}
|
|
615
|
+
|
|
616
|
+
return {
|
|
617
|
+
display: lines.join('\n'),
|
|
618
|
+
data: { sent: false, held: true, definite: true, for_github: s.to, join_url: joinUrl, expires_at: outcome.expiresAt || null },
|
|
619
|
+
};
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
/**
|
|
623
|
+
* dry_run first (side-effect free: the platform checks sign-in, the unused-
|
|
624
|
+
* code cap and the thought before it answers), confirm its digest is the one
|
|
625
|
+
* computed from the approved snapshot, then create with approved_sha256 so
|
|
626
|
+
* the platform refuses anything other than what was previewed.
|
|
627
|
+
*/
|
|
628
|
+
async function createHeld(s) {
|
|
629
|
+
if (typeof store.createInvite !== 'function') return { definite: true, display: 'Nothing was kept. Holding a message for someone needs the /vibe service; this session is in local mode.' };
|
|
630
|
+
const thought = s.message;
|
|
631
|
+
if (thought.length > 2000) return { definite: true, display: `Nothing was kept. The message is ${thought.length} chars and the limit is 2000. Edit it shorter, then keep it again.` };
|
|
632
|
+
if (thought.includes('\u0000') || LONE_SURROGATE.test(thought)) return { definite: true, display: 'Nothing was kept. The message has a character /vibe cannot keep. Edit it, then keep it again.' };
|
|
633
|
+
|
|
634
|
+
// They may have joined since the preview: then this is a message, not an invitation.
|
|
635
|
+
const who = await invite.lookup(s.to).catch(() => ({ found: null }));
|
|
636
|
+
if (who.found === true) return { definite: true, cancel: true, display: `Nothing was kept. @${s.to} joined /vibe since you saw the preview, so this draft is cancelled. Prepare it again and it goes to them directly.` };
|
|
637
|
+
|
|
638
|
+
const body = { first_thought: thought, for_github: s.to };
|
|
639
|
+
const dry = await store.createInvite({ ...body, dry_run: true });
|
|
640
|
+
if (!dry || dry.success !== true) {
|
|
641
|
+
if (dry && dry.statusCode) return { definite: true, display: heldRefusal(dry) };
|
|
642
|
+
return { definite: true, display: 'Nothing was kept. /vibe could not be reached. Try again in a moment.' };
|
|
643
|
+
}
|
|
644
|
+
const serverSha = dry.approval && typeof dry.approval.sha256 === 'string' ? dry.approval.sha256 : null;
|
|
645
|
+
if (dry.dry_run !== true || dry.carries_thought !== true || dry.named_for !== s.to || serverSha !== s.approvedSha256) {
|
|
646
|
+
return { definite: true, display: heldRefusal({ statusCode: 409, error: 'approval_mismatch' }) };
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
const made = await store.createInvite({ ...body, approved_sha256: s.approvedSha256 });
|
|
650
|
+
if (made && made.success === true) {
|
|
651
|
+
// Created only on the served truth: a code of the platform's shape, named
|
|
652
|
+
// for this login, carrying the thought. Anything else may have minted
|
|
653
|
+
// something, so it is unconfirmed, never success.
|
|
654
|
+
const code = typeof made.code === 'string' ? made.code : '';
|
|
655
|
+
if (INVITE_CODE.test(code) && made.carries_thought === true && made.named_for === s.to) {
|
|
656
|
+
const t = Date.parse(made.expires_at);
|
|
657
|
+
return { held: true, code, expiresAt: Number.isFinite(t) ? new Date(t).toISOString().slice(0, 10) : null };
|
|
658
|
+
}
|
|
659
|
+
return { definite: false, display: '/vibe answered, but not with an invitation held for this account. It may or may not have been created.' };
|
|
660
|
+
}
|
|
661
|
+
// A reply with a 4xx status, or a 503 (both of its paths say nothing was
|
|
662
|
+
// saved), was decided before anything was minted. No reply, or a crash,
|
|
663
|
+
// leaves it open.
|
|
664
|
+
const st = made && made.statusCode;
|
|
665
|
+
if (st && (st < 500 || st === 503)) return { definite: true, display: heldRefusal(made) };
|
|
666
|
+
return { definite: false, display: 'The invitation could not be confirmed — it may or may not have been created.' };
|
|
667
|
+
}
|
|
668
|
+
|
|
473
669
|
module.exports = {
|
|
474
670
|
vibe_prepare: { definition: draftDefinition, handler: draftHandler },
|
|
475
671
|
vibe_send: { definition: sendDefinition, handler: sendHandler },
|