@resourcexjs/core 0.4.0 → 0.7.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 +194 -120
- package/dist/index.d.ts +139 -275
- package/dist/index.js +347 -462
- package/dist/index.js.map +11 -15
- package/package.json +7 -6
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# @resourcexjs/core
|
|
2
2
|
|
|
3
|
-
Core
|
|
3
|
+
Core types and implementations for ResourceX.
|
|
4
4
|
|
|
5
|
-
> **Note**: For most use cases, use the [`resourcexjs`](https://www.npmjs.com/package/resourcexjs) package
|
|
5
|
+
> **Note**: For most use cases, use the main [`resourcexjs`](https://www.npmjs.com/package/resourcexjs) package. This package is for advanced usage.
|
|
6
6
|
|
|
7
7
|
## Installation
|
|
8
8
|
|
|
@@ -12,171 +12,245 @@ npm install @resourcexjs/core
|
|
|
12
12
|
bun add @resourcexjs/core
|
|
13
13
|
```
|
|
14
14
|
|
|
15
|
-
##
|
|
15
|
+
## What's Inside
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
Core building blocks for ResourceX:
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
- **RXL** (Locator) - Parse locator strings
|
|
20
|
+
- **RXM** (Manifest) - Create resource metadata
|
|
21
|
+
- **RXC** (Content) - Stream-based content
|
|
22
|
+
- **RXR** (Resource) - Complete resource type
|
|
23
|
+
- **ResourceType** - Type system with serializer & resolver
|
|
24
|
+
- **TypeHandlerChain** - Responsibility chain for type handling
|
|
25
|
+
- **Errors** - Error hierarchy
|
|
21
26
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
27
|
+
## API
|
|
28
|
+
|
|
29
|
+
### RXL - Resource Locator
|
|
25
30
|
|
|
26
|
-
|
|
31
|
+
Parse resource locator strings. Format: `[domain/path/]name[.type][@version]`
|
|
27
32
|
|
|
28
33
|
```typescript
|
|
29
|
-
import {
|
|
34
|
+
import { parseRXL } from "@resourcexjs/core";
|
|
30
35
|
|
|
31
|
-
const
|
|
32
|
-
// { type: "text", content: "...", meta: { ... } }
|
|
36
|
+
const rxl = parseRXL("deepractice.ai/sean/assistant.prompt@1.0.0");
|
|
33
37
|
|
|
34
|
-
|
|
35
|
-
//
|
|
38
|
+
rxl.domain; // "deepractice.ai"
|
|
39
|
+
rxl.path; // "sean"
|
|
40
|
+
rxl.name; // "assistant"
|
|
41
|
+
rxl.type; // "prompt"
|
|
42
|
+
rxl.version; // "1.0.0"
|
|
43
|
+
rxl.toString(); // "deepractice.ai/sean/assistant.prompt@1.0.0"
|
|
36
44
|
```
|
|
37
45
|
|
|
38
|
-
###
|
|
46
|
+
### RXM - Resource Manifest
|
|
39
47
|
|
|
40
|
-
|
|
41
|
-
import { deposit } from "@resourcexjs/core";
|
|
48
|
+
Create and validate resource metadata:
|
|
42
49
|
|
|
43
|
-
|
|
44
|
-
|
|
50
|
+
```typescript
|
|
51
|
+
import { createRXM } from "@resourcexjs/core";
|
|
52
|
+
|
|
53
|
+
const manifest = createRXM({
|
|
54
|
+
domain: "deepractice.ai",
|
|
55
|
+
path: "sean", // optional
|
|
56
|
+
name: "assistant",
|
|
57
|
+
type: "prompt",
|
|
58
|
+
version: "1.0.0",
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
manifest.toLocator(); // → "deepractice.ai/sean/assistant.prompt@1.0.0"
|
|
62
|
+
manifest.toJSON(); // → plain object
|
|
45
63
|
```
|
|
46
64
|
|
|
47
|
-
|
|
65
|
+
Required fields: domain, name, type, version
|
|
48
66
|
|
|
49
|
-
|
|
50
|
-
|
|
67
|
+
### RXC - Resource Content
|
|
68
|
+
|
|
69
|
+
Stream-based content (can only be consumed once, like fetch Response):
|
|
51
70
|
|
|
52
|
-
|
|
53
|
-
|
|
71
|
+
```typescript
|
|
72
|
+
import { createRXC, loadRXC } from "@resourcexjs/core";
|
|
73
|
+
|
|
74
|
+
// Create from memory
|
|
75
|
+
const content = createRXC("Hello, World!");
|
|
76
|
+
const content = createRXC(Buffer.from([1, 2, 3]));
|
|
77
|
+
const content = createRXC(readableStream);
|
|
78
|
+
|
|
79
|
+
// Load from file or URL (async)
|
|
80
|
+
const content = await loadRXC("./file.txt");
|
|
81
|
+
const content = await loadRXC("https://example.com/data.txt");
|
|
82
|
+
|
|
83
|
+
// Consume (choose one, can only use once)
|
|
84
|
+
const text = await content.text(); // → string
|
|
85
|
+
const buffer = await content.buffer(); // → Buffer
|
|
86
|
+
const json = await content.json<T>(); // → T
|
|
87
|
+
const stream = content.stream; // → ReadableStream<Uint8Array>
|
|
54
88
|
```
|
|
55
89
|
|
|
56
|
-
###
|
|
90
|
+
### RXR - Resource
|
|
57
91
|
|
|
58
|
-
|
|
92
|
+
Complete resource object (pure interface, no factory):
|
|
59
93
|
|
|
60
94
|
```typescript
|
|
61
|
-
import {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
return buffer;
|
|
75
|
-
},
|
|
76
|
-
async write(location: string, content: Buffer): Promise<void> {
|
|
77
|
-
// write to S3...
|
|
78
|
-
},
|
|
79
|
-
async list(location: string): Promise<string[]> {
|
|
80
|
-
// list S3 objects...
|
|
81
|
-
return keys;
|
|
82
|
-
},
|
|
95
|
+
import type { RXR } from "@resourcexjs/core";
|
|
96
|
+
|
|
97
|
+
interface RXR {
|
|
98
|
+
locator: RXL;
|
|
99
|
+
manifest: RXM;
|
|
100
|
+
content: RXC;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Create from literals
|
|
104
|
+
const rxr: RXR = {
|
|
105
|
+
locator: parseRXL("localhost/test.text@1.0.0"),
|
|
106
|
+
manifest: createRXM({ domain: "localhost", name: "test", type: "text", version: "1.0.0" }),
|
|
107
|
+
content: createRXC("content"),
|
|
83
108
|
};
|
|
84
|
-
|
|
85
|
-
registerTransportHandler(s3Handler);
|
|
86
109
|
```
|
|
87
110
|
|
|
88
|
-
###
|
|
111
|
+
### Resource Types
|
|
89
112
|
|
|
90
|
-
|
|
113
|
+
Built-in types:
|
|
91
114
|
|
|
92
115
|
```typescript
|
|
93
|
-
import {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
116
|
+
import { textType, jsonType, binaryType, builtinTypes } from "@resourcexjs/core";
|
|
117
|
+
|
|
118
|
+
console.log(textType.name); // "text"
|
|
119
|
+
console.log(textType.aliases); // ["txt", "plaintext"]
|
|
120
|
+
console.log(jsonType.aliases); // ["config", "manifest"]
|
|
121
|
+
console.log(binaryType.aliases); // ["bin", "blob", "raw"]
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
Define custom types:
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
import { defineResourceType } from "@resourcexjs/core";
|
|
128
|
+
|
|
129
|
+
defineResourceType({
|
|
130
|
+
name: "prompt",
|
|
131
|
+
aliases: ["deepractice-prompt"],
|
|
132
|
+
description: "AI Prompt template",
|
|
133
|
+
serializer: {
|
|
134
|
+
async serialize(rxr: RXR): Promise<Buffer> {
|
|
135
|
+
// Convert RXR to Buffer for storage
|
|
136
|
+
const text = await rxr.content.text();
|
|
137
|
+
return Buffer.from(JSON.stringify({ template: text }));
|
|
138
|
+
},
|
|
139
|
+
async deserialize(data: Buffer, manifest: RXM): Promise<RXR> {
|
|
140
|
+
// Convert Buffer back to RXR
|
|
141
|
+
const obj = JSON.parse(data.toString());
|
|
142
|
+
return {
|
|
143
|
+
locator: parseRXL(manifest.toLocator()),
|
|
144
|
+
manifest,
|
|
145
|
+
content: createRXC(obj.template),
|
|
146
|
+
};
|
|
147
|
+
},
|
|
111
148
|
},
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
149
|
+
resolver: {
|
|
150
|
+
async resolve(rxr: RXR): Promise<PromptTemplate> {
|
|
151
|
+
// Convert RXR to usable object
|
|
152
|
+
return {
|
|
153
|
+
template: await rxr.content.text(),
|
|
154
|
+
compile: (vars) => {
|
|
155
|
+
/* ... */
|
|
156
|
+
},
|
|
157
|
+
};
|
|
158
|
+
},
|
|
116
159
|
},
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
registerSemanticHandler(jsonHandler);
|
|
160
|
+
});
|
|
120
161
|
```
|
|
121
162
|
|
|
122
|
-
|
|
163
|
+
Query registered types:
|
|
164
|
+
|
|
165
|
+
```typescript
|
|
166
|
+
import { getResourceType, clearResourceTypes } from "@resourcexjs/core";
|
|
167
|
+
|
|
168
|
+
const type = getResourceType("text");
|
|
169
|
+
const type = getResourceType("txt"); // Works with aliases
|
|
170
|
+
|
|
171
|
+
clearResourceTypes(); // For testing
|
|
172
|
+
```
|
|
123
173
|
|
|
124
|
-
###
|
|
174
|
+
### TypeHandlerChain
|
|
125
175
|
|
|
126
|
-
|
|
127
|
-
- `resolve(url)` - Resolve ARP URL to resource
|
|
128
|
-
- `deposit(url, data)` - Deposit data to ARP URL
|
|
129
|
-
- `resourceExists(url)` - Check if resource exists
|
|
130
|
-
- `resourceDelete(url)` - Delete resource
|
|
131
|
-
- `getTransportHandler(name)` - Get registered transport handler
|
|
132
|
-
- `registerTransportHandler(handler)` - Register custom transport
|
|
133
|
-
- `getSemanticHandler(name)` - Get registered semantic handler
|
|
134
|
-
- `registerSemanticHandler(handler)` - Register custom semantic
|
|
135
|
-
- `createResourceRegistry()` - Create a resource definition registry
|
|
176
|
+
Responsibility chain for type handling (used internally by Registry):
|
|
136
177
|
|
|
137
|
-
|
|
178
|
+
```typescript
|
|
179
|
+
import { createTypeHandlerChain, builtinTypes } from "@resourcexjs/core";
|
|
138
180
|
|
|
139
|
-
|
|
181
|
+
// Create with initial types
|
|
182
|
+
const chain = createTypeHandlerChain(builtinTypes);
|
|
140
183
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
184
|
+
// Or start empty
|
|
185
|
+
const chain = createTypeHandlerChain();
|
|
186
|
+
chain.register(customType);
|
|
187
|
+
chain.registerAll([type1, type2]);
|
|
145
188
|
|
|
146
|
-
|
|
189
|
+
// Use the chain
|
|
190
|
+
chain.canHandle("text"); // → boolean
|
|
191
|
+
chain.getHandler("txt"); // → ResourceType (via alias)
|
|
147
192
|
|
|
148
|
-
|
|
149
|
-
|
|
193
|
+
await chain.serialize(rxr); // → Buffer
|
|
194
|
+
await chain.deserialize(buffer, manifest); // → RXR
|
|
195
|
+
await chain.resolve<T>(rxr); // → T (usable object)
|
|
196
|
+
```
|
|
150
197
|
|
|
151
|
-
|
|
198
|
+
## Error Handling
|
|
152
199
|
|
|
153
200
|
```typescript
|
|
154
201
|
import {
|
|
155
|
-
ResourceXError,
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
202
|
+
ResourceXError,
|
|
203
|
+
LocatorError,
|
|
204
|
+
ManifestError,
|
|
205
|
+
ContentError,
|
|
206
|
+
ResourceTypeError,
|
|
159
207
|
} from "@resourcexjs/core";
|
|
208
|
+
|
|
209
|
+
try {
|
|
210
|
+
const rxl = parseRXL("invalid");
|
|
211
|
+
} catch (error) {
|
|
212
|
+
if (error instanceof LocatorError) {
|
|
213
|
+
console.error("Invalid locator:", error.message);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
160
216
|
```
|
|
161
217
|
|
|
162
|
-
|
|
218
|
+
Error hierarchy:
|
|
219
|
+
|
|
220
|
+
```
|
|
221
|
+
Error
|
|
222
|
+
└── ResourceXError
|
|
223
|
+
├── LocatorError (RXL parsing)
|
|
224
|
+
├── ManifestError (RXM validation)
|
|
225
|
+
├── ContentError (RXC consumption)
|
|
226
|
+
└── ResourceTypeError (Type not found/duplicate)
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
## Complete Exports
|
|
163
230
|
|
|
164
231
|
```typescript
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
232
|
+
// Errors
|
|
233
|
+
export { ResourceXError, LocatorError, ManifestError, ContentError, ResourceTypeError };
|
|
234
|
+
|
|
235
|
+
// RXL (Locator)
|
|
236
|
+
export { parseRXL };
|
|
237
|
+
export type { RXL };
|
|
238
|
+
|
|
239
|
+
// RXM (Manifest)
|
|
240
|
+
export { createRXM };
|
|
241
|
+
export type { RXM, ManifestData };
|
|
242
|
+
|
|
243
|
+
// RXC (Content)
|
|
244
|
+
export { createRXC, loadRXC };
|
|
245
|
+
export type { RXC };
|
|
246
|
+
|
|
247
|
+
// RXR (Resource)
|
|
248
|
+
export type { RXR, ResourceType, ResourceSerializer, ResourceResolver };
|
|
249
|
+
|
|
250
|
+
// ResourceType
|
|
251
|
+
export { defineResourceType, getResourceType, clearResourceTypes };
|
|
252
|
+
export { textType, jsonType, binaryType, builtinTypes };
|
|
253
|
+
export { TypeHandlerChain, createTypeHandlerChain };
|
|
180
254
|
```
|
|
181
255
|
|
|
182
256
|
## License
|