mirror of
https://github.com/HeyPuter/puter.git
synced 2026-11-13 20:12:57 -10:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1f75ee51e0 |
No files matched your search
@@ -53,7 +53,7 @@ jobs:
|
||||
# step above fails; the tests skip themselves without the key.
|
||||
- name: Run live realtime conversation tests
|
||||
if: ${{ !cancelled() }}
|
||||
run: npm run setupExtensions && (cd src/puter-js && npm run build) && npx vitest run --config src/puter-js/tests/api/vitest.config.ts src/puter-js/tests/api/realtime/realtimeLive.test.ts
|
||||
run: npm run setupExtensions && (cd src/puter-js && npm run build) && npx vitest run --config src/puter-js/tests/api/vitest.config.ts src/puter-js/tests/api/realtime/convoLive.test.ts
|
||||
env:
|
||||
CI: 'true'
|
||||
PUTER_TEST_AI_OPENAI_API_KEY: ${{ secrets.PUTER_TEST_AI_OPENAI_API_KEY }}
|
||||
@@ -124,7 +124,7 @@ localization system.
|
||||
</script>
|
||||
```
|
||||
|
||||
## Implementation
|
||||
## Implementation and checks
|
||||
|
||||
`createAppBrowser` in `src/gui/src/UI/Dashboard/AppBrowser.js` owns the shared UI
|
||||
and data behavior; its `guestApps` option is the signed-out list. `TabApps.js`
|
||||
@@ -134,93 +134,14 @@ injects desktop actions. `appsEmbed.js` supplies new-tab launching through
|
||||
`launch_app`, and loads only the embed entry, its SDK, and shared styles. `embedHostUser` in
|
||||
`src/gui/src/helpers/embedHostUser.js` reads the host's user from the fragment, and
|
||||
`embedHostTheme.js` beside it applies the host's theme.
|
||||
The normal GUI webpack build emits `bundle.min.js`, `apps-embed.min.js`, and `feedback-embed.min.js`.
|
||||
The normal GUI webpack build emits `bundle.min.js` and `apps-embed.min.js`.
|
||||
The `/embed/apps` route puts the recommended list in the page's config as
|
||||
`guestApps`. No puter.js API or app-list endpoint changes are needed.
|
||||
|
||||
## Feedback dialog
|
||||
|
||||
`GET /embed/feedback` is the app feedback dialog (`puter.ui.showFeedbackDialog()`)
|
||||
for a first-party page, with the same framing rules, query redirect, and
|
||||
host-user binding as `/embed/apps`. It sends the feedback for the framing
|
||||
page's origin, which the server reads from the referrer and pins with
|
||||
`frame-ancestors`; opened top-level, it does nothing. Its page is transparent
|
||||
so the dialog's own scrim covers the host.
|
||||
|
||||
puter.js uses it on a page one label under the GUI origin, other than the
|
||||
subdomains the server refuses, when the page is signed in, framing it in a
|
||||
modal `<dialog>` that stays hidden until the embed is ready. A session check that takes longer than 200ms shows a loading scrim
|
||||
with a spinner in its place; Escape or a click on it cancels. The embed posts `{ msg: 'feedbackEmbed', state }` to its parent
|
||||
(`loading`, then `ready` or `unavailable`) and finally
|
||||
`{ msg: 'feedbackDialogClosed', sent }`. It reports `unavailable` without the
|
||||
named user's session, or while that user's email still needs confirming, and
|
||||
puter.js then uses the `/action/send-feedback` popup, which can sign in. So does
|
||||
a frame that never answers, such as one the host's CSP blocks. `feedbackEmbed.js`
|
||||
is the entry, sharing `embedSession` in `src/gui/src/helpers/embedSession.js`
|
||||
with the app browser; `showFeedbackEmbed` in `src/puter-js/src/lib/guiEmbed.js`
|
||||
is the host side.
|
||||
|
||||
## Sign-in and file pickers
|
||||
|
||||
`GET /embed/sign-in`, `/embed/show-open-file-picker`, `/embed/show-save-file-picker`
|
||||
and `/embed/show-directory-picker` serve the same GUI popups as `/action/<action>`
|
||||
for a first-party page, with the same framing rules, query redirect, and
|
||||
`no-store` as `/embed/apps`. Any other spelling of the path (another case, a
|
||||
trailing slash) redirects to it, since the shell boots the desktop for it. They render the normal GUI shell, which names the
|
||||
framing page (read from the referrer, pinned with `frame-ancestors`) as
|
||||
`gui_params.embed_host_origin`. The GUI then runs the popup boot unchanged, with
|
||||
the framing page as `window.opener`, and `window.close()` asks the page to
|
||||
remove the frame (`src/gui/src/helpers/popupFrame.js`). Without a framing page
|
||||
to answer (opened top-level, or framed by a refused page) it does nothing. The
|
||||
popup's parameters come from the fragment instead of the query: `msg_id`,
|
||||
`options` and `request_auth`, plus `user` for a picker.
|
||||
|
||||
The frame posts `{ msg: 'puterPopupFrame', state }` to its parent: `loading` as
|
||||
soon as the shell boots, `ready` once it shows a window or dialog, `unavailable`
|
||||
when the page has to use the popup, `provider` when the user picks a sign-in
|
||||
provider (below), and `closed` where a popup would close. Its
|
||||
answers (`puter.token`, `fileOpenPicked`, `directoryPicked`, `fileSaved`, the
|
||||
cancel messages, `sendMeFileData`) are the popup's.
|
||||
|
||||
puter.js uses them on a page one label under the GUI origin, other than the
|
||||
subdomains the server refuses, unless the page is cross-origin isolated or in a
|
||||
frame of another site (which `frame-ancestors` refuses too).
|
||||
`showPopupFrame` in `src/puter-js/src/lib/guiEmbed.js` shows the frame in a box
|
||||
the size of the popup window, over a scrim, hidden until `ready` and behind the
|
||||
loading spinner after 200ms. Escape closes it, on the page or in the frame. One shows per popup, as each
|
||||
popup has its own window name: a second picker request of the same kind
|
||||
replaces the one showing, and a `signIn()` call made while a framed sign-in is
|
||||
open settles with it. On
|
||||
screens up to 600px wide the box fills the screen. A frame that is refused,
|
||||
never answers, or reports `unavailable` falls back to the popup, opened through
|
||||
a consent dialog when the click that asked for it has expired. A reload or navigation the frame
|
||||
starts itself (logging out, a sign-out in another tab) names the frame as its
|
||||
referrer, so the server refuses it; the page then shows its spinner and opens
|
||||
the frame again, once per `ready`.
|
||||
|
||||
- **Sign-in** (`puter.auth.signIn()`) has no host-user binding: it is how the
|
||||
page gets a user. The box has a close button, which rejects with
|
||||
`auth_window_closed` as closing the popup does. Providers refuse to be
|
||||
framed, so choosing one posts `{ state: 'provider', url }` with the
|
||||
provider's start URL for this sign-in's `msg_id`, then `unavailable`. The
|
||||
page opens the popup straight at that URL, once it checks the URL is the
|
||||
GUI's `/auth/oidc/<provider>/start` for this `msg_id`, and the provider
|
||||
returns it as this sign-in's popup. `attempt_temp_user_creation` keeps the popup, since
|
||||
only the popup creates a temporary account.
|
||||
- **Pickers** frame only for a signed-in page. The GUI reports `unavailable`
|
||||
unless its session belongs to the user named in `user`, and the page then uses
|
||||
the popup, which can sign in and switch accounts. The token the GUI sends
|
||||
before a picker's answer isn't adopted, since it is for the page's own user.
|
||||
Dismissing the loading scrim settles the picker as cancelled.
|
||||
|
||||
## Checks
|
||||
|
||||
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 src/feedbackEmbed
|
||||
npx vitest run src/puter-js/src/lib/guiEmbed.test.js src/puter-js/src/modules/UI.showFeedbackDialog.test.js src/puter-js/src/modules/Auth.signInFrame.test.js src/puter-js/src/modules/UI.pickerFrame.test.js
|
||||
npx vitest run --config src/gui/vitest.config.js src/gui/src/helpers/popupFrame.test.js
|
||||
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
|
||||
```
|
||||
@@ -27,7 +27,7 @@ import type { Actor } from '../../core/actor.js';
|
||||
import { PuterRouter } from '../../core/http/PuterRouter.js';
|
||||
import { PuterServer } from '../../server.js';
|
||||
import { setupTestServer } from '../../testUtil.js';
|
||||
import { embedFrameAncestors, embedHostOrigin } from './HomepageController.js';
|
||||
import { embedFrameAncestors } from './HomepageController.js';
|
||||
|
||||
// ── Test harness ────────────────────────────────────────────────────
|
||||
//
|
||||
@@ -550,26 +550,3 @@ describe('embedFrameAncestors', () => {
|
||||
).toBe("'self'");
|
||||
});
|
||||
});
|
||||
|
||||
describe('embedHostOrigin', () => {
|
||||
const config = {
|
||||
origin: 'https://example.test',
|
||||
domain: 'example.test',
|
||||
api_base_url: 'https://backend.example.test',
|
||||
static_hosting_domain: 'site.example.test',
|
||||
};
|
||||
|
||||
it.each([
|
||||
['https://sheets.example.test/doc/1', 'https://sheets.example.test'],
|
||||
['https://backend.example.test/x', null],
|
||||
['https://api.example.test/', null],
|
||||
['https://someone.site.example.test/', null],
|
||||
['https://a.sheets.example.test/', null],
|
||||
['http://sheets.example.test/', null],
|
||||
['https://example.test/', null],
|
||||
['not a url', null],
|
||||
[undefined, null],
|
||||
])('%s frames as %s', (referer, expected) => {
|
||||
expect(embedHostOrigin(config, referer)).toBe(expected);
|
||||
});
|
||||
});
|
||||
@@ -31,20 +31,8 @@ import type {
|
||||
PuterHomepageService,
|
||||
PageMeta,
|
||||
LaunchOptions,
|
||||
PopupFrame,
|
||||
} from '../../services/homepage/PuterHomepageService';
|
||||
|
||||
/**
|
||||
* GUI popups a first-party page can show in a frame at `/embed/<action>`,
|
||||
* instead of opening `/action/<action>` in a window.
|
||||
*/
|
||||
export const FRAMED_POPUP_ACTIONS = [
|
||||
'sign-in',
|
||||
'show-open-file-picker',
|
||||
'show-save-file-picker',
|
||||
'show-directory-picker',
|
||||
] as const;
|
||||
|
||||
/** Longest description/og:description/twitter:description on `/app/<name>`. */
|
||||
export const APP_META_DESCRIPTION_MAX = 150;
|
||||
|
||||
@@ -73,34 +61,32 @@ export function appMetaDescription(text: string): string {
|
||||
return `${cut.replace(/[\s,;:.!?…—-]+$/, '')}…`;
|
||||
}
|
||||
|
||||
type EmbedFramingConfig = Pick<
|
||||
IConfig,
|
||||
| 'origin'
|
||||
| 'domain'
|
||||
| 'api_base_url'
|
||||
| 'static_hosting_domain'
|
||||
| 'static_hosting_domain_alt'
|
||||
| 'private_app_hosting_domain'
|
||||
| 'private_app_hosting_domain_alt'
|
||||
>;
|
||||
|
||||
/**
|
||||
* The origin of a first-party page that may frame an `/embed/*` page, named
|
||||
* exactly from the request's referrer, or null. A wildcard over `*.<domain>`
|
||||
* `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 only a single label under `<domain>` qualifies, and not one this server
|
||||
* answers itself (the API, WebDAV, SDK and native-app hosts).
|
||||
* so the embedding page is named exactly, from the request's referrer. Only a
|
||||
* single label under `<domain>` qualifies, and not one this server answers
|
||||
* itself (the API, WebDAV, SDK and native-app hosts).
|
||||
*/
|
||||
export function embedHostOrigin(
|
||||
config: EmbedFramingConfig,
|
||||
export function embedFrameAncestors(
|
||||
config: Pick<
|
||||
IConfig,
|
||||
| 'origin'
|
||||
| 'domain'
|
||||
| 'api_base_url'
|
||||
| 'static_hosting_domain'
|
||||
| 'static_hosting_domain_alt'
|
||||
| 'private_app_hosting_domain'
|
||||
| 'private_app_hosting_domain_alt'
|
||||
>,
|
||||
referer: string | undefined,
|
||||
): string | null {
|
||||
): string {
|
||||
const gui = new URL(config.origin ?? `https://${config.domain}`);
|
||||
let parent: URL;
|
||||
try {
|
||||
parent = new URL(referer ?? '');
|
||||
} catch {
|
||||
return null;
|
||||
return "'self'";
|
||||
}
|
||||
const host = parent.hostname.toLowerCase();
|
||||
const label = subdomainLabelOf(host, config.domain);
|
||||
@@ -119,16 +105,7 @@ export function embedHostOrigin(
|
||||
!isReservedSubdomain(label) &&
|
||||
host !== new URL(config.api_base_url).hostname &&
|
||||
!hostingDomains.some((d) => host === d || host.endsWith(`.${d}`));
|
||||
return allowed ? parent.origin : null;
|
||||
}
|
||||
|
||||
/** `frame-ancestors` for an `/embed/*` page: the GUI and {@link embedHostOrigin}. */
|
||||
export function embedFrameAncestors(
|
||||
config: EmbedFramingConfig,
|
||||
referer: string | undefined,
|
||||
): string {
|
||||
const host = embedHostOrigin(config, referer);
|
||||
return host ? `'self' ${host}` : "'self'";
|
||||
return allowed ? `'self' ${parent.origin}` : "'self'";
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -165,7 +142,6 @@ export class HomepageController extends PuterController {
|
||||
res: express.Response,
|
||||
metaOverrides: Partial<PageMeta> = {},
|
||||
launch: LaunchOptions = {},
|
||||
popupFrame?: PopupFrame,
|
||||
) => {
|
||||
const meta = { ...defaultMeta(req), ...metaOverrides };
|
||||
const actor =
|
||||
@@ -174,27 +150,19 @@ export class HomepageController extends PuterController {
|
||||
actor?: Parameters<typeof homepage.send>[0]['actor'];
|
||||
}
|
||||
).actor ?? null;
|
||||
await homepage.send({ req, res, actor }, meta, launch, popupFrame);
|
||||
await homepage.send({ req, res, actor }, meta, launch);
|
||||
};
|
||||
|
||||
// -- Root + path-aliased shell routes ------------------------
|
||||
|
||||
router.get('/', {}, (req, res) => sendShell(req, res));
|
||||
|
||||
/**
|
||||
* Headers shared by the `/embed/*` pages; false when the request was
|
||||
* redirected instead. SDK bootstrap parameters must not change an
|
||||
* embed's account or API origin, so a query string is dropped. Checked
|
||||
* on the raw URL; `req.query` drops pairs past the parser's limit.
|
||||
*/
|
||||
const prepareEmbed = (
|
||||
req: express.Request,
|
||||
res: express.Response,
|
||||
path: string,
|
||||
): boolean => {
|
||||
router.get('/embed/apps', {}, async (req, res) => {
|
||||
// SDK bootstrap parameters must not change this page's account or API origin.
|
||||
// Checked on the raw URL; `req.query` drops pairs past the parser's limit.
|
||||
if (req.originalUrl.includes('?')) {
|
||||
res.redirect(path);
|
||||
return false;
|
||||
res.redirect('/embed/apps');
|
||||
return;
|
||||
}
|
||||
res.removeHeader('X-Frame-Options');
|
||||
res.setHeader(
|
||||
@@ -202,11 +170,6 @@ export class HomepageController extends PuterController {
|
||||
`frame-ancestors ${embedFrameAncestors(this.config, req.get('referer'))}`,
|
||||
);
|
||||
res.setHeader('Cache-Control', 'no-store');
|
||||
return true;
|
||||
};
|
||||
|
||||
router.get('/embed/apps', {}, async (req, res) => {
|
||||
if (!prepareEmbed(req, res, '/embed/apps')) return;
|
||||
// Shown to visitors without a session; a signed-in embed fetches
|
||||
// its own list, so a failure here only costs signed-out visitors.
|
||||
const guestApps = await this.services.recommendedApps
|
||||
@@ -215,48 +178,6 @@ export class HomepageController extends PuterController {
|
||||
res.send(homepage.renderAppBrowserEmbed({ guestApps }));
|
||||
});
|
||||
|
||||
router.get('/embed/feedback', {}, (req, res) => {
|
||||
if (!prepareEmbed(req, res, '/embed/feedback')) return;
|
||||
// The feedback is recorded against this origin, which
|
||||
// frame-ancestors has just pinned to the actual framing page.
|
||||
res.send(
|
||||
homepage.renderFeedbackEmbed({
|
||||
hostOrigin: embedHostOrigin(
|
||||
this.config,
|
||||
req.get('referer'),
|
||||
),
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
// The GUI runs the popup's own boot, answering the framing page
|
||||
// instead of an opener.
|
||||
for (const action of FRAMED_POPUP_ACTIONS) {
|
||||
const embedPath = `/embed/${action}`;
|
||||
router.get(embedPath, {}, async (req, res) => {
|
||||
// Routes match case-insensitively and with a trailing slash,
|
||||
// and the shell boots the desktop for any other spelling, so
|
||||
// only the exact one may be framed.
|
||||
if (req.originalUrl !== embedPath) {
|
||||
res.redirect(embedPath);
|
||||
return;
|
||||
}
|
||||
if (!prepareEmbed(req, res, embedPath)) return;
|
||||
await sendShell(
|
||||
req,
|
||||
res,
|
||||
{},
|
||||
{},
|
||||
{
|
||||
hostOrigin: embedHostOrigin(
|
||||
this.config,
|
||||
req.get('referer'),
|
||||
),
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
router.get('/settings', {}, (req, res) => sendShell(req, res));
|
||||
router.get('/settings/*splat', {}, (req, res) => sendShell(req, res));
|
||||
|
||||
|
||||
@@ -368,107 +368,6 @@ describe('PuterServer host header validation', () => {
|
||||
expect(api.status).toBe(404);
|
||||
});
|
||||
|
||||
it('lets a deployment subdomain frame the feedback embed and names it', async () => {
|
||||
const parent = `http://spreadsheet.puter.localhost:${port}`;
|
||||
const res = await request('/embed/feedback', {
|
||||
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/feedback-embed.min.js');
|
||||
expect(res.body).not.toContain('/dist/bundle.min.js');
|
||||
const config = JSON.parse(
|
||||
res.body.match(/id="feedback-embed-config">(.*?)<\/script>/)[1],
|
||||
);
|
||||
expect(config).toEqual({
|
||||
apiOrigin: `http://api.puter.localhost:${port}`,
|
||||
hostOrigin: parent,
|
||||
});
|
||||
|
||||
const hosted = await request('/embed/feedback', {
|
||||
host: `puter.localhost:${port}`,
|
||||
referer: `http://someone.site.puter.localhost:${port}/`,
|
||||
});
|
||||
expect(hosted.headers['content-security-policy']).toBe("frame-ancestors 'self'");
|
||||
expect(
|
||||
JSON.parse(
|
||||
hosted.body.match(/id="feedback-embed-config">(.*?)<\/script>/)[1],
|
||||
).hostOrigin,
|
||||
).toBeNull();
|
||||
|
||||
const redirected = await request('/embed/feedback?puter.api_origin=https://other.test', {
|
||||
host: `puter.localhost:${port}`,
|
||||
});
|
||||
expect(redirected.status).toBe(302);
|
||||
expect(redirected.headers.location).toBe('/embed/feedback');
|
||||
});
|
||||
|
||||
it.each([
|
||||
'sign-in',
|
||||
'show-open-file-picker',
|
||||
'show-save-file-picker',
|
||||
'show-directory-picker',
|
||||
])('lets a deployment subdomain frame the %s popup and names it to the GUI', async (action) => {
|
||||
const parent = `http://spreadsheet.puter.localhost:${port}`;
|
||||
const guiParams = (body: string) =>
|
||||
JSON.parse(body.match(/gui\((\{.*\})\);/)[1]);
|
||||
const res = await request(`/embed/${action}`, {
|
||||
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(guiParams(res.body).embed_host_origin).toBe(parent);
|
||||
|
||||
const hosted = await request(`/embed/${action}`, {
|
||||
host: `puter.localhost:${port}`,
|
||||
referer: `http://someone.site.puter.localhost:${port}/`,
|
||||
});
|
||||
expect(hosted.headers['content-security-policy']).toBe("frame-ancestors 'self'");
|
||||
expect(guiParams(hosted.body).embed_host_origin).toBeNull();
|
||||
|
||||
const redirected = await request(`/embed/${action}?auth_token=x&embedded_in_popup=true`, {
|
||||
host: `puter.localhost:${port}`,
|
||||
referer: `${parent}/sheet/1`,
|
||||
});
|
||||
expect(redirected.status).toBe(302);
|
||||
expect(redirected.headers.location).toBe(`/embed/${action}`);
|
||||
expect(redirected.headers['x-frame-options']).toBe('SAMEORIGIN');
|
||||
});
|
||||
|
||||
it.each(['/embed/SIGN-IN', '/embed/Show-Open-File-Picker', '/embed/sign-in/'])(
|
||||
'lets a first-party page frame only the exact spelling of %s',
|
||||
async (spelling) => {
|
||||
const res = await request(spelling, {
|
||||
host: `puter.localhost:${port}`,
|
||||
referer: `http://spreadsheet.puter.localhost:${port}/`,
|
||||
});
|
||||
expect(res.status).toBe(302);
|
||||
expect(res.headers.location).toBe(spelling.toLowerCase().replace(/\/$/, ''));
|
||||
expect(res.headers['x-frame-options']).toBe('SAMEORIGIN');
|
||||
expect(res.headers['content-security-policy']).toBeUndefined();
|
||||
},
|
||||
);
|
||||
|
||||
it('names no framing page to the popups it serves in a window', async () => {
|
||||
const res = await request('/action/sign-in?embedded_in_popup=true', {
|
||||
host: `puter.localhost:${port}`,
|
||||
referer: `http://spreadsheet.puter.localhost:${port}/`,
|
||||
});
|
||||
expect(res.headers['x-frame-options']).toBe('SAMEORIGIN');
|
||||
expect(res.headers['content-security-policy']).toBeUndefined();
|
||||
expect(res.body).not.toContain('embed_host_origin');
|
||||
});
|
||||
|
||||
it.each([
|
||||
['a user-hosted site', 'http://someone.site.puter.localhost:PORT/'],
|
||||
['a private app host', 'http://someone.app.puter.localhost:PORT/'],
|
||||
|
||||
@@ -107,31 +107,6 @@ describe('app browser embed shell', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('feedback embed shell', () => {
|
||||
it('carries the framing page and API origin, and loads only its own script', () => {
|
||||
const html = makeService({
|
||||
api_base_url: 'https://api.deployment.test',
|
||||
gui_bundle: '/static/bundle.min.js?v=42',
|
||||
}).renderFeedbackEmbed({ hostOrigin: 'https://notes.deployment.test' });
|
||||
const configJson = html.match(/id="feedback-embed-config">(.*?)<\/script>/)![1];
|
||||
expect(JSON.parse(configJson)).toEqual({
|
||||
apiOrigin: 'https://api.deployment.test',
|
||||
hostOrigin: 'https://notes.deployment.test',
|
||||
});
|
||||
expect(html).toContain('<script defer src="/static/feedback-embed.min.js?v=42"></script>');
|
||||
expect(html).toContain('<link rel="stylesheet" href="/css/feedback-embed.css">');
|
||||
expect(html).toContain('<main id="feedback-embed"></main>');
|
||||
expect(html).not.toContain('bundle.min.js');
|
||||
expect(html).not.toContain('apps-embed');
|
||||
});
|
||||
|
||||
it('carries no framing page when none is allowed', () => {
|
||||
const html = makeService({}).renderFeedbackEmbed({ hostOrigin: null });
|
||||
const configJson = html.match(/id="feedback-embed-config">(.*?)<\/script>/)![1];
|
||||
expect(JSON.parse(configJson).hostOrigin).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
/** Capture what the service sends, and return the rendered HTML. */
|
||||
const render = async (
|
||||
service: PuterHomepageService,
|
||||
|
||||
@@ -48,15 +48,6 @@ export interface LaunchOptions {
|
||||
on_initialized?: Array<Record<string, unknown>>;
|
||||
}
|
||||
|
||||
/**
|
||||
* A GUI popup a first-party page frames at `/embed/<action>`. `hostOrigin` is
|
||||
* that page, read from the referrer and pinned with frame-ancestors; null when
|
||||
* no allowed page frames it.
|
||||
*/
|
||||
export interface PopupFrame {
|
||||
hostOrigin: string | null;
|
||||
}
|
||||
|
||||
interface PuterGuiAddonsEvent {
|
||||
req: Request;
|
||||
path: string;
|
||||
@@ -180,50 +171,15 @@ export class PuterHomepageService extends PuterService {
|
||||
renderAppBrowserEmbed({
|
||||
guestApps = [],
|
||||
}: { guestApps?: Array<Record<string, unknown>> } = {}): string {
|
||||
return this.#renderEmbed({
|
||||
rootId: 'app-browser',
|
||||
rootClass: 'dashboard',
|
||||
stylesheet: 'apps-embed.css',
|
||||
script: 'apps-embed.min.js',
|
||||
config: {
|
||||
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/'),
|
||||
guestApps,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* `hostOrigin` is the first-party page framing the dialog, which the
|
||||
* feedback is sent for; null when no allowed page frames it.
|
||||
*/
|
||||
renderFeedbackEmbed({ hostOrigin }: { hostOrigin: string | null }): string {
|
||||
return this.#renderEmbed({
|
||||
rootId: 'feedback-embed',
|
||||
stylesheet: 'feedback-embed.css',
|
||||
script: 'feedback-embed.min.js',
|
||||
config: { apiOrigin: this.config.api_base_url, hostOrigin },
|
||||
});
|
||||
}
|
||||
|
||||
#renderEmbed({
|
||||
rootId,
|
||||
rootClass,
|
||||
stylesheet,
|
||||
script,
|
||||
config,
|
||||
}: {
|
||||
rootId: string;
|
||||
rootClass?: string;
|
||||
stylesheet: string;
|
||||
script: string;
|
||||
config: Record<string, unknown>;
|
||||
}): string {
|
||||
const configJson = JSON.stringify(config).replace(/</g, '\\u003c');
|
||||
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/'),
|
||||
guestApps,
|
||||
}).replace(/</g, '\\u003c');
|
||||
|
||||
return `<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
@@ -234,27 +190,27 @@ export class PuterHomepageService extends PuterService {
|
||||
<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/${stylesheet}">
|
||||
<link rel="stylesheet" href="/css/apps-embed.css">
|
||||
</head>
|
||||
<body>
|
||||
<main id="${rootId}"${rootClass ? ` class="${rootClass}"` : ''}></main>
|
||||
<script type="application/json" id="${rootId}-config">${configJson}</script>
|
||||
<script defer src="${encode(this.#embedScript(script))}"></script>
|
||||
<main id="app-browser" class="dashboard"></main>
|
||||
<script type="application/json" id="app-browser-config">${configJson}</script>
|
||||
<script defer src="${encode(this.#appBrowserEmbedScript())}"></script>
|
||||
</body>
|
||||
</html>`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Embed scripts are built beside the GUI bundle, so they are served from
|
||||
* The embed's script is built beside the GUI bundle, so it is served from
|
||||
* wherever `gui_bundle` is (a CDN in production), with the same query.
|
||||
*/
|
||||
#embedScript(name: string): string {
|
||||
#appBrowserEmbedScript(): string {
|
||||
const base = 'http://relative.invalid';
|
||||
const bundle = new URL(
|
||||
this.config.gui_bundle ?? '/dist/bundle.min.js',
|
||||
base,
|
||||
);
|
||||
const script = new URL(name, bundle);
|
||||
const script = new URL('apps-embed.min.js', bundle);
|
||||
script.search = bundle.search;
|
||||
return script.origin === base
|
||||
? `${script.pathname}${script.search}`
|
||||
@@ -312,7 +268,6 @@ export class PuterHomepageService extends PuterService {
|
||||
ctx: { req: Request; res: Response; actor?: Actor | null },
|
||||
meta: PageMeta,
|
||||
launchOptions: LaunchOptions = {},
|
||||
popupFrame?: PopupFrame,
|
||||
): Promise<void> {
|
||||
const { req, res, actor } = ctx;
|
||||
|
||||
@@ -337,7 +292,6 @@ export class PuterHomepageService extends PuterService {
|
||||
{ req, actor: actor ?? null },
|
||||
meta,
|
||||
launchOptions,
|
||||
popupFrame,
|
||||
);
|
||||
res.send(html);
|
||||
}
|
||||
@@ -348,7 +302,6 @@ export class PuterHomepageService extends PuterService {
|
||||
ctx: { req: Request; actor: Actor | null },
|
||||
meta: PageMeta,
|
||||
launchOptions: LaunchOptions,
|
||||
popupFrame?: PopupFrame,
|
||||
): Promise<string> {
|
||||
const { req, actor } = ctx;
|
||||
const env = this.config.env ?? 'prod';
|
||||
@@ -394,8 +347,6 @@ export class PuterHomepageService extends PuterService {
|
||||
eventsNotifications: notificationsFoldInEnabled(this.config),
|
||||
...meta,
|
||||
launch_options: launchOptions,
|
||||
// After `meta`, so nothing page-specific can name the opener.
|
||||
...(popupFrame ? { embed_host_origin: popupFrame.hostOrigin } : {}),
|
||||
};
|
||||
|
||||
const event: PuterGuiAddonsEvent = {
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
# OpenAI Realtime relay
|
||||
|
||||
The relay serves `/puterai/openai/v1/realtime` on the API host. The SDK exposes
|
||||
it through [`puter.ai.realtime()`](../../../docs/src/AI/realtime.md).
|
||||
it through [`puter.ai.convo()`](../../../docs/src/AI/convo.md).
|
||||
|
||||
**Beta:** This API is incomplete. Its methods, options, events, and behavior may change abruptly, including breaking changes without notice.
|
||||
|
||||
Direct clients are documented in the [Realtime WebSocket API reference](../../../docs/src/AI/realtime-websocket.md).
|
||||
Direct clients are documented in the [Realtime WebSocket API reference](../../../docs/src/AI/realtime.md).
|
||||
|
||||
## Transport
|
||||
|
||||
@@ -27,7 +27,7 @@ The socket opens before authentication completes. Authentication and model
|
||||
failures arrive as OpenAI-shaped error events followed by a close frame.
|
||||
|
||||
```js
|
||||
const conversation = await puter.ai.realtime({ output: ['text'] });
|
||||
const conversation = await puter.ai.convo({ output: ['text'] });
|
||||
conversation.on('error', (event) => console.error(event));
|
||||
conversation.on('text', ({ delta }) => console.log(delta));
|
||||
conversation.on('turn.done', () => conversation.close());
|
||||
|
||||
@@ -1102,9 +1102,8 @@ interface IConfigOptional {
|
||||
/** Force the bundled GUI even in dev. Default: false. */
|
||||
use_bundled_gui: boolean;
|
||||
/**
|
||||
* Override the GUI bundle JS path. Default: `/dist/bundle.min.js`. The
|
||||
* `/embed/*` pages load `apps-embed.min.js` and `feedback-embed.min.js`
|
||||
* from the same directory.
|
||||
* Override the GUI bundle JS path. Default: `/dist/bundle.min.js`. The app
|
||||
* browser embed loads `apps-embed.min.js` from the same directory.
|
||||
*/
|
||||
gui_bundle: string;
|
||||
/** Override the GUI CSS path when bundled. Default: `/dist/bundle.min.css`. */
|
||||
|
||||
+2
-2
@@ -200,8 +200,8 @@ These AI features are supported out of the box when using Puter.js:
|
||||
- **[`puter.ai.img2txt()`](/AI/img2txt/)** - Extract text from images (OCR)
|
||||
- **[`puter.ai.txt2vid()`](/AI/txt2vid/)** - Generate short video clips from text or a reference image with Wan, Seedance, Veo, Kling, MiniMax H3 and other models, or edit a video you supply with Gemini Omni, FLUX 3 or Kling Omni
|
||||
- **[`puter.ai.speech2speech()`](/AI/speech2speech/)** - Convert speech in one voice to another voice
|
||||
- **[`puter.ai.realtime()`](/AI/realtime/)** - Prepare a beta realtime conversation with optional text, microphone, playback, and interruption helpers
|
||||
- **[Realtime WebSocket API](/AI/realtime-websocket/)** - Connect directly without puter.js. This API is in beta and may change abruptly.
|
||||
- **[`puter.ai.convo()`](/AI/convo/)** - Prepare a beta realtime conversation with optional text, microphone, playback, and interruption helpers
|
||||
- **[Realtime WebSocket API](/AI/realtime/)** - Connect directly without puter.js. This API is in beta and may change abruptly.
|
||||
- **[`puter.ai.listModels()`](/AI/listModels/)** - List available AI chat models (and providers) that Puter currently exposes.
|
||||
- **[`puter.ai.listModelProviders()`](/AI/listModelProviders/)** - List the AI providers that Puter currently exposes.
|
||||
- **[`puter.ai.txt2speech.listEngines()`](/AI/txt2speech.listEngines/)** - List available TTS engines/models
|
||||
|
||||
@@ -0,0 +1,151 @@
|
||||
---
|
||||
title: puter.ai.convo()
|
||||
description: Prepare an OpenAI realtime conversation with optional audio and text helpers.
|
||||
platforms: [websites, apps, nodejs, workers]
|
||||
---
|
||||
|
||||
Prepares a realtime OpenAI conversation that your app can connect to when it's ready.
|
||||
|
||||
Preparing the conversation verifies Puter sign-in. Realtime conversations require a paid Puter plan, and usage is billed to the signed-in user. Native OpenAI events and convenience methods use the same session. Microphone capture and speaker playback start when your app requests them.
|
||||
|
||||
In Node.js, the SDK requires a global `WebSocket`, available in Node.js 22 or later.
|
||||
|
||||
**Beta:** This API is incomplete. Its methods, options, events, and behavior may change abruptly, including breaking changes without notice.
|
||||
|
||||
To connect without puter.js, see the [Realtime WebSocket API](/AI/realtime/).
|
||||
|
||||
## Syntax
|
||||
|
||||
```js
|
||||
const conversation = await puter.ai.convo(options = {});
|
||||
await conversation.connect();
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
#### `options` (Object) (Optional)
|
||||
|
||||
The options object accepts these fields.
|
||||
|
||||
| Option | Type | Description |
|
||||
|--------|------|-------------|
|
||||
| `model` | `String` | The realtime model. Defaults to `gpt-realtime-2.1-mini`. Also accepts `gpt-realtime-2.1`, `gpt-realtime-2`, `gpt-realtime-1.5`, `gpt-realtime`, and `gpt-realtime-mini`. |
|
||||
| `instructions` | `String` | Session instructions, up to 8192 characters. Defaults to the provider's default. |
|
||||
| `output` | `Array<String>` | Accepts `['text']`, `['audio']`, or both. Defaults to `['audio']`. Audio includes a transcript. Requesting both uses audio output and emits transcript deltas through the `text` event. |
|
||||
| `voice` | `String` | OpenAI voice identifier, up to 128 characters. Defaults to the provider's default. |
|
||||
| `turnDetection` | `Boolean` | Whether the provider detects the end of spoken turns automatically. Defaults to `true`. Set to `false` for manual audio commits. |
|
||||
|
||||
## Return value
|
||||
|
||||
A Promise resolves to a prepared conversation after options and sign-in are checked. In a browser, preparation opens the Puter sign-in prompt if needed. Preparation opens no realtime connection and requests no microphone access.
|
||||
|
||||
The conversation exposes these properties and methods.
|
||||
|
||||
| Member | Behavior |
|
||||
| --- | --- |
|
||||
| `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 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. 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. |
|
||||
| `close()` | Closes the session, rejects pending operations, and releases SDK-owned capture and playback resources. Repeated calls have no additional effect. |
|
||||
|
||||
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. 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. 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.
|
||||
|
||||
The conversation makes up to ten attempts over about a minute. In a browser that reports being offline, it first waits up to two minutes for the connection to come back. If every attempt fails, or the server refuses with an error such as `insufficient_funds`, the conversation closes. A closed conversation cannot reconnect. To start another session, prepare a new one.
|
||||
|
||||
Native event definitions are listed in the [OpenAI realtime reference](https://developers.openai.com/api/reference/resources/realtime/server-events). Native events retain their provider fields. Sending native events preserves direct control of the session. Puter refuses `conversation.item.retrieve` events and MCP tools (`"type": "mcp"`) in `session.update` and `response.create` events. Sending one fires an `error` event with code `invalid_event`, and the conversation closes with code 1008. Audio helpers expect the session to use PCM16 at 24 kHz.
|
||||
|
||||
## Errors and limits
|
||||
|
||||
Failures use objects containing `code` and `message`. Preparation can reject with `invalid_argument`, `model_not_found`, `auth_required`, `auth_canceled`, or `auth_failed`. Connection can reject with `subscription_required` or `insufficient_funds`, which also show the Puter upgrade prompt. Connection and asynchronous operations can reject with provider errors, `session_closed`, `connection_lost`, `operation_timeout`, or `operation_in_progress`. Sending before readiness, or while reconnecting, throws `session_not_ready`.
|
||||
|
||||
Invalid input throws `invalid_argument`, `invalid_event`, or `input_too_large`. A full SDK send queue throws `queue_full`. Media helpers report `unsupported_environment`, `permission_denied`, `media_error`, `media_timeout`, or `queue_full`. Runtime media errors and `context_restore_failed` arrive through the `error` event. Provider errors also arrive as native `error` events with a nested `error` object.
|
||||
|
||||
Each native event is capped at 64 KiB. The SDK bounds JSON traversal, outgoing queues, acknowledgment waits, microphone work, and playback. All numeric limits are listed on [rate limits and quotas](/rate-limits-and-quotas/).
|
||||
|
||||
## Convenience events
|
||||
|
||||
The conversation emits these events alongside native events.
|
||||
|
||||
| Event | Fields |
|
||||
| --- | --- |
|
||||
| `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`. |
|
||||
| `interrupted` | `source` (`manual` or `speech`), `responseId` |
|
||||
| `turn.done` | `responseId`, native provider `usage`, `status` |
|
||||
| `close` | `code`, `reason` |
|
||||
|
||||
Each event includes its `type`. When the provider event leaves out an ID, `responseId` and `itemId` are empty strings and `contentIndex` is `0`. The `interrupted` event signals local interruption immediately. Its corresponding operation resolves after the provider acknowledges cancellation and any requested truncation. Late audio from a canceled response remains visible as native events and is excluded from convenience audio and built-in playback.
|
||||
|
||||
## Browser media
|
||||
|
||||
The optional microphone and speaker helpers require a browser with Web Audio support. Microphone capture also requires media permissions and a secure context. Start audio from a user action so the browser can allow playback.
|
||||
|
||||
| Member | Behavior |
|
||||
| --- | --- |
|
||||
| `microphone.start()` | Requests microphone access and streams PCM16, resampled to 24 kHz, in 20 ms chunks through an AudioWorklet. If the page or network falls behind, chunks are dropped rather than stopping capture. |
|
||||
| `microphone.mute()` | Disables capture tracks and stops forwarding captured chunks. |
|
||||
| `microphone.unmute()` | Enables capture tracks and forwarding. |
|
||||
| `microphone.stop()` | Stops capture tracks and releases capture resources. A `start()` still waiting on the permission prompt rejects with `session_closed`. |
|
||||
| `speaker.start({ bargeIn: true })` | Starts playback. Barge-in defaults to `true`. With barge-in enabled, provider speech-start events interrupt playback and synchronize played audio with conversation history. Starting the speaker requests no microphone access. |
|
||||
| `speaker.stop()` | Clears playback and releases its audio context. |
|
||||
|
||||
Both start methods require a connected session. Capture and playback are independent, so custom audio input can use the built-in speaker, and built-in capture can use custom playback.
|
||||
|
||||
## Examples
|
||||
|
||||
### Have a text conversation
|
||||
|
||||
A text conversation uses convenience methods for input and streamed output.
|
||||
|
||||
```js
|
||||
const conversation = await puter.ai.convo({
|
||||
output: ['text'],
|
||||
instructions: 'Keep replies short.',
|
||||
});
|
||||
|
||||
conversation.on('text', ({ delta }) => console.log(delta));
|
||||
conversation.on('turn.done', () => conversation.close());
|
||||
conversation.on('error', (event) => console.error(event));
|
||||
|
||||
await conversation.connect();
|
||||
conversation.sendText('Say hello in three words.');
|
||||
```
|
||||
|
||||
### Have a voice conversation
|
||||
|
||||
A voice conversation enables the microphone and speaker after connection.
|
||||
|
||||
```js
|
||||
const conversation = await puter.ai.convo();
|
||||
conversation.on('transcript', ({ role, text }) => console.log(role, text));
|
||||
conversation.on('error', (event) => console.error(event));
|
||||
|
||||
await conversation.connect();
|
||||
await conversation.speaker.start();
|
||||
await conversation.microphone.start();
|
||||
|
||||
// To interrupt a response and submit a replacement text turn:
|
||||
await conversation.interrupt();
|
||||
conversation.sendText('Actually, make it Wednesday.');
|
||||
|
||||
// When the user ends the conversation:
|
||||
conversation.close();
|
||||
```
|
||||
@@ -1,126 +0,0 @@
|
||||
---
|
||||
title: Realtime WebSocket API (Beta)
|
||||
description: Connect to OpenAI realtime conversations through Puter without puter.js.
|
||||
platforms: [websites, apps, nodejs, workers]
|
||||
---
|
||||
|
||||
The Realtime WebSocket API lets your client send and receive native OpenAI realtime JSON events through Puter.
|
||||
|
||||
Puter authenticates the user and charges their account for usage.
|
||||
|
||||
**Beta:** This API is incomplete. Its methods, options, events, and behavior may change abruptly, including breaking changes without notice.
|
||||
|
||||
For optional text, microphone, playback, and interruption helpers, use [`puter.ai.realtime()`](/AI/realtime/).
|
||||
|
||||
## Connection and authentication
|
||||
|
||||
Connect to this URL on the Puter API host:
|
||||
|
||||
```text
|
||||
wss://api.puter.com/puterai/openai/v1/realtime?model=gpt-realtime-2.1-mini
|
||||
```
|
||||
|
||||
The `model` query parameter defaults to `gpt-realtime-2.1-mini`. The model is fixed for the connection. A session update that changes it is rejected. It also accepts `gpt-realtime-2.1`, `gpt-realtime-2`, `gpt-realtime-1.5`, `gpt-realtime`, and `gpt-realtime-mini`.
|
||||
|
||||
Authenticate with a Puter session, app, or full-access API token. Puter supplies the provider credential. Server clients pass this header:
|
||||
|
||||
```text
|
||||
Authorization: Bearer <PUTER_TOKEN>
|
||||
```
|
||||
|
||||
Browser WebSocket clients pass `realtime` and `openai-insecure-api-key.<PUTER_TOKEN>` as subprotocols. The server echoes only `realtime`. The endpoint also accepts an `auth_token` query parameter. Prefer the authorization header where supported because URLs and subprotocols can appear in logs. Authentication takes precedence in this order, header, subprotocol, then query parameter.
|
||||
|
||||
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:
|
||||
|
||||
```js
|
||||
import WebSocket from 'ws';
|
||||
|
||||
const socket = new WebSocket(
|
||||
'wss://api.puter.com/puterai/openai/v1/realtime?model=gpt-realtime-2.1-mini',
|
||||
{ headers: { Authorization: `Bearer ${process.env.PUTER_TOKEN}` } },
|
||||
);
|
||||
const send = (event) => socket.send(JSON.stringify(event));
|
||||
|
||||
socket.on('message', (data) => {
|
||||
const event = JSON.parse(data.toString());
|
||||
|
||||
if (event.type === 'session.created') {
|
||||
send({
|
||||
type: 'session.update',
|
||||
session: {
|
||||
type: 'realtime',
|
||||
output_modalities: ['text'],
|
||||
instructions: 'Keep replies short.',
|
||||
},
|
||||
});
|
||||
}
|
||||
if (event.type === 'session.updated') {
|
||||
send({
|
||||
type: 'conversation.item.create',
|
||||
item: {
|
||||
type: 'message',
|
||||
role: 'user',
|
||||
content: [{ type: 'input_text', text: 'Hello!' }],
|
||||
},
|
||||
});
|
||||
send({ type: 'response.create' });
|
||||
}
|
||||
if (event.type === 'response.output_text.delta')
|
||||
process.stdout.write(event.delta);
|
||||
if (event.type === 'error')
|
||||
console.error(event.error);
|
||||
if (event.type === 'response.done')
|
||||
socket.close();
|
||||
});
|
||||
socket.on('error', console.error);
|
||||
socket.on('close', (code, reason) => console.log(code, reason.toString()));
|
||||
```
|
||||
|
||||
## Events, audio, and interruption
|
||||
|
||||
Each message contains one JSON object with a nonempty string `type` of at most 128 characters. Binary messages are rejected. Native events retain their provider fields. The endpoint doesn't deliver `rate_limits.updated`, which describes Puter's provider account rather than yours, and it removes provider organization IDs from error messages. The endpoint exposes the WebSocket event protocol. Your client manages capture, playback, acknowledgments, and timeouts.
|
||||
|
||||
The endpoint refuses `conversation.item.retrieve` events and MCP tools (`"type": "mcp"`) in `session.update` and `response.create` events. A refused event isn't sent to the provider. The session gets an `invalid_event` error and closes with 1008.
|
||||
|
||||
PCM16 audio configured at 24 kHz mono is sent as base64 chunks in `input_audio_buffer.append` events. Output arrives in `response.output_audio.delta` events. With manual turn detection, commit buffered input with `input_audio_buffer.commit`, then request a reply with `response.create`.
|
||||
|
||||
To interrupt, stop local playback immediately and send `response.cancel` with the active `response_id`. If some generated audio was not played, send `conversation.item.truncate` with its `item_id`, `content_index`, and `audio_end_ms` set to the amount actually played. Wait for `response.done` and any requested `conversation.item.truncated` acknowledgment before submitting a replacement turn.
|
||||
|
||||
Connection loss ends the session. Reconnecting creates a new conversation. Tool execution and automatic reconnection belong to your client.
|
||||
|
||||
## Server restarts
|
||||
|
||||
When Puter knows ahead of time that the server a session runs on will restart, it sends each open session this event, at most once a minute:
|
||||
|
||||
```json
|
||||
{
|
||||
"type": "puter.reconnect",
|
||||
"event_id": "event_puter_1767225600000",
|
||||
"reason": "server_restarting",
|
||||
"close_in_ms": 90000
|
||||
}
|
||||
```
|
||||
|
||||
The `reason` field is `server_restarting`. The `close_in_ms` field, when present, is about how long until the server closes the session itself. The session keeps working until then. Open a new connection after a short, randomized delay, and once it gets `session.created` and your client is idle, with no response generating and no user turn in progress, move to it and close the old one. A connection that reaches a server that is also restarting gets a `server_restarting` error and closes with 1012. In that case keep the old session and try again after a short, randomized delay. If the server closes the old session first, the close code is 1012, and your client reconnects the same way. A restart can also drop the connection without any notice, so treat an unexpected close the same way.
|
||||
|
||||
A new connection starts an empty conversation. To keep context, add earlier items back with `conversation.item.create` before you request a response. Event types that start with `puter.` come from Puter, not the provider.
|
||||
|
||||
## Errors, billing, and limits
|
||||
|
||||
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, 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.
|
||||
+96
-121
@@ -1,151 +1,126 @@
|
||||
---
|
||||
title: puter.ai.realtime()
|
||||
description: Prepare an OpenAI realtime conversation with optional audio and text helpers.
|
||||
title: Realtime WebSocket API (Beta)
|
||||
description: Connect to OpenAI realtime conversations through Puter without puter.js.
|
||||
platforms: [websites, apps, nodejs, workers]
|
||||
---
|
||||
|
||||
Prepares a realtime OpenAI conversation that your app can connect to when it's ready.
|
||||
The Realtime WebSocket API lets your client send and receive native OpenAI realtime JSON events through Puter.
|
||||
|
||||
Preparing the conversation verifies Puter sign-in. Realtime conversations require a paid Puter plan, and usage is billed to the signed-in user. Native OpenAI events and convenience methods use the same session. Microphone capture and speaker playback start when your app requests them.
|
||||
|
||||
In Node.js, the SDK requires a global `WebSocket`, available in Node.js 22 or later.
|
||||
Puter authenticates the user and charges their account for usage.
|
||||
|
||||
**Beta:** This API is incomplete. Its methods, options, events, and behavior may change abruptly, including breaking changes without notice.
|
||||
|
||||
To connect without puter.js, see the [Realtime WebSocket API](/AI/realtime-websocket/).
|
||||
For optional text, microphone, playback, and interruption helpers, use [`puter.ai.convo()`](/AI/convo/).
|
||||
|
||||
## Syntax
|
||||
## Connection and authentication
|
||||
|
||||
```js
|
||||
const conversation = await puter.ai.realtime(options = {});
|
||||
await conversation.connect();
|
||||
Connect to this URL on the Puter API host:
|
||||
|
||||
```text
|
||||
wss://api.puter.com/puterai/openai/v1/realtime?model=gpt-realtime-2.1-mini
|
||||
```
|
||||
|
||||
## Parameters
|
||||
The `model` query parameter defaults to `gpt-realtime-2.1-mini`. The model is fixed for the connection. A session update that changes it is rejected. It also accepts `gpt-realtime-2.1`, `gpt-realtime-2`, `gpt-realtime-1.5`, `gpt-realtime`, and `gpt-realtime-mini`.
|
||||
|
||||
#### `options` (Object) (Optional)
|
||||
Authenticate with a Puter session, app, or full-access API token. Puter supplies the provider credential. Server clients pass this header:
|
||||
|
||||
The options object accepts these fields.
|
||||
```text
|
||||
Authorization: Bearer <PUTER_TOKEN>
|
||||
```
|
||||
|
||||
| Option | Type | Description |
|
||||
|--------|------|-------------|
|
||||
| `model` | `String` | The realtime model. Defaults to `gpt-realtime-2.1-mini`. Also accepts `gpt-realtime-2.1`, `gpt-realtime-2`, `gpt-realtime-1.5`, `gpt-realtime`, and `gpt-realtime-mini`. |
|
||||
| `instructions` | `String` | Session instructions, up to 8192 characters. Defaults to the provider's default. |
|
||||
| `output` | `Array<String>` | Accepts `['text']`, `['audio']`, or both. Defaults to `['audio']`. Audio includes a transcript. Requesting both uses audio output and emits transcript deltas through the `text` event. |
|
||||
| `voice` | `String` | OpenAI voice identifier, up to 128 characters. Defaults to the provider's default. |
|
||||
| `turnDetection` | `Boolean` | Whether the provider detects the end of spoken turns automatically. Defaults to `true`. Set to `false` for manual audio commits. |
|
||||
Browser WebSocket clients pass `realtime` and `openai-insecure-api-key.<PUTER_TOKEN>` as subprotocols. The server echoes only `realtime`. The endpoint also accepts an `auth_token` query parameter. Prefer the authorization header where supported because URLs and subprotocols can appear in logs. Authentication takes precedence in this order, header, subprotocol, then query parameter.
|
||||
|
||||
## Return value
|
||||
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.
|
||||
|
||||
A Promise resolves to a prepared conversation after options and sign-in are checked. In a browser, preparation opens the Puter sign-in prompt if needed. Preparation opens no realtime connection and requests no microphone access.
|
||||
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/).
|
||||
|
||||
The conversation exposes these properties and methods.
|
||||
## Text example
|
||||
|
||||
| Member | Behavior |
|
||||
| --- | --- |
|
||||
| `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 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. 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. |
|
||||
| `close()` | Closes the session, rejects pending operations, and releases SDK-owned capture and playback resources. Repeated calls have no additional effect. |
|
||||
|
||||
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. 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. 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.
|
||||
|
||||
The conversation makes up to ten attempts over about a minute. In a browser that reports being offline, it first waits up to two minutes for the connection to come back. If every attempt fails, or the server refuses with an error such as `insufficient_funds`, the conversation closes. A closed conversation cannot reconnect. To start another session, prepare a new one.
|
||||
|
||||
Native event definitions are listed in the [OpenAI realtime reference](https://developers.openai.com/api/reference/resources/realtime/server-events). Native events retain their provider fields. Sending native events preserves direct control of the session. Puter refuses `conversation.item.retrieve` events and MCP tools (`"type": "mcp"`) in `session.update` and `response.create` events. Sending one fires an `error` event with code `invalid_event`, and the conversation closes with code 1008. Audio helpers expect the session to use PCM16 at 24 kHz.
|
||||
|
||||
## Errors and limits
|
||||
|
||||
Failures use objects containing `code` and `message`. Preparation can reject with `invalid_argument`, `model_not_found`, `auth_required`, `auth_canceled`, or `auth_failed`. Connection can reject with `subscription_required` or `insufficient_funds`, which also show the Puter upgrade prompt. Connection and asynchronous operations can reject with provider errors, `session_closed`, `connection_lost`, `operation_timeout`, or `operation_in_progress`. Sending before readiness, or while reconnecting, throws `session_not_ready`.
|
||||
|
||||
Invalid input throws `invalid_argument`, `invalid_event`, or `input_too_large`. A full SDK send queue throws `queue_full`. Media helpers report `unsupported_environment`, `permission_denied`, `media_error`, `media_timeout`, or `queue_full`. Runtime media errors and `context_restore_failed` arrive through the `error` event. Provider errors also arrive as native `error` events with a nested `error` object.
|
||||
|
||||
Each native event is capped at 64 KiB. The SDK bounds JSON traversal, outgoing queues, acknowledgment waits, microphone work, and playback. All numeric limits are listed on [rate limits and quotas](/rate-limits-and-quotas/).
|
||||
|
||||
## Convenience events
|
||||
|
||||
The conversation emits these events alongside native events.
|
||||
|
||||
| Event | Fields |
|
||||
| --- | --- |
|
||||
| `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`. |
|
||||
| `interrupted` | `source` (`manual` or `speech`), `responseId` |
|
||||
| `turn.done` | `responseId`, native provider `usage`, `status` |
|
||||
| `close` | `code`, `reason` |
|
||||
|
||||
Each event includes its `type`. When the provider event leaves out an ID, `responseId` and `itemId` are empty strings and `contentIndex` is `0`. The `interrupted` event signals local interruption immediately. Its corresponding operation resolves after the provider acknowledges cancellation and any requested truncation. Late audio from a canceled response remains visible as native events and is excluded from convenience audio and built-in playback.
|
||||
|
||||
## Browser media
|
||||
|
||||
The optional microphone and speaker helpers require a browser with Web Audio support. Microphone capture also requires media permissions and a secure context. Start audio from a user action so the browser can allow playback.
|
||||
|
||||
| Member | Behavior |
|
||||
| --- | --- |
|
||||
| `microphone.start()` | Requests microphone access and streams PCM16, resampled to 24 kHz, in 20 ms chunks through an AudioWorklet. If the page or network falls behind, chunks are dropped rather than stopping capture. |
|
||||
| `microphone.mute()` | Disables capture tracks and stops forwarding captured chunks. |
|
||||
| `microphone.unmute()` | Enables capture tracks and forwarding. |
|
||||
| `microphone.stop()` | Stops capture tracks and releases capture resources. A `start()` still waiting on the permission prompt rejects with `session_closed`. |
|
||||
| `speaker.start({ bargeIn: true })` | Starts playback. Barge-in defaults to `true`. With barge-in enabled, provider speech-start events interrupt playback and synchronize played audio with conversation history. Starting the speaker requests no microphone access. |
|
||||
| `speaker.stop()` | Clears playback and releases its audio context. |
|
||||
|
||||
Both start methods require a connected session. Capture and playback are independent, so custom audio input can use the built-in speaker, and built-in capture can use custom playback.
|
||||
|
||||
## Examples
|
||||
|
||||
### Have a text conversation
|
||||
|
||||
A text conversation uses convenience methods for input and streamed output.
|
||||
Install the `ws` package and set `PUTER_TOKEN` to your Puter token. Run this example as a Node.js ES module:
|
||||
|
||||
```js
|
||||
const conversation = await puter.ai.realtime({
|
||||
output: ['text'],
|
||||
instructions: 'Keep replies short.',
|
||||
import WebSocket from 'ws';
|
||||
|
||||
const socket = new WebSocket(
|
||||
'wss://api.puter.com/puterai/openai/v1/realtime?model=gpt-realtime-2.1-mini',
|
||||
{ headers: { Authorization: `Bearer ${process.env.PUTER_TOKEN}` } },
|
||||
);
|
||||
const send = (event) => socket.send(JSON.stringify(event));
|
||||
|
||||
socket.on('message', (data) => {
|
||||
const event = JSON.parse(data.toString());
|
||||
|
||||
if (event.type === 'session.created') {
|
||||
send({
|
||||
type: 'session.update',
|
||||
session: {
|
||||
type: 'realtime',
|
||||
output_modalities: ['text'],
|
||||
instructions: 'Keep replies short.',
|
||||
},
|
||||
});
|
||||
}
|
||||
if (event.type === 'session.updated') {
|
||||
send({
|
||||
type: 'conversation.item.create',
|
||||
item: {
|
||||
type: 'message',
|
||||
role: 'user',
|
||||
content: [{ type: 'input_text', text: 'Hello!' }],
|
||||
},
|
||||
});
|
||||
send({ type: 'response.create' });
|
||||
}
|
||||
if (event.type === 'response.output_text.delta')
|
||||
process.stdout.write(event.delta);
|
||||
if (event.type === 'error')
|
||||
console.error(event.error);
|
||||
if (event.type === 'response.done')
|
||||
socket.close();
|
||||
});
|
||||
|
||||
conversation.on('text', ({ delta }) => console.log(delta));
|
||||
conversation.on('turn.done', () => conversation.close());
|
||||
conversation.on('error', (event) => console.error(event));
|
||||
|
||||
await conversation.connect();
|
||||
conversation.sendText('Say hello in three words.');
|
||||
socket.on('error', console.error);
|
||||
socket.on('close', (code, reason) => console.log(code, reason.toString()));
|
||||
```
|
||||
|
||||
### Have a voice conversation
|
||||
## Events, audio, and interruption
|
||||
|
||||
A voice conversation enables the microphone and speaker after connection.
|
||||
Each message contains one JSON object with a nonempty string `type` of at most 128 characters. Binary messages are rejected. Native events retain their provider fields. The endpoint doesn't deliver `rate_limits.updated`, which describes Puter's provider account rather than yours, and it removes provider organization IDs from error messages. The endpoint exposes the WebSocket event protocol. Your client manages capture, playback, acknowledgments, and timeouts.
|
||||
|
||||
```js
|
||||
const conversation = await puter.ai.realtime();
|
||||
conversation.on('transcript', ({ role, text }) => console.log(role, text));
|
||||
conversation.on('error', (event) => console.error(event));
|
||||
The endpoint refuses `conversation.item.retrieve` events and MCP tools (`"type": "mcp"`) in `session.update` and `response.create` events. A refused event isn't sent to the provider. The session gets an `invalid_event` error and closes with 1008.
|
||||
|
||||
await conversation.connect();
|
||||
await conversation.speaker.start();
|
||||
await conversation.microphone.start();
|
||||
PCM16 audio configured at 24 kHz mono is sent as base64 chunks in `input_audio_buffer.append` events. Output arrives in `response.output_audio.delta` events. With manual turn detection, commit buffered input with `input_audio_buffer.commit`, then request a reply with `response.create`.
|
||||
|
||||
// To interrupt a response and submit a replacement text turn:
|
||||
await conversation.interrupt();
|
||||
conversation.sendText('Actually, make it Wednesday.');
|
||||
To interrupt, stop local playback immediately and send `response.cancel` with the active `response_id`. If some generated audio was not played, send `conversation.item.truncate` with its `item_id`, `content_index`, and `audio_end_ms` set to the amount actually played. Wait for `response.done` and any requested `conversation.item.truncated` acknowledgment before submitting a replacement turn.
|
||||
|
||||
// When the user ends the conversation:
|
||||
conversation.close();
|
||||
Connection loss ends the session. Reconnecting creates a new conversation. Tool execution and automatic reconnection belong to your client.
|
||||
|
||||
## Server restarts
|
||||
|
||||
When Puter knows ahead of time that the server a session runs on will restart, it sends each open session this event, at most once a minute:
|
||||
|
||||
```json
|
||||
{
|
||||
"type": "puter.reconnect",
|
||||
"event_id": "event_puter_1767225600000",
|
||||
"reason": "server_restarting",
|
||||
"close_in_ms": 90000
|
||||
}
|
||||
```
|
||||
|
||||
The `reason` field is `server_restarting`. The `close_in_ms` field, when present, is about how long until the server closes the session itself. The session keeps working until then. Open a new connection after a short, randomized delay, and once it gets `session.created` and your client is idle, with no response generating and no user turn in progress, move to it and close the old one. A connection that reaches a server that is also restarting gets a `server_restarting` error and closes with 1012. In that case keep the old session and try again after a short, randomized delay. If the server closes the old session first, the close code is 1012, and your client reconnects the same way. A restart can also drop the connection without any notice, so treat an unexpected close the same way.
|
||||
|
||||
A new connection starts an empty conversation. To keep context, add earlier items back with `conversation.item.create` before you request a response. Event types that start with `puter.` come from Puter, not the provider.
|
||||
|
||||
## Errors, billing, and limits
|
||||
|
||||
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, 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]
|
||||
|
||||
Initiates the sign in process for the user.
|
||||
|
||||
This will open a popup window with the appropriate authentication method. Puter automatically handles the authentication process and will resolve the promise when the user has signed in. Puter's own apps on puter.com subdomains show the sign-in in a frame over the page instead, and open the popup when the user picks a sign-in provider such as Google.
|
||||
This will open a popup window with the appropriate authentication method. Puter automatically handles the authentication process and will resolve the promise when the user has signed in.
|
||||
|
||||
It is important to note that all essential methods in Puter handle authentication automatically. This method is only necessary if you want to handle authentication manually, for example if you want to build your own custom authentication flow.
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ Opens a dialog where the user can send you, the app's developer, feedback about
|
||||
|
||||
The message is delivered by Puter: it is stored and emailed to the email address on your Puter account. The feedback never passes through your app's code, and the dialog tells the user what is shared with you: their username, plus their email address (as the email's reply-to, so you can respond) when their email is verified.
|
||||
|
||||
Inside Puter, the dialog is rendered by the desktop environment. On a website, a puter.com popup hosts the dialog (signing the user in first if needed). Puter's own apps on puter.com subdomains show it in a frame over the page instead, when the user is signed in to both.
|
||||
Inside Puter, the dialog is rendered by the desktop environment. On a website, a puter.com popup hosts the dialog (signing the user in first if needed).
|
||||
|
||||
**Feedback is opt-in.** Users can only send feedback if it's enabled for your app. Apps created in the Dev Center have feedback enabled at creation — you can turn it off with the "User Feedback" toggle in the app's settings. Apps created through `puter.apps.create` default to off; enable it by setting `feedbackEnabled`:
|
||||
|
||||
|
||||
@@ -91,7 +91,7 @@ Gemini speech-to-text first counts the audio's tokens, which times out after 30
|
||||
|
||||
The OpenAI- and Anthropic-compatible endpoints (`/puterai/openai/v1/*`, `/puterai/anthropic/v1/messages`) require a paid plan; a free account gets `402 subscription_required`. The same models are available to every account through `puter.ai.*` and `/drivers/call`, and the model catalogue endpoints are open to everyone.
|
||||
|
||||
Realtime sessions ([`puter.ai.realtime()`](/AI/realtime/), `/puterai/openai/v1/realtime`) require a paid plan, like the other OpenAI-compatible endpoints, and are capped at 20 open at once per account. An account without a plan gets an `error` event with code `subscription_required`. A session over the cap gets `rate_limit_exceeded`. Both close with 1008.
|
||||
Realtime sessions ([`puter.ai.convo()`](/AI/convo/), `/puterai/openai/v1/realtime`) require a paid plan, like the other OpenAI-compatible endpoints, and are capped at 20 open at once per account. An account without a plan gets an `error` event with code `subscription_required`. A session over the cap gets `rate_limit_exceeded`. Both close with 1008.
|
||||
|
||||
| Realtime transport limit | Value | Behavior at the limit |
|
||||
| --- | --- | --- |
|
||||
@@ -131,7 +131,7 @@ Realtime sessions ([`puter.ai.realtime()`](/AI/realtime/), `/puterai/openai/v1/r
|
||||
|
||||
Send audio as a stream of small chunks. The message cap applies to text, configuration, tool output, and audio alike.
|
||||
|
||||
The [`puter.ai.realtime()`](/AI/realtime/) SDK also applies these limits before sending or retaining data.
|
||||
The [`puter.ai.convo()`](/AI/convo/) SDK also applies these limits before sending or retaining data.
|
||||
|
||||
| Conversation SDK limit | Value | Behavior at the limit |
|
||||
| --- | --- | --- |
|
||||
|
||||
@@ -124,17 +124,17 @@ let sidebar = [
|
||||
path: '/AI/speech2speech',
|
||||
},
|
||||
{
|
||||
title: '<code>realtime()</code>',
|
||||
page_title: '<code>puter.ai.realtime()</code>',
|
||||
title_tag: 'puter.ai.realtime()',
|
||||
title: '<code>convo()</code>',
|
||||
page_title: '<code>puter.ai.convo()</code>',
|
||||
title_tag: 'puter.ai.convo()',
|
||||
icon: '/assets/img/function.svg',
|
||||
source: '/AI/realtime.md',
|
||||
path: '/AI/realtime',
|
||||
source: '/AI/convo.md',
|
||||
path: '/AI/convo',
|
||||
},
|
||||
{
|
||||
title: 'Realtime WebSocket API (Beta)',
|
||||
source: '/AI/realtime-websocket.md',
|
||||
path: '/AI/realtime-websocket',
|
||||
source: '/AI/realtime.md',
|
||||
path: '/AI/realtime',
|
||||
},
|
||||
{
|
||||
title: '<code>listModels()</code>',
|
||||
|
||||
@@ -225,29 +225,6 @@ describe('OIDC buttons on the login and signup screens', () => {
|
||||
window.location = original;
|
||||
expect(assign.mock.calls[0][0]).toBe('https://puter.test/auth/oidc/google/start?flow=login&referrer=ref');
|
||||
});
|
||||
|
||||
it('hands a framed sign-in to the popup instead of leaving for the provider', async () => {
|
||||
asPopup('msg_id=1');
|
||||
window.embedded_in_frame = true;
|
||||
window.tell_popup_frame = vi.fn();
|
||||
try {
|
||||
void UIAuthLogin();
|
||||
await vi.waitFor(() => expect($('.oidc-google-btn')).toHaveLength(1));
|
||||
const href = window.location.href;
|
||||
$('.oidc-google-btn').trigger('click');
|
||||
expect(window.tell_popup_frame.mock.calls).toEqual([
|
||||
['provider', { url: expect.stringContaining('https://puter.test/auth/oidc/google/start?flow=login') }],
|
||||
['unavailable'],
|
||||
]);
|
||||
const start = new URL(window.tell_popup_frame.mock.calls[0][1].url);
|
||||
expect(start.searchParams.get('embedded_in_popup')).toBe('true');
|
||||
expect(start.searchParams.get('msg_id')).toBe('1');
|
||||
expect(window.location.href).toBe(href);
|
||||
} finally {
|
||||
delete window.embedded_in_frame;
|
||||
delete window.tell_popup_frame;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('second factor', () => {
|
||||
|
||||
@@ -191,15 +191,9 @@ export default function openAuthFlow ({ page = false, dismissable = ! page, clos
|
||||
/** @type {Array<AuthScreen & { $el: jQuery, el_return?: Element | null }>} */
|
||||
const stack = [];
|
||||
|
||||
// A frame's focus() calls do nothing until the framing page focuses it,
|
||||
// so the screen takes focus then, unless something already has it.
|
||||
const onFrameFocus = () => {
|
||||
if ( ! document.activeElement || document.activeElement === document.body ) flow.focus();
|
||||
};
|
||||
const teardown = () => {
|
||||
if ( torn_down ) return;
|
||||
torn_down = true;
|
||||
window.removeEventListener('focus', onFrameFocus);
|
||||
if ( active_flow === flow ) active_flow = null;
|
||||
// Screens waiting on the user (the second factor) hear they won't get an answer.
|
||||
while ( stack.length ) stack.pop().onLeave?.();
|
||||
@@ -358,14 +352,8 @@ export default function openAuthFlow ({ page = false, dismissable = ! page, clos
|
||||
$overlay.on('keydown keyup keypress', function (e) {
|
||||
e.stopPropagation();
|
||||
if ( e.type !== 'keydown' || e.key !== 'Escape' ) return;
|
||||
// An Escape that cancels an IME conversion is the IME's; jQuery's
|
||||
// event doesn't carry isComposing, the browser's does.
|
||||
if ( e.originalEvent?.isComposing || e.keyCode === 229 ) return;
|
||||
e.preventDefault();
|
||||
if ( busy ) return;
|
||||
if ( dismissable ) $dialog.find('.dashboard-dialog-close').trigger('click');
|
||||
// A framed sign-in's ✕ is the framing page's, which close() asks.
|
||||
else if ( window.embedded_in_frame ) window.close();
|
||||
if ( dismissable && ! busy ) $dialog.find('.dashboard-dialog-close').trigger('click');
|
||||
});
|
||||
// A press on the backdrop (or a page's background) would move focus to
|
||||
// the page, and with it the keys; it stays where it was.
|
||||
@@ -373,8 +361,6 @@ export default function openAuthFlow ({ page = false, dismissable = ! page, clos
|
||||
if ( e.target === this ) e.preventDefault();
|
||||
});
|
||||
|
||||
if ( window.embedded_in_frame ) window.addEventListener('focus', onFrameFocus);
|
||||
|
||||
active_flow = flow;
|
||||
return flow;
|
||||
}
|
||||
|
||||
@@ -75,56 +75,6 @@ describe('openAuthFlow', () => {
|
||||
expect(flow.isClosed()).toBe(false);
|
||||
});
|
||||
|
||||
it('lets Escape ask the framing page to close a framed sign-in, unless busy', async () => {
|
||||
window.embedded_in_frame = true;
|
||||
const close = vi.spyOn(window, 'close').mockImplementation(() => {});
|
||||
try {
|
||||
const flow = openAuthFlow({ page: true });
|
||||
flow.push({ name: 'a', html: '<input>' });
|
||||
await tick();
|
||||
flow.setBusy(true);
|
||||
key($('.auth-dialog input').get(0), 'Escape');
|
||||
expect(close).not.toHaveBeenCalled();
|
||||
flow.setBusy(false);
|
||||
// Escape that cancels an IME conversion is the IME's.
|
||||
$('.auth-dialog input').get(0).dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', isComposing: true, bubbles: true }));
|
||||
expect(close).not.toHaveBeenCalled();
|
||||
key($('.auth-dialog input').get(0), 'Escape');
|
||||
expect(close).toHaveBeenCalledTimes(1);
|
||||
flow.finish(false);
|
||||
} finally {
|
||||
close.mockRestore();
|
||||
delete window.embedded_in_frame;
|
||||
}
|
||||
});
|
||||
|
||||
it('focuses a framed screen once the framing page focuses the frame', async () => {
|
||||
window.embedded_in_frame = true;
|
||||
try {
|
||||
const flow = openAuthFlow({ page: true });
|
||||
flow.push({ name: 'a', html: '<input>' });
|
||||
// What a frame's focus() amounts to before the page focuses it.
|
||||
document.activeElement.blur();
|
||||
window.dispatchEvent(new FocusEvent('focus'));
|
||||
// jsdom lays nothing out, so the screen's fallback, the card, takes it.
|
||||
expect($('.auth-dialog').get(0).contains(document.activeElement)).toBe(true);
|
||||
|
||||
// Focus the user moved elsewhere stays there.
|
||||
const el_outside = $('<button class="outside">').appendTo('body').get(0);
|
||||
el_outside.focus();
|
||||
window.dispatchEvent(new FocusEvent('focus'));
|
||||
expect(document.activeElement).toBe(el_outside);
|
||||
el_outside.remove();
|
||||
|
||||
flow.finish(false);
|
||||
document.activeElement.blur();
|
||||
window.dispatchEvent(new FocusEvent('focus'));
|
||||
expect(document.activeElement).toBe(document.body);
|
||||
} finally {
|
||||
delete window.embedded_in_frame;
|
||||
}
|
||||
});
|
||||
|
||||
it('ignores the backdrop unless asked, so a stray click keeps typed credentials', async () => {
|
||||
const kept = openAuthFlow();
|
||||
kept.push({ name: 'a', html: '' });
|
||||
|
||||
@@ -134,15 +134,7 @@ export async function renderProviderButtons ($list, { kind, referrer, flow }) {
|
||||
}
|
||||
$list.html(h);
|
||||
$list.on('click', '.auth-provider-btn', function () {
|
||||
const url = providerStartUrl($(this).attr('data-provider'), kind, referrer());
|
||||
// A provider won't load in a frame, so the framing page opens this
|
||||
// in a popup instead. `unavailable` covers an SDK that can't.
|
||||
if ( window.embedded_in_frame ) {
|
||||
window.tell_popup_frame('provider', { url });
|
||||
window.tell_popup_frame('unavailable');
|
||||
return;
|
||||
}
|
||||
window.location.href = url;
|
||||
window.location.href = providerStartUrl($(this).attr('data-provider'), kind, referrer());
|
||||
});
|
||||
return { providers: ordered, signupOidcOnly: data.signupOidcOnly === true };
|
||||
}
|
||||
@@ -54,8 +54,7 @@ async function UIWindowAppFeedback (options) {
|
||||
let sending = false;
|
||||
let closed = false;
|
||||
|
||||
// The feedback embed runs without puter.js.
|
||||
const authToken = globalThis.puter?.authToken ?? window.auth_token;
|
||||
const authToken = puter.authToken ?? window.auth_token;
|
||||
const target_params = options.app
|
||||
? { app: options.app }
|
||||
: { origin: options.origin };
|
||||
|
||||
@@ -25,7 +25,7 @@ import { appTileLink } from './UI/Dashboard/appLink.js';
|
||||
import { installAppIconFallback } from './helpers/appIcon.js';
|
||||
import { guestAppLaunchUrl, launchAppInTab } from './helpers/appTabLaunch.js';
|
||||
import { embedHostTheme, forceColorScheme } from './helpers/embedHostTheme.js';
|
||||
import { embedSession } from './helpers/embedSession.js';
|
||||
import { embedHostUser } from './helpers/embedHostUser.js';
|
||||
|
||||
window.$ = window.jQuery = jQuery;
|
||||
window.html_encode = value => encode(String(value ?? ''), { mode: 'nonAsciiPrintable' });
|
||||
@@ -104,12 +104,56 @@ function launchTile ({ appName, targetLink }) {
|
||||
.finally(() => launching.delete(appName));
|
||||
}
|
||||
|
||||
/**
|
||||
* The GUI session, from local storage or else the session cookie. Safari keys a
|
||||
* frame's local storage by the top-level origin, so under a sibling subdomain
|
||||
* the GUI's stored token is out of reach while its cookie is still sent.
|
||||
*/
|
||||
async function readSession () {
|
||||
let stored = null;
|
||||
try {
|
||||
stored = localStorage.getItem('auth_token_v2');
|
||||
} catch { /* Storage the browser refuses holds no session; the cookie may. */ }
|
||||
if ( stored ) return stored;
|
||||
const response = await fetch('/get-gui-token', { credentials: 'include' });
|
||||
// Only a 401 means there is no session; a rate limit or server error
|
||||
// must not pass a signed-in user off as signed out.
|
||||
if ( response.status === 401 ) return null;
|
||||
if ( ! response.ok ) throw new Error(`Session lookup responded ${response.status}`);
|
||||
return (await response.json())?.token || null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The uuid of the user a GUI token belongs to, or null when the API refuses it.
|
||||
*/
|
||||
async function sessionUser (token) {
|
||||
const response = await fetch(new URL('/whoami', config.apiOrigin), {
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
});
|
||||
if ( response.status === 401 ) return null;
|
||||
if ( ! response.ok ) throw new Error(`Session check responded ${response.status}`);
|
||||
const uuid = (await response.json())?.uuid;
|
||||
return typeof uuid === 'string' ? uuid.toLowerCase() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The GUI session, only when it belongs to the user the host page names. A
|
||||
* host signed out, or signed in as someone else, gets the signed-out view.
|
||||
*/
|
||||
async function hostSession () {
|
||||
const hostUser = embedHostUser(window.location.hash);
|
||||
if ( ! hostUser ) return null;
|
||||
const token = await readSession();
|
||||
if ( ! token ) return null;
|
||||
return await sessionUser(token) === hostUser ? token : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Signed in, the user's own apps through the SDK. Without a session, the
|
||||
* recommended apps the page was rendered with, and no SDK.
|
||||
*/
|
||||
async function mount () {
|
||||
window.auth_token = (await embedSession(config.apiOrigin, window.location.hash))?.token ?? null;
|
||||
window.auth_token = await hostSession();
|
||||
const guestApps = window.auth_token ? undefined : config.guestApps;
|
||||
if ( !window.auth_token && !guestApps?.length ) throw new Error('No apps to show without a session');
|
||||
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
/* Transparent, so the dialog's own scrim covers the framing page. */
|
||||
html,
|
||||
body {
|
||||
margin: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
overflow: hidden;
|
||||
background: transparent;
|
||||
font-family: "Inter", "Helvetica Neue", Helvetica, Arial, sans-serif;
|
||||
}
|
||||
|
||||
/* A scroll over the scrim must not scroll the framing page behind it. */
|
||||
html {
|
||||
overscroll-behavior: none;
|
||||
/* Matches the frame's, so it stays transparent and follows the OS theme. */
|
||||
color-scheme: light dark;
|
||||
}
|
||||
@@ -181,20 +181,6 @@ input[type=text], input[type=password], input[type=email], select {
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
@media (prefers-color-scheme: dark) {
|
||||
input[type=text], input[type=password], input[type=email], select {
|
||||
background-color: #2d2d2d;
|
||||
border: 1px solid #555;
|
||||
color: #e6e6e6;
|
||||
}
|
||||
|
||||
input[type=text]::placeholder,
|
||||
input[type=password]::placeholder,
|
||||
input[type=email]::placeholder {
|
||||
color: #999;
|
||||
}
|
||||
}
|
||||
|
||||
/* to prevent auto-zoom on input focus in mobile */
|
||||
.device-phone input[type=text], .device-phone input[type=password], .device-phone input[type=email], .device-phone select {
|
||||
font-size: 17px;
|
||||
@@ -1492,18 +1478,6 @@ span.header-sort-icon img {
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
/* The file name shrinks so the buttons stay on one line, on screen, in a narrow dialog. */
|
||||
.window-filedialog-prompt > div {
|
||||
min-width: 0;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
@media (max-width: 480px) {
|
||||
.window-filedialog-prompt .button-small {
|
||||
padding: 0 16px;
|
||||
}
|
||||
}
|
||||
|
||||
.savefiledialog-filename {
|
||||
float: left;
|
||||
margin-right: 10px;
|
||||
@@ -1511,21 +1485,10 @@ span.header-sort-icon img {
|
||||
border-width: 1px !important;
|
||||
height: 31px;
|
||||
flex-grow: 1;
|
||||
min-width: 0;
|
||||
width: initial !important;
|
||||
}
|
||||
|
||||
/* In a narrow open dialog, the upload label gives way so its buttons stay whole. */
|
||||
.window-filedialog-upload-here + div {
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.window-filedialog-upload-here {
|
||||
min-width: 0;
|
||||
margin-right: 10px;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
opacity: 0.7;
|
||||
font-size: 14px;
|
||||
|
||||
@@ -1,252 +0,0 @@
|
||||
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';
|
||||
import { inflateSync } from 'node:zlib';
|
||||
|
||||
let browser;
|
||||
let server;
|
||||
let directory;
|
||||
let origin;
|
||||
let hostOrigin;
|
||||
let port;
|
||||
const requests = [];
|
||||
const guiRoot = fileURLToPath(new URL('../', import.meta.url));
|
||||
const sessionToken = 'feedback-embed-test-session';
|
||||
const unconfirmedToken = 'feedback-embed-test-unconfirmed';
|
||||
const sessionUser = '0b1c9a52-3f0e-4c7a-9a65-2f6d1e8b7c40';
|
||||
const unconfirmedUser = '1c2d3e4f-5a6b-4c7d-8e9f-0a1b2c3d4e5f';
|
||||
const otherUser = '7d2e4f10-8a3b-4c5d-9e6f-0a1b2c3d4e5f';
|
||||
|
||||
beforeAll(async () => {
|
||||
directory = await mkdtemp(path.join(tmpdir(), 'puter-feedback-embed-'));
|
||||
await new Promise((resolve, reject) => {
|
||||
const compiler = webpack({
|
||||
mode: 'development',
|
||||
entry: { 'feedback-embed': path.join(guiRoot, 'src/feedbackEmbed.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());
|
||||
app.use((req, res, next) => {
|
||||
requests.push({ path: req.path, query: req.query, 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('/get-gui-token', (_req, res) => res.sendStatus(401));
|
||||
const whoamiByToken = {
|
||||
[sessionToken]: { uuid: sessionUser, username: 'session-user', email: '[email protected]', email_confirmed: true },
|
||||
[unconfirmedToken]: {
|
||||
uuid: unconfirmedUser, username: 'new-user', email_confirmed: false, requires_email_confirmation: true,
|
||||
},
|
||||
};
|
||||
app.get('/whoami', (req, res) => {
|
||||
const user = whoamiByToken[req.headers.authorization?.replace(/^Bearer /, '')];
|
||||
if ( ! user ) return res.sendStatus(401);
|
||||
res.json(user);
|
||||
});
|
||||
app.get('/app-feedback/target', (req, res) => {
|
||||
if ( req.headers.authorization !== `Bearer ${sessionToken}` ) return res.sendStatus(401);
|
||||
if ( req.query.origin !== hostOrigin ) return res.json({ enabled: false });
|
||||
res.json({ enabled: true, app: { name: 'notes', title: 'Notes' } });
|
||||
});
|
||||
app.post('/app-feedback', (req, res) => {
|
||||
if ( req.headers.authorization !== `Bearer ${sessionToken}` ) return res.sendStatus(401);
|
||||
res.json({});
|
||||
});
|
||||
// A first-party page that frames the dialog as puter.js does, recording what the embed tells it.
|
||||
app.get('/host', (req, res) => res.send(`<!DOCTYPE html><html><body style="margin:0;background:rgb(255,0,0)">
|
||||
<script>
|
||||
window.received = [];
|
||||
addEventListener('message', (e) => {
|
||||
if ( e.origin === '${origin}' ) window.received.push(e.data);
|
||||
});
|
||||
</script>
|
||||
<iframe referrerpolicy="origin" title="Send feedback" src="${origin}/embed/feedback#user=${req.query.user ?? ''}"
|
||||
style="display:block;border:0;width:100vw;height:100vh;color-scheme:light dark"></iframe>
|
||||
<div style="height:3000px"></div>
|
||||
</body></html>`));
|
||||
app.get('/embed/feedback', (_req, res) => {
|
||||
res.setHeader('Content-Security-Policy', `frame-ancestors 'self' ${hostOrigin}`);
|
||||
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/feedback-embed.css"></head><body>
|
||||
<main id="feedback-embed"></main>
|
||||
<script type="application/json" id="feedback-embed-config">${JSON.stringify({ apiOrigin: origin, hostOrigin })}</script>
|
||||
<script defer src="/dist/feedback-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}`;
|
||||
hostOrigin = `http://notes.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 });
|
||||
});
|
||||
|
||||
/** A browser whose GUI origin is signed in with `token`, if any. */
|
||||
const signedIn = (token, options = {}) => browser.newContext({ ...options, storageState: {
|
||||
cookies: [],
|
||||
origins: token ? [{ origin, localStorage: [{ name: 'auth_token_v2', value: token }] }] : [],
|
||||
} });
|
||||
|
||||
const received = page => page.evaluate(() => window.received);
|
||||
|
||||
/** The [r, g, b] of one pixel of the page, from a screenshot. */
|
||||
const pixel = async (page, x, y) => {
|
||||
const png = await page.screenshot({ clip: { x, y, width: 1, height: 1 }, animations: 'disabled' });
|
||||
const data = [];
|
||||
for ( let at = 8; at < png.length; ) {
|
||||
const length = png.readUInt32BE(at);
|
||||
if ( png.toString('ascii', at + 4, at + 8) === 'IDAT' ) data.push(png.subarray(at + 8, at + 8 + length));
|
||||
at += 12 + length;
|
||||
}
|
||||
// One pixel: a filter byte, then its channels unchanged.
|
||||
return [...inflateSync(Buffer.concat(data)).subarray(1, 4)];
|
||||
};
|
||||
|
||||
describe('feedback embed', () => {
|
||||
it('sends feedback for the framing page as the user it names, then tells it', async () => {
|
||||
const context = await signedIn(sessionToken);
|
||||
try {
|
||||
const page = await context.newPage();
|
||||
page.setDefaultTimeout(6000);
|
||||
const errors = [];
|
||||
page.on('pageerror', error => errors.push(error.message));
|
||||
requests.length = 0;
|
||||
await page.goto(`${hostOrigin}/host?user=${sessionUser}`);
|
||||
const frame = page.frameLocator('iframe');
|
||||
await frame.locator('.app-feedback-form').waitFor();
|
||||
expect(await frame.locator('.app-feedback-target-title').textContent()).toBe('Notes');
|
||||
expect(await received(page)).toEqual([
|
||||
{ msg: 'feedbackEmbed', state: 'loading' },
|
||||
{ msg: 'feedbackEmbed', state: 'ready' },
|
||||
]);
|
||||
// The page behind shows through around the dialog, under its scrim.
|
||||
expect(await frame.locator('body').evaluate(el => getComputedStyle(el).backgroundColor)).toBe('rgba(0, 0, 0, 0)');
|
||||
await page.screenshot({ path: path.join(tmpdir(), 'puter-feedback-embed.png'), animations: 'disabled' });
|
||||
|
||||
await frame.locator('.app-feedback-message').fill('Love the new editor');
|
||||
await frame.locator('.app-feedback-send-btn').click();
|
||||
await frame.locator('.app-feedback-success').waitFor();
|
||||
await page.waitForFunction(() => window.received.some(m => m.msg === 'feedbackDialogClosed'));
|
||||
expect((await received(page)).at(-1)).toEqual({ msg: 'feedbackDialogClosed', sent: true });
|
||||
expect(await frame.locator('.app-feedback-overlay').count()).toBe(0);
|
||||
|
||||
const submit = requests.find(req => req.path === '/app-feedback');
|
||||
expect(submit.body).toEqual({ origin: hostOrigin, message: 'Love the new editor', context: 'web' });
|
||||
expect(submit.auth).toBe(`Bearer ${sessionToken}`);
|
||||
expect(errors).toEqual([]);
|
||||
} finally {
|
||||
await context.close();
|
||||
}
|
||||
}, 30000);
|
||||
|
||||
it('answers not sent when the user dismisses it', async () => {
|
||||
const context = await signedIn(sessionToken);
|
||||
try {
|
||||
const page = await context.newPage();
|
||||
page.setDefaultTimeout(6000);
|
||||
await page.goto(`${hostOrigin}/host?user=${sessionUser}`);
|
||||
const frame = page.frameLocator('iframe');
|
||||
await frame.locator('.app-feedback-form').waitFor();
|
||||
await frame.locator('.app-feedback-message').press('Escape');
|
||||
await page.waitForFunction(() => window.received.some(m => m.msg === 'feedbackDialogClosed'));
|
||||
expect((await received(page)).at(-1)).toEqual({ msg: 'feedbackDialogClosed', sent: false });
|
||||
} finally {
|
||||
await context.close();
|
||||
}
|
||||
}, 30000);
|
||||
|
||||
it.each(['light', 'dark'])('lets the page show through under the scrim in %s mode', async (colorScheme) => {
|
||||
const context = await signedIn(sessionToken, { colorScheme });
|
||||
try {
|
||||
const page = await context.newPage();
|
||||
page.setDefaultTimeout(6000);
|
||||
await page.goto(`${hostOrigin}/host?user=${sessionUser}`);
|
||||
const frame = page.frameLocator('iframe');
|
||||
await frame.locator('.app-feedback-form').waitFor();
|
||||
expect(await frame.locator('body').evaluate(() => matchMedia('(prefers-color-scheme: dark)').matches))
|
||||
.toBe(colorScheme === 'dark');
|
||||
await page.waitForTimeout(300);
|
||||
// The red page, dimmed by the scrim, rather than an opaque frame.
|
||||
const [red, green, blue] = await pixel(page, 10, 10);
|
||||
expect(red).toBeGreaterThan(green + 40);
|
||||
expect(green).toBe(blue);
|
||||
} finally {
|
||||
await context.close();
|
||||
}
|
||||
}, 30000);
|
||||
|
||||
it('keeps the page behind from scrolling under the scrim', async () => {
|
||||
const context = await signedIn(sessionToken);
|
||||
try {
|
||||
const page = await context.newPage();
|
||||
page.setDefaultTimeout(6000);
|
||||
await page.goto(`${hostOrigin}/host?user=${sessionUser}`);
|
||||
await page.frameLocator('iframe').locator('.app-feedback-form').waitFor();
|
||||
await page.mouse.move(10, 10);
|
||||
await page.mouse.wheel(0, 600);
|
||||
await page.waitForTimeout(300);
|
||||
expect(await page.evaluate(() => window.scrollY)).toBe(0);
|
||||
} finally {
|
||||
await context.close();
|
||||
}
|
||||
}, 30000);
|
||||
|
||||
it.each([
|
||||
['no Puter session', null, sessionUser],
|
||||
['a session for another user', sessionToken, otherUser],
|
||||
['a host that names no user', sessionToken, ''],
|
||||
['an email still to confirm', unconfirmedToken, unconfirmedUser],
|
||||
])('tells the host it is unavailable with %s', async (_label, token, user) => {
|
||||
const context = await signedIn(token);
|
||||
try {
|
||||
const page = await context.newPage();
|
||||
page.setDefaultTimeout(6000);
|
||||
requests.length = 0;
|
||||
await page.goto(`${hostOrigin}/host?user=${user}`);
|
||||
await page.waitForFunction(() => window.received.length >= 2);
|
||||
expect(await received(page)).toEqual([
|
||||
{ msg: 'feedbackEmbed', state: 'loading' },
|
||||
{ msg: 'feedbackEmbed', state: 'unavailable' },
|
||||
]);
|
||||
expect(await page.frameLocator('iframe').locator('.app-feedback-overlay').count()).toBe(0);
|
||||
expect(requests.some(req => req.path.startsWith('/app-feedback'))).toBe(false);
|
||||
} finally {
|
||||
await context.close();
|
||||
}
|
||||
}, 30000);
|
||||
|
||||
it('does nothing when opened on its own', async () => {
|
||||
const context = await signedIn(sessionToken);
|
||||
try {
|
||||
const page = await context.newPage();
|
||||
requests.length = 0;
|
||||
await page.goto(`${origin}/embed/feedback#user=${sessionUser}`);
|
||||
await page.waitForLoadState('networkidle');
|
||||
expect(await page.locator('.app-feedback-overlay').count()).toBe(0);
|
||||
expect(requests.some(req => req.path === '/whoami')).toBe(false);
|
||||
} finally {
|
||||
await context.close();
|
||||
}
|
||||
}, 30000);
|
||||
});
|
||||
@@ -1,86 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2024-present Puter Technologies Inc.
|
||||
*
|
||||
* This file is part of Puter.
|
||||
*
|
||||
* Puter is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import 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 UIWindowAppFeedback from './UI/UIWindowAppFeedback.js';
|
||||
import { embedSession } from './helpers/embedSession.js';
|
||||
|
||||
window.$ = window.jQuery = jQuery;
|
||||
window.html_encode = value => encode(String(value ?? ''), { mode: 'nonAsciiPrintable' });
|
||||
|
||||
const config = JSON.parse(document.querySelector('#feedback-embed-config').textContent);
|
||||
window.api_origin = config.apiOrigin;
|
||||
window.locale = 'en';
|
||||
try {
|
||||
window.locale = JSON.parse(localStorage.getItem('user_preferences') || '{}')?.language || 'en';
|
||||
} catch { /* Corrupt preferences must not prevent sending feedback. */ }
|
||||
const localeReady = loadLocale(window.locale);
|
||||
|
||||
/** Answers only the page frame-ancestors pinned, which the feedback is for. */
|
||||
const tellHost = (data) => window.parent.postMessage(data, config.hostOrigin);
|
||||
|
||||
/** Resolves once the dialog's overlay has left the page, after its success pane and exit. */
|
||||
const overlayGone = () => new Promise((resolve) => {
|
||||
const gone = () => !document.querySelector('.app-feedback-overlay');
|
||||
if ( gone() ) return resolve();
|
||||
const observer = new MutationObserver(() => {
|
||||
if ( ! gone() ) return;
|
||||
observer.disconnect();
|
||||
resolve();
|
||||
});
|
||||
observer.observe(document.body, { childList: true });
|
||||
});
|
||||
|
||||
/**
|
||||
* Shows the app feedback dialog for the framing page, as the signed-in user it
|
||||
* names. Without that user's session, or with an email still to confirm, the
|
||||
* host is told the embed is unavailable and uses the popup, which can sign in.
|
||||
*/
|
||||
async function run () {
|
||||
tellHost({ msg: 'feedbackEmbed', state: 'loading' });
|
||||
let session = null;
|
||||
try {
|
||||
session = await embedSession(config.apiOrigin, window.location.hash);
|
||||
} catch (e) {
|
||||
console.error('feedback embed: session check failed', e);
|
||||
}
|
||||
const user = session?.user;
|
||||
if ( !user || (user.requires_email_confirmation && !user.email_confirmed) ) {
|
||||
tellHost({ msg: 'feedbackEmbed', state: 'unavailable' });
|
||||
return;
|
||||
}
|
||||
window.auth_token = session.token;
|
||||
window.user = user;
|
||||
await localeReady;
|
||||
|
||||
tellHost({ msg: 'feedbackEmbed', state: 'ready' });
|
||||
let sent = false;
|
||||
try {
|
||||
sent = await UIWindowAppFeedback({ origin: config.hostOrigin, source: 'web' });
|
||||
await overlayGone();
|
||||
} catch (e) {
|
||||
console.error('feedback embed: dialog failed', e);
|
||||
}
|
||||
tellHost({ msg: 'feedbackDialogClosed', sent: sent === true });
|
||||
}
|
||||
|
||||
// Opened on its own, there is no page to send feedback for or answer.
|
||||
if ( config.hostOrigin && window.parent !== window ) run();
|
||||
@@ -1,83 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2024-present Puter Technologies Inc.
|
||||
*
|
||||
* This file is part of Puter.
|
||||
*
|
||||
* Puter is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import { embedHostUser } from './embedHostUser.js';
|
||||
|
||||
/**
|
||||
* @typedef {Object} EmbedSession
|
||||
* @property {string} token - the GUI session's token
|
||||
* @property {Record<string, unknown>} user - `/whoami` for that token
|
||||
*/
|
||||
|
||||
/**
|
||||
* The GUI session, from local storage or else the session cookie. Safari keys a
|
||||
* frame's local storage by the top-level origin, so under a sibling subdomain
|
||||
* the GUI's stored token is out of reach while its cookie is still sent.
|
||||
*
|
||||
* @returns {Promise<string | null>}
|
||||
*/
|
||||
async function readSession () {
|
||||
let stored = null;
|
||||
try {
|
||||
stored = localStorage.getItem('auth_token_v2');
|
||||
} catch { /* Storage the browser refuses holds no session; the cookie may. */ }
|
||||
if ( stored ) return stored;
|
||||
const response = await fetch('/get-gui-token', { credentials: 'include' });
|
||||
// Only a 401 means there is no session; a rate limit or server error
|
||||
// must not pass a signed-in user off as signed out.
|
||||
if ( response.status === 401 ) return null;
|
||||
if ( ! response.ok ) throw new Error(`Session lookup responded ${response.status}`);
|
||||
return (await response.json())?.token || null;
|
||||
}
|
||||
|
||||
/**
|
||||
* `/whoami` for a GUI token, or null when the API refuses it.
|
||||
*
|
||||
* @param {string} token
|
||||
* @param {string} apiOrigin
|
||||
* @returns {Promise<Record<string, unknown> | null>}
|
||||
*/
|
||||
async function whoami (token, apiOrigin) {
|
||||
const response = await fetch(new URL('/whoami', apiOrigin), {
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
});
|
||||
if ( response.status === 401 ) return null;
|
||||
if ( ! response.ok ) throw new Error(`Session check responded ${response.status}`);
|
||||
const user = await response.json();
|
||||
return user && typeof user === 'object' ? user : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The GUI session, only when it belongs to the user the embedding page names
|
||||
* in its `#user=<uuid>` fragment. A host signed out, or signed in as someone
|
||||
* else, gets null. Rejects when the session can't be checked.
|
||||
*
|
||||
* @param {string} apiOrigin
|
||||
* @param {string} hash - `location.hash`
|
||||
* @returns {Promise<EmbedSession | null>}
|
||||
*/
|
||||
export async function embedSession (apiOrigin, hash) {
|
||||
const hostUser = embedHostUser(hash);
|
||||
if ( ! hostUser ) return null;
|
||||
const token = await readSession();
|
||||
if ( ! token ) return null;
|
||||
const user = await whoami(token, apiOrigin);
|
||||
const uuid = typeof user?.uuid === 'string' ? user.uuid.toLowerCase() : null;
|
||||
return uuid === hostUser ? { token, user } : null;
|
||||
}
|
||||
@@ -1,158 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2024-present Puter Technologies Inc.
|
||||
*
|
||||
* This file is part of Puter.
|
||||
*
|
||||
* Puter is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* A GUI popup that a first-party page shows in a frame instead of a window,
|
||||
* served at `/embed/<action>`. It runs the popup boot unchanged: the framing
|
||||
* page stands in for `window.opener`, and closing asks it to remove the frame.
|
||||
*/
|
||||
|
||||
/** Popup actions a first-party page can frame. */
|
||||
export const FRAMED_POPUP_ACTIONS = new Set([
|
||||
'sign-in',
|
||||
'show-open-file-picker',
|
||||
'show-save-file-picker',
|
||||
'show-directory-picker',
|
||||
]);
|
||||
|
||||
/** Lifecycle messages to the framing page carry this as `msg`. */
|
||||
export const POPUP_FRAME_MSG = 'puterPopupFrame';
|
||||
|
||||
// The popup's query parameters a framing page may pass, in the fragment.
|
||||
const FRAGMENT_PARAMS = ['msg_id', 'options', 'request_auth'];
|
||||
|
||||
// The account checks a sign-in can show (email, phone, card), which are
|
||||
// windows rather than auth screens.
|
||||
const ACCOUNT_CHECK_WINDOWS = '.window-confirm-email-using-code, .window-confirm-phone-using-code, .window-card-verification';
|
||||
|
||||
// What a framed popup shows first; until one is on the page the framing page
|
||||
// keeps the frame hidden behind its loading scrim.
|
||||
const VISIBLE_UI = '.window, .dashboard-dialog-overlay, .captcha-modal';
|
||||
|
||||
/**
|
||||
* The action a `/embed/<action>` path frames, or null for any other path.
|
||||
*
|
||||
* @param {string} pathname
|
||||
* @returns {string | null}
|
||||
*/
|
||||
export const framedPopupAction = (pathname) => {
|
||||
const match = /^\/embed\/([a-z-]+)\/?$/.exec(String(pathname ?? ''));
|
||||
return match && FRAMED_POPUP_ACTIONS.has(match[1]) ? match[1] : null;
|
||||
};
|
||||
|
||||
/**
|
||||
* The popup parameters the framing page put in the fragment, and no others.
|
||||
* The route refuses a query string, so nothing else reaches the boot.
|
||||
*
|
||||
* @param {string} hash - `location.hash`
|
||||
* @returns {URLSearchParams}
|
||||
*/
|
||||
export const framedPopupParams = (hash) => {
|
||||
const given = new URLSearchParams(String(hash ?? '').replace(/^#/, ''));
|
||||
const params = new URLSearchParams();
|
||||
for ( const key of FRAGMENT_PARAMS ) {
|
||||
if ( given.has(key) ) params.set(key, given.get(key));
|
||||
}
|
||||
return params;
|
||||
};
|
||||
|
||||
/**
|
||||
* Starts a framed popup in `win`: the framing page, at the origin the server
|
||||
* pinned with frame-ancestors, becomes `opener`, `close()` asks it to remove
|
||||
* the frame, and it hears `ready` once the popup shows something. Returns a
|
||||
* function that sends it a lifecycle state, with any fields that go with it,
|
||||
* or null when there is no framing page to answer (opened on its own, or
|
||||
* framed by a page the server refused).
|
||||
*
|
||||
* @param {Window} win
|
||||
* @param {string | null | undefined} hostOrigin - `gui_params.embed_host_origin`
|
||||
* @returns {((state: 'loading' | 'ready' | 'unavailable' | 'closed' | 'provider', fields?: Record<string, string>) => void) | null}
|
||||
*/
|
||||
export const startPopupFrame = (win, hostOrigin) => {
|
||||
if ( ! hostOrigin || win.parent === win ) return null;
|
||||
const parent = win.parent;
|
||||
const tell = (state, fields = {}) =>
|
||||
parent.postMessage({ ...fields, msg: POPUP_FRAME_MSG, state }, hostOrigin);
|
||||
|
||||
win.opener = parent;
|
||||
win.close = () => tell('closed');
|
||||
// The popup closes with `window.open('', '_self').close()` as well, which
|
||||
// must reach the `close` above rather than navigate the frame.
|
||||
const open = win.open.bind(win);
|
||||
win.open = (url, target, ...rest) =>
|
||||
url === '' && target === '_self' ? win : open(url, target, ...rest);
|
||||
|
||||
const announceReady = () => {
|
||||
if ( ! win.document.querySelector(VISIBLE_UI) ) return false;
|
||||
tell('ready');
|
||||
return true;
|
||||
};
|
||||
const observer = new win.MutationObserver(() => {
|
||||
if ( announceReady() ) observer.disconnect();
|
||||
});
|
||||
observer.observe(win.document.body, { childList: true, subtree: true });
|
||||
|
||||
tell('loading');
|
||||
return tell;
|
||||
};
|
||||
|
||||
/**
|
||||
* Gives a framed popup's latest focus() back whenever the frame gains focus
|
||||
* with nothing focused, until the user presses a key or the pointer in it. A
|
||||
* cross-origin frame can't take focus until the framing page focuses it, which
|
||||
* also resets it, so a save picker's file name would start unfocused.
|
||||
*
|
||||
* @param {Window} win
|
||||
*/
|
||||
export const focusOnFrameFocus = (win) => {
|
||||
const focus = win.HTMLElement.prototype.focus;
|
||||
let latest = null;
|
||||
win.HTMLElement.prototype.focus = function (...args) {
|
||||
latest = { el: this, args };
|
||||
return focus.apply(this, args);
|
||||
};
|
||||
const forget = () => {
|
||||
latest = null;
|
||||
win.HTMLElement.prototype.focus = focus;
|
||||
};
|
||||
win.addEventListener('pointerdown', forget, { capture: true, once: true });
|
||||
win.addEventListener('keydown', forget, { capture: true, once: true });
|
||||
win.addEventListener('focus', () => {
|
||||
const active = win.document.activeElement;
|
||||
if ( ! latest?.el.isConnected || (active && active !== win.document.body) ) return;
|
||||
focus.apply(latest.el, latest.args);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Closes a framed sign-in on Escape in one of its account checks, as Escape
|
||||
* on its auth screens does. Those screens handle Escape themselves; the
|
||||
* account checks are windows, which don't.
|
||||
*
|
||||
* @param {Window} win
|
||||
*/
|
||||
export const closeAccountChecksOnEscape = (win) => {
|
||||
win.document.addEventListener('keydown', (e) => {
|
||||
// Safari sends the Escape that ends an IME conversion as keyCode 229
|
||||
// with isComposing false.
|
||||
if ( e.key !== 'Escape' || e.defaultPrevented || e.isComposing || e.keyCode === 229 ) return;
|
||||
const el = e.target === win.document.body ? win.document.querySelector('.window-active') : e.target;
|
||||
if ( el?.closest?.(ACCOUNT_CHECK_WINDOWS) ) win.close();
|
||||
});
|
||||
};
|
||||
@@ -1,235 +0,0 @@
|
||||
// @vitest-environment jsdom
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import {
|
||||
POPUP_FRAME_MSG,
|
||||
closeAccountChecksOnEscape,
|
||||
focusOnFrameFocus,
|
||||
framedPopupAction,
|
||||
framedPopupParams,
|
||||
startPopupFrame,
|
||||
} from './popupFrame.js';
|
||||
|
||||
const HOST = 'https://notes.puter.test';
|
||||
|
||||
/** A stand-in for the frame's window, with its own parent to post to. */
|
||||
const frameWindow = () => {
|
||||
const parent = { postMessage: vi.fn() };
|
||||
const open = vi.fn(() => 'opened');
|
||||
return {
|
||||
parent,
|
||||
open,
|
||||
opener: null,
|
||||
close: () => {},
|
||||
document,
|
||||
MutationObserver: window.MutationObserver,
|
||||
};
|
||||
};
|
||||
|
||||
const statesSent = (win) => win.parent.postMessage.mock.calls.map(([data, origin]) => {
|
||||
expect(origin).toBe(HOST);
|
||||
expect(data.msg).toBe(POPUP_FRAME_MSG);
|
||||
return data.state;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
document.body.innerHTML = '';
|
||||
});
|
||||
|
||||
describe('framedPopupAction', () => {
|
||||
it.each([
|
||||
['/embed/sign-in', 'sign-in'],
|
||||
['/embed/sign-in/', 'sign-in'],
|
||||
['/embed/show-open-file-picker', 'show-open-file-picker'],
|
||||
['/embed/show-save-file-picker', 'show-save-file-picker'],
|
||||
['/embed/show-directory-picker', 'show-directory-picker'],
|
||||
['/embed/apps', null],
|
||||
['/embed/feedback', null],
|
||||
['/embed/request-permission', null],
|
||||
['/action/sign-in', null],
|
||||
['/embed/sign-in/extra', null],
|
||||
['/', null],
|
||||
[undefined, null],
|
||||
])('%s frames %s', (pathname, expected) => {
|
||||
expect(framedPopupAction(pathname)).toBe(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe('framedPopupParams', () => {
|
||||
it('keeps only the popup parameters a framing page may pass', () => {
|
||||
const params = framedPopupParams('#msg_id=7&options=%7B%22multiple%22%3Atrue%7D&request_auth=true&user=abc&auth_token=x&attempt_temp_user_creation=true&api_origin=https%3A%2F%2Fevil.test');
|
||||
expect(Object.fromEntries(params)).toEqual({
|
||||
msg_id: '7',
|
||||
options: '{"multiple":true}',
|
||||
request_auth: 'true',
|
||||
});
|
||||
});
|
||||
|
||||
it('is empty without a fragment', () => {
|
||||
expect([...framedPopupParams('')]).toEqual([]);
|
||||
expect([...framedPopupParams(undefined)]).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('startPopupFrame', () => {
|
||||
it('does nothing without a framing page to answer', () => {
|
||||
const win = frameWindow();
|
||||
expect(startPopupFrame(win, null)).toBeNull();
|
||||
const top = frameWindow();
|
||||
top.parent = top;
|
||||
expect(startPopupFrame(top, HOST)).toBeNull();
|
||||
expect(win.opener).toBeNull();
|
||||
});
|
||||
|
||||
it('makes the framing page the opener and tells it the frame is loading', () => {
|
||||
const win = frameWindow();
|
||||
startPopupFrame(win, HOST);
|
||||
expect(win.opener).toBe(win.parent);
|
||||
expect(statesSent(win)).toEqual(['loading']);
|
||||
});
|
||||
|
||||
it('asks the framing page to remove the frame on close, either way the popup closes', () => {
|
||||
const win = frameWindow();
|
||||
startPopupFrame(win, HOST);
|
||||
win.close();
|
||||
win.open('', '_self').close();
|
||||
expect(statesSent(win)).toEqual(['loading', 'closed', 'closed']);
|
||||
});
|
||||
|
||||
it('sends the fields that go with a state, without letting them rename it', () => {
|
||||
const win = frameWindow();
|
||||
const tell = startPopupFrame(win, HOST);
|
||||
tell('provider', { url: 'https://puter.test/auth/oidc/google/start', msg: 'x', state: 'y' });
|
||||
expect(win.parent.postMessage).toHaveBeenLastCalledWith(
|
||||
{ url: 'https://puter.test/auth/oidc/google/start', msg: POPUP_FRAME_MSG, state: 'provider' },
|
||||
HOST,
|
||||
);
|
||||
});
|
||||
|
||||
it('leaves other window.open calls alone', () => {
|
||||
const win = frameWindow();
|
||||
const open = win.open;
|
||||
startPopupFrame(win, HOST);
|
||||
expect(win.open('https://puter.com/terms', '_blank')).toBe('opened');
|
||||
expect(open).toHaveBeenCalledWith('https://puter.com/terms', '_blank');
|
||||
});
|
||||
|
||||
it('tells the framing page it is ready once the popup shows something', async () => {
|
||||
const win = frameWindow();
|
||||
startPopupFrame(win, HOST);
|
||||
document.body.append(Object.assign(document.createElement('div'), { className: 'busy' }));
|
||||
await Promise.resolve();
|
||||
expect(statesSent(win)).toEqual(['loading']);
|
||||
|
||||
const dialog = document.createElement('div');
|
||||
dialog.className = 'dashboard-dialog-overlay auth-dialog-page';
|
||||
document.body.append(dialog);
|
||||
await Promise.resolve();
|
||||
document.body.append(Object.assign(document.createElement('div'), { className: 'window' }));
|
||||
await Promise.resolve();
|
||||
expect(statesSent(win)).toEqual(['loading', 'ready']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('focusOnFrameFocus', () => {
|
||||
/** A window of its own, so patching its focus() leaves the test's alone. */
|
||||
const ownWindow = () => {
|
||||
const iframe = document.createElement('iframe');
|
||||
document.body.append(iframe);
|
||||
const win = iframe.contentWindow;
|
||||
const input = win.document.createElement('input');
|
||||
win.document.body.append(input);
|
||||
return { win, input };
|
||||
};
|
||||
/** The frame losing its focus, then the framing page focusing it again. */
|
||||
const refocusFrame = (win) => {
|
||||
win.document.activeElement?.blur();
|
||||
win.dispatchEvent(new win.FocusEvent('focus'));
|
||||
};
|
||||
|
||||
it('gives the popup\'s latest focus back each time the frame gains focus with nothing focused', () => {
|
||||
const { win, input } = ownWindow();
|
||||
focusOnFrameFocus(win);
|
||||
input.focus();
|
||||
refocusFrame(win);
|
||||
expect(win.document.activeElement).toBe(input);
|
||||
refocusFrame(win);
|
||||
expect(win.document.activeElement).toBe(input);
|
||||
});
|
||||
|
||||
it('leaves focus to the user once they press a key or the pointer', () => {
|
||||
for ( const type of ['keydown', 'pointerdown'] ) {
|
||||
const { win, input } = ownWindow();
|
||||
const focus = win.HTMLElement.prototype.focus;
|
||||
focusOnFrameFocus(win);
|
||||
input.focus();
|
||||
win.document.body.dispatchEvent(new win.Event(type, { bubbles: true }));
|
||||
refocusFrame(win);
|
||||
expect(win.document.activeElement).toBe(win.document.body);
|
||||
expect(win.HTMLElement.prototype.focus).toBe(focus);
|
||||
}
|
||||
});
|
||||
|
||||
it('leaves an element that has focus, and one that has left the page', () => {
|
||||
const { win, input } = ownWindow();
|
||||
const nativeFocus = win.HTMLElement.prototype.focus;
|
||||
const other = win.document.createElement('button');
|
||||
win.document.body.append(other);
|
||||
focusOnFrameFocus(win);
|
||||
input.focus();
|
||||
nativeFocus.call(other);
|
||||
win.dispatchEvent(new win.FocusEvent('focus'));
|
||||
expect(win.document.activeElement).toBe(other);
|
||||
|
||||
input.remove();
|
||||
refocusFrame(win);
|
||||
expect(win.document.activeElement).toBe(win.document.body);
|
||||
});
|
||||
});
|
||||
|
||||
describe('closeAccountChecksOnEscape', () => {
|
||||
/** A sign-in window with its own document, whose close() is spied on. */
|
||||
const signInWindow = () => {
|
||||
const win = { document: document.implementation.createHTMLDocument(), close: vi.fn() };
|
||||
closeAccountChecksOnEscape(win);
|
||||
return win;
|
||||
};
|
||||
const escapeOn = (el, init = {}) => {
|
||||
el.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', bubbles: true, cancelable: true, ...init }));
|
||||
};
|
||||
const showWindow = (win, className = 'window window-active window-confirm-email-using-code') => {
|
||||
const el = Object.assign(win.document.createElement('div'), { className });
|
||||
el.innerHTML = '<input class="digit-input">';
|
||||
win.document.body.append(el);
|
||||
return el;
|
||||
};
|
||||
|
||||
it.each([
|
||||
'window-confirm-email-using-code',
|
||||
'window-confirm-phone-using-code',
|
||||
'window-card-verification',
|
||||
])('closes on Escape in the %s window', (cls) => {
|
||||
const win = signInWindow();
|
||||
escapeOn(showWindow(win, `window window-active ${cls}`).querySelector('input'));
|
||||
expect(win.close).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('closes on Escape with focus on the page behind an account check', () => {
|
||||
const win = signInWindow();
|
||||
showWindow(win);
|
||||
escapeOn(win.document.body);
|
||||
expect(win.close).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('leaves Escape that something or an IME handled, other keys, and other windows alone', () => {
|
||||
const win = signInWindow();
|
||||
const input = showWindow(win).querySelector('input');
|
||||
input.addEventListener('keydown', (e) => e.preventDefault(), { once: true });
|
||||
escapeOn(input);
|
||||
escapeOn(input, { key: 'Enter' });
|
||||
escapeOn(input, { isComposing: true });
|
||||
// Safari ends a conversion with keyCode 229 and isComposing false.
|
||||
escapeOn(input, { keyCode: 229 });
|
||||
escapeOn(showWindow(win, 'window window-alert').querySelector('input'));
|
||||
expect(win.close).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -60,10 +60,6 @@ window.gui = async (options) => {
|
||||
window.co_isolation_enabled = options.co_isolation_enabled;
|
||||
window.teams_ui = options.teams_ui ?? false;
|
||||
|
||||
// A popup a first-party page frames answers it before the SDK loads, so
|
||||
// the page knows the frame wasn't refused.
|
||||
window.start_popup_frame?.(options.embed_host_origin);
|
||||
|
||||
// DEV: Load the initgui.js file if we are in development mode
|
||||
if ( !window.gui_env || window.gui_env === 'dev' ) {
|
||||
await window.loadScript('/sdk/puter.dev.js');
|
||||
|
||||
+11
-74
@@ -46,8 +46,6 @@ import UIAuthSignup from './UI/Auth/UIAuthSignup.js';
|
||||
import UIAuthRecoverPassword from './UI/Auth/UIAuthRecoverPassword.js';
|
||||
import { PROCESS_RUNNING } from './definitions.js';
|
||||
import { isAttestedOrigin } from './helpers/attestedOrigin.js';
|
||||
import { embedHostUser } from './helpers/embedHostUser.js';
|
||||
import { closeAccountChecksOnEscape, focusOnFrameFocus, framedPopupAction, framedPopupParams, startPopupFrame } from './helpers/popupFrame.js';
|
||||
import confirm_before_unload from './helpers/confirmBeforeUnload.js';
|
||||
import create_access_token from './helpers/createAccessToken.js';
|
||||
import create_gui_token from './helpers/createGuiToken.js';
|
||||
@@ -95,7 +93,6 @@ import { ThemeService } from './services/ThemeService.js';
|
||||
import { privacy_aware_path as privacy_aware_path_factory } from './util/desktop.js';
|
||||
import { resolveAPIOrigin } from './util/apiOrigin.js';
|
||||
import {
|
||||
defersTokenToOpener,
|
||||
deliversTokenAtBoot,
|
||||
relationshipIsConsent,
|
||||
relaysTokenAtBoot,
|
||||
@@ -569,9 +566,6 @@ const postAuthActions = async (action) => {
|
||||
msg,
|
||||
original_msg_id: msg_id,
|
||||
}, '*');
|
||||
// Escape closes only the picker window, which would leave the
|
||||
// page's frame open over an empty desktop.
|
||||
if ( window.embedded_in_frame ) window.close();
|
||||
};
|
||||
|
||||
if ( action === 'show-open-file-picker' ) {
|
||||
@@ -638,15 +632,12 @@ const postAuthActions = async (action) => {
|
||||
msg: 'sendMeFileData',
|
||||
}, '*');
|
||||
|
||||
// listen for 'showSaveFilePickerPopup' event from parent. Only the
|
||||
// page that asked supplies the file, once; any other window that
|
||||
// can reach this one (a sibling frame of the framing page) can't.
|
||||
window.addEventListener('message', async function onFileData (event) {
|
||||
if ( event.data?.msg !== 'showSaveFilePickerPopup' || event.source !== window.opener )
|
||||
// listen for 'showSaveFilePickerPopup' event from parent
|
||||
window.addEventListener('message', async (event) => {
|
||||
if ( event.data.msg !== 'showSaveFilePickerPopup' )
|
||||
{
|
||||
return;
|
||||
}
|
||||
window.removeEventListener('message', onFileData);
|
||||
|
||||
// Open dialog
|
||||
UIWindow({
|
||||
@@ -659,7 +650,6 @@ const postAuthActions = async (action) => {
|
||||
title: 'Save',
|
||||
is_dir: true,
|
||||
is_saveFileDialog: true,
|
||||
saveFileDialog_default_filename: event.data.suggestedName ?? '',
|
||||
is_resizable: false,
|
||||
has_head: false,
|
||||
cover_page: true,
|
||||
@@ -719,14 +709,7 @@ const postAuthActions = async (action) => {
|
||||
catch ( err ) {
|
||||
// item with same name exists
|
||||
if ( err.code === 'item_with_same_name_exists' ) {
|
||||
// Escape closes the alert without an answer,
|
||||
// which counts as Cancel, so the picker isn't
|
||||
// left behind its busy mask.
|
||||
let alert_closed;
|
||||
const alert_dismissed = new Promise((resolve) => {
|
||||
alert_closed = () => resolve('cancel');
|
||||
});
|
||||
const alert_resp = await Promise.race([UIAlert({
|
||||
const alert_resp = await UIAlert({
|
||||
message: `<strong>${html_encode(err.entry_name)}</strong> already exists.`,
|
||||
buttons: [
|
||||
{
|
||||
@@ -740,11 +723,10 @@ const postAuthActions = async (action) => {
|
||||
},
|
||||
],
|
||||
parent_uuid: $(el_filedialog_window).attr('data-element_uuid'),
|
||||
window_options: { on_close: alert_closed },
|
||||
}), alert_dismissed]);
|
||||
});
|
||||
if ( alert_resp === 'replace' ) {
|
||||
overwrite = true;
|
||||
} else {
|
||||
} else if ( alert_resp === 'cancel' ) {
|
||||
// enable parent window
|
||||
$(el_filedialog_window).find('.window-disable-mask, .busy-indicator').hide();
|
||||
return;
|
||||
@@ -1172,17 +1154,9 @@ if (jQuery) {
|
||||
{
|
||||
const pathname = window.location.pathname;
|
||||
const search_params = new URLSearchParams(window.location.search);
|
||||
// A popup a first-party page frames at `/embed/<action>` boots as one.
|
||||
if (['true', '1'].includes(search_params.get('embedded_in_popup')) || framedPopupAction(pathname)) {
|
||||
if (['true', '1'].includes(search_params.get('embedded_in_popup'))) {
|
||||
window.embedded_in_popup = true;
|
||||
}
|
||||
// Called by the bootstrap in index.js, which can't import.
|
||||
window.start_popup_frame = (hostOrigin) => {
|
||||
if (!framedPopupAction(pathname)) return;
|
||||
window.tell_popup_frame = startPopupFrame(window, hostOrigin);
|
||||
window.embedded_in_frame = !!window.tell_popup_frame;
|
||||
if (window.embedded_in_frame) focusOnFrameFocus(window);
|
||||
};
|
||||
// note: iframe detection is inlined because globals.js (window.is_embedded) loads after this module
|
||||
const in_iframe = window.location !== window.parent.location;
|
||||
const needs_desktop_at_root =
|
||||
@@ -1437,16 +1411,8 @@ window.initgui = async function (options) {
|
||||
const url_paths = parse_url_paths(window.location.pathname);
|
||||
window.url_paths = url_paths;
|
||||
|
||||
// A framed popup without a framing page to answer (opened on its own, or
|
||||
// framed by a page the server refused) has nothing to do.
|
||||
const framed_action = framedPopupAction(window.location.pathname);
|
||||
if (framed_action && !window.embedded_in_frame) return;
|
||||
|
||||
// GET query params provided. A framed popup takes the popup's parameters
|
||||
// from its fragment; its route refuses a query string.
|
||||
window.url_query_params = framed_action
|
||||
? framedPopupParams(window.location.hash)
|
||||
: new URLSearchParams(window.location.search);
|
||||
// GET query params provided
|
||||
window.url_query_params = new URLSearchParams(window.location.search);
|
||||
window.signup_bonus_code = readSignupBonusCode(window.url_query_params);
|
||||
|
||||
// Install device signal helpers; collection is lazy. The fingerprint is
|
||||
@@ -1540,9 +1506,7 @@ window.initgui = async function (options) {
|
||||
// Extract 'action' from URL
|
||||
//--------------------------------------------------------------------------------------
|
||||
let action;
|
||||
if (framed_action) {
|
||||
action = framed_action;
|
||||
} else if (
|
||||
if (
|
||||
window.url_paths[0]?.toLocaleLowerCase() === 'action' &&
|
||||
window.url_paths[1]
|
||||
) {
|
||||
@@ -1556,8 +1520,6 @@ window.initgui = async function (options) {
|
||||
// action. See util/popupAuth.js.
|
||||
window.gui_action = action;
|
||||
|
||||
if (window.embedded_in_frame && action === 'sign-in') closeAccountChecksOnEscape(window);
|
||||
|
||||
if (action === 'oidc-2fa') {
|
||||
const result = oidcChallenge?.token && await UIAuth2FA(oidcChallenge.token);
|
||||
if (result?.redirectUrl) {
|
||||
@@ -1681,9 +1643,7 @@ window.initgui = async function (options) {
|
||||
action,
|
||||
);
|
||||
try {
|
||||
// A framed popup's opener is the page the server read from the
|
||||
// referrer and pinned with frame-ancestors.
|
||||
window.openerOrigin = window.embedded_in_frame ? window.gui_params.embed_host_origin : await resolveOpenerOrigin({
|
||||
window.openerOrigin = await resolveOpenerOrigin({
|
||||
relaysToken: relaysTokenAtBoot(
|
||||
action,
|
||||
window.url_query_params.get('cross_origin_isolated') ===
|
||||
@@ -1740,26 +1700,6 @@ window.initgui = async function (options) {
|
||||
// this is the referrer in terms of user acquisition
|
||||
window.referrerStr = window.openerOrigin;
|
||||
|
||||
// A framed picker shows only the Puter session of the user the
|
||||
// framing page is signed in as; for anyone else the page uses the
|
||||
// popup, which can sign in and switch accounts.
|
||||
if (window.embedded_in_frame && defersTokenToOpener(action)) {
|
||||
const host_user = embedHostUser(window.location.hash);
|
||||
let session_user = null;
|
||||
if (host_user && window.is_auth()) {
|
||||
try {
|
||||
const session = await puter.os.user({ query: 'icon_size=64' });
|
||||
session_user = String(session?.uuid ?? '').toLowerCase();
|
||||
} catch (e) {
|
||||
session_user = null;
|
||||
}
|
||||
}
|
||||
if (!host_user || session_user !== host_user) {
|
||||
window.tell_popup_frame('unavailable');
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Tell a request-permission opener that this popup can reach it. Sent
|
||||
// here, before any sign-in gate, because the SDK reads its absence as
|
||||
// "the opener link was severed by COOP" — where a close means the
|
||||
@@ -2011,11 +1951,8 @@ window.initgui = async function (options) {
|
||||
);
|
||||
window.userAppToken = response?.token;
|
||||
|
||||
// A framed picker already holds the framing page's user, so it
|
||||
// doesn't offer to switch.
|
||||
if (
|
||||
!picked_a_user_for_sdk_login &&
|
||||
!(window.embedded_in_frame && defersTokenToOpener(action)) &&
|
||||
window.logged_in_users.length > 1 &&
|
||||
(!window.userAppToken ||
|
||||
window.url_query_params.get('request_auth'))
|
||||
|
||||
@@ -1,65 +0,0 @@
|
||||
// @vitest-environment jsdom
|
||||
/*
|
||||
* 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 { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import jQuery from './lib/jquery-3.6.1/jquery-3.6.1.min.js';
|
||||
|
||||
vi.mock('./UI/UIAlert.js', () => ({ default: vi.fn() }));
|
||||
vi.mock('./UI/UIWindowSearch.js', () => ({ default: vi.fn() }));
|
||||
vi.mock('./helpers/launchApp.js', () => ({ default: vi.fn() }));
|
||||
vi.mock('./helpers/openItem.js', () => ({ default: vi.fn() }));
|
||||
|
||||
globalThis.$ = jQuery;
|
||||
globalThis.jQuery = jQuery;
|
||||
globalThis.i18n = key => key;
|
||||
globalThis.def = globalThis.def ?? (value => value);
|
||||
globalThis.use = globalThis.use ?? (() => class {});
|
||||
|
||||
await import('./keyboard.js');
|
||||
|
||||
const escape = () => document.body.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', keyCode: 27, which: 27, bubbles: true }));
|
||||
|
||||
describe('Escape on a file dialog', () => {
|
||||
const closed = [];
|
||||
|
||||
beforeEach(() => {
|
||||
closed.length = 0;
|
||||
jQuery.fn.close = function () {
|
||||
closed.push(...this.toArray());
|
||||
return this;
|
||||
};
|
||||
document.body.innerHTML = '<div class="window window-active window-filedialog"></div>';
|
||||
});
|
||||
|
||||
it('closes the file dialog', () => {
|
||||
escape();
|
||||
expect(closed).toEqual([document.querySelector('.window-filedialog')]);
|
||||
});
|
||||
|
||||
it('closes only a context menu open over it', () => {
|
||||
document.body.insertAdjacentHTML('beforeend', '<div class="context-menu"></div>');
|
||||
escape();
|
||||
expect(document.querySelector('.context-menu')).toBeNull();
|
||||
expect(closed).toEqual([]);
|
||||
|
||||
escape();
|
||||
expect(closed).toEqual([document.querySelector('.window-filedialog')]);
|
||||
});
|
||||
});
|
||||
@@ -448,8 +448,6 @@ $(document).bind('keydown', async function (e) {
|
||||
else if (
|
||||
// escape key code
|
||||
e.which === 27 &&
|
||||
// an open context menu takes the Escape (below) instead
|
||||
$('.context-menu').length === 0 &&
|
||||
// active window must be a FileDialog or Alert
|
||||
($('.window-active').hasClass('window-filedialog') || $('.window-active').hasClass('window-alert')) &&
|
||||
// either don't close if an input is focused or if the input is the filename input
|
||||
|
||||
@@ -93,7 +93,6 @@ module.exports = async (options = {}) => {
|
||||
...entries,
|
||||
],
|
||||
'apps-embed': './src/appsEmbed.js',
|
||||
'feedback-embed': './src/feedbackEmbed.js',
|
||||
};
|
||||
config.output = {
|
||||
path: path.resolve(__dirname, '../dist'),
|
||||
|
||||
Vendored
+13
-13
@@ -44,17 +44,17 @@ export type {
|
||||
ListTTSEnginesOptions,
|
||||
ListTTSVoicesOptions,
|
||||
OcrAnnotationFormat,
|
||||
RealtimeOptions,
|
||||
RealtimeTextEvent,
|
||||
RealtimeAudioEvent,
|
||||
RealtimeTranscriptEvent,
|
||||
RealtimeInterruptedEvent,
|
||||
RealtimeTurnDoneEvent,
|
||||
RealtimeErrorEvent,
|
||||
RealtimeEventMap,
|
||||
RealtimeSessionOptions,
|
||||
RealtimePlaybackPosition,
|
||||
RealtimeSpeakerOptions,
|
||||
ConvoOptions,
|
||||
ConvoTextEvent,
|
||||
ConvoAudioEvent,
|
||||
ConvoTranscriptEvent,
|
||||
ConvoInterruptedEvent,
|
||||
ConvoTurnDoneEvent,
|
||||
ConvoErrorEvent,
|
||||
ConvoEventMap,
|
||||
ConvoSessionOptions,
|
||||
ConvoPlaybackPosition,
|
||||
ConvoSpeakerOptions,
|
||||
RealtimeServerEvent,
|
||||
RealtimeClientEvent,
|
||||
Speech2SpeechOptions,
|
||||
@@ -391,5 +391,5 @@ export type Teams = InstanceType<import('./types/modules/teams/index.js').TeamsC
|
||||
export type UI = InstanceType<import('./types/modules/UI.js').UIConstructor>;
|
||||
export type WorkersHandler = InstanceType<import('./types/modules/Workers.js').WorkersConstructor>;
|
||||
|
||||
export type { Conversation, RealtimeError } from './types/modules/ai/realtime.js';
|
||||
export type { RealtimeMicrophone, RealtimeSpeaker } from './types/modules/ai/realtimeMedia.js';
|
||||
export type { Conversation, ConvoError } from './types/modules/ai/convo.js';
|
||||
export type { ConvoMicrophone, ConvoSpeaker } from './types/modules/ai/convoMedia.js';
|
||||
@@ -1,412 +0,0 @@
|
||||
/**
|
||||
* Puter GUI pages a first-party site can frame, under `/embed/*` on the GUI
|
||||
* origin. The server lets only a page one label under that origin frame them.
|
||||
*/
|
||||
|
||||
// After the frame loads, how long to wait for the embed's first message
|
||||
// before treating it as refused (a CSP that blocks it still fires `load`).
|
||||
const LOAD_GRACE_MS = 1000;
|
||||
// How long the embed may take to check the session before the popup is used.
|
||||
const READY_TIMEOUT_MS = 15_000;
|
||||
// A session check slower than this shows a loading scrim over the page.
|
||||
const LOADING_DELAY_MS = 200;
|
||||
|
||||
// The loading scrim matches the feedback embed's own, which fades in over it
|
||||
// once ready. A popup frame keeps it as the scrim behind its box. Scrolling
|
||||
// over the dialog stays there rather than scrolling the page behind it.
|
||||
const embedStyle = (cls) => `
|
||||
dialog.${cls}{overscroll-behavior:contain}
|
||||
dialog.${cls}::backdrop{background:transparent}
|
||||
dialog.${cls}:focus{outline:none}
|
||||
.${cls}-loading{position:absolute;inset:0;display:flex;align-items:center;justify-content:center;background:rgba(0,0,0,.5);opacity:0;pointer-events:none;transition:opacity .16s ease-in-out}
|
||||
.${cls}.is-loading .${cls}-loading{opacity:1;pointer-events:auto}
|
||||
.${cls}.is-loading iframe{visibility:hidden}
|
||||
.${cls}-loading span{width:28px;height:28px;box-sizing:border-box;border:3px solid rgba(255,255,255,.3);border-top-color:#fff;border-radius:50%;animation:${cls}-spin .7s linear infinite}
|
||||
@keyframes ${cls}-spin{to{transform:rotate(360deg)}}
|
||||
@media (prefers-color-scheme:dark){.${cls}-loading{background:rgba(0,0,0,.66)}}
|
||||
@media (prefers-reduced-motion:reduce){.${cls}-loading{transition:none}.${cls}-loading span{animation-duration:1.6s}}
|
||||
`;
|
||||
|
||||
// A popup frame sits in a box the size of the popup window it replaces, over
|
||||
// the scrim, and fills the screen on a phone as the popup's tab would. It's
|
||||
// centered by margins, which snap to device pixels where a translate can
|
||||
// leave its edge on a fraction of one. Its white backing stops 1px inside the
|
||||
// rounded edge, so in dark mode it doesn't show as a light fringe around the
|
||||
// frame. The close button starts from `all:unset` so the page's own button
|
||||
// styles don't reach it.
|
||||
const BOX_STYLE = `
|
||||
.puter-popup-frame .puter-popup-frame-loading{opacity:1;pointer-events:auto}
|
||||
.puter-popup-frame:not(.is-loading) .puter-popup-frame-loading span{display:none}
|
||||
.puter-popup-frame.is-loading .puter-popup-frame-box{visibility:hidden}
|
||||
.puter-popup-frame-box{position:absolute;inset:0;margin:auto;isolation:isolate;box-sizing:border-box;width:min(var(--puter-popup-frame-width),calc(100% - 32px));height:min(var(--puter-popup-frame-height),calc(100% - 32px));border-radius:12px;overflow:hidden;box-shadow:0 24px 64px rgba(0,0,0,.35)}
|
||||
.puter-popup-frame-box::before{content:"";position:absolute;inset:1px;z-index:-1;border-radius:11px;background:#fff}
|
||||
.puter-popup-frame-close{all:unset;box-sizing:border-box;position:absolute;top:10px;right:10px;width:32px;height:32px;display:flex;align-items:center;justify-content:center;padding:0;border:0;border-radius:50%;background:transparent;color:#6b7280;cursor:pointer}
|
||||
.puter-popup-frame-close:hover{background:rgba(127,127,127,.15)}
|
||||
.puter-popup-frame-close:focus-visible{outline:2px solid #3b82f6;outline-offset:1px}
|
||||
.puter-popup-frame-close svg{width:16px;height:16px}
|
||||
@media (prefers-color-scheme:dark){.puter-popup-frame-close{color:#9ca3af}}
|
||||
@media (max-width:600px){.puter-popup-frame-box{width:100%;height:100%;border-radius:0}.puter-popup-frame-box::before{inset:0;border-radius:0}}
|
||||
.puter-popup-frame[open] .puter-popup-frame-loading,.puter-popup-frame:not(.is-loading) .puter-popup-frame-box{animation:puter-popup-frame-in .16s ease-out}
|
||||
@keyframes puter-popup-frame-in{from{opacity:0}}
|
||||
@media (prefers-reduced-motion:reduce){.puter-popup-frame[open] .puter-popup-frame-loading,.puter-popup-frame:not(.is-loading) .puter-popup-frame-box{animation:none}}
|
||||
`;
|
||||
|
||||
const CLOSE_ICON = '<svg viewBox="0 0 16 16" aria-hidden="true"><path d="M3.5 3.5l9 9m0-9l-9 9" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" fill="none"/></svg>';
|
||||
|
||||
// Subdomains the server answers or reserves itself, which it never lets frame
|
||||
// an embed (the backend's `isReservedSubdomain`). Framing from one would wait
|
||||
// on a refused frame before falling back.
|
||||
const RESERVED_LABELS = new Set(['www', 'api', 'js', 'dav', 'about', 'developer', 'docs', 'apps', 'puter-app-icons']);
|
||||
|
||||
// Dismisses the single-instance embed showing at each path.
|
||||
const showingEmbeds = new Map();
|
||||
|
||||
/**
|
||||
* Whether a page at `pageUrl` may frame an embed served by `guiOrigin`:
|
||||
* exactly one label under its host, on the same scheme and port, and not one
|
||||
* of the GUI's reserved subdomains.
|
||||
*
|
||||
* @param {string | undefined} pageUrl
|
||||
* @param {string} guiOrigin
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export const canFrameGuiEmbed = (pageUrl, guiOrigin) => {
|
||||
let page;
|
||||
let gui;
|
||||
try {
|
||||
page = new URL(pageUrl);
|
||||
gui = new URL(guiOrigin);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
const host = page.hostname.replace(/\.$/, '').toLowerCase();
|
||||
const suffix = `.${gui.hostname.toLowerCase()}`;
|
||||
if ( page.protocol !== gui.protocol || page.port !== gui.port || !host.endsWith(suffix) ) return false;
|
||||
const label = host.slice(0, -suffix.length);
|
||||
return label.length > 0 && !label.includes('.') && !RESERVED_LABELS.has(label);
|
||||
};
|
||||
|
||||
/**
|
||||
* Whether every frame above `win` may frame an embed too. The server's
|
||||
* frame-ancestors allows only the GUI and the page, so in a frame of any other
|
||||
* site the embed is refused, and opening the popup straight away is faster.
|
||||
*
|
||||
* @param {Window} win - the page
|
||||
* @param {string} guiOrigin
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export const ancestorsAllowGuiEmbed = (win, guiOrigin) => {
|
||||
let gui;
|
||||
try {
|
||||
gui = new URL(guiOrigin).origin;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
const page = win.location.origin;
|
||||
const ancestors = win.location.ancestorOrigins;
|
||||
if ( ancestors ) return Array.from(ancestors).every((origin) => origin === page || origin === gui);
|
||||
// Without ancestorOrigins (Firefox), only a same-origin ancestor can be named.
|
||||
try {
|
||||
for ( let w = win; w.parent !== w; w = w.parent ) {
|
||||
if ( w.parent.location.origin !== page ) return false;
|
||||
}
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Mounts `/embed/<path>` in a modal frame, hidden until the embed reports
|
||||
* `ready` and behind a loading scrim once its start takes longer than
|
||||
* LOADING_DELAY_MS. Resolves what `onMessage` finishes with, what `dismissed`
|
||||
* returns when the user cancels, or null when the frame is refused, never
|
||||
* answers, or reports itself unavailable. Never rejects.
|
||||
*
|
||||
* @template T
|
||||
* @param {Object} opts
|
||||
* @param {string} opts.guiOrigin
|
||||
* @param {string} opts.path - under the GUI origin, such as `/embed/feedback`
|
||||
* @param {Record<string, string>} opts.fragment - what the embed reads from its fragment
|
||||
* @param {string} opts.className - the dialog's class, prefixing its parts'
|
||||
* @param {string} opts.title - the frame's accessible name
|
||||
* @param {string} opts.stateMsg - `msg` of the embed's loading, ready and unavailable messages
|
||||
* @param {{ width: number, height: number }} [opts.box] - shows the frame in a box this size over a scrim; without it the frame covers the page and the embed draws its own scrim
|
||||
* @param {string} [opts.closeLabel] - puts a close button on the box, which dismisses it
|
||||
* @param {string} [opts.allow] - the frame's permissions policy, for features the embed uses
|
||||
* @param {boolean} [opts.escapeDismisses] - Escape on the page dismisses a ready embed too, instead of handing it the keys
|
||||
* @param {boolean} [opts.single] - a newer embed at this path dismisses this one, as a named popup window is reused
|
||||
* @param {() => T} opts.dismissed - the result of a dismissal: Escape or a click on the loading scrim, or the close button
|
||||
* @param {(data: Record<string, unknown>, finish: (result: T | null) => void) => void} opts.onMessage - every other message from the embed
|
||||
* @param {(frame: Window) => void} [opts.onFrame] - the frame's window, once it exists
|
||||
* @returns {Promise<T | null>}
|
||||
*/
|
||||
const mountGuiEmbed = ({ guiOrigin, path, fragment, className, title, stateMsg, box, closeLabel, allow, escapeDismisses = false, single = false, dismissed, onMessage, onFrame }) => new Promise((resolve) => {
|
||||
let origin;
|
||||
try {
|
||||
origin = new URL(guiOrigin).origin;
|
||||
} catch {
|
||||
resolve(null);
|
||||
return;
|
||||
}
|
||||
const url = new URL(path, origin);
|
||||
url.hash = new URLSearchParams(fragment).toString();
|
||||
// Before reading the focus this one restores, which the older one hands back.
|
||||
if ( single ) showingEmbeds.get(path)?.();
|
||||
|
||||
const dialog = document.createElement('dialog');
|
||||
dialog.className = className;
|
||||
dialog.setAttribute('aria-label', title);
|
||||
// Inline, so the page's own `dialog` and `iframe` styles can't move,
|
||||
// round or shrink them.
|
||||
dialog.style.cssText = 'position:fixed;inset:0;width:100%;height:100%;min-width:0;min-height:0;max-width:none;max-height:none;margin:0;padding:0;border:0;border-radius:0;box-shadow:none;transform:none;background:transparent;overflow:hidden;';
|
||||
const style = document.createElement('style');
|
||||
style.textContent = embedStyle(className) + (box ? BOX_STYLE : '');
|
||||
const frame = document.createElement('iframe');
|
||||
frame.title = title;
|
||||
// The embed allows a framing page it can name from the referrer.
|
||||
frame.setAttribute('referrerpolicy', 'origin');
|
||||
if ( allow ) frame.setAttribute('allow', allow);
|
||||
const frameBase = 'display:block;width:100%;height:100%;min-width:0;min-height:0;max-width:none;max-height:none;margin:0;padding:0;border:0;';
|
||||
// A frame whose color-scheme differs from its document's is painted
|
||||
// opaque, so this matches the embed's. WebKit also gives the frame this
|
||||
// scheme as its preferred one, so it has to follow the OS.
|
||||
frame.style.cssText = box
|
||||
? frameBase
|
||||
: `${frameBase}background:transparent;color-scheme:light dark;`;
|
||||
const loading = document.createElement('div');
|
||||
loading.className = `${className}-loading`;
|
||||
loading.setAttribute('role', 'status');
|
||||
loading.setAttribute('aria-label', 'Loading');
|
||||
loading.setAttribute('aria-hidden', 'true');
|
||||
loading.append(document.createElement('span'));
|
||||
dialog.append(style, loading);
|
||||
if ( box ) {
|
||||
const container = document.createElement('div');
|
||||
container.className = `${className}-box`;
|
||||
container.style.setProperty('--puter-popup-frame-width', `${box.width}px`);
|
||||
container.style.setProperty('--puter-popup-frame-height', `${box.height}px`);
|
||||
container.append(frame);
|
||||
if ( closeLabel ) {
|
||||
const close = document.createElement('button');
|
||||
close.type = 'button';
|
||||
close.className = `${className}-close`;
|
||||
close.setAttribute('aria-label', closeLabel);
|
||||
close.title = closeLabel;
|
||||
close.innerHTML = CLOSE_ICON;
|
||||
close.addEventListener('click', () => dismiss());
|
||||
container.append(close);
|
||||
}
|
||||
dialog.append(container);
|
||||
} else {
|
||||
dialog.insertBefore(frame, loading);
|
||||
}
|
||||
|
||||
let settled = false;
|
||||
let heard = false;
|
||||
let shown = false;
|
||||
let ready = false;
|
||||
let timer = null;
|
||||
let loadingTimer = null;
|
||||
// Each embed document posts `loading` once; these tell a document that
|
||||
// never did (one the server refused) from one that did.
|
||||
let loads = 0;
|
||||
let boots = 0;
|
||||
let wasReady = false;
|
||||
let restarted = false;
|
||||
let restartTimer = null;
|
||||
const restoreFocus = document.activeElement;
|
||||
const wait = (ms) => {
|
||||
clearTimeout(timer);
|
||||
timer = setTimeout(() => finish(null), ms);
|
||||
};
|
||||
const finish = (result) => {
|
||||
if ( settled ) return;
|
||||
settled = true;
|
||||
clearTimeout(timer);
|
||||
clearTimeout(loadingTimer);
|
||||
clearTimeout(restartTimer);
|
||||
window.removeEventListener('message', onWindowMessage);
|
||||
if ( showingEmbeds.get(path) === dismiss ) showingEmbeds.delete(path);
|
||||
dialog.remove();
|
||||
if ( shown ) restoreFocus?.focus?.({ preventScroll: true });
|
||||
resolve(result);
|
||||
};
|
||||
const dismiss = () => finish(dismissed());
|
||||
if ( single ) showingEmbeds.set(path, dismiss);
|
||||
// The spinner is only faded out, so assistive tech hears it only while it shows.
|
||||
const setLoading = (on) => {
|
||||
dialog.classList.toggle('is-loading', on);
|
||||
if ( on ) loading.removeAttribute('aria-hidden');
|
||||
else loading.setAttribute('aria-hidden', 'true');
|
||||
};
|
||||
const show = () => {
|
||||
if ( dialog.open ) return;
|
||||
shown = true;
|
||||
dialog.showModal();
|
||||
};
|
||||
const onWindowMessage = (event) => {
|
||||
if ( event.origin !== origin || event.source !== frame.contentWindow ) return;
|
||||
const data = event.data;
|
||||
if ( !data || typeof data !== 'object' ) return;
|
||||
if ( data.msg !== stateMsg || !['loading', 'ready', 'unavailable'].includes(data.state) ) {
|
||||
onMessage(data, finish);
|
||||
} else if ( data.state === 'loading' ) {
|
||||
heard = true;
|
||||
boots++;
|
||||
wait(READY_TIMEOUT_MS);
|
||||
} else if ( data.state === 'ready' ) {
|
||||
heard = true;
|
||||
ready = true;
|
||||
wasReady = true;
|
||||
restarted = false;
|
||||
clearTimeout(timer);
|
||||
clearTimeout(loadingTimer);
|
||||
setLoading(false);
|
||||
show();
|
||||
frame.focus();
|
||||
} else {
|
||||
finish(null);
|
||||
}
|
||||
};
|
||||
// A reload or navigation the embed starts itself names the embed, not
|
||||
// this page, as its referrer, so the server refuses to let it frame. This
|
||||
// page opens the embed again, once, which it allows.
|
||||
const restart = () => {
|
||||
if ( restarted ) {
|
||||
finish(null);
|
||||
return;
|
||||
}
|
||||
restarted = true;
|
||||
ready = false;
|
||||
loads = boots;
|
||||
setLoading(true);
|
||||
// As long as its first start may take, then the popup.
|
||||
wait(READY_TIMEOUT_MS);
|
||||
// WebKit takes the refused URL itself as a fragment navigation of its
|
||||
// error page, which loads nothing. The server redirects a query away,
|
||||
// and the fragment carries over the redirect.
|
||||
const reopen = new URL(url);
|
||||
reopen.search = 'reopen';
|
||||
frame.src = reopen.href;
|
||||
};
|
||||
window.addEventListener('message', onWindowMessage);
|
||||
frame.addEventListener('load', () => {
|
||||
loads++;
|
||||
if ( ! heard ) wait(LOAD_GRACE_MS);
|
||||
if ( ! wasReady || boots >= loads ) return;
|
||||
// Most likely refused: the spinner covers the error page meanwhile.
|
||||
ready = false;
|
||||
setLoading(true);
|
||||
const expected = loads;
|
||||
clearTimeout(restartTimer);
|
||||
restartTimer = setTimeout(() => {
|
||||
if ( boots < expected ) restart();
|
||||
}, LOAD_GRACE_MS);
|
||||
});
|
||||
// Once ready, Escape outside the frame would close the dialog with the
|
||||
// embed's own dialog still open inside it, so the embed owns dismissal
|
||||
// unless it dismisses on Escape too.
|
||||
dialog.addEventListener('cancel', (event) => {
|
||||
event.preventDefault();
|
||||
if ( ready && ! escapeDismisses ) frame.focus();
|
||||
else dismiss();
|
||||
});
|
||||
loading.addEventListener('click', () => {
|
||||
if ( ! ready ) dismiss();
|
||||
});
|
||||
|
||||
try {
|
||||
frame.src = url.href;
|
||||
// Hidden at first, so a quickly refused session shows nothing.
|
||||
document.body.append(dialog);
|
||||
onFrame?.(frame.contentWindow);
|
||||
wait(READY_TIMEOUT_MS);
|
||||
loadingTimer = setTimeout(() => {
|
||||
setLoading(true);
|
||||
show();
|
||||
}, LOADING_DELAY_MS);
|
||||
} catch {
|
||||
finish(null);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Shows the app feedback dialog from `/embed/feedback` in a modal frame over
|
||||
* the page, as the user named, behind a loading scrim while a slow session
|
||||
* check runs. Resolves `{ sent }` once the dialog closes or the user cancels
|
||||
* the loading scrim, or null when the embed can't serve this user (no
|
||||
* matching Puter session, an email to confirm, or a refused frame), so the
|
||||
* caller can use the popup instead. Never rejects.
|
||||
*
|
||||
* @param {Object} opts
|
||||
* @param {string} opts.guiOrigin
|
||||
* @param {string} opts.userUuid - the page's signed-in user
|
||||
* @returns {Promise<{ sent: boolean } | null>}
|
||||
*/
|
||||
export const showFeedbackEmbed = ({ guiOrigin, userUuid }) => mountGuiEmbed({
|
||||
guiOrigin,
|
||||
path: '/embed/feedback',
|
||||
fragment: { user: userUuid },
|
||||
className: 'puter-feedback-embed',
|
||||
title: 'Send feedback',
|
||||
stateMsg: 'feedbackEmbed',
|
||||
dismissed: () => ({ sent: false }),
|
||||
onMessage: ({ msg, sent }, finish) => {
|
||||
if ( msg === 'feedbackDialogClosed' ) finish({ sent: sent === true });
|
||||
},
|
||||
});
|
||||
|
||||
/** Popup actions a first-party page shows framed, each in a box its popup window's size. */
|
||||
const POPUP_FRAME_BOXES = {
|
||||
'sign-in': { width: 600, height: 700 },
|
||||
'show-open-file-picker': { width: 700, height: 400 },
|
||||
'show-save-file-picker': { width: 700, height: 400 },
|
||||
'show-directory-picker': { width: 700, height: 400 },
|
||||
};
|
||||
|
||||
/**
|
||||
* Shows the GUI popup for `action` in a frame over the page instead of a
|
||||
* window, from `/embed/<action>`. The GUI runs the popup unchanged and posts
|
||||
* its answers to this page as it would to an opener; `onMessage` sees each of
|
||||
* them and finishes with the result. The frame closing on its own, as the
|
||||
* popup would, finishes with `closed()`, and so does the user dismissing it.
|
||||
* Resolves null when the frame can't serve the request (refused, never
|
||||
* answering, or reporting itself unavailable), so the caller can open the
|
||||
* popup instead. Never rejects.
|
||||
*
|
||||
* @template T
|
||||
* @param {Object} opts
|
||||
* @param {string} opts.guiOrigin
|
||||
* @param {'sign-in' | 'show-open-file-picker' | 'show-save-file-picker' | 'show-directory-picker'} opts.action
|
||||
* @param {Record<string, string>} opts.fragment - the popup's parameters, and `user` for a picker
|
||||
* @param {string} opts.title - the frame's accessible name
|
||||
* @param {string} [opts.closeLabel] - puts a close button on the frame
|
||||
* @param {() => T} opts.closed
|
||||
* @param {(data: Record<string, unknown>, finish: (result: T) => void) => void} opts.onMessage
|
||||
* @param {(frame: Window) => void} [opts.onFrame]
|
||||
* @returns {Promise<T | null>}
|
||||
*/
|
||||
export const showPopupFrame = ({ guiOrigin, action, fragment, title, closeLabel, closed, onMessage, onFrame }) => mountGuiEmbed({
|
||||
guiOrigin,
|
||||
path: `/embed/${action}`,
|
||||
fragment,
|
||||
className: 'puter-popup-frame',
|
||||
title,
|
||||
stateMsg: 'puterPopupFrame',
|
||||
box: POPUP_FRAME_BOXES[action],
|
||||
closeLabel,
|
||||
// Signup and the account checks fill in a texted code, as in the popup.
|
||||
allow: 'otp-credentials',
|
||||
// Escape inside the frame closes it as well.
|
||||
escapeDismisses: true,
|
||||
// A second request for this popup, such as from a double click, replaces the
|
||||
// first. Each popup has its own window name, so other popups stay open.
|
||||
single: true,
|
||||
dismissed: closed,
|
||||
onFrame,
|
||||
onMessage: (data, finish) => {
|
||||
if ( data.msg === 'puterPopupFrame' && data.state === 'closed' ) finish(closed());
|
||||
else onMessage(data, finish);
|
||||
},
|
||||
});
|
||||
@@ -1,477 +0,0 @@
|
||||
// @vitest-environment jsdom
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { ancestorsAllowGuiEmbed, canFrameGuiEmbed, showFeedbackEmbed, showPopupFrame } from './guiEmbed.js';
|
||||
|
||||
const GUI_ORIGIN = 'https://puter.test';
|
||||
const USER = '0b1c9a52-3f0e-4c7a-9a65-2f6d1e8b7c40';
|
||||
|
||||
// jsdom has no top-layer dialogs.
|
||||
HTMLDialogElement.prototype.showModal = function () {
|
||||
this.open = true;
|
||||
};
|
||||
|
||||
const frame = () => document.querySelector('dialog.puter-feedback-embed iframe');
|
||||
|
||||
/** A message from the embed, by default from its frame on the GUI origin. */
|
||||
const fromEmbed = (data, { origin = GUI_ORIGIN, source = frame().contentWindow } = {}) =>
|
||||
window.dispatchEvent(new MessageEvent('message', { data, origin, source }));
|
||||
|
||||
beforeEach(() => {
|
||||
document.body.innerHTML = '';
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
describe('canFrameGuiEmbed', () => {
|
||||
it.each([
|
||||
['https://notes.puter.com/a', 'https://puter.com', true],
|
||||
['https://NOTES.puter.com./a', 'https://puter.com', true],
|
||||
['http://notes.puter.localhost:4100/', 'http://puter.localhost:4100', true],
|
||||
['https://puter.com/', 'https://puter.com', false],
|
||||
['https://a.b.puter.com/', 'https://puter.com', false],
|
||||
['https://notputer.com/', 'https://puter.com', false],
|
||||
['https://notes.puter.com.evil.com/', 'https://puter.com', false],
|
||||
['http://notes.puter.com/', 'https://puter.com', false],
|
||||
['https://notes.puter.com:8443/', 'https://puter.com', false],
|
||||
['https://notes.puter.site/', 'https://puter.com', false],
|
||||
['https://docs.puter.com/playground/', 'https://puter.com', false],
|
||||
['https://api.puter.com/', 'https://puter.com', false],
|
||||
['https://www.puter.com/', 'https://puter.com', false],
|
||||
['http://apps.puter.localhost:4100/', 'http://puter.localhost:4100', false],
|
||||
[undefined, 'https://puter.com', false],
|
||||
])('%s under %s is %s', (page, gui, expected) => {
|
||||
expect(canFrameGuiEmbed(page, gui)).toBe(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe('ancestorsAllowGuiEmbed', () => {
|
||||
const PAGE = 'https://notes.puter.test';
|
||||
/** A window at `origin` under `parent`; a cross-origin parent throws on `location`, as in a browser. */
|
||||
const fakeWindow = ({ origin = PAGE, ancestorOrigins, parent } = {}) => {
|
||||
const win = { location: { origin, ancestorOrigins } };
|
||||
win.parent = parent ?? win;
|
||||
return win;
|
||||
};
|
||||
const crossOrigin = () => {
|
||||
const win = {};
|
||||
Object.defineProperty(win, 'location', { get: () => { throw new DOMException('Blocked', 'SecurityError'); } });
|
||||
win.parent = win;
|
||||
return win;
|
||||
};
|
||||
|
||||
it.each([
|
||||
['a top-level page', [], true],
|
||||
['a frame of the same page', [PAGE], true],
|
||||
['a frame in the GUI', [PAGE, GUI_ORIGIN], true],
|
||||
['a frame of another site', ['https://other.test'], false],
|
||||
['a frame of the page inside another site', [PAGE, 'https://other.test'], false],
|
||||
])('reads ancestorOrigins for %s', (_, ancestorOrigins, expected) => {
|
||||
expect(ancestorsAllowGuiEmbed(fakeWindow({ ancestorOrigins }), GUI_ORIGIN)).toBe(expected);
|
||||
});
|
||||
|
||||
it('walks same-origin parents without ancestorOrigins', () => {
|
||||
expect(ancestorsAllowGuiEmbed(fakeWindow(), GUI_ORIGIN)).toBe(true);
|
||||
expect(ancestorsAllowGuiEmbed(fakeWindow({ parent: fakeWindow() }), GUI_ORIGIN)).toBe(true);
|
||||
expect(ancestorsAllowGuiEmbed(fakeWindow({ parent: crossOrigin() }), GUI_ORIGIN)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('showFeedbackEmbed', () => {
|
||||
it('loads the embed for the named user, hidden until it is ready', () => {
|
||||
showFeedbackEmbed({ guiOrigin: `${GUI_ORIGIN}/`, userUuid: USER });
|
||||
const url = new URL(frame().src);
|
||||
expect(url.origin + url.pathname).toBe(`${GUI_ORIGIN}/embed/feedback`);
|
||||
expect(url.search).toBe('');
|
||||
expect(new URLSearchParams(url.hash.slice(1)).get('user')).toBe(USER);
|
||||
expect(frame().getAttribute('referrerpolicy')).toBe('origin');
|
||||
// WebKit hands the frame this as its preferred scheme; it must follow the OS.
|
||||
expect(frame().getAttribute('style')).toContain('color-scheme: light dark');
|
||||
expect(frame().closest('dialog').open).toBe(false);
|
||||
});
|
||||
|
||||
it('shows the dialog once ready and resolves what it reports, restoring focus', async () => {
|
||||
const trigger = document.createElement('button');
|
||||
document.body.append(trigger);
|
||||
trigger.focus();
|
||||
const result = showFeedbackEmbed({ guiOrigin: GUI_ORIGIN, userUuid: USER });
|
||||
const dialog = frame().closest('dialog');
|
||||
|
||||
fromEmbed({ msg: 'feedbackEmbed', state: 'loading' });
|
||||
expect(dialog.open).toBe(false);
|
||||
fromEmbed({ msg: 'feedbackEmbed', state: 'ready' });
|
||||
expect(dialog.open).toBe(true);
|
||||
expect(document.activeElement).toBe(frame());
|
||||
|
||||
fromEmbed({ msg: 'feedbackDialogClosed', sent: true });
|
||||
await expect(result).resolves.toEqual({ sent: true });
|
||||
expect(dialog.isConnected).toBe(false);
|
||||
expect(document.activeElement).toBe(trigger);
|
||||
});
|
||||
|
||||
it('shows a loading scrim over the page while a slow session check runs, then the dialog', async () => {
|
||||
vi.useFakeTimers();
|
||||
const trigger = document.createElement('button');
|
||||
document.body.append(trigger);
|
||||
trigger.focus();
|
||||
const result = showFeedbackEmbed({ guiOrigin: GUI_ORIGIN, userUuid: USER });
|
||||
const dialog = frame().closest('dialog');
|
||||
fromEmbed({ msg: 'feedbackEmbed', state: 'loading' });
|
||||
await vi.advanceTimersByTimeAsync(199);
|
||||
expect(dialog.open).toBe(false);
|
||||
await vi.advanceTimersByTimeAsync(1);
|
||||
expect(dialog.open).toBe(true);
|
||||
expect(dialog.classList.contains('is-loading')).toBe(true);
|
||||
expect(dialog.querySelector('[role="status"]').hasAttribute('aria-hidden')).toBe(false);
|
||||
|
||||
fromEmbed({ msg: 'feedbackEmbed', state: 'ready' });
|
||||
expect(dialog.classList.contains('is-loading')).toBe(false);
|
||||
expect(dialog.querySelector('[role="status"]').getAttribute('aria-hidden')).toBe('true');
|
||||
expect(document.activeElement).toBe(frame());
|
||||
fromEmbed({ msg: 'feedbackDialogClosed', sent: true });
|
||||
await expect(result).resolves.toEqual({ sent: true });
|
||||
expect(document.activeElement).toBe(trigger);
|
||||
});
|
||||
|
||||
it('never shows the loading scrim when the embed is ready quickly', async () => {
|
||||
vi.useFakeTimers();
|
||||
showFeedbackEmbed({ guiOrigin: GUI_ORIGIN, userUuid: USER });
|
||||
const dialog = frame().closest('dialog');
|
||||
fromEmbed({ msg: 'feedbackEmbed', state: 'ready' });
|
||||
await vi.advanceTimersByTimeAsync(1000);
|
||||
expect(dialog.open).toBe(true);
|
||||
expect(dialog.classList.contains('is-loading')).toBe(false);
|
||||
});
|
||||
|
||||
it('takes the loading scrim down for the popup when the embed turns out unavailable', async () => {
|
||||
vi.useFakeTimers();
|
||||
const trigger = document.createElement('button');
|
||||
document.body.append(trigger);
|
||||
trigger.focus();
|
||||
const result = showFeedbackEmbed({ guiOrigin: GUI_ORIGIN, userUuid: USER });
|
||||
await vi.advanceTimersByTimeAsync(200);
|
||||
fromEmbed({ msg: 'feedbackEmbed', state: 'unavailable' });
|
||||
await expect(result).resolves.toBeNull();
|
||||
expect(document.querySelector('dialog')).toBeNull();
|
||||
expect(document.activeElement).toBe(trigger);
|
||||
});
|
||||
|
||||
it.each([
|
||||
['Escape', (dialog) => dialog.dispatchEvent(new Event('cancel', { cancelable: true }))],
|
||||
['a click on the scrim', (dialog) => dialog.querySelector('[role="status"]').click()],
|
||||
])('lets %s cancel while loading, without falling back to the popup', async (_label, cancel) => {
|
||||
vi.useFakeTimers();
|
||||
const result = showFeedbackEmbed({ guiOrigin: GUI_ORIGIN, userUuid: USER });
|
||||
await vi.advanceTimersByTimeAsync(200);
|
||||
cancel(frame().closest('dialog'));
|
||||
await expect(result).resolves.toEqual({ sent: false });
|
||||
expect(document.querySelector('dialog')).toBeNull();
|
||||
});
|
||||
|
||||
it('resolves null without showing anything when the embed is unavailable', async () => {
|
||||
const result = showFeedbackEmbed({ guiOrigin: GUI_ORIGIN, userUuid: USER });
|
||||
const dialog = frame().closest('dialog');
|
||||
fromEmbed({ msg: 'feedbackEmbed', state: 'loading' });
|
||||
fromEmbed({ msg: 'feedbackEmbed', state: 'unavailable' });
|
||||
await expect(result).resolves.toBeNull();
|
||||
expect(dialog.open).toBe(false);
|
||||
expect(dialog.isConnected).toBe(false);
|
||||
});
|
||||
|
||||
it('ignores messages from another origin or another window', async () => {
|
||||
const result = showFeedbackEmbed({ guiOrigin: GUI_ORIGIN, userUuid: USER });
|
||||
fromEmbed({ msg: 'feedbackDialogClosed', sent: true }, { origin: 'https://other.test' });
|
||||
fromEmbed({ msg: 'feedbackDialogClosed', sent: true }, { source: window });
|
||||
fromEmbed({ msg: 'feedbackEmbed', state: 'ready' }, { origin: 'https://notes.puter.test' });
|
||||
expect(frame().closest('dialog').open).toBe(false);
|
||||
|
||||
fromEmbed({ msg: 'feedbackDialogClosed', sent: false });
|
||||
await expect(result).resolves.toEqual({ sent: false });
|
||||
});
|
||||
|
||||
it('resolves null when the frame loads without the embed answering', async () => {
|
||||
vi.useFakeTimers();
|
||||
const result = showFeedbackEmbed({ guiOrigin: GUI_ORIGIN, userUuid: USER });
|
||||
frame().dispatchEvent(new Event('load'));
|
||||
await vi.advanceTimersByTimeAsync(1000);
|
||||
await expect(result).resolves.toBeNull();
|
||||
expect(document.querySelector('dialog')).toBeNull();
|
||||
});
|
||||
|
||||
it('gives a loading embed time to check the session, then gives up', async () => {
|
||||
vi.useFakeTimers();
|
||||
const result = showFeedbackEmbed({ guiOrigin: GUI_ORIGIN, userUuid: USER });
|
||||
fromEmbed({ msg: 'feedbackEmbed', state: 'loading' });
|
||||
frame().dispatchEvent(new Event('load'));
|
||||
let settled = false;
|
||||
result.then(() => {
|
||||
settled = true;
|
||||
});
|
||||
await vi.advanceTimersByTimeAsync(5000);
|
||||
expect(settled).toBe(false);
|
||||
await vi.advanceTimersByTimeAsync(10_000);
|
||||
await expect(result).resolves.toBeNull();
|
||||
});
|
||||
|
||||
it('keeps the dialog open on Escape outside the frame, since the embed owns dismissal', () => {
|
||||
showFeedbackEmbed({ guiOrigin: GUI_ORIGIN, userUuid: USER });
|
||||
fromEmbed({ msg: 'feedbackEmbed', state: 'ready' });
|
||||
const dialog = frame().closest('dialog');
|
||||
const cancel = new Event('cancel', { cancelable: true });
|
||||
dialog.dispatchEvent(cancel);
|
||||
expect(cancel.defaultPrevented).toBe(true);
|
||||
expect(dialog.isConnected).toBe(true);
|
||||
});
|
||||
|
||||
it('resolves null for an unusable GUI origin', async () => {
|
||||
await expect(showFeedbackEmbed({ guiOrigin: 'not a url', userUuid: USER })).resolves.toBeNull();
|
||||
expect(document.querySelector('dialog')).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('showPopupFrame', () => {
|
||||
const popupFrame = () => document.querySelector('dialog.puter-popup-frame iframe');
|
||||
const fromPopup = (data, { origin = GUI_ORIGIN, source = popupFrame().contentWindow } = {}) =>
|
||||
window.dispatchEvent(new MessageEvent('message', { data, origin, source }));
|
||||
const show = (overrides = {}) => showPopupFrame({
|
||||
guiOrigin: GUI_ORIGIN,
|
||||
action: 'show-open-file-picker',
|
||||
fragment: { msg_id: '3', user: USER },
|
||||
title: 'Open file',
|
||||
closed: () => 'closed',
|
||||
onMessage: () => {},
|
||||
...overrides,
|
||||
});
|
||||
|
||||
it('loads the popup action with its parameters in the fragment, hidden until ready', () => {
|
||||
let framed;
|
||||
show({ onFrame: (win) => {
|
||||
framed = win;
|
||||
} });
|
||||
const url = new URL(popupFrame().src);
|
||||
expect(url.origin + url.pathname).toBe(`${GUI_ORIGIN}/embed/show-open-file-picker`);
|
||||
expect(url.search).toBe('');
|
||||
expect(Object.fromEntries(new URLSearchParams(url.hash.slice(1)))).toEqual({ msg_id: '3', user: USER });
|
||||
expect(popupFrame().getAttribute('referrerpolicy')).toBe('origin');
|
||||
expect(popupFrame().closest('dialog').getAttribute('aria-label')).toBe('Open file');
|
||||
expect(popupFrame().getAttribute('allow')).toBe('otp-credentials');
|
||||
expect(framed).toBe(popupFrame().contentWindow);
|
||||
expect(popupFrame().closest('dialog').open).toBe(false);
|
||||
});
|
||||
|
||||
it('sizes the box like the popup window it replaces', () => {
|
||||
show({ action: 'sign-in', fragment: { msg_id: 'a' } });
|
||||
const box = document.querySelector('.puter-popup-frame-box');
|
||||
expect(box.style.getPropertyValue('--puter-popup-frame-width')).toBe('600px');
|
||||
expect(box.style.getPropertyValue('--puter-popup-frame-height')).toBe('700px');
|
||||
expect(box.contains(popupFrame())).toBe(true);
|
||||
});
|
||||
|
||||
it('shows once ready and hands every other message to the caller to finish with', async () => {
|
||||
const seen = [];
|
||||
const result = show({ onMessage: (data, finish) => {
|
||||
seen.push(data.msg);
|
||||
if ( data.msg === 'fileOpenPicked' ) finish('picked');
|
||||
} });
|
||||
const dialog = popupFrame().closest('dialog');
|
||||
const status = dialog.querySelector('[role="status"]');
|
||||
expect(status.getAttribute('aria-hidden')).toBe('true');
|
||||
fromPopup({ msg: 'puterPopupFrame', state: 'loading' });
|
||||
fromPopup({ msg: 'puterPopupFrame', state: 'ready' });
|
||||
expect(dialog.open).toBe(true);
|
||||
expect(status.getAttribute('aria-hidden')).toBe('true');
|
||||
expect(document.activeElement).toBe(popupFrame());
|
||||
fromPopup({ msg: 'puter.token', success: true });
|
||||
fromPopup({ msg: 'fileOpenPicked' });
|
||||
await expect(result).resolves.toBe('picked');
|
||||
expect(seen).toEqual(['puter.token', 'fileOpenPicked']);
|
||||
expect(dialog.isConnected).toBe(false);
|
||||
});
|
||||
|
||||
it('opens itself again when the embed reloads into a refused frame, once per ready', async () => {
|
||||
vi.useFakeTimers();
|
||||
const result = show();
|
||||
const iframe = popupFrame();
|
||||
const dialog = iframe.closest('dialog');
|
||||
const srcSet = vi.spyOn(iframe, 'src', 'set');
|
||||
const load = () => iframe.dispatchEvent(new Event('load'));
|
||||
fromPopup({ msg: 'puterPopupFrame', state: 'loading' });
|
||||
load();
|
||||
fromPopup({ msg: 'puterPopupFrame', state: 'ready' });
|
||||
|
||||
// A reload the embed starts itself loads a document that never posts.
|
||||
load();
|
||||
expect(dialog.classList.contains('is-loading')).toBe(true);
|
||||
vi.advanceTimersByTime(1000);
|
||||
expect(srcSet).toHaveBeenCalledTimes(1);
|
||||
// Not just the refused URL again, which WebKit takes as a fragment
|
||||
// navigation of the error page: the server redirects the query away.
|
||||
const opened = new URL(iframe.src);
|
||||
const reopened = new URL(srcSet.mock.calls[0][0]);
|
||||
expect(reopened.search).not.toBe('');
|
||||
expect(reopened.origin + reopened.pathname + reopened.hash).toBe(opened.origin + opened.pathname + opened.hash);
|
||||
expect(dialog.classList.contains('is-loading')).toBe(true);
|
||||
|
||||
// The page's own reload boots, before or after its load event.
|
||||
fromPopup({ msg: 'puterPopupFrame', state: 'loading' });
|
||||
load();
|
||||
vi.advanceTimersByTime(1000);
|
||||
expect(srcSet).toHaveBeenCalledTimes(1);
|
||||
fromPopup({ msg: 'puterPopupFrame', state: 'ready' });
|
||||
expect(dialog.classList.contains('is-loading')).toBe(false);
|
||||
|
||||
// Ready again, so a later refusal is retried too, but not twice running.
|
||||
load();
|
||||
vi.advanceTimersByTime(1000);
|
||||
expect(srcSet).toHaveBeenCalledTimes(2);
|
||||
load();
|
||||
vi.advanceTimersByTime(1000);
|
||||
await expect(result).resolves.toBeNull();
|
||||
expect(srcSet).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('falls back to the popup when the embed it opens again never loads', async () => {
|
||||
vi.useFakeTimers();
|
||||
const result = show();
|
||||
const iframe = popupFrame();
|
||||
const dialog = iframe.closest('dialog');
|
||||
const load = () => iframe.dispatchEvent(new Event('load'));
|
||||
fromPopup({ msg: 'puterPopupFrame', state: 'loading' });
|
||||
load();
|
||||
fromPopup({ msg: 'puterPopupFrame', state: 'ready' });
|
||||
|
||||
// Refused after a reload of its own, then reopened on a stalled connection.
|
||||
load();
|
||||
vi.advanceTimersByTime(1000);
|
||||
vi.advanceTimersByTime(14_999);
|
||||
expect(dialog.isConnected).toBe(true);
|
||||
vi.advanceTimersByTime(1);
|
||||
await expect(result).resolves.toBeNull();
|
||||
expect(dialog.isConnected).toBe(false);
|
||||
});
|
||||
|
||||
it('keeps showing an embed that was ready before its load event fired', () => {
|
||||
vi.useFakeTimers();
|
||||
show();
|
||||
const iframe = popupFrame();
|
||||
const dialog = iframe.closest('dialog');
|
||||
const srcSet = vi.spyOn(iframe, 'src', 'set');
|
||||
const load = () => iframe.dispatchEvent(new Event('load'));
|
||||
fromPopup({ msg: 'puterPopupFrame', state: 'loading' });
|
||||
load();
|
||||
fromPopup({ msg: 'puterPopupFrame', state: 'ready' });
|
||||
load();
|
||||
vi.advanceTimersByTime(1000);
|
||||
expect(srcSet).toHaveBeenCalledTimes(1);
|
||||
|
||||
// The page's reload shows its UI while a slow image still loads.
|
||||
fromPopup({ msg: 'puterPopupFrame', state: 'loading' });
|
||||
fromPopup({ msg: 'puterPopupFrame', state: 'ready' });
|
||||
load();
|
||||
vi.advanceTimersByTime(5000);
|
||||
expect(dialog.classList.contains('is-loading')).toBe(false);
|
||||
expect(srcSet).toHaveBeenCalledTimes(1);
|
||||
expect(dialog.isConnected).toBe(true);
|
||||
});
|
||||
|
||||
it('closes on Escape outside the frame once ready, as Escape inside it does', async () => {
|
||||
const result = show();
|
||||
const dialog = popupFrame().closest('dialog');
|
||||
fromPopup({ msg: 'puterPopupFrame', state: 'ready' });
|
||||
const cancel = new Event('cancel', { cancelable: true });
|
||||
dialog.dispatchEvent(cancel);
|
||||
expect(cancel.defaultPrevented).toBe(true);
|
||||
await expect(result).resolves.toBe('closed');
|
||||
expect(dialog.isConnected).toBe(false);
|
||||
});
|
||||
|
||||
it('finishes as closed when the popup closes itself', async () => {
|
||||
const result = show();
|
||||
fromPopup({ msg: 'puterPopupFrame', state: 'ready' });
|
||||
fromPopup({ msg: 'puterPopupFrame', state: 'closed' });
|
||||
await expect(result).resolves.toBe('closed');
|
||||
expect(document.querySelector('dialog')).toBeNull();
|
||||
});
|
||||
|
||||
it('offers a close button only when asked, which finishes as closed', async () => {
|
||||
show();
|
||||
expect(document.querySelector('.puter-popup-frame-close')).toBeNull();
|
||||
document.body.innerHTML = '';
|
||||
|
||||
const result = show({ action: 'sign-in', closeLabel: 'Close' });
|
||||
fromPopup({ msg: 'puterPopupFrame', state: 'ready' });
|
||||
const close = document.querySelector('.puter-popup-frame-close');
|
||||
expect(close.getAttribute('aria-label')).toBe('Close');
|
||||
close.click();
|
||||
await expect(result).resolves.toBe('closed');
|
||||
});
|
||||
|
||||
it('resolves null for the popup when the frame is unavailable or refused', async () => {
|
||||
const unavailable = show();
|
||||
fromPopup({ msg: 'puterPopupFrame', state: 'loading' });
|
||||
fromPopup({ msg: 'puterPopupFrame', state: 'unavailable' });
|
||||
await expect(unavailable).resolves.toBeNull();
|
||||
|
||||
vi.useFakeTimers();
|
||||
const refused = show();
|
||||
popupFrame().dispatchEvent(new Event('load'));
|
||||
await vi.advanceTimersByTimeAsync(1000);
|
||||
await expect(refused).resolves.toBeNull();
|
||||
});
|
||||
|
||||
it('lets the user dismiss the loading scrim, finishing as closed', async () => {
|
||||
vi.useFakeTimers();
|
||||
const result = show();
|
||||
await vi.advanceTimersByTimeAsync(200);
|
||||
document.querySelector('.puter-popup-frame-loading').click();
|
||||
await expect(result).resolves.toBe('closed');
|
||||
});
|
||||
|
||||
it('replaces a popup frame that is showing, finishing it as closed', async () => {
|
||||
const button = document.createElement('button');
|
||||
document.body.append(button);
|
||||
button.focus();
|
||||
const first = show();
|
||||
fromPopup({ msg: 'puterPopupFrame', state: 'ready' });
|
||||
const firstDialog = popupFrame().closest('dialog');
|
||||
const second = show({ fragment: { msg_id: '4', user: USER } });
|
||||
await expect(first).resolves.toBe('closed');
|
||||
expect(firstDialog.isConnected).toBe(false);
|
||||
expect(document.querySelectorAll('dialog.puter-popup-frame')).toHaveLength(1);
|
||||
fromPopup({ msg: 'puterPopupFrame', state: 'ready' });
|
||||
fromPopup({ msg: 'puterPopupFrame', state: 'closed' });
|
||||
await expect(second).resolves.toBe('closed');
|
||||
expect(document.activeElement).toBe(button);
|
||||
});
|
||||
|
||||
it('leaves a popup frame for another action showing, as its popup window would stay open', async () => {
|
||||
const picker = show();
|
||||
fromPopup({ msg: 'puterPopupFrame', state: 'ready' });
|
||||
const pickerFrame = popupFrame();
|
||||
const signIn = show({ action: 'sign-in', fragment: { msg_id: 'a' } });
|
||||
expect(document.querySelectorAll('dialog.puter-popup-frame')).toHaveLength(2);
|
||||
const signInFrame = document.querySelectorAll('dialog.puter-popup-frame iframe')[1];
|
||||
fromPopup({ msg: 'puterPopupFrame', state: 'closed' }, { source: signInFrame.contentWindow });
|
||||
await expect(signIn).resolves.toBe('closed');
|
||||
expect(pickerFrame.isConnected).toBe(true);
|
||||
fromPopup({ msg: 'puterPopupFrame', state: 'closed' }, { source: pickerFrame.contentWindow });
|
||||
await expect(picker).resolves.toBe('closed');
|
||||
});
|
||||
|
||||
it('keeps the scrim behind a ready frame from dismissing it', () => {
|
||||
show();
|
||||
fromPopup({ msg: 'puterPopupFrame', state: 'ready' });
|
||||
document.querySelector('.puter-popup-frame-loading').click();
|
||||
expect(popupFrame().closest('dialog').isConnected).toBe(true);
|
||||
});
|
||||
|
||||
it('ignores messages from another origin or another window', async () => {
|
||||
const onMessage = vi.fn();
|
||||
show({ onMessage });
|
||||
fromPopup({ msg: 'puterPopupFrame', state: 'closed' }, { origin: 'https://other.test' });
|
||||
fromPopup({ msg: 'fileOpenPicked' }, { source: window });
|
||||
expect(onMessage).not.toHaveBeenCalled();
|
||||
expect(popupFrame().closest('dialog').isConnected).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -3,7 +3,6 @@ import { fetchUrl } from '../lib/networkUtils.js';
|
||||
import { PuterModule } from '../lib/PuterModule.js';
|
||||
import PuterDialog from './PuterDialog.js';
|
||||
import { hasOpaqueOrigin } from '../lib/auth-popup.js';
|
||||
import { ancestorsAllowGuiEmbed, canFrameGuiEmbed, showPopupFrame } from '../lib/guiEmbed.js';
|
||||
|
||||
/**
|
||||
* Puter user details, as returned by `getUser()`.
|
||||
@@ -116,19 +115,12 @@ export class AuthModule extends PuterModule {
|
||||
/** @internal */
|
||||
pendingSignInMessageIds_ = new Set();
|
||||
|
||||
/** The framed sign-in in progress, which a repeated call joins. */
|
||||
#framedSignIn = null;
|
||||
|
||||
/**
|
||||
* Signs the user in, opening a popup with the appropriate authentication
|
||||
* method. Must be called from a user gesture (such as a click) — without
|
||||
* one, a consent dialog is shown first so the popup can be opened from the
|
||||
* user's click on it. Resolves once the user has signed in.
|
||||
*
|
||||
* On a page one label under the Puter origin, the sign-in shows in a frame
|
||||
* over the page instead, and continues in the popup when the user picks a
|
||||
* federated provider or the frame can't be shown.
|
||||
*
|
||||
* Rejects with `{ error: 'popup_blocked' }` if the browser blocked the
|
||||
* popup, `{ error: 'auth_window_closed' }` if the user closed it, `{ error:
|
||||
* 'not_available_in_app' }` when called from an app — an app's token comes
|
||||
@@ -176,128 +168,12 @@ export class AuthModule extends PuterModule {
|
||||
: this.signIn(options));
|
||||
}
|
||||
|
||||
if (this.#canSignInInFrame(options)) {
|
||||
// A second call, such as from a double click or a `puter.*` call
|
||||
// asking for implicit auth, waits on the first rather than
|
||||
// replacing its frame, which would reject the first.
|
||||
if (this.#framedSignIn) return this.#framedSignIn;
|
||||
const promise = this.#signInInFrame(options).then((result) =>
|
||||
result ?? this.#signInWithPopup(options));
|
||||
const done = () => {
|
||||
if (this.#framedSignIn === promise) this.#framedSignIn = null;
|
||||
};
|
||||
this.#framedSignIn = promise;
|
||||
promise.then(done, done);
|
||||
return promise;
|
||||
}
|
||||
return this.#signInWithPopup(options);
|
||||
};
|
||||
|
||||
/**
|
||||
* Whether this page can show the sign-in framed: a first-party page one
|
||||
* label under the GUI origin, not in a frame of another site. Only the
|
||||
* popup creates a temporary account, so a sign-in that asks for one keeps
|
||||
* it.
|
||||
*/
|
||||
#canSignInInFrame (options) {
|
||||
return !options.attempt_temp_user_creation &&
|
||||
!globalThis.crossOriginIsolated &&
|
||||
!!globalThis.document?.body &&
|
||||
canFrameGuiEmbed(globalThis.location?.href, puter.defaultGUIOrigin) &&
|
||||
ancestorsAllowGuiEmbed(globalThis, puter.defaultGUIOrigin);
|
||||
}
|
||||
|
||||
/** Runs `puter.onAuth`, if set, with the user just signed in. */
|
||||
#notifyAuth () {
|
||||
if (typeof puter.onAuth !== 'function') return;
|
||||
void (async () => {
|
||||
try {
|
||||
const user = await puter.whoamiCache_;
|
||||
puter.onAuth(user ?? (await puter.getUser()));
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
})();
|
||||
}
|
||||
|
||||
/**
|
||||
* The sign-in in a frame over the page. Settles as the popup would, or
|
||||
* resolves null when the popup has to take over.
|
||||
*
|
||||
* @returns {Promise<SignInResult | null>}
|
||||
*/
|
||||
async #signInInFrame (options) {
|
||||
const msg_id = crypto.randomUUID();
|
||||
this.pendingSignInMessageIds_.add(msg_id);
|
||||
const fragment = { msg_id };
|
||||
if (options.request_auth) fragment.request_auth = 'true';
|
||||
let outcome;
|
||||
try {
|
||||
outcome = await showPopupFrame({
|
||||
guiOrigin: puter.defaultGUIOrigin,
|
||||
action: 'sign-in',
|
||||
fragment,
|
||||
title: 'Sign in to Puter',
|
||||
closeLabel: 'Close',
|
||||
closed: () => ({ closed: true }),
|
||||
onMessage: (data, finish) => {
|
||||
if (data.msg === 'puterPopupFrame' && data.state === 'provider') {
|
||||
if (this.#isProviderStart(data.url, msg_id)) finish({ providerUrl: data.url });
|
||||
return;
|
||||
}
|
||||
if (data.msg !== 'puter.token' || data.msg_id != msg_id) return;
|
||||
finish({ reply: data });
|
||||
},
|
||||
});
|
||||
} finally {
|
||||
this.pendingSignInMessageIds_.delete(msg_id);
|
||||
}
|
||||
if (!outcome) return null;
|
||||
// A provider won't load in a frame, so its sign-in continues in the
|
||||
// popup, which the provider returns as this attempt's sign-in popup.
|
||||
if (outcome.providerUrl) {
|
||||
return this.#signInWithPopup(options, { url: outcome.providerUrl, msgId: msg_id });
|
||||
}
|
||||
if (outcome.closed) {
|
||||
throw {
|
||||
error: 'auth_window_closed',
|
||||
msg: 'Authentication window was closed by the user without completing the process.',
|
||||
};
|
||||
}
|
||||
const { msg: _msg, msg_id: _msgId, ...result } = outcome.reply;
|
||||
if (!result.success) throw result;
|
||||
puter.setAuthToken(result.token);
|
||||
this.#notifyAuth();
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether `url` is the GUI's start of a provider sign-in that returns as
|
||||
* the popup for sign-in `msgId`.
|
||||
*/
|
||||
#isProviderStart (url, msgId) {
|
||||
try {
|
||||
const start = new URL(url);
|
||||
return start.origin === new URL(puter.defaultGUIOrigin).origin &&
|
||||
/^\/auth\/oidc\/[^/]+\/start$/.test(start.pathname) &&
|
||||
start.searchParams.get('embedded_in_popup') === 'true' &&
|
||||
start.searchParams.get('msg_id') === msgId;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The sign-in in a popup window, opened at `start.url` for sign-in
|
||||
* `start.msgId` when a framed sign-in hands one over.
|
||||
*/
|
||||
#signInWithPopup (options, start = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const signinsession = crypto.randomUUID();
|
||||
const msg_id = start.msgId ?? signinsession;
|
||||
const msg_id = signinsession;
|
||||
this.pendingSignInMessageIds_.add(msg_id);
|
||||
const popupName = `Puter-sign-in-${signinsession}`;
|
||||
const url = start.url ?? `${puter.defaultGUIOrigin}/action/sign-in?embedded_in_popup=true&msg_id=${msg_id}${window.crossOriginIsolated ? `&cross_origin_isolated=true&signin_session=${signinsession}` : ''}${options.attempt_temp_user_creation ? '&attempt_temp_user_creation=true' : ''}${options.request_auth ? '&request_auth=true' : ''}`;
|
||||
const url = `${puter.defaultGUIOrigin}/action/sign-in?embedded_in_popup=true&msg_id=${msg_id}${window.crossOriginIsolated ? `&cross_origin_isolated=true&signin_session=${signinsession}` : ''}${options.attempt_temp_user_creation ? '&attempt_temp_user_creation=true' : ''}${options.request_auth ? '&request_auth=true' : ''}`;
|
||||
|
||||
// Guards against settling the promise more than once across the
|
||||
// message, popup-closed, and dialog-cancel code paths.
|
||||
@@ -320,7 +196,17 @@ export class AuthModule extends PuterModule {
|
||||
consentDialog = null;
|
||||
};
|
||||
|
||||
const notifyAuth = () => this.#notifyAuth();
|
||||
const notifyAuth = () => {
|
||||
if (typeof puter.onAuth !== 'function') return;
|
||||
void (async () => {
|
||||
try {
|
||||
const user = await puter.whoamiCache_;
|
||||
puter.onAuth(user ?? (await puter.getUser()));
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
})();
|
||||
};
|
||||
|
||||
if (window.crossOriginIsolated) {
|
||||
(async () => {
|
||||
@@ -480,7 +366,7 @@ export class AuthModule extends PuterModule {
|
||||
rejectBlocked();
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Whether the user is currently signed in.
|
||||
|
||||
@@ -1,235 +0,0 @@
|
||||
// @vitest-environment jsdom
|
||||
// @vitest-environment-options {"url": "https://notes.puter.test/doc"}
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { AuthModule } from './Auth.js';
|
||||
|
||||
// An isolated page polls the sign-in relay; nothing here answers it.
|
||||
vi.mock('../lib/networkUtils.js', () => ({ fetchUrl: vi.fn(() => new Promise(() => {})) }));
|
||||
|
||||
const GUI_ORIGIN = 'https://puter.test';
|
||||
|
||||
// jsdom has no top-layer dialogs.
|
||||
HTMLDialogElement.prototype.showModal = function () {
|
||||
this.open = true;
|
||||
};
|
||||
HTMLDialogElement.prototype.close = function () {
|
||||
this.open = false;
|
||||
};
|
||||
|
||||
const signInFrame = () => document.querySelector('dialog.puter-popup-frame iframe');
|
||||
|
||||
/** The framed sign-in's `msg_id`, from its fragment. */
|
||||
const frameMsgId = () => new URLSearchParams(new URL(signInFrame().src).hash.slice(1)).get('msg_id');
|
||||
|
||||
const fromFrame = (data, source = signInFrame().contentWindow) =>
|
||||
window.dispatchEvent(new MessageEvent('message', { data, origin: GUI_ORIGIN, source }));
|
||||
|
||||
let auth;
|
||||
|
||||
beforeEach(() => {
|
||||
document.body.innerHTML = '';
|
||||
vi.stubGlobal('puter', {
|
||||
env: 'web',
|
||||
defaultGUIOrigin: GUI_ORIGIN,
|
||||
setAuthToken: vi.fn(),
|
||||
trackOpenedWindow_: vi.fn(),
|
||||
});
|
||||
vi.spyOn(window, 'open').mockImplementation((url, name) => ({ name, url, closed: false }));
|
||||
Object.defineProperty(navigator, 'userActivation', {
|
||||
configurable: true,
|
||||
value: { hasBeenActive: true, isActive: true },
|
||||
});
|
||||
auth = new AuthModule(puter);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
delete navigator.userActivation;
|
||||
vi.restoreAllMocks();
|
||||
vi.unstubAllGlobals();
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
describe('auth.signIn on a first-party page', () => {
|
||||
it('signs in through the frame instead of a popup', async () => {
|
||||
const result = auth.signIn();
|
||||
const url = new URL(signInFrame().src);
|
||||
expect(url.origin + url.pathname).toBe(`${GUI_ORIGIN}/embed/sign-in`);
|
||||
expect(url.search).toBe('');
|
||||
expect(auth.pendingSignInMessageIds_.has(frameMsgId())).toBe(true);
|
||||
|
||||
fromFrame({ msg: 'puterPopupFrame', state: 'ready' });
|
||||
fromFrame({ msg: 'puter.token', msg_id: frameMsgId(), success: true, token: 'tok', app_uid: 'app-1', username: 'alice' });
|
||||
|
||||
await expect(result).resolves.toEqual({ success: true, token: 'tok', app_uid: 'app-1', username: 'alice' });
|
||||
expect(puter.setAuthToken).toHaveBeenCalledWith('tok');
|
||||
expect(window.open).not.toHaveBeenCalled();
|
||||
expect(signInFrame()).toBeNull();
|
||||
expect(auth.pendingSignInMessageIds_.size).toBe(0);
|
||||
});
|
||||
|
||||
it('passes request_auth on to the frame', () => {
|
||||
auth.signIn({ request_auth: true }).catch(() => {});
|
||||
expect(new URLSearchParams(new URL(signInFrame().src).hash.slice(1)).get('request_auth')).toBe('true');
|
||||
});
|
||||
|
||||
it('ignores a token for another sign-in or from another window', async () => {
|
||||
const result = auth.signIn();
|
||||
let settled = false;
|
||||
result.then(() => {
|
||||
settled = true;
|
||||
}, () => {
|
||||
settled = true;
|
||||
});
|
||||
fromFrame({ msg: 'puter.token', msg_id: 'other', success: true, token: 'x' });
|
||||
fromFrame({ msg: 'puter.token', msg_id: frameMsgId(), success: true, token: 'x' }, window);
|
||||
await Promise.resolve();
|
||||
expect(settled).toBe(false);
|
||||
expect(puter.setAuthToken).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('rejects as a closed popup when the user closes the frame', async () => {
|
||||
const result = auth.signIn();
|
||||
fromFrame({ msg: 'puterPopupFrame', state: 'ready' });
|
||||
document.querySelector('.puter-popup-frame-close').click();
|
||||
await expect(result).rejects.toMatchObject({ error: 'auth_window_closed' });
|
||||
expect(window.open).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('rejects as a closed popup when the frame closes without a token', async () => {
|
||||
const result = auth.signIn();
|
||||
fromFrame({ msg: 'puterPopupFrame', state: 'closed' });
|
||||
await expect(result).rejects.toMatchObject({ error: 'auth_window_closed' });
|
||||
});
|
||||
|
||||
it('shares one framed sign-in between calls made while it is open', async () => {
|
||||
const first = auth.signIn();
|
||||
const second = auth.signIn();
|
||||
expect(document.querySelectorAll('dialog.puter-popup-frame')).toHaveLength(1);
|
||||
fromFrame({ msg: 'puterPopupFrame', state: 'ready' });
|
||||
fromFrame({ msg: 'puter.token', msg_id: frameMsgId(), success: true, token: 'tok' });
|
||||
await expect(first).resolves.toEqual({ success: true, token: 'tok' });
|
||||
await expect(second).resolves.toEqual({ success: true, token: 'tok' });
|
||||
|
||||
// Once it settles, the next call starts a new one.
|
||||
const third = auth.signIn();
|
||||
expect(signInFrame()).not.toBeNull();
|
||||
fromFrame({ msg: 'puterPopupFrame', state: 'closed' });
|
||||
await expect(third).rejects.toMatchObject({ error: 'auth_window_closed' });
|
||||
});
|
||||
|
||||
it('shares the framed sign-in with a call that differs in request_auth', async () => {
|
||||
// Implicit auth (a `puter.*` call without a token) asks with request_auth.
|
||||
const implicit = auth.signIn({ request_auth: true });
|
||||
const explicit = auth.signIn();
|
||||
expect(document.querySelectorAll('dialog.puter-popup-frame')).toHaveLength(1);
|
||||
fromFrame({ msg: 'puterPopupFrame', state: 'ready' });
|
||||
fromFrame({ msg: 'puter.token', msg_id: frameMsgId(), success: true, token: 'tok' });
|
||||
await expect(implicit).resolves.toEqual({ success: true, token: 'tok' });
|
||||
await expect(explicit).resolves.toEqual({ success: true, token: 'tok' });
|
||||
});
|
||||
|
||||
it('rejects with the reply a failed sign-in sends, as the popup does', async () => {
|
||||
const result = auth.signIn();
|
||||
fromFrame({ msg: 'puter.token', msg_id: frameMsgId(), success: false, token: null });
|
||||
await expect(result).rejects.toEqual({ success: false, token: null });
|
||||
expect(puter.setAuthToken).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('continues in the popup when the frame hands off, such as for a federated provider', async () => {
|
||||
const result = auth.signIn();
|
||||
fromFrame({ msg: 'puterPopupFrame', state: 'loading' });
|
||||
fromFrame({ msg: 'puterPopupFrame', state: 'unavailable' });
|
||||
await vi.waitFor(() => expect(window.open).toHaveBeenCalledTimes(1));
|
||||
const [url] = window.open.mock.calls[0];
|
||||
expect(url).toContain(`${GUI_ORIGIN}/action/sign-in?embedded_in_popup=true`);
|
||||
expect(signInFrame()).toBeNull();
|
||||
|
||||
const popup = window.open.mock.results[0].value;
|
||||
const msgId = new URL(url).searchParams.get('msg_id');
|
||||
window.dispatchEvent(new MessageEvent('message', {
|
||||
data: { msg: 'puter.token', msg_id: msgId, success: true, token: 'tok' },
|
||||
origin: GUI_ORIGIN,
|
||||
source: popup,
|
||||
}));
|
||||
await expect(result).resolves.toMatchObject({ success: true, token: 'tok' });
|
||||
});
|
||||
|
||||
it('opens the provider the user picked in the frame straight in the popup', async () => {
|
||||
const result = auth.signIn();
|
||||
const msgId = frameMsgId();
|
||||
const start = `${GUI_ORIGIN}/auth/oidc/google/start?flow=login&referrer=x&embedded_in_popup=true&msg_id=${msgId}&opener_origin=https%3A%2F%2Fnotes.puter.test`;
|
||||
const frameWindow = signInFrame().contentWindow;
|
||||
fromFrame({ msg: 'puterPopupFrame', state: 'ready' });
|
||||
fromFrame({ msg: 'puterPopupFrame', state: 'provider', url: start });
|
||||
fromFrame({ msg: 'puterPopupFrame', state: 'unavailable' }, frameWindow);
|
||||
await vi.waitFor(() => expect(window.open).toHaveBeenCalledTimes(1));
|
||||
expect(window.open.mock.calls[0][0]).toBe(start);
|
||||
expect(signInFrame()).toBeNull();
|
||||
expect(auth.pendingSignInMessageIds_.has(msgId)).toBe(true);
|
||||
|
||||
// The provider returns the popup as a sign-in popup for the same attempt.
|
||||
window.dispatchEvent(new MessageEvent('message', {
|
||||
data: { msg: 'puter.token', msg_id: msgId, success: true, token: 'tok' },
|
||||
origin: GUI_ORIGIN,
|
||||
source: window.open.mock.results[0].value,
|
||||
}));
|
||||
await expect(result).resolves.toMatchObject({ success: true, token: 'tok' });
|
||||
expect(puter.setAuthToken).toHaveBeenCalledWith('tok');
|
||||
expect(auth.pendingSignInMessageIds_.size).toBe(0);
|
||||
});
|
||||
|
||||
it.each([
|
||||
['another origin', (id) => `https://evil.test/auth/oidc/google/start?embedded_in_popup=true&msg_id=${id}`],
|
||||
['another path', (id) => `${GUI_ORIGIN}/action/sign-in?embedded_in_popup=true&msg_id=${id}&auth_token=x`],
|
||||
['another sign-in', () => `${GUI_ORIGIN}/auth/oidc/google/start?embedded_in_popup=true&msg_id=other`],
|
||||
['no popup return', (id) => `${GUI_ORIGIN}/auth/oidc/google/start?msg_id=${id}`],
|
||||
['no URL', () => undefined],
|
||||
])('opens the sign-in popup instead of a provider URL to %s', async (_, url) => {
|
||||
auth.signIn();
|
||||
fromFrame({ msg: 'puterPopupFrame', state: 'provider', url: url(frameMsgId()) });
|
||||
fromFrame({ msg: 'puterPopupFrame', state: 'unavailable' });
|
||||
await vi.waitFor(() => expect(window.open).toHaveBeenCalledTimes(1));
|
||||
expect(window.open.mock.calls[0][0]).toContain(`${GUI_ORIGIN}/action/sign-in?embedded_in_popup=true`);
|
||||
});
|
||||
|
||||
it('continues in the popup when the frame is refused', async () => {
|
||||
vi.useFakeTimers();
|
||||
auth.signIn();
|
||||
signInFrame().dispatchEvent(new Event('load'));
|
||||
await vi.advanceTimersByTimeAsync(1000);
|
||||
expect(window.open).toHaveBeenCalledTimes(1);
|
||||
expect(signInFrame()).toBeNull();
|
||||
});
|
||||
|
||||
it('keeps the popup for a temporary account', () => {
|
||||
auth.signIn({ attempt_temp_user_creation: true });
|
||||
expect(signInFrame()).toBeNull();
|
||||
expect(window.open).toHaveBeenCalledTimes(1);
|
||||
expect(window.open.mock.calls[0][0]).toContain('attempt_temp_user_creation=true');
|
||||
});
|
||||
|
||||
it('keeps the popup for a page outside the Puter origin', () => {
|
||||
puter.defaultGUIOrigin = 'https://puter.com';
|
||||
auth.signIn();
|
||||
expect(signInFrame()).toBeNull();
|
||||
expect(window.open).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('keeps the popup for a page in a frame of another site', () => {
|
||||
Object.defineProperty(window.location, 'ancestorOrigins', { configurable: true, value: ['https://other.test'] });
|
||||
try {
|
||||
auth.signIn();
|
||||
expect(signInFrame()).toBeNull();
|
||||
expect(window.open).toHaveBeenCalledTimes(1);
|
||||
} finally {
|
||||
delete window.location.ancestorOrigins;
|
||||
}
|
||||
});
|
||||
|
||||
it('keeps the popup for a cross-origin isolated page', () => {
|
||||
vi.stubGlobal('crossOriginIsolated', true);
|
||||
auth.signIn().catch(() => {});
|
||||
expect(signInFrame()).toBeNull();
|
||||
expect(window.open).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
+63
-208
@@ -2,8 +2,6 @@ import EventListener from '../lib/EventListener.js';
|
||||
import { PuterJSError } from '../lib/PuterJSError.js';
|
||||
import FSItem from './FSItem.js';
|
||||
import PuterDialog from './PuterDialog.js';
|
||||
import { ancestorsAllowGuiEmbed, canFrameGuiEmbed, showFeedbackEmbed, showPopupFrame } from '../lib/guiEmbed.js';
|
||||
import { hasUserActivation } from '../lib/auth-popup.js';
|
||||
import { checkPermissions } from './perms/lib/holds.js';
|
||||
|
||||
|
||||
@@ -530,9 +528,6 @@ export class UIModule extends EventListener {
|
||||
#guiFeatures = new URLSearchParams(globalThis.location?.search ?? '')
|
||||
.get('puter.gui_features')?.split(',') ?? [];
|
||||
|
||||
// Set while the framed feedback dialog is open on a first-party page.
|
||||
#feedbackEmbedOpen = false;
|
||||
|
||||
// Holds the callback functions for the various events
|
||||
// that are triggered when a watched item has changed.
|
||||
itemWatchCallbackFunctions = [];
|
||||
@@ -583,131 +578,6 @@ export class UIModule extends EventListener {
|
||||
if ( popup ) this.#pickerPopups.add(popup);
|
||||
}
|
||||
|
||||
/** What a picker's consent dialog says, when the popup needs a click to open. */
|
||||
static #PICKER_CONSENT = {
|
||||
'show-open-file-picker': 'This website wants you to choose a file from your Puter account.',
|
||||
'show-save-file-picker': 'This website wants to save a file to your Puter account.',
|
||||
'show-directory-picker': 'This website wants you to choose a folder from your Puter account.',
|
||||
};
|
||||
|
||||
/** Opens a picker's popup window, centered, at the pickers' size. */
|
||||
#openPickerPopup (url, title) {
|
||||
const w = 700;
|
||||
const h = 400;
|
||||
const left = (screen.width / 2) - (w / 2);
|
||||
const top = (screen.height / 2) - (h / 2);
|
||||
return window.open(
|
||||
url,
|
||||
title,
|
||||
`toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=${w}, height=${h}, top=${top}, left=${left}`,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows a picker on a `web` page. A first-party page one label under the
|
||||
* Puter origin that is signed in gets it in a frame over the page; every
|
||||
* other page, or a frame that can't serve this user, gets the popup.
|
||||
* `onWindow` sees the window that will ask for and answer the picker, with
|
||||
* `framed` set for a frame, and may return a function that stops listening
|
||||
* to it.
|
||||
*
|
||||
* @param {Object} opts
|
||||
* @param {'show-open-file-picker' | 'show-save-file-picker' | 'show-directory-picker'} opts.action
|
||||
* @param {number} opts.msgId
|
||||
* @param {string} opts.url - the popup's URL
|
||||
* @param {string} opts.title - the popup's window name, and the frame's accessible name
|
||||
* @param {Record<string, string>} [opts.fragment] - the popup parameters the frame needs besides `msg_id`
|
||||
* @param {(win: Window | null, how?: { framed: boolean }) => (() => void) | void} [opts.onWindow]
|
||||
*/
|
||||
#showWebPicker ({ action, msgId, url, title, fragment = {}, onWindow }) {
|
||||
if ( ! this.#canFramePicker() ) {
|
||||
// window.open() returns synchronously and the popup cannot post
|
||||
// back until this function yields, so there is no race.
|
||||
const popup = this.#openPickerPopup(url, title);
|
||||
onWindow?.(popup);
|
||||
this.#trackPickerPopup(popup);
|
||||
return;
|
||||
}
|
||||
void (async () => {
|
||||
if ( await this.#showPickerFrame({ action, msgId, title, fragment, onWindow }) ) return;
|
||||
// The click that asked for the picker may have expired by now, so
|
||||
// without it the user opens the popup from a consent dialog.
|
||||
const launched = (popup) => {
|
||||
onWindow?.(popup);
|
||||
this.#trackPickerPopup(popup);
|
||||
};
|
||||
if ( hasUserActivation() ) {
|
||||
const popup = this.#openPickerPopup(url, title);
|
||||
if ( popup ) {
|
||||
launched(popup);
|
||||
return;
|
||||
}
|
||||
}
|
||||
let consent = null;
|
||||
try {
|
||||
consent = PuterDialog.show({
|
||||
message: UIModule.#PICKER_CONSENT[action],
|
||||
onContinue: () => {
|
||||
launched(this.#openPickerPopup(url, title));
|
||||
consent?.remove();
|
||||
},
|
||||
onCancel: () => {
|
||||
consent?.remove();
|
||||
this.#settleCallback(msgId, PICKER_CANCELLED);
|
||||
},
|
||||
});
|
||||
} catch {
|
||||
// No dialog either; as with a blocked popup, the picker never opens.
|
||||
}
|
||||
})();
|
||||
}
|
||||
|
||||
/** Whether a picker can show framed: a signed-in first-party page, not in a frame of another site. */
|
||||
#canFramePicker () {
|
||||
return this.env === 'web' &&
|
||||
!globalThis.crossOriginIsolated &&
|
||||
!!globalThis.document?.body &&
|
||||
canFrameGuiEmbed(globalThis.location?.href, puter.defaultGUIOrigin) &&
|
||||
ancestorsAllowGuiEmbed(globalThis, puter.defaultGUIOrigin) &&
|
||||
!!puter.auth?.isSignedIn?.();
|
||||
}
|
||||
|
||||
/**
|
||||
* The picker in a frame over the page, for the page's signed-in user. The
|
||||
* GUI answers it as it would the popup, through the module's message
|
||||
* handler. Resolves false when the popup has to take over.
|
||||
*
|
||||
* @returns {Promise<boolean>}
|
||||
*/
|
||||
async #showPickerFrame ({ action, msgId, title, fragment, onWindow }) {
|
||||
const known = await Promise.resolve(puter.whoami ?? puter.whoamiCache_).catch(() => null);
|
||||
const user = known?.uuid ? known : await puter.auth.getUser().catch(() => null);
|
||||
if ( typeof user?.uuid !== 'string' || ! user.uuid ) return false;
|
||||
let frame = null;
|
||||
let stopListening;
|
||||
// The frame only serves this page's own user, so the token it sends
|
||||
// with the answer isn't adopted; the page keeps the one it has.
|
||||
const closed = await showPopupFrame({
|
||||
guiOrigin: puter.defaultGUIOrigin,
|
||||
action,
|
||||
fragment: { ...fragment, msg_id: String(msgId), user: user.uuid },
|
||||
title,
|
||||
closed: () => true,
|
||||
onMessage: () => {},
|
||||
onFrame: (win) => {
|
||||
frame = win;
|
||||
this.#pickerPopups.add(win);
|
||||
stopListening = onWindow?.(win, { framed: true });
|
||||
},
|
||||
});
|
||||
this.#pickerPopups.delete(frame);
|
||||
stopListening?.();
|
||||
if ( ! closed ) return false;
|
||||
// Gone without an answer: dismissed, so it settles as a cancel.
|
||||
this.#settleCallback(msgId, PICKER_CANCELLED);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Whether a message on the window may drive this module.
|
||||
#isTrustedMessageSource (e) {
|
||||
if ( this.messageTarget ) {
|
||||
@@ -1508,12 +1378,20 @@ export class UIModule extends EventListener {
|
||||
env: this.env,
|
||||
}, '*');
|
||||
} else {
|
||||
this.#showWebPicker({
|
||||
action: 'show-directory-picker',
|
||||
msgId: msg_id,
|
||||
url: `${puter.defaultGUIOrigin}/action/show-directory-picker?embedded_in_popup=true&msg_id=${msg_id}&appInstanceID=${this.appInstanceID}&env=${this.env}&options=${JSON.stringify(options)}`,
|
||||
title: 'Puter: Open Directory',
|
||||
});
|
||||
let w = 700;
|
||||
let h = 400;
|
||||
let title = 'Puter: Open Directory';
|
||||
var left = (screen.width / 2) - (w / 2);
|
||||
var top = (screen.height / 2) - (h / 2);
|
||||
// Track the window we opened so the message listener accepts
|
||||
// its reply. window.open() returns synchronously and the popup
|
||||
// cannot post back until this function yields, so there is no
|
||||
// race.
|
||||
this.#trackPickerPopup(window.open(
|
||||
`${puter.defaultGUIOrigin}/action/show-directory-picker?embedded_in_popup=true&msg_id=${msg_id}&appInstanceID=${this.appInstanceID}&env=${this.env}&options=${JSON.stringify(options)}`,
|
||||
title,
|
||||
`toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=${w}, height=${h}, top=${top}, left=${left}`,
|
||||
));
|
||||
}
|
||||
|
||||
this.#registerPickerCallback(msg_id, resolve, undefinedOnCancel);
|
||||
@@ -1550,13 +1428,16 @@ export class UIModule extends EventListener {
|
||||
env: this.env,
|
||||
}, '*');
|
||||
} else {
|
||||
this.#showWebPicker({
|
||||
action: 'show-open-file-picker',
|
||||
msgId: msg_id,
|
||||
url: `${puter.defaultGUIOrigin}/action/show-open-file-picker?embedded_in_popup=true&msg_id=${msg_id}&appInstanceID=${this.appInstanceID}&env=${this.env}&options=${JSON.stringify(options ?? {})}`,
|
||||
title: 'Puter: Open File',
|
||||
fragment: { options: JSON.stringify(options ?? {}) },
|
||||
});
|
||||
let w = 700;
|
||||
let h = 400;
|
||||
let title = 'Puter: Open File';
|
||||
var left = (screen.width / 2) - (w / 2);
|
||||
var top = (screen.height / 2) - (h / 2);
|
||||
this.#trackPickerPopup(window.open(
|
||||
`${puter.defaultGUIOrigin}/action/show-open-file-picker?embedded_in_popup=true&msg_id=${msg_id}&appInstanceID=${this.appInstanceID}&env=${this.env}&options=${JSON.stringify(options ?? {})}`,
|
||||
title,
|
||||
`toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=${w}, height=${h}, top=${top}, left=${left}`,
|
||||
));
|
||||
}
|
||||
this.#registerPickerCallback(msg_id, resolve, undefinedOnCancel);
|
||||
});
|
||||
@@ -1754,43 +1635,47 @@ export class UIModule extends EventListener {
|
||||
// Create an object URL for the Blob
|
||||
let objectUrl = URL.createObjectURL(blob);
|
||||
|
||||
// Only hand the file content to the Puter GUI window that shows
|
||||
// the picker (its popup, or its frame on a first-party page).
|
||||
// Without this, any framing parent or sibling iframe could post
|
||||
let w = 700;
|
||||
let h = 400;
|
||||
let title = 'Puter: Save File';
|
||||
var left = (screen.width / 2) - (w / 2);
|
||||
var top = (screen.height / 2) - (h / 2);
|
||||
|
||||
// Open the picker popup first so we can bind the data handler to the
|
||||
// exact window we opened. window.open() returns synchronously and the
|
||||
// popup cannot post back until this function yields, so there is no race.
|
||||
const popup = window.open(
|
||||
`${puter.defaultGUIOrigin}/action/show-save-file-picker?embedded_in_popup=true&msg_id=${msg_id}&appInstanceID=${this.appInstanceID}&env=${this.env}&blobUrl=${encodeURIComponent(objectUrl)}`,
|
||||
title,
|
||||
`toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=${w}, height=${h}, top=${top}, left=${left}`,
|
||||
);
|
||||
|
||||
// Only hand the file content to the trusted Puter GUI popup we just
|
||||
// opened. Without this, any framing parent or sibling iframe could post
|
||||
// { msg: 'sendMeFileData' } and receive the content the page meant to save.
|
||||
// A frame that reloads asks again, so it's answered until it
|
||||
// closes; a popup is answered once.
|
||||
const sendFileDataTo = (picker, { framed = false } = {}) => {
|
||||
const onSendMeFileData = (e) => {
|
||||
if ( e.data?.msg !== 'sendMeFileData' ) return;
|
||||
// Reject anything that isn't our picker on the trusted GUI origin.
|
||||
if ( e.origin !== puter.defaultGUIOrigin ) return;
|
||||
if ( ! picker || e.source !== picker ) return;
|
||||
const onSendMeFileData = (e) => {
|
||||
if ( e.data?.msg !== 'sendMeFileData' ) return;
|
||||
// Reject anything that isn't our popup on the trusted GUI origin.
|
||||
if ( e.origin !== puter.defaultGUIOrigin ) return;
|
||||
if ( ! popup || e.source !== popup ) return;
|
||||
|
||||
// Send the file data to the picker, targeting the trusted origin only.
|
||||
e.source.postMessage({
|
||||
msg: 'showSaveFilePickerPopup',
|
||||
content: url ? undefined : content,
|
||||
url: url ? url.toString() : undefined,
|
||||
suggestedName: suggestedName ?? '',
|
||||
env: this.env,
|
||||
uuid: msg_id,
|
||||
}, puter.defaultGUIOrigin);
|
||||
// Send the file data to the popup, targeting the trusted origin only.
|
||||
e.source.postMessage({
|
||||
msg: 'showSaveFilePickerPopup',
|
||||
content: url ? undefined : content,
|
||||
url: url ? url.toString() : undefined,
|
||||
suggestedName: suggestedName ?? '',
|
||||
env: this.env,
|
||||
uuid: msg_id,
|
||||
}, puter.defaultGUIOrigin);
|
||||
|
||||
// A popup has the data now; remove this exact listener.
|
||||
if ( ! framed ) window.removeEventListener('message', onSendMeFileData);
|
||||
};
|
||||
window.addEventListener('message', onSendMeFileData);
|
||||
return () => window.removeEventListener('message', onSendMeFileData);
|
||||
// The trusted popup has the data; remove this exact listener.
|
||||
window.removeEventListener('message', onSendMeFileData);
|
||||
};
|
||||
window.addEventListener('message', onSendMeFileData);
|
||||
|
||||
this.#showWebPicker({
|
||||
action: 'show-save-file-picker',
|
||||
msgId: msg_id,
|
||||
url: `${puter.defaultGUIOrigin}/action/show-save-file-picker?embedded_in_popup=true&msg_id=${msg_id}&appInstanceID=${this.appInstanceID}&env=${this.env}&blobUrl=${encodeURIComponent(objectUrl)}`,
|
||||
title: 'Puter: Save File',
|
||||
onWindow: sendFileDataTo,
|
||||
});
|
||||
// Same window, for the picker's own reply on the main listener.
|
||||
this.#trackPickerPopup(popup);
|
||||
}
|
||||
this.#registerPickerCallback(msg_id, resolve, undefinedOnCancel);
|
||||
});
|
||||
@@ -2364,12 +2249,9 @@ export class UIModule extends EventListener {
|
||||
* have opted in by setting `feedbackEnabled` on the app — otherwise the
|
||||
* dialog tells the user feedback is unavailable.
|
||||
*
|
||||
* In the `app` environment the Puter desktop renders the dialog. In the
|
||||
* `web` environment a first-party page one label under the Puter origin
|
||||
* shows it in a frame over the page when the user's Puter session is the
|
||||
* one the page is signed in as; otherwise a puter.com popup hosts it
|
||||
* (signing the user in first if needed). Every other environment
|
||||
* resolves `false`.
|
||||
* In the `app` environment the Puter desktop renders the dialog; in the
|
||||
* `web` environment a puter.com popup hosts it (signing the user in first
|
||||
* if needed). Every other environment resolves `false`.
|
||||
*
|
||||
* @returns {Promise<boolean>} `true` when the user submitted feedback,
|
||||
* `false` when the dialog was dismissed or feedback is unavailable.
|
||||
@@ -2394,10 +2276,6 @@ export class UIModule extends EventListener {
|
||||
if ( this.env !== 'web' ) {
|
||||
return false;
|
||||
}
|
||||
const embedded = await this.#showFeedbackEmbed();
|
||||
if ( embedded ) {
|
||||
return embedded.sent;
|
||||
}
|
||||
// The app's identity is deliberately NOT in the popup URL: the GUI
|
||||
// derives it from the browser-attested opener origin, so a link
|
||||
// can't open a feedback dialog in another app's name.
|
||||
@@ -2409,29 +2287,6 @@ export class UIModule extends EventListener {
|
||||
return result?.sent === true;
|
||||
};
|
||||
|
||||
/**
|
||||
* The framed feedback dialog for a first-party page that is signed in, or
|
||||
* null when the popup has to host it. One at a time: a call while one is
|
||||
* open resolves `{ sent: false }`, as the desktop does.
|
||||
*
|
||||
* @returns {Promise<{ sent: boolean } | null>}
|
||||
*/
|
||||
async #showFeedbackEmbed () {
|
||||
if ( ! globalThis.document?.body ) return null;
|
||||
if ( ! canFrameGuiEmbed(globalThis.location?.href, puter.defaultGUIOrigin) ) return null;
|
||||
if ( ! puter.auth?.isSignedIn?.() ) return null;
|
||||
if ( this.#feedbackEmbedOpen ) return { sent: false };
|
||||
this.#feedbackEmbedOpen = true;
|
||||
try {
|
||||
const known = await Promise.resolve(puter.whoami ?? puter.whoamiCache_).catch(() => null);
|
||||
const user = known?.uuid ? known : await puter.auth.getUser().catch(() => null);
|
||||
if ( typeof user?.uuid !== 'string' || ! user.uuid ) return null;
|
||||
return await showFeedbackEmbed({ guiOrigin: puter.defaultGUIOrigin, userUuid: user.uuid });
|
||||
} finally {
|
||||
this.#feedbackEmbedOpen = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens `/action/<action>` on the GUI origin in a popup and waits for
|
||||
* its single result message. Resolves the message's data, or `null` when
|
||||
|
||||
@@ -1,236 +0,0 @@
|
||||
// @vitest-environment jsdom
|
||||
// @vitest-environment-options {"url": "https://notes.puter.test/doc"}
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
const { UIModule } = await import('./UI.js');
|
||||
|
||||
// jsdom has no top-layer dialogs.
|
||||
HTMLDialogElement.prototype.showModal = function () {
|
||||
this.open = true;
|
||||
};
|
||||
HTMLDialogElement.prototype.close = function () {
|
||||
this.open = false;
|
||||
};
|
||||
|
||||
const GUI_ORIGIN = 'https://puter.test';
|
||||
const USER = '0b1c9a52-3f0e-4c7a-9a65-2f6d1e8b7c40';
|
||||
|
||||
const pickerFrame = () => document.querySelector('dialog.puter-popup-frame iframe');
|
||||
const fragment = () => Object.fromEntries(new URLSearchParams(new URL(pickerFrame().src).hash.slice(1)));
|
||||
|
||||
const fromFrame = (data, source = pickerFrame().contentWindow) =>
|
||||
window.dispatchEvent(new MessageEvent('message', { data, origin: GUI_ORIGIN, source }));
|
||||
|
||||
const makeUI = () => new UIModule(
|
||||
{ env: 'web', authToken: 'token-1', APIOrigin: 'https://api.puter.test', appID: 'app-1', util: {} },
|
||||
{ appInstanceID: 'instance-1' },
|
||||
);
|
||||
|
||||
const fakePuter = ({ signedIn = true } = {}) => {
|
||||
globalThis.puter = {
|
||||
defaultGUIOrigin: GUI_ORIGIN,
|
||||
whoamiCache_: Promise.resolve({ uuid: USER }),
|
||||
trackOpenedWindow_: vi.fn(),
|
||||
auth: {
|
||||
isSignedIn: () => signedIn,
|
||||
getUser: vi.fn(async () => ({ uuid: USER })),
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
const PENDING = Symbol('pending');
|
||||
const settled = (promise) => Promise.race([promise, new Promise((r) => setTimeout(() => r(PENDING), 0))]);
|
||||
|
||||
let popup;
|
||||
|
||||
beforeEach(() => {
|
||||
document.body.innerHTML = '';
|
||||
popup = { closed: false, postMessage: vi.fn() };
|
||||
window.open = vi.fn(() => popup);
|
||||
Object.defineProperty(navigator, 'userActivation', {
|
||||
configurable: true,
|
||||
value: { hasBeenActive: true, isActive: true },
|
||||
});
|
||||
fakePuter();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
delete navigator.userActivation;
|
||||
delete globalThis.puter;
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
describe('pickers on a signed-in first-party page', () => {
|
||||
it('shows the open file picker in a frame and resolves what it picks', async () => {
|
||||
const ui = makeUI();
|
||||
const result = ui.showOpenFilePicker({ multiple: false, accept: '.txt' });
|
||||
await vi.waitFor(() => expect(pickerFrame()).not.toBeNull());
|
||||
const url = new URL(pickerFrame().src);
|
||||
expect(url.pathname).toBe('/embed/show-open-file-picker');
|
||||
expect(fragment()).toEqual({ options: '{"multiple":false,"accept":".txt"}', msg_id: '1', user: USER });
|
||||
|
||||
fromFrame({ msg: 'puterPopupFrame', state: 'ready' });
|
||||
fromFrame({
|
||||
msg: 'fileOpenPicked',
|
||||
original_msg_id: '1',
|
||||
items: [{ uid: 'uid-1', name: 'a.txt', path: '~/Desktop/a.txt' }],
|
||||
});
|
||||
await expect(result).resolves.toMatchObject({ uid: 'uid-1', name: 'a.txt' });
|
||||
fromFrame({ msg: 'puterPopupFrame', state: 'closed' });
|
||||
expect(pickerFrame()).toBeNull();
|
||||
expect(window.open).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('keeps the page token instead of adopting the one the frame sends', async () => {
|
||||
makeUI().showDirectoryPicker();
|
||||
await vi.waitFor(() => expect(pickerFrame()).not.toBeNull());
|
||||
expect(puter.trackOpenedWindow_).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('settles a picker the GUI reports cancelled', async () => {
|
||||
const result = makeUI().showDirectoryPicker();
|
||||
await vi.waitFor(() => expect(pickerFrame()).not.toBeNull());
|
||||
expect(new URL(pickerFrame().src).pathname).toBe('/embed/show-directory-picker');
|
||||
fromFrame({ msg: 'directoryOpenCancelled', original_msg_id: '1' });
|
||||
fromFrame({ msg: 'puterPopupFrame', state: 'closed' });
|
||||
await expect(result.undefinedOnCancel).resolves.toBeUndefined();
|
||||
expect(await settled(result)).toBe(PENDING);
|
||||
});
|
||||
|
||||
it('settles as cancelled when the frame is dismissed without an answer', async () => {
|
||||
vi.useFakeTimers();
|
||||
const result = makeUI().showOpenFilePicker();
|
||||
await vi.waitFor(() => expect(pickerFrame()).not.toBeNull());
|
||||
await vi.advanceTimersByTimeAsync(200);
|
||||
document.querySelector('.puter-popup-frame-loading').click();
|
||||
await expect(result.undefinedOnCancel).resolves.toBeUndefined();
|
||||
expect(pickerFrame()).toBeNull();
|
||||
});
|
||||
|
||||
it('hands the file content to the save picker frame, and only to it', async () => {
|
||||
const result = makeUI().showSaveFilePicker('hello', 'a.txt');
|
||||
await vi.waitFor(() => expect(pickerFrame()).not.toBeNull());
|
||||
expect(new URL(pickerFrame().src).pathname).toBe('/embed/show-save-file-picker');
|
||||
const frameWindow = pickerFrame().contentWindow;
|
||||
const post = vi.spyOn(frameWindow, 'postMessage').mockImplementation(() => {});
|
||||
|
||||
window.dispatchEvent(new MessageEvent('message', { data: { msg: 'sendMeFileData' }, origin: GUI_ORIGIN, source: window }));
|
||||
window.dispatchEvent(new MessageEvent('message', { data: { msg: 'sendMeFileData' }, origin: 'https://other.test', source: frameWindow }));
|
||||
expect(post).not.toHaveBeenCalled();
|
||||
|
||||
fromFrame({ msg: 'sendMeFileData' });
|
||||
expect(post).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ msg: 'showSaveFilePickerPopup', content: 'hello', suggestedName: 'a.txt' }),
|
||||
GUI_ORIGIN,
|
||||
);
|
||||
fromFrame({ msg: 'fileSaved', original_msg_id: '1', saved_file: { name: 'a.txt', uid: 'uid-2' } });
|
||||
await expect(result).resolves.toMatchObject({ name: 'a.txt' });
|
||||
});
|
||||
|
||||
it('hands the file content again to a save picker frame that reloads, until it closes', async () => {
|
||||
const result = makeUI().showSaveFilePicker('hello', 'a.txt');
|
||||
await vi.waitFor(() => expect(pickerFrame()).not.toBeNull());
|
||||
const frameWindow = pickerFrame().contentWindow;
|
||||
const post = vi.spyOn(frameWindow, 'postMessage').mockImplementation(() => {});
|
||||
|
||||
fromFrame({ msg: 'sendMeFileData' });
|
||||
// The reloaded document asks again.
|
||||
fromFrame({ msg: 'sendMeFileData' });
|
||||
expect(post).toHaveBeenCalledTimes(2);
|
||||
expect(post.mock.calls[1][0]).toMatchObject({ msg: 'showSaveFilePickerPopup', content: 'hello' });
|
||||
|
||||
fromFrame({ msg: 'puterPopupFrame', state: 'closed' });
|
||||
await expect(result.undefinedOnCancel).resolves.toBeUndefined();
|
||||
fromFrame({ msg: 'sendMeFileData' }, frameWindow);
|
||||
expect(post).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('opens the popup instead when the frame cannot serve the user', async () => {
|
||||
const ui = makeUI();
|
||||
const result = ui.showOpenFilePicker();
|
||||
await vi.waitFor(() => expect(pickerFrame()).not.toBeNull());
|
||||
fromFrame({ msg: 'puterPopupFrame', state: 'loading' });
|
||||
fromFrame({ msg: 'puterPopupFrame', state: 'unavailable' });
|
||||
await vi.waitFor(() => expect(window.open).toHaveBeenCalledTimes(1));
|
||||
expect(pickerFrame()).toBeNull();
|
||||
expect(window.open.mock.calls[0][0]).toContain(`${GUI_ORIGIN}/action/show-open-file-picker?embedded_in_popup=true&msg_id=1`);
|
||||
expect(puter.trackOpenedWindow_).toHaveBeenCalledWith(popup);
|
||||
|
||||
// The popup's answer reaches the picker as before.
|
||||
window.dispatchEvent(new MessageEvent('message', {
|
||||
data: { msg: 'fileOpenPicked', original_msg_id: 1, items: [{ uid: 'uid-1', name: 'a.txt' }] },
|
||||
origin: GUI_ORIGIN,
|
||||
source: popup,
|
||||
}));
|
||||
await expect(result).resolves.toMatchObject({ uid: 'uid-1' });
|
||||
});
|
||||
|
||||
it('asks for a click before opening the popup once the original click has expired', async () => {
|
||||
const result = makeUI().showOpenFilePicker();
|
||||
await vi.waitFor(() => expect(pickerFrame()).not.toBeNull());
|
||||
Object.defineProperty(navigator, 'userActivation', {
|
||||
configurable: true,
|
||||
value: { hasBeenActive: true, isActive: false },
|
||||
});
|
||||
fromFrame({ msg: 'puterPopupFrame', state: 'unavailable' });
|
||||
await vi.waitFor(() => expect(document.querySelector('puter-dialog')).not.toBeNull());
|
||||
expect(window.open).not.toHaveBeenCalled();
|
||||
expect(document.querySelector('puter-dialog').shadowRoot.querySelector('.about').textContent)
|
||||
.toBe('This website wants you to choose a file from your Puter account.');
|
||||
|
||||
document.querySelector('puter-dialog').shadowRoot.querySelector('#launch-auth-popup').click();
|
||||
expect(window.open).toHaveBeenCalledTimes(1);
|
||||
expect(window.open.mock.calls[0][2]).toContain('width=700, height=400');
|
||||
expect(document.querySelector('puter-dialog')).toBeNull();
|
||||
|
||||
const cancelled = makeUI().showDirectoryPicker();
|
||||
await vi.waitFor(() => expect(pickerFrame()).not.toBeNull());
|
||||
fromFrame({ msg: 'puterPopupFrame', state: 'unavailable' });
|
||||
await vi.waitFor(() => expect(document.querySelector('puter-dialog')).not.toBeNull());
|
||||
expect(document.querySelector('puter-dialog').shadowRoot.querySelector('.about').textContent)
|
||||
.toBe('This website wants you to choose a folder from your Puter account.');
|
||||
document.querySelector('puter-dialog').shadowRoot.querySelector('#launch-auth-popup-cancel').click();
|
||||
await expect(cancelled.undefinedOnCancel).resolves.toBeUndefined();
|
||||
expect(document.querySelector('puter-dialog')).toBeNull();
|
||||
expect(await settled(result)).toBe(PENDING);
|
||||
expect(await settled(result.undefinedOnCancel)).toBe(PENDING);
|
||||
});
|
||||
|
||||
it('opens the popup when the page has no user to name', async () => {
|
||||
puter.whoamiCache_ = Promise.resolve(null);
|
||||
puter.auth.getUser = vi.fn(async () => {
|
||||
throw new Error('nope');
|
||||
});
|
||||
makeUI().showDirectoryPicker();
|
||||
await vi.waitFor(() => expect(window.open).toHaveBeenCalledTimes(1));
|
||||
expect(pickerFrame()).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('pickers that keep the popup', () => {
|
||||
it('opens it synchronously for a signed-out first-party page', () => {
|
||||
fakePuter({ signedIn: false });
|
||||
makeUI().showOpenFilePicker();
|
||||
expect(window.open).toHaveBeenCalledTimes(1);
|
||||
expect(pickerFrame()).toBeNull();
|
||||
});
|
||||
|
||||
it('opens it synchronously for a page in a frame of another site', () => {
|
||||
Object.defineProperty(window.location, 'ancestorOrigins', { configurable: true, value: ['https://other.test'] });
|
||||
try {
|
||||
makeUI().showOpenFilePicker();
|
||||
expect(window.open).toHaveBeenCalledTimes(1);
|
||||
expect(pickerFrame()).toBeNull();
|
||||
} finally {
|
||||
delete window.location.ancestorOrigins;
|
||||
}
|
||||
});
|
||||
|
||||
it('opens it synchronously for a page outside the Puter origin', () => {
|
||||
globalThis.puter.defaultGUIOrigin = 'https://puter.com';
|
||||
makeUI().showSaveFilePicker('x', 'a.txt');
|
||||
makeUI().showDirectoryPicker();
|
||||
expect(window.open).toHaveBeenCalledTimes(2);
|
||||
expect(pickerFrame()).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -1,111 +0,0 @@
|
||||
// @vitest-environment jsdom
|
||||
// @vitest-environment-options {"url": "https://notes.puter.test/doc"}
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
const { UIModule } = await import('./UI.js');
|
||||
|
||||
// jsdom has no top-layer dialogs.
|
||||
HTMLDialogElement.prototype.showModal = function () {
|
||||
this.open = true;
|
||||
};
|
||||
|
||||
const GUI_ORIGIN = 'https://puter.test';
|
||||
const USER = '0b1c9a52-3f0e-4c7a-9a65-2f6d1e8b7c40';
|
||||
|
||||
const makeUI = () => new UIModule(
|
||||
{ env: 'web', authToken: 'token-1', APIOrigin: 'https://api.puter.test', util: {} },
|
||||
{},
|
||||
);
|
||||
|
||||
const embedFrame = () => document.querySelector('dialog.puter-feedback-embed iframe');
|
||||
|
||||
const fromEmbed = (data) => window.dispatchEvent(new MessageEvent('message', {
|
||||
data, origin: GUI_ORIGIN, source: embedFrame().contentWindow,
|
||||
}));
|
||||
|
||||
let popup;
|
||||
|
||||
const fakePuter = ({ guiOrigin = GUI_ORIGIN, signedIn = true } = {}) => {
|
||||
globalThis.puter = {
|
||||
defaultGUIOrigin: guiOrigin,
|
||||
auth: {
|
||||
isSignedIn: () => signedIn,
|
||||
getUser: vi.fn(async () => ({ uuid: USER })),
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
document.body.innerHTML = '';
|
||||
popup = { closed: false, close: vi.fn() };
|
||||
window.open = vi.fn(() => popup);
|
||||
Object.defineProperty(navigator, 'userActivation', {
|
||||
configurable: true,
|
||||
value: { hasBeenActive: true, isActive: true },
|
||||
});
|
||||
fakePuter();
|
||||
});
|
||||
|
||||
describe('ui.showFeedbackDialog on a first-party page', () => {
|
||||
it('shows the dialog in a frame instead of a popup', async () => {
|
||||
const result = makeUI().showFeedbackDialog();
|
||||
await vi.waitFor(() => expect(embedFrame()).not.toBeNull());
|
||||
fromEmbed({ msg: 'feedbackEmbed', state: 'ready' });
|
||||
fromEmbed({ msg: 'feedbackDialogClosed', sent: true });
|
||||
await expect(result).resolves.toBe(true);
|
||||
expect(window.open).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('names the user puter.js already knows, without asking the API again', async () => {
|
||||
globalThis.puter.whoamiCache_ = Promise.resolve({ uuid: USER });
|
||||
const result = makeUI().showFeedbackDialog();
|
||||
await vi.waitFor(() => expect(embedFrame()).not.toBeNull());
|
||||
expect(new URLSearchParams(new URL(embedFrame().src).hash.slice(1)).get('user')).toBe(USER);
|
||||
expect(globalThis.puter.auth.getUser).not.toHaveBeenCalled();
|
||||
fromEmbed({ msg: 'feedbackDialogClosed', sent: false });
|
||||
await expect(result).resolves.toBe(false);
|
||||
});
|
||||
|
||||
it('uses the popup when the embed cannot serve the user', async () => {
|
||||
const result = makeUI().showFeedbackDialog();
|
||||
await vi.waitFor(() => expect(embedFrame()).not.toBeNull());
|
||||
fromEmbed({ msg: 'feedbackEmbed', state: 'unavailable' });
|
||||
await vi.waitFor(() => expect(window.open).toHaveBeenCalledTimes(1));
|
||||
const url = new URL(window.open.mock.calls[0][0]);
|
||||
expect(url.origin + url.pathname).toBe(`${GUI_ORIGIN}/action/send-feedback`);
|
||||
|
||||
window.dispatchEvent(new MessageEvent('message', {
|
||||
data: { msg: 'feedbackDialogClosed', sent: true, original_msg_id: url.searchParams.get('msg_id') },
|
||||
origin: GUI_ORIGIN,
|
||||
source: popup,
|
||||
}));
|
||||
await expect(result).resolves.toBe(true);
|
||||
});
|
||||
|
||||
it('answers false to a second call while the framed dialog is open', async () => {
|
||||
const ui = makeUI();
|
||||
const first = ui.showFeedbackDialog();
|
||||
await vi.waitFor(() => expect(embedFrame()).not.toBeNull());
|
||||
await expect(ui.showFeedbackDialog()).resolves.toBe(false);
|
||||
expect(document.querySelectorAll('dialog.puter-feedback-embed')).toHaveLength(1);
|
||||
fromEmbed({ msg: 'feedbackDialogClosed', sent: false });
|
||||
await expect(first).resolves.toBe(false);
|
||||
});
|
||||
|
||||
it('goes straight to the popup, which can sign in, when the page is signed out', async () => {
|
||||
fakePuter({ signedIn: false });
|
||||
makeUI().showFeedbackDialog();
|
||||
await vi.waitFor(() => expect(window.open).toHaveBeenCalledTimes(1));
|
||||
expect(embedFrame()).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('ui.showFeedbackDialog on a third-party page', () => {
|
||||
it('uses the popup', async () => {
|
||||
fakePuter({ guiOrigin: 'https://other-puter.test' });
|
||||
makeUI().showFeedbackDialog();
|
||||
await vi.waitFor(() => expect(window.open).toHaveBeenCalledTimes(1));
|
||||
expect(embedFrame()).toBeNull();
|
||||
expect(globalThis.puter.auth.getUser).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -4,17 +4,17 @@ import {
|
||||
CLOSE_MESSAGE_TOO_LARGE,
|
||||
RealtimeSession,
|
||||
createRealtimeSocket,
|
||||
} from './realtimeTransport.js';
|
||||
} from './convoTransport.js';
|
||||
import { RealtimeEvents } from './realtimeEvents.js';
|
||||
import { RealtimeMicrophone, RealtimeSpeaker } from './realtimeMedia.js';
|
||||
import { RealtimeHistory } from './realtimeHistory.js';
|
||||
import { ConvoMicrophone, ConvoSpeaker } from './convoMedia.js';
|
||||
import { ConvoHistory } from './convoHistory.js';
|
||||
|
||||
/** @typedef {import('./types.js').RealtimeOptions} RealtimeOptions */
|
||||
/** @typedef {import('./types.js').RealtimeSessionOptions} RealtimeSessionOptions */
|
||||
/** @typedef {import('./types.js').RealtimePlaybackPosition} RealtimePlaybackPosition */
|
||||
/** @typedef {import('./types.js').ConvoOptions} ConvoOptions */
|
||||
/** @typedef {import('./types.js').ConvoSessionOptions} ConvoSessionOptions */
|
||||
/** @typedef {import('./types.js').ConvoPlaybackPosition} ConvoPlaybackPosition */
|
||||
/** @typedef {import('./types.js').RealtimeServerEvent} RealtimeServerEvent */
|
||||
/** @typedef {import('./types.js').RealtimeClientEvent} RealtimeClientEvent */
|
||||
/** @typedef {{ code: string; message: string }} RealtimeError */
|
||||
/** @typedef {{ code: string; message: string }} ConvoError */
|
||||
/**
|
||||
* A pending reconnect notice. A replacement session opens right away and takes
|
||||
* over once the conversation is idle.
|
||||
@@ -29,7 +29,7 @@ import { RealtimeHistory } from './realtimeHistory.js';
|
||||
* probe: RealtimeSession | null;
|
||||
* created: RealtimeServerEvent | null;
|
||||
* stopProbe: (() => void) | null;
|
||||
* }} RealtimeHandover
|
||||
* }} ConvoHandover
|
||||
* @internal
|
||||
*/
|
||||
|
||||
@@ -106,7 +106,7 @@ const encoder = new TextEncoder();
|
||||
/**
|
||||
* @param {string} code
|
||||
* @param {string} message
|
||||
* @returns {RealtimeError}
|
||||
* @returns {ConvoError}
|
||||
*/
|
||||
const failure = (code, message) => ({ code, message });
|
||||
|
||||
@@ -138,7 +138,7 @@ function mergeSession(base, patch) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {RealtimeOptions} options
|
||||
* @param {ConvoOptions} options
|
||||
* @param {boolean} [allowModel]
|
||||
* @returns {void}
|
||||
*/
|
||||
@@ -261,12 +261,12 @@ function encodeEvent(event) {
|
||||
/**
|
||||
* A prepared realtime conversation with optional media helpers.
|
||||
*
|
||||
* @extends {RealtimeEvents<import('./types.js').RealtimeEventMap>}
|
||||
* @extends {RealtimeEvents<import('./types.js').ConvoEventMap>}
|
||||
*/
|
||||
export class Conversation extends RealtimeEvents {
|
||||
/** @type {import('../../index.js').Puter} */
|
||||
#puter;
|
||||
/** @type {RealtimeOptions} */
|
||||
/** @type {ConvoOptions} */
|
||||
#options;
|
||||
/** @type {'prepared' | 'connecting' | 'connected' | 'closed'} */
|
||||
#state = 'prepared';
|
||||
@@ -285,7 +285,7 @@ export class Conversation extends RealtimeEvents {
|
||||
* type: string;
|
||||
* matches: (event: RealtimeServerEvent) => boolean;
|
||||
* resolve: (event: RealtimeServerEvent) => void;
|
||||
* reject: (error: RealtimeError) => void;
|
||||
* reject: (error: ConvoError) => void;
|
||||
* timer: ReturnType<typeof setTimeout>;
|
||||
* eventId?: string;
|
||||
* anyError?: boolean;
|
||||
@@ -313,7 +313,7 @@ export class Conversation extends RealtimeEvents {
|
||||
/**
|
||||
* @type {{
|
||||
* resolve: () => void;
|
||||
* reject: (error: RealtimeError) => void;
|
||||
* reject: (error: ConvoError) => void;
|
||||
* }[]}
|
||||
*/
|
||||
#reconnectWaiters = [];
|
||||
@@ -330,8 +330,8 @@ export class Conversation extends RealtimeEvents {
|
||||
*/
|
||||
#interruption = null;
|
||||
#cancelled = new Set();
|
||||
#history = new RealtimeHistory((id) => this.#cancelled.has(id));
|
||||
/** @type {RealtimeHandover | null} */
|
||||
#history = new ConvoHistory((id) => this.#cancelled.has(id));
|
||||
/** @type {ConvoHandover | null} */
|
||||
#handover = null;
|
||||
// Speech started and its audio isn't committed or cleared yet.
|
||||
#speaking = false;
|
||||
@@ -358,17 +358,17 @@ export class Conversation extends RealtimeEvents {
|
||||
/** @type {Set<string>} */
|
||||
#committedItems = new Set();
|
||||
// Replayed `conversation.item.create` event IDs on the current session.
|
||||
/** @type {Map<string, import('./realtimeHistory.js').ReplayEntry>} */
|
||||
/** @type {Map<string, import('./convoHistory.js').ReplayEntry>} */
|
||||
#replayEvents = new Map();
|
||||
#replayReported = false;
|
||||
/** @type {RealtimeMicrophone} */
|
||||
/** @type {ConvoMicrophone} */
|
||||
microphone;
|
||||
/** @type {RealtimeSpeaker} */
|
||||
/** @type {ConvoSpeaker} */
|
||||
speaker;
|
||||
|
||||
/**
|
||||
* @param {import('../../index.js').Puter} puter
|
||||
* @param {RealtimeOptions} options
|
||||
* @param {ConvoOptions} options
|
||||
* @internal
|
||||
*/
|
||||
constructor(puter, options) {
|
||||
@@ -380,8 +380,8 @@ export class Conversation extends RealtimeEvents {
|
||||
model: options.model ?? DEFAULT_MODEL,
|
||||
};
|
||||
this.#turnDetection = this.#options.turnDetection !== false;
|
||||
this.microphone = new RealtimeMicrophone(this);
|
||||
this.speaker = new RealtimeSpeaker(this);
|
||||
this.microphone = new ConvoMicrophone(this);
|
||||
this.speaker = new ConvoSpeaker(this);
|
||||
}
|
||||
|
||||
/** @returns {'prepared' | 'connecting' | 'connected' | 'closed'} */
|
||||
@@ -807,7 +807,7 @@ export class Conversation extends RealtimeEvents {
|
||||
* restarting, is retried with backoff while the current session keeps
|
||||
* working; any other refusal ends the attempts.
|
||||
*
|
||||
* @param {RealtimeHandover} handover
|
||||
* @param {ConvoHandover} handover
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async #openReplacement(handover) {
|
||||
@@ -887,7 +887,7 @@ export class Conversation extends RealtimeEvents {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {RealtimeHandover} handover
|
||||
* @param {ConvoHandover} handover
|
||||
* @param {string} [code]
|
||||
* @returns {void}
|
||||
*/
|
||||
@@ -1011,7 +1011,7 @@ export class Conversation extends RealtimeEvents {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {RealtimeSessionOptions} options
|
||||
* @param {ConvoSessionOptions} options
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async configure(options) {
|
||||
@@ -1025,7 +1025,7 @@ export class Conversation extends RealtimeEvents {
|
||||
* new session needs. A partial update leaves settings made through native
|
||||
* events alone.
|
||||
*
|
||||
* @param {RealtimeSessionOptions} options
|
||||
* @param {ConvoSessionOptions} options
|
||||
* @param {boolean} [full]
|
||||
* @param {number} [ackMs]
|
||||
* @returns {Promise<void>}
|
||||
@@ -1168,7 +1168,7 @@ export class Conversation extends RealtimeEvents {
|
||||
* Stops local playback immediately and waits for cancellation and
|
||||
* truncation.
|
||||
*
|
||||
* @param {RealtimePlaybackPosition} [position]
|
||||
* @param {ConvoPlaybackPosition} [position]
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
interrupt(position) {
|
||||
@@ -1181,7 +1181,7 @@ export class Conversation extends RealtimeEvents {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {RealtimePlaybackPosition | undefined} position
|
||||
* @param {ConvoPlaybackPosition | undefined} position
|
||||
* @param {'manual' | 'speech'} source
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
@@ -1323,7 +1323,7 @@ export class Conversation extends RealtimeEvents {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {RealtimeError} error
|
||||
* @param {ConvoError} error
|
||||
* @returns {void}
|
||||
* @internal
|
||||
*/
|
||||
@@ -1347,7 +1347,7 @@ export class Conversation extends RealtimeEvents {
|
||||
promptIfUpgradeRequired(
|
||||
error,
|
||||
{
|
||||
method: 'puter.ai.realtime',
|
||||
method: 'puter.ai.convo',
|
||||
subscriptionMessage:
|
||||
'Realtime conversations require a subscription.',
|
||||
},
|
||||
@@ -1404,7 +1404,7 @@ export class Conversation extends RealtimeEvents {
|
||||
}
|
||||
}
|
||||
if (event.type === RECONNECT_NOTICE && !this.#handover) {
|
||||
/** @type {RealtimeHandover} */
|
||||
/** @type {ConvoHandover} */
|
||||
const handover = {
|
||||
reason:
|
||||
typeof event.reason === 'string' && event.reason
|
||||
@@ -1707,7 +1707,7 @@ export class Conversation extends RealtimeEvents {
|
||||
|
||||
/** @returns {string} */
|
||||
#eventId() {
|
||||
return `puter_realtime_${++this.#sequence}`;
|
||||
return `puter_convo_${++this.#sequence}`;
|
||||
}
|
||||
/**
|
||||
* Sends events together or not at all. `limit` caps the socket's send
|
||||
@@ -1830,7 +1830,7 @@ export class Conversation extends RealtimeEvents {
|
||||
return this.#wait(type, matches, event.event_id, false, timeoutMs);
|
||||
}
|
||||
/**
|
||||
* @param {RealtimeError} error
|
||||
* @param {ConvoError} error
|
||||
* @returns {void}
|
||||
*/
|
||||
#rejectPending(error) {
|
||||
@@ -1847,10 +1847,10 @@ export class Conversation extends RealtimeEvents {
|
||||
* connection.
|
||||
*
|
||||
* @this {import('./index.js').AIModule}
|
||||
* @param {RealtimeOptions} [options]
|
||||
* @param {ConvoOptions} [options]
|
||||
* @returns {Promise<Conversation>}
|
||||
*/
|
||||
export async function realtime(options = {}) {
|
||||
export async function convo(options = {}) {
|
||||
validateOptions(options, true);
|
||||
const settings = {
|
||||
...options,
|
||||
+5
-5
@@ -1,5 +1,5 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { Conversation, realtime } from './realtime.js';
|
||||
import { Conversation, convo } from './convo.js';
|
||||
|
||||
class FakeWebSocket extends EventTarget {
|
||||
static instances = [];
|
||||
@@ -283,14 +283,14 @@ describe('conversation preparation', () => {
|
||||
'rejects invalid options before opening a socket (%j)',
|
||||
async (options) => {
|
||||
await expect(
|
||||
realtime.call({ puter: owner() }, options),
|
||||
convo.call({ puter: owner() }, options),
|
||||
).rejects.toMatchObject({ code: expect.any(String) });
|
||||
expect(FakeWebSocket.instances).toHaveLength(0);
|
||||
},
|
||||
);
|
||||
it('requires authentication outside a browser', async () => {
|
||||
await expect(
|
||||
realtime.call({ puter: { ...owner(), authToken: null } }),
|
||||
convo.call({ puter: { ...owner(), authToken: null } }),
|
||||
).rejects.toMatchObject({ code: 'auth_required' });
|
||||
});
|
||||
it('reports canceled browser authentication', async () => {
|
||||
@@ -304,7 +304,7 @@ describe('conversation preparation', () => {
|
||||
},
|
||||
},
|
||||
};
|
||||
await expect(realtime.call({ puter })).rejects.toMatchObject({
|
||||
await expect(convo.call({ puter })).rejects.toMatchObject({
|
||||
code: 'auth_canceled',
|
||||
});
|
||||
});
|
||||
@@ -988,7 +988,7 @@ it('preflights both events of a text turn before sending either', async () => {
|
||||
socket().bufferedAmount =
|
||||
192 * 1024 -
|
||||
new TextEncoder().encode(
|
||||
JSON.stringify({ ...item, event_id: 'puter_realtime_99' }),
|
||||
JSON.stringify({ ...item, event_id: 'puter_convo_99' }),
|
||||
).byteLength;
|
||||
expect(() => c.sendText(text)).toThrow(
|
||||
expect.objectContaining({ code: 'queue_full' }),
|
||||
+4
-4
@@ -4,7 +4,7 @@
|
||||
* An item `conversation.item.create` accepts: a text message, a function call
|
||||
* or a function call's output.
|
||||
*
|
||||
* @typedef {Record<string, unknown> & { type: string }} RealtimeReplayItem
|
||||
* @typedef {Record<string, unknown> & { type: string }} ConvoReplayItem
|
||||
*/
|
||||
|
||||
/**
|
||||
@@ -29,7 +29,7 @@
|
||||
* @property {string} [output]
|
||||
*/
|
||||
|
||||
/** @typedef {{ record: HistoryRecord; item: RealtimeReplayItem }} ReplayEntry */
|
||||
/** @typedef {{ record: HistoryRecord; item: ConvoReplayItem }} ReplayEntry */
|
||||
|
||||
const ITEM_EVENTS = new Set([
|
||||
'conversation.item.added',
|
||||
@@ -99,7 +99,7 @@ function partText(part) {
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
export class RealtimeHistory {
|
||||
export class ConvoHistory {
|
||||
/** @type {HistoryRecord[]} */
|
||||
#records = [];
|
||||
/** @type {Map<string, HistoryRecord>} */
|
||||
@@ -426,7 +426,7 @@ export class RealtimeHistory {
|
||||
* The `conversation.item.create` item that recreates `record`, or `null`.
|
||||
*
|
||||
* @param {HistoryRecord} record
|
||||
* @returns {RealtimeReplayItem | null}
|
||||
* @returns {ConvoReplayItem | null}
|
||||
*/
|
||||
#replayItem(record) {
|
||||
if (!record.final) return null;
|
||||
+9
-9
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { RealtimeHistory } from './realtimeHistory.js';
|
||||
import { ConvoHistory } from './convoHistory.js';
|
||||
|
||||
const user = (id, content) => ({
|
||||
type: 'conversation.item.added',
|
||||
@@ -10,9 +10,9 @@ const texts = (history) =>
|
||||
.replay(40, 48 * 1024)
|
||||
.entries.map(({ item }) => item.content[0].text);
|
||||
|
||||
describe('RealtimeHistory', () => {
|
||||
describe('ConvoHistory', () => {
|
||||
it('cuts text without word breaks at the played share', () => {
|
||||
const history = new RealtimeHistory(() => false);
|
||||
const history = new ConvoHistory(() => false);
|
||||
history.observe({
|
||||
type: 'response.output_item.added',
|
||||
response_id: 'r',
|
||||
@@ -40,7 +40,7 @@ describe('RealtimeHistory', () => {
|
||||
});
|
||||
|
||||
it('keeps a known transcript when a later copy of the item lacks it', () => {
|
||||
const history = new RealtimeHistory(() => false);
|
||||
const history = new ConvoHistory(() => false);
|
||||
history.observe(user('u', [{ type: 'input_audio', transcript: null }]));
|
||||
expect(texts(history)).toEqual([]);
|
||||
history.observe({
|
||||
@@ -57,7 +57,7 @@ describe('RealtimeHistory', () => {
|
||||
});
|
||||
|
||||
it('keeps a bounded number of the newest items', () => {
|
||||
const history = new RealtimeHistory(() => false);
|
||||
const history = new ConvoHistory(() => false);
|
||||
for (let i = 0; i < 100; i++)
|
||||
history.observe(
|
||||
user(`u${i}`, [{ type: 'input_text', text: `${i}` }]),
|
||||
@@ -67,7 +67,7 @@ describe('RealtimeHistory', () => {
|
||||
});
|
||||
|
||||
it('stops at the first item over the byte budget instead of leaving a gap', () => {
|
||||
const history = new RealtimeHistory(() => false);
|
||||
const history = new ConvoHistory(() => false);
|
||||
history.observe(user('u1', [{ type: 'input_text', text: 'old' }]));
|
||||
history.observe(
|
||||
user('u2', [{ type: 'input_text', text: 'x'.repeat(50_000) }]),
|
||||
@@ -79,7 +79,7 @@ describe('RealtimeHistory', () => {
|
||||
it.each(['x'.repeat(49 * 1024), '😀'.repeat(13 * 1024)])(
|
||||
'reports an oversized newest item without truncating it',
|
||||
(text) => {
|
||||
const history = new RealtimeHistory(() => false);
|
||||
const history = new ConvoHistory(() => false);
|
||||
history.observe(
|
||||
user('old', [{ type: 'input_text', text: 'tiny' }]),
|
||||
);
|
||||
@@ -96,7 +96,7 @@ describe('RealtimeHistory', () => {
|
||||
);
|
||||
|
||||
it('waits on every unanswered call since the last user message', () => {
|
||||
const history = new RealtimeHistory(() => false);
|
||||
const history = new ConvoHistory(() => false);
|
||||
const call = (id, status = 'completed') =>
|
||||
history.observe({
|
||||
type: 'response.output_item.done',
|
||||
@@ -133,7 +133,7 @@ describe('RealtimeHistory', () => {
|
||||
});
|
||||
|
||||
it('reports the new session copies of replayed items until they are done', () => {
|
||||
const history = new RealtimeHistory(() => false);
|
||||
const history = new ConvoHistory(() => false);
|
||||
history.observe(user('old', [{ type: 'input_text', text: 'hi' }]));
|
||||
history.replayed(history.replay(40, 48 * 1024).entries);
|
||||
const copy = user('new', [{ type: 'input_text', text: 'hi' }]);
|
||||
+31
-32
@@ -1,6 +1,6 @@
|
||||
/** @typedef {import('./realtime.js').Conversation} Conversation */
|
||||
/** @typedef {import('./types.js').RealtimePlaybackPosition} RealtimePlaybackPosition */
|
||||
/** @typedef {import('./types.js').RealtimeSpeakerOptions} RealtimeSpeakerOptions */
|
||||
/** @typedef {import('./convo.js').Conversation} Conversation */
|
||||
/** @typedef {import('./types.js').ConvoPlaybackPosition} ConvoPlaybackPosition */
|
||||
/** @typedef {import('./types.js').ConvoSpeakerOptions} ConvoSpeakerOptions */
|
||||
/** @typedef {import('./types.js').RealtimeServerEvent} RealtimeServerEvent */
|
||||
|
||||
const SAMPLE_RATE = 24000;
|
||||
@@ -69,7 +69,7 @@ function audioContext(sampleRate) {
|
||||
// The worklet owns resampling. Chunks the main thread hasn't taken within
|
||||
// about half a second are dropped rather than queued.
|
||||
const CAPTURE_WORKLET = `
|
||||
class PuterRealtimeCapture extends AudioWorkletProcessor {
|
||||
class PuterConvoCapture extends AudioWorkletProcessor {
|
||||
constructor() {
|
||||
super();
|
||||
this.samples = new Int16Array(480);
|
||||
@@ -108,13 +108,13 @@ class PuterRealtimeCapture extends AudioWorkletProcessor {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
registerProcessor('puter-realtime-capture', PuterRealtimeCapture);
|
||||
registerProcessor('puter-convo-capture', PuterConvoCapture);
|
||||
`;
|
||||
|
||||
/** Optional browser microphone capture. */
|
||||
export class RealtimeMicrophone {
|
||||
export class ConvoMicrophone {
|
||||
/** @type {Conversation} */
|
||||
#conversation;
|
||||
#convo;
|
||||
/** @type {MediaStream | null} */
|
||||
#stream = null;
|
||||
/** @type {AudioContext | null} */
|
||||
@@ -131,11 +131,11 @@ export class RealtimeMicrophone {
|
||||
#cancelStart = null;
|
||||
|
||||
/**
|
||||
* @param {Conversation} conversation
|
||||
* @param {Conversation} convo
|
||||
* @internal
|
||||
*/
|
||||
constructor(conversation) {
|
||||
this.#conversation = conversation;
|
||||
constructor(convo) {
|
||||
this.#convo = convo;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -145,7 +145,7 @@ export class RealtimeMicrophone {
|
||||
*/
|
||||
start() {
|
||||
try {
|
||||
this.#conversation.assertConnected();
|
||||
this.#convo.assertConnected();
|
||||
} catch (error) {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
@@ -184,8 +184,7 @@ export class RealtimeMicrophone {
|
||||
this.#context = context;
|
||||
// A reconnect keeps capture running; chunks are dropped meanwhile.
|
||||
const current = () =>
|
||||
generation === this.#generation &&
|
||||
this.#conversation.state !== 'closed';
|
||||
generation === this.#generation && this.#convo.state !== 'closed';
|
||||
const permission = navigator.mediaDevices
|
||||
.getUserMedia({
|
||||
audio: {
|
||||
@@ -229,27 +228,27 @@ export class RealtimeMicrophone {
|
||||
}
|
||||
if (!current())
|
||||
throw { code: 'session_closed', message: 'Capture was stopped' };
|
||||
this.#node = new AudioWorkletNode(context, 'puter-realtime-capture');
|
||||
this.#node = new AudioWorkletNode(context, 'puter-convo-capture');
|
||||
this.#source = context.createMediaStreamSource(stream);
|
||||
const node = this.#node;
|
||||
node.port.onmessage = ({ data }) => {
|
||||
if (!current()) return;
|
||||
node.port.postMessage('ack');
|
||||
if (this.#muted || this.#conversation.state !== 'connected') return;
|
||||
if (this.#muted || this.#convo.state !== 'connected') return;
|
||||
try {
|
||||
this.#conversation.appendAudio(data.pcm);
|
||||
this.#convo.appendAudio(data.pcm);
|
||||
} catch (error) {
|
||||
// A congested socket, or one closing before the conversation
|
||||
// knows it, drops the chunk instead of ending capture: a
|
||||
// reconnect resumes it and a final close stops it.
|
||||
if (DROPPED_CHUNK_ERRORS.has(error?.code)) return;
|
||||
this.stop();
|
||||
this.#conversation.reportMediaError(error);
|
||||
this.#convo.reportMediaError(error);
|
||||
}
|
||||
};
|
||||
node.onprocessorerror = () => {
|
||||
this.stop();
|
||||
this.#conversation.reportMediaError({
|
||||
this.#convo.reportMediaError({
|
||||
code: 'media_error',
|
||||
message: 'Microphone processor failed',
|
||||
});
|
||||
@@ -299,9 +298,9 @@ export class RealtimeMicrophone {
|
||||
}
|
||||
|
||||
/** Optional bounded browser PCM playback. */
|
||||
export class RealtimeSpeaker {
|
||||
export class ConvoSpeaker {
|
||||
/** @type {Conversation} */
|
||||
#conversation;
|
||||
#convo;
|
||||
/** @type {AudioContext | null} */
|
||||
#context = null;
|
||||
/** @type {Promise<void> | null} */
|
||||
@@ -318,7 +317,7 @@ export class RealtimeSpeaker {
|
||||
* }>}
|
||||
*/
|
||||
#sources = new Set();
|
||||
/** @type {(RealtimePlaybackPosition & { responseId: string }) | null} */
|
||||
/** @type {(ConvoPlaybackPosition & { responseId: string }) | null} */
|
||||
#last = null;
|
||||
#nextAt = 0;
|
||||
#bargeIn = true;
|
||||
@@ -326,11 +325,11 @@ export class RealtimeSpeaker {
|
||||
#overflowed = '';
|
||||
|
||||
/**
|
||||
* @param {Conversation} conversation
|
||||
* @param {Conversation} convo
|
||||
* @internal
|
||||
*/
|
||||
constructor(conversation) {
|
||||
this.#conversation = conversation;
|
||||
constructor(convo) {
|
||||
this.#convo = convo;
|
||||
}
|
||||
/**
|
||||
* @returns {boolean}
|
||||
@@ -366,12 +365,12 @@ export class RealtimeSpeaker {
|
||||
/**
|
||||
* Starts playback without requesting microphone access.
|
||||
*
|
||||
* @param {RealtimeSpeakerOptions} [options]
|
||||
* @param {ConvoSpeakerOptions} [options]
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
start(options = {}) {
|
||||
try {
|
||||
this.#conversation.assertConnected();
|
||||
this.#convo.assertConnected();
|
||||
} catch (error) {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
@@ -425,7 +424,7 @@ export class RealtimeSpeaker {
|
||||
const responseId = String(event.response_id ?? '');
|
||||
if (this.#overflowed !== responseId) {
|
||||
this.#overflowed = responseId;
|
||||
this.#conversation.reportMediaError({
|
||||
this.#convo.reportMediaError({
|
||||
code: 'queue_full',
|
||||
message: `Playback exceeds ${MAX_SCHEDULED_SECONDS} seconds`,
|
||||
});
|
||||
@@ -472,7 +471,7 @@ export class RealtimeSpeaker {
|
||||
this.#last.playedMs,
|
||||
(offset + duration) * 1000,
|
||||
);
|
||||
if (!this.#sources.size) this.#conversation.playbackEnded();
|
||||
if (!this.#sources.size) this.#convo.playbackEnded();
|
||||
};
|
||||
this.#nextAt = source.end;
|
||||
node.start(start);
|
||||
@@ -484,7 +483,7 @@ export class RealtimeSpeaker {
|
||||
* is scheduled, it is the end of what played for `responseId`.
|
||||
*
|
||||
* @param {string} [responseId] The response still generating, if any.
|
||||
* @returns {RealtimePlaybackPosition | undefined}
|
||||
* @returns {ConvoPlaybackPosition | undefined}
|
||||
* @internal
|
||||
*/
|
||||
playbackPosition(responseId) {
|
||||
@@ -504,7 +503,7 @@ export class RealtimeSpeaker {
|
||||
* Each item with scheduled audio the listener hasn't fully heard, in play
|
||||
* order, with how much of it was heard.
|
||||
*
|
||||
* @returns {RealtimePlaybackPosition[]}
|
||||
* @returns {ConvoPlaybackPosition[]}
|
||||
* @internal
|
||||
*/
|
||||
unheard() {
|
||||
@@ -512,7 +511,7 @@ export class RealtimeSpeaker {
|
||||
const heard =
|
||||
(context?.currentTime ?? 0) -
|
||||
(context?.outputLatency || context?.baseLatency || 0);
|
||||
/** @type {Map<string, RealtimePlaybackPosition>} */
|
||||
/** @type {Map<string, ConvoPlaybackPosition>} */
|
||||
const items = new Map();
|
||||
for (const source of this.#sources) {
|
||||
if (source.end <= heard || items.has(source.itemId)) continue;
|
||||
@@ -541,7 +540,7 @@ export class RealtimeSpeaker {
|
||||
this.#sources.clear();
|
||||
this.#last = null;
|
||||
this.#nextAt = 0;
|
||||
if (playing) this.#conversation.playbackEnded();
|
||||
if (playing) this.#convo.playbackEnded();
|
||||
}
|
||||
|
||||
/**
|
||||
+21
-21
@@ -1,5 +1,5 @@
|
||||
import { afterEach, beforeEach, expect, it, vi } from 'vitest';
|
||||
import { RealtimeMicrophone, RealtimeSpeaker } from './realtimeMedia.js';
|
||||
import { ConvoMicrophone, ConvoSpeaker } from './convoMedia.js';
|
||||
|
||||
class FakeContext {
|
||||
static instances = [];
|
||||
@@ -43,7 +43,7 @@ class FakeWorklet {
|
||||
connect = vi.fn();
|
||||
disconnect = vi.fn();
|
||||
}
|
||||
const makeConversation = () => ({
|
||||
const makeConvo = () => ({
|
||||
state: 'connected',
|
||||
assertConnected() {
|
||||
if (this.state !== 'connected') throw { code: 'session_closed' };
|
||||
@@ -73,7 +73,7 @@ afterEach(async () => {
|
||||
});
|
||||
|
||||
it('captures PCM, acknowledges bounded worklet chunks, mutes and releases tracks', async () => {
|
||||
const c = makeConversation();
|
||||
const c = makeConvo();
|
||||
const audioTrack = track();
|
||||
vi.stubGlobal('navigator', {
|
||||
mediaDevices: {
|
||||
@@ -83,7 +83,7 @@ it('captures PCM, acknowledges bounded worklet chunks, mutes and releases tracks
|
||||
})),
|
||||
},
|
||||
});
|
||||
const mic = new RealtimeMicrophone(c);
|
||||
const mic = new ConvoMicrophone(c);
|
||||
helpers.push(mic);
|
||||
const pending = mic.start();
|
||||
expect(mic.start()).toBe(pending);
|
||||
@@ -115,7 +115,7 @@ it('releases a stream when permission resolves after stop', async () => {
|
||||
}),
|
||||
},
|
||||
});
|
||||
const mic = new RealtimeMicrophone(makeConversation());
|
||||
const mic = new ConvoMicrophone(makeConvo());
|
||||
helpers.push(mic);
|
||||
const pending = mic.start();
|
||||
mic.stop();
|
||||
@@ -134,7 +134,7 @@ it('settles start() when stopped during the permission prompt', async () => {
|
||||
}),
|
||||
},
|
||||
});
|
||||
const mic = new RealtimeMicrophone(makeConversation());
|
||||
const mic = new ConvoMicrophone(makeConvo());
|
||||
helpers.push(mic);
|
||||
const pending = mic.start();
|
||||
mic.stop();
|
||||
@@ -154,7 +154,7 @@ it('reports browser playback failures with SDK error codes', async () => {
|
||||
}
|
||||
},
|
||||
);
|
||||
const speaker = new RealtimeSpeaker(makeConversation());
|
||||
const speaker = new ConvoSpeaker(makeConvo());
|
||||
helpers.push(speaker);
|
||||
await expect(speaker.start()).rejects.toEqual({
|
||||
code: 'media_error',
|
||||
@@ -162,14 +162,14 @@ it('reports browser playback failures with SDK error codes', async () => {
|
||||
});
|
||||
});
|
||||
/** A started microphone over fake browser media. */
|
||||
const startedMic = async (c = makeConversation()) => {
|
||||
const startedMic = async (c = makeConvo()) => {
|
||||
const audioTrack = track();
|
||||
const getUserMedia = vi.fn(async () => ({
|
||||
getTracks: () => [audioTrack],
|
||||
getAudioTracks: () => [audioTrack],
|
||||
}));
|
||||
vi.stubGlobal('navigator', { mediaDevices: { getUserMedia } });
|
||||
const mic = new RealtimeMicrophone(c);
|
||||
const mic = new ConvoMicrophone(c);
|
||||
helpers.push(mic);
|
||||
await mic.start();
|
||||
const port = FakeWorklet.instances.at(-1).port;
|
||||
@@ -269,7 +269,7 @@ it('captures at the device rate', async () => {
|
||||
return new FakeContext();
|
||||
});
|
||||
vi.stubGlobal('AudioContext', ContextSpy);
|
||||
const mic = new RealtimeMicrophone(makeConversation());
|
||||
const mic = new ConvoMicrophone(makeConvo());
|
||||
helpers.push(mic);
|
||||
await mic.start();
|
||||
expect(ContextSpy).toHaveBeenCalledWith(undefined);
|
||||
@@ -284,7 +284,7 @@ it('reports a denied permission', async () => {
|
||||
},
|
||||
},
|
||||
});
|
||||
const mic = new RealtimeMicrophone(makeConversation());
|
||||
const mic = new ConvoMicrophone(makeConvo());
|
||||
helpers.push(mic);
|
||||
await expect(mic.start()).rejects.toMatchObject({
|
||||
code: 'permission_denied',
|
||||
@@ -292,7 +292,7 @@ it('reports a denied permission', async () => {
|
||||
});
|
||||
it('rejects unsupported capture environments', async () => {
|
||||
vi.stubGlobal('navigator', {});
|
||||
const mic = new RealtimeMicrophone(makeConversation());
|
||||
const mic = new ConvoMicrophone(makeConvo());
|
||||
helpers.push(mic);
|
||||
await expect(mic.start()).rejects.toMatchObject({
|
||||
code: 'unsupported_environment',
|
||||
@@ -310,7 +310,7 @@ it('waits on the permission prompt but times out a stuck audio context', async (
|
||||
}),
|
||||
},
|
||||
});
|
||||
const mic = new RealtimeMicrophone(makeConversation());
|
||||
const mic = new ConvoMicrophone(makeConvo());
|
||||
helpers.push(mic);
|
||||
const result = expect(mic.start()).rejects.toMatchObject({
|
||||
code: 'media_timeout',
|
||||
@@ -323,7 +323,7 @@ it('waits on the permission prompt but times out a stuck audio context', async (
|
||||
expect(audioTrack.stop).toHaveBeenCalled();
|
||||
});
|
||||
it('tracks played position and clears playback immediately', async () => {
|
||||
const speaker = new RealtimeSpeaker(makeConversation());
|
||||
const speaker = new ConvoSpeaker(makeConvo());
|
||||
helpers.push(speaker);
|
||||
await speaker.start();
|
||||
speaker.enqueue(new Int16Array(2400), {
|
||||
@@ -348,8 +348,8 @@ it('tracks played position and clears playback immediately', async () => {
|
||||
expect(context.close).toHaveBeenCalled();
|
||||
});
|
||||
it('bounds scheduled playback without interrupting the reply', async () => {
|
||||
const c = makeConversation();
|
||||
const speaker = new RealtimeSpeaker(c);
|
||||
const c = makeConvo();
|
||||
const speaker = new ConvoSpeaker(c);
|
||||
helpers.push(speaker);
|
||||
await speaker.start({ bargeIn: false });
|
||||
const delta = {
|
||||
@@ -371,7 +371,7 @@ it('bounds scheduled playback without interrupting the reply', async () => {
|
||||
expect(speaker.bargeIn).toBe(false);
|
||||
});
|
||||
it('subtracts output latency and covers gaps between chunks', async () => {
|
||||
const speaker = new RealtimeSpeaker(makeConversation());
|
||||
const speaker = new ConvoSpeaker(makeConvo());
|
||||
helpers.push(speaker);
|
||||
await speaker.start();
|
||||
const context = FakeContext.instances[0];
|
||||
@@ -396,8 +396,8 @@ it('subtracts output latency and covers gaps between chunks', async () => {
|
||||
});
|
||||
|
||||
it('reports playing until the last scheduled chunk ends', async () => {
|
||||
const c = makeConversation();
|
||||
const speaker = new RealtimeSpeaker(c);
|
||||
const c = makeConvo();
|
||||
const speaker = new ConvoSpeaker(c);
|
||||
helpers.push(speaker);
|
||||
await speaker.start();
|
||||
const context = FakeContext.instances[0];
|
||||
@@ -429,7 +429,7 @@ it('reports playing until the last scheduled chunk ends', async () => {
|
||||
});
|
||||
|
||||
it('lists every item not fully heard, in play order', async () => {
|
||||
const speaker = new RealtimeSpeaker(makeConversation());
|
||||
const speaker = new ConvoSpeaker(makeConvo());
|
||||
helpers.push(speaker);
|
||||
await speaker.start();
|
||||
const context = FakeContext.instances[0];
|
||||
@@ -469,7 +469,7 @@ it('a late canceled permission request does not stop a newer capture', async ()
|
||||
: Promise.resolve({ getTracks: () => [newTrack] }),
|
||||
},
|
||||
});
|
||||
const mic = new RealtimeMicrophone(makeConversation());
|
||||
const mic = new ConvoMicrophone(makeConvo());
|
||||
helpers.push(mic);
|
||||
const oldStart = mic.start();
|
||||
mic.stop();
|
||||
File renamed without changes.
+1
-1
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { RealtimeSession } from './realtimeTransport.js';
|
||||
import { RealtimeSession } from './convoTransport.js';
|
||||
|
||||
/** Just enough of a WebSocket to drive the session's listeners. */
|
||||
const fakeSocket = () => {
|
||||
@@ -1,5 +1,5 @@
|
||||
import { PuterModule } from '../../lib/PuterModule.js';
|
||||
import { realtime } from './realtime.js';
|
||||
import { convo } from './convo.js';
|
||||
import { chat } from './chat.js';
|
||||
import { txt2img } from './image.js';
|
||||
import { listModelProviders, listModels } from './models.js';
|
||||
@@ -49,7 +49,7 @@ export class AIModule extends PuterModule {
|
||||
// runtime so destructured calls (`const { chat } = puter.ai`) keep
|
||||
// working like the old arrow fields did.
|
||||
chat = chat;
|
||||
realtime = realtime;
|
||||
convo = convo;
|
||||
img2txt = img2txt;
|
||||
speech2txt = speech2txt;
|
||||
speech2speech = speech2speech;
|
||||
@@ -66,7 +66,7 @@ export class AIModule extends PuterModule {
|
||||
/** @type {unknown} */ (this)
|
||||
);
|
||||
for ( const name of [
|
||||
'chat', 'realtime', 'img2txt', 'speech2txt', 'speech2speech',
|
||||
'chat', 'convo', 'img2txt', 'speech2txt', 'speech2speech',
|
||||
'txt2img', 'txt2vid', 'listModels', 'listModelProviders',
|
||||
] ) {
|
||||
methods[name] = methods[name].bind(this);
|
||||
|
||||
@@ -602,7 +602,7 @@ export {};
|
||||
/**
|
||||
* Session settings for a live conversation.
|
||||
*
|
||||
* @typedef {Object} RealtimeSessionOptions
|
||||
* @typedef {Object} ConvoSessionOptions
|
||||
* @property {string} [instructions] Instructions, up to 8192 characters.
|
||||
* @property {('text' | 'audio')[]} [output] Defaults to audio. Audio includes transcript events.
|
||||
* @property {string} [voice] OpenAI voice identifier, up to 128 characters.
|
||||
@@ -611,12 +611,12 @@ export {};
|
||||
/**
|
||||
* Options for preparing a conversation.
|
||||
*
|
||||
* @typedef {RealtimeSessionOptions & { model?: string }} RealtimeOptions
|
||||
* @typedef {ConvoSessionOptions & { model?: string }} ConvoOptions
|
||||
*/
|
||||
/**
|
||||
* How much of an assistant audio item a custom player has played.
|
||||
*
|
||||
* @typedef {Object} RealtimePlaybackPosition
|
||||
* @typedef {Object} ConvoPlaybackPosition
|
||||
* @property {string} itemId Assistant message item ID.
|
||||
* @property {number} contentIndex Audio content index.
|
||||
* @property {number} playedMs Played duration in milliseconds.
|
||||
@@ -624,24 +624,24 @@ export {};
|
||||
/**
|
||||
* Built-in speaker options.
|
||||
*
|
||||
* @typedef {Object} RealtimeSpeakerOptions
|
||||
* @typedef {Object} ConvoSpeakerOptions
|
||||
* @property {boolean} [bargeIn] Stop replies when user speech is detected. Defaults to true.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {{ type: 'text'; delta: string; responseId: string }} RealtimeTextEvent */
|
||||
* @typedef {{ type: 'text'; delta: string; responseId: string }} ConvoTextEvent */
|
||||
/**
|
||||
* @typedef {{ type: 'audio'; pcm: Int16Array; sampleRate: number; responseId: string; itemId: string; contentIndex: number }} RealtimeAudioEvent */
|
||||
* @typedef {{ type: 'audio'; pcm: Int16Array; sampleRate: number; responseId: string; itemId: string; contentIndex: number }} ConvoAudioEvent */
|
||||
/**
|
||||
* @typedef {{ type: 'transcript'; role: 'user' | 'assistant'; text: string; final: boolean }} RealtimeTranscriptEvent */
|
||||
* @typedef {{ type: 'transcript'; role: 'user' | 'assistant'; text: string; final: boolean }} ConvoTranscriptEvent */
|
||||
/**
|
||||
* @typedef {{ type: 'interrupted'; source: 'manual' | 'speech'; responseId: string }} RealtimeInterruptedEvent */
|
||||
* @typedef {{ type: 'interrupted'; source: 'manual' | 'speech'; responseId: string }} ConvoInterruptedEvent */
|
||||
/**
|
||||
* A finished reply. `usage` and `status` are OpenAI's, unchanged.
|
||||
* @typedef {{ type: 'turn.done'; responseId: string | undefined; usage: unknown; status: unknown }} RealtimeTurnDoneEvent */
|
||||
* @typedef {{ type: 'turn.done'; responseId: string | undefined; usage: unknown; status: unknown }} ConvoTurnDoneEvent */
|
||||
/**
|
||||
* An OpenAI `error` event, or an SDK media failure, which also carries `code` and `message` at the
|
||||
* top level.
|
||||
* @typedef {{ type: 'error'; error: { code?: string; message?: string } & Record<string, unknown>; code?: string; message?: string }} RealtimeErrorEvent */
|
||||
* @typedef {{ type: 'error'; error: { code?: string; message?: string } & Record<string, unknown>; code?: string; message?: string }} ConvoErrorEvent */
|
||||
/** Native provider events retain their provider-owned fields.
|
||||
* @typedef {Record<string, RealtimeServerEvent> & { text: RealtimeTextEvent; audio: RealtimeAudioEvent; transcript: RealtimeTranscriptEvent; interrupted: RealtimeInterruptedEvent; 'turn.done': RealtimeTurnDoneEvent; error: RealtimeErrorEvent; ready: { type: 'ready'; model: string; provider: 'openai'; reconnected?: boolean }; reconnecting: { type: 'reconnecting'; code: number; reason: string }; context_trimmed: { type: 'context_trimmed'; omittedItems: number }; close: { type: 'close'; code: number; reason: string } }} RealtimeEventMap */
|
||||
* @typedef {Record<string, RealtimeServerEvent> & { text: ConvoTextEvent; audio: ConvoAudioEvent; transcript: ConvoTranscriptEvent; interrupted: ConvoInterruptedEvent; 'turn.done': ConvoTurnDoneEvent; error: ConvoErrorEvent; ready: { type: 'ready'; model: string; provider: 'openai'; reconnected?: boolean }; reconnecting: { type: 'reconnecting'; code: number; reason: string }; context_trimmed: { type: 'context_trimmed'; omittedItems: number }; close: { type: 'close'; code: number; reason: string } }} ConvoEventMap */
|
||||
@@ -340,13 +340,8 @@ el.open();
|
||||
The account button and Puter's app browser for a first-party app on a Puter subdomain (`notes.puter.com`). It is internal and left out of the public docs, because the app browser only works one subdomain under the Puter origin. It follows the page's own puter.js sign-in, never the Puter session's.
|
||||
|
||||
- Signed out, it shows **Sign in**, which calls `puter.auth.signIn()`. A temporary account counts as signed out.
|
||||
- Signed in, it shows the user's profile picture, or the username's initial when there is none. Clicking it opens a menu with:
|
||||
- The user's picture, username and email. The email line is hidden when whoami leaves it out, such as for an app that can't read it.
|
||||
- **Manage account**, **Usage** and **Open Puter**, links that open the GUI origin's `/dashboard/#account`, `/dashboard/#usage` and `/` in a new tab.
|
||||
- **Send feedback**, which calls `puter.ui.showFeedbackDialog()`.
|
||||
- **Sign out**, which calls `puter.auth.signOut()`.
|
||||
- The menu opens with focus on its first item. ArrowUp and ArrowDown move between items and wrap around, and Home and End jump to the first and last.
|
||||
- On a page exactly one label under the Puter origin, with the same scheme and port, other than Puter's own subdomains such as `api` and `docs`, an **Apps** button opens the app browser (`/embed/apps`) in a panel under the button. It names the signed-in user as `#user=<uuid>`, so the app browser shows the Puter session's apps only to that user (see `doc/embedding-app-browser.md`), and passes a forced `theme` on as `theme=`. Elsewhere the button is hidden, because the server refuses the framing.
|
||||
- Signed in, it shows the user's profile picture, or the username's initial when there is none. Clicking it opens a menu with the username and **Sign out**, which calls `puter.auth.signOut()`.
|
||||
- On a page exactly one label under the Puter origin, with the same scheme and port, an **Apps** button opens the app browser (`/embed/apps`) in a panel under the button. It names the signed-in user as `#user=<uuid>`, so the app browser shows the Puter session's apps only to that user (see `doc/embedding-app-browser.md`), and passes a forced `theme` on as `theme=`. Elsewhere the button is hidden, because the server refuses the framing.
|
||||
- The menu and the Apps panel hang under their button with a caret pointing at it, and close on Escape, a press outside them, or their button. They're in the browser's top layer where it has one, so a host's `transform`, `overflow` or `z-index` can't clip or cover them. On screens up to 600px wide, the Apps panel spans the width below the button.
|
||||
- Closing the Apps panel keeps the app browser, so reopening it keeps its scroll and search. Any change to the page's sign-in discards it, so it never stays loaded for the previous user.
|
||||
- It renders nothing outside a top-level page (`puter.env !== 'web'`), such as inside the Puter desktop.
|
||||
@@ -360,10 +355,6 @@ The account button and Puter's app browser for a first-party app on a Puter subd
|
||||
| `sign-out-label` | `Sign out` | Sign-out menu item text |
|
||||
| `close-label` | `Close` | The Apps panel's close button label |
|
||||
| `account-label` | `Account` | Account button label, read as `Account: <username>` |
|
||||
| `manage-account-label` | `Manage account` | Menu link to the account settings |
|
||||
| `usage-label` | `Usage` | Menu link to the usage page |
|
||||
| `open-puter-label` | `Open Puter` | Menu link to Puter |
|
||||
| `feedback-label` | `Send feedback` | Menu item that opens the feedback dialog |
|
||||
| `theme` | follows the OS | `light` or `dark`, for the component and the app browser in it |
|
||||
|
||||
Changing an attribute updates the component in place.
|
||||
|
||||
@@ -4,8 +4,7 @@
|
||||
*
|
||||
* Signed out (a temporary account counts as signed out), it shows a Sign in
|
||||
* button. Signed in, it shows the user's profile picture (or initial), which
|
||||
* opens a menu with the account, links to manage it, see its usage and open
|
||||
* Puter, Send feedback, and Sign out. On a page one subdomain under
|
||||
* opens a menu with the username and Sign out. On a page one subdomain under
|
||||
* the Puter origin, an Apps button opens Puter's app browser in a panel under
|
||||
* it, for the same user: signed out, or signed in as an account other than
|
||||
* the Puter session's, the browser shows only recommended apps. The browser
|
||||
@@ -13,20 +12,17 @@
|
||||
* Renders nothing inside the Puter desktop.
|
||||
*
|
||||
* Attributes: theme, apps-label, sign-in-label, sign-out-label, close-label,
|
||||
* account-label, manage-account-label, usage-label, open-puter-label,
|
||||
* feedback-label
|
||||
* account-label
|
||||
* Parts: apps-button, avatar, sign-in, menu, apps-panel
|
||||
* Events: signin (detail.user), signout
|
||||
*/
|
||||
|
||||
import PuterWebComponent from '../PuterWebComponent.js';
|
||||
import { defaultFontFamily } from '../PuterDefaultStyles.js';
|
||||
import { canFrameGuiEmbed } from '../../lib/guiEmbed.js';
|
||||
|
||||
const APPS_ICON = `<svg viewBox="0 0 24 24" width="20" height="20" aria-hidden="true" focusable="false">${
|
||||
[5, 12, 19].flatMap(y => [5, 12, 19].map(x => `<circle cx="${x}" cy="${y}" r="2"/>`)).join('')
|
||||
}</svg>`;
|
||||
const EXTERNAL_ICON = '<svg class="menu-external" viewBox="0 0 32 32" width="11" height="11" aria-hidden="true" focusable="false"><path d="m26 28h-20a2 2 0 0 1 -2-2v-20a2 2 0 0 1 2-2h10v2h-10v20h20v-10h2v10a2 2 0 0 1 -2 2z"/><path d="m20 2v2h6.586l-8.586 8.586 1.414 1.414 8.586-8.586v6.586h2v-10z"/></svg>';
|
||||
const CLOSE_ICON = '<svg viewBox="0 0 24 24" width="16" height="16" aria-hidden="true" focusable="false" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><path d="M6 6l12 12M18 6L6 18"/></svg>';
|
||||
|
||||
const LABELS = {
|
||||
@@ -35,23 +31,36 @@ const LABELS = {
|
||||
'sign-out-label': 'Sign out',
|
||||
'close-label': 'Close',
|
||||
'account-label': 'Account',
|
||||
'manage-account-label': 'Manage account',
|
||||
'usage-label': 'Usage',
|
||||
'open-puter-label': 'Open Puter',
|
||||
'feedback-label': 'Send feedback',
|
||||
};
|
||||
|
||||
// Menu links, each opening a page of the Puter GUI in a new tab.
|
||||
const MENU_LINKS = [
|
||||
{ className: 'manage-account', label: 'manage-account-label', path: '/dashboard/#account' },
|
||||
{ className: 'usage', label: 'usage-label', path: '/dashboard/#usage' },
|
||||
{ className: 'open-puter', label: 'open-puter-label', path: '/' },
|
||||
];
|
||||
|
||||
// Gap between a panel and the viewport edge, and between its caret and corners.
|
||||
const EDGE = 8;
|
||||
const CARET_INSET = 20;
|
||||
|
||||
/**
|
||||
* Whether a page at `pageUrl` may frame the app browser served by
|
||||
* `guiOrigin`: exactly one label under its host, on the same scheme and port.
|
||||
*
|
||||
* @param {string | undefined} pageUrl
|
||||
* @param {string} guiOrigin
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export const canFrameAppBrowser = (pageUrl, guiOrigin) => {
|
||||
let page;
|
||||
let gui;
|
||||
try {
|
||||
page = new URL(pageUrl);
|
||||
gui = new URL(guiOrigin);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
const host = page.hostname.replace(/\.$/, '').toLowerCase();
|
||||
const suffix = `.${gui.hostname.toLowerCase()}`;
|
||||
if ( page.protocol !== gui.protocol || page.port !== gui.port || !host.endsWith(suffix) ) return false;
|
||||
const label = host.slice(0, -suffix.length);
|
||||
return label.length > 0 && !label.includes('.');
|
||||
};
|
||||
|
||||
class PuterUserMenu extends PuterWebComponent {
|
||||
static get observedAttributes () {
|
||||
return Object.keys(LABELS);
|
||||
@@ -124,7 +133,7 @@ class PuterUserMenu extends PuterWebComponent {
|
||||
}
|
||||
|
||||
_appsAllowed () {
|
||||
return canFrameGuiEmbed(globalThis.location?.href, globalThis.puter.defaultGUIOrigin);
|
||||
return canFrameAppBrowser(globalThis.location?.href, globalThis.puter.defaultGUIOrigin);
|
||||
}
|
||||
|
||||
/** A temporary account has nothing to sign out of, so it gets Sign in. */
|
||||
@@ -230,8 +239,7 @@ class PuterUserMenu extends PuterWebComponent {
|
||||
text-align: center;
|
||||
}
|
||||
.avatar[aria-expanded="true"] { box-shadow: 0 0 0 3px rgba(8, 142, 240, .25); }
|
||||
.avatar img,
|
||||
.menu-avatar img {
|
||||
.avatar img {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
width: 100%;
|
||||
@@ -300,78 +308,31 @@ class PuterUserMenu extends PuterWebComponent {
|
||||
}
|
||||
|
||||
.menu {
|
||||
min-width: 220px;
|
||||
min-width: 200px;
|
||||
max-width: min(300px, calc(100vw - ${EDGE * 2}px));
|
||||
padding: 4px 0;
|
||||
text-align: left;
|
||||
}
|
||||
.menu-user {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
padding: 8px 14px 10px;
|
||||
margin-bottom: 4px;
|
||||
border-bottom: 1px solid #eee;
|
||||
}
|
||||
.menu-avatar {
|
||||
position: relative;
|
||||
flex: none;
|
||||
width: 40px;
|
||||
height: 40px;
|
||||
overflow: hidden;
|
||||
border-radius: 50%;
|
||||
background: linear-gradient(#34a5f8, #088ef0);
|
||||
color: #fff;
|
||||
font-size: 18px;
|
||||
color: #414650;
|
||||
font-size: 14px;
|
||||
font-weight: 600;
|
||||
line-height: 40px;
|
||||
text-align: center;
|
||||
}
|
||||
.menu-identity {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
min-width: 0;
|
||||
}
|
||||
.menu-username,
|
||||
.menu-email {
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
.menu-username {
|
||||
color: #414650;
|
||||
font-size: 14px;
|
||||
font-weight: 600;
|
||||
}
|
||||
.menu-email {
|
||||
color: #6b7280;
|
||||
font-size: 12px;
|
||||
}
|
||||
.menu-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
box-sizing: border-box;
|
||||
display: block;
|
||||
width: 100%;
|
||||
padding: 8px 14px;
|
||||
border: none;
|
||||
background: none;
|
||||
color: #333;
|
||||
font-size: 14px;
|
||||
line-height: normal;
|
||||
text-align: left;
|
||||
text-decoration: none;
|
||||
cursor: pointer;
|
||||
}
|
||||
.menu-external {
|
||||
flex: none;
|
||||
fill: currentColor;
|
||||
opacity: .7;
|
||||
}
|
||||
.menu-separator {
|
||||
height: 1px;
|
||||
margin: 4px 0;
|
||||
background-color: #eee;
|
||||
}
|
||||
.menu-item:focus-visible {
|
||||
outline: none;
|
||||
@@ -473,9 +434,7 @@ class PuterUserMenu extends PuterWebComponent {
|
||||
}
|
||||
:host(.puter-theme-dark) .menu-user,
|
||||
:host(.puter-theme-dark) .apps-header { border-color: #3a3a3a; }
|
||||
:host(.puter-theme-dark) .menu-separator { background-color: #3a3a3a; }
|
||||
:host(.puter-theme-dark) .menu-email { color: #a0a0a0; }
|
||||
:host(.puter-theme-dark) .menu-username,
|
||||
:host(.puter-theme-dark) .menu-user,
|
||||
:host(.puter-theme-dark) .apps-title,
|
||||
:host(.puter-theme-dark) .menu-item { color: #e6e6e6; }
|
||||
:host(.puter-theme-dark) .menu-item:focus-visible { background-color: #3a3a3a; color: #fff; }
|
||||
@@ -506,16 +465,7 @@ class PuterUserMenu extends PuterWebComponent {
|
||||
<button type="button" class="avatar" part="avatar" aria-haspopup="menu" aria-expanded="false" hidden></button>
|
||||
</div>
|
||||
<div class="panel menu" part="menu" role="menu" tabindex="-1" hidden>
|
||||
<div class="menu-user" hidden>
|
||||
<span class="menu-avatar" aria-hidden="true"></span>
|
||||
<span class="menu-identity">
|
||||
<span class="menu-username"></span>
|
||||
<span class="menu-email" hidden></span>
|
||||
</span>
|
||||
</div>
|
||||
${MENU_LINKS.map(link => `<a class="menu-item ${link.className}" role="menuitem" target="_blank" rel="noopener"><span class="menu-label"></span>${EXTERNAL_ICON}</a>`).join('')}
|
||||
<button type="button" class="menu-item feedback" role="menuitem"></button>
|
||||
<div class="menu-separator" role="separator"></div>
|
||||
<div class="menu-user" hidden></div>
|
||||
<button type="button" class="menu-item sign-out" role="menuitem"></button>
|
||||
</div>
|
||||
<div class="panel apps-panel" part="apps-panel" role="dialog" tabindex="-1" hidden>
|
||||
@@ -544,10 +494,6 @@ class PuterUserMenu extends PuterWebComponent {
|
||||
if ( this.$('.menu').hidden ) this._openMenu();
|
||||
else this._closePanels();
|
||||
});
|
||||
for ( const link of MENU_LINKS ) {
|
||||
this.$(`.${link.className}`).addEventListener('click', () => this._closePanels());
|
||||
}
|
||||
this.$('.feedback').addEventListener('click', () => this._sendFeedback());
|
||||
this.$('.sign-out').addEventListener('click', () => this._signOut());
|
||||
this.$('.apps-close').addEventListener('click', () => this._closePanels({ returnFocus: true }));
|
||||
this.shadowRoot.addEventListener('keydown', (event) => {
|
||||
@@ -559,8 +505,6 @@ class PuterUserMenu extends PuterWebComponent {
|
||||
} else if ( event.key === 'Tab' && open.classList.contains('menu') ) {
|
||||
// Tab leaves the menu: it closes, and focus moves on from its button.
|
||||
this._closePanels({ returnFocus: true });
|
||||
} else if ( open.classList.contains('menu') && this._moveMenuFocus(event.key) ) {
|
||||
event.preventDefault();
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -580,10 +524,6 @@ class PuterUserMenu extends PuterWebComponent {
|
||||
close.title = this._label('close-label');
|
||||
this.$('.sign-in').textContent = this._label('sign-in-label');
|
||||
this.$('.sign-out').textContent = this._label('sign-out-label');
|
||||
this.$('.feedback').textContent = this._label('feedback-label');
|
||||
for ( const link of MENU_LINKS ) {
|
||||
this.$(`.${link.className} .menu-label`).textContent = this._label(link.label);
|
||||
}
|
||||
this._renderAvatarLabel();
|
||||
}
|
||||
|
||||
@@ -605,17 +545,19 @@ class PuterUserMenu extends PuterWebComponent {
|
||||
|
||||
const avatar = this.$('.avatar');
|
||||
avatar.hidden = !this.#ready || !signedIn;
|
||||
this._fillAvatar(avatar, username);
|
||||
this._renderAvatarLabel();
|
||||
this.$('.menu-user').hidden = !username;
|
||||
this.$('.menu-username').textContent = username;
|
||||
this._fillAvatar(this.$('.menu-avatar'), username);
|
||||
const email = typeof this.#user?.email === 'string' ? this.#user.email : '';
|
||||
this.$('.menu-email').textContent = email;
|
||||
this.$('.menu-email').hidden = !email;
|
||||
for ( const link of MENU_LINKS ) {
|
||||
this.$(`.${link.className}`).href = new URL(link.path, globalThis.puter.defaultGUIOrigin).href;
|
||||
avatar.textContent = username ? username[0].toUpperCase() : '?';
|
||||
if ( this.#picture ) {
|
||||
const img = document.createElement('img');
|
||||
img.alt = '';
|
||||
// The initial stays underneath for a picture the page's CSP refuses.
|
||||
img.addEventListener('error', () => img.remove());
|
||||
img.src = this.#picture;
|
||||
avatar.append(img);
|
||||
}
|
||||
this._renderAvatarLabel();
|
||||
const menuUser = this.$('.menu-user');
|
||||
menuUser.textContent = username;
|
||||
menuUser.hidden = !username;
|
||||
if ( avatar.hidden && !this.$('.menu').hidden ) this._closePanels();
|
||||
if ( this.#ready && this.#focusSignIn ) {
|
||||
this.#focusSignIn = false;
|
||||
@@ -623,18 +565,6 @@ class PuterUserMenu extends PuterWebComponent {
|
||||
}
|
||||
}
|
||||
|
||||
/** The username's initial, with the profile picture over it once there is one. */
|
||||
_fillAvatar (el, username) {
|
||||
el.textContent = username ? username[0].toUpperCase() : '?';
|
||||
if ( ! this.#picture ) return;
|
||||
const img = document.createElement('img');
|
||||
img.alt = '';
|
||||
// The initial stays underneath for a picture the page's CSP refuses.
|
||||
img.addEventListener('error', () => img.remove());
|
||||
img.src = this.#picture;
|
||||
el.append(img);
|
||||
}
|
||||
|
||||
// -- Panels --
|
||||
|
||||
_openPanel () {
|
||||
@@ -700,23 +630,7 @@ class PuterUserMenu extends PuterWebComponent {
|
||||
_openMenu () {
|
||||
const menu = this.$('.menu');
|
||||
this._showPanel(menu);
|
||||
this.$('.menu-item').focus({ preventScroll: true });
|
||||
}
|
||||
|
||||
/** Moves focus between menu items for an arrow, Home or End key; false for any other key. */
|
||||
_moveMenuFocus (key) {
|
||||
const items = [...this.$$('.menu .menu-item')];
|
||||
const current = items.indexOf(this.shadowRoot.activeElement);
|
||||
const last = items.length - 1;
|
||||
const next = {
|
||||
ArrowDown: current < 0 || current === last ? 0 : current + 1,
|
||||
ArrowUp: current <= 0 ? last : current - 1,
|
||||
Home: 0,
|
||||
End: last,
|
||||
}[key];
|
||||
if ( next === undefined ) return false;
|
||||
items[next].focus({ preventScroll: true });
|
||||
return true;
|
||||
this.$('.sign-out').focus({ preventScroll: true });
|
||||
}
|
||||
|
||||
_openApps () {
|
||||
@@ -780,11 +694,6 @@ class PuterUserMenu extends PuterWebComponent {
|
||||
);
|
||||
}
|
||||
|
||||
_sendFeedback () {
|
||||
this._closePanels();
|
||||
void globalThis.puter.ui.showFeedbackDialog();
|
||||
}
|
||||
|
||||
async _signOut () {
|
||||
this.#focusSignIn = Boolean(this.shadowRoot.activeElement);
|
||||
this._closePanels();
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
// @vitest-environment jsdom
|
||||
// @vitest-environment-options {"url": "https://notes.puter.com/sheet"}
|
||||
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import PuterUserMenu from './PuterUserMenu.js';
|
||||
import PuterUserMenu, { canFrameAppBrowser } from './PuterUserMenu.js';
|
||||
|
||||
const USER = { username: 'jelveh', uuid: '0b1c9a52-3f0e-4c7a-9a65-2f6d1e8b7c40', email: '[email protected]' };
|
||||
const USER = { username: 'jelveh', uuid: '0b1c9a52-3f0e-4c7a-9a65-2f6d1e8b7c40' };
|
||||
const OTHER = { username: 'sam', uuid: '7d2e4f10-8a3b-4c5d-9e6f-0a1b2c3d4e5f' };
|
||||
const TEMP = { username: 'quiet_otter_42', uuid: '5c4b3a29-1807-4f6e-9d5c-4b3a29180716', is_temp: true };
|
||||
const PICTURE = 'data:image/png;base64,iVBORw0KGgo=';
|
||||
@@ -24,9 +24,6 @@ const fakePuter = ({ token = null, user = null, picture = null } = {}) => {
|
||||
listeners.add(listener);
|
||||
return () => listeners.delete(listener);
|
||||
},
|
||||
ui: {
|
||||
showFeedbackDialog: vi.fn(() => new Promise(() => {})),
|
||||
},
|
||||
auth: {
|
||||
getProfilePicture: vi.fn(async () => currentPicture),
|
||||
signIn: vi.fn(() => new Promise(() => {})),
|
||||
@@ -79,6 +76,24 @@ afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
describe('canFrameAppBrowser', () => {
|
||||
it.each([
|
||||
['https://notes.puter.com/a', 'https://puter.com', true],
|
||||
['https://NOTES.puter.com./a', 'https://puter.com', true],
|
||||
['http://notes.puter.localhost:4100/', 'http://puter.localhost:4100', true],
|
||||
['https://puter.com/', 'https://puter.com', false],
|
||||
['https://a.b.puter.com/', 'https://puter.com', false],
|
||||
['https://notputer.com/', 'https://puter.com', false],
|
||||
['https://notes.puter.com.evil.com/', 'https://puter.com', false],
|
||||
['http://notes.puter.com/', 'https://puter.com', false],
|
||||
['https://notes.puter.com:8443/', 'https://puter.com', false],
|
||||
['https://notes.puter.site/', 'https://puter.com', false],
|
||||
[undefined, 'https://puter.com', false],
|
||||
])('%s under %s is %s', (page, gui, expected) => {
|
||||
expect(canFrameAppBrowser(page, gui)).toBe(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe('<puter-user-menu>', () => {
|
||||
it.each(['app', 'gui'])('renders nothing in the %s environment', async (env) => {
|
||||
vi.stubGlobal('puter', { ...fakePuter({ token: 't', user: USER }), env });
|
||||
@@ -167,10 +182,7 @@ describe('<puter-user-menu>', () => {
|
||||
part(el, '.avatar').click();
|
||||
expect(shown(el, '.menu')).toBe(true);
|
||||
expect(part(el, '.avatar').getAttribute('aria-expanded')).toBe('true');
|
||||
expect(part(el, '.menu-username').textContent).toBe('jelveh');
|
||||
expect(part(el, '.menu-email').textContent).toBe(USER.email);
|
||||
expect(shown(el, '.menu-email')).toBe(true);
|
||||
expect(part(el, '.menu-avatar').textContent).toBe('J');
|
||||
expect(part(el, '.menu-user').textContent).toBe('jelveh');
|
||||
|
||||
part(el, '.apps-button').click();
|
||||
expect(shown(el, '.menu')).toBe(false);
|
||||
@@ -179,80 +191,6 @@ describe('<puter-user-menu>', () => {
|
||||
expect(frameOf(el).src).toBe(`https://puter.com/embed/apps#user=${USER.uuid}`);
|
||||
});
|
||||
|
||||
it('links to the account, its usage and Puter, each in a new tab', async () => {
|
||||
vi.stubGlobal('puter', fakePuter({ token: 'app-token', user: USER }));
|
||||
const el = await mount();
|
||||
part(el, '.avatar').click();
|
||||
expect(el.shadowRoot.activeElement).toBe(part(el, '.manage-account'));
|
||||
const links = [...el.shadowRoot.querySelectorAll('.menu a.menu-item')].map(link => ({
|
||||
label: link.textContent,
|
||||
href: link.href,
|
||||
target: link.target,
|
||||
rel: link.rel,
|
||||
}));
|
||||
expect(links).toEqual([
|
||||
{ label: 'Manage account', href: 'https://puter.com/dashboard/#account', target: '_blank', rel: 'noopener' },
|
||||
{ label: 'Usage', href: 'https://puter.com/dashboard/#usage', target: '_blank', rel: 'noopener' },
|
||||
{ label: 'Open Puter', href: 'https://puter.com/', target: '_blank', rel: 'noopener' },
|
||||
]);
|
||||
|
||||
part(el, '.usage').dispatchEvent(new MouseEvent('click', { cancelable: true }));
|
||||
expect(shown(el, '.menu')).toBe(false);
|
||||
});
|
||||
|
||||
it('hides the email line when the account has none to show', async () => {
|
||||
vi.stubGlobal('puter', fakePuter({ token: 'app-token', user: OTHER }));
|
||||
const el = await mount();
|
||||
part(el, '.avatar').click();
|
||||
expect(part(el, '.menu-username').textContent).toBe('sam');
|
||||
expect(shown(el, '.menu-email')).toBe(false);
|
||||
});
|
||||
|
||||
it('shows the profile picture in the menu header too', async () => {
|
||||
vi.stubGlobal('puter', fakePuter({ token: 'app-token', user: USER, picture: PICTURE }));
|
||||
const el = await mount();
|
||||
expect(part(el, '.menu-avatar img').getAttribute('src')).toBe(PICTURE);
|
||||
expect(part(el, '.menu-avatar').textContent).toBe('J');
|
||||
});
|
||||
|
||||
it('opens the feedback dialog from the menu', async () => {
|
||||
const sdk = fakePuter({ token: 'app-token', user: USER });
|
||||
vi.stubGlobal('puter', sdk);
|
||||
const el = await mount();
|
||||
part(el, '.avatar').click();
|
||||
part(el, '.feedback').click();
|
||||
expect(sdk.ui.showFeedbackDialog).toHaveBeenCalledTimes(1);
|
||||
expect(shown(el, '.menu')).toBe(false);
|
||||
});
|
||||
|
||||
it('moves focus through the menu with the arrow, Home and End keys', async () => {
|
||||
vi.stubGlobal('puter', fakePuter({ token: 'app-token', user: USER }));
|
||||
const el = await mount();
|
||||
part(el, '.avatar').click();
|
||||
const press = (key) => {
|
||||
const event = new KeyboardEvent('keydown', { key, bubbles: true, composed: true, cancelable: true });
|
||||
el.shadowRoot.activeElement.dispatchEvent(event);
|
||||
return event;
|
||||
};
|
||||
const focused = () => el.shadowRoot.activeElement.classList[1];
|
||||
|
||||
expect(focused()).toBe('manage-account');
|
||||
expect(press('ArrowDown').defaultPrevented).toBe(true);
|
||||
expect(focused()).toBe('usage');
|
||||
press('End');
|
||||
expect(focused()).toBe('sign-out');
|
||||
press('ArrowDown');
|
||||
expect(focused()).toBe('manage-account');
|
||||
press('ArrowUp');
|
||||
expect(focused()).toBe('sign-out');
|
||||
press('ArrowUp');
|
||||
expect(focused()).toBe('feedback');
|
||||
press('Home');
|
||||
expect(focused()).toBe('manage-account');
|
||||
expect(press('a').defaultPrevented).toBe(false);
|
||||
expect(shown(el, '.menu')).toBe(true);
|
||||
});
|
||||
|
||||
it('hangs the panel under its button with the caret pointing at it', async () => {
|
||||
vi.stubGlobal('puter', fakePuter({ token: 'app-token', user: USER }));
|
||||
const el = await mount();
|
||||
@@ -461,25 +399,12 @@ describe('<puter-user-menu>', () => {
|
||||
|
||||
it('uses label attributes, including ones changed later', async () => {
|
||||
vi.stubGlobal('puter', fakePuter({ token: 'app-token', user: USER }));
|
||||
const el = await mount({
|
||||
'apps-label': 'Applis',
|
||||
'sign-out-label': 'Déconnexion',
|
||||
'account-label': 'Compte',
|
||||
'manage-account-label': 'Gérer le compte',
|
||||
'usage-label': 'Utilisation',
|
||||
'open-puter-label': 'Ouvrir Puter',
|
||||
'feedback-label': 'Envoyer un avis',
|
||||
});
|
||||
const el = await mount({ 'apps-label': 'Applis', 'sign-out-label': 'Déconnexion', 'account-label': 'Compte' });
|
||||
expect(part(el, '.apps-button').getAttribute('aria-label')).toBe('Applis');
|
||||
expect(part(el, '.apps-title').textContent).toBe('Applis');
|
||||
expect(part(el, '.apps-panel').getAttribute('aria-label')).toBe('Applis');
|
||||
expect(part(el, '.sign-out').textContent).toBe('Déconnexion');
|
||||
expect(part(el, '.avatar').getAttribute('aria-label')).toBe('Compte: jelveh');
|
||||
expect(part(el, '.manage-account').textContent).toBe('Gérer le compte');
|
||||
expect(part(el, '.usage').textContent).toBe('Utilisation');
|
||||
expect(part(el, '.open-puter').textContent).toBe('Ouvrir Puter');
|
||||
expect(part(el, '.feedback').textContent).toBe('Envoyer un avis');
|
||||
expect(part(el, '.manage-account .menu-external')).not.toBeNull();
|
||||
|
||||
el.setAttribute('close-label', 'Fermer');
|
||||
el.setAttribute('sign-in-label', 'Connexion');
|
||||
|
||||
+16
-16
@@ -6,7 +6,7 @@ import WebSocket, { WebSocketServer } from 'ws';
|
||||
import type { AddressInfo } from 'node:net';
|
||||
import { loadNodePuter, closeNodePuter } from '../harness/nodeSdkLoader';
|
||||
import type { EnvManifest, PuterSDK } from '../harness/types';
|
||||
import type { Conversation } from '../../../types/modules/ai/realtime';
|
||||
import type { Conversation } from '../../../types/modules/ai/convo';
|
||||
import type { RealtimeServerEvent } from '../../../types/modules/ai/types';
|
||||
import {
|
||||
setupPuterTestEnv,
|
||||
@@ -14,7 +14,7 @@ import {
|
||||
} from '@heyputer/backend/testUtil.ts';
|
||||
import type { IConfig } from '@heyputer/backend/types.ts';
|
||||
|
||||
describe('realtime built SDK and browser media', () => {
|
||||
describe('convo built SDK and browser media', () => {
|
||||
let env: PuterTestEnv;
|
||||
let upstream: WebSocketServer;
|
||||
let puter: PuterSDK;
|
||||
@@ -220,7 +220,7 @@ describe('realtime built SDK and browser media', () => {
|
||||
it('resolves the original built SDK connect after an initial provider restart', async () => {
|
||||
upstreamRefusals = 1;
|
||||
const before = connections.length;
|
||||
const conversation = await puter.ai.realtime({ output: ['text'] });
|
||||
const conversation = await puter.ai.convo({ output: ['text'] });
|
||||
try {
|
||||
const ready = conversation.connect();
|
||||
expect(conversation.connect()).toBe(ready);
|
||||
@@ -234,7 +234,7 @@ describe('realtime built SDK and browser media', () => {
|
||||
});
|
||||
|
||||
it('prepares, configures, sends, interrupts and truncates using the built SDK', async () => {
|
||||
const conversation = await puter.ai.realtime({
|
||||
const conversation = await puter.ai.convo({
|
||||
output: ['text'],
|
||||
turnDetection: false,
|
||||
});
|
||||
@@ -283,13 +283,13 @@ describe('realtime built SDK and browser media', () => {
|
||||
new URL('../../../dist/puter.js', import.meta.url),
|
||||
'utf8',
|
||||
);
|
||||
await page.route('**/__realtime_test__', (route) =>
|
||||
await page.route('**/__convo_test__', (route) =>
|
||||
route.fulfill({
|
||||
contentType: 'text/html',
|
||||
body: '<!doctype html><title>Conversation media test</title>',
|
||||
}),
|
||||
);
|
||||
await page.goto(`${env.apiOrigin}/__realtime_test__`);
|
||||
await page.goto(`${env.apiOrigin}/__convo_test__`);
|
||||
await page.evaluate(
|
||||
({ origin, apiOrigin }) => {
|
||||
Object.assign(globalThis, {
|
||||
@@ -315,7 +315,7 @@ describe('realtime built SDK and browser media', () => {
|
||||
};
|
||||
let c;
|
||||
try {
|
||||
c = await sdk.ai.realtime();
|
||||
c = await sdk.ai.convo();
|
||||
} catch (error) {
|
||||
throw new Error(`prepare: ${JSON.stringify(error)}`);
|
||||
}
|
||||
@@ -432,7 +432,7 @@ describe('realtime built SDK and browser media', () => {
|
||||
{ ...manifest, apiOrigin: proxyOrigin },
|
||||
env.users.user.token,
|
||||
);
|
||||
const conversation = await dropping.ai.realtime({ output: ['text'] });
|
||||
const conversation = await dropping.ai.convo({ output: ['text'] });
|
||||
const log = watch(conversation);
|
||||
try {
|
||||
await conversation.connect();
|
||||
@@ -481,7 +481,7 @@ describe('realtime built SDK and browser media', () => {
|
||||
{ ...manifest, apiOrigin: proxyOrigin },
|
||||
env.users.user.token,
|
||||
);
|
||||
const conversation = await dropping.ai.realtime({ output: ['text'] });
|
||||
const conversation = await dropping.ai.convo({ output: ['text'] });
|
||||
const log = watch(conversation);
|
||||
const auth = vi.spyOn(env.server.services.auth, 'authenticate');
|
||||
const warn = vi.spyOn(console, 'warn');
|
||||
@@ -524,7 +524,7 @@ describe('realtime built SDK and browser media', () => {
|
||||
{ ...manifest, apiOrigin: proxyOrigin },
|
||||
env.users.user.token,
|
||||
);
|
||||
const conversation = await dropping.ai.realtime({ output: ['text'] });
|
||||
const conversation = await dropping.ai.convo({ output: ['text'] });
|
||||
const log = watch(conversation);
|
||||
try {
|
||||
await conversation.connect();
|
||||
@@ -564,7 +564,7 @@ describe('realtime built SDK and browser media', () => {
|
||||
});
|
||||
|
||||
it('moves to a new session when a restart is announced, restoring context', async () => {
|
||||
const conversation = await puter.ai.realtime({ output: ['text'] });
|
||||
const conversation = await puter.ai.convo({ output: ['text'] });
|
||||
const log = watch(conversation);
|
||||
try {
|
||||
await conversation.connect();
|
||||
@@ -609,7 +609,7 @@ describe('realtime built SDK and browser media', () => {
|
||||
});
|
||||
|
||||
it('closes with invalid_event when Puter refuses a native event', async () => {
|
||||
const conversation = await puter.ai.realtime({ output: ['text'] });
|
||||
const conversation = await puter.ai.convo({ output: ['text'] });
|
||||
const log = watch(conversation);
|
||||
try {
|
||||
await conversation.connect();
|
||||
@@ -646,13 +646,13 @@ describe('realtime built SDK and browser media', () => {
|
||||
});
|
||||
const page = await browser.newPage();
|
||||
try {
|
||||
await page.route('**/__realtime_mic_test__', (route) =>
|
||||
await page.route('**/__convo_mic_test__', (route) =>
|
||||
route.fulfill({
|
||||
contentType: 'text/html',
|
||||
body: '<!doctype html><title>Microphone reconnect test</title>',
|
||||
}),
|
||||
);
|
||||
await page.goto(`${env.apiOrigin}/__realtime_mic_test__`);
|
||||
await page.goto(`${env.apiOrigin}/__convo_mic_test__`);
|
||||
// Through the proxy, so the test can drop the connection.
|
||||
await page.evaluate(
|
||||
({ origin, apiOrigin }) => {
|
||||
@@ -695,7 +695,7 @@ describe('realtime built SDK and browser media', () => {
|
||||
tracks.push(...stream.getTracks());
|
||||
return stream;
|
||||
};
|
||||
const c = await g.puter.ai.realtime({ output: ['text'] });
|
||||
const c = await g.puter.ai.convo({ output: ['text'] });
|
||||
const events: string[] = [];
|
||||
c.on('*', (event) => events.push(event.type));
|
||||
await c.connect();
|
||||
@@ -800,7 +800,7 @@ describe('realtime built SDK and browser media', () => {
|
||||
{ ...manifest, apiOrigin: proxyOrigin },
|
||||
env.users.user.token,
|
||||
);
|
||||
const conversation = await draining.ai.realtime({ output: ['text'] });
|
||||
const conversation = await draining.ai.convo({ output: ['text'] });
|
||||
const log = watch(conversation);
|
||||
try {
|
||||
await conversation.connect();
|
||||
+7
-7
@@ -2,7 +2,7 @@ import WebSocket from 'ws';
|
||||
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
|
||||
import { closeNodePuter, loadNodePuter } from '../harness/nodeSdkLoader';
|
||||
import type { EnvManifest, PuterSDK } from '../harness/types';
|
||||
import type { Conversation } from '../../../types/modules/ai/realtime';
|
||||
import type { Conversation } from '../../../types/modules/ai/convo';
|
||||
import type { RealtimeServerEvent } from '../../../types/modules/ai/types';
|
||||
import { optionalEnv } from '@heyputer/backend/drivers/integrationTestUtil.ts';
|
||||
import {
|
||||
@@ -146,7 +146,7 @@ describe.skipIf(!apiKey)('OpenAI realtime live relay', () => {
|
||||
'batchIncrementUsages',
|
||||
);
|
||||
const startedAt = performance.now();
|
||||
const session = await puter.ai.realtime({
|
||||
const session = await puter.ai.convo({
|
||||
...(model ? { model } : {}),
|
||||
output: ['text'],
|
||||
instructions: 'Reply with exactly realtime-ok',
|
||||
@@ -188,7 +188,7 @@ describe.skipIf(!apiKey)('OpenAI realtime live relay', () => {
|
||||
.map((result) => result.value),
|
||||
);
|
||||
process.stdout.write(
|
||||
`[live realtime] model=${model ?? 'default'} ready=${Math.round(readyAt - startedAt)}ms firstText=${Math.round(firstDeltaAt! - readyAt)}ms reply=${JSON.stringify(text)} metering=recorded\n`,
|
||||
`[live convo] model=${model ?? 'default'} ready=${Math.round(readyAt - startedAt)}ms firstText=${Math.round(firstDeltaAt! - readyAt)}ms reply=${JSON.stringify(text)} metering=recorded\n`,
|
||||
);
|
||||
} finally {
|
||||
session.close();
|
||||
@@ -199,7 +199,7 @@ describe.skipIf(!apiKey)('OpenAI realtime live relay', () => {
|
||||
45_000,
|
||||
);
|
||||
it('restores tool and text context on a new provider session after a restart notice', async () => {
|
||||
const session = await puter.ai.realtime({
|
||||
const session = await puter.ai.convo({
|
||||
output: ['text'],
|
||||
instructions: 'Answer in one short sentence.',
|
||||
});
|
||||
@@ -272,7 +272,7 @@ describe.skipIf(!apiKey)('OpenAI realtime live relay', () => {
|
||||
expect(text.toLowerCase()).toContain('pineapple');
|
||||
expect(errors).toEqual([]);
|
||||
process.stdout.write(
|
||||
`[live realtime] context restored after reconnect, reply=${JSON.stringify(text)}\n`,
|
||||
`[live convo] context restored after reconnect, reply=${JSON.stringify(text)}\n`,
|
||||
);
|
||||
} finally {
|
||||
session.close();
|
||||
@@ -281,7 +281,7 @@ describe.skipIf(!apiKey)('OpenAI realtime live relay', () => {
|
||||
}, 60_000);
|
||||
|
||||
it('receives live PCM audio and interrupts the current response', async () => {
|
||||
const c = await puter.ai.realtime({
|
||||
const c = await puter.ai.convo({
|
||||
output: ['audio'],
|
||||
instructions: 'Count slowly from one to fifty.',
|
||||
});
|
||||
@@ -301,7 +301,7 @@ describe.skipIf(!apiKey)('OpenAI realtime live relay', () => {
|
||||
const response = await done;
|
||||
expect(response.response).toMatchObject({ status: 'cancelled' });
|
||||
process.stdout.write(
|
||||
'[live realtime] PCM audio received, response canceled, playback history truncated\n',
|
||||
'[live convo] PCM audio received, response canceled, playback history truncated\n',
|
||||
);
|
||||
} finally {
|
||||
c.close();
|
||||
@@ -1743,10 +1743,10 @@ export default suite('ai', {
|
||||
t.assert.equal(error.message, 'Input size cannot be larger than 25 MB');
|
||||
},
|
||||
|
||||
'realtime streams a live text turn': {
|
||||
'convo streams a live text turn': {
|
||||
requires: ['ai.openai'],
|
||||
fn: async (t) => {
|
||||
const conversation = await t.puter.ai.realtime({ output: ['text'], instructions: 'Reply with exactly realtime-ok.' });
|
||||
const conversation = await t.puter.ai.convo({ output: ['text'], instructions: 'Reply with exactly realtime-ok.' });
|
||||
let text = '';
|
||||
conversation.on('text', ({ delta }) => { text += delta; });
|
||||
let timer: ReturnType<typeof setTimeout> | undefined;
|
||||
@@ -1767,8 +1767,8 @@ export default suite('ai', {
|
||||
},
|
||||
},
|
||||
|
||||
'realtime prepares and closes without opening a provider session': async (t) => {
|
||||
const conversation = await t.puter.ai.realtime({ output: ['text'] });
|
||||
'convo prepares and closes without opening a provider session': async (t) => {
|
||||
const conversation = await t.puter.ai.convo({ output: ['text'] });
|
||||
t.assert.equal(conversation.state, 'prepared');
|
||||
t.assert.equal(conversation.model, 'gpt-realtime-2.1-mini');
|
||||
let closed = 0;
|
||||
@@ -1779,9 +1779,9 @@ export default suite('ai', {
|
||||
t.assert.equal(closed, 1);
|
||||
},
|
||||
|
||||
'realtime rejects invalid options and sending before connect': async (t) => {
|
||||
await t.assert.rejects(() => t.puter.ai.realtime({ model: 'not-a-realtime-model' }));
|
||||
const conversation = await t.puter.ai.realtime({ output: ['text'] });
|
||||
'convo rejects invalid options and sending before connect': async (t) => {
|
||||
await t.assert.rejects(() => t.puter.ai.convo({ model: 'not-a-realtime-model' }));
|
||||
const conversation = await t.puter.ai.convo({ output: ['text'] });
|
||||
try {
|
||||
await t.assert.rejects(async () => conversation.sendText('Hello'));
|
||||
} finally {
|
||||
@@ -1789,8 +1789,8 @@ export default suite('ai', {
|
||||
}
|
||||
},
|
||||
|
||||
'realtime rejects interruption readiness through its Promise': async (t) => {
|
||||
const conversation = await t.puter.ai.realtime({ output: ['text'] });
|
||||
'convo rejects interruption readiness through its Promise': async (t) => {
|
||||
const conversation = await t.puter.ai.convo({ output: ['text'] });
|
||||
try {
|
||||
const prepared = conversation.interrupt();
|
||||
t.assert.equal(typeof prepared.then, 'function');
|
||||
@@ -1804,9 +1804,9 @@ export default suite('ai', {
|
||||
}
|
||||
},
|
||||
|
||||
'realtime is the only realtime SDK entry point': async (t) => {
|
||||
'convo is the realtime SDK entry point': async (t) => {
|
||||
t.assert.equal('assistant' in t.puter.ai, false);
|
||||
t.assert.equal('conversation' in t.puter.ai, false);
|
||||
t.assert.equal(typeof t.puter.ai.realtime, 'function');
|
||||
t.assert.equal(typeof t.puter.ai.convo, 'function');
|
||||
},
|
||||
});
|
||||
@@ -1,339 +0,0 @@
|
||||
import { test, expect, devices } from '@playwright/test';
|
||||
import { readFileSync } from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
const PUTER_ORIGIN = process.env.PUTER_TEST_ORIGIN || 'http://puter.localhost:4100';
|
||||
const API_ORIGIN = PUTER_ORIGIN.replace('://', '://api.');
|
||||
// One label under the GUI origin: a first-party app page.
|
||||
const FIRST_PARTY = PUTER_ORIGIN.replace('://', '://notes.');
|
||||
const SDK = readFileSync(
|
||||
path.join(path.dirname(fileURLToPath(import.meta.url)), '../../../dist/puter.dev.js'),
|
||||
'utf8',
|
||||
);
|
||||
|
||||
// Locally both origins are loopback, which Chromium's local network checks
|
||||
// refuse to frame from a page the test serves itself.
|
||||
test.use({
|
||||
launchOptions: {
|
||||
args: ['--disable-features=LocalNetworkAccessChecks,BlockInsecurePrivateNetworkRequests,PrivateNetworkAccessForNavigations'],
|
||||
},
|
||||
});
|
||||
|
||||
/** Opens the first-party page, serving it and the SDK from the test. */
|
||||
const openFirstPartyPage = async (page) => {
|
||||
await page.context().route(`${FIRST_PARTY}/**`, (route) => {
|
||||
if ( new URL(route.request().url()).pathname === '/sdk.js' ) {
|
||||
return route.fulfill({ contentType: 'text/javascript', body: SDK });
|
||||
}
|
||||
return route.fulfill({
|
||||
contentType: 'text/html',
|
||||
body: `<!doctype html><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><button id="go">Go</button>
|
||||
<script>window.PUTER_ORIGIN = ${JSON.stringify(PUTER_ORIGIN)}; window.PUTER_API_ORIGIN = ${JSON.stringify(API_ORIGIN)};</script>
|
||||
<script src="/sdk.js"></script>`,
|
||||
});
|
||||
});
|
||||
await page.goto(`${FIRST_PARTY}/`);
|
||||
await page.waitForFunction(() => !!window.puter);
|
||||
};
|
||||
|
||||
/** Runs `expr` from a click on the page, settling into `window.__result`. */
|
||||
const runFromClick = async (page, expr) => {
|
||||
await page.evaluate((expr) => {
|
||||
window.__result = undefined;
|
||||
document.getElementById('go').onclick = () => {
|
||||
Promise.resolve().then(() => (0, eval)(expr)).then(
|
||||
(value) => {
|
||||
window.__result = { ok: true, value: JSON.parse(JSON.stringify(value ?? null)) };
|
||||
},
|
||||
(error) => {
|
||||
window.__result = { ok: false, error: JSON.parse(JSON.stringify(error ?? null)) };
|
||||
},
|
||||
);
|
||||
};
|
||||
}, expr);
|
||||
await page.click('#go');
|
||||
};
|
||||
const result = (page) => page.waitForFunction(() => window.__result, null, { timeout: 60_000 }).then((h) => h.jsonValue());
|
||||
|
||||
const frame = (page) => page.frameLocator('dialog.puter-popup-frame iframe');
|
||||
const frameReady = (page) => page.locator('dialog.puter-popup-frame[open]:not(.is-loading)').waitFor({ timeout: 60_000 });
|
||||
|
||||
/** The red channel of a screenshot of `clip`, row by row, decoded in the page. */
|
||||
const screenshotRed = async (page, clip) => {
|
||||
const png = await page.screenshot({ clip });
|
||||
return page.evaluate(async (base64) => {
|
||||
const image = await createImageBitmap(await (await fetch(`data:image/png;base64,${base64}`)).blob());
|
||||
const context = new OffscreenCanvas(image.width, image.height).getContext('2d');
|
||||
context.drawImage(image, 0, 0);
|
||||
const { data } = context.getImageData(0, 0, image.width, image.height);
|
||||
return { width: image.width, red: Array.from(data.filter((_, i) => i % 4 === 0)) };
|
||||
}, png.toString('base64'));
|
||||
};
|
||||
|
||||
/** Waits for the box's fade-in to finish, so screenshots show it as it stays. */
|
||||
const boxSettled = (page) => page.waitForFunction(() =>
|
||||
document.querySelector('.puter-popup-frame-box')?.getAnimations().length === 0);
|
||||
|
||||
test.describe('popups a first-party page shows framed', () => {
|
||||
test('signs in, then picks and saves files, without opening a window', async ({ page }) => {
|
||||
// The admin's Puter session, and a file of theirs to pick.
|
||||
await page.goto('/');
|
||||
await page.waitForFunction(() => !!window.user?.username && !!window.puter?.authToken, null, { timeout: 60_000 });
|
||||
const username = await page.evaluate(() => window.user.username);
|
||||
await page.evaluate((u) => puter.fs.write(`/${u}/Desktop/popup-frame.txt`, 'framed'), username);
|
||||
|
||||
await openFirstPartyPage(page);
|
||||
const popups = [];
|
||||
page.on('popup', (p) => popups.push(p));
|
||||
|
||||
await runFromClick(page, 'puter.auth.signIn()');
|
||||
await frameReady(page);
|
||||
await frame(page).locator('.auth-dialog [data-screen="accounts"] button').first().click();
|
||||
const signedIn = await result(page);
|
||||
expect(signedIn.ok).toBe(true);
|
||||
expect(signedIn.value).toMatchObject({ success: true, username });
|
||||
await expect(page.locator('dialog.puter-popup-frame')).toHaveCount(0);
|
||||
|
||||
await runFromClick(page, 'puter.ui.showOpenFilePicker()');
|
||||
await frameReady(page);
|
||||
await frame(page).locator('.item[data-name="popup-frame.txt"]').dblclick();
|
||||
const picked = await result(page);
|
||||
expect(picked.value).toMatchObject({ name: 'popup-frame.txt' });
|
||||
await expect(page.locator('dialog.puter-popup-frame')).toHaveCount(0);
|
||||
|
||||
// A name no earlier run left behind, so the save doesn't ask to replace it.
|
||||
const suggestedName = `popup-frame-${Date.now()}.txt`;
|
||||
await runFromClick(page, `puter.ui.showSaveFilePicker("saved in a frame", ${JSON.stringify(suggestedName)})`);
|
||||
await frameReady(page);
|
||||
await expect(frame(page).locator('.savefiledialog-filename')).toHaveValue(suggestedName);
|
||||
// As in the popup, the name has focus with all but its extension
|
||||
// selected, so typing renames it straight away.
|
||||
const savedName = `renamed-${suggestedName}`;
|
||||
await page.keyboard.type(savedName.replace(/\.txt$/, ''));
|
||||
await expect(frame(page).locator('.savefiledialog-filename')).toHaveValue(savedName);
|
||||
await frame(page).locator('.savefiledialog-save-btn').click();
|
||||
const saved = await result(page);
|
||||
expect(saved.value).toMatchObject({ name: savedName });
|
||||
|
||||
// Escape on the alert asking to replace that file goes back to the
|
||||
// picker, as its Cancel does, rather than leaving it behind its mask.
|
||||
await runFromClick(page, `puter.ui.showSaveFilePicker("again", ${JSON.stringify(savedName)}).undefinedOnCancel`);
|
||||
await frameReady(page);
|
||||
await frame(page).locator('.savefiledialog-save-btn').click();
|
||||
await expect(frame(page).locator('.window-alert')).toHaveCount(1);
|
||||
await page.keyboard.press('Escape');
|
||||
await expect(frame(page).locator('.window-alert')).toHaveCount(0);
|
||||
await expect(frame(page).locator('.window-filedialog .window-disable-mask')).toBeHidden();
|
||||
await frame(page).getByRole('button', { name: 'Cancel' }).click();
|
||||
expect(await result(page)).toEqual({ ok: true, value: null });
|
||||
|
||||
await runFromClick(page, 'puter.ui.showDirectoryPicker().undefinedOnCancel');
|
||||
await frameReady(page);
|
||||
await frame(page).getByRole('button', { name: 'Cancel' }).click();
|
||||
expect(await result(page)).toEqual({ ok: true, value: null });
|
||||
|
||||
// Escape closes the picker, and the frame goes with it.
|
||||
await runFromClick(page, 'puter.ui.showOpenFilePicker().undefinedOnCancel');
|
||||
await frameReady(page);
|
||||
await frame(page).locator('.window-filedialog').click({ position: { x: 5, y: 5 } });
|
||||
await page.keyboard.press('Escape');
|
||||
expect(await result(page)).toEqual({ ok: true, value: null });
|
||||
await expect(page.locator('dialog.puter-popup-frame')).toHaveCount(0);
|
||||
|
||||
expect(popups).toHaveLength(0);
|
||||
});
|
||||
|
||||
test('closing the framed sign-in rejects as closing its popup would', async ({ page }) => {
|
||||
await openFirstPartyPage(page);
|
||||
await page.evaluate(() => {
|
||||
document.body.style.minHeight = '4000px';
|
||||
});
|
||||
await runFromClick(page, 'puter.auth.signIn()');
|
||||
await frameReady(page);
|
||||
|
||||
// Scrolling over the scrim leaves the page behind it where it was.
|
||||
await page.mouse.move(5, 300);
|
||||
await page.mouse.wheel(0, 800);
|
||||
await page.waitForTimeout(300);
|
||||
expect(await page.evaluate(() => window.scrollY)).toBe(0);
|
||||
|
||||
await page.locator('.puter-popup-frame-close').click();
|
||||
const closed = await result(page);
|
||||
expect(closed).toMatchObject({ ok: false, error: { error: 'auth_window_closed' } });
|
||||
await expect(page.locator('dialog.puter-popup-frame')).toHaveCount(0);
|
||||
});
|
||||
|
||||
test('the page\'s own dialog, frame and button styles leave the framed sign-in as it is', async ({ page }) => {
|
||||
await openFirstPartyPage(page);
|
||||
await page.addStyleTag({
|
||||
content: `dialog { top: 50%; left: 50%; transform: translate(-50%, -50%); border-radius: 24px; }
|
||||
iframe { max-width: 420px; margin: 12px; }
|
||||
button { margin: 0 6px 8px 0; min-width: 88px; min-height: 44px; box-shadow: 0 1px 2px #000; }`,
|
||||
});
|
||||
await runFromClick(page, 'puter.auth.signIn()');
|
||||
await frameReady(page);
|
||||
|
||||
const viewport = page.viewportSize();
|
||||
const dialog = await page.locator('dialog.puter-popup-frame').boundingBox();
|
||||
expect(dialog).toEqual({ x: 0, y: 0, width: viewport.width, height: viewport.height });
|
||||
const box = await page.locator('.puter-popup-frame-box').boundingBox();
|
||||
expect(await page.locator('.puter-popup-frame-box iframe').boundingBox()).toEqual(box);
|
||||
const close = await page.locator('.puter-popup-frame-close').boundingBox();
|
||||
expect([close.width, close.height]).toEqual([32, 32]);
|
||||
expect(box.x + box.width - (close.x + close.width)).toBe(10);
|
||||
await expect(page.locator('.puter-popup-frame-close')).toHaveCSS('box-shadow', 'none');
|
||||
});
|
||||
|
||||
test.describe('in dark mode', () => {
|
||||
test.use({ colorScheme: 'dark', deviceScaleFactor: 2 });
|
||||
|
||||
test('the framed sign-in\'s rounded corners show no light fringe', async ({ page }) => {
|
||||
await openFirstPartyPage(page);
|
||||
await runFromClick(page, 'puter.auth.signIn()');
|
||||
await frameReady(page);
|
||||
await boxSettled(page);
|
||||
|
||||
// The bottom-left corner: the dialog at the top right, the scrim
|
||||
// at the bottom left, and the curve between them.
|
||||
const box = await page.locator('.puter-popup-frame-box').boundingBox();
|
||||
const { width, red } = await screenshotRed(page, { x: box.x, y: box.y + box.height - 12, width: 12, height: 12 });
|
||||
const inside = red[width - 1];
|
||||
const outside = red[red.length - width];
|
||||
expect(Math.max(...red)).toBeLessThanOrEqual(Math.max(inside, outside) + 3);
|
||||
});
|
||||
});
|
||||
|
||||
test.describe('on a phone with a fractional pixel ratio', () => {
|
||||
// A describe group can't switch browsers; the profile's is Chromium anyway.
|
||||
const { defaultBrowserType: _browser, ...pixel7 } = devices['Pixel 7'];
|
||||
test.use(pixel7);
|
||||
|
||||
test('the framed sign-in fills the screen with no line of scrim along its edge', async ({ page }) => {
|
||||
await openFirstPartyPage(page);
|
||||
await runFromClick(page, 'puter.auth.signIn()');
|
||||
await frameReady(page);
|
||||
await boxSettled(page);
|
||||
|
||||
// A column down the middle of the top: the white sign-in all the way up.
|
||||
const { width } = page.viewportSize();
|
||||
const { red } = await screenshotRed(page, { x: width / 2, y: 0, width: 1, height: 4 });
|
||||
expect(red[0]).toBe(red[red.length - 1]);
|
||||
});
|
||||
|
||||
test('the framed save picker keeps its file name and buttons on screen', async ({ page }) => {
|
||||
await page.goto('/');
|
||||
await page.waitForFunction(() => !!window.puter?.authToken, null, { timeout: 60_000 });
|
||||
await openFirstPartyPage(page);
|
||||
await runFromClick(page, 'puter.auth.signIn()');
|
||||
await frameReady(page);
|
||||
await frame(page).locator('.auth-dialog [data-screen="accounts"] button').first().click();
|
||||
expect((await result(page)).ok).toBe(true);
|
||||
|
||||
await runFromClick(page, 'puter.ui.showSaveFilePicker("x", "a-long-suggested-file-name.txt")');
|
||||
await frameReady(page);
|
||||
const { width } = page.viewportSize();
|
||||
for ( const control of ['.savefiledialog-filename', '.filedialog-cancel-btn', '.savefiledialog-save-btn'] ) {
|
||||
const box = await frame(page).locator(control).boundingBox();
|
||||
expect(box.x).toBeGreaterThanOrEqual(0);
|
||||
expect(box.x + box.width).toBeLessThanOrEqual(width);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
test.describe('on a small phone', () => {
|
||||
const { defaultBrowserType: _browser, ...iPhoneSE } = devices['iPhone SE'];
|
||||
test.use(iPhoneSE);
|
||||
|
||||
test('the framed open picker keeps its buttons on one line inside its bar', async ({ page }) => {
|
||||
await page.goto('/');
|
||||
await page.waitForFunction(() => !!window.puter?.authToken, null, { timeout: 60_000 });
|
||||
await openFirstPartyPage(page);
|
||||
await runFromClick(page, 'puter.auth.signIn()');
|
||||
await frameReady(page);
|
||||
await frame(page).locator('.auth-dialog [data-screen="accounts"] button').first().click();
|
||||
expect((await result(page)).ok).toBe(true);
|
||||
|
||||
await runFromClick(page, 'puter.ui.showOpenFilePicker()');
|
||||
await frameReady(page);
|
||||
const bar = await frame(page).locator('.window-filedialog-prompt').boundingBox();
|
||||
const cancel = await frame(page).locator('.filedialog-cancel-btn').boundingBox();
|
||||
const open = await frame(page).locator('.openfiledialog-open-btn').boundingBox();
|
||||
expect(open.y).toBe(cancel.y);
|
||||
expect(open.x).toBeGreaterThan(cancel.x + cancel.width);
|
||||
expect(open.y + open.height).toBeLessThan(bar.y + bar.height);
|
||||
|
||||
// With German's longer labels, the upload label gives way so both
|
||||
// buttons stay whole on screen.
|
||||
await frame(page).locator('.window-filedialog-prompt').evaluate((el) => {
|
||||
el.querySelector('.window-filedialog-upload-here').lastChild.textContent = ' Hochladen';
|
||||
el.querySelector('.filedialog-cancel-btn').textContent = 'Abbrechen';
|
||||
el.querySelector('.openfiledialog-open-btn').textContent = 'Öffnen';
|
||||
});
|
||||
const germanOpen = await frame(page).locator('.openfiledialog-open-btn').boundingBox();
|
||||
expect(germanOpen.x + germanOpen.width).toBeLessThanOrEqual(bar.x + bar.width);
|
||||
});
|
||||
});
|
||||
|
||||
test('a provider picked in the framed sign-in opens straight in the popup', async ({ page }) => {
|
||||
await page.goto('/');
|
||||
await page.waitForFunction(() => !!window.user?.username && !!window.puter?.authToken, null, { timeout: 60_000 });
|
||||
const username = await page.evaluate(() => window.user.username);
|
||||
// Offer Google, and stand in for its round trip: the provider returns
|
||||
// the popup to the sign-in popup for the same attempt.
|
||||
await page.context().route('**/auth/oidc/providers', (route) =>
|
||||
route.fulfill({ contentType: 'application/json', body: JSON.stringify({ providers: ['google'] }) }));
|
||||
let started = null;
|
||||
await page.context().route('**/auth/oidc/google/start?*', (route) => {
|
||||
started = new URL(route.request().url());
|
||||
const back = new URL('/action/sign-in', PUTER_ORIGIN);
|
||||
back.searchParams.set('embedded_in_popup', 'true');
|
||||
back.searchParams.set('msg_id', started.searchParams.get('msg_id'));
|
||||
return route.fulfill({ status: 302, headers: { location: back.href } });
|
||||
});
|
||||
|
||||
await openFirstPartyPage(page);
|
||||
const popups = [];
|
||||
page.on('popup', (p) => popups.push(p));
|
||||
await runFromClick(page, 'puter.auth.signIn()');
|
||||
await frameReady(page);
|
||||
const frameMsgId = await page.evaluate(() =>
|
||||
new URLSearchParams(new URL(document.querySelector('dialog.puter-popup-frame iframe').src).hash.slice(1)).get('msg_id'));
|
||||
await frame(page).locator('.auth-dialog [data-screen="accounts"]').waitFor();
|
||||
await frame(page).locator('.auth-dialog .auth-to-login').click();
|
||||
await frame(page).locator('.oidc-google-btn').click();
|
||||
|
||||
await expect.poll(() => popups.length).toBe(1);
|
||||
await expect(page.locator('dialog.puter-popup-frame')).toHaveCount(0);
|
||||
expect(started.origin + started.pathname).toBe(`${PUTER_ORIGIN}/auth/oidc/google/start`);
|
||||
expect(started.searchParams.get('msg_id')).toBe(frameMsgId);
|
||||
expect(started.searchParams.get('embedded_in_popup')).toBe('true');
|
||||
expect(started.searchParams.get('opener_origin')).toBe(FIRST_PARTY);
|
||||
|
||||
const popup = popups[0];
|
||||
await popup.locator('.auth-dialog [data-screen="accounts"] button').first().click();
|
||||
const signedIn = await result(page);
|
||||
expect(signedIn).toMatchObject({ ok: true, value: { success: true, username } });
|
||||
expect(popups).toHaveLength(1);
|
||||
});
|
||||
|
||||
test('a page signed in as someone else gets the picker popup', async ({ page }) => {
|
||||
await page.goto('/');
|
||||
await page.waitForFunction(() => !!window.puter?.authToken, null, { timeout: 60_000 });
|
||||
await openFirstPartyPage(page);
|
||||
await runFromClick(page, 'puter.auth.signIn()');
|
||||
await frameReady(page);
|
||||
await frame(page).locator('.auth-dialog [data-screen="accounts"] button').first().click();
|
||||
await result(page);
|
||||
// Name another user as the page's own, once sign-in's own whoami has landed.
|
||||
await page.evaluate(async () => {
|
||||
await puter.whoamiCache_;
|
||||
puter.whoami = { uuid: '00000000-0000-4000-8000-000000000000' };
|
||||
});
|
||||
|
||||
const popup = page.waitForEvent('popup', { timeout: 60_000 });
|
||||
await runFromClick(page, 'puter.ui.showOpenFilePicker()');
|
||||
expect(new URL((await popup).url()).pathname).toBe('/action/show-open-file-picker');
|
||||
await expect(page.locator('dialog.puter-popup-frame')).toHaveCount(0);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user