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


##########
agent-service/src/operator-recommendation/operator-recommender.ts:
##########
@@ -0,0 +1,127 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import {
+  DEFAULT_OPERATOR_RECOMMENDATION_LIMIT,
+  DEFAULT_SOURCE_PORT_ID,
+  MAX_OPERATOR_RECOMMENDATION_LIMIT,
+  type OperatorRecommendation,
+  type OperatorRecommendationRequest,
+  type OperatorRecommendationResponse,
+} from "./operator-recommendation-types";
+import {
+  candidateSuccessors,
+  categoryOf,
+  rationaleFor,
+  targetPortIdFor,
+  type OperatorCatalogEntry,
+} from "./operator-recommendation-rules";
+
+/**
+ * Minimal read-only view of the operator catalog the recommender needs. The
+ * `WorkflowSystemMetadata` singleton satisfies this structurally; tests can 
pass
+ * a stub (or omit it entirely to exercise the uninitialized path).
+ */
+export interface OperatorCatalog {
+  isInitialized(): boolean;
+  getSchema(operatorType: string): unknown;
+  getAdditionalMetadata(operatorType: string): OperatorCatalogEntry | 
undefined;
+}
+
+// Top suggestion scores 0.9 and each lower rank drops one step, floored at 
0.3.
+// With MAX_OPERATOR_RECOMMENDATION_LIMIT = 5 this yields 0.9 / 0.75 / 0.6 / 
0.45 / 0.3.
+const TOP_SCORE = 0.9;
+const SCORE_STEP = 0.15;
+const MIN_SCORE = 0.3;
+
+const OUTPUT_PORT_ID = /^output-(\d+)$/;
+
+function scoreForRank(rank: number): number {
+  return Math.max(MIN_SCORE, +(TOP_SCORE - rank * SCORE_STEP).toFixed(2));
+}
+
+function clampLimit(limit: number | undefined): number {
+  if (limit === undefined || !Number.isFinite(limit)) return 
DEFAULT_OPERATOR_RECOMMENDATION_LIMIT;
+  return Math.min(MAX_OPERATOR_RECOMMENDATION_LIMIT, Math.max(1, 
Math.floor(limit)));
+}
+
+/** Whether `portId` names an output port the operator actually has. */
+function hasOutputPort(entry: OperatorCatalogEntry | undefined, portId: 
string): boolean {
+  const match = OUTPUT_PORT_ID.exec(portId);
+  if (!match) return false;
+  // Without metadata, or with user-added ports, any well-formed port may 
exist.
+  if (!entry?.outputPorts || entry.dynamicOutputPorts) return true;
+  return Number(match[1]) < entry.outputPorts.length;
+}
+
+/**
+ * Version 1 hardcoded recommender: given the operator just added, return a
+ * short ranked list of likely successors drawn from {@link 
candidateSuccessors}.
+ *
+ * The result is deterministic and requires no LLM call. When a `catalog` is
+ * supplied and initialized, the operator is classified from its metadata,
+ * suggestions are filtered to operator types that actually exist (so a stale
+ * rule never yields an unusable suggestion), and each suggestion is enriched
+ * with its display name and the input port to wire to. Without an initialized
+ * catalog, only the per-operator overrides and the generic fallback apply, and
+ * the raw ranked candidates are returned unvalidated.
+ */
+export function recommendOperators(
+  req: OperatorRecommendationRequest,
+  catalog?: OperatorCatalog
+): OperatorRecommendationResponse {
+  const operatorType = req.operatorType?.trim();
+  const limit = clampLimit(req.limit);
+  const empty: OperatorRecommendationResponse = { recommendations: [], 
strategy: "hardcoded" };
+
+  if (!operatorType) return empty;
+
+  const catalogReady = catalog?.isInitialized() ?? false;

Review Comment:
   Done in 4ba64c9a0. The recommender now returns an empty list whenever the 
catalog is not loaded, or does not contain the requested operator or output 
port, including operators with a per-operator rule, since their ports cannot be 
checked either. Tests cover a backend 503 (Filter, Bar Chart, Training: 
Logistic Regression all return nothing) and unknown operator types on any port.
   



-- 
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