@yetter/client 0.0.3 → 0.0.4

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
@@ -10,12 +10,26 @@ npm install @yetter/client
10
10
 
11
11
  ## Authentication
12
12
 
13
- The client requires a Yetter API key for authentication. Ensure you have the `YTR_API_KEY` environment variable set:
13
+ The client requires a Yetter API key for authentication. Ensure you have the `YTR_API_KEY` or `REACT_APP_YTR_API_KEY` environment variable set:
14
14
 
15
15
  ```bash
16
16
  export YTR_API_KEY="your_api_key_here"
17
+ export REACT_APP_YTR_API_KEY="your_api_key_here"
17
18
  ```
18
19
 
20
+ Alternatively, you can configure the API key (and optionally the API endpoint) programmatically using the `yetter.configure()` method. This is useful if you prefer not to use environment variables or need to set the key at runtime.
21
+
22
+ ```typescript
23
+ import { yetter } from "@yetter/client";
24
+
25
+ yetter.configure({
26
+ apiKey: "your_api_key_here",
27
+ // endpoint: "https://custom.api.yetter.ai" // if you need to override the default endpoint
28
+ });
29
+ ```
30
+
31
+ If `yetter.configure()` is used, it will override any API key found in environment variables for subsequent API calls. If neither environment variables are set nor `yetter.configure()` is called with an API key, an error will be thrown when attempting to make an API call.
32
+
19
33
  ## Core Functionalities
20
34
 
21
35
  The client is available via the `yetter` object imported from `yetter-js` (or the relevant path to `client.js`/`client.ts` if used directly).
package/dist/client.d.ts CHANGED
@@ -1,5 +1,8 @@
1
- import { GetResponseResponse, SubscribeOptions, GenerateImageResponse, SubmitQueueOptions, GetResultOptions, GetResultResponse, StatusOptions, StatusResponse, StreamOptions, YetterStream } from "./types.js";
1
+ import { ClientOptions, GetResponseResponse, SubscribeOptions, GenerateImageResponse, SubmitQueueOptions, GetResultOptions, GetResultResponse, StatusOptions, StatusResponse, StreamOptions, YetterStream } from "./types.js";
2
2
  export declare class yetter {
3
+ private static apiKey;
4
+ private static endpoint;
5
+ static configure(options: ClientOptions): void;
3
6
  static subscribe(model: string, options: SubscribeOptions): Promise<GetResponseResponse>;
4
7
  static queue: {
5
8
  submit: (model: string, options: SubmitQueueOptions) => Promise<GenerateImageResponse>;
package/dist/client.js CHANGED
@@ -2,13 +2,22 @@ var _a;
2
2
  import { YetterImageClient } from "./api.js";
3
3
  import { EventSourcePolyfill } from 'event-source-polyfill';
4
4
  export class yetter {
5
+ static configure(options) {
6
+ if (options.apiKey) {
7
+ _a.apiKey = options.apiKey;
8
+ }
9
+ if (options.endpoint) {
10
+ _a.endpoint = options.endpoint;
11
+ }
12
+ }
5
13
  static async subscribe(model, options) {
6
14
  var _b;
7
- if (!process.env.YTR_API_KEY && !process.env.REACT_APP_YTR_API_KEY) {
8
- throw new Error("YTR_API_KEY and REACT_APP_YTR_API_KEY are not set");
15
+ if (!_a.apiKey) {
16
+ throw new Error("API key is not configured. Call yetter.configure({ apiKey: 'your_key' }) or set YTR_API_KEY.");
9
17
  }
10
18
  const client = new YetterImageClient({
11
- apiKey: process.env.YTR_API_KEY || process.env.REACT_APP_YTR_API_KEY || "",
19
+ apiKey: _a.apiKey,
20
+ endpoint: _a.endpoint,
12
21
  });
13
22
  const generateResponse = await client.generateImage({
14
23
  model: model,
@@ -55,11 +64,12 @@ export class yetter {
55
64
  return finalResponse;
56
65
  }
57
66
  static async stream(model, options) {
58
- if (!process.env.YTR_API_KEY) {
59
- throw new Error("YTR_API_KEY is not set");
67
+ if (!_a.apiKey) {
68
+ throw new Error("API key is not configured. Call yetter.configure({ apiKey: 'your_key' }) or set YTR_API_KEY.");
60
69
  }
61
70
  const apiClient = new YetterImageClient({
62
- apiKey: process.env.YTR_API_KEY,
71
+ apiKey: _a.apiKey,
72
+ endpoint: _a.endpoint,
63
73
  });
64
74
  const initialApiResponse = await apiClient.generateImage({
65
75
  model: model,
@@ -154,7 +164,7 @@ export class yetter {
154
164
  }
155
165
  };
156
166
  eventSource = new EventSourcePolyfill(sseStreamUrl, {
157
- headers: { 'Authorization': `${process.env.YTR_API_KEY}` }
167
+ headers: { 'Authorization': `${_a.apiKey}` }
158
168
  });
159
169
  eventSource.onopen = (event) => {
160
170
  console.log("SSE Connection Opened:", event);
@@ -214,13 +224,16 @@ export class yetter {
214
224
  }
215
225
  }
216
226
  _a = yetter;
227
+ yetter.apiKey = process.env.YTR_API_KEY || process.env.REACT_APP_YTR_API_KEY || "";
228
+ yetter.endpoint = "https://api.yetter.ai";
217
229
  yetter.queue = {
218
230
  submit: async (model, options) => {
219
- if (!process.env.YTR_API_KEY) {
220
- throw new Error("YTR_API_KEY is not set");
231
+ if (!_a.apiKey) {
232
+ throw new Error("API key is not configured. Call yetter.configure({ apiKey: 'your_key' }) or set YTR_API_KEY.");
221
233
  }
222
234
  const client = new YetterImageClient({
223
- apiKey: process.env.YTR_API_KEY,
235
+ apiKey: _a.apiKey,
236
+ endpoint: _a.endpoint,
224
237
  });
225
238
  const generateResponse = await client.generateImage({
226
239
  model: model,
@@ -229,11 +242,12 @@ yetter.queue = {
229
242
  return generateResponse;
230
243
  },
231
244
  status: async (model, options) => {
232
- if (!process.env.YTR_API_KEY) {
233
- throw new Error("YTR_API_KEY is not set");
245
+ if (!_a.apiKey) {
246
+ throw new Error("API key is not configured. Call yetter.configure({ apiKey: 'your_key' }) or set YTR_API_KEY.");
234
247
  }
235
248
  const client = new YetterImageClient({
236
- apiKey: process.env.YTR_API_KEY,
249
+ apiKey: _a.apiKey,
250
+ endpoint: _a.endpoint,
237
251
  });
238
252
  const endpoint = client.getApiEndpoint();
239
253
  const statusUrl = `${endpoint}/${model}/requests/${options.requestId}/status`;
@@ -244,11 +258,12 @@ yetter.queue = {
244
258
  };
245
259
  },
246
260
  result: async (model, options) => {
247
- if (!process.env.YTR_API_KEY) {
248
- throw new Error("YTR_API_KEY is not set");
261
+ if (!_a.apiKey) {
262
+ throw new Error("API key is not configured. Call yetter.configure({ apiKey: 'your_key' }) or set YTR_API_KEY.");
249
263
  }
250
264
  const client = new YetterImageClient({
251
- apiKey: process.env.YTR_API_KEY,
265
+ apiKey: _a.apiKey,
266
+ endpoint: _a.endpoint,
252
267
  });
253
268
  const endpoint = client.getApiEndpoint();
254
269
  const responseUrl = `${endpoint}/${model}/requests/${options.requestId}`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yetter/client",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "build": "tsc",
package/src/client.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { YetterImageClient } from "./api.js";
2
2
  import { EventSourcePolyfill } from 'event-source-polyfill';
3
3
  import {
4
+ ClientOptions,
4
5
  GetStatusResponse,
5
6
  GetResponseResponse,
6
7
  SubscribeOptions,
@@ -16,15 +17,28 @@ import {
16
17
  } from "./types.js";
17
18
 
18
19
  export class yetter {
20
+ private static apiKey: string = process.env.YTR_API_KEY || process.env.REACT_APP_YTR_API_KEY || "";
21
+ private static endpoint: string = "https://api.yetter.ai";
22
+
23
+ public static configure(options: ClientOptions) {
24
+ if (options.apiKey) {
25
+ yetter.apiKey = options.apiKey;
26
+ }
27
+ if (options.endpoint) {
28
+ yetter.endpoint = options.endpoint;
29
+ }
30
+ }
31
+
19
32
  public static async subscribe(
20
33
  model: string,
21
34
  options: SubscribeOptions
22
35
  ): Promise<GetResponseResponse> {
23
- if (!process.env.YTR_API_KEY && !process.env.REACT_APP_YTR_API_KEY) {
24
- throw new Error("YTR_API_KEY and REACT_APP_YTR_API_KEY are not set");
36
+ if (!yetter.apiKey) {
37
+ throw new Error("API key is not configured. Call yetter.configure({ apiKey: 'your_key' }) or set YTR_API_KEY.");
25
38
  }
26
39
  const client = new YetterImageClient({
27
- apiKey: process.env.YTR_API_KEY || process.env.REACT_APP_YTR_API_KEY || "",
40
+ apiKey: yetter.apiKey,
41
+ endpoint: yetter.endpoint,
28
42
  });
29
43
 
30
44
  const generateResponse = await client.generateImage({
@@ -83,11 +97,12 @@ export class yetter {
83
97
  model: string,
84
98
  options: SubmitQueueOptions
85
99
  ): Promise<GenerateImageResponse> => {
86
- if (!process.env.YTR_API_KEY) {
87
- throw new Error("YTR_API_KEY is not set");
100
+ if (!yetter.apiKey) {
101
+ throw new Error("API key is not configured. Call yetter.configure({ apiKey: 'your_key' }) or set YTR_API_KEY.");
88
102
  }
89
103
  const client = new YetterImageClient({
90
- apiKey: process.env.YTR_API_KEY,
104
+ apiKey: yetter.apiKey,
105
+ endpoint: yetter.endpoint,
91
106
  });
92
107
 
93
108
  const generateResponse = await client.generateImage({
@@ -102,11 +117,12 @@ export class yetter {
102
117
  model: string,
103
118
  options: StatusOptions
104
119
  ): Promise<StatusResponse> => {
105
- if (!process.env.YTR_API_KEY) {
106
- throw new Error("YTR_API_KEY is not set");
120
+ if (!yetter.apiKey) {
121
+ throw new Error("API key is not configured. Call yetter.configure({ apiKey: 'your_key' }) or set YTR_API_KEY.");
107
122
  }
108
123
  const client = new YetterImageClient({
109
- apiKey: process.env.YTR_API_KEY,
124
+ apiKey: yetter.apiKey,
125
+ endpoint: yetter.endpoint,
110
126
  });
111
127
 
112
128
  const endpoint = client.getApiEndpoint();
@@ -125,11 +141,12 @@ export class yetter {
125
141
  model: string,
126
142
  options: GetResultOptions
127
143
  ): Promise<GetResultResponse> => {
128
- if (!process.env.YTR_API_KEY) {
129
- throw new Error("YTR_API_KEY is not set");
144
+ if (!yetter.apiKey) {
145
+ throw new Error("API key is not configured. Call yetter.configure({ apiKey: 'your_key' }) or set YTR_API_KEY.");
130
146
  }
131
147
  const client = new YetterImageClient({
132
- apiKey: process.env.YTR_API_KEY,
148
+ apiKey: yetter.apiKey,
149
+ endpoint: yetter.endpoint,
133
150
  });
134
151
 
135
152
  const endpoint = client.getApiEndpoint();
@@ -149,11 +166,12 @@ export class yetter {
149
166
  model: string,
150
167
  options: StreamOptions
151
168
  ): Promise<YetterStream> {
152
- if (!process.env.YTR_API_KEY) {
153
- throw new Error("YTR_API_KEY is not set");
169
+ if (!yetter.apiKey) {
170
+ throw new Error("API key is not configured. Call yetter.configure({ apiKey: 'your_key' }) or set YTR_API_KEY.");
154
171
  }
155
172
  const apiClient = new YetterImageClient({
156
- apiKey: process.env.YTR_API_KEY,
173
+ apiKey: yetter.apiKey,
174
+ endpoint: yetter.endpoint,
157
175
  });
158
176
 
159
177
  const initialApiResponse = await apiClient.generateImage({
@@ -249,7 +267,7 @@ export class yetter {
249
267
  };
250
268
 
251
269
  eventSource = new EventSourcePolyfill(sseStreamUrl, {
252
- headers: { 'Authorization': `${process.env.YTR_API_KEY}` }
270
+ headers: { 'Authorization': `${yetter.apiKey}` }
253
271
  } as any);
254
272
 
255
273
  eventSource.onopen = (event: Event) => {
package/stress.ts DELETED
@@ -1,135 +0,0 @@
1
- import { yetter } from "../src/client.js";
2
-
3
- const CONCURRENCY_LEVEL = 5; // Number of streams to run in parallel
4
- const MODEL_NAME = "ytr-ai/flux/dev"; // Model to use for streaming
5
-
6
- let streamCounter = 0;
7
- const activePromises: Promise<void>[] = [];
8
- let shuttingDown = false;
9
-
10
- /**
11
- * Runs a single stream, measures latency, and logs results.
12
- * @param id - A unique identifier for the stream.
13
- */
14
- async function runStream(id: number): Promise<void> {
15
- const startTime = Date.now();
16
- let streamRequestId = "";
17
-
18
- console.log(`[${new Date().toLocaleTimeString()}] [Stream ${id}] Starting...`);
19
-
20
- try {
21
- const streamInstance = await yetter.stream(MODEL_NAME, {
22
- input: {
23
- prompt: `A beautiful landscape painting, style of Van Gogh, stream ${id}`,
24
- // You can add other parameters like seed or num_inference_steps here
25
- },
26
- });
27
- streamRequestId = streamInstance.getRequestId();
28
- console.log(`[${new Date().toLocaleTimeString()}] [Stream ${id}] Initiated. Request ID: ${streamRequestId}`);
29
-
30
- // We are primarily interested in the latency of done(), so event iteration can be minimal or skipped.
31
- // for await (const event of streamInstance) {
32
- // console.log(`[${new Date().toLocaleTimeString()}][STREAM EVENT - ${streamRequestId}] Status: ${event.status}, QPos: ${event.queue_position}`);
33
- // }
34
-
35
- // Wait for the final result from the done() method
36
- await streamInstance.done();
37
- const endTime = Date.now();
38
- const latency = endTime - startTime;
39
- console.log(`[${new Date().toLocaleTimeString()}] [Stream ${id}] Finished. Request ID: ${streamRequestId}. Latency: ${latency}ms`);
40
-
41
- } catch (err: any) {
42
- const endTime = Date.now();
43
- const latency = endTime - startTime; // Still record latency up to the point of failure
44
- console.error(`[${new Date().toLocaleTimeString()}] [Stream ${id}] Failed. Request ID: ${streamRequestId || 'UNKNOWN'}. Latency: ${latency}ms. Error: ${err.message || err}`);
45
- // Optionally, rethrow or handle more specifically if needed
46
- }
47
- }
48
-
49
- /**
50
- * Launches a new stream if concurrency limit is not reached and not shutting down.
51
- * Manages the activePromises array.
52
- */
53
- async function launchStreamIfNotBusy(): Promise<void> {
54
- if (shuttingDown || activePromises.length >= CONCURRENCY_LEVEL) {
55
- return;
56
- }
57
-
58
- streamCounter++;
59
- const currentStreamId = streamCounter;
60
-
61
- console.log(`[${new Date().toLocaleTimeString()}] Launching stream ${currentStreamId}. Active: ${activePromises.length}/${CONCURRENCY_LEVEL}`);
62
-
63
- const streamTask = runStream(currentStreamId);
64
- activePromises.push(streamTask);
65
-
66
- try {
67
- await streamTask;
68
- } catch (e) {
69
- // Errors are logged within runStream
70
- } finally {
71
- const index = activePromises.indexOf(streamTask);
72
- if (index > -1) {
73
- activePromises.splice(index, 1);
74
- }
75
- console.log(`[${new Date().toLocaleTimeString()}] Stream ${currentStreamId} completed. Active: ${activePromises.length}/${CONCURRENCY_LEVEL}`);
76
- // After a stream finishes, try to fill its slot immediately
77
- if (!shuttingDown) {
78
- launchStreamIfNotBusy(); // Non-blocking call to fill the slot
79
- }
80
- }
81
- }
82
-
83
- /**
84
- * Fills available slots up to the CONCURRENCY_LEVEL.
85
- */
86
- function fillSlots() {
87
- while (!shuttingDown && activePromises.length < CONCURRENCY_LEVEL) {
88
- // launchStreamIfNotBusy is async but we don't await it here
89
- // as we want to launch multiple streams in parallel.
90
- // It handles its own addition to activePromises.
91
- launchStreamIfNotBusy();
92
- }
93
- }
94
-
95
- /**
96
- * Main function to run the stress test.
97
- */
98
- async function mainStressTest() {
99
- console.log("--- Starting Yetter Stream Stress Test ---");
100
- console.log(`Concurrency Level (N): ${CONCURRENCY_LEVEL}`);
101
- console.log(`Target Model: ${MODEL_NAME}`);
102
- console.log("Press Ctrl+C to stop gracefully (will finish active streams).");
103
-
104
- process.on('SIGINT', async () => {
105
- console.log('\nSIGINT received. Gracefully shutting down...');
106
- shuttingDown = true;
107
- console.log(`No new streams will be launched. Waiting for ${activePromises.length} active stream(s) to complete...`);
108
- // The main loop will exit once activePromises is empty after shuttingDown is true.
109
- });
110
-
111
- // Initial fill
112
- fillSlots();
113
-
114
- // Keep the script running and responsive to shutdown, also periodically check to fill slots
115
- // in case some mechanism external to stream completion is needed (though launchStreamIfNotBusy's finally block should cover it)
116
- while (true) {
117
- if (shuttingDown && activePromises.length === 0) {
118
- break; // Exit condition: shutting down and all streams are done.
119
- }
120
- // Periodically try to fill slots, mainly as a fallback or if initial fills didn't max out.
121
- // The primary mechanism for refilling is within launchStreamIfNotBusy's finally block.
122
- if (!shuttingDown) {
123
- fillSlots();
124
- }
125
- await new Promise(resolve => setTimeout(resolve, 200)); // Interval for the main loop check
126
- }
127
-
128
- console.log('--- Stress Test Finished ---');
129
- console.log(`Total streams launched during the session: ${streamCounter}`);
130
- }
131
-
132
- mainStressTest().catch(err => {
133
- console.error("Unhandled error in mainStressTest:", err);
134
- process.exit(1);
135
- });