pendra 0.19.0 → 0.21.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/README.md CHANGED
@@ -138,7 +138,7 @@ Embeddings, reranking, image generation, transcriptions and speech carry the
138
138
  same field:
139
139
 
140
140
  ```typescript
141
- const embeddings = await client.embeddings.create({ model: 'nomic-embed-text:latest', input: 'hi' });
141
+ const embeddings = await client.embeddings.create({ model: 'embeddinggemma:300m', input: 'hi' });
142
142
  console.log(embeddings.pendra?.worker?.id);
143
143
  ```
144
144
 
package/dist/index.d.mts CHANGED
@@ -19,6 +19,10 @@ interface Usage {
19
19
  * on {@link ChatCompletion.pendra} as a `truncated_during_reasoning` notice.
20
20
  */
21
21
  completion_tokens_details?: CompletionTokensDetails | null;
22
+ /** Breakdown of `prompt_tokens`. */
23
+ prompt_tokens_details?: {
24
+ cached_tokens?: number;
25
+ } | null;
22
26
  }
23
27
  interface ChatCompletionMessageToolCall {
24
28
  /**
@@ -116,10 +120,11 @@ interface PendraNotice {
116
120
  message: string;
117
121
  }
118
122
  /**
119
- * Per-request performance for a streaming completion, on the last chunk before
120
- * `[DONE]`. Non-streaming replies get the same figures in the `Server-Timing`
121
- * response header instead — headers flush before the first token, so a stream
122
- * cannot use them.
123
+ * Per-request performance for a completion. A stream carries it on the last
124
+ * chunk before `[DONE]`. A non-streaming reply carries the same figures as
125
+ * `pendra.timing` in the response body, and also in the `Server-Timing` header,
126
+ * which a slow request may not get because its headers flush before the result
127
+ * exists.
123
128
  *
124
129
  * Every field is present only when it was measured, and the whole block is
125
130
  * omitted when nothing was (an older worker reports no timings at all).
@@ -142,6 +147,8 @@ interface PendraTiming {
142
147
  model_load_ms?: number;
143
148
  /** `true` when the model had to be loaded for this request. */
144
149
  cold_start?: boolean;
150
+ /** Share of draft tokens the model kept, 0-1. Present only when the request speculated. */
151
+ draft_acceptance_rate?: number;
145
152
  }
146
153
  /**
147
154
  * One web-tool step a model ran while answering — reported only when the
@@ -977,8 +984,21 @@ declare class APIStatusError extends APIError {
977
984
  declare class AuthenticationError extends APIStatusError {
978
985
  constructor(message?: string, options?: APIStatusErrorOptions);
979
986
  }
987
+ /**
988
+ * Raised on 429 Too Many Requests: every worker that can serve the request is
989
+ * busy, on a stream as on any other request.
990
+ *
991
+ * `retryAfter` is the seconds the API asks you to wait before retrying (its
992
+ * `Retry-After` header), `queueDepth` how many requests were already waiting
993
+ * for the model (`X-Pendra-Queue-Depth`); each is `undefined` when not sent.
994
+ */
980
995
  declare class RateLimitError extends APIStatusError {
981
- constructor(message?: string, options?: APIStatusErrorOptions);
996
+ readonly retryAfter: number | undefined;
997
+ readonly queueDepth: number | undefined;
998
+ constructor(message?: string, options?: APIStatusErrorOptions & {
999
+ retryAfter?: number;
1000
+ queueDepth?: number;
1001
+ });
982
1002
  }
983
1003
  /**
984
1004
  * Raised when a response arrives but isn't the shape the SDK expects.
@@ -1025,12 +1045,16 @@ declare class APIStreamError extends APIError {
1025
1045
  readonly requestId: string | undefined;
1026
1046
  readonly workerId: string | undefined;
1027
1047
  readonly workerName: string | undefined;
1048
+ /** Seconds to wait before retrying when the report carried one (a capacity
1049
+ * 429), else `undefined`. Same meaning as on `RateLimitError`. */
1050
+ readonly retryAfter: number | undefined;
1028
1051
  constructor(message: string, options?: {
1029
1052
  type?: string;
1030
1053
  code?: number;
1031
1054
  requestId?: string;
1032
1055
  workerId?: string;
1033
1056
  workerName?: string;
1057
+ retryAfter?: number;
1034
1058
  });
1035
1059
  }
1036
1060
 
