Repository navigation
Expand file tree
/
Copy pathlib.js
More file actions
844 lines (775 loc) · 42.9 KB
/
Copy pathlib.js
File metadata and controls
844 lines (775 loc) · 42.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
/* DeepSeek Kit - pure logic (no DOM, no browser APIs), shared by content.js and the Node tests. */
(function (root) {
'use strict';
// ---------- Pure logic (unit-testable) ----------
const MARKER_START = '[DeepSeek Kit]';
const MARKER_END = '[End of Instructions]';
const BUILTIN_DEFAULT_PROMPT = [
'You are using a DeepSeek Kit extension. Today is {{date}} ({{weekday}}), local time {{time}} {{timezone}}.',
'',
'## Answering',
'- If instructions conflict, prioritize: honesty and safety > answering the actual question > brevity > formatting.',
'- Lead with the answer. Yes/no questions open with yes or no; how-to questions with the method; analysis with the conclusion. Supporting detail follows only as far as it earns its place.',
'- Calibrate length to the question, not the topic. A simple question gets one to three sentences; a complex one gets structure. Do not pad, and do not truncate substance.',
'- If you are unsure, say so in one sentence, then give your best estimate, a confidence label (low/medium/high), and what would change it. Do not guess silently.',
'- Make a reasonable assumption and state it. Ask at most one short clarifying question, and only when a wrong assumption would likely make the answer materially wrong or useless; otherwise proceed.',
'- Work to completion. Deliver the finished result, not a plan for it, unless a plan was requested.',
'- For opinion questions, give a clear view and brief reasoning; don\'t hide behind neutrality.',
'- Before sending, check the output against the request: does it answer what was asked, at the requested depth, in the requested form?',
'',
'## Formatting',
'- Plain prose is the default. Use lists for parallel items, tables for comparisons, headings for long structured answers.',
'- Put code, commands, config, and file contents in fenced blocks with a language tag. Code should be complete and runnable as written unless a sketch was explicitly requested.',
'- When a reply contains several files, make the first line of each file\'s block a comment with its path, e.g. // file: src/app.js, # file: main.py, <!-- file: index.html -->, /* file: style.css */. Skip this for JSON and other formats that cannot hold comments.',
'- Reply in the language the user writes in. Keep identifiers and proper nouns in their original form.',
'',
'## Tone',
'- Match the user\'s register. Be direct but not terse. No filler ("Certainly," "Great question," "I hope this helps"), no sycophancy, and no moralizing unless asked or genuinely necessary.',
'- Own mistakes plainly: name the error, correct it, move on. Do not over-apologize.',
'- If the user is wrong about a fact that matters, say so and explain why, briefly.',
'',
'DeepSeek Kit Github Repository: https://github.com/Zeroichi0040/deepseek-kit',
].join('\n');
const VISUALS_PROMPT = [
'When a chart or an interactive demo would explain something better than text, put it in a fenced code block whose language is chart or viz. The chat draws it automatically, so do not also describe the code.',
'- chart: one JSON object: {"type":"bar|line|area|scatter|pie|donut","title":"...","subtitle":"one-line takeaway","labels":["Jan","Feb"],"series":[{"name":"Calls","values":[12,19]}],"xTitle":"","yTitle":"","unit":"","stacked":false}. For scatter, give series[].points as [[x,y],...] and no labels. Bars always start at zero, so use line or scatter for years and other values that sit far from zero. Use only numbers you were given or actually know; never invent data.',
'- viz: one self-contained HTML snippet (HTML, CSS and JS together) for simulations, calculators and interactive diagrams. It runs in an isolated frame with no network: no external scripts, fonts, images or libraries. Ready-made classes: v-card, v-title, v-btn, v-btn-outline, v-row, v-control-group, v-label, v-stat; buttons, sliders, inputs and selects are already styled. Keep the background transparent so it reads on dark and light pages, never size anything with vh, and keep it simple.',
'- Use ordinary markdown for anything else, and do not make a visual when plain text answers the question.',
].join('\n');
const ASK_PROMPT = [
'When you need the user to choose between options or answer a few short questions before you can go on, put them in a fenced code block whose language is ask. The chat turns it into a form the user can click, so do not also write the questions out.',
'Use one JSON object: {"questions":[{"question":"What kind of app?","type":"single","options":[{"label":"Web","recommended":true},"Desktop","Mobile"]},{"question":"Which features?","type":"multi","options":[{"label":"Login","description":"email and password"},"Dashboard"]},{"question":"Anything else?","type":"text"}]}. type is single (pick one), multi (pick several) or text (a free answer). Choice questions get an Other field automatically; add "other":false to remove it. When one option is clearly the better choice, list it first and add "recommended":true to it; mark at most one per question, and only with a real reason. Ask only what you need (usually 1 to 4 questions), keep options short, and use plain text for ordinary questions that need no choices.',
].join('\n');
const DEFAULTS = {
enabled: true,
showInjected: false, // false = attach context automatically and hide it in the chat
autoInsertOnEnter: true,
memoryEnabled: true,
showSummary: true, // the "next message includes" strip in the drawer
showChips: true, // the chip row in the message box toolbar
prompts: [], // [{ id, name, text, enabled, schedule, everyN }]
defaultEnabled: true, // built-in default prompt
defaultText: '', // '' = use the built-in text
defaultSchedule: 'first',
defaultEveryN: 5,
snippets: [], // [{ id, name, text }]
memoryBudget: 0, // max tokens of memory per message, 0 = no limit
codeTools: true, // header buttons (collapse, ZIP) on code blocks
collapseThinking: false, // close DeepThink reasoning blocks, while thinking and once finished (also after a refresh)
lineNumbers: true, // number the lines of code blocks (CSS counters, never part of copied text)
mdPreview: true, // Preview button on markdown code blocks
collapseLong: true, // collapse long code blocks automatically
collapseLines: 20, // blocks longer than this many lines count as long
collapseAlways: false, // collapse every code block (of more than 3 lines); takes over from collapseLong while on
visualsEnabled: true, // draw chart and viz code blocks as inline cards
visualsPrompt: true, // built-in "Visuals" prompt that teaches the model the two formats
visualsText: '', // '' = use the built-in text
visualsSchedule: 'first',
visualsEveryN: 5,
askPrompt: true, // built-in "Questions" prompt that teaches the model the format
askText: '', // '' = use the built-in text
askSchedule: 'first',
askEveryN: 5,
listContinue: true, // Shift+Enter in a list in the message box continues it
askAutoSend: true, // false = answers go into the message box for you to send; true = sent straight away
};
const VARIABLES = [
{ name: 'date', desc: 'Today, YYYY-MM-DD' },
{ name: 'time', desc: 'Local time, 24-hour' },
{ name: 'datetime', desc: 'Date and time' },
{ name: 'weekday', desc: 'e.g. Saturday' },
{ name: 'timezone', desc: 'e.g. Etc/UTC' },
{ name: 'message_number', desc: 'Message # in this chat' },
{ name: 'selection', desc: 'Text selected on the page' },
];
const VARIABLE_NAMES = VARIABLES.map((v) => v.name);
const WEEKDAYS = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
function newId() {
return typeof globalThis.crypto?.randomUUID === 'function' ? globalThis.crypto.randomUUID() : Date.now().toString(36) + Math.random().toString(36).slice(2) + Math.random().toString(36).slice(2);
}
// Normalizes stored settings. Older versions had a single "systemPrompt" plus a multi-prompt switch;
// now there is just a list, so a leftover single prompt becomes the first list entry.
// Lists that came from storage or a hand-edited backup: keep only real objects and coerce the fields we read.
const isObj = (v) => !!v && typeof v === 'object' && !Array.isArray(v);
// Keep valid identities, including those referenced by drawer order. Repair missing, duplicate
// and reserved identities deterministically so older data has the same IDs in every tab.
const BUILTIN_PROMPT_IDS = new Set(['default', 'ask', 'visuals']);
function identifiedRows(list, kind, reserved = new Set()) {
const rows = (Array.isArray(list) ? list : []).filter(isObj);
const idOf = row => kind === 'memory'
? (typeof row.id === 'string' && row.id.trim() ? row.id : '')
: (row.id && String(row.id).trim() ? String(row.id) : '');
const existing = new Set(rows.map(idOf).filter(Boolean));
const seen = new Set(reserved);
return rows.map((row, index) => {
let id = idOf(row);
if (!id || seen.has(id)) {
id = 'legacy-' + kind + '-' + index;
while (existing.has(id) || seen.has(id)) id += '-';
}
seen.add(id);
return { ...row, id };
});
}
function cleanPrompts(list) {
return identifiedRows(list, 'prompt', BUILTIN_PROMPT_IDS).map((p) => ({
...p,
name: String(p.name == null ? '' : p.name),
text: String(p.text == null ? '' : p.text),
enabled: p.enabled === undefined ? true : !!p.enabled,
schedule: ['first', 'always', 'every'].indexOf(p.schedule) >= 0 ? p.schedule : 'first',
everyN: Math.max(1, parseInt(p.everyN, 10) || 5),
}));
}
function cleanSnippets(list) {
return identifiedRows(list, 'snippet').map((x) => ({
...x,
name: String(x.name == null ? '' : x.name),
text: String(x.text == null ? '' : x.text),
}));
}
function cleanMemories(list) {
return identifiedRows(list, 'memory').map((m) => ({
...m,
key: String(m.key == null ? '' : m.key),
value: String(m.value == null ? '' : m.value),
importance: m.importance === 'always' || m.importance === 'first' ? m.importance : 'called',
category: String(m.category == null ? '' : m.category),
enabled: m.enabled !== false,
}));
}
// Validate all supplied data before migration or modification of state.
// Legacy partial backups are accepted; omitted top-level sections remain untouched.
function validateBackup(data) {
const has = (o, k) => Object.prototype.hasOwnProperty.call(o, k);
const fail = () => { throw new Error('Invalid backup'); };
if (!isObj(data) || (!has(data, 'settings') && !has(data, 'memories'))) fail();
const out = {};
const records = (list, fields) => Array.isArray(list) && list.every(row => isObj(row) &&
fields.every(k => typeof row[k] === 'string') &&
(!has(row, 'enabled') || typeof row.enabled === 'boolean') &&
(!has(row, 'category') || typeof row.category === 'string') &&
(!has(row, 'id') || typeof row.id === 'string'));
if (has(data, 'settings')) {
const s = data.settings;
if (!isObj(s) || !Object.keys(s).some(k => has(DEFAULTS, k) || hasLegacyKeys({ [k]: true }) || k === 'drawerOrganization')) fail();
for (const [k, type] of Object.entries({ systemPrompt: 'string', mode: 'string', everyN: 'number', multiPrompt: 'boolean', askEnabled: 'boolean', starterSeeded: 'boolean', chipsInline: 'boolean' })) {
if (has(s, k) && typeof s[k] !== type) fail();
}
for (const k of Object.keys(DEFAULTS)) {
if (!has(s, k)) continue;
const v = s[k], expected = DEFAULTS[k];
if (Array.isArray(expected)) {
if (!records(v, ['name', 'text'])) fail();
if (k === 'prompts' && v.some(row => (has(row, 'schedule') && !['first','always','every'].includes(row.schedule)) ||
(has(row, 'everyN') && (!Number.isInteger(row.everyN) || row.everyN < 1)))) fail();
} else if (typeof v !== typeof expected || (typeof v === 'number' && !Number.isFinite(v))) fail();
}
if (has(s, 'drawerOrganization') && (!isObj(s.drawerOrganization) || Object.values(s.drawerOrganization).some(entry =>
!isObj(entry) || Object.values(entry).some(value => !Array.isArray(value) || value.some(x => typeof x !== 'string'))))) fail();
out.settings = migrateSettings(s);
}
if (has(data, 'memories')) {
if (!records(data.memories, ['key','value']) || data.memories.some(m => has(m, 'importance') && !['always','first','called'].includes(m.importance))) fail();
out.memories = cleanMemories(data.memories);
}
return out;
}
// Drawer-only metadata: never used by context generation or snippet matching.
function cleanDrawerOrganization(raw) {
const src = isObj(raw) ? raw : {};
const out = {};
for (const tab of ['prompts', 'memory', 'snippets']) {
const entry = isObj(src[tab]) ? src[tab] : {};
const strings = (v) => Array.isArray(v) ? [...new Set(v.filter(x => typeof x === 'string' && x.trim()))] : [];
out[tab] = { categories: strings(entry.categories), collapsed: strings(entry.collapsed), order: strings(entry.order) };
}
return out;
}
function migrateSettings(raw) {
const s = { ...DEFAULTS, ...(isObj(raw) ? raw : {}) };
s.prompts = Array.isArray(s.prompts) ? s.prompts.slice() : [];
s.snippets = Array.isArray(s.snippets) ? s.snippets.slice() : [];
const legacy = String(s.systemPrompt || '');
if (legacy.trim()) {
s.prompts.unshift({
id: newId(),
name: s.multiPrompt ? 'Previous prompt' : 'Custom prompt',
text: legacy,
enabled: !s.multiPrompt, // in multi mode the single prompt was not in use, so keep it switched off
schedule: s.mode || 'first',
everyN: s.everyN || 5,
});
}
delete s.systemPrompt;
delete s.mode;
delete s.everyN;
delete s.multiPrompt;
delete s.starterSeeded; // starter content was removed
delete s.chipsInline; // placement is automatic now: the toolbar
delete s.askEnabled; // question forms are always drawn now; the built-in Questions prompt is the only switch
s.prompts = cleanPrompts(s.prompts);
s.snippets = cleanSnippets(s.snippets);
s.drawerOrganization = cleanDrawerOrganization(s.drawerOrganization);
return s;
}
function hasLegacyKeys(raw) {
return isObj(raw) && ['systemPrompt', 'multiPrompt', 'mode', 'everyN', 'askEnabled', 'starterSeeded', 'chipsInline'].some(k => Object.prototype.hasOwnProperty.call(raw, k));
}
// Words are runs of letters, digits and combining marks in any script. (The old class, \u00c0-\uffff, also held
// curly quotes, dashes and the ellipsis, so "\u201cPython\u201d" was one word and never matched the key "python".)
const NOT_WORD = /[^\p{L}\p{N}\p{M}]+/u;
function tokens(s) {
return new Set(
String(s || '')
.toLowerCase()
.split(NOT_WORD)
.filter((t) => t.length > 2)
);
}
// A note key matches a message when one of its words (3+ letters) appears in it, when a very short key
// ("AI", "Go", "C#") appears as a whole word, or when a key with CJK characters appears as written.
const CJK = /[\u3040-\u30ff\u3400-\u9fff\uac00-\ud7af]/;
function keyMatches(key, text, msgTokens) {
const k = String(key || '').trim().toLowerCase();
if (!k) return false;
for (const t of tokens(k)) if (msgTokens.has(t)) return true;
if (CJK.test(k)) return text.indexOf(k) !== -1;
if (k.length >= 2 && k.length <= 3) {
const esc = k.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
return new RegExp('(^|[^\\p{L}\\p{N}\\p{M}])' + esc + '($|[^\\p{L}\\p{N}\\p{M}])', 'u').test(text);
}
return false;
}
const MEMORY_GENERAL = 'General';
const MEMORY_HEADER = 'Things to remember about me:\n';
function memoryLine(m) {
return '- ' + m.key + ': ' + m.value;
}
function memoryCategory(m) {
const c = String((m && m.category) || '').trim();
return c || MEMORY_GENERAL;
}
// Decides, for every note, whether it goes into the next message and why not otherwise.
// status: 'off' (switched off), 'idle' ("when mentioned" and it was not), 'later' ("first message" note after the
// first message), 'budget' (would not fit), 'included'.
// opts = { sentCount, force }: "first message" notes are only eligible when no message has been sent in this chat yet
// (sentCount 0) or when forced (the "Insert now" button). Without opts they are never eligible.
// Priority under a token budget, in three tiers: due "first message" notes, then "always" notes, then "when
// mentioned" ones. A first-message note has a single automatic chance to go out, an always note has one on every
// message. List order is kept inside each tier; a note that does not fit the budget is skipped and smaller ones after
// it may still fit.
// Eligible is not the same as sent: this plan says which notes would go into a context insertion. Whether one is
// scheduled for the next message at all is a separate question; see memoryUsage.
function firstNoteDue(opts) {
return !!opts && (!!opts.force || opts.sentCount === 0);
}
function memoryPlan(memories, userText, budget, opts) {
const list = memories || [];
const text = String(userText || '').toLowerCase();
const msg = tokens(text);
const firstDue = firstNoteDue(opts);
const plan = list.map(() => 'idle');
const cands = [];
const rank = (m) => (m.importance === 'first' ? 0 : m.importance === 'always' ? 1 : 2);
list.forEach((m, i) => {
if (!m || !m.key || m.enabled === false) { plan[i] = 'off'; return; }
let match;
if (m.importance === 'first') {
match = firstDue;
if (!match) plan[i] = 'later';
} else {
match = m.importance === 'always';
if (!match) match = keyMatches(m.key, text, msg);
}
if (match) cands.push(i);
});
cands.sort((a, b) => rank(list[a]) - rank(list[b]) || a - b);
const limit = Number(budget) > 0 ? Number(budget) : Infinity;
const header = estimateTokens(MEMORY_HEADER);
let used = 0;
let any = false;
cands.forEach((i) => {
const total = used + estimateTokens(memoryLine(list[i]) + '\n') + (any ? 0 : header);
if (total <= limit) { plan[i] = 'included'; used = total; any = true; } else plan[i] = 'budget';
});
return { status: plan, tokens: used };
}
// The notes that go into the next message, in list order.
function selectMemories(memories, userText, budget, opts) {
const plan = memoryPlan(memories, userText, budget, opts).status;
return (memories || []).filter((m, i) => plan[i] === 'included');
}
// What the Memory tab shows for the next message.
// status per note: the memoryPlan statuses, except that 'included' becomes 'eligible' while no context insertion is
// scheduled (the note would go out with one, but nothing is due), and every note shows 'paused' (switched-off notes
// stay 'off') while memory, or the Kit as a whole, is switched off.
// tokens: what the next message will really carry. eligibleTokens: what the eligible notes would add to an insertion.
// They differ only while nothing is scheduled. `scheduled` says whether an insertion is due.
// opts = { sentCount, force } as for memoryPlan. Without a sentCount the schedule is not consulted and an insertion
// counts as scheduled; with force (Insert now) it always is.
function memoryUsage(settings, memories, userText, opts) {
const plan = memoryPlan(memories, userText, settings.memoryBudget, opts);
const live = !!settings.memoryEnabled && settings.enabled !== false;
const scheduled = !opts || !!opts.force || typeof opts.sentCount !== 'number' || shouldInject(settings, opts.sentCount, memories);
return {
status: plan.status.map((st) => (!live ? (st === 'off' ? 'off' : 'paused') : st === 'included' && !scheduled ? 'eligible' : st)),
scheduled: scheduled,
tokens: live && scheduled ? plan.tokens : 0,
eligibleTokens: live ? plan.tokens : 0,
};
}
// Per-note status for the Memory tab (see memoryUsage).
function memoryStatuses(settings, memories, userText, opts) {
return memoryUsage(settings, memories, userText, opts).status;
}
// Tokens of memory the next message will really carry (see memoryUsage).
function memoryTokens(settings, memories, userText, opts) {
return memoryUsage(settings, memories, userText, opts).tokens;
}
// True when a "first message" note (enabled, with a key and a value) is waiting for the first message of a chat.
function firstNoteWaiting(settings, memories, sentCount) {
if (!settings.memoryEnabled || sentCount !== 0) return false;
return (memories || []).some((m) => m && m.importance === 'first' && m.enabled !== false && String(m.key || '').trim() && String(m.value || '').trim());
}
// Groups notes by category: General first, then alphabetical. Items keep their index in the full list.
function groupMemories(memories) {
const map = new Map();
(memories || []).forEach((m, index) => {
if (!m) return;
const c = memoryCategory(m);
if (!map.has(c)) map.set(c, []);
map.get(c).push({ m: m, index: index });
});
const names = Array.from(map.keys()).sort((a, b) => (a === MEMORY_GENERAL ? -1 : b === MEMORY_GENERAL ? 1 : a.toLowerCase().localeCompare(b.toLowerCase())));
return names.map((n) => ({ category: n, items: map.get(n) }));
}
// Reuses the spelling of an existing category when the new one matches ignoring case ("work" -> "Work").
function cleanCategory(input, memories) {
const c = String(input || '').trim().replace(/\s+/g, ' ');
if (!c || c.toLowerCase() === MEMORY_GENERAL.toLowerCase()) return '';
const hit = (memories || []).map((m) => String((m && m.category) || '').trim()).find((x) => x && x.toLowerCase() === c.toLowerCase());
return hit || c;
}
// sentCount = how many messages have already been sent in this chat.
function scheduleDue(mode, everyN, sentCount) {
if (mode === 'always') return true;
if (mode === 'every') {
const n = Math.max(1, parseInt(everyN, 10) || 1);
return sentCount % n === 0;
}
return sentCount === 0; // 'first'
}
function pad(n) { return String(n).padStart(2, '0'); }
function localTimezone() {
try { return Intl.DateTimeFormat().resolvedOptions().timeZone || 'UTC'; } catch (e) { return 'UTC'; }
}
// Replaces {{date}}, {{time}}, ... Unknown variables are left untouched.
function applyVariables(text, ctx) {
ctx = ctx || {};
const d = ctx.now || new Date();
const vars = {
date: d.getFullYear() + '-' + pad(d.getMonth() + 1) + '-' + pad(d.getDate()),
time: pad(d.getHours()) + ':' + pad(d.getMinutes()),
weekday: WEEKDAYS[d.getDay()],
timezone: ctx.timezone || localTimezone(),
message_number: String(ctx.messageNumber || 1),
selection: ctx.selection || '',
};
vars.datetime = vars.date + ' ' + vars.time;
return String(text == null ? '' : text).replace(/\{\{\s*([a-z_]+)\s*\}\}/gi, (m, k) => {
k = k.toLowerCase();
return Object.prototype.hasOwnProperty.call(vars, k) ? vars[k] : m;
});
}
function defaultPromptText(settings) {
return (settings.defaultText || '').trim() ? settings.defaultText : BUILTIN_DEFAULT_PROMPT;
}
function visualsPromptText(settings) {
return (settings.visualsText || '').trim() ? settings.visualsText : VISUALS_PROMPT;
}
function askPromptText(settings) {
return (settings.askText || '').trim() ? settings.askText : ASK_PROMPT;
}
// Every prompt that may take part: the built-in default and Visuals prompts, then your own list.
function promptEntries(settings) {
const list = [];
if (settings.defaultEnabled !== false) {
list.push({ id: 'default', name: 'Default', text: defaultPromptText(settings), enabled: true, schedule: settings.defaultSchedule || 'first', everyN: settings.defaultEveryN || 5 });
}
if (settings.visualsPrompt !== false && settings.visualsEnabled !== false) {
list.push({ id: 'visuals', name: 'Visuals', text: visualsPromptText(settings), enabled: true, schedule: settings.visualsSchedule || 'first', everyN: settings.visualsEveryN || 5 });
}
if (settings.askPrompt !== false) {
list.push({ id: 'ask', name: 'Questions', text: askPromptText(settings), enabled: true, schedule: settings.askSchedule || 'first', everyN: settings.askEveryN || 5 });
}
(settings.prompts || []).forEach((p) => { if (p) list.push(p); });
return list;
}
// `memories` is optional: when given, a "first message" note also makes the first message of a chat due, even when
// no prompt is.
function shouldInject(settings, sentCount, memories) {
if (!settings.enabled) return false;
if (promptEntries(settings).some((e) => e.enabled && (e.text || '').trim() && scheduleDue(e.schedule, e.everyN, sentCount))) return true;
return firstNoteWaiting(settings, memories, sentCount);
}
// Prompts that go out with this message. `force` ignores schedules (the "Insert now" button).
function activePrompts(settings, sentCount, force) {
return promptEntries(settings)
.filter((e) => e && e.enabled && (e.text || '').trim() && (force || scheduleDue(e.schedule, e.everyN, sentCount)))
.map((e) => ({ name: (e.name || '').trim() || 'Untitled', text: e.text.trim() }));
}
function buildPrefix(settings, memories, userText, sentCount, force, ctx) {
const parts = activePrompts(settings, sentCount || 0, !!force).map((p) => 'Instructions (' + p.name + '):\n' + p.text);
if (settings.memoryEnabled) {
const sel = selectMemories(memories, userText, settings.memoryBudget, { sentCount: sentCount || 0, force: !!force });
if (sel.length) {
parts.push(MEMORY_HEADER + sel.map(memoryLine).join('\n'));
}
}
if (!parts.length) return '';
const out = MARKER_START + '\n' + parts.join('\n\n') + '\n' + MARKER_END + '\n\n';
return ctx ? applyVariables(out, ctx) : out;
}
function estimateTokens(text) {
return Math.ceil(String(text || '').length / 4);
}
// What the next message will carry, for the strip in the drawer and the chips above the message box.
function summarize(settings, memories, userText, sentCount) {
const none = { names: [], memories: 0, skipped: 0, tokens: 0 };
if (!settings.enabled) return Object.assign({ state: 'off' }, none);
if (!shouldInject(settings, sentCount, memories)) return Object.assign({ state: 'idle' }, none);
const names = activePrompts(settings, sentCount, false).map((p) => p.name);
let included = 0;
let skipped = 0;
if (settings.memoryEnabled) {
memoryPlan(memories, userText, settings.memoryBudget, { sentCount: sentCount }).status.forEach((st) => {
if (st === 'included') included++;
else if (st === 'budget') skipped++;
});
}
const prefix = buildPrefix(settings, memories, userText, sentCount, false, {});
return { state: 'ready', names: names, memories: included, skipped: skipped, tokens: estimateTokens(prefix) };
}
function memoryLabel(n) {
return n + (n === 1 ? ' memory' : ' memories');
}
function describeSummary(sum) {
if (sum.state === 'off') return 'Kit is off';
if (sum.state === 'idle') return 'Nothing is due for the next message';
let t = sum.names.join(', ');
if (sum.memories) t += (t ? ' + ' : '') + memoryLabel(sum.memories);
if (sum.skipped) t += ' (' + sum.skipped + ' over budget)';
return 'Next message includes ' + t;
}
function alreadyHasContext(text) {
return String(text || '').trimStart().startsWith(MARKER_START);
}
// Removes the inserted block (used to hide it in the chat view when "show inserted context" is off).
function stripContext(text) {
const escape = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
return String(text || '').replace(new RegExp(escape(MARKER_START) + '[\\s\\S]*?' + escape(MARKER_END) + '\\s*'), '');
}
// "/foo" (and nothing else) in the message box opens the snippet picker.
// Shift+Enter in a list: carry the list on to the next line, or leave the list when the item is empty. Pure: takes the
// box text and caret and returns null (let the key do its normal newline) or { from, to, insert }, meaning "replace
// value[from..to] with insert". Only acts with a plain caret at the very end of a line, and never inside a code fence.
function continueList(value, start, end) {
value = String(value == null ? '' : value);
if (start !== end || start < 0 || start > value.length) return null;
const lineStart = value.lastIndexOf('\n', start - 1) + 1;
let lineEnd = value.indexOf('\n', start);
if (lineEnd < 0) lineEnd = value.length;
if (lineEnd !== start) return null;
const fences = value.slice(0, lineStart).split('\n').filter((l) => /^\s*(```|~~~)/.test(l)).length;
if (fences % 2 === 1) return null; // inside a code block the text is meant literally
const line = value.slice(lineStart, lineEnd);
const m = /^([ \t]*)(>|[-*+]|(\d{1,9})([.)]))([ \t]+)(.*)$/.exec(line);
if (!m) return null;
if (!m[6].trim()) return { from: lineStart, to: lineEnd, insert: '' }; // an empty item: the list ends here
let marker = m[2];
if (m[3] !== undefined) {
const next = parseInt(m[3], 10) + 1;
if (next > 999999999) return null;
marker = next + m[4];
}
return { from: start, to: start, insert: '\n' + m[1] + marker + m[5] };
}
function slashQuery(value) {
const m = /^\/([^\s\/]*)$/.exec(String(value || ''));
return m ? m[1] : null;
}
function cleanSnippetName(s) {
return String(s || '').trim().toLowerCase().replace(/\s+/g, '-').replace(/[^\p{L}\p{N}\p{M}_-]/gu, '');
}
function filterSnippets(snippets, query, limit) {
const q = String(query || '').toLowerCase();
return (snippets || [])
.filter((s) => s && s.name && String(s.name).toLowerCase().includes(q))
.sort((a, b) => {
const as = a.name.toLowerCase().startsWith(q) ? 0 : 1;
const bs = b.name.toLowerCase().startsWith(q) ? 0 : 1;
return as - bs || a.name.localeCompare(b.name);
})
.slice(0, limit || 8);
}
// Drawer search box: matches the name or the text, keeps your order.
function searchSnippets(snippets, query) {
const q = String(query || '').trim().toLowerCase();
if (!q) return (snippets || []).filter(Boolean);
return (snippets || []).filter((s) => s && ((s.name || '').toLowerCase().includes(q) || (s.text || '').toLowerCase().includes(q)));
}
function describeSchedule(schedule, everyN) {
if (schedule === 'always') return 'Every message';
if (schedule === 'every') return 'Every ' + Math.max(1, parseInt(everyN, 10) || 1) + ' messages';
return 'First message';
}
// ---------- Code blocks: language detection, file names, ZIP ----------
const LANG_EXT = {
javascript: 'js', typescript: 'ts', python: 'py', bash: 'sh', html: 'html', css: 'css', scss: 'scss', json: 'json',
markdown: 'md', yaml: 'yaml', xml: 'xml', svg: 'svg', mermaid: 'mmd', sql: 'sql', java: 'java', c: 'c', cpp: 'cpp',
csharp: 'cs', go: 'go', rust: 'rs', php: 'php', ruby: 'rb', swift: 'swift', kotlin: 'kt', powershell: 'ps1',
toml: 'toml', ini: 'ini', csv: 'csv', text: 'txt', diff: 'diff', lua: 'lua', r: 'r', dart: 'dart', perl: 'pl',
scala: 'scala', haskell: 'hs', vue: 'vue', latex: 'tex', batch: 'bat',
};
const LANG_ALIASES = {
js: 'javascript', jsx: 'javascript', mjs: 'javascript', node: 'javascript', ts: 'typescript', tsx: 'typescript',
py: 'python', python3: 'python', sh: 'bash', shell: 'bash', zsh: 'bash', console: 'bash', htm: 'html', xhtml: 'html',
md: 'markdown', yml: 'yaml', 'c++': 'cpp', 'c#': 'csharp', cs: 'csharp', rs: 'rust', rb: 'ruby', golang: 'go',
ps1: 'powershell', kt: 'kotlin', txt: 'text', plaintext: 'text', plain: 'text', tex: 'latex', bat: 'batch', cmd: 'batch',
};
const MERMAID_START = /^(graph|flowchart|sequenceDiagram|classDiagram|stateDiagram(-v2)?|erDiagram|gantt|pie|journey|gitGraph|mindmap|timeline|quadrantChart|requirementDiagram|C4Context|sankey-beta|xychart-beta)\b/;
function normalizeLang(s) {
const t = String(s || '').trim().toLowerCase().replace(/^(language|lang)-/, '');
return LANG_ALIASES[t] || t;
}
// "language-html" -> "html"
function langFromClass(className) {
const m = /(?:^|\s)(?:language|lang)-([\w+#.-]+)/i.exec(String(className || ''));
return m ? normalizeLang(m[1]) : '';
}
function extFor(lang) {
return LANG_EXT[lang] || 'txt';
}
// Looks at the content itself when no (useful) language label is available.
function sniffLang(text) {
const t = String(text || '').replace(/^\uFEFF/, '');
const head = t.replace(/^\s*(<\?xml[^>]*\?>\s*)?(<!--[\s\S]*?-->\s*)?/, '');
if (/^<svg[\s>]/i.test(head)) return 'svg';
if (/^\s*(<!doctype html|<html[\s>])/i.test(t)) return 'html';
const first = t.split(/\r?\n/).map((l) => l.trim()).find((l) => l && l.indexOf('%%') !== 0) || '';
if (MERMAID_START.test(first)) return 'mermaid';
if (/^\s*[{[]/.test(t)) { try { JSON.parse(t); return 'json'; } catch (e) { /* not json */ } }
return '';
}
function previewKind(lang) {
return lang === 'html' || lang === 'svg' || lang === 'mermaid' || lang === 'markdown' ? lang : null;
}
// hints: label candidates in priority order (class name, header text, ...). The first one that is a known
// language wins; with no usable label (or a generic one) the content is inspected instead.
function detectBlock(hints, text) {
let lang = '';
for (const hint of hints || []) {
const n = normalizeLang(hint);
if (n && Object.prototype.hasOwnProperty.call(LANG_EXT, n)) { lang = n; break; }
}
if (!lang || lang === 'text' || lang === 'xml') lang = sniffLang(text) || lang;
if (!lang) lang = 'text';
return { lang: lang, kind: previewKind(lang), ext: extFor(lang) };
}
// "dir/name.ext" (a leading ./ or ../ is allowed and cleaned later) or a dotfile such as ".gitignore".
const FILE_RE = /^(?:[\w.][\w\-. /]*\.[A-Za-z][A-Za-z0-9]{0,7}|\.[A-Za-z][\w-]*)$/;
function cleanPath(p) {
return String(p || '').trim().replace(/\\/g, '/').replace(/^[a-z]:/i, '').split('/').filter((x) => x && x !== '.' && x !== '..').join('/');
}
// Uses a file name from a comment in the first lines ("// file: app.js", "# main.py", "<!-- index.html -->"),
// otherwise snippet-N.ext.
function suggestFilename(lang, text, index) {
const lines = String(text || '').split(/\r?\n/).slice(0, 3);
for (const line of lines) {
const m = /^(?:\/\/|#|--|;|<!--|\/\*|\*)\s*(?:file(?:name)?|path)?\s*:?\s*([^\s*]+?)\s*(?:-->|\*\/)?$/i.exec(line.trim());
if (m && FILE_RE.test(m[1])) {
const name = cleanPath(m[1]);
if (name) return name;
}
}
return 'snippet-' + (index || 1) + '.' + extFor(lang);
}
// Adds -2, -3 ... before the extension when a name is already taken. `used` is a Set that gets updated.
function uniqueName(name, used) {
if (!used.has(name)) { used.add(name); return name; }
const dot = name.lastIndexOf('.');
const stem = dot > 0 ? name.slice(0, dot) : name;
const ext = dot > 0 ? name.slice(dot) : '';
let n = 2;
while (used.has(stem + '-' + n + ext)) n++;
const out = stem + '-' + n + ext;
used.add(out);
return out;
}
let crcTable = null;
function crc32(bytes) {
if (!crcTable) {
crcTable = new Uint32Array(256);
for (let n = 0; n < 256; n++) {
let c = n;
for (let k = 0; k < 8; k++) c = c & 1 ? 0xEDB88320 ^ (c >>> 1) : c >>> 1;
crcTable[n] = c >>> 0;
}
}
let crc = 0xFFFFFFFF;
for (let i = 0; i < bytes.length; i++) crc = crcTable[(crc ^ bytes[i]) & 0xFF] ^ (crc >>> 8);
return (crc ^ 0xFFFFFFFF) >>> 0;
}
// Minimal ZIP writer (no compression). files: [{ name, data }] where data is a string or Uint8Array.
function makeZip(files, date) {
const enc = new TextEncoder();
const d = date || new Date();
const dosTime = (d.getHours() << 11) | (d.getMinutes() << 5) | (d.getSeconds() >> 1);
const dosDate = (Math.max(0, d.getFullYear() - 1980) << 9) | ((d.getMonth() + 1) << 5) | d.getDate();
const parts = [];
const central = [];
let offset = 0;
files.forEach((f) => {
const name = enc.encode(f.name);
const data = typeof f.data === 'string' ? enc.encode(f.data) : f.data;
const crc = crc32(data);
const local = new DataView(new ArrayBuffer(30));
local.setUint32(0, 0x04034b50, true);
local.setUint16(4, 20, true);
local.setUint16(6, 0x0800, true); // names are UTF-8
local.setUint16(8, 0, true); // stored
local.setUint16(10, dosTime, true);
local.setUint16(12, dosDate, true);
local.setUint32(14, crc, true);
local.setUint32(18, data.length, true);
local.setUint32(22, data.length, true);
local.setUint16(26, name.length, true);
local.setUint16(28, 0, true);
parts.push(new Uint8Array(local.buffer), name, data);
const cd = new DataView(new ArrayBuffer(46));
cd.setUint32(0, 0x02014b50, true);
cd.setUint16(4, 20, true);
cd.setUint16(6, 20, true);
cd.setUint16(8, 0x0800, true);
cd.setUint16(10, 0, true);
cd.setUint16(12, dosTime, true);
cd.setUint16(14, dosDate, true);
cd.setUint32(16, crc, true);
cd.setUint32(20, data.length, true);
cd.setUint32(24, data.length, true);
cd.setUint16(28, name.length, true);
cd.setUint32(42, offset, true);
central.push(new Uint8Array(cd.buffer), name);
offset += 30 + name.length + data.length;
});
const cdSize = central.reduce((n, a) => n + a.length, 0);
const end = new DataView(new ArrayBuffer(22));
end.setUint32(0, 0x06054b50, true);
end.setUint16(8, files.length, true);
end.setUint16(10, files.length, true);
end.setUint32(12, cdSize, true);
end.setUint32(16, offset, true);
const all = parts.concat(central, [new Uint8Array(end.buffer)]);
const out = new Uint8Array(all.reduce((n, a) => n + a.length, 0));
let pos = 0;
all.forEach((a) => { out.set(a, pos); pos += a.length; });
return out;
}
// A chat other than the new-chat screen ("/") already has messages in it. Used when we have no record of a chat,
// for example after a page refresh, so "first message" prompts do not fire again in the middle of a conversation.
function isExistingChatPath(path) {
const p = String(path == null ? '/' : path);
return p !== '/' && p !== '';
}
// How many messages have been sent in this chat. Unknown existing chats count as 1 (already started).
function resolveCount(counts, path) {
if (counts && Object.prototype.hasOwnProperty.call(counts, path)) {
const n = Number(counts[path]);
if (Number.isFinite(n) && n >= 0) return Math.floor(n);
}
return isExistingChatPath(path) ? 1 : 0;
}
// Adds this tab's not-yet-saved increments to the stored counters. Increments, never absolute values: two tabs that
// both sent a message in the same chat each add 1, so neither is lost. A chat that gets an increment moves to the end
// (the most recently used), non-counts in the stored copy are dropped.
// startOf(path) is where a chat with no stored count begins (default 0).
function addCounts(stored, adds, startOf) {
const out = {};
const has = (o, k) => Object.prototype.hasOwnProperty.call(o, k);
if (isObj(stored)) {
Object.keys(stored).forEach((k) => {
const n = Number(stored[k]);
if (Number.isFinite(n) && n >= 0) out[k] = Math.floor(n);
});
}
if (isObj(adds)) {
Object.keys(adds).forEach((k) => {
const n = Math.floor(Number(adds[k]));
if (!Number.isFinite(n) || n <= 0) return;
const v = (has(out, k) ? out[k] : typeof startOf === 'function' ? Number(startOf(k)) || 0 : 0) + n;
delete out[k];
out[k] = v;
});
}
return out;
}
// Keeps only the most recently updated chats so the stored counters cannot grow without limit.
function pruneCounts(counts, max) {
const keys = Object.keys(counts || {});
if (keys.length <= max) return counts || {};
const out = {};
keys.slice(keys.length - max).forEach((k) => { out[k] = counts[k]; });
return out;
}
// Inserts text into `value` at the selection; returns the new value and caret position.
function insertAt(value, start, end, text) {
value = String(value || '');
start = Math.max(0, Math.min(start == null ? value.length : start, value.length));
end = Math.max(start, Math.min(end == null ? start : end, value.length));
return { value: value.slice(0, start) + text + value.slice(end), caret: start + text.length };
}
// Elapsed time for the reasoning timer, in seconds only ("23s", "86s"), like the site's own "Thought for 86 seconds".
function formatElapsed(ms) {
return Math.max(0, Math.floor((Number(ms) || 0) / 1000)) + 's';
}
// Tolerate fences, prose and trailing commas without changing quoted strings.
function looseJson(text, objectOnly = false) {
const raw = String(text == null ? '' : text).trim().replace(/^\x60\x60\x60[a-z]*\s*/i, '').replace(/\x60\x60\x60\s*$/, '').trim();
const tidy = (s) => {
let out = '', quoted = false, escaped = false;
for (let i = 0; i < s.length; i++) {
const c = s[i];
if (quoted) {
out += c;
if (escaped) escaped = false;
else if (c === '\\') escaped = true;
else if (c === '"') quoted = false;
} else if (c === '"') { quoted = true; out += c; }
else if (c !== ',' || !/^\s*[}\]]/.test(s.slice(i + 1))) out += c;
}
return out;
};
const parse = (s) => { try { return { value: JSON.parse(s) }; } catch (_) { return null; } };
let result = parse(raw) || parse(tidy(raw));
if (result) return result.value;
const starts = (objectOnly ? ['{'] : ['{', '[']).map(c => raw.indexOf(c)).filter(i => i >= 0);
const end = objectOnly ? raw.lastIndexOf('}') : Math.max(raw.lastIndexOf('}'), raw.lastIndexOf(']'));
if (starts.length && end > Math.min(...starts)) {
const candidate = raw.slice(Math.min(...starts), end + 1);
result = parse(candidate) || parse(tidy(candidate));
if (result) return result.value;
}
return undefined;
}
const core = {
looseJson, formatElapsed, DEFAULTS, BUILTIN_DEFAULT_PROMPT, VISUALS_PROMPT, ASK_PROMPT, askPromptText, cleanPrompts, cleanSnippets, cleanMemories, keyMatches, visualsPromptText, VARIABLES, VARIABLE_NAMES, migrateSettings, hasLegacyKeys, validateBackup, tokens, selectMemories,
scheduleDue, applyVariables, promptEntries, shouldInject, activePrompts, buildPrefix, estimateTokens, summarize,
describeSummary, memoryLabel, alreadyHasContext, stripContext, slashQuery, continueList, cleanSnippetName, filterSnippets,
searchSnippets, describeSchedule, insertAt, defaultPromptText, newId,
LANG_EXT, normalizeLang, langFromClass, extFor, sniffLang, previewKind, detectBlock, suggestFilename, uniqueName, crc32, makeZip,
MEMORY_GENERAL, memoryLine, memoryCategory, memoryPlan, memoryUsage, memoryStatuses, memoryTokens, firstNoteWaiting, groupMemories, cleanCategory, isExistingChatPath, resolveCount, addCounts, pruneCounts, MARKER_START, MARKER_END,
};
if (typeof module !== 'undefined' && module.exports) module.exports = core;
else root.DCK_LIB = core;
})(typeof globalThis !== 'undefined' ? globalThis : this);