This commit is contained in:
elpresidank 2026-04-05 22:44:45 -05:00
parent c386f68743
commit b6536eca38
100 changed files with 17680 additions and 377 deletions

View file

@ -3,10 +3,11 @@
*/
import type { Command } from "commander";
import { SocketManager } from "@trustgraph/mcp";
import { createTrustGraphSocket, type BaseApi } from "@trustgraph/client";
export interface CliOpts {
gateway: string;
user: string;
token?: string;
flow: string;
}
@ -18,11 +19,36 @@ export function getOpts(cmd: Command): CliOpts {
return root.opts() as CliOpts;
}
export async function createSocket(opts: CliOpts): Promise<SocketManager> {
const socket = new SocketManager({
gatewayUrl: opts.gateway,
token: opts.token,
/**
* Create a BaseApi socket client and wait for the connection to be established.
* The client auto-connects; we listen for the first "connected/authenticated"
* state before handing it back to the caller.
*/
export async function createSocket(opts: CliOpts): Promise<BaseApi> {
const socket = createTrustGraphSocket(opts.user, opts.token, opts.gateway);
// Wait for the socket to reach an open state
await new Promise<void>((resolve, reject) => {
const timeout = setTimeout(() => {
unsub();
reject(new Error("Timed out waiting for WebSocket connection"));
}, 15_000);
const unsub = socket.onConnectionStateChange((state) => {
if (
state.status === "authenticated" ||
state.status === "unauthenticated"
) {
clearTimeout(timeout);
unsub();
resolve();
} else if (state.status === "failed") {
clearTimeout(timeout);
unsub();
reject(new Error(state.lastError ?? "WebSocket connection failed"));
}
});
});
await socket.connect();
return socket;
}