package/dist/index.d.ts CHANGED
@@ -19,6 +19,10 @@ interface Usage {
19
19
  * on {@link ChatCompletion.pendra} as a `truncated_during_reasoning` notice.
20
20
  */
21
21
  completion_tokens_details?: CompletionTokensDetails | null;
22
+ /** Breakdown of `prompt_tokens`. */
23
+ prompt_tokens_details?: {
24
+ cached_tokens?: number;
25
+ } | null;
22
26
  }
23
27
  interface ChatCompletionMessageToolCall {
24
28
  /**
@@ -116,10 +120,11 @@ interface PendraNotice {
116
120
  message: string;
117
121
  }
118
122
  /**
119
- * Per-request performance for a streaming completion, on the last chunk before
120
- * `[DONE]`. Non-streaming replies get the same figures in the `Server-Timing`
121
- * response header instead — headers flush before the first token, so a stream
122
- * cannot use them.
123
+ * Per-request performance for a completion. A stream carries it on the last
124
+ * chunk before `[DONE]`. A non-streaming reply carries the same figures as
125
+ * `pendra.timing` in the response body, and also in the `Server-Timing` header,
126
+ * which a slow request may not get because its headers flush before the result
127
+ * exists.
123
128
  *
124
129
  * Every field is present only when it was measured, and the whole block is
125
130
  * omitted when nothing was (an older worker reports no timings at all).
@@ -142,6 +147,8 @@ interface PendraTiming {
142
147
  model_load_ms?: number;
143
148
  /** `true` when the model had to be loaded for this request. */
144
149
  cold_start?: boolean;
150
+ /** Share of draft tokens the model kept, 0-1. Present only when the request speculated. */
151
+ draft_acceptance_rate?: number;
145
152
  }
146
153
  /**
147
154
  * One web-tool step a model ran while answering — reported only when the
@@ -977,8 +984,21 @@ declare class APIStatusError extends APIError {
977
984
  declare class AuthenticationError extends APIStatusError {
978
985
  constructor(message?: string, options?: APIStatusErrorOptions);
979
986
  }
987
+ /**
988
+ * Raised on 429 Too Many Requests: every worker that can serve the request is
989
+ * busy, on a stream as on any other request.
990
+ *
991
+ * `retryAfter` is the seconds the API asks you to wait before retrying (its
992
+ * `Retry-After` header), `queueDepth` how many requests were already waiting
993
+ * for the model (`X-Pendra-Queue-Depth`); each is `undefined` when not sent.
994
+ */
980
995
  declare class RateLimitError extends APIStatusError {
981
- constructor(message?: string, options?: APIStatusErrorOptions);
996
+ readonly retryAfter: number | undefined;
997
+ readonly queueDepth: number | undefined;
998
+ constructor(message?: string, options?: APIStatusErrorOptions & {
999
+ retryAfter?: number;
1000
+ queueDepth?: number;
1001
+ });
982
1002
  }
983
1003
  /**
984
1004
  * Raised when a response arrives but isn't the shape the SDK expects.
@@ -1025,12 +1045,16 @@ declare class APIStreamError extends APIError {
1025
1045
  readonly requestId: string | undefined;
1026
1046
  readonly workerId: string | undefined;
1027
1047
  readonly workerName: string | undefined;
1048
+ /** Seconds to wait before retrying when the report carried one (a capacity
1049
+ * 429), else `undefined`. Same meaning as on `RateLimitError`. */
1050
+ readonly retryAfter: number | undefined;
1028
1051
  constructor(message: string, options?: {
1029
1052
  type?: string;
1030
1053
  code?: number;
1031
1054
  requestId?: string;
1032
1055
  workerId?: string;
1033
1056
  workerName?: string;
1057
+ retryAfter?: number;
1034
1058
  });
1035
1059
  }
1036
1060
 
package/dist/index.js CHANGED
@@ -77,8 +77,18 @@ var RateLimitError = class extends APIStatusError {
77
77
  constructor(message = "Rate limit exceeded", options = {}) {
78
78
  super(message, 429, options);
79
79
  this.name = "RateLimitError";
80
+ this.retryAfter = options.retryAfter;
81
+ this.queueDepth = options.queueDepth;
80
82
  }
81
83
  };
