pulse-coder-engine 0.0.1-alpha.13 → 0.0.1-alpha.15

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.
package/dist/index.d.cts CHANGED
@@ -435,6 +435,19 @@ declare class Engine {
435
435
  * 运行AI循环
436
436
  */
437
437
  run(context: Context, options?: LoopOptions): Promise<string>;
438
+ /**
439
+ * 手动触发上下文压缩
440
+ * 默认复用 Engine 初始化时配置的 provider/model
441
+ */
442
+ compactContext(context: Context, options?: {
443
+ force?: boolean;
444
+ provider?: LLMProviderFactory;
445
+ model?: string;
446
+ }): Promise<{
447
+ didCompact: boolean;
448
+ reason?: string;
449
+ newMessages?: Context['messages'];
450
+ }>;
438
451
  /**
439
452
  * 获取插件状态
440
453
  */
package/dist/index.d.ts CHANGED
@@ -435,6 +435,19 @@ declare class Engine {
435
435
  * 运行AI循环
436
436
  */
437
437
  run(context: Context, options?: LoopOptions): Promise<string>;
438
+ /**
439
+ * 手动触发上下文压缩
440
+ * 默认复用 Engine 初始化时配置的 provider/model
441
+ */
442
+ compactContext(context: Context, options?: {
443
+ force?: boolean;
444
+ provider?: LLMProviderFactory;
445
+ model?: string;
446
+ }): Promise<{
447
+ didCompact: boolean;
448
+ reason?: string;
449
+ newMessages?: Context['messages'];
450
+ }>;
438
451
  /**
439
452
  * 获取插件状态
440
453
  */
package/dist/index.js CHANGED
@@ -391,6 +391,9 @@ async function loop(context, options) {
391
391
  const loopHooks = options?.hooks ?? {};
392
392
  while (true) {
393
393
  try {
394
+ if (options?.abortSignal?.aborted) {
395
+ return "Request aborted.";
396
+ }
394
397
  if (compactionAttempts < MAX_COMPACTION_ATTEMPTS) {
395
398
  const { didCompact, newMessages } = await maybeCompactContext(context, {
396
399
  provider: options?.provider,
@@ -404,6 +407,9 @@ async function loop(context, options) {
404
407
  continue;
405
408
  }
406
409
  }
410
+ if (options?.abortSignal?.aborted) {
411
+ return "Request aborted.";
412
+ }
407
413
  let tools = options?.tools || {};
408
414
  let systemPrompt = options?.systemPrompt;
409
415
  if (loopHooks.beforeLLMCall?.length) {
@@ -428,6 +434,9 @@ async function loop(context, options) {
428
434
  onClarificationRequest: options?.onClarificationRequest,
429
435
  abortSignal: options?.abortSignal
430
436
  };
437
+ if (options?.abortSignal?.aborted) {
438
+ return "Request aborted.";
439
+ }
431
440
  const result = streamTextAI(context.messages, tools, {
432
441
  abortSignal: options?.abortSignal,
433
442
  toolExecutionContext,
@@ -466,6 +475,9 @@ async function loop(context, options) {
466
475
  await hook({ context, finishReason, text });
467
476
  }
468
477
  }
478
+ if (options?.abortSignal?.aborted) {
479
+ return "Request aborted.";
480
+ }
469
481
  if (finishReason === "stop") {
470
482
  if (!text) {
471
483
  continue;
@@ -515,7 +527,7 @@ async function loop(context, options) {
515
527
  }
516
528
  if (isRetryableError(error)) {
517
529
  const delay = Math.min(2e3 * Math.pow(2, errorCount - 1), 3e4);
518
- await sleep(delay);
530
+ await sleep(delay, options?.abortSignal);
519
531
  continue;
520
532
  }
521
533
  return `Error: ${error?.message ?? String(error)}`;
@@ -526,8 +538,34 @@ function isRetryableError(error) {
526
538
  const status = error?.status ?? error?.statusCode;
527
539
  return status === 429 || status === 500 || status === 502 || status === 503;
528
540
  }
529
- function sleep(ms) {
530
- return new Promise((resolve) => setTimeout(resolve, ms));
541
+ function sleep(ms, abortSignal) {
542
+ return new Promise((resolve, reject) => {
543
+ if (abortSignal?.aborted) {
544
+ const abortError = new Error("Aborted");
545
+ abortError.name = "AbortError";
546
+ reject(abortError);
547
+ return;
548
+ }
549
+ let timeoutId;
550
+ const onAbort = () => {
551
+ clearTimeout(timeoutId);
552
+ if (abortSignal) {
553
+ abortSignal.removeEventListener("abort", onAbort);
554
+ }
555
+ const abortError = new Error("Aborted");
556
+ abortError.name = "AbortError";
557
+ reject(abortError);
558
+ };
559
+ timeoutId = setTimeout(() => {
560
+ if (abortSignal) {
561
+ abortSignal.removeEventListener("abort", onAbort);
562
+ }
563
+ resolve();
564
+ }, ms);
565
+ if (abortSignal) {
566
+ abortSignal.addEventListener("abort", onAbort, { once: true });
567
+ }
568
+ });
531
569
  }
532
570
 
533
571
  // src/tools/read.ts
@@ -2730,6 +2768,17 @@ var Engine = class {
2730
2768
  }
2731
2769
  return resultText;
2732
2770
  }
2771
+ /**
2772
+ * 手动触发上下文压缩
2773
+ * 默认复用 Engine 初始化时配置的 provider/model
2774
+ */
2775
+ async compactContext(context, options) {
2776
+ return await maybeCompactContext(context, {
2777
+ force: options?.force,
2778
+ provider: options?.provider ?? this.options.llmProvider,
2779
+ model: options?.model ?? this.options.model
2780
+ });
2781
+ }
2733
2782
  /**
2734
2783
  * 获取插件状态
2735
2784
  */