summaryrefslogtreecommitdiff
path: root/indra/newview/llscripteditorws.cpp
blob: 503624d15ab9a49637470e69073d9a984ae41d18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
/**
 * @file llscripteditorws.cpp
 *
 * $LicenseInfo:firstyear=2002&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2010, Linden Research, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

/*
 * ============================================================================
 * WEBSOCKET MESSAGE FORMAT SPECIFICATION
 * ============================================================================
 *
 * This WebSocket implementation uses a structured JSON message format for
 * communication between the Second Life viewer and external script editors.
 * All messages are encoded as UTF-8 JSON strings.
 *
 * ## BASE MESSAGE STRUCTURE
 *
 * Every WebSocket message follows this mandatory structure:
 *
 * ```json
 * {
 *   "command": "message_type_identifier",    // REQUIRED: Command/message type
 *   "data": {                                // OPTIONAL: Command-specific payload
 *     // ... command-specific fields ...
 *   },
 *   "timestamp": "2025-01-27T15:30:00Z",    // OPTIONAL: ISO8601 timestamp
 *   "session_id": "uuid-string",             // OPTIONAL: Session identifier
 *   "message_id": "unique-id"                // OPTIONAL: For request/response correlation
 * }
 * ```
 *
 * ## MESSAGE TYPES
 *
 * ### 1. SERVER-TO-CLIENT MESSAGES
 *
 * #### capabilities
 * Sent immediately when client connects to announce server capabilities.
 * ```json
 * {
 *   "command": "capabilities",
 *   "data": {
 *     "server_name": "Second Life Script Editor WebSocket Server",
 *     "server_version": "1.0.0",
 *     "protocol_version": "1.0",
 *     "viewer_name": "Second Life",
 *     "viewer_version": "Second Life Release 6.4.26",
 *     "session_id": "generated-uuid",
 *     "timestamp": "2025-01-27T15:30:00Z",
 *     "capabilities": ["script_editing", "compilation", "metadata", ...],
 *     "supported_languages": ["lsl", "luau"],
 *     "features": {
 *       "live_compilation": true,
 *       "debugging": false,
 *       "breakpoints": false
 *     }
 *   }
 * }
 * ```
 *
 * #### error
 * Sent when message processing fails or protocol violations occur.
 * ```json
 * {
 *   "command": "error",
 *   "data": {
 *     "error_code": "PARSE_ERROR|INVALID_MESSAGE|UNKNOWN_MESSAGE_TYPE",
 *     "error_message": "Human-readable error description"
 *   }
 * }
 * ```
 *
 * ### 2. CLIENT-TO-SERVER MESSAGES
 *
 * #### connect
 * Client handshake message to establish connection and negotiate capabilities.
 * ```json
 * {
 *   "command": "connect",
 *   "data": {
 *     "client_name": "VS Code LSL Extension",
 *     "client_version": "1.2.3",
 *     "protocol_version": "1.0",
 *     "capabilities": ["script_editing", "syntax_highlighting"],
 *     "supported_languages": ["lsl", "luau"],
 *     "features": {
 *       "auto_completion": true,
 *       "live_preview": false
 *     },
 *     "session_id": "optional-client-provided-uuid"
 *   }
 * }
 * ```
 *
 * ### 3. BIDIRECTIONAL MESSAGES
 *
 * #### connect_ack
 * Server response to client connect message.
 * ```json
 * {
 *   "command": "connect_ack",
 *   "data": {
 *     "status": "connected|rejected",
 *     "message": "Connection established successfully",
 *     "session_id": "established-session-uuid",
 *     "timestamp": "2025-01-27T15:30:00Z",
 *     "mutual_capabilities": ["script_editing", "compilation"],
 *     "supported_languages": ["lsl", "luau"],
 *     "max_script_size": 65536,
 *     "heartbeat_interval": 30,
 *     // For rejected connections:
 *     "error": "incompatible_protocol|missing_capabilities|connection_failed"
 *   }
 * }
 * ```
 *
 * ## FUTURE MESSAGE TYPES (Planned)
 *
 * The following message types are referenced in the protocol design but not
 * yet implemented:
 *
 * - `script_updated`: Editor notifies viewer of script content changes
 * - `save_request`: Editor requests script save to SL servers
 * - `compile_request`: Editor requests script compilation
 * - `script_content`: Viewer sends full script content to editor
 * - `compile_result`: Viewer sends compilation results to editor
 * - `save_result`: Viewer sends save operation results to editor
 * - `metadata`: Script and object metadata exchange
 * - `ping`/`pong`: Connection health check messages
 * - `editor_ready`: Editor initialization complete notification
 *
 * ## PROTOCOL RULES
 *
 * 1. **Encoding**: All messages must be valid UTF-8 JSON
 * 2. **Required Fields**: Every message MUST have a "command" field
 * 3. **Case Sensitivity**: All field names are case-sensitive
 * 4. **Protocol Version**: Currently only "1.0" is supported
 * 5. **Error Handling**: Invalid messages trigger "error" responses
 * 6. **Connection Flow**: Client must send "connect" before other commands
 * 7. **Session Management**: session_id tracks individual editor sessions
 * 8. **Capability Negotiation**: Features limited to mutual capabilities
 *
 * ## ERROR CODES
 *
 * - `PARSE_ERROR`: Invalid JSON syntax
 * - `PARSE_EXCEPTION`: JSON parsing threw exception
 * - `INVALID_MESSAGE`: Missing required "command" field
 * - `UNKNOWN_MESSAGE_TYPE`: Unrecognized command type
 * - `INCOMPATIBLE_PROTOCOL`: Unsupported protocol version
 * - `MISSING_CAPABILITIES`: Required capabilities not supported
 * - `CONNECTION_FAILED`: General connection establishment failure
 *
 * ## IMPLEMENTATION NOTES
 *
 * - Messages are parsed using boost::json and converted to LLSD internally
 * - All timestamps use ISO 8601 format in UTC timezone
 * - UUIDs are generated using LLUUID::generateNewID() for session tracking
 * - Maximum script size is currently limited to 65536 bytes
 * - WebSocket server binds to localhost only for security by default
 * - Protocol designed for extensibility with additional message types
 *
 * ============================================================================
 */


