vim-xai.pl - A Perl script for interacting with Grok AI completion service
use LWP::UserAgent;
use HTTP::Request;
use JSON;
use utf8;
use open ':std', ':encoding(UTF-8)';
main();This script is designed to interact with an AI completion service, for use within a Vim plugin. It sends a request to an AI service with user-defined parameters and processes the streaming response to update the Vim buffer in real-time.
- main()
-
The main function orchestrates the script's operation:
Evaluates Vim variables for configuration.
Prepares and sends an HTTP POST request to the AI service.
Handles the response, either streaming or non-streaming, and updates the Vim buffer accordingly.
- do_msg($chunk)
-
Processes chunks of the streaming response:
Accumulates chunks until a complete message is received.
Decodes JSON from the message chunks.
Updates the Vim buffer with the AI's response content.
Ensures immediate output by setting autoflush.
$chunk_buffer-
Stores incomplete chunks of the streaming response.
$collect_messages-
Accumulates the content of the AI's response for display.
@evals-
An array of Vim variables to evaluate for configuration.
$xai_xd_obj-
A JSON object representing the request to the AI service.
$json_string-
The JSON encoded string of the request object.
$headers-
HTTP headers for the request, including authentication.
$ua-
An instance of LWP::UserAgent for making HTTP requests.
$req-
The HTTP request object.
$response-
The HTTP response object from the AI service.
LWP::UserAgent
HTTP::Request
JSON
utf8
open
Albert Mendes <tray.mendes@gmail.com>
