diff --git a/docs/affine-chatgpt.md b/docs/affine-chatgpt.md index 78a85572..ed8100d9 100644 --- a/docs/affine-chatgpt.md +++ b/docs/affine-chatgpt.md @@ -204,3 +204,73 @@ chat SSE endpoint also passed, including automatic profile routing. Only the server now owns the imported rotating session; the local active credential copy was removed after remote verification. Images and attachments remain unsupported and are not advertised by the profile. + +## AFFiNE managed trial counter versus provider limits + +`currentUser.copilot.quota { limit used }` describes AFFiNE's managed AI trial, +not the connected ChatGPT account's remaining usage. It must not be presented as +an OpenAI allowance or an invoice. BYOK execution is authorized independently +from quota-backed managed routes and records usage as `byok_server`. + +The pinned release has a legacy accounting edge case: deleted sessions retain +`messageCost`, while the BYOK exemption query joins usage billing units to live +message rows. Removing test conversations can therefore leave a misleading +managed-trial cost after their BYOK message rows disappear. The observed `8/10` +was explained by eight such retained units; the connected provider's request +history was tagged BYOK, not AFFiNE-managed usage. + +Do not fix this by resetting usage, promoting a plan, granting unlimited quota, +or weakening server enforcement. The raw quota and all provider/quota errors +remain unchanged. A self-hosted UI should instead identify AI as self-hosted +and explain that provider and server limits may apply, without a managed-trial +progress bar, Free badge or AFFiNE upgrade action. Cloud quota presentation +continues to use the original managed allowance. + +ChatGPT subscription access is already paid for by the account owner; it is not +unlimited. OpenAI still enforces the account's model, usage-window and app-access +limits. This connector does not add an API-key billing fallback. + +### Verified self-hosted usage rollout + +On 2026-11-06, frontend static package +`3s22k6a5p9731xmlr6x33icilcsfkzv2-affine-frontend-static-0.27.4` was copied to +`hectic-lab` and mounted over the unchanged backend's static directory. This +was a bounded systemd override, **not** a NixOS generation switch: the active +and boot generation remain `6rb81fr95gngs99x1dvjw9vzzd7snkn5`. A whole-host +build was interrupted rather than activating unrelated package updates. + +The root-owned persistent drop-in is +`/etc/systemd/system.control/affine-native.service.d/99-frontend-static-rollout.conf`. +It resets `BindReadOnlyPaths` and supplies only the new static bind. This +control directory is used because NixOS's `/etc/systemd/system` points into +the immutable store. GC roots `affine-frontend-static-rollout` and +`affine-frontend-static-rollback` under `/nix/var/nix/gcroots` retain the new +bundle and the previous `hrg3dkcacg5drizifiinjx481k6k33d5` bundle. + +Only `affine-native.service` restarted. The migration unit's active timestamp, +PostgreSQL, Redis and ChatGPT gateway process IDs and active timestamps stayed +unchanged. The running application's mount namespace was checked against the +new package's `index.html`; HTTPS `/info` remained healthy. An authenticated +headless browser verified the sidebar and account settings on desktop and a +Pixel 5 viewport: neutral self-hosted text, no trial count, progress bar or +billing action, in both English and Russian. This stable release serves the +responsive web application to mobile browsers; upstream's separate mobile shell +is canary-only. The separate +mobile component is covered by the source-level rendering check. + +A real BYOK text probe succeeded after deployment. AFFiNE's raw managed quota +remained `{ limit: 10, used: 8 }` before and after; no accounting records or +provider limits were altered. Existing browser sessions need a hard refresh. + +The drop-in survives reboots and NixOS generation rollbacks, and masks future +declarative frontend changes until removed. During the next reviewed NixOS +switch containing the desired `frontendPackage`, remove this exact drop-in, +run `systemctl daemon-reload`, and restart only `affine-native.service` so the +declared bind takes effect. Remove the temporary GC roots only after checking +that the active generation retains the desired package and rollback retention +is no longer needed. Do not use a broad `systemctl revert` operation. + +To roll back this bounded rollout while the original generation remains active, +remove that exact drop-in, daemon-reload and restart `affine-native.service`. +Its declared unit still binds the previous `hrg3dkcacg5drizifiinjx481k6k33d5` +bundle. Confirm health and the effective bind before removing any GC root. diff --git a/package/affine/default.nix b/package/affine/default.nix index 874dc5ff..7cde4fdc 100644 --- a/package/affine/default.nix +++ b/package/affine/default.nix @@ -14,7 +14,7 @@ openssl, inputs, system, - frontendPatches ? [ ./gitea-document-typography.patch ./byok-model-picker.patch ], + frontendPatches ? [ ./gitea-document-typography.patch ./byok-model-picker.patch ./self-hosted-ai-usage.patch ], }: let @@ -188,11 +188,12 @@ let cp -R packages/frontend/apps/mobile/dist "$out/mobile" runHook postInstall ''; - } // lib.optionalAttrs (builtins.elem ./byok-model-picker.patch frontendPatches) { + } // lib.optionalAttrs (builtins.elem ./byok-model-picker.patch frontendPatches || builtins.elem ./self-hosted-ai-usage.patch frontendPatches) { doCheck = true; checkPhase = '' runHook preCheck - AFFINE_SOURCE_DIR="$PWD" ${nodejs}/bin/node ${../../test/package/affine/test/model-picker.cjs} + ${lib.optionalString (builtins.elem ./byok-model-picker.patch frontendPatches) ''AFFINE_SOURCE_DIR="$PWD" ${nodejs}/bin/node ${../../test/package/affine/test/model-picker.cjs}''} + ${lib.optionalString (builtins.elem ./self-hosted-ai-usage.patch frontendPatches) ''AFFINE_SOURCE_DIR="$PWD" ${nodejs}/bin/node ${../../test/package/affine/test/ai-usage.cjs}''} runHook postCheck ''; }); diff --git a/package/affine/self-hosted-ai-usage.patch b/package/affine/self-hosted-ai-usage.patch new file mode 100644 index 00000000..b8d7e6b0 --- /dev/null +++ b/package/affine/self-hosted-ai-usage.patch @@ -0,0 +1,190 @@ +diff --git a/packages/frontend/core/src/components/root-app-sidebar/user-info/ai-usage.tsx b/packages/frontend/core/src/components/root-app-sidebar/user-info/ai-usage.tsx +--- a/packages/frontend/core/src/components/root-app-sidebar/user-info/ai-usage.tsx ++++ b/packages/frontend/core/src/components/root-app-sidebar/user-info/ai-usage.tsx +@@ -1,8 +1,10 @@ + import { ++ ServerService, + SubscriptionService, + UserCopilotQuotaService, + } from '@affine/core/modules/cloud'; + import { WorkspaceDialogService } from '@affine/core/modules/dialogs'; ++import { ServerDeploymentType } from '@affine/graphql'; + import { useI18n } from '@affine/i18n'; + import { useLiveData, useService } from '@toeverything/infra'; + import { cssVar } from '@toeverything/theme'; +@@ -15,6 +17,12 @@ import * as styles from './index.css'; + export const AIUsage = () => { + const t = useI18n(); + const copilotQuotaService = useService(UserCopilotQuotaService); ++ const serverService = useService(ServerService); ++ const isSelfhosted = useLiveData( ++ serverService.server.config$.selector( ++ c => c.type === ServerDeploymentType.Selfhosted ++ ) ++ ); + const subscriptionService = useService(SubscriptionService); + + useEffect(() => { +@@ -49,6 +55,24 @@ export const AIUsage = () => { + }); + }, [workspaceDialogService]); + ++ if (isSelfhosted) { ++ return ( ++