fix(agent): materialize knowledge files to temp path for DuckDB (#1007)

Dev-mode repro from the issue reporter's logs:

  [Tool][DataAnalysis] Failed to create table from Excel: IO Error:
  GDAL Error (4): Failed to open file
  'local://10000/.../1777030910246871000.xlsx': No such file or directory

The local file service returns a custom 'local://' URL from GetFileURL,
which the duckdb spatial/excel extensions can't resolve (they expect
plain paths or http(s)/s3:// style URIs). Presigned HTTPS URLs from
cloud backends worked by accident; the 'local://' dev path has always
been broken for the Data Analysis tool.

Instead of per-scheme adapters, stream the file through
FileService.GetFile into a temp file and hand DuckDB the resulting
filesystem path. This works uniformly across every backend (local,
OSS, S3, MinIO, COS) and survives future scheme changes without any
changes to the Data Analysis tool.

- Preserve the original file extension on the temp file so DuckDB's
  format auto-detection (csv / xlsx / xls) still kicks in.
- Clean up the temp file when LoadFromKnowledge returns, including on
  every error branch. Cleanup is idempotent (double-invoke safe).
- New tests stub FileService to lock in that (a) we never leak a
  provider:// URL to DuckDB, (b) GetFile errors propagate, (c) the
  extension survives case normalization.

Refs: https://github.com/Tencent/WeKnora/issues/1007
This commit is contained in:
wizardchen
2026-04-24 19:49:11 +08:00
committed by lyingbug
parent 002d98a640
commit 8103398fcf
2 changed files with 229 additions and 6 deletions
+68 -6
View File
@@ -5,6 +5,8 @@ import (
"database/sql"
"encoding/json"
"fmt"
"io"
"os"
"strings"
"github.com/Tencent/WeKnora/internal/logger"
@@ -439,8 +441,14 @@ func buildExcelCreateTableSQL(tableName, filename string, sheetNames []string) s
)
}
// LoadFromKnowledge loads data from a Knowledge entity into a DuckDB table and returns the table schema
// It automatically determines the file type and calls the appropriate loading method
// LoadFromKnowledge loads data from a Knowledge entity into a DuckDB table and returns the table schema.
// It automatically determines the file type and calls the appropriate loading method.
//
// The source file is first materialized to a local temp file via FileService.GetFile
// so DuckDB's st_read / read_xlsx / read_csv_auto can open it directly. This
// side-steps provider-specific URL schemes (e.g. the local:// URL returned by
// the local file service) that DuckDB's extensions cannot resolve on their own.
//
// Parameters:
// - ctx: context for cancellation and timeout
// - knowledge: the Knowledge entity containing file information
@@ -460,16 +468,17 @@ func (t *DataAnalysisTool) LoadFromKnowledge(ctx context.Context, knowledge *typ
logger.Infof(ctx, "[Tool][DataAnalysis] Loading knowledge '%s' (type: %s) into table '%s' for session %s",
knowledge.ID, fileType, tableName, t.sessionID)
fileURL, err := t.fileService.GetFileURL(ctx, knowledge.FilePath)
localPath, cleanup, err := t.materializeKnowledgeFile(ctx, knowledge)
if err != nil {
return nil, fmt.Errorf("failed to get file URL for knowledge '%s': %w", knowledge.ID, err)
return nil, fmt.Errorf("failed to materialize knowledge '%s' for DuckDB: %w", knowledge.ID, err)
}
defer cleanup()
switch fileType {
case "csv":
return t.LoadFromCSV(ctx, fileURL, tableName)
return t.LoadFromCSV(ctx, localPath, tableName)
case "xlsx", "xls":
return t.LoadFromExcel(ctx, fileURL, tableName)
return t.LoadFromExcel(ctx, localPath, tableName)
default:
logger.Warnf(ctx, "[Tool][DataAnalysis] Unsupported file type '%s' for knowledge '%s' in session %s",
fileType, knowledge.ID, t.sessionID)
@@ -477,6 +486,59 @@ func (t *DataAnalysisTool) LoadFromKnowledge(ctx context.Context, knowledge *typ
}
}
// materializeKnowledgeFile copies the knowledge's backing blob into a fresh
// temp file on the local filesystem so DuckDB can open it with ordinary path
// semantics. It returns the temp path and a cleanup closure that removes the
// temp file; the closure is always safe to call and is a no-op on failure.
//
// This hides storage-backend-specific URL schemes (local://, oss://, s3://,
// minio://, cos://, …) behind the FileService.GetFile abstraction, so the
// Data Analysis tool works identically across all deployments.
func (t *DataAnalysisTool) materializeKnowledgeFile(ctx context.Context, knowledge *types.Knowledge) (string, func(), error) {
noop := func() {}
reader, err := t.fileService.GetFile(ctx, knowledge.FilePath)
if err != nil {
return "", noop, fmt.Errorf("failed to open file for knowledge '%s': %w", knowledge.ID, err)
}
defer reader.Close()
// Preserve the file extension so DuckDB's format auto-detection still
// works (e.g. the CSV reader expects .csv, xlsx reader expects .xlsx).
suffix := ""
if ext := strings.ToLower(strings.TrimSpace(knowledge.FileType)); ext != "" {
suffix = "." + ext
}
tmp, err := os.CreateTemp("", "weknora-data-analysis-*"+suffix)
if err != nil {
return "", noop, fmt.Errorf("failed to create temp file: %w", err)
}
tmpPath := tmp.Name()
cleanup := func() {
// Best-effort cleanup; a missing file is fine, any other error is
// only logged to avoid masking the original operation's result.
if err := os.Remove(tmpPath); err != nil && !os.IsNotExist(err) {
logger.Warnf(ctx, "[Tool][DataAnalysis] Failed to remove temp file %s: %v", tmpPath, err)
}
}
if _, err := io.Copy(tmp, reader); err != nil {
_ = tmp.Close()
cleanup()
return "", noop, fmt.Errorf("failed to copy knowledge '%s' to temp file: %w", knowledge.ID, err)
}
if err := tmp.Close(); err != nil {
cleanup()
return "", noop, fmt.Errorf("failed to finalize temp file for knowledge '%s': %w", knowledge.ID, err)
}
logger.Infof(ctx, "[Tool][DataAnalysis] Materialized knowledge '%s' to temp file %s for session %s",
knowledge.ID, tmpPath, t.sessionID)
return tmpPath, cleanup, nil
}
// LoadFromKnowledgeID loads data from a Knowledge ID into a DuckDB table and returns the table schema
// Parameters:
// - ctx: context for cancellation and timeout