@reopt-ai/brandapp-sdk 3.3.0 → 3.4.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.
Files changed (102) hide show
  1. package/CHANGELOG.md +67 -0
  2. package/README.md +10 -1
  3. package/dist/abort--nOBFN4M.js +50 -0
  4. package/dist/ai/index.d.ts +2 -314
  5. package/dist/ai/index.js +3 -8
  6. package/dist/ai-provider/index.d.ts +79 -163
  7. package/dist/ai-provider/index.js +752 -685
  8. package/dist/auth/index.d.ts +27 -27
  9. package/dist/auth/index.js +41 -34
  10. package/dist/better-auth/client.d.ts +6 -5
  11. package/dist/better-auth/client.js +28 -5
  12. package/dist/better-auth/index.d.ts +19 -17
  13. package/dist/better-auth/index.js +180 -144
  14. package/dist/client-B1q5P8rv.js +83 -0
  15. package/dist/client-B9QHVhdM.js +77 -0
  16. package/dist/client-BGGd_cpV.d.ts +103 -0
  17. package/dist/client-BJcy1V1a.js +140 -0
  18. package/dist/client-Beivhucf.js +55 -0
  19. package/dist/client-BruP5_nU.d.ts +147 -0
  20. package/dist/client-D-yNxNfk.js +28 -0
  21. package/dist/client-D0eBxh8j.d.ts +284 -0
  22. package/dist/client-D3RHHUUW.d.ts +207 -0
  23. package/dist/client-D6gs61w8.d.ts +53 -0
  24. package/dist/client-DBnK7ooQ.js +86 -0
  25. package/dist/client-DDw5yz_h.d.ts +53 -0
  26. package/dist/client-DPmoKuR9.js +305 -0
  27. package/dist/client-DsdakibF.js +503 -0
  28. package/dist/client-Dxg8_DIH.d.ts +71 -0
  29. package/dist/client-EbhUIhHq.js +53 -0
  30. package/dist/client-lk6wBu_0.d.ts +48 -0
  31. package/dist/client-p49ol9pg.d.ts +124 -0
  32. package/dist/cms/index.d.ts +52 -108
  33. package/dist/cms/index.js +103 -86
  34. package/dist/config-CRytOPPl.d.ts +394 -0
  35. package/dist/config-Cw0a3-C8.js +487 -0
  36. package/dist/dev/index.d.ts +2 -528
  37. package/dist/dev/index.js +2731 -2967
  38. package/dist/eav/coerce.d.ts +3 -1
  39. package/dist/eav/coerce.js +72 -46
  40. package/dist/eav/index.d.ts +72 -100
  41. package/dist/eav/index.js +158 -126
  42. package/dist/eav/migrate.d.ts +112 -109
  43. package/dist/eav/migrate.js +230 -196
  44. package/dist/eav/schema.d.ts +111 -150
  45. package/dist/eav/schema.js +624 -603
  46. package/dist/encoding-Bwfm3iaE.js +21 -0
  47. package/dist/{errors-BjDFIyA6.d.ts → errors-CRCcErNC.d.ts} +55 -53
  48. package/dist/feedback/index.d.ts +2 -132
  49. package/dist/feedback/index.js +3 -6
  50. package/dist/files/index.d.ts +14 -14
  51. package/dist/files/index.js +41 -25
  52. package/dist/http-client-QKUvvirs.js +386 -0
  53. package/dist/index-DrVaKfhs.d.ts +456 -0
  54. package/dist/index-Z6OGcurJ.d.ts +169 -0
  55. package/dist/index.d.ts +6 -230
  56. package/dist/index.js +119 -71
  57. package/dist/logout/index.d.ts +56 -53
  58. package/dist/logout/index.js +232 -289
  59. package/dist/plans/index.d.ts +2 -236
  60. package/dist/plans/index.js +3 -6
  61. package/dist/push/index.d.ts +2 -79
  62. package/dist/push/index.js +3 -6
  63. package/dist/query-BvbCiGWb.js +35 -0
  64. package/dist/react/hooks.d.ts +97 -99
  65. package/dist/react/hooks.js +742 -404
  66. package/dist/sse-parser-FMjxM7sY.js +86 -0
  67. package/dist/terms/index.d.ts +3 -79
  68. package/dist/terms/index.js +3 -6
  69. package/dist/testing/index.d.ts +52 -104
  70. package/dist/testing/index.js +94 -72
  71. package/dist/types-B-SMkH6s.d.ts +26 -0
  72. package/dist/types-BXDcoJHG.d.ts +131 -0
  73. package/dist/types-BrghN2rJ.d.ts +105 -0
  74. package/dist/types-DjDRszNj.js +11 -0
  75. package/dist/webhooks/index.d.ts +77 -50
  76. package/dist/webhooks/index.js +150 -127
  77. package/docs/api-reference.md +87 -6
  78. package/package.json +6 -5
  79. package/dist/chunk-2LSCQHAU.js +0 -21
  80. package/dist/chunk-4U4ZPPL4.js +0 -599
  81. package/dist/chunk-55KHY5GH.js +0 -96
  82. package/dist/chunk-C5VTRZWX.js +0 -458
  83. package/dist/chunk-F7BXBTZI.js +0 -54
  84. package/dist/chunk-FJUCWXX3.js +0 -350
  85. package/dist/chunk-H33OTQEN.js +0 -112
  86. package/dist/chunk-L4DHB36G.js +0 -75
  87. package/dist/chunk-OG7R3UAR.js +0 -105
  88. package/dist/chunk-PF24XU54.js +0 -32
  89. package/dist/chunk-QN3QD67K.js +0 -49
  90. package/dist/chunk-REIKXSMY.js +0 -7
  91. package/dist/chunk-UGBBXCZE.js +0 -157
  92. package/dist/chunk-UVT2E6KD.js +0 -81
  93. package/dist/chunk-YPLT3RFL.js +0 -63
  94. package/dist/chunk-YTKSAWV2.js +0 -463
  95. package/dist/client-BkcO-0C2.d.ts +0 -216
  96. package/dist/client-CN5_tNOC.d.ts +0 -147
  97. package/dist/client-DttyeM_h.d.ts +0 -102
  98. package/dist/config-CtxyJsDR.d.ts +0 -397
  99. package/dist/typed-client-BxOVIiMU.d.ts +0 -40
  100. package/dist/types-6E3ZLL9X.d.ts +0 -24
  101. package/dist/types-DskYLVb8.d.ts +0 -104
  102. package/dist/types-ckmDDW3N.d.ts +0 -129
