opencode-puter-auth 1.0.0-beta.1

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/dist/auth.js ADDED
@@ -0,0 +1,436 @@
1
+ /**
2
+ * Puter Authentication Manager
3
+ *
4
+ * Handles OAuth authentication with Puter.com via browser popup
5
+ *
6
+ * IMPORTANT: Puter uses popup-based auth (puter.auth.signIn()) which returns a token.
7
+ * For CLI tools, we serve an HTML page that handles the popup auth flow,
8
+ * then redirects to our local callback with the token.
9
+ */
10
+ import { promises as fs } from 'node:fs';
11
+ import path from 'node:path';
12
+ import http from 'node:http';
13
+ import { URL } from 'node:url';
14
+ import { PuterAccountsStorageSchema } from './types.js';
15
+ const DEFAULT_CALLBACK_PORT = 19847;
16
+ const AUTH_TIMEOUT_MS = 300000; // 5 minutes
17
+ /**
18
+ * HTML page that handles Puter popup auth flow
19
+ * This page loads the Puter SDK, triggers signIn(), and redirects to our callback
20
+ */
21
+ const getAuthHtml = (callbackUrl) => `
22
+ <!DOCTYPE html>
23
+ <html lang="en">
24
+ <head>
25
+ <meta charset="UTF-8">
26
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
27
+ <title>Puter Authentication - OpenCode</title>
28
+ <script src="https://js.puter.com/v2/"></script>
29
+ <style>
30
+ * { margin: 0; padding: 0; box-sizing: border-box; }
31
+ body {
32
+ font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
33
+ min-height: 100vh;
34
+ display: flex;
35
+ justify-content: center;
36
+ align-items: center;
37
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
38
+ }
39
+ .card {
40
+ background: white;
41
+ padding: 48px;
42
+ border-radius: 16px;
43
+ text-align: center;
44
+ box-shadow: 0 20px 60px rgba(0,0,0,0.3);
45
+ max-width: 420px;
46
+ width: 90%;
47
+ }
48
+ .logo { font-size: 48px; margin-bottom: 16px; }
49
+ h1 { color: #333; margin-bottom: 8px; font-size: 24px; }
50
+ .subtitle { color: #666; margin-bottom: 24px; font-size: 14px; }
51
+ .status {
52
+ padding: 16px;
53
+ border-radius: 8px;
54
+ margin-bottom: 16px;
55
+ font-size: 14px;
56
+ }
57
+ .status.loading { background: #e3f2fd; color: #1565c0; }
58
+ .status.success { background: #e8f5e9; color: #2e7d32; }
59
+ .status.error { background: #ffebee; color: #c62828; }
60
+ .btn {
61
+ display: inline-block;
62
+ padding: 14px 32px;
63
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
64
+ color: white;
65
+ border: none;
66
+ border-radius: 8px;
67
+ font-size: 16px;
68
+ font-weight: 600;
69
+ cursor: pointer;
70
+ transition: transform 0.2s, box-shadow 0.2s;
71
+ }
72
+ .btn:hover { transform: translateY(-2px); box-shadow: 0 8px 25px rgba(102,126,234,0.4); }
73
+ .btn:disabled { opacity: 0.6; cursor: not-allowed; transform: none; }
74
+ .features {
75
+ margin-top: 24px;
76
+ padding-top: 24px;
77
+ border-top: 1px solid #eee;
78
+ text-align: left;
79
+ }
80
+ .feature {
81
+ display: flex;
82
+ align-items: center;
83
+ gap: 8px;
84
+ margin-bottom: 8px;
85
+ font-size: 13px;
86
+ color: #666;
87
+ }
88
+ .feature-icon { color: #22c55e; }
89
+ .hidden { display: none; }
90
+ </style>
91
+ </head>
92
+ <body>
93
+ <div class="card">
94
+ <div class="logo">🟣</div>
95
+ <h1>Connect to Puter</h1>
96
+ <p class="subtitle">Get FREE unlimited access to Claude, GPT-5, Gemini & 500+ AI models</p>
97
+
98
+ <div id="status" class="status loading">Initializing Puter SDK...</div>
99
+
100
+ <button id="signInBtn" class="btn hidden" onclick="signIn()">
101
+ Sign in with Puter
102
+ </button>
103
+
104
+ <div class="features">
105
+ <div class="feature"><span class="feature-icon">✓</span> Claude Opus 4.5 & Sonnet 4.5</div>
106
+ <div class="feature"><span class="feature-icon">✓</span> GPT-5.2 & o3-mini</div>
107
+ <div class="feature"><span class="feature-icon">✓</span> Gemini 2.5 Pro (1M context)</div>
108
+ <div class="feature"><span class="feature-icon">✓</span> No rate limits, no API keys</div>
109
+ </div>
110
+ </div>
111
+
112
+ <script>
113
+ const callbackUrl = ${JSON.stringify(callbackUrl)};
114
+ const statusEl = document.getElementById('status');
115
+ const signInBtn = document.getElementById('signInBtn');
116
+
117
+ function setStatus(message, type = 'loading') {
118
+ statusEl.textContent = message;
119
+ statusEl.className = 'status ' + type;
120
+ }
121
+
122
+ // Check if already signed in
123
+ async function checkAuth() {
124
+ try {
125
+ if (puter.auth.isSignedIn()) {
126
+ setStatus('Already signed in, getting token...', 'loading');
127
+ // Already signed in, get user info and redirect
128
+ const user = await puter.auth.getUser();
129
+ // We need to trigger signIn again to get a fresh token
130
+ await triggerSignIn();
131
+ } else {
132
+ setStatus('Click the button below to sign in', 'loading');
133
+ signInBtn.classList.remove('hidden');
134
+ }
135
+ } catch (err) {
136
+ setStatus('Click the button below to sign in', 'loading');
137
+ signInBtn.classList.remove('hidden');
138
+ }
139
+ }
140
+
141
+ async function triggerSignIn() {
142
+ try {
143
+ setStatus('Opening Puter sign-in popup...', 'loading');
144
+ signInBtn.disabled = true;
145
+
146
+ const result = await puter.auth.signIn();
147
+
148
+ if (result.success && result.token) {
149
+ setStatus('Success! Redirecting to OpenCode...', 'success');
150
+
151
+ // Get username from user info
152
+ let username = result.username || 'puter_user';
153
+ try {
154
+ const user = await puter.auth.getUser();
155
+ username = user.username || username;
156
+ } catch (e) {}
157
+
158
+ // Redirect to callback with token
159
+ const params = new URLSearchParams({
160
+ token: result.token,
161
+ username: username,
162
+ success: 'true'
163
+ });
164
+
165
+ window.location.href = callbackUrl + '?' + params.toString();
166
+ } else {
167
+ setStatus('Sign-in was not completed. ' + (result.error || ''), 'error');
168
+ signInBtn.disabled = false;
169
+ }
170
+ } catch (err) {
171
+ setStatus('Error: ' + (err.message || 'Sign-in failed'), 'error');
172
+ signInBtn.disabled = false;
173
+ }
174
+ }
175
+
176
+ function signIn() {
177
+ triggerSignIn();
178
+ }
179
+
180
+ // Initialize on load
181
+ setTimeout(checkAuth, 500);
182
+ </script>
183
+ </body>
184
+ </html>
185
+ `;
186
+ export class PuterAuthManager {
187
+ configDir;
188
+ accountsFile;
189
+ storage = null;
190
+ constructor(configDir, _config = {}) {
191
+ this.configDir = configDir;
192
+ this.accountsFile = path.join(configDir, 'puter-accounts.json');
193
+ }
194
+ /**
195
+ * Initialize the auth manager and load existing accounts
196
+ */
197
+ async init() {
198
+ await this.ensureConfigDir();
199
+ await this.loadAccounts();
200
+ }
201
+ /**
202
+ * Ensure the config directory exists
203
+ */
204
+ async ensureConfigDir() {
205
+ try {
206
+ await fs.mkdir(this.configDir, { recursive: true });
207
+ }
208
+ catch {
209
+ // Directory exists
210
+ }
211
+ }
212
+ /**
213
+ * Load accounts from disk
214
+ */
215
+ async loadAccounts() {
216
+ try {
217
+ const data = await fs.readFile(this.accountsFile, 'utf-8');
218
+ const parsed = JSON.parse(data);
219
+ this.storage = PuterAccountsStorageSchema.parse(parsed);
220
+ }
221
+ catch {
222
+ // No accounts file or invalid - start fresh
223
+ this.storage = {
224
+ version: 1,
225
+ accounts: [],
226
+ activeIndex: 0,
227
+ };
228
+ }
229
+ }
230
+ /**
231
+ * Save accounts to disk
232
+ */
233
+ async saveAccounts() {
234
+ if (!this.storage)
235
+ return;
236
+ const data = JSON.stringify(this.storage, null, 2);
237
+ await fs.writeFile(this.accountsFile, data, 'utf-8');
238
+ }
239
+ /**
240
+ * Get the active account
241
+ */
242
+ getActiveAccount() {
243
+ if (!this.storage || this.storage.accounts.length === 0) {
244
+ return null;
245
+ }
246
+ return this.storage.accounts[this.storage.activeIndex] || null;
247
+ }
248
+ /**
249
+ * Get all accounts
250
+ */
251
+ getAllAccounts() {
252
+ return this.storage?.accounts || [];
253
+ }
254
+ /**
255
+ * Check if we have any authenticated accounts
256
+ */
257
+ isAuthenticated() {
258
+ return this.getActiveAccount() !== null;
259
+ }
260
+ /**
261
+ * Start the OAuth flow in browser
262
+ *
263
+ * This serves an HTML page that handles the Puter popup auth flow,
264
+ * then redirects to our local callback with the token.
265
+ */
266
+ async login() {
267
+ return new Promise((resolve) => {
268
+ const port = DEFAULT_CALLBACK_PORT;
269
+ let resolved = false;
270
+ // Create callback server
271
+ const server = http.createServer(async (req, res) => {
272
+ if (resolved) {
273
+ res.writeHead(200, { 'Content-Type': 'text/html' });
274
+ res.end('<html><body><h1>Already authenticated</h1></body></html>');
275
+ return;
276
+ }
277
+ const url = new URL(req.url || '/', `http://localhost:${port}`);
278
+ if (url.pathname === '/callback') {
279
+ const token = url.searchParams.get('token');
280
+ const username = url.searchParams.get('username') || 'puter_user';
281
+ const success = url.searchParams.get('success') === 'true';
282
+ if (token && success) {
283
+ resolved = true;
284
+ const account = {
285
+ username,
286
+ authToken: token,
287
+ addedAt: Date.now(),
288
+ isTemporary: false,
289
+ };
290
+ // Add account to storage
291
+ await this.addAccount(account);
292
+ res.writeHead(200, { 'Content-Type': 'text/html' });
293
+ res.end(`
294
+ <!DOCTYPE html>
295
+ <html>
296
+ <head>
297
+ <title>Puter Auth Success</title>
298
+ <style>
299
+ body { font-family: system-ui, sans-serif; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); }
300
+ .card { background: white; padding: 40px; border-radius: 16px; text-align: center; box-shadow: 0 10px 40px rgba(0,0,0,0.2); }
301
+ h1 { color: #22c55e; margin: 0 0 16px 0; }
302
+ p { color: #666; margin: 0; }
303
+ .emoji { font-size: 48px; margin-bottom: 16px; }
304
+ </style>
305
+ </head>
306
+ <body>
307
+ <div class="card">
308
+ <div class="emoji">✅</div>
309
+ <h1>Authentication Successful!</h1>
310
+ <p>You can close this window and return to OpenCode.</p>
311
+ <p style="margin-top: 16px; font-size: 14px; color: #888;">Logged in as: ${username}</p>
312
+ </div>
313
+ <script>
314
+ // Try to close the window after a short delay
315
+ setTimeout(() => { try { window.close(); } catch(e) {} }, 2000);
316
+ </script>
317
+ </body>
318
+ </html>
319
+ `);
320
+ server.close();
321
+ resolve({ success: true, account });
322
+ }
323
+ else {
324
+ res.writeHead(400, { 'Content-Type': 'text/html' });
325
+ res.end('<html><body><h1>Missing token or auth failed</h1></body></html>');
326
+ }
327
+ }
328
+ else if (url.pathname === '/') {
329
+ // Serve the auth HTML page
330
+ const callbackUrl = `http://localhost:${port}/callback`;
331
+ res.writeHead(200, { 'Content-Type': 'text/html' });
332
+ res.end(getAuthHtml(callbackUrl));
333
+ }
334
+ else {
335
+ res.writeHead(404);
336
+ res.end('Not found');
337
+ }
338
+ });
339
+ server.listen(port, 'localhost', async () => {
340
+ console.log(`\n🔐 Opening browser for Puter authentication...`);
341
+ console.log(` If browser doesn't open, visit: http://localhost:${port}\n`);
342
+ // Open browser
343
+ try {
344
+ const open = await import('open');
345
+ await open.default(`http://localhost:${port}`);
346
+ }
347
+ catch {
348
+ console.log(` Please open http://localhost:${port} in your browser`);
349
+ }
350
+ });
351
+ // Timeout
352
+ setTimeout(() => {
353
+ if (!resolved) {
354
+ resolved = true;
355
+ server.close();
356
+ resolve({ success: false, error: 'Authentication timeout' });
357
+ }
358
+ }, AUTH_TIMEOUT_MS);
359
+ // Handle server errors
360
+ server.on('error', (err) => {
361
+ if (!resolved) {
362
+ resolved = true;
363
+ resolve({ success: false, error: `Server error: ${err.message}` });
364
+ }
365
+ });
366
+ });
367
+ }
368
+ /**
369
+ * Add a new account (or update existing)
370
+ */
371
+ async addAccount(account) {
372
+ if (!this.storage) {
373
+ this.storage = { version: 1, accounts: [], activeIndex: 0 };
374
+ }
375
+ // Check if account already exists
376
+ const existingIndex = this.storage.accounts.findIndex(a => a.username === account.username);
377
+ if (existingIndex >= 0) {
378
+ // Update existing
379
+ this.storage.accounts[existingIndex] = account;
380
+ this.storage.activeIndex = existingIndex;
381
+ }
382
+ else {
383
+ // Add new
384
+ this.storage.accounts.push(account);
385
+ this.storage.activeIndex = this.storage.accounts.length - 1;
386
+ }
387
+ await this.saveAccounts();
388
+ }
389
+ /**
390
+ * Switch to a different account
391
+ */
392
+ async switchAccount(index) {
393
+ if (!this.storage || index < 0 || index >= this.storage.accounts.length) {
394
+ return false;
395
+ }
396
+ this.storage.activeIndex = index;
397
+ await this.saveAccounts();
398
+ return true;
399
+ }
400
+ /**
401
+ * Remove an account
402
+ */
403
+ async removeAccount(index) {
404
+ if (!this.storage || index < 0 || index >= this.storage.accounts.length) {
405
+ return false;
406
+ }
407
+ this.storage.accounts.splice(index, 1);
408
+ if (this.storage.activeIndex >= this.storage.accounts.length) {
409
+ this.storage.activeIndex = Math.max(0, this.storage.accounts.length - 1);
410
+ }
411
+ await this.saveAccounts();
412
+ return true;
413
+ }
414
+ /**
415
+ * Update the last used timestamp for the active account
416
+ */
417
+ async touchActiveAccount() {
418
+ const account = this.getActiveAccount();
419
+ if (account && this.storage) {
420
+ account.lastUsed = Date.now();
421
+ await this.saveAccounts();
422
+ }
423
+ }
424
+ /**
425
+ * Logout - remove all accounts
426
+ */
427
+ async logout() {
428
+ this.storage = {
429
+ version: 1,
430
+ accounts: [],
431
+ activeIndex: 0,
432
+ };
433
+ await this.saveAccounts();
434
+ }
435
+ }
436
+ //# sourceMappingURL=auth.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth.js","sourceRoot":"","sources":["../src/auth.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,QAAQ,IAAI,EAAE,EAAE,MAAM,SAAS,CAAC;AACzC,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAO/B,OAAO,EAAE,0BAA0B,EAAE,MAAM,YAAY,CAAC;AAExD,MAAM,qBAAqB,GAAG,KAAK,CAAC;AACpC,MAAM,eAAe,GAAG,MAAM,CAAC,CAAC,YAAY;AAE5C;;;GAGG;AACH,MAAM,WAAW,GAAG,CAAC,WAAmB,EAAE,EAAE,CAAC;;;;;;;;;;;;;;;;;;;;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,CAAC,YAAY,EAAE,CAAC;QAC5B,CAAC;IACH,CAAC;IAED;;OAEG;IACI,KAAK,CAAC,MAAM;QACjB,IAAI,CAAC,OAAO,GAAG;YACb,OAAO,EAAE,CAAC;YACV,QAAQ,EAAE,EAAE;YACZ,WAAW,EAAE,CAAC;SACf,CAAC;QACF,MAAM,IAAI,CAAC,YAAY,EAAE,CAAC;IAC5B,CAAC;CACF"}
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Puter API Client
3
+ *
4
+ * Handles all communication with Puter.com's AI API
5
+ */
6
+ import type { PuterChatMessage, PuterChatOptions, PuterChatResponse, PuterChatStreamChunk, PuterModelInfo, PuterConfig } from './types.js';
7
+ export declare class PuterClient {
8
+ private authToken;
9
+ private config;
10
+ constructor(authToken: string, config?: Partial<PuterConfig>);
11
+ /**
12
+ * Get the API base URL
13
+ */
14
+ private get apiUrl();
15
+ /**
16
+ * Get the request timeout
17
+ */
18
+ private get timeout();
19
+ /**
20
+ * Update the auth token
21
+ */
22
+ setAuthToken(token: string): void;
23
+ /**
24
+ * Send a chat completion request (non-streaming)
25
+ */
26
+ chat(messages: PuterChatMessage[], options?: PuterChatOptions): Promise<PuterChatResponse>;
27
+ /**
28
+ * Send a streaming chat completion request
29
+ */
30
+ chatStream(messages: PuterChatMessage[], options?: PuterChatOptions): AsyncGenerator<PuterChatStreamChunk>;
31
+ /**
32
+ * List available models
33
+ */
34
+ listModels(): Promise<PuterModelInfo[]>;
35
+ /**
36
+ * Get default model list (fallback)
37
+ */
38
+ private getDefaultModels;
39
+ /**
40
+ * Make a generic API request to the drivers endpoint
41
+ */
42
+ private makeRequest;
43
+ /**
44
+ * Test the connection and auth token
45
+ */
46
+ testConnection(): Promise<boolean>;
47
+ }
48
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EACV,gBAAgB,EAChB,gBAAgB,EAChB,iBAAiB,EACjB,oBAAoB,EACpB,cAAc,EACd,WAAW,EACZ,MAAM,YAAY,CAAC;AAKpB,qBAAa,WAAW;IACtB,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,MAAM,CAAuB;gBAEzB,SAAS,EAAE,MAAM,EAAE,MAAM,GAAE,OAAO,CAAC,WAAW,CAAM;IAKhE;;OAEG;IACH,OAAO,KAAK,MAAM,GAEjB;IAED;;OAEG;IACH,OAAO,KAAK,OAAO,GAElB;IAED;;OAEG;IACI,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAIxC;;OAEG;IACU,IAAI,CACf,QAAQ,EAAE,gBAAgB,EAAE,EAC5B,OAAO,GAAE,gBAAqB,GAC7B,OAAO,CAAC,iBAAiB,CAAC;IAa7B;;OAEG;IACW,UAAU,CACtB,QAAQ,EAAE,gBAAgB,EAAE,EAC5B,OAAO,GAAE,gBAAqB,GAC7B,cAAc,CAAC,oBAAoB,CAAC;IA+EvC;;OAEG;IACU,UAAU,IAAI,OAAO,CAAC,cAAc,EAAE,CAAC;IAqBpD;;OAEG;IACH,OAAO,CAAC,gBAAgB;IAoBxB;;OAEG;YACW,WAAW;IAmCzB;;OAEG;IACU,cAAc,IAAI,OAAO,CAAC,OAAO,CAAC;CAWhD"}