@superatomai/sdk-node 0.0.49 → 0.0.52

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -5670,26 +5670,27 @@ ${JSON.stringify(tool.requiredFields || [], null, 2)}`;
5670
5670
  if (executedTools && executedTools.length > 0) {
5671
5671
  logger.info(`[${this.getProviderName()}] Passing ${executedTools.length} executed tools to component matching`);
5672
5672
  executedToolsText = "The following external tools were executed to fetch data.\n**IMPORTANT: For components displaying this data, use externalTool prop instead of query.**\n**IMPORTANT: Use the outputSchema fields to set correct config keys (xAxisKey, yAxisKey, valueKey, nameKey, etc.)**\n**IMPORTANT: Use the result data to populate deferred tool parameters when applicable.**\n\n" + executedTools.map((tool, idx) => {
5673
- let resultPreview = "No result data";
5674
- if (tool.result) {
5675
- const resultStr = typeof tool.result === "string" ? tool.result : JSON.stringify(tool.result, null, 2);
5676
- resultPreview = resultStr.length > 2e3 ? resultStr.substring(0, 2e3) + "\n... (truncated)" : resultStr;
5677
- }
5678
5673
  let outputSchemaText = "Not available";
5674
+ let recordCount = "unknown";
5679
5675
  if (tool.outputSchema) {
5680
5676
  const fields = tool.outputSchema.fields || [];
5681
- const recordCount = tool.result?._recordCount || (Array.isArray(tool.result) ? tool.result.length : "unknown");
5682
- outputSchemaText = `Returns ARRAY of ${recordCount} records. ${tool.outputSchema.description}
5683
- Fields in EACH ROW (these are per-row values, NOT aggregates):
5684
- ${fields.map((f) => ` - ${f.name} (${f.type}): ${f.description}`).join("\n")}
5685
- NOTE: For aggregate values (SUM, COUNT, AVG across all rows), write SQL query instead.`;
5677
+ recordCount = tool.result?._recordCount || (Array.isArray(tool.result) ? tool.result.length : "unknown");
5678
+ const fieldsText = fields.map(
5679
+ (f) => ` - name: "${f.name}" \u2190 USE THIS EXACT VALUE in config
5680
+ type: ${f.type}
5681
+ description: ${f.description}`
5682
+ ).join("\n");
5683
+ outputSchemaText = `${tool.outputSchema.description}
5684
+ Fields (use the "name" value in your config):
5685
+ ${fieldsText}`;
5686
5686
  }
5687
5687
  return `${idx + 1}. **${tool.name}**
5688
5688
  toolId: "${tool.id}" (USE THIS EXACT VALUE for externalTool.toolId)
5689
5689
  toolName: "${tool.name}" (USE THIS EXACT VALUE for externalTool.toolName)
5690
5690
  parameters: ${JSON.stringify(tool.params || {})} (USE THESE for externalTool.parameters)
5691
+ recordCount: ${recordCount} rows returned
5691
5692
  outputSchema: ${outputSchemaText}
5692
- result preview: ${resultPreview}`;
5693
+ \u26A0\uFE0F DO NOT embed this tool's data in SQL - use externalTool prop OR query database tables`;
5693
5694
  }).join("\n\n");
5694
5695
  }
5695
5696
  const schemaDoc = schema.generateSchemaDocumentation();
@@ -5830,11 +5831,111 @@ ${fields.map((f) => ` - ${f.name} (${f.type}): ${f.description}`).join("\n")
5830
5831
  logger.warn(`[${this.getProviderName()}] Component ${mc.componentId} not found in available components`);
5831
5832
  return null;
5832
5833
  }
