Repository navigation
Studio ai #3574
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Studio ai #3574
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
7b24d1b
feat: add ArcadeDBi AI assistant to Studio
lvca 5c198d8
Update ai.html
lvca cf05aa4
feat: parallel query
lvca 4f65136
fx: fixed parallel query: avoided when can deadlock
lvca ebfd666
Added test file
lvca 5acb644
Improvements
lvca d2efe9f
Merge branch 'main' into studio-ai
lvca 02ca773
studio: Add ai profiler
lvca 9e93554
Implemented Profiler AI Analyze
lvca ad29165
Misc improvements
lvca 47098cc
fix: map Cypher to OpenCypher when the old cypher module (on gremlin)…
lvca 739c69e
fix: display button at the bottom of the code block now
lvca 670ecb1
removed ai.json cfg file
lvca 7f14fcc
Improved activation with disclaimer
lvca a3fa197
Fixed studio AI panel
lvca 201f0ea
studio: 2 modes of execution in ai panel
lvca 0129fc4
Merge branch 'main' into studio-ai
lvca dbe90bb
Improved agentic system
lvca 2f8fbb7
Merge branch 'main' into studio-ai
lvca 96a59e6
studio: added Drop Type button
lvca e08f80f
feat: studio -> double confirmation on database deletion
lvca 9f58abe
Merge branch 'main' into studio-ai
lvca File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -560,3 +560,4 @@ dist | |
| /.claude/worktrees | ||
| /server/profiler | ||
| /server/chats | ||
| config/ai.json | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
server/src/main/java/com/arcadedb/server/ai/AiActivateHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| /* | ||
| * Copyright © 2021-present Arcade Data Ltd (info@arcadedata.com) | ||
| * | ||
| * Licensed 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. | ||
| * | ||
| * SPDX-FileCopyrightText: 2021-present Arcade Data Ltd (info@arcadedata.com) | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package com.arcadedb.server.ai; | ||
|
|
||
| import com.arcadedb.Constants; | ||
| import com.arcadedb.log.LogManager; | ||
| import com.arcadedb.serializer.json.JSONObject; | ||
| import com.arcadedb.server.http.HttpServer; | ||
| import com.arcadedb.server.http.handler.AbstractServerHttpHandler; | ||
| import com.arcadedb.server.http.handler.ExecutionResponse; | ||
| import com.arcadedb.server.security.ServerSecurityUser; | ||
| import io.undertow.server.HttpServerExchange; | ||
|
|
||
| import java.net.InetAddress; | ||
| import java.net.NetworkInterface; | ||
| import java.net.URI; | ||
| import java.net.http.HttpClient; | ||
| import java.net.http.HttpRequest; | ||
| import java.net.http.HttpResponse; | ||
| import java.security.MessageDigest; | ||
| import java.time.Duration; | ||
| import java.util.Enumeration; | ||
| import java.util.logging.Level; | ||
|
|
||
| /** | ||
| * POST /api/v1/ai/activate - Activates an AI subscription. | ||
| * Collects hardware fingerprint, validates the key against the gateway, and saves to config/ai.json. | ||
| */ | ||
| public class AiActivateHandler extends AbstractServerHttpHandler { | ||
| private final AiConfiguration config; | ||
| private final HttpClient httpClient; | ||
|
|
||
| public AiActivateHandler(final HttpServer httpServer, final AiConfiguration config) { | ||
| super(httpServer); | ||
| this.config = config; | ||
| this.httpClient = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(10)).build(); | ||
| } | ||
|
|
||
| @Override | ||
| protected boolean mustExecuteOnWorkerThread() { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| protected ExecutionResponse execute(final HttpServerExchange exchange, final ServerSecurityUser user, final JSONObject payload) { | ||
| if (payload == null) | ||
| return new ExecutionResponse(400, errorJson("Request body is required")); | ||
|
|
||
| final String subscriptionKey = payload.getString("subscriptionKey", ""); | ||
| if (subscriptionKey.isEmpty()) | ||
| return new ExecutionResponse(400, errorJson("Subscription key is required")); | ||
|
|
||
| try { | ||
| final String serverVersion = Constants.getVersion(); | ||
| final String hardwareId = getHardwareId(); | ||
| final String clientIp = getClientIp(exchange); | ||
|
|
||
| // Validate the key against the gateway | ||
| final JSONObject activationRequest = new JSONObject(); | ||
| activationRequest.put("subscriptionKey", subscriptionKey); | ||
| activationRequest.put("serverVersion", serverVersion); | ||
| activationRequest.put("hardwareId", hardwareId); | ||
|
|
||
| final HttpRequest request = HttpRequest.newBuilder()// | ||
| .uri(URI.create(config.getGatewayUrl() + "/api/activate"))// | ||
| .header("Content-Type", "application/json")// | ||
| .POST(HttpRequest.BodyPublishers.ofString(activationRequest.toString()))// | ||
| .timeout(Duration.ofSeconds(15))// | ||
| .build(); | ||
|
|
||
| final HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); | ||
|
|
||
| if (response.statusCode() != 200) { | ||
| String errorMsg = "Activation failed"; | ||
| try { | ||
| final JSONObject errBody = new JSONObject(response.body()); | ||
| errorMsg = errBody.getString("error", errorMsg); | ||
| } catch (final Exception ignored) { | ||
| } | ||
| return new ExecutionResponse(response.statusCode(), errorJson(errorMsg)); | ||
| } | ||
|
|
||
| // Activation successful - save to config/ai.json | ||
| config.activate(subscriptionKey, clientIp, hardwareId, serverVersion); | ||
|
|
||
| LogManager.instance().log(this, Level.INFO, "AI subscription activated (user=%s, ip=%s)", user.getName(), clientIp); | ||
|
|
||
| return new ExecutionResponse(200, new JSONObject().put("activated", true).toString()); | ||
|
|
||
| } catch (final Exception e) { | ||
| LogManager.instance().log(this, Level.WARNING, "AI activation error: %s", e.getMessage()); | ||
| final String msg = e.getMessage() != null ? e.getMessage() : e.getClass().getSimpleName(); | ||
| return new ExecutionResponse(500, errorJson("Activation failed: " + msg)); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Generates a hardware fingerprint by hashing MAC addresses + hostname. | ||
| * This provides a stable identifier for the server without exposing raw MAC addresses. | ||
| */ | ||
| static String getHardwareId() { | ||
| try { | ||
| final StringBuilder raw = new StringBuilder(); | ||
|
|
||
| // Collect all non-loopback MAC addresses | ||
| final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); | ||
| while (interfaces.hasMoreElements()) { | ||
| final NetworkInterface ni = interfaces.nextElement(); | ||
| if (ni.isLoopback() || ni.isVirtual()) | ||
| continue; | ||
| final byte[] mac = ni.getHardwareAddress(); | ||
| if (mac != null) { | ||
| for (final byte b : mac) | ||
| raw.append(String.format("%02x", b)); | ||
| raw.append("|"); | ||
| } | ||
| } | ||
|
|
||
| // Add hostname for extra uniqueness | ||
| raw.append(InetAddress.getLocalHost().getHostName()); | ||
|
|
||
| // Hash it to produce a stable, non-reversible fingerprint | ||
| final MessageDigest digest = MessageDigest.getInstance("SHA-256"); | ||
| final byte[] hash = digest.digest(raw.toString().getBytes()); | ||
| final StringBuilder hex = new StringBuilder(); | ||
| for (int i = 0; i < 16; i++) // Use first 16 bytes (128 bits) for a shorter ID | ||
| hex.append(String.format("%02x", hash[i])); | ||
| return hex.toString(); | ||
| } catch (final Exception e) { | ||
| LogManager.instance().log(AiActivateHandler.class, Level.FINE, "Could not generate hardware ID: %s", e.getMessage()); | ||
| return "unknown"; | ||
| } | ||
| } | ||
|
|
||
| private static String getClientIp(final HttpServerExchange exchange) { | ||
| // Check for X-Forwarded-For (reverse proxy) | ||
| final String forwarded = exchange.getRequestHeaders().getFirst("X-Forwarded-For"); | ||
| if (forwarded != null && !forwarded.isEmpty()) | ||
| return forwarded.split(",")[0].trim(); | ||
| return exchange.getSourceAddress().getAddress().getHostAddress(); | ||
| } | ||
|
|
||
| private static String errorJson(final String message) { | ||
| return new JSONObject().put("error", message).toString(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The order of network interfaces returned by
NetworkInterface.getNetworkInterfaces()is not guaranteed to be consistent across JVM restarts. This could lead to thehardwareIdchanging for the same machine, potentially causing issues with the AI subscription activation. To ensure a stable hardware ID, the collected MAC addresses should be sorted before being hashed.