common

package
v2.22.0 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: Apache-2.0 Imports: 10 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package common provides shared functionality for persistence repositories.

Index

Constants

View Source
const (
	DirPerm  = 0o750 // Directory permissions: rwxr-x---
	FilePerm = 0o600 // File permissions: rw-------
)

File permission constants for persistence operations

Variables

This section is empty.

Functions

func FragmentFilePath added in v2.19.0

func FragmentFilePath(basePath, name, relativePath, baseDir, ext string) (string, error)

FragmentFilePath returns the file a fragment lookup reads, for a repository over basePath:

  • an absolute path is used as it is, since a config wrote it;
  • a relative path resolves against baseDir (the working directory when baseDir is empty) and may climb only as far as its own leading "../" segments go. Those are the config's: "../fragments/x.yaml" from a prompts directory reaches its sibling fragments directory. A ".." after a name, which climbs back past where the path started, is refused with persistence.ErrFragmentPathOutsideBase. For the working-directory fallback the error also satisfies fs.ErrNotExist (nothing it may read is there). FragmentResolver keeps a variable from adding "..", so a variable cannot move this point;
  • no path is the default file, basePath/fragments/<name><ext>, which must stay in basePath/fragments.

Containment holds on the resolved file too: a symlink that points outside the point the path may reach is refused. This is a check on the files as they are now; ReadFragmentFile holds the same rules on the read itself.

func ReadFragmentFile added in v2.19.0

func ReadFragmentFile(basePath, name, relativePath, baseDir, ext string) ([]byte, string, error)

ReadFragmentFile reads the fragment file a lookup names (see FragmentFilePath for the rules) and returns its content and path.

A relative or default path is read through an os.Root opened at the anchor, the point the path may reach (the climb anchor, or basePath). The anchor is resolved once, when the root is opened: symlinks in it or in its parent directories are followed then, so a pack directory that is a link to elsewhere is read where it really is. Every read of the lookup goes through that root, so nothing changed afterwards moves it: a fragment link swapped to point outside, or a parent directory swapped for a link, is not followed. A path or link that leaves the anchor is persistence.ErrFragmentPathOutsideBase, which also satisfies fs.ErrNotExist for the working-directory fallback. A missing file is fs.ErrNotExist. An absolute path, which the config wrote, is read as it is.

func ValidatePromptConfig

func ValidatePromptConfig(config *prompt.Config) error

ValidatePromptConfig validates the prompt configuration structure

Types

type BasePromptRepository

type BasePromptRepository struct {
	BasePath       string
	TaskTypeToFile map[string]string
	Cache          map[string]*prompt.Config
	Extensions     []string
	Unmarshal      UnmarshalFunc
	Marshal        MarshalFunc
	// contains filtered or unexported fields
}

BasePromptRepository provides common prompt repository functionality.

LoadPrompt and SavePrompt are safe to call concurrently: a prompt.Registry loads prompts outside its own lock, so conversations sharing one registry reach the repository at the same time. Code that reads or writes Cache or TaskTypeToFile directly must not run alongside them.

func NewBasePromptRepository

func NewBasePromptRepository(
	basePath string, taskTypeToFile map[string]string, extensions []string, unmarshal UnmarshalFunc,
) *BasePromptRepository

NewBasePromptRepository creates a new base repository

func (*BasePromptRepository) HasMatchingTaskType

func (r *BasePromptRepository) HasMatchingTaskType(path, taskType string) bool

HasMatchingTaskType checks if a file contains the specified task type

func (*BasePromptRepository) HasValidExtension

func (r *BasePromptRepository) HasValidExtension(path string) bool

HasValidExtension checks if a file has a valid extension

func (*BasePromptRepository) ListPrompts

func (r *BasePromptRepository) ListPrompts() ([]string, error)

ListPrompts returns all available prompt task types

func (*BasePromptRepository) LoadPrompt

func (r *BasePromptRepository) LoadPrompt(taskType string) (*prompt.Config, error)

LoadPrompt loads a prompt configuration by task type

func (*BasePromptRepository) PromptDir added in v2.18.0

func (r *BasePromptRepository) PromptDir(taskType string) (string, error)

PromptDir returns the directory holding the config file for taskType, which is the base a fragment reference's path resolves against. It implements prompt.ConfigDirResolver.

func (*BasePromptRepository) ResolveFilePath

func (r *BasePromptRepository) ResolveFilePath(taskType string) (string, error)

ResolveFilePath finds the file path for a given task type

func (*BasePromptRepository) SavePrompt

func (r *BasePromptRepository) SavePrompt(config *prompt.Config) error

SavePrompt saves a prompt configuration to disk

func (*BasePromptRepository) SearchByContent

func (r *BasePromptRepository) SearchByContent(taskType string) string

SearchByContent searches for files by parsing and checking task type

func (*BasePromptRepository) SearchByFilename

func (r *BasePromptRepository) SearchByFilename(taskType string) string

SearchByFilename searches for files by filename patterns

func (*BasePromptRepository) SearchForPrompt

func (r *BasePromptRepository) SearchForPrompt(taskType string) (string, error)

SearchForPrompt searches for a file matching the task type

type MarshalFunc

type MarshalFunc func(interface{}) ([]byte, error)

MarshalFunc is a function that marshals a prompt config to bytes

type UnmarshalFunc

type UnmarshalFunc func([]byte, interface{}) error

UnmarshalFunc is a function that unmarshals data into a prompt config

Jump to

Keyboard shortcuts

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