Skip to content

Instantly share code, notes, and snippets.

@gtrak
Last active November 12, 2024 02:25
Show Gist options
  • Save gtrak/22a573c3ca4c1db67b21fefa81a3b780 to your computer and use it in GitHub Desktop.
Save gtrak/22a573c3ca4c1db67b21fefa81a3b780 to your computer and use it in GitHub Desktop.
continue.dev client POC
(require 'json)
(defconst continue-core-host "127.0.0.1")
(defconst continue-core-port 3000)
(defvar continue-core-socket nil)
(defvar continue-core-response-queue '()
"Queue for storing responses from Continue Core server.")
(defun continue-core-process-filter (process output)
"Process filter for Continue Core TCP server.
OUTPUT is the data received from the server."
;; Split the output into lines and process each line
(let ((lines (split-string output "\r\n")))
(dolist (line lines)
;; Check if the line is not empty or just a newline character
(when (and (> (length line) 0))
;; Do something with each non-empty line
(setf continue-core-response-queue (cons line continue-core-response-queue))))))
(defun read-from-continue-core-queue-with-request-id (request-id)
"Reads from the front of `continue-core-response-queue` and returns the message if it matches REQUEST-ID.
If no matching message is found, nil is returned."
(when continue-core-response-queue
;; Pop each message from the queue and check for a match
(let ((response (pop continue-core-response-queue)))
(condition-case err
(let* ((parsed-json (json-parse-string response))
(actual-message-id (gethash "messageId" parsed-json)))
(when (string= actual-message-id request-id)
parsed-json))
(error (progn
;; If there's an error in parsing, log it and return nil
(message "Error parsing message: %s" (error-message-string err))
nil))))))
(defun poll-responses (timeout-seconds request-id)
"Polls `read-from-continue-core-queue-with-request-id` with a TIMEOUT-SECONDS timeout for REQUEST-ID.
If a matching message is found within the timeout, it returns the parsed JSON. Otherwise, it returns nil."
(let ((start-time (current-time))
(found-message nil))
(while (and (not found-message)
(< (float-time (time-since start-time)) timeout-seconds))
(setq found-message (read-from-continue-core-queue-with-request-id request-id))
(when found-message
(message "Found message: %s" found-message))
(sleep-for 0.1)) ;; Sleep to prevent busy-waiting
found-message))
(defun continue-core-connect ()
"Connect to the Continue Core TCP server and set up a process filter."
(setq continue-core-socket
(open-network-stream "continue-core" nil continue-core-host continue-core-port))
(when continue-core-socket
;; Set the process filter for the new network stream
(set-process-filter continue-core-socket 'continue-core-process-filter)
(message "Connected to Continue Core"))
continue-core-socket)
;; (continue-core-connect)
(defun continue-core-disconnect ()
"Disconnect from the Continue Core TCP server."
(when continue-core-socket
(delete-process continue-core-socket)
(setq continue-core-socket nil))
(message "Disconnected from Continue Core"))
;; (continue-core-disconnect)
(defun generate-uuid ()
"Generate a UUID using the `uuidgen` command."
(let ((uuid (shell-command-to-string "uuidgen")))
;; Trim any trailing newline characters from the output
(string-trim uuid)))
(defun continue-core-send-message (message-type data &optional message-id)
"Send a message to the Continue Core with MESSAGE-TYPE and DATA.
If MESSAGE-ID is not provided, generate one."
(let* ((message-id (or message-id (generate-uuid)))
(msg `((messageType . ,message-type)
(data . ,data)
(messageId . ,message-id)))
(msg-json (json-encode msg)))
(process-send-string continue-core-socket
(concat msg-json "\r\n"))
message-id))
(defun continue-core-ping ()
"Send a ping to the Continue Core and return \"pong\" if successful."
(when (continue-core-connect)
(let* ((message-id (continue-core-send-message "ping" "ping"))
(response (poll-responses 5 message-id)))
(if response
() ;; Do something
(progn
(continue-core-disconnect)
(error "No response from Continue Core"))))))
;; (continue-core-ping)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment