Skip to content

Commit

Permalink
Feat: Add file tree, add include and exclude prefix for auto ingestion
Browse files Browse the repository at this point in the history
Signed-off-by: Daishan Peng <[email protected]>
  • Loading branch information
StrongMonkey committed Nov 20, 2024
1 parent fd072b4 commit 58ed35e
Show file tree
Hide file tree
Showing 9 changed files with 681 additions and 590 deletions.
10 changes: 7 additions & 3 deletions apiclient/types/knowledgesource.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@ type KnowledgeSource struct {
LastSyncStartTime *Time `json:"lastSyncStartTime,omitempty"`
LastSyncEndTime *Time `json:"lastSyncEndTime,omitempty"`
LastRunID string `json:"lastRunID,omitempty"`
FilePathPrefixInclude []string `json:"filePathPrefixInclude,omitempty"`
FilePathPrefixExclude []string `json:"filePathPrefixExclude,omitempty"`
}

type KnowledgeSourceManifest struct {
SyncSchedule string `json:"syncSchedule,omitempty"`
AutoApprove *bool `json:"autoApprove,omitempty"`
KnowledgeSourceInput `json:",inline"`
SyncSchedule string `json:"syncSchedule,omitempty"`
AutoApprove *bool `json:"autoApprove,omitempty"`
FilePathPrefixInclude []string `json:"filePathPrefixInclude,omitempty"`
FilePathPrefixExclude []string `json:"filePathPrefixExclude,omitempty"`
KnowledgeSourceInput `json:",inline"`
}

type KnowledgeSourceList List[KnowledgeSource]
Expand Down
2 changes: 2 additions & 0 deletions pkg/api/handlers/knowledgesource.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ func convertKnowledgeSource(agentName string, knowledgeSource v1.KnowledgeSource
LastSyncStartTime: types.NewTime(knowledgeSource.Status.LastSyncStartTime.Time),
LastSyncEndTime: types.NewTime(knowledgeSource.Status.LastSyncEndTime.Time),
LastRunID: knowledgeSource.Status.RunName,
FilePathPrefixExclude: knowledgeSource.Spec.Manifest.FilePathPrefixExclude,
FilePathPrefixInclude: knowledgeSource.Spec.Manifest.FilePathPrefixInclude,
}
}

Expand Down
54 changes: 44 additions & 10 deletions pkg/controller/handlers/knowledgefile/knowledgefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,30 @@ func (h *Handler) IngestFile(req router.Request, _ router.Response) error {
}
}

// Check approval
matchInclude := isFileMatchIncludePattern(file.Spec.FileName, source.Spec.Manifest.FilePathPrefixInclude)
matchExclude := isFileMatchExcludePattern(file.Spec.FileName, source.Spec.Manifest.FilePathPrefixExclude)
if file.Spec.Approved == nil {
if source.Spec.Manifest.AutoApprove != nil && *source.Spec.Manifest.AutoApprove {
file.Spec.Approved = typed.Pointer(true)
if err := req.Client.Update(req.Ctx, file); err != nil {
return err
}
}

if matchInclude && !matchExclude {
file.Spec.Approved = typed.Pointer(true)
if err := req.Client.Update(req.Ctx, file); err != nil {
return err
}
} else if matchExclude {
file.Spec.Approved = typed.Pointer(false)
if err := req.Client.Update(req.Ctx, file); err != nil {
return err
}
}
}

if file.Status.State.IsTerminal() && !shouldReIngest(file) {
return nil
}
Expand All @@ -111,16 +135,6 @@ func (h *Handler) IngestFile(req router.Request, _ router.Response) error {
}
}

// Check approval
if file.Spec.Approved == nil {
if source.Spec.Manifest.AutoApprove != nil && *source.Spec.Manifest.AutoApprove {
file.Spec.Approved = typed.Pointer(true)
if err := req.Client.Update(req.Ctx, file); err != nil {
return err
}
}
}

if file.Spec.Approved == nil || !*file.Spec.Approved {
// Not approved, wait for user action
return nil
Expand Down Expand Up @@ -402,3 +416,23 @@ func (h *Handler) Cleanup(req router.Request, _ router.Response) error {
}
return nil
}

func isFileMatchIncludePattern(filePath string, filePathPrefixInclude []string) bool {
for _, include := range filePathPrefixInclude {
if strings.HasPrefix(filePath, include) {
return true
}
}

return false
}

func isFileMatchExcludePattern(filePath string, filePathPrefixExclude []string) bool {
for _, exclude := range filePathPrefixExclude {
if strings.HasPrefix(filePath, exclude) {
return true
}
}

return false
}
Loading

0 comments on commit 58ed35e

Please sign in to comment.