From 6db8fdd60172449839f128928a2303f652a253b2 Mon Sep 17 00:00:00 2001 From: lyingbug Date: Fri, 18 Sep 2026 12:10:19 +0800 Subject: [PATCH] feat(artifacts): artifacts library page backed by a message_artifacts table (#3384) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(artifacts): add an artifacts library with its own message_artifacts table Skill-generated files were only reachable from inside the conversation that produced them. This adds a sidebar entry and a page that lists them across every conversation the user can see. Storage: artifacts move out of the messages.artifacts JSONB column into a new message_artifacts table (PostgreSQL 000103, SQLite 000023), backfilled on upgrade. The table is now the only source of truth; the old column is kept and the down migration writes the table back into it. The message repository loads artifacts for every message it returns and writes them on create, update (when non-nil) and fork, so callers still use Message.Artifacts. mod_time is stored as RFC 3339 text because the collector matches files on nanosecond mtimes, which PostgreSQL timestamps would truncate. API: GET /api/v1/artifacts lists the latest version of each file (grouped by session + source path, with version_count), scoped like the session list, with keyword / file type filters and paging. Downloads keep using the per-session endpoint. Frontend: "产物/Artifacts" sidebar entry (hidden without sandbox support), a library page with type tabs, search, date groups, infinite loading, preview drawer, download and jump-to-conversation; strings in all five locales. The sidebar's top-menu paths are now one shared list. * fix(artifacts): keep the restore hash guard and count versions by URL RecordRestoredArtifactMtime again only stamps rows whose content hash matches the restored content (and ignores empty hashes), and never rewrites the hash, matching MessageArtifacts.WithRestoredMtime. Without the guard a restore of v1's content also moved v2's mtime and relabelled v2 with v1's hash. The library's version_count now counts distinct URLs per (session, source path). A later answer that references an earlier file stores another row with the same URL, which COUNT(*) reported as an extra version. * fix(artifacts): scope the library to web chats and clear the legacy column The library used the session list's default scope, which counts sessions without an owner as legacy tenant-level rows. IM sessions are created without an owner, so every member saw the file names and titles of every IM chat (downloads then 404 for non-admins). The library now applies the same "web" bucket predicate as the home sidebar, extracted from QueryPaged so the two cannot drift: no IM mapping (including cleared ones), no embed marker, no API-key owner. Migrations 000103 / SQLite 000023 now clear messages.artifacts after the backfill. Message loads still SELECT *, so the stale copy was read on every history load. Only rows that carried artifacts are rewritten; the column stays for instances on the old binary during a rolling upgrade, and the down migration still rebuilds it from the table. * fix(migrations): keep the 000103 backfill alive on malformed artifact JSON A single file_size that is not an integer ("12.5", "") or a created_at that does not parse ("garbage", month 13) raised a cast error and failed the whole migration, blocking startup. Casts now go through pg_temp helpers that return NULL on bad input, falling back to 0 and the message's created_at; valid rows are unchanged. SQLite casts never raise; its migration test now covers the same malformed values. * docs(artifacts): describe the web-only library scope and the cleared legacy column --- frontend/src/api/artifacts.ts | 49 ++ frontend/src/assets/img/artifact-green.svg | 6 + frontend/src/assets/img/artifact.svg | 6 + frontend/src/components/menu.vue | 22 +- frontend/src/i18n/locales/en-US.ts | 42 + frontend/src/i18n/locales/ja-JP.ts | 42 + frontend/src/i18n/locales/ko-KR.ts | 42 + frontend/src/i18n/locales/ru-RU.ts | 42 + frontend/src/i18n/locales/zh-CN.ts | 42 + frontend/src/router/index.ts | 6 + frontend/src/stores/menu.ts | 2 + frontend/src/utils/artifactLibrary.test.mjs | 73 ++ frontend/src/utils/artifactLibrary.ts | 72 ++ .../src/views/artifacts/ArtifactLibrary.vue | 730 ++++++++++++++++++ internal/application/repository/message.go | 134 ++-- .../repository/message_artifact.go | 250 ++++++ .../repository/message_artifact_test.go | 268 +++++++ .../repository/message_fork_boundary_test.go | 21 +- .../repository/message_owner_scope_test.go | 2 +- .../repository/message_search_test.go | 2 +- internal/application/repository/session.go | 39 +- .../repository/session_fork_test.go | 4 +- .../memory/persistence_consistency_test.go | 2 +- internal/application/service/message.go | 58 ++ .../service/message_artifact_library_test.go | 53 ++ internal/application/service/session_fork.go | 6 +- ...migration_sqlite_message_artifacts_test.go | 135 ++++ .../migration_sqlite_versioned_schema_test.go | 6 +- internal/handler/session/artifact_library.go | 61 ++ internal/router/routes_chat.go | 8 + internal/types/interfaces/message.go | 10 + internal/types/message.go | 6 +- internal/types/message_artifact.go | 132 ++++ .../000023_message_artifacts_table.down.sql | 27 + .../000023_message_artifacts_table.up.sql | 59 ++ .../000103_message_artifacts_table.down.sql | 28 + .../000103_message_artifacts_table.up.sql | 86 +++ website-docs/04-api/02-api-chat.md | 17 + .../06-development/02-database-schema.md | 7 +- 39 files changed, 2475 insertions(+), 122 deletions(-) create mode 100644 frontend/src/api/artifacts.ts create mode 100644 frontend/src/assets/img/artifact-green.svg create mode 100644 frontend/src/assets/img/artifact.svg create mode 100644 frontend/src/utils/artifactLibrary.test.mjs create mode 100644 frontend/src/utils/artifactLibrary.ts create mode 100644 frontend/src/views/artifacts/ArtifactLibrary.vue create mode 100644 internal/application/repository/message_artifact.go create mode 100644 internal/application/repository/message_artifact_test.go create mode 100644 internal/application/service/message_artifact_library_test.go create mode 100644 internal/database/migration_sqlite_message_artifacts_test.go create mode 100644 internal/handler/session/artifact_library.go create mode 100644 internal/types/message_artifact.go create mode 100644 migrations/sqlite/000023_message_artifacts_table.down.sql create mode 100644 migrations/sqlite/000023_message_artifacts_table.up.sql create mode 100644 migrations/versioned/000103_message_artifacts_table.down.sql create mode 100644 migrations/versioned/000103_message_artifacts_table.up.sql diff --git a/frontend/src/api/artifacts.ts b/frontend/src/api/artifacts.ts new file mode 100644 index 00000000..c4e50271 --- /dev/null +++ b/frontend/src/api/artifacts.ts @@ -0,0 +1,49 @@ +import { get } from '@/utils/request' + +/** + * One file in the artifact library: the latest version of a file a skill + * generated in one of the caller's sessions. Download it through + * downloadArtifact(session_id, message_id, index) from '@/api/chat', which + * re-checks session ownership server side. + */ +export interface ArtifactLibraryItem { + session_id: string + session_title: string + message_id: string + index: number + /** `resource://` when the deployment runs a resource catalog. */ + handle?: string + file_name: string + file_type: string + file_size: number + source_path: string + created_at: string + /** How many times this file was (re)generated in its session. */ + version_count: number +} + +export interface ArtifactLibraryParams { + keyword?: string + /** Extensions such as ".pdf"; empty means every type. */ + fileTypes?: string[] + page?: number + pageSize?: number +} + +export interface ArtifactLibraryPage { + success: boolean + data: ArtifactLibraryItem[] + total: number + page: number + page_size: number +} + +export function listArtifactLibrary(params: ArtifactLibraryParams = {}) { + const query: Record = {} + const keyword = params.keyword?.trim() + if (keyword) query.keyword = keyword + if (params.fileTypes?.length) query.file_types = params.fileTypes.join(',') + if (params.page) query.page = params.page + if (params.pageSize) query.page_size = params.pageSize + return get('/api/v1/artifacts', { params: query }) +} diff --git a/frontend/src/assets/img/artifact-green.svg b/frontend/src/assets/img/artifact-green.svg new file mode 100644 index 00000000..c7280127 --- /dev/null +++ b/frontend/src/assets/img/artifact-green.svg @@ -0,0 +1,6 @@ + + + + + + diff --git a/frontend/src/assets/img/artifact.svg b/frontend/src/assets/img/artifact.svg new file mode 100644 index 00000000..3178793b --- /dev/null +++ b/frontend/src/assets/img/artifact.svg @@ -0,0 +1,6 @@ + + + + + + diff --git a/frontend/src/components/menu.vue b/frontend/src/components/menu.vue index 666bf24a..38f9166b 100644 --- a/frontend/src/components/menu.vue +++ b/frontend/src/components/menu.vue @@ -85,7 +85,7 @@