wherever-dev 0.11.2 → 0.13.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.
Files changed (47) hide show
  1. package/dist/drafts.d.ts +119 -0
  2. package/dist/drafts.d.ts.map +1 -0
  3. package/dist/drafts.js +272 -0
  4. package/dist/drafts.js.map +1 -0
  5. package/dist/index.js +271 -15
  6. package/dist/index.js.map +1 -1
  7. package/dist/session-pool.d.ts +31 -0
  8. package/dist/session-pool.d.ts.map +1 -1
  9. package/dist/session-pool.js +34 -2
  10. package/dist/session-pool.js.map +1 -1
  11. package/package.json +1 -1
  12. package/public/_app/immutable/assets/0.BS_EGmNW.css +1 -0
  13. package/public/_app/immutable/chunks/BeRKnBIj.js +3 -0
  14. package/public/_app/immutable/chunks/{itgD7vGH.js.map → BeRKnBIj.js.map} +1 -1
  15. package/public/_app/immutable/chunks/{Dtm96ZKi.js → C8CBGqeA.js} +2 -2
  16. package/public/_app/immutable/chunks/{Dtm96ZKi.js.map → C8CBGqeA.js.map} +1 -1
  17. package/public/_app/immutable/chunks/{CRRPBe07.js → Cum3gQOV.js} +2 -2
  18. package/public/_app/immutable/chunks/{CRRPBe07.js.map → Cum3gQOV.js.map} +1 -1
  19. package/public/_app/immutable/chunks/{CE0jm7EC.js → CyQq9q8L.js} +2 -2
  20. package/public/_app/immutable/chunks/{CE0jm7EC.js.map → CyQq9q8L.js.map} +1 -1
  21. package/public/_app/immutable/chunks/{CAWbdcQM.js → D5t5dKgW.js} +2 -2
  22. package/public/_app/immutable/chunks/{CAWbdcQM.js.map → D5t5dKgW.js.map} +1 -1
  23. package/public/_app/immutable/chunks/{8jn76iTS.js → DdFruy3e.js} +2 -2
  24. package/public/_app/immutable/chunks/{8jn76iTS.js.map → DdFruy3e.js.map} +1 -1
  25. package/public/_app/immutable/chunks/{BDmEqqqL.js → vSWNc7VF.js} +2 -2
  26. package/public/_app/immutable/chunks/{BDmEqqqL.js.map → vSWNc7VF.js.map} +1 -1
  27. package/public/_app/immutable/entry/app.BzMua2z6.js +3 -0
  28. package/public/_app/immutable/entry/{app.YbNg9lmT.js.map → app.BzMua2z6.js.map} +1 -1
  29. package/public/_app/immutable/entry/start.B0ap2dTz.js +2 -0
  30. package/public/_app/immutable/entry/start.B0ap2dTz.js.map +1 -0
  31. package/public/_app/immutable/nodes/{0.BwSufUPS.js → 0.BDXduH3G.js} +3 -3
  32. package/public/_app/immutable/nodes/{0.BwSufUPS.js.map → 0.BDXduH3G.js.map} +1 -1
  33. package/public/_app/immutable/nodes/1.CqJo-807.js +2 -0
  34. package/public/_app/immutable/nodes/{1.x8yi4PgO.js.map → 1.CqJo-807.js.map} +1 -1
  35. package/public/_app/immutable/nodes/2.C6-6KHYk.js +110 -0
  36. package/public/_app/immutable/nodes/2.C6-6KHYk.js.map +1 -0
  37. package/public/_app/version.json +1 -1
  38. package/public/index.html +15 -15
  39. package/public/service-worker.js +1 -1
  40. package/public/_app/immutable/assets/0.BnJtv5fG.css +0 -1
  41. package/public/_app/immutable/chunks/itgD7vGH.js +0 -3
  42. package/public/_app/immutable/entry/app.YbNg9lmT.js +0 -3
  43. package/public/_app/immutable/entry/start.Mhebc2Bx.js +0 -2
  44. package/public/_app/immutable/entry/start.Mhebc2Bx.js.map +0 -1
  45. package/public/_app/immutable/nodes/1.x8yi4PgO.js +0 -2
  46. package/public/_app/immutable/nodes/2.BWbDsdRU.js +0 -104
  47. package/public/_app/immutable/nodes/2.BWbDsdRU.js.map +0 -1
