-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat(serverless-spark): add cancel-batch tool #1827
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
Merged
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
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
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
51 changes: 51 additions & 0 deletions
51
docs/en/resources/tools/serverless-spark/serverless-spark-cancel-batch.md
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,51 @@ | ||
| --- | ||
| title: "serverless-spark-cancel-batch" | ||
| type: docs | ||
| weight: 2 | ||
| description: > | ||
| A "serverless-spark-cancel-batch" tool cancels a running Spark batch operation. | ||
| aliases: | ||
| - /resources/tools/serverless-spark-cancel-batch | ||
| --- | ||
|
|
||
| ## About | ||
|
|
||
| `serverless-spark-cancel-batch` tool cancels a running Spark batch operation in | ||
| a Google Cloud Serverless for Apache Spark source. The cancellation request is | ||
| asynchronous, so the batch state will not change immediately after the tool | ||
| returns; it can take a minute or so for the cancellation to be reflected. | ||
|
|
||
| It's compatible with the following sources: | ||
|
|
||
| - [serverless-spark](../../sources/serverless-spark.md) | ||
|
|
||
| `serverless-spark-cancel-batch` accepts the following parameters: | ||
|
|
||
| - **`operation`** (required): The name of the operation to cancel. For example, for `projects/my-project/locations/us-central1/operations/my-operation`, you would pass `my-operation`. | ||
|
|
||
| The tool inherits the `project` and `location` from the source configuration. | ||
|
|
||
| ## Example | ||
|
|
||
| ```yaml | ||
| tools: | ||
| cancel_spark_batch: | ||
| kind: serverless-spark-cancel-batch | ||
| source: my-serverless-spark-source | ||
| description: Use this tool to cancel a running serverless spark batch operation. | ||
| ``` | ||
|
|
||
| ## Response Format | ||
|
|
||
| ```json | ||
| "Cancelled [projects/my-project/regions/us-central1/operations/my-operation]." | ||
| ``` | ||
|
|
||
| ## Reference | ||
|
|
||
| | **field** | **type** | **required** | **description** | | ||
| | ------------ | :------: | :----------: | -------------------------------------------------- | | ||
| | kind | string | true | Must be "serverless-spark-cancel-batch". | | ||
| | source | string | true | Name of the source the tool should use. | | ||
| | description | string | true | Description of the tool that is passed to the LLM. | | ||
| | authRequired | string[] | false | List of auth services required to invoke this tool | |
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
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
internal/tools/serverlessspark/serverlesssparkcancelbatch/serverlesssparkcancelbatch.go
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 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. | ||
|
|
||
| package serverlesssparkcancelbatch | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "cloud.google.com/go/longrunning/autogen/longrunningpb" | ||
| "github.com/goccy/go-yaml" | ||
| "github.com/googleapis/genai-toolbox/internal/sources" | ||
| "github.com/googleapis/genai-toolbox/internal/sources/serverlessspark" | ||
| "github.com/googleapis/genai-toolbox/internal/tools" | ||
| ) | ||
|
|
||
| const kind = "serverless-spark-cancel-batch" | ||
|
|
||
| func init() { | ||
| if !tools.Register(kind, newConfig) { | ||
| panic(fmt.Sprintf("tool kind %q already registered", kind)) | ||
| } | ||
| } | ||
|
|
||
| func newConfig(ctx context.Context, name string, decoder *yaml.Decoder) (tools.ToolConfig, error) { | ||
| actual := Config{Name: name} | ||
| if err := decoder.DecodeContext(ctx, &actual); err != nil { | ||
| return nil, err | ||
| } | ||
| return actual, nil | ||
| } | ||
|
|
||
| type Config struct { | ||
| Name string `yaml:"name" validate:"required"` | ||
| Kind string `yaml:"kind" validate:"required"` | ||
| Source string `yaml:"source" validate:"required"` | ||
| Description string `yaml:"description"` | ||
| AuthRequired []string `yaml:"authRequired"` | ||
| } | ||
|
|
||
| // validate interface | ||
| var _ tools.ToolConfig = Config{} | ||
|
|
||
| // ToolConfigKind returns the unique name for this tool. | ||
| func (cfg Config) ToolConfigKind() string { | ||
| return kind | ||
| } | ||
|
|
||
| // Initialize creates a new Tool instance. | ||
| func (cfg Config) Initialize(srcs map[string]sources.Source) (tools.Tool, error) { | ||
| rawS, ok := srcs[cfg.Source] | ||
| if !ok { | ||
| return nil, fmt.Errorf("source %q not found", cfg.Source) | ||
| } | ||
|
|
||
| ds, ok := rawS.(*serverlessspark.Source) | ||
| if !ok { | ||
| return nil, fmt.Errorf("invalid source for %q tool: source kind must be `%s`", kind, serverlessspark.SourceKind) | ||
| } | ||
|
|
||
| desc := cfg.Description | ||
| if desc == "" { | ||
| desc = "Cancels a running Serverless Spark (aka Dataproc Serverless) batch operation. Note that the batch state will not change immediately after the tool returns; it can take a minute or so for the cancellation to be reflected." | ||
| } | ||
|
|
||
| allParameters := tools.Parameters{ | ||
| tools.NewStringParameter("operation", "The name of the operation to cancel, e.g. for \"projects/my-project/locations/us-central1/operations/my-operation\", pass \"my-operation\""), | ||
| } | ||
| inputSchema, _ := allParameters.McpManifest() | ||
|
|
||
| mcpManifest := tools.McpManifest{ | ||
| Name: cfg.Name, | ||
| Description: desc, | ||
| InputSchema: inputSchema, | ||
| } | ||
|
|
||
| return &Tool{ | ||
| Name: cfg.Name, | ||
| Kind: kind, | ||
| Source: ds, | ||
| AuthRequired: cfg.AuthRequired, | ||
| manifest: tools.Manifest{Description: desc, Parameters: allParameters.Manifest()}, | ||
| mcpManifest: mcpManifest, | ||
| Parameters: allParameters, | ||
| }, nil | ||
| } | ||
|
|
||
| // Tool is the implementation of the tool. | ||
| type Tool struct { | ||
| Name string `yaml:"name"` | ||
| Kind string `yaml:"kind"` | ||
| Description string `yaml:"description"` | ||
| AuthRequired []string `yaml:"authRequired"` | ||
|
|
||
| Source *serverlessspark.Source | ||
|
|
||
| manifest tools.Manifest | ||
| mcpManifest tools.McpManifest | ||
| Parameters tools.Parameters | ||
| } | ||
|
|
||
| // Invoke executes the tool's operation. | ||
| func (t *Tool) Invoke(ctx context.Context, params tools.ParamValues, accessToken tools.AccessToken) (any, error) { | ||
| client, err := t.Source.GetOperationsClient(ctx) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to get operations client: %w", err) | ||
| } | ||
|
|
||
| paramMap := params.AsMap() | ||
| operation, ok := paramMap["operation"].(string) | ||
| if !ok { | ||
| return nil, fmt.Errorf("missing required parameter: operation") | ||
| } | ||
|
|
||
| if strings.Contains(operation, "/") { | ||
| return nil, fmt.Errorf("operation must be a short operation name without '/': %s", operation) | ||
| } | ||
|
|
||
| req := &longrunningpb.CancelOperationRequest{ | ||
| Name: fmt.Sprintf("projects/%s/locations/%s/operations/%s", t.Source.Project, t.Source.Location, operation), | ||
| } | ||
|
|
||
| err = client.CancelOperation(ctx, req) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to cancel operation: %w", err) | ||
| } | ||
|
|
||
| return fmt.Sprintf("Cancelled [%s].", operation), nil | ||
| } | ||
|
|
||
| func (t *Tool) ParseParams(data map[string]any, claims map[string]map[string]any) (tools.ParamValues, error) { | ||
| return tools.ParseParams(t.Parameters, data, claims) | ||
| } | ||
|
|
||
| func (t *Tool) Manifest() tools.Manifest { | ||
| return t.manifest | ||
| } | ||
|
|
||
| func (t *Tool) McpManifest() tools.McpManifest { | ||
| return t.mcpManifest | ||
| } | ||
|
|
||
| func (t *Tool) Authorized(services []string) bool { | ||
| return tools.IsAuthorized(t.AuthRequired, services) | ||
| } | ||
|
|
||
| func (t *Tool) RequiresClientAuthorization() bool { | ||
| // Client OAuth not supported, rely on ADCs. | ||
| return false | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.