@alquimia-ai/tools 1.0.13 → 1.0.16
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/hooks/index.js +5 -2
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +5 -2
- package/dist/hooks/index.mjs.map +1 -1
- package/dist/types/index.d.mts +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.js.map +1 -1
- package/dist/types/index.mjs.map +1 -1
- package/package.json +25 -39
- package/src/actions/alquimia.action.ts +0 -42
- package/src/actions/apm.action.ts +0 -56
- package/src/actions/baseApi.action.ts +0 -187
- package/src/actions/chat.action.ts +0 -134
- package/src/actions/index.ts +0 -4
- package/src/button.tsx +0 -20
- package/src/card.tsx +0 -25
- package/src/code.tsx +0 -9
- package/src/hooks/alquimia.hook.tsx +0 -239
- package/src/hooks/index.ts +0 -2
- package/src/hooks/useRatings.ts +0 -107
- package/src/providers/alquimia.ts +0 -97
- package/src/providers/elastic-search.ts +0 -82
- package/src/providers/eleven-labs.ts +0 -58
- package/src/providers/index.ts +0 -7
- package/src/providers/openai.ts +0 -69
- package/src/providers/providers.ts +0 -61
- package/src/providers/stability.ts +0 -38
- package/src/reducer/characterization-reducer.tsx +0 -33
- package/src/sdk/alquimia-sdk.ts +0 -205
- package/src/sdk/index.ts +0 -1
- package/src/services/apm/elastic-apm.ts +0 -103
- package/src/services/apm/index.ts +0 -1
- package/src/services/index.ts +0 -1
- package/src/types/index.ts +0 -1
- package/src/types/type.ts +0 -127
- package/src/utils/index.ts +0 -1
- package/src/utils/utils.ts +0 -137
|
@@ -1,187 +0,0 @@
|
|
|
1
|
-
"use server";
|
|
2
|
-
import { BaseAPIConfig } from "../types/type";
|
|
3
|
-
import { generateHeaders, isTextContent } from "../utils";
|
|
4
|
-
|
|
5
|
-
interface ActionResponse<T = void> {
|
|
6
|
-
success: boolean;
|
|
7
|
-
data?: T;
|
|
8
|
-
contentType?: string;
|
|
9
|
-
error?: {
|
|
10
|
-
message: string;
|
|
11
|
-
code?: string;
|
|
12
|
-
details?: any;
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export async function createResource<TResponse, TPayload>(
|
|
17
|
-
config: BaseAPIConfig,
|
|
18
|
-
payload: TPayload
|
|
19
|
-
): Promise<ActionResponse<TResponse>> {
|
|
20
|
-
try {
|
|
21
|
-
const headers = generateHeaders(config);
|
|
22
|
-
|
|
23
|
-
const response = await fetch(`${config.baseUrl}${config.route}`, {
|
|
24
|
-
method: "POST",
|
|
25
|
-
headers,
|
|
26
|
-
body: JSON.stringify(payload),
|
|
27
|
-
cache: "default",
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
if (!response.ok) {
|
|
31
|
-
const error = await response.json();
|
|
32
|
-
return {
|
|
33
|
-
success: false,
|
|
34
|
-
error: {
|
|
35
|
-
message: error.message || `Server returned ${response.status}`,
|
|
36
|
-
code: response.status.toString(),
|
|
37
|
-
details: {
|
|
38
|
-
status: response.status,
|
|
39
|
-
statusText: response.statusText,
|
|
40
|
-
},
|
|
41
|
-
},
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
return {
|
|
46
|
-
success: true,
|
|
47
|
-
data: await response.json(),
|
|
48
|
-
};
|
|
49
|
-
} catch (error) {
|
|
50
|
-
throw new Error("Failed to create resource");
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export async function readResource<T>(
|
|
55
|
-
config: BaseAPIConfig,
|
|
56
|
-
id?: string
|
|
57
|
-
): Promise<ActionResponse<T>> {
|
|
58
|
-
try {
|
|
59
|
-
const headers = generateHeaders(config);
|
|
60
|
-
const url = id
|
|
61
|
-
? `${config.baseUrl}${config.route}/${id}`
|
|
62
|
-
: `${config.baseUrl}${config.route}`;
|
|
63
|
-
|
|
64
|
-
const response = await fetch(url, {
|
|
65
|
-
method: "GET",
|
|
66
|
-
headers,
|
|
67
|
-
cache: "default",
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
if (!response.ok) {
|
|
71
|
-
const error = await response.json();
|
|
72
|
-
return {
|
|
73
|
-
success: false,
|
|
74
|
-
error: {
|
|
75
|
-
message: error.message || `Server returned ${response.status}`,
|
|
76
|
-
code: response.status.toString(),
|
|
77
|
-
details: {
|
|
78
|
-
status: response.status,
|
|
79
|
-
statusText: response.statusText,
|
|
80
|
-
},
|
|
81
|
-
},
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
const contentType = response.headers.get("content-type");
|
|
86
|
-
if (contentType?.includes("application/json")) {
|
|
87
|
-
return {
|
|
88
|
-
success: true,
|
|
89
|
-
data: await response.json(),
|
|
90
|
-
contentType: contentType || undefined,
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
const arrayBuffer = await response.arrayBuffer();
|
|
95
|
-
|
|
96
|
-
if (contentType?.includes("octet-stream") && isTextContent(arrayBuffer)) {
|
|
97
|
-
return {
|
|
98
|
-
success: true,
|
|
99
|
-
data: arrayBuffer as T,
|
|
100
|
-
contentType: "text/plain",
|
|
101
|
-
};
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
return {
|
|
105
|
-
success: true,
|
|
106
|
-
data: await response.json(),
|
|
107
|
-
contentType: contentType || undefined,
|
|
108
|
-
};
|
|
109
|
-
} catch (error) {
|
|
110
|
-
throw new Error("Failed to read resource");
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
export async function updateResource<T>(
|
|
115
|
-
config: BaseAPIConfig,
|
|
116
|
-
id: string,
|
|
117
|
-
data: T
|
|
118
|
-
): Promise<ActionResponse<T>> {
|
|
119
|
-
try {
|
|
120
|
-
const headers = generateHeaders(config);
|
|
121
|
-
|
|
122
|
-
const response = await fetch(`${config.baseUrl}${config.route}/${id}`, {
|
|
123
|
-
method: "PUT",
|
|
124
|
-
headers,
|
|
125
|
-
body: JSON.stringify(data),
|
|
126
|
-
cache: "default",
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
if (!response.ok) {
|
|
130
|
-
const error = await response.json();
|
|
131
|
-
return {
|
|
132
|
-
success: false,
|
|
133
|
-
error: {
|
|
134
|
-
message: error.message || `Server returned ${response.status}`,
|
|
135
|
-
code: response.status.toString(),
|
|
136
|
-
details: {
|
|
137
|
-
status: response.status,
|
|
138
|
-
statusText: response.statusText,
|
|
139
|
-
},
|
|
140
|
-
},
|
|
141
|
-
};
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
return {
|
|
145
|
-
success: true,
|
|
146
|
-
data: await response.json(),
|
|
147
|
-
};
|
|
148
|
-
} catch (error) {
|
|
149
|
-
throw new Error("Failed to update resource");
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
export async function deleteResource(
|
|
154
|
-
config: BaseAPIConfig,
|
|
155
|
-
id: string
|
|
156
|
-
): Promise<ActionResponse<void>> {
|
|
157
|
-
try {
|
|
158
|
-
const headers = generateHeaders(config);
|
|
159
|
-
|
|
160
|
-
const response = await fetch(`${config.baseUrl}${config.route}/${id}`, {
|
|
161
|
-
method: "DELETE",
|
|
162
|
-
headers,
|
|
163
|
-
cache: "default",
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
if (!response.ok) {
|
|
167
|
-
const error = await response.json();
|
|
168
|
-
return {
|
|
169
|
-
success: false,
|
|
170
|
-
error: {
|
|
171
|
-
message: error.message || `Server returned ${response.status}`,
|
|
172
|
-
code: response.status.toString(),
|
|
173
|
-
details: {
|
|
174
|
-
status: response.status,
|
|
175
|
-
statusText: response.statusText,
|
|
176
|
-
},
|
|
177
|
-
},
|
|
178
|
-
};
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
return {
|
|
182
|
-
success: true,
|
|
183
|
-
};
|
|
184
|
-
} catch (error) {
|
|
185
|
-
throw new Error("Failed to delete resource");
|
|
186
|
-
}
|
|
187
|
-
}
|
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
"use server";
|
|
2
|
-
import { NextResponse } from "next/server";
|
|
3
|
-
import { headers } from "next/headers";
|
|
4
|
-
|
|
5
|
-
export type ChatRouteContext = {
|
|
6
|
-
params: {
|
|
7
|
-
path: string[];
|
|
8
|
-
};
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
export type ChatRequestConfig = {
|
|
12
|
-
assistantBaseUrl: string;
|
|
13
|
-
assistantRoute?: string;
|
|
14
|
-
apiKey: string;
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
export type ChatResponse = Response | any;
|
|
18
|
-
|
|
19
|
-
export async function handleChatRequest(
|
|
20
|
-
request: Request,
|
|
21
|
-
context: ChatRouteContext,
|
|
22
|
-
config: ChatRequestConfig
|
|
23
|
-
): Promise<ChatResponse> {
|
|
24
|
-
try {
|
|
25
|
-
const headersList = await headers();
|
|
26
|
-
const body = await request.json();
|
|
27
|
-
const routeParams = await context.params;
|
|
28
|
-
const routePath = routeParams.path.join("/");
|
|
29
|
-
const assistantRoute = config.assistantRoute || "infer";
|
|
30
|
-
|
|
31
|
-
const assistantUrl = `${config.assistantBaseUrl}/${assistantRoute}/${routePath}`;
|
|
32
|
-
|
|
33
|
-
const response = await fetch(assistantUrl, {
|
|
34
|
-
method: "POST",
|
|
35
|
-
headers: {
|
|
36
|
-
"Content-Type": "application/json",
|
|
37
|
-
Authorization: `Bearer ${config.apiKey}`,
|
|
38
|
-
...headersList,
|
|
39
|
-
},
|
|
40
|
-
body: JSON.stringify(body),
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
if (!response.ok) {
|
|
44
|
-
const errorJson = await response.json();
|
|
45
|
-
console.error("Chat inference server error:", {
|
|
46
|
-
status: response.status,
|
|
47
|
-
statusText: response.statusText,
|
|
48
|
-
body: errorJson,
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
return NextResponse.json(
|
|
52
|
-
{
|
|
53
|
-
error:
|
|
54
|
-
errorJson.message ||
|
|
55
|
-
response.statusText ||
|
|
56
|
-
"Failed to forward Chat data",
|
|
57
|
-
},
|
|
58
|
-
{ status: response.status }
|
|
59
|
-
);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
return new NextResponse(response.body, {
|
|
63
|
-
status: 200,
|
|
64
|
-
headers: {
|
|
65
|
-
"Access-Control-Allow-Origin": "*",
|
|
66
|
-
"Access-Control-Allow-Methods": "POST, OPTIONS",
|
|
67
|
-
"Access-Control-Allow-Headers": "Content-Type, Content-Encoding",
|
|
68
|
-
},
|
|
69
|
-
});
|
|
70
|
-
} catch (error) {
|
|
71
|
-
return NextResponse.json(
|
|
72
|
-
{ error: "Failed to forward Chat data" },
|
|
73
|
-
{ status: 500 }
|
|
74
|
-
);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export async function handleStreamRequest(
|
|
79
|
-
request: Request,
|
|
80
|
-
context: ChatRouteContext,
|
|
81
|
-
config: ChatRequestConfig
|
|
82
|
-
): Promise<Response> {
|
|
83
|
-
try {
|
|
84
|
-
const headersList = await headers();
|
|
85
|
-
const routeParams = context.params;
|
|
86
|
-
const path = routeParams.path.join("/");
|
|
87
|
-
const assistantRoute = config.assistantRoute || "stream";
|
|
88
|
-
|
|
89
|
-
const streamUrl = `${config.assistantBaseUrl}/${assistantRoute}/${path}`;
|
|
90
|
-
|
|
91
|
-
const response = await fetch(streamUrl, {
|
|
92
|
-
headers: {
|
|
93
|
-
"Content-Type": "application/json",
|
|
94
|
-
Authorization: `Bearer ${config.apiKey}`,
|
|
95
|
-
...Object.fromEntries(headersList.entries()),
|
|
96
|
-
},
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
if (!response.ok) {
|
|
100
|
-
const errorJson = await response.json();
|
|
101
|
-
console.error("Stream server error:", {
|
|
102
|
-
status: response.status,
|
|
103
|
-
statusText: response.statusText,
|
|
104
|
-
body: errorJson,
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
return new Response(
|
|
108
|
-
JSON.stringify({
|
|
109
|
-
error:
|
|
110
|
-
errorJson.message ||
|
|
111
|
-
response.statusText ||
|
|
112
|
-
"Failed to forward Stream data",
|
|
113
|
-
}),
|
|
114
|
-
{ status: response.status }
|
|
115
|
-
);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
return new Response(response.body, {
|
|
119
|
-
status: 200,
|
|
120
|
-
headers: {
|
|
121
|
-
"Content-Type": "text/event-stream",
|
|
122
|
-
"Cache-Control": "no-cache",
|
|
123
|
-
Connection: "keep-alive",
|
|
124
|
-
},
|
|
125
|
-
});
|
|
126
|
-
} catch (error) {
|
|
127
|
-
console.error("Stream error:", error);
|
|
128
|
-
return new Response(
|
|
129
|
-
JSON.stringify({ error: "Failed to forward stream data" }),
|
|
130
|
-
{ status: 500 }
|
|
131
|
-
);
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
package/src/actions/index.ts
DELETED
package/src/button.tsx
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
|
|
3
|
-
import { ReactNode } from "react";
|
|
4
|
-
|
|
5
|
-
interface ButtonProps {
|
|
6
|
-
children: ReactNode;
|
|
7
|
-
className?: string;
|
|
8
|
-
appName: string;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export const Button = ({ children, className, appName }: ButtonProps) => {
|
|
12
|
-
return (
|
|
13
|
-
<button
|
|
14
|
-
className={className}
|
|
15
|
-
onClick={() => alert(`Hello from your ${appName} app!`)}
|
|
16
|
-
>
|
|
17
|
-
{children}
|
|
18
|
-
</button>
|
|
19
|
-
);
|
|
20
|
-
};
|
package/src/card.tsx
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
export function Card({
|
|
2
|
-
className,
|
|
3
|
-
title,
|
|
4
|
-
children,
|
|
5
|
-
href,
|
|
6
|
-
}: {
|
|
7
|
-
className?: string;
|
|
8
|
-
title: string;
|
|
9
|
-
children: React.ReactNode;
|
|
10
|
-
href: string;
|
|
11
|
-
}): JSX.Element {
|
|
12
|
-
return (
|
|
13
|
-
<a
|
|
14
|
-
className={className}
|
|
15
|
-
href={`${href}?utm_source=create-turbo&utm_medium=basic&utm_campaign=create-turbo"`}
|
|
16
|
-
rel="noopener noreferrer"
|
|
17
|
-
target="_blank"
|
|
18
|
-
>
|
|
19
|
-
<h2>
|
|
20
|
-
{title} <span>-></span>
|
|
21
|
-
</h2>
|
|
22
|
-
<p>{children}</p>
|
|
23
|
-
</a>
|
|
24
|
-
);
|
|
25
|
-
}
|
package/src/code.tsx
DELETED
|
@@ -1,239 +0,0 @@
|
|
|
1
|
-
import { Message } from "ai";
|
|
2
|
-
|
|
3
|
-
import { useEffect, useState } from "react";
|
|
4
|
-
import { AlquimiaSDK } from "../sdk";
|
|
5
|
-
import { AIMessageChunk, AlquimiaMessage } from "../types/type";
|
|
6
|
-
import { createMessageId } from "../utils";
|
|
7
|
-
export function useAlquimia(sdk: AlquimiaSDK) {
|
|
8
|
-
const [chunkReceived, setChunkReceived] = useState<string>("");
|
|
9
|
-
const [currentMessageIdChuncked, setCurrentMessageIdChuncked] =
|
|
10
|
-
useState<string>("");
|
|
11
|
-
const [input, setInput] = useState<string>("");
|
|
12
|
-
const [isLoading, setIsLoading] = useState<boolean>(false);
|
|
13
|
-
const [isMessageStreaming, setIsMessageStreaming] = useState<boolean>(false);
|
|
14
|
-
const [streamingMessageId, setStreamingMessageId] = useState<string | null>(null);
|
|
15
|
-
const [isAudioRecording, setIsAudioRecording] = useState<boolean>(false);
|
|
16
|
-
const [messageMetaData, setMessageMetaData] = useState<any>(null);
|
|
17
|
-
const [messages, setMessages] = useState<AlquimiaMessage[]>([]);
|
|
18
|
-
const [sessionId, setSessionId] = useState<string | null>(null);
|
|
19
|
-
const [activeTool, setActiveTool] = useState<any | null>(null);
|
|
20
|
-
const [lastRequest, setLastRequest] = useState<string | null>(null);
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
useEffect(() => {
|
|
24
|
-
if (chunkReceived)
|
|
25
|
-
processMessageChunk(currentMessageIdChuncked, chunkReceived);
|
|
26
|
-
}, [chunkReceived]);
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
function cleanMessages() {
|
|
30
|
-
setMessages([]);
|
|
31
|
-
}
|
|
32
|
-
async function processMessageChunk(
|
|
33
|
-
messageId: string,
|
|
34
|
-
chunk: string,
|
|
35
|
-
error_code?: string,
|
|
36
|
-
error_detail?: string,
|
|
37
|
-
additionalInfo?: string,
|
|
38
|
-
loading?: boolean
|
|
39
|
-
) {
|
|
40
|
-
setMessages((currentMessages) => {
|
|
41
|
-
const messageIndex = currentMessages.findIndex(
|
|
42
|
-
(message) => message?.id === messageId
|
|
43
|
-
);
|
|
44
|
-
if (messageIndex !== -1) {
|
|
45
|
-
const updatedMessages = [...currentMessages];
|
|
46
|
-
|
|
47
|
-
const updatedMessage: AlquimiaMessage = {
|
|
48
|
-
...updatedMessages[messageIndex],
|
|
49
|
-
content: `${updatedMessages[messageIndex]?.content || ""}${chunk}`,
|
|
50
|
-
id: updatedMessages[messageIndex]?.id || "",
|
|
51
|
-
role: updatedMessages[messageIndex]?.role || "assistant",
|
|
52
|
-
error_code,
|
|
53
|
-
error_detail,
|
|
54
|
-
additionalInfo,
|
|
55
|
-
created_at: new Date().getTime().toString(),
|
|
56
|
-
loading,
|
|
57
|
-
};
|
|
58
|
-
updatedMessages[messageIndex] = updatedMessage;
|
|
59
|
-
return updatedMessages;
|
|
60
|
-
} else {
|
|
61
|
-
return [
|
|
62
|
-
...currentMessages,
|
|
63
|
-
{
|
|
64
|
-
content: `${chunk}`,
|
|
65
|
-
role: "assistant",
|
|
66
|
-
id: messageId,
|
|
67
|
-
error_code,
|
|
68
|
-
error_detail,
|
|
69
|
-
created_at: new Date().getTime().toString(),
|
|
70
|
-
loading,
|
|
71
|
-
},
|
|
72
|
-
];
|
|
73
|
-
}
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
async function handleSubmit(event: React.FormEvent<HTMLFormElement>, traceParentId?: string, sessionId?: string, additionalInfo?: string) {
|
|
78
|
-
setIsLoading(true);
|
|
79
|
-
event.preventDefault();
|
|
80
|
-
|
|
81
|
-
if (input) {
|
|
82
|
-
addUserMessage(input);
|
|
83
|
-
const messageId = createMessageId();
|
|
84
|
-
setCurrentMessageIdChuncked(messageId);
|
|
85
|
-
setStreamingMessageId(messageId);
|
|
86
|
-
await sendMessage(input, (chunk: AIMessageChunk) => {
|
|
87
|
-
processMessageChunk(
|
|
88
|
-
messageId,
|
|
89
|
-
chunk?.data?.content || "",
|
|
90
|
-
chunk?.error_code,
|
|
91
|
-
chunk?.error_detail,
|
|
92
|
-
additionalInfo,
|
|
93
|
-
chunk?.loading
|
|
94
|
-
);
|
|
95
|
-
}, traceParentId, sessionId);
|
|
96
|
-
setInput("");
|
|
97
|
-
}
|
|
98
|
-
setIsLoading(false);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
function handleInputChange(event: React.ChangeEvent<HTMLInputElement>) {
|
|
102
|
-
setInput(event.target.value);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
function handleReplaceInput(input: string) {
|
|
106
|
-
setInput(input);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
function populateMessages(messages: Message[]) {
|
|
110
|
-
setMessages(messages);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
function handleLoadingCancel() {
|
|
114
|
-
setIsLoading(false);
|
|
115
|
-
setIsMessageStreaming(false);
|
|
116
|
-
setInput("");
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
function addUserMessage(message: string) {
|
|
120
|
-
setMessages((prevMessages: AlquimiaMessage[]) => [
|
|
121
|
-
...prevMessages,
|
|
122
|
-
{
|
|
123
|
-
content: message,
|
|
124
|
-
role: "user",
|
|
125
|
-
id: createMessageId(),
|
|
126
|
-
created_at: new Date().getTime().toString(),
|
|
127
|
-
},
|
|
128
|
-
]);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
function getCookie(name: string): string | null {
|
|
132
|
-
const cookies = document.cookie.split(";");
|
|
133
|
-
for (let cookie of cookies) {
|
|
134
|
-
const [cookieName, cookieValue] = cookie.trim().split("=");
|
|
135
|
-
if (cookieName === name) {
|
|
136
|
-
return decodeURIComponent(cookieValue as string);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
return null;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
async function sendMessage(
|
|
143
|
-
message: string,
|
|
144
|
-
callBack: (chunk: AIMessageChunk) => void,
|
|
145
|
-
traceParentId?: string,
|
|
146
|
-
sessionId?: string
|
|
147
|
-
) {
|
|
148
|
-
callBack({
|
|
149
|
-
type: "loading",
|
|
150
|
-
loading: true,
|
|
151
|
-
});
|
|
152
|
-
const conversationId = sessionId || getCookie("alquimia-session");
|
|
153
|
-
setIsMessageStreaming(true);
|
|
154
|
-
setActiveTool(null);
|
|
155
|
-
setLastRequest(message);
|
|
156
|
-
|
|
157
|
-
try {
|
|
158
|
-
const response = await sdk
|
|
159
|
-
.widthConversationId(conversationId || "")
|
|
160
|
-
.sendMessage(message, traceParentId);
|
|
161
|
-
const eventSource = new EventSource(response.getUrlStream());
|
|
162
|
-
|
|
163
|
-
const cleanup = (isStreaming: boolean) => {
|
|
164
|
-
setIsMessageStreaming(isStreaming);
|
|
165
|
-
setStreamingMessageId(null);
|
|
166
|
-
eventSource.close();
|
|
167
|
-
};
|
|
168
|
-
|
|
169
|
-
eventSource.onmessage = (event: MessageEvent) => {
|
|
170
|
-
const parsedData = JSON.parse(event.data);
|
|
171
|
-
|
|
172
|
-
if (parsedData.tooler?.length > 0 && !activeTool) {
|
|
173
|
-
setActiveTool({ tooler: parsedData.tooler });
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
if (parsedData.error_code) {
|
|
177
|
-
callBack({
|
|
178
|
-
type: "error",
|
|
179
|
-
error_code: parsedData.error_code,
|
|
180
|
-
error_detail: parsedData.error_detail,
|
|
181
|
-
});
|
|
182
|
-
cleanup(false);
|
|
183
|
-
return;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
if (parsedData.is_complete) {
|
|
187
|
-
cleanup(false);
|
|
188
|
-
} else {
|
|
189
|
-
callBack(parsedData.answer);
|
|
190
|
-
}
|
|
191
|
-
};
|
|
192
|
-
|
|
193
|
-
eventSource.onerror = (event: Event) => {
|
|
194
|
-
console.error("Stream connection error", event);
|
|
195
|
-
cleanup(false);
|
|
196
|
-
callBack({
|
|
197
|
-
type: "error",
|
|
198
|
-
error_code: "STREAM_ERROR",
|
|
199
|
-
error_detail: "Failed to establish stream connection.",
|
|
200
|
-
});
|
|
201
|
-
};
|
|
202
|
-
} catch (error: any) {
|
|
203
|
-
console.error("Error sending message:", error);
|
|
204
|
-
setIsMessageStreaming(false);
|
|
205
|
-
setStreamingMessageId(null);
|
|
206
|
-
callBack({
|
|
207
|
-
type: "error",
|
|
208
|
-
error_code: error.status?.toString() || "REQUEST_ERROR",
|
|
209
|
-
error_detail: error.message || "Error initiating the stream.",
|
|
210
|
-
});
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
return {
|
|
215
|
-
activeTool,
|
|
216
|
-
cleanMessages,
|
|
217
|
-
createMessageId,
|
|
218
|
-
handleInputChange,
|
|
219
|
-
handleReplaceInput,
|
|
220
|
-
handleSubmit,
|
|
221
|
-
handleLoadingCancel,
|
|
222
|
-
input,
|
|
223
|
-
isLoading,
|
|
224
|
-
isMessageStreaming,
|
|
225
|
-
streamingMessageId,
|
|
226
|
-
isAudioRecording,
|
|
227
|
-
lastRequest,
|
|
228
|
-
messageMetaData,
|
|
229
|
-
messages,
|
|
230
|
-
populateMessages,
|
|
231
|
-
processMessageChunk,
|
|
232
|
-
sendMessage,
|
|
233
|
-
sessionId,
|
|
234
|
-
setActiveTool,
|
|
235
|
-
setSessionId,
|
|
236
|
-
setLastRequest,
|
|
237
|
-
setIsAudioRecording,
|
|
238
|
-
};
|
|
239
|
-
}
|
package/src/hooks/index.ts
DELETED