5834
+ let cleanedProps = { ...mc.props };
5835
+ if (cleanedProps.externalTool) {
5836
+ const toolId = cleanedProps.externalTool.toolId;
5837
+ const validToolIds = (executedTools || []).map((t) => t.id);
5838
+ const isValidTool = toolId && typeof toolId === "string" && validToolIds.includes(toolId);
5839
+ if (!isValidTool) {
5840
+ logger.warn(`[${this.getProviderName()}] externalTool.toolId "${toolId}" not found in executed tools [${validToolIds.join(", ")}], setting to null`);
5841
+ cleanedProps.externalTool = null;
5842
+ } else {
5843
+ const executedTool = executedTools?.find((t) => t.id === toolId);
5844
+ if (executedTool?.outputSchema?.fields && cleanedProps.config) {
5845
+ const validFieldNames = executedTool.outputSchema.fields.map((f) => f.name);
5846
+ const validFieldNamesLower = validFieldNames.map((n) => n.toLowerCase());
5847
+ const findMatchingField = (fieldName) => {
5848
+ if (!fieldName) return null;
5849
+ const lowerField = fieldName.toLowerCase();
5850
+ const exactIdx = validFieldNamesLower.indexOf(lowerField);
5851
+ if (exactIdx !== -1) return validFieldNames[exactIdx];
5852
+ const partialMatches = [];
5853
+ for (let i = 0; i < validFieldNames.length; i++) {
5854
+ const validName = validFieldNames[i];
5855
+ const validLower = validFieldNamesLower[i];
5856
+ if (validLower.endsWith(lowerField)) {
5857
+ const score = lowerField.length / validLower.length;
5858
+ partialMatches.push({ name: validName, score });
5859
+ continue;
5860
+ }
5861
+ if (lowerField.endsWith(validLower)) {
5862
+ const score = validLower.length / lowerField.length;
5863
+ partialMatches.push({ name: validName, score });
5864
+ continue;
5865
+ }
5866
+ if (validLower.endsWith(lowerField) || lowerField.length <= 5 && validLower.includes(lowerField)) {
5867
+ const score = lowerField.length <= 5 ? 0.3 : 0.5;
5868
+ partialMatches.push({ name: validName, score });
5869
+ }
5870
+ }
5871
+ if (partialMatches.length > 0) {
5872
+ partialMatches.sort((a, b) => b.score - a.score);
5873
+ if (partialMatches.length === 1 || partialMatches[0].score > partialMatches[1].score + 0.1) {
5874
+ return partialMatches[0].name;
5875
+ } else {
5876
+ logger.warn(`[${this.getProviderName()}] Ambiguous field match for "${fieldName}": ${partialMatches.map((m) => m.name).join(", ")}`);
5877
+ return null;
5878
+ }
5879
+ }
5880
+ return null;
5881
+ };
5882
+ const configFieldsToValidate = [
5883
+ "xAxisKey",
5884
+ "yAxisKey",
5885
+ "valueKey",
5886
+ "nameKey",
5887
+ "labelKey",
5888
+ "groupBy",
5889
+ "aggregationField",
5890
+ "seriesKey",
5891
+ "sizeKey",
5892
+ "xAggregationField",
5893
+ "yAggregationField"
5894
+ ];
5895
+ for (const configKey of configFieldsToValidate) {
5896
+ const fieldValue = cleanedProps.config[configKey];
5897
+ if (fieldValue && typeof fieldValue === "string") {
5898
+ if (!validFieldNames.includes(fieldValue)) {
5899
+ const correctedField = findMatchingField(fieldValue);
5900
+ if (correctedField) {
5901
+ logger.warn(`[${this.getProviderName()}] Correcting config.${configKey}: "${fieldValue}" \u2192 "${correctedField}"`);
5902
+ cleanedProps.config[configKey] = correctedField;
5903
+ } else {
5904
+ logger.warn(`[${this.getProviderName()}] config.${configKey}="${fieldValue}" not found in outputSchema [${validFieldNames.join(", ")}]`);
5905
+ }
5906
+ }
5907
+ }
5908
+ }
5909
+ if (Array.isArray(cleanedProps.config.series)) {
5910
+ cleanedProps.config.series = cleanedProps.config.series.map((s) => {
5911
+ if (s.dataKey && typeof s.dataKey === "string" && !validFieldNames.includes(s.dataKey)) {
5912
+ const correctedField = findMatchingField(s.dataKey);
5913
+ if (correctedField) {
5914
+ logger.warn(`[${this.getProviderName()}] Correcting series.dataKey: "${s.dataKey}" \u2192 "${correctedField}"`);
5915
+ return { ...s, dataKey: correctedField };
5916
+ }
5917
+ }
5918
+ return s;
5919
+ });
5920
+ }
5921
+ }
5922
+ }
5923
+ }
5924
+ if (cleanedProps.query) {
5925
+ const queryStr = typeof cleanedProps.query === "string" ? cleanedProps.query : cleanedProps.query?.sql || "";
5926
+ if (queryStr.includes("OPENJSON") || queryStr.includes("JSON_VALUE")) {
5927
+ logger.warn(`[${this.getProviderName()}] Query contains OPENJSON/JSON_VALUE (invalid - cannot parse tool result), setting query to null`);
5928
+ cleanedProps.query = null;
5929
+ }
5930
+ }
5931
+ if (cleanedProps.query && cleanedProps.externalTool) {
5932
+ logger.info(`[${this.getProviderName()}] Both query and externalTool exist, keeping both - frontend will decide`);
5933
+ }
5833
5934
  return {
5834
5935
  ...originalComponent,
5835
5936
  props: {
5836
5937
  ...originalComponent.props,
5837
- ...mc.props
5938
+ ...cleanedProps
5838
5939
  }
5839
5940
  };
5840
5941
  }).filter(Boolean);