pendra 0.1.0 → 0.2.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
@@ -37,7 +37,7 @@ const stream = await client.chat.completions.create({
37
37
  });
38
38
 
39
39
  for await (const chunk of stream) {
40
- process.stdout.write(chunk.choices[0].delta.content ?? '');
40
+ process.stdout.write(chunk.choices[0]?.delta?.content ?? '');
41
41
  }
42
42
  ```
43
43
 
@@ -48,6 +48,27 @@ const models = await client.models.list();
48
48
  models.forEach((m) => console.log(m.id));
49
49
  ```
50
50
 
51
+ ## Image Generation
52
+
53
+ Generate images from a text prompt. Returns base64-encoded PNGs by default.
54
+
55
+ ```typescript
56
+ import { writeFileSync } from 'node:fs';
57
+
58
+ const response = await client.images.generations.create({
59
+ model: 'x/z-image-turbo',
60
+ prompt: 'A red London double-decker bus at sunset',
61
+ size: '1024x1024',
62
+ });
63
+
64
+ const b64 = response.data[0].b64_json;
65
+ if (b64) {
66
+ writeFileSync('bus.png', Buffer.from(b64, 'base64'));
67
+ }
68
+ ```
69
+
70
+ Image generation is non-streaming — the response is returned as a single JSON payload once the worker finishes.
71
+
51
72
  ## Configuration
52
73
 
53
74
  | Option | Env var | Default |
package/dist/index.d.mts CHANGED
@@ -77,6 +77,36 @@ declare class Chat {
77
77
  constructor(client: Pendra);
78
78
  }
79
79
 
