@zqagent/core 0.2.5 → 0.2.7

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.
@@ -266,6 +266,11 @@ interface StreamOpts {
266
266
  * 仅置引擎内部的取消标志位无法结束已静默挂死的网络流。
267
267
  */
268
268
  signal?: AbortSignal;
269
+ /**
270
+ * 最大输出 token 数(可选)。用于摘要器等轻量请求限制输出长度;
271
+ * Responses → max_output_tokens,Chat → max_tokens / max_completion_tokens。
272
+ */
273
+ maxTokens?: number;
269
274
  /**
270
275
  * 内部字段:流空闲看门狗(由 `stream()` 注入,各流路径复用)。
271
276
  * 引擎侧无需感知,请勿在 `stream()` 之外构造。
@@ -528,8 +533,19 @@ interface TurnEngineOpts {
528
533
  contextWindow?: number;
529
534
  /** 压缩触发阈值比例(占窗口比例,默认 0.8) */
530
535
  thresholdPct?: number;
536
+ /**
537
+ * 软阈值比例(占窗口比例):达到它只淘汰旧工具结果,不调用 LLM。
538
+ * 缺省为硬阈值的 75%(即默认 60% 窗口)。
539
+ */
540
+ softThresholdPct?: number;
531
541
  /** 压缩触发阈值绝对上限(token 数;缺省不限制,即按比例用满窗口) */
532
542
  capTokens?: number | null;
543
+ /**
544
+ * 超预算工具结果的落盘目录(可选)。
545
+ *
546
+ * 多租户 Server 应指向租户/会话自己的目录,避免不同租户的内容混在一个目录里。
547
+ */
548
+ toolResultDir?: string;
533
549
  }
534
550
  /** 核心 Agent 循环 (对齐 Python 版 TurnEngine) */
