@arinova-ai/spaces-sdk 0.1.2 → 0.1.4

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.d.ts CHANGED
@@ -30,11 +30,33 @@ export interface TokenResponse {
30
30
  image: string | null;
31
31
  };
32
32
  }
33
+ export interface BalanceResponse {
34
+ balance: number;
35
+ }
36
+ export interface PurchaseResponse {
37
+ transactionId: string;
38
+ newBalance: number;
39
+ }
40
+ export interface TransactionRecord {
41
+ id: string;
42
+ type: string;
43
+ amount: number;
44
+ description: string | null;
45
+ createdAt: string;
46
+ }
47
+ export interface TransactionsResponse {
48
+ transactions: TransactionRecord[];
49
+ total: number;
50
+ limit: number;
51
+ offset: number;
52
+ }
33
53
  export declare class Arinova {
34
54
  private appId;
35
55
  private endpoint;
36
56
  private redirectUri;
37
57
  private scope;
58
+ /** The access token from the most recent login/handleCallback. */
59
+ accessToken: string | null;
38
60
  constructor(config: ArinovaConfig);
39
61
  /**
40
62
  * Start the OAuth PKCE login flow.
@@ -42,11 +64,39 @@ export declare class Arinova {
42
64
  * Returns the token response on success.
43
65
  */
44
66
  login(): Promise<TokenResponse>;
67
+ /**
68
+ * Connect to Arinova — auto-detects iframe vs standalone.
69
+ * In iframe: listens for postMessage from parent (Arinova Chat).
70
+ * Standalone: falls back to PKCE login popup.
71
+ */
72
+ connect(options?: {
73
+ timeout?: number;
74
+ }): Promise<{
75
+ user: TokenResponse["user"];
76
+ accessToken: string;
77
+ agents: {
78
+ id: string;
79
+ name: string;
80
+ description: string | null;
81
+ avatarUrl: string | null;
82
+ }[];
83
+ }>;
45
84
  /**
46
85
  * Handle the OAuth callback (call this on your redirect_uri page).
47
86
  * Reads code and state from URL, exchanges for token.
48
87
  */
49
88
  handleCallback(): Promise<TokenResponse>;
89
+ private getToken;
90
+ private apiFetch;
91
+ /** Get the current user's coin balance. */
92
+ balance(): Promise<BalanceResponse>;
93
+ /**
94
+ * Purchase / charge coins from the user's balance.
95
+ * Requires the "economy" scope.
96
+ */
97
+ purchase(productId: string, amount: number, description?: string): Promise<PurchaseResponse>;
98
+ /** Get the user's transaction history. */
99
+ transactions(limit?: number, offset?: number): Promise<TransactionsResponse>;
50
100
  private exchangeCode;
51
101
  }
52
102
  export default Arinova;
package/dist/index.js CHANGED
@@ -10,6 +10,8 @@
10
10
  */
11
11
  export class Arinova {
12
12
  constructor(config) {
13
+ /** The access token from the most recent login/handleCallback. */
14
+ this.accessToken = null;
13
15
  this.appId = config.appId;
14
16
  this.endpoint = (config.endpoint ?? "https://chat.arinova.ai").replace(/\/+$/, "");
15
17
  this.redirectUri =
@@ -66,7 +68,10 @@ export class Arinova {
66
68
  reject(new Error(url.searchParams.get("error_description") ?? "No code received"));
67
69
  return;
68
70
  }
69
- this.exchangeCode(code, verifier).then(resolve).catch(reject);
71
+ this.exchangeCode(code, verifier).then((token) => {
72
+ this.accessToken = token.access_token;
73
+ resolve(token);
74
+ }).catch(reject);
70
75
  }
71
76
  }
72
77
  catch {
@@ -84,6 +89,42 @@ export class Arinova {
84
89
  }, 300000);
85
90
  });
86
91
  }
