@idevconn/ai-chat-client 0.1.2

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.
@@ -0,0 +1,19 @@
1
+ import { A as AiChatConfig, a as ChatStreamEvent } from './types-GYJVuR2m.mjs';
2
+ export { C as ChatMessageUI, S as StarterTopic, T as ThemeConfig } from './types-GYJVuR2m.mjs';
3
+
4
+ type TransportStatus = 'disconnected' | 'connected' | 'streaming';
5
+ declare class ChatClient {
6
+ private readonly config;
7
+ private readonly endpoint;
8
+ private readonly headers;
9
+ private sessionId;
10
+ private status;
11
+ constructor(config: AiChatConfig);
12
+ getStatus(): TransportStatus;
13
+ getSessionId(): string;
14
+ connect(): Promise<void>;
15
+ disconnect(): Promise<void>;
16
+ send(message: string): AsyncGenerator<ChatStreamEvent>;
17
+ }
18
+
19
+ export { AiChatConfig, ChatClient, ChatStreamEvent, type TransportStatus };
@@ -0,0 +1,19 @@
1
+ import { A as AiChatConfig, a as ChatStreamEvent } from './types-GYJVuR2m.js';
2
+ export { C as ChatMessageUI, S as StarterTopic, T as ThemeConfig } from './types-GYJVuR2m.js';
3
+
4
+ type TransportStatus = 'disconnected' | 'connected' | 'streaming';
5
+ declare class ChatClient {
6
+ private readonly config;
7
+ private readonly endpoint;
8
+ private readonly headers;
9
+ private sessionId;
10
+ private status;
11
+ constructor(config: AiChatConfig);
12
+ getStatus(): TransportStatus;
13
+ getSessionId(): string;
14
+ connect(): Promise<void>;
15
+ disconnect(): Promise<void>;
16
+ send(message: string): AsyncGenerator<ChatStreamEvent>;
17
+ }
18
+
19
+ export { AiChatConfig, ChatClient, ChatStreamEvent, type TransportStatus };
package/dist/index.js ADDED
@@ -0,0 +1,116 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ ChatClient: () => ChatClient
24
+ });
25
+ module.exports = __toCommonJS(src_exports);
26
+
27
+ // src/lib/utils.ts
28
+ function generateId() {
29
+ return crypto.randomUUID?.() || Math.random().toString(36).slice(2);
30
+ }
31
+
32
+ // src/core/chat-client.ts
33
+ var ChatClient = class {
34
+ constructor(config) {
35
+ this.config = config;
36
+ this.endpoint = config.endpoint;
37
+ this.headers = { "Content-Type": "application/json" };
38
+ if (config.apiKey) this.headers["x-api-key"] = config.apiKey;
39
+ if (config.token) this.headers["Authorization"] = `Bearer ${config.token}`;
40
+ this.sessionId = generateId();
41
+ }
42
+ config;
43
+ endpoint;
44
+ headers;
45
+ sessionId;
46
+ status = "disconnected";
47
+ getStatus() {
48
+ return this.status;
49
+ }
50
+ getSessionId() {
51
+ return this.sessionId;
52
+ }
53
+ async connect() {
54
+ this.status = "connected";
55
+ }
56
+ async disconnect() {
57
+ this.status = "disconnected";
58
+ }
59
+ async *send(message) {
60
+ this.status = "streaming";
61
+ if (this.config.getToken) {
62
+ const token = await this.config.getToken();
63
+ this.headers["Authorization"] = `Bearer ${token}`;
64
+ }
65
+ const response = await fetch(this.endpoint, {
66
+ method: "POST",
67
+ headers: this.headers,
68
+ body: JSON.stringify({
69
+ sessionId: this.sessionId,
70
+ message
71
+ })
72
+ });
73
+ if (!response.ok) {
74
+ yield {
75
+ type: "error",
76
+ error: `Server error: ${response.status}`
77
+ };
78
+ this.status = "connected";
79
+ return;
80
+ }
81
+ const reader = response.body?.getReader();
82
+ if (!reader) {
83
+ yield { type: "error", error: "No response body" };
84
+ this.status = "connected";
85
+ return;
86
+ }
87
+ const decoder = new TextDecoder();
88
+ let buffer = "";
89
+ while (true) {
90
+ const { done, value } = await reader.read();
91
+ if (done) break;
92
+ buffer += decoder.decode(value, { stream: true });
93
+ const lines = buffer.split("\n");
94
+ buffer = lines.pop() || "";
95
+ for (const line of lines) {
96
+ if (!line.startsWith("data: ")) continue;
97
+ const data = line.slice(6).trim();
98
+ if (!data || data === "[DONE]") continue;
99
+ try {
100
+ const event = JSON.parse(data);
101
+ yield event;
102
+ if (event.type === "done" || event.type === "error") {
103
+ this.status = "connected";
104
+ return;
105
+ }
106
+ } catch {
107
+ }
108
+ }
109
+ }
110
+ this.status = "connected";
111
+ }
112
+ };
113
+ // Annotate the CommonJS export names for ESM import in node:
114
+ 0 && (module.exports = {
115
+ ChatClient
116
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,89 @@
1
+ // src/lib/utils.ts
2
+ function generateId() {
3
+ return crypto.randomUUID?.() || Math.random().toString(36).slice(2);
4
+ }
5
+
6
+ // src/core/chat-client.ts
7
+ var ChatClient = class {
8
+ constructor(config) {
9
+ this.config = config;
10
+ this.endpoint = config.endpoint;
11
+ this.headers = { "Content-Type": "application/json" };
12
+ if (config.apiKey) this.headers["x-api-key"] = config.apiKey;
13
+ if (config.token) this.headers["Authorization"] = `Bearer ${config.token}`;
14
+ this.sessionId = generateId();
15
+ }
16
+ config;
17
+ endpoint;
18
+ headers;
19
+ sessionId;
20
+ status = "disconnected";
21
+ getStatus() {
22
+ return this.status;
23
+ }
24
+ getSessionId() {
25
+ return this.sessionId;
26
+ }
27
+ async connect() {
28
+ this.status = "connected";
29
+ }
30
+ async disconnect() {
31
+ this.status = "disconnected";
32
+ }
33
+ async *send(message) {
34
+ this.status = "streaming";
35
+ if (this.config.getToken) {
36
+ const token = await this.config.getToken();
37
+ this.headers["Authorization"] = `Bearer ${token}`;
38
+ }
39
+ const response = await fetch(this.endpoint, {
40
+ method: "POST",
41
+ headers: this.headers,
42
+ body: JSON.stringify({
43
+ sessionId: this.sessionId,
44
+ message
45
+ })
46
+ });
47
+ if (!response.ok) {
48
+ yield {
49
+ type: "error",
50
+ error: `Server error: ${response.status}`
51
+ };
52
+ this.status = "connected";
53
+ return;
54
+ }
55
+ const reader = response.body?.getReader();
56
+ if (!reader) {
57
+ yield { type: "error", error: "No response body" };
58
+ this.status = "connected";
59
+ return;
60
+ }
61
+ const decoder = new TextDecoder();
62
+ let buffer = "";
63
+ while (true) {
64
+ const { done, value } = await reader.read();
65
+ if (done) break;
66
+ buffer += decoder.decode(value, { stream: true });
67
+ const lines = buffer.split("\n");
68
+ buffer = lines.pop() || "";
69
+ for (const line of lines) {
70
+ if (!line.startsWith("data: ")) continue;
71
+ const data = line.slice(6).trim();
72
+ if (!data || data === "[DONE]") continue;
73
+ try {
74
+ const event = JSON.parse(data);
75
+ yield event;
76
+ if (event.type === "done" || event.type === "error") {
77
+ this.status = "connected";
78
+ return;
79
+ }
80
+ } catch {
81
+ }
82
+ }
83
+ }
84
+ this.status = "connected";
85
+ }
86
+ };
87
+ export {
88
+ ChatClient
89
+ };
@@ -0,0 +1,7 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { A as AiChatConfig } from '../types-GYJVuR2m.mjs';
3
+
4
+ type AiChatProps = AiChatConfig;
5
+ declare function AiChat(props: AiChatProps): react_jsx_runtime.JSX.Element;
6
+
7
+ export { AiChat, type AiChatProps };
@@ -0,0 +1,7 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { A as AiChatConfig } from '../types-GYJVuR2m.js';
3
+
4
+ type AiChatProps = AiChatConfig;
5
+ declare function AiChat(props: AiChatProps): react_jsx_runtime.JSX.Element;
6
+
7
+ export { AiChat, type AiChatProps };