sam-coder-cli 1.0.34 → 1.0.35

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/bin/ai-team.js CHANGED
@@ -80,14 +80,32 @@ class AITeam {
80
80
  }
81
81
 
82
82
  async start(sessionId = null) {
83
- if (sessionId) {
84
- console.log(`Joining session ${sessionId}...`);
85
- await this.client.joinSession(sessionId);
86
- } else {
87
- console.log('Creating new session...');
88
- const newSessionId = await this.client.createSession();
89
- console.log(`Created new session: ${newSessionId}`);
90
- console.log('Share this ID with others to collaborate!');
83
+ let serverId = null;
84
+ let actualSessionId = sessionId;
85
+
86
+ // Check if sessionId contains server information (format: serverId:sessionId)
87
+ if (sessionId && sessionId.includes(':')) {
88
+ [serverId, actualSessionId] = sessionId.split(':');
89
+ console.log(chalk.blue(`Attempting to connect to server: ${serverId}`));
90
+ }
91
+
92
+ // Connect to the server first
93
+ try {
94
+ await this.client.connect(serverId);
95
+
96
+ if (actualSessionId) {
97
+ console.log(`Joining session ${actualSessionId}...`);
98
+ await this.client.joinSession(actualSessionId);
99
+ } else {
100
+ console.log('Creating new session...');
101
+ const newSessionId = await this.client.createSession();
102
+ console.log(chalk.green(`✅ Created new session: ${newSessionId}`));
103
+ console.log('Share this ID with others to collaborate!');
104
+ console.log(chalk.yellow(`To join this session, use: ${serverId || 'localhost:8080'}:${newSessionId}`));
105
+ }
106
+ } catch (error) {
107
+ console.error(chalk.red('Failed to connect to server:'), error.message);
108
+ process.exit(1);
91
109
  }
92
110
 
93
111
  // Start chat input loop
@@ -34,7 +34,32 @@ async function findAvailablePort(startPort = 8080, maxAttempts = 10) {
34
34
  throw new Error(`Could not find an available port after ${maxAttempts} attempts`);
35
35
  }
36
36
 
37
- async function ensureServerRunning(port = 8080) {
37
+ async function ensureServerRunning(port = 8080, serverId = null) {
38
+ // First try to connect to an existing server by ID if provided
39
+ if (serverId) {
40
+ try {
41
+ console.log(chalk.blue(`Attempting to connect to existing server ${serverId}...`));
42
+ // Try to connect to the server
43
+ const ws = new WebSocket(`ws://${serverId}`);
44
+
45
+ // Wait for connection or timeout
46
+ const connectionResult = await Promise.race([
47
+ new Promise(resolve => ws.once('open', () => resolve(true))),
48
+ new Promise(resolve => setTimeout(() => resolve(false), 2000))
49
+ ]);
50
+
51
+ if (connectionResult) {
52
+ console.log(chalk.green(`✅ Connected to existing server at ${serverId}`));
53
+ ws.terminate();
54
+ return `ws://${serverId}`;
55
+ }
56
+ ws.terminate();
57
+ } catch (error) {
58
+ console.log(chalk.yellow(`Could not connect to server ${serverId}, starting a new server...`));
59
+ }
60
+ }
61
+
62
+ // If no server ID provided or connection failed, start a new server
38
63
  try {
39
64
  const availablePort = await findAvailablePort(port);
40
65
  const isDefaultPort = (availablePort === port);
@@ -271,14 +296,14 @@ class MultiplayerClient extends EventEmitter {
271
296
  this.completeTask = this.completeTask.bind(this);
272
297
  }
273
298
 
274
- async connect() {
299
+ async connect(serverId = null) {
275
300
  return new Promise((resolve, reject) => {
276
301
  const connectWithRetry = async () => {
277
302
  try {
278
- // If no server URL is provided, try to start a local server
303
+ // If no server URL is provided, try to start a local server or connect to the specified server ID
279
304
  if (this.serverUrl === 'ws://localhost:8080') {
280
305
  try {
281
- this.serverUrl = await ensureServerRunning(8080);
306
+ this.serverUrl = await ensureServerRunning(8080, serverId);
282
307
  } catch (error) {
283
308
  console.error(chalk.red('Failed to start local server:'), error.message);
284
309
  reject(error);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sam-coder-cli",
3
- "version": "1.0.34",
3
+ "version": "1.0.35",
4
4
  "description": "SAM-CODER: An animated command-line AI assistant with agency capabilities.",
5
5
  "main": "bin/agi-cli.js",
6
6
  "bin": {