@defai.digital/ability-domain 13.0.3
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/LICENSE +214 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/dist/loader.d.ts +38 -0
- package/dist/loader.d.ts.map +1 -0
- package/dist/loader.js +171 -0
- package/dist/loader.js.map +1 -0
- package/dist/manager.d.ts +44 -0
- package/dist/manager.d.ts.map +1 -0
- package/dist/manager.js +132 -0
- package/dist/manager.js.map +1 -0
- package/dist/registry.d.ts +24 -0
- package/dist/registry.d.ts.map +1 -0
- package/dist/registry.js +64 -0
- package/dist/registry.js.map +1 -0
- package/dist/types.d.ts +111 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +14 -0
- package/dist/types.js.map +1 -0
- package/package.json +48 -0
- package/src/index.ts +50 -0
- package/src/loader.ts +209 -0
- package/src/manager.ts +184 -0
- package/src/registry.ts +84 -0
- package/src/types.ts +149 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"manager.js","sourceRoot":"","sources":["../src/manager.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AASH,OAAO,EAAE,6BAA6B,EAAE,MAAM,YAAY,CAAC;AAE3D;;GAEG;AACH,MAAM,OAAO,qBAAqB;IAEb;IACA;IAFnB,YACmB,QAAyB,EACzB,SAA8B,6BAA6B;QAD3D,aAAQ,GAAR,QAAQ,CAAiB;QACzB,WAAM,GAAN,MAAM,CAAqD;IAC3E,CAAC;IAEJ;;OAEG;IACH,KAAK,CAAC,mBAAmB,CACvB,OAAe,EACf,IAAY,EACZ,aAAwB,EACxB,YAAqB;QAErB,MAAM,GAAG,GAAG,YAAY,IAAI,IAAI,CAAC,MAAM,CAAC,oBAAoB,CAAC;QAC7D,MAAM,SAAS,GAAG,IAAI,CAAC,WAAW,EAAE,CAAC;QACrC,MAAM,SAAS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QAAQ,EAAE;QACzC,GAAG,6BAA6B;QAChC,GAAG,MAAM;KACV,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ability Registry
|
|
3
|
+
*
|
|
4
|
+
* In-memory storage for abilities with filtering support.
|
|
5
|
+
*/
|
|
6
|
+
import type { Ability } from '@defai.digital/contracts';
|
|
7
|
+
import type { AbilityRegistry, AbilityFilter } from './types.js';
|
|
8
|
+
/**
|
|
9
|
+
* In-memory ability registry implementation
|
|
10
|
+
*/
|
|
11
|
+
export declare class InMemoryAbilityRegistry implements AbilityRegistry {
|
|
12
|
+
private abilities;
|
|
13
|
+
register(ability: Ability): Promise<void>;
|
|
14
|
+
get(abilityId: string): Promise<Ability | undefined>;
|
|
15
|
+
list(filter?: AbilityFilter): Promise<Ability[]>;
|
|
16
|
+
remove(abilityId: string): Promise<void>;
|
|
17
|
+
exists(abilityId: string): Promise<boolean>;
|
|
18
|
+
clear(): Promise<void>;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Creates an in-memory ability registry
|
|
22
|
+
*/
|
|
23
|
+
export declare function createAbilityRegistry(): AbilityRegistry;
|
|
24
|
+
//# sourceMappingURL=registry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../src/registry.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAC;AACxD,OAAO,KAAK,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAEjE;;GAEG;AACH,qBAAa,uBAAwB,YAAW,eAAe;IAC7D,OAAO,CAAC,SAAS,CAA8B;IAEzC,QAAQ,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;IAIzC,GAAG,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,GAAG,SAAS,CAAC;IAIpD,IAAI,CAAC,MAAM,CAAC,EAAE,aAAa,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC;IA0ChD,MAAM,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAIxC,MAAM,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAI3C,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAG7B;AAED;;GAEG;AACH,wBAAgB,qBAAqB,IAAI,eAAe,CAEvD"}
|
package/dist/registry.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ability Registry
|
|
3
|
+
*
|
|
4
|
+
* In-memory storage for abilities with filtering support.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* In-memory ability registry implementation
|
|
8
|
+
*/
|
|
9
|
+
export class InMemoryAbilityRegistry {
|
|
10
|
+
abilities = new Map();
|
|
11
|
+
async register(ability) {
|
|
12
|
+
this.abilities.set(ability.abilityId, ability);
|
|
13
|
+
}
|
|
14
|
+
async get(abilityId) {
|
|
15
|
+
return this.abilities.get(abilityId);
|
|
16
|
+
}
|
|
17
|
+
async list(filter) {
|
|
18
|
+
let results = Array.from(this.abilities.values());
|
|
19
|
+
if (filter) {
|
|
20
|
+
if (filter.category !== undefined) {
|
|
21
|
+
results = results.filter((a) => a.category === filter.category);
|
|
22
|
+
}
|
|
23
|
+
if (filter.tags !== undefined && filter.tags.length > 0) {
|
|
24
|
+
results = results.filter((a) => filter.tags.some((tag) => a.tags?.includes(tag)));
|
|
25
|
+
}
|
|
26
|
+
if (filter.enabled !== undefined) {
|
|
27
|
+
results = results.filter((a) => a.enabled === filter.enabled);
|
|
28
|
+
}
|
|
29
|
+
if (filter.applicableTo !== undefined) {
|
|
30
|
+
results = results.filter((a) => {
|
|
31
|
+
// Check if excluded
|
|
32
|
+
if (a.excludeFrom?.includes(filter.applicableTo)) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
// Check if applicable (empty means all)
|
|
36
|
+
if (!a.applicableTo || a.applicableTo.length === 0) {
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
return (a.applicableTo.includes('*') ||
|
|
40
|
+
a.applicableTo.includes(filter.applicableTo));
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
// Sort by priority (higher first)
|
|
45
|
+
results.sort((a, b) => (b.priority ?? 50) - (a.priority ?? 50));
|
|
46
|
+
return results;
|
|
47
|
+
}
|
|
48
|
+
async remove(abilityId) {
|
|
49
|
+
this.abilities.delete(abilityId);
|
|
50
|
+
}
|
|
51
|
+
async exists(abilityId) {
|
|
52
|
+
return this.abilities.has(abilityId);
|
|
53
|
+
}
|
|
54
|
+
async clear() {
|
|
55
|
+
this.abilities.clear();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Creates an in-memory ability registry
|
|
60
|
+
*/
|
|
61
|
+
export function createAbilityRegistry() {
|
|
62
|
+
return new InMemoryAbilityRegistry();
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=registry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.js","sourceRoot":"","sources":["../src/registry.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAKH;;GAEG;AACH,MAAM,OAAO,uBAAuB;IAC1B,SAAS,GAAG,IAAI,GAAG,EAAmB,CAAC;IAE/C,KAAK,CAAC,QAAQ,CAAC,OAAgB;QAC7B,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,OAAO,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;IACjD,CAAC;IAED,KAAK,CAAC,GAAG,CAAC,SAAiB;QACzB,OAAO,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;IACvC,CAAC;IAED,KAAK,CAAC,IAAI,CAAC,MAAsB;QAC/B,IAAI,OAAO,GAAG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AO,CACL,CAAC,CAAC,YAAY,CAAC,QAAQ,CAAC,GAAG,CAAC;wBAC5B,CAAC,CAAC,YAAY,CAAC,QAAQ,CAAC,MAAM,CAAC,YAAa,CAAC,CAC9C,CAAC;gBACJ,CAAC,CAAC,CAAC;YACL,CAAC;QACH,CAAC;QAED,kCAAkC;QAClC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,QAAQ,IAAI,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,QAAQ,IAAI,EAAE,CAAC,CAAC,CAAC;QAEhE,OAAO,OAAO,CAAC;IACjB,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,SAAiB;QAC5B,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IACnC,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,SAAiB;QAC5B,OAAO,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;IACvC,CAAC;IAED,KAAK,CAAC,KAAK;QACT,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,CAAC;IACzB,CAAC;CACF;AAED;;GAEG;AACH,MAAM,UAAU,qBAAqB;IACnC,OAAO,IAAI,uBAAuB,EAAE,CAAC;AACvC,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ability Domain Types
|
|
3
|
+
*/
|
|
4
|
+
import { type Ability, type AbilityInjectionResult } from '@defai.digital/contracts';
|
|
5
|
+
/**
|
|
6
|
+
* Ability registry interface
|
|
7
|
+
*/
|
|
8
|
+
export interface AbilityRegistry {
|
|
9
|
+
/**
|
|
10
|
+
* Register an ability
|
|
11
|
+
*/
|
|
12
|
+
register(ability: Ability): Promise<void>;
|
|
13
|
+
/**
|
|
14
|
+
* Get an ability by ID
|
|
15
|
+
*/
|
|
16
|
+
get(abilityId: string): Promise<Ability | undefined>;
|
|
17
|
+
/**
|
|
18
|
+
* List all abilities
|
|
19
|
+
*/
|
|
20
|
+
list(filter?: AbilityFilter): Promise<Ability[]>;
|
|
21
|
+
/**
|
|
22
|
+
* Remove an ability
|
|
23
|
+
*/
|
|
24
|
+
remove(abilityId: string): Promise<void>;
|
|
25
|
+
/**
|
|
26
|
+
* Check if ability exists
|
|
27
|
+
*/
|
|
28
|
+
exists(abilityId: string): Promise<boolean>;
|
|
29
|
+
/**
|
|
30
|
+
* Clear all abilities
|
|
31
|
+
*/
|
|
32
|
+
clear(): Promise<void>;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Filter for listing abilities
|
|
36
|
+
*/
|
|
37
|
+
export interface AbilityFilter {
|
|
38
|
+
category?: string;
|
|
39
|
+
tags?: string[];
|
|
40
|
+
enabled?: boolean;
|
|
41
|
+
applicableTo?: string;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Ability loader interface
|
|
45
|
+
*/
|
|
46
|
+
export interface AbilityLoader {
|
|
47
|
+
/**
|
|
48
|
+
* Load an ability by ID
|
|
49
|
+
*/
|
|
50
|
+
load(abilityId: string): Promise<Ability | undefined>;
|
|
51
|
+
/**
|
|
52
|
+
* Load all abilities from the source
|
|
53
|
+
*/
|
|
54
|
+
loadAll(): Promise<Ability[]>;
|
|
55
|
+
/**
|
|
56
|
+
* Check if ability exists
|
|
57
|
+
*/
|
|
58
|
+
exists(abilityId: string): Promise<boolean>;
|
|
59
|
+
/**
|
|
60
|
+
* Reload abilities from source
|
|
61
|
+
*/
|
|
62
|
+
reload(): Promise<void>;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Ability loader configuration
|
|
66
|
+
*/
|
|
67
|
+
export interface AbilityLoaderConfig {
|
|
68
|
+
/** Directory to load abilities from */
|
|
69
|
+
abilitiesDir: string;
|
|
70
|
+
/** File extensions to load */
|
|
71
|
+
extensions?: string[];
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Ability manager interface - coordinates loading and injection
|
|
75
|
+
*/
|
|
76
|
+
export interface AbilityManager {
|
|
77
|
+
/**
|
|
78
|
+
* Get abilities for an agent based on task
|
|
79
|
+
*/
|
|
80
|
+
getAbilitiesForTask(agentId: string, task: string, coreAbilities?: string[], maxAbilities?: number): Promise<Ability[]>;
|
|
81
|
+
/**
|
|
82
|
+
* Inject abilities into agent context
|
|
83
|
+
*/
|
|
84
|
+
injectAbilities(agentId: string, task: string, coreAbilities?: string[], options?: AbilityInjectionOptions): Promise<AbilityInjectionResult>;
|
|
85
|
+
/**
|
|
86
|
+
* Get all abilities applicable to an agent
|
|
87
|
+
*/
|
|
88
|
+
getApplicableAbilities(agentId: string): Promise<Ability[]>;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Options for ability injection
|
|
92
|
+
*/
|
|
93
|
+
export interface AbilityInjectionOptions {
|
|
94
|
+
maxAbilities?: number;
|
|
95
|
+
maxTokens?: number;
|
|
96
|
+
includeMetadata?: boolean;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Ability domain configuration
|
|
100
|
+
*/
|
|
101
|
+
export interface AbilityDomainConfig {
|
|
102
|
+
abilitiesDir: string;
|
|
103
|
+
maxAbilitiesPerAgent: number;
|
|
104
|
+
maxTokensPerInjection: number;
|
|
105
|
+
cacheEnabled: boolean;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Default ability domain configuration
|
|
109
|
+
*/
|
|
110
|
+
export declare const DEFAULT_ABILITY_DOMAIN_CONFIG: AbilityDomainConfig;
|
|
111
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EACL,KAAK,OAAO,EACZ,KAAK,sBAAsB,EAG5B,MAAM,0BAA0B,CAAC;AAElC;;GAEG;AACH,MAAM,WAAW,eA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AAM,WAAW,cAAc;IAC7B;;OAEG;IACH,mBAAmB,CACjB,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,MAAM,EACZ,aAAa,CAAC,EAAE,MAAM,EAAE,EACxB,YAAY,CAAC,EAAE,MAAM,GACpB,OAAO,CAAC,OAAO,EAAE,CAAC,CAAC;IAEtB;;OAEG;IACH,eAAe,CACb,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,MAAM,EACZ,aAAa,CAAC,EAAE,MAAM,EAAE,EACxB,OAAO,CAAC,EAAE,uBAAuB,GAChC,OAAO,CAAC,sBAAsB,CAAC,CAAC;IAEnC;;OAEG;IACH,sBAAsB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC,CAAC;CAC7D;AAED;;GAEG;AACH,MAAM,WAAW,uBAAuB;IACtC,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,eAAe,CAAC,EAAE,OAAO,CAAC;CAC3B;AAED;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC,YAAY,EAAE,MAAM,CAAC;IACrB,oBAAoB,EAAE,MAAM,CAAC;IAC7B,qBAAqB,EAAE,MAAM,CAAC;IAC9B,YAAY,EAAE,OAAO,CAAC;CACvB;AAED;;GAEG;AACH,eAAO,MAAM,6BAA6B,EAAE,mBAK3C,CAAC"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ability Domain Types
|
|
3
|
+
*/
|
|
4
|
+
import { LIMIT_ABILITY_TOKENS, LIMIT_ABILITIES_INJECT, } from '@defai.digital/contracts';
|
|
5
|
+
/**
|
|
6
|
+
* Default ability domain configuration
|
|
7
|
+
*/
|
|
8
|
+
export const DEFAULT_ABILITY_DOMAIN_CONFIG = {
|
|
9
|
+
abilitiesDir: 'examples/abilities',
|
|
10
|
+
maxAbilitiesPerAgent: LIMIT_ABILITIES_INJECT,
|
|
11
|
+
maxTokensPerInjection: LIMIT_ABILITY_TOKENS,
|
|
12
|
+
cacheEnabled: true,
|
|
13
|
+
};
|
|
14
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAGL,oBAAoB,EACpB,sBAAsB,GACvB,MAAM,0BAA0B,CAAC;AAmIlC;;GAEG;AACH,MAAM,CAAC,MAAM,6BAA6B,GAAwB;IAChE,YAAY,EAAE,oBAAoB;IAClC,oBAAoB,EAAE,sBAAsB;IAC5C,qBAAqB,EAAE,oBAAoB;IAC3C,YAAY,EAAE,IAAI;CACnB,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@defai.digital/ability-domain",
|
|
3
|
+
"version": "13.0.3",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Ability management for AutomatosX agents",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"author": "DEFAI Private Limited",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/defai-digital/automatosx.git",
|
|
11
|
+
"directory": "packages/core/ability-domain"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "https://github.com/defai-digital/automatosx#readme",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/defai-digital/automatosx/issues"
|
|
16
|
+
},
|
|
17
|
+
"main": "dist/index.js",
|
|
18
|
+
"types": "dist/index.d.ts",
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"import": "./dist/index.js"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist",
|
|
27
|
+
"src"
|
|
28
|
+
],
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": ">=20.0.0"
|
|
31
|
+
},
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@defai.digital/contracts": "13.0.3"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"typescript": "^5.7.2"
|
|
40
|
+
},
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"zod": "^3.23.0"
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"build": "tsc --build",
|
|
46
|
+
"clean": "rm -rf dist"
|
|
47
|
+
}
|
|
48
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @defai.digital/ability-domain
|
|
3
|
+
*
|
|
4
|
+
* Ability management for AutomatosX agents.
|
|
5
|
+
* Abilities are reusable knowledge modules that can be loaded
|
|
6
|
+
* into agent contexts based on task requirements.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
// Types
|
|
10
|
+
export type {
|
|
11
|
+
AbilityRegistry,
|
|
12
|
+
AbilityFilter,
|
|
13
|
+
AbilityLoader,
|
|
14
|
+
AbilityLoaderConfig,
|
|
15
|
+
AbilityManager,
|
|
16
|
+
AbilityInjectionOptions,
|
|
17
|
+
AbilityDomainConfig,
|
|
18
|
+
} from './types.js';
|
|
19
|
+
|
|
20
|
+
export { DEFAULT_ABILITY_DOMAIN_CONFIG } from './types.js';
|
|
21
|
+
|
|
22
|
+
// Registry
|
|
23
|
+
export { InMemoryAbilityRegistry, createAbilityRegistry } from './registry.js';
|
|
24
|
+
|
|
25
|
+
// Loader
|
|
26
|
+
export { FileSystemAbilityLoader, createAbilityLoader } from './loader.js';
|
|
27
|
+
|
|
28
|
+
// Manager
|
|
29
|
+
export { DefaultAbilityManager, createAbilityManager } from './manager.js';
|
|
30
|
+
|
|
31
|
+
// Re-export contract types
|
|
32
|
+
export type {
|
|
33
|
+
Ability,
|
|
34
|
+
AbilityManifest,
|
|
35
|
+
AbilityLoadResult,
|
|
36
|
+
AbilityInjectionRequest,
|
|
37
|
+
AbilityInjectionResult,
|
|
38
|
+
} from '@defai.digital/contracts';
|
|
39
|
+
|
|
40
|
+
export {
|
|
41
|
+
AbilitySchema,
|
|
42
|
+
AbilityManifestSchema,
|
|
43
|
+
AbilityLoadResultSchema,
|
|
44
|
+
AbilityInjectionRequestSchema,
|
|
45
|
+
AbilityInjectionResultSchema,
|
|
46
|
+
AbilityErrorCode,
|
|
47
|
+
validateAbility,
|
|
48
|
+
safeValidateAbility,
|
|
49
|
+
validateAbilityInjectionRequest,
|
|
50
|
+
} from '@defai.digital/contracts';
|
package/src/loader.ts
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ability Loader
|
|
3
|
+
*
|
|
4
|
+
* Loads abilities from markdown files.
|
|
5
|
+
* Format: Each .md file is an ability with YAML frontmatter for metadata.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import * as fs from 'node:fs';
|
|
9
|
+
import * as path from 'node:path';
|
|
10
|
+
import type { Ability } from '@defai.digital/contracts';
|
|
11
|
+
import { validateAbility } from '@defai.digital/contracts';
|
|
12
|
+
import type { AbilityLoader, AbilityLoaderConfig } from './types.js';
|
|
13
|
+
|
|
14
|
+
// ============================================================================
|
|
15
|
+
// Constants
|
|
16
|
+
// ============================================================================
|
|
17
|
+
|
|
18
|
+
const DEFAULT_EXTENSIONS = ['.md'];
|
|
19
|
+
|
|
20
|
+
// ============================================================================
|
|
21
|
+
// File System Ability Loader
|
|
22
|
+
// ============================================================================
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Loads abilities from markdown files
|
|
26
|
+
*/
|
|
27
|
+
export class FileSystemAbilityLoader implements AbilityLoader {
|
|
28
|
+
private readonly config: Required<AbilityLoaderConfig>;
|
|
29
|
+
private cache = new Map<string, Ability>();
|
|
30
|
+
private loaded = false;
|
|
31
|
+
|
|
32
|
+
constructor(config: AbilityLoaderConfig) {
|
|
33
|
+
this.config = {
|
|
34
|
+
abilitiesDir: config.abilitiesDir,
|
|
35
|
+
extensions: config.extensions ?? DEFAULT_EXTENSIONS,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async load(abilityId: string): Promise<Ability | undefined> {
|
|
40
|
+
if (!this.loaded) {
|
|
41
|
+
await this.loadAll();
|
|
42
|
+
}
|
|
43
|
+
return this.cache.get(abilityId);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async loadAll(): Promise<Ability[]> {
|
|
47
|
+
this.cache.clear();
|
|
48
|
+
|
|
49
|
+
const dirPath = this.config.abilitiesDir;
|
|
50
|
+
|
|
51
|
+
if (!fs.existsSync(dirPath)) {
|
|
52
|
+
this.loaded = true;
|
|
53
|
+
return [];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const files = fs.readdirSync(dirPath);
|
|
57
|
+
const abilities: Ability[] = [];
|
|
58
|
+
|
|
59
|
+
for (const file of files) {
|
|
60
|
+
const ext = path.extname(file).toLowerCase();
|
|
61
|
+
if (!this.config.extensions.includes(ext)) {
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const filePath = path.join(dirPath, file);
|
|
66
|
+
const ability = await this.loadFile(filePath, file);
|
|
67
|
+
|
|
68
|
+
if (ability) {
|
|
69
|
+
if (this.cache.has(ability.abilityId)) {
|
|
70
|
+
console.warn(
|
|
71
|
+
`Duplicate ability ID "${ability.abilityId}" in ${file}, skipping`
|
|
72
|
+
);
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
this.cache.set(ability.abilityId, ability);
|
|
77
|
+
abilities.push(ability);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
this.loaded = true;
|
|
82
|
+
return abilities;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async exists(abilityId: string): Promise<boolean> {
|
|
86
|
+
if (!this.loaded) {
|
|
87
|
+
await this.loadAll();
|
|
88
|
+
}
|
|
89
|
+
return this.cache.has(abilityId);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async reload(): Promise<void> {
|
|
93
|
+
this.loaded = false;
|
|
94
|
+
await this.loadAll();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Load an ability from a markdown file
|
|
99
|
+
*/
|
|
100
|
+
private async loadFile(
|
|
101
|
+
filePath: string,
|
|
102
|
+
fileName: string
|
|
103
|
+
): Promise<Ability | undefined> {
|
|
104
|
+
try {
|
|
105
|
+
const content = fs.readFileSync(filePath, 'utf-8');
|
|
106
|
+
const { metadata, body } = this.parseMarkdown(content);
|
|
107
|
+
|
|
108
|
+
// Generate ability ID from filename if not in metadata
|
|
109
|
+
const abilityId = typeof metadata.abilityId === 'string'
|
|
110
|
+
? metadata.abilityId
|
|
111
|
+
: fileName.replace(/\.md$/i, '').toLowerCase().replace(/[^a-z0-9-]/g, '-');
|
|
112
|
+
|
|
113
|
+
const ability: Ability = {
|
|
114
|
+
abilityId,
|
|
115
|
+
displayName: typeof metadata.displayName === 'string' ? metadata.displayName : this.titleCase(abilityId),
|
|
116
|
+
version: typeof metadata.version === 'string' ? metadata.version : undefined,
|
|
117
|
+
description: typeof metadata.description === 'string' ? metadata.description : undefined,
|
|
118
|
+
category: typeof metadata.category === 'string' ? metadata.category : undefined,
|
|
119
|
+
tags: Array.isArray(metadata.tags) ? metadata.tags as string[] : undefined,
|
|
120
|
+
content: body.trim(),
|
|
121
|
+
author: typeof metadata.author === 'string' ? metadata.author : undefined,
|
|
122
|
+
source: typeof metadata.source === 'string' ? metadata.source : undefined,
|
|
123
|
+
requires: Array.isArray(metadata.requires) ? metadata.requires as string[] : undefined,
|
|
124
|
+
conflicts: Array.isArray(metadata.conflicts) ? metadata.conflicts as string[] : undefined,
|
|
125
|
+
applicableTo: Array.isArray(metadata.applicableTo) ? metadata.applicableTo as string[] : undefined,
|
|
126
|
+
excludeFrom: Array.isArray(metadata.excludeFrom) ? metadata.excludeFrom as string[] : undefined,
|
|
127
|
+
priority: typeof metadata.priority === 'number' ? metadata.priority : 50,
|
|
128
|
+
enabled: typeof metadata.enabled === 'boolean' ? metadata.enabled : true,
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
// Validate
|
|
132
|
+
return validateAbility(ability);
|
|
133
|
+
} catch (error) {
|
|
134
|
+
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
135
|
+
console.warn(`Failed to load ability from ${filePath}: ${message}`);
|
|
136
|
+
return undefined;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Parse markdown with optional YAML frontmatter
|
|
142
|
+
*/
|
|
143
|
+
private parseMarkdown(content: string): {
|
|
144
|
+
metadata: Record<string, unknown>;
|
|
145
|
+
body: string;
|
|
146
|
+
} {
|
|
147
|
+
const frontmatterRegex = /^---\n([\s\S]*?)\n---\n([\s\S]*)$/;
|
|
148
|
+
const match = frontmatterRegex.exec(content);
|
|
149
|
+
|
|
150
|
+
if (!match) {
|
|
151
|
+
return { metadata: {}, body: content };
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const frontmatter = match[1]!;
|
|
155
|
+
const body = match[2]!;
|
|
156
|
+
|
|
157
|
+
// Simple YAML parser for frontmatter
|
|
158
|
+
const metadata: Record<string, unknown> = {};
|
|
159
|
+
const lines = frontmatter.split('\n');
|
|
160
|
+
|
|
161
|
+
for (const line of lines) {
|
|
162
|
+
const colonIndex = line.indexOf(':');
|
|
163
|
+
if (colonIndex === -1) continue;
|
|
164
|
+
|
|
165
|
+
const key = line.substring(0, colonIndex).trim();
|
|
166
|
+
let value: unknown = line.substring(colonIndex + 1).trim();
|
|
167
|
+
|
|
168
|
+
// Parse arrays
|
|
169
|
+
if (value === '') {
|
|
170
|
+
// Check if next lines are array items
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// Parse booleans
|
|
175
|
+
if (value === 'true') value = true;
|
|
176
|
+
else if (value === 'false') value = false;
|
|
177
|
+
// Parse numbers
|
|
178
|
+
else if (/^\d+$/.test(value as string)) value = parseInt(value as string, 10);
|
|
179
|
+
// Parse arrays (simple inline format)
|
|
180
|
+
else if ((value as string).startsWith('[') && (value as string).endsWith(']')) {
|
|
181
|
+
value = (value as string)
|
|
182
|
+
.slice(1, -1)
|
|
183
|
+
.split(',')
|
|
184
|
+
.map((s) => s.trim().replace(/^['"]|['"]$/g, ''));
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
metadata[key] = value;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return { metadata, body };
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Convert kebab-case to Title Case
|
|
195
|
+
*/
|
|
196
|
+
private titleCase(str: string): string {
|
|
197
|
+
return str
|
|
198
|
+
.split('-')
|
|
199
|
+
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
|
|
200
|
+
.join(' ');
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Creates a file system ability loader
|
|
206
|
+
*/
|
|
207
|
+
export function createAbilityLoader(config: AbilityLoaderConfig): AbilityLoader {
|
|
208
|
+
return new FileSystemAbilityLoader(config);
|
|
209
|
+
}
|