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 CHANGED
@@ -1,213 +1,287 @@
1
1
  # resourcexjs
2
2
 
3
- Agent Resource Protocol (ARP) for AI Agents.
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 { createResourceX } from "resourcexjs";
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
- const rx = createResourceX();
36
+ // Link to local registry
37
+ await registry.link(rxr);
19
38
 
20
- // Resolve a remote text resource
21
- const resource = await rx.resolve("arp:text:https://example.com/file.txt");
22
- // Or use shorthand (@ is default)
23
- const resource = await rx.resolve("@text:https://example.com/file.txt");
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
- console.log(resource.type); // "text"
26
- console.log(resource.content); // file content as string
27
- console.log(resource.meta); // { url, semantic, transport, ... }
46
+ ### RXL - Resource Locator
28
47
 
29
- // Deposit a local text resource
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
- // Binary resources
33
- await rx.deposit("@binary:file://./data/image.png", imageBuffer);
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
- // Check if resource exists
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
- // Delete a resource
41
- await rx.delete("@text:file://./data/config.txt");
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
- ### URL Prefix
63
+ ### RXM - Resource Manifest
45
64
 
46
- - `arp:` - Standard prefix (always supported)
47
- - `@` - Shorthand alias (default, configurable via `alias` config)
65
+ Create resource metadata:
48
66
 
49
- ## ARP URL Format
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
- arp:{semantic}:{transport}://{location}
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
- - **semantic**: What the resource is (e.g., `text`, `binary`)
56
- - **transport**: How to access it (e.g., `https`, `http`, `file`)
57
- - **location**: Where to find it
105
+ ### RXR - Resource
58
106
 
59
- Examples:
107
+ Complete resource object (pure interface):
60
108
 
61
- - `arp:text:https://example.com/readme.txt`
62
- - `arp:binary:file:///path/to/image.png`
63
- - `arp:text:file://./local/file.txt`
109
+ ```typescript
110
+ interface RXR {
111
+ locator: RXL;
112
+ manifest: RXM;
113
+ content: RXC;
114
+ }
64
115
 
65
- ## Resource Definition
116
+ // Create from literals
117
+ const rxr: RXR = { locator, manifest, content };
118
+ ```
66
119
 
67
- Define custom resources as shortcuts for commonly used ARP URLs:
120
+ ### Registry
121
+
122
+ Resource storage and retrieval (from `@resourcexjs/registry`):
68
123
 
69
124
  ```typescript
70
- import { createResourceX } from "resourcexjs";
71
- import { join } from "path";
72
- import { homedir } from "os";
73
-
74
- const rx = createResourceX({
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
- // Use resource URL
92
- await rx.deposit("logs://app.log", "log entry");
93
- await rx.deposit("cache://data.bin", buffer);
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
- // Equivalent to full ARP URL
96
- await rx.deposit("arp:text:file://~/.myapp/logs/app.log", "log entry");
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
- ## API
148
+ ### Resource Types
149
+
150
+ Built-in types:
100
151
 
101
- ### `createResourceX(config?)`
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
- Create a ResourceX instance.
158
+ Define custom types:
104
159
 
105
160
  ```typescript
106
- const rx = createResourceX({
107
- timeout: 5000, // request timeout in ms
108
- transports: [], // custom transport handlers
109
- semantics: [], // custom semantic handlers
110
- resources: [], // resource definitions
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
- ### `rx.resolve(url)`
184
+ ### TypeHandlerChain
115
185
 
116
- Resolve an ARP or Resource URL and return the resource.
186
+ Responsibility chain for type handling (used internally):
117
187
 
118
188
  ```typescript
119
- const resource = await rx.resolve("arp:text:https://example.com/file.txt");
120
- // Returns: { type, content, meta }
189
+ import { createTypeHandlerChain, builtinTypes } from "resourcexjs";
121
190
 
122
- const resource = await rx.resolve("myresource://file.txt");
123
- // Also works with resource URLs
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
- ### `rx.deposit(url, data)`
198
+ ## ARP - Low-level I/O
127
199
 
128
- Deposit data to an ARP or Resource URL.
200
+ For direct file/network operations:
129
201
 
130
202
  ```typescript
131
- await rx.deposit("arp:text:file://./data/config.txt", "content");
132
- await rx.deposit("arp:binary:file://./data/image.png", buffer);
133
- ```
203
+ import { createARP } from "resourcexjs/arp";
134
204
 
135
- ### `rx.exists(url)`
205
+ const arp = createARP(); // Defaults include file, http, https, text, binary
136
206
 
137
- Check if a resource exists.
207
+ // Parse URL
208
+ const arl = arp.parse("arp:text:file://./config.txt");
138
209
 
139
- ```typescript
140
- const exists = await rx.exists("arp:text:file://./data/config.txt");
141
- // Returns: boolean
142
- ```
210
+ // Read
211
+ const resource = await arl.resolve();
212
+ console.log(resource.content); // string
143
213
 
144
- ### `rx.delete(url)`
214
+ // Write
215
+ await arl.deposit("hello world");
145
216
 
146
- Delete a resource.
147
-
148
- ```typescript
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
- ### `rx.parse(url)`
222
+ ## Exports
153
223
 
154
- Parse a URL without fetching.
224
+ ### Main Package (`resourcexjs`)
155
225
 
156
226
  ```typescript
157
- const parsed = rx.parse("arp:text:https://example.com/file.txt");
158
- // Returns: { semantic: "text", transport: "https", location: "example.com/file.txt" }
227
+ // Errors
228
+ export { ResourceXError, LocatorError, ManifestError, ContentError, ResourceTypeError };
159
229
 
160
- const parsed = rx.parse("myresource://file.txt");
161
- // Also works with resource URLs (expanded to full location)
162
- ```
230
+ // RXL (Locator)
231
+ export { parseRXL };
232
+ export type { RXL };
163
233
 
164
- ## Built-in Semantic Types
234
+ // RXM (Manifest)
235
+ export { createRXM };
236
+ export type { RXM, ManifestData };
165
237
 
166
- | Type | Content | Description |
167
- | -------- | -------- | ------------------------------ |
168
- | `text` | `string` | Plain text with UTF-8 encoding |
169
- | `binary` | `Buffer` | Raw binary, no transformation |
238
+ // RXC (Content)
239
+ export { createRXC, loadRXC };
240
+ export type { RXC };
170
241
 
171
- ## Resource Object
242
+ // RXR (Resource)
243
+ export type { RXR, ResourceType, ResourceSerializer, ResourceResolver };
172
244
 
173
- ```typescript
174
- interface Resource {
175
- type: string; // semantic type (e.g., "text", "binary")
176
- content: unknown; // parsed content (string for text, Buffer for binary)
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
- ## Error Handling
190
-
191
- All errors extend `ResourceXError`:
251
+ ### Registry Package (`@resourcexjs/registry`)
192
252
 
193
253
  ```typescript
194
- import { createResourceX, ResourceXError } from "resourcexjs";
254
+ export { createRegistry, ARPRegistry };
255
+ export { RegistryError };
256
+ export type { Registry, RegistryConfig };
257
+ ```
195
258
 
196
- const rx = createResourceX();
259
+ ### ARP Package (`resourcexjs/arp`)
197
260
 
198
- try {
199
- await rx.resolve("arp:text:https://example.com/file.txt");
200
- } catch (error) {
201
- if (error instanceof ResourceXError) {
202
- console.error("ResourceX error:", error.message);
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
- For fine-grained error handling, import specific error types from `@resourcexjs/core`:
269
+ ## Error Hierarchy
208
270
 
209
- ```typescript
210
- import { ParseError, TransportError, SemanticError } from "@resourcexjs/core";
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";