mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-07-02 02:58:10 +02:00
init
This commit is contained in:
parent
c386f68743
commit
b6536eca38
100 changed files with 17680 additions and 377 deletions
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue