attachment

package
v0.0.80 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 20, 2026 License: MPL-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	FileFilterAllFiles = FileFilter{
		DisplayName: "All Files",
		Extensions:  nil,
	}

	FileFilterTextMarkdown = FileFilter{
		DisplayName: "Text",
		Extensions:  ModeToExtensions[ExtensionModeText],
	}

	FileFilterDocuments = FileFilter{
		DisplayName: "Documents",
		Extensions:  ModeToExtensions[ExtensionModeDocument],
	}

	FileFilterImages = FileFilter{
		DisplayName: "Images",
		Extensions:  ModeToExtensions[ExtensionModeImage],
	}

	DefaultFileFilters = []FileFilter{
		FileFilterAllFiles,
		FileFilterTextMarkdown,
		FileFilterDocuments,
		FileFilterImages,
	}
)
View Source
var (
	ErrNonTextContentBlock             = errors.New("content block is not of kind - text")
	ErrUnreadableFile                  = errors.New("unreadable file")
	ErrExistingContentBlock            = errors.New("content block already exists")
	ErrAttachmentModifiedSinceSnapshot = errors.New("attachment modified since snapshot")

	// ErrResponseTooLarge is used internally by fetchURLBytes to signal
	// that the remote resource exceeded the configured maxBytes limit.
	ErrResponseTooLarge = errors.New("remote resource larger than the configured limit")

	// ErrPageTooLarge is returned by ExtractReadableMarkdownFromURL when
	// the HTML page exceeds maxURLPageContentBytes.
	ErrPageTooLarge = errors.New("page content too large")

	// ErrNoContentExtracted is returned when the extractor produces no
	// readable content at all (e.g., empty body, or extraction failure).
	ErrNoContentExtracted = errors.New("no readable content extracted")
)
View Source
var ExtensionToMIMEType = map[FileExt]MIMEType{
	ExtTxt:      MIMETextPlain,
	ExtMd:       MIMETextMarkdown,
	ExtMarkdown: MIMETextMarkdown,
	ExtMdx:      MIMETextMarkdown,
	ExtLog:      MIMETextPlain,
	ExtJSON:     MIMEApplicationJSON,
	ExtYAML:     MIMEApplicationYAML,
	ExtYML:      MIMEApplicationYAML,
	ExtTOML:     MIMEApplicationTOML,
	ExtJS:       MIMEApplicationJS,
	ExtTS:       MIMETextPlain,
	ExtTSX:      MIMETextPlain,
	ExtJSX:      MIMETextPlain,
	ExtPY:       MIMETextPlain,
	ExtGO:       MIMETextPlain,
	ExtRS:       MIMETextPlain,
	ExtJAVA:     MIMETextPlain,
	ExtC:        MIMETextPlain,
	ExtCPP:      MIMETextPlain,
	ExtH:        MIMETextPlain,
	ExtHPP:      MIMETextPlain,
	ExtCS:       MIMETextPlain,
	ExtRB:       MIMETextPlain,
	ExtPHP:      MIMETextPlain,
	ExtHTML:     MIMETextHTML,
	ExtHTM:      MIMETextHTML,
	ExtCSS:      MIMETextCSS,
	ExtSCSS:     MIMETextPlain,
	ExtLESS:     MIMETextPlain,
	ExtSQL:      MIMEApplicationSQL,
	ExtMod:      MIMETextPlain,
	ExtSum:      MIMETextPlain,
	ExtJSONL:    MIMETextPlain,
	ExtShell:    MIMETextPlain,
	ExtSWIFT:    MIMETextPlain,
	ExtM:        MIMETextPlain,
	ExtKT:       MIMETextPlain,
	ExtPL:       MIMETextPlain,
	ExtSCALA:    MIMETextPlain,
	ExtHS:       MIMETextPlain,
	ExtLUA:      MIMETextPlain,
	ExtDART:     MIMETextPlain,
	ExtCmake:    MIMETextPlain,
	ExtBazel:    MIMETextPlain,

	ExtJPG:  MIMEImageJPEG,
	ExtJPEG: MIMEImageJPEG,
	ExtPNG:  MIMEImagePNG,
	ExtGIF:  MIMEImageGIF,
	ExtWEBP: MIMEImageWEBP,
	ExtBMP:  MIMEImageBMP,
	ExtSVG:  MIMEImageSVG,

	ExtPDF:  MIMEApplicationPDF,
	ExtDOC:  MIMEApplicationMSWord,
	ExtDOCX: MIMEApplicationOpenXMLDoc,
	ExtPPT:  MIMEApplicationMSPowerPt,
	ExtPPTX: MIMEApplicationOpenXMLPPT,
	ExtXLS:  MIMEApplicationMSExcel,
	ExtXLSX: MIMEApplicationOpenXMLXLS,
	ExtODT:  MIMEApplicationODT,
	ExtODS:  MIMEApplicationODS,
}
View Source
var ModeToExtensions = func() map[ExtensionMode][]FileExt {
	m := make(map[ExtensionMode][]FileExt, len(AllExtensionModes))

	for ext, mimeType := range ExtensionToMIMEType {
		if mode, ok := MIMETypeToExtensionMode[mimeType]; ok {
			m[mode] = append(m[mode], ext)
		} else {
			m[ExtensionModeDefault] = append(m[ExtensionModeDefault], ext)
		}
	}
	return m
}()

Functions

func ExtractReadableMarkdownFromURL

func ExtractReadableMarkdownFromURL(ctx context.Context, rawURL string) (string, error)

ExtractReadableMarkdownFromURL downloads a web page, runs it through go-trafilatura to obtain the main readable content, converts that to Markdown, and returns the Markdown text.

It enforces a hard size limit (maxURLPageContentBytes). If the remote server is larger than that, ErrPageTooLarge is returned. Only the HTML of the page itself is downloaded; sub-resources (images, scripts, etc.) are not fetched.

This function is conservative: it only attempts extraction on content types that look like HTML or generic text.

func FormatTextBlockForLLM added in v0.0.80

func FormatTextBlockForLLM(b ContentBlock) (string, error)

Types

type Attachment

type Attachment struct {
	Kind  AttachmentKind `json:"kind"`
	Label string         `json:"label"`

	// Mode selected for this attachment for the current turn.
	// Frontend picks smart defaults; backend can override missing values.
	Mode AttachmentContentBlockMode `json:"mode,omitempty"`
	// Optional: allowed modes for this attachment (primarily for UI).
	AvailableContentBlockModes []AttachmentContentBlockMode `json:"availableContentBlockModes,omitempty"`

	// Exactly one field below should be non-nil.
	FileRef    *FileRef    `json:"fileRef,omitempty"`
	ImageRef   *ImageRef   `json:"imageRef,omitempty"`
	URLRef     *URLRef     `json:"urlRef,omitempty"`
	GenericRef *GenericRef `json:"genericRef,omitempty"`

	ContentBlock *ContentBlock `json:"contentBlock,omitempty"`
}

Attachment is a lightweight reference to external context (files, docs, images, etc.).

func BuildAttachmentForFile

func BuildAttachmentForFile(ctx context.Context, pathInfo *PathInfo) (*Attachment, error)

BuildAttachmentForFile builds an Attachment for a local filesystem path. It inspects the MIME type / extension and chooses an appropriate AttachmentKind, default Mode, and AvailableContentBlockModes. The returned attachment is fully populated via PopulateRef(). Note that this builds a fresh attachment, i.e both original ref and current are populated here.

func BuildAttachmentForURL

func BuildAttachmentForURL(rawURL string) (*Attachment, error)

BuildAttachmentForURL builds an Attachment for a remote URL. It uses a timeout context to peek and infer type and then give proper options.

func BuildAttachmentForURLWithContext

func BuildAttachmentForURLWithContext(ctx context.Context, rawURL string) (*Attachment, error)

BuildAttachmentForURLWithContext builds an Attachment for a remote URL.

Mode detection strategy:

  1. Best effort Content-Type detection via HEAD -> Range GET sniff.
  2. Fallback to extension-based detection.
  3. Ultimate fallback: LinkOnly (do not error out due to detection failures).

Note: It can still return an error for invalid/empty/non-absolute URLs because PopulateRef enforces validity.

func (*Attachment) BuildContentBlock

func (att *Attachment) BuildContentBlock(ctx context.Context, opts ...ContentBlockOption,
) (*ContentBlock, error)

BuildContentBlock function builds and returns a content block for an attachment. It does NOT attach the content block to the attachment.

func (*Attachment) GetTextBlockWithDisplayNameOnly

func (att *Attachment) GetTextBlockWithDisplayNameOnly(suffix string) (*ContentBlock, error)

func (*Attachment) PopulateRef

func (att *Attachment) PopulateRef(ctx context.Context, replaceOrig bool) error

type AttachmentContentBlockKind

