@ai-zen/agents-sdk 0.2.2 → 0.2.3

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.
@@ -3,6 +3,9 @@ import { SdkAgent } from "../runtime/SdkAgent.js";
3
3
  export interface AutoMigrateOptions {
4
4
  maxTokens: number;
5
5
  migrationAgent: SdkAgent;
6
+ /** 迁移开始前触发(promptTokens 刚超限时),可在此向用户展示迁移提示 */
7
+ onBeforeMigrate?: (promptTokens: number, maxTokens: number, agent: SdkAgent) => void;
8
+ /** 迁移完成后触发(交接文档已注入 agent.messages),可在此保存对话等 */
6
9
  onHandoff?: (handoffDoc: string, agent: SdkAgent) => void;
7
10
  }
8
11
  /**
@@ -23,13 +23,21 @@ export class AutoMigratePlugin {
23
23
  }
24
24
  async onAfterSend(ctx) {
25
25
  const { agent } = ctx;
26
- const { maxTokens, migrationAgent, onHandoff } = this.options;
26
+ const { maxTokens, migrationAgent, onBeforeMigrate, onHandoff } = this.options;
27
27
  const promptTokens = agent.lastUsage?.prompt_tokens;
28
28
  if (promptTokens == null)
29
29
  return;
30
30
  if (!TaskMigrationService.shouldMigrate(promptTokens, maxTokens))
31
31
  return;
32
- log.warn(`[autoMigrate] Token 使用量已达 ${promptTokens},超出限制 ${maxTokens},开始自动迁移……`);
32
+ // 迁移前钩子:由上层(CLI)处理用户提示
33
+ if (onBeforeMigrate) {
34
+ try {
35
+ await onBeforeMigrate(promptTokens, maxTokens, agent);
36
+ }
37
+ catch (err) {
38
+ log.error(`[autoMigrate] onBeforeMigrate 回调失败: ${err?.message ?? err}`);
39
+ }
40
+ }
33
41
  try {
34
42
  const historyText = this.serializeMessages(agent.messages);
35
43
  const migrationResult = await migrationAgent.send(historyText);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-zen/agents-sdk",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "description": "AI-Zen Agents SDK — shared business logic for CLI and Desktop",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",