Repository navigation
Expand file tree
/
Copy pathextension.js
More file actions
1787 lines (1670 loc) · 95 KB
/
Copy pathextension.js
File metadata and controls
1787 lines (1670 loc) · 95 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
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*---------------------------------------------------------------------------------------------
* LevelCode — AI (M2, feature 1: Chat with Claude)
*
* A native chat side panel. Requests go directly from the editor to the provider
* (Anthropic or local Ollama) — there is no LevelCode server in the middle. The Anthropic
* API key is stored in VS Code SecretStorage (encrypted by the OS keychain).
*--------------------------------------------------------------------------------------------*/
// @ts-check
'use strict';
const vscode = require('vscode');
const path = require('path');
const fs = require('fs');
const cp = require('child_process');
const crypto = require('crypto');
const providers = require('./providers/index');
const catalog = require('./providers/catalog');
const { resolveGateway } = require('./providers/gateway');
const { registerAiEdit } = require('./aiEdit');
const { registerLmProvider } = require('./lmProvider');
const { registerInlineComplete } = require('./inlineComplete');
const { runAgent } = require('./agent');
const { findCompactionCut, estimateMsgTokens } = require('./agentMemory');
const { registerReview } = require('./reviewSession');
const { formatDiagnosticLines, diagKey, createPreviewGate } = require('./verify');
const { loadSkills, skillsMenu, getSkillBody } = require('./skills');
const { openCustomize } = require('./customize');
const { importFromVscode } = require('./importVscode');
const { reapMcp } = require('./mcpClient');
const { userScopedSetting, isNamespacedToolName, safeCopy } = require('./mcpConfig');
const SECRET_KEY = 'levelcode.ai.anthropicKey'; // legacy Anthropic key location (kept for back-compat)
const FILE_EXCLUDES = '{**/node_modules/**,**/.git/**,**/out/**,**/dist/**,**/.vscode-test/**,**/*.map}';
const STOPWORDS = new Set(['the','and','for','with','that','this','how','does','what','where','when','why','from','into','your','you','are','was','were','will','can','could','should','would','about','have','has','its','it','the','file','code','function','please','show','tell','explain','using','use','used','there','their','then','than','they','them','some','any','all','not','but','get','set']);
const SYSTEM_PROMPT =
'You are LevelCode\'s built-in AI assistant, helping the user write and understand code inside their editor. ' +
'Be concise and practical. Use Markdown and fenced code blocks. When given a code selection as context, focus your answer on it.';
/** @type {vscode.ExtensionContext} */
let ctx;
/** @type {vscode.Webview | undefined} */
let activeWebview;
/** @type {{role:string,content:string}[]} */
let conversation = [];
/** @type {string | null} */
let pendingContext = null;
/** Files pinned as chat context (whole codebase-wide context). @type {{id:string,uri:vscode.Uri,name:string,rel:string}[]} */
let contextFiles = [];
/** @type {AbortController | null} */
let abort = null;
/** Cached "signed in to LevelCode Cloud" flag — the gateway only routes when signed in, so the footer/
* model chip must gate on this too (kept in sync by currentAccount / storeSession / accountSignOut). */
let cloudSignedIn = false;
/** Agent mode: sending runs the autonomous tool loop instead of plain chat. */
let agentMode = true;
/** Autopilot: the agent runs commands without asking (except the danger set — see commandSafety.js)
* and prefers self-verifying over pausing to ask. Initialized from config on 'ready', toggled live,
* and persisted so an explicit opt-in survives a restart. */
let autopilot = false;
/** Apply-then-review session (Keep/Undo for applied agent edits). Set in activate(). */
let review;
/** Persistent agent transcript for the session (tool calls + results), so follow-up goals
* remember prior runs. Reset by New Chat. */
let agentMessages = [];
function post(msg) { if (activeWebview) { activeWebview.postMessage(msg); } }
function aiConfig() { return vscode.workspace.getConfiguration('levelcode.ai'); }
/** Inline debug trace — prints a 🐛 DEBUG line in the chat (gated by levelcode.ai.debug). */
function dbg(label, data) { if (aiConfig().get('debug', false)) { post({ type: 'debug', label, data: data != null ? data : null }); } }
/** The currently selected provider id (settings value; `claude` is the default/legacy Anthropic). */
function currentProviderId() { return providers.normId(aiConfig().get('provider', 'claude')); }
/** SecretStorage key for a provider (Anthropic keeps its legacy location; others namespaced; noKey → null). */
function secretKeyFor(providerId) { return providers.secretStorageKey(providerId); }
/** The active model id for a provider — per-provider settings for the two legacy ones, generic `model` otherwise. */
function activeModel(cfg, providerId) {
const id = providers.normId(providerId);
if (id === 'claude') { return cfg.get('claude.model', 'claude-sonnet-4-6'); }
if (id === 'ollama') { return cfg.get('ollama.model', 'llama3.1'); }
const m = cfg.get('model', '');
if (m) { return m; }
const p = providers.getProvider(id);
return (p && p.models && p.models[0]) ? p.models[0].id : '';
}
/** The base URL for a provider — Ollama honors `ollama.url`, `custom` uses `levelcode.ai.baseURL`, else the registry default. */
function baseUrlFor(cfg, providerId) {
const id = providers.normId(providerId);
if (id === 'ollama') { return String(cfg.get('ollama.url', 'http://localhost:11434')).replace(/\/+$/, '') + '/v1'; }
if (id === 'custom') { return String(cfg.get('baseURL', '') || '').replace(/\/+$/, ''); }
const p = providers.getProvider(id);
return p ? p.baseURL : null;
}
/** Max output tokens for chat/edit (shared across providers; keeps the existing Claude setting name). */
function maxOutputTokens(cfg) { return cfg.get('claude.maxTokens', 4096); }
/** An explicitly-set `levelcode.ai.contextWindow` (user override, e.g. the Claude 1M-token beta), or undefined. */
function explicitContextWindow() {
const insp = aiConfig().inspect('contextWindow');
if (!insp) { return undefined; }
return insp.globalValue != null ? insp.globalValue
: insp.workspaceValue != null ? insp.workspaceValue
: insp.workspaceFolderValue != null ? insp.workspaceFolderValue
: undefined;
}
/** Effective context window (tokens): an explicit user override wins; otherwise the model's real window. */
function contextLimitFor(providerId, model) {
const explicit = explicitContextWindow();
if (explicit != null) { return explicit; }
return catalog.contextWindowFor(providerId, model, 200000);
}
/** The active model's context window (tokens) — drives the chat context-usage meter. */
function currentContextLimit() {
const cfg = aiConfig();
if (providerMode() === 'gateway' && cloudSignedIn) {
return contextLimitFor('openai', capsModel(gatewayModel())); // Auto → flagship window (widest it could route to)
}
const pid = currentProviderId();
return contextLimitFor(pid, activeModel(cfg, pid));
}
/** Prompt for — and store — the API key for a provider. Provider-aware copy. `noKey`/unknown → undefined. */
async function promptForKey(providerId) {
const id = (typeof providerId === 'string' && providerId) ? providerId : currentProviderId();
const p = providers.getProvider(id) || providers.getProvider('claude');
if (p.noKey) { vscode.window.showInformationMessage('LevelCode AI: ' + p.label + ' needs no API key.'); return undefined; }
const skey = secretKeyFor(p.id);
const key = await vscode.window.showInputBox({
title: 'LevelCode AI — ' + p.label + ' API Key',
prompt: 'Paste your ' + p.label + ' API key. Stored encrypted in your OS keychain — it never leaves your machine except to ' + p.label + '.',
password: true,
ignoreFocusOut: true,
placeHolder: p.kind === 'anthropic' ? 'sk-ant-…' : 'sk-…'
});
if (key && skey) { await ctx.secrets.store(skey, key.trim()); }
return key ? key.trim() : undefined;
}
/** Look up a provider's key from SecretStorage; optionally prompt if missing.
* Returns '' for a `noKey` provider (Ollama), or `undefined` if the key is missing/declined. */
async function getProviderKey(providerId, opts) {
const skey = secretKeyFor(providerId);
if (!skey) { return ''; } // noKey provider (e.g. Ollama)
let key = await ctx.secrets.get(skey);
if (!key && opts && opts.prompt) { key = await promptForKey(providerId); }
return key || undefined;
}
/** User-facing message for a failed prepProviderRequest (shared by chat + edit). */
function providerErrorMessage(req) {
if (req.reason === 'baseURL') { return 'Set a base URL for the custom OpenAI-compatible provider first (levelcode.ai.baseURL).'; }
if (req.reason === 'insecureBaseURL') { return 'Refusing to send your API key over plain http to a non-local host. Use an https base URL (or a localhost endpoint) for the custom provider.'; }
if (req.reason === 'insecureGateway') { return 'Refusing to send your LevelCode Cloud token over plain http. Set "levelcode.cloud.endpoint" to an https URL to use gateway mode.'; }
return 'No API key set for ' + req.label + '. Use the key button or “LevelCode: AI: Set API Key”.';
}
/** The configured provider routing mode ('byok' default | 'gateway'). NOTE: the key is
* `levelcode.ai.providerMode`, NOT `provider.mode` — the latter collides with the scalar
* `levelcode.ai.provider` (VS Code navigates into the string → always undefined). */
function providerMode() { return aiConfig().get('providerMode', 'gateway'); }
// ── LevelCode Cloud gateway model entitlement (mirrors backend LevelCode.gateway_model) ──────────
// The free plan runs the cheap open-weights engine; a paid plan runs the flagship. The BACKEND
// is the source of truth — it forces the model per the user's wallet — so these are the CLIENT
// reflection: they drive which model the picker/footer show and let the free tier surface an
// "Upgrade" CTA. Selecting a model here never overrides the server's plan-based decision.
const GATEWAY_FREE_MODEL = 'openai/gpt-oss-120b';
const GATEWAY_FREE_LABEL = 'gpt-oss-120b';
const GATEWAY_PRO_MODEL = 'moonshotai/kimi-k2.7-code';
const GATEWAY_PRO_LABEL = 'Kimi K2.7 Code';
// "Auto" pseudo-model: the gateway routes each turn to the cheapest engine that fits (trivial →
// open-weights, standard/agent → flagship). The backend does the routing; the client just requests it.
const GATEWAY_AUTO_MODEL = 'auto';
const GATEWAY_AUTO_LABEL = 'Auto';
/** True for a paid LevelCode Cloud plan — anything managed that isn't the free tier. */
function isPaidCloudPlan(plan) {
const p = String(plan || '').trim().toLowerCase();
return p !== '' && p !== 'free';
}
/** The stored LevelCode Cloud plan name (from the cached profile); '' when signed out/unknown. */
function cloudPlanName() {
const p = (ctx && ctx.globalState.get(ACCOUNT_PROFILE_KEY)) || {};
return p.plan || '';
}
/** The active gateway model. Free plan → the open-weights engine (only option). Paid plan → the
* user's chosen engine (`levelcode.ai.cloudModel`: flagship OR open-weights), defaulting to the
* flagship. The backend re-checks entitlement, so this can never over-reach the plan. */
function gatewayModel() {
if (!isPaidCloudPlan(cloudPlanName())) { return GATEWAY_FREE_MODEL; } // free can't pick the flagship
// Paid: the user's chosen roster model (the picker only offers LIVE ones), else the flagship.
// The backend re-checks entitlement + price-confirmation, so this can never over-reach the plan.
return aiConfig().get('cloudModel', '') || GATEWAY_PRO_MODEL;
}
/** The model id to use for CAPABILITY lookups (context window, tool support). "Auto" is a routing
* pseudo-model resolved per-turn on the server, so for caps we use the flagship — the widest window
* it could route to (avoids under-reporting the context meter). Any real id passes through. */
function capsModel(id) {
return id === GATEWAY_AUTO_MODEL ? GATEWAY_PRO_MODEL : id;
}
/** The plan model roster from the last GET /account/models fetch — powers the picker + footer labels. */
let cloudRoster = [];
/** Friendly label for a gateway model id (footer chip). Prefers the fetched roster; falls back to the
* built-in flagship/free labels, then the raw id. */
function gatewayModelLabel(id) {
if (id === GATEWAY_AUTO_MODEL) { return GATEWAY_AUTO_LABEL; }
const m = cloudRoster.find((x) => x.id === id);
if (m) { return m.label; }
if (id === GATEWAY_PRO_MODEL) { return GATEWAY_PRO_LABEL; }
if (id === GATEWAY_FREE_MODEL) { return GATEWAY_FREE_LABEL; }
return id;
}
/** Fetch the plan's model roster (entitled models + credits + ≈ turns-left). Caches it for the
* footer/picker. Best-effort; returns null when signed out / offline / not gateway. */
async function fetchCloudRoster() {
// The presence of a stored token IS the signed-in truth; don't gate on the cloudSignedIn flag, which
// can still be false mid-activation while a valid token already exists (another way the picker was
// degrading to the 2-model fallback on a fresh open).
if (providerMode() !== 'gateway' || !ctx) { return null; }
let token = await ctx.secrets.get(ACCOUNT_TOKEN_KEY);
if (!token) { return null; }
const api = cloudApiUrl();
if (!/^https:\/\//i.test(api) && !/^http:\/\/(localhost|127\.0\.0\.1)([:/]|$)/i.test(api)) { return null; }
// Last-known-good roster: a transient failure keeps the FULL model list rather than collapsing to the
// 2-model offline fallback. The per-model fields — INCLUDING "≈ turns left" — are whatever the last
// good fetch returned, so they may be slightly stale. The account-level credit BALANCE is NOT carried
// here, so pickCloudModel just omits the "$X credits left" header until the next successful fetch.
const cached = () => (cloudRoster && cloudRoster.length ? { plan: cloudPlanName(), models: cloudRoster } : null);
const get = (bearer) => fetch(api + '/api/levelcode/v1/account/models', { headers: { authorization: 'Bearer ' + bearer } });
try {
let res = await get(token);
// THE FIX: on a fresh open, last session's short-lived access token is usually EXPIRED, so this
// first call 401s. Refresh once and retry. Without it the 401 silently degrades the picker to the
// 2-model offline fallback and hides the plan's real roster (Opus, K3, …) — exactly the reported
// bug. The profile fetch and the agent loop already refresh on 401; the roster fetch didn't.
if (res.status === 401 && await refreshCloudToken()) {
// Guard the refreshed token: if it comes back falsy for any reason, retrying would send
// `Authorization: Bearer null` — noise that masks the real 401. Skip the retry instead and let
// the !res.ok path below fall back to the cached roster.
const fresh = await ctx.secrets.get(ACCOUNT_TOKEN_KEY);
if (fresh) { token = fresh; res = await get(token); }
}
if (!res.ok) { dbg('cloud.roster', { ok: false, status: res.status }); return cached(); }
const data = await res.json().catch(() => null);
// Only a payload with a real models array IS a roster. A 200 carrying an error object, a partial
// response, or a schema drift is not — returning it would make pickCloudModel see "no models" and
// collapse to the 2-model fallback despite a valid last-known-good list. Prefer the cache then.
if (data && Array.isArray(data.models)) {
cloudRoster = data.models;
// The roster carries each model's short label — refresh the footer chip so it shows
// "Opus 4.8" instead of the raw id it fell back to before the roster finished loading.
sendConfigToWebview();
return data;
}
return cached();
} catch (e) { dbg('cloud.roster', { error: String((e && e.message) || e) }); return cached(); }
}
/**
* Report a 👍/👎 reaction to LevelCode Cloud as a per-model quality signal — but ONLY on the
* metered gateway when signed in. BYOK stays fully private: the reaction is a local toggle
* and nothing leaves the machine. Best-effort + fire-and-forget (feedback is low-stakes).
*/
async function recordFeedback(rating, turnModel) {
if (rating !== 'up' && rating !== 'down') { return; }
if (providerMode() !== 'gateway' || !cloudSignedIn) { return; } // BYOK / signed out → local only
const token = ctx ? await ctx.secrets.get(ACCOUNT_TOKEN_KEY) : null;
if (!token) { return; }
const api = cloudApiUrl();
if (!/^https:\/\//i.test(api) && !/^http:\/\/(localhost|127\.0\.0\.1)([:/]|$)/i.test(api)) { return; }
// The model the RATED turn actually ran on (stamped by the webview), falling back to the
// current entitlement — so a mid-session plan change can't misattribute the vote.
const model = (typeof turnModel === 'string' && turnModel) ? turnModel : gatewayModel();
try {
await fetch(api + '/api/levelcode/v1/feedback', {
method: 'POST',
headers: { 'content-type': 'application/json', authorization: 'Bearer ' + token },
body: JSON.stringify({ rating, model })
});
dbg('feedback.post', { rating, model });
} catch (e) { dbg('feedback.post', { error: String((e && e.message) || e) }); }
}
/** True when an error thrown by a provider adapter is an auth failure (HTTP 401) — used to trigger
* a single gateway-token refresh + retry. The openai adapter throws `<label> API 401: <body>`. */
function isAuthError(e) { return /\bAPI 401\b|\b401\b.*unauthor/i.test(String((e && e.message) || e)); }
/**
* Refresh the LevelCode Cloud access token in gateway mode: POST the stored refresh token to
* {apiUrl}/api/levelcode/v1/auth/refresh (the Rails backend), store the new access token, return true.
* No-op (returns false) when not applicable (byok, signed out, no refresh token, or non-https apiUrl).
*/
async function refreshCloudToken() {
if (!ctx) { return false; }
const endpoint = cloudApiUrl();
if (!/^https:\/\//i.test(endpoint) && !/^http:\/\/(localhost|127\.0\.0\.1)([:/]|$)/i.test(endpoint)) { return false; }
const refresh = await ctx.secrets.get(ACCOUNT_REFRESH_KEY);
if (!refresh) { return false; }
try {
const res = await fetch(endpoint + '/api/levelcode/v1/auth/refresh', {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ refresh })
});
if (!res.ok) { dbg('cloud.refresh', { ok: false, status: res.status }); return false; }
const data = await res.json().catch(() => null);
const access = data && (data.access || data.token);
if (!access) { return false; }
await ctx.secrets.store(ACCOUNT_TOKEN_KEY, access);
dbg('cloud.refresh', { ok: true });
return true;
} catch (e) { dbg('cloud.refresh', { error: String((e && e.message) || e) }); return false; }
}
/** Gateway-mode token refresh (the streaming 401 retry path). Delegates to refreshCloudToken. */
async function refreshGatewayToken() {
if (providerMode() !== 'gateway') { return false; }
return refreshCloudToken();
}
/**
* Resolve everything needed to call the active provider: id, key, model, baseURL, maxTokens.
* Returns { ok:false, reason } when a required key/baseURL is missing (after optional prompting) or
* a custom endpoint would leak the key over plaintext — the caller renders providerErrorMessage().
* @returns {Promise<{ok:boolean, providerId?:string, apiKey?:string, model?:string, baseURL?:string, maxTokens?:number, label?:string, reason?:string}>}
*/
async function prepProviderRequest(opts) {
const cfg = aiConfig();
// Gateway mode: when signed in (a cloud token is present) and mode==='gateway', route AI through the
// LevelCode Cloud metered gateway (an openai-kind endpoint) instead of the user's own provider/key. Falls
// back to the BYOK path below when signed out or in byok mode — the default is untouched.
const token = ctx ? await ctx.secrets.get(ACCOUNT_TOKEN_KEY) : null;
const gw = resolveGateway({ mode: providerMode(), endpoint: cloudApiUrl(), token: token || '' });
if (gw.use) {
if (!gw.ok) { return { ok: false, providerId: 'openai', label: 'LevelCode Cloud', reason: gw.reason }; }
return {
ok: true, providerId: gw.providerId, apiKey: gw.apiKey,
// Plan-scoped model (free → gpt-oss-120b, paid → Kimi K2.7 Code). The backend
// re-forces this per the wallet, so we send the entitled model, not the BYOK one.
model: gatewayModel(),
baseURL: gw.baseURL,
maxTokens: maxOutputTokens(cfg),
label: 'LevelCode Cloud',
gateway: true
};
}
const providerId = currentProviderId();
const p = providers.getProvider(providerId) || providers.getProvider('claude');
const baseURL = baseUrlFor(cfg, providerId);
if (providerId === 'custom' && !baseURL) {
return { ok: false, providerId, label: p.label, reason: 'baseURL' };
}
let apiKey = '';
if (!p.noKey) {
apiKey = await getProviderKey(providerId, opts);
if (!apiKey) { return { ok: false, providerId, label: p.label, reason: 'key' }; }
}
if (providerId === 'custom' && apiKey && providers.isInsecureCustomUrl(baseURL)) {
return { ok: false, providerId, label: p.label, reason: 'insecureBaseURL' };
}
return {
ok: true, providerId, apiKey,
model: activeModel(cfg, providerId),
baseURL,
maxTokens: maxOutputTokens(cfg),
label: p.label
};
}
function captureSelection() {
const ed = vscode.window.activeTextEditor;
if (!ed || ed.selection.isEmpty) { return null; }
const text = ed.document.getText(ed.selection);
const lang = ed.document.languageId || '';
const name = ed.document.uri.scheme === 'file' ? path.basename(ed.document.uri.fsPath) : 'selection';
const lines = ed.selection.end.line - ed.selection.start.line + 1;
return {
block: 'Context from `' + name + '`:\n```' + lang + '\n' + text + '\n```',
label: name + ' · ' + lines + ' line' + (lines === 1 ? '' : 's')
};
}
function addSelection() {
const sel = captureSelection();
if (!sel) { vscode.window.showInformationMessage('LevelCode AI: select some code first.'); return; }
pendingContext = sel.block;
vscode.commands.executeCommand('levelcodeAi.chat.focus');
post({ type: 'context', label: sel.label });
}
function postContextFiles() {
post({ type: 'contextFiles', files: contextFiles.map((f) => ({ id: f.id, name: f.name, rel: f.rel })) });
}
function removeFileContext(id) {
contextFiles = contextFiles.filter((f) => f.id !== id);
postContextFiles();
}
/** Unified "+ Add context" picker: choose the current selection and/or files from the workspace. */
async function addContext() {
/** @type {any[]} */
const items = [];
const sel = captureSelection();
if (sel) { items.push({ label: '$(selection) Selected code', description: sel.label, _kind: 'sel', _sel: sel }); }
const uris = await vscode.workspace.findFiles('**/*', FILE_EXCLUDES, 5000);
if (sel && uris.length) { items.push({ label: 'Workspace files', kind: vscode.QuickPickItemKind.Separator }); }
for (const u of uris) {
items.push({ label: '$(file) ' + path.basename(u.fsPath), description: vscode.workspace.asRelativePath(u), _kind: 'file', _uri: u });
}
if (!items.length) { vscode.window.showInformationMessage('LevelCode AI: no workspace files to add.'); return; }
const picks = await vscode.window.showQuickPick(items, {
canPickMany: true,
matchOnDescription: true,
placeHolder: 'Add context — pick the selection and/or files (type to filter)'
});
if (!picks || !picks.length) { return; }
for (const p of picks) {
if (p._kind === 'sel') {
pendingContext = p._sel.block;
post({ type: 'context', label: p._sel.label });
} else if (p._kind === 'file') {
const id = p._uri.fsPath;
if (!contextFiles.find((f) => f.id === id)) {
contextFiles.push({ id, uri: p._uri, name: path.basename(id), rel: vscode.workspace.asRelativePath(p._uri) });
}
}
}
vscode.commands.executeCommand('levelcodeAi.chat.focus');
postContextFiles();
}
/** Read pinned context files as content blocks, capped to protect the context window. */
async function contextFileBlocks() {
const PER_FILE = 80 * 1024;
const TOTAL = 250 * 1024;
const blocks = [];
let used = 0;
for (const f of contextFiles) {
try {
const doc = await vscode.workspace.openTextDocument(f.uri);
let body = doc.getText();
if (body.length > PER_FILE) { body = body.slice(0, PER_FILE) + '\n…(file truncated for context)…'; }
if (used + body.length > TOTAL) { blocks.push('…(some pinned files omitted to fit the context window)…'); break; }
used += body.length;
blocks.push('File `' + f.rel + '`:\n```' + (doc.languageId || '') + '\n' + body + '\n```');
} catch { /* unreadable/binary file — skip */ }
}
return blocks;
}
/** List workspace files once (cached per send), respecting the standard excludes. */
async function listWorkspaceFiles() {
if (!vscode.workspace.workspaceFolders || !vscode.workspace.workspaceFolders.length) { return []; }
return vscode.workspace.findFiles('**/*', FILE_EXCLUDES, 5000);
}
/** Identifiers/keywords from a question, minus common stop-words. */
function extractKeywords(text) {
const raw = text.match(/[A-Za-z_][A-Za-z0-9_]{2,}/g) || [];
const seen = new Set();
const out = [];
for (const w of raw) {
const lw = w.toLowerCase();
if (STOPWORDS.has(lw) || seen.has(lw)) { continue; }
seen.add(lw);
out.push(w);
if (out.length >= 12) { break; }
}
return out;
}
/** Resolve the ripgrep binary bundled with the editor (dev and packaged paths differ). */
let _rgPath; // cached: string | null
function rgPath() {
if (_rgPath !== undefined) { return _rgPath; }
const root = vscode.env.appRoot;
const candidates = [
path.join(root, 'node_modules', '@vscode', 'ripgrep', 'bin', 'rg'),
path.join(root, 'node_modules', '@vscode', 'ripgrep-universal', 'bin', process.platform + '-' + process.arch, 'rg'),
path.join(root, 'node_modules.asar.unpacked', '@vscode', 'ripgrep', 'bin', 'rg')
];
_rgPath = candidates.find((c) => { try { return fs.existsSync(c); } catch { return false; } }) || null;
return _rgPath;
}
/** Files (absolute paths) under cwd whose CONTENT contains the literal term. Empty on any failure. */
function rgFiles(term, cwd) {
return new Promise((resolve) => {
const bin = rgPath();
if (!bin || !cwd) { resolve([]); return; }
const args = [
'--files-with-matches', '--no-messages', '--no-config', '-i', '-F',
'--max-filesize', '1M', '--max-count', '1',
'-g', '!**/node_modules/**', '-g', '!**/.git/**', '-g', '!**/out/**',
'-g', '!**/dist/**', '-g', '!**/*.map', '-g', '!**/*.min.*',
'-e', term, '.'
];
let out = '';
let done = false;
const finish = (paths) => { if (!done) { done = true; resolve(paths); } };
try {
const child = cp.spawn(bin, args, { cwd });
const timer = setTimeout(() => { try { child.kill(); } catch { /* noop */ } finish([]); }, 4000);
child.stdout.on('data', (d) => { out += d.toString(); });
child.on('error', () => { clearTimeout(timer); finish([]); });
child.on('close', () => {
clearTimeout(timer);
finish(out.split('\n').map((s) => s.trim()).filter(Boolean).map((rel) => path.resolve(cwd, rel)));
});
} catch { finish([]); }
});
}
/** Scope retrieval to the active file's top-level sub-project, so unrelated sibling trees
* (e.g. a vendored source dump) don't pollute results. Returns the search dir + rel prefix. */
function activeProjectScope() {
const folders = vscode.workspace.workspaceFolders || [];
const fallback = { dir: folders.length ? folders[0].uri.fsPath : '', prefix: '' };
if (!aiConfig().get('chat.scopeToActiveProject', true)) { return fallback; }
const ed = vscode.window.activeTextEditor;
if (!ed || ed.document.uri.scheme !== 'file') { return fallback; }
const wsFolder = vscode.workspace.getWorkspaceFolder(ed.document.uri);
if (!wsFolder) { return fallback; }
const rel = path.relative(wsFolder.uri.fsPath, ed.document.uri.fsPath);
if (!rel || rel.startsWith('..')) { return fallback; }
const top = rel.split(path.sep)[0];
const topPath = path.join(wsFolder.uri.fsPath, top);
try { if (!fs.statSync(topPath).isDirectory()) { return fallback; } } catch { return fallback; }
return { dir: topPath, prefix: top + '/' };
}
/** A specific identifier (long or camelCase) is a much stronger relevance signal than a common word. */
function contentWeight(kw) {
return (kw.length >= 8 || /[A-Z]/.test(kw.slice(1))) ? 6 : 3;
}
/**
* Auto-discover files relevant to the question via ripgrep content search (primary),
* filename matches, and workspace symbols — scoped to the active sub-project. Returns their
* contents as context blocks (capped). Skips the active & pinned files; thresholds out noise.
* @returns {Promise<{blocks:string[], names:string[]}>}
*/
async function gatherAutoContext(question, allFiles) {
const cfg = aiConfig();
const keywords = extractKeywords(question);
if (!keywords.length) { return { blocks: [], names: [] }; }
const scope = activeProjectScope();
const inScope = (uri) => !scope.prefix || vscode.workspace.asRelativePath(uri).startsWith(scope.prefix);
/** @type {Map<string,{uri:vscode.Uri,score:number}>} */
const score = new Map();
const bump = (uri, s) => {
const k = uri.fsPath;
const cur = score.get(k);
if (cur) { cur.score += s; } else { score.set(k, { uri, score: s }); }
};
// 1. CONTENT search via ripgrep (primary signal — finds files by what's inside them).
const terms = keywords.slice(0, 6);
const hits = await Promise.all(terms.map((kw) => rgFiles(kw, scope.dir)));
for (let i = 0; i < terms.length; i++) {
for (const abs of hits[i]) { bump(vscode.Uri.file(abs), contentWeight(terms[i])); }
}
// 2. filename matches (scoped).
for (const u of allFiles) {
if (!inScope(u)) { continue; }
const base = path.basename(u.fsPath).toLowerCase();
for (const kw of keywords) { if (kw.length >= 3 && base.includes(kw.toLowerCase())) { bump(u, 4); } }
}
// 3. workspace symbols (scoped; uses language-server indexes when present).
for (const kw of terms) {
let syms = [];
try { syms = await vscode.commands.executeCommand('vscode.executeWorkspaceSymbolProvider', kw) || []; } catch { syms = []; }
for (const sym of syms.slice(0, 20)) {
const uri = sym && sym.location && sym.location.uri;
if (!uri || !inScope(uri)) { continue; }
const exact = sym.name && sym.name.toLowerCase() === kw.toLowerCase();
bump(uri, exact ? 5 : 2);
}
}
const MIN_SCORE = 4; // a single common-word match (3) is not enough on its own.
const activeUri = vscode.window.activeTextEditor ? vscode.window.activeTextEditor.document.uri.fsPath : null;
const pinned = new Set(contextFiles.map((f) => f.id));
const max = Math.max(0, cfg.get('chat.autoContextMaxFiles', 4));
const ranked = [...score.values()]
.filter((e) => e.score >= MIN_SCORE && e.uri.fsPath !== activeUri && !pinned.has(e.uri.fsPath))
.sort((a, b) => b.score - a.score)
.slice(0, max);
const blocks = [];
const names = [];
let used = 0;
const PER = 60 * 1024, TOTAL = 180 * 1024;
for (const e of ranked) {
try {
const doc = await vscode.workspace.openTextDocument(e.uri);
let body = doc.getText();
if (body.length > PER) { body = body.slice(0, PER) + '\n…(truncated)…'; }
if (used + body.length > TOTAL) { break; }
used += body.length;
const rel = vscode.workspace.asRelativePath(e.uri);
blocks.push('Possibly relevant file `' + rel + '`:\n```' + (doc.languageId || '') + '\n' + body + '\n```');
names.push(rel);
} catch { /* unreadable/binary — skip */ }
}
return { blocks, names };
}
/** A compact list of project file paths, so the model knows the repo structure. */
function workspaceMapBlock(allFiles) {
if (!allFiles.length) { return null; }
const rels = allFiles.map((u) => vscode.workspace.asRelativePath(u)).sort();
const CAP = 400;
let list = rels.slice(0, CAP).join('\n');
if (rels.length > CAP) { list += '\n…(' + (rels.length - CAP) + ' more files)…'; }
return 'Project files (paths only, for orientation):\n```\n' + list + '\n```';
}
function newChat() {
conversation = [];
agentMessages = [];
checkpoints.length = 0; currentCheckpoint = null; // drop the per-turn restore stack
pendingContext = null;
contextFiles = [];
if (abort) { abort.abort(); }
reapCommands(); // kill any background servers/watchers from the old session
reapMcp(); // …and any MCP servers: they are detached children too
if (review) { review.finalizeAll(); } // drop review UI without reverting the user's files
post({ type: 'reset' });
postContextFiles();
}
/** The currently open file as a context block (capped), or null. */
/** True only for real files inside the workspace (excludes temp diff files, output, etc.). */
function isWorkspaceFile(uri) {
return uri && uri.scheme === 'file' && !!vscode.workspace.getWorkspaceFolder(uri);
}
function activeFileBlock() {
const ed = vscode.window.activeTextEditor;
if (!ed || !isWorkspaceFile(ed.document.uri)) { return null; }
if (!aiConfig().get('includeActiveFile', true)) { return null; }
const name = path.basename(ed.document.uri.fsPath);
const lang = ed.document.languageId || '';
let body = ed.document.getText();
const MAX = 120 * 1024;
if (body.length > MAX) { body = body.slice(0, MAX) + '\n…(file truncated for context)…'; }
return 'Currently open file `' + name + '` (full contents):\n```' + lang + '\n' + body + '\n```';
}
/** Tell the webview which file is open, to show as a context chip. */
function postActiveFile() {
const ed = vscode.window.activeTextEditor;
if (!ed || !isWorkspaceFile(ed.document.uri) || !aiConfig().get('includeActiveFile', true)) {
post({ type: 'activeFile', label: null });
return;
}
post({ type: 'activeFile', label: path.basename(ed.document.uri.fsPath) + ' · ' + ed.document.lineCount + ' lines' });
}
/** Running approved commands: runId → stop() that kills the command's process group. */
const commandStops = new Map();
// Background command registry (runId → {command,status,ring,port,…}) so the agent can read a server's
// output across turns via read_command_output. Module-scoped so it survives between agent runs; reaped
// on New Chat + extension unload (servers hold ports — orphaning them breaks the next run).
const bgRuns = new Map();
/** Kill every running command and drop all command state (servers must not survive New Chat/unload). */
function reapCommands() {
for (const [, stop] of commandStops) { try { stop(); } catch (e) { /* already gone */ } }
commandStops.clear();
bgRuns.clear();
previewGate.clear(); // the servers are gone; a fresh session may legitimately preview again
}
// Auto-preview (see openPreview): tracks which addresses have actually been SHOWN this session, so a
// chatty server can't stack tabs and — more importantly — so closing the tab is RESPECTED rather than
// undone by the next log line. A FAILED open stays retryable; see createPreviewGate.
const previewGate = createPreviewGate();
/**
* Show a locally-served URL in the built-in Simple Browser, beside the chat.
*
* This exists because the browser was already there and nobody knew: LevelCode ships VS Code's
* simple-browser, but you had to know the command name to find it. Opening it automatically the moment
* the agent brings a site up turns an invisible feature into the obvious one.
*
* Three deliberate choices: `preserveFocus` so a server coming up never steals the caret from whoever
* is typing; `Beside` so the site sits next to the work rather than replacing it; and dedupe-by-URL so
* the user closing the tab is final. Never throws — a preview must not be able to fail a run.
*
* The gate records a URL as shown only AFTER the open succeeds (PR #35 review): marking it up-front
* meant one transient failure — Simple Browser disabled for a moment — blacklisted that address for the
* whole session, so the preview silently never appeared again. See createPreviewGate.
*/
async function openPreview(url) {
if (!aiConfig().get('preview.autoOpen', true)) { return; }
if (!previewGate.shouldOpen(url)) { return; }
previewGate.begin(url);
try {
await vscode.commands.executeCommand('simpleBrowser.api.open', vscode.Uri.parse(url), {
preserveFocus: true,
viewColumn: vscode.ViewColumn.Beside
});
previewGate.succeeded(url); // it really appeared → never reopen, so closing the tab is final
post({ type: 'agentTool', icon: 'globe', text: '🌐 preview · ' + url });
} catch (e) {
// simple-browser disabled or the id moved upstream — log, never surface as a run failure. The
// URL stays retryable: nothing was shown, so a later run should be free to try again.
previewGate.failed(url);
dbg('preview.failed', { url: url, error: String((e && e.message) || e) });
}
}
// Workspace checkpoints: a per-user-turn stack of file pre-images so the user can roll the workspace back
// to before any turn ran. In-memory per chat session (reset on New Chat); NOT routed through reapCommands
// (these are pure data, not process state — restoring files must not kill running servers).
const checkpoints = []; // oldest-first: { turnId, label, ts, goalMsg, files: Map<key,{before,created}> }
let currentCheckpoint = null;
let checkpointSeq = 0;
/** First-touch hook handed to reviewSession.applyEdit — record a file's pre-image into the open turn. */
function recordCheckpointTouch(key, before, created) {
if (currentCheckpoint && !currentCheckpoint.files.has(key)) { currentCheckpoint.files.set(key, { before: before, created: created }); }
}
/** Restore the workspace to before `turnId` ran — reverting that turn AND every turn after it. */
async function restoreCheckpoint(turnId) {
if (abort) { post({ type: 'checkpointBusy', turnId: turnId }); return; } // never restore under a live turn
const idx = checkpoints.findIndex((c) => c.turnId === turnId);
if (idx < 0) { post({ type: 'checkpointBusy', turnId: turnId, reason: 'gone' }); return; }
const cp = checkpoints[idx];
const cutIdx = agentMessages.indexOf(cp.goalMsg); // by identity → survives trimAgentMemory
// Union of files across this checkpoint + every later one; EARLIEST pre-image wins (true pre-turn state).
const union = new Map();
for (let i = idx; i < checkpoints.length; i++) {
for (const [k, v] of checkpoints[i].files) { if (!union.has(k)) { union.set(k, v); } }
}
let restored = 0;
for (const k of [...union.keys()].reverse()) { // reverse for interdependency safety
const v = union.get(k);
try { if (await review.restoreOne(k, v.before, v.created)) { restored++; } } catch (e) { dbg('checkpoint.restoreErr', { key: k, msg: String((e && e.message) || e) }); }
}
review.finalizeAll(); // pending Keep/Undo cards now reference stale snapshots
if (cutIdx >= 0) { agentMessages.length = cutIdx; } // truncate transcript at the goal boundary
checkpoints.length = idx; // drop restored + all later checkpoints (no redo)
dbg('checkpoint.restore', { turnId: turnId, files: restored, truncatedAt: cutIdx });
post({ type: 'checkpointRestored', turnId: turnId, filesRestored: restored });
}
/** Pending in-chat approval requests, keyed by id, resolved by the webview. */
const pendingApprovals = new Map();
let approvalSeq = 0;
/**
* Persist an MCP tool to the allow-list (the ONLY thing that grants 'allow' — G3). Writes to the USER
* (Global) tier, matching the application scope the setting is declared with, so a repo can never flip
* it. Reads the current value the same user-scoped way it is read at run start. Idempotent, and refuses
* a tool name that is not a namespaced server__tool to avoid writing junk from a malformed message.
*/
async function mcpAllowAlways(name) {
// isNamespacedToolName owns the rule (mcpConfig.js), rather than a second regex here: this used to
// hand-roll one that required a `__` separator, which REJECTED names namespaceToolName legitimately
// produces — a server whose name alone hits the 64-char cap truncates inside that first segment and
// comes back without one. "Always allow" then silently did nothing for that server. The shared check
// also bounds the length, so a malformed message cannot persist an arbitrarily long settings key.
if (!isNamespacedToolName(name)) {
dbg('mcp.allow.reject', { name: String(name).slice(0, 80) }); return;
}
try {
const cfg = aiConfig();
// safeCopy, not Object.assign: the existing value comes from the user's settings.json, where a
// literal "__proto__" key survives JSON.parse as a real own property. Object.assign would hand it
// to the prototype setter instead of copying it; safeCopy drops the unsafe keys outright.
const cur = safeCopy(userScopedSetting(cfg.inspect('mcp.toolPolicy'), {}) || {});
if (cur[name] === 'allow') { return; }
cur[name] = 'allow';
await cfg.update('mcp.toolPolicy', cur, vscode.ConfigurationTarget.Global);
post({ type: 'agentTool', icon: 'check', text: '🔌 mcp · always allow ' + name });
dbg('mcp.allow.persisted', { name });
} catch (e) {
dbg('mcp.allow.failed', { name, error: String((e && e.message) || e) });
}
}
/** Ask the webview to approve an action; resolves true/false. */
function requestApproval(req) {
const id = String(++approvalSeq);
dbg('approval.request', { id, kind: req.kind, cmd: req.command });
return new Promise((resolve) => {
pendingApprovals.set(id, resolve);
post(Object.assign({ type: 'agentApproval', id }, req));
});
}
function resolveApproval(id, approved) {
dbg('approval.response', { id, approved: !!approved });
const r = pendingApprovals.get(id);
if (r) { pendingApprovals.delete(id); r(!!approved); }
}
function clearApprovals() {
for (const [, r] of pendingApprovals) { r(false); }
pendingApprovals.clear();
}
/** Pending in-chat clarifying-question prompts (ask_user), keyed by id, resolved by the webview. */
const pendingQuestions = new Map();
let questionSeq = 0;
/** Ask the webview to present clickable multiple-choice questions; resolves with {answers, notes} or null. */
function requestQuestions(req) {
const id = String(++questionSeq);
dbg('ask.request', { id, questions: (req.questions || []).length });
return new Promise((resolve) => {
pendingQuestions.set(id, resolve);
post({ type: 'agentQuestions', id, questions: req.questions || [] });
});
}
function resolveQuestions(id, answers, notes) {
dbg('ask.response', { id, answered: Array.isArray(answers) ? answers.length : 0, notes: !!notes });
const r = pendingQuestions.get(id);
if (r) { pendingQuestions.delete(id); r({ answers: answers || [], notes: notes || '' }); }
}
function clearQuestions() {
for (const [, r] of pendingQuestions) { r(null); }
pendingQuestions.clear();
}
/** Heal any dangling tool_use (e.g. from a run the user stopped mid-tool) by inserting the
* missing tool_result blocks, so the next API call doesn't 400. */
function repairAgentMemory() {
for (let i = 0; i < agentMessages.length; i++) {
const m = agentMessages[i];
if (m.role !== 'assistant' || !Array.isArray(m.content)) { continue; }
const ids = m.content.filter((c) => c.type === 'tool_use').map((c) => c.id);
if (!ids.length) { continue; }
const next = agentMessages[i + 1];
const have = (next && next.role === 'user' && Array.isArray(next.content))
? new Set(next.content.filter((c) => c.type === 'tool_result').map((c) => c.tool_use_id)) : new Set();
const missing = ids.filter((id) => !have.has(id)).map((id) => ({ type: 'tool_result', tool_use_id: id, content: 'Cancelled.' }));
if (!missing.length) { continue; }
if (next && next.role === 'user' && Array.isArray(next.content)) { next.content.unshift(...missing); }
else { agentMessages.splice(i + 1, 0, { role: 'user', content: missing }); }
}
}
/** Keep agent memory bounded. Trim oldest messages, but only at a goal boundary (a
* string-content user message) so we never orphan a tool_use/tool_result pair. */
// --- M5 auto-verify: editor-diagnostics gathering (vscode glue; pure formatting lives in verify.js) ---
/** Pre-run snapshot of every diagnostic so we only ever report problems the agent NEWLY introduced
* (keyed by severity+message, not position, so they survive the line shifts an edit causes). */
function snapshotDiagnostics() {
const base = new Map();
for (const [uri, diags] of vscode.languages.getDiagnostics()) {
const set = new Set();
for (const d of diags) { set.add(diagKey(d.severity, d.message)); }
base.set(uri.toString(), set);
}
return base;
}
/** Language servers publish diagnostics asynchronously after a save — wait until they settle (a quiet
* period after the last relevant change) or a hard cap, so we don't read stale/empty results. */
function waitForDiagnostics(uris, maxMs) {
return new Promise((resolve) => {
if (!uris.length) { resolve(); return; }
const want = new Set(uris.map((u) => u.toString()));
let done = false, quiet = null;
const finish = () => { if (done) { return; } done = true; if (quiet) { clearTimeout(quiet); } clearTimeout(hard); sub.dispose(); resolve(); };
const sub = vscode.languages.onDidChangeDiagnostics((e) => {
if (e.uris.some((u) => want.has(u.toString()))) { if (quiet) { clearTimeout(quiet); } quiet = setTimeout(finish, 250); }
});
const hard = setTimeout(finish, maxMs);
});
}
async function collectNewDiagnostics(uris, baseline, includeWarnings) {
if (!uris.length) { return { text: '', count: 0 }; }
await waitForDiagnostics(uris, 1500);
const perFile = uris.map((uri) => ({
rel: vscode.workspace.asRelativePath(uri),
uriKey: uri.toString(),
diags: (vscode.languages.getDiagnostics(uri) || []).map((d) => ({
severity: d.severity, message: d.message, source: d.source,
line: d.range.start.line, character: d.range.start.character
}))
}));
return formatDiagnosticLines(perFile, baseline, includeWarnings, 40);
}
function trimAgentMemory() {
const MAX = 80, KEEP = 60;
if (agentMessages.length <= MAX) { return; }
let cut = agentMessages.length - KEEP;
while (cut < agentMessages.length && !(agentMessages[cut].role === 'user' && typeof agentMessages[cut].content === 'string')) { cut++; }
if (cut > 0 && cut < agentMessages.length) { agentMessages.splice(0, cut); }
}
/** Flatten one transcript message to a compact plain-text line for the summarizer. Per-item caps keep
* a single giant file read or command dump from dominating (or blowing) the summary call's own input. */
function serializeMsgForSummary(m) {
const who = m.role === 'assistant' ? 'Assistant' : 'User';
if (typeof m.content === 'string') { return who + ': ' + m.content.slice(0, 4000); }
if (!Array.isArray(m.content)) { return who + ': ' + JSON.stringify(m.content).slice(0, 2000); }
const parts = [];
for (const c of m.content) {
if (c.type === 'text' && c.text) { parts.push(c.text.slice(0, 4000)); }
else if (c.type === 'tool_use') { parts.push('[calls ' + c.name + ' ' + JSON.stringify(c.input || {}).slice(0, 400) + ']'); }
else if (c.type === 'tool_result') { const t = typeof c.content === 'string' ? c.content : JSON.stringify(c.content); parts.push('[tool result: ' + String(t).slice(0, 800) + ']'); }
}
return who + ': ' + parts.join('\n');
}
const COMPACT_SYSTEM = 'You compress a coding-agent conversation into a durable briefing so the agent can continue in a fresh, shorter context. Preserve everything future work depends on; drop everything it does not.';
const COMPACT_INSTRUCTIONS = [
'Summarize the conversation below into a compact briefing the agent will read INSTEAD of the full history.',
'Use tight bullet lists under clear headings, keeping:',
'- The user’s goal(s) and any explicit constraints or preferences.',
'- Decisions and approaches chosen — and rejected, with why.',
'- Files created/edited and what changed; key file/function names and paths.',
'- Commands run and their outcomes; anything currently broken or pending.',
'- The exact current task state — what is done, and what is next.',
'Omit chit-chat, resolved dead-ends, and raw file/command dumps. Be specific (names, paths, numbers). Invent nothing not present below.',
'',
'--- CONVERSATION ---',
''
].join('\n');
/** Manually compact the session transcript: summarize the bulky head (everything up to a recent goal
* boundary) into one message and keep recent turns verbatim, so a long session can continue instead of
* hitting the model’s context window. Provider-agnostic: it flattens the head to text and asks the model
* for a briefing, so no tool_use/tool_result pairing or tool defs are involved. Cutting only at a goal
* boundary (a user STRING message) is the one splice point that can’t orphan a tool_use/tool_result pair.
* NOTE: rewriting the prefix invalidates prompt caching — the next turn pays one cache write. That is the
* intended trade: a one-off rebuild on a much shorter base beats re-sending the whole transcript forever. */
async function compactAgentMemory() {
if (abort) { return { ok: false, reason: 'running' }; }
const msgs = agentMessages;
const KEEP_RECENT = 8;
const beforeMsgTokens = estimateMsgTokens(msgs);
if (msgs.length <= KEEP_RECENT + 2) { return { ok: false, reason: 'small' }; }
const cut = findCompactionCut(msgs, KEEP_RECENT);
if (cut < 0) { return { ok: false, reason: 'noboundary' }; }
// Cap the summarizer input, but keep BOTH ends when it's too long: the opening (the original goal +
// early constraints, first in the transcript) AND the most recent pre-cut decisions (closest to the
// kept tail). Those are the two highest-value regions — a plain head slice would drop the recent
// decisions. Only the lower-value middle is dropped, with a marker.
const flatFull = msgs.slice(0, cut).map(serializeMsgForSummary).join('\n\n');
const CAP = 60000, HEAD = 20000;
const flat = flatFull.length <= CAP
? flatFull
: flatFull.slice(0, HEAD) + '\n\n…[older turns omitted for length]…\n\n' + flatFull.slice(flatFull.length - (CAP - HEAD));
const anchor = msgs[cut]; // identity of the boundary we'll cut at — see the recheck after the await
const req = await prepProviderRequest({ prompt: true });
if (!req.ok) { return { ok: false, reason: 'provider' }; }
let summary;
try {
summary = await providers.complete({
providerId: req.providerId, apiKey: req.apiKey, baseURL: req.baseURL, label: req.label,
model: req.model, maxTokens: 1500,
system: COMPACT_SYSTEM,
messages: [{ role: 'user', content: COMPACT_INSTRUCTIONS + flat }]
});
} catch (e) { dbg('compact.error', { msg: String((e && e.message) || e) }); return { ok: false, reason: 'failed' }; }
if (!summary || !summary.trim()) { return { ok: false, reason: 'empty' }; }
// The summary call awaited for seconds; the transcript may have moved under us — refuse rather than
// splice at a stale place. Three ways it moves: New chat reassigns agentMessages; a checkpoint restore
// or a started turn's trimAgentMemory splices the HEAD (caught by the anchor identity check); and a
// turn STARTED mid-await (`abort` now set) may be streaming from this very array — splicing its prefix
// would inject this (billed) summary and blow its cache mid-run. In every case, yield: the transcript
// is unharmed and the user can compact again once the turn ends.
if (agentMessages !== msgs || msgs[cut] !== anchor || abort) { dbg('compact.stale', { cut, running: !!abort }); return { ok: false, reason: 'changed' }; }
// Replace the head with [summary(user) → ack(assistant)]; the kept tail begins with the user goal at
// `cut`, so role alternation (user → assistant → user …) holds across the seam.
msgs.splice(0, cut,
{ role: 'user', content: '[Summary of the earlier conversation, compacted to save context]\n\n' + summary.trim() },
{ role: 'assistant', content: 'Got it — I have that summary of the work so far and will continue from here.' }
);
// Drop checkpoints whose goal message was summarized away: restoreCheckpoint can no longer truncate the
// transcript for them (it guards on indexOf), so keeping them would offer a half-working rollback.
for (let i = checkpoints.length - 1; i >= 0; i--) { if (msgs.indexOf(checkpoints[i].goalMsg) < 0) { checkpoints.splice(i, 1); } }