mirror of
https://github.com/HeyPuter/puter.git
synced 2026-11-13 20:12:57 -10:00
Compare commits
58
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9675ddee7b | ||
|
|
6330a069be | ||
|
|
3869ab646d | ||
|
|
23afd07db8 | ||
|
|
01c1b45cd0 | ||
|
|
df1d017958 | ||
|
|
faea082180 | ||
|
|
efd5098125 | ||
|
|
b2646930b3 | ||
|
|
c6987d9606 | ||
|
|
796b42c74d | ||
|
|
e2043a5e58 | ||
|
|
8b98c7ada8 | ||
|
|
b27e84d181 | ||
|
|
add0e7be7d | ||
|
|
b5d221ede6 | ||
|
|
3137a631a7 | ||
|
|
57706d8093 | ||
|
|
4f36152850 | ||
|
|
86db96d587 | ||
|
|
214ffb6605 | ||
|
|
871cc2cf87 | ||
|
|
ba002be0f7 | ||
|
|
2832926f8d | ||
|
|
1ab1c04760 | ||
|
|
63a98f89ff | ||
|
|
d2cef27085 | ||
|
|
74b60c1227 | ||
|
|
2ed3e78dfa | ||
|
|
22ebd353b8 | ||
|
|
7551e12370 | ||
|
|
6e29e32fe9 | ||
|
|
54bfc0ffe5 | ||
|
|
c27883134b | ||
|
|
cf4c1f6b64 | ||
|
|
305d20ab1c | ||
|
|
0ce6b0646a | ||
|
|
6b5c4f3d9e | ||
|
|
d6fdc5f268 | ||
|
|
4f3f946941 | ||
|
|
4e36672b6b | ||
|
|
6b0b530ab1 | ||
|
|
68b8f341e9 | ||
|
|
c3756d3ac3 | ||
|
|
0c927ac5a4 | ||
|
|
0128e8779d | ||
|
|
89f070f7d0 | ||
|
|
3b3c3750b0 | ||
|
|
26f7e8eb2e | ||
|
|
62ce691ac9 | ||
|
|
654cee36c3 | ||
|
|
c328283fa7 | ||
|
|
fa6e76254b | ||
|
|
ed32c212d3 | ||
|
|
a338c8db46 | ||
|
|
ffa6b176d0 | ||
|
|
f1008559ca | ||
|
|
4ee1360779 |
No files matched your search
@@ -0,0 +1,96 @@
|
||||
# Embedded app browser
|
||||
|
||||
`GET /embed/apps` on the deployment's main GUI origin renders the same app browser
|
||||
as the dashboard's Apps tab, without the desktop or dashboard navigation.
|
||||
|
||||
It supports search, paging, saved folders, and saved ordering. It uses the same
|
||||
installed-app and recommendation data as the dashboard, including the user's
|
||||
removed recommendations. Editing, adding, reordering, renaming, and uninstalling
|
||||
are disabled. Selecting an app opens a new browser tab with no opener; the host
|
||||
page stays open. Normal apps use `/app/<name>` on the GUI origin, and website
|
||||
shortcuts use their external destination.
|
||||
|
||||
## Session and framing
|
||||
|
||||
Only show the iframe after the user has signed in. The embed reads the existing
|
||||
GUI session from local storage on its own origin. It does not create accounts,
|
||||
show a login dialog, or accept a token from its parent. A missing session or
|
||||
failed load displays an unavailable/error state. Being signed in on a subdomain
|
||||
alone does not populate the main GUI origin's storage; the main GUI session must
|
||||
already exist.
|
||||
|
||||
The route's `Content-Security-Policy: frame-ancestors` allows the GUI origin and
|
||||
the embedding page's origin when it is a subdomain of `config.domain` with the
|
||||
scheme and port from `config.origin`, and not under one of the user-content
|
||||
hosting domains (`static_hosting_domain`, `private_app_hosting_domain`, and their
|
||||
`_alt` variants). For example, `https://puter.com` permits
|
||||
`https://spreadsheet.puter.com`, and `http://puter.localhost:4100` permits
|
||||
`http://spreadsheet.puter.localhost:4100` but not
|
||||
`http://someone.site.puter.localhost:4100`. The embedding page is read from the
|
||||
iframe request's referrer, so the host must not suppress it (the browser default
|
||||
`strict-origin-when-cross-origin` is enough). All ancestors in a nested iframe
|
||||
chain must be allowed. Other GUI routes retain `X-Frame-Options: SAMEORIGIN`.
|
||||
|
||||
The embed accepts no query parameters. Requests with a query string redirect to
|
||||
the same embed path so SDK bootstrap parameters cannot override the session or API
|
||||
origin. The host needs to allow the GUI origin in its own `frame-src` CSP if it
|
||||
sets one. If sandboxing the iframe, allow scripts, same-origin storage, popups,
|
||||
and popups to escape the sandbox.
|
||||
|
||||
## Host integration
|
||||
|
||||
Create the iframe when “More apps” is opened, and remove it on close so reopening
|
||||
loads current apps and preferences. The host owns the modal and close control;
|
||||
there is no parent messaging API and no app list or credential is sent to it.
|
||||
The app browser fills its iframe and responds to its size.
|
||||
|
||||
This example assumes `puterOrigin` comes from the host app's deployment settings
|
||||
and the caller has already established the GUI session. Translate the button and
|
||||
frame labels using the host app's localization system.
|
||||
|
||||
```html
|
||||
<button id="more-apps" type="button">More apps</button>
|
||||
<dialog id="apps-dialog" aria-label="More apps">
|
||||
<button id="close-apps" type="button">Close</button>
|
||||
<div id="apps-frame"></div>
|
||||
</dialog>
|
||||
<style>
|
||||
#apps-dialog { padding: 12px; width: min(900px, 90vw); }
|
||||
#apps-frame iframe { width: 100%; height: min(650px, 75vh); border: 0; }
|
||||
</style>
|
||||
<script type="module">
|
||||
const puterOrigin = 'https://puter.com'; // Use this deployment's configured origin.
|
||||
const dialog = document.querySelector('#apps-dialog');
|
||||
const container = document.querySelector('#apps-frame');
|
||||
const trigger = document.querySelector('#more-apps');
|
||||
|
||||
trigger.addEventListener('click', () => {
|
||||
const frame = document.createElement('iframe');
|
||||
frame.title = 'Your apps';
|
||||
frame.src = new URL('/embed/apps', puterOrigin).href;
|
||||
container.replaceChildren(frame);
|
||||
dialog.showModal();
|
||||
});
|
||||
document.querySelector('#close-apps').addEventListener('click', () => dialog.close());
|
||||
dialog.addEventListener('close', () => {
|
||||
container.replaceChildren();
|
||||
trigger.focus();
|
||||
});
|
||||
</script>
|
||||
```
|
||||
|
||||
## Implementation and checks
|
||||
|
||||
`createAppBrowser` in `src/gui/src/UI/Dashboard/AppBrowser.js` owns the shared UI
|
||||
and data behavior. `TabApps.js` injects desktop actions. `appsEmbed.js` supplies
|
||||
new-tab launching and loads only the embed entry, its SDK, and shared styles.
|
||||
The normal GUI webpack build emits `bundle.min.js` and `apps-embed.min.js`.
|
||||
No puter.js API or app-list endpoint changes are needed.
|
||||
|
||||
Run the GUI component/browser tests (with the SDK bundle built and Playwright
|
||||
Chromium installed), and the backend route tests:
|
||||
|
||||
```sh
|
||||
npx vitest run --config src/gui/vitest.config.js src/gui/src/UI/Dashboard/AppBrowser
|
||||
npx vitest run --config src/backend/vitest.config.ts src/backend/server.test.ts
|
||||
```
|
||||
+7
-6
@@ -1,6 +1,6 @@
|
||||
# Browser push client
|
||||
|
||||
Puter's desktop shows pushes through `/sw.js`. Core registers it, and shows Settings → Notifications, only when the `pushEnabled` gui param is `true`; core never sets it. A backend extension that delivers pushes sets it with `services.homepage.setGuiParam('pushEnabled', true)` and can load its own code into the worker with `services.homepage.registerServiceWorkerScript(path)`.
|
||||
Puter's desktop shows pushes through `/sw.js`. Core shows Settings → Notifications only when the `pushEnabled` gui param is `true`; core never sets it. The worker is registered only when push is turned on in the browser, and a load removes one left without a subscription. A backend extension that delivers pushes sets it with `services.homepage.setGuiParam('pushEnabled', true)` and can load its own code into the worker with `services.homepage.registerServiceWorkerScript(path)`.
|
||||
|
||||
Core subscribes the browser to a push service only when the server also publishes `pushWebPushKey` (see [Built-in Web Push client](#built-in-web-push-client)), and it has no `/push/*` routes of its own. The device, app, preference and subscription endpoints below come from that extension. On a deployment without one, such as most self-hosted servers, they answer core's plain 404 (`code: 'not_found'`), which `puter.notifications` reports as `push_unavailable` and the Notifications tab shows as unavailable. A device the extension can't find is `device_not_found`.
|
||||
|
||||
@@ -22,12 +22,13 @@ When the browser replaces the subscription (`pushsubscriptionchange`), `sw.js` p
|
||||
|
||||
Before a notification is pushed, the server asks the open clients whether they received it live. It sends `notif.receipt` with `{ uid }` on the user's sockets for the app that sent it, or on the desktop's sockets for Puter's own notifications. The SDK remembers the uids its page received through `onNotification()`, `onPush()` or the desktop's feed, and answers once its page has that notification, or with `{ received: false }` when it never gets it. The server waits up to 5 seconds (`notificationAckTimeoutMs`).
|
||||
|
||||
An answer carries `visibilityState`, `focused` and `showsOwn`, the last being whether the page shows the notification itself. A website page shows it itself when it is hidden or unfocused, has an `onPush()` handler and notification permission, and no other tab of the same site is focused. Inside the desktop, the desktop shows it, and an app's `onPush()` result is never shown as a browser notification.
|
||||
An answer carries `visibilityState` and `focused` as they were when the page received the notification. Pages never show a browser notification themselves; pushes do.
|
||||
|
||||
- An answer from a page that is visible and focused, or that shows the notification itself, stops the push.
|
||||
- Otherwise the notification is pushed. A website's pushes go only to the browsers that subscribed through its own worker, never to Puter's `/sw.js`. An app inside the desktop pushes through `/sw.js`, which shows the app's name and icon.
|
||||
- A hidden desktop tab shows no banner, so answers only from hidden desktop tabs don't stop a push of Puter's own notifications.
|
||||
- A client that can't answer, such as an older puter.js, holds the push back while it is connected.
|
||||
- An answer from a page that was visible and focused stops the push (`handled` on `notif.delivery.settled`). An older client that doesn't send `focused` counts while visible.
|
||||
- Otherwise the notification is pushed, to every browser the user subscribed. A website's pushes go only to the browsers that subscribed through its own worker, never to Puter's `/sw.js`. An app inside the desktop pushes through `/sw.js`, which shows the app's name and icon.
|
||||
- A client that can't answer, such as an older puter.js, doesn't hold the push back.
|
||||
|
||||
The ask reaches only the sockets in the asking region. So that other regions hear about a page too, a page that receives a notification live while visible and focused also sends `notif.handled` with `{ uid }` on the socket it came on. Core relays it as the node-local `notif.handled` event with the socket's user and app, at most 60 a minute per user; an extension that delivers pushes decides what to do with it.
|
||||
|
||||
## Registering
|
||||
|
||||
|
||||
@@ -636,6 +636,18 @@ export type EventMap = {
|
||||
/** A peer region holds a socket for the user. */
|
||||
peerRegions: boolean;
|
||||
responders: NotifDeliveryResponder[];
|
||||
/** A page here got it live while visible and focused. */
|
||||
handled: boolean;
|
||||
};
|
||||
/**
|
||||
* A page reported that it got a notification live while visible and
|
||||
* focused. Node-local; `appUid` is the reporting socket's app, absent for
|
||||
* the desktop.
|
||||
*/
|
||||
'notif.handled': {
|
||||
userId: number;
|
||||
appUid?: string;
|
||||
uid: string;
|
||||
};
|
||||
|
||||
// ---- Metering ----
|
||||
@@ -861,6 +873,12 @@ export type EventMap = {
|
||||
/** One access token was revoked; only its own connections should drop. */
|
||||
'auth.access-token.revoked': { token_uid: string };
|
||||
|
||||
/** Same-cluster eviction for raw realtime WebSockets. */
|
||||
'pubsub.realtime.auth-revoked': {
|
||||
sessionUids?: string[];
|
||||
tokenUid?: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* A grant was withdrawn, so whatever was standing on it has to be settled
|
||||
* rather than left to fail its next check.
|
||||
@@ -1061,10 +1079,11 @@ export type EventKey = keyof EventMap & string;
|
||||
// Generates a wildcard for every non-final dot-separated prefix of K.
|
||||
export type WildcardPrefixes<K extends string> =
|
||||
K extends `${infer Head}.${infer Tail}`
|
||||
? | `${Head}.*`
|
||||
| (Tail extends `${string}.${string}`
|
||||
? `${Head}.${WildcardPrefixes<Tail>}`
|
||||
: never)
|
||||
?
|
||||
| `${Head}.*`
|
||||
| (Tail extends `${string}.${string}`
|
||||
? `${Head}.${WildcardPrefixes<Tail>}`
|
||||
: never)
|
||||
: never;
|
||||
|
||||
export type ListenKey = EventKey | WildcardPrefixes<EventKey>;
|
||||
@@ -1075,18 +1094,16 @@ export type MatchingEvents<P extends ListenKey> = P extends `${infer Prefix}.*`
|
||||
|
||||
/** Acked, not acked, no socket anywhere, or sockets only in a peer region. */
|
||||
export type NotifDeliveryOutcome =
|
||||
'received' | 'unreceived' | 'no-socket' | 'remote-only';
|
||||
| 'received'
|
||||
| 'unreceived'
|
||||
| 'no-socket'
|
||||
| 'remote-only';
|
||||
|
||||
/** One socket's receipt for a live notification. */
|
||||
export interface NotifDeliveryResponder {
|
||||
visibilityState: 'visible' | 'hidden' | 'unknown';
|
||||
/** The page's window has focus. Absent from older clients. */
|
||||
focused?: boolean;
|
||||
/**
|
||||
* The page shows its own browser notification while hidden or unfocused.
|
||||
* Absent from older clients.
|
||||
*/
|
||||
showsOwn?: boolean;
|
||||
}
|
||||
|
||||
export type EventMetadata = {
|
||||
|
||||
@@ -22,6 +22,7 @@ import path from 'node:path';
|
||||
import { PuterController } from '../types.js';
|
||||
import { toAppShellView } from '../../util/appShellView.js';
|
||||
import type { PuterRouter } from '../../core/http/PuterRouter';
|
||||
import type { IConfig } from '../../types';
|
||||
import type {
|
||||
PuterHomepageService,
|
||||
PageMeta,
|
||||
@@ -43,9 +44,9 @@ const APP_LANDING_LIMIT = {
|
||||
};
|
||||
|
||||
/**
|
||||
* App descriptions can run long; search snippets and social cards want a
|
||||
* short blurb. Collapses whitespace, then cuts at a word boundary with an
|
||||
* ellipsis so the result never exceeds `APP_META_DESCRIPTION_MAX`.
|
||||
* App descriptions can run long; search snippets and social cards want a short
|
||||
* blurb. Collapses whitespace, then cuts at a word boundary with an ellipsis so
|
||||
* the result never exceeds `APP_META_DESCRIPTION_MAX`.
|
||||
*/
|
||||
export function appMetaDescription(text: string): string {
|
||||
const clean = text.replace(/\s+/g, ' ').trim();
|
||||
@@ -56,6 +57,47 @@ export function appMetaDescription(text: string): string {
|
||||
return `${cut.replace(/[\s,;:.!?…—-]+$/, '')}…`;
|
||||
}
|
||||
|
||||
/**
|
||||
* `frame-ancestors` for the app browser embed. A wildcard over `*.<domain>`
|
||||
* would also admit user-hosted sites whenever the hosting domains sit under
|
||||
* it, so the embedding page is named exactly, from the request's referrer.
|
||||
*/
|
||||
export function embedFrameAncestors(
|
||||
config: Pick<
|
||||
IConfig,
|
||||
| 'origin'
|
||||
| 'domain'
|
||||
| 'static_hosting_domain'
|
||||
| 'static_hosting_domain_alt'
|
||||
| 'private_app_hosting_domain'
|
||||
| 'private_app_hosting_domain_alt'
|
||||
>,
|
||||
referer: string | undefined,
|
||||
): string {
|
||||
const gui = new URL(config.origin ?? `https://${config.domain}`);
|
||||
let parent: URL;
|
||||
try {
|
||||
parent = new URL(referer ?? '');
|
||||
} catch {
|
||||
return "'self'";
|
||||
}
|
||||
const host = parent.hostname.toLowerCase();
|
||||
const hostingDomains = [
|
||||
config.static_hosting_domain,
|
||||
config.static_hosting_domain_alt,
|
||||
config.private_app_hosting_domain,
|
||||
config.private_app_hosting_domain_alt,
|
||||
]
|
||||
.map((d) => (d ?? '').toLowerCase().split(':')[0])
|
||||
.filter(Boolean);
|
||||
const allowed =
|
||||
parent.protocol === gui.protocol &&
|
||||
parent.port === gui.port &&
|
||||
host.endsWith(`.${config.domain.toLowerCase()}`) &&
|
||||
!hostingDomains.some((d) => host === d || host.endsWith(`.${d}`));
|
||||
return allowed ? `'self' ${parent.origin}` : "'self'";
|
||||
}
|
||||
|
||||
/**
|
||||
* Routes that render the Puter GUI shell, plus a catch-all static fallback
|
||||
* under `<gui_assets_root>/src` for non-dist/src paths (images, fonts, lib
|
||||
@@ -105,6 +147,21 @@ export class HomepageController extends PuterController {
|
||||
|
||||
router.get('/', {}, (req, res) => sendShell(req, res));
|
||||
|
||||
router.get('/embed/apps', {}, (req, res) => {
|
||||
// SDK bootstrap parameters must not change this page's account or API origin.
|
||||
if (Object.keys(req.query).length) {
|
||||
res.redirect('/embed/apps');
|
||||
return;
|
||||
}
|
||||
res.removeHeader('X-Frame-Options');
|
||||
res.setHeader(
|
||||
'Content-Security-Policy',
|
||||
`frame-ancestors ${embedFrameAncestors(this.config, req.get('referer'))}`,
|
||||
);
|
||||
res.setHeader('Cache-Control', 'no-store');
|
||||
res.send(homepage.renderAppBrowserEmbed());
|
||||
});
|
||||
|
||||
router.get('/settings', {}, (req, res) => sendShell(req, res));
|
||||
router.get('/settings/*splat', {}, (req, res) => sendShell(req, res));
|
||||
|
||||
|
||||
@@ -201,6 +201,7 @@ describe('PuterAIController.openaiResponses parameter forwarding', () => {
|
||||
context_management: [{ type: 'compaction' }],
|
||||
prompt_cache_key: 'ck',
|
||||
prompt_cache_retention: '24h',
|
||||
prompt_cache_options: { ttl: '12h', prewarm: true },
|
||||
store: true,
|
||||
truncation: 'auto',
|
||||
service_tier: 'default',
|
||||
@@ -226,6 +227,9 @@ describe('PuterAIController.openaiResponses parameter forwarding', () => {
|
||||
context_management: [{ type: 'compaction' }],
|
||||
prompt_cache_key: 'ck',
|
||||
prompt_cache_retention: '24h',
|
||||
// Forwarded as sent; the provider keeps only the fields it
|
||||
// accepts.
|
||||
prompt_cache_options: { ttl: '12h', prewarm: true },
|
||||
// forced false regardless of the client's request.
|
||||
store: false,
|
||||
truncation: 'auto',
|
||||
@@ -359,13 +363,18 @@ describe('PuterAIController.openaiResponses input normalization', () => {
|
||||
role: 'user',
|
||||
content: [
|
||||
'a bare string part',
|
||||
{ type: 'input_text', text: 'typed text' },
|
||||
{
|
||||
type: 'input_text',
|
||||
text: 'typed text',
|
||||
prompt_cache_breakpoint: { mode: 'explicit', ttl: '12h' },
|
||||
},
|
||||
{ type: 'output_text', text: 'echoed text' },
|
||||
{
|
||||
type: 'input_image',
|
||||
detail: 'high',
|
||||
image_url: 'https://img.test/a.png',
|
||||
file_id: 'file_img',
|
||||
prompt_cache_breakpoint: { mode: 'explicit' },
|
||||
},
|
||||
{ type: 'input_audio', input_audio: { data: 'AAA' } },
|
||||
{
|
||||
@@ -386,13 +395,18 @@ describe('PuterAIController.openaiResponses input normalization', () => {
|
||||
role: 'user',
|
||||
content: [
|
||||
{ type: 'text', text: 'a bare string part' },
|
||||
{ type: 'text', text: 'typed text' },
|
||||
{
|
||||
type: 'text',
|
||||
text: 'typed text',
|
||||
prompt_cache_breakpoint: { mode: 'explicit', ttl: '12h' },
|
||||
},
|
||||
{ type: 'text', text: 'echoed text' },
|
||||
{
|
||||
type: 'image_url',
|
||||
detail: 'high',
|
||||
image_url: { url: 'https://img.test/a.png' },
|
||||
file_id: 'file_img',
|
||||
prompt_cache_breakpoint: { mode: 'explicit' },
|
||||
},
|
||||
{ type: 'input_audio', input_audio: { data: 'AAA' } },
|
||||
{
|
||||
@@ -639,6 +653,33 @@ describe('PuterAIController.openaiResponses output items', () => {
|
||||
// -- /openai/v1/chat/completions edges -------------------------------
|
||||
|
||||
describe('PuterAIController.openaiChatCompletions translation edges', () => {
|
||||
it('forwards the prompt-cache fields to the driver', async () => {
|
||||
const completeSpy = stubChatComplete({
|
||||
message: { role: 'assistant', content: 'ok' },
|
||||
});
|
||||
|
||||
const { res } = makeRes();
|
||||
await controller.openaiChatCompletions(
|
||||
makeReq({
|
||||
body: {
|
||||
model: 'gpt-test',
|
||||
messages: [{ role: 'user', content: 'hi' }],
|
||||
prompt_cache_key: 'ck',
|
||||
prompt_cache_retention: 'in_memory',
|
||||
prompt_cache_options: { ttl: '30m' },
|
||||
},
|
||||
}),
|
||||
res,
|
||||
);
|
||||
|
||||
const args = completeSpy.mock.calls[0]![0] as Record<string, unknown>;
|
||||
expect(args).toMatchObject({
|
||||
prompt_cache_key: 'ck',
|
||||
prompt_cache_retention: 'in_memory',
|
||||
prompt_cache_options: { ttl: '30m' },
|
||||
});
|
||||
});
|
||||
|
||||
it('derives tool_calls from tool_use content blocks when the message has none', async () => {
|
||||
stubChatComplete({
|
||||
message: {
|
||||
|
||||
@@ -447,6 +447,7 @@ export class PuterAIController extends PuterController {
|
||||
body.reasoning_effort as ICompleteArguments['reasoning_effort'],
|
||||
}
|
||||
: {}),
|
||||
...promptCacheArgsFromBody(body),
|
||||
...openaiCompatProvider(body),
|
||||
...(body.fallback === false ? { fallback: false } : {}),
|
||||
};
|
||||
@@ -842,15 +843,7 @@ export class PuterAIController extends PuterController {
|
||||
body.compaction as ICompleteArguments['compaction'],
|
||||
}
|
||||
: {}),
|
||||
...(body.prompt_cache_key
|
||||
? { prompt_cache_key: String(body.prompt_cache_key) }
|
||||
: {}),
|
||||
...(body.prompt_cache_retention
|
||||
? {
|
||||
prompt_cache_retention:
|
||||
body.prompt_cache_retention as ICompleteArguments['prompt_cache_retention'],
|
||||
}
|
||||
: {}),
|
||||
...promptCacheArgsFromBody(body),
|
||||
// forced off regardless of what the caller sent — OpenAI
|
||||
// defaults `store` to true, which would persist state server-side
|
||||
// on OpenAI's end that this route gives no way to retrieve.
|
||||
@@ -1343,6 +1336,33 @@ const openaiCompatProvider = (
|
||||
return { provider: DEFAULTS.openaiChat };
|
||||
};
|
||||
|
||||
/**
|
||||
* The prompt-cache fields both OpenAI-shaped routes forward as sent; the
|
||||
* provider checks the values.
|
||||
*/
|
||||
const promptCacheArgsFromBody = (
|
||||
body: Record<string, unknown>,
|
||||
): Pick<
|
||||
ICompleteArguments,
|
||||
'prompt_cache_key' | 'prompt_cache_retention' | 'prompt_cache_options'
|
||||
> => ({
|
||||
...(body.prompt_cache_key
|
||||
? { prompt_cache_key: String(body.prompt_cache_key) }
|
||||
: {}),
|
||||
...(body.prompt_cache_retention
|
||||
? {
|
||||
prompt_cache_retention:
|
||||
body.prompt_cache_retention as ICompleteArguments['prompt_cache_retention'],
|
||||
}
|
||||
: {}),
|
||||
...(body.prompt_cache_options
|
||||
? {
|
||||
prompt_cache_options:
|
||||
body.prompt_cache_options as ICompleteArguments['prompt_cache_options'],
|
||||
}
|
||||
: {}),
|
||||
});
|
||||
|
||||
const randomId = (): string => crypto.randomUUID().replace(/-/g, '');
|
||||
const generateId = (prefix: string): string => `${prefix}_${randomId()}`;
|
||||
|
||||
@@ -1590,12 +1610,24 @@ const parseJsonMaybe = (value: unknown): unknown => {
|
||||
}
|
||||
};
|
||||
|
||||
/** An OpenAI `prompt_cache_breakpoint` marker, kept on the translated part. */
|
||||
const cacheBreakpointOf = (
|
||||
p: Record<string, unknown>,
|
||||
): { prompt_cache_breakpoint?: unknown } =>
|
||||
p.prompt_cache_breakpoint && typeof p.prompt_cache_breakpoint === 'object'
|
||||
? { prompt_cache_breakpoint: p.prompt_cache_breakpoint }
|
||||
: {};
|
||||
|
||||
const normalizeContentPart = (part: unknown): Record<string, unknown> => {
|
||||
if (typeof part === 'string') return { type: 'text', text: part };
|
||||
if (!part || typeof part !== 'object') return { type: 'text', text: '' };
|
||||
const p = part as Record<string, unknown>;
|
||||
if (p.type === 'input_text' || p.type === 'output_text') {
|
||||
return { type: 'text', text: String(p.text ?? '') };
|
||||
return {
|
||||
type: 'text',
|
||||
text: String(p.text ?? ''),
|
||||
...cacheBreakpointOf(p),
|
||||
};
|
||||
}
|
||||
if (p.type === 'input_image') {
|
||||
return {
|
||||
@@ -1603,6 +1635,7 @@ const normalizeContentPart = (part: unknown): Record<string, unknown> => {
|
||||
...(p.detail ? { detail: p.detail } : {}),
|
||||
...(p.image_url ? { image_url: { url: p.image_url } } : {}),
|
||||
...(p.file_id ? { file_id: p.file_id } : {}),
|
||||
...cacheBreakpointOf(p),
|
||||
};
|
||||
}
|
||||
if (p.type === 'input_audio')
|
||||
@@ -1614,6 +1647,7 @@ const normalizeContentPart = (part: unknown): Record<string, unknown> => {
|
||||
...(p.file_id ? { file_id: p.file_id } : {}),
|
||||
...(p.file_url ? { file_url: p.file_url } : {}),
|
||||
...(p.filename ? { filename: p.filename } : {}),
|
||||
...cacheBreakpointOf(p),
|
||||
};
|
||||
}
|
||||
return p;
|
||||
|
||||
@@ -449,6 +449,22 @@ describe('AzureResponsesProvider.complete request shape', () => {
|
||||
expect(args.service_tier).toBe('default');
|
||||
});
|
||||
|
||||
it('sends the wire spelling of the in-memory cache retention', async () => {
|
||||
const provider = makeProvider();
|
||||
responsesCreateMock.mockResolvedValueOnce(okResponse);
|
||||
|
||||
await withTestActor(() =>
|
||||
provider.complete({
|
||||
model: 'gpt-5.3-codex',
|
||||
messages: [{ role: 'user', content: 'hi' }],
|
||||
prompt_cache_retention: 'in-memory',
|
||||
} as never),
|
||||
);
|
||||
|
||||
const [args] = responsesCreateMock.mock.calls[0]!;
|
||||
expect(args.prompt_cache_retention).toBe('in_memory');
|
||||
});
|
||||
|
||||
it.each([
|
||||
['previous_response_id', 'resp_1'],
|
||||
['conversation', 'conv_1'],
|
||||
|
||||
@@ -35,6 +35,7 @@ import * as OpenAiUtil from '../../utils/OpenAIUtil.js';
|
||||
import {
|
||||
clampReasoningEffort,
|
||||
openAICompatParams,
|
||||
promptCacheRetentionToWire,
|
||||
rejectStatefulResponsesFields,
|
||||
} from '../../utils/openaiParams.js';
|
||||
import { buildCostsOverride } from '../../utils/pricing.js';
|
||||
@@ -209,6 +210,9 @@ export class AzureResponsesProvider implements IChatProvider {
|
||||
const userIdentifier = upstreamUserIdentifier(actor);
|
||||
// Cache key defaults to the actor identifier; see upstreamUserIdentifier.
|
||||
const cacheKey = prompt_cache_key ?? userIdentifier;
|
||||
const cacheRetention = promptCacheRetentionToWire(
|
||||
prompt_cache_retention,
|
||||
);
|
||||
|
||||
// Resolve any `puter_path` content parts into inline base64 data URLs
|
||||
// before the Responses API sees them.
|
||||
@@ -307,8 +311,8 @@ export class AzureResponsesProvider implements IChatProvider {
|
||||
...(instructions !== undefined ? { instructions } : {}),
|
||||
...(metadata !== undefined ? { metadata } : {}),
|
||||
...(cacheKey !== undefined ? { prompt_cache_key: cacheKey } : {}),
|
||||
...(prompt_cache_retention !== undefined
|
||||
? { prompt_cache_retention }
|
||||
...(cacheRetention !== undefined
|
||||
? { prompt_cache_retention: cacheRetention }
|
||||
: {}),
|
||||
...(store !== undefined ? { store } : {}),
|
||||
...(max_tokens !== undefined
|
||||
|
||||
@@ -23,6 +23,7 @@ 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,
|
||||
@@ -107,12 +108,9 @@ export class MetaProvider extends OpenAICompatProvider {
|
||||
? requestedEffort
|
||||
: undefined;
|
||||
|
||||
// Puter spells the in-memory retention with a hyphen; Meta's enum
|
||||
// uses an underscore.
|
||||
const cacheRetention =
|
||||
args.prompt_cache_retention === 'in-memory'
|
||||
? 'in_memory'
|
||||
: args.prompt_cache_retention;
|
||||
const cacheRetention = promptCacheRetentionToWire(
|
||||
args.prompt_cache_retention,
|
||||
);
|
||||
|
||||
// The identifier is Puter's abuse attribution, so `custom` can't
|
||||
// override it. Cache key defaults to it; see upstreamUserIdentifier.
|
||||
|
||||
+157
-1
@@ -249,6 +249,130 @@ describe('OpenAiChatProvider.complete argument validation', () => {
|
||||
expect(result).toEqual({ delegated: true });
|
||||
});
|
||||
|
||||
it('delegates a 12h cache lifetime to the Responses provider, which is the only API that writes it', async () => {
|
||||
const { provider } = makeProvider();
|
||||
const sibling = {
|
||||
complete: vi.fn().mockResolvedValue({ delegated: true }),
|
||||
};
|
||||
provider.setResponsesProvider(
|
||||
sibling as unknown as Parameters<
|
||||
typeof provider.setResponsesProvider
|
||||
>[0],
|
||||
);
|
||||
|
||||
const params = {
|
||||
model: 'gpt-6-luna',
|
||||
messages: [{ role: 'user', content: 'hi' }],
|
||||
prompt_cache_options: { ttl: '12h' as const },
|
||||
};
|
||||
const result = await withTestActor(() => provider.complete(params));
|
||||
|
||||
expect(createMock).not.toHaveBeenCalled();
|
||||
expect(sibling.complete).toHaveBeenCalledWith(params);
|
||||
expect(result).toEqual({ delegated: true });
|
||||
});
|
||||
|
||||
it.each([
|
||||
['30m', { mode: 'explicit', ttl: '30m' }],
|
||||
['12h', { mode: 'explicit', ttl: '12h' }],
|
||||
])(
|
||||
'delegates a %s breakpoint lifetime to the Responses provider, since Chat Completions rejects the field',
|
||||
async (_ttl, breakpoint) => {
|
||||
const { provider } = makeProvider();
|
||||
const sibling = {
|
||||
complete: vi.fn().mockResolvedValue({ delegated: true }),
|
||||
};
|
||||
provider.setResponsesProvider(
|
||||
sibling as unknown as Parameters<
|
||||
typeof provider.setResponsesProvider
|
||||
>[0],
|
||||
);
|
||||
|
||||
const params = {
|
||||
model: 'gpt-6-luna',
|
||||
messages: [
|
||||
{
|
||||
role: 'system',
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: 'long prefix',
|
||||
prompt_cache_breakpoint: breakpoint,
|
||||
},
|
||||
],
|
||||
},
|
||||
{ role: 'user', content: 'hi' },
|
||||
],
|
||||
};
|
||||
const result = await withTestActor(() =>
|
||||
provider.complete(params as never),
|
||||
);
|
||||
|
||||
expect(createMock).not.toHaveBeenCalled();
|
||||
expect(sibling.complete).toHaveBeenCalledWith(params);
|
||||
expect(result).toEqual({ delegated: true });
|
||||
},
|
||||
);
|
||||
|
||||
it('keeps a breakpoint without a lifetime on the Chat Completions path, intact', async () => {
|
||||
const { provider } = makeProvider();
|
||||
createMock.mockResolvedValueOnce({
|
||||
choices: [
|
||||
{
|
||||
message: { content: 'ok', role: 'assistant' },
|
||||
finish_reason: 'stop',
|
||||
},
|
||||
],
|
||||
usage: { prompt_tokens: 1, completion_tokens: 1 },
|
||||
});
|
||||
|
||||
await withTestActor(() =>
|
||||
provider.complete({
|
||||
model: 'gpt-6-luna',
|
||||
messages: [
|
||||
{
|
||||
role: 'system',
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: 'long prefix',
|
||||
prompt_cache_breakpoint: { mode: 'explicit' },
|
||||
},
|
||||
],
|
||||
},
|
||||
{ role: 'user', content: 'hi' },
|
||||
],
|
||||
} as never),
|
||||
);
|
||||
|
||||
const [args] = createMock.mock.calls[0]!;
|
||||
expect(args.messages[0]).toMatchObject({
|
||||
role: 'system',
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: 'long prefix',
|
||||
prompt_cache_breakpoint: { mode: 'explicit' },
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it('throws 400 for a 12h cache lifetime without a Responses sibling provider', async () => {
|
||||
const { provider } = makeProvider();
|
||||
|
||||
await expect(
|
||||
withTestActor(() =>
|
||||
provider.complete({
|
||||
model: 'gpt-6-luna',
|
||||
messages: [{ role: 'user', content: 'hi' }],
|
||||
prompt_cache_options: { ttl: '12h' },
|
||||
}),
|
||||
),
|
||||
).rejects.toMatchObject({ statusCode: 400 });
|
||||
expect(createMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('requestPricing delegates to the Responses sibling (web_search\'s own hold), and no-ops without one', async () => {
|
||||
const { provider } = makeProvider();
|
||||
const args = {
|
||||
@@ -349,6 +473,31 @@ describe('OpenAiChatProvider.complete request shape', () => {
|
||||
expect(args.safety_identifier).toBe('puter-u42');
|
||||
});
|
||||
|
||||
it('forwards 30m prompt_cache_options and the wire spelling of prompt_cache_retention', async () => {
|
||||
const { provider } = makeProvider();
|
||||
createMock.mockResolvedValueOnce(baseCompletion);
|
||||
|
||||
await withTestActor(() =>
|
||||
provider.complete({
|
||||
model: 'gpt-6-luna',
|
||||
messages: [{ role: 'user', content: 'hello' }],
|
||||
prompt_cache_retention: 'in-memory',
|
||||
prompt_cache_options: {
|
||||
mode: 'explicit',
|
||||
ttl: '30m',
|
||||
prewarm: true,
|
||||
} as never,
|
||||
}),
|
||||
);
|
||||
|
||||
const [args] = createMock.mock.calls[0]!;
|
||||
expect(args.prompt_cache_retention).toBe('in_memory');
|
||||
expect(args.prompt_cache_options).toEqual({
|
||||
mode: 'explicit',
|
||||
ttl: '30m',
|
||||
});
|
||||
});
|
||||
|
||||
it.each([
|
||||
'gpt-6-astra',
|
||||
'gpt-6.1-sol',
|
||||
@@ -665,7 +814,7 @@ describe('OpenAiChatProvider.complete non-stream output', () => {
|
||||
},
|
||||
});
|
||||
|
||||
await withTestActor(() =>
|
||||
const result = await withTestActor(() =>
|
||||
provider.complete({
|
||||
model: 'gpt-6-luna',
|
||||
messages: [{ role: 'user', content: 'hi' }],
|
||||
@@ -685,6 +834,13 @@ describe('OpenAiChatProvider.complete non-stream output', () => {
|
||||
cached_tokens: 1000 * Number(luna.costs.cached_tokens),
|
||||
cache_write_tokens: 3000 * Number(luna.costs.cache_write_tokens),
|
||||
});
|
||||
// Chat Completions only writes OpenAI's default 30m entries.
|
||||
expect((result as { usageDetails: unknown }).usageDetails).toEqual({
|
||||
inputTokens: 1000,
|
||||
outputTokens: 12,
|
||||
cacheReadTokens: 1000,
|
||||
cacheWrite30mTokens: 3000,
|
||||
});
|
||||
});
|
||||
|
||||
it('zeroes cached_tokens when prompt_tokens_details is missing', async () => {
|
||||
|
||||
@@ -35,7 +35,10 @@ import { make_openai_tools } from '../../utils/FunctionCalling.js';
|
||||
import * as OpenAiUtil from '../../utils/OpenAIUtil.js';
|
||||
import {
|
||||
clampReasoningEffort,
|
||||
messagesHaveCacheBreakpointTtl,
|
||||
openAICompatParams,
|
||||
promptCacheOptionsToWire,
|
||||
promptCacheRetentionToWire,
|
||||
} from '../../utils/openaiParams.js';
|
||||
import { OPEN_AI_MODELS } from './models.js';
|
||||
import type { OpenAiResponsesChatProvider } from './OpenAiChatResponsesProvider.js';
|
||||
@@ -126,8 +129,11 @@ export class OpenAiChatProvider implements IChatProvider {
|
||||
temperature,
|
||||
text,
|
||||
prompt_cache_key,
|
||||
prompt_cache_retention,
|
||||
prompt_cache_options,
|
||||
} = params;
|
||||
let { messages, model } = params;
|
||||
const cacheOptions = promptCacheOptionsToWire(prompt_cache_options);
|
||||
if (
|
||||
tools?.filter((e) => isWebSearchTool(e as Record<string, unknown>))
|
||||
.length
|
||||
@@ -157,6 +163,21 @@ export class OpenAiChatProvider implements IChatProvider {
|
||||
}
|
||||
return await this.#responsesProvider.complete(params);
|
||||
}
|
||||
// Only the Responses API writes 12h cache entries or takes a lifetime
|
||||
// on a `prompt_cache_breakpoint`; Chat Completions rejects both.
|
||||
if (
|
||||
cacheOptions?.ttl === '12h' ||
|
||||
messagesHaveCacheBreakpointTtl(messages)
|
||||
) {
|
||||
if (!this.#responsesProvider) {
|
||||
throw new HttpError(
|
||||
400,
|
||||
'a prompt cache lifetime requires the OpenAI Responses provider, which is not configured',
|
||||
{ legacyCode: 'bad_request' },
|
||||
);
|
||||
}
|
||||
return await this.#responsesProvider.complete(params);
|
||||
}
|
||||
// Validate messages
|
||||
if (!Array.isArray(messages)) {
|
||||
throw new HttpError(400, '`messages` must be an array', {
|
||||
@@ -180,6 +201,9 @@ export class OpenAiChatProvider implements IChatProvider {
|
||||
const userIdentifier = upstreamUserIdentifier(actor);
|
||||
// Cache key defaults to the actor identifier; see upstreamUserIdentifier.
|
||||
const cacheKey = prompt_cache_key ?? userIdentifier;
|
||||
const cacheRetention = promptCacheRetentionToWire(
|
||||
prompt_cache_retention,
|
||||
);
|
||||
|
||||
// Strip Anthropic-only shape a fallback-replayed message can carry
|
||||
// (thinking/server-tool blocks, cache_control, citations) before the
|
||||
@@ -216,6 +240,12 @@ export class OpenAiChatProvider implements IChatProvider {
|
||||
user: userIdentifier,
|
||||
safety_identifier: userIdentifier,
|
||||
...(cacheKey !== undefined ? { prompt_cache_key: cacheKey } : {}),
|
||||
...(cacheRetention !== undefined
|
||||
? { prompt_cache_retention: cacheRetention }
|
||||
: {}),
|
||||
...(cacheOptions !== undefined
|
||||
? { prompt_cache_options: cacheOptions }
|
||||
: {}),
|
||||
messages: messages,
|
||||
model: modelUsed.id,
|
||||
...(mappedTools?.length ? { tools: mappedTools } : {}),
|
||||
@@ -254,28 +284,11 @@ export class OpenAiChatProvider implements IChatProvider {
|
||||
);
|
||||
|
||||
return OpenAiUtil.handle_completion_output({
|
||||
usage_calculator: ({ usage, setUsageCosts }) => {
|
||||
const cachedTokens =
|
||||
usage.prompt_tokens_details?.cached_tokens ?? 0;
|
||||
// GPT-5.6 and later bill cache writes at 1.25x input. They're
|
||||
// reported inside `prompt_tokens`, like cached reads.
|
||||
// The SDK doesn't type `cache_write_tokens` yet.
|
||||
const cacheWriteTokens =
|
||||
(
|
||||
usage.prompt_tokens_details as
|
||||
{ cache_write_tokens?: number } | undefined
|
||||
)?.cache_write_tokens ?? 0;
|
||||
const trackedUsage = {
|
||||
prompt_tokens:
|
||||
(usage.prompt_tokens ?? 0) -
|
||||
cachedTokens -
|
||||
cacheWriteTokens,
|
||||
completion_tokens: usage.completion_tokens ?? 0,
|
||||
cached_tokens: cachedTokens,
|
||||
...(cacheWriteTokens
|
||||
? { cache_write_tokens: cacheWriteTokens }
|
||||
: {}),
|
||||
};
|
||||
usage_calculator: ({ usage, setUsageCosts, setUsageDetails }) => {
|
||||
// GPT-5.6 and later report cache writes inside
|
||||
// `prompt_tokens`, like cached reads; Chat Completions only
|
||||
// writes the default 30m entries, billed at 1.25x input.
|
||||
const trackedUsage = OpenAiUtil.splitCachedPrompt(usage);
|
||||
|
||||
const metered = meterChatUsage(
|
||||
this.#meteringService,
|
||||
@@ -285,6 +298,11 @@ export class OpenAiChatProvider implements IChatProvider {
|
||||
trackedUsage,
|
||||
);
|
||||
setUsageCosts(metered.costs);
|
||||
setUsageDetails(
|
||||
OpenAiUtil.usageDetailsFromTrackedUsage(metered.usage, {
|
||||
writeTtl: '30m',
|
||||
})!,
|
||||
);
|
||||
return metered.usage;
|
||||
},
|
||||
stream,
|
||||
|
||||
@@ -246,6 +246,7 @@ describe('OpenAiResponsesChatProvider model catalog', () => {
|
||||
prompt_tokens: input,
|
||||
cached_tokens: cached,
|
||||
cache_write_tokens: input * 1.25,
|
||||
cache_write_12h_tokens: input * 2.5,
|
||||
completion_tokens: output,
|
||||
},
|
||||
context: 1_050_000,
|
||||
@@ -844,7 +845,7 @@ describe('OpenAiResponsesChatProvider.complete non-stream output', () => {
|
||||
},
|
||||
});
|
||||
|
||||
await withTestActor(() =>
|
||||
const result = await withTestActor(() =>
|
||||
provider.complete({
|
||||
model: 'gpt-6-luna',
|
||||
messages: [{ role: 'user', content: 'hi' }],
|
||||
@@ -867,6 +868,115 @@ describe('OpenAiResponsesChatProvider.complete non-stream output', () => {
|
||||
cached_tokens: 1000 * Number(luna.costs.cached_tokens),
|
||||
cache_write_tokens: 3000 * Number(luna.costs.cache_write_tokens),
|
||||
});
|
||||
// Writes without a per-lifetime breakdown are OpenAI's default 30m
|
||||
// entries, and the neutral breakdown says so.
|
||||
expect((result as { usageDetails: unknown }).usageDetails).toEqual({
|
||||
inputTokens: 1000,
|
||||
outputTokens: 20,
|
||||
cacheReadTokens: 1000,
|
||||
cacheWrite30mTokens: 3000,
|
||||
});
|
||||
});
|
||||
|
||||
it('bills 12h cache writes at 2.5x input and the other writes at 1.25x', async () => {
|
||||
const luna = OPEN_AI_MODELS.find((m) => m.id === 'gpt-6-luna')!;
|
||||
const { provider } = makeProvider();
|
||||
// One request can write both lifetimes: an explicit 12h breakpoint
|
||||
// plus the implicit 30m one OpenAI adds at the end of the prompt.
|
||||
responsesCreateMock.mockResolvedValueOnce({
|
||||
output: [{ role: 'assistant' }],
|
||||
output_text: 'ok',
|
||||
usage: {
|
||||
input_tokens: 6026,
|
||||
output_tokens: 5,
|
||||
input_tokens_details: {
|
||||
cached_tokens: 0,
|
||||
cache_write_tokens: 6023,
|
||||
cache_write_tokens_details: {
|
||||
ttl_12h_tokens: 6012,
|
||||
ttl_30m_tokens: 11,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const result = await withTestActor(() =>
|
||||
provider.complete({
|
||||
model: 'gpt-6-luna',
|
||||
messages: [{ role: 'user', content: 'hi' }],
|
||||
prompt_cache_options: { ttl: '12h' },
|
||||
}),
|
||||
);
|
||||
|
||||
const [usage, , , overrides] = recordSpy.mock.calls[0]!;
|
||||
expect(usage).toEqual({
|
||||
prompt_tokens: 3,
|
||||
completion_tokens: 5,
|
||||
cached_tokens: 0,
|
||||
cache_write_tokens: 11,
|
||||
cache_write_12h_tokens: 6012,
|
||||
});
|
||||
expect(luna.costs.cache_write_12h_tokens).toBe(
|
||||
Number(luna.costs.prompt_tokens) * 2.5,
|
||||
);
|
||||
expect(overrides).toEqual({
|
||||
prompt_tokens: 3 * Number(luna.costs.prompt_tokens),
|
||||
completion_tokens: 5 * Number(luna.costs.completion_tokens),
|
||||
cached_tokens: 0,
|
||||
cache_write_tokens: 11 * Number(luna.costs.cache_write_tokens),
|
||||
cache_write_12h_tokens:
|
||||
6012 * Number(luna.costs.cache_write_12h_tokens),
|
||||
});
|
||||
expect((result as { usageDetails: unknown }).usageDetails).toEqual({
|
||||
inputTokens: 3,
|
||||
outputTokens: 5,
|
||||
cacheWrite30mTokens: 11,
|
||||
cacheWrite12hTokens: 6012,
|
||||
});
|
||||
});
|
||||
|
||||
it('forwards prompt_cache_options and the wire spelling of prompt_cache_retention', async () => {
|
||||
const { provider } = makeProvider();
|
||||
responsesCreateMock.mockResolvedValueOnce({
|
||||
output: [{ role: 'assistant' }],
|
||||
output_text: 'ok',
|
||||
usage: { input_tokens: 1, output_tokens: 1 },
|
||||
});
|
||||
|
||||
await withTestActor(() =>
|
||||
provider.complete({
|
||||
model: 'gpt-6-luna',
|
||||
messages: [{ role: 'user', content: 'hi' }],
|
||||
prompt_cache_retention: 'in-memory',
|
||||
prompt_cache_options: {
|
||||
mode: 'explicit',
|
||||
ttl: '12h',
|
||||
comparison_response_id: 'resp_1',
|
||||
} as never,
|
||||
}),
|
||||
);
|
||||
|
||||
const [args] = responsesCreateMock.mock.calls[0]!;
|
||||
expect(args.prompt_cache_retention).toBe('in_memory');
|
||||
expect(args.prompt_cache_options).toEqual({
|
||||
mode: 'explicit',
|
||||
ttl: '12h',
|
||||
});
|
||||
});
|
||||
|
||||
it('rejects a cache lifetime OpenAI does not offer before calling upstream', async () => {
|
||||
const { provider } = makeProvider();
|
||||
|
||||
await expect(
|
||||
withTestActor(() =>
|
||||
provider.complete({
|
||||
model: 'gpt-6-luna',
|
||||
messages: [{ role: 'user', content: 'hi' }],
|
||||
prompt_cache_options: { ttl: '2h' } as never,
|
||||
}),
|
||||
),
|
||||
).rejects.toMatchObject({ statusCode: 400 });
|
||||
expect(responsesCreateMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it.each([
|
||||
@@ -1068,6 +1178,69 @@ describe('OpenAiResponsesChatProvider.complete streaming', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('labels streamed cache writes by lifetime on the final usage chunk', async () => {
|
||||
const luna = OPEN_AI_MODELS.find((m) => m.id === 'gpt-6-luna')!;
|
||||
const { provider } = makeProvider();
|
||||
responsesCreateMock.mockReturnValueOnce(
|
||||
asAsyncIterable([
|
||||
{ type: 'response.output_text.delta', delta: 'ok' },
|
||||
{
|
||||
type: 'response.completed',
|
||||
response: {
|
||||
usage: {
|
||||
input_tokens: 6026,
|
||||
output_tokens: 5,
|
||||
input_tokens_details: {
|
||||
cached_tokens: 0,
|
||||
cache_write_tokens: 6023,
|
||||
cache_write_tokens_details: {
|
||||
ttl_12h_tokens: 6012,
|
||||
ttl_30m_tokens: 11,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
]),
|
||||
);
|
||||
|
||||
const result = await withTestActor(() =>
|
||||
provider.complete({
|
||||
model: 'gpt-6-luna',
|
||||
messages: [{ role: 'user', content: 'say ok' }],
|
||||
stream: true,
|
||||
prompt_cache_options: { ttl: '12h' },
|
||||
}),
|
||||
);
|
||||
const harness = makeCapturingChatStream();
|
||||
await (
|
||||
result as {
|
||||
init_chat_stream: (p: { chatStream: unknown }) => Promise<void>;
|
||||
}
|
||||
).init_chat_stream({ chatStream: harness.chatStream });
|
||||
|
||||
const usageEvent = harness.events().find((e) => e.type === 'usage');
|
||||
expect(usageEvent?.usage).toEqual({
|
||||
prompt_tokens: 3,
|
||||
completion_tokens: 5,
|
||||
cached_tokens: 0,
|
||||
cache_write_tokens: 11,
|
||||
cache_write_12h_tokens: 6012,
|
||||
});
|
||||
expect(usageEvent?.usageDetails).toEqual({
|
||||
inputTokens: 3,
|
||||
outputTokens: 5,
|
||||
cacheWrite30mTokens: 11,
|
||||
cacheWrite12hTokens: 6012,
|
||||
});
|
||||
const [, , , overrides] = recordSpy.mock.calls[0]!;
|
||||
expect(overrides).toMatchObject({
|
||||
cache_write_tokens: 11 * Number(luna.costs.cache_write_tokens),
|
||||
cache_write_12h_tokens:
|
||||
6012 * Number(luna.costs.cache_write_12h_tokens),
|
||||
});
|
||||
});
|
||||
|
||||
it('emits a tool_use block when response.output_item.done arrives with a function_call', async () => {
|
||||
const { provider } = makeProvider();
|
||||
responsesCreateMock.mockReturnValueOnce(
|
||||
|
||||
@@ -32,6 +32,8 @@ import * as OpenAiUtil from '../../utils/OpenAIUtil.js';
|
||||
import {
|
||||
clampReasoningEffort,
|
||||
openAICompatParams,
|
||||
promptCacheOptionsToWire,
|
||||
promptCacheRetentionToWire,
|
||||
rejectStatefulResponsesFields,
|
||||
} from '../../utils/openaiParams.js';
|
||||
import { responseSamplingParams } from '../../utils/responseSampling.js';
|
||||
@@ -150,6 +152,7 @@ export class OpenAiResponsesChatProvider implements IChatProvider {
|
||||
prompt,
|
||||
prompt_cache_key,
|
||||
prompt_cache_retention,
|
||||
prompt_cache_options,
|
||||
store,
|
||||
top_p,
|
||||
truncation,
|
||||
@@ -195,6 +198,10 @@ export class OpenAiResponsesChatProvider implements IChatProvider {
|
||||
const userIdentifier = upstreamUserIdentifier(actor);
|
||||
// Cache key defaults to the actor identifier; see upstreamUserIdentifier.
|
||||
const cacheKey = prompt_cache_key ?? userIdentifier;
|
||||
const cacheRetention = promptCacheRetentionToWire(
|
||||
prompt_cache_retention,
|
||||
);
|
||||
const cacheOptions = promptCacheOptionsToWire(prompt_cache_options);
|
||||
|
||||
if (tools) {
|
||||
// Unravel tools to OpenAI Responses API format
|
||||
@@ -288,8 +295,11 @@ export class OpenAiResponsesChatProvider implements IChatProvider {
|
||||
...(instructions !== undefined ? { instructions } : {}),
|
||||
...(metadata !== undefined ? { metadata } : {}),
|
||||
...(cacheKey !== undefined ? { prompt_cache_key: cacheKey } : {}),
|
||||
...(prompt_cache_retention !== undefined
|
||||
? { prompt_cache_retention }
|
||||
...(cacheRetention !== undefined
|
||||
? { prompt_cache_retention: cacheRetention }
|
||||
: {}),
|
||||
...(cacheOptions !== undefined
|
||||
? { prompt_cache_options: cacheOptions }
|
||||
: {}),
|
||||
...(store !== undefined ? { store } : {}),
|
||||
...(max_tokens !== undefined
|
||||
@@ -310,24 +320,17 @@ export class OpenAiResponsesChatProvider implements IChatProvider {
|
||||
{ signal: Context.get('upstreamSignal') },
|
||||
);
|
||||
return OpenAiUtil.handle_completion_output_responses_api({
|
||||
usage_calculator: ({ usage, webSearchCalls, setUsageCosts }) => {
|
||||
const cachedTokens =
|
||||
(usage as any).input_tokens_details?.cached_tokens ?? 0;
|
||||
// GPT-5.6 and later bill cache writes at 1.25x input. They're
|
||||
// reported inside `input_tokens`, like cached reads.
|
||||
const cacheWriteTokens =
|
||||
(usage as any).input_tokens_details?.cache_write_tokens ??
|
||||
0;
|
||||
usage_calculator: ({
|
||||
usage,
|
||||
webSearchCalls,
|
||||
setUsageCosts,
|
||||
setUsageDetails,
|
||||
}) => {
|
||||
// GPT-5.6 and later report cache writes inside `input_tokens`,
|
||||
// like cached reads, split by lifetime (30m and 12h entries
|
||||
// cost 1.25x and 2.5x input to write).
|
||||
const trackedUsage: Record<string, number> = {
|
||||
prompt_tokens:
|
||||
((usage as any).input_tokens ?? 0) -
|
||||
cachedTokens -
|
||||
cacheWriteTokens,
|
||||
completion_tokens: (usage as any).output_tokens ?? 0,
|
||||
cached_tokens: cachedTokens,
|
||||
...(cacheWriteTokens
|
||||
? { cache_write_tokens: cacheWriteTokens }
|
||||
: {}),
|
||||
...OpenAiUtil.splitCachedPrompt(usage),
|
||||
...(webSearchCalls
|
||||
? { web_search_calls: webSearchCalls }
|
||||
: {}),
|
||||
@@ -356,6 +359,11 @@ export class OpenAiResponsesChatProvider implements IChatProvider {
|
||||
: {},
|
||||
);
|
||||
setUsageCosts?.(metered.costs);
|
||||
setUsageDetails?.(
|
||||
OpenAiUtil.usageDetailsFromTrackedUsage(metered.usage, {
|
||||
writeTtl: '30m',
|
||||
})!,
|
||||
);
|
||||
return metered.usage;
|
||||
},
|
||||
stream,
|
||||
|
||||
@@ -23,6 +23,8 @@ import type { IChatModel } from '../../types.js';
|
||||
|
||||
// Prompts over 272K input tokens are billed at 2x input (cached reads and
|
||||
// cache writes included) and 1.5x output for the full request.
|
||||
// GPT-5.6 and later bill cache writes by the lifetime requested: 1.25x input
|
||||
// for the default 30m entries, 2.5x for 12h entries.
|
||||
const GPT_LONG_CONTEXT_PRICING = {
|
||||
threshold: 272_000,
|
||||
input_multiplier: 2,
|
||||
@@ -49,6 +51,7 @@ export const OPEN_AI_MODELS: IChatModel[] = [
|
||||
prompt_tokens: 100,
|
||||
cached_tokens: 5,
|
||||
cache_write_tokens: 100 * 1.25,
|
||||
cache_write_12h_tokens: 100 * 2.5,
|
||||
completion_tokens: 500,
|
||||
},
|
||||
long_context_pricing: GPT_LONG_CONTEXT_PRICING,
|
||||
@@ -74,6 +77,7 @@ export const OPEN_AI_MODELS: IChatModel[] = [
|
||||
prompt_tokens: 200,
|
||||
cached_tokens: 10,
|
||||
cache_write_tokens: 200 * 1.25,
|
||||
cache_write_12h_tokens: 200 * 2.5,
|
||||
completion_tokens: 1000,
|
||||
},
|
||||
long_context_pricing: GPT_LONG_CONTEXT_PRICING,
|
||||
@@ -99,6 +103,7 @@ export const OPEN_AI_MODELS: IChatModel[] = [
|
||||
prompt_tokens: 200,
|
||||
cached_tokens: 20,
|
||||
cache_write_tokens: 200 * 1.25,
|
||||
cache_write_12h_tokens: 200 * 2.5,
|
||||
completion_tokens: 1000,
|
||||
},
|
||||
long_context_pricing: GPT_LONG_CONTEXT_PRICING,
|
||||
@@ -124,6 +129,7 @@ export const OPEN_AI_MODELS: IChatModel[] = [
|
||||
prompt_tokens: 10,
|
||||
cached_tokens: 1,
|
||||
cache_write_tokens: 10 * 1.25,
|
||||
cache_write_12h_tokens: 10 * 2.5,
|
||||
completion_tokens: 50,
|
||||
},
|
||||
long_context_pricing: GPT_LONG_CONTEXT_PRICING,
|
||||
@@ -149,6 +155,7 @@ export const OPEN_AI_MODELS: IChatModel[] = [
|
||||
prompt_tokens: 1000,
|
||||
cached_tokens: 100,
|
||||
cache_write_tokens: 1000 * 1.25,
|
||||
cache_write_12h_tokens: 1000 * 2.5,
|
||||
completion_tokens: 5000,
|
||||
},
|
||||
long_context_pricing: GPT_LONG_CONTEXT_PRICING,
|
||||
@@ -174,6 +181,7 @@ export const OPEN_AI_MODELS: IChatModel[] = [
|
||||
prompt_tokens: 400,
|
||||
cached_tokens: 40,
|
||||
cache_write_tokens: 400 * 1.25,
|
||||
cache_write_12h_tokens: 400 * 2.5,
|
||||
completion_tokens: 2000,
|
||||
},
|
||||
long_context_pricing: GPT_LONG_CONTEXT_PRICING,
|
||||
@@ -198,6 +206,7 @@ export const OPEN_AI_MODELS: IChatModel[] = [
|
||||
prompt_tokens: 200,
|
||||
cached_tokens: 20,
|
||||
cache_write_tokens: 200 * 1.25,
|
||||
cache_write_12h_tokens: 200 * 2.5,
|
||||
completion_tokens: 1200,
|
||||
},
|
||||
long_context_pricing: GPT_LONG_CONTEXT_PRICING,
|
||||
@@ -222,6 +231,7 @@ export const OPEN_AI_MODELS: IChatModel[] = [
|
||||
prompt_tokens: 20,
|
||||
cached_tokens: 2,
|
||||
cache_write_tokens: 20 * 1.25,
|
||||
cache_write_12h_tokens: 20 * 2.5,
|
||||
completion_tokens: 120,
|
||||
},
|
||||
long_context_pricing: GPT_LONG_CONTEXT_PRICING,
|
||||
|
||||
@@ -145,13 +145,19 @@ export type StopReason =
|
||||
| 'model_context_window_exceeded'
|
||||
| (string & {});
|
||||
|
||||
/** Provider-neutral usage breakdown. `inputTokens` excludes cache reads/writes. */
|
||||
/**
|
||||
* Provider-neutral usage breakdown. `inputTokens` excludes cache reads/writes.
|
||||
* Cache writes are keyed by the lifetime the entry was written with: Claude and
|
||||
* Moonshot write 5m and 1h entries, OpenAI writes 30m and 12h entries.
|
||||
*/
|
||||
export interface UsageDetails {
|
||||
inputTokens: number;
|
||||
outputTokens: number; // includes reasoning
|
||||
cacheReadTokens?: number;
|
||||
cacheWrite5mTokens?: number;
|
||||
cacheWrite30mTokens?: number;
|
||||
cacheWrite1hTokens?: number;
|
||||
cacheWrite12hTokens?: number;
|
||||
reasoningTokens?: number;
|
||||
webSearchRequests?: number;
|
||||
webFetchRequests?: number;
|
||||
@@ -168,10 +174,22 @@ export interface UsageDetails {
|
||||
outputTokens: number;
|
||||
cacheReadTokens?: number;
|
||||
cacheWrite5mTokens?: number;
|
||||
cacheWrite30mTokens?: number;
|
||||
cacheWrite1hTokens?: number;
|
||||
cacheWrite12hTokens?: number;
|
||||
}>;
|
||||
}
|
||||
|
||||
/**
|
||||
* OpenAI `prompt_cache_options`, GPT-5.6 and later. `ttl` is the minimum
|
||||
* lifetime of every cache entry the request writes; `12h` entries cost twice
|
||||
* what `30m` entries do to write and only the Responses API writes them.
|
||||
*/
|
||||
export interface PromptCacheOptions {
|
||||
mode?: 'implicit' | 'explicit';
|
||||
ttl?: '30m' | '12h';
|
||||
}
|
||||
|
||||
export interface ICompleteArguments {
|
||||
messages: PuterMessage[];
|
||||
provider?: string;
|
||||
@@ -234,7 +252,9 @@ export interface ICompleteArguments {
|
||||
metadata?: Record<string, string>;
|
||||
prompt?: unknown;
|
||||
prompt_cache_key?: string;
|
||||
prompt_cache_retention?: 'in-memory' | '24h' | undefined;
|
||||
/** `in-memory` is accepted as a spelling of OpenAI's `in_memory`. */
|
||||
prompt_cache_retention?: 'in-memory' | 'in_memory' | '24h' | undefined;
|
||||
prompt_cache_options?: PromptCacheOptions;
|
||||
store?: boolean;
|
||||
top_p?: number;
|
||||
truncation?: 'auto' | 'disabled' | undefined;
|
||||
|
||||
@@ -481,43 +481,86 @@ export const extractMeteredUsage = (usage) => {
|
||||
};
|
||||
|
||||
/**
|
||||
* Chat Completions usage as it's metered: `prompt_tokens` reports cached reads
|
||||
* inside its count, so they come out of it and are priced only once, under
|
||||
* `cached_tokens`.
|
||||
* @typedef {{
|
||||
* cached_tokens?: number | null;
|
||||
* cache_write_tokens?: number | null;
|
||||
* cache_write_tokens_details?: {
|
||||
* ttl_30m_tokens?: number | null;
|
||||
* ttl_12h_tokens?: number | null;
|
||||
* } | null;
|
||||
* }} OpenAiCacheDetails
|
||||
*/
|
||||
|
||||
/**
|
||||
* OpenAI-shaped usage as it's metered. The prompt count reports cached reads
|
||||
* and cache writes inside it, so they come out of it and are priced once each
|
||||
* under their own keys: `cached_tokens` for reads, `cache_write_tokens` for
|
||||
* writes at the model's default lifetime and `cache_write_12h_tokens` for
|
||||
* writes at OpenAI's 12h lifetime. Without the per-lifetime breakdown every
|
||||
* write is a default-lifetime write: Chat Completions writes no 12h entries,
|
||||
* and Responses reports the breakdown whenever a request asked for them. Reads
|
||||
* the Chat Completions (`prompt_tokens`) and Responses (`input_tokens`)
|
||||
* spellings alike.
|
||||
*
|
||||
* @param {import('openai/resources/completions.mjs').CompletionUsage | undefined} usage
|
||||
* @param {{
|
||||
* prompt_tokens?: number;
|
||||
* completion_tokens?: number;
|
||||
* prompt_tokens_details?: OpenAiCacheDetails | null;
|
||||
* input_tokens?: number;
|
||||
* output_tokens?: number;
|
||||
* input_tokens_details?: OpenAiCacheDetails | null;
|
||||
* }
|
||||
* | undefined} usage
|
||||
*/
|
||||
export const splitCachedPrompt = (usage) => {
|
||||
const details = usage?.prompt_tokens_details;
|
||||
const details = usage?.prompt_tokens_details ?? usage?.input_tokens_details;
|
||||
const cachedTokens = details?.cached_tokens ?? 0;
|
||||
// Moonshot counts the prompt tokens it wrote to its prefix cache here;
|
||||
// they're billed at the model's write rate, not the plain prompt rate.
|
||||
// Moonshot and GPT-5.6+ count the prompt tokens they wrote to the prefix
|
||||
// cache here; they're billed at the model's write rate, not the prompt rate.
|
||||
const writeTokens =
|
||||
typeof details?.cache_write_tokens === 'number'
|
||||
? details.cache_write_tokens
|
||||
: undefined;
|
||||
const reported12h = details?.cache_write_tokens_details?.ttl_12h_tokens;
|
||||
const write12hTokens =
|
||||
typeof reported12h === 'number'
|
||||
? Math.min(reported12h, writeTokens ?? 0)
|
||||
: undefined;
|
||||
return {
|
||||
prompt_tokens:
|
||||
(usage?.prompt_tokens ?? 0) - cachedTokens - (writeTokens ?? 0),
|
||||
completion_tokens: usage?.completion_tokens ?? 0,
|
||||
(usage?.prompt_tokens ?? usage?.input_tokens ?? 0) -
|
||||
cachedTokens -
|
||||
(writeTokens ?? 0),
|
||||
completion_tokens:
|
||||
usage?.completion_tokens ?? usage?.output_tokens ?? 0,
|
||||
cached_tokens: cachedTokens,
|
||||
...(writeTokens !== undefined
|
||||
? { cache_write_tokens: writeTokens }
|
||||
? { cache_write_tokens: writeTokens - (write12hTokens ?? 0) }
|
||||
: {}),
|
||||
...(write12hTokens !== undefined
|
||||
? { cache_write_12h_tokens: write12hTokens }
|
||||
: {}),
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* The `{prompt_tokens, completion_tokens, cached_tokens, cache_write_tokens}`
|
||||
* shape every OpenAI-family `usage_calculator` already returns for billing,
|
||||
* mapped to the provider-neutral `UsageDetails` the end-of-stream/non-stream
|
||||
* result carries. `inputTokens`/`outputTokens` are the same numbers already
|
||||
* billed — this never introduces a new figure, only exposes the breakdown.
|
||||
* The `{prompt_tokens, completion_tokens, cached_tokens, cache_write_tokens,
|
||||
* cache_write_12h_tokens}` shape every OpenAI-family `usage_calculator` already
|
||||
* returns for billing, mapped to the provider-neutral `UsageDetails` the
|
||||
* end-of-stream/non-stream result carries. `inputTokens`/`outputTokens` are the
|
||||
* same numbers already billed — this never introduces a new figure, only
|
||||
* exposes the breakdown.
|
||||
*
|
||||
* @param {Record<string, number> | undefined | null} trackedUsage
|
||||
* @param {{ writeTtl?: '5m' | '30m' }} [opts] The lifetime `cache_write_tokens`
|
||||
* were written with: 5m unless the provider says otherwise (OpenAI's default
|
||||
* entries live 30m).
|
||||
* @returns {import('../types.js').UsageDetails | undefined}
|
||||
*/
|
||||
export const usageDetailsFromTrackedUsage = (trackedUsage) => {
|
||||
export const usageDetailsFromTrackedUsage = (
|
||||
trackedUsage,
|
||||
{ writeTtl = '5m' } = {},
|
||||
) => {
|
||||
if (!trackedUsage || typeof trackedUsage !== 'object') return undefined;
|
||||
// OpenRouter, Infron and Ollama track `{prompt, completion,
|
||||
// input_cache_read}` instead; any other shape is left to the driver.
|
||||
@@ -526,12 +569,19 @@ export const usageDetailsFromTrackedUsage = (trackedUsage) => {
|
||||
if (input === undefined && output === undefined) return undefined;
|
||||
const cacheRead =
|
||||
trackedUsage.cached_tokens ?? trackedUsage.input_cache_read;
|
||||
const cacheWrite = trackedUsage.cache_write_tokens;
|
||||
return {
|
||||
inputTokens: input ?? 0,
|
||||
outputTokens: output ?? 0,
|
||||
...(cacheRead ? { cacheReadTokens: cacheRead } : {}),
|
||||
...(trackedUsage.cache_write_tokens
|
||||
? { cacheWrite5mTokens: trackedUsage.cache_write_tokens }
|
||||
...(cacheWrite && writeTtl === '30m'
|
||||
? { cacheWrite30mTokens: cacheWrite }
|
||||
: {}),
|
||||
...(cacheWrite && writeTtl !== '30m'
|
||||
? { cacheWrite5mTokens: cacheWrite }
|
||||
: {}),
|
||||
...(trackedUsage.cache_write_12h_tokens
|
||||
? { cacheWrite12hTokens: trackedUsage.cache_write_12h_tokens }
|
||||
: {}),
|
||||
};
|
||||
};
|
||||
@@ -864,12 +914,16 @@ export const create_chat_stream_handler_responses_api =
|
||||
// Missing usage is left undefined rather than fed to the calculator —
|
||||
// see the sibling handler above, including why usage is reported
|
||||
// before the block flushes.
|
||||
let usageDetails;
|
||||
const usage = last_usage
|
||||
? usage_calculator({
|
||||
usage: last_usage,
|
||||
webSearchCalls,
|
||||
tool_usage: toolUsage,
|
||||
setUsageCosts: (c) => chatStream.setUsageCosts(c),
|
||||
setUsageDetails: (d) => {
|
||||
usageDetails = d;
|
||||
},
|
||||
})
|
||||
: undefined;
|
||||
chatStream.reportUsage(usage);
|
||||
@@ -877,7 +931,7 @@ export const create_chat_stream_handler_responses_api =
|
||||
reason:
|
||||
incompleteReason ?? (sawFunctionCall ? 'tool_use' : 'end_turn'),
|
||||
});
|
||||
const usageDetails = usageDetailsFromTrackedUsage(usage);
|
||||
usageDetails ??= usageDetailsFromTrackedUsage(usage);
|
||||
if (usageDetails) chatStream.setUsageDetails(usageDetails);
|
||||
|
||||
textblock.end();
|
||||
@@ -1015,6 +1069,7 @@ export const handle_completion_output = async (
|
||||
* webSearchCalls?: number;
|
||||
* tool_usage?: Record<string, unknown>;
|
||||
* setUsageCosts?: (costs: Record<string, number>) => void;
|
||||
* setUsageDetails?: (details: import('../types').UsageDetails) => void;
|
||||
* }) => unknown} params.usage_calculator
|
||||
* @param {() => Promise<void>} [params.finally_fn]
|
||||
* @returns {ReturnType<import('../types').IChatProvider['complete']>}
|
||||
@@ -1156,12 +1211,15 @@ export const handle_completion_output_responses_api = async ({
|
||||
setUsageCosts: (c) => {
|
||||
ret.usageCosts = c;
|
||||
},
|
||||
setUsageDetails: (d) => {
|
||||
ret.usageDetails = d;
|
||||
},
|
||||
})
|
||||
: {
|
||||
input_tokens: completion.usage.input_tokens,
|
||||
output_tokens: completion.usage.output_tokens,
|
||||
};
|
||||
if (usage_calculator) {
|
||||
if (usage_calculator && ret.usageDetails === undefined) {
|
||||
const usageDetails = usageDetailsFromTrackedUsage(ret.usage);
|
||||
if (usageDetails) ret.usageDetails = usageDetails;
|
||||
}
|
||||
|
||||
@@ -247,6 +247,40 @@ describe('usageDetailsFromTrackedUsage', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('labels cache writes by the lifetime the provider says it wrote them with', () => {
|
||||
expect(
|
||||
usageDetailsFromTrackedUsage(
|
||||
{
|
||||
prompt_tokens: 10,
|
||||
completion_tokens: 5,
|
||||
cached_tokens: 3,
|
||||
cache_write_tokens: 11,
|
||||
cache_write_12h_tokens: 6012,
|
||||
},
|
||||
{ writeTtl: '30m' },
|
||||
),
|
||||
).toEqual({
|
||||
inputTokens: 10,
|
||||
outputTokens: 5,
|
||||
cacheReadTokens: 3,
|
||||
cacheWrite30mTokens: 11,
|
||||
cacheWrite12hTokens: 6012,
|
||||
});
|
||||
// Zero counts, as OpenAI reports on a cache hit, add no field.
|
||||
expect(
|
||||
usageDetailsFromTrackedUsage(
|
||||
{
|
||||
prompt_tokens: 10,
|
||||
completion_tokens: 5,
|
||||
cached_tokens: 3,
|
||||
cache_write_tokens: 0,
|
||||
cache_write_12h_tokens: 0,
|
||||
},
|
||||
{ writeTtl: '30m' },
|
||||
),
|
||||
).toEqual({ inputTokens: 10, outputTokens: 5, cacheReadTokens: 3 });
|
||||
});
|
||||
|
||||
it('returns undefined for a missing/non-object usage', () => {
|
||||
expect(usageDetailsFromTrackedUsage(undefined)).toBeUndefined();
|
||||
expect(usageDetailsFromTrackedUsage(null)).toBeUndefined();
|
||||
@@ -962,6 +996,66 @@ describe('splitCachedPrompt', () => {
|
||||
cached_tokens: 3,
|
||||
});
|
||||
});
|
||||
|
||||
it('reads the Responses usage spelling', () => {
|
||||
expect(
|
||||
splitCachedPrompt({
|
||||
input_tokens: 6026,
|
||||
output_tokens: 5,
|
||||
input_tokens_details: { cached_tokens: 0, cache_write_tokens: 6023 },
|
||||
}),
|
||||
).toEqual({
|
||||
prompt_tokens: 3,
|
||||
completion_tokens: 5,
|
||||
cached_tokens: 0,
|
||||
cache_write_tokens: 6023,
|
||||
});
|
||||
});
|
||||
|
||||
it('splits 12h writes out of the write count when the breakdown is reported', () => {
|
||||
// One request can write both lifetimes: an explicit 12h breakpoint
|
||||
// plus the implicit 30m one OpenAI adds at the end of the prompt.
|
||||
expect(
|
||||
splitCachedPrompt({
|
||||
input_tokens: 6026,
|
||||
output_tokens: 5,
|
||||
input_tokens_details: {
|
||||
cached_tokens: 0,
|
||||
cache_write_tokens: 6023,
|
||||
cache_write_tokens_details: {
|
||||
ttl_12h_tokens: 6012,
|
||||
ttl_30m_tokens: 11,
|
||||
},
|
||||
},
|
||||
}),
|
||||
).toEqual({
|
||||
prompt_tokens: 3,
|
||||
completion_tokens: 5,
|
||||
cached_tokens: 0,
|
||||
cache_write_tokens: 11,
|
||||
cache_write_12h_tokens: 6012,
|
||||
});
|
||||
});
|
||||
|
||||
it('never reports more 12h writes than writes in total', () => {
|
||||
expect(
|
||||
splitCachedPrompt({
|
||||
input_tokens: 100,
|
||||
output_tokens: 1,
|
||||
input_tokens_details: {
|
||||
cached_tokens: 0,
|
||||
cache_write_tokens: 40,
|
||||
cache_write_tokens_details: { ttl_12h_tokens: 50 },
|
||||
},
|
||||
}),
|
||||
).toEqual({
|
||||
prompt_tokens: 60,
|
||||
completion_tokens: 1,
|
||||
cached_tokens: 0,
|
||||
cache_write_tokens: 0,
|
||||
cache_write_12h_tokens: 40,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// ── recorded usage costs ────────────────────────────────────────────
|
||||
|
||||
@@ -22,11 +22,14 @@ import { describe, expect, it } from 'vitest';
|
||||
import type { ICompleteArguments } from '../types.js';
|
||||
import {
|
||||
clampReasoningEffort,
|
||||
messagesHaveCacheBreakpointTtl,
|
||||
openAICompatParams,
|
||||
outputFormatFromResponseFormat,
|
||||
outputFormatFromResponsesText,
|
||||
outputFormatToResponseFormat,
|
||||
outputFormatToResponsesText,
|
||||
promptCacheOptionsToWire,
|
||||
promptCacheRetentionToWire,
|
||||
rejectStatefulResponsesFields,
|
||||
toolChoiceFromWire,
|
||||
toolChoiceToWire,
|
||||
@@ -334,3 +337,109 @@ describe('rejectStatefulResponsesFields', () => {
|
||||
).toThrow(/previous_response_id/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('promptCacheRetentionToWire', () => {
|
||||
it('spells the in-memory retention the way the upstreams expect', () => {
|
||||
expect(promptCacheRetentionToWire('in-memory')).toBe('in_memory');
|
||||
expect(promptCacheRetentionToWire('in_memory')).toBe('in_memory');
|
||||
expect(promptCacheRetentionToWire('24h')).toBe('24h');
|
||||
expect(promptCacheRetentionToWire(undefined)).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('promptCacheOptionsToWire', () => {
|
||||
it('forwards mode and ttl and nothing else', () => {
|
||||
expect(
|
||||
promptCacheOptionsToWire({
|
||||
mode: 'explicit',
|
||||
ttl: '12h',
|
||||
prewarm: true,
|
||||
comparison_response_id: 'resp_1',
|
||||
}),
|
||||
).toEqual({ mode: 'explicit', ttl: '12h' });
|
||||
expect(promptCacheOptionsToWire({ ttl: '30m' })).toEqual({
|
||||
ttl: '30m',
|
||||
});
|
||||
});
|
||||
|
||||
it('sends nothing for an absent or empty object', () => {
|
||||
expect(promptCacheOptionsToWire(undefined)).toBeUndefined();
|
||||
expect(promptCacheOptionsToWire(null)).toBeUndefined();
|
||||
expect(promptCacheOptionsToWire({})).toBeUndefined();
|
||||
expect(promptCacheOptionsToWire({ prewarm: true })).toBeUndefined();
|
||||
});
|
||||
|
||||
it('rejects a lifetime or mode OpenAI does not offer', () => {
|
||||
expect(() => promptCacheOptionsToWire({ ttl: '2h' })).toThrow(
|
||||
expect.objectContaining({ statusCode: 400 }),
|
||||
);
|
||||
expect(() => promptCacheOptionsToWire({ ttl: '24h' })).toThrow(
|
||||
/30m.*12h/,
|
||||
);
|
||||
expect(() => promptCacheOptionsToWire({ mode: 'auto' })).toThrow(
|
||||
/implicit.*explicit/,
|
||||
);
|
||||
expect(() => promptCacheOptionsToWire('12h')).toThrow(
|
||||
/must be an object/,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('messagesHaveCacheBreakpointTtl', () => {
|
||||
it('finds a breakpoint lifetime on any content part', () => {
|
||||
expect(
|
||||
messagesHaveCacheBreakpointTtl([
|
||||
{ role: 'user', content: 'plain' },
|
||||
{
|
||||
role: 'system',
|
||||
content: [
|
||||
{ type: 'text', text: 'a' },
|
||||
{
|
||||
type: 'text',
|
||||
text: 'b',
|
||||
prompt_cache_breakpoint: {
|
||||
mode: 'explicit',
|
||||
ttl: '30m',
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
]),
|
||||
).toBe(true);
|
||||
expect(
|
||||
messagesHaveCacheBreakpointTtl([
|
||||
{
|
||||
role: 'user',
|
||||
content: {
|
||||
type: 'text',
|
||||
text: 'a',
|
||||
prompt_cache_breakpoint: { ttl: '12h' },
|
||||
},
|
||||
},
|
||||
]),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it('ignores breakpoints without a lifetime and odd message shapes', () => {
|
||||
expect(
|
||||
messagesHaveCacheBreakpointTtl([
|
||||
{
|
||||
role: 'system',
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: 'a',
|
||||
prompt_cache_breakpoint: { mode: 'explicit' },
|
||||
},
|
||||
'bare string part',
|
||||
null,
|
||||
],
|
||||
},
|
||||
{ role: 'user', content: null },
|
||||
null,
|
||||
]),
|
||||
).toBe(false);
|
||||
expect(messagesHaveCacheBreakpointTtl(undefined)).toBe(false);
|
||||
expect(messagesHaveCacheBreakpointTtl('nope')).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -29,6 +29,7 @@ import { HttpError } from '../../../core/http/HttpError.js';
|
||||
import type {
|
||||
ICompleteArguments,
|
||||
OutputFormat,
|
||||
PromptCacheOptions,
|
||||
ReasoningEffort,
|
||||
ToolChoice,
|
||||
} from '../types.js';
|
||||
@@ -305,6 +306,80 @@ export const openAICompatParams = (
|
||||
return out;
|
||||
};
|
||||
|
||||
// -- prompt caching ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* The wire spelling of `prompt_cache_retention`. Puter also accepts the
|
||||
* hyphenated `in-memory`, which every OpenAI-shaped upstream rejects.
|
||||
*/
|
||||
export const promptCacheRetentionToWire = (
|
||||
retention: ICompleteArguments['prompt_cache_retention'],
|
||||
): 'in_memory' | '24h' | undefined =>
|
||||
retention === 'in-memory' ? 'in_memory' : retention;
|
||||
|
||||
const PROMPT_CACHE_MODES = new Set(['implicit', 'explicit']);
|
||||
const PROMPT_CACHE_TTLS = new Set(['30m', '12h']);
|
||||
|
||||
/**
|
||||
* `prompt_cache_options` as forwarded upstream: `mode` and `ttl` only, each
|
||||
* checked against the values OpenAI accepts, so a bad value fails here with a
|
||||
* clear message instead of as an upstream 400 the fallback loop may hide.
|
||||
* `comparison_response_id` names a stored response, which Puter never keeps,
|
||||
* and `prewarm` is dropped with it.
|
||||
*/
|
||||
export const promptCacheOptionsToWire = (
|
||||
options: unknown,
|
||||
): PromptCacheOptions | undefined => {
|
||||
if (options === undefined || options === null) return undefined;
|
||||
if (typeof options !== 'object' || Array.isArray(options)) {
|
||||
throw new HttpError(400, '`prompt_cache_options` must be an object', {
|
||||
legacyCode: 'bad_request',
|
||||
});
|
||||
}
|
||||
const { mode, ttl } = options as Record<string, unknown>;
|
||||
if (mode !== undefined && !PROMPT_CACHE_MODES.has(mode as string)) {
|
||||
throw new HttpError(
|
||||
400,
|
||||
'`prompt_cache_options.mode` must be `implicit` or `explicit`',
|
||||
{ legacyCode: 'bad_request' },
|
||||
);
|
||||
}
|
||||
if (ttl !== undefined && !PROMPT_CACHE_TTLS.has(ttl as string)) {
|
||||
throw new HttpError(
|
||||
400,
|
||||
'`prompt_cache_options.ttl` must be `30m` or `12h`',
|
||||
{ legacyCode: 'bad_request' },
|
||||
);
|
||||
}
|
||||
const wire: PromptCacheOptions = {
|
||||
...(mode !== undefined
|
||||
? { mode: mode as PromptCacheOptions['mode'] }
|
||||
: {}),
|
||||
...(ttl !== undefined ? { ttl: ttl as PromptCacheOptions['ttl'] } : {}),
|
||||
};
|
||||
return Object.keys(wire).length ? wire : undefined;
|
||||
};
|
||||
|
||||
/**
|
||||
* Whether any content part carries a `prompt_cache_breakpoint` with its own
|
||||
* `ttl`. Chat Completions rejects the field whatever its value; only the
|
||||
* Responses API honours a per-breakpoint lifetime.
|
||||
*/
|
||||
export const messagesHaveCacheBreakpointTtl = (messages: unknown): boolean =>
|
||||
Array.isArray(messages) &&
|
||||
messages.some((message) => {
|
||||
const content = (message as { content?: unknown } | null)?.content;
|
||||
const parts = Array.isArray(content) ? content : [content];
|
||||
return parts.some(
|
||||
(part) =>
|
||||
(
|
||||
part as {
|
||||
prompt_cache_breakpoint?: { ttl?: unknown } | null;
|
||||
} | null
|
||||
)?.prompt_cache_breakpoint?.ttl !== undefined,
|
||||
);
|
||||
});
|
||||
|
||||
// -- Responses server-side state ---------------------------------------------
|
||||
|
||||
const STATEFUL_RESPONSES_FIELDS = [
|
||||
|
||||
@@ -758,7 +758,10 @@ describe('ImageGenerationDriver.generate provider routing', () => {
|
||||
}),
|
||||
);
|
||||
|
||||
const reserveSpy = vi.spyOn(server.services.metering, 'reserveCredits');
|
||||
const reserveSpy = vi.spyOn(
|
||||
server.services.metering,
|
||||
'reserveCreditsIfAffordable',
|
||||
);
|
||||
const usageSpy = vi.spyOn(
|
||||
server.services.metering,
|
||||
'batchIncrementUsages',
|
||||
@@ -852,7 +855,7 @@ describe('ImageGenerationDriver.generate provider routing', () => {
|
||||
});
|
||||
const reserveSpy = vi.spyOn(
|
||||
server.services.metering,
|
||||
'reserveCredits',
|
||||
'reserveCreditsIfAffordable',
|
||||
);
|
||||
|
||||
// Cloudflare answers the scheduled refresh with new rates at once;
|
||||
@@ -1162,8 +1165,8 @@ describe('ImageGenerationDriver.generate Puter file inputs', () => {
|
||||
);
|
||||
|
||||
beforeEach(() => {
|
||||
vi.spyOn(server.services.metering, 'hasEnoughCredits').mockResolvedValue(
|
||||
true,
|
||||
vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable').mockImplementation((who, amount, opts) =>
|
||||
server.services.metering.reserveCredits(who, amount, opts),
|
||||
);
|
||||
});
|
||||
|
||||
@@ -1665,7 +1668,10 @@ it.each([undefined, null, '', ' ', 42, {}])(
|
||||
|
||||
describe('ImageGenerationDriver cost factor', () => {
|
||||
it('holds and meters at the AI cost factor', async () => {
|
||||
const reserveSpy = vi.spyOn(server.services.metering, 'reserveCredits');
|
||||
const reserveSpy = vi.spyOn(
|
||||
server.services.metering,
|
||||
'reserveCreditsIfAffordable',
|
||||
);
|
||||
const usageSpy = vi.spyOn(
|
||||
server.services.metering,
|
||||
'batchIncrementUsages',
|
||||
|
||||
@@ -72,7 +72,7 @@ vi.mock('openai', () => {
|
||||
// -- Test harness ----------------------------------------------------
|
||||
|
||||
let server: PuterServer;
|
||||
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
|
||||
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
|
||||
let batchIncrementUsagesSpy: MockInstance<
|
||||
MeteringService['batchIncrementUsages']
|
||||
>;
|
||||
@@ -107,7 +107,7 @@ const makeProvider = (
|
||||
beforeEach(() => {
|
||||
generateMock.mockReset();
|
||||
openAICtor.mockReset();
|
||||
hasCreditsSpy = vi.spyOn(server.services.metering, 'hasEnoughCredits');
|
||||
admitSpy = vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable');
|
||||
batchIncrementUsagesSpy = vi.spyOn(
|
||||
server.services.metering,
|
||||
'batchIncrementUsages',
|
||||
@@ -177,7 +177,7 @@ describe('BytePlusImageProvider.generate gates', () => {
|
||||
expect(result).toBe(
|
||||
'https://puter-sample-data.puter.site/image_example.png',
|
||||
);
|
||||
expect(hasCreditsSpy).not.toHaveBeenCalled();
|
||||
expect(admitSpy).not.toHaveBeenCalled();
|
||||
expect(generateMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
@@ -196,7 +196,7 @@ describe('BytePlusImageProvider.generate gates', () => {
|
||||
});
|
||||
|
||||
it('throws 402 BEFORE hitting ModelArk when the actor lacks credits', async () => {
|
||||
hasCreditsSpy.mockResolvedValueOnce(false);
|
||||
admitSpy.mockResolvedValueOnce(null);
|
||||
await expect(
|
||||
withTestActor(() => makeProvider().generate({ prompt: 'hi' })),
|
||||
).rejects.toMatchObject({ statusCode: 402 });
|
||||
@@ -713,7 +713,7 @@ it.each([
|
||||
}),
|
||||
),
|
||||
).rejects.toMatchObject({ statusCode: 400, legacyCode: 'bad_request' });
|
||||
expect(hasCreditsSpy).not.toHaveBeenCalled();
|
||||
expect(admitSpy).not.toHaveBeenCalled();
|
||||
expect(generateMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
|
||||
@@ -73,7 +73,7 @@ let server: PuterServer;
|
||||
let globalFetchSpy: MockInstance<typeof fetch>;
|
||||
let fetchSpy: Mock<typeof fetch>;
|
||||
let modelSearchFetch: Mock<typeof fetch>;
|
||||
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
|
||||
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
|
||||
let batchIncrementUsagesSpy: MockInstance<
|
||||
MeteringService['batchIncrementUsages']
|
||||
>;
|
||||
@@ -123,7 +123,7 @@ beforeEach(() => {
|
||||
? modelSearchFetch(input, init)
|
||||
: fetchSpy(input, init),
|
||||
);
|
||||
hasCreditsSpy = vi.spyOn(server.services.metering, 'hasEnoughCredits');
|
||||
admitSpy = vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable');
|
||||
batchIncrementUsagesSpy = vi.spyOn(
|
||||
server.services.metering,
|
||||
'batchIncrementUsages',
|
||||
@@ -188,7 +188,7 @@ describe('CloudflareImageProvider.generate test_mode', () => {
|
||||
expect(result).toBe(
|
||||
'https://puter-sample-data.puter.site/image_example.png',
|
||||
);
|
||||
expect(hasCreditsSpy).not.toHaveBeenCalled();
|
||||
expect(admitSpy).not.toHaveBeenCalled();
|
||||
expect(fetchSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -210,7 +210,7 @@ describe('CloudflareImageProvider.generate argument validation', () => {
|
||||
describe('CloudflareImageProvider.generate credit gate', () => {
|
||||
it('throws 402 BEFORE hitting Cloudflare when actor lacks credits', async () => {
|
||||
const provider = makeProvider();
|
||||
hasCreditsSpy.mockResolvedValueOnce(false);
|
||||
admitSpy.mockResolvedValueOnce(null);
|
||||
|
||||
await expect(
|
||||
withTestActor(() => provider.generate({ prompt: 'hi' })),
|
||||
@@ -240,7 +240,7 @@ describe('CloudflareImageProvider.generate credit gate', () => {
|
||||
actor,
|
||||
);
|
||||
|
||||
const [, estimate] = hasCreditsSpy.mock.calls[0]!;
|
||||
const [, estimate] = admitSpy.mock.calls[0]!;
|
||||
expect(estimate).toBeGreaterThan(0);
|
||||
expect(heldDuringCall).toBe(Math.ceil(estimate));
|
||||
// The hold stays until the usage write lands.
|
||||
@@ -708,7 +708,7 @@ describe('Cloudflare legacy image option', () => {
|
||||
legacyCode: 'bad_request',
|
||||
});
|
||||
expect(fetchSpy).not.toHaveBeenCalled();
|
||||
expect(hasCreditsSpy).not.toHaveBeenCalled();
|
||||
expect(admitSpy).not.toHaveBeenCalled();
|
||||
},
|
||||
);
|
||||
|
||||
@@ -726,7 +726,7 @@ describe('Cloudflare legacy image option', () => {
|
||||
message: 'Invalid input image (empty or not base64)',
|
||||
});
|
||||
expect(fetchSpy).not.toHaveBeenCalled();
|
||||
expect(hasCreditsSpy).not.toHaveBeenCalled();
|
||||
expect(admitSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -823,12 +823,12 @@ describe('Cloudflare priced legacy models', () => {
|
||||
imageSize: { kind: 'pixels', w: 512, h: 512 },
|
||||
}));
|
||||
expect(JSON.parse(fetchSpy.mock.calls[0]![1]!.body as string)).toMatchObject({ image: [1, 2, 3], mask: [4, 5, 6], num_steps: 20, width: 512, height: 512 });
|
||||
expect(hasCreditsSpy).toHaveBeenCalledWith(expect.anything(), 0);
|
||||
expect(admitSpy).toHaveBeenCalledWith(expect.anything(), 0, undefined);
|
||||
});
|
||||
|
||||
it('rejects missing inpainting inputs before credit checks', async () => {
|
||||
await expect(withTestActor(() => makeProvider().generate({ model: '@cf/runwayml/stable-diffusion-v1-5-inpainting', prompt: 'A cup' }))).rejects.toMatchObject({ statusCode: 400 });
|
||||
expect(hasCreditsSpy).not.toHaveBeenCalled();
|
||||
expect(admitSpy).not.toHaveBeenCalled();
|
||||
expect(fetchSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
@@ -844,7 +844,7 @@ describe('Cloudflare priced legacy models', () => {
|
||||
it('charges Lucid at the price returned by the model API', async () => {
|
||||
fetchSpy.mockResolvedValueOnce(new Response(new Uint8Array([1, 2, 3]), { headers: { 'content-type': 'image/png' } }));
|
||||
await withTestActor(() => makeProvider().generate({ model: '@cf/leonardo/lucid-origin', prompt: 'A cup', imageSize: { kind: 'pixels', w: 512, h: 512 }, steps: 1 }));
|
||||
expect(hasCreditsSpy).toHaveBeenCalledWith(expect.anything(), 713200);
|
||||
expect(admitSpy).toHaveBeenCalledWith(expect.anything(), 713200, undefined);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -897,9 +897,10 @@ describe('Cloudflare model-search pricing', () => {
|
||||
await runPhoenix(provider);
|
||||
|
||||
expect(listed?.costs).toEqual({ tile_512: 1_000_000, step: 20_200 });
|
||||
expect(hasCreditsSpy).toHaveBeenCalledWith(
|
||||
expect(admitSpy).toHaveBeenCalledWith(
|
||||
expect.anything(),
|
||||
4 * 1_000_000 + 10 * 20_200,
|
||||
undefined,
|
||||
);
|
||||
expect(batchIncrementUsagesSpy.mock.calls[0]![1]).toEqual([
|
||||
expect.objectContaining({
|
||||
@@ -930,9 +931,10 @@ describe('Cloudflare model-search pricing', () => {
|
||||
await runPhoenix(makeProvider());
|
||||
|
||||
expect(modelSearchFetch).not.toHaveBeenCalled();
|
||||
expect(hasCreditsSpy).toHaveBeenCalledWith(
|
||||
expect(admitSpy).toHaveBeenCalledWith(
|
||||
expect.anything(),
|
||||
4 * 583_000 + 10 * 11_000,
|
||||
undefined,
|
||||
);
|
||||
});
|
||||
|
||||
@@ -955,7 +957,7 @@ describe('Cloudflare model-search pricing', () => {
|
||||
await refresh;
|
||||
await runPhoenix(provider);
|
||||
|
||||
expect(hasCreditsSpy.mock.calls.map(([, cost]) => cost)).toEqual([
|
||||
expect(admitSpy.mock.calls.map(([, cost]) => cost)).toEqual([
|
||||
2_442_000, 4_202_000,
|
||||
]);
|
||||
});
|
||||
@@ -977,9 +979,10 @@ describe('Cloudflare model-search pricing', () => {
|
||||
await runPhoenix(provider);
|
||||
|
||||
expect(modelSearchFetch).toHaveBeenCalledTimes(2);
|
||||
expect(hasCreditsSpy).toHaveBeenCalledWith(
|
||||
expect(admitSpy).toHaveBeenCalledWith(
|
||||
expect.anything(),
|
||||
4_202_000,
|
||||
undefined,
|
||||
);
|
||||
});
|
||||
|
||||
@@ -1015,7 +1018,7 @@ describe('Cloudflare model-search pricing', () => {
|
||||
steps: 10,
|
||||
} as never),
|
||||
);
|
||||
return hasCreditsSpy.mock.lastCall![1];
|
||||
return admitSpy.mock.lastCall![1];
|
||||
};
|
||||
const settle = () => new Promise((resolve) => setImmediate(resolve));
|
||||
const OLD = { tile_512: 583_000, step: 11_000 };
|
||||
@@ -1069,9 +1072,10 @@ describe('Cloudflare model-search pricing', () => {
|
||||
}),
|
||||
);
|
||||
expect(modelSearchFetch).toHaveBeenCalledOnce();
|
||||
expect(hasCreditsSpy).toHaveBeenCalledWith(
|
||||
expect(admitSpy).toHaveBeenCalledWith(
|
||||
expect.anything(),
|
||||
4 * 5280 + 4 * 10560,
|
||||
undefined,
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
@@ -82,7 +82,7 @@ vi.mock('../../inputImage.js', async (orig) => ({
|
||||
// ── Test harness ────────────────────────────────────────────────────
|
||||
|
||||
let server: PuterServer;
|
||||
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
|
||||
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
|
||||
let incrementUsageSpy: MockInstance<MeteringService['incrementUsage']>;
|
||||
let batchIncrementUsagesSpy: MockInstance<
|
||||
MeteringService['batchIncrementUsages']
|
||||
@@ -111,7 +111,7 @@ beforeEach(() => {
|
||||
generateContentMock.mockReset();
|
||||
toBase64DataUriMock.mockReset();
|
||||
googleAICtor.mockReset();
|
||||
hasCreditsSpy = vi.spyOn(server.services.metering, 'hasEnoughCredits');
|
||||
admitSpy = vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable');
|
||||
incrementUsageSpy = vi.spyOn(server.services.metering, 'incrementUsage');
|
||||
batchIncrementUsagesSpy = vi.spyOn(
|
||||
server.services.metering,
|
||||
@@ -167,7 +167,7 @@ describe('GeminiImageProvider.generate test_mode', () => {
|
||||
expect(result).toBe(
|
||||
'https://puter-sample-data.puter.site/image_example.png',
|
||||
);
|
||||
expect(hasCreditsSpy).not.toHaveBeenCalled();
|
||||
expect(admitSpy).not.toHaveBeenCalled();
|
||||
expect(generateContentMock).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -219,7 +219,7 @@ describe('GeminiImageProvider.generate argument validation', () => {
|
||||
legacyCode: 'bad_request',
|
||||
message: expect.stringContaining('Unsupported quality tier'),
|
||||
});
|
||||
expect(hasCreditsSpy).not.toHaveBeenCalled();
|
||||
expect(admitSpy).not.toHaveBeenCalled();
|
||||
expect(generateContentMock).not.toHaveBeenCalled();
|
||||
},
|
||||
);
|
||||
@@ -427,7 +427,7 @@ describe('GeminiImageProvider.generate Flash path (generateContent)', () => {
|
||||
|
||||
it('throws 402 BEFORE hitting Gemini when actor lacks credits', async () => {
|
||||
const provider = makeProvider();
|
||||
hasCreditsSpy.mockResolvedValueOnce(false);
|
||||
admitSpy.mockResolvedValueOnce(null);
|
||||
|
||||
await expect(
|
||||
withTestActor(() =>
|
||||
@@ -466,7 +466,7 @@ describe('GeminiImageProvider.generate Flash path (generateContent)', () => {
|
||||
actor,
|
||||
);
|
||||
|
||||
const [, estimate] = hasCreditsSpy.mock.calls[0]!;
|
||||
const [, estimate] = admitSpy.mock.calls[0]!;
|
||||
expect(estimate).toBeGreaterThan(0);
|
||||
expect(heldDuringCall).toBe(Math.ceil(estimate));
|
||||
// The hold stays until the usage write lands.
|
||||
@@ -643,7 +643,7 @@ describe('Gemini Nano Banana image generation', () => {
|
||||
const request = generateContentMock.mock.calls[0][0];
|
||||
expect(request.config.imageConfig.imageSize).toBe('1K');
|
||||
// Input + 1120 image tokens at $30/1M + 1000 text tokens at $7.50/1M.
|
||||
expect(hasCreditsSpy).toHaveBeenCalledWith(expect.anything(), 4_194_450);
|
||||
expect(admitSpy).toHaveBeenCalledWith(expect.anything(), 4_194_450, undefined);
|
||||
expect(batchIncrementUsagesSpy).toHaveBeenCalledWith(
|
||||
expect.anything(),
|
||||
expect.arrayContaining([
|
||||
|
||||
@@ -50,7 +50,7 @@ import { KlingImageProvider } from './KlingImageProvider.js';
|
||||
|
||||
let server: PuterServer;
|
||||
let fetchSpy: MockInstance<typeof fetch>;
|
||||
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
|
||||
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
|
||||
let incrementUsageSpy: MockInstance<MeteringService['incrementUsage']>;
|
||||
|
||||
beforeAll(async () => {
|
||||
@@ -73,7 +73,7 @@ const makeProvider = () =>
|
||||
|
||||
beforeEach(() => {
|
||||
fetchSpy = vi.spyOn(globalThis, 'fetch') as MockInstance<typeof fetch>;
|
||||
hasCreditsSpy = vi.spyOn(server.services.metering, 'hasEnoughCredits');
|
||||
admitSpy = vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable');
|
||||
incrementUsageSpy = vi.spyOn(server.services.metering, 'incrementUsage');
|
||||
incrementUsageSpy.mockResolvedValue({} as never);
|
||||
});
|
||||
@@ -318,7 +318,7 @@ describe('KlingImageProvider billing', () => {
|
||||
});
|
||||
|
||||
it('rejects before calling Kling when credits run short', async () => {
|
||||
hasCreditsSpy.mockResolvedValueOnce(false);
|
||||
admitSpy.mockResolvedValueOnce(null);
|
||||
await expect(generate({})).rejects.toMatchObject({ statusCode: 402 });
|
||||
expect(fetchSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
@@ -97,7 +97,7 @@ vi.mock('../../inputImage.js', async (orig) => ({
|
||||
// ── Test harness ────────────────────────────────────────────────────
|
||||
|
||||
let server: PuterServer;
|
||||
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
|
||||
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
|
||||
let batchIncrementUsagesSpy: MockInstance<
|
||||
MeteringService['batchIncrementUsages']
|
||||
>;
|
||||
@@ -126,7 +126,7 @@ beforeEach(() => {
|
||||
editMock.mockReset();
|
||||
fetchImageAsBase64Mock.mockReset();
|
||||
openAICtor.mockReset();
|
||||
hasCreditsSpy = vi.spyOn(server.services.metering, 'hasEnoughCredits');
|
||||
admitSpy = vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable');
|
||||
batchIncrementUsagesSpy = vi.spyOn(
|
||||
server.services.metering,
|
||||
'batchIncrementUsages',
|
||||
@@ -195,7 +195,7 @@ describe('OpenAiImageProvider.generate test_mode', () => {
|
||||
expect(result).toBe(
|
||||
'https://puter-sample-data.puter.site/image_example.png',
|
||||
);
|
||||
expect(hasCreditsSpy).not.toHaveBeenCalled();
|
||||
expect(admitSpy).not.toHaveBeenCalled();
|
||||
expect(generateMock).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -253,7 +253,8 @@ describe('OpenAiImageProvider.generate credit gate', () => {
|
||||
});
|
||||
let blocked = false;
|
||||
vi.spyOn(redis, 'meterIncr').mockImplementation(async (...args) => {
|
||||
if (!blocked && args[5].includes(`:actor:${actor.user!.uuid}:`)) {
|
||||
const actorKey = `:actor:${actor.user!.uuid}:`;
|
||||
if (!blocked && args.some((arg) => arg.includes(actorKey))) {
|
||||
blocked = true;
|
||||
await held;
|
||||
}
|
||||
@@ -287,7 +288,7 @@ describe('OpenAiImageProvider.generate credit gate', () => {
|
||||
|
||||
it('throws 402 BEFORE hitting OpenAI when actor lacks credits', async () => {
|
||||
const provider = makeProvider();
|
||||
hasCreditsSpy.mockResolvedValueOnce(false);
|
||||
admitSpy.mockResolvedValueOnce(null);
|
||||
await expect(
|
||||
withTestActor(() =>
|
||||
provider.generate({
|
||||
@@ -323,7 +324,7 @@ describe('OpenAiImageProvider.generate credit gate', () => {
|
||||
actor,
|
||||
);
|
||||
|
||||
const [, estimate] = hasCreditsSpy.mock.calls[0]!;
|
||||
const [, estimate] = admitSpy.mock.calls[0]!;
|
||||
expect(estimate).toBeGreaterThan(0);
|
||||
expect(heldDuringCall).toBe(Math.ceil(estimate));
|
||||
// The hold stays until the usage write lands.
|
||||
@@ -727,8 +728,8 @@ describe('OpenAiImageProvider gpt-image-2.5 models', () => {
|
||||
);
|
||||
|
||||
// Latent factor 48 vs 24 at the medium tier.
|
||||
const gptImage2Estimate = hasCreditsSpy.mock.calls[0]![1];
|
||||
const gptImage25Estimate = hasCreditsSpy.mock.calls[1]![1];
|
||||
const gptImage2Estimate = admitSpy.mock.calls[0]![1];
|
||||
const gptImage25Estimate = admitSpy.mock.calls[1]![1];
|
||||
expect(gptImage25Estimate).toBeLessThan(gptImage2Estimate);
|
||||
});
|
||||
|
||||
@@ -753,8 +754,8 @@ describe('OpenAiImageProvider gpt-image-2.5 models', () => {
|
||||
}),
|
||||
);
|
||||
|
||||
const highEstimate = hasCreditsSpy.mock.calls[0]![1];
|
||||
const maxEstimate = hasCreditsSpy.mock.calls[1]![1];
|
||||
const highEstimate = admitSpy.mock.calls[0]![1];
|
||||
const maxEstimate = admitSpy.mock.calls[1]![1];
|
||||
expect(maxEstimate).toBeGreaterThan(highEstimate);
|
||||
});
|
||||
});
|
||||
|
||||
+14
-9
@@ -84,7 +84,7 @@ vi.mock('../../../../util/secureHttp.js', () => ({
|
||||
// ── Test harness ────────────────────────────────────────────────────
|
||||
|
||||
let server: PuterServer;
|
||||
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
|
||||
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
|
||||
let incrementUsageSpy: MockInstance<MeteringService['incrementUsage']>;
|
||||
let batchIncrementUsagesSpy: MockInstance<
|
||||
MeteringService['batchIncrementUsages']
|
||||
@@ -115,7 +115,7 @@ beforeEach(() => {
|
||||
cancelMock.mockReset();
|
||||
replicateCtor.mockReset();
|
||||
secureFetchMock.mockReset();
|
||||
hasCreditsSpy = vi.spyOn(server.services.metering, 'hasEnoughCredits');
|
||||
admitSpy = vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable');
|
||||
incrementUsageSpy = vi.spyOn(server.services.metering, 'incrementUsage');
|
||||
batchIncrementUsagesSpy = vi.spyOn(
|
||||
server.services.metering,
|
||||
@@ -177,7 +177,7 @@ describe('ReplicateImageGenerationProvider.generate test_mode', () => {
|
||||
expect(result).toBe(
|
||||
'https://puter-sample-data.puter.site/image_example.png',
|
||||
);
|
||||
expect(hasCreditsSpy).not.toHaveBeenCalled();
|
||||
expect(admitSpy).not.toHaveBeenCalled();
|
||||
expect(createPredictionMock).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -199,7 +199,7 @@ describe('ReplicateImageGenerationProvider.generate argument validation', () =>
|
||||
describe('ReplicateImageGenerationProvider.generate credit gate', () => {
|
||||
it('throws 402 BEFORE hitting Replicate when actor lacks credits', async () => {
|
||||
const provider = makeProvider();
|
||||
hasCreditsSpy.mockResolvedValueOnce(false);
|
||||
admitSpy.mockResolvedValueOnce(null);
|
||||
await expect(
|
||||
withTestActor(() => provider.generate({ prompt: 'hi' })),
|
||||
).rejects.toMatchObject({ statusCode: 402 });
|
||||
@@ -255,7 +255,10 @@ describe('ReplicateImageGenerationProvider.generate credit gate', () => {
|
||||
it('holds credits for longer than a prediction can run', async () => {
|
||||
const provider = makeProvider();
|
||||
const actor = holdUser('ttl');
|
||||
const reserveSpy = vi.spyOn(server.services.metering, 'reserveCredits');
|
||||
const reserveSpy = vi.spyOn(
|
||||
server.services.metering,
|
||||
'reserveCreditsIfAffordable',
|
||||
);
|
||||
createPredictionMock.mockResolvedValueOnce({
|
||||
id: 'ttl',
|
||||
status: 'succeeded',
|
||||
@@ -274,7 +277,9 @@ describe('ReplicateImageGenerationProvider.generate credit gate', () => {
|
||||
const provider = makeProvider();
|
||||
const actor = holdUser('surcharge');
|
||||
secureFetchMock.mockResolvedValueOnce(new Response('not-an-image'));
|
||||
hasCreditsSpy.mockResolvedValueOnce(true).mockResolvedValueOnce(false);
|
||||
admitSpy.mockImplementationOnce((who, amount, opts) =>
|
||||
server.services.metering.reserveCredits(who, amount, opts),
|
||||
).mockResolvedValueOnce(false);
|
||||
|
||||
await expect(
|
||||
withTestActor(
|
||||
@@ -950,7 +955,7 @@ it('does not start a prediction for an already-aborted request', async () => {
|
||||
),
|
||||
).rejects.toMatchObject({ statusCode: 400, legacyCode: 'client_aborted' });
|
||||
expect(createPredictionMock).not.toHaveBeenCalled();
|
||||
expect(hasCreditsSpy).not.toHaveBeenCalled();
|
||||
expect(admitSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('stops polling and cancels at the prediction deadline', async () => {
|
||||
@@ -1192,12 +1197,12 @@ it('rejects more than ten input images before fetching or pricing any', async ()
|
||||
),
|
||||
).rejects.toMatchObject({ statusCode: 400, legacyCode: 'bad_request' });
|
||||
expect(secureFetchMock).not.toHaveBeenCalled();
|
||||
expect(hasCreditsSpy).not.toHaveBeenCalled();
|
||||
expect(admitSpy).not.toHaveBeenCalled();
|
||||
expect(createPredictionMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('checks credits on the output-only estimate before fetching input images', async () => {
|
||||
hasCreditsSpy.mockResolvedValueOnce(false);
|
||||
admitSpy.mockResolvedValueOnce(null);
|
||||
await expect(
|
||||
withTestActor(() =>
|
||||
makeProvider().generate({
|
||||
|
||||
@@ -71,7 +71,7 @@ vi.mock('together-ai', () => {
|
||||
// ── Test harness ────────────────────────────────────────────────────
|
||||
|
||||
let server: PuterServer;
|
||||
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
|
||||
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
|
||||
let incrementUsageSpy: MockInstance<MeteringService['incrementUsage']>;
|
||||
|
||||
beforeAll(async () => {
|
||||
@@ -96,7 +96,7 @@ const makeProvider = () =>
|
||||
beforeEach(() => {
|
||||
generateMock.mockReset();
|
||||
togetherCtor.mockReset();
|
||||
hasCreditsSpy = vi.spyOn(server.services.metering, 'hasEnoughCredits');
|
||||
admitSpy = vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable');
|
||||
incrementUsageSpy = vi.spyOn(server.services.metering, 'incrementUsage');
|
||||
});
|
||||
|
||||
@@ -158,7 +158,7 @@ describe('TogetherImageProvider.generate test_mode', () => {
|
||||
expect(result).toBe(
|
||||
'https://puter-sample-data.puter.site/image_example.png',
|
||||
);
|
||||
expect(hasCreditsSpy).not.toHaveBeenCalled();
|
||||
expect(admitSpy).not.toHaveBeenCalled();
|
||||
expect(generateMock).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -189,7 +189,7 @@ describe('TogetherImageProvider.generate argument validation', () => {
|
||||
describe('TogetherImageProvider.generate credit gate', () => {
|
||||
it('throws 402 BEFORE hitting Together when actor lacks credits', async () => {
|
||||
const provider = makeProvider();
|
||||
hasCreditsSpy.mockResolvedValueOnce(false);
|
||||
admitSpy.mockResolvedValueOnce(null);
|
||||
|
||||
await expect(
|
||||
withTestActor(() =>
|
||||
@@ -760,7 +760,7 @@ describe('Together supported sizes and references', () => {
|
||||
width: 1024,
|
||||
height: 1024,
|
||||
});
|
||||
expect(Number.isFinite(hasCreditsSpy.mock.calls[0][1])).toBe(true);
|
||||
expect(Number.isFinite(admitSpy.mock.calls[0][1])).toBe(true);
|
||||
});
|
||||
|
||||
it('resolves provider aliases without changing the selected model', async () => {
|
||||
@@ -790,7 +790,7 @@ describe('Together supported sizes and references', () => {
|
||||
}),
|
||||
),
|
||||
).rejects.toMatchObject({ statusCode: 400 });
|
||||
expect(hasCreditsSpy).not.toHaveBeenCalled();
|
||||
expect(admitSpy).not.toHaveBeenCalled();
|
||||
expect(generateMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
@@ -859,7 +859,7 @@ it.each(['3K', 'unknown', ' 5k '])(
|
||||
}),
|
||||
),
|
||||
).rejects.toMatchObject({ statusCode: 400, legacyCode: 'bad_request' });
|
||||
expect(hasCreditsSpy).not.toHaveBeenCalled();
|
||||
expect(admitSpy).not.toHaveBeenCalled();
|
||||
expect(generateMock).not.toHaveBeenCalled();
|
||||
},
|
||||
);
|
||||
@@ -895,7 +895,7 @@ describe('Together current API prices', () => {
|
||||
input_image: 'https://example.com/reference.png',
|
||||
}),
|
||||
);
|
||||
expect(hasCreditsSpy).toHaveBeenCalledWith(expect.anything(), 4685000);
|
||||
expect(admitSpy).toHaveBeenCalledWith(expect.anything(), 4685000, undefined);
|
||||
expect(incrementUsageSpy).toHaveBeenCalledWith(
|
||||
expect.anything(),
|
||||
'togetherai:google/flash-image-3.1:0.5K',
|
||||
|
||||
@@ -85,7 +85,7 @@ vi.mock('openai', () => {
|
||||
// ── Test harness ────────────────────────────────────────────────────
|
||||
|
||||
let server: PuterServer;
|
||||
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
|
||||
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
|
||||
let batchIncrementUsagesSpy: MockInstance<
|
||||
MeteringService['batchIncrementUsages']
|
||||
>;
|
||||
@@ -115,7 +115,7 @@ beforeEach(() => {
|
||||
openAICtor.mockReset();
|
||||
axiosGetMock.mockReset();
|
||||
axiosGetMock.mockRejectedValue(new Error('Catalog unavailable'));
|
||||
hasCreditsSpy = vi.spyOn(server.services.metering, 'hasEnoughCredits');
|
||||
admitSpy = vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable');
|
||||
batchIncrementUsagesSpy = vi.spyOn(
|
||||
server.services.metering,
|
||||
'batchIncrementUsages',
|
||||
@@ -182,7 +182,7 @@ describe('XAIImageProvider.generate test_mode', () => {
|
||||
expect(result).toBe(
|
||||
'https://puter-sample-data.puter.site/image_example.png',
|
||||
);
|
||||
expect(hasCreditsSpy).not.toHaveBeenCalled();
|
||||
expect(admitSpy).not.toHaveBeenCalled();
|
||||
expect(generateMock).not.toHaveBeenCalled();
|
||||
expect(batchIncrementUsagesSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
@@ -231,7 +231,7 @@ describe('XAIImageProvider.generate argument validation', () => {
|
||||
describe('XAIImageProvider.generate credit gate', () => {
|
||||
it('throws 402 BEFORE hitting xAI when actor lacks credits', async () => {
|
||||
const provider = makeProvider();
|
||||
hasCreditsSpy.mockResolvedValueOnce(false);
|
||||
admitSpy.mockResolvedValueOnce(null);
|
||||
|
||||
await expect(
|
||||
withTestActor(() =>
|
||||
@@ -718,7 +718,7 @@ describe('XAIImageProvider quality and reference limits', () => {
|
||||
}),
|
||||
),
|
||||
).rejects.toMatchObject({ statusCode: 400 });
|
||||
expect(hasCreditsSpy).not.toHaveBeenCalled();
|
||||
expect(admitSpy).not.toHaveBeenCalled();
|
||||
expect(postMock).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -746,7 +746,7 @@ describe('xAI option validation', () => {
|
||||
makeProvider().generate({ prompt: 'hi', ...options } as never),
|
||||
),
|
||||
).rejects.toMatchObject({ statusCode: 400 });
|
||||
expect(hasCreditsSpy).not.toHaveBeenCalled();
|
||||
expect(admitSpy).not.toHaveBeenCalled();
|
||||
expect(generateMock).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -167,7 +167,7 @@ const sentRegions = () =>
|
||||
|
||||
let server: PuterServer;
|
||||
let driver: OCRDriver;
|
||||
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
|
||||
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
|
||||
let incrementUsageSpy: MockInstance<MeteringService['incrementUsage']>;
|
||||
|
||||
beforeAll(async () => {
|
||||
@@ -199,7 +199,7 @@ beforeEach(() => {
|
||||
mistralCtor.mockReset();
|
||||
// Spy on metering — keep the real impl so its recording side runs,
|
||||
// but capture calls so per-test assertions can inspect them.
|
||||
hasCreditsSpy = vi.spyOn(server.services.metering, 'hasEnoughCredits');
|
||||
admitSpy = vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable');
|
||||
incrementUsageSpy = vi.spyOn(server.services.metering, 'incrementUsage');
|
||||
});
|
||||
|
||||
@@ -335,7 +335,7 @@ describe('OCRDriver.recognize (aws-textract)', () => {
|
||||
};
|
||||
|
||||
it('throws 402 when the actor does not have enough credits', async () => {
|
||||
hasCreditsSpy.mockResolvedValueOnce(false);
|
||||
admitSpy.mockResolvedValueOnce(null);
|
||||
const { actor } = await makeUser();
|
||||
|
||||
await expect(
|
||||
@@ -546,7 +546,7 @@ describe('OCRDriver.recognize (aws-textract)', () => {
|
||||
describe('regional pricing', () => {
|
||||
const onePage = { Blocks: [{ BlockType: 'PAGE' }] };
|
||||
const checkedCosts = () =>
|
||||
hasCreditsSpy.mock.calls.map(([, cost]) => cost);
|
||||
admitSpy.mock.calls.map(([, cost]) => cost);
|
||||
const textractCharges = () =>
|
||||
incrementUsageSpy.mock.calls
|
||||
.filter(([, type]) => type === TEXTRACT_USAGE)
|
||||
@@ -719,7 +719,7 @@ describe('OCRDriver.recognize (mistral)', () => {
|
||||
};
|
||||
|
||||
it('throws 402 when the actor does not have enough credits', async () => {
|
||||
hasCreditsSpy.mockResolvedValueOnce(false);
|
||||
admitSpy.mockResolvedValueOnce(null);
|
||||
const { actor } = await makeUser();
|
||||
|
||||
await expect(
|
||||
@@ -1033,7 +1033,7 @@ describe('OCRDriver.recognize (mistral)', () => {
|
||||
const pageType = 'mistral-ocr:mistral-ocr-4-1:page';
|
||||
const annotationType = 'mistral-ocr:mistral-ocr-4-1:annotations:page';
|
||||
// The pre-flight covers the page plus its annotation.
|
||||
expect(hasCreditsSpy.mock.calls[0]![1]).toBe(
|
||||
expect(admitSpy.mock.calls[0]![1]).toBe(
|
||||
OCR_COSTS[pageType] + OCR_COSTS[annotationType],
|
||||
);
|
||||
const ocrCalls = incrementUsageSpy.mock.calls.filter(
|
||||
@@ -1080,7 +1080,7 @@ describe('OCRDriver credit pre-flight', () => {
|
||||
const pageType = 'mistral-ocr:mistral-ocr-4-1:page';
|
||||
const perPage = OCR_COSTS[pageType];
|
||||
const pdfSource = (buffer: Buffer) => dataUrl(buffer, 'application/pdf');
|
||||
const checkedCosts = () => hasCreditsSpy.mock.calls.map(([, cost]) => cost);
|
||||
const checkedCosts = () => admitSpy.mock.calls.map(([, cost]) => cost);
|
||||
const outstandingHolds = (actor: Actor) =>
|
||||
server.stores.creditHold.outstanding(actor.user.uuid!);
|
||||
|
||||
@@ -1171,8 +1171,8 @@ describe('OCRDriver credit pre-flight', () => {
|
||||
),
|
||||
).rejects.toMatchObject({ statusCode: 402 });
|
||||
|
||||
// The estimate/hold path (which calls hasEnoughCredits) never ran.
|
||||
expect(hasCreditsSpy).not.toHaveBeenCalled();
|
||||
// The estimate/hold path never ran.
|
||||
expect(admitSpy).not.toHaveBeenCalled();
|
||||
expect(mistralOcrProcessMock).not.toHaveBeenCalled();
|
||||
noUsage.mockRestore();
|
||||
});
|
||||
|
||||
@@ -72,7 +72,7 @@ vi.mock('undici', async (importOriginal) => ({
|
||||
let server: PuterServer;
|
||||
let driver: VoiceChangerDriver;
|
||||
let fetchSpy: MockInstance<typeof fetch>;
|
||||
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
|
||||
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
|
||||
let incrementUsageSpy: MockInstance<MeteringService['incrementUsage']>;
|
||||
|
||||
beforeAll(async () => {
|
||||
@@ -91,7 +91,7 @@ afterAll(async () => {
|
||||
beforeEach(() => {
|
||||
webFetchMock.mockReset();
|
||||
fetchSpy = vi.spyOn(globalThis, 'fetch') as MockInstance<typeof fetch>;
|
||||
hasCreditsSpy = vi.spyOn(server.services.metering, 'hasEnoughCredits');
|
||||
admitSpy = vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable');
|
||||
incrementUsageSpy = vi.spyOn(server.services.metering, 'incrementUsage');
|
||||
});
|
||||
|
||||
@@ -197,7 +197,7 @@ describe('VoiceChangerDriver.convert argument validation', () => {
|
||||
describe('VoiceChangerDriver.convert credit gate', () => {
|
||||
it('throws 402 BEFORE hitting ElevenLabs when the actor lacks credits', async () => {
|
||||
const { actor } = await makeUser();
|
||||
hasCreditsSpy.mockResolvedValueOnce(false);
|
||||
admitSpy.mockResolvedValueOnce(null);
|
||||
|
||||
await expect(
|
||||
withActor(actor, () =>
|
||||
@@ -387,7 +387,7 @@ describe('VoiceChangerDriver.convert success path', () => {
|
||||
|
||||
const [base, factored] = metered();
|
||||
expect(factored).toBe(base! * 2);
|
||||
const held = hasCreditsSpy.mock.calls.map(([, amount]) => amount);
|
||||
const held = admitSpy.mock.calls.map(([, amount]) => amount);
|
||||
expect(held).toEqual([base, base! * 2]);
|
||||
});
|
||||
|
||||
|
||||
@@ -122,7 +122,7 @@ vi.mock('openai', () => {
|
||||
|
||||
let server: PuterServer;
|
||||
let driver: SpeechToTextDriver;
|
||||
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
|
||||
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
|
||||
let incrementUsageSpy: MockInstance<MeteringService['incrementUsage']>;
|
||||
|
||||
beforeAll(async () => {
|
||||
@@ -145,7 +145,7 @@ beforeEach(() => {
|
||||
toFileMock.mockClear();
|
||||
// Spy on metering — keep the real impl so its recording side runs,
|
||||
// but capture calls so per-test assertions can inspect them.
|
||||
hasCreditsSpy = vi.spyOn(server.services.metering, 'hasEnoughCredits');
|
||||
admitSpy = vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable');
|
||||
incrementUsageSpy = vi.spyOn(server.services.metering, 'incrementUsage');
|
||||
});
|
||||
|
||||
@@ -360,7 +360,7 @@ describe('SpeechToTextDriver argument validation', () => {
|
||||
|
||||
describe('SpeechToTextDriver credit gate', () => {
|
||||
it('throws 402 BEFORE hitting OpenAI when actor lacks credits', async () => {
|
||||
hasCreditsSpy.mockResolvedValueOnce(false);
|
||||
admitSpy.mockResolvedValueOnce(null);
|
||||
const { actor } = await makeUser();
|
||||
|
||||
await expect(
|
||||
@@ -686,7 +686,7 @@ describe('SpeechToTextDriver metering', () => {
|
||||
expect(cost).toBe(SPEECH_TO_TEXT_COSTS[usageType]);
|
||||
});
|
||||
|
||||
it('asks hasEnoughCredits for the same total it later meters', async () => {
|
||||
it('reserves the same total it later meters', async () => {
|
||||
const { actor } = await makeUser();
|
||||
transcriptionsCreateMock.mockResolvedValueOnce({ text: 'x' });
|
||||
|
||||
@@ -700,7 +700,7 @@ describe('SpeechToTextDriver metering', () => {
|
||||
|
||||
const usageType = 'openai:gpt-transcribe:second';
|
||||
const expected = SPEECH_TO_TEXT_COSTS[usageType] * 2;
|
||||
const creditCall = hasCreditsSpy.mock.calls[0]!;
|
||||
const creditCall = admitSpy.mock.calls[0]!;
|
||||
expect(creditCall[1]).toBe(expected);
|
||||
});
|
||||
|
||||
@@ -794,7 +794,7 @@ describe('SpeechToTextDriver metering', () => {
|
||||
|
||||
const [base, factored] = metered();
|
||||
expect(factored).toBe(base! * 2);
|
||||
const held = hasCreditsSpy.mock.calls.map(([, amount]) => amount);
|
||||
const held = admitSpy.mock.calls.map(([, amount]) => amount);
|
||||
expect(held).toEqual([base, base! * 2]);
|
||||
});
|
||||
});
|
||||
|
||||
+10
-7
@@ -72,7 +72,7 @@ const HOLD = COUNTED_TOKENS * AUDIO_RATE + 8192 * OUTPUT_RATE;
|
||||
let server: PuterServer;
|
||||
let sttDriver: SpeechToTextDriver;
|
||||
let fetchSpy: MockInstance<typeof fetch>;
|
||||
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
|
||||
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
|
||||
let batchSpy: MockInstance<MeteringService['batchIncrementUsages']>;
|
||||
|
||||
const driver = {
|
||||
@@ -102,7 +102,7 @@ beforeEach(() => {
|
||||
.mockRejectedValue(new Error('unexpected fetch')) as MockInstance<
|
||||
typeof fetch
|
||||
>;
|
||||
hasCreditsSpy = vi.spyOn(server.services.metering, 'hasEnoughCredits');
|
||||
admitSpy = vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable');
|
||||
batchSpy = vi.spyOn(server.services.metering, 'batchIncrementUsages');
|
||||
});
|
||||
|
||||
@@ -220,13 +220,16 @@ const metered = (): UsageInput[] =>
|
||||
/** Records each credit hold taken, its options, and its release. */
|
||||
const trackHolds = (order: string[] = []) => {
|
||||
const metering = server.services.metering;
|
||||
const realReserve = metering.reserveCredits.bind(metering);
|
||||
const realReserve = Object.getPrototypeOf(
|
||||
metering,
|
||||
).reserveCreditsIfAffordable.bind(metering) as typeof metering.reserveCreditsIfAffordable;
|
||||
const held: { amount: number; ttlMs?: number }[] = [];
|
||||
const released = vi.fn();
|
||||
vi.spyOn(metering, 'reserveCredits').mockImplementation(
|
||||
vi.spyOn(metering, 'reserveCreditsIfAffordable').mockImplementation(
|
||||
async (actor, amount, opts) => {
|
||||
held.push({ amount, ttlMs: opts?.ttlMs });
|
||||
const hold = await realReserve(actor, amount, opts);
|
||||
if (!hold) return null;
|
||||
return {
|
||||
...hold,
|
||||
release: async () => {
|
||||
@@ -425,7 +428,7 @@ describe('GeminiSpeechToTextProvider test_mode', () => {
|
||||
model: MODEL,
|
||||
});
|
||||
expect(fetchSpy).not.toHaveBeenCalled();
|
||||
expect(hasCreditsSpy).not.toHaveBeenCalled();
|
||||
expect(admitSpy).not.toHaveBeenCalled();
|
||||
expect(batchSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
@@ -1263,12 +1266,12 @@ describe('GeminiSpeechToTextProvider countTokens preflight', () => {
|
||||
describe('GeminiSpeechToTextProvider credit hold', () => {
|
||||
it('throws 402 after counting tokens, before transcribing', async () => {
|
||||
const { actor } = await makeUser();
|
||||
hasCreditsSpy.mockResolvedValueOnce(false);
|
||||
admitSpy.mockResolvedValueOnce(null);
|
||||
fetchSpy.mockResolvedValueOnce(Response.json({ totalTokens: 91 }));
|
||||
await expect(
|
||||
withActor(actor, () => driver.transcribe({ file: dataUrl() })),
|
||||
).rejects.toMatchObject({ statusCode: 402 });
|
||||
expect(hasCreditsSpy.mock.calls[0]![1]).toBe(HOLD);
|
||||
expect(admitSpy.mock.calls[0]![1]).toBe(HOLD);
|
||||
expect(fetchSpy).toHaveBeenCalledTimes(1);
|
||||
expect(batchSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
@@ -70,7 +70,7 @@ const driver = {
|
||||
getReportedCosts: () => sttDriver.getReportedCosts(),
|
||||
};
|
||||
let fetchSpy: MockInstance<typeof fetch>;
|
||||
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
|
||||
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
|
||||
let incrementUsageSpy: MockInstance<MeteringService['incrementUsage']>;
|
||||
|
||||
beforeAll(async () => {
|
||||
@@ -88,7 +88,7 @@ afterAll(async () => {
|
||||
|
||||
beforeEach(() => {
|
||||
fetchSpy = vi.spyOn(globalThis, 'fetch') as MockInstance<typeof fetch>;
|
||||
hasCreditsSpy = vi.spyOn(server.services.metering, 'hasEnoughCredits');
|
||||
admitSpy = vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable');
|
||||
incrementUsageSpy = vi.spyOn(server.services.metering, 'incrementUsage');
|
||||
});
|
||||
|
||||
@@ -193,7 +193,7 @@ describe('XAISpeechToTextProvider test_mode', () => {
|
||||
expect(Array.isArray(result.words)).toBe(true);
|
||||
// No file required, no actor required, no fetch / metering hit.
|
||||
expect(fetchSpy).not.toHaveBeenCalled();
|
||||
expect(hasCreditsSpy).not.toHaveBeenCalled();
|
||||
expect(admitSpy).not.toHaveBeenCalled();
|
||||
expect(incrementUsageSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
@@ -258,7 +258,7 @@ describe('XAISpeechToTextProvider missing API key', () => {
|
||||
|
||||
describe('XAISpeechToTextProvider credit gate', () => {
|
||||
it('throws 402 BEFORE hitting xAI when actor lacks credits', async () => {
|
||||
hasCreditsSpy.mockResolvedValueOnce(false);
|
||||
admitSpy.mockResolvedValueOnce(null);
|
||||
const { actor } = await makeUser();
|
||||
|
||||
await expect(
|
||||
@@ -273,7 +273,7 @@ describe('XAISpeechToTextProvider credit gate', () => {
|
||||
expect(incrementUsageSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('asks hasEnoughCredits for estimated seconds × per-second ucents', async () => {
|
||||
it('reserves estimated seconds × per-second ucents', async () => {
|
||||
const { actor } = await makeUser();
|
||||
fetchSpy.mockResolvedValueOnce(
|
||||
sttResponse({ text: 'ok', duration: 1 }),
|
||||
@@ -285,7 +285,7 @@ describe('XAISpeechToTextProvider credit gate', () => {
|
||||
driver.transcribe({ file: dataUrl(audio, 'audio/mp3') }),
|
||||
);
|
||||
|
||||
expect(hasCreditsSpy.mock.calls[0]![1]).toBe(UCENTS_PER_SECOND * 2);
|
||||
expect(admitSpy.mock.calls[0]![1]).toBe(UCENTS_PER_SECOND * 2);
|
||||
});
|
||||
|
||||
it('estimates 60 seconds for URL inputs (we cannot inspect size locally)', async () => {
|
||||
@@ -298,7 +298,7 @@ describe('XAISpeechToTextProvider credit gate', () => {
|
||||
driver.transcribe({ file: 'https://example.com/clip.mp3' }),
|
||||
);
|
||||
|
||||
expect(hasCreditsSpy.mock.calls[0]![1]).toBe(UCENTS_PER_SECOND * 60);
|
||||
expect(admitSpy.mock.calls[0]![1]).toBe(UCENTS_PER_SECOND * 60);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -699,9 +699,9 @@ describe('TTSDriver cost factor', () => {
|
||||
server.services.metering,
|
||||
'incrementUsage',
|
||||
);
|
||||
const hasCreditsSpy = vi.spyOn(
|
||||
const admitSpy = vi.spyOn(
|
||||
server.services.metering,
|
||||
'hasEnoughCredits',
|
||||
'reserveCreditsIfAffordable',
|
||||
);
|
||||
const metered = () =>
|
||||
incrementUsageSpy.mock.calls.map(([, , , cost]) => cost!);
|
||||
@@ -725,7 +725,7 @@ describe('TTSDriver cost factor', () => {
|
||||
const [base, factored] = metered();
|
||||
expect(base).toBeGreaterThan(0);
|
||||
expect(factored).toBe(Math.round(base! * 2));
|
||||
const held = hasCreditsSpy.mock.calls.map(([, amount]) => amount);
|
||||
const held = admitSpy.mock.calls.map(([, amount]) => amount);
|
||||
expect(held).toEqual([base, Math.round(base! * 2)]);
|
||||
});
|
||||
});
|
||||
@@ -84,7 +84,7 @@ vi.mock('@aws-sdk/client-polly', async () => {
|
||||
|
||||
let server: PuterServer;
|
||||
let priceFetch: MockInstance<typeof fetch>;
|
||||
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
|
||||
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
|
||||
let incrementUsageSpy: MockInstance<MeteringService['incrementUsage']>;
|
||||
|
||||
beforeAll(async () => {
|
||||
@@ -180,7 +180,7 @@ beforeEach(() => {
|
||||
pollyCtor.mockReset();
|
||||
priceFetch = vi.fn(servePriceLists());
|
||||
vi.stubGlobal('fetch', priceFetch);
|
||||
hasCreditsSpy = vi.spyOn(server.services.metering, 'hasEnoughCredits');
|
||||
admitSpy = vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable');
|
||||
incrementUsageSpy = vi.spyOn(server.services.metering, 'incrementUsage');
|
||||
});
|
||||
|
||||
@@ -314,7 +314,7 @@ describe('AWSPollyTTSProvider.synthesize test_mode', () => {
|
||||
url: 'https://puter-sample-data.puter.site/tts_example.mp3',
|
||||
content_type: 'audio',
|
||||
});
|
||||
expect(hasCreditsSpy).not.toHaveBeenCalled();
|
||||
expect(admitSpy).not.toHaveBeenCalled();
|
||||
expect(pollySendMock).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -349,7 +349,7 @@ describe('AWSPollyTTSProvider.synthesize argument validation', () => {
|
||||
describe('AWSPollyTTSProvider.synthesize credit gate', () => {
|
||||
it('throws 402 BEFORE hitting Polly when actor lacks credits', async () => {
|
||||
const provider = makeProvider();
|
||||
hasCreditsSpy.mockResolvedValueOnce(false);
|
||||
admitSpy.mockResolvedValueOnce(null);
|
||||
sendDispatch();
|
||||
|
||||
await expect(
|
||||
@@ -481,7 +481,7 @@ describe('AWSPollyTTSProvider.synthesize metering', () => {
|
||||
expect(cost).toBe(expectedCost);
|
||||
});
|
||||
|
||||
it('asks for hasEnoughCredits with the same total it later meters', async () => {
|
||||
it('reserves the same total it later meters', async () => {
|
||||
const provider = makeProvider();
|
||||
sendDispatch();
|
||||
|
||||
@@ -495,7 +495,7 @@ describe('AWSPollyTTSProvider.synthesize metering', () => {
|
||||
);
|
||||
|
||||
const expectedCost = AWS_POLLY_COSTS['generative'] * text.length;
|
||||
expect(hasCreditsSpy.mock.calls[0]![1]).toBe(expectedCost);
|
||||
expect(admitSpy.mock.calls[0]![1]).toBe(expectedCost);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -599,7 +599,7 @@ describe('AWSPollyTTSProvider published pricing', () => {
|
||||
engine: 'generative',
|
||||
});
|
||||
|
||||
expect(hasCreditsSpy.mock.calls.map((c) => c[1])).toEqual([
|
||||
expect(admitSpy.mock.calls.map((c) => c[1])).toEqual([
|
||||
1700 * 5,
|
||||
3000 * 5,
|
||||
]);
|
||||
@@ -648,7 +648,7 @@ describe('AWSPollyTTSProvider published pricing', () => {
|
||||
await readStreamResult(result);
|
||||
|
||||
expect(refreshedNeural).toBe(19);
|
||||
expect(hasCreditsSpy.mock.calls[0]![1]).toBe(1700 * 5);
|
||||
expect(admitSpy.mock.calls[0]![1]).toBe(1700 * 5);
|
||||
expect(incrementUsageSpy.mock.calls[0]![3]).toBe(1700 * 5);
|
||||
});
|
||||
|
||||
|
||||
@@ -86,8 +86,10 @@ describe('meterPerCharacter', () => {
|
||||
|
||||
it('holds and then meters exactly text length × price', async () => {
|
||||
const check = vi
|
||||
.spyOn(server.services.metering, 'hasEnoughCredits')
|
||||
.mockResolvedValue(true);
|
||||
.spyOn(server.services.metering, 'reserveCreditsIfAffordable')
|
||||
.mockImplementation((who, amount, opts) =>
|
||||
server.services.metering.reserveCredits(who, amount, opts),
|
||||
);
|
||||
const record = vi.spyOn(server.services.metering, 'incrementUsage');
|
||||
|
||||
await drain(
|
||||
@@ -101,7 +103,7 @@ describe('meterPerCharacter', () => {
|
||||
),
|
||||
);
|
||||
|
||||
expect(check).toHaveBeenCalledWith(actor, 7500);
|
||||
expect(check).toHaveBeenCalledWith(actor, 7500, undefined);
|
||||
expect(record).toHaveBeenCalledWith(
|
||||
actor,
|
||||
'openai:tts-1:character',
|
||||
@@ -118,8 +120,10 @@ describe('meterPerCharacter', () => {
|
||||
it('meters nothing when the upstream breaks before any audio', async () => {
|
||||
vi.spyOn(
|
||||
server.services.metering,
|
||||
'hasEnoughCredits',
|
||||
).mockResolvedValue(true);
|
||||
'reserveCreditsIfAffordable',
|
||||
).mockImplementation((who, amount, opts) =>
|
||||
server.services.metering.reserveCredits(who, amount, opts),
|
||||
);
|
||||
const record = vi.spyOn(server.services.metering, 'incrementUsage');
|
||||
|
||||
await drain(
|
||||
@@ -139,8 +143,10 @@ describe('meterPerCharacter', () => {
|
||||
it('meters the full text once audio has started, even if the upstream then breaks', async () => {
|
||||
vi.spyOn(
|
||||
server.services.metering,
|
||||
'hasEnoughCredits',
|
||||
).mockResolvedValue(true);
|
||||
'reserveCreditsIfAffordable',
|
||||
).mockImplementation((who, amount, opts) =>
|
||||
server.services.metering.reserveCredits(who, amount, opts),
|
||||
);
|
||||
const record = vi.spyOn(server.services.metering, 'incrementUsage');
|
||||
|
||||
await drain(
|
||||
@@ -165,8 +171,10 @@ describe('meterPerCharacter', () => {
|
||||
it('meters the full text when the caller hangs up before any audio', async () => {
|
||||
vi.spyOn(
|
||||
server.services.metering,
|
||||
'hasEnoughCredits',
|
||||
).mockResolvedValue(true);
|
||||
'reserveCreditsIfAffordable',
|
||||
).mockImplementation((who, amount, opts) =>
|
||||
server.services.metering.reserveCredits(who, amount, opts),
|
||||
);
|
||||
const record = vi.spyOn(server.services.metering, 'incrementUsage');
|
||||
|
||||
const result = await meterPerCharacter(
|
||||
@@ -192,8 +200,10 @@ describe('meterPerCharacter', () => {
|
||||
it('meters nothing when synthesis fails', async () => {
|
||||
vi.spyOn(
|
||||
server.services.metering,
|
||||
'hasEnoughCredits',
|
||||
).mockResolvedValue(true);
|
||||
'reserveCreditsIfAffordable',
|
||||
).mockImplementation((who, amount, opts) =>
|
||||
server.services.metering.reserveCredits(who, amount, opts),
|
||||
);
|
||||
const record = vi.spyOn(server.services.metering, 'incrementUsage');
|
||||
const boom = new Error('vendor down');
|
||||
|
||||
@@ -213,8 +223,8 @@ describe('meterPerCharacter', () => {
|
||||
it('refuses with a 402 before synthesizing when unaffordable', async () => {
|
||||
vi.spyOn(
|
||||
server.services.metering,
|
||||
'hasEnoughCredits',
|
||||
).mockResolvedValue(false);
|
||||
'reserveCreditsIfAffordable',
|
||||
).mockResolvedValue(null);
|
||||
const synthesize = vi.fn(audio);
|
||||
|
||||
await expect(
|
||||
|
||||
@@ -57,7 +57,7 @@ import { ELEVENLABS_TTS_COSTS } from './costs.js';
|
||||
|
||||
let server: PuterServer;
|
||||
let fetchSpy: MockInstance<typeof fetch>;
|
||||
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
|
||||
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
|
||||
let incrementUsageSpy: MockInstance<MeteringService['incrementUsage']>;
|
||||
|
||||
beforeAll(async () => {
|
||||
@@ -88,7 +88,7 @@ const audioResponse = (body = 'audio-bytes', contentType = 'audio/mpeg') =>
|
||||
|
||||
beforeEach(() => {
|
||||
fetchSpy = vi.spyOn(globalThis, 'fetch') as MockInstance<typeof fetch>;
|
||||
hasCreditsSpy = vi.spyOn(server.services.metering, 'hasEnoughCredits');
|
||||
admitSpy = vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable');
|
||||
incrementUsageSpy = vi.spyOn(server.services.metering, 'incrementUsage');
|
||||
});
|
||||
|
||||
@@ -283,7 +283,7 @@ describe('ElevenLabsTTSProvider.synthesize test_mode', () => {
|
||||
url: 'https://puter-sample-data.puter.site/tts_example.mp3',
|
||||
content_type: 'audio',
|
||||
});
|
||||
expect(hasCreditsSpy).not.toHaveBeenCalled();
|
||||
expect(admitSpy).not.toHaveBeenCalled();
|
||||
expect(fetchSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -323,7 +323,7 @@ describe('ElevenLabsTTSProvider.synthesize argument validation', () => {
|
||||
});
|
||||
|
||||
expect(fetchSpy).not.toHaveBeenCalled();
|
||||
expect(hasCreditsSpy).not.toHaveBeenCalled();
|
||||
expect(admitSpy).not.toHaveBeenCalled();
|
||||
expect(incrementUsageSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
@@ -358,7 +358,7 @@ describe('ElevenLabsTTSProvider.synthesize argument validation', () => {
|
||||
describe('ElevenLabsTTSProvider.synthesize credit gate', () => {
|
||||
it('throws 402 BEFORE hitting ElevenLabs when actor lacks credits', async () => {
|
||||
const provider = makeProvider();
|
||||
hasCreditsSpy.mockResolvedValueOnce(false);
|
||||
admitSpy.mockResolvedValueOnce(null);
|
||||
|
||||
await expect(
|
||||
withTestActor(() => provider.synthesize({ text: 'hi' })),
|
||||
@@ -510,7 +510,7 @@ describe('ElevenLabsTTSProvider.synthesize request shape', () => {
|
||||
fields: { key, got: value },
|
||||
});
|
||||
expect(fetchSpy).not.toHaveBeenCalled();
|
||||
expect(hasCreditsSpy).not.toHaveBeenCalled();
|
||||
expect(admitSpy).not.toHaveBeenCalled();
|
||||
expect(incrementUsageSpy).not.toHaveBeenCalled();
|
||||
},
|
||||
);
|
||||
@@ -893,7 +893,7 @@ describe('ElevenLabsTTSProvider.synthesize metering', () => {
|
||||
expect(cost).toBe(expectedCost);
|
||||
});
|
||||
|
||||
it('asks for hasEnoughCredits with the same total it later meters', async () => {
|
||||
it('reserves the same total it later meters', async () => {
|
||||
const provider = makeProvider();
|
||||
fetchSpy.mockResolvedValueOnce(audioResponse());
|
||||
|
||||
@@ -904,7 +904,7 @@ describe('ElevenLabsTTSProvider.synthesize metering', () => {
|
||||
|
||||
const expectedCost =
|
||||
ELEVENLABS_TTS_COSTS['eleven_multilingual_v2'] * text.length;
|
||||
expect(hasCreditsSpy.mock.calls[0]![1]).toBe(expectedCost);
|
||||
expect(admitSpy.mock.calls[0]![1]).toBe(expectedCost);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -61,7 +61,7 @@ const INTERACTIONS_URL =
|
||||
|
||||
let server: PuterServer;
|
||||
let fetchSpy: MockInstance<typeof fetch>;
|
||||
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
|
||||
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
|
||||
let batchIncrementUsagesSpy: MockInstance<
|
||||
MeteringService['batchIncrementUsages']
|
||||
>;
|
||||
@@ -183,13 +183,16 @@ const payingUser = () =>
|
||||
/** Spies on the credit holds taken, recording the amount and each release. */
|
||||
const trackHolds = () => {
|
||||
const metering = server.services.metering;
|
||||
const realReserve = metering.reserveCredits.bind(metering);
|
||||
const realReserve = Object.getPrototypeOf(
|
||||
metering,
|
||||
).reserveCreditsIfAffordable.bind(metering) as typeof metering.reserveCreditsIfAffordable;
|
||||
const held: number[] = [];
|
||||
const released = vi.fn();
|
||||
vi.spyOn(metering, 'reserveCredits').mockImplementation(
|
||||
vi.spyOn(metering, 'reserveCreditsIfAffordable').mockImplementation(
|
||||
async (actor, amount, opts) => {
|
||||
held.push(amount);
|
||||
const hold = await realReserve(actor, amount, opts);
|
||||
if (!hold) return null;
|
||||
return {
|
||||
...hold,
|
||||
release: async () => {
|
||||
@@ -214,7 +217,7 @@ beforeEach(() => {
|
||||
.mockRejectedValue(new Error('unexpected fetch')) as MockInstance<
|
||||
typeof fetch
|
||||
>;
|
||||
hasCreditsSpy = vi.spyOn(server.services.metering, 'hasEnoughCredits');
|
||||
admitSpy = vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable');
|
||||
batchIncrementUsagesSpy = vi.spyOn(
|
||||
server.services.metering,
|
||||
'batchIncrementUsages',
|
||||
@@ -332,7 +335,7 @@ describe('GeminiTTSProvider.synthesize test_mode', () => {
|
||||
url: 'https://puter-sample-data.puter.site/tts_example.mp3',
|
||||
content_type: 'audio',
|
||||
});
|
||||
expect(hasCreditsSpy).not.toHaveBeenCalled();
|
||||
expect(admitSpy).not.toHaveBeenCalled();
|
||||
expect(fetchSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -406,7 +409,7 @@ describe('GeminiTTSProvider.synthesize argument validation', () => {
|
||||
legacyCode: 'input_too_large',
|
||||
fields: { key: 'text', max: 3000 },
|
||||
});
|
||||
expect(hasCreditsSpy).not.toHaveBeenCalled();
|
||||
expect(admitSpy).not.toHaveBeenCalled();
|
||||
expect(fetchSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
@@ -434,7 +437,7 @@ describe('GeminiTTSProvider.synthesize argument validation', () => {
|
||||
legacyCode: 'input_too_large',
|
||||
fields: { key: 'instructions', max: 3000 },
|
||||
});
|
||||
expect(hasCreditsSpy).not.toHaveBeenCalled();
|
||||
expect(admitSpy).not.toHaveBeenCalled();
|
||||
expect(fetchSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -443,7 +446,7 @@ describe('GeminiTTSProvider.synthesize argument validation', () => {
|
||||
|
||||
describe('GeminiTTSProvider.synthesize credit gate', () => {
|
||||
it('throws 402 before calling Gemini when the actor lacks credits', async () => {
|
||||
hasCreditsSpy.mockResolvedValueOnce(false);
|
||||
admitSpy.mockResolvedValueOnce(null);
|
||||
await expect(
|
||||
withTestActor(() => makeProvider().synthesize({ text: 'hi' })),
|
||||
).rejects.toMatchObject({ statusCode: 402 });
|
||||
@@ -473,9 +476,10 @@ describe('GeminiTTSProvider.synthesize credit gate', () => {
|
||||
actor,
|
||||
),
|
||||
).rejects.toMatchObject({ statusCode: 402 });
|
||||
expect(hasCreditsSpy).toHaveBeenCalledWith(
|
||||
expect(admitSpy).toHaveBeenCalledWith(
|
||||
actor,
|
||||
FLASH_RESERVATION,
|
||||
undefined,
|
||||
);
|
||||
expect(fetchSpy).not.toHaveBeenCalled();
|
||||
expect(batchIncrementUsagesSpy).not.toHaveBeenCalled();
|
||||
@@ -1365,9 +1369,10 @@ describe('GeminiTTSProvider.synthesize scheduled rates', () => {
|
||||
server.clients.event.off('ai.cost.factor.*', factor);
|
||||
}
|
||||
|
||||
expect(hasCreditsSpy).toHaveBeenCalledWith(
|
||||
expect(admitSpy).toHaveBeenCalledWith(
|
||||
expect.anything(),
|
||||
reserved * 1.5,
|
||||
undefined,
|
||||
);
|
||||
expect(held).toEqual([reserved * 1.5]);
|
||||
expect(metered(':input').costOverride).toBe(7 * inputRate * 1.5);
|
||||
|
||||
@@ -84,7 +84,7 @@ vi.mock('openai', () => {
|
||||
// ── Test harness ────────────────────────────────────────────────────
|
||||
|
||||
let server: PuterServer;
|
||||
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
|
||||
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
|
||||
let incrementUsageSpy: MockInstance<MeteringService['incrementUsage']>;
|
||||
|
||||
beforeAll(async () => {
|
||||
@@ -108,7 +108,7 @@ const mockAudioResponse = (bytes = 'opus-audio') => new Response(bytes);
|
||||
beforeEach(() => {
|
||||
speechCreateMock.mockReset();
|
||||
openAICtor.mockReset();
|
||||
hasCreditsSpy = vi.spyOn(server.services.metering, 'hasEnoughCredits');
|
||||
admitSpy = vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable');
|
||||
incrementUsageSpy = vi.spyOn(server.services.metering, 'incrementUsage');
|
||||
});
|
||||
|
||||
@@ -205,7 +205,7 @@ describe('OpenAITTSProvider.synthesize test_mode', () => {
|
||||
url: 'https://puter-sample-data.puter.site/tts_example.mp3',
|
||||
content_type: 'audio',
|
||||
});
|
||||
expect(hasCreditsSpy).not.toHaveBeenCalled();
|
||||
expect(admitSpy).not.toHaveBeenCalled();
|
||||
expect(speechCreateMock).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -283,7 +283,7 @@ describe('OpenAITTSProvider.synthesize argument validation', () => {
|
||||
describe('OpenAITTSProvider.synthesize credit gate', () => {
|
||||
it('throws 402 BEFORE hitting OpenAI when actor lacks credits', async () => {
|
||||
const provider = makeProvider();
|
||||
hasCreditsSpy.mockResolvedValueOnce(false);
|
||||
admitSpy.mockResolvedValueOnce(null);
|
||||
|
||||
await expect(
|
||||
withTestActor(() => provider.synthesize({ text: 'hi' })),
|
||||
@@ -298,7 +298,9 @@ describe('OpenAITTSProvider.synthesize credit gate', () => {
|
||||
user: { id: 7, uuid: `tts-hold-${Date.now()}`, username: 'hold' },
|
||||
});
|
||||
const userId = actor.user!.uuid;
|
||||
hasCreditsSpy.mockResolvedValue(true);
|
||||
admitSpy.mockImplementation((who, amount, opts) =>
|
||||
server.services.metering.reserveCredits(who, amount, opts),
|
||||
);
|
||||
let heldDuringCall = -1;
|
||||
speechCreateMock.mockImplementationOnce(async () => {
|
||||
heldDuringCall = await server.stores.creditHold.outstanding(userId);
|
||||
@@ -324,7 +326,9 @@ describe('OpenAITTSProvider.synthesize credit gate', () => {
|
||||
const actor = makeActor({
|
||||
user: { id: 8, uuid: `tts-fail-${Date.now()}`, username: 'fail' },
|
||||
});
|
||||
hasCreditsSpy.mockResolvedValue(true);
|
||||
admitSpy.mockImplementation((who, amount, opts) =>
|
||||
server.services.metering.reserveCredits(who, amount, opts),
|
||||
);
|
||||
speechCreateMock.mockRejectedValueOnce(new Error('upstream down'));
|
||||
|
||||
await expect(
|
||||
@@ -463,7 +467,7 @@ describe('OpenAITTSProvider.synthesize metering', () => {
|
||||
expect(cost).toBe(expectedCost);
|
||||
});
|
||||
|
||||
it('asks for hasEnoughCredits with the same total it later meters', async () => {
|
||||
it('reserves the same total it later meters', async () => {
|
||||
const provider = makeProvider();
|
||||
speechCreateMock.mockResolvedValueOnce(mockAudioResponse());
|
||||
|
||||
@@ -473,8 +477,8 @@ describe('OpenAITTSProvider.synthesize metering', () => {
|
||||
);
|
||||
|
||||
const expectedCost = OPENAI_TTS_COSTS['tts-1'] * text.length;
|
||||
// First call to hasEnoughCredits should match the metered cost.
|
||||
const creditCall = hasCreditsSpy.mock.calls[0]!;
|
||||
// The first reservation should match the metered cost.
|
||||
const creditCall = admitSpy.mock.calls[0]!;
|
||||
expect(creditCall[1]).toBe(expectedCost);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -56,7 +56,7 @@ import { SPEECHIFY_TTS_COSTS } from './costs.js';
|
||||
|
||||
let server: PuterServer;
|
||||
let fetchSpy: MockInstance<typeof fetch>;
|
||||
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
|
||||
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
|
||||
let incrementUsageSpy: MockInstance<MeteringService['incrementUsage']>;
|
||||
|
||||
beforeAll(async () => {
|
||||
@@ -82,7 +82,7 @@ const audioResponse = (audioData = Buffer.from('audio-bytes').toString('base64')
|
||||
|
||||
beforeEach(() => {
|
||||
fetchSpy = vi.spyOn(globalThis, 'fetch') as MockInstance<typeof fetch>;
|
||||
hasCreditsSpy = vi.spyOn(server.services.metering, 'hasEnoughCredits');
|
||||
admitSpy = vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable');
|
||||
incrementUsageSpy = vi.spyOn(server.services.metering, 'incrementUsage');
|
||||
});
|
||||
|
||||
@@ -159,7 +159,7 @@ describe('SpeechifyTTSProvider.synthesize test_mode', () => {
|
||||
url: 'https://puter-sample-data.puter.site/tts_example.mp3',
|
||||
content_type: 'audio',
|
||||
});
|
||||
expect(hasCreditsSpy).not.toHaveBeenCalled();
|
||||
expect(admitSpy).not.toHaveBeenCalled();
|
||||
expect(fetchSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -206,7 +206,7 @@ describe('SpeechifyTTSProvider.synthesize argument validation', () => {
|
||||
describe('SpeechifyTTSProvider.synthesize credit gate', () => {
|
||||
it('throws 402 BEFORE hitting Speechify when actor lacks credits', async () => {
|
||||
const provider = makeProvider();
|
||||
hasCreditsSpy.mockResolvedValueOnce(false);
|
||||
admitSpy.mockResolvedValueOnce(null);
|
||||
|
||||
await expect(
|
||||
withTestActor(() => provider.synthesize({ text: 'hi' })),
|
||||
@@ -324,7 +324,7 @@ describe('SpeechifyTTSProvider.synthesize metering', () => {
|
||||
expect(cost).toBe(expectedCost);
|
||||
});
|
||||
|
||||
it('asks for hasEnoughCredits with the same total it later meters', async () => {
|
||||
it('reserves the same total it later meters', async () => {
|
||||
const provider = makeProvider();
|
||||
fetchSpy.mockResolvedValueOnce(audioResponse());
|
||||
|
||||
@@ -332,7 +332,7 @@ describe('SpeechifyTTSProvider.synthesize metering', () => {
|
||||
await withTestActor(() => provider.synthesize({ text }));
|
||||
|
||||
const expectedCost = SPEECHIFY_TTS_COSTS['simba-3.2'] * text.length;
|
||||
expect(hasCreditsSpy.mock.calls[0]![1]).toBe(expectedCost);
|
||||
expect(admitSpy.mock.calls[0]![1]).toBe(expectedCost);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -56,7 +56,7 @@ import { XAI_TTS_COSTS } from './costs.js';
|
||||
|
||||
let server: PuterServer;
|
||||
let fetchSpy: MockInstance<typeof fetch>;
|
||||
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
|
||||
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
|
||||
let incrementUsageSpy: MockInstance<MeteringService['incrementUsage']>;
|
||||
|
||||
beforeAll(async () => {
|
||||
@@ -79,7 +79,7 @@ const audioResponse = (body = 'audio-bytes', contentType = 'audio/mpeg') =>
|
||||
|
||||
beforeEach(() => {
|
||||
fetchSpy = vi.spyOn(globalThis, 'fetch') as MockInstance<typeof fetch>;
|
||||
hasCreditsSpy = vi.spyOn(server.services.metering, 'hasEnoughCredits');
|
||||
admitSpy = vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable');
|
||||
incrementUsageSpy = vi.spyOn(server.services.metering, 'incrementUsage');
|
||||
});
|
||||
|
||||
@@ -160,7 +160,7 @@ describe('XAITTSProvider.synthesize test_mode', () => {
|
||||
url: 'https://puter-sample-data.puter.site/tts_example.mp3',
|
||||
content_type: 'audio',
|
||||
});
|
||||
expect(hasCreditsSpy).not.toHaveBeenCalled();
|
||||
expect(admitSpy).not.toHaveBeenCalled();
|
||||
expect(fetchSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -195,7 +195,7 @@ describe('XAITTSProvider.synthesize argument validation', () => {
|
||||
describe('XAITTSProvider.synthesize credit gate', () => {
|
||||
it('throws 402 BEFORE hitting xAI when actor lacks credits', async () => {
|
||||
const provider = makeProvider();
|
||||
hasCreditsSpy.mockResolvedValueOnce(false);
|
||||
admitSpy.mockResolvedValueOnce(null);
|
||||
|
||||
await expect(
|
||||
withTestActor(() => provider.synthesize({ text: 'hi' })),
|
||||
@@ -327,7 +327,7 @@ describe('XAITTSProvider.synthesize metering', () => {
|
||||
expect(cost).toBe(expectedCost);
|
||||
});
|
||||
|
||||
it('asks for hasEnoughCredits with the same total it later meters', async () => {
|
||||
it('reserves the same total it later meters', async () => {
|
||||
const provider = makeProvider();
|
||||
fetchSpy.mockResolvedValueOnce(audioResponse());
|
||||
|
||||
@@ -335,7 +335,7 @@ describe('XAITTSProvider.synthesize metering', () => {
|
||||
await withTestActor(() => provider.synthesize({ text }));
|
||||
|
||||
const expectedCost = XAI_TTS_COSTS['xai-tts'] * text.length;
|
||||
expect(hasCreditsSpy.mock.calls[0]![1]).toBe(expectedCost);
|
||||
expect(admitSpy.mock.calls[0]![1]).toBe(expectedCost);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -94,7 +94,7 @@ vi.mock('../../util/secureHttp.js', async (importOriginal) => ({
|
||||
|
||||
let server: PuterServer;
|
||||
let driver: VideoGenerationDriver;
|
||||
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
|
||||
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
|
||||
|
||||
beforeAll(async () => {
|
||||
server = await setupTestServer({
|
||||
@@ -122,8 +122,10 @@ beforeEach(() => {
|
||||
togetherVideosCreateMock.mockReset();
|
||||
togetherVideosRetrieveMock.mockReset();
|
||||
secureFetchMock.mockReset();
|
||||
hasCreditsSpy = vi.spyOn(server.services.metering, 'hasEnoughCredits');
|
||||
hasCreditsSpy.mockResolvedValue(true);
|
||||
admitSpy = vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable');
|
||||
admitSpy.mockImplementation((who, amount, opts) =>
|
||||
server.services.metering.reserveCredits(who, amount, opts),
|
||||
);
|
||||
vi.spyOn(server.services.metering, 'getRemainingUsage').mockResolvedValue(
|
||||
100_000_000_000,
|
||||
);
|
||||
|
||||
@@ -106,7 +106,7 @@ const MP4 = Buffer.concat([
|
||||
let server: PuterServer;
|
||||
let videoDriver: VideoGenerationDriver;
|
||||
let fetchSpy: MockInstance<typeof fetch>;
|
||||
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
|
||||
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
|
||||
let batchSpy: MockInstance<MeteringService['batchIncrementUsages']>;
|
||||
|
||||
const generate = (args: Record<string, unknown> = {}) =>
|
||||
@@ -139,9 +139,11 @@ beforeEach(() => {
|
||||
secureFetchMock.mockReset();
|
||||
secureFetchMock.mockRejectedValue(new Error('unexpected secureFetch'));
|
||||
// The full hold is more than a new account's allowance.
|
||||
hasCreditsSpy = vi
|
||||
.spyOn(server.services.metering, 'hasEnoughCredits')
|
||||
.mockResolvedValue(true);
|
||||
admitSpy = vi
|
||||
.spyOn(server.services.metering, 'reserveCreditsIfAffordable')
|
||||
.mockImplementation((who, amount, opts) =>
|
||||
server.services.metering.reserveCredits(who, amount, opts),
|
||||
);
|
||||
batchSpy = vi.spyOn(server.services.metering, 'batchIncrementUsages');
|
||||
});
|
||||
|
||||
@@ -589,7 +591,7 @@ describe('GeminiVideoProvider test_mode', () => {
|
||||
it('returns the sample clip without network, credits or metering', async () => {
|
||||
expect(await testMode()).toBe(SAMPLE_URL);
|
||||
expect(fetchSpy).not.toHaveBeenCalled();
|
||||
expect(hasCreditsSpy).not.toHaveBeenCalled();
|
||||
expect(admitSpy).not.toHaveBeenCalled();
|
||||
expect(batchSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
@@ -1028,7 +1030,7 @@ describe('GeminiVideoProvider source video', () => {
|
||||
|
||||
const path = await writeVideo(user, 'dog.mp4', MP4);
|
||||
const read = vi.spyOn(server.stores.s3Object, 'getObjectStream');
|
||||
hasCreditsSpy.mockResolvedValue(false);
|
||||
admitSpy.mockResolvedValue(null);
|
||||
countOnly();
|
||||
await expect(
|
||||
withActor(user.actor, () => generate({ input_video: path })),
|
||||
@@ -1573,12 +1575,12 @@ describe('GeminiVideoProvider countTokens preflight', () => {
|
||||
describe('GeminiVideoProvider credit hold', () => {
|
||||
it('throws 402 after counting tokens, before generating', async () => {
|
||||
const { actor } = await makeUser();
|
||||
hasCreditsSpy.mockResolvedValueOnce(false);
|
||||
admitSpy.mockResolvedValueOnce(null);
|
||||
fetchSpy.mockResolvedValueOnce(Response.json({ totalTokens: 18 }));
|
||||
await expect(withActor(actor, () => generate())).rejects.toMatchObject({
|
||||
statusCode: 402,
|
||||
});
|
||||
expect(hasCreditsSpy.mock.calls[0]![1]).toBe(HOLD);
|
||||
expect(admitSpy.mock.calls[0]![1]).toBe(HOLD);
|
||||
expect(fetchSpy).toHaveBeenCalledTimes(1);
|
||||
expect(batchSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
@@ -85,7 +85,7 @@ vi.mock('together-ai', () => {
|
||||
// ── Test harness ────────────────────────────────────────────────────
|
||||
|
||||
let server: PuterServer;
|
||||
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
|
||||
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
|
||||
let remainingUsageSpy: MockInstance<MeteringService['getRemainingUsage']>;
|
||||
let incrementUsageSpy: MockInstance<MeteringService['incrementUsage']>;
|
||||
|
||||
@@ -117,11 +117,13 @@ beforeEach(() => {
|
||||
videosCreateMock.mockReset();
|
||||
videosRetrieveMock.mockReset();
|
||||
togetherCtor.mockReset();
|
||||
hasCreditsSpy = vi.spyOn(server.services.metering, 'hasEnoughCredits');
|
||||
admitSpy = vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable');
|
||||
// Default to "has credits" so per-test setup only needs to override
|
||||
// for the explicit credit-gate scenarios. SYSTEM_ACTOR has no uuid,
|
||||
// so the live `getRemainingUsage` path can short-circuit to 0.
|
||||
hasCreditsSpy.mockResolvedValue(true);
|
||||
admitSpy.mockImplementation((who, amount, opts) =>
|
||||
server.services.metering.reserveCredits(who, amount, opts),
|
||||
);
|
||||
remainingUsageSpy = vi.spyOn(server.services.metering, 'getRemainingUsage');
|
||||
remainingUsageSpy.mockResolvedValue(AMPLE_CREDIT);
|
||||
incrementUsageSpy = vi.spyOn(server.services.metering, 'incrementUsage');
|
||||
@@ -197,7 +199,7 @@ describe('TogetherVideoProvider.generate test_mode', () => {
|
||||
provider.generate({ prompt: 'hi', test_mode: true }),
|
||||
);
|
||||
expect(result).toBe('https://assets.puter.site/txt2vid.mp4');
|
||||
expect(hasCreditsSpy).not.toHaveBeenCalled();
|
||||
expect(admitSpy).not.toHaveBeenCalled();
|
||||
expect(videosCreateMock).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -238,7 +240,7 @@ const DIRECTOR = 'togetherai:minimax/video-01-director';
|
||||
describe('TogetherVideoProvider.generate credit gate', () => {
|
||||
it('throws 402 BEFORE hitting Together when actor lacks credits', async () => {
|
||||
const provider = makeProvider();
|
||||
hasCreditsSpy.mockResolvedValueOnce(false);
|
||||
admitSpy.mockResolvedValueOnce(null);
|
||||
|
||||
await expect(
|
||||
withTestActor(() =>
|
||||
@@ -715,7 +717,7 @@ describe('TogetherVideoProvider.generate per-second models', () => {
|
||||
expect('height' in sent).toBe(false);
|
||||
expect('ratio' in sent).toBe(false);
|
||||
// The flat-rate gate is not consulted for per-second models.
|
||||
expect(hasCreditsSpy).not.toHaveBeenCalled();
|
||||
expect(admitSpy).not.toHaveBeenCalled();
|
||||
|
||||
const seedance = TOGETHER_VIDEO_GENERATION_MODELS.find(
|
||||
(m) => m.model === 'ByteDance/Seedance-2.5',
|
||||
|
||||
@@ -324,8 +324,10 @@ describe('AI cost factor', () => {
|
||||
it('gates and holds at the factored cost', async () => {
|
||||
listen(1.3);
|
||||
const check = vi
|
||||
.spyOn(metering, 'hasEnoughCredits')
|
||||
.mockResolvedValue(true);
|
||||
.spyOn(metering, 'reserveCreditsIfAffordable')
|
||||
.mockImplementation((who, amount, opts) =>
|
||||
metering.reserveCredits(who, amount, opts),
|
||||
);
|
||||
const reserve = vi.spyOn(metering, 'reserveCredits');
|
||||
try {
|
||||
const hold = await scoped.reserveAiCredits(
|
||||
@@ -334,7 +336,11 @@ describe('AI cost factor', () => {
|
||||
1000,
|
||||
);
|
||||
await hold?.release();
|
||||
expect(check).toHaveBeenCalledWith(actor, 1300);
|
||||
expect(check).toHaveBeenCalledExactlyOnceWith(
|
||||
actor,
|
||||
1300,
|
||||
undefined,
|
||||
);
|
||||
expect(reserve).toHaveBeenCalledWith(actor, 1300, undefined);
|
||||
} finally {
|
||||
check.mockRestore();
|
||||
@@ -342,10 +348,40 @@ describe('AI cost factor', () => {
|
||||
}
|
||||
});
|
||||
|
||||
it('admits one of two concurrent holds the balance only covers once', async () => {
|
||||
listen(1.3);
|
||||
vi.spyOn(metering, 'getAllowedUsage').mockResolvedValue({
|
||||
remaining: 20_000_000,
|
||||
monthUsageAllowance: 20_000_000,
|
||||
addons: {},
|
||||
});
|
||||
const holds = await Promise.all(
|
||||
[0, 1].map(() =>
|
||||
scoped.reserveAiCredits(
|
||||
actor,
|
||||
'openai:gpt-realtime:realtime:audio_output',
|
||||
12_500_000,
|
||||
),
|
||||
),
|
||||
);
|
||||
try {
|
||||
expect(holds.filter((h) => h !== null)).toHaveLength(1);
|
||||
// The factor applies once: 12.5M at 1.3 is 16.25M held.
|
||||
expect(await metering.getOutstandingHolds(actor)).toBe(
|
||||
16_250_000,
|
||||
);
|
||||
} finally {
|
||||
await Promise.all(holds.map((h) => h?.release()));
|
||||
vi.restoreAllMocks();
|
||||
}
|
||||
});
|
||||
|
||||
it('passes a hold lifetime through to the service', async () => {
|
||||
const check = vi
|
||||
.spyOn(metering, 'hasEnoughCredits')
|
||||
.mockResolvedValue(true);
|
||||
.spyOn(metering, 'reserveCreditsIfAffordable')
|
||||
.mockImplementation((who, amount, opts) =>
|
||||
metering.reserveCredits(who, amount, opts),
|
||||
);
|
||||
const reserve = vi.spyOn(metering, 'reserveCredits');
|
||||
try {
|
||||
const hold = await scoped.reserveAiCredits(
|
||||
@@ -367,8 +403,12 @@ describe('AI cost factor', () => {
|
||||
it('takes no hold when the factored cost is unaffordable', async () => {
|
||||
listen(1.3);
|
||||
const check = vi
|
||||
.spyOn(metering, 'hasEnoughCredits')
|
||||
.mockImplementation(async (_actor, amount) => amount <= 1000);
|
||||
.spyOn(metering, 'reserveCreditsIfAffordable')
|
||||
.mockImplementation(async (who, amount, opts) =>
|
||||
amount <= 1000
|
||||
? metering.reserveCredits(who, amount, opts)
|
||||
: null,
|
||||
);
|
||||
const reserve = vi.spyOn(metering, 'reserveCredits');
|
||||
try {
|
||||
const hold = await scoped.reserveAiCredits(
|
||||
@@ -409,8 +449,10 @@ describe('AI cost factor', () => {
|
||||
it('holds the factored cost while run executes, then releases it', async () => {
|
||||
listen(1.3);
|
||||
const check = vi
|
||||
.spyOn(metering, 'hasEnoughCredits')
|
||||
.mockResolvedValue(true);
|
||||
.spyOn(metering, 'reserveCreditsIfAffordable')
|
||||
.mockImplementation((who, amount, opts) =>
|
||||
metering.reserveCredits(who, amount, opts),
|
||||
);
|
||||
try {
|
||||
let heldDuringRun = 0;
|
||||
const result = await withAiCreditHold(
|
||||
@@ -448,8 +490,10 @@ describe('AI cost factor', () => {
|
||||
};
|
||||
server.clients.event.on('ai.cost.factor.*', slowFactor as never);
|
||||
const check = vi
|
||||
.spyOn(metering, 'hasEnoughCredits')
|
||||
.mockResolvedValue(true);
|
||||
.spyOn(metering, 'reserveCreditsIfAffordable')
|
||||
.mockImplementation((who, amount, opts) =>
|
||||
metering.reserveCredits(who, amount, opts),
|
||||
);
|
||||
try {
|
||||
await runWithContext({ actor }, () =>
|
||||
withAiCreditHold(
|
||||
@@ -485,8 +529,10 @@ describe('AI cost factor', () => {
|
||||
|
||||
it('releases the hold when the usage write fails', async () => {
|
||||
const check = vi
|
||||
.spyOn(metering, 'hasEnoughCredits')
|
||||
.mockResolvedValue(true);
|
||||
.spyOn(metering, 'reserveCreditsIfAffordable')
|
||||
.mockImplementation((who, amount, opts) =>
|
||||
metering.reserveCredits(who, amount, opts),
|
||||
);
|
||||
const write = vi
|
||||
.spyOn(metering, 'incrementUsage')
|
||||
.mockRejectedValueOnce(new Error('ledger down'));
|
||||
@@ -515,8 +561,10 @@ describe('AI cost factor', () => {
|
||||
|
||||
it('releases at once when nothing was recorded', async () => {
|
||||
const check = vi
|
||||
.spyOn(metering, 'hasEnoughCredits')
|
||||
.mockResolvedValue(true);
|
||||
.spyOn(metering, 'reserveCreditsIfAffordable')
|
||||
.mockImplementation((who, amount, opts) =>
|
||||
metering.reserveCredits(who, amount, opts),
|
||||
);
|
||||
try {
|
||||
await runWithContext({ actor }, () =>
|
||||
withAiCreditHold(
|
||||
@@ -535,8 +583,10 @@ describe('AI cost factor', () => {
|
||||
|
||||
it('releases the hold when run throws', async () => {
|
||||
const check = vi
|
||||
.spyOn(metering, 'hasEnoughCredits')
|
||||
.mockResolvedValue(true);
|
||||
.spyOn(metering, 'reserveCreditsIfAffordable')
|
||||
.mockImplementation((who, amount, opts) =>
|
||||
metering.reserveCredits(who, amount, opts),
|
||||
);
|
||||
const boom = new Error('upstream down');
|
||||
try {
|
||||
await expect(
|
||||
@@ -552,8 +602,8 @@ describe('AI cost factor', () => {
|
||||
|
||||
it('refuses with a 402 and never runs when the cost is unaffordable', async () => {
|
||||
const check = vi
|
||||
.spyOn(metering, 'hasEnoughCredits')
|
||||
.mockResolvedValue(false);
|
||||
.spyOn(metering, 'reserveCreditsIfAffordable')
|
||||
.mockResolvedValue(null);
|
||||
const run = vi.fn();
|
||||
try {
|
||||
await expect(
|
||||
@@ -575,8 +625,10 @@ describe('AI cost factor', () => {
|
||||
const relay = async (source: Readable) => {
|
||||
const record = vi.fn();
|
||||
const check = vi
|
||||
.spyOn(metering, 'hasEnoughCredits')
|
||||
.mockResolvedValue(true);
|
||||
.spyOn(metering, 'reserveCreditsIfAffordable')
|
||||
.mockImplementation((who, amount, opts) =>
|
||||
metering.reserveCredits(who, amount, opts),
|
||||
);
|
||||
try {
|
||||
const result = await withStreamedAiCharge(
|
||||
scoped,
|
||||
@@ -624,8 +676,10 @@ describe('AI cost factor', () => {
|
||||
landCharge = resolve;
|
||||
});
|
||||
const check = vi
|
||||
.spyOn(metering, 'hasEnoughCredits')
|
||||
.mockResolvedValue(true);
|
||||
.spyOn(metering, 'reserveCreditsIfAffordable')
|
||||
.mockImplementation((who, amount, opts) =>
|
||||
metering.reserveCredits(who, amount, opts),
|
||||
);
|
||||
try {
|
||||
const result = await withStreamedAiCharge(
|
||||
scoped,
|
||||
@@ -655,8 +709,10 @@ describe('AI cost factor', () => {
|
||||
vi.useFakeTimers({ toFake: ['setInterval', 'clearInterval'] });
|
||||
const refresh = vi.spyOn(server.stores.creditHold, 'refresh');
|
||||
const check = vi
|
||||
.spyOn(metering, 'hasEnoughCredits')
|
||||
.mockResolvedValue(true);
|
||||
.spyOn(metering, 'reserveCreditsIfAffordable')
|
||||
.mockImplementation((who, amount, opts) =>
|
||||
metering.reserveCredits(who, amount, opts),
|
||||
);
|
||||
let landCharge = () => {};
|
||||
const charge = new Promise<void>((resolve) => {
|
||||
landCharge = resolve;
|
||||
@@ -697,8 +753,10 @@ describe('AI cost factor', () => {
|
||||
vi.useFakeTimers({ toFake: ['setInterval', 'clearInterval'] });
|
||||
const refresh = vi.spyOn(server.stores.creditHold, 'refresh');
|
||||
const check = vi
|
||||
.spyOn(metering, 'hasEnoughCredits')
|
||||
.mockResolvedValue(true);
|
||||
.spyOn(metering, 'reserveCreditsIfAffordable')
|
||||
.mockImplementation((who, amount, opts) =>
|
||||
metering.reserveCredits(who, amount, opts),
|
||||
);
|
||||
let opened = () => {};
|
||||
const opening = new Promise<void>((resolve) => {
|
||||
opened = resolve;
|
||||
@@ -741,8 +799,10 @@ describe('AI cost factor', () => {
|
||||
|
||||
it('releases the hold when recording the charge throws', async () => {
|
||||
const check = vi
|
||||
.spyOn(metering, 'hasEnoughCredits')
|
||||
.mockResolvedValue(true);
|
||||
.spyOn(metering, 'reserveCreditsIfAffordable')
|
||||
.mockImplementation((who, amount, opts) =>
|
||||
metering.reserveCredits(who, amount, opts),
|
||||
);
|
||||
try {
|
||||
const result = await withStreamedAiCharge(
|
||||
scoped,
|
||||
|
||||
@@ -354,10 +354,12 @@ export function withAiCostFactor(
|
||||
? await costFactor(actor, aiModelKey(usageType))
|
||||
: 1;
|
||||
const cost = factor === 1 ? amount : scaleCost(amount, factor);
|
||||
if (!(await metering.hasEnoughCredits(actor, cost))) return null;
|
||||
return outlastingWrites(
|
||||
await metering.reserveCredits(actor, cost, opts),
|
||||
const hold = await metering.reserveCreditsIfAffordable(
|
||||
actor,
|
||||
cost,
|
||||
opts,
|
||||
);
|
||||
return hold && outlastingWrites(hold);
|
||||
};
|
||||
|
||||
const reserveCredits = async (
|
||||
|
||||
@@ -336,6 +336,58 @@ describe('PuterServer host header validation', () => {
|
||||
expect(api.headers['x-frame-options']).toBeUndefined();
|
||||
});
|
||||
|
||||
it('allows deployment subdomains to frame the app browser embed', async () => {
|
||||
const parent = `http://spreadsheet.puter.localhost:${port}`;
|
||||
for (const path of ['/embed/apps', '/embed/apps/']) {
|
||||
const res = await request(path, {
|
||||
host: `puter.localhost:${port}`,
|
||||
referer: `${parent}/sheet/1`,
|
||||
});
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.headers['x-frame-options']).toBeUndefined();
|
||||
expect(res.headers['content-security-policy']).toBe(
|
||||
`frame-ancestors 'self' ${parent}`,
|
||||
);
|
||||
expect(res.headers['cache-control']).toBe('no-store');
|
||||
expect(res.body).toContain('/dist/apps-embed.min.js');
|
||||
expect(res.body).not.toContain('/dist/bundle.min.js');
|
||||
expect(res.body).toContain(`http://api.puter.localhost:${port}`);
|
||||
}
|
||||
const gui = await request('/embed/apps', {
|
||||
host: `puter.localhost:${port}`,
|
||||
referer: `http://puter.localhost:${port}/dashboard`,
|
||||
});
|
||||
expect(gui.headers['content-security-policy']).toBe("frame-ancestors 'self'");
|
||||
const dashboard = await request('/dashboard', { host: `puter.localhost:${port}` });
|
||||
expect(dashboard.headers['x-frame-options']).toBe('SAMEORIGIN');
|
||||
const api = await request('/embed/apps', { host: `api.puter.localhost:${port}` });
|
||||
expect(api.status).toBe(404);
|
||||
});
|
||||
|
||||
it.each([
|
||||
['a user-hosted site', 'http://someone.site.puter.localhost:PORT/'],
|
||||
['a private app host', 'http://someone.app.puter.localhost:PORT/'],
|
||||
['another site', 'http://evil.test:PORT/'],
|
||||
['another port', 'http://spreadsheet.puter.localhost:1/'],
|
||||
['a suppressed referrer', undefined],
|
||||
])('keeps the embed same-origin only when framed by %s', async (_label, referer) => {
|
||||
const res = await request('/embed/apps', {
|
||||
host: `puter.localhost:${port}`,
|
||||
...(referer ? { referer: referer.replace('PORT', String(port)) } : {}),
|
||||
});
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.headers['content-security-policy']).toBe("frame-ancestors 'self'");
|
||||
});
|
||||
|
||||
it('strips SDK bootstrap query parameters from the embed route', async () => {
|
||||
const res = await request('/embed/apps?puter.app_instance_id=other&puter.api_origin=https://other.test', {
|
||||
host: `puter.localhost:${port}`,
|
||||
});
|
||||
expect(res.status).toBe(302);
|
||||
expect(res.headers.location).toBe('/embed/apps');
|
||||
expect(res.headers['x-frame-options']).toBe('SAMEORIGIN');
|
||||
});
|
||||
|
||||
it('blocks a request the ip.validate listeners veto', async () => {
|
||||
const handler = (_key: unknown, data: unknown) => {
|
||||
(data as { allow: boolean }).allow = false;
|
||||
|
||||
@@ -121,7 +121,9 @@ export interface ProjectedNotifEvent extends ProjectedEventBase {
|
||||
}
|
||||
|
||||
export type ProjectedEvent =
|
||||
ProjectedFsEvent | ProjectedKvEvent | ProjectedNotifEvent;
|
||||
| ProjectedFsEvent
|
||||
| ProjectedKvEvent
|
||||
| ProjectedNotifEvent;
|
||||
|
||||
export type GapReason =
|
||||
| 'matched_subscription_limit'
|
||||
@@ -270,7 +272,9 @@ export interface NotifPublicSubject extends SubjectSpec<
|
||||
}
|
||||
|
||||
export type PublicSubject =
|
||||
FsPublicSubject | KvPublicSubject | NotifPublicSubject;
|
||||
| FsPublicSubject
|
||||
| KvPublicSubject
|
||||
| NotifPublicSubject;
|
||||
|
||||
export interface UnpublishedInternalEvent {
|
||||
event: EventKey;
|
||||
@@ -508,6 +512,10 @@ export const UNPUBLISHED_INTERNAL_EVENTS = [
|
||||
event: 'notif.delivery.settled',
|
||||
reason: 'A node-local report on how the live socket copy of a notification went; the notification itself publishes through `notif.created`.',
|
||||
},
|
||||
{
|
||||
event: 'notif.handled',
|
||||
reason: 'A node-local report that a page saw a notification live, for extensions deciding whether to push it; not a change to the notification.',
|
||||
},
|
||||
] as const satisfies readonly UnpublishedInternalEvent[];
|
||||
|
||||
// Every `fs.*`/`kv.*`/`notif.*` bus key needs a decision. A new key that is in
|
||||
|
||||
@@ -49,6 +49,39 @@ const makeReq = (over: Partial<Request> = {}): Request =>
|
||||
...over,
|
||||
}) as unknown as Request;
|
||||
|
||||
describe('app browser embed shell', () => {
|
||||
it.each([
|
||||
[{ env: 'dev' }, '/sdk/puter.dev.js'],
|
||||
[{ env: 'prod' }, 'https://js.puter.com/v2/'],
|
||||
[{ gui_puterjs_bundle: '/custom-sdk.js' }, '/custom-sdk.js'],
|
||||
])('uses the deployment SDK configuration %j', (config, sdkUrl) => {
|
||||
const service = makeService({
|
||||
...config,
|
||||
api_base_url: 'https://api.deployment.test',
|
||||
domain: 'deployment.test',
|
||||
});
|
||||
const html = service.renderAppBrowserEmbed();
|
||||
const configJson = html.match(/id="app-browser-config">(.*?)<\/script>/)![1];
|
||||
expect(JSON.parse(configJson)).toEqual({
|
||||
apiOrigin: 'https://api.deployment.test', sdkUrl,
|
||||
});
|
||||
expect(html).toContain('/dist/apps-embed.min.js');
|
||||
expect(html).not.toContain('/dist/bundle.min.js');
|
||||
expect(html).not.toContain('gui(');
|
||||
});
|
||||
|
||||
it('escapes configured values without breaking its JSON bootstrap', () => {
|
||||
const sdkUrl = '/sdk.js?value=</script><script>unexpected()</script>';
|
||||
const html = makeService({
|
||||
gui_params: { title: '<script>unexpected()</script>' },
|
||||
gui_puterjs_bundle: sdkUrl,
|
||||
}).renderAppBrowserEmbed();
|
||||
expect(html).not.toContain('<script>unexpected()');
|
||||
const configJson = html.match(/id="app-browser-config">(.*?)<\/script>/)![1];
|
||||
expect(JSON.parse(configJson).sdkUrl).toBe(sdkUrl);
|
||||
});
|
||||
});
|
||||
|
||||
/** Capture what the service sends, and return the rendered HTML. */
|
||||
const render = async (
|
||||
service: PuterHomepageService,
|
||||
|
||||
@@ -167,6 +167,35 @@ export class PuterHomepageService extends PuterService {
|
||||
this.#guiParams[key] = val;
|
||||
}
|
||||
|
||||
renderAppBrowserEmbed(): string {
|
||||
const configJson = JSON.stringify({
|
||||
apiOrigin: this.config.api_base_url,
|
||||
sdkUrl:
|
||||
this.config.env === 'dev'
|
||||
? '/sdk/puter.dev.js'
|
||||
: (this.config.gui_puterjs_bundle ??
|
||||
'https://js.puter.com/v2/'),
|
||||
}).replace(/</g, '\\u003c');
|
||||
|
||||
return `<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<meta name="robots" content="noindex">
|
||||
<title>${encode(String(this.config.gui_params?.title ?? 'Puter'))}</title>
|
||||
<link rel="stylesheet" href="/css/normalize.css">
|
||||
<link rel="stylesheet" href="/css/dashboard.css">
|
||||
<link rel="stylesheet" href="/css/apps-embed.css">
|
||||
</head>
|
||||
<body>
|
||||
<main id="app-browser" class="dashboard"></main>
|
||||
<script type="application/json" id="app-browser-config">${configJson}</script>
|
||||
<script defer src="/dist/apps-embed.min.js"></script>
|
||||
</body>
|
||||
</html>`;
|
||||
}
|
||||
|
||||
/** `scriptPath` is a same-origin absolute path, like `/ext/push/worker.js`. */
|
||||
registerServiceWorkerScript(scriptPath: string): void {
|
||||
if (
|
||||
|
||||
@@ -4290,6 +4290,295 @@ describe('MeteringService', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('reserveCreditsIfAffordable', () => {
|
||||
const BUDGET = 20_000_000;
|
||||
const HOLD = 16_200_000;
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
/** Spends the fresh actor's allowance down to exactly `balance`. */
|
||||
const withBalance = async (balance: number) => {
|
||||
const { remaining } = await target.getAllowedUsage(actor);
|
||||
if (remaining > balance) {
|
||||
await target.incrementUsage(
|
||||
actor,
|
||||
'kv:read',
|
||||
1,
|
||||
remaining - balance,
|
||||
);
|
||||
}
|
||||
expect((await target.getAllowedUsage(actor)).remaining).toBe(
|
||||
balance,
|
||||
);
|
||||
};
|
||||
|
||||
it('admits exactly one of two concurrent holds that only fit alone', async () => {
|
||||
await withBalance(BUDGET);
|
||||
const holds = await Promise.all([
|
||||
target.reserveCreditsIfAffordable(actor, HOLD),
|
||||
target.reserveCreditsIfAffordable(actor, HOLD),
|
||||
]);
|
||||
|
||||
expect(holds.filter((h) => h !== null)).toHaveLength(1);
|
||||
expect(await target.getOutstandingHolds(actor)).toBe(HOLD);
|
||||
await Promise.all(holds.map((h) => h?.release()));
|
||||
});
|
||||
|
||||
it('compares against the gross balance, counting live holds once', async () => {
|
||||
await withBalance(BUDGET);
|
||||
const other = await target.reserveCredits(actor, 5_000_000);
|
||||
|
||||
expect(
|
||||
await target.reserveCreditsIfAffordable(actor, 15_000_001),
|
||||
).toBeNull();
|
||||
const fits = await target.reserveCreditsIfAffordable(
|
||||
actor,
|
||||
15_000_000,
|
||||
);
|
||||
expect(fits).not.toBeNull();
|
||||
expect(await target.getOutstandingHolds(actor)).toBe(BUDGET);
|
||||
|
||||
// A release gives the headroom back for a retry.
|
||||
await other.release();
|
||||
expect(
|
||||
await target.reserveCreditsIfAffordable(actor, 5_000_000),
|
||||
).not.toBeNull();
|
||||
});
|
||||
|
||||
it('counts allowance and purchased credit together', async () => {
|
||||
await withBalance(1_000_000);
|
||||
await target.updateAddonCredit(actor.user.uuid!, 2_000_000);
|
||||
|
||||
expect(
|
||||
await target.reserveCreditsIfAffordable(actor, 3_000_001),
|
||||
).toBeNull();
|
||||
expect(
|
||||
await target.reserveCreditsIfAffordable(actor, 3_000_000),
|
||||
).not.toBeNull();
|
||||
});
|
||||
|
||||
it('reads the balance again when usage lands and a hold is released after the read', async () => {
|
||||
await withBalance(BUDGET);
|
||||
// A request still running, about to record its usage and release.
|
||||
const running = await target.reserveCredits(actor, HOLD);
|
||||
const settle = async () => {
|
||||
await target.incrementUsage(actor, 'kv:read', 1, HOLD);
|
||||
await running.release();
|
||||
};
|
||||
// The admission's balance read finishes first, then the
|
||||
// settlement lands before its reservation runs.
|
||||
const read = target.getAllowedUsage.bind(target);
|
||||
let settled = false;
|
||||
vi.spyOn(target, 'getAllowedUsage').mockImplementation(
|
||||
async (...args) => {
|
||||
const balance = await read(...args);
|
||||
if (!settled) {
|
||||
settled = true;
|
||||
await settle();
|
||||
}
|
||||
return balance;
|
||||
},
|
||||
);
|
||||
|
||||
expect(await target.reserveCreditsIfAffordable(actor, HOLD)).toBe(
|
||||
null,
|
||||
);
|
||||
expect(await target.getOutstandingHolds(actor)).toBe(0);
|
||||
});
|
||||
|
||||
it('would over-admit that interleaving without the version check', async () => {
|
||||
await withBalance(BUDGET);
|
||||
const running = await target.reserveCredits(actor, HOLD);
|
||||
const read = target.getAllowedUsage.bind(target);
|
||||
let settled = false;
|
||||
vi.spyOn(target, 'getAllowedUsage').mockImplementation(
|
||||
async (...args) => {
|
||||
const balance = await read(...args);
|
||||
if (!settled) {
|
||||
settled = true;
|
||||
await target.incrementUsage(actor, 'kv:read', 1, HOLD);
|
||||
await running.release();
|
||||
}
|
||||
return balance;
|
||||
},
|
||||
);
|
||||
vi.spyOn(server.stores.creditHold, 'version').mockResolvedValue('');
|
||||
|
||||
const hold = await target.reserveCreditsIfAffordable(actor, HOLD);
|
||||
expect(hold).not.toBeNull();
|
||||
await hold?.release();
|
||||
});
|
||||
|
||||
it('marks the balance changed once a usage write lands', async () => {
|
||||
const before = await server.stores.creditHold.version(
|
||||
actor.user.uuid!,
|
||||
);
|
||||
await target.incrementUsage(actor, 'kv:read', 1, 1_000);
|
||||
expect(
|
||||
await server.stores.creditHold.version(actor.user.uuid!),
|
||||
).not.toBe(before);
|
||||
});
|
||||
|
||||
it('lets the balance decide, holding nothing, when the cache is unavailable', async () => {
|
||||
await withBalance(BUDGET);
|
||||
vi.spyOn(server.stores.creditHold, 'reserve').mockResolvedValue({
|
||||
outcome: 'unavailable',
|
||||
});
|
||||
|
||||
const hold = await target.reserveCreditsIfAffordable(actor, HOLD);
|
||||
expect(hold).not.toBeNull();
|
||||
expect(await target.getOutstandingHolds(actor)).toBe(0);
|
||||
expect(
|
||||
await target.reserveCreditsIfAffordable(actor, BUDGET + 1),
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
it('reads the balance again while it keeps changing, then refuses', async () => {
|
||||
await withBalance(BUDGET);
|
||||
const reserve = vi
|
||||
.spyOn(server.stores.creditHold, 'reserve')
|
||||
.mockResolvedValue({ outcome: 'stale' });
|
||||
|
||||
expect(await target.reserveCreditsIfAffordable(actor, HOLD)).toBe(
|
||||
null,
|
||||
);
|
||||
expect(reserve).toHaveBeenCalledTimes(5);
|
||||
// Every attempt checks the version; none falls back to skipping it.
|
||||
expect(reserve.mock.calls.every(([, , , v]) => v !== '')).toBe(
|
||||
true,
|
||||
);
|
||||
expect(await target.getOutstandingHolds(actor)).toBe(0);
|
||||
});
|
||||
|
||||
it('admits once the balance holds still', async () => {
|
||||
await withBalance(BUDGET);
|
||||
const reserve = vi.spyOn(server.stores.creditHold, 'reserve');
|
||||
reserve
|
||||
.mockResolvedValueOnce({ outcome: 'stale' })
|
||||
.mockResolvedValueOnce({ outcome: 'stale' });
|
||||
|
||||
const hold = await target.reserveCreditsIfAffordable(actor, HOLD);
|
||||
expect(hold).not.toBeNull();
|
||||
expect(reserve).toHaveBeenCalledTimes(3);
|
||||
await hold?.release();
|
||||
});
|
||||
|
||||
it('reads purchased credit consistently only while some is left', async () => {
|
||||
await withBalance(BUDGET);
|
||||
const get = vi.spyOn(server.stores.kv, 'get');
|
||||
const consistentAddonReads = () =>
|
||||
get.mock.calls.filter(
|
||||
([args]) =>
|
||||
args.consistentRead &&
|
||||
String(args.key).endsWith(':addons'),
|
||||
).length;
|
||||
// With none, the cached record that says so answers.
|
||||
await target.getAllowedUsage(actor);
|
||||
get.mockClear();
|
||||
await (
|
||||
await target.reserveCreditsIfAffordable(actor, 1000)
|
||||
)?.release();
|
||||
expect(consistentAddonReads()).toBe(0);
|
||||
|
||||
await target.updateAddonCredit(actor.user.uuid!, 5_000);
|
||||
get.mockClear();
|
||||
await (
|
||||
await target.reserveCreditsIfAffordable(actor, 1000)
|
||||
)?.release();
|
||||
expect(consistentAddonReads()).toBe(1);
|
||||
});
|
||||
|
||||
it('marks the balance changed on credit, allowance and manual usage changes', async () => {
|
||||
const uuid = actor.user.uuid!;
|
||||
const versions = [await server.stores.creditHold.version(uuid)];
|
||||
await target.updateAddonCredit(uuid, 1_000);
|
||||
versions.push(await server.stores.creditHold.version(uuid));
|
||||
await target.setActorCurrentMonthUsageTotal(actor, 5_000);
|
||||
versions.push(await server.stores.creditHold.version(uuid));
|
||||
expect(new Set(versions).size).toBe(3);
|
||||
});
|
||||
|
||||
it('treats the system actor, a userless actor and amounts it can’t hold as the plain check does', async () => {
|
||||
for (const [who, amount] of [
|
||||
[SYSTEM_ACTOR, HOLD],
|
||||
[actor, 0],
|
||||
[actor, -5],
|
||||
] as const) {
|
||||
const hold = await target.reserveCreditsIfAffordable(
|
||||
who,
|
||||
amount,
|
||||
);
|
||||
expect(hold).not.toBeNull();
|
||||
}
|
||||
for (const amount of [Number.NaN, Number.POSITIVE_INFINITY]) {
|
||||
expect(
|
||||
await target.reserveCreditsIfAffordable(actor, amount),
|
||||
).toBeNull();
|
||||
}
|
||||
await expect(
|
||||
target.reserveCreditsIfAffordable({ user: {} } as Actor, HOLD),
|
||||
).rejects.toThrow();
|
||||
expect(await target.getOutstandingHolds(actor)).toBe(0);
|
||||
});
|
||||
|
||||
it('passes the hold lifetime through and renews it', async () => {
|
||||
await withBalance(BUDGET);
|
||||
const hold = await target.reserveCreditsIfAffordable(actor, 750, {
|
||||
ttlMs: 500,
|
||||
});
|
||||
await new Promise((r) => setTimeout(r, 620));
|
||||
expect(await target.getOutstandingHolds(actor)).toBe(0);
|
||||
await hold?.extend?.();
|
||||
expect(await target.getOutstandingHolds(actor)).toBe(750);
|
||||
await hold?.release();
|
||||
expect(await target.getOutstandingHolds(actor)).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('settling a charge while a flush is out', () => {
|
||||
it('splits a charge over the allowance using usage a flush has claimed', async () => {
|
||||
const uuid = actor.user.uuid!;
|
||||
const { monthUsageAllowance: allowance } =
|
||||
await target.getActorSubscription(actor);
|
||||
await target.updateAddonCredit(uuid, 1_000_000);
|
||||
const Service = target.constructor as typeof MeteringService;
|
||||
const threshold = Service.PRECISION_THRESHOLD;
|
||||
// Keeps the first charge buffered instead of read exactly.
|
||||
Service.PRECISION_THRESHOLD = 2;
|
||||
await target.incrementUsage(actor, 'kv:read', 1, allowance - 5);
|
||||
Service.PRECISION_THRESHOLD = threshold;
|
||||
|
||||
// A flush claims it and stalls before writing it onward.
|
||||
const kv = server.stores.kv;
|
||||
const realIncr = kv.incr.bind(kv);
|
||||
let release: () => void = () => {};
|
||||
const gate = new Promise<void>((r) => (release = r));
|
||||
const write = vi
|
||||
.spyOn(kv, 'incr')
|
||||
.mockImplementation(async (...args) => {
|
||||
await gate;
|
||||
return realIncr(...args);
|
||||
});
|
||||
const flushing = server.stores.meteringBuffer.flushCycle();
|
||||
await vi.waitFor(() => expect(write).toHaveBeenCalled());
|
||||
write.mockImplementation(realIncr);
|
||||
|
||||
// 5 µ¢ of this is left in the allowance; the rest is credit.
|
||||
await target.incrementUsage(actor, 'kv:read', 1, 10);
|
||||
release();
|
||||
await flushing;
|
||||
write.mockRestore();
|
||||
await waitFor(async () => {
|
||||
const addons = await target.getActorAddons(actor, {
|
||||
consistentRead: true,
|
||||
});
|
||||
expect(addons.consumedPurchaseCredits).toBe(5);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// ── Resolver registration ────────────────────────────────────────
|
||||
|
||||
describe('resolver registration', () => {
|
||||
|
||||
@@ -698,6 +698,8 @@ export class MeteringService extends PuterService {
|
||||
actorAddons,
|
||||
totalBatchCost,
|
||||
);
|
||||
// Before any hold this usage stands for is released.
|
||||
await this.stores.creditHold.bump(userId);
|
||||
|
||||
this.maybeAlertOveruse({
|
||||
actor,
|
||||
@@ -729,6 +731,10 @@ export class MeteringService extends PuterService {
|
||||
? ({ ...own, ...charged } as UsageByType)
|
||||
: (charged ?? own);
|
||||
} catch (e) {
|
||||
// Part of the usage may have landed before the failure.
|
||||
if (actor?.user?.uuid) {
|
||||
await this.stores.creditHold.bump(actor.user.uuid);
|
||||
}
|
||||
console.error('[metering] batchIncrementUsages failed', {
|
||||
actor,
|
||||
usages,
|
||||
@@ -1432,6 +1438,7 @@ export class MeteringService extends PuterService {
|
||||
}
|
||||
|
||||
this.invalidateActorCredits(userId);
|
||||
await this.stores.creditHold.bump(userId);
|
||||
await Promise.all([
|
||||
this.stores.meteringBuffer.forgetSummed(actorUsageKey),
|
||||
this.stores.meteringBuffer.forgetSummed(
|
||||
@@ -1545,7 +1552,86 @@ export class MeteringService extends PuterService {
|
||||
opts.ttlMs,
|
||||
);
|
||||
if (!member) return NO_CREDIT_HOLD;
|
||||
return this.#holdHandle(userId, member, opts.ttlMs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Commit part of an actor's budget only if it fits beside what their other
|
||||
* operations hold; null when it doesn't. The check and the hold are one
|
||||
* step in the cache, and a usage write or release that lands after the
|
||||
* balance read sends the reservation back to read it again. When the cache
|
||||
* can't answer, the balance alone decides and nothing is held.
|
||||
*/
|
||||
async reserveCreditsIfAffordable(
|
||||
actor: Actor,
|
||||
amount: number,
|
||||
opts: { ttlMs?: number } = {},
|
||||
): Promise<CreditHold | null> {
|
||||
const userId = actor?.user?.uuid;
|
||||
if (
|
||||
!userId ||
|
||||
isSystemActor(actor) ||
|
||||
!Number.isFinite(amount) ||
|
||||
!(amount > 0)
|
||||
) {
|
||||
// Nothing to hold; the plain check decides, as it always has.
|
||||
return (await this.hasEnoughCredits(actor, amount))
|
||||
? NO_CREDIT_HOLD
|
||||
: null;
|
||||
}
|
||||
|
||||
for (let attempt = 1; ; attempt++) {
|
||||
const version = await this.stores.creditHold.version(userId);
|
||||
// Read after the version, so a write it misses bumps the version.
|
||||
const { remaining } = await this.getAllowedUsage(actor, {
|
||||
exact: true,
|
||||
});
|
||||
const budget = remaining || 0;
|
||||
const result =
|
||||
version === null
|
||||
? ({ outcome: 'unavailable' } as const)
|
||||
: await this.stores.creditHold.reserve(
|
||||
userId,
|
||||
amount,
|
||||
budget,
|
||||
version,
|
||||
opts.ttlMs,
|
||||
);
|
||||
switch (result.outcome) {
|
||||
case 'accepted':
|
||||
return this.#holdHandle(userId, result.member, opts.ttlMs);
|
||||
case 'insufficient':
|
||||
return null;
|
||||
case 'unavailable':
|
||||
return budget >= amount ? NO_CREDIT_HOLD : null;
|
||||
case 'stale':
|
||||
break;
|
||||
}
|
||||
// A balance that never holds still long enough to read is refused
|
||||
// rather than checked against a read that may be out of date.
|
||||
if (attempt >= MeteringService.RESERVE_ATTEMPTS) {
|
||||
console.warn(
|
||||
`[metering] credit reservation refused for ${userId}: balance kept changing`,
|
||||
);
|
||||
return null;
|
||||
}
|
||||
await new Promise((resolve) =>
|
||||
setTimeout(
|
||||
resolve,
|
||||
Math.random() *
|
||||
MeteringService.RESERVE_BACKOFF_MS *
|
||||
attempt,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/** Reservations tried while the balance keeps changing under them. */
|
||||
private static readonly RESERVE_ATTEMPTS = 5;
|
||||
/** Upper bound of the randomized wait before each retry, times the attempt. */
|
||||
private static readonly RESERVE_BACKOFF_MS = 20;
|
||||
|
||||
#holdHandle(userId: string, member: string, ttlMs?: number): CreditHold {
|
||||
let released = false;
|
||||
return {
|
||||
release: async () => {
|
||||
@@ -1555,11 +1641,7 @@ export class MeteringService extends PuterService {
|
||||
},
|
||||
extend: async () => {
|
||||
if (released) return;
|
||||
await this.stores.creditHold.refresh(
|
||||
userId,
|
||||
member,
|
||||
opts.ttlMs,
|
||||
);
|
||||
await this.stores.creditHold.refresh(userId, member, ttlMs);
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -1571,14 +1653,22 @@ export class MeteringService extends PuterService {
|
||||
return this.stores.creditHold.outstanding(userId);
|
||||
}
|
||||
|
||||
async getAllowedUsage(actor: Actor): Promise<{
|
||||
/**
|
||||
* `exact` reads purchased credit consistently, for a spend decision that
|
||||
* has to see the consumption a write just recorded. A cached record with no
|
||||
* credit left still answers: it can only understate the balance.
|
||||
*/
|
||||
async getAllowedUsage(
|
||||
actor: Actor,
|
||||
{ exact = false }: { exact?: boolean } = {},
|
||||
): Promise<{
|
||||
remaining: number;
|
||||
monthUsageAllowance: number;
|
||||
addons: UsageAddons;
|
||||
}> {
|
||||
const [userSubscription, addons, totals] = await Promise.all([
|
||||
this.getActorSubscription(actor),
|
||||
this.#addonsForSettle(actor),
|
||||
this.#addonsForSettle(actor, { consistentRead: exact }),
|
||||
this.currentMonthTotals(actor),
|
||||
]);
|
||||
|
||||
@@ -2245,13 +2335,16 @@ export class MeteringService extends PuterService {
|
||||
}
|
||||
|
||||
/** `getActorAddons`, through `#spentAddons`. Shared by gates and settles. */
|
||||
async #addonsForSettle(actor: Actor): Promise<UsageAddons> {
|
||||
async #addonsForSettle(
|
||||
actor: Actor,
|
||||
{ consistentRead }: { consistentRead?: boolean } = {},
|
||||
): Promise<UsageAddons> {
|
||||
const uuid = actor.user?.uuid;
|
||||
const cached = uuid ? this.#spentAddons.get(uuid) : undefined;
|
||||
if (cached && cached.expiresAt > Date.now()) return cached.addons;
|
||||
|
||||
const drops = this.#spentAddonsDrops;
|
||||
const addons = await this.getActorAddons(actor);
|
||||
const addons = await this.getActorAddons(actor, { consistentRead });
|
||||
if (!uuid) return addons;
|
||||
this.#spentAddons.delete(uuid);
|
||||
const creditLeft =
|
||||
@@ -2382,6 +2475,7 @@ export class MeteringService extends PuterService {
|
||||
// Credit that lands while the account is being turned away has to take
|
||||
// effect on the next request, not at the end of the cache window.
|
||||
this.invalidateActorCredits(userId);
|
||||
await this.stores.creditHold.bump(userId);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2429,6 +2523,7 @@ export class MeteringService extends PuterService {
|
||||
});
|
||||
// Every node drops its cached policy, and the budget answer with it.
|
||||
this.invalidateActorSubscription(userUuid);
|
||||
await this.stores.creditHold.bump(userUuid);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -23,10 +23,14 @@ import { handlerDepthOf, type Actor } from '../../core/actor.js';
|
||||
import { Context } from '../../core/context.js';
|
||||
import type { NotifEventContext } from '../events/registry.js';
|
||||
import { PuterService } from '../types.js';
|
||||
import { checkRateLimit } from '../../core/http/middleware/rateLimit.js';
|
||||
import {
|
||||
NOTIF_HANDLED_EVENT,
|
||||
NOTIF_HANDLED_PER_MINUTE,
|
||||
NotificationSocketAdapter,
|
||||
notificationAckTimeoutMs,
|
||||
notificationsFoldInEnabled,
|
||||
type NotifSocket,
|
||||
type ReceiptScope,
|
||||
} from './notificationSocket.js';
|
||||
import {
|
||||
@@ -404,6 +408,11 @@ export class NotificationService extends PuterService {
|
||||
return this.#socket().receipt(scope);
|
||||
}
|
||||
|
||||
/** Installs the `notif.handled` verb on one connection. */
|
||||
attachSocket(socket: NotifSocket, actor: Actor): void {
|
||||
this.#socket().attachSocket(socket, actor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Drop notifications past the retention window, in batches, and report how
|
||||
* many went. Deleting is all there is to it: nothing is pushed, because a
|
||||
@@ -439,6 +448,12 @@ export class NotificationService extends PuterService {
|
||||
ackTimeoutMs: () => notificationAckTimeoutMs(this.config),
|
||||
heldInPeerRegion: (userId, appUid) =>
|
||||
this.#heldInPeerRegion(userId, appUid),
|
||||
allowHandled: (userId) =>
|
||||
checkRateLimit(
|
||||
`socket:${NOTIF_HANDLED_EVENT}:${userId}`,
|
||||
NOTIF_HANDLED_PER_MINUTE,
|
||||
60_000,
|
||||
),
|
||||
});
|
||||
return this.#adapter;
|
||||
}
|
||||
|
||||
@@ -32,7 +32,10 @@ import {
|
||||
import type { EventMap } from '../../clients/event/types.js';
|
||||
import type { PuterServer } from '../../server.js';
|
||||
import { accountSocketRoom, SocketService } from '../socket/SocketService.js';
|
||||
import { NOTIF_RECEIPT_EVENT } from './notificationSocket.js';
|
||||
import {
|
||||
NOTIF_HANDLED_EVENT,
|
||||
NOTIF_RECEIPT_EVENT,
|
||||
} from './notificationSocket.js';
|
||||
import {
|
||||
allocateEphemeralPort,
|
||||
createTestUser,
|
||||
@@ -309,12 +312,13 @@ describe('live notification delivery receipts', () => {
|
||||
complete: true,
|
||||
peerRegions: false,
|
||||
responders: [{ visibilityState: 'hidden' }],
|
||||
handled: false,
|
||||
};
|
||||
expect(settled).toEqual(expected);
|
||||
expect(state.settled).toEqual([expected]);
|
||||
});
|
||||
|
||||
it('passes a tab`s focus and own display through, typed strictly', async () => {
|
||||
it('passes a tab`s focus through, typed strictly', async () => {
|
||||
const answers = [
|
||||
{
|
||||
received: true,
|
||||
@@ -326,7 +330,6 @@ describe('live notification delivery receipts', () => {
|
||||
received: true,
|
||||
visibilityState: 'nope',
|
||||
focused: 1,
|
||||
showsOwn: 'yes',
|
||||
},
|
||||
];
|
||||
for (const answer of answers) {
|
||||
@@ -341,11 +344,39 @@ describe('live notification delivery receipts', () => {
|
||||
expect(settled.outcome).toBe('received');
|
||||
expect(settled.responders).toEqual(
|
||||
expect.arrayContaining([
|
||||
{ visibilityState: 'hidden', focused: false, showsOwn: true },
|
||||
{ visibilityState: 'hidden', focused: false },
|
||||
{ visibilityState: 'unknown' },
|
||||
]),
|
||||
);
|
||||
expect(settled.responders).toHaveLength(2);
|
||||
expect(settled.handled).toBe(false);
|
||||
});
|
||||
|
||||
it('counts as handled only a visible tab that is focused, or an older one that is visible', async () => {
|
||||
const cases: Array<[Record<string, unknown>, boolean]> = [
|
||||
[{ visibilityState: 'visible', focused: true }, true],
|
||||
[{ visibilityState: 'visible', focused: false }, false],
|
||||
[{ visibilityState: 'hidden', focused: true }, false],
|
||||
[{ visibilityState: 'visible' }, true],
|
||||
];
|
||||
for (const [answer, handled] of cases) {
|
||||
const tab = await connect({ acks: [NOTIF_RECEIPT_EVENT] });
|
||||
tab.on(NOTIF_RECEIPT_EVENT, (_data: unknown, ack: Ack) =>
|
||||
ack({ received: true, ...answer }),
|
||||
);
|
||||
|
||||
const { settled } = await ask();
|
||||
|
||||
expect(settled.handled, JSON.stringify(answer)).toBe(handled);
|
||||
tab.disconnect();
|
||||
await vi.waitFor(() =>
|
||||
expect(
|
||||
state.server.services.socket.has({
|
||||
room: accountSocketRoom(state.userId),
|
||||
}),
|
||||
).toBe(false),
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
it('settles as unreceived when every tab answers that it never got it', async () => {
|
||||
@@ -549,6 +580,7 @@ describe('live receipts from an app’s own sockets', () => {
|
||||
complete: true,
|
||||
peerRegions: false,
|
||||
responders: [{ visibilityState: 'visible' }],
|
||||
handled: true,
|
||||
};
|
||||
expect(settled).toEqual(expected);
|
||||
expect(state.settled).toEqual([expected]);
|
||||
@@ -719,3 +751,59 @@ describe('app receipts where the adapter cannot count servers', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('a page reporting a notification it handled', () => {
|
||||
const { state, connect, appSocket } = withAppReceipts({ cluster: true });
|
||||
const reports: Array<EventMap['notif.handled']> = [];
|
||||
const onHandled = (_key: string, data: unknown) => {
|
||||
reports.push(data as EventMap['notif.handled']);
|
||||
};
|
||||
|
||||
beforeAll(() => {
|
||||
state.server.clients.event.on('notif.handled', onHandled);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
reports.length = 0;
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
state.server?.clients.event.off('notif.handled', onHandled);
|
||||
});
|
||||
|
||||
it('relays a desktop tab`s report for its user, without an app', async () => {
|
||||
const tab = await connect({});
|
||||
const uid = uuidv4();
|
||||
|
||||
tab.emit(NOTIF_HANDLED_EVENT, { uid });
|
||||
|
||||
await vi.waitFor(() =>
|
||||
expect(reports).toEqual([{ userId: state.userId, uid }]),
|
||||
);
|
||||
});
|
||||
|
||||
it('relays an app page`s report with the app it acts as', async () => {
|
||||
const page = await appSocket();
|
||||
const uid = uuidv4();
|
||||
|
||||
page.emit(NOTIF_HANDLED_EVENT, { uid });
|
||||
|
||||
await vi.waitFor(() =>
|
||||
expect(reports).toEqual([
|
||||
{ userId: state.userId, uid, appUid: OWN_APP },
|
||||
]),
|
||||
);
|
||||
});
|
||||
|
||||
it('drops a report without a notification uid', async () => {
|
||||
const tab = await connect({});
|
||||
|
||||
tab.emit(NOTIF_HANDLED_EVENT, { uid: 'not-a-uid' });
|
||||
tab.emit(NOTIF_HANDLED_EVENT, null);
|
||||
tab.emit(NOTIF_HANDLED_EVENT, { uid: uuidv4() });
|
||||
|
||||
await vi.waitFor(() => expect(reports).toHaveLength(1));
|
||||
await new Promise((resolve) => setTimeout(resolve, 100));
|
||||
expect(reports).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
@@ -17,7 +17,9 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import { validate as validateUuid } from 'uuid';
|
||||
import type { EventClient } from '../../clients/event/EventClient.js';
|
||||
import type { Actor } from '../../core/actor.js';
|
||||
import type {
|
||||
EventMap,
|
||||
NotifDeliveryOutcome,
|
||||
@@ -53,7 +55,10 @@ import type { IConfig } from '../../types.js';
|
||||
*/
|
||||
|
||||
export type NotifWire =
|
||||
'notif.message' | 'notif.unreads' | 'notif.ack' | 'notif.removed';
|
||||
| 'notif.message'
|
||||
| 'notif.unreads'
|
||||
| 'notif.ack'
|
||||
| 'notif.removed';
|
||||
|
||||
export const NOTIF_DELIVERY_EVENT = 'outer.notif.delivery';
|
||||
|
||||
@@ -67,6 +72,15 @@ export const DEFAULT_NOTIFICATION_ACK_TIMEOUT_MS = 5_000;
|
||||
*/
|
||||
export const NOTIF_RECEIPT_EVENT = 'notif.receipt';
|
||||
|
||||
/**
|
||||
* Sent by a page with `{ uid }` when it got that notification live while
|
||||
* visible and focused, so a region that didn't ask the page can still hear it.
|
||||
*/
|
||||
export const NOTIF_HANDLED_EVENT = 'notif.handled';
|
||||
|
||||
/** `notif.handled` reports one user's sockets may send per minute. */
|
||||
export const NOTIF_HANDLED_PER_MINUTE = 60;
|
||||
|
||||
/** One unread as the replay payload carries it. */
|
||||
export interface UnreadNotification {
|
||||
uid: string;
|
||||
@@ -122,12 +136,16 @@ const responderOf = (response: unknown): NotifDeliveryResponder | null => {
|
||||
...(typeof answer.focused === 'boolean'
|
||||
? { focused: answer.focused }
|
||||
: {}),
|
||||
...(typeof answer.showsOwn === 'boolean'
|
||||
? { showsOwn: answer.showsOwn }
|
||||
: {}),
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Visible and focused. An older client doesn't say whether it's focused, so it
|
||||
* counts while visible.
|
||||
*/
|
||||
export const handledBy = (responder: NotifDeliveryResponder): boolean =>
|
||||
responder.visibilityState === 'visible' && responder.focused !== false;
|
||||
|
||||
interface AdapterDeps {
|
||||
event: EventClient;
|
||||
socket: SocketService;
|
||||
@@ -138,6 +156,13 @@ interface AdapterDeps {
|
||||
userId: number,
|
||||
appUid: string | null,
|
||||
) => Promise<boolean>;
|
||||
/** Spends one of the user's `notif.handled` reports; false past the limit. */
|
||||
allowHandled: (userId: number) => Promise<boolean>;
|
||||
}
|
||||
|
||||
/** The part of a socket `attachSocket` installs its verb on. */
|
||||
export interface NotifSocket {
|
||||
on(event: string, listener: (...args: never[]) => void): unknown;
|
||||
}
|
||||
|
||||
type SettledDelivery = EventMap['notif.delivery.settled'];
|
||||
@@ -253,6 +278,7 @@ export class NotificationSocketAdapter {
|
||||
complete: asked.complete,
|
||||
peerRegions,
|
||||
responders,
|
||||
handled: responders.some(handledBy),
|
||||
};
|
||||
this.#deps.event.emit(
|
||||
'notif.delivery.settled',
|
||||
@@ -266,6 +292,28 @@ export class NotificationSocketAdapter {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Relays a page's `notif.handled` as the node-local `notif.handled` event,
|
||||
* scoped to the socket's user and app.
|
||||
*/
|
||||
attachSocket(socket: NotifSocket, actor: Actor): void {
|
||||
const userId = actor.user?.id;
|
||||
if (userId === undefined) return;
|
||||
const appUid = actor.effectiveApp?.uid;
|
||||
socket.on(NOTIF_HANDLED_EVENT, ((payload: unknown) => {
|
||||
const uid = (payload as { uid?: unknown } | null)?.uid;
|
||||
if (typeof uid !== 'string' || !validateUuid(uid)) return;
|
||||
void this.#deps.allowHandled(userId).then((ok) => {
|
||||
if (!ok) return;
|
||||
this.#deps.event.emit(
|
||||
'notif.handled',
|
||||
{ userId, uid, ...(appUid ? { appUid } : {}) },
|
||||
{},
|
||||
);
|
||||
});
|
||||
}) as (...args: never[]) => void);
|
||||
}
|
||||
|
||||
/** Whether this region holds a socket for the recipient right now. */
|
||||
hasSocket(userId: number): boolean {
|
||||
return this.#deps.socket.has({ room: userId });
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -11,7 +11,10 @@ Direct clients are documented in the [Realtime WebSocket API reference](../../..
|
||||
|
||||
The relay forwards native OpenAI JSON events unchanged after validating the
|
||||
event envelope, except that `rate_limits.updated` (Puter's provider account) is
|
||||
dropped and provider organization IDs are removed from error messages. An event must be an object with a nonempty string `type` of at
|
||||
dropped and provider organization IDs are removed from diagnostic messages in
|
||||
`error`, `response.done.status_details.error`, and
|
||||
`conversation.item.input_audio_transcription.failed.error`, and from provider
|
||||
close reasons. Conversation content is preserved. An event must be an object with a nonempty string `type` of at
|
||||
most 128 characters. Binary messages and malformed envelopes close the session.
|
||||
Usage fields consumed by metering are validated before use. Event field order
|
||||
does not affect billing.
|
||||
@@ -46,7 +49,8 @@ traffic is not rate limited: OpenAI sends audio faster than real time, and
|
||||
backpressure bounds what a slow client can queue. Usage is priced as each event
|
||||
arrives, before any forwarding decision, and recorded serially off the
|
||||
forwarding path through the AI cost-factor hook. Pricing follows the model the
|
||||
session reports, falling back to the connection's; a client `session.update`
|
||||
session reports. A missing model uses the last known model, while an explicitly
|
||||
unpriced model closes the connection; a client `session.update`
|
||||
that changes the model is refused. So are `conversation.item.retrieve` and MCP
|
||||
tools in `session.update` or `response.create`, since a retrieved item or MCP
|
||||
output can exceed the upstream message cap.
|
||||
@@ -65,9 +69,53 @@ Each response holds a fixed amount of the account's budget
|
||||
until its usage is recorded, so concurrent sessions can't overdraw it; a session
|
||||
opens only if one hold fits. Input transcription is priced at the costlier of
|
||||
the transcription models in force at commit and at completion. Session attempts
|
||||
are limited per credential before authentication runs.
|
||||
are limited per credential after signature verification. Local, bounded IP and
|
||||
node admission limits protect signature verification and authentication lookups.
|
||||
Upgrades use Express's configured proxy trust policy and the `ip.validate` gate.
|
||||
Authentication uses `touch: false` on both initial connection and rechecks, so
|
||||
opening or maintaining a realtime socket does not update session activity metadata.
|
||||
|
||||
Input transcription holds credit per item, at a fixed budget for the selected
|
||||
transcription model and that model's cost factor. Audio and the configuration
|
||||
that enables transcription are forwarded only while a reserved spare item
|
||||
exists. Each provider commit takes one, and the pool refills to two spares in
|
||||
the background, up to 64 pending plus spare items. A commit with no spare left
|
||||
reserves its own item. A model change, requested or reported, releases the spares
|
||||
and reserves new ones at the new model's price. Committed items keep their hold
|
||||
and model. Disabling transcription keeps the spares until the connection ends.
|
||||
If the account can't cover another item, client reads pause while the session
|
||||
still holds items that may free credit, and the session is refused with
|
||||
`insufficient_funds` once it holds none.
|
||||
|
||||
Audio the provider may commit without the relay seeing the acknowledgement is
|
||||
tracked apart from the pool, as a duration at the session's input format. Without
|
||||
turn detection that is all audio since the last acknowledged commit or clear.
|
||||
With server turn detection it is only speech from `speech_started`, since the
|
||||
provider drops silence, plus the buffer after an explicit commit. Each started
|
||||
160 seconds of it claims one held item, and an acknowledged commit, a clear, or
|
||||
an `input_audio_buffer_commit_empty` error returns those items to the spares.
|
||||
The session is never closed for its length. If the upstream closes first, each
|
||||
claimed item is billed. Each committed item retains its model
|
||||
until completion, failure, or bounded settlement. Missing usage is billed the
|
||||
item's fixed budget. Overflow closes the connection without evicting existing
|
||||
pending identities.
|
||||
|
||||
The post-record continuation check asks whether settled credit remains. It
|
||||
excludes outstanding holds and accepts fractional positive credit. Admission of
|
||||
new billable work continues to use the atomic hold path, which includes every
|
||||
outstanding reservation.
|
||||
|
||||
Text, transcripts and raw audio are forwarded without new content logging.
|
||||
The relay does not invoke `ai.prompt.validate`. A realtime adapter needs the host
|
||||
listeners' chat payload and denial contracts before that hook can safely apply
|
||||
to instructions and conversation text. Raw audio and generated transcripts need
|
||||
separate policy decisions.
|
||||
|
||||
Credential checks run in batches of at most 16 with no overlapping sweeps.
|
||||
Session and access-token revocation events close affected sockets and invalidate
|
||||
their auth cache entries. Subscription eligibility is checked independently of
|
||||
authentication and after subscription invalidations. A failed verification check after a 60-second
|
||||
retry grace from the first failed lookup closes with a retryable internal error. Periodic checks remain the missed-event fallback.
|
||||
When the drain starts, open sessions get a `puter.reconnect` event and new
|
||||
sessions receive `server_restarting` and close with 1012. Open sessions can
|
||||
finish an in-flight response for up to five seconds at shutdown, then close
|
||||
|
||||
@@ -699,7 +699,8 @@ export class SocketService extends PuterService {
|
||||
// `{ auth: { ... } }`, not the query string. puter-js uses
|
||||
// `io(url, { auth: { auth_token } })`.
|
||||
const handshakeAuth = socket.handshake.auth as
|
||||
Record<string, unknown> | undefined;
|
||||
| Record<string, unknown>
|
||||
| undefined;
|
||||
const tokenRaw =
|
||||
typeof handshakeAuth?.auth_token === 'string'
|
||||
? handshakeAuth.auth_token
|
||||
@@ -1085,6 +1086,7 @@ export class SocketService extends PuterService {
|
||||
// them. Off unless events are enabled, in which case the verbs
|
||||
// answer with `events_disabled` rather than going unanswered.
|
||||
this.services.events.attachSocket(socket, actor);
|
||||
this.services.notification.attachSocket(socket, actor);
|
||||
|
||||
// Everything below is the desktop session's own traffic: two verbs
|
||||
// that reach the user's other tabs, and a connect announcement
|
||||
@@ -1221,7 +1223,8 @@ export class SocketService extends PuterService {
|
||||
// their next poll of /cache/last-change-timestamp.
|
||||
const originalSocketId = (
|
||||
data.response as
|
||||
{ original_client_socket_id?: string } | undefined
|
||||
| { original_client_socket_id?: string }
|
||||
| undefined
|
||||
)?.original_client_socket_id;
|
||||
await this.send({ room: userId }, 'cache.updated', {
|
||||
timestamp,
|
||||
@@ -1235,7 +1238,9 @@ export class SocketService extends PuterService {
|
||||
#handleUploadProgress(data: UploadProgressPayload): void {
|
||||
const meta = data.meta ?? {};
|
||||
const userId = (meta.user_id ?? meta.userId) as
|
||||
number | string | undefined;
|
||||
| number
|
||||
| string
|
||||
| undefined;
|
||||
if (!userId) {
|
||||
console.warn('[socket] upload-progress missing user_id', { meta });
|
||||
return;
|
||||
|
||||
@@ -21,6 +21,7 @@ import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import { PuterServer } from '../../server.js';
|
||||
import { setupTestServer } from '../../testUtil.js';
|
||||
import type { IConfig, IRedisConfig } from '../../types.js';
|
||||
import type { CreditHoldStore } from './CreditHoldStore.js';
|
||||
|
||||
let server: PuterServer;
|
||||
@@ -142,3 +143,204 @@ describe('CreditHoldStore', () => {
|
||||
spy.mockRestore();
|
||||
});
|
||||
});
|
||||
|
||||
// Opt in to a real Redis Cluster with PUTER_TEST_REDIS_CLUSTER=host:port.
|
||||
const clusterNode = process.env.PUTER_TEST_REDIS_CLUSTER;
|
||||
const backends: { name: string; redis: IRedisConfig }[] = [
|
||||
{ name: 'the redis mock', redis: { useMock: true } },
|
||||
...(clusterNode
|
||||
? [
|
||||
{
|
||||
name: `redis cluster ${clusterNode}`,
|
||||
redis: {
|
||||
useMock: false,
|
||||
tls: false,
|
||||
startupNodes: [
|
||||
{
|
||||
host: clusterNode.split(':')[0],
|
||||
port: Number(clusterNode.split(':')[1]),
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
]
|
||||
: []),
|
||||
];
|
||||
|
||||
describe.each(backends)('CreditHoldStore.reserve on $name', ({ redis }) => {
|
||||
// Two backends sharing one cache, as separate instances do in production.
|
||||
let servers: PuterServer[];
|
||||
let a: CreditHoldStore;
|
||||
let b: CreditHoldStore;
|
||||
const BUDGET = 20_000_000;
|
||||
const HOLD = 16_200_000;
|
||||
|
||||
beforeAll(async () => {
|
||||
servers = await Promise.all(
|
||||
[0, 1].map(() => setupTestServer({ redis } as IConfig)),
|
||||
);
|
||||
[a, b] = servers.map((s) => s.stores.creditHold as CreditHoldStore);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await Promise.all((servers ?? []).map((s) => s.shutdown()));
|
||||
});
|
||||
|
||||
const reserve = async (
|
||||
on: CreditHoldStore,
|
||||
id: string,
|
||||
amount: number,
|
||||
budget = BUDGET,
|
||||
ttlMs?: number,
|
||||
) => on.reserve(id, amount, budget, (await on.version(id))!, ttlMs);
|
||||
|
||||
it('admits exactly one of two concurrent holds that only fit alone', async () => {
|
||||
const id = user();
|
||||
const results = await Promise.all([
|
||||
reserve(a, id, HOLD),
|
||||
reserve(b, id, HOLD),
|
||||
]);
|
||||
|
||||
expect(results.map((r) => r.outcome).sort()).toEqual([
|
||||
'accepted',
|
||||
'insufficient',
|
||||
]);
|
||||
expect(await a.outstanding(id)).toBe(HOLD);
|
||||
expect(await b.outstanding(id)).toBe(HOLD);
|
||||
});
|
||||
|
||||
it('admits exact headroom and refuses one unit more', async () => {
|
||||
const id = user();
|
||||
expect((await reserve(a, id, 15_000_000)).outcome).toBe('accepted');
|
||||
expect((await reserve(b, id, 5_000_001)).outcome).toBe('insufficient');
|
||||
expect((await reserve(b, id, 5_000_000)).outcome).toBe('accepted');
|
||||
expect(await a.outstanding(id)).toBe(BUDGET);
|
||||
});
|
||||
|
||||
it('rounds a fractional amount up once', async () => {
|
||||
const id = user();
|
||||
expect((await reserve(a, id, 9.2, 10)).outcome).toBe('accepted');
|
||||
expect(await a.outstanding(id)).toBe(10);
|
||||
expect((await reserve(a, id, 0.5, 10)).outcome).toBe('insufficient');
|
||||
});
|
||||
|
||||
it('admits several holds that fit together, and keeps actors apart', async () => {
|
||||
const [id, other] = [user(), user()];
|
||||
const results = await Promise.all(
|
||||
[a, b, a, b].map((s) => reserve(s, id, 5_000_000)),
|
||||
);
|
||||
expect(results.every((r) => r.outcome === 'accepted')).toBe(true);
|
||||
expect((await reserve(a, other, HOLD)).outcome).toBe('accepted');
|
||||
expect(await a.outstanding(id)).toBe(BUDGET);
|
||||
expect(await a.outstanding(other)).toBe(HOLD);
|
||||
});
|
||||
|
||||
it('leaves nothing behind when it refuses', async () => {
|
||||
const id = user();
|
||||
await reserve(a, id, HOLD);
|
||||
await reserve(b, id, HOLD);
|
||||
await reserve(b, id, HOLD, 0);
|
||||
expect(await a.outstanding(id)).toBe(HOLD);
|
||||
});
|
||||
|
||||
it('frees headroom on release and on expiry', async () => {
|
||||
const id = user();
|
||||
const first = await reserve(a, id, HOLD);
|
||||
expect((await reserve(b, id, HOLD)).outcome).toBe('insufficient');
|
||||
await a.release(id, first.outcome === 'accepted' ? first.member : null);
|
||||
expect((await reserve(b, id, HOLD, BUDGET, 50)).outcome).toBe(
|
||||
'accepted',
|
||||
);
|
||||
await vi.waitFor(
|
||||
async () =>
|
||||
expect((await reserve(a, id, HOLD)).outcome).toBe('accepted'),
|
||||
{ timeout: 2000, interval: 25 },
|
||||
);
|
||||
});
|
||||
|
||||
it('keeps a long hold when a short one is reserved beside it', async () => {
|
||||
const id = user();
|
||||
await reserve(a, id, 1000);
|
||||
await reserve(b, id, 500, BUDGET, 50);
|
||||
const pttl = await (
|
||||
servers[0].clients.redis as unknown as {
|
||||
pttl: (key: string) => Promise<number>;
|
||||
}
|
||||
).pttl(`meter:holds:{${id}}`);
|
||||
expect(pttl).toBeGreaterThan(500_000);
|
||||
});
|
||||
|
||||
it('refuses a budget read before a usage write landed, but not one before a release', async () => {
|
||||
const id = user();
|
||||
// A release alone leaves the balance as it was.
|
||||
const before = (await a.version(id))!;
|
||||
const held = await reserve(a, id, 1000);
|
||||
await b.release(id, held.outcome === 'accepted' ? held.member : null);
|
||||
const after = await a.reserve(id, 1000, BUDGET, before);
|
||||
expect(after.outcome).toBe('accepted');
|
||||
await a.release(id, after.outcome === 'accepted' ? after.member : null);
|
||||
|
||||
const read = (await a.version(id))!;
|
||||
await b.bump(id);
|
||||
expect((await a.reserve(id, 1000, BUDGET, read)).outcome).toBe('stale');
|
||||
expect(await a.outstanding(id)).toBe(0);
|
||||
|
||||
// Skipping the check still can't overlap another hold.
|
||||
await reserve(a, id, HOLD);
|
||||
expect((await a.reserve(id, HOLD, BUDGET, '')).outcome).toBe(
|
||||
'insufficient',
|
||||
);
|
||||
});
|
||||
|
||||
it('keeps renewal and release consistent when they race', async () => {
|
||||
const id = user();
|
||||
const held = await reserve(a, id, HOLD, BUDGET, 200);
|
||||
const member = held.outcome === 'accepted' ? held.member : null;
|
||||
await Promise.all([a.refresh(id, member), b.release(id, member)]);
|
||||
// Whichever ran last decides; a renewal that lost the race only
|
||||
// brings the hold back until its owner releases it again.
|
||||
await a.release(id, member);
|
||||
expect(await a.outstanding(id)).toBe(0);
|
||||
expect((await reserve(b, id, HOLD)).outcome).toBe('accepted');
|
||||
});
|
||||
|
||||
it('refuses amounts it can’t hold without touching the hold set', async () => {
|
||||
const id = user();
|
||||
await reserve(a, id, 1000);
|
||||
for (const amount of [0, -5_000, Number.NaN, Infinity]) {
|
||||
expect((await reserve(a, id, amount)).outcome).toBe('insufficient');
|
||||
}
|
||||
expect(await a.outstanding(id)).toBe(1000);
|
||||
});
|
||||
|
||||
it('starts a missing version from a random value', async () => {
|
||||
const id = user();
|
||||
expect(await a.version(id)).toBe('0');
|
||||
await b.bump(id);
|
||||
const first = (await a.version(id))!;
|
||||
expect(first).not.toBe('1');
|
||||
await b.bump(id);
|
||||
expect(Number(await a.version(id))).toBe(Number(first) + 1);
|
||||
});
|
||||
|
||||
it('reports an unreachable cache as unavailable, not as insufficient', async () => {
|
||||
const id = user();
|
||||
const client = servers[0].clients.redis as unknown as {
|
||||
creditHoldReserve: () => Promise<unknown>;
|
||||
get: () => Promise<unknown>;
|
||||
};
|
||||
const reserveSpy = vi
|
||||
.spyOn(client, 'creditHoldReserve')
|
||||
.mockRejectedValueOnce(new Error('cache down'));
|
||||
expect((await a.reserve(id, 1000, BUDGET, '0')).outcome).toBe(
|
||||
'unavailable',
|
||||
);
|
||||
const getSpy = vi
|
||||
.spyOn(client, 'get')
|
||||
.mockRejectedValueOnce(new Error('cache down'));
|
||||
expect(await a.version(id)).toBeNull();
|
||||
reserveSpy.mockRestore();
|
||||
getSpy.mockRestore();
|
||||
expect(await a.outstanding(id)).toBe(0);
|
||||
});
|
||||
});
|
||||
@@ -48,6 +48,25 @@ const KEY_TTL_SLACK_MS = 60 * 1000;
|
||||
|
||||
const holdsKey = (userId: string): string => `meter:holds:{${userId}}`;
|
||||
|
||||
/**
|
||||
* Counts changes to an actor's balance and holds, so a reservation can tell
|
||||
* whether the balance it read is still current. Shares the hold key's slot.
|
||||
*/
|
||||
const versionKey = (userId: string): string =>
|
||||
`meter:holds:{${userId}}:version`;
|
||||
|
||||
/** Outlives any read-then-reserve window by orders of magnitude. */
|
||||
const VERSION_TTL_MS = 60 * 60 * 1000;
|
||||
|
||||
/** What a conditional reservation came to. */
|
||||
export type CreditReservation =
|
||||
| { outcome: 'accepted'; member: string }
|
||||
| { outcome: 'insufficient' }
|
||||
// The balance changed since `version` was read; read it again.
|
||||
| { outcome: 'stale' }
|
||||
// The cache couldn't answer; nothing was held.
|
||||
| { outcome: 'unavailable' };
|
||||
|
||||
// -- Scripts ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
@@ -79,8 +98,8 @@ return 1
|
||||
* name (`<id>:<amount>`) so one sorted set carries both the deadline and the
|
||||
* number, and pruning and summing happen in the same pass.
|
||||
*/
|
||||
const SUM_SCRIPT = `
|
||||
local dropped = redis.call('ZREMRANGEBYSCORE', KEYS[1], '-inf', ARGV[1])
|
||||
const pruneAndSum = (now: string) => `
|
||||
local dropped = redis.call('ZREMRANGEBYSCORE', KEYS[1], '-inf', ${now})
|
||||
local total = 0
|
||||
local members = redis.call('ZRANGE', KEYS[1], 0, -1)
|
||||
for i = 1, #members do
|
||||
@@ -89,13 +108,65 @@ for i = 1, #members do
|
||||
total = total + (tonumber(string.sub(members[i], sep + 1)) or 0)
|
||||
end
|
||||
end
|
||||
`;
|
||||
|
||||
const SUM_SCRIPT = `${pruneAndSum('ARGV[1]')}
|
||||
return { tostring(total), dropped }
|
||||
`;
|
||||
|
||||
/**
|
||||
* KEYS: the actor's hold set, its version. ARGV: member, deadline, key ttl,
|
||||
* now, amount, budget, expected version ('' skips the check).
|
||||
*
|
||||
* Takes the hold only if it fits in `budget` beside the live holds, in one step
|
||||
* so two reservations can't both fit in the same headroom. The version check
|
||||
* refuses a budget read before a usage write or release that has since landed.
|
||||
*/
|
||||
const RESERVE_SCRIPT = `
|
||||
if ARGV[7] ~= '' and (redis.call('GET', KEYS[2]) or '0') ~= ARGV[7] then
|
||||
return { 'stale', 0 }
|
||||
end
|
||||
${pruneAndSum('ARGV[4]')}
|
||||
if total + tonumber(ARGV[5]) > tonumber(ARGV[6]) then
|
||||
return { 'insufficient', dropped }
|
||||
end
|
||||
local ttl = redis.call('PTTL', KEYS[1])
|
||||
redis.call('ZADD', KEYS[1], ARGV[2], ARGV[1])
|
||||
if tonumber(ARGV[3]) > ttl then
|
||||
redis.call('PEXPIRE', KEYS[1], ARGV[3])
|
||||
end
|
||||
return { 'accepted', dropped }
|
||||
`;
|
||||
|
||||
/**
|
||||
* Bumps the version in KEYS[n], starting a missing one from a random value so a
|
||||
* version read before the key expired can't match its successor.
|
||||
*/
|
||||
const bumpVersion = (key: string, seed: string, ttl: string) => `
|
||||
if redis.call('EXISTS', ${key}) == 0 then
|
||||
redis.call('SET', ${key}, ${seed})
|
||||
end
|
||||
redis.call('INCR', ${key})
|
||||
redis.call('PEXPIRE', ${key}, ${ttl})
|
||||
`;
|
||||
|
||||
/** KEYS: the actor's hold version. ARGV: version ttl, seed. */
|
||||
const BUMP_SCRIPT = `
|
||||
${bumpVersion('KEYS[1]', 'ARGV[2]', 'ARGV[1]')}
|
||||
return 1
|
||||
`;
|
||||
|
||||
/** A random starting version; see `bumpVersion`. */
|
||||
const versionSeed = (): string =>
|
||||
String(Math.floor((Math.random() * Number.MAX_SAFE_INTEGER) / 2));
|
||||
|
||||
type ScriptRunner = {
|
||||
creditHoldTake(...args: string[]): Promise<number>;
|
||||
creditHoldSum(...args: string[]): Promise<[string, number]>;
|
||||
creditHoldReserve(...args: string[]): Promise<[string, number]>;
|
||||
zrem(key: string, member: string): Promise<number>;
|
||||
creditHoldBump(...args: string[]): Promise<number>;
|
||||
get(key: string): Promise<string | null>;
|
||||
};
|
||||
|
||||
// -- CreditHoldStore --------------------------------------------------
|
||||
@@ -135,6 +206,14 @@ export class CreditHoldStore extends PuterStore {
|
||||
numberOfKeys: 1,
|
||||
lua: SUM_SCRIPT,
|
||||
});
|
||||
client.defineCommand('creditHoldReserve', {
|
||||
numberOfKeys: 2,
|
||||
lua: RESERVE_SCRIPT,
|
||||
});
|
||||
client.defineCommand('creditHoldBump', {
|
||||
numberOfKeys: 1,
|
||||
lua: BUMP_SCRIPT,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -195,7 +274,92 @@ export class CreditHoldStore extends PuterStore {
|
||||
}
|
||||
}
|
||||
|
||||
/** Give a hold back. Safe to call twice; the second call is a no-op. */
|
||||
/**
|
||||
* The actor's balance version, to pass to `reserve` once the balance has
|
||||
* been read. Null when the cache can't answer.
|
||||
*/
|
||||
async version(userId: string): Promise<string | null> {
|
||||
try {
|
||||
return (await this.#redis.get(versionKey(userId))) ?? '0';
|
||||
} catch (e) {
|
||||
console.warn(
|
||||
`[metering] credit hold version unreadable for ${userId}: ${(e as Error).message}`,
|
||||
);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Commit `amount` only if it fits in `budget` beside the holds already
|
||||
* live, as one step. `budget` is the gross balance read after `version`;
|
||||
* pass `version: ''` to skip the staleness check.
|
||||
*/
|
||||
async reserve(
|
||||
userId: string,
|
||||
amount: number,
|
||||
budget: number,
|
||||
version: string,
|
||||
ttlMs: number = DEFAULT_HOLD_TTL_MS,
|
||||
): Promise<CreditReservation> {
|
||||
// Nothing to hold, and a negative hold would lower everyone else's.
|
||||
if (!userId || !Number.isFinite(amount) || amount <= 0) {
|
||||
return { outcome: 'insufficient' };
|
||||
}
|
||||
const units = Math.ceil(amount);
|
||||
const member = `${randomUUID()}:${units}`;
|
||||
const now = Date.now();
|
||||
try {
|
||||
const [outcome, dropped] = await this.#redis.creditHoldReserve(
|
||||
holdsKey(userId),
|
||||
versionKey(userId),
|
||||
member,
|
||||
String(now + ttlMs),
|
||||
String(ttlMs + KEY_TTL_SLACK_MS),
|
||||
String(now),
|
||||
String(units),
|
||||
String(Math.floor(Math.min(budget, Number.MAX_SAFE_INTEGER))),
|
||||
version,
|
||||
);
|
||||
if (dropped > 0) expiredHoldsCounter.add(dropped);
|
||||
if (outcome === 'accepted') return { outcome, member };
|
||||
if (outcome === 'insufficient' || outcome === 'stale') {
|
||||
return { outcome };
|
||||
}
|
||||
throw new Error(`unexpected reservation outcome ${outcome}`);
|
||||
} catch (e) {
|
||||
console.warn(
|
||||
`[metering] credit hold not reserved for ${userId}: ${(e as Error).message}`,
|
||||
);
|
||||
return { outcome: 'unavailable' };
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Marks the actor's balance as changed, so a reservation that read it
|
||||
* before the change reads it again. Call once a usage write has landed.
|
||||
*/
|
||||
async bump(userId: string): Promise<void> {
|
||||
if (!userId) return;
|
||||
try {
|
||||
await this.#redis.creditHoldBump(
|
||||
versionKey(userId),
|
||||
String(VERSION_TTL_MS),
|
||||
versionSeed(),
|
||||
);
|
||||
} catch (e) {
|
||||
console.warn(
|
||||
`[metering] credit hold version not bumped for ${userId}: ${(e as Error).message}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Give a hold back. Safe to call twice; the second call releases nothing.
|
||||
*
|
||||
* The version isn't bumped here: the usage a hold stands for bumps it when
|
||||
* it's recorded, before the hold is released, and a release with no usage
|
||||
* leaves the balance as it was.
|
||||
*/
|
||||
async release(userId: string, member: string | null): Promise<void> {
|
||||
if (!userId || !member) return;
|
||||
try {
|
||||
|
||||
@@ -420,6 +420,625 @@ describe('MeteringBufferStore', () => {
|
||||
expect(await storedTotal(key)).toBe(0);
|
||||
});
|
||||
|
||||
/** Holds `method` on `target` until the returned release is called. */
|
||||
const holdCalls = <T extends object>(on: T, method: keyof T) => {
|
||||
let release: () => void = () => {};
|
||||
const gate = new Promise<void>((r) => (release = r));
|
||||
const real = (on[method] as (...a: unknown[]) => unknown).bind(on);
|
||||
const spy = vi
|
||||
.spyOn(on, method as never)
|
||||
.mockImplementation((async (...args: unknown[]) => {
|
||||
await gate;
|
||||
return real(...args);
|
||||
}) as never);
|
||||
return { spy, release };
|
||||
};
|
||||
const readTotal = async () =>
|
||||
((await target.get({ key })).res as { total?: number } | null)
|
||||
?.total ?? 0;
|
||||
|
||||
it('counts usage a flush has claimed but not yet written onward', async () => {
|
||||
await target.incr({ key, pathAndAmountMap: { total: 10 } });
|
||||
const write = holdCalls(kv, 'incr');
|
||||
try {
|
||||
const flushing = target.flushCycle();
|
||||
await vi.waitFor(() => expect(write.spy).toHaveBeenCalled());
|
||||
expect(await readTotal()).toBe(10);
|
||||
write.release();
|
||||
await flushing;
|
||||
} finally {
|
||||
write.spy.mockRestore();
|
||||
}
|
||||
expect(await readTotal()).toBe(10);
|
||||
expect(await storedTotal(key)).toBe(10);
|
||||
});
|
||||
|
||||
it('counts usage written onward until its settle lands', async () => {
|
||||
await target.incr({ key, pathAndAmountMap: { total: 10 } });
|
||||
await target.flushCycle();
|
||||
await target.incr({ key, pathAndAmountMap: { total: 5 } });
|
||||
const redis = server.clients.redis as unknown as {
|
||||
meterSettle: (...args: string[]) => Promise<number>;
|
||||
};
|
||||
const settle = holdCalls(redis, 'meterSettle');
|
||||
try {
|
||||
const flushing = target.flushCycle();
|
||||
await vi.waitFor(() => expect(settle.spy).toHaveBeenCalled());
|
||||
expect(await storedTotal(key)).toBe(15);
|
||||
expect(await readTotal()).toBe(15);
|
||||
settle.release();
|
||||
await flushing;
|
||||
} finally {
|
||||
settle.spy.mockRestore();
|
||||
}
|
||||
expect(await readTotal()).toBe(15);
|
||||
});
|
||||
|
||||
it('counts a claim in array reads too', async () => {
|
||||
await target.incr({ key, pathAndAmountMap: { total: 4 } });
|
||||
const write = holdCalls(kv, 'incr');
|
||||
try {
|
||||
const flushing = target.flushCycle();
|
||||
await vi.waitFor(() => expect(write.spy).toHaveBeenCalled());
|
||||
const { res } = await target.get({ key: [key] });
|
||||
expect((res as [{ total: number }])[0].total).toBe(4);
|
||||
write.release();
|
||||
await flushing;
|
||||
} finally {
|
||||
write.spy.mockRestore();
|
||||
}
|
||||
});
|
||||
|
||||
it('counts a claim in the running total incr returns', async () => {
|
||||
await target.incr({ key, pathAndAmountMap: { total: 10 } });
|
||||
const write = holdCalls(kv, 'incr');
|
||||
try {
|
||||
const flushing = target.flushCycle();
|
||||
await vi.waitFor(() => expect(write.spy).toHaveBeenCalled());
|
||||
const { res } = await target.incr({
|
||||
key,
|
||||
pathAndAmountMap: { total: 5 },
|
||||
});
|
||||
expect((res as { total: number }).total).toBe(15);
|
||||
write.release();
|
||||
await flushing;
|
||||
} finally {
|
||||
write.spy.mockRestore();
|
||||
}
|
||||
});
|
||||
|
||||
it('counts another flush’s claim in an exact read', async () => {
|
||||
await target.incr({ key, pathAndAmountMap: { total: 10 } });
|
||||
const write = holdCalls(kv, 'incr');
|
||||
try {
|
||||
const flushing = target.flushCycle();
|
||||
await vi.waitFor(() => expect(write.spy).toHaveBeenCalled());
|
||||
const { res } = await target.readExact({ key });
|
||||
expect((res as { total: number }).total).toBe(10);
|
||||
write.release();
|
||||
await flushing;
|
||||
} finally {
|
||||
write.spy.mockRestore();
|
||||
}
|
||||
const { res } = await target.readExact({ key });
|
||||
expect((res as { total: number }).total).toBe(10);
|
||||
});
|
||||
|
||||
it('misses nothing in an exact read when a flush lands between its reads', async () => {
|
||||
await target.incr({ key, pathAndAmountMap: { total: 10 } });
|
||||
const write = holdCalls(kv, 'incr');
|
||||
const flushing = target.flushCycle();
|
||||
await vi.waitFor(() => expect(write.spy).toHaveBeenCalled());
|
||||
|
||||
// The store read sees the counter from before the flush's write;
|
||||
// the flush then writes it and leaves its claim before any claim
|
||||
// read that follows the store read.
|
||||
let storeReadStarted = false;
|
||||
let flushed!: Promise<void>;
|
||||
const realGet = kv.get.bind(kv);
|
||||
const get = vi.spyOn(kv, 'get').mockImplementation(async (args) => {
|
||||
if (!args.consistentRead || args.key !== key) {
|
||||
return realGet(args);
|
||||
}
|
||||
if (storeReadStarted) return realGet(args);
|
||||
storeReadStarted = true;
|
||||
const before = await realGet(args);
|
||||
write.release();
|
||||
flushed = flushing.then(() => {});
|
||||
await flushed;
|
||||
return before;
|
||||
});
|
||||
const redis = server.clients.redis as unknown as {
|
||||
meterRead: (...args: string[]) => Promise<unknown>;
|
||||
};
|
||||
const realRead = redis.meterRead.bind(redis);
|
||||
const read = vi
|
||||
.spyOn(redis, 'meterRead')
|
||||
.mockImplementation(async (...args: string[]) => {
|
||||
if (storeReadStarted) {
|
||||
await vi.waitFor(() => expect(flushed).toBeDefined());
|
||||
await flushed;
|
||||
}
|
||||
return realRead(...args);
|
||||
});
|
||||
try {
|
||||
const { res } = await target.readExact({ key });
|
||||
expect((res as { total: number }).total).toBe(10);
|
||||
} finally {
|
||||
get.mockRestore();
|
||||
read.mockRestore();
|
||||
write.spy.mockRestore();
|
||||
await flushing;
|
||||
}
|
||||
expect(await storedTotal(key)).toBe(10);
|
||||
});
|
||||
|
||||
it('counts nothing twice in an exact read when a flush lands during its store read', async () => {
|
||||
await target.incr({ key, pathAndAmountMap: { total: 10 } });
|
||||
const write = holdCalls(kv, 'incr');
|
||||
const flushing = target.flushCycle();
|
||||
await vi.waitFor(() => expect(write.spy).toHaveBeenCalled());
|
||||
|
||||
// The claim was read with the amount still on it; the store read
|
||||
// then sees it written.
|
||||
let landed = false;
|
||||
const realGet = kv.get.bind(kv);
|
||||
const get = vi.spyOn(kv, 'get').mockImplementation(async (args) => {
|
||||
if (args.consistentRead && args.key === key && !landed) {
|
||||
landed = true;
|
||||
write.release();
|
||||
await flushing;
|
||||
}
|
||||
return realGet(args);
|
||||
});
|
||||
try {
|
||||
const { res } = await target.readExact({ key });
|
||||
expect((res as { total: number }).total).toBe(10);
|
||||
} finally {
|
||||
get.mockRestore();
|
||||
write.spy.mockRestore();
|
||||
await flushing;
|
||||
}
|
||||
});
|
||||
|
||||
it('counts usage behind another flush’s claim in an exact read', async () => {
|
||||
await target.incr({ key, pathAndAmountMap: { total: 10 } });
|
||||
const write = holdCalls(kv, 'incr');
|
||||
try {
|
||||
const flushing = target.flushCycle();
|
||||
await vi.waitFor(() => expect(write.spy).toHaveBeenCalled());
|
||||
// Arrives while the claim is out, so it can't be flushed yet.
|
||||
await target.incr({ key, pathAndAmountMap: { total: 5 } });
|
||||
const { res } = await target.readExact({ key });
|
||||
expect((res as { total: number }).total).toBe(15);
|
||||
write.release();
|
||||
await flushing;
|
||||
} finally {
|
||||
write.spy.mockRestore();
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Lets a held flush finish during the first consistent store read of
|
||||
* `key`, which then answers with what the store holds afterwards.
|
||||
*/
|
||||
const landDuringStoreRead = (
|
||||
release: () => void,
|
||||
flushing: Promise<unknown>,
|
||||
) => {
|
||||
let landed = false;
|
||||
const realGet = kv.get.bind(kv);
|
||||
return vi.spyOn(kv, 'get').mockImplementation(async (args) => {
|
||||
const ours = Array.isArray(args.key)
|
||||
? args.key.includes(key)
|
||||
: args.key === key;
|
||||
if (args.consistentRead && ours && !landed) {
|
||||
landed = true;
|
||||
release();
|
||||
await flushing;
|
||||
}
|
||||
return realGet(args);
|
||||
});
|
||||
};
|
||||
|
||||
for (const shape of ['single', 'array'] as const) {
|
||||
it(`counts nothing twice in ${shape === 'single' ? 'a single' : 'an array'} read without a base when a flush lands during it`, async () => {
|
||||
await target.incr({ key, pathAndAmountMap: { total: 10 } });
|
||||
// No cached base: the counter is new to the store.
|
||||
await target.forgetBase(key);
|
||||
const write = holdCalls(kv, 'incr');
|
||||
const flushing = target.flushCycle();
|
||||
await vi.waitFor(() => expect(write.spy).toHaveBeenCalled());
|
||||
const get = landDuringStoreRead(write.release, flushing);
|
||||
try {
|
||||
const { res } = await target.get({
|
||||
key: shape === 'single' ? key : [key],
|
||||
});
|
||||
const value = (
|
||||
shape === 'single' ? res : (res as unknown[])[0]
|
||||
) as { total: number };
|
||||
expect(value.total).toBe(10);
|
||||
} finally {
|
||||
get.mockRestore();
|
||||
write.spy.mockRestore();
|
||||
await flushing;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
it('reads a written-through increment once the cache is back', async () => {
|
||||
await target.incr({ key, pathAndAmountMap: { total: 10 } });
|
||||
await target.flushCycle();
|
||||
const redis = server.clients.redis as unknown as {
|
||||
meterIncr: (...args: string[]) => Promise<unknown>;
|
||||
};
|
||||
const failing = vi
|
||||
.spyOn(redis, 'meterIncr')
|
||||
.mockRejectedValueOnce(new Error('cache down'));
|
||||
const { res, exact } = await target.incr({
|
||||
key,
|
||||
pathAndAmountMap: { total: 5 },
|
||||
});
|
||||
failing.mockRestore();
|
||||
expect(exact).toBe(true);
|
||||
expect((res as { total: number }).total).toBe(15);
|
||||
expect(await readTotal()).toBe(15);
|
||||
});
|
||||
|
||||
it('reads the store consistently when no base is cached', async () => {
|
||||
await kv.incr({ key, pathAndAmountMap: { total: 3 } });
|
||||
const get = vi.spyOn(kv, 'get');
|
||||
try {
|
||||
expect(await readTotal()).toBe(3);
|
||||
expect(get).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ key, consistentRead: true }),
|
||||
);
|
||||
} finally {
|
||||
get.mockRestore();
|
||||
}
|
||||
});
|
||||
|
||||
for (const shape of ['single', 'array', 'exact'] as const) {
|
||||
it(`does not retry a ${shape} store read when only new increments arrive`, async () => {
|
||||
await kv.incr({ key, pathAndAmountMap: { total: 100 } });
|
||||
await target.incrAux({ key, pathAndAmountMap: { total: 10 } });
|
||||
const dynamo = server.clients.dynamo;
|
||||
const realGet = dynamo.get.bind(dynamo);
|
||||
const realBatchGet = dynamo.batchGet.bind(dynamo);
|
||||
let storeReads = 0;
|
||||
const increment = async () => {
|
||||
storeReads++;
|
||||
await target.incrAux({
|
||||
key,
|
||||
pathAndAmountMap: { total: 5 },
|
||||
});
|
||||
};
|
||||
const get = vi
|
||||
.spyOn(dynamo, 'get')
|
||||
.mockImplementation(async (...args) => {
|
||||
const result = await realGet(...args);
|
||||
if (args[1].key === key) await increment();
|
||||
return result;
|
||||
});
|
||||
const batchGet = vi
|
||||
.spyOn(dynamo, 'batchGet')
|
||||
.mockImplementation(async (...args) => {
|
||||
const result = await realBatchGet(...args);
|
||||
if (args[0].some(({ items }) => items.key === key))
|
||||
await increment();
|
||||
return result;
|
||||
});
|
||||
const redis = server.clients.redis as unknown as {
|
||||
meterRead: (...args: string[]) => Promise<unknown>;
|
||||
};
|
||||
const read = vi.spyOn(redis, 'meterRead');
|
||||
try {
|
||||
const { res } =
|
||||
shape === 'exact'
|
||||
? await target.readExact({ key })
|
||||
: await target.get({
|
||||
key: shape === 'single' ? key : [key],
|
||||
});
|
||||
const value = (
|
||||
shape === 'array' ? (res as unknown[])[0] : res
|
||||
) as { total: number };
|
||||
expect(value.total).toBe(110);
|
||||
expect(storeReads).toBe(1);
|
||||
expect(read).toHaveBeenCalledTimes(2);
|
||||
} finally {
|
||||
get.mockRestore();
|
||||
batchGet.mockRestore();
|
||||
read.mockRestore();
|
||||
}
|
||||
expect(await readTotal()).toBe(115);
|
||||
});
|
||||
|
||||
it(`retries a ${shape} read when new increments hide a completed flush`, async () => {
|
||||
await kv.incr({ key, pathAndAmountMap: { total: 0 } });
|
||||
await target.incrAux({ key, pathAndAmountMap: { total: 10 } });
|
||||
const dynamo = server.clients.dynamo;
|
||||
const write = holdCalls(dynamo, 'update');
|
||||
const flushing = target.flushCycle();
|
||||
await vi.waitFor(() => expect(write.spy).toHaveBeenCalled());
|
||||
const realGet = dynamo.get.bind(dynamo);
|
||||
const realBatchGet = dynamo.batchGet.bind(dynamo);
|
||||
let storeReads = 0;
|
||||
const land = async () => {
|
||||
if (++storeReads !== 1) return;
|
||||
write.release();
|
||||
await flushing;
|
||||
await target.incrAux({
|
||||
key,
|
||||
pathAndAmountMap: { total: 20 },
|
||||
});
|
||||
};
|
||||
const get = vi
|
||||
.spyOn(dynamo, 'get')
|
||||
.mockImplementation(async (...args) => {
|
||||
if (args[1].key === key) await land();
|
||||
return realGet(...args);
|
||||
});
|
||||
const batchGet = vi
|
||||
.spyOn(dynamo, 'batchGet')
|
||||
.mockImplementation(async (...args) => {
|
||||
if (args[0].some(({ items }) => items.key === key))
|
||||
await land();
|
||||
return realBatchGet(...args);
|
||||
});
|
||||
try {
|
||||
const { res } =
|
||||
shape === 'exact'
|
||||
? await target.readExact({ key })
|
||||
: await target.get({
|
||||
key: shape === 'single' ? key : [key],
|
||||
});
|
||||
const value = (
|
||||
shape === 'array' ? (res as unknown[])[0] : res
|
||||
) as { total: number };
|
||||
expect(value.total).toBe(30);
|
||||
expect(storeReads).toBe(2);
|
||||
} finally {
|
||||
get.mockRestore();
|
||||
batchGet.mockRestore();
|
||||
write.release();
|
||||
await flushing;
|
||||
write.spy.mockRestore();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
it('keeps changed counters together in a batch retry', async () => {
|
||||
const keys = [`${key}:a`, `${key}:b`];
|
||||
await Promise.all(
|
||||
keys.map((k) =>
|
||||
kv.incr({ key: k, pathAndAmountMap: { total: 0 } }),
|
||||
),
|
||||
);
|
||||
await Promise.all(
|
||||
keys.map((k) =>
|
||||
target.incrAux({ key: k, pathAndAmountMap: { total: 10 } }),
|
||||
),
|
||||
);
|
||||
const dynamo = server.clients.dynamo;
|
||||
const realBatchGet = dynamo.batchGet.bind(dynamo);
|
||||
const write = holdCalls(dynamo, 'update');
|
||||
const flushing = target.flushCycle();
|
||||
await vi.waitFor(() => expect(write.spy).toHaveBeenCalledTimes(2));
|
||||
const batches: string[][] = [];
|
||||
const batchGet = vi
|
||||
.spyOn(dynamo, 'batchGet')
|
||||
.mockImplementation(async (...args) => {
|
||||
batches.push(
|
||||
args[0].map(({ items }) => items.key as string),
|
||||
);
|
||||
if (batches.length === 1) {
|
||||
write.release();
|
||||
await flushing;
|
||||
}
|
||||
return realBatchGet(...args);
|
||||
});
|
||||
const get = vi.spyOn(dynamo, 'get');
|
||||
try {
|
||||
expect((await target.get({ key: keys })).res).toEqual([
|
||||
{ total: 10 },
|
||||
{ total: 10 },
|
||||
]);
|
||||
expect(batches).toHaveLength(2);
|
||||
expect(batches.map((batch) => batch.sort())).toEqual([
|
||||
keys,
|
||||
keys,
|
||||
]);
|
||||
expect(get).not.toHaveBeenCalled();
|
||||
} finally {
|
||||
batchGet.mockRestore();
|
||||
get.mockRestore();
|
||||
write.release();
|
||||
await flushing;
|
||||
write.spy.mockRestore();
|
||||
}
|
||||
});
|
||||
|
||||
it('detects a whole flush between snapshots with no outstanding claim', async () => {
|
||||
await kv.incr({ key, pathAndAmountMap: { total: 0 } });
|
||||
await target.incrAux({ key, pathAndAmountMap: { total: 10 } });
|
||||
const dynamo = server.clients.dynamo;
|
||||
const realGet = dynamo.get.bind(dynamo);
|
||||
let reads = 0;
|
||||
const get = vi
|
||||
.spyOn(dynamo, 'get')
|
||||
.mockImplementation(async (...args) => {
|
||||
if (args[1].key === key && ++reads === 1) {
|
||||
await target.flushCycle();
|
||||
await target.incrAux({
|
||||
key,
|
||||
pathAndAmountMap: { total: 20 },
|
||||
});
|
||||
}
|
||||
return realGet(...args);
|
||||
});
|
||||
try {
|
||||
expect(await readTotal()).toBe(30);
|
||||
expect(reads).toBe(2);
|
||||
} finally {
|
||||
get.mockRestore();
|
||||
}
|
||||
});
|
||||
|
||||
it('retries a decreasing buffer even when the flush metadata is unchanged', async () => {
|
||||
await kv.incr({ key, pathAndAmountMap: { total: 100 } });
|
||||
await target.incrAux({ key, pathAndAmountMap: { total: 10 } });
|
||||
const dynamo = server.clients.dynamo;
|
||||
const realGet = dynamo.get.bind(dynamo);
|
||||
let reads = 0;
|
||||
const get = vi
|
||||
.spyOn(dynamo, 'get')
|
||||
.mockImplementation(async (...args) => {
|
||||
const result = await realGet(...args);
|
||||
if (args[1].key === key && ++reads === 1) {
|
||||
await target.incrAux({
|
||||
key,
|
||||
pathAndAmountMap: { total: -5 },
|
||||
});
|
||||
}
|
||||
return result;
|
||||
});
|
||||
try {
|
||||
expect(await readTotal()).toBe(105);
|
||||
expect(reads).toBe(2);
|
||||
} finally {
|
||||
get.mockRestore();
|
||||
}
|
||||
});
|
||||
|
||||
it('detects applied paths while the claim id and fence token stay unchanged', async () => {
|
||||
await kv.incr({ key, pathAndAmountMap: { total: 0 } });
|
||||
await target.incrAux({ key, pathAndAmountMap: { total: 10 } });
|
||||
const dynamo = server.clients.dynamo;
|
||||
const redis = server.clients.redis as unknown as {
|
||||
meterSettle: (...args: string[]) => Promise<number>;
|
||||
};
|
||||
const write = holdCalls(dynamo, 'update');
|
||||
const settle = holdCalls(redis, 'meterSettle');
|
||||
const flushing = target.flushCycle();
|
||||
await vi.waitFor(() => expect(write.spy).toHaveBeenCalled());
|
||||
const realGet = dynamo.get.bind(dynamo);
|
||||
let reads = 0;
|
||||
const get = vi
|
||||
.spyOn(dynamo, 'get')
|
||||
.mockImplementation(async (...args) => {
|
||||
if (args[1].key === key && ++reads === 1) {
|
||||
write.release();
|
||||
await vi.waitFor(() =>
|
||||
expect(settle.spy).toHaveBeenCalled(),
|
||||
);
|
||||
await target.incrAux({
|
||||
key,
|
||||
pathAndAmountMap: { total: 20 },
|
||||
});
|
||||
}
|
||||
return realGet(...args);
|
||||
});
|
||||
try {
|
||||
expect(await readTotal()).toBe(30);
|
||||
expect(reads).toBe(2);
|
||||
} finally {
|
||||
get.mockRestore();
|
||||
write.release();
|
||||
settle.release();
|
||||
await flushing;
|
||||
write.spy.mockRestore();
|
||||
settle.spy.mockRestore();
|
||||
}
|
||||
});
|
||||
|
||||
it('does not retry unchanged amounts when another counter advances the bucket token', async () => {
|
||||
await kv.incr({ key, pathAndAmountMap: { total: 100 } });
|
||||
let other = `${key}:other`;
|
||||
for (let i = 0; bucketTag(other) !== bucketTag(key); i++)
|
||||
other = `${key}:${i}`;
|
||||
await kv.incr({ key: other, pathAndAmountMap: { total: 0 } });
|
||||
await target.incrAux({
|
||||
key: other,
|
||||
pathAndAmountMap: { total: 10 },
|
||||
});
|
||||
const dynamo = server.clients.dynamo;
|
||||
const realGet = dynamo.get.bind(dynamo);
|
||||
let reads = 0;
|
||||
const get = vi
|
||||
.spyOn(dynamo, 'get')
|
||||
.mockImplementation(async (...args) => {
|
||||
if (args[1].key === key && ++reads === 1)
|
||||
await target.flushCycle();
|
||||
return realGet(...args);
|
||||
});
|
||||
try {
|
||||
expect(await readTotal()).toBe(100);
|
||||
expect(reads).toBe(1);
|
||||
} finally {
|
||||
get.mockRestore();
|
||||
}
|
||||
});
|
||||
|
||||
it('recovers a failed initial cache snapshot before retrying the batch', async () => {
|
||||
await kv.incr({ key, pathAndAmountMap: { total: 100 } });
|
||||
await target.incrAux({ key, pathAndAmountMap: { total: 10 } });
|
||||
const redis = server.clients.redis as unknown as {
|
||||
meterRead: (...args: string[]) => Promise<unknown>;
|
||||
};
|
||||
const read = vi
|
||||
.spyOn(redis, 'meterRead')
|
||||
.mockRejectedValueOnce(new Error('cache unavailable'));
|
||||
const batchGet = vi.spyOn(server.clients.dynamo, 'batchGet');
|
||||
try {
|
||||
expect((await target.get({ key: [key] })).res).toEqual([
|
||||
{ total: 110 },
|
||||
]);
|
||||
expect(batchGet).toHaveBeenCalledTimes(2);
|
||||
} finally {
|
||||
read.mockRestore();
|
||||
batchGet.mockRestore();
|
||||
}
|
||||
});
|
||||
|
||||
it('bounds concurrent cache reads while preserving array order', async () => {
|
||||
const keys = Array.from({ length: 25 }, (_, i) => `${key}:${i}`);
|
||||
await Promise.all(
|
||||
keys.map((k, i) =>
|
||||
target.incrAux({
|
||||
key: k,
|
||||
pathAndAmountMap: { total: i + 1 },
|
||||
}),
|
||||
),
|
||||
);
|
||||
const redis = server.clients.redis as unknown as {
|
||||
meterRead: (...args: string[]) => Promise<unknown>;
|
||||
};
|
||||
const realRead = redis.meterRead.bind(redis);
|
||||
let active = 0;
|
||||
let peak = 0;
|
||||
const read = vi
|
||||
.spyOn(redis, 'meterRead')
|
||||
.mockImplementation(async (...args) => {
|
||||
active++;
|
||||
peak = Math.max(peak, active);
|
||||
try {
|
||||
await new Promise((resolve) => setTimeout(resolve, 1));
|
||||
return await realRead(...args);
|
||||
} finally {
|
||||
active--;
|
||||
}
|
||||
});
|
||||
try {
|
||||
expect((await target.get({ key: keys })).res).toEqual(
|
||||
keys.map((_, i) => ({ total: i + 1 })),
|
||||
);
|
||||
expect(peak).toBe(20);
|
||||
expect(active).toBe(0);
|
||||
} finally {
|
||||
read.mockRestore();
|
||||
}
|
||||
});
|
||||
|
||||
it('reads an unknown counter as absent', async () => {
|
||||
const { res } = await target.get({ key });
|
||||
expect(res).toBeNull();
|
||||
@@ -906,12 +1525,16 @@ describe('MeteringBufferStore', () => {
|
||||
'1000000000',
|
||||
);
|
||||
|
||||
// Another deployment's write, which only a replacing settle would
|
||||
// bring into the base.
|
||||
await kv.incr({ key, pathAndAmountMap: { total: 1000 } });
|
||||
await target.incr({ key, pathAndAmountMap: { total: 5 } });
|
||||
await target.flushCycle();
|
||||
|
||||
expect(await storedTotal(key)).toBe(105);
|
||||
expect(await storedTotal(key)).toBe(1105);
|
||||
// The newer base keeps its view, plus what this flush wrote.
|
||||
expect(Number(await server.clients.redis.hget(base, 'total'))).toBe(
|
||||
100,
|
||||
105,
|
||||
);
|
||||
});
|
||||
|
||||
@@ -1438,6 +2061,30 @@ describe('MeteringBufferStore', () => {
|
||||
).toEqual({});
|
||||
});
|
||||
|
||||
it('reads a re-driven write that already landed, with a warm base', async () => {
|
||||
await target.incr({ key, pathAndAmountMap: { total: 100 } });
|
||||
await target.flushCycle();
|
||||
await target.incr({ key, pathAndAmountMap: { total: 5 } });
|
||||
|
||||
const passThrough = kv.incr.bind(kv);
|
||||
const lost = vi
|
||||
.spyOn(kv, 'incr')
|
||||
.mockImplementationOnce(
|
||||
async (...args: Parameters<typeof kv.incr>) => {
|
||||
await passThrough(...args);
|
||||
throw new Error('request timed out');
|
||||
},
|
||||
);
|
||||
await target.flushCycle();
|
||||
lost.mockRestore();
|
||||
await ageClaim();
|
||||
await target.flushCycle();
|
||||
|
||||
expect(await storedTotal(key)).toBe(105);
|
||||
const { res } = await target.get({ key });
|
||||
expect((res as { total: number }).total).toBe(105);
|
||||
});
|
||||
|
||||
it('applies a re-driven write whose first copy never landed', async () => {
|
||||
await target.incr({ key, pathAndAmountMap: { total: 5 } });
|
||||
const boom = vi
|
||||
@@ -1742,6 +2389,17 @@ describe('MeteringBufferStore', () => {
|
||||
order.push('dropFromClaim');
|
||||
return realHdel(...args);
|
||||
});
|
||||
// A written batch leaves the claim through this script.
|
||||
const redis = server.clients.redis as unknown as {
|
||||
meterApply: (...args: string[]) => Promise<number>;
|
||||
};
|
||||
const realApply = redis.meterApply.bind(redis);
|
||||
const applySpy = vi
|
||||
.spyOn(redis, 'meterApply')
|
||||
.mockImplementation((...args: string[]) => {
|
||||
order.push('dropFromClaim');
|
||||
return realApply(...args);
|
||||
});
|
||||
const alarmSpy = vi
|
||||
.spyOn(server.clients.alarm, 'create')
|
||||
.mockImplementation(() => {
|
||||
@@ -1751,6 +2409,7 @@ describe('MeteringBufferStore', () => {
|
||||
await target.flushCycle();
|
||||
incrSpy.mockRestore();
|
||||
hdelSpy.mockRestore();
|
||||
applySpy.mockRestore();
|
||||
alarmSpy.mockRestore();
|
||||
|
||||
expect(order).toEqual(['dropFromClaim', 'alarm']);
|
||||
@@ -1783,7 +2442,13 @@ describe('MeteringBufferStore', () => {
|
||||
// right up until the TTL takes the amounts with it.
|
||||
const tag = bucketTag(key);
|
||||
await server.clients.redis.del(`meter:dirty:{${tag}}`);
|
||||
// Every few cycles a flush reconciles on its own; keep this one
|
||||
// from doing so, whatever the earlier tests left the count at.
|
||||
const scheduled = vi
|
||||
.spyOn(target, 'reconcile')
|
||||
.mockResolvedValue(undefined);
|
||||
expect(await target.flushCycle()).toBe(0);
|
||||
scheduled.mockRestore();
|
||||
expect(await storedTotal(key)).toBe(0);
|
||||
|
||||
const logged = vi
|
||||
|
||||
@@ -83,6 +83,19 @@ interface IncrInput {
|
||||
|
||||
type FlatAmounts = Record<string, number>;
|
||||
|
||||
interface BufferedRead {
|
||||
delta: FlatAmounts;
|
||||
base: FlatAmounts;
|
||||
claim: FlatAmounts;
|
||||
claimId: string;
|
||||
writeToken: string;
|
||||
}
|
||||
|
||||
interface StoreAndBufferRead {
|
||||
stored: unknown;
|
||||
buffered: FlatAmounts;
|
||||
}
|
||||
|
||||
/** A claim's write that was recorded before it was sent; see `INTENT_SCRIPT`. */
|
||||
interface SettleIntent {
|
||||
writer: string;
|
||||
@@ -126,8 +139,8 @@ const ORPHAN_AGE_MS = 30_000;
|
||||
/** Long enough that a counter for the current month can never expire. */
|
||||
const BUFFER_TTL_MS = 40 * 24 * 60 * 60 * 1000;
|
||||
|
||||
/** How many counters are written onward at once, to keep the load even. */
|
||||
const SETTLE_CONCURRENCY = 20;
|
||||
/** Cache reads per call or counter writes per cycle in flight at once. */
|
||||
const BUFFER_IO_CONCURRENCY = 20;
|
||||
|
||||
/** Roughly a minute between compression reports, so they don't flood the log. */
|
||||
const CYCLES_PER_COMPRESSION_REPORT = 12;
|
||||
@@ -152,6 +165,12 @@ const UNWRITABLE_PATH_MEMO_LIMIT = 1000;
|
||||
*/
|
||||
const EXACT_READ_MEMO_LIMIT = 10_000;
|
||||
|
||||
/**
|
||||
* Store reads a consistent read makes while the buffer keeps shrinking under
|
||||
* it; see `#readStoreAndBuffer`.
|
||||
*/
|
||||
const EXACT_READ_ATTEMPTS = 3;
|
||||
|
||||
/** Write units past which settling a counter raises the large-item alarm. */
|
||||
const LARGE_SETTLE_WRITE_UNITS = 100;
|
||||
|
||||
@@ -339,6 +358,15 @@ const toScriptArgs = (amounts: Record<string, number>): string[] => {
|
||||
return args;
|
||||
};
|
||||
|
||||
/** Whether two sets of amounts hold the same paths and values. */
|
||||
const sameAmounts = (a: FlatAmounts, b: FlatAmounts): boolean => {
|
||||
const paths = Object.keys(a);
|
||||
return (
|
||||
paths.length === Object.keys(b).length &&
|
||||
paths.every((path) => a[path] === b[path])
|
||||
);
|
||||
};
|
||||
|
||||
/** The subset of `amounts` at `paths`. */
|
||||
const pickAmounts = (amounts: FlatAmounts, paths: string[]): FlatAmounts =>
|
||||
Object.fromEntries(paths.map((path) => [path, amounts[path]!]));
|
||||
@@ -380,11 +408,12 @@ export const isBilledCounter = (key: string): boolean =>
|
||||
// -- Scripts ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* KEYS: delta, base, dirty set, tracked set. ARGV: ttl, member, then
|
||||
* path/amount pairs.
|
||||
* KEYS: delta, base, dirty set, tracked set, claimed. ARGV: ttl, member,
|
||||
* pending key prefix, then path/amount pairs.
|
||||
*
|
||||
* Returns the delta and the base together so a running total can be served
|
||||
* without a second round trip. Amounts are floats — costs are fractional.
|
||||
* Returns the delta, the base and any outstanding claim together, so a running
|
||||
* total can be served without a second round trip (see `READ_SCRIPT` for the
|
||||
* claim). Amounts are floats — costs are fractional.
|
||||
*
|
||||
* The counter joins both sets here. The dirty set is the working list a flush
|
||||
* takes from and hands back; the tracked set is only ever added to here and
|
||||
@@ -392,7 +421,7 @@ export const isBilledCounter = (key: string): boolean =>
|
||||
* working list does not.
|
||||
*/
|
||||
const INCR_SCRIPT = `
|
||||
for i = 3, #ARGV, 2 do
|
||||
for i = 4, #ARGV, 2 do
|
||||
redis.call('HINCRBYFLOAT', KEYS[1], ARGV[i], ARGV[i + 1])
|
||||
end
|
||||
redis.call('PEXPIRE', KEYS[1], ARGV[1])
|
||||
@@ -400,12 +429,48 @@ redis.call('SADD', KEYS[3], ARGV[2])
|
||||
redis.call('PEXPIRE', KEYS[3], ARGV[1])
|
||||
redis.call('SADD', KEYS[4], ARGV[2])
|
||||
redis.call('PEXPIRE', KEYS[4], ARGV[1])
|
||||
return { redis.call('HGETALL', KEYS[1]), redis.call('HGETALL', KEYS[2]) }
|
||||
local claim = {}
|
||||
local nonce = redis.call('HGET', KEYS[5], ARGV[2])
|
||||
if nonce then claim = redis.call('HGETALL', ARGV[3] .. nonce) end
|
||||
return { redis.call('HGETALL', KEYS[1]), redis.call('HGETALL', KEYS[2]), claim }
|
||||
`;
|
||||
|
||||
/** KEYS: delta, base. Reads both without marking the counter for flushing. */
|
||||
/**
|
||||
* KEYS: delta, base, claimed, fence token. ARGV: counter, pending key prefix.
|
||||
*
|
||||
* Reads both without marking the counter for flushing, plus the counter's
|
||||
* outstanding claim: amounts a flush has taken but not yet written onward are
|
||||
* in neither the delta nor the base. The claim's key is built here, which is
|
||||
* safe for the reason given at `RECONCILE_SCRIPT`.
|
||||
*/
|
||||
const READ_SCRIPT = `
|
||||
return { redis.call('HGETALL', KEYS[1]), redis.call('HGETALL', KEYS[2]) }
|
||||
local claim = {}
|
||||
local nonce = redis.call('HGET', KEYS[3], ARGV[1])
|
||||
if nonce then claim = redis.call('HGETALL', ARGV[2] .. nonce) end
|
||||
return {
|
||||
redis.call('HGETALL', KEYS[1]), redis.call('HGETALL', KEYS[2]), claim,
|
||||
nonce or '', redis.call('GET', KEYS[4]) or ''
|
||||
}
|
||||
`;
|
||||
|
||||
/**
|
||||
* KEYS: pending, base. ARGV: path/amount pairs just written onward.
|
||||
*
|
||||
* Takes written paths off a claim and, when a base is cached, adds them to it
|
||||
* in the same step, so a read counts them all the way to the settle that
|
||||
* replaces the base with the store's own view. Without a base, reads go to the
|
||||
* store, which already has them.
|
||||
*/
|
||||
const APPLY_SCRIPT = `
|
||||
redis.call('HDEL', KEYS[1], '${INTENT_FIELD}')
|
||||
local based = redis.call('EXISTS', KEYS[2]) == 1
|
||||
for i = 1, #ARGV, 2 do
|
||||
redis.call('HDEL', KEYS[1], ARGV[i])
|
||||
if based then
|
||||
redis.call('HINCRBYFLOAT', KEYS[2], ARGV[i], ARGV[i + 1])
|
||||
end
|
||||
end
|
||||
return 1
|
||||
`;
|
||||
|
||||
/**
|
||||
@@ -651,9 +716,29 @@ end
|
||||
return redis.call('HGETALL', KEYS[1])
|
||||
`;
|
||||
|
||||
/**
|
||||
* KEYS: pending, base, base sequence. ARGV: paths found already applied.
|
||||
*
|
||||
* Takes paths an earlier copy of the write already applied off a claim, and
|
||||
* drops the cached base in the same step: the base may or may not have them,
|
||||
* and the store does, so the next read seeds a fresh one from the store.
|
||||
*/
|
||||
const FENCED_SCRIPT = `
|
||||
redis.call('HDEL', KEYS[1], '${INTENT_FIELD}')
|
||||
for i = 1, #ARGV do
|
||||
redis.call('HDEL', KEYS[1], ARGV[i])
|
||||
end
|
||||
redis.call('DEL', KEYS[2], KEYS[3])
|
||||
return 1
|
||||
`;
|
||||
|
||||
type ScriptRunner = {
|
||||
meterIncr(...args: string[]): Promise<[string[], string[]]>;
|
||||
meterRead(...args: string[]): Promise<[string[], string[]]>;
|
||||
meterIncr(...args: string[]): Promise<[string[], string[], string[]]>;
|
||||
meterRead(
|
||||
...args: string[]
|
||||
): Promise<[string[], string[], string[], string, string]>;
|
||||
meterApply(...args: string[]): Promise<number>;
|
||||
meterFenced(...args: string[]): Promise<number>;
|
||||
meterDrain(...args: string[]): Promise<[string[], number]>;
|
||||
meterClaim(...args: string[]): Promise<string[] | null>;
|
||||
/** `[1, amounts]` when re-claimed, `[0, hadIndexEntry]` when not. */
|
||||
@@ -770,6 +855,8 @@ export class MeteringBufferStore extends PuterStore {
|
||||
`[metering] buffered incr failed, writing through: ${(e as Error).message}`,
|
||||
);
|
||||
const { res } = await this.#writeThrough(input);
|
||||
// The cached base doesn't have this write; the next read reseeds it.
|
||||
await this.forgetBase(input.key);
|
||||
return { res, exact: true };
|
||||
}
|
||||
}
|
||||
@@ -817,43 +904,40 @@ export class MeteringBufferStore extends PuterStore {
|
||||
|
||||
const keys = key;
|
||||
const results: (unknown | null)[] = new Array(keys.length).fill(null);
|
||||
const deltas: FlatAmounts[] = new Array(keys.length).fill(
|
||||
{} as FlatAmounts,
|
||||
const snapshots: (BufferedRead | null)[] = new Array(keys.length).fill(
|
||||
null,
|
||||
);
|
||||
const misses: number[] = [];
|
||||
|
||||
await Promise.all(
|
||||
keys.map(async (k, i) => {
|
||||
try {
|
||||
const { delta, base } = await this.#readBuffered(k);
|
||||
if (Object.keys(base).length > 0) {
|
||||
results[i] = mergedOrNull(base, delta);
|
||||
return;
|
||||
}
|
||||
// No cached base yet — the store is the only place left to
|
||||
// ask, and it's cheaper to ask once for every key like it.
|
||||
deltas[i] = delta;
|
||||
misses.push(i);
|
||||
} catch (e) {
|
||||
console.warn(
|
||||
`[metering] buffered read failed, reading through: ${(e as Error).message}`,
|
||||
);
|
||||
misses.push(i);
|
||||
}
|
||||
}),
|
||||
const reads = await runWithConcurrencyLimitSettled(
|
||||
keys,
|
||||
BUFFER_IO_CONCURRENCY,
|
||||
(k) => this.#readBuffered(k),
|
||||
);
|
||||
reads.forEach((read, i) => {
|
||||
if (read.status === 'fulfilled') {
|
||||
const { delta, base } = read.value;
|
||||
if (Object.keys(base).length > 0) {
|
||||
results[i] = mergedOrNull(base, delta);
|
||||
return;
|
||||
}
|
||||
snapshots[i] = read.value;
|
||||
} else {
|
||||
console.warn(
|
||||
`[metering] buffered read failed, reading through: ${(read.reason as Error).message}`,
|
||||
);
|
||||
}
|
||||
misses.push(i);
|
||||
});
|
||||
|
||||
if (misses.length > 0) {
|
||||
// Unlike the per-key fallbacks above, a failure here is not
|
||||
// absorbed: a caller deciding on a balance must know its answer
|
||||
// is incomplete rather than silently read a buffered-only view.
|
||||
const { res: fetched } = await this.stores.kv.get({
|
||||
key: misses.map((i) => keys[i]!),
|
||||
});
|
||||
const fetchedValues = fetched as (unknown | null)[];
|
||||
const fetched = await this.#readStoreAndBuffer(
|
||||
misses.map((i) => keys[i]!),
|
||||
misses.map((i) => snapshots[i]!),
|
||||
);
|
||||
misses.forEach((i, idx) => {
|
||||
const persisted = flattenAmounts(fetchedValues[idx]);
|
||||
results[i] = mergedOrNull(persisted, deltas[i]!);
|
||||
const { stored, buffered } = fetched[idx]!;
|
||||
results[i] = mergedOrNull(flattenAmounts(stored), buffered);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -897,7 +981,11 @@ export class MeteringBufferStore extends PuterStore {
|
||||
`[metering] exact read could not flush ${key}: ${(e as Error).message}`,
|
||||
);
|
||||
}
|
||||
return this.stores.kv.get({ key, consistentRead: true });
|
||||
// Whatever is still buffered, a claim another deployment holds and
|
||||
// usage that arrived behind it included, isn't in the store yet.
|
||||
const [{ stored, buffered }] = await this.#readStoreAndBuffer(key);
|
||||
if (Object.keys(buffered).length === 0) return { res: stored };
|
||||
return { res: mergedOrNull(flattenAmounts(stored), buffered) };
|
||||
}
|
||||
|
||||
#rememberExactRead(key: string, now: number): void {
|
||||
@@ -1115,13 +1203,21 @@ export class MeteringBufferStore extends PuterStore {
|
||||
this.#definedScripts = true;
|
||||
const client = this.clients.redis;
|
||||
client.defineCommand('meterIncr', {
|
||||
numberOfKeys: 4,
|
||||
numberOfKeys: 5,
|
||||
lua: INCR_SCRIPT,
|
||||
});
|
||||
client.defineCommand('meterRead', {
|
||||
numberOfKeys: 2,
|
||||
numberOfKeys: 4,
|
||||
lua: READ_SCRIPT,
|
||||
});
|
||||
client.defineCommand('meterApply', {
|
||||
numberOfKeys: 2,
|
||||
lua: APPLY_SCRIPT,
|
||||
});
|
||||
client.defineCommand('meterFenced', {
|
||||
numberOfKeys: 3,
|
||||
lua: FENCED_SCRIPT,
|
||||
});
|
||||
client.defineCommand('meterDrain', {
|
||||
numberOfKeys: 2,
|
||||
lua: DRAIN_SCRIPT,
|
||||
@@ -1172,18 +1268,23 @@ export class MeteringBufferStore extends PuterStore {
|
||||
pathAndAmountMap,
|
||||
}: IncrInput): Promise<{ delta: FlatAmounts; base: FlatAmounts }> {
|
||||
const tag = bucketTag(key);
|
||||
const [delta, base] = await this.#redis.meterIncr(
|
||||
const [delta, base, claim] = await this.#redis.meterIncr(
|
||||
deltaKey(tag, key),
|
||||
baseKey(tag, key),
|
||||
dirtyKey(tag),
|
||||
trackedKey(tag),
|
||||
claimedKey(tag),
|
||||
String(BUFFER_TTL_MS),
|
||||
key,
|
||||
pendingKey(tag, ''),
|
||||
...toScriptArgs(pathAndAmountMap),
|
||||
);
|
||||
this.#absorbedCount++;
|
||||
absorbedCounter.add(1);
|
||||
return { delta: pairsToAmounts(delta), base: pairsToAmounts(base) };
|
||||
return {
|
||||
delta: addAmounts(pairsToAmounts(delta), parseClaim(claim).amounts),
|
||||
base: pairsToAmounts(base),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1196,7 +1297,9 @@ export class MeteringBufferStore extends PuterStore {
|
||||
* is over within seconds.
|
||||
*/
|
||||
async #seedBase(key: string): Promise<FlatAmounts> {
|
||||
const { res } = await this.stores.kv.get({ key });
|
||||
// Consistent, or a write that just landed could be missing from the
|
||||
// base until the next settle replaces it.
|
||||
const { res } = await this.stores.kv.get({ key, consistentRead: true });
|
||||
const persisted = flattenAmounts(res);
|
||||
const seeded = await this.#redis.meterSeed(
|
||||
baseKey(bucketTag(key), key),
|
||||
@@ -1212,25 +1315,107 @@ export class MeteringBufferStore extends PuterStore {
|
||||
* This deployment's cached delta and base for a counter, straight from
|
||||
* redis.
|
||||
*/
|
||||
async #readBuffered(
|
||||
key: string,
|
||||
): Promise<{ delta: FlatAmounts; base: FlatAmounts }> {
|
||||
async #readBuffered(key: string): Promise<BufferedRead> {
|
||||
const tag = bucketTag(key);
|
||||
const [delta, base] = await this.#redis.meterRead(
|
||||
deltaKey(tag, key),
|
||||
baseKey(tag, key),
|
||||
);
|
||||
return { delta: pairsToAmounts(delta), base: pairsToAmounts(base) };
|
||||
const [delta, base, claim, claimId, writeToken] =
|
||||
await this.#redis.meterRead(
|
||||
deltaKey(tag, key),
|
||||
baseKey(tag, key),
|
||||
claimedKey(tag),
|
||||
tokenKey(tag),
|
||||
key,
|
||||
pendingKey(tag, ''),
|
||||
);
|
||||
const claimed = parseClaim(claim).amounts;
|
||||
// Claimed amounts count like buffered ones until their settle.
|
||||
return {
|
||||
delta: addAmounts(pairsToAmounts(delta), claimed),
|
||||
base: pairsToAmounts(base),
|
||||
claim: claimed,
|
||||
claimId,
|
||||
writeToken,
|
||||
};
|
||||
}
|
||||
|
||||
async #readThrough(key: string): Promise<unknown | null> {
|
||||
const { delta, base } = await this.#readBuffered(key);
|
||||
const resolved =
|
||||
Object.keys(base).length > 0
|
||||
? base
|
||||
: flattenAmounts((await this.stores.kv.get({ key })).res);
|
||||
const snapshot = await this.#readBuffered(key);
|
||||
const { delta, base } = snapshot;
|
||||
if (Object.keys(base).length > 0) return mergedOrNull(base, delta);
|
||||
const [{ stored, buffered }] = await this.#readStoreAndBuffer(key, [
|
||||
snapshot,
|
||||
]);
|
||||
return mergedOrNull(flattenAmounts(stored), buffered);
|
||||
}
|
||||
|
||||
return mergedOrNull(resolved, delta);
|
||||
/**
|
||||
* Order buffer snapshots around consistent store reads to avoid losing
|
||||
* flushed usage. Retry changed claims together; new increments alone do not
|
||||
* move amounts to the store and do not invalidate the first snapshot.
|
||||
*/
|
||||
async #readStoreAndBuffer(
|
||||
key: string | string[],
|
||||
snapshots?: (BufferedRead | null)[],
|
||||
): Promise<StoreAndBufferRead[]> {
|
||||
const multi = Array.isArray(key);
|
||||
const keys = multi ? key : [key];
|
||||
const initial = snapshots ?? [await this.#readBuffered(keys[0]!)];
|
||||
let pending = keys.map((k, i) => ({
|
||||
key: k,
|
||||
index: i,
|
||||
before: initial[i] ?? null,
|
||||
}));
|
||||
const results: StoreAndBufferRead[] = new Array(keys.length);
|
||||
for (let attempt = 1; pending.length > 0; attempt++) {
|
||||
const { res } = await this.stores.kv.get({
|
||||
key: multi ? pending.map((item) => item.key) : pending[0]!.key,
|
||||
consistentRead: true,
|
||||
});
|
||||
const stored = multi ? (res as unknown[]) : [res];
|
||||
const checks =
|
||||
attempt < EXACT_READ_ATTEMPTS
|
||||
? await runWithConcurrencyLimitSettled(
|
||||
pending,
|
||||
BUFFER_IO_CONCURRENCY,
|
||||
(item) => this.#readBuffered(item.key),
|
||||
)
|
||||
: [];
|
||||
const retry: typeof pending = [];
|
||||
pending.forEach((item, i) => {
|
||||
const check = checks[i];
|
||||
if (check?.status === 'fulfilled') {
|
||||
const after = check.value;
|
||||
const before = item.before;
|
||||
// The token catches a whole claim starting and finishing
|
||||
// between snapshots, even when new increments hide it.
|
||||
const unchanged =
|
||||
before &&
|
||||
(sameAmounts(before.delta, after.delta) ||
|
||||
(before.writeToken === after.writeToken &&
|
||||
before.claimId === after.claimId &&
|
||||
sameAmounts(before.claim, after.claim) &&
|
||||
Object.entries(before.delta).every(
|
||||
([path, amount]) =>
|
||||
(after.delta[path] ?? 0) >= amount,
|
||||
) &&
|
||||
Object.entries(after.delta).every(
|
||||
([path, amount]) =>
|
||||
amount >= (before.delta[path] ?? 0),
|
||||
)));
|
||||
if (!unchanged) {
|
||||
retry.push({ ...item, before: after });
|
||||
return;
|
||||
}
|
||||
} else if (check?.status === 'rejected' && !multi) {
|
||||
throw check.reason;
|
||||
}
|
||||
results[item.index] = {
|
||||
stored: stored[i],
|
||||
buffered: item.before?.delta ?? {},
|
||||
};
|
||||
});
|
||||
pending = retry;
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
// -- Internals: flush ---------------------------------------------
|
||||
@@ -1302,7 +1487,7 @@ export class MeteringBufferStore extends PuterStore {
|
||||
|
||||
const outcomes = await runWithConcurrencyLimitSettled(
|
||||
work,
|
||||
SETTLE_CONCURRENCY,
|
||||
BUFFER_IO_CONCURRENCY,
|
||||
(run) => run(),
|
||||
);
|
||||
for (const outcome of outcomes) {
|
||||
@@ -1590,7 +1775,12 @@ export class MeteringBufferStore extends PuterStore {
|
||||
if (err instanceof KvFencedWriteError) {
|
||||
// Applied already, by an earlier copy of this write.
|
||||
fencedCounter.add(1);
|
||||
await this.#dropFromClaim(tag, nonce, paths);
|
||||
await this.#redis.meterFenced(
|
||||
pendingKey(tag, nonce),
|
||||
baseKey(tag, key),
|
||||
baseSeqKey(tag, key),
|
||||
...paths,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
if (!isPermanentSettleError(err)) throw e;
|
||||
@@ -1618,7 +1808,11 @@ export class MeteringBufferStore extends PuterStore {
|
||||
// before anything else can fail: a re-drive then picks up only what
|
||||
// is still outstanding. The alarm below is the "anything else" — it
|
||||
// comes after.
|
||||
await this.#dropFromClaim(tag, nonce, paths);
|
||||
await this.#redis.meterApply(
|
||||
pendingKey(tag, nonce),
|
||||
baseKey(tag, key),
|
||||
...toScriptArgs(batch),
|
||||
);
|
||||
|
||||
if (usage.write >= LARGE_SETTLE_WRITE_UNITS) {
|
||||
this.#recordLargeSettle(key, usage.write);
|
||||
|
||||
@@ -399,6 +399,23 @@ The code implementation is available in our [web search example](/playground/ai-
|
||||
|
||||
List of OpenAI models that support the web search can be found in their [API compatibility documentation](https://platform.openai.com/docs/guides/tools-web-search#api-compatibility).
|
||||
|
||||
#### Prompt Caching
|
||||
|
||||
OpenAI models cache prompt prefixes on their own. Requests from the same app and user share one cache by default, and you can pick the cache with `prompt_cache_key`. Their `usage` reports the tokens read from the cache as `cached_tokens`.
|
||||
|
||||
GPT-5.6 and later models charge for cache writes and let you choose how long an entry lives. Pass `prompt_cache_options` with a `ttl` of `"30m"` (the default) or `"12h"`. The `usage` reports the tokens written with the 30 minute lifetime as `cache_write_tokens` and the tokens written with the 12 hour lifetime as `cache_write_12h_tokens`. Writing a 30 minute entry costs 1.25 times the model's input rate, and writing a 12 hour entry costs 2.5 times the input rate. Reads cost the same for both lifetimes. To mark where a reusable prefix ends, add `prompt_cache_breakpoint: { mode: "explicit" }` to a content part. A breakpoint can carry its own `ttl`, which overrides the request's. OpenAI only takes a 12 hour lifetime, or a lifetime on a breakpoint, through its Responses API, so a request that uses either is sent there.
|
||||
|
||||
```js
|
||||
puter.ai.chat('<your message>', {
|
||||
model: 'gpt-6-luna',
|
||||
prompt_cache_options: { ttl: '12h' }
|
||||
});
|
||||
```
|
||||
|
||||
Older OpenAI models charge nothing for cache writes. Pass `prompt_cache_retention` with `"in_memory"` for an entry that lives a few minutes or `"24h"` for one that can live up to a day. The 24 hour option is the default, and GPT-5.5 and later models only support it.
|
||||
|
||||
The `usageDetails` object on every result carries the same counts under provider-neutral names, with one field per lifetime, such as `cacheWrite30mTokens` and `cacheWrite12hTokens`. See the [ChatResponse](/Objects/chatresponse/) object for the full list.
|
||||
|
||||
### Anthropic models
|
||||
|
||||
#### Prompt Caching
|
||||
|
||||
@@ -41,12 +41,12 @@ The conversation exposes these properties and methods.
|
||||
| --- | --- |
|
||||
| `state` | `prepared`, `connecting`, `connected`, or `closed`. |
|
||||
| `model` | Resolved model name. |
|
||||
| `connect()` | Opens the connection and applies settings. Resolves after the provider acknowledges session configuration. Concurrent calls share one attempt. Calling it while connected opens no additional connection. During a reconnection, it resolves when the reconnection succeeds and rejects if the conversation closes instead. |
|
||||
| `connect()` | Opens the connection and applies settings. Resolves after the provider acknowledges session configuration. Concurrent calls share one connection Promise, including bounded retries for transient setup failures. Calling it while connected opens no additional connection. During a reconnection, it resolves when the reconnection succeeds and rejects if the conversation closes instead. |
|
||||
| `configure(options)` | Updates the given session settings and resolves after the provider acknowledges this update. Settings it isn't given, including ones set through native events, are left alone. Accepts the factory options except `model`. One update can be pending at a time. Provider rules apply to changes such as switching a voice after audio has been generated. |
|
||||
| `sendText(text)` | Adds a user message and requests a response. Text must be nonempty and at most 8192 characters. |
|
||||
| `appendAudio(pcm)` | Appends an `Int16Array` containing PCM16, 24 kHz mono audio. A chunk contains at most 12000 samples. |
|
||||
| `commitAudio()` | Commits buffered audio and requests a response. Requires turn detection to be off, through `turnDetection: false` or a native session update. The provider requires at least 100 ms of buffered audio. |
|
||||
| `interrupt(position?)` | Stops built-in playback immediately and waits for cancellation and playback-history synchronization. A reply requested but not yet started is cancelled as soon as it starts. Custom players provide `{ itemId, contentIndex, playedMs }`. An idle call resolves immediately. |
|
||||
| `interrupt(position?)` | Stops built-in playback immediately and waits for cancellation and playback-history synchronization. A reply requested but not yet started is cancelled as soon as it starts. Custom players provide `{ itemId, contentIndex, playedMs }`. An idle call resolves immediately. Readiness failures reject the returned Promise with `session_not_ready` or `session_closed`. |
|
||||
| `send(event)` | Sends a native OpenAI JSON event. |
|
||||
| `on(type, handler)` | Registers a listener and returns an unsubscribe function. Use `'*'` to receive every native and convenience event. |
|
||||
| `off(type, handler)` | Removes a listener. |
|
||||
@@ -54,9 +54,9 @@ The conversation exposes these properties and methods.
|
||||
|
||||
Attach listeners before connecting to receive initial session events. Closing during connection rejects the pending attempt.
|
||||
|
||||
If the connection drops because of network loss, a server restart or a provider error, the conversation reconnects on its own. While it reconnects, `state` is `connecting`, a `reconnecting` event fires, and pending operations reject with `connection_lost`. The new session gets every setting the provider acknowledged, from the factory options, `configure()` and native `session.update` events. Then `ready` fires again with `reconnected: true`. Listeners, the microphone and the speaker carry over. Microphone audio captured while reconnecting is dropped.
|
||||
If the connection drops because of network loss, a server restart or a provider error, the conversation reconnects on its own. Normal closes with code 1000 end the conversation. While it reconnects, `state` is `connecting`, a `reconnecting` event fires, and pending operations reject with `connection_lost`. The new session gets every setting the provider acknowledged, from the factory options, `configure()` and native `session.update` events. Then `ready` fires again with `reconnected: true`. Listeners, the microphone and the speaker carry over. Microphone audio captured while reconnecting is dropped.
|
||||
|
||||
The conversation also restores recent context. Before `ready` fires, it adds back up to 40 of the most recent conversation items, 48 KiB in total. Restored items are user text, user speech that has a transcript, assistant replies as text, and function calls with their outputs. User speech has a transcript only when input transcription is configured through native session settings. An assistant reply that was interrupted, or still playing when the connection dropped, is cut to what the user saw or heard. The cut is estimated from how much of the audio played, so it can be off by a few words. Images, speech without a transcript, deleted items, and replies still generating when the connection dropped are left out. Restored items get new item IDs, don't fire item events, and count as input tokens on the next response. Item IDs from before the reconnect don't work in later events. If the provider refuses a restored item, an `error` event fires with code `context_restore_failed` and the conversation stays connected.
|
||||
The conversation also restores recent context. Before `ready` fires, it adds back up to 40 of the most recent conversation items, 48 KiB in total. Restored items are user text, user speech that has a transcript, assistant replies as text, and function calls with their outputs. User speech has a transcript only when input transcription is configured through native session settings. An assistant reply that was interrupted, or still playing when the connection dropped, is cut to what the user saw or heard. The cut is estimated from how much of the audio played, so it can be off by a few words. Images, speech without a transcript, deleted items, and replies still generating when the connection dropped are left out. Restored items get new item IDs, don't fire item events, and count as input tokens on the next response. Item IDs from before the reconnect don't work in later events. Restoration uses a contiguous suffix of restorable items. An oversized newest item leaves that suffix empty. Older items left out to fit the window fire a `context_trimmed` event with the count in `omittedItems`; this is routine and not an error. A provider refusal or a failure to send the restored items fires one `error` event with code `context_restore_failed` per restoration. In both cases the conversation stays connected. Items are preserved without truncating text, function arguments or function results.
|
||||
|
||||
When Puter knows ahead of time that the server a session runs on will restart, it sends a `puter.reconnect` event, which also reaches your listeners. Without that notice, a restart drops the connection and the conversation reconnects as above. Within a few seconds, the conversation opens a replacement session in the background while the current one keeps working. It switches once no reply is generating or playing, no user turn is being spoken or transcribed, no function call is waiting for its output, and nothing you sent is waiting on the provider. The switch goes through the same steps as a reconnect, and the `reconnecting` event has code 1012. If the replacement can't open yet, the conversation keeps the current session and tries again later. A refusal that retrying can't fix, such as `insufficient_funds`, stops the attempts. If the server closes the session first, the conversation switches to the replacement, or reconnects if none is open.
|
||||
|
||||
@@ -72,6 +72,7 @@ The conversation emits these events alongside native events.
|
||||
| --- | --- |
|
||||
| `ready` | `model`, `provider`, and `reconnected: true` after a reconnection |
|
||||
| `reconnecting` | `code`, `reason` of the dropped connection. After a `puter.reconnect` event, `code` is 1012. |
|
||||
| `context_trimmed` | `omittedItems`, the number of restorable items left out of the restored context after a reconnection. Fires before `ready`. |
|
||||
| `text` | `delta`, `responseId` |
|
||||
| `transcript` | `role`, `text`, `final`. Assistant deltas use `final: false`. Completed assistant and user transcripts use `final: true`. User transcripts require transcription to be configured through native session settings. |
|
||||
| `audio` | `pcm`, `sampleRate`, `responseId`, `itemId`, `contentIndex`. The PCM chunk is an `Int16Array`. |
|
||||
|
||||
@@ -30,6 +30,8 @@ Browser WebSocket clients pass `realtime` and `openai-insecure-api-key.<PUTER_TO
|
||||
|
||||
The socket can open before authentication and provider setup finish. Listen for errors and closure immediately. Wait for `session.created`, send your session settings, then wait for `session.updated` before submitting a turn.
|
||||
|
||||
To open a replacement while another connection remains healthy, set the optional `purpose=handover` query parameter. These attempts share a smaller per-credential budget, checked before the general connection-attempt budget. Initial connections and dropped-connection recovery use the URL without this parameter. The limits are listed in [rate limits and quotas](/rate-limits-and-quotas/).
|
||||
|
||||
## Text example
|
||||
|
||||
Install the `ws` package and set `PUTER_TOKEN` to your Puter token. Run this example as a Node.js ES module:
|
||||
@@ -109,8 +111,14 @@ A new connection starts an empty conversation. To keep context, add earlier item
|
||||
|
||||
## Errors, billing, and limits
|
||||
|
||||
API errors arrive as `{ "type": "error", "error": { "code": "...", "message": "..." } }`. Handle socket errors and close frames too. Authentication failures use `invalid_api_key`. Other relay codes include `model_not_found`, `insufficient_funds`, `subscription_required`, `rate_limit_exceeded`, `invalid_event`, `connection_timeout`, `provider_unavailable`, `upstream_error`, and `server_restarting`.
|
||||
API errors arrive as `{ "type": "error", "error": { "code": "...", "message": "..." } }`. Handle socket errors and close frames too. Invalid, missing, or revoked credentials use `invalid_api_key`. If Puter can't check a credential or the account's plan, the error is `internal_error` and the session closes with 1011, so try again after a short, randomized delay. Other relay codes include `permission_denied`, `model_not_found`, `insufficient_funds`, `subscription_required`, `rate_limit_exceeded`, `invalid_event`, `connection_timeout`, `provider_unavailable`, `upstream_error`, and `server_restarting`.
|
||||
|
||||
Policy failures generally close with 1008, oversized client messages with 1009, upstream or setup failures with 1011, and server restarts with 1012. Authentication and model failures can arrive after the WebSocket handshake succeeds.
|
||||
Policy failures generally close with 1008, oversized client messages with 1009, upstream or setup failures with 1011, server restarts with 1012, and temporary server overload with 1013. Reconnect after 1011, 1012, or 1013. Authentication and model failures can arrive after the WebSocket handshake succeeds.
|
||||
|
||||
The endpoint requires a paid Puter plan. Usage is charged after completed responses and input transcriptions. While a response generates, part of your balance is held for it, so a session opens only if your balance covers one response. If the provider connection closes before a response reports its usage, the response is charged the full amount held for it. Each account can open 20 sessions at once by default. Each JSON message you send is limited to 64 KiB, including reassembled fragments. Messages from the provider can be up to 4 MiB. Stream audio in small chunks. All message rates, queue caps, timeouts, and other bounds are published in [rate limits and quotas](/rate-limits-and-quotas/).
|
||||
|
||||
Input transcription holds a small amount of your balance per transcribed item, priced at the transcription model you select. Two items are reserved ahead of your audio and replenished as the provider commits them, so a session holds only its spare items plus the items still awaiting a transcript. Changing the transcription model reprices the spare items. If your balance can't cover another item, audio waits while earlier items settle; once none are left to settle, the session closes with `insufficient_funds`. Failed transcriptions, items without final usage, and audio whose commit the provider never acknowledges before the connection closes are each charged that model's fixed per-item amount. Unacknowledged audio is charged one item per started 160 seconds. With server turn detection, only detected speech counts, so silence is never charged. The amounts, timeout and audio limits are listed in [rate limits and quotas](/rate-limits-and-quotas/).
|
||||
|
||||
Provider-reported realtime models must have a known price. A missing model uses the last known model. An explicitly unpriced model closes the connection with `upstream_error`.
|
||||
|
||||
After recording usage, the connection can continue while settled credit is positive, including a fractional balance. Each new billable operation still needs its full reservation after accounting for other outstanding holds.
|
||||
@@ -6,7 +6,7 @@ platforms: [websites, apps]
|
||||
|
||||
Turns push notifications off for your app. Notifications still reach your app live while it's open.
|
||||
|
||||
On a website, it ends this browser's push subscription for your app and removes it from Puter. Only the subscription Puter made is removed, so a subscription another push service made on your site stays as it is. Pages of your site in this browser, including ones open in other tabs, stop showing notifications themselves while they're in the background, so a notification goes out as a push to the user's other browsers instead. The browser's permission isn't changed, so a later [`enable()`](/Notifications/enable/) turns pushes back on without a prompt.
|
||||
On a website, it ends this browser's push subscription for your app and removes it from Puter. Only the subscription Puter made is removed, so a subscription another push service made on your site stays as it is. The browser's permission isn't changed, so a later [`enable()`](/Notifications/enable/) turns pushes back on without a prompt.
|
||||
|
||||
The choice sticks. Page loads don't turn pushes back on, because [`onPush()`](/Notifications/onPush/) only keeps a subscription current after [`enable()`](/Notifications/enable/). Call [`enable()`](/Notifications/enable/) from a click to turn them on again.
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ description: Send a notification from your app to its user, or to the other side
|
||||
platforms: [websites, apps]
|
||||
---
|
||||
|
||||
Sends a notification to the user your app runs for, or with `to` to the other side of a [key-value share](/KV/share/). It goes to your app, not the Puter desktop's notifications panel. Every open page of your app gets it live, on any of the user's devices, through [`onNotification()`](/Notifications/onNotification/) and [`onPush()`](/Notifications/onPush/). When no open page shows it to the user within 5 seconds, it also goes out as a push to the browsers where the user [turned pushes on](/Notifications/enable/) for your app. [When a notification is pushed](/Notifications/push/#when-a-notification-is-pushed) has the exact rule.
|
||||
Sends a notification to the user your app runs for, or with `to` to the other side of a [key-value share](/KV/share/). It goes to your app, not the Puter desktop's notifications panel. Every open page of your app gets it live, on any of the user's devices, through [`onNotification()`](/Notifications/onNotification/) and [`onPush()`](/Notifications/onPush/). When no open page is visible and focused as it arrives, it also goes out as a push to the browsers where the user [turned pushes on](/Notifications/enable/) for your app. [When a notification is pushed](/Notifications/push/#when-a-notification-is-pushed) has the exact rule.
|
||||
|
||||
Only an app can send notifications, meaning an app running on Puter or a website using Puter.js. Every notification carries your app's name. On a website with nobody signed in, the call asks the user to sign in first, as other Puter.js calls do.
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ For `click`, the handler can open the screen the notification is about.
|
||||
|
||||
Clicks reach your app in two ways:
|
||||
|
||||
- **On a website**, from a tap on a push, or on a notification a page of your site showed itself. When a page of your site is open, the tap focuses it and calls its `click` handlers. A page without a `click` handler goes to the notification's `url` instead, which is the `url` your [`onPush()`](/Notifications/onPush/) handler returned, or else the notification's `target`. When no page is open, the tap opens that `url`, and the new page's first `click` handler gets the click.
|
||||
- **On a website**, from a tap on a push. When a page of your site is open, the tap focuses it and calls its `click` handlers. A page without a `click` handler goes to the notification's `url` instead, which is the `url` your [`onPush()`](/Notifications/onPush/) handler returned, or else the notification's `target`. When no page is open, the tap opens that `url`, and the new page's first `click` handler gets the click.
|
||||
- **Inside the Puter desktop**, from a tap on a push that Puter shows. Puter opens your app, or brings it to the front, and delivers the click.
|
||||
|
||||
A click that arrives before your first `click` handler is added, such as the one that opened your app, goes to that first handler, once. Handlers added after it don't get it, so add your handler early during startup. On a website, a page a tap opened drops the click when no `click` handler is added within 10 seconds of the page loading.
|
||||
|
||||
@@ -6,7 +6,7 @@ platforms: [websites, apps]
|
||||
|
||||
Handles the notifications your app sends with `channel: key` in [`notify()`](/Notifications/notify/). While a page of your app is open with this handler, the handler runs in the page as each one arrives. On a website, it also runs in your service worker when a push arrives, and what it returns decides what the push shows.
|
||||
|
||||
A page with a handler tells Puter that it received each notification, before your handler runs. When the page is visible and focused, nothing is pushed. When it's hidden or its window isn't focused, a page of your website shows the notification itself, unless another window of your site is focused. It shows the handler's result, or the notification's own title and text when no handler matches the notification's `channel`. It does this only after the user turned pushes on in this browser with [`enable()`](/Notifications/enable/). Inside the Puter desktop, the page doesn't show it, and it goes out as a push through Puter instead. [When a notification is pushed](/Notifications/push/#when-a-notification-is-pushed) has the full rule.
|
||||
A page with a handler tells Puter that it received each notification, before your handler runs. When the page is visible and focused, nothing is pushed, and your handler is the only thing that runs. When it's hidden or its window isn't focused, the notification goes out as a push to every browser where the user turned pushes on, this one included. The page doesn't show a notification itself. [When a notification is pushed](/Notifications/push/#when-a-notification-is-pushed) has the full rule.
|
||||
|
||||
The user turns pushes on once per browser with [`enable()`](/Notifications/enable/). After that, each page load's [`onPush()`](/Notifications/onPush/) call keeps this browser's subscription current without asking again. Different keys share that one subscription. After [`disable()`](/Notifications/disable/), after the user turns your app off or removes this browser in Puter, or after [`puter.auth.signOut()`](/Auth/signOut/), page loads don't subscribe again until your page calls [`enable()`](/Notifications/enable/) from a click.
|
||||
|
||||
@@ -51,7 +51,7 @@ A [sensitive](/Notifications/notify/#parameters) notification shows only your ap
|
||||
|
||||
#### `options` (Object) (optional)
|
||||
- `serviceWorker` (String): The path of the worker that imports `push-sw.js`. Defaults to `/puter-sw.js`.
|
||||
- `onError` (Function): Called with `{ message, code }` when the handler throws in the page or runs past 5 seconds there (`push_handler_timeout`), when the live listener stops, when keeping the subscription current fails, or when the confirmation push after [`enable()`](/Notifications/enable/) doesn't arrive within 30 seconds (`push_verification_timeout`). When the handler fails, the notification shows its own title and text. Errors that [`enable()`](/Notifications/enable/) rejects with, such as `push_unavailable`, `push_disabled`, `push_revoked` and `too_many_requests`, aren't reported here. Neither is a browser the user removed in Puter, since [`enable()`](/Notifications/enable/) turns pushes back on there. Without `onError`, errors go to the console.
|
||||
- `onError` (Function): Called with `{ message, code }` when the handler throws in the page or runs past 5 seconds there (`push_handler_timeout`), when the live listener stops, when keeping the subscription current fails, or when the confirmation push after [`enable()`](/Notifications/enable/) doesn't arrive within 30 seconds (`push_verification_timeout`). Errors that [`enable()`](/Notifications/enable/) rejects with, such as `push_unavailable`, `push_disabled`, `push_revoked` and `too_many_requests`, aren't reported here. Neither is a browser the user removed in Puter, since [`enable()`](/Notifications/enable/) turns pushes back on there. Without `onError`, errors go to the console.
|
||||
|
||||
## Handler limits
|
||||
|
||||
|
||||
@@ -10,19 +10,9 @@ A website on its own domain needs three steps, and an app inside the Puter deskt
|
||||
|
||||
## When a notification is pushed
|
||||
|
||||
Every open page of your app gets each notification live, on any of the user's devices, through [`onNotification()`](/Notifications/onNotification/) and [`onPush()`](/Notifications/onPush/). Puter then asks those pages whether they received it. If a page answers within 5 seconds that it's visible and focused, or that it shows the notification itself, nothing is pushed. Otherwise Puter pushes it. A page answers only while it has an [`onNotification()`](/Notifications/onNotification/) or [`onPush()`](/Notifications/onPush/) listener.
|
||||
Every open page of your app gets each notification live, on any of the user's devices, through [`onNotification()`](/Notifications/onNotification/) and [`onPush()`](/Notifications/onPush/). A page that's visible and focused when the notification arrives tells Puter, and nothing is pushed. Otherwise Puter pushes it to every browser where the user turned pushes on. While a page of your app is open, the push can take up to 5 seconds to go out. A page tells Puter only while it has an [`onNotification()`](/Notifications/onNotification/) or [`onPush()`](/Notifications/onPush/) listener.
|
||||
|
||||
A page of your website shows the notification itself when all of these are true:
|
||||
|
||||
- The page is hidden, or its window isn't focused.
|
||||
- It has an [`onPush()`](/Notifications/onPush/) handler.
|
||||
- The user turned pushes on in this browser with [`enable()`](/Notifications/enable/), and hasn't turned them off with [`disable()`](/Notifications/disable/).
|
||||
- The user allowed notifications for your site in this browser.
|
||||
- No other window of your site is focused.
|
||||
|
||||
It shows what the handler returns, or the notification's own title and text when no handler matches the notification's `channel`. Inside the Puter desktop, a page doesn't show notifications itself, so a notification goes out as a push whenever your app's window isn't focused.
|
||||
|
||||
So a push goes out when no page of your app is open, or when the open pages are in the background and can't show the notification themselves. A page that loads an older version of Puter.js, such as one bundled from npm, holds the push back while it's visible. A page with a version from before Puter asked pages whether they received a notification holds it back whenever it's open.
|
||||
So a push goes out when no page of your app is open, or when every open page is hidden or in a window that isn't focused. Pages never show notifications themselves, so a page in the background gets the push like the user's other browsers do. Inside the Puter desktop, a notification goes out as a push whenever your app's window isn't focused. A page that loads an older version of Puter.js, such as one bundled from npm, holds the push back while it's visible.
|
||||
|
||||
## Where a push goes
|
||||
|
||||
@@ -168,7 +158,7 @@ To notify another user, share data with them with `notify: true` and send throug
|
||||
|
||||
## When the user taps
|
||||
|
||||
On a website, a tap on a push, or on a notification a page of your site showed itself, focuses an open page of your site and calls its [`on('click')`](/Notifications/on/) handlers. With no page open, it opens the handler's `url`, or else the notification's `target`, and the new page's first [`on('click')`](/Notifications/on/) handler gets the click. A tap reaches one page, once: when an open page doesn't answer within about half a second, the tap opens its URL instead, and the slow page doesn't get it, unless that page is already at the tap's URL. If that page wasn't loaded through your worker, such as one opened before the worker was installed, the URL opens in a new tab. Inside the Puter desktop, a tap opens your app and hands the click to [`on('click')`](/Notifications/on/).
|
||||
On a website, a tap on a push focuses an open page of your site and calls its [`on('click')`](/Notifications/on/) handlers. With no page open, it opens the handler's `url`, or else the notification's `target`, and the new page's first [`on('click')`](/Notifications/on/) handler gets the click. A tap reaches one page, once: when an open page doesn't answer within about half a second, the tap opens its URL instead, and the slow page doesn't get it, unless that page is already at the tap's URL. If that page wasn't loaded through your worker, such as one opened before the worker was installed, the URL opens in a new tab. Inside the Puter desktop, a tap opens your app and hands the click to [`on('click')`](/Notifications/on/).
|
||||
|
||||
A push can also carry up to two buttons, which your [`onPush()`](/Notifications/onPush/) handler returns as `actions`. The pressed button's id reaches [`on('click')`](/Notifications/on/) as `action`. [Add Buttons to Push Notifications](/recipes/add-buttons-to-push-notifications/) shows how.
|
||||
|
||||
@@ -180,13 +170,13 @@ When the user reads a notification in your app, call [`dismiss()`](/Notification
|
||||
|
||||
A notification with a `group` replaces your earlier one in the same group, on the device and in your app, and alerts the user again. A group can name a chat, a thread or a person, such as `dm:[email protected]`. [`notify()`](/Notifications/notify/#groups) has the rule.
|
||||
|
||||
With `sensitive: true`, pushes and the notification a page shows itself name only your app instead of showing the title and text. [`onNotification()`](/Notifications/onNotification/) still gets the whole notification. [`notify()`](/Notifications/notify/#parameters) says what else a sensitive push leaves out.
|
||||
With `sensitive: true`, pushes name only your app instead of showing the title and text. [`onNotification()`](/Notifications/onNotification/) still gets the whole notification. [`notify()`](/Notifications/notify/#parameters) says what else a sensitive push leaves out.
|
||||
|
||||
## A minimal page
|
||||
|
||||
To try it, serve the page below from a site that has `/puter-sw.js`. It registers the handler, turns pushes on from a click, and sends a notification 5 seconds after you click Send. Switch to another tab during those 5 seconds. The page is then in the background, so it shows the notification itself and nothing is pushed.
|
||||
To try it, serve the page below from a site that has `/puter-sw.js`. It registers the handler, turns pushes on from a click, and sends a notification 5 seconds after you click Send. Switch to another tab during those 5 seconds. The page is then in the background, so the notification goes out as a push and your worker shows it. If you stay on the page, it gets the notification live and nothing is pushed.
|
||||
|
||||
A real push goes out only when no page of your site can show the notification. To see one, turn pushes on, close every tab of your site, and send from somewhere else, such as a [Puter Worker](/Workers/) that runs as your app or an [events handler](/Events/handlers/).
|
||||
To reach the user with every tab of your site closed, send from somewhere else, such as a [Puter Worker](/Workers/) that runs as your app or an [events handler](/Events/handlers/).
|
||||
|
||||
```html
|
||||
<html>
|
||||
@@ -347,7 +337,7 @@ The browser can be allowed to show notifications while the operating system hide
|
||||
| The `onError` of [`onPush()`](/Notifications/onPush/) gets `push_verification_timeout` | The worker doesn't import `push-sw.js`, or the confirmation push didn't arrive within 30 seconds. The next page load tries again. |
|
||||
| A push shows the plain title and text, not your handler's result | This browser hasn't run [`onPush()`](/Notifications/onPush/) for that `channel` yet, or the handler threw, took over 5 seconds, or used an outside variable. Check the worker's console. |
|
||||
| Pushes stop until the user opens your site again | The browser replaced its push subscription for your site, as browsers sometimes do. Your service worker keeps the new one, and the next page load that calls [`onPush()`](/Notifications/onPush/) registers it with Puter. Pushes sent before that don't arrive. |
|
||||
| No push while your site is open in a tab | The open page received the notification live. It shows one only when it's hidden or its window isn't focused. |
|
||||
| No push while your site is open in a tab | The open page received the notification live while it was visible and focused. |
|
||||
| No push at all | The browser isn't running, the operating system hides its notifications ([above](#operating-system-settings)), the user reached the [push limit](/rate-limits-and-quotas/#notifications) for your app, or the user has no usage credit left. |
|
||||
|
||||
## Push delivery
|
||||
|
||||
@@ -64,7 +64,7 @@ Extra detail on why generation stopped (e.g. a refusal classification). Provider
|
||||
|
||||
#### `usageDetails` (Object)
|
||||
|
||||
A provider-neutral usage breakdown, present alongside `usage` on every result. `inputTokens` excludes cache reads/writes; `outputTokens` includes reasoning tokens. Fields: `inputTokens`, `outputTokens`, `cacheReadTokens`, `cacheWrite5mTokens`, `cacheWrite1hTokens`, `reasoningTokens`, `webSearchRequests`, `webFetchRequests`, `speed` (`"fast"` or `"standard"`), `serviceTier`, and `iterations` (a per-pass breakdown when the upstream ran several passes, e.g. compaction or an advisor sub-call).
|
||||
A provider-neutral usage breakdown, present alongside `usage` on every result. `inputTokens` excludes cache reads/writes; `outputTokens` includes reasoning tokens. Fields: `inputTokens`, `outputTokens`, `cacheReadTokens`, `cacheWrite5mTokens`, `cacheWrite30mTokens`, `cacheWrite1hTokens`, `cacheWrite12hTokens`, `reasoningTokens`, `webSearchRequests`, `webFetchRequests`, `speed` (`"fast"` or `"standard"`), `serviceTier`, and `iterations` (a per-pass breakdown when the upstream ran several passes, e.g. compaction or an advisor sub-call). Each `cacheWrite*Tokens` field counts the tokens written to the cache with that lifetime: Claude and Moonshot write 5 minute and 1 hour entries, OpenAI writes 30 minute and 12 hour entries.
|
||||
|
||||
Prefer `usageDetails` over `usage` for anything beyond the raw billing totals. Claude's `usage.thinking_tokens` is informational: it's included in `output_tokens` and not billed separately — `usageDetails.reasoningTokens` carries the same count.
|
||||
|
||||
|
||||
@@ -102,7 +102,7 @@ Extra detail on why generation stopped (e.g. a refusal classification). Provider
|
||||
|
||||
#### `usageDetails` (Object)
|
||||
|
||||
A provider-neutral usage breakdown, present on the final `usage` chunk. `inputTokens` excludes cache reads/writes; `outputTokens` includes reasoning tokens. Fields: `inputTokens`, `outputTokens`, `cacheReadTokens`, `cacheWrite5mTokens`, `cacheWrite1hTokens`, `reasoningTokens`, `webSearchRequests`, `webFetchRequests`, `speed` (`"fast"` or `"standard"`), `serviceTier`, and `iterations` (a per-pass breakdown when the upstream ran several passes, e.g. compaction or an advisor sub-call).
|
||||
A provider-neutral usage breakdown, present on the final `usage` chunk. `inputTokens` excludes cache reads/writes; `outputTokens` includes reasoning tokens. Fields: `inputTokens`, `outputTokens`, `cacheReadTokens`, `cacheWrite5mTokens`, `cacheWrite30mTokens`, `cacheWrite1hTokens`, `cacheWrite12hTokens`, `reasoningTokens`, `webSearchRequests`, `webFetchRequests`, `speed` (`"fast"` or `"standard"`), `serviceTier`, and `iterations` (a per-pass breakdown when the upstream ran several passes, e.g. compaction or an advisor sub-call). Each `cacheWrite*Tokens` field counts the tokens written to the cache with that lifetime: Claude and Moonshot write 5 minute and 1 hour entries, OpenAI writes 30 minute and 12 hour entries.
|
||||
|
||||
#### `message` (String)
|
||||
|
||||
|
||||
@@ -102,20 +102,32 @@ Realtime sessions ([`puter.ai.convo()`](/AI/convo/), `/puterai/openai/v1/realtim
|
||||
| Event type length | 1–128 characters | Invalid client envelopes receive `invalid_event` and close with 1008. Invalid upstream envelopes close with 1011. |
|
||||
| Client message rate | 100/s, burst 100 | Error `rate_limit_exceeded`, close 1008. Ping and pong frames count toward this allowance. |
|
||||
| Client upload rate | 128 KiB/s, burst 64 KiB | Reads pause to refill the budget. More than 64 KiB of overdraft closes with `rate_limit_exceeded`. |
|
||||
| Input pending session setup | 32 messages or 64 KiB | Error `rate_limit_exceeded`, close 1008. |
|
||||
| Client events waiting for forwarding | 32 messages or 64 KiB | Error `rate_limit_exceeded`, close 1008. |
|
||||
| Forwarding queue in either direction | Reads pause at 128 KiB. Admission stops above 192 KiB toward the provider and above 4 MiB plus 128 KiB toward the client. | A full queue closes with `rate_limit_exceeded`. |
|
||||
| Client that stops reading | 30 seconds of paused upstream reads | Close 1008. |
|
||||
| Pending usage records | 32 | Error `rate_limit_exceeded`, close 1008. Records already received are still billed. |
|
||||
| Pending usage records | 32 | Error `rate_limit_exceeded`, close 1013. Records already received are still billed. |
|
||||
| Numeric usage fields | Finite, nonnegative, at most 1 billion | Invalid upstream usage closes with 1011. A `null` usage counts as none. |
|
||||
| Session setup timeout | 15 seconds | Error `connection_timeout`, close 1011. |
|
||||
| Upstream handshake timeout | 10 seconds | Close 1011. |
|
||||
| Heartbeat interval | 30 seconds | A missing pong terminates the connection on the next heartbeat. |
|
||||
| Credential cache and recheck | Cache 60 seconds, sweep every 15 seconds, at most 16 checks in parallel | Invalid credentials close with 1008. Overlapping sweeps are skipped. |
|
||||
| Session attempts per credential | 60 per minute | Error `rate_limit_exceeded`, close 1008. |
|
||||
| Credential cache and recheck | Cache 60 seconds, sweep every 15 seconds, at most 16 checks in parallel | Invalid credentials close with 1008. A check that fails at connection gets `internal_error` and closes with 1011. Once failures have continued for 60 seconds from the first failed lookup, the next failed check closes with `internal_error` and 1011. Subscription eligibility is checked independently on each sweep and after a plan invalidation. Revocation events close the affected credentials. Overlapping sweeps are skipped. |
|
||||
| Session attempts per signature-verified credential | 60 per minute | Error `rate_limit_exceeded`, close 1008. |
|
||||
| Pre-auth attempts | 120 per client IP and 600 per node per minute | Local memory admission runs before the WebSocket handshake and signature verification. Exceeding either rate returns HTTP 429. Client IP follows the configured proxy trust policy. The shared memory limiter caps its state at 10,000 keys with a 1-hour retention floor and a 60-second sweep. |
|
||||
| Unauthenticated upgrade sockets, including closing sockets | 64 per node | Further upgrades receive HTTP 503. The slot is retained until successful admission or transport closure. |
|
||||
| Authentication setups in progress | 64 per node | Further upgrades receive HTTP 503. |
|
||||
| Presented credential | 8192 UTF-8 bytes | Longer or malformed credentials receive `invalid_api_key`. Unverified credentials allocate no distributed credential limiter state. Limiter lookup failures close with `internal_error` and 1011. |
|
||||
| Credit held per transcription item | The selected model's budget for 8,000 audio input, 8,000 text input and 4,096 text output tokens, or 160 seconds for duration-priced models, at that model's cost factor. Before cost factors: `gpt-4o-transcribe` 10,896,000 micro-cents, `gpt-4o-mini-transcribe` 5,448,000, `whisper-1` 1,600,000, `gpt-transcribe` 1,200,000, and 10,896,000 for any other model | Two spare items are reserved before transcription configuration or audio is forwarded, and replenished as the provider commits items. Enabling `gpt-4o-transcribe` therefore holds 21,792,000 micro-cents, or $0.21792, before cost factors. A model change releases the spare items and reserves new ones at the new price. The hold is a budget rather than a cap on reported usage. |
|
||||
| Transcription credit exhausted | When the account can't cover another item | Client reads pause while the session still holds items that may free credit. Once it holds none, error `insufficient_funds`, close 1008. |
|
||||
| Pending transcription identity | 64 items, spare items included | Reaching the cap pauses client reads until items settle. Provider commits beyond the cap close the connection with `rate_limit_exceeded` and 1013, and bill unresolved work conservatively. Unresolved item identities are retained. |
|
||||
| Transcription usage timeout | 30 seconds per committed item | Missing usage, transcription failure, or an upstream close bills the item's model budget before cost factors. A late or duplicate completion is ignored. |
|
||||
| Transcription reservation lifetime | 90 seconds, renewed on the 30-second heartbeat | Credit stays held until accepted usage is recorded. Unused reservation credit is released when the connection ends. |
|
||||
| Audio awaiting a commit acknowledgement | One held item per 160 seconds, at the session's input format | Counts all audio since the last commit or clear without turn detection, or only detected speech with server turn detection. Longer audio holds more items rather than closing the session. If the connection closes before the commit is acknowledged, each held item is billed. |
|
||||
| Completed transcription identity history | 128 items | Recent duplicate commits are ignored. Pending identities have a separate lifetime. |
|
||||
| Replacement attempts marked `purpose=handover` | 20 per credential per minute | This shared limit is checked before the general 60-attempt limit. A refused probe consumes no general attempt. Initial and dropped-session recovery attempts retain their general budget. |
|
||||
| Credit held per response | 8,000 audio input and 4,096 audio output tokens at the session model's rates, held until the response's usage is recorded | A session opens only if the account can cover one hold. A response the account can't cover gets `insufficient_funds`, close 1008, and is billed for what it generated. A response whose usage never arrives because the provider connection closed first is billed the full hold. |
|
||||
| Restart notice (`puter.reconnect`) | At most once a minute per session | Repeated announcements within the minute aren't sent. |
|
||||
| In-flight response drain at shutdown | 5 seconds | Close 1012. |
|
||||
| Billing a response after the client disconnects | 5 seconds | The response is cancelled and billed for what it generated, including one that was requested but starts only after the disconnect. A response that doesn't report its usage within the 5 seconds is billed the credit held for it. |
|
||||
| Billing a response after the client disconnects | 5 seconds | The response is cancelled and billed for what it generated, including one that was requested but starts only after the disconnect. Pending input transcriptions share this settlement window. Audio whose commit was not acknowledged is committed during settlement. Accepted work without final usage is billed conservatively. A response that does not report its usage within the 5 seconds is billed the credit held for it. |
|
||||
|
||||
Send audio as a stream of small chunks. The message cap applies to text, configuration, tool output, and audio alike.
|
||||
|
||||
@@ -129,6 +141,7 @@ The [`puter.ai.convo()`](/AI/convo/) SDK also applies these limits before sendin
|
||||
| PCM chunk | 12000 samples at 24 kHz, or 500 ms | Throws `input_too_large`. |
|
||||
| Native JSON traversal | Depth 16, 2048 visited values, 2048 array entries or object fields | Throws `input_too_large` before serialization. |
|
||||
| Outbound buffered data | 128 KiB for audio, 192 KiB for other events | Throws `queue_full`. The built-in microphone drops the chunk instead. |
|
||||
| Incoming server event | 4 MiB of UTF-8 | The conversation closes with code 4009 and doesn't reconnect. |
|
||||
| Pending acknowledgments | 8 | Rejects with `operation_in_progress`. |
|
||||
| Concurrent configuration updates | 1 | Rejects with `operation_in_progress`. |
|
||||
| Authentication and provider acknowledgment timeout | 10 seconds | Rejects preparation or the pending operation. |
|
||||
@@ -137,10 +150,10 @@ The [`puter.ai.convo()`](/AI/convo/) SDK also applies these limits before sendin
|
||||
| Capture chunk duration | 20 ms | AudioWorklet sends 480 PCM16 samples per chunk. |
|
||||
| Unacknowledged capture chunks | 25, or 500 ms | Further chunks are dropped until the page catches up. |
|
||||
| Queued playback | 300 seconds | Further audio for the reply is dropped and `queue_full` is reported once. |
|
||||
| Reconnection attempts | 10, backing off from 500 ms to 10 seconds, each delay randomized between half and one and a half times its value | The conversation closes with the original close code. |
|
||||
| Replacement session after a restart notice | Opened within 3 seconds of the notice, then retried from 1 second, backing off to 10 seconds, each delay randomized between half and one and a half times its value. Settings on a held replacement are acknowledged within 3 seconds. | The current session keeps working until the conversation switches or the server closes it. A refusal that retrying can't fix stops the attempts. |
|
||||
| Connection and reconnection attempts | 10, backing off from 500 ms to 10 seconds, each delay randomized between half and one and a half times its value | Initial transient failures retry through the original connection Promise. Terminal credential, model, funds, normal-close and oversized-event failures stop retries. An established conversation closes with the original close code after retries are exhausted. |
|
||||
| Replacement session after a restart notice | Opened within 3 seconds of the notice, then retried from 1 second, backing off to 10 seconds, each delay randomized between half and one and a half times its value. Settings on a held replacement are acknowledged within 3 seconds. | The current session keeps working until the conversation switches or the server closes it. Probes stop after 3 attempts or 30 seconds. Conversations sharing one SDK instance and credential share a budget of 20 probes per minute. Dropped-session recovery uses its separate attempt loop. A refusal that retrying cannot fix stops the probes. |
|
||||
| Offline wait while reconnecting | 2 minutes per reconnection, in browsers that report being offline | Attempts continue after the wait. |
|
||||
| Context restored after a reconnection | The 40 most recent restorable items, 48 KiB in total | Older items are left out. |
|
||||
| Context restored after a reconnection | The 40 most recent restorable items, 48 KiB in total | Restoration uses a contiguous suffix of restorable items. If the newest item exceeds the byte budget, the suffix is empty. Omitted items fire a nonfatal `context_trimmed` event with `omittedItems`. `context_restore_failed` is reserved for a provider refusal or a failed send. Text, function arguments and function results are preserved without truncation. |
|
||||
| Remembered canceled responses | 32 | Older response IDs are evicted from the suppression set. |
|
||||
|
||||
|
||||
@@ -284,6 +297,8 @@ Each call is also capped, the same for every account:
|
||||
|
||||
A `get()` or batch `set()` over its cap, or an `offset` over 5,000, is rejected with `bad_request` and nothing is read or written. A `limit` over 1,000 is lowered to 1,000, and the page's `cursor` picks up from there.
|
||||
|
||||
Internal metering reads check up to 20 counters in Redis concurrently per call. Store-backed metering reads make up to 3 attempts when a flush overlaps the read. Each metering flush writes up to 20 counters concurrently.
|
||||
|
||||
### Filesystem
|
||||
|
||||
Per minute unless stated:
|
||||
@@ -494,7 +509,7 @@ The notification limits count notifications written. A notification that replace
|
||||
|
||||
Notifications one side of a KV share sends the other through its handle count against the recipient's `app.message` limits, and also against the limit for that sender, recipient and app, across all their shares in that app. Over either one, the call fails with the same error, so the sender can't tell which limit it hit. A call that the recipient's limits refuse still counts against the share limit. The pushes count against the recipient's push limits and are billed to the recipient.
|
||||
|
||||
A notification goes out as a push only when no open page of the app answers within 5 seconds that it's visible and focused, or that it shows the notification itself. The push limits count only pushes sent, and a replacement that goes out as a push counts like any other. An app gets the lower push limits while it's younger than 30 days or not approved for listing. A website using Puter.js starts out unlisted, so it gets the lower limits. Over a push limit, the call still succeeds and the notification still reaches the app live, but it isn't pushed.
|
||||
A notification goes out as a push only when no open page of the app is visible and focused when it arrives. The push limits count only pushes sent, and a replacement that goes out as a push counts like any other. An app gets the lower push limits while it's younger than 30 days or not approved for listing. A website using Puter.js starts out unlisted, so it gets the lower limits. Over a push limit, the call still succeeds and the notification still reaches the app live, but it isn't pushed.
|
||||
|
||||
A push carries about 4 KB, and one that doesn't fit has its text shortened. Turning pushes on in another browser past the app's browser limit removes that app's least recently seen one. A browser is also removed when its push service answers `404` or `410`, after 10 failed sends and over 3 days without receiving a push or renewing its registration, or after 60 days without either. A page that calls [`onPush()`](/Notifications/onPush/) renews it on load.
|
||||
|
||||
|
||||
@@ -104,10 +104,10 @@ puter.notifications.on('click', ({ data }) => {
|
||||
|
||||
Here `openRoomById()` is your app's own function that shows a room.
|
||||
|
||||
While the chat is open, the notification reaches this handler in the page, and
|
||||
the page shows it only when its tab is hidden or its window isn't focused. The
|
||||
message itself arrives through the room's `watch()`. While the chat is closed,
|
||||
the same handler runs in your service worker to decide what the push shows.
|
||||
While the chat is open, the notification reaches this handler in the page. When
|
||||
the tab is visible and focused, nothing is pushed. Otherwise the notification
|
||||
goes out as a push, and the same handler runs in your service worker to decide
|
||||
what it shows. The message itself arrives through the room's `watch()`.
|
||||
|
||||
## Turn Notifications On
|
||||
|
||||
|
||||
@@ -89,7 +89,7 @@ async function sendMessageNotification () {
|
||||
document.getElementById('send').addEventListener('click', sendMessageNotification);
|
||||
```
|
||||
|
||||
The `group` makes a newer notification for the same chat replace this one, instead of adding another. Every open page gets the notification live, and a page that's visible and focused, or shows the notification itself, holds the push back. So a notification sent from the page the user is looking at isn't pushed. [When a notification is pushed](/Notifications/push/#when-a-notification-is-pushed) has the full rule. To send while the user's tabs are closed, call [`notify()`](/Notifications/notify/) from somewhere that keeps running, such as a [Puter Worker](/Workers/) with [`user.puter.notifications.notify()`](/Notifications/notify/), or an [events handler](/Events/handlers/) with [`user.notifications.notify()`](/Notifications/notify/). [Push notifications for your app](/Notifications/push/#sending-a-notification) has the details, and [Run Code in the Background](/recipes/run-code-in-the-background/) sets up an events handler. To notify another user, see [Notify Chat Members of New Messages](/recipes/notify-chat-members-of-new-messages/).
|
||||
The `group` makes a newer notification for the same chat replace this one, instead of adding another. Every open page gets the notification live, and a page that's visible and focused holds the push back. So a notification sent from the page the user is looking at isn't pushed. [When a notification is pushed](/Notifications/push/#when-a-notification-is-pushed) has the full rule. To send while the user's tabs are closed, call [`notify()`](/Notifications/notify/) from somewhere that keeps running, such as a [Puter Worker](/Workers/) with [`user.puter.notifications.notify()`](/Notifications/notify/), or an [events handler](/Events/handlers/) with [`user.notifications.notify()`](/Notifications/notify/). [Push notifications for your app](/Notifications/push/#sending-a-notification) has the details, and [Run Code in the Background](/recipes/run-code-in-the-background/) sets up an events handler. To notify another user, see [Notify Chat Members of New Messages](/recipes/notify-chat-members-of-new-messages/).
|
||||
|
||||
A website using Puter.js starts out unlisted, so it gets the lower push limits for new apps. A notification over a limit still reaches your app live. The numbers are on [Rate Limits and Quotas](/rate-limits-and-quotas/#notifications).
|
||||
|
||||
|
||||
@@ -0,0 +1,180 @@
|
||||
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
|
||||
import { chromium } from '@playwright/test';
|
||||
import express from 'express';
|
||||
import webpack from 'webpack';
|
||||
import { mkdtemp, rm } from 'node:fs/promises';
|
||||
import { tmpdir } from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
let browser;
|
||||
let server;
|
||||
let directory;
|
||||
let origin;
|
||||
let port;
|
||||
const requests = [];
|
||||
const guiRoot = fileURLToPath(new URL('../../../', import.meta.url));
|
||||
const sessionToken = 'app-browser-test-session';
|
||||
const appRows = Array.from({ length: 105 }, (_, i) => ({
|
||||
name: `app-${i}`, title: `App ${i}`, icon: null,
|
||||
}));
|
||||
const kv = {
|
||||
dashboard_apps_order: JSON.stringify(['app-104', 'app-1', 'app-2']),
|
||||
dashboard_app_groups: JSON.stringify([{ id: 'work', name: 'Work', apps: ['app-1', 'app-2'] }]),
|
||||
dashboard_removed_apps: JSON.stringify(['removed']),
|
||||
};
|
||||
|
||||
beforeAll(async () => {
|
||||
directory = await mkdtemp(path.join(tmpdir(), 'puter-app-browser-'));
|
||||
await new Promise((resolve, reject) => {
|
||||
const compiler = webpack({
|
||||
mode: 'development',
|
||||
entry: { 'apps-embed': path.join(guiRoot, 'src/appsEmbed.js') },
|
||||
output: { path: directory, filename: '[name].min.js' },
|
||||
module: { rules: [{ test: /jquery-3\.6\.1\.min\.js$/, type: 'javascript/auto' }] },
|
||||
});
|
||||
compiler.run((error, stats) => compiler.close(closeError => {
|
||||
if ( error || closeError ) return reject(error || closeError);
|
||||
if ( stats.hasErrors() ) return reject(new Error(stats.toString()));
|
||||
resolve();
|
||||
}));
|
||||
});
|
||||
const app = express();
|
||||
app.use(express.json({ type: ['application/json', 'text/plain'] }));
|
||||
app.use((req, res, next) => {
|
||||
requests.push({ path: req.path, body: req.body, auth: req.headers.authorization });
|
||||
next();
|
||||
});
|
||||
app.use('/dist', express.static(directory));
|
||||
app.use('/css', express.static(path.join(guiRoot, 'src/css')));
|
||||
app.get('/sdk.js', (_req, res) => res.sendFile(path.resolve(guiRoot, '../puter-js/dist/puter.js')));
|
||||
app.get('/installedApps', (req, res) => {
|
||||
if ( req.headers.authorization !== `Bearer ${sessionToken}` ) return res.sendStatus(401);
|
||||
const offset = (Number(req.query.page) - 1) * 100;
|
||||
res.json(appRows.slice(offset, offset + 100));
|
||||
});
|
||||
app.get('/get-launch-apps', (_req, res) => res.json({ recommended: [{ name: 'removed', title: 'Removed' }] }));
|
||||
app.get('/cache/last-change-timestamp', (_req, res) => res.json({ timestamp: 0 }));
|
||||
app.post('/drivers/call', (req, res) => {
|
||||
if ( req.body.auth_token !== sessionToken ) return res.sendStatus(401);
|
||||
res.json({ success: true, result: kv[req.body.args?.key] ?? null });
|
||||
});
|
||||
app.get('/app/:name', (_req, res) => res.send('<title>Opened app</title>'));
|
||||
app.get('/host', (_req, res) => res.send(`<!DOCTYPE html><html><body style="margin:0">
|
||||
<iframe title="More apps" src="${origin}/embed/apps" style="border:0;width:100vw;height:100vh"></iframe>
|
||||
</body></html>`));
|
||||
app.get('/embed/apps', (_req, res) => {
|
||||
res.setHeader('Content-Security-Policy', `frame-ancestors 'self' http://*.puter.localhost:${port}`);
|
||||
res.send(`<!DOCTYPE html><html><head><meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<link rel="stylesheet" href="/css/normalize.css"><link rel="stylesheet" href="/css/dashboard.css">
|
||||
<link rel="stylesheet" href="/css/apps-embed.css"></head><body>
|
||||
<main id="app-browser" class="dashboard"></main>
|
||||
<script type="application/json" id="app-browser-config">${JSON.stringify({ apiOrigin: origin, sdkUrl: '/sdk.js' })}</script>
|
||||
<script defer src="/dist/apps-embed.min.js"></script></body></html>`);
|
||||
});
|
||||
server = await new Promise(resolve => {
|
||||
const listener = app.listen(0, '127.0.0.1', () => resolve(listener));
|
||||
});
|
||||
port = server.address().port;
|
||||
origin = `http://puter.localhost:${port}`;
|
||||
browser = await chromium.launch();
|
||||
}, 60000);
|
||||
|
||||
afterAll(async () => {
|
||||
await browser?.close();
|
||||
if ( server ) await new Promise(resolve => server.close(resolve));
|
||||
if ( directory ) await rm(directory, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
describe('embedded app browser', () => {
|
||||
it.each([{ width: 1100, height: 720 }, { width: 390, height: 700 }])(
|
||||
'browses and launches apps across subdomains at $width px', async viewport => {
|
||||
const context = await browser.newContext({ viewport, storageState: {
|
||||
cookies: [], origins: [{ origin, localStorage: [
|
||||
{ name: 'auth_token_v2', value: sessionToken },
|
||||
{ name: 'user_preferences', value: viewport.width < 500 ? '{invalid' : '{"language":"en"}' },
|
||||
] }],
|
||||
} });
|
||||
const page = await context.newPage();
|
||||
page.setDefaultTimeout(6000);
|
||||
const errors = [];
|
||||
page.on('pageerror', error => errors.push(error.message));
|
||||
requests.length = 0;
|
||||
try {
|
||||
const parentUrl = `http://spreadsheet.puter.localhost:${port}/host`;
|
||||
await page.goto(parentUrl);
|
||||
const frame = page.frameLocator('iframe');
|
||||
await frame.locator('.myapps-group-tile').waitFor();
|
||||
expect(await frame.locator('.myapps-reorder-btn, .myapps-add-tile, .myapps-tile-remove').count()).toBe(0);
|
||||
expect(await frame.locator('.myapps-search-wrap').evaluate(el => getComputedStyle(el).paddingTop)).toBe('16px');
|
||||
expect(await frame.locator('.myapps-tile').first().getAttribute('data-app-name')).toBe('app-104');
|
||||
expect(await frame.locator('[data-app-name="removed"]').count()).toBe(0);
|
||||
expect(requests.filter(req => req.path === '/installedApps')).toHaveLength(2);
|
||||
await frame.locator('.myapps-group-tile').click();
|
||||
const name = frame.locator('.myapps-group-name');
|
||||
expect(await name.getAttribute('readonly')).not.toBeNull();
|
||||
await frame.locator('.myapps-group-panel .myapps-tile').first().waitFor();
|
||||
await frame.locator('.myapps-group-open').waitFor();
|
||||
await page.screenshot({ path: path.join(tmpdir(), `puter-app-browser-${viewport.width}.png`), animations: 'disabled' });
|
||||
const panel = await frame.locator('.myapps-group-panel').boundingBox();
|
||||
expect(panel.width).toBeGreaterThan(150);
|
||||
expect(panel.x).toBeGreaterThanOrEqual(0);
|
||||
expect(panel.x + panel.width).toBeLessThanOrEqual(viewport.width + 1);
|
||||
await frame.locator('.myapps-group-panel .myapps-tile').first().press('Escape');
|
||||
await frame.locator('.myapps-group-overlay').waitFor({ state: 'detached' });
|
||||
await frame.locator('.myapps-search').fill('App 104');
|
||||
expect(await frame.locator('.myapps-tile').count()).toBe(1);
|
||||
const popupPromise = context.waitForEvent('page');
|
||||
await frame.locator('.myapps-tile').click();
|
||||
const popup = await popupPromise;
|
||||
await popup.waitForLoadState();
|
||||
expect(popup.url()).toBe(`${origin}/app/app-104`);
|
||||
expect(page.url()).toBe(parentUrl);
|
||||
expect(await popup.evaluate(() => window.opener)).toBeNull();
|
||||
expect(requests.filter(req => req.path === '/drivers/call').every(req => req.body.method === 'get')).toBe(true);
|
||||
expect(errors).toEqual([]);
|
||||
} finally {
|
||||
await context.close();
|
||||
}
|
||||
}, 30000,
|
||||
);
|
||||
|
||||
it('does not start login or load the SDK when the existing session is absent', async () => {
|
||||
const page = await browser.newPage();
|
||||
requests.length = 0;
|
||||
try {
|
||||
await page.goto(`${origin}/embed/apps`);
|
||||
await page.getByRole('status').waitFor();
|
||||
expect(requests.some(req => ['/sdk.js', '/installedApps', '/login', '/signup'].includes(req.path))).toBe(false);
|
||||
} finally {
|
||||
await page.close();
|
||||
}
|
||||
});
|
||||
|
||||
it('shows an unavailable state if the SDK cannot load', async () => {
|
||||
const context = await browser.newContext({ storageState: {
|
||||
cookies: [], origins: [{ origin, localStorage: [{ name: 'auth_token_v2', value: sessionToken }] }],
|
||||
} });
|
||||
try {
|
||||
const page = await context.newPage();
|
||||
await page.route('**/sdk.js', route => route.abort());
|
||||
await page.goto(`${origin}/embed/apps`);
|
||||
await page.getByRole('status').waitFor();
|
||||
expect(await page.getByRole('status').textContent()).toContain('Apps are unavailable');
|
||||
} finally {
|
||||
await context.close();
|
||||
}
|
||||
});
|
||||
|
||||
it('blocks a parent outside the deployment domain', async () => {
|
||||
const page = await browser.newPage();
|
||||
requests.length = 0;
|
||||
try {
|
||||
await page.goto(`http://outside.localhost:${port}/host`);
|
||||
expect(requests.some(req => req.path === '/embed/apps')).toBe(true);
|
||||
expect(requests.some(req => req.path === '/dist/apps-embed.min.js')).toBe(false);
|
||||
} finally {
|
||||
await page.close();
|
||||
}
|
||||
});
|
||||
});
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,166 @@
|
||||
// @vitest-environment jsdom
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import jQuery from '../../lib/jquery-3.6.1/jquery-3.6.1.min.js';
|
||||
import { encode } from 'html-entities';
|
||||
import { createAppBrowser } from './AppBrowser.js';
|
||||
import { APPS_ORDER_KV_KEY } from './appOrder.js';
|
||||
import { APP_GROUPS_KV_KEY } from './appGroups.js';
|
||||
import { REMOVED_APPS_KV_KEY } from './removedApps.js';
|
||||
|
||||
let installed;
|
||||
let recommended;
|
||||
let saved;
|
||||
let browser;
|
||||
let $root;
|
||||
const onLaunch = vi.fn();
|
||||
|
||||
beforeEach(() => {
|
||||
globalThis.$ = globalThis.jQuery = jQuery;
|
||||
globalThis.html_encode = value => encode(String(value ?? ''));
|
||||
globalThis.i18n = key => key;
|
||||
window.icons = {};
|
||||
window.api_origin = 'https://api.test.local';
|
||||
window.auth_token = 'test-session';
|
||||
window.matchMedia = () => ({ matches: false });
|
||||
globalThis.ResizeObserver = class { observe () {} disconnect () {} };
|
||||
HTMLElement.prototype.scrollTo = function ({ left }) { this.scrollLeft = left; };
|
||||
installed = [
|
||||
{ name: 'sheet', title: 'Spreadsheet' },
|
||||
{ name: 'notes', title: 'Notes' },
|
||||
{ name: 'editor', title: 'Editor' },
|
||||
];
|
||||
recommended = [{ name: 'camera', title: 'Camera' }];
|
||||
saved = new Map([
|
||||
[APPS_ORDER_KV_KEY, JSON.stringify(['notes', 'sheet', 'camera', 'editor'])],
|
||||
[APP_GROUPS_KV_KEY, JSON.stringify([{ id: 'work', name: 'Work', apps: ['notes', 'sheet'] }])],
|
||||
[REMOVED_APPS_KV_KEY, JSON.stringify(['camera'])],
|
||||
]);
|
||||
vi.stubGlobal('fetch', vi.fn(async url => ({
|
||||
json: async () => String(url).includes('/installedApps') ? installed : { recommended },
|
||||
})));
|
||||
vi.stubGlobal('puter', {
|
||||
authToken: 'test-session',
|
||||
kv: { get: async key => saved.get(key), set: vi.fn() },
|
||||
});
|
||||
onLaunch.mockClear();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
browser?._resizeObserver?.disconnect();
|
||||
$(document).off('keydown.myapps-keyboard');
|
||||
$(document).off('keydown.myapps-group');
|
||||
document.body.innerHTML = '';
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
async function mount (options = {}) {
|
||||
browser = createAppBrowser({ editable: false, onLaunch, ...options });
|
||||
document.body.innerHTML = `<main><section class="dashboard-section-apps active">${browser.html()}</section></main>`;
|
||||
$root = $('main');
|
||||
const container = document.querySelector('.myapps-container');
|
||||
Object.defineProperties(container, {
|
||||
clientWidth: { value: 600 },
|
||||
clientHeight: { value: 400 },
|
||||
});
|
||||
browser.init($root);
|
||||
await browser.loadApps($root);
|
||||
}
|
||||
|
||||
describe('shared app browser', () => {
|
||||
it('uses saved order, folders, and removed recommendations in read-only mode', async () => {
|
||||
await mount();
|
||||
expect(browser._apps.map(app => app.name)).toEqual(['notes', 'sheet', 'editor']);
|
||||
expect($('.myapps-group-tile')).toHaveLength(1);
|
||||
expect($('.myapps-reorder-btn, .myapps-add-tile')).toHaveLength(0);
|
||||
expect(fetch.mock.calls[0][1].headers.Authorization).toBe('Bearer test-session');
|
||||
});
|
||||
|
||||
it('searches inside folders and delegates launches without desktop actions', async () => {
|
||||
await mount();
|
||||
$('.myapps-search').val('spread').trigger('input');
|
||||
expect($('.myapps-tile')).toHaveLength(1);
|
||||
$('.myapps-tile').trigger('click');
|
||||
expect(onLaunch).toHaveBeenCalledWith({ appName: 'sheet', targetLink: '' });
|
||||
expect($('.window')).toHaveLength(0);
|
||||
$('.myapps-search').val('missing').trigger('input');
|
||||
expect($('.myapps-empty').text()).toBe('no_apps_match');
|
||||
expect(browser._page).toBe(0);
|
||||
expect($('.myapps-add-tile')).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('opens folders but cannot rename, reorder, remove, or persist changes', async () => {
|
||||
await mount();
|
||||
$('.myapps-group-tile').trigger('click');
|
||||
expect($('.myapps-group-name').prop('readOnly')).toBe(true);
|
||||
expect($('.myapps-group-panel .myapps-tile')).toHaveLength(2);
|
||||
$('.myapps-group-name').val('Changed').trigger('blur');
|
||||
browser._setReorderMode($root, true);
|
||||
$('.myapps-tile').first().trigger($.Event('pointerdown', { button: 0, pointerType: 'mouse' }));
|
||||
$('.myapps-tile').first().trigger('contextmenu');
|
||||
browser.saveOrder();
|
||||
browser.saveGroups();
|
||||
expect(browser._reorderMode).toBe(false);
|
||||
expect(browser._drag).toBeNull();
|
||||
expect(browser._groups[0].name).toBe('Work');
|
||||
expect(puter.kv.set).not.toHaveBeenCalled();
|
||||
$('.myapps-group-panel .myapps-tile').first().trigger('click');
|
||||
expect(onLaunch).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('retains the dashboard editing controls and isolates component state', async () => {
|
||||
await mount({ editable: true });
|
||||
expect($('.myapps-reorder-btn, .myapps-add-tile')).toHaveLength(2);
|
||||
browser._setReorderMode($root, true);
|
||||
expect(browser._reorderMode).toBe(true);
|
||||
const other = createAppBrowser({ editable: false });
|
||||
expect(other._apps).toBeNull();
|
||||
expect(other._groups).toEqual([]);
|
||||
expect(other._launchingApps).not.toBe(browser._launchingApps);
|
||||
});
|
||||
|
||||
it('keeps dashboard launches routed through desktop actions', async () => {
|
||||
const actions = {
|
||||
launchApp: vi.fn(async () => {}),
|
||||
beginTileLaunch: vi.fn(),
|
||||
settleTileLaunch: vi.fn(),
|
||||
};
|
||||
await mount({ editable: true, onLaunch: undefined, actions });
|
||||
$('.myapps-search').val('editor').trigger('input');
|
||||
$('.myapps-tile[data-app-name="editor"]').trigger('click');
|
||||
expect(actions.launchApp).toHaveBeenCalledWith({
|
||||
name: 'editor', maximized: true,
|
||||
window_options: { morph_from_dashboard_tile: true },
|
||||
});
|
||||
await vi.waitFor(() => expect(actions.settleTileLaunch).toHaveBeenCalledOnce());
|
||||
expect(browser._launchingApps.size).toBe(0);
|
||||
});
|
||||
|
||||
it('launches the app store from the dashboard add-app dialog', async () => {
|
||||
const actions = {
|
||||
launchApp: vi.fn(async () => {}),
|
||||
settleTileLaunch: vi.fn(),
|
||||
};
|
||||
puter.apps = { get: async name => ({ name, title: name }) };
|
||||
await mount({ editable: true, onLaunch: undefined, actions });
|
||||
$('.myapps-add-tile').trigger('click');
|
||||
$('.myapps-add-option[data-add-option="browse"]').trigger('click');
|
||||
await vi.waitFor(() => expect(actions.launchApp).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ name: 'app-center', maximized: true }),
|
||||
));
|
||||
expect($('.myapps-add-modal')).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('renders an empty list and an API failure without offering authentication', async () => {
|
||||
installed = [];
|
||||
recommended = [];
|
||||
await mount();
|
||||
expect($('.myapps-empty').text()).toBe('no_apps_installed');
|
||||
fetch.mockRejectedValue(new Error('unauthorized'));
|
||||
browser._apps = null;
|
||||
const log = vi.spyOn(console, 'error').mockImplementation(() => {});
|
||||
await browser.loadApps($root);
|
||||
expect($('.myapps-empty').text()).toBe('apps_load_failed');
|
||||
expect($('.window, form')).toHaveLength(0);
|
||||
log.mockRestore();
|
||||
});
|
||||
});
|
||||
+12
-3920
File diff suppressed because it is too large.
Load diff
@@ -29,7 +29,7 @@ import {
|
||||
storedTapHandler,
|
||||
tapRoute,
|
||||
} from '../../helpers/notificationTap.js';
|
||||
import { showLocalBanner, startPushWorker } from '../../helpers/pushWorker.js';
|
||||
import { startPushWorker } from '../../helpers/pushWorker.js';
|
||||
import { syncBrowserPush } from '../../helpers/pushSync.js';
|
||||
import { get_app_by_uid } from '../../helpers/getAppByUid.js';
|
||||
import launch_app from '../../helpers/launchApp.js';
|
||||
@@ -637,9 +637,6 @@ export default function UIDashboardNotifications ({ $el_window, socket }) {
|
||||
|
||||
const fresh = incoming.filter((entry) => ! surfaced.has(entry.uid));
|
||||
for ( const entry of fresh ) surfaced.add(entry.uid);
|
||||
for ( const entry of fresh ) {
|
||||
void showLocalBanner(entry, { replay, acknowledged: ! isUnread(entry) });
|
||||
}
|
||||
for ( const entry of result.added ) justAdded.add(entry.uid);
|
||||
render();
|
||||
announce(fresh);
|
||||
|
||||
@@ -61,7 +61,7 @@ import {
|
||||
setPushTapHandler,
|
||||
storedTapHandler,
|
||||
} from '../helpers/notificationTap.js';
|
||||
import { closeLocalBanner, showLocalBanner, startPushWorker } from '../helpers/pushWorker.js';
|
||||
import { closeLocalBanner, startPushWorker } from '../helpers/pushWorker.js';
|
||||
import { syncBrowserPush } from '../helpers/pushSync.js';
|
||||
import { get_app_by_uid } from '../helpers/getAppByUid.js';
|
||||
import { httpsIconFor, notificationTarget, senderNameFor } from './Dashboard/notificationCenter.js';
|
||||
@@ -289,7 +289,6 @@ async function UIDesktop (options) {
|
||||
* row isn't marked twice.
|
||||
*/
|
||||
const show_notification = ({ uid, notification, app_uid }, { replay = false } = {}) => {
|
||||
void showLocalBanner({ uid, notification }, { replay });
|
||||
// A notification can be re-sent under its own uid when what it says has
|
||||
// grown — several people sharing with you is one notification that
|
||||
// counts them. Refresh the one on screen rather than stacking a copy.
|
||||
|
||||
@@ -704,12 +704,12 @@ async function UIWindow (options) {
|
||||
attach_dashboard_app_drawer(el_window, options);
|
||||
}
|
||||
|
||||
// Let the Apps tab refresh its tiles' running dots (see TabApps).
|
||||
// Let the Apps tab refresh its tiles' running dots (see AppBrowser.js).
|
||||
if ( window.is_dashboard_mode && options.app ) {
|
||||
document.dispatchEvent(new CustomEvent('dashboard-app-windows-changed'));
|
||||
}
|
||||
|
||||
// A launch from a dashboard Apps-tab tile (TabApps passes
|
||||
// A launch from a dashboard Apps-tab tile (AppBrowser.js passes
|
||||
// morph_from_dashboard_tile) morphs the tile's icon into the opening
|
||||
// window — the reverse of hideWindow's minimize morph. The window is
|
||||
// already appended, displayed, and at its final (maximized) geometry
|
||||
@@ -2882,7 +2882,7 @@ function delete_window_element (el_window) {
|
||||
window.window_counter = 0;
|
||||
}
|
||||
// Every close path funnels through here — after the element is gone,
|
||||
// let the Apps tab turn the app tile's running dot off (see TabApps).
|
||||
// let the Apps tab turn the app tile's running dot off (see AppBrowser.js).
|
||||
if ( window.is_dashboard_mode && was_app ) {
|
||||
document.dispatchEvent(new CustomEvent('dashboard-app-windows-changed'));
|
||||
}
|
||||
@@ -4664,7 +4664,7 @@ function dashboard_rendered_app_icon (app_name) {
|
||||
// -- Drawer intro exposure decay --
|
||||
// The drawer's opening flash (see attach_dashboard_app_drawer) is pedagogy:
|
||||
// it teaches that the app's controls live in the tongue. Like the
|
||||
// dashboard's deep-link intro (TabApps.js), the lesson decays — after this
|
||||
// dashboard's deep-link intro (AppBrowser.js), the lesson decays — after this
|
||||
// many deliveries, new windows keep the bare tongue and the flash stops
|
||||
// occluding the app's top edge. A delivery is counted once per window:
|
||||
// either the flash played while the user could see it, or the user opened
|
||||
@@ -4969,7 +4969,7 @@ function attach_dashboard_app_drawer (el_window, options) {
|
||||
* Tiles whose click feedback has played (or is playing) for the launch
|
||||
* currently in flight. A fresh app launch has a server round-trip between
|
||||
* the click and the window's creation; so the click is acknowledged
|
||||
* instantly, TabApps plays the icon half of the open morph immediately
|
||||
* instantly, AppBrowser.js plays the icon half of the open morph immediately
|
||||
* (begin_dashboard_tile_launch) and the mark here tells the window's half
|
||||
* not to replay it when the window finally opens (morph_window_from_tile).
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* 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 jQuery from './lib/jquery-3.6.1/jquery-3.6.1.min.js';
|
||||
import { encode } from 'html-entities';
|
||||
import { loadLocale } from './i18n/embedI18n.js';
|
||||
import { createAppBrowser } from './UI/Dashboard/AppBrowser.js';
|
||||
import { appTileLink } from './UI/Dashboard/appLink.js';
|
||||
import { installAppIconFallback } from './helpers/appIcon.js';
|
||||
|
||||
window.$ = window.jQuery = jQuery;
|
||||
window.html_encode = value => encode(String(value ?? ''), { mode: 'nonAsciiPrintable' });
|
||||
window.icons = {
|
||||
'app.svg': new URL('./icons/app.svg', import.meta.url).href,
|
||||
'app-default.svg': new URL('./icons/app-default.svg', import.meta.url).href,
|
||||
};
|
||||
|
||||
const root = document.querySelector('#app-browser');
|
||||
const config = JSON.parse(document.querySelector('#app-browser-config').textContent);
|
||||
window.api_origin = config.apiOrigin;
|
||||
window.PUTER_API_ORIGIN = config.apiOrigin;
|
||||
window.PUTER_GUI_ORIGIN = window.location.origin;
|
||||
window.gui_origin = window.location.origin;
|
||||
window.puter_gui_enabled = true;
|
||||
window.puter_socket_enabled = false;
|
||||
window.locale = 'en';
|
||||
try {
|
||||
window.locale = JSON.parse(localStorage.getItem('user_preferences') || '{}')?.language || 'en';
|
||||
} catch { /* Corrupt preferences must not prevent loading apps. */ }
|
||||
const localeReady = loadLocale(window.locale);
|
||||
|
||||
async function mount () {
|
||||
window.auth_token = localStorage.getItem('auth_token_v2');
|
||||
if ( ! window.auth_token ) throw new Error('Missing session');
|
||||
|
||||
await Promise.all([new Promise((resolve, reject) => {
|
||||
const script = document.createElement('script');
|
||||
script.src = config.sdkUrl;
|
||||
script.onload = resolve;
|
||||
script.onerror = reject;
|
||||
document.head.appendChild(script);
|
||||
}), localeReady]);
|
||||
puter.setAPIOrigin(config.apiOrigin);
|
||||
installAppIconFallback();
|
||||
|
||||
const browser = createAppBrowser({
|
||||
editable: false,
|
||||
onLaunch: app => {
|
||||
const href = appTileLink(app, window.location.origin);
|
||||
if ( href ) window.open(href, '_blank', 'noopener,noreferrer');
|
||||
},
|
||||
});
|
||||
const $root = $(root);
|
||||
$root.html(`<section class="dashboard-section dashboard-section-apps active">${browser.html()}</section>`);
|
||||
browser.init($root);
|
||||
}
|
||||
|
||||
mount().catch(async () => {
|
||||
await localeReady;
|
||||
root.innerHTML = `<p class="myapps-empty" role="status">${i18n('app_browser_unavailable')}</p>`;
|
||||
});
|
||||
@@ -0,0 +1,31 @@
|
||||
html,
|
||||
body {
|
||||
margin: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
overflow: hidden;
|
||||
font-family: "Inter", "Helvetica Neue", Helvetica, Arial, sans-serif;
|
||||
color: var(--dashboard-text);
|
||||
background: var(--dashboard-background);
|
||||
}
|
||||
|
||||
#app-browser {
|
||||
display: block;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
#app-browser .dashboard-section-apps {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
/* The dashboard's narrow-screen padding clears its hamburger button, which embeds don't have. */
|
||||
#app-browser .myapps-search-wrap {
|
||||
padding-top: 16px;
|
||||
padding-bottom: 16px;
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
#app-browser .myapps-group-name[readonly] {
|
||||
cursor: default;
|
||||
}
|
||||
@@ -950,7 +950,7 @@ input.myapps-search::-webkit-search-decoration {
|
||||
appearance: none;
|
||||
}
|
||||
|
||||
/* Frozen while reorder mode is on (see TabApps._setReorderMode). */
|
||||
/* Frozen while reorder mode is on (see _setReorderMode in AppBrowser.js). */
|
||||
input.myapps-search:disabled {
|
||||
opacity: 0.55;
|
||||
}
|
||||
@@ -1016,7 +1016,7 @@ input.myapps-search:disabled {
|
||||
}
|
||||
|
||||
.myapps-container {
|
||||
/* Pager geometry is inherited from .myapps-tab; TabApps.js reads it here
|
||||
/* Pager geometry is inherited from .myapps-tab; AppBrowser.js reads it here
|
||||
(computeLayout) to size pages. */
|
||||
flex: 1;
|
||||
min-height: 0;
|
||||
@@ -1294,7 +1294,7 @@ input.myapps-search:disabled {
|
||||
|
||||
/* Uninstall: the tile shrinks away in place, then the surviving tiles
|
||||
FLIP-slide into the closed-up grid (see showUninstallModal). Duration is
|
||||
mirrored by TILE_REMOVE_ANIM_MS in TabApps.js. */
|
||||
mirrored by TILE_REMOVE_ANIM_MS in AppBrowser.js. */
|
||||
.myapps-tile.myapps-tile-removing {
|
||||
transition: transform 0.18s ease-in, opacity 0.18s ease-in;
|
||||
transform: scale(0.5);
|
||||
@@ -1414,7 +1414,7 @@ input.myapps-search:disabled {
|
||||
}
|
||||
|
||||
/* Pending: the well fills over the dwell the drop is waiting out
|
||||
(DRAG_MERGE_DWELL_MS in TabApps.js) — for the ordinary gesture (carry the
|
||||
(DRAG_MERGE_DWELL_MS in AppBrowser.js) — for the ordinary gesture (carry the
|
||||
icon over, stop) the fill IS the countdown, so leaving before it completes
|
||||
is an informed choice rather than a surprise. */
|
||||
body.myapps-reordering .myapps-tile-merge-pending::before {
|
||||
@@ -1491,7 +1491,7 @@ body.myapps-reordering .myapps-tile.myapps-tile-merge-armed .myapps-tile-icon {
|
||||
opacity: 0;
|
||||
transform: scale(0.92);
|
||||
transform-origin: center;
|
||||
/* Mirrors GROUP_PANEL_OPEN_MS / GROUP_PANEL_CLOSE_MS in TabApps.js; the
|
||||
/* Mirrors GROUP_PANEL_OPEN_MS / GROUP_PANEL_CLOSE_MS in AppBrowser.js; the
|
||||
curve is the window morph's, so a folder opening and an app opening
|
||||
feel like the same gesture at two scales. */
|
||||
transition: transform 0.24s cubic-bezier(0.32, 0.72, 0, 1), opacity 0.2s ease;
|
||||
@@ -4597,7 +4597,7 @@ select.share-modal-row-mode:focus {
|
||||
/* A tile installed by a deep-link landing, not yet arrived: rendered in
|
||||
place — layout, page count, and the wayfinding flip all see it — but held
|
||||
invisible for the intro's arrival beat to reveal (see _spliceDeepLinkApp /
|
||||
_revealArrivingTile in TabApps.js). */
|
||||
_revealArrivingTile in AppBrowser.js). */
|
||||
.myapps-tile-installing {
|
||||
opacity: 0;
|
||||
transform: scale(0.55);
|
||||
@@ -4605,7 +4605,7 @@ select.share-modal-row-mode:focus {
|
||||
}
|
||||
|
||||
/* The arrival itself: an INSTALLATION, staged over 1400ms (mirrored by
|
||||
DEEP_LINK_INSTALL_ARRIVE_MS in TabApps.js) with the grammar users know
|
||||
DEEP_LINK_INSTALL_ARRIVE_MS in AppBrowser.js) with the grammar users know
|
||||
from every app store:
|
||||
1. the slot opens — the folder well's own surface — and the icon
|
||||
materializes into it DIM (grayscale, translucent, slightly small):
|
||||
@@ -4750,7 +4750,7 @@ body.myapps-reordering .myapps-tile {
|
||||
touch-action: none;
|
||||
}
|
||||
|
||||
/* -- Reorder mode (touch; see TabApps._setReorderMode) --
|
||||
/* -- Reorder mode (touch; see _setReorderMode in AppBrowser.js) --
|
||||
touch-action must be locked down BEFORE the finger goes down — the browser
|
||||
reads it at gesture start, so the mid-drag body.myapps-reordering rule
|
||||
above can't stop a touch the scroller has already claimed. While the mode
|
||||
@@ -9092,7 +9092,7 @@ body.dashboard-mode .window-dashboard-headless .window-body-app {
|
||||
|
||||
/* Running dot: marks Apps-tab tiles whose app has a live window (visible or
|
||||
minimized), macOS-dock style — sits centered in the gap between the 56px
|
||||
icon and the label. Kept/removed by updateRunningDots in TabApps.js. */
|
||||
icon and the label. Kept/removed by updateRunningDots in AppBrowser.js. */
|
||||
.myapps-tile {
|
||||
position: relative;
|
||||
}
|
||||
|
||||
@@ -47,13 +47,13 @@ const receiptLog = (sdk) =>
|
||||
export const notificationSocketAcks = (sdk = globalThis.puter) =>
|
||||
receiptLog(sdk) ? ['notif.receipt'] : [];
|
||||
|
||||
/** Notes each live `notif.message` in the receipt log and answers the server's asks from it. */
|
||||
/** Notes each live `notif.message` in the receipt log, reporting over `socket` one the desktop handled, and answers the server's asks from it. */
|
||||
export const answerNotificationReceipts = (socket, sdk = globalThis.puter) => {
|
||||
const log = receiptLog(sdk);
|
||||
if ( ! log ) return;
|
||||
socket.on('notif.message', (message) => {
|
||||
if ( typeof message?.uid !== 'string' ) return;
|
||||
if ( ! isAppOnlyNotification(message.notification) ) log._noteReceived(message.uid);
|
||||
if ( ! isAppOnlyNotification(message.notification) ) log._noteReceived(message.uid, undefined, socket);
|
||||
});
|
||||
socket.on('notif.receipt', (data, ack) => log._answerReceipt(data, ack));
|
||||
};
|
||||
|
||||
@@ -407,7 +407,7 @@ describe('notification receipts', () => {
|
||||
expect(notificationSocketAcks(undefined)).toEqual([]);
|
||||
});
|
||||
|
||||
it('notes each live notif.message and answers asks from the log', () => {
|
||||
it('notes each live notif.message with the socket it came on and answers asks from the log', () => {
|
||||
const sdk = sdkWithLog();
|
||||
const socket = fakeSocket();
|
||||
answerNotificationReceipts(socket, sdk);
|
||||
@@ -416,7 +416,7 @@ describe('notification receipts', () => {
|
||||
const ack = vi.fn();
|
||||
socket.handlers['notif.receipt'][0]({ uid: 'n-1' }, ack);
|
||||
|
||||
expect(sdk.notifications._noteReceived).toHaveBeenCalledWith('n-1');
|
||||
expect(sdk.notifications._noteReceived).toHaveBeenCalledWith('n-1', undefined, socket);
|
||||
expect(sdk.notifications._answerReceipt).toHaveBeenCalledWith({ uid: 'n-1' }, ack);
|
||||
});
|
||||
|
||||
@@ -429,7 +429,7 @@ describe('notification receipts', () => {
|
||||
socket.handlers['notif.message'][0]({ uid: 'n-3', notification: { type: 'share.received' } });
|
||||
|
||||
expect(sdk.notifications._noteReceived).toHaveBeenCalledTimes(1);
|
||||
expect(sdk.notifications._noteReceived).toHaveBeenCalledWith('n-3');
|
||||
expect(sdk.notifications._noteReceived).toHaveBeenCalledWith('n-3', undefined, socket);
|
||||
});
|
||||
|
||||
it('registers nothing with a puter.js that has no log', () => {
|
||||
|
||||
@@ -29,7 +29,7 @@ import { is_window_on_screen, user_facing_windows } from './windowVisibility.js'
|
||||
// Files whose app launch is still in flight, by file uid. Between the click
|
||||
// and the window's creation there is nothing in the DOM to restore, so a
|
||||
// re-click during that stretch would mint a duplicate instance — mark the
|
||||
// launch and swallow re-clicks instead (same idea as TabApps._launchingApps).
|
||||
// launch and swallow re-clicks instead (same idea as _launchingApps in AppBrowser.js).
|
||||
// Entries carry a timestamp so a launch that dies without settling (an
|
||||
// unexpected throw before launch_app is reached) can't swallow clicks forever.
|
||||
const launching_file_uids = new Map();
|
||||
|
||||
@@ -17,11 +17,9 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
// The page's side of `/sw.js`: registration, routed taps, hidden-tab banners.
|
||||
// The page's side of `/sw.js`: routed taps, and updates once push is on.
|
||||
|
||||
import { httpsIconFor, isShareNotification } from '../UI/Dashboard/notificationCenter.js';
|
||||
import {
|
||||
APP_MESSAGE_TYPE,
|
||||
dispatchPushTap,
|
||||
normalizePushTap,
|
||||
pushTapFromParams,
|
||||
@@ -34,7 +32,6 @@ import { webPushClient } from './webPushClient.js';
|
||||
export const WORKER_URL = '/sw.js';
|
||||
export const WORKER_CLICK_MESSAGE = 'puter-notification-click';
|
||||
export const WORKER_CLICK_ACK_MESSAGE = 'puter-notification-click-ack';
|
||||
export const WORKER_SHOW_MESSAGE = 'puter-show-notification';
|
||||
|
||||
/**
|
||||
* @param {Object} [params] - `window.gui_params`
|
||||
@@ -46,91 +43,6 @@ export const pushWorkerEnabled = (params, nav) => (
|
||||
&& typeof nav?.serviceWorker?.register === 'function'
|
||||
);
|
||||
|
||||
/**
|
||||
* @param {{ replay?: boolean, acknowledged?: boolean, notification?: Object,
|
||||
* visibilityState?: string, permission?: string }} input
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export const shouldShowLocalBanner = ({ replay = false, acknowledged = false, notification, visibilityState, permission }) => (
|
||||
! replay
|
||||
&& ! acknowledged
|
||||
&& visibilityState === 'hidden'
|
||||
&& permission === 'granted'
|
||||
&& !! notification
|
||||
&& typeof notification === 'object'
|
||||
&& notification.silent !== true
|
||||
);
|
||||
|
||||
const nonEmpty = (value) => (typeof value === 'string' && value.trim() !== '' ? value : undefined);
|
||||
|
||||
/**
|
||||
* @param {string} uid
|
||||
* @param {Object} notification
|
||||
* @param {string|null} [appUid] - The app the row names; wins over the value's
|
||||
* @returns {Object} The push payload shape `sw.js` shows
|
||||
*/
|
||||
export const pushMessageFor = (uid, notification, appUid = null) => {
|
||||
const group = nonEmpty(notification.group);
|
||||
const message = { uid, group: group ?? uid };
|
||||
if ( notification.type === APP_MESSAGE_TYPE ) {
|
||||
const app = nonEmpty(appUid) ?? nonEmpty(notification.app?.uid);
|
||||
// An app's group is its own, as the server names it on a push.
|
||||
if ( app && group ) message.group = `${app}:${group}`;
|
||||
if ( notification.sensitive === true ) {
|
||||
return { ...message, title: nonEmpty(notification.app?.name), ...(app ? { target: { app } } : {}) };
|
||||
}
|
||||
const path = nonEmpty(notification.target);
|
||||
if ( app ) message.target = { app, ...(path ? { path } : {}) };
|
||||
if ( notification.data !== undefined ) message.data = notification.data;
|
||||
} else if ( isShareNotification(notification) ) {
|
||||
const path = nonEmpty(notification.fields?.target?.path);
|
||||
if ( path ) message.target = { path };
|
||||
}
|
||||
const body = nonEmpty(notification.text);
|
||||
const icon = httpsIconFor(notification);
|
||||
return {
|
||||
...message,
|
||||
title: nonEmpty(notification.title),
|
||||
...(body ? { body } : {}),
|
||||
...(icon ? { icon } : {}),
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Only while push is on in this browser, and only for Puter's own notifications: an app's message is shown by the app's page while it is open, and by the app's own push while it is closed.
|
||||
* @param {{ uid: string, notification: Object }} item
|
||||
* @param {{ replay?: boolean, acknowledged?: boolean }} [meta]
|
||||
* @param {{ params?: Object, doc?: Document, nav?: Navigator, NotificationApi?: { permission: string } }} [env]
|
||||
* @returns {Promise<boolean>} Whether one was handed to the worker
|
||||
*/
|
||||
export const showLocalBanner = async ({ uid, notification }, { replay = false, acknowledged = false } = {}, {
|
||||
params = globalThis.gui_params,
|
||||
doc = globalThis.document,
|
||||
nav = globalThis.navigator,
|
||||
NotificationApi = globalThis.Notification,
|
||||
} = {}) => {
|
||||
if ( ! pushWorkerEnabled(params, nav) ) return false;
|
||||
if ( notification?.type === APP_MESSAGE_TYPE ) return false;
|
||||
if ( ! shouldShowLocalBanner({
|
||||
replay,
|
||||
acknowledged,
|
||||
notification,
|
||||
visibilityState: doc?.visibilityState,
|
||||
permission: NotificationApi?.permission,
|
||||
}) ) return false;
|
||||
try {
|
||||
const registration = await nav.serviceWorker.getRegistration?.('/');
|
||||
const worker = registration?.active;
|
||||
if ( ! worker ) return false;
|
||||
if ( ! await registration.pushManager?.getSubscription() ) return false;
|
||||
worker.postMessage({ type: WORKER_SHOW_MESSAGE, puter: pushMessageFor(uid, notification) });
|
||||
return true;
|
||||
} catch ( err ) {
|
||||
console.warn('Could not show a browser notification:', err);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
let started = false;
|
||||
|
||||
/**
|
||||
@@ -164,11 +76,15 @@ export const startPushWorker = async ({
|
||||
event.source?.postMessage?.({ type: WORKER_CLICK_ACK_MESSAGE, id: event.data.id });
|
||||
dispatchPushTap(event.data.notification);
|
||||
});
|
||||
// Turning push on registers the worker. One left without a subscription
|
||||
// goes, so nothing but push installs it.
|
||||
try {
|
||||
// Scripts the worker imports are fetched fresh on each update check.
|
||||
await nav.serviceWorker.register(WORKER_URL, { scope: '/', updateViaCache: 'none' });
|
||||
const registration = await nav.serviceWorker.getRegistration?.('/');
|
||||
if ( ! registration ) return;
|
||||
if ( await registration.pushManager?.getSubscription() ) await registration.update?.();
|
||||
else await registration.unregister();
|
||||
} catch ( err ) {
|
||||
console.warn('Could not register the service worker:', err);
|
||||
console.warn('Could not update the service worker:', err);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -18,175 +18,13 @@
|
||||
*/
|
||||
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import {
|
||||
closeLocalBanner,
|
||||
pushMessageFor,
|
||||
pushWorkerEnabled,
|
||||
shouldShowLocalBanner,
|
||||
showLocalBanner,
|
||||
} from './pushWorker.js';
|
||||
import { closeLocalBanner, pushWorkerEnabled } from './pushWorker.js';
|
||||
|
||||
afterEach(() => {
|
||||
vi.resetModules();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
const share = {
|
||||
type: 'share.received',
|
||||
title: 'alice shared a file with you',
|
||||
icon: 'shared.svg',
|
||||
fields: { target: { path: '/alice/Shared/a.txt', name: 'a.txt' } },
|
||||
};
|
||||
|
||||
describe('shouldShowLocalBanner', () => {
|
||||
const live = { notification: share, visibilityState: 'hidden', permission: 'granted' };
|
||||
|
||||
it('shows one for a live arrival in a hidden tab with permission', () => {
|
||||
expect(shouldShowLocalBanner(live)).toBe(true);
|
||||
});
|
||||
|
||||
it('shows none while the tab is visible', () => {
|
||||
expect(shouldShowLocalBanner({ ...live, visibilityState: 'visible' })).toBe(false);
|
||||
});
|
||||
|
||||
it('shows none for a replay, an acknowledged or a silent one', () => {
|
||||
expect(shouldShowLocalBanner({ ...live, replay: true })).toBe(false);
|
||||
expect(shouldShowLocalBanner({ ...live, acknowledged: true })).toBe(false);
|
||||
expect(shouldShowLocalBanner({ ...live, notification: { ...share, silent: true } })).toBe(false);
|
||||
});
|
||||
|
||||
it('shows none without permission granted', () => {
|
||||
expect(shouldShowLocalBanner({ ...live, permission: 'default' })).toBe(false);
|
||||
expect(shouldShowLocalBanner({ ...live, permission: 'denied' })).toBe(false);
|
||||
expect(shouldShowLocalBanner({ ...live, permission: undefined })).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('pushMessageFor', () => {
|
||||
it('targets the app and path an app message names, with its data and https icon', () => {
|
||||
expect(pushMessageFor('n1', {
|
||||
type: 'app.message',
|
||||
title: 'New reply',
|
||||
text: 'see you',
|
||||
icon: 'https://example.com/i.png',
|
||||
target: '/rooms/7',
|
||||
data: { room: 7 },
|
||||
group: 'room-7',
|
||||
app: { uid: 'app-1', name: 'Chat' },
|
||||
})).toEqual({
|
||||
uid: 'n1',
|
||||
group: 'app-1:room-7',
|
||||
title: 'New reply',
|
||||
body: 'see you',
|
||||
icon: 'https://example.com/i.png',
|
||||
target: { app: 'app-1', path: '/rooms/7' },
|
||||
data: { room: 7 },
|
||||
});
|
||||
});
|
||||
|
||||
it('shows only the app name for a sensitive app message', () => {
|
||||
expect(pushMessageFor('n1', {
|
||||
type: 'app.message',
|
||||
title: 'Code 1234',
|
||||
text: 'secret',
|
||||
sensitive: true,
|
||||
app: { uid: 'app-1', name: 'Bank' },
|
||||
})).toEqual({ uid: 'n1', group: 'n1', title: 'Bank', target: { app: 'app-1' } });
|
||||
});
|
||||
|
||||
it('points a share at the item it names and drops a non-https icon', () => {
|
||||
expect(pushMessageFor('n2', share)).toEqual({
|
||||
uid: 'n2',
|
||||
group: 'n2',
|
||||
title: 'alice shared a file with you',
|
||||
target: { path: '/alice/Shared/a.txt' },
|
||||
});
|
||||
});
|
||||
|
||||
it("tags an app message's group with its app, so two apps never share one", () => {
|
||||
const message = (app, over = {}) => ({ type: 'app.message', title: 't', group: 'room-7', app: { uid: app }, ...over });
|
||||
expect(pushMessageFor('n1', message('app-1')).group).toBe('app-1:room-7');
|
||||
expect(pushMessageFor('n1', message('app-2')).group).toBe('app-2:room-7');
|
||||
expect(pushMessageFor('n1', message('app-1', { sensitive: true })).group).toBe('app-1:room-7');
|
||||
expect(pushMessageFor('n1', message('app-b'), 'app-a').group).toBe('app-a:room-7');
|
||||
expect(pushMessageFor('n1', message('app-1', { group: undefined })).group).toBe('n1');
|
||||
expect(pushMessageFor('n2', { ...share, group: 'room-7' }).group).toBe('room-7');
|
||||
});
|
||||
|
||||
it("uses the row's app uid over the one the message names", () => {
|
||||
expect(pushMessageFor('n1', { type: 'app.message', title: 't' }, 'app-9').target).toEqual({ app: 'app-9' });
|
||||
expect(pushMessageFor('n1', { type: 'app.message', title: 't', app: { uid: 'app-b' } }, 'app-a').target)
|
||||
.toEqual({ app: 'app-a' });
|
||||
expect(pushMessageFor('n1', { type: 'app.message', title: 't', app: { uid: 'app-b' } }, '').target)
|
||||
.toEqual({ app: 'app-b' });
|
||||
});
|
||||
});
|
||||
|
||||
describe('showLocalBanner', () => {
|
||||
const env = ({
|
||||
visibilityState = 'hidden',
|
||||
permission = 'granted',
|
||||
active = { postMessage: vi.fn() },
|
||||
pushEnabled = true,
|
||||
subscribed = true,
|
||||
} = {}) => ({
|
||||
params: { pushEnabled },
|
||||
doc: { visibilityState },
|
||||
NotificationApi: { permission },
|
||||
nav: {
|
||||
serviceWorker: {
|
||||
register: vi.fn(),
|
||||
getRegistration: vi.fn(async () => (active
|
||||
? { active, pushManager: { getSubscription: async () => (subscribed ? {} : null) } }
|
||||
: undefined)),
|
||||
},
|
||||
},
|
||||
active,
|
||||
});
|
||||
|
||||
it('hands one notification to the worker, tagged by its uid', async () => {
|
||||
const e = env();
|
||||
expect(await showLocalBanner({ uid: 'n1', notification: share }, {}, e)).toBe(true);
|
||||
expect(e.nav.serviceWorker.getRegistration).toHaveBeenCalledWith('/');
|
||||
expect(e.active.postMessage).toHaveBeenCalledOnce();
|
||||
expect(e.active.postMessage).toHaveBeenCalledWith({
|
||||
type: 'puter-show-notification',
|
||||
puter: pushMessageFor('n1', share),
|
||||
});
|
||||
});
|
||||
|
||||
it('hands nothing over while visible, for a replay, or without a worker', async () => {
|
||||
const visible = env({ visibilityState: 'visible' });
|
||||
expect(await showLocalBanner({ uid: 'n1', notification: share }, {}, visible)).toBe(false);
|
||||
expect(visible.nav.serviceWorker.getRegistration).not.toHaveBeenCalled();
|
||||
|
||||
const replay = env();
|
||||
expect(await showLocalBanner({ uid: 'n1', notification: share }, { replay: true }, replay)).toBe(false);
|
||||
expect(replay.active.postMessage).not.toHaveBeenCalled();
|
||||
|
||||
expect(await showLocalBanner({ uid: 'n1', notification: share }, {}, env({ active: null }))).toBe(false);
|
||||
});
|
||||
|
||||
it('hands nothing over while push is off in this browser', async () => {
|
||||
const off = env({ subscribed: false });
|
||||
expect(await showLocalBanner({ uid: 'n1', notification: share }, {}, off)).toBe(false);
|
||||
expect(off.active.postMessage).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("hands nothing over for an app's message, even one from an app that may push", async () => {
|
||||
const e = { ...env(), holdsPush: vi.fn(async () => true) };
|
||||
const message = { type: 'app.message', title: 'Hi', text: 'see you', app: { uid: 'app-1', name: 'Chat' } };
|
||||
expect(await showLocalBanner({ uid: 'n1', notification: message, appUid: 'app-1' }, {}, e)).toBe(false);
|
||||
expect(e.active.postMessage).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('hands nothing over where push is off for the deployment', async () => {
|
||||
const off = env({ pushEnabled: false });
|
||||
expect(await showLocalBanner({ uid: 'n1', notification: share }, {}, off)).toBe(false);
|
||||
expect(off.nav.serviceWorker.getRegistration).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('pushWorkerEnabled', () => {
|
||||
it('needs the deployment flag and a browser with service workers', () => {
|
||||
const nav = { serviceWorker: { register: () => {} } };
|
||||
@@ -200,13 +38,14 @@ describe('pushWorkerEnabled', () => {
|
||||
describe('startPushWorker', () => {
|
||||
const UID = '0b6a2c1e-6f1d-4a57-9c1e-3d2f8a9b7c10';
|
||||
|
||||
const makeNav = (registrations = []) => {
|
||||
/** `registration`: the `/sw.js` already installed, if any. */
|
||||
const makeNav = (registration = null) => {
|
||||
const listeners = {};
|
||||
return {
|
||||
listeners,
|
||||
serviceWorker: {
|
||||
register: vi.fn(async () => ({})),
|
||||
getRegistrations: vi.fn(async () => registrations),
|
||||
getRegistration: vi.fn(async () => registration),
|
||||
addEventListener: vi.fn((type, fn) => {
|
||||
listeners[type] = fn;
|
||||
}),
|
||||
@@ -214,6 +53,12 @@ describe('startPushWorker', () => {
|
||||
};
|
||||
};
|
||||
|
||||
const installed = (subscription) => ({
|
||||
pushManager: { getSubscription: vi.fn(async () => subscription) },
|
||||
update: vi.fn(async () => {}),
|
||||
unregister: vi.fn(async () => true),
|
||||
});
|
||||
|
||||
const load = async () => {
|
||||
const worker = await import('./pushWorker.js');
|
||||
const tap = await import('./notificationTap.js');
|
||||
@@ -228,13 +73,14 @@ describe('startPushWorker', () => {
|
||||
...over,
|
||||
});
|
||||
|
||||
it('registers /sw.js at the root scope, acks its taps and routes them', async () => {
|
||||
it('registers no worker before push is turned on, acks its taps and routes them', async () => {
|
||||
const mod = await load();
|
||||
const handler = vi.fn();
|
||||
mod.setPushTapHandler(handler);
|
||||
const nav = makeNav();
|
||||
await start(mod, { nav });
|
||||
expect(nav.serviceWorker.register).toHaveBeenCalledWith('/sw.js', { scope: '/', updateViaCache: 'none' });
|
||||
expect(nav.serviceWorker.register).not.toHaveBeenCalled();
|
||||
expect(nav.serviceWorker.getRegistration).toHaveBeenCalledWith('/');
|
||||
|
||||
const source = { postMessage: vi.fn() };
|
||||
nav.listeners.message({ data: { type: 'other', notification: { uid: UID } }, source });
|
||||
@@ -254,11 +100,25 @@ describe('startPushWorker', () => {
|
||||
});
|
||||
|
||||
it('leaves an existing /sw.js alone where push is off, and registers nothing', async () => {
|
||||
const puter = { active: { scriptURL: 'https://puter.test/sw.js' }, unregister: vi.fn(async () => true) };
|
||||
const nav = makeNav([puter]);
|
||||
const worker = installed(null);
|
||||
const nav = makeNav(worker);
|
||||
await start(await load(), { nav, params: { pushEnabled: false } });
|
||||
expect(nav.serviceWorker.register).not.toHaveBeenCalled();
|
||||
expect(puter.unregister).not.toHaveBeenCalled();
|
||||
expect(worker.unregister).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('updates the worker this browser subscribed through', async () => {
|
||||
const worker = installed({ endpoint: 'https://push.example/1' });
|
||||
await start(await load(), { nav: makeNav(worker) });
|
||||
expect(worker.update).toHaveBeenCalled();
|
||||
expect(worker.unregister).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('removes a worker left without a subscription', async () => {
|
||||
const worker = installed(null);
|
||||
await start(await load(), { nav: makeNav(worker) });
|
||||
expect(worker.unregister).toHaveBeenCalled();
|
||||
expect(worker.update).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('hands over the notification uid a page was opened for and clears it', async () => {
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* 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 en from './translations/en.js';
|
||||
import { installI18n } from './i18nCore.js';
|
||||
|
||||
installI18n({ en });
|
||||
|
||||
/**
|
||||
* Loads only the preferred language, so embeds don't ship every translation.
|
||||
* Unknown or unavailable languages keep English.
|
||||
* @param {string} locale
|
||||
*/
|
||||
export async function loadLocale (locale) {
|
||||
if ( typeof locale !== 'string' || locale === 'en' || ! /^[a-z]+$/.test(locale) ) return;
|
||||
try {
|
||||
const { default: language } = await import(
|
||||
/* webpackExclude: /translations\.js$/ */ `./translations/${locale}.js`
|
||||
);
|
||||
installI18n({ en, [locale]: language });
|
||||
} catch {
|
||||
// English is already installed.
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import { beforeAll, describe, expect, it } from 'vitest';
|
||||
|
||||
beforeAll(() => {
|
||||
globalThis.window = globalThis;
|
||||
globalThis.html_encode = value => String(value);
|
||||
});
|
||||
|
||||
describe('embed translations', () => {
|
||||
it('starts in English and loads only the requested language', async () => {
|
||||
const { loadLocale } = await import('./embedI18n.js');
|
||||
window.locale = 'de';
|
||||
expect(i18n('close')).toBe('Close');
|
||||
await loadLocale('de');
|
||||
expect(i18n('close')).toBe('Schließen');
|
||||
expect(i18n('app_browser_unavailable')).toBe('Apps are unavailable. Please reopen this browser from your app.');
|
||||
});
|
||||
|
||||
it('keeps English for unknown or unsafe language values', async () => {
|
||||
const { loadLocale } = await import('./embedI18n.js');
|
||||
for ( const locale of ['xx', '../translations', 'en', null] ) {
|
||||
window.locale = locale;
|
||||
await loadLocale(locale);
|
||||
expect(i18n('close')).toBe('Close');
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -18,75 +18,10 @@
|
||||
*/
|
||||
|
||||
import translations from './translations/translations.js';
|
||||
import { installI18n } from './i18nCore.js';
|
||||
|
||||
window.listSupportedLanguages = () => Object.keys(translations).map(lang => translations[lang]);
|
||||
|
||||
const variables = {
|
||||
docs: 'https://docs.puter.com/',
|
||||
terms: 'https://puter.com/terms',
|
||||
privacy: 'https://puter.com/privacy',
|
||||
};
|
||||
installI18n(translations);
|
||||
|
||||
function ReplacePlaceholders (str, arg_variables = {}) {
|
||||
const all_variables = { ...variables, ...arg_variables };
|
||||
str = str.replace(/{{link=(.*?)}}(.*?){{\/link}}/g, (_, key, text) => `<a href="${all_variables[key]}" target="_blank">${text}</a>`);
|
||||
str = str.replace(/{{(.*?)}}/g, (_, key) => all_variables[key]);
|
||||
return str;
|
||||
}
|
||||
|
||||
window.i18n = function (key, replacements = [], encode_html = true) {
|
||||
let arg_variables = {};
|
||||
if ( Array.isArray(replacements) === false ) {
|
||||
if ( typeof replacements === 'object' ) {
|
||||
arg_variables = replacements;
|
||||
replacements = [];
|
||||
} else {
|
||||
replacements = [replacements];
|
||||
}
|
||||
}
|
||||
|
||||
let language = translations[window.locale] ?? translations['en'];
|
||||
let str = language.dictionary[key] ?? translations['en'].dictionary[key];
|
||||
|
||||
if ( ! str ) {
|
||||
str = key;
|
||||
}
|
||||
str = ReplacePlaceholders(str, arg_variables);
|
||||
if ( encode_html ) {
|
||||
str = html_encode(str);
|
||||
// html_encode doesn't render line breaks
|
||||
str = str.replace(/\n/g, '<br />');
|
||||
}
|
||||
// replace %% occurrences with the values in replacements
|
||||
// %% is for simple text replacements
|
||||
// %strong% is for <strong> tags
|
||||
// e.g. "Hello, %strong%" => "Hello, <strong>World</strong>"
|
||||
// e.g. "Hello, %%" => "Hello, World"
|
||||
// e.g. "Hello, %strong%, %%!" => "Hello, <strong>World</strong>, Universe!"
|
||||
for ( let i = 0; i < replacements.length; i++ ) {
|
||||
// sanitize the replacement
|
||||
replacements[i] = encode_html ? html_encode(replacements[i]) : replacements[i];
|
||||
// Function replacements below: a string one would expand `$&`, `$'`
|
||||
// and the like inside the value (a team named "R&D $& Co").
|
||||
const value = replacements[i];
|
||||
// find first occurrence of %strong%
|
||||
let index = str.indexOf('%strong%');
|
||||
// find first occurrence of %%
|
||||
let index2 = str.indexOf('%%');
|
||||
// decide which one to replace
|
||||
if ( index === -1 && index2 === -1 ) {
|
||||
break;
|
||||
} else if ( index === -1 ) {
|
||||
str = str.replace('%%', () => value);
|
||||
} else if ( index2 === -1 ) {
|
||||
str = str.replace('%strong%', () => `<strong>${ value }</strong>`);
|
||||
} else if ( index < index2 ) {
|
||||
str = str.replace('%strong%', () => `<strong>${ value }</strong>`);
|
||||
} else {
|
||||
str = str.replace('%%', () => value);
|
||||
}
|
||||
}
|
||||
return str;
|
||||
};
|
||||
|
||||
export default {};
|
||||
export default {};
|
||||
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
const variables = {
|
||||
docs: 'https://docs.puter.com/',
|
||||
terms: 'https://puter.com/terms',
|
||||
privacy: 'https://puter.com/privacy',
|
||||
};
|
||||
|
||||
function ReplacePlaceholders (str, arg_variables = {}) {
|
||||
const all_variables = { ...variables, ...arg_variables };
|
||||
str = str.replace(/{{link=(.*?)}}(.*?){{\/link}}/g, (_, key, text) => `<a href="${all_variables[key]}" target="_blank">${text}</a>`);
|
||||
str = str.replace(/{{(.*?)}}/g, (_, key) => all_variables[key]);
|
||||
return str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Installs `window.i18n` over the given language table, which must include `en`.
|
||||
* @param {Record<string, { dictionary: Record<string, string> }>} translations
|
||||
*/
|
||||
export function installI18n (translations) {
|
||||
window.i18n = function (key, replacements = [], encode_html = true) {
|
||||
let arg_variables = {};
|
||||
if ( Array.isArray(replacements) === false ) {
|
||||
if ( typeof replacements === 'object' ) {
|
||||
arg_variables = replacements;
|
||||
replacements = [];
|
||||
} else {
|
||||
replacements = [replacements];
|
||||
}
|
||||
}
|
||||
|
||||
let language = translations[window.locale] ?? translations['en'];
|
||||
let str = language.dictionary[key] ?? translations['en'].dictionary[key];
|
||||
|
||||
if ( ! str ) {
|
||||
str = key;
|
||||
}
|
||||
str = ReplacePlaceholders(str, arg_variables);
|
||||
if ( encode_html ) {
|
||||
str = html_encode(str);
|
||||
// html_encode doesn't render line breaks
|
||||
str = str.replace(/\n/g, '<br />');
|
||||
}
|
||||
// replace %% occurrences with the values in replacements
|
||||
// %% is for simple text replacements
|
||||
// %strong% is for <strong> tags
|
||||
// e.g. "Hello, %strong%" => "Hello, <strong>World</strong>"
|
||||
// e.g. "Hello, %%" => "Hello, World"
|
||||
// e.g. "Hello, %strong%, %%!" => "Hello, <strong>World</strong>, Universe!"
|
||||
for ( let i = 0; i < replacements.length; i++ ) {
|
||||
// sanitize the replacement
|
||||
replacements[i] = encode_html ? html_encode(replacements[i]) : replacements[i];
|
||||
// Function replacements below: a string one would expand `$&`, `$'`
|
||||
// and the like inside the value (a team named "R&D $& Co").
|
||||
const value = replacements[i];
|
||||
// find first occurrence of %strong%
|
||||
let index = str.indexOf('%strong%');
|
||||
// find first occurrence of %%
|
||||
let index2 = str.indexOf('%%');
|
||||
// decide which one to replace
|
||||
if ( index === -1 && index2 === -1 ) {
|
||||
break;
|
||||
} else if ( index === -1 ) {
|
||||
str = str.replace('%%', () => value);
|
||||
} else if ( index2 === -1 ) {
|
||||
str = str.replace('%strong%', () => `<strong>${ value }</strong>`);
|
||||
} else if ( index < index2 ) {
|
||||
str = str.replace('%strong%', () => `<strong>${ value }</strong>`);
|
||||
} else {
|
||||
str = str.replace('%%', () => value);
|
||||
}
|
||||
}
|
||||
return str;
|
||||
};
|
||||
}
|
||||
@@ -22,6 +22,7 @@ const en = {
|
||||
english_name: 'English',
|
||||
code: 'en',
|
||||
dictionary: {
|
||||
app_browser_unavailable: 'Apps are unavailable. Please reopen this browser from your app.',
|
||||
about: 'About',
|
||||
account: 'Account',
|
||||
account_password: 'Verify Account Password',
|
||||
|
||||
@@ -29,7 +29,6 @@
|
||||
|
||||
const DEFAULT_TITLE = 'New notification';
|
||||
const DEFAULT_ICON = '/favicons/android-icon-192x192.png';
|
||||
const SHOW_MESSAGE = 'puter-show-notification';
|
||||
const CLICK_MESSAGE = 'puter-notification-click';
|
||||
const CLICK_ACK_MESSAGE = 'puter-notification-click-ack';
|
||||
const SUBSCRIPTION_CHANGED_MESSAGE = 'puter-push-subscription-changed';
|
||||
@@ -205,19 +204,6 @@ async function routeClick (scope, click, startedAt = Date.now(), budgetMs = CLIC
|
||||
await scope.clients.openWindow(clickUrl(click, origin));
|
||||
}
|
||||
|
||||
async function hasVisibleWindow (scope) {
|
||||
const origin = scope.location.origin;
|
||||
const windowClients = await scope.clients.matchAll({ type: 'window', includeUncontrolled: true });
|
||||
return windowClients.some((client) => (
|
||||
sameOriginUrl(client.url, origin) && client.visibilityState === 'visible'
|
||||
));
|
||||
}
|
||||
|
||||
async function showPageMessage (scope, message) {
|
||||
if ( await hasVisibleWindow(scope) ) return;
|
||||
await showMessage(scope.registration, message, scope.location.origin);
|
||||
}
|
||||
|
||||
async function postToWindows (scope, message) {
|
||||
const origin = scope.location.origin;
|
||||
const windowClients = await scope.clients.matchAll({ type: 'window', includeUncontrolled: true });
|
||||
@@ -254,8 +240,6 @@ self.addEventListener('message', (event) => {
|
||||
if ( !message || !event.source || !sameOriginUrl(event.source.url, self.location.origin) ) return;
|
||||
if ( message.type === CLICK_ACK_MESSAGE ) {
|
||||
pendingClickAcks.get(message.id)?.();
|
||||
} else if ( message.type === SHOW_MESSAGE ) {
|
||||
event.waitUntil(showPageMessage(self, message.puter));
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
+2
-25
@@ -222,36 +222,13 @@ describe('service worker push', () => {
|
||||
expect(worker.shown).toEqual([{ title: 'New notification', options: { icon: DEFAULT_ICON } }]);
|
||||
});
|
||||
|
||||
it('shows a page\'s message only while no Puter window is visible', async () => {
|
||||
const visible = loadWorker({ windows: [windowClient(`${ORIGIN}/desktop`, { visibilityState: 'visible' })] });
|
||||
await visible.fire('message', {
|
||||
data: { type: 'puter-show-notification', puter: { uid: 'n1', title: 'Hi' } },
|
||||
source: { url: `${ORIGIN}/` },
|
||||
});
|
||||
expect(visible.shown).toEqual([]);
|
||||
expect(visible.scope.clients.matchAll).toHaveBeenCalledWith({ type: 'window', includeUncontrolled: true });
|
||||
|
||||
const otherOrigin = loadWorker({ windows: [windowClient('https://elsewhere.example/', { visibilityState: 'visible' })] });
|
||||
await otherOrigin.fire('message', {
|
||||
data: { type: 'puter-show-notification', puter: { uid: 'n1', title: 'Hi' } },
|
||||
source: { url: `${ORIGIN}/` },
|
||||
});
|
||||
expect(otherOrigin.shown.map((n) => n.title)).toEqual(['Hi']);
|
||||
});
|
||||
|
||||
it('shows a page\'s message from this origin and ignores other messages', async () => {
|
||||
it('shows nothing a page posts, only pushes', async () => {
|
||||
const worker = loadWorker();
|
||||
await worker.fire('message', {
|
||||
data: { type: 'puter-show-notification', puter: { uid: 'n1', title: 'Hi' } },
|
||||
source: { url: `${ORIGIN}/` },
|
||||
});
|
||||
worker.listeners.message({ data: { type: 'other' }, source: { url: `${ORIGIN}/` }, waitUntil: () => {} });
|
||||
worker.listeners.message({
|
||||
data: { type: 'puter-show-notification', puter: { title: 'x' } },
|
||||
source: { url: 'https://elsewhere.example/' },
|
||||
waitUntil: () => {},
|
||||
});
|
||||
expect(worker.shown.map((n) => n.title)).toEqual(['Hi']);
|
||||
expect(worker.shown).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -79,21 +79,27 @@ module.exports = async (options = {}) => {
|
||||
}
|
||||
|
||||
const config = {};
|
||||
config.entry = [
|
||||
'./src/init_sync.js',
|
||||
'./src/init_async.js',
|
||||
'./src/initgui.js',
|
||||
'./src/helpers.js',
|
||||
'./src/IPC.js',
|
||||
'./src/globals.js',
|
||||
'./src/i18n/i18n.js',
|
||||
'./src/keyboard.js',
|
||||
'./src/index.js',
|
||||
...entries,
|
||||
];
|
||||
config.entry = {
|
||||
bundle: [
|
||||
'./src/init_sync.js',
|
||||
'./src/init_async.js',
|
||||
'./src/initgui.js',
|
||||
'./src/helpers.js',
|
||||
'./src/IPC.js',
|
||||
'./src/globals.js',
|
||||
'./src/i18n/i18n.js',
|
||||
'./src/keyboard.js',
|
||||
'./src/index.js',
|
||||
...entries,
|
||||
],
|
||||
'apps-embed': './src/appsEmbed.js',
|
||||
};
|
||||
config.output = {
|
||||
path: path.resolve(__dirname, '../dist'),
|
||||
filename: 'bundle.min.js',
|
||||
filename: '[name].min.js',
|
||||
};
|
||||
config.module = {
|
||||
rules: [{ test: /jquery-3\.6\.1\.min\.js$/, type: 'javascript/auto' }],
|
||||
};
|
||||
config.resolve = {
|
||||
modules: [
|
||||
@@ -114,4 +120,4 @@ module.exports = async (options = {}) => {
|
||||
}),
|
||||
];
|
||||
return config;
|
||||
};
|
||||
};
|
||||
@@ -87,6 +87,7 @@ module.exports = async ({ dir, options }) => {
|
||||
|
||||
return new webpack.BannerPlugin({
|
||||
banner: prefix_text,
|
||||
include: /bundle\.min\.js$/,
|
||||
raw: true,
|
||||
});
|
||||
};
|
||||
Loaded 100 of 120 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user