@resourcexjs/core 0.5.0 → 0.7.1

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,8 +1,8 @@
1
1
  # @resourcexjs/core
2
2
 
3
- Core implementation of Agent Resource Protocol (ARP).
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 instead. This package is for advanced usage and custom extensions.
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
- ## Usage
15
+ ## What's Inside
16
16
 
17
- ### Parse ARP URLs
17
+ Core building blocks for ResourceX:
18
18
 
19
- ```typescript
20
- import { parseARP } from "@resourcexjs/core";
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
- const parsed = parseARP("arp:text:https://example.com/file.txt");
23
- // { semantic: "text", transport: "https", location: "example.com/file.txt" }
24
- ```
27
+ ## API
28
+
29
+ ### RXL - Resource Locator
25
30
 
26
- ### Resolve Resources
31
+ Parse resource locator strings. Format: `[domain/path/]name[.type][@version]`
27
32
 
28
33
  ```typescript
29
- import { resolve } from "@resourcexjs/core";
34
+ import { parseRXL } from "@resourcexjs/core";
30
35
 
31
- const resource = await resolve("arp:text:https://example.com/file.txt");
32
- // { type: "text", content: "...", meta: { ... } }
36
+ const rxl = parseRXL("deepractice.ai/sean/assistant.prompt@1.0.0");
33
37
 
34
- const binary = await resolve("arp:binary:file://./image.png");
35
- // { type: "binary", content: Buffer, meta: { ... } }
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
- ### Deposit Resources
46
+ ### RXM - Resource Manifest
39
47
 
40
- ```typescript
41
- import { deposit } from "@resourcexjs/core";
48
+ Create and validate resource metadata:
42
49
 
43
- await deposit("arp:text:file://./data/config.txt", "hello world");
44
- await deposit("arp:binary:file://./data/image.png", buffer);
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
- ### Check Existence & Delete
65
+ Required fields: domain, name, type, version
48
66
 
49
- ```typescript
50
- import { resourceExists, resourceDelete } from "@resourcexjs/core";
67
+ ### RXC - Resource Content
68
+
69
+ Stream-based content (can only be consumed once, like fetch Response):
51
70
 
52
- const exists = await resourceExists("arp:text:file://./data/config.txt");
53
- await resourceDelete("arp:text:file://./data/config.txt");
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
- ### Custom Transport Handler
90
+ ### RXR - Resource
57
91
 
58
- Transport provides I/O primitives (read/write/list/exists/delete):
92
+ Complete resource object (pure interface, no factory):
59
93
 
60
94
  ```typescript
61
- import { registerTransportHandler, type TransportHandler } from "@resourcexjs/core";
62
-
63
- const s3Handler: TransportHandler = {
64
- name: "s3",
65
- capabilities: {
66
- canRead: true,
67
- canWrite: true,
68
- canList: true,
69
- canDelete: true,
70
- canStat: false,
71
- },
72
- async read(location: string): Promise<Buffer> {
73
- // read from S3...
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
- ### Custom Semantic Handler
111
+ ### Resource Types
89
112
 
90
- Semantic orchestrates Transport primitives to handle resource logic:
113
+ Built-in types:
91
114
 
92
115
  ```typescript
93
- import { registerSemanticHandler, type SemanticHandler, type Resource } from "@resourcexjs/core";
94
-
95
- const jsonHandler: SemanticHandler = {
96
- name: "json",
97
- async resolve(transport, location, context): Promise<Resource> {
98
- const buffer = await transport.read(location);
99
- return {
100
- type: "json",
101
- content: JSON.parse(buffer.toString("utf-8")),
102
- meta: {
103
- url: context.url,
104
- semantic: context.semantic,
105
- transport: context.transport,
106
- location: context.location,
107
- size: buffer.length,
108
- resolvedAt: context.timestamp.toISOString(),
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
- async deposit(transport, location, data, context): Promise<void> {
113
- if (!transport.write) throw new Error("Transport does not support write");
114
- const buffer = Buffer.from(JSON.stringify(data), "utf-8");
115
- await transport.write(location, buffer);
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
- ## Exports
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
- ### Functions
174
+ ### TypeHandlerChain
125
175
 
126
- - `parseARP(url)` - Parse ARP URL string
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
- ### Built-in Handlers
178
+ ```typescript
179
+ import { createTypeHandlerChain, builtinTypes } from "@resourcexjs/core";
138
180
 
139
- **Transport:**
181
+ // Create with initial types
182
+ const chain = createTypeHandlerChain(builtinTypes);
140
183
 
141
- - `httpsHandler` - HTTPS protocol (read-only)
142
- - `httpHandler` - HTTP protocol (read-only)
143
- - `fileHandler` - Local file system (read/write/list/delete)
144
- - `agentvmHandler(config?)` - AgentVM local storage (factory function, configurable parent directory)
184
+ // Or start empty
185
+ const chain = createTypeHandlerChain();
186
+ chain.register(customType);
187
+ chain.registerAll([type1, type2]);
145
188
 
146
- **Semantic:**
189
+ // Use the chain
190
+ chain.canHandle("text"); // → boolean
191
+ chain.getHandler("txt"); // → ResourceType (via alias)
147
192
 
148
- - `textHandler` - Plain text (UTF-8 encoding)
149
- - `binaryHandler` - Raw binary (Buffer passthrough)
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
- ### Error Classes
198
+ ## Error Handling
152
199
 
153
200
  ```typescript
154
201
  import {
155
- ResourceXError, // Base error
156
- ParseError, // ARP URL parsing failed
157
- TransportError, // Transport layer failed
158
- SemanticError, // Semantic layer failed
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
- ### Types
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
- import type {
166
- ParsedARP,
167
- Resource,
168
- ResourceMeta,
169
- SemanticContext,
170
- TransportHandler,
171
- TransportCapabilities,
172
- ResourceStat,
173
- SemanticHandler,
174
- TextResource,
175
- BinaryResource,
176
- BinaryInput,
177
- ResourceDefinition,
178
- ResourceRegistry,
179
- } from "@resourcexjs/core";
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