resourcexjs 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 +206 -132
- package/dist/arp.d.ts +1 -0
- package/dist/arp.js +447 -0
- package/dist/arp.js.map +10 -0
- package/dist/index.d.ts +9 -87
- package/dist/index.js +241 -546
- package/dist/index.js.map +5 -6
- package/package.json +10 -4
package/README.md
CHANGED
|
@@ -1,213 +1,287 @@
|
|
|
1
1
|
# resourcexjs
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
ResourceX - Resource management protocol for AI Agents. Like npm for AI resources.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
|
-
npm install resourcexjs
|
|
8
|
+
npm install resourcexjs @resourcexjs/registry
|
|
9
9
|
# or
|
|
10
|
-
bun add resourcexjs
|
|
10
|
+
bun add resourcexjs @resourcexjs/registry
|
|
11
11
|
```
|
|
12
12
|
|
|
13
13
|
## Quick Start
|
|
14
14
|
|
|
15
15
|
```typescript
|
|
16
|
-
import {
|
|
16
|
+
import { createRegistry } from "@resourcexjs/registry";
|
|
17
|
+
import { createRXM, createRXC, parseRXL } from "resourcexjs";
|
|
18
|
+
|
|
19
|
+
// Create registry
|
|
20
|
+
const registry = createRegistry();
|
|
21
|
+
|
|
22
|
+
// Prepare a resource
|
|
23
|
+
const manifest = createRXM({
|
|
24
|
+
domain: "localhost",
|
|
25
|
+
name: "my-prompt",
|
|
26
|
+
type: "text",
|
|
27
|
+
version: "1.0.0",
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const rxr = {
|
|
31
|
+
locator: parseRXL(manifest.toLocator()),
|
|
32
|
+
manifest,
|
|
33
|
+
content: createRXC("You are a helpful assistant."),
|
|
34
|
+
};
|
|
17
35
|
|
|
18
|
-
|
|
36
|
+
// Link to local registry
|
|
37
|
+
await registry.link(rxr);
|
|
19
38
|
|
|
20
|
-
// Resolve
|
|
21
|
-
const resource = await
|
|
22
|
-
|
|
23
|
-
|
|
39
|
+
// Resolve resource
|
|
40
|
+
const resource = await registry.resolve("localhost/my-prompt.text@1.0.0");
|
|
41
|
+
console.log(await resource.content.text());
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## API
|
|
24
45
|
|
|
25
|
-
|
|
26
|
-
console.log(resource.content); // file content as string
|
|
27
|
-
console.log(resource.meta); // { url, semantic, transport, ... }
|
|
46
|
+
### RXL - Resource Locator
|
|
28
47
|
|
|
29
|
-
|
|
30
|
-
await rx.deposit("@text:file://./data/config.txt", "hello world");
|
|
48
|
+
Parse resource locator strings. Format: `[domain/path/]name[.type][@version]`
|
|
31
49
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
const binary = await rx.resolve("@binary:file://./data/image.png");
|
|
35
|
-
console.log(binary.content); // Buffer
|
|
50
|
+
```typescript
|
|
51
|
+
import { parseRXL } from "resourcexjs";
|
|
36
52
|
|
|
37
|
-
|
|
38
|
-
const exists = await rx.exists("@text:file://./data/config.txt");
|
|
53
|
+
const rxl = parseRXL("deepractice.ai/sean/assistant.prompt@1.0.0");
|
|
39
54
|
|
|
40
|
-
//
|
|
41
|
-
|
|
55
|
+
rxl.domain; // "deepractice.ai"
|
|
56
|
+
rxl.path; // "sean"
|
|
57
|
+
rxl.name; // "assistant"
|
|
58
|
+
rxl.type; // "prompt"
|
|
59
|
+
rxl.version; // "1.0.0"
|
|
60
|
+
rxl.toString(); // reconstructs the locator
|
|
42
61
|
```
|
|
43
62
|
|
|
44
|
-
###
|
|
63
|
+
### RXM - Resource Manifest
|
|
45
64
|
|
|
46
|
-
|
|
47
|
-
- `@` - Shorthand alias (default, configurable via `alias` config)
|
|
65
|
+
Create resource metadata:
|
|
48
66
|
|
|
49
|
-
|
|
67
|
+
```typescript
|
|
68
|
+
import { createRXM } from "resourcexjs";
|
|
69
|
+
|
|
70
|
+
const manifest = createRXM({
|
|
71
|
+
domain: "deepractice.ai",
|
|
72
|
+
path: "sean", // optional
|
|
73
|
+
name: "assistant",
|
|
74
|
+
type: "prompt",
|
|
75
|
+
version: "1.0.0",
|
|
76
|
+
});
|
|
50
77
|
|
|
78
|
+
manifest.toLocator(); // → "deepractice.ai/sean/assistant.prompt@1.0.0"
|
|
79
|
+
manifest.toJSON(); // → plain object
|
|
51
80
|
```
|
|
52
|
-
|
|
81
|
+
|
|
82
|
+
### RXC - Resource Content
|
|
83
|
+
|
|
84
|
+
Stream-based content (consumed once, like fetch Response):
|
|
85
|
+
|
|
86
|
+
```typescript
|
|
87
|
+
import { createRXC, loadRXC } from "resourcexjs";
|
|
88
|
+
|
|
89
|
+
// Create from memory
|
|
90
|
+
const content = createRXC("Hello");
|
|
91
|
+
const content = createRXC(Buffer.from([1, 2, 3]));
|
|
92
|
+
const content = createRXC(readableStream);
|
|
93
|
+
|
|
94
|
+
// Load from file or URL
|
|
95
|
+
const content = await loadRXC("./file.txt");
|
|
96
|
+
const content = await loadRXC("https://example.com/data.txt");
|
|
97
|
+
|
|
98
|
+
// Consume (can only use one method)
|
|
99
|
+
await content.text(); // → string
|
|
100
|
+
await content.buffer(); // → Buffer
|
|
101
|
+
await content.json<T>(); // → T
|
|
102
|
+
content.stream; // → ReadableStream<Uint8Array>
|
|
53
103
|
```
|
|
54
104
|
|
|
55
|
-
|
|
56
|
-
- **transport**: How to access it (e.g., `https`, `http`, `file`)
|
|
57
|
-
- **location**: Where to find it
|
|
105
|
+
### RXR - Resource
|
|
58
106
|
|
|
59
|
-
|
|
107
|
+
Complete resource object (pure interface):
|
|
60
108
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
109
|
+
```typescript
|
|
110
|
+
interface RXR {
|
|
111
|
+
locator: RXL;
|
|
112
|
+
manifest: RXM;
|
|
113
|
+
content: RXC;
|
|
114
|
+
}
|
|
64
115
|
|
|
65
|
-
|
|
116
|
+
// Create from literals
|
|
117
|
+
const rxr: RXR = { locator, manifest, content };
|
|
118
|
+
```
|
|
66
119
|
|
|
67
|
-
|
|
120
|
+
### Registry
|
|
121
|
+
|
|
122
|
+
Resource storage and retrieval (from `@resourcexjs/registry`):
|
|
68
123
|
|
|
69
124
|
```typescript
|
|
70
|
-
import {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
resources: [
|
|
76
|
-
{
|
|
77
|
-
name: "logs",
|
|
78
|
-
semantic: "text",
|
|
79
|
-
transport: "file",
|
|
80
|
-
basePath: join(homedir(), ".myapp", "logs"),
|
|
81
|
-
},
|
|
82
|
-
{
|
|
83
|
-
name: "cache",
|
|
84
|
-
semantic: "binary",
|
|
85
|
-
transport: "file",
|
|
86
|
-
basePath: join(homedir(), ".myapp", "cache"),
|
|
87
|
-
},
|
|
88
|
-
],
|
|
125
|
+
import { createRegistry } from "@resourcexjs/registry";
|
|
126
|
+
|
|
127
|
+
const registry = createRegistry({
|
|
128
|
+
path: "~/.resourcex", // optional
|
|
129
|
+
types: [customType], // optional, defaults to built-in types
|
|
89
130
|
});
|
|
90
131
|
|
|
91
|
-
//
|
|
92
|
-
await
|
|
93
|
-
|
|
132
|
+
// Link (local development/cache)
|
|
133
|
+
await registry.link(rxr);
|
|
134
|
+
|
|
135
|
+
// Resolve (local-first, then remote)
|
|
136
|
+
const rxr = await registry.resolve("deepractice.ai/assistant.prompt@1.0.0");
|
|
137
|
+
|
|
138
|
+
// Check existence
|
|
139
|
+
const exists = await registry.exists("localhost/test.text@1.0.0");
|
|
94
140
|
|
|
95
|
-
//
|
|
96
|
-
await
|
|
141
|
+
// Delete
|
|
142
|
+
await registry.delete("localhost/test.text@1.0.0");
|
|
143
|
+
|
|
144
|
+
// Search (TODO)
|
|
145
|
+
const results = await registry.search("assistant");
|
|
97
146
|
```
|
|
98
147
|
|
|
99
|
-
|
|
148
|
+
### Resource Types
|
|
149
|
+
|
|
150
|
+
Built-in types:
|
|
100
151
|
|
|
101
|
-
|
|
152
|
+
| Type | Aliases | Description |
|
|
153
|
+
| -------- | ---------------- | -------------- |
|
|
154
|
+
| `text` | txt, plaintext | Plain text |
|
|
155
|
+
| `json` | config, manifest | JSON content |
|
|
156
|
+
| `binary` | bin, blob, raw | Binary content |
|
|
102
157
|
|
|
103
|
-
|
|
158
|
+
Define custom types:
|
|
104
159
|
|
|
105
160
|
```typescript
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
161
|
+
import { defineResourceType } from "resourcexjs";
|
|
162
|
+
|
|
163
|
+
defineResourceType({
|
|
164
|
+
name: "prompt",
|
|
165
|
+
aliases: ["deepractice-prompt"],
|
|
166
|
+
description: "AI Prompt template",
|
|
167
|
+
serializer: {
|
|
168
|
+
serialize: async (rxr) => Buffer.from(await rxr.content.text()),
|
|
169
|
+
deserialize: async (data, manifest) => ({
|
|
170
|
+
locator: parseRXL(manifest.toLocator()),
|
|
171
|
+
manifest,
|
|
172
|
+
content: createRXC(data.toString()),
|
|
173
|
+
}),
|
|
174
|
+
},
|
|
175
|
+
resolver: {
|
|
176
|
+
resolve: async (rxr) => ({
|
|
177
|
+
template: await rxr.content.text(),
|
|
178
|
+
// ... custom methods
|
|
179
|
+
}),
|
|
180
|
+
},
|
|
111
181
|
});
|
|
112
182
|
```
|
|
113
183
|
|
|
114
|
-
###
|
|
184
|
+
### TypeHandlerChain
|
|
115
185
|
|
|
116
|
-
|
|
186
|
+
Responsibility chain for type handling (used internally):
|
|
117
187
|
|
|
118
188
|
```typescript
|
|
119
|
-
|
|
120
|
-
// Returns: { type, content, meta }
|
|
189
|
+
import { createTypeHandlerChain, builtinTypes } from "resourcexjs";
|
|
121
190
|
|
|
122
|
-
const
|
|
123
|
-
|
|
191
|
+
const chain = createTypeHandlerChain(builtinTypes);
|
|
192
|
+
|
|
193
|
+
chain.serialize(rxr); // → Buffer
|
|
194
|
+
chain.deserialize(buffer, manifest); // → RXR
|
|
195
|
+
chain.resolve<T>(rxr); // → T (usable object)
|
|
124
196
|
```
|
|
125
197
|
|
|
126
|
-
|
|
198
|
+
## ARP - Low-level I/O
|
|
127
199
|
|
|
128
|
-
|
|
200
|
+
For direct file/network operations:
|
|
129
201
|
|
|
130
202
|
```typescript
|
|
131
|
-
|
|
132
|
-
await rx.deposit("arp:binary:file://./data/image.png", buffer);
|
|
133
|
-
```
|
|
203
|
+
import { createARP } from "resourcexjs/arp";
|
|
134
204
|
|
|
135
|
-
|
|
205
|
+
const arp = createARP(); // Defaults include file, http, https, text, binary
|
|
136
206
|
|
|
137
|
-
|
|
207
|
+
// Parse URL
|
|
208
|
+
const arl = arp.parse("arp:text:file://./config.txt");
|
|
138
209
|
|
|
139
|
-
|
|
140
|
-
const
|
|
141
|
-
//
|
|
142
|
-
```
|
|
210
|
+
// Read
|
|
211
|
+
const resource = await arl.resolve();
|
|
212
|
+
console.log(resource.content); // string
|
|
143
213
|
|
|
144
|
-
|
|
214
|
+
// Write
|
|
215
|
+
await arl.deposit("hello world");
|
|
145
216
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
await rx.delete("arp:text:file://./data/config.txt");
|
|
217
|
+
// Operations
|
|
218
|
+
await arl.exists(); // → boolean
|
|
219
|
+
await arl.delete();
|
|
150
220
|
```
|
|
151
221
|
|
|
152
|
-
|
|
222
|
+
## Exports
|
|
153
223
|
|
|
154
|
-
|
|
224
|
+
### Main Package (`resourcexjs`)
|
|
155
225
|
|
|
156
226
|
```typescript
|
|
157
|
-
|
|
158
|
-
|
|
227
|
+
// Errors
|
|
228
|
+
export { ResourceXError, LocatorError, ManifestError, ContentError, ResourceTypeError };
|
|
159
229
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
230
|
+
// RXL (Locator)
|
|
231
|
+
export { parseRXL };
|
|
232
|
+
export type { RXL };
|
|
163
233
|
|
|
164
|
-
|
|
234
|
+
// RXM (Manifest)
|
|
235
|
+
export { createRXM };
|
|
236
|
+
export type { RXM, ManifestData };
|
|
165
237
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
| `binary` | `Buffer` | Raw binary, no transformation |
|
|
238
|
+
// RXC (Content)
|
|
239
|
+
export { createRXC, loadRXC };
|
|
240
|
+
export type { RXC };
|
|
170
241
|
|
|
171
|
-
|
|
242
|
+
// RXR (Resource)
|
|
243
|
+
export type { RXR, ResourceType, ResourceSerializer, ResourceResolver };
|
|
172
244
|
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
meta: {
|
|
178
|
-
url: string; // original URL
|
|
179
|
-
semantic: string; // semantic type
|
|
180
|
-
transport: string; // transport protocol
|
|
181
|
-
location: string; // resource location
|
|
182
|
-
size: number; // content size in bytes
|
|
183
|
-
encoding?: string; // content encoding (for text)
|
|
184
|
-
resolvedAt: string; // ISO timestamp
|
|
185
|
-
};
|
|
186
|
-
}
|
|
245
|
+
// ResourceType
|
|
246
|
+
export { defineResourceType, getResourceType, clearResourceTypes };
|
|
247
|
+
export { textType, jsonType, binaryType, builtinTypes };
|
|
248
|
+
export { TypeHandlerChain, createTypeHandlerChain };
|
|
187
249
|
```
|
|
188
250
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
All errors extend `ResourceXError`:
|
|
251
|
+
### Registry Package (`@resourcexjs/registry`)
|
|
192
252
|
|
|
193
253
|
```typescript
|
|
194
|
-
|
|
254
|
+
export { createRegistry, ARPRegistry };
|
|
255
|
+
export { RegistryError };
|
|
256
|
+
export type { Registry, RegistryConfig };
|
|
257
|
+
```
|
|
195
258
|
|
|
196
|
-
|
|
259
|
+
### ARP Package (`resourcexjs/arp`)
|
|
197
260
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
}
|
|
261
|
+
```typescript
|
|
262
|
+
export { createARP, ARP, type ARPConfig };
|
|
263
|
+
export { ARPError, ParseError, TransportError, SemanticError };
|
|
264
|
+
export type { ARI, ARL };
|
|
265
|
+
export { fileTransport, httpTransport, httpsTransport };
|
|
266
|
+
export { textSemantic, binarySemantic };
|
|
205
267
|
```
|
|
206
268
|
|
|
207
|
-
|
|
269
|
+
## Error Hierarchy
|
|
208
270
|
|
|
209
|
-
```
|
|
210
|
-
|
|
271
|
+
```
|
|
272
|
+
Error
|
|
273
|
+
└── ResourceXError
|
|
274
|
+
├── LocatorError (RXL parsing)
|
|
275
|
+
├── ManifestError (RXM validation)
|
|
276
|
+
├── ContentError (RXC consumption)
|
|
277
|
+
└── ResourceTypeError (Type registration)
|
|
278
|
+
|
|
279
|
+
└── RegistryError (Registry operations)
|
|
280
|
+
|
|
281
|
+
└── ARPError
|
|
282
|
+
├── ParseError (URL parsing)
|
|
283
|
+
├── TransportError (Transport not found)
|
|
284
|
+
└── SemanticError (Semantic not found)
|
|
211
285
|
```
|
|
212
286
|
|
|
213
287
|
## License
|
package/dist/arp.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@resourcexjs/arp";
|