type AttachmentContentBlockKind string
const (
	ContentBlockText  AttachmentContentBlockKind = "text"
	ContentBlockImage AttachmentContentBlockKind = "image"
	ContentBlockFile  AttachmentContentBlockKind = "file"
)

type AttachmentContentBlockMode

type AttachmentContentBlockMode string

AttachmentContentBlockMode describes how a given attachment should be used for the current turn.

const (
	AttachmentContentBlockModeText  AttachmentContentBlockMode = "text"  // "Text content"
	AttachmentContentBlockModeFile  AttachmentContentBlockMode = "file"  // "File (original format)"
	AttachmentContentBlockModeImage AttachmentContentBlockMode = "image" // Image rendering

	AttachmentContentBlockModePageContent AttachmentContentBlockMode = "page"     // "Page content" for HTML/URLs
	AttachmentContentBlockModeTextLink    AttachmentContentBlockMode = "textlink" // "Link as text block" – no fetch
	AttachmentContentBlockModeImageURL    AttachmentContentBlockMode = "imageurl"
	AttachmentContentBlockModeFileURL     AttachmentContentBlockMode = "fileurl"

	AttachmentContentBlockModeNotReadable AttachmentContentBlockMode = "not-readable" // Binary/unknown – cannot process

	AttachmentContentBlockModePRDiff     AttachmentContentBlockMode = "pr-diff"
	AttachmentContentBlockModePRPage     AttachmentContentBlockMode = "pr-page"
	AttachmentContentBlockModeCommitDiff AttachmentContentBlockMode = "commit-diff"
	AttachmentContentBlockModeCommitPage AttachmentContentBlockMode = "commit-page"
)

type AttachmentKind

type AttachmentKind string

AttachmentKind enumerates contextual attachment categories that can be associated with messages sent to the inference layer.

const (
	AttachmentFile     AttachmentKind = "file"
	AttachmentImage    AttachmentKind = "image"
	AttachmentURL      AttachmentKind = "url"
	AttachmentDocIndex AttachmentKind = "docIndex"
	AttachmentPR       AttachmentKind = "pr"
	AttachmentCommit   AttachmentKind = "commit"
)

type ContentBlock

type ContentBlock struct {
	Kind AttachmentContentBlockKind `json:"kind"`

	// For Kind == text: Text, MIMEType and FileName are populated when read from local file.
	// For URL based text attachments, Filename will not be present, MIMEType _may_ be present.
	Text     *string `json:"text,omitempty"`
	MIMEType *string `json:"mimeType,omitempty"`
	FileName *string `json:"fileName,omitempty"`

	// For Kind == image or file: Base64Data will be populated for local file.
	// For URL based attachments, Base64Data may or may not be present depending on AttachmentMode.
	Base64Data *string `json:"base64Data,omitempty"`

	// URL is populated for URL-based attachments.
	URL *string `json:"url,omitempty"`
}

ContentBlock represents a provider-agnostic chunk of content derived from an Attachment. Providers (OpenAI, Anthropic, etc.) adapt this into their own message/part formats.

func BuildContentBlocks

func BuildContentBlocks(ctx context.Context, atts []Attachment, opts ...ContentBlockOption) ([]ContentBlock, error)

BuildContentBlocks converts high-level attachments (file paths, URLs, etc.) into provider-agnostic content blocks that can then be adapted for each LLM.

type ContentBlockOption

type ContentBlockOption func(*buildContentBlockOptions)

func WithForceFetchContentBlock

func WithForceFetchContentBlock(forceFetch bool) ContentBlockOption

WithForceFetchContentBlock. Default False. False = Dont fetch the content block if already present in attachment. True = Override content block even if present.

func WithOnlyTextKindContentBlock

func WithOnlyTextKindContentBlock(textOnly bool) ContentBlockOption

WithOnlyTextKindContentBlock. Default False. False = Build content block of any kind. True = Return content block only if it is of kind text, else error.

func WithOverrideOriginalContentBlock

func WithOverrideOriginalContentBlock(override bool) ContentBlockOption

WithOverrideOriginalContentBlock. Default false. False = If this is an attachment from a previous turn whose underlying ref (file path, size, mod time, etc.) has changed, do not re-send the possibly mismatched content.

type DirectoryAttachmentsResult

