gupta-sahil01 commented on code in PR #6436:
URL: https://github.com/apache/texera/pull/6436#discussion_r4687885121


##########
agent-service/src/server.ts:
##########
@@ -389,6 +401,65 @@ const agentsRouter = new Elysia({ prefix: "/agents" })
     }
   );
 
+// The process-wide operator catalog, loading it first if startup could not
+// (for example because the backend was not ready yet). Concurrent requests
+// share one load; a failed load is logged and retried on the next request, and
+// the caller proceeds with the catalog uninitialized.
+let operatorCatalogLoad: Promise<unknown> | null = null;
+
+async function getOperatorCatalog(): Promise<WorkflowSystemMetadata> {
+  const catalog = WorkflowSystemMetadata.getInstance();
+  if (!catalog.isInitialized()) {
+    operatorCatalogLoad ??= WorkflowSystemMetadata.initializeGlobal()
+      .catch(error => log.warn({ err: error }, "failed to load operator 
catalog; will retry on next request"))
+      .finally(() => {
+        operatorCatalogLoad = null;
+      });
+    await operatorCatalogLoad;
+  }
+  return WorkflowSystemMetadata.getInstance();
+}
+
+// Stateless operator recommender (apache/texera#5240). Given the operator a
+// user just added, returns a short ranked list of likely next operators for 
the
+// canvas to render as suggestions. Version 1 uses a hardcoded rule table with
+// no LLM call, but still requires the signed-in user's token, as agent 
creation
+// does, so that Version 2 can call the LLM gateway as that user without
+// changing the auth contract.
+const operatorRecommendationsRouter = new Elysia({ prefix: 
"/operator-recommendations" })
+  // Local-scoped error handler, mirroring agentsRouter: body-schema and
+  // malformed-JSON failures are client errors (400), not 500s.
+  .onError(({ code, error, set }) => {
+    log.error({ err: error }, "operator recommendation request error");
+    const errorMessage = error instanceof Error ? error.message : 
String(error);
+    if (code === "VALIDATION" || code === "PARSE") {
+      set.status = 400;
+      return { error: errorMessage || "Invalid request body" };
+    }
+    set.status = ERROR_STATUS[errorMessage] ?? 500;
+    return { error: errorMessage || "Internal server error" };
+  })
+  .post(
+    "/",
+    async ({ body, headers, set }) => {
+      requireUserToken(headers.authorization);
+      const req = body as OperatorRecommendationRequest;
+      const operatorType = req.operatorType.trim();
+      if (!operatorType) {
+        set.status = 400;
+        return { error: "operatorType must be a non-empty string" };
+      }
+      return recommendOperators({ ...req, operatorType }, await 
getOperatorCatalog());
+    },
+    {
+      body: t.Object({
+        operatorType: t.String({ minLength: 1, maxLength: 
MAX_OPERATOR_TYPE_LENGTH }),
+        sourcePortID: t.Optional(t.String({ pattern: "^output-[0-9]+$" })),

Review Comment:
   Done in 4ba64c9a0. `sourcePortID` is capped at 16 characters and must match 
`^output-(0|[1-9][0-9]*)$`. The route validator and the recommender share that 
one pattern constant, so `output-00` and `output-01` are rejected with 400 
rather than read as port 0.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to