@inferencesh/sdk 0.22.1 → 0.23.0

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 (78) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/dist/agent/actions.d.ts +2 -2
  3. package/dist/agent/actions.js +93 -21
  4. package/dist/agent/api.d.ts +3 -3
  5. package/dist/agent/api.js +1 -1
  6. package/dist/agent/context.d.ts +1 -1
  7. package/dist/agent/hooks.d.ts +2 -2
  8. package/dist/agent/hooks.js +1 -1
  9. package/dist/agent/index.d.ts +6 -6
  10. package/dist/agent/index.js +5 -5
  11. package/dist/agent/mcp-input.d.ts +1 -1
  12. package/dist/agent/provider.d.ts +1 -1
  13. package/dist/agent/provider.js +5 -5
  14. package/dist/agent/reducer.d.ts +1 -1
  15. package/dist/agent/reducer.js +2 -2
  16. package/dist/agent/types.d.ts +5 -5
  17. package/dist/api/agents.d.ts +8 -10
  18. package/dist/api/agents.js +45 -34
  19. package/dist/api/api-keys.d.ts +3 -3
  20. package/dist/api/apps.d.ts +4 -4
  21. package/dist/api/artifacts.d.ts +3 -3
  22. package/dist/api/chats.d.ts +4 -4
  23. package/dist/api/credentials.d.ts +3 -3
  24. package/dist/api/engines.d.ts +4 -4
  25. package/dist/api/files.d.ts +3 -3
  26. package/dist/api/flow-runs.d.ts +3 -3
  27. package/dist/api/flows.d.ts +4 -4
  28. package/dist/api/index.d.ts +4 -4
  29. package/dist/api/index.js +4 -4
  30. package/dist/api/knowledge.d.ts +4 -4
  31. package/dist/api/mcp-servers.d.ts +4 -4
  32. package/dist/api/projects.d.ts +4 -4
  33. package/dist/api/search.d.ts +3 -3
  34. package/dist/api/secrets.d.ts +3 -3
  35. package/dist/api/sessions.d.ts +3 -3
  36. package/dist/api/sockets.d.ts +6 -6
  37. package/dist/api/sockets.js +2 -2
  38. package/dist/api/tasks.d.ts +3 -3
  39. package/dist/api/tasks.js +4 -4
  40. package/dist/api/teams.d.ts +4 -4
  41. package/dist/api/write-bodies.d.ts +1 -1
  42. package/dist/delta.d.ts +1 -1
  43. package/dist/delta.js +1 -1
  44. package/dist/hook-builder.d.ts +1 -1
  45. package/dist/hook-builder.js +1 -1
  46. package/dist/http/client.d.ts +2 -2
  47. package/dist/http/client.js +2 -2
  48. package/dist/http/errors.d.ts +1 -1
  49. package/dist/http/index.d.ts +4 -4
  50. package/dist/http/index.js +4 -4
  51. package/dist/http/response.d.ts +1 -1
  52. package/dist/http/stream-until-terminal.d.ts +22 -8
  53. package/dist/http/stream-until-terminal.js +17 -9
  54. package/dist/http/streamable.d.ts +63 -7
  55. package/dist/http/streamable.js +277 -119
  56. package/dist/index.d.ts +65 -65
  57. package/dist/index.js +53 -53
  58. package/dist/live/schema.js +1 -1
  59. package/dist/live/session.d.ts +3 -3
  60. package/dist/live/session.js +2 -2
  61. package/dist/proxy/express.d.ts +1 -1
  62. package/dist/proxy/express.js +1 -1
  63. package/dist/proxy/hono.d.ts +1 -1
  64. package/dist/proxy/hono.js +1 -1
  65. package/dist/proxy/index.js +0 -2
  66. package/dist/proxy/nextjs.d.ts +3 -3
  67. package/dist/proxy/nextjs.js +2 -2
  68. package/dist/proxy/remix.d.ts +1 -1
  69. package/dist/proxy/remix.js +1 -1
  70. package/dist/proxy/svelte.d.ts +1 -1
  71. package/dist/proxy/svelte.js +1 -1
  72. package/dist/tool-builder.d.ts +1 -1
  73. package/dist/tool-builder.js +1 -1
  74. package/dist/types.d.ts +164 -40
  75. package/dist/types.js +33 -9
  76. package/dist/utils.d.ts +2 -2
  77. package/dist/utils.js +2 -2
  78. package/package.json +2 -1
