Compare commits

..
Author SHA1 Message Date
Daniel Salazar af01a554fe feat(gui): a top-up asked for from a page names the app it runs as (#4637)
The framed top-up passes the deployment's dialog the app the framing page's
origin resolves to, as the framed upgrade does. The dashboard link puter.js
opens when no frame can show carries the app in `topup_app`.
2026-11-13 23:00:39 -07:00
Nariman JelvehandClaude Opus 5.5 3e11e7dc79 refactor: keep folder sort and layout in puter.kv, drop /set_sort_by and /set_layout (#4642)
Explorer saved each folder's sort and layout through POST /set_sort_by and
/set_layout, which wrote them onto the folder's fsentries row. stat only
returns `layout`, so a saved sort was never read back.

The GUI now keeps them in the user's key-value store, one key per folder
uid (`folder_view:<uid>` holding `{ sort_by, sort_order, layout }`), via
helpers/folderViewPrefs.js. A change to one preference keeps the others.
A folder with nothing stored falls back to the layout its fsentry still
carries, then to the defaults, so existing layouts carry over.

refresh_item_container reads the folder's view after stat and re-sorts
items that went in under the previous sort; a view for a folder the
window has left is ignored. update_window_path no longer applies sort and
layout itself, since the refresh it starts does.

With no callers left, DesktopController and its two routes are removed.
The fsentries columns and stat's `layout` field stay.

Co-authored-by: Claude Opus 5.5 <[email protected]>
2026-11-13 21:34:37 -07:00
Nariman JelvehandClaude Opus 5.5 5b1b5b8a7a fix(puter-js): apps launched from desktop.puter.com take its messages (#4640)
In an app, puter.js trusted only https://puter.com as the GUI origin, so
every desktop message from desktop.puter.com failed the origin check and
was dropped. The desktop waits for the app's answer to windowWillClose,
so app windows there never closed; itemsOpened, drag-and-drop, focus,
theme and locale changes were lost the same way, and a reauth request
was posted to the wrong origin.

An app now takes its GUI origin from the launch URL's `puter.origin`,
but only when that is one of Puter's own desktops (exactly
https://puter.com or https://desktop.puter.com); anything else keeps
https://puter.com, so a page that frames an app still can't drive it.
An explicit PUTER_ORIGIN still wins, and a top-level page never reads it.

Co-authored-by: Claude Opus 5.5 <[email protected]>
2026-11-13 20:51:33 -07:00
392 changed files with 4425 additions and 8626 deletions

No files matched your search

+5 -8
View File
@@ -2,7 +2,7 @@
# The last matching rule takes precedence.
* @Salazareo
- @Salazareo
# GUI
@@ -18,10 +18,7 @@
# Backend AI
/src/backend/controllers/puterai/ @404oops @ProgrammerIn-wonderland @Salazareo
/src/backend/drivers/ai/ @404oops @ProgrammerIn-wonderland @Salazareo
/src/backend/util/aiCostFactor* @404oops @ProgrammerIn-wonderland @Salazareo
/src/backend/util/aiPrices* @404oops @ProgrammerIn-wonderland @Salazareo
/src/backend/util/aiProviderKey* @404oops @ProgrammerIn-wonderland @Salazareo
/src/backend/drivers/ai-\*/ @404oops @ProgrammerIn-wonderland @Salazareo
# Backend: AI, hosting, workers, networking, and WebDAV
@@ -32,7 +29,7 @@
/src/backend/drivers/workers/ @ProgrammerIn-wonderland @Salazareo
/src/backend/services/localworker/ @ProgrammerIn-wonderland @Salazareo
/src/backend/stores/subdomain/ @ProgrammerIn-wonderland @Salazareo
/extensions/workerSandbox* @ProgrammerIn-wonderland @Salazareo
/extensions/workerSandbox\* @ProgrammerIn-wonderland @Salazareo
/src/worker/ @ProgrammerIn-wonderland @Salazareo
/src/worker-types/ @ProgrammerIn-wonderland @Salazareo
/src/backend/controllers/peer/ @ProgrammerIn-wonderland @Salazareo
@@ -45,7 +42,7 @@
/src/backend/services/permission/ @jfcastro92 @Salazareo
/src/backend/services/acl/ @jfcastro92 @Salazareo
/src/backend/services/team/ @jfcastro92 @Salazareo
/src/backend/services/apps/AppPermissionService* @jfcastro92 @Salazareo
/src/backend/services/apps/AppPermissionService\* @jfcastro92 @Salazareo
/src/backend/stores/share/ @jfcastro92 @Salazareo
/src/backend/stores/permission/ @jfcastro92 @Salazareo
/src/backend/stores/group/ @jfcastro92 @Salazareo
@@ -62,7 +59,7 @@
/src/puter-js/tests/api/suites/hosting.suite.ts @ProgrammerIn-wonderland @Salazareo
/src/puter-js/tests/api/suites/workers.suite.ts @ProgrammerIn-wonderland @Salazareo
/src/puter-js/tests/api/suites/net.suite.ts @ProgrammerIn-wonderland @Salazareo
/src/puter-js/src/modules/Peer* @ProgrammerIn-wonderland @Salazareo
/src/puter-js/src/modules/Peer\* @ProgrammerIn-wonderland @Salazareo
# SDK: sharing and permissions
@@ -31,9 +31,10 @@ jobs:
run: npm ci
- name: Run AI provider integration tests
# Only the provider tests: the other `*.integration.test.ts`
# files need no credentials and run with the backend suite.
run: npm run test:backend -- $(git ls-files 'src/backend/drivers/ai/**/*.integration.test.ts')
# Vitest treats positional args as filename substring filters.
# The pattern matches every `*.integration.test.ts` file and
# nothing else — the regular backend test suite stays out.
run: npm run test:backend -- integration.test
env:
CI: 'true'
PUTER_TEST_AI_CLAUDE_API_KEY: ${{ secrets.PUTER_TEST_AI_CLAUDE_API_KEY }}
@@ -48,15 +49,6 @@ jobs:
PUTER_TEST_AI_MOONSHOT_API_KEY: ${{ secrets.PUTER_TEST_AI_MOONSHOT_API_KEY }}
PUTER_TEST_AI_ZAI_API_KEY: ${{ secrets.PUTER_TEST_AI_ZAI_API_KEY }}
PUTER_TEST_AI_ELEVENLABS_API_KEY: ${{ secrets.PUTER_TEST_AI_ELEVENLABS_API_KEY }}
PUTER_TEST_AI_ALIBABA_API_KEY: ${{ secrets.PUTER_TEST_AI_ALIBABA_API_KEY }}
PUTER_TEST_AI_AZURE_OPENAI_API_KEY: ${{ secrets.PUTER_TEST_AI_AZURE_OPENAI_API_KEY }}
PUTER_TEST_AI_AZURE_OPENAI_API_URL: ${{ secrets.PUTER_TEST_AI_AZURE_OPENAI_API_URL }}
PUTER_TEST_AI_BYTEPLUS_API_KEY: ${{ secrets.PUTER_TEST_AI_BYTEPLUS_API_KEY }}
PUTER_TEST_AI_HOONIFY_API_KEY: ${{ secrets.PUTER_TEST_AI_HOONIFY_API_KEY }}
PUTER_TEST_AI_INFRON_API_KEY: ${{ secrets.PUTER_TEST_AI_INFRON_API_KEY }}
PUTER_TEST_AI_KLING_API_KEY: ${{ secrets.PUTER_TEST_AI_KLING_API_KEY }}
PUTER_TEST_AI_META_API_KEY: ${{ secrets.PUTER_TEST_AI_META_API_KEY }}
PUTER_TEST_AI_MINIMAX_API_KEY: ${{ secrets.PUTER_TEST_AI_MINIMAX_API_KEY }}
# Realtime conversations through the SDK against live OpenAI. Builds
# the SDK bundle these tests load itself, so it runs even when the
+1 -6
View File
@@ -9,7 +9,6 @@ Use these as the source of truth before exploring further:
- [README.md](README.md) — project overview and quickstart.
- [doc/architecture.md](doc/architecture.md) — backend layered stack (controllers → drivers → services → stores → clients), `PuterServer` wiring, `Context` (ALS), actors, and extensions.
- [doc/contributing-apis.md](doc/contributing-apis.md) — adding and maintaining public APIs end to end (backend surface → puter.js → types → docs → tests). Follow it for any API work.
- [doc/ai-drivers.md](doc/ai-drivers.md) — where AI driver code lives (interfaces, providers, protocols, shared), the import rules between those folders, and how to add a provider or an AI interface.
- [doc/pagination.md](doc/pagination.md) — the one pagination convention for list APIs (limit/cursor/offset/includeTotal, envelope shape, cursor semantics).
- [doc/alarms.md](doc/alarms.md) — raising alarms and picking a severity (what pages, what only gets recorded), plus the config that routes them.
- [doc/self-hosting.md](doc/self-hosting.md) — running Puter outside hosted infra.
@@ -71,7 +70,7 @@ Commit messages and PR descriptions never credit anyone or mention a bug report.
- **Test new behavior.** Every new function, endpoint, driver method, or logic branch gets a test; every bug fix gets a regression test that fails before the fix. If something is genuinely hard to test, skip it but say so in the PR.
- **Boy Scout Rule, proportional to the change.** In files you're already touching, fix the typo, the dead import, the missing test, the bit you had to read twice. Don't ride a refactor along with a bug fix.
- **Understand what you commit.** AI assistance is fine; shipping code you couldn't have written, debugged, or defended in review is not.
- **A limit change is not done until the docs change with it.** Every rate limit, concurrency cap, quota, and allowance in the code is published in [src/docs/src/rate-limits-and-quotas.md](src/docs/src/rate-limits-and-quotas.md) — an undisclosed limit is one developers discover as a service failure. If a PR moves any of these numbers, the same PR updates that page. The numbers live in `src/backend/controllers/fs/limits.ts` (filesystem), `src/backend/drivers/ai/shared/aiLimits.ts` (all AI drivers), per-driver `rateLimit`/`concurrent` configs, and `src/backend/data/subPolicies/` (free-tier allowances).
- **A limit change is not done until the docs change with it.** Every rate limit, concurrency cap, quota, and allowance in the code is published in [src/docs/src/rate-limits-and-quotas.md](src/docs/src/rate-limits-and-quotas.md) — an undisclosed limit is one developers discover as a service failure. If a PR moves any of these numbers, the same PR updates that page. The numbers live in `src/backend/controllers/fs/limits.ts` (filesystem), `src/backend/drivers/util/aiLimits.ts` (all AI drivers), per-driver `rateLimit`/`concurrent` configs, and `src/backend/data/subPolicies/` (free-tier allowances).
---
@@ -98,10 +97,6 @@ Layer rules:
- **Drivers and controllers stay thin.** Domain rules a second entry point needs (signup, app CRUD, worker deploy, AI routing/billing) live in a service, not in the first driver or controller that needed them.
- **Prefer explicit arguments over [`Context`](src/backend/core/context.ts) (ALS).** Use it only for request-scoped values that would otherwise thread through many layers; today that's mostly `actor` and `req`.
### AI drivers
AI drivers live in [src/backend/drivers/ai/](src/backend/drivers/ai/): `interfaces/` holds the Puter-facing drivers, `providers/<vendor>/<interface>/` holds vendor code, `protocols/` holds wire protocols several vendors speak, and `shared/` holds helpers more than one interface uses. Follow [doc/ai-drivers.md](doc/ai-drivers.md) when adding or moving AI code.
### Extensions
[extensions/](extensions/) is for **non-crucial parts of the system**: Puter must still work with the extension removed. If core needs to call it, it belongs in core ([whoami](extensions/whoami.ts) is the cautionary example). Inside an extension, follow the same layered structure, unless it only needs a few route handlers, in which case the `extension.get/post/...` helpers are enough on their own. The `extension` API is in [doc/architecture.md](doc/architecture.md#extension-api).
-153
View File
@@ -1,153 +0,0 @@
# AI Drivers
Where the AI driver code lives under [src/backend/drivers/ai/](../src/backend/drivers/ai/), what each folder owns, and how to add a provider or a new AI interface. The general layer rules are in [architecture.md](architecture.md) and [AGENTS.md](../AGENTS.md#backend). This page covers what is specific to AI.
## Layout
```text
src/backend/drivers/ai/
├── interfaces/ Puter-facing drivers, one folder per AI interface
├── providers/ Vendor code, as providers/<vendor>/<interface>/
├── protocols/ Wire protocols that several vendors speak
└── shared/ Helpers that more than one interface uses
```
| Folder | Owns | Example |
| --- | --- | --- |
| `interfaces/<interface>/` | The driver class, the interface's contracts in `types.ts`, its provider base class where one exists, and the helpers and tests that apply to every provider of that interface. | [interfaces/chat/ChatCompletionDriver.ts](../src/backend/drivers/ai/interfaces/chat/ChatCompletionDriver.ts) |
| `providers/<vendor>/<interface>/` | One vendor's implementation of one interface: the adapter, its model catalog and prices, its fixtures, and its tests. | [providers/gemini/video/](../src/backend/drivers/ai/providers/gemini/video/) |
| `providers/<vendor>/` | Helpers that several of that vendor's interfaces use. | [providers/gemini/geminiRest.ts](../src/backend/drivers/ai/providers/gemini/geminiRest.ts) |
| `protocols/openaiCompatible/` | The OpenAI wire protocol: the base provider class, message and usage conversion, and parameter mapping for Chat Completions and Responses. | [protocols/openaiCompatible/OpenAICompatProvider.ts](../src/backend/drivers/ai/protocols/openaiCompatible/OpenAICompatProvider.ts) |
| `shared/` | Helpers that more than one interface uses: limits, the provider registry, the remote catalog cache, job polling, streamed charging, and media input. | [shared/aiLimits.ts](../src/backend/drivers/ai/shared/aiLimits.ts) |
Helpers that non-AI code also uses stay in [src/backend/drivers/util/](../src/backend/drivers/util/): file input, data URIs, upstream error mapping, and stream piping.
AI code that a service also needs lives in [src/backend/util/](../src/backend/util/), because a service can't import from the driver layer. The realtime relay in `services/realtime/` bills through the same cost factor as the drivers, so these three files are there:
| File | Holds |
| --- | --- |
| [aiCostFactor.ts](../src/backend/util/aiCostFactor.ts) | `withAiCostFactor`, the metering view every AI driver and the realtime relay record usage through, and `withAiCreditHold`. |
| [aiProviderKey.ts](../src/backend/util/aiProviderKey.ts) | `readProviderKey`, which reads an API key from `config.providers` blocks. |
| [aiPrices.ts](../src/backend/util/aiPrices.ts) | Vendor prices that a driver and a service both bill. |
### Interfaces
| Folder | Driver | `driverInterface` | `driverName` | Key in `puterDrivers` |
| --- | --- | --- | --- | --- |
| `interfaces/chat/` | `ChatCompletionDriver` | `puter-chat-completion` | `ai-chat` | `aiChat` |
| `interfaces/image/` | `ImageGenerationDriver` | `puter-image-generation` | `ai-image` | `aiImage` |
| `interfaces/video/` | `VideoGenerationDriver` | `puter-video-generation` | `ai-video` | `aiVideo` |
| `interfaces/textToSpeech/` | `TTSDriver` | `puter-tts` | `ai-tts` | `aiTts` |
| `interfaces/speechToText/` | `SpeechToTextDriver` | `puter-speech2txt` | `ai-speech2txt` | `aiSpeech2Txt` |
| `interfaces/speechToSpeech/` | `VoiceChangerDriver` | `puter-speech2speech` | `ai-speech2speech` | `aiSpeech2Speech` |
| `interfaces/ocr/` | `OCRDriver` | `puter-ocr` | `ai-ocr` | `aiOcr` |
Folder names are not runtime identifiers. `driverInterface`, `driverName`, driver aliases, the keys under `providers` in config, provider ids, model ids, and usage keys are public contracts. Moving or renaming a file never changes them.
## Request flow
A call goes from the interface to a provider, and from there to a shared protocol when the vendor speaks one:
```text
interfaces/chat/ChatCompletionDriver.ts
validates the call, picks the model and provider, holds credits
→ providers/deepseek/chat/DeepSeekProvider.ts
DeepSeek's catalog, endpoint, and request differences
→ protocols/openaiCompatible/OpenAICompatProvider.ts
the OpenAI-compatible request, response, and stream handling
```
A provider whose vendor has its own API, such as `ClaudeProvider` or `ElevenLabsTTSProvider`, implements the interface's contract directly and uses no protocol.
## Import rules
- `shared/` imports nothing from `interfaces/`, `providers/`, or `protocols/`.
- Nothing in `services/`, `stores/`, or `clients/` imports from `drivers/ai/`. Code that a service and a driver both need goes in `src/backend/util/`.
- `providers/` and `protocols/` import an interface's `types.ts` and helpers. They never import a driver class. Their tests can, to drive a provider through the real driver.
- Only an interface's driver file imports providers, to register them. The interface's helpers don't.
- An interface doesn't import another interface. When a second interface needs a helper, move the helper to `shared/`, or to `providers/<vendor>/` when it belongs to one vendor.
- A vendor folder imports another vendor's folder only when it serves that vendor's models. `providers/azure/` reads the OpenAI and xAI catalogs for that reason.
## Where a file goes
| The code is | It goes in |
| --- | --- |
| Input validation, gates, routing, credit holds, or anything else every provider of the interface gets | `interfaces/<interface>/` |
| A request or response contract that providers implement | `interfaces/<interface>/types.ts` |
| A vendor's endpoint, request shape, catalog, prices, or quirks | `providers/<vendor>/<interface>/` |
| A vendor's auth, REST plumbing, or constants that two of its interfaces use | `providers/<vendor>/` |
| Behavior of a wire protocol that several vendors speak | `protocols/<protocol>/` |
| A helper that two or more interfaces use and that belongs to no vendor | `shared/` |
| A helper that non-AI drivers or controllers also use | `src/backend/drivers/util/` |
| AI code that a service also uses | `src/backend/util/`, in a file named `ai<Something>.ts` |
Keep a helper next to its only consumer. Move it up when a second consumer appears, not before.
When one Puter option becomes a different request shape per vendor, the interface reads the option and each dialect maps it. The chat `compaction` option is read in `interfaces/chat/utils/compaction.js`, mapped for OpenAI by `toOpenAiContextManagement` in `protocols/openaiCompatible/openaiParams.ts`, and mapped for Anthropic by `toAnthropicContextManagement` in `providers/claude/chat/contextManagement.ts`. Tools work the same way: `normalize_tools_object` in the interface, then `toolsToWire` for OpenAI and `claudeToolPolicy` for Anthropic.
Tests sit next to the code they test. Fixtures go in a `fixtures/` folder next to the test that reads them.
## Adding a provider
1. Create `providers/<vendor>/<interface>/`. Use the vendor's existing folder if it has one, so all of a vendor's interfaces stay together.
2. Implement the interface's contract from `interfaces/<interface>/types.ts`:
| Interface | Contract | Base class |
| --- | --- | --- |
| chat | `IChatProvider` | `OpenAICompatProvider` for an OpenAI-compatible vendor |
| image | `IImageProvider` | none |
| video | `IVideoProvider` | `VideoProvider` |
| textToSpeech | `ITTSProvider` | none |
| speechToText | `ISpeechToTextProvider` | `SpeechToTextProvider` |
| ocr | `IOcrProvider` | none |
Speech to speech has one provider, so `VoiceChangerDriver` uses `ElevenLabsSpeechToSpeechProvider` directly. Add a contract to its `types.ts` when a second provider needs one.
3. Put the model catalog and prices in the same folder, as `models.ts` or `costs.ts`. A provider that loads its catalog from the vendor wraps the fetch in `cachedRemoteCatalog` from `shared/`.
4. Register the provider in the interface's driver: in `#registerProviders()`, or in `onServerStart()` for OCR and speech to speech. A chat provider built from an API key and an optional base URL is one row in `KEYED_PROVIDERS` instead. Text to speech and speech to text also list the provider id and its aliases in `providerAliases.ts`.
5. Read credentials from `config.providers[<key>]`. The key is part of the self-hosting config contract, so reuse the vendor's existing key when it has one.
6. Add tests next to the provider. Mock the vendor SDK or `fetch` at the network boundary and drive the provider through the real driver where you can. A live test is a `*.integration.test.ts` file that skips itself, with `skipUnlessEnv` from [testEnv.ts](../src/backend/testEnv.ts), unless its `PUTER_TEST_AI_*` variable is set. Helpers for provider tests are in [shared/integrationTestUtil.ts](../src/backend/drivers/ai/shared/integrationTestUtil.ts).
7. For a chat provider, add its static catalog to `CATALOGS` in [modelCatalogs.test.ts](../src/backend/drivers/ai/interfaces/chat/modelCatalogs.test.ts) and the provider to [providerConsistency.test.ts](../src/backend/drivers/ai/interfaces/chat/providerConsistency.test.ts). The catalog test fails when a `providers/*/chat/` file with "model" in its name exports a catalog that isn't listed.
8. Add the provider's config key to the `providers` config of the interface's [fuzz suite](#fuzz-tests), and its id to the list of providers the suite expects.
9. Update the published docs for the models and limits the provider adds, following [contributing-apis.md](contributing-apis.md).
### OpenAI-compatible vendors
A vendor that speaks the OpenAI Chat Completions protocol extends `OpenAICompatProvider` and overrides only what differs. It still gets its own `providers/<vendor>/chat/` folder.
Vendors share one constants-only folder only when at least six of them differ in nothing but base URL and model catalog. Each then becomes one data file under `providers/openaiCompatible/providers/`, read by a single implementation. No vendor qualifies today, because every subclass overrides request, catalog, or billing behavior.
## Fuzz tests
Two seeded suites send malformed input through the real drivers and providers. They run with the rest of the backend tests.
| Suite | Covers |
| --- | --- |
| [interfaces/chat/ChatCompletionDriver.fuzz.test.ts](../src/backend/drivers/ai/interfaces/chat/ChatCompletionDriver.fuzz.test.ts) | Chat requests with a few values changed, and valid requests against wrong statuses, bodies of the wrong shape, and broken event streams. |
| [interfaces/mediaDrivers.fuzz.test.ts](../src/backend/drivers/ai/interfaces/mediaDrivers.fuzz.test.ts) | Image, video, speech and OCR requests with a few values changed. Every vendor refuses the request. |
What the suites assert:
- A malformed request never fails with an error that has no status, which `/drivers/call` would report as an unclassified 500.
- For chat, with the vendors answering, a malformed request also never fails with a 5xx and never marks a route unhealthy.
- When the upstream misbehaves, chat ends with an answer or an `HttpError`. A stream only has to end: the error line a stream carries isn't checked, so a malformed event can still end one with a 500.
- Requests reach the vendors, and with the vendors answering some chat requests are answered, so a suite that stops exercising the providers fails.
The vendors are played at the HTTP boundary by `upstream` in [shared/fuzzTestUtil.ts](../src/backend/drivers/ai/shared/fuzzTestUtil.ts), which stands in for global `fetch`, undici `fetch` and axios, and makes `https.request` throw so an SDK with its own client fails the suite. The vendors' SDKs run, except Groq's, which is swapped for the OpenAI SDK, and the AWS clients, whose `send` is stubbed. The media suite calls the drivers as a user with unlimited metering, so a request gets past file input and the credit hold to the vendor. A provider that loads its catalog from the vendor registers no models there, so the suites skip it.
A failure lists each distinct finding with its case number and input. Each case is generated from the seed and its own number, so the same case fails again on the next run. Routes are picked by rendezvous hashing: a model added to a catalog takes only the cases it wins, and every other case keeps its route. To search further, raise the case count:
```sh
PUTER_FUZZ_CASES=100000 npx vitest run --config src/backend/vitest.config.ts src/backend/drivers/ai/interfaces/chat/ChatCompletionDriver.fuzz.test.ts
```
When a suite finds a crash, validate the value where the request enters, in the driver or in `normalize_single_message`, so every provider is covered. Reject only what crashed: a value a provider ignored or refused before must still reach it. An option a driver reads as text, a number or a flag goes in the driver's `SCALAR_OPTIONS`, and `assertScalarOptions` from `shared/scalarOptions.ts` answers 400 for a value there that can't be converted to text. Add a unit test for the case next to the fix.
## Adding an interface
1. Create `interfaces/<interface>/` with the driver class and a `types.ts` for the contracts providers implement.
2. Register the driver in [src/backend/drivers/index.ts](../src/backend/drivers/index.ts).
3. Use `AI_RATE_LIMIT` and `AI_CONCURRENT` from `shared/aiLimits.ts`, and publish any new limit in [rate-limits-and-quotas.md](../src/docs/src/rate-limits-and-quotas.md).
4. Meter through `withAiCostFactor` from `src/backend/util/aiCostFactor.ts`, and route between providers with `ProviderRegistry` from `shared/providerRegistry.ts`.
5. List the options the providers read as text, a number or a flag in a `SCALAR_OPTIONS` array, and call `assertScalarOptions` from `shared/scalarOptions.ts` first in each method. Add the driver to [mediaDrivers.fuzz.test.ts](../src/backend/drivers/ai/interfaces/mediaDrivers.fuzz.test.ts).
6. Follow [contributing-apis.md](contributing-apis.md) for the puter.js method, types, docs, and tests.
-13
View File
@@ -42,19 +42,6 @@ block-beta
Extensions sit alongside this stack and can register into any layer; see [Extensions](#extensions).
### AI drivers
The AI drivers have their own layout under [src/backend/drivers/ai/](../src/backend/drivers/ai/):
| Folder | Holds |
| --- | --- |
| `interfaces/` | The Puter-facing drivers, one folder per AI interface (chat, image, video, text to speech, speech to text, speech to speech, OCR). |
| `providers/` | Vendor code, as `providers/<vendor>/<interface>/`. |
| `protocols/` | Wire protocols that several vendors speak, such as the OpenAI-compatible one. |
| `shared/` | Helpers that more than one interface uses. |
A call goes from an interface's driver to a provider, and from there to a shared protocol when the vendor speaks one. [ai-drivers.md](ai-drivers.md) has the ownership rules, the import rules, and the steps for adding a provider.
## Entry point: `PuterServer`
`PuterServer` is the bootstrap. It:
@@ -1,165 +0,0 @@
/*
* Copyright (C) 2024-present Puter Technologies Inc.
*
* This file is part of Puter.
*
* Puter is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { HttpError } from '../../core/http/HttpError.js';
import { PuterController } from '../types.js';
const ALLOWED_LAYOUTS = ['icons', 'details', 'list'];
const ALLOWED_SORT_BY = ['name', 'size', 'modified', 'type'];
const ALLOWED_SORT_ORDER = ['asc', 'desc'];
/**
* Folder view preference routes: layout and sort_by/sort_order, stored on the
* folder's fsentries row.
*/
/**
* Desktop preference writes — layout, sort order. Each persists on every call,
* and the GUI fires them on direct user action, so a per-minute ceiling well
* above human speed is enough to catch a stuck client.
*/
const PREFERENCE_WRITE_LIMIT = {
scope: 'desktop-preference',
limit: 120,
window: 60_000,
key: 'user',
};
export class DesktopController extends PuterController {
registerRoutes(router) {
// -- Folder layout -------------------------------------------
router.post(
'/set_layout',
{
subdomain: 'api',
requireUserActor: true,
allowFullAccessToken: true,
rateLimit: PREFERENCE_WRITE_LIMIT,
},
async (req, res) => {
const { item_uid, item_path, layout } = req.body ?? {};
if (!layout || !ALLOWED_LAYOUTS.includes(layout)) {
throw new HttpError(
400,
`\`layout\` must be one of: ${ALLOWED_LAYOUTS.join(', ')}`,
{ legacyCode: 'bad_request' },
);
}
await this.#updateFSEntry(
req.actor,
{ item_uid, item_path },
{ layout },
);
res.json({});
},
);
// -- Folder sort ---------------------------------------------
router.post(
'/set_sort_by',
{
subdomain: 'api',
requireUserActor: true,
allowFullAccessToken: true,
rateLimit: PREFERENCE_WRITE_LIMIT,
},
async (req, res) => {
const { item_uid, item_path, sort_by, sort_order } =
req.body ?? {};
if (!sort_by || !ALLOWED_SORT_BY.includes(sort_by)) {
throw new HttpError(
400,
`\`sort_by\` must be one of: ${ALLOWED_SORT_BY.join(', ')}`,
{ legacyCode: 'bad_request' },
);
}
const resolvedOrder = sort_order ?? 'asc';
if (!ALLOWED_SORT_ORDER.includes(resolvedOrder)) {
throw new HttpError(
400,
`\`sort_order\` must be one of: ${ALLOWED_SORT_ORDER.join(', ')}`,
{ legacyCode: 'bad_request' },
);
}
await this.#updateFSEntry(
req.actor,
{ item_uid, item_path },
{
sort_by,
sort_order: resolvedOrder,
},
);
res.json({});
},
);
}
// -- Helpers ------------------------------------------------------
/**
* Update columns on an actor-owned fsentry. Accepts either `item_uid` (fast
* path) or `item_path` (path lookups in FSEntryStore now have a
* recursive-CTE fallback for legacy rows with a NULL `path` column, so this
* works for old accounts too).
*
* Ownership: `entry.user_id === actor.user.id`. Kept as added validation
* against bad paths — the previous "drop user_id entirely" theory turned
* out to be wrong (the actual legacy issue was NULL paths, not user_id
* drift), so this filter doesn't lock out old accounts in practice.
*/
async #updateFSEntry(actor, { item_uid, item_path }, patch) {
if (!item_uid && !item_path) {
throw new HttpError(400, 'Missing `item_uid` or `item_path`', {
legacyCode: 'bad_request',
});
}
const entry = item_uid
? await this.stores.fsEntry.getEntryByUuid(item_uid)
: await this.stores.fsEntry.getEntryByPath(item_path);
if (!entry) {
throw new HttpError(404, 'Item not found', {
legacyCode: 'not_found',
});
}
const actorUserId = actor?.user?.id;
if (typeof actorUserId !== 'number' || entry.userId !== actorUserId) {
throw new HttpError(403, 'Not allowed to update this item', {
legacyCode: 'forbidden',
});
}
const keys = Object.keys(patch);
const setClause = keys.map((k) => `\`${k}\` = ?`).join(', ');
const values = keys.map((k) => patch[k]);
await this.clients.db.write(
`UPDATE \`fsentries\` SET ${setClause} WHERE \`id\` = ?`,
[...values, entry.id],
);
await this.stores.fsEntry.invalidateEntryCacheByUuid(entry.uuid);
}
onServerStart() {}
onServerPrepareShutdown() {}
onServerShutdown() {}
}
@@ -1,381 +0,0 @@
/*
* Copyright (C) 2024-present Puter Technologies Inc.
*
* This file is part of Puter.
*
* Puter is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import type { Request, RequestHandler, Response } from 'express';
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
import { v4 as uuidv4 } from 'uuid';
import type { Actor } from '../../core/actor.js';
import { PuterRouter } from '../../core/http/PuterRouter.js';
import { PuterServer } from '../../server.js';
import { setupTestServer } from '../../testUtil.js';
import { generateDefaultFsentries } from '../../util/userProvisioning.js';
// ── Test harness ────────────────────────────────────────────────────
//
// Boots one real PuterServer (in-memory sqlite + dynamo + s3 + mock
// redis) and re-registers DesktopController's inline lambda routes
// onto a fresh PuterRouter so each handler is reachable. Each test
// makes its own user via `makeUser` and exercises the live controller
// against the real wired stores (user, fsEntry) and DB client.
let server: PuterServer;
let router: PuterRouter;
beforeAll(async () => {
server = await setupTestServer();
router = new PuterRouter();
server.controllers.desktop.registerRoutes(router);
});
afterAll(async () => {
await server?.shutdown();
});
const makeUser = async (): Promise<{ actor: Actor; userId: number }> => {
const username = `dc-${Math.random().toString(36).slice(2, 10)}`;
const created = await server.stores.user.create({
username,
uuid: uuidv4(),
password: null,
email: `${username}@test.local`,
free_storage: 100 * 1024 * 1024,
requires_email_confirmation: false,
});
await generateDefaultFsentries(
server.clients.db,
server.stores.user,
created,
);
const refreshed = (await server.stores.user.getById(created.id))!;
return {
userId: refreshed.id,
actor: {
user: {
id: refreshed.id,
uuid: refreshed.uuid,
username: refreshed.username,
email: refreshed.email ?? null,
email_confirmed: true,
} as Actor['user'],
},
};
};
interface CapturedResponse {
statusCode: number;
body: unknown;
}
const makeReq = (init: {
body?: unknown;
actor?: Actor;
}): Request => {
return {
body: init.body ?? {},
query: {},
headers: {},
actor: init.actor,
} as unknown as Request;
};
const makeRes = () => {
const captured: CapturedResponse = { statusCode: 200, body: undefined };
const res = {
json: vi.fn((value: unknown) => {
captured.body = value;
return res;
}),
status: vi.fn((code: number) => {
captured.statusCode = code;
return res;
}),
setHeader: vi.fn(() => res),
};
return { res: res as unknown as Response, captured };
};
const findHandler = (method: string, path: string): RequestHandler => {
const route = router.routes.find(
(r) => r.method === method && r.path === path,
);
if (!route) throw new Error(`No ${method.toUpperCase()} ${path} route`);
return route.handler;
};
const callRoute = async (
method: string,
path: string,
req: Request,
res: Response,
) => {
const handler = findHandler(method, path);
await handler(req, res, () => {
throw new Error('handler called next() unexpectedly');
});
};
describe('DesktopController routes', () => {
it('has no desktop background route', () => {
expect(
router.routes.some((r) => r.path === '/set-desktop-bg'),
).toBe(false);
});
it('has no taskbar items route', () => {
expect(
router.routes.some((r) => r.path === '/update-taskbar-items'),
).toBe(false);
});
});
// ── /set_layout ─────────────────────────────────────────────────────
describe('DesktopController POST /set_layout', () => {
it('persists the new layout on the matching fsentry', async () => {
const { actor } = await makeUser();
const username = actor.user!.username!;
const documents = await server.stores.fsEntry.getEntryByPath(
`/${username}/Documents`,
);
expect(documents).not.toBeNull();
const { res, captured } = makeRes();
await callRoute(
'post',
'/set_layout',
makeReq({
body: { item_uid: documents!.uuid, layout: 'icons' },
actor,
}),
res,
);
expect(captured.body).toEqual({});
const refreshed = await server.stores.fsEntry.getEntryByUuid(
documents!.uuid,
);
expect(refreshed?.layout).toBe('icons');
});
it('resolves by item_path when item_uid is omitted', async () => {
const { actor } = await makeUser();
const username = actor.user!.username!;
const path = `/${username}/Pictures`;
const { res } = makeRes();
await callRoute(
'post',
'/set_layout',
makeReq({
body: { item_path: path, layout: 'list' },
actor,
}),
res,
);
const refreshed = await server.stores.fsEntry.getEntryByPath(path);
expect(refreshed?.layout).toBe('list');
});
it('throws 400 for an unknown layout value', async () => {
const { actor } = await makeUser();
const username = actor.user!.username!;
const documents = await server.stores.fsEntry.getEntryByPath(
`/${username}/Documents`,
);
const { res } = makeRes();
await expect(
callRoute(
'post',
'/set_layout',
makeReq({
body: { item_uid: documents!.uuid, layout: 'gallery' },
actor,
}),
res,
),
).rejects.toMatchObject({ statusCode: 400 });
});
it('throws 400 when neither item_uid nor item_path is supplied', async () => {
const { actor } = await makeUser();
const { res } = makeRes();
await expect(
callRoute(
'post',
'/set_layout',
makeReq({ body: { layout: 'icons' }, actor }),
res,
),
).rejects.toMatchObject({ statusCode: 400 });
});
it('throws 404 when the fsentry cannot be resolved', async () => {
const { actor } = await makeUser();
const { res } = makeRes();
await expect(
callRoute(
'post',
'/set_layout',
makeReq({
body: {
item_uid: '00000000-0000-0000-0000-000000000000',
layout: 'icons',
},
actor,
}),
res,
),
).rejects.toMatchObject({ statusCode: 404 });
});
it("throws 403 when the fsentry belongs to another user", async () => {
const owner = await makeUser();
const intruder = await makeUser();
const ownerUsername = owner.actor.user!.username!;
const ownerEntry = await server.stores.fsEntry.getEntryByPath(
`/${ownerUsername}/Documents`,
);
const { res } = makeRes();
await expect(
callRoute(
'post',
'/set_layout',
makeReq({
body: { item_uid: ownerEntry!.uuid, layout: 'icons' },
actor: intruder.actor,
}),
res,
),
).rejects.toMatchObject({ statusCode: 403 });
// Owner's entry remains untouched.
const refreshed = await server.stores.fsEntry.getEntryByUuid(
ownerEntry!.uuid,
);
expect(refreshed?.layout).toBeFalsy();
});
});
// ── /set_sort_by ────────────────────────────────────────────────────
describe('DesktopController POST /set_sort_by', () => {
it('persists sort_by + sort_order on the matching fsentry', async () => {
const { actor } = await makeUser();
const username = actor.user!.username!;
const documents = await server.stores.fsEntry.getEntryByPath(
`/${username}/Documents`,
);
const { res, captured } = makeRes();
await callRoute(
'post',
'/set_sort_by',
makeReq({
body: {
item_uid: documents!.uuid,
sort_by: 'name',
sort_order: 'desc',
},
actor,
}),
res,
);
expect(captured.body).toEqual({});
const refreshed = await server.stores.fsEntry.getEntryByUuid(
documents!.uuid,
);
expect(refreshed?.sortBy).toBe('name');
expect(refreshed?.sortOrder).toBe('desc');
});
it('defaults sort_order to "asc" when omitted', async () => {
const { actor } = await makeUser();
const username = actor.user!.username!;
const pictures = await server.stores.fsEntry.getEntryByPath(
`/${username}/Pictures`,
);
const { res } = makeRes();
await callRoute(
'post',
'/set_sort_by',
makeReq({
body: { item_uid: pictures!.uuid, sort_by: 'modified' },
actor,
}),
res,
);
const refreshed = await server.stores.fsEntry.getEntryByUuid(
pictures!.uuid,
);
expect(refreshed?.sortBy).toBe('modified');
expect(refreshed?.sortOrder).toBe('asc');
});
it('throws 400 for an unknown sort_by value', async () => {
const { actor } = await makeUser();
const username = actor.user!.username!;
const docs = await server.stores.fsEntry.getEntryByPath(
`/${username}/Documents`,
);
const { res } = makeRes();
await expect(
callRoute(
'post',
'/set_sort_by',
makeReq({
body: { item_uid: docs!.uuid, sort_by: 'random' },
actor,
}),
res,
),
).rejects.toMatchObject({ statusCode: 400 });
});
it('throws 400 for an invalid sort_order', async () => {
const { actor } = await makeUser();
const username = actor.user!.username!;
const docs = await server.stores.fsEntry.getEntryByPath(
`/${username}/Documents`,
);
const { res } = makeRes();
await expect(
callRoute(
'post',
'/set_sort_by',
makeReq({
body: {
item_uid: docs!.uuid,
sort_by: 'name',
sort_order: 'sideways',
},
actor,
}),
res,
),
).rejects.toMatchObject({ statusCode: 400 });
});
});
@@ -324,7 +324,7 @@ describe('DriverController — concurrent acquire/release', () => {
const driver = {
...makeSyntheticDriver(),
ping: async () => {
Context.get('keepConcurrencySlot')?.(
Context.get('keepDriverSlot')?.(
new Promise<void>((resolve) => {
settleWork = resolve;
}),
@@ -51,7 +51,7 @@ import { PermissionUtil } from '../../services/permission/permissionUtil.js';
import type { WithLifecycle } from '../../types';
import { withSpan } from '../../util/span.js';
import { PuterController } from '../types.js';
import { CallSlot } from '../../core/http/callSlot.js';
import { CallSlot } from './callSlot.js';
type DriverInstance = WithLifecycle & Record<string, unknown>;
@@ -446,7 +446,7 @@ export class DriverController extends PuterController {
});
}
slot.attach(handle, res);
Context.set('keepConcurrencySlot', (work) => slot.keep(work));
Context.set('keepDriverSlot', (work) => slot.keep(work));
}
// Stash the requested driver name in Context so multi-provider
@@ -18,7 +18,7 @@
*/
import type { Response } from 'express';
import { onResponseClose } from './onResponseClose.js';
import { onResponseClose } from '../../core/http/onResponseClose.js';
// Under the concurrency store's one-hour orphan TTL.
const SLOT_RENEW_MS = 10 * 60 * 1000;
@@ -29,9 +29,9 @@ interface SlotHandle {
}
/**
* A request's concurrency slot, held until the response is done and the work
* registered with `keep` has settled, including work that carries on after the
* caller hung up or was answered.
* A driver call's concurrency slot, held until the response is done and the
* call's work has settled, including work a driver carries on after the caller
* hung up or was answered.
*/
export class CallSlot {
#handle?: SlotHandle;
-2
View File
@@ -21,7 +21,6 @@ import { AppController } from './apps/AppController.js';
import { AppFeedbackController } from './feedback/AppFeedbackController.js';
import { AuthController } from './auth/AuthController.js';
import { BroadcastController } from './broadcast/BroadcastController.js';
import { DesktopController } from './desktop/DesktopController.js';
import { DriverController } from './drivers/DriverController.js';
import { EventsController } from './events/EventsController.js';
import { FSController } from './fs/FSController.js';
@@ -49,7 +48,6 @@ export const puterControllers = {
profile: ProfileController,
apps: AppController,
appFeedback: AppFeedbackController,
desktop: DesktopController,
hosting: HostingController,
system: SystemController,
fs: FSController,
@@ -44,7 +44,7 @@ import {
} from 'vitest';
import type { Actor } from '../../core/actor.js';
import type { ChatCompletionDriver } from '../../drivers/ai/interfaces/chat/ChatCompletionDriver.js';
import type { ChatCompletionDriver } from '../../drivers/ai-chat/ChatCompletionDriver.js';
import { PuterServer } from '../../server.js';
import { inRequestScope, setupTestServer } from '../../testUtil.js';
import { PuterAIController } from './PuterAIController.js';
@@ -49,8 +49,8 @@ import type { RouteOptions } from '../../core/http/index.js';
import {
COUNT_TOKENS,
type ChatCompletionDriver,
} from '../../drivers/ai/interfaces/chat/ChatCompletionDriver.js';
import { AI_CONCURRENT, AI_RATE_LIMIT } from '../../drivers/ai/shared/aiLimits.js';
} from '../../drivers/ai-chat/ChatCompletionDriver.js';
import { AI_CONCURRENT, AI_RATE_LIMIT } from '../../drivers/util/aiLimits.js';
import { PuterServer } from '../../server.js';
import { inRequestScope, setupTestServer } from '../../testUtil.js';
import { PuterAIController } from './PuterAIController.js';
@@ -30,28 +30,25 @@ import type { PuterRouter } from '../../core/http/PuterRouter.js';
import {
COUNT_TOKENS,
type ChatCompletionDriver,
} from '../../drivers/ai/interfaces/chat/ChatCompletionDriver.js';
} from '../../drivers/ai-chat/ChatCompletionDriver.js';
import type {
IChatCompleteResult,
IChatMessageResult,
IChatModel,
ICompleteArguments,
UsageDetails,
} from '../../drivers/ai/interfaces/chat/types.js';
} from '../../drivers/ai-chat/types.js';
import {
outputFormatFromResponseFormat,
rejectStatefulResponsesFields,
toolChoiceFromWire,
} from '../../drivers/ai/protocols/openaiCompatible/openaiParams.js';
} from '../../drivers/ai-chat/utils/openaiParams.js';
import {
promoteStopForToolCalls,
toFinishReason,
} from '../../drivers/ai/interfaces/chat/utils/stopReason.js';
} from '../../drivers/ai-chat/utils/stopReason.js';
import { isDriverStreamResult } from '../../drivers/meta.js';
import {
AI_CONCURRENT,
AI_RATE_LIMIT,
} from '../../drivers/ai/shared/aiLimits.js';
import { AI_CONCURRENT, AI_RATE_LIMIT } from '../../drivers/util/aiLimits.js';
import { PuterController } from '../types.js';
import { parseAnthropicRequest, toAnthropicMessage } from './anthropicWire.js';
import { pipeNdjsonStream } from './ndjsonStream.js';
@@ -1304,18 +1301,13 @@ export class PuterAIController extends PuterController {
return driver;
}
/**
* A completion whose caller may hang up. It runs on to its usage report
* either way, so it holds the route's concurrency slot until it settles.
*/
/** A completion that stops, and stops billing, when the caller hangs up. */
#complete(
res: Response,
args: ICompleteArguments,
): Promise<IChatCompleteResult> {
Context.set('abortSignal', abortOnDisconnect(res));
const completion = this.#driver().complete(args);
Context.get('keepConcurrencySlot')?.(completion);
return completion;
return this.#driver().complete(args);
}
}
@@ -18,7 +18,7 @@
*/
import { describe, expect, it } from 'vitest';
import type { IChatMessageResult } from '../../drivers/ai/interfaces/chat/types.js';
import type { IChatMessageResult } from '../../drivers/ai-chat/types.js';
import {
anthropicUsage,
parseAnthropicRequest,
@@ -32,7 +32,7 @@
import type { IncomingHttpHeaders } from 'node:http';
import { HttpError } from '../../core/http/HttpError.js';
import { sanitizeCacheControl } from '../../drivers/ai/providers/claude/chat/anthropicPolicy.js';
import { sanitizeCacheControl } from '../../drivers/ai-chat/providers/claude/anthropicPolicy.js';
import type {
ICompleteArguments,
IChatMessageResult,
@@ -42,9 +42,9 @@ import type {
ThinkingConfig,
ToolChoice,
UsageDetails,
} from '../../drivers/ai/interfaces/chat/types.js';
import { needsOpenAICoercion } from '../../drivers/ai/interfaces/chat/utils/normalizeToOpenAI.js';
import { fromFinishReason } from '../../drivers/ai/interfaces/chat/utils/stopReason.js';
} from '../../drivers/ai-chat/types.js';
import { needsOpenAICoercion } from '../../drivers/ai-chat/utils/normalizeToOpenAI.js';
import { fromFinishReason } from '../../drivers/ai-chat/utils/stopReason.js';
const toStringOrEmpty = (v: unknown): string =>
typeof v === 'string' ? v : '';
+4 -4
View File
@@ -20,15 +20,15 @@
/**
* SSE transport (`startSse`) plus the Anthropic `/v1/messages` streaming writer
* (`AnthropicSseWriter`), which turns the driver's NDJSON chunk protocol
* (`drivers/ai/interfaces/chat/utils/Streaming.js`) into the exact Anthropic
* event sequence: `message_start` → `content_block_start/delta/stop`* →
* (`drivers/ai-chat/utils/Streaming.js`) into the exact Anthropic event
* sequence: `message_start` → `content_block_start/delta/stop`* →
* `message_delta` → `message_stop`.
*/
import type { Response } from 'express';
import { onResponseClose } from '../../core/http/onResponseClose.js';
import type { UsageDetails } from '../../drivers/ai/interfaces/chat/types.js';
import { promoteStopForToolCalls } from '../../drivers/ai/interfaces/chat/utils/stopReason.js';
import type { UsageDetails } from '../../drivers/ai-chat/types.js';
import { promoteStopForToolCalls } from '../../drivers/ai-chat/utils/stopReason.js';
import { anthropicUsage } from './anthropicWire.js';
// -- SSE transport ------------------------------------------------------------
+5 -5
View File
@@ -69,11 +69,11 @@ export interface KnownContextFields {
*/
abortSignal: AbortSignal;
/**
* Keeps the request's concurrency slot until `work` settles, for work that
* outlives the response. Set by DriverController for a capped method and by
* the route concurrency gate.
* Keeps the call's concurrency slot until `work` settles (set by
* DriverController when the method is capped), for work that outlives the
* response.
*/
keepConcurrencySlot: (work: Promise<unknown>) => void;
keepDriverSlot: (work: Promise<unknown>) => void;
/**
* Aborts the chat driver's upstream request. A client disconnect doesn't
* fire it: the upstream bills a request it has accepted either way, so the
@@ -102,7 +102,7 @@ const KNOWN_KEY_FLAGS: Record<keyof KnownContextFields, true> = {
requestId: true,
driverName: true,
abortSignal: true,
keepConcurrencySlot: true,
keepDriverSlot: true,
upstreamSignal: true,
strictUpstreamErrors: true,
platformWrite: true,
+12 -12
View File
@@ -20,9 +20,8 @@
import crypto from 'node:crypto';
import { withSpan } from '../../../util/span.js';
import { Context } from '../../context.js';
import { CallSlot } from '../callSlot.js';
import { HttpError } from '../HttpError.js';
import { onResponseClose } from '../onResponseClose.js';
import { subscriptionOverride } from '../../../services/metering/consts.js';
/**
@@ -414,8 +413,8 @@ async function peekKv(kv, key, limit) {
//
// Lifecycle is the wedge between rate and concurrent limits: rate just
// records a tick, concurrent has to track "still in flight" → "done"
// across a request boundary. The route middleware and the driver controller
// both hold the slot in a `CallSlot`.
// across a request boundary. The route middleware hooks `res.finish` /
// `res.close`; the driver helper wraps the invocation in `try/finally`.
// Orphan TTL safety nets — used when the process dies between acquire
// and release. Memory backend doesn't need one (the process is also
@@ -1139,9 +1138,8 @@ async function resolveSubscriptionLimit(req, opts) {
* bySubscription: { user_free: 2, unlimited: 50 } } } { concurrent: { limit:
* 10, backend: 'redis', scope: 'expensive-op' } }
*
* On accept, holds the slot until the response is done and any work the handler
* registered through `keepConcurrencySlot` has settled, so work that outlives a
* caller who hung up still counts. On reject, 429 with the same
* On accept, schedules release on `res.finish` / `res.close` so even aborted
* requests give their slot back. On reject, 429 with the same
* `too_many_requests` legacyCode as the rate gate (clients already handle that
* branch). Fails open on backend error.
*/
@@ -1167,11 +1165,13 @@ export function concurrencyGate(opts) {
return;
}
const held = new CallSlot();
held.attach(guardSlot(slot, '[concurrent]'), res);
if (Context.current()) {
Context.set('keepConcurrencySlot', (work) => held.keep(work));
}
// `finish` (response sent) and `close` (connection closed, possibly
// aborted before finish) can both fire; the guard releases once.
const { release } = guardSlot(slot, '[concurrent]');
const onDone = () => void release();
res.once('finish', onDone);
onResponseClose(res, onDone);
if (res.destroyed || res.writableFinished) onDone();
next();
};
}
@@ -29,7 +29,6 @@ import {
vi,
} from 'vitest';
import { EventEmitter } from 'node:events';
import { Context, runWithContext } from '../../context.js';
import { isHttpError } from '../HttpError.js';
import { setupTestServer } from '../../../testUtil.ts';
import {
@@ -978,27 +977,6 @@ describe('concurrencyGate — memory backend', () => {
expect(isHttpError(third.err)).toBe(true);
});
it('holds the slot past the response for work the handler keeps', async () => {
const opts = { limit: 1, key: 'ip', scope: 'cg-kept-work' };
let finishWork;
const work = new Promise((resolve) => {
finishWork = resolve;
});
const res = makeRes();
await runWithContext({}, async () => {
await concurrencyGate(opts)(baseReq(), res, () => {
Context.get('keepConcurrencySlot')(work);
});
});
res.emit('close');
await new Promise((r) => setImmediate(r));
expect((await runGate(opts, baseReq())).err?.statusCode).toBe(429);
finishWork();
await new Promise((r) => setImmediate(r));
expect((await runGate(opts, baseReq())).err).toBeUndefined();
});
it('different keys do not share slots', async () => {
const opts = { limit: 1, key: 'ip', scope: 'cg-keyed' };
const a = await runGate(opts, baseReq({ ip: '1.1.1.1' }));
+4 -4
View File
@@ -263,10 +263,10 @@ export interface RouteOptions {
rateLimit?: RouteRateLimit | RouteRateLimit[];
/**
* Cap simultaneous in-flight requests per key; a slot is released once the
* response is done and any work the handler kept it for has settled.
* `bySubscription` overrides `limit` per `SubscriptionPolicy.id` when
* metering is wired in. `key`, `scope`, `backend` as in `rateLimit`.
* Cap simultaneous in-flight requests per key; slots are released on
* response finish or close. `bySubscription` overrides `limit` per
* `SubscriptionPolicy.id` when metering is wired in. `key`, `scope`,
* `backend` as in `rateLimit`.
*/
concurrent?: {
limit: number;
@@ -19,9 +19,9 @@
import { createServer, type Server } from 'node:http';
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
import { setupPuterTestEnv, type PuterTestEnv } from '../../../../testUtil.js';
import { setupPuterTestEnv, type PuterTestEnv } from '../../testUtil.js';
import { clearUnhealthyRoutes } from './utils/providerHealth.js';
import { makeActor } from '../../../../core/actor.js';
import { makeActor } from '../../core/actor.js';
const policyMessage =
'Policy Violation: this user has been blocked for a previous policy violation. Learn more: https://vendor.test/private';
@@ -129,17 +129,6 @@ describe('model availability through real HTTP', () => {
meteringEnforcement: { subscriptions: false },
} as never);
}, 120000);
it('serves qualified chat model names through HTTP', async () => {
const response = await fetch(
new URL('/puterai/chat/models', env.apiOrigin),
);
expect(response.status).toBe(200);
const { models } = (await response.json()) as { models: string[] };
expect(models).toContain('openrouter:vendor/availability-test');
expect(models.every((id) => /^[^:/]+:[^/]+\/.+/.test(id))).toBe(true);
expect(models).not.toContain('vendor/availability-test');
});
afterAll(async () => {
await env?.shutdown();
upstream?.closeAllConnections();
@@ -19,8 +19,8 @@
import { createServer, type Server, type ServerResponse } from 'node:http';
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
import { makeActor, type Actor } from '../../../../core/actor.js';
import { setupPuterTestEnv, type PuterTestEnv } from '../../../../testUtil.js';
import { makeActor, type Actor } from '../../core/actor.js';
import { setupPuterTestEnv, type PuterTestEnv } from '../../testUtil.js';
const routes = [
'/drivers/call',
@@ -42,22 +42,22 @@ import {
vi,
} from 'vitest';
import { Context } from '../../../../core/context.js';
import { HttpError } from '../../../../core/http/HttpError.js';
import { PuterServer } from '../../../../server.js';
import { setupTestServer } from '../../../../testUtil.js';
import { withTestActor } from '../../shared/integrationTestUtil.js';
import { Context } from '../../core/context.js';
import { HttpError } from '../../core/http/HttpError.js';
import { PuterServer } from '../../server.js';
import { setupTestServer } from '../../testUtil.js';
import { withTestActor } from '../integrationTestUtil.js';
import { ChatCompletionDriver } from './ChatCompletionDriver.js';
import { AzureChatProvider } from '../../providers/azure/chat/AzureChatProvider.js';
import { FakeChatProvider } from '../../providers/fake/chat/FakeChatProvider.js';
import { InfronProvider } from '../../providers/infron/chat/InfronProvider.js';
import { NeuralwattProvider } from '../../providers/neuralwatt/chat/NeuralwattProvider.js';
import { OpenAiChatProvider } from '../../providers/openai/chat/OpenAiChatCompletionsProvider.js';
import { OpenRouterProvider } from '../../providers/openrouter/chat/OpenRouterProvider.js';
import { TogetherAIProvider } from '../../providers/together/chat/TogetherAIProvider.js';
import { XAI_CHAT_MODELS } from '../../providers/xai/chat/catalog.js';
import { XAIProvider } from '../../providers/xai/chat/XAIProvider.js';
import { handle_completion_output } from '../../protocols/openaiCompatible/OpenAIUtil.js';
import { AzureChatProvider } from './providers/azure/AzureChatProvider.js';
import { FakeChatProvider } from './providers/FakeChatProvider.js';
import { InfronProvider } from './providers/infron/InfronProvider.js';
import { NeuralwattProvider } from './providers/neuralwatt/NeuralwattProvider.js';
import { OpenAiChatProvider } from './providers/openai/OpenAiChatCompletionsProvider.js';
import { OpenRouterProvider } from './providers/openrouter/OpenRouterProvider.js';
import { TogetherAIProvider } from './providers/together/TogetherAIProvider.js';
import { XAI_CHAT_MODELS } from './providers/xai/catalog.js';
import { XAIProvider } from './providers/xai/XAIProvider.js';
import { handle_completion_output } from './utils/OpenAIUtil.js';
import { clearUnhealthyRoutes, isRouteUnhealthy } from './utils/providerHealth.js';
let server: PuterServer;
@@ -1231,41 +1231,4 @@ describe('ChatCompletionDriver streaming failure handling', () => {
expect(events).toContainEqual({ type: 'text', text: 'hello' });
expect(cleanup).toHaveBeenCalledTimes(1);
});
it('frees the concurrency slot before the provider cleanup hook finishes', async () => {
let finishCleanup!: () => void;
const cleanup = vi.fn(
() =>
new Promise<void>((resolve) => {
finishCleanup = resolve;
}),
);
vi.spyOn(FakeChatProvider.prototype, 'complete').mockResolvedValue({
stream: true,
init_chat_stream: async ({
chatStream,
}: {
chatStream: { end: (usage?: Record<string, number>) => void };
}) => {
chatStream.end({ input_tokens: 1, output_tokens: 1 });
},
finally_fn: cleanup,
} as never);
const kept: Promise<unknown>[] = [];
const result = (await withTestActor(() => {
Context.set('keepConcurrencySlot', (work) => kept.push(work));
return fakeOnlyDriver.complete({
model: 'fake',
messages: [{ role: 'user', content: 'hi' }],
stream: true,
} as never);
})) as unknown as { stream: Readable };
await collect(result.stream);
expect(kept).toHaveLength(1);
await kept[0];
await vi.waitFor(() => expect(cleanup).toHaveBeenCalledTimes(1));
finishCleanup();
});
});
@@ -38,11 +38,11 @@ import {
vi,
} from 'vitest';
import { PuterServer } from '../../../../server.js';
import { setupTestServer } from '../../../../testUtil.js';
import { withTestActor } from '../../shared/integrationTestUtil.js';
import { PuterServer } from '../../server.js';
import { setupTestServer } from '../../testUtil.js';
import { withTestActor } from '../integrationTestUtil.js';
import { ChatCompletionDriver } from './ChatCompletionDriver.js';
import { FakeChatProvider } from '../../providers/fake/chat/FakeChatProvider.js';
import { FakeChatProvider } from './providers/FakeChatProvider.js';
import type { IChatModel, ICompleteArguments } from './types.js';
// The puter_path resolver reads the user's filesystem; stub it and assert on
@@ -51,7 +51,7 @@ const { processPuterPathUploadsMock } = vi.hoisted(() => ({
processPuterPathUploadsMock: vi.fn(async (_messages: unknown) => {}),
}));
vi.mock('../../providers/openai/chat/fileUpload.js', () => ({
vi.mock('./providers/openai/fileUpload.js', () => ({
processPuterPathUploads: processPuterPathUploadsMock,
MAX_FILE_SIZE: 5 * 1_000_000,
}));
@@ -38,14 +38,14 @@ import {
vi,
} from 'vitest';
import { Context } from '../../../../core/context.js';
import type { UsageInput } from '../../../../services/metering/types.js';
import { PuterServer } from '../../../../server.js';
import { setupTestServer } from '../../../../testUtil.js';
import { withTestActor } from '../../shared/integrationTestUtil.js';
import { Context } from '../../core/context.js';
import type { UsageInput } from '../../services/metering/types.js';
import { PuterServer } from '../../server.js';
import { setupTestServer } from '../../testUtil.js';
import { withTestActor } from '../integrationTestUtil.js';
import { ChatCompletionDriver } from './ChatCompletionDriver.js';
import { clearUnhealthyRoutes, isRouteUnhealthy } from './utils/providerHealth.js';
import { FakeChatProvider } from '../../providers/fake/chat/FakeChatProvider.js';
import { FakeChatProvider } from './providers/FakeChatProvider.js';
import type { IChatCompleteResult } from './types.js';
import { SDK_TIMEOUT_MS } from './utils/sdkClient.js';
import type { AIChatStream } from './utils/Streaming.js';
@@ -44,21 +44,21 @@ import type { Request } from 'express';
import {
renderAnthropicError,
renderOpenAIError,
} from '../../../../controllers/puterai/wireErrors.js';
import { Context } from '../../../../core/context.js';
import { HttpError } from '../../../../core/http/HttpError.js';
import { PuterServer } from '../../../../server.js';
import { setupTestServer } from '../../../../testUtil.js';
import { withTestActor } from '../../shared/integrationTestUtil.js';
} from '../../controllers/puterai/wireErrors.js';
import { Context } from '../../core/context.js';
import { HttpError } from '../../core/http/HttpError.js';
import { PuterServer } from '../../server.js';
import { setupTestServer } from '../../testUtil.js';
import { withTestActor } from '../integrationTestUtil.js';
import { ChatCompletionDriver } from './ChatCompletionDriver.js';
import { OPEN_AI_MODELS } from '../../providers/openai/chat/models.js';
import { OPEN_AI_MODELS } from './providers/openai/models.js';
import {
clearUnhealthyRoutes,
isRouteUnhealthy,
markRouteUnhealthy,
} from './utils/providerHealth.js';
import { InfronProvider } from '../../providers/infron/chat/InfronProvider.js';
import { UpstreamDeadlineError } from '../../../util/upstreamErrors.js';
import { InfronProvider } from './providers/infron/InfronProvider.js';
import { UpstreamDeadlineError } from '../util/upstreamErrors.js';
// -- OpenAI SDK mock ------------------------------------------------
// Gemini reaches Google through `new openai.OpenAI()` (default export) and
@@ -44,17 +44,17 @@ import {
vi,
} from 'vitest';
import { Context } from '../../../../core/context.js';
import { HttpError } from '../../../../core/http/HttpError.js';
import { PuterServer } from '../../../../server.js';
import { setupTestServer } from '../../../../testUtil.js';
import { withTestActor } from '../../shared/integrationTestUtil.js';
import { Context } from '../../core/context.js';
import { HttpError } from '../../core/http/HttpError.js';
import { PuterServer } from '../../server.js';
import { setupTestServer } from '../../testUtil.js';
import { withTestActor } from '../integrationTestUtil.js';
import { COUNT_TOKENS, ChatCompletionDriver } from './ChatCompletionDriver.js';
import { ClaudeProvider } from '../../providers/claude/chat/ClaudeProvider.js';
import { FakeChatProvider } from '../../providers/fake/chat/FakeChatProvider.js';
import { OllamaChatProvider } from '../../providers/ollama/chat/OllamaProvider.js';
import { XAI_CHAT_MODELS } from '../../providers/xai/chat/catalog.js';
import { XAIProvider } from '../../providers/xai/chat/XAIProvider.js';
import { ClaudeProvider } from './providers/claude/ClaudeProvider.js';
import { FakeChatProvider } from './providers/FakeChatProvider.js';
import { OllamaChatProvider } from './providers/ollama/OllamaProvider.js';
import { XAI_CHAT_MODELS } from './providers/xai/catalog.js';
import { XAIProvider } from './providers/xai/XAIProvider.js';
import type {
IChatCompleteResult,
IChatProvider,
@@ -207,50 +207,6 @@ describe('ChatCompletionDriver model map', () => {
d.onServerShutdown();
});
it('lists qualified model ids without publishing aliases as models', async () => {
const ids = [
'kimi/kimi-3',
'ollama:plain-model',
'ollama:vendor/qualified',
'vendor/tagged:free',
];
vi.spyOn(OllamaChatProvider.prototype, 'models').mockResolvedValue(
ids.map((id) => ({
id,
aliases: ['legacy-' + id],
costs_currency: 'usd-cents',
costs: {},
max_tokens: 8192,
})),
);
const d = newDriver({ ollama: {} });
await d.onServerStart();
try {
expect(
(await d.list()).filter(
(id) => !['abuse', 'costly', 'fake'].includes(id),
),
).toEqual([
'ollama:kimi/kimi-3',
'ollama:ollama/plain-model',
'ollama:vendor/qualified',
'ollama:vendor/tagged:free',
]);
const models = (await d.models()).filter(
(model) => model.provider === 'ollama',
);
expect(models.map((model) => model.id).sort()).toEqual(
[...ids].sort(),
);
for (const model of models) {
expect(model.aliases).toContain(model.puterId);
expect(model.aliases).toContain('legacy-' + model.id);
}
} finally {
d.onServerShutdown();
}
});
it('lists the catalog to a caller that asks while it is still loading', async () => {
const d = newDriver({ ollama: { enabled: false } });
d.onServerStart();
@@ -2440,97 +2396,3 @@ describe('ChatCompletionDriver service speed', () => {
},
);
});
describe('ChatCompletionDriver.complete option shapes', () => {
const hi = [{ role: 'user', content: 'hi' }];
it.each(['model', 'provider', 'service_tier'])(
'rejects a value that cannot be text as `%s`',
async (name) => {
const args = { model: 'fake', messages: hi, [name]: { toString: null } };
await expect(
withTestActor(() => driver.complete(args as never)),
).rejects.toMatchObject({
statusCode: 400,
legacyCode: 'bad_request',
message: `\`${name}\` is not a usable value`,
});
await expect(
withTestActor(() => driver[COUNT_TOKENS](args as never)),
).rejects.toMatchObject({ statusCode: 400 });
},
);
it('routes by the model when `provider` is an object, as it did before', async () => {
const completeSpy = vi.spyOn(FakeChatProvider.prototype, 'complete');
await withTestActor(() =>
driver.complete({
model: 'fake',
messages: hi,
provider: { order: ['Anthropic'] },
} as never),
);
expect(completeSpy).toHaveBeenCalledTimes(1);
});
it.each([false, 0, '', null])(
'treats `tools: %j` as no tools when counting tokens',
async (tools) => {
const countTokens = vi.fn(async (_args: object) => 1);
const fake = FakeChatProvider.prototype as {
countTokens?: typeof countTokens;
};
fake.countTokens = countTokens;
try {
await withTestActor(() =>
driver[COUNT_TOKENS]({
model: 'fake',
messages: hi,
tools,
} as never),
);
} finally {
delete fake.countTokens;
}
expect(countTokens.mock.calls[0]![0]).not.toHaveProperty('tools');
},
);
it.each([false, 0, '', null])(
'treats `tools: %j` as no tools',
async (tools) => {
const completeSpy = vi.spyOn(FakeChatProvider.prototype, 'complete');
await withTestActor(() =>
driver.complete({ model: 'fake', messages: hi, tools } as never),
);
expect(completeSpy.mock.calls[0]![0]).not.toHaveProperty('tools');
},
);
it('reports zero tokens in the cost event for a usage count that is not a number', async () => {
vi.spyOn(FakeChatProvider.prototype, 'models').mockResolvedValueOnce([
{
id: 'priced',
aliases: [],
costs_currency: 'usd-cents',
costs: { input_tokens: 1000, output_tokens: 2000 },
max_tokens: 8192,
},
]);
const d = await makeDriver();
const costEvents = captureEvent('ai.prompt.cost-calculated');
vi.spyOn(FakeChatProvider.prototype, 'complete').mockResolvedValueOnce({
message: { role: 'assistant', content: [{ type: 'text', text: 'ok' }] },
usage: { input_tokens: 10, output_tokens: { toString: null } },
finish_reason: 'stop',
} as never);
await withTestActor(() => d.complete({ model: 'priced', messages: hi }));
expect(costEvents[0]![1]).toMatchObject({
input_tokens: 10,
output_tokens: 0,
total_ucents: 10 * 1000,
});
});
});
@@ -19,56 +19,56 @@
import crypto from 'node:crypto';
import { PassThrough } from 'node:stream';
import { EventMap } from '../../../../clients/event/types.js';
import type { Actor } from '../../../../core/actor.js';
import { Context } from '../../../../core/context.js';
import { HttpError, isHttpError } from '../../../../core/http/HttpError.js';
import { insufficientCreditsError } from '../../../../services/metering/enforcement.js';
import { isFreeSubscription } from '../../../../services/metering/consts.js';
import type { MeteringService } from '../../../../services/metering/MeteringService.js';
import type { CreditHold } from '../../../../services/metering/types.js';
import { NO_CREDIT_HOLD } from '../../../../services/metering/types.js';
import type { DriverStreamResult } from '../../../meta.js';
import { PuterDriver } from '../../../types.js';
import { EventMap } from '../../clients/event/types.js';
import type { Actor } from '../../core/actor.js';
import { Context } from '../../core/context.js';
import { HttpError, isHttpError } from '../../core/http/HttpError.js';
import { insufficientCreditsError } from '../../services/metering/enforcement.js';
import { isFreeSubscription } from '../../services/metering/consts.js';
import type { MeteringService } from '../../services/metering/MeteringService.js';
import type { CreditHold } from '../../services/metering/types.js';
import { NO_CREDIT_HOLD } from '../../services/metering/types.js';
import type { DriverStreamResult } from '../meta.js';
import { PuterDriver } from '../types.js';
import {
type AiMeteringService,
factoredTotal,
requestAiCosts,
withAiCostFactor,
} from '../../../../util/aiCostFactor.js';
import { AI_CONCURRENT, AI_RATE_LIMIT } from '../../shared/aiLimits.js';
} from '../util/aiCostFactor.js';
import { AI_CONCURRENT, AI_RATE_LIMIT } from '../util/aiLimits.js';
import {
isCreditExhaustion as isUpstreamCreditExhaustion,
isUpstreamConnectionError,
isUpstreamTimeoutError,
sanitizeUpstreamMessage,
UpstreamDeadlineError,
} from '../../../util/upstreamErrors.js';
import { AlibabaProvider } from '../../providers/alibaba/chat/AlibabaProvider.js';
import { AzureChatProvider } from '../../providers/azure/chat/AzureChatProvider.js';
import { AzureResponsesProvider } from '../../providers/azure/chat/AzureResponsesProvider.js';
import { BytePlusProvider } from '../../providers/byteplus/chat/BytePlusProvider.js';
import { ClaudeProvider } from '../../providers/claude/chat/ClaudeProvider.js';
import { DeepSeekProvider } from '../../providers/deepseek/chat/DeepSeekProvider.js';
import { FakeChatProvider } from '../../providers/fake/chat/FakeChatProvider.js';
import { GeminiChatProvider } from '../../providers/gemini/chat/GeminiChatProvider.js';
import { GroqAIProvider } from '../../providers/groq/chat/GroqAIProvider.js';
import { HoonifyProvider } from '../../providers/hoonify/chat/HoonifyProvider.js';
import { InfronProvider } from '../../providers/infron/chat/InfronProvider.js';
import { MetaProvider } from '../../providers/meta/chat/MetaProvider.js';
import { MiniMaxProvider } from '../../providers/minimax/chat/MiniMaxProvider.js';
import { MistralAIProvider } from '../../providers/mistral/chat/MistralAiProvider.js';
import { MoonshotProvider } from '../../providers/moonshot/chat/MoonshotProvider.js';
import { NeuralwattProvider } from '../../providers/neuralwatt/chat/NeuralwattProvider.js';
import { OllamaChatProvider } from '../../providers/ollama/chat/OllamaProvider.js';
import type { ChatProviderConfig } from '../../protocols/openaiCompatible/OpenAICompatProvider.js';
import { processPuterPathUploads } from '../../providers/openai/chat/fileUpload.js';
import { OpenAiChatProvider } from '../../providers/openai/chat/OpenAiChatCompletionsProvider.js';
import { OpenAiResponsesChatProvider } from '../../providers/openai/chat/OpenAiChatResponsesProvider.js';
import { OpenRouterProvider } from '../../providers/openrouter/chat/OpenRouterProvider.js';
import { TogetherAIProvider } from '../../providers/together/chat/TogetherAIProvider.js';
import { XAIProvider } from '../../providers/xai/chat/XAIProvider.js';
import { ZAIProvider } from '../../providers/zai/chat/ZAIProvider.js';
} from '../util/upstreamErrors.js';
import { AlibabaProvider } from './providers/alibaba/AlibabaProvider.js';
import { AzureChatProvider } from './providers/azure/AzureChatProvider.js';
import { AzureResponsesProvider } from './providers/azure/AzureResponsesProvider.js';
import { BytePlusProvider } from './providers/byteplus/BytePlusProvider.js';
import { ClaudeProvider } from './providers/claude/ClaudeProvider.js';
import { DeepSeekProvider } from './providers/deepseek/DeepSeekProvider.js';
import { FakeChatProvider } from './providers/FakeChatProvider.js';
import { GeminiChatProvider } from './providers/gemini/GeminiChatProvider.js';
import { GroqAIProvider } from './providers/groq/GroqAIProvider.js';
import { HoonifyProvider } from './providers/hoonify/HoonifyProvider.js';
import { InfronProvider } from './providers/infron/InfronProvider.js';
import { MetaProvider } from './providers/meta/MetaProvider.js';
import { MiniMaxProvider } from './providers/minimax/MiniMaxProvider.js';
import { MistralAIProvider } from './providers/mistral/MistralAiProvider.js';
import { MoonshotProvider } from './providers/moonshot/MoonshotProvider.js';
import { NeuralwattProvider } from './providers/neuralwatt/NeuralwattProvider.js';
import { OllamaChatProvider } from './providers/ollama/OllamaProvider.js';
import type { ChatProviderConfig } from './providers/OpenAICompatProvider.js';
import { processPuterPathUploads } from './providers/openai/fileUpload.js';
import { OpenAiChatProvider } from './providers/openai/OpenAiChatCompletionsProvider.js';
import { OpenAiResponsesChatProvider } from './providers/openai/OpenAiChatResponsesProvider.js';
import { OpenRouterProvider } from './providers/openrouter/OpenRouterProvider.js';
import { TogetherAIProvider } from './providers/together/TogetherAIProvider.js';
import { XAIProvider } from './providers/xai/XAIProvider.js';
import { ZAIProvider } from './providers/zai/ZAIProvider.js';
import type {
IChatCompleteResult,
IChatMessageResult,
@@ -103,13 +103,9 @@ import {
normalizeResultToOpenAI,
shouldPresentAsOpenAI,
} from './utils/normalizeToOpenAI.js';
import { isToolChoice, toolChoiceFromWire } from './utils/openaiParams.js';
import {
isToolChoice,
toolChoiceFromWire,
} from '../../protocols/openaiCompatible/openaiParams.js';
import { assertScalarOptions } from '../../shared/scalarOptions.js';
import { catalogPrice } from '../../shared/catalogPrice.js';
import {
catalogPrice,
costKeys,
costsForRequest,
isFreeModel,
@@ -125,7 +121,7 @@ import {
TRANSIENT_UNHEALTHY_TTL_SEC,
UNHEALTHY_TTL_SEC,
} from './utils/providerHealth.js';
import { CATALOG_TTL_MS } from '../../shared/cachedRemoteCatalog.js';
import { CATALOG_TTL_MS } from './utils/cachedRemoteCatalog.js';
import { SDK_TIMEOUT_MS } from './utils/sdkClient.js';
import { fromFinishReason } from './utils/stopReason.js';
import { AIChatStream } from './utils/Streaming.js';
@@ -182,9 +178,6 @@ export const COUNT_TOKENS = Symbol('aiChat.countTokens');
*/
const HOLD_RENEW_INTERVAL_MS = 5 * 60 * 1000;
// Options that reach error messages and request headers as text.
const SCALAR_OPTIONS = ['model', 'provider', 'service_tier'];
/**
* A moderation refusal is a completion that was produced and charged, then
* withheld — not a route failure. Retrying it on a fallback provider would bill
@@ -592,7 +585,7 @@ export class ChatCompletionDriver extends PuterDriver {
readonly driverName = 'ai-chat';
readonly isDefault = true;
// Shared AI policy — see `drivers/ai/shared/aiLimits.ts` for the tier table.
// Shared AI policy — see `drivers/util/aiLimits.ts` for the tier table.
readonly rateLimit = AI_RATE_LIMIT;
readonly concurrent = AI_CONCURRENT;
@@ -697,7 +690,6 @@ export class ChatCompletionDriver extends PuterDriver {
{ legacyCode: 'invalid_service_speed' },
);
}
assertScalarOptions(args, SCALAR_OPTIONS);
const abortSignal = Context.get('abortSignal');
abortSignal?.throwIfAborted();
const actor = Context.get('actor');
@@ -752,9 +744,6 @@ export class ChatCompletionDriver extends PuterDriver {
args.messages = normalizeMediaParts(normalize_messages(args.messages));
if (args.tools) {
normalize_tools_object(args.tools);
} else {
// `tools: enabled && list` leaves `false` here; it means no tools.
delete args.tools;
}
// A raw `/drivers/call` caller still sends the OpenAI wire form
// ('auto'/'none'/'required', {type:'function', function:{name}}) —
@@ -1308,17 +1297,8 @@ export class ChatCompletionDriver extends PuterDriver {
}, HOLD_RENEW_INTERVAL_MS);
renewHold.unref?.();
// The upstream runs on after a caller hangs up, so the request
// keeps its concurrency slot until the upstream is done, not until
// the cleanup after it.
let upstreamDone!: () => void;
const upstreamRunning = new Promise<void>((resolve) => {
upstreamDone = resolve;
});
Context.get('keepConcurrencySlot')?.(upstreamRunning);
// The stream writes happen async while the response is piped to
// the client.
// Fire-and-forget — the stream writes happen async while the
// response is being piped to the client.
(async () => {
try {
await init({ chatStream });
@@ -1343,7 +1323,6 @@ export class ChatCompletionDriver extends PuterDriver {
}
passthrough.end();
} finally {
upstreamDone();
clearInterval(renewHold);
clearTimeout(detachedLimit);
abortSignal?.removeEventListener('abort', detachStream);
@@ -1511,7 +1490,6 @@ export class ChatCompletionDriver extends PuterDriver {
async [COUNT_TOKENS](
args: ICompleteArguments,
): Promise<{ input_tokens: number }> {
assertScalarOptions(args, SCALAR_OPTIONS);
let intendedProvider = args.provider || '';
if (!args.model && !intendedProvider) {
intendedProvider = 'azure-openai';
@@ -1537,9 +1515,6 @@ export class ChatCompletionDriver extends PuterDriver {
args.messages = normalizeMediaParts(normalize_messages(args.messages));
if (args.tools) {
normalize_tools_object(args.tools);
} else {
// `tools: enabled && list` leaves `false` here; it means no tools.
delete args.tools;
}
const provider = this.#providers[model.provider!];
@@ -1873,20 +1848,20 @@ export class ChatCompletionDriver extends PuterDriver {
const cost = usageCost(usage, model, usageCosts, pricingTime);
const { inputKey, outputKey } = costKeys(model);
// The vendor reports usage, and pricing skips a count that is not a
// number; so does this.
const count = (value: unknown) =>
typeof value === 'number' && Number.isFinite(value) ? value : 0;
const inputTokens = cost
? count(
usage[inputKey] ?? usage.prompt_tokens ?? usage.input_tokens,
? Number(
usage[inputKey] ??
usage.prompt_tokens ??
usage.input_tokens ??
0,
)
: 0;
const outputTokens = cost
? count(
? Number(
usage[outputKey] ??
usage.completion_tokens ??
usage.output_tokens,
usage.output_tokens ??
0,
)
: 0;
const inputMicroCents = cost?.inputMicroCents ?? 0;
@@ -2086,39 +2061,14 @@ export class ChatCompletionDriver extends PuterDriver {
// instance, so they are read and never written: normalizing
// the id or appending puterId in place would accumulate across
// instantiations. The bucket gets its own copy instead.
// Keep upstream ids for provider calls and publish a qualified
// routing name even when a catalog omits its vendor namespace.
const publicId = entry.puterId || entry.id;
// A colon after the first slash belongs to the model name, as
// in `vendor/model:free`, and names no service.
const serviceSeparator = publicId
.split('/', 1)[0]!
.indexOf(':');
const service =
serviceSeparator < 0
? providerName
: publicId.slice(0, serviceSeparator);
const modelPath =
serviceSeparator < 0
? publicId
: publicId.slice(serviceSeparator + 1);
const puterId =
providerName === 'fake-chat'
? entry.puterId
: `${service}:${modelPath.includes('/') ? modelPath : `${service}/${modelPath}`}`;
const aliases = puterId
? [
...new Set([
...(entry.aliases ?? []),
...(entry.puterId ? [entry.puterId] : []),
puterId,
]),
]
: entry.aliases;
const aliases =
entry.puterId &&
!(entry.aliases ?? []).includes(entry.puterId)
? [...(entry.aliases ?? []), entry.puterId]
: entry.aliases;
const model = {
...entry,
id: normalizeModelKey(entry.id),
...(puterId ? { puterId } : {}),
};
// Assigned only when the entry has names to carry: models()
// is serialized to the API, and an entry that declared no
@@ -19,9 +19,9 @@
import { PassThrough } from 'node:stream';
import { describe, expect, it } from 'vitest';
import { Context } from '../../../../../core/context.js';
import { withTestActor } from '../../../shared/integrationTestUtil.js';
import { AIChatStream } from '../../../interfaces/chat/utils/Streaming.js';
import { Context } from '../../../core/context.js';
import { withTestActor } from '../../integrationTestUtil.js';
import { AIChatStream } from '../utils/Streaming.js';
import { FakeChatProvider } from './FakeChatProvider.js';
describe('fake chat streaming lifecycle', () => {
@@ -19,15 +19,15 @@
import dedent from 'dedent';
import { setTimeout as delay } from 'node:timers/promises';
import { Context } from '../../../../../core/context.js';
import { Context } from '../../../core/context.js';
import { LoremIpsum } from 'lorem-ipsum';
import { AIChatStream } from '../../../interfaces/chat/utils/Streaming.js';
import { AIChatStream } from '../utils/Streaming.js';
import {
IChatModel,
IChatProvider,
ICompleteArguments,
PuterMessage,
} from '../../../interfaces/chat/types.js';
} from '../types.js';
export class FakeChatProvider implements IChatProvider {
checkModeration(_text: string) {
@@ -18,12 +18,12 @@
*/
import { describe, expect, it } from 'vitest';
import type { MeteringService } from '../../../../services/metering/MeteringService.js';
import type { MeteringService } from '../../../services/metering/MeteringService.js';
import type {
IChatMessageResult,
IChatModel,
ICompleteArguments,
} from '../../interfaces/chat/types.js';
} from '../types.js';
import {
type OpenAICompatOptions,
OpenAICompatProvider,
@@ -18,10 +18,10 @@
*/
import type { CompletionUsage } from 'openai/resources/completions.mjs';
import type { Actor } from '../../../../core/actor.js';
import { Context } from '../../../../core/context.js';
import { HttpError } from '../../../../core/http/HttpError.js';
import type { MeteringService } from '../../../../services/metering/MeteringService.js';
import type { Actor } from '../../../core/actor.js';
import { Context } from '../../../core/context.js';
import { HttpError } from '../../../core/http/HttpError.js';
import type { MeteringService } from '../../../services/metering/MeteringService.js';
import type {
IChatCompleteResult,
IChatModel,
@@ -29,27 +29,26 @@ import type {
ICompleteArguments,
PuterMessage,
UsageDetails,
} from '../../interfaces/chat/types.js';
} from '../types.js';
import {
contextLengthRetryParams,
isContextLengthError,
} from './contextLimit.js';
import { inlineHttpImageUrls } from './inlineImages.js';
import { modelSupportsVision } from '../../interfaces/chat/utils/mediaParts.js';
} from '../utils/contextLimit.js';
import { make_openai_tools } from '../utils/FunctionCalling.js';
import { inlineHttpImageUrls } from '../utils/inlineImages.js';
import { modelSupportsVision } from '../utils/mediaParts.js';
import {
meterChatUsage,
type MeterChatUsageOptions,
} from '../../interfaces/chat/utils/meterChatUsage.js';
import { servedServiceSpeed } from '../../interfaces/chat/utils/serviceSpeed.js';
import { costKeys } from '../../interfaces/chat/utils/pricing.js';
import { catalogPrice } from '../../shared/catalogPrice.js';
import { normalizeModelKey } from '../../interfaces/chat/utils/modelRouting.js';
import * as OpenAIUtil from './OpenAIUtil.js';
} from '../utils/meterChatUsage.js';
import { servedServiceSpeed } from '../utils/serviceSpeed.js';
import { catalogPrice, costKeys } from '../utils/pricing.js';
import { normalizeModelKey } from '../utils/modelRouting.js';
import * as OpenAIUtil from '../utils/OpenAIUtil.js';
import {
openAICompatParams,
type OpenAIDialect,
toolsToWire,
} from './openaiParams.js';
} from '../utils/openaiParams.js';
/** What the driver hands every keyed provider. */
export interface ChatProviderConfig {
@@ -201,7 +200,9 @@ export class OpenAICompatProvider implements IChatProvider {
model,
);
const tools = args.tools ? toolsToWire(args.tools, 'chat') : undefined;
const tools = args.tools
? make_openai_tools(args.tools, { dialect: 'chat' })
: undefined;
const maxTokens = args.max_tokens ?? options.defaultMaxTokens;
const streamUsage = options.streamUsage ?? true;
const shared: Record<string, unknown> = {
@@ -28,9 +28,10 @@ import { describe, expect, it } from 'vitest';
import {
INTEGRATION_TEST_TIMEOUT_MS,
makeMeteringStub,
optionalEnv,
skipUnlessEnv,
withTestActor,
} from '../../../shared/integrationTestUtil.js';
import { optionalEnv, skipUnlessEnv } from '../../../../../testEnv.js';
} from '../../../integrationTestUtil.js';
import { AlibabaProvider } from './AlibabaProvider.js';
const ENV_VAR = 'PUTER_TEST_AI_ALIBABA_API_KEY';
@@ -41,19 +41,19 @@ import {
type MockInstance,
} from 'vitest';
import { SYSTEM_ACTOR } from '../../../../../core/actor.js';
import type { MeteringService } from '../../../../../services/metering/MeteringService.js';
import { PuterServer } from '../../../../../server.js';
import { setupTestServer } from '../../../../../testUtil.js';
import { withTestActor } from '../../../shared/integrationTestUtil.js';
import { AIChatStream } from '../../../interfaces/chat/utils/Streaming.js';
import { SYSTEM_ACTOR } from '../../../../core/actor.js';
import type { MeteringService } from '../../../../services/metering/MeteringService.js';
import { PuterServer } from '../../../../server.js';
import { setupTestServer } from '../../../../testUtil.js';
import { withTestActor } from '../../../integrationTestUtil.js';
import { AIChatStream } from '../../utils/Streaming.js';
import { mapAlibabaCatalogModel, type AlibabaApiModel } from './catalog.js';
import page1 from './fixtures/page1.test.json';
import page2 from './fixtures/page2.test.json';
import { CATALOG_TTL_MS } from '../../../shared/cachedRemoteCatalog.js';
import { CATALOG_TTL_MS } from '../../utils/cachedRemoteCatalog.js';
import { ALIBABA_MODELS } from './models.js';
import { AlibabaProvider } from './AlibabaProvider.js';
import { modelLookupNames } from '../../../interfaces/chat/utils/modelRouting.js';
import { modelLookupNames } from '../../utils/modelRouting.js';
// ── OpenAI SDK mock ─────────────────────────────────────────────────
@@ -18,15 +18,15 @@
*/
import { OpenAI } from 'openai';
import type { MeteringService } from '../../../../../services/metering/MeteringService.js';
import type { IChatModel } from '../../../interfaces/chat/types.js';
import { cachedRemoteCatalog } from '../../../shared/cachedRemoteCatalog.js';
import type { MeteringService } from '../../../../services/metering/MeteringService.js';
import type { IChatModel } from '../../types.js';
import { cachedRemoteCatalog } from '../../utils/cachedRemoteCatalog.js';
import { fetchAlibabaCatalog } from './catalog.js';
import { sdkClientOptions } from '../../../interfaces/chat/utils/sdkClient.js';
import { sdkClientOptions } from '../../utils/sdkClient.js';
import {
type ChatProviderConfig,
OpenAICompatProvider,
} from '../../../protocols/openaiCompatible/OpenAICompatProvider.js';
} from '../OpenAICompatProvider.js';
import { ALIBABA_MODELS } from './models.js';
export class AlibabaProvider extends OpenAICompatProvider {
@@ -18,8 +18,8 @@
*/
import axios from 'axios';
import type { IChatModel } from '../../../interfaces/chat/types.js';
import { catalogPrice } from '../../../shared/catalogPrice.js';
import type { IChatModel } from '../../types.js';
import { catalogPrice } from '../../utils/pricing.js';
import { ALIBABA_MODELS } from './models.js';
export interface AlibabaApiModel {
@@ -17,7 +17,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import type { IChatModel } from '../../../interfaces/chat/types.js';
import type { IChatModel } from '../../types.js';
// Hardcoded from https://models.dev/api.json
export const ALIBABA_MODELS: IChatModel[] = [
@@ -36,9 +36,10 @@ import { describe, expect, it } from 'vitest';
import {
INTEGRATION_TEST_TIMEOUT_MS,
makeMeteringStub,
optionalEnv,
skipUnlessEnv,
withTestActor,
} from '../../../shared/integrationTestUtil.js';
import { optionalEnv, skipUnlessEnv } from '../../../../../testEnv.js';
} from '../../../integrationTestUtil.js';
import { AzureChatProvider } from './AzureChatProvider.js';
const KEY_ENV = 'PUTER_TEST_AI_AZURE_OPENAI_API_KEY';
@@ -46,17 +46,17 @@ import {
type MockInstance,
} from 'vitest';
import { SYSTEM_ACTOR, makeActor } from '../../../../../core/actor.js';
import type { MeteringService } from '../../../../../services/metering/MeteringService.js';
import { PuterServer } from '../../../../../server.js';
import { setupTestServer } from '../../../../../testUtil.js';
import { SYSTEM_ACTOR, makeActor } from '../../../../core/actor.js';
import type { MeteringService } from '../../../../services/metering/MeteringService.js';
import { PuterServer } from '../../../../server.js';
import { setupTestServer } from '../../../../testUtil.js';
import {
expectedIdentifierFields,
makeActorMatrix,
sentIdentifierFields,
withTestActor,
} from '../../../shared/integrationTestUtil.js';
import { AIChatStream } from '../../../interfaces/chat/utils/Streaming.js';
} from '../../../integrationTestUtil.js';
import { AIChatStream } from '../../utils/Streaming.js';
import { AzureChatProvider } from './AzureChatProvider.js';
import { AZURE_MODELS } from './models.js';
@@ -85,7 +85,7 @@ const { inlineHttpImageUrlsMock } = vi.hoisted(() => ({
inlineHttpImageUrlsMock: vi.fn(async (_messages: unknown) => {}),
}));
vi.mock('../../../protocols/openaiCompatible/inlineImages.js', () => ({
vi.mock('../../utils/inlineImages.js', () => ({
inlineHttpImageUrls: inlineHttpImageUrlsMock,
}));
@@ -23,33 +23,33 @@ import { ChatCompletionCreateParams } from 'openai/resources/index.js';
import {
serviceSpeedBillingModel,
servedServiceSpeed,
} from '../../../interfaces/chat/utils/serviceSpeed.js';
import { Context } from '../../../../../core/context.js';
import type { FSService } from '../../../../../services/fs/FSService.js';
import type { MeteringService } from '../../../../../services/metering/MeteringService.js';
import type { FSEntryStore } from '../../../../../stores/fs/FSEntryStore.js';
import type { S3ObjectStore } from '../../../../../stores/fs/S3ObjectStore.js';
} from '../../utils/serviceSpeed.js';
import { Context } from '../../../../core/context.js';
import type { FSService } from '../../../../services/fs/FSService.js';
import type { MeteringService } from '../../../../services/metering/MeteringService.js';
import type { FSEntryStore } from '../../../../stores/fs/FSEntryStore.js';
import type { S3ObjectStore } from '../../../../stores/fs/S3ObjectStore.js';
import type {
IChatModel,
IChatProvider,
ICompleteArguments,
} from '../../../interfaces/chat/types.js';
} from '../../types.js';
import {
messagesHaveCompaction,
wantsCompaction,
} from '../../../interfaces/chat/utils/compaction.js';
import * as OpenAiUtil from '../../../protocols/openaiCompatible/OpenAIUtil.js';
} from '../../utils/compaction.js';
import { make_openai_tools } from '../../utils/FunctionCalling.js';
import * as OpenAiUtil from '../../utils/OpenAIUtil.js';
import {
clampReasoningEffort,
openAICompatParams,
toolsToWire,
} from '../../../protocols/openaiCompatible/openaiParams.js';
import { buildCostsOverride } from '../../../interfaces/chat/utils/pricing.js';
import { inlineHttpImageUrls } from '../../../protocols/openaiCompatible/inlineImages.js';
import { processPuterPathUploads } from '../../openai/chat/fileUpload.js';
} from '../../utils/openaiParams.js';
import { buildCostsOverride } from '../../utils/pricing.js';
import { inlineHttpImageUrls } from '../../utils/inlineImages.js';
import { processPuterPathUploads } from '../openai/fileUpload.js';
import { AZURE_MODELS } from './models.js';
import { modelLookupNames } from '../../../interfaces/chat/utils/modelRouting.js';
import { upstreamUserIdentifier } from '../../../shared/upstreamIdentifier.js';
import { modelLookupNames } from '../../utils/modelRouting.js';
import { upstreamUserIdentifier } from '../../../util/upstreamIdentifier.js';
const isWebSearchTool = (tool: Record<string, unknown>): boolean =>
tool.type === 'web_search' ||
@@ -256,7 +256,9 @@ export class AzureChatProvider implements IChatProvider {
// that's missing. We normalise it here so the token count code works.
messages = await OpenAiUtil.process_input_messages(messages);
const mappedTools = tools ? toolsToWire(tools, 'chat') : undefined;
const mappedTools = tools
? make_openai_tools(tools, { dialect: 'chat' })
: undefined;
const requestedReasoningEffort = reasoning_effort ?? reasoning?.effort;
const requestedVerbosity = verbosity ?? text?.verbosity;
@@ -34,9 +34,10 @@ import { describe, expect, it } from 'vitest';
import {
INTEGRATION_TEST_TIMEOUT_MS,
makeMeteringStub,
optionalEnv,
skipUnlessEnv,
withTestActor,
} from '../../../shared/integrationTestUtil.js';
import { optionalEnv, skipUnlessEnv } from '../../../../../testEnv.js';
} from '../../../integrationTestUtil.js';
import { AzureResponsesProvider } from './AzureResponsesProvider.js';
const KEY_ENV = 'PUTER_TEST_AI_AZURE_OPENAI_API_KEY';
@@ -40,17 +40,17 @@ import {
type MockInstance,
} from 'vitest';
import { SYSTEM_ACTOR, makeActor } from '../../../../../core/actor.js';
import type { MeteringService } from '../../../../../services/metering/MeteringService.js';
import { PuterServer } from '../../../../../server.js';
import { setupTestServer } from '../../../../../testUtil.js';
import { SYSTEM_ACTOR, makeActor } from '../../../../core/actor.js';
import type { MeteringService } from '../../../../services/metering/MeteringService.js';
import { PuterServer } from '../../../../server.js';
import { setupTestServer } from '../../../../testUtil.js';
import {
expectedIdentifierFields,
makeActorMatrix,
sentIdentifierFields,
withTestActor,
} from '../../../shared/integrationTestUtil.js';
import { AIChatStream } from '../../../interfaces/chat/utils/Streaming.js';
} from '../../../integrationTestUtil.js';
import { AIChatStream } from '../../utils/Streaming.js';
import { AzureResponsesProvider } from './AzureResponsesProvider.js';
import { AZURE_MODELS } from './models.js';
@@ -22,33 +22,33 @@ import { ResponseCreateParams } from 'openai/resources/responses/responses.mjs';
import {
serviceSpeedBillingModel,
servedServiceSpeed,
} from '../../../interfaces/chat/utils/serviceSpeed.js';
import { Context } from '../../../../../core/context.js';
import type { FSService } from '../../../../../services/fs/FSService.js';
import type { MeteringService } from '../../../../../services/metering/MeteringService.js';
import type { FSEntryStore } from '../../../../../stores/fs/FSEntryStore.js';
import type { S3ObjectStore } from '../../../../../stores/fs/S3ObjectStore.js';
} from '../../utils/serviceSpeed.js';
import { Context } from '../../../../core/context.js';
import type { FSService } from '../../../../services/fs/FSService.js';
import type { MeteringService } from '../../../../services/metering/MeteringService.js';
import type { FSEntryStore } from '../../../../stores/fs/FSEntryStore.js';
import type { S3ObjectStore } from '../../../../stores/fs/S3ObjectStore.js';
import type {
IChatModel,
IChatProvider,
ICompleteArguments,
} from '../../../interfaces/chat/types.js';
import * as OpenAiUtil from '../../../protocols/openaiCompatible/OpenAIUtil.js';
} from '../../types.js';
import { toOpenAiContextManagement } from '../../utils/compaction.js';
import { make_openai_tools } from '../../utils/FunctionCalling.js';
import * as OpenAiUtil from '../../utils/OpenAIUtil.js';
import {
clampReasoningEffort,
openAICompatParams,
promptCacheRetentionToWire,
rejectStatefulResponsesFields,
toolsToWire,
toOpenAiContextManagement,
} from '../../../protocols/openaiCompatible/openaiParams.js';
import { buildCostsOverride } from '../../../interfaces/chat/utils/pricing.js';
import { responseSamplingParams } from '../../../protocols/openaiCompatible/responseSampling.js';
import { processPuterPathUploads } from '../../openai/chat/fileUpload.js';
} from '../../utils/openaiParams.js';
import { buildCostsOverride } from '../../utils/pricing.js';
import { responseSamplingParams } from '../../utils/responseSampling.js';
import { processPuterPathUploads } from '../openai/fileUpload.js';
import { AZURE_MODELS } from './models.js';
import { HttpError } from '@heyputer/backend/src/core/http/HttpError.js';
import { modelLookupNames } from '../../../interfaces/chat/utils/modelRouting.js';
import { upstreamUserIdentifier } from '../../../shared/upstreamIdentifier.js';
import { modelLookupNames } from '../../utils/modelRouting.js';
import { upstreamUserIdentifier } from '../../../util/upstreamIdentifier.js';
const ANTHROPIC_WEB_SEARCH_TYPE = (type: unknown): boolean =>
type === 'web_search_20250305' ||
@@ -245,7 +245,9 @@ export class AzureResponsesProvider implements IChatProvider {
}
});
}
const mappedTools = tools ? toolsToWire(tools, 'responses') : undefined;
const mappedTools = tools
? make_openai_tools(tools, { dialect: 'responses' })
: undefined;
// Strip Anthropic-only shape a fallback-replayed message can carry
// before the existing in-place coercion mutates only this pass's copy.
@@ -25,9 +25,9 @@
import { describe, expect, it } from 'vitest';
import type { IChatModel } from '../../../interfaces/chat/types.js';
import { OPEN_AI_MODELS } from '../../openai/chat/models.js';
import { XAI_MODELS } from '../../xai/chat/models.js';
import type { IChatModel } from '../../types.js';
import { OPEN_AI_MODELS } from '../openai/models.js';
import { XAI_MODELS } from '../xai/models.js';
import { AZURE_MODELS } from './models.js';
// Deployments of models the direct providers have retired: nothing to mirror.
@@ -17,9 +17,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import type { IChatModel } from '../../../interfaces/chat/types.js';
import { OPEN_AI_MODELS } from '../../openai/chat/models.js';
import { XAI_MODELS } from '../../xai/chat/models.js';
import type { IChatModel } from '../../types.js';
import { OPEN_AI_MODELS } from '../openai/models.js';
import { XAI_MODELS } from '../xai/models.js';
/**
* Azure shares its source model's list prices, context tiers, and limits.
@@ -54,10 +54,10 @@ const mirror = (
// own provider folder rather than sharing the OpenAI list.
//
// IMPORTANT: we bill users at the public list price of the equivalent OpenAI /
// xAI model, which `mirror` copies from `../../openai/chat/models.ts` and
// `../../xai/chat/models.ts`. Azure is subsidised for us, so our actual spend
// is lower — but billing at the standard model price is the whole reason we
// route through Azure. Do NOT replace these with Azure's own rates.
// xAI model, which `mirror` copies from `../openai/models.ts` and
// `../xai/models.ts`. Azure is subsidised for us, so our actual spend is lower
// — but billing at the standard model price is the whole reason we route
// through Azure. Do NOT replace these with Azure's own rates.
//
// `id` is the Azure deployment name and is what we send upstream.
export const AZURE_MODELS: IChatModel[] = [
@@ -32,9 +32,10 @@ import { describe, expect, it } from 'vitest';
import {
INTEGRATION_TEST_TIMEOUT_MS,
makeMeteringStub,
optionalEnv,
skipUnlessEnv,
withTestActor,
} from '../../../shared/integrationTestUtil.js';
import { optionalEnv, skipUnlessEnv } from '../../../../../testEnv.js';
} from '../../../integrationTestUtil.js';
import { BytePlusProvider } from './BytePlusProvider.js';
const ENV_VAR = 'PUTER_TEST_AI_BYTEPLUS_API_KEY';
@@ -43,15 +43,15 @@ import {
type MockInstance,
} from 'vitest';
import { SYSTEM_ACTOR } from '../../../../../core/actor.js';
import type { MeteringService } from '../../../../../services/metering/MeteringService.js';
import { PuterServer } from '../../../../../server.js';
import { setupTestServer } from '../../../../../testUtil.js';
import { withTestActor } from '../../../shared/integrationTestUtil.js';
import { AIChatStream } from '../../../interfaces/chat/utils/Streaming.js';
import { SYSTEM_ACTOR } from '../../../../core/actor.js';
import type { MeteringService } from '../../../../services/metering/MeteringService.js';
import { PuterServer } from '../../../../server.js';
import { setupTestServer } from '../../../../testUtil.js';
import { withTestActor } from '../../../integrationTestUtil.js';
import { AIChatStream } from '../../utils/Streaming.js';
import { BytePlusProvider } from './BytePlusProvider.js';
import { BYTEPLUS_MODELS } from './models.js';
import { modelLookupNames } from '../../../interfaces/chat/utils/modelRouting.js';
import { modelLookupNames } from '../../utils/modelRouting.js';
// -- OpenAI SDK mock -------------------------------------------------
//
@@ -18,13 +18,13 @@
*/
import { OpenAI } from 'openai';
import type { MeteringService } from '../../../../../services/metering/MeteringService.js';
import type { ICompleteArguments } from '../../../interfaces/chat/types.js';
import { sdkClientOptions } from '../../../interfaces/chat/utils/sdkClient.js';
import type { MeteringService } from '../../../../services/metering/MeteringService.js';
import type { ICompleteArguments } from '../../types.js';
import { sdkClientOptions } from '../../utils/sdkClient.js';
import {
type ChatProviderConfig,
OpenAICompatProvider,
} from '../../../protocols/openaiCompatible/OpenAICompatProvider.js';
} from '../OpenAICompatProvider.js';
import { BYTEPLUS_MODELS } from './models.js';
type BytePlusCustomParams = {
@@ -17,8 +17,8 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import type { IChatModel } from '../../../interfaces/chat/types.js';
import { usdPerMToken } from '../../../interfaces/chat/utils/pricing.js';
import type { IChatModel } from '../../types.js';
import { usdPerMToken } from '../../utils/pricing.js';
const K = 1_000;
@@ -36,14 +36,15 @@
import { Writable } from 'node:stream';
import { describe, expect, it, vi } from 'vitest';
import type { MeteringService } from '../../../../../services/metering/MeteringService.js';
import type { MeteringService } from '../../../../services/metering/MeteringService.js';
import {
INTEGRATION_TEST_TIMEOUT_MS,
makeMeteringStub,
optionalEnv,
skipUnlessEnv,
withTestActor,
} from '../../../shared/integrationTestUtil.js';
import { optionalEnv, skipUnlessEnv } from '../../../../../testEnv.js';
import { AIChatStream } from '../../../interfaces/chat/utils/Streaming.js';
} from '../../../integrationTestUtil.js';
import { AIChatStream } from '../../utils/Streaming.js';
import { ClaudeProvider } from './ClaudeProvider.js';
const ENV_VAR = 'PUTER_TEST_AI_CLAUDE_API_KEY';
@@ -46,20 +46,20 @@ import {
import { v4 as uuidv4 } from 'uuid';
import type { Actor } from '../../../../../core/actor.js';
import { Context } from '../../../../../core/context.js';
import { SYSTEM_ACTOR } from '../../../../../core/actor.js';
import type { MeteringService } from '../../../../../services/metering/MeteringService.js';
import { PuterServer } from '../../../../../server.js';
import { setupTestServer } from '../../../../../testUtil.js';
import { generateDefaultFsentries } from '../../../../../util/userProvisioning.js';
import { withTestActor } from '../../../shared/integrationTestUtil.js';
import { normalize_messages } from '../../../interfaces/chat/utils/Messages.js';
import { AIChatStream } from '../../../interfaces/chat/utils/Streaming.js';
import type { Actor } from '../../../../core/actor.js';
import { Context } from '../../../../core/context.js';
import { SYSTEM_ACTOR } from '../../../../core/actor.js';
import type { MeteringService } from '../../../../services/metering/MeteringService.js';
import { PuterServer } from '../../../../server.js';
import { setupTestServer } from '../../../../testUtil.js';
import { generateDefaultFsentries } from '../../../../util/userProvisioning.js';
import { withTestActor } from '../../../integrationTestUtil.js';
import { normalize_messages } from '../../utils/Messages.js';
import { AIChatStream } from '../../utils/Streaming.js';
import { FILES_API_BETA } from './fileUpload.js';
import { CLAUDE_MODELS } from './models.js';
import { ClaudeProvider } from './ClaudeProvider.js';
import { modelLookupNames } from '../../../interfaces/chat/utils/modelRouting.js';
import { modelLookupNames } from '../../utils/modelRouting.js';
// ── Anthropic SDK mock ──────────────────────────────────────────────
@@ -387,25 +387,6 @@ describe('ClaudeProvider.complete request shape', () => {
expect(messagesCreateMock).not.toHaveBeenCalled();
});
it.each(['context-management-2025-06-27', { 0: 'x' }, [42, null]])(
'drops `anthropicBetas` %j, which is not a list of names',
async (anthropicBetas) => {
const { provider } = makeProvider();
messagesCreateMock.mockResolvedValueOnce(baseResponse);
await withTestActor(() =>
provider.complete({
model: 'claude-haiku-4-5-20251001',
messages: [{ role: 'user', content: 'hi' }],
anthropicBetas,
} as never),
);
const [args] = messagesCreateMock.mock.calls[0]!;
expect(args.betas).toBeUndefined();
},
);
it('never forwards mcp_servers — sdkParams is an explicit field list', async () => {
const { provider } = makeProvider();
messagesCreateMock.mockResolvedValueOnce(baseResponse);
@@ -19,37 +19,36 @@
import Anthropic from '@anthropic-ai/sdk';
import type { Message } from '@anthropic-ai/sdk/resources';
import { Context } from '../../../../../core/context.js';
import type { FSService } from '../../../../../services/fs/FSService.js';
import type { MeteringService } from '../../../../../services/metering/MeteringService.js';
import type { FSEntryStore } from '../../../../../stores/fs/FSEntryStore.js';
import type { S3ObjectStore } from '../../../../../stores/fs/S3ObjectStore.js';
import { upstreamUserIdentifier } from '../../../shared/upstreamIdentifier.js';
import { Context } from '../../../../core/context.js';
import type { FSService } from '../../../../services/fs/FSService.js';
import type { MeteringService } from '../../../../services/metering/MeteringService.js';
import type { FSEntryStore } from '../../../../stores/fs/FSEntryStore.js';
import type { S3ObjectStore } from '../../../../stores/fs/S3ObjectStore.js';
import { upstreamUserIdentifier } from '../../../util/upstreamIdentifier.js';
import type {
IChatModel,
IChatProvider,
ICompleteArguments,
IChatCompleteResult,
UsageDetails,
} from '../../../interfaces/chat/types.js';
import { messagesHaveCompaction } from '../../../interfaces/chat/utils/compaction.js';
import { toAnthropicContextManagement } from './contextManagement.js';
} from '../../types.js';
import {
messagesHaveCompaction,
toAnthropicContextManagement,
} from '../../utils/compaction.js';
import {
mediaUrlOf,
parseDataUri,
unsupportedMediaTextPart,
} from '../../../interfaces/chat/utils/mediaParts.js';
import {
isToolChoice,
toolChoiceFromWire,
} from '../../../protocols/openaiCompatible/openaiParams.js';
import { buildCostsOverride } from '../../../interfaces/chat/utils/pricing.js';
import { serviceSpeedCosts } from '../../../interfaces/chat/utils/serviceSpeed.js';
} from '../../utils/mediaParts.js';
import { isToolChoice, toolChoiceFromWire } from '../../utils/openaiParams.js';
import { buildCostsOverride } from '../../utils/pricing.js';
import { serviceSpeedCosts } from '../../utils/serviceSpeed.js';
import type {
AIChatStream,
AIChatTextStream,
AIChatToolUseStream,
} from '../../../interfaces/chat/utils/Streaming.js';
} from '../../utils/Streaming.js';
import {
allowlistedFromHeader,
applySafeguardsPolicy,
@@ -71,10 +70,7 @@ import {
} from './anthropicPolicy.js';
import { FILES_API_BETA, processPuterPathUploads } from './fileUpload.js';
import { CLAUDE_MODELS } from './models.js';
import {
sdkClientOptions,
sdkRetryOptions,
} from '../../../interfaces/chat/utils/sdkClient.js';
import { sdkClientOptions, sdkRetryOptions } from '../../utils/sdkClient.js';
/**
* Canonical media part → Anthropic block: `url` source for links, `base64`
@@ -629,13 +625,9 @@ export class ClaudeProvider implements IChatProvider {
const fast =
args.speed === 'fast' &&
modelUsed.serviceSpeedRates?.fast !== undefined;
// The Anthropic route passes the header's names; anything else is dropped.
const headerBetas = Array.isArray(args.anthropicBetas)
? args.anthropicBetas.filter((beta) => typeof beta === 'string')
: undefined;
const safeguardsPolicy = applySafeguardsPolicy(
args.safeguards,
headerBetas,
args.anthropicBetas,
);
// -- metadata -----------------------------------------------------
@@ -721,7 +713,7 @@ export class ClaudeProvider implements IChatProvider {
};
const betas = combineBetas(
deriveBetas(features),
allowlistedFromHeader(headerBetas, features),
allowlistedFromHeader(args.anthropicBetas, features),
safeguardsPolicy ? [safeguardsPolicy.beta] : [],
);
const usesBeta = betas.length > 0;
@@ -18,7 +18,7 @@
*/
import { describe, expect, it } from 'vitest';
import type { IChatModel } from '../../../interfaces/chat/types.js';
import type { IChatModel } from '../../types.js';
import {
allowlistedFromHeader,
applySafeguardsPolicy,
@@ -283,45 +283,16 @@ describe('claudeToolPolicy', () => {
it('throws 400 for a function tool with no function property', () => {
expect(() => claudeToolPolicy([{ type: 'function' }], models)).toThrowError(
expect.objectContaining({
statusCode: 400,
legacyCode: 'bad_request',
}),
expect.objectContaining({ statusCode: 400 }),
);
});
it('throws 400 for a non-object tool entry', () => {
expect(() => claudeToolPolicy([null], models)).toThrowError(
expect.objectContaining({
statusCode: 400,
legacyCode: 'bad_request',
}),
expect.objectContaining({ statusCode: 400 }),
);
});
it('throws 400 for a bare string tool entry', () => {
// A TypeError here has no status, so the retry loop reads it as a
// provider failure and marks the route unhealthy for every caller.
expect(() => claudeToolPolicy(['lookup'], models)).toThrowError(
expect.objectContaining({
statusCode: 400,
legacyCode: 'bad_request',
}),
);
});
it('returns no tools for an empty list', () => {
expect(claudeToolPolicy([], models).tools).toEqual([]);
});
it('defaults max_uses on the OpenAI web_search form', () => {
expect(
claudeToolPolicy([{ type: 'web_search' }], models).tools,
).toEqual([
{ type: 'web_search_20250305', name: 'web_search', max_uses: 10 },
]);
});
it('maps the OpenAI web_search form and clamps max_uses', () => {
const r = claudeToolPolicy([{ type: 'web_search', max_uses: 999 }], models);
expect(r.tools).toEqual([
@@ -380,12 +351,7 @@ describe('claudeToolPolicy', () => {
it('rejects an advisor tool naming a model outside the catalog', () => {
expect(() =>
claudeToolPolicy([{ type: 'advisor_20260301', model: 'not-a-model' }], models),
).toThrowError(
expect.objectContaining({
statusCode: 400,
legacyCode: 'bad_request',
}),
);
).toThrowError(expect.objectContaining({ statusCode: 400 }));
});
it('passes client-executed tools through verbatim', () => {
@@ -26,15 +26,9 @@
* rules.
*/
import { HttpError } from '../../../../../core/http/HttpError.js';
import {
AI_ADVISOR,
AI_WEB_SEARCH_MAX_USES,
} from '../../../shared/aiLimits.js';
import type {
CacheControl,
IChatModel,
} from '../../../interfaces/chat/types.js';
import { HttpError } from '../../../../core/http/HttpError.js';
import { AI_ADVISOR, AI_WEB_SEARCH_MAX_USES } from '../../../util/aiLimits.js';
import type { CacheControl, IChatModel } from '../../types.js';
import { FILES_API_BETA } from './fileUpload.js';
// -- cache_control ------------------------------------------------------
@@ -29,11 +29,11 @@
import type Anthropic from '@anthropic-ai/sdk';
import { v4 as uuidv4 } from 'uuid';
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
import type { Actor } from '../../../../../core/actor.js';
import { runWithContext } from '../../../../../core/context.js';
import { PuterServer } from '../../../../../server.js';
import { setupTestServer } from '../../../../../testUtil.js';
import { generateDefaultFsentries } from '../../../../../util/userProvisioning.js';
import type { Actor } from '../../../../core/actor.js';
import { runWithContext } from '../../../../core/context.js';
import { PuterServer } from '../../../../server.js';
import { setupTestServer } from '../../../../testUtil.js';
import { generateDefaultFsentries } from '../../../../util/userProvisioning.js';
import { FILES_API_BETA, processPuterPathUploads } from './fileUpload.js';
const CLAUDE_MAX_FILE_SIZE = 30 * 1_000_000;
@@ -18,11 +18,11 @@
*/
import Anthropic, { toFile } from '@anthropic-ai/sdk';
import type { Actor } from '../../../../../core/actor.js';
import type { FSService } from '../../../../../services/fs/FSService.js';
import type { FSEntryStore } from '../../../../../stores/fs/FSEntryStore.js';
import type { S3ObjectStore } from '../../../../../stores/fs/S3ObjectStore.js';
import { loadFileInput } from '../../../../util/fileInput.js';
import type { Actor } from '../../../../core/actor.js';
import type { FSService } from '../../../../services/fs/FSService.js';
import type { FSEntryStore } from '../../../../stores/fs/FSEntryStore.js';
import type { S3ObjectStore } from '../../../../stores/fs/S3ObjectStore.js';
import { loadFileInput } from '../../../util/fileInput.js';
export const FILES_API_BETA = 'files-api-2025-04-14';
// Claude's documented per-file cap is 500MB, but pulling huge objects
@@ -17,7 +17,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import type { IChatModel } from '../../../interfaces/chat/types.js';
import type { IChatModel } from '../../types.js';
// Sources: https://models.dev/api.json and https://platform.claude.com/docs/en/about-claude/pricing
export const CLAUDE_MODELS: IChatModel[] = [
@@ -28,9 +28,10 @@ import { describe, expect, it } from 'vitest';
import {
INTEGRATION_TEST_TIMEOUT_MS,
makeMeteringStub,
optionalEnv,
skipUnlessEnv,
withTestActor,
} from '../../../shared/integrationTestUtil.js';
import { optionalEnv, skipUnlessEnv } from '../../../../../testEnv.js';
} from '../../../integrationTestUtil.js';
import { DeepSeekProvider } from './DeepSeekProvider.js';
const ENV_VAR = 'PUTER_TEST_AI_DEEPSEEK_API_KEY';
@@ -42,16 +42,16 @@ import {
type MockInstance,
} from 'vitest';
import { SYSTEM_ACTOR } from '../../../../../core/actor.js';
import type { MeteringService } from '../../../../../services/metering/MeteringService.js';
import { PuterServer } from '../../../../../server.js';
import { setupTestServer } from '../../../../../testUtil.js';
import { withTestActor } from '../../../shared/integrationTestUtil.js';
import { costsForRequest, usageCost } from '../../../interfaces/chat/utils/pricing.js';
import { AIChatStream } from '../../../interfaces/chat/utils/Streaming.js';
import { SYSTEM_ACTOR } from '../../../../core/actor.js';
import type { MeteringService } from '../../../../services/metering/MeteringService.js';
import { PuterServer } from '../../../../server.js';
import { setupTestServer } from '../../../../testUtil.js';
import { withTestActor } from '../../../integrationTestUtil.js';
import { costsForRequest, usageCost } from '../../utils/pricing.js';
import { AIChatStream } from '../../utils/Streaming.js';
import { DEEPSEEK_MODELS } from './models.js';
import { DeepSeekProvider } from './DeepSeekProvider.js';
import { modelLookupNames } from '../../../interfaces/chat/utils/modelRouting.js';
import { modelLookupNames } from '../../utils/modelRouting.js';
// ── OpenAI SDK mock ─────────────────────────────────────────────────
@@ -19,13 +19,13 @@
import dedent from 'dedent';
import { OpenAI } from 'openai';
import type { MeteringService } from '../../../../../services/metering/MeteringService.js';
import type { PuterMessage } from '../../../interfaces/chat/types.js';
import { sdkClientOptions } from '../../../interfaces/chat/utils/sdkClient.js';
import type { MeteringService } from '../../../../services/metering/MeteringService.js';
import type { PuterMessage } from '../../types.js';
import { sdkClientOptions } from '../../utils/sdkClient.js';
import {
type ChatProviderConfig,
OpenAICompatProvider,
} from '../../../protocols/openaiCompatible/OpenAICompatProvider.js';
} from '../OpenAICompatProvider.js';
import { DEEPSEEK_MODELS } from './models.js';
// Function calling currently loops unless the tool result is restated as a
@@ -17,7 +17,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import type { IChatModel } from '../../../interfaces/chat/types.js';
import type { IChatModel } from '../../types.js';
// Peak rates from https://api-docs.deepseek.com/quick_start/pricing. Direct
// DeepSeek always bills at these rates, including off-peak hours, weekends and
@@ -26,21 +26,22 @@
import { Writable } from 'node:stream';
import axios from 'axios';
import { setupPuterTestEnv } from '../../../../../testUtil.js';
import { setupPuterTestEnv } from '../../../../testUtil.js';
import { describe, expect, it } from 'vitest';
import {
INTEGRATION_TEST_TIMEOUT_MS,
makeMeteringStub,
optionalEnv,
skipUnlessEnv,
withTestActor,
} from '../../../shared/integrationTestUtil.js';
import { optionalEnv, skipUnlessEnv } from '../../../../../testEnv.js';
} from '../../../integrationTestUtil.js';
import type {
ICompleteArguments,
IChatMessageResult,
UsageDetails,
} from '../../../interfaces/chat/types.js';
import { normalize_messages } from '../../../interfaces/chat/utils/Messages.js';
import { AIChatStream } from '../../../interfaces/chat/utils/Streaming.js';
} from '../../types.js';
import { normalize_messages } from '../../utils/Messages.js';
import { AIChatStream } from '../../utils/Streaming.js';
import { GeminiChatProvider } from './GeminiChatProvider.js';
const ENV_VAR = 'PUTER_TEST_AI_GEMINI_API_KEY';
@@ -113,6 +114,7 @@ describe.skipIf(skipUnlessEnv(ENV_VAR))(
expect(typeof text === 'string' && text.length > 0).toBe(true);
},
);
},
);
@@ -376,36 +378,3 @@ describe.skipIf(skipUnlessEnv(ENV_VAR))(
);
},
);
describe.skipIf(skipUnlessEnv(ENV_VAR))(
'Gemini 4 Carbon and Hydrogen (integration)',
() => {
it.each([
['gemini-4-carbon', 'low'],
['gemini-4-hydrogen', 'xhigh'],
])(
'generates a native text completion from %s at %s effort',
{ timeout: INTEGRATION_TEST_TIMEOUT_MS },
async (model, effort) => {
const provider = new GeminiChatProvider(
{ apiKey: optionalEnv(ENV_VAR)! },
makeMeteringStub(),
);
const result = await withTestActor(() =>
provider.complete({
model,
messages: [{ role: 'user', content: 'Reply with OK.' }],
reasoning_effort: effort,
max_tokens: 32768,
} as ICompleteArguments),
);
const message = result as IChatMessageResult;
expect(typeof message.message.content).toBe('string');
expect(
(message.message.content as string).trim().length,
).toBeGreaterThan(0);
expectBilledOutput(message.usage, message.usageDetails);
},
);
},
);
@@ -36,23 +36,23 @@ import {
type MockInstance,
} from 'vitest';
import { Context } from '../../../../../core/context.js';
import type { MeteringService } from '../../../../../services/metering/MeteringService.js';
import { PuterServer } from '../../../../../server.js';
import { setupTestServer } from '../../../../../testUtil.js';
import { Context } from '../../../../core/context.js';
import type { MeteringService } from '../../../../services/metering/MeteringService.js';
import { PuterServer } from '../../../../server.js';
import { setupTestServer } from '../../../../testUtil.js';
import {
readStreamResult,
withTestActor,
} from '../../../shared/integrationTestUtil.js';
import { ChatCompletionDriver } from '../../../interfaces/chat/ChatCompletionDriver.js';
import type { ICompleteArguments, UsageDetails } from '../../../interfaces/chat/types.js';
import { normalize_messages } from '../../../interfaces/chat/utils/Messages.js';
import { trackedOutputTokens } from '../../../interfaces/chat/utils/pricing.js';
import { AIChatStream } from '../../../interfaces/chat/utils/Streaming.js';
} from '../../../integrationTestUtil.js';
import { ChatCompletionDriver } from '../../ChatCompletionDriver.js';
import type { ICompleteArguments, UsageDetails } from '../../types.js';
import { normalize_messages } from '../../utils/Messages.js';
import { trackedOutputTokens } from '../../utils/pricing.js';
import { AIChatStream } from '../../utils/Streaming.js';
import {
isUpstreamTimeoutError,
UpstreamDeadlineError,
} from '../../../../util/upstreamErrors.js';
} from '../../../util/upstreamErrors.js';
import { GeminiChatProvider } from './GeminiChatProvider.js';
import { geminiNativeClient } from './geminiNative.js';
import { GEMINI_MODELS } from './models.js';
@@ -114,8 +114,6 @@ const makeProvider = (
const ARGON = GEMINI_MODELS.find((m) => m.id === 'gemini-4-argon')!;
const LAUNCH = new Date('2026-10-14T12:00:00Z');
const NATIVE_IDS = [
'gemini-4-hydrogen',
'gemini-4-carbon',
'gemini-4-argon',
'gemini-3.1-flash-lite',
'gemini-3-flash-preview',
@@ -529,20 +527,6 @@ describe('gemini-4-argon text completion', () => {
});
});
describe('gemini-4-argon request without a turn', () => {
it.each([
['no messages', []],
['only a system message', [{ role: 'system', content: 'Be brief.' }]],
])('rejects %s with 400 before calling Gemini', async (_label, messages) => {
await expect(complete({ messages } as never)).rejects.toMatchObject({
statusCode: 400,
legacyCode: 'bad_request',
message: '`messages` must include a user or assistant message',
});
expect(generateContent).not.toHaveBeenCalled();
});
});
describe('gemini-4-argon unsupported content', () => {
const AUDIO = {
type: 'input_audio',
@@ -1618,119 +1602,3 @@ describe('geminiNativeClient deadline', () => {
expect(isUpstreamTimeoutError(err)).toBe(false);
});
});
describe('Gemini 4 Carbon', () => {
it.each([
['2026-11-30T23:59:59.999Z', 200, 1000, 10],
['2026-12-01T00:00:00.000Z', 400, 2000, 20],
])(
'routes natively and meters thinking at %s',
async (date, input, output, cached) => {
generateContent.mockResolvedValueOnce(
textResponse('OK', { ...LIVE_USAGE, cachedContentTokenCount: 6 }),
);
await complete(
{
model: 'google/gemini-4-carbon',
temperature: 0.5,
reasoning_effort: 'medium',
},
new Date(date),
);
expect(requestOf().model).toBe('gemini-4-carbon');
expect(requestOf().config).not.toHaveProperty('temperature');
expect(openAICreate).not.toHaveBeenCalled();
expect(recordSpy.mock.calls[0]![3]).toMatchObject({
prompt_tokens: 30 * input,
completion_tokens: 5 * output,
thinking_tokens: 69 * output,
cached_tokens: 6 * cached,
});
},
);
it.each([
['no max_tokens', undefined],
['a larger max_tokens', 65_536],
])(
'caps a non-streamed request with %s at 32,768 output tokens',
async (_label, maxTokens) => {
generateContent.mockResolvedValueOnce(textResponse('OK'));
await complete({ model: 'gemini-4-carbon', max_tokens: maxTokens });
expect(requestOf().config.maxOutputTokens).toBe(32_768);
},
);
it('sends a stream the max_tokens it asked for', async () => {
generateContentStream.mockResolvedValueOnce(
asAsyncIterable([textResponse('OK')]),
);
await runStream({ model: 'gemini-4-carbon', max_tokens: 65_536 });
expect(requestOf(generateContentStream).config.maxOutputTokens).toBe(
65_536,
);
});
});
describe('Gemini 4 Hydrogen', () => {
const hydrogen = (args: Partial<ICompleteArguments>) =>
complete(
{ model: 'gemini-4-hydrogen', ...args },
new Date('2026-12-01T00:00:00.000Z'),
);
it('routes natively and meters thinking at its own rates', async () => {
generateContent.mockResolvedValueOnce(
textResponse('OK', { ...LIVE_USAGE, cachedContentTokenCount: 6 }),
);
await hydrogen({ model: 'google/gemini-4-hydrogen', temperature: 0.5 });
expect(requestOf().model).toBe('gemini-4-hydrogen');
expect(requestOf().config).not.toHaveProperty('temperature');
expect(requestOf().config).not.toHaveProperty('thinkingConfig');
expect(openAICreate).not.toHaveBeenCalled();
expect(recordSpy.mock.calls[0]![2]).toBe('gemini:gemini-4-hydrogen');
expect(recordSpy.mock.calls[0]![3]).toMatchObject({
prompt_tokens: 30 * 150,
completion_tokens: 5 * 750,
thinking_tokens: 69 * 750,
cached_tokens: 6 * 7.5,
});
});
it.each([
['low', 'LOW'],
['medium', 'MEDIUM'],
['high', 'HIGH'],
['xhigh', 'XHIGH'],
])('sends reasoning_effort %s as thinkingLevel %s', async (effort, level) => {
generateContent.mockResolvedValueOnce(textResponse('OK'));
await hydrogen({
reasoning_effort: effort as ICompleteArguments['reasoning_effort'],
});
expect(requestOf().config.thinkingConfig).toEqual({
thinkingLevel: level,
});
});
it.each(['none', 'minimal', 'max'])(
'rejects reasoning_effort %s before calling Google',
async (effort) => {
await expect(
hydrogen({
reasoning_effort:
effort as ICompleteArguments['reasoning_effort'],
}),
).rejects.toMatchObject({
statusCode: 400,
legacyCode: 'bad_request',
});
expect(generateContent).not.toHaveBeenCalled();
},
);
it('sends a non-streamed request the max_tokens it asked for', async () => {
generateContent.mockResolvedValueOnce(textResponse('OK'));
await hydrogen({ max_tokens: 65_536 });
expect(requestOf().config.maxOutputTokens).toBe(65_536);
});
});
@@ -43,17 +43,17 @@ import {
type MockInstance,
} from 'vitest';
import { SYSTEM_ACTOR } from '../../../../../core/actor.js';
import type { MeteringService } from '../../../../../services/metering/MeteringService.js';
import { PuterServer } from '../../../../../server.js';
import { setupTestServer } from '../../../../../testUtil.js';
import { withTestActor } from '../../../shared/integrationTestUtil.js';
import { normalize_messages } from '../../../interfaces/chat/utils/Messages.js';
import { AIChatStream } from '../../../interfaces/chat/utils/Streaming.js';
import { SYSTEM_ACTOR } from '../../../../core/actor.js';
import type { MeteringService } from '../../../../services/metering/MeteringService.js';
import { PuterServer } from '../../../../server.js';
import { setupTestServer } from '../../../../testUtil.js';
import { withTestActor } from '../../../integrationTestUtil.js';
import { normalize_messages } from '../../utils/Messages.js';
import { AIChatStream } from '../../utils/Streaming.js';
import { GEMINI_MODELS } from './models.js';
import { GeminiChatProvider } from './GeminiChatProvider.js';
import { modelLookupNames } from '../../../interfaces/chat/utils/modelRouting.js';
import { costsForRequest, listedCosts } from '../../../interfaces/chat/utils/pricing.js';
import { modelLookupNames } from '../../utils/modelRouting.js';
import { costsForRequest, listedCosts } from '../../utils/pricing.js';
// ── OpenAI SDK mock ─────────────────────────────────────────────────
//
@@ -82,7 +82,7 @@ const { inlineHttpImageUrlsMock } = vi.hoisted(() => ({
inlineHttpImageUrlsMock: vi.fn(async (_messages: unknown) => {}),
}));
vi.mock('../../../protocols/openaiCompatible/inlineImages.js', () => ({
vi.mock('../../utils/inlineImages.js', () => ({
inlineHttpImageUrls: inlineHttpImageUrlsMock,
}));
@@ -191,7 +191,7 @@ describe('GeminiChatProvider model catalog', () => {
// happily on a doubled id, and a doubled id is not hypothetical here --
// gemini-3.7-flash was once declared twice with two different cache
// prices. Catalog-wide uniqueness is enforced for every provider in
// interfaces/chat/modelCatalogs.test.ts; this checks the other end, that the
// providers/modelCatalogs.test.ts; this checks the other end, that the
// provider actually routes through the deduplicating helper rather than
// flattening the catalog itself.
it('list() emits every id exactly once', async () => {
@@ -24,36 +24,40 @@ import {
GoogleGenAI,
} from '@google/genai';
import { OpenAI } from 'openai';
import { HttpError } from '../../../../../core/http/HttpError.js';
import type { MeteringService } from '../../../../../services/metering/MeteringService.js';
import { HttpError } from '../../../../core/http/HttpError.js';
import type { MeteringService } from '../../../../services/metering/MeteringService.js';
import type {
IChatModel,
ICompleteArguments,
PuterMessage,
UsageDetails,
} from '../../../interfaces/chat/types.js';
import { toolChoiceToWire } from '../../../protocols/openaiCompatible/openaiParams.js';
import { sdkClientOptions } from '../../../interfaces/chat/utils/sdkClient.js';
} from '../../types.js';
import { toolChoiceToWire } from '../../utils/openaiParams.js';
import { sdkClientOptions } from '../../utils/sdkClient.js';
import {
type ChatCompletionsClient,
type ChatProviderConfig,
OpenAICompatProvider,
type UsageSource,
} from '../../../protocols/openaiCompatible/OpenAICompatProvider.js';
import { usesGoogleGeminiEndpoint } from '../geminiRest.js';
} from '../OpenAICompatProvider.js';
import { usesGoogleGeminiEndpoint } from '../../../util/geminiRest.js';
import { geminiNativeClient } from './geminiNative.js';
import { GEMINI_MODELS, GEMINI_NATIVE_MODELS } from './models.js';
import {
GEMINI_MODELS,
GEMINI_NATIVE_MODELS,
GEMINI_ARGON_NON_STREAM_MAX_TOKENS,
} from './models.js';
type GroundingContent = { grounding_metadata?: unknown };
/** Some native models have a lower output cap when the response isn't streamed. */
/** Unary Argon uses a lower output cap than streaming. */
const nativeOutputCap = (
args: ICompleteArguments,
model: IChatModel,
): number | undefined =>
args.stream
? undefined
: GEMINI_NATIVE_MODELS.get(model.id)?.unaryMaxTokens;
model.id === 'gemini-4-argon' && !args.stream
? GEMINI_ARGON_NON_STREAM_MAX_TOKENS
: undefined;
const COMPAT_BASE_URL =
'https://generativelanguage.googleapis.com/v1beta/openai/';
@@ -31,15 +31,15 @@ import type {
Part,
ThinkingLevel,
} from '@google/genai';
import { HttpError } from '../../../../../core/http/HttpError.js';
import { HttpError } from '../../../../core/http/HttpError.js';
import {
parseDataUri,
unsupportedMediaTextPart,
} from '../../../interfaces/chat/utils/mediaParts.js';
import { SDK_TIMEOUT_MS } from '../../../interfaces/chat/utils/sdkClient.js';
import { promoteStopForToolCalls } from '../../../interfaces/chat/utils/stopReason.js';
import { UpstreamDeadlineError } from '../../../../util/upstreamErrors.js';
import type { ChatCompletionsClient } from '../../../protocols/openaiCompatible/OpenAICompatProvider.js';
} from '../../utils/mediaParts.js';
import { SDK_TIMEOUT_MS } from '../../utils/sdkClient.js';
import { promoteStopForToolCalls } from '../../utils/stopReason.js';
import { UpstreamDeadlineError } from '../../../util/upstreamErrors.js';
import type { ChatCompletionsClient } from '../OpenAICompatProvider.js';
type ChatContentPart =
string | { type?: string; text?: string; image_url?: { url?: string } };
@@ -78,7 +78,7 @@ interface ChatParams {
tool_choice?: ChatToolChoice;
max_completion_tokens?: number;
temperature?: number;
reasoning_effort?: 'minimal' | 'low' | 'medium' | 'high' | 'xhigh';
reasoning_effort?: 'minimal' | 'low' | 'medium' | 'high';
stream?: boolean;
service_tier?: string;
}
@@ -233,13 +233,6 @@ const toGenerateContentRequest = (
signal: AbortSignal | undefined,
) => {
const { contents, systemInstruction } = toGeminiContents(params.messages);
if (!contents.length) {
throw new HttpError(
400,
'`messages` must include a user or assistant message',
{ legacyCode: 'bad_request' },
);
}
// Only caller-defined functions; Google's built-in tools aren't offered.
const functions = (params.tools ?? []).flatMap((tool) =>
tool.type === 'function' && tool.function ? [tool.function] : [],
@@ -22,15 +22,13 @@ import type {
ModelCost,
ModelPricingOverride,
ReasoningEffort,
} from '../../../interfaces/chat/types.js';
} from '../../types.js';
/** What a natively served model accepts beyond the shared request fields. */
export interface GeminiNativeOptions {
reasoningEfforts: ReadonlySet<ReasoningEffort>;
/** False where Google ignores temperature, so it isn't sent. */
temperature: boolean;
/** Output cap of a response that isn't streamed, where Google's is lower. */
unaryMaxTokens?: number;
}
const FLASH_NATIVE_OPTIONS: GeminiNativeOptions = {
@@ -38,25 +36,16 @@ const FLASH_NATIVE_OPTIONS: GeminiNativeOptions = {
temperature: true,
};
// Google's model metadata reports the unary cap; streaming keeps the catalog cap.
const ARGON_CARBON_NATIVE_OPTIONS: GeminiNativeOptions = {
reasoningEfforts: new Set(['low', 'medium', 'high']),
temperature: false,
unaryMaxTokens: 32_768,
};
/**
* Served natively: the OpenAI-compatible endpoint omits these models' thinking
* count from usage.
*/
export const GEMINI_NATIVE_MODELS: ReadonlyMap<string, GeminiNativeOptions> =
new Map([
['gemini-4-argon', ARGON_CARBON_NATIVE_OPTIONS],
['gemini-4-carbon', ARGON_CARBON_NATIVE_OPTIONS],
[
'gemini-4-hydrogen',
'gemini-4-argon',
{
reasoningEfforts: new Set(['low', 'medium', 'high', 'xhigh']),
reasoningEfforts: new Set(['low', 'medium', 'high']),
temperature: false,
},
],
@@ -99,7 +88,7 @@ const FLASH_LIST_PRICING = listPricingFrom(
FLASH_LAUNCH_COSTS,
);
const GEMINI_4_LAUNCH_COSTS: ModelCost = {
const ARGON_LAUNCH_COSTS: ModelCost = {
tokens: 1_000_000,
prompt_tokens: 200,
completion_tokens: 1000,
@@ -130,6 +119,9 @@ const PRO_LONG_CONTEXT_PRICING: ModelPricingOverride[] = [
},
];
// Google's unary model metadata reports this cap; streaming keeps the catalog cap.
export const GEMINI_ARGON_NON_STREAM_MAX_TOKENS = 32_768;
export const GEMINI_MODELS: IChatModel[] = [
{
puterId: 'google:google/gemini-4-argon',
@@ -151,66 +143,12 @@ export const GEMINI_MODELS: IChatModel[] = [
input_cost_key: 'prompt_tokens',
output_cost_key: 'completion_tokens',
serviceSpeedRates: { fast: 1.8 },
costs: GEMINI_4_LAUNCH_COSTS,
costs: ARGON_LAUNCH_COSTS,
pricingOverrides: listPricingFrom(
'2026-12-01T00:00:00Z',
GEMINI_4_LAUNCH_COSTS,
ARGON_LAUNCH_COSTS,
),
},
{
puterId: 'google:google/gemini-4-carbon',
id: 'gemini-4-carbon',
// The native adapter forwards only text and image parts.
modalities: {
input: ['text', 'image'],
output: ['text'],
},
open_weights: false,
tool_call: true,
release_date: '2026-11-10',
name: 'Gemini 4 Carbon',
aliases: ['google/gemini-4-carbon'],
// One window shared by input, thinking and output.
context: 1_048_576,
max_tokens: 65_536,
costs_currency: 'usd-cents',
input_cost_key: 'prompt_tokens',
output_cost_key: 'completion_tokens',
serviceSpeedRates: { fast: 1.8 },
costs: GEMINI_4_LAUNCH_COSTS,
pricingOverrides: listPricingFrom(
'2026-12-01T00:00:00Z',
GEMINI_4_LAUNCH_COSTS,
),
},
{
puterId: 'google:google/gemini-4-hydrogen',
id: 'gemini-4-hydrogen',
// The native adapter forwards only text and image parts.
modalities: {
input: ['text', 'image'],
output: ['text'],
},
open_weights: false,
tool_call: true,
release_date: '2026-11-12',
name: 'Gemini 4 Hydrogen',
aliases: ['google/gemini-4-hydrogen'],
// One window shared by input, thinking and output.
context: 1_048_576,
max_tokens: 65_536,
costs_currency: 'usd-cents',
input_cost_key: 'prompt_tokens',
output_cost_key: 'completion_tokens',
serviceSpeedRates: { fast: 1.8 },
costs: {
tokens: 1_000_000,
prompt_tokens: 150,
completion_tokens: 750,
thinking_tokens: 750,
cached_tokens: 7.5,
},
},
{
puterId: 'google:google/gemini-3.5-flash',
id: 'gemini-3.5-flash',
@@ -29,9 +29,10 @@ import { describe, expect, it } from 'vitest';
import {
INTEGRATION_TEST_TIMEOUT_MS,
makeMeteringStub,
optionalEnv,
skipUnlessEnv,
withTestActor,
} from '../../../shared/integrationTestUtil.js';
import { optionalEnv, skipUnlessEnv } from '../../../../../testEnv.js';
} from '../../../integrationTestUtil.js';
import { GroqAIProvider } from './GroqAIProvider.js';
const ENV_VAR = 'PUTER_TEST_AI_GROQ_API_KEY';
@@ -29,7 +29,7 @@
* exercises the real Groq endpoint.
*/
import { Context } from '../../../../../core/context.js';
import { Context } from '../../../../core/context.js';
import { Writable } from 'node:stream';
import {
afterAll,
@@ -43,15 +43,15 @@ import {
type MockInstance,
} from 'vitest';
import { SYSTEM_ACTOR } from '../../../../../core/actor.js';
import type { MeteringService } from '../../../../../services/metering/MeteringService.js';
import { PuterServer } from '../../../../../server.js';
import { setupTestServer } from '../../../../../testUtil.js';
import { withTestActor } from '../../../shared/integrationTestUtil.js';
import { AIChatStream } from '../../../interfaces/chat/utils/Streaming.js';
import { SYSTEM_ACTOR } from '../../../../core/actor.js';
import type { MeteringService } from '../../../../services/metering/MeteringService.js';
import { PuterServer } from '../../../../server.js';
import { setupTestServer } from '../../../../testUtil.js';
import { withTestActor } from '../../../integrationTestUtil.js';
import { AIChatStream } from '../../utils/Streaming.js';
import { GROQ_MODELS } from './models.js';
import { GroqAIProvider } from './GroqAIProvider.js';
import { modelLookupNames } from '../../../interfaces/chat/utils/modelRouting.js';
import { modelLookupNames } from '../../utils/modelRouting.js';
// ── Groq SDK mock ───────────────────────────────────────────────────
@@ -19,13 +19,13 @@
import Groq from 'groq-sdk';
import type { CompletionUsage } from 'openai/resources';
import type { MeteringService } from '../../../../../services/metering/MeteringService.js';
import type { PuterMessage } from '../../../interfaces/chat/types.js';
import { sdkClientOptions } from '../../../interfaces/chat/utils/sdkClient.js';
import type { MeteringService } from '../../../../services/metering/MeteringService.js';
import type { PuterMessage } from '../../types.js';
import { sdkClientOptions } from '../../utils/sdkClient.js';
import {
type ChatProviderConfig,
OpenAICompatProvider,
} from '../../../protocols/openaiCompatible/OpenAICompatProvider.js';
} from '../OpenAICompatProvider.js';
import { GROQ_MODELS } from './models.js';
export class GroqAIProvider extends OpenAICompatProvider {
@@ -17,7 +17,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import type { IChatModel } from '../../../interfaces/chat/types.js';
import type { IChatModel } from '../../types.js';
// Hardcoded from https://models.dev/api.json (Groq chat/text models)
export const GROQ_MODELS: IChatModel[] = [
@@ -29,9 +29,10 @@ import { describe, expect, it } from 'vitest';
import {
INTEGRATION_TEST_TIMEOUT_MS,
makeMeteringStub,
optionalEnv,
skipUnlessEnv,
withTestActor,
} from '../../../shared/integrationTestUtil.js';
import { optionalEnv, skipUnlessEnv } from '../../../../../testEnv.js';
} from '../../../integrationTestUtil.js';
import { HoonifyProvider } from './HoonifyProvider.js';
const ENV_VAR = 'PUTER_TEST_AI_HOONIFY_API_KEY';
@@ -43,16 +43,16 @@ import {
type MockInstance,
} from 'vitest';
import { SYSTEM_ACTOR } from '../../../../../core/actor.js';
import type { MeteringService } from '../../../../../services/metering/MeteringService.js';
import { PuterServer } from '../../../../../server.js';
import { setupTestServer } from '../../../../../testUtil.js';
import { withTestActor } from '../../../shared/integrationTestUtil.js';
import { AIChatStream } from '../../../interfaces/chat/utils/Streaming.js';
import { usdPerMToken } from '../../../interfaces/chat/utils/pricing.js';
import { SYSTEM_ACTOR } from '../../../../core/actor.js';
import type { MeteringService } from '../../../../services/metering/MeteringService.js';
import { PuterServer } from '../../../../server.js';
import { setupTestServer } from '../../../../testUtil.js';
import { withTestActor } from '../../../integrationTestUtil.js';
import { AIChatStream } from '../../utils/Streaming.js';
import { usdPerMToken } from '../../utils/pricing.js';
import { HOONIFY_MODELS } from './models.js';
import { HoonifyProvider } from './HoonifyProvider.js';
import { modelLookupNames } from '../../../interfaces/chat/utils/modelRouting.js';
import { modelLookupNames } from '../../utils/modelRouting.js';
// ── OpenAI SDK mock ─────────────────────────────────────────────────
//
@@ -18,16 +18,13 @@
*/
import { OpenAI } from 'openai';
import type { MeteringService } from '../../../../../services/metering/MeteringService.js';
import type {
IChatModel,
ICompleteArguments,
} from '../../../interfaces/chat/types.js';
import { sdkClientOptions } from '../../../interfaces/chat/utils/sdkClient.js';
import type { MeteringService } from '../../../../services/metering/MeteringService.js';
import type { IChatModel, ICompleteArguments } from '../../types.js';
import { sdkClientOptions } from '../../utils/sdkClient.js';
import {
type ChatProviderConfig,
OpenAICompatProvider,
} from '../../../protocols/openaiCompatible/OpenAICompatProvider.js';
} from '../OpenAICompatProvider.js';
import { HOONIFY_MODELS } from './models.js';
const asRecord = (value: unknown): Record<string, unknown> =>
@@ -17,8 +17,8 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import type { IChatModel } from '../../../interfaces/chat/types.js';
import { usdPerMToken } from '../../../interfaces/chat/utils/pricing.js';
import type { IChatModel } from '../../types.js';
import { usdPerMToken } from '../../utils/pricing.js';
const K = 1_000;
@@ -29,9 +29,10 @@ import { describe, expect, it } from 'vitest';
import {
INTEGRATION_TEST_TIMEOUT_MS,
makeMeteringStub,
optionalEnv,
skipUnlessEnv,
withTestActor,
} from '../../../shared/integrationTestUtil.js';
import { optionalEnv, skipUnlessEnv } from '../../../../../testEnv.js';
} from '../../../integrationTestUtil.js';
import { InfronProvider } from './InfronProvider.js';
const ENV_VAR = 'PUTER_TEST_AI_INFRON_API_KEY';
@@ -44,13 +44,13 @@ import {
type MockInstance,
} from 'vitest';
import type { MeteringService } from '../../../../../services/metering/MeteringService.js';
import { PuterServer } from '../../../../../server.js';
import { setupTestServer } from '../../../../../testUtil.js';
import { withTestActor } from '../../../shared/integrationTestUtil.js';
import { AIChatStream } from '../../../interfaces/chat/utils/Streaming.js';
import type { MeteringService } from '../../../../services/metering/MeteringService.js';
import { PuterServer } from '../../../../server.js';
import { setupTestServer } from '../../../../testUtil.js';
import { withTestActor } from '../../../integrationTestUtil.js';
import { AIChatStream } from '../../utils/Streaming.js';
import { InfronProvider } from './InfronProvider.js';
import { modelLookupNames } from '../../../interfaces/chat/utils/modelRouting.js';
import { modelLookupNames } from '../../utils/modelRouting.js';
// ── OpenAI SDK mock ─────────────────────────────────────────────────
@@ -19,16 +19,16 @@
import axios from 'axios';
import { OpenAI } from 'openai';
import type { MeteringService } from '../../../../../services/metering/MeteringService.js';
import type { IChatModel } from '../../../interfaces/chat/types.js';
import { cachedRemoteCatalog } from '../../../shared/cachedRemoteCatalog.js';
import { catalogPrice } from '../../../shared/catalogPrice.js';
import { sdkClientOptions } from '../../../interfaces/chat/utils/sdkClient.js';
import type { MeteringService } from '../../../../services/metering/MeteringService.js';
import type { IChatModel } from '../../types.js';
import { cachedRemoteCatalog } from '../../utils/cachedRemoteCatalog.js';
import { catalogPrice } from '../../utils/pricing.js';
import { sdkClientOptions } from '../../utils/sdkClient.js';
import {
type ChatProviderConfig,
OpenAICompatProvider,
type UsageSource,
} from '../../../protocols/openaiCompatible/OpenAICompatProvider.js';
} from '../OpenAICompatProvider.js';
/**
* One upstream offering of a model in Infron's catalog. The same model is often
@@ -30,9 +30,10 @@ import { describe, expect, it } from 'vitest';
import {
INTEGRATION_TEST_TIMEOUT_MS,
makeMeteringStub,
optionalEnv,
skipUnlessEnv,
withTestActor,
} from '../../../shared/integrationTestUtil.js';
import { optionalEnv, skipUnlessEnv } from '../../../../../testEnv.js';
} from '../../../integrationTestUtil.js';
import { MetaProvider } from './MetaProvider.js';
const ENV_VAR = 'PUTER_TEST_AI_META_API_KEY';
@@ -42,17 +42,17 @@ import {
type MockInstance,
} from 'vitest';
import type { Actor } from '../../../../../core/actor.js';
import { SYSTEM_ACTOR, makeActor } from '../../../../../core/actor.js';
import type { MeteringService } from '../../../../../services/metering/MeteringService.js';
import { PuterServer } from '../../../../../server.js';
import { setupTestServer } from '../../../../../testUtil.js';
import { withTestActor } from '../../../shared/integrationTestUtil.js';
import { AIChatStream } from '../../../interfaces/chat/utils/Streaming.js';
import { usdPerMToken } from '../../../interfaces/chat/utils/pricing.js';
import type { Actor } from '../../../../core/actor.js';
import { SYSTEM_ACTOR, makeActor } from '../../../../core/actor.js';
import type { MeteringService } from '../../../../services/metering/MeteringService.js';
import { PuterServer } from '../../../../server.js';
import { setupTestServer } from '../../../../testUtil.js';
import { withTestActor } from '../../../integrationTestUtil.js';
import { AIChatStream } from '../../utils/Streaming.js';
import { usdPerMToken } from '../../utils/pricing.js';
import { MetaProvider } from './MetaProvider.js';
import { META_MODELS } from './models.js';
import { modelLookupNames } from '../../../interfaces/chat/utils/modelRouting.js';
import { modelLookupNames } from '../../utils/modelRouting.js';
// -- OpenAI SDK mock ----------------------------------------------
//
@@ -18,20 +18,17 @@
*/
import { OpenAI } from 'openai';
import type { Actor } from '../../../../../core/actor.js';
import { HttpError } from '../../../../../core/http/HttpError.js';
import type { MeteringService } from '../../../../../services/metering/MeteringService.js';
import { upstreamUserIdentifier } from '../../../shared/upstreamIdentifier.js';
import type {
IChatModel,
ICompleteArguments,
} from '../../../interfaces/chat/types.js';
import { promptCacheRetentionToWire } from '../../../protocols/openaiCompatible/openaiParams.js';
import { sdkClientOptions } from '../../../interfaces/chat/utils/sdkClient.js';
import type { Actor } from '../../../../core/actor.js';
import { HttpError } from '../../../../core/http/HttpError.js';
import type { MeteringService } from '../../../../services/metering/MeteringService.js';
import { upstreamUserIdentifier } from '../../../util/upstreamIdentifier.js';
import type { IChatModel, ICompleteArguments } from '../../types.js';
import { promptCacheRetentionToWire } from '../../utils/openaiParams.js';
import { sdkClientOptions } from '../../utils/sdkClient.js';
import {
type ChatProviderConfig,
OpenAICompatProvider,
} from '../../../protocols/openaiCompatible/OpenAICompatProvider.js';
} from '../OpenAICompatProvider.js';
import { META_MODELS, MUSE_SPARK_DEFAULT_MODEL } from './models.js';
const DEFAULT_API_BASE_URL = 'https://api.meta.ai/v1';
@@ -17,8 +17,8 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import type { IChatModel } from '../../../interfaces/chat/types.js';
import { usdPerMToken } from '../../../interfaces/chat/utils/pricing.js';
import type { IChatModel } from '../../types.js';
import { usdPerMToken } from '../../utils/pricing.js';
export const MUSE_SPARK_DEFAULT_MODEL = 'muse-spark-1.2';
@@ -29,14 +29,14 @@ import {
type MockInstance,
} from 'vitest';
import { SYSTEM_ACTOR } from '../../../../../core/actor.js';
import type { MeteringService } from '../../../../../services/metering/MeteringService.js';
import { PuterServer } from '../../../../../server.js';
import { setupTestServer } from '../../../../../testUtil.js';
import { withTestActor } from '../../../shared/integrationTestUtil.js';
import { SYSTEM_ACTOR } from '../../../../core/actor.js';
import type { MeteringService } from '../../../../services/metering/MeteringService.js';
import { PuterServer } from '../../../../server.js';
import { setupTestServer } from '../../../../testUtil.js';
import { withTestActor } from '../../../integrationTestUtil.js';
import { MINIMAX_MODELS } from './models.js';
import { MiniMaxProvider } from './MiniMaxProvider.js';
import { modelLookupNames } from '../../../interfaces/chat/utils/modelRouting.js';
import { modelLookupNames } from '../../utils/modelRouting.js';
const { createMock, openAICtor } = vi.hoisted(() => {
const createMock = vi.fn();
@@ -18,16 +18,13 @@
*/
import { OpenAI } from 'openai';
import type { MeteringService } from '../../../../../services/metering/MeteringService.js';
import type {
IChatModel,
ICompleteArguments,
} from '../../../interfaces/chat/types.js';
import { sdkClientOptions } from '../../../interfaces/chat/utils/sdkClient.js';
import type { MeteringService } from '../../../../services/metering/MeteringService.js';
import type { IChatModel, ICompleteArguments } from '../../types.js';
import { sdkClientOptions } from '../../utils/sdkClient.js';
import {
type ChatProviderConfig,
OpenAICompatProvider,
} from '../../../protocols/openaiCompatible/OpenAICompatProvider.js';
} from '../OpenAICompatProvider.js';
import { MINIMAX_MODELS } from './models.js';
export class MiniMaxProvider extends OpenAICompatProvider {
@@ -17,7 +17,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import type { IChatModel } from '../../../interfaces/chat/types.js';
import type { IChatModel } from '../../types.js';
type MiniMaxChatModel = IChatModel & {
apiModel: string;
@@ -27,9 +27,10 @@ import { describe, expect, it } from 'vitest';
import {
INTEGRATION_TEST_TIMEOUT_MS,
makeMeteringStub,
optionalEnv,
skipUnlessEnv,
withTestActor,
} from '../../../shared/integrationTestUtil.js';
import { optionalEnv, skipUnlessEnv } from '../../../../../testEnv.js';
} from '../../../integrationTestUtil.js';
import { MistralAIProvider } from './MistralAiProvider.js';
const ENV_VAR = 'PUTER_TEST_AI_MISTRAL_API_KEY';
@@ -28,7 +28,7 @@
* (MistralAiProvider.integration.test.ts) exercises the real Mistral endpoint.
*/
import { Context } from '../../../../../core/context.js';
import { Context } from '../../../../core/context.js';
import { Writable } from 'node:stream';
import {
afterAll,
@@ -45,15 +45,15 @@ import {
import { ResponseValidationError } from '@mistralai/mistralai/models/errors/responsevalidationerror.js';
import { SDKValidationError } from '@mistralai/mistralai/models/errors/sdkvalidationerror.js';
import { UsageInfo$inboundSchema } from '@mistralai/mistralai/models/components/usageinfo.js';
import { SYSTEM_ACTOR } from '../../../../../core/actor.js';
import type { MeteringService } from '../../../../../services/metering/MeteringService.js';
import { PuterServer } from '../../../../../server.js';
import { setupTestServer } from '../../../../../testUtil.js';
import { withTestActor } from '../../../shared/integrationTestUtil.js';
import { AIChatStream } from '../../../interfaces/chat/utils/Streaming.js';
import { SYSTEM_ACTOR } from '../../../../core/actor.js';
import type { MeteringService } from '../../../../services/metering/MeteringService.js';
import { PuterServer } from '../../../../server.js';
import { setupTestServer } from '../../../../testUtil.js';
import { withTestActor } from '../../../integrationTestUtil.js';
import { AIChatStream } from '../../utils/Streaming.js';
import { MISTRAL_MODELS } from './models.js';
import { MistralAIProvider } from './MistralAiProvider.js';
import { modelLookupNames } from '../../../interfaces/chat/utils/modelRouting.js';
import { modelLookupNames } from '../../utils/modelRouting.js';
// ── Mistral SDK mock ────────────────────────────────────────────────
//
@@ -83,10 +83,7 @@ let server: PuterServer;
let recordSpy: MockInstance<MeteringService['utilRecordUsageObject']>;
beforeAll(async () => {
// The key puts Mistral on the server's own chat driver.
server = await setupTestServer({
providers: { mistral: { apiKey: 'test-key' } },
});
server = await setupTestServer();
});
afterAll(async () => {
@@ -190,31 +187,11 @@ describe('MistralAIProvider model catalog', () => {
expect(ids).not.toContain('magistral-medium-latest');
});
it.each(['mistral-large-2512', 'mistral-medium-2604'])(
'keeps %s, which Mistral serves until it retires the model',
async (id) => {
const { provider } = makeProvider();
expect((await provider.models()).map((model) => model.id)).toContain(
id,
);
},
);
it('moves the floating Large aliases off the deprecated Large 3', async () => {
it('keeps mistral-large-2512, which Mistral still serves as active', async () => {
const { provider } = makeProvider();
const models = await provider.models();
const aliasesOf = (id: string) =>
models.find((model) => model.id === id)?.aliases ?? [];
expect(aliasesOf('mistral-large-4')).toEqual(
expect.arrayContaining([
'mistral-large-latest',
'mistral-large',
'mistralai/mistral-large-latest',
]),
);
expect(aliasesOf('mistral-large-2512')).toEqual([
'mistralai/mistral-large-2512',
]);
const ids = modelLookupNames(await provider.models());
expect(ids).toContain('mistral-large-2512');
expect(ids).toContain('mistral-large-latest');
});
it('lists Large 4 with standard pricing and multimodal tool support', async () => {
@@ -227,47 +204,17 @@ describe('MistralAIProvider model catalog', () => {
modalities: { input: ['text', 'image'], output: ['text'] },
tool_call: true,
context: 1_000_000,
max_tokens: 262_144,
open_weights: true,
max_tokens: 1_000_000,
open_weights: false,
costs_currency: 'usd-cents',
costs: {
tokens: 1_000_000,
prompt_tokens: 70,
cached_tokens: 7,
completion_tokens: 210,
prompt_tokens: 136,
cached_tokens: 14,
completion_tokens: 418,
},
});
});
it.each(['mistral-large-4', 'mistral-large-latest'])(
'asks for no more output from %s than Mistral accepts',
async (model) => {
// Credit for far more output than the context window holds.
vi.spyOn(
server.services.metering,
'getUsageHeadroom',
).mockResolvedValue({ balance: 1e12, held: 0 });
completeMock.mockResolvedValueOnce({
choices: [
{
message: { content: 'hi', role: 'assistant' },
finishReason: 'stop',
},
],
usage: { promptTokens: 1, completionTokens: 1 },
});
await withTestActor(() =>
server.drivers.aiChat.complete({
model,
provider: 'mistral',
messages: [{ role: 'user', content: 'hi' }],
}),
);
expect(completeMock.mock.calls[0]![0].maxTokens).toBe(262_144);
},
);
});
// ── Request shape (Mistral-specific quirks) ─────────────────────────
@@ -592,11 +539,7 @@ describe('MistralAIProvider model resolution', () => {
['mistral-large-4-0', 'mistral-large-4'],
['mistralai/mistral-large-4', 'mistral-large-4'],
['mistralai/mistral-large-4-0', 'mistral-large-4'],
['mistral-large-latest', 'mistral-large-4'],
['mistral-large', 'mistral-large-4'],
['mistralai/mistral-large-latest', 'mistral-large-4'],
['mistral-large-2512', 'mistral-large-2512'],
['mistralai/mistral-large-2512', 'mistral-large-2512'],
['mistral-large-latest', 'mistral-large-2512'],
['zai-glm-latest', 'zai-glm-5-3'],
['zai-glm-5', 'zai-glm-5-3'],
['mistral-code-latest', 'codestral-2508'],
@@ -645,8 +588,8 @@ describe('MistralAIProvider model resolution', () => {
describe('MistralAIProvider.complete non-stream output', () => {
it.each([
['mistral-large-4', false, 70, 7, 210],
['mistral-large-4', true, 70, 7, 210],
['mistral-large-4', false, 136, 14, 418],
['mistral-large-4', true, 136, 14, 418],
['mistral-small-2603', false, 15, 1.5, 60],
['mistral-small-2603', true, 15, 1.5, 60],
['ministral-8b-2512', false, 15, 1.5, 15],
@@ -18,29 +18,30 @@
*/
import { HTTPClient } from '@mistralai/mistralai/lib/http.js';
import { servedServiceSpeed } from '../../../interfaces/chat/utils/serviceSpeed.js';
import { servedServiceSpeed } from '../../utils/serviceSpeed.js';
import { Mistral } from '@mistralai/mistralai';
import { ChatCompletionResponse } from '@mistralai/mistralai/models/components/chatcompletionresponse.js';
import { MistralError } from '@mistralai/mistralai/models/errors/mistralerror.js';
import { SDKValidationError } from '@mistralai/mistralai/models/errors/sdkvalidationerror.js';
import type { UsageInfo } from '@mistralai/mistralai/models/components/usageinfo.js';
import { Context } from '../../../../../core/context.js';
import { HttpError } from '../../../../../core/http/HttpError.js';
import type { MeteringService } from '../../../../../services/metering/MeteringService.js';
import { Context } from '../../../../core/context.js';
import { HttpError } from '../../../../core/http/HttpError.js';
import type { MeteringService } from '../../../../services/metering/MeteringService.js';
import type {
IChatModel,
IChatCompleteResult,
IChatProvider,
ICompleteArguments,
} from '../../../interfaces/chat/types.js';
} from '../../types.js';
import {
mediaUrlOf,
unsupportedMediaTextPart,
} from '../../../interfaces/chat/utils/mediaParts.js';
import * as OpenAIUtil from '../../../protocols/openaiCompatible/OpenAIUtil.js';
import { isMistralInputError } from '../inputError.js';
} from '../../utils/mediaParts.js';
import * as OpenAIUtil from '../../utils/OpenAIUtil.js';
import { MISTRAL_MODELS } from './models.js';
import { shouldPresentAsOpenAI } from '../../../interfaces/chat/utils/normalizeToOpenAI.js';
import { withSdkTimeout } from '../../../interfaces/chat/utils/sdkClient.js';
import { meterChatUsage } from '../../../interfaces/chat/utils/meterChatUsage.js';
import { shouldPresentAsOpenAI } from '../../utils/normalizeToOpenAI.js';
import { withSdkTimeout } from '../../utils/sdkClient.js';
import { meterChatUsage } from '../../utils/meterChatUsage.js';
/**
* Mistral's reasoning-capable models return `content` as a chunk array rather
@@ -279,8 +280,14 @@ export class MistralAIProvider implements IChatProvider {
},
);
} catch (e) {
// Without a status the driver would count this as a route failure.
if (isMistralInputError(e)) {
// The SDK validates input client-side and throws without a status,
// which the driver would count as a route failure. Its `instanceof`
// also matches ResponseValidationError (an unparseable upstream
// response), which is a MistralError and not the caller's fault.
if (
e instanceof SDKValidationError &&
!(e instanceof MistralError)
) {
throw new HttpError(400, 'invalid request for Mistral models', {
legacyCode: 'bad_request',
});
@@ -17,7 +17,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import type { IChatModel } from '../../../interfaces/chat/types.js';
import type { IChatModel } from '../../types.js';
// Standard rates: https://docs.mistral.ai/inference/pricing
export const MISTRAL_MODELS: IChatModel[] = [
@@ -25,23 +25,19 @@ export const MISTRAL_MODELS: IChatModel[] = [
puterId: 'mistralai:mistralai/mistral-large-4',
id: 'mistral-large-4',
modalities: { input: ['text', 'image'], output: ['text'] },
open_weights: true,
open_weights: false,
tool_call: true,
release_date: '2026-10-06',
name: 'Mistral Large 4',
aliases: [
'mistral-large-4-0',
'mistral-large-latest',
'mistral-large',
'mistralai/mistral-large-4',
'mistralai/mistral-large-4-0',
'mistralai/mistral-large-latest',
],
context: 1_000_000,
// Mistral rejects a request for more completion tokens than this.
max_tokens: 262_144,
max_tokens: 1_000_000,
description:
'State-of-the-art, open-weight, general-purpose multimodal model for reasoning, coding, and agentic workflows.',
'Public-preview multimodal model for reasoning, coding, and agentic workflows; open weights are forthcoming.',
provider: 'mistral',
costs_currency: 'usd-cents',
input_cost_key: 'prompt_tokens',
@@ -49,13 +45,12 @@ export const MISTRAL_MODELS: IChatModel[] = [
serviceSpeedRates: { fast: 1.75 },
costs: {
tokens: 1_000_000,
prompt_tokens: 70,
cached_tokens: 7,
completion_tokens: 210,
prompt_tokens: 136,
cached_tokens: 14,
completion_tokens: 418,
},
},
{
// Mistral deprecated this model on 2026-10-31 and retires it on 2027-04-30.
puterId: 'mistralai:mistralai/mistral-medium-2604',
id: 'mistral-medium-2604',
modalities: { input: ['text', 'image'], output: ['text'] },
@@ -90,7 +85,6 @@ export const MISTRAL_MODELS: IChatModel[] = [
},
},
{
// Mistral deprecated this model on 2026-10-31 and retires it on 2027-04-30.
puterId: 'mistralai:mistralai/mistral-large-2512',
id: 'mistral-large-2512',
modalities: { input: ['text', 'image'], output: ['text'] },
@@ -99,7 +93,12 @@ export const MISTRAL_MODELS: IChatModel[] = [
knowledge: '2025-12',
release_date: '2025-12-02',
name: 'Mistral Large 3',
aliases: ['mistralai/mistral-large-2512'],
aliases: [
'mistral-large-latest',
'mistral-large',
'mistralai/mistral-large-latest',
'mistralai/mistral-large-2512',
],
context: 262_144,
max_tokens: 262_144,
description:
@@ -47,29 +47,29 @@
* on that test.)
*/
import { existsSync, readdirSync } from 'node:fs';
import { readdirSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { describe, expect, it } from 'vitest';
import type { IChatModel } from './types.js';
import { ALIBABA_MODELS } from '../../providers/alibaba/chat/models.js';
import { AZURE_MODELS } from '../../providers/azure/chat/models.js';
import { BYTEPLUS_MODELS } from '../../providers/byteplus/chat/models.js';
import { CLAUDE_MODELS } from '../../providers/claude/chat/models.js';
import { DEEPSEEK_MODELS } from '../../providers/deepseek/chat/models.js';
import { GEMINI_MODELS } from '../../providers/gemini/chat/models.js';
import { GROQ_MODELS } from '../../providers/groq/chat/models.js';
import { HOONIFY_MODELS } from '../../providers/hoonify/chat/models.js';
import { META_MODELS } from '../../providers/meta/chat/models.js';
import { MINIMAX_MODELS } from '../../providers/minimax/chat/models.js';
import { MISTRAL_MODELS } from '../../providers/mistral/chat/models.js';
import { MOONSHOT_MODELS } from '../../providers/moonshot/chat/models.js';
import { OPEN_AI_MODELS } from '../../providers/openai/chat/models.js';
import { OPEN_ROUTER_MODEL_OVERRIDES } from '../../providers/openrouter/chat/modelOverrides.js';
import { XAI_MODELS } from '../../providers/xai/chat/models.js';
import { ZAI_MODELS } from '../../providers/zai/chat/models.js';
import type { IChatModel } from '../types.js';
import { ALIBABA_MODELS } from './alibaba/models.js';
import { AZURE_MODELS } from './azure/models.js';
import { BYTEPLUS_MODELS } from './byteplus/models.js';
import { CLAUDE_MODELS } from './claude/models.js';
import { DEEPSEEK_MODELS } from './deepseek/models.js';
import { GEMINI_MODELS } from './gemini/models.js';
import { GROQ_MODELS } from './groq/models.js';
import { HOONIFY_MODELS } from './hoonify/models.js';
import { META_MODELS } from './meta/models.js';
import { MINIMAX_MODELS } from './minimax/models.js';
import { MISTRAL_MODELS } from './mistral/models.js';
import { MOONSHOT_MODELS } from './moonshot/models.js';
import { OPEN_AI_MODELS } from './openai/models.js';
import { OPEN_ROUTER_MODEL_OVERRIDES } from './openrouter/modelOverrides.js';
import { XAI_MODELS } from './xai/models.js';
import { ZAI_MODELS } from './zai/models.js';
// Providers whose catalog is fetched at runtime (OpenRouter, Ollama, Together,
// Infron, Neuralwatt) have nothing static to check and are absent by design;
@@ -201,34 +201,28 @@ describe('CATALOGS', () => {
// silent gap these tests are about, so the list is checked against what
// is actually on disk.
//
// The scan covers every non-test source file under providers/*/chat/
// with "model" in its name — models.ts, but also siblings like
// openrouter's modelOverrides.ts. Importing every provider file
// regardless of name would drag in SDK modules for a filename sweep, so
// that naming convention is the one assumption left unenforced here: a
// catalog in a file named without "model" would escape this net.
it('lists every static catalog under providers/*/chat/', async () => {
const providersDir = join(
dirname(fileURLToPath(import.meta.url)),
'../../providers',
);
// The scan covers every non-test source file under providers/*/ with
// "model" in its name — models.ts, but also siblings like openrouter's
// modelOverrides.ts. Importing every provider file regardless of name
// would drag in SDK modules for a filename sweep, so that naming
// convention is the one assumption left unenforced here: a catalog in a
// file named without "model" would escape this net.
it('lists every static catalog under providers/', async () => {
const here = dirname(fileURLToPath(import.meta.url));
const registered = new Map(CATALOGS);
const found = new Set<string>();
const problems: string[] = [];
for (const dir of readdirSync(providersDir, { withFileTypes: true })) {
for (const dir of readdirSync(here, { withFileTypes: true })) {
if (!dir.isDirectory()) continue;
const chatDir = join(providersDir, dir.name, 'chat');
if (!existsSync(chatDir)) continue;
for (const file of readdirSync(chatDir)) {
for (const file of readdirSync(join(here, dir.name))) {
if (!/model/i.test(file)) continue;
if (!file.endsWith('.ts') || file.endsWith('.test.ts')) {
continue;
}
const module = await import(
pathToFileURL(join(chatDir, file)).href
pathToFileURL(join(here, dir.name, file)).href
);
for (const [name, value] of Object.entries(module)) {
// A catalog is a non-empty array of entries carrying an
@@ -239,19 +233,16 @@ describe('CATALOGS', () => {
value.length > 0 &&
typeof value[0]?.id === 'string';
if (!isCatalog) continue;
found.add(name);
if (!registered.has(name)) {
problems.push(
`${dir.name}/chat/${file} exports ${name}`,
);
problems.push(`${dir.name}/${file} exports ${name}`);
} else if (registered.get(name) !== value) {
// The row's label names this export but its value is
// a different array — the wrong catalog would be the
// one getting checked.
problems.push(
`the CATALOGS row named ${name} does not hold ` +
`the ${name} that ${dir.name}/chat/${file} exports`,
`the ${name} that ${dir.name}/${file} exports`,
);
}
}
@@ -259,7 +250,5 @@ describe('CATALOGS', () => {
}
expect(problems, problems.join('\n')).toEqual([]);
// A scan pointed at the wrong directory finds nothing and passes.
expect([...found].sort()).toEqual([...registered.keys()].sort());
});
});
@@ -28,9 +28,10 @@ import { describe, expect, it } from 'vitest';
import {
INTEGRATION_TEST_TIMEOUT_MS,
makeMeteringStub,
optionalEnv,
skipUnlessEnv,
withTestActor,
} from '../../../shared/integrationTestUtil.js';
import { optionalEnv, skipUnlessEnv } from '../../../../../testEnv.js';
} from '../../../integrationTestUtil.js';
import { MoonshotProvider } from './MoonshotProvider.js';
const ENV_VAR = 'PUTER_TEST_AI_MOONSHOT_API_KEY';
@@ -25,7 +25,7 @@
* so the recording side is exercised end-to- end. Moonshot is OpenAI-compatible
* so the OpenAI SDK is mocked at the module boundary; that's the real network
* egress point. Image- inlining behaviour is covered by
* `protocols/openaiCompatible/inlineImages.test.ts`; here we stub `inlineHttpImageUrls` so http URLs
* `utils/inlineImages.test.ts`; here we stub `inlineHttpImageUrls` so http URLs
* in vision messages don't trigger network fetches and only verify the provider
* invokes it for vision-capable models. The companion integration test
* (MoonshotProvider.integration.test.ts) exercises the real Moonshot endpoint.
@@ -44,24 +44,24 @@ import {
type MockInstance,
} from 'vitest';
import { ChatCompletionDriver } from '../../../interfaces/chat/ChatCompletionDriver.js';
import { clearUnhealthyRoutes } from '../../../interfaces/chat/utils/providerHealth.js';
import { SYSTEM_ACTOR } from '../../../../../core/actor.js';
import type { MeteringService } from '../../../../../services/metering/MeteringService.js';
import { PuterServer } from '../../../../../server.js';
import { setupTestServer } from '../../../../../testUtil.js';
import { ChatCompletionDriver } from '../../ChatCompletionDriver.js';
import { clearUnhealthyRoutes } from '../../utils/providerHealth.js';
import { SYSTEM_ACTOR } from '../../../../core/actor.js';
import type { MeteringService } from '../../../../services/metering/MeteringService.js';
import { PuterServer } from '../../../../server.js';
import { setupTestServer } from '../../../../testUtil.js';
import {
readStreamResult,
withTestActor,
} from '../../../shared/integrationTestUtil.js';
import { AIChatStream } from '../../../interfaces/chat/utils/Streaming.js';
} from '../../../integrationTestUtil.js';
import { AIChatStream } from '../../utils/Streaming.js';
import { MOONSHOT_MODELS } from './models.js';
import { MoonshotProvider } from './MoonshotProvider.js';
import {
estimatePromptTokens,
estimateTextTokens,
} from '../../../interfaces/chat/utils/usageEstimate.js';
import { modelLookupNames } from '../../../interfaces/chat/utils/modelRouting.js';
} from '../../utils/usageEstimate.js';
import { modelLookupNames } from '../../utils/modelRouting.js';
// ── OpenAI SDK mock ─────────────────────────────────────────────────
@@ -93,7 +93,7 @@ const { inlineHttpImageUrlsMock } = vi.hoisted(() => ({
inlineHttpImageUrlsMock: vi.fn(async (_messages: unknown) => {}),
}));
vi.mock('../../../protocols/openaiCompatible/inlineImages.js', () => ({
vi.mock('../../utils/inlineImages.js', () => ({
inlineHttpImageUrls: inlineHttpImageUrlsMock,
}));
@@ -18,20 +18,20 @@
*/
import { OpenAI } from 'openai';
import { HttpError } from '../../../../../core/http/HttpError.js';
import type { MeteringService } from '../../../../../services/metering/MeteringService.js';
import { HttpError } from '../../../../core/http/HttpError.js';
import type { MeteringService } from '../../../../services/metering/MeteringService.js';
import type {
IChatModel,
ICompleteArguments,
PuterMessage,
} from '../../../interfaces/chat/types.js';
import { openAICompatParams } from '../../../protocols/openaiCompatible/openaiParams.js';
import { sdkClientOptions } from '../../../interfaces/chat/utils/sdkClient.js';
import { estimateTextTokens } from '../../../interfaces/chat/utils/usageEstimate.js';
} from '../../types.js';
import { openAICompatParams } from '../../utils/openaiParams.js';
import { sdkClientOptions } from '../../utils/sdkClient.js';
import { estimateTextTokens } from '../../utils/usageEstimate.js';
import {
type ChatProviderConfig,
OpenAICompatProvider,
} from '../../../protocols/openaiCompatible/OpenAICompatProvider.js';
} from '../OpenAICompatProvider.js';
import { MOONSHOT_MODELS } from './models.js';
/**
@@ -17,7 +17,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import type { IChatModel } from '../../../interfaces/chat/types.js';
import type { IChatModel } from '../../types.js';
export const MOONSHOT_MODELS: IChatModel[] = [
// -- Kimi K3.1 Pro ------------------------------------------------
@@ -38,18 +38,18 @@ import {
type MockInstance,
} from 'vitest';
import type { MeteringService } from '../../../../../services/metering/MeteringService.js';
import { PuterServer } from '../../../../../server.js';
import { setupTestServer } from '../../../../../testUtil.js';
import { withTestActor } from '../../../shared/integrationTestUtil.js';
import { AIChatStream } from '../../../interfaces/chat/utils/Streaming.js';
import type { MeteringService } from '../../../../services/metering/MeteringService.js';
import { PuterServer } from '../../../../server.js';
import { setupTestServer } from '../../../../testUtil.js';
import { withTestActor } from '../../../integrationTestUtil.js';
import { AIChatStream } from '../../utils/Streaming.js';
import {
mapNeuralwattApiModel,
NEURALWATT_DEFAULT_MODEL,
stripNeuralwattPrefix,
} from './models.js';
import { NeuralwattProvider } from './NeuralwattProvider.js';
import { modelLookupNames } from '../../../interfaces/chat/utils/modelRouting.js';
import { modelLookupNames } from '../../utils/modelRouting.js';
// ── OpenAI SDK mock ─────────────────────────────────────────────────
@@ -19,25 +19,22 @@
import axios from 'axios';
import { OpenAI } from 'openai';
import { HttpError } from '../../../../../core/http/HttpError.js';
import type { MeteringService } from '../../../../../services/metering/MeteringService.js';
import type {
IChatModel,
ICompleteArguments,
} from '../../../interfaces/chat/types.js';
import { cachedRemoteCatalog } from '../../../shared/cachedRemoteCatalog.js';
import { HttpError } from '../../../../core/http/HttpError.js';
import type { MeteringService } from '../../../../services/metering/MeteringService.js';
import type { IChatModel, ICompleteArguments } from '../../types.js';
import { cachedRemoteCatalog } from '../../utils/cachedRemoteCatalog.js';
import {
messagesHaveImageContent,
modelSupportsVision,
} from '../../../interfaces/chat/utils/mediaParts.js';
import * as OpenAIUtil from '../../../protocols/openaiCompatible/OpenAIUtil.js';
import { catalogPrice } from '../../../shared/catalogPrice.js';
import { sdkClientOptions } from '../../../interfaces/chat/utils/sdkClient.js';
} from '../../utils/mediaParts.js';
import * as OpenAIUtil from '../../utils/OpenAIUtil.js';
import { catalogPrice } from '../../utils/pricing.js';
import { sdkClientOptions } from '../../utils/sdkClient.js';
import {
type ChatProviderConfig,
OpenAICompatProvider,
type UsageSource,
} from '../../../protocols/openaiCompatible/OpenAICompatProvider.js';
} from '../OpenAICompatProvider.js';
import {
mapNeuralwattApiModel,
NEURALWATT_DEFAULT_MODEL,
Loaded 100 of 392 files, more files were not shown because too many files have changed in this diff. Show more