Files
runny 9058149576
Build & Publish (dsh-academy) / End-to-end inside dsh (latest, advisory) (push) Skipped
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Frontend / Docker multi-stage image (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-academy) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-academy) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-academy) / Publish to npm (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Helm / chart (push) Canceled after 0s
refactor: 项目更名 WeKnora → Academy
- 全量替换代码/配置/文档中的项目名(保留上游 GitHub 链接、weixin 文档域名、Docker Hub 镜像名)
- 重命名 32 个含项目名的文件/目录;Go module 路径改为 academy(含 client/cli 子模块)
- docreader proto 描述符采用等长替换,保持序列化字节布局有效
- Dockerfile.app 构建网络修复: browserskill 离线产物通道、PyPI/apt 国内镜像
- 运行验证: go build ✓ docker compose up ✓ 健康检查 ✓
2026-11-13 20:52:51 +08:00

147 lines
5.0 KiB
Go

package service
import (
"context"
"fmt"
"io"
"os"
"strconv"
"strings"
"time"
"academy/internal/agent/tools"
"academy/internal/application/attachment"
"academy/internal/application/repository"
"academy/internal/logger"
"academy/internal/types"
"academy/internal/types/interfaces"
)
const defaultAgentAttachmentParseWait = 60 * time.Second
// agentSessionAttachments is attachment://'s view of one session. Documents
// are looked up by tenant and ID only after the application reader matched them against
// the session's own message attachments, so a forked session can still read
// files whose temporary_documents row stays on the parent.
type agentSessionAttachments struct {
messages interfaces.MessageRepository
documents interfaces.TemporaryDocumentRepository
files interfaces.FileService
tenantID uint64
sessionID string
}
func (a *agentSessionAttachments) SessionAttachments(ctx context.Context) (types.MessageAttachments, error) {
return a.messages.GetSessionAttachments(ctx, a.sessionID)
}
func (a *agentSessionAttachments) TemporaryDocument(
ctx context.Context, documentID string,
) (*types.TemporaryDocument, error) {
return a.documents.GetByID(ctx, a.tenantID, documentID)
}
func (a *agentSessionAttachments) ReadImage(ctx context.Context, ref string) ([]byte, error) {
if a.files == nil {
return nil, fmt.Errorf("attachment image storage is unavailable")
}
// Shared agents execute with their source tenant in the model context;
// attachment storage belongs to the conversation owner instead.
ctx = context.WithValue(ctx, types.TenantIDContextKey, a.tenantID)
file, err := a.files.GetFile(ctx, ref)
if err != nil {
return nil, err
}
defer func() { _ = file.Close() }()
const maxAttachmentImageBytes = 32 << 20
data, err := io.ReadAll(io.LimitReader(file, maxAttachmentImageBytes+1))
if err != nil {
return nil, err
}
if len(data) > maxAttachmentImageBytes {
return nil, fmt.Errorf("attachment image exceeds %d bytes", maxAttachmentImageBytes)
}
return data, nil
}
// agentAttachmentParseWait honours the agent's own timeout, then
// ACADEMY_CHAT_ATTACHMENT_WAIT_TIMEOUT_SEC, then the default.
func agentAttachmentParseWait(config *types.AgentConfig) time.Duration {
if config != nil && config.AttachmentParseWaitTimeoutSec > 0 {
return time.Duration(config.AttachmentParseWaitTimeoutSec) * time.Second
}
if raw := strings.TrimSpace(os.Getenv("ACADEMY_CHAT_ATTACHMENT_WAIT_TIMEOUT_SEC")); raw != "" {
if sec, err := strconv.Atoi(raw); err == nil && sec > 0 {
return time.Duration(sec) * time.Second
}
}
return defaultAgentAttachmentParseWait
}
// registerAttachmentReader adds attachment:// to read_file when the session
// has at least one stored attachment. It follows the session's content rather
// than the tool allowlist: a file the user sent must stay readable.
func (s *agentService) registerAttachmentReader(
ctx context.Context, registry *tools.ToolRegistry, config *types.AgentConfig, sessionID string,
) *attachment.Reader {
if s.db == nil || sessionID == "" || config.SkillInstallMode() {
return nil
}
// Attachments belong to the session owner, which differs from the agent's
// tenant when a shared agent runs in someone else's workspace.
tenantID, ok := types.SandboxTenantIDFromContext(ctx)
if !ok || tenantID == 0 {
return nil
}
store := &agentSessionAttachments{
messages: repository.NewMessageRepository(s.db),
documents: repository.NewTemporaryDocumentRepository(s.db),
files: s.fileService,
tenantID: tenantID,
sessionID: sessionID,
}
attachments, err := store.SessionAttachments(ctx)
if err != nil {
logger.Warnf(ctx, "attachment:// not offered: load session attachments: %v", err)
return nil
}
stored := false
for _, attachment := range attachments {
if strings.TrimSpace(attachment.ID) != "" {
stored = true
break
}
}
if !stored {
return nil
}
var reader *tools.ReadFileTool
if existing, err := registry.GetTool(tools.ToolReadFile); err == nil {
if reader, _ = existing.(*tools.ReadFileTool); reader == nil {
logger.Warnf(ctx, "Cannot attach conversation attachments: read_file is registered by another tool")
return nil
}
} else {
reader = tools.NewReadFileTool(nil)
registry.RegisterTool(reader)
}
source := attachment.NewReader(store, agentAttachmentParseWait(config), store.ReadImage)
if len(config.AttachmentSandboxCopies) > 0 {
ownerCtx := context.WithValue(ctx, types.TenantIDContextKey, tenantID)
inputStore, err := s.sessionSandboxInputStore(ownerCtx, sessionID, config.SandboxConfigID)
if err != nil {
logger.Warnf(ctx, "Attachment sandbox sync unavailable: %v", err)
} else if inputStore != nil {
originals := make(map[string]string, len(config.AttachmentSandboxCopies))
for original, address := range config.AttachmentSandboxCopies {
originals[address] = original
}
source.Projection = &attachmentSandboxProjection{
store: inputStore, sessionID: sessionID, originals: originals,
}
}
}
reader.WithAttachments(source).WithAttachmentCopies(config.AttachmentSandboxCopies)
return source
}