92
+ /**
93
+ * Connect to Arinova — auto-detects iframe vs standalone.
94
+ * In iframe: listens for postMessage from parent (Arinova Chat).
95
+ * Standalone: falls back to PKCE login popup.
96
+ */
97
+ async connect(options) {
98
+ const timeout = options?.timeout ?? 5000;
99
+ const inIframe = typeof window !== "undefined" && window.self !== window.top;
100
+ if (!inIframe) {
101
+ const token = await this.login();
102
+ return { user: token.user, accessToken: token.access_token, agents: [] };
103
+ }
104
+ return new Promise((resolve, reject) => {
105
+ let settled = false;
106
+ const timer = setTimeout(() => {
107
+ if (!settled) {
108
+ settled = true;
109
+ window.removeEventListener("message", handler);
110
+ reject(new Error("connect timeout"));
111
+ }
112
+ }, timeout);
113
+ const handler = (event) => {
114
+ if (event.data?.type !== "arinova:auth")
115
+ return;
116
+ if (settled)
117
+ return;
118
+ settled = true;
119
+ clearTimeout(timer);
120
+ window.removeEventListener("message", handler);
121
+ const payload = event.data.payload;
122
+ this.accessToken = payload.accessToken;
123
+ resolve(payload);
124
+ };
125
+ window.addEventListener("message", handler);
126
+ });
127
+ }
87
128
  /**
88
129
  * Handle the OAuth callback (call this on your redirect_uri page).
89
130
  * Reads code and state from URL, exchanges for token.
@@ -105,7 +146,58 @@ export class Arinova {
105
146
  if (!verifier) {
106
147
  throw new Error("No PKCE verifier found — did you start login()?");
107
148
  }
108
- return this.exchangeCode(code, verifier);
149
+ const token = await this.exchangeCode(code, verifier);
150
+ this.accessToken = token.access_token;
151
+ return token;
152
+ }
153
+ // ── Economy Methods ───────────────────────────────────────────
154
+ getToken() {
155
+ if (!this.accessToken) {
156
+ throw new Error("Not logged in — call login() or handleCallback() first");
157
+ }
158
+ return this.accessToken;
159
+ }
160
+ async apiFetch(path, init) {
161
+ const token = this.getToken();
162
+ const res = await fetch(`${this.endpoint}${path}`, {
163
+ ...init,
164
+ headers: {
165
+ "Content-Type": "application/json",
166
+ Authorization: `Bearer ${token}`,
167
+ ...init?.headers,
168
+ },
169
+ });
170
+ if (!res.ok) {
171
+ const body = await res.json().catch(() => ({}));
172
+ throw new Error(body.error_description ??
173
+ body.error ??
174
+ `API error (${res.status})`);
175
+ }
176
+ return res.json();
177
+ }
178
+ /** Get the current user's coin balance. */
179
+ async balance() {
180
+ return this.apiFetch("/api/v1/economy/balance");
181
+ }
182
+ /**
183
+ * Purchase / charge coins from the user's balance.
184
+ * Requires the "economy" scope.
185
+ */
186
+ async purchase(productId, amount, description) {
187
+ return this.apiFetch("/api/v1/economy/purchase", {
188
+ method: "POST",
189
+ body: JSON.stringify({ productId, amount, description }),
190
+ });
191
+ }
192
+ /** Get the user's transaction history. */
193
+ async transactions(limit, offset) {
194
+ const params = new URLSearchParams();
195
+ if (limit != null)
196
+ params.set("limit", String(limit));
197
+ if (offset != null)
198
+ params.set("offset", String(offset));
199
+ const qs = params.toString();
200
+ return this.apiFetch(`/api/v1/economy/transactions${qs ? `?${qs}` : ""}`);
109
201
  }
110
202
  async exchangeCode(code, codeVerifier) {
111
203
  const res = await fetch(`${this.endpoint}/oauth/token`, {
package/dist/types.d.ts CHANGED
@@ -68,6 +68,28 @@ export interface AwardResponse {
68
68
  export interface BalanceResponse {
69
69
  balance: number;
70
70
  }
71
+ export interface PurchaseOptions {
72
+ productId: string;
73
+ amount: number;
74
+ description?: string;
75
+ }
76
+ export interface PurchaseResponse {
77
+ transactionId: string;
78
+ newBalance: number;
79
+ }
80
+ export interface TransactionRecord {
81
+ id: string;
82
+ type: string;
83
+ amount: number;
84
+ description: string | null;
85
+ createdAt: string;
86
+ }
87
+ export interface TransactionsResponse {
88
+ transactions: TransactionRecord[];
89
+ total: number;
90
+ limit: number;
91
+ offset: number;
92
+ }
71
93
  export interface SSEChunkEvent {
72
94
  type: "chunk";
73
95
  content: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arinova-ai/spaces-sdk",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "Arinova Spaces SDK — OAuth PKCE login for third-party apps",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",