diff --git a/Packages/src/TypeScriptServer~/dist/server.bundle.js b/Packages/src/TypeScriptServer~/dist/server.bundle.js index d1a3269fd..e0839027a 100755 --- a/Packages/src/TypeScriptServer~/dist/server.bundle.js +++ b/Packages/src/TypeScriptServer~/dist/server.bundle.js @@ -7061,7 +7061,9 @@ var UnityClient = class _UnityClient { if (!currentSocket.destroyed) { currentSocket.destroy(); } - this.socket = null; + if (this.socket === currentSocket) { + this.socket = null; + } reject(error); }; currentSocket.connect(this.port, this.host, () => { @@ -7224,9 +7226,7 @@ var UnityClient = class _UnityClient { */ async executeTool(toolName, params = {}) { if (!this.connected) { - throw new Error( - "Not connected to Unity. Please wait for connection to be established. Note: If you just executed the compile tool, the Unity connection may be temporarily disconnected. Please wait a few seconds and try again." - ); + throw new Error(this.getOsSpecificReconnectMessage()); } await this.setClientName(); const request = { @@ -7245,6 +7245,21 @@ var UnityClient = class _UnityClient { throw error; } } + /** + * Build an OS-specific guidance message for temporary disconnection after compile. + * Explicitly instructs how to wait before retrying without assuming a fixed duration. + */ + getOsSpecificReconnectMessage() { + const commonPrefix = "Not connected to Unity. If you just executed the compile tool, Unity reconnects automatically after compilation finishes. This can take from several seconds to tens of seconds depending on project size. Wait before your next tool call, then retry once."; + const platform = typeof process !== "undefined" && typeof process.platform === "string" ? process.platform : "unknown"; + if (platform === "win32") { + return `${commonPrefix} Examples: PowerShell: Start-Sleep -Seconds ; cmd: timeout /T /NOBREAK. Avoid repeated retries; increase if needed.`; + } + if (platform === "darwin" || platform === "linux") { + return `${commonPrefix} Example: sleep . Avoid repeated retries; increase if needed.`; + } + return `${commonPrefix} Wait a bit longer if needed before retrying. Avoid repeated retries.`; + } handleToolResponse(response, toolName) { if (response.error) { throw new Error(`Failed to execute tool '${toolName}': ${response.error.message}`); diff --git a/Packages/src/TypeScriptServer~/src/unity-client.ts b/Packages/src/TypeScriptServer~/src/unity-client.ts index 587d85115..d48568a17 100644 --- a/Packages/src/TypeScriptServer~/src/unity-client.ts +++ b/Packages/src/TypeScriptServer~/src/unity-client.ts @@ -452,9 +452,7 @@ export class UnityClient { async executeTool(toolName: string, params: Record = {}): Promise { // Ensure connection before executing tool if (!this.connected) { - throw new Error( - 'Not connected to Unity. Please wait for connection to be established. Note: If you just executed the compile tool, the Unity connection may be temporarily disconnected. Please wait a few seconds and try again.', - ); + throw new Error(this.getOsSpecificReconnectMessage()); } // Ensure client name is set (this completes the connection handshake) @@ -486,6 +484,31 @@ export class UnityClient { } } + /** + * Build an OS-specific guidance message for temporary disconnection after compile. + * Explicitly instructs how to wait before retrying without assuming a fixed duration. + */ + private getOsSpecificReconnectMessage(): string { + const commonPrefix: string = + 'Not connected to Unity. If you just executed the compile tool, Unity reconnects automatically after compilation finishes. This can take from several seconds to tens of seconds depending on project size. Wait before your next tool call, then retry once.'; + + const platform: string = + typeof process !== 'undefined' && typeof process.platform === 'string' + ? process.platform + : 'unknown'; + + if (platform === 'win32') { + return `${commonPrefix} Examples: PowerShell: Start-Sleep -Seconds ; cmd: timeout /T /NOBREAK. Avoid repeated retries; increase if needed.`; + } + + if (platform === 'darwin' || platform === 'linux') { + return `${commonPrefix} Example: sleep . Avoid repeated retries; increase if needed.`; + } + + // Fallback for other platforms + return `${commonPrefix} Wait a bit longer if needed before retrying. Avoid repeated retries.`; + } + private handleToolResponse( response: { error?: { message: string }; result?: unknown }, toolName: string,