Home Explore Blog CI



nushell

2nd chunk of `contributor-book/plugin_protocol_reference.md`
c61453877f9beac976f925da90aae1dfd04da97d08df5d780000000100000fbd
4. If the plugin specified stream data as output in the response, it **should** now send [stream messages](#stream-messages) with the corresponding stream ID(s).

The plugin **should** respond to further plugin calls. The engine **may** send additional plugin calls before responses have been received, and it is up to the plugin to decide whether to handle each call immediately as it is received, or to process only one at a time and hold on to them for later. In any case, sending another plugin call before a response has been received **should not** cause an error.

The plugin **may** send [engine calls](#enginecall) during the execution of a call to request operations from the engine. Engine calls are [only valid within the context of a call](#enginecall-context) and may not be sent otherwise.

The engine **may** send a [`Goodbye`](#goodbye) message to the plugin indicating that it will no longer send any more plugin calls. Upon receiving this message, the plugin **may** choose not to accept any more plugin calls, and **should** exit after any in-progress plugin calls have finished.

**Note**: During the sequence, the engine may also send a [`Signal`](#signal) message asynchronously, such as when an interrupt (Ctrl+C) or reset signal is triggered. Plugins should handle these messages as they are received, for example, by pausing or stopping operations when an `Interrupt` signal is sent.

## `Hello`

After the encoding type has been decided, both the engine and plugin **must** send a `Hello` message containing relevant version and protocol support information.

| Field        | Type   | Description                                                                           |
| ------------ | ------ | ------------------------------------------------------------------------------------- |
| **protocol** | string | **Must** be `"nu-plugin"`.                                                            |
| **version**  | string | The engine's version, or the target version of Nu that the plugin supports.           |
| **features** | array  | Protocol features supported by the plugin. Unrecognized elements **must** be ignored. |

To be accepted, the `version` specified **must** be [semver](https://semver.org) compatible with the engine's version. "0.x.y" and "x.y.z" for differing values of "x" are considered to be incompatible.

Plugins **may** decide to refuse engine versions with more strict criteria than specified here.

Example:

```json
{
  "Hello": {
    "protocol": "nu-plugin",
    "version": "0.94.0",
    "features": []
  }
}
```

### Features

All features are maps that **must** contain at least a `name` key, and **may** contain other keys. Features that are not recognized by `name` **must** be ignored, and not cause an error. Plugins **must** only advertise support for features they implement, and **should not** determine the features they will advertise depending on the engine's `Hello` message.

#### `LocalSocket` feature

This feature advertises support for local socket communication, instead of stdio.

Example:

```json
{
  "name": "LocalSocket"
}
```

When local socket communication is advertised to an engine supporting the feature, the engine will cease stdio communication and launch the plugin again with the `--local-socket` command line argument. The second argument is either a path to a Unix domain socket on Linux, Android, macOS, and other Unix-like operating systems, or the name of a named pipe (without the `\\.\pipe\` prefix) on Windows.

In either case, during startup, the plugin is expected to establish two separate connections to the socket, in this order:

1. The input stream connection, used to send messages from the engine to the plugin
2. The output stream connection, used to send messages from the plugin to the engine

The connections are separate in order to facilitate ownership of the streams by separate threads. After these connections are both established, the engine will remove the socket, and will not accept further connections.

Title: Plugin Protocol: Message Handling, Hello Message, and LocalSocket Feature
Summary
This section details how plugins handle messages after initial communication. Plugins should respond to further plugin calls, and the engine may send additional calls before responses are received. Plugins can send 'EngineCall' messages within a call's context and may receive a 'Goodbye' message to signal the end of communication. The 'Hello' message, containing protocol, version, and supported features, is exchanged after the encoding type is decided. The 'LocalSocket' feature enables local socket communication instead of stdio. If a plugin supports the `LocalSocket` feature, the engine will cease stdio communication and launch the plugin again with the `--local-socket` command line argument. The plugin is expected to establish two separate connections to the socket, in a specific order.