@pentoshi/clai 3.8.40 → 3.8.42

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 (46) hide show
  1. package/dist/agent/runner.js +30 -10
  2. package/dist/agent/runner.js.map +1 -1
  3. package/dist/agent/session-title.js +5 -4
  4. package/dist/agent/session-title.js.map +1 -1
  5. package/dist/app/controllers/session-controller.js +2 -1
  6. package/dist/app/controllers/session-controller.js.map +1 -1
  7. package/dist/app/controllers/turn-controller.js +5 -21
  8. package/dist/app/controllers/turn-controller.js.map +1 -1
  9. package/dist/llm/adapters/gemini-tools.d.ts +3 -0
  10. package/dist/llm/adapters/gemini-tools.js +87 -3
  11. package/dist/llm/adapters/gemini-tools.js.map +1 -1
  12. package/dist/llm/anthropic.d.ts +2 -0
  13. package/dist/llm/anthropic.js +32 -11
  14. package/dist/llm/anthropic.js.map +1 -1
  15. package/dist/llm/aws-mantle.js +22 -18
  16. package/dist/llm/aws-mantle.js.map +1 -1
  17. package/dist/llm/gemini.js.map +1 -1
  18. package/dist/llm/http.d.ts +24 -0
  19. package/dist/llm/http.js +25 -3
  20. package/dist/llm/http.js.map +1 -1
  21. package/dist/llm/router.js +19 -10
  22. package/dist/llm/router.js.map +1 -1
  23. package/dist/tui-v2/components/pager/pager.js +3 -3
  24. package/dist/tui-v2/components/pager/pager.js.map +1 -1
  25. package/dist/tui-v2/components/transcript/notice-row.js +3 -1
  26. package/dist/tui-v2/components/transcript/notice-row.js.map +1 -1
  27. package/dist/tui-v2/components/transcript/tool-card.js +14 -1
  28. package/dist/tui-v2/components/transcript/tool-card.js.map +1 -1
  29. package/dist/tui-v2/composer/completion.d.ts +2 -0
  30. package/dist/tui-v2/composer/completion.js +19 -0
  31. package/dist/tui-v2/composer/completion.js.map +1 -1
  32. package/dist/tui-v2/composer/composer-editor.js +15 -6
  33. package/dist/tui-v2/composer/composer-editor.js.map +1 -1
  34. package/dist/tui-v2/composer/paste-placeholder.d.ts +1 -0
  35. package/dist/tui-v2/composer/paste-placeholder.js +3 -0
  36. package/dist/tui-v2/composer/paste-placeholder.js.map +1 -1
  37. package/dist/tui-v2/state/transcript-reducer.d.ts +2 -2
  38. package/dist/tui-v2/state/transcript-reducer.js +17 -94
  39. package/dist/tui-v2/state/transcript-reducer.js.map +1 -1
  40. package/dist/types.d.ts +9 -0
  41. package/dist/types.js.map +1 -1
  42. package/dist/ui/thinking.js +9 -7
  43. package/dist/ui/thinking.js.map +1 -1
  44. package/dist/version.generated.d.ts +2 -2
  45. package/dist/version.generated.js +2 -2
  46. package/package.json +1 -1
@@ -15,7 +15,7 @@ import { availableToolNames, normalizeToolCall, runToolCall, BATCH_SAFE_TOOLS, }
15
15
  import { getToolDefinitions, getCompactToolDefinitions, PLAN_TOOL_NAMES, } from "../tools/definitions.js";
16
16
  import { appendAssistantWithTools, ensureUniqueToolCallIds, appendToolResult, assertValidToolProtocol, fillMissingToolResults, repairToolProtocol, } from "./tool-history.js";
17
17
  import { formatViewportHint, registerViewport } from "../ui/output-pane.js";
18
- import { compactMessagesWithSummary, estimateMessagesTokens, shouldApplyAutoCompact, COMPACTION_MEMORY_PREFIX, PLAN_IMPLEMENT_MEMORY_PREFIX, isCompactionMemoryMessage, } from "./context-manager.js";
18
+ import { compactMessagesWithSummary, shouldApplyAutoCompact, COMPACTION_MEMORY_PREFIX, PLAN_IMPLEMENT_MEMORY_PREFIX, isCompactionMemoryMessage, } from "./context-manager.js";
19
19
  import { buildContextBreakdown, contextBreakdownAuditPayload, } from "./context-breakdown.js";
20
20
  import { autoCompactTriggerTokens, dedupeToolContextOutput, freeTierGuardNotices, getReliabilityPolicy, resolveStepMaxTokens, } from "./reliability-policy.js";
21
21
  import { auditLog } from "../store/logs.js";
@@ -2010,8 +2010,19 @@ export async function runAgentTurn(prompt, options = {}) {
2010
2010
  });
2011
2011
  return response.text;
2012
2012
  };
