Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright (c) 2026, WSO2 LLC. (http://www.wso2.com)
*
* WSO2 LLC. 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.
*/

package io.ballerina.flowmodelgenerator.core;

import io.ballerina.compiler.api.SemanticModel;
import io.ballerina.compiler.api.symbols.ClassSymbol;
import io.ballerina.compiler.api.symbols.Qualifier;
import io.ballerina.flowmodelgenerator.core.model.Client;
import io.ballerina.flowmodelgenerator.core.model.LibraryFunction;
import io.ballerina.flowmodelgenerator.core.model.TypeDef;
import io.ballerina.modelgenerator.commons.FunctionData;
import io.ballerina.modelgenerator.commons.FunctionDataBuilder;
import io.ballerina.modelgenerator.commons.ModuleInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
* Handler for processing CLASS symbols from semantic model.
* Extracts client classes and normal classes with their methods.
*
* @since 1.0.1
*/
public class ClassSymbolHandler {

private ClassSymbolHandler() {
// Prevent instantiation
}

/**
* Processes a CLASS symbol and returns either a Client or TypeDef.
*
* @param classSymbol the class symbol to process
* @param semanticModel the semantic model
* @param moduleInfo the module information
* @param org the organization name
* @param packageName the package name
* @return Optional containing either Client or TypeDef, or empty if not public
*/
public static Optional<Object> process(ClassSymbol classSymbol,
SemanticModel semanticModel,
ModuleInfo moduleInfo,
String org,
String packageName) {
// Process only PUBLIC classes: CLIENT classes (connectors) and normal classes
if (!classSymbol.qualifiers().contains(Qualifier.PUBLIC)) {
return Optional.empty();
}

boolean isClient = classSymbol.qualifiers().contains(Qualifier.CLIENT);
String className = classSymbol.getName().orElse(isClient ? "Client" : "Class");

FunctionData.Kind classKind = isClient ? FunctionData.Kind.CONNECTOR : FunctionData.Kind.CLASS_INIT;

FunctionData classData = new FunctionDataBuilder()
.semanticModel(semanticModel)
.moduleInfo(moduleInfo)
.name(className)
.parentSymbol(classSymbol)
.functionResultKind(classKind)
.build();

List<LibraryFunction> functions = new ArrayList<>();

// Add the constructor/init function first
LibraryFunction constructor = LibraryModelConverter.functionDataToModel(classData, org, packageName);
constructor.setName("init"); // Override name to "init" for constructor
functions.add(constructor);

// Then add all other methods (remote functions, resource functions, etc.)
List<FunctionData> classMethods = new FunctionDataBuilder()
.semanticModel(semanticModel)
.moduleInfo(moduleInfo)
.parentSymbolType(className)
.parentSymbol(classSymbol)
.buildChildNodes();

for (FunctionData method : classMethods) {
LibraryFunction methodFunc = LibraryModelConverter.functionDataToModel(method, org, packageName);
functions.add(methodFunc);
}

if (isClient) {
Client client = new Client(className, classData.description());
client.setFunctions(functions);
return Optional.of(client);
} else {
TypeDef typeDef = new TypeDef();
typeDef.setName(className);
typeDef.setDescription(classData.description());
typeDef.setFunctions(functions);
return Optional.of(typeDef);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2026, WSO2 LLC. (http://www.wso2.com)
*
* WSO2 LLC. 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.
*/

package io.ballerina.flowmodelgenerator.core;

import io.ballerina.compiler.api.symbols.ConstantSymbol;
import io.ballerina.compiler.api.symbols.Qualifier;
import io.ballerina.compiler.api.symbols.TypeSymbol;
import io.ballerina.compiler.api.values.ConstantValue;
import io.ballerina.flowmodelgenerator.core.model.Type;
import io.ballerina.flowmodelgenerator.core.model.TypeDef;
import io.ballerina.modelgenerator.commons.TypeDefData;

import java.util.Optional;

/**
* Handler for processing CONSTANT symbols from semantic model.
*
* @since 1.0.1
*/
public class ConstantSymbolHandler {

private ConstantSymbolHandler() {
}

public static Optional<TypeDef> process(ConstantSymbol constantSymbol,
String org,
String packageName) {
if (!constantSymbol.qualifiers().contains(Qualifier.PUBLIC)) {
return Optional.empty();
}

TypeDefData constantData = TypeDefDataBuilder.buildFromConstant(constantSymbol);
TypeDef typeDef = LibraryModelConverter.typeDefDataToModel(constantData, org, packageName);

// Add varType using ConstantValue
String varTypeName = "";
Object constValue = constantSymbol.constValue();
if (constValue instanceof ConstantValue constantValue) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check if theres a kind or type which we can do instanceof. There is, we should follow that approach

varTypeName = constantValue.valueType().typeKind().getName();
}

// Fallback to type descriptor if constValue is null or not ConstantValue
if (varTypeName.isEmpty()) {
TypeSymbol typeSymbol = constantSymbol.typeDescriptor();
if (typeSymbol != null && !typeSymbol.signature().isEmpty()) {
varTypeName = typeSymbol.signature();
}
}

Type varType = new Type(varTypeName);
typeDef.setVarType(varType);

return Optional.of(typeDef);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* Copyright (c) 2026, WSO2 LLC. (http://www.wso2.com)
*
* WSO2 LLC. 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.
*/

package io.ballerina.flowmodelgenerator.core;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import io.ballerina.compiler.api.SemanticModel;
import io.ballerina.flowmodelgenerator.core.model.Library;
import io.ballerina.flowmodelgenerator.core.model.Service;
import io.ballerina.modelgenerator.commons.ModuleInfo;
import io.ballerina.modelgenerator.commons.PackageUtil;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
* Core orchestrator for Copilot library operations.
* Coordinates between database access, symbol processing, and service loading.
*
* @since 1.0.1
*/
public class CopilotLibraryManager {

private static final Gson GSON = new Gson();

/**
* Loads all libraries from the database.
* Returns a list of libraries with name and description only.
*
* @return List of Library objects containing name and description
*/
public List<Library> loadLibrariesFromDatabase() {
List<Library> libraries = new ArrayList<>();

try {
Map<String, String> packageToDescriptionMap = LibraryDatabaseAccessor.loadAllPackages();

for (Map.Entry<String, String> entry : packageToDescriptionMap.entrySet()) {
Library library = new Library(entry.getKey(), entry.getValue());
libraries.add(library);
}
} catch (IOException | SQLException e) {
throw new RuntimeException("Failed to load libraries from database: " + e.getMessage(), e);
}

return libraries;
}

/**
* Loads filtered libraries using the semantic model.
* Returns libraries with full details including clients, functions, typedefs, and services.
*
* @param libraryNames Array of library names in "org/package_name" format to filter
* @return List of Library objects with complete information
*/
public List<Library> loadFilteredLibraries(String[] libraryNames) {
List<Library> libraries = new ArrayList<>();

for (String libraryName : libraryNames) {
// Parse library name "org/package_name"
String[] parts = libraryName.split("/");
if (parts.length != 2) {
continue; // Skip invalid format
}
String org = parts[0];
String packageName = parts[1];

// Create module info (use latest version by passing null)
ModuleInfo moduleInfo = new ModuleInfo(org, packageName, org + "/" +
packageName, null);

// Get semantic model for the module
Optional<SemanticModel> optSemanticModel = PackageUtil.getSemanticModel(org, packageName);
if (optSemanticModel.isEmpty()) {
continue; // Skip if semantic model not found
}

SemanticModel semanticModel = optSemanticModel.get();

// Get the package description from database
String description = LibraryDatabaseAccessor.getPackageDescription(org, packageName).orElse("");

// Create library object
Library library = new Library(libraryName, description);

// Process module symbols to extract clients, functions, and typedefs
SymbolProcessor.SymbolProcessingResult symbolResult = SymbolProcessor.processModuleSymbols(
semanticModel,
moduleInfo,
org,
packageName
);

library.setClients(symbolResult.getClients());
library.setFunctions(symbolResult.getFunctions());
library.setTypeDefs(symbolResult.getTypeDefs());

// Load services from both inbuilt triggers and generic services
// For now, keep using JSON and convert to Service POJOs
JsonArray servicesJson = ServiceLoader.loadAllServices(libraryName);
List<Service> services = new ArrayList<>();
for (JsonElement serviceElement : servicesJson) {
Service service = GSON.fromJson(serviceElement, Service.class);
services.add(service);
}
library.setServices(services);

libraries.add(library);
}

return libraries;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2026, WSO2 LLC. (http://www.wso2.com)
*
* WSO2 LLC. 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.
*/

package io.ballerina.flowmodelgenerator.core;

import io.ballerina.compiler.api.symbols.EnumSymbol;
import io.ballerina.compiler.api.symbols.Qualifier;
import io.ballerina.flowmodelgenerator.core.model.TypeDef;
import io.ballerina.modelgenerator.commons.TypeDefData;

import java.util.Optional;

/**
* Handler for processing ENUM symbols from semantic model.
*
* @since 1.0.1
*/
public class EnumSymbolHandler {

private EnumSymbolHandler() {
}

public static Optional<TypeDef> process(EnumSymbol enumSymbol,
String org,
String packageName) {
if (!enumSymbol.qualifiers().contains(Qualifier.PUBLIC)) {
return Optional.empty();
}

TypeDefData enumData = TypeDefDataBuilder.buildFromEnum(enumSymbol);
TypeDef typeDef = LibraryModelConverter.typeDefDataToModel(enumData, org, packageName);
return Optional.of(typeDef);
}
}
Loading
Loading