@@ -0,0 +1,119 @@
1
+ /**
2
+ * Hard cap on retained drafts, dropping the least recently touched. A draft is
3
+ * kept forever by definition, so without a cap this file is the one thing here
4
+ * that grows without bound.
5
+ */
6
+ export declare const MAX_DRAFTS = 100;
7
+ /** Per-draft character cap. A composer message is prose, not a file paste. */
8
+ export declare const MAX_DRAFT_CHARS = 20000;
9
+ /**
10
+ * Cap on the display metadata (`sessionId` / `cwd`). These are not user prose,
11
+ * they are values the client already holds, so anything longer is a client bug
12
+ * or an abuse attempt. Uncapped, ~100 saves of a 500 KB `cwd` would push the
13
+ * file past MAX_DRAFTS_FILE_BYTES and make the whole list unreadable.
14
+ */
15
+ export declare const MAX_DRAFT_META_CHARS = 1024;
16
+ export interface Draft {
17
+ /** Stable id: the handle for load/delete. */
18
+ id: string;
19
+ /** The message text, trimmed. Never blank. */
20
+ text: string;
21
+ /** Epoch ms first saved. */
22
+ createdAt: number;
23
+ /** Epoch ms of the last save of this same text (drives list order). */
24
+ updatedAt: number;
25
+ /** Session it was written in, if any. Display metadata only. */
26
+ sessionId?: string;
27
+ /** Working folder it was written in, if any. Display metadata only. */
28
+ cwd?: string;
29
+ }
30
+ /**
31
+ * The drafts file exists but cannot be trusted (unreadable, implausibly large,
32
+ * or not parseable). Every mutation is a read-modify-write, so this MUST abort
33
+ * the write rather than let an empty read overwrite the file: that file is the
34
+ * only copy of text the user explicitly asked to keep, and treating "I could not
35
+ * read it" as "it is empty" would delete every draft on the next save. Fail
36
+ * closed, keep the file, and tell the user which file to look at.
37
+ */
38
+ export declare class DraftsUnavailableError extends Error {
39
+ constructor(reason: string);
40
+ }
41
+ /**
42
+ * Drafts live in the STATE dir, not the config dir. They are the canonical
43
+ * example of the split: the config directory can be read-only (rendered by the
44
+ * deployment), while this file is written on every save. `WHEREVER_STATE_DIR`
45
+ * defaults to the config dir, so an install that sets neither variable (or only
46
+ * `WHEREVER_CONFIG_DIR`) keeps the exact path it has today.
47
+ */
48
+ export declare function getDraftsPath(): string;
49
+ /** Newest-updated first, ties broken by id so the order is deterministic. */
50
+ export declare function sortDrafts(drafts: Draft[]): Draft[];
51
+ /**
52
+ * Parse the file. Returns NULL when the content is not a drafts file at all
53
+ * (truncated, corrupt, some other JSON), which callers must treat as "unknown",
54
+ * never as "empty" -- see DraftsUnavailableError. A malformed MEMBER inside an
55
+ * otherwise valid file is DROPPED rather than repaired: a draft is user-authored
56
+ * text, and inventing fields for it is worse than losing the one broken record.
57
+ * Accepts both the `{version, drafts}` envelope and a bare array, so a
58
+ * hand-written file works too.
59
+ */
60
+ export declare function parseDrafts(raw: string | null | undefined): Draft[] | null;
61
+ export declare function serializeDrafts(drafts: Draft[]): string;
62
+ export interface SaveDraftInput {
63
+ text: string;
64
+ id: string;
65
+ now: number;
66
+ sessionId?: string;
67
+ cwd?: string;
68
+ }
69
+ /**
70
+ * Add `text` to the list, returning the new list (newest first).
71
+ *
72
+ * - Blank text saves nothing: a whitespace-only draft is indistinguishable from
73
+ * the others in a list and there would be no way to tell them apart.
74
+ * - Text IDENTICAL to an existing draft does NOT duplicate it: the existing
75
+ * entry is touched (updatedAt) and floats to the top. Re-saving the same
76
+ * reusable prompt is the normal case, and a wall of identical rows is noise
77
+ * the user would then have to prune by hand.
78
+ */
79
+ /**
80
+ * Validate an incoming draft, returning an error message or null.
81
+ *
82
+ * ONE place decides what is acceptable, and the route turns a message into a
83
+ * 400. Overlong text is REJECTED rather than truncated: the client clears the
84
+ * composer once the server has the draft, so silently storing a prefix would
85
+ * destroy the tail of the message with no error and no undo.
86
+ */
87
+ export declare function validateDraftInput(input: {
88
+ text: unknown;
89
+ sessionId?: unknown;
90
+ cwd?: unknown;
91
+ }): string | null;
92
+ export declare function saveDraft(drafts: Draft[], input: SaveDraftInput): Draft[];
93
+ export declare function removeDraft(drafts: Draft[], id: string): Draft[];
94
+ /**
95
+ * Read the stored list. A MISSING file is genuinely an empty list; anything else
96
+ * that goes wrong throws DraftsUnavailableError, so a mutation aborts instead of
97
+ * overwriting a file it could not read (see that class). Callers that only
98
+ * display the list can catch and show the error; none of them may swallow it and
99
+ * carry on writing.
100
+ */
101
+ export declare function readDrafts(): Draft[];
102
+ /**
103
+ * Write the list. ATOMIC (tmp file + rename) because this file is the only copy
104
+ * of text the user explicitly asked to keep: a torn write from a crash or a full
105
+ * disk would otherwise lose every draft at once, and a truncated file is exactly
106
+ * the case parseDrafts has to throw away.
107
+ *
108
+ * Sync fs on purpose (like getWhereverConfig): the file is a few KB and this
109
+ * runs on a rare, human-initiated action, so there is nothing here to block the
110
+ * event loop with. Anything that could grow (the session listing, transcripts)
111
+ * is streamed elsewhere; this is not that.
112
+ */
113
+ export declare function writeDrafts(drafts: Draft[]): void;
114
+ export declare function newDraftId(): string;
115
+ /** Save `input` and persist. Returns the new list. */
116
+ export declare function addDraft(input: Omit<SaveDraftInput, 'id' | 'now'> & Partial<Pick<SaveDraftInput, 'id' | 'now'>>): Draft[];
117
+ /** Delete by id and persist. Returns the new list. */
118
+ export declare function deleteDraft(id: string): Draft[];
119
+ //# sourceMappingURL=drafts.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"drafts.d.ts","sourceRoot":"","sources":["../src/drafts.ts"],"names":[],"mappings":"AAwBA;;;;GAIG;AACH,eAAO,MAAM,UAAU,MAAM,CAAC;AAE9B,8EAA8E;AAC9E,eAAO,MAAM,eAAe,QAAQ,CAAC;AAErC;;;;;GAKG;AACH,eAAO,MAAM,oBAAoB,OAAO,CAAC;AAKzC,MAAM,WAAW,KAAK;IACpB,6CAA6C;IAC7C,EAAE,EAAE,MAAM,CAAC;IACX,8CAA8C;IAC9C,IAAI,EAAE,MAAM,CAAC;IACb,4BAA4B;IAC5B,SAAS,EAAE,MAAM,CAAC;IAClB,uEAAuE;IACvE,SAAS,EAAE,MAAM,CAAC;IAClB,gEAAgE;IAChE,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,uEAAuE;IACvE,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAOD;;;;;;;GAOG;AACH,qBAAa,sBAAuB,SAAQ,KAAK;gBACnC,MAAM,EAAE,MAAM;CAI3B;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,IAAI,MAAM,CAEtC;AAED,6EAA6E;AAC7E,wBAAgB,UAAU,CAAC,MAAM,EAAE,KAAK,EAAE,GAAG,KAAK,EAAE,CAEnD;AAED;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,K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user chose to KEEP instead of sending.
2
+ //
3
+ // The store lives on the SERVER (`<state dir>/drafts.json`), not in a browser,
4
+ // because that is the whole point of wherever: the machine holds the state and
5
+ // any device picks it up. A draft written on a phone has to be there on the
6
+ // laptop, and clearing site data must not destroy it. (The composer's per-session
7
+ // AUTO-draft is a different thing and stays client-side: it is crash protection
8
+ // for the one text currently in the box, rewritten on every keystroke, and
9
+ // round-tripping that to the server per keystroke would be absurd.)
10
+ //
11
+ // Drafts are GLOBAL to a server, deliberately not scoped to a session: a prompt
12
+ // written while looking at one repo is often exactly what you want to send in
13
+ // another, and the home page (no session at all) is a place you want to load one.
14
+ // The originating session/cwd is retained as DISPLAY metadata only.
15
+ //
16
+ // This module is the ONE writer of the list: the client never merges, caps or
17
+ // dedupes a list of its own (it would be a second writer with no conflict rule,
18
+ // and the two would drift). Every mutation returns the whole new list, which the
19
+ // client adopts verbatim.
20
+ import fs from 'node:fs';
21
+ import path from 'node:path';
22
+ import { getWhereverStateDir } from './session-pool.js';
23
+ /**
24
+ * Hard cap on retained drafts, dropping the least recently touched. A draft is
25
+ * kept forever by definition, so without a cap this file is the one thing here
26
+ * that grows without bound.
27
+ */
28
+ export const MAX_DRAFTS = 100;
29
+ /** Per-draft character cap. A composer message is prose, not a file paste. */
30
+ export const MAX_DRAFT_CHARS = 20000;
31
+ /**
32
+ * Cap on the display metadata (`sessionId` / `cwd`). These are not user prose,
33
+ * they are values the client already holds, so anything longer is a client bug
34
+ * or an abuse attempt. Uncapped, ~100 saves of a 500 KB `cwd` would push the
35
+ * file past MAX_DRAFTS_FILE_BYTES and make the whole list unreadable.
36
+ */
37
+ export const MAX_DRAFT_META_CHARS = 1024;
38
+ /** Max bytes read from drafts.json. Belt-and-braces against a corrupt file. */
39
+ const MAX_DRAFTS_FILE_BYTES = 5 * 1024 * 1024;
40
+ /**
41
+ * The drafts file exists but cannot be trusted (unreadable, implausibly large,
42
+ * or not parseable). Every mutation is a read-modify-write, so this MUST abort
43
+ * the write rather than let an empty read overwrite the file: that file is the
44
+ * only copy of text the user explicitly asked to keep, and treating "I could not
45
+ * read it" as "it is empty" would delete every draft on the next save. Fail
46
+ * closed, keep the file, and tell the user which file to look at.
47
+ */
48
+ export class DraftsUnavailableError extends Error {
49
+ constructor(reason) {
50
+ super(`${getDraftsPath()} could not be read (${reason}). Drafts left untouched.`);
51
+ this.name = 'DraftsUnavailableError';
52
+ }
53
+ }
54
+ /**
55
+ * Drafts live in the STATE dir, not the config dir. They are the canonical
56
+ * example of the split: the config directory can be read-only (rendered by the
57
+ * deployment), while this file is written on every save. `WHEREVER_STATE_DIR`
58
+ * defaults to the config dir, so an install that sets neither variable (or only
59
+ * `WHEREVER_CONFIG_DIR`) keeps the exact path it has today.
60
+ */
61
+ export function getDraftsPath() {
62
+ return path.join(getWhereverStateDir(), 'drafts.json');
63
+ }
64
+ /** Newest-updated first, ties broken by id so the order is deterministic. */
65
+ export function sortDrafts(drafts) {
66
+ return [...drafts].sort((a, b) => b.updatedAt - a.updatedAt || a.id.localeCompare(b.id));
67
+ }
68
+ /**
69
+ * Parse the file. Returns NULL when the content is not a drafts file at all
70
+ * (truncated, corrupt, some other JSON), which callers must treat as "unknown",
71
+ * never as "empty" -- see DraftsUnavailableError. A malformed MEMBER inside an
72
+ * otherwise valid file is DROPPED rather than repaired: a draft is user-authored
73
+ * text, and inventing fields for it is worse than losing the one broken record.
74
+ * Accepts both the `{version, drafts}` envelope and a bare array, so a
75
+ * hand-written file works too.
76
+ */
77
+ export function parseDrafts(raw) {
78
+ if (raw == null)
79
+ return null;
80
+ if (raw.trim().length === 0)
81
+ return [];
82
+ let parsed;
83
+ try {
84
+ parsed = JSON.parse(raw);
85
+ }
86
+ catch {
87
+ return null;
88
+ }
89
+ const arr = Array.isArray(parsed)
90
+ ? parsed
91
+ : parsed && typeof parsed === 'object' && Array.isArray(parsed.drafts)
92
+ ? parsed.drafts
93
+ : null;
94
+ if (!arr)
95
+ return null;
96
+ const out = [];
97
+ for (const item of arr) {
98
+ if (!item || typeof item !== 'object')
99
+ continue;
100
+ const d = item;
101
+ if (typeof d.id !== 'string' || d.id.length === 0)
102
+ continue;
103
+ if (typeof d.text !== 'string')
104
+ continue;
105
+ const text = d.text.trim();
106
+ if (text.length === 0)
107
+ continue;
108
+ const createdAt = typeof d.createdAt === 'number' ? d.createdAt : 0;
109
+ const updatedAt = typeof d.updatedAt === 'number' ? d.updatedAt : createdAt;
110
+ out.push({
111
+ id: d.id,
112
+ text,
113
+ createdAt,
114
+ updatedAt,
115
+ ...(typeof d.sessionId === 'string' ? { sessionId: d.sessionId } : {}),
116
+ ...(typeof d.cwd === 'string' ? { cwd: d.cwd } : {}),
117
+ });
118
+ }
119
+ return sortDrafts(out);
120
+ }
121
+ export function serializeDrafts(drafts) {
122
+ const file = { version: 1, drafts };
123
+ return JSON.stringify(file, null, 2);
124
+ }
125
+ /**
126
+ * Add `text` to the list, returning the new list (newest first).
127
+ *
128
+ * - Blank text saves nothing: a whitespace-only draft is indistinguishable from
129
+ * the others in a list and there would be no way to tell them apart.
130
+ * - Text IDENTICAL to an existing draft does NOT duplicate it: the existing
131
+ * entry is touched (updatedAt) and floats to the top. Re-saving the same
132
+ * reusable prompt is the normal case, and a wall of identical rows is noise
133
+ * the user would then have to prune by hand.
134
+ */
135
+ /**
136
+ * Validate an incoming draft, returning an error message or null.
137
+ *
138
+ * ONE place decides what is acceptable, and the route turns a message into a
139
+ * 400. Overlong text is REJECTED rather than truncated: the client clears the
140
+ * composer once the server has the draft, so silently storing a prefix would
141
+ * destroy the tail of the message with no error and no undo.
142
+ */
143
+ export function validateDraftInput(input) {
144
+ if (typeof input.text !== 'string' || input.text.trim().length === 0) {
145
+ return 'A draft cannot be empty';
146
+ }
147
+ if (input.text.trim().length > MAX_DRAFT_CHARS) {
148
+ return `A draft cannot exceed ${MAX_DRAFT_CHARS} characters`;
149
+ }
150
+ for (const [field, value] of [
151
+ ['sessionId', input.sessionId],
152
+ ['cwd', input.cwd],
153
+ ]) {
154
+ if (value === undefined || value === null)
155
+ continue;
156
+ if (typeof value !== 'string')
157
+ return `${field} must be a string`;
158
+ if (value.length > MAX_DRAFT_META_CHARS) {
159
+ return `${field} cannot exceed ${MAX_DRAFT_META_CHARS} characters`;
160
+ }
161
+ }
162
+ return null;
163
+ }
164
+ export function saveDraft(drafts, input) {
165
+ const text = input.text.trim();
166
+ if (text.length === 0 || text.length > MAX_DRAFT_CHARS)
167
+ return sortDrafts(drafts);
168
+ const existing = drafts.find((d) => d.text === text);
169
+ const next = existing
170
+ ? drafts.map((d) => (d.id === existing.id ? { ...d, updatedAt: input.now } : d))
171
+ : [
172
+ {
173
+ id: input.id,
174
+ text,
175
+ createdAt: input.now,
176
+ updatedAt: input.now,
177
+ ...(input.sessionId ? { sessionId: input.sessionId } : {}),
178
+ ...(input.cwd ? { cwd: input.cwd } : {}),
179
+ },
180
+ ...drafts,
181
+ ];
182
+ return sortDrafts(next).slice(0, MAX_DRAFTS);
183
+ }
184
+ export function removeDraft(drafts, id) {
185
+ return sortDrafts(drafts.filter((d) => d.id !== id));
186
+ }
187
+ /**
188
+ * Read the stored list. A MISSING file is genuinely an empty list; anything else
189
+ * that goes wrong throws DraftsUnavailableError, so a mutation aborts instead of
190
+ * overwriting a file it could not read (see that class). Callers that only
191
+ * display the list can catch and show the error; none of them may swallow it and
192
+ * carry on writing.
193
+ */
194
+ export function readDrafts() {
195
+ const file = getDraftsPath();
196
+ let raw;
197
+ try {
198
+ if (!fs.existsSync(file))
199
+ return [];
200
+ const stat = fs.statSync(file);
201
+ if (stat.size > MAX_DRAFTS_FILE_BYTES) {
202
+ throw new DraftsUnavailableError(`${stat.size} bytes, implausibly large`);
203
+ }
204
+ raw = fs.readFileSync(file, 'utf8');
205
+ }
206
+ catch (err) {
207
+ if (err instanceof DraftsUnavailableError)
208
+ throw err;
209
+ throw new DraftsUnavailableError(err?.message || 'read failed');
210
+ }
211
+ const parsed = parseDrafts(raw);
212
+ if (parsed === null) {
213
+ throw new DraftsUnavailableError('not a valid drafts file');
214
+ }
215
+ return parsed;
216
+ }
217
+ /**
218
+ * Write the list. ATOMIC (tmp file + rename) because this file is the only copy
219
+ * of text the user explicitly asked to keep: a torn write from a crash or a full
220
+ * disk would otherwise lose every draft at once, and a truncated file is exactly
221
+ * the case parseDrafts has to throw away.
222
+ *
223
+ * Sync fs on purpose (like getWhereverConfig): the file is a few KB and this
224
+ * runs on a rare, human-initiated action, so there is nothing here to block the
225
+ * event loop with. Anything that could grow (the session listing, transcripts)
226
+ * is streamed elsewhere; this is not that.
227
+ */
228
+ export function writeDrafts(drafts) {
229
+ const file = getDraftsPath();
230
+ const dir = path.dirname(file);
231
+ fs.mkdirSync(dir, { recursive: true });
232
+ const tmp = path.join(dir, `.drafts.json.${process.pid}.tmp`);
233
+ try {
234
+ // 0600: these are the user's unsent private words, not shared config.
235
+ fs.writeFileSync(tmp, serializeDrafts(drafts), { encoding: 'utf8', mode: 0o600 });
236
+ fs.renameSync(tmp, file);
237
+ }
238
+ catch (err) {
239
+ try {
240
+ fs.unlinkSync(tmp);
241
+ }
242
+ catch { }
243
+ throw err;
244
+ }
245
+ }
246
+ export function newDraftId() {
247
+ try {
248
+ return globalThis.crypto.randomUUID();
249
+ }
250
+ catch {
251
+ return `${Date.now()}-${Math.random().toString(36).slice(2, 10)}`;
252
+ }
253
+ }
254
+ /** Save `input` and persist. Returns the new list. */
255
+ export function addDraft(input) {
256
+ const next = saveDraft(readDrafts(), {
257
+ text: input.text,
258
+ id: input.id ?? newDraftId(),
259
+ now: input.now ?? Date.now(),
260
+ sessionId: input.sessionId,
261
+ cwd: input.cwd,
262
+ });
263
+ writeDrafts(next);
264
+ return next;
265
+ }
266
+ /** Delete by id and persist. Returns the new list. */
267
+ export function deleteDraft(id) {
268
+ const next = removeDraft(readDrafts(), id);
269
+ writeDrafts(next);
270
+ return next;
271
+ }
272
+ //# sourceMappingURL=drafts.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"drafts.js","sourceRoot":"","sources":["../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