@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
package/jest.config.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
import { readFileSync } from 'fs'
|
|
3
|
+
|
|
4
|
+
// Reading the SWC compilation config and remove the "exclude"
|
|
5
|
+
// for the test files to be compiled by SWC
|
|
6
|
+
const { exclude: _, ...swcJestConfig } = JSON.parse(readFileSync(`${__dirname}/.swcrc`, 'utf-8'))
|
|
7
|
+
|
|
8
|
+
// disable .swcrc look-up by SWC core because we're passing in swcJestConfig ourselves.
|
|
9
|
+
// If we do not disable this, SWC Core will read .swcrc and won't transform our test files due to "exclude"
|
|
10
|
+
if (swcJestConfig.swcrc === undefined) {
|
|
11
|
+
swcJestConfig.swcrc = false
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// Uncomment if using global setup/teardown files being transformed via swc
|
|
15
|
+
// https://nx.dev/packages/jest/documents/overview#global-setup/teardown-with-nx-libraries
|
|
16
|
+
// jest needs EsModule Interop to find the default exported setup/teardown functions
|
|
17
|
+
// swcJestConfig.module.noInterop = false;
|
|
18
|
+
|
|
19
|
+
export default {
|
|
20
|
+
displayName: 'plugin-sdk',
|
|
21
|
+
preset: '../../jest.preset.js',
|
|
22
|
+
transform: {
|
|
23
|
+
'^.+\\.[tj]s$': ['@swc/jest', swcJestConfig]
|
|
24
|
+
},
|
|
25
|
+
moduleFileExtensions: ['ts', 'js', 'html'],
|
|
26
|
+
testEnvironment: 'node',
|
|
27
|
+
coverageDirectory: '../../coverage/packages/plugin-sdk'
|
|
28
|
+
}
|
package/package.json
CHANGED
|
@@ -1,11 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xpert-ai/plugin-sdk",
|
|
3
|
-
"version": "3.6.0
|
|
3
|
+
"version": "3.6.0",
|
|
4
4
|
"license": "AGPL-3.0",
|
|
5
5
|
"dependencies": {},
|
|
6
|
-
"main": "./index.cjs
|
|
7
|
-
"module": "./index.
|
|
6
|
+
"main": "./index.cjs",
|
|
7
|
+
"module": "./index.js",
|
|
8
8
|
"publishConfig": {
|
|
9
9
|
"access": "public"
|
|
10
|
+
},
|
|
11
|
+
"peerDependencies": {
|
|
12
|
+
"@langchain/core": "*",
|
|
13
|
+
"@metad/contracts": "*",
|
|
14
|
+
"@nestjs/common": "*",
|
|
15
|
+
"@nestjs/core": "*",
|
|
16
|
+
"@nestjs/microservices": "*",
|
|
17
|
+
"lodash-es": "*",
|
|
18
|
+
"i18next-fs-backend": "*",
|
|
19
|
+
"i18next": "*",
|
|
20
|
+
"zod": "*",
|
|
21
|
+
"fs": "*",
|
|
22
|
+
"path": "*"
|
|
10
23
|
}
|
|
11
|
-
}
|
|
24
|
+
}
|
package/project.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "plugin-sdk",
|
|
3
|
+
"$schema": "../../node_modules/nx/schemas/project-schema.json",
|
|
4
|
+
"sourceRoot": "packages/plugin-sdk/src",
|
|
5
|
+
"projectType": "library",
|
|
6
|
+
"tags": [],
|
|
7
|
+
"targets": {
|
|
8
|
+
"build": {
|
|
9
|
+
"executor": "@nx/rollup:rollup",
|
|
10
|
+
"outputs": ["{options.outputPath}"],
|
|
11
|
+
"options": {
|
|
12
|
+
"outputPath": "packages/plugin-sdk/dist",
|
|
13
|
+
"main": "packages/plugin-sdk/src/index.ts",
|
|
14
|
+
"tsConfig": "packages/plugin-sdk/tsconfig.lib.json",
|
|
15
|
+
"assets": [
|
|
16
|
+
{
|
|
17
|
+
"glob": "*.md",
|
|
18
|
+
"input": "packages/plugin-sdk/",
|
|
19
|
+
"output": "."
|
|
20
|
+
}
|
|
21
|
+
],
|
|
22
|
+
"project": "packages/plugin-sdk/package.json",
|
|
23
|
+
"compiler": "swc",
|
|
24
|
+
"format": ["cjs", "esm"],
|
|
25
|
+
"external": [
|
|
26
|
+
"@langchain/core",
|
|
27
|
+
"@metad/contracts",
|
|
28
|
+
"@nestjs/common",
|
|
29
|
+
"@nestjs/core",
|
|
30
|
+
"@nestjs/microservices",
|
|
31
|
+
"lodash-es",
|
|
32
|
+
"i18next-fs-backend",
|
|
33
|
+
"i18next",
|
|
34
|
+
"kafkajs",
|
|
35
|
+
"amqplib",
|
|
36
|
+
"mqtt",
|
|
37
|
+
"nats",
|
|
38
|
+
"zod",
|
|
39
|
+
"fs",
|
|
40
|
+
"path"
|
|
41
|
+
]
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
"test": {
|
|
45
|
+
"executor": "@nx/jest:jest",
|
|
46
|
+
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
|
|
47
|
+
"options": {
|
|
48
|
+
"jestConfig": "packages/plugin-sdk/jest.config.ts"
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
"nx-release-publish": {
|
|
52
|
+
"executor": "@nx/js:release-publish",
|
|
53
|
+
"options": {
|
|
54
|
+
"packageRoot": "packages/plugin-sdk/dist",
|
|
55
|
+
"access": "public"
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export * from './lib/plugin'
|
|
2
|
+
export * from './lib/plugin.interface'
|
|
3
|
+
export * from './lib/plugin-metadata'
|
|
4
|
+
export * from './lib/types'
|
|
5
|
+
export * from './lib/logger'
|
|
6
|
+
export * from './lib/integration/index'
|
|
7
|
+
export * from './lib/workflow/index'
|
|
8
|
+
export * from './lib/vectorstore/index'
|
|
9
|
+
export * from './lib/rag/index'
|
|
10
|
+
export * from './lib/toolset/index'
|
|
11
|
+
export * from './lib/core/index'
|
|
@@ -0,0 +1,113 @@
|
|
|
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
|
+
/**
|
|
41
|
+
* Get the absolute path of file in the file system.
|
|
42
|
+
*
|
|
43
|
+
* @param filePath Relative file path
|
|
44
|
+
* @returns Absolute file path
|
|
45
|
+
*/
|
|
46
|
+
fullPath(filePath: string): string {
|
|
47
|
+
return path.join(this.basePath, filePath)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Get web url for a given file path in the file system.
|
|
52
|
+
*
|
|
53
|
+
* @param filePath Relative file path
|
|
54
|
+
* @returns Web URL of file
|
|
55
|
+
*/
|
|
56
|
+
fullUrl(filePath: string): string {
|
|
57
|
+
const url = new URL(filePath, this.baseUrl)
|
|
58
|
+
return url.href
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Read file contents
|
|
63
|
+
*/
|
|
64
|
+
async readFile(filePath: string, encoding: BufferEncoding = 'utf-8') {
|
|
65
|
+
this.ensureAllowed('read')
|
|
66
|
+
const fullPath = this.fullPath(filePath)
|
|
67
|
+
this.ensureInScope(fullPath)
|
|
68
|
+
return await fsPromises.readFile(fullPath)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Write file contents
|
|
73
|
+
*/
|
|
74
|
+
async writeFile(filePath: string, content: string | Buffer): Promise<string> {
|
|
75
|
+
this.ensureAllowed('write')
|
|
76
|
+
const fullPath = this.fullPath(filePath)
|
|
77
|
+
this.ensureInScope(fullPath)
|
|
78
|
+
await fsPromises.mkdir(path.dirname(fullPath), { recursive: true })
|
|
79
|
+
await fsPromises.writeFile(fullPath, content)
|
|
80
|
+
const url = new URL(filePath, this.baseUrl)
|
|
81
|
+
return url.href
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Delete a file
|
|
86
|
+
*/
|
|
87
|
+
async deleteFile(filePath: string): Promise<void> {
|
|
88
|
+
this.ensureAllowed('delete')
|
|
89
|
+
this.ensureInScope(filePath)
|
|
90
|
+
await fsPromises.unlink(filePath)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* List directory contents
|
|
95
|
+
*/
|
|
96
|
+
async listDir(dirPath: string): Promise<string[]> {
|
|
97
|
+
this.ensureAllowed('list')
|
|
98
|
+
this.ensureInScope(dirPath)
|
|
99
|
+
return fsPromises.readdir(dirPath)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Utility: check if a file or directory exists
|
|
104
|
+
*/
|
|
105
|
+
async exists(targetPath: string): Promise<boolean> {
|
|
106
|
+
try {
|
|
107
|
+
await fsPromises.access(targetPath)
|
|
108
|
+
return true
|
|
109
|
+
} catch {
|
|
110
|
+
return false
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { createInstance, i18n as I18nInstance } from 'i18next'
|
|
2
|
+
import FsBackend from 'i18next-fs-backend'
|
|
3
|
+
import path from 'path'
|
|
4
|
+
import fs from 'fs'
|
|
5
|
+
|
|
6
|
+
export async function createI18nInstance(pluginDir: string, language?: string): Promise<I18nInstance> {
|
|
7
|
+
const instance = createInstance()
|
|
8
|
+
const i18nDir = path.join(pluginDir, 'i18n')
|
|
9
|
+
|
|
10
|
+
// detect available languages dynamically
|
|
11
|
+
const lngs = fs
|
|
12
|
+
.readdirSync(i18nDir)
|
|
13
|
+
.filter((f) => f.endsWith('.json'))
|
|
14
|
+
.map((f) => f.replace('.json', ''))
|
|
15
|
+
|
|
16
|
+
await instance
|
|
17
|
+
.use(FsBackend)
|
|
18
|
+
.init({
|
|
19
|
+
lng: language,
|
|
20
|
+
fallbackLng: 'en',
|
|
21
|
+
preload: lngs,
|
|
22
|
+
ns: ['default'],
|
|
23
|
+
defaultNS: 'default',
|
|
24
|
+
backend: {
|
|
25
|
+
loadPath: path.join(i18nDir, '{{lng}}.json'),
|
|
26
|
+
},
|
|
27
|
+
interpolation: { escapeValue: false },
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
return instance
|
|
31
|
+
}
|
|
@@ -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
|
+
* Common UI Schema field definitions
|
|
3
|
+
*/
|
|
4
|
+
export interface ISchemaUIBase {
|
|
5
|
+
component: string; // UI component type
|
|
6
|
+
label?: string; // Field label
|
|
7
|
+
description?: string; // Field description
|
|
8
|
+
placeholder?: string; // Input placeholder
|
|
9
|
+
order?: number; // UI display order
|
|
10
|
+
required?: boolean; // Whether the field is required
|
|
11
|
+
visibleWhen?: Record<string, any>; // Conditional rendering
|
|
12
|
+
enabledWhen?: Record<string, any>; // Conditional enabling
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Secret field extensions
|
|
17
|
+
*/
|
|
18
|
+
export interface ISchemaSecretField extends ISchemaUIBase {
|
|
19
|
+
component: 'secretInput'; // Fixed component type
|
|
20
|
+
revealable?: boolean; // Whether plaintext display is allowed (👁 button)
|
|
21
|
+
maskSymbol?: string; // Mask symbol (default *)
|
|
22
|
+
persist?: boolean; // Whether to persist, false means only used at runtime
|
|
23
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
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
|
+
validateConfig?(config: T): Promise<void>
|
|
11
|
+
}
|
|
@@ -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,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];
|
|
@@ -8,6 +8,7 @@ export interface IOnPluginBootstrap {
|
|
|
8
8
|
*/
|
|
9
9
|
onPluginBootstrap(): void | Promise<void>;
|
|
10
10
|
}
|
|
11
|
+
|
|
11
12
|
/**
|
|
12
13
|
* Interface for plugins with a destroy lifecycle method.
|
|
13
14
|
*/
|
|
@@ -18,6 +19,7 @@ export interface IOnPluginDestroy {
|
|
|
18
19
|
*/
|
|
19
20
|
onPluginDestroy(): void | Promise<void>;
|
|
20
21
|
}
|
|
22
|
+
|
|
21
23
|
/**
|
|
22
24
|
* Interface for plugins supporting basic seed operations.
|
|
23
25
|
*/
|
|
@@ -28,6 +30,7 @@ export interface IOnPluginWithBasicSeed {
|
|
|
28
30
|
*/
|
|
29
31
|
onPluginBasicSeed(): void | Promise<void>;
|
|
30
32
|
}
|
|
33
|
+
|
|
31
34
|
/**
|
|
32
35
|
* Interface for plugins supporting default seed operations.
|
|
33
36
|
*/
|
|
@@ -38,6 +41,7 @@ export interface IOnPluginWithDefaultSeed {
|
|
|
38
41
|
*/
|
|
39
42
|
onPluginDefaultSeed(): void | Promise<void>;
|
|
40
43
|
}
|
|
44
|
+
|
|
41
45
|
/**
|
|
42
46
|
* Interface for plugins supporting random seed operations.
|
|
43
47
|
*/
|
|
@@ -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 'lodash-es';
|
|
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,37 @@
|
|
|
1
|
+
import { DocumentInterface } from '@langchain/core/documents'
|
|
2
|
+
import { BaseChatModel } from '@langchain/core/language_models/chat_models'
|
|
3
|
+
import { IDocumentUnderstandingProvider, IKnowledgeDocument } from '@metad/contracts'
|
|
4
|
+
import { Permissions, XpFileSystem } from '../../core/index'
|
|
5
|
+
import { ChunkMetadata } 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 TImageUnderstandingResult = {
|
|
16
|
+
chunks: DocumentInterface<Partial<ChunkMetadata>>[]
|
|
17
|
+
// pages?: Document<Partial<ChunkMetadata>>[]
|
|
18
|
+
metadata: any // Additional metadata (e.g. model name, processing time)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface IImageUnderstandingStrategy<TConfig extends TImageUnderstandingConfig = TImageUnderstandingConfig> {
|
|
22
|
+
readonly permissions: Permissions
|
|
23
|
+
/**
|
|
24
|
+
* Metadata about this strategy
|
|
25
|
+
*/
|
|
26
|
+
readonly meta: IDocumentUnderstandingProvider
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Validate the configuration
|
|
30
|
+
*/
|
|
31
|
+
validateConfig(config: TConfig): Promise<void>
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Understand image files (e.g., OCR, VLM, Chart Parsing)
|
|
35
|
+
*/
|
|
36
|
+
understandImages(doc: IKnowledgeDocument<ChunkMetadata>, config: TConfig): Promise<TImageUnderstandingResult>
|
|
37
|
+
}
|