@aizu-chat/react 0.1.1 → 0.1.3

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.mjs CHANGED
@@ -997,6 +997,26 @@ var useChatContext = () => {
997
997
 
998
998
  // src/sdk/client.ts
999
999
  var AizuClient = class {
1000
+ /**
1001
+ * Creates a new AizuClient instance
1002
+ *
1003
+ * @param config - Configuration options for the client
1004
+ * @param config.hostUrl - The base URL of the Aizu API (e.g., 'https://api.aizu.chat')
1005
+ * @param config.apiKey - Your API key for authentication
1006
+ * @param config.timeout - Request timeout in milliseconds (default: 30000)
1007
+ *
1008
+ * @throws {Error} If hostUrl is not a non-empty string
1009
+ * @throws {Error} If apiKey is not a non-empty string
1010
+ *
1011
+ * @example
1012
+ * ```typescript
1013
+ * const client = new AizuClient({
1014
+ * hostUrl: 'https://api.aizu.chat',
1015
+ * apiKey: 'sk_test_123456',
1016
+ * timeout: 60000 // 60 seconds
1017
+ * });
1018
+ * ```
1019
+ */
1000
1020
  constructor(config) {
1001
1021
  const { hostUrl, apiKey, timeout } = config;
1002
1022
  if (typeof hostUrl !== "string" || hostUrl.trim().length === 0) {
@@ -1013,6 +1033,26 @@ var AizuClient = class {
1013
1033
  this.apiKey = apiKey;
1014
1034
  this.timeout = timeout != null ? timeout : 3e4;
1015
1035
  }
1036
+ /**
1037
+ * Internal method for making HTTP requests to the API
1038
+ *
1039
+ * Handles timeout management, abort signal combination, and error handling.
1040
+ * Supports both user-initiated cancellation and automatic timeout.
1041
+ *
1042
+ * @template T - The expected response type
1043
+ * @template TBody - The request body type
1044
+ *
1045
+ * @param path - API endpoint path (e.g., '/chat/query')
1046
+ * @param options - Request options including body and signal
1047
+ *
1048
+ * @returns Promise resolving to the API response
1049
+ *
1050
+ * @throws {Error} "Request cancelled by user" - If the request is cancelled via AbortSignal
1051
+ * @throws {Error} "Request timeout: The server took too long to respond" - If the request exceeds the timeout
1052
+ * @throws {Error} "API Error: {status} {statusText}" - If the API returns an error status
1053
+ *
1054
+ * @private
1055
+ */
1016
1056
  request(_0) {
1017
1057
  return __async(this, arguments, function* (path, options = {}) {
1018
1058
  const url = `${this.hostUrl}${path}`;
@@ -1083,6 +1123,64 @@ var AizuClient = class {
1083
1123
  }
1084
1124
  });
1085
1125
  }
1126
+ /**
1127
+ * Sends a chat query to the Aizu API
1128
+ *
1129
+ * @param params - Chat request parameters
1130
+ * @param params.query - The user's message/question (required, non-empty)
1131
+ * @param params.conversationId - Optional conversation ID to continue an existing conversation
1132
+ * @param options - Additional options
1133
+ * @param options.signal - Optional AbortSignal to cancel the request
1134
+ *
1135
+ * @returns Promise resolving to the chat response from the API
1136
+ *
1137
+ * @throws {Error} "Query is required" - If query is empty or whitespace only
1138
+ * @throws {Error} "Request cancelled by user" - If the request is cancelled
1139
+ * @throws {Error} "Request timeout: The server took too long to respond" - If the request times out
1140
+ * @throws {Error} "API Error: {status} {statusText}" - If the API returns an error
1141
+ *
1142
+ * @example
1143
+ * ```typescript
1144
+ * // Simple query
1145
+ * const response = await client.chat({
1146
+ * query: 'What is the weather today?'
1147
+ * });
1148
+ * console.log(response.answer);
1149
+ * ```
1150
+ *
1151
+ * @example
1152
+ * ```typescript
1153
+ * // Continue conversation
1154
+ * const response1 = await client.chat({
1155
+ * query: 'What is the weather today?'
1156
+ * });
1157
+ *
1158
+ * const response2 = await client.chat({
1159
+ * query: 'And tomorrow?',
1160
+ * conversationId: response1.conversationId
1161
+ * });
1162
+ * ```
1163
+ *
1164
+ * @example
1165
+ * ```typescript
1166
+ * // With cancellation support
1167
+ * const controller = new AbortController();
1168
+ *
1169
+ * const promise = client.chat(
1170
+ * { query: 'Long running query...' },
1171
+ * { signal: controller.signal }
1172
+ * );
1173
+ *
1174
+ * // Cancel after 5 seconds
1175
+ * setTimeout(() => controller.abort(), 5000);
1176
+ *
1177
+ * try {
1178
+ * const response = await promise;
1179
+ * } catch (error) {
1180
+ * console.error('Request was cancelled:', error);
1181
+ * }
1182
+ * ```
1183
+ */
1086
1184
  chat(params, options) {
1087
1185
  return __async(this, null, function* () {
1088
1186
  if (!params.query.trim()) {
@@ -1201,12 +1299,15 @@ import {
1201
1299
 
1202
1300
  // src/hooks/use-keyboard.ts
1203
1301
  import { useCallback as useCallback6, useEffect as useEffect3, useRef as useRef4, useState as useState4 } from "react";
1302
+ var MAX_LENGTH = 300;
1204
1303
  var useKeyboard = ({ userMessages, isLoading, onSend }) => {
1205
1304
  const [value, setValue] = useState4("");
1206
1305
  const [historyIndex, setHistoryIndex] = useState4((userMessages == null ? void 0 : userMessages.length) || 0);
1207
1306
  const textareaRef = useRef4(null);
1208
1307
  const handleChange = (e) => {
1209
- setValue(e.target.value);
1308
+ const currentValue = e.target.value;
1309
+ const truncatedValue = currentValue.length > MAX_LENGTH ? currentValue.slice(0, MAX_LENGTH) : currentValue;
1310
+ setValue(truncatedValue);
1210
1311
  };
1211
1312
  const handleSubmit = (e) => {
1212
1313
  e.preventDefault();
@@ -1230,17 +1331,21 @@ var useKeyboard = ({ userMessages, isLoading, onSend }) => {
1230
1331
  if (key === "ArrowUp") {
1231
1332
  e.preventDefault();
1232
1333
  if (userMessages.length === 0) return;
1233
- const next = historyIndex === userMessages.length ? userMessages.length - 1 : Math.max(0, historyIndex - 1);
1234
- setHistoryIndex(next);
1235
- setValue((_b = (_a = userMessages[next]) == null ? void 0 : _a.text) != null ? _b : "");
1334
+ const nextIndex = historyIndex === userMessages.length ? userMessages.length - 1 : Math.max(0, historyIndex - 1);
1335
+ setHistoryIndex(nextIndex);
1336
+ const nextValue = (_b = (_a = userMessages[nextIndex]) == null ? void 0 : _a.text) != null ? _b : "";
1337
+ const truncatedValue = nextValue.slice(0, MAX_LENGTH);
1338
+ setValue(truncatedValue);
1236
1339
  return;
1237
1340
  }
1238
1341
  if (key === "ArrowDown") {
1239
1342
  e.preventDefault();
1240
1343
  if (userMessages.length === 0) return;
1241
- const next = historyIndex >= userMessages.length - 1 ? userMessages.length : historyIndex + 1;
1242
- setHistoryIndex(next);
1243
- setValue(next === userMessages.length ? "" : (_d = (_c = userMessages[next]) == null ? void 0 : _c.text) != null ? _d : "");
1344
+ const nextIndex = historyIndex >= userMessages.length - 1 ? userMessages.length : historyIndex + 1;
1345
+ setHistoryIndex(nextIndex);
1346
+ const nextValue = nextIndex === userMessages.length ? "" : (_d = (_c = userMessages[nextIndex]) == null ? void 0 : _c.text) != null ? _d : "";
1347
+ const truncatedValue = nextValue.slice(0, MAX_LENGTH);
1348
+ setValue(truncatedValue);
1244
1349
  }
1245
1350
  };
1246
1351
  const adjustTextareaHeight = useCallback6(() => {
package/dist/sdk.d.mts ADDED
@@ -0,0 +1,55 @@
1
+ interface AizuClientConfig {
2
+ hostUrl: string;
3
+ apiKey: string;
4
+ timeout?: number;
5
+ }
6
+ interface ChatRequest {
7
+ query: string;
8
+ thread_id?: string;
9
+ resource_id?: string;
10
+ }
11
+ interface ChatResponse {
12
+ thread_id: string;
13
+ resource_id: string;
14
+ response: string;
15
+ products?: Product[];
16
+ type?: TypeResponse;
17
+ }
18
+ interface Product {
19
+ product_id: string;
20
+ spu?: string;
21
+ sku: string;
22
+ title: string;
23
+ description: string;
24
+ handle: string;
25
+ price: number;
26
+ stock: number;
27
+ thumbnail?: string | null;
28
+ rating?: number;
29
+ review_count?: number;
30
+ estimated_delivery_days?: number;
31
+ similarity?: number;
32
+ }
33
+ declare enum TypeResponse {
34
+ SUGGEST_PRODUCTS = "suggest_products",
35
+ REQUEST_CLARIFICATION = "request_clarification",
36
+ START_SHOPPING = "start_shopping"
37
+ }
38
+
39
+ declare class AizuClient {
40
+ private hostUrl;
41
+ private apiKey;
42
+ private timeout;
43
+ constructor(config: AizuClientConfig);
44
+ private request;
45
+ chat(params: ChatRequest, options?: {
46
+ signal?: AbortSignal;
47
+ }): Promise<ChatResponse>;
48
+ }
49
+ declare const createAizuClient: ({ hostUrl, apiKey, timeout, }: {
50
+ hostUrl: string;
51
+ apiKey: string;
52
+ timeout?: number;
53
+ }) => AizuClient;
54
+
55
+ export { AizuClient, type AizuClientConfig, type ChatRequest, type ChatResponse, type Product, TypeResponse, createAizuClient };
package/dist/sdk.d.ts ADDED
@@ -0,0 +1,55 @@
1
+ interface AizuClientConfig {
2
+ hostUrl: string;
3
+ apiKey: string;
4
+ timeout?: number;
5
+ }
6
+ interface ChatRequest {
7
+ query: string;
8
+ thread_id?: string;
9
+ resource_id?: string;
10
+ }
11
+ interface ChatResponse {
12
+ thread_id: string;
13
+ resource_id: string;
14
+ response: string;
15
+ products?: Product[];
16
+ type?: TypeResponse;
17
+ }
18
+ interface Product {
19
+ product_id: string;
20
+ spu?: string;
21
+ sku: string;
22
+ title: string;
23
+ description: string;
24
+ handle: string;
25
+ price: number;
26
+ stock: number;
27
+ thumbnail?: string | null;
28
+ rating?: number;
29
+ review_count?: number;
30
+ estimated_delivery_days?: number;
31
+ similarity?: number;
32
+ }
33
+ declare enum TypeResponse {
34
+ SUGGEST_PRODUCTS = "suggest_products",
35
+ REQUEST_CLARIFICATION = "request_clarification",
36
+ START_SHOPPING = "start_shopping"
37
+ }
38
+
39
+ declare class AizuClient {
40
+ private hostUrl;
41
+ private apiKey;
42
+ private timeout;
43
+ constructor(config: AizuClientConfig);
44
+ private request;
45
+ chat(params: ChatRequest, options?: {
46
+ signal?: AbortSignal;
47
+ }): Promise<ChatResponse>;
48
+ }
49
+ declare const createAizuClient: ({ hostUrl, apiKey, timeout, }: {
50
+ hostUrl: string;
51
+ apiKey: string;
52
+ timeout?: number;
53
+ }) => AizuClient;
54
+
55
+ export { AizuClient, type AizuClientConfig, type ChatRequest, type ChatResponse, type Product, TypeResponse, createAizuClient };
package/dist/sdk.js ADDED
@@ -0,0 +1,200 @@
1
+ 'use client'
2
+ "use strict";
3
+ var __defProp = Object.defineProperty;
4
+ var __defProps = Object.defineProperties;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
7
+ var __getOwnPropNames = Object.getOwnPropertyNames;
8
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
9
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
10
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
11
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
12
+ var __spreadValues = (a, b) => {
13
+ for (var prop in b || (b = {}))
14
+ if (__hasOwnProp.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ if (__getOwnPropSymbols)
17
+ for (var prop of __getOwnPropSymbols(b)) {
18
+ if (__propIsEnum.call(b, prop))
19
+ __defNormalProp(a, prop, b[prop]);
20
+ }
21
+ return a;
22
+ };
23
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
24
+ var __objRest = (source, exclude) => {
25
+ var target = {};
26
+ for (var prop in source)
27
+ if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
28
+ target[prop] = source[prop];
29
+ if (source != null && __getOwnPropSymbols)
30
+ for (var prop of __getOwnPropSymbols(source)) {
31
+ if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
32
+ target[prop] = source[prop];
33
+ }
34
+ return target;
35
+ };
36
+ var __export = (target, all) => {
37
+ for (var name in all)
38
+ __defProp(target, name, { get: all[name], enumerable: true });
39
+ };
40
+ var __copyProps = (to, from, except, desc) => {
41
+ if (from && typeof from === "object" || typeof from === "function") {
42
+ for (let key of __getOwnPropNames(from))
43
+ if (!__hasOwnProp.call(to, key) && key !== except)
44
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
45
+ }
46
+ return to;
47
+ };
48
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
49
+ var __async = (__this, __arguments, generator) => {
50
+ return new Promise((resolve, reject) => {
51
+ var fulfilled = (value) => {
52
+ try {
53
+ step(generator.next(value));
54
+ } catch (e) {
55
+ reject(e);
56
+ }
57
+ };
58
+ var rejected = (value) => {
59
+ try {
60
+ step(generator.throw(value));
61
+ } catch (e) {
62
+ reject(e);
63
+ }
64
+ };
65
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
66
+ step((generator = generator.apply(__this, __arguments)).next());
67
+ });
68
+ };
69
+
70
+ // src/sdk/index.ts
71
+ var sdk_exports = {};
72
+ __export(sdk_exports, {
73
+ AizuClient: () => AizuClient,
74
+ TypeResponse: () => TypeResponse,
75
+ createAizuClient: () => createAizuClient
76
+ });
77
+ module.exports = __toCommonJS(sdk_exports);
78
+
79
+ // src/sdk/client.ts
80
+ var AizuClient = class {
81
+ constructor(config) {
82
+ const { hostUrl, apiKey, timeout } = config;
83
+ if (typeof hostUrl !== "string" || hostUrl.trim().length === 0) {
84
+ throw new Error(
85
+ "Invalid AizuClient configuration: 'hostUrl' must be a non-empty string."
86
+ );
87
+ }
88
+ if (typeof apiKey !== "string" || apiKey.trim().length === 0) {
89
+ throw new Error(
90
+ "Invalid AizuClient configuration: 'apiKey' must be a non-empty string."
91
+ );
92
+ }
93
+ this.hostUrl = hostUrl;
94
+ this.apiKey = apiKey;
95
+ this.timeout = timeout != null ? timeout : 3e4;
96
+ }
97
+ request(_0) {
98
+ return __async(this, arguments, function* (path, options = {}) {
99
+ const url = `${this.hostUrl}${path}`;
100
+ const _a = options, { body, signal: externalSignal } = _a, restOptions = __objRest(_a, ["body", "signal"]);
101
+ const timeoutController = new AbortController();
102
+ const timeoutId = setTimeout(() => timeoutController.abort(), this.timeout);
103
+ let abortController = null;
104
+ let abortHandler = null;
105
+ let abortReason = null;
106
+ const combinedSignal = externalSignal ? (() => {
107
+ abortController = new AbortController();
108
+ const userAbortHandler = () => {
109
+ if (!abortReason) abortReason = "user";
110
+ abortController == null ? void 0 : abortController.abort();
111
+ clearTimeout(timeoutId);
112
+ };
113
+ const timeoutAbortHandler = () => {
114
+ if (!abortReason) abortReason = "timeout";
115
+ abortController == null ? void 0 : abortController.abort();
116
+ };
117
+ const listeners = [
118
+ { signal: externalSignal, handler: userAbortHandler },
119
+ { signal: timeoutController.signal, handler: timeoutAbortHandler }
120
+ ];
121
+ listeners.forEach(({ signal, handler }) => {
122
+ signal.addEventListener("abort", handler);
123
+ });
124
+ abortHandler = () => {
125
+ listeners.forEach(({ signal, handler }) => {
126
+ signal.removeEventListener("abort", handler);
127
+ });
128
+ };
129
+ return abortController.signal;
130
+ })() : timeoutController.signal;
131
+ try {
132
+ const response = yield fetch(url, __spreadProps(__spreadValues({}, restOptions), {
133
+ signal: combinedSignal,
134
+ headers: __spreadValues({
135
+ "Content-Type": "application/json",
136
+ "x-api-key": this.apiKey
137
+ }, options.headers),
138
+ body: body ? JSON.stringify(body) : void 0
139
+ }));
140
+ if (!response.ok) {
141
+ const errorBody = yield response.text().catch(() => "");
142
+ const errorMessage = `API Error: ${response.status} ${response.statusText}${errorBody ? ` - ${errorBody}` : ""}`;
143
+ throw new Error(errorMessage);
144
+ }
145
+ return yield response.json();
146
+ } catch (error) {
147
+ if (error instanceof Error && error.name === "AbortError") {
148
+ if (abortReason === "user") {
149
+ throw new Error("Request cancelled by user");
150
+ }
151
+ if (abortReason === "timeout") {
152
+ throw new Error(
153
+ "Request timeout: The server took too long to respond"
154
+ );
155
+ }
156
+ throw new Error("Request was aborted");
157
+ }
158
+ throw error;
159
+ } finally {
160
+ clearTimeout(timeoutId);
161
+ if (abortHandler) {
162
+ abortHandler();
163
+ }
164
+ }
165
+ });
166
+ }
167
+ chat(params, options) {
168
+ return __async(this, null, function* () {
169
+ if (!params.query.trim()) {
170
+ throw new Error("Query is required");
171
+ }
172
+ return this.request("/chat/query", {
173
+ method: "POST",
174
+ body: params,
175
+ signal: options == null ? void 0 : options.signal
176
+ });
177
+ });
178
+ }
179
+ };
180
+ var createAizuClient = ({
181
+ hostUrl,
182
+ apiKey,
183
+ timeout
184
+ }) => {
185
+ return new AizuClient({ hostUrl, apiKey, timeout });
186
+ };
187
+
188
+ // src/sdk/types.ts
189
+ var TypeResponse = /* @__PURE__ */ ((TypeResponse2) => {
190
+ TypeResponse2["SUGGEST_PRODUCTS"] = "suggest_products";
191
+ TypeResponse2["REQUEST_CLARIFICATION"] = "request_clarification";
192
+ TypeResponse2["START_SHOPPING"] = "start_shopping";
193
+ return TypeResponse2;
194
+ })(TypeResponse || {});
195
+ // Annotate the CommonJS export names for ESM import in node:
196
+ 0 && (module.exports = {
197
+ AizuClient,
198
+ TypeResponse,
199
+ createAizuClient
200
+ });
package/dist/sdk.mjs ADDED
@@ -0,0 +1,12 @@
1
+ 'use client'
2
+ import {
3
+ AizuClient,
4
+ TypeResponse,
5
+ createAizuClient
6
+ } from "./chunk-NBQP747C.mjs";
7
+ import "./chunk-33CPD3DF.mjs";
8
+ export {
9
+ AizuClient,
10
+ TypeResponse,
11
+ createAizuClient
12
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aizu-chat/react",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "main": "./dist/index.js",
5
5
  "module": "./dist/index.mjs",
6
6
  "types": "./dist/index.d.ts",
@@ -49,4 +49,4 @@
49
49
  "clsx": "^2.1.1",
50
50
  "tailwind-merge": "^3.4.0"
51
51
  }
52
- }
52
+ }