Skip to content

armgit / Ix

Public Imported from GitHub
0

Understand any codebase instantly. System intelligence for codebases, built for humans and AI.

feat: add /v1/expand endpoint and ix callers/callees commands

riley02274b70d284d08f
diff --git a/ix-cli/src/cli/commands/callers.ts b/ix-cli/src/cli/commands/callers.ts
new file mode 100644
index 00000000..593ef4e9
--- /dev/null
+++ b/ix-cli/src/cli/commands/callers.ts
@@ -0,0 +1,42 @@
+import type { Command } from "commander";
+import { IxClient } from "../../client/api.js";
+import { getEndpoint } from "../config.js";
+import { formatEdgeResults } from "../format.js";
+
+export function registerCallersCommand(program: Command): void {
+ program
+ .command("callers <symbol>")
+ .description("Show methods/functions that call the given symbol")
+ .option("--format <fmt>", "Output format (text|json)", "text")
+ .action(async (symbol: string, opts: { format: string }) => {
+ const client = new IxClient(getEndpoint());
+
+ const nodes = await client.search(symbol, { limit: 1 });
+ if (nodes.length === 0) {
+ console.log(`No entity found matching "${symbol}".`);
+ return;
+ }
+ const entityId = (nodes[0] as any).id;
+
+ const result = await client.expand(entityId, { direction: "in", predicates: ["CALLS"] });
+ formatEdgeResults(result.nodes, "callers", symbol, opts.format);
+ });
+
+ program
+ .command("callees <symbol>")
+ .description("Show methods/functions called by the given symbol")
+ .option("--format <fmt>", "Output format (text|json)", "text")
+ .action(async (symbol: string, opts: { format: string }) => {
+ const client = new IxClient(getEndpoint());
+
+ const nodes = await client.search(symbol, { limit: 1 });
+ if (nodes.length === 0) {
+ console.log(`No entity found matching "${symbol}".`);
+ return;
+ }
+ const entityId = (nodes[0] as any).id;
+
+ const result = await client.expand(entityId, { direction: "out", predicates: ["CALLS"] });
+ formatEdgeResults(result.nodes, "callees", symbol, opts.format);
+ });
+}
diff --git a/ix-cli/src/cli/format.ts b/ix-cli/src/cli/format.ts
index 12f39da5..ee4e105b 100644
--- a/ix-cli/src/cli/format.ts
+++ b/ix-cli/src/cli/format.ts
@@ -268,6 +268,23 @@ export function formatLocateResults(results: LocateResult[], format: string): vo
}
}
+export function formatEdgeResults(nodes: any[], relation: string, symbol: string, format: string): void {
+ if (format === "json") {
+ console.log(JSON.stringify(nodes, null, 2));
+ return;
+ }
+ if (nodes.length === 0) {
+ console.log(`No ${relation} found for "${symbol}".`);
+ return;
+ }
+ console.log(`${chalk.bold(relation)} of ${chalk.cyan(symbol)}:`);
+ for (const n of nodes) {
+ const shortId = n.id?.slice(0, 8) ?? "";
+ const name = n.attrs?.name ?? n.name ?? n.id;
+ console.log(` ${chalk.cyan(n.kind)} ${chalk.dim(shortId)} ${name}`);
+ }
+}
+
export function formatConflicts(conflicts: any[], format: string): void {
if (format === "json") {
console.log(JSON.stringify(conflicts, null, 2));
diff --git a/ix-cli/src/cli/main.ts b/ix-cli/src/cli/main.ts
index 7a357dac..0bb616fa 100644
--- a/ix-cli/src/cli/main.ts
+++ b/ix-cli/src/cli/main.ts
@@ -17,6 +17,7 @@ import { registerMcpInstallCommand } from "./commands/mcp-install.js";
import { registerTextCommand } from "./commands/text.js";
import { registerLocateCommand } from "./commands/locate.js";
import { registerExplainCommand } from "./commands/explain.js";
+import { registerCallersCommand } from "./commands/callers.js";
const program = new Command();
program
@@ -41,6 +42,7 @@ registerMcpInstallCommand(program);
registerTextCommand(program);
registerLocateCommand(program);
registerExplainCommand(program);
+registerCallersCommand(program);
program
.command("mcp-start")
diff --git a/ix-cli/src/client/api.ts b/ix-cli/src/client/api.ts
index 57d15f28..6825e360 100644
--- a/ix-cli/src/client/api.ts
+++ b/ix-cli/src/client/api.ts
@@ -69,6 +69,18 @@ export class IxClient {
return this.get(`/v1/entity/${id}`);
}
+ async expand(
+ id: string,
+ opts?: { direction?: string; predicates?: string[]; hops?: number }
+ ): Promise<{ nodes: any[]; edges: any[] }> {
+ return this.post("/v1/expand", {
+ nodeId: id,
+ direction: opts?.direction ?? "both",
+ predicates: opts?.predicates,
+ hops: opts?.hops ?? 1,
+ });
+ }
+
async listTruth(): Promise<GraphNode[]> {
return this.get("/v1/truth");
}
diff --git a/memory-layer/src/main/scala/ix/memory/api/ExpandRoutes.scala b/memory-layer/src/main/scala/ix/memory/api/ExpandRoutes.scala
new file mode 100644
index 00000000..e1290276
--- /dev/null
+++ b/memory-layer/src/main/scala/ix/memory/api/ExpandRoutes.scala
@@ -0,0 +1,47 @@
+package ix.memory.api
+
+import java.util.UUID
+
+import cats.effect.IO
+import io.circe.{Decoder, Encoder}
+import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
+import io.circe.syntax._
+import org.http4s.HttpRoutes
+import org.http4s.circe.CirceEntityCodec._
+import org.http4s.dsl.io._
+
+import ix.memory.db.{Direction, ExpandResult, GraphQueryApi}
+import ix.memory.model._
+
+case class ExpandRequest(
+ nodeId: String,
+ direction: Option[String] = None,
+ predicates: Option[List[String]] = None,
+ hops: Option[Int] = None,
+ asOfRev: Option[Long] = None
+)
+
+object ExpandRequest {
+ implicit val decoder: Decoder[ExpandRequest] = deriveDecoder[ExpandRequest]
+}
+
+class ExpandRoutes(queryApi: GraphQueryApi) {
+
+ val routes: HttpRoutes[IO] = HttpRoutes.of[IO] {
+ case req @ POST -> Root / "v1" / "expand" =>
+ (for {
+ body <- req.as[ExpandRequest]
+ nodeId <- IO.fromOption(
+ scala.util.Try(UUID.fromString(body.nodeId)).toOption.map(NodeId(_))
+ )(new IllegalArgumentException(s"Invalid node ID: ${body.nodeId}"))
+ dir = body.direction match {
+ case Some("in") => Direction.In
+ case Some("out") => Direction.Out
+ case _ => Direction.Both
+ }
+ preds = body.predicates.map(_.toSet)
+ result <- queryApi.expand(nodeId, dir, preds, body.hops.getOrElse(1), body.asOfRev.map(Rev(_)))
+ resp <- Ok(result.asJson)
+ } yield resp).handleErrorWith(ErrorHandler.handle(_))
+ }
+}
diff --git a/memory-layer/src/main/scala/ix/memory/api/Routes.scala b/memory-layer/src/main/scala/ix/memory/api/Routes.scala
index e4283e31..9d507a43 100644
--- a/memory-layer/src/main/scala/ix/memory/api/Routes.scala
+++ b/memory-layer/src/main/scala/ix/memory/api/Routes.scala
@@ -40,9 +40,10 @@ object Routes {
val truthRoutes = new TruthRoutes(writeApi, queryApi).routes
val patchRoutes = new PatchRoutes(client).routes
val decisionListRoutes = new DecisionRoutes(queryApi).routes
+ val expandRoutes = new ExpandRoutes(queryApi).routes
health <+> contextRoutes <+> ingestionRoutes <+> entityRoutes <+>
diffRoutes <+> conflictRoutes <+> decideRoutes <+> searchRoutes <+>
- truthRoutes <+> patchRoutes <+> decisionListRoutes
+ truthRoutes <+> patchRoutes <+> decisionListRoutes <+> expandRoutes
}
}
diff --git a/memory-layer/src/main/scala/ix/memory/db/GraphQueryApi.scala b/memory-layer/src/main/scala/ix/memory/db/GraphQueryApi.scala
index e8c32c79..9c8447d4 100644
--- a/memory-layer/src/main/scala/ix/memory/db/GraphQueryApi.scala
+++ b/memory-layer/src/main/scala/ix/memory/db/GraphQueryApi.scala
@@ -31,3 +31,9 @@ object Direction {
}
final case class ExpandResult(nodes: Vector[GraphNode], edges: Vector[GraphEdge])
+
+object ExpandResult {
+ import io.circe.Encoder
+ import io.circe.generic.semiauto.deriveEncoder
+ implicit val encoder: Encoder[ExpandResult] = deriveEncoder[ExpandResult]
+}