2dai-sdk 1.4.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/LICENSE +21 -0
- package/README.md +1287 -0
- package/dist/client.d.ts +222 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +643 -0
- package/dist/client.js.map +1 -0
- package/dist/constants.d.ts +175 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +260 -0
- package/dist/constants.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +31 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +438 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +6 -0
- package/dist/types.js.map +1 -0
- package/package.json +61 -0
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 2DAI SDK Client
|
|
3
|
+
* Official TypeScript/JavaScript client for 2DAI API
|
|
4
|
+
*/
|
|
5
|
+
import { ClientOptions, ImageGenerationOptions, VideoGenerationOptions, TextGenerationOptions, ImageGenerationResult, VideoGenerationResult, TextGenerationResult, UpscaleOptions, UpscaleResult, APIKeySettings, RateLimitInfo, WsRequestData, WsResponseData, WsImageRequest, WsImg2ImgRequest, WsVideoRequest, WsLlmRequest, WsUpscaleRequest, WsImageResponse, WsVideoResponse, WsLlmResponse, WsUpscaleResponse } from './types';
|
|
6
|
+
/**
|
|
7
|
+
* Main 2DAI SDK Client
|
|
8
|
+
*/
|
|
9
|
+
export declare class TwoDAIClient {
|
|
10
|
+
private apiKey;
|
|
11
|
+
private baseUrl;
|
|
12
|
+
private timeout;
|
|
13
|
+
private debug;
|
|
14
|
+
private axios;
|
|
15
|
+
private ws;
|
|
16
|
+
private wsPingInterval;
|
|
17
|
+
private wsReconnectAttempts;
|
|
18
|
+
private wsPendingRequests;
|
|
19
|
+
private wsShouldReconnect;
|
|
20
|
+
private wsAuthenticated;
|
|
21
|
+
private wsConnecting;
|
|
22
|
+
private wsClosingIntentionally;
|
|
23
|
+
private wsOptions;
|
|
24
|
+
/**
|
|
25
|
+
* Create a new 2DAI SDK client
|
|
26
|
+
*
|
|
27
|
+
* @param options Client configuration options
|
|
28
|
+
*/
|
|
29
|
+
constructor(options: ClientOptions);
|
|
30
|
+
/**
|
|
31
|
+
* Log debug messages
|
|
32
|
+
*/
|
|
33
|
+
private log;
|
|
34
|
+
/**
|
|
35
|
+
* Generate an image from a text prompt
|
|
36
|
+
*/
|
|
37
|
+
generateImage(options: ImageGenerationOptions): Promise<ImageGenerationResult>;
|
|
38
|
+
/**
|
|
39
|
+
* Edit an existing image (img2img)
|
|
40
|
+
* Supports optional width/height for resizing the output image
|
|
41
|
+
* Note: The API does not support strength parameter - it uses fixed settings
|
|
42
|
+
*/
|
|
43
|
+
editImage(imageId: string, options: Omit<ImageGenerationOptions, 'format' | 'style'>): Promise<ImageGenerationResult>;
|
|
44
|
+
/**
|
|
45
|
+
* AI upscale an image (img2ximg)
|
|
46
|
+
* Uses AI to generate prompt automatically - no user prompt needed
|
|
47
|
+
*
|
|
48
|
+
* @param imageId Source image ID from CDN
|
|
49
|
+
* @param options Upscale options (factor: 2-4, default: 2)
|
|
50
|
+
*/
|
|
51
|
+
upscaleImage(imageId: string, options?: UpscaleOptions): Promise<UpscaleResult>;
|
|
52
|
+
/**
|
|
53
|
+
* Generate a video from an image
|
|
54
|
+
*/
|
|
55
|
+
generateVideo(options: VideoGenerationOptions): Promise<VideoGenerationResult>;
|
|
56
|
+
/**
|
|
57
|
+
* Generate text using LLM
|
|
58
|
+
*/
|
|
59
|
+
generateText(options: TextGenerationOptions): Promise<TextGenerationResult>;
|
|
60
|
+
/**
|
|
61
|
+
* Get API key settings
|
|
62
|
+
*/
|
|
63
|
+
getSettings(): Promise<APIKeySettings>;
|
|
64
|
+
/**
|
|
65
|
+
* Update API key watermark
|
|
66
|
+
*/
|
|
67
|
+
updateWatermark(watermarkCdnId: string | undefined): Promise<boolean>;
|
|
68
|
+
/**
|
|
69
|
+
* Check rate limits for all operations
|
|
70
|
+
*/
|
|
71
|
+
checkLimits(): Promise<RateLimitInfo[]>;
|
|
72
|
+
/**
|
|
73
|
+
* Get health status of the API
|
|
74
|
+
*/
|
|
75
|
+
health(): Promise<{
|
|
76
|
+
status: string;
|
|
77
|
+
timestamp: string;
|
|
78
|
+
}>;
|
|
79
|
+
/**
|
|
80
|
+
* Connect to WebSocket server with auto-reconnect support
|
|
81
|
+
*/
|
|
82
|
+
wsConnect(): Promise<void>;
|
|
83
|
+
/**
|
|
84
|
+
* Authenticate with the WebSocket server
|
|
85
|
+
*/
|
|
86
|
+
private wsAuthenticate;
|
|
87
|
+
/**
|
|
88
|
+
* Handle incoming WebSocket messages
|
|
89
|
+
*/
|
|
90
|
+
private wsHandleMessage;
|
|
91
|
+
/**
|
|
92
|
+
* Handle generation result
|
|
93
|
+
*/
|
|
94
|
+
private wsHandleResult;
|
|
95
|
+
/**
|
|
96
|
+
* Handle error response
|
|
97
|
+
*/
|
|
98
|
+
private wsHandleError;
|
|
99
|
+
/**
|
|
100
|
+
* Start ping interval to keep connection alive
|
|
101
|
+
*/
|
|
102
|
+
private wsStartPing;
|
|
103
|
+
/**
|
|
104
|
+
* Stop ping interval
|
|
105
|
+
*/
|
|
106
|
+
private wsStopPing;
|
|
107
|
+
/**
|
|
108
|
+
* Handle WebSocket close
|
|
109
|
+
*/
|
|
110
|
+
private wsHandleClose;
|
|
111
|
+
/**
|
|
112
|
+
* Reconnect with exponential backoff
|
|
113
|
+
*/
|
|
114
|
+
private wsReconnectWithBackoff;
|
|
115
|
+
/**
|
|
116
|
+
* Retry pending requests after reconnection
|
|
117
|
+
*/
|
|
118
|
+
private wsRetryPending;
|
|
119
|
+
/**
|
|
120
|
+
* Reject all pending requests
|
|
121
|
+
*/
|
|
122
|
+
private wsRejectPending;
|
|
123
|
+
/**
|
|
124
|
+
* Low-level WebSocket send. Prefer helper methods: wsGenerateImage, wsGenerateVideo, wsGenerateLlm, wsUpscaleImage
|
|
125
|
+
* @internal
|
|
126
|
+
*/
|
|
127
|
+
wsSend(type: 'generate_image', data: WsImageRequest | WsImg2ImgRequest, timeout?: number): Promise<WsImageResponse>;
|
|
128
|
+
wsSend(type: 'generate_video', data: WsVideoRequest, timeout?: number): Promise<WsVideoResponse>;
|
|
129
|
+
wsSend(type: 'generate_llm', data: WsLlmRequest, timeout?: number): Promise<WsLlmResponse>;
|
|
130
|
+
wsSend(type: 'generate_upscale', data: WsUpscaleRequest, timeout?: number): Promise<WsUpscaleResponse>;
|
|
131
|
+
wsSend(type: string, data: WsRequestData, timeout?: number): Promise<WsResponseData>;
|
|
132
|
+
/**
|
|
133
|
+
* Generate an image via WebSocket (text2img or img2img)
|
|
134
|
+
*
|
|
135
|
+
* @example Text-to-Image
|
|
136
|
+
* ```typescript
|
|
137
|
+
* const result = await client.wsGenerateImage({
|
|
138
|
+
* prompt: 'A sunset over mountains',
|
|
139
|
+
* style: 'realistic',
|
|
140
|
+
* format: 'landscape'
|
|
141
|
+
* });
|
|
142
|
+
* console.log(result.result.imageId);
|
|
143
|
+
* ```
|
|
144
|
+
*
|
|
145
|
+
* @example Image-to-Image (edit)
|
|
146
|
+
* ```typescript
|
|
147
|
+
* const result = await client.wsGenerateImage({
|
|
148
|
+
* imageId: 'existing-image-id',
|
|
149
|
+
* prompt: 'add dramatic lighting'
|
|
150
|
+
* });
|
|
151
|
+
* ```
|
|
152
|
+
*/
|
|
153
|
+
wsGenerateImage(data: WsImageRequest | WsImg2ImgRequest, timeout?: number): Promise<WsImageResponse>;
|
|
154
|
+
/**
|
|
155
|
+
* Generate a video from an image via WebSocket
|
|
156
|
+
*
|
|
157
|
+
* @example
|
|
158
|
+
* ```typescript
|
|
159
|
+
* const result = await client.wsGenerateVideo({
|
|
160
|
+
* imageId: 'source-image-id',
|
|
161
|
+
* duration: 5,
|
|
162
|
+
* fps: 16
|
|
163
|
+
* });
|
|
164
|
+
* console.log(result.result.videoId);
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
wsGenerateVideo(data: WsVideoRequest, timeout?: number): Promise<WsVideoResponse>;
|
|
168
|
+
/**
|
|
169
|
+
* Generate text via LLM over WebSocket
|
|
170
|
+
*
|
|
171
|
+
* @example Basic
|
|
172
|
+
* ```typescript
|
|
173
|
+
* const result = await client.wsGenerateLlm({
|
|
174
|
+
* prompt: 'Explain TypeScript'
|
|
175
|
+
* });
|
|
176
|
+
* console.log(result.result.text);
|
|
177
|
+
* ```
|
|
178
|
+
*
|
|
179
|
+
* @example With system prompt and JSON output
|
|
180
|
+
* ```typescript
|
|
181
|
+
* const result = await client.wsGenerateLlm({
|
|
182
|
+
* prompt: 'List 3 programming languages',
|
|
183
|
+
* system: 'You are a helpful assistant',
|
|
184
|
+
* jsonFormat: true,
|
|
185
|
+
* jsonTemplate: { languages: 'array' }
|
|
186
|
+
* });
|
|
187
|
+
* console.log(result.result.json);
|
|
188
|
+
* ```
|
|
189
|
+
*/
|
|
190
|
+
wsGenerateLlm(data: WsLlmRequest, timeout?: number): Promise<WsLlmResponse>;
|
|
191
|
+
/**
|
|
192
|
+
* AI upscale an image via WebSocket (img2ximg)
|
|
193
|
+
* Uses AI to generate prompt automatically - no user prompt needed
|
|
194
|
+
*
|
|
195
|
+
* @example
|
|
196
|
+
* ```typescript
|
|
197
|
+
* const result = await client.wsUpscaleImage({
|
|
198
|
+
* imageId: 'source-image-id',
|
|
199
|
+
* factor: 2
|
|
200
|
+
* });
|
|
201
|
+
* console.log(result.result.imageId, result.result.width, result.result.height);
|
|
202
|
+
* ```
|
|
203
|
+
*/
|
|
204
|
+
wsUpscaleImage(data: WsUpscaleRequest, timeout?: number): Promise<WsUpscaleResponse>;
|
|
205
|
+
/**
|
|
206
|
+
* Check if WebSocket is connected
|
|
207
|
+
*/
|
|
208
|
+
wsIsConnected(): boolean;
|
|
209
|
+
/**
|
|
210
|
+
* Close WebSocket connection and cleanup
|
|
211
|
+
*/
|
|
212
|
+
close(): Promise<void>;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Create a new 2DAI SDK client
|
|
216
|
+
*
|
|
217
|
+
* @param apiKey Your 2DAI API key
|
|
218
|
+
* @param options Optional client configuration
|
|
219
|
+
*/
|
|
220
|
+
export declare function createClient(apiKey: string, options?: Partial<ClientOptions>): TwoDAIClient;
|
|
221
|
+
export default TwoDAIClient;
|
|
222
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH,OAAO,EACL,aAAa,EACb,sBAAsB,EACtB,sBAAsB,EACtB,qBAAqB,EACrB,qBAAqB,EACrB,qBAAqB,EACrB,oBAAoB,EACpB,cAAc,EACd,aAAa,EACb,cAAc,EACd,aAAa,EAEb,aAAa,EACb,cAAc,EACd,cAAc,EACd,gBAAgB,EAChB,cAAc,EACd,YAAY,EACZ,gBAAgB,EAChB,eAAe,EACf,eAAe,EACf,aAAa,EACb,iBAAiB,EAClB,MAAM,SAAS,CAAC;AAgBjB;;GAEG;AACH,qBAAa,YAAY;IACvB,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,KAAK,CAAU;IACvB,OAAO,CAAC,KAAK,CAAgB;IAG7B,OAAO,CAAC,EAAE,CAA0B;IACpC,OAAO,CAAC,cAAc,CAA+B;IACrD,OAAO,CAAC,mBAAmB,CAAK;IAChC,OAAO,CAAC,iBAAiB,CAAuC;IAChE,OAAO,CAAC,iBAAiB,CAAQ;IACjC,OAAO,CAAC,eAAe,CAAS;IAChC,OAAO,CAAC,YAAY,CAAS;IAC7B,OAAO,CAAC,sBAAsB,CAAS;IACvC,OAAO,CAAC,SAAS,CAKf;IAEF;;;;OAIG;gBACS,OAAO,EAAE,aAAa;IA+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