type DirectoryAttachmentsResult struct {
	DirPath      string                  `json:"dirPath"`
	Attachments  []Attachment            `json:"attachments"`  // included attachments (flattened)
	OverflowDirs []DirectoryOverflowInfo `json:"overflowDirs"` // directories not fully included
	MaxFiles     int                     `json:"maxFiles"`     // max number of files returned (after clamping)
	TotalSize    int64                   `json:"totalSize"`    // sum of Files[i].Size
	HasMore      bool                    `json:"hasMore"`      // true if not all content included
}

type DirectoryOverflowInfo added in v0.0.70

type DirectoryOverflowInfo struct {
	DirPath      string `json:"dirPath"`
	RelativePath string `json:"relativePath"`
	FileCount    int    `json:"fileCount"`
	Partial      bool   `json:"partial"`
}

DirectoryOverflowInfo represents a directory that was *not fully walked* because we hit the max-files limit or had an error.

Semantics:

  • DirPath / RelativePath: Absolute / relative-to-root path of that directory.
  • FileCount: For completely unvisited dirs (left in the BFS queue):
  • number of direct entries (files + subdirs) from a single os.ReadDir, no recursion. For the single "partial" dir where we hit the limit mid-scan:
  • number of remaining entries in that directory (files + subdirs) that we did NOT process. This is *approximate UI sugar*, not a full subtree count.
  • Partial: true only for the directory where we stopped in the middle of its entries because maxFiles was reached. For all other overflow dirs it's false.

type ExtensionMode added in v0.0.70

type ExtensionMode string
const (
	ExtensionModeText     ExtensionMode = "text"
	ExtensionModeImage    ExtensionMode = "image"
	ExtensionModeDocument ExtensionMode = "document"
	ExtensionModeDefault  ExtensionMode = "default"
)

type FileExt added in v0.0.70

type FileExt string
const (
	ExtTxt      FileExt = ".txt"
	ExtMd       FileExt = ".md"
	ExtMarkdown FileExt = ".markdown"
	ExtMdx      FileExt = ".mdx"
	ExtLog      FileExt = ".log"
	ExtJSON     FileExt = ".json"
	ExtYAML     FileExt = ".yaml"
	ExtYML      FileExt = ".yml"
	ExtTOML     FileExt = ".toml"
	ExtJS       FileExt = ".js"
	ExtTS       FileExt = ".ts"
	ExtTSX      FileExt = ".tsx"
	ExtJSX      FileExt = ".jsx"
	ExtPY       FileExt = ".py"
	ExtGO       FileExt = ".go"
	ExtRS       FileExt = ".rs"
	ExtJAVA     FileExt = ".java"
	ExtC        FileExt = ".c"
	ExtCPP      FileExt = ".cpp"
	ExtH        FileExt = ".h"
	ExtHPP      FileExt = ".hpp"
	ExtCS       FileExt = ".cs"
	ExtRB       FileExt = ".rb"
	ExtPHP      FileExt = ".php"
	ExtHTML     FileExt = ".html"
	ExtHTM      FileExt = ".htm"
	ExtCSS      FileExt = ".css"
	ExtSCSS     FileExt = ".scss"
	ExtLESS     FileExt = ".less"
	ExtSQL      FileExt = ".sql"
	ExtMod      FileExt = ".mod"
	ExtSum      FileExt = ".sum"
	ExtJSONL    FileExt = ".jsonl"
	ExtShell    FileExt = ".sh"
	ExtSWIFT    FileExt = ".swift"
	ExtM        FileExt = ".m"
	ExtKT       FileExt = ".kt"
	ExtPL       FileExt = ".pl"
	ExtSCALA    FileExt = ".scala"
	ExtHS       FileExt = ".hs"
	ExtLUA      FileExt = ".lua"
	ExtDART     FileExt = ".dart"
	ExtCmake    FileExt = ".cmake"
	ExtBazel    FileExt = ".bazel"

	ExtJPG  FileExt = ".jpg"
	ExtJPEG FileExt = ".jpeg"
	ExtPNG  FileExt = ".png"
	ExtGIF  FileExt = ".gif"
	ExtWEBP FileExt = ".webp"
	ExtBMP  FileExt = ".bmp"
	ExtSVG  FileExt = ".svg"

	ExtPDF  FileExt = ".pdf"
	ExtDOC  FileExt = ".doc"
	ExtDOCX FileExt = ".docx"
	ExtPPT  FileExt = ".ppt"
	ExtPPTX FileExt = ".pptx"
	ExtXLS  FileExt = ".xls"
	ExtXLSX FileExt = ".xlsx"
	ExtODT  FileExt = ".odt"
	ExtODS  FileExt = ".ods"
)

