happy-coder 0.9.1 → 0.10.0-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/lib.cjs CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var types = require('./types-DNUk09Np.cjs');
3
+ var types = require('./types-D9P2bndj.cjs');
4
4
  require('axios');
5
5
  require('chalk');
6
6
  require('fs');
@@ -13,6 +13,12 @@ require('node:crypto');
13
13
  require('tweetnacl');
14
14
  require('node:events');
15
15
  require('socket.io-client');
16
+ require('child_process');
17
+ require('util');
18
+ require('fs/promises');
19
+ require('crypto');
20
+ require('path');
21
+ require('url');
16
22
  require('expo-server-sdk');
17
23
 
18
24
 
package/dist/lib.d.cts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { z } from 'zod';
2
2
  import { EventEmitter } from 'node:events';
3
- import { ChildProcess } from 'child_process';
3
+ import { Socket } from 'socket.io-client';
4
4
  import { ExpoPushMessage } from 'expo-server-sdk';
5
5
 
6
6
  /**
@@ -288,7 +288,80 @@ declare const RawJSONLinesSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
288
288
  }, z.ZodTypeAny, "passthrough">>]>;
289
289
  type RawJSONLines = z.infer<typeof RawJSONLinesSchema>;
290
290
 
291
- type RpcHandler<T = any, R = any> = (data: T) => R | Promise<R>;
291
+ /**
292
+ * Common RPC types and interfaces for both session and machine clients
293
+ */
294
+ /**
295
+ * Generic RPC handler function type
296
+ * @template TRequest - The request data type
297
+ * @template TResponse - The response data type
298
+ */
299
+ type RpcHandler<TRequest = any, TResponse = any> = (data: TRequest) => TResponse | Promise<TResponse>;
300
+ /**
301
+ * RPC request data from server
302
+ */
303
+ interface RpcRequest {
304
+ method: string;
305
+ params: string;
306
+ }
307
+ /**
308
+ * Configuration for RPC handler manager
309
+ */
310
+ interface RpcHandlerConfig {
311
+ /** Prefix to add to all method names (e.g., sessionId or machineId) */
312
+ scopePrefix: string;
313
+ /** Secret key for encryption/decryption */
314
+ secret: Uint8Array;
315
+ /** Logger function for debugging */
316
+ logger?: (message: string, data?: any) => void;
317
+ }
318
+
319
+ /**
320
+ * Generic RPC handler manager for session and machine clients
321
+ * Manages RPC method registration, encryption/decryption, and handler execution
322
+ */
323
+
324
+ declare class RpcHandlerManager {
325
+ private handlers;
326
+ private readonly scopePrefix;
327
+ private readonly secret;
328
+ private readonly logger;
329
+ private socket;
330
+ constructor(config: RpcHandlerConfig);
331
+ /**
332
+ * Register an RPC handler for a specific method
333
+ * @param method - The method name (without prefix)
334
+ * @param handler - The handler function
335
+ */
336
+ registerHandler<TRequest = any, TResponse = any>(method: string, handler: RpcHandler<TRequest, TResponse>): void;
337
+ /**
338
+ * Handle an incoming RPC request
339
+ * @param request - The RPC request data
340
+ * @param callback - The response callback
341
+ */
342
+ handleRequest(request: RpcRequest): Promise<any>;
343
+ onSocketConnect(socket: Socket): void;
344
+ onSocketDisconnect(): void;
345
+ /**
346
+ * Get the number of registered handlers
347
+ */
348
+ getHandlerCount(): number;
349
+ /**
350
+ * Check if a handler is registered
351
+ * @param method - The method name (without prefix)
352
+ */
353
+ hasHandler(method: string): boolean;
354
+ /**
355
+ * Clear all handlers
356
+ */
357
+ clearHandlers(): void;
358
+ /**
359
+ * Get the prefixed method name
360
+ * @param method - The method name
361
+ */
362
+ private getPrefixedMethod;
363
+ }
364
+
292
365
  declare class ApiSessionClient extends EventEmitter {
293
366
  private readonly token;
294
367
  private readonly secret;
@@ -300,7 +373,7 @@ declare class ApiSessionClient extends EventEmitter {
300
373
  private socket;
301
374
  private pendingMessages;
302
375
  private pendingMessageCallback;
303
- private rpcHandlers;
376
+ readonly rpcHandlerManager: RpcHandlerManager;
304
377
  private agentStateLock;
305
378
  private metadataLock;
306
379
  constructor(token: string, secret: Uint8Array, session: Session);
@@ -344,16 +417,6 @@ declare class ApiSessionClient extends EventEmitter {
344
417
  * @param handler - Handler function that returns the updated agent state
345
418
  */
346
419
  updateAgentState(handler: (metadata: AgentState) => AgentState): void;
347
- /**
348
- * Set a custom RPC handler for a specific method with encrypted arguments and responses
349
- * @param method - The method name to handle
350
- * @param handler - The handler function to call when the method is invoked
351
- */
352
- setHandler<T = any, R = any>(method: string, handler: RpcHandler<T, R>): void;
353
- /**
354
- * Re-register all RPC handlers after reconnection
355
- */
356
- private reregisterHandlers;
357
420
  /**
358
421
  * Wait for socket buffer to flush
359
422
  */
@@ -643,23 +706,22 @@ type AgentState = {
643
706
  };
644
707
  };
645
708
 
646
- /**
647
- * Daemon-specific types (not related to API/server communication)
648
- */
649
-
650
- /**
651
- * Session tracking for daemon
652
- */
653
- interface TrackedSession {
654
- startedBy: 'daemon' | string;
655
- happySessionId?: string;
656
- happySessionMetadataFromLocalWebhook?: Metadata;
657
- pid: number;
658
- childProcess?: ChildProcess;
659
- error?: string;
660
- directoryCreated?: boolean;
661
- message?: string;
709
+ interface SpawnSessionOptions {
710
+ machineId?: string;
711
+ directory: string;
712
+ sessionId?: string;
713
+ approvedNewDirectoryCreation?: boolean;
662
714
  }
715
+ type SpawnSessionResult = {
716
+ type: 'success';
717
+ sessionId: string;
718
+ } | {
719
+ type: 'requestToApproveDirectoryCreation';
720
+ directory: string;
721
+ } | {
722
+ type: 'error';
723
+ errorMessage: string;
724
+ };
663
725
 
664
726
  /**
665
727
  * WebSocket client for machine/daemon communication with Happy server
@@ -667,7 +729,7 @@ interface TrackedSession {
667
729
  */
668
730
 
669
731
  type MachineRpcHandlers = {
670
- spawnSession: (directory: string, sessionId?: string) => Promise<TrackedSession | null>;
732
+ spawnSession: (options: SpawnSessionOptions) => Promise<SpawnSessionResult>;
671
733
  stopSession: (sessionId: string) => boolean;
672
734
  requestShutdown: () => void;
673
735
  };
@@ -677,9 +739,7 @@ declare class ApiMachineClient {
677
739
  private machine;
678
740
  private socket;
679
741
  private keepAliveInterval;
680
- private spawnSession?;
681
- private stopSession?;
682
- private requestShutdown?;
742
+ private rpcHandlerManager;
683
743
  constructor(token: string, secret: Uint8Array, machine: Machine);
684
744
  setRPCHandlers({ spawnSession, stopSession, requestShutdown }: MachineRpcHandlers): void;
685
745
  /**
@@ -750,7 +810,7 @@ declare class ApiClient {
750
810
  * Register or update machine with the server
751
811
  * Returns the current machine state from the server with decrypted metadata and daemonState
752
812
  */
753
- createMachineOrGetExistingAsIs(opts: {
813
+ getOrCreateMachine(opts: {
754
814
  machineId: string;
755
815
  metadata: MachineMetadata;
756
816
  daemonState?: DaemonState;
@@ -758,6 +818,11 @@ declare class ApiClient {
758
818
  sessionSyncClient(session: Session): ApiSessionClient;
759
819
  machineSyncClient(machine: Machine): ApiMachineClient;
760
820
  push(): PushNotificationClient;
821
+ /**
822
+ * Register a vendor API token with the server
823
+ * The token is sent as a JSON string - server handles encryption
824
+ */
825
+ registerVendorToken(vendor: 'openai' | 'anthropic' | 'gemini', apiKey: any): Promise<void>;
761
826
  }
762
827
 
763
828
  /**
package/dist/lib.d.mts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { z } from 'zod';
2
2
  import { EventEmitter } from 'node:events';
3
- import { ChildProcess } from 'child_process';
3
+ import { Socket } from 'socket.io-client';
4
4
  import { ExpoPushMessage } from 'expo-server-sdk';
5
5
 
6
6
  /**
@@ -288,7 +288,80 @@ declare const RawJSONLinesSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
288
288
  }, z.ZodTypeAny, "passthrough">>]>;
289
289
  type RawJSONLines = z.infer<typeof RawJSONLinesSchema>;
290
290
 
291
- type RpcHandler<T = any, R = any> = (data: T) => R | Promise<R>;
291
+ /**
292
+ * Common RPC types and interfaces for both session and machine clients
293
+ */
294
+ /**
295
+ * Generic RPC handler function type
296
+ * @template TRequest - The request data type
297
+ * @template TResponse - The response data type
298
+ */
299
+ type RpcHandler<TRequest = any, TResponse = any> = (data: TRequest) => TResponse | Promise<TResponse>;
300
+ /**
301
+ * RPC request data from server
302
+ */
303
+ interface RpcRequest {
304
+ method: string;
305
+ params: string;
306
+ }
307
+ /**
308
+ * Configuration for RPC handler manager
309
+ */
310
+ interface RpcHandlerConfig {
311
+ /** Prefix to add to all method names (e.g., sessionId or machineId) */
312
+ scopePrefix: string;
313
+ /** Secret key for encryption/decryption */
314
+ secret: Uint8Array;
315
+ /** Logger function for debugging */
316
+ logger?: (message: string, data?: any) => void;
317
+ }
318
+
319
+ /**
320
+ * Generic RPC handler manager for session and machine clients
321
+ * Manages RPC method registration, encryption/decryption, and handler execution
322
+ */
323
+
324
+ declare class RpcHandlerManager {
325
+ private handlers;
326
+ private readonly scopePrefix;
327
+ private readonly secret;
328
+ private readonly logger;
329
+ private socket;
330
+ constructor(config: RpcHandlerConfig);
331
+ /**
332
+ * Register an RPC handler for a specific method
333
+ * @param method - The method name (without prefix)
334
+ * @param handler - The handler function
335
+ */
336
+ registerHandler<TRequest = any, TResponse = any>(method: string, handler: RpcHandler<TRequest, TResponse>): void;
337
+ /**
338
+ * Handle an incoming RPC request
339
+ * @param request - The RPC request data
340
+ * @param callback - The response callback
341
+ */
342
+ handleRequest(request: RpcRequest): Promise<any>;
343
+ onSocketConnect(socket: Socket): void;
344
+ onSocketDisconnect(): void;
345
+ /**
346
+ * Get the number of registered handlers
347
+ */
348
+ getHandlerCount(): number;
349
+ /**
350
+ * Check if a handler is registered
351
+ * @param method - The method name (without prefix)
352
+ */
353
+ hasHandler(method: string): boolean;
354
+ /**
355
+ * Clear all handlers
356
+ */
357
+ clearHandlers(): void;
358
+ /**
359
+ * Get the prefixed method name
360
+ * @param method - The method name
361
+ */
362
+ private getPrefixedMethod;
363
+ }
364
+
292
365
  declare class ApiSessionClient extends EventEmitter {
293
366
  private readonly token;
294
367
  private readonly secret;
@@ -300,7 +373,7 @@ declare class ApiSessionClient extends EventEmitter {
300
373
  private socket;
301
374
  private pendingMessages;
302
375
  private pendingMessageCallback;
303
- private rpcHandlers;
376
+ readonly rpcHandlerManager: RpcHandlerManager;
304
377
  private agentStateLock;
305
378
  private metadataLock;
306
379
  constructor(token: string, secret: Uint8Array, session: Session);
@@ -344,16 +417,6 @@ declare class ApiSessionClient extends EventEmitter {
344
417
  * @param handler - Handler function that returns the updated agent state
345
418
  */
346
419
  updateAgentState(handler: (metadata: AgentState) => AgentState): void;
347
- /**
348
- * Set a custom RPC handler for a specific method with encrypted arguments and responses
349
- * @param method - The method name to handle
350
- * @param handler - The handler function to call when the method is invoked
351
- */
352
- setHandler<T = any, R = any>(method: string, handler: RpcHandler<T, R>): void;
353
- /**
354
- * Re-register all RPC handlers after reconnection
355
- */
356
- private reregisterHandlers;
357
420
  /**
358
421
  * Wait for socket buffer to flush
359
422
  */
@@ -643,23 +706,22 @@ type AgentState = {
643
706
  };
644
707
  };
645
708
 
646
- /**
647
- * Daemon-specific types (not related to API/server communication)
648
- */
649
-
650
- /**
651
- * Session tracking for daemon
652
- */
653
- interface TrackedSession {
654
- startedBy: 'daemon' | string;
655
- happySessionId?: string;
656
- happySessionMetadataFromLocalWebhook?: Metadata;
657
- pid: number;
658
- childProcess?: ChildProcess;
659
- error?: string;
660
- directoryCreated?: boolean;
661
- message?: string;
709
+ interface SpawnSessionOptions {
710
+ machineId?: string;
711
+ directory: string;
712
+ sessionId?: string;
713
+ approvedNewDirectoryCreation?: boolean;
662
714
  }
715
+ type SpawnSessionResult = {
716
+ type: 'success';
717
+ sessionId: string;
718
+ } | {
719
+ type: 'requestToApproveDirectoryCreation';
720
+ directory: string;
721
+ } | {
722
+ type: 'error';
723
+ errorMessage: string;
724
+ };
663
725
 
664
726
  /**
665
727
  * WebSocket client for machine/daemon communication with Happy server
@@ -667,7 +729,7 @@ interface TrackedSession {
667
729
  */
668
730
 
669
731
  type MachineRpcHandlers = {
670
- spawnSession: (directory: string, sessionId?: string) => Promise<TrackedSession | null>;
732
+ spawnSession: (options: SpawnSessionOptions) => Promise<SpawnSessionResult>;
671
733
  stopSession: (sessionId: string) => boolean;
672
734
  requestShutdown: () => void;
673
735
  };
@@ -677,9 +739,7 @@ declare class ApiMachineClient {
677
739
  private machine;
678
740
  private socket;
679
741
  private keepAliveInterval;
680
- private spawnSession?;
681
- private stopSession?;
682
- private requestShutdown?;
742
+ private rpcHandlerManager;
683
743
  constructor(token: string, secret: Uint8Array, machine: Machine);
684
744
  setRPCHandlers({ spawnSession, stopSession, requestShutdown }: MachineRpcHandlers): void;
685
745
  /**
@@ -750,7 +810,7 @@ declare class ApiClient {
750
810
  * Register or update machine with the server
751
811
  * Returns the current machine state from the server with decrypted metadata and daemonState
752
812
  */
753
- createMachineOrGetExistingAsIs(opts: {
813
+ getOrCreateMachine(opts: {
754
814
  machineId: string;
755
815
  metadata: MachineMetadata;
756
816
  daemonState?: DaemonState;
@@ -758,6 +818,11 @@ declare class ApiClient {
758
818
  sessionSyncClient(session: Session): ApiSessionClient;
759
819
  machineSyncClient(machine: Machine): ApiMachineClient;
760
820
  push(): PushNotificationClient;
821
+ /**
822
+ * Register a vendor API token with the server
823
+ * The token is sent as a JSON string - server handles encryption
824
+ */
825
+ registerVendorToken(vendor: 'openai' | 'anthropic' | 'gemini', apiKey: any): Promise<void>;
761
826
  }
762
827
 
763
828
  /**
package/dist/lib.mjs CHANGED
@@ -1,4 +1,4 @@
1
- export { A as ApiClient, a as ApiSessionClient, R as RawJSONLinesSchema, c as configuration, l as logger } from './types-BS8Pr3Im.mjs';
1
+ export { A as ApiClient, a as ApiSessionClient, R as RawJSONLinesSchema, c as configuration, l as logger } from './types-BUXwivpV.mjs';
2
2
  import 'axios';
3
3
  import 'chalk';
4
4
  import 'fs';
@@ -11,4 +11,10 @@ import 'node:crypto';
11
11
  import 'tweetnacl';
12
12
  import 'node:events';
13
13
  import 'socket.io-client';
14
+ import 'child_process';
15
+ import 'util';
16
+ import 'fs/promises';
17
+ import 'crypto';
18
+ import 'path';
19
+ import 'url';
14
20
  import 'expo-server-sdk';