@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
package/src/sdk/alquimia-sdk.ts
DELETED
|
@@ -1,205 +0,0 @@
|
|
|
1
|
-
import axios, { AxiosInstance } from "axios";
|
|
2
|
-
import {
|
|
3
|
-
StableDiffusionProvider,
|
|
4
|
-
WhisperProvider,
|
|
5
|
-
CharacterizationProvider,
|
|
6
|
-
RatingsProvider,
|
|
7
|
-
LoggerProvider,
|
|
8
|
-
} from "../providers";
|
|
9
|
-
interface AlquimiaSDKConfig {
|
|
10
|
-
apiKey: string;
|
|
11
|
-
chatUrl: string;
|
|
12
|
-
streamUrl: string;
|
|
13
|
-
assistantId: string;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
class AlquimiaSDK {
|
|
18
|
-
private config: AlquimiaSDKConfig;
|
|
19
|
-
private axiosInstance: AxiosInstance;
|
|
20
|
-
private conversationId: string | null = null;
|
|
21
|
-
private sessionId: string | null = null;
|
|
22
|
-
private streamId: string | null = null;
|
|
23
|
-
private tools: any = {};
|
|
24
|
-
private extraData: any = {};
|
|
25
|
-
private forceProfile: any = {};
|
|
26
|
-
private whisperProvider?: WhisperProvider
|
|
27
|
-
private stableDiffusionProvider?: StableDiffusionProvider
|
|
28
|
-
private analyzeCharacterizationProvider?: CharacterizationProvider
|
|
29
|
-
private ratingsProvider?: RatingsProvider
|
|
30
|
-
private loggerProvider?: LoggerProvider
|
|
31
|
-
private enforceCharacterization?: boolean
|
|
32
|
-
|
|
33
|
-
constructor(config: AlquimiaSDKConfig, enforceCharacterization: boolean = true) {
|
|
34
|
-
this.config = config;
|
|
35
|
-
this.enforceCharacterization = enforceCharacterization;
|
|
36
|
-
|
|
37
|
-
this.axiosInstance = axios.create();
|
|
38
|
-
this.axiosInstance.interceptors.response.use(
|
|
39
|
-
(response) => response,
|
|
40
|
-
async (error) => {
|
|
41
|
-
if (error.response?.status) {
|
|
42
|
-
if (this.loggerProvider) {
|
|
43
|
-
await this.loggerProvider.logError(
|
|
44
|
-
'Server Error',
|
|
45
|
-
error,
|
|
46
|
-
{
|
|
47
|
-
url: error.config.url,
|
|
48
|
-
method: error.config.method,
|
|
49
|
-
data: error.config.data,
|
|
50
|
-
status: error.response.status,
|
|
51
|
-
responseData: error.response.data
|
|
52
|
-
}
|
|
53
|
-
);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
return Promise.reject(error);
|
|
57
|
-
}
|
|
58
|
-
);
|
|
59
|
-
|
|
60
|
-
this.textToSpeech = this.textToSpeech.bind(this);
|
|
61
|
-
this.speechToText = this.speechToText.bind(this);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
static configure(
|
|
65
|
-
apiKey: string,
|
|
66
|
-
inferUrl: string,
|
|
67
|
-
streamUrl: string,
|
|
68
|
-
assistantId: string
|
|
69
|
-
): AlquimiaSDKConfig {
|
|
70
|
-
return {
|
|
71
|
-
apiKey,
|
|
72
|
-
chatUrl: `${inferUrl}/chat/${assistantId}`,
|
|
73
|
-
streamUrl: `${streamUrl}`,
|
|
74
|
-
assistantId: assistantId
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
widthConversationId(conversationId: string ): AlquimiaSDK {
|
|
79
|
-
this.conversationId = conversationId
|
|
80
|
-
return this;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
withWhisperProvider(provider: WhisperProvider): AlquimiaSDK {
|
|
84
|
-
this.whisperProvider = provider;
|
|
85
|
-
return this;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
withStableDiffusionProvider(provider: StableDiffusionProvider): AlquimiaSDK {
|
|
89
|
-
this.stableDiffusionProvider = provider
|
|
90
|
-
return this
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
withAnalyzeCharacterizationProvider(provider: CharacterizationProvider): AlquimiaSDK {
|
|
94
|
-
this.analyzeCharacterizationProvider = provider
|
|
95
|
-
return this
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
withRatingsProvider(provider: RatingsProvider): AlquimiaSDK {
|
|
99
|
-
this.ratingsProvider = provider
|
|
100
|
-
return this
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
withLoggerProvider(provider: LoggerProvider): AlquimiaSDK {
|
|
104
|
-
this.loggerProvider = provider
|
|
105
|
-
return this
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
getEnforceCharacterization(): boolean {
|
|
109
|
-
return this.enforceCharacterization ?? true;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
withTools(tools: any): AlquimiaSDK {
|
|
113
|
-
this.tools = tools;
|
|
114
|
-
return this;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
withExtraData(extraData: any): AlquimiaSDK {
|
|
118
|
-
this.extraData = extraData;
|
|
119
|
-
return this;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
withForceProfile(forceProfile: any): AlquimiaSDK {
|
|
123
|
-
this.forceProfile = forceProfile;
|
|
124
|
-
return this;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
textToSpeech(text: string): Promise<Blob> {
|
|
128
|
-
if (!this.whisperProvider) {
|
|
129
|
-
throw new Error("Whisper provider not initialized");
|
|
130
|
-
}
|
|
131
|
-
return this.whisperProvider.textToSpeech(text)
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
speechToText(audio: string): Promise<string> {
|
|
135
|
-
if (!this.whisperProvider) {
|
|
136
|
-
throw new Error("Whisper provider not initialized");
|
|
137
|
-
}
|
|
138
|
-
return this.whisperProvider.speechToText(audio)
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
async sendMessage(query: string, traceParent?: string) {
|
|
142
|
-
if (!this.conversationId) {
|
|
143
|
-
throw new Error("Conversation not initialized");
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
const initMessage = {
|
|
147
|
-
query,
|
|
148
|
-
session_id: this.conversationId,
|
|
149
|
-
tools: this.tools,
|
|
150
|
-
extra_data: this.extraData,
|
|
151
|
-
force_profile: this.forceProfile
|
|
152
|
-
};
|
|
153
|
-
|
|
154
|
-
const result = (await this.axiosInstance.post(this.config.chatUrl, initMessage, {
|
|
155
|
-
headers: {
|
|
156
|
-
"Content-Type": "application/json",
|
|
157
|
-
"x-trace-parent": traceParent || "",
|
|
158
|
-
},
|
|
159
|
-
})).data
|
|
160
|
-
|
|
161
|
-
this.streamId = result.data.stream_id;
|
|
162
|
-
return this
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
async generateImage(query: string) {
|
|
166
|
-
if (!this.stableDiffusionProvider) {
|
|
167
|
-
throw new Error("Stable Diffusion provider not initialized");
|
|
168
|
-
}
|
|
169
|
-
return this.stableDiffusionProvider.generateImage(query)
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
async analyzeCharacterization(text: string) {
|
|
173
|
-
if (!this.analyzeCharacterizationProvider) {
|
|
174
|
-
throw new Error("analyze characterization provider not initialized");
|
|
175
|
-
}
|
|
176
|
-
return this.analyzeCharacterizationProvider.analyzeCharacterization(text)
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
async rate(data: any) {
|
|
180
|
-
if (!this.ratingsProvider) {
|
|
181
|
-
throw new Error("ratings provider not initialized");
|
|
182
|
-
}
|
|
183
|
-
return this.ratingsProvider.rate(data)
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
async logInfo(message: string, data: any) {
|
|
187
|
-
if (!this.loggerProvider) {
|
|
188
|
-
throw new Error("logger provider not initialized");
|
|
189
|
-
}
|
|
190
|
-
return this.loggerProvider.logInfo(message, data)
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
async logError(message: string, error: Error, data: any) {
|
|
194
|
-
if (!this.loggerProvider) {
|
|
195
|
-
throw new Error("logger provider not initialized");
|
|
196
|
-
}
|
|
197
|
-
return this.loggerProvider.logError(message, error, data)
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
getUrlStream() {
|
|
201
|
-
return `${this.config.streamUrl}/${this.streamId}`;
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
export default AlquimiaSDK;
|
package/src/sdk/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { default as AlquimiaSDK } from './alquimia-sdk';
|
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
'use client'
|
|
2
|
-
|
|
3
|
-
import { useEffect } from 'react'
|
|
4
|
-
import { init as initApm, apm, Transaction } from '@elastic/apm-rum'
|
|
5
|
-
import { ApmSpan } from '../../types/type'
|
|
6
|
-
|
|
7
|
-
interface APMInitializerProps {
|
|
8
|
-
serviceName: string
|
|
9
|
-
serverUrl?: string
|
|
10
|
-
serverUrlPrefix?: string | undefined
|
|
11
|
-
logLevel?: 'trace' | 'debug' | 'info' | 'warn' | 'error'
|
|
12
|
-
environment: string
|
|
13
|
-
ignoreUrls?: Array<string | RegExp>
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
type LabelValue = string | number | boolean | null;
|
|
17
|
-
|
|
18
|
-
interface TransactionLabels {
|
|
19
|
-
[key: string]: LabelValue;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export function APMInitializer({
|
|
23
|
-
serviceName,
|
|
24
|
-
serverUrl,
|
|
25
|
-
serverUrlPrefix = undefined,
|
|
26
|
-
logLevel = 'warn',
|
|
27
|
-
environment,
|
|
28
|
-
ignoreUrls = []
|
|
29
|
-
}: APMInitializerProps) {
|
|
30
|
-
|
|
31
|
-
useEffect(() => {
|
|
32
|
-
const serviceUrl = !serverUrl ? (typeof window !== 'undefined' ? window.location.origin : '') : serverUrl
|
|
33
|
-
initApm({
|
|
34
|
-
serviceName: serviceName,
|
|
35
|
-
serverUrl: serviceUrl,
|
|
36
|
-
serverUrlPrefix: serverUrlPrefix,
|
|
37
|
-
serviceVersion: '1.0.0',
|
|
38
|
-
logLevel: logLevel,
|
|
39
|
-
environment: environment,
|
|
40
|
-
ignoreTransactions: ignoreUrls
|
|
41
|
-
})
|
|
42
|
-
}, [])
|
|
43
|
-
|
|
44
|
-
return null
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
export async function handleApmTransaction(
|
|
49
|
-
name: string,
|
|
50
|
-
type: string,
|
|
51
|
-
labels: TransactionLabels,
|
|
52
|
-
callback: () => Promise<void> | void
|
|
53
|
-
) {
|
|
54
|
-
const transaction = apm.startTransaction(name, type);
|
|
55
|
-
|
|
56
|
-
try {
|
|
57
|
-
transaction?.addLabels(labels as Labels);
|
|
58
|
-
await callback();
|
|
59
|
-
} catch (error) {
|
|
60
|
-
apm.captureError(error as Error);
|
|
61
|
-
} finally {
|
|
62
|
-
transaction?.end();
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export function traceError(
|
|
67
|
-
error: string | Error,
|
|
68
|
-
labels?: TransactionLabels,
|
|
69
|
-
customMessage?: string
|
|
70
|
-
) {
|
|
71
|
-
try {
|
|
72
|
-
const errorObject =
|
|
73
|
-
error instanceof Error
|
|
74
|
-
? error
|
|
75
|
-
: new Error(customMessage ?? String(error));
|
|
76
|
-
|
|
77
|
-
if (labels) {
|
|
78
|
-
apm.addLabels(labels as Labels);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
apm.captureError(errorObject);
|
|
82
|
-
} catch (e) {
|
|
83
|
-
throw new Error("Failed to capture error in APM:", e as Error);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
export const startManagedTransaction = (
|
|
88
|
-
name: string,
|
|
89
|
-
type: string = 'http-request',
|
|
90
|
-
spanName: string,
|
|
91
|
-
spanType: string = 'external.http'
|
|
92
|
-
): { transaction: Transaction | undefined, span: ApmSpan | undefined, traceParentId: string | undefined } => {
|
|
93
|
-
const transaction = apm.startTransaction(name, type, { managed: true });
|
|
94
|
-
const span: ApmSpan | undefined = transaction?.startSpan(spanName, spanType);
|
|
95
|
-
|
|
96
|
-
const traceParentId = span ? createTraceParentId(span.traceId || '', span.id || '') : undefined;
|
|
97
|
-
|
|
98
|
-
return { transaction, span, traceParentId };
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
const createTraceParentId = (traceId: string, spanId: string) => {
|
|
102
|
-
return `00-${traceId}-${spanId}-01`;
|
|
103
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./elastic-apm";
|
package/src/services/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './apm'
|
package/src/types/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './type'
|
package/src/types/type.ts
DELETED
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
import { Span } from "@elastic/apm-rum";
|
|
2
|
-
import { Message } from "ai";
|
|
3
|
-
export interface ITooler {
|
|
4
|
-
tooler: Array<{
|
|
5
|
-
name: string;
|
|
6
|
-
parameters: Record<string, any>;
|
|
7
|
-
}>;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export interface IToolSpec {
|
|
11
|
-
name: string;
|
|
12
|
-
parameters: Record<string, any>;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export abstract class ToolFactory {
|
|
16
|
-
protected toolSpec: IToolSpec | null = null;
|
|
17
|
-
|
|
18
|
-
constructor(toolSpec: IToolSpec) {
|
|
19
|
-
this.toolSpec = toolSpec;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
static createTool(toolSpec: IToolSpec): IToolSpec {
|
|
23
|
-
return toolSpec;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
abstract execute(): Promise<any>;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export interface AIMessageChunk {
|
|
30
|
-
type: string;
|
|
31
|
-
data?: AIMessageChunkData;
|
|
32
|
-
error_code?: string;
|
|
33
|
-
error_detail?: string;
|
|
34
|
-
loading?: boolean;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export interface AIMessageChunkData {
|
|
38
|
-
content: string;
|
|
39
|
-
additional_kwargs?: Record<string, unknown>;
|
|
40
|
-
response_metadata?: Record<string, unknown>;
|
|
41
|
-
type: string;
|
|
42
|
-
name?: string | null;
|
|
43
|
-
id?: string | null;
|
|
44
|
-
example?: boolean;
|
|
45
|
-
tool_calls?: ToolCall[];
|
|
46
|
-
invalid_tool_calls?: InvalidToolCall[];
|
|
47
|
-
usage_metadata?: any;
|
|
48
|
-
tool_call_chunks?: unknown[];
|
|
49
|
-
error_code?: string;
|
|
50
|
-
error_detail?: string;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
type ToolCall = {
|
|
54
|
-
name: string;
|
|
55
|
-
args: Record<string, any>;
|
|
56
|
-
id?: string | null;
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
type InvalidToolCall = {
|
|
60
|
-
name: string | null;
|
|
61
|
-
args: string | null;
|
|
62
|
-
id: string | null;
|
|
63
|
-
error: string | null;
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
export interface BaseAPIConfig {
|
|
67
|
-
baseUrl: string;
|
|
68
|
-
route: string;
|
|
69
|
-
token?: string;
|
|
70
|
-
headers?: Record<string, string>;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
export interface RatingData {
|
|
74
|
-
assistantId: string;
|
|
75
|
-
sessionId: string;
|
|
76
|
-
topicId: number;
|
|
77
|
-
score?: number;
|
|
78
|
-
description?: string;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
export type AlquimiaMessage = Message & {
|
|
82
|
-
error_code?: string;
|
|
83
|
-
error_detail?: string;
|
|
84
|
-
loading?: boolean;
|
|
85
|
-
created_at?: string;
|
|
86
|
-
additionalInfo?: string;
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
export interface ActionResponse<T = void> {
|
|
90
|
-
success: boolean;
|
|
91
|
-
data?: T;
|
|
92
|
-
error?: {
|
|
93
|
-
message: string;
|
|
94
|
-
code?: string;
|
|
95
|
-
details?: any;
|
|
96
|
-
name?: string;
|
|
97
|
-
cause?: string;
|
|
98
|
-
status?: string;
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
export type ApmSpan = Span & {
|
|
103
|
-
id?: string
|
|
104
|
-
traceId?: string
|
|
105
|
-
} | undefined
|
|
106
|
-
|
|
107
|
-
export type ApiError = Error & {
|
|
108
|
-
message: string;
|
|
109
|
-
name: string;
|
|
110
|
-
cause: string;
|
|
111
|
-
code?: string | number;
|
|
112
|
-
status?: string | number;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
export interface ConversationsMap {
|
|
116
|
-
[topicId: string]: string;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
export interface DocumentSearchResult {
|
|
120
|
-
pageContent: string;
|
|
121
|
-
metadata: {
|
|
122
|
-
source: string;
|
|
123
|
-
_id: string;
|
|
124
|
-
_collection_name: string;
|
|
125
|
-
};
|
|
126
|
-
score: number;
|
|
127
|
-
}
|
package/src/utils/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './utils';
|
package/src/utils/utils.ts
DELETED
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
import { BaseAPIConfig, ApiError, ConversationsMap } from "../types/type";
|
|
2
|
-
|
|
3
|
-
export function generateTranslatePrompt(text: string) {
|
|
4
|
-
const prompt = `
|
|
5
|
-
Arrange the following text according to the given object structure and return it as plain json, values summarized to one word, no formatting:
|
|
6
|
-
{
|
|
7
|
-
restrictions: {
|
|
8
|
-
value: [''],
|
|
9
|
-
description: 'Restrictions to certain foods'
|
|
10
|
-
},
|
|
11
|
-
alergies: {
|
|
12
|
-
value: [''],
|
|
13
|
-
description: 'Alergies to certain foods'
|
|
14
|
-
},
|
|
15
|
-
wine_preference: {
|
|
16
|
-
value: [''],
|
|
17
|
-
description: 'Preferences for the wine'
|
|
18
|
-
},
|
|
19
|
-
meal_preference: {
|
|
20
|
-
value: [''],
|
|
21
|
-
description: 'Preferences for the meal'
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
Text: "${text}"
|
|
26
|
-
|
|
27
|
-
Output:
|
|
28
|
-
`;
|
|
29
|
-
|
|
30
|
-
return prompt;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export function getCookies(name: string) {
|
|
34
|
-
if (typeof document === 'undefined') return undefined;
|
|
35
|
-
const value = `; ${document.cookie}`;
|
|
36
|
-
const parts = value.split(`; ${name}=`);
|
|
37
|
-
if (parts.length === 2) return parts.pop()?.split(";").shift();
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export function generateHeaders(config: BaseAPIConfig): HeadersInit {
|
|
41
|
-
const headers: HeadersInit = {
|
|
42
|
-
"Content-Type": "application/json",
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
if (config.token) {
|
|
46
|
-
headers["Authorization"] = `Bearer ${config.token}`;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
if (config.headers) {
|
|
50
|
-
Object.entries(config.headers).forEach(([key, value]) => {
|
|
51
|
-
headers[key] = value;
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
return headers;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export function isTextContent(buffer: ArrayBuffer): boolean {
|
|
59
|
-
try {
|
|
60
|
-
const text = new TextDecoder().decode(buffer);
|
|
61
|
-
return /^[\w\#\-\*]/.test(text.trim());
|
|
62
|
-
} catch {
|
|
63
|
-
return false;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export function getQueryParam(param: string): string | null {
|
|
68
|
-
if (typeof window !== 'undefined') {
|
|
69
|
-
const urlParams = new URLSearchParams(window.location.search);
|
|
70
|
-
return urlParams.get(param);
|
|
71
|
-
}
|
|
72
|
-
return null;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export function defineAssistantId(defaultId: string): string {
|
|
76
|
-
if (typeof window !== 'undefined' && window.location.pathname === '/') {
|
|
77
|
-
const queryAssistantId = getQueryParam('talkwith');
|
|
78
|
-
if (queryAssistantId) {
|
|
79
|
-
localStorage.setItem('assistantId', queryAssistantId);
|
|
80
|
-
return queryAssistantId;
|
|
81
|
-
}
|
|
82
|
-
const storedAssistantId = localStorage.getItem('assistantId');
|
|
83
|
-
return storedAssistantId || defaultId;
|
|
84
|
-
}
|
|
85
|
-
return defaultId;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
export function formatTimeWithUnit(timeMs: number): string {
|
|
89
|
-
if (timeMs >= 1000) {
|
|
90
|
-
return `${(timeMs / 1000).toFixed(1)}s`;
|
|
91
|
-
}
|
|
92
|
-
return `${Math.round(timeMs)}ms`;
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
export function serializeAxiosError(error: unknown){
|
|
96
|
-
if (error instanceof Error) {
|
|
97
|
-
const customError = error as ApiError;
|
|
98
|
-
return {
|
|
99
|
-
message: customError?.message,
|
|
100
|
-
name: customError?.name,
|
|
101
|
-
stack: customError?.stack,
|
|
102
|
-
code: customError?.code,
|
|
103
|
-
status: customError?.status,
|
|
104
|
-
};
|
|
105
|
-
}
|
|
106
|
-
return {
|
|
107
|
-
message: String(error),
|
|
108
|
-
name: 'Unknown Error',
|
|
109
|
-
};
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
export function parseConversationsMapCookie(cookieValue: string): ConversationsMap {
|
|
113
|
-
try {
|
|
114
|
-
const decodedValue = decodeURIComponent(cookieValue);
|
|
115
|
-
return JSON.parse(decodedValue) as ConversationsMap;
|
|
116
|
-
} catch (e) {
|
|
117
|
-
console.error('Error parsing conversations cookie:', e);
|
|
118
|
-
return {};
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
export function getTopicSessionId(topicId: string): string {
|
|
123
|
-
const conversationsStr = getCookies("alquimia-sessions") || "{}";
|
|
124
|
-
if (!conversationsStr) {
|
|
125
|
-
const sessionCookie = getCookies("alquimia-session");
|
|
126
|
-
return sessionCookie || "";
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
const conversationsMap = parseConversationsMapCookie(conversationsStr);
|
|
130
|
-
return conversationsMap[topicId] || "";
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
export function createMessageId() {
|
|
134
|
-
return Math.floor(Math.random() * 1000000).toString();
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
|