-
Notifications
You must be signed in to change notification settings - Fork 12
fix(types): eliminate as-any gaps for serverTool mixing and fromChatMessages input #32
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
mattapperson
wants to merge
4
commits into
main
Choose a base branch
from
fix-server-tool-type-gaps
base: main
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
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8188b63
fix(types): eliminate as-any gaps for serverTool mixing and fromChatM…
mattapperson 1c91662
fix(types): keep ServerTool<T> generic, drop ServerToolNarrow
mattapperson 1e77b0d
chore(release): bump to minor and apply biome formatting
mattapperson 491014f
docs(changeset): revise to reflect backward-compatible design
mattapperson 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 |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| --- | ||
| "@openrouter/agent": patch | ||
| --- | ||
|
|
||
| Fix two type gaps that forced consumers to use `as any` when wiring up | ||
| `callModel` with server tools and chat-format inputs: | ||
|
|
||
| - **`ServerTool` is now a structural-base alias**, not a generic. The | ||
| factory returns a narrow `ServerToolNarrow<T>` that extends | ||
| `ServerToolBase` via interface extension, so a specific | ||
| `ServerToolNarrow<'openrouter:datetime'>` flows into the public | ||
| `ServerTool` alias without a cast. Mixed arrays like | ||
| `Array<ClientTool | ServerTool>` or `Tool[]` now accept any mix of | ||
| `tool()` and `serverTool()` results directly. New public types: | ||
| `ServerToolBase` (structural base) and `ServerToolNarrow<T>` (narrow | ||
| form when the exact `config` shape matters). The old `ServerTool<T>` | ||
| generic is replaced by `ServerToolNarrow<T>`; code that only used | ||
| `ServerTool` without a type argument is unaffected. | ||
|
|
||
| - **`callModel`'s `request.input` now accepts `InputsUnion`** (the SDK's | ||
| wider message shape returned by `fromChatMessages()`), alongside the | ||
| existing `Item[]` and plain `string` forms. The docstring on | ||
| `fromChatMessages()` already claims its output "can be passed directly | ||
| to `callModel()`"; the types now match. | ||
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
103 changes: 103 additions & 0 deletions
103
packages/agent/tests/unit/consumer-type-ergonomics.test-d.ts
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,103 @@ | ||
| /** | ||
| * Regression coverage for two consumer-facing type gaps reported against | ||
| * v0.4.0 that previously required `as any`: | ||
| * | ||
| * 1. Mixing `tool()` + `serverTool()` results in a single array typed as | ||
| * `Array<ClientTool | ServerTool>` must assign to `callModel`'s `tools` | ||
| * parameter without a cast. The factory return type is narrow | ||
| * (`ServerToolNarrow<T>`) and must flow through interface extension up | ||
| * to the `ServerToolBase`-based `ServerTool` alias. | ||
| * | ||
| * 2. `fromChatMessages()` returns the SDK's `InputsUnion`, which must be | ||
| * directly assignable to `callModel`'s `request.input` without a cast. | ||
| * Previously the input was typed as `Item[] | string`, which is a | ||
| * narrower union that `InputsUnion` does not extend. | ||
| */ | ||
|
|
||
| import type * as models from '@openrouter/sdk/models'; | ||
| import { expectTypeOf } from 'vitest'; | ||
| import { z } from 'zod/v4'; | ||
| import type { CallModelInput } from '../../src/lib/async-params.js'; | ||
| import { fromChatMessages } from '../../src/lib/chat-compat.js'; | ||
| import { serverTool, tool } from '../../src/lib/tool.js'; | ||
| import type { | ||
| ClientTool, | ||
| ServerTool, | ||
| ServerToolBase, | ||
| ServerToolNarrow, | ||
| Tool, | ||
| } from '../../src/lib/tool-types.js'; | ||
|
|
||
| // --- Issue 1: mixed arrays assign without `as any` -------------------------- | ||
|
|
||
| // Specific narrow factory return types must flow to the public `ServerTool` | ||
| // alias via interface extension. | ||
| expectTypeOf<ServerToolNarrow<'openrouter:datetime'>>().toExtend<ServerTool>(); | ||
| expectTypeOf<ServerToolNarrow<'openrouter:datetime'>>().toExtend<ServerToolBase>(); | ||
| expectTypeOf<ServerToolNarrow<'openrouter:datetime'>>().toExtend<Tool>(); | ||
|
|
||
| // ServerTool (bare, no generic) is the structural base — it should accept | ||
| // any narrow variant assigned to it. | ||
| const _dt: ServerTool = serverTool({ | ||
| type: 'openrouter:datetime', | ||
| }); | ||
| const _ws: ServerTool = serverTool({ | ||
| type: 'openrouter:web_search', | ||
| }); | ||
| void _dt; | ||
| void _ws; | ||
|
|
||
| // Array<ClientTool | ServerTool> accepts a mix without cast. | ||
| const _mixed: Array<ClientTool | ServerTool> = [ | ||
| tool({ | ||
| name: 'save_note', | ||
| inputSchema: z.object({ | ||
| title: z.string(), | ||
| }), | ||
| execute: async () => ({ | ||
| ok: true, | ||
| }), | ||
| }), | ||
| serverTool({ | ||
| type: 'openrouter:datetime', | ||
| }), | ||
| serverTool({ | ||
| type: 'openrouter:web_search', | ||
| }), | ||
| ]; | ||
| void _mixed; | ||
|
|
||
| // Tool[] accepts the same mix. | ||
| const _asTool: Tool[] = [ | ||
| tool({ | ||
| name: 'save_note', | ||
| inputSchema: z.object({ | ||
| title: z.string(), | ||
| }), | ||
| execute: async () => ({ | ||
| ok: true, | ||
| }), | ||
| }), | ||
| serverTool({ | ||
| type: 'openrouter:datetime', | ||
| }), | ||
| ]; | ||
| void _asTool; | ||
|
|
||
| // --- Issue 2: fromChatMessages() output is assignable to input ------------- | ||
|
|
||
| // A `CallModelInput`'s `input` field accepts `InputsUnion` directly. We use | ||
| // `Extract` instead of `toExtend` because `input` is a field-or-fn union; we | ||
| // just need the plain data variant to accept `InputsUnion`. | ||
| type _InputField = CallModelInput['input']; | ||
| expectTypeOf<models.InputsUnion>().toExtend<_InputField>(); | ||
|
|
||
| // And the concrete return of `fromChatMessages()` must be assignable. | ||
| const _converted = fromChatMessages([ | ||
| { | ||
| role: 'user', | ||
| content: 'hi', | ||
| }, | ||
| ]); | ||
| const _asInput: _InputField = _converted; | ||
| void _asInput; |
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
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.