Skip to content

WebSocket API does not correctly format init message #715

@mgagvani

Description

@mgagvani

I am trying to reproduce the bash example in the README using the following python code:

import asyncio
import json

import websockets


async def main():
    uri = "ws://localhost:2000/api/v2/connect"
    async with websockets.connect(uri) as ws:
        # 1) Initialize the job (like POST /execute without stdin)
        init_msg = {
            "type": "init",
            "language": "bash",
            "version": "5.1.0",
            "files": [
                {"content": "cat"}     # echo stdin to stdout
            ]
        }
        await ws.send(json.dumps(init_msg))
        print("→ init sent")

        # 2) Read the runtime and compile/run stage messages
        for _ in range(2):
            raw = await ws.recv()
            msg = json.loads(raw)
            print("←", msg)

        # 3) Send stdin data
        data_msg = {
            "type": "data",
            "stream": "stdin",
            "data": "Hello World!\n"
        }
        await ws.send(json.dumps(data_msg))
        print("→ stdin data sent")

        # 4) Echo everything until exit
        async for raw in ws:
            msg = json.loads(raw)
            print("←", msg)
            if msg.get("type") == "exit":
                print("Process exited; closing connection.")
                break

if __name__ == "__main__":
    asyncio.run(main())

However, running this code results in the following output:

→ init sent
← {'type': 'error', 'message': 'bash-5.1.0 runtime is unknown'}
Traceback (most recent call last):
  File "/Users/mgagvani/Documents/sedaro/sedaro-synapse/src/sedaro-synapse/TEMP_SOCKET.py", line 47, in <module>
    asyncio.run(main())
  File "/opt/homebrew/Cellar/python@3.11/3.11.12/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.11/3.11.12/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.11/3.11.12/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/base_events.py", line 654, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/Users/mgagvani/Documents/sedaro/sedaro-synapse/src/sedaro-synapse/TEMP_SOCKET.py", line 25, in main
    raw = await ws.recv()
          ^^^^^^^^^^^^^^^
  File "/Users/mgagvani/Documents/sedaro/sedaro-synapse/.venv/lib/python3.11/site-packages/websockets/asyncio/connection.py", line 322, in recv
    raise self.protocol.close_exc from self.recv_exc
websockets.exceptions.ConnectionClosedError: received 4002 (private use) Notified Error; then sent 4002 (private use) Notified Error

It looks like an extra dash is being added to the language/version since running cli/index.js ppman list shows that bash 5.1.0 is an installed language. Changing the version to * does not resolve the issue, the resulting error will be "{'type': 'error', 'message': 'bash-* runtime is unknown'}"

Are there any working examples of working with the websocket connection over Python that could be referenced? The two APIs linked do not seem to support it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions