feat(core): adapt new lifecycle (#15575)

fix #15574


#### PR Dependency Tree


* **PR #15575** 👈

This tree was auto-generated by
[Charcoal](https://github.com/danerwilliams/charcoal)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

## New Features
* Added source-aware blob handling for current documents and page
history.
* Added document lifecycle support for trashing, restoring, and
permanently deleting documents.
* Copilot attachments now upload directly to session attachment
endpoints.

## Bug Fixes
* Document actions now complete before confirmations, notifications, and
navigation proceed.
* Improved cancellation, upload fallback, size-limit handling, and cloud
synchronization reliability.
* Invite-link expiration now remains reliable for long durations.
* Improved synchronization for document permissions and reserved
documents.

## Changes
* Blob management now displays attachment icons instead of image
previews.
* Cloud synchronization now uses a unified batch protocol.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
DarkSky authored and GitHub committed 2026-09-09 23:00:06 +08:00
1 parent b5556f12d8
commit 51fdf602ba
216 files changed
+4023 -1865

No files matched your search

+1 -1
View File
@@ -3,4 +3,4 @@ name: affine
description: AFFiNE cloud chart
type: application
version: 0.0.0
appVersion: "0.27.0"
appVersion: "0.27.5"
+1 -1
View File
@@ -3,7 +3,7 @@ name: front
description: AFFiNE front server
type: application
version: 0.0.0
appVersion: "0.27.0"
appVersion: "0.27.5"
dependencies:
- name: gcloud-sql-proxy
version: 0.0.0
@@ -3,7 +3,7 @@ name: graphql
description: AFFiNE GraphQL server
type: application
version: 0.0.0
appVersion: "0.27.0"
appVersion: "0.27.5"
dependencies:
- name: gcloud-sql-proxy
version: 0.0.0
Generated
+4 -4
View File
@@ -3109,7 +3109,7 @@ dependencies = [
"libc",
"log",
"rustversion",
"windows-link 0.2.1",
"windows-link 0.1.3",
"windows-result 0.4.1",
]
@@ -4933,9 +4933,9 @@ dependencies = [
[[package]]
name = "llm_adapter"
version = "0.2.23"
version = "0.2.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd18d17c60b800981d83224309ff9ce6b4093ae12929f2baad77dd5208383bdc"
checksum = "2ffd1c4caf6e911d0d756c195854b8e2665a574c7bd30edddebcabea6a0542b8"
dependencies = [
"base64 0.22.1",
"jsonschema",
@@ -10627,7 +10627,7 @@ version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
dependencies = [
"windows-sys 0.61.2",
"windows-sys 0.48.0",
]
[[package]]
+1 -1
View File
@@ -295,7 +295,7 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0",
"version": "0.27.5",
"devDependencies": {
"@vanilla-extract/vite-plugin": "^5.0.0",
"msw": "^2.13.2",
@@ -37,5 +37,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
@@ -44,5 +44,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
@@ -36,5 +36,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
+1 -1
View File
@@ -44,5 +44,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
@@ -36,5 +36,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
@@ -45,5 +45,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
@@ -31,5 +31,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
@@ -34,5 +34,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
@@ -41,5 +41,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
+1 -1
View File
@@ -37,5 +37,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
+1 -1
View File
@@ -38,5 +38,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
+1 -1
View File
@@ -39,5 +39,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
+1 -1
View File
@@ -37,5 +37,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
+1 -1
View File
@@ -37,5 +37,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
+1 -1
View File
@@ -47,5 +47,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
@@ -38,5 +38,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
+1 -1
View File
@@ -61,5 +61,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
@@ -36,5 +36,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
@@ -41,5 +41,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
+1 -1
View File
@@ -38,5 +38,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
+1 -1
View File
@@ -74,5 +74,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
+1 -1
View File
@@ -46,5 +46,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
+1 -1
View File
@@ -26,5 +26,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
+1 -1
View File
@@ -31,5 +31,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
@@ -32,5 +32,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
@@ -30,5 +30,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
@@ -35,5 +35,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
@@ -35,5 +35,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
+1 -1
View File
@@ -32,5 +32,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
+1 -1
View File
@@ -40,5 +40,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
+1 -1
View File
@@ -39,5 +39,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
+1 -1
View File
@@ -30,5 +30,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
+1 -1
View File
@@ -45,5 +45,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
+1 -1
View File
@@ -37,5 +37,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
+1 -1
View File
@@ -34,5 +34,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
+1 -1
View File
@@ -41,5 +41,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
+1 -1
View File
@@ -35,5 +35,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
+1 -1
View File
@@ -36,5 +36,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
@@ -26,5 +26,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
@@ -35,5 +35,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
@@ -40,5 +40,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
+1 -1
View File
@@ -38,5 +38,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
+1 -1
View File
@@ -36,5 +36,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
@@ -28,5 +28,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
@@ -41,5 +41,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
@@ -36,5 +36,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
+1 -1
View File
@@ -29,5 +29,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
+1 -1
View File
@@ -32,5 +32,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
+1 -1
View File
@@ -76,5 +76,5 @@
"@types/pdfmake": "^0.2.12",
"vitest": "^4.1.8"
},
"version": "0.27.0"
"version": "0.27.5"
}
@@ -40,5 +40,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
@@ -30,5 +30,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
@@ -27,5 +27,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
@@ -37,5 +37,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
@@ -37,5 +37,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
@@ -35,5 +35,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
@@ -32,5 +32,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
@@ -48,5 +48,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
@@ -44,5 +44,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
@@ -31,5 +31,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
@@ -26,5 +26,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
@@ -31,5 +31,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
@@ -29,5 +29,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
@@ -34,5 +34,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
@@ -36,5 +36,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
@@ -25,5 +25,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
+1 -1
View File
@@ -29,5 +29,5 @@
"vitepress": "^1.6.4",
"vue": "^3.4.38"
},
"version": "0.27.0"
"version": "0.27.5"
}
+1 -1
View File
@@ -14,5 +14,5 @@
"scripts": {
"build": "typedoc"
},
"version": "0.27.0"
"version": "0.27.5"
}
+1 -1
View File
@@ -63,5 +63,5 @@
"devDependencies": {
"vitest": "^4.1.8"
},
"version": "0.27.0"
"version": "0.27.5"
}
+1 -1
View File
@@ -49,5 +49,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
+1 -1
View File
@@ -38,5 +38,5 @@
"!dist/__tests__",
"shim.d.ts"
],
"version": "0.27.0"
"version": "0.27.5"
}
+1 -1
View File
@@ -33,5 +33,5 @@
"!src/__tests__",
"!dist/__tests__"
],
"version": "0.27.0"
"version": "0.27.5"
}
+1 -1
View File
@@ -41,5 +41,5 @@
"vite-plugin-wasm": "^3.5.0",
"vitest": "^4.1.8"
},
"version": "0.27.0"
"version": "0.27.5"
}
+1 -1
View File
@@ -39,5 +39,5 @@
"vite-plugin-wasm": "^3.5.0",
"vite-plugin-web-components-hmr": "^0.1.3"
},
"version": "0.27.0"
"version": "0.27.5"
}
+1 -1
View File
@@ -19,5 +19,5 @@
],
"ext": "ts,md,json"
},
"version": "0.27.0"
"version": "0.27.5"
}
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@affine/monorepo",
"version": "0.27.0",
"version": "0.27.5",
"private": true,
"author": "toeverything",
"license": "MIT",
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@affine/server-native",
"version": "0.27.0",
"version": "0.27.5",
"engines": {
"node": ">= 10.16.0 < 11 || >= 11.8.0"
},
@@ -1,6 +1,8 @@
use std::sync::Arc;
use affine_core::access_control::DomainCommand;
use affine_core::access_control::{
DomainCommand, ReservedDocumentAccessDecision, authorize_reserved_document, classify_reserved_document,
};
use chrono::Duration;
use napi::{Result, bindgen_prelude::Buffer};
use sqlx::PgPool;
@@ -110,7 +112,17 @@ async fn append_authorized_updates(
.map_err(super::to_napi_error)?;
}
let authorizer = PermissionAuthorizer::with_telemetry(pool.clone(), deployment, telemetry);
match input.write_intent {
let classification = classify_reserved_document(&input.workspace_id, &input.doc_id);
if authorize_reserved_document(&input.actor_user_id, classification) == ReservedDocumentAccessDecision::Denied {
return Err(napi_error("domain_permission_denied"));
}
let reserved = input.doc_id == permission_doc_id && classification.is_valid_reserved();
let write_intent = if reserved {
WorkspaceDocWriteIntentV1::UpdateDoc
} else {
input.write_intent
};
match write_intent {
WorkspaceDocWriteIntentV1::UpdateDoc => {
let command = DomainCommand::UpdateDoc {
doc_id: permission_doc_id.to_string(),
@@ -134,7 +146,7 @@ async fn append_authorized_updates(
.fetch_one(&mut *transaction)
.await
.map_err(|error| napi_error(format!("load workspace document source: {error}")))?;
if !source_exists {
if !source_exists && !reserved {
return Err(napi_error("doc_not_found"));
}
}
@@ -330,7 +342,7 @@ mod tests {
.bind(&workspace_id)
.execute(&pool)
.await?;
sqlx::query("INSERT INTO workspace_members(workspace_id,user_id,role,state) VALUES($1,$2,'member','active')")
sqlx::query("INSERT INTO workspace_members(workspace_id,user_id,role,state) VALUES($1,$2,'owner','active')")
.bind(&workspace_id)
.bind(&user_id)
.execute(&pool)
@@ -542,6 +554,26 @@ mod tests {
delete.commit().await?;
assert!(write.await?.is_err(), "a write must not recreate a deleted document");
for (reserved_doc_id, allowed) in [
(format!("db${workspace_id}$docProperties"), true),
(format!("userdata${user_id}${workspace_id}$settings"), true),
(format!("userdata$another-user${workspace_id}$settings"), false),
(format!("db${workspace_id}$unknown"), false),
] {
let result = runtime
.append_workspace_doc_updates_v1(AppendWorkspaceDocUpdatesInputV1 {
workspace_id: workspace_id.clone(),
doc_id: reserved_doc_id.clone(),
updates: vec![Buffer::from(vec![0, 0])],
actor_user_id: user_id.clone(),
write_intent: WorkspaceDocWriteIntentV1::CreateDoc,
permission_doc_id: None,
expected_permission_generation: None,
})
.await;
assert_eq!(result.is_ok(), allowed, "{reserved_doc_id}: {result:?}");
}
let creating_doc_id = format!("doc-writer-creating-{suffix}");
let root = affine_doc_loader::add_doc_to_root_doc(vec![0, 0], &creating_doc_id, None)?;
sqlx::query("INSERT INTO snapshots(workspace_id,guid,blob,size,updated_at) VALUES($1,$1,$2,$3,now())")
@@ -136,8 +136,7 @@ pub(super) async fn append_root_update(
.bind(&workspace_id)
.fetch_optional(&mut **transaction)
.await
.map_err(|error| RuntimeError::database("load root snapshot for append", error))?
.ok_or_else(|| RuntimeError::invalid_input("workspace_root_not_found"))?;
.map_err(|error| RuntimeError::database("load root snapshot for append", error))?;
let updates = sqlx::query_scalar::<_, Vec<u8>>(
"SELECT blob FROM updates WHERE workspace_id=$1 AND guid=$1 ORDER BY created_at FOR SHARE",
)
@@ -148,16 +147,32 @@ pub(super) async fn append_root_update(
let update = BASE64
.decode(encoded_update)
.map_err(|error| RuntimeError::invalid_input(format!("invalid root update: {error}")))?;
assert_no_lifecycle_change(snapshot, updates, &update)?;
let initialize = snapshot.is_none();
let merged = validate_root_update(snapshot.unwrap_or_else(|| vec![0, 0]), updates, &update)?;
let timestamp = next_workspace_doc_update_timestamp(transaction, &workspace_id, &workspace_id).await?;
sqlx::query("INSERT INTO updates (workspace_id,guid,blob,created_at,created_by) VALUES($1,$1,$2,$3,$4)")
if initialize {
sqlx::query(
"INSERT INTO snapshots (workspace_id,guid,blob,size,updated_at,created_by,updated_by) \
VALUES($1,$1,$2,$3,$4,$5,$5)",
)
.bind(&workspace_id)
.bind(&update)
.bind(&merged)
.bind(merged.len() as i64)
.bind(timestamp)
.bind(&actor_user_id)
.execute(&mut **transaction)
.await
.map_err(|error| RuntimeError::database("append canonical root update", error))?;
.map_err(|error| RuntimeError::database("initialize canonical root snapshot", error))?;
} else {
sqlx::query("INSERT INTO updates (workspace_id,guid,blob,created_at,created_by) VALUES($1,$1,$2,$3,$4)")
.bind(&workspace_id)
.bind(&update)
.bind(timestamp)
.bind(&actor_user_id)
.execute(&mut **transaction)
.await
.map_err(|error| RuntimeError::database("append canonical root update", error))?;
}
invalidate_doc_blob_projection(transaction, &workspace_id, &workspace_id, embedding_schema_ready).await?;
Ok(json!({ "timestamp": timestamp }))
}
@@ -198,7 +213,7 @@ pub(in crate::runtime::backend_runtime) async fn workspace_root_contains_active_
Ok(project_complete_root(binary, false, "before document creation")?.contains(doc_id))
}
fn assert_no_lifecycle_change(snapshot: Vec<u8>, updates: Vec<Vec<u8>>, incoming: &[u8]) -> RuntimeResult<()> {
fn validate_root_update(snapshot: Vec<u8>, updates: Vec<Vec<u8>>, incoming: &[u8]) -> RuntimeResult<Vec<u8>> {
let mut root = Doc::default();
root
.apply_update_from_binary_v1(&snapshot)
@@ -220,7 +235,7 @@ fn assert_no_lifecycle_change(snapshot: Vec<u8>, updates: Vec<Vec<u8>>, incoming
.encode_update_v1()
.map_err(|error| RuntimeError::invalid_state(format!("encode root after append failed: {error}")))?;
let new_all = project_complete_root(after.clone(), true, "after append")?;
let new_active = project_complete_root(after, false, "after append")?;
let new_active = project_complete_root(after.clone(), false, "after append")?;
let deletes = old_all.iter().any(|id| !new_all.contains(id));
let trashes = old_active.iter().any(|id| !new_active.contains(id));
let restores = new_active
@@ -229,7 +244,7 @@ fn assert_no_lifecycle_change(snapshot: Vec<u8>, updates: Vec<Vec<u8>>, incoming
if deletes || trashes || restores {
return Err(RuntimeError::invalid_input("doc_lifecycle_requires_command"));
}
Ok(())
Ok(after)
}
fn project_complete_root(
@@ -237,6 +252,9 @@ fn project_complete_root(
include_trash: bool,
operation: &str,
) -> RuntimeResult<std::collections::BTreeSet<String>> {
if binary == [0, 0] {
return Ok(Default::default());
}
let projection = affine_doc_loader::project_workspace_root(binary, include_trash)
.map_err(|error| RuntimeError::invalid_state(format!("project root {operation} failed: {error}")))?;
if !projection.complete {
@@ -389,24 +407,25 @@ mod tests {
fn ordinary_root_updates_cannot_encode_lifecycle_effects() {
let snapshot = affine_doc_loader::add_doc_to_root_doc(vec![0, 0], "a", None).unwrap();
let trash = mutate_root(snapshot.clone(), Vec::new(), "a", DocLifecycle::Trash).unwrap();
assert!(assert_no_lifecycle_change(snapshot.clone(), Vec::new(), &trash).is_err());
assert!(validate_root_update(snapshot.clone(), Vec::new(), &trash).is_err());
let restore = mutate_root(snapshot.clone(), vec![trash.clone()], "a", DocLifecycle::Restore).unwrap();
assert!(assert_no_lifecycle_change(snapshot.clone(), vec![trash], &restore).is_err());
assert!(validate_root_update(snapshot.clone(), vec![trash], &restore).is_err());
let delete = mutate_root(snapshot.clone(), Vec::new(), "a", DocLifecycle::Delete).unwrap();
assert!(assert_no_lifecycle_change(snapshot.clone(), Vec::new(), &delete).is_err());
assert!(validate_root_update(snapshot.clone(), Vec::new(), &delete).is_err());
let add = affine_doc_loader::add_doc_to_root_doc(snapshot.clone(), "b", None).unwrap();
assert!(assert_no_lifecycle_change(snapshot, Vec::new(), &add).is_ok());
assert!(validate_root_update(snapshot, Vec::new(), &add).is_ok());
}
#[test]
fn partial_workspace_root_is_rejected_before_semantic_comparison() {
let snapshot = affine_doc_loader::add_doc_to_root_doc(vec![0, 0], "a", None).unwrap();
let dependent_delta = mutate_root(snapshot.clone(), Vec::new(), "a", DocLifecycle::Trash).unwrap();
assert!(validate_root_update(vec![0, 0], Vec::new(), &dependent_delta).is_err());
assert!(
assert_no_lifecycle_change(dependent_delta, Vec::new(), &snapshot).is_err(),
validate_root_update(dependent_delta, Vec::new(), &snapshot).is_err(),
"a root update with missing client-clock predecessors must fail closed"
);
}
@@ -420,15 +439,20 @@ mod tests {
let suffix = uuid::Uuid::new_v4().simple().to_string();
let doc_id = format!("domain-lifecycle-{suffix}");
let root = affine_doc_loader::add_doc_to_root_doc(vec![0, 0], &doc_id, None).unwrap();
sqlx::query(
"INSERT INTO snapshots(workspace_id,guid,blob,updated_at,created_by,updated_by) VALUES($1,$1,$2,now(),$3,$3)",
let authorizer = PermissionAuthorizer::new(pool.clone(), Deployment::Cloud);
let mut transaction = pool.begin().await.unwrap();
append_root_update(
&authorizer,
&mut transaction,
actor_user_id.clone(),
workspace_id.clone(),
BASE64.encode(root),
None,
true,
)
.bind(&workspace_id)
.bind(root)
.bind(&actor_user_id)
.execute(&pool)
.await
.unwrap();
transaction.commit().await.unwrap();
for index in 0..3 {
let user_id = format!("domain-lifecycle-overflow-{index}-{suffix}");
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "@affine/server",
"private": true,
"version": "0.27.0",
"version": "0.27.5",
"description": "Affine Node.js server",
"type": "module",
"scripts": {
@@ -1,15 +1,22 @@
import { Readable } from 'node:stream';
import ava from 'ava';
import Sinon from 'sinon';
import { z } from 'zod';
import { Config, CopilotQuotaExceeded } from '../../base';
import type { BackendRuntimeProvider } from '../../core/backend-runtime';
import { StorageRuntimeProvider } from '../../core/storage-runtime';
import type { Models } from '../../models';
import type { ByokEntitlementPolicy } from '../../plugins/copilot/byok';
import type { ConversationPolicy } from '../../plugins/copilot/conversation/policy';
import { CapabilityRuntime } from '../../plugins/copilot/runtime/capability-runtime';
import { CopilotRuntimeEventConsumer } from '../../plugins/copilot/runtime/copilot-runtime-event-consumer';
import { AttachmentAdmissionHost } from '../../plugins/copilot/runtime/hosts/attachment-admission';
import { AttachmentMaterializer } from '../../plugins/copilot/runtime/hosts/attachment-materializer';
import { executeToolCall } from '../../plugins/copilot/runtime/tool/bridge';
import type { ToolRuntime } from '../../plugins/copilot/runtime/tool-runtime';
import { CopilotStorage } from '../../plugins/copilot/storage';
const test = ava;
@@ -36,6 +43,14 @@ function runtimeFixture(streamError?: string, enabled = true) {
return { events: [], result: { embeddings: [[1, 2]] } };
if (input.slot === 'search.rerank')
return { events: [], result: { scores: [0.9] } };
if (input.slot === 'prompt.structured')
return {
events: [],
result: {
output_json: { text: 'hello' },
output_text: '{"text":"hello"}',
},
};
if (
input.slot === 'image.generate' ||
input.slot === 'action.image.filter.sketch'
@@ -85,26 +100,61 @@ function runtimeFixture(streamError?: string, enabled = true) {
consume: async () => {},
} as unknown as CopilotRuntimeEventConsumer;
const config = { copilot: { enabled } } as Config;
const storageRuntime = Sinon.createStubInstance(StorageRuntimeProvider);
storageRuntime.getObject.callsFake(async () => ({
body: Readable.from(Buffer.from('image')),
metadata: {
contentType: 'image/png',
contentLength: 5,
lastModified: new Date(0),
},
}));
const storage = new CopilotStorage(storageRuntime);
const materializer = Sinon.createStubInstance(AttachmentMaterializer);
materializer.fetchRemoteAttachment.resolves({
data: 'aW1hZ2U=',
mimeType: 'image/png',
});
return {
calls,
storageRuntime,
storage,
materializer,
runtime: new CapabilityRuntime(
backend,
entitlement,
conversation,
tools,
consumer,
config
config,
new AttachmentAdmissionHost(materializer, storage)
),
};
}
test('disabled copilot rejects native execution before route access', async t => {
const { runtime, calls } = runtimeFixture(undefined, false);
const { runtime, calls, materializer } = runtimeFixture(undefined, false);
t.false(await runtime.embeddingConfigured('ignored'));
await t.throwsAsync(runtime.embed('ignored', ['text']), {
message: 'Copilot is disabled.',
});
await t.throwsAsync(
async () =>
await collect(
runtime.streamText({}, [
{
role: 'user',
content: 'hello',
attachments: ['https://example.com/image.png'],
},
])
),
{
message: 'Copilot is disabled.',
}
);
t.false(materializer.fetchRemoteAttachment.called);
t.deepEqual(calls, []);
});
@@ -136,8 +186,9 @@ test('all operation kinds enter the native slot pipeline', async t => {
);
});
test('image request builder receives only serializable request options', async t => {
const { runtime, calls } = runtimeFixture();
test('image request builder receives serializable options and materialized attachments', async t => {
const { runtime, calls, storage, storageRuntime, materializer } =
runtimeFixture();
const controller = new AbortController();
await collect(
@@ -168,10 +219,68 @@ test('image request builder receives only serializable request options', async t
},
},
});
const locator = storage.sessionAttachmentUrl(
'session-1',
'workspace-1',
'image'
);
for (const attachment of [
locator,
{ attachment: locator, mimeType: 'image/png' },
{ kind: 'url' as const, url: locator },
'https://example.com/input.png',
]) {
await collect(
runtime.streamImageArtifacts(
{},
[{ role: 'user', content: 'edit', attachments: [attachment] }],
{
user: 'user-1',
workspace: 'workspace-1',
session: 'session-1',
}
)
);
t.like(calls.at(-1)?.request, {
operation: 'edit',
images: [
{
kind: 'bytes',
data: [...Buffer.from('image')],
mediaType: 'image/png',
},
],
});
}
t.is(storageRuntime.getObject.callCount, 3);
t.true(
storageRuntime.getObject.alwaysCalledWithExactly(
'copilot',
'user-1/workspace-1/image'
)
);
t.true(materializer.fetchRemoteAttachment.calledOnce);
await t.throwsAsync(
async () =>
await collect(
runtime.streamImageArtifacts(
{},
[{ role: 'user', content: 'edit', attachments: [locator] }],
{
user: 'user-1',
workspace: 'workspace-2',
session: 'session-1',
}
)
),
{ message: 'Copilot attachment scope mismatch' }
);
t.is(calls.length, 5);
});
test('text streaming consumes native generic events', async t => {
const { runtime, calls } = runtimeFixture();
test('chat and structured requests materialize attachments and consume native results', async t => {
const { runtime, calls, storage } = runtimeFixture();
const chunks = await collect(
runtime.streamText({ profileId: 'profile-1', modelId: 'vendor/model:B' }, [
{ role: 'user', content: 'hello' },
@@ -183,6 +292,48 @@ test('text streaming consumes native generic events', async t => {
profileId: 'profile-1',
modelId: 'vendor/model:B',
});
const messages = [
{
role: 'user' as const,
content: 'hello',
attachments: [
storage.sessionAttachmentUrl('session-1', 'workspace-1', 'image'),
],
},
];
const options = {
user: 'user-1',
workspace: 'workspace-1',
session: 'session-1',
};
await collect(runtime.streamText({}, messages, options));
await collect(runtime.streamObject({}, messages, options));
await runtime.generateStructured({}, messages, options, undefined, {
responseSchemaJson: {
type: 'object',
properties: { text: { type: 'string' } },
required: ['text'],
},
schemaHash: 'test',
});
for (const call of calls.slice(1)) {
t.like(call.request, {
messages: [
{
role: 'user',
content: [
{ type: 'text', text: 'hello' },
{
type: 'image',
source: { media_type: 'image/png', data: 'aW1hZ2U=' },
},
],
},
],
});
t.false(JSON.stringify(call.request).includes('/api/copilot/'));
}
t.true(messages[0].attachments[0].startsWith('/api/copilot/'));
await t.throwsAsync(runtime.assertRoute('chat.default', {}, {}), {
instanceOf: CopilotQuotaExceeded,
});
@@ -41,6 +41,7 @@ import {
} from '../../plugins/copilot/feature';
import type { PromptService } from '../../plugins/copilot/prompt';
import type { ResolvedPrompt } from '../../plugins/copilot/prompt/spec';
import { ChatMessageAttachment } from '../../plugins/copilot/providers/types';
import { TextStreamParser } from '../../plugins/copilot/providers/utils';
import { ArtifactRetrievalService } from '../../plugins/copilot/retrieval/artifact';
import { DocumentRetrievalService } from '../../plugins/copilot/retrieval/document';
@@ -586,11 +587,26 @@ test('document tools enforce the user-selected hard scope', async t => {
}>
) => candidates,
};
const readDocIds: string[] = [];
const docReader = {
getDocMarkdown: async (_workspaceId: string, docId: string) => {
readDocIds.push(docId);
if (docId === 'missing-doc') return null;
return {
title: docId,
markdown: docId.startsWith('long-doc')
? 'a'.repeat(25_000)
: `${docId} content`,
revision: '1',
};
},
} as unknown as DocReader;
const hybrid = new DocumentRetrievalService(
readableAc,
lexicalIndexer,
vectorSearch,
documentModels
documentModels,
docReader
);
const hybridResult = await hybrid.search(options, 'query', undefined, 10);
t.is(hybridResult.retrievalMode, 'hybrid');
@@ -599,12 +615,21 @@ test('document tools enforce the user-selected hard scope', async t => {
['shared-doc']
);
t.true(hybridResult.hits[0].score > 1 / 61);
const healthyScoped = await hybrid.search(
options,
'query',
['shared-doc'],
10
);
t.is(healthyScoped.retrievalMode, 'hybrid');
t.deepEqual(readDocIds, []);
const lexicalOnly = new DocumentRetrievalService(
readableAc,
lexicalIndexer,
{ ...vectorSearch, canEmbedding: false },
documentModels
documentModels,
docReader
);
const lexicalResult = await lexicalOnly.search(
options,
@@ -623,7 +648,8 @@ test('document tools enforce the user-selected hard scope', async t => {
},
} as unknown as IndexerService,
vectorSearch,
documentModels
documentModels,
docReader
);
const vectorResult = await vectorOnly.search(options, 'query', undefined, 10);
t.is(vectorResult.retrievalMode, 'vector');
@@ -632,6 +658,65 @@ test('document tools enforce the user-selected hard scope', async t => {
vectorResult.hits.map(result => result.docId),
['shared-doc']
);
t.deepEqual(readDocIds, []);
const unavailable = new DocumentRetrievalService(
readableAc,
{
searchDocsByKeyword: async () => {
throw new SearchProviderUnavailable();
},
} as unknown as IndexerService,
{
...vectorSearch,
matchWorkspaceDocCandidates: async () => {
throw new Error('embedding_unavailable');
},
},
documentModels,
docReader
);
await t.throwsAsync(unavailable.search(options, 'query', undefined, 10), {
message: 'SEARCH_UNAVAILABLE',
});
const scoped = await unavailable.search(
options,
'query',
['cat-doc', 'hidden-doc', 'dog-doc', 'cat-doc'],
10
);
t.like(scoped, {
retrievalMode: 'scoped',
degradedReason: 'SEARCH_UNAVAILABLE',
});
t.deepEqual(readDocIds, ['cat-doc', 'dog-doc']);
t.deepEqual(
scoped.hits.map(hit => [hit.docId, hit.excerpt]),
[
['cat-doc', 'cat-doc content'],
['dog-doc', 'dog-doc content'],
]
);
readDocIds.length = 0;
await unavailable.search(options, 'query', ['cat-doc', 'dog-doc'], 1);
t.deepEqual(readDocIds, ['cat-doc']);
const bounded = await unavailable.search(options, 'query', ['long-doc'], 1);
t.is(bounded.hits[0].excerpt.length, 20_000);
const boundedMultiple = await unavailable.search(
options,
'query',
['long-doc-1', 'long-doc-2'],
2
);
t.deepEqual(
boundedMultiple.hits.map(hit => hit.excerpt.length),
[10_000, 10_000]
);
await t.throwsAsync(
unavailable.search(options, 'query', ['missing-doc'], 1),
{
message: 'SEARCH_UNAVAILABLE',
}
);
// model omits doc_ids: pinned scope applies
let search = buildDocumentSearch(retrieval, options, {
@@ -1632,6 +1717,16 @@ test.serial('copilot attachments require canonical session scope', async t => {
'notes.txt',
{ rawBody: attachmentBody } as never
);
for (const attachment of [
uploaded.url,
{ attachment: uploaded.url, mimeType: 'text/plain' },
{ kind: 'url', url: uploaded.url, mimeType: 'text/plain' },
]) {
t.true(ChatMessageAttachment.safeParse(attachment).success);
}
for (const url of ['/private/file.txt', '//example.com/file.txt']) {
t.false(ChatMessageAttachment.safeParse(url).success);
}
const admitted = await new AttachmentAdmissionHost(
{
fetchRemoteAttachment: async () =>
@@ -1697,6 +1792,17 @@ test.serial('copilot attachments require canonical session scope', async t => {
sessionId: 'session-2',
})
);
await t.throwsAsync(
new AttachmentAdmissionHost(
{} as never,
attachmentStorage
).admitPromptAttachment(uploaded.url, {
userId: 'user-1',
workspaceId: 'workspace-2',
sessionId: 'session-1',
}),
{ message: 'Copilot attachment scope mismatch' }
);
t.snapshot({
uploaded,
@@ -3,6 +3,7 @@ import test from 'ava';
import * as Sinon from 'sinon';
import { applyUpdate, Doc as YDoc, encodeStateAsUpdate } from 'yjs';
import { EventBus } from '../../base';
import { BackendRuntimeProvider } from '../../core/backend-runtime';
import {
DocStorageModule,
@@ -75,8 +76,16 @@ test('should not compact from a stale update view', async t => {
t.not(await db.update.count(), 0);
});
test('should retry if failed to insert updates', async t => {
test('should finish doc creation after retrying failed updates', async t => {
const stub = Sinon.stub(runtime, 'appendWorkspaceDocUpdatesTrustedV1');
t.teardown(() => stub.restore());
let creationCompleted = false;
t.teardown(
m.get(EventBus).on('doc.created', async () => {
await new Promise<void>(resolve => setImmediate(resolve));
creationCompleted = true;
})
);
stub.onCall(0).rejects(new Error());
stub.onCall(1).resolves(Date.now());
@@ -85,8 +94,7 @@ test('should retry if failed to insert updates', async t => {
adapter.pushDocUpdatesTrusted('1', '1', [Buffer.from([0, 0])])
);
t.is(stub.callCount, 2);
stub.restore();
t.true(creationCompleted);
});
test('should throw if meet max retry times', async t => {
@@ -47,23 +47,27 @@ test('should dispatch event listener', t => {
off();
});
test('should dispatch async event listener', async t => {
const { eventbus, listeners } = t.context;
for (const method of ['emitAsync', 'emitDetachedAsync'] as const) {
test(`should await event listeners with ${method}`, async t => {
const { eventbus, listeners } = t.context;
const runtimeListener = Sinon.stub().returnsArg(0);
const off = eventbus.on('__test__.event', runtimeListener);
const runtimeListener = Sinon.stub().callsFake(async payload => {
await new Promise<void>(resolve => setImmediate(resolve));
return payload;
});
const off = eventbus.on('__test__.event', runtimeListener);
t.teardown(off);
const payload = { count: 0 };
const returns = await eventbus.emitAsync('__test__.event', payload);
const payload = { count: 0 };
const returns = await eventbus[method]('__test__.event', payload);
t.true(listeners.onTestEvent.calledOnceWithExactly(payload));
t.true(listeners.onTestEventAndEvent2.calledOnceWithExactly(payload));
t.true(runtimeListener.calledOnceWithExactly(payload));
t.true(listeners.onTestEvent.calledOnceWithExactly(payload));
t.true(listeners.onTestEventAndEvent2.calledOnceWithExactly(payload));
t.true(runtimeListener.calledOnceWithExactly(payload));
t.deepEqual(returns, [payload, payload, payload]);
off();
});
t.deepEqual(returns, [payload, payload, payload]);
});
}
test('should dispatch multiple event handlers with same name', async t => {
const { eventbus, listeners } = t.context;
@@ -9,6 +9,7 @@ export class MockEventBus {
emit: Sinon.SinonStub = this.stub.emitAsync;
emitAsync: Sinon.SinonStub = this.stub.emitAsync;
emitDetached: Sinon.SinonStub = this.stub.emitAsync;
emitDetachedAsync: Sinon.SinonStub = this.stub.emitAsync;
broadcast: Sinon.SinonStub = this.stub.broadcast;
last<Event extends EventName>(
@@ -1,3 +1,6 @@
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import { PrismaClient } from '@prisma/client';
import test, { type ExecutionContext } from 'ava';
import Sinon from 'sinon';
@@ -606,31 +609,42 @@ test('space:join-batch should validate entries before joining', async t => {
}
});
test('space:join-batch should reject clients before 0.27.5', async t => {
test('space:join-batch rejects old clients and accepts the current web build', async t => {
const { user, cookieHeader } = await login(app);
const socket = createClient(url, cookieHeader);
const webPackage = JSON.parse(
readFileSync(
resolve(process.cwd(), '../../frontend/apps/web/package.json'),
'utf8'
)
);
try {
await waitForConnect(socket);
const result = unwrapResponse(
t,
await emitWithAck<{ clientId: string; success: boolean }>(
socket,
'space:join-batch',
{
spaces: [{ spaceType: 'userspace', spaceId: user.id }],
clientVersion: '0.27.4',
}
)
);
t.false(result.success);
await waitForDisconnect(socket);
} finally {
socket.disconnect();
for (const [clientVersion, accepted] of [
['0.27.4', false],
[webPackage.version, true],
] as const) {
const socket = createClient(url, cookieHeader);
try {
await waitForConnect(socket);
const result = unwrapResponse(
t,
await emitWithAck<{ clientId: string; success: boolean }>(
socket,
'space:join-batch',
{
spaces: [{ spaceType: 'userspace', spaceId: user.id }],
clientVersion,
}
)
);
t.is(result.success, accepted, clientVersion);
if (!accepted) await waitForDisconnect(socket);
} finally {
socket.disconnect();
}
}
});
test('space:join-batch should authorize once and join all requested rooms', async t => {
test('space:join-batch joins rooms and initializes workspace documents', async t => {
const models = app.get(Models);
const { user: owner, cookieHeader: ownerCookieHeader } = await login(app);
const { cookieHeader: deniedCookieHeader } = await login(app);
@@ -652,6 +666,11 @@ test('space:join-batch should authorize once and join all requested rooms', asyn
{ spaceType: 'workspace', spaceId: workspace.id },
{ spaceType: 'workspace', spaceId: workspace.id, docId: 'doc-a' },
{ spaceType: 'workspace', spaceId: workspace.id, docId: 'doc-b' },
{
spaceType: 'workspace',
spaceId: workspace.id,
docId: `db$${workspace.id}$docProperties`,
},
],
clientVersion: '0.27.5',
};
@@ -763,6 +782,54 @@ test('space:join-batch should authorize once and join all requested rooms', asyn
new Set(['doc-a', 'doc-b'])
);
await noDeniedEvent;
unwrapResponse(
t,
await emitWithAck(ownerSocket, 'space:join-batch', {
spaces: [
{
spaceType: 'workspace',
spaceId: workspace.id,
docId: workspace.id,
},
],
clientVersion: '0.27.5',
})
);
const root = addDocToRootDoc(Buffer.from([0, 0]), 'doc-a');
unwrapResponse(
t,
await emitWithAck(ownerSocket, 'space:push-doc-update', {
spaceType: 'workspace',
spaceId: workspace.id,
docId: workspace.id,
update: Buffer.from(root).toString('base64'),
})
);
for (const docId of [
'doc-a',
'doc-a',
`db$${workspace.id}$docProperties`,
]) {
await app.get(PrismaClient).$executeRaw`
UPDATE workspace_sync_permission_generations
SET generation = generation + 1
WHERE workspace_id = ${workspace.id}
`;
unwrapResponse(
t,
await emitWithAck(ownerSocket, 'space:join-batch', batch)
);
unwrapResponse(
t,
await emitWithAck(ownerSocket, 'space:push-doc-update', {
spaceType: 'workspace',
spaceId: workspace.id,
docId,
update: createYjsUpdateBase64(),
})
);
}
t.true(await models.doc.exists(workspace.id, 'doc-a'));
} finally {
ownerSocket.disconnect();
receiverSocket.disconnect();
@@ -108,6 +108,17 @@ export class EventBus
return true;
}
/**
* Await listeners without inheriting the current transaction.
*/
async emitDetachedAsync<T extends EventName>(event: T, payload: Events[T]) {
const requestId = this.cls.getId();
return await this.cls.run({ ifNested: 'override' }, async () => {
this.cls.set(CLS_ID, requestId ?? genRequestId('event'));
return await this.emitAsync(event, payload);
});
}
/**
* Broadcast event to trigger all listeners on all instance in cluster
*/
@@ -167,7 +167,7 @@ export class PgWorkspaceDocStorageAdapter extends DocStorageAdapter {
});
if (isNewDoc) {
this.event.emitDetached('doc.created', {
await this.event.emitDetachedAsync('doc.created', {
workspaceId,
docId,
editor: editorId,
@@ -651,7 +651,7 @@ test('doc realtime providers register share state and grants handlers', async t
} as unknown as PermissionAccess;
const models = {
doc: {
getDocInfo: async () => ({
getMeta: async () => ({
public: true,
mode: PublicDocMode.Page,
defaultRole: DocRole.Reader,
@@ -1210,7 +1210,7 @@ export class SpaceSyncGateway
});
}
}
for (const space of subscriptionsToAdd) {
for (const space of docSpaces) {
this.addActiveDocSubscription(
client,
space.spaceType,
@@ -1638,7 +1638,8 @@ abstract class SyncSocketAdapter {
docId: string,
updates: Buffer[],
editorId: string,
expectedPermissionGeneration?: number
expectedPermissionGeneration?: number,
writeIntent: 'update_doc' | 'create_doc' = 'update_doc'
) {
this.assertIn(spaceId);
return await this.storage.pushDocUpdates(
@@ -1646,7 +1647,8 @@ abstract class SyncSocketAdapter {
docId,
updates,
editorId,
expectedPermissionGeneration
expectedPermissionGeneration,
writeIntent
);
}
@@ -1697,7 +1699,10 @@ class WorkspaceSyncAdapter extends SyncSocketAdapter {
docId,
updates,
editorId,
expectedPermissionGeneration
expectedPermissionGeneration,
(await this.models.doc.exists(spaceId, docId))
? 'update_doc'
: 'create_doc'
);
}
@@ -79,10 +79,8 @@ export class DocShareRealtimeProvider implements OnModuleInit {
docId: string
): Promise<DocShareStateSnapshot | null> {
await this.assertRead(userId, workspaceId, docId);
const doc = await this.models.doc.getDocInfo(workspaceId, docId);
if (!doc) {
return null;
}
const doc = await this.models.doc.getMeta(workspaceId, docId);
if (!doc) return null;
return {
public: doc.public,
mode: PublicDocMode[doc.mode],
@@ -146,6 +146,10 @@ const AttachmentUrlSchema = z.string().refine(value => {
return true;
}
if (/^\/api\/copilot\/chat\/[^/?#]+\/attachments\/[^/?#]+\?/.test(value)) {
return true;
}
try {
const url = new URL(value);
return (
@@ -156,7 +160,7 @@ const AttachmentUrlSchema = z.string().refine(value => {
} catch {
return false;
}
}, 'attachments must use https?://, gs:// or data: urls');
}, 'attachments must use https?://, gs://, data: urls or Copilot session attachment locators');
export const PromptAttachmentSourceKindSchema = z.enum([
'url',
@@ -1,6 +1,7 @@
import { Inject, Injectable } from '@nestjs/common';
import { SearchProviderUnavailable } from '../../../base';
import { DocReader } from '../../../core/doc';
import { PermissionAccess } from '../../../core/permission';
import type { DocVisibility } from '../../../core/utils/blocksuite';
import { type DocChunkSimilarity, Models } from '../../../models';
@@ -74,7 +75,8 @@ export class DocumentRetrievalService {
private readonly indexer: IndexerService,
@Inject(DOCUMENT_VECTOR_SEARCH)
private readonly context: DocumentVectorSearch,
private readonly models: Models
private readonly models: Models,
private readonly docReader: DocReader
) {}
async search(
@@ -199,7 +201,38 @@ export class DocumentRetrievalService {
);
});
if (lexicalResult === null && vector === null) {
throw new Error('SEARCH_UNAVAILABLE');
if (!docIds?.length) throw new Error('SEARCH_UNAVAILABLE');
const readable = await this.readable(
userId,
workspaceId,
[...new Set(docIds)].map(docId => ({ docId }))
);
const documents = readable.slice(0, limit);
const maxChars = Math.floor(20_000 / Math.max(documents.length, 1));
const hits: DocumentSearchHit[] = await Promise.all(
documents.map(async ({ docId }) => {
const content = await this.docReader.getDocMarkdown(
workspaceId,
docId,
true
);
if (!content) throw new Error('SEARCH_UNAVAILABLE');
return {
docId,
title: content.title,
excerpt: content.markdown.slice(0, maxChars),
visibility: 'page' as const,
score: 0,
unitId: `document:${docId}`,
};
})
);
if (signal?.aborted) throw new Error('SEARCH_ABORTED');
return {
retrievalMode: 'scoped',
degradedReason: 'SEARCH_UNAVAILABLE',
hits,
} as const;
}
const perDoc = new Map<string, number>();
@@ -45,6 +45,7 @@ import {
type CopilotRuntimeEvent,
CopilotRuntimeEventConsumer,
} from './copilot-runtime-event-consumer';
import { AttachmentAdmissionHost } from './hosts/attachment-admission';
import { mapNativeSemanticError } from './native-errors';
import {
buildCanonicalNativeRequest,
@@ -80,9 +81,23 @@ export class CapabilityRuntime {
private readonly conversations: ConversationPolicy,
private readonly tools: ToolRuntime,
private readonly events: CopilotRuntimeEventConsumer,
private readonly config: Config
private readonly config: Config,
private readonly attachments: AttachmentAdmissionHost
) {}
private async prepareMessages(
messages: PromptMessage[],
options: RuntimeOptions
) {
assertCopilotEnabled(this.config);
return await this.attachments.preparePromptMessages(messages, {
userId: options.user ?? '',
workspaceId: options.workspace ?? '',
sessionId: options.session,
signal: options.signal,
});
}
private async access(options: RuntimeOptions) {
assertCopilotEnabled(this.config);
const workspaceId = options.workspace;
@@ -183,7 +198,7 @@ export class CapabilityRuntime {
const toolSet = await this.tools.getTools(options, '');
const { request } = await buildCanonicalNativeRequest({
model: 'route-selected',
messages,
messages: await this.prepareMessages(messages, options),
options,
toolContracts: buildToolContracts(toolSet),
attachmentCapability,
@@ -306,7 +321,7 @@ export class CapabilityRuntime {
}
const { request } = await buildCanonicalNativeStructuredRequest({
model: 'route-selected',
messages,
messages: await this.prepareMessages(messages, options),
options,
responseContract: contract,
attachmentCapability,
@@ -408,7 +423,10 @@ export class CapabilityRuntime {
slot,
buildLlmImageRequestFromMessages({
model: 'route-selected',
messages: preparePromptMessagesForNativeRequest(messages, true),
messages: preparePromptMessagesForNativeRequest(
await this.prepareMessages(messages, options),
true
),
options: { quality, seed, modelName, loras },
}),
cond,
@@ -3,9 +3,11 @@ import { createHash } from 'node:crypto';
import { Injectable } from '@nestjs/common';
import { OneMB, readBuffer } from '../../../../base';
import { applyPromptAttachmentMimeTypeHintForNative } from '../../providers/attachments';
import type {
PromptAttachment,
PromptAttachmentSourceKind,
PromptMessage,
} from '../../providers/types';
import { promptAttachmentMimeType } from '../../providers/utils';
import { CopilotStorage } from '../../storage';
@@ -162,6 +164,41 @@ export class AttachmentAdmissionHost {
private readonly storage: CopilotStorage
) {}
async preparePromptMessages(
messages: PromptMessage[],
context: AttachmentAdmissionContext
): Promise<PromptMessage[]> {
context.signal?.throwIfAborted();
return await Promise.all(
messages.map(async message => ({
...message,
attachments: message.attachments
? await Promise.all(
message.attachments.map(async attachment => {
const source = parsePromptAttachment(attachment);
if (
source.kind === 'file_handle' ||
source.kind === 'data' ||
source.kind === 'bytes' ||
source.url?.startsWith('data:')
)
return attachment;
return admittedAttachmentToPromptAttachment(
await this.admitPromptAttachment(
applyPromptAttachmentMimeTypeHintForNative(
attachment,
message
),
context
)
);
})
)
: message.attachments,
}))
);
}
async admitPromptAttachment(
attachment: PromptAttachment,
context: AttachmentAdmissionContext
+1 -1
View File
@@ -2,7 +2,7 @@
"name": "@affine/auth",
"private": true,
"type": "module",
"version": "0.27.0",
"version": "0.27.5",
"exports": {
".": "./src/index.ts"
},
Loaded 100 of 216 files, more files were not shown because too many files have changed in this diff. Show more