Skip to content

Commit

Permalink
integrate cerebro and fix trpc link issues
Browse files Browse the repository at this point in the history
  • Loading branch information
hashwarp committed Oct 18, 2024
1 parent d6e7dc0 commit fb6bba2
Show file tree
Hide file tree
Showing 17 changed files with 235 additions and 206 deletions.
3 changes: 2 additions & 1 deletion .rush/temp/shrinkwrap-deps.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"../../packages/cli": "../../packages/cli:4nNWbeZzHgjRntdwc5L2pRZuwBJt6J7ljcWIAyXUw4g=:",
"../../packages/cli": "../../packages/cli:xZwKgNBciDhvxccitIFYrm0dqlHDs3DlAPHl9nE0D/U=:",
"/@ampproject/remapping/2.3.0": "sha512-30iZtAPgz+LTIYoeivqYo853f02jBYSd5uGnGpkFV0M3xOt9aN73erkgYAmZU43x4VfqcnLxW9Kpg3R5LC4YYw==",
"/@babel/code-frame/7.24.7": "sha512-BcYH1CVJBO9tvyIZ2jVeXgSIMvGZ2FDRvDdOIVQyuklNKSsx+eppDEBq/g47Ayw+RqNFE+URvOShmf+f/qwAlA==",
"/@babel/compat-data/7.25.2": "sha512-bYcppcpKBvX4znYaPEeFau03bp89ShqNMLs+rmdptMw+heSZh9+z84d2YG+K7cYLbWwzdjtDoW/uqZmPjulClQ==",
Expand Down Expand Up @@ -172,6 +172,7 @@
"/@sindresorhus/merge-streams/4.0.0": "sha512-tlqY9xq5ukxTUZBmoOp+m61cqwQD5pHJtFY3Mn8CA8ps6yghLH/Hw8UPdqg4OLmFW3IFlcXnQNmo/dh8HzXYIQ==",
"/@sinonjs/commons/3.0.1": "sha512-K3mCHKQ9sVh8o1C9cxkwxaOmXoAMlDxC1mYyHrjqOWEcBjYr76t96zL2zlj5dUGZ3HSw240X1qgH3Mjf1yJWpQ==",
"/@sinonjs/fake-timers/10.3.0": "sha512-V4BG07kuYSUkTCSBHG8G8TNhM+F19jXFWnQtzj+we8DrkpSBCee9Z3Ms8yiGer/dlmhe35/Xdgyo3/0rQKg7YA==",
"/@trpc/client/11.0.0-rc.586_@trpc+server@11.0.0-rc.586": "sha512-shCIpBzT+SzEbVXbCdpbSrPogG4c9J6hXh+xh5pidY1MTYcBHkeZVBLjy/fVSX+fB9wRoZXNaaoXO+ijYAZBcQ==",
"/@trpc/server/11.0.0-rc.586": "sha512-G0713HRFYyBLjN58DYq88hTH4kfKNZt9GXR0/TkVD7rENpOUBk6LKorqSDQ0y0/8aqu11HdDHsn6vBTWK3D44Q==",
"/@tsconfig/node10/1.0.11": "sha512-DcRjDCujK/kCk/cUe8Xz8ZSpm8mS3mNNpta+jGCA6USEDfktlNvm1+IuZ9eTcDbNk41BHwpHHeW+N1lKCz4zOw==",
"/@tsconfig/node12/1.0.11": "sha512-cqefuRsh12pWyGsIoBKJA9luFu3mRxCA+ORZvA4ktLSzIuCUtWVxGIuXigEwO5/ywWFMZ2QEGKWvkZG1zDMTag==",
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"test:watch": "jest --watch --collect-coverage --config=jest.unit.config.js",
"test:coverage": "jest --coverage",
"lint": "eslint --max-warnings=0 .",
"dev": "cd src && tsx cli.ts --interactive",
"cli": "cd src && tsx cli.ts",
"cli:math": "cd test/fixtures && tsx math.ts",
"cli:fs": "cd test/fixtures && tsx fs.ts",
Expand All @@ -27,6 +28,7 @@
"@arken/node": "workspace:*",
"@arken/seer-protocol": "workspace:*",
"@arken/evolution-protocol": "workspace:*",
"@arken/cerebro-protocol": "workspace:*",
"@types/jest": "^29",
"@types/node": "^20",
"jest": "^29",
Expand All @@ -36,6 +38,7 @@
"yargs": "^17",
"zod": "^3",
"@types/yargs": "^17",
"@trpc/client": "next",
"@trpc/server": "next",
"commander": "^12",
"enquirer": "^2",
Expand Down
4 changes: 2 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createCli } from "./";
import { router, combinedLink } from "./router";
import { router, link } from "./router";

void createCli({ router: router, link: combinedLink }).run();
void createCli({ router: router, link: link }).run();
41 changes: 26 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,25 +205,27 @@ export function createCli<R extends AnyRouter>({
const caller = createTRPCProxyClient<R>({
links: [link],
});

// console.log("argv", parsedArgv);
// Adjust the die function to handle interactive mode
const isInteractive =
parsedArgv.flags.interactive ||
parsedArgv._.length === 0 ||
!parsedArgv.command;

// console.log("vvv", isInteractive);
const die: Fail = (
message: string,
{ cause, help = true }: { cause?: unknown; help?: boolean } = {}
) => {
if (verboseErrors !== undefined && verboseErrors) {
console.log("throwing error");
throw (cause as Error) || new Error(message);
}
logger.error?.(colors.red(message));
if (help) {
parsedArgv.showHelp();
}
if (!isInteractive) {
console.log("exiting");
_process.exit(1);
}
};
Expand Down Expand Up @@ -296,7 +298,7 @@ export function createCli<R extends AnyRouter>({
}

const procedureInfo = command && procedureMap[command];

// console.log(procedureInfo);
if (!procedureInfo) {
const name = JSON.stringify(command || parsedArgv._[0]);
const message = name
Expand All @@ -305,14 +307,14 @@ export function createCli<R extends AnyRouter>({
return die(message);
}

if (Object.entries(parsedArgv.unknownFlags).length > 0) {
const s = Object.entries(parsedArgv.unknownFlags).length === 1 ? "" : "s";
return die(
`Unexpected flag${s}: ${Object.keys(parsedArgv.unknownFlags).join(
", "
)}`
);
}
// if (Object.entries(parsedArgv.unknownFlags).length > 0) {
// const s = Object.entries(parsedArgv.unknownFlags).length === 1 ? "" : "s";
// return die(
// `Unexpected flag${s}: ${Object.keys(parsedArgv.unknownFlags).join(
// ", "
// )}`
// );
// }

const incompatibleMessages = procedureInfo.incompatiblePairs
.filter(([a, b]) => a in flags && b in flags)
Expand All @@ -331,16 +333,23 @@ export function createCli<R extends AnyRouter>({
}) as never;

try {
// console.log("TRPC-CLI running command", procedureInfo);
// console.log("TRPC-CLI running command", procedureInfo, input);
const result: unknown = await (
caller[procedureInfo.name][procedureInfo.type] as Function
caller[procedureInfo.name][
procedureInfo.type === "query" ? "query" : "mutate"
] as Function
)(input);
if (result) logger.info?.(result);
if (!parsedArgv.flags.interactive) {
const isInteractive =
parsedArgv.flags.interactive ||
parsedArgv._.length === 0 ||
!parsedArgv.command;
if (!isInteractive) {
process.exit(0);
}
} catch (err) {
throw transformError(err, die);
console.log("zzz", err);
// throw transformError(err, die);
}
}

Expand Down Expand Up @@ -410,6 +419,8 @@ export function createCli<R extends AnyRouter>({
inputArgv
);

// console.log(parsedArgv);

parsedArgv.flags.interactive = true;

try {
Expand Down
3 changes: 2 additions & 1 deletion src/modules/application/application.cli.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { createCli } from "../../";
import Service from "./application.service";
import { createRouter } from "./application.router";
import { link } from "../../router";

const router = createRouter(new Service());

void createCli({ router }).run();
void createCli({ router, link }).run();
2 changes: 2 additions & 0 deletions src/modules/config/config.cli.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { createCli } from "../../";
import Service from "./config.service";
import { createRouter } from "./config.router";
import { link } from "../../router";

const router = createRouter(new Service());

void createCli({
router,
link,
alias: (name, { command }) => {
if (command === "ls") {
return "list";
Expand Down
3 changes: 2 additions & 1 deletion src/modules/help/help.cli.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { createCli } from "../..";
import Service from "./help.service";
import { createRouter } from "./help.router";
import { link } from "../../router";

const router = createRouter(new Service());

void createCli({ router }).run();
void createCli({ router, link }).run();
5 changes: 3 additions & 2 deletions src/modules/math/math.cli.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { createCli } from "../../";
import { createCli } from "../..";
import Service from "./math.service";
import { createRouter } from "./math.router";
import { link } from "../../router";

const router = createRouter(new Service());

void createCli({ router }).run();
void createCli({ router, link }).run();
25 changes: 16 additions & 9 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import {
createRouter as createSeerRouter,
Router as SeerRouter,
} from "@arken/seer-protocol";
import {
createRouter as createCerebroRouter,
Router as CerebroRouter,
} from "@arken/cerebro-protocol";
import { io as ioClient } from "socket.io-client";
import { serialize, deserialize } from "@arken/node/util/rpc";
import { generateShortId } from "@arken/node/util/db";
Expand All @@ -37,6 +41,7 @@ type MergedRouter = {
relay: RelayRouter;
evolution: EvolutionRouter;
seer: SeerRouter;
cerebro: CerebroRouter;
};

// Initialize tRPC with the merged context if needed
Expand All @@ -59,6 +64,7 @@ export const router = t.router<MergedRouter>({
relay: createRelayRouter(),
evolution: createEvolutionRouter(),
seer: createSeerRouter(),
cerebro: createCerebroRouter(),
});

export type AppRouter = typeof router;
Expand Down Expand Up @@ -137,6 +143,7 @@ const backends: BackendConfig[] = [
{ name: "relay", url: "http://localhost:8020" },
{ name: "evolution", url: "http://localhost:4010" },
{ name: "seer", url: "http://localhost:7060" },
{ name: "cerebro", url: "http://localhost:9010" },
];

// Initialize socket clients for each backend
Expand Down Expand Up @@ -241,7 +248,7 @@ backends.forEach((backend) => {
// Combined TRPC Link
// ======================

export const combinedLink: TRPCLink<any> =
export const link: TRPCLink<any> =
() =>
({ op, next }) => {
// Extract the router namespace from the operation path
Expand Down Expand Up @@ -300,11 +307,11 @@ export const combinedLink: TRPCLink<any> =
client.ioCallbacks[uuid] = {
timeout,
resolve: (response) => {
// console.log(
// `[${routerName} Link] Callback resolved:`,
// uuid,
// response
// );
console.log(
`[${routerName} Link] Callback resolved:`,
uuid,
response
);
clearTimeout(timeout);
if (response.error) {
observer.error(response.error);
Expand Down Expand Up @@ -361,9 +368,9 @@ export const combinedLink: TRPCLink<any> =
// Create a single tRPC instance

// Create the tRPC client with the combined link
export const trpcClient = createTRPCProxyClient<AppRouter>({
links: [combinedLink],
});
// export const trpcClient = createTRPCProxyClient<AppRouter>({
// links: [link],
// });

// export const trpcClient = trpc.createClient({
// links: [combinedLink],
Expand Down
Loading

0 comments on commit fb6bba2

Please sign in to comment.