@jack-yang/magpie-portkey-vertex 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 yangwudong
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,117 @@
1
- # Temporary Holding Version
1
+ # magpie-portkey-vertex
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ A [magpie](https://github.com/yetone/magpie) provider for Google Vertex AI through [Portkey](https://portkey.ai): **Gemini** and **Claude** on one account, each in its own native protocol, through Portkey's passthrough route. Built on [`@ai-sdk/google-vertex`](https://www.npmjs.com/package/@ai-sdk/google-vertex). Works in magpie (through its pi package bridge) and in [pi](https://github.com/earendil-works/pi).
4
+
5
+ The provider id is **`portkey-vertex`**.
6
+
7
+ 中文文档:[README.zh-CN.md](./README.zh-CN.md)
8
+
9
+ ## How requests travel
10
+
11
+ | Models | Path after the gateway's `/v1` | Protocol |
12
+ | --- | --- | --- |
13
+ | `gemini-*` | `/v1beta1/projects/{p}/locations/{l}/publishers/google/models/{id}:streamGenerateContent?alt=sse` | Gemini `generateContent` |
14
+ | `anthropic.claude-*` | `/v1/projects/{p}/locations/{l}/publishers/anthropic/models/{id}:streamRawPredict` | Anthropic Messages |
15
+
16
+ Portkey drops the gateway's `/v1` and sends the rest to Google's regional host for the saved provider, authenticating with the provider's service account. The Portkey key travels only in `x-portkey-api-key`. No `Authorization` header is sent.
17
+
18
+ ## Features
19
+
20
+ - **Gemini**: thought signatures stay on the part they came with (text, thinking, tool calls) and are replayed to the same model. They survive magpie's Anthropic bridge too, so Gemini 3 tool loops do not fail with a 400. Thinking uses `thinkingLevel` on Gemini 3 and later and `thinkingBudget` on 2.5, and streams as thought summaries. Gemini's implicit prompt cache shows up as cache reads.
21
+ - **Claude**: adaptive thinking at the requested effort (`low` to `max`), streamed as summarized thinking. Signed and redacted thinking survives replay. Prompt-cache checkpoints are placed on the system prompt and on the end of the conversation, and the optional one-hour TTL is supported.
22
+ - Text, vision, streamed tool calls and tool results, including image results and failed tools.
23
+ - Input, output, reasoning, cache-read and cache-write token accounting at catalog prices.
24
+ - Sign-in asks only for the gateway, provider slug, API key and an optional name. The GCP project and region are read from the Portkey provider itself.
25
+ - The model list is the provider's own Model Catalog: what the integration allows, limited to chat models.
26
+ - Errors say who refused and why: a Portkey budget or scope, a Google IAM permission (without the project name), a disabled API, saved-only policies, a guardrail, a Vertex safety block or a Claude refusal. Every error includes Portkey's trace id.
27
+ - Requests that fail with `429`, `408` or `5xx` are retried on the same account, so its prompt cache is kept.
28
+
29
+ ## Before you sign in
30
+
31
+ In Portkey, add **Google Vertex AI** under **Model Catalog → Add Provider** with a **service account JSON** and a region. The service account needs `roles/aiplatform.user`. Allow the Gemini and Claude models you want on the provider; the plugin lists exactly those.
32
+
33
+ ## Install in magpie
34
+
35
+ ```sh
36
+ magpie plugin add @jack-yang/magpie-portkey-vertex
37
+ # or GitHub, or a local checkout
38
+ magpie plugin add github:yangwudong/magpie-portkey-vertex
39
+ ```
40
+
41
+ Sign in from the Providers page (or `magpie plugin login portkey-vertex`):
42
+
43
+ 1. **Base URL**: your Portkey gateway, ending in `/v1`. Leave it empty for `https://api.portkey.ai/v1`.
44
+ 2. **Provider slug**: the Vertex AI provider, written `@your-vertex-provider`.
45
+ 3. **API key**: the account's Portkey key.
46
+ 4. **Account name** (optional). Left empty, the account is named by both ends of the key.
47
+
48
+ When you sign in, the plugin reads the provider's GCP project and region from Portkey's control plane (`GET /v1/providers/{slug}`) with the same key. If that provider has no values, it uses the first Vertex provider the key can see that does. Only when nothing answers does the form ask for the two values, and it says why.
49
+
50
+ ```sh
51
+ magpie claude portkey-vertex/gemini-4-neon
52
+ magpie claude portkey-vertex/anthropic.claude-opus-5-5
53
+ ```
54
+
55
+ ## Configure
56
+
57
+ Three switches apply to every account. A value saved in magpie's **Options** wins over the config file, which wins over the default:
58
+
59
+ - `log` (default `false`): whether Portkey may store request and response content. While off, every request carries `x-portkey-debug: false`; tokens, cost and latency are still recorded.
60
+ - `retry` (default `2`, `0`–`5`): extra tries on the same account for `429`, `408` or `5xx` (1s, then 2s). Retry-After is honoured; a wait of a minute or more is not waited.
61
+ - `cacheTtl1h` (default `false`): keep Claude's cache checkpoints for an hour instead of five minutes. Writes cost more (2× input against 1.25×), so it only pays when you come back to a conversation within the hour. Gemini ignores it.
62
+
63
+ `~/.config/magpie/portkey-vertex/config.json` can also hold:
64
+
65
+ - `configId`: sent as `x-portkey-config`.
66
+ - `headers`: extra non-secret gateway headers.
67
+ - `models`: extra model ids, always listed.
68
+ - `controlPlaneUrl`: a self-hosted Portkey control plane.
69
+
70
+ Secrets are refused in this file.
71
+
72
+ ## Models
73
+
74
+ The bundled `models.json` holds names, limits and prices (USD per million tokens) taken from pi's own catalogs. Models the provider allows but the catalog does not know get family defaults: Gemini 2.5 and later think and have a 1M context; Claude 4.6 and later think adaptively up to `max` and have a 1M context.
75
+
76
+ | Name | Model id | Context | Max output |
77
+ | --- | --- | ---: | ---: |
78
+ | Gemini 2.5 Flash | `gemini-2.5-flash` | 1,048,576 | 65,536 |
79
+ | Gemini 2.5 Flash-Lite | `gemini-2.5-flash-lite` | 1,048,576 | 65,536 |
80
+ | Gemini 2.5 Pro | `gemini-2.5-pro` | 1,048,576 | 65,536 |
81
+ | Gemini 3 Flash Preview | `gemini-3-flash-preview` | 1,048,576 | 65,536 |
82
+ | Gemini 3.1 Flash Lite | `gemini-3.1-flash-lite` | 1,048,576 | 65,536 |
83
+ | Gemini 3.1 Pro Preview | `gemini-3.1-pro-preview` | 1,048,576 | 65,536 |
84
+ | Gemini 3.1 Pro Preview Custom Tools | `gemini-3.1-pro-preview-customtools` | 1,048,576 | 65,536 |
85
+ | Gemini 3.5 Flash | `gemini-3.5-flash` | 1,048,576 | 65,536 |
86
+ | Gemini 3.5 Flash Lite | `gemini-3.5-flash-lite` | 1,048,576 | 65,536 |
87
+ | Gemini 3.6 Flash | `gemini-3.6-flash` | 1,048,576 | 65,536 |
88
+ | Gemini 3.7 Flash | `gemini-3.7-flash` | 1,048,576 | 65,536 |
89
+ | Gemini 3.8 Flash | `gemini-3.8-flash` | 1,048,576 | 65,536 |
90
+ | Gemini 4 Argon | `gemini-4-argon` | 1,048,576 | 65,536 |
91
+ | Claude Opus 5.5 | `anthropic.claude-opus-5-5` | 1,000,000 | 128,000 |
92
+ | Claude Sonnet 5.5 | `anthropic.claude-sonnet-5-5` | 1,000,000 | 128,000 |
93
+ | Claude Haiku 5.5 | `anthropic.claude-haiku-5-5` | 1,000,000 | 128,000 |
94
+ | Claude Opus 5 | `anthropic.claude-opus-5` | 1,000,000 | 128,000 |
95
+ | Claude Sonnet 5 | `anthropic.claude-sonnet-5` | 1,000,000 | 128,000 |
96
+ | Claude Opus 4.8 | `anthropic.claude-opus-4-8` | 1,000,000 | 128,000 |
97
+ | Claude Opus 4.6 | `anthropic.claude-opus-4-6` | 1,000,000 | 128,000 |
98
+ | Claude Sonnet 4.6 | `anthropic.claude-sonnet-4-6` | 1,000,000 | 128,000 |
99
+
100
+ ## Development
101
+
102
+ ```sh
103
+ bun install --ignore-scripts
104
+ bun run check # typecheck + tests
105
+ MAGPIE_SOURCE_DIR=/tmp/magpie bun test test/magpie.test.ts # magpie's real plugin host
106
+ bun scripts/live.ts # live, from .env (see .env.example)
107
+ ```
108
+
109
+ The unit tests send real Gemini and Anthropic SSE streams through the SDKs' own parsers; no credentials are needed. `scripts/live.ts` runs against a real gateway and masks the gateway, key and project in its output. What Portkey does on this route, with sources and what was observed live, is in [docs/portkey-reference.md](docs/portkey-reference.md).
110
+
111
+ ## Credits
112
+
113
+ Account handling, configuration layering and secret hygiene follow [magpie-portkey-bedrock](https://github.com/yangwudong/magpie-portkey-bedrock).
114
+
115
+ ## License
116
+
117
+ MIT
@@ -0,0 +1,87 @@
1
+ # magpie-portkey-vertex
2
+
3
+ [magpie](https://github.com/yetone/magpie) 的供应商插件:通过 [Portkey](https://portkey.ai) 使用 Google Vertex AI。一个账号同时支持 **Gemini** 和 **Claude**,两者都经 Portkey 的透传路由、用各自的原生协议通信。插件基于 [`@ai-sdk/google-vertex`](https://www.npmjs.com/package/@ai-sdk/google-vertex),magpie(经 pi 包桥接)和 [pi](https://github.com/earendil-works/pi) 都能用。
4
+
5
+ 供应商 id 是 **`portkey-vertex`**。
6
+
7
+ English documentation: [README.md](./README.md)
8
+
9
+ ## 请求怎么走
10
+
11
+ | 模型 | 网关 `/v1` 之后的路径 | 协议 |
12
+ | --- | --- | --- |
13
+ | `gemini-*` | `/v1beta1/projects/{p}/locations/{l}/publishers/google/models/{id}:streamGenerateContent?alt=sse` | Gemini `generateContent` |
14
+ | `anthropic.claude-*` | `/v1/projects/{p}/locations/{l}/publishers/anthropic/models/{id}:streamRawPredict` | Anthropic Messages |
15
+
16
+ Portkey 去掉网关地址里的 `/v1`,把剩下的路径发到 provider 所在区域的 Google 域名,并用 provider 的服务账号认证。Portkey key 只放在 `x-portkey-api-key` 里,插件不发送 `Authorization`。
17
+
18
+ ## 功能
19
+
20
+ - **Gemini**:thought signature 留在它原来所在的那一段(文本、思考、工具调用)上,只回放给生成它的同一个模型;经过 magpie 的 Anthropic 桥接也不会丢,所以 Gemini 3 的工具循环不会报 400。Gemini 3 及以后用 `thinkingLevel` 控制思考,2.5 用 `thinkingBudget`,思考摘要以流式输出。Gemini 的隐式缓存计为缓存读取。
21
+ - **Claude**:按请求的强度(`low` 到 `max`)做自适应思考,以摘要形式流式输出。带签名的思考和被屏蔽(redacted)的思考都能回放。系统提示词和对话末尾各打一个缓存断点,可选 1 小时 TTL。
22
+ - 文本、图片、流式工具调用和工具结果都支持,工具结果可以带图片,失败的工具也能回传。
23
+ - 统计 input、output、reasoning、缓存读取和缓存写入的 token,按目录价格计费。
24
+ - 登录只问网关、provider slug、API key 和选填的名称;GCP 项目和区域从 Portkey provider 自动读取。
25
+ - 模型列表就是该 provider 在 Model Catalog 里允许的模型,只保留能对话的那些。
26
+ - 报错会说明是谁拒绝的、为什么:Portkey 预算或 scope 不够、Google IAM 权限缺失(不显示项目名)、API 未启用、组织只允许使用已保存的 provider 或配置、护栏拦截、Vertex 安全过滤、Claude 拒答。每条报错都带 Portkey trace id。
27
+ - `429`、`408`、`5xx` 会在同一账号上重试,这样该账号的缓存不会丢。
28
+
29
+ ## 登录前
30
+
31
+ 在 Portkey 的 **Model Catalog → Add Provider** 里添加 **Google Vertex AI**:上传服务账号 JSON,选好区域。服务账号需要 `roles/aiplatform.user` 角色。在 provider 上放行你要用的 Gemini 和 Claude 模型,插件列出的就是这些模型。
32
+
33
+ ## 安装与登录
34
+
35
+ ```sh
36
+ magpie plugin add @jack-yang/magpie-portkey-vertex
37
+ ```
38
+
39
+ 登录(Providers 页,或 `magpie plugin login portkey-vertex`)要填四项:
40
+
41
+ 1. **网关地址**:以 `/v1` 结尾,留空表示 `https://api.portkey.ai/v1`。
42
+ 2. **Provider slug**:形如 `@your-vertex-provider`。
43
+ 3. **API key**:Portkey key。
44
+ 4. **账号名称**(选填)。
45
+
46
+ 登录时,插件用同一个 key 调用 Portkey 控制面的 `GET /v1/providers/{slug}`,读取该 provider 的 GCP 项目和区域。如果这个 provider 没有这两个值,就改用这个 key 能看到的第一个带值的 Vertex provider。都读不到时,表单才会让你手动填,并说明读不到的原因。
47
+
48
+ ```sh
49
+ magpie claude portkey-vertex/gemini-4-neon
50
+ magpie claude portkey-vertex/anthropic.claude-opus-5-5
51
+ ```
52
+
53
+ ## 配置
54
+
55
+ 有三个开关,对所有账号生效。优先级:magpie 的 **Options** > 配置文件 > 默认值。
56
+
57
+ - `log`(默认 `false`):是否允许 Portkey 保存请求和响应的内容。关闭时每个请求都带 `x-portkey-debug: false`;token、成本和延迟照常记录。
58
+ - `retry`(默认 `2`,范围 `0`–`5`):遇到 `429`、`408`、`5xx` 时在同一账号上再试几次,间隔先 1 秒、再 2 秒,遵守 Retry-After;如果要求等一分钟以上,就不等了。
59
+ - `cacheTtl1h`(默认 `false`):把 Claude 的缓存断点保留 1 小时,而不是 5 分钟。写入价是 input 价的 2 倍(5 分钟的是 1.25 倍),所以只有在一小时内会回到同一段对话时才划算。Gemini 不受这个开关影响。
60
+
61
+ 配置文件 `~/.config/magpie/portkey-vertex/config.json` 里还可以写:
62
+
63
+ - `configId`:作为 `x-portkey-config` 发送。
64
+ - `headers`:额外的非机密请求头。
65
+ - `models`:始终列出的额外模型 id。
66
+ - `controlPlaneUrl`:自托管 Portkey 控制面的地址。
67
+
68
+ 配置文件里不能写机密。
69
+
70
+ ## 开发
71
+
72
+ ```sh
73
+ bun install --ignore-scripts
74
+ bun run check
75
+ MAGPIE_SOURCE_DIR=/tmp/magpie bun test test/magpie.test.ts
76
+ bun scripts/live.ts # 实测,读取 .env(见 .env.example),输出里的网关、key、项目都会打码
77
+ ```
78
+
79
+ Portkey 在这条路由上的具体行为、每条结论的出处和实测结果,见 [docs/portkey-reference.md](docs/portkey-reference.md)。
80
+
81
+ ## 致谢
82
+
83
+ 账号处理、配置分层和机密防护沿用 [magpie-portkey-bedrock](https://github.com/yangwudong/magpie-portkey-bedrock)。
84
+
85
+ ## 许可
86
+
87
+ MIT
@@ -0,0 +1,220 @@
1
+ # Portkey reference (Vertex AI)
2
+
3
+ What this provider relies on Portkey for, where each fact comes from, and what
4
+ is not yet confirmed. Written in December 2026 after reading Portkey's
5
+ documentation (`docs.portkey.ai`, `.md` pages via `llms.txt`) and the
6
+ open-source gateway (`Portkey-AI/gateway`, commit `669825c`). The Bedrock
7
+ sibling, `magpie-portkey-bedrock`, has a longer reference on logging, retries
8
+ and refusals; what holds for every route is not repeated here.
9
+
10
+ "Documented" means Portkey's documentation says it. "Source" means it was read
11
+ in the open-source gateway. "Unverified" means this provider depends on it and
12
+ nobody has yet confirmed it against the managed gateway.
13
+
14
+ ## Which route, and why
15
+
16
+ Portkey offers three ways to reach Gemini on Vertex AI:
17
+
18
+ | Route | Shape | Used here |
19
+ | --- | --- | --- |
20
+ | `/v1/chat/completions` | OpenAI's, translated by the gateway | no |
21
+ | `/v1/messages` | Anthropic's, Claude models only | no |
22
+ | `/v1/{vertex path}` (passthrough) | Vertex's own `generateContent` | **yes** |
23
+
24
+ The unified route translates, and translation loses what this provider exists
25
+ to keep: Gemini's `thoughtSignature` on every part (Portkey's OpenAI shape
26
+ carries it on tool calls only, and needs `strict_open_ai_compliance: false`
27
+ even for that), streamed thought summaries, and `parametersJsonSchema` tools.
28
+ The passthrough route carries Vertex's native request and response untouched.
29
+
30
+ - Portkey documents this route for Google's own GenAI SDK: `base_url` set to
31
+ the gateway, `x-portkey-api-key` and `x-portkey-provider` as headers, and
32
+ `vertexai=True` with a project and location. **Documented** —
33
+ `integrations/llms/vertex-ai`, "Google GenAI SDK (Gemini-only)". The SDK
34
+ there sends a dummy token; the gateway does the authentication.
35
+ - "Gemini models only" is said of Google's GenAI SDK, which speaks
36
+ `generateContent`. **Documented**, same section. Claude is served on the
37
+ same passthrough route in its own protocol (see "Claude on Vertex").
38
+
39
+ ## What this provider sends
40
+
41
+ `POST {gateway}/v1/v1beta1/projects/{project}/locations/{location}/publishers/google/models/{id}:streamGenerateContent?alt=sse`
42
+
43
+ built by `@ai-sdk/google-vertex` (its `edge` entry, in express mode — see
44
+ below), with these headers:
45
+
46
+ | Header | Value | From |
47
+ | --- | --- | --- |
48
+ | `x-portkey-api-key` | the account's key | the sign-in form |
49
+ | `x-portkey-provider` | the account's Vertex provider slug | the sign-in form |
50
+ | `x-portkey-config` | a saved config's id, only when `configId` is set | `configId` |
51
+ | `x-portkey-debug` | `false` | the `log` switch |
52
+
53
+ No `Authorization` and no `x-goog-api-key` is sent.
54
+
55
+ ### How the path is resolved
56
+
57
+ - The gateway answers `POST /v1/*` with its proxy handler. **Source** —
58
+ `src/index.ts`, `app.post('/v1/*', requestValidator, proxyHandler)`.
59
+ - The proxy strips the first `/v1` and appends the rest to the provider's base
60
+ URL. **Source** — `ProviderContext.getProxyPath`.
61
+ - For Vertex the base URL is the region host of the saved provider:
62
+ `https://{region}-aiplatform.googleapis.com`, or
63
+ `https://aiplatform.googleapis.com` for `global`. **Source** —
64
+ `providers/google-vertex-ai/api.ts`, `getBaseURL`. The managed gateway also
65
+ routes `us`/`eu` to `aiplatform.{region}.rep.googleapis.com`. **Documented**
66
+ — `integrations/llms/vertex-ai`, "Supported Regions".
67
+ - So the project and location in the path must be the provider's: the gateway
68
+ takes the host from the provider and the path from the request, and Google
69
+ refuses a path whose location does not match its host. That is why the
70
+ sign-in form asks for both. **Inferred** from the two facts above.
71
+ - The path's API version (`v1beta1`) is the SDK's choice and goes through as
72
+ it is. Portkey's own unified route uses `v1`. Both are Google's.
73
+ - The gateway treats a Vertex request whose URL contains `stream` as
74
+ streaming, and splits Gemini's SSE on `\r\n\r\n` for `/publishers/google`
75
+ paths. **Source** — `utils.ts`, `getStreamingMode`, `getStreamModeSplitPattern`.
76
+ With no response transformer on the proxy route, events are passed through
77
+ as Google sent them.
78
+
79
+ ### Why no Authorization header
80
+
81
+ - On the proxy route the gateway copies every client header that is not an
82
+ `x-portkey-*` header onto the upstream request, **after** the headers it set
83
+ from the provider. **Source** — `handlerUtils.ts`, `constructRequestHeaders`:
84
+ `{ ...baseHeaders, ...headers, ...forwardHeadersMap, ...proxyHeaders }`.
85
+ - The Vertex provider sets `Authorization: Bearer {token}`, minted from the
86
+ saved service account. **Source** — `providers/google-vertex-ai/api.ts`,
87
+ `headers`.
88
+ - So in the open-source gateway a client `Authorization` replaces Google's
89
+ token. On the managed gateway, requests without one succeed for both
90
+ families. **Observed**. This provider sends none: nothing needs one, and
91
+ the Portkey key should not go to Google.
92
+ - `@ai-sdk/google-vertex` would otherwise authenticate on its own: with no API
93
+ key it loads Google credentials (`google-auth-library`, or a JWT in the edge
94
+ build). Express mode (an API key) skips that and sends `x-goog-api-key`
95
+ instead. The provider uses express mode with a placeholder key and removes
96
+ the header in its `fetch`. Express mode has one consequence: tuned-model
97
+ endpoints are refused by the SDK. They are not Gemini publisher models and
98
+ not on this route anyway.
99
+
100
+ ## The provider in Portkey
101
+
102
+ - Add **Google Vertex AI** in Model Catalog with a **service account JSON**
103
+ and a region; its slug is what the sign-in form asks for. **Documented** —
104
+ `integrations/llms/vertex-ai`, "Add Provider in Model Catalog".
105
+ - A provider set up with only a project and region (no service account)
106
+ expects an OAuth access token in `Authorization` on every request.
107
+ **Documented**, same page, "Authentication Note". This provider sends none,
108
+ so such a provider does not work with it.
109
+ - A self-hosted gateway on GKE with Workload Identity mints the token from the
110
+ metadata server, and needs no service account file. **Documented**.
111
+ - The service account needs `roles/aiplatform.user`. **Documented**.
112
+
113
+ ## The model list
114
+
115
+ - `GET /v1/models` with an `@provider-slug` proxies the provider's own list;
116
+ Google Vertex AI is among the providers that support it. **Documented** —
117
+ `api-reference/inference-api/models/models`, "Supported Providers".
118
+ - What the managed gateway returns for Vertex — ids as `gemini-…`,
119
+ `google.gemini-…` or `publishers/google/models/gemini-…` — is
120
+ **unverified**; all three are read as the bare id, and anything that is not
121
+ `gemini-*` is dropped.
122
+ - The route needs the `models.list` or `completions.write` scope. A refusal
123
+ keeps the catalog and is logged. **Documented**.
124
+
125
+ ## Thinking and thought signatures
126
+
127
+ - Gemini 3 and later take `thinkingConfig.thinkingLevel`; Gemini 2.5 takes
128
+ `thinkingBudget`. The SDK chooses per model; `includeThoughts: true` asks
129
+ for thought summaries. Google's documentation, applied by
130
+ `@ai-sdk/google`.
131
+ - Gemini returns `thoughtSignature` on parts (text, thought, function call)
132
+ and validates it when the turn is replayed; a Gemini 3 function call
133
+ replayed without one is refused with a 400 unless it carries the
134
+ documented `skip_thought_signature_validator`. The SDK injects that
135
+ sentinel when a signature is missing. Google's documentation
136
+ (`ai.google.dev/gemini-api/docs/thought-signatures`), implemented in
137
+ `@ai-sdk/google`.
138
+ - This provider keeps each signature on its pi block (`textSignature`,
139
+ `thinkingSignature`, `thoughtSignature`) and replays it only to the same
140
+ provider and model. magpie's Anthropic bridge keeps tool-call signatures by
141
+ call id across turns (`keepSig` in its `pi.js`); thinking and text
142
+ signatures go back through Anthropic's `signature` field.
143
+
144
+ ## Logging
145
+
146
+ - `x-portkey-debug: false` keeps request and response content out of
147
+ Portkey's logs on the passthrough route. **Observed** (confirmed in the
148
+ Logs page, December 2026).
149
+
150
+ ## Caching
151
+
152
+ - Gemini caches prompts implicitly on Vertex: with a ~11K token system
153
+ prompt, the first call read nothing and the next four read 8,895 tokens
154
+ each. **Observed**; the cached part is reported as
155
+ `cachedContentTokenCount` and billed at the cache-read price. Nothing has
156
+ to be marked, and no checkpoint is sent. Explicit context caches (`cachedContents`) exist too and are
157
+ not used. **Documented** — `integrations/llms/vertex-ai`, "Explicit
158
+ context caching".
159
+ - `x-portkey-debug: false` turns off Portkey's own response cache, which is a
160
+ different thing. **Documented** — `product/ai-gateway/cache-simple-and-semantic`.
161
+
162
+ ## Claude on Vertex
163
+
164
+ - Portkey's catalog names Vertex's Claude models `anthropic.claude-…`.
165
+ **Documented** — `integrations/llms/vertex-ai` ("prepend `anthropic.`").
166
+ - On the passthrough route Claude takes Anthropic's Messages body plus
167
+ `anthropic_version: "vertex-2023-10-16"`, at
168
+ `{gateway}/v1/v1/projects/{p}/locations/{l}/publishers/anthropic/models/{id}:streamRawPredict`
169
+ (`:rawPredict` without streaming), and answers Anthropic's own SSE.
170
+ **Observed**, December 2026. The same path with `:generateContent` is
171
+ refused: `Publisher Model … is not servable in region global`
172
+ (`FAILED_PRECONDITION`).
173
+ - No `Authorization` is needed here either. **Observed**.
174
+ - Claude caches only at explicit `cache_control` checkpoints. With a ~13K
175
+ token system prompt the first call wrote 13,202 tokens and the following
176
+ calls read 13,202; a checkpoint on the last message also wrote the new
177
+ turn (144 tokens). `ttl: "1h"` is accepted and reported as
178
+ `ephemeral_1h_input_tokens`. **Observed**.
179
+ - Thinking on Opus 5.5 and Sonnet 5.5 is adaptive and cannot be turned off;
180
+ `effort` (`low`…`max`) sets how much. At `low` neither model showed a
181
+ thinking block on simple questions; at `max` both did, signed. Sonnet 5.5
182
+ at `high` often skips it. **Observed**. A replayed signed thinking block
183
+ was accepted. **Observed**.
184
+
185
+ ## Where project and region come from
186
+
187
+ - They are on the saved provider: `GET {control plane}/providers/{slug}`
188
+ (Admin API, "Retrieve Provider") returns `model_config.vertexRegion` and,
189
+ for a service-account provider, `model_config.vertexServiceAccountJson`
190
+ with its `project_id` (the private key masked out). A provider saved with
191
+ a project field has `vertexProjectId` instead. **Documented** — the Admin
192
+ API's `VertexAIConfiguration` schema; **observed** with an ordinary
193
+ workspace API key (no admin scope needed). `GET /integrations/{slug}` was
194
+ refused (403) with the same key.
195
+ - The control plane is `https://api.portkey.ai/v1`, not the gateway: the
196
+ gateway answers `/v1/providers` as a passthrough request and refuses it
197
+ without a provider header. **Observed**. A self-hosted control plane is
198
+ set with `controlPlaneUrl` in the config file.
199
+ - The plugin reads the slug's own provider at sign-in, and failing that the
200
+ first Vertex provider of the key that has both values (`GET /providers`).
201
+ Only if neither answers does the form ask.
202
+ - The project must be in the path: without one Google answers
203
+ `RESOURCE_PROJECT_INVALID`, and `projects/-` is refused. **Observed**.
204
+
205
+ ## Model discovery
206
+
207
+ - `GET /v1/models` with `x-portkey-fetch-integrated-models: true` returns the
208
+ provider's Model Catalog — what the integration allows — with ids
209
+ `@{provider}/{slug}` and a `slug` field. Without the header it proxies
210
+ Google's own list, which includes models the integration refuses with a
211
+ 412 `model_not_allowed`. **Observed**.
212
+ - The catalog also lists image, speech, transcription, embedding models and
213
+ self-deployed endpoints (`endpoints.{id}`); they are dropped. It can list a
214
+ model Google no longer serves (`gemini-3-pro-preview` answered 404).
215
+
216
+ ## Open questions
217
+
218
+ 1. Block Inline Configs refusals on this route have the same body as on
219
+ `/v1/chat/completions` (not seen: this organization does not block).
220
+ 2. Explicit Gemini context caches (`cachedContents`) through the passthrough.
package/icon.svg ADDED
@@ -0,0 +1 @@
1
+ <svg width='1024' height='1024' viewBox='0 0 24 24' fill='none' xmlns='http://www.w3.org/2000/svg'><path d='M14.9682 1C16.9392 1 18.7509 2.06556 19.737 3.78016L22.8197 9.13893L22.8659 9.22081C23.8204 10.9464 23.8096 13.0586 22.8315 14.7759L19.7409 20.1999C18.7577 21.9258 16.9395 23 14.9603 23H8.98853C6.99509 23 5.16598 21.9107 4.18804 20.1654L1.14939 14.7413C0.181824 13.0145 0.185677 10.8963 1.1603 9.17347L4.19193 3.8147C5.17291 2.08063 6.99537 1.00001 8.98061 1H14.9682ZM8.98061 3.73995C7.99508 3.73995 7.07233 4.27616 6.56888 5.1661L3.53726 10.5249C3.03506 11.4127 3.0329 12.5089 3.53143 13.399L6.57098 18.8239L6.59476 18.8657C7.10183 19.7365 8.0144 20.26 8.98853 20.2601H14.9603C15.9429 20.2601 16.8634 19.7271 17.3679 18.8414L20.4576 13.4174C20.9698 12.5182 20.9676 11.4043 20.4518 10.5074L17.37 5.1477C16.8641 4.26815 15.9467 3.73995 14.9682 3.73995H8.98061ZM12.3728 6.68801C12.9418 6.21604 13.7855 6.28304 14.2735 6.84424L17.6296 10.7051L17.6594 10.7401C18.2746 11.4819 18.2647 12.5642 17.6296 13.2949L14.2735 17.1558C13.7855 17.717 12.9418 17.784 12.3728 17.312L12.3458 17.2891C11.7768 16.792 11.7179 15.9261 12.2137 15.3559L15.131 12L12.2137 8.64414C11.7179 8.07385 11.7768 7.20795 12.3458 6.71087L12.3728 6.68801Z' fill='url(#paint0_linear_3399_57332)'/><defs><linearGradient id='paint0_linear_3399_57332' x1='-11.9082' y1='6.95833' x2='26.3273' y2='28.8985' gradientUnits='userSpaceOnUse'><stop offset='0.173094' stop-color='#00A3FF'/><stop offset='0.899073' stop-color='#FF0F00'/></linearGradient></defs></svg>
package/index.ts ADDED
@@ -0,0 +1,57 @@
1
+ // Portkey Vertex: Google Vertex AI's Gemini and Claude through a Portkey
2
+ // gateway's passthrough route, as a pi provider. Each module holds one concern; this file only registers it.
3
+ import type { Api } from "@earendil-works/pi-ai";
4
+ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
5
+ import { formToCredentials, portkeyLoginForm } from "./src/account.ts";
6
+ import { loadConfig, readMagpieOptions } from "./src/config.ts";
7
+ import { discoverGatewayModels, toProviderModels } from "./src/models.ts";
8
+ import { createPortkeyStream } from "./src/stream.ts";
9
+
10
+ export { accountFormOf, keyName, lookupVertexRoute, normalizeBaseUrl, type PortkeyAccountForm } from "./src/account.ts";
11
+ export { CONFIG_PATH, DEFAULT_SWITCHES, MAGPIE_PLUGINS_PATH, loadConfig, readConfig, readMagpieOptions, type PortkeyConfig } from "./src/config.ts";
12
+ export { discoverGatewayModels, isChatModel, toProviderModels, type GatewayModelContext } from "./src/models.ts";
13
+ export { createPortkeyStream, vertexBaseUrl } from "./src/stream.ts";
14
+
15
+ const PROVIDER_ID = "portkey-vertex";
16
+ const API_ID = "portkey-vertex-ai-sdk" as Api;
17
+ // This package's own name, as its entry in magpie's plugins.json is titled.
18
+ const PACKAGE_NAME = "@jack-yang/magpie-portkey-vertex";
19
+
20
+ // Every request goes to its own account's gateway, so the provider has no
21
+ // endpoint of its own. pi still wants one to register with; this one is never
22
+ // meant to be contacted — `.invalid` (RFC 2606) resolves nowhere, and a
23
+ // request reaching streamSimple without an account gateway is refused before
24
+ // its key could be sent anywhere else.
25
+ const UNROUTED_BASE_URL = "https://account-gateway.invalid/v1";
26
+
27
+ export default function portkeyVertexExtension(pi: ExtensionAPI): void {
28
+ const over = readMagpieOptions(undefined, PACKAGE_NAME);
29
+ const config = loadConfig(undefined, undefined, console.error, over);
30
+ const models = toProviderModels(config.models, config.headers);
31
+
32
+ pi.registerProvider(PROVIDER_ID, {
33
+ name: "Portkey Vertex",
34
+ baseUrl: UNROUTED_BASE_URL,
35
+ api: API_ID,
36
+ headers: config.headers,
37
+ models,
38
+ streamSimple: createPortkeyStream(config.headers, config),
39
+ async refreshModels(context) {
40
+ return await discoverGatewayModels(config, context);
41
+ },
42
+ oauth: {
43
+ name: "Portkey Vertex AI gateway",
44
+ async login(callbacks) {
45
+ return formToCredentials(await portkeyLoginForm(callbacks, { controlPlaneUrl: config.controlPlaneUrl }));
46
+ },
47
+ // A Portkey key never expires on its own; nothing to renew. The far
48
+ // future expiry keeps pi's refresh path from ever running.
49
+ async refreshToken(credentials) {
50
+ return credentials;
51
+ },
52
+ getApiKey(credentials) {
53
+ return String((credentials as { access?: string }).access ?? "");
54
+ },
55
+ },
56
+ });
57
+ }