From 58535a580fd792b5c8ccd28b6572cfc84a87ee41 Mon Sep 17 00:00:00 2001 From: "shumeiko.it" Date: Wed, 4 Nov 2026 10:30:33 +0100 Subject: [PATCH] feat(server,web): open the license and third-party notices from Settings The sidecar serves LICENSE and THIRD_PARTY_NOTICES.md read-only at GET /api/legal/license and GET /api/legal/third-party-notices, from the copies next to service.js in the desktop app and the server archive, or from the repository root when run from source. Like every API route it needs the session token. Settings > General gets an About section at every detail level with License and Third-party licenses buttons; each opens the document in a dialog, loaded once through the adapter, so it works the same in the Tauri webview and in a browser. --- .../src/components/os/apps/SettingsApp.tsx | 4 + .../os/settings/LegalNotices.test.tsx | 65 ++++++++++ .../components/os/settings/LegalNotices.tsx | 95 ++++++++++++++ apps/web/src/lib/adapter.ts | 7 ++ apps/web/src/test/settings-trust.test.tsx | 13 ++ packages/server/src/local/index.ts | 2 + packages/server/src/local/routes/legal.ts | 47 +++++++ .../server/tests/local/legal-routes.test.ts | 116 ++++++++++++++++++ 8 files changed, 349 insertions(+) create mode 100644 apps/web/src/components/os/settings/LegalNotices.test.tsx create mode 100644 apps/web/src/components/os/settings/LegalNotices.tsx create mode 100644 packages/server/src/local/routes/legal.ts create mode 100644 packages/server/tests/local/legal-routes.test.ts diff --git a/apps/web/src/components/os/apps/SettingsApp.tsx b/apps/web/src/components/os/apps/SettingsApp.tsx index 7139915..8b64a09 100644 --- a/apps/web/src/components/os/apps/SettingsApp.tsx +++ b/apps/web/src/components/os/apps/SettingsApp.tsx @@ -34,6 +34,7 @@ import EraseDataDialog from '@/components/os/overlays/EraseDataDialog'; import TelegramDigestCard from '@/components/os/settings/TelegramDigestCard'; import ChannelsSettings from '@/components/os/settings/ChannelsSettings'; import BrowserCompanionSettings from '@/components/os/settings/BrowserCompanionSettings'; +import LegalNotices from '@/components/os/settings/LegalNotices'; import { AVAILABLE_SHAPES, useSelectedShape, type PromptShape } from '@/lib/shape-selection'; import { SectionLabel } from '@/components/os/warm'; import { ApprovalModal, type ApprovalRequest } from '@/components/ui/approval-modal'; @@ -565,6 +566,9 @@ const SettingsApp = () => { )} + {/* UF-059: the license and the third-party notices, at every level. */} + + {/* Phase 4.1: Erase All Data — visible at ALL tiers (GDPR Art. 17 cannot be tier-gated, so this stays on General rather than moving to the Everything-only Advanced tab). Closes the only diff --git a/apps/web/src/components/os/settings/LegalNotices.test.tsx b/apps/web/src/components/os/settings/LegalNotices.test.tsx new file mode 100644 index 0000000..4ff00d1 --- /dev/null +++ b/apps/web/src/components/os/settings/LegalNotices.test.tsx @@ -0,0 +1,65 @@ +import { cleanup, fireEvent, render, screen, within } from '@testing-library/react'; +import { afterEach, describe, expect, it, vi } from 'vitest'; + +const mocks = vi.hoisted(() => ({ + getLegalDocument: vi.fn(), +})); + +vi.mock('@/lib/adapter', () => ({ adapter: { getLegalDocument: mocks.getLegalDocument } })); + +import LegalNotices from './LegalNotices'; + +afterEach(() => { + cleanup(); + vi.clearAllMocks(); +}); + +describe('LegalNotices', () => { + it('opens the third-party notices served by the sidecar', async () => { + mocks.getLegalDocument.mockResolvedValue('# Third-party notices\n\n| sharp | 0.35.3 | Apache-2.0 |'); + render(); + + fireEvent.click(screen.getByRole('button', { name: /third-party licenses/i })); + + const dialog = await screen.findByRole('dialog', { name: /third-party licenses/i }); + expect(mocks.getLegalDocument).toHaveBeenCalledWith('third-party-notices'); + const text = await within(dialog).findByLabelText(/third-party licenses text/i); + expect(text).toHaveTextContent('# Third-party notices'); + expect(text).toHaveTextContent('sharp | 0.35.3'); + }); + + it('opens the MIT license', async () => { + mocks.getLegalDocument.mockResolvedValue('MIT License\n\nCopyright (c) 2026'); + render(); + + fireEvent.click(screen.getByRole('button', { name: /^license$/i })); + + const dialog = await screen.findByRole('dialog', { name: /^license$/i }); + expect(mocks.getLegalDocument).toHaveBeenCalledWith('license'); + expect(await within(dialog).findByLabelText(/license text/i)).toHaveTextContent('MIT License'); + }); + + it('loads each document once', async () => { + mocks.getLegalDocument.mockResolvedValue('notices'); + render(); + + fireEvent.click(screen.getByRole('button', { name: /third-party licenses/i })); + await screen.findByLabelText(/third-party licenses text/i); + fireEvent.keyDown(screen.getByRole('dialog'), { key: 'Escape' }); + fireEvent.click(screen.getByRole('button', { name: /third-party licenses/i })); + await screen.findByLabelText(/third-party licenses text/i); + + expect(mocks.getLegalDocument).toHaveBeenCalledTimes(1); + }); + + it('says so when the server cannot send the document', async () => { + mocks.getLegalDocument.mockRejectedValue(new Error('404')); + render(); + + fireEvent.click(screen.getByRole('button', { name: /third-party licenses/i })); + + const alert = await screen.findByRole('alert'); + expect(alert).toHaveTextContent(/could not load this document/i); + expect(alert.textContent).not.toMatch(/—/); + }); +}); diff --git a/apps/web/src/components/os/settings/LegalNotices.tsx b/apps/web/src/components/os/settings/LegalNotices.tsx new file mode 100644 index 0000000..7508a59 --- /dev/null +++ b/apps/web/src/components/os/settings/LegalNotices.tsx @@ -0,0 +1,95 @@ +/** + * Settings > General > About: the license and the third-party notices (UF-059). + * + * Both files ship with every installation; the sidecar serves them from + * /api/legal/*, so this works in the desktop app, the server install and a + * source checkout alike. + */ + +import { useState } from 'react'; +import { FileText, Loader2, Scale } from 'lucide-react'; +import { adapter } from '@/lib/adapter'; +import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/components/ui/dialog'; + +type LegalDocument = 'license' | 'third-party-notices'; +type DocumentState = { status: 'loading' } | { status: 'ready'; text: string } | { status: 'error' }; + +const DOCUMENTS: Record = { + license: { + label: 'License', + description: 'The MIT License that covers Waggle.', + }, + 'third-party-notices': { + label: 'Third-party licenses', + description: 'The open-source components that ship with Waggle, their licenses and where to get their source code.', + }, +}; + +const buttonClass = 'inline-flex items-center gap-1.5 px-3 py-1.5 text-[11px] rounded-lg bg-muted/50 text-foreground hover:bg-muted transition-colors'; + +export default function LegalNotices() { + const [openDocument, setOpenDocument] = useState(null); + const [documents, setDocuments] = useState>>({}); + + const show = (document: LegalDocument) => { + setOpenDocument(document); + if (documents[document]?.status === 'ready' || documents[document]?.status === 'loading') return; + setDocuments((current) => ({ ...current, [document]: { status: 'loading' } })); + adapter.getLegalDocument(document) + .then((text) => setDocuments((current) => ({ ...current, [document]: { status: 'ready', text } }))) + .catch(() => setDocuments((current) => ({ ...current, [document]: { status: 'error' } }))); + }; + + const state = openDocument ? documents[openDocument] : undefined; + + return ( +
+

+ + About +

+

+ Waggle is open-source software under the MIT License. The open-source components it ships keep their own licenses. +

+
+ {(Object.keys(DOCUMENTS) as LegalDocument[]).map((document) => ( + + ))} +
+ + { if (!open) setOpenDocument(null); }}> + + {openDocument && ( + <> + + {DOCUMENTS[openDocument].label} + {DOCUMENTS[openDocument].description} + + {state?.status === 'ready' ? ( +
+                  {state.text}
+                
+ ) : state?.status === 'error' ? ( +

+ Could not load this document from the Waggle server. Check that the server is running, then try again. +

+ ) : ( +

+ + Loading... +

+ )} + + )} +
+
+
+ ); +} diff --git a/apps/web/src/lib/adapter.ts b/apps/web/src/lib/adapter.ts index 449930c..3d3e79c 100644 --- a/apps/web/src/lib/adapter.ts +++ b/apps/web/src/lib/adapter.ts @@ -2712,6 +2712,13 @@ class LocalAdapter { return this.healthProbe(); } + // --- Legal --- + /** The license or the third-party notices that ship with this installation (UF-059). */ + async getLegalDocument(document: 'license' | 'third-party-notices'): Promise { + const res = await this.fetch(`/api/legal/${document}`); + return res.text(); + } + // --- Connectors --- /** §8a consumption switch: the shared ConnectorDefinition (incl. the C16 * lastSyncAt enrichment) replaces the deleted thin FE `Connector` type. */ diff --git a/apps/web/src/test/settings-trust.test.tsx b/apps/web/src/test/settings-trust.test.tsx index e083502..cab048a 100644 --- a/apps/web/src/test/settings-trust.test.tsx +++ b/apps/web/src/test/settings-trust.test.tsx @@ -17,6 +17,7 @@ const mocks = vi.hoisted(() => ({ clearTelemetry: vi.fn(), getMemoryCapabilities: vi.fn(), setMemoryExtraction: vi.fn(), + getLegalDocument: vi.fn(), }, })); @@ -75,6 +76,18 @@ afterEach(() => { }); describe('Settings trust flows', () => { + it('opens the third-party licenses from General at the default level (UF-059)', async () => { + mocks.adapter.getLegalDocument.mockResolvedValue('# Third-party notices'); + renderSettings(); + fireEvent.click(await screen.findByRole('tab', { name: /^general$/i })); + const section = await screen.findByTestId('settings-legal'); + expect(section.textContent).not.toMatch(/\u2014/); + fireEvent.click(within(section).getByRole('button', { name: /third-party licenses/i })); + const dialog = await screen.findByRole('dialog', { name: /third-party licenses/i }); + expect(await within(dialog).findByLabelText(/third-party licenses text/i)).toHaveTextContent('# Third-party notices'); + expect(mocks.adapter.getLegalDocument).toHaveBeenCalledWith('third-party-notices'); + }); + it('turns background fact extraction off and on from General', async () => { renderSettings(); fireEvent.click(await screen.findByRole('tab', { name: /^general$/i })); diff --git a/packages/server/src/local/index.ts b/packages/server/src/local/index.ts index dd3243e..560b935 100644 --- a/packages/server/src/local/index.ts +++ b/packages/server/src/local/index.ts @@ -156,6 +156,7 @@ import { identityRoutes } from './routes/identity.js'; import { agentRunRoutes } from './routes/agent-run.js'; import { agentRunsRoutes } from './routes/agent-runs.js'; import { localInferenceRoutes } from './routes/local-inference.js'; +import { legalRoutes } from './routes/legal.js'; import { OfflineManager } from './offline-manager.js'; import { listOllamaChatModelIds } from './model-availability.js'; import { log, createLogger } from './logger.js'; @@ -2045,6 +2046,7 @@ export async function buildLocalServer(config: Partial = {}) { await server.register(agentRunRoutes); await server.register(agentRunsRoutes); await server.register(localInferenceRoutes); + await server.register(legalRoutes); // ── Static file serving for web mode ────────────────────────── // When WAGGLE_FRONTEND_DIR is set, serve the React frontend as static files. diff --git a/packages/server/src/local/routes/legal.ts b/packages/server/src/local/routes/legal.ts new file mode 100644 index 0000000..f7a6b3c --- /dev/null +++ b/packages/server/src/local/routes/legal.ts @@ -0,0 +1,47 @@ +/** + * The license and the third-party notices, read-only, for Settings (UF-059). + * + * GET /api/legal/license LICENSE, text/plain + * GET /api/legal/third-party-notices THIRD_PARTY_NOTICES.md, text/markdown + * + * The desktop app and the server archive carry both files next to service.js + * (scripts/build-sidecar.mjs). Run from source, they come from the repository root. + */ + +import fs from 'node:fs'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; +import type { FastifyInstance } from 'fastify'; + +const LEGAL_DOCUMENTS = [ + { route: '/api/legal/license', file: 'LICENSE', contentType: 'text/plain; charset=utf-8' }, + { route: '/api/legal/third-party-notices', file: 'THIRD_PARTY_NOTICES.md', contentType: 'text/markdown; charset=utf-8' }, +] as const; + +export interface LegalRoutesOptions { + /** Directories searched in order for the files. */ + searchDirs?: string[]; +} + +/** + * The directory of this module (service.js in a packaged build), then the + * repository root when this module runs from packages/server/{src,dist}/local/routes. + */ +export function defaultLegalSearchDirs(moduleDir = path.dirname(fileURLToPath(import.meta.url))): string[] { + const repositoryRoot = path.resolve(moduleDir, '..', '..', '..', '..', '..'); + const fromSource = fs.existsSync(path.join(repositoryRoot, 'packages', 'server', 'package.json')); + return fromSource ? [moduleDir, repositoryRoot] : [moduleDir]; +} + +export async function legalRoutes(fastify: FastifyInstance, options: LegalRoutesOptions = {}) { + const searchDirs = options.searchDirs ?? defaultLegalSearchDirs(); + for (const document of LEGAL_DOCUMENTS) { + fastify.get(document.route, async (_request, reply) => { + const file = searchDirs.map((dir) => path.join(dir, document.file)).find((candidate) => fs.existsSync(candidate)); + if (!file) { + return reply.code(404).send({ error: `${document.file} is not part of this installation` }); + } + return reply.type(document.contentType).send(await fs.promises.readFile(file, 'utf8')); + }); + } +} diff --git a/packages/server/tests/local/legal-routes.test.ts b/packages/server/tests/local/legal-routes.test.ts new file mode 100644 index 0000000..df606a2 --- /dev/null +++ b/packages/server/tests/local/legal-routes.test.ts @@ -0,0 +1,116 @@ +/** + * GET /api/legal/license and GET /api/legal/third-party-notices (UF-059). + * + * Packaged builds read the files next to service.js; a source checkout reads + * them from the repository root. + */ + +import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from 'vitest'; +import fs from 'node:fs'; +import os from 'node:os'; +import path from 'node:path'; +import Fastify, { type FastifyInstance } from 'fastify'; +import { buildLocalServer } from '../../src/local/index.js'; +import { defaultLegalSearchDirs, legalRoutes } from '../../src/local/routes/legal.js'; +import { injectWithAuth } from '../test-utils.js'; + +const REPO_ROOT = path.resolve(import.meta.dirname, '..', '..', '..', '..'); +const ROUTES_DIR = path.resolve(import.meta.dirname, '..', '..', 'src', 'local', 'routes'); + +const temps: string[] = []; +function tempDir(): string { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-legal-')); + temps.push(dir); + return dir; +} + +async function serverWith(searchDirs: string[]): Promise { + const server = Fastify({ logger: false }); + await server.register(legalRoutes, { searchDirs }); + return server; +} + +afterEach(() => { + for (const dir of temps.splice(0)) fs.rmSync(dir, { recursive: true, force: true }); +}); + +describe('legal routes', () => { + it('serves the copies next to service.js in a packaged build', async () => { + const resources = tempDir(); + fs.writeFileSync(path.join(resources, 'LICENSE'), 'MIT License (packaged)\n'); + fs.writeFileSync(path.join(resources, 'THIRD_PARTY_NOTICES.md'), '# Third-party notices (packaged)\n'); + const server = await serverWith(defaultLegalSearchDirs(resources)); + try { + const notices = await server.inject({ method: 'GET', url: '/api/legal/third-party-notices' }); + expect(notices.statusCode).toBe(200); + expect(notices.headers['content-type']).toBe('text/markdown; charset=utf-8'); + expect(notices.payload).toBe('# Third-party notices (packaged)\n'); + + const license = await server.inject({ method: 'GET', url: '/api/legal/license' }); + expect(license.statusCode).toBe(200); + expect(license.headers['content-type']).toBe('text/plain; charset=utf-8'); + expect(license.payload).toBe('MIT License (packaged)\n'); + } finally { + await server.close(); + } + }); + + it('falls back to the repository root when run from source', async () => { + expect(defaultLegalSearchDirs(ROUTES_DIR)).toEqual([ROUTES_DIR, REPO_ROOT]); + const server = await serverWith(defaultLegalSearchDirs(ROUTES_DIR)); + try { + const notices = await server.inject({ method: 'GET', url: '/api/legal/third-party-notices' }); + expect(notices.statusCode).toBe(200); + expect(notices.payload).toBe(fs.readFileSync(path.join(REPO_ROOT, 'THIRD_PARTY_NOTICES.md'), 'utf8')); + const license = await server.inject({ method: 'GET', url: '/api/legal/license' }); + expect(license.payload).toBe(fs.readFileSync(path.join(REPO_ROOT, 'LICENSE'), 'utf8')); + } finally { + await server.close(); + } + }); + + it('does not look outside the packaged folder when it is not in a source checkout', () => { + const resources = tempDir(); + expect(defaultLegalSearchDirs(resources)).toEqual([resources]); + }); + + it('answers 404 when a file is missing', async () => { + const server = await serverWith([tempDir()]); + try { + const res = await server.inject({ method: 'GET', url: '/api/legal/third-party-notices' }); + expect(res.statusCode).toBe(404); + expect(JSON.parse(res.payload).error).toContain('THIRD_PARTY_NOTICES.md'); + } finally { + await server.close(); + } + }); +}); + +describe('legal routes in the sidecar', () => { + let server: FastifyInstance; + let dataDir: string; + + beforeAll(async () => { + // Exercise the real auth path: no loopback trust (vitest.setup.ts turns it on). + vi.stubEnv('WAGGLE_TRUST_LOCALHOST', '0'); + dataDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-legal-server-')); + server = await buildLocalServer({ dataDir }); + }); + + afterAll(async () => { + await server.close(); + fs.rmSync(dataDir, { recursive: true, force: true }); + vi.unstubAllEnvs(); + }); + + it('serves the notices to an authenticated client', async () => { + const res = await injectWithAuth(server, { method: 'GET', url: '/api/legal/third-party-notices' }); + expect(res.statusCode).toBe(200); + expect(res.payload).toContain('# Third-party notices'); + }); + + it('requires the session token like every other API route', async () => { + const res = await server.inject({ method: 'GET', url: '/api/legal/third-party-notices' }); + expect(res.statusCode).toBe(401); + }); +});