@xpert-ai/plugin-sdk 0.0.1 → 0.0.3-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/README.md +1 -2
- package/index.cjs.d.ts +1 -0
- package/index.cjs.js +56393 -0
- package/index.esm.d.ts +1 -0
- package/index.esm.js +56360 -0
- package/package.json +3 -3
- package/src/index.d.ts +12 -0
- package/src/lib/core/file-system.d.ts +42 -0
- package/src/lib/core/index.d.ts +3 -0
- package/src/lib/core/permissions.d.ts +74 -0
- package/src/lib/core/schema.d.ts +22 -0
- package/src/lib/integration/index.d.ts +3 -0
- package/src/lib/integration/strategy.decorator.d.ts +2 -0
- package/src/lib/integration/{strategy.interface.ts → strategy.interface.d.ts} +5 -7
- package/src/lib/integration/strategy.registry.d.ts +6 -0
- package/src/lib/knowledge/index.d.ts +3 -0
- package/src/lib/knowledge/knowledge-strategy.decorator.d.ts +2 -0
- package/src/lib/knowledge/knowledge-strategy.interface.d.ts +15 -0
- package/src/lib/knowledge/knowledge-strategy.registry.d.ts +12 -0
- package/src/lib/logger.d.ts +2 -0
- package/src/lib/plugin-metadata.d.ts +17 -0
- package/src/lib/plugin.d.ts +8 -0
- package/src/lib/{plugin.hook.ts → plugin.hook.d.ts} +0 -4
- package/src/lib/plugin.interface.d.ts +59 -0
- package/src/lib/rag/image/index.d.ts +3 -0
- package/src/lib/rag/image/strategy.decorator.d.ts +5 -0
- package/src/lib/rag/image/strategy.interface.d.ts +36 -0
- package/src/lib/rag/image/strategy.registry.d.ts +6 -0
- package/src/lib/rag/index.d.ts +6 -0
- package/src/lib/rag/retriever/index.d.ts +3 -0
- package/src/lib/rag/retriever/strategy.decorator.d.ts +5 -0
- package/src/lib/rag/retriever/strategy.interface.d.ts +30 -0
- package/src/lib/rag/retriever/strategy.registry.d.ts +6 -0
- package/src/lib/rag/source/index.d.ts +3 -0
- package/src/lib/rag/source/strategy.decorator.d.ts +5 -0
- package/src/lib/rag/source/strategy.interface.d.ts +26 -0
- package/src/lib/rag/source/strategy.registry.d.ts +6 -0
- package/src/lib/rag/textsplitter/index.d.ts +3 -0
- package/src/lib/rag/textsplitter/strategy.decorator.d.ts +2 -0
- package/src/lib/rag/textsplitter/strategy.interface.d.ts +24 -0
- package/src/lib/rag/textsplitter/strategy.registry.d.ts +6 -0
- package/src/lib/rag/transformer/index.d.ts +3 -0
- package/src/lib/rag/transformer/strategy.decorator.d.ts +5 -0
- package/src/lib/rag/transformer/strategy.interface.d.ts +40 -0
- package/src/lib/rag/transformer/strategy.registry.d.ts +6 -0
- package/src/lib/rag/types.d.ts +29 -0
- package/src/lib/strategy.d.ts +12 -0
- package/src/lib/toolset/builtin.d.ts +54 -0
- package/src/lib/toolset/index.d.ts +5 -0
- package/src/lib/toolset/strategy.decorator.d.ts +5 -0
- package/src/lib/toolset/strategy.interface.d.ts +28 -0
- package/src/lib/toolset/strategy.registry.d.ts +6 -0
- package/src/lib/toolset/toolset.d.ts +53 -0
- package/src/lib/types.d.ts +47 -0
- package/src/lib/vectorstore/index.d.ts +3 -0
- package/src/lib/vectorstore/strategy.decorator.d.ts +2 -0
- package/src/lib/vectorstore/strategy.interface.d.ts +24 -0
- package/src/lib/vectorstore/strategy.registry.d.ts +6 -0
- package/src/lib/workflow/index.d.ts +2 -0
- package/src/lib/workflow/node/index.d.ts +3 -0
- package/src/lib/workflow/node/strategy.decorator.d.ts +5 -0
- package/src/lib/workflow/node/strategy.interface.d.ts +40 -0
- package/src/lib/workflow/node/strategy.registry.d.ts +9 -0
- package/src/lib/workflow/trigger/index.d.ts +3 -0
- package/src/lib/workflow/trigger/strategy.decorator.d.ts +2 -0
- package/src/lib/workflow/trigger/strategy.interface.d.ts +22 -0
- package/src/lib/workflow/trigger/strategy.registry.d.ts +6 -0
- package/.eslintrc.json +0 -30
- package/.swcrc +0 -29
- package/jest.config.ts +0 -28
- package/project.json +0 -29
- package/src/index.ts +0 -12
- package/src/lib/core/file-system.ts +0 -96
- package/src/lib/core/index.ts +0 -3
- package/src/lib/core/permissions.ts +0 -97
- package/src/lib/core/schema.ts +0 -23
- package/src/lib/integration/index.ts +0 -3
- package/src/lib/integration/strategy.decorator.ts +0 -6
- package/src/lib/integration/strategy.registry.ts +0 -12
- package/src/lib/knowledge/index.ts +0 -3
- package/src/lib/knowledge/knowledge-strategy.decorator.ts +0 -6
- package/src/lib/knowledge/knowledge-strategy.interface.ts +0 -15
- package/src/lib/knowledge/knowledge-strategy.registry.ts +0 -36
- package/src/lib/logger.ts +0 -18
- package/src/lib/plugin-metadata.ts +0 -20
- package/src/lib/plugin.interface.ts +0 -67
- package/src/lib/plugin.ts +0 -32
- package/src/lib/rag/image/index.ts +0 -3
- package/src/lib/rag/image/strategy.decorator.ts +0 -9
- package/src/lib/rag/image/strategy.interface.ts +0 -42
- package/src/lib/rag/image/strategy.registry.ts +0 -17
- package/src/lib/rag/index.ts +0 -6
- package/src/lib/rag/retriever/index.ts +0 -3
- package/src/lib/rag/retriever/strategy.decorator.ts +0 -9
- package/src/lib/rag/retriever/strategy.interface.ts +0 -32
- package/src/lib/rag/retriever/strategy.registry.ts +0 -12
- package/src/lib/rag/source/index.ts +0 -3
- package/src/lib/rag/source/strategy.decorator.ts +0 -9
- package/src/lib/rag/source/strategy.interface.ts +0 -28
- package/src/lib/rag/source/strategy.registry.ts +0 -17
- package/src/lib/rag/textsplitter/index.ts +0 -3
- package/src/lib/rag/textsplitter/strategy.decorator.ts +0 -6
- package/src/lib/rag/textsplitter/strategy.interface.ts +0 -28
- package/src/lib/rag/textsplitter/strategy.registry.ts +0 -17
- package/src/lib/rag/transformer/index.ts +0 -3
- package/src/lib/rag/transformer/strategy.decorator.ts +0 -9
- package/src/lib/rag/transformer/strategy.interface.ts +0 -48
- package/src/lib/rag/transformer/strategy.registry.ts +0 -14
- package/src/lib/rag/types.ts +0 -86
- package/src/lib/strategy.ts +0 -37
- package/src/lib/toolset/builtin.ts +0 -111
- package/src/lib/toolset/index.ts +0 -5
- package/src/lib/toolset/strategy.decorator.ts +0 -9
- package/src/lib/toolset/strategy.interface.ts +0 -32
- package/src/lib/toolset/strategy.registry.ts +0 -17
- package/src/lib/toolset/toolset.ts +0 -76
- package/src/lib/types.ts +0 -47
- package/src/lib/vectorstore/index.ts +0 -3
- package/src/lib/vectorstore/strategy.decorator.ts +0 -6
- package/src/lib/vectorstore/strategy.interface.ts +0 -25
- package/src/lib/vectorstore/strategy.registry.ts +0 -17
- package/src/lib/workflow/index.ts +0 -2
- package/src/lib/workflow/node/index.ts +0 -3
- package/src/lib/workflow/node/strategy.decorator.ts +0 -9
- package/src/lib/workflow/node/strategy.interface.ts +0 -49
- package/src/lib/workflow/node/strategy.registry.ts +0 -18
- package/src/lib/workflow/trigger/index.ts +0 -3
- package/src/lib/workflow/trigger/strategy.decorator.ts +0 -6
- package/src/lib/workflow/trigger/strategy.interface.ts +0 -27
- package/src/lib/workflow/trigger/strategy.registry.ts +0 -17
- package/tsconfig.json +0 -22
- package/tsconfig.lib.json +0 -10
- package/tsconfig.spec.json +0 -9
|
@@ -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
|
-
}
|
package/src/lib/rag/types.ts
DELETED
|
@@ -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
|
-
}
|
package/src/lib/strategy.ts
DELETED
|
@@ -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
|
-
}
|
|
@@ -1,111 +0,0 @@
|
|
|
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
|
-
}
|
package/src/lib/toolset/index.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
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);
|
|
@@ -1,32 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import { BaseToolkit, StructuredTool, StructuredToolInterface } from "@langchain/core/tools"
|
|
2
|
-
import { I18nObject, TStateVariable, TToolsetParams } from "@metad/contracts"
|
|
3
|
-
import { z } from 'zod'
|
|
4
|
-
|
|
5
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
6
|
-
type ZodObjectAny = z.ZodObject<any, any, any, any>;
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Base ability for all toolsets
|
|
10
|
-
*/
|
|
11
|
-
export abstract class BaseToolset<T extends StructuredToolInterface = StructuredToolInterface> extends BaseToolkit {
|
|
12
|
-
abstract providerName: string
|
|
13
|
-
// For Langchain
|
|
14
|
-
tools: T[]
|
|
15
|
-
// For Langgraph
|
|
16
|
-
stateVariables: TStateVariable[]
|
|
17
|
-
|
|
18
|
-
constructor(protected params?: TToolsetParams) {
|
|
19
|
-
super()
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Async init tools
|
|
24
|
-
*
|
|
25
|
-
* @returns
|
|
26
|
-
*/
|
|
27
|
-
async initTools() {
|
|
28
|
-
return this.tools
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Get ID of the toolset
|
|
33
|
-
*/
|
|
34
|
-
abstract getId(): string
|
|
35
|
-
/**
|
|
36
|
-
* Get name of the toolset
|
|
37
|
-
*/
|
|
38
|
-
abstract getName(): string
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Get one tool
|
|
42
|
-
*
|
|
43
|
-
* @param toolName
|
|
44
|
-
* @returns
|
|
45
|
-
*/
|
|
46
|
-
getTool(toolName: string) {
|
|
47
|
-
return this.getTools()?.find((tool) => tool.name === toolName)
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Get state variables config
|
|
52
|
-
*
|
|
53
|
-
* @returns State variables
|
|
54
|
-
*/
|
|
55
|
-
async getVariables(): Promise<TStateVariable[]> {
|
|
56
|
-
return null
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Get title of tool
|
|
61
|
-
*/
|
|
62
|
-
abstract getToolTitle(name: string): string | I18nObject
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Close all (connections).
|
|
66
|
-
*/
|
|
67
|
-
async close(): Promise<void> {
|
|
68
|
-
//
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
export abstract class BaseTool<T extends ZodObjectAny = ZodObjectAny> extends StructuredTool<T> {
|
|
73
|
-
schema: any = z
|
|
74
|
-
.object({ input: z.string().optional() })
|
|
75
|
-
.transform((obj) => obj.input)
|
|
76
|
-
}
|
package/src/lib/types.ts
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { PluginMeta } from '@metad/contracts';
|
|
2
|
-
import type { DynamicModule, INestApplicationContext } from '@nestjs/common';
|
|
3
|
-
import type { ZodSchema } from 'zod';
|
|
4
|
-
|
|
5
|
-
export interface PluginLifecycle {
|
|
6
|
-
/** 在模块注册完成但应用启动前调用 */
|
|
7
|
-
onInit?(ctx: PluginContext): Promise<void> | void;
|
|
8
|
-
/** 在应用启动后调用(可开始对外服务) */
|
|
9
|
-
onStart?(ctx: PluginContext): Promise<void> | void;
|
|
10
|
-
/** 优雅停机时调用 */
|
|
11
|
-
onStop?(ctx: PluginContext): Promise<void> | void;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export interface PluginHealth {
|
|
15
|
-
/** 返回健康状态与可选的依赖检查详情 */
|
|
16
|
-
checkHealth?(ctx: PluginContext): Promise<{ status: 'up' | 'down'; details?: any }> | { status: 'up' | 'down'; details?: any };
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export interface PluginConfigSpec<T extends object = any> {
|
|
20
|
-
/** 插件级配置的 zod 校验 schema(可选) */
|
|
21
|
-
schema?: ZodSchema<T>;
|
|
22
|
-
/** 默认配置 */
|
|
23
|
-
defaults?: Partial<T>;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export interface XpertPlugin<TConfig extends object = any> extends PluginLifecycle, PluginHealth {
|
|
27
|
-
meta: PluginMeta;
|
|
28
|
-
config?: PluginConfigSpec<TConfig>;
|
|
29
|
-
/** 返回要挂载到主应用的 DynamicModule(可设为 global) */
|
|
30
|
-
register(ctx: PluginContext<TConfig>): DynamicModule;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export interface PluginContext<TConfig extends object = any> {
|
|
34
|
-
app: INestApplicationContext; // Nest 运行时上下文(启动后注入)
|
|
35
|
-
logger: PluginLogger; // SDK 提供的 Logger 包装
|
|
36
|
-
config: TConfig; // 校验合并后的最终配置
|
|
37
|
-
/** 访问容器中其他 Provider 的辅助方法 */
|
|
38
|
-
resolve<TInput = any, TResult = TInput>(token: any): TResult;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export interface PluginLogger {
|
|
42
|
-
child(meta: Record<string, any>): PluginLogger;
|
|
43
|
-
debug(msg: string, meta?: any): void;
|
|
44
|
-
log(msg: string, meta?: any): void;
|
|
45
|
-
warn(msg: string, meta?: any): void;
|
|
46
|
-
error(msg: string, meta?: any): void;
|
|
47
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { EmbeddingsInterface } from '@langchain/core/embeddings'
|
|
2
|
-
import { VectorStore } from '@langchain/core/vectorstores'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Interface for VectorStore strategy。
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*/
|
|
9
|
-
export interface IVectorStoreStrategy<TConfig extends {collectionName?: string}> {
|
|
10
|
-
/**
|
|
11
|
-
* Metadata about the strategy
|
|
12
|
-
*/
|
|
13
|
-
readonly name: string;
|
|
14
|
-
readonly description?: string;
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Validate configuration for VectorStore
|
|
18
|
-
*/
|
|
19
|
-
validateConfig(config: TConfig): Promise<void>;
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Create a VectorStore with given config
|
|
23
|
-
*/
|
|
24
|
-
createStore(embeddings: EmbeddingsInterface, config: TConfig): Promise<VectorStore>;
|
|
25
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { Injectable } from '@nestjs/common';
|
|
2
|
-
import { DiscoveryService, Reflector } from '@nestjs/core';
|
|
3
|
-
import { BaseStrategyRegistry } from '../strategy';
|
|
4
|
-
import { VECTOR_STORE_STRATEGY } from './strategy.decorator';
|
|
5
|
-
import { IVectorStoreStrategy } from './strategy.interface';
|
|
6
|
-
|
|
7
|
-
@Injectable()
|
|
8
|
-
export class VectorStoreRegistry<TConfig = any>
|
|
9
|
-
extends BaseStrategyRegistry<IVectorStoreStrategy<TConfig>>
|
|
10
|
-
{
|
|
11
|
-
constructor(
|
|
12
|
-
discoveryService: DiscoveryService,
|
|
13
|
-
reflector: Reflector
|
|
14
|
-
) {
|
|
15
|
-
super(VECTOR_STORE_STRATEGY, discoveryService, reflector);
|
|
16
|
-
}
|
|
17
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { SetMetadata } from '@nestjs/common';
|
|
2
|
-
|
|
3
|
-
export const WORKFLOW_NODE_STRATEGY = 'WORKFLOW_NODE_STRATEGY';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Decorator to mark a provider as a Workflow Node Strategy
|
|
7
|
-
*/
|
|
8
|
-
export const WorkflowNodeStrategy = (provider: string) =>
|
|
9
|
-
SetMetadata(WORKFLOW_NODE_STRATEGY, provider);
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { Runnable } from '@langchain/core/runnables'
|
|
2
|
-
import { BaseChannel } from '@langchain/langgraph'
|
|
3
|
-
import {
|
|
4
|
-
IEnvironment,
|
|
5
|
-
IWorkflowNode,
|
|
6
|
-
TWorkflowNodeMeta,
|
|
7
|
-
TXpertGraph,
|
|
8
|
-
TXpertParameter,
|
|
9
|
-
TXpertTeamNode
|
|
10
|
-
} from '@metad/contracts'
|
|
11
|
-
|
|
12
|
-
export type TWorkflowNodeParams<TConfig = any> = {
|
|
13
|
-
xpertId: string
|
|
14
|
-
agentKey?: string
|
|
15
|
-
node: TXpertTeamNode
|
|
16
|
-
config: TConfig
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Workflow Node Strategy interface
|
|
21
|
-
*/
|
|
22
|
-
export interface IWorkflowNodeStrategy<TConfig = any, TResult = any> {
|
|
23
|
-
/**
|
|
24
|
-
* Metadata describing the node (type, label, description, config schema, etc.)
|
|
25
|
-
*/
|
|
26
|
-
meta: TWorkflowNodeMeta
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Create the node subgraph
|
|
30
|
-
*/
|
|
31
|
-
create(payload: {
|
|
32
|
-
graph: TXpertGraph
|
|
33
|
-
node: TXpertTeamNode & { type: 'workflow' }
|
|
34
|
-
xpertId: string
|
|
35
|
-
environment: IEnvironment
|
|
36
|
-
isDraft: boolean
|
|
37
|
-
}): {
|
|
38
|
-
name?: string
|
|
39
|
-
graph: Runnable
|
|
40
|
-
ends: string[]
|
|
41
|
-
channel: {
|
|
42
|
-
name: string
|
|
43
|
-
annotation: BaseChannel
|
|
44
|
-
}
|
|
45
|
-
navigator?: (state, config) => Promise<any>
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
outputVariables(entity: IWorkflowNode): TXpertParameter[]
|
|
49
|
-
}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { Injectable } from '@nestjs/common';
|
|
2
|
-
import { DiscoveryService, Reflector } from '@nestjs/core';
|
|
3
|
-
import { BaseStrategyRegistry } from '../../strategy';
|
|
4
|
-
import { WORKFLOW_NODE_STRATEGY } from './strategy.decorator';
|
|
5
|
-
import { IWorkflowNodeStrategy } from './strategy.interface';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Registry for Workflow Node Strategies
|
|
9
|
-
*/
|
|
10
|
-
@Injectable()
|
|
11
|
-
export class WorkflowNodeRegistry<TConfig = any, TResult = any> extends BaseStrategyRegistry<IWorkflowNodeStrategy<TConfig, TResult>> {
|
|
12
|
-
constructor(
|
|
13
|
-
discoveryService: DiscoveryService,
|
|
14
|
-
reflector: Reflector
|
|
15
|
-
) {
|
|
16
|
-
super(WORKFLOW_NODE_STRATEGY, discoveryService, reflector);
|
|
17
|
-
}
|
|
18
|
-
}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { TWorkflowTriggerMeta, TXpertTeamNode } from '@metad/contracts'
|
|
2
|
-
|
|
3
|
-
export type TWorkflowTriggerParams<T> = {
|
|
4
|
-
xpertId: string
|
|
5
|
-
agentKey?: string
|
|
6
|
-
node?: TXpertTeamNode
|
|
7
|
-
config: T
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export interface IWorkflowTriggerStrategy<T> {
|
|
11
|
-
meta: TWorkflowTriggerMeta
|
|
12
|
-
|
|
13
|
-
validate(payload: TWorkflowTriggerParams<T>): Promise<any[]>
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Initialize the trigger when publish xpert workflow
|
|
17
|
-
*
|
|
18
|
-
* @param payload
|
|
19
|
-
* @param callback
|
|
20
|
-
*/
|
|
21
|
-
publish(payload: TWorkflowTriggerParams<T>, callback: (payload: any) => void): Promise<any> | void
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Stop the trigger
|
|
25
|
-
*/
|
|
26
|
-
stop(payload: TWorkflowTriggerParams<T>): void;
|
|
27
|
-
}
|