mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-08-27 14:17:10 -04:00
Token-level cache hit rate, compression-vs-cache tracking, dashboard SQL, security plan
Cache stats: - hit_rate is now token-level (cache_read / total_input) not request-level - Track uncached_input_tokens per provider in metrics - Preserve request_hit_rate as secondary metric Compression-vs-cache: - Detect when compression busts the prefix cache (expected_cached - actual_read) - Two simple session-level numbers: tokens_saved vs cache_bust_tokens - Log CACHE-BUST per request, aggregate in /stats and telemetry beacon - Single new column in proxy_telemetry_v2: cache_bust_tokens Dashboard infra: - SQL for dashboard_summary table + pg_cron hourly refresh - Hourly + daily aggregation from proxy_telemetry_v2 - Upgrade scripts for adding hourly_stats and cache bust columns
This commit is contained in:
parent
f4160f9e47
commit
14d00c6c74
1656 changed files with 259772 additions and 1 deletions
1
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/.bin/esbuild
generated
vendored
Symbolic link
1
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/.bin/esbuild
generated
vendored
Symbolic link
|
|
@ -0,0 +1 @@
|
|||
../esbuild/bin/esbuild
|
||||
1
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/.bin/tsc
generated
vendored
Symbolic link
1
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/.bin/tsc
generated
vendored
Symbolic link
|
|
@ -0,0 +1 @@
|
|||
../typescript/bin/tsc
|
||||
1
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/.bin/tsserver
generated
vendored
Symbolic link
1
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/.bin/tsserver
generated
vendored
Symbolic link
|
|
@ -0,0 +1 @@
|
|||
../typescript/bin/tsserver
|
||||
1
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/.bin/tsx
generated
vendored
Symbolic link
1
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/.bin/tsx
generated
vendored
Symbolic link
|
|
@ -0,0 +1 @@
|
|||
../tsx/dist/cli.mjs
|
||||
324
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/.package-lock.json
generated
vendored
Normal file
324
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/.package-lock.json
generated
vendored
Normal file
|
|
@ -0,0 +1,324 @@
|
|||
{
|
||||
"name": "@example/node-headroom-compression",
|
||||
"version": "0.0.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"node_modules/@ai-sdk/anthropic": {
|
||||
"version": "3.0.64",
|
||||
"resolved": "https://registry.npmjs.org/@ai-sdk/anthropic/-/anthropic-3.0.64.tgz",
|
||||
"integrity": "sha512-rwLi/Rsuj2pYniQXIrvClHvXDzgM4UQHHnvHTWEF14efnlKclG/1ghpNC+adsRujAbCTr6gRsSbDE2vEqriV7g==",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"@ai-sdk/provider": "3.0.8",
|
||||
"@ai-sdk/provider-utils": "4.0.21"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"zod": "^3.25.76 || ^4.1.8"
|
||||
}
|
||||
},
|
||||
"node_modules/@ai-sdk/gateway": {
|
||||
"version": "3.0.80",
|
||||
"resolved": "https://registry.npmjs.org/@ai-sdk/gateway/-/gateway-3.0.80.tgz",
|
||||
"integrity": "sha512-uM7kpZB5l977lW7+2X1+klBUxIZQ78+1a9jHlaHFEzcOcmmslTl3sdP0QqfuuBcO0YBM2gwOiqVdp8i4TRQYcw==",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"@ai-sdk/provider": "3.0.8",
|
||||
"@ai-sdk/provider-utils": "4.0.21",
|
||||
"@vercel/oidc": "3.1.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"zod": "^3.25.76 || ^4.1.8"
|
||||
}
|
||||
},
|
||||
"node_modules/@ai-sdk/openai": {
|
||||
"version": "3.0.48",
|
||||
"resolved": "https://registry.npmjs.org/@ai-sdk/openai/-/openai-3.0.48.tgz",
|
||||
"integrity": "sha512-ALmj/53EXpcRqMbGpPJPP4UOSWw0q4VGpnDo7YctvsynjkrKDmoneDG/1a7VQnSPYHnJp6tTRMf5ZdxZ5whulg==",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"@ai-sdk/provider": "3.0.8",
|
||||
"@ai-sdk/provider-utils": "4.0.21"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"zod": "^3.25.76 || ^4.1.8"
|
||||
}
|
||||
},
|
||||
"node_modules/@ai-sdk/provider": {
|
||||
"version": "3.0.8",
|
||||
"resolved": "https://registry.npmjs.org/@ai-sdk/provider/-/provider-3.0.8.tgz",
|
||||
"integrity": "sha512-oGMAgGoQdBXbZqNG0Ze56CHjDZ1IDYOwGYxYjO5KLSlz5HiNQ9udIXsPZ61VWaHGZ5XW/jyjmr6t2xz2jGVwbQ==",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"json-schema": "^0.4.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
}
|
||||
},
|
||||
"node_modules/@ai-sdk/provider-utils": {
|
||||
"version": "4.0.21",
|
||||
"resolved": "https://registry.npmjs.org/@ai-sdk/provider-utils/-/provider-utils-4.0.21.tgz",
|
||||
"integrity": "sha512-MtFUYI1/8mgDvRmaBDjbLJPFFrMG777AvSgyIFQtZHIMzm88R/12vYBBpnk7pfiWLFE1DSZzY4WDYzGbKAcmiw==",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"@ai-sdk/provider": "3.0.8",
|
||||
"@standard-schema/spec": "^1.1.0",
|
||||
"eventsource-parser": "^3.0.6"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"zod": "^3.25.76 || ^4.1.8"
|
||||
}
|
||||
},
|
||||
"node_modules/@esbuild/darwin-arm64": {
|
||||
"version": "0.27.4",
|
||||
"resolved": "https://registry.npmjs.org/@esbuild/darwin-arm64/-/darwin-arm64-0.27.4.tgz",
|
||||
"integrity": "sha512-b7xaGIwdJlht8ZFCvMkpDN6uiSmnxxK56N2GDTMYPr2/gzvfdQN8rTfBsvVKmIVY/X7EM+/hJKEIbbHs9oA4tQ==",
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"darwin"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
}
|
||||
},
|
||||
"node_modules/@opentelemetry/api": {
|
||||
"version": "1.9.0",
|
||||
"resolved": "https://registry.npmjs.org/@opentelemetry/api/-/api-1.9.0.tgz",
|
||||
"integrity": "sha512-3giAOQvZiH5F9bMlMiv8+GSPMeqg0dbaeo58/0SlA9sxSqZhnUtxzX9/2FzyhS9sWQf5S0GJE0AKBrFqjpeYcg==",
|
||||
"license": "Apache-2.0",
|
||||
"engines": {
|
||||
"node": ">=8.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@standard-schema/spec": {
|
||||
"version": "1.1.0",
|
||||
"resolved": "https://registry.npmjs.org/@standard-schema/spec/-/spec-1.1.0.tgz",
|
||||
"integrity": "sha512-l2aFy5jALhniG5HgqrD6jXLi/rUWrKvqN/qJx6yoJsgKhblVd+iqqU4RCXavm/jPityDo5TCvKMnpjKnOriy0w==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/@vercel/oidc": {
|
||||
"version": "3.1.0",
|
||||
"resolved": "https://registry.npmjs.org/@vercel/oidc/-/oidc-3.1.0.tgz",
|
||||
"integrity": "sha512-Fw28YZpRnA3cAHHDlkt7xQHiJ0fcL+NRcIqsocZQUSmbzeIKRpwttJjik5ZGanXP+vlA4SbTg+AbA3bP363l+w==",
|
||||
"license": "Apache-2.0",
|
||||
"engines": {
|
||||
"node": ">= 20"
|
||||
}
|
||||
},
|
||||
"node_modules/ai": {
|
||||
"version": "6.0.138",
|
||||
"resolved": "https://registry.npmjs.org/ai/-/ai-6.0.138.tgz",
|
||||
"integrity": "sha512-49OfPe0f5uxJ6jUdA5BBXjIinP6+ZdYfAtpF2aEH64GA5wPcxH2rf/TBUQQ0bbamBz/D+TLMV18xilZqOC+zaA==",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"@ai-sdk/gateway": "3.0.80",
|
||||
"@ai-sdk/provider": "3.0.8",
|
||||
"@ai-sdk/provider-utils": "4.0.21",
|
||||
"@opentelemetry/api": "1.9.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"zod": "^3.25.76 || ^4.1.8"
|
||||
}
|
||||
},
|
||||
"node_modules/dotenv": {
|
||||
"version": "17.3.1",
|
||||
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-17.3.1.tgz",
|
||||
"integrity": "sha512-IO8C/dzEb6O3F9/twg6ZLXz164a2fhTnEWb95H23Dm4OuN+92NmEAlTrupP9VW6Jm3sO26tQlqyvyi4CsnY9GA==",
|
||||
"license": "BSD-2-Clause",
|
||||
"engines": {
|
||||
"node": ">=12"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://dotenvx.com"
|
||||
}
|
||||
},
|
||||
"node_modules/esbuild": {
|
||||
"version": "0.27.4",
|
||||
"resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.27.4.tgz",
|
||||
"integrity": "sha512-Rq4vbHnYkK5fws5NF7MYTU68FPRE1ajX7heQ/8QXXWqNgqqJ/GkmmyxIzUnf2Sr/bakf8l54716CcMGHYhMrrQ==",
|
||||
"dev": true,
|
||||
"hasInstallScript": true,
|
||||
"license": "MIT",
|
||||
"bin": {
|
||||
"esbuild": "bin/esbuild"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
},
|
||||
"optionalDependencies": {
|
||||
"@esbuild/aix-ppc64": "0.27.4",
|
||||
"@esbuild/android-arm": "0.27.4",
|
||||
"@esbuild/android-arm64": "0.27.4",
|
||||
"@esbuild/android-x64": "0.27.4",
|
||||
"@esbuild/darwin-arm64": "0.27.4",
|
||||
"@esbuild/darwin-x64": "0.27.4",
|
||||
"@esbuild/freebsd-arm64": "0.27.4",
|
||||
"@esbuild/freebsd-x64": "0.27.4",
|
||||
"@esbuild/linux-arm": "0.27.4",
|
||||
"@esbuild/linux-arm64": "0.27.4",
|
||||
"@esbuild/linux-ia32": "0.27.4",
|
||||
"@esbuild/linux-loong64": "0.27.4",
|
||||
"@esbuild/linux-mips64el": "0.27.4",
|
||||
"@esbuild/linux-ppc64": "0.27.4",
|
||||
"@esbuild/linux-riscv64": "0.27.4",
|
||||
"@esbuild/linux-s390x": "0.27.4",
|
||||
"@esbuild/linux-x64": "0.27.4",
|
||||
"@esbuild/netbsd-arm64": "0.27.4",
|
||||
"@esbuild/netbsd-x64": "0.27.4",
|
||||
"@esbuild/openbsd-arm64": "0.27.4",
|
||||
"@esbuild/openbsd-x64": "0.27.4",
|
||||
"@esbuild/openharmony-arm64": "0.27.4",
|
||||
"@esbuild/sunos-x64": "0.27.4",
|
||||
"@esbuild/win32-arm64": "0.27.4",
|
||||
"@esbuild/win32-ia32": "0.27.4",
|
||||
"@esbuild/win32-x64": "0.27.4"
|
||||
}
|
||||
},
|
||||
"node_modules/eventsource-parser": {
|
||||
"version": "3.0.6",
|
||||
"resolved": "https://registry.npmjs.org/eventsource-parser/-/eventsource-parser-3.0.6.tgz",
|
||||
"integrity": "sha512-Vo1ab+QXPzZ4tCa8SwIHJFaSzy4R6SHf7BY79rFBDf0idraZWAkYrDjDj8uWaSm3S2TK+hJ7/t1CEmZ7jXw+pg==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=18.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/fsevents": {
|
||||
"version": "2.3.3",
|
||||
"resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz",
|
||||
"integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==",
|
||||
"dev": true,
|
||||
"hasInstallScript": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"darwin"
|
||||
],
|
||||
"engines": {
|
||||
"node": "^8.16.0 || ^10.6.0 || >=11.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/get-tsconfig": {
|
||||
"version": "4.13.7",
|
||||
"resolved": "https://registry.npmjs.org/get-tsconfig/-/get-tsconfig-4.13.7.tgz",
|
||||
"integrity": "sha512-7tN6rFgBlMgpBML5j8typ92BKFi2sFQvIdpAqLA2beia5avZDrMs0FLZiM5etShWq5irVyGcGMEA1jcDaK7A/Q==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"resolve-pkg-maps": "^1.0.0"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/privatenumber/get-tsconfig?sponsor=1"
|
||||
}
|
||||
},
|
||||
"node_modules/headroom-ai": {
|
||||
"version": "0.1.0",
|
||||
"resolved": "https://registry.npmjs.org/headroom-ai/-/headroom-ai-0.1.0.tgz",
|
||||
"integrity": "sha512-SM5i2kptwsO+KnQqpaLThZVx1ZrmobVw5JRXwF5OvnMvhV/3WbwUI+VvXHTwTCvVq+AAyhkhuVcNXod1piAVuQ==",
|
||||
"license": "Apache-2.0",
|
||||
"engines": {
|
||||
"node": ">=18.0.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@ai-sdk/provider": ">=1.0.0",
|
||||
"@anthropic-ai/sdk": ">=0.30.0",
|
||||
"ai": ">=6.0.0",
|
||||
"openai": ">=4.0.0"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@ai-sdk/provider": {
|
||||
"optional": true
|
||||
},
|
||||
"@anthropic-ai/sdk": {
|
||||
"optional": true
|
||||
},
|
||||
"ai": {
|
||||
"optional": true
|
||||
},
|
||||
"openai": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/json-schema": {
|
||||
"version": "0.4.0",
|
||||
"resolved": "https://registry.npmjs.org/json-schema/-/json-schema-0.4.0.tgz",
|
||||
"integrity": "sha512-es94M3nTIfsEPisRafak+HDLfHXnKBhV3vU5eqPcS3flIWqcxJWgXHXiey3YrpaNsanY5ei1VoYEbOzijuq9BA==",
|
||||
"license": "(AFL-2.1 OR BSD-3-Clause)"
|
||||
},
|
||||
"node_modules/resolve-pkg-maps": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/resolve-pkg-maps/-/resolve-pkg-maps-1.0.0.tgz",
|
||||
"integrity": "sha512-seS2Tj26TBVOC2NIc2rOe2y2ZO7efxITtLZcGSOnHHNOQ7CkiUBfw0Iw2ck6xkIhPwLhKNLS8BO+hEpngQlqzw==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"funding": {
|
||||
"url": "https://github.com/privatenumber/resolve-pkg-maps?sponsor=1"
|
||||
}
|
||||
},
|
||||
"node_modules/tsx": {
|
||||
"version": "4.21.0",
|
||||
"resolved": "https://registry.npmjs.org/tsx/-/tsx-4.21.0.tgz",
|
||||
"integrity": "sha512-5C1sg4USs1lfG0GFb2RLXsdpXqBSEhAaA/0kPL01wxzpMqLILNxIxIOKiILz+cdg/pLnOUxFYOR5yhHU666wbw==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"esbuild": "~0.27.0",
|
||||
"get-tsconfig": "^4.7.5"
|
||||
},
|
||||
"bin": {
|
||||
"tsx": "dist/cli.mjs"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18.0.0"
|
||||
},
|
||||
"optionalDependencies": {
|
||||
"fsevents": "~2.3.3"
|
||||
}
|
||||
},
|
||||
"node_modules/typescript": {
|
||||
"version": "6.0.2",
|
||||
"resolved": "https://registry.npmjs.org/typescript/-/typescript-6.0.2.tgz",
|
||||
"integrity": "sha512-bGdAIrZ0wiGDo5l8c++HWtbaNCWTS4UTv7RaTH/ThVIgjkveJt83m74bBHMJkuCbslY8ixgLBVZJIOiQlQTjfQ==",
|
||||
"dev": true,
|
||||
"license": "Apache-2.0",
|
||||
"bin": {
|
||||
"tsc": "bin/tsc",
|
||||
"tsserver": "bin/tsserver"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=14.17"
|
||||
}
|
||||
},
|
||||
"node_modules/zod": {
|
||||
"version": "4.3.6",
|
||||
"resolved": "https://registry.npmjs.org/zod/-/zod-4.3.6.tgz",
|
||||
"integrity": "sha512-rftlrkhHZOcjDwkGlnUtZZkvaPHCsDATp4pGpuOOMDaTdDDXF91wuVDJoWoPsKX/3YPQ5fHuF3STjcYyKr+Qhg==",
|
||||
"license": "MIT",
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/colinhacks"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
2560
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/CHANGELOG.md
generated
vendored
Normal file
2560
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/CHANGELOG.md
generated
vendored
Normal file
File diff suppressed because it is too large
Load diff
13
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/LICENSE
generated
vendored
Normal file
13
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/LICENSE
generated
vendored
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
Copyright 2023 Vercel, Inc.
|
||||
|
||||
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.
|
||||
43
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/README.md
generated
vendored
Normal file
43
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/README.md
generated
vendored
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
# AI SDK - Anthropic Provider
|
||||
|
||||
The **[Anthropic provider](https://ai-sdk.dev/providers/ai-sdk-providers/anthropic)** for the [AI SDK](https://ai-sdk.dev/docs) contains language model support for the [Anthropic Messages API](https://docs.anthropic.com/claude/reference/messages_post).
|
||||
|
||||
## Setup
|
||||
|
||||
The Anthropic provider is available in the `@ai-sdk/anthropic` module. You can install it with
|
||||
|
||||
```bash
|
||||
npm i @ai-sdk/anthropic
|
||||
```
|
||||
|
||||
## Skill for Coding Agents
|
||||
|
||||
If you use coding agents such as Claude Code or Cursor, we highly recommend adding the AI SDK skill to your repository:
|
||||
|
||||
```shell
|
||||
npx skills add vercel/ai
|
||||
```
|
||||
|
||||
## Provider Instance
|
||||
|
||||
You can import the default provider instance `anthropic` from `@ai-sdk/anthropic`:
|
||||
|
||||
```ts
|
||||
import { anthropic } from '@ai-sdk/anthropic';
|
||||
```
|
||||
|
||||
## Example
|
||||
|
||||
```ts
|
||||
import { anthropic } from '@ai-sdk/anthropic';
|
||||
import { generateText } from 'ai';
|
||||
|
||||
const { text } = await generateText({
|
||||
model: anthropic('claude-3-haiku-20240307'),
|
||||
prompt: 'Write a vegetarian lasagna recipe for 4 people.',
|
||||
});
|
||||
```
|
||||
|
||||
## Documentation
|
||||
|
||||
Please check out the **[Anthropic provider documentation](https://ai-sdk.dev/providers/ai-sdk-providers/anthropic)** for more information.
|
||||
1368
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/docs/05-anthropic.mdx
generated
vendored
Normal file
1368
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/docs/05-anthropic.mdx
generated
vendored
Normal file
File diff suppressed because it is too large
Load diff
1
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/internal.d.ts
generated
vendored
Normal file
1
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/internal.d.ts
generated
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
export * from './dist/internal';
|
||||
81
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/package.json
generated
vendored
Normal file
81
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/package.json
generated
vendored
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
{
|
||||
"name": "@ai-sdk/anthropic",
|
||||
"version": "3.0.64",
|
||||
"license": "Apache-2.0",
|
||||
"sideEffects": false,
|
||||
"main": "./dist/index.js",
|
||||
"module": "./dist/index.mjs",
|
||||
"types": "./dist/index.d.ts",
|
||||
"files": [
|
||||
"dist/**/*",
|
||||
"docs/**/*",
|
||||
"src",
|
||||
"!src/**/*.test.ts",
|
||||
"!src/**/*.test-d.ts",
|
||||
"!src/**/__snapshots__",
|
||||
"!src/**/__fixtures__",
|
||||
"CHANGELOG.md",
|
||||
"README.md",
|
||||
"internal.d.ts"
|
||||
],
|
||||
"directories": {
|
||||
"doc": "./docs"
|
||||
},
|
||||
"exports": {
|
||||
"./package.json": "./package.json",
|
||||
".": {
|
||||
"types": "./dist/index.d.ts",
|
||||
"import": "./dist/index.mjs",
|
||||
"require": "./dist/index.js"
|
||||
},
|
||||
"./internal": {
|
||||
"types": "./dist/internal/index.d.ts",
|
||||
"import": "./dist/internal/index.mjs",
|
||||
"module": "./dist/internal/index.mjs",
|
||||
"require": "./dist/internal/index.js"
|
||||
}
|
||||
},
|
||||
"dependencies": {
|
||||
"@ai-sdk/provider": "3.0.8",
|
||||
"@ai-sdk/provider-utils": "4.0.21"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "20.17.24",
|
||||
"tsup": "^8",
|
||||
"typescript": "5.8.3",
|
||||
"zod": "3.25.76",
|
||||
"@ai-sdk/test-server": "1.0.3",
|
||||
"@vercel/ai-tsconfig": "0.0.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"zod": "^3.25.76 || ^4.1.8"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
"homepage": "https://ai-sdk.dev/docs",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/vercel/ai.git"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/vercel/ai/issues"
|
||||
},
|
||||
"keywords": [
|
||||
"ai"
|
||||
],
|
||||
"scripts": {
|
||||
"build": "pnpm clean && tsup --tsconfig tsconfig.build.json",
|
||||
"build:watch": "pnpm clean && tsup --watch --tsconfig tsconfig.build.json",
|
||||
"clean": "del-cli dist docs *.tsbuildinfo",
|
||||
"type-check": "tsc --build",
|
||||
"test": "pnpm test:node && pnpm test:edge",
|
||||
"test:update": "pnpm test:node -u",
|
||||
"test:watch": "vitest --config vitest.node.config.js",
|
||||
"test:edge": "vitest --config vitest.edge.config.js --run",
|
||||
"test:node": "vitest --config vitest.node.config.js --run"
|
||||
}
|
||||
}
|
||||
26
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/anthropic-error.ts
generated
vendored
Normal file
26
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/anthropic-error.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
import {
|
||||
createJsonErrorResponseHandler,
|
||||
InferSchema,
|
||||
lazySchema,
|
||||
zodSchema,
|
||||
} from '@ai-sdk/provider-utils';
|
||||
import { z } from 'zod/v4';
|
||||
|
||||
export const anthropicErrorDataSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.object({
|
||||
type: z.literal('error'),
|
||||
error: z.object({
|
||||
type: z.string(),
|
||||
message: z.string(),
|
||||
}),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
export type AnthropicErrorData = InferSchema<typeof anthropicErrorDataSchema>;
|
||||
|
||||
export const anthropicFailedResponseHandler = createJsonErrorResponseHandler({
|
||||
errorSchema: anthropicErrorDataSchema,
|
||||
errorToMessage: data => data.error.message,
|
||||
});
|
||||
143
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/anthropic-message-metadata.ts
generated
vendored
Normal file
143
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/anthropic-message-metadata.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,143 @@
|
|||
import { JSONObject } from '@ai-sdk/provider';
|
||||
|
||||
/**
|
||||
* Represents a single iteration in the usage breakdown.
|
||||
* When compaction occurs, the API returns an iterations array showing
|
||||
* usage for each sampling iteration (compaction + message).
|
||||
*/
|
||||
export interface AnthropicUsageIteration {
|
||||
type: 'compaction' | 'message';
|
||||
|
||||
/**
|
||||
* Number of input tokens consumed in this iteration.
|
||||
*/
|
||||
inputTokens: number;
|
||||
|
||||
/**
|
||||
* Number of output tokens generated in this iteration.
|
||||
*/
|
||||
outputTokens: number;
|
||||
}
|
||||
|
||||
export interface AnthropicMessageMetadata {
|
||||
usage: JSONObject;
|
||||
// TODO remove cacheCreationInputTokens in AI SDK 6
|
||||
// (use value in usage object instead)
|
||||
cacheCreationInputTokens: number | null;
|
||||
stopSequence: string | null;
|
||||
|
||||
/**
|
||||
* Usage breakdown by iteration when compaction is triggered.
|
||||
*
|
||||
* When compaction occurs, this array contains usage for each sampling iteration.
|
||||
* The first iteration is typically the compaction step, followed by the main
|
||||
* message iteration.
|
||||
*/
|
||||
iterations: AnthropicUsageIteration[] | null;
|
||||
|
||||
/**
|
||||
* Information about the container used in this request.
|
||||
*
|
||||
* This will be non-null if a container tool (e.g., code execution) was used.
|
||||
* Information about the container used in the request (for the code execution tool).
|
||||
*/
|
||||
container: {
|
||||
/**
|
||||
* The time at which the container will expire (RFC3339 timestamp).
|
||||
*/
|
||||
expiresAt: string;
|
||||
|
||||
/**
|
||||
* Identifier for the container used in this request.
|
||||
*/
|
||||
id: string;
|
||||
|
||||
/**
|
||||
* Skills loaded in the container.
|
||||
*/
|
||||
skills: Array<{
|
||||
/**
|
||||
* Type of skill: either 'anthropic' (built-in) or 'custom' (user-defined).
|
||||
*/
|
||||
type: 'anthropic' | 'custom';
|
||||
|
||||
/**
|
||||
* Skill ID (1-64 characters).
|
||||
*/
|
||||
skillId: string;
|
||||
|
||||
/**
|
||||
* Skill version or 'latest' for most recent version (1-64 characters).
|
||||
*/
|
||||
version: string;
|
||||
}> | null;
|
||||
} | null;
|
||||
|
||||
/**
|
||||
* Context management response.
|
||||
*
|
||||
* Information about context management strategies applied during the request.
|
||||
*/
|
||||
contextManagement: {
|
||||
/**
|
||||
* List of context management edits that were applied.
|
||||
* Each item in the array is a specific type of context management edit.
|
||||
*/
|
||||
appliedEdits: Array<
|
||||
/**
|
||||
* Represents an edit where a certain number of tool uses and input tokens were cleared.
|
||||
*/
|
||||
| {
|
||||
/**
|
||||
* The type of context management edit applied.
|
||||
* Possible value: 'clear_tool_uses_20250919'
|
||||
*/
|
||||
type: 'clear_tool_uses_20250919';
|
||||
|
||||
/**
|
||||
* Number of tool uses that were cleared by this edit.
|
||||
* Minimum: 0
|
||||
*/
|
||||
clearedToolUses: number;
|
||||
|
||||
/**
|
||||
* Number of input tokens cleared by this edit.
|
||||
* Minimum: 0
|
||||
*/
|
||||
clearedInputTokens: number;
|
||||
}
|
||||
/**
|
||||
* Represents an edit where a certain number of thinking turns and input tokens were cleared.
|
||||
*/
|
||||
| {
|
||||
/**
|
||||
* The type of context management edit applied.
|
||||
* Possible value: 'clear_thinking_20251015'
|
||||
*/
|
||||
type: 'clear_thinking_20251015';
|
||||
|
||||
/**
|
||||
* Number of thinking turns that were cleared by this edit.
|
||||
* Minimum: 0
|
||||
*/
|
||||
clearedThinkingTurns: number;
|
||||
|
||||
/**
|
||||
* Number of input tokens cleared by this edit.
|
||||
* Minimum: 0
|
||||
*/
|
||||
clearedInputTokens: number;
|
||||
}
|
||||
/**
|
||||
* Represents a compaction edit where the conversation context was summarized.
|
||||
*/
|
||||
| {
|
||||
/**
|
||||
* The type of context management edit applied.
|
||||
* Possible value: 'compact_20260112'
|
||||
*/
|
||||
type: 'compact_20260112';
|
||||
}
|
||||
>;
|
||||
} | null;
|
||||
}
|
||||
1344
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/anthropic-messages-api.ts
generated
vendored
Normal file
1344
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/anthropic-messages-api.ts
generated
vendored
Normal file
File diff suppressed because it is too large
Load diff
2392
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/anthropic-messages-language-model.ts
generated
vendored
Normal file
2392
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/anthropic-messages-language-model.ts
generated
vendored
Normal file
File diff suppressed because it is too large
Load diff
263
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/anthropic-messages-options.ts
generated
vendored
Normal file
263
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/anthropic-messages-options.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,263 @@
|
|||
import { z } from 'zod/v4';
|
||||
|
||||
// https://docs.claude.com/en/docs/about-claude/models/overview
|
||||
export type AnthropicMessagesModelId =
|
||||
| 'claude-3-haiku-20240307'
|
||||
| 'claude-haiku-4-5-20251001'
|
||||
| 'claude-haiku-4-5'
|
||||
| 'claude-opus-4-0'
|
||||
| 'claude-opus-4-20250514'
|
||||
| 'claude-opus-4-1-20250805'
|
||||
| 'claude-opus-4-1'
|
||||
| 'claude-opus-4-5'
|
||||
| 'claude-opus-4-5-20251101'
|
||||
| 'claude-sonnet-4-0'
|
||||
| 'claude-sonnet-4-20250514'
|
||||
| 'claude-sonnet-4-5-20250929'
|
||||
| 'claude-sonnet-4-5'
|
||||
| 'claude-sonnet-4-6'
|
||||
| 'claude-opus-4-6'
|
||||
| (string & {});
|
||||
|
||||
/**
|
||||
* Anthropic file part provider options for document-specific features.
|
||||
* These options apply to individual file parts (documents).
|
||||
*/
|
||||
export const anthropicFilePartProviderOptions = z.object({
|
||||
/**
|
||||
* Citation configuration for this document.
|
||||
* When enabled, this document will generate citations in the response.
|
||||
*/
|
||||
citations: z
|
||||
.object({
|
||||
/**
|
||||
* Enable citations for this document
|
||||
*/
|
||||
enabled: z.boolean(),
|
||||
})
|
||||
.optional(),
|
||||
|
||||
/**
|
||||
* Custom title for the document.
|
||||
* If not provided, the filename will be used.
|
||||
*/
|
||||
title: z.string().optional(),
|
||||
|
||||
/**
|
||||
* Context about the document that will be passed to the model
|
||||
* but not used towards cited content.
|
||||
* Useful for storing document metadata as text or stringified JSON.
|
||||
*/
|
||||
context: z.string().optional(),
|
||||
});
|
||||
|
||||
export type AnthropicFilePartProviderOptions = z.infer<
|
||||
typeof anthropicFilePartProviderOptions
|
||||
>;
|
||||
|
||||
export const anthropicLanguageModelOptions = z.object({
|
||||
/**
|
||||
* Whether to send reasoning to the model.
|
||||
*
|
||||
* This allows you to deactivate reasoning inputs for models that do not support them.
|
||||
*/
|
||||
sendReasoning: z.boolean().optional(),
|
||||
|
||||
/**
|
||||
* Determines how structured outputs are generated.
|
||||
*
|
||||
* - `outputFormat`: Use the `output_config.format` parameter to specify the structured output format.
|
||||
* - `jsonTool`: Use a special 'json' tool to specify the structured output format.
|
||||
* - `auto`: Use 'outputFormat' when supported, otherwise use 'jsonTool' (default).
|
||||
*/
|
||||
structuredOutputMode: z.enum(['outputFormat', 'jsonTool', 'auto']).optional(),
|
||||
|
||||
/**
|
||||
* Configuration for enabling Claude's extended thinking.
|
||||
*
|
||||
* When enabled, responses include thinking content blocks showing Claude's thinking process before the final answer.
|
||||
* Requires a minimum budget of 1,024 tokens and counts towards the `max_tokens` limit.
|
||||
*/
|
||||
thinking: z
|
||||
.discriminatedUnion('type', [
|
||||
z.object({
|
||||
/** for Sonnet 4.6, Opus 4.6, and newer models */
|
||||
type: z.literal('adaptive'),
|
||||
}),
|
||||
z.object({
|
||||
/** for models before Opus 4.6, except Sonnet 4.6 still supports it */
|
||||
type: z.literal('enabled'),
|
||||
budgetTokens: z.number().optional(),
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('disabled'),
|
||||
}),
|
||||
])
|
||||
.optional(),
|
||||
|
||||
/**
|
||||
* Whether to disable parallel function calling during tool use. Default is false.
|
||||
* When set to true, Claude will use at most one tool per response.
|
||||
*/
|
||||
disableParallelToolUse: z.boolean().optional(),
|
||||
|
||||
/**
|
||||
* Cache control settings for this message.
|
||||
* See https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching
|
||||
*/
|
||||
cacheControl: z
|
||||
.object({
|
||||
type: z.literal('ephemeral'),
|
||||
ttl: z.union([z.literal('5m'), z.literal('1h')]).optional(),
|
||||
})
|
||||
.optional(),
|
||||
|
||||
/**
|
||||
* Metadata to include with the request.
|
||||
*
|
||||
* See https://platform.claude.com/docs/en/api/messages/create for details.
|
||||
*/
|
||||
metadata: z
|
||||
.object({
|
||||
/**
|
||||
* An external identifier for the user associated with the request.
|
||||
*
|
||||
* Should be a UUID, hash value, or other opaque identifier.
|
||||
* Must not contain PII (name, email, phone number, etc.).
|
||||
*/
|
||||
userId: z.string().optional(),
|
||||
})
|
||||
.optional(),
|
||||
|
||||
/**
|
||||
* MCP servers to be utilized in this request.
|
||||
*/
|
||||
mcpServers: z
|
||||
.array(
|
||||
z.object({
|
||||
type: z.literal('url'),
|
||||
name: z.string(),
|
||||
url: z.string(),
|
||||
authorizationToken: z.string().nullish(),
|
||||
toolConfiguration: z
|
||||
.object({
|
||||
enabled: z.boolean().nullish(),
|
||||
allowedTools: z.array(z.string()).nullish(),
|
||||
})
|
||||
.nullish(),
|
||||
}),
|
||||
)
|
||||
.optional(),
|
||||
|
||||
/**
|
||||
* Agent Skills configuration. Skills enable Claude to perform specialized tasks
|
||||
* like document processing (PPTX, DOCX, PDF, XLSX) and data analysis.
|
||||
* Requires code execution tool to be enabled.
|
||||
*/
|
||||
container: z
|
||||
.object({
|
||||
id: z.string().optional(),
|
||||
skills: z
|
||||
.array(
|
||||
z.object({
|
||||
type: z.union([z.literal('anthropic'), z.literal('custom')]),
|
||||
skillId: z.string(),
|
||||
version: z.string().optional(),
|
||||
}),
|
||||
)
|
||||
.optional(),
|
||||
})
|
||||
.optional(),
|
||||
|
||||
/**
|
||||
* Whether to enable tool streaming (and structured output streaming).
|
||||
*
|
||||
* When set to false, the model will return all tool calls and results
|
||||
* at once after a delay.
|
||||
*
|
||||
* @default true
|
||||
*/
|
||||
toolStreaming: z.boolean().optional(),
|
||||
|
||||
/**
|
||||
* @default 'high'
|
||||
*/
|
||||
effort: z.enum(['low', 'medium', 'high', 'max']).optional(),
|
||||
|
||||
/**
|
||||
* Enable fast mode for faster inference (2.5x faster output token speeds).
|
||||
* Only supported with claude-opus-4-6.
|
||||
*/
|
||||
speed: z.enum(['fast', 'standard']).optional(),
|
||||
|
||||
/**
|
||||
* A set of beta features to enable.
|
||||
* Allow a provider to receive the full `betas` set if it needs it.
|
||||
*/
|
||||
anthropicBeta: z.array(z.string()).optional(),
|
||||
|
||||
contextManagement: z
|
||||
.object({
|
||||
edits: z.array(
|
||||
z.discriminatedUnion('type', [
|
||||
z.object({
|
||||
type: z.literal('clear_tool_uses_20250919'),
|
||||
trigger: z
|
||||
.discriminatedUnion('type', [
|
||||
z.object({
|
||||
type: z.literal('input_tokens'),
|
||||
value: z.number(),
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('tool_uses'),
|
||||
value: z.number(),
|
||||
}),
|
||||
])
|
||||
.optional(),
|
||||
keep: z
|
||||
.object({
|
||||
type: z.literal('tool_uses'),
|
||||
value: z.number(),
|
||||
})
|
||||
.optional(),
|
||||
clearAtLeast: z
|
||||
.object({
|
||||
type: z.literal('input_tokens'),
|
||||
value: z.number(),
|
||||
})
|
||||
.optional(),
|
||||
clearToolInputs: z.boolean().optional(),
|
||||
excludeTools: z.array(z.string()).optional(),
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('clear_thinking_20251015'),
|
||||
keep: z
|
||||
.union([
|
||||
z.literal('all'),
|
||||
z.object({
|
||||
type: z.literal('thinking_turns'),
|
||||
value: z.number(),
|
||||
}),
|
||||
])
|
||||
.optional(),
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('compact_20260112'),
|
||||
trigger: z
|
||||
.object({
|
||||
type: z.literal('input_tokens'),
|
||||
value: z.number(),
|
||||
})
|
||||
.optional(),
|
||||
pauseAfterCompaction: z.boolean().optional(),
|
||||
instructions: z.string().optional(),
|
||||
}),
|
||||
]),
|
||||
),
|
||||
})
|
||||
.optional(),
|
||||
});
|
||||
|
||||
export type AnthropicLanguageModelOptions = z.infer<
|
||||
typeof anthropicLanguageModelOptions
|
||||
>;
|
||||
416
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/anthropic-prepare-tools.ts
generated
vendored
Normal file
416
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/anthropic-prepare-tools.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,416 @@
|
|||
import {
|
||||
LanguageModelV3CallOptions,
|
||||
SharedV3Warning,
|
||||
UnsupportedFunctionalityError,
|
||||
} from '@ai-sdk/provider';
|
||||
import { AnthropicTool, AnthropicToolChoice } from './anthropic-messages-api';
|
||||
import { CacheControlValidator } from './get-cache-control';
|
||||
import { textEditor_20250728ArgsSchema } from './tool/text-editor_20250728';
|
||||
import { webSearch_20260209ArgsSchema } from './tool/web-search_20260209';
|
||||
import { webSearch_20250305ArgsSchema } from './tool/web-search_20250305';
|
||||
import { webFetch_20260209ArgsSchema } from './tool/web-fetch-20260209';
|
||||
import { webFetch_20250910ArgsSchema } from './tool/web-fetch-20250910';
|
||||
import { validateTypes } from '@ai-sdk/provider-utils';
|
||||
|
||||
export interface AnthropicToolOptions {
|
||||
deferLoading?: boolean;
|
||||
allowedCallers?: Array<
|
||||
'direct' | 'code_execution_20250825' | 'code_execution_20260120'
|
||||
>;
|
||||
eagerInputStreaming?: boolean;
|
||||
}
|
||||
|
||||
export async function prepareTools({
|
||||
tools,
|
||||
toolChoice,
|
||||
disableParallelToolUse,
|
||||
cacheControlValidator,
|
||||
supportsStructuredOutput,
|
||||
supportsStrictTools,
|
||||
}: {
|
||||
tools: LanguageModelV3CallOptions['tools'];
|
||||
toolChoice: LanguageModelV3CallOptions['toolChoice'] | undefined;
|
||||
disableParallelToolUse?: boolean;
|
||||
cacheControlValidator?: CacheControlValidator;
|
||||
|
||||
/**
|
||||
* Whether the model supports native structured output response format.
|
||||
*/
|
||||
supportsStructuredOutput: boolean;
|
||||
|
||||
/**
|
||||
* Whether the model supports strict mode on tool definitions.
|
||||
*/
|
||||
supportsStrictTools: boolean;
|
||||
}): Promise<{
|
||||
tools: Array<AnthropicTool> | undefined;
|
||||
toolChoice: AnthropicToolChoice | undefined;
|
||||
toolWarnings: SharedV3Warning[];
|
||||
betas: Set<string>;
|
||||
}> {
|
||||
// when the tools array is empty, change it to undefined to prevent errors:
|
||||
tools = tools?.length ? tools : undefined;
|
||||
|
||||
const toolWarnings: SharedV3Warning[] = [];
|
||||
const betas = new Set<string>();
|
||||
const validator = cacheControlValidator || new CacheControlValidator();
|
||||
|
||||
if (tools == null) {
|
||||
return { tools: undefined, toolChoice: undefined, toolWarnings, betas };
|
||||
}
|
||||
|
||||
const anthropicTools: AnthropicTool[] = [];
|
||||
|
||||
for (const tool of tools) {
|
||||
switch (tool.type) {
|
||||
case 'function': {
|
||||
const cacheControl = validator.getCacheControl(tool.providerOptions, {
|
||||
type: 'tool definition',
|
||||
canCache: true,
|
||||
});
|
||||
|
||||
// Read Anthropic-specific provider options
|
||||
const anthropicOptions = tool.providerOptions?.anthropic as
|
||||
| AnthropicToolOptions
|
||||
| undefined;
|
||||
// eager_input_streaming is only supported on custom (function) tools
|
||||
const eagerInputStreaming = anthropicOptions?.eagerInputStreaming;
|
||||
const deferLoading = anthropicOptions?.deferLoading;
|
||||
const allowedCallers = anthropicOptions?.allowedCallers;
|
||||
|
||||
if (!supportsStrictTools && tool.strict != null) {
|
||||
toolWarnings.push({
|
||||
type: 'unsupported',
|
||||
feature: 'strict',
|
||||
details: `Tool '${tool.name}' has strict: ${tool.strict}, but strict mode is not supported by this provider. The strict property will be ignored.`,
|
||||
});
|
||||
}
|
||||
|
||||
anthropicTools.push({
|
||||
name: tool.name,
|
||||
description: tool.description,
|
||||
input_schema: tool.inputSchema,
|
||||
cache_control: cacheControl,
|
||||
...(eagerInputStreaming ? { eager_input_streaming: true } : {}),
|
||||
...(supportsStrictTools === true && tool.strict != null
|
||||
? { strict: tool.strict }
|
||||
: {}),
|
||||
...(deferLoading != null ? { defer_loading: deferLoading } : {}),
|
||||
...(allowedCallers != null
|
||||
? { allowed_callers: allowedCallers }
|
||||
: {}),
|
||||
...(tool.inputExamples != null
|
||||
? {
|
||||
input_examples: tool.inputExamples.map(
|
||||
example => example.input,
|
||||
),
|
||||
}
|
||||
: {}),
|
||||
});
|
||||
|
||||
if (supportsStructuredOutput === true) {
|
||||
betas.add('structured-outputs-2025-11-13');
|
||||
}
|
||||
|
||||
if (tool.inputExamples != null || allowedCallers != null) {
|
||||
betas.add('advanced-tool-use-2025-11-20');
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 'provider': {
|
||||
// Note: Provider-defined tools don't currently support providerOptions in the SDK,
|
||||
// so cache_control cannot be set on them. The Anthropic API supports caching all tools,
|
||||
// but the SDK would need to be updated to expose providerOptions on provider-defined tools.
|
||||
switch (tool.id) {
|
||||
case 'anthropic.code_execution_20250522': {
|
||||
betas.add('code-execution-2025-05-22');
|
||||
anthropicTools.push({
|
||||
type: 'code_execution_20250522',
|
||||
name: 'code_execution',
|
||||
cache_control: undefined,
|
||||
});
|
||||
break;
|
||||
}
|
||||
case 'anthropic.code_execution_20250825': {
|
||||
betas.add('code-execution-2025-08-25');
|
||||
anthropicTools.push({
|
||||
type: 'code_execution_20250825',
|
||||
name: 'code_execution',
|
||||
});
|
||||
break;
|
||||
}
|
||||
case 'anthropic.code_execution_20260120': {
|
||||
anthropicTools.push({
|
||||
type: 'code_execution_20260120',
|
||||
name: 'code_execution',
|
||||
});
|
||||
break;
|
||||
}
|
||||
case 'anthropic.computer_20250124': {
|
||||
betas.add('computer-use-2025-01-24');
|
||||
anthropicTools.push({
|
||||
name: 'computer',
|
||||
type: 'computer_20250124',
|
||||
display_width_px: tool.args.displayWidthPx as number,
|
||||
display_height_px: tool.args.displayHeightPx as number,
|
||||
display_number: tool.args.displayNumber as number,
|
||||
cache_control: undefined,
|
||||
});
|
||||
break;
|
||||
}
|
||||
case 'anthropic.computer_20251124': {
|
||||
betas.add('computer-use-2025-11-24');
|
||||
anthropicTools.push({
|
||||
name: 'computer',
|
||||
type: 'computer_20251124',
|
||||
display_width_px: tool.args.displayWidthPx as number,
|
||||
display_height_px: tool.args.displayHeightPx as number,
|
||||
display_number: tool.args.displayNumber as number,
|
||||
enable_zoom: tool.args.enableZoom as boolean,
|
||||
cache_control: undefined,
|
||||
});
|
||||
break;
|
||||
}
|
||||
case 'anthropic.computer_20241022': {
|
||||
betas.add('computer-use-2024-10-22');
|
||||
anthropicTools.push({
|
||||
name: 'computer',
|
||||
type: 'computer_20241022',
|
||||
display_width_px: tool.args.displayWidthPx as number,
|
||||
display_height_px: tool.args.displayHeightPx as number,
|
||||
display_number: tool.args.displayNumber as number,
|
||||
cache_control: undefined,
|
||||
});
|
||||
break;
|
||||
}
|
||||
case 'anthropic.text_editor_20250124': {
|
||||
betas.add('computer-use-2025-01-24');
|
||||
anthropicTools.push({
|
||||
name: 'str_replace_editor',
|
||||
type: 'text_editor_20250124',
|
||||
cache_control: undefined,
|
||||
});
|
||||
break;
|
||||
}
|
||||
case 'anthropic.text_editor_20241022': {
|
||||
betas.add('computer-use-2024-10-22');
|
||||
anthropicTools.push({
|
||||
name: 'str_replace_editor',
|
||||
type: 'text_editor_20241022',
|
||||
cache_control: undefined,
|
||||
});
|
||||
break;
|
||||
}
|
||||
case 'anthropic.text_editor_20250429': {
|
||||
betas.add('computer-use-2025-01-24');
|
||||
anthropicTools.push({
|
||||
name: 'str_replace_based_edit_tool',
|
||||
type: 'text_editor_20250429',
|
||||
cache_control: undefined,
|
||||
});
|
||||
break;
|
||||
}
|
||||
case 'anthropic.text_editor_20250728': {
|
||||
const args = await validateTypes({
|
||||
value: tool.args,
|
||||
schema: textEditor_20250728ArgsSchema,
|
||||
});
|
||||
anthropicTools.push({
|
||||
name: 'str_replace_based_edit_tool',
|
||||
type: 'text_editor_20250728',
|
||||
max_characters: args.maxCharacters,
|
||||
cache_control: undefined,
|
||||
});
|
||||
break;
|
||||
}
|
||||
case 'anthropic.bash_20250124': {
|
||||
betas.add('computer-use-2025-01-24');
|
||||
anthropicTools.push({
|
||||
name: 'bash',
|
||||
type: 'bash_20250124',
|
||||
cache_control: undefined,
|
||||
});
|
||||
break;
|
||||
}
|
||||
case 'anthropic.bash_20241022': {
|
||||
betas.add('computer-use-2024-10-22');
|
||||
anthropicTools.push({
|
||||
name: 'bash',
|
||||
type: 'bash_20241022',
|
||||
cache_control: undefined,
|
||||
});
|
||||
break;
|
||||
}
|
||||
case 'anthropic.memory_20250818': {
|
||||
betas.add('context-management-2025-06-27');
|
||||
anthropicTools.push({
|
||||
name: 'memory',
|
||||
type: 'memory_20250818',
|
||||
});
|
||||
break;
|
||||
}
|
||||
case 'anthropic.web_fetch_20250910': {
|
||||
betas.add('web-fetch-2025-09-10');
|
||||
const args = await validateTypes({
|
||||
value: tool.args,
|
||||
schema: webFetch_20250910ArgsSchema,
|
||||
});
|
||||
anthropicTools.push({
|
||||
type: 'web_fetch_20250910',
|
||||
name: 'web_fetch',
|
||||
max_uses: args.maxUses,
|
||||
allowed_domains: args.allowedDomains,
|
||||
blocked_domains: args.blockedDomains,
|
||||
citations: args.citations,
|
||||
max_content_tokens: args.maxContentTokens,
|
||||
cache_control: undefined,
|
||||
});
|
||||
break;
|
||||
}
|
||||
case 'anthropic.web_fetch_20260209': {
|
||||
betas.add('code-execution-web-tools-2026-02-09');
|
||||
const args = await validateTypes({
|
||||
value: tool.args,
|
||||
schema: webFetch_20260209ArgsSchema,
|
||||
});
|
||||
anthropicTools.push({
|
||||
type: 'web_fetch_20260209',
|
||||
name: 'web_fetch',
|
||||
max_uses: args.maxUses,
|
||||
allowed_domains: args.allowedDomains,
|
||||
blocked_domains: args.blockedDomains,
|
||||
citations: args.citations,
|
||||
max_content_tokens: args.maxContentTokens,
|
||||
cache_control: undefined,
|
||||
});
|
||||
break;
|
||||
}
|
||||
case 'anthropic.web_search_20250305': {
|
||||
const args = await validateTypes({
|
||||
value: tool.args,
|
||||
schema: webSearch_20250305ArgsSchema,
|
||||
});
|
||||
anthropicTools.push({
|
||||
type: 'web_search_20250305',
|
||||
name: 'web_search',
|
||||
max_uses: args.maxUses,
|
||||
allowed_domains: args.allowedDomains,
|
||||
blocked_domains: args.blockedDomains,
|
||||
user_location: args.userLocation,
|
||||
cache_control: undefined,
|
||||
});
|
||||
break;
|
||||
}
|
||||
case 'anthropic.web_search_20260209': {
|
||||
betas.add('code-execution-web-tools-2026-02-09');
|
||||
const args = await validateTypes({
|
||||
value: tool.args,
|
||||
schema: webSearch_20260209ArgsSchema,
|
||||
});
|
||||
anthropicTools.push({
|
||||
type: 'web_search_20260209',
|
||||
name: 'web_search',
|
||||
max_uses: args.maxUses,
|
||||
allowed_domains: args.allowedDomains,
|
||||
blocked_domains: args.blockedDomains,
|
||||
user_location: args.userLocation,
|
||||
cache_control: undefined,
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
||||
case 'anthropic.tool_search_regex_20251119': {
|
||||
anthropicTools.push({
|
||||
type: 'tool_search_tool_regex_20251119',
|
||||
name: 'tool_search_tool_regex',
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
||||
case 'anthropic.tool_search_bm25_20251119': {
|
||||
anthropicTools.push({
|
||||
type: 'tool_search_tool_bm25_20251119',
|
||||
name: 'tool_search_tool_bm25',
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
||||
default: {
|
||||
toolWarnings.push({
|
||||
type: 'unsupported',
|
||||
feature: `provider-defined tool ${tool.id}`,
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default: {
|
||||
toolWarnings.push({
|
||||
type: 'unsupported',
|
||||
feature: `tool ${tool}`,
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (toolChoice == null) {
|
||||
return {
|
||||
tools: anthropicTools,
|
||||
toolChoice: disableParallelToolUse
|
||||
? { type: 'auto', disable_parallel_tool_use: disableParallelToolUse }
|
||||
: undefined,
|
||||
toolWarnings,
|
||||
betas,
|
||||
};
|
||||
}
|
||||
|
||||
const type = toolChoice.type;
|
||||
|
||||
switch (type) {
|
||||
case 'auto':
|
||||
return {
|
||||
tools: anthropicTools,
|
||||
toolChoice: {
|
||||
type: 'auto',
|
||||
disable_parallel_tool_use: disableParallelToolUse,
|
||||
},
|
||||
toolWarnings,
|
||||
betas,
|
||||
};
|
||||
case 'required':
|
||||
return {
|
||||
tools: anthropicTools,
|
||||
toolChoice: {
|
||||
type: 'any',
|
||||
disable_parallel_tool_use: disableParallelToolUse,
|
||||
},
|
||||
toolWarnings,
|
||||
betas,
|
||||
};
|
||||
case 'none':
|
||||
// Anthropic does not support 'none' tool choice, so we remove the tools:
|
||||
return { tools: undefined, toolChoice: undefined, toolWarnings, betas };
|
||||
case 'tool':
|
||||
return {
|
||||
tools: anthropicTools,
|
||||
toolChoice: {
|
||||
type: 'tool',
|
||||
name: toolChoice.toolName,
|
||||
disable_parallel_tool_use: disableParallelToolUse,
|
||||
},
|
||||
toolWarnings,
|
||||
betas,
|
||||
};
|
||||
default: {
|
||||
const _exhaustiveCheck: never = type;
|
||||
throw new UnsupportedFunctionalityError({
|
||||
functionality: `tool choice type: ${_exhaustiveCheck}`,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
177
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/anthropic-provider.ts
generated
vendored
Normal file
177
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/anthropic-provider.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,177 @@
|
|||
import {
|
||||
InvalidArgumentError,
|
||||
LanguageModelV3,
|
||||
NoSuchModelError,
|
||||
ProviderV3,
|
||||
} from '@ai-sdk/provider';
|
||||
import {
|
||||
FetchFunction,
|
||||
generateId,
|
||||
loadApiKey,
|
||||
loadOptionalSetting,
|
||||
withoutTrailingSlash,
|
||||
withUserAgentSuffix,
|
||||
} from '@ai-sdk/provider-utils';
|
||||
import { VERSION } from './version';
|
||||
import { AnthropicMessagesLanguageModel } from './anthropic-messages-language-model';
|
||||
import { AnthropicMessagesModelId } from './anthropic-messages-options';
|
||||
import { anthropicTools } from './anthropic-tools';
|
||||
|
||||
export interface AnthropicProvider extends ProviderV3 {
|
||||
/**
|
||||
* Creates a model for text generation.
|
||||
*/
|
||||
(modelId: AnthropicMessagesModelId): LanguageModelV3;
|
||||
|
||||
/**
|
||||
* Creates a model for text generation.
|
||||
*/
|
||||
languageModel(modelId: AnthropicMessagesModelId): LanguageModelV3;
|
||||
|
||||
chat(modelId: AnthropicMessagesModelId): LanguageModelV3;
|
||||
|
||||
messages(modelId: AnthropicMessagesModelId): LanguageModelV3;
|
||||
|
||||
/**
|
||||
* @deprecated Use `embeddingModel` instead.
|
||||
*/
|
||||
textEmbeddingModel(modelId: string): never;
|
||||
|
||||
/**
|
||||
* Anthropic-specific computer use tool.
|
||||
*/
|
||||
tools: typeof anthropicTools;
|
||||
}
|
||||
|
||||
export interface AnthropicProviderSettings {
|
||||
/**
|
||||
* Use a different URL prefix for API calls, e.g. to use proxy servers.
|
||||
* The default prefix is `https://api.anthropic.com/v1`.
|
||||
*/
|
||||
baseURL?: string;
|
||||
|
||||
/**
|
||||
* API key that is being send using the `x-api-key` header.
|
||||
* It defaults to the `ANTHROPIC_API_KEY` environment variable.
|
||||
* Only one of `apiKey` or `authToken` is required.
|
||||
*/
|
||||
apiKey?: string;
|
||||
|
||||
/**
|
||||
* Auth token that is being sent using the `Authorization: Bearer` header.
|
||||
* It defaults to the `ANTHROPIC_AUTH_TOKEN` environment variable.
|
||||
* Only one of `apiKey` or `authToken` is required.
|
||||
*/
|
||||
authToken?: string;
|
||||
|
||||
/**
|
||||
* Custom headers to include in the requests.
|
||||
*/
|
||||
headers?: Record<string, string>;
|
||||
|
||||
/**
|
||||
* Custom fetch implementation. You can use it as a middleware to intercept requests,
|
||||
* or to provide a custom fetch implementation for e.g. testing.
|
||||
*/
|
||||
fetch?: FetchFunction;
|
||||
|
||||
generateId?: () => string;
|
||||
|
||||
/**
|
||||
* Custom provider name
|
||||
* Defaults to 'anthropic.messages'.
|
||||
*/
|
||||
name?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an Anthropic provider instance.
|
||||
*/
|
||||
export function createAnthropic(
|
||||
options: AnthropicProviderSettings = {},
|
||||
): AnthropicProvider {
|
||||
const baseURL =
|
||||
withoutTrailingSlash(
|
||||
loadOptionalSetting({
|
||||
settingValue: options.baseURL,
|
||||
environmentVariableName: 'ANTHROPIC_BASE_URL',
|
||||
}),
|
||||
) ?? 'https://api.anthropic.com/v1';
|
||||
|
||||
const providerName = options.name ?? 'anthropic.messages';
|
||||
|
||||
// Only error if both are explicitly provided in options
|
||||
if (options.apiKey && options.authToken) {
|
||||
throw new InvalidArgumentError({
|
||||
argument: 'apiKey/authToken',
|
||||
message:
|
||||
'Both apiKey and authToken were provided. Please use only one authentication method.',
|
||||
});
|
||||
}
|
||||
|
||||
const getHeaders = () => {
|
||||
const authHeaders: Record<string, string> = options.authToken
|
||||
? { Authorization: `Bearer ${options.authToken}` }
|
||||
: {
|
||||
'x-api-key': loadApiKey({
|
||||
apiKey: options.apiKey,
|
||||
environmentVariableName: 'ANTHROPIC_API_KEY',
|
||||
description: 'Anthropic',
|
||||
}),
|
||||
};
|
||||
|
||||
return withUserAgentSuffix(
|
||||
{
|
||||
'anthropic-version': '2023-06-01',
|
||||
...authHeaders,
|
||||
...options.headers,
|
||||
},
|
||||
`ai-sdk/anthropic/${VERSION}`,
|
||||
);
|
||||
};
|
||||
|
||||
const createChatModel = (modelId: AnthropicMessagesModelId) =>
|
||||
new AnthropicMessagesLanguageModel(modelId, {
|
||||
provider: providerName,
|
||||
baseURL,
|
||||
headers: getHeaders,
|
||||
fetch: options.fetch,
|
||||
generateId: options.generateId ?? generateId,
|
||||
supportedUrls: () => ({
|
||||
'image/*': [/^https?:\/\/.*$/],
|
||||
'application/pdf': [/^https?:\/\/.*$/],
|
||||
}),
|
||||
});
|
||||
|
||||
const provider = function (modelId: AnthropicMessagesModelId) {
|
||||
if (new.target) {
|
||||
throw new Error(
|
||||
'The Anthropic model function cannot be called with the new keyword.',
|
||||
);
|
||||
}
|
||||
|
||||
return createChatModel(modelId);
|
||||
};
|
||||
|
||||
provider.specificationVersion = 'v3' as const;
|
||||
provider.languageModel = createChatModel;
|
||||
provider.chat = createChatModel;
|
||||
provider.messages = createChatModel;
|
||||
|
||||
provider.embeddingModel = (modelId: string) => {
|
||||
throw new NoSuchModelError({ modelId, modelType: 'embeddingModel' });
|
||||
};
|
||||
provider.textEmbeddingModel = provider.embeddingModel;
|
||||
provider.imageModel = (modelId: string) => {
|
||||
throw new NoSuchModelError({ modelId, modelType: 'imageModel' });
|
||||
};
|
||||
|
||||
provider.tools = anthropicTools;
|
||||
|
||||
return provider;
|
||||
}
|
||||
|
||||
/**
|
||||
* Default Anthropic provider instance.
|
||||
*/
|
||||
export const anthropic = createAnthropic();
|
||||
238
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/anthropic-tools.ts
generated
vendored
Normal file
238
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/anthropic-tools.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,238 @@
|
|||
import { bash_20241022 } from './tool/bash_20241022';
|
||||
import { bash_20250124 } from './tool/bash_20250124';
|
||||
import { codeExecution_20250522 } from './tool/code-execution_20250522';
|
||||
import { codeExecution_20250825 } from './tool/code-execution_20250825';
|
||||
import { codeExecution_20260120 } from './tool/code-execution_20260120';
|
||||
import { computer_20241022 } from './tool/computer_20241022';
|
||||
import { computer_20250124 } from './tool/computer_20250124';
|
||||
import { computer_20251124 } from './tool/computer_20251124';
|
||||
import { memory_20250818 } from './tool/memory_20250818';
|
||||
import { textEditor_20241022 } from './tool/text-editor_20241022';
|
||||
import { textEditor_20250124 } from './tool/text-editor_20250124';
|
||||
import { textEditor_20250429 } from './tool/text-editor_20250429';
|
||||
import { textEditor_20250728 } from './tool/text-editor_20250728';
|
||||
import { toolSearchBm25_20251119 } from './tool/tool-search-bm25_20251119';
|
||||
import { toolSearchRegex_20251119 } from './tool/tool-search-regex_20251119';
|
||||
import { webFetch_20260209 } from './tool/web-fetch-20260209';
|
||||
import { webFetch_20250910 } from './tool/web-fetch-20250910';
|
||||
import { webSearch_20260209 } from './tool/web-search_20260209';
|
||||
import { webSearch_20250305 } from './tool/web-search_20250305';
|
||||
|
||||
export const anthropicTools = {
|
||||
/**
|
||||
* The bash tool enables Claude to execute shell commands in a persistent bash session,
|
||||
* allowing system operations, script execution, and command-line automation.
|
||||
*
|
||||
* Image results are supported.
|
||||
*/
|
||||
bash_20241022,
|
||||
|
||||
/**
|
||||
* The bash tool enables Claude to execute shell commands in a persistent bash session,
|
||||
* allowing system operations, script execution, and command-line automation.
|
||||
*
|
||||
* Image results are supported.
|
||||
*/
|
||||
bash_20250124,
|
||||
|
||||
/**
|
||||
* Claude can analyze data, create visualizations, perform complex calculations,
|
||||
* run system commands, create and edit files, and process uploaded files directly within
|
||||
* the API conversation.
|
||||
*
|
||||
* The code execution tool allows Claude to run Bash commands and manipulate files,
|
||||
* including writing code, in a secure, sandboxed environment.
|
||||
*/
|
||||
codeExecution_20250522,
|
||||
|
||||
/**
|
||||
* Claude can analyze data, create visualizations, perform complex calculations,
|
||||
* run system commands, create and edit files, and process uploaded files directly within
|
||||
* the API conversation.
|
||||
*
|
||||
* The code execution tool allows Claude to run both Python and Bash commands and manipulate files,
|
||||
* including writing code, in a secure, sandboxed environment.
|
||||
*
|
||||
* This is the latest version with enhanced Bash support and file operations.
|
||||
*/
|
||||
codeExecution_20250825,
|
||||
|
||||
/**
|
||||
* Claude can analyze data, create visualizations, perform complex calculations,
|
||||
* run system commands, create and edit files, and process uploaded files directly within
|
||||
* the API conversation.
|
||||
*
|
||||
* The code execution tool allows Claude to run both Python and Bash commands and manipulate files,
|
||||
* including writing code, in a secure, sandboxed environment.
|
||||
*
|
||||
* This is the recommended version. Does not require a beta header.
|
||||
*
|
||||
* Supported models: Claude Opus 4.6, Sonnet 4.6, Sonnet 4.5, Opus 4.5
|
||||
*/
|
||||
codeExecution_20260120,
|
||||
|
||||
/**
|
||||
* Claude can interact with computer environments through the computer use tool, which
|
||||
* provides screenshot capabilities and mouse/keyboard control for autonomous desktop interaction.
|
||||
*
|
||||
* Image results are supported.
|
||||
*
|
||||
* @param displayWidthPx - The width of the display being controlled by the model in pixels.
|
||||
* @param displayHeightPx - The height of the display being controlled by the model in pixels.
|
||||
* @param displayNumber - The display number to control (only relevant for X11 environments). If specified, the tool will be provided a display number in the tool definition.
|
||||
*/
|
||||
computer_20241022,
|
||||
|
||||
/**
|
||||
* Claude can interact with computer environments through the computer use tool, which
|
||||
* provides screenshot capabilities and mouse/keyboard control for autonomous desktop interaction.
|
||||
*
|
||||
* Image results are supported.
|
||||
*
|
||||
* @param displayWidthPx - The width of the display being controlled by the model in pixels.
|
||||
* @param displayHeightPx - The height of the display being controlled by the model in pixels.
|
||||
* @param displayNumber - The display number to control (only relevant for X11 environments). If specified, the tool will be provided a display number in the tool definition.
|
||||
*/
|
||||
computer_20250124,
|
||||
|
||||
/**
|
||||
* Claude can interact with computer environments through the computer use tool, which
|
||||
* provides screenshot capabilities and mouse/keyboard control for autonomous desktop interaction.
|
||||
*
|
||||
* This version adds the zoom action for detailed screen region inspection.
|
||||
*
|
||||
* Image results are supported.
|
||||
*
|
||||
* Supported models: Claude Opus 4.5
|
||||
*
|
||||
* @param displayWidthPx - The width of the display being controlled by the model in pixels.
|
||||
* @param displayHeightPx - The height of the display being controlled by the model in pixels.
|
||||
* @param displayNumber - The display number to control (only relevant for X11 environments). If specified, the tool will be provided a display number in the tool definition.
|
||||
* @param enableZoom - Enable zoom action. Set to true to allow Claude to zoom into specific screen regions. Default: false.
|
||||
*/
|
||||
computer_20251124,
|
||||
|
||||
/**
|
||||
* The memory tool enables Claude to store and retrieve information across conversations through a memory file directory.
|
||||
* Claude can create, read, update, and delete files that persist between sessions,
|
||||
* allowing it to build knowledge over time without keeping everything in the context window.
|
||||
* The memory tool operates client-side—you control where and how the data is stored through your own infrastructure.
|
||||
*
|
||||
* Supported models: Claude Sonnet 4.5, Claude Sonnet 4, Claude Opus 4.1, Claude Opus 4.
|
||||
*/
|
||||
memory_20250818,
|
||||
|
||||
/**
|
||||
* Claude can use an Anthropic-defined text editor tool to view and modify text files,
|
||||
* helping you debug, fix, and improve your code or other text documents. This allows Claude
|
||||
* to directly interact with your files, providing hands-on assistance rather than just suggesting changes.
|
||||
*
|
||||
* Supported models: Claude Sonnet 3.5
|
||||
*/
|
||||
textEditor_20241022,
|
||||
|
||||
/**
|
||||
* Claude can use an Anthropic-defined text editor tool to view and modify text files,
|
||||
* helping you debug, fix, and improve your code or other text documents. This allows Claude
|
||||
* to directly interact with your files, providing hands-on assistance rather than just suggesting changes.
|
||||
*
|
||||
* Supported models: Claude Sonnet 3.7
|
||||
*/
|
||||
textEditor_20250124,
|
||||
|
||||
/**
|
||||
* Claude can use an Anthropic-defined text editor tool to view and modify text files,
|
||||
* helping you debug, fix, and improve your code or other text documents. This allows Claude
|
||||
* to directly interact with your files, providing hands-on assistance rather than just suggesting changes.
|
||||
*
|
||||
* Note: This version does not support the "undo_edit" command.
|
||||
*
|
||||
* @deprecated Use textEditor_20250728 instead
|
||||
*/
|
||||
textEditor_20250429,
|
||||
|
||||
/**
|
||||
* Claude can use an Anthropic-defined text editor tool to view and modify text files,
|
||||
* helping you debug, fix, and improve your code or other text documents. This allows Claude
|
||||
* to directly interact with your files, providing hands-on assistance rather than just suggesting changes.
|
||||
*
|
||||
* Note: This version does not support the "undo_edit" command and adds optional max_characters parameter.
|
||||
*
|
||||
* Supported models: Claude Sonnet 4, Opus 4, and Opus 4.1
|
||||
*
|
||||
* @param maxCharacters - Optional maximum number of characters to view in the file
|
||||
*/
|
||||
textEditor_20250728,
|
||||
|
||||
/**
|
||||
* Creates a web fetch tool that gives Claude direct access to real-time web content.
|
||||
*
|
||||
* @param maxUses - The max_uses parameter limits the number of web fetches performed
|
||||
* @param allowedDomains - Only fetch from these domains
|
||||
* @param blockedDomains - Never fetch from these domains
|
||||
* @param citations - Unlike web search where citations are always enabled, citations are optional for web fetch. Set "citations": {"enabled": true} to enable Claude to cite specific passages from fetched documents.
|
||||
* @param maxContentTokens - The max_content_tokens parameter limits the amount of content that will be included in the context.
|
||||
*/
|
||||
webFetch_20250910,
|
||||
|
||||
/**
|
||||
* Creates a web fetch tool that gives Claude direct access to real-time web content.
|
||||
*
|
||||
* @param maxUses - The max_uses parameter limits the number of web fetches performed
|
||||
* @param allowedDomains - Only fetch from these domains
|
||||
* @param blockedDomains - Never fetch from these domains
|
||||
* @param citations - Unlike web search where citations are always enabled, citations are optional for web fetch. Set "citations": {"enabled": true} to enable Claude to cite specific passages from fetched documents.
|
||||
* @param maxContentTokens - The max_content_tokens parameter limits the amount of content that will be included in the context.
|
||||
*/
|
||||
webFetch_20260209,
|
||||
|
||||
/**
|
||||
* Creates a web search tool that gives Claude direct access to real-time web content.
|
||||
*
|
||||
* @param maxUses - Maximum number of web searches Claude can perform during the conversation.
|
||||
* @param allowedDomains - Optional list of domains that Claude is allowed to search.
|
||||
* @param blockedDomains - Optional list of domains that Claude should avoid when searching.
|
||||
* @param userLocation - Optional user location information to provide geographically relevant search results.
|
||||
*/
|
||||
webSearch_20250305,
|
||||
|
||||
/**
|
||||
* Creates a web search tool that gives Claude direct access to real-time web content.
|
||||
*
|
||||
* @param maxUses - Maximum number of web searches Claude can perform during the conversation.
|
||||
* @param allowedDomains - Optional list of domains that Claude is allowed to search.
|
||||
* @param blockedDomains - Optional list of domains that Claude should avoid when searching.
|
||||
* @param userLocation - Optional user location information to provide geographically relevant search results.
|
||||
*/
|
||||
webSearch_20260209,
|
||||
|
||||
/**
|
||||
* Creates a tool search tool that uses regex patterns to find tools.
|
||||
*
|
||||
* The tool search tool enables Claude to work with hundreds or thousands of tools
|
||||
* by dynamically discovering and loading them on-demand. Instead of loading all
|
||||
* tool definitions into the context window upfront, Claude searches your tool
|
||||
* catalog and loads only the tools it needs.
|
||||
*
|
||||
* Use `providerOptions: { anthropic: { deferLoading: true } }` on other tools
|
||||
* to mark them for deferred loading.
|
||||
*
|
||||
* Supported models: Claude Opus 4.5, Claude Sonnet 4.5
|
||||
*/
|
||||
toolSearchRegex_20251119,
|
||||
|
||||
/**
|
||||
* Creates a tool search tool that uses BM25 (natural language) to find tools.
|
||||
*
|
||||
* The tool search tool enables Claude to work with hundreds or thousands of tools
|
||||
* by dynamically discovering and loading them on-demand. Instead of loading all
|
||||
* tool definitions into the context window upfront, Claude searches your tool
|
||||
* catalog and loads only the tools it needs.
|
||||
*
|
||||
* Use `providerOptions: { anthropic: { deferLoading: true } }` on other tools
|
||||
* to mark them for deferred loading.
|
||||
*
|
||||
* Supported models: Claude Opus 4.5, Claude Sonnet 4.5
|
||||
*/
|
||||
toolSearchBm25_20251119,
|
||||
};
|
||||
73
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/convert-anthropic-messages-usage.ts
generated
vendored
Normal file
73
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/convert-anthropic-messages-usage.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
import { JSONObject, LanguageModelV3Usage } from '@ai-sdk/provider';
|
||||
|
||||
/**
|
||||
* Represents a single iteration in the usage breakdown.
|
||||
* When compaction occurs, the API returns an iterations array showing
|
||||
* usage for each sampling iteration (compaction + message).
|
||||
*/
|
||||
export type AnthropicUsageIteration = {
|
||||
type: 'compaction' | 'message';
|
||||
input_tokens: number;
|
||||
output_tokens: number;
|
||||
};
|
||||
|
||||
export type AnthropicMessagesUsage = {
|
||||
input_tokens: number;
|
||||
output_tokens: number;
|
||||
cache_creation_input_tokens?: number | null;
|
||||
cache_read_input_tokens?: number | null;
|
||||
/**
|
||||
* When compaction is triggered, this array contains usage for each
|
||||
* sampling iteration. The top-level input_tokens and output_tokens
|
||||
* do NOT include compaction iteration usage - to get total tokens
|
||||
* consumed and billed, sum across all entries in this array.
|
||||
*/
|
||||
iterations?: AnthropicUsageIteration[] | null;
|
||||
};
|
||||
|
||||
export function convertAnthropicMessagesUsage({
|
||||
usage,
|
||||
rawUsage,
|
||||
}: {
|
||||
usage: AnthropicMessagesUsage;
|
||||
rawUsage?: JSONObject;
|
||||
}): LanguageModelV3Usage {
|
||||
const cacheCreationTokens = usage.cache_creation_input_tokens ?? 0;
|
||||
const cacheReadTokens = usage.cache_read_input_tokens ?? 0;
|
||||
|
||||
// When iterations is present (compaction occurred), sum across all iterations
|
||||
// to get the true total tokens consumed/billed. The top-level input_tokens
|
||||
// and output_tokens exclude compaction iteration usage.
|
||||
let inputTokens: number;
|
||||
let outputTokens: number;
|
||||
|
||||
if (usage.iterations && usage.iterations.length > 0) {
|
||||
const totals = usage.iterations.reduce(
|
||||
(acc, iter) => ({
|
||||
input: acc.input + iter.input_tokens,
|
||||
output: acc.output + iter.output_tokens,
|
||||
}),
|
||||
{ input: 0, output: 0 },
|
||||
);
|
||||
inputTokens = totals.input;
|
||||
outputTokens = totals.output;
|
||||
} else {
|
||||
inputTokens = usage.input_tokens;
|
||||
outputTokens = usage.output_tokens;
|
||||
}
|
||||
|
||||
return {
|
||||
inputTokens: {
|
||||
total: inputTokens + cacheCreationTokens + cacheReadTokens,
|
||||
noCache: inputTokens,
|
||||
cacheRead: cacheReadTokens,
|
||||
cacheWrite: cacheCreationTokens,
|
||||
},
|
||||
outputTokens: {
|
||||
total: outputTokens,
|
||||
text: undefined,
|
||||
reasoning: undefined,
|
||||
},
|
||||
raw: rawUsage ?? usage,
|
||||
};
|
||||
}
|
||||
1119
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/convert-to-anthropic-messages-prompt.ts
generated
vendored
Normal file
1119
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/convert-to-anthropic-messages-prompt.ts
generated
vendored
Normal file
File diff suppressed because it is too large
Load diff
38
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/forward-anthropic-container-id-from-last-step.ts
generated
vendored
Normal file
38
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/forward-anthropic-container-id-from-last-step.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
import { JSONObject } from '@ai-sdk/provider';
|
||||
import { AnthropicMessageMetadata } from './anthropic-message-metadata';
|
||||
|
||||
/**
|
||||
* Sets the Anthropic container ID in the provider options based on
|
||||
* any previous step's provider metadata.
|
||||
*
|
||||
* Searches backwards through steps to find the most recent container ID.
|
||||
* You can use this function in `prepareStep` to forward the container ID between steps.
|
||||
*/
|
||||
export function forwardAnthropicContainerIdFromLastStep({
|
||||
steps,
|
||||
}: {
|
||||
steps: Array<{
|
||||
providerMetadata?: Record<string, JSONObject>;
|
||||
}>;
|
||||
}): undefined | { providerOptions?: Record<string, JSONObject> } {
|
||||
// Search backwards through steps to find the most recent container ID
|
||||
for (let i = steps.length - 1; i >= 0; i--) {
|
||||
const containerId = (
|
||||
steps[i].providerMetadata?.anthropic as
|
||||
| AnthropicMessageMetadata
|
||||
| undefined
|
||||
)?.container?.id;
|
||||
|
||||
if (containerId) {
|
||||
return {
|
||||
providerOptions: {
|
||||
anthropic: {
|
||||
container: { id: containerId },
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
63
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/get-cache-control.ts
generated
vendored
Normal file
63
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/get-cache-control.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
import { SharedV3Warning, SharedV3ProviderMetadata } from '@ai-sdk/provider';
|
||||
import { AnthropicCacheControl } from './anthropic-messages-api';
|
||||
|
||||
// Anthropic allows a maximum of 4 cache breakpoints per request
|
||||
const MAX_CACHE_BREAKPOINTS = 4;
|
||||
|
||||
// Helper function to extract cache_control from provider metadata
|
||||
// Allows both cacheControl and cache_control for flexibility
|
||||
function getCacheControl(
|
||||
providerMetadata: SharedV3ProviderMetadata | undefined,
|
||||
): AnthropicCacheControl | undefined {
|
||||
const anthropic = providerMetadata?.anthropic;
|
||||
|
||||
// allow both cacheControl and cache_control:
|
||||
const cacheControlValue = anthropic?.cacheControl ?? anthropic?.cache_control;
|
||||
|
||||
// Pass through value assuming it is of the correct type.
|
||||
// The Anthropic API will validate the value.
|
||||
return cacheControlValue as AnthropicCacheControl | undefined;
|
||||
}
|
||||
|
||||
export class CacheControlValidator {
|
||||
private breakpointCount = 0;
|
||||
private warnings: SharedV3Warning[] = [];
|
||||
|
||||
getCacheControl(
|
||||
providerMetadata: SharedV3ProviderMetadata | undefined,
|
||||
context: { type: string; canCache: boolean },
|
||||
): AnthropicCacheControl | undefined {
|
||||
const cacheControlValue = getCacheControl(providerMetadata);
|
||||
|
||||
if (!cacheControlValue) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// Validate that cache_control is allowed in this context
|
||||
if (!context.canCache) {
|
||||
this.warnings.push({
|
||||
type: 'unsupported',
|
||||
feature: 'cache_control on non-cacheable context',
|
||||
details: `cache_control cannot be set on ${context.type}. It will be ignored.`,
|
||||
});
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// Validate cache breakpoint limit
|
||||
this.breakpointCount++;
|
||||
if (this.breakpointCount > MAX_CACHE_BREAKPOINTS) {
|
||||
this.warnings.push({
|
||||
type: 'unsupported',
|
||||
feature: 'cacheControl breakpoint limit',
|
||||
details: `Maximum ${MAX_CACHE_BREAKPOINTS} cache breakpoints exceeded (found ${this.breakpointCount}). This breakpoint will be ignored.`,
|
||||
});
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return cacheControlValue;
|
||||
}
|
||||
|
||||
getWarnings(): SharedV3Warning[] {
|
||||
return this.warnings;
|
||||
}
|
||||
}
|
||||
17
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/index.ts
generated
vendored
Normal file
17
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/index.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
export type {
|
||||
AnthropicMessageMetadata,
|
||||
AnthropicUsageIteration,
|
||||
} from './anthropic-message-metadata';
|
||||
export type {
|
||||
AnthropicLanguageModelOptions,
|
||||
/** @deprecated Use `AnthropicLanguageModelOptions` instead. */
|
||||
AnthropicLanguageModelOptions as AnthropicProviderOptions,
|
||||
} from './anthropic-messages-options';
|
||||
export type { AnthropicToolOptions } from './anthropic-prepare-tools';
|
||||
export { anthropic, createAnthropic } from './anthropic-provider';
|
||||
export type {
|
||||
AnthropicProvider,
|
||||
AnthropicProviderSettings,
|
||||
} from './anthropic-provider';
|
||||
export { forwardAnthropicContainerIdFromLastStep } from './forward-anthropic-container-id-from-last-step';
|
||||
export { VERSION } from './version';
|
||||
4
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/internal/index.ts
generated
vendored
Normal file
4
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/internal/index.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
export { AnthropicMessagesLanguageModel } from '../anthropic-messages-language-model';
|
||||
export { anthropicTools } from '../anthropic-tools';
|
||||
export type { AnthropicMessagesModelId } from '../anthropic-messages-options';
|
||||
export { prepareTools } from '../anthropic-prepare-tools';
|
||||
30
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/map-anthropic-stop-reason.ts
generated
vendored
Normal file
30
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/map-anthropic-stop-reason.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
import { LanguageModelV3FinishReason } from '@ai-sdk/provider';
|
||||
|
||||
/**
|
||||
* @see https://docs.anthropic.com/en/api/messages#response-stop-reason
|
||||
*/
|
||||
export function mapAnthropicStopReason({
|
||||
finishReason,
|
||||
isJsonResponseFromTool,
|
||||
}: {
|
||||
finishReason: string | null | undefined;
|
||||
isJsonResponseFromTool?: boolean;
|
||||
}): LanguageModelV3FinishReason['unified'] {
|
||||
switch (finishReason) {
|
||||
case 'pause_turn':
|
||||
case 'end_turn':
|
||||
case 'stop_sequence':
|
||||
return 'stop';
|
||||
case 'refusal':
|
||||
return 'content-filter';
|
||||
case 'tool_use':
|
||||
return isJsonResponseFromTool ? 'stop' : 'tool-calls';
|
||||
case 'max_tokens':
|
||||
case 'model_context_window_exceeded':
|
||||
return 'length';
|
||||
case 'compaction':
|
||||
return 'other';
|
||||
default:
|
||||
return 'other';
|
||||
}
|
||||
}
|
||||
33
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/tool/bash_20241022.ts
generated
vendored
Normal file
33
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/tool/bash_20241022.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import {
|
||||
createProviderToolFactory,
|
||||
lazySchema,
|
||||
zodSchema,
|
||||
} from '@ai-sdk/provider-utils';
|
||||
import { z } from 'zod/v4';
|
||||
|
||||
const bash_20241022InputSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.object({
|
||||
command: z.string(),
|
||||
restart: z.boolean().optional(),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
export const bash_20241022 = createProviderToolFactory<
|
||||
{
|
||||
/**
|
||||
* The bash command to run. Required unless the tool is being restarted.
|
||||
*/
|
||||
command: string;
|
||||
|
||||
/**
|
||||
* Specifying true will restart this tool. Otherwise, leave this unspecified.
|
||||
*/
|
||||
restart?: boolean;
|
||||
},
|
||||
{}
|
||||
>({
|
||||
id: 'anthropic.bash_20241022',
|
||||
inputSchema: bash_20241022InputSchema,
|
||||
});
|
||||
33
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/tool/bash_20250124.ts
generated
vendored
Normal file
33
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/tool/bash_20250124.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import {
|
||||
createProviderToolFactory,
|
||||
lazySchema,
|
||||
zodSchema,
|
||||
} from '@ai-sdk/provider-utils';
|
||||
import { z } from 'zod/v4';
|
||||
|
||||
const bash_20250124InputSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.object({
|
||||
command: z.string(),
|
||||
restart: z.boolean().optional(),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
export const bash_20250124 = createProviderToolFactory<
|
||||
{
|
||||
/**
|
||||
* The bash command to run. Required unless the tool is being restarted.
|
||||
*/
|
||||
command: string;
|
||||
|
||||
/**
|
||||
* Specifying true will restart this tool. Otherwise, leave this unspecified.
|
||||
*/
|
||||
restart?: boolean;
|
||||
},
|
||||
{}
|
||||
>({
|
||||
id: 'anthropic.bash_20250124',
|
||||
inputSchema: bash_20250124InputSchema,
|
||||
});
|
||||
61
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/tool/code-execution_20250522.ts
generated
vendored
Normal file
61
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/tool/code-execution_20250522.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
import {
|
||||
createProviderToolFactoryWithOutputSchema,
|
||||
lazySchema,
|
||||
zodSchema,
|
||||
} from '@ai-sdk/provider-utils';
|
||||
import { z } from 'zod/v4';
|
||||
|
||||
export const codeExecution_20250522OutputSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.object({
|
||||
type: z.literal('code_execution_result'),
|
||||
stdout: z.string(),
|
||||
stderr: z.string(),
|
||||
return_code: z.number(),
|
||||
content: z
|
||||
.array(
|
||||
z.object({
|
||||
type: z.literal('code_execution_output'),
|
||||
file_id: z.string(),
|
||||
}),
|
||||
)
|
||||
.optional()
|
||||
.default([]),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
const codeExecution_20250522InputSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.object({
|
||||
code: z.string(),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
const factory = createProviderToolFactoryWithOutputSchema<
|
||||
{
|
||||
/**
|
||||
* The Python code to execute.
|
||||
*/
|
||||
code: string;
|
||||
},
|
||||
{
|
||||
type: 'code_execution_result';
|
||||
stdout: string;
|
||||
stderr: string;
|
||||
return_code: number;
|
||||
content: Array<{ type: 'code_execution_output'; file_id: string }>;
|
||||
},
|
||||
{}
|
||||
>({
|
||||
id: 'anthropic.code_execution_20250522',
|
||||
inputSchema: codeExecution_20250522InputSchema,
|
||||
outputSchema: codeExecution_20250522OutputSchema,
|
||||
});
|
||||
|
||||
export const codeExecution_20250522 = (
|
||||
args: Parameters<typeof factory>[0] = {},
|
||||
) => {
|
||||
return factory(args);
|
||||
};
|
||||
281
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/tool/code-execution_20250825.ts
generated
vendored
Normal file
281
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/tool/code-execution_20250825.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,281 @@
|
|||
import {
|
||||
createProviderToolFactoryWithOutputSchema,
|
||||
lazySchema,
|
||||
zodSchema,
|
||||
} from '@ai-sdk/provider-utils';
|
||||
import { z } from 'zod/v4';
|
||||
|
||||
export const codeExecution_20250825OutputSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.discriminatedUnion('type', [
|
||||
z.object({
|
||||
type: z.literal('code_execution_result'),
|
||||
stdout: z.string(),
|
||||
stderr: z.string(),
|
||||
return_code: z.number(),
|
||||
content: z
|
||||
.array(
|
||||
z.object({
|
||||
type: z.literal('code_execution_output'),
|
||||
file_id: z.string(),
|
||||
}),
|
||||
)
|
||||
.optional()
|
||||
.default([]),
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('bash_code_execution_result'),
|
||||
content: z.array(
|
||||
z.object({
|
||||
type: z.literal('bash_code_execution_output'),
|
||||
file_id: z.string(),
|
||||
}),
|
||||
),
|
||||
stdout: z.string(),
|
||||
stderr: z.string(),
|
||||
return_code: z.number(),
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('bash_code_execution_tool_result_error'),
|
||||
error_code: z.string(),
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('text_editor_code_execution_tool_result_error'),
|
||||
error_code: z.string(),
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('text_editor_code_execution_view_result'),
|
||||
content: z.string(),
|
||||
file_type: z.string(),
|
||||
num_lines: z.number().nullable(),
|
||||
start_line: z.number().nullable(),
|
||||
total_lines: z.number().nullable(),
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('text_editor_code_execution_create_result'),
|
||||
is_file_update: z.boolean(),
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('text_editor_code_execution_str_replace_result'),
|
||||
lines: z.array(z.string()).nullable(),
|
||||
new_lines: z.number().nullable(),
|
||||
new_start: z.number().nullable(),
|
||||
old_lines: z.number().nullable(),
|
||||
old_start: z.number().nullable(),
|
||||
}),
|
||||
]),
|
||||
),
|
||||
);
|
||||
|
||||
export const codeExecution_20250825InputSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.discriminatedUnion('type', [
|
||||
// Programmatic tool calling format (mapped from { code } by AI SDK)
|
||||
z.object({
|
||||
type: z.literal('programmatic-tool-call'),
|
||||
code: z.string(),
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('bash_code_execution'),
|
||||
command: z.string(),
|
||||
}),
|
||||
z.discriminatedUnion('command', [
|
||||
z.object({
|
||||
type: z.literal('text_editor_code_execution'),
|
||||
command: z.literal('view'),
|
||||
path: z.string(),
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('text_editor_code_execution'),
|
||||
command: z.literal('create'),
|
||||
path: z.string(),
|
||||
file_text: z.string().nullish(),
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('text_editor_code_execution'),
|
||||
command: z.literal('str_replace'),
|
||||
path: z.string(),
|
||||
old_str: z.string(),
|
||||
new_str: z.string(),
|
||||
}),
|
||||
]),
|
||||
]),
|
||||
),
|
||||
);
|
||||
|
||||
const factory = createProviderToolFactoryWithOutputSchema<
|
||||
| {
|
||||
type: 'programmatic-tool-call';
|
||||
/**
|
||||
* Programmatic tool calling: Python code to execute when code_execution
|
||||
* is used with allowedCallers to trigger client-executed tools.
|
||||
*/
|
||||
code: string;
|
||||
}
|
||||
| {
|
||||
type: 'bash_code_execution';
|
||||
|
||||
/**
|
||||
* Shell command to execute.
|
||||
*/
|
||||
command: string;
|
||||
}
|
||||
| {
|
||||
type: 'text_editor_code_execution';
|
||||
command: 'view';
|
||||
|
||||
/**
|
||||
* The path to the file to view.
|
||||
*/
|
||||
path: string;
|
||||
}
|
||||
| {
|
||||
type: 'text_editor_code_execution';
|
||||
command: 'create';
|
||||
|
||||
/**
|
||||
* The path to the file to edit.
|
||||
*/
|
||||
path: string;
|
||||
|
||||
/**
|
||||
* The text of the file to edit.
|
||||
*/
|
||||
file_text?: string | null;
|
||||
}
|
||||
| {
|
||||
type: 'text_editor_code_execution';
|
||||
command: 'str_replace';
|
||||
|
||||
/**
|
||||
* The path to the file to edit.
|
||||
*/
|
||||
path: string;
|
||||
|
||||
/**
|
||||
* The string to replace.
|
||||
*/
|
||||
old_str: string;
|
||||
|
||||
/**
|
||||
* The new string to replace the old string with.
|
||||
*/
|
||||
new_str: string;
|
||||
},
|
||||
| {
|
||||
/**
|
||||
* Programmatic tool calling result: returned when code_execution runs code
|
||||
* that calls client-executed tools via allowedCallers.
|
||||
*/
|
||||
type: 'code_execution_result';
|
||||
|
||||
/**
|
||||
* Output from successful execution
|
||||
*/
|
||||
stdout: string;
|
||||
|
||||
/**
|
||||
* Error messages if execution fails
|
||||
*/
|
||||
stderr: string;
|
||||
|
||||
/**
|
||||
* 0 for success, non-zero for failure
|
||||
*/
|
||||
return_code: number;
|
||||
|
||||
/**
|
||||
* Output file Id list
|
||||
*/
|
||||
content: Array<{ type: 'code_execution_output'; file_id: string }>;
|
||||
}
|
||||
| {
|
||||
type: 'bash_code_execution_result';
|
||||
|
||||
/**
|
||||
* Output file Id list
|
||||
*/
|
||||
content: Array<{
|
||||
type: 'bash_code_execution_output';
|
||||
file_id: string;
|
||||
}>;
|
||||
|
||||
/**
|
||||
* Output from successful execution
|
||||
*/
|
||||
stdout: string;
|
||||
|
||||
/**
|
||||
* Error messages if execution fails
|
||||
*/
|
||||
stderr: string;
|
||||
|
||||
/**
|
||||
* 0 for success, non-zero for failure
|
||||
*/
|
||||
return_code: number;
|
||||
}
|
||||
| {
|
||||
type: 'bash_code_execution_tool_result_error';
|
||||
|
||||
/**
|
||||
* Available options: invalid_tool_input, unavailable, too_many_requests,
|
||||
* execution_time_exceeded, output_file_too_large.
|
||||
*/
|
||||
error_code: string;
|
||||
}
|
||||
| {
|
||||
type: 'text_editor_code_execution_tool_result_error';
|
||||
|
||||
/**
|
||||
* Available options: invalid_tool_input, unavailable, too_many_requests,
|
||||
* execution_time_exceeded, file_not_found.
|
||||
*/
|
||||
error_code: string;
|
||||
}
|
||||
| {
|
||||
type: 'text_editor_code_execution_view_result';
|
||||
|
||||
content: string;
|
||||
|
||||
/**
|
||||
* The type of the file. Available options: text, image, pdf.
|
||||
*/
|
||||
file_type: string;
|
||||
|
||||
num_lines: number | null;
|
||||
start_line: number | null;
|
||||
total_lines: number | null;
|
||||
}
|
||||
| {
|
||||
type: 'text_editor_code_execution_create_result';
|
||||
|
||||
is_file_update: boolean;
|
||||
}
|
||||
| {
|
||||
type: 'text_editor_code_execution_str_replace_result';
|
||||
|
||||
lines: string[] | null;
|
||||
new_lines: number | null;
|
||||
new_start: number | null;
|
||||
old_lines: number | null;
|
||||
old_start: number | null;
|
||||
},
|
||||
{
|
||||
// no arguments
|
||||
}
|
||||
>({
|
||||
id: 'anthropic.code_execution_20250825',
|
||||
inputSchema: codeExecution_20250825InputSchema,
|
||||
outputSchema: codeExecution_20250825OutputSchema,
|
||||
// Programmatic tool calling: tool results may be deferred to a later turn
|
||||
// when code execution triggers a client-executed tool that needs to be
|
||||
// resolved before the code execution result can be returned.
|
||||
supportsDeferredResults: true,
|
||||
});
|
||||
|
||||
export const codeExecution_20250825 = (
|
||||
args: Parameters<typeof factory>[0] = {},
|
||||
) => {
|
||||
return factory(args);
|
||||
};
|
||||
315
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/tool/code-execution_20260120.ts
generated
vendored
Normal file
315
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/tool/code-execution_20260120.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,315 @@
|
|||
import {
|
||||
createProviderToolFactoryWithOutputSchema,
|
||||
lazySchema,
|
||||
zodSchema,
|
||||
} from '@ai-sdk/provider-utils';
|
||||
import { z } from 'zod/v4';
|
||||
|
||||
export const codeExecution_20260120OutputSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.discriminatedUnion('type', [
|
||||
z.object({
|
||||
type: z.literal('code_execution_result'),
|
||||
stdout: z.string(),
|
||||
stderr: z.string(),
|
||||
return_code: z.number(),
|
||||
content: z
|
||||
.array(
|
||||
z.object({
|
||||
type: z.literal('code_execution_output'),
|
||||
file_id: z.string(),
|
||||
}),
|
||||
)
|
||||
.optional()
|
||||
.default([]),
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('encrypted_code_execution_result'),
|
||||
encrypted_stdout: z.string(),
|
||||
stderr: z.string(),
|
||||
return_code: z.number(),
|
||||
content: z
|
||||
.array(
|
||||
z.object({
|
||||
type: z.literal('code_execution_output'),
|
||||
file_id: z.string(),
|
||||
}),
|
||||
)
|
||||
.optional()
|
||||
.default([]),
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('bash_code_execution_result'),
|
||||
content: z.array(
|
||||
z.object({
|
||||
type: z.literal('bash_code_execution_output'),
|
||||
file_id: z.string(),
|
||||
}),
|
||||
),
|
||||
stdout: z.string(),
|
||||
stderr: z.string(),
|
||||
return_code: z.number(),
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('bash_code_execution_tool_result_error'),
|
||||
error_code: z.string(),
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('text_editor_code_execution_tool_result_error'),
|
||||
error_code: z.string(),
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('text_editor_code_execution_view_result'),
|
||||
content: z.string(),
|
||||
file_type: z.string(),
|
||||
num_lines: z.number().nullable(),
|
||||
start_line: z.number().nullable(),
|
||||
total_lines: z.number().nullable(),
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('text_editor_code_execution_create_result'),
|
||||
is_file_update: z.boolean(),
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('text_editor_code_execution_str_replace_result'),
|
||||
lines: z.array(z.string()).nullable(),
|
||||
new_lines: z.number().nullable(),
|
||||
new_start: z.number().nullable(),
|
||||
old_lines: z.number().nullable(),
|
||||
old_start: z.number().nullable(),
|
||||
}),
|
||||
]),
|
||||
),
|
||||
);
|
||||
|
||||
export const codeExecution_20260120InputSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.discriminatedUnion('type', [
|
||||
z.object({
|
||||
type: z.literal('programmatic-tool-call'),
|
||||
code: z.string(),
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('bash_code_execution'),
|
||||
command: z.string(),
|
||||
}),
|
||||
z.discriminatedUnion('command', [
|
||||
z.object({
|
||||
type: z.literal('text_editor_code_execution'),
|
||||
command: z.literal('view'),
|
||||
path: z.string(),
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('text_editor_code_execution'),
|
||||
command: z.literal('create'),
|
||||
path: z.string(),
|
||||
file_text: z.string().nullish(),
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('text_editor_code_execution'),
|
||||
command: z.literal('str_replace'),
|
||||
path: z.string(),
|
||||
old_str: z.string(),
|
||||
new_str: z.string(),
|
||||
}),
|
||||
]),
|
||||
]),
|
||||
),
|
||||
);
|
||||
|
||||
const factory = createProviderToolFactoryWithOutputSchema<
|
||||
| {
|
||||
type: 'programmatic-tool-call';
|
||||
/**
|
||||
* Programmatic tool calling: Python code to execute when code_execution
|
||||
* is used with allowedCallers to trigger client-executed tools.
|
||||
*/
|
||||
code: string;
|
||||
}
|
||||
| {
|
||||
type: 'bash_code_execution';
|
||||
|
||||
/**
|
||||
* Shell command to execute.
|
||||
*/
|
||||
command: string;
|
||||
}
|
||||
| {
|
||||
type: 'text_editor_code_execution';
|
||||
command: 'view';
|
||||
|
||||
/**
|
||||
* The path to the file to view.
|
||||
*/
|
||||
path: string;
|
||||
}
|
||||
| {
|
||||
type: 'text_editor_code_execution';
|
||||
command: 'create';
|
||||
|
||||
/**
|
||||
* The path to the file to edit.
|
||||
*/
|
||||
path: string;
|
||||
|
||||
/**
|
||||
* The text of the file to edit.
|
||||
*/
|
||||
file_text?: string | null;
|
||||
}
|
||||
| {
|
||||
type: 'text_editor_code_execution';
|
||||
command: 'str_replace';
|
||||
|
||||
/**
|
||||
* The path to the file to edit.
|
||||
*/
|
||||
path: string;
|
||||
|
||||
/**
|
||||
* The string to replace.
|
||||
*/
|
||||
old_str: string;
|
||||
|
||||
/**
|
||||
* The new string to replace the old string with.
|
||||
*/
|
||||
new_str: string;
|
||||
},
|
||||
| {
|
||||
/**
|
||||
* Programmatic tool calling result: returned when code_execution runs code
|
||||
* that calls client-executed tools via allowedCallers.
|
||||
*/
|
||||
type: 'code_execution_result';
|
||||
|
||||
/**
|
||||
* Output from successful execution
|
||||
*/
|
||||
stdout: string;
|
||||
|
||||
/**
|
||||
* Error messages if execution fails
|
||||
*/
|
||||
stderr: string;
|
||||
|
||||
/**
|
||||
* 0 for success, non-zero for failure
|
||||
*/
|
||||
return_code: number;
|
||||
|
||||
/**
|
||||
* Output file Id list
|
||||
*/
|
||||
content: Array<{ type: 'code_execution_output'; file_id: string }>;
|
||||
}
|
||||
| {
|
||||
type: 'encrypted_code_execution_result';
|
||||
|
||||
/**
|
||||
* Encrypted output from successful execution
|
||||
*/
|
||||
encrypted_stdout: string;
|
||||
|
||||
/**
|
||||
* Error messages if execution fails
|
||||
*/
|
||||
stderr: string;
|
||||
|
||||
/**
|
||||
* 0 for success, non-zero for failure
|
||||
*/
|
||||
return_code: number;
|
||||
|
||||
/**
|
||||
* Output file Id list
|
||||
*/
|
||||
content: Array<{ type: 'code_execution_output'; file_id: string }>;
|
||||
}
|
||||
| {
|
||||
type: 'bash_code_execution_result';
|
||||
|
||||
/**
|
||||
* Output file Id list
|
||||
*/
|
||||
content: Array<{
|
||||
type: 'bash_code_execution_output';
|
||||
file_id: string;
|
||||
}>;
|
||||
|
||||
/**
|
||||
* Output from successful execution
|
||||
*/
|
||||
stdout: string;
|
||||
|
||||
/**
|
||||
* Error messages if execution fails
|
||||
*/
|
||||
stderr: string;
|
||||
|
||||
/**
|
||||
* 0 for success, non-zero for failure
|
||||
*/
|
||||
return_code: number;
|
||||
}
|
||||
| {
|
||||
type: 'bash_code_execution_tool_result_error';
|
||||
|
||||
/**
|
||||
* Available options: invalid_tool_input, unavailable, too_many_requests,
|
||||
* execution_time_exceeded, output_file_too_large.
|
||||
*/
|
||||
error_code: string;
|
||||
}
|
||||
| {
|
||||
type: 'text_editor_code_execution_tool_result_error';
|
||||
|
||||
/**
|
||||
* Available options: invalid_tool_input, unavailable, too_many_requests,
|
||||
* execution_time_exceeded, file_not_found.
|
||||
*/
|
||||
error_code: string;
|
||||
}
|
||||
| {
|
||||
type: 'text_editor_code_execution_view_result';
|
||||
|
||||
content: string;
|
||||
|
||||
/**
|
||||
* The type of the file. Available options: text, image, pdf.
|
||||
*/
|
||||
file_type: string;
|
||||
|
||||
num_lines: number | null;
|
||||
start_line: number | null;
|
||||
total_lines: number | null;
|
||||
}
|
||||
| {
|
||||
type: 'text_editor_code_execution_create_result';
|
||||
|
||||
is_file_update: boolean;
|
||||
}
|
||||
| {
|
||||
type: 'text_editor_code_execution_str_replace_result';
|
||||
|
||||
lines: string[] | null;
|
||||
new_lines: number | null;
|
||||
new_start: number | null;
|
||||
old_lines: number | null;
|
||||
old_start: number | null;
|
||||
},
|
||||
{
|
||||
// no arguments
|
||||
}
|
||||
>({
|
||||
id: 'anthropic.code_execution_20260120',
|
||||
inputSchema: codeExecution_20260120InputSchema,
|
||||
outputSchema: codeExecution_20260120OutputSchema,
|
||||
supportsDeferredResults: true,
|
||||
});
|
||||
|
||||
export const codeExecution_20260120 = (
|
||||
args: Parameters<typeof factory>[0] = {},
|
||||
) => {
|
||||
return factory(args);
|
||||
};
|
||||
87
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/tool/computer_20241022.ts
generated
vendored
Normal file
87
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/tool/computer_20241022.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
import {
|
||||
createProviderToolFactory,
|
||||
lazySchema,
|
||||
zodSchema,
|
||||
} from '@ai-sdk/provider-utils';
|
||||
import { z } from 'zod/v4';
|
||||
|
||||
const computer_20241022InputSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.object({
|
||||
action: z.enum([
|
||||
'key',
|
||||
'type',
|
||||
'mouse_move',
|
||||
'left_click',
|
||||
'left_click_drag',
|
||||
'right_click',
|
||||
'middle_click',
|
||||
'double_click',
|
||||
'screenshot',
|
||||
'cursor_position',
|
||||
]),
|
||||
coordinate: z.array(z.number().int()).optional(),
|
||||
text: z.string().optional(),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
export const computer_20241022 = createProviderToolFactory<
|
||||
{
|
||||
/**
|
||||
* The action to perform. The available actions are:
|
||||
* - `key`: Press a key or key-combination on the keyboard.
|
||||
* - This supports xdotool's `key` syntax.
|
||||
* - Examples: "a", "Return", "alt+Tab", "ctrl+s", "Up", "KP_0" (for the numpad 0 key).
|
||||
* - `type`: Type a string of text on the keyboard.
|
||||
* - `cursor_position`: Get the current (x, y) pixel coordinate of the cursor on the screen.
|
||||
* - `mouse_move`: Move the cursor to a specified (x, y) pixel coordinate on the screen.
|
||||
* - `left_click`: Click the left mouse button.
|
||||
* - `left_click_drag`: Click and drag the cursor to a specified (x, y) pixel coordinate on the screen.
|
||||
* - `right_click`: Click the right mouse button.
|
||||
* - `middle_click`: Click the middle mouse button.
|
||||
* - `double_click`: Double-click the left mouse button.
|
||||
* - `screenshot`: Take a screenshot of the screen.
|
||||
*/
|
||||
action:
|
||||
| 'key'
|
||||
| 'type'
|
||||
| 'mouse_move'
|
||||
| 'left_click'
|
||||
| 'left_click_drag'
|
||||
| 'right_click'
|
||||
| 'middle_click'
|
||||
| 'double_click'
|
||||
| 'screenshot'
|
||||
| 'cursor_position';
|
||||
|
||||
/**
|
||||
* (x, y): The x (pixels from the left edge) and y (pixels from the top edge) coordinates to move the mouse to. Required only by `action=mouse_move` and `action=left_click_drag`.
|
||||
*/
|
||||
coordinate?: number[];
|
||||
|
||||
/**
|
||||
* Required only by `action=type` and `action=key`.
|
||||
*/
|
||||
text?: string;
|
||||
},
|
||||
{
|
||||
/**
|
||||
* The width of the display being controlled by the model in pixels.
|
||||
*/
|
||||
displayWidthPx: number;
|
||||
|
||||
/**
|
||||
* The height of the display being controlled by the model in pixels.
|
||||
*/
|
||||
displayHeightPx: number;
|
||||
|
||||
/**
|
||||
* The display number to control (only relevant for X11 environments). If specified, the tool will be provided a display number in the tool definition.
|
||||
*/
|
||||
displayNumber?: number;
|
||||
}
|
||||
>({
|
||||
id: 'anthropic.computer_20241022',
|
||||
inputSchema: computer_20241022InputSchema,
|
||||
});
|
||||
130
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/tool/computer_20250124.ts
generated
vendored
Normal file
130
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/tool/computer_20250124.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
import {
|
||||
createProviderToolFactory,
|
||||
lazySchema,
|
||||
zodSchema,
|
||||
} from '@ai-sdk/provider-utils';
|
||||
import { z } from 'zod/v4';
|
||||
|
||||
const computer_20250124InputSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.object({
|
||||
action: z.enum([
|
||||
'key',
|
||||
'hold_key',
|
||||
'type',
|
||||
'cursor_position',
|
||||
'mouse_move',
|
||||
'left_mouse_down',
|
||||
'left_mouse_up',
|
||||
'left_click',
|
||||
'left_click_drag',
|
||||
'right_click',
|
||||
'middle_click',
|
||||
'double_click',
|
||||
'triple_click',
|
||||
'scroll',
|
||||
'wait',
|
||||
'screenshot',
|
||||
]),
|
||||
coordinate: z.tuple([z.number().int(), z.number().int()]).optional(),
|
||||
duration: z.number().optional(),
|
||||
scroll_amount: z.number().optional(),
|
||||
scroll_direction: z.enum(['up', 'down', 'left', 'right']).optional(),
|
||||
start_coordinate: z
|
||||
.tuple([z.number().int(), z.number().int()])
|
||||
.optional(),
|
||||
text: z.string().optional(),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
export const computer_20250124 = createProviderToolFactory<
|
||||
{
|
||||
/**
|
||||
* - `key`: Press a key or key-combination on the keyboard.
|
||||
* - This supports xdotool's `key` syntax.
|
||||
* - Examples: "a", "Return", "alt+Tab", "ctrl+s", "Up", "KP_0" (for the numpad 0 key).
|
||||
* - `hold_key`: Hold down a key or multiple keys for a specified duration (in seconds). Supports the same syntax as `key`.
|
||||
* - `type`: Type a string of text on the keyboard.
|
||||
* - `cursor_position`: Get the current (x, y) pixel coordinate of the cursor on the screen.
|
||||
* - `mouse_move`: Move the cursor to a specified (x, y) pixel coordinate on the screen.
|
||||
* - `left_mouse_down`: Press the left mouse button.
|
||||
* - `left_mouse_up`: Release the left mouse button.
|
||||
* - `left_click`: Click the left mouse button at the specified (x, y) pixel coordinate on the screen. You can also include a key combination to hold down while clicking using the `text` parameter.
|
||||
* - `left_click_drag`: Click and drag the cursor from `start_coordinate` to a specified (x, y) pixel coordinate on the screen.
|
||||
* - `right_click`: Click the right mouse button at the specified (x, y) pixel coordinate on the screen.
|
||||
* - `middle_click`: Click the middle mouse button at the specified (x, y) pixel coordinate on the screen.
|
||||
* - `double_click`: Double-click the left mouse button at the specified (x, y) pixel coordinate on the screen.
|
||||
* - `triple_click`: Triple-click the left mouse button at the specified (x, y) pixel coordinate on the screen.
|
||||
* - `scroll`: Scroll the screen in a specified direction by a specified amount of clicks of the scroll wheel, at the specified (x, y) pixel coordinate. DO NOT use PageUp/PageDown to scroll.
|
||||
* - `wait`: Wait for a specified duration (in seconds).
|
||||
* - `screenshot`: Take a screenshot of the screen.
|
||||
*/
|
||||
action:
|
||||
| 'key'
|
||||
| 'hold_key'
|
||||
| 'type'
|
||||
| 'cursor_position'
|
||||
| 'mouse_move'
|
||||
| 'left_mouse_down'
|
||||
| 'left_mouse_up'
|
||||
| 'left_click'
|
||||
| 'left_click_drag'
|
||||
| 'right_click'
|
||||
| 'middle_click'
|
||||
| 'double_click'
|
||||
| 'triple_click'
|
||||
| 'scroll'
|
||||
| 'wait'
|
||||
| 'screenshot';
|
||||
|
||||
/**
|
||||
* (x, y): The x (pixels from the left edge) and y (pixels from the top edge) coordinates to move the mouse to. Required only by `action=mouse_move` and `action=left_click_drag`.
|
||||
*/
|
||||
coordinate?: [number, number];
|
||||
|
||||
/**
|
||||
* The duration to hold the key down for. Required only by `action=hold_key` and `action=wait`.
|
||||
*/
|
||||
duration?: number;
|
||||
|
||||
/**
|
||||
* The number of 'clicks' to scroll. Required only by `action=scroll`.
|
||||
*/
|
||||
scroll_amount?: number;
|
||||
|
||||
/**
|
||||
* The direction to scroll the screen. Required only by `action=scroll`.
|
||||
*/
|
||||
scroll_direction?: 'up' | 'down' | 'left' | 'right';
|
||||
|
||||
/**
|
||||
* (x, y): The x (pixels from the left edge) and y (pixels from the top edge) coordinates to start the drag from. Required only by `action=left_click_drag`.
|
||||
*/
|
||||
start_coordinate?: [number, number];
|
||||
|
||||
/**
|
||||
* Required only by `action=type`, `action=key`, and `action=hold_key`. Can also be used by click or scroll actions to hold down keys while clicking or scrolling.
|
||||
*/
|
||||
text?: string;
|
||||
},
|
||||
{
|
||||
/**
|
||||
* The width of the display being controlled by the model in pixels.
|
||||
*/
|
||||
displayWidthPx: number;
|
||||
|
||||
/**
|
||||
* The height of the display being controlled by the model in pixels.
|
||||
*/
|
||||
displayHeightPx: number;
|
||||
|
||||
/**
|
||||
* The display number to control (only relevant for X11 environments). If specified, the tool will be provided a display number in the tool definition.
|
||||
*/
|
||||
displayNumber?: number;
|
||||
}
|
||||
>({
|
||||
id: 'anthropic.computer_20250124',
|
||||
inputSchema: computer_20250124InputSchema,
|
||||
});
|
||||
151
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/tool/computer_20251124.ts
generated
vendored
Normal file
151
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/tool/computer_20251124.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,151 @@
|
|||
import {
|
||||
createProviderToolFactory,
|
||||
lazySchema,
|
||||
zodSchema,
|
||||
} from '@ai-sdk/provider-utils';
|
||||
import { z } from 'zod/v4';
|
||||
|
||||
const computer_20251124InputSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.object({
|
||||
action: z.enum([
|
||||
'key',
|
||||
'hold_key',
|
||||
'type',
|
||||
'cursor_position',
|
||||
'mouse_move',
|
||||
'left_mouse_down',
|
||||
'left_mouse_up',
|
||||
'left_click',
|
||||
'left_click_drag',
|
||||
'right_click',
|
||||
'middle_click',
|
||||
'double_click',
|
||||
'triple_click',
|
||||
'scroll',
|
||||
'wait',
|
||||
'screenshot',
|
||||
'zoom',
|
||||
]),
|
||||
coordinate: z.tuple([z.number().int(), z.number().int()]).optional(),
|
||||
duration: z.number().optional(),
|
||||
region: z
|
||||
.tuple([
|
||||
z.number().int(),
|
||||
z.number().int(),
|
||||
z.number().int(),
|
||||
z.number().int(),
|
||||
])
|
||||
.optional(),
|
||||
scroll_amount: z.number().optional(),
|
||||
scroll_direction: z.enum(['up', 'down', 'left', 'right']).optional(),
|
||||
start_coordinate: z
|
||||
.tuple([z.number().int(), z.number().int()])
|
||||
.optional(),
|
||||
text: z.string().optional(),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
export const computer_20251124 = createProviderToolFactory<
|
||||
{
|
||||
/**
|
||||
* - `key`: Press a key or key-combination on the keyboard.
|
||||
* - This supports xdotool's `key` syntax.
|
||||
* - Examples: "a", "Return", "alt+Tab", "ctrl+s", "Up", "KP_0" (for the numpad 0 key).
|
||||
* - `hold_key`: Hold down a key or multiple keys for a specified duration (in seconds). Supports the same syntax as `key`.
|
||||
* - `type`: Type a string of text on the keyboard.
|
||||
* - `cursor_position`: Get the current (x, y) pixel coordinate of the cursor on the screen.
|
||||
* - `mouse_move`: Move the cursor to a specified (x, y) pixel coordinate on the screen.
|
||||
* - `left_mouse_down`: Press the left mouse button.
|
||||
* - `left_mouse_up`: Release the left mouse button.
|
||||
* - `left_click`: Click the left mouse button at the specified (x, y) pixel coordinate on the screen. You can also include a key combination to hold down while clicking using the `text` parameter.
|
||||
* - `left_click_drag`: Click and drag the cursor from `start_coordinate` to a specified (x, y) pixel coordinate on the screen.
|
||||
* - `right_click`: Click the right mouse button at the specified (x, y) pixel coordinate on the screen.
|
||||
* - `middle_click`: Click the middle mouse button at the specified (x, y) pixel coordinate on the screen.
|
||||
* - `double_click`: Double-click the left mouse button at the specified (x, y) pixel coordinate on the screen.
|
||||
* - `triple_click`: Triple-click the left mouse button at the specified (x, y) pixel coordinate on the screen.
|
||||
* - `scroll`: Scroll the screen in a specified direction by a specified amount of clicks of the scroll wheel, at the specified (x, y) pixel coordinate. DO NOT use PageUp/PageDown to scroll.
|
||||
* - `wait`: Wait for a specified duration (in seconds).
|
||||
* - `screenshot`: Take a screenshot of the screen.
|
||||
* - `zoom`: View a specific region of the screen at full resolution. Requires `enableZoom: true` in tool definition. Takes a `region` parameter with coordinates `[x1, y1, x2, y2]` defining top-left and bottom-right corners of the area to inspect.
|
||||
*/
|
||||
action:
|
||||
| 'key'
|
||||
| 'hold_key'
|
||||
| 'type'
|
||||
| 'cursor_position'
|
||||
| 'mouse_move'
|
||||
| 'left_mouse_down'
|
||||
| 'left_mouse_up'
|
||||
| 'left_click'
|
||||
| 'left_click_drag'
|
||||
| 'right_click'
|
||||
| 'middle_click'
|
||||
| 'double_click'
|
||||
| 'triple_click'
|
||||
| 'scroll'
|
||||
| 'wait'
|
||||
| 'screenshot'
|
||||
| 'zoom';
|
||||
|
||||
/**
|
||||
* (x, y): The x (pixels from the left edge) and y (pixels from the top edge) coordinates to move the mouse to. Required only by `action=mouse_move` and `action=left_click_drag`.
|
||||
*/
|
||||
coordinate?: [number, number];
|
||||
|
||||
/**
|
||||
* The duration to hold the key down for. Required only by `action=hold_key` and `action=wait`.
|
||||
*/
|
||||
duration?: number;
|
||||
|
||||
/**
|
||||
* [x1, y1, x2, y2]: The coordinates defining the region to zoom into. x1, y1 is the top-left corner and x2, y2 is the bottom-right corner. Required only by `action=zoom`.
|
||||
*/
|
||||
region?: [number, number, number, number];
|
||||
|
||||
/**
|
||||
* The number of 'clicks' to scroll. Required only by `action=scroll`.
|
||||
*/
|
||||
scroll_amount?: number;
|
||||
|
||||
/**
|
||||
* The direction to scroll the screen. Required only by `action=scroll`.
|
||||
*/
|
||||
scroll_direction?: 'up' | 'down' | 'left' | 'right';
|
||||
|
||||
/**
|
||||
* (x, y): The x (pixels from the left edge) and y (pixels from the top edge) coordinates to start the drag from. Required only by `action=left_click_drag`.
|
||||
*/
|
||||
start_coordinate?: [number, number];
|
||||
|
||||
/**
|
||||
* Required only by `action=type`, `action=key`, and `action=hold_key`. Can also be used by click or scroll actions to hold down keys while clicking or scrolling.
|
||||
*/
|
||||
text?: string;
|
||||
},
|
||||
{
|
||||
/**
|
||||
* The width of the display being controlled by the model in pixels.
|
||||
*/
|
||||
displayWidthPx: number;
|
||||
|
||||
/**
|
||||
* The height of the display being controlled by the model in pixels.
|
||||
*/
|
||||
displayHeightPx: number;
|
||||
|
||||
/**
|
||||
* The display number to control (only relevant for X11 environments). If specified, the tool will be provided a display number in the tool definition.
|
||||
*/
|
||||
displayNumber?: number;
|
||||
|
||||
/**
|
||||
* Enable zoom action. Set to true to allow Claude to zoom into specific screen regions. Default: false.
|
||||
*/
|
||||
enableZoom?: boolean;
|
||||
}
|
||||
>({
|
||||
id: 'anthropic.computer_20251124',
|
||||
inputSchema: computer_20251124InputSchema,
|
||||
});
|
||||
62
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/tool/memory_20250818.ts
generated
vendored
Normal file
62
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/tool/memory_20250818.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
import {
|
||||
createProviderToolFactory,
|
||||
lazySchema,
|
||||
zodSchema,
|
||||
} from '@ai-sdk/provider-utils';
|
||||
import { z } from 'zod/v4';
|
||||
|
||||
const memory_20250818InputSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.discriminatedUnion('command', [
|
||||
z.object({
|
||||
command: z.literal('view'),
|
||||
path: z.string(),
|
||||
view_range: z.tuple([z.number(), z.number()]).optional(),
|
||||
}),
|
||||
z.object({
|
||||
command: z.literal('create'),
|
||||
path: z.string(),
|
||||
file_text: z.string(),
|
||||
}),
|
||||
z.object({
|
||||
command: z.literal('str_replace'),
|
||||
path: z.string(),
|
||||
old_str: z.string(),
|
||||
new_str: z.string(),
|
||||
}),
|
||||
z.object({
|
||||
command: z.literal('insert'),
|
||||
path: z.string(),
|
||||
insert_line: z.number(),
|
||||
insert_text: z.string(),
|
||||
}),
|
||||
z.object({
|
||||
command: z.literal('delete'),
|
||||
path: z.string(),
|
||||
}),
|
||||
z.object({
|
||||
command: z.literal('rename'),
|
||||
old_path: z.string(),
|
||||
new_path: z.string(),
|
||||
}),
|
||||
]),
|
||||
),
|
||||
);
|
||||
|
||||
export const memory_20250818 = createProviderToolFactory<
|
||||
| { command: 'view'; path: string; view_range?: [number, number] }
|
||||
| { command: 'create'; path: string; file_text: string }
|
||||
| { command: 'str_replace'; path: string; old_str: string; new_str: string }
|
||||
| {
|
||||
command: 'insert';
|
||||
path: string;
|
||||
insert_line: number;
|
||||
insert_text: string;
|
||||
}
|
||||
| { command: 'delete'; path: string }
|
||||
| { command: 'rename'; old_path: string; new_path: string },
|
||||
{}
|
||||
>({
|
||||
id: 'anthropic.memory_20250818',
|
||||
inputSchema: memory_20250818InputSchema,
|
||||
});
|
||||
69
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/tool/text-editor_20241022.ts
generated
vendored
Normal file
69
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/tool/text-editor_20241022.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
import {
|
||||
createProviderToolFactory,
|
||||
lazySchema,
|
||||
zodSchema,
|
||||
} from '@ai-sdk/provider-utils';
|
||||
import { z } from 'zod/v4';
|
||||
|
||||
const textEditor_20241022InputSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.object({
|
||||
command: z.enum(['view', 'create', 'str_replace', 'insert', 'undo_edit']),
|
||||
path: z.string(),
|
||||
file_text: z.string().optional(),
|
||||
insert_line: z.number().int().optional(),
|
||||
new_str: z.string().optional(),
|
||||
insert_text: z.string().optional(),
|
||||
old_str: z.string().optional(),
|
||||
view_range: z.array(z.number().int()).optional(),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
export const textEditor_20241022 = createProviderToolFactory<
|
||||
{
|
||||
/**
|
||||
* The commands to run. Allowed options are: `view`, `create`, `str_replace`, `insert`, `undo_edit`.
|
||||
*/
|
||||
command: 'view' | 'create' | 'str_replace' | 'insert' | 'undo_edit';
|
||||
|
||||
/**
|
||||
* Absolute path to file or directory, e.g. `/repo/file.py` or `/repo`.
|
||||
*/
|
||||
path: string;
|
||||
|
||||
/**
|
||||
* Required parameter of `create` command, with the content of the file to be created.
|
||||
*/
|
||||
file_text?: string;
|
||||
|
||||
/**
|
||||
* Required parameter of `insert` command. The `new_str` will be inserted AFTER the line `insert_line` of `path`.
|
||||
*/
|
||||
insert_line?: number;
|
||||
|
||||
/**
|
||||
* Optional parameter of `str_replace` command containing the new string (if not given, no string will be added).
|
||||
*/
|
||||
new_str?: string;
|
||||
|
||||
/**
|
||||
* Required parameter of `insert` command containing the text to insert.
|
||||
*/
|
||||
insert_text?: string;
|
||||
|
||||
/**
|
||||
* Required parameter of `str_replace` command containing the string in `path` to replace.
|
||||
*/
|
||||
old_str?: string;
|
||||
|
||||
/**
|
||||
* Optional parameter of `view` command when `path` points to a file. If none is given, the full file is shown. If provided, the file will be shown in the indicated line number range, e.g. [11, 12] will show lines 11 and 12. Indexing at 1 to start. Setting `[start_line, -1]` shows all lines from `start_line` to the end of the file.
|
||||
*/
|
||||
view_range?: number[];
|
||||
},
|
||||
{}
|
||||
>({
|
||||
id: 'anthropic.text_editor_20241022',
|
||||
inputSchema: textEditor_20241022InputSchema,
|
||||
});
|
||||
69
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/tool/text-editor_20250124.ts
generated
vendored
Normal file
69
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/tool/text-editor_20250124.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
import {
|
||||
createProviderToolFactory,
|
||||
lazySchema,
|
||||
zodSchema,
|
||||
} from '@ai-sdk/provider-utils';
|
||||
import { z } from 'zod/v4';
|
||||
|
||||
const textEditor_20250124InputSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.object({
|
||||
command: z.enum(['view', 'create', 'str_replace', 'insert', 'undo_edit']),
|
||||
path: z.string(),
|
||||
file_text: z.string().optional(),
|
||||
insert_line: z.number().int().optional(),
|
||||
new_str: z.string().optional(),
|
||||
insert_text: z.string().optional(),
|
||||
old_str: z.string().optional(),
|
||||
view_range: z.array(z.number().int()).optional(),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
export const textEditor_20250124 = createProviderToolFactory<
|
||||
{
|
||||
/**
|
||||
* The commands to run. Allowed options are: `view`, `create`, `str_replace`, `insert`, `undo_edit`.
|
||||
*/
|
||||
command: 'view' | 'create' | 'str_replace' | 'insert' | 'undo_edit';
|
||||
|
||||
/**
|
||||
* Absolute path to file or directory, e.g. `/repo/file.py` or `/repo`.
|
||||
*/
|
||||
path: string;
|
||||
|
||||
/**
|
||||
* Required parameter of `create` command, with the content of the file to be created.
|
||||
*/
|
||||
file_text?: string;
|
||||
|
||||
/**
|
||||
* Required parameter of `insert` command. The `new_str` will be inserted AFTER the line `insert_line` of `path`.
|
||||
*/
|
||||
insert_line?: number;
|
||||
|
||||
/**
|
||||
* Optional parameter of `str_replace` command containing the new string (if not given, no string will be added).
|
||||
*/
|
||||
new_str?: string;
|
||||
|
||||
/**
|
||||
* Required parameter of `insert` command containing the text to insert.
|
||||
*/
|
||||
insert_text?: string;
|
||||
|
||||
/**
|
||||
* Required parameter of `str_replace` command containing the string in `path` to replace.
|
||||
*/
|
||||
old_str?: string;
|
||||
|
||||
/**
|
||||
* Optional parameter of `view` command when `path` points to a file. If none is given, the full file is shown. If provided, the file will be shown in the indicated line number range, e.g. [11, 12] will show lines 11 and 12. Indexing at 1 to start. Setting `[start_line, -1]` shows all lines from `start_line` to the end of the file.
|
||||
*/
|
||||
view_range?: number[];
|
||||
},
|
||||
{}
|
||||
>({
|
||||
id: 'anthropic.text_editor_20250124',
|
||||
inputSchema: textEditor_20250124InputSchema,
|
||||
});
|
||||
70
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/tool/text-editor_20250429.ts
generated
vendored
Normal file
70
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/tool/text-editor_20250429.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
import {
|
||||
createProviderToolFactory,
|
||||
lazySchema,
|
||||
zodSchema,
|
||||
} from '@ai-sdk/provider-utils';
|
||||
import { z } from 'zod/v4';
|
||||
|
||||
const textEditor_20250429InputSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.object({
|
||||
command: z.enum(['view', 'create', 'str_replace', 'insert']),
|
||||
path: z.string(),
|
||||
file_text: z.string().optional(),
|
||||
insert_line: z.number().int().optional(),
|
||||
new_str: z.string().optional(),
|
||||
insert_text: z.string().optional(),
|
||||
old_str: z.string().optional(),
|
||||
view_range: z.array(z.number().int()).optional(),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
export const textEditor_20250429 = createProviderToolFactory<
|
||||
{
|
||||
/**
|
||||
* The commands to run. Allowed options are: `view`, `create`, `str_replace`, `insert`.
|
||||
* Note: `undo_edit` is not supported in Claude 4 models.
|
||||
*/
|
||||
command: 'view' | 'create' | 'str_replace' | 'insert';
|
||||
|
||||
/**
|
||||
* Absolute path to file or directory, e.g. `/repo/file.py` or `/repo`.
|
||||
*/
|
||||
path: string;
|
||||
|
||||
/**
|
||||
* Required parameter of `create` command, with the content of the file to be created.
|
||||
*/
|
||||
file_text?: string;
|
||||
|
||||
/**
|
||||
* Required parameter of `insert` command. The `new_str` will be inserted AFTER the line `insert_line` of `path`.
|
||||
*/
|
||||
insert_line?: number;
|
||||
|
||||
/**
|
||||
* Optional parameter of `str_replace` command containing the new string (if not given, no string will be added).
|
||||
*/
|
||||
new_str?: string;
|
||||
|
||||
/**
|
||||
* Required parameter of `insert` command containing the text to insert.
|
||||
*/
|
||||
insert_text?: string;
|
||||
|
||||
/**
|
||||
* Required parameter of `str_replace` command containing the string in `path` to replace.
|
||||
*/
|
||||
old_str?: string;
|
||||
|
||||
/**
|
||||
* Optional parameter of `view` command when `path` points to a file. If none is given, the full file is shown. If provided, the file will be shown in the indicated line number range, e.g. [11, 12] will show lines 11 and 12. Indexing at 1 to start. Setting `[start_line, -1]` shows all lines from `start_line` to the end of the file.
|
||||
*/
|
||||
view_range?: number[];
|
||||
},
|
||||
{}
|
||||
>({
|
||||
id: 'anthropic.text_editor_20250429',
|
||||
inputSchema: textEditor_20250429InputSchema,
|
||||
});
|
||||
86
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/tool/text-editor_20250728.ts
generated
vendored
Normal file
86
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/tool/text-editor_20250728.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
import { createProviderToolFactory } from '@ai-sdk/provider-utils';
|
||||
import { z } from 'zod/v4';
|
||||
import { lazySchema, zodSchema } from '@ai-sdk/provider-utils';
|
||||
|
||||
export const textEditor_20250728ArgsSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.object({
|
||||
maxCharacters: z.number().optional(),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
const textEditor_20250728InputSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.object({
|
||||
command: z.enum(['view', 'create', 'str_replace', 'insert']),
|
||||
path: z.string(),
|
||||
file_text: z.string().optional(),
|
||||
insert_line: z.number().int().optional(),
|
||||
new_str: z.string().optional(),
|
||||
insert_text: z.string().optional(),
|
||||
old_str: z.string().optional(),
|
||||
view_range: z.array(z.number().int()).optional(),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
const factory = createProviderToolFactory<
|
||||
{
|
||||
/**
|
||||
* The commands to run. Allowed options are: `view`, `create`, `str_replace`, `insert`.
|
||||
* Note: `undo_edit` is not supported in Claude 4 models.
|
||||
*/
|
||||
command: 'view' | 'create' | 'str_replace' | 'insert';
|
||||
|
||||
/**
|
||||
* Absolute path to file or directory, e.g. `/repo/file.py` or `/repo`.
|
||||
*/
|
||||
path: string;
|
||||
|
||||
/**
|
||||
* Required parameter of `create` command, with the content of the file to be created.
|
||||
*/
|
||||
file_text?: string;
|
||||
|
||||
/**
|
||||
* Required parameter of `insert` command. The `new_str` will be inserted AFTER the line `insert_line` of `path`.
|
||||
*/
|
||||
insert_line?: number;
|
||||
|
||||
/**
|
||||
* Optional parameter of `str_replace` command containing the new string (if not given, no string will be added).
|
||||
*/
|
||||
new_str?: string;
|
||||
|
||||
/**
|
||||
* Required parameter of `insert` command containing the text to insert.
|
||||
*/
|
||||
insert_text?: string;
|
||||
|
||||
/**
|
||||
* Required parameter of `str_replace` command containing the string in `path` to replace.
|
||||
*/
|
||||
old_str?: string;
|
||||
|
||||
/**
|
||||
* Optional parameter of `view` command when `path` points to a file. If none is given, the full file is shown. If provided, the file will be shown in the indicated line number range, e.g. [11, 12] will show lines 11 and 12. Indexing at 1 to start. Setting `[start_line, -1]` shows all lines from `start_line` to the end of the file.
|
||||
*/
|
||||
view_range?: number[];
|
||||
},
|
||||
{
|
||||
/**
|
||||
* Optional parameter to control truncation when viewing large files. Only compatible with text_editor_20250728 and later versions.
|
||||
*/
|
||||
maxCharacters?: number;
|
||||
}
|
||||
>({
|
||||
id: 'anthropic.text_editor_20250728',
|
||||
inputSchema: textEditor_20250728InputSchema,
|
||||
});
|
||||
|
||||
export const textEditor_20250728 = (
|
||||
args: Parameters<typeof factory>[0] = {}, // default
|
||||
) => {
|
||||
return factory(args);
|
||||
};
|
||||
99
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/tool/tool-search-bm25_20251119.ts
generated
vendored
Normal file
99
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/tool/tool-search-bm25_20251119.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
import {
|
||||
createProviderToolFactoryWithOutputSchema,
|
||||
lazySchema,
|
||||
zodSchema,
|
||||
} from '@ai-sdk/provider-utils';
|
||||
import { z } from 'zod/v4';
|
||||
|
||||
/**
|
||||
* Output schema for tool search results - returns tool references
|
||||
* that are automatically expanded into full tool definitions by the API.
|
||||
*/
|
||||
export const toolSearchBm25_20251119OutputSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.array(
|
||||
z.object({
|
||||
type: z.literal('tool_reference'),
|
||||
toolName: z.string(),
|
||||
}),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
/**
|
||||
* Input schema for BM25-based tool search.
|
||||
* Claude uses natural language queries to search for tools.
|
||||
*/
|
||||
const toolSearchBm25_20251119InputSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.object({
|
||||
/**
|
||||
* A natural language query to search for tools.
|
||||
* Claude will use BM25 text search to find relevant tools.
|
||||
*/
|
||||
query: z.string(),
|
||||
/**
|
||||
* Maximum number of tools to return. Optional.
|
||||
*/
|
||||
limit: z.number().optional(),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
const factory = createProviderToolFactoryWithOutputSchema<
|
||||
{
|
||||
/**
|
||||
* A natural language query to search for tools.
|
||||
* Claude will use BM25 text search to find relevant tools.
|
||||
*/
|
||||
query: string;
|
||||
/**
|
||||
* Maximum number of tools to return. Optional.
|
||||
*/
|
||||
limit?: number;
|
||||
},
|
||||
Array<{
|
||||
type: 'tool_reference';
|
||||
/**
|
||||
* The name of the discovered tool.
|
||||
*/
|
||||
toolName: string;
|
||||
}>,
|
||||
{}
|
||||
>({
|
||||
id: 'anthropic.tool_search_bm25_20251119',
|
||||
inputSchema: toolSearchBm25_20251119InputSchema,
|
||||
outputSchema: toolSearchBm25_20251119OutputSchema,
|
||||
supportsDeferredResults: true,
|
||||
});
|
||||
|
||||
/**
|
||||
* Creates a tool search tool that uses BM25 (natural language) to find tools.
|
||||
*
|
||||
* The tool search tool enables Claude to work with hundreds or thousands of tools
|
||||
* by dynamically discovering and loading them on-demand. Instead of loading all
|
||||
* tool definitions into the context window upfront, Claude searches your tool
|
||||
* catalog and loads only the tools it needs.
|
||||
*
|
||||
* When Claude uses this tool, it uses natural language queries (NOT regex patterns)
|
||||
* to search for tools using BM25 text search.
|
||||
*
|
||||
* **Important**: This tool should never have `deferLoading: true` in providerOptions.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* import { anthropicTools } from '@ai-sdk/anthropic';
|
||||
*
|
||||
* const tools = {
|
||||
* toolSearch: anthropicTools.toolSearchBm25_20251119(),
|
||||
* // Other tools with deferLoading...
|
||||
* };
|
||||
* ```
|
||||
*
|
||||
* @see https://docs.anthropic.com/en/docs/agents-and-tools/tool-search-tool
|
||||
*/
|
||||
export const toolSearchBm25_20251119 = (
|
||||
args: Parameters<typeof factory>[0] = {},
|
||||
) => {
|
||||
return factory(args);
|
||||
};
|
||||
111
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/tool/tool-search-regex_20251119.ts
generated
vendored
Normal file
111
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/tool/tool-search-regex_20251119.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
import {
|
||||
createProviderToolFactoryWithOutputSchema,
|
||||
lazySchema,
|
||||
zodSchema,
|
||||
} from '@ai-sdk/provider-utils';
|
||||
import { z } from 'zod/v4';
|
||||
|
||||
/**
|
||||
* Output schema for tool search results - returns tool references
|
||||
* that are automatically expanded into full tool definitions by the API.
|
||||
*/
|
||||
export const toolSearchRegex_20251119OutputSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.array(
|
||||
z.object({
|
||||
type: z.literal('tool_reference'),
|
||||
toolName: z.string(),
|
||||
}),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
/**
|
||||
* Input schema for regex-based tool search.
|
||||
* Claude constructs regex patterns using Python's re.search() syntax.
|
||||
*/
|
||||
const toolSearchRegex_20251119InputSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.object({
|
||||
/**
|
||||
* A regex pattern to search for tools.
|
||||
* Uses Python re.search() syntax. Maximum 200 characters.
|
||||
*
|
||||
* Examples:
|
||||
* - "weather" - matches tool names/descriptions containing "weather"
|
||||
* - "get_.*_data" - matches tools like get_user_data, get_weather_data
|
||||
* - "database.*query|query.*database" - OR patterns for flexibility
|
||||
* - "(?i)slack" - case-insensitive search
|
||||
*/
|
||||
pattern: z.string(),
|
||||
/**
|
||||
* Maximum number of tools to return. Optional.
|
||||
*/
|
||||
limit: z.number().optional(),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
const factory = createProviderToolFactoryWithOutputSchema<
|
||||
{
|
||||
/**
|
||||
* A regex pattern to search for tools.
|
||||
* Uses Python re.search() syntax. Maximum 200 characters.
|
||||
*
|
||||
* Examples:
|
||||
* - "weather" - matches tool names/descriptions containing "weather"
|
||||
* - "get_.*_data" - matches tools like get_user_data, get_weather_data
|
||||
* - "database.*query|query.*database" - OR patterns for flexibility
|
||||
* - "(?i)slack" - case-insensitive search
|
||||
*/
|
||||
pattern: string;
|
||||
/**
|
||||
* Maximum number of tools to return. Optional.
|
||||
*/
|
||||
limit?: number;
|
||||
},
|
||||
Array<{
|
||||
type: 'tool_reference';
|
||||
/**
|
||||
* The name of the discovered tool.
|
||||
*/
|
||||
toolName: string;
|
||||
}>,
|
||||
{}
|
||||
>({
|
||||
id: 'anthropic.tool_search_regex_20251119',
|
||||
inputSchema: toolSearchRegex_20251119InputSchema,
|
||||
outputSchema: toolSearchRegex_20251119OutputSchema,
|
||||
supportsDeferredResults: true,
|
||||
});
|
||||
|
||||
/**
|
||||
* Creates a tool search tool that uses regex patterns to find tools.
|
||||
*
|
||||
* The tool search tool enables Claude to work with hundreds or thousands of tools
|
||||
* by dynamically discovering and loading them on-demand. Instead of loading all
|
||||
* tool definitions into the context window upfront, Claude searches your tool
|
||||
* catalog and loads only the tools it needs.
|
||||
*
|
||||
* When Claude uses this tool, it constructs regex patterns using Python's
|
||||
* re.search() syntax (NOT natural language queries).
|
||||
*
|
||||
* **Important**: This tool should never have `deferLoading: true` in providerOptions.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* import { anthropicTools } from '@ai-sdk/anthropic';
|
||||
*
|
||||
* const tools = {
|
||||
* toolSearch: anthropicTools.toolSearchRegex_20251119(),
|
||||
* // Other tools with deferLoading...
|
||||
* };
|
||||
* ```
|
||||
*
|
||||
* @see https://docs.anthropic.com/en/docs/agents-and-tools/tool-search-tool
|
||||
*/
|
||||
export const toolSearchRegex_20251119 = (
|
||||
args: Parameters<typeof factory>[0] = {},
|
||||
) => {
|
||||
return factory(args);
|
||||
};
|
||||
145
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/tool/web-fetch-20250910.ts
generated
vendored
Normal file
145
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/tool/web-fetch-20250910.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,145 @@
|
|||
import {
|
||||
createProviderToolFactoryWithOutputSchema,
|
||||
lazySchema,
|
||||
zodSchema,
|
||||
} from '@ai-sdk/provider-utils';
|
||||
import { z } from 'zod/v4';
|
||||
|
||||
export const webFetch_20250910ArgsSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.object({
|
||||
maxUses: z.number().optional(),
|
||||
allowedDomains: z.array(z.string()).optional(),
|
||||
blockedDomains: z.array(z.string()).optional(),
|
||||
citations: z.object({ enabled: z.boolean() }).optional(),
|
||||
maxContentTokens: z.number().optional(),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
export const webFetch_20250910OutputSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.object({
|
||||
type: z.literal('web_fetch_result'),
|
||||
url: z.string(),
|
||||
content: z.object({
|
||||
type: z.literal('document'),
|
||||
title: z.string().nullable(),
|
||||
citations: z.object({ enabled: z.boolean() }).optional(),
|
||||
source: z.union([
|
||||
z.object({
|
||||
type: z.literal('base64'),
|
||||
mediaType: z.literal('application/pdf'),
|
||||
data: z.string(),
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('text'),
|
||||
mediaType: z.literal('text/plain'),
|
||||
data: z.string(),
|
||||
}),
|
||||
]),
|
||||
}),
|
||||
retrievedAt: z.string().nullable(),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
const webFetch_20250910InputSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.object({
|
||||
url: z.string(),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
const factory = createProviderToolFactoryWithOutputSchema<
|
||||
{
|
||||
/**
|
||||
* The URL to fetch.
|
||||
*/
|
||||
url: string;
|
||||
},
|
||||
{
|
||||
type: 'web_fetch_result';
|
||||
|
||||
/**
|
||||
* Fetched content URL
|
||||
*/
|
||||
url: string;
|
||||
|
||||
/**
|
||||
* Fetched content.
|
||||
*/
|
||||
content: {
|
||||
type: 'document';
|
||||
|
||||
/**
|
||||
* Title of the document
|
||||
*/
|
||||
title: string | null;
|
||||
|
||||
/**
|
||||
* Citation configuration for the document
|
||||
*/
|
||||
citations?: { enabled: boolean };
|
||||
|
||||
source:
|
||||
| {
|
||||
type: 'base64';
|
||||
mediaType: 'application/pdf';
|
||||
data: string;
|
||||
}
|
||||
| {
|
||||
type: 'text';
|
||||
mediaType: 'text/plain';
|
||||
data: string;
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* ISO 8601 timestamp when the content was retrieved
|
||||
*/
|
||||
retrievedAt: string | null;
|
||||
},
|
||||
{
|
||||
/**
|
||||
* The maxUses parameter limits the number of web fetches performed
|
||||
*/
|
||||
maxUses?: number;
|
||||
|
||||
/**
|
||||
* Only fetch from these domains
|
||||
*/
|
||||
allowedDomains?: string[];
|
||||
|
||||
/**
|
||||
* Never fetch from these domains
|
||||
*/
|
||||
blockedDomains?: string[];
|
||||
|
||||
/**
|
||||
* Unlike web search where citations are always enabled, citations are optional for
|
||||
* web fetch. Set "citations": {"enabled": true} to enable Claude to cite specific passages
|
||||
* from fetched documents.
|
||||
*/
|
||||
citations?: {
|
||||
enabled: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
* The maxContentTokens parameter limits the amount of content that will be included in the context.
|
||||
*/
|
||||
maxContentTokens?: number;
|
||||
}
|
||||
>({
|
||||
id: 'anthropic.web_fetch_20250910',
|
||||
inputSchema: webFetch_20250910InputSchema,
|
||||
outputSchema: webFetch_20250910OutputSchema,
|
||||
supportsDeferredResults: true,
|
||||
});
|
||||
|
||||
export const webFetch_20250910 = (
|
||||
args: Parameters<typeof factory>[0] = {}, // default
|
||||
) => {
|
||||
return factory(args);
|
||||
};
|
||||
145
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/tool/web-fetch-20260209.ts
generated
vendored
Normal file
145
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/tool/web-fetch-20260209.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,145 @@
|
|||
import {
|
||||
createProviderToolFactoryWithOutputSchema,
|
||||
lazySchema,
|
||||
zodSchema,
|
||||
} from '@ai-sdk/provider-utils';
|
||||
import { z } from 'zod/v4';
|
||||
|
||||
export const webFetch_20260209ArgsSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.object({
|
||||
maxUses: z.number().optional(),
|
||||
allowedDomains: z.array(z.string()).optional(),
|
||||
blockedDomains: z.array(z.string()).optional(),
|
||||
citations: z.object({ enabled: z.boolean() }).optional(),
|
||||
maxContentTokens: z.number().optional(),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
export const webFetch_20260209OutputSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.object({
|
||||
type: z.literal('web_fetch_result'),
|
||||
url: z.string(),
|
||||
content: z.object({
|
||||
type: z.literal('document'),
|
||||
title: z.string().nullable(),
|
||||
citations: z.object({ enabled: z.boolean() }).optional(),
|
||||
source: z.union([
|
||||
z.object({
|
||||
type: z.literal('base64'),
|
||||
mediaType: z.literal('application/pdf'),
|
||||
data: z.string(),
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('text'),
|
||||
mediaType: z.literal('text/plain'),
|
||||
data: z.string(),
|
||||
}),
|
||||
]),
|
||||
}),
|
||||
retrievedAt: z.string().nullable(),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
const webFetch_20260209InputSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.object({
|
||||
url: z.string(),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
const factory = createProviderToolFactoryWithOutputSchema<
|
||||
{
|
||||
/**
|
||||
* The URL to fetch.
|
||||
*/
|
||||
url: string;
|
||||
},
|
||||
{
|
||||
type: 'web_fetch_result';
|
||||
|
||||
/**
|
||||
* Fetched content URL
|
||||
*/
|
||||
url: string;
|
||||
|
||||
/**
|
||||
* Fetched content.
|
||||
*/
|
||||
content: {
|
||||
type: 'document';
|
||||
|
||||
/**
|
||||
* Title of the document
|
||||
*/
|
||||
title: string | null;
|
||||
|
||||
/**
|
||||
* Citation configuration for the document
|
||||
*/
|
||||
citations?: { enabled: boolean };
|
||||
|
||||
source:
|
||||
| {
|
||||
type: 'base64';
|
||||
mediaType: 'application/pdf';
|
||||
data: string;
|
||||
}
|
||||
| {
|
||||
type: 'text';
|
||||
mediaType: 'text/plain';
|
||||
data: string;
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* ISO 8601 timestamp when the content was retrieved
|
||||
*/
|
||||
retrievedAt: string | null;
|
||||
},
|
||||
{
|
||||
/**
|
||||
* The maxUses parameter limits the number of web fetches performed
|
||||
*/
|
||||
maxUses?: number;
|
||||
|
||||
/**
|
||||
* Only fetch from these domains
|
||||
*/
|
||||
allowedDomains?: string[];
|
||||
|
||||
/**
|
||||
* Never fetch from these domains
|
||||
*/
|
||||
blockedDomains?: string[];
|
||||
|
||||
/**
|
||||
* Unlike web search where citations are always enabled, citations are optional for
|
||||
* web fetch. Set "citations": {"enabled": true} to enable Claude to cite specific passages
|
||||
* from fetched documents.
|
||||
*/
|
||||
citations?: {
|
||||
enabled: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
* The maxContentTokens parameter limits the amount of content that will be included in the context.
|
||||
*/
|
||||
maxContentTokens?: number;
|
||||
}
|
||||
>({
|
||||
id: 'anthropic.web_fetch_20260209',
|
||||
inputSchema: webFetch_20260209InputSchema,
|
||||
outputSchema: webFetch_20260209OutputSchema,
|
||||
supportsDeferredResults: true,
|
||||
});
|
||||
|
||||
export const webFetch_20260209 = (
|
||||
args: Parameters<typeof factory>[0] = {}, // default
|
||||
) => {
|
||||
return factory(args);
|
||||
};
|
||||
136
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/tool/web-search_20250305.ts
generated
vendored
Normal file
136
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/tool/web-search_20250305.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,136 @@
|
|||
import {
|
||||
createProviderToolFactoryWithOutputSchema,
|
||||
lazySchema,
|
||||
zodSchema,
|
||||
} from '@ai-sdk/provider-utils';
|
||||
import { z } from 'zod/v4';
|
||||
|
||||
export const webSearch_20250305ArgsSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.object({
|
||||
maxUses: z.number().optional(),
|
||||
allowedDomains: z.array(z.string()).optional(),
|
||||
blockedDomains: z.array(z.string()).optional(),
|
||||
userLocation: z
|
||||
.object({
|
||||
type: z.literal('approximate'),
|
||||
city: z.string().optional(),
|
||||
region: z.string().optional(),
|
||||
country: z.string().optional(),
|
||||
timezone: z.string().optional(),
|
||||
})
|
||||
.optional(),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
export const webSearch_20250305OutputSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.array(
|
||||
z.object({
|
||||
url: z.string(),
|
||||
title: z.string().nullable(),
|
||||
pageAge: z.string().nullable(),
|
||||
encryptedContent: z.string(),
|
||||
type: z.literal('web_search_result'),
|
||||
}),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
const webSearch_20250305InputSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.object({
|
||||
query: z.string(),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
const factory = createProviderToolFactoryWithOutputSchema<
|
||||
{
|
||||
/**
|
||||
* The search query to execute.
|
||||
*/
|
||||
query: string;
|
||||
},
|
||||
Array<{
|
||||
type: 'web_search_result';
|
||||
|
||||
/**
|
||||
* The URL of the source page.
|
||||
*/
|
||||
url: string;
|
||||
|
||||
/**
|
||||
* The title of the source page.
|
||||
*/
|
||||
title: string | null;
|
||||
|
||||
/**
|
||||
* When the site was last updated
|
||||
*/
|
||||
pageAge: string | null;
|
||||
|
||||
/**
|
||||
* Encrypted content that must be passed back in multi-turn conversations for citations
|
||||
*/
|
||||
encryptedContent: string;
|
||||
}>,
|
||||
{
|
||||
/**
|
||||
* Maximum number of web searches Claude can perform during the conversation.
|
||||
*/
|
||||
maxUses?: number;
|
||||
|
||||
/**
|
||||
* Optional list of domains that Claude is allowed to search.
|
||||
*/
|
||||
allowedDomains?: string[];
|
||||
|
||||
/**
|
||||
* Optional list of domains that Claude should avoid when searching.
|
||||
*/
|
||||
blockedDomains?: string[];
|
||||
|
||||
/**
|
||||
* Optional user location information to provide geographically relevant search results.
|
||||
*/
|
||||
userLocation?: {
|
||||
/**
|
||||
* The type of location (must be approximate)
|
||||
*/
|
||||
type: 'approximate';
|
||||
|
||||
/**
|
||||
* The city name
|
||||
*/
|
||||
city?: string;
|
||||
|
||||
/**
|
||||
* The region or state
|
||||
*/
|
||||
region?: string;
|
||||
|
||||
/**
|
||||
* The country
|
||||
*/
|
||||
country?: string;
|
||||
|
||||
/**
|
||||
* The IANA timezone ID.
|
||||
*/
|
||||
timezone?: string;
|
||||
};
|
||||
}
|
||||
>({
|
||||
id: 'anthropic.web_search_20250305',
|
||||
inputSchema: webSearch_20250305InputSchema,
|
||||
outputSchema: webSearch_20250305OutputSchema,
|
||||
supportsDeferredResults: true,
|
||||
});
|
||||
|
||||
export const webSearch_20250305 = (
|
||||
args: Parameters<typeof factory>[0] = {}, // default
|
||||
) => {
|
||||
return factory(args);
|
||||
};
|
||||
136
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/tool/web-search_20260209.ts
generated
vendored
Normal file
136
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/tool/web-search_20260209.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,136 @@
|
|||
import {
|
||||
createProviderToolFactoryWithOutputSchema,
|
||||
lazySchema,
|
||||
zodSchema,
|
||||
} from '@ai-sdk/provider-utils';
|
||||
import { z } from 'zod/v4';
|
||||
|
||||
export const webSearch_20260209ArgsSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.object({
|
||||
maxUses: z.number().optional(),
|
||||
allowedDomains: z.array(z.string()).optional(),
|
||||
blockedDomains: z.array(z.string()).optional(),
|
||||
userLocation: z
|
||||
.object({
|
||||
type: z.literal('approximate'),
|
||||
city: z.string().optional(),
|
||||
region: z.string().optional(),
|
||||
country: z.string().optional(),
|
||||
timezone: z.string().optional(),
|
||||
})
|
||||
.optional(),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
export const webSearch_20260209OutputSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.array(
|
||||
z.object({
|
||||
url: z.string(),
|
||||
title: z.string().nullable(),
|
||||
pageAge: z.string().nullable(),
|
||||
encryptedContent: z.string(),
|
||||
type: z.literal('web_search_result'),
|
||||
}),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
const webSearch_20260209InputSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.object({
|
||||
query: z.string(),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
const factory = createProviderToolFactoryWithOutputSchema<
|
||||
{
|
||||
/**
|
||||
* The search query to execute.
|
||||
*/
|
||||
query: string;
|
||||
},
|
||||
Array<{
|
||||
type: 'web_search_result';
|
||||
|
||||
/**
|
||||
* The URL of the source page.
|
||||
*/
|
||||
url: string;
|
||||
|
||||
/**
|
||||
* The title of the source page.
|
||||
*/
|
||||
title: string | null;
|
||||
|
||||
/**
|
||||
* When the site was last updated
|
||||
*/
|
||||
pageAge: string | null;
|
||||
|
||||
/**
|
||||
* Encrypted content that must be passed back in multi-turn conversations for citations
|
||||
*/
|
||||
encryptedContent: string;
|
||||
}>,
|
||||
{
|
||||
/**
|
||||
* Maximum number of web searches Claude can perform during the conversation.
|
||||
*/
|
||||
maxUses?: number;
|
||||
|
||||
/**
|
||||
* Optional list of domains that Claude is allowed to search.
|
||||
*/
|
||||
allowedDomains?: string[];
|
||||
|
||||
/**
|
||||
* Optional list of domains that Claude should avoid when searching.
|
||||
*/
|
||||
blockedDomains?: string[];
|
||||
|
||||
/**
|
||||
* Optional user location information to provide geographically relevant search results.
|
||||
*/
|
||||
userLocation?: {
|
||||
/**
|
||||
* The type of location (must be approximate)
|
||||
*/
|
||||
type: 'approximate';
|
||||
|
||||
/**
|
||||
* The city name
|
||||
*/
|
||||
city?: string;
|
||||
|
||||
/**
|
||||
* The region or state
|
||||
*/
|
||||
region?: string;
|
||||
|
||||
/**
|
||||
* The country
|
||||
*/
|
||||
country?: string;
|
||||
|
||||
/**
|
||||
* The IANA timezone ID.
|
||||
*/
|
||||
timezone?: string;
|
||||
};
|
||||
}
|
||||
>({
|
||||
id: 'anthropic.web_search_20260209',
|
||||
inputSchema: webSearch_20260209InputSchema,
|
||||
outputSchema: webSearch_20260209OutputSchema,
|
||||
supportsDeferredResults: true,
|
||||
});
|
||||
|
||||
export const webSearch_20260209 = (
|
||||
args: Parameters<typeof factory>[0] = {}, // default
|
||||
) => {
|
||||
return factory(args);
|
||||
};
|
||||
6
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/version.ts
generated
vendored
Normal file
6
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/anthropic/src/version.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
// Version string of this package injected at build time.
|
||||
declare const __PACKAGE_VERSION__: string | undefined;
|
||||
export const VERSION: string =
|
||||
typeof __PACKAGE_VERSION__ !== 'undefined'
|
||||
? __PACKAGE_VERSION__
|
||||
: '0.0.0-test';
|
||||
1718
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/CHANGELOG.md
generated
vendored
Normal file
1718
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/CHANGELOG.md
generated
vendored
Normal file
File diff suppressed because it is too large
Load diff
13
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/LICENSE
generated
vendored
Normal file
13
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/LICENSE
generated
vendored
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
Copyright 2023 Vercel, Inc.
|
||||
|
||||
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.
|
||||
44
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/README.md
generated
vendored
Normal file
44
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/README.md
generated
vendored
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
# AI SDK - Gateway Provider
|
||||
|
||||
The Gateway provider for the [AI SDK](https://ai-sdk.dev/docs) allows the use of a wide variety of AI models and providers.
|
||||
|
||||
## Setup
|
||||
|
||||
The Gateway provider is available in the `@ai-sdk/gateway` module. You can install it with
|
||||
|
||||
```bash
|
||||
npm i @ai-sdk/gateway
|
||||
```
|
||||
|
||||
## Skill for Coding Agents
|
||||
|
||||
If you use coding agents such as Claude Code or Cursor, we highly recommend adding the AI SDK skill to your repository:
|
||||
|
||||
```shell
|
||||
npx skills add vercel/ai
|
||||
```
|
||||
|
||||
## Provider Instance
|
||||
|
||||
You can import the default provider instance `gateway` from `@ai-sdk/gateway`:
|
||||
|
||||
```ts
|
||||
import { gateway } from '@ai-sdk/gateway';
|
||||
```
|
||||
|
||||
## Example
|
||||
|
||||
```ts
|
||||
import { gateway } from '@ai-sdk/gateway';
|
||||
import { generateText } from 'ai';
|
||||
|
||||
const { text } = await generateText({
|
||||
model: gateway('xai/grok-3-beta'),
|
||||
prompt:
|
||||
'Tell me about the history of the San Francisco Mission-style burrito.',
|
||||
});
|
||||
```
|
||||
|
||||
## Documentation
|
||||
|
||||
Please check out the [AI SDK documentation](https://ai-sdk.dev/docs) for more information.
|
||||
742
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/docs/00-ai-gateway.mdx
generated
vendored
Normal file
742
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/docs/00-ai-gateway.mdx
generated
vendored
Normal file
|
|
@ -0,0 +1,742 @@
|
|||
---
|
||||
title: AI Gateway
|
||||
description: Learn how to use the AI Gateway provider with the AI SDK.
|
||||
---
|
||||
|
||||
# AI Gateway Provider
|
||||
|
||||
The [AI Gateway](https://vercel.com/docs/ai-gateway) provider connects you to models from multiple AI providers through a single interface. Instead of integrating with each provider separately, you can access OpenAI, Anthropic, Google, Meta, xAI, and other providers and their models.
|
||||
|
||||
## Features
|
||||
|
||||
- Access models from multiple providers without having to install additional provider modules/dependencies
|
||||
- Use the same code structure across different AI providers
|
||||
- Switch between models and providers easily
|
||||
- Automatic authentication when deployed on Vercel
|
||||
- View pricing information across providers
|
||||
- Observability for AI model usage through the Vercel dashboard
|
||||
|
||||
## Setup
|
||||
|
||||
The Vercel AI Gateway provider is part of the AI SDK.
|
||||
|
||||
## Basic Usage
|
||||
|
||||
For most use cases, you can use the AI Gateway directly with a model string:
|
||||
|
||||
```ts
|
||||
// use plain model string with global provider
|
||||
import { generateText } from 'ai';
|
||||
|
||||
const { text } = await generateText({
|
||||
model: 'openai/gpt-5',
|
||||
prompt: 'Hello world',
|
||||
});
|
||||
```
|
||||
|
||||
```ts
|
||||
// use provider instance (requires version 5.0.36 or later)
|
||||
import { generateText, gateway } from 'ai';
|
||||
|
||||
const { text } = await generateText({
|
||||
model: gateway('openai/gpt-5'),
|
||||
prompt: 'Hello world',
|
||||
});
|
||||
```
|
||||
|
||||
The AI SDK automatically uses the AI Gateway when you pass a model string in the `creator/model-name` format.
|
||||
|
||||
## Provider Instance
|
||||
|
||||
<Note>
|
||||
The `gateway` provider instance is available from the `ai` package in version
|
||||
5.0.36 and later.
|
||||
</Note>
|
||||
|
||||
You can also import the default provider instance `gateway` from `ai`:
|
||||
|
||||
```ts
|
||||
import { gateway } from 'ai';
|
||||
```
|
||||
|
||||
You may want to create a custom provider instance when you need to:
|
||||
|
||||
- Set custom configuration options (API key, base URL, headers)
|
||||
- Use the provider in a [provider registry](/docs/ai-sdk-core/provider-management)
|
||||
- Wrap the provider with [middleware](/docs/ai-sdk-core/middleware)
|
||||
- Use different settings for different parts of your application
|
||||
|
||||
To create a custom provider instance, import `createGateway` from `ai`:
|
||||
|
||||
```ts
|
||||
import { createGateway } from 'ai';
|
||||
|
||||
const gateway = createGateway({
|
||||
apiKey: process.env.AI_GATEWAY_API_KEY ?? '',
|
||||
});
|
||||
```
|
||||
|
||||
You can use the following optional settings to customize the AI Gateway provider instance:
|
||||
|
||||
- **baseURL** _string_
|
||||
|
||||
Use a different URL prefix for API calls. The default prefix is `https://ai-gateway.vercel.sh/v3/ai`.
|
||||
|
||||
- **apiKey** _string_
|
||||
|
||||
API key that is being sent using the `Authorization` header. It defaults to
|
||||
the `AI_GATEWAY_API_KEY` environment variable.
|
||||
|
||||
- **headers** _Record<string,string>_
|
||||
|
||||
Custom headers to include in the requests.
|
||||
|
||||
- **fetch** _(input: RequestInfo, init?: RequestInit) => Promise<Response>_
|
||||
|
||||
Custom [fetch](https://developer.mozilla.org/en-US/docs/Web/API/fetch) implementation.
|
||||
Defaults to the global `fetch` function.
|
||||
You can use it as a middleware to intercept requests,
|
||||
or to provide a custom fetch implementation for e.g. testing.
|
||||
|
||||
- **metadataCacheRefreshMillis** _number_
|
||||
|
||||
How frequently to refresh the metadata cache in milliseconds. Defaults to 5 minutes (300,000ms).
|
||||
|
||||
## Authentication
|
||||
|
||||
The Gateway provider supports two authentication methods:
|
||||
|
||||
### API Key Authentication
|
||||
|
||||
Set your API key via environment variable:
|
||||
|
||||
```bash
|
||||
AI_GATEWAY_API_KEY=your_api_key_here
|
||||
```
|
||||
|
||||
Or pass it directly to the provider:
|
||||
|
||||
```ts
|
||||
import { createGateway } from 'ai';
|
||||
|
||||
const gateway = createGateway({
|
||||
apiKey: 'your_api_key_here',
|
||||
});
|
||||
```
|
||||
|
||||
### OIDC Authentication (Vercel Deployments)
|
||||
|
||||
When deployed to Vercel, the AI Gateway provider supports authenticating using [OIDC (OpenID Connect)
|
||||
tokens](https://vercel.com/docs/oidc) without API Keys.
|
||||
|
||||
#### How OIDC Authentication Works
|
||||
|
||||
1. **In Production/Preview Deployments**:
|
||||
|
||||
- OIDC authentication is automatically handled
|
||||
- No manual configuration needed
|
||||
- Tokens are automatically obtained and refreshed
|
||||
|
||||
2. **In Local Development**:
|
||||
- First, install and authenticate with the [Vercel CLI](https://vercel.com/docs/cli)
|
||||
- Run `vercel env pull` to download your project's OIDC token locally
|
||||
- For automatic token management:
|
||||
- Use `vercel dev` to start your development server - this will handle token refreshing automatically
|
||||
- For manual token management:
|
||||
- If not using `vercel dev`, note that OIDC tokens expire after 12 hours
|
||||
- You'll need to run `vercel env pull` again to refresh the token before it expires
|
||||
|
||||
<Note>
|
||||
If an API Key is present (either passed directly or via environment), it will
|
||||
always be used, even if invalid.
|
||||
</Note>
|
||||
|
||||
Read more about using OIDC tokens in the [Vercel AI Gateway docs](https://vercel.com/docs/ai-gateway#using-the-ai-gateway-with-a-vercel-oidc-token).
|
||||
|
||||
## Bring Your Own Key (BYOK)
|
||||
|
||||
You can connect your own provider credentials to use with Vercel AI Gateway. This lets you use your existing provider accounts and access private resources.
|
||||
|
||||
To set up BYOK, add your provider credentials in your Vercel team's AI Gateway settings. Once configured, AI Gateway automatically uses your credentials. No code changes are needed.
|
||||
|
||||
Learn more in the [BYOK documentation](https://vercel.com/docs/ai-gateway/byok).
|
||||
|
||||
## Language Models
|
||||
|
||||
You can create language models using a provider instance. The first argument is the model ID in the format `creator/model-name`:
|
||||
|
||||
```ts
|
||||
import { generateText } from 'ai';
|
||||
|
||||
const { text } = await generateText({
|
||||
model: 'openai/gpt-5',
|
||||
prompt: 'Explain quantum computing in simple terms',
|
||||
});
|
||||
```
|
||||
|
||||
AI Gateway language models can also be used in the `streamText` function and support structured data generation with [`Output`](/docs/reference/ai-sdk-core/output) (see [AI SDK Core](/docs/ai-sdk-core)).
|
||||
|
||||
## Available Models
|
||||
|
||||
The AI Gateway supports models from OpenAI, Anthropic, Google, Meta, xAI, Mistral, DeepSeek, Amazon Bedrock, Cohere, Perplexity, Alibaba, and other providers.
|
||||
|
||||
For the complete list of available models, see the [AI Gateway documentation](https://vercel.com/docs/ai-gateway).
|
||||
|
||||
## Dynamic Model Discovery
|
||||
|
||||
You can discover available models programmatically:
|
||||
|
||||
```ts
|
||||
import { gateway, generateText } from 'ai';
|
||||
|
||||
const availableModels = await gateway.getAvailableModels();
|
||||
|
||||
// List all available models
|
||||
availableModels.models.forEach(model => {
|
||||
console.log(`${model.id}: ${model.name}`);
|
||||
if (model.description) {
|
||||
console.log(` Description: ${model.description}`);
|
||||
}
|
||||
if (model.pricing) {
|
||||
console.log(` Input: $${model.pricing.input}/token`);
|
||||
console.log(` Output: $${model.pricing.output}/token`);
|
||||
if (model.pricing.cachedInputTokens) {
|
||||
console.log(
|
||||
` Cached input (read): $${model.pricing.cachedInputTokens}/token`,
|
||||
);
|
||||
}
|
||||
if (model.pricing.cacheCreationInputTokens) {
|
||||
console.log(
|
||||
` Cache creation (write): $${model.pricing.cacheCreationInputTokens}/token`,
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Use any discovered model with plain string
|
||||
const { text } = await generateText({
|
||||
model: availableModels.models[0].id, // e.g., 'openai/gpt-4o'
|
||||
prompt: 'Hello world',
|
||||
});
|
||||
```
|
||||
|
||||
## Credit Usage
|
||||
|
||||
You can check your team's current credit balance and usage:
|
||||
|
||||
```ts
|
||||
import { gateway } from 'ai';
|
||||
|
||||
const credits = await gateway.getCredits();
|
||||
|
||||
console.log(`Team balance: ${credits.balance} credits`);
|
||||
console.log(`Team total used: ${credits.total_used} credits`);
|
||||
```
|
||||
|
||||
The `getCredits()` method returns your team's credit information based on the authenticated API key or OIDC token:
|
||||
|
||||
- **balance** _number_ - Your team's current available credit balance
|
||||
- **total_used** _number_ - Total credits consumed by your team
|
||||
|
||||
## Examples
|
||||
|
||||
### Basic Text Generation
|
||||
|
||||
```ts
|
||||
import { generateText } from 'ai';
|
||||
|
||||
const { text } = await generateText({
|
||||
model: 'anthropic/claude-sonnet-4',
|
||||
prompt: 'Write a haiku about programming',
|
||||
});
|
||||
|
||||
console.log(text);
|
||||
```
|
||||
|
||||
### Streaming
|
||||
|
||||
```ts
|
||||
import { streamText } from 'ai';
|
||||
|
||||
const { textStream } = await streamText({
|
||||
model: 'openai/gpt-5',
|
||||
prompt: 'Explain the benefits of serverless architecture',
|
||||
});
|
||||
|
||||
for await (const textPart of textStream) {
|
||||
process.stdout.write(textPart);
|
||||
}
|
||||
```
|
||||
|
||||
### Tool Usage
|
||||
|
||||
```ts
|
||||
import { generateText, tool } from 'ai';
|
||||
import { z } from 'zod';
|
||||
|
||||
const { text } = await generateText({
|
||||
model: 'xai/grok-4',
|
||||
prompt: 'What is the weather like in San Francisco?',
|
||||
tools: {
|
||||
getWeather: tool({
|
||||
description: 'Get the current weather for a location',
|
||||
parameters: z.object({
|
||||
location: z.string().describe('The location to get weather for'),
|
||||
}),
|
||||
execute: async ({ location }) => {
|
||||
// Your weather API call here
|
||||
return `It's sunny in ${location}`;
|
||||
},
|
||||
}),
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
### Provider-Executed Tools
|
||||
|
||||
Some providers offer tools that are executed by the provider itself, such as [OpenAI's web search tool](/providers/ai-sdk-providers/openai#web-search-tool). To use these tools through AI Gateway, import the provider to access the tool definitions:
|
||||
|
||||
```ts
|
||||
import { generateText, stepCountIs } from 'ai';
|
||||
import { openai } from '@ai-sdk/openai';
|
||||
|
||||
const result = await generateText({
|
||||
model: 'openai/gpt-5-mini',
|
||||
prompt: 'What is the Vercel AI Gateway?',
|
||||
stopWhen: stepCountIs(10),
|
||||
tools: {
|
||||
web_search: openai.tools.webSearch({}),
|
||||
},
|
||||
});
|
||||
|
||||
console.dir(result.text);
|
||||
```
|
||||
|
||||
<Note>
|
||||
Some provider-executed tools require account-specific configuration (such as
|
||||
Claude Agent Skills) and may not work through AI Gateway. To use these tools,
|
||||
you must bring your own key (BYOK) directly to the provider.
|
||||
</Note>
|
||||
|
||||
### Gateway Tools
|
||||
|
||||
The AI Gateway provider includes built-in tools that are executed by the gateway itself. These tools can be used with any model through the gateway.
|
||||
|
||||
#### Perplexity Search
|
||||
|
||||
The Perplexity Search tool enables models to search the web using [Perplexity's search API](https://docs.perplexity.ai/guides/search-quickstart). This tool is executed by the AI Gateway and returns web search results that the model can use to provide up-to-date information.
|
||||
|
||||
```ts
|
||||
import { gateway, generateText } from 'ai';
|
||||
|
||||
const result = await generateText({
|
||||
model: 'openai/gpt-5-nano',
|
||||
prompt: 'Search for news about AI regulations in January 2025.',
|
||||
tools: {
|
||||
perplexity_search: gateway.tools.perplexitySearch(),
|
||||
},
|
||||
});
|
||||
|
||||
console.log(result.text);
|
||||
console.log('Tool calls:', JSON.stringify(result.toolCalls, null, 2));
|
||||
console.log('Tool results:', JSON.stringify(result.toolResults, null, 2));
|
||||
```
|
||||
|
||||
You can also configure the search with optional parameters:
|
||||
|
||||
```ts
|
||||
import { gateway, generateText } from 'ai';
|
||||
|
||||
const result = await generateText({
|
||||
model: 'openai/gpt-5-nano',
|
||||
prompt:
|
||||
'Search for news about AI regulations from the first week of January 2025.',
|
||||
tools: {
|
||||
perplexity_search: gateway.tools.perplexitySearch({
|
||||
maxResults: 5,
|
||||
searchLanguageFilter: ['en'],
|
||||
country: 'US',
|
||||
searchDomainFilter: ['reuters.com', 'bbc.com', 'nytimes.com'],
|
||||
}),
|
||||
},
|
||||
});
|
||||
|
||||
console.log(result.text);
|
||||
console.log('Tool calls:', JSON.stringify(result.toolCalls, null, 2));
|
||||
console.log('Tool results:', JSON.stringify(result.toolResults, null, 2));
|
||||
```
|
||||
|
||||
The Perplexity Search tool supports the following optional configuration options:
|
||||
|
||||
- **maxResults** _number_
|
||||
|
||||
The maximum number of search results to return (1-20, default: 10).
|
||||
|
||||
- **maxTokensPerPage** _number_
|
||||
|
||||
The maximum number of tokens to extract per search result page (256-2048, default: 2048).
|
||||
|
||||
- **maxTokens** _number_
|
||||
|
||||
The maximum total tokens across all search results (default: 25000, max: 1000000).
|
||||
|
||||
- **searchLanguageFilter** _string[]_
|
||||
|
||||
Filter search results by language using ISO 639-1 language codes (e.g., `['en']` for English, `['en', 'es']` for English and Spanish).
|
||||
|
||||
- **country** _string_
|
||||
|
||||
Filter search results by country using ISO 3166-1 alpha-2 country codes (e.g., `'US'` for United States, `'GB'` for United Kingdom).
|
||||
|
||||
- **searchDomainFilter** _string[]_
|
||||
|
||||
Limit search results to specific domains (e.g., `['reuters.com', 'bbc.com']`). This is useful for restricting results to trusted sources.
|
||||
|
||||
- **searchRecencyFilter** _'day' | 'week' | 'month' | 'year'_
|
||||
|
||||
Filter search results by relative time period. Useful for always getting recent results (e.g., 'week' for results from the last week).
|
||||
|
||||
The tool works with both `generateText` and `streamText`:
|
||||
|
||||
```ts
|
||||
import { gateway, streamText } from 'ai';
|
||||
|
||||
const result = streamText({
|
||||
model: 'openai/gpt-5-nano',
|
||||
prompt: 'Search for the latest news about AI regulations.',
|
||||
tools: {
|
||||
perplexity_search: gateway.tools.perplexitySearch(),
|
||||
},
|
||||
});
|
||||
|
||||
for await (const part of result.fullStream) {
|
||||
switch (part.type) {
|
||||
case 'text-delta':
|
||||
process.stdout.write(part.text);
|
||||
break;
|
||||
case 'tool-call':
|
||||
console.log('\nTool call:', JSON.stringify(part, null, 2));
|
||||
break;
|
||||
case 'tool-result':
|
||||
console.log('\nTool result:', JSON.stringify(part, null, 2));
|
||||
break;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### Parallel Search
|
||||
|
||||
The Parallel Search tool enables models to search the web using [Parallel AI's Search API](https://docs.parallel.ai/api-reference/search-beta/search). This tool is optimized for LLM consumption, returning relevant excerpts from web pages that can replace multiple keyword searches with a single call.
|
||||
|
||||
```ts
|
||||
import { gateway, generateText } from 'ai';
|
||||
|
||||
const result = await generateText({
|
||||
model: 'openai/gpt-5-nano',
|
||||
prompt: 'Research the latest developments in quantum computing.',
|
||||
tools: {
|
||||
parallel_search: gateway.tools.parallelSearch(),
|
||||
},
|
||||
});
|
||||
|
||||
console.log(result.text);
|
||||
console.log('Tool calls:', JSON.stringify(result.toolCalls, null, 2));
|
||||
console.log('Tool results:', JSON.stringify(result.toolResults, null, 2));
|
||||
```
|
||||
|
||||
You can also configure the search with optional parameters:
|
||||
|
||||
```ts
|
||||
import { gateway, generateText } from 'ai';
|
||||
|
||||
const result = await generateText({
|
||||
model: 'openai/gpt-5-nano',
|
||||
prompt: 'Find detailed information about TypeScript 5.0 features.',
|
||||
tools: {
|
||||
parallel_search: gateway.tools.parallelSearch({
|
||||
mode: 'agentic',
|
||||
maxResults: 5,
|
||||
sourcePolicy: {
|
||||
includeDomains: ['typescriptlang.org', 'github.com'],
|
||||
},
|
||||
excerpts: {
|
||||
maxCharsPerResult: 8000,
|
||||
},
|
||||
}),
|
||||
},
|
||||
});
|
||||
|
||||
console.log(result.text);
|
||||
console.log('Tool calls:', JSON.stringify(result.toolCalls, null, 2));
|
||||
console.log('Tool results:', JSON.stringify(result.toolResults, null, 2));
|
||||
```
|
||||
|
||||
The Parallel Search tool supports the following optional configuration options:
|
||||
|
||||
- **mode** _'one-shot' | 'agentic'_
|
||||
|
||||
Mode preset for different use cases:
|
||||
|
||||
- `'one-shot'` - Comprehensive results with longer excerpts for single-response answers (default)
|
||||
- `'agentic'` - Concise, token-efficient results optimized for multi-step agentic workflows
|
||||
|
||||
- **maxResults** _number_
|
||||
|
||||
Maximum number of results to return (1-20). Defaults to 10 if not specified.
|
||||
|
||||
- **sourcePolicy** _object_
|
||||
|
||||
Source policy for controlling which domains to include/exclude:
|
||||
|
||||
- `includeDomains` - List of domains to include in search results
|
||||
- `excludeDomains` - List of domains to exclude from search results
|
||||
- `afterDate` - Only include results published after this date (ISO 8601 format)
|
||||
|
||||
- **excerpts** _object_
|
||||
|
||||
Excerpt configuration for controlling result length:
|
||||
|
||||
- `maxCharsPerResult` - Maximum characters per result
|
||||
- `maxCharsTotal` - Maximum total characters across all results
|
||||
|
||||
- **fetchPolicy** _object_
|
||||
|
||||
Fetch policy for controlling content freshness:
|
||||
|
||||
- `maxAgeSeconds` - Maximum age in seconds for cached content (set to 0 for always fresh)
|
||||
|
||||
The tool works with both `generateText` and `streamText`:
|
||||
|
||||
```ts
|
||||
import { gateway, streamText } from 'ai';
|
||||
|
||||
const result = streamText({
|
||||
model: 'openai/gpt-5-nano',
|
||||
prompt: 'Research the latest AI safety guidelines.',
|
||||
tools: {
|
||||
parallel_search: gateway.tools.parallelSearch(),
|
||||
},
|
||||
});
|
||||
|
||||
for await (const part of result.fullStream) {
|
||||
switch (part.type) {
|
||||
case 'text-delta':
|
||||
process.stdout.write(part.text);
|
||||
break;
|
||||
case 'tool-call':
|
||||
console.log('\nTool call:', JSON.stringify(part, null, 2));
|
||||
break;
|
||||
case 'tool-result':
|
||||
console.log('\nTool result:', JSON.stringify(part, null, 2));
|
||||
break;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Usage Tracking with User and Tags
|
||||
|
||||
Track usage per end-user and categorize requests with tags:
|
||||
|
||||
```ts
|
||||
import type { GatewayLanguageModelOptions } from '@ai-sdk/gateway';
|
||||
import { generateText } from 'ai';
|
||||
|
||||
const { text } = await generateText({
|
||||
model: 'openai/gpt-5',
|
||||
prompt: 'Summarize this document...',
|
||||
providerOptions: {
|
||||
gateway: {
|
||||
user: 'user-abc-123', // Track usage for this specific end-user
|
||||
tags: ['document-summary', 'premium-feature'], // Categorize for reporting
|
||||
} satisfies GatewayLanguageModelOptions,
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
This allows you to:
|
||||
|
||||
- View usage and costs broken down by end-user in your analytics
|
||||
- Filter and analyze spending by feature or use case using tags
|
||||
- Track which users or features are driving the most AI usage
|
||||
|
||||
## Provider Options
|
||||
|
||||
The AI Gateway provider accepts provider options that control routing behavior and provider-specific configurations.
|
||||
|
||||
### Gateway Provider Options
|
||||
|
||||
You can use the `gateway` key in `providerOptions` to control how AI Gateway routes requests:
|
||||
|
||||
```ts
|
||||
import type { GatewayLanguageModelOptions } from '@ai-sdk/gateway';
|
||||
import { generateText } from 'ai';
|
||||
|
||||
const { text } = await generateText({
|
||||
model: 'anthropic/claude-sonnet-4',
|
||||
prompt: 'Explain quantum computing',
|
||||
providerOptions: {
|
||||
gateway: {
|
||||
order: ['vertex', 'anthropic'], // Try Vertex AI first, then Anthropic
|
||||
only: ['vertex', 'anthropic'], // Only use these providers
|
||||
} satisfies GatewayLanguageModelOptions,
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
The following gateway provider options are available:
|
||||
|
||||
- **order** _string[]_
|
||||
|
||||
Specifies the sequence of providers to attempt when routing requests. The gateway will try providers in the order specified. If a provider fails or is unavailable, it will move to the next provider in the list.
|
||||
|
||||
Example: `order: ['bedrock', 'anthropic']` will attempt Amazon Bedrock first, then fall back to Anthropic.
|
||||
|
||||
- **only** _string[]_
|
||||
|
||||
Restricts routing to only the specified providers. When set, the gateway will never route to providers not in this list, even if they would otherwise be available.
|
||||
|
||||
Example: `only: ['anthropic', 'vertex']` will only allow routing to Anthropic or Vertex AI.
|
||||
|
||||
- **models** _string[]_
|
||||
|
||||
Specifies fallback models to use when the primary model fails or is unavailable. The gateway will try the primary model first (specified in the `model` parameter), then try each model in this array in order until one succeeds.
|
||||
|
||||
Example: `models: ['openai/gpt-5-nano', 'gemini-2.0-flash']` will try the fallback models in order if the primary model fails.
|
||||
|
||||
- **user** _string_
|
||||
|
||||
Optional identifier for the end user on whose behalf the request is being made. This is used for spend tracking and attribution purposes, allowing you to track usage per end-user in your application.
|
||||
|
||||
Example: `user: 'user-123'` will associate this request with end-user ID "user-123" in usage reports.
|
||||
|
||||
- **tags** _string[]_
|
||||
|
||||
Optional array of tags for categorizing and filtering usage in reports. Useful for tracking spend by feature, prompt version, or any other dimension relevant to your application.
|
||||
|
||||
Example: `tags: ['chat', 'v2']` will tag this request with "chat" and "v2" for filtering in usage analytics.
|
||||
|
||||
- **byok** _Record<string, Array<Record<string, unknown>>>_
|
||||
|
||||
Request-scoped BYOK (Bring Your Own Key) credentials to use for this request. When provided, any cached BYOK credentials configured in the gateway system are not considered. Requests may still fall back to use system credentials if the provided credentials fail.
|
||||
|
||||
Each provider can have multiple credentials (tried in order). The structure is a record where keys are provider slugs and values are arrays of credential objects.
|
||||
|
||||
Examples:
|
||||
|
||||
- Single provider: `byok: { 'anthropic': [{ apiKey: 'sk-ant-...' }] }`
|
||||
- Multiple credentials: `byok: { 'vertex': [{ project: 'proj-1', googleCredentials: { privateKey: '...', clientEmail: '...' } }, { project: 'proj-2', googleCredentials: { privateKey: '...', clientEmail: '...' } }] }`
|
||||
- Multiple providers: `byok: { 'anthropic': [{ apiKey: '...' }], 'bedrock': [{ accessKeyId: '...', secretAccessKey: '...' }] }`
|
||||
|
||||
- **zeroDataRetention** _boolean_
|
||||
|
||||
Restricts routing requests to providers that have zero data retention policies.
|
||||
|
||||
- **providerTimeouts** _object_
|
||||
|
||||
Per-provider timeouts for BYOK credentials in milliseconds. Controls how long to wait for a provider to start responding before falling back to the next available provider.
|
||||
|
||||
Example: `providerTimeouts: { byok: { openai: 5000, anthropic: 2000 } }`
|
||||
|
||||
For full details, see [Provider Timeouts](https://vercel.com/docs/ai-gateway/models-and-providers/provider-timeouts).
|
||||
|
||||
You can combine these options to have fine-grained control over routing and tracking:
|
||||
|
||||
```ts
|
||||
import type { GatewayLanguageModelOptions } from '@ai-sdk/gateway';
|
||||
import { generateText } from 'ai';
|
||||
|
||||
const { text } = await generateText({
|
||||
model: 'anthropic/claude-sonnet-4',
|
||||
prompt: 'Write a haiku about programming',
|
||||
providerOptions: {
|
||||
gateway: {
|
||||
order: ['vertex'], // Prefer Vertex AI
|
||||
only: ['anthropic', 'vertex'], // Only allow these providers
|
||||
} satisfies GatewayLanguageModelOptions,
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
#### Model Fallbacks Example
|
||||
|
||||
The `models` option enables automatic fallback to alternative models when the primary model fails:
|
||||
|
||||
```ts
|
||||
import type { GatewayLanguageModelOptions } from '@ai-sdk/gateway';
|
||||
import { generateText } from 'ai';
|
||||
|
||||
const { text } = await generateText({
|
||||
model: 'openai/gpt-4o', // Primary model
|
||||
prompt: 'Write a TypeScript haiku',
|
||||
providerOptions: {
|
||||
gateway: {
|
||||
models: ['openai/gpt-5-nano', 'gemini-2.0-flash'], // Fallback models
|
||||
} satisfies GatewayLanguageModelOptions,
|
||||
},
|
||||
});
|
||||
|
||||
// This will:
|
||||
// 1. Try openai/gpt-4o first
|
||||
// 2. If it fails, try openai/gpt-5-nano
|
||||
// 3. If that fails, try gemini-2.0-flash
|
||||
// 4. Return the result from the first model that succeeds
|
||||
```
|
||||
|
||||
#### Zero Data Retention Example
|
||||
|
||||
Set `zeroDataRetention` to true to ensure requests are only routed to providers
|
||||
that have zero data retention policies. When `zeroDataRetention` is `false` or not
|
||||
specified, there is no enforcement of restricting routing.
|
||||
|
||||
```ts
|
||||
import type { GatewayLanguageModelOptions } from '@ai-sdk/gateway';
|
||||
import { generateText } from 'ai';
|
||||
|
||||
const { text } = await generateText({
|
||||
model: 'anthropic/claude-sonnet-4.5',
|
||||
prompt: 'Analyze this sensitive document...',
|
||||
providerOptions: {
|
||||
gateway: {
|
||||
zeroDataRetention: true,
|
||||
} satisfies GatewayLanguageModelOptions,
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
### Provider-Specific Options
|
||||
|
||||
When using provider-specific options through AI Gateway, use the actual provider name (e.g. `anthropic`, `openai`, not `gateway`) as the key:
|
||||
|
||||
```ts
|
||||
import type { AnthropicLanguageModelOptions } from '@ai-sdk/anthropic';
|
||||
import type { GatewayLanguageModelOptions } from '@ai-sdk/gateway';
|
||||
import { generateText } from 'ai';
|
||||
|
||||
const { text } = await generateText({
|
||||
model: 'anthropic/claude-sonnet-4',
|
||||
prompt: 'Explain quantum computing',
|
||||
providerOptions: {
|
||||
gateway: {
|
||||
order: ['vertex', 'anthropic'],
|
||||
} satisfies GatewayLanguageModelOptions,
|
||||
anthropic: {
|
||||
thinking: { type: 'enabled', budgetTokens: 12000 },
|
||||
} satisfies AnthropicLanguageModelOptions,
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
This works with any provider supported by AI Gateway. Each provider has its own set of options - see the individual [provider documentation pages](/providers/ai-sdk-providers) for details on provider-specific options.
|
||||
|
||||
### Available Providers
|
||||
|
||||
AI Gateway supports routing to 20+ providers.
|
||||
|
||||
For a complete list of available providers and their slugs, see the [AI Gateway documentation](https://vercel.com/docs/ai-gateway/provider-options#available-providers).
|
||||
|
||||
## Model Capabilities
|
||||
|
||||
Model capabilities depend on the specific provider and model you're using. For detailed capability information, see:
|
||||
|
||||
- [AI Gateway provider options](https://vercel.com/docs/ai-gateway/provider-options#available-providers) for an overview of available providers
|
||||
- Individual [AI SDK provider pages](/providers/ai-sdk-providers) for specific model capabilities and features
|
||||
78
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/package.json
generated
vendored
Normal file
78
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/package.json
generated
vendored
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
{
|
||||
"name": "@ai-sdk/gateway",
|
||||
"private": false,
|
||||
"version": "3.0.80",
|
||||
"license": "Apache-2.0",
|
||||
"sideEffects": false,
|
||||
"main": "./dist/index.js",
|
||||
"module": "./dist/index.mjs",
|
||||
"types": "./dist/index.d.ts",
|
||||
"files": [
|
||||
"dist/**/*",
|
||||
"docs/**/*",
|
||||
"src",
|
||||
"!src/**/*.test.ts",
|
||||
"!src/**/*.test-d.ts",
|
||||
"!src/**/__snapshots__",
|
||||
"!src/**/__fixtures__",
|
||||
"CHANGELOG.md",
|
||||
"README.md"
|
||||
],
|
||||
"directories": {
|
||||
"doc": "./docs"
|
||||
},
|
||||
"exports": {
|
||||
"./package.json": "./package.json",
|
||||
".": {
|
||||
"types": "./dist/index.d.ts",
|
||||
"import": "./dist/index.mjs",
|
||||
"require": "./dist/index.js"
|
||||
}
|
||||
},
|
||||
"dependencies": {
|
||||
"@vercel/oidc": "3.1.0",
|
||||
"@ai-sdk/provider": "3.0.8",
|
||||
"@ai-sdk/provider-utils": "4.0.21"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "18.15.11",
|
||||
"tsup": "^8",
|
||||
"tsx": "4.19.2",
|
||||
"typescript": "5.8.3",
|
||||
"zod": "3.25.76",
|
||||
"@ai-sdk/test-server": "1.0.3",
|
||||
"@vercel/ai-tsconfig": "0.0.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"zod": "^3.25.76 || ^4.1.8"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
"homepage": "https://ai-sdk.dev/docs",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/vercel/ai.git"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/vercel/ai/issues"
|
||||
},
|
||||
"keywords": [
|
||||
"ai"
|
||||
],
|
||||
"scripts": {
|
||||
"build": "pnpm clean && tsup --tsconfig tsconfig.build.json",
|
||||
"build:watch": "pnpm clean && tsup --watch",
|
||||
"clean": "del-cli dist docs *.tsbuildinfo",
|
||||
"generate-model-settings": "tsx scripts/generate-model-settings.ts",
|
||||
"type-check": "tsc --build",
|
||||
"test": "pnpm test:node && pnpm test:edge",
|
||||
"test:update": "pnpm test:node -u",
|
||||
"test:watch": "vitest --config vitest.node.config.js",
|
||||
"test:edge": "vitest --config vitest.edge.config.js --run",
|
||||
"test:node": "vitest --config vitest.node.config.js --run"
|
||||
}
|
||||
}
|
||||
74
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/errors/as-gateway-error.ts
generated
vendored
Normal file
74
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/errors/as-gateway-error.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
import { APICallError } from '@ai-sdk/provider';
|
||||
import { extractApiCallResponse, GatewayError } from '.';
|
||||
import { createGatewayErrorFromResponse } from './create-gateway-error';
|
||||
import { GatewayTimeoutError } from './gateway-timeout-error';
|
||||
|
||||
/**
|
||||
* Checks if an error is a timeout error from undici.
|
||||
* Only checks undici-specific error codes to avoid false positives.
|
||||
*/
|
||||
function isTimeoutError(error: unknown): boolean {
|
||||
if (!(error instanceof Error)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check for undici-specific timeout error codes
|
||||
const errorCode = (error as any).code;
|
||||
if (typeof errorCode === 'string') {
|
||||
const undiciTimeoutCodes = [
|
||||
'UND_ERR_HEADERS_TIMEOUT',
|
||||
'UND_ERR_BODY_TIMEOUT',
|
||||
'UND_ERR_CONNECT_TIMEOUT',
|
||||
];
|
||||
return undiciTimeoutCodes.includes(errorCode);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
export async function asGatewayError(
|
||||
error: unknown,
|
||||
authMethod?: 'api-key' | 'oidc',
|
||||
) {
|
||||
if (GatewayError.isInstance(error)) {
|
||||
return error;
|
||||
}
|
||||
|
||||
// Check if this is a timeout error (or has a timeout error in the cause chain)
|
||||
if (isTimeoutError(error)) {
|
||||
return GatewayTimeoutError.createTimeoutError({
|
||||
originalMessage: error instanceof Error ? error.message : 'Unknown error',
|
||||
cause: error,
|
||||
});
|
||||
}
|
||||
|
||||
// Check if this is an APICallError caused by a timeout
|
||||
if (APICallError.isInstance(error)) {
|
||||
// Check if the cause is a timeout error
|
||||
if (error.cause && isTimeoutError(error.cause)) {
|
||||
return GatewayTimeoutError.createTimeoutError({
|
||||
originalMessage: error.message,
|
||||
cause: error,
|
||||
});
|
||||
}
|
||||
|
||||
return await createGatewayErrorFromResponse({
|
||||
response: extractApiCallResponse(error),
|
||||
statusCode: error.statusCode ?? 500,
|
||||
defaultMessage: 'Gateway request failed',
|
||||
cause: error,
|
||||
authMethod,
|
||||
});
|
||||
}
|
||||
|
||||
return await createGatewayErrorFromResponse({
|
||||
response: {},
|
||||
statusCode: 500,
|
||||
defaultMessage:
|
||||
error instanceof Error
|
||||
? `Gateway request failed: ${error.message}`
|
||||
: 'Unknown Gateway error',
|
||||
cause: error,
|
||||
authMethod,
|
||||
});
|
||||
}
|
||||
132
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/errors/create-gateway-error.ts
generated
vendored
Normal file
132
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/errors/create-gateway-error.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,132 @@
|
|||
import { z } from 'zod/v4';
|
||||
import type { GatewayError } from './gateway-error';
|
||||
import { GatewayAuthenticationError } from './gateway-authentication-error';
|
||||
import { GatewayInvalidRequestError } from './gateway-invalid-request-error';
|
||||
import { GatewayRateLimitError } from './gateway-rate-limit-error';
|
||||
import {
|
||||
GatewayModelNotFoundError,
|
||||
modelNotFoundParamSchema,
|
||||
} from './gateway-model-not-found-error';
|
||||
import { GatewayInternalServerError } from './gateway-internal-server-error';
|
||||
import { GatewayResponseError } from './gateway-response-error';
|
||||
import {
|
||||
InferSchema,
|
||||
lazySchema,
|
||||
safeValidateTypes,
|
||||
validateTypes,
|
||||
zodSchema,
|
||||
} from '@ai-sdk/provider-utils';
|
||||
|
||||
export async function createGatewayErrorFromResponse({
|
||||
response,
|
||||
statusCode,
|
||||
defaultMessage = 'Gateway request failed',
|
||||
cause,
|
||||
authMethod,
|
||||
}: {
|
||||
response: unknown;
|
||||
statusCode: number;
|
||||
defaultMessage?: string;
|
||||
cause?: unknown;
|
||||
authMethod?: 'api-key' | 'oidc';
|
||||
}): Promise<GatewayError> {
|
||||
const parseResult = await safeValidateTypes({
|
||||
value: response,
|
||||
schema: gatewayErrorResponseSchema,
|
||||
});
|
||||
|
||||
if (!parseResult.success) {
|
||||
// Try to extract generationId even if validation failed
|
||||
const rawGenerationId =
|
||||
typeof response === 'object' &&
|
||||
response !== null &&
|
||||
'generationId' in response
|
||||
? (response as { generationId?: string }).generationId
|
||||
: undefined;
|
||||
|
||||
return new GatewayResponseError({
|
||||
message: `Invalid error response format: ${defaultMessage}`,
|
||||
statusCode,
|
||||
response,
|
||||
validationError: parseResult.error,
|
||||
cause,
|
||||
generationId: rawGenerationId,
|
||||
});
|
||||
}
|
||||
|
||||
const validatedResponse: GatewayErrorResponse = parseResult.value;
|
||||
const errorType = validatedResponse.error.type;
|
||||
const message = validatedResponse.error.message;
|
||||
const generationId = validatedResponse.generationId ?? undefined;
|
||||
|
||||
switch (errorType) {
|
||||
case 'authentication_error':
|
||||
return GatewayAuthenticationError.createContextualError({
|
||||
apiKeyProvided: authMethod === 'api-key',
|
||||
oidcTokenProvided: authMethod === 'oidc',
|
||||
statusCode,
|
||||
cause,
|
||||
generationId,
|
||||
});
|
||||
case 'invalid_request_error':
|
||||
return new GatewayInvalidRequestError({
|
||||
message,
|
||||
statusCode,
|
||||
cause,
|
||||
generationId,
|
||||
});
|
||||
case 'rate_limit_exceeded':
|
||||
return new GatewayRateLimitError({
|
||||
message,
|
||||
statusCode,
|
||||
cause,
|
||||
generationId,
|
||||
});
|
||||
case 'model_not_found': {
|
||||
const modelResult = await safeValidateTypes({
|
||||
value: validatedResponse.error.param,
|
||||
schema: modelNotFoundParamSchema,
|
||||
});
|
||||
|
||||
return new GatewayModelNotFoundError({
|
||||
message,
|
||||
statusCode,
|
||||
modelId: modelResult.success ? modelResult.value.modelId : undefined,
|
||||
cause,
|
||||
generationId,
|
||||
});
|
||||
}
|
||||
case 'internal_server_error':
|
||||
return new GatewayInternalServerError({
|
||||
message,
|
||||
statusCode,
|
||||
cause,
|
||||
generationId,
|
||||
});
|
||||
default:
|
||||
return new GatewayInternalServerError({
|
||||
message,
|
||||
statusCode,
|
||||
cause,
|
||||
generationId,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const gatewayErrorResponseSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.object({
|
||||
error: z.object({
|
||||
message: z.string(),
|
||||
type: z.string().nullish(),
|
||||
param: z.unknown().nullish(),
|
||||
code: z.union([z.string(), z.number()]).nullish(),
|
||||
}),
|
||||
generationId: z.string().nullish(),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
export type GatewayErrorResponse = InferSchema<
|
||||
typeof gatewayErrorResponseSchema
|
||||
>;
|
||||
15
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/errors/extract-api-call-response.ts
generated
vendored
Normal file
15
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/errors/extract-api-call-response.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
import type { APICallError } from '@ai-sdk/provider';
|
||||
|
||||
export function extractApiCallResponse(error: APICallError): unknown {
|
||||
if (error.data !== undefined) {
|
||||
return error.data;
|
||||
}
|
||||
if (error.responseBody != null) {
|
||||
try {
|
||||
return JSON.parse(error.responseBody);
|
||||
} catch {
|
||||
return error.responseBody;
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
84
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/errors/gateway-authentication-error.ts
generated
vendored
Normal file
84
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/errors/gateway-authentication-error.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
import { GatewayError } from './gateway-error';
|
||||
|
||||
const name = 'GatewayAuthenticationError';
|
||||
const marker = `vercel.ai.gateway.error.${name}`;
|
||||
const symbol = Symbol.for(marker);
|
||||
|
||||
/**
|
||||
* Authentication failed - invalid API key or OIDC token
|
||||
*/
|
||||
export class GatewayAuthenticationError extends GatewayError {
|
||||
private readonly [symbol] = true; // used in isInstance
|
||||
|
||||
readonly name = name;
|
||||
readonly type = 'authentication_error';
|
||||
|
||||
constructor({
|
||||
message = 'Authentication failed',
|
||||
statusCode = 401,
|
||||
cause,
|
||||
generationId,
|
||||
}: {
|
||||
message?: string;
|
||||
statusCode?: number;
|
||||
cause?: unknown;
|
||||
generationId?: string;
|
||||
} = {}) {
|
||||
super({ message, statusCode, cause, generationId });
|
||||
}
|
||||
|
||||
static isInstance(error: unknown): error is GatewayAuthenticationError {
|
||||
return GatewayError.hasMarker(error) && symbol in error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a contextual error message when authentication fails
|
||||
*/
|
||||
static createContextualError({
|
||||
apiKeyProvided,
|
||||
oidcTokenProvided,
|
||||
message = 'Authentication failed',
|
||||
statusCode = 401,
|
||||
cause,
|
||||
generationId,
|
||||
}: {
|
||||
apiKeyProvided: boolean;
|
||||
oidcTokenProvided: boolean;
|
||||
message?: string;
|
||||
statusCode?: number;
|
||||
cause?: unknown;
|
||||
generationId?: string;
|
||||
}): GatewayAuthenticationError {
|
||||
let contextualMessage: string;
|
||||
|
||||
if (apiKeyProvided) {
|
||||
contextualMessage = `AI Gateway authentication failed: Invalid API key.
|
||||
|
||||
Create a new API key: https://vercel.com/d?to=%2F%5Bteam%5D%2F%7E%2Fai%2Fapi-keys
|
||||
|
||||
Provide via 'apiKey' option or 'AI_GATEWAY_API_KEY' environment variable.`;
|
||||
} else if (oidcTokenProvided) {
|
||||
contextualMessage = `AI Gateway authentication failed: Invalid OIDC token.
|
||||
|
||||
Run 'npx vercel link' to link your project, then 'vc env pull' to fetch the token.
|
||||
|
||||
Alternatively, use an API key: https://vercel.com/d?to=%2F%5Bteam%5D%2F%7E%2Fai%2Fapi-keys`;
|
||||
} else {
|
||||
contextualMessage = `AI Gateway authentication failed: No authentication provided.
|
||||
|
||||
Option 1 - API key:
|
||||
Create an API key: https://vercel.com/d?to=%2F%5Bteam%5D%2F%7E%2Fai%2Fapi-keys
|
||||
Provide via 'apiKey' option or 'AI_GATEWAY_API_KEY' environment variable.
|
||||
|
||||
Option 2 - OIDC token:
|
||||
Run 'npx vercel link' to link your project, then 'vc env pull' to fetch the token.`;
|
||||
}
|
||||
|
||||
return new GatewayAuthenticationError({
|
||||
message: contextualMessage,
|
||||
statusCode,
|
||||
cause,
|
||||
generationId,
|
||||
});
|
||||
}
|
||||
}
|
||||
47
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/errors/gateway-error.ts
generated
vendored
Normal file
47
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/errors/gateway-error.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
const marker = 'vercel.ai.gateway.error';
|
||||
const symbol = Symbol.for(marker);
|
||||
|
||||
export abstract class GatewayError extends Error {
|
||||
private readonly [symbol] = true; // used in isInstance
|
||||
|
||||
abstract readonly name: string;
|
||||
abstract readonly type: string;
|
||||
readonly statusCode: number;
|
||||
readonly cause?: unknown;
|
||||
readonly generationId?: string;
|
||||
|
||||
constructor({
|
||||
message,
|
||||
statusCode = 500,
|
||||
cause,
|
||||
generationId,
|
||||
}: {
|
||||
message: string;
|
||||
statusCode?: number;
|
||||
cause?: unknown;
|
||||
generationId?: string;
|
||||
}) {
|
||||
super(generationId ? `${message} [${generationId}]` : message);
|
||||
this.statusCode = statusCode;
|
||||
this.cause = cause;
|
||||
this.generationId = generationId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the given error is a Gateway Error.
|
||||
* @param {unknown} error - The error to check.
|
||||
* @returns {boolean} True if the error is a Gateway Error, false otherwise.
|
||||
*/
|
||||
static isInstance(error: unknown): error is GatewayError {
|
||||
return GatewayError.hasMarker(error);
|
||||
}
|
||||
|
||||
static hasMarker(error: unknown): error is GatewayError {
|
||||
return (
|
||||
typeof error === 'object' &&
|
||||
error !== null &&
|
||||
symbol in error &&
|
||||
(error as any)[symbol] === true
|
||||
);
|
||||
}
|
||||
}
|
||||
33
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/errors/gateway-internal-server-error.ts
generated
vendored
Normal file
33
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/errors/gateway-internal-server-error.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import { GatewayError } from './gateway-error';
|
||||
|
||||
const name = 'GatewayInternalServerError';
|
||||
const marker = `vercel.ai.gateway.error.${name}`;
|
||||
const symbol = Symbol.for(marker);
|
||||
|
||||
/**
|
||||
* Internal server error from the Gateway
|
||||
*/
|
||||
export class GatewayInternalServerError extends GatewayError {
|
||||
private readonly [symbol] = true; // used in isInstance
|
||||
|
||||
readonly name = name;
|
||||
readonly type = 'internal_server_error';
|
||||
|
||||
constructor({
|
||||
message = 'Internal server error',
|
||||
statusCode = 500,
|
||||
cause,
|
||||
generationId,
|
||||
}: {
|
||||
message?: string;
|
||||
statusCode?: number;
|
||||
cause?: unknown;
|
||||
generationId?: string;
|
||||
} = {}) {
|
||||
super({ message, statusCode, cause, generationId });
|
||||
}
|
||||
|
||||
static isInstance(error: unknown): error is GatewayInternalServerError {
|
||||
return GatewayError.hasMarker(error) && symbol in error;
|
||||
}
|
||||
}
|
||||
33
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/errors/gateway-invalid-request-error.ts
generated
vendored
Normal file
33
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/errors/gateway-invalid-request-error.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import { GatewayError } from './gateway-error';
|
||||
|
||||
const name = 'GatewayInvalidRequestError';
|
||||
const marker = `vercel.ai.gateway.error.${name}`;
|
||||
const symbol = Symbol.for(marker);
|
||||
|
||||
/**
|
||||
* Invalid request - missing headers, malformed data, etc.
|
||||
*/
|
||||
export class GatewayInvalidRequestError extends GatewayError {
|
||||
private readonly [symbol] = true; // used in isInstance
|
||||
|
||||
readonly name = name;
|
||||
readonly type = 'invalid_request_error';
|
||||
|
||||
constructor({
|
||||
message = 'Invalid request',
|
||||
statusCode = 400,
|
||||
cause,
|
||||
generationId,
|
||||
}: {
|
||||
message?: string;
|
||||
statusCode?: number;
|
||||
cause?: unknown;
|
||||
generationId?: string;
|
||||
} = {}) {
|
||||
super({ message, statusCode, cause, generationId });
|
||||
}
|
||||
|
||||
static isInstance(error: unknown): error is GatewayInvalidRequestError {
|
||||
return GatewayError.hasMarker(error) && symbol in error;
|
||||
}
|
||||
}
|
||||
47
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/errors/gateway-model-not-found-error.ts
generated
vendored
Normal file
47
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/errors/gateway-model-not-found-error.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
import { z } from 'zod/v4';
|
||||
import { GatewayError } from './gateway-error';
|
||||
import { lazySchema, zodSchema } from '@ai-sdk/provider-utils';
|
||||
|
||||
const name = 'GatewayModelNotFoundError';
|
||||
const marker = `vercel.ai.gateway.error.${name}`;
|
||||
const symbol = Symbol.for(marker);
|
||||
|
||||
export const modelNotFoundParamSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.object({
|
||||
modelId: z.string(),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
/**
|
||||
* Model not found or not available
|
||||
*/
|
||||
export class GatewayModelNotFoundError extends GatewayError {
|
||||
private readonly [symbol] = true; // used in isInstance
|
||||
|
||||
readonly name = name;
|
||||
readonly type = 'model_not_found';
|
||||
readonly modelId?: string;
|
||||
|
||||
constructor({
|
||||
message = 'Model not found',
|
||||
statusCode = 404,
|
||||
modelId,
|
||||
cause,
|
||||
generationId,
|
||||
}: {
|
||||
message?: string;
|
||||
statusCode?: number;
|
||||
modelId?: string;
|
||||
cause?: unknown;
|
||||
generationId?: string;
|
||||
} = {}) {
|
||||
super({ message, statusCode, cause, generationId });
|
||||
this.modelId = modelId;
|
||||
}
|
||||
|
||||
static isInstance(error: unknown): error is GatewayModelNotFoundError {
|
||||
return GatewayError.hasMarker(error) && symbol in error;
|
||||
}
|
||||
}
|
||||
33
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/errors/gateway-rate-limit-error.ts
generated
vendored
Normal file
33
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/errors/gateway-rate-limit-error.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import { GatewayError } from './gateway-error';
|
||||
|
||||
const name = 'GatewayRateLimitError';
|
||||
const marker = `vercel.ai.gateway.error.${name}`;
|
||||
const symbol = Symbol.for(marker);
|
||||
|
||||
/**
|
||||
* Rate limit exceeded.
|
||||
*/
|
||||
export class GatewayRateLimitError extends GatewayError {
|
||||
private readonly [symbol] = true; // used in isInstance
|
||||
|
||||
readonly name = name;
|
||||
readonly type = 'rate_limit_exceeded';
|
||||
|
||||
constructor({
|
||||
message = 'Rate limit exceeded',
|
||||
statusCode = 429,
|
||||
cause,
|
||||
generationId,
|
||||
}: {
|
||||
message?: string;
|
||||
statusCode?: number;
|
||||
cause?: unknown;
|
||||
generationId?: string;
|
||||
} = {}) {
|
||||
super({ message, statusCode, cause, generationId });
|
||||
}
|
||||
|
||||
static isInstance(error: unknown): error is GatewayRateLimitError {
|
||||
return GatewayError.hasMarker(error) && symbol in error;
|
||||
}
|
||||
}
|
||||
42
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/errors/gateway-response-error.ts
generated
vendored
Normal file
42
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/errors/gateway-response-error.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
import { TypeValidationError } from '@ai-sdk/provider';
|
||||
import { GatewayError } from './gateway-error';
|
||||
|
||||
const name = 'GatewayResponseError';
|
||||
const marker = `vercel.ai.gateway.error.${name}`;
|
||||
const symbol = Symbol.for(marker);
|
||||
|
||||
/**
|
||||
* Gateway response parsing error
|
||||
*/
|
||||
export class GatewayResponseError extends GatewayError {
|
||||
private readonly [symbol] = true; // used in isInstance
|
||||
|
||||
readonly name = name;
|
||||
readonly type = 'response_error';
|
||||
readonly response?: unknown;
|
||||
readonly validationError?: TypeValidationError;
|
||||
|
||||
constructor({
|
||||
message = 'Invalid response from Gateway',
|
||||
statusCode = 502,
|
||||
response,
|
||||
validationError,
|
||||
cause,
|
||||
generationId,
|
||||
}: {
|
||||
message?: string;
|
||||
statusCode?: number;
|
||||
response?: unknown;
|
||||
validationError?: TypeValidationError;
|
||||
cause?: unknown;
|
||||
generationId?: string;
|
||||
} = {}) {
|
||||
super({ message, statusCode, cause, generationId });
|
||||
this.response = response;
|
||||
this.validationError = validationError;
|
||||
}
|
||||
|
||||
static isInstance(error: unknown): error is GatewayResponseError {
|
||||
return GatewayError.hasMarker(error) && symbol in error;
|
||||
}
|
||||
}
|
||||
59
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/errors/gateway-timeout-error.ts
generated
vendored
Normal file
59
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/errors/gateway-timeout-error.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
import { GatewayError } from './gateway-error';
|
||||
|
||||
const name = 'GatewayTimeoutError';
|
||||
const marker = `vercel.ai.gateway.error.${name}`;
|
||||
const symbol = Symbol.for(marker);
|
||||
|
||||
/**
|
||||
* Client request timed out before receiving a response.
|
||||
*/
|
||||
export class GatewayTimeoutError extends GatewayError {
|
||||
private readonly [symbol] = true; // used in isInstance
|
||||
|
||||
readonly name = name;
|
||||
readonly type = 'timeout_error';
|
||||
|
||||
constructor({
|
||||
message = 'Request timed out',
|
||||
statusCode = 408,
|
||||
cause,
|
||||
generationId,
|
||||
}: {
|
||||
message?: string;
|
||||
statusCode?: number;
|
||||
cause?: unknown;
|
||||
generationId?: string;
|
||||
} = {}) {
|
||||
super({ message, statusCode, cause, generationId });
|
||||
}
|
||||
|
||||
static isInstance(error: unknown): error is GatewayTimeoutError {
|
||||
return GatewayError.hasMarker(error) && symbol in error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a helpful timeout error message with troubleshooting guidance
|
||||
*/
|
||||
static createTimeoutError({
|
||||
originalMessage,
|
||||
statusCode = 408,
|
||||
cause,
|
||||
generationId,
|
||||
}: {
|
||||
originalMessage: string;
|
||||
statusCode?: number;
|
||||
cause?: unknown;
|
||||
generationId?: string;
|
||||
}): GatewayTimeoutError {
|
||||
const message = `Gateway request timed out: ${originalMessage}
|
||||
|
||||
This is a client-side timeout. To resolve this, increase your timeout configuration: https://vercel.com/docs/ai-gateway/capabilities/video-generation#extending-timeouts-for-node.js`;
|
||||
|
||||
return new GatewayTimeoutError({
|
||||
message,
|
||||
statusCode,
|
||||
cause,
|
||||
generationId,
|
||||
});
|
||||
}
|
||||
}
|
||||
17
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/errors/index.ts
generated
vendored
Normal file
17
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/errors/index.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
export { asGatewayError } from './as-gateway-error';
|
||||
export {
|
||||
createGatewayErrorFromResponse,
|
||||
type GatewayErrorResponse,
|
||||
} from './create-gateway-error';
|
||||
export { extractApiCallResponse } from './extract-api-call-response';
|
||||
export { GatewayError } from './gateway-error';
|
||||
export { GatewayAuthenticationError } from './gateway-authentication-error';
|
||||
export { GatewayInternalServerError } from './gateway-internal-server-error';
|
||||
export { GatewayInvalidRequestError } from './gateway-invalid-request-error';
|
||||
export {
|
||||
GatewayModelNotFoundError,
|
||||
modelNotFoundParamSchema,
|
||||
} from './gateway-model-not-found-error';
|
||||
export { GatewayRateLimitError } from './gateway-rate-limit-error';
|
||||
export { GatewayResponseError } from './gateway-response-error';
|
||||
export { GatewayTimeoutError } from './gateway-timeout-error';
|
||||
23
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/errors/parse-auth-method.ts
generated
vendored
Normal file
23
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/errors/parse-auth-method.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
import { z } from 'zod/v4';
|
||||
import {
|
||||
lazySchema,
|
||||
safeValidateTypes,
|
||||
zodSchema,
|
||||
} from '@ai-sdk/provider-utils';
|
||||
|
||||
export const GATEWAY_AUTH_METHOD_HEADER = 'ai-gateway-auth-method' as const;
|
||||
|
||||
export async function parseAuthMethod(
|
||||
headers: Record<string, string | undefined>,
|
||||
) {
|
||||
const result = await safeValidateTypes({
|
||||
value: headers[GATEWAY_AUTH_METHOD_HEADER],
|
||||
schema: gatewayAuthMethodSchema,
|
||||
});
|
||||
|
||||
return result.success ? result.value : undefined;
|
||||
}
|
||||
|
||||
const gatewayAuthMethodSchema = lazySchema(() =>
|
||||
zodSchema(z.union([z.literal('api-key'), z.literal('oidc')])),
|
||||
);
|
||||
7
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/gateway-config.ts
generated
vendored
Normal file
7
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/gateway-config.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
import type { FetchFunction, Resolvable } from '@ai-sdk/provider-utils';
|
||||
|
||||
export type GatewayConfig = {
|
||||
baseURL: string;
|
||||
headers: () => Resolvable<Record<string, string | undefined>>;
|
||||
fetch?: FetchFunction;
|
||||
};
|
||||
26
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/gateway-embedding-model-settings.ts
generated
vendored
Normal file
26
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/gateway-embedding-model-settings.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
export type GatewayEmbeddingModelId =
|
||||
| 'alibaba/qwen3-embedding-0.6b'
|
||||
| 'alibaba/qwen3-embedding-4b'
|
||||
| 'alibaba/qwen3-embedding-8b'
|
||||
| 'amazon/titan-embed-text-v2'
|
||||
| 'cohere/embed-v4.0'
|
||||
| 'google/gemini-embedding-001'
|
||||
| 'google/gemini-embedding-2'
|
||||
| 'google/text-embedding-005'
|
||||
| 'google/text-multilingual-embedding-002'
|
||||
| 'mistral/codestral-embed'
|
||||
| 'mistral/mistral-embed'
|
||||
| 'openai/text-embedding-3-large'
|
||||
| 'openai/text-embedding-3-small'
|
||||
| 'openai/text-embedding-ada-002'
|
||||
| 'voyage/voyage-3-large'
|
||||
| 'voyage/voyage-3.5'
|
||||
| 'voyage/voyage-3.5-lite'
|
||||
| 'voyage/voyage-4'
|
||||
| 'voyage/voyage-4-large'
|
||||
| 'voyage/voyage-4-lite'
|
||||
| 'voyage/voyage-code-2'
|
||||
| 'voyage/voyage-code-3'
|
||||
| 'voyage/voyage-finance-2'
|
||||
| 'voyage/voyage-law-2'
|
||||
| (string & {});
|
||||
109
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/gateway-embedding-model.ts
generated
vendored
Normal file
109
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/gateway-embedding-model.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
import type {
|
||||
EmbeddingModelV3,
|
||||
SharedV3ProviderMetadata,
|
||||
} from '@ai-sdk/provider';
|
||||
import {
|
||||
combineHeaders,
|
||||
createJsonErrorResponseHandler,
|
||||
createJsonResponseHandler,
|
||||
lazySchema,
|
||||
postJsonToApi,
|
||||
resolve,
|
||||
zodSchema,
|
||||
type Resolvable,
|
||||
} from '@ai-sdk/provider-utils';
|
||||
import { z } from 'zod/v4';
|
||||
import { asGatewayError } from './errors';
|
||||
import { parseAuthMethod } from './errors/parse-auth-method';
|
||||
import type { GatewayConfig } from './gateway-config';
|
||||
|
||||
export class GatewayEmbeddingModel implements EmbeddingModelV3 {
|
||||
readonly specificationVersion = 'v3';
|
||||
readonly maxEmbeddingsPerCall = 2048;
|
||||
readonly supportsParallelCalls = true;
|
||||
|
||||
constructor(
|
||||
readonly modelId: string,
|
||||
private readonly config: GatewayConfig & {
|
||||
provider: string;
|
||||
o11yHeaders: Resolvable<Record<string, string>>;
|
||||
},
|
||||
) {}
|
||||
|
||||
get provider(): string {
|
||||
return this.config.provider;
|
||||
}
|
||||
|
||||
async doEmbed({
|
||||
values,
|
||||
headers,
|
||||
abortSignal,
|
||||
providerOptions,
|
||||
}: Parameters<EmbeddingModelV3['doEmbed']>[0]): Promise<
|
||||
Awaited<ReturnType<EmbeddingModelV3['doEmbed']>>
|
||||
> {
|
||||
const resolvedHeaders = await resolve(this.config.headers());
|
||||
try {
|
||||
const {
|
||||
responseHeaders,
|
||||
value: responseBody,
|
||||
rawValue,
|
||||
} = await postJsonToApi({
|
||||
url: this.getUrl(),
|
||||
headers: combineHeaders(
|
||||
resolvedHeaders,
|
||||
headers ?? {},
|
||||
this.getModelConfigHeaders(),
|
||||
await resolve(this.config.o11yHeaders),
|
||||
),
|
||||
body: {
|
||||
values,
|
||||
...(providerOptions ? { providerOptions } : {}),
|
||||
},
|
||||
successfulResponseHandler: createJsonResponseHandler(
|
||||
gatewayEmbeddingResponseSchema,
|
||||
),
|
||||
failedResponseHandler: createJsonErrorResponseHandler({
|
||||
errorSchema: z.any(),
|
||||
errorToMessage: data => data,
|
||||
}),
|
||||
...(abortSignal && { abortSignal }),
|
||||
fetch: this.config.fetch,
|
||||
});
|
||||
|
||||
return {
|
||||
embeddings: responseBody.embeddings,
|
||||
usage: responseBody.usage ?? undefined,
|
||||
providerMetadata:
|
||||
responseBody.providerMetadata as unknown as SharedV3ProviderMetadata,
|
||||
response: { headers: responseHeaders, body: rawValue },
|
||||
warnings: [],
|
||||
};
|
||||
} catch (error) {
|
||||
throw await asGatewayError(error, await parseAuthMethod(resolvedHeaders));
|
||||
}
|
||||
}
|
||||
|
||||
private getUrl() {
|
||||
return `${this.config.baseURL}/embedding-model`;
|
||||
}
|
||||
|
||||
private getModelConfigHeaders() {
|
||||
return {
|
||||
'ai-embedding-model-specification-version': '3',
|
||||
'ai-model-id': this.modelId,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const gatewayEmbeddingResponseSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.object({
|
||||
embeddings: z.array(z.array(z.number())),
|
||||
usage: z.object({ tokens: z.number() }).nullish(),
|
||||
providerMetadata: z
|
||||
.record(z.string(), z.record(z.string(), z.unknown()))
|
||||
.optional(),
|
||||
}),
|
||||
),
|
||||
);
|
||||
129
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/gateway-fetch-metadata.ts
generated
vendored
Normal file
129
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/gateway-fetch-metadata.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
import {
|
||||
createJsonErrorResponseHandler,
|
||||
createJsonResponseHandler,
|
||||
getFromApi,
|
||||
lazySchema,
|
||||
resolve,
|
||||
zodSchema,
|
||||
} from '@ai-sdk/provider-utils';
|
||||
import { z } from 'zod/v4';
|
||||
import { asGatewayError } from './errors';
|
||||
import type { GatewayConfig } from './gateway-config';
|
||||
import type { GatewayLanguageModelEntry } from './gateway-model-entry';
|
||||
|
||||
type GatewayFetchMetadataConfig = GatewayConfig;
|
||||
|
||||
export interface GatewayFetchMetadataResponse {
|
||||
models: GatewayLanguageModelEntry[];
|
||||
}
|
||||
|
||||
export interface GatewayCreditsResponse {
|
||||
/** The remaining gateway credit balance available for API usage */
|
||||
balance: string;
|
||||
/** The total amount of gateway credits that have been consumed */
|
||||
totalUsed: string;
|
||||
}
|
||||
|
||||
export class GatewayFetchMetadata {
|
||||
constructor(private readonly config: GatewayFetchMetadataConfig) {}
|
||||
|
||||
async getAvailableModels(): Promise<GatewayFetchMetadataResponse> {
|
||||
try {
|
||||
const { value } = await getFromApi({
|
||||
url: `${this.config.baseURL}/config`,
|
||||
headers: await resolve(this.config.headers()),
|
||||
successfulResponseHandler: createJsonResponseHandler(
|
||||
gatewayAvailableModelsResponseSchema,
|
||||
),
|
||||
failedResponseHandler: createJsonErrorResponseHandler({
|
||||
errorSchema: z.any(),
|
||||
errorToMessage: data => data,
|
||||
}),
|
||||
fetch: this.config.fetch,
|
||||
});
|
||||
|
||||
return value;
|
||||
} catch (error) {
|
||||
throw await asGatewayError(error);
|
||||
}
|
||||
}
|
||||
|
||||
async getCredits(): Promise<GatewayCreditsResponse> {
|
||||
try {
|
||||
const baseUrl = new URL(this.config.baseURL);
|
||||
|
||||
const { value } = await getFromApi({
|
||||
url: `${baseUrl.origin}/v1/credits`,
|
||||
headers: await resolve(this.config.headers()),
|
||||
successfulResponseHandler: createJsonResponseHandler(
|
||||
gatewayCreditsResponseSchema,
|
||||
),
|
||||
failedResponseHandler: createJsonErrorResponseHandler({
|
||||
errorSchema: z.any(),
|
||||
errorToMessage: data => data,
|
||||
}),
|
||||
fetch: this.config.fetch,
|
||||
});
|
||||
|
||||
return value;
|
||||
} catch (error) {
|
||||
throw await asGatewayError(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const gatewayAvailableModelsResponseSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.object({
|
||||
models: z.array(
|
||||
z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
description: z.string().nullish(),
|
||||
pricing: z
|
||||
.object({
|
||||
input: z.string(),
|
||||
output: z.string(),
|
||||
input_cache_read: z.string().nullish(),
|
||||
input_cache_write: z.string().nullish(),
|
||||
})
|
||||
.transform(
|
||||
({ input, output, input_cache_read, input_cache_write }) => ({
|
||||
input,
|
||||
output,
|
||||
...(input_cache_read
|
||||
? { cachedInputTokens: input_cache_read }
|
||||
: {}),
|
||||
...(input_cache_write
|
||||
? { cacheCreationInputTokens: input_cache_write }
|
||||
: {}),
|
||||
}),
|
||||
)
|
||||
.nullish(),
|
||||
specification: z.object({
|
||||
specificationVersion: z.literal('v3'),
|
||||
provider: z.string(),
|
||||
modelId: z.string(),
|
||||
}),
|
||||
modelType: z
|
||||
.enum(['embedding', 'image', 'language', 'video'])
|
||||
.nullish(),
|
||||
}),
|
||||
),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
const gatewayCreditsResponseSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z
|
||||
.object({
|
||||
balance: z.string(),
|
||||
total_used: z.string(),
|
||||
})
|
||||
.transform(({ balance, total_used }) => ({
|
||||
balance,
|
||||
totalUsed: total_used,
|
||||
})),
|
||||
),
|
||||
);
|
||||
25
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/gateway-image-model-settings.ts
generated
vendored
Normal file
25
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/gateway-image-model-settings.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
export type GatewayImageModelId =
|
||||
| 'bfl/flux-2-flex'
|
||||
| 'bfl/flux-2-klein-4b'
|
||||
| 'bfl/flux-2-klein-9b'
|
||||
| 'bfl/flux-2-max'
|
||||
| 'bfl/flux-2-pro'
|
||||
| 'bfl/flux-kontext-max'
|
||||
| 'bfl/flux-kontext-pro'
|
||||
| 'bfl/flux-pro-1.0-fill'
|
||||
| 'bfl/flux-pro-1.1'
|
||||
| 'bfl/flux-pro-1.1-ultra'
|
||||
| 'google/imagen-4.0-fast-generate-001'
|
||||
| 'google/imagen-4.0-generate-001'
|
||||
| 'google/imagen-4.0-ultra-generate-001'
|
||||
| 'openai/gpt-image-1'
|
||||
| 'openai/gpt-image-1-mini'
|
||||
| 'openai/gpt-image-1.5'
|
||||
| 'prodia/flux-fast-schnell'
|
||||
| 'recraft/recraft-v2'
|
||||
| 'recraft/recraft-v3'
|
||||
| 'recraft/recraft-v4'
|
||||
| 'recraft/recraft-v4-pro'
|
||||
| 'xai/grok-imagine-image'
|
||||
| 'xai/grok-imagine-image-pro'
|
||||
| (string & {});
|
||||
169
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/gateway-image-model.ts
generated
vendored
Normal file
169
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/gateway-image-model.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,169 @@
|
|||
import type {
|
||||
ImageModelV3,
|
||||
ImageModelV3File,
|
||||
ImageModelV3ProviderMetadata,
|
||||
} from '@ai-sdk/provider';
|
||||
import {
|
||||
combineHeaders,
|
||||
convertUint8ArrayToBase64,
|
||||
createJsonResponseHandler,
|
||||
createJsonErrorResponseHandler,
|
||||
postJsonToApi,
|
||||
resolve,
|
||||
type Resolvable,
|
||||
} from '@ai-sdk/provider-utils';
|
||||
import { z } from 'zod/v4';
|
||||
import type { GatewayConfig } from './gateway-config';
|
||||
import { asGatewayError } from './errors';
|
||||
import { parseAuthMethod } from './errors/parse-auth-method';
|
||||
|
||||
export class GatewayImageModel implements ImageModelV3 {
|
||||
readonly specificationVersion = 'v3' as const;
|
||||
// Set a very large number to prevent client-side splitting of requests
|
||||
readonly maxImagesPerCall = Number.MAX_SAFE_INTEGER;
|
||||
|
||||
constructor(
|
||||
readonly modelId: string,
|
||||
private readonly config: GatewayConfig & {
|
||||
provider: string;
|
||||
o11yHeaders: Resolvable<Record<string, string>>;
|
||||
},
|
||||
) {}
|
||||
|
||||
get provider(): string {
|
||||
return this.config.provider;
|
||||
}
|
||||
|
||||
async doGenerate({
|
||||
prompt,
|
||||
n,
|
||||
size,
|
||||
aspectRatio,
|
||||
seed,
|
||||
files,
|
||||
mask,
|
||||
providerOptions,
|
||||
headers,
|
||||
abortSignal,
|
||||
}: Parameters<ImageModelV3['doGenerate']>[0]): Promise<
|
||||
Awaited<ReturnType<ImageModelV3['doGenerate']>>
|
||||
> {
|
||||
const resolvedHeaders = await resolve(this.config.headers());
|
||||
try {
|
||||
const {
|
||||
responseHeaders,
|
||||
value: responseBody,
|
||||
rawValue,
|
||||
} = await postJsonToApi({
|
||||
url: this.getUrl(),
|
||||
headers: combineHeaders(
|
||||
resolvedHeaders,
|
||||
headers ?? {},
|
||||
this.getModelConfigHeaders(),
|
||||
await resolve(this.config.o11yHeaders),
|
||||
),
|
||||
body: {
|
||||
prompt,
|
||||
n,
|
||||
...(size && { size }),
|
||||
...(aspectRatio && { aspectRatio }),
|
||||
...(seed && { seed }),
|
||||
...(providerOptions && { providerOptions }),
|
||||
...(files && {
|
||||
files: files.map(file => maybeEncodeImageFile(file)),
|
||||
}),
|
||||
...(mask && { mask: maybeEncodeImageFile(mask) }),
|
||||
},
|
||||
successfulResponseHandler: createJsonResponseHandler(
|
||||
gatewayImageResponseSchema,
|
||||
),
|
||||
failedResponseHandler: createJsonErrorResponseHandler({
|
||||
errorSchema: z.any(),
|
||||
errorToMessage: data => data,
|
||||
}),
|
||||
...(abortSignal && { abortSignal }),
|
||||
fetch: this.config.fetch,
|
||||
});
|
||||
|
||||
return {
|
||||
images: responseBody.images, // Always base64 strings from server
|
||||
warnings: responseBody.warnings ?? [],
|
||||
providerMetadata:
|
||||
responseBody.providerMetadata as ImageModelV3ProviderMetadata,
|
||||
response: {
|
||||
timestamp: new Date(),
|
||||
modelId: this.modelId,
|
||||
headers: responseHeaders,
|
||||
},
|
||||
...(responseBody.usage != null && {
|
||||
usage: {
|
||||
inputTokens: responseBody.usage.inputTokens ?? undefined,
|
||||
outputTokens: responseBody.usage.outputTokens ?? undefined,
|
||||
totalTokens: responseBody.usage.totalTokens ?? undefined,
|
||||
},
|
||||
}),
|
||||
};
|
||||
} catch (error) {
|
||||
throw await asGatewayError(error, await parseAuthMethod(resolvedHeaders));
|
||||
}
|
||||
}
|
||||
|
||||
private getUrl() {
|
||||
return `${this.config.baseURL}/image-model`;
|
||||
}
|
||||
|
||||
private getModelConfigHeaders() {
|
||||
return {
|
||||
'ai-image-model-specification-version': '3',
|
||||
'ai-model-id': this.modelId,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function maybeEncodeImageFile(file: ImageModelV3File) {
|
||||
if (file.type === 'file' && file.data instanceof Uint8Array) {
|
||||
return {
|
||||
...file,
|
||||
data: convertUint8ArrayToBase64(file.data),
|
||||
};
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
const providerMetadataEntrySchema = z
|
||||
.object({
|
||||
images: z.array(z.unknown()).optional(),
|
||||
})
|
||||
.catchall(z.unknown());
|
||||
|
||||
const gatewayImageWarningSchema = z.discriminatedUnion('type', [
|
||||
z.object({
|
||||
type: z.literal('unsupported'),
|
||||
feature: z.string(),
|
||||
details: z.string().optional(),
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('compatibility'),
|
||||
feature: z.string(),
|
||||
details: z.string().optional(),
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('other'),
|
||||
message: z.string(),
|
||||
}),
|
||||
]);
|
||||
|
||||
const gatewayImageUsageSchema = z.object({
|
||||
inputTokens: z.number().nullish(),
|
||||
outputTokens: z.number().nullish(),
|
||||
totalTokens: z.number().nullish(),
|
||||
});
|
||||
|
||||
const gatewayImageResponseSchema = z.object({
|
||||
images: z.array(z.string()), // Always base64 strings over the wire
|
||||
warnings: z.array(gatewayImageWarningSchema).optional(),
|
||||
providerMetadata: z
|
||||
.record(z.string(), providerMetadataEntrySchema)
|
||||
.optional(),
|
||||
usage: gatewayImageUsageSchema.optional(),
|
||||
});
|
||||
183
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/gateway-language-model-settings.ts
generated
vendored
Normal file
183
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/gateway-language-model-settings.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,183 @@
|
|||
export type GatewayModelId =
|
||||
| 'alibaba/qwen-3-14b'
|
||||
| 'alibaba/qwen-3-235b'
|
||||
| 'alibaba/qwen-3-30b'
|
||||
| 'alibaba/qwen-3-32b'
|
||||
| 'alibaba/qwen3-235b-a22b-thinking'
|
||||
| 'alibaba/qwen3-coder'
|
||||
| 'alibaba/qwen3-coder-30b-a3b'
|
||||
| 'alibaba/qwen3-coder-next'
|
||||
| 'alibaba/qwen3-coder-plus'
|
||||
| 'alibaba/qwen3-max'
|
||||
| 'alibaba/qwen3-max-preview'
|
||||
| 'alibaba/qwen3-max-thinking'
|
||||
| 'alibaba/qwen3-next-80b-a3b-instruct'
|
||||
| 'alibaba/qwen3-next-80b-a3b-thinking'
|
||||
| 'alibaba/qwen3-vl-instruct'
|
||||
| 'alibaba/qwen3-vl-thinking'
|
||||
| 'alibaba/qwen3.5-flash'
|
||||
| 'alibaba/qwen3.5-plus'
|
||||
| 'amazon/nova-2-lite'
|
||||
| 'amazon/nova-lite'
|
||||
| 'amazon/nova-micro'
|
||||
| 'amazon/nova-pro'
|
||||
| 'anthropic/claude-3-haiku'
|
||||
| 'anthropic/claude-3-opus'
|
||||
| 'anthropic/claude-3.5-haiku'
|
||||
| 'anthropic/claude-3.5-sonnet'
|
||||
| 'anthropic/claude-3.5-sonnet-20240620'
|
||||
| 'anthropic/claude-3.7-sonnet'
|
||||
| 'anthropic/claude-haiku-4.5'
|
||||
| 'anthropic/claude-opus-4'
|
||||
| 'anthropic/claude-opus-4.1'
|
||||
| 'anthropic/claude-opus-4.5'
|
||||
| 'anthropic/claude-opus-4.6'
|
||||
| 'anthropic/claude-sonnet-4'
|
||||
| 'anthropic/claude-sonnet-4.5'
|
||||
| 'anthropic/claude-sonnet-4.6'
|
||||
| 'arcee-ai/trinity-large-preview'
|
||||
| 'arcee-ai/trinity-mini'
|
||||
| 'bytedance/seed-1.6'
|
||||
| 'bytedance/seed-1.8'
|
||||
| 'cohere/command-a'
|
||||
| 'deepseek/deepseek-r1'
|
||||
| 'deepseek/deepseek-v3'
|
||||
| 'deepseek/deepseek-v3.1'
|
||||
| 'deepseek/deepseek-v3.1-terminus'
|
||||
| 'deepseek/deepseek-v3.2'
|
||||
| 'deepseek/deepseek-v3.2-thinking'
|
||||
| 'google/gemini-2.0-flash'
|
||||
| 'google/gemini-2.0-flash-lite'
|
||||
| 'google/gemini-2.5-flash'
|
||||
| 'google/gemini-2.5-flash-image'
|
||||
| 'google/gemini-2.5-flash-lite'
|
||||
| 'google/gemini-2.5-pro'
|
||||
| 'google/gemini-3-flash'
|
||||
| 'google/gemini-3-pro-image'
|
||||
| 'google/gemini-3-pro-preview'
|
||||
| 'google/gemini-3.1-flash-image-preview'
|
||||
| 'google/gemini-3.1-flash-lite-preview'
|
||||
| 'google/gemini-3.1-pro-preview'
|
||||
| 'inception/mercury-2'
|
||||
| 'inception/mercury-coder-small'
|
||||
| 'kwaipilot/kat-coder-pro-v1'
|
||||
| 'meituan/longcat-flash-chat'
|
||||
| 'meituan/longcat-flash-thinking'
|
||||
| 'meituan/longcat-flash-thinking-2601'
|
||||
| 'meta/llama-3.1-70b'
|
||||
| 'meta/llama-3.1-8b'
|
||||
| 'meta/llama-3.2-11b'
|
||||
| 'meta/llama-3.2-1b'
|
||||
| 'meta/llama-3.2-3b'
|
||||
| 'meta/llama-3.2-90b'
|
||||
| 'meta/llama-3.3-70b'
|
||||
| 'meta/llama-4-maverick'
|
||||
| 'meta/llama-4-scout'
|
||||
| 'minimax/minimax-m2'
|
||||
| 'minimax/minimax-m2.1'
|
||||
| 'minimax/minimax-m2.1-lightning'
|
||||
| 'minimax/minimax-m2.5'
|
||||
| 'minimax/minimax-m2.5-highspeed'
|
||||
| 'minimax/minimax-m2.7'
|
||||
| 'minimax/minimax-m2.7-highspeed'
|
||||
| 'mistral/codestral'
|
||||
| 'mistral/devstral-2'
|
||||
| 'mistral/devstral-small'
|
||||
| 'mistral/devstral-small-2'
|
||||
| 'mistral/magistral-medium'
|
||||
| 'mistral/magistral-small'
|
||||
| 'mistral/ministral-14b'
|
||||
| 'mistral/ministral-3b'
|
||||
| 'mistral/ministral-8b'
|
||||
| 'mistral/mistral-large-3'
|
||||
| 'mistral/mistral-medium'
|
||||
| 'mistral/mistral-nemo'
|
||||
| 'mistral/mistral-small'
|
||||
| 'mistral/mixtral-8x22b-instruct'
|
||||
| 'mistral/pixtral-12b'
|
||||
| 'mistral/pixtral-large'
|
||||
| 'moonshotai/kimi-k2'
|
||||
| 'moonshotai/kimi-k2-0905'
|
||||
| 'moonshotai/kimi-k2-thinking'
|
||||
| 'moonshotai/kimi-k2-thinking-turbo'
|
||||
| 'moonshotai/kimi-k2-turbo'
|
||||
| 'moonshotai/kimi-k2.5'
|
||||
| 'morph/morph-v3-fast'
|
||||
| 'morph/morph-v3-large'
|
||||
| 'nvidia/nemotron-3-nano-30b-a3b'
|
||||
| 'nvidia/nemotron-nano-12b-v2-vl'
|
||||
| 'nvidia/nemotron-nano-9b-v2'
|
||||
| 'openai/gpt-3.5-turbo'
|
||||
| 'openai/gpt-3.5-turbo-instruct'
|
||||
| 'openai/gpt-4-turbo'
|
||||
| 'openai/gpt-4.1'
|
||||
| 'openai/gpt-4.1-mini'
|
||||
| 'openai/gpt-4.1-nano'
|
||||
| 'openai/gpt-4o'
|
||||
| 'openai/gpt-4o-mini'
|
||||
| 'openai/gpt-4o-mini-search-preview'
|
||||
| 'openai/gpt-5'
|
||||
| 'openai/gpt-5-chat'
|
||||
| 'openai/gpt-5-codex'
|
||||
| 'openai/gpt-5-mini'
|
||||
| 'openai/gpt-5-nano'
|
||||
| 'openai/gpt-5-pro'
|
||||
| 'openai/gpt-5.1-codex'
|
||||
| 'openai/gpt-5.1-codex-max'
|
||||
| 'openai/gpt-5.1-codex-mini'
|
||||
| 'openai/gpt-5.1-instant'
|
||||
| 'openai/gpt-5.1-thinking'
|
||||
| 'openai/gpt-5.2'
|
||||
| 'openai/gpt-5.2-chat'
|
||||
| 'openai/gpt-5.2-codex'
|
||||
| 'openai/gpt-5.2-pro'
|
||||
| 'openai/gpt-5.3-chat'
|
||||
| 'openai/gpt-5.3-codex'
|
||||
| 'openai/gpt-5.4'
|
||||
| 'openai/gpt-5.4-mini'
|
||||
| 'openai/gpt-5.4-nano'
|
||||
| 'openai/gpt-5.4-pro'
|
||||
| 'openai/gpt-oss-120b'
|
||||
| 'openai/gpt-oss-20b'
|
||||
| 'openai/gpt-oss-safeguard-20b'
|
||||
| 'openai/o1'
|
||||
| 'openai/o3'
|
||||
| 'openai/o3-deep-research'
|
||||
| 'openai/o3-mini'
|
||||
| 'openai/o3-pro'
|
||||
| 'openai/o4-mini'
|
||||
| 'perplexity/sonar'
|
||||
| 'perplexity/sonar-pro'
|
||||
| 'perplexity/sonar-reasoning-pro'
|
||||
| 'prime-intellect/intellect-3'
|
||||
| 'xai/grok-2-vision'
|
||||
| 'xai/grok-3'
|
||||
| 'xai/grok-3-fast'
|
||||
| 'xai/grok-3-mini'
|
||||
| 'xai/grok-3-mini-fast'
|
||||
| 'xai/grok-4'
|
||||
| 'xai/grok-4-fast-non-reasoning'
|
||||
| 'xai/grok-4-fast-reasoning'
|
||||
| 'xai/grok-4.1-fast-non-reasoning'
|
||||
| 'xai/grok-4.1-fast-reasoning'
|
||||
| 'xai/grok-4.20-multi-agent'
|
||||
| 'xai/grok-4.20-multi-agent-beta'
|
||||
| 'xai/grok-4.20-non-reasoning'
|
||||
| 'xai/grok-4.20-non-reasoning-beta'
|
||||
| 'xai/grok-4.20-reasoning'
|
||||
| 'xai/grok-4.20-reasoning-beta'
|
||||
| 'xai/grok-code-fast-1'
|
||||
| 'xiaomi/mimo-v2-flash'
|
||||
| 'xiaomi/mimo-v2-pro'
|
||||
| 'zai/glm-4.5'
|
||||
| 'zai/glm-4.5-air'
|
||||
| 'zai/glm-4.5v'
|
||||
| 'zai/glm-4.6'
|
||||
| 'zai/glm-4.6v'
|
||||
| 'zai/glm-4.6v-flash'
|
||||
| 'zai/glm-4.7'
|
||||
| 'zai/glm-4.7-flash'
|
||||
| 'zai/glm-4.7-flashx'
|
||||
| 'zai/glm-5'
|
||||
| 'zai/glm-5-turbo'
|
||||
| (string & {});
|
||||
212
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/gateway-language-model.ts
generated
vendored
Normal file
212
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/gateway-language-model.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,212 @@
|
|||
import type {
|
||||
LanguageModelV3,
|
||||
LanguageModelV3CallOptions,
|
||||
SharedV3Warning,
|
||||
LanguageModelV3FilePart,
|
||||
LanguageModelV3StreamPart,
|
||||
LanguageModelV3GenerateResult,
|
||||
LanguageModelV3StreamResult,
|
||||
} from '@ai-sdk/provider';
|
||||
import {
|
||||
combineHeaders,
|
||||
createEventSourceResponseHandler,
|
||||
createJsonErrorResponseHandler,
|
||||
createJsonResponseHandler,
|
||||
postJsonToApi,
|
||||
resolve,
|
||||
type ParseResult,
|
||||
type Resolvable,
|
||||
} from '@ai-sdk/provider-utils';
|
||||
import { z } from 'zod/v4';
|
||||
import type { GatewayConfig } from './gateway-config';
|
||||
import type { GatewayModelId } from './gateway-language-model-settings';
|
||||
import { asGatewayError } from './errors';
|
||||
import { parseAuthMethod } from './errors/parse-auth-method';
|
||||
|
||||
type GatewayChatConfig = GatewayConfig & {
|
||||
provider: string;
|
||||
o11yHeaders: Resolvable<Record<string, string>>;
|
||||
};
|
||||
|
||||
export class GatewayLanguageModel implements LanguageModelV3 {
|
||||
readonly specificationVersion = 'v3';
|
||||
readonly supportedUrls = { '*/*': [/.*/] };
|
||||
|
||||
constructor(
|
||||
readonly modelId: GatewayModelId,
|
||||
private readonly config: GatewayChatConfig,
|
||||
) {}
|
||||
|
||||
get provider(): string {
|
||||
return this.config.provider;
|
||||
}
|
||||
|
||||
private async getArgs(options: LanguageModelV3CallOptions) {
|
||||
const { abortSignal: _abortSignal, ...optionsWithoutSignal } = options;
|
||||
|
||||
return {
|
||||
args: this.maybeEncodeFileParts(optionsWithoutSignal),
|
||||
warnings: [],
|
||||
};
|
||||
}
|
||||
|
||||
async doGenerate(
|
||||
options: LanguageModelV3CallOptions,
|
||||
): Promise<LanguageModelV3GenerateResult> {
|
||||
const { args, warnings } = await this.getArgs(options);
|
||||
const { abortSignal } = options;
|
||||
|
||||
const resolvedHeaders = await resolve(this.config.headers());
|
||||
|
||||
try {
|
||||
const {
|
||||
responseHeaders,
|
||||
value: responseBody,
|
||||
rawValue: rawResponse,
|
||||
} = await postJsonToApi({
|
||||
url: this.getUrl(),
|
||||
headers: combineHeaders(
|
||||
resolvedHeaders,
|
||||
options.headers,
|
||||
this.getModelConfigHeaders(this.modelId, false),
|
||||
await resolve(this.config.o11yHeaders),
|
||||
),
|
||||
body: args,
|
||||
successfulResponseHandler: createJsonResponseHandler(z.any()),
|
||||
failedResponseHandler: createJsonErrorResponseHandler({
|
||||
errorSchema: z.any(),
|
||||
errorToMessage: data => data,
|
||||
}),
|
||||
...(abortSignal && { abortSignal }),
|
||||
fetch: this.config.fetch,
|
||||
});
|
||||
|
||||
return {
|
||||
...responseBody,
|
||||
request: { body: args },
|
||||
response: { headers: responseHeaders, body: rawResponse },
|
||||
warnings,
|
||||
};
|
||||
} catch (error) {
|
||||
throw await asGatewayError(error, await parseAuthMethod(resolvedHeaders));
|
||||
}
|
||||
}
|
||||
|
||||
async doStream(
|
||||
options: LanguageModelV3CallOptions,
|
||||
): Promise<LanguageModelV3StreamResult> {
|
||||
const { args, warnings } = await this.getArgs(options);
|
||||
const { abortSignal } = options;
|
||||
|
||||
const resolvedHeaders = await resolve(this.config.headers());
|
||||
|
||||
try {
|
||||
const { value: response, responseHeaders } = await postJsonToApi({
|
||||
url: this.getUrl(),
|
||||
headers: combineHeaders(
|
||||
resolvedHeaders,
|
||||
options.headers,
|
||||
this.getModelConfigHeaders(this.modelId, true),
|
||||
await resolve(this.config.o11yHeaders),
|
||||
),
|
||||
body: args,
|
||||
successfulResponseHandler: createEventSourceResponseHandler(z.any()),
|
||||
failedResponseHandler: createJsonErrorResponseHandler({
|
||||
errorSchema: z.any(),
|
||||
errorToMessage: data => data,
|
||||
}),
|
||||
...(abortSignal && { abortSignal }),
|
||||
fetch: this.config.fetch,
|
||||
});
|
||||
|
||||
return {
|
||||
stream: response.pipeThrough(
|
||||
new TransformStream<
|
||||
ParseResult<LanguageModelV3StreamPart>,
|
||||
LanguageModelV3StreamPart
|
||||
>({
|
||||
start(controller) {
|
||||
if (warnings.length > 0) {
|
||||
controller.enqueue({ type: 'stream-start', warnings });
|
||||
}
|
||||
},
|
||||
transform(chunk, controller) {
|
||||
if (chunk.success) {
|
||||
const streamPart = chunk.value;
|
||||
|
||||
// Handle raw chunks: if this is a raw chunk from the gateway API,
|
||||
// only emit it if includeRawChunks is true
|
||||
if (streamPart.type === 'raw' && !options.includeRawChunks) {
|
||||
return; // Skip raw chunks if not requested
|
||||
}
|
||||
|
||||
if (
|
||||
streamPart.type === 'response-metadata' &&
|
||||
streamPart.timestamp &&
|
||||
typeof streamPart.timestamp === 'string'
|
||||
) {
|
||||
streamPart.timestamp = new Date(streamPart.timestamp);
|
||||
}
|
||||
|
||||
controller.enqueue(streamPart);
|
||||
} else {
|
||||
controller.error(
|
||||
(chunk as { success: false; error: unknown }).error,
|
||||
);
|
||||
}
|
||||
},
|
||||
}),
|
||||
),
|
||||
request: { body: args },
|
||||
response: { headers: responseHeaders },
|
||||
};
|
||||
} catch (error) {
|
||||
throw await asGatewayError(error, await parseAuthMethod(resolvedHeaders));
|
||||
}
|
||||
}
|
||||
|
||||
private isFilePart(part: unknown) {
|
||||
return (
|
||||
part && typeof part === 'object' && 'type' in part && part.type === 'file'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Encodes file parts in the prompt to base64. Mutates the passed options
|
||||
* instance directly to avoid copying the file data.
|
||||
* @param options - The options to encode.
|
||||
* @returns The options with the file parts encoded.
|
||||
*/
|
||||
private maybeEncodeFileParts(options: LanguageModelV3CallOptions) {
|
||||
for (const message of options.prompt) {
|
||||
for (const part of message.content) {
|
||||
if (this.isFilePart(part)) {
|
||||
const filePart = part as LanguageModelV3FilePart;
|
||||
// If the file part is a URL it will get cleanly converted to a string.
|
||||
// If it's a binary file attachment we convert it to a data url.
|
||||
// In either case, server-side we should only ever see URLs as strings.
|
||||
if (filePart.data instanceof Uint8Array) {
|
||||
const buffer = Uint8Array.from(filePart.data);
|
||||
const base64Data = Buffer.from(buffer).toString('base64');
|
||||
filePart.data = new URL(
|
||||
`data:${filePart.mediaType || 'application/octet-stream'};base64,${base64Data}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return options;
|
||||
}
|
||||
|
||||
private getUrl() {
|
||||
return `${this.config.baseURL}/language-model`;
|
||||
}
|
||||
|
||||
private getModelConfigHeaders(modelId: string, streaming: boolean) {
|
||||
return {
|
||||
'ai-language-model-specification-version': '3',
|
||||
'ai-language-model-id': modelId,
|
||||
'ai-language-model-streaming': String(streaming),
|
||||
};
|
||||
}
|
||||
}
|
||||
58
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/gateway-model-entry.ts
generated
vendored
Normal file
58
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/gateway-model-entry.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
import type { LanguageModelV3 } from '@ai-sdk/provider';
|
||||
|
||||
export interface GatewayLanguageModelEntry {
|
||||
/**
|
||||
* The model id used by the remote provider in model settings and for specifying the
|
||||
* intended model for text generation.
|
||||
*/
|
||||
id: string;
|
||||
|
||||
/**
|
||||
* The display name of the model for presentation in user-facing contexts.
|
||||
*/
|
||||
name: string;
|
||||
|
||||
/**
|
||||
* Optional description of the model.
|
||||
*/
|
||||
description?: string | null;
|
||||
|
||||
/**
|
||||
* Optional pricing information for the model.
|
||||
*/
|
||||
pricing?: {
|
||||
/**
|
||||
* Cost per input token in USD.
|
||||
*/
|
||||
input: string;
|
||||
/**
|
||||
* Cost per output token in USD.
|
||||
*/
|
||||
output: string;
|
||||
/**
|
||||
* Cost per cached input token in USD.
|
||||
* Only present for providers/models that support prompt caching.
|
||||
*/
|
||||
cachedInputTokens?: string;
|
||||
/**
|
||||
* Cost per input token to create/write cache entries in USD.
|
||||
* Only present for providers/models that support prompt caching.
|
||||
*/
|
||||
cacheCreationInputTokens?: string;
|
||||
} | null;
|
||||
|
||||
/**
|
||||
* Additional AI SDK language model specifications for the model.
|
||||
*/
|
||||
specification: GatewayLanguageModelSpecification;
|
||||
|
||||
/**
|
||||
* Optional field to differentiate between model types.
|
||||
*/
|
||||
modelType?: 'language' | 'embedding' | 'image' | 'video' | null;
|
||||
}
|
||||
|
||||
export type GatewayLanguageModelSpecification = Pick<
|
||||
LanguageModelV3,
|
||||
'specificationVersion' | 'provider' | 'modelId'
|
||||
>;
|
||||
80
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/gateway-provider-options.ts
generated
vendored
Normal file
80
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/gateway-provider-options.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
import { InferSchema, lazySchema, zodSchema } from '@ai-sdk/provider-utils';
|
||||
import { z } from 'zod/v4';
|
||||
|
||||
// https://vercel.com/docs/ai-gateway/provider-options
|
||||
const gatewayLanguageModelOptions = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.object({
|
||||
/**
|
||||
* Array of provider slugs that are the only ones allowed to be used.
|
||||
*
|
||||
* Example: `['azure', 'openai']` will only allow Azure and OpenAI to be used.
|
||||
*/
|
||||
only: z.array(z.string()).optional(),
|
||||
/**
|
||||
* Array of provider slugs that specifies the sequence in which providers should be attempted.
|
||||
*
|
||||
* Example: `['bedrock', 'anthropic']` will try Amazon Bedrock first, then Anthropic as fallback.
|
||||
*/
|
||||
order: z.array(z.string()).optional(),
|
||||
/**
|
||||
* The unique identifier for the end user on behalf of whom the request was made.
|
||||
*
|
||||
* Used for spend tracking and attribution purposes.
|
||||
*/
|
||||
user: z.string().optional(),
|
||||
/**
|
||||
* User-specified tags for use in reporting and filtering usage.
|
||||
*
|
||||
* For example, spend tracking reporting by feature or prompt version.
|
||||
*
|
||||
* Example: `['chat', 'v2']`
|
||||
*/
|
||||
tags: z.array(z.string()).optional(),
|
||||
/**
|
||||
* Array of model slugs specifying fallback models to use in order.
|
||||
*
|
||||
* Example: `['openai/gpt-5-nano', 'zai/glm-4.6']` will try `openai/gpt-5-nano` first, then `zai/glm-4.6` as fallback.
|
||||
*/
|
||||
models: z.array(z.string()).optional(),
|
||||
/**
|
||||
* Request-scoped BYOK credentials to use instead of cached credentials.
|
||||
*
|
||||
* When provided, cached BYOK credentials are ignored entirely.
|
||||
*
|
||||
* Each provider can have multiple credentials (tried in order).
|
||||
*
|
||||
* Examples:
|
||||
* - Simple: `{ 'anthropic': [{ apiKey: 'sk-ant-...' }] }`
|
||||
* - Multiple: `{ 'vertex': [{ projectId: 'proj-1', privateKey: '...' }, { projectId: 'proj-2', privateKey: '...' }] }`
|
||||
* - Multi-provider: `{ 'anthropic': [{ apiKey: '...' }], 'bedrock': [{ accessKeyId: '...', secretAccessKey: '...' }] }`
|
||||
*/
|
||||
byok: z
|
||||
.record(z.string(), z.array(z.record(z.string(), z.unknown())))
|
||||
.optional(),
|
||||
/**
|
||||
* Whether to filter by only providers that state they have zero data
|
||||
* retention with Vercel AI Gateway. When enabled, only providers that
|
||||
* have agreements with Vercel AI Gateway for zero data retention will be
|
||||
* used.
|
||||
*/
|
||||
zeroDataRetention: z.boolean().optional(),
|
||||
/**
|
||||
* Per-provider timeouts for BYOK credentials in milliseconds.
|
||||
* Controls how long to wait for a provider to start responding
|
||||
* before falling back to the next available provider.
|
||||
*
|
||||
* Example: `{ byok: { openai: 5000, anthropic: 2000 } }`
|
||||
*/
|
||||
providerTimeouts: z
|
||||
.object({
|
||||
byok: z.record(z.string(), z.number().int().min(1000)).optional(),
|
||||
})
|
||||
.optional(),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
export type GatewayLanguageModelOptions = InferSchema<
|
||||
typeof gatewayLanguageModelOptions
|
||||
>;
|
||||
329
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/gateway-provider.ts
generated
vendored
Normal file
329
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/gateway-provider.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,329 @@
|
|||
import {
|
||||
loadOptionalSetting,
|
||||
withoutTrailingSlash,
|
||||
type FetchFunction,
|
||||
} from '@ai-sdk/provider-utils';
|
||||
import { asGatewayError, GatewayAuthenticationError } from './errors';
|
||||
import {
|
||||
GATEWAY_AUTH_METHOD_HEADER,
|
||||
parseAuthMethod,
|
||||
} from './errors/parse-auth-method';
|
||||
import {
|
||||
GatewayFetchMetadata,
|
||||
type GatewayFetchMetadataResponse,
|
||||
type GatewayCreditsResponse,
|
||||
} from './gateway-fetch-metadata';
|
||||
import { GatewayLanguageModel } from './gateway-language-model';
|
||||
import { GatewayEmbeddingModel } from './gateway-embedding-model';
|
||||
import { GatewayImageModel } from './gateway-image-model';
|
||||
import { GatewayVideoModel } from './gateway-video-model';
|
||||
import type { GatewayEmbeddingModelId } from './gateway-embedding-model-settings';
|
||||
import type { GatewayImageModelId } from './gateway-image-model-settings';
|
||||
import type { GatewayVideoModelId } from './gateway-video-model-settings';
|
||||
import { gatewayTools } from './gateway-tools';
|
||||
import { getVercelOidcToken, getVercelRequestId } from './vercel-environment';
|
||||
import type { GatewayModelId } from './gateway-language-model-settings';
|
||||
import type {
|
||||
LanguageModelV3,
|
||||
EmbeddingModelV3,
|
||||
ImageModelV3,
|
||||
Experimental_VideoModelV3,
|
||||
ProviderV3,
|
||||
} from '@ai-sdk/provider';
|
||||
import { withUserAgentSuffix } from '@ai-sdk/provider-utils';
|
||||
import { VERSION } from './version';
|
||||
|
||||
export interface GatewayProvider extends ProviderV3 {
|
||||
(modelId: GatewayModelId): LanguageModelV3;
|
||||
|
||||
/**
|
||||
* Creates a model for text generation.
|
||||
*/
|
||||
chat(modelId: GatewayModelId): LanguageModelV3;
|
||||
|
||||
/**
|
||||
* Creates a model for text generation.
|
||||
*/
|
||||
languageModel(modelId: GatewayModelId): LanguageModelV3;
|
||||
|
||||
/**
|
||||
* Returns available providers and models for use with the remote provider.
|
||||
*/
|
||||
getAvailableModels(): Promise<GatewayFetchMetadataResponse>;
|
||||
|
||||
/**
|
||||
* Returns credit information for the authenticated user.
|
||||
*/
|
||||
getCredits(): Promise<GatewayCreditsResponse>;
|
||||
|
||||
/**
|
||||
* Creates a model for generating text embeddings.
|
||||
*/
|
||||
embedding(modelId: GatewayEmbeddingModelId): EmbeddingModelV3;
|
||||
|
||||
/**
|
||||
* Creates a model for generating text embeddings.
|
||||
*/
|
||||
embeddingModel(modelId: GatewayEmbeddingModelId): EmbeddingModelV3;
|
||||
|
||||
/**
|
||||
* @deprecated Use `embeddingModel` instead.
|
||||
*/
|
||||
textEmbeddingModel(modelId: GatewayEmbeddingModelId): EmbeddingModelV3;
|
||||
|
||||
/**
|
||||
* Creates a model for generating images.
|
||||
*/
|
||||
image(modelId: GatewayImageModelId): ImageModelV3;
|
||||
|
||||
/**
|
||||
* Creates a model for generating images.
|
||||
*/
|
||||
imageModel(modelId: GatewayImageModelId): ImageModelV3;
|
||||
|
||||
/**
|
||||
* Creates a model for generating videos.
|
||||
*/
|
||||
video(modelId: GatewayVideoModelId): Experimental_VideoModelV3;
|
||||
|
||||
/**
|
||||
* Creates a model for generating videos.
|
||||
*/
|
||||
videoModel(modelId: GatewayVideoModelId): Experimental_VideoModelV3;
|
||||
|
||||
/**
|
||||
* Gateway-specific tools executed server-side.
|
||||
*/
|
||||
tools: typeof gatewayTools;
|
||||
}
|
||||
|
||||
export interface GatewayProviderSettings {
|
||||
/**
|
||||
* The base URL prefix for API calls. Defaults to `https://ai-gateway.vercel.sh/v1/ai`.
|
||||
*/
|
||||
baseURL?: string;
|
||||
|
||||
/**
|
||||
* API key that is being sent using the `Authorization` header.
|
||||
*/
|
||||
apiKey?: string;
|
||||
|
||||
/**
|
||||
* Custom headers to include in the requests.
|
||||
*/
|
||||
headers?: Record<string, string>;
|
||||
|
||||
/**
|
||||
* Custom fetch implementation. You can use it as a middleware to intercept requests,
|
||||
* or to provide a custom fetch implementation for e.g. testing.
|
||||
*/
|
||||
fetch?: FetchFunction;
|
||||
|
||||
/**
|
||||
* How frequently to refresh the metadata cache in milliseconds.
|
||||
*/
|
||||
metadataCacheRefreshMillis?: number;
|
||||
|
||||
/**
|
||||
* @internal For testing purposes only
|
||||
*/
|
||||
_internal?: {
|
||||
currentDate?: () => Date;
|
||||
};
|
||||
}
|
||||
|
||||
const AI_GATEWAY_PROTOCOL_VERSION = '0.0.1';
|
||||
|
||||
/**
|
||||
* Create a remote provider instance.
|
||||
*/
|
||||
export function createGatewayProvider(
|
||||
options: GatewayProviderSettings = {},
|
||||
): GatewayProvider {
|
||||
let pendingMetadata: Promise<GatewayFetchMetadataResponse> | null = null;
|
||||
let metadataCache: GatewayFetchMetadataResponse | null = null;
|
||||
const cacheRefreshMillis =
|
||||
options.metadataCacheRefreshMillis ?? 1000 * 60 * 5;
|
||||
let lastFetchTime = 0;
|
||||
|
||||
const baseURL =
|
||||
withoutTrailingSlash(options.baseURL) ??
|
||||
'https://ai-gateway.vercel.sh/v3/ai';
|
||||
|
||||
const getHeaders = async () => {
|
||||
try {
|
||||
const auth = await getGatewayAuthToken(options);
|
||||
return withUserAgentSuffix(
|
||||
{
|
||||
Authorization: `Bearer ${auth.token}`,
|
||||
'ai-gateway-protocol-version': AI_GATEWAY_PROTOCOL_VERSION,
|
||||
[GATEWAY_AUTH_METHOD_HEADER]: auth.authMethod,
|
||||
...options.headers,
|
||||
},
|
||||
`ai-sdk/gateway/${VERSION}`,
|
||||
);
|
||||
} catch (error) {
|
||||
throw GatewayAuthenticationError.createContextualError({
|
||||
apiKeyProvided: false,
|
||||
oidcTokenProvided: false,
|
||||
statusCode: 401,
|
||||
cause: error,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const createO11yHeaders = () => {
|
||||
const deploymentId = loadOptionalSetting({
|
||||
settingValue: undefined,
|
||||
environmentVariableName: 'VERCEL_DEPLOYMENT_ID',
|
||||
});
|
||||
const environment = loadOptionalSetting({
|
||||
settingValue: undefined,
|
||||
environmentVariableName: 'VERCEL_ENV',
|
||||
});
|
||||
const region = loadOptionalSetting({
|
||||
settingValue: undefined,
|
||||
environmentVariableName: 'VERCEL_REGION',
|
||||
});
|
||||
const projectId = loadOptionalSetting({
|
||||
settingValue: undefined,
|
||||
environmentVariableName: 'VERCEL_PROJECT_ID',
|
||||
});
|
||||
|
||||
return async () => {
|
||||
const requestId = await getVercelRequestId();
|
||||
return {
|
||||
...(deploymentId && { 'ai-o11y-deployment-id': deploymentId }),
|
||||
...(environment && { 'ai-o11y-environment': environment }),
|
||||
...(region && { 'ai-o11y-region': region }),
|
||||
...(requestId && { 'ai-o11y-request-id': requestId }),
|
||||
...(projectId && { 'ai-o11y-project-id': projectId }),
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
const createLanguageModel = (modelId: GatewayModelId) => {
|
||||
return new GatewayLanguageModel(modelId, {
|
||||
provider: 'gateway',
|
||||
baseURL,
|
||||
headers: getHeaders,
|
||||
fetch: options.fetch,
|
||||
o11yHeaders: createO11yHeaders(),
|
||||
});
|
||||
};
|
||||
|
||||
const getAvailableModels = async () => {
|
||||
const now = options._internal?.currentDate?.().getTime() ?? Date.now();
|
||||
if (!pendingMetadata || now - lastFetchTime > cacheRefreshMillis) {
|
||||
lastFetchTime = now;
|
||||
|
||||
pendingMetadata = new GatewayFetchMetadata({
|
||||
baseURL,
|
||||
headers: getHeaders,
|
||||
fetch: options.fetch,
|
||||
})
|
||||
.getAvailableModels()
|
||||
.then(metadata => {
|
||||
metadataCache = metadata;
|
||||
return metadata;
|
||||
})
|
||||
.catch(async (error: unknown) => {
|
||||
throw await asGatewayError(
|
||||
error,
|
||||
await parseAuthMethod(await getHeaders()),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
return metadataCache ? Promise.resolve(metadataCache) : pendingMetadata;
|
||||
};
|
||||
|
||||
const getCredits = async () => {
|
||||
return new GatewayFetchMetadata({
|
||||
baseURL,
|
||||
headers: getHeaders,
|
||||
fetch: options.fetch,
|
||||
})
|
||||
.getCredits()
|
||||
.catch(async (error: unknown) => {
|
||||
throw await asGatewayError(
|
||||
error,
|
||||
await parseAuthMethod(await getHeaders()),
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
const provider = function (modelId: GatewayModelId) {
|
||||
if (new.target) {
|
||||
throw new Error(
|
||||
'The Gateway Provider model function cannot be called with the new keyword.',
|
||||
);
|
||||
}
|
||||
|
||||
return createLanguageModel(modelId);
|
||||
};
|
||||
|
||||
provider.specificationVersion = 'v3' as const;
|
||||
provider.getAvailableModels = getAvailableModels;
|
||||
provider.getCredits = getCredits;
|
||||
provider.imageModel = (modelId: GatewayImageModelId) => {
|
||||
return new GatewayImageModel(modelId, {
|
||||
provider: 'gateway',
|
||||
baseURL,
|
||||
headers: getHeaders,
|
||||
fetch: options.fetch,
|
||||
o11yHeaders: createO11yHeaders(),
|
||||
});
|
||||
};
|
||||
provider.languageModel = createLanguageModel;
|
||||
const createEmbeddingModel = (modelId: GatewayEmbeddingModelId) => {
|
||||
return new GatewayEmbeddingModel(modelId, {
|
||||
provider: 'gateway',
|
||||
baseURL,
|
||||
headers: getHeaders,
|
||||
fetch: options.fetch,
|
||||
o11yHeaders: createO11yHeaders(),
|
||||
});
|
||||
};
|
||||
provider.embeddingModel = createEmbeddingModel;
|
||||
provider.textEmbeddingModel = createEmbeddingModel;
|
||||
provider.videoModel = (modelId: GatewayVideoModelId) => {
|
||||
return new GatewayVideoModel(modelId, {
|
||||
provider: 'gateway',
|
||||
baseURL,
|
||||
headers: getHeaders,
|
||||
fetch: options.fetch,
|
||||
o11yHeaders: createO11yHeaders(),
|
||||
});
|
||||
};
|
||||
provider.chat = provider.languageModel;
|
||||
provider.embedding = provider.embeddingModel;
|
||||
provider.image = provider.imageModel;
|
||||
provider.video = provider.videoModel;
|
||||
provider.tools = gatewayTools;
|
||||
return provider;
|
||||
}
|
||||
|
||||
export const gateway = createGatewayProvider();
|
||||
|
||||
export async function getGatewayAuthToken(
|
||||
options: GatewayProviderSettings,
|
||||
): Promise<{ token: string; authMethod: 'api-key' | 'oidc' }> {
|
||||
const apiKey = loadOptionalSetting({
|
||||
settingValue: options.apiKey,
|
||||
environmentVariableName: 'AI_GATEWAY_API_KEY',
|
||||
});
|
||||
|
||||
if (apiKey) {
|
||||
return {
|
||||
token: apiKey,
|
||||
authMethod: 'api-key',
|
||||
};
|
||||
}
|
||||
|
||||
const oidcToken = await getVercelOidcToken();
|
||||
return {
|
||||
token: oidcToken,
|
||||
authMethod: 'oidc',
|
||||
};
|
||||
}
|
||||
26
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/gateway-tools.ts
generated
vendored
Normal file
26
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/gateway-tools.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
import { parallelSearch } from './tool/parallel-search';
|
||||
import { perplexitySearch } from './tool/perplexity-search';
|
||||
|
||||
/**
|
||||
* Gateway-specific provider-defined tools.
|
||||
*/
|
||||
export const gatewayTools = {
|
||||
/**
|
||||
* Search the web using Parallel AI's Search API for LLM-optimized excerpts.
|
||||
*
|
||||
* Takes a natural language objective and returns relevant excerpts,
|
||||
* replacing multiple keyword searches with a single call for broad
|
||||
* or complex queries. Supports different search types for depth vs
|
||||
* breadth tradeoffs.
|
||||
*/
|
||||
parallelSearch,
|
||||
|
||||
/**
|
||||
* Search the web using Perplexity's Search API for real-time information,
|
||||
* news, research papers, and articles.
|
||||
*
|
||||
* Provides ranked search results with advanced filtering options including
|
||||
* domain, language, date range, and recency filters.
|
||||
*/
|
||||
perplexitySearch,
|
||||
};
|
||||
25
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/gateway-video-model-settings.ts
generated
vendored
Normal file
25
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/gateway-video-model-settings.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
export type GatewayVideoModelId =
|
||||
| 'alibaba/wan-v2.5-t2v-preview'
|
||||
| 'alibaba/wan-v2.6-i2v'
|
||||
| 'alibaba/wan-v2.6-i2v-flash'
|
||||
| 'alibaba/wan-v2.6-r2v'
|
||||
| 'alibaba/wan-v2.6-r2v-flash'
|
||||
| 'alibaba/wan-v2.6-t2v'
|
||||
| 'bytedance/seedance-v1.0-lite-i2v'
|
||||
| 'bytedance/seedance-v1.0-lite-t2v'
|
||||
| 'bytedance/seedance-v1.0-pro'
|
||||
| 'bytedance/seedance-v1.0-pro-fast'
|
||||
| 'bytedance/seedance-v1.5-pro'
|
||||
| 'google/veo-3.0-fast-generate-001'
|
||||
| 'google/veo-3.0-generate-001'
|
||||
| 'google/veo-3.1-fast-generate-001'
|
||||
| 'google/veo-3.1-generate-001'
|
||||
| 'klingai/kling-v2.5-turbo-i2v'
|
||||
| 'klingai/kling-v2.5-turbo-t2v'
|
||||
| 'klingai/kling-v2.6-i2v'
|
||||
| 'klingai/kling-v2.6-motion-control'
|
||||
| 'klingai/kling-v2.6-t2v'
|
||||
| 'klingai/kling-v3.0-i2v'
|
||||
| 'klingai/kling-v3.0-t2v'
|
||||
| 'xai/grok-imagine-video'
|
||||
| (string & {});
|
||||
259
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/gateway-video-model.ts
generated
vendored
Normal file
259
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/gateway-video-model.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,259 @@
|
|||
import type {
|
||||
Experimental_VideoModelV3,
|
||||
Experimental_VideoModelV3CallOptions,
|
||||
Experimental_VideoModelV3File,
|
||||
Experimental_VideoModelV3VideoData,
|
||||
SharedV3ProviderMetadata,
|
||||
SharedV3Warning,
|
||||
} from '@ai-sdk/provider';
|
||||
import { APICallError } from '@ai-sdk/provider';
|
||||
import {
|
||||
combineHeaders,
|
||||
convertUint8ArrayToBase64,
|
||||
createJsonErrorResponseHandler,
|
||||
parseJsonEventStream,
|
||||
postJsonToApi,
|
||||
resolve,
|
||||
type Resolvable,
|
||||
} from '@ai-sdk/provider-utils';
|
||||
import { z } from 'zod/v4';
|
||||
import type { GatewayConfig } from './gateway-config';
|
||||
import { asGatewayError } from './errors';
|
||||
import { parseAuthMethod } from './errors/parse-auth-method';
|
||||
|
||||
export class GatewayVideoModel implements Experimental_VideoModelV3 {
|
||||
readonly specificationVersion = 'v3' as const;
|
||||
// Set a very large number to prevent client-side splitting of requests
|
||||
readonly maxVideosPerCall = Number.MAX_SAFE_INTEGER;
|
||||
|
||||
constructor(
|
||||
readonly modelId: string,
|
||||
private readonly config: GatewayConfig & {
|
||||
provider: string;
|
||||
o11yHeaders: Resolvable<Record<string, string>>;
|
||||
},
|
||||
) {}
|
||||
|
||||
get provider(): string {
|
||||
return this.config.provider;
|
||||
}
|
||||
|
||||
async doGenerate({
|
||||
prompt,
|
||||
n,
|
||||
aspectRatio,
|
||||
resolution,
|
||||
duration,
|
||||
fps,
|
||||
seed,
|
||||
image,
|
||||
providerOptions,
|
||||
headers,
|
||||
abortSignal,
|
||||
}: Experimental_VideoModelV3CallOptions): Promise<{
|
||||
videos: Array<Experimental_VideoModelV3VideoData>;
|
||||
warnings: Array<SharedV3Warning>;
|
||||
providerMetadata?: SharedV3ProviderMetadata;
|
||||
response: {
|
||||
timestamp: Date;
|
||||
modelId: string;
|
||||
headers: Record<string, string> | undefined;
|
||||
};
|
||||
}> {
|
||||
const resolvedHeaders = await resolve(this.config.headers());
|
||||
try {
|
||||
const { responseHeaders, value: responseBody } = await postJsonToApi({
|
||||
url: this.getUrl(),
|
||||
headers: combineHeaders(
|
||||
resolvedHeaders,
|
||||
headers ?? {},
|
||||
this.getModelConfigHeaders(),
|
||||
await resolve(this.config.o11yHeaders),
|
||||
{ accept: 'text/event-stream' },
|
||||
),
|
||||
body: {
|
||||
prompt,
|
||||
n,
|
||||
...(aspectRatio && { aspectRatio }),
|
||||
...(resolution && { resolution }),
|
||||
...(duration && { duration }),
|
||||
...(fps && { fps }),
|
||||
...(seed && { seed }),
|
||||
...(providerOptions && { providerOptions }),
|
||||
...(image && { image: maybeEncodeVideoFile(image) }),
|
||||
},
|
||||
successfulResponseHandler: async ({
|
||||
response,
|
||||
url,
|
||||
requestBodyValues,
|
||||
}: {
|
||||
url: string;
|
||||
requestBodyValues: unknown;
|
||||
response: Response;
|
||||
}) => {
|
||||
if (response.body == null) {
|
||||
throw new APICallError({
|
||||
message: 'SSE response body is empty',
|
||||
url,
|
||||
requestBodyValues,
|
||||
statusCode: response.status,
|
||||
});
|
||||
}
|
||||
|
||||
const eventStream = parseJsonEventStream({
|
||||
stream: response.body,
|
||||
schema: gatewayVideoEventSchema,
|
||||
});
|
||||
|
||||
const reader = eventStream.getReader();
|
||||
const { done, value: parseResult } = await reader.read();
|
||||
reader.releaseLock();
|
||||
|
||||
if (done || !parseResult) {
|
||||
throw new APICallError({
|
||||
message: 'SSE stream ended without a data event',
|
||||
url,
|
||||
requestBodyValues,
|
||||
statusCode: response.status,
|
||||
});
|
||||
}
|
||||
|
||||
if (!parseResult.success) {
|
||||
throw new APICallError({
|
||||
message: 'Failed to parse video SSE event',
|
||||
cause: parseResult.error,
|
||||
url,
|
||||
requestBodyValues,
|
||||
statusCode: response.status,
|
||||
});
|
||||
}
|
||||
|
||||
const event = parseResult.value;
|
||||
|
||||
if (event.type === 'error') {
|
||||
throw new APICallError({
|
||||
message: event.message,
|
||||
statusCode: event.statusCode,
|
||||
url,
|
||||
requestBodyValues,
|
||||
responseHeaders: Object.fromEntries([...response.headers]),
|
||||
responseBody: JSON.stringify(event),
|
||||
data: {
|
||||
error: {
|
||||
message: event.message,
|
||||
type: event.errorType,
|
||||
param: event.param,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// event.type === 'result'
|
||||
return {
|
||||
value: {
|
||||
videos: event.videos,
|
||||
warnings: event.warnings,
|
||||
providerMetadata: event.providerMetadata,
|
||||
},
|
||||
responseHeaders: Object.fromEntries([...response.headers]),
|
||||
};
|
||||
},
|
||||
failedResponseHandler: createJsonErrorResponseHandler({
|
||||
errorSchema: z.any(),
|
||||
errorToMessage: data => data,
|
||||
}),
|
||||
...(abortSignal && { abortSignal }),
|
||||
fetch: this.config.fetch,
|
||||
});
|
||||
|
||||
return {
|
||||
videos: responseBody.videos,
|
||||
warnings: responseBody.warnings ?? [],
|
||||
providerMetadata:
|
||||
responseBody.providerMetadata as SharedV3ProviderMetadata,
|
||||
response: {
|
||||
timestamp: new Date(),
|
||||
modelId: this.modelId,
|
||||
headers: responseHeaders,
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
throw await asGatewayError(error, await parseAuthMethod(resolvedHeaders));
|
||||
}
|
||||
}
|
||||
|
||||
private getUrl() {
|
||||
return `${this.config.baseURL}/video-model`;
|
||||
}
|
||||
|
||||
private getModelConfigHeaders() {
|
||||
return {
|
||||
'ai-video-model-specification-version': '3',
|
||||
'ai-model-id': this.modelId,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function maybeEncodeVideoFile(file: Experimental_VideoModelV3File) {
|
||||
if (file.type === 'file' && file.data instanceof Uint8Array) {
|
||||
return {
|
||||
...file,
|
||||
data: convertUint8ArrayToBase64(file.data),
|
||||
};
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
const providerMetadataEntrySchema = z
|
||||
.object({
|
||||
videos: z.array(z.unknown()).optional(),
|
||||
})
|
||||
.catchall(z.unknown());
|
||||
|
||||
const gatewayVideoDataSchema = z.union([
|
||||
z.object({
|
||||
type: z.literal('url'),
|
||||
url: z.string(),
|
||||
mediaType: z.string(),
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('base64'),
|
||||
data: z.string(),
|
||||
mediaType: z.string(),
|
||||
}),
|
||||
]);
|
||||
|
||||
const gatewayVideoWarningSchema = z.discriminatedUnion('type', [
|
||||
z.object({
|
||||
type: z.literal('unsupported'),
|
||||
feature: z.string(),
|
||||
details: z.string().optional(),
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('compatibility'),
|
||||
feature: z.string(),
|
||||
details: z.string().optional(),
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('other'),
|
||||
message: z.string(),
|
||||
}),
|
||||
]);
|
||||
|
||||
const gatewayVideoEventSchema = z.discriminatedUnion('type', [
|
||||
z.object({
|
||||
type: z.literal('result'),
|
||||
videos: z.array(gatewayVideoDataSchema),
|
||||
warnings: z.array(gatewayVideoWarningSchema).optional(),
|
||||
providerMetadata: z
|
||||
.record(z.string(), providerMetadataEntrySchema)
|
||||
.optional(),
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('error'),
|
||||
message: z.string(),
|
||||
errorType: z.string(),
|
||||
statusCode: z.number(),
|
||||
param: z.unknown().nullable(),
|
||||
}),
|
||||
]);
|
||||
32
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/index.ts
generated
vendored
Normal file
32
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/index.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
export type { GatewayModelId } from './gateway-language-model-settings';
|
||||
export type { GatewayVideoModelId } from './gateway-video-model-settings';
|
||||
export type {
|
||||
GatewayLanguageModelEntry,
|
||||
GatewayLanguageModelSpecification,
|
||||
} from './gateway-model-entry';
|
||||
export type { GatewayCreditsResponse } from './gateway-fetch-metadata';
|
||||
export type { GatewayLanguageModelEntry as GatewayModelEntry } from './gateway-model-entry';
|
||||
export {
|
||||
createGatewayProvider,
|
||||
createGatewayProvider as createGateway,
|
||||
gateway,
|
||||
} from './gateway-provider';
|
||||
export type {
|
||||
GatewayProvider,
|
||||
GatewayProviderSettings,
|
||||
} from './gateway-provider';
|
||||
export type {
|
||||
GatewayLanguageModelOptions,
|
||||
/** @deprecated Use `GatewayLanguageModelOptions` instead. */
|
||||
GatewayLanguageModelOptions as GatewayProviderOptions,
|
||||
} from './gateway-provider-options';
|
||||
export {
|
||||
GatewayError,
|
||||
GatewayAuthenticationError,
|
||||
GatewayInvalidRequestError,
|
||||
GatewayRateLimitError,
|
||||
GatewayModelNotFoundError,
|
||||
GatewayInternalServerError,
|
||||
GatewayResponseError,
|
||||
} from './errors';
|
||||
export type { GatewayErrorResponse } from './errors';
|
||||
295
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/tool/parallel-search.ts
generated
vendored
Normal file
295
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/tool/parallel-search.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,295 @@
|
|||
import {
|
||||
createProviderToolFactoryWithOutputSchema,
|
||||
lazySchema,
|
||||
zodSchema,
|
||||
} from '@ai-sdk/provider-utils';
|
||||
import { z } from 'zod';
|
||||
|
||||
export interface ParallelSearchSourcePolicy {
|
||||
/**
|
||||
* List of domains to include in search results.
|
||||
* Example: ['wikipedia.org', 'nature.com']
|
||||
*/
|
||||
includeDomains?: string[];
|
||||
|
||||
/**
|
||||
* List of domains to exclude from search results.
|
||||
* Example: ['reddit.com', 'twitter.com']
|
||||
*/
|
||||
excludeDomains?: string[];
|
||||
|
||||
/**
|
||||
* Only include results published after this date (ISO 8601 format).
|
||||
* Example: '2024-01-01'
|
||||
*/
|
||||
afterDate?: string;
|
||||
}
|
||||
|
||||
export interface ParallelSearchExcerpts {
|
||||
/**
|
||||
* Maximum characters per result.
|
||||
*/
|
||||
maxCharsPerResult?: number;
|
||||
|
||||
/**
|
||||
* Maximum total characters across all results.
|
||||
*/
|
||||
maxCharsTotal?: number;
|
||||
}
|
||||
|
||||
export interface ParallelSearchFetchPolicy {
|
||||
/**
|
||||
* Maximum age in seconds for cached content.
|
||||
* Set to 0 to always fetch fresh content.
|
||||
*/
|
||||
maxAgeSeconds?: number;
|
||||
}
|
||||
|
||||
export interface ParallelSearchConfig {
|
||||
/**
|
||||
* Mode preset for different use cases:
|
||||
* - "one-shot": Comprehensive results with longer excerpts for single-response answers (default)
|
||||
* - "agentic": Concise, token-efficient results for multi-step agentic workflows
|
||||
*/
|
||||
mode?: 'one-shot' | 'agentic';
|
||||
|
||||
/**
|
||||
* Default maximum number of results to return (1-20).
|
||||
* Defaults to 10 if not specified.
|
||||
*/
|
||||
maxResults?: number;
|
||||
|
||||
/**
|
||||
* Default source policy for controlling which domains to include/exclude.
|
||||
*/
|
||||
sourcePolicy?: ParallelSearchSourcePolicy;
|
||||
|
||||
/**
|
||||
* Default excerpt configuration for controlling result length.
|
||||
*/
|
||||
excerpts?: ParallelSearchExcerpts;
|
||||
|
||||
/**
|
||||
* Default fetch policy for controlling content freshness.
|
||||
*/
|
||||
fetchPolicy?: ParallelSearchFetchPolicy;
|
||||
}
|
||||
|
||||
export interface ParallelSearchResult {
|
||||
/** URL of the search result */
|
||||
url: string;
|
||||
/** Title of the search result */
|
||||
title: string;
|
||||
/** Extracted text excerpt/content from the page */
|
||||
excerpt: string;
|
||||
/** Publication date of the content (may be null) */
|
||||
publishDate?: string | null;
|
||||
/** Relevance score for the result */
|
||||
relevanceScore?: number;
|
||||
}
|
||||
|
||||
export interface ParallelSearchResponse {
|
||||
/** Unique identifier for this search request */
|
||||
searchId: string;
|
||||
/** Array of search results */
|
||||
results: ParallelSearchResult[];
|
||||
}
|
||||
|
||||
export interface ParallelSearchError {
|
||||
/** Error type */
|
||||
error:
|
||||
| 'api_error'
|
||||
| 'rate_limit'
|
||||
| 'timeout'
|
||||
| 'invalid_input'
|
||||
| 'configuration_error'
|
||||
| 'unknown';
|
||||
/** HTTP status code if applicable */
|
||||
statusCode?: number;
|
||||
/** Human-readable error message */
|
||||
message: string;
|
||||
}
|
||||
|
||||
export interface ParallelSearchInput {
|
||||
/**
|
||||
* Natural-language description of the web research goal.
|
||||
* Include source or freshness guidance and broader context from the task.
|
||||
* Maximum 5000 characters.
|
||||
*/
|
||||
objective: string;
|
||||
|
||||
/**
|
||||
* Optional search queries to supplement the objective.
|
||||
* Maximum 200 characters per query.
|
||||
*/
|
||||
search_queries?: string[];
|
||||
|
||||
/**
|
||||
* Mode preset for different use cases:
|
||||
* - "one-shot": Comprehensive results with longer excerpts
|
||||
* - "agentic": Concise, token-efficient results for multi-step workflows
|
||||
*/
|
||||
mode?: 'one-shot' | 'agentic';
|
||||
|
||||
/**
|
||||
* Maximum number of results to return (1-20).
|
||||
* Defaults to 10 if not specified.
|
||||
*/
|
||||
max_results?: number;
|
||||
|
||||
/**
|
||||
* Source policy for controlling which domains to include/exclude.
|
||||
*/
|
||||
source_policy?: {
|
||||
include_domains?: string[];
|
||||
exclude_domains?: string[];
|
||||
after_date?: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* Excerpt configuration for controlling result length.
|
||||
*/
|
||||
excerpts?: {
|
||||
max_chars_per_result?: number;
|
||||
max_chars_total?: number;
|
||||
};
|
||||
|
||||
/**
|
||||
* Fetch policy for controlling content freshness.
|
||||
*/
|
||||
fetch_policy?: {
|
||||
max_age_seconds?: number;
|
||||
};
|
||||
}
|
||||
|
||||
export type ParallelSearchOutput = ParallelSearchResponse | ParallelSearchError;
|
||||
|
||||
const parallelSearchInputSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.object({
|
||||
objective: z
|
||||
.string()
|
||||
.describe(
|
||||
'Natural-language description of the web research goal, including source or freshness guidance and broader context from the task. Maximum 5000 characters.',
|
||||
),
|
||||
|
||||
search_queries: z
|
||||
.array(z.string())
|
||||
.optional()
|
||||
.describe(
|
||||
'Optional search queries to supplement the objective. Maximum 200 characters per query.',
|
||||
),
|
||||
|
||||
mode: z
|
||||
.enum(['one-shot', 'agentic'])
|
||||
.optional()
|
||||
.describe(
|
||||
'Mode preset: "one-shot" for comprehensive results with longer excerpts (default), "agentic" for concise, token-efficient results for multi-step workflows.',
|
||||
),
|
||||
|
||||
max_results: z
|
||||
.number()
|
||||
.optional()
|
||||
.describe(
|
||||
'Maximum number of results to return (1-20). Defaults to 10 if not specified.',
|
||||
),
|
||||
|
||||
source_policy: z
|
||||
.object({
|
||||
include_domains: z
|
||||
.array(z.string())
|
||||
.optional()
|
||||
.describe('List of domains to include in search results.'),
|
||||
exclude_domains: z
|
||||
.array(z.string())
|
||||
.optional()
|
||||
.describe('List of domains to exclude from search results.'),
|
||||
after_date: z
|
||||
.string()
|
||||
.optional()
|
||||
.describe(
|
||||
'Only include results published after this date (ISO 8601 format).',
|
||||
),
|
||||
})
|
||||
.optional()
|
||||
.describe(
|
||||
'Source policy for controlling which domains to include/exclude and freshness.',
|
||||
),
|
||||
|
||||
excerpts: z
|
||||
.object({
|
||||
max_chars_per_result: z
|
||||
.number()
|
||||
.optional()
|
||||
.describe('Maximum characters per result.'),
|
||||
max_chars_total: z
|
||||
.number()
|
||||
.optional()
|
||||
.describe('Maximum total characters across all results.'),
|
||||
})
|
||||
.optional()
|
||||
.describe('Excerpt configuration for controlling result length.'),
|
||||
|
||||
fetch_policy: z
|
||||
.object({
|
||||
max_age_seconds: z
|
||||
.number()
|
||||
.optional()
|
||||
.describe(
|
||||
'Maximum age in seconds for cached content. Set to 0 to always fetch fresh content.',
|
||||
),
|
||||
})
|
||||
.optional()
|
||||
.describe('Fetch policy for controlling content freshness.'),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
const parallelSearchOutputSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.union([
|
||||
// Success response
|
||||
z.object({
|
||||
searchId: z.string(),
|
||||
results: z.array(
|
||||
z.object({
|
||||
url: z.string(),
|
||||
title: z.string(),
|
||||
excerpt: z.string(),
|
||||
publishDate: z.string().nullable().optional(),
|
||||
relevanceScore: z.number().optional(),
|
||||
}),
|
||||
),
|
||||
}),
|
||||
// Error response
|
||||
z.object({
|
||||
error: z.enum([
|
||||
'api_error',
|
||||
'rate_limit',
|
||||
'timeout',
|
||||
'invalid_input',
|
||||
'configuration_error',
|
||||
'unknown',
|
||||
]),
|
||||
statusCode: z.number().optional(),
|
||||
message: z.string(),
|
||||
}),
|
||||
]),
|
||||
),
|
||||
);
|
||||
|
||||
export const parallelSearchToolFactory =
|
||||
createProviderToolFactoryWithOutputSchema<
|
||||
ParallelSearchInput,
|
||||
ParallelSearchOutput,
|
||||
ParallelSearchConfig
|
||||
>({
|
||||
id: 'gateway.parallel_search',
|
||||
inputSchema: parallelSearchInputSchema,
|
||||
outputSchema: parallelSearchOutputSchema,
|
||||
});
|
||||
|
||||
export const parallelSearch = (
|
||||
config: ParallelSearchConfig = {},
|
||||
): ReturnType<typeof parallelSearchToolFactory> =>
|
||||
parallelSearchToolFactory(config);
|
||||
294
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/tool/perplexity-search.ts
generated
vendored
Normal file
294
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/tool/perplexity-search.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,294 @@
|
|||
import {
|
||||
createProviderToolFactoryWithOutputSchema,
|
||||
lazySchema,
|
||||
zodSchema,
|
||||
} from '@ai-sdk/provider-utils';
|
||||
import { z } from 'zod';
|
||||
|
||||
export interface PerplexitySearchConfig {
|
||||
/**
|
||||
* Default maximum number of search results to return (1-20, default: 10).
|
||||
*/
|
||||
maxResults?: number;
|
||||
|
||||
/**
|
||||
* Default maximum tokens to extract per search result page (256-2048, default: 2048).
|
||||
*/
|
||||
maxTokensPerPage?: number;
|
||||
|
||||
/**
|
||||
* Default maximum total tokens across all search results (default: 25000, max: 1000000).
|
||||
*/
|
||||
maxTokens?: number;
|
||||
|
||||
/**
|
||||
* Default two-letter ISO 3166-1 alpha-2 country code for regional search results.
|
||||
* Examples: 'US', 'GB', 'FR'
|
||||
*/
|
||||
country?: string;
|
||||
|
||||
/**
|
||||
* Default list of domains to include or exclude from search results (max 20).
|
||||
* To include: ['nature.com', 'science.org']
|
||||
* To exclude: ['-example.com', '-spam.net']
|
||||
*/
|
||||
searchDomainFilter?: string[];
|
||||
|
||||
/**
|
||||
* Default list of ISO 639-1 language codes to filter results (max 10, lowercase).
|
||||
* Examples: ['en', 'fr', 'de']
|
||||
*/
|
||||
searchLanguageFilter?: string[];
|
||||
|
||||
/**
|
||||
* Default recency filter for results.
|
||||
* Cannot be combined with searchAfterDate/searchBeforeDate at runtime.
|
||||
*/
|
||||
searchRecencyFilter?: 'day' | 'week' | 'month' | 'year';
|
||||
}
|
||||
|
||||
export interface PerplexitySearchResult {
|
||||
/** Title of the search result */
|
||||
title: string;
|
||||
/** URL of the search result */
|
||||
url: string;
|
||||
/** Text snippet/preview of the content */
|
||||
snippet: string;
|
||||
/** Publication date of the content */
|
||||
date?: string;
|
||||
/** Last updated date of the content */
|
||||
lastUpdated?: string;
|
||||
}
|
||||
|
||||
export interface PerplexitySearchResponse {
|
||||
/** Array of search results */
|
||||
results: PerplexitySearchResult[];
|
||||
/** Unique identifier for this search request */
|
||||
id: string;
|
||||
}
|
||||
|
||||
export interface PerplexitySearchError {
|
||||
/** Error type */
|
||||
error: 'api_error' | 'rate_limit' | 'timeout' | 'invalid_input' | 'unknown';
|
||||
/** HTTP status code if applicable */
|
||||
statusCode?: number;
|
||||
/** Human-readable error message */
|
||||
message: string;
|
||||
}
|
||||
|
||||
export interface PerplexitySearchInput {
|
||||
/**
|
||||
* Search query (string) or multiple queries (array of up to 5 strings).
|
||||
* Multi-query searches return combined results from all queries.
|
||||
*/
|
||||
query: string | string[];
|
||||
|
||||
/**
|
||||
* Maximum number of search results to return (1-20, default: 10).
|
||||
*/
|
||||
max_results?: number;
|
||||
|
||||
/**
|
||||
* Maximum number of tokens to extract per search result page (256-2048, default: 2048).
|
||||
*/
|
||||
max_tokens_per_page?: number;
|
||||
|
||||
/**
|
||||
* Maximum total tokens across all search results (default: 25000, max: 1000000).
|
||||
*/
|
||||
max_tokens?: number;
|
||||
|
||||
/**
|
||||
* Two-letter ISO 3166-1 alpha-2 country code for regional search results.
|
||||
* Examples: 'US', 'GB', 'FR'
|
||||
*/
|
||||
country?: string;
|
||||
|
||||
/**
|
||||
* List of domains to include or exclude from search results (max 20).
|
||||
* To include: ['nature.com', 'science.org']
|
||||
* To exclude: ['-example.com', '-spam.net']
|
||||
*/
|
||||
search_domain_filter?: string[];
|
||||
|
||||
/**
|
||||
* List of ISO 639-1 language codes to filter results (max 10, lowercase).
|
||||
* Examples: ['en', 'fr', 'de']
|
||||
*/
|
||||
search_language_filter?: string[];
|
||||
|
||||
/**
|
||||
* Include only results published after this date.
|
||||
* Format: 'MM/DD/YYYY' (e.g., '3/1/2025')
|
||||
* Cannot be used with search_recency_filter.
|
||||
*/
|
||||
search_after_date?: string;
|
||||
|
||||
/**
|
||||
* Include only results published before this date.
|
||||
* Format: 'MM/DD/YYYY' (e.g., '3/15/2025')
|
||||
* Cannot be used with search_recency_filter.
|
||||
*/
|
||||
search_before_date?: string;
|
||||
|
||||
/**
|
||||
* Include only results last updated after this date.
|
||||
* Format: 'MM/DD/YYYY' (e.g., '3/1/2025')
|
||||
* Cannot be used with search_recency_filter.
|
||||
*/
|
||||
last_updated_after_filter?: string;
|
||||
|
||||
/**
|
||||
* Include only results last updated before this date.
|
||||
* Format: 'MM/DD/YYYY' (e.g., '3/15/2025')
|
||||
* Cannot be used with search_recency_filter.
|
||||
*/
|
||||
last_updated_before_filter?: string;
|
||||
|
||||
/**
|
||||
* Filter results by relative time period.
|
||||
* Cannot be used with search_after_date or search_before_date.
|
||||
*/
|
||||
search_recency_filter?: 'day' | 'week' | 'month' | 'year';
|
||||
}
|
||||
|
||||
export type PerplexitySearchOutput =
|
||||
| PerplexitySearchResponse
|
||||
| PerplexitySearchError;
|
||||
|
||||
const perplexitySearchInputSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.object({
|
||||
query: z
|
||||
.union([z.string(), z.array(z.string())])
|
||||
.describe(
|
||||
'Search query (string) or multiple queries (array of up to 5 strings). Multi-query searches return combined results from all queries.',
|
||||
),
|
||||
|
||||
max_results: z
|
||||
.number()
|
||||
.optional()
|
||||
.describe(
|
||||
'Maximum number of search results to return (1-20, default: 10)',
|
||||
),
|
||||
|
||||
max_tokens_per_page: z
|
||||
.number()
|
||||
.optional()
|
||||
.describe(
|
||||
'Maximum number of tokens to extract per search result page (256-2048, default: 2048)',
|
||||
),
|
||||
|
||||
max_tokens: z
|
||||
.number()
|
||||
.optional()
|
||||
.describe(
|
||||
'Maximum total tokens across all search results (default: 25000, max: 1000000)',
|
||||
),
|
||||
|
||||
country: z
|
||||
.string()
|
||||
.optional()
|
||||
.describe(
|
||||
"Two-letter ISO 3166-1 alpha-2 country code for regional search results (e.g., 'US', 'GB', 'FR')",
|
||||
),
|
||||
|
||||
search_domain_filter: z
|
||||
.array(z.string())
|
||||
.optional()
|
||||
.describe(
|
||||
"List of domains to include or exclude from search results (max 20). To include: ['nature.com', 'science.org']. To exclude: ['-example.com', '-spam.net']",
|
||||
),
|
||||
|
||||
search_language_filter: z
|
||||
.array(z.string())
|
||||
.optional()
|
||||
.describe(
|
||||
"List of ISO 639-1 language codes to filter results (max 10, lowercase). Examples: ['en', 'fr', 'de']",
|
||||
),
|
||||
|
||||
search_after_date: z
|
||||
.string()
|
||||
.optional()
|
||||
.describe(
|
||||
"Include only results published after this date. Format: 'MM/DD/YYYY' (e.g., '3/1/2025'). Cannot be used with search_recency_filter.",
|
||||
),
|
||||
|
||||
search_before_date: z
|
||||
.string()
|
||||
.optional()
|
||||
.describe(
|
||||
"Include only results published before this date. Format: 'MM/DD/YYYY' (e.g., '3/15/2025'). Cannot be used with search_recency_filter.",
|
||||
),
|
||||
|
||||
last_updated_after_filter: z
|
||||
.string()
|
||||
.optional()
|
||||
.describe(
|
||||
"Include only results last updated after this date. Format: 'MM/DD/YYYY' (e.g., '3/1/2025'). Cannot be used with search_recency_filter.",
|
||||
),
|
||||
|
||||
last_updated_before_filter: z
|
||||
.string()
|
||||
.optional()
|
||||
.describe(
|
||||
"Include only results last updated before this date. Format: 'MM/DD/YYYY' (e.g., '3/15/2025'). Cannot be used with search_recency_filter.",
|
||||
),
|
||||
|
||||
search_recency_filter: z
|
||||
.enum(['day', 'week', 'month', 'year'])
|
||||
.optional()
|
||||
.describe(
|
||||
'Filter results by relative time period. Cannot be used with search_after_date or search_before_date.',
|
||||
),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
const perplexitySearchOutputSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.union([
|
||||
// Success response
|
||||
z.object({
|
||||
results: z.array(
|
||||
z.object({
|
||||
title: z.string(),
|
||||
url: z.string(),
|
||||
snippet: z.string(),
|
||||
date: z.string().optional(),
|
||||
lastUpdated: z.string().optional(),
|
||||
}),
|
||||
),
|
||||
id: z.string(),
|
||||
}),
|
||||
// Error response
|
||||
z.object({
|
||||
error: z.enum([
|
||||
'api_error',
|
||||
'rate_limit',
|
||||
'timeout',
|
||||
'invalid_input',
|
||||
'unknown',
|
||||
]),
|
||||
statusCode: z.number().optional(),
|
||||
message: z.string(),
|
||||
}),
|
||||
]),
|
||||
),
|
||||
);
|
||||
|
||||
export const perplexitySearchToolFactory =
|
||||
createProviderToolFactoryWithOutputSchema<
|
||||
PerplexitySearchInput,
|
||||
PerplexitySearchOutput,
|
||||
PerplexitySearchConfig
|
||||
>({
|
||||
id: 'gateway.perplexity_search',
|
||||
inputSchema: perplexitySearchInputSchema,
|
||||
outputSchema: perplexitySearchOutputSchema,
|
||||
});
|
||||
|
||||
export const perplexitySearch = (
|
||||
config: PerplexitySearchConfig = {},
|
||||
): ReturnType<typeof perplexitySearchToolFactory> =>
|
||||
perplexitySearchToolFactory(config);
|
||||
6
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/vercel-environment.ts
generated
vendored
Normal file
6
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/vercel-environment.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
import { getContext } from '@vercel/oidc';
|
||||
export { getVercelOidcToken } from '@vercel/oidc';
|
||||
|
||||
export async function getVercelRequestId(): Promise<string | undefined> {
|
||||
return getContext().headers?.['x-vercel-id'];
|
||||
}
|
||||
6
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/version.ts
generated
vendored
Normal file
6
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/gateway/src/version.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
// Version string of this package injected at build time.
|
||||
declare const __PACKAGE_VERSION__: string | undefined;
|
||||
export const VERSION: string =
|
||||
typeof __PACKAGE_VERSION__ !== 'undefined'
|
||||
? __PACKAGE_VERSION__
|
||||
: '0.0.0-test';
|
||||
3149
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/openai/CHANGELOG.md
generated
vendored
Normal file
3149
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/openai/CHANGELOG.md
generated
vendored
Normal file
File diff suppressed because it is too large
Load diff
13
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/openai/LICENSE
generated
vendored
Normal file
13
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/openai/LICENSE
generated
vendored
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
Copyright 2023 Vercel, Inc.
|
||||
|
||||
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.
|
||||
44
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/openai/README.md
generated
vendored
Normal file
44
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/openai/README.md
generated
vendored
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
# AI SDK - OpenAI Provider
|
||||
|
||||
The **[OpenAI provider](https://ai-sdk.dev/providers/ai-sdk-providers/openai)** for the [AI SDK](https://ai-sdk.dev/docs)
|
||||
contains language model support for the OpenAI chat and completion APIs and embedding model support for the OpenAI embeddings API.
|
||||
|
||||
## Setup
|
||||
|
||||
The OpenAI provider is available in the `@ai-sdk/openai` module. You can install it with
|
||||
|
||||
```bash
|
||||
npm i @ai-sdk/openai
|
||||
```
|
||||
|
||||
## Skill for Coding Agents
|
||||
|
||||
If you use coding agents such as Claude Code or Cursor, we highly recommend adding the AI SDK skill to your repository:
|
||||
|
||||
```shell
|
||||
npx skills add vercel/ai
|
||||
```
|
||||
|
||||
## Provider Instance
|
||||
|
||||
You can import the default provider instance `openai` from `@ai-sdk/openai`:
|
||||
|
||||
```ts
|
||||
import { openai } from '@ai-sdk/openai';
|
||||
```
|
||||
|
||||
## Example
|
||||
|
||||
```ts
|
||||
import { openai } from '@ai-sdk/openai';
|
||||
import { generateText } from 'ai';
|
||||
|
||||
const { text } = await generateText({
|
||||
model: openai('gpt-5-mini'),
|
||||
prompt: 'Write a vegetarian lasagna recipe for 4 people.',
|
||||
});
|
||||
```
|
||||
|
||||
## Documentation
|
||||
|
||||
Please check out the **[OpenAI provider documentation](https://ai-sdk.dev/providers/ai-sdk-providers/openai)** for more information.
|
||||
2540
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/openai/docs/03-openai.mdx
generated
vendored
Normal file
2540
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/openai/docs/03-openai.mdx
generated
vendored
Normal file
File diff suppressed because it is too large
Load diff
1
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/openai/internal.d.ts
generated
vendored
Normal file
1
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/openai/internal.d.ts
generated
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
export * from './dist/internal';
|
||||
81
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/openai/package.json
generated
vendored
Normal file
81
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/openai/package.json
generated
vendored
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
{
|
||||
"name": "@ai-sdk/openai",
|
||||
"version": "3.0.48",
|
||||
"license": "Apache-2.0",
|
||||
"sideEffects": false,
|
||||
"main": "./dist/index.js",
|
||||
"module": "./dist/index.mjs",
|
||||
"types": "./dist/index.d.ts",
|
||||
"files": [
|
||||
"dist/**/*",
|
||||
"docs/**/*",
|
||||
"src",
|
||||
"!src/**/*.test.ts",
|
||||
"!src/**/*.test-d.ts",
|
||||
"!src/**/__snapshots__",
|
||||
"!src/**/__fixtures__",
|
||||
"CHANGELOG.md",
|
||||
"README.md",
|
||||
"internal.d.ts"
|
||||
],
|
||||
"directories": {
|
||||
"doc": "./docs"
|
||||
},
|
||||
"exports": {
|
||||
"./package.json": "./package.json",
|
||||
".": {
|
||||
"types": "./dist/index.d.ts",
|
||||
"import": "./dist/index.mjs",
|
||||
"require": "./dist/index.js"
|
||||
},
|
||||
"./internal": {
|
||||
"types": "./dist/internal/index.d.ts",
|
||||
"import": "./dist/internal/index.mjs",
|
||||
"module": "./dist/internal/index.mjs",
|
||||
"require": "./dist/internal/index.js"
|
||||
}
|
||||
},
|
||||
"dependencies": {
|
||||
"@ai-sdk/provider": "3.0.8",
|
||||
"@ai-sdk/provider-utils": "4.0.21"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "20.17.24",
|
||||
"tsup": "^8",
|
||||
"typescript": "5.8.3",
|
||||
"zod": "3.25.76",
|
||||
"@vercel/ai-tsconfig": "0.0.0",
|
||||
"@ai-sdk/test-server": "1.0.3"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"zod": "^3.25.76 || ^4.1.8"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
"homepage": "https://ai-sdk.dev/docs",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/vercel/ai.git"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/vercel/ai/issues"
|
||||
},
|
||||
"keywords": [
|
||||
"ai"
|
||||
],
|
||||
"scripts": {
|
||||
"build": "pnpm clean && tsup --tsconfig tsconfig.build.json",
|
||||
"build:watch": "pnpm clean && tsup --watch",
|
||||
"clean": "del-cli dist docs *.tsbuildinfo",
|
||||
"type-check": "tsc --build",
|
||||
"test": "pnpm test:node && pnpm test:edge",
|
||||
"test:update": "pnpm test:node -u",
|
||||
"test:watch": "vitest --config vitest.node.config.js",
|
||||
"test:edge": "vitest --config vitest.edge.config.js --run",
|
||||
"test:node": "vitest --config vitest.node.config.js --run"
|
||||
}
|
||||
}
|
||||
57
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/openai/src/chat/convert-openai-chat-usage.ts
generated
vendored
Normal file
57
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/openai/src/chat/convert-openai-chat-usage.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
import { LanguageModelV3Usage } from '@ai-sdk/provider';
|
||||
|
||||
export type OpenAIChatUsage = {
|
||||
prompt_tokens?: number | null;
|
||||
completion_tokens?: number | null;
|
||||
total_tokens?: number | null;
|
||||
prompt_tokens_details?: {
|
||||
cached_tokens?: number | null;
|
||||
} | null;
|
||||
completion_tokens_details?: {
|
||||
reasoning_tokens?: number | null;
|
||||
accepted_prediction_tokens?: number | null;
|
||||
rejected_prediction_tokens?: number | null;
|
||||
} | null;
|
||||
};
|
||||
|
||||
export function convertOpenAIChatUsage(
|
||||
usage: OpenAIChatUsage | undefined | null,
|
||||
): LanguageModelV3Usage {
|
||||
if (usage == null) {
|
||||
return {
|
||||
inputTokens: {
|
||||
total: undefined,
|
||||
noCache: undefined,
|
||||
cacheRead: undefined,
|
||||
cacheWrite: undefined,
|
||||
},
|
||||
outputTokens: {
|
||||
total: undefined,
|
||||
text: undefined,
|
||||
reasoning: undefined,
|
||||
},
|
||||
raw: undefined,
|
||||
};
|
||||
}
|
||||
|
||||
const promptTokens = usage.prompt_tokens ?? 0;
|
||||
const completionTokens = usage.completion_tokens ?? 0;
|
||||
const cachedTokens = usage.prompt_tokens_details?.cached_tokens ?? 0;
|
||||
const reasoningTokens =
|
||||
usage.completion_tokens_details?.reasoning_tokens ?? 0;
|
||||
|
||||
return {
|
||||
inputTokens: {
|
||||
total: promptTokens,
|
||||
noCache: promptTokens - cachedTokens,
|
||||
cacheRead: cachedTokens,
|
||||
cacheWrite: undefined,
|
||||
},
|
||||
outputTokens: {
|
||||
total: completionTokens,
|
||||
text: completionTokens - reasoningTokens,
|
||||
reasoning: reasoningTokens,
|
||||
},
|
||||
raw: usage,
|
||||
};
|
||||
}
|
||||
225
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/openai/src/chat/convert-to-openai-chat-messages.ts
generated
vendored
Normal file
225
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/openai/src/chat/convert-to-openai-chat-messages.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,225 @@
|
|||
import {
|
||||
SharedV3Warning,
|
||||
LanguageModelV3Prompt,
|
||||
UnsupportedFunctionalityError,
|
||||
} from '@ai-sdk/provider';
|
||||
import { OpenAIChatPrompt } from './openai-chat-prompt';
|
||||
import { convertToBase64 } from '@ai-sdk/provider-utils';
|
||||
|
||||
export function convertToOpenAIChatMessages({
|
||||
prompt,
|
||||
systemMessageMode = 'system',
|
||||
}: {
|
||||
prompt: LanguageModelV3Prompt;
|
||||
systemMessageMode?: 'system' | 'developer' | 'remove';
|
||||
}): {
|
||||
messages: OpenAIChatPrompt;
|
||||
warnings: Array<SharedV3Warning>;
|
||||
} {
|
||||
const messages: OpenAIChatPrompt = [];
|
||||
const warnings: Array<SharedV3Warning> = [];
|
||||
|
||||
for (const { role, content } of prompt) {
|
||||
switch (role) {
|
||||
case 'system': {
|
||||
switch (systemMessageMode) {
|
||||
case 'system': {
|
||||
messages.push({ role: 'system', content });
|
||||
break;
|
||||
}
|
||||
case 'developer': {
|
||||
messages.push({ role: 'developer', content });
|
||||
break;
|
||||
}
|
||||
case 'remove': {
|
||||
warnings.push({
|
||||
type: 'other',
|
||||
message: 'system messages are removed for this model',
|
||||
});
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
const _exhaustiveCheck: never = systemMessageMode;
|
||||
throw new Error(
|
||||
`Unsupported system message mode: ${_exhaustiveCheck}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case 'user': {
|
||||
if (content.length === 1 && content[0].type === 'text') {
|
||||
messages.push({ role: 'user', content: content[0].text });
|
||||
break;
|
||||
}
|
||||
|
||||
messages.push({
|
||||
role: 'user',
|
||||
content: content.map((part, index) => {
|
||||
switch (part.type) {
|
||||
case 'text': {
|
||||
return { type: 'text', text: part.text };
|
||||
}
|
||||
case 'file': {
|
||||
if (part.mediaType.startsWith('image/')) {
|
||||
const mediaType =
|
||||
part.mediaType === 'image/*'
|
||||
? 'image/jpeg'
|
||||
: part.mediaType;
|
||||
|
||||
return {
|
||||
type: 'image_url',
|
||||
image_url: {
|
||||
url:
|
||||
part.data instanceof URL
|
||||
? part.data.toString()
|
||||
: `data:${mediaType};base64,${convertToBase64(part.data)}`,
|
||||
|
||||
// OpenAI specific extension: image detail
|
||||
detail: part.providerOptions?.openai?.imageDetail,
|
||||
},
|
||||
};
|
||||
} else if (part.mediaType.startsWith('audio/')) {
|
||||
if (part.data instanceof URL) {
|
||||
throw new UnsupportedFunctionalityError({
|
||||
functionality: 'audio file parts with URLs',
|
||||
});
|
||||
}
|
||||
|
||||
switch (part.mediaType) {
|
||||
case 'audio/wav': {
|
||||
return {
|
||||
type: 'input_audio',
|
||||
input_audio: {
|
||||
data: convertToBase64(part.data),
|
||||
format: 'wav',
|
||||
},
|
||||
};
|
||||
}
|
||||
case 'audio/mp3':
|
||||
case 'audio/mpeg': {
|
||||
return {
|
||||
type: 'input_audio',
|
||||
input_audio: {
|
||||
data: convertToBase64(part.data),
|
||||
format: 'mp3',
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
default: {
|
||||
throw new UnsupportedFunctionalityError({
|
||||
functionality: `audio content parts with media type ${part.mediaType}`,
|
||||
});
|
||||
}
|
||||
}
|
||||
} else if (part.mediaType === 'application/pdf') {
|
||||
if (part.data instanceof URL) {
|
||||
throw new UnsupportedFunctionalityError({
|
||||
functionality: 'PDF file parts with URLs',
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
type: 'file',
|
||||
file:
|
||||
typeof part.data === 'string' &&
|
||||
part.data.startsWith('file-')
|
||||
? { file_id: part.data }
|
||||
: {
|
||||
filename: part.filename ?? `part-${index}.pdf`,
|
||||
file_data: `data:application/pdf;base64,${convertToBase64(part.data)}`,
|
||||
},
|
||||
};
|
||||
} else {
|
||||
throw new UnsupportedFunctionalityError({
|
||||
functionality: `file part media type ${part.mediaType}`,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}),
|
||||
});
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 'assistant': {
|
||||
let text = '';
|
||||
const toolCalls: Array<{
|
||||
id: string;
|
||||
type: 'function';
|
||||
function: { name: string; arguments: string };
|
||||
}> = [];
|
||||
|
||||
for (const part of content) {
|
||||
switch (part.type) {
|
||||
case 'text': {
|
||||
text += part.text;
|
||||
break;
|
||||
}
|
||||
case 'tool-call': {
|
||||
toolCalls.push({
|
||||
id: part.toolCallId,
|
||||
type: 'function',
|
||||
function: {
|
||||
name: part.toolName,
|
||||
arguments: JSON.stringify(part.input),
|
||||
},
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
messages.push({
|
||||
role: 'assistant',
|
||||
content: text,
|
||||
tool_calls: toolCalls.length > 0 ? toolCalls : undefined,
|
||||
});
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 'tool': {
|
||||
for (const toolResponse of content) {
|
||||
if (toolResponse.type === 'tool-approval-response') {
|
||||
continue;
|
||||
}
|
||||
const output = toolResponse.output;
|
||||
|
||||
let contentValue: string;
|
||||
switch (output.type) {
|
||||
case 'text':
|
||||
case 'error-text':
|
||||
contentValue = output.value;
|
||||
break;
|
||||
case 'execution-denied':
|
||||
contentValue = output.reason ?? 'Tool execution denied.';
|
||||
break;
|
||||
case 'content':
|
||||
case 'json':
|
||||
case 'error-json':
|
||||
contentValue = JSON.stringify(output.value);
|
||||
break;
|
||||
}
|
||||
|
||||
messages.push({
|
||||
role: 'tool',
|
||||
tool_call_id: toolResponse.toolCallId,
|
||||
content: contentValue,
|
||||
});
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default: {
|
||||
const _exhaustiveCheck: never = role;
|
||||
throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { messages, warnings };
|
||||
}
|
||||
15
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/openai/src/chat/get-response-metadata.ts
generated
vendored
Normal file
15
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/openai/src/chat/get-response-metadata.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
export function getResponseMetadata({
|
||||
id,
|
||||
model,
|
||||
created,
|
||||
}: {
|
||||
id?: string | undefined | null;
|
||||
created?: number | undefined | null;
|
||||
model?: string | undefined | null;
|
||||
}) {
|
||||
return {
|
||||
id: id ?? undefined,
|
||||
modelId: model ?? undefined,
|
||||
timestamp: created ? new Date(created * 1000) : undefined,
|
||||
};
|
||||
}
|
||||
19
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/openai/src/chat/map-openai-finish-reason.ts
generated
vendored
Normal file
19
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/openai/src/chat/map-openai-finish-reason.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import { LanguageModelV3FinishReason } from '@ai-sdk/provider';
|
||||
|
||||
export function mapOpenAIFinishReason(
|
||||
finishReason: string | null | undefined,
|
||||
): LanguageModelV3FinishReason['unified'] {
|
||||
switch (finishReason) {
|
||||
case 'stop':
|
||||
return 'stop';
|
||||
case 'length':
|
||||
return 'length';
|
||||
case 'content_filter':
|
||||
return 'content-filter';
|
||||
case 'function_call':
|
||||
case 'tool_calls':
|
||||
return 'tool-calls';
|
||||
default:
|
||||
return 'other';
|
||||
}
|
||||
}
|
||||
198
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/openai/src/chat/openai-chat-api.ts
generated
vendored
Normal file
198
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/openai/src/chat/openai-chat-api.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,198 @@
|
|||
import { JSONSchema7 } from '@ai-sdk/provider';
|
||||
import { InferSchema, lazySchema, zodSchema } from '@ai-sdk/provider-utils';
|
||||
import { z } from 'zod/v4';
|
||||
import { openaiErrorDataSchema } from '../openai-error';
|
||||
|
||||
export interface OpenAIChatFunctionTool {
|
||||
type: 'function';
|
||||
function: {
|
||||
name: string;
|
||||
description: string | undefined;
|
||||
parameters: JSONSchema7;
|
||||
strict?: boolean;
|
||||
};
|
||||
}
|
||||
|
||||
export type OpenAIChatToolChoice =
|
||||
| 'auto'
|
||||
| 'none'
|
||||
| 'required'
|
||||
| { type: 'function'; function: { name: string } };
|
||||
|
||||
// limited version of the schema, focussed on what is needed for the implementation
|
||||
// this approach limits breakages when the API changes and increases efficiency
|
||||
export const openaiChatResponseSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.object({
|
||||
id: z.string().nullish(),
|
||||
created: z.number().nullish(),
|
||||
model: z.string().nullish(),
|
||||
choices: z.array(
|
||||
z.object({
|
||||
message: z.object({
|
||||
role: z.literal('assistant').nullish(),
|
||||
content: z.string().nullish(),
|
||||
tool_calls: z
|
||||
.array(
|
||||
z.object({
|
||||
id: z.string().nullish(),
|
||||
type: z.literal('function'),
|
||||
function: z.object({
|
||||
name: z.string(),
|
||||
arguments: z.string(),
|
||||
}),
|
||||
}),
|
||||
)
|
||||
.nullish(),
|
||||
annotations: z
|
||||
.array(
|
||||
z.object({
|
||||
type: z.literal('url_citation'),
|
||||
url_citation: z.object({
|
||||
start_index: z.number(),
|
||||
end_index: z.number(),
|
||||
url: z.string(),
|
||||
title: z.string(),
|
||||
}),
|
||||
}),
|
||||
)
|
||||
.nullish(),
|
||||
}),
|
||||
index: z.number(),
|
||||
logprobs: z
|
||||
.object({
|
||||
content: z
|
||||
.array(
|
||||
z.object({
|
||||
token: z.string(),
|
||||
logprob: z.number(),
|
||||
top_logprobs: z.array(
|
||||
z.object({
|
||||
token: z.string(),
|
||||
logprob: z.number(),
|
||||
}),
|
||||
),
|
||||
}),
|
||||
)
|
||||
.nullish(),
|
||||
})
|
||||
.nullish(),
|
||||
finish_reason: z.string().nullish(),
|
||||
}),
|
||||
),
|
||||
usage: z
|
||||
.object({
|
||||
prompt_tokens: z.number().nullish(),
|
||||
completion_tokens: z.number().nullish(),
|
||||
total_tokens: z.number().nullish(),
|
||||
prompt_tokens_details: z
|
||||
.object({
|
||||
cached_tokens: z.number().nullish(),
|
||||
})
|
||||
.nullish(),
|
||||
completion_tokens_details: z
|
||||
.object({
|
||||
reasoning_tokens: z.number().nullish(),
|
||||
accepted_prediction_tokens: z.number().nullish(),
|
||||
rejected_prediction_tokens: z.number().nullish(),
|
||||
})
|
||||
.nullish(),
|
||||
})
|
||||
.nullish(),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
// limited version of the schema, focussed on what is needed for the implementation
|
||||
// this approach limits breakages when the API changes and increases efficiency
|
||||
export const openaiChatChunkSchema = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.union([
|
||||
z.object({
|
||||
id: z.string().nullish(),
|
||||
created: z.number().nullish(),
|
||||
model: z.string().nullish(),
|
||||
choices: z.array(
|
||||
z.object({
|
||||
delta: z
|
||||
.object({
|
||||
role: z.enum(['assistant']).nullish(),
|
||||
content: z.string().nullish(),
|
||||
tool_calls: z
|
||||
.array(
|
||||
z.object({
|
||||
index: z.number(),
|
||||
id: z.string().nullish(),
|
||||
type: z.literal('function').nullish(),
|
||||
function: z.object({
|
||||
name: z.string().nullish(),
|
||||
arguments: z.string().nullish(),
|
||||
}),
|
||||
}),
|
||||
)
|
||||
.nullish(),
|
||||
annotations: z
|
||||
.array(
|
||||
z.object({
|
||||
type: z.literal('url_citation'),
|
||||
url_citation: z.object({
|
||||
start_index: z.number(),
|
||||
end_index: z.number(),
|
||||
url: z.string(),
|
||||
title: z.string(),
|
||||
}),
|
||||
}),
|
||||
)
|
||||
.nullish(),
|
||||
})
|
||||
.nullish(),
|
||||
logprobs: z
|
||||
.object({
|
||||
content: z
|
||||
.array(
|
||||
z.object({
|
||||
token: z.string(),
|
||||
logprob: z.number(),
|
||||
top_logprobs: z.array(
|
||||
z.object({
|
||||
token: z.string(),
|
||||
logprob: z.number(),
|
||||
}),
|
||||
),
|
||||
}),
|
||||
)
|
||||
.nullish(),
|
||||
})
|
||||
.nullish(),
|
||||
finish_reason: z.string().nullish(),
|
||||
index: z.number(),
|
||||
}),
|
||||
),
|
||||
usage: z
|
||||
.object({
|
||||
prompt_tokens: z.number().nullish(),
|
||||
completion_tokens: z.number().nullish(),
|
||||
total_tokens: z.number().nullish(),
|
||||
prompt_tokens_details: z
|
||||
.object({
|
||||
cached_tokens: z.number().nullish(),
|
||||
})
|
||||
.nullish(),
|
||||
completion_tokens_details: z
|
||||
.object({
|
||||
reasoning_tokens: z.number().nullish(),
|
||||
accepted_prediction_tokens: z.number().nullish(),
|
||||
rejected_prediction_tokens: z.number().nullish(),
|
||||
})
|
||||
.nullish(),
|
||||
})
|
||||
.nullish(),
|
||||
}),
|
||||
openaiErrorDataSchema,
|
||||
]),
|
||||
),
|
||||
);
|
||||
|
||||
export type OpenAIChatResponse = InferSchema<typeof openaiChatResponseSchema>;
|
||||
|
||||
export type OpenAIChatChunk = InferSchema<typeof openaiChatChunkSchema>;
|
||||
703
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/openai/src/chat/openai-chat-language-model.ts
generated
vendored
Normal file
703
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/openai/src/chat/openai-chat-language-model.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,703 @@
|
|||
import {
|
||||
InvalidResponseDataError,
|
||||
LanguageModelV3,
|
||||
LanguageModelV3CallOptions,
|
||||
LanguageModelV3Content,
|
||||
LanguageModelV3FinishReason,
|
||||
LanguageModelV3GenerateResult,
|
||||
LanguageModelV3StreamPart,
|
||||
LanguageModelV3StreamResult,
|
||||
SharedV3ProviderMetadata,
|
||||
SharedV3Warning,
|
||||
} from '@ai-sdk/provider';
|
||||
import {
|
||||
FetchFunction,
|
||||
ParseResult,
|
||||
combineHeaders,
|
||||
createEventSourceResponseHandler,
|
||||
createJsonResponseHandler,
|
||||
generateId,
|
||||
isParsableJson,
|
||||
parseProviderOptions,
|
||||
postJsonToApi,
|
||||
} from '@ai-sdk/provider-utils';
|
||||
import { openaiFailedResponseHandler } from '../openai-error';
|
||||
import { getOpenAILanguageModelCapabilities } from '../openai-language-model-capabilities';
|
||||
import {
|
||||
OpenAIChatUsage,
|
||||
convertOpenAIChatUsage,
|
||||
} from './convert-openai-chat-usage';
|
||||
import { convertToOpenAIChatMessages } from './convert-to-openai-chat-messages';
|
||||
import { getResponseMetadata } from './get-response-metadata';
|
||||
import { mapOpenAIFinishReason } from './map-openai-finish-reason';
|
||||
import {
|
||||
OpenAIChatChunk,
|
||||
openaiChatChunkSchema,
|
||||
openaiChatResponseSchema,
|
||||
} from './openai-chat-api';
|
||||
import {
|
||||
OpenAIChatModelId,
|
||||
openaiLanguageModelChatOptions,
|
||||
} from './openai-chat-options';
|
||||
import { prepareChatTools } from './openai-chat-prepare-tools';
|
||||
|
||||
type OpenAIChatConfig = {
|
||||
provider: string;
|
||||
headers: () => Record<string, string | undefined>;
|
||||
url: (options: { modelId: string; path: string }) => string;
|
||||
fetch?: FetchFunction;
|
||||
};
|
||||
|
||||
export class OpenAIChatLanguageModel implements LanguageModelV3 {
|
||||
readonly specificationVersion = 'v3';
|
||||
|
||||
readonly modelId: OpenAIChatModelId;
|
||||
|
||||
readonly supportedUrls = {
|
||||
'image/*': [/^https?:\/\/.*$/],
|
||||
};
|
||||
|
||||
private readonly config: OpenAIChatConfig;
|
||||
|
||||
constructor(modelId: OpenAIChatModelId, config: OpenAIChatConfig) {
|
||||
this.modelId = modelId;
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
get provider(): string {
|
||||
return this.config.provider;
|
||||
}
|
||||
|
||||
private async getArgs({
|
||||
prompt,
|
||||
maxOutputTokens,
|
||||
temperature,
|
||||
topP,
|
||||
topK,
|
||||
frequencyPenalty,
|
||||
presencePenalty,
|
||||
stopSequences,
|
||||
responseFormat,
|
||||
seed,
|
||||
tools,
|
||||
toolChoice,
|
||||
providerOptions,
|
||||
}: LanguageModelV3CallOptions) {
|
||||
const warnings: SharedV3Warning[] = [];
|
||||
|
||||
// Parse provider options
|
||||
const openaiOptions =
|
||||
(await parseProviderOptions({
|
||||
provider: 'openai',
|
||||
providerOptions,
|
||||
schema: openaiLanguageModelChatOptions,
|
||||
})) ?? {};
|
||||
|
||||
const modelCapabilities = getOpenAILanguageModelCapabilities(this.modelId);
|
||||
const isReasoningModel =
|
||||
openaiOptions.forceReasoning ?? modelCapabilities.isReasoningModel;
|
||||
|
||||
if (topK != null) {
|
||||
warnings.push({ type: 'unsupported', feature: 'topK' });
|
||||
}
|
||||
|
||||
const { messages, warnings: messageWarnings } = convertToOpenAIChatMessages(
|
||||
{
|
||||
prompt,
|
||||
systemMessageMode:
|
||||
openaiOptions.systemMessageMode ??
|
||||
(isReasoningModel
|
||||
? 'developer'
|
||||
: modelCapabilities.systemMessageMode),
|
||||
},
|
||||
);
|
||||
|
||||
warnings.push(...messageWarnings);
|
||||
|
||||
const strictJsonSchema = openaiOptions.strictJsonSchema ?? true;
|
||||
|
||||
const baseArgs = {
|
||||
// model id:
|
||||
model: this.modelId,
|
||||
|
||||
// model specific settings:
|
||||
logit_bias: openaiOptions.logitBias,
|
||||
logprobs:
|
||||
openaiOptions.logprobs === true ||
|
||||
typeof openaiOptions.logprobs === 'number'
|
||||
? true
|
||||
: undefined,
|
||||
top_logprobs:
|
||||
typeof openaiOptions.logprobs === 'number'
|
||||
? openaiOptions.logprobs
|
||||
: typeof openaiOptions.logprobs === 'boolean'
|
||||
? openaiOptions.logprobs
|
||||
? 0
|
||||
: undefined
|
||||
: undefined,
|
||||
user: openaiOptions.user,
|
||||
parallel_tool_calls: openaiOptions.parallelToolCalls,
|
||||
|
||||
// standardized settings:
|
||||
max_tokens: maxOutputTokens,
|
||||
temperature,
|
||||
top_p: topP,
|
||||
frequency_penalty: frequencyPenalty,
|
||||
presence_penalty: presencePenalty,
|
||||
response_format:
|
||||
responseFormat?.type === 'json'
|
||||
? responseFormat.schema != null
|
||||
? {
|
||||
type: 'json_schema',
|
||||
json_schema: {
|
||||
schema: responseFormat.schema,
|
||||
strict: strictJsonSchema,
|
||||
name: responseFormat.name ?? 'response',
|
||||
description: responseFormat.description,
|
||||
},
|
||||
}
|
||||
: { type: 'json_object' }
|
||||
: undefined,
|
||||
stop: stopSequences,
|
||||
seed,
|
||||
verbosity: openaiOptions.textVerbosity,
|
||||
|
||||
// openai specific settings:
|
||||
// TODO AI SDK 6: remove, we auto-map maxOutputTokens now
|
||||
max_completion_tokens: openaiOptions.maxCompletionTokens,
|
||||
store: openaiOptions.store,
|
||||
metadata: openaiOptions.metadata,
|
||||
prediction: openaiOptions.prediction,
|
||||
reasoning_effort: openaiOptions.reasoningEffort,
|
||||
service_tier: openaiOptions.serviceTier,
|
||||
prompt_cache_key: openaiOptions.promptCacheKey,
|
||||
prompt_cache_retention: openaiOptions.promptCacheRetention,
|
||||
safety_identifier: openaiOptions.safetyIdentifier,
|
||||
|
||||
// messages:
|
||||
messages,
|
||||
};
|
||||
|
||||
// remove unsupported settings for reasoning models
|
||||
// see https://platform.openai.com/docs/guides/reasoning#limitations
|
||||
if (isReasoningModel) {
|
||||
// when reasoning effort is none, gpt-5.1 models allow temperature, topP, logprobs
|
||||
// https://platform.openai.com/docs/guides/latest-model#gpt-5-1-parameter-compatibility
|
||||
if (
|
||||
openaiOptions.reasoningEffort !== 'none' ||
|
||||
!modelCapabilities.supportsNonReasoningParameters
|
||||
) {
|
||||
if (baseArgs.temperature != null) {
|
||||
baseArgs.temperature = undefined;
|
||||
warnings.push({
|
||||
type: 'unsupported',
|
||||
feature: 'temperature',
|
||||
details: 'temperature is not supported for reasoning models',
|
||||
});
|
||||
}
|
||||
if (baseArgs.top_p != null) {
|
||||
baseArgs.top_p = undefined;
|
||||
warnings.push({
|
||||
type: 'unsupported',
|
||||
feature: 'topP',
|
||||
details: 'topP is not supported for reasoning models',
|
||||
});
|
||||
}
|
||||
if (baseArgs.logprobs != null) {
|
||||
baseArgs.logprobs = undefined;
|
||||
warnings.push({
|
||||
type: 'other',
|
||||
message: 'logprobs is not supported for reasoning models',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (baseArgs.frequency_penalty != null) {
|
||||
baseArgs.frequency_penalty = undefined;
|
||||
warnings.push({
|
||||
type: 'unsupported',
|
||||
feature: 'frequencyPenalty',
|
||||
details: 'frequencyPenalty is not supported for reasoning models',
|
||||
});
|
||||
}
|
||||
if (baseArgs.presence_penalty != null) {
|
||||
baseArgs.presence_penalty = undefined;
|
||||
warnings.push({
|
||||
type: 'unsupported',
|
||||
feature: 'presencePenalty',
|
||||
details: 'presencePenalty is not supported for reasoning models',
|
||||
});
|
||||
}
|
||||
if (baseArgs.logit_bias != null) {
|
||||
baseArgs.logit_bias = undefined;
|
||||
warnings.push({
|
||||
type: 'other',
|
||||
message: 'logitBias is not supported for reasoning models',
|
||||
});
|
||||
}
|
||||
|
||||
if (baseArgs.top_logprobs != null) {
|
||||
baseArgs.top_logprobs = undefined;
|
||||
warnings.push({
|
||||
type: 'other',
|
||||
message: 'topLogprobs is not supported for reasoning models',
|
||||
});
|
||||
}
|
||||
|
||||
// reasoning models use max_completion_tokens instead of max_tokens:
|
||||
if (baseArgs.max_tokens != null) {
|
||||
if (baseArgs.max_completion_tokens == null) {
|
||||
baseArgs.max_completion_tokens = baseArgs.max_tokens;
|
||||
}
|
||||
baseArgs.max_tokens = undefined;
|
||||
}
|
||||
} else if (
|
||||
this.modelId.startsWith('gpt-4o-search-preview') ||
|
||||
this.modelId.startsWith('gpt-4o-mini-search-preview')
|
||||
) {
|
||||
if (baseArgs.temperature != null) {
|
||||
baseArgs.temperature = undefined;
|
||||
warnings.push({
|
||||
type: 'unsupported',
|
||||
feature: 'temperature',
|
||||
details:
|
||||
'temperature is not supported for the search preview models and has been removed.',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Validate flex processing support
|
||||
if (
|
||||
openaiOptions.serviceTier === 'flex' &&
|
||||
!modelCapabilities.supportsFlexProcessing
|
||||
) {
|
||||
warnings.push({
|
||||
type: 'unsupported',
|
||||
feature: 'serviceTier',
|
||||
details:
|
||||
'flex processing is only available for o3, o4-mini, and gpt-5 models',
|
||||
});
|
||||
baseArgs.service_tier = undefined;
|
||||
}
|
||||
|
||||
// Validate priority processing support
|
||||
if (
|
||||
openaiOptions.serviceTier === 'priority' &&
|
||||
!modelCapabilities.supportsPriorityProcessing
|
||||
) {
|
||||
warnings.push({
|
||||
type: 'unsupported',
|
||||
feature: 'serviceTier',
|
||||
details:
|
||||
'priority processing is only available for supported models (gpt-4, gpt-5, gpt-5-mini, o3, o4-mini) and requires Enterprise access. gpt-5-nano is not supported',
|
||||
});
|
||||
baseArgs.service_tier = undefined;
|
||||
}
|
||||
|
||||
const {
|
||||
tools: openaiTools,
|
||||
toolChoice: openaiToolChoice,
|
||||
toolWarnings,
|
||||
} = prepareChatTools({
|
||||
tools,
|
||||
toolChoice,
|
||||
});
|
||||
|
||||
return {
|
||||
args: {
|
||||
...baseArgs,
|
||||
tools: openaiTools,
|
||||
tool_choice: openaiToolChoice,
|
||||
},
|
||||
warnings: [...warnings, ...toolWarnings],
|
||||
};
|
||||
}
|
||||
|
||||
async doGenerate(
|
||||
options: LanguageModelV3CallOptions,
|
||||
): Promise<LanguageModelV3GenerateResult> {
|
||||
const { args: body, warnings } = await this.getArgs(options);
|
||||
|
||||
const {
|
||||
responseHeaders,
|
||||
value: response,
|
||||
rawValue: rawResponse,
|
||||
} = await postJsonToApi({
|
||||
url: this.config.url({
|
||||
path: '/chat/completions',
|
||||
modelId: this.modelId,
|
||||
}),
|
||||
headers: combineHeaders(this.config.headers(), options.headers),
|
||||
body,
|
||||
failedResponseHandler: openaiFailedResponseHandler,
|
||||
successfulResponseHandler: createJsonResponseHandler(
|
||||
openaiChatResponseSchema,
|
||||
),
|
||||
abortSignal: options.abortSignal,
|
||||
fetch: this.config.fetch,
|
||||
});
|
||||
|
||||
const choice = response.choices[0];
|
||||
const content: Array<LanguageModelV3Content> = [];
|
||||
|
||||
// text content:
|
||||
const text = choice.message.content;
|
||||
if (text != null && text.length > 0) {
|
||||
content.push({ type: 'text', text });
|
||||
}
|
||||
|
||||
// tool calls:
|
||||
for (const toolCall of choice.message.tool_calls ?? []) {
|
||||
content.push({
|
||||
type: 'tool-call' as const,
|
||||
toolCallId: toolCall.id ?? generateId(),
|
||||
toolName: toolCall.function.name,
|
||||
input: toolCall.function.arguments!,
|
||||
});
|
||||
}
|
||||
|
||||
// annotations/citations:
|
||||
for (const annotation of choice.message.annotations ?? []) {
|
||||
content.push({
|
||||
type: 'source',
|
||||
sourceType: 'url',
|
||||
id: generateId(),
|
||||
url: annotation.url_citation.url,
|
||||
title: annotation.url_citation.title,
|
||||
});
|
||||
}
|
||||
|
||||
// provider metadata:
|
||||
const completionTokenDetails = response.usage?.completion_tokens_details;
|
||||
const promptTokenDetails = response.usage?.prompt_tokens_details;
|
||||
const providerMetadata: SharedV3ProviderMetadata = { openai: {} };
|
||||
if (completionTokenDetails?.accepted_prediction_tokens != null) {
|
||||
providerMetadata.openai.acceptedPredictionTokens =
|
||||
completionTokenDetails?.accepted_prediction_tokens;
|
||||
}
|
||||
if (completionTokenDetails?.rejected_prediction_tokens != null) {
|
||||
providerMetadata.openai.rejectedPredictionTokens =
|
||||
completionTokenDetails?.rejected_prediction_tokens;
|
||||
}
|
||||
if (choice.logprobs?.content != null) {
|
||||
providerMetadata.openai.logprobs = choice.logprobs.content;
|
||||
}
|
||||
|
||||
return {
|
||||
content,
|
||||
finishReason: {
|
||||
unified: mapOpenAIFinishReason(choice.finish_reason),
|
||||
raw: choice.finish_reason ?? undefined,
|
||||
},
|
||||
usage: convertOpenAIChatUsage(response.usage),
|
||||
request: { body },
|
||||
response: {
|
||||
...getResponseMetadata(response),
|
||||
headers: responseHeaders,
|
||||
body: rawResponse,
|
||||
},
|
||||
warnings,
|
||||
providerMetadata,
|
||||
};
|
||||
}
|
||||
|
||||
async doStream(
|
||||
options: LanguageModelV3CallOptions,
|
||||
): Promise<LanguageModelV3StreamResult> {
|
||||
const { args, warnings } = await this.getArgs(options);
|
||||
|
||||
const body = {
|
||||
...args,
|
||||
stream: true,
|
||||
stream_options: {
|
||||
include_usage: true,
|
||||
},
|
||||
};
|
||||
|
||||
const { responseHeaders, value: response } = await postJsonToApi({
|
||||
url: this.config.url({
|
||||
path: '/chat/completions',
|
||||
modelId: this.modelId,
|
||||
}),
|
||||
headers: combineHeaders(this.config.headers(), options.headers),
|
||||
body,
|
||||
failedResponseHandler: openaiFailedResponseHandler,
|
||||
successfulResponseHandler: createEventSourceResponseHandler(
|
||||
openaiChatChunkSchema,
|
||||
),
|
||||
abortSignal: options.abortSignal,
|
||||
fetch: this.config.fetch,
|
||||
});
|
||||
|
||||
const toolCalls: Array<{
|
||||
id: string;
|
||||
type: 'function';
|
||||
function: {
|
||||
name: string;
|
||||
arguments: string;
|
||||
};
|
||||
hasFinished: boolean;
|
||||
}> = [];
|
||||
|
||||
let finishReason: LanguageModelV3FinishReason = {
|
||||
unified: 'other',
|
||||
raw: undefined,
|
||||
};
|
||||
let usage: OpenAIChatUsage | undefined = undefined;
|
||||
let metadataExtracted = false;
|
||||
let isActiveText = false;
|
||||
|
||||
const providerMetadata: SharedV3ProviderMetadata = { openai: {} };
|
||||
|
||||
return {
|
||||
stream: response.pipeThrough(
|
||||
new TransformStream<
|
||||
ParseResult<OpenAIChatChunk>,
|
||||
LanguageModelV3StreamPart
|
||||
>({
|
||||
start(controller) {
|
||||
controller.enqueue({ type: 'stream-start', warnings });
|
||||
},
|
||||
|
||||
transform(chunk, controller) {
|
||||
if (options.includeRawChunks) {
|
||||
controller.enqueue({ type: 'raw', rawValue: chunk.rawValue });
|
||||
}
|
||||
|
||||
// handle failed chunk parsing / validation:
|
||||
if (!chunk.success) {
|
||||
finishReason = { unified: 'error', raw: undefined };
|
||||
controller.enqueue({ type: 'error', error: chunk.error });
|
||||
return;
|
||||
}
|
||||
|
||||
const value = chunk.value;
|
||||
|
||||
// handle error chunks:
|
||||
if ('error' in value) {
|
||||
finishReason = { unified: 'error', raw: undefined };
|
||||
controller.enqueue({ type: 'error', error: value.error });
|
||||
return;
|
||||
}
|
||||
|
||||
// extract and emit response metadata once. Usually it comes in the first chunk.
|
||||
// Azure may prepend a chunk with a `"prompt_filter_results"` key which does not contain other metadata,
|
||||
// https://learn.microsoft.com/en-us/azure/ai-foundry/openai/concepts/content-filter-annotations?tabs=powershell
|
||||
if (!metadataExtracted) {
|
||||
const metadata = getResponseMetadata(value);
|
||||
if (Object.values(metadata).some(Boolean)) {
|
||||
metadataExtracted = true;
|
||||
controller.enqueue({
|
||||
type: 'response-metadata',
|
||||
...getResponseMetadata(value),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (value.usage != null) {
|
||||
usage = value.usage;
|
||||
|
||||
if (
|
||||
value.usage.completion_tokens_details
|
||||
?.accepted_prediction_tokens != null
|
||||
) {
|
||||
providerMetadata.openai.acceptedPredictionTokens =
|
||||
value.usage.completion_tokens_details?.accepted_prediction_tokens;
|
||||
}
|
||||
if (
|
||||
value.usage.completion_tokens_details
|
||||
?.rejected_prediction_tokens != null
|
||||
) {
|
||||
providerMetadata.openai.rejectedPredictionTokens =
|
||||
value.usage.completion_tokens_details?.rejected_prediction_tokens;
|
||||
}
|
||||
}
|
||||
|
||||
const choice = value.choices[0];
|
||||
|
||||
if (choice?.finish_reason != null) {
|
||||
finishReason = {
|
||||
unified: mapOpenAIFinishReason(choice.finish_reason),
|
||||
raw: choice.finish_reason,
|
||||
};
|
||||
}
|
||||
|
||||
if (choice?.logprobs?.content != null) {
|
||||
providerMetadata.openai.logprobs = choice.logprobs.content;
|
||||
}
|
||||
|
||||
if (choice?.delta == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
const delta = choice.delta;
|
||||
|
||||
if (delta.content != null) {
|
||||
if (!isActiveText) {
|
||||
controller.enqueue({ type: 'text-start', id: '0' });
|
||||
isActiveText = true;
|
||||
}
|
||||
|
||||
controller.enqueue({
|
||||
type: 'text-delta',
|
||||
id: '0',
|
||||
delta: delta.content,
|
||||
});
|
||||
}
|
||||
|
||||
if (delta.tool_calls != null) {
|
||||
for (const toolCallDelta of delta.tool_calls) {
|
||||
const index = toolCallDelta.index;
|
||||
|
||||
// Tool call start. OpenAI returns all information except the arguments in the first chunk.
|
||||
if (toolCalls[index] == null) {
|
||||
if (
|
||||
toolCallDelta.type != null &&
|
||||
toolCallDelta.type !== 'function'
|
||||
) {
|
||||
throw new InvalidResponseDataError({
|
||||
data: toolCallDelta,
|
||||
message: `Expected 'function' type.`,
|
||||
});
|
||||
}
|
||||
|
||||
if (toolCallDelta.id == null) {
|
||||
throw new InvalidResponseDataError({
|
||||
data: toolCallDelta,
|
||||
message: `Expected 'id' to be a string.`,
|
||||
});
|
||||
}
|
||||
|
||||
if (toolCallDelta.function?.name == null) {
|
||||
throw new InvalidResponseDataError({
|
||||
data: toolCallDelta,
|
||||
message: `Expected 'function.name' to be a string.`,
|
||||
});
|
||||
}
|
||||
|
||||
controller.enqueue({
|
||||
type: 'tool-input-start',
|
||||
id: toolCallDelta.id,
|
||||
toolName: toolCallDelta.function.name,
|
||||
});
|
||||
|
||||
toolCalls[index] = {
|
||||
id: toolCallDelta.id,
|
||||
type: 'function',
|
||||
function: {
|
||||
name: toolCallDelta.function.name,
|
||||
arguments: toolCallDelta.function.arguments ?? '',
|
||||
},
|
||||
hasFinished: false,
|
||||
};
|
||||
|
||||
const toolCall = toolCalls[index];
|
||||
|
||||
if (
|
||||
toolCall.function?.name != null &&
|
||||
toolCall.function?.arguments != null
|
||||
) {
|
||||
// send delta if the argument text has already started:
|
||||
if (toolCall.function.arguments.length > 0) {
|
||||
controller.enqueue({
|
||||
type: 'tool-input-delta',
|
||||
id: toolCall.id,
|
||||
delta: toolCall.function.arguments,
|
||||
});
|
||||
}
|
||||
|
||||
// check if tool call is complete
|
||||
// (some providers send the full tool call in one chunk):
|
||||
if (isParsableJson(toolCall.function.arguments)) {
|
||||
controller.enqueue({
|
||||
type: 'tool-input-end',
|
||||
id: toolCall.id,
|
||||
});
|
||||
|
||||
controller.enqueue({
|
||||
type: 'tool-call',
|
||||
toolCallId: toolCall.id ?? generateId(),
|
||||
toolName: toolCall.function.name,
|
||||
input: toolCall.function.arguments,
|
||||
});
|
||||
toolCall.hasFinished = true;
|
||||
}
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
// existing tool call, merge if not finished
|
||||
const toolCall = toolCalls[index];
|
||||
|
||||
if (toolCall.hasFinished) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (toolCallDelta.function?.arguments != null) {
|
||||
toolCall.function!.arguments +=
|
||||
toolCallDelta.function?.arguments ?? '';
|
||||
}
|
||||
|
||||
// send delta
|
||||
controller.enqueue({
|
||||
type: 'tool-input-delta',
|
||||
id: toolCall.id,
|
||||
delta: toolCallDelta.function.arguments ?? '',
|
||||
});
|
||||
|
||||
// check if tool call is complete
|
||||
if (
|
||||
toolCall.function?.name != null &&
|
||||
toolCall.function?.arguments != null &&
|
||||
isParsableJson(toolCall.function.arguments)
|
||||
) {
|
||||
controller.enqueue({
|
||||
type: 'tool-input-end',
|
||||
id: toolCall.id,
|
||||
});
|
||||
|
||||
controller.enqueue({
|
||||
type: 'tool-call',
|
||||
toolCallId: toolCall.id ?? generateId(),
|
||||
toolName: toolCall.function.name,
|
||||
input: toolCall.function.arguments,
|
||||
});
|
||||
toolCall.hasFinished = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// annotations/citations:
|
||||
if (delta.annotations != null) {
|
||||
for (const annotation of delta.annotations) {
|
||||
controller.enqueue({
|
||||
type: 'source',
|
||||
sourceType: 'url',
|
||||
id: generateId(),
|
||||
url: annotation.url_citation.url,
|
||||
title: annotation.url_citation.title,
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
flush(controller) {
|
||||
if (isActiveText) {
|
||||
controller.enqueue({ type: 'text-end', id: '0' });
|
||||
}
|
||||
|
||||
controller.enqueue({
|
||||
type: 'finish',
|
||||
finishReason,
|
||||
usage: convertOpenAIChatUsage(usage),
|
||||
...(providerMetadata != null ? { providerMetadata } : {}),
|
||||
});
|
||||
},
|
||||
}),
|
||||
),
|
||||
request: { body },
|
||||
response: { headers: responseHeaders },
|
||||
};
|
||||
}
|
||||
}
|
||||
201
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/openai/src/chat/openai-chat-options.ts
generated
vendored
Normal file
201
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/openai/src/chat/openai-chat-options.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,201 @@
|
|||
import { InferSchema, lazySchema, zodSchema } from '@ai-sdk/provider-utils';
|
||||
import { z } from 'zod/v4';
|
||||
|
||||
// https://platform.openai.com/docs/models
|
||||
export type OpenAIChatModelId =
|
||||
| 'o1'
|
||||
| 'o1-2024-12-17'
|
||||
| 'o3-mini'
|
||||
| 'o3-mini-2025-01-31'
|
||||
| 'o3'
|
||||
| 'o3-2025-04-16'
|
||||
| 'o4-mini'
|
||||
| 'o4-mini-2025-04-16'
|
||||
| 'gpt-4.1'
|
||||
| 'gpt-4.1-2025-04-14'
|
||||
| 'gpt-4.1-mini'
|
||||
| 'gpt-4.1-mini-2025-04-14'
|
||||
| 'gpt-4.1-nano'
|
||||
| 'gpt-4.1-nano-2025-04-14'
|
||||
| 'gpt-4o'
|
||||
| 'gpt-4o-2024-05-13'
|
||||
| 'gpt-4o-2024-08-06'
|
||||
| 'gpt-4o-2024-11-20'
|
||||
| 'gpt-4o-audio-preview'
|
||||
| 'gpt-4o-audio-preview-2024-12-17'
|
||||
| 'gpt-4o-audio-preview-2025-06-03'
|
||||
| 'gpt-4o-mini'
|
||||
| 'gpt-4o-mini-2024-07-18'
|
||||
| 'gpt-4o-mini-audio-preview'
|
||||
| 'gpt-4o-mini-audio-preview-2024-12-17'
|
||||
| 'gpt-4o-search-preview'
|
||||
| 'gpt-4o-search-preview-2025-03-11'
|
||||
| 'gpt-4o-mini-search-preview'
|
||||
| 'gpt-4o-mini-search-preview-2025-03-11'
|
||||
| 'gpt-3.5-turbo-0125'
|
||||
| 'gpt-3.5-turbo'
|
||||
| 'gpt-3.5-turbo-1106'
|
||||
| 'gpt-3.5-turbo-16k'
|
||||
| 'gpt-5'
|
||||
| 'gpt-5-2025-08-07'
|
||||
| 'gpt-5-mini'
|
||||
| 'gpt-5-mini-2025-08-07'
|
||||
| 'gpt-5-nano'
|
||||
| 'gpt-5-nano-2025-08-07'
|
||||
| 'gpt-5-chat-latest'
|
||||
| 'gpt-5.1'
|
||||
| 'gpt-5.1-2025-11-13'
|
||||
| 'gpt-5.1-chat-latest'
|
||||
| 'gpt-5.2'
|
||||
| 'gpt-5.2-2025-12-11'
|
||||
| 'gpt-5.2-chat-latest'
|
||||
| 'gpt-5.2-pro'
|
||||
| 'gpt-5.2-pro-2025-12-11'
|
||||
| 'gpt-5.3-chat-latest'
|
||||
| 'gpt-5.4'
|
||||
| 'gpt-5.4-2026-03-05'
|
||||
| 'gpt-5.4-mini'
|
||||
| 'gpt-5.4-mini-2026-03-17'
|
||||
| 'gpt-5.4-nano'
|
||||
| 'gpt-5.4-nano-2026-03-17'
|
||||
| 'gpt-5.4-pro'
|
||||
| 'gpt-5.4-pro-2026-03-05'
|
||||
| (string & {});
|
||||
|
||||
export const openaiLanguageModelChatOptions = lazySchema(() =>
|
||||
zodSchema(
|
||||
z.object({
|
||||
/**
|
||||
* Modify the likelihood of specified tokens appearing in the completion.
|
||||
*
|
||||
* Accepts a JSON object that maps tokens (specified by their token ID in
|
||||
* the GPT tokenizer) to an associated bias value from -100 to 100.
|
||||
*/
|
||||
logitBias: z.record(z.coerce.number<string>(), z.number()).optional(),
|
||||
|
||||
/**
|
||||
* Return the log probabilities of the tokens.
|
||||
*
|
||||
* Setting to true will return the log probabilities of the tokens that
|
||||
* were generated.
|
||||
*
|
||||
* Setting to a number will return the log probabilities of the top n
|
||||
* tokens that were generated.
|
||||
*/
|
||||
logprobs: z.union([z.boolean(), z.number()]).optional(),
|
||||
|
||||
/**
|
||||
* Whether to enable parallel function calling during tool use. Default to true.
|
||||
*/
|
||||
parallelToolCalls: z.boolean().optional(),
|
||||
|
||||
/**
|
||||
* A unique identifier representing your end-user, which can help OpenAI to
|
||||
* monitor and detect abuse.
|
||||
*/
|
||||
user: z.string().optional(),
|
||||
|
||||
/**
|
||||
* Reasoning effort for reasoning models. Defaults to `medium`.
|
||||
*/
|
||||
reasoningEffort: z
|
||||
.enum(['none', 'minimal', 'low', 'medium', 'high', 'xhigh'])
|
||||
.optional(),
|
||||
|
||||
/**
|
||||
* Maximum number of completion tokens to generate. Useful for reasoning models.
|
||||
*/
|
||||
maxCompletionTokens: z.number().optional(),
|
||||
|
||||
/**
|
||||
* Whether to enable persistence in responses API.
|
||||
*/
|
||||
store: z.boolean().optional(),
|
||||
|
||||
/**
|
||||
* Metadata to associate with the request.
|
||||
*/
|
||||
metadata: z.record(z.string().max(64), z.string().max(512)).optional(),
|
||||
|
||||
/**
|
||||
* Parameters for prediction mode.
|
||||
*/
|
||||
prediction: z.record(z.string(), z.any()).optional(),
|
||||
|
||||
/**
|
||||
* Service tier for the request.
|
||||
* - 'auto': Default service tier. The request will be processed with the service tier configured in the
|
||||
* Project settings. Unless otherwise configured, the Project will use 'default'.
|
||||
* - 'flex': 50% cheaper processing at the cost of increased latency. Only available for o3 and o4-mini models.
|
||||
* - 'priority': Higher-speed processing with predictably low latency at premium cost. Available for Enterprise customers.
|
||||
* - 'default': The request will be processed with the standard pricing and performance for the selected model.
|
||||
*
|
||||
* @default 'auto'
|
||||
*/
|
||||
serviceTier: z.enum(['auto', 'flex', 'priority', 'default']).optional(),
|
||||
|
||||
/**
|
||||
* Whether to use strict JSON schema validation.
|
||||
*
|
||||
* @default true
|
||||
*/
|
||||
strictJsonSchema: z.boolean().optional(),
|
||||
|
||||
/**
|
||||
* Controls the verbosity of the model's responses.
|
||||
* Lower values will result in more concise responses, while higher values will result in more verbose responses.
|
||||
*/
|
||||
textVerbosity: z.enum(['low', 'medium', 'high']).optional(),
|
||||
|
||||
/**
|
||||
* A cache key for prompt caching. Allows manual control over prompt caching behavior.
|
||||
* Useful for improving cache hit rates and working around automatic caching issues.
|
||||
*/
|
||||
promptCacheKey: z.string().optional(),
|
||||
|
||||
/**
|
||||
* The retention policy for the prompt cache.
|
||||
* - 'in_memory': Default. Standard prompt caching behavior.
|
||||
* - '24h': Extended prompt caching that keeps cached prefixes active for up to 24 hours.
|
||||
* Currently only available for 5.1 series models.
|
||||
*
|
||||
* @default 'in_memory'
|
||||
*/
|
||||
promptCacheRetention: z.enum(['in_memory', '24h']).optional(),
|
||||
|
||||
/**
|
||||
* A stable identifier used to help detect users of your application
|
||||
* that may be violating OpenAI's usage policies. The IDs should be a
|
||||
* string that uniquely identifies each user. We recommend hashing their
|
||||
* username or email address, in order to avoid sending us any identifying
|
||||
* information.
|
||||
*/
|
||||
safetyIdentifier: z.string().optional(),
|
||||
|
||||
/**
|
||||
* Override the system message mode for this model.
|
||||
* - 'system': Use the 'system' role for system messages (default for most models)
|
||||
* - 'developer': Use the 'developer' role for system messages (used by reasoning models)
|
||||
* - 'remove': Remove system messages entirely
|
||||
*
|
||||
* If not specified, the mode is automatically determined based on the model.
|
||||
*/
|
||||
systemMessageMode: z.enum(['system', 'developer', 'remove']).optional(),
|
||||
|
||||
/**
|
||||
* Force treating this model as a reasoning model.
|
||||
*
|
||||
* This is useful for "stealth" reasoning models (e.g. via a custom baseURL)
|
||||
* where the model ID is not recognized by the SDK's allowlist.
|
||||
*
|
||||
* When enabled, the SDK applies reasoning-model parameter compatibility rules
|
||||
* and defaults `systemMessageMode` to `developer` unless overridden.
|
||||
*/
|
||||
forceReasoning: z.boolean().optional(),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
export type OpenAILanguageModelChatOptions = InferSchema<
|
||||
typeof openaiLanguageModelChatOptions
|
||||
>;
|
||||
84
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/openai/src/chat/openai-chat-prepare-tools.ts
generated
vendored
Normal file
84
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/openai/src/chat/openai-chat-prepare-tools.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
import {
|
||||
LanguageModelV3CallOptions,
|
||||
SharedV3Warning,
|
||||
UnsupportedFunctionalityError,
|
||||
} from '@ai-sdk/provider';
|
||||
import {
|
||||
OpenAIChatToolChoice,
|
||||
OpenAIChatFunctionTool,
|
||||
} from './openai-chat-api';
|
||||
|
||||
export function prepareChatTools({
|
||||
tools,
|
||||
toolChoice,
|
||||
}: {
|
||||
tools: LanguageModelV3CallOptions['tools'];
|
||||
toolChoice?: LanguageModelV3CallOptions['toolChoice'];
|
||||
}): {
|
||||
tools?: OpenAIChatFunctionTool[];
|
||||
toolChoice?: OpenAIChatToolChoice;
|
||||
toolWarnings: Array<SharedV3Warning>;
|
||||
} {
|
||||
// when the tools array is empty, change it to undefined to prevent errors:
|
||||
tools = tools?.length ? tools : undefined;
|
||||
|
||||
const toolWarnings: SharedV3Warning[] = [];
|
||||
|
||||
if (tools == null) {
|
||||
return { tools: undefined, toolChoice: undefined, toolWarnings };
|
||||
}
|
||||
|
||||
const openaiTools: OpenAIChatFunctionTool[] = [];
|
||||
|
||||
for (const tool of tools) {
|
||||
switch (tool.type) {
|
||||
case 'function':
|
||||
openaiTools.push({
|
||||
type: 'function',
|
||||
function: {
|
||||
name: tool.name,
|
||||
description: tool.description,
|
||||
parameters: tool.inputSchema,
|
||||
...(tool.strict != null ? { strict: tool.strict } : {}),
|
||||
},
|
||||
});
|
||||
break;
|
||||
default:
|
||||
toolWarnings.push({
|
||||
type: 'unsupported',
|
||||
feature: `tool type: ${tool.type}`,
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (toolChoice == null) {
|
||||
return { tools: openaiTools, toolChoice: undefined, toolWarnings };
|
||||
}
|
||||
|
||||
const type = toolChoice.type;
|
||||
|
||||
switch (type) {
|
||||
case 'auto':
|
||||
case 'none':
|
||||
case 'required':
|
||||
return { tools: openaiTools, toolChoice: type, toolWarnings };
|
||||
case 'tool':
|
||||
return {
|
||||
tools: openaiTools,
|
||||
toolChoice: {
|
||||
type: 'function',
|
||||
function: {
|
||||
name: toolChoice.toolName,
|
||||
},
|
||||
},
|
||||
toolWarnings,
|
||||
};
|
||||
default: {
|
||||
const _exhaustiveCheck: never = type;
|
||||
throw new UnsupportedFunctionalityError({
|
||||
functionality: `tool choice type: ${_exhaustiveCheck}`,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
70
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/openai/src/chat/openai-chat-prompt.ts
generated
vendored
Normal file
70
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/openai/src/chat/openai-chat-prompt.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
export type OpenAIChatPrompt = Array<ChatCompletionMessage>;
|
||||
|
||||
export type ChatCompletionMessage =
|
||||
| ChatCompletionSystemMessage
|
||||
| ChatCompletionDeveloperMessage
|
||||
| ChatCompletionUserMessage
|
||||
| ChatCompletionAssistantMessage
|
||||
| ChatCompletionToolMessage;
|
||||
|
||||
export interface ChatCompletionSystemMessage {
|
||||
role: 'system';
|
||||
content: string;
|
||||
}
|
||||
|
||||
export interface ChatCompletionDeveloperMessage {
|
||||
role: 'developer';
|
||||
content: string;
|
||||
}
|
||||
|
||||
export interface ChatCompletionUserMessage {
|
||||
role: 'user';
|
||||
content: string | Array<ChatCompletionContentPart>;
|
||||
}
|
||||
|
||||
export type ChatCompletionContentPart =
|
||||
| ChatCompletionContentPartText
|
||||
| ChatCompletionContentPartImage
|
||||
| ChatCompletionContentPartInputAudio
|
||||
| ChatCompletionContentPartFile;
|
||||
|
||||
export interface ChatCompletionContentPartText {
|
||||
type: 'text';
|
||||
text: string;
|
||||
}
|
||||
|
||||
export interface ChatCompletionContentPartImage {
|
||||
type: 'image_url';
|
||||
image_url: { url: string };
|
||||
}
|
||||
|
||||
export interface ChatCompletionContentPartInputAudio {
|
||||
type: 'input_audio';
|
||||
input_audio: { data: string; format: 'wav' | 'mp3' };
|
||||
}
|
||||
|
||||
export interface ChatCompletionContentPartFile {
|
||||
type: 'file';
|
||||
file: { filename: string; file_data: string } | { file_id: string };
|
||||
}
|
||||
|
||||
export interface ChatCompletionAssistantMessage {
|
||||
role: 'assistant';
|
||||
content?: string | null;
|
||||
tool_calls?: Array<ChatCompletionMessageToolCall>;
|
||||
}
|
||||
|
||||
export interface ChatCompletionMessageToolCall {
|
||||
type: 'function';
|
||||
id: string;
|
||||
function: {
|
||||
arguments: string;
|
||||
name: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface ChatCompletionToolMessage {
|
||||
role: 'tool';
|
||||
content: string;
|
||||
tool_call_id: string;
|
||||
}
|
||||
46
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/openai/src/completion/convert-openai-completion-usage.ts
generated
vendored
Normal file
46
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/openai/src/completion/convert-openai-completion-usage.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
import { LanguageModelV3Usage } from '@ai-sdk/provider';
|
||||
|
||||
export type OpenAICompletionUsage = {
|
||||
prompt_tokens?: number | null;
|
||||
completion_tokens?: number | null;
|
||||
total_tokens?: number | null;
|
||||
};
|
||||
|
||||
export function convertOpenAICompletionUsage(
|
||||
usage: OpenAICompletionUsage | undefined | null,
|
||||
): LanguageModelV3Usage {
|
||||
if (usage == null) {
|
||||
return {
|
||||
inputTokens: {
|
||||
total: undefined,
|
||||
noCache: undefined,
|
||||
cacheRead: undefined,
|
||||
cacheWrite: undefined,
|
||||
},
|
||||
outputTokens: {
|
||||
total: undefined,
|
||||
text: undefined,
|
||||
reasoning: undefined,
|
||||
},
|
||||
raw: undefined,
|
||||
};
|
||||
}
|
||||
|
||||
const promptTokens = usage.prompt_tokens ?? 0;
|
||||
const completionTokens = usage.completion_tokens ?? 0;
|
||||
|
||||
return {
|
||||
inputTokens: {
|
||||
total: usage.prompt_tokens ?? undefined,
|
||||
noCache: promptTokens,
|
||||
cacheRead: undefined,
|
||||
cacheWrite: undefined,
|
||||
},
|
||||
outputTokens: {
|
||||
total: usage.completion_tokens ?? undefined,
|
||||
text: completionTokens,
|
||||
reasoning: undefined,
|
||||
},
|
||||
raw: usage,
|
||||
};
|
||||
}
|
||||
93
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/openai/src/completion/convert-to-openai-completion-prompt.ts
generated
vendored
Normal file
93
examples/vercel-ai-sdk-pr/node-headroom-compression/node_modules/@ai-sdk/openai/src/completion/convert-to-openai-completion-prompt.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
import {
|
||||
InvalidPromptError,
|
||||
LanguageModelV3Prompt,
|
||||
UnsupportedFunctionalityError,
|
||||
} from '@ai-sdk/provider';
|
||||
|
||||
export function convertToOpenAICompletionPrompt({
|
||||
prompt,
|
||||
user = 'user',
|
||||
assistant = 'assistant',
|
||||
}: {
|
||||
prompt: LanguageModelV3Prompt;
|
||||
user?: string;
|
||||
assistant?: string;
|
||||
}): {
|
||||
prompt: string;
|
||||
stopSequences?: string[];
|
||||
} {
|
||||
// transform to a chat message format:
|
||||
let text = '';
|
||||
|
||||
// if first message is a system message, add it to the text:
|
||||
if (prompt[0].role === 'system') {
|
||||
text += `${prompt[0].content}\n\n`;
|
||||
prompt = prompt.slice(1);
|
||||
}
|
||||
|
||||
for (const { role, content } of prompt) {
|
||||
switch (role) {
|
||||
case 'system': {
|
||||
throw new InvalidPromptError({
|
||||
message: 'Unexpected system message in prompt: ${content}',
|
||||
prompt,
|
||||
});
|
||||
}
|
||||
|
||||
case 'user': {
|
||||
const userMessage = content
|
||||
.map(part => {
|
||||
switch (part.type) {
|
||||
case 'text': {
|
||||
return part.text;
|
||||
}
|
||||
}
|
||||
})
|
||||
.filter(Boolean)
|
||||
.join('');
|
||||
|
||||
text += `${user}:\n${userMessage}\n\n`;
|
||||
break;
|
||||
}
|
||||
|
||||
case 'assistant': {
|
||||
const assistantMessage = content
|
||||
.map(part => {
|
||||
switch (part.type) {
|
||||
case 'text': {
|
||||
return part.text;
|
||||
}
|
||||
case 'tool-call': {
|
||||
throw new UnsupportedFunctionalityError({
|
||||
functionality: 'tool-call messages',
|
||||
});
|
||||
}
|
||||
}
|
||||
})
|
||||
.join('');
|
||||
|
||||
text += `${assistant}:\n${assistantMessage}\n\n`;
|
||||
break;
|
||||
}
|
||||
|
||||
case 'tool': {
|
||||
throw new UnsupportedFunctionalityError({
|
||||
functionality: 'tool messages',
|
||||
});
|
||||
}
|
||||
|
||||
default: {
|
||||
const _exhaustiveCheck: never = role;
|
||||
throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Assistant message prefix:
|
||||
text += `${assistant}:\n`;
|
||||
|
||||
return {
|
||||
prompt: text,
|
||||
stopSequences: [`\n${user}:`],
|
||||
};
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue