@alquimia-ai/tools 1.0.1 → 1.0.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/actions/index.d.mts +22 -0
- package/dist/actions/index.d.ts +22 -0
- package/dist/actions/index.js +228 -0
- package/dist/actions/index.js.map +1 -0
- package/dist/actions/index.mjs +206 -0
- package/dist/actions/index.mjs.map +1 -0
- package/dist/hooks/index.d.mts +53 -0
- package/dist/hooks/index.d.ts +53 -0
- package/dist/hooks/index.js +302 -0
- package/dist/hooks/index.js.map +1 -0
- package/dist/hooks/index.mjs +279 -0
- package/dist/hooks/index.mjs.map +1 -0
- package/dist/providers/index.d.mts +74 -0
- package/dist/providers/index.d.ts +74 -0
- package/dist/providers/index.js +2183 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/providers/index.mjs +2173 -0
- package/dist/providers/index.mjs.map +1 -0
- package/dist/providers-D6FJ6tlA.d.mts +33 -0
- package/dist/providers-D6FJ6tlA.d.ts +33 -0
- package/dist/sdk/index.d.mts +47 -0
- package/dist/sdk/index.d.ts +47 -0
- package/dist/sdk/index.js +188 -0
- package/dist/sdk/index.js.map +1 -0
- package/dist/sdk/index.mjs +155 -0
- package/dist/sdk/index.mjs.map +1 -0
- package/dist/services/index.d.mts +26 -0
- package/dist/services/index.d.ts +26 -0
- package/dist/services/index.js +86 -0
- package/dist/services/index.js.map +1 -0
- package/dist/services/index.mjs +64 -0
- package/dist/services/index.mjs.map +1 -0
- package/dist/types/index.d.mts +105 -0
- package/dist/types/index.d.ts +105 -0
- package/dist/types/index.js +37 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/index.mjs +14 -0
- package/dist/types/index.mjs.map +1 -0
- package/dist/utils/index.d.mts +29 -0
- package/dist/utils/index.d.ts +29 -0
- package/dist/utils/index.js +156 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/index.mjs +133 -0
- package/dist/utils/index.mjs.map +1 -0
- package/package.json +38 -20
- package/dist/index.d.mts +0 -322
- package/dist/index.d.ts +0 -322
- package/dist/index.js +0 -2582
- package/dist/index.js.map +0 -1
- package/dist/index.mjs +0 -2336
- package/dist/index.mjs.map +0 -1
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/sdk/index.ts
|
|
31
|
+
var sdk_exports = {};
|
|
32
|
+
__export(sdk_exports, {
|
|
33
|
+
AlquimiaSDK: () => alquimia_sdk_default
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(sdk_exports);
|
|
36
|
+
|
|
37
|
+
// src/sdk/alquimia-sdk.ts
|
|
38
|
+
var import_axios = __toESM(require("axios"));
|
|
39
|
+
var AlquimiaSDK = class {
|
|
40
|
+
constructor(config, enforceCharacterization = true) {
|
|
41
|
+
this.conversationId = null;
|
|
42
|
+
this.sessionId = null;
|
|
43
|
+
this.streamId = null;
|
|
44
|
+
this.tools = [];
|
|
45
|
+
this.extraData = {};
|
|
46
|
+
this.forceProfile = {};
|
|
47
|
+
this.config = config;
|
|
48
|
+
this.enforceCharacterization = enforceCharacterization;
|
|
49
|
+
this.axiosInstance = import_axios.default.create();
|
|
50
|
+
this.axiosInstance.interceptors.response.use(
|
|
51
|
+
(response) => response,
|
|
52
|
+
async (error) => {
|
|
53
|
+
if (error.response?.status) {
|
|
54
|
+
if (this.loggerProvider) {
|
|
55
|
+
await this.loggerProvider.logError(
|
|
56
|
+
"Server Error",
|
|
57
|
+
error,
|
|
58
|
+
{
|
|
59
|
+
url: error.config.url,
|
|
60
|
+
method: error.config.method,
|
|
61
|
+
data: error.config.data,
|
|
62
|
+
status: error.response.status,
|
|
63
|
+
responseData: error.response.data
|
|
64
|
+
}
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return Promise.reject(error);
|
|
69
|
+
}
|
|
70
|
+
);
|
|
71
|
+
this.textToSpeech = this.textToSpeech.bind(this);
|
|
72
|
+
this.speechToText = this.speechToText.bind(this);
|
|
73
|
+
}
|
|
74
|
+
static configure(apiKey, inferUrl, streamUrl, assistantId) {
|
|
75
|
+
return {
|
|
76
|
+
apiKey,
|
|
77
|
+
chatUrl: `${inferUrl}/chat/${assistantId}`,
|
|
78
|
+
streamUrl: `${streamUrl}/${assistantId}`,
|
|
79
|
+
assistantId
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
widthConversationId(conversationId) {
|
|
83
|
+
this.conversationId = conversationId;
|
|
84
|
+
return this;
|
|
85
|
+
}
|
|
86
|
+
withWhisperProvider(provider) {
|
|
87
|
+
this.whisperProvider = provider;
|
|
88
|
+
return this;
|
|
89
|
+
}
|
|
90
|
+
withStableDiffusionProvider(provider) {
|
|
91
|
+
this.stableDiffusionProvider = provider;
|
|
92
|
+
return this;
|
|
93
|
+
}
|
|
94
|
+
withAnalyzeCharacterizationProvider(provider) {
|
|
95
|
+
this.analyzeCharacterizationProvider = provider;
|
|
96
|
+
return this;
|
|
97
|
+
}
|
|
98
|
+
withRatingsProvider(provider) {
|
|
99
|
+
this.ratingsProvider = provider;
|
|
100
|
+
return this;
|
|
101
|
+
}
|
|
102
|
+
withLoggerProvider(provider) {
|
|
103
|
+
this.loggerProvider = provider;
|
|
104
|
+
return this;
|
|
105
|
+
}
|
|
106
|
+
getEnforceCharacterization() {
|
|
107
|
+
return this.enforceCharacterization ?? true;
|
|
108
|
+
}
|
|
109
|
+
withTools(tools) {
|
|
110
|
+
this.tools = tools;
|
|
111
|
+
return this;
|
|
112
|
+
}
|
|
113
|
+
withExtraData(extraData) {
|
|
114
|
+
this.extraData = extraData;
|
|
115
|
+
return this;
|
|
116
|
+
}
|
|
117
|
+
withForceProfile(forceProfile) {
|
|
118
|
+
this.forceProfile = forceProfile;
|
|
119
|
+
return this;
|
|
120
|
+
}
|
|
121
|
+
textToSpeech(text) {
|
|
122
|
+
if (!this.whisperProvider) {
|
|
123
|
+
throw new Error("Whisper provider not initialized");
|
|
124
|
+
}
|
|
125
|
+
return this.whisperProvider.textToSpeech(text);
|
|
126
|
+
}
|
|
127
|
+
speechToText(audio) {
|
|
128
|
+
if (!this.whisperProvider) {
|
|
129
|
+
throw new Error("Whisper provider not initialized");
|
|
130
|
+
}
|
|
131
|
+
return this.whisperProvider.speechToText(audio);
|
|
132
|
+
}
|
|
133
|
+
async sendMessage(query, traceParent) {
|
|
134
|
+
if (!this.conversationId) {
|
|
135
|
+
throw new Error("Conversation not initialized");
|
|
136
|
+
}
|
|
137
|
+
const initMessage = {
|
|
138
|
+
query,
|
|
139
|
+
session_id: this.conversationId,
|
|
140
|
+
tools: this.tools,
|
|
141
|
+
extra_data: this.extraData,
|
|
142
|
+
force_profile: this.forceProfile
|
|
143
|
+
};
|
|
144
|
+
const result = (await this.axiosInstance.post(this.config.chatUrl, initMessage, {
|
|
145
|
+
headers: {
|
|
146
|
+
"Content-Type": "application/json",
|
|
147
|
+
"x-trace-parent": traceParent || ""
|
|
148
|
+
}
|
|
149
|
+
})).data;
|
|
150
|
+
this.streamId = result.data.stream_id;
|
|
151
|
+
return this;
|
|
152
|
+
}
|
|
153
|
+
async generateImage(query) {
|
|
154
|
+
if (!this.stableDiffusionProvider) {
|
|
155
|
+
throw new Error("Stable Diffusion provider not initialized");
|
|
156
|
+
}
|
|
157
|
+
return this.stableDiffusionProvider.generateImage(query);
|
|
158
|
+
}
|
|
159
|
+
async analyzeCharacterization(text) {
|
|
160
|
+
if (!this.analyzeCharacterizationProvider) {
|
|
161
|
+
throw new Error("analyze characterization provider not initialized");
|
|
162
|
+
}
|
|
163
|
+
return this.analyzeCharacterizationProvider.analyzeCharacterization(text);
|
|
164
|
+
}
|
|
165
|
+
async rate(data) {
|
|
166
|
+
if (!this.ratingsProvider) {
|
|
167
|
+
throw new Error("ratings provider not initialized");
|
|
168
|
+
}
|
|
169
|
+
return this.ratingsProvider.rate(data);
|
|
170
|
+
}
|
|
171
|
+
async logInfo(message, data) {
|
|
172
|
+
if (!this.loggerProvider) {
|
|
173
|
+
throw new Error("logger provider not initialized");
|
|
174
|
+
}
|
|
175
|
+
return this.loggerProvider.logInfo(message, data);
|
|
176
|
+
}
|
|
177
|
+
async logError(message, error, data) {
|
|
178
|
+
if (!this.loggerProvider) {
|
|
179
|
+
throw new Error("logger provider not initialized");
|
|
180
|
+
}
|
|
181
|
+
return this.loggerProvider.logError(message, error, data);
|
|
182
|
+
}
|
|
183
|
+
getUrlStream() {
|
|
184
|
+
return `${this.config.streamUrl}/${this.streamId}`;
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
var alquimia_sdk_default = AlquimiaSDK;
|
|
188
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/sdk/index.ts","../../src/sdk/alquimia-sdk.ts"],"sourcesContent":["export { default as AlquimiaSDK } from './alquimia-sdk';","import axios, { AxiosInstance } from \"axios\";\nimport {\n StableDiffusionProvider,\n WhisperProvider,\n CharacterizationProvider,\n RatingsProvider,\n LoggerProvider,\n} from \"../providers\";\ninterface AlquimiaSDKConfig {\n apiKey: string;\n chatUrl: string;\n streamUrl: string;\n assistantId: string;\n}\n\n\nclass AlquimiaSDK {\n private config: AlquimiaSDKConfig;\n private axiosInstance: AxiosInstance;\n private conversationId: string | null = null;\n private sessionId: string | null = null;\n private streamId: string | null = null;\n private tools: any[] = [];\n private extraData: any = {};\n private forceProfile: any = {};\n private whisperProvider?: WhisperProvider\n private stableDiffusionProvider?: StableDiffusionProvider\n private analyzeCharacterizationProvider?: CharacterizationProvider\n private ratingsProvider?: RatingsProvider\n private loggerProvider?: LoggerProvider\n private enforceCharacterization?: boolean\n \n constructor(config: AlquimiaSDKConfig, enforceCharacterization: boolean = true) {\n this.config = config;\n this.enforceCharacterization = enforceCharacterization;\n\n this.axiosInstance = axios.create();\n this.axiosInstance.interceptors.response.use(\n (response) => response,\n async (error) => {\n if (error.response?.status) {\n if (this.loggerProvider) {\n await this.loggerProvider.logError(\n 'Server Error',\n error,\n {\n url: error.config.url,\n method: error.config.method,\n data: error.config.data,\n status: error.response.status,\n responseData: error.response.data\n }\n );\n }\n }\n return Promise.reject(error);\n }\n );\n \n this.textToSpeech = this.textToSpeech.bind(this);\n this.speechToText = this.speechToText.bind(this);\n }\n\n static configure(\n apiKey: string,\n inferUrl: string,\n streamUrl: string,\n assistantId: string\n ): AlquimiaSDKConfig {\n return {\n apiKey,\n chatUrl: `${inferUrl}/chat/${assistantId}`,\n streamUrl: `${streamUrl}/${assistantId}`,\n assistantId: assistantId\n };\n }\n\n widthConversationId(conversationId: string ): AlquimiaSDK {\n this.conversationId = conversationId \n return this;\n }\n\n withWhisperProvider(provider: WhisperProvider): AlquimiaSDK {\n this.whisperProvider = provider;\n return this;\n }\n\n withStableDiffusionProvider(provider: StableDiffusionProvider): AlquimiaSDK {\n this.stableDiffusionProvider = provider\n return this\n }\n\n withAnalyzeCharacterizationProvider(provider: CharacterizationProvider): AlquimiaSDK {\n this.analyzeCharacterizationProvider = provider\n return this\n }\n\n withRatingsProvider(provider: RatingsProvider): AlquimiaSDK {\n this.ratingsProvider = provider\n return this\n }\n\n withLoggerProvider(provider: LoggerProvider): AlquimiaSDK {\n this.loggerProvider = provider\n return this\n }\n\n getEnforceCharacterization(): boolean {\n return this.enforceCharacterization ?? true;\n }\n\n withTools(tools: any[]): AlquimiaSDK {\n this.tools = tools;\n return this;\n }\n\n withExtraData(extraData: any): AlquimiaSDK {\n this.extraData = extraData;\n return this;\n }\n\n withForceProfile(forceProfile: any): AlquimiaSDK {\n this.forceProfile = forceProfile;\n return this;\n }\n\n textToSpeech(text: string): Promise<Blob> {\n if (!this.whisperProvider) {\n throw new Error(\"Whisper provider not initialized\");\n }\n return this.whisperProvider.textToSpeech(text)\n }\n\n speechToText(audio: string): Promise<string> {\n if (!this.whisperProvider) {\n throw new Error(\"Whisper provider not initialized\");\n }\n return this.whisperProvider.speechToText(audio)\n }\n\n async sendMessage(query: string, traceParent?: string) {\n if (!this.conversationId) {\n throw new Error(\"Conversation not initialized\");\n }\n\n const initMessage = {\n query,\n session_id: this.conversationId,\n tools: this.tools,\n extra_data: this.extraData,\n force_profile: this.forceProfile\n };\n \n const result = (await this.axiosInstance.post(this.config.chatUrl, initMessage, {\n headers: {\n \"Content-Type\": \"application/json\",\n \"x-trace-parent\": traceParent || \"\",\n },\n })).data\n \n this.streamId = result.data.stream_id;\n return this\n }\n\n async generateImage(query: string) {\n if (!this.stableDiffusionProvider) {\n throw new Error(\"Stable Diffusion provider not initialized\");\n }\n return this.stableDiffusionProvider.generateImage(query)\n }\n\n async analyzeCharacterization(text: string) {\n if (!this.analyzeCharacterizationProvider) {\n throw new Error(\"analyze characterization provider not initialized\");\n }\n return this.analyzeCharacterizationProvider.analyzeCharacterization(text)\n }\n\n async rate(data: any) {\n if (!this.ratingsProvider) {\n throw new Error(\"ratings provider not initialized\");\n }\n return this.ratingsProvider.rate(data)\n }\n\n async logInfo(message: string, data: any) {\n if (!this.loggerProvider) {\n throw new Error(\"logger provider not initialized\");\n }\n return this.loggerProvider.logInfo(message, data)\n }\n\n async logError(message: string, error: Error, data: any) {\n if (!this.loggerProvider) {\n throw new Error(\"logger provider not initialized\");\n }\n return this.loggerProvider.logError(message, error, data)\n }\n\n getUrlStream() {\n return `${this.config.streamUrl}/${this.streamId}`;\n }\n}\n\nexport default AlquimiaSDK;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAAqC;AAgBrC,IAAM,cAAN,MAAkB;AAAA,EAgBhB,YAAY,QAA2B,0BAAmC,MAAM;AAbhF,SAAQ,iBAAgC;AACxC,SAAQ,YAA2B;AACnC,SAAQ,WAA0B;AAClC,SAAQ,QAAe,CAAC;AACxB,SAAQ,YAAiB,CAAC;AAC1B,SAAQ,eAAoB,CAAC;AAS3B,SAAK,SAAS;AACd,SAAK,0BAA0B;AAE/B,SAAK,gBAAgB,aAAAA,QAAM,OAAO;AAClC,SAAK,cAAc,aAAa,SAAS;AAAA,MACvC,CAAC,aAAa;AAAA,MACd,OAAO,UAAU;AACf,YAAI,MAAM,UAAU,QAAQ;AAC1B,cAAI,KAAK,gBAAgB;AACvB,kBAAM,KAAK,eAAe;AAAA,cACxB;AAAA,cACA;AAAA,cACA;AAAA,gBACE,KAAK,MAAM,OAAO;AAAA,gBACl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,QAAQ,SAAiB,MAAW;AACxC,QAAI,CAAC,KAAK,gBAAgB;AACxB,YAAM,IAAI,MAAM,iCAAiC;AAAA,IACnD;AACA,WAAO,KAAK,eAAe,QAAQ,SAAS,IAAI;AAAA,EAClD;AAAA,EAEA,MAAM,SAAS,SAAiB,OAAc,MAAW;AACvD,QAAI,CAAC,KAAK,gBAAgB;AACxB,YAAM,IAAI,MAAM,iCAAiC;AAAA,IACnD;AACA,WAAO,KAAK,eAAe,SAAS,SAAS,OAAO,IAAI;AAAA,EAC1D;AAAA,EAEA,eAAe;AACb,WAAO,GAAG,KAAK,OAAO,SAAS,IAAI,KAAK,QAAQ;AAAA,EAClD;AACF;AAEA,IAAO,uBAAQ;","names":["axios"]}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
// src/sdk/alquimia-sdk.ts
|
|
2
|
+
import axios from "axios";
|
|
3
|
+
var AlquimiaSDK = class {
|
|
4
|
+
constructor(config, enforceCharacterization = true) {
|
|
5
|
+
this.conversationId = null;
|
|
6
|
+
this.sessionId = null;
|
|
7
|
+
this.streamId = null;
|
|
8
|
+
this.tools = [];
|
|
9
|
+
this.extraData = {};
|
|
10
|
+
this.forceProfile = {};
|
|
11
|
+
this.config = config;
|
|
12
|
+
this.enforceCharacterization = enforceCharacterization;
|
|
13
|
+
this.axiosInstance = axios.create();
|
|
14
|
+
this.axiosInstance.interceptors.response.use(
|
|
15
|
+
(response) => response,
|
|
16
|
+
async (error) => {
|
|
17
|
+
if (error.response?.status) {
|
|
18
|
+
if (this.loggerProvider) {
|
|
19
|
+
await this.loggerProvider.logError(
|
|
20
|
+
"Server Error",
|
|
21
|
+
error,
|
|
22
|
+
{
|
|
23
|
+
url: error.config.url,
|
|
24
|
+
method: error.config.method,
|
|
25
|
+
data: error.config.data,
|
|
26
|
+
status: error.response.status,
|
|
27
|
+
responseData: error.response.data
|
|
28
|
+
}
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return Promise.reject(error);
|
|
33
|
+
}
|
|
34
|
+
);
|
|
35
|
+
this.textToSpeech = this.textToSpeech.bind(this);
|
|
36
|
+
this.speechToText = this.speechToText.bind(this);
|
|
37
|
+
}
|
|
38
|
+
static configure(apiKey, inferUrl, streamUrl, assistantId) {
|
|
39
|
+
return {
|
|
40
|
+
apiKey,
|
|
41
|
+
chatUrl: `${inferUrl}/chat/${assistantId}`,
|
|
42
|
+
streamUrl: `${streamUrl}/${assistantId}`,
|
|
43
|
+
assistantId
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
widthConversationId(conversationId) {
|
|
47
|
+
this.conversationId = conversationId;
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
withWhisperProvider(provider) {
|
|
51
|
+
this.whisperProvider = provider;
|
|
52
|
+
return this;
|
|
53
|
+
}
|
|
54
|
+
withStableDiffusionProvider(provider) {
|
|
55
|
+
this.stableDiffusionProvider = provider;
|
|
56
|
+
return this;
|
|
57
|
+
}
|
|
58
|
+
withAnalyzeCharacterizationProvider(provider) {
|
|
59
|
+
this.analyzeCharacterizationProvider = provider;
|
|
60
|
+
return this;
|
|
61
|
+
}
|
|
62
|
+
withRatingsProvider(provider) {
|
|
63
|
+
this.ratingsProvider = provider;
|
|
64
|
+
return this;
|
|
65
|
+
}
|
|
66
|
+
withLoggerProvider(provider) {
|
|
67
|
+
this.loggerProvider = provider;
|
|
68
|
+
return this;
|
|
69
|
+
}
|
|
70
|
+
getEnforceCharacterization() {
|
|
71
|
+
return this.enforceCharacterization ?? true;
|
|
72
|
+
}
|
|
73
|
+
withTools(tools) {
|
|
74
|
+
this.tools = tools;
|
|
75
|
+
return this;
|
|
76
|
+
}
|
|
77
|
+
withExtraData(extraData) {
|
|
78
|
+
this.extraData = extraData;
|
|
79
|
+
return this;
|
|
80
|
+
}
|
|
81
|
+
withForceProfile(forceProfile) {
|
|
82
|
+
this.forceProfile = forceProfile;
|
|
83
|
+
return this;
|
|
84
|
+
}
|
|
85
|
+
textToSpeech(text) {
|
|
86
|
+
if (!this.whisperProvider) {
|
|
87
|
+
throw new Error("Whisper provider not initialized");
|
|
88
|
+
}
|
|
89
|
+
return this.whisperProvider.textToSpeech(text);
|
|
90
|
+
}
|
|
91
|
+
speechToText(audio) {
|
|
92
|
+
if (!this.whisperProvider) {
|
|
93
|
+
throw new Error("Whisper provider not initialized");
|
|
94
|
+
}
|
|
95
|
+
return this.whisperProvider.speechToText(audio);
|
|
96
|
+
}
|
|
97
|
+
async sendMessage(query, traceParent) {
|
|
98
|
+
if (!this.conversationId) {
|
|
99
|
+
throw new Error("Conversation not initialized");
|
|
100
|
+
}
|
|
101
|
+
const initMessage = {
|
|
102
|
+
query,
|
|
103
|
+
session_id: this.conversationId,
|
|
104
|
+
tools: this.tools,
|
|
105
|
+
extra_data: this.extraData,
|
|
106
|
+
force_profile: this.forceProfile
|
|
107
|
+
};
|
|
108
|
+
const result = (await this.axiosInstance.post(this.config.chatUrl, initMessage, {
|
|
109
|
+
headers: {
|
|
110
|
+
"Content-Type": "application/json",
|
|
111
|
+
"x-trace-parent": traceParent || ""
|
|
112
|
+
}
|
|
113
|
+
})).data;
|
|
114
|
+
this.streamId = result.data.stream_id;
|
|
115
|
+
return this;
|
|
116
|
+
}
|
|
117
|
+
async generateImage(query) {
|
|
118
|
+
if (!this.stableDiffusionProvider) {
|
|
119
|
+
throw new Error("Stable Diffusion provider not initialized");
|
|
120
|
+
}
|
|
121
|
+
return this.stableDiffusionProvider.generateImage(query);
|
|
122
|
+
}
|
|
123
|
+
async analyzeCharacterization(text) {
|
|
124
|
+
if (!this.analyzeCharacterizationProvider) {
|
|
125
|
+
throw new Error("analyze characterization provider not initialized");
|
|
126
|
+
}
|
|
127
|
+
return this.analyzeCharacterizationProvider.analyzeCharacterization(text);
|
|
128
|
+
}
|
|
129
|
+
async rate(data) {
|
|
130
|
+
if (!this.ratingsProvider) {
|
|
131
|
+
throw new Error("ratings provider not initialized");
|
|
132
|
+
}
|
|
133
|
+
return this.ratingsProvider.rate(data);
|
|
134
|
+
}
|
|
135
|
+
async logInfo(message, data) {
|
|
136
|
+
if (!this.loggerProvider) {
|
|
137
|
+
throw new Error("logger provider not initialized");
|
|
138
|
+
}
|
|
139
|
+
return this.loggerProvider.logInfo(message, data);
|
|
140
|
+
}
|
|
141
|
+
async logError(message, error, data) {
|
|
142
|
+
if (!this.loggerProvider) {
|
|
143
|
+
throw new Error("logger provider not initialized");
|
|
144
|
+
}
|
|
145
|
+
return this.loggerProvider.logError(message, error, data);
|
|
146
|
+
}
|
|
147
|
+
getUrlStream() {
|
|
148
|
+
return `${this.config.streamUrl}/${this.streamId}`;
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
var alquimia_sdk_default = AlquimiaSDK;
|
|
152
|
+
export {
|
|
153
|
+
alquimia_sdk_default as AlquimiaSDK
|
|
154
|
+
};
|
|
155
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/sdk/alquimia-sdk.ts"],"sourcesContent":["import axios, { AxiosInstance } from \"axios\";\nimport {\n StableDiffusionProvider,\n WhisperProvider,\n CharacterizationProvider,\n RatingsProvider,\n LoggerProvider,\n} from \"../providers\";\ninterface AlquimiaSDKConfig {\n apiKey: string;\n chatUrl: string;\n streamUrl: string;\n assistantId: string;\n}\n\n\nclass AlquimiaSDK {\n private config: AlquimiaSDKConfig;\n private axiosInstance: AxiosInstance;\n private conversationId: string | null = null;\n private sessionId: string | null = null;\n private streamId: string | null = null;\n private tools: any[] = [];\n private extraData: any = {};\n private forceProfile: any = {};\n private whisperProvider?: WhisperProvider\n private stableDiffusionProvider?: StableDiffusionProvider\n private analyzeCharacterizationProvider?: CharacterizationProvider\n private ratingsProvider?: RatingsProvider\n private loggerProvider?: LoggerProvider\n private enforceCharacterization?: boolean\n \n constructor(config: AlquimiaSDKConfig, enforceCharacterization: boolean = true) {\n this.config = config;\n this.enforceCharacterization = enforceCharacterization;\n\n this.axiosInstance = axios.create();\n this.axiosInstance.interceptors.response.use(\n (response) => response,\n async (error) => {\n if (error.response?.status) {\n if (this.loggerProvider) {\n await this.loggerProvider.logError(\n 'Server Error',\n error,\n {\n url: error.config.url,\n method: error.config.method,\n data: error.config.data,\n status: error.response.status,\n responseData: error.response.data\n }\n );\n }\n }\n return Promise.reject(error);\n }\n );\n \n this.textToSpeech = this.textToSpeech.bind(this);\n this.speechToText = this.speechToText.bind(this);\n }\n\n static configure(\n apiKey: string,\n inferUrl: string,\n streamUrl: string,\n assistantId: string\n ): AlquimiaSDKConfig {\n return {\n apiKey,\n chatUrl: `${inferUrl}/chat/${assistantId}`,\n streamUrl: `${streamUrl}/${assistantId}`,\n assistantId: assistantId\n };\n }\n\n widthConversationId(conversationId: string ): AlquimiaSDK {\n this.conversationId = conversationId \n return this;\n }\n\n withWhisperProvider(provider: WhisperProvider): AlquimiaSDK {\n this.whisperProvider = provider;\n return this;\n }\n\n withStableDiffusionProvider(provider: StableDiffusionProvider): AlquimiaSDK {\n this.stableDiffusionProvider = provider\n return this\n }\n\n withAnalyzeCharacterizationProvider(provider: CharacterizationProvider): AlquimiaSDK {\n this.analyzeCharacterizationProvider = provider\n return this\n }\n\n withRatingsProvider(provider: RatingsProvider): AlquimiaSDK {\n this.ratingsProvider = provider\n return this\n }\n\n withLoggerProvider(provider: LoggerProvider): AlquimiaSDK {\n this.loggerProvider = provider\n return this\n }\n\n getEnforceCharacterization(): boolean {\n return this.enforceCharacterization ?? true;\n }\n\n withTools(tools: any[]): AlquimiaSDK {\n this.tools = tools;\n return this;\n }\n\n withExtraData(extraData: any): AlquimiaSDK {\n this.extraData = extraData;\n return this;\n }\n\n withForceProfile(forceProfile: any): AlquimiaSDK {\n this.forceProfile = forceProfile;\n return this;\n }\n\n textToSpeech(text: string): Promise<Blob> {\n if (!this.whisperProvider) {\n throw new Error(\"Whisper provider not initialized\");\n }\n return this.whisperProvider.textToSpeech(text)\n }\n\n speechToText(audio: string): Promise<string> {\n if (!this.whisperProvider) {\n throw new Error(\"Whisper provider not initialized\");\n }\n return this.whisperProvider.speechToText(audio)\n }\n\n async sendMessage(query: string, traceParent?: string) {\n if (!this.conversationId) {\n throw new Error(\"Conversation not initialized\");\n }\n\n const initMessage = {\n query,\n session_id: this.conversationId,\n tools: this.tools,\n extra_data: this.extraData,\n force_profile: this.forceProfile\n };\n \n const result = (await this.axiosInstance.post(this.config.chatUrl, initMessage, {\n headers: {\n \"Content-Type\": \"application/json\",\n \"x-trace-parent\": traceParent || \"\",\n },\n })).data\n \n this.streamId = result.data.stream_id;\n return this\n }\n\n async generateImage(query: string) {\n if (!this.stableDiffusionProvider) {\n throw new Error(\"Stable Diffusion provider not initialized\");\n }\n return this.stableDiffusionProvider.generateImage(query)\n }\n\n async analyzeCharacterization(text: string) {\n if (!this.analyzeCharacterizationProvider) {\n throw new Error(\"analyze characterization provider not initialized\");\n }\n return this.analyzeCharacterizationProvider.analyzeCharacterization(text)\n }\n\n async rate(data: any) {\n if (!this.ratingsProvider) {\n throw new Error(\"ratings provider not initialized\");\n }\n return this.ratingsProvider.rate(data)\n }\n\n async logInfo(message: string, data: any) {\n if (!this.loggerProvider) {\n throw new Error(\"logger provider not initialized\");\n }\n return this.loggerProvider.logInfo(message, data)\n }\n\n async logError(message: string, error: Error, data: any) {\n if (!this.loggerProvider) {\n throw new Error(\"logger provider not initialized\");\n }\n return this.loggerProvider.logError(message, error, data)\n }\n\n getUrlStream() {\n return `${this.config.streamUrl}/${this.streamId}`;\n }\n}\n\nexport default AlquimiaSDK;\n"],"mappings":";AAAA,OAAO,WAA8B;AAgBrC,IAAM,cAAN,MAAkB;AAAA,EAgBhB,YAAY,QAA2B,0BAAmC,MAAM;AAbhF,SAAQ,iBAAgC;AACxC,SAAQ,YAA2B;AACnC,SAAQ,WAA0B;AAClC,SAAQ,QAAe,CAAC;AACxB,SAAQ,YAAiB,CAAC;AAC1B,SAAQ,eAAoB,CAAC;AAS3B,SAAK,SAAS;AACd,SAAK,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AAiC;AACzC,YAAM,IAAI,MAAM,mDAAmD;AAAA,IACrE;AACA,WAAO,KAAK,gCAAgC,wBAAwB,IAAI;AAAA,EAC1E;AAAA,EAEA,MAAM,KAAK,MAAW;AACpB,QAAI,CAAC,KAAK,iBAAiB;AACzB,YAAM,IAAI,MAAM,kCAAkC;AAAA,IACpD;AACA,WAAO,KAAK,gBAAgB,KAAK,IAAI;AAAA,EACvC;AAAA,EAEA,MAAM,QAAQ,SAAiB,MAAW;AACxC,QAAI,CAAC,KAAK,gBAAgB;AACxB,YAAM,IAAI,MAAM,iCAAiC;AAAA,IACnD;AACA,WAAO,KAAK,eAAe,QAAQ,SAAS,IAAI;AAAA,EAClD;AAAA,EAEA,MAAM,SAAS,SAAiB,OAAc,MAAW;AACvD,QAAI,CAAC,KAAK,gBAAgB;AACxB,YAAM,IAAI,MAAM,iCAAiC;AAAA,IACnD;AACA,WAAO,KAAK,eAAe,SAAS,SAAS,OAAO,IAAI;AAAA,EAC1D;AAAA,EAEA,eAAe;AACb,WAAO,GAAG,KAAK,OAAO,SAAS,IAAI,KAAK,QAAQ;AAAA,EAClD;AACF;AAEA,IAAO,uBAAQ;","names":[]}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Transaction } from '@elastic/apm-rum';
|
|
2
|
+
import { ApmSpan } from '../types/index.mjs';
|
|
3
|
+
import 'ai';
|
|
4
|
+
|
|
5
|
+
interface APMInitializerProps {
|
|
6
|
+
serviceName: string;
|
|
7
|
+
serverUrl?: string;
|
|
8
|
+
serverUrlPrefix?: string | undefined;
|
|
9
|
+
logLevel?: 'trace' | 'debug' | 'info' | 'warn' | 'error';
|
|
10
|
+
environment: string;
|
|
11
|
+
ignoreUrls?: Array<string | RegExp>;
|
|
12
|
+
}
|
|
13
|
+
type LabelValue = string | number | boolean | null;
|
|
14
|
+
interface TransactionLabels {
|
|
15
|
+
[key: string]: LabelValue;
|
|
16
|
+
}
|
|
17
|
+
declare function APMInitializer({ serviceName, serverUrl, serverUrlPrefix, logLevel, environment, ignoreUrls }: APMInitializerProps): null;
|
|
18
|
+
declare function handleApmTransaction(name: string, type: string, labels: TransactionLabels, callback: () => Promise<void> | void): Promise<void>;
|
|
19
|
+
declare function traceError(error: string | Error, labels?: TransactionLabels, customMessage?: string): void;
|
|
20
|
+
declare const startManagedTransaction: (name: string, type: string | undefined, spanName: string, spanType?: string) => {
|
|
21
|
+
transaction: Transaction | undefined;
|
|
22
|
+
span: ApmSpan | undefined;
|
|
23
|
+
traceParentId: string | undefined;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export { APMInitializer, handleApmTransaction, startManagedTransaction, traceError };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Transaction } from '@elastic/apm-rum';
|
|
2
|
+
import { ApmSpan } from '../types/index.js';
|
|
3
|
+
import 'ai';
|
|
4
|
+
|
|
5
|
+
interface APMInitializerProps {
|
|
6
|
+
serviceName: string;
|
|
7
|
+
serverUrl?: string;
|
|
8
|
+
serverUrlPrefix?: string | undefined;
|
|
9
|
+
logLevel?: 'trace' | 'debug' | 'info' | 'warn' | 'error';
|
|
10
|
+
environment: string;
|
|
11
|
+
ignoreUrls?: Array<string | RegExp>;
|
|
12
|
+
}
|
|
13
|
+
type LabelValue = string | number | boolean | null;
|
|
14
|
+
interface TransactionLabels {
|
|
15
|
+
[key: string]: LabelValue;
|
|
16
|
+
}
|
|
17
|
+
declare function APMInitializer({ serviceName, serverUrl, serverUrlPrefix, logLevel, environment, ignoreUrls }: APMInitializerProps): null;
|
|
18
|
+
declare function handleApmTransaction(name: string, type: string, labels: TransactionLabels, callback: () => Promise<void> | void): Promise<void>;
|
|
19
|
+
declare function traceError(error: string | Error, labels?: TransactionLabels, customMessage?: string): void;
|
|
20
|
+
declare const startManagedTransaction: (name: string, type: string | undefined, spanName: string, spanType?: string) => {
|
|
21
|
+
transaction: Transaction | undefined;
|
|
22
|
+
span: ApmSpan | undefined;
|
|
23
|
+
traceParentId: string | undefined;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export { APMInitializer, handleApmTransaction, startManagedTransaction, traceError };
|
|
@@ -0,0 +1,86 @@
|
|
|
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/services/index.ts
|
|
21
|
+
var services_exports = {};
|
|
22
|
+
__export(services_exports, {
|
|
23
|
+
APMInitializer: () => APMInitializer,
|
|
24
|
+
handleApmTransaction: () => handleApmTransaction,
|
|
25
|
+
startManagedTransaction: () => startManagedTransaction,
|
|
26
|
+
traceError: () => traceError
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(services_exports);
|
|
29
|
+
|
|
30
|
+
// src/services/apm/elastic-apm.ts
|
|
31
|
+
var import_react = require("react");
|
|
32
|
+
var import_apm_rum = require("@elastic/apm-rum");
|
|
33
|
+
function APMInitializer({
|
|
34
|
+
serviceName,
|
|
35
|
+
serverUrl,
|
|
36
|
+
serverUrlPrefix = void 0,
|
|
37
|
+
logLevel = "warn",
|
|
38
|
+
environment,
|
|
39
|
+
ignoreUrls = []
|
|
40
|
+
}) {
|
|
41
|
+
(0, import_react.useEffect)(() => {
|
|
42
|
+
const serviceUrl = !serverUrl ? typeof window !== "undefined" ? window.location.origin : "" : serverUrl;
|
|
43
|
+
(0, import_apm_rum.init)({
|
|
44
|
+
serviceName,
|
|
45
|
+
serverUrl: serviceUrl,
|
|
46
|
+
serverUrlPrefix,
|
|
47
|
+
serviceVersion: "1.0.0",
|
|
48
|
+
logLevel,
|
|
49
|
+
environment,
|
|
50
|
+
ignoreTransactions: ignoreUrls
|
|
51
|
+
});
|
|
52
|
+
}, []);
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
async function handleApmTransaction(name, type, labels, callback) {
|
|
56
|
+
const transaction = import_apm_rum.apm.startTransaction(name, type);
|
|
57
|
+
try {
|
|
58
|
+
transaction?.addLabels(labels);
|
|
59
|
+
await callback();
|
|
60
|
+
} catch (error) {
|
|
61
|
+
import_apm_rum.apm.captureError(error);
|
|
62
|
+
} finally {
|
|
63
|
+
transaction?.end();
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
function traceError(error, labels, customMessage) {
|
|
67
|
+
try {
|
|
68
|
+
const errorObject = error instanceof Error ? error : new Error(customMessage ?? String(error));
|
|
69
|
+
if (labels) {
|
|
70
|
+
import_apm_rum.apm.addLabels(labels);
|
|
71
|
+
}
|
|
72
|
+
import_apm_rum.apm.captureError(errorObject);
|
|
73
|
+
} catch (e) {
|
|
74
|
+
throw new Error("Failed to capture error in APM:", e);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
var startManagedTransaction = (name, type = "http-request", spanName, spanType = "external.http") => {
|
|
78
|
+
const transaction = import_apm_rum.apm.startTransaction(name, type, { managed: true });
|
|
79
|
+
const span = transaction?.startSpan(spanName, spanType);
|
|
80
|
+
const traceParentId = span ? createTraceParentId(span.traceId || "", span.id || "") : void 0;
|
|
81
|
+
return { transaction, span, traceParentId };
|
|
82
|
+
};
|
|
83
|
+
var createTraceParentId = (traceId, spanId) => {
|
|
84
|
+
return `00-${traceId}-${spanId}-01`;
|
|
85
|
+
};
|
|
86
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/services/index.ts","../../src/services/apm/elastic-apm.ts"],"sourcesContent":["export * from './apm'","'use client'\n\nimport { useEffect } from 'react'\nimport { init as initApm, apm, Transaction } from '@elastic/apm-rum'\nimport { ApmSpan } from '../../types/type'\n\ninterface APMInitializerProps {\n serviceName: string\n serverUrl?: string\n serverUrlPrefix?: string | undefined\n logLevel?: 'trace' | 'debug' | 'info' | 'warn' | 'error'\n environment: string\n ignoreUrls?: Array<string | RegExp>\n}\n\ntype LabelValue = string | number | boolean | null;\n\ninterface TransactionLabels {\n [key: string]: LabelValue;\n}\n\nexport function APMInitializer({\n serviceName,\n serverUrl,\n serverUrlPrefix = undefined,\n logLevel = 'warn',\n environment,\n ignoreUrls = []\n}: APMInitializerProps) {\n \n useEffect(() => {\n const serviceUrl = !serverUrl ? (typeof window !== 'undefined' ? window.location.origin : '') : serverUrl\n initApm({\n serviceName: serviceName,\n serverUrl: serviceUrl,\n serverUrlPrefix: serverUrlPrefix,\n serviceVersion: '1.0.0',\n logLevel: logLevel,\n environment: environment,\n ignoreTransactions: ignoreUrls\n })\n }, [])\n \n return null\n}\n\n\nexport async function handleApmTransaction(\n name: string,\n type: string,\n labels: TransactionLabels,\n callback: () => Promise<void> | void\n) {\n const transaction = apm.startTransaction(name, type);\n\n try {\n transaction?.addLabels(labels as Labels);\n await callback();\n } catch (error) {\n apm.captureError(error as Error);\n } finally {\n transaction?.end();\n }\n}\n\nexport function traceError(\n error: string | Error,\n labels?: TransactionLabels,\n customMessage?: string\n) {\n try {\n const errorObject =\n error instanceof Error\n ? error\n : new Error(customMessage ?? String(error));\n\n if (labels) {\n apm.addLabels(labels as Labels);\n }\n\n apm.captureError(errorObject);\n } catch (e) {\n throw new Error(\"Failed to capture error in APM:\", e as Error);\n }\n}\n\nexport const startManagedTransaction = (\n name: string,\n type: string = 'http-request',\n spanName: string,\n spanType: string = 'external.http'\n): { transaction: Transaction | undefined, span: ApmSpan | undefined, traceParentId: string | undefined } => {\n const transaction = apm.startTransaction(name, type, { managed: true });\n const span: ApmSpan | undefined = transaction?.startSpan(spanName, spanType);\n \n const traceParentId = span ? createTraceParentId(span.traceId || '', span.id || '') : undefined;\n \n return { transaction, span, traceParentId };\n};\n\nconst createTraceParentId = (traceId: string, spanId: string) => {\n return `00-${traceId}-${spanId}-01`;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACEA,mBAA0B;AAC1B,qBAAkD;AAkB3C,SAAS,eAAe;AAAA,EAC7B;AAAA,EACA;AAAA,EACA,kBAAkB;AAAA,EAClB,WAAW;AAAA,EACX;AAAA,EACA,aAAa,CAAC;AAChB,GAAwB;AAEtB,8BAAU,MAAM;AACd,UAAM,aAAa,CAAC,YAAa,OAAO,WAAW,cAAc,OAAO,SAAS,SAAS,KAAM;AAChG,uBAAAA,MAAQ;AAAA,MACN;AAAA,MACA,WAAW;AAAA,MACX;AAAA,MACA,gBAAgB;AAAA,MAChB;AAAA,MACA;AAAA,MACA,oBAAoB;AAAA,IACtB,CAAC;AAAA,EACH,GAAG,CAAC,CAAC;AAEL,SAAO;AACT;AAGA,eAAsB,qBACpB,MACA,MACA,QACA,UACA;AACA,QAAM,cAAc,mBAAI,iBAAiB,MAAM,IAAI;AAEnD,MAAI;AACF,iBAAa,UAAU,MAAgB;AACvC,UAAM,SAAS;AAAA,EACjB,SAAS,OAAO;AACd,uBAAI,aAAa,KAAc;AAAA,EACjC,UAAE;AACA,iBAAa,IAAI;AAAA,EACnB;AACF;AAEO,SAAS,WACd,OACA,QACA,eACA;AACA,MAAI;AACF,UAAM,cACJ,iBAAiB,QACb,QACA,IAAI,MAAM,iBAAiB,OAAO,KAAK,CAAC;AAE9C,QAAI,QAAQ;AACV,yBAAI,UAAU,MAAgB;AAAA,IAChC;AAEA,uBAAI,aAAa,WAAW;AAAA,EAC9B,SAAS,GAAG;AACV,UAAM,IAAI,MAAM,mCAAmC,CAAU;AAAA,EAC/D;AACF;AAEO,IAAM,0BAA0B,CACrC,MACA,OAAe,gBACf,UACA,WAAmB,oBACwF;AAC3G,QAAM,cAAc,mBAAI,iBAAiB,MAAM,MAAM,EAAE,SAAS,KAAK,CAAC;AACtE,QAAM,OAA4B,aAAa,UAAU,UAAU,QAAQ;AAE3E,QAAM,gBAAgB,OAAO,oBAAoB,KAAK,WAAW,IAAI,KAAK,MAAM,EAAE,IAAI;AAEtF,SAAO,EAAE,aAAa,MAAM,cAAc;AAC5C;AAEA,IAAM,sBAAsB,CAAC,SAAiB,WAAmB;AAC/D,SAAO,MAAM,OAAO,IAAI,MAAM;AAChC;","names":["initApm"]}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
// src/services/apm/elastic-apm.ts
|
|
3
|
+
import { useEffect } from "react";
|
|
4
|
+
import { init as initApm, apm } from "@elastic/apm-rum";
|
|
5
|
+
function APMInitializer({
|
|
6
|
+
serviceName,
|
|
7
|
+
serverUrl,
|
|
8
|
+
serverUrlPrefix = void 0,
|
|
9
|
+
logLevel = "warn",
|
|
10
|
+
environment,
|
|
11
|
+
ignoreUrls = []
|
|
12
|
+
}) {
|
|
13
|
+
useEffect(() => {
|
|
14
|
+
const serviceUrl = !serverUrl ? typeof window !== "undefined" ? window.location.origin : "" : serverUrl;
|
|
15
|
+
initApm({
|
|
16
|
+
serviceName,
|
|
17
|
+
serverUrl: serviceUrl,
|
|
18
|
+
serverUrlPrefix,
|
|
19
|
+
serviceVersion: "1.0.0",
|
|
20
|
+
logLevel,
|
|
21
|
+
environment,
|
|
22
|
+
ignoreTransactions: ignoreUrls
|
|
23
|
+
});
|
|
24
|
+
}, []);
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
async function handleApmTransaction(name, type, labels, callback) {
|
|
28
|
+
const transaction = apm.startTransaction(name, type);
|
|
29
|
+
try {
|
|
30
|
+
transaction?.addLabels(labels);
|
|
31
|
+
await callback();
|
|
32
|
+
} catch (error) {
|
|
33
|
+
apm.captureError(error);
|
|
34
|
+
} finally {
|
|
35
|
+
transaction?.end();
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function traceError(error, labels, customMessage) {
|
|
39
|
+
try {
|
|
40
|
+
const errorObject = error instanceof Error ? error : new Error(customMessage ?? String(error));
|
|
41
|
+
if (labels) {
|
|
42
|
+
apm.addLabels(labels);
|
|
43
|
+
}
|
|
44
|
+
apm.captureError(errorObject);
|
|
45
|
+
} catch (e) {
|
|
46
|
+
throw new Error("Failed to capture error in APM:", e);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
var startManagedTransaction = (name, type = "http-request", spanName, spanType = "external.http") => {
|
|
50
|
+
const transaction = apm.startTransaction(name, type, { managed: true });
|
|
51
|
+
const span = transaction?.startSpan(spanName, spanType);
|
|
52
|
+
const traceParentId = span ? createTraceParentId(span.traceId || "", span.id || "") : void 0;
|
|
53
|
+
return { transaction, span, traceParentId };
|
|
54
|
+
};
|
|
55
|
+
var createTraceParentId = (traceId, spanId) => {
|
|
56
|
+
return `00-${traceId}-${spanId}-01`;
|
|
57
|
+
};
|
|
58
|
+
export {
|
|
59
|
+
APMInitializer,
|
|
60
|
+
handleApmTransaction,
|
|
61
|
+
startManagedTransaction,
|
|
62
|
+
traceError
|
|
63
|
+
};
|
|
64
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/services/apm/elastic-apm.ts"],"sourcesContent":["'use client'\n\nimport { useEffect } from 'react'\nimport { init as initApm, apm, Transaction } from '@elastic/apm-rum'\nimport { ApmSpan } from '../../types/type'\n\ninterface APMInitializerProps {\n serviceName: string\n serverUrl?: string\n serverUrlPrefix?: string | undefined\n logLevel?: 'trace' | 'debug' | 'info' | 'warn' | 'error'\n environment: string\n ignoreUrls?: Array<string | RegExp>\n}\n\ntype LabelValue = string | number | boolean | null;\n\ninterface TransactionLabels {\n [key: string]: LabelValue;\n}\n\nexport function APMInitializer({\n serviceName,\n serverUrl,\n serverUrlPrefix = undefined,\n logLevel = 'warn',\n environment,\n ignoreUrls = []\n}: APMInitializerProps) {\n \n useEffect(() => {\n const serviceUrl = !serverUrl ? (typeof window !== 'undefined' ? window.location.origin : '') : serverUrl\n initApm({\n serviceName: serviceName,\n serverUrl: serviceUrl,\n serverUrlPrefix: serverUrlPrefix,\n serviceVersion: '1.0.0',\n logLevel: logLevel,\n environment: environment,\n ignoreTransactions: ignoreUrls\n })\n }, [])\n \n return null\n}\n\n\nexport async function handleApmTransaction(\n name: string,\n type: string,\n labels: TransactionLabels,\n callback: () => Promise<void> | void\n) {\n const transaction = apm.startTransaction(name, type);\n\n try {\n transaction?.addLabels(labels as Labels);\n await callback();\n } catch (error) {\n apm.captureError(error as Error);\n } finally {\n transaction?.end();\n }\n}\n\nexport function traceError(\n error: string | Error,\n labels?: TransactionLabels,\n customMessage?: string\n) {\n try {\n const errorObject =\n error instanceof Error\n ? error\n : new Error(customMessage ?? String(error));\n\n if (labels) {\n apm.addLabels(labels as Labels);\n }\n\n apm.captureError(errorObject);\n } catch (e) {\n throw new Error(\"Failed to capture error in APM:\", e as Error);\n }\n}\n\nexport const startManagedTransaction = (\n name: string,\n type: string = 'http-request',\n spanName: string,\n spanType: string = 'external.http'\n): { transaction: Transaction | undefined, span: ApmSpan | undefined, traceParentId: string | undefined } => {\n const transaction = apm.startTransaction(name, type, { managed: true });\n const span: ApmSpan | undefined = transaction?.startSpan(spanName, spanType);\n \n const traceParentId = span ? createTraceParentId(span.traceId || '', span.id || '') : undefined;\n \n return { transaction, span, traceParentId };\n};\n\nconst createTraceParentId = (traceId: string, spanId: string) => {\n return `00-${traceId}-${spanId}-01`;\n}\n"],"mappings":";AAEA,SAA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