@xpert-ai/plugin-sdk 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (135) hide show
  1. package/index.cjs.d.ts +1 -0
  2. package/index.cjs.js +307977 -0
  3. package/index.esm.d.ts +1 -0
  4. package/index.esm.js +307944 -0
  5. package/package.json +3 -3
  6. package/src/index.d.ts +12 -0
  7. package/src/lib/core/file-system.d.ts +42 -0
  8. package/src/lib/core/index.d.ts +3 -0
  9. package/src/lib/core/permissions.d.ts +74 -0
  10. package/src/lib/core/schema.d.ts +22 -0
  11. package/src/lib/integration/index.d.ts +3 -0
  12. package/src/lib/integration/strategy.decorator.d.ts +2 -0
  13. package/src/lib/integration/{strategy.interface.ts → strategy.interface.d.ts} +5 -7
  14. package/src/lib/integration/strategy.registry.d.ts +6 -0
  15. package/src/lib/knowledge/index.d.ts +3 -0
  16. package/src/lib/knowledge/knowledge-strategy.decorator.d.ts +2 -0
  17. package/src/lib/knowledge/knowledge-strategy.interface.d.ts +15 -0
  18. package/src/lib/knowledge/knowledge-strategy.registry.d.ts +12 -0
  19. package/src/lib/logger.d.ts +2 -0
  20. package/src/lib/plugin-metadata.d.ts +17 -0
  21. package/src/lib/plugin.d.ts +8 -0
  22. package/src/lib/{plugin.hook.ts → plugin.hook.d.ts} +0 -4
  23. package/src/lib/plugin.interface.d.ts +59 -0
  24. package/src/lib/rag/image/index.d.ts +3 -0
  25. package/src/lib/rag/image/strategy.decorator.d.ts +5 -0
  26. package/src/lib/rag/image/strategy.interface.d.ts +36 -0
  27. package/src/lib/rag/image/strategy.registry.d.ts +6 -0
  28. package/src/lib/rag/index.d.ts +6 -0
  29. package/src/lib/rag/retriever/index.d.ts +3 -0
  30. package/src/lib/rag/retriever/strategy.decorator.d.ts +5 -0
  31. package/src/lib/rag/retriever/strategy.interface.d.ts +30 -0
  32. package/src/lib/rag/retriever/strategy.registry.d.ts +6 -0
  33. package/src/lib/rag/source/index.d.ts +3 -0
  34. package/src/lib/rag/source/strategy.decorator.d.ts +5 -0
  35. package/src/lib/rag/source/strategy.interface.d.ts +26 -0
  36. package/src/lib/rag/source/strategy.registry.d.ts +6 -0
  37. package/src/lib/rag/textsplitter/index.d.ts +3 -0
  38. package/src/lib/rag/textsplitter/strategy.decorator.d.ts +2 -0
  39. package/src/lib/rag/textsplitter/strategy.interface.d.ts +24 -0
  40. package/src/lib/rag/textsplitter/strategy.registry.d.ts +6 -0
  41. package/src/lib/rag/transformer/index.d.ts +3 -0
  42. package/src/lib/rag/transformer/strategy.decorator.d.ts +5 -0
  43. package/src/lib/rag/transformer/strategy.interface.d.ts +40 -0
  44. package/src/lib/rag/transformer/strategy.registry.d.ts +6 -0
  45. package/src/lib/rag/types.d.ts +29 -0
  46. package/src/lib/strategy.d.ts +12 -0
  47. package/src/lib/toolset/builtin.d.ts +54 -0
  48. package/src/lib/toolset/index.d.ts +5 -0
  49. package/src/lib/toolset/strategy.decorator.d.ts +5 -0
  50. package/src/lib/toolset/strategy.interface.d.ts +28 -0
  51. package/src/lib/toolset/strategy.registry.d.ts +6 -0
  52. package/src/lib/toolset/toolset.d.ts +53 -0
  53. package/src/lib/types.d.ts +47 -0
  54. package/src/lib/vectorstore/index.d.ts +3 -0
  55. package/src/lib/vectorstore/strategy.decorator.d.ts +2 -0
  56. package/src/lib/vectorstore/strategy.interface.d.ts +24 -0
  57. package/src/lib/vectorstore/strategy.registry.d.ts +6 -0
  58. package/src/lib/workflow/index.d.ts +2 -0
  59. package/src/lib/workflow/node/index.d.ts +3 -0
  60. package/src/lib/workflow/node/strategy.decorator.d.ts +5 -0
  61. package/src/lib/workflow/node/strategy.interface.d.ts +40 -0
  62. package/src/lib/workflow/node/strategy.registry.d.ts +9 -0
  63. package/src/lib/workflow/trigger/index.d.ts +3 -0
  64. package/src/lib/workflow/trigger/strategy.decorator.d.ts +2 -0
  65. package/src/lib/workflow/trigger/strategy.interface.d.ts +22 -0
  66. package/src/lib/workflow/trigger/strategy.registry.d.ts +6 -0
  67. package/.eslintrc.json +0 -30
  68. package/.swcrc +0 -29
  69. package/PERMISSIONS.md +0 -181
  70. package/README.md +0 -19
  71. package/SCHEMA_SPECIFICATION.md +0 -332
  72. package/jest.config.ts +0 -28
  73. package/project.json +0 -29
  74. package/src/index.ts +0 -12
  75. package/src/lib/core/file-system.ts +0 -96
  76. package/src/lib/core/index.ts +0 -3
  77. package/src/lib/core/permissions.ts +0 -97
  78. package/src/lib/core/schema.ts +0 -23
  79. package/src/lib/integration/index.ts +0 -3
  80. package/src/lib/integration/strategy.decorator.ts +0 -6
  81. package/src/lib/integration/strategy.registry.ts +0 -12
  82. package/src/lib/knowledge/index.ts +0 -3
  83. package/src/lib/knowledge/knowledge-strategy.decorator.ts +0 -6
  84. package/src/lib/knowledge/knowledge-strategy.interface.ts +0 -15
  85. package/src/lib/knowledge/knowledge-strategy.registry.ts +0 -36
  86. package/src/lib/logger.ts +0 -18
  87. package/src/lib/plugin-metadata.ts +0 -20
  88. package/src/lib/plugin.interface.ts +0 -67
  89. package/src/lib/plugin.ts +0 -32
  90. package/src/lib/rag/image/index.ts +0 -3
  91. package/src/lib/rag/image/strategy.decorator.ts +0 -9
  92. package/src/lib/rag/image/strategy.interface.ts +0 -42
  93. package/src/lib/rag/image/strategy.registry.ts +0 -17
  94. package/src/lib/rag/index.ts +0 -6
  95. package/src/lib/rag/retriever/index.ts +0 -3
  96. package/src/lib/rag/retriever/strategy.decorator.ts +0 -9
  97. package/src/lib/rag/retriever/strategy.interface.ts +0 -32
  98. package/src/lib/rag/retriever/strategy.registry.ts +0 -12
  99. package/src/lib/rag/source/index.ts +0 -3
  100. package/src/lib/rag/source/strategy.decorator.ts +0 -9
  101. package/src/lib/rag/source/strategy.interface.ts +0 -28
  102. package/src/lib/rag/source/strategy.registry.ts +0 -17
  103. package/src/lib/rag/textsplitter/index.ts +0 -3
  104. package/src/lib/rag/textsplitter/strategy.decorator.ts +0 -6
  105. package/src/lib/rag/textsplitter/strategy.interface.ts +0 -28
  106. package/src/lib/rag/textsplitter/strategy.registry.ts +0 -17
  107. package/src/lib/rag/transformer/index.ts +0 -3
  108. package/src/lib/rag/transformer/strategy.decorator.ts +0 -9
  109. package/src/lib/rag/transformer/strategy.interface.ts +0 -48
  110. package/src/lib/rag/transformer/strategy.registry.ts +0 -14
  111. package/src/lib/rag/types.ts +0 -86
  112. package/src/lib/strategy.ts +0 -37
  113. package/src/lib/toolset/builtin.ts +0 -111
  114. package/src/lib/toolset/index.ts +0 -5
  115. package/src/lib/toolset/strategy.decorator.ts +0 -9
  116. package/src/lib/toolset/strategy.interface.ts +0 -32
  117. package/src/lib/toolset/strategy.registry.ts +0 -17
  118. package/src/lib/toolset/toolset.ts +0 -76
  119. package/src/lib/types.ts +0 -47
  120. package/src/lib/vectorstore/index.ts +0 -3
  121. package/src/lib/vectorstore/strategy.decorator.ts +0 -6
  122. package/src/lib/vectorstore/strategy.interface.ts +0 -25
  123. package/src/lib/vectorstore/strategy.registry.ts +0 -17
  124. package/src/lib/workflow/index.ts +0 -2
  125. package/src/lib/workflow/node/index.ts +0 -3
  126. package/src/lib/workflow/node/strategy.decorator.ts +0 -9
  127. package/src/lib/workflow/node/strategy.interface.ts +0 -49
  128. package/src/lib/workflow/node/strategy.registry.ts +0 -18
  129. package/src/lib/workflow/trigger/index.ts +0 -3
  130. package/src/lib/workflow/trigger/strategy.decorator.ts +0 -6
  131. package/src/lib/workflow/trigger/strategy.interface.ts +0 -27
  132. package/src/lib/workflow/trigger/strategy.registry.ts +0 -17
  133. package/tsconfig.json +0 -22
  134. package/tsconfig.lib.json +0 -10
  135. package/tsconfig.spec.json +0 -9
