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
30 changes: 29 additions & 1 deletion genkit/src/main/java/com/google/genkit/Genkit.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class Genkit {
private final Map<String, DotPrompt<?>> promptCache;
private final Map<String, Agent> agentRegistry;
private ReflectionServer reflectionServer;
private ReflectionServerV2 reflectionServerV2;
private EvaluationManager evaluationManager;

/** Creates a new Genkit instance with default options. */
Expand Down Expand Up @@ -1540,9 +1541,18 @@ public List<Plugin> getPlugins() {

/** Starts the reflection server for dev tools integration. */
private void startReflectionServer() {
String v2ServerUrl = System.getenv("GENKIT_REFLECTION_V2_SERVER");
if (v2ServerUrl != null && !v2ServerUrl.isEmpty()) {
startReflectionServerV2(v2ServerUrl);
} else {
startReflectionServerV1();
}
}

private void startReflectionServerV1() {
try {
int port = options.getReflectionPort();
reflectionServer = new ReflectionServer(registry, port);
reflectionServer = new ReflectionServer(registry, port, options.getName());
reflectionServer.start();
logger.info("Reflection server started on port {}", port);

Expand All @@ -1554,8 +1564,26 @@ private void startReflectionServer() {
}
}

private void startReflectionServerV2(String serverUrl) {
try {
reflectionServerV2 = new ReflectionServerV2(registry, serverUrl, options.getName());
reflectionServerV2.start();
logger.info("Reflection V2 client connecting to {}", serverUrl);
} catch (Exception e) {
logger.error("Failed to start reflection V2 client", e);
throw new GenkitException("Failed to start reflection V2 client", e);
}
}

/** Stops the Genkit instance and cleans up resources. */
public void stop() {
if (reflectionServerV2 != null) {
try {
reflectionServerV2.stop();
} catch (Exception e) {
logger.warn("Error stopping reflection V2 client", e);
}
}
if (reflectionServer != null) {
try {
reflectionServer.stop();
Expand Down
17 changes: 17 additions & 0 deletions genkit/src/main/java/com/google/genkit/GenkitOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ public class GenkitOptions {
private final int reflectionPort;
private final String projectRoot;
private final String promptDir;
private final String name;

private GenkitOptions(Builder builder) {
this.devMode = builder.devMode;
this.reflectionPort = builder.reflectionPort;
this.projectRoot = builder.projectRoot;
this.promptDir = builder.promptDir;
this.name = builder.name;
}

/**
Expand Down Expand Up @@ -79,12 +81,22 @@ public String getPromptDir() {
return promptDir;
}

/**
* Returns the optional instance name for this Genkit instance.
*
* @return the instance name, or null if not set
*/
public String getName() {
return name;
}

/** Builder for GenkitOptions. */
public static class Builder {
private boolean devMode = isDevModeFromEnv();
private int reflectionPort = getReflectionPortFromEnv();
private String projectRoot = System.getProperty("user.dir");
private String promptDir = "/prompts";
private String name;

private static boolean isDevModeFromEnv() {
String env = System.getenv("GENKIT_ENV");
Expand Down Expand Up @@ -123,6 +135,11 @@ public Builder promptDir(String promptDir) {
return this;
}

public Builder name(String name) {
this.name = name;
return this;
}

public GenkitOptions build() {
return new GenkitOptions(this);
}
Expand Down
5 changes: 3 additions & 2 deletions genkit/src/main/java/com/google/genkit/ReflectionServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,12 @@ public class ReflectionServer {
*
* @param registry the Genkit registry
* @param port the port to listen on
* @param instanceName optional instance name for the runtime ID (may be null)
*/
public ReflectionServer(Registry registry, int port) {
public ReflectionServer(Registry registry, int port, String instanceName) {
this.registry = registry;
this.port = port;
this.runtimeId = "java-" + ProcessHandle.current().pid() + "-" + System.currentTimeMillis();
this.runtimeId = RuntimeIdGenerator.generate(instanceName);
this.evaluationManager = new EvaluationManager(registry);

// Register local telemetry store for Dev UI trace access
Expand Down
Loading
Loading