2013
+ /**
2014
+ * Estimate the complete next model request, including attached native-tool
2015
+ * schemas. This must match the request-context accounting used by the UI
2016
+ * and audit trail: large schemas can otherwise push an actual request past
2017
+ * the compaction threshold while message-only accounting says it is safe.
2018
+ */
2019
+ const estimateNextRequestTokens = (contextMessages) => {
2020
+ const { native } = resolveNativeTools(provider, model);
2021
+ const nextTools = selectToolDefs(native, useCompactSystemPrompt);
2022
+ return buildContextBreakdown(contextMessages, nextTools).estimatedTotalTokens;
2023
+ };
2013
2024
  async function maybeAutoCompact(reason, force = false) {
2014
- const beforeTokens = estimateMessagesTokens(messages);
2025
+ const beforeTokens = estimateNextRequestTokens(messages);
2015
2026
  // E1: soft early compact (default 70k) while hard ceiling remains 100k.
2016
2027
  const compactTrigger = autoCompactTriggerTokens();
2017
2028
  if (!force && beforeTokens < compactTrigger)
@@ -2037,8 +2048,8 @@ export async function runAgentTurn(prompt, options = {}) {
2037
2048
  }
2038
2049
  messages.splice(0, messages.length, ...result.messages);
2039
2050
  loopGuard.resetReadOnly();
2040
- // Token stats BEFORE plan re-injection so the reduction is accurate.
2041
- const compactedTokens = estimateMessagesTokens(messages);
2051
+ // Token stats use the same complete request estimate as the trigger.
2052
+ const compactedTokens = estimateNextRequestTokens(messages);
2042
2053
  // Re-inject the live plan so the model keeps full plan awareness even
2043
2054
  // after older turns (which carried the plan context) were summarized.
2044
2055
  const livePlan = await loadPlan(session.sessionId).catch(() => undefined);
@@ -2051,8 +2062,8 @@ export async function runAgentTurn(prompt, options = {}) {
2051
2062
  // Re-inject live SESSION STATE after compaction (older flags survive).
2052
2063
  refreshSessionState(livePlan);
2053
2064
  lastCompactionMsgCount = messages.length;
2054
- // Final count the model actually receives (may include re-injected plan).
2055
- const afterTokens = estimateMessagesTokens(messages);
2065
+ // Final request count the model receives (may include re-injected plan).
2066
+ const afterTokens = estimateNextRequestTokens(messages);
2056
2067
  await auditLog("agent.compact", {
2057
2068
  newLength: messages.length,
2058
2069
  estimatedTokens: afterTokens,
@@ -2347,20 +2358,20 @@ export async function runAgentTurn(prompt, options = {}) {
2347
2358
  }
2348
2359
  }
2349
2360
  }
2350
- if (!sawReasoning && /<think/i.test(token)) {
2361
+ if (!sawReasoning && /<think(?:ing)?\b/i.test(token)) {
2351
2362
  sawReasoning = true;
2352
2363
  inThinking = true;
2353
2364
  spinner.setLabel("thinking");
2354
2365
  if (!writesDirectly)
2355
2366
  emit({ type: "status", text: "thinking" });
2356
2367
  }
2357
- if (/<\/think>/i.test(token)) {
2368
+ if (/<\/think(?:ing)?>/i.test(token)) {
2358
2369
  inThinking = false;
2359
2370
  spinner.setLabel("generating response (0 tokens)");
2360
2371
  generatedTokens = 0;
2361
2372
  }
2362
2373
  if (inThinking) {
2363
- const cleaned = token.replace(/<\/?think[^>]*>/gi, "");
2374
+ const cleaned = token.replace(/<\/?think(?:ing)?[^>]*>/gi, "");
2364
2375
  if (cleaned) {
2365
2376
  spinner.pushPreview(cleaned);
2366
2377
  const approx = cleaned.split(/\s+/).filter(Boolean).length;
@@ -2422,7 +2433,16 @@ export async function runAgentTurn(prompt, options = {}) {
2422
2433
  (toolsAttached && !isTextOnlyModel(provider, model));
2423
2434
  const assistantTextResult = rememberThinkingFromText(completion.text);
2424
2435
  assistantText = assistantTextResult;
2425
- if (assistantText.hasThinking) {
2436
+ // Only emit a thinking-block event when the classic renderer is
2437
+ // active (writesDirectly / no deltaParser). In TUI v2 the
2438
+ // deltaParser already streamed thinking-delta events that created
2439
+ // the thinking item in the correct transcript position; emitting
2440
+ // a redundant thinking-block after tool-call events have cleared
2441
+ // pendingThinkingId causes the reducer to append a *duplicate*
2442
+ // thinking item at the end of the transcript — the root cause of
2443
+ // the "ghost thinking blocks below the response" bug seen with
2444
+ // models that use reasoning_content (e.g. Kimi K2-thinking).
2445
+ if (assistantText.hasThinking && !deltaParser) {
2426
2446
  writeThinkingBlock(assistantText.thinkContent);
2427
2447
  }
2428
2448
  // Native-first: prefer structured toolCalls from the provider.