@xpert-ai/plugin-sdk 0.0.1
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/PERMISSIONS.md +181 -0
- package/README.md +19 -0
- package/SCHEMA_SPECIFICATION.md +332 -0
- package/jest.config.ts +28 -0
- package/package.json +11 -0
- package/project.json +29 -0
- package/src/index.ts +12 -0
- package/src/lib/core/file-system.ts +96 -0
- package/src/lib/core/index.ts +3 -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 +10 -0
- package/src/lib/integration/strategy.registry.ts +12 -0
- package/src/lib/knowledge/index.ts +3 -0
- package/src/lib/knowledge/knowledge-strategy.decorator.ts +6 -0
- package/src/lib/knowledge/knowledge-strategy.interface.ts +15 -0
- package/src/lib/knowledge/knowledge-strategy.registry.ts +36 -0
- package/src/lib/logger.ts +18 -0
- package/src/lib/plugin-metadata.ts +20 -0
- package/src/lib/plugin.hook.ts +54 -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 +42 -0
- package/src/lib/rag/image/strategy.registry.ts +17 -0
- package/src/lib/rag/index.ts +6 -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 +28 -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 +48 -0
- package/src/lib/rag/transformer/strategy.registry.ts +14 -0
- package/src/lib/rag/types.ts +86 -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 +49 -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
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import fsPromises from 'fs/promises'
|
|
2
|
+
import path from 'path'
|
|
3
|
+
import { FileSystemPermission } from './permissions'
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Restricted FileSystem based on granted permissions
|
|
8
|
+
*/
|
|
9
|
+
export class XpFileSystem {
|
|
10
|
+
private allowedOps: Set<'read' | 'write' | 'delete' | 'list'>
|
|
11
|
+
private scope: string[] | undefined
|
|
12
|
+
|
|
13
|
+
constructor(permission: FileSystemPermission, private basePath: string, private baseUrl: string) {
|
|
14
|
+
this.allowedOps = new Set(permission.operations)
|
|
15
|
+
this.scope = permission.scope
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Check if operation is allowed
|
|
20
|
+
*/
|
|
21
|
+
private ensureAllowed(op: 'read' | 'write' | 'delete' | 'list') {
|
|
22
|
+
if (!this.allowedOps.has(op)) {
|
|
23
|
+
throw new Error(`Permission denied: ${op} operation not allowed`)
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Check if path is within scope
|
|
29
|
+
*/
|
|
30
|
+
private ensureInScope(targetPath: string) {
|
|
31
|
+
if (!this.scope || this.scope.length === 0) return
|
|
32
|
+
const resolved = path.resolve(targetPath)
|
|
33
|
+
for (const s of this.scope) {
|
|
34
|
+
const absScope = path.resolve(s)
|
|
35
|
+
if (resolved.startsWith(absScope)) return
|
|
36
|
+
}
|
|
37
|
+
throw new Error(`Permission denied: path "${targetPath}" is out of scope`)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
fullPath(filePath: string): string {
|
|
41
|
+
return path.join(this.basePath, filePath)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Read file contents
|
|
46
|
+
*/
|
|
47
|
+
async readFile(filePath: string, encoding: BufferEncoding = 'utf-8') {
|
|
48
|
+
this.ensureAllowed('read')
|
|
49
|
+
const fullPath = this.fullPath(filePath)
|
|
50
|
+
this.ensureInScope(fullPath)
|
|
51
|
+
return await fsPromises.readFile(fullPath)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Write file contents
|
|
56
|
+
*/
|
|
57
|
+
async writeFile(filePath: string, content: string | Buffer): Promise<string> {
|
|
58
|
+
this.ensureAllowed('write')
|
|
59
|
+
const fullPath = this.fullPath(filePath)
|
|
60
|
+
this.ensureInScope(fullPath)
|
|
61
|
+
await fsPromises.mkdir(path.dirname(fullPath), { recursive: true })
|
|
62
|
+
await fsPromises.writeFile(fullPath, content)
|
|
63
|
+
const url = new URL(filePath, this.baseUrl)
|
|
64
|
+
return url.href
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Delete a file
|
|
69
|
+
*/
|
|
70
|
+
async deleteFile(filePath: string): Promise<void> {
|
|
71
|
+
this.ensureAllowed('delete')
|
|
72
|
+
this.ensureInScope(filePath)
|
|
73
|
+
await fsPromises.unlink(filePath)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* List directory contents
|
|
78
|
+
*/
|
|
79
|
+
async listDir(dirPath: string): Promise<string[]> {
|
|
80
|
+
this.ensureAllowed('list')
|
|
81
|
+
this.ensureInScope(dirPath)
|
|
82
|
+
return fsPromises.readdir(dirPath)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Utility: check if a file or directory exists
|
|
87
|
+
*/
|
|
88
|
+
async exists(targetPath: string): Promise<boolean> {
|
|
89
|
+
try {
|
|
90
|
+
await fsPromises.access(targetPath)
|
|
91
|
+
return true
|
|
92
|
+
} catch {
|
|
93
|
+
return false
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ===============================
|
|
3
|
+
* Unified Permissions Definition
|
|
4
|
+
* ===============================
|
|
5
|
+
* Used by Agent / Plugin developers to declare required capabilities.
|
|
6
|
+
* Core system will check and inject allowed resources accordingly.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Base Permission type
|
|
11
|
+
*/
|
|
12
|
+
export interface BasePermission {
|
|
13
|
+
type: string; // Discriminator
|
|
14
|
+
description?: string; // Optional description for UI
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 1. LLM Permission
|
|
19
|
+
* Example: { type: 'llm', provider: 'openai', capability: 'vision' }
|
|
20
|
+
*/
|
|
21
|
+
export interface LLMPermission extends BasePermission {
|
|
22
|
+
type: 'llm';
|
|
23
|
+
provider?: string; // e.g. "openai", "anthropic", "azure", "ollama"
|
|
24
|
+
capability: 'text' | 'chat' | 'vision' | 'embedding';
|
|
25
|
+
scope?: string[]; // Allowed model names, e.g. ["gpt-4", "gpt-4-vision-preview"]
|
|
26
|
+
maxTokens?: number; // Maximum output tokens allowed
|
|
27
|
+
rateLimit?: { rps: number }; // Rate limit per second
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 2. Vector Store Permission
|
|
32
|
+
* Example: { type: 'vectorstore', provider: 'pinecone', operations: ['insert', 'query'] }
|
|
33
|
+
*/
|
|
34
|
+
export interface VectorStorePermission extends BasePermission {
|
|
35
|
+
type: 'vectorstore';
|
|
36
|
+
provider: string; // "pinecone" | "milvus" | "chromadb" | ...
|
|
37
|
+
operations: Array<'insert' | 'query' | 'delete'>;
|
|
38
|
+
scope?: string[]; // Restrict to index / collection
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* 3. Knowledge Base Permission
|
|
43
|
+
* Example: { type: 'knowledge', operations: ['read', 'write'], scope: ['kb_123'] }
|
|
44
|
+
*/
|
|
45
|
+
export interface KnowledgePermission extends BasePermission {
|
|
46
|
+
type: 'knowledge';
|
|
47
|
+
operations: Array<'read' | 'write' | 'update' | 'delete'>;
|
|
48
|
+
scope?: string[]; // Restrict to certain KB IDs
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* 4. File System Permission
|
|
53
|
+
* Example: { type: 'filesystem', operations: ['read', 'write'], scope: ['/documents', '/images'] }
|
|
54
|
+
*/
|
|
55
|
+
export interface FileSystemPermission extends BasePermission {
|
|
56
|
+
type: 'filesystem';
|
|
57
|
+
operations: Array<'read' | 'write' | 'delete' | 'list'>;
|
|
58
|
+
scope?: string[]; // Restrict to certain directories or file types
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* 5. Integration Permission
|
|
63
|
+
* Example: { type: 'integration', service: 'feishu', operations: ['read', 'write'] }
|
|
64
|
+
*/
|
|
65
|
+
export interface IntegrationPermission extends BasePermission {
|
|
66
|
+
type: 'integration';
|
|
67
|
+
service: string; // e.g. 'slack', 'feishu', 'jira', 'sap', etc.
|
|
68
|
+
operations?: Array<'read' | 'write' | 'update' | 'delete'>;
|
|
69
|
+
scope?: string[];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// /**
|
|
73
|
+
// * 4. Document Permission
|
|
74
|
+
// * Example: { type: 'document', formats: ['pdf'], operations: ['load', 'transform'] }
|
|
75
|
+
// */
|
|
76
|
+
// export interface DocumentPermission extends BasePermission {
|
|
77
|
+
// type: 'document';
|
|
78
|
+
// formats: string[]; // ['pdf', 'pptx', 'docx', 'image', 'html', 'md']
|
|
79
|
+
// operations: Array<'load' | 'transform' | 'ocr' | 'imageUnderstanding'>;
|
|
80
|
+
// }
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Union type for all permissions
|
|
84
|
+
*/
|
|
85
|
+
export type Permission =
|
|
86
|
+
| LLMPermission
|
|
87
|
+
| VectorStorePermission
|
|
88
|
+
| KnowledgePermission
|
|
89
|
+
| FileSystemPermission
|
|
90
|
+
| IntegrationPermission
|
|
91
|
+
// | DocumentPermission
|
|
92
|
+
// | ExternalPermission;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Permissions array type
|
|
96
|
+
*/
|
|
97
|
+
export type Permissions = Permission[];
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 通用 UI Schema 字段定义
|
|
3
|
+
*/
|
|
4
|
+
export interface ISchemaUIBase {
|
|
5
|
+
component: string; // UI 组件类型
|
|
6
|
+
label?: string; // 字段标签
|
|
7
|
+
description?: string; // 字段说明
|
|
8
|
+
placeholder?: string; // 输入占位符
|
|
9
|
+
order?: number; // UI 显示顺序
|
|
10
|
+
required?: boolean; // 是否必填
|
|
11
|
+
visibleWhen?: Record<string, any>; // 条件渲染
|
|
12
|
+
enabledWhen?: Record<string, any>; // 条件启用
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Secret 字段扩展
|
|
17
|
+
*/
|
|
18
|
+
export interface ISchemaSecretField extends ISchemaUIBase {
|
|
19
|
+
component: 'secretInput'; // 固定组件类型
|
|
20
|
+
revealable?: boolean; // 是否允许明文显示(👁 按钮)
|
|
21
|
+
maskSymbol?: string; // 遮罩符号(默认 *)
|
|
22
|
+
persist?: boolean; // 是否持久保存,false 表示仅运行时使用
|
|
23
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { IIntegration, TIntegrationProvider } from '@metad/contracts'
|
|
2
|
+
|
|
3
|
+
export type TIntegrationStrategyParams = {
|
|
4
|
+
query: string
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface IntegrationStrategy<T = unknown> {
|
|
8
|
+
meta: TIntegrationProvider
|
|
9
|
+
execute(integration: IIntegration<T>, payload: TIntegrationStrategyParams): Promise<any>
|
|
10
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Injectable } from '@nestjs/common'
|
|
2
|
+
import { DiscoveryService, Reflector } from '@nestjs/core'
|
|
3
|
+
import { BaseStrategyRegistry } from '../strategy'
|
|
4
|
+
import { INTEGRATION_STRATEGY } from './strategy.decorator'
|
|
5
|
+
import { IntegrationStrategy } from './strategy.interface'
|
|
6
|
+
|
|
7
|
+
@Injectable()
|
|
8
|
+
export class IntegrationStrategyRegistry extends BaseStrategyRegistry<IntegrationStrategy> {
|
|
9
|
+
constructor(discoveryService: DiscoveryService, reflector: Reflector) {
|
|
10
|
+
super(INTEGRATION_STRATEGY, discoveryService, reflector)
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { IIntegration } from '@metad/contracts'
|
|
2
|
+
import { Document } from 'langchain/document'
|
|
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,36 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
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];
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Interface for plugins with a bootstrap lifecycle method.
|
|
3
|
+
*/
|
|
4
|
+
export interface IOnPluginBootstrap {
|
|
5
|
+
/**
|
|
6
|
+
* Called when the plugin is being initialized.
|
|
7
|
+
* @returns A void or a Promise representing the completion of the operation.
|
|
8
|
+
*/
|
|
9
|
+
onPluginBootstrap(): void | Promise<void>;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Interface for plugins with a destroy lifecycle method.
|
|
14
|
+
*/
|
|
15
|
+
export interface IOnPluginDestroy {
|
|
16
|
+
/**
|
|
17
|
+
* Called when the plugin is being destroyed.
|
|
18
|
+
* @returns A void or a Promise representing the completion of the operation.
|
|
19
|
+
*/
|
|
20
|
+
onPluginDestroy(): void | Promise<void>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Interface for plugins supporting basic seed operations.
|
|
25
|
+
*/
|
|
26
|
+
export interface IOnPluginWithBasicSeed {
|
|
27
|
+
/**
|
|
28
|
+
* Invoked when seeding basic plugin data.
|
|
29
|
+
* @returns A void or a Promise representing the completion of the operation.
|
|
30
|
+
*/
|
|
31
|
+
onPluginBasicSeed(): void | Promise<void>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Interface for plugins supporting default seed operations.
|
|
36
|
+
*/
|
|
37
|
+
export interface IOnPluginWithDefaultSeed {
|
|
38
|
+
/**
|
|
39
|
+
* Invoked when seeding default plugin data.
|
|
40
|
+
* @returns A void or a Promise representing the completion of the operation.
|
|
41
|
+
*/
|
|
42
|
+
onPluginDefaultSeed(): void | Promise<void>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Interface for plugins supporting random seed operations.
|
|
47
|
+
*/
|
|
48
|
+
export interface IOnPluginWithRandomSeed {
|
|
49
|
+
/**
|
|
50
|
+
* Invoked when seeding random plugin data.
|
|
51
|
+
* @returns A void or a Promise representing the completion of the operation.
|
|
52
|
+
*/
|
|
53
|
+
onPluginRandomSeed(): void | Promise<void>;
|
|
54
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
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;
|
|
@@ -0,0 +1,32 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
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);
|
|
@@ -0,0 +1,42 @@
|
|
|
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
|
+
}
|
|
@@ -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,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/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
|
+
}
|
|
@@ -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
|
+
}
|