fix: improve ai table file handling

This commit is contained in:
richjohnrambo
2026-09-30 10:02:00 +08:00
parent 1bcce3cdde
commit 47a10c93be
5 changed files with 334 additions and 15 deletions
+26 -2
View File
@@ -379,7 +379,7 @@ func (a *API) aiTableAnalysisCreate(w http.ResponseWriter, r *http.Request, p *a
writeStoreError(w, r, err)
return
}
profile := tableProfileForAI(resolveTableFilePath(fileID, fileMeta.Location), fileType)
profile := a.tableProfileForAI(r, fileID, fileMeta, fileType)
result, err := a.ai.AnalyzeTable(r.Context(), ai.TableAnalysisInput{
Topic: topic, FileID: fileID, FileName: strings.TrimSpace(body.FileName), FileType: fileType, FileSize: fileSize, Profile: profile,
})
@@ -831,10 +831,15 @@ func aiRecordPayloads(records []store.AIRecord) []map[string]any {
func aiTableRecordPayloads(records []store.AIRecord) []map[string]any {
items := make([]map[string]any, 0, len(records))
for _, record := range records {
fileType := strings.ToLower(strings.TrimSpace(record.FileType))
items = append(items, map[string]any{
"record_id": record.RecordID,
"title": record.Title,
"summary": record.Summary,
"format": fileType,
"file_type": fileType,
"file_name": record.FileName,
"file_size": record.FileSize,
"status": record.Status,
"created_at": record.CreatedAt.UTC().Format(time.RFC3339Nano),
"updated_at": record.UpdatedAt.UTC().Format(time.RFC3339Nano),
@@ -976,7 +981,26 @@ func resolveTableFilePath(fileID, location string) string {
return ""
}
func tableProfileForAI(path, fileType string) ai.TableProfile {
func (a *API) tableProfileForAI(r *http.Request, fileID string, meta store.AIFileMetadata, fileType string) ai.TableProfile {
if path := resolveTableFilePath(fileID, meta.Location); path != "" {
return tableProfileFromPath(path, fileType)
}
if path, cleanup, err := downloadWorkbenchFileToTemp(r, fileID, fileType); err == nil {
defer cleanup()
return tableProfileFromPath(path, fileType)
}
if strings.TrimSpace(meta.Location) == "" {
return ai.TableProfile{Parsed: false, Message: "source file metadata is not available"}
}
path, cleanup, err := a.downloadOSSObjectToTemp(r.Context(), meta.Location, fileType)
if err != nil {
return ai.TableProfile{Parsed: false, Message: "source file is not available: " + err.Error()}
}
defer cleanup()
return tableProfileFromPath(path, fileType)
}
func tableProfileFromPath(path, fileType string) ai.TableProfile {
if strings.TrimSpace(path) == "" {
return ai.TableProfile{Parsed: false, Message: "source file is not available locally"}
}