@opencow-ai/opencow-agent-sdk 0.4.24 → 0.4.25

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.
@@ -235,6 +235,10 @@ export declare const ENV_VARS: {
235
235
  readonly DEBUG_INPUT: EnvVarSpec;
236
236
  readonly DISABLE_EARLY_INPUT: EnvVarSpec;
237
237
  readonly ENABLE_EXTENDED_KEYS: EnvVarSpec;
238
+ readonly ENABLE_STREAM_WATCHDOG: EnvVarSpec;
239
+ readonly STREAM_IDLE_TIMEOUT_MS: EnvVarSpec;
240
+ readonly UNBOUNDED_CONNECTION_RETRY: EnvVarSpec;
241
+ readonly STREAM_REPLAY_RETRIES: EnvVarSpec;
238
242
  readonly USE_READABLE_STDIN: EnvVarSpec;
239
243
  };
240
244
  export type EnvVarKey = keyof typeof ENV_VARS;
@@ -26,6 +26,11 @@ export declare function adjustParamsForNonStreaming<T extends {
26
26
  * tool schemas, session identity, caching strategy, and host callbacks.
27
27
  */
28
28
  export type Options = {
29
+ /**
30
+ * 内部:由 replayStreamOnTransportError 置位。为 true 时,读流阶段的可重放传输错误
31
+ * (且尚无完整消息送出)作为异常抛给调用方重放,而不是转成 assistant 错误消息。
32
+ */
33
+ streamReplayEscape?: boolean;
29
34
  getToolPermissionContext: () => Promise<ToolPermissionContext>;
30
35
  model: string;
31
36
  toolChoice?: BetaToolChoiceTool | BetaToolChoiceAuto | undefined;
@@ -1,16 +1,48 @@
1
1
  import type Anthropic from '@anthropic-ai/sdk';
2
- import { APIError } from '@anthropic-ai/sdk';
2
+ import { APIConnectionError, APIError } from '@anthropic-ai/sdk';
3
3
  import type { QuerySource } from 'src/constants/querySource.js';
4
4
  import type { SystemAPIErrorMessage } from 'src/types/message.js';
5
5
  import type { ThinkingConfig } from '../../lib/thinking.js';
6
6
  export declare const BASE_DELAY_MS = 500;
7
+ /**
8
+ * 200 头已到、读 body 时连接被切断,Anthropic SDK 抛的是原始 TypeError,
9
+ * 不经过它自己的连接错误包装;不补包装就会落入下方「非 APIError 不重试」。
10
+ * 只认传输特征(cause 带系统错误码或固定文案),普通 TypeError 仍按程序错误处理。
11
+ */
12
+ export declare function wrapBodyTransportError(error: unknown): unknown;
13
+ /** 读流阶段的传输中断(对端关闭、socket 断开、空闲看门狗中止)可以整条请求重放;其余不行。 */
14
+ export declare function toReplayableStreamError(error: unknown): APIConnectionError | null;
15
+ /**
16
+ * 流式响应读到一半断开、且还没有任何完整消息送出时,整个流式请求重放。
17
+ * 只在宿主显式给出重放次数时启用(默认 0 = 关闭,行为不变)。
18
+ * `run(escapable)`:前 maxReplays 次尝试 escapable=true,允许 queryModel 把可重放的流错误
19
+ * 作为异常抛出(否则它会把错误转成一条 assistant 错误消息);最后一次 escapable=false,
20
+ * 错误照常转成消息,所以总尝试次数是 maxReplays + 1,最终报错文案与不开启时一致。
21
+ * 已有完整 assistant 消息送出(工具可能已在执行)则不重放,避免重复执行;
22
+ * 重放前送出的 stream_event 半截内容由新的 message_start 取代。
23
+ * 建立连接阶段的失败由 withRetry 处理,这里只管读流阶段。
24
+ */
25
+ export declare function replayStreamOnTransportError<T extends {
26
+ type: string;
27
+ }>(run: (escapable: boolean) => AsyncGenerator<T, void>, options: {
28
+ maxReplays: number;
29
+ signal?: AbortSignal;
30
+ delayMs?: (replay: number) => number;
31
+ }): AsyncGenerator<T | SystemAPIErrorMessage, void>;
32
+ export declare function getConnectionWaitDelay(attempt: number, backoff?: ConnectionWaitBackoff, random?: () => number): number;
7
33
  export interface RetryContext {
8
34
  maxTokensOverride?: number;
9
35
  model: string;
10
36
  thinkingConfig: ThinkingConfig;
11
37
  fastMode?: boolean;
12
38
  }
39
+ export interface ConnectionWaitBackoff {
40
+ initialMs: number;
41
+ maxMs: number;
42
+ }
13
43
  interface RetryOptions {
44
+ /** 连接错误等待的退避节奏,默认 5s 起翻倍、上限 60s;主要供测试缩短等待。 */
45
+ connectionWaitBackoff?: ConnectionWaitBackoff;
14
46
  maxRetries?: number;
15
47
  model: string;
16
48
  fallbackModel?: string;