type FileFilter added in v0.0.70

type FileFilter struct {
	DisplayName string
	Extensions  []FileExt // normalized, e.g. ".png".
}

FileFilter is used to build file dialog filters (Patterns like "*.png;*.jpg").

func (FileFilter) Pattern added in v0.0.70

func (f FileFilter) Pattern() string

Pattern converts a FileFilter into a semicolon-separated pattern string. W.g. "*.png;*.jpg".

type FileRef

type FileRef struct {
	PathInfo

	// Snapshot of the original file state when it was first attached.
	// This lets us detect if the underlying file changed between turns.
	OrigPath    string    `json:"origPath"`
	OrigSize    int64     `json:"origSize"`
	OrigModTime time.Time `json:"origModTime"`
}

FileRef carries metadata for file attachments.

func (*FileRef) BuildContentBlock

func (ref *FileRef) BuildContentBlock(
	ctx context.Context,
	attachmentContentBlockMode AttachmentContentBlockMode,
	onlyIfTextKind bool,
) (*ContentBlock, error)

func (*FileRef) IsModified

func (ref *FileRef) IsModified() bool

IsModified reports whether the current file state differs from the original snapshot captured when the attachment was first used.

func (*FileRef) PopulateRef

func (ref *FileRef) PopulateRef(ctx context.Context, replaceOrig bool) error

type GenericRef

type GenericRef struct {
	Handle string `json:"handle"`

	OrigHandle string `json:"origHandle"`
}

GenericRef preserves legacy handle-style references (PRs, doc indexes, etc.).

func (*GenericRef) IsModified

func (ref *GenericRef) IsModified() bool

func (*GenericRef) PopulateRef

func (ref *GenericRef) PopulateRef(ctx context.Context, replaceOrig bool) error

type ImageRef

type ImageRef struct {
	Path    string     `json:"path"`
	Name    string     `json:"name"`
	Exists  bool       `json:"exists"`
	IsDir   bool       `json:"isDir"`
	Size    int64      `json:"size,omitempty"`
	ModTime *time.Time `json:"modTime,omitempty"`

	Width    int      `json:"width,omitempty"`
	Height   int      `json:"height,omitempty"`
	Format   string   `json:"format,omitempty"`   // e.g. "jpeg", "png"
	MIMEType MIMEType `json:"mimeType,omitempty"` // e.g. "image/jpeg"

	// Original snapshot (for change detection across turns).
	OrigPath    string    `json:"origPath"`
	OrigSize    int64     `json:"origSize"`
	OrigModTime time.Time `json:"origModTime"`
}

ImageRef carries metadata for image attachments.

func (*ImageRef) BuildContentBlock

func (ref *ImageRef) BuildContentBlock(ctx context.Context) (*ContentBlock, error)

func (*ImageRef) IsModified

func (ref *ImageRef) IsModified() bool

func (*ImageRef) PopulateRef

func (ref *ImageRef) PopulateRef(ctx context.Context, replaceOrig bool) error

type MIMEType added in v0.0.70

type MIMEType string
const (
	MIMEEmpty                  MIMEType = ""
	MIMEApplicationOctetStream MIMEType = "application/octet-stream"

	MIMETextPlain       MIMEType = "text/plain; charset=utf-8"
	MIMETextMarkdown    MIMEType = "text/markdown; charset=utf-8"
	MIMETextHTML        MIMEType = "text/html; charset=utf-8"
	MIMETextCSS         MIMEType = "text/css; charset=utf-8"
	MIMEApplicationJSON MIMEType = "application/json"
	MIMEApplicationYAML MIMEType = "application/x-yaml"
	MIMEApplicationTOML MIMEType = "application/toml"
	MIMEApplicationSQL  MIMEType = "application/sql"
	MIMEApplicationJS   MIMEType = "application/javascript"

	MIMEImageJPEG MIMEType = "image/jpeg"
	MIMEImagePNG  MIMEType = "image/png"
	MIMEImageGIF  MIMEType = "image/gif"
	MIMEImageWEBP MIMEType = "image/webp"
	MIMEImageBMP  MIMEType = "image/bmp"
	MIMEImageSVG  MIMEType = "image/svg+xml"

	MIMEApplicationPDF        MIMEType = "application/pdf"
	MIMEApplicationMSWord     MIMEType = "application/msword"
	MIMEApplicationMSPowerPt  MIMEType = "application/vnd.ms-powerpoint"
	MIMEApplicationMSExcel    MIMEType = "application/vnd.ms-excel"
	MIMEApplicationOpenXMLDoc MIMEType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
	MIMEApplicationOpenXMLPPT MIMEType = "application/vnd.openxmlformats-officedocument.presentationml.presentation"
	MIMEApplicationOpenXMLXLS MIMEType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
	MIMEApplicationODT        MIMEType = "application/vnd.oasis.opendocument.text"
	MIMEApplicationODS        MIMEType = "application/vnd.oasis.opendocument.spreadsheet"
)

