minimal-agent 0.1.4 → 0.1.5

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 (2) hide show
  1. package/dist/main.js +36 -22
  2. package/package.json +1 -1
package/dist/main.js CHANGED
@@ -3480,9 +3480,12 @@ function findHeadCutpoint(messages, proposedCut) {
3480
3480
  }
3481
3481
 
3482
3482
  // src/context/reactiveCompact.ts
3483
- var attemptedThisSession = false;
3484
- function resetReactiveCompactState() {
3485
- attemptedThisSession = false;
3483
+ function createReactiveCompactState() {
3484
+ return { attempted: false };
3485
+ }
3486
+ var defaultState = createReactiveCompactState();
3487
+ function resetReactiveCompactState(state = defaultState) {
3488
+ state.attempted = false;
3486
3489
  }
3487
3490
  function isPromptTooLongError(error) {
3488
3491
  const msg = errorMessage(error).toLowerCase();
@@ -3499,18 +3502,18 @@ function errorMessage(error) {
3499
3502
  }
3500
3503
  return String(error ?? "");
3501
3504
  }
3502
- async function reactiveCompactIfApplicable(messages, provider, error) {
3505
+ async function reactiveCompactIfApplicable(messages, provider, error, state = defaultState) {
3503
3506
  if (!isPromptTooLongError(error)) {
3504
3507
  return { recovered: false, messages, reason: "not a prompt-too-long error" };
3505
3508
  }
3506
- if (attemptedThisSession) {
3509
+ if (state.attempted) {
3507
3510
  return {
3508
3511
  recovered: false,
3509
3512
  messages,
3510
3513
  reason: "already attempted this session \u2014 use /new or /compact manually"
3511
3514
  };
3512
3515
  }
3513
- attemptedThisSession = true;
3516
+ state.attempted = true;
3514
3517
  try {
3515
3518
  const r = await forceCompact(messages, provider);
3516
3519
  return {
@@ -3552,10 +3555,12 @@ var HEAD_KEEP_CHARS = 2e3;
3552
3555
  var TAIL_KEEP_CHARS = 1e3;
3553
3556
  var MAX_KEEP_ROUNDS = 10;
3554
3557
  var SHORT_CONTENT_THRESHOLD = 200;
3555
- var currentTurn = 0;
3556
- var contentCache = /* @__PURE__ */ new Map();
3557
- function incrementTurn() {
3558
- currentTurn++;
3558
+ function createMicroCompactState() {
3559
+ return { turn: 0, cache: /* @__PURE__ */ new Map() };
3560
+ }
3561
+ var defaultState2 = createMicroCompactState();
3562
+ function incrementTurn(state = defaultState2) {
3563
+ state.turn++;
3559
3564
  }
3560
3565
  var COMPRESSIBLE_TOOLS = /* @__PURE__ */ new Set([
3561
3566
  "Grep",
@@ -3563,7 +3568,7 @@ var COMPRESSIBLE_TOOLS = /* @__PURE__ */ new Set([
3563
3568
  "WebFetch",
3564
3569
  "Glob"
3565
3570
  ]);
3566
- function microCompact(toolName, content) {
3571
+ function microCompact(toolName, content, state = defaultState2) {
3567
3572
  if (!content) return content;
3568
3573
  if (content.startsWith("Error:") || content.startsWith("\u9519\u8BEF")) {
3569
3574
  return content;
@@ -3572,7 +3577,7 @@ function microCompact(toolName, content) {
3572
3577
  return content;
3573
3578
  }
3574
3579
  const hash = sha1(content);
3575
- const existing = contentCache.get(hash);
3580
+ const existing = state.cache.get(hash);
3576
3581
  if (existing) {
3577
3582
  existing.count++;
3578
3583
  if (existing.count > MAX_REPEAT_COUNT) {
@@ -3580,17 +3585,17 @@ function microCompact(toolName, content) {
3580
3585
  }
3581
3586
  return content;
3582
3587
  }
3583
- contentCache.set(hash, { count: 1, firstToolName: toolName, firstSeenTurn: currentTurn });
3588
+ state.cache.set(hash, { count: 1, firstToolName: toolName, firstSeenTurn: state.turn });
3584
3589
  if (content.length > MAX_RESULT_SIZE && COMPRESSIBLE_TOOLS.has(toolName)) {
3585
3590
  return truncateContent(content);
3586
3591
  }
3587
3592
  return content;
3588
3593
  }
3589
- function expireOldEntries() {
3594
+ function expireOldEntries(state = defaultState2) {
3590
3595
  let expired = 0;
3591
- for (const [hash, entry] of contentCache) {
3592
- if (currentTurn - entry.firstSeenTurn > MAX_KEEP_ROUNDS) {
3593
- contentCache.delete(hash);
3596
+ for (const [hash, entry] of state.cache) {
3597
+ if (state.turn - entry.firstSeenTurn > MAX_KEEP_ROUNDS) {
3598
+ state.cache.delete(hash);
3594
3599
  expired++;
3595
3600
  }
3596
3601
  }
@@ -3610,15 +3615,15 @@ function truncateContent(content) {
3610
3615
 
3611
3616
  // src/loop.ts
3612
3617
  async function* runQuery(userInput, options) {
3613
- const { provider, history, signal } = options;
3618
+ const { provider, history, signal, sessionState } = options;
3614
3619
  const maxTurns = options.maxTurns ?? 50;
3615
3620
  history.push({ role: "user", content: userInput });
3616
3621
  let reactiveAttempted = false;
3617
3622
  let turn = 0;
3618
3623
  while (turn < maxTurns) {
3619
3624
  turn++;
3620
- incrementTurn();
3621
- expireOldEntries();
3625
+ incrementTurn(sessionState?.microCompact);
3626
+ expireOldEntries(sessionState?.microCompact);
3622
3627
  if (signal?.aborted) {
3623
3628
  history.push({
3624
3629
  role: "user",
@@ -3680,7 +3685,12 @@ async function* runQuery(userInput, options) {
3680
3685
  if (isPromptTooLongError(e) && !reactiveAttempted) {
3681
3686
  reactiveAttempted = true;
3682
3687
  yield { type: "compact_start" };
3683
- const result = await reactiveCompactIfApplicable(history, provider, e);
3688
+ const result = await reactiveCompactIfApplicable(
3689
+ history,
3690
+ provider,
3691
+ e,
3692
+ sessionState?.reactive
3693
+ );
3684
3694
  if (result.recovered) {
3685
3695
  history.length = 0;
3686
3696
  history.push(...result.messages);
@@ -3728,7 +3738,11 @@ async function* runQuery(userInput, options) {
3728
3738
  };
3729
3739
  const result = await executeTool(tc.function.name, tc.function.arguments, signal);
3730
3740
  const rawContent = result.ok ? result.content : `Error: ${result.error}`;
3731
- const content = microCompact(tc.function.name, rawContent);
3741
+ const content = microCompact(
3742
+ tc.function.name,
3743
+ rawContent,
3744
+ sessionState?.microCompact
3745
+ );
3732
3746
  history.push({
3733
3747
  role: "tool",
3734
3748
  content,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "minimal-agent",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "description": "最小化 Agent 系统 —— 单对话 + 9 工具 + 自动压缩 + OpenAI 兼容 + Ink TUI(学习/教学用)",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "author": "Bill Wang <leiwang0359@gmail.com>",