@onlyworlds/sdk 2.0.1 → 2.1.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/dist/index.js CHANGED
@@ -26,6 +26,7 @@ __export(index_exports, {
26
26
  ELEMENT_UNICODE_ICONS: () => ELEMENT_UNICODE_ICONS,
27
27
  ElementType: () => ElementType,
28
28
  FIELD_SCHEMA: () => FIELD_SCHEMA,
29
+ GameTier: () => GameTier,
29
30
  ONLYWORLDS_VERSION: () => ONLYWORLDS_VERSION,
30
31
  OnlyWorldsClient: () => OnlyWorldsClient,
31
32
  createAnyElementId: () => createAnyElementId,
@@ -38,6 +39,151 @@ __export(index_exports, {
38
39
  });
39
40
  module.exports = __toCommonJS(index_exports);
40
41
 
42
+ // src/token-resource.ts
43
+ var TokenResource = class {
44
+ constructor(client) {
45
+ this.client = client;
46
+ }
47
+ /**
48
+ * Get current token status for authenticated user
49
+ *
50
+ * Returns daily token allowance, usage, and availability.
51
+ * Matches base-tool's checkStatus() pattern.
52
+ *
53
+ * @returns Current token status
54
+ * @example
55
+ * ```typescript
56
+ * const status = await client.tokens.getStatus();
57
+ * console.log(`Available: ${status.tokens_available_today}/${status.token_rating}`);
58
+ * console.log(`Used today: ${status.tokens_used_today}`);
59
+ * console.log(`Active sessions: ${status.sessions_active}`);
60
+ * ```
61
+ */
62
+ async getStatus() {
63
+ return this.client.request("GET", "/tokens/status/");
64
+ }
65
+ /**
66
+ * Consume tokens for service usage
67
+ *
68
+ * Reports token consumption to track daily usage. Allows consumption even if
69
+ * exceeds available tokens (tracks as debt), but warns via error field.
70
+ * Matches base-tool's reportUsage() pattern.
71
+ *
72
+ * @param params - Token consumption parameters
73
+ * @returns Consumption result with updated balance
74
+ * @example
75
+ * ```typescript
76
+ * const result = await client.tokens.consume({
77
+ * amount: 500,
78
+ * service: 'worldbuilding_tool',
79
+ * metadata: {
80
+ * feature: 'character_generation',
81
+ * model: 'gpt-4',
82
+ * prompt_tokens: 300,
83
+ * completion_tokens: 200
84
+ * }
85
+ * });
86
+ *
87
+ * if (result.error) {
88
+ * console.warn('Token warning:', result.error);
89
+ * }
90
+ * console.log(`${result.tokens_remaining} tokens remaining`);
91
+ * ```
92
+ */
93
+ async consume(params) {
94
+ return this.client.request("POST", "/tokens/consume/", {
95
+ body: {
96
+ amount: params.amount,
97
+ service: params.service || "sdk_client",
98
+ session_id: params.sessionId ?? null,
99
+ metadata: params.metadata ?? null
100
+ }
101
+ });
102
+ }
103
+ /**
104
+ * Get encrypted OpenAI API key (advanced use case)
105
+ *
106
+ * Requires minimum 100 tokens available. Creates a 1-hour session for tracking.
107
+ * Returns encrypted key that must be decrypted client-side using Fernet.
108
+ *
109
+ * See base-tool/src/llm/token-service.ts:99-155 for full implementation example
110
+ * including client-side decryption with the 'fernet' npm package.
111
+ *
112
+ * @returns Encrypted access key and session info
113
+ * @throws Error if insufficient tokens (< 100)
114
+ * @example
115
+ * ```typescript
116
+ * // Get encrypted key
117
+ * const access = await client.tokens.getAccessKey();
118
+ *
119
+ * // Decrypt using fernet library (see base-tool for full example)
120
+ * // 1. Derive key from world ID using SHA-256
121
+ * // 2. Use 'fernet' npm package to decrypt
122
+ * // 3. Use decrypted OpenAI key for direct API calls
123
+ * // 4. Report usage with access.session_id
124
+ *
125
+ * console.log('Session:', access.session_id);
126
+ * console.log('Expires:', access.expires_at);
127
+ * ```
128
+ */
129
+ async getAccessKey() {
130
+ return this.client.request("GET", "/tokens/access-key/");
131
+ }
132
+ /**
133
+ * Revoke a specific token session
134
+ *
135
+ * Invalidates the session ID obtained from getAccessKey().
136
+ * Use when cleaning up or on logout.
137
+ *
138
+ * @param sessionId - Session ID to revoke
139
+ * @returns Revocation result
140
+ * @example
141
+ * ```typescript
142
+ * await client.tokens.revokeSession('session-id-here');
143
+ * ```
144
+ */
145
+ async revokeSession(sessionId) {
146
+ return this.client.request(
147
+ "POST",
148
+ `/tokens/revoke-session/?session_id=${encodeURIComponent(sessionId)}`
149
+ );
150
+ }
151
+ /**
152
+ * Revoke all active sessions (emergency use)
153
+ *
154
+ * Invalidates all token sessions for the authenticated user.
155
+ * Use for security cleanup or when sessions are stuck.
156
+ *
157
+ * @returns Revocation result with count of revoked sessions
158
+ * @example
159
+ * ```typescript
160
+ * const result = await client.tokens.revokeAllSessions();
161
+ * console.log(`Revoked ${result.sessions_revoked} sessions`);
162
+ * ```
163
+ */
164
+ async revokeAllSessions() {
165
+ return this.client.request("POST", "/tokens/revoke-all-sessions/");
166
+ }
167
+ /**
168
+ * Get public encryption info (no auth required)
169
+ *
170
+ * Returns algorithm details and example code for client-side decryption.
171
+ * Public endpoint - can be called without authentication.
172
+ *
173
+ * @returns Encryption algorithm and implementation details
174
+ * @example
175
+ * ```typescript
176
+ * const info = await client.tokens.getEncryptionInfo();
177
+ * console.log('Algorithm:', info.algorithm);
178
+ * console.log('Key derivation:', info.key_derivation);
179
+ * console.log(info.javascript_example);
180
+ * ```
181
+ */
182
+ async getEncryptionInfo() {
183
+ return this.client.request("GET", "/tokens/encryption-info/");
184
+ }
185
+ };
186
+
41
187
  // src/client.ts
42
188
  var Resource = class {
43
189
  constructor(client, elementType) {
@@ -60,6 +206,24 @@ var Resource = class {
60
206
  return this.client.request("DELETE", `/${this.elementType}/${id}/`);
61
207
  }
62
208
  };
209
+ var WorldResource = class {
210
+ constructor(client) {
211
+ this.client = client;
212
+ }
213
+ /**
214
+ * Get the world associated with the current API key
215
+ * Returns the world directly (not wrapped in pagination)
216
+ */
217
+ async get() {
218
+ return this.client.request("GET", "/world/");
219
+ }
220
+ /**
221
+ * Update the current world
222
+ */
223
+ async update(data) {
224
+ return this.client.request("PATCH", "/world/", { body: data });
225
+ }
226
+ };
63
227
  var OnlyWorldsClient = class {
64
228
  constructor(config) {
65
229
  this.baseUrl = config.baseUrl || "https://www.onlyworlds.com/api/worldapi";
@@ -68,7 +232,8 @@ var OnlyWorldsClient = class {
68
232
  "API-Key": config.apiKey,
69
233
  "API-Pin": config.apiPin
70
234
  };
71
- this.worlds = new Resource(this, "world");
235
+ this.worlds = new WorldResource(this);
236
+ this.tokens = new TokenResource(this);
72
237
  this.abilities = new Resource(this, "ability");
73
238
  this.characters = new Resource(this, "character");
74
239
  this.collectives = new Resource(this, "collective");
@@ -133,13 +298,6 @@ var OnlyWorldsClient = class {
133
298
  }
134
299
  return response.json();
135
300
  }
136
- /**
137
- * Get all worlds accessible with current credentials
138
- * @deprecated Use client.worlds.list() instead for consistent API
139
- */
140
- async getWorlds() {
141
- return this.request("GET", "/world/");
142
- }
143
301
  /**
144
302
  * Helper to convert nested objects to _id/_ids format
145
303
  */
@@ -974,6 +1132,17 @@ function createElementIds(ids) {
974
1132
  function createAnyElementId(id) {
975
1133
  return id;
976
1134
  }
1135
+
1136
+ // src/token-types.ts
1137
+ var GameTier = /* @__PURE__ */ ((GameTier2) => {
1138
+ GameTier2["FREE"] = "free";
1139
+ GameTier2["SILVER"] = "silver";
1140
+ GameTier2["GOLD"] = "gold";
1141
+ GameTier2["PLATINUM"] = "platinum";
1142
+ GameTier2["DIAMOND"] = "diamond";
1143
+ GameTier2["DELUXE"] = "deluxe";
1144
+ return GameTier2;
1145
+ })(GameTier || {});
977
1146
  // Annotate the CommonJS export names for ESM import in node:
978
1147
  0 && (module.exports = {
979
1148
  ELEMENT_ICONS,
@@ -982,6 +1151,7 @@ function createAnyElementId(id) {
982
1151
  ELEMENT_UNICODE_ICONS,
983
1152
  ElementType,
984
1153
  FIELD_SCHEMA,
1154
+ GameTier,
985
1155
  ONLYWORLDS_VERSION,
986
1156
  OnlyWorldsClient,
987
1157
  createAnyElementId,
package/dist/index.mjs CHANGED
@@ -1,3 +1,148 @@
1
+ // src/token-resource.ts
2
+ var TokenResource = class {
3
+ constructor(client) {
4
+ this.client = client;
5
+ }
6
+ /**
7
+ * Get current token status for authenticated user
8
+ *
9
+ * Returns daily token allowance, usage, and availability.
10
+ * Matches base-tool's checkStatus() pattern.
11
+ *
12
+ * @returns Current token status
13
+ * @example
14
+ * ```typescript
15
+ * const status = await client.tokens.getStatus();
16
+ * console.log(`Available: ${status.tokens_available_today}/${status.token_rating}`);
17
+ * console.log(`Used today: ${status.tokens_used_today}`);
18
+ * console.log(`Active sessions: ${status.sessions_active}`);
19
+ * ```
20
+ */
21
+ async getStatus() {
22
+ return this.client.request("GET", "/tokens/status/");
23
+ }
24
+ /**
25
+ * Consume tokens for service usage
26
+ *
27
+ * Reports token consumption to track daily usage. Allows consumption even if
28
+ * exceeds available tokens (tracks as debt), but warns via error field.
29
+ * Matches base-tool's reportUsage() pattern.
30
+ *
31
+ * @param params - Token consumption parameters
32
+ * @returns Consumption result with updated balance
33
+ * @example
34
+ * ```typescript
35
+ * const result = await client.tokens.consume({
36
+ * amount: 500,
37
+ * service: 'worldbuilding_tool',
38
+ * metadata: {
39
+ * feature: 'character_generation',
40
+ * model: 'gpt-4',
41
+ * prompt_tokens: 300,
42
+ * completion_tokens: 200
43
+ * }
44
+ * });
45
+ *
46
+ * if (result.error) {
47
+ * console.warn('Token warning:', result.error);
48
+ * }
49
+ * console.log(`${result.tokens_remaining} tokens remaining`);
50
+ * ```
51
+ */
52
+ async consume(params) {
53
+ return this.client.request("POST", "/tokens/consume/", {
54
+ body: {
55
+ amount: params.amount,
56
+ service: params.service || "sdk_client",
57
+ session_id: params.sessionId ?? null,
58
+ metadata: params.metadata ?? null
59
+ }
60
+ });
61
+ }
62
+ /**
63
+ * Get encrypted OpenAI API key (advanced use case)
64
+ *
65
+ * Requires minimum 100 tokens available. Creates a 1-hour session for tracking.
66
+ * Returns encrypted key that must be decrypted client-side using Fernet.
67
+ *
68
+ * See base-tool/src/llm/token-service.ts:99-155 for full implementation example
69
+ * including client-side decryption with the 'fernet' npm package.
70
+ *
71
+ * @returns Encrypted access key and session info
72
+ * @throws Error if insufficient tokens (< 100)
73
+ * @example
74
+ * ```typescript
75
+ * // Get encrypted key
76
+ * const access = await client.tokens.getAccessKey();
77
+ *
78
+ * // Decrypt using fernet library (see base-tool for full example)
79
+ * // 1. Derive key from world ID using SHA-256
80
+ * // 2. Use 'fernet' npm package to decrypt
81
+ * // 3. Use decrypted OpenAI key for direct API calls
82
+ * // 4. Report usage with access.session_id
83
+ *
84
+ * console.log('Session:', access.session_id);
85
+ * console.log('Expires:', access.expires_at);
86
+ * ```
87
+ */
88
+ async getAccessKey() {
89
+ return this.client.request("GET", "/tokens/access-key/");
90
+ }
91
+ /**
92
+ * Revoke a specific token session
93
+ *
94
+ * Invalidates the session ID obtained from getAccessKey().
95
+ * Use when cleaning up or on logout.
96
+ *
97
+ * @param sessionId - Session ID to revoke
98
+ * @returns Revocation result
99
+ * @example
100
+ * ```typescript
101
+ * await client.tokens.revokeSession('session-id-here');
102
+ * ```
103
+ */
104
+ async revokeSession(sessionId) {
105
+ return this.client.request(
106
+ "POST",
107
+ `/tokens/revoke-session/?session_id=${encodeURIComponent(sessionId)}`
108
+ );
109
+ }
110
+ /**
111
+ * Revoke all active sessions (emergency use)
112
+ *
113
+ * Invalidates all token sessions for the authenticated user.
114
+ * Use for security cleanup or when sessions are stuck.
115
+ *
116
+ * @returns Revocation result with count of revoked sessions
117
+ * @example
118
+ * ```typescript
119
+ * const result = await client.tokens.revokeAllSessions();
120
+ * console.log(`Revoked ${result.sessions_revoked} sessions`);
121
+ * ```
122
+ */
123
+ async revokeAllSessions() {
124
+ return this.client.request("POST", "/tokens/revoke-all-sessions/");
125
+ }
126
+ /**
127
+ * Get public encryption info (no auth required)
128
+ *
129
+ * Returns algorithm details and example code for client-side decryption.
130
+ * Public endpoint - can be called without authentication.
131
+ *
132
+ * @returns Encryption algorithm and implementation details
133
+ * @example
134
+ * ```typescript
135
+ * const info = await client.tokens.getEncryptionInfo();
136
+ * console.log('Algorithm:', info.algorithm);
137
+ * console.log('Key derivation:', info.key_derivation);
138
+ * console.log(info.javascript_example);
139
+ * ```
140
+ */
141
+ async getEncryptionInfo() {
142
+ return this.client.request("GET", "/tokens/encryption-info/");
143
+ }
144
+ };
145
+
1
146
  // src/client.ts
2
147
  var Resource = class {
3
148
  constructor(client, elementType) {
@@ -20,6 +165,24 @@ var Resource = class {
20
165
  return this.client.request("DELETE", `/${this.elementType}/${id}/`);
21
166
  }
22
167
  };
168
+ var WorldResource = class {
169
+ constructor(client) {
170
+ this.client = client;
171
+ }
172
+ /**
173
+ * Get the world associated with the current API key
174
+ * Returns the world directly (not wrapped in pagination)
175
+ */
176
+ async get() {
177
+ return this.client.request("GET", "/world/");
178
+ }
179
+ /**
180
+ * Update the current world
181
+ */
182
+ async update(data) {
183
+ return this.client.request("PATCH", "/world/", { body: data });
184
+ }
185
+ };
23
186
  var OnlyWorldsClient = class {
24
187
  constructor(config) {
25
188
  this.baseUrl = config.baseUrl || "https://www.onlyworlds.com/api/worldapi";
@@ -28,7 +191,8 @@ var OnlyWorldsClient = class {
28
191
  "API-Key": config.apiKey,
29
192
  "API-Pin": config.apiPin
30
193
  };
31
- this.worlds = new Resource(this, "world");
194
+ this.worlds = new WorldResource(this);
195
+ this.tokens = new TokenResource(this);
32
196
  this.abilities = new Resource(this, "ability");
33
197
  this.characters = new Resource(this, "character");
34
198
  this.collectives = new Resource(this, "collective");
@@ -93,13 +257,6 @@ var OnlyWorldsClient = class {
93
257
  }
94
258
  return response.json();
95
259
  }
96
- /**
97
- * Get all worlds accessible with current credentials
98
- * @deprecated Use client.worlds.list() instead for consistent API
99
- */
100
- async getWorlds() {
101
- return this.request("GET", "/world/");
102
- }
103
260
  /**
104
261
  * Helper to convert nested objects to _id/_ids format
105
262
  */
@@ -934,6 +1091,17 @@ function createElementIds(ids) {
934
1091
  function createAnyElementId(id) {
935
1092
  return id;
936
1093
  }
1094
+
1095
+ // src/token-types.ts
1096
+ var GameTier = /* @__PURE__ */ ((GameTier2) => {
1097
+ GameTier2["FREE"] = "free";
1098
+ GameTier2["SILVER"] = "silver";
1099
+ GameTier2["GOLD"] = "gold";
1100
+ GameTier2["PLATINUM"] = "platinum";
1101
+ GameTier2["DIAMOND"] = "diamond";
1102
+ GameTier2["DELUXE"] = "deluxe";
1103
+ return GameTier2;
1104
+ })(GameTier || {});
937
1105
  export {
938
1106
  ELEMENT_ICONS,
939
1107
  ELEMENT_LABELS,
@@ -941,6 +1109,7 @@ export {
941
1109
  ELEMENT_UNICODE_ICONS,
942
1110
  ElementType,
943
1111
  FIELD_SCHEMA,
1112
+ GameTier,
944
1113
  ONLYWORLDS_VERSION,
945
1114
  OnlyWorldsClient,
946
1115
  createAnyElementId,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onlyworlds/sdk",
3
- "version": "2.0.1",
3
+ "version": "2.1.0",
4
4
  "description": "TypeScript SDK for the OnlyWorlds API - build world-building applications with type safety",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -28,7 +28,11 @@
28
28
  "license": "MIT",
29
29
  "repository": {
30
30
  "type": "git",
31
- "url": "git+https://github.com/onlyworlds/sdk.git"
31
+ "url": "git+https://github.com/OnlyWorlds/sdk.git"
32
+ },
33
+ "homepage": "https://onlyworlds.github.io/",
34
+ "bugs": {
35
+ "url": "https://github.com/OnlyWorlds/sdk/issues"
32
36
  },
33
37
  "devDependencies": {
34
38
  "tsup": "^8.0.1",