mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-07-01 09:29:38 +02:00
saving
This commit is contained in:
parent
9e9307a2aa
commit
e26caa0b12
123 changed files with 3478 additions and 10078 deletions
25
ts/packages/cli/package.json
Normal file
25
ts/packages/cli/package.json
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
{
|
||||
"name": "@trustgraph/cli",
|
||||
"version": "0.1.0",
|
||||
"type": "module",
|
||||
"bin": {
|
||||
"tg": "dist/index.js"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"dev": "tsc --watch",
|
||||
"clean": "rm -rf dist",
|
||||
"test": "vitest run"
|
||||
},
|
||||
"dependencies": {
|
||||
"@trustgraph/base": "workspace:*",
|
||||
"@trustgraph/mcp": "workspace:*",
|
||||
"commander": "^13.1.0",
|
||||
"ws": "^8.18.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/ws": "^8.5.0",
|
||||
"typescript": "^5.8.0",
|
||||
"vitest": "^3.1.0"
|
||||
}
|
||||
}
|
||||
34
ts/packages/cli/src/commands/agent.ts
Normal file
34
ts/packages/cli/src/commands/agent.ts
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
/**
|
||||
* Agent CLI commands.
|
||||
*
|
||||
* Python reference: trustgraph-cli/trustgraph/cli/invoke_agent.py
|
||||
*/
|
||||
|
||||
import type { Command } from "commander";
|
||||
import { createSocket, getOpts } from "./util.js";
|
||||
|
||||
export function registerAgentCommands(program: Command): void {
|
||||
program
|
||||
.command("agent")
|
||||
.description("Ask the TrustGraph agent a question")
|
||||
.argument("<question>", "Question to ask")
|
||||
.action(async (question: string, _opts, cmd) => {
|
||||
const opts = getOpts(cmd);
|
||||
const socket = await createSocket(opts);
|
||||
|
||||
try {
|
||||
const resp = await socket.request("agent", { question }, {
|
||||
flowId: opts.flow,
|
||||
onChunk: (chunk) => {
|
||||
const c = chunk as { answer?: string };
|
||||
if (c.answer) process.stdout.write(c.answer);
|
||||
},
|
||||
});
|
||||
|
||||
const r = resp as { answer?: string };
|
||||
if (r.answer) console.log(r.answer);
|
||||
} finally {
|
||||
await socket.close();
|
||||
}
|
||||
});
|
||||
}
|
||||
81
ts/packages/cli/src/commands/config.ts
Normal file
81
ts/packages/cli/src/commands/config.ts
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
/**
|
||||
* Config CLI commands.
|
||||
*
|
||||
* Python reference: trustgraph-cli/trustgraph/cli/show_config.py etc.
|
||||
*/
|
||||
|
||||
import type { Command } from "commander";
|
||||
import { createSocket, getOpts } from "./util.js";
|
||||
|
||||
export function registerConfigCommands(program: Command): void {
|
||||
const config = program
|
||||
.command("config")
|
||||
.description("Configuration management");
|
||||
|
||||
config
|
||||
.command("show")
|
||||
.description("Show current configuration")
|
||||
.action(async (_opts, cmd) => {
|
||||
const opts = getOpts(cmd);
|
||||
const socket = await createSocket(opts);
|
||||
|
||||
try {
|
||||
const resp = await socket.request("config", { operation: "config" });
|
||||
console.log(JSON.stringify(resp, null, 2));
|
||||
} finally {
|
||||
await socket.close();
|
||||
}
|
||||
});
|
||||
|
||||
config
|
||||
.command("get")
|
||||
.description("Get a configuration value")
|
||||
.argument("<key>", "Config key")
|
||||
.action(async (key: string, _opts, cmd) => {
|
||||
const opts = getOpts(cmd);
|
||||
const socket = await createSocket(opts);
|
||||
|
||||
try {
|
||||
const resp = await socket.request("config", { operation: "get", keys: [key] });
|
||||
console.log(JSON.stringify(resp, null, 2));
|
||||
} finally {
|
||||
await socket.close();
|
||||
}
|
||||
});
|
||||
|
||||
config
|
||||
.command("set")
|
||||
.description("Set a configuration value")
|
||||
.argument("<key>", "Config key")
|
||||
.argument("<value>", "Config value (JSON)")
|
||||
.action(async (key: string, value: string, _opts, cmd) => {
|
||||
const opts = getOpts(cmd);
|
||||
const socket = await createSocket(opts);
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(value);
|
||||
const resp = await socket.request("config", {
|
||||
operation: "put",
|
||||
values: { [key]: parsed },
|
||||
});
|
||||
console.log(JSON.stringify(resp, null, 2));
|
||||
} finally {
|
||||
await socket.close();
|
||||
}
|
||||
});
|
||||
|
||||
config
|
||||
.command("list")
|
||||
.description("List configuration keys")
|
||||
.action(async (_opts, cmd) => {
|
||||
const opts = getOpts(cmd);
|
||||
const socket = await createSocket(opts);
|
||||
|
||||
try {
|
||||
const resp = await socket.request("config", { operation: "list" });
|
||||
console.log(JSON.stringify(resp, null, 2));
|
||||
} finally {
|
||||
await socket.close();
|
||||
}
|
||||
});
|
||||
}
|
||||
80
ts/packages/cli/src/commands/flow.ts
Normal file
80
ts/packages/cli/src/commands/flow.ts
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
/**
|
||||
* Flow management CLI commands.
|
||||
*
|
||||
* Python reference: trustgraph-cli/trustgraph/cli/start_flow.py, stop_flow.py, etc.
|
||||
*/
|
||||
|
||||
import type { Command } from "commander";
|
||||
import { createSocket, getOpts } from "./util.js";
|
||||
|
||||
export function registerFlowCommands(program: Command): void {
|
||||
const flow = program
|
||||
.command("flow")
|
||||
.description("Flow management");
|
||||
|
||||
flow
|
||||
.command("list")
|
||||
.description("List active flows")
|
||||
.action(async (_opts, cmd) => {
|
||||
const opts = getOpts(cmd);
|
||||
const socket = await createSocket(opts);
|
||||
|
||||
try {
|
||||
const resp = await socket.request("flow", { operation: "list" });
|
||||
console.log(JSON.stringify(resp, null, 2));
|
||||
} finally {
|
||||
await socket.close();
|
||||
}
|
||||
});
|
||||
|
||||
flow
|
||||
.command("start")
|
||||
.description("Start a flow")
|
||||
.argument("<name>", "Flow name")
|
||||
.action(async (name: string, _opts, cmd) => {
|
||||
const opts = getOpts(cmd);
|
||||
const socket = await createSocket(opts);
|
||||
|
||||
try {
|
||||
const resp = await socket.request("flow", { operation: "start", name });
|
||||
console.log(JSON.stringify(resp, null, 2));
|
||||
} finally {
|
||||
await socket.close();
|
||||
}
|
||||
});
|
||||
|
||||
flow
|
||||
.command("stop")
|
||||
.description("Stop a flow")
|
||||
.argument("<name>", "Flow name")
|
||||
.action(async (name: string, _opts, cmd) => {
|
||||
const opts = getOpts(cmd);
|
||||
const socket = await createSocket(opts);
|
||||
|
||||
try {
|
||||
const resp = await socket.request("flow", { operation: "stop", name });
|
||||
console.log(JSON.stringify(resp, null, 2));
|
||||
} finally {
|
||||
await socket.close();
|
||||
}
|
||||
});
|
||||
|
||||
flow
|
||||
.command("status")
|
||||
.description("Show flow status")
|
||||
.argument("[name]", "Flow name (all if omitted)")
|
||||
.action(async (name: string | undefined, _opts, cmd) => {
|
||||
const opts = getOpts(cmd);
|
||||
const socket = await createSocket(opts);
|
||||
|
||||
try {
|
||||
const resp = await socket.request("flow", {
|
||||
operation: "status",
|
||||
...(name ? { name } : {}),
|
||||
});
|
||||
console.log(JSON.stringify(resp, null, 2));
|
||||
} finally {
|
||||
await socket.close();
|
||||
}
|
||||
});
|
||||
}
|
||||
58
ts/packages/cli/src/commands/graph-rag.ts
Normal file
58
ts/packages/cli/src/commands/graph-rag.ts
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
/**
|
||||
* Graph RAG CLI commands.
|
||||
*
|
||||
* Python reference: trustgraph-cli/trustgraph/cli/invoke_graph_rag.py
|
||||
*/
|
||||
|
||||
import type { Command } from "commander";
|
||||
import { createSocket, getOpts } from "./util.js";
|
||||
|
||||
export function registerGraphRagCommands(program: Command): void {
|
||||
program
|
||||
.command("graph-rag")
|
||||
.description("Query the knowledge graph using RAG")
|
||||
.argument("<query>", "Natural language query")
|
||||
.option("--entity-limit <n>", "Max entities", "50")
|
||||
.option("--triple-limit <n>", "Max triples per entity", "30")
|
||||
.action(async (query: string, cmdOpts, cmd) => {
|
||||
const opts = getOpts(cmd);
|
||||
const socket = await createSocket(opts);
|
||||
|
||||
try {
|
||||
const resp = await socket.request(
|
||||
"graph-rag",
|
||||
{
|
||||
query,
|
||||
entity_limit: parseInt(cmdOpts.entityLimit, 10),
|
||||
triple_limit: parseInt(cmdOpts.tripleLimit, 10),
|
||||
},
|
||||
{ flowId: opts.flow },
|
||||
) as { response?: string };
|
||||
|
||||
console.log(resp.response ?? JSON.stringify(resp, null, 2));
|
||||
} finally {
|
||||
await socket.close();
|
||||
}
|
||||
});
|
||||
|
||||
program
|
||||
.command("document-rag")
|
||||
.description("Query documents using RAG")
|
||||
.argument("<query>", "Natural language query")
|
||||
.action(async (query: string, _cmdOpts, cmd) => {
|
||||
const opts = getOpts(cmd);
|
||||
const socket = await createSocket(opts);
|
||||
|
||||
try {
|
||||
const resp = await socket.request(
|
||||
"document-rag",
|
||||
{ query },
|
||||
{ flowId: opts.flow },
|
||||
) as { response?: string };
|
||||
|
||||
console.log(resp.response ?? JSON.stringify(resp, null, 2));
|
||||
} finally {
|
||||
await socket.close();
|
||||
}
|
||||
});
|
||||
}
|
||||
28
ts/packages/cli/src/commands/util.ts
Normal file
28
ts/packages/cli/src/commands/util.ts
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
/**
|
||||
* Shared CLI utilities.
|
||||
*/
|
||||
|
||||
import type { Command } from "commander";
|
||||
import { SocketManager } from "@trustgraph/mcp";
|
||||
|
||||
export interface CliOpts {
|
||||
gateway: string;
|
||||
token?: string;
|
||||
flow: string;
|
||||
}
|
||||
|
||||
export function getOpts(cmd: Command): CliOpts {
|
||||
// Walk up to root command to get global options
|
||||
let root = cmd;
|
||||
while (root.parent) root = root.parent;
|
||||
return root.opts() as CliOpts;
|
||||
}
|
||||
|
||||
export async function createSocket(opts: CliOpts): Promise<SocketManager> {
|
||||
const socket = new SocketManager({
|
||||
gatewayUrl: opts.gateway,
|
||||
token: opts.token,
|
||||
});
|
||||
await socket.connect();
|
||||
return socket;
|
||||
}
|
||||
33
ts/packages/cli/src/index.ts
Normal file
33
ts/packages/cli/src/index.ts
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
/**
|
||||
* Unified TrustGraph CLI.
|
||||
*
|
||||
* Replaces the 60+ individual Python CLI scripts with a single
|
||||
* `tg` command using subcommands.
|
||||
*
|
||||
* Python reference: trustgraph-cli/trustgraph/cli/
|
||||
*/
|
||||
|
||||
import { Command } from "commander";
|
||||
import { registerAgentCommands } from "./commands/agent.js";
|
||||
import { registerGraphRagCommands } from "./commands/graph-rag.js";
|
||||
import { registerConfigCommands } from "./commands/config.js";
|
||||
import { registerFlowCommands } from "./commands/flow.js";
|
||||
|
||||
const program = new Command();
|
||||
|
||||
program
|
||||
.name("tg")
|
||||
.description("TrustGraph CLI — interact with TrustGraph services")
|
||||
.version("0.1.0")
|
||||
.option("-g, --gateway <url>", "Gateway WebSocket URL", "ws://localhost:8088/api/v1/socket")
|
||||
.option("-t, --token <token>", "Authentication token")
|
||||
.option("-f, --flow <id>", "Flow ID", "default");
|
||||
|
||||
registerAgentCommands(program);
|
||||
registerGraphRagCommands(program);
|
||||
registerConfigCommands(program);
|
||||
registerFlowCommands(program);
|
||||
|
||||
program.parse();
|
||||
12
ts/packages/cli/tsconfig.json
Normal file
12
ts/packages/cli/tsconfig.json
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
{
|
||||
"extends": "../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "dist",
|
||||
"rootDir": "src"
|
||||
},
|
||||
"include": ["src"],
|
||||
"references": [
|
||||
{ "path": "../base" },
|
||||
{ "path": "../mcp" }
|
||||
]
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue