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/resolve-prefix endpoint for short ID resolution

riley02274574503b2a6e
diff --git a/memory-layer/src/main/scala/ix/memory/api/EntityRoutes.scala b/memory-layer/src/main/scala/ix/memory/api/EntityRoutes.scala
index 20023d1b..e73e892e 100644
--- a/memory-layer/src/main/scala/ix/memory/api/EntityRoutes.scala
+++ b/memory-layer/src/main/scala/ix/memory/api/EntityRoutes.scala
@@ -35,6 +35,20 @@ class EntityRoutes(queryApi: GraphQueryApi) {
val routes: HttpRoutes[IO] = HttpRoutes.of[IO] {
+ // GET /v1/resolve-prefix/:prefix
+ case GET -> Root / "v1" / "resolve-prefix" / prefix =>
+ (for {
+ ids <- queryApi.resolvePrefix(prefix)
+ resp <- ids.size match {
+ case 0 => NotFound(Json.obj("error" -> s"No entity matches prefix: $prefix".asJson))
+ case 1 => Ok(Json.obj("id" -> ids.head.value.toString.asJson))
+ case _ => Ok(Json.obj(
+ "error" -> "ambiguous".asJson,
+ "matches" -> ids.map(_.value.toString).asJson
+ ))
+ }
+ } yield resp).handleErrorWith(ErrorHandler.handle(_))
+
// GET /v1/entity/:id
case GET -> Root / "v1" / "entity" / UUIDVar(id) =>
val nodeId = NodeId(id)
diff --git a/memory-layer/src/main/scala/ix/memory/db/ArangoGraphQueryApi.scala b/memory-layer/src/main/scala/ix/memory/db/ArangoGraphQueryApi.scala
index 2ecc1744..c773c10f 100644
--- a/memory-layer/src/main/scala/ix/memory/db/ArangoGraphQueryApi.scala
+++ b/memory-layer/src/main/scala/ix/memory/db/ArangoGraphQueryApi.scala
@@ -295,6 +295,19 @@ class ArangoGraphQueryApi(client: ArangoClient) extends GraphQueryApi {
)
).map(_.flatMap(parseNode).toVector)
+ override def resolvePrefix(prefix: String): IO[Vector[NodeId]] =
+ client.query(
+ """FOR n IN nodes
+ | FILTER n.deleted_rev == null
+ | AND STARTS_WITH(n.logical_id, @prefix)
+ | COLLECT lid = n.logical_id
+ | LIMIT 10
+ | RETURN lid""".stripMargin,
+ Map("prefix" -> prefix.asInstanceOf[AnyRef])
+ ).map(_.flatMap(_.asString).flatMap(s =>
+ scala.util.Try(java.util.UUID.fromString(s)).toOption.map(NodeId(_))
+ ).toVector)
+
// ── JSON Parsers (snake_case → camelCase) ───────────────────────────
private def parseNode(json: Json): Option[GraphNode] = {
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 b6b70432..044c0417 100644
--- a/memory-layer/src/main/scala/ix/memory/db/GraphQueryApi.scala
+++ b/memory-layer/src/main/scala/ix/memory/db/GraphQueryApi.scala
@@ -19,6 +19,7 @@ trait GraphQueryApi {
def getPatchesForEntity(entityId: NodeId): IO[List[Json]]
def getPatchesBySource(sourceUri: String, extractor: String): IO[Vector[Json]]
def getChangedEntities(fromRev: Rev, toRev: Rev): IO[Vector[GraphNode]]
+ def resolvePrefix(prefix: String): IO[Vector[NodeId]]
}
sealed trait Direction