#include "llviewerprecompiledheaders.h"
#include "llscripteditorws.h"
#include "llpreviewscript.h"
#include "llappviewer.h"
#include "lltrans.h"
#include "lldate.h"
#include "llerror.h"
#include "lluuid.h"
#include "llsdjson.h"
#include <boost/json.hpp>

//------------------------------------------------------------------------

LLScriptEditorWSServer::LLScriptEditorWSServer(const std::string_view name, U16 port, bool local_only):
    LLWebsocketMgr::WSServer(name, port, local_only)
{
}

LLWebsocketMgr::WSConnection::ptr_t LLScriptEditorWSServer::connectionFactory(WSServer::ptr_t server, LLWebsocketMgr::connection_h handle)
{
    return std::make_shared<LLScriptEditorWSConnection>(server, handle);
}

//------------------------------------------------------------------------
void LLScriptEditorWSServer::onConnectionOpened(const LLWebsocketMgr::WSConnection::ptr_t& connection)
{
    LL_INFOS("ScriptEditorWS") << "New script editor client connected" << LL_ENDL;

    // Build capabilities message to send to the newly connected external editor
    LLSD capabilities_message;
    capabilities_message["command"] = "capabilities";

    LLSD& data = capabilities_message["data"];

    // Server identification and version information
    data["server_name"] = "Second Life Script Editor WebSocket Server";
    data["server_version"] = "1.0.0";
    data["protocol_version"] = "1.0";

    // Viewer information
    data["viewer_name"] = LLTrans::getString("APP_NAME");
    data["viewer_version"] = LLAppViewer::instance()->getSecondLifeTitle();

    // Session information
    data["session_id"] = LLUUID::generateNewID().asString();
    data["timestamp"] = LLDate::now().asString();

    // Server capabilities - what the viewer/server supports
    LLSD capabilities = LLSD::emptyArray();
    capabilities.append("script_editing");          // Basic script content editing
    capabilities.append("script_synchronization");  // Real-time sync between viewer and editor
    capabilities.append("compilation");             // Compile results with errors/warnings
    capabilities.append("metadata");                // Script and object metadata
    capabilities.append("syntax_highlighting");     // LSL/Luau syntax information
    capabilities.append("error_reporting");         // Detailed error reporting

    data["capabilities"] = capabilities;

    // Language support information
    LLSD languages = LLSD::emptyArray();
    languages.append("lsl");    // Linden Scripting Language
    languages.append("luau");   // Luau scripting language
    data["supported_languages"] = languages;

    // Feature flags
    LLSD features;
    features["live_compilation"] = true;
    features["debugging"] = false;        // Not implemented yet
    features["breakpoints"] = false;      // Not implemented yet
    data["features"] = features;

    // Send the capabilities message to the newly connected client
    if (connection->sendMessage(capabilities_message))
    {
        LL_INFOS("ScriptEditorWS") << "Sent capabilities message to new client" << LL_ENDL;
    }
    else
    {
        LL_WARNS("ScriptEditorWS") << "Failed to send capabilities message to new client" << LL_ENDL;
    }
}

