Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/blink/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "blink",
"version": "1.1.29",
"version": "1.1.30",
"description": "Blink is a tool for building and deploying AI agents.",
"type": "module",
"bin": {
Expand Down
70 changes: 70 additions & 0 deletions packages/blink/src/cli/compute-server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { Server, type ServerOptions } from "@blink-sdk/compute-protocol/server";
import { Emitter } from "@blink-sdk/events";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { WebSocketServer } from "ws";

const defaultEnvVariables = {
// These are so Blink can use commits to GitHub properly.
GIT_TERMINAL_PROMPT: "0",
GIT_PAGER: "cat",
GIT_AUTHOR_NAME: "blink-so[bot]",
GIT_AUTHOR_EMAIL: "211532188+blink-so[bot]@users.noreply.github.com",
GIT_COMMITTER_NAME: "blink-so[bot]",
GIT_COMMITTER_EMAIL: "211532188+blink-so[bot]@users.noreply.github.com",

// The `gh` CLI is required to be in the workspace.
// Eventually, we should move this credential helper to just be in the Blink CLI.
GIT_CONFIG_COUNT: "1",
GIT_CONFIG_KEY_0: "credential.https://github.com.helper",
GIT_CONFIG_VALUE_0: "!gh auth git-credential",
};

export default async function serveCompute() {
for (const [key, value] of Object.entries(defaultEnvVariables)) {
if (process.env[key] === undefined) {
process.env[key] = value;
}
}

const port = parseInt(process.env.PORT ?? "22137");
if (isNaN(port)) {
throw new Error("PORT environment variable is not a number");
}

const wss = new WebSocketServer({ port });

console.log(`Compute server running on port ${port}`);

wss.on("connection", (ws) => {
console.log("Client connected");

let nodePty: typeof import("@lydell/node-pty") | undefined;
try {
nodePty = require("@lydell/node-pty");
} catch (e) {
// It's fine, we don't _need_ to use TTYs.
}
if (typeof Bun !== "undefined") {
nodePty = undefined;
}

const server = new Server({
nodePty,
send: (message: Uint8Array) => {
// Send binary data to the WebSocket client
ws.send(message);
},
});

ws.on("message", (data: Buffer) => {
// Forward WebSocket messages to the server
server.handleMessage(new Uint8Array(data));
});

ws.on("close", () => {
console.log("Client disconnected");
});
});
}
9 changes: 9 additions & 0 deletions packages/blink/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,15 @@ program
.description("Log in to the Blink Cloud.")
.action(asyncEntry(() => import("./login")));

const computeCommand = program
.command("compute")
.description("Compute-related commands.");

computeCommand
.command("server")
.description("Run a compute protocol server on localhost.")
.action(asyncEntry(() => import("./compute-server")));

// Configure error output
program.configureOutput({
writeErr: (str) => {
Expand Down
Loading