@draht/coding-agent 2026.7.7 → 2026.7.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (149) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/README.md +2 -2
  3. package/agents/advisor.md +44 -0
  4. package/bin/draht-tools.cjs +1808 -988
  5. package/dist/agents/advisor.md +44 -0
  6. package/dist/cli/args.d.ts.map +1 -1
  7. package/dist/cli/args.js +1 -1
  8. package/dist/cli/args.js.map +1 -1
  9. package/dist/core/agent-session.d.ts +16 -3
  10. package/dist/core/agent-session.d.ts.map +1 -1
  11. package/dist/core/agent-session.js +76 -18
  12. package/dist/core/agent-session.js.map +1 -1
  13. package/dist/core/cache-stats.d.ts +49 -0
  14. package/dist/core/cache-stats.d.ts.map +1 -0
  15. package/dist/core/cache-stats.js +101 -0
  16. package/dist/core/cache-stats.js.map +1 -0
  17. package/dist/core/compaction/compaction.d.ts +1 -2
  18. package/dist/core/compaction/compaction.d.ts.map +1 -1
  19. package/dist/core/compaction/compaction.js +51 -73
  20. package/dist/core/compaction/compaction.js.map +1 -1
  21. package/dist/core/extensions/index.d.ts +1 -1
  22. package/dist/core/extensions/index.d.ts.map +1 -1
  23. package/dist/core/extensions/index.js.map +1 -1
  24. package/dist/core/extensions/loader.d.ts.map +1 -1
  25. package/dist/core/extensions/loader.js +23 -4
  26. package/dist/core/extensions/loader.js.map +1 -1
  27. package/dist/core/extensions/runner.d.ts.map +1 -1
  28. package/dist/core/extensions/runner.js +1 -24
  29. package/dist/core/extensions/runner.js.map +1 -1
  30. package/dist/core/extensions/types.d.ts +9 -9
  31. package/dist/core/extensions/types.d.ts.map +1 -1
  32. package/dist/core/extensions/types.js.map +1 -1
  33. package/dist/core/index.d.ts +1 -1
  34. package/dist/core/index.d.ts.map +1 -1
  35. package/dist/core/index.js.map +1 -1
  36. package/dist/core/model-registry.d.ts +4 -6
  37. package/dist/core/model-registry.d.ts.map +1 -1
  38. package/dist/core/model-registry.js +34 -8
  39. package/dist/core/model-registry.js.map +1 -1
  40. package/dist/core/resource-loader.d.ts.map +1 -1
  41. package/dist/core/resource-loader.js +2 -5
  42. package/dist/core/resource-loader.js.map +1 -1
  43. package/dist/core/settings-manager.d.ts +7 -3
  44. package/dist/core/settings-manager.d.ts.map +1 -1
  45. package/dist/core/settings-manager.js +10 -1
  46. package/dist/core/settings-manager.js.map +1 -1
  47. package/dist/core/slash-commands.d.ts +1 -0
  48. package/dist/core/slash-commands.d.ts.map +1 -1
  49. package/dist/core/slash-commands.js +3 -3
  50. package/dist/core/slash-commands.js.map +1 -1
  51. package/dist/gsd/hook-utils.d.ts.map +1 -1
  52. package/dist/gsd/hook-utils.js +3 -1
  53. package/dist/gsd/hook-utils.js.map +1 -1
  54. package/dist/index.d.ts +1 -1
  55. package/dist/index.d.ts.map +1 -1
  56. package/dist/index.js.map +1 -1
  57. package/dist/main.d.ts.map +1 -1
  58. package/dist/main.js +1 -0
  59. package/dist/main.js.map +1 -1
  60. package/dist/modes/interactive/components/footer.d.ts +4 -0
  61. package/dist/modes/interactive/components/footer.d.ts.map +1 -1
  62. package/dist/modes/interactive/components/footer.js +1 -1
  63. package/dist/modes/interactive/components/footer.js.map +1 -1
  64. package/dist/modes/interactive/components/oauth-selector.d.ts +3 -1
  65. package/dist/modes/interactive/components/oauth-selector.d.ts.map +1 -1
  66. package/dist/modes/interactive/components/oauth-selector.js +17 -6
  67. package/dist/modes/interactive/components/oauth-selector.js.map +1 -1
  68. package/dist/modes/interactive/components/settings-selector.d.ts +2 -0
  69. package/dist/modes/interactive/components/settings-selector.d.ts.map +1 -1
  70. package/dist/modes/interactive/components/settings-selector.js +10 -0
  71. package/dist/modes/interactive/components/settings-selector.js.map +1 -1
  72. package/dist/modes/interactive/interactive-mode.d.ts +10 -0
  73. package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
  74. package/dist/modes/interactive/interactive-mode.js +247 -47
  75. package/dist/modes/interactive/interactive-mode.js.map +1 -1
  76. package/dist/modes/interactive/theme/theme-schema.json +3 -4
  77. package/dist/modes/interactive/theme/theme.d.ts +2 -1
  78. package/dist/modes/interactive/theme/theme.d.ts.map +1 -1
  79. package/dist/modes/interactive/theme/theme.js +8 -4
  80. package/dist/modes/interactive/theme/theme.js.map +1 -1
  81. package/dist/modes/print-mode.d.ts.map +1 -1
  82. package/dist/modes/print-mode.js +1 -1
  83. package/dist/modes/print-mode.js.map +1 -1
  84. package/dist/modes/rpc/rpc-client.d.ts +6 -6
  85. package/dist/modes/rpc/rpc-client.d.ts.map +1 -1
  86. package/dist/modes/rpc/rpc-client.js +3 -3
  87. package/dist/modes/rpc/rpc-client.js.map +1 -1
  88. package/dist/modes/rpc/rpc-mode.d.ts.map +1 -1
  89. package/dist/modes/rpc/rpc-mode.js +4 -1
  90. package/dist/modes/rpc/rpc-mode.js.map +1 -1
  91. package/dist/prompts/agents/build.md +38 -0
  92. package/dist/prompts/agents/plan.md +36 -0
  93. package/dist/prompts/agents/verify.md +36 -4
  94. package/dist/prompts/commands/brainstorm.md +16 -2
  95. package/dist/prompts/commands/discuss-phase.md +1 -1
  96. package/dist/prompts/commands/execute-phase.md +6 -3
  97. package/dist/prompts/commands/fix.md +13 -1
  98. package/dist/prompts/commands/init-project.md +1 -0
  99. package/dist/prompts/commands/map-codebase.md +9 -9
  100. package/dist/prompts/commands/new-project.md +1 -0
  101. package/dist/prompts/commands/next-milestone.md +3 -0
  102. package/dist/prompts/commands/orchestrate-loop.md +99 -0
  103. package/dist/prompts/commands/orchestrate.md +29 -6
  104. package/dist/prompts/commands/pause-work.md +2 -1
  105. package/dist/prompts/commands/plan-phase.md +6 -0
  106. package/dist/prompts/commands/quick.md +6 -2
  107. package/dist/prompts/commands/review.md +4 -1
  108. package/dist/prompts/commands/verify-work.md +10 -8
  109. package/dist/utils/clipboard-image.d.ts.map +1 -1
  110. package/dist/utils/clipboard-image.js +1 -1
  111. package/dist/utils/clipboard-image.js.map +1 -1
  112. package/docs/custom-provider.md +4 -3
  113. package/docs/extensions.md +15 -10
  114. package/docs/models.md +49 -8
  115. package/docs/rpc.md +15 -5
  116. package/docs/sdk.md +1 -1
  117. package/docs/settings.md +3 -2
  118. package/docs/themes.md +5 -3
  119. package/docs/tui.md +1 -1
  120. package/docs/usage.md +2 -2
  121. package/examples/extensions/custom-provider-anthropic/package-lock.json +2 -2
  122. package/examples/extensions/custom-provider-anthropic/package.json +1 -1
  123. package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
  124. package/examples/extensions/preset.ts +2 -2
  125. package/examples/extensions/reload-runtime.ts +2 -2
  126. package/examples/extensions/sandbox/package-lock.json +2 -2
  127. package/examples/extensions/sandbox/package.json +1 -1
  128. package/examples/extensions/with-deps/package-lock.json +2 -2
  129. package/examples/extensions/with-deps/package.json +1 -1
  130. package/examples/sdk/README.md +1 -1
  131. package/package.json +5 -5
  132. package/prompts/agents/build.md +38 -0
  133. package/prompts/agents/plan.md +36 -0
  134. package/prompts/agents/verify.md +36 -4
  135. package/prompts/commands/brainstorm.md +16 -2
  136. package/prompts/commands/discuss-phase.md +1 -1
  137. package/prompts/commands/execute-phase.md +6 -3
  138. package/prompts/commands/fix.md +13 -1
  139. package/prompts/commands/init-project.md +1 -0
  140. package/prompts/commands/map-codebase.md +9 -9
  141. package/prompts/commands/new-project.md +1 -0
  142. package/prompts/commands/next-milestone.md +3 -0
  143. package/prompts/commands/orchestrate-loop.md +99 -0
  144. package/prompts/commands/orchestrate.md +29 -6
  145. package/prompts/commands/pause-work.md +2 -1
  146. package/prompts/commands/plan-phase.md +6 -0
  147. package/prompts/commands/quick.md +6 -2
  148. package/prompts/commands/review.md +4 -1
  149. package/prompts/commands/verify-work.md +10 -8
@@ -13,6 +13,7 @@ import { spawn, spawnSync } from "child_process";
13
13
  import { APP_NAME, APP_TITLE, CONFIG_DIR_NAME, DISPLAY_VERSION, getAgentDir, getAuthPath, getDebugLogPath, getDocsPath, getShareViewerUrl, VERSION, } from "../../config.js";
14
14
  import { parseSkillBlock } from "../../core/agent-session.js";
15
15
  import { SessionImportFileNotFoundError } from "../../core/agent-session-runtime.js";
16
+ import { CACHE_TTL_MS, collectCacheMisses, computeCacheWaste, detectCacheMiss, } from "../../core/cache-stats.js";
16
17
  import { FooterDataProvider } from "../../core/footer-data-provider.js";
17
18
  import { configureHttpDispatcher, formatHttpIdleTimeoutMs } from "../../core/http-dispatcher.js";
18
19
  import { KeybindingsManager } from "../../core/keybindings.js";
@@ -49,11 +50,11 @@ import { EarendilAnnouncementComponent } from "./components/earendil-announcemen
49
50
  import { ExtensionEditorComponent } from "./components/extension-editor.js";
50
51
  import { ExtensionInputComponent } from "./components/extension-input.js";
51
52
  import { ExtensionSelectorComponent } from "./components/extension-selector.js";
52
- import { FooterComponent } from "./components/footer.js";
53
+ import { FooterComponent, formatTokens } from "./components/footer.js";
53
54
  import { formatKeyText, keyDisplayText, keyHint, keyText, rawKeyHint } from "./components/keybinding-hints.js";
54
55
  import { LoginDialogComponent } from "./components/login-dialog.js";
55
56
  import { ModelSelectorComponent } from "./components/model-selector.js";
56
- import { OAuthSelectorComponent } from "./components/oauth-selector.js";
57
+ import { formatAuthSelectorProviderType, OAuthSelectorComponent, } from "./components/oauth-selector.js";
57
58
  import { ScopedModelsSelectorComponent } from "./components/scoped-models-selector.js";
58
59
  import { SessionSelectorComponent } from "./components/session-selector.js";
59
60
  import { SettingsSelectorComponent } from "./components/settings-selector.js";
@@ -135,6 +136,42 @@ export function isApiKeyLoginProvider(providerId, oauthProviderIds, builtInProvi
135
136
  }
136
137
  return !oauthProviderIds.has(providerId);
137
138
  }
139
+ const AUTH_TYPE_ORDER = { oauth: 0, api_key: 1 };
140
+ function createFuzzyAutocompleteItems(items, prefix, getSearchText, toAutocompleteItem) {
141
+ const filtered = fuzzyFilter(items, prefix, getSearchText);
142
+ if (filtered.length === 0)
143
+ return null;
144
+ return filtered.map(toAutocompleteItem);
145
+ }
146
+ function getLoginProviderCompletionOptions(providerOptions) {
147
+ const byId = new Map();
148
+ for (const provider of providerOptions) {
149
+ const existing = byId.get(provider.id);
150
+ if (existing) {
151
+ if (!existing.authTypes.includes(provider.authType)) {
152
+ existing.authTypes.push(provider.authType);
153
+ existing.authTypes.sort((a, b) => AUTH_TYPE_ORDER[a] - AUTH_TYPE_ORDER[b]);
154
+ }
155
+ continue;
156
+ }
157
+ byId.set(provider.id, {
158
+ id: provider.id,
159
+ name: provider.name,
160
+ authTypes: [provider.authType],
161
+ });
162
+ }
163
+ return Array.from(byId.values()).sort((a, b) => a.name.localeCompare(b.name));
164
+ }
165
+ function getLoginProviderSearchText(provider) {
166
+ const authTypes = provider.authTypes
167
+ .map((authType) => `${authType} ${formatAuthSelectorProviderType(authType)}`)
168
+ .join(" ");
169
+ return `${provider.id} ${provider.name} ${authTypes}`;
170
+ }
171
+ function formatLoginProviderCompletionDescription(provider) {
172
+ const authTypes = provider.authTypes.map(formatAuthSelectorProviderType).join("/");
173
+ return provider.name === provider.id ? authTypes : `${provider.name} · ${authTypes}`;
174
+ }
138
175
  export class InteractiveMode {
139
176
  runtimeHost;
140
177
  ui;
@@ -321,6 +358,7 @@ export class InteractiveMode {
321
358
  const slashCommands = BUILTIN_SLASH_COMMANDS.map((command) => ({
322
359
  name: command.name,
323
360
  description: command.description,
361
+ ...(command.argumentHint && { argumentHint: command.argumentHint }),
324
362
  }));
325
363
  const modelCommand = slashCommands.find((command) => command.name === "model");
326
364
  if (modelCommand) {
@@ -338,17 +376,24 @@ export class InteractiveMode {
338
376
  name: m.name,
339
377
  label: `${m.provider}/${m.id}`,
340
378
  }));
341
- // Fuzzy filter by model ID + provider in either order.
342
- const filtered = fuzzyFilter(items, prefix, getModelSearchText);
343
- if (filtered.length === 0)
344
- return null;
345
- return filtered.map((item) => ({
379
+ return createFuzzyAutocompleteItems(items, prefix, getModelSearchText, (item) => ({
346
380
  value: item.label,
347
381
  label: item.id,
348
382
  description: item.provider,
349
383
  }));
350
384
  };
351
385
  }
386
+ const loginCommand = slashCommands.find((command) => command.name === "login");
387
+ if (loginCommand) {
388
+ loginCommand.getArgumentCompletions = (prefix) => {
389
+ const providers = getLoginProviderCompletionOptions(this.getLoginProviderOptions());
390
+ return createFuzzyAutocompleteItems(providers, prefix, getLoginProviderSearchText, (provider) => ({
391
+ value: provider.id,
392
+ label: provider.id,
393
+ description: formatLoginProviderCompletionDescription(provider),
394
+ }));
395
+ };
396
+ }
352
397
  // Convert prompt templates to SlashCommand format for autocomplete
353
398
  const templateCommands = this.session.promptTemplates.map((cmd) => ({
354
399
  name: cmd.name,
@@ -1171,7 +1216,7 @@ export class InteractiveMode {
1171
1216
  this.restoreQueuedMessagesToEditor({ abort: true });
1172
1217
  },
1173
1218
  commandContextActions: {
1174
- waitForIdle: () => this.session.agent.waitForIdle(),
1219
+ waitForIdle: () => this.session.waitForIdle(),
1175
1220
  newSession: async (options) => {
1176
1221
  this.clearStatusIndicator();
1177
1222
  try {
@@ -1222,7 +1267,7 @@ export class InteractiveMode {
1222
1267
  },
1223
1268
  shutdownHandler: () => {
1224
1269
  this.shutdownRequested = true;
1225
- if (!this.session.isStreaming) {
1270
+ if (this.session.isIdle) {
1226
1271
  void this.shutdown();
1227
1272
  }
1228
1273
  },
@@ -1315,7 +1360,7 @@ export class InteractiveMode {
1315
1360
  sessionManager: this.sessionManager,
1316
1361
  modelRegistry: this.session.modelRegistry,
1317
1362
  model: this.session.model,
1318
- isIdle: () => !this.session.isStreaming,
1363
+ isIdle: () => this.session.isIdle,
1319
1364
  isProjectTrusted: () => this.settingsManager.isProjectTrusted(),
1320
1365
  signal: this.session.agent.signal,
1321
1366
  abort: () => {
@@ -2121,9 +2166,10 @@ export class InteractiveMode {
2121
2166
  this.editor.setText("");
2122
2167
  return;
2123
2168
  }
2124
- if (text === "/login") {
2125
- this.showOAuthSelector("login");
2169
+ if (text === "/login" || text.startsWith("/login ")) {
2170
+ const providerRef = text.startsWith("/login ") ? text.slice(7).trim() : undefined;
2126
2171
  this.editor.setText("");
2172
+ await this.handleLoginCommand(providerRef);
2127
2173
  return;
2128
2174
  }
2129
2175
  if (text === "/logout") {
@@ -2348,6 +2394,7 @@ export class InteractiveMode {
2348
2394
  for (const [, component] of this.pendingTools.entries()) {
2349
2395
  component.setArgsComplete();
2350
2396
  }
2397
+ this.maybeShowCacheMissNotice(this.streamingMessage);
2351
2398
  }
2352
2399
  this.streamingComponent = undefined;
2353
2400
  this.streamingMessage = undefined;
@@ -2398,9 +2445,11 @@ export class InteractiveMode {
2398
2445
  this.streamingMessage = undefined;
2399
2446
  }
2400
2447
  this.pendingTools.clear();
2401
- await this.checkShutdownRequested();
2402
2448
  this.ui.requestRender();
2403
2449
  break;
2450
+ case "agent_settled":
2451
+ await this.checkShutdownRequested();
2452
+ break;
2404
2453
  case "compaction_start": {
2405
2454
  if (this.settingsManager.getShowTerminalProgress()) {
2406
2455
  this.ui.terminal.setProgress(true);
@@ -2607,6 +2656,11 @@ export class InteractiveMode {
2607
2656
  renderSessionItems(items, options = {}) {
2608
2657
  this.pendingTools.clear();
2609
2658
  const renderedPendingTools = new Map();
2659
+ // Cache-miss notices are not persisted; re-derive them from the full entry
2660
+ // list and re-inject them after the assistant messages that paid for them.
2661
+ const cacheMisses = this.settingsManager.getShowCacheMissNotices()
2662
+ ? collectCacheMisses(this.sessionManager.getEntries(), this.session.modelRegistry)
2663
+ : new Map();
2610
2664
  if (options.updateFooter) {
2611
2665
  this.footer.invalidate();
2612
2666
  this.updateEditorBorderColor();
@@ -2648,6 +2702,11 @@ export class InteractiveMode {
2648
2702
  }
2649
2703
  }
2650
2704
  }
2705
+ if (message.stopReason !== "aborted" && message.stopReason !== "error") {
2706
+ const miss = cacheMisses.get(message);
2707
+ if (miss)
2708
+ this.addCacheMissNotice(miss);
2709
+ }
2651
2710
  }
2652
2711
  else if (message.role === "toolResult") {
2653
2712
  // Match tool results to pending tool components
@@ -2682,6 +2741,35 @@ export class InteractiveMode {
2682
2741
  });
2683
2742
  this.renderSessionItems(items, options);
2684
2743
  }
2744
+ /**
2745
+ * Show a transcript notice when a completed assistant message paid for a
2746
+ * significant cache miss. Only states observable facts: the miss itself,
2747
+ * a model switch, or an idle gap past the cache TTL.
2748
+ */
2749
+ maybeShowCacheMissNotice(message) {
2750
+ if (!this.settingsManager.getShowCacheMissNotices())
2751
+ return;
2752
+ // Entries don't contain `message` yet: message_end fires before persistence.
2753
+ const miss = detectCacheMiss(this.sessionManager.getEntries(), message, this.session.modelRegistry);
2754
+ if (miss)
2755
+ this.addCacheMissNotice(miss);
2756
+ }
2757
+ addCacheMissNotice(miss) {
2758
+ if (miss.missedTokens < 20_000 && miss.missedCost < 0.1)
2759
+ return;
2760
+ const cost = miss.missedCost >= 0.01 ? ` (~$${miss.missedCost.toFixed(2)})` : "";
2761
+ const reBilled = `${formatTokens(miss.missedTokens)} tokens re-billed${cost}`;
2762
+ let label = "Cache miss";
2763
+ if (miss.modelChanged) {
2764
+ label = "Cache miss after model switch";
2765
+ }
2766
+ else if (miss.idleMs >= CACHE_TTL_MS) {
2767
+ label = `Cache miss after ${Math.round(miss.idleMs / 60_000)}m idle`;
2768
+ }
2769
+ const text = theme.fg("warning", `${label}: ${reBilled}`);
2770
+ this.chatContainer.addChild(new Spacer(1));
2771
+ this.chatContainer.addChild(new Text(text, 1, 0));
2772
+ }
2685
2773
  renderInitialMessages() {
2686
2774
  const entries = this.sessionManager.buildContextEntries();
2687
2775
  this.renderSessionEntries(entries, {
@@ -3319,6 +3407,7 @@ export class InteractiveMode {
3319
3407
  doubleEscapeAction: this.settingsManager.getDoubleEscapeAction(),
3320
3408
  treeFilterMode: this.settingsManager.getTreeFilterMode(),
3321
3409
  showHardwareCursor: this.settingsManager.getShowHardwareCursor(),
3410
+ showCacheMissNotices: this.settingsManager.getShowCacheMissNotices(),
3322
3411
  defaultProjectTrust: this.settingsManager.getDefaultProjectTrust(),
3323
3412
  editorPaddingX: this.settingsManager.getEditorPaddingX(),
3324
3413
  outputPad: this.settingsManager.getOutputPad(),
@@ -3394,6 +3483,10 @@ export class InteractiveMode {
3394
3483
  this.chatContainer.clear();
3395
3484
  this.rebuildChatFromMessages();
3396
3485
  },
3486
+ onShowCacheMissNoticesChange: (shown) => {
3487
+ this.settingsManager.setShowCacheMissNotices(shown);
3488
+ this.rebuildChatFromMessages();
3489
+ },
3397
3490
  onCollapseChangelogChange: (collapsed) => {
3398
3491
  this.settingsManager.setCollapseChangelog(collapsed);
3399
3492
  },
@@ -3683,19 +3776,17 @@ export class InteractiveMode {
3683
3776
  const initialSelectedId = userMessages[userMessages.length - 1]?.entryId;
3684
3777
  this.showSelector((done) => {
3685
3778
  const selector = new UserMessageSelectorComponent(userMessages.map((m) => ({ id: m.entryId, text: m.text })), async (entryId) => {
3779
+ done();
3686
3780
  try {
3687
3781
  const result = await this.runtimeHost.fork(entryId);
3688
3782
  if (result.cancelled) {
3689
- done();
3690
3783
  this.ui.requestRender();
3691
3784
  return;
3692
3785
  }
3693
3786
  this.editor.setText(result.selectedText ?? "");
3694
- done();
3695
3787
  this.showStatus("Forked to new session");
3696
3788
  }
3697
3789
  catch (error) {
3698
- done();
3699
3790
  this.showError(error instanceof Error ? error.message : String(error));
3700
3791
  }
3701
3792
  }, () => {
@@ -3924,13 +4015,82 @@ export class InteractiveMode {
3924
4015
  }
3925
4016
  return options.sort((a, b) => a.name.localeCompare(b.name));
3926
4017
  }
3927
- showLoginAuthTypeSelector() {
4018
+ findLoginProviderOptions(providerRef) {
4019
+ const normalizedProviderRef = providerRef.trim().toLowerCase();
4020
+ if (!normalizedProviderRef) {
4021
+ return [];
4022
+ }
4023
+ return this.getLoginProviderOptions().filter((provider) => provider.id.toLowerCase() === normalizedProviderRef ||
4024
+ provider.name.toLowerCase() === normalizedProviderRef);
4025
+ }
4026
+ async handleLoginCommand(providerRef) {
4027
+ if (!providerRef) {
4028
+ this.showLoginAuthTypeSelector();
4029
+ return;
4030
+ }
4031
+ const providerOptions = this.findLoginProviderOptions(providerRef);
4032
+ if (providerOptions.length === 1) {
4033
+ await this.startProviderLogin(providerOptions[0]);
4034
+ return;
4035
+ }
4036
+ if (providerOptions.length > 1) {
4037
+ const providerIds = new Set(providerOptions.map((provider) => provider.id));
4038
+ if (providerIds.size === 1) {
4039
+ this.showLoginAuthTypeSelector(providerOptions);
4040
+ return;
4041
+ }
4042
+ }
4043
+ this.showLoginProviderSelector(undefined, providerRef);
4044
+ }
4045
+ async startProviderLogin(providerOption) {
4046
+ if (providerOption.authType === "oauth") {
4047
+ await this.showLoginDialog(providerOption.id, providerOption.name);
4048
+ }
4049
+ else if (providerOption.id === BEDROCK_PROVIDER_ID) {
4050
+ this.showBedrockSetupDialog(providerOption.id, providerOption.name);
4051
+ }
4052
+ else {
4053
+ await this.showApiKeyLoginDialog(providerOption.id, providerOption.name);
4054
+ }
4055
+ }
4056
+ showLoginAuthTypeSelector(providerOptions) {
3928
4057
  const subscriptionLabel = "Use a subscription";
3929
4058
  const apiKeyLabel = "Use an API key";
4059
+ const availableAuthTypes = providerOptions
4060
+ ? new Set(providerOptions.map((provider) => provider.authType))
4061
+ : new Set(["oauth", "api_key"]);
4062
+ const options = [];
4063
+ if (availableAuthTypes.has("oauth")) {
4064
+ options.push(subscriptionLabel);
4065
+ }
4066
+ if (availableAuthTypes.has("api_key")) {
4067
+ options.push(apiKeyLabel);
4068
+ }
4069
+ if (options.length === 0) {
4070
+ this.showStatus("No login methods available.");
4071
+ return;
4072
+ }
4073
+ if (providerOptions && options.length === 1) {
4074
+ const providerOption = providerOptions[0];
4075
+ if (providerOption) {
4076
+ void this.startProviderLogin(providerOption);
4077
+ }
4078
+ return;
4079
+ }
4080
+ const title = providerOptions?.[0]
4081
+ ? `Select authentication method for ${providerOptions[0].name}:`
4082
+ : "Select authentication method:";
3930
4083
  this.showSelector((done) => {
3931
- const selector = new ExtensionSelectorComponent("Select authentication method:", [subscriptionLabel, apiKeyLabel], (option) => {
4084
+ const selector = new ExtensionSelectorComponent(title, options, (option) => {
3932
4085
  done();
3933
4086
  const authType = option === subscriptionLabel ? "oauth" : "api_key";
4087
+ if (providerOptions) {
4088
+ const providerOption = providerOptions.find((provider) => provider.authType === authType);
4089
+ if (providerOption) {
4090
+ void this.startProviderLogin(providerOption);
4091
+ }
4092
+ return;
4093
+ }
3934
4094
  this.showLoginProviderSelector(authType);
3935
4095
  }, () => {
3936
4096
  done();
@@ -3939,32 +4099,34 @@ export class InteractiveMode {
3939
4099
  return { component: selector, focus: selector };
3940
4100
  });
3941
4101
  }
3942
- showLoginProviderSelector(authType) {
4102
+ showLoginProviderSelector(authType, initialSearchInput) {
3943
4103
  const providerOptions = this.getLoginProviderOptions(authType);
3944
4104
  if (providerOptions.length === 0) {
3945
- this.showStatus(authType === "oauth" ? "No subscription providers available." : "No API key providers available.");
4105
+ const message = authType === "oauth"
4106
+ ? "No subscription providers available."
4107
+ : authType === "api_key"
4108
+ ? "No API key providers available."
4109
+ : "No login providers available.";
4110
+ this.showStatus(message);
3946
4111
  return;
3947
4112
  }
3948
4113
  this.showSelector((done) => {
3949
- const selector = new OAuthSelectorComponent("login", this.session.modelRegistry.authStorage, providerOptions, async (providerId) => {
4114
+ const selector = new OAuthSelectorComponent("login", this.session.modelRegistry.authStorage, providerOptions, async (providerId, selectedAuthType) => {
3950
4115
  done();
3951
- const providerOption = providerOptions.find((provider) => provider.id === providerId);
4116
+ const providerOption = providerOptions.find((provider) => provider.id === providerId && provider.authType === selectedAuthType);
3952
4117
  if (!providerOption) {
3953
4118
  return;
3954
4119
  }
3955
- if (providerOption.authType === "oauth") {
3956
- await this.showLoginDialog(providerOption.id, providerOption.name);
3957
- }
3958
- else if (providerOption.id === BEDROCK_PROVIDER_ID) {
3959
- this.showBedrockSetupDialog(providerOption.id, providerOption.name);
4120
+ await this.startProviderLogin(providerOption);
4121
+ }, () => {
4122
+ done();
4123
+ if (authType) {
4124
+ this.showLoginAuthTypeSelector();
3960
4125
  }
3961
4126
  else {
3962
- await this.showApiKeyLoginDialog(providerOption.id, providerOption.name);
4127
+ this.ui.requestRender();
3963
4128
  }
3964
- }, () => {
3965
- done();
3966
- this.showLoginAuthTypeSelector();
3967
- }, (providerId) => this.session.modelRegistry.getProviderAuthStatus(providerId));
4129
+ }, (providerId) => this.session.modelRegistry.getProviderAuthStatus(providerId), initialSearchInput);
3968
4130
  return { component: selector, focus: selector };
3969
4131
  });
3970
4132
  }
@@ -4223,7 +4385,7 @@ export class InteractiveMode {
4223
4385
  const borderColor = (s) => theme.fg("border", s);
4224
4386
  reloadBox.addChild(new DynamicBorder(borderColor));
4225
4387
  reloadBox.addChild(new Spacer(1));
4226
- reloadBox.addChild(new Text(theme.fg("muted", "Reloading keybindings, extensions, skills, prompts, themes..."), 1, 0));
4388
+ reloadBox.addChild(new Text(theme.fg("muted", "Reloading keybindings, extensions, skills, prompts, themes, and context files..."), 1, 0));
4227
4389
  reloadBox.addChild(new Spacer(1));
4228
4390
  reloadBox.addChild(new DynamicBorder(borderColor));
4229
4391
  const previousEditor = this.editor;
@@ -4287,8 +4449,8 @@ export class InteractiveMode {
4287
4449
  this.showError(`models.json error: ${modelsJsonError}`);
4288
4450
  }
4289
4451
  this.showStatus(savedImplicitProjectTrust
4290
- ? "Reloaded keybindings, extensions, skills, prompts, themes; saved project trust"
4291
- : "Reloaded keybindings, extensions, skills, prompts, themes");
4452
+ ? "Reloaded keybindings, extensions, skills, prompts, themes, and context files; saved project trust"
4453
+ : "Reloaded keybindings, extensions, skills, prompts, themes, and context files");
4292
4454
  dismissReloadBox(this.editor);
4293
4455
  reloadBoxDismissed = true;
4294
4456
  }
@@ -4509,6 +4671,26 @@ export class InteractiveMode {
4509
4671
  handleSessionCommand() {
4510
4672
  const stats = this.session.getSessionStats();
4511
4673
  const sessionName = this.sessionManager.getSessionName();
4674
+ const entries = this.sessionManager.getEntries();
4675
+ const cacheWaste = computeCacheWaste(entries, this.session.modelRegistry);
4676
+ // Cost/token totals per provider/model actually used (e.g. OpenRouter `auto`
4677
+ // resolves to a concrete responseModel), sorted by cost descending.
4678
+ const perModelMap = new Map();
4679
+ for (const entry of entries) {
4680
+ if (entry.type !== "message" || entry.message.role !== "assistant")
4681
+ continue;
4682
+ const message = entry.message;
4683
+ const usage = message.usage;
4684
+ const key = `${message.provider}/${message.responseModel ?? message.model}`;
4685
+ let bucket = perModelMap.get(key);
4686
+ if (!bucket) {
4687
+ bucket = { key, cost: 0, tokens: 0 };
4688
+ perModelMap.set(key, bucket);
4689
+ }
4690
+ bucket.cost += usage.cost.total;
4691
+ bucket.tokens += usage.input + usage.output + usage.cacheRead + usage.cacheWrite;
4692
+ }
4693
+ const perModel = Array.from(perModelMap.values()).sort((a, b) => b.cost - a.cost);
4512
4694
  let info = `${theme.bold("Session Info")}\n\n`;
4513
4695
  if (sessionName) {
4514
4696
  info += `${theme.fg("dim", "Name:")} ${sessionName}\n`;
@@ -4516,24 +4698,42 @@ export class InteractiveMode {
4516
4698
  info += `${theme.fg("dim", "File:")} ${stats.sessionFile ?? "In-memory"}\n`;
4517
4699
  info += `${theme.fg("dim", "ID:")} ${stats.sessionId}\n\n`;
4518
4700
  info += `${theme.bold("Messages")}\n`;
4701
+ info += `${theme.fg("dim", "Total:")} ${stats.totalMessages}\n`;
4519
4702
  info += `${theme.fg("dim", "User:")} ${stats.userMessages}\n`;
4520
4703
  info += `${theme.fg("dim", "Assistant:")} ${stats.assistantMessages}\n`;
4521
- info += `${theme.fg("dim", "Tool Calls:")} ${stats.toolCalls}\n`;
4522
- info += `${theme.fg("dim", "Tool Results:")} ${stats.toolResults}\n`;
4523
- info += `${theme.fg("dim", "Total:")} ${stats.totalMessages}\n\n`;
4704
+ info += `${theme.fg("dim", "Tools:")} ${stats.toolCalls} calls, ${stats.toolResults} results\n\n`;
4524
4705
  info += `${theme.bold("Tokens")}\n`;
4525
- info += `${theme.fg("dim", "Input:")} ${stats.tokens.input.toLocaleString()}\n`;
4526
- info += `${theme.fg("dim", "Output:")} ${stats.tokens.output.toLocaleString()}\n`;
4527
- if (stats.tokens.cacheRead > 0) {
4528
- info += `${theme.fg("dim", "Cache Read:")} ${stats.tokens.cacheRead.toLocaleString()}\n`;
4529
- }
4530
- if (stats.tokens.cacheWrite > 0) {
4531
- info += `${theme.fg("dim", "Cache Write:")} ${stats.tokens.cacheWrite.toLocaleString()}\n`;
4706
+ // "Input" is the full prompt volume. With cache activity, split it into
4707
+ // cached (served from cache) vs uncached (everything else) - the only
4708
+ // provider-independent split. Cache writes, where reported, are a detail
4709
+ // of the uncached portion.
4710
+ const { input, cacheRead, cacheWrite } = stats.tokens;
4711
+ const promptTokens = input + cacheRead + cacheWrite;
4712
+ info += `${theme.fg("dim", "Input:")} ${promptTokens.toLocaleString()}\n`;
4713
+ if (promptTokens > 0 && (cacheRead > 0 || cacheWrite > 0)) {
4714
+ const hitRate = theme.fg("dim", `(${((cacheRead / promptTokens) * 100).toFixed(1)}%)`);
4715
+ info += ` ${theme.fg("dim", "Cached:")} ${cacheRead.toLocaleString()} ${hitRate}\n`;
4716
+ const written = cacheWrite > 0 ? ` ${theme.fg("dim", `(${cacheWrite.toLocaleString()} written to cache)`)}` : "";
4717
+ info += ` ${theme.fg("dim", "Uncached:")} ${(input + cacheWrite).toLocaleString()}${written}\n`;
4532
4718
  }
4719
+ info += `${theme.fg("dim", "Output:")} ${stats.tokens.output.toLocaleString()}\n`;
4533
4720
  info += `${theme.fg("dim", "Total:")} ${stats.tokens.total.toLocaleString()}\n`;
4534
- if (stats.cost > 0) {
4721
+ if (stats.cost > 0 || cacheWaste.missedTokens > 0) {
4535
4722
  info += `\n${theme.bold("Cost")}\n`;
4536
- info += `${theme.fg("dim", "Total:")} ${stats.cost.toFixed(4)}`;
4723
+ info += `${theme.fg("dim", "Total:")} $${stats.cost.toFixed(3)}`;
4724
+ if (perModel.length > 1) {
4725
+ for (const entry of perModel) {
4726
+ info += `\n ${theme.fg("dim", `${entry.key}:`)} $${entry.cost.toFixed(3)} ${theme.fg("dim", `(${formatTokens(entry.tokens)} tokens)`)}`;
4727
+ }
4728
+ }
4729
+ if (cacheWaste.missedTokens > 0) {
4730
+ const missLabel = cacheWaste.missCount === 1 ? "1 miss" : `${cacheWaste.missCount} misses`;
4731
+ const detail = `${cacheWaste.missedTokens.toLocaleString()} tokens, ${missLabel}`;
4732
+ info +=
4733
+ cacheWaste.missedCost >= 0.0001
4734
+ ? `\n${theme.fg("dim", "Cache Re-billed:")} $${cacheWaste.missedCost.toFixed(3)} ${theme.fg("dim", `(${detail})`)}`
4735
+ : `\n${theme.fg("dim", "Cache Re-billed:")} ${detail}`;
4736
+ }
4537
4737
  }
4538
4738
  this.chatContainer.addChild(new Spacer(1));
4539
4739
  this.chatContainer.addChild(new Text(info, 1, 0));