WeKnora/internal/application/service/temporary_document.go

840 lines
32 KiB
Go
Raw Normal View History

package service
import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"sync"
"time"
"unicode"
"github.com/Tencent/WeKnora/internal/common"
"github.com/Tencent/WeKnora/internal/infrastructure/chunker"
"github.com/Tencent/WeKnora/internal/infrastructure/docparser"
"github.com/Tencent/WeKnora/internal/logger"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
secutils "github.com/Tencent/WeKnora/internal/utils"
"github.com/google/uuid"
"github.com/hibiken/asynq"
)
const (
temporaryDocumentDefaultTTL = 24 * time.Hour
temporaryDocumentInlineTokens = 12000
temporaryDocumentPromptBudget = 12000
temporaryDocumentMaxPromptParts = 16
// defaultTemporaryDocumentImageOCRMaxPages caps how many page images a
// scanned / image-only document may send to the VLM, bounding OCR latency.
// Override via WEKNORA_CHAT_ATTACHMENT_OCR_MAX_PAGES.
defaultTemporaryDocumentImageOCRMaxPages = 8
// temporaryDocumentLowTextRunes is the extracted-text threshold (in runes,
// ignoring image markdown) below which a document is treated as
// scanned/image-only and eligible for the VLM OCR fallback.
temporaryDocumentLowTextRunes = 200
// defaultTemporaryDocumentOCRConcurrency bounds how many page images are
// OCR'd by the VLM at once. Multi-page scans benefit from concurrent OCR
// (wall-clock latency drops roughly linearly); the default matches the page
// cap so a full scan finishes in a single wave. Raising it loads the VLM
// backend harder — tune via WEKNORA_CHAT_ATTACHMENT_OCR_CONCURRENCY.
defaultTemporaryDocumentOCRConcurrency = 8
// temporaryDocumentOCRSufficientRunes is the OCR-yield threshold (in runes)
// above which a standalone image is treated as text-rich enough that a VLM
// caption fallback is unnecessary. Mirrors RAGFlow's OCR>VLM cascade cutoff
// (~32 chars): text-bearing screenshots / scans are served by OCR alone,
// while sparse-text images (diagrams, photos, icons) fall back to a caption
// whose semantic description is more useful than the little text OCR found.
temporaryDocumentOCRSufficientRunes = 32
)
// temporaryDocumentImageOCRMaxPages returns the max page count OCR'd per
// scanned document, honoring WEKNORA_CHAT_ATTACHMENT_OCR_MAX_PAGES.
func temporaryDocumentImageOCRMaxPages() int {
return envPositiveInt("WEKNORA_CHAT_ATTACHMENT_OCR_MAX_PAGES", defaultTemporaryDocumentImageOCRMaxPages)
}
// temporaryDocumentOCRConcurrency returns the VLM OCR concurrency, honoring
// WEKNORA_CHAT_ATTACHMENT_OCR_CONCURRENCY.
func temporaryDocumentOCRConcurrency() int {
return envPositiveInt("WEKNORA_CHAT_ATTACHMENT_OCR_CONCURRENCY", defaultTemporaryDocumentOCRConcurrency)
}
// envPositiveInt reads a positive integer from the environment, falling back to
// def when the variable is unset, non-numeric, or non-positive.
func envPositiveInt(key string, def int) int {
if raw := strings.TrimSpace(os.Getenv(key)); raw != "" {
if v, err := strconv.Atoi(raw); err == nil && v > 0 {
return v
}
}
return def
}
// markdownImagePattern matches markdown image references so text-yield
// estimation ignores image-only content (e.g. scanned PDFs).
var markdownImagePattern = regexp.MustCompile(`!\[[^\]]*\]\([^)]*\)`)
var temporaryDocumentExtensions = map[string]struct{}{
".docx": {}, ".doc": {}, ".pdf": {}, ".ppt": {}, ".pptx": {}, ".epub": {}, ".mhtml": {},
".xmind": {},
".xlsx": {}, ".xls": {},
".md": {}, ".markdown": {}, ".txt": {}, ".csv": {}, ".json": {}, ".xml": {}, ".yaml": {}, ".yml": {}, ".log": {}, ".html": {},
".jpg": {}, ".jpeg": {}, ".png": {}, ".gif": {}, ".bmp": {}, ".tiff": {}, ".webp": {},
".mp3": {}, ".wav": {}, ".m4a": {}, ".flac": {}, ".ogg": {}, ".aac": {},
}
var temporaryTextExtensions = map[string]struct{}{
".md": {}, ".markdown": {}, ".txt": {}, ".csv": {}, ".json": {}, ".xml": {}, ".yaml": {}, ".yml": {}, ".log": {},
}
feat(session): fork conversations from a historical user message (#3304) * feat(session): add fork schema and history queries 为会话分叉增加数据模型与迁移,并提供截至分叉点的消息查询以及会话内的 request_id 配对。 * feat(sandbox): add git, fork snapshots, and bootstrap hook 沙箱镜像安装 git,Docker 快照使用独立的 fork 命名空间,并在首次创建会话沙箱时接入可选 bootstrap。 * feat(session): checkpoint workspace at the end of each turn 每轮结束后对沙箱 /workspace 打 git checkpoint,并避免 fork 后因 mtime 刷新重复挂上产物、以及附件仍绑在父会话而无法还原。 * feat(session): fork conversations and reap orphan snapshots 支持从历史 user 消息分叉会话、首次打开时从快照 bootstrap,并在失败或删除未打开的分叉时回收孤儿快照。 * feat(chat): add session fork entry, prefill, and badges 在用户消息上提供分叉入口,进入新会话时预填原问题,并展示降级提示与来源角标。 * style: add missing newline so session_fork_delete_test.go is gofmt-clean * fix(session): prune later git history when rolling a fork workspace back reset --hard 只还原工作区,分叉点之后的 commit 仍可被 checkout。校验 SHA 后丢掉其他 ref 并 gc,避免错误基线。 * style(session): clear golangci-lint findings on the session-fork diff Pre-push lints the whole PR against main; wrap long lines, check c.Error, and fix gofumpt/revive/staticcheck so the merge can be pushed. * fix(chat): show the fork button on hover without shifting the thread Overlay the action so revealing it no longer expands the user bubble and janks the messages below. * fix(chat): drop the fork landing banners Keep the original question prefilled; do not explain sandbox snapshot vs checkpoint on the new thread. * fix(agent): dedupe artifacts by content hash, not size Same-path same-size rewrites were skipped as restores. Keep path+mtime as the cheap skip, hash bytes when mtime moves, and stamp the restored mtime onto this session only. * feat(session): allow forking from an assistant answer User forks still copy history before the question and prefill it. Assistant forks copy through that reply and reuse its checkpoint, with a degrade tooltip when none exists. * fix(session): resolve parent attachments from a forked chat Fork copies attachment IDs onto the new messages but leaves temporary_documents on the parent. Preview and Get now look up those IDs on the fork's copied history instead of a scoped miss. * fix(agent): rewrite sandbox refs after a content-hash skip Fork restores change mtime on unchanged files, so Collect returns empty and named citations were left as sandbox: paths. Resolve those names against session artifacts, notify only real uploads, and stamp restored mtime only on the matching content hash. * fix(session): drop a dead fork snapshot and retry checkpoint rewrite Create from a missing template now abandons bootstrap so the next Resolve uses a normal sandbox. A failed checkpoint rewrite no longer marks the bootstrap consumed, which previously made nested forks SANDBOX_REPLACED. * fix(session): load forked parent attachments into the prompt ResolveForPrompt used session-scoped lookup, so copied parent attachment IDs passed Get but never reached the model. Use the same resolveInSession path as preview. * fix(chat): keep the user-message fork button visible The control was hover-only, which hid it on touch. Always show it, and drop the unused degrade tooltip. --------- Co-authored-by: hailongzhao <hailongzhao@tencent.com> Co-authored-by: wizardchen <wizardchen@tencent.com>
2026-09-17 06:20:18 +00:00
// sessionAttachmentLookup is the message-store surface Get/OpenFile need to
// authorize a parent-session temporary document after a fork. Fork copies
// attachment IDs onto the new messages but leaves the temporary_documents
// row on the parent; preview still has to work from the fork session id.
type sessionAttachmentLookup interface {
GetSessionAttachments(ctx context.Context, sessionID string) (types.MessageAttachments, error)
}
type temporaryDocumentService struct {
feat(session): fork conversations from a historical user message (#3304) * feat(session): add fork schema and history queries 为会话分叉增加数据模型与迁移,并提供截至分叉点的消息查询以及会话内的 request_id 配对。 * feat(sandbox): add git, fork snapshots, and bootstrap hook 沙箱镜像安装 git,Docker 快照使用独立的 fork 命名空间,并在首次创建会话沙箱时接入可选 bootstrap。 * feat(session): checkpoint workspace at the end of each turn 每轮结束后对沙箱 /workspace 打 git checkpoint,并避免 fork 后因 mtime 刷新重复挂上产物、以及附件仍绑在父会话而无法还原。 * feat(session): fork conversations and reap orphan snapshots 支持从历史 user 消息分叉会话、首次打开时从快照 bootstrap,并在失败或删除未打开的分叉时回收孤儿快照。 * feat(chat): add session fork entry, prefill, and badges 在用户消息上提供分叉入口,进入新会话时预填原问题,并展示降级提示与来源角标。 * style: add missing newline so session_fork_delete_test.go is gofmt-clean * fix(session): prune later git history when rolling a fork workspace back reset --hard 只还原工作区,分叉点之后的 commit 仍可被 checkout。校验 SHA 后丢掉其他 ref 并 gc,避免错误基线。 * style(session): clear golangci-lint findings on the session-fork diff Pre-push lints the whole PR against main; wrap long lines, check c.Error, and fix gofumpt/revive/staticcheck so the merge can be pushed. * fix(chat): show the fork button on hover without shifting the thread Overlay the action so revealing it no longer expands the user bubble and janks the messages below. * fix(chat): drop the fork landing banners Keep the original question prefilled; do not explain sandbox snapshot vs checkpoint on the new thread. * fix(agent): dedupe artifacts by content hash, not size Same-path same-size rewrites were skipped as restores. Keep path+mtime as the cheap skip, hash bytes when mtime moves, and stamp the restored mtime onto this session only. * feat(session): allow forking from an assistant answer User forks still copy history before the question and prefill it. Assistant forks copy through that reply and reuse its checkpoint, with a degrade tooltip when none exists. * fix(session): resolve parent attachments from a forked chat Fork copies attachment IDs onto the new messages but leaves temporary_documents on the parent. Preview and Get now look up those IDs on the fork's copied history instead of a scoped miss. * fix(agent): rewrite sandbox refs after a content-hash skip Fork restores change mtime on unchanged files, so Collect returns empty and named citations were left as sandbox: paths. Resolve those names against session artifacts, notify only real uploads, and stamp restored mtime only on the matching content hash. * fix(session): drop a dead fork snapshot and retry checkpoint rewrite Create from a missing template now abandons bootstrap so the next Resolve uses a normal sandbox. A failed checkpoint rewrite no longer marks the bootstrap consumed, which previously made nested forks SANDBOX_REPLACED. * fix(session): load forked parent attachments into the prompt ResolveForPrompt used session-scoped lookup, so copied parent attachment IDs passed Get but never reached the model. Use the same resolveInSession path as preview. * fix(chat): keep the user-message fork button visible The control was hover-only, which hid it on touch. Always show it, and drop the unused degrade tooltip. --------- Co-authored-by: hailongzhao <hailongzhao@tencent.com> Co-authored-by: wizardchen <wizardchen@tencent.com>
2026-09-17 06:20:18 +00:00
repo interfaces.TemporaryDocumentRepository
fileService interfaces.FileService
resourceCatalog interfaces.ResourceCatalog
documentReader interfaces.DocumentReader
imageResolver *docparser.ImageResolver
modelService interfaces.ModelService
tenantService interfaces.TenantService
taskEnqueuer interfaces.TaskEnqueuer
sessionAttachments sessionAttachmentLookup
}
func NewTemporaryDocumentService(
repo interfaces.TemporaryDocumentRepository,
fileService interfaces.FileService,
resourceCatalog interfaces.ResourceCatalog,
documentReader interfaces.DocumentReader,
imageResolver *docparser.ImageResolver,
modelService interfaces.ModelService,
tenantService interfaces.TenantService,
taskEnqueuer interfaces.TaskEnqueuer,
feat(session): fork conversations from a historical user message (#3304) * feat(session): add fork schema and history queries 为会话分叉增加数据模型与迁移,并提供截至分叉点的消息查询以及会话内的 request_id 配对。 * feat(sandbox): add git, fork snapshots, and bootstrap hook 沙箱镜像安装 git,Docker 快照使用独立的 fork 命名空间,并在首次创建会话沙箱时接入可选 bootstrap。 * feat(session): checkpoint workspace at the end of each turn 每轮结束后对沙箱 /workspace 打 git checkpoint,并避免 fork 后因 mtime 刷新重复挂上产物、以及附件仍绑在父会话而无法还原。 * feat(session): fork conversations and reap orphan snapshots 支持从历史 user 消息分叉会话、首次打开时从快照 bootstrap,并在失败或删除未打开的分叉时回收孤儿快照。 * feat(chat): add session fork entry, prefill, and badges 在用户消息上提供分叉入口,进入新会话时预填原问题,并展示降级提示与来源角标。 * style: add missing newline so session_fork_delete_test.go is gofmt-clean * fix(session): prune later git history when rolling a fork workspace back reset --hard 只还原工作区,分叉点之后的 commit 仍可被 checkout。校验 SHA 后丢掉其他 ref 并 gc,避免错误基线。 * style(session): clear golangci-lint findings on the session-fork diff Pre-push lints the whole PR against main; wrap long lines, check c.Error, and fix gofumpt/revive/staticcheck so the merge can be pushed. * fix(chat): show the fork button on hover without shifting the thread Overlay the action so revealing it no longer expands the user bubble and janks the messages below. * fix(chat): drop the fork landing banners Keep the original question prefilled; do not explain sandbox snapshot vs checkpoint on the new thread. * fix(agent): dedupe artifacts by content hash, not size Same-path same-size rewrites were skipped as restores. Keep path+mtime as the cheap skip, hash bytes when mtime moves, and stamp the restored mtime onto this session only. * feat(session): allow forking from an assistant answer User forks still copy history before the question and prefill it. Assistant forks copy through that reply and reuse its checkpoint, with a degrade tooltip when none exists. * fix(session): resolve parent attachments from a forked chat Fork copies attachment IDs onto the new messages but leaves temporary_documents on the parent. Preview and Get now look up those IDs on the fork's copied history instead of a scoped miss. * fix(agent): rewrite sandbox refs after a content-hash skip Fork restores change mtime on unchanged files, so Collect returns empty and named citations were left as sandbox: paths. Resolve those names against session artifacts, notify only real uploads, and stamp restored mtime only on the matching content hash. * fix(session): drop a dead fork snapshot and retry checkpoint rewrite Create from a missing template now abandons bootstrap so the next Resolve uses a normal sandbox. A failed checkpoint rewrite no longer marks the bootstrap consumed, which previously made nested forks SANDBOX_REPLACED. * fix(session): load forked parent attachments into the prompt ResolveForPrompt used session-scoped lookup, so copied parent attachment IDs passed Get but never reached the model. Use the same resolveInSession path as preview. * fix(chat): keep the user-message fork button visible The control was hover-only, which hid it on touch. Always show it, and drop the unused degrade tooltip. --------- Co-authored-by: hailongzhao <hailongzhao@tencent.com> Co-authored-by: wizardchen <wizardchen@tencent.com>
2026-09-17 06:20:18 +00:00
messages interfaces.MessageRepository,
) interfaces.TemporaryDocumentService {
return &temporaryDocumentService{
repo: repo, fileService: fileService, resourceCatalog: resourceCatalog,
documentReader: documentReader, imageResolver: imageResolver,
modelService: modelService, tenantService: tenantService, taskEnqueuer: taskEnqueuer,
feat(session): fork conversations from a historical user message (#3304) * feat(session): add fork schema and history queries 为会话分叉增加数据模型与迁移,并提供截至分叉点的消息查询以及会话内的 request_id 配对。 * feat(sandbox): add git, fork snapshots, and bootstrap hook 沙箱镜像安装 git,Docker 快照使用独立的 fork 命名空间,并在首次创建会话沙箱时接入可选 bootstrap。 * feat(session): checkpoint workspace at the end of each turn 每轮结束后对沙箱 /workspace 打 git checkpoint,并避免 fork 后因 mtime 刷新重复挂上产物、以及附件仍绑在父会话而无法还原。 * feat(session): fork conversations and reap orphan snapshots 支持从历史 user 消息分叉会话、首次打开时从快照 bootstrap,并在失败或删除未打开的分叉时回收孤儿快照。 * feat(chat): add session fork entry, prefill, and badges 在用户消息上提供分叉入口,进入新会话时预填原问题,并展示降级提示与来源角标。 * style: add missing newline so session_fork_delete_test.go is gofmt-clean * fix(session): prune later git history when rolling a fork workspace back reset --hard 只还原工作区,分叉点之后的 commit 仍可被 checkout。校验 SHA 后丢掉其他 ref 并 gc,避免错误基线。 * style(session): clear golangci-lint findings on the session-fork diff Pre-push lints the whole PR against main; wrap long lines, check c.Error, and fix gofumpt/revive/staticcheck so the merge can be pushed. * fix(chat): show the fork button on hover without shifting the thread Overlay the action so revealing it no longer expands the user bubble and janks the messages below. * fix(chat): drop the fork landing banners Keep the original question prefilled; do not explain sandbox snapshot vs checkpoint on the new thread. * fix(agent): dedupe artifacts by content hash, not size Same-path same-size rewrites were skipped as restores. Keep path+mtime as the cheap skip, hash bytes when mtime moves, and stamp the restored mtime onto this session only. * feat(session): allow forking from an assistant answer User forks still copy history before the question and prefill it. Assistant forks copy through that reply and reuse its checkpoint, with a degrade tooltip when none exists. * fix(session): resolve parent attachments from a forked chat Fork copies attachment IDs onto the new messages but leaves temporary_documents on the parent. Preview and Get now look up those IDs on the fork's copied history instead of a scoped miss. * fix(agent): rewrite sandbox refs after a content-hash skip Fork restores change mtime on unchanged files, so Collect returns empty and named citations were left as sandbox: paths. Resolve those names against session artifacts, notify only real uploads, and stamp restored mtime only on the matching content hash. * fix(session): drop a dead fork snapshot and retry checkpoint rewrite Create from a missing template now abandons bootstrap so the next Resolve uses a normal sandbox. A failed checkpoint rewrite no longer marks the bootstrap consumed, which previously made nested forks SANDBOX_REPLACED. * fix(session): load forked parent attachments into the prompt ResolveForPrompt used session-scoped lookup, so copied parent attachment IDs passed Get but never reached the model. Use the same resolveInSession path as preview. * fix(chat): keep the user-message fork button visible The control was hover-only, which hid it on touch. Always show it, and drop the unused degrade tooltip. --------- Co-authored-by: hailongzhao <hailongzhao@tencent.com> Co-authored-by: wizardchen <wizardchen@tencent.com>
2026-09-17 06:20:18 +00:00
sessionAttachments: messages,
}
}
func temporaryDocumentTTL() time.Duration {
if raw := strings.TrimSpace(os.Getenv("WEKNORA_CHAT_ATTACHMENT_TTL_HOURS")); raw != "" {
if hours, err := strconv.Atoi(raw); err == nil && hours > 0 {
return time.Duration(hours) * time.Hour
}
}
return temporaryDocumentDefaultTTL
}
func (s *temporaryDocumentService) Create(
ctx context.Context,
tenantID uint64,
sessionID, fileName, mimeType string,
fileSize int64,
reader io.Reader,
options types.TemporaryDocumentCreateOptions,
) (*types.TemporaryDocument, error) {
if tenantID == 0 || strings.TrimSpace(sessionID) == "" {
return nil, fmt.Errorf("invalid attachment scope")
}
safeName, valid := secutils.ValidateInput(fileName)
if !valid {
return nil, fmt.Errorf("invalid characters in file name")
}
baseName, err := secutils.SafeFileName(safeName)
if err != nil {
return nil, fmt.Errorf("unsafe file name: %w", err)
}
ext := strings.ToLower(filepath.Ext(baseName))
resourceTenantID := options.ResourceTenantID
if resourceTenantID == 0 {
resourceTenantID = tenantID
}
if !s.supportsExtension(ctx, resourceTenantID, ext) {
return nil, fmt.Errorf("unsupported file type: %s", ext)
}
maxSize := secutils.GetMaxFileSizeMB() * 1024 * 1024
if fileSize <= 0 || fileSize > maxSize {
return nil, fmt.Errorf("file size must be between 1 byte and %dMB", secutils.GetMaxFileSizeMB())
}
data, err := io.ReadAll(io.LimitReader(reader, maxSize+1))
if err != nil {
return nil, fmt.Errorf("read attachment: %w", err)
}
if int64(len(data)) > maxSize {
return nil, fmt.Errorf("file exceeds size limit of %dMB", secutils.GetMaxFileSizeMB())
}
if int64(len(data)) != fileSize {
fileSize = int64(len(data))
}
storageName := fmt.Sprintf("chat_attachment_%s%s", uuid.NewString()[:12], ext)
resourceRef, err := s.fileService.SaveBytes(ctx, data, tenantID, storageName, true)
if err != nil {
return nil, fmt.Errorf("save attachment: %w", err)
}
optionsJSON, _ := json.Marshal(options)
document := &types.TemporaryDocument{
TenantID: tenantID, SessionID: sessionID, ResourceRef: resourceRef,
FileName: baseName, FileType: ext, MimeType: strings.TrimSpace(mimeType), FileSize: fileSize,
Status: types.TemporaryDocumentStatusUploaded, ExpiresAt: time.Now().Add(temporaryDocumentTTL()),
ProcessingOptions: types.JSON(optionsJSON),
}
if err := s.repo.Create(ctx, document); err != nil {
_ = s.fileService.DeleteFile(ctx, resourceRef)
return nil, fmt.Errorf("create attachment record: %w", err)
}
if s.resourceCatalog != nil {
feat(agent): preview skill artifacts and verify installs without running them (#2823) * feat(agent): preview skill artifacts and verify installs without running them Generated files open in the chat drawer with overlay/Esc returning to the list, and install verification no longer executes guessed skill entry points. A failed install can be retried from the saved archive. * ui(chat): use a folder icon and corner count for skill artifacts The eye and document glyphs read as preview or copy, and TDesign's loading spinner sat beside the icon. A folder with a top-right count matches the generated-files drawer without covering the toolbar action. * feat(artifact): implement inline artifact references with previews Add support for inline artifact references in Markdown, allowing generated files to be displayed as clickable cards. Implemented a new rendering mechanism for artifacts, enabling previews directly from the chat interface. Updated localization files for new inline preview hints and missing file messages. Introduced tests for artifact reference normalization and rendering logic. * fix(client): document AgentResponseType constants for revive Touching the const block made golangci-lint require comments on every exported value. Match the rest of the SDK and describe artifacts_pending. * fix(agent): wrap artifact-reference prompt lines for lll Pre-push golangci-lint rejected the branch on three lines over 120 characters in the sandbox: reference guidance. * fix(agent): scope HTML scripts and tighten install verification Keep scripted HTML preview on skill artifacts only, recreate sandbox workspace dirs after they are deleted, and treat nested skill files as dependencies rather than first-party imports. * refactor(artifact): enhance artifact reference handling and metadata Updated artifact metadata to include a stable resource handle for better identification. Adjusted artifact reference resolution to support both handle and name forms, ensuring compatibility with existing references. Enhanced tests to validate the new handling logic and ensure proper rendering of artifacts in chat messages. Improved documentation for clarity on artifact reference formats and their usage in the system.
2026-08-27 02:30:32 +00:00
if err := s.resourceCatalog.Bind(ctx, resourceRef, types.ResourceOwnerTemporaryDocument, document.ID, types.ResourceRelationSourceFile); err != nil {
_ = s.repo.DeleteScoped(ctx, tenantID, sessionID, document.ID)
_ = s.fileService.DeleteFile(ctx, resourceRef)
return nil, fmt.Errorf("bind attachment resource: %w", err)
}
}
payload, _ := json.Marshal(types.TemporaryDocumentTaskPayload{TenantID: tenantID, DocumentID: document.ID})
queue, _ := types.QueueForTaskType(types.TypeTemporaryDocumentProcess)
if _, err := s.taskEnqueuer.Enqueue(
asynq.NewTask(types.TypeTemporaryDocumentProcess, payload),
asynq.Queue(queue), asynq.MaxRetry(2), asynq.Timeout(10*time.Minute),
); err != nil {
_ = s.repo.MarkFailed(ctx, tenantID, document.ID, "failed to schedule document parsing")
document.Status = types.TemporaryDocumentStatusFailed
document.ErrorMessage = "failed to schedule document parsing"
return document, fmt.Errorf("schedule attachment parsing: %w", err)
}
return document, nil
}
func (s *temporaryDocumentService) supportsExtension(ctx context.Context, tenantID uint64, ext string) bool {
if _, ok := temporaryDocumentExtensions[ext]; ok {
return true
}
if s.documentReader == nil {
return false
}
var overrides map[string]string
if tenant, err := s.tenantService.GetTenantByID(ctx, tenantID); err == nil && tenant != nil {
overrides = tenant.ParserEngineConfig.ToOverridesMap()
}
engines, err := s.documentReader.ListEngines(ctx, overrides)
if err != nil {
return false
}
wanted := strings.TrimPrefix(strings.ToLower(ext), ".")
if wanted == "" || wanted == "url" {
return false
}
for _, engine := range engines {
if !engine.Available {
continue
}
for _, fileType := range engine.FileTypes {
if strings.TrimPrefix(strings.ToLower(strings.TrimSpace(fileType)), ".") == wanted {
return true
}
}
}
return false
}
func (s *temporaryDocumentService) Get(ctx context.Context, tenantID uint64, sessionID, documentID string) (*types.TemporaryDocument, error) {
feat(session): fork conversations from a historical user message (#3304) * feat(session): add fork schema and history queries 为会话分叉增加数据模型与迁移,并提供截至分叉点的消息查询以及会话内的 request_id 配对。 * feat(sandbox): add git, fork snapshots, and bootstrap hook 沙箱镜像安装 git,Docker 快照使用独立的 fork 命名空间,并在首次创建会话沙箱时接入可选 bootstrap。 * feat(session): checkpoint workspace at the end of each turn 每轮结束后对沙箱 /workspace 打 git checkpoint,并避免 fork 后因 mtime 刷新重复挂上产物、以及附件仍绑在父会话而无法还原。 * feat(session): fork conversations and reap orphan snapshots 支持从历史 user 消息分叉会话、首次打开时从快照 bootstrap,并在失败或删除未打开的分叉时回收孤儿快照。 * feat(chat): add session fork entry, prefill, and badges 在用户消息上提供分叉入口,进入新会话时预填原问题,并展示降级提示与来源角标。 * style: add missing newline so session_fork_delete_test.go is gofmt-clean * fix(session): prune later git history when rolling a fork workspace back reset --hard 只还原工作区,分叉点之后的 commit 仍可被 checkout。校验 SHA 后丢掉其他 ref 并 gc,避免错误基线。 * style(session): clear golangci-lint findings on the session-fork diff Pre-push lints the whole PR against main; wrap long lines, check c.Error, and fix gofumpt/revive/staticcheck so the merge can be pushed. * fix(chat): show the fork button on hover without shifting the thread Overlay the action so revealing it no longer expands the user bubble and janks the messages below. * fix(chat): drop the fork landing banners Keep the original question prefilled; do not explain sandbox snapshot vs checkpoint on the new thread. * fix(agent): dedupe artifacts by content hash, not size Same-path same-size rewrites were skipped as restores. Keep path+mtime as the cheap skip, hash bytes when mtime moves, and stamp the restored mtime onto this session only. * feat(session): allow forking from an assistant answer User forks still copy history before the question and prefill it. Assistant forks copy through that reply and reuse its checkpoint, with a degrade tooltip when none exists. * fix(session): resolve parent attachments from a forked chat Fork copies attachment IDs onto the new messages but leaves temporary_documents on the parent. Preview and Get now look up those IDs on the fork's copied history instead of a scoped miss. * fix(agent): rewrite sandbox refs after a content-hash skip Fork restores change mtime on unchanged files, so Collect returns empty and named citations were left as sandbox: paths. Resolve those names against session artifacts, notify only real uploads, and stamp restored mtime only on the matching content hash. * fix(session): drop a dead fork snapshot and retry checkpoint rewrite Create from a missing template now abandons bootstrap so the next Resolve uses a normal sandbox. A failed checkpoint rewrite no longer marks the bootstrap consumed, which previously made nested forks SANDBOX_REPLACED. * fix(session): load forked parent attachments into the prompt ResolveForPrompt used session-scoped lookup, so copied parent attachment IDs passed Get but never reached the model. Use the same resolveInSession path as preview. * fix(chat): keep the user-message fork button visible The control was hover-only, which hid it on touch. Always show it, and drop the unused degrade tooltip. --------- Co-authored-by: hailongzhao <hailongzhao@tencent.com> Co-authored-by: wizardchen <wizardchen@tencent.com>
2026-09-17 06:20:18 +00:00
return s.resolveInSession(ctx, tenantID, sessionID, documentID)
}
func (s *temporaryDocumentService) OpenFile(ctx context.Context, tenantID uint64, sessionID, documentID string) (io.ReadCloser, string, error) {
feat(session): fork conversations from a historical user message (#3304) * feat(session): add fork schema and history queries 为会话分叉增加数据模型与迁移,并提供截至分叉点的消息查询以及会话内的 request_id 配对。 * feat(sandbox): add git, fork snapshots, and bootstrap hook 沙箱镜像安装 git,Docker 快照使用独立的 fork 命名空间,并在首次创建会话沙箱时接入可选 bootstrap。 * feat(session): checkpoint workspace at the end of each turn 每轮结束后对沙箱 /workspace 打 git checkpoint,并避免 fork 后因 mtime 刷新重复挂上产物、以及附件仍绑在父会话而无法还原。 * feat(session): fork conversations and reap orphan snapshots 支持从历史 user 消息分叉会话、首次打开时从快照 bootstrap,并在失败或删除未打开的分叉时回收孤儿快照。 * feat(chat): add session fork entry, prefill, and badges 在用户消息上提供分叉入口,进入新会话时预填原问题,并展示降级提示与来源角标。 * style: add missing newline so session_fork_delete_test.go is gofmt-clean * fix(session): prune later git history when rolling a fork workspace back reset --hard 只还原工作区,分叉点之后的 commit 仍可被 checkout。校验 SHA 后丢掉其他 ref 并 gc,避免错误基线。 * style(session): clear golangci-lint findings on the session-fork diff Pre-push lints the whole PR against main; wrap long lines, check c.Error, and fix gofumpt/revive/staticcheck so the merge can be pushed. * fix(chat): show the fork button on hover without shifting the thread Overlay the action so revealing it no longer expands the user bubble and janks the messages below. * fix(chat): drop the fork landing banners Keep the original question prefilled; do not explain sandbox snapshot vs checkpoint on the new thread. * fix(agent): dedupe artifacts by content hash, not size Same-path same-size rewrites were skipped as restores. Keep path+mtime as the cheap skip, hash bytes when mtime moves, and stamp the restored mtime onto this session only. * feat(session): allow forking from an assistant answer User forks still copy history before the question and prefill it. Assistant forks copy through that reply and reuse its checkpoint, with a degrade tooltip when none exists. * fix(session): resolve parent attachments from a forked chat Fork copies attachment IDs onto the new messages but leaves temporary_documents on the parent. Preview and Get now look up those IDs on the fork's copied history instead of a scoped miss. * fix(agent): rewrite sandbox refs after a content-hash skip Fork restores change mtime on unchanged files, so Collect returns empty and named citations were left as sandbox: paths. Resolve those names against session artifacts, notify only real uploads, and stamp restored mtime only on the matching content hash. * fix(session): drop a dead fork snapshot and retry checkpoint rewrite Create from a missing template now abandons bootstrap so the next Resolve uses a normal sandbox. A failed checkpoint rewrite no longer marks the bootstrap consumed, which previously made nested forks SANDBOX_REPLACED. * fix(session): load forked parent attachments into the prompt ResolveForPrompt used session-scoped lookup, so copied parent attachment IDs passed Get but never reached the model. Use the same resolveInSession path as preview. * fix(chat): keep the user-message fork button visible The control was hover-only, which hid it on touch. Always show it, and drop the unused degrade tooltip. --------- Co-authored-by: hailongzhao <hailongzhao@tencent.com> Co-authored-by: wizardchen <wizardchen@tencent.com>
2026-09-17 06:20:18 +00:00
document, err := s.resolveInSession(ctx, tenantID, sessionID, documentID)
if err != nil {
return nil, "", err
}
if document == nil {
return nil, "", fmt.Errorf("attachment not found")
}
file, err := s.fileService.GetFile(ctx, document.ResourceRef)
if err != nil {
return nil, "", err
}
return file, document.FileName, nil
}
feat(session): fork conversations from a historical user message (#3304) * feat(session): add fork schema and history queries 为会话分叉增加数据模型与迁移,并提供截至分叉点的消息查询以及会话内的 request_id 配对。 * feat(sandbox): add git, fork snapshots, and bootstrap hook 沙箱镜像安装 git,Docker 快照使用独立的 fork 命名空间,并在首次创建会话沙箱时接入可选 bootstrap。 * feat(session): checkpoint workspace at the end of each turn 每轮结束后对沙箱 /workspace 打 git checkpoint,并避免 fork 后因 mtime 刷新重复挂上产物、以及附件仍绑在父会话而无法还原。 * feat(session): fork conversations and reap orphan snapshots 支持从历史 user 消息分叉会话、首次打开时从快照 bootstrap,并在失败或删除未打开的分叉时回收孤儿快照。 * feat(chat): add session fork entry, prefill, and badges 在用户消息上提供分叉入口,进入新会话时预填原问题,并展示降级提示与来源角标。 * style: add missing newline so session_fork_delete_test.go is gofmt-clean * fix(session): prune later git history when rolling a fork workspace back reset --hard 只还原工作区,分叉点之后的 commit 仍可被 checkout。校验 SHA 后丢掉其他 ref 并 gc,避免错误基线。 * style(session): clear golangci-lint findings on the session-fork diff Pre-push lints the whole PR against main; wrap long lines, check c.Error, and fix gofumpt/revive/staticcheck so the merge can be pushed. * fix(chat): show the fork button on hover without shifting the thread Overlay the action so revealing it no longer expands the user bubble and janks the messages below. * fix(chat): drop the fork landing banners Keep the original question prefilled; do not explain sandbox snapshot vs checkpoint on the new thread. * fix(agent): dedupe artifacts by content hash, not size Same-path same-size rewrites were skipped as restores. Keep path+mtime as the cheap skip, hash bytes when mtime moves, and stamp the restored mtime onto this session only. * feat(session): allow forking from an assistant answer User forks still copy history before the question and prefill it. Assistant forks copy through that reply and reuse its checkpoint, with a degrade tooltip when none exists. * fix(session): resolve parent attachments from a forked chat Fork copies attachment IDs onto the new messages but leaves temporary_documents on the parent. Preview and Get now look up those IDs on the fork's copied history instead of a scoped miss. * fix(agent): rewrite sandbox refs after a content-hash skip Fork restores change mtime on unchanged files, so Collect returns empty and named citations were left as sandbox: paths. Resolve those names against session artifacts, notify only real uploads, and stamp restored mtime only on the matching content hash. * fix(session): drop a dead fork snapshot and retry checkpoint rewrite Create from a missing template now abandons bootstrap so the next Resolve uses a normal sandbox. A failed checkpoint rewrite no longer marks the bootstrap consumed, which previously made nested forks SANDBOX_REPLACED. * fix(session): load forked parent attachments into the prompt ResolveForPrompt used session-scoped lookup, so copied parent attachment IDs passed Get but never reached the model. Use the same resolveInSession path as preview. * fix(chat): keep the user-message fork button visible The control was hover-only, which hid it on touch. Always show it, and drop the unused degrade tooltip. --------- Co-authored-by: hailongzhao <hailongzhao@tencent.com> Co-authored-by: wizardchen <wizardchen@tencent.com>
2026-09-17 06:20:18 +00:00
// resolveInSession finds a temporary document the caller may read from this
// session. Composer uploads live on temporary_documents.session_id; forked
// history keeps the parent's document id on copied messages, so a scoped miss
// falls back to tenant+id when that id appears on this session's messages.
func (s *temporaryDocumentService) resolveInSession(
ctx context.Context, tenantID uint64, sessionID, documentID string,
) (*types.TemporaryDocument, error) {
document, err := s.repo.GetScoped(ctx, tenantID, sessionID, documentID)
if err != nil || document != nil {
return document, err
}
document, err = s.repo.GetByID(ctx, tenantID, documentID)
if err != nil || document == nil {
return document, err
}
if document.SessionID == sessionID {
return document, nil
}
ok, authErr := s.sessionReferencesAttachment(ctx, sessionID, documentID)
if authErr != nil {
return nil, authErr
}
if !ok {
return nil, nil
}
return document, nil
}
func (s *temporaryDocumentService) sessionReferencesAttachment(
ctx context.Context, sessionID, documentID string,
) (bool, error) {
if s == nil || s.sessionAttachments == nil || sessionID == "" || documentID == "" {
return false, nil
}
attachments, err := s.sessionAttachments.GetSessionAttachments(ctx, sessionID)
if err != nil {
return false, fmt.Errorf("authorize forked attachment: %w", err)
}
for _, attachment := range attachments {
if attachment.ID == documentID {
return true, nil
}
}
return false, nil
}
func (s *temporaryDocumentService) List(ctx context.Context, tenantID uint64, sessionID string) ([]*types.TemporaryDocument, error) {
return s.repo.ListScoped(ctx, tenantID, sessionID)
}
func (s *temporaryDocumentService) Delete(ctx context.Context, tenantID uint64, sessionID, documentID string) error {
document, err := s.repo.GetScoped(ctx, tenantID, sessionID, documentID)
if err != nil || document == nil {
return err
}
for _, ref := range temporaryDocumentImageRefs(document.ImageRefs) {
_ = s.fileService.DeleteFile(ctx, ref.URL)
}
_ = s.fileService.DeleteFile(ctx, document.ResourceRef)
return s.repo.DeleteScoped(ctx, tenantID, sessionID, documentID)
}
func (s *temporaryDocumentService) Process(ctx context.Context, task *asynq.Task) error {
var payload types.TemporaryDocumentTaskPayload
if err := json.Unmarshal(task.Payload(), &payload); err != nil {
return fmt.Errorf("decode temporary document task: %w", err)
}
document, err := s.repo.GetByID(ctx, payload.TenantID, payload.DocumentID)
if err != nil || document == nil {
return err
}
if document.Status == types.TemporaryDocumentStatusReady {
return nil
}
startedAt := time.Now()
if err := s.repo.MarkProcessing(ctx, payload.TenantID, payload.DocumentID, startedAt); err != nil {
return err
}
// The attachment row and file remain scoped to payload.TenantID, while
// parser/model dependencies may belong to a verified shared-agent source.
resourceTenantID := payload.TenantID
var options types.TemporaryDocumentCreateOptions
if json.Unmarshal(document.ProcessingOptions, &options) == nil && options.ResourceTenantID != 0 {
resourceTenantID = options.ResourceTenantID
}
ctx = context.WithValue(ctx, types.TenantIDContextKey, resourceTenantID)
if tenant, tenantErr := s.tenantService.GetTenantByID(ctx, resourceTenantID); tenantErr == nil && tenant != nil {
ctx = context.WithValue(ctx, types.TenantInfoContextKey, tenant)
}
content, images, metadata, parseErr := s.parse(ctx, document)
if parseErr != nil {
retryCount, hasRetryCount := asynq.GetRetryCount(ctx)
maxRetry, hasMaxRetry := asynq.GetMaxRetry(ctx)
if hasRetryCount && hasMaxRetry && retryCount < maxRetry {
logger.Warnf(ctx, "temporary document parse will retry: document_id=%s attempt=%d/%d err=%v",
payload.DocumentID, retryCount+1, maxRetry+1, parseErr)
return parseErr
}
message := parseErr.Error()
if len(message) > 2000 {
message = message[:2000]
}
_ = s.repo.MarkFailed(ctx, payload.TenantID, payload.DocumentID, message)
logger.Errorf(ctx, "temporary document parse failed: document_id=%s err=%v", payload.DocumentID, parseErr)
if hasRetryCount && hasMaxRetry {
return parseErr
}
// Lite mode doesn't expose Asynq retry metadata in the context. Surface
// a terminal state immediately instead of leaving the UI spinning.
return nil
}
content = common.CleanInvalidUTF8(content)
fix(chunking): 修复内联 HTML 表格与保护模式导致的超长 chunk (#3404) * fix(ocr): normalize inline HTML tables in VLM/OCR sanitizer The VLM/OCR path left raw HTML <table> blocks in the body: looksLikeHTML does not match markdown text with an embedded table, so ocrHTMLToMarkdown (which also lacked the table plugin) never ran. The chunker then split the raw markup mid-row, tripping the 7500-char hard limit and invalidating the heading layer for the whole document. Reuse docparser.NormalizeHTMLTables (already used by PaddleOCR-VL) instead: export it and call it unconditionally from sanitizeOCRText, since it is a safe no-op when no <table> is present. * fix(ocr): keep merged-cell HTML tables chunkable - Narrow span detection to values >1 so rowspan=1/colspan=1 tables still convert to GFM instead of falling back to raw HTML. - On the merged-cell fallback, split each <tr> onto its own line and pad the block with blank lines so the chunker can split by "\n" instead of force-cutting at the absolute max size. * fix(knowledge): normalize inline HTML tables for all parser engines before chunking Previously NormalizeHTMLTables was only wired into the paddleocr-vl converters, so tables emitted by other engines (notably MinerU) reached the chunker as raw single-line <table> blocks and were force-cut at the 7500 char hard limit. Add the normalization at the two central pre-chunk boundaries instead: - knowledge_process.go: after all converter/ASR/image markdown rewrites, before the chunker step (covers every parser engine) - knowledge_create.go: before building the splitter config for manual markdown Converters keep their existing calls (idempotent no-op). * fix(chunker): bound Markdown link/image protected patterns to one line The unbounded [^\]]* / [^)]+ character classes in the Markdown link and image protectedPatterns also matched newlines, so a stray '[' left behind by OCR (e.g. a full-width 【公章】 mis-read as half-width) could pair with a distant '](' and swallow whole paragraphs as one protected atomic span. buildUnitsWithProtection then treated the region as a single unit and mergeUnits could not split it, defeating chunking (observed 1 chunk of 1753 runes where 16 chunks were expected). Exclude newlines and bound the link text (<=200) / destination (<=500), matching CommonMark's rule that neither may span a blank line. Regression: protected_spans_test.go — same input now yields multiple chunks with max 125 runes (chunkSize=128) instead of a single 1753-rune chunk; well-formed single-line links/images stay protected. * fix(ocr): keep non-GFM HTML tables chunkable across all ingest paths Conversion fallbacks for <br>, nested lists, and one-column tables still left single-line HTML, so MinerU output could hit the same oversized-chunk bug. Split those rows, accept one-column GFM, skip fenced examples, and normalize before image resolution so cell images are not escaped. * style: wrap long lines and avoid shadowing max in table-chunk tests Pre-push lll/revive failed on the PR diff versus origin/main. --------- Co-authored-by: neoczr <neoczr@users.noreply.github.com> Co-authored-by: wizardchen <wizardchen@tencent.com>
2026-09-18 13:10:17 +00:00
content = chunker.NormalizeLineEndings(content)
content = docparser.NormalizeHTMLTables(content)
lang := chunker.DetectLanguage(content)
cfg := chunker.DefaultConfig()
cfg.Strategy = chunker.StrategyAuto
cfg.ChunkSize = 1600
cfg.ChunkOverlap = 160
parts := chunker.Split(content, cfg)
chunks := make([]types.TemporaryDocumentChunk, 0, len(parts))
for _, part := range parts {
chunks = append(chunks, types.TemporaryDocumentChunk{
Seq: part.Seq, Content: part.Content, ContextHeader: part.ContextHeader,
Start: part.Start, End: part.End, TokenCount: chunker.ApproxTokenCount(part.EmbeddingContent(), lang),
})
}
chunksJSON, _ := json.Marshal(chunks)
imagesJSON, _ := json.Marshal(images)
metadataJSON, _ := json.Marshal(metadata)
return s.repo.MarkReady(ctx, payload.TenantID, payload.DocumentID, content,
types.JSON(chunksJSON), types.JSON(imagesJSON), types.JSON(metadataJSON),
chunker.ApproxTokenCount(content, lang), len(chunks), time.Now())
}
func (s *temporaryDocumentService) parse(ctx context.Context, document *types.TemporaryDocument) (string, []types.TemporaryDocumentImage, map[string]string, error) {
file, err := s.fileService.GetFile(ctx, document.ResourceRef)
if err != nil {
return "", nil, nil, fmt.Errorf("open source file: %w", err)
}
defer file.Close()
data, err := io.ReadAll(io.LimitReader(file, secutils.GetMaxFileSizeMB()*1024*1024+1))
if err != nil {
return "", nil, nil, fmt.Errorf("read source file: %w", err)
}
ext := document.FileType
var options types.TemporaryDocumentCreateOptions
_ = json.Unmarshal(document.ProcessingOptions, &options)
if options.ParserEngine == "" || options.ParserEngine == "auto" {
if tenant, ok := ctx.Value(types.TenantInfoContextKey).(*types.Tenant); ok && tenant != nil {
options.ParserEngine = tenant.ParserEngineConfig.ResolveChatParserEngine(ext)
}
}
if _, ok := temporaryTextExtensions[ext]; ok && (options.ParserEngine == "" || options.ParserEngine == "auto") {
return string(data), nil, map[string]string{"parser": "plain_text"}, nil
}
if docparser.IsAudioFormat(ext) {
if options.ASRModelID == "" {
return "", nil, nil, fmt.Errorf("audio transcription model is not configured")
}
asrModel, err := s.modelService.GetASRModel(ctx, options.ASRModelID)
if err != nil {
return "", nil, nil, fmt.Errorf("load ASR model: %w", err)
}
result, err := asrModel.Transcribe(ctx, data, document.FileName)
if err != nil {
return "", nil, nil, fmt.Errorf("transcribe audio: %w", err)
}
return result.Text, nil, map[string]string{"parser": "asr"}, nil
}
parserEngine := strings.TrimSpace(options.ParserEngine)
if parserEngine == "auto" {
parserEngine = ""
}
request := &types.ReadRequest{
FileContent: data, FileName: document.FileName, FileType: strings.TrimPrefix(ext, "."),
ParserEngine: parserEngine,
}
if tenant, ok := ctx.Value(types.TenantInfoContextKey).(*types.Tenant); ok && tenant != nil && tenant.ParserEngineConfig != nil {
request.ParserEngineOverrides = tenant.ParserEngineConfig.ToOverridesMap()
}
deps := docparser.ReaderDeps{Overrides: request.ParserEngineOverrides, Remote: s.documentReader}
if s.tenantService != nil {
deps.WeKnoraCloudCredentials = s.tenantService.GetWeKnoraCloudCredentials
}
reader, err := docparser.NewReader(ctx, parserEngine, strings.TrimPrefix(ext, "."), false, deps)
if err != nil {
return "", nil, nil, fmt.Errorf("parse document: %w", err)
}
result, err := reader.Read(ctx, request)
if err != nil {
return "", nil, nil, fmt.Errorf("parse document: %w", err)
}
fix(chunking): 修复内联 HTML 表格与保护模式导致的超长 chunk (#3404) * fix(ocr): normalize inline HTML tables in VLM/OCR sanitizer The VLM/OCR path left raw HTML <table> blocks in the body: looksLikeHTML does not match markdown text with an embedded table, so ocrHTMLToMarkdown (which also lacked the table plugin) never ran. The chunker then split the raw markup mid-row, tripping the 7500-char hard limit and invalidating the heading layer for the whole document. Reuse docparser.NormalizeHTMLTables (already used by PaddleOCR-VL) instead: export it and call it unconditionally from sanitizeOCRText, since it is a safe no-op when no <table> is present. * fix(ocr): keep merged-cell HTML tables chunkable - Narrow span detection to values >1 so rowspan=1/colspan=1 tables still convert to GFM instead of falling back to raw HTML. - On the merged-cell fallback, split each <tr> onto its own line and pad the block with blank lines so the chunker can split by "\n" instead of force-cutting at the absolute max size. * fix(knowledge): normalize inline HTML tables for all parser engines before chunking Previously NormalizeHTMLTables was only wired into the paddleocr-vl converters, so tables emitted by other engines (notably MinerU) reached the chunker as raw single-line <table> blocks and were force-cut at the 7500 char hard limit. Add the normalization at the two central pre-chunk boundaries instead: - knowledge_process.go: after all converter/ASR/image markdown rewrites, before the chunker step (covers every parser engine) - knowledge_create.go: before building the splitter config for manual markdown Converters keep their existing calls (idempotent no-op). * fix(chunker): bound Markdown link/image protected patterns to one line The unbounded [^\]]* / [^)]+ character classes in the Markdown link and image protectedPatterns also matched newlines, so a stray '[' left behind by OCR (e.g. a full-width 【公章】 mis-read as half-width) could pair with a distant '](' and swallow whole paragraphs as one protected atomic span. buildUnitsWithProtection then treated the region as a single unit and mergeUnits could not split it, defeating chunking (observed 1 chunk of 1753 runes where 16 chunks were expected). Exclude newlines and bound the link text (<=200) / destination (<=500), matching CommonMark's rule that neither may span a blank line. Regression: protected_spans_test.go — same input now yields multiple chunks with max 125 runes (chunkSize=128) instead of a single 1753-rune chunk; well-formed single-line links/images stay protected. * fix(ocr): keep non-GFM HTML tables chunkable across all ingest paths Conversion fallbacks for <br>, nested lists, and one-column tables still left single-line HTML, so MinerU output could hit the same oversized-chunk bug. Split those rows, accept one-column GFM, skip fenced examples, and normalize before image resolution so cell images are not escaped. * style: wrap long lines and avoid shadowing max in table-chunk tests Pre-push lll/revive failed on the PR diff versus origin/main. --------- Co-authored-by: neoczr <neoczr@users.noreply.github.com> Co-authored-by: wizardchen <wizardchen@tencent.com>
2026-09-18 13:10:17 +00:00
if result != nil && result.MarkdownContent != "" {
result.MarkdownContent = chunker.NormalizeLineEndings(result.MarkdownContent)
result.MarkdownContent = docparser.NormalizeHTMLTables(result.MarkdownContent)
}
// Capture raw page-image bytes before ResolveAndStore stores/rewrites them,
// so the VLM OCR fallback for scanned documents has bytes to work with.
maxOCRPages := temporaryDocumentImageOCRMaxPages()
if options.OCRMaxPages > 0 {
maxOCRPages = options.OCRMaxPages
}
pageImages := collectImageBytes(result.ImageRefs, maxOCRPages)
images := make([]types.TemporaryDocumentImage, 0)
if s.imageResolver != nil {
updated, stored, resolveErr := s.imageResolver.ResolveAndStore(
ctx, result, temporarySaveFileService{s.fileService}, document.TenantID,
)
if resolveErr != nil {
logger.Warnf(ctx, "temporary document image resolution failed: %v", resolveErr)
} else {
result.MarkdownContent = updated
for _, image := range stored {
images = append(images, types.TemporaryDocumentImage{OriginalRef: image.OriginalRef, URL: image.ServingURL, MimeType: image.MimeType})
if s.resourceCatalog != nil {
feat(agent): preview skill artifacts and verify installs without running them (#2823) * feat(agent): preview skill artifacts and verify installs without running them Generated files open in the chat drawer with overlay/Esc returning to the list, and install verification no longer executes guessed skill entry points. A failed install can be retried from the saved archive. * ui(chat): use a folder icon and corner count for skill artifacts The eye and document glyphs read as preview or copy, and TDesign's loading spinner sat beside the icon. A folder with a top-right count matches the generated-files drawer without covering the toolbar action. * feat(artifact): implement inline artifact references with previews Add support for inline artifact references in Markdown, allowing generated files to be displayed as clickable cards. Implemented a new rendering mechanism for artifacts, enabling previews directly from the chat interface. Updated localization files for new inline preview hints and missing file messages. Introduced tests for artifact reference normalization and rendering logic. * fix(client): document AgentResponseType constants for revive Touching the const block made golangci-lint require comments on every exported value. Match the rest of the SDK and describe artifacts_pending. * fix(agent): wrap artifact-reference prompt lines for lll Pre-push golangci-lint rejected the branch on three lines over 120 characters in the sandbox: reference guidance. * fix(agent): scope HTML scripts and tighten install verification Keep scripted HTML preview on skill artifacts only, recreate sandbox workspace dirs after they are deleted, and treat nested skill files as dependencies rather than first-party imports. * refactor(artifact): enhance artifact reference handling and metadata Updated artifact metadata to include a stable resource handle for better identification. Adjusted artifact reference resolution to support both handle and name forms, ensuring compatibility with existing references. Enhanced tests to validate the new handling logic and ensure proper rendering of artifacts in chat messages. Improved documentation for clarity on artifact reference formats and their usage in the system.
2026-08-27 02:30:32 +00:00
_ = s.resourceCatalog.Bind(ctx, image.ServingURL, types.ResourceOwnerTemporaryDocument, document.ID, types.ResourceRelationExtractedImage)
}
}
}
}
metadata := result.Metadata
if metadata == nil {
metadata = make(map[string]string)
}
if _, exists := metadata["parser"]; !exists {
metadata["parser"] = "document_reader"
if request.ParserEngine != "" {
metadata["parser"] = request.ParserEngine
}
}
content := result.MarkdownContent
if enriched := s.applyImageUnderstanding(ctx, ext, options, data, pageImages, content); enriched != "" {
content = enriched
metadata["image_understanding"] = "vlm"
}
return content, images, metadata, nil
}
// applyImageUnderstanding uses the configured VLM to turn image content into
// text. Standalone image uploads run an OCR-first cascade: OCR is attempted and
// a caption is only generated as a fallback when OCR yields little text (so a
// text-bearing screenshot costs one VLM call, while a diagram/photo falls back
// to a caption). This only kicks in when the parsed content is text-poor, i.e.
// no dedicated OCR engine already ran. Image-only / scanned documents get an
// OCR-only pass, gated by the agent opt-in and the low-text threshold to keep
// latency predictable. Returns the enriched content, or "" to keep the original
// content unchanged.
func (s *temporaryDocumentService) applyImageUnderstanding(
ctx context.Context,
ext string,
options types.TemporaryDocumentCreateOptions,
fileData []byte,
pageImages [][]byte,
content string,
) string {
if options.VLMModelID == "" {
return ""
}
lowText := approxTextContentRunes(content) < temporaryDocumentLowTextRunes
var extracted string
switch {
case docparser.IsImageFormat(ext):
// A dedicated OCR parser engine may already have produced text; only
// fall back to the VLM when the parsed content is text-poor. OCR runs
// first and a caption is generated only if OCR comes back sparse.
if !lowText || len(fileData) == 0 {
return ""
}
extracted = s.understandImagesWithVLM(ctx, options.VLMModelID, [][]byte{fileData}, false, true)
case options.ImageUnderstanding && lowText && len(pageImages) > 0:
extracted = s.understandImagesWithVLM(ctx, options.VLMModelID, pageImages, true, false)
default:
return ""
}
extracted = strings.TrimSpace(extracted)
if extracted == "" {
return ""
}
if strings.TrimSpace(content) == "" {
return extracted
}
return content + "\n\n" + extracted
}
// understandImagesWithVLM runs an OCR-first cascade over the given image bytes
// using the configured VLM. Every page is OCR'd (with bounded concurrency); a
// caption is generated only when captionFallback is set AND the combined OCR
// yield is too sparse to be useful (below temporaryDocumentOCRSufficientRunes).
// This mirrors RAGFlow's OCR>VLM cascade: text-bearing images are served by OCR
// alone in a single round-trip, while diagrams/photos/icons — where OCR finds
// little — get a semantic caption instead. Errors on individual images are
// logged and skipped; the combined extracted text is returned best-effort.
func (s *temporaryDocumentService) understandImagesWithVLM(
ctx context.Context, vlmModelID string, images [][]byte, scanned, captionFallback bool,
) string {
model, err := s.modelService.GetVLMModel(ctx, vlmModelID)
if err != nil {
logger.Warnf(ctx, "temporary document VLM model load failed: %v", err)
return ""
}
ocrPrompt := vlmOCRPrompt
if scanned {
ocrPrompt = vlmOCRScannedPDFPrompt
}
// Page OCR runs with bounded concurrency so multi-page scans don't pay the
// full sequential latency of one VLM round-trip per page. Results are
// collected per index and re-assembled in page order afterwards.
ocrResults := make([]string, len(images))
var wg sync.WaitGroup
sem := make(chan struct{}, temporaryDocumentOCRConcurrency())
acquire := func() { sem <- struct{}{} }
release := func() { <-sem }
for idx, img := range images {
if len(img) == 0 {
continue
}
wg.Add(1)
acquire()
go func(idx int, img []byte) {
defer wg.Done()
defer release()
ocrText, ocrErr := model.Predict(ctx, [][]byte{img}, ocrPrompt)
if ocrErr != nil {
logger.Warnf(ctx, "temporary document VLM OCR failed on image %d: %v", idx, ocrErr)
return
}
ocrResults[idx] = sanitizeOCRText(ocrText)
}(idx, img)
}
wg.Wait()
parts := make([]string, 0, len(images)+1)
ocrRunes := 0
for _, t := range ocrResults {
if t != "" {
parts = append(parts, t)
ocrRunes += len([]rune(t))
}
}
// Caption fallback: only when OCR came back sparse. A single caption over
// the first image is prepended so the semantic description leads the (thin)
// OCR text. Text-rich images skip this entirely, saving a VLM round-trip.
if captionFallback && len(images) > 0 && len(images[0]) > 0 &&
ocrRunes < temporaryDocumentOCRSufficientRunes {
c, capErr := model.Predict(ctx, [][]byte{images[0]}, buildVLMCaptionPrompt(ctx, types.VLMConfig{}))
if capErr != nil {
logger.Warnf(ctx, "temporary document VLM caption failed: %v", capErr)
} else if caption := strings.TrimSpace(c); caption != "" {
parts = append([]string{caption}, parts...)
}
}
return strings.Join(parts, "\n\n")
}
// collectImageBytes gathers inline image bytes from parsed image refs, up to a
// cap, for the VLM OCR fallback. Refs without inline data are skipped.
func collectImageBytes(refs []types.ImageRef, limit int) [][]byte {
if limit <= 0 {
return nil
}
out := make([][]byte, 0, limit)
for _, ref := range refs {
if len(ref.ImageData) == 0 {
continue
}
out = append(out, ref.ImageData)
if len(out) >= limit {
break
}
}
return out
}
// approxTextContentRunes counts the runes of real text in markdown, ignoring
// image references, so scanned/image-only documents register as low-text.
func approxTextContentRunes(md string) int {
stripped := markdownImagePattern.ReplaceAllString(md, "")
return len([]rune(strings.TrimSpace(stripped)))
}
func (s *temporaryDocumentService) ResolveForPrompt(ctx context.Context, tenantID uint64, sessionID string, documentIDs []string, query string) (*types.TemporaryDocumentPromptResult, error) {
result := &types.TemporaryDocumentPromptResult{}
if len(documentIDs) > types.MaxTemporaryAttachmentsPerMessage {
return nil, fmt.Errorf("a message can use at most %d attachments", types.MaxTemporaryAttachmentsPerMessage)
}
perDocumentBudget := temporaryDocumentPromptBudget
if len(documentIDs) > 0 {
perDocumentBudget = temporaryDocumentPromptBudget / len(documentIDs)
}
seen := make(map[string]struct{}, len(documentIDs))
for _, documentID := range documentIDs {
if _, duplicate := seen[documentID]; duplicate {
continue
}
seen[documentID] = struct{}{}
feat(session): fork conversations from a historical user message (#3304) * feat(session): add fork schema and history queries 为会话分叉增加数据模型与迁移,并提供截至分叉点的消息查询以及会话内的 request_id 配对。 * feat(sandbox): add git, fork snapshots, and bootstrap hook 沙箱镜像安装 git,Docker 快照使用独立的 fork 命名空间,并在首次创建会话沙箱时接入可选 bootstrap。 * feat(session): checkpoint workspace at the end of each turn 每轮结束后对沙箱 /workspace 打 git checkpoint,并避免 fork 后因 mtime 刷新重复挂上产物、以及附件仍绑在父会话而无法还原。 * feat(session): fork conversations and reap orphan snapshots 支持从历史 user 消息分叉会话、首次打开时从快照 bootstrap,并在失败或删除未打开的分叉时回收孤儿快照。 * feat(chat): add session fork entry, prefill, and badges 在用户消息上提供分叉入口,进入新会话时预填原问题,并展示降级提示与来源角标。 * style: add missing newline so session_fork_delete_test.go is gofmt-clean * fix(session): prune later git history when rolling a fork workspace back reset --hard 只还原工作区,分叉点之后的 commit 仍可被 checkout。校验 SHA 后丢掉其他 ref 并 gc,避免错误基线。 * style(session): clear golangci-lint findings on the session-fork diff Pre-push lints the whole PR against main; wrap long lines, check c.Error, and fix gofumpt/revive/staticcheck so the merge can be pushed. * fix(chat): show the fork button on hover without shifting the thread Overlay the action so revealing it no longer expands the user bubble and janks the messages below. * fix(chat): drop the fork landing banners Keep the original question prefilled; do not explain sandbox snapshot vs checkpoint on the new thread. * fix(agent): dedupe artifacts by content hash, not size Same-path same-size rewrites were skipped as restores. Keep path+mtime as the cheap skip, hash bytes when mtime moves, and stamp the restored mtime onto this session only. * feat(session): allow forking from an assistant answer User forks still copy history before the question and prefill it. Assistant forks copy through that reply and reuse its checkpoint, with a degrade tooltip when none exists. * fix(session): resolve parent attachments from a forked chat Fork copies attachment IDs onto the new messages but leaves temporary_documents on the parent. Preview and Get now look up those IDs on the fork's copied history instead of a scoped miss. * fix(agent): rewrite sandbox refs after a content-hash skip Fork restores change mtime on unchanged files, so Collect returns empty and named citations were left as sandbox: paths. Resolve those names against session artifacts, notify only real uploads, and stamp restored mtime only on the matching content hash. * fix(session): drop a dead fork snapshot and retry checkpoint rewrite Create from a missing template now abandons bootstrap so the next Resolve uses a normal sandbox. A failed checkpoint rewrite no longer marks the bootstrap consumed, which previously made nested forks SANDBOX_REPLACED. * fix(session): load forked parent attachments into the prompt ResolveForPrompt used session-scoped lookup, so copied parent attachment IDs passed Get but never reached the model. Use the same resolveInSession path as preview. * fix(chat): keep the user-message fork button visible The control was hover-only, which hid it on touch. Always show it, and drop the unused degrade tooltip. --------- Co-authored-by: hailongzhao <hailongzhao@tencent.com> Co-authored-by: wizardchen <wizardchen@tencent.com>
2026-09-17 06:20:18 +00:00
document, err := s.resolveInSession(ctx, tenantID, sessionID, documentID)
if err != nil {
return nil, err
}
if document == nil {
return nil, fmt.Errorf("attachment %s was not found in this session", documentID)
}
if document.Status != types.TemporaryDocumentStatusReady {
if document.Status == types.TemporaryDocumentStatusFailed {
return nil, fmt.Errorf("attachment %s failed to parse: %s", document.FileName, document.ErrorMessage)
}
return nil, fmt.Errorf("attachment %s is still being processed", document.FileName)
}
content, selected, total := selectTemporaryDocumentContentWithBudget(document, query, perDocumentBudget)
result.Attachments = append(result.Attachments, types.MessageAttachment{
ID: document.ID, URL: document.ResourceRef, FileName: document.FileName,
FileType: document.FileType, FileSize: document.FileSize, Content: content,
ContentMode: map[bool]string{true: "full", false: "selected_chunks"}[selected == total],
TokenCount: document.TokenCount, SelectedChunks: selected, TotalChunks: total,
})
// Image-type attachments always expose their image so vision models can
// see it directly; text documents only attach extracted images when the
// question is visual, to avoid gratuitous multimodal latency.
if docparser.IsImageFormat(document.FileType) || isVisualDocumentQuery(query) {
for _, image := range temporaryDocumentImageRefs(document.ImageRefs) {
if image.URL != "" && len(result.ImageURLs) < 4 {
result.ImageURLs = append(result.ImageURLs, image.URL)
}
}
}
}
return result, nil
}
func selectTemporaryDocumentContent(document *types.TemporaryDocument, query string) (string, int, int) {
return selectTemporaryDocumentContentWithBudget(document, query, temporaryDocumentPromptBudget)
}
func selectTemporaryDocumentContentWithBudget(document *types.TemporaryDocument, query string, budget int) (string, int, int) {
var chunks []types.TemporaryDocumentChunk
_ = json.Unmarshal(document.Chunks, &chunks)
if budget <= 0 {
budget = temporaryDocumentPromptBudget
}
if len(chunks) == 0 || (document.TokenCount <= temporaryDocumentInlineTokens && document.TokenCount <= budget) {
return document.Content, len(chunks), len(chunks)
}
terms := temporaryDocumentQueryTerms(query)
type rankedChunk struct {
chunk types.TemporaryDocumentChunk
score int
}
ranked := make([]rankedChunk, 0, len(chunks))
for _, part := range chunks {
text := strings.ToLower(part.ContextHeader + "\n" + part.Content)
score := 0
for _, term := range terms {
score += strings.Count(text, term) * (1 + len([]rune(term))/2)
}
ranked = append(ranked, rankedChunk{chunk: part, score: score})
}
sort.SliceStable(ranked, func(i, j int) bool {
if ranked[i].score == ranked[j].score {
return ranked[i].chunk.Seq < ranked[j].chunk.Seq
}
return ranked[i].score > ranked[j].score
})
selected := make([]types.TemporaryDocumentChunk, 0, temporaryDocumentMaxPromptParts)
tokens := 0
for _, candidate := range ranked {
if len(selected) >= temporaryDocumentMaxPromptParts {
break
}
if tokens > 0 && tokens+candidate.chunk.TokenCount > budget {
continue
}
selected = append(selected, candidate.chunk)
tokens += candidate.chunk.TokenCount
}
sort.Slice(selected, func(i, j int) bool { return selected[i].Seq < selected[j].Seq })
var builder strings.Builder
for _, part := range selected {
if builder.Len() > 0 {
builder.WriteString("\n\n---\n\n")
}
if part.ContextHeader != "" {
builder.WriteString(part.ContextHeader)
builder.WriteString("\n\n")
}
builder.WriteString(strings.TrimSpace(part.Content))
}
return builder.String(), len(selected), len(chunks)
}
// temporarySaveFileService makes images extracted from an expiring chat
// document use temporary storage without changing the shared ImageResolver API.
// Embedded interface methods forward to the original service.
type temporarySaveFileService struct{ interfaces.FileService }
func (s temporarySaveFileService) SaveBytes(ctx context.Context, data []byte, tenantID uint64, fileName string, _ bool) (string, error) {
return s.FileService.SaveBytes(ctx, data, tenantID, fileName, true)
}
func temporaryDocumentQueryTerms(query string) []string {
query = strings.ToLower(strings.TrimSpace(query))
seen := make(map[string]struct{})
var terms []string
for _, field := range strings.FieldsFunc(query, func(r rune) bool { return unicode.IsSpace(r) || unicode.IsPunct(r) }) {
if len([]rune(field)) < 2 {
continue
}
if _, ok := seen[field]; !ok {
seen[field] = struct{}{}
terms = append(terms, field)
}
}
runes := []rune(query)
for i := 0; i+1 < len(runes); i++ {
if unicode.Is(unicode.Han, runes[i]) && unicode.Is(unicode.Han, runes[i+1]) {
term := string(runes[i : i+2])
if _, ok := seen[term]; !ok {
seen[term] = struct{}{}
terms = append(terms, term)
}
}
}
return terms
}
func isVisualDocumentQuery(query string) bool {
lower := strings.ToLower(query)
for _, marker := range []string{"图", "表格", "截图", "页面", "排版", "chart", "figure", "diagram", "image", "layout"} {
if strings.Contains(lower, marker) {
return true
}
}
return false
}
func temporaryDocumentImageRefs(raw types.JSON) []types.TemporaryDocumentImage {
var images []types.TemporaryDocumentImage
_ = json.Unmarshal(raw, &images)
return images
}
func (s *temporaryDocumentService) CleanupExpired(ctx context.Context) error {
for {
documents, err := s.repo.ListExpired(ctx, time.Now(), 100)
if err != nil {
return err
}
if len(documents) == 0 {
return nil
}
for _, document := range documents {
if err := s.Delete(ctx, document.TenantID, document.SessionID, document.ID); err != nil {
logger.Warnf(ctx, "cleanup temporary document failed: document_id=%s err=%v", document.ID, err)
}
}
if len(documents) < 100 {
return nil
}
}
}