@@ -0,0 +1,305 @@
1
+ import { A as mapHttpStatusToError, n as SDK_VERSION, o as validateConfig, x as ReoptSDKError } from "./config-Cw0a3-C8.js";
2
+ import { t as createManagedAbort } from "./abort--nOBFN4M.js";
3
+ import { t as BaseHttpClient } from "./http-client-QKUvvirs.js";
4
+ import { n as parseSSEStream } from "./sse-parser-FMjxM7sY.js";
5
+
6
+ //#region src/ai/client.ts
7
+ var ReoptAiClient = class {
8
+ http;
9
+ /**
10
+ * Idle (inactivity) timeout for streaming, in ms — the maximum gap allowed
11
+ * *between* SSE chunks, not a deadline on the whole generation. A long but
12
+ * healthy generation keeps resetting this; only a genuine stall aborts.
13
+ */
14
+ streamIdleTimeout;
15
+ constructor(config) {
16
+ validateConfig(config);
17
+ const pathPrefix = `/api/v1/brandapp/${config.brandappId}/ai`;
18
+ this.http = new BaseHttpClient(config, pathPrefix);
19
+ this.streamIdleTimeout = config.timeout ?? 6e4;
20
+ }
21
+ /**
22
+ * AI 텍스트 생성 (non-streaming)
23
+ *
24
+ * @example
25
+ * ```ts
26
+ * const result = await sdk.ai.complete({
27
+ * messages: [{ role: "user", content: "Hello" }],
28
+ * model: "anthropic/claude-haiku-4.5",
29
+ * maxTokens: 1024,
30
+ * });
31
+ * console.log(result.message.content);
32
+ * ```
33
+ */
34
+ async complete(options) {
35
+ return (await this.http.fetch("POST", "/chat/completions", {
36
+ model: options.model,
37
+ agentId: options.agentId,
38
+ messages: options.messages,
39
+ maxTokens: options.maxTokens,
40
+ temperature: options.temperature,
41
+ stream: false,
42
+ customerId: options.customerId
43
+ })).data;
44
+ }
45
+ /**
46
+ * AI 텍스트 생성 (SSE 스트리밍)
47
+ *
48
+ * async generator로 텍스트 청크를 반환합니다.
49
+ * 사용량 메타데이터가 필요하면 `streamWithMetadata()`를 사용하세요.
50
+ *
51
+ * @example
52
+ * ```ts
53
+ * for await (const chunk of sdk.ai.stream({
54
+ * messages: [{ role: "user", content: "Hello" }],
55
+ * })) {
56
+ * process.stdout.write(chunk);
57
+ * }
58
+ * ```
59
+ */
60
+ async *stream(options) {
61
+ const { response, managed, requestId } = await this.fetchSSE(options);
62
+ if (!response.body) {
63
+ managed.cleanup();
64
+ throw new ReoptSDKError("No response body", 0, "STREAM_ERROR");
65
+ }
66
+ try {
67
+ for await (const { event, data } of parseSSEStream(response.body)) {
68
+ managed.kick();
69
+ if (event === "text-delta") yield data;
70
+ else if (event === "error") {
71
+ const ctx = { requestId };
72
+ try {
73
+ const err = JSON.parse(data);
74
+ if (typeof err.status === "number") throw mapHttpStatusToError(err.status, err.code, err.message, ctx);
75
+ throw new ReoptSDKError(err.message ?? "Stream error", 0, err.code ?? "STREAM_ERROR", ctx);
76
+ } catch (e) {
77
+ if (e instanceof ReoptSDKError) throw e;
78
+ throw new ReoptSDKError(data, 0, "STREAM_ERROR", ctx);
79
+ }
80
+ }
81
+ }
82
+ } catch (e) {
83
+ throw mapStreamError(e, managed);
84
+ } finally {
85
+ managed.cleanup();
86
+ }
87
+ }
88
+ /**
89
+ * AI 텍스트 생성 (SSE 스트리밍 + 메타데이터)
90
+ *
91
+ * 텍스트 청크와 함께 최종 사용량/크레딧 메타데이터를 반환합니다.
92
+ *
93
+ * @example
94
+ * ```ts
95
+ * const { textStream, metadata } = sdk.ai.streamWithMetadata({
96
+ * messages: [{ role: "user", content: "Hello" }],
97
+ * });
98
+ *
99
+ * let text = "";
100
+ * for await (const chunk of textStream) {
101
+ * text += chunk;
102
+ * }
103
+ *
104
+ * const meta = await metadata;
105
+ * console.log(`사용 크레딧: ${meta?.usage.creditCost}`);
106
+ * ```
107
+ */
108
+ streamWithMetadata(options) {
109
+ let resolveMeta;
110
+ const metadata = new Promise((r) => {
111
+ resolveMeta = r;
112
+ });
113
+ let settled = false;
114
+ const settle = (v) => {
115
+ if (settled) return;
116
+ settled = true;
117
+ resolveMeta(v);
118
+ };
119
+ const self = this;
120
+ async function* gen() {
121
+ const { response, managed, requestId } = await self.fetchSSE(options);
122
+ if (!response.body) {
123
+ managed.cleanup();
124
+ settle(null);
125
+ throw new ReoptSDKError("No response body", 0, "STREAM_ERROR");
126
+ }
127
+ try {
128
+ for await (const { event, data } of parseSSEStream(response.body)) {
129
+ managed.kick();
130
+ if (event === "text-delta") yield data;
131
+ else if (event === "done") try {
132
+ settle(JSON.parse(data));
133
+ } catch {
134
+ settle(null);
135
+ }
136
+ else if (event === "error") {
137
+ const ctx = { requestId };
138
+ try {
139
+ const err = JSON.parse(data);
140
+ if (typeof err.status === "number") throw mapHttpStatusToError(err.status, err.code, err.message, ctx);
141
+ throw new ReoptSDKError(err.message ?? "Stream error", 0, err.code ?? "STREAM_ERROR", ctx);
142
+ } catch (e) {
143
+ if (e instanceof ReoptSDKError) throw e;
144
+ throw new ReoptSDKError(data, 0, "STREAM_ERROR", ctx);
145
+ }
146
+ }
147
+ }
148
+ } catch (e) {
149
+ throw mapStreamError(e, managed);
150
+ } finally {
151
+ settle(null);
152
+ managed.cleanup();
153
+ }
154
+ }
155
+ return {
156
+ textStream: gen(),
157
+ metadata
158
+ };
159
+ }
160
+ /**
161
+ * 이미지 생성
162
+ *
163
+ * BrandApp의 AI 크레딧으로 이미지를 생성합니다.
164
+ * 생성된 이미지는 자동으로 Vercel Blob에 저장되고 URL이 반환됩니다.
165
+ *
166
+ * @example
167
+ * ```ts
168
+ * const image = await sdk.ai.generateImage({
169
+ * prompt: "브랜드 배너 이미지",
170
+ * style: "minimalist",
171
+ * aspectRatio: "16:9",
172
+ * });
173
+ * console.log(image.url); // https://...blob.vercel-storage.com/...
174
+ * console.log(image.size); // 12345 (bytes)
175
+ * ```
176
+ */
177
+ async generateImage(options) {
178
+ return (await this.http.fetch("POST", "/images/generate", {
179
+ prompt: options.prompt,
180
+ negativePrompt: options.negativePrompt,
181
+ style: options.style,
182
+ aspectRatio: options.aspectRatio ?? "1:1",
183
+ model: options.model,
184
+ customerId: options.customerId
185
+ })).data;
186
+ }
187
+ /**
188
+ * 크레딧 잔액 조회
189
+ *
190
+ * @example
191
+ * ```ts
192
+ * const credits = await sdk.ai.credits();
193
+ * console.log(`${credits.balance} / ${credits.limit} (${credits.status})`);
194
+ * ```
195
+ */
196
+ async credits() {
197
+ return (await this.http.fetch("GET", "/credits")).data;
198
+ }
199
+ /**
200
+ * 사용 가능 모델 목록 조회
201
+ *
202
+ * @example
203
+ * ```ts
204
+ * const models = await sdk.ai.models();
205
+ * models.forEach(m => console.log(`${m.id} (${m.tierLabel})`));
206
+ * ```
207
+ */
208
+ async models() {
209
+ return (await this.http.fetch("GET", "/models")).data;
210
+ }
211
+ /**
212
+ * 사용 가능한 커스텀 에이전트 목록 조회
213
+ *
214
+ * @example
215
+ * ```ts
216
+ * const agents = await sdk.ai.agents();
217
+ * for (const agent of agents) {
218
+ * console.log(`${agent.name}: ${agent.description}`);
219
+ * }
220
+ * ```
221
+ */
222
+ async agents() {
223
+ return (await this.http.fetch("GET", "/agents")).data;
224
+ }
225
+ async fetchSSE(options) {
226
+ const url = `${this.http.requestBaseUrl}/chat/completions`;
227
+ const requestId = globalThis.crypto?.randomUUID?.() ?? `sdk-${"3.4.0"}-${url.length}`;
228
+ const ctx = {
229
+ method: "POST",
230
+ url,
231
+ requestId
232
+ };
233
+ const maxConnectAttempts = 2;
234
+ let lastConnectError;
235
+ for (let attempt = 0; attempt < maxConnectAttempts; attempt++) {
236
+ const managed = createManagedAbort({
237
+ timeoutMs: this.streamIdleTimeout,
238
+ external: options.signal,
239
+ idle: true
240
+ });
241
+ try {
242
+ const response = await globalThis.fetch(url, {
243
+ method: "POST",
244
+ headers: {
245
+ Authorization: this.http.authorizationHeader,
246
+ "Content-Type": "application/json",
247
+ Accept: "text/event-stream",
248
+ "X-SDK-Version": `brandapp-sdk/${SDK_VERSION}`,
249
+ "X-Request-Id": requestId,
250
+ "Idempotency-Key": requestId
251
+ },
252
+ body: JSON.stringify({
253
+ model: options.model,
254
+ agentId: options.agentId,
255
+ messages: options.messages,
256
+ maxTokens: options.maxTokens,
257
+ temperature: options.temperature,
258
+ stream: true,
259
+ customerId: options.customerId
260
+ }),
261
+ signal: managed.signal
262
+ });
263
+ if (!response.ok) {
264
+ const err = (await response.json().catch(() => ({})))?.error ?? {};
265
+ managed.cleanup();
266
+ throw mapHttpStatusToError(response.status, err.code, err.message ?? `HTTP ${response.status}`, ctx);
267
+ }
268
+ return {
269
+ response,
270
+ managed,
271
+ requestId
272
+ };
273
+ } catch (e) {
274
+ managed.cleanup();
275
+ if (e instanceof ReoptSDKError) throw e;
276
+ if (managed.timedOut || options.signal?.aborted) throw mapStreamError(e, managed);
277
+ lastConnectError = e;
278
+ if (attempt < maxConnectAttempts - 1) continue;
279
+ throw mapStreamError(e, managed);
280
+ }
281
+ }
282
+ throw mapStreamError(lastConnectError, void 0);
283
+ }
284
+ };
285
+ /**
286
+ * Map a raw streaming error to a `ReoptSDKError`. An abort is disambiguated via
287
+ * `managed.timedOut`: our idle timeout → `TIMEOUT` (408); a caller-initiated
288
+ * cancel (external signal) → `CANCELLED` (0). Other errors pass through so the
289
+ * caller sees the same structured shape as the rest of the SDK.
290
+ */
291
+ function mapStreamError(e, managed) {
292
+ if (e instanceof ReoptSDKError) return e;
293
+ if (e instanceof Error && e.name === "AbortError") {
294
+ if (managed?.timedOut) return new ReoptSDKError("Stream request timed out", 408, "TIMEOUT");
295
+ return new ReoptSDKError("Stream cancelled by caller", 0, "CANCELLED");
296
+ }
297
+ return e;
298
+ }
299
+ function createReoptAiClient(config) {
300
+ return new ReoptAiClient(config);
301
+ }
302
+
303
+ //#endregion
304
+ export { createReoptAiClient as n, ReoptAiClient as t };
305
+ //# sourceMappingURL=client-DPmoKuR9.js.map