Files
ragflow/web/src/hooks/parser-config-utils.ts
Jin Hai cb3de6ae98 TS: add apache license (#18064)
Signed-off-by: Jin Hai <haijin.chn@gmail.com>
2026-08-11 11:15:45 +08:00

137 lines
3.6 KiB
TypeScript

/*
* 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.
*/
/**
* Utility functions for extracting parser and raptor config extensions.
* These functions extract known fields from parser/raptor config objects
* and merge unknown fields into the `ext` field for flexible configuration.
*/
/**
* Pipeline parser configs are keyed by operator id (e.g. "Parser:xxx"), so a
* top-level key containing ":" marks the pipeline structure, which must be
* sent as-is instead of being reshaped by extractParserConfigExt.
*/
export const isPipelineParserConfig = (
parserConfig: Record<string, any> | undefined,
): boolean => {
if (!parserConfig || typeof parserConfig !== 'object') {
return false;
}
return Object.keys(parserConfig).some((key) => key.includes(':'));
};
/**
* Extracts Raptor configuration with extra fields merged into ext.
* @param raptorConfig - The raptor configuration object
* @returns Processed raptor config with extra fields in ext
*/
export const extractRaptorConfigExt = (
raptorConfig: Record<string, any> | undefined,
) => {
if (!raptorConfig) return raptorConfig;
const {
use_raptor,
prompt,
max_token,
threshold,
max_cluster,
random_seed,
scope,
clustering_method,
tree_builder,
auto_disable_for_structured_data,
ext,
...raptorExt
} = raptorConfig;
const extClusteringMethod = ext?.clustering_method;
const normalizedClusteringMethod =
clustering_method ?? extClusteringMethod ?? 'gmm';
const normalizedTreeBuilder = tree_builder ?? ext?.tree_builder ?? 'raptor';
return {
use_raptor,
prompt,
max_token,
threshold,
max_cluster,
random_seed,
scope,
auto_disable_for_structured_data,
ext: {
...ext,
...raptorExt,
clustering_method: normalizedClusteringMethod,
tree_builder: normalizedTreeBuilder,
},
};
};
/**
* Extracts Parser configuration with extra fields merged into ext.
* @param parserConfig - The parser configuration object
* @returns Processed parser config with extra fields in ext
*/
export const extractParserConfigExt = (
parserConfig: Record<string, any> | undefined,
) => {
if (!parserConfig) return parserConfig;
const {
auto_keywords,
auto_questions,
chunk_token_num,
delimiter,
graphrag,
html4excel,
layout_recognize,
raptor,
tag_kb_ids,
topn_tags,
filename_embd_weight,
task_page_size,
pages,
children_delimiter,
use_parent_child,
enable_children,
ext,
...parserExt
} = parserConfig;
return {
auto_keywords,
auto_questions,
chunk_token_num,
delimiter,
graphrag,
html4excel,
layout_recognize,
raptor: extractRaptorConfigExt(raptor),
tag_kb_ids,
topn_tags,
filename_embd_weight,
task_page_size,
pages,
children_delimiter,
enable_children,
parent_child: enable_children
? {
children_delimiter,
use_parent_child: use_parent_child ?? enable_children,
}
: undefined,
ext: { ...ext, ...parserExt },
};
};