@@ -1,6 +1,6 @@
1
- import { HttpClient } from '../http/client';
2
- import type { Response } from '../http/response';
3
- import { FlowRunDTO as FlowRun, CursorListRequest, CursorListResponse } from '../types';
1
+ import { HttpClient } from '../http/client.js';
2
+ import type { Response } from '../http/response.js';
3
+ import { FlowRunDTO as FlowRun, CursorListRequest, CursorListResponse } from '../types.js';
4
4
  /**
5
5
  * Flow Runs API
6
6
  */
@@ -1,7 +1,7 @@
1
- import { HttpClient } from '../http/client';
2
- import type { Response } from '../http/response';
3
- import type { FlowPatchBody, FlowUpdateBody, FlowViewportBody } from './write-bodies';
4
- import { FlowDTO as Flow, FlowVersionDTO, AppDTO as App, CursorListRequest, CursorListResponse } from '../types';
1
+ import { HttpClient } from '../http/client.js';
2
+ import type { Response } from '../http/response.js';
3
+ import type { FlowPatchBody, FlowUpdateBody, FlowViewportBody } from './write-bodies.js';
4
+ import { FlowDTO as Flow, FlowVersionDTO, AppDTO as App, CursorListRequest, CursorListResponse } from '../types.js';
5
5
  /**
6
6
  * Flows API
7
7
  */
@@ -1,4 +1,4 @@
1
- export { TasksAPI, createTasksAPI, type RunOptions } from './tasks';
2
- export { FilesAPI, createFilesAPI, type UploadFileOptions } from './files';
3
- export { AgentsAPI, createAgentsAPI, Agent, type AgentOptions, type SendMessageOptions, type AgentRunOptions } from './agents';
4
- export { SessionsAPI, createSessionsAPI } from './sessions';
1
+ export { TasksAPI, createTasksAPI, type RunOptions } from './tasks.js';
2
+ export { FilesAPI, createFilesAPI, type UploadFileOptions } from './files.js';
3
+ export { AgentsAPI, createAgentsAPI, Agent, type AgentOptions, type SendMessageOptions, type AgentRunOptions } from './agents.js';
4
+ export { SessionsAPI, createSessionsAPI } from './sessions.js';
package/dist/api/index.js CHANGED
@@ -1,4 +1,4 @@
1
- export { TasksAPI, createTasksAPI } from './tasks';
2
- export { FilesAPI, createFilesAPI } from './files';
3
- export { AgentsAPI, createAgentsAPI, Agent } from './agents';
4
- export { SessionsAPI, createSessionsAPI } from './sessions';
1
+ export { TasksAPI, createTasksAPI } from './tasks.js';
2
+ export { FilesAPI, createFilesAPI } from './files.js';
3
+ export { AgentsAPI, createAgentsAPI, Agent } from './agents.js';
4
+ export { SessionsAPI, createSessionsAPI } from './sessions.js';
@@ -1,7 +1,7 @@
1
- import { HttpClient } from '../http/client';
2
- import type { Response } from '../http/response';
3
- import type { KnowledgeUpdateBody } from './write-bodies';
4
- import { KnowledgeDTO, KnowledgeVersionDTO, KnowledgeCreateRequest, SkillDTO, SkillVersionDTO, CursorListRequest, CursorListResponse, PublicSkillStoreDTO } from '../types';
1
+ import { HttpClient } from '../http/client.js';
2
+ import type { Response } from '../http/response.js';
3
+ import type { KnowledgeUpdateBody } from './write-bodies.js';
4
+ import { KnowledgeDTO, KnowledgeVersionDTO, KnowledgeCreateRequest, SkillDTO, SkillVersionDTO, CursorListRequest, CursorListResponse, PublicSkillStoreDTO } from '../types.js';
5
5
  /**
6
6
  * Knowledge API
7
7
  */
