@inferencesh/sdk 0.38.16 → 0.39.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.
package/CHANGELOG.md CHANGED
@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ### Changed
11
+ - `tasks.run()` and `tasks.watch().done` reject with `TaskCancelledError` when the task is cancelled and `TaskFailedError` when it fails, instead of a plain `Error`. Both carry the task as `error.task`. A task cancelled after its app returned a result now ends cancelled with that result as its `output`, so a caller that cancels reads the partial result from `error.task.output`. Messages are unchanged (`task cancelled`, the task's error). `isTaskCancelledError` and `isTaskFailedError` are the type guards.
12
+
10
13
  ## [0.38.0] - 2026-10-29
11
14
 
12
15
  ### Changed
package/README.md CHANGED
@@ -176,6 +176,22 @@ const task = await client.tasks.run(
176
176
  await client.tasks.cancel(task.id);
177
177
  ```
178
178
 
179
+ A `run()` that is waiting on a task rejects with `TaskCancelledError` when the task is cancelled. A task cancelled after its app returned a result ends cancelled and keeps that result as its `output` (billed like any other); it is `null` when the app returned nothing. The task is on the error:
180
+
181
+ ```typescript
182
+ import { isTaskCancelledError } from '@inferencesh/sdk';
183
+
184
+ try {
185
+ const result = await client.tasks.run({ app: 'long-running-app', input: {} });
186
+ } catch (error) {
187
+ if (isTaskCancelledError(error) && error.task.output != null) {
188
+ console.log('cancelled, partial result:', error.task.output);
189
+ }
190
+ }
191
+ ```
192
+
193
+ A failed task rejects with `TaskFailedError` the same way: the task's error as the message and the task on `error.task`.
194
+
179
195
  ### Stream Functions (Live Sockets)
180
196
 
181
197
  A stream function keeps a socket open with its caller for the life of the task: frames go both ways until the caller closes or the app returns. `client.live` starts the task and dials its socket; the run response carries where to dial (`task.socket`).
@@ -572,6 +588,8 @@ Runs a task on inference.sh.
572
588
  | `onPartialUpdate` | `function` | - | Callback for partial NDJSON stream updates `(task, fields)` |
573
589
  | `maxReconnects` | `number` | `5` | Max poll retries when `stream: false` |
574
590
 
591
+ **Returns:** the completed task. Rejects with `TaskFailedError` when the task fails and `TaskCancelledError` when it is cancelled; both carry the task as `error.task`, so a cancelled task's output (when its app returned one) is `error.task.output`.
592
+
575
593
  ### `client.tasks.get(taskId)`
576
594
 
577
595
  Gets a task by ID.
@@ -74,13 +74,18 @@ export declare class TasksAPI {
74
74
  */
75
75
  stream(taskId: string): Promise<import("eventsource").EventSource | null>;
76
76
  /**
77
- * Run a task and optionally wait for completion
77
+ * Run a task and optionally wait for completion. With `wait` (the
78
+ * default) it resolves with the completed task and rejects with
79
+ * `TaskFailedError` or `TaskCancelledError`, both carrying the task; a
80
+ * cancelled task's output, when its app returned one, is on
81
+ * `error.task.output`.
78
82
  */
79
83
  run(params: ApiAppRunRequest, processedInput: unknown, options?: RunOptions): Promise<Task>;
80
84
  /**
81
85
  * Follows a task until it ends. `done` resolves with the task when it
82
- * completes and rejects when it fails or is cancelled; `stop` ends the
83
- * watch early and leaves `done` pending.
86
+ * completes and rejects with `TaskFailedError` or `TaskCancelledError`
87
+ * (the task, with any output, on `error.task`) when it fails or is
88
+ * cancelled; `stop` ends the watch early and leaves `done` pending.
84
89
  */
85
90
  watch(task: Pick<Task, 'id' | 'status'>, options?: WatchOptions): TaskWatch;
86
91
  private watchStream;
package/dist/api/tasks.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { streamUntilTerminal } from '../http/stream-until-terminal.js';
2
2
  import { PollManager } from '../http/poll.js';
3
+ import { TaskCancelledError, TaskFailedError } from '../http/errors.js';
3
4
  import { TaskStatusCompleted, TaskStatusFailed, TaskStatusCancelled, TaskStatusRejected, } from '../types.js';
4
5
  import { parseStatus } from '../utils.js';
5
6
  //TODO: This is ugly...
@@ -16,17 +17,22 @@ function stripTask(task) {
16
17
  session_id: task.session_id,
17
18
  };
18
19
  }
19
- /** Settles a watch when `data` carries a terminal status; no-op otherwise. */
20
+ /**
21
+ * Settles a watch when `data` carries a terminal status; no-op otherwise.
22
+ * Failed and cancelled reject with the task on the error: a task cancelled
23
+ * after its app returned a result ends cancelled with that result as its
24
+ * output, and the caller reads it from `TaskCancelledError.task`.
25
+ */
20
26
  function settleTerminal(data, stripped, settle) {
21
27
  switch (parseStatus(data.status)) {
22
28
  case TaskStatusCompleted:
23
29
  settle.resolve(stripped);
24
30
  break;
25
31
  case TaskStatusFailed:
26
- settle.reject(new Error(data.error || 'task failed'));
32
+ settle.reject(new TaskFailedError(stripped));
27
33
  break;
28
34
  case TaskStatusCancelled:
29
- settle.reject(new Error('task cancelled'));
35
+ settle.reject(new TaskCancelledError(stripped));
30
36
  break;
31
37
  case TaskStatusRejected:
32
38
  settle.reject(new Error(data.error || 'task rejected'));
@@ -101,7 +107,11 @@ export class TasksAPI {
101
107
  return this.http.createEventSource(`/tasks/${taskId}/stream`);
102
108
  }
103
109
  /**
104
- * Run a task and optionally wait for completion
110
+ * Run a task and optionally wait for completion. With `wait` (the
111
+ * default) it resolves with the completed task and rejects with
112
+ * `TaskFailedError` or `TaskCancelledError`, both carrying the task; a
113
+ * cancelled task's output, when its app returned one, is on
114
+ * `error.task.output`.
105
115
  */
106
116
  async run(params, processedInput, options = {}) {
107
117
  const { wait = true } = options;
@@ -116,8 +126,9 @@ export class TasksAPI {
116
126
  }
117
127
  /**
118
128
  * Follows a task until it ends. `done` resolves with the task when it
119
- * completes and rejects when it fails or is cancelled; `stop` ends the
120
- * watch early and leaves `done` pending.
129
+ * completes and rejects with `TaskFailedError` or `TaskCancelledError`
130
+ * (the task, with any output, on `error.task`) when it fails or is
131
+ * cancelled; `stop` ends the watch early and leaves `done` pending.
121
132
  */
122
133
  watch(task, options = {}) {
123
134
  const useStream = options.stream ?? this.http.getStreamDefault();
@@ -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.js';
7
+ import type { RequirementError, TaskDTO } from '../types.js';
8
8
  /**
9
9
  * General HTTP/API error thrown by the SDK.
10
10
  *
@@ -132,6 +132,36 @@ export declare class SessionEndedError extends SessionError {
132
132
  export declare class WorkerLostError extends SessionError {
133
133
  constructor(sessionId: string, responseBody?: string);
134
134
  }
135
+ /**
136
+ * Error `tasks.run()` and `tasks.watch().done` reject with when a task is
137
+ * cancelled. A task cancelled after its app returned a result keeps that
138
+ * result: `task.output` carries it, billed like any other. `task.output`
139
+ * is null when the app returned nothing.
140
+ *
141
+ * @example
142
+ * ```typescript
143
+ * try {
144
+ * const task = await client.tasks.run(params);
145
+ * } catch (e) {
146
+ * if (isTaskCancelledError(e) && e.task.output != null) {
147
+ * usePartialResult(e.task.output);
148
+ * }
149
+ * }
150
+ * ```
151
+ */
152
+ export declare class TaskCancelledError extends Error {
153
+ /** The task as it ended; its output when the app returned one before stopping. */
154
+ readonly task: TaskDTO;
155
+ constructor(task: TaskDTO);
156
+ }
157
+ /**
158
+ * Error `tasks.run()` and `tasks.watch().done` reject with when a task
159
+ * fails. The message is the task's error; `task` is the task as it ended.
160
+ */
161
+ export declare class TaskFailedError extends Error {
162
+ readonly task: TaskDTO;
163
+ constructor(task: TaskDTO);
164
+ }
135
165
  /**
136
166
  * Type guard for RequirementsNotMetException.
137
167
  * Handles cross-module issues where instanceof may fail.
@@ -145,3 +175,11 @@ export declare function isInferenceError(err: unknown): err is InferenceError;
145
175
  * Type guard for SessionError (or any subclass).
146
176
  */
147
177
  export declare function isSessionError(err: unknown): err is SessionError;
178
+ /**
179
+ * Type guard for TaskCancelledError.
180
+ */
181
+ export declare function isTaskCancelledError(err: unknown): err is TaskCancelledError;
182
+ /**
183
+ * Type guard for TaskFailedError.
184
+ */
185
+ export declare function isTaskFailedError(err: unknown): err is TaskFailedError;
@@ -154,6 +154,42 @@ export class WorkerLostError extends SessionError {
154
154
  this.name = 'WorkerLostError';
155
155
  }
156
156
  }
157
+ // Task outcome errors
158
+ /**
159
+ * Error `tasks.run()` and `tasks.watch().done` reject with when a task is
160
+ * cancelled. A task cancelled after its app returned a result keeps that
161
+ * result: `task.output` carries it, billed like any other. `task.output`
162
+ * is null when the app returned nothing.
163
+ *
164
+ * @example
165
+ * ```typescript
166
+ * try {
167
+ * const task = await client.tasks.run(params);
168
+ * } catch (e) {
169
+ * if (isTaskCancelledError(e) && e.task.output != null) {
170
+ * usePartialResult(e.task.output);
171
+ * }
172
+ * }
173
+ * ```
174
+ */
175
+ export class TaskCancelledError extends Error {
176
+ constructor(task) {
177
+ super('task cancelled');
178
+ this.name = 'TaskCancelledError';
179
+ this.task = task;
180
+ }
181
+ }
182
+ /**
183
+ * Error `tasks.run()` and `tasks.watch().done` reject with when a task
184
+ * fails. The message is the task's error; `task` is the task as it ended.
185
+ */
186
+ export class TaskFailedError extends Error {
187
+ constructor(task) {
188
+ super(task.error || 'task failed');
189
+ this.name = 'TaskFailedError';
190
+ this.task = task;
191
+ }
192
+ }
157
193
  // =============================================================================
158
194
  // Type Guards
159
195
  // =============================================================================
@@ -195,3 +231,25 @@ export function isSessionError(err) {
195
231
  return typeof e.sessionId === 'string' && typeof e.statusCode === 'number' &&
196
232
  ['SessionError', 'SessionNotFoundError', 'SessionExpiredError', 'SessionEndedError', 'WorkerLostError'].includes(e.name);
197
233
  }
234
+ /**
235
+ * Type guard for TaskCancelledError.
236
+ */
237
+ export function isTaskCancelledError(err) {
238
+ if (err instanceof TaskCancelledError)
239
+ return true;
240
+ if (typeof err !== 'object' || err === null)
241
+ return false;
242
+ const e = err;
243
+ return e.name === 'TaskCancelledError' && typeof e.task === 'object' && e.task !== null;
244
+ }
245
+ /**
246
+ * Type guard for TaskFailedError.
247
+ */
248
+ export function isTaskFailedError(err) {
249
+ if (err instanceof TaskFailedError)
250
+ return true;
251
+ if (typeof err !== 'object' || err === null)
252
+ return false;
253
+ const e = err;
254
+ return e.name === 'TaskFailedError' && typeof e.task === 'object' && e.task !== null;
255
+ }
package/dist/index.d.ts CHANGED
@@ -5,7 +5,7 @@ export { StreamManager, type StreamManagerOptions, type PartialDataWrapper } fro
5
5
  export { StreamableManager, type StreamableManagerOptions, type StreamReconnectOptions, type StreamableSource, type StreamRequest, type StreamRequestInit, type StreamableMessage, streamable, streamableRaw } from './http/streamable.js';
6
6
  export { PollManager, type PollManagerOptions } from './http/poll.js';
7
7
  export { isLoopbackHost, isHttpsOrLoopback } from './http/loopback.js';
8
- export { InferenceError, RequirementsNotMetException, SessionError, SessionNotFoundError, SessionExpiredError, SessionEndedError, WorkerLostError, isRequirementsNotMetException, isInferenceError, isSessionError, } from './http/errors.js';
8
+ export { InferenceError, RequirementsNotMetException, SessionError, SessionNotFoundError, SessionExpiredError, SessionEndedError, WorkerLostError, TaskCancelledError, TaskFailedError, isRequirementsNotMetException, isInferenceError, isSessionError, isTaskCancelledError, isTaskFailedError, } from './http/errors.js';
9
9
  export { TasksAPI, type RunOptions, type WatchOptions, type TaskWatch } from './api/tasks.js';
10
10
  export { SocketsAPI, type SocketTarget, type OpenSocketOptions } from './api/sockets.js';
11
11
  export { FilesAPI, type UploadFileOptions } from './api/files.js';
package/dist/index.js CHANGED
@@ -4,9 +4,9 @@ export { StreamManager } from './http/stream.js';
4
4
  export { StreamableManager, streamable, streamableRaw } from './http/streamable.js';
5
5
  export { PollManager } from './http/poll.js';
6
6
  export { isLoopbackHost, isHttpsOrLoopback } from './http/loopback.js';
7
- export { InferenceError, RequirementsNotMetException, SessionError, SessionNotFoundError, SessionExpiredError, SessionEndedError, WorkerLostError,
7
+ export { InferenceError, RequirementsNotMetException, SessionError, SessionNotFoundError, SessionExpiredError, SessionEndedError, WorkerLostError, TaskCancelledError, TaskFailedError,
8
8
  // Type guards (use these instead of instanceof for reliability)
9
- isRequirementsNotMetException, isInferenceError, isSessionError, } from './http/errors.js';
9
+ isRequirementsNotMetException, isInferenceError, isSessionError, isTaskCancelledError, isTaskFailedError, } from './http/errors.js';
10
10
  // API modules
11
11
  export { TasksAPI } from './api/tasks.js';
12
12
  export { SocketsAPI } from './api/sockets.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inferencesh/sdk",
3
- "version": "0.38.16",
3
+ "version": "0.39.0",
4
4
  "description": "Official JavaScript/TypeScript SDK for inference.sh - Run AI models with a simple API",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",