84
+ function secondsFrom(...candidates) {
85
+ for (const value of candidates) {
86
+ if (typeof value !== "number" && (typeof value !== "string" || value.trim() === "")) continue;
87
+ const n = Number(value);
88
+ if (Number.isFinite(n) && n >= 0) return n;
89
+ }
90
+ return void 0;
91
+ }
82
92
  var _APIResponseValidationError = class _APIResponseValidationError extends APIError {
83
93
  constructor(message, options = {}) {
84
94
  const { status, body } = options;
@@ -104,6 +114,7 @@ var APIStreamError = class extends APIError {
104
114
  this.requestId = options.requestId;
105
115
  this.workerId = options.workerId;
106
116
  this.workerName = options.workerName;
117
+ this.retryAfter = options.retryAfter;
107
118
  }
108
119
  };
109
120
  var LOC_SOURCES = /* @__PURE__ */ new Set(["body", "query", "path", "header", "cookie"]);
@@ -165,7 +176,7 @@ async function readErrorBody(response) {
165
176
  return raw;
166
177
  }
167
178
  }
168
- function statusErrorFromBody(status, body, fallback) {
179
+ function statusErrorFromBody(status, body, fallback, headers) {
169
180
  const detail = isRecord(body) ? body.detail : void 0;
170
181
  const err = isRecord(body) ? body.error : void 0;
171
182
  let message = (isRecord(err) ? nonEmptyString(err.message) : void 0) ?? messageFromDetail(detail) ?? (isRecord(body) ? nonEmptyString(body.message) : void 0);
@@ -177,7 +188,13 @@ function statusErrorFromBody(status, body, fallback) {
177
188
  }
178
189
  const options = { body, detail };
179
190
  if (status === 401) return new AuthenticationError(message, options);
180
- if (status === 429) return new RateLimitError(message, options);
191
+ if (status === 429) {
192
+ return new RateLimitError(message, {
193
+ ...options,
194
+ retryAfter: secondsFrom(headers?.get("Retry-After"), isRecord(err) ? err.retry_after : void 0),
195
+ queueDepth: secondsFrom(headers?.get("X-Pendra-Queue-Depth"), isRecord(err) ? err.queue_depth : void 0)
196
+ });
197
+ }
181
198
  return new APIStatusError(message, status, options);
182
199
  }
183
200
 
@@ -579,7 +596,8 @@ function throwIfInBandError(parsed, fallback = "The server reported an error mid
579
596
  code: typeof e.code === "number" ? e.code : void 0,
580
597
  requestId: asString(e.request_id),
581
598
  workerId: asString(e.worker_id),
582
- workerName: asString(e.worker_name)
599
+ workerName: asString(e.worker_name),
600
+ retryAfter: secondsFrom(e.retry_after)
583
601
  }
584
602
  );
585
603
  }
@@ -1169,7 +1187,7 @@ var Transcriptions = class {
1169
1187
  clearTimeout(timeoutId);
1170
1188
  if (response.status >= 400) {
1171
1189
  const body = await readErrorBody(response);
1172
- throw statusErrorFromBody(response.status, body, response.statusText);
1190
+ throw statusErrorFromBody(response.status, body, response.statusText, response.headers);
1173
1191
  }
1174
1192
  const format = params.response_format;
1175
1193
  if (format === "text" || format === "srt" || format === "vtt") {
@@ -1957,7 +1975,7 @@ var Rerank = class {
1957
1975
  };
1958
1976
 
1959
1977
  // src/version.ts
1960
- var VERSION = "0.19.0";
1978
+ var VERSION = "0.21.0";
1961
1979
  var USER_AGENT = `pendra-node/${VERSION}`;
1962
1980
 
1963
1981
  // src/client.ts
@@ -2072,7 +2090,7 @@ var Pendra = class {
2072
2090
  );
2073
2091
  }
2074
2092
  if (!response.ok) {
2075
- throw statusErrorFromBody(response.status, await readErrorBody(response), response.statusText);
2093
+ throw statusErrorFromBody(response.status, await readErrorBody(response), response.statusText, response.headers);
2076
2094
  }
2077
2095
  const json = await response.json();
2078
2096
  await engine.resolveOrgKey(json.keys ?? []);
@@ -2110,7 +2128,7 @@ var Pendra = class {
2110
2128
  clearTimeout(timeoutId);
2111
2129
  if (!response.ok) {
2112
2130
  this.invalidateKeyCacheIfRekeyed(response);
2113
- throw statusErrorFromBody(response.status, await readErrorBody(response), response.statusText);
2131
+ throw statusErrorFromBody(response.status, await readErrorBody(response), response.statusText, response.headers);
2114
2132
  }
2115
2133
  return { response, controller };
2116
2134
  }