@@ -1,7 +1,7 @@
1
- import { HttpClient } from '../http/client';
2
- import type { Response } from '../http/response';
3
- import type { MCPServerUpdateBody } from './write-bodies';
4
- import { MCPServerDTO, CursorListRequest, CursorListResponse } from '../types';
1
+ import { HttpClient } from '../http/client.js';
2
+ import type { Response } from '../http/response.js';
3
+ import type { MCPServerUpdateBody } from './write-bodies.js';
4
+ import { MCPServerDTO, CursorListRequest, CursorListResponse } from '../types.js';
5
5
  /**
6
6
  * MCP Servers API
7
7
  */
@@ -1,7 +1,7 @@
1
- import { HttpClient } from '../http/client';
2
- import type { Response } from '../http/response';
3
- import type { ProjectWriteBody } from './write-bodies';
4
- import { ProjectDTO, CursorListRequest, CursorListResponse } from '../types';
1
+ import { HttpClient } from '../http/client.js';
2
+ import type { Response } from '../http/response.js';
3
+ import type { ProjectWriteBody } from './write-bodies.js';
4
+ import { ProjectDTO, CursorListRequest, CursorListResponse } from '../types.js';
5
5
  /**
6
6
  * Projects API
7
7
  */
@@ -1,6 +1,6 @@
1
- import { HttpClient } from '../http/client';
2
- import type { Response } from '../http/response';
3
- import { SuggestRequest, SuggestResponse } from '../types';
1
+ import { HttpClient } from '../http/client.js';
2
+ import type { Response } from '../http/response.js';
3
+ import { SuggestRequest, SuggestResponse } from '../types.js';
4
4
  /**
5
5
  * Search API
6
6
  */
@@ -1,6 +1,6 @@
1
- import { HttpClient } from '../http/client';
2
- import type { Response } from '../http/response';
3
- import { SecretDTO, SecretCreateRequest, SecretUpdateRequest, CursorListRequest, CursorListResponse } from '../types';
1
+ import { HttpClient } from '../http/client.js';
2
+ import type { Response } from '../http/response.js';
3
+ import { SecretDTO, SecretCreateRequest, SecretUpdateRequest, CursorListRequest, CursorListResponse } from '../types.js';
4
4
  /**
5
5
  * Secrets API
6
6
  */
@@ -1,9 +1,9 @@
1
1
  /**
2
2
  * Sessions API - namespaced session operations.
3
3
  */
4
- import { HttpClient } from '../http/client';
5
- import type { Response } from '../http/response';
6
- import { AppSessionDTO } from '../types';
4
+ import { HttpClient } from '../http/client.js';
5
+ import type { Response } from '../http/response.js';
6
+ import { AppSessionDTO } from '../types.js';
7
7
  /**
8
8
  * Sessions API for managing stateful worker leases.
9
9
  *
@@ -1,9 +1,9 @@
1
- import { HttpClient } from '../http/client';
2
- import type { Response } from '../http/response';
3
- import { LiveSession, type LiveHandlers, type WebSocketConstructor } from '../live/session';
4
- import type { JsonSchema } from '../live/schema';
5
- import { CursorListRequest, CursorListResponse, SocketAccess, SocketDTO, TaskDTO as Task } from '../types';
6
- import type { TasksAPI } from './tasks';
1
+ import { HttpClient } from '../http/client.js';
2
+ import type { Response } from '../http/response.js';
3
+ import { LiveSession, type LiveHandlers, type WebSocketConstructor } from '../live/session.js';
4
+ import type { JsonSchema } from '../live/schema.js';
5
+ import { CursorListRequest, CursorListResponse, SocketAccess, SocketDTO, TaskDTO as Task } from '../types.js';
6
+ import type { TasksAPI } from './tasks.js';
7
7
  /** What identifies the socket to open: the run response (which carries the access), a task, or a task id. */
