Skip to content

Add the agent tools-as-code builder - #9

Merged
PaulAsjes merged 8 commits into
mainfrom
pma/agent-tools-builder
Nov 20, 2026
Merged

PaulAsjes merged 8 commits into
mainfrom
pma/agent-tools-builder

Conversation

@PaulAsjes

@PaulAsjes PaulAsjes commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Adds com.elevenlabs.api.agenttools, a hand-written builder for agent tools in code: webhook, client and system tools, plus the Schema helper they share. build() returns the generated ToolRequestModel that agents().tools().create() and update() take, and toJsonString() writes the same bytes as the JS SDK and the CLI's tool_configs/*.json. It reuses the workflow builder's CanonicalJson and Destination.

Stacked on #7 (the workflows builder): it targets pma/agent-workflows and must merge after #7.

Details

Contract. Follows docs/agents-as-code/tool-spec.md in elevenlabs-js. The 21 golden fixtures are vendored into src/test/resources/agenttools/fixtures and run by ToolFixtureTest. Expected output is compared byte for byte, and for every output fixture the test also checks that the build() model loads back to the same bytes.

API. Tool.webhook(name, description) / Tool.client(...) / SystemTool.endCall(), .skipTurn(), .transferToNumber() and the rest. Tool.fromJson(String | JsonNode) loads a bare config or a {tool_config} wrapper, and Tool.fromResponse(ToolResponseModel) loads a get response. Then asWebhook() / asClient() / asSystem() (as Optional), build(), buildConfig() and toJson() / toJsonString(). SystemTool.buildBuiltInConfig() returns the SystemToolConfigInput for prompt.built_in_tools. Java won't let buildConfig() change its return type in a subclass, so the system tool gets its own method name. Schema.string()/integer()/number()/bool() return LiteralSchema, object() returns ObjectSchema and array(items) returns ArraySchema, each with covariant returns. Options are one ToolOptions class that takes generated enum types (PreToolSpeechMode, ToolExecutionMode, ...), as the workflow builder does; a key that doesn't apply to the tool's kind is invalid_option. Transfer options are ToolTransferOptions, and the system tool constructor params are nested classes in SystemToolOptions.

Shared rules where the new SDKs deviate from JS on purpose (same as .NET, PHP, Ruby and Rust):

  1. Removing the last assignment, client param or query param leaves the field out. Removing a name that doesn't exist changes nothing, and loaded empty values round-trip unchanged. A leftover loaded type on an emptied query schema doesn't count as content.
  2. An empty URL is missing_url.
  3. A loaded body, response, client params or query schema is inferred to be an object only when it has no type. Body, response and params then write "type": "object"; the query type stays synthetic and is written only if it was loaded. A declared non-object type is kept and reported as body_not_object at body, response, params or query.
  4. A nullable literal's constant_value: null survives load and toJson(). The generated model reads null as unset, so build() reports constant_type_mismatch.
  5. A loaded query_params_schema keeps all its fields, and loaded allowed_values and required_constraints (and their groups) keep fields the builder doesn't model. Edits are overlaid on the loaded object.

Latest review fixes from the other ports.

  • Path params are always required: omit() on one is omitted_required, whatever optional() says.
  • Malformed loaded fields are kept and reported by build() as invalid_model, while toJson() writes them back. This covers schema fields of the wrong shape, client parameters that aren't an object, system params without a system_tool_type, and webhook api_schema parts that aren't objects.
  • Null stripping and the constant check walk property and path param names as names, so a param named constant_value is not treated as a metadata key.
  • Integer constants are judged from their digits, exponents included. Non-finite numbers are rejected, empty objects inside constants stay {}, and numeric property names stay a JSON object in JavaScript key order.

Constants and the Java model. build() serializes the model it produced and compares every schema constant with the wire config. A constant the model would change or drop is reported as constant_type_mismatch with its path; nothing is changed silently. Integers beyond the long range are rejected by toJson() too, as in the other SDKs. Two limits are specific to Java, because the generated LiteralJsonSchemaPropertyConstantValue holds an int or a double:

  • A literal integer constant outside the int range (for example 3000000000, or 9007199254740993 for a number) is written exactly by toJsonString(), but build() reports it, since the model would truncate it.
  • A decimal that no double holds exactly (9007199254740992.5), and an integer beyond long nested inside an object or array constant, are reported the same way.

An integer that JavaScript would read and write back as a double (123456789012345680000) is accepted and sent as that double.

Fern. The package, its tests and its fixtures are in .fernignore with a why-comment. The README has a new "Agent tools as code" section.

Testing

  • The new main sources compile cleanly with javac --release 8 -Xlint:all against the generated classes, and the tests compile with --release 8 (checks the Java 8 API level).
  • CI (gradle --no-daemon compileJava and gradle --no-daemon test, Gradle 8.14.3) passes on this PR. Locally, gradle compileJava in gradle:8.14.3-jdk11 passed; later Docker runs couldn't start because the local Docker daemon's storage began returning I/O errors.
  • Ran the JUnit 5.10.2 suites directly (JDK 17, classes compiled for release 8): com.elevenlabs.api.agenttools passes 60/60 (21 fixtures, the fixture-count check and 38 builder tests), and com.elevenlabs.api.agentworkflows still passes 34/34.