80
+ interface ImageGenerationCreateParams {
81
+ model: string;
82
+ prompt: string;
83
+ n?: number;
84
+ size?: string;
85
+ response_format?: string;
86
+ num_inference_steps?: number;
87
+ seed?: number;
88
+ negative_prompt?: string;
89
+ }
90
+ interface ImageData {
91
+ b64_json?: string;
92
+ url?: string;
93
+ }
94
+ interface ImageResponse {
95
+ created: number;
96
+ data: ImageData[];
97
+ }
98
+
99
+ declare class Generations {
100
+ private client;
101
+ constructor(client: Pendra);
102
+ create(params: ImageGenerationCreateParams): Promise<ImageResponse>;
103
+ }
104
+
105
+ declare class Images {
106
+ generations: Generations;
107
+ constructor(client: Pendra);
108
+ }
109
+
80
110
  interface Model {
81
111
  id: string;
82
112
  object: string;
@@ -100,6 +130,7 @@ declare class Pendra {
100
130
  readonly baseURL: string;
101
131
  readonly timeout: number;
102
132
  chat: Chat;
133
+ images: Images;
103
134
  models: Models;
104
135
  constructor(options?: PendraOptions);
105
136
  /** @internal */
@@ -131,4 +162,4 @@ declare class RateLimitError extends APIStatusError {
131
162
  constructor(message?: string);
132
163
  }
133
164
 
134
- export { APIConnectionError, APIError, APIStatusError, AuthenticationError, type ChatCompletion, type ChatCompletionChunk, type ChatCompletionCreateParams, type ChatCompletionMessage, type Choice, type ChoiceDelta, type Model, Pendra, type PendraOptions, RateLimitError, Stream, type StreamChoice, type Usage, Pendra as default };
165
+ export { APIConnectionError, APIError, APIStatusError, AuthenticationError, type ChatCompletion, type ChatCompletionChunk, type ChatCompletionCreateParams, type ChatCompletionMessage, type Choice, type ChoiceDelta, type ImageData, type ImageGenerationCreateParams, type ImageResponse, type Model, Pendra, type PendraOptions, RateLimitError, Stream, type StreamChoice, type Usage, Pendra as default };
package/dist/index.d.ts CHANGED
@@ -77,6 +77,36 @@ declare class Chat {
77
77
  constructor(client: Pendra);
78
78
  }
79
79
 
80
+ interface ImageGenerationCreateParams {
81
+ model: string;
82
+ prompt: string;
83
+ n?: number;
84
+ size?: string;
85
+ response_format?: string;
86
+ num_inference_steps?: number;
87
+ seed?: number;
88
+ negative_prompt?: string;
89
+ }
90
+ interface ImageData {
91
+ b64_json?: string;
92
+ url?: string;
93
+ }
94
+ interface ImageResponse {
95
+ created: number;
96
+ data: ImageData[];
97
+ }
98
+
99
+ declare class Generations {
100
+ private client;
101
+ constructor(client: Pendra);
102
+ create(params: ImageGenerationCreateParams): Promise<ImageResponse>;
103
+ }
104
+
105
+ declare class Images {
106
+ generations: Generations;
107
+ constructor(client: Pendra);
108
+ }
109
+
80
110
  interface Model {
81
111
  id: string;
82
112
  object: string;
@@ -100,6 +130,7 @@ declare class Pendra {
100
130
  readonly baseURL: string;
101
131
  readonly timeout: number;
102
132
  chat: Chat;
133
+ images: Images;
103
134
  models: Models;
104
135
  constructor(options?: PendraOptions);
105
136
  /** @internal */
@@ -131,4 +162,4 @@ declare class RateLimitError extends APIStatusError {
131
162
  constructor(message?: string);
132
163
  }
133
164
 
134
- export { APIConnectionError, APIError, APIStatusError, AuthenticationError, type ChatCompletion, type ChatCompletionChunk, type ChatCompletionCreateParams, type ChatCompletionMessage, type Choice, type ChoiceDelta, type Model, Pendra, type PendraOptions, RateLimitError, Stream, type StreamChoice, type Usage, Pendra as default };
165
+ export { APIConnectionError, APIError, APIStatusError, AuthenticationError, type ChatCompletion, type ChatCompletionChunk, type ChatCompletionCreateParams, type ChatCompletionMessage, type Choice, type ChoiceDelta, type ImageData, type ImageGenerationCreateParams, type ImageResponse, type Model, Pendra, type PendraOptions, RateLimitError, Stream, type StreamChoice, type Usage, Pendra as default };
package/dist/index.js CHANGED
@@ -116,6 +116,50 @@ var Chat = class {
116
116
  }
117
117
  };
118
118
 
119
+ // src/resources/images/generations.ts
120
+ var SIZE_RE = /^\d+x\d+$/;
121
+ function validateParams(params) {
122
+ if (!params.prompt || !params.prompt.trim()) {
123
+ throw new Error("prompt must be a non-empty string");
124
+ }
125
+ if (params.n != null && (params.n < 1 || params.n > 4)) {
126
+ throw new Error("n must be between 1 and 4");
127
+ }
128
+ if (params.size != null && !SIZE_RE.test(params.size)) {
129
+ throw new Error("size must be in the form WIDTHxHEIGHT (e.g. '1024x1024')");
130
+ }
131
+ }
132
+ var Generations = class {
133
+ constructor(client) {
134
+ this.client = client;
135
+ }
136
+ async create(params) {
137
+ validateParams(params);
138
+ const body = {
139
+ model: params.model,
140
+ prompt: params.prompt
141
+ };
142
+ if (params.n != null) body.n = params.n;
143
+ if (params.size != null) body.size = params.size;
144
+ if (params.response_format != null) body.response_format = params.response_format;
145
+ if (params.num_inference_steps != null) body.num_inference_steps = params.num_inference_steps;
146
+ if (params.seed != null) body.seed = params.seed;
147
+ if (params.negative_prompt != null) body.negative_prompt = params.negative_prompt;
148
+ const { response } = await this.client._fetch(
149
+ "/api/v1/images/generations",
150
+ { method: "POST", body }
151
+ );
152
+ return await response.json();
153
+ }
154
+ };
155
+
156
+ // src/resources/images/index.ts
157
+ var Images = class {
158
+ constructor(client) {
159
+ this.generations = new Generations(client);
160
+ }
161
+ };
162
+
119
163
  // src/resources/models.ts
120
164
  var Models = class {
121
165
  constructor(client) {
@@ -180,6 +224,7 @@ var Pendra = class {
180
224
  );
181
225
  this.timeout = options.timeout ?? 12e4;
182
226
  this.chat = new Chat(this);
227
+ this.images = new Images(this);
183
228
  this.models = new Models(this);
184
229
  }
185
230
  /** @internal */
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/streaming.ts","../src/resources/chat/completions.ts","../src/resources/chat/index.ts","../src/resources/models.ts","../src/errors.ts","../src/client.ts"],"sourcesContent":["export { Pendra } from \"./client\";\nexport type { PendraOptions } from \"./client\";\nexport { Stream } from \"./streaming\";\n\nexport type {\n ChatCompletion,\n ChatCompletionChunk,\n ChatCompletionMessage,\n ChatCompletionCreateParams,\n Choice,\n StreamChoice,\n ChoiceDelta,\n Usage,\n} from \"./types/chat\";\n\nexport type { Model } from \"./types/models\";\n\nexport {\n APIError,\n APIConnectionError,\n APIStatusError,\n AuthenticationError,\n RateLimitError,\n} from \"./errors\";\n\n// Default export for convenient usage:\n// import Pendra from 'pendra';\nimport { Pendra as _Pendra } from \"./client\";\nexport default _Pendra;\n","import type { ChatCompletionChunk } from \"./types/chat\";\n\nexport class Stream implements AsyncIterable<ChatCompletionChunk> {\n private reader: ReadableStreamDefaultReader<Uint8Array>;\n private decoder = new TextDecoder();\n private buffer = \"\";\n private done = false;\n private controller: AbortController;\n\n constructor(response: Response, controller: AbortController) {\n if (!response.body) {\n throw new Error(\"Response body is null — streaming not supported\");\n }\n this.reader = response.body.getReader();\n this.controller = controller;\n }\n\n async *[Symbol.asyncIterator](): AsyncIterator<ChatCompletionChunk> {\n try {\n while (!this.done) {\n const { value, done: readerDone } = await this.reader.read();\n if (readerDone) {\n this.done = true;\n break;\n }\n\n this.buffer += this.decoder.decode(value, { stream: true });\n const lines = this.buffer.split(\"\\n\");\n this.buffer = lines.pop() ?? \"\";\n\n for (const line of lines) {\n const trimmed = line.trim();\n if (!trimmed || trimmed.startsWith(\":\")) continue;\n\n if (!trimmed.startsWith(\"data: \")) continue;\n const data = trimmed.slice(6);\n\n if (data === \"[DONE]\") {\n this.done = true;\n return;\n }\n\n try {\n yield JSON.parse(data) as ChatCompletionChunk;\n } catch {\n // skip malformed JSON\n }\n }\n }\n } finally {\n this.reader.releaseLock();\n }\n }\n\n abort(): void {\n this.done = true;\n this.controller.abort();\n }\n}\n","import type { Pendra } from \"../../client\";\nimport type {\n ChatCompletion,\n ChatCompletionChunk,\n ChatCompletionCreateParams,\n} from \"../../types/chat\";\nimport { Stream } from \"../../streaming\";\n\nexport class Completions {\n private client: Pendra;\n\n constructor(client: Pendra) {\n this.client = client;\n }\n\n create(\n params: ChatCompletionCreateParams & { stream: true },\n ): Promise<Stream>;\n create(\n params: ChatCompletionCreateParams & { stream?: false },\n ): Promise<ChatCompletion>;\n create(\n params: ChatCompletionCreateParams,\n ): Promise<ChatCompletion | Stream>;\n async create(\n params: ChatCompletionCreateParams,\n ): Promise<ChatCompletion | Stream> {\n const body: Record<string, unknown> = {\n model: params.model,\n messages: params.messages,\n };\n\n if (params.stream != null) body.stream = params.stream;\n if (params.temperature != null) body.temperature = params.temperature;\n if (params.max_tokens != null) body.max_tokens = params.max_tokens;\n if (params.top_p != null) body.top_p = params.top_p;\n if (params.stop != null) body.stop = params.stop;\n\n if (params.stream) {\n const { response, controller } = await this.client._fetch(\n \"/api/v1/chat/completions\",\n { method: \"POST\", body, stream: true },\n );\n return new Stream(response, controller);\n }\n\n const { response } = await this.client._fetch(\n \"/api/v1/chat/completions\",\n { method: \"POST\", body },\n );\n return (await response.json()) as ChatCompletion;\n }\n}\n","import type { Pendra } from \"../../client\";\nimport { Completions } from \"./completions\";\n\nexport class Chat {\n completions: Completions;\n\n constructor(client: Pendra) {\n this.completions = new Completions(client);\n }\n}\n","import type { Pendra } from \"../client\";\nimport type { Model } from \"../types/models\";\n\nexport class Models {\n private client: Pendra;\n\n constructor(client: Pendra) {\n this.client = client;\n }\n\n async list(): Promise<Model[]> {\n const { response } = await this.client._fetch(\"/api/v1/models\", {\n method: \"GET\",\n });\n const data = (await response.json()) as { data: Model[] };\n return data.data;\n }\n}\n","export class APIError extends Error {\n readonly status: number | undefined;\n\n constructor(message: string, status?: number) {\n super(message);\n this.name = \"APIError\";\n this.status = status;\n }\n}\n\nexport class APIConnectionError extends APIError {\n constructor(message = \"Failed to connect to the Pendra API\") {\n super(message);\n this.name = \"APIConnectionError\";\n }\n}\n\nexport class APIStatusError extends APIError {\n override readonly status: number;\n\n constructor(message: string, status: number) {\n super(message, status);\n this.name = \"APIStatusError\";\n this.status = status;\n }\n}\n\nexport class AuthenticationError extends APIStatusError {\n constructor(\n message = \"Invalid API key — check your PENDRA_API_KEY\",\n ) {\n super(message, 401);\n this.name = \"AuthenticationError\";\n }\n}\n\nexport class RateLimitError extends APIStatusError {\n constructor(message = \"Rate limit exceeded\") {\n super(message, 429);\n this.name = \"RateLimitError\";\n }\n}\n","import { Chat } from \"./resources/chat/index\";\nimport { Models } from \"./resources/models\";\nimport {\n APIConnectionError,\n APIStatusError,\n AuthenticationError,\n RateLimitError,\n} from \"./errors\";\n\nexport interface PendraOptions {\n apiKey?: string;\n baseURL?: string;\n timeout?: number;\n}\n\nexport class Pendra {\n readonly apiKey: string;\n readonly baseURL: string;\n readonly timeout: number;\n\n chat: Chat;\n models: Models;\n\n constructor(options: PendraOptions = {}) {\n const apiKey =\n options.apiKey ??\n (typeof process !== \"undefined\" ? process.env?.PENDRA_API_KEY : undefined);\n\n if (!apiKey) {\n throw new Error(\n \"API key is required — pass apiKey or set the PENDRA_API_KEY environment variable\",\n );\n }\n\n this.apiKey = apiKey;\n this.baseURL = (options.baseURL ?? \"https://api.pendra.ai\").replace(\n /\\/$/,\n \"\",\n );\n this.timeout = options.timeout ?? 120_000;\n\n this.chat = new Chat(this);\n this.models = new Models(this);\n }\n\n /** @internal */\n async _fetch(\n path: string,\n options: {\n method: string;\n body?: Record<string, unknown>;\n stream?: boolean;\n },\n ): Promise<{ response: Response; controller: AbortController }> {\n const controller = new AbortController();\n const timeoutId = setTimeout(() => controller.abort(), this.timeout);\n\n const headers: Record<string, string> = {\n Authorization: `Bearer ${this.apiKey}`,\n \"Content-Type\": \"application/json\",\n };\n\n let response: Response;\n try {\n response = await fetch(`${this.baseURL}${path}`, {\n method: options.method,\n headers,\n body: options.body ? JSON.stringify(options.body) : undefined,\n signal: controller.signal,\n });\n } catch (err) {\n clearTimeout(timeoutId);\n if (err instanceof DOMException && err.name === \"AbortError\") {\n throw new APIConnectionError(\n `Request timed out after ${this.timeout}ms`,\n );\n }\n throw new APIConnectionError(\n err instanceof Error ? err.message : \"Failed to connect to the Pendra API\",\n );\n }\n\n if (!options.stream) {\n clearTimeout(timeoutId);\n }\n\n if (!response.ok) {\n clearTimeout(timeoutId);\n let message: string;\n try {\n const body = (await response.json()) as { detail?: string };\n message = body.detail ?? response.statusText;\n } catch {\n message = response.statusText;\n }\n\n if (response.status === 401) throw new AuthenticationError(message);\n if (response.status === 429) throw new RateLimitError(message);\n throw new APIStatusError(message, response.status);\n }\n\n return { response, controller };\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACEO,IAAM,SAAN,MAA2D;AAAA,EAOhE,YAAY,UAAoB,YAA6B;AAL7D,SAAQ,UAAU,IAAI,YAAY;AAClC,SAAQ,SAAS;AACjB,SAAQ,OAAO;AAIb,QAAI,CAAC,SAAS,MAAM;AAClB,YAAM,IAAI,MAAM,sDAAiD;AAAA,IACnE;AACA,S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AC5D,cAAM,IAAI;AAAA,UACR,2BAA2B,KAAK,OAAO;AAAA,QACzC;AAAA,MACF;AACA,YAAM,IAAI;AAAA,QACR,eAAe,QAAQ,IAAI,UAAU;AAAA,MACvC;AAAA,IACF;AAEA,QAAI,CAAC,QAAQ,QAAQ;AACnB,mBAAa,SAAS;AAAA,IACxB;AAEA,QAAI,CAAC,SAAS,IAAI;AAChB,mBAAa,SAAS;AACtB,UAAI;AACJ,UAAI;AACF,cAAM,OAAQ,MAAM,SAAS,KAAK;AAClC,kBAAU,KAAK,UAAU,SAAS;AAAA,MACpC,QAAQ;AACN,kBAAU,SAAS;AAAA,MACrB;AAEA,UAAI,SAAS,WAAW,IAAK,OAAM,IAAI,oBAAoB,OAAO;AAClE,UAAI,SAAS,WAAW,IAAK,OAAM,IAAI,eAAe,OAAO;AAC7D,YAAM,IAAI,eAAe,SAAS,SAAS,MAAM;AAAA,IACnD;AAEA,WAAO,EAAE,UAAU,WAAW;AAAA,EAChC;AACF;;;AN3EA,IAAO,gBAAQ;","names":["response"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/streaming.ts","../src/resources/chat/completions.ts","../src/resources/chat/index.ts","../src/resources/images/generations.ts","../src/resources/images/index.ts","../src/resources/models.ts","../src/errors.ts","../src/client.ts"],"sourcesContent":["export { Pendra } from \"./client\";\nexport type { PendraOptions } from \"./client\";\nexport { Stream } from \"./streaming\";\n\nexport type {\n ChatCompletion,\n ChatCompletionChunk,\n ChatCompletionMessage,\n ChatCompletionCreateParams,\n Choice,\n StreamChoice,\n ChoiceDelta,\n Usage,\n} from \"./types/chat\";\n\nexport type { Model } from \"./types/models\";\n\nexport type {\n ImageGenerationCreateParams,\n ImageData,\n ImageResponse,\n} from \"./types/images\";\n\nexport {\n APIError,\n APIConnectionError,\n APIStatusError,\n AuthenticationError,\n RateLimitError,\n} from \"./errors\";\n\n// Default export for convenient usage:\n// import Pendra from 'pendra';\nimport { Pendra as _Pendra } from \"./client\";\nexport default _Pendra;\n","import type { ChatCompletionChunk } from \"./types/chat\";\n\nexport class Stream implements AsyncIterable<ChatCompletionChunk> {\n private reader: ReadableStreamDefaultReader<Uint8Array>;\n private decoder = new TextDecoder();\n private buffer = \"\";\n private done = false;\n private controller: AbortController;\n\n constructor(response: Response, controller: AbortController) {\n if (!response.body) {\n throw new Error(\"Response body is null — streaming not supported\");\n }\n this.reader = response.body.getReader();\n this.controller = controller;\n }\n\n async *[Symbol.asyncIterator](): AsyncIterator<ChatCompletionChunk> {\n try {\n while (!this.done) {\n const { value, done: readerDone } = await this.reader.read();\n if (readerDone) {\n this.done = true;\n break;\n }\n\n this.buffer += this.decoder.decode(value, { stream: true });\n const lines = this.buffer.split(\"\\n\");\n this.buffer = lines.pop() ?? \"\";\n\n for (const line of lines) {\n const trimmed = line.trim();\n if (!trimmed || trimmed.startsWith(\":\")) continue;\n\n if (!trimmed.startsWith(\"data: \")) continue;\n const data = trimmed.slice(6);\n\n if (data === \"[DONE]\") {\n this.done = true;\n return;\n }\n\n try {\n yield JSON.parse(data) as ChatCompletionChunk;\n } catch {\n // skip malformed JSON\n }\n }\n }\n } finally {\n this.reader.releaseLock();\n }\n }\n\n abort(): void {\n this.done = true;\n this.controller.abort();\n }\n}\n","import type { Pendra } from \"../../client\";\nimport type {\n ChatCompletion,\n ChatCompletionChunk,\n ChatCompletionCreateParams,\n} from \"../../types/chat\";\nimport { Stream } from \"../../streaming\";\n\nexport class Completions {\n private client: Pendra;\n\n constructor(client: Pendra) {\n this.client = client;\n }\n\n create(\n params: ChatCompletionCreateParams & { stream: true },\n ): Promise<Stream>;\n create(\n params: ChatCompletionCreateParams & { stream?: false },\n ): Promise<ChatCompletion>;\n create(\n params: ChatCompletionCreateParams,\n ): Promise<ChatCompletion | Stream>;\n async create(\n params: ChatCompletionCreateParams,\n ): Promise<ChatCompletion | Stream> {\n const body: Record<string, unknown> = {\n model: params.model,\n messages: params.messages,\n };\n\n if (params.stream != null) body.stream = params.stream;\n if (params.temperature != null) body.temperature = params.temperature;\n if (params.max_tokens != null) body.max_tokens = params.max_tokens;\n if (params.top_p != null) body.top_p = params.top_p;\n if (params.stop != null) body.stop = params.stop;\n\n if (params.stream) {\n const { response, controller } = await this.client._fetch(\n \"/api/v1/chat/completions\",\n { method: \"POST\", body, stream: true },\n );\n return new Stream(response, controller);\n }\n\n const { response } = await this.client._fetch(\n \"/api/v1/chat/completions\",\n { method: \"POST\", body },\n );\n return (await response.json()) as ChatCompletion;\n }\n}\n","import type { Pendra } from \"../../client\";\nimport { Completions } from \"./completions\";\n\nexport class Chat {\n completions: Completions;\n\n constructor(client: Pendra) {\n this.completions = new Completions(client);\n }\n}\n","import type { Pendra } from \"../../client\";\nimport type { ImageGenerationCreateParams, ImageResponse } from \"../../types/images\";\n\nconst SIZE_RE = /^\\d+x\\d+$/;\n\nfunction validateParams(params: ImageGenerationCreateParams): void {\n if (!params.prompt || !params.prompt.trim()) {\n throw new Error(\"prompt must be a non-empty string\");\n }\n if (params.n != null && (params.n < 1 || params.n > 4)) {\n throw new Error(\"n must be between 1 and 4\");\n }\n if (params.size != null && !SIZE_RE.test(params.size)) {\n throw new Error(\"size must be in the form WIDTHxHEIGHT (e.g. '1024x1024')\");\n }\n}\n\nexport class Generations {\n private client: Pendra;\n\n constructor(client: Pendra) {\n this.client = client;\n }\n\n async create(params: ImageGenerationCreateParams): Promise<ImageResponse> {\n validateParams(params);\n\n const body: Record<string, unknown> = {\n model: params.model,\n prompt: params.prompt,\n };\n\n if (params.n != null) body.n = params.n;\n if (params.size != null) body.size = params.size;\n if (params.response_format != null) body.response_format = params.response_format;\n if (params.num_inference_steps != null) body.num_inference_steps = params.num_inference_steps;\n if (params.seed != null) body.seed = params.seed;\n if (params.negative_prompt != null) body.negative_prompt = params.negative_prompt;\n\n const { response } = await this.client._fetch(\n \"/api/v1/images/generations\",\n { method: \"POST\", body },\n );\n return (await response.json()) as ImageResponse;\n }\n}\n","import type { Pendra } from \"../../client\";\nimport { Generations } from \"./generations\";\n\nexport class Images {\n generations: Generations;\n\n constructor(client: Pendra) {\n this.generations = new Generations(client);\n }\n}\n","import type { Pendra } from \"../client\";\nimport type { Model } from \"../types/models\";\n\nexport class Models {\n private client: Pendra;\n\n constructor(client: Pendra) {\n this.client = client;\n }\n\n async list(): Promise<Model[]> {\n const { response } = await this.client._fetch(\"/api/v1/models\", {\n method: \"GET\",\n });\n const data = (await response.json()) as { data: Model[] };\n return data.data;\n }\n}\n","export class APIError extends Error {\n readonly status: number | undefined;\n\n constructor(message: string, status?: number) {\n super(message);\n this.name = \"APIError\";\n this.status = status;\n }\n}\n\nexport class APIConnectionError extends APIError {\n constructor(message = \"Failed to connect to the Pendra API\") {\n super(message);\n this.name = \"APIConnectionError\";\n }\n}\n\nexport class APIStatusError extends APIError {\n override readonly status: number;\n\n constructor(message: string, status: number) {\n super(message, status);\n this.name = \"APIStatusError\";\n this.status = status;\n }\n}\n\nexport class AuthenticationError extends APIStatusError {\n constructor(\n message = \"Invalid API key — check your PENDRA_API_KEY\",\n ) {\n super(message, 401);\n this.name = \"AuthenticationError\";\n }\n}\n\nexport class RateLimitError extends APIStatusError {\n constructor(message = \"Rate limit exceeded\") {\n super(message, 429);\n this.name = \"RateLimitError\";\n }\n}\n","import { Chat } from \"./resources/chat/index\";\nimport { Images } from \"./resources/images/index\";\nimport { Models } from \"./resources/models\";\nimport {\n APIConnectionError,\n APIStatusError,\n AuthenticationError,\n RateLimitError,\n} from \"./errors\";\n\nexport interface PendraOptions {\n apiKey?: string;\n baseURL?: string;\n timeout?: number;\n}\n\nexport class Pendra {\n readonly apiKey: string;\n readonly baseURL: string;\n readonly timeout: number;\n\n chat: Chat;\n images: Images;\n models: Models;\n\n constructor(options: PendraOptions = {}) {\n const apiKey =\n options.apiKey ??\n (typeof process !== \"undefined\" ? process.env?.PENDRA_API_KEY : undefined);\n\n if (!apiKey) {\n throw new Error(\n \"API key is required — pass apiKey or set the PENDRA_API_KEY environment variable\",\n );\n }\n\n this.apiKey = apiKey;\n this.baseURL = (options.baseURL ?? \"https://api.pendra.ai\").replace(\n /\\/$/,\n \"\",\n );\n this.timeout = options.timeout ?? 120_000;\n\n this.chat = new Chat(this);\n this.images = new Images(this);\n this.models = new Models(this);\n }\n\n /** @internal */\n async _fetch(\n path: string,\n options: {\n method: string;\n body?: Record<string, unknown>;\n stream?: boolean;\n },\n ): Promise<{ response: Response; controller: AbortController }> {\n const controller = new AbortController();\n const timeoutId = setTimeout(() => controller.abort(), this.timeout);\n\n const headers: Record<string, string> = {\n Authorization: `Bearer ${this.apiKey}`,\n \"Content-Type\": \"application/json\",\n };\n\n let response: Response;\n try {\n response = await fetch(`${this.baseURL}${path}`, {\n method: options.method,\n headers,\n body: options.body ? JSON.stringify(options.body) : undefined,\n signal: controller.signal,\n });\n } catch (err) {\n clearTimeout(timeoutId);\n if (err instanceof DOMException && err.name === \"AbortError\") {\n throw new APIConnectionError(\n `Request timed out after ${this.timeout}ms`,\n );\n }\n throw new APIConnectionError(\n err instanceof Error ? err.message : \"Failed to connect to the Pendra API\",\n );\n }\n\n if (!options.stream) {\n clearTimeout(timeoutId);\n }\n\n if (!response.ok) {\n clearTimeout(timeoutId);\n let message: string;\n try {\n const body = (await response.json()) as { detail?: string };\n message = body.detail ?? response.statusText;\n } catch {\n message = response.statusText;\n }\n\n if (response.status === 401) throw new AuthenticationError(message);\n if (response.status === 429) throw new RateLimitError(message);\n throw new APIStatusError(message, response.status);\n }\n\n return { response, controller };\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACEO,IAAM,SAAN,MAA2D;AAAA,EAOhE,YAAY,UAAoB,YAA6B;AAL7D,SAAQ,UAAU,IAAI,YAAY;AAClC,SAAQ,SAAS;AACjB,SAAQ,OAAO;AAIb,QAAI,CAAC,SAAS,MAAM;AAClB,YAAM,IAAI,MAAM,sDAAiD;AAAA,IACnE;AACA,SAAK,SAAS,S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,KAAK,OAAO;AAAA,QACzC;AAAA,MACF;AACA,YAAM,IAAI;AAAA,QACR,eAAe,QAAQ,IAAI,UAAU;AAAA,MACvC;AAAA,IACF;AAEA,QAAI,CAAC,QAAQ,QAAQ;AACnB,mBAAa,SAAS;AAAA,IACxB;AAEA,QAAI,CAAC,SAAS,IAAI;AAChB,mBAAa,SAAS;AACtB,UAAI;AACJ,UAAI;AACF,cAAM,OAAQ,MAAM,SAAS,KAAK;AAClC,kBAAU,KAAK,UAAU,SAAS;AAAA,MACpC,QAAQ;AACN,kBAAU,SAAS;AAAA,MACrB;AAEA,UAAI,SAAS,WAAW,IAAK,OAAM,IAAI,oBAAoB,OAAO;AAClE,UAAI,SAAS,WAAW,IAAK,OAAM,IAAI,eAAe,OAAO;AAC7D,YAAM,IAAI,eAAe,SAAS,SAAS,MAAM;AAAA,IACnD;AAEA,WAAO,EAAE,UAAU,WAAW;AAAA,EAChC;AACF;;;ARxEA,IAAO,gBAAQ;","names":["response"]}
package/dist/index.mjs CHANGED
@@ -83,6 +83,50 @@ var Chat = class {
83
83
  }
84
84
  };
85
85
 
86
+ // src/resources/images/generations.ts
87
+ var SIZE_RE = /^\d+x\d+$/;
88
+ function validateParams(params) {
89
+ if (!params.prompt || !params.prompt.trim()) {
90
+ throw new Error("prompt must be a non-empty string");
91
+ }
92
+ if (params.n != null && (params.n < 1 || params.n > 4)) {
93
+ throw new Error("n must be between 1 and 4");
94
+ }
95
+ if (params.size != null && !SIZE_RE.test(params.size)) {
96
+ throw new Error("size must be in the form WIDTHxHEIGHT (e.g. '1024x1024')");
97
+ }
98
+ }
99
+ var Generations = class {
100
+ constructor(client) {
101
+ this.client = client;
102
+ }
103
+ async create(params) {
104
+ validateParams(params);
105
+ const body = {
106
+ model: params.model,
107
+ prompt: params.prompt
108
+ };
109
+ if (params.n != null) body.n = params.n;
110
+ if (params.size != null) body.size = params.size;
111
+ if (params.response_format != null) body.response_format = params.response_format;
112
+ if (params.num_inference_steps != null) body.num_inference_steps = params.num_inference_steps;
113
+ if (params.seed != null) body.seed = params.seed;
114
+ if (params.negative_prompt != null) body.negative_prompt = params.negative_prompt;
115
+ const { response } = await this.client._fetch(
116
+ "/api/v1/images/generations",
117
+ { method: "POST", body }
118
+ );
119
+ return await response.json();
120
+ }
121
+ };
122
+
123
+ // src/resources/images/index.ts
124
+ var Images = class {
125
+ constructor(client) {
126
+ this.generations = new Generations(client);
127
+ }
128
+ };
129
+
86
130
  // src/resources/models.ts
87
131
  var Models = class {
88
132
  constructor(client) {
@@ -147,6 +191,7 @@ var Pendra = class {
147
191
  );
148
192
  this.timeout = options.timeout ?? 12e4;
149
193
  this.chat = new Chat(this);
194
+ this.images = new Images(this);
150
195
  this.models = new Models(this);
151
196
  }
152
197
  /** @internal */
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/streaming.ts","../src/resources/chat/completions.ts","../src/resources/chat/index.ts","../src/resources/models.ts","../src/errors.ts","../src/client.ts","../src/index.ts"],"sourcesContent":["import type { ChatCompletionChunk } from \"./types/chat\";\n\nexport class Stream implements AsyncIterable<ChatCompletionChunk> {\n private reader: ReadableStreamDefaultReader<Uint8Array>;\n private decoder = new TextDecoder();\n private buffer = \"\";\n private done = false;\n private controller: AbortController;\n\n constructor(response: Response, controller: AbortController) {\n if (!response.body) {\n throw new Error(\"Response body is null — streaming not supported\");\n }\n this.reader = response.body.getReader();\n this.controller = controller;\n }\n\n async *[Symbol.asyncIterator](): AsyncIterator<ChatCompletionChunk> {\n try {\n while (!this.done) {\n const { value, done: readerDone } = await this.reader.read();\n if (readerDone) {\n this.done = true;\n break;\n }\n\n this.buffer += this.decoder.decode(value, { stream: true });\n const lines = this.buffer.split(\"\\n\");\n this.buffer = lines.pop() ?? \"\";\n\n for (const line of lines) {\n const trimmed = line.trim();\n if (!trimmed || trimmed.startsWith(\":\")) continue;\n\n if (!trimmed.startsWith(\"data: \")) continue;\n const data = trimmed.slice(6);\n\n if (data === \"[DONE]\") {\n this.done = true;\n return;\n }\n\n try {\n yield JSON.parse(data) as ChatCompletionChunk;\n } catch {\n // skip malformed JSON\n }\n }\n }\n } finally {\n this.reader.releaseLock();\n }\n }\n\n abort(): void {\n this.done = true;\n this.controller.abort();\n }\n}\n","import type { Pendra } from \"../../client\";\nimport type {\n ChatCompletion,\n ChatCompletionChunk,\n ChatCompletionCreateParams,\n} from \"../../types/chat\";\nimport { Stream } from \"../../streaming\";\n\nexport class Completions {\n private client: Pendra;\n\n constructor(client: Pendra) {\n this.client = client;\n }\n\n create(\n params: ChatCompletionCreateParams & { stream: true },\n ): Promise<Stream>;\n create(\n params: ChatCompletionCreateParams & { stream?: false },\n ): Promise<ChatCompletion>;\n create(\n params: ChatCompletionCreateParams,\n ): Promise<ChatCompletion | Stream>;\n async create(\n params: ChatCompletionCreateParams,\n ): Promise<ChatCompletion | Stream> {\n const body: Record<string, unknown> = {\n model: params.model,\n messages: params.messages,\n };\n\n if (params.stream != null) body.stream = params.stream;\n if (params.temperature != null) body.temperature = params.temperature;\n if (params.max_tokens != null) body.max_tokens = params.max_tokens;\n if (params.top_p != null) body.top_p = params.top_p;\n if (params.stop != null) body.stop = params.stop;\n\n if (params.stream) {\n const { response, controller } = await this.client._fetch(\n \"/api/v1/chat/completions\",\n { method: \"POST\", body, stream: true },\n );\n return new Stream(response, controller);\n }\n\n const { response } = await this.client._fetch(\n \"/api/v1/chat/completions\",\n { method: \"POST\", body },\n );\n return (await response.json()) as ChatCompletion;\n }\n}\n","import type { Pendra } from \"../../client\";\nimport { Completions } from \"./completions\";\n\nexport class Chat {\n completions: Completions;\n\n constructor(client: Pendra) {\n this.completions = new Completions(client);\n }\n}\n","import type { Pendra } from \"../client\";\nimport type { Model } from \"../types/models\";\n\nexport class Models {\n private client: Pendra;\n\n constructor(client: Pendra) {\n this.client = client;\n }\n\n async list(): Promise<Model[]> {\n const { response } = await this.client._fetch(\"/api/v1/models\", {\n method: \"GET\",\n });\n const data = (await response.json()) as { data: Model[] };\n return data.data;\n }\n}\n","export class APIError extends Error {\n readonly status: number | undefined;\n\n constructor(message: string, status?: number) {\n super(message);\n this.name = \"APIError\";\n this.status = status;\n }\n}\n\nexport class APIConnectionError extends APIError {\n constructor(message = \"Failed to connect to the Pendra API\") {\n super(message);\n this.name = \"APIConnectionError\";\n }\n}\n\nexport class APIStatusError extends APIError {\n override readonly status: number;\n\n constructor(message: string, status: number) {\n super(message, status);\n this.name = \"APIStatusError\";\n this.status = status;\n }\n}\n\nexport class AuthenticationError extends APIStatusError {\n constructor(\n message = \"Invalid API key — check your PENDRA_API_KEY\",\n ) {\n super(message, 401);\n this.name = \"AuthenticationError\";\n }\n}\n\nexport class RateLimitError extends APIStatusError {\n constructor(message = \"Rate limit exceeded\") {\n super(message, 429);\n this.name = \"RateLimitError\";\n }\n}\n","import { Chat } from \"./resources/chat/index\";\nimport { Models } from \"./resources/models\";\nimport {\n APIConnectionError,\n APIStatusError,\n AuthenticationError,\n RateLimitError,\n} from \"./errors\";\n\nexport interface PendraOptions {\n apiKey?: string;\n baseURL?: string;\n timeout?: number;\n}\n\nexport class Pendra {\n readonly apiKey: string;\n readonly baseURL: string;\n readonly timeout: number;\n\n chat: Chat;\n models: Models;\n\n constructor(options: PendraOptions = {}) {\n const apiKey =\n options.apiKey ??\n (typeof process !== \"undefined\" ? process.env?.PENDRA_API_KEY : undefined);\n\n if (!apiKey) {\n throw new Error(\n \"API key is required — pass apiKey or set the PENDRA_API_KEY environment variable\",\n );\n }\n\n this.apiKey = apiKey;\n this.baseURL = (options.baseURL ?? \"https://api.pendra.ai\").replace(\n /\\/$/,\n \"\",\n );\n this.timeout = options.timeout ?? 120_000;\n\n this.chat = new Chat(this);\n this.models = new Models(this);\n }\n\n /** @internal */\n async _fetch(\n path: string,\n options: {\n method: string;\n body?: Record<string, unknown>;\n stream?: boolean;\n },\n ): Promise<{ response: Response; controller: AbortController }> {\n const controller = new AbortController();\n const timeoutId = setTimeout(() => controller.abort(), this.timeout);\n\n const headers: Record<string, string> = {\n Authorization: `Bearer ${this.apiKey}`,\n \"Content-Type\": \"application/json\",\n };\n\n let response: Response;\n try {\n response = await fetch(`${this.baseURL}${path}`, {\n method: options.method,\n headers,\n body: options.body ? JSON.stringify(options.body) : undefined,\n signal: controller.signal,\n });\n } catch (err) {\n clearTimeout(timeoutId);\n if (err instanceof DOMException && err.name === \"AbortError\") {\n throw new APIConnectionError(\n `Request timed out after ${this.timeout}ms`,\n );\n }\n throw new APIConnectionError(\n err instanceof Error ? err.message : \"Failed to connect to the Pendra API\",\n );\n }\n\n if (!options.stream) {\n clearTimeout(timeoutId);\n }\n\n if (!response.ok) {\n clearTimeout(timeoutId);\n let message: string;\n try {\n const body = (await response.json()) as { detail?: string };\n message = body.detail ?? response.statusText;\n } catch {\n message = response.statusText;\n }\n\n if (response.status === 401) throw new AuthenticationError(message);\n if (response.status === 429) throw new RateLimitError(message);\n throw new APIStatusError(message, response.status);\n }\n\n return { response, controller };\n }\n}\n","export { Pendra } from \"./client\";\nexport type { PendraOptions } from \"./client\";\nexport { Stream } from \"./streaming\";\n\nexport type {\n ChatCompletion,\n ChatCompletionChunk,\n ChatCompletionMessage,\n ChatCompletionCreateParams,\n Choice,\n StreamChoice,\n ChoiceDelta,\n Usage,\n} from \"./types/chat\";\n\nexport type { Model } from \"./types/models\";\n\nexport {\n APIError,\n APIConnectionError,\n APIStatusError,\n AuthenticationError,\n RateLimitError,\n} from \"./errors\";\n\n// Default export for convenient usage:\n// import Pendra from 'pendra';\nimport { Pendra as _Pendra } from \"./client\";\nexport default _Pendra;\n"],"mappings":";AAEO,IAAM,SAAN,MAA2D;AAAA,EAOhE,YAAY,UAAoB,YAA6B;AAL7D,SAAQ,UAAU,IAAI,YAAY;AAClC,SAAQ,SAAS;AACjB,SAAQ,OAAO;AAIb,QAAI,CAAC,SAAS,MAAM;AAClB,YAAM,IAAI,MAAM,sDAAiD;AAAA,IACnE;AACA,SAAK,SAAS,SAAS,KAAK,UAAU;AACtC,SAAK,aAAa;AAAA,EACpB;AAAA,E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,MACF;AACA,YAAM,IAAI;AAAA,QACR,eAAe,QAAQ,IAAI,UAAU;AAAA,MACvC;AAAA,IACF;AAEA,QAAI,CAAC,QAAQ,QAAQ;AACnB,mBAAa,SAAS;AAAA,IACxB;AAEA,QAAI,CAAC,SAAS,IAAI;AAChB,mBAAa,SAAS;AACtB,UAAI;AACJ,UAAI;AACF,cAAM,OAAQ,MAAM,SAAS,KAAK;AAClC,kBAAU,KAAK,UAAU,SAAS;AAAA,MACpC,QAAQ;AACN,kBAAU,SAAS;AAAA,MACrB;AAEA,UAAI,SAAS,WAAW,IAAK,OAAM,IAAI,oBAAoB,OAAO;AAClE,UAAI,SAAS,WAAW,IAAK,OAAM,IAAI,eAAe,OAAO;AAC7D,YAAM,IAAI,eAAe,SAAS,SAAS,MAAM;AAAA,IACnD;AAEA,WAAO,EAAE,UAAU,WAAW;AAAA,EAChC;AACF;;;AC3EA,IAAO,gBAAQ;","names":["response"]}
1
+ {"version":3,"sources":["../src/streaming.ts","../src/resources/chat/completions.ts","../src/resources/chat/index.ts","../src/resources/images/generations.ts","../src/resources/images/index.ts","../src/resources/models.ts","../src/errors.ts","../src/client.ts","../src/index.ts"],"sourcesContent":["import type { ChatCompletionChunk } from \"./types/chat\";\n\nexport class Stream implements AsyncIterable<ChatCompletionChunk> {\n private reader: ReadableStreamDefaultReader<Uint8Array>;\n private decoder = new TextDecoder();\n private buffer = \"\";\n private done = false;\n private controller: AbortController;\n\n constructor(response: Response, controller: AbortController) {\n if (!response.body) {\n throw new Error(\"Response body is null — streaming not supported\");\n }\n this.reader = response.body.getReader();\n this.controller = controller;\n }\n\n async *[Symbol.asyncIterator](): AsyncIterator<ChatCompletionChunk> {\n try {\n while (!this.done) {\n const { value, done: readerDone } = await this.reader.read();\n if (readerDone) {\n this.done = true;\n break;\n }\n\n this.buffer += this.decoder.decode(value, { stream: true });\n const lines = this.buffer.split(\"\\n\");\n this.buffer = lines.pop() ?? \"\";\n\n for (const line of lines) {\n const trimmed = line.trim();\n if (!trimmed || trimmed.startsWith(\":\")) continue;\n\n if (!trimmed.startsWith(\"data: \")) continue;\n const data = trimmed.slice(6);\n\n if (data === \"[DONE]\") {\n this.done = true;\n return;\n }\n\n try {\n yield JSON.parse(data) as ChatCompletionChunk;\n } catch {\n // skip malformed JSON\n }\n }\n }\n } finally {\n this.reader.releaseLock();\n }\n }\n\n abort(): void {\n this.done = true;\n this.controller.abort();\n }\n}\n","import type { Pendra } from \"../../client\";\nimport type {\n ChatCompletion,\n ChatCompletionChunk,\n ChatCompletionCreateParams,\n} from \"../../types/chat\";\nimport { Stream } from \"../../streaming\";\n\nexport class Completions {\n private client: Pendra;\n\n constructor(client: Pendra) {\n this.client = client;\n }\n\n create(\n params: ChatCompletionCreateParams & { stream: true },\n ): Promise<Stream>;\n create(\n params: ChatCompletionCreateParams & { stream?: false },\n ): Promise<ChatCompletion>;\n create(\n params: ChatCompletionCreateParams,\n ): Promise<ChatCompletion | Stream>;\n async create(\n params: ChatCompletionCreateParams,\n ): Promise<ChatCompletion | Stream> {\n const body: Record<string, unknown> = {\n model: params.model,\n messages: params.messages,\n };\n\n if (params.stream != null) body.stream = params.stream;\n if (params.temperature != null) body.temperature = params.temperature;\n if (params.max_tokens != null) body.max_tokens = params.max_tokens;\n if (params.top_p != null) body.top_p = params.top_p;\n if (params.stop != null) body.stop = params.stop;\n\n if (params.stream) {\n const { response, controller } = await this.client._fetch(\n \"/api/v1/chat/completions\",\n { method: \"POST\", body, stream: true },\n );\n return new Stream(response, controller);\n }\n\n const { response } = await this.client._fetch(\n \"/api/v1/chat/completions\",\n { method: \"POST\", body },\n );\n return (await response.json()) as ChatCompletion;\n }\n}\n","import type { Pendra } from \"../../client\";\nimport { Completions } from \"./completions\";\n\nexport class Chat {\n completions: Completions;\n\n constructor(client: Pendra) {\n this.completions = new Completions(client);\n }\n}\n","import type { Pendra } from \"../../client\";\nimport type { ImageGenerationCreateParams, ImageResponse } from \"../../types/images\";\n\nconst SIZE_RE = /^\\d+x\\d+$/;\n\nfunction validateParams(params: ImageGenerationCreateParams): void {\n if (!params.prompt || !params.prompt.trim()) {\n throw new Error(\"prompt must be a non-empty string\");\n }\n if (params.n != null && (params.n < 1 || params.n > 4)) {\n throw new Error(\"n must be between 1 and 4\");\n }\n if (params.size != null && !SIZE_RE.test(params.size)) {\n throw new Error(\"size must be in the form WIDTHxHEIGHT (e.g. '1024x1024')\");\n }\n}\n\nexport class Generations {\n private client: Pendra;\n\n constructor(client: Pendra) {\n this.client = client;\n }\n\n async create(params: ImageGenerationCreateParams): Promise<ImageResponse> {\n validateParams(params);\n\n const body: Record<string, unknown> = {\n model: params.model,\n prompt: params.prompt,\n };\n\n if (params.n != null) body.n = params.n;\n if (params.size != null) body.size = params.size;\n if (params.response_format != null) body.response_format = params.response_format;\n if (params.num_inference_steps != null) body.num_inference_steps = params.num_inference_steps;\n if (params.seed != null) body.seed = params.seed;\n if (params.negative_prompt != null) body.negative_prompt = params.negative_prompt;\n\n const { response } = await this.client._fetch(\n \"/api/v1/images/generations\",\n { method: \"POST\", body },\n );\n return (await response.json()) as ImageResponse;\n }\n}\n","import type { Pendra } from \"../../client\";\nimport { Generations } from \"./generations\";\n\nexport class Images {\n generations: Generations;\n\n constructor(client: Pendra) {\n this.generations = new Generations(client);\n }\n}\n","import type { Pendra } from \"../client\";\nimport type { Model } from \"../types/models\";\n\nexport class Models {\n private client: Pendra;\n\n constructor(client: Pendra) {\n this.client = client;\n }\n\n async list(): Promise<Model[]> {\n const { response } = await this.client._fetch(\"/api/v1/models\", {\n method: \"GET\",\n });\n const data = (await response.json()) as { data: Model[] };\n return data.data;\n }\n}\n","export class APIError extends Error {\n readonly status: number | undefined;\n\n constructor(message: string, status?: number) {\n super(message);\n this.name = \"APIError\";\n this.status = status;\n }\n}\n\nexport class APIConnectionError extends APIError {\n constructor(message = \"Failed to connect to the Pendra API\") {\n super(message);\n this.name = \"APIConnectionError\";\n }\n}\n\nexport class APIStatusError extends APIError {\n override readonly status: number;\n\n constructor(message: string, status: number) {\n super(message, status);\n this.name = \"APIStatusError\";\n this.status = status;\n }\n}\n\nexport class AuthenticationError extends APIStatusError {\n constructor(\n message = \"Invalid API key — check your PENDRA_API_KEY\",\n ) {\n super(message, 401);\n this.name = \"AuthenticationError\";\n }\n}\n\nexport class RateLimitError extends APIStatusError {\n constructor(message = \"Rate limit exceeded\") {\n super(message, 429);\n this.name = \"RateLimitError\";\n }\n}\n","import { Chat } from \"./resources/chat/index\";\nimport { Images } from \"./resources/images/index\";\nimport { Models } from \"./resources/models\";\nimport {\n APIConnectionError,\n APIStatusError,\n AuthenticationError,\n RateLimitError,\n} from \"./errors\";\n\nexport interface PendraOptions {\n apiKey?: string;\n baseURL?: string;\n timeout?: number;\n}\n\nexport class Pendra {\n readonly apiKey: string;\n readonly baseURL: string;\n readonly timeout: number;\n\n chat: Chat;\n images: Images;\n models: Models;\n\n constructor(options: PendraOptions = {}) {\n const apiKey =\n options.apiKey ??\n (typeof process !== \"undefined\" ? process.env?.PENDRA_API_KEY : undefined);\n\n if (!apiKey) {\n throw new Error(\n \"API key is required — pass apiKey or set the PENDRA_API_KEY environment variable\",\n );\n }\n\n this.apiKey = apiKey;\n this.baseURL = (options.baseURL ?? \"https://api.pendra.ai\").replace(\n /\\/$/,\n \"\",\n );\n this.timeout = options.timeout ?? 120_000;\n\n this.chat = new Chat(this);\n this.images = new Images(this);\n this.models = new Models(this);\n }\n\n /** @internal */\n async _fetch(\n path: string,\n options: {\n method: string;\n body?: Record<string, unknown>;\n stream?: boolean;\n },\n ): Promise<{ response: Response; controller: AbortController }> {\n const controller = new AbortController();\n const timeoutId = setTimeout(() => controller.abort(), this.timeout);\n\n const headers: Record<string, string> = {\n Authorization: `Bearer ${this.apiKey}`,\n \"Content-Type\": \"application/json\",\n };\n\n let response: Response;\n try {\n response = await fetch(`${this.baseURL}${path}`, {\n method: options.method,\n headers,\n body: options.body ? JSON.stringify(options.body) : undefined,\n signal: controller.signal,\n });\n } catch (err) {\n clearTimeout(timeoutId);\n if (err instanceof DOMException && err.name === \"AbortError\") {\n throw new APIConnectionError(\n `Request timed out after ${this.timeout}ms`,\n );\n }\n throw new APIConnectionError(\n err instanceof Error ? err.message : \"Failed to connect to the Pendra API\",\n );\n }\n\n if (!options.stream) {\n clearTimeout(timeoutId);\n }\n\n if (!response.ok) {\n clearTimeout(timeoutId);\n let message: string;\n try {\n const body = (await response.json()) as { detail?: string };\n message = body.detail ?? response.statusText;\n } catch {\n message = response.statusText;\n }\n\n if (response.status === 401) throw new AuthenticationError(message);\n if (response.status === 429) throw new RateLimitError(message);\n throw new APIStatusError(message, response.status);\n }\n\n return { response, controller };\n }\n}\n","export { Pendra } from \"./client\";\nexport type { PendraOptions } from \"./client\";\nexport { Stream } from \"./streaming\";\n\nexport type {\n ChatCompletion,\n ChatCompletionChunk,\n ChatCompletionMessage,\n ChatCompletionCreateParams,\n Choice,\n StreamChoice,\n ChoiceDelta,\n Usage,\n} from \"./types/chat\";\n\nexport type { Model } from \"./types/models\";\n\nexport type {\n ImageGenerationCreateParams,\n ImageData,\n ImageResponse,\n} from \"./types/images\";\n\nexport {\n APIError,\n APIConnectionError,\n APIStatusError,\n AuthenticationError,\n RateLimitError,\n} from \"./errors\";\n\n// Default export for convenient usage:\n// import Pendra from 'pendra';\nimport { Pendra as _Pendra } from \"./client\";\nexport default _Pendra;\n"],"mappings":";AAEO,IAAM,SAAN,MAA2D;AAAA,EAOhE,YAAY,UAAoB,YAA6B;AAL7D,SAAQ,UAAU,IAAI,YAAY;AAClC,SAAQ,SAAS;AACjB,SAAQ,OAAO;AAIb,QAAI,CAAC,SAAS,MAAM;AAClB,YAAM,IAAI,MAAM,sDAAiD;AAAA,IACnE;AACA,SAAK,SAAS,SAAS,KAAK,UAAU;AACtC,SAAK,aAAa;AAAA,EACpB;AAAA,EAEA,QAAQ,OAAO,aAAa,IAAwC;AAClE,QAAI;AACF,aAAO,CAAC,KAAK,MAAM;AACjB,cAAM,EAAE,OAAO,MAAM,WAAW,I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AAAA,IACxB;AAEA,QAAI,CAAC,SAAS,IAAI;AAChB,mBAAa,SAAS;AACtB,UAAI;AACJ,UAAI;AACF,cAAM,OAAQ,MAAM,SAAS,KAAK;AAClC,kBAAU,KAAK,UAAU,SAAS;AAAA,MACpC,QAAQ;AACN,kBAAU,SAAS;AAAA,MACrB;AAEA,UAAI,SAAS,WAAW,IAAK,OAAM,IAAI,oBAAoB,OAAO;AAClE,UAAI,SAAS,WAAW,IAAK,OAAM,IAAI,eAAe,OAAO;AAC7D,YAAM,IAAI,eAAe,SAAS,SAAS,MAAM;AAAA,IACnD;AAEA,WAAO,EAAE,UAAU,WAAW;AAAA,EAChC;AACF;;;ACxEA,IAAO,gBAAQ;","names":["response"]}
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "pendra",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Official TypeScript SDK for the Pendra LLM inference API",
5
+ "homepage": "https://pendra.ai",
5
6
  "main": "./dist/index.cjs",
6
7
  "module": "./dist/index.js",
7
8
  "types": "./dist/index.d.ts",