-
Notifications
You must be signed in to change notification settings - Fork 0
feat: middleware V2 #125
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
Open
xavidop
wants to merge
2
commits into
xavier/reflection-v2-api
Choose a base branch
from
xavier/middleware-v2
base: xavier/reflection-v2-api
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: middleware V2 #125
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
78 changes: 78 additions & 0 deletions
78
ai/src/main/java/com/google/genkit/ai/middleware/BaseGenerationMiddleware.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,78 @@ | ||
| /* | ||
| * Copyright 2025 Google LLC | ||
| * | ||
| * 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-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package com.google.genkit.ai.middleware; | ||
|
|
||
| import com.google.genkit.ai.ModelResponse; | ||
| import com.google.genkit.ai.Tool; | ||
| import com.google.genkit.ai.ToolResponse; | ||
| import com.google.genkit.core.ActionContext; | ||
| import com.google.genkit.core.GenkitException; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * BaseGenerationMiddleware provides default pass-through implementations for all three hooks. | ||
| * Extend this class and override only the hooks you need. | ||
| * | ||
| * <p>Example: | ||
| * | ||
| * <pre>{@code | ||
| * public class TimingMiddleware extends BaseGenerationMiddleware { | ||
| * @Override | ||
| * public String name() { return "timing"; } | ||
| * | ||
| * @Override | ||
| * public GenerationMiddleware newInstance() { return new TimingMiddleware(); } | ||
| * | ||
| * @Override | ||
| * public ModelResponse wrapModel(ActionContext ctx, ModelParams params, ModelNext next) | ||
| * throws GenkitException { | ||
| * long start = System.currentTimeMillis(); | ||
| * ModelResponse resp = next.apply(ctx, params); | ||
| * System.out.println("Model call took " + (System.currentTimeMillis() - start) + "ms"); | ||
| * return resp; | ||
| * } | ||
| * } | ||
| * }</pre> | ||
| */ | ||
| public abstract class BaseGenerationMiddleware implements GenerationMiddleware { | ||
|
|
||
| @Override | ||
| public ModelResponse wrapGenerate(ActionContext ctx, GenerateParams params, GenerateNext next) | ||
| throws GenkitException { | ||
| return next.apply(ctx, params); | ||
| } | ||
|
|
||
| @Override | ||
| public ModelResponse wrapModel(ActionContext ctx, ModelParams params, ModelNext next) | ||
| throws GenkitException { | ||
| return next.apply(ctx, params); | ||
| } | ||
|
|
||
| @Override | ||
| public ToolResponse wrapTool(ActionContext ctx, ToolParams params, ToolNext next) | ||
| throws GenkitException { | ||
| return next.apply(ctx, params); | ||
| } | ||
|
|
||
| @Override | ||
| public List<Tool<?, ?>> tools() { | ||
| return Collections.emptyList(); | ||
| } | ||
| } | ||
38 changes: 38 additions & 0 deletions
38
ai/src/main/java/com/google/genkit/ai/middleware/GenerateNext.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,38 @@ | ||
| /* | ||
| * Copyright 2025 Google LLC | ||
| * | ||
| * 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-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package com.google.genkit.ai.middleware; | ||
|
|
||
| import com.google.genkit.ai.ModelResponse; | ||
| import com.google.genkit.core.ActionContext; | ||
| import com.google.genkit.core.GenkitException; | ||
|
|
||
| /** Next function in the {@link GenerationMiddleware#wrapGenerate} hook chain. */ | ||
| @FunctionalInterface | ||
| public interface GenerateNext { | ||
|
|
||
| /** | ||
| * Calls the next handler in the generate chain. | ||
| * | ||
| * @param ctx the action context | ||
| * @param params the generate parameters | ||
| * @return the model response | ||
| * @throws GenkitException if processing fails | ||
| */ | ||
| ModelResponse apply(ActionContext ctx, GenerateParams params) throws GenkitException; | ||
| } |
54 changes: 54 additions & 0 deletions
54
ai/src/main/java/com/google/genkit/ai/middleware/GenerateParams.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,54 @@ | ||
| /* | ||
| * Copyright 2025 Google LLC | ||
| * | ||
| * 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-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package com.google.genkit.ai.middleware; | ||
|
|
||
| import com.google.genkit.ai.ModelRequest; | ||
|
|
||
| /** Holds parameters for the {@link GenerationMiddleware#wrapGenerate} hook. */ | ||
| public class GenerateParams { | ||
|
|
||
| private final ModelRequest request; | ||
| private final int iteration; | ||
|
|
||
| /** | ||
| * Creates GenerateParams. | ||
| * | ||
| * @param request the current model request for this iteration | ||
| * @param iteration the current tool-loop iteration (0-indexed) | ||
| */ | ||
| public GenerateParams(ModelRequest request, int iteration) { | ||
| this.request = request; | ||
| this.iteration = iteration; | ||
| } | ||
|
|
||
| /** Returns the current model request with accumulated messages. */ | ||
| public ModelRequest getRequest() { | ||
| return request; | ||
| } | ||
|
|
||
| /** Returns the current tool-loop iteration (0-indexed). */ | ||
| public int getIteration() { | ||
| return iteration; | ||
| } | ||
|
|
||
| /** Returns a new GenerateParams with the given request, preserving the iteration. */ | ||
| public GenerateParams withRequest(ModelRequest request) { | ||
| return new GenerateParams(request, this.iteration); | ||
| } | ||
| } |
126 changes: 126 additions & 0 deletions
126
ai/src/main/java/com/google/genkit/ai/middleware/GenerationMiddleware.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,126 @@ | ||
| /* | ||
| * Copyright 2025 Google LLC | ||
| * | ||
| * 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-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package com.google.genkit.ai.middleware; | ||
|
|
||
| import com.google.genkit.ai.ModelResponse; | ||
| import com.google.genkit.ai.Tool; | ||
| import com.google.genkit.ai.ToolResponse; | ||
| import com.google.genkit.core.ActionContext; | ||
| import com.google.genkit.core.GenkitException; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * GenerationMiddleware provides hooks for different stages of the generation pipeline. | ||
| * | ||
| * <p>This is the V2 middleware interface that replaces the generic {@code Middleware<I, O>}. It | ||
| * provides three distinct hooks: | ||
| * | ||
| * <ul> | ||
| * <li>{@link #wrapGenerate} - wraps each iteration of the tool loop | ||
| * <li>{@link #wrapModel} - wraps each model API call | ||
| * <li>{@link #wrapTool} - wraps each tool execution | ||
| * </ul> | ||
| * | ||
| * <p>Each {@code generate()} call creates a fresh instance via {@link #newInstance()}, enabling | ||
| * per-invocation state (e.g., counters, timers) without shared mutable state across requests. | ||
| * | ||
| * <p>Example: | ||
| * | ||
| * <pre>{@code | ||
| * public class LoggingMiddleware extends BaseGenerationMiddleware { | ||
| * private int modelCalls = 0; | ||
| * | ||
| * @Override | ||
| * public String name() { return "logging"; } | ||
| * | ||
| * @Override | ||
| * public GenerationMiddleware newInstance() { return new LoggingMiddleware(); } | ||
| * | ||
| * @Override | ||
| * public ModelResponse wrapModel(ActionContext ctx, ModelParams params, ModelNext next) | ||
| * throws GenkitException { | ||
| * modelCalls++; | ||
| * System.out.println("Model call #" + modelCalls); | ||
| * ModelResponse resp = next.apply(ctx, params); | ||
| * System.out.println("Model responded with " + resp.getText()); | ||
| * return resp; | ||
| * } | ||
| * } | ||
| * }</pre> | ||
| */ | ||
| public interface GenerationMiddleware { | ||
|
|
||
| /** Returns the middleware's unique identifier. */ | ||
| String name(); | ||
|
|
||
| /** | ||
| * Returns a fresh instance for each {@code generate()} call, enabling per-invocation state. | ||
| * | ||
| * <p>Stable state (e.g., API keys, configuration) should be preserved. Per-request state (e.g., | ||
| * counters) should be reset. | ||
| */ | ||
| GenerationMiddleware newInstance(); | ||
|
|
||
| /** | ||
| * Wraps each iteration of the generate tool loop. | ||
| * | ||
| * @param ctx the action context | ||
| * @param params the generate parameters including the current request and iteration | ||
| * @param next the next function in the chain | ||
| * @return the model response | ||
| * @throws GenkitException if processing fails | ||
| */ | ||
| ModelResponse wrapGenerate(ActionContext ctx, GenerateParams params, GenerateNext next) | ||
| throws GenkitException; | ||
|
|
||
| /** | ||
| * Wraps each model API call. | ||
| * | ||
| * @param ctx the action context | ||
| * @param params the model parameters including the request | ||
| * @param next the next function in the chain | ||
| * @return the model response | ||
| * @throws GenkitException if processing fails | ||
| */ | ||
| ModelResponse wrapModel(ActionContext ctx, ModelParams params, ModelNext next) | ||
| throws GenkitException; | ||
|
|
||
| /** | ||
| * Wraps each tool execution. May be called concurrently when multiple tools execute in parallel. | ||
| * Implementations must be safe for concurrent use. | ||
| * | ||
| * @param ctx the action context | ||
| * @param params the tool parameters including the request and resolved tool | ||
| * @param next the next function in the chain | ||
| * @return the tool response | ||
| * @throws GenkitException if processing fails | ||
| */ | ||
| ToolResponse wrapTool(ActionContext ctx, ToolParams params, ToolNext next) throws GenkitException; | ||
|
|
||
| /** | ||
| * Returns additional tools to make available during generation. These tools are dynamically added | ||
| * when the middleware is used. | ||
| * | ||
| * @return the list of additional tools, or empty list if none | ||
| */ | ||
| default List<Tool<?, ?>> tools() { | ||
| return Collections.emptyList(); | ||
| } | ||
| } |
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.
It's best if this takes ToolPaquestPart and returns ToolResponsePart as opposed to ToolPaquest/ToolResponse. The part has metadata, which is critical to some usecases.