type PathInfo added in v0.0.70

type PathInfo struct {
	Path    string     `json:"path"`
	Name    string     `json:"name"`
	Exists  bool       `json:"exists"`
	IsDir   bool       `json:"isDir"`
	Size    int64      `json:"size,omitempty"`
	ModTime *time.Time `json:"modTime,omitempty"`
}

type URLRef

type URLRef struct {
	URL            string `json:"url"`
	Normalized     string `json:"normalized,omitempty"`
	OrigNormalized string `json:"origNormalized"`
}

URLRef carries metadata for URL-based attachments.

URL: the raw user-provided URL (after trimming whitespace). Normalized: a canonicalized string representation used internally. OrigNormalized: snapshot of the original Normalized value so we can

detect whether a URL has been modified in-place.

func (*URLRef) BuildContentBlock

func (ref *URLRef) BuildContentBlock(
	ctx context.Context,
	attachmentContentBlockMode AttachmentContentBlockMode,
	onlyIfTextKind bool,
) (*ContentBlock, error)

BuildContentBlock builds a ContentBlock representation for a URL-based attachment, depending on the desired AttachmentContentBlockMode.

The behaviour is:

  • AttachmentContentBlockModeTextLink: always returns a simple text link.
  • AttachmentContentBlockModeImage: tries to fetch the URL as an image and return a ContentBlockImage, else falls back to a simple link.
  • AttachmentContentBlockModePageContent / AttachmentContentBlockModeText: runs the URL through the "page pipeline" (HTML/text/PDF/image handling) and returns a text or file/image block as appropriate.
  • AttachmentContentBlockModeFile: fetches the raw bytes and returns a ContentBlockFile, else falls back to a link.
  • Any other modes (PR diff/page, commit diff/page, not readable, etc.): safest fallback is a link-only block.

This function assumes that Attachment, AttachmentContentBlockMode, ContentBlock and ContentBlock* constants are defined elsewhere in this package.

func (*URLRef) IsModified

func (ref *URLRef) IsModified() bool

IsModified reports whether the URL has been modified from its original normalized form. This is useful for detecting in-place edits.

func (*URLRef) PopulateRef

func (ref *URLRef) PopulateRef(ctx context.Context, replaceOrig bool) error

PopulateRef validates and normalizes the URL stored in the URLRef. It must be called before the URLRef is used.

It ensures:

  • The URL is non-empty.
  • The URL parses successfully.
  • The URL is absolute (has a scheme/host).
  • Normalized and OrigNormalized are populated.

type WalkDirectoryWithFilesResult added in v0.0.70

type WalkDirectoryWithFilesResult struct {
	DirPath      string                  `json:"dirPath"`
	Files        []PathInfo              `json:"files"`        // included files (flattened)
	OverflowDirs []DirectoryOverflowInfo `json:"overflowDirs"` // directories not fully included
	MaxFiles     int                     `json:"maxFiles"`     // max number of files returned (after clamping)
	TotalSize    int64                   `json:"totalSize"`    // sum of Files[i].Size
	HasMore      bool                    `json:"hasMore"`      // true if not all content included
}

WalkDirectoryWithFilesResult is returned when user selects a directory.

func WalkDirectoryWithFiles added in v0.0.70

func WalkDirectoryWithFiles(ctx context.Context, dirPath string, maxFiles int) (*WalkDirectoryWithFilesResult, error)

WalkDirectoryWithFiles implements:

  • Pure BFS over the directory tree starting at dirPath.
  • For each directory:
  • process all regular, non-dot files first (highest priority), adding them to Files until maxFiles is reached.
  • then enqueue its subdirectories for BFS.
  • As soon as Files reaches maxFiles, walking stops.
  • Remaining directories in the BFS queue are returned as OverflowDirs, with a shallow os.ReadDir() to get a one-level item count.
  • The directory where we actually hit the limit (if any) is also listed as an overflow entry with Partial = true and a count of remaining items (files + subdirs) in that directory.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL