create-freeholder 0.2.0-beta.5 → 0.2.0-beta.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/template/.changeset/blog-playground-content.md +4 -0
- package/dist/template/.changeset/blog-wordpress-structure.md +6 -0
- package/dist/template/.changeset/managed-preview-assistant.md +4 -0
- package/dist/template/.changeset/public-beta-6.md +4 -0
- package/dist/template/.github/ISSUE_TEMPLATE/bug_report.yml +1 -1
- package/dist/template/MASTER.md +81 -8
- package/dist/template/README.md +1 -1
- package/dist/template/app/(admin)/admin/assistant/page.tsx +9 -1
- package/dist/template/app/(admin)/admin/blog/PostSettings.tsx +26 -0
- package/dist/template/app/(admin)/admin/blog/[kind]/page.tsx +53 -0
- package/dist/template/app/(admin)/admin/blog/actions.ts +186 -6
- package/dist/template/app/(admin)/admin/blog/comments/page.tsx +25 -0
- package/dist/template/app/(admin)/admin/blog/page.tsx +21 -5
- package/dist/template/app/(admin)/admin/imports/[id]/page.tsx +2 -0
- package/dist/template/app/(admin)/admin/pages/[id]/page.tsx +3 -0
- package/dist/template/app/(admin)/assistant-actions.ts +13 -6
- package/dist/template/app/(public)/[[...slug]]/ReaderComments.tsx +25 -0
- package/dist/template/app/(public)/[[...slug]]/blog-parts.tsx +153 -0
- package/dist/template/app/(public)/[[...slug]]/comment-actions.ts +16 -0
- package/dist/template/app/(public)/[[...slug]]/page.tsx +182 -18
- package/dist/template/app/api/chat/assistant/call/route.ts +35 -0
- package/dist/template/db/migrations/0061_blog_structure.sql +89 -0
- package/dist/template/db/migrations/0062_blog_reader_comments.sql +24 -0
- package/dist/template/db/migrations/meta/_journal.json +14 -0
- package/dist/template/deploy/blog-wordpress-2026-11-07.md +146 -0
- package/dist/template/deploy/docker-selfhost/.env.example +1 -1
- package/dist/template/deploy/docker-selfhost/README.md +3 -2
- package/dist/template/deploy/docker-selfhost/infra/compose.https.yml +1 -1
- package/dist/template/deploy/docker-selfhost/infra/compose.yml +1 -1
- package/dist/template/deploy/f-criteria-matrix.md +74 -0
- package/dist/template/deploy/linux/README.md +1 -1
- package/dist/template/deploy/linux/install.sh +1 -1
- package/dist/template/deploy/managed-preview-assistant.md +99 -0
- package/dist/template/eslint.config.mjs +3 -1
- package/dist/template/locales/ar.json +85 -6
- package/dist/template/locales/en.json +83 -4
- package/dist/template/locales/es.json +83 -4
- package/dist/template/locales/fr.json +85 -6
- package/dist/template/next.config.ts +2 -0
- package/dist/template/package.json +1 -1
- package/dist/template/packages/cli/package.json +1 -1
- package/dist/template/packages/create-freeholder/package.json +1 -1
- package/dist/template/packages/freeholder-app/package.json +1 -1
- package/dist/template/packages/mobile-app/package.json +1 -1
- package/dist/template/packages/plugin-kit/package.json +1 -1
- package/dist/template/packages/sdk/package.json +1 -1
- package/dist/template/packages/sdk/src/generated.ts +177 -9
- package/dist/template/packages/sdk/src/version.ts +1 -1
- package/dist/template/packages/templates/package.json +1 -1
- package/dist/template/proxy.ts +18 -1
- package/dist/template/public/managed-assistant/0.1.10/duplex-codec/NOTICE.txt +117 -0
- package/dist/template/public/managed-assistant/0.1.10/duplex-codec/audio-output-processor.js +267 -0
- package/dist/template/public/managed-assistant/0.1.10/duplex-codec/decoderWorker.min.js +847 -0
- package/dist/template/public/managed-assistant/0.1.10/duplex-codec/decoderWorker.min.wasm +0 -0
- package/dist/template/public/managed-assistant/0.1.10/duplex-codec/encoderWorker.min.js +1 -0
- package/dist/template/public/managed-assistant/0.1.10/manifest.json +12 -0
- package/dist/template/public/managed-assistant/0.1.10/pm-ai-duplex-client.js +1376 -0
- package/dist/template/scripts/license-headers.mjs +3 -0
- package/dist/template/scripts/verify-wordpress-migration.d.mts +8 -0
- package/dist/template/scripts/verify-wordpress-migration.mjs +105 -0
- package/dist/template/security/adversarial-audit-2026-10-08.md +1 -1
- package/dist/template/src/adapters/ai/types.ts +5 -0
- package/dist/template/src/adapters/paradise/assistant.ts +37 -0
- package/dist/template/src/adapters/paradise/http.ts +4 -3
- package/dist/template/src/core/contract/json-schema.ts +8 -0
- package/dist/template/src/core/demo/playground-inserts.ts +29 -0
- package/dist/template/src/core/demo/playground-policy.ts +9 -0
- package/dist/template/src/core/env.ts +2 -0
- package/dist/template/src/core/http/csp.ts +10 -2
- package/dist/template/src/core/http/csrf-client.ts +10 -0
- package/dist/template/src/core/import/service.ts +217 -12
- package/dist/template/src/core/import/wordpress.ts +80 -15
- package/dist/template/src/core/notifications/service.ts +1 -0
- package/dist/template/src/core/retention/registry.ts +2 -0
- package/dist/template/src/core/search/registry.ts +2 -0
- package/dist/template/src/core/seo/feeds.ts +2 -0
- package/dist/template/src/core/seo/jsonld.ts +46 -2
- package/dist/template/src/core/seo/legacy-proxy.ts +53 -0
- package/dist/template/src/core/seo/meta.ts +10 -7
- package/dist/template/src/core/seo/service.ts +14 -1
- package/dist/template/src/core/update/release-declaration.json +1 -1
- package/dist/template/src/modules/assistant/contract.ts +1 -1
- package/dist/template/src/modules/assistant/limits.ts +3 -0
- package/dist/template/src/modules/assistant/preview.ts +124 -0
- package/dist/template/src/modules/assistant/provider.ts +7 -0
- package/dist/template/src/modules/assistant/service.ts +21 -5
- package/dist/template/src/modules/cms/blocks/PreviewAssistantCall.tsx +176 -0
- package/dist/template/src/modules/cms/blocks/SiteChatClient.tsx +10 -4
- package/dist/template/src/modules/cms/blocks/surfaces.tsx +10 -0
- package/dist/template/src/modules/cms/blog-comments-schema.ts +27 -0
- package/dist/template/src/modules/cms/blog-comments.ts +111 -0
- package/dist/template/src/modules/cms/blog-schema.ts +146 -0
- package/dist/template/src/modules/cms/blog-service.ts +999 -0
- package/dist/template/src/modules/cms/blog.tsx +245 -25
- package/dist/template/src/modules/cms/manifest.ts +7 -1
- package/dist/template/src/modules/cms/read.ts +18 -0
- package/dist/template/src/modules/cms/service.ts +22 -4
- package/dist/template/src/modules/cms/tables.ts +2 -0
- package/dist/template/src/modules/seed/launch.ts +31 -3
- package/dist/template/tests/browser/blog-structure.spec.ts +69 -0
- package/dist/template/tests/core/analytics-consent.test.ts +1 -1
- package/dist/template/tests/core/blog-playground.test.ts +106 -0
- package/dist/template/tests/core/blog-structure-comments.test.ts +166 -0
- package/dist/template/tests/core/csp.test.ts +1 -1
- package/dist/template/tests/core/internal-services.test.ts +4 -0
- package/dist/template/tests/core/legacy-redirect-proxy.test.ts +59 -0
- package/dist/template/tests/core/locale-routing.test.ts +1 -1
- package/dist/template/tests/core/playground-cache.test.ts +1 -1
- package/dist/template/tests/core/playground.test.ts +3 -3
- package/dist/template/tests/core/preview-assistant-call.test.ts +88 -0
- package/dist/template/tests/core/private-preview-webhooks.test.ts +1 -1
- package/dist/template/tests/core/private-preview.test.ts +1 -1
- package/dist/template/tests/core/proxy-locale.test.ts +1 -1
- package/dist/template/tests/core/sdk-schema.test.ts +20 -0
- package/dist/template/tests/core/seo.test.ts +1 -1
- package/dist/template/tests/core/site-chat-client.test.ts +50 -0
- package/dist/template/tests/core/wordpress-import-flow.test.ts +81 -18
- package/dist/template/tests/core/wordpress-import.test.ts +25 -12
- package/dist/template/tests/core/wordpress-migration-verification.test.ts +37 -0
- package/dist/template/tests/modules/preview-assistant.test.ts +164 -0
- package/dist/template-manifest.json +349 -139
- package/package.json +1 -1
package/dist/template/proxy.ts
CHANGED
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
// prefix before the route sees it is exactly this layer's job.
|
|
19
19
|
import { NextResponse, type NextRequest } from "next/server";
|
|
20
20
|
import instanceConfig from "./freeholder.config";
|
|
21
|
+
import { legacyRedirect } from "@/core/seo/legacy-proxy";
|
|
21
22
|
import {
|
|
22
23
|
LOCALE_HEADER,
|
|
23
24
|
PATH_HEADER,
|
|
@@ -132,7 +133,22 @@ function sfuOrigins(url: string | undefined): string[] {
|
|
|
132
133
|
}
|
|
133
134
|
}
|
|
134
135
|
|
|
135
|
-
export function proxy(request: NextRequest): NextResponse {
|
|
136
|
+
export async function proxy(request: NextRequest): Promise<NextResponse> {
|
|
137
|
+
const context = requestContextProxy(request);
|
|
138
|
+
// The preview access decision must run before a public route is examined.
|
|
139
|
+
if (context.status >= 300 || context.headers.has("X-Robots-Tag")) return context;
|
|
140
|
+
const moved = await legacyRedirect(request);
|
|
141
|
+
if (!moved) return context;
|
|
142
|
+
const response = new NextResponse(null, { status: moved.status, headers: new Headers(moved.headers) });
|
|
143
|
+
for (const name of ["Content-Security-Policy", "Reporting-Endpoints"]) {
|
|
144
|
+
const value = context.headers.get(name);
|
|
145
|
+
if (value) response.headers.set(name, value);
|
|
146
|
+
}
|
|
147
|
+
return response;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Request-context projection, independently testable without a running server. */
|
|
151
|
+
export function requestContextProxy(request: NextRequest): NextResponse {
|
|
136
152
|
const headers = new Headers(request.headers);
|
|
137
153
|
const path = request.nextUrl.pathname;
|
|
138
154
|
const playground = env().FREEHOLDER_PLAYGROUND === "1";
|
|
@@ -314,6 +330,7 @@ function requestPolicy(
|
|
|
314
330
|
uploadOrigins: storageOrigins,
|
|
315
331
|
creativeOrigins: parseCspOrigins(runtime.CSP_THIRD_PARTY_ORIGINS),
|
|
316
332
|
callOrigins: sfuOrigins(runtime.LIVEKIT_URL),
|
|
333
|
+
assistantOrigins: parseCspOrigins(runtime.PARADISEMODERN_ASSISTANT_ORIGINS).flatMap(origin => [origin, origin.replace(/^https:/, "wss:")]),
|
|
317
334
|
thirdPartyCreativeConsent: thirdPartyCreativeConsentGranted(
|
|
318
335
|
request.cookies.get(THIRD_PARTY_CREATIVE_CONSENT_COOKIE)?.value,
|
|
319
336
|
),
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
PM duplex browser codec distribution
|
|
2
|
+
|
|
3
|
+
The encoder and decoder files originate from opus-recorder 8.0.5 and the
|
|
4
|
+
Kyutai Unmute frontend. The encoder embeds libopus and speexdsp. Keep these
|
|
5
|
+
licenses and notices with all redistributed source and binary assets.
|
|
6
|
+
|
|
7
|
+
PM modified audio-output-processor.js on 13 October 2026 to report non-silent
|
|
8
|
+
output and correct its sample predicate. It is not byte-identical to upstream.
|
|
9
|
+
The decoder and encoder files remain unchanged from the inspected TRLD copy.
|
|
10
|
+
|
|
11
|
+
Current asset SHA-256 digests:
|
|
12
|
+
963d72b0ed123783f3ee66da8281f53436ec587f7dbbc94da2efd394c675a0b2 audio-output-processor.js
|
|
13
|
+
4967ed1f2c7f702d16e83b54c79ce3b6e9b3e3fd686ba73cfe55d59f61b96aee decoderWorker.min.js
|
|
14
|
+
cd1d29c43b3fa05719c3d024ed9b9f1528be92415bd6d39d413b262a61d1891f decoderWorker.min.wasm
|
|
15
|
+
c3a64a87b2868e37c0387da45262900820ec85ab448869011247cbd351858a95 encoderWorker.min.js
|
|
16
|
+
|
|
17
|
+
Upstream license source: https://raw.githubusercontent.com/chris-rudmin/opus-recorder/v8.0.5/LICENSE.md
|
|
18
|
+
|
|
19
|
+
## Opus Recorder License (MIT)
|
|
20
|
+
|
|
21
|
+
Original Work Copyright © 2013 Matt Diamond
|
|
22
|
+
|
|
23
|
+
Modified Work Copyright © 2014 Christopher Rudmin
|
|
24
|
+
|
|
25
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
26
|
+
|
|
27
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
28
|
+
|
|
29
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
## Opus License (BSD)
|
|
33
|
+
|
|
34
|
+
Copyright 2001-2011 Xiph.Org, Skype Limited, Octasic,
|
|
35
|
+
Jean-Marc Valin, Timothy B. Terriberry,
|
|
36
|
+
CSIRO, Gregory Maxwell, Mark Borgerding,
|
|
37
|
+
Erik de Castro Lopo
|
|
38
|
+
|
|
39
|
+
Redistribution and use in source and binary forms, with or without
|
|
40
|
+
modification, are permitted provided that the following conditions
|
|
41
|
+
are met:
|
|
42
|
+
|
|
43
|
+
- Redistributions of source code must retain the above copyright
|
|
44
|
+
notice, this list of conditions and the following disclaimer.
|
|
45
|
+
|
|
46
|
+
- Redistributions in binary form must reproduce the above copyright
|
|
47
|
+
notice, this list of conditions and the following disclaimer in the
|
|
48
|
+
documentation and/or other materials provided with the distribution.
|
|
49
|
+
|
|
50
|
+
- Neither the name of Internet Society, IETF or IETF Trust, nor the
|
|
51
|
+
names of specific contributors, may be used to endorse or promote
|
|
52
|
+
products derived from this software without specific prior written
|
|
53
|
+
permission.
|
|
54
|
+
|
|
55
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
56
|
+
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
57
|
+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
58
|
+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
|
|
59
|
+
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
60
|
+
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
61
|
+
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
62
|
+
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
63
|
+
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
64
|
+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
65
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
66
|
+
|
|
67
|
+
Opus is subject to the royalty-free patent licenses which are
|
|
68
|
+
specified at:
|
|
69
|
+
|
|
70
|
+
Xiph.Org Foundation:
|
|
71
|
+
https://datatracker.ietf.org/ipr/1524/
|
|
72
|
+
|
|
73
|
+
Microsoft Corporation:
|
|
74
|
+
https://datatracker.ietf.org/ipr/1914/
|
|
75
|
+
|
|
76
|
+
Broadcom Corporation:
|
|
77
|
+
https://datatracker.ietf.org/ipr/1526/
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
## Speex License (BSD)
|
|
81
|
+
|
|
82
|
+
© 2002-2003, Jean-Marc Valin/Xiph.Org Foundation
|
|
83
|
+
|
|
84
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
85
|
+
|
|
86
|
+
- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
87
|
+
|
|
88
|
+
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
89
|
+
|
|
90
|
+
- Neither the name of the Xiph.org Foundation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
|
91
|
+
|
|
92
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
Kyutai Unmute license (from the inspected source repository):
|
|
96
|
+
|
|
97
|
+
MIT License
|
|
98
|
+
|
|
99
|
+
Copyright (c) 2025 kyutai
|
|
100
|
+
|
|
101
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
102
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
103
|
+
in the Software without restriction, including without limitation the rights
|
|
104
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
105
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
106
|
+
furnished to do so, subject to the following conditions:
|
|
107
|
+
|
|
108
|
+
The above copyright notice and this permission notice shall be included in all
|
|
109
|
+
copies or substantial portions of the Software.
|
|
110
|
+
|
|
111
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
112
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
113
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
114
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
115
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
116
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
117
|
+
SOFTWARE.
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
// TODO: is there a way to get type-checking on this?
|
|
2
|
+
|
|
3
|
+
function asMs(samples) {
|
|
4
|
+
return ((samples * 1000) / sampleRate).toFixed(1);
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function asSamples(mili) {
|
|
8
|
+
return Math.round((mili * sampleRate) / 1000);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const DEFAULT_MAX_BUFFER_MS = 30 * 1000;
|
|
12
|
+
|
|
13
|
+
const debug = (...args) => {
|
|
14
|
+
// console.debug(...args);
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
class AudioOutputProcessor extends AudioWorkletProcessor {
|
|
18
|
+
constructor() {
|
|
19
|
+
super();
|
|
20
|
+
debug("AudioOutputProcessor created", currentFrame, sampleRate);
|
|
21
|
+
|
|
22
|
+
// Buffer length definitions
|
|
23
|
+
const frameSize = asSamples(80);
|
|
24
|
+
// initialBufferSamples: we wait to have at least that many samples before starting to play
|
|
25
|
+
this.initialBufferSamples = 1 * frameSize;
|
|
26
|
+
// once we have enough samples, we further wait that long before starting to play.
|
|
27
|
+
// This allows to have buffer lengths that are not a multiple of frameSize.
|
|
28
|
+
this.partialBufferSamples = asSamples(10);
|
|
29
|
+
// If the buffer length goes over that many, we will drop the oldest packets until
|
|
30
|
+
// we reach back initialBufferSamples + partialBufferSamples.
|
|
31
|
+
this.maxBufferSamples = asSamples(DEFAULT_MAX_BUFFER_MS);
|
|
32
|
+
// increments
|
|
33
|
+
this.partialBufferIncrement = asSamples(5);
|
|
34
|
+
this.maxPartialWithIncrements = asSamples(80);
|
|
35
|
+
this.maxBufferSamplesIncrement = asSamples(5);
|
|
36
|
+
this.maxMaxBufferWithIncrements = asSamples(80);
|
|
37
|
+
|
|
38
|
+
// State and metrics
|
|
39
|
+
this.initState();
|
|
40
|
+
this.resetGeneration = 0;
|
|
41
|
+
this.resetHeld = false;
|
|
42
|
+
|
|
43
|
+
this.port.onmessage = (event) => {
|
|
44
|
+
if (event.data.type == "reset") {
|
|
45
|
+
const generation = event.data.generation;
|
|
46
|
+
if (generation !== undefined && (!Number.isSafeInteger(generation) || generation <= 0 || generation <= this.resetGeneration)) return;
|
|
47
|
+
if (generation === undefined && this.resetGeneration > 0) return;
|
|
48
|
+
debug("Reset audio processor state.");
|
|
49
|
+
this.initState();
|
|
50
|
+
this.resetGeneration = generation ?? 0;
|
|
51
|
+
this.resetHeld = generation !== undefined;
|
|
52
|
+
if (this.resetHeld) this.port.postMessage({ type: "audio-output-reset", generation: this.resetGeneration });
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
if (event.data.type === "resume") {
|
|
56
|
+
if (event.data.generation === this.resetGeneration) {
|
|
57
|
+
this.resetHeld = false;
|
|
58
|
+
this.port.postMessage({ type: "audio-output-resumed", generation: this.resetGeneration });
|
|
59
|
+
}
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
let frame = event.data.frame;
|
|
63
|
+
this.frames.push(frame);
|
|
64
|
+
if (this.currentSamples() >= this.initialBufferSamples && !this.started) {
|
|
65
|
+
this.start();
|
|
66
|
+
}
|
|
67
|
+
if (this.pidx < 20) {
|
|
68
|
+
debug(
|
|
69
|
+
this.timestamp(),
|
|
70
|
+
"Got packet",
|
|
71
|
+
this.pidx++,
|
|
72
|
+
asMs(this.currentSamples()),
|
|
73
|
+
asMs(frame.length)
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
if (this.currentSamples() >= this.totalMaxBufferSamples()) {
|
|
77
|
+
debug(
|
|
78
|
+
this.timestamp(),
|
|
79
|
+
"Dropping packets",
|
|
80
|
+
asMs(this.currentSamples()),
|
|
81
|
+
asMs(this.totalMaxBufferSamples())
|
|
82
|
+
);
|
|
83
|
+
const target = this.initialBufferSamples + this.partialBufferSamples;
|
|
84
|
+
while (
|
|
85
|
+
this.currentSamples() >
|
|
86
|
+
this.initialBufferSamples + this.partialBufferSamples
|
|
87
|
+
) {
|
|
88
|
+
const first = this.frames[0];
|
|
89
|
+
let to_remove = this.currentSamples() - target;
|
|
90
|
+
to_remove = Math.min(
|
|
91
|
+
first.length - this.offsetInFirstBuffer,
|
|
92
|
+
to_remove
|
|
93
|
+
);
|
|
94
|
+
this.offsetInFirstBuffer += to_remove;
|
|
95
|
+
this.timeInStream += to_remove / sampleRate;
|
|
96
|
+
if (this.offsetInFirstBuffer == first.length) {
|
|
97
|
+
this.frames.shift();
|
|
98
|
+
this.offsetInFirstBuffer = 0;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
debug(this.timestamp(), "Packet dropped", asMs(this.currentSamples()));
|
|
102
|
+
this.maxBufferSamples += this.maxBufferSamplesIncrement;
|
|
103
|
+
this.maxBufferSamples = Math.min(
|
|
104
|
+
this.maxMaxBufferWithIncrements,
|
|
105
|
+
this.maxBufferSamples
|
|
106
|
+
);
|
|
107
|
+
debug("Increased maxBuffer to", asMs(this.maxBufferSamples));
|
|
108
|
+
}
|
|
109
|
+
this.port.postMessage({
|
|
110
|
+
totalAudioPlayed: this.totalAudioPlayed,
|
|
111
|
+
actualAudioPlayed: this.actualAudioPlayed,
|
|
112
|
+
delay: event.data.micDuration - this.timeInStream,
|
|
113
|
+
minDelay: this.minDelay,
|
|
114
|
+
maxDelay: this.maxDelay,
|
|
115
|
+
});
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
initState() {
|
|
120
|
+
this.frames = [];
|
|
121
|
+
this.offsetInFirstBuffer = 0;
|
|
122
|
+
this.firstOut = false;
|
|
123
|
+
this.outputStarted = false;
|
|
124
|
+
this.remainingPartialBufferSamples = 0;
|
|
125
|
+
this.timeInStream = 0;
|
|
126
|
+
this.resetStart();
|
|
127
|
+
|
|
128
|
+
// Metrics
|
|
129
|
+
this.totalAudioPlayed = 0;
|
|
130
|
+
this.actualAudioPlayed = 0;
|
|
131
|
+
this.maxDelay = 0;
|
|
132
|
+
this.minDelay = 2000;
|
|
133
|
+
// Debug
|
|
134
|
+
this.pidx = 0;
|
|
135
|
+
|
|
136
|
+
// For now let's reset the buffer params.
|
|
137
|
+
this.partialBufferSamples = asSamples(10);
|
|
138
|
+
this.maxBufferSamples = asSamples(DEFAULT_MAX_BUFFER_MS);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
totalMaxBufferSamples() {
|
|
142
|
+
return (
|
|
143
|
+
this.maxBufferSamples +
|
|
144
|
+
this.partialBufferSamples +
|
|
145
|
+
this.initialBufferSamples
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
timestamp() {
|
|
150
|
+
return Date.now() % 1000;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
currentSamples() {
|
|
154
|
+
let samples = 0;
|
|
155
|
+
for (let k = 0; k < this.frames.length; k++) {
|
|
156
|
+
samples += this.frames[k].length;
|
|
157
|
+
}
|
|
158
|
+
samples -= this.offsetInFirstBuffer;
|
|
159
|
+
return samples;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
resetStart() {
|
|
163
|
+
this.started = false;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
start() {
|
|
167
|
+
this.started = true;
|
|
168
|
+
this.remainingPartialBufferSamples = this.partialBufferSamples;
|
|
169
|
+
this.firstOut = true;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
canPlay() {
|
|
173
|
+
return (
|
|
174
|
+
this.started &&
|
|
175
|
+
this.frames.length > 0 &&
|
|
176
|
+
this.remainingPartialBufferSamples <= 0
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
process(inputs, outputs) {
|
|
181
|
+
if (this.resetHeld) return true;
|
|
182
|
+
const delay = this.currentSamples() / sampleRate;
|
|
183
|
+
if (this.canPlay()) {
|
|
184
|
+
this.maxDelay = Math.max(this.maxDelay, delay);
|
|
185
|
+
this.minDelay = Math.min(this.minDelay, delay);
|
|
186
|
+
}
|
|
187
|
+
const output = outputs[0][0];
|
|
188
|
+
if (!this.canPlay()) {
|
|
189
|
+
if (this.actualAudioPlayed > 0) {
|
|
190
|
+
this.totalAudioPlayed += output.length / sampleRate;
|
|
191
|
+
}
|
|
192
|
+
this.remainingPartialBufferSamples -= output.length;
|
|
193
|
+
return true;
|
|
194
|
+
}
|
|
195
|
+
if (this.firstOut) {
|
|
196
|
+
debug(
|
|
197
|
+
this.timestamp(),
|
|
198
|
+
"Audio resumed",
|
|
199
|
+
asMs(this.currentSamples()),
|
|
200
|
+
this.remainingPartialBufferSamples
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
let out_idx = 0;
|
|
204
|
+
let anyAudio = false;
|
|
205
|
+
while (out_idx < output.length && this.frames.length) {
|
|
206
|
+
const first = this.frames[0];
|
|
207
|
+
const to_copy = Math.min(
|
|
208
|
+
first.length - this.offsetInFirstBuffer,
|
|
209
|
+
output.length - out_idx
|
|
210
|
+
);
|
|
211
|
+
const subArray = first.subarray(
|
|
212
|
+
this.offsetInFirstBuffer,
|
|
213
|
+
this.offsetInFirstBuffer + to_copy
|
|
214
|
+
);
|
|
215
|
+
output.set(subArray, out_idx);
|
|
216
|
+
anyAudio =
|
|
217
|
+
anyAudio ||
|
|
218
|
+
output.some(function (x) {
|
|
219
|
+
return x > 1e-4 || x < -1e-4;
|
|
220
|
+
});
|
|
221
|
+
this.offsetInFirstBuffer += to_copy;
|
|
222
|
+
out_idx += to_copy;
|
|
223
|
+
if (this.offsetInFirstBuffer == first.length) {
|
|
224
|
+
this.offsetInFirstBuffer = 0;
|
|
225
|
+
this.frames.shift();
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
if (this.firstOut) {
|
|
229
|
+
this.firstOut = false;
|
|
230
|
+
for (let i = 0; i < out_idx; i++) {
|
|
231
|
+
output[i] *= i / out_idx;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
if (out_idx < output.length && !anyAudio) {
|
|
235
|
+
// At the end of a turn, we will get some padding of 0, so we only
|
|
236
|
+
// incease the buffer if we got some audio, e.g. we truly lagged in the middle of something.
|
|
237
|
+
debug(this.timestamp(), "Missed some audio", output.length - out_idx);
|
|
238
|
+
this.partialBufferSamples += this.partialBufferIncrement;
|
|
239
|
+
this.partialBufferSamples = Math.min(
|
|
240
|
+
this.partialBufferSamples,
|
|
241
|
+
this.maxPartialWithIncrements
|
|
242
|
+
);
|
|
243
|
+
debug("Increased partial buffer to", asMs(this.partialBufferSamples));
|
|
244
|
+
// We ran out of a buffer, let's revert to the started state to replenish it.
|
|
245
|
+
this.resetStart();
|
|
246
|
+
for (let i = 0; i < out_idx; i++) {
|
|
247
|
+
output[i] *= (out_idx - i) / out_idx;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
if (anyAudio && !this.outputStarted) {
|
|
251
|
+
this.outputStarted = true;
|
|
252
|
+
this.port.postMessage({ type: "audio-output-started" });
|
|
253
|
+
}
|
|
254
|
+
this.totalAudioPlayed += output.length / sampleRate;
|
|
255
|
+
this.actualAudioPlayed += out_idx / sampleRate;
|
|
256
|
+
this.timeInStream += out_idx / sampleRate;
|
|
257
|
+
return true;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
registerProcessor("audio-output-processor-reset-v1", AudioOutputProcessor);
|
|
261
|
+
// Keep old SDKs functional when they fetch this updated asset. A reused
|
|
262
|
+
// context may already have the legacy registration; the new name still wins.
|
|
263
|
+
try {
|
|
264
|
+
registerProcessor("audio-output-processor", class extends AudioOutputProcessor {});
|
|
265
|
+
} catch (error) {
|
|
266
|
+
if (error?.name !== "NotSupportedError") throw error;
|
|
267
|
+
}
|