void LLScriptEditorWSServer::onConnectionClosed(const LLWebsocketMgr::WSConnection::ptr_t& connection)
{
    LL_INFOS("ScriptEditorWS") << "Script editor client disconnected" << LL_ENDL;

    // Remove from active connections
    auto script_connection = std::dynamic_pointer_cast<LLScriptEditorWSConnection>(connection);
    if (script_connection)
    {
        mActiveConnections.erase(script_connection);

        // Remove from any script associations
        LL_INFOS("ScriptEditorWS") << "Removed connection from active connections. Total: "
                                   << mActiveConnections.size() << LL_ENDL;
    }
}

//------------------------------------------------------------------------
bool LLScriptEditorWSServer::associateEditor(const LLHandle<LLPanel>& editor_handle, const std::string& script_id)
{
    if (!editor_handle.isDead())
    {
        mScriptEditors[script_id] = editor_handle;
        return true;
    }
    return false;
}

void LLScriptEditorWSServer::dissociateEditor(const std::string& script_id)
{
    mScriptEditors.erase(script_id);
}

LLHandle<LLPanel> LLScriptEditorWSServer::findEditorForScript(const std::string& script_id) const
{
    auto it = mScriptEditors.find(script_id);
    if (it != mScriptEditors.end())
    {
        return it->second;
    }
    return LLHandle<LLPanel>();
}

//========================================================================
void LLScriptEditorWSConnection::onOpen()
{

}

void LLScriptEditorWSConnection::onClose()
{
}

void LLScriptEditorWSConnection::onMessage(const std::string& message)
{
    LL_DEBUGS("ScriptEditorWS") << "Received message: " << message << LL_ENDL;

    // Convert JSON string to LLSD
    LLSD parsed_message;
    try
    {
        boost::system::error_code ec;
        boost::json::value json_value = boost::json::parse(message, ec);

        if (ec.failed())
        {
            LL_WARNS("ScriptEditorWS") << "Failed to parse JSON message: " << ec.message() << LL_ENDL;

            // Send error response back to client
            LLSD error_response;
            error_response["command"] = "error";
            error_response["data"]["error_code"] = "PARSE_ERROR";
            error_response["data"]["error_message"] = "Invalid JSON format: " + std::string(ec.message());
            sendMessage(error_response);
            return;
        }

        // Convert boost::json::value to LLSD
        parsed_message = LlsdFromJson(json_value);

        LL_DEBUGS("ScriptEditorWS") << "Parsed LLSD message type: " << parsed_message.type()
                                    << ", has 'type' field: " << parsed_message.has("command") << LL_ENDL;
    }
    catch (const std::exception& e)
    {
        LL_WARNS("ScriptEditorWS") << "Exception parsing JSON message: " << e.what() << LL_ENDL;

        // Send error response back to client
        LLSD error_response;
        error_response["command"] = "error";
        error_response["data"]["error_code"] = "PARSE_EXCEPTION";
        error_response["data"]["error_message"] = "JSON parsing exception: " + std::string(e.what());
        sendMessage(error_response);
        return;
    }

    // Validate that we have a proper message structure
    if (!parsed_message.has("command"))
    {
        LL_WARNS("ScriptEditorWS") << "Received message without 'type' field" << LL_ENDL;

        LLSD error_response;
        error_response["command"] = "error";
        error_response["data"]["error_code"] = "INVALID_MESSAGE";
        error_response["data"]["error_message"] = "Message must have a 'type' field";
        sendMessage(error_response);
        return;
    }

    std::string message_type = parsed_message["command"].asString();
    LL_INFOS("ScriptEditorWS") << "Processing message of type: " << message_type << LL_ENDL;

    // Route message to appropriate handler based on type
    if (message_type == "connect")
    {
        processConnectMessage(parsed_message);
    }
    else
    {
        LL_WARNS("ScriptEditorWS") << "Received unknown message type: " << message_type << LL_ENDL;

        LLSD error_response;
        error_response["command"] = "error";
        error_response["data"]["error_code"] = "UNKNOWN_MESSAGE_TYPE";
        error_response["data"]["error_message"] = "Unknown message type: " + message_type;
        sendMessage(error_response);
    }
}

void LLScriptEditorWSConnection::processConnectMessage(const LLSD& message)
{
    LL_INFOS("ScriptEditorWS") << "Processing connect message from client" << LL_ENDL;

    // Extract connection information from the message
    LLSD data;
    if (message.has("data"))
    {
        data = message["data"];
    }

    // Parse client information
    std::string client_name;
    std::string client_version;
    std::string protocol_version;
    LLSD client_capabilities;
    LLSD supported_languages;
    LLSD client_features;

    // Extract client identification
    if (data.has("client_name"))
    {
        client_name = data["client_name"].asString();
        LL_INFOS("ScriptEditorWS") << "Client name: " << client_name << LL_ENDL;
    }

    if (data.has("client_version"))
    {
        client_version = data["client_version"].asString();
        LL_INFOS("ScriptEditorWS") << "Client version: " << client_version << LL_ENDL;
    }

    if (data.has("protocol_version"))
    {
        protocol_version = data["protocol_version"].asString();
        LL_INFOS("ScriptEditorWS") << "Protocol version: " << protocol_version << LL_ENDL;
    }

    // Extract client capabilities
    if (data.has("capabilities"))
    {
        client_capabilities = data["capabilities"];
        mEditorCapabilities = client_capabilities; // Store for later use
        LL_INFOS("ScriptEditorWS") << "Client capabilities count: " << client_capabilities.size() << LL_ENDL;
    }

    // Extract supported languages
    if (data.has("supported_languages"))
    {
        supported_languages = data["supported_languages"];
        LL_INFOS("ScriptEditorWS") << "Supported languages count: " << supported_languages.size() << LL_ENDL;
    }

    // Extract client features
    if (data.has("features"))
    {
        client_features = data["features"];
        LL_INFOS("ScriptEditorWS") << "Client features available" << LL_ENDL;
    }

    // Generate or extract editor session ID
    if (data.has("session_id"))
    {
        mEditorId = data["session_id"].asString();
        LL_INFOS("ScriptEditorWS") << "Using client session ID: " << mEditorId << LL_ENDL;
    }
    else
    {
        // Generate a new session ID if client didn't provide one
        mEditorId = LLUUID::generateNewID().asString();
        LL_INFOS("ScriptEditorWS") << "Generated session ID: " << mEditorId << LL_ENDL;
    }

    // Validate protocol compatibility
    bool protocol_compatible = true;
    if (!protocol_version.empty())
    {
        // For now, we only support protocol version "1.0"
        if (protocol_version != "1.0")
        {
            protocol_compatible = false;
            LL_WARNS("ScriptEditorWS") << "Unsupported protocol version: " << protocol_version
                                       << ", expected: 1.0" << LL_ENDL;
        }
    }

    // Determine if we have feature compatibility
    bool has_script_editing = false;
    if (client_capabilities.isArray())
    {
        for (LLSD::array_const_iterator it = client_capabilities.beginArray();
             it != client_capabilities.endArray(); ++it)
        {
            if (it->asString() == "script_editing")
            {
                has_script_editing = true;
                break;
            }
        }
    }

    // Build response message
    LLSD response;
    response["command"] = "connect_ack";

    LLSD& response_data = response["data"];

    if (protocol_compatible && has_script_editing)
    {
        // Successful connection
        response_data["status"] = "connected";
        response_data["message"] = "Connection established successfully";
        response_data["session_id"] = mEditorId;
        response_data["timestamp"] = LLDate::now().asString();

        // Send back our supported capabilities that match the client's
        LLSD mutual_capabilities = LLSD::emptyArray();

        // Check which capabilities we both support
        if (client_capabilities.isArray())
        {
            // Our server capabilities (from onConnectionOpened)
            std::set<std::string> server_caps = {
                "script_editing", "script_synchronization", "compilation",
                "metadata", "syntax_highlighting", "error_reporting"
            };

            for (LLSD::array_const_iterator it = client_capabilities.beginArray();
                 it != client_capabilities.endArray(); ++it)
            {
                std::string cap = it->asString();
                if (server_caps.count(cap) > 0)
                {
                    mutual_capabilities.append(cap);
                }
            }
        }

        response_data["mutual_capabilities"] = mutual_capabilities;

        // Send supported languages intersection
        LLSD mutual_languages = LLSD::emptyArray();
        if (supported_languages.isArray())
        {
            std::set<std::string> server_languages = {"lsl", "luau"};

            for (LLSD::array_const_iterator it = supported_languages.beginArray();
                 it != supported_languages.endArray(); ++it)
            {
                std::string lang = it->asString();
                if (server_languages.count(lang) > 0)
                {
                    mutual_languages.append(lang);
                }
            }
        }
        else
        {
            // Default to all our supported languages if client didn't specify
            mutual_languages.append("lsl");
            mutual_languages.append("luau");
        }

        response_data["supported_languages"] = mutual_languages;

        // Connection limits and constraints
        response_data["max_script_size"] = 65536;
        response_data["heartbeat_interval"] = 30;

        LL_INFOS("ScriptEditorWS") << "Successfully connected client: " << client_name
                                   << " v" << client_version
                                   << " with " << mutual_capabilities.size() << " mutual capabilities" << LL_ENDL;
    }
    else
    {
        // Connection failed
        response_data["status"] = "rejected";

        if (!protocol_compatible)
        {
            response_data["error"] = "incompatible_protocol";
            response_data["message"] = "Unsupported protocol version: " + protocol_version;
        }
        else if (!has_script_editing)
        {
            response_data["error"] = "missing_capabilities";
            response_data["message"] = "Client must support 'script_editing' capability";
        }
        else
        {
            response_data["error"] = "connection_failed";
            response_data["message"] = "Connection failed for unknown reason";
        }

        LL_WARNS("ScriptEditorWS") << "Rejected connection from client: " << client_name
                                   << " - " << response_data["message"].asString() << LL_ENDL;
    }

    // Send the response back to the client
    if (sendMessage(response))
    {
        LL_INFOS("ScriptEditorWS") << "Sent connect acknowledgment to client" << LL_ENDL;
    }
    else
    {
        LL_WARNS("ScriptEditorWS") << "Failed to send connect acknowledgment to client" << LL_ENDL;
    }

    // If connection was successful, we could also send any initial state or configuration
    if (response_data["status"].asString() == "connected")
    {
        // TODO: Send initial script list, active editors, or other relevant state
        // Example: sendScriptList(), sendActiveEditors(), etc.
    }
}