-
-
Notifications
You must be signed in to change notification settings - Fork 56
[WIP] feat(messages): add Anthropic Messages API extension #272
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
Menci
wants to merge
5
commits into
moeru-ai:main
Choose a base branch
from
Menci:feat/anthropic-messages
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 all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7ffefb4
feat(messages): add Anthropic Messages API extension
Menci fc4f12e
[autofix.ci] apply automated fixes
autofix-ci[bot] 9efcf68
fix(messages): harden proxy compatibility
Menci 7b4c46c
fix(messages): avoid unhandled stream promise rejections
Menci 4807c29
test(messages): harden anthropic live checks
Menci 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| # Anthropic Messages Live Investigation | ||
|
|
||
| Date: 2026-03-15 | ||
|
|
||
| ## xsAI changes made during investigation | ||
|
|
||
| - Ignore trailing `data: [DONE]` sentinels in the streaming event parser. | ||
| - Stop defaulting Anthropic tool definitions to `strict: true`. | ||
| - Add a live verification script at `scripts/live.ts`. | ||
|
|
||
| ## Live results against current gateway | ||
|
|
||
| Environment: | ||
|
|
||
| - `ANTHROPIC_BASE_URL=https://copilot.api.menci.xyz` | ||
| - `ANTHROPIC_MODEL=claude-opus-4.6-1m` | ||
|
|
||
| Actual API base used by the live script: | ||
|
|
||
| - `https://copilot.api.menci.xyz/v1/` | ||
|
|
||
| Results: | ||
|
|
||
| - `countTokens`: failed. | ||
| - Gateway response: `{"error":{"type":"invalid_request_error","message":"Failed to count tokens: malloc is not a function"}}` | ||
| - `messages` basic stream: passed. | ||
| - `messages` thinking stream: passed. | ||
| - `messages` tool loop: passed when `tool_choice` was omitted and the prompt strongly required tool use. | ||
|
|
||
| Additional manual verification: | ||
|
|
||
| - Raw streaming output from the gateway ends with `data: [DONE]` after `event: message_stop`. | ||
| - `tool_choice: { type: 'tool', name: 'add' }` and `tool_choice: { type: 'any' }` both failed through the gateway with: | ||
| - `Thinking may not be enabled when tool_choice forces tool use.` | ||
|
|
||
| ## What this means for xsAI | ||
|
|
||
| - Ignoring `[DONE]` is required for real proxy compatibility. | ||
| - Defaulting Anthropic tools to `strict: true` is too aggressive for compatibility layers. | ||
| - The remaining `countTokens` failure is not explained by xsAI request shape. | ||
|
|
||
| ## copilot-deno findings | ||
|
|
||
| ### Endpoint shape | ||
|
|
||
| - The app only exposes `POST /v1/messages` and `POST /v1/messages/count_tokens`. | ||
| - It does not expose `/messages` or `/messages/count_tokens`. | ||
|
|
||
| ### Native messages proxy behavior | ||
|
|
||
| - Native `/v1/messages` requests are forwarded through `proxySSE(...)` without filtering `[DONE]`. | ||
| - This is why xsAI must tolerate a terminal `[DONE]` line even though Anthropic-native streams do not require it. | ||
|
|
||
| ### Request rewriting | ||
|
|
||
| - `web_search` tools are stripped before forwarding. | ||
| - The reserved keyword `x-anthropic-billing-header` is stripped from prompts. | ||
| - Invalid thinking blocks are removed before native forwarding. | ||
| - If the selected model advertises `adaptive_thinking`, the route overwrites the request with `payload.thinking = { type: 'adaptive' }` and also injects `output_config.effort = 'high'` when missing. | ||
|
|
||
| ### Tool schema shape | ||
|
|
||
| - `copilot-deno`'s local Anthropic type for tools is: | ||
| - `name` | ||
| - `description?` | ||
| - `input_schema` | ||
| - It does not include `strict` in its local Anthropic route types. | ||
|
|
||
| ### Why `countTokens` fails on the deployed gateway | ||
|
|
||
| - The route tries to use `@anthropic-ai/tokenizer` for Claude models. | ||
| - It only falls back to estimation if loading the tokenizer fails. | ||
| - It does not fall back if calling the tokenizer function throws. | ||
| - The deployed error `malloc is not a function` strongly suggests the tokenizer function is being loaded but then crashing at runtime in the deployed environment. | ||
|
|
||
| ## Suggested follow-up in copilot-deno | ||
|
|
||
| - Add a second fallback around `countFn(extractPayloadText(payload))` so tokenizer invocation failures downgrade to estimation instead of returning 400. | ||
| - Decide whether native `/v1/messages` should strip trailing `[DONE]` when proxying Anthropic-compatible streams. | ||
| - Decide whether forced `tool_choice` should disable adaptive thinking rather than returning an upstream validation error. |
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 @@ | ||
| https://xsai.js.org/docs/packages-ext/messages |
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,49 @@ | ||
| { | ||
| "name": "@xsai-ext/messages", | ||
| "type": "module", | ||
| "version": "0.4.4", | ||
| "description": "extra-small AI SDK.", | ||
| "author": "Moeru AI", | ||
| "license": "MIT", | ||
| "homepage": "https://xsai.js.org", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/moeru-ai/xsai.git", | ||
| "directory": "packages-ext/messages" | ||
| }, | ||
| "bugs": "https://github.com/moeru-ai/xsai/issues", | ||
| "keywords": [ | ||
| "xsai", | ||
| "anthropic", | ||
| "ai" | ||
| ], | ||
| "sideEffects": false, | ||
| "exports": "./src/index.ts", | ||
| "files": [ | ||
| "dist" | ||
| ], | ||
| "publishConfig": { | ||
| "exports": { | ||
| ".": { | ||
| "types": "./dist/index.d.ts", | ||
| "default": "./dist/index.js" | ||
| }, | ||
| "./package.json": "./package.json" | ||
| }, | ||
| "main": "./dist/index.js", | ||
| "types": "./dist/index.d.ts" | ||
| }, | ||
| "scripts": { | ||
| "build": "pkgroll", | ||
| "test": "vitest run", | ||
| "test:live": "tsx scripts/live.ts" | ||
| }, | ||
| "dependencies": { | ||
| "@xsai/shared": "workspace:~", | ||
| "eventsource-parser": "catalog:" | ||
| }, | ||
| "devDependencies": { | ||
| "@standard-schema/spec": "catalog:", | ||
| "zod": "catalog:schema-dev" | ||
| } | ||
| } | ||
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.
The
@standard-schema/specpackage is used for types in the public API of this package (e.g., insrc/types/anthropic-tool.ts). Therefore, it should be listed underdependenciesinstead ofdevDependenciesto ensure its types are available to consumers of this package.