@heyputer/puter.js 2.0.0 → 2.0.2

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
@@ -15,7 +15,7 @@ npm install @heyputer/puter.js
15
15
  #### ES Modules
16
16
 
17
17
  ```js
18
- import puter from '@heyputer/puterjs';
18
+ import '@heyputer/puter.js';
19
19
  ```
20
20
 
21
21
  #### CommonJS
@@ -34,6 +34,8 @@ Include Puter.js directly in your HTML via CDN in the `<head>` section:
34
34
 
35
35
  ## Usage Example
36
36
 
37
+ After importing, you can use the global `puter` object:
38
+
37
39
  ```js
38
40
  // Print a message
39
41
  puter.print('Hello from Puter.js!');
package/index.d.ts ADDED
@@ -0,0 +1,479 @@
1
+ declare global {
2
+ interface Window {
3
+ puter: Puter;
4
+ }
5
+ }
6
+
7
+ declare namespace Puter {
8
+ // Main Puter interface
9
+ interface Puter {
10
+ // Properties
11
+ appID: string;
12
+ env: 'app' | 'web' | 'gui';
13
+
14
+ // Utility methods
15
+ print(text: string, options?: { code?: boolean }): void;
16
+ randName(separator?: string): string;
17
+ exit(statusCode?: number): void;
18
+
19
+ // Sub-modules
20
+ ai: AI;
21
+ apps: Apps;
22
+ auth: Auth;
23
+ drivers: Drivers;
24
+ fs: FileSystem;
25
+ hosting: Hosting;
26
+ kv: KV;
27
+ net: Networking;
28
+ perms: Permissions;
29
+ ui: UI;
30
+ workers: Workers;
31
+ }
32
+
33
+ // AI Module
34
+ interface AI {
35
+ chat(prompt: string, options?: ChatOptions): Promise<ChatResponse>;
36
+ chat(prompt: string, testMode?: boolean, options?: ChatOptions): Promise<ChatResponse>;
37
+ chat(prompt: string, imageURL?: string, testMode?: boolean, options?: ChatOptions): Promise<ChatResponse>;
38
+ chat(prompt: string, imageURLArray?: string[], testMode?: boolean, options?: ChatOptions): Promise<ChatResponse>;
39
+ chat(messages: ChatMessage[], testMode?: boolean, options?: ChatOptions): Promise<ChatResponse>;
40
+
41
+ img2txt(image: string | File | Blob, testMode?: boolean): Promise<string>;
42
+
43
+ txt2img(prompt: string, testMode?: boolean): Promise<HTMLImageElement>;
44
+ txt2img(prompt: string, options?: Txt2ImgOptions): Promise<HTMLImageElement>;
45
+
46
+ txt2speech(text: string): Promise<HTMLAudioElement>;
47
+ txt2speech(text: string, options?: Txt2SpeechOptions): Promise<HTMLAudioElement>;
48
+ txt2speech(text: string, language?: string): Promise<HTMLAudioElement>;
49
+ txt2speech(text: string, language?: string, voice?: string): Promise<HTMLAudioElement>;
50
+ txt2speech(text: string, language?: string, voice?: string, engine?: string): Promise<HTMLAudioElement>;
51
+ }
52
+
53
+ interface ChatOptions {
54
+ model?: string;
55
+ stream?: boolean;
56
+ max_tokens?: number;
57
+ temperature?: number;
58
+ tools?: ToolDefinition[];
59
+ }
60
+
61
+ interface ToolDefinition {
62
+ type: 'function';
63
+ function: {
64
+ name: string;
65
+ description: string;
66
+ parameters: object;
67
+ strict?: boolean;
68
+ };
69
+ }
70
+
71
+ interface ChatMessage {
72
+ role: 'system' | 'assistant' | 'user' | 'function' | 'tool';
73
+ content: string | ContentObject[];
74
+ tool_call_id?: string;
75
+ }
76
+
77
+ interface ContentObject {
78
+ type: 'text' | 'file';
79
+ text?: string;
80
+ puter_path?: string;
81
+ }
82
+
83
+ interface ChatResponse {
84
+ message: {
85
+ role: string;
86
+ content: string;
87
+ tool_calls?: ToolCall[];
88
+ };
89
+ }
90
+
91
+ interface ToolCall {
92
+ id: string;
93
+ function: {
94
+ name: string;
95
+ arguments: string;
96
+ };
97
+ }
98
+
99
+ interface Txt2ImgOptions {
100
+ model?: 'gpt-image-1' | 'gemini-2.5-flash-image-preview' | 'dall-e-3';
101
+ quality?: 'high' | 'medium' | 'low' | 'hd' | 'standard';
102
+ input_image?: string;
103
+ input_image_mime_type?: string;
104
+ }
105
+
106
+ interface Txt2SpeechOptions {
107
+ language?: string;
108
+ voice?: string;
109
+ engine?: 'standard' | 'neural' | 'generative';
110
+ }
111
+
112
+ // Apps Module
113
+ interface Apps {
114
+ create(name: string, indexURL: string): Promise<App>;
115
+ create(name: string, indexURL: string, title?: string): Promise<App>;
116
+ create(name: string, indexURL: string, title?: string, description?: string): Promise<App>;
117
+ create(options: CreateAppOptions): Promise<App>;
118
+
119
+ delete(name: string): Promise<App>;
120
+ get(name: string, options?: GetAppOptions): Promise<App>;
121
+ list(options?: ListAppOptions): Promise<App[]>;
122
+ update(name: string, attributes: UpdateAppAttributes): Promise<App>;
123
+ }
124
+
125
+ interface CreateAppOptions {
126
+ name: string;
127
+ indexURL: string;
128
+ title?: string;
129
+ description?: string;
130
+ icon?: string;
131
+ maximizeOnStart?: boolean;
132
+ filetypeAssociations?: string[];
133
+ }
134
+
135
+ interface GetAppOptions {
136
+ stats_period?: StatsPeriod;
137
+ icon_size?: null | 16 | 32 | 64 | 128 | 256 | 512;
138
+ }
139
+
140
+ interface ListAppOptions extends GetAppOptions { }
141
+
142
+ interface UpdateAppAttributes {
143
+ name?: string;
144
+ indexURL?: string;
145
+ title?: string;
146
+ description?: string;
147
+ icon?: string;
148
+ maximizeOnStart?: boolean;
149
+ filetypeAssociations?: string[];
150
+ }
151
+
152
+ type StatsPeriod = 'all' | 'today' | 'yesterday' | '7d' | '30d' | 'this_month' | 'last_month' | 'this_year' | 'last_year' | 'month_to_date' | 'year_to_date' | 'last_12_months';
153
+
154
+ interface App {
155
+ uid: string;
156
+ name: string;
157
+ icon: string;
158
+ description: string;
159
+ title: string;
160
+ maximize_on_start: boolean;
161
+ index_url: string;
162
+ created_at: string;
163
+ background: boolean;
164
+ filetype_associations: string[];
165
+ open_count: number;
166
+ user_count: number;
167
+ }
168
+
169
+ // Auth Module
170
+ interface Auth {
171
+ signIn(options?: { attempt_temp_user_creation?: boolean }): Promise<boolean>;
172
+ signOut(): void;
173
+ isSignedIn(): boolean;
174
+ getUser(): Promise<User>;
175
+ }
176
+
177
+ interface User {
178
+ uuid: string;
179
+ username: string;
180
+ email_confirmed: boolean;
181
+ }
182
+
183
+ // Drivers Module
184
+ interface Drivers {
185
+ call(interface: string, driver: string, method: string, args?: object): Promise<any>;
186
+ }
187
+
188
+ // FileSystem Module
189
+ interface FileSystem {
190
+ copy(source: string, destination: string, options?: CopyOptions): Promise<FSItem>;
191
+ delete(path: string, options?: DeleteOptions): Promise<void>;
192
+ getReadURL(path: string, expiresIn?: number): Promise<string>;
193
+ mkdir(path: string, options?: MkdirOptions): Promise<FSItem>;
194
+ move(source: string, destination: string, options?: MoveOptions): Promise<FSItem>;
195
+ read(path: string, options?: ReadOptions): Promise<Blob>;
196
+ readdir(path: string, options?: ReaddirOptions): Promise<FSItem[]>;
197
+ readdir(options?: ReaddirOptions): Promise<FSItem[]>;
198
+ rename(path: string, newName: string): Promise<FSItem>;
199
+ space(): Promise<SpaceInfo>;
200
+ stat(path: string): Promise<FSItem>;
201
+ upload(items: FileList | File[] | Blob[], dirPath?: string, options?: object): Promise<FSItem[]>;
202
+ write(path: string, data?: string | File | Blob, options?: WriteOptions): Promise<FSItem>;
203
+ }
204
+
205
+ interface CopyOptions {
206
+ overwrite?: boolean;
207
+ dedupeName?: boolean;
208
+ newName?: string;
209
+ }
210
+
211
+ interface DeleteOptions {
212
+ recursive?: boolean;
213
+ descendantsOnly?: boolean;
214
+ }
215
+
216
+ interface MkdirOptions {
217
+ overwrite?: boolean;
218
+ dedupeName?: boolean;
219
+ createMissingParents?: boolean;
220
+ }
221
+
222
+ interface MoveOptions extends CopyOptions {
223
+ createMissingParents?: boolean;
224
+ }
225
+
226
+ interface ReadOptions {
227
+ offset?: number;
228
+ byte_count?: number;
229
+ }
230
+
231
+ interface ReaddirOptions {
232
+ path?: string;
233
+ uid?: string;
234
+ }
235
+
236
+ interface WriteOptions {
237
+ overwrite?: boolean;
238
+ dedupeName?: boolean;
239
+ createMissingParents?: boolean;
240
+ }
241
+
242
+ interface SpaceInfo {
243
+ capacity: number;
244
+ used: number;
245
+ }
246
+
247
+ interface FSItem {
248
+ id: string;
249
+ uid: string;
250
+ name: string;
251
+ path: string;
252
+ is_dir: boolean;
253
+ parent_id: string;
254
+ parent_uid: string;
255
+ created: number;
256
+ modified: number;
257
+ accessed: number;
258
+ size: number | null;
259
+ writable: boolean;
260
+ read(): Promise<Blob>;
261
+ readdir(): Promise<FSItem[]>;
262
+ }
263
+
264
+ // Hosting Module
265
+ interface Hosting {
266
+ create(subdomain: string, dirPath?: string): Promise<Subdomain>;
267
+ delete(subdomain: string): Promise<boolean>;
268
+ get(subdomain: string): Promise<Subdomain>;
269
+ list(): Promise<Subdomain[]>;
270
+ update(subdomain: string, dirPath?: string): Promise<Subdomain>;
271
+ }
272
+
273
+ interface Subdomain {
274
+ uid: string;
275
+ subdomain: string;
276
+ root_dir: FSItem;
277
+ }
278
+
279
+ // KV Module
280
+ interface KV {
281
+ set(key: string, value: string | number | boolean | object | any[]): Promise<boolean>;
282
+ get(key: string): Promise<any>;
283
+ del(key: string): Promise<boolean>;
284
+ incr(key: string, amount?: number): Promise<number>;
285
+ decr(key: string, amount?: number): Promise<number>;
286
+ list(pattern?: string, returnValues?: boolean): Promise<string[] | KVPair[]>;
287
+ list(returnValues?: boolean): Promise<string[] | KVPair[]>;
288
+ flush(): Promise<boolean>;
289
+ }
290
+
291
+ interface KVPair {
292
+ key: string;
293
+ value: any;
294
+ }
295
+
296
+ // Networking Module
297
+ interface Networking {
298
+ fetch(url: string, options?: RequestInit): Promise<Response>;
299
+ Socket: typeof Socket;
300
+ tls: {
301
+ TLSSocket: typeof TLSSocket;
302
+ };
303
+ }
304
+
305
+ class Socket {
306
+ constructor(hostname: string, port: number);
307
+ write(data: ArrayBuffer | Uint8Array | string): void;
308
+ close(): void;
309
+ on(event: 'open', callback: () => void): void;
310
+ on(event: 'data', callback: (buffer: Uint8Array) => void): void;
311
+ on(event: 'error', callback: (reason: string) => void): void;
312
+ on(event: 'close', callback: (hadError: boolean) => void): void;
313
+ }
314
+
315
+ class TLSSocket extends Socket {
316
+ constructor(hostname: string, port: number);
317
+ }
318
+
319
+ // Permissions Module
320
+ interface Permissions {
321
+ grantApp(app_uid: string, permissionString: string): Promise<object>;
322
+ grantAppAnyUser(app_uid: string, permissionString: string): Promise<object>;
323
+ grantGroup(group_uid: string, permissionString: string): Promise<object>;
324
+ grantOrigin(origin: string, permissionString: string): Promise<object>;
325
+ grantUser(username: string, permissionString: string): Promise<object>;
326
+ revokeApp(app_uid: string, permissionString: string): Promise<object>;
327
+ revokeAppAnyUser(app_uid: string, permissionString: string): Promise<object>;
328
+ revokeGroup(group_uid: string, permissionString: string): Promise<object>;
329
+ revokeOrigin(origin: string, permissionString: string): Promise<object>;
330
+ revokeUser(username: string, permissionString: string): Promise<object>;
331
+ }
332
+
333
+ // UI Module
334
+ interface UI {
335
+ alert(message?: string, buttons?: AlertButton[]): Promise<string>;
336
+ prompt(message?: string, defaultValue?: string): Promise<string | null>;
337
+ authenticateWithPuter(): Promise<boolean>;
338
+ contextMenu(options: ContextMenuOptions): void;
339
+ createWindow(options?: WindowOptions): void;
340
+ exit(statusCode?: number): void;
341
+ getLanguage(): Promise<string>;
342
+ hideSpinner(): void;
343
+ launchApp(appName?: string, args?: object): Promise<AppConnection>;
344
+ launchApp(options: LaunchAppOptions): Promise<AppConnection>;
345
+ on(eventName: 'localeChanged', handler: (data: { language: string }) => void): void;
346
+ on(eventName: 'themeChanged', handler: (data: ThemeData) => void): void;
347
+ onItemsOpened(handler: (items: FSItem[]) => void): void;
348
+ onLaunchedWithItems(handler: (items: FSItem[]) => void): void;
349
+ onWindowClose(handler: () => void): void;
350
+ parentApp(): AppConnection | null;
351
+ setMenubar(options: MenubarOptions): void;
352
+ setWindowHeight(height: number): void;
353
+ setWindowPosition(x: number, y: number): void;
354
+ setWindowSize(width: number, height: number): void;
355
+ setWindowTitle(title: string): void;
356
+ setWindowWidth(width: number): void;
357
+ setWindowX(x: number): void;
358
+ setWindowY(y: number): void;
359
+ showColorPicker(defaultColor?: string): Promise<string>;
360
+ showColorPicker(options?: object): Promise<string>;
361
+ showDirectoryPicker(options?: { multiple?: boolean }): Promise<FSItem | FSItem[]>;
362
+ showFontPicker(defaultFont?: string): Promise<{ fontFamily: string }>;
363
+ showFontPicker(options?: object): Promise<{ fontFamily: string }>;
364
+ showOpenFilePicker(options?: FilePickerOptions): Promise<FSItem | FSItem[]>;
365
+ showSaveFilePicker(data?: any, defaultFileName?: string): Promise<FSItem>;
366
+ showSpinner(): void;
367
+ socialShare(url: string, message?: string, options?: { left?: number; top?: number }): void;
368
+ wasLaunchedWithItems(): boolean;
369
+ }
370
+
371
+ interface AlertButton {
372
+ label: string;
373
+ value?: string;
374
+ type?: 'primary' | 'success' | 'info' | 'warning' | 'danger';
375
+ }
376
+
377
+ interface ContextMenuOptions {
378
+ items: (ContextMenuItem | '-')[];
379
+ }
380
+
381
+ interface ContextMenuItem {
382
+ label: string;
383
+ action?: () => void;
384
+ icon?: string;
385
+ icon_active?: string;
386
+ disabled?: boolean;
387
+ items?: (ContextMenuItem | '-')[];
388
+ }
389
+
390
+ interface WindowOptions {
391
+ center?: boolean;
392
+ content?: string;
393
+ disable_parent_window?: boolean;
394
+ has_head?: boolean;
395
+ height?: number;
396
+ is_resizable?: boolean;
397
+ show_in_taskbar?: boolean;
398
+ title?: string;
399
+ width?: number;
400
+ }
401
+
402
+ interface LaunchAppOptions {
403
+ name?: string;
404
+ args?: object;
405
+ }
406
+
407
+ interface ThemeData {
408
+ palette: {
409
+ primaryHue: number;
410
+ primarySaturation: string;
411
+ primaryLightness: string;
412
+ primaryAlpha: number;
413
+ primaryColor: string;
414
+ };
415
+ }
416
+
417
+ interface MenubarOptions {
418
+ items: MenuItem[];
419
+ }
420
+
421
+ interface MenuItem {
422
+ label: string;
423
+ action?: () => void;
424
+ items?: MenuItem[];
425
+ }
426
+
427
+ interface FilePickerOptions {
428
+ multiple?: boolean;
429
+ accept?: string | string[];
430
+ }
431
+
432
+ interface AppConnection {
433
+ usesSDK: boolean;
434
+ on(eventName: 'message', handler: (message: any) => void): void;
435
+ on(eventName: 'close', handler: (data: { appInstanceID: string }) => void): void;
436
+ off(eventName: string, handler: Function): void;
437
+ postMessage(message: any): void;
438
+ close(): void;
439
+ }
440
+
441
+ // Workers Module
442
+ interface Workers {
443
+ create(workerName: string, filePath: string): Promise<WorkerDeployment>;
444
+ delete(workerName: string): Promise<boolean>;
445
+ exec(workerURL: string, options?: WorkerExecOptions): Promise<Response>;
446
+ get(workerName: string): Promise<WorkerInfo>;
447
+ list(): Promise<WorkerInfo[]>;
448
+ }
449
+
450
+ interface WorkerDeployment {
451
+ success: boolean;
452
+ url: string;
453
+ errors: any[];
454
+ }
455
+
456
+ interface WorkerExecOptions extends RequestInit {
457
+ method?: string;
458
+ headers?: object;
459
+ body?: string | object;
460
+ cache?: RequestCache;
461
+ credentials?: RequestCredentials;
462
+ mode?: RequestMode;
463
+ redirect?: RequestRedirect;
464
+ referrer?: string;
465
+ signal?: AbortSignal;
466
+ }
467
+
468
+ interface WorkerInfo {
469
+ name: string;
470
+ url: string;
471
+ file_path: string;
472
+ file_uid: string;
473
+ created_at: string;
474
+ }
475
+ }
476
+
477
+ declare const puter: Puter.Puter;
478
+
479
+ export = Puter;
package/package.json CHANGED
@@ -1,8 +1,10 @@
1
1
  {
2
2
  "name": "@heyputer/puter.js",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "description": "Puter.js - A JavaScript library for interacting with Puter services.",
5
- "main": "index.js",
5
+ "main": "src/entry.js",
6
+ "types": "index.d.ts",
7
+ "typings": "index.d.ts",
6
8
  "type": "module",
7
9
  "publishConfig": {
8
10
  "registry": "https://registry.npmjs.org/"
@@ -11,13 +13,17 @@
11
13
  "type": "git",
12
14
  "url": "git+https://github.com/HeyPuter/puter.git"
13
15
  },
16
+ "keywords": [
17
+ "puter",
18
+ "puter.js",
19
+ "puterjs"
20
+ ],
14
21
  "scripts": {
15
22
  "start-server": "npx http-server --cors -c-1",
16
23
  "start-webpack": "webpack && webpack --output-filename puter.dev.js --watch --devtool source-map",
17
24
  "start": "concurrently \"npm run start-server\" \"npm run start-webpack\"",
18
25
  "build": "webpack && { echo \"// Copyright 2024-present Puter Technologies Inc. All rights reserved.\"; echo \"// Generated on $(date '+%Y-%m-%d %H:%M')\n\"; cat ./dist/puter.js; } > temp && mv temp ./dist/puter.js"
19
26
  },
20
- "keywords": [],
21
27
  "author": "Puter Technologies Inc.",
22
28
  "license": "Apache-2.0",
23
29
  "devDependencies": {
@@ -25,6 +31,7 @@
25
31
  "webpack-cli": "^5.1.4"
26
32
  },
27
33
  "dependencies": {
28
- "@heyputer/kv.js": "^0.1.92"
34
+ "@heyputer/kv.js": "^0.1.92",
35
+ "@heyputer/putility": "^1.0.3"
29
36
  }
30
37
  }
package/src/entry.js ADDED
@@ -0,0 +1,9 @@
1
+ const goodContext = {}
2
+
3
+ let puter;
4
+ with (goodContext) {
5
+ puter = await import('./src/index.js');
6
+ }
7
+
8
+
9
+ module.exports = { puter };
package/webpack.config.js CHANGED
@@ -11,15 +11,15 @@ import { fileURLToPath } from 'url';
11
11
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
12
12
 
13
13
  export default {
14
- entry: './src/index.js',
15
- output: {
16
- filename: 'puter.js',
17
- path: path.resolve(__dirname, 'dist'),
18
- },
19
- plugins: [
20
- new webpack.DefinePlugin({
21
- 'globalThis.PUTER_ORIGIN': JSON.stringify(process.env.PUTER_ORIGIN || 'https://puter.com'),
22
- 'globalThis.PUTER_API_ORIGIN': JSON.stringify(process.env.PUTER_API_ORIGIN || 'https://api.puter.com'),
23
- }),
24
- ],
14
+ entry: './src/index.js',
15
+ output: {
16
+ filename: 'puter.js',
17
+ path: path.resolve(__dirname, 'dist'),
18
+ },
19
+ plugins: [
20
+ new webpack.DefinePlugin({
21
+ 'globalThis.PUTER_ORIGIN': JSON.stringify(process.env.PUTER_ORIGIN || 'https://puter.com'),
22
+ 'globalThis.PUTER_API_ORIGIN': JSON.stringify(process.env.PUTER_API_ORIGIN || 'https://api.puter.com'),
23
+ }),
24
+ ],
25
25
  };