535
551
  declare class TurnEngine {
@@ -552,12 +568,22 @@ declare class TurnEngine {
552
568
  private _steering;
553
569
  compactionState: CompactionState | null;
554
570
  private _lastContextTokens;
571
+ /** 压缩无进展时的消息条数快照;-1 表示无冷却 */
572
+ private _compactStalledAt;
555
573
  /** 显式配置的上下文窗口大小(token 数),未设置时为 undefined(走自动检测) */
556
574
  private _contextWindow;
557
575
  /** 压缩触发阈值比例(默认 0.8) */
558
576
  private _thresholdPct;
577
+ /** 软阈值比例(undefined = 取硬阈值的 75%) */
578
+ private _softThresholdPct;
579
+ /** 是否启用软阈值淘汰(COMPACTION_ELIDE=0 可关闭) */
580
+ private _elisionEnabled;
581
+ /** 是否启用重复读取去重(COMPACTION_DEDUP=0 可关闭) */
582
+ private _dedupEnabled;
559
583
  /** 压缩触发阈值绝对上限(null = 不限制) */
560
584
  private _capTokens;
585
+ /** 超预算工具结果的落盘目录(undefined = 使用默认状态目录) */
586
+ private _toolResultDir;
561
587
  private _producedFiles;
562
588
  private _turnStartTime;
563
589
  private static readonly FILE_WRITE_TOOLS;
@@ -631,8 +657,14 @@ declare class TurnEngine {
631
657
  * 过滤 notice 消息;tool 消息只保留必要字段;
632
658
  * 其余消息剥离 display-only sidecar 字段(source/_display/ts/
633
659
  * reasoning/usage),避免污染模型上下文。
660
+ *
661
+ * 达到软阈值时先淘汰旧工具结果(零 LLM 成本),再产出最终视图。
634
662
  */
635
663
  private _outboundMessages;
664
+ /** 过滤 notice + 剥离 display-only 字段,得到最终发送视图 */
665
+ private _finalizeOutbound;
666
+ /** 软阈值(token 数):达到它只淘汰旧工具结果 */
667
+ private _softTriggerTokens;
636
668
  /** 通知消息:仅用于日志/展示,不发给模型 */
637
669
  private _appendNotice;
638
670
  /**
@@ -649,8 +681,18 @@ declare class TurnEngine {
649
681
  * 消息即停止;只有末尾全部是 error 通知才返回 true。
650
682
  */
651
683
  private _tailIsRetriableError;
652
- /** 当前上下文 token 用量:优先采信 API 返回的精确数量,否则用 chars/4 估算 */
684
+ /**
685
+ * 当前上下文 token 用量。
686
+ *
687
+ * API 返回的 `usage.input` 是精确值,但反映的是「上一次请求」——工具结果
688
+ * 与淘汰/压缩都会让它过时。因此同时估算当前 outbound 视图,取较大者:
689
+ * 宁可早一点压缩,也不要漏判导致溢出。
690
+ */
653
691
  private _compactionTokens;
692
+ /** 压缩后重置精确计数:后续按估算值判断 */
693
+ private _resetContextTokens;
694
+ /** 当前 outbound 视图的估算 token 数 */
695
+ private _estimateContextTokens;
654
696
  /** 检查是否需要压缩(每轮 LLM 调用前) */
655
697
  private _compactionDue;
656
698
  /** 组装压缩开始时的用量信息(供 hook 与 COMPACTING 事件携带) */
@@ -702,6 +744,8 @@ declare function buildExplorerEngine(opts: {
702
744
  * 会回调给调用方渲染(如 CLI 的 SubagentHook);不传则与旧版一致(静默)。
703
745
  */
704
746
  hook?: EngineHook;
747
+ /** 超预算工具结果的落盘目录(与主引擎共用,保持租户隔离) */
748
+ toolResultDir?: string;
705
749
  }): TurnEngine;
706
750
  /**
707
751
  * 创建 explore 工具函数
@@ -721,6 +765,8 @@ declare function makeExploreTool(opts: {
721
765
  * explore 会同时运行),共享同一个渲染器会互相串状态、输出交错难辨。
722
766
  */
723
767
  hookFactory?: () => EngineHook;
768
+ /** 超预算工具结果的落盘目录(透传给子引擎) */
769
+ toolResultDir?: string;
724
770
  }): (args: Record<string, unknown>) => Promise<Record<string, unknown>>;
725
771
  /** 注册 explore 工具(低风险,无需审批) */
726
772
  declare function registerExploreTool(registry: ToolRegistry, opts: {
@@ -730,6 +776,8 @@ declare function registerExploreTool(registry: ToolRegistry, opts: {
730
776
  readonlyRegistrars?: (registry: ToolRegistry) => void;
731
777
  /** 子引擎钩子工厂(可选):见 makeExploreTool */
732
778
  hookFactory?: () => EngineHook;
779
+ /** 超预算工具结果的落盘目录(透传给子引擎) */
780
+ toolResultDir?: string;
733
781
  }): void;
734
782
 
735
783
  export { ApprovalOutcome as A, type CompactionStartInfo as C, type Decision as D, type EngineHook as E, Mode as M, OpenAIProvider as O, type ProviderClient as P, TurnEngine as T, type WsEvent as W, type CompactionDoneInfo as a, EventType as b, type EngineErrorInfo as c, type Approver as d, type CompactionTrigger as e, PermissionEngine as f, type PermissionRequest as g, type TurnEngineOpts as h, buildExplorerEngine as i, makeExploreTool as m, registerExploreTool as r };
@@ -266,6 +266,11 @@ interface StreamOpts {
266
266
  * 仅置引擎内部的取消标志位无法结束已静默挂死的网络流。
267
267
  */
268
268
  signal?: AbortSignal;
269
+ /**
270
+ * 最大输出 token 数(可选)。用于摘要器等轻量请求限制输出长度;
271
+ * Responses → max_output_tokens,Chat → max_tokens / max_completion_tokens。
272
+ */
273
+ maxTokens?: number;
269
274
  /**
270
275
  * 内部字段:流空闲看门狗(由 `stream()` 注入,各流路径复用)。
271
276
  * 引擎侧无需感知,请勿在 `stream()` 之外构造。
@@ -528,8 +533,19 @@ interface TurnEngineOpts {
528
533
  contextWindow?: number;
529
534
  /** 压缩触发阈值比例(占窗口比例,默认 0.8) */
530
535
  thresholdPct?: number;
536
+ /**
537
+ * 软阈值比例(占窗口比例):达到它只淘汰旧工具结果,不调用 LLM。
538
+ * 缺省为硬阈值的 75%(即默认 60% 窗口)。
539
+ */
540
+ softThresholdPct?: number;
531
541
  /** 压缩触发阈值绝对上限(token 数;缺省不限制,即按比例用满窗口) */
532
542
  capTokens?: number | null;
543
+ /**
544
+ * 超预算工具结果的落盘目录(可选)。
545
+ *
546
+ * 多租户 Server 应指向租户/会话自己的目录,避免不同租户的内容混在一个目录里。
547
+ */
548
+ toolResultDir?: string;
533
549
  }
534
550
  /** 核心 Agent 循环 (对齐 Python 版 TurnEngine) */
535
551
  declare class TurnEngine {
@@ -552,12 +568,22 @@ declare class TurnEngine {
552
568
  private _steering;
553
569
  compactionState: CompactionState | null;
554
570
  private _lastContextTokens;
571
+ /** 压缩无进展时的消息条数快照;-1 表示无冷却 */
572
+ private _compactStalledAt;
555
573
  /** 显式配置的上下文窗口大小(token 数),未设置时为 undefined(走自动检测) */
556
574
  private _contextWindow;
557
575
  /** 压缩触发阈值比例(默认 0.8) */
558
576
  private _thresholdPct;
577
+ /** 软阈值比例(undefined = 取硬阈值的 75%) */
578
+ private _softThresholdPct;
579
+ /** 是否启用软阈值淘汰(COMPACTION_ELIDE=0 可关闭) */
580
+ private _elisionEnabled;
581
+ /** 是否启用重复读取去重(COMPACTION_DEDUP=0 可关闭) */
582
+ private _dedupEnabled;
559
583
  /** 压缩触发阈值绝对上限(null = 不限制) */
560
584
  private _capTokens;
585
+ /** 超预算工具结果的落盘目录(undefined = 使用默认状态目录) */
586
+ private _toolResultDir;
561
587
  private _producedFiles;
562
588
  private _turnStartTime;
563
589
  private static readonly FILE_WRITE_TOOLS;
@@ -631,8 +657,14 @@ declare class TurnEngine {
631
657
  * 过滤 notice 消息;tool 消息只保留必要字段;
632
658
  * 其余消息剥离 display-only sidecar 字段(source/_display/ts/
633
659
  * reasoning/usage),避免污染模型上下文。
660
+ *
661
+ * 达到软阈值时先淘汰旧工具结果(零 LLM 成本),再产出最终视图。
634
662
  */
635
663
  private _outboundMessages;
664
+ /** 过滤 notice + 剥离 display-only 字段,得到最终发送视图 */
665
+ private _finalizeOutbound;
666
+ /** 软阈值(token 数):达到它只淘汰旧工具结果 */
667
+ private _softTriggerTokens;
636
668
  /** 通知消息:仅用于日志/展示,不发给模型 */
637
669
  private _appendNotice;
638
670
  /**
@@ -649,8 +681,18 @@ declare class TurnEngine {
649
681
  * 消息即停止;只有末尾全部是 error 通知才返回 true。
650
682
  */
651
683
  private _tailIsRetriableError;
652
- /** 当前上下文 token 用量:优先采信 API 返回的精确数量,否则用 chars/4 估算 */
684
+ /**
685
+ * 当前上下文 token 用量。
686
+ *
687
+ * API 返回的 `usage.input` 是精确值,但反映的是「上一次请求」——工具结果
688
+ * 与淘汰/压缩都会让它过时。因此同时估算当前 outbound 视图,取较大者:
689
+ * 宁可早一点压缩,也不要漏判导致溢出。
690
+ */
653
691
  private _compactionTokens;
692
+ /** 压缩后重置精确计数:后续按估算值判断 */
693
+ private _resetContextTokens;
694
+ /** 当前 outbound 视图的估算 token 数 */
695
+ private _estimateContextTokens;
654
696
  /** 检查是否需要压缩(每轮 LLM 调用前) */
655
697
  private _compactionDue;
656
698
  /** 组装压缩开始时的用量信息(供 hook 与 COMPACTING 事件携带) */
@@ -702,6 +744,8 @@ declare function buildExplorerEngine(opts: {
702
744
  * 会回调给调用方渲染(如 CLI 的 SubagentHook);不传则与旧版一致(静默)。
703
745
  */
704
746
  hook?: EngineHook;
747
+ /** 超预算工具结果的落盘目录(与主引擎共用,保持租户隔离) */
748
+ toolResultDir?: string;
705
749
  }): TurnEngine;
706
750
  /**
707
751
  * 创建 explore 工具函数
@@ -721,6 +765,8 @@ declare function makeExploreTool(opts: {
721
765
  * explore 会同时运行),共享同一个渲染器会互相串状态、输出交错难辨。
722
766
  */
723
767
  hookFactory?: () => EngineHook;
768
+ /** 超预算工具结果的落盘目录(透传给子引擎) */
769
+ toolResultDir?: string;
724
770
  }): (args: Record<string, unknown>) => Promise<Record<string, unknown>>;
725
771
  /** 注册 explore 工具(低风险,无需审批) */
726
772
  declare function registerExploreTool(registry: ToolRegistry, opts: {
@@ -730,6 +776,8 @@ declare function registerExploreTool(registry: ToolRegistry, opts: {
730
776
  readonlyRegistrars?: (registry: ToolRegistry) => void;
731
777
  /** 子引擎钩子工厂(可选):见 makeExploreTool */
732
778
  hookFactory?: () => EngineHook;
779
+ /** 超预算工具结果的落盘目录(透传给子引擎) */
780
+ toolResultDir?: string;
733
781
  }): void;
734
782
 
735
783
  export { ApprovalOutcome as A, type CompactionStartInfo as C, type Decision as D, type EngineHook as E, Mode as M, OpenAIProvider as O, type ProviderClient as P, TurnEngine as T, type WsEvent as W, type CompactionDoneInfo as a, EventType as b, type EngineErrorInfo as c, type Approver as d, type CompactionTrigger as e, PermissionEngine as f, type PermissionRequest as g, type TurnEngineOpts as h, buildExplorerEngine as i, makeExploreTool as m, registerExploreTool as r };