Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

3.1 KB
Raw Blame
//
//  Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

// PageChunker emits one chunk per upstream slide or page.
// It is the faithful Go port of the Python `presentation` chunk method
// (rag/app/presentation.py), whose docstring states: "Every page will
// be treated as a chunk."
//
// Unlike TokenChunker (which merges slides into a single chunk) or
// OneChunker (which collapses many slides into one), PageChunker
// preserves the upstream slide records, including structured tables and
// images. Every record retains its slide_number for source-page identity.
package chunker

import (
	"context"

	"ragflow/internal/agent/runtime"
	"ragflow/internal/ingestion/component/schema"

	"gorm.io/gorm"
)

type pageChunkerParam struct{}

func (p *pageChunkerParam) Update(conf map[string]any) {}

func (pageChunkerParam) Defaults() pageChunkerParam {
	return pageChunkerParam{}
}

func (pageChunkerParam) Validate() error { return nil }

type PageChunkerComponent struct {
	name  string
	param pageChunkerParam
}

func NewPageChunker(params map[string]any) (runtime.Component, error) {
	p := pageChunkerParam{}.Defaults()
	(&p).Update(params)
	if err := p.Validate(); err != nil {
		return nil, err
	}
	return &PageChunkerComponent{
		name:  ComponentNamePageChunker,
		param: p,
	}, nil
}
func (c *PageChunkerComponent) Inputs() map[string]string { return ChunkerInputs }

func (c *PageChunkerComponent) Outputs() map[string]string { return ChunkerOutputs }

func (c *PageChunkerComponent) Invoke(ctx context.Context, db *gorm.DB, inputs map[string]any) (map[string]any, error) {
	return c.invoke(ctx, inputs)
}

// invoke emits one chunk per decoded upstream item (slide or page).
func (c *PageChunkerComponent) invoke(_ context.Context, inputs map[string]any) (map[string]any, error) {
	if inputs == nil {
		return emptyOutputs(), nil
	}
	upstream, err := decodeChunkerFromUpstream(inputs)
	if err != nil {
		return inputErrorOutput(err), nil
	}

	// The Page chunker preserves one incoming JSON item per output chunk.
	// It has no file-family restriction: item granularity is determined by
	// the upstream parser backend.
	items := slideItems(upstream.JSONResult, upstream.Chunks)
	if len(items) == 0 {
		return emptyOutputs(), nil
	}
	return chunkOutputs(items), nil
}

// slideItems returns the per-slide records, preferring JSONResult and
// falling back to Chunks. Each record (slide/page) becomes one chunk.
func slideItems(items, chunks []schema.ChunkDoc) []schema.ChunkDoc {
	if len(items) > 0 {
		return items
	}
	return chunks
}

func init() {
	MustRegisterChunker(ComponentNamePageChunker)
}