8
8
  export type SocketTarget = (Pick<Task, 'id' | 'status'> & {
9
9
  socket?: SocketAccess;
@@ -1,5 +1,5 @@
1
- import { LiveSession } from '../live/session';
2
- import { OpEqual } from '../types';
1
+ import { LiveSession } from '../live/session.js';
2
+ import { OpEqual } from '../types.js';
3
3
  /**
4
4
  * Sockets API: the duplex connection of a stream task.
5
5
  *
@@ -1,6 +1,6 @@
1
- import { HttpClient } from '../http/client';
2
- import type { Response } from '../http/response';
3
- import { TaskDTO as Task, TaskLogsDTO, TaskTimingsDTO, ApiAppRunRequest, TaskResultDTO, CursorListRequest, CursorListResponse, TaskFileDTO, DeleteTaskFilesResponse } from '../types';
1
+ import { HttpClient } from '../http/client.js';
2
+ import type { Response } from '../http/response.js';
3
+ import { TaskDTO as Task, TaskLogsDTO, TaskTimingsDTO, ApiAppRunRequest, TaskResultDTO, CursorListRequest, CursorListResponse, TaskFileDTO, DeleteTaskFilesResponse } from '../types.js';
4
4
  /** How to follow a task while it runs; see TasksAPI.watch. */
5
5
  export interface WatchOptions {
6
6
  /** Callback for real-time status updates */
package/dist/api/tasks.js CHANGED
@@ -1,7 +1,7 @@
1
- import { streamUntilTerminal } from '../http/stream-until-terminal';
2
- import { PollManager } from '../http/poll';
3
- import { TaskStatusCompleted, TaskStatusFailed, TaskStatusCancelled, } from '../types';
4
- import { parseStatus } from '../utils';
1
+ import { streamUntilTerminal } from '../http/stream-until-terminal.js';
2
+ import { PollManager } from '../http/poll.js';
3
+ import { TaskStatusCompleted, TaskStatusFailed, TaskStatusCancelled, } from '../types.js';
4
+ import { parseStatus } from '../utils.js';
5
5
  //TODO: This is ugly...
6
6
  function stripTask(task) {
7
7
  return {
@@ -1,7 +1,7 @@
1
- import { HttpClient } from '../http/client';
2
- import type { Response } from '../http/response';
3
- import type { TeamUpdateBody } from './write-bodies';
4
- import { MeResponse, TeamDTO, TeamMemberDTO, TeamInviteDTO, TeamCreateRequest, TeamMemberAddRequest, TeamInviteCreateRequest, AvailabilityResponse } from '../types';
1
+ import { HttpClient } from '../http/client.js';
2
+ import type { Response } from '../http/response.js';
3
+ import type { TeamUpdateBody } from './write-bodies.js';
4
+ import { MeResponse, TeamDTO, TeamMemberDTO, TeamInviteDTO, TeamCreateRequest, TeamMemberAddRequest, TeamInviteCreateRequest, AvailabilityResponse } from '../types.js';
5
5
  /**
6
6
  * Teams API
7
7
  */
@@ -12,7 +12,7 @@
12
12
  * chats, engines and teams send PATCH; apps.patch and flows.patch are the
13
13
  * partial siblings of the POST updates that also edit a version.
14
14
  */
15
- import type { AgentConfigInput, AgentImages, AppDTO, ChatDTO, EngineDTO, FlowDTO, FlowVersionDTO, KnowledgeDTO, MCPServerAuthType, ProjectDTO, TeamDTO, Visibility } from '../types';
15
+ import type { AgentConfigInput, AgentImages, AppDTO, ChatDTO, EngineDTO, FlowDTO, FlowVersionDTO, KnowledgeDTO, MCPServerAuthType, ProjectDTO, TeamDTO, Visibility } from '../types.js';
16
16
  /**
17
17
  * POST /agents/{id}. Visibility and images, and a new version when
18
18
  * `version` is set. Name, title, harness, placement: set at creation.
package/dist/delta.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export type { DeltaEvent } from './types';
1
+ export type { DeltaEvent } from './types.js';
2
2
  export type FieldTags = Record<string, {
3
3
  merge: string;
4
4
  }>;
package/dist/delta.js CHANGED
@@ -1,4 +1,4 @@
1
- import { LLMDelta_fieldTags, ToolCallDelta_fieldTags, ToolCallFunctionDelta_fieldTags, MergeStrategyConcat, MergeStrategyIndexed, MergeStrategyNested, MergeStrategyReplace, } from './types';
1
+ import { LLMDelta_fieldTags, ToolCallDelta_fieldTags, ToolCallFunctionDelta_fieldTags, MergeStrategyConcat, MergeStrategyIndexed, MergeStrategyNested, MergeStrategyReplace, } from './types.js';
2
2
  function mergeField(current, incoming, strategy, childTags, registry) {
3
3
  if (incoming == null)
4
4
  return current;
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Hook Builder - Fluent API for defining lifecycle hooks
3
3
  */
4
- import { LifecycleHookConfig, HookEvent, BuiltinHook } from './types';
4
+ import { LifecycleHookConfig, HookEvent, BuiltinHook } from './types.js';
5
5
  export declare class LifecycleHookBuilder {
6
6
  private event;
7
7
  private handlerType;
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Hook Builder - Fluent API for defining lifecycle hooks
3
3
  */
4
- import { HookHandlerWebhook, HookHandlerTask, HookHandlerBuiltin, BuiltinHookBeltSuggest, BuiltinHookBeltExtract, HookEventTurnStart, HookEventAgentComplete, HookEventPreCompact, } from './types';
4
+ import { HookHandlerWebhook, HookHandlerTask, HookHandlerBuiltin, BuiltinHookBeltSuggest, BuiltinHookBeltExtract, HookEventTurnStart, HookEventAgentComplete, HookEventPreCompact, } from './types.js';
5
5
  // =============================================================================
6
6
  // Hook Builder
7
7
  // =============================================================================
@@ -1,4 +1,4 @@
1
- import type { Response } from './response';
1
+ import type { Response } from './response.js';
2
2
  import { EventSource } from 'eventsource';
3
3
  /** What a failed request was sent with, for an error handler that acts on it. */
4
4
  export interface FailedRequest {
@@ -21,7 +21,7 @@ export type ErrorHandler = (error: unknown, retry: () => Promise<unknown>, reque
21
21
  export type HttpFetchInit = Omit<RequestInit, 'headers' | 'credentials'> & {
22
22
  headers?: Record<string, string>;
23
23
  };
24
- export type MessageHandler = (messages: import('../types').ResponseMessage[]) => void;
24
+ export type MessageHandler = (messages: import('../types.js').ResponseMessage[]) => void;
25
25
  export interface HttpClientConfig {
26
26
  /** Your inference.sh API key (required unless using proxyUrl) */
27
27
  apiKey?: string;
@@ -1,6 +1,6 @@
1
- import { InferenceError, RequirementsNotMetException } from './errors';
1
+ import { InferenceError, RequirementsNotMetException } from './errors.js';
2
2
  import { EventSource } from 'eventsource';
3
- import { SDK_VERSION } from '../version';
3
+ import { SDK_VERSION } from '../version.js';
4
4
  /**
5
5
  * Low-level HTTP client for inference.sh API
6
6
  *
@@ -4,7 +4,7 @@
4
4
  * Note: types.ts contains interfaces (APIError, RequirementsNotMetError) that describe
5
5
  * the API response data shapes. These classes are throwable Error subclasses for SDK use.
6
6
  */
7
- import type { RequirementError } from '../types';
7
+ import type { RequirementError } from '../types.js';
8
8
  /**
9
9
  * General HTTP/API error thrown by the SDK.
10
10
  *
@@ -1,4 +1,4 @@
1
- export { HttpClient, type HttpClientConfig, type ErrorHandler, type FailedRequest, type HttpFetchInit, createHttpClient } from './client';
2
- export { StreamManager, type StreamManagerOptions, type PartialDataWrapper } from './stream';
3
- export { streamable, streamableRaw, StreamableManager, type StreamableOptions, type StreamableMessage, type StreamableManagerOptions, type StreamableSource, type StreamRequest, type StreamRequestInit } from './streamable';
4
- export { InferenceError, RequirementsNotMetException } from './errors';
1
+ export { HttpClient, type HttpClientConfig, type ErrorHandler, type FailedRequest, type HttpFetchInit, createHttpClient } from './client.js';
2
+ export { StreamManager, type StreamManagerOptions, type PartialDataWrapper } from './stream.js';
3
+ export { streamable, streamableRaw, StreamableManager, type StreamableOptions, type StreamableMessage, type StreamableManagerOptions, type StreamReconnectOptions, type StreamableSource, type StreamRequest, type StreamRequestInit } from './streamable.js';
4
+ export { InferenceError, RequirementsNotMetException } from './errors.js';
@@ -1,4 +1,4 @@
1
- export { HttpClient, createHttpClient } from './client';
2
- export { StreamManager } from './stream';
3
- export { streamable, streamableRaw, StreamableManager } from './streamable';
4
- export { InferenceError, RequirementsNotMetException } from './errors';
1
+ export { HttpClient, createHttpClient } from './client.js';
2
+ export { StreamManager } from './stream.js';
3
+ export { streamable, streamableRaw, StreamableManager } from './streamable.js';
4
+ export { InferenceError, RequirementsNotMetException } from './errors.js';
@@ -1,4 +1,4 @@
1
- import type { ResponseMessage } from '../types';
1
+ import type { ResponseMessage } from '../types.js';
2
2
  /**
3
3
  * Response wraps the V3 API envelope — every API call returns this.
4
4
  * Access .data for the DTO and .messages for any warnings the server surfaced.
@@ -2,15 +2,21 @@
2
2
  * Follow a resource's NDJSON stream until the resource reaches a terminal
3
3
  * state.
4
4
  *
5
- * StreamableManager never reconnects, and a stream can close cleanly before
6
- * the resource is done (load balancer idle timeout, API deploy). A wait built
7
- * on it must not hang then. This helper owns that contract for every
8
- * "stream until done" wait: when the stream ends early it fetches the
9
- * resource once (`reconcile`) and settles with what that shows, or rejects
10
- * when the resource is still not terminal. sdk-py's `wait_for_completion`
11
- * does the same final GET.
5
+ * The stream reconnects after a network failure or a silent connection
6
+ * (StreamableManager `reconnect`, on by default here). The API does not
7
+ * replay what a dropped stream missed, so every reconnect fetches the
8
+ * resource (`resync`, default `reconcile`) before reading on; a resource
9
+ * that finished meanwhile settles the wait.
10
+ *
11
+ * A stream can also close cleanly before the resource is done (load balancer
12
+ * idle timeout, API deploy). A wait built on it must not hang then. This
13
+ * helper owns that contract for every "stream until done" wait: when the
14
+ * stream ends early it fetches the resource once (`reconcile`) and settles
15
+ * with what that shows, or rejects when the resource is still not terminal.
16
+ * sdk-py's `wait_for_completion` does the same final GET. The wait rejects
17
+ * with the stream's error once it stops retrying.
12
18
  */
13
- import { type StreamableManagerOptions, type StreamRequest } from './streamable';
19
+ import { type StreamableManagerOptions, type StreamReconnectOptions, type StreamRequest } from './streamable.js';
14
20
  /** Settles a stream-until-terminal wait. Calls after the first are ignored. */
15
21
  export interface TerminalSettler<R> {
16
22
  resolve(value: R): void;
@@ -28,6 +34,14 @@ export type StreamUntilTerminalSpec<T> = Pick<StreamableManagerOptions<T>, 'body
28
34
  * rejects the wait with `closedMessage`.
29
35
  */
30
36
  reconcile: () => Promise<void>;
37
+ /**
38
+ * Called after the stream reconnected, before it is read again: fetch the
39
+ * resource and report it, settling if it is terminal (default `reconcile`).
40
+ * A rejection counts as a failed reconnect attempt.
41
+ */
42
+ resync?: () => Promise<void>;
43
+ /** How the stream reconnects (default true: StreamReconnectOptions defaults); false fails the wait on the first error. */
44
+ reconnect?: boolean | StreamReconnectOptions;
31
45
  /** Rejection message when the stream closed and the resource is still not terminal. */
32
46
  closedMessage: string;
33
47
  /**
@@ -2,15 +2,21 @@
2
2
  * Follow a resource's NDJSON stream until the resource reaches a terminal
3
3
  * state.
4
4
  *
5
- * StreamableManager never reconnects, and a stream can close cleanly before
6
- * the resource is done (load balancer idle timeout, API deploy). A wait built
7
- * on it must not hang then. This helper owns that contract for every
8
- * "stream until done" wait: when the stream ends early it fetches the
9
- * resource once (`reconcile`) and settles with what that shows, or rejects
10
- * when the resource is still not terminal. sdk-py's `wait_for_completion`
11
- * does the same final GET.
5
+ * The stream reconnects after a network failure or a silent connection
6
+ * (StreamableManager `reconnect`, on by default here). The API does not
7
+ * replay what a dropped stream missed, so every reconnect fetches the
8
+ * resource (`resync`, default `reconcile`) before reading on; a resource
9
+ * that finished meanwhile settles the wait.
10
+ *
11
+ * A stream can also close cleanly before the resource is done (load balancer
12
+ * idle timeout, API deploy). A wait built on it must not hang then. This
13
+ * helper owns that contract for every "stream until done" wait: when the
14
+ * stream ends early it fetches the resource once (`reconcile`) and settles
15
+ * with what that shows, or rejects when the resource is still not terminal.
16
+ * sdk-py's `wait_for_completion` does the same final GET. The wait rejects
17
+ * with the stream's error once it stops retrying.
12
18
  */
13
- import { StreamableManager } from './streamable';
19
+ import { StreamableManager } from './streamable.js';
14
20
  /**
15
21
  * `build` receives the settler and returns the stream spec; its handlers call
16
22
  * `settle.resolve`/`settle.reject` once the resource is terminal. Settling
@@ -39,10 +45,12 @@ export function streamUntilTerminal(build) {
39
45
  reject(error);
40
46
  },
41
47
  };
42
- const { events, reconcile, closedMessage, onStopped, keepOpen, ...managerOptions } = build(settle);
48
+ const { events, reconcile, resync = reconcile, reconnect = true, closedMessage, onStopped, keepOpen, ...managerOptions } = build(settle);
43
49
  let streamError = null;
44
50
  manager = new StreamableManager({
45
51
  ...managerOptions,
52
+ reconnect,
53
+ onReconnect: () => resync(),
46
54
  onError: (error) => {
47
55
  streamError = error;
48
56
  },
@@ -46,6 +46,28 @@ export interface StreamableMessage<T = unknown> {
46
46
  * `source` is the URL to fetch, or a StreamRequest that sends the request.
47
47
  */
48
48
  export declare function streamable<T = unknown>(source: string | StreamRequest, options?: StreamableOptions): AsyncGenerator<T>;
49
+ /**
50
+ * Raw streamable that yields the full message including wrapper.
51
+ * Use this when you need access to event type or fields.
52
+ */
53
+ export declare function streamableRaw<T = unknown>(source: string | StreamRequest, options?: StreamableOptions): AsyncGenerator<StreamableMessage<T> | T>;
54
+ /** How a StreamableManager reconnects after its stream fails. */
55
+ export interface StreamReconnectOptions {
56
+ /**
57
+ * Failed connection attempts in a row before the manager gives up and calls
58
+ * `onError` (default 10). Any line received, heartbeats included, resets the count.
59
+ */
60
+ maxAttempts?: number;
61
+ /** Delay before the first retry in ms (default 1000). It doubles per attempt, with jitter. */
62
+ baseDelayMs?: number;
63
+ /** Longest delay between retries in ms (default 30000). */
64
+ maxDelayMs?: number;
65
+ /**
66
+ * Reconnect when nothing, not even a heartbeat, arrives for this many ms
67
+ * (default 30000; the API sends a heartbeat every 10s). 0 turns it off.
68
+ */
69
+ idleTimeoutMs?: number;
70
+ }
49
71
  /**
50
72
  * StreamableManager provides a callback-based API similar to StreamManager
51
73
  * but uses fetch + ReadableStream instead of EventSource.
@@ -69,19 +91,46 @@ export type StreamableManagerOptions<T> = StreamableSource & {
69
91
  onData?: (data: T) => void;
70
92
  /** Called for partial updates with fields list */
71
93
  onPartialData?: (data: T, fields: string[]) => void;
72
- /** Called on error */
94
+ /**
95
+ * Called when the stream fails. With `reconnect`, only once the manager
96
+ * gives up: a refusal no retry would change (4xx other than 408/429), or
97
+ * `maxAttempts` failures in a row.
98
+ */
73
99
  onError?: (error: Error) => void;
74
100
  /** Called when stream starts */
75
101
  onStart?: () => void;
76
- /** Called when stream ends */
102
+ /**
103
+ * Called once when the manager is done: the stream ended cleanly, stop()
104
+ * was called, or it failed and will not retry.
105
+ */
77
106
  onEnd?: () => void;
78
107
  /** Called for delta events (streaming token deltas) */
79
108
  onDelta?: (delta: Record<string, any>, seq: number) => void;
109
+ /**
110
+ * Reconnect after the stream fails or goes silent (default false: a failure
111
+ * ends the stream). `true` uses the StreamReconnectOptions defaults. A clean
112
+ * end of the stream is not retried.
113
+ */
114
+ reconnect?: boolean | StreamReconnectOptions;
115
+ /** With `reconnect`: the stream failed and the manager retries after `delayMs`. */
116
+ onRetry?: (error: Error, attempt: number, delayMs: number) => void;
117
+ /**
118
+ * With `reconnect`: a retry got a new connection. Events sent while the
119
+ * stream was down are lost, so this is where the caller fetches the current
120
+ * state. The manager reads the new stream only after the returned promise
121
+ * settles, so what it reads is newer than what the caller fetched; a
122
+ * rejection counts as a failed attempt.
123
+ */
124
+ onReconnect?: (attempt: number) => void | Promise<void>;
80
125
  };
81
126
  export declare class StreamableManager<T> {
82
127
  private options;
83
128
  private abortController;
84
129
  private isRunning;
130
+ /** Bumped by start() and stop(), so a stopped run never acts again. */
131
+ private runId;
132
+ /** Ends a pending retry wait early (stop, online, tab visible). */
133
+ private wakeRetry;
85
134
  private eventListeners;
86
135
  constructor(options: StreamableManagerOptions<T>);
87
136
  /**
@@ -90,10 +139,17 @@ export declare class StreamableManager<T> {
90
139
  */
91
140
  addEventListener<E = unknown>(eventName: string, callback: (data: E, fields?: string[]) => void): () => void;
92
141
  start(): Promise<void>;
142
+ /**
143
+ * One connection: open the stream and dispatch its messages until it ends.
144
+ * Resolves on a clean end, throws when the request or the read fails.
145
+ */
146
+ private connect;
147
+ /** Route one message to onDelta, a typed event listener, onPartialData or onData. */
148
+ private dispatch;
149
+ /**
150
+ * Wait `delayMs` before the next attempt. In a browser, coming back online
151
+ * or the tab becoming visible retries at once; stop() ends the wait.
152
+ */
153
+ private waitToRetry;
93
154
  stop(): void;
94
155
  }
95
- /**
96
- * Raw streamable that yields the full message including wrapper.
97
- * Use this when you need access to event type or fields.
98
- */
99
- export declare function streamableRaw<T = unknown>(source: string | StreamRequest, options?: StreamableOptions): AsyncGenerator<StreamableMessage<T> | T>;