Documentation
¶
Overview ¶
Package note contains the shared logic for persisting a user- or agent-authored note into the second-brain knowledge base. It is used by both the MCP add_note tool (cmd/mcp/main.go) and the POST /api/v1/notes REST handler (internal/api/notes.go), so a change here affects both paths identically — behaviour must not silently diverge between them.
Index ¶
Constants ¶
const MaxContentBytes = 10 * 1024 * 1024
MaxContentBytes is the upper bound for note content (10 MiB). Unchanged from the pre-extraction MCP add_note limit.
const MaxEmbedChunks = 2000
MaxEmbedChunks is the upper bound for the number of chunks embedded in a single Save call. Notes that produce more chunks are stored and indexed via FTS, but embedding is skipped to avoid unbounded API cost and long-running DB transactions.
const MaxTitleBytes = 1024
MaxTitleBytes is the upper bound for note title (1 KiB). Unchanged from the pre-extraction MCP add_note limit.
Variables ¶
var ErrEmbedSkipped = errors.New("embedding skipped: chunk count exceeds limit")
ErrEmbedSkipped is returned by embedChunks when the chunk count exceeds MaxEmbedChunks. It is a sentinel that Save treats as a deliberate skip (non-fatal, non-success) rather than an actual embedding failure. Callers MUST NOT surface this as a user-facing error.
Functions ¶
This section is empty.
Types ¶
type ChunkWriter ¶
type ChunkWriter interface {
ReplaceDocument(ctx context.Context, documentID uuid.UUID, chunks []store.Chunk) error
ListByDocument(ctx context.Context, documentID uuid.UUID) ([]store.Chunk, error)
UpdateChunkEmbeddings(ctx context.Context, embeddings []store.ChunkEmbedding) error
}
ChunkWriter is the subset of ChunkStore used by Save.
type DocumentUpserter ¶
DocumentUpserter is the subset of DocumentStore used by Save.
type Embedder ¶
type Embedder interface {
Enabled() bool
EmbedBatch(ctx context.Context, texts []string) ([][]float32, error)
}
Embedder is the subset of EmbedClient used by Save.
type Result ¶
type Result struct {
ID string `json:"id"`
ChunksCreated int `json:"chunks_created"`
EmbeddingCreated bool `json:"embedding_created"`
}
Result is returned by Save on success.
func Save ¶
func Save( ctx context.Context, docs DocumentUpserter, chunks ChunkWriter, embed Embedder, sourceType model.SourceType, title, content, sourceID string, metadata map[string]any, doEmbed bool, requireTitle bool, ) (*Result, string)
Save persists a note document, splits it into chunks, and optionally embeds those chunks. It is the extraction of the former cmd/mcp/main.go handleAddNote, parameterized so both the MCP add_note tool and the POST /api/v1/notes REST handler share identical persistence behaviour.
sourceType controls the stored model.SourceType (model.SourceAgentNote for MCP, model.SourceNote for the REST path — spec §6.2, updated 2026-08-25; MCP add_note previously wrote model.SourceLLMMemory, since deprecated).
requireTitle=true rejects an empty title (MCP add_note's pre-extraction behaviour). requireTitle=false allows an empty title, which POST /api/v1/notes relies on: the enrichment worker fills in the real title later rather than the server guessing one from content (spec §6.1).
Returns (result, "") on success, or (nil, errMsg) on failure.