🤖 Generated with Claude Code

Port the hand-written tools-as-code builder (elevenlabs-js
docs/agents-as-code/tool-spec.md) to Java as com.elevenlabs.api.agenttools:
webhook, client and system tools, the Schema helper, Header and
AuthConnection, Tool.fromJson/fromResponse loaders, build() returning the
generated ToolRequestModel and toJsonString() byte-identical to the JS SDK.
It reuses the workflow builder's CanonicalJson and Destination.

It follows the rules the .NET, PHP, Ruby and Rust ports share where they
deviate from JS on purpose (removing the last assignment, client param or
query param omits the field; an empty URL is missing_url; loaded
containers are objects only when untyped; a nullable literal's null
constant survives loading; loaded query, allowed_values and
required_constraints keep unmodeled fields), plus the latest review fixes:
constants the generated model would change are reported instead of sent
altered, path params are always required, and malformed loaded fields are
kept and reported as invalid_model.

The 21 shared fixtures are vendored and run in ToolFixtureTest, which also
checks that build()'s model loads back to the same bytes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@cursor

cursor Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Large new public API surface with strict validation and generated-model edge cases (constants, malformed loaded JSON), but changes are additive and sit atop existing generated types rather than altering core client behavior.

Overview
Introduces com.elevenlabs.api.agenttools, a hand-written fluent builder for defining agent tools in code (webhook, client, and system tools), aligned with the cross-SDK tool-spec.md. Tools chain methods like get/post, pathParam, body, assign, and options, then build() produces the generated ToolRequestModel for agents().tools().create() / update(), while toJsonString() emits canonical JSON matching other SDKs and the CLI’s tool_configs/*.json.

The package adds an immutable Schema model (literals, objects, arrays; value sources and validation), ToolValidationException with stable issue codes, load/edit via Tool.fromJson / Tool.fromResponse, and SystemTool.buildBuiltInConfig() for prompt.built_in_tools. It reuses the workflow builder’s CanonicalJson and Destination. .fernignore and a new README “Agent tools as code” section document the non-generated code. Tests include ToolBuilderTest, a fixture runner, and 23 vendored golden fixtures in src/test/resources/agenttools/fixtures.

Reviewed by Cursor Bugbot for commit 26972d8. Bugbot is set up for automated code reviews on this repo. Configure here.

@PaulAsjes
PaulAsjes added this pull request to stack #10 November 19, 2026 14:25
Co-Authored-By: Claude Opus 5.5 <[email protected]>

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

}
ToolValidation.checkSchema(prop.getValue(), path, true, issues);
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Query schema skips container checks

Low Severity

Webhook query validation walks each query param but never runs checkSchema on the query object itself. Client params, body, and response all do, so loaded query requireAnyOf()/requireAllOf() names that do not exist are not reported as unknown_required_property.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 4c8788b. Configure here.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Leaving this as is. The API's query params schema has no required_constraints (the generated QueryParamsJsonSchemaInput has only properties and required), and query params can only be added through queryParam(), so requireAnyOf()/requireAllOf() can't target them. A loaded required_constraints there is kept as an unmodeled field, as the spec says, and no other SDK checks it.

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

PaulAsjes and others added 3 commits November 19, 2026 16:26
Co-Authored-By: Claude Opus 5.5 <[email protected]>
A transfer_to_number tool may hold per-provider transfer_rules instead of
transfers; the API takes exactly one of the two. A pulled rules-only tool
was rejected as no_transfers. Now no_transfers fires only when neither list
has entries, loaded rules are kept as loaded, and a tool with both is a new
conflicting_transfers error. Vendors shared fixtures 22 and 23 from
elevenlabs-js.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

There are 2 total unresolved issues (including 1 from previous review).

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit cf63c7e. Configure here.

boolean hasRules = hasEntries(params.get("transfer_rules"));
if (!hasTransfers && !hasRules) {
issues.add(ToolIssue.error("no_transfers", "transfer", systemToolType + " needs at least one transfer()"));
} else if (hasTransfers && hasRules) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Rules check covers agent transfers

Low Severity

validate() treats a non-empty transfer_rules list as a valid substitute for transfers on every transfer tool, including transfer_to_agent. That field exists only on transfer_to_number; a loaded agent-transfer tool with rules and no destinations now passes instead of reporting no_transfers, and one that also has transfers is rejected as conflicting_transfers even though transfer_rules is just an unmodeled extra field there.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit cf63c7e. Configure here.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Base automatically changed from pma/agent-workflows to main November 20, 2026 12:52
@PaulAsjes
PaulAsjes merged commit 1a23279 into main Nov 20, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant