@onlyworlds/sdk 2.0.2 → 2.1.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 +174 -44
- package/dist/index.d.mts +306 -7
- package/dist/index.d.ts +306 -7
- package/dist/index.js +212 -12
- package/dist/index.mjs +211 -12
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -22,27 +22,29 @@ const client = new OnlyWorldsClient({
|
|
|
22
22
|
baseUrl: 'https://onlyworlds.com'
|
|
23
23
|
});
|
|
24
24
|
|
|
25
|
-
//
|
|
26
|
-
const
|
|
25
|
+
// Get your world (API keys are world-scoped)
|
|
26
|
+
const world = await client.worlds.get();
|
|
27
|
+
|
|
28
|
+
// Fetch characters (paginated)
|
|
29
|
+
const characters = await client.characters.list();
|
|
27
30
|
|
|
28
31
|
// Create a new location
|
|
29
|
-
const location = await client.
|
|
32
|
+
const location = await client.locations.create({
|
|
30
33
|
name: 'Dragon Peak',
|
|
31
34
|
description: 'A treacherous mountain peak where dragons nest',
|
|
32
|
-
supertype: 'mountain'
|
|
33
|
-
climate: 'cold'
|
|
35
|
+
supertype: 'mountain'
|
|
34
36
|
});
|
|
35
37
|
|
|
36
38
|
// Get a specific element
|
|
37
|
-
const character = await client.
|
|
39
|
+
const character = await client.characters.get('element-id');
|
|
38
40
|
|
|
39
41
|
// Update an element
|
|
40
|
-
await client.
|
|
42
|
+
await client.characters.update('element-id', {
|
|
41
43
|
name: 'Updated Name'
|
|
42
44
|
});
|
|
43
45
|
|
|
44
46
|
// Delete an element
|
|
45
|
-
await client.
|
|
47
|
+
await client.characters.delete('element-id');
|
|
46
48
|
```
|
|
47
49
|
|
|
48
50
|
## Features
|
|
@@ -50,36 +52,47 @@ await client.character.delete('element-id');
|
|
|
50
52
|
- ✅ **Full Type Safety** - Complete TypeScript definitions for all 22 OnlyWorlds element types
|
|
51
53
|
- ✅ **Auto-Generated Types** - Types synchronized with the latest OnlyWorlds schema
|
|
52
54
|
- ✅ **CRUD Operations** - Create, read, update, and delete operations for all elements
|
|
55
|
+
- ✅ **Token Management** - Built-in support for OnlyWorlds token rating system
|
|
53
56
|
- ✅ **Branded Types** - Compile-time safety for element relationships
|
|
54
57
|
- ✅ **Zero Runtime Overhead** - Type system has no runtime cost
|
|
55
58
|
- ✅ **Modern ESM/CJS** - Supports both ES modules and CommonJS
|
|
56
59
|
|
|
57
|
-
##
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
60
|
+
## API Reference
|
|
61
|
+
|
|
62
|
+
### World Endpoint
|
|
63
|
+
|
|
64
|
+
The `worlds` resource is special because API keys are world-scoped (one key = one world). The endpoint returns a single `World` object directly, not a paginated list.
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
// Get your world
|
|
68
|
+
const world = await client.worlds.get();
|
|
69
|
+
|
|
70
|
+
// Update your world
|
|
71
|
+
const updated = await client.worlds.update({
|
|
72
|
+
description: 'A dark fantasy realm'
|
|
73
|
+
});
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
**Note**: The `worlds` resource only has `get()` and `update()` methods (no `list()`, `create()`, or `delete()`) because API keys are world-scoped.
|
|
77
|
+
|
|
78
|
+
### Element Endpoints
|
|
79
|
+
|
|
80
|
+
All other element types return paginated results:
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
// List with pagination
|
|
84
|
+
const response = await client.characters.list({
|
|
85
|
+
limit: 10,
|
|
86
|
+
offset: 0,
|
|
87
|
+
ordering: '-created_at',
|
|
88
|
+
search: 'dragon'
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
console.log(response.count); // Total count
|
|
92
|
+
console.log(response.results); // Array of Characters
|
|
93
|
+
console.log(response.next); // URL for next page
|
|
94
|
+
console.log(response.previous); // URL for previous page
|
|
95
|
+
```
|
|
83
96
|
|
|
84
97
|
## Type-Safe Relationships
|
|
85
98
|
|
|
@@ -94,23 +107,140 @@ const character: Character = {
|
|
|
94
107
|
};
|
|
95
108
|
```
|
|
96
109
|
|
|
97
|
-
##
|
|
110
|
+
## Token Management
|
|
111
|
+
|
|
112
|
+
OnlyWorlds provides a token rating system for tracking API usage and enabling AI-powered features. Users get a daily token allowance (default: 10,000 tokens) that resets every day.
|
|
113
|
+
|
|
114
|
+
### Working Reference Implementation
|
|
115
|
+
|
|
116
|
+
See [base-tool/src/llm/token-service.ts](https://github.com/OnlyWorlds/base-tool) for the complete working implementation that this SDK enables.
|
|
98
117
|
|
|
99
|
-
|
|
118
|
+
### Check Token Status
|
|
100
119
|
|
|
101
|
-
|
|
120
|
+
```typescript
|
|
121
|
+
// Get current token status
|
|
122
|
+
const status = await client.tokens.getStatus();
|
|
123
|
+
|
|
124
|
+
console.log(`Available: ${status.tokens_available_today}/${status.token_rating}`);
|
|
125
|
+
console.log(`Used today: ${status.tokens_used_today}`);
|
|
126
|
+
console.log(`Active sessions: ${status.sessions_active}`);
|
|
127
|
+
console.log(`Last reset: ${status.last_reset}`);
|
|
128
|
+
```
|
|
102
129
|
|
|
103
|
-
|
|
130
|
+
### Consume Tokens
|
|
104
131
|
|
|
105
|
-
|
|
132
|
+
Report token consumption when your tool uses AI features or other token-tracked services:
|
|
106
133
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
134
|
+
```typescript
|
|
135
|
+
// Report token usage
|
|
136
|
+
const result = await client.tokens.consume({
|
|
137
|
+
amount: 500,
|
|
138
|
+
service: 'my_worldbuilding_tool',
|
|
139
|
+
metadata: {
|
|
140
|
+
feature: 'character_generation',
|
|
141
|
+
model: 'gpt-4',
|
|
142
|
+
prompt_tokens: 300,
|
|
143
|
+
completion_tokens: 200
|
|
144
|
+
}
|
|
145
|
+
});
|
|
110
146
|
|
|
111
|
-
|
|
147
|
+
// Check if consumption succeeded
|
|
148
|
+
if (result.error) {
|
|
149
|
+
console.warn('Token warning:', result.error);
|
|
150
|
+
}
|
|
112
151
|
|
|
113
|
-
|
|
152
|
+
console.log(`Consumed ${result.tokens_consumed} tokens`);
|
|
153
|
+
console.log(`${result.tokens_remaining} tokens remaining`);
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
**Important**: The API allows consumption even when exceeding available tokens (tracks as debt), but returns a warning in the `error` field.
|
|
157
|
+
|
|
158
|
+
### Advanced: Encrypted API Key Access
|
|
159
|
+
|
|
160
|
+
For tools that need direct OpenAI API access using OnlyWorlds tokens:
|
|
161
|
+
|
|
162
|
+
```typescript
|
|
163
|
+
// Get encrypted OpenAI key (requires 100+ tokens)
|
|
164
|
+
const access = await client.tokens.getAccessKey();
|
|
165
|
+
|
|
166
|
+
console.log('Session ID:', access.session_id);
|
|
167
|
+
console.log('Expires:', access.expires_at);
|
|
168
|
+
console.log('Available tokens:', access.tokens_available);
|
|
169
|
+
|
|
170
|
+
// Decrypt the key client-side (see base-tool for full implementation)
|
|
171
|
+
// 1. Derive decryption key from world ID using SHA-256
|
|
172
|
+
// 2. Use 'fernet' npm package to decrypt
|
|
173
|
+
// 3. Use decrypted OpenAI key for direct API calls
|
|
174
|
+
// 4. Report usage with session_id
|
|
175
|
+
|
|
176
|
+
// See base-tool/src/llm/token-service.ts:99-235 for complete example
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
**Full decryption implementation** (based on base-tool):
|
|
180
|
+
|
|
181
|
+
```typescript
|
|
182
|
+
import { fernet } from 'fernet';
|
|
183
|
+
|
|
184
|
+
// Derive decryption key from world ID
|
|
185
|
+
async function deriveKey(worldId: string): Promise<string> {
|
|
186
|
+
const salt = 'onlyworlds-token-api-2024-public-salt';
|
|
187
|
+
const keyMaterial = `${worldId}:${salt}`;
|
|
188
|
+
const encoder = new TextEncoder();
|
|
189
|
+
const data = encoder.encode(keyMaterial);
|
|
190
|
+
const hashBuffer = await crypto.subtle.digest('SHA-256', data);
|
|
191
|
+
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
|
192
|
+
const base64 = btoa(String.fromCharCode(...hashArray));
|
|
193
|
+
return base64.replace(/\+/g, '-').replace(/\//g, '_');
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Decrypt the API key
|
|
197
|
+
async function decryptApiKey(encryptedKey: string, worldId: string): Promise<string> {
|
|
198
|
+
const derivedKey = await deriveKey(worldId);
|
|
199
|
+
const secret = new fernet.Secret(derivedKey);
|
|
200
|
+
const token = new fernet.Token({
|
|
201
|
+
secret: secret,
|
|
202
|
+
token: encryptedKey,
|
|
203
|
+
ttl: 0 // Don't enforce TTL client-side
|
|
204
|
+
});
|
|
205
|
+
return token.decode();
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// Usage
|
|
209
|
+
const world = await client.worlds.get();
|
|
210
|
+
const access = await client.tokens.getAccessKey();
|
|
211
|
+
const apiKey = await decryptApiKey(access.encrypted_key, world.id);
|
|
212
|
+
|
|
213
|
+
// Use apiKey for OpenAI API calls, then report usage:
|
|
214
|
+
await client.tokens.consume({
|
|
215
|
+
amount: tokensUsed,
|
|
216
|
+
sessionId: access.session_id,
|
|
217
|
+
service: 'direct_openai',
|
|
218
|
+
metadata: { model: 'gpt-4', /* ... */ }
|
|
219
|
+
});
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
### Session Management
|
|
223
|
+
|
|
224
|
+
```typescript
|
|
225
|
+
// Revoke a specific session
|
|
226
|
+
await client.tokens.revokeSession(sessionId);
|
|
227
|
+
|
|
228
|
+
// Revoke all sessions (emergency cleanup)
|
|
229
|
+
const result = await client.tokens.revokeAllSessions();
|
|
230
|
+
console.log(`Revoked ${result.sessions_revoked} sessions`);
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
### Encryption Info
|
|
234
|
+
|
|
235
|
+
Get public encryption details (no auth required):
|
|
236
|
+
|
|
237
|
+
```typescript
|
|
238
|
+
const info = await client.tokens.getEncryptionInfo();
|
|
239
|
+
console.log('Algorithm:', info.algorithm);
|
|
240
|
+
console.log('Key derivation:', info.key_derivation);
|
|
241
|
+
console.log('Public salt:', info.salt);
|
|
242
|
+
console.log(info.javascript_example);
|
|
243
|
+
```
|
|
114
244
|
|
|
115
245
|
## License
|
|
116
246
|
|
package/dist/index.d.mts
CHANGED
|
@@ -553,8 +553,22 @@ interface Pin extends BaseElement {
|
|
|
553
553
|
map?: ElementId<ElementType.Map>;
|
|
554
554
|
element_type?: ElementType;
|
|
555
555
|
element_id?: AnyElementId;
|
|
556
|
+
/**
|
|
557
|
+
* X coordinate on map (must be integer)
|
|
558
|
+
* The API requires integer values. Float coordinates from mouse events
|
|
559
|
+
* or calculations should be rounded: Math.round(x)
|
|
560
|
+
*/
|
|
556
561
|
x?: number;
|
|
562
|
+
/**
|
|
563
|
+
* Y coordinate on map (must be integer)
|
|
564
|
+
* The API requires integer values. Float coordinates from mouse events
|
|
565
|
+
* or calculations should be rounded: Math.round(y)
|
|
566
|
+
*/
|
|
557
567
|
y?: number;
|
|
568
|
+
/**
|
|
569
|
+
* Z coordinate for 3D positioning (must be integer)
|
|
570
|
+
* The API requires integer values. Float coordinates should be rounded: Math.round(z)
|
|
571
|
+
*/
|
|
558
572
|
z?: number;
|
|
559
573
|
}
|
|
560
574
|
/**
|
|
@@ -2711,6 +2725,272 @@ declare function createElementIds<T extends ElementType>(ids: string[]): Element
|
|
|
2711
2725
|
*/
|
|
2712
2726
|
declare function createAnyElementId(id: string): AnyElementId;
|
|
2713
2727
|
|
|
2728
|
+
/**
|
|
2729
|
+
* OnlyWorlds Token Management API Types
|
|
2730
|
+
*
|
|
2731
|
+
* Types for the token rating system that provides daily token allowances
|
|
2732
|
+
* for API usage and AI integration features.
|
|
2733
|
+
*/
|
|
2734
|
+
/**
|
|
2735
|
+
* Current token status for the authenticated user
|
|
2736
|
+
*/
|
|
2737
|
+
interface TokenStatus {
|
|
2738
|
+
/** Remaining tokens available for today */
|
|
2739
|
+
tokens_available_today: number;
|
|
2740
|
+
/** User's daily token allowance (default: 10000) */
|
|
2741
|
+
token_rating: number;
|
|
2742
|
+
/** Tokens consumed so far today */
|
|
2743
|
+
tokens_used_today: number;
|
|
2744
|
+
/** Date of last reset (ISO format) */
|
|
2745
|
+
last_reset: string;
|
|
2746
|
+
/** Number of active token sessions */
|
|
2747
|
+
sessions_active: number;
|
|
2748
|
+
}
|
|
2749
|
+
/**
|
|
2750
|
+
* Response from token consumption endpoint
|
|
2751
|
+
*/
|
|
2752
|
+
interface TokenConsumeResponse {
|
|
2753
|
+
/** Whether the consumption was successful */
|
|
2754
|
+
success: boolean;
|
|
2755
|
+
/** Actual tokens consumed */
|
|
2756
|
+
tokens_consumed: number;
|
|
2757
|
+
/** Updated token balance */
|
|
2758
|
+
tokens_remaining: number;
|
|
2759
|
+
/** User's daily allowance */
|
|
2760
|
+
token_rating: number;
|
|
2761
|
+
/** Warning message if insufficient tokens (consumption still succeeds but tracks debt) */
|
|
2762
|
+
error?: string | null;
|
|
2763
|
+
}
|
|
2764
|
+
/**
|
|
2765
|
+
* Parameters for consuming tokens
|
|
2766
|
+
*/
|
|
2767
|
+
interface TokenConsumeParams {
|
|
2768
|
+
/** Number of tokens to consume (required) */
|
|
2769
|
+
amount: number;
|
|
2770
|
+
/** Service identifier (default: "sdk_client") */
|
|
2771
|
+
service?: string;
|
|
2772
|
+
/** Session ID from access key response (optional) */
|
|
2773
|
+
sessionId?: string | null;
|
|
2774
|
+
/** Additional metadata for analytics (optional) */
|
|
2775
|
+
metadata?: Record<string, any> | null;
|
|
2776
|
+
}
|
|
2777
|
+
/**
|
|
2778
|
+
* Response from getting encrypted access key (advanced use case)
|
|
2779
|
+
*/
|
|
2780
|
+
interface AccessKeyResponse {
|
|
2781
|
+
/** Base64 encoded encrypted OpenAI key */
|
|
2782
|
+
encrypted_key: string;
|
|
2783
|
+
/** Key expiration timestamp (ISO format) */
|
|
2784
|
+
expires_at: string;
|
|
2785
|
+
/** Session ID for tracking usage */
|
|
2786
|
+
session_id: string;
|
|
2787
|
+
/** Current token balance */
|
|
2788
|
+
tokens_available: number;
|
|
2789
|
+
/** User's token allowance */
|
|
2790
|
+
token_rating: number;
|
|
2791
|
+
/** Encryption algorithm used */
|
|
2792
|
+
encryption_method: string;
|
|
2793
|
+
}
|
|
2794
|
+
/**
|
|
2795
|
+
* Public encryption information for client-side decryption
|
|
2796
|
+
*/
|
|
2797
|
+
interface EncryptionInfo {
|
|
2798
|
+
/** Encryption algorithm (e.g., "Fernet") */
|
|
2799
|
+
algorithm: string;
|
|
2800
|
+
/** Key derivation method (e.g., "SHA256") */
|
|
2801
|
+
key_derivation: string;
|
|
2802
|
+
/** Public salt used in key derivation */
|
|
2803
|
+
salt: string;
|
|
2804
|
+
/** Format description */
|
|
2805
|
+
format: string;
|
|
2806
|
+
/** Human-readable description */
|
|
2807
|
+
description: string;
|
|
2808
|
+
/** Example JavaScript implementation */
|
|
2809
|
+
javascript_example: string;
|
|
2810
|
+
}
|
|
2811
|
+
/**
|
|
2812
|
+
* Response from session revocation
|
|
2813
|
+
*/
|
|
2814
|
+
interface RevokeSessionResponse {
|
|
2815
|
+
/** Whether revocation was successful */
|
|
2816
|
+
success: boolean;
|
|
2817
|
+
/** Status message */
|
|
2818
|
+
message: string;
|
|
2819
|
+
}
|
|
2820
|
+
/**
|
|
2821
|
+
* Response from revoking all sessions
|
|
2822
|
+
*/
|
|
2823
|
+
interface RevokeAllSessionsResponse {
|
|
2824
|
+
/** Whether revocation was successful */
|
|
2825
|
+
success: boolean;
|
|
2826
|
+
/** Number of sessions revoked */
|
|
2827
|
+
sessions_revoked: number;
|
|
2828
|
+
}
|
|
2829
|
+
/**
|
|
2830
|
+
* Game tier / subscription level
|
|
2831
|
+
* (affects future features, currently all tiers get same token rating)
|
|
2832
|
+
*/
|
|
2833
|
+
declare enum GameTier {
|
|
2834
|
+
FREE = "free",
|
|
2835
|
+
SILVER = "silver",
|
|
2836
|
+
GOLD = "gold",
|
|
2837
|
+
PLATINUM = "platinum",
|
|
2838
|
+
DIAMOND = "diamond",
|
|
2839
|
+
DELUXE = "deluxe"
|
|
2840
|
+
}
|
|
2841
|
+
|
|
2842
|
+
/**
|
|
2843
|
+
* Token Management Resource
|
|
2844
|
+
*
|
|
2845
|
+
* Provides access to the OnlyWorlds token rating system API.
|
|
2846
|
+
* Based on the working implementation in base-tool/src/llm/token-service.ts
|
|
2847
|
+
*/
|
|
2848
|
+
|
|
2849
|
+
/**
|
|
2850
|
+
* Token Management Resource
|
|
2851
|
+
*
|
|
2852
|
+
* Handles token status checking, consumption tracking, and encrypted key access
|
|
2853
|
+
* for AI integration features.
|
|
2854
|
+
*
|
|
2855
|
+
* All endpoints require authentication (API-Key + API-Pin headers).
|
|
2856
|
+
*
|
|
2857
|
+
* Example usage:
|
|
2858
|
+
* ```typescript
|
|
2859
|
+
* const client = new OnlyWorldsClient({ apiKey, apiPin });
|
|
2860
|
+
*
|
|
2861
|
+
* // Check token status
|
|
2862
|
+
* const status = await client.tokens.getStatus();
|
|
2863
|
+
* console.log(`${status.tokens_available_today}/${status.token_rating} tokens available`);
|
|
2864
|
+
*
|
|
2865
|
+
* // Consume tokens
|
|
2866
|
+
* await client.tokens.consume({
|
|
2867
|
+
* amount: 500,
|
|
2868
|
+
* service: 'my_tool',
|
|
2869
|
+
* metadata: { feature: 'chat', model: 'gpt-4' }
|
|
2870
|
+
* });
|
|
2871
|
+
* ```
|
|
2872
|
+
*/
|
|
2873
|
+
declare class TokenResource {
|
|
2874
|
+
private client;
|
|
2875
|
+
constructor(client: OnlyWorldsClient);
|
|
2876
|
+
/**
|
|
2877
|
+
* Get current token status for authenticated user
|
|
2878
|
+
*
|
|
2879
|
+
* Returns daily token allowance, usage, and availability.
|
|
2880
|
+
* Matches base-tool's checkStatus() pattern.
|
|
2881
|
+
*
|
|
2882
|
+
* @returns Current token status
|
|
2883
|
+
* @example
|
|
2884
|
+
* ```typescript
|
|
2885
|
+
* const status = await client.tokens.getStatus();
|
|
2886
|
+
* console.log(`Available: ${status.tokens_available_today}/${status.token_rating}`);
|
|
2887
|
+
* console.log(`Used today: ${status.tokens_used_today}`);
|
|
2888
|
+
* console.log(`Active sessions: ${status.sessions_active}`);
|
|
2889
|
+
* ```
|
|
2890
|
+
*/
|
|
2891
|
+
getStatus(): Promise<TokenStatus>;
|
|
2892
|
+
/**
|
|
2893
|
+
* Consume tokens for service usage
|
|
2894
|
+
*
|
|
2895
|
+
* Reports token consumption to track daily usage. Allows consumption even if
|
|
2896
|
+
* exceeds available tokens (tracks as debt), but warns via error field.
|
|
2897
|
+
* Matches base-tool's reportUsage() pattern.
|
|
2898
|
+
*
|
|
2899
|
+
* @param params - Token consumption parameters
|
|
2900
|
+
* @returns Consumption result with updated balance
|
|
2901
|
+
* @example
|
|
2902
|
+
* ```typescript
|
|
2903
|
+
* const result = await client.tokens.consume({
|
|
2904
|
+
* amount: 500,
|
|
2905
|
+
* service: 'worldbuilding_tool',
|
|
2906
|
+
* metadata: {
|
|
2907
|
+
* feature: 'character_generation',
|
|
2908
|
+
* model: 'gpt-4',
|
|
2909
|
+
* prompt_tokens: 300,
|
|
2910
|
+
* completion_tokens: 200
|
|
2911
|
+
* }
|
|
2912
|
+
* });
|
|
2913
|
+
*
|
|
2914
|
+
* if (result.error) {
|
|
2915
|
+
* console.warn('Token warning:', result.error);
|
|
2916
|
+
* }
|
|
2917
|
+
* console.log(`${result.tokens_remaining} tokens remaining`);
|
|
2918
|
+
* ```
|
|
2919
|
+
*/
|
|
2920
|
+
consume(params: TokenConsumeParams): Promise<TokenConsumeResponse>;
|
|
2921
|
+
/**
|
|
2922
|
+
* Get encrypted OpenAI API key (advanced use case)
|
|
2923
|
+
*
|
|
2924
|
+
* Requires minimum 100 tokens available. Creates a 1-hour session for tracking.
|
|
2925
|
+
* Returns encrypted key that must be decrypted client-side using Fernet.
|
|
2926
|
+
*
|
|
2927
|
+
* See base-tool/src/llm/token-service.ts:99-155 for full implementation example
|
|
2928
|
+
* including client-side decryption with the 'fernet' npm package.
|
|
2929
|
+
*
|
|
2930
|
+
* @returns Encrypted access key and session info
|
|
2931
|
+
* @throws Error if insufficient tokens (< 100)
|
|
2932
|
+
* @example
|
|
2933
|
+
* ```typescript
|
|
2934
|
+
* // Get encrypted key
|
|
2935
|
+
* const access = await client.tokens.getAccessKey();
|
|
2936
|
+
*
|
|
2937
|
+
* // Decrypt using fernet library (see base-tool for full example)
|
|
2938
|
+
* // 1. Derive key from world ID using SHA-256
|
|
2939
|
+
* // 2. Use 'fernet' npm package to decrypt
|
|
2940
|
+
* // 3. Use decrypted OpenAI key for direct API calls
|
|
2941
|
+
* // 4. Report usage with access.session_id
|
|
2942
|
+
*
|
|
2943
|
+
* console.log('Session:', access.session_id);
|
|
2944
|
+
* console.log('Expires:', access.expires_at);
|
|
2945
|
+
* ```
|
|
2946
|
+
*/
|
|
2947
|
+
getAccessKey(): Promise<AccessKeyResponse>;
|
|
2948
|
+
/**
|
|
2949
|
+
* Revoke a specific token session
|
|
2950
|
+
*
|
|
2951
|
+
* Invalidates the session ID obtained from getAccessKey().
|
|
2952
|
+
* Use when cleaning up or on logout.
|
|
2953
|
+
*
|
|
2954
|
+
* @param sessionId - Session ID to revoke
|
|
2955
|
+
* @returns Revocation result
|
|
2956
|
+
* @example
|
|
2957
|
+
* ```typescript
|
|
2958
|
+
* await client.tokens.revokeSession('session-id-here');
|
|
2959
|
+
* ```
|
|
2960
|
+
*/
|
|
2961
|
+
revokeSession(sessionId: string): Promise<RevokeSessionResponse>;
|
|
2962
|
+
/**
|
|
2963
|
+
* Revoke all active sessions (emergency use)
|
|
2964
|
+
*
|
|
2965
|
+
* Invalidates all token sessions for the authenticated user.
|
|
2966
|
+
* Use for security cleanup or when sessions are stuck.
|
|
2967
|
+
*
|
|
2968
|
+
* @returns Revocation result with count of revoked sessions
|
|
2969
|
+
* @example
|
|
2970
|
+
* ```typescript
|
|
2971
|
+
* const result = await client.tokens.revokeAllSessions();
|
|
2972
|
+
* console.log(`Revoked ${result.sessions_revoked} sessions`);
|
|
2973
|
+
* ```
|
|
2974
|
+
*/
|
|
2975
|
+
revokeAllSessions(): Promise<RevokeAllSessionsResponse>;
|
|
2976
|
+
/**
|
|
2977
|
+
* Get public encryption info (no auth required)
|
|
2978
|
+
*
|
|
2979
|
+
* Returns algorithm details and example code for client-side decryption.
|
|
2980
|
+
* Public endpoint - can be called without authentication.
|
|
2981
|
+
*
|
|
2982
|
+
* @returns Encryption algorithm and implementation details
|
|
2983
|
+
* @example
|
|
2984
|
+
* ```typescript
|
|
2985
|
+
* const info = await client.tokens.getEncryptionInfo();
|
|
2986
|
+
* console.log('Algorithm:', info.algorithm);
|
|
2987
|
+
* console.log('Key derivation:', info.key_derivation);
|
|
2988
|
+
* console.log(info.javascript_example);
|
|
2989
|
+
* ```
|
|
2990
|
+
*/
|
|
2991
|
+
getEncryptionInfo(): Promise<EncryptionInfo>;
|
|
2992
|
+
}
|
|
2993
|
+
|
|
2714
2994
|
interface OnlyWorldsConfig {
|
|
2715
2995
|
apiKey: string;
|
|
2716
2996
|
apiPin: string;
|
|
@@ -2741,6 +3021,29 @@ declare class Resource<T, TInput> {
|
|
|
2741
3021
|
create(data: TInput): Promise<T>;
|
|
2742
3022
|
update(id: string, data: Partial<TInput>): Promise<T>;
|
|
2743
3023
|
delete(id: string): Promise<void>;
|
|
3024
|
+
/**
|
|
3025
|
+
* Round Pin coordinates to integers (API requirement)
|
|
3026
|
+
* @private
|
|
3027
|
+
*/
|
|
3028
|
+
private roundPinCoordinates;
|
|
3029
|
+
}
|
|
3030
|
+
/**
|
|
3031
|
+
* Special resource class for World endpoint
|
|
3032
|
+
* The /world/ endpoint returns a single World object directly (not paginated)
|
|
3033
|
+
* because API keys are world-scoped (one key = one world)
|
|
3034
|
+
*/
|
|
3035
|
+
declare class WorldResource {
|
|
3036
|
+
private client;
|
|
3037
|
+
constructor(client: OnlyWorldsClient);
|
|
3038
|
+
/**
|
|
3039
|
+
* Get the world associated with the current API key
|
|
3040
|
+
* Returns the world directly (not wrapped in pagination)
|
|
3041
|
+
*/
|
|
3042
|
+
get(): Promise<World>;
|
|
3043
|
+
/**
|
|
3044
|
+
* Update the current world
|
|
3045
|
+
*/
|
|
3046
|
+
update(data: Partial<WorldInput>): Promise<World>;
|
|
2744
3047
|
}
|
|
2745
3048
|
/**
|
|
2746
3049
|
* Main OnlyWorlds API client
|
|
@@ -2748,7 +3051,8 @@ declare class Resource<T, TInput> {
|
|
|
2748
3051
|
declare class OnlyWorldsClient {
|
|
2749
3052
|
private baseUrl;
|
|
2750
3053
|
private headers;
|
|
2751
|
-
worlds:
|
|
3054
|
+
worlds: WorldResource;
|
|
3055
|
+
tokens: TokenResource;
|
|
2752
3056
|
abilities: Resource<Ability, AbilityInput>;
|
|
2753
3057
|
characters: Resource<Character, CharacterInput>;
|
|
2754
3058
|
collectives: Resource<Collective, CollectiveInput>;
|
|
@@ -2779,15 +3083,10 @@ declare class OnlyWorldsClient {
|
|
|
2779
3083
|
params?: Record<string, any>;
|
|
2780
3084
|
body?: any;
|
|
2781
3085
|
}): Promise<T>;
|
|
2782
|
-
/**
|
|
2783
|
-
* Get all worlds accessible with current credentials
|
|
2784
|
-
* @deprecated Use client.worlds.list() instead for consistent API
|
|
2785
|
-
*/
|
|
2786
|
-
getWorlds(): Promise<ApiResponse<World>>;
|
|
2787
3086
|
/**
|
|
2788
3087
|
* Helper to convert nested objects to _id/_ids format
|
|
2789
3088
|
*/
|
|
2790
3089
|
static prepareInput<T extends Record<string, any>>(data: T): any;
|
|
2791
3090
|
}
|
|
2792
3091
|
|
|
2793
|
-
export { type Ability, type AbilityInput, type AnyElementId, type ApiResponse, type BaseElement, type Character, type CharacterInput, type Collective, type CollectiveInput, type Construct, type ConstructInput, type Creature, type CreatureInput, ELEMENT_ICONS, ELEMENT_LABELS, ELEMENT_SECTIONS, ELEMENT_UNICODE_ICONS, type ElementId, type ElementIds, ElementType, type Event, type EventInput, FIELD_SCHEMA, type Family, type FamilyInput, type FieldInfo, type FieldType, type Institution, type InstitutionInput, type Language, type LanguageInput, type Law, type LawInput, type ListOptions, type Location, type LocationInput, type Map, type MapInput, type Marker, type MarkerInput, type Narrative, type NarrativeInput, ONLYWORLDS_VERSION, type Object$1 as Object, type ObjectInput, OnlyWorldsClient, type OnlyWorldsConfig, type Phenomenon, type PhenomenonInput, type Pin, type PinInput, type Relation, type RelationInput, type SectionInfo, type Species, type SpeciesInput, type Title, type TitleInput, type Trait, type TraitInput, type World, type WorldInput, type Zone, type ZoneInput, createAnyElementId, createElementId, createElementIds, getElementIcon, getElementLabel, getElementSections, getElementUnicodeIcon };
|
|
3092
|
+
export { type Ability, type AbilityInput, type AccessKeyResponse, type AnyElementId, type ApiResponse, type BaseElement, type Character, type CharacterInput, type Collective, type CollectiveInput, type Construct, type ConstructInput, type Creature, type CreatureInput, ELEMENT_ICONS, ELEMENT_LABELS, ELEMENT_SECTIONS, ELEMENT_UNICODE_ICONS, type ElementId, type ElementIds, ElementType, type EncryptionInfo, type Event, type EventInput, FIELD_SCHEMA, type Family, type FamilyInput, type FieldInfo, type FieldType, GameTier, type Institution, type InstitutionInput, type Language, type LanguageInput, type Law, type LawInput, type ListOptions, type Location, type LocationInput, type Map, type MapInput, type Marker, type MarkerInput, type Narrative, type NarrativeInput, ONLYWORLDS_VERSION, type Object$1 as Object, type ObjectInput, OnlyWorldsClient, type OnlyWorldsConfig, type Phenomenon, type PhenomenonInput, type Pin, type PinInput, type Relation, type RelationInput, type RevokeAllSessionsResponse, type RevokeSessionResponse, type SectionInfo, type Species, type SpeciesInput, type Title, type TitleInput, type TokenConsumeParams, type TokenConsumeResponse, type TokenStatus, type Trait, type TraitInput, type World, type WorldInput, type Zone, type ZoneInput, createAnyElementId, createElementId, createElementIds, getElementIcon, getElementLabel, getElementSections, getElementUnicodeIcon };
|