@xpert-ai/plugin-sdk 3.6.0-beta.6 → 3.6.0
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.
- package/.eslintrc.json +30 -0
- package/.swcrc +29 -0
- package/README.md +0 -1
- package/SCHEMA_SPECIFICATION.md +2 -0
- package/index.cjs.js +5 -56410
- package/jest.config.ts +28 -0
- package/package.json +17 -4
- package/project.json +59 -0
- package/src/index.ts +11 -0
- package/src/lib/core/file-system.ts +113 -0
- package/src/lib/core/i18n.ts +31 -0
- package/src/lib/core/index.ts +4 -0
- package/src/lib/core/permissions.ts +97 -0
- package/src/lib/core/schema.ts +23 -0
- package/src/lib/integration/index.ts +3 -0
- package/src/lib/integration/strategy.decorator.ts +6 -0
- package/src/lib/integration/strategy.interface.ts +11 -0
- package/src/lib/integration/strategy.registry.ts +12 -0
- package/src/lib/logger.ts +18 -0
- package/src/lib/plugin-metadata.ts +20 -0
- package/src/lib/{plugin.hook.d.ts → plugin.hook.ts} +4 -0
- package/src/lib/plugin.interface.ts +67 -0
- package/src/lib/plugin.ts +32 -0
- package/src/lib/rag/image/index.ts +3 -0
- package/src/lib/rag/image/strategy.decorator.ts +9 -0
- package/src/lib/rag/image/strategy.interface.ts +37 -0
- package/src/lib/rag/image/strategy.registry.ts +17 -0
- package/src/lib/rag/index.ts +7 -0
- package/src/lib/rag/knowledge/index.ts +3 -0
- package/src/lib/rag/knowledge/knowledge-strategy.decorator.ts +6 -0
- package/src/lib/rag/knowledge/knowledge-strategy.interface.ts +15 -0
- package/src/lib/rag/knowledge/knowledge-strategy.registry.ts +12 -0
- package/src/lib/rag/retriever/index.ts +3 -0
- package/src/lib/rag/retriever/strategy.decorator.ts +9 -0
- package/src/lib/rag/retriever/strategy.interface.ts +32 -0
- package/src/lib/rag/retriever/strategy.registry.ts +12 -0
- package/src/lib/rag/source/index.ts +3 -0
- package/src/lib/rag/source/strategy.decorator.ts +9 -0
- package/src/lib/rag/source/strategy.interface.ts +36 -0
- package/src/lib/rag/source/strategy.registry.ts +17 -0
- package/src/lib/rag/textsplitter/index.ts +3 -0
- package/src/lib/rag/textsplitter/strategy.decorator.ts +6 -0
- package/src/lib/rag/textsplitter/strategy.interface.ts +28 -0
- package/src/lib/rag/textsplitter/strategy.registry.ts +17 -0
- package/src/lib/rag/transformer/index.ts +3 -0
- package/src/lib/rag/transformer/strategy.decorator.ts +9 -0
- package/src/lib/rag/transformer/strategy.interface.ts +34 -0
- package/src/lib/rag/transformer/strategy.registry.ts +14 -0
- package/src/lib/rag/types.ts +76 -0
- package/src/lib/strategy.ts +37 -0
- package/src/lib/toolset/builtin.ts +111 -0
- package/src/lib/toolset/index.ts +5 -0
- package/src/lib/toolset/strategy.decorator.ts +9 -0
- package/src/lib/toolset/strategy.interface.ts +32 -0
- package/src/lib/toolset/strategy.registry.ts +17 -0
- package/src/lib/toolset/toolset.ts +76 -0
- package/src/lib/types.ts +47 -0
- package/src/lib/vectorstore/index.ts +3 -0
- package/src/lib/vectorstore/strategy.decorator.ts +6 -0
- package/src/lib/vectorstore/strategy.interface.ts +25 -0
- package/src/lib/vectorstore/strategy.registry.ts +17 -0
- package/src/lib/workflow/index.ts +2 -0
- package/src/lib/workflow/node/index.ts +3 -0
- package/src/lib/workflow/node/strategy.decorator.ts +9 -0
- package/src/lib/workflow/node/strategy.interface.ts +51 -0
- package/src/lib/workflow/node/strategy.registry.ts +18 -0
- package/src/lib/workflow/trigger/index.ts +3 -0
- package/src/lib/workflow/trigger/strategy.decorator.ts +6 -0
- package/src/lib/workflow/trigger/strategy.interface.ts +27 -0
- package/src/lib/workflow/trigger/strategy.registry.ts +17 -0
- package/tsconfig.json +22 -0
- package/tsconfig.lib.json +10 -0
- package/tsconfig.spec.json +9 -0
- package/index.cjs.d.ts +0 -1
- package/index.esm.d.ts +0 -1
- package/index.esm.js +0 -56377
- package/src/index.d.ts +0 -12
- package/src/lib/core/file-system.d.ts +0 -42
- package/src/lib/core/i18n.d.ts +0 -2
- package/src/lib/core/index.d.ts +0 -4
- package/src/lib/core/permissions.d.ts +0 -74
- package/src/lib/core/schema.d.ts +0 -22
- package/src/lib/integration/index.d.ts +0 -3
- package/src/lib/integration/strategy.decorator.d.ts +0 -2
- package/src/lib/integration/strategy.interface.d.ts +0 -9
- package/src/lib/integration/strategy.registry.d.ts +0 -6
- package/src/lib/knowledge/index.d.ts +0 -3
- package/src/lib/knowledge/knowledge-strategy.decorator.d.ts +0 -2
- package/src/lib/knowledge/knowledge-strategy.interface.d.ts +0 -15
- package/src/lib/knowledge/knowledge-strategy.registry.d.ts +0 -12
- package/src/lib/logger.d.ts +0 -2
- package/src/lib/plugin-metadata.d.ts +0 -17
- package/src/lib/plugin.d.ts +0 -8
- package/src/lib/plugin.interface.d.ts +0 -59
- package/src/lib/rag/image/index.d.ts +0 -3
- package/src/lib/rag/image/strategy.decorator.d.ts +0 -5
- package/src/lib/rag/image/strategy.interface.d.ts +0 -36
- package/src/lib/rag/image/strategy.registry.d.ts +0 -6
- package/src/lib/rag/index.d.ts +0 -6
- package/src/lib/rag/retriever/index.d.ts +0 -3
- package/src/lib/rag/retriever/strategy.decorator.d.ts +0 -5
- package/src/lib/rag/retriever/strategy.interface.d.ts +0 -30
- package/src/lib/rag/retriever/strategy.registry.d.ts +0 -6
- package/src/lib/rag/source/index.d.ts +0 -3
- package/src/lib/rag/source/strategy.decorator.d.ts +0 -5
- package/src/lib/rag/source/strategy.interface.d.ts +0 -36
- package/src/lib/rag/source/strategy.registry.d.ts +0 -6
- package/src/lib/rag/textsplitter/index.d.ts +0 -3
- package/src/lib/rag/textsplitter/strategy.decorator.d.ts +0 -2
- package/src/lib/rag/textsplitter/strategy.interface.d.ts +0 -24
- package/src/lib/rag/textsplitter/strategy.registry.d.ts +0 -6
- package/src/lib/rag/transformer/index.d.ts +0 -3
- package/src/lib/rag/transformer/strategy.decorator.d.ts +0 -5
- package/src/lib/rag/transformer/strategy.interface.d.ts +0 -25
- package/src/lib/rag/transformer/strategy.registry.d.ts +0 -6
- package/src/lib/rag/types.d.ts +0 -29
- package/src/lib/strategy.d.ts +0 -12
- package/src/lib/toolset/builtin.d.ts +0 -54
- package/src/lib/toolset/index.d.ts +0 -5
- package/src/lib/toolset/strategy.decorator.d.ts +0 -5
- package/src/lib/toolset/strategy.interface.d.ts +0 -28
- package/src/lib/toolset/strategy.registry.d.ts +0 -6
- package/src/lib/toolset/toolset.d.ts +0 -53
- package/src/lib/types.d.ts +0 -47
- package/src/lib/vectorstore/index.d.ts +0 -3
- package/src/lib/vectorstore/strategy.decorator.d.ts +0 -2
- package/src/lib/vectorstore/strategy.interface.d.ts +0 -24
- package/src/lib/vectorstore/strategy.registry.d.ts +0 -6
- package/src/lib/workflow/index.d.ts +0 -2
- package/src/lib/workflow/node/index.d.ts +0 -3
- package/src/lib/workflow/node/strategy.decorator.d.ts +0 -5
- package/src/lib/workflow/node/strategy.interface.d.ts +0 -40
- package/src/lib/workflow/node/strategy.registry.d.ts +0 -9
- package/src/lib/workflow/trigger/index.d.ts +0 -3
- package/src/lib/workflow/trigger/strategy.decorator.d.ts +0 -2
- package/src/lib/workflow/trigger/strategy.interface.d.ts +0 -22
- package/src/lib/workflow/trigger/strategy.registry.d.ts +0 -6
|
@@ -0,0 +1,17 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { IIntegration } from '@metad/contracts'
|
|
2
|
+
import { Document } from '@langchain/core/documents'
|
|
3
|
+
|
|
4
|
+
export type TKnowledgeStrategyParams = {
|
|
5
|
+
query: string;
|
|
6
|
+
k: number;
|
|
7
|
+
filter: Record<string, any>
|
|
8
|
+
options: {
|
|
9
|
+
knowledgebaseId: string
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface KnowledgeStrategy {
|
|
14
|
+
execute(integration: IIntegration, payload: TKnowledgeStrategyParams): Promise<{ chunks: [Document, number][] }>
|
|
15
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Injectable } from '@nestjs/common'
|
|
2
|
+
import { DiscoveryService, Reflector } from '@nestjs/core'
|
|
3
|
+
import { BaseStrategyRegistry } from '../../strategy'
|
|
4
|
+
import { KNOWLEDGE_STRATEGY } from './knowledge-strategy.decorator'
|
|
5
|
+
import { KnowledgeStrategy } from './knowledge-strategy.interface'
|
|
6
|
+
|
|
7
|
+
@Injectable()
|
|
8
|
+
export class KnowledgeStrategyRegistry extends BaseStrategyRegistry<KnowledgeStrategy> {
|
|
9
|
+
constructor(discoveryService: DiscoveryService, reflector: Reflector) {
|
|
10
|
+
super(KNOWLEDGE_STRATEGY, discoveryService, reflector)
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
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);
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { VectorStore } from '@langchain/core/vectorstores'
|
|
2
|
+
import { I18nObject } from '@metad/contracts'
|
|
3
|
+
import { Document } from '@langchain/core/documents'
|
|
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
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
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);
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { IDocumentSourceProvider, IIntegration } from '@metad/contracts';
|
|
2
|
+
import { Document } from '@langchain/core/documents';
|
|
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
|
+
/**
|
|
29
|
+
* Load a single document by its ID
|
|
30
|
+
*
|
|
31
|
+
* @deprecated Planning
|
|
32
|
+
* @param documentId
|
|
33
|
+
* @param context
|
|
34
|
+
*/
|
|
35
|
+
loadDocument?(document: Document, context: {integration?: IIntegration}): Promise<Document>;
|
|
36
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { IDocumentChunkerProvider, KnowledgeStructureEnum } from '@metad/contracts'
|
|
2
|
+
import { DocumentInterface } from '@langchain/core/documents'
|
|
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: DocumentInterface[],
|
|
26
|
+
options?: TConfig
|
|
27
|
+
): Promise<{ chunks: DocumentInterface<ChunkMetadata>[] }>
|
|
28
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
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);
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { IDocumentProcessorProvider, IIntegration, IKnowledgeDocument } from '@metad/contracts'
|
|
2
|
+
import { Permissions, XpFileSystem } from '../../core/index'
|
|
3
|
+
import { ChunkMetadata } from '../types'
|
|
4
|
+
|
|
5
|
+
export type TDocumentTransformerConfig = {
|
|
6
|
+
stage: 'test' | 'prod'
|
|
7
|
+
tempDir?: string
|
|
8
|
+
permissions?: {
|
|
9
|
+
fileSystem?: XpFileSystem
|
|
10
|
+
integration?: IIntegration
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface IDocumentTransformerStrategy<TConfig extends TDocumentTransformerConfig = TDocumentTransformerConfig> {
|
|
15
|
+
/**
|
|
16
|
+
* Metadata about this transformer
|
|
17
|
+
*/
|
|
18
|
+
readonly meta: IDocumentProcessorProvider
|
|
19
|
+
|
|
20
|
+
readonly permissions: Permissions
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Validate the configuration
|
|
24
|
+
*/
|
|
25
|
+
validateConfig(config: TConfig): Promise<void>
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Transform documents (e.g., extract, OCR, normalize, enrich metadata)
|
|
29
|
+
*/
|
|
30
|
+
transformDocuments(
|
|
31
|
+
files: Partial<IKnowledgeDocument>[],
|
|
32
|
+
config: TConfig
|
|
33
|
+
): Promise<Partial<IKnowledgeDocument<ChunkMetadata>>[]>
|
|
34
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { DocumentInterface } from '@langchain/core/documents'
|
|
2
|
+
import { IDocChunkMetadata } from '@metad/contracts'
|
|
3
|
+
import fs from 'fs'
|
|
4
|
+
import http from 'http'
|
|
5
|
+
import https from 'https'
|
|
6
|
+
|
|
7
|
+
export { TDocumentAsset } from '@metad/contracts'
|
|
8
|
+
|
|
9
|
+
export interface ChunkMetadata extends IDocChunkMetadata{
|
|
10
|
+
startOffset?: number // Start position in the original text
|
|
11
|
+
endOffset?: number // End position in the original text
|
|
12
|
+
type?: 'parent' | 'child' // Chunk type
|
|
13
|
+
|
|
14
|
+
[key: string]: any // Allow plugin extensions
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Merge parent chunks with their child chunks based on metadata (parentId and chunkId)
|
|
19
|
+
*
|
|
20
|
+
* @deprecated use buildChunkTreeAndFindLeaves instead
|
|
21
|
+
*/
|
|
22
|
+
export function mergeParentChildChunks(
|
|
23
|
+
chunks: DocumentInterface<ChunkMetadata>[], // Parent chunks
|
|
24
|
+
children: DocumentInterface<ChunkMetadata>[] // Child chunks
|
|
25
|
+
): DocumentInterface<ChunkMetadata>[] {
|
|
26
|
+
const chunkMap = new Map<string, DocumentInterface<ChunkMetadata>>()
|
|
27
|
+
for (const chunk of chunks) {
|
|
28
|
+
chunkMap.set(chunk.metadata.chunkId, chunk)
|
|
29
|
+
}
|
|
30
|
+
for (const child of children) {
|
|
31
|
+
if (!child.metadata.parentId) {
|
|
32
|
+
if (chunkMap.has(child.metadata.chunkId)) {
|
|
33
|
+
console.warn(`Duplicate chunkId found: ${child.metadata.chunkId}, skipping...`)
|
|
34
|
+
continue
|
|
35
|
+
}
|
|
36
|
+
chunkMap.set(child.metadata.chunkId, child)
|
|
37
|
+
continue
|
|
38
|
+
}
|
|
39
|
+
const parent = chunkMap.get(child.metadata.parentId)
|
|
40
|
+
if (parent) {
|
|
41
|
+
if (!parent.metadata.children) {
|
|
42
|
+
parent.metadata.children = []
|
|
43
|
+
}
|
|
44
|
+
parent.metadata.children.push(child)
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return Array.from(chunkMap.values())
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function isRemoteFile(path: string): boolean {
|
|
51
|
+
return path.startsWith('http://') || path.startsWith('https://') || path.startsWith('ftp://')
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export async function downloadRemoteFile(url: string, dest: string): Promise<string> {
|
|
55
|
+
return new Promise((resolve, reject) => {
|
|
56
|
+
const file = fs.createWriteStream(dest)
|
|
57
|
+
const client = url.startsWith('https') ? https : http
|
|
58
|
+
const request = client.get(url, (response) => {
|
|
59
|
+
if (response.statusCode !== 200) {
|
|
60
|
+
reject(new Error(`Failed to get '${url}' (${response.statusCode})`))
|
|
61
|
+
return
|
|
62
|
+
}
|
|
63
|
+
response.pipe(file)
|
|
64
|
+
})
|
|
65
|
+
file.on('finish', () => {
|
|
66
|
+
file.close()
|
|
67
|
+
resolve(dest)
|
|
68
|
+
})
|
|
69
|
+
request.on('error', (err) => {
|
|
70
|
+
fs.unlink(dest, () => reject(err))
|
|
71
|
+
})
|
|
72
|
+
file.on('error', (err) => {
|
|
73
|
+
fs.unlink(dest, () => reject(err))
|
|
74
|
+
})
|
|
75
|
+
})
|
|
76
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { StructuredToolInterface } from '@langchain/core/tools'
|
|
2
|
+
import { BaseStore } from '@langchain/langgraph'
|
|
3
|
+
import {
|
|
4
|
+
I18nObject,
|
|
5
|
+
IBuiltinTool,
|
|
6
|
+
IXpertToolset,
|
|
7
|
+
ToolProviderCredentials,
|
|
8
|
+
TToolCredentials,
|
|
9
|
+
TToolsetParams,
|
|
10
|
+
XpertToolsetCategoryEnum
|
|
11
|
+
} from '@metad/contracts'
|
|
12
|
+
import { Logger } from '@nestjs/common'
|
|
13
|
+
import { CommandBus, QueryBus } from '@nestjs/cqrs'
|
|
14
|
+
import { BaseToolset } from './toolset'
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The context params of creating toolset
|
|
18
|
+
*/
|
|
19
|
+
export type TBuiltinToolsetParams = TToolsetParams & {
|
|
20
|
+
commandBus: CommandBus
|
|
21
|
+
queryBus: QueryBus
|
|
22
|
+
store?: BaseStore
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface IBuiltinToolset {
|
|
26
|
+
validateCredentials(credentials: TToolCredentials): Promise<void>
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export abstract class BuiltinToolset<T extends StructuredToolInterface = StructuredToolInterface, C = TToolCredentials>
|
|
30
|
+
extends BaseToolset<T>
|
|
31
|
+
implements IBuiltinToolset
|
|
32
|
+
{
|
|
33
|
+
static provider = ''
|
|
34
|
+
protected logger = new Logger(this.constructor.name)
|
|
35
|
+
|
|
36
|
+
providerType: XpertToolsetCategoryEnum.BUILTIN
|
|
37
|
+
|
|
38
|
+
credentialsSchema?: { [key: string]: ToolProviderCredentials }
|
|
39
|
+
|
|
40
|
+
get tenantId() {
|
|
41
|
+
return this.params?.tenantId
|
|
42
|
+
}
|
|
43
|
+
get organizationId() {
|
|
44
|
+
return this.params?.organizationId
|
|
45
|
+
}
|
|
46
|
+
get commandBus() {
|
|
47
|
+
return this.params?.commandBus
|
|
48
|
+
}
|
|
49
|
+
get queryBus() {
|
|
50
|
+
return this.params?.queryBus
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
get xpertId() {
|
|
54
|
+
return this.params?.xpertId
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
constructor(
|
|
58
|
+
public providerName: string,
|
|
59
|
+
protected toolset?: IXpertToolset,
|
|
60
|
+
protected override params?: TBuiltinToolsetParams
|
|
61
|
+
) {
|
|
62
|
+
super(params)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async validateCredentials(credentials: C): Promise<void> {
|
|
66
|
+
await this._validateCredentials(credentials)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async _validateCredentials(credentials: C) {
|
|
70
|
+
throw new Error('Method not implemented.')
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
getId() {
|
|
74
|
+
return this.toolset?.id
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
getCredentials() {
|
|
78
|
+
return this.toolset?.credentials as C
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
getToolTitle(name: string): string | I18nObject {
|
|
82
|
+
const tool = this.toolset?.tools?.find((tool) => tool.name === name)
|
|
83
|
+
const identity = (<IBuiltinTool>tool?.schema)?.identity
|
|
84
|
+
if (identity) {
|
|
85
|
+
return identity.label
|
|
86
|
+
}
|
|
87
|
+
return null
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Get credentials schema
|
|
92
|
+
*
|
|
93
|
+
* @returns Credentials schema
|
|
94
|
+
*/
|
|
95
|
+
getCredentialsSchema(): { [key: string]: ToolProviderCredentials } {
|
|
96
|
+
return { ...this.credentialsSchema }
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Get toolset entity
|
|
101
|
+
*
|
|
102
|
+
* @returns XpertToolset
|
|
103
|
+
*/
|
|
104
|
+
getToolset() {
|
|
105
|
+
return this.toolset
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
getName() {
|
|
109
|
+
return this.getToolset()?.name
|
|
110
|
+
}
|
|
111
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { SetMetadata } from '@nestjs/common';
|
|
2
|
+
|
|
3
|
+
export const TOOLSET_STRATEGY = 'TOOLSET_STRATEGY';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Decorator to mark a provider as a Toolset Strategy
|
|
7
|
+
*/
|
|
8
|
+
export const ToolsetStrategy = (provider: string) =>
|
|
9
|
+
SetMetadata(TOOLSET_STRATEGY, provider);
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { DynamicStructuredTool } from '@langchain/core/tools'
|
|
2
|
+
import { I18nObject } from '@metad/contracts'
|
|
3
|
+
import { ZodSchema } from 'zod'
|
|
4
|
+
import { BuiltinToolset } from './builtin'
|
|
5
|
+
|
|
6
|
+
export interface IToolsetStrategy<TConfig = any> {
|
|
7
|
+
/**
|
|
8
|
+
* Metadata about this toolset
|
|
9
|
+
*/
|
|
10
|
+
readonly meta: {
|
|
11
|
+
author: string // author name
|
|
12
|
+
tags: string[] // tags for categorization
|
|
13
|
+
name: string // unique key
|
|
14
|
+
label: I18nObject // i18n label
|
|
15
|
+
description?: I18nObject // optional i18n description
|
|
16
|
+
configSchema: any // JSON schema for config validation
|
|
17
|
+
icon?: {
|
|
18
|
+
svg?: string
|
|
19
|
+
png?: string // base64 encoded png image
|
|
20
|
+
color?: string
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Validate the configuration
|
|
26
|
+
*/
|
|
27
|
+
validateConfig(config: TConfig): Promise<void>
|
|
28
|
+
|
|
29
|
+
create(config: TConfig): Promise<BuiltinToolset>
|
|
30
|
+
|
|
31
|
+
createTools(): DynamicStructuredTool<ZodSchema>[]
|
|
32
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Injectable } from '@nestjs/common';
|
|
2
|
+
import { DiscoveryService, Reflector } from '@nestjs/core';
|
|
3
|
+
import { TOOLSET_STRATEGY } from './strategy.decorator';
|
|
4
|
+
import { IToolsetStrategy } from './strategy.interface';
|
|
5
|
+
import { BaseStrategyRegistry } from '../strategy';
|
|
6
|
+
|
|
7
|
+
@Injectable()
|
|
8
|
+
export class ToolsetRegistry<TConfig = any>
|
|
9
|
+
extends BaseStrategyRegistry<IToolsetStrategy<TConfig>>
|
|
10
|
+
{
|
|
11
|
+
constructor(
|
|
12
|
+
discoveryService: DiscoveryService,
|
|
13
|
+
reflector: Reflector
|
|
14
|
+
) {
|
|
15
|
+
super(TOOLSET_STRATEGY, discoveryService, reflector);
|
|
16
|
+
}
|
|
17
|
+
}
|