@@ -1,36 +0,0 @@
1
- import { KnowledgeProviderEnum } from '@metad/contracts'
2
- import { Injectable, OnModuleInit } from '@nestjs/common'
3
- import { DiscoveryService, Reflector } from '@nestjs/core'
4
- import { KNOWLEDGE_STRATEGY } from './knowledge-strategy.decorator'
5
- import { KnowledgeStrategy } from './knowledge-strategy.interface'
6
-
7
- @Injectable()
8
- export class KnowledgeStrategyRegistry implements OnModuleInit {
9
- private strategies = new Map<KnowledgeProviderEnum, KnowledgeStrategy>()
10
-
11
- constructor(
12
- private discoveryService: DiscoveryService,
13
- private reflector: Reflector
14
- ) {}
15
-
16
- onModuleInit() {
17
- const providers = this.discoveryService.getProviders()
18
- for (const wrapper of providers) {
19
- const { instance } = wrapper
20
- if (!instance) continue
21
-
22
- const type = this.reflector.get<KnowledgeProviderEnum>(KNOWLEDGE_STRATEGY, instance.constructor)
23
- if (type) {
24
- this.strategies.set(type, instance as KnowledgeStrategy)
25
- }
26
- }
27
- }
28
-
29
- get(type: KnowledgeProviderEnum): KnowledgeStrategy {
30
- const strategy = this.strategies.get(type)
31
- if (!strategy) {
32
- throw new Error(`No strategy found for type ${type}`)
33
- }
34
- return strategy
35
- }
36
- }
package/src/lib/logger.ts DELETED
@@ -1,18 +0,0 @@
1
- import { Logger } from '@nestjs/common';
2
- import type { PluginLogger } from './types';
3
-
4
- export function createPluginLogger(scope: string, baseMeta: Record<string, any> = {}): PluginLogger {
5
- const nestLogger = new Logger(scope);
6
- const wrap = (level: keyof Logger, msg: string, meta?: any) => {
7
- const payload = meta ? { ...baseMeta, ...meta } : baseMeta;
8
- // 保持与 Nest Logger 接口对齐
9
- (nestLogger as any)[level]?.(msg + (Object.keys(payload).length ? ` ${JSON.stringify(payload)}` : ''));
10
- };
11
- return {
12
- child(meta) { return createPluginLogger(scope, { ...baseMeta, ...meta }); },
13
- debug: (msg, meta) => wrap('debug' as any, msg, meta),
14
- log: (msg, meta) => wrap('log', msg, meta),
15
- warn: (msg, meta) => wrap('warn', msg, meta),
16
- error: (msg, meta) => wrap('error', msg, meta),
17
- };
18
- }
@@ -1,20 +0,0 @@
1
- /**
2
- * Metadata keys used in plugins for defining various aspects like entities, subscribers, and configurations.
3
- */
4
- export const PLUGIN_METADATA = {
5
- /**
6
- * Key representing the entities registered within the plugin.
7
- */
8
- ENTITIES: 'entities',
9
-
10
- /**
11
- * Key representing event subscribers within the plugin.
12
- */
13
- SUBSCRIBERS: 'subscribers',
14
-
15
- } as const;
16
-
17
- /**
18
- * Type definition for valid plugin metadata keys.
19
- */
20
- export type PluginMetadataKey = (typeof PLUGIN_METADATA)[keyof typeof PLUGIN_METADATA];
@@ -1,67 +0,0 @@
1
- import { ModuleMetadata, Type } from '@nestjs/common';
2
-
3
- /**
4
- * Metadata definition for a plugin in NestJS.
5
- */
6
- export interface PluginMetadata extends ModuleMetadata {
7
- /**
8
- * List of entities injected by the plugin.
9
- */
10
- entities?: Array<Type<any>> | (() => Array<Type<any>>);
11
-
12
- /**
13
- * List of subscribers injected by the plugin.
14
- */
15
- subscribers?: Array<Type<any>> | (() => Array<Type<any>>);
16
- }
17
-
18
- /**
19
- * Interface for plugins with a bootstrap lifecycle method.
20
- */
21
- export interface IOnPluginBootstrap {
22
- /**
23
- * Called when the plugin is being initialized.
24
- * @returns A void or a Promise representing the completion of the operation.
25
- */
26
- onPluginBootstrap(): void | Promise<void>;
27
- }
28
-
29
- /**
30
- * Interface for plugins with a destroy lifecycle method.
31
- */
32
- export interface IOnPluginDestroy {
33
- /**
34
- * Called when the plugin is being destroyed.
35
- * @returns A void or a Promise representing the completion of the operation.
36
- */
37
- onPluginDestroy(): void | Promise<void>;
38
- }
39
-
40
- /**
41
- * Interface for plugins supporting various seed operations.
42
- */
43
- export interface IOnPluginSeedable {
44
- /**
45
- * Invoked when seeding basic plugin data.
46
- * @returns A void or a Promise representing the completion of the operation.
47
- */
48
- onPluginBasicSeed?(): void | Promise<void>;
49
-
50
- /**
51
- * Invoked when seeding default plugin data.
52
- * @returns A void or a Promise representing the completion of the operation.
53
- */
54
- onPluginDefaultSeed?(): void | Promise<void>;
55
-
56
- /**
57
- * Invoked when seeding random plugin data.
58
- * @returns A void or a Promise representing the completion of the operation.
59
- */
60
- onPluginRandomSeed?(): void | Promise<void>;
61
- }
62
-
63
- /**
64
- * Represents the combined lifecycle methods for a plugin.
65
- * This type combines interfaces for initializing and destroying a plugin.
66
- */
67
- export type PluginLifecycleMethods = IOnPluginBootstrap & IOnPluginDestroy & IOnPluginSeedable;
package/src/lib/plugin.ts DELETED
@@ -1,32 +0,0 @@
1
- import { Module } from '@nestjs/common';
2
- import { MODULE_METADATA } from '@nestjs/common/constants';
3
- import { pick } from 'underscore';
4
- import { PluginMetadata } from './plugin.interface';
5
- import { PLUGIN_METADATA } from './plugin-metadata';
6
-
7
- /**
8
- * Decorator function for extending NestJS features with additional metadata.
9
- *
10
- * @param pluginMetadata Metadata to be applied to the target class.
11
- * @returns Class decorator function.
12
- */
13
- export function XpertServerPlugin(pluginMetadata: PluginMetadata): ClassDecorator {
14
- return (targetClass) => {
15
- // Iterate over properties in PLUGIN_METADATA
16
- for (const metadataProperty of Object.values(PLUGIN_METADATA)) {
17
- const property = metadataProperty as keyof PluginMetadata;
18
-
19
- // Check if the property exists in pluginMetadata and is not undefined
20
- if (property in pluginMetadata && pluginMetadata[property] !== undefined) {
21
- // Set metadata on the target class using Reflect
22
- Reflect.defineMetadata(property, pluginMetadata[property] || [], targetClass);
23
- }
24
- }
25
-
26
- // Pick relevant metadata from pluginMetadata based on MODULE_METADATA values
27
- const metadata = pick(pluginMetadata, Object.values(MODULE_METADATA) as string[]);
28
-
29
- // Apply the Module decorator with the picked metadata
30
- Module(metadata)(targetClass);
31
- };
32
- }
@@ -1,3 +0,0 @@
1
- export * from './strategy.decorator'
2
- export * from './strategy.interface'
3
- export * from './strategy.registry'
@@ -1,9 +0,0 @@
1
- import { SetMetadata } from '@nestjs/common';
2
-
3
- export const IMAGE_UNDERSTANDING_STRATEGY = 'IMAGE_UNDERSTANDING_STRATEGY';
4
-
5
- /**
6
- * Decorator to mark a provider as an Image Understanding Strategy
7
- */
8
- export const ImageUnderstandingStrategy = (provider: string) =>
9
- SetMetadata(IMAGE_UNDERSTANDING_STRATEGY, provider);
@@ -1,42 +0,0 @@
1
- import { BaseChatModel } from '@langchain/core/language_models/chat_models'
2
- import { IDocumentUnderstandingProvider } from '@metad/contracts'
3
- import { Document } from 'langchain/document'
4
- import { Permissions, XpFileSystem } from '../../core/index'
5
- import { ChunkMetadata, TDocumentAsset } from '../types'
6
-
7
- export type TImageUnderstandingConfig = {
8
- stage: 'test' | 'prod'
9
- visionModel: BaseChatModel
10
- permissions?: {
11
- fileSystem?: XpFileSystem
12
- }
13
- }
14
-
15
- export type TImageUnderstandingInput = {
16
- chunks: Document<ChunkMetadata>[] // 来自 Loader 的初始文档块
17
- files: TDocumentAsset[] // 需要处理的图像文件
18
- }
19
-
20
- export type TImageUnderstandingResult = {
21
- chunks: Document<Partial<ChunkMetadata>>[]
22
- pages?: Document<Partial<ChunkMetadata>>[]
23
- metadata: any // 额外的元数据(例如模型名称、处理耗时)
24
- }
25
-
26
- export interface IImageUnderstandingStrategy<TConfig extends TImageUnderstandingConfig = TImageUnderstandingConfig> {
27
- readonly permissions: Permissions
28
- /**
29
- * Metadata about this strategy
30
- */
31
- readonly meta: IDocumentUnderstandingProvider
32
-
33
- /**
34
- * Validate the configuration
35
- */
36
- validateConfig(config: TConfig): Promise<void>
37
-
38
- /**
39
- * Understand image files (e.g., OCR, VLM, Chart Parsing)
40
- */
41
- understandImages(params: TImageUnderstandingInput, config: TConfig): Promise<TImageUnderstandingResult>
42
- }
@@ -1,17 +0,0 @@
1
- import { Injectable } from '@nestjs/common';
2
- import { DiscoveryService, Reflector } from '@nestjs/core';
3
- import { BaseStrategyRegistry } from '../../strategy';
4
- import { IMAGE_UNDERSTANDING_STRATEGY } from './strategy.decorator';
5
- import { IImageUnderstandingStrategy, TImageUnderstandingConfig } from './strategy.interface';
6
-
7
- @Injectable()
8
- export class ImageUnderstandingRegistry<TConfig extends TImageUnderstandingConfig = TImageUnderstandingConfig>
9
- extends BaseStrategyRegistry<IImageUnderstandingStrategy<TConfig>>
10
- {
11
- constructor(
12
- discoveryService: DiscoveryService,
13
- reflector: Reflector
14
- ) {
15
- super(IMAGE_UNDERSTANDING_STRATEGY, discoveryService, reflector);
16
- }
17
- }
@@ -1,6 +0,0 @@
1
- export * from './textsplitter/index'
2
- export * from './source/index'
3
- export * from './transformer/index'
4
- export * from './retriever/index'
5
- export * from './types'
6
- export * from './image/index'
@@ -1,3 +0,0 @@
1
- export * from './strategy.decorator'
2
- export * from './strategy.interface'
3
- export * from './strategy.registry'
@@ -1,9 +0,0 @@
1
- import { SetMetadata } from '@nestjs/common';
2
-
3
- export const RETRIEVER_STRATEGY = 'RETRIEVER_STRATEGY';
4
-
5
- /**
6
- * Decorator to mark a provider as a Retriever Strategy
7
- */
8
- export const RetrieverStrategy = (provider: string) =>
9
- SetMetadata(RETRIEVER_STRATEGY, provider);
@@ -1,32 +0,0 @@
1
- import { VectorStore } from '@langchain/core/vectorstores'
2
- import { I18nObject } from '@metad/contracts'
3
- import { Document } from 'langchain/document'
4
-
5
- export type TRetrieverConfig = {
6
- vectorStore: VectorStore
7
- }
8
-
9
- export interface IRetrieverStrategy<TConfig extends TRetrieverConfig = TRetrieverConfig> {
10
- /**
11
- * Metadata about this retriever
12
- */
13
- readonly meta: {
14
- name: string
15
- label: I18nObject
16
- configSchema: any
17
- icon: {
18
- svg?: string
19
- color?: string
20
- }
21
- }
22
-
23
- /**
24
- * Validate the configuration
25
- */
26
- validateConfig(config: TConfig): Promise<void>;
27
-
28
- /**
29
- * Retrieve relevant documents for a given query
30
- */
31
- retrieve(query: string, options?: TConfig): Promise<{ documents: Document[] }>;
32
- }
@@ -1,12 +0,0 @@
1
- import { Injectable } from '@nestjs/common'
2
- import { DiscoveryService, Reflector } from '@nestjs/core'
3
- import { BaseStrategyRegistry } from '../../strategy'
4
- import { RETRIEVER_STRATEGY } from './strategy.decorator'
5
- import { IRetrieverStrategy } from './strategy.interface'
6
-
7
- @Injectable()
8
- export class RetrieverRegistry extends BaseStrategyRegistry<IRetrieverStrategy> {
9
- constructor(discoveryService: DiscoveryService, reflector: Reflector) {
10
- super(RETRIEVER_STRATEGY, discoveryService, reflector)
11
- }
12
- }
@@ -1,3 +0,0 @@
1
- export * from './strategy.decorator'
2
- export * from './strategy.interface'
3
- export * from './strategy.registry'
@@ -1,9 +0,0 @@
1
- import { SetMetadata } from '@nestjs/common';
2
-
3
- export const DOCUMENT_SOURCE_STRATEGY = 'DOCUMENT_SOURCE_STRATEGY';
4
-
5
- /**
6
- * Decorator to mark a provider as a Document Source Strategy
7
- */
8
- export const DocumentSourceStrategy = (provider: string) =>
9
- SetMetadata(DOCUMENT_SOURCE_STRATEGY, provider);
@@ -1,28 +0,0 @@
1
- import { IDocumentSourceProvider, IIntegration } from '@metad/contracts';
2
- import { Document } from 'langchain/document';
3
- import { Permissions } from '../../core';
4
-
5
- export interface IDocumentSourceStrategy<TConfig = any> {
6
- readonly permissions: Permissions
7
- /**
8
- * Metadata about this document source
9
- */
10
- readonly meta: IDocumentSourceProvider
11
-
12
- /**
13
- * Validate the configuration
14
- */
15
- validateConfig(config: TConfig): Promise<void>;
16
-
17
- /**
18
- * Test the connection to the source
19
- *
20
- * @param config
21
- */
22
- test(config: TConfig): Promise<any>;
23
-
24
- /**
25
- * Load documents from the source
26
- */
27
- loadDocuments(config: TConfig, context?: {integration?: IIntegration}): Promise<Document[]>;
28
- }
@@ -1,17 +0,0 @@
1
- import { Injectable } from '@nestjs/common';
2
- import { DiscoveryService, Reflector } from '@nestjs/core';
3
- import { BaseStrategyRegistry } from '../../strategy';
4
- import { DOCUMENT_SOURCE_STRATEGY } from './strategy.decorator';
5
- import { IDocumentSourceStrategy } from './strategy.interface';
6
-
7
- @Injectable()
8
- export class DocumentSourceRegistry<TConfig = any>
9
- extends BaseStrategyRegistry<IDocumentSourceStrategy<TConfig>>
10
- {
11
- constructor(
12
- discoveryService: DiscoveryService,
13
- reflector: Reflector
14
- ) {
15
- super(DOCUMENT_SOURCE_STRATEGY, discoveryService, reflector);
16
- }
17
- }
@@ -1,3 +0,0 @@
1
- export * from './strategy.decorator'
2
- export * from './strategy.interface'
3
- export * from './strategy.registry'
@@ -1,6 +0,0 @@
1
- import { SetMetadata } from '@nestjs/common';
2
-
3
- export const TEXT_SPLITTER_STRATEGY = 'TEXT_SPLITTER_STRATEGY';
4
-
5
- export const TextSplitterStrategy = (provider: string) =>
6
- SetMetadata(TEXT_SPLITTER_STRATEGY, provider);
@@ -1,28 +0,0 @@
1
- import { IDocumentChunkerProvider, KnowledgeStructureEnum } from '@metad/contracts'
2
- import { Document } from 'langchain/document'
3
- import { ChunkMetadata } from '../types'
4
-
5
- /**
6
- * Split text content into chunks for embedding and retrieval
7
- */
8
- export interface ITextSplitterStrategy<TConfig = any> {
9
- /**
10
- * Metadata about this splitter
11
- */
12
- readonly meta: IDocumentChunkerProvider
13
-
14
- readonly structure: KnowledgeStructureEnum
15
-
16
- /**
17
- * Validate the configuration
18
- */
19
- validateConfig(config: TConfig): Promise<void>
20
-
21
- /**
22
- * Split a text into chunks and pages (if applicable)
23
- */
24
- splitDocuments(
25
- documents: Document[],
26
- options?: TConfig
27
- ): Promise<{ chunks: Document<ChunkMetadata>[]; pages?: Document<ChunkMetadata>[] }>
28
- }
@@ -1,17 +0,0 @@
1
- import { Injectable } from '@nestjs/common';
2
- import { DiscoveryService, Reflector } from '@nestjs/core';
3
- import { BaseStrategyRegistry } from '../../strategy';
4
- import { TEXT_SPLITTER_STRATEGY } from './strategy.decorator';
5
- import { ITextSplitterStrategy } from './strategy.interface';
6
-
7
- @Injectable()
8
- export class TextSplitterRegistry<TConfig = any>
9
- extends BaseStrategyRegistry<ITextSplitterStrategy<TConfig>>
10
- {
11
- constructor(
12
- discoveryService: DiscoveryService,
13
- reflector: Reflector
14
- ) {
15
- super(TEXT_SPLITTER_STRATEGY, discoveryService, reflector);
16
- }
17
- }
@@ -1,3 +0,0 @@
1
- export * from './strategy.decorator'
2
- export * from './strategy.interface'
3
- export * from './strategy.registry'
@@ -1,9 +0,0 @@
1
- import { SetMetadata } from '@nestjs/common';
2
-
3
- export const DOCUMENT_TRANSFORMER_STRATEGY = 'DOCUMENT_TRANSFORMER_STRATEGY';
4
-
5
- /**
6
- * Decorator to mark a provider as a Document Transformer Strategy
7
- */
8
- export const DocumentTransformerStrategy = (provider: string) =>
9
- SetMetadata(DOCUMENT_TRANSFORMER_STRATEGY, provider);
@@ -1,48 +0,0 @@
1
- import { DocumentInterface } from '@langchain/core/documents'
2
- import { _TFile, IDocumentProcessorProvider, IIntegration } from '@metad/contracts'
3
- import { Permissions, XpFileSystem } from '../../core/index'
4
- import { ChunkMetadata, TDocumentAsset } from '../types'
5
-
6
- export type TDocumentTransformerConfig = {
7
- stage: 'test' | 'prod'
8
- tempDir?: string
9
- permissions?: {
10
- fileSystem?: XpFileSystem
11
- integration?: IIntegration
12
- }
13
- }
14
-
15
- export type TDocumentTransformerFile = _TFile & {
16
- id?: string
17
- filename: string
18
- extension: string | undefined
19
- }
20
-
21
- export type TDocumentTransformerInput = TDocumentTransformerFile[] | string | string[]
22
-
23
- export type TDocumentTransformerResult = {
24
- id?: string
25
- chunks: DocumentInterface<ChunkMetadata>[]
26
- metadata: {
27
- assets?: TDocumentAsset[]
28
- }
29
- }
30
-
31
- export interface IDocumentTransformerStrategy<TConfig extends TDocumentTransformerConfig = TDocumentTransformerConfig> {
32
- /**
33
- * Metadata about this transformer
34
- */
35
- readonly meta: IDocumentProcessorProvider
36
-
37
- readonly permissions: Permissions
38
-
39
- /**
40
- * Validate the configuration
41
- */
42
- validateConfig(config: TConfig): Promise<void>
43
-
44
- /**
45
- * Transform documents (e.g., extract, OCR, normalize, enrich metadata)
46
- */
47
- transformDocuments(files: TDocumentTransformerInput, config: TConfig): Promise<TDocumentTransformerResult[]>
48
- }
@@ -1,14 +0,0 @@
1
- import { Injectable } from '@nestjs/common'
2
- import { DiscoveryService, Reflector } from '@nestjs/core'
3
- import { BaseStrategyRegistry } from '../../strategy'
4
- import { DOCUMENT_TRANSFORMER_STRATEGY } from './strategy.decorator'
5
- import { IDocumentTransformerStrategy, TDocumentTransformerConfig } from './strategy.interface'
6
-
7
- @Injectable()
8
- export class DocumentTransformerRegistry<
9
- TConfig extends TDocumentTransformerConfig = TDocumentTransformerConfig
10
- > extends BaseStrategyRegistry<IDocumentTransformerStrategy<TConfig>> {
11
- constructor(discoveryService: DiscoveryService, reflector: Reflector) {
12
- super(DOCUMENT_TRANSFORMER_STRATEGY, discoveryService, reflector)
13
- }
14
- }
@@ -1,86 +0,0 @@
1
- import { DocumentInterface } from '@langchain/core/documents'
2
- import fs from 'fs'
3
- import http from 'http'
4
- import https from 'https'
5
-
6
- export type TDocumentAsset = {
7
- type: 'image' | 'video' | 'audio' | 'file'
8
- url: string // remote url
9
- filePath: string // local path
10
- }
11
-
12
- export interface ChunkMetadata {
13
- documentId?: string // 原始文档 ID
14
- pageId?: string // 如果有分页,引用页 ID
15
- chunkId: string // 当前块的唯一 ID
16
- parentId?: string // 如果是子块,引用父块 ID
17
- chunkIndex?: number // 在文档内或在父块内的序号
18
- startOffset?: number // 原始文本起始位置
19
- endOffset?: number // 原始文本结束位置
20
- type?: 'parent' | 'child' // 分块类型
21
- children?: DocumentInterface<ChunkMetadata>[]
22
- assets?: TDocumentAsset[]
23
- [key: string]: any // 允许插件扩展
24
- }
25
-
26
- /**
27
- * Merge parent chunks with their child chunks based on metadata (parentId and chunkId)
28
- * @param chunks
29
- * @param children
30
- * @returns
31
- */
32
- export function mergeParentChildChunks(
33
- chunks: DocumentInterface<ChunkMetadata>[], // Parent chunks
34
- children: DocumentInterface<ChunkMetadata>[] // Child chunks
35
- ): DocumentInterface<ChunkMetadata>[] {
36
- const chunkMap = new Map<string, DocumentInterface<ChunkMetadata>>()
37
- for (const chunk of chunks) {
38
- chunkMap.set(chunk.metadata.chunkId, chunk)
39
- }
40
- for (const child of children) {
41
- if (!child.metadata.parentId) {
42
- if (chunkMap.has(child.metadata.chunkId)) {
43
- console.warn(`Duplicate chunkId found: ${child.metadata.chunkId}, skipping...`)
44
- continue
45
- }
46
- chunkMap.set(child.metadata.chunkId, child)
47
- continue
48
- }
49
- const parent = chunkMap.get(child.metadata.parentId)
50
- if (parent) {
51
- if (!parent.metadata.children) {
52
- parent.metadata.children = []
53
- }
54
- parent.metadata.children.push(child)
55
- }
56
- }
57
- return Array.from(chunkMap.values())
58
- }
59
-
60
- export function isRemoteFile(path: string): boolean {
61
- return path.startsWith('http://') || path.startsWith('https://') || path.startsWith('ftp://')
62
- }
63
-
64
- export async function downloadRemoteFile(url: string, dest: string): Promise<string> {
65
- return new Promise((resolve, reject) => {
66
- const file = fs.createWriteStream(dest)
67
- const client = url.startsWith('https') ? https : http
68
- const request = client.get(url, (response) => {
69
- if (response.statusCode !== 200) {
70
- reject(new Error(`Failed to get '${url}' (${response.statusCode})`))
71
- return
72
- }
73
- response.pipe(file)
74
- })
75
- file.on('finish', () => {
76
- file.close()
77
- resolve(dest)
78
- })
79
- request.on('error', (err) => {
80
- fs.unlink(dest, () => reject(err))
81
- })
82
- file.on('error', (err) => {
83
- fs.unlink(dest, () => reject(err))
84
- })
85
- })
86
- }
@@ -1,37 +0,0 @@
1
- import { OnModuleInit } from "@nestjs/common"
2
- import { DiscoveryService, Reflector } from "@nestjs/core"
3
-
4
- export class BaseStrategyRegistry<S> implements OnModuleInit {
5
- protected strategies = new Map<string, S>()
6
-
7
- constructor(
8
- protected readonly strategyKey: string,
9
- protected discoveryService: DiscoveryService,
10
- protected reflector: Reflector
11
- ) {}
12
-
13
- onModuleInit() {
14
- const providers = this.discoveryService.getProviders()
15
- for (const wrapper of providers) {
16
- const { instance } = wrapper
17
- if (!instance) continue
18
-
19
- const type = this.reflector.get<string>(this.strategyKey, instance.constructor)
20
- if (type) {
21
- this.strategies.set(type, instance as S)
22
- }
23
- }
24
- }
25
-
26
- get(type: string): S {
27
- const strategy = this.strategies.get(type)
28
- if (!strategy) {
29
- throw new Error(`No strategy found for type ${type}`)
30
- }
31
- return strategy
32
- }
33
-
34
- list(): S[] {
35
- return Array.from(this.strategies.values())
36
- }
37
- }