UNPKG

neuro-game-sdk

Version:

A TypeScript/JavaScript SDK for integrating games with Neuro-sama AI streamer.

134 lines (133 loc) 4.79 kB
import WebSocket from 'modern-isomorphic-ws'; /** * An action is a registerable command that Neuro can execute whenever she wants. */ export interface Action { /** * The name of the action, which is its unique identifier. * Should be a lowercase string with words separated by underscores or dashes. */ name: string; /** * A plaintext description of what this action does. * This information will be directly received by Neuro. */ description: string; /** * A valid simple JSON schema object that describes how the response data should look like. * If your action does not have any parameters, you can omit this field or set it to {}. */ schema?: any; } /** * The type of the action handler function. */ type ActionHandler = (actionData: { id: string; name: string; params: any; }) => void; /** * The NeuroClient class handles communication with Neuro-sama's server. */ export declare class NeuroClient { /** * The WebSocket connection to Neuro-sama's server. */ ws?: WebSocket; /** * The game name, used to identify the game. */ game: string; /** * The WebSocket server URL. */ url: string; /** * Array of handlers for incoming actions from Neuro-sama. */ actionHandlers: ActionHandler[]; /** * Handler for WebSocket 'close' events. */ onClose?: (event: any) => void; /** * Handler for WebSocket 'error' events. */ onError?: (error: any) => void; /** * Creates an instance of NeuroClient. * @param url The WebSocket server URL. * @param game The game name. * @param onConnected Callback invoked when the WebSocket connection is established. */ constructor(url: string, game: string, onConnected: () => void); /** * Initializes the WebSocket connection. * @param onConnected Callback invoked when the WebSocket connection is established. */ private connect; /** * Sends the 'startup' message to inform Neuro-sama that the game is running. */ private sendStartup; /** * Sends a message over the WebSocket connection. * @param message The message to send. */ private sendMessage; /** * Handles incoming messages from Neuro-sama. * @param data The message data received. */ private handleMessage; /** * Handles 'action' messages from Neuro-sama. * @param data The action message data. */ private handleActionMessage; /** * Sends a 'context' message to let Neuro know about something that is happening in game. * @param messageText A plaintext message that describes what is happening in the game. * @param silent If true, the message will be added to Neuro's context without prompting her to respond to it. */ sendContext(messageText: string, silent?: boolean): void; /** * Registers one or more actions for Neuro to use. * @param actions An array of actions to be registered. */ registerActions(actions: Action[]): void; /** * Unregisters one or more actions, preventing Neuro from using them anymore. * @param actionNames The names of the actions to unregister. */ unregisterActions(actionNames: string[]): void; /** * Forces Neuro to execute one of the listed actions as soon as possible. * Note that this might take a bit if she is already talking. * @param query A plaintext message that tells Neuro what she is currently supposed to be doing. * @param actionNames The names of the actions that Neuro should choose from. * @param state An arbitrary string that describes the current state of the game. * @param ephemeralContext If true, Neuro will only remember the context for the duration of the actions force. */ forceActions(query: string, actionNames: string[], state?: string, ephemeralContext?: boolean): void; /** * Sends an action result message to Neuro-sama. * Needs to be sent as soon as possible after an action is validated, to allow Neuro to continue. * @param id The id of the action that this result is for. * @param success Whether or not the action was successful. * @param messageText A plaintext message that describes what happened when the action was executed. */ sendActionResult(id: string, success: boolean, messageText?: string): void; /** * Registers an action handler to process incoming actions from Neuro-sama. * Multiple handlers can be registered. * @param handler The action handler function. */ onAction(handler: ActionHandler): void; /** * Closes the WebSocket connection. */ disconnect(): void; } export {};