@tradejs/node 3.0.0 → 3.1.0
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/README.md +2 -0
- package/dist/ai.d.mts +26 -4
- package/dist/ai.d.ts +26 -4
- package/dist/ai.js +161 -165
- package/dist/ai.mjs +3 -1
- package/dist/backtest.js +496 -188
- package/dist/backtest.mjs +3 -3
- package/dist/chunk-3TWULKHV.mjs +595 -0
- package/dist/chunk-FB5NUEOQ.mjs +295 -0
- package/dist/{chunk-VRXU4E4O.mjs → chunk-KOQSLFT2.mjs} +169 -169
- package/dist/chunk-LAJ7NA3Q.mjs +377 -0
- package/dist/{chunk-OGAWBO3Z.mjs → chunk-MKLTJQLY.mjs} +1 -1
- package/dist/cli.js +150 -161
- package/dist/cli.mjs +1 -1
- package/dist/connectors.d.mts +5 -2
- package/dist/connectors.d.ts +5 -2
- package/dist/connectors.js +329 -8
- package/dist/connectors.mjs +5 -1
- package/dist/registry.d.mts +2 -1
- package/dist/registry.d.ts +2 -1
- package/dist/registry.js +157 -161
- package/dist/registry.mjs +4 -2
- package/dist/runtimeDashboard.d.mts +12 -0
- package/dist/runtimeDashboard.d.ts +12 -0
- package/dist/runtimeDashboard.js +8433 -0
- package/dist/runtimeDashboard.mjs +955 -0
- package/dist/runtimeTrades.d.mts +31 -0
- package/dist/runtimeTrades.d.ts +31 -0
- package/dist/runtimeTrades.js +321 -0
- package/dist/runtimeTrades.mjs +11 -0
- package/dist/strategies.d.mts +2 -2
- package/dist/strategies.d.ts +2 -2
- package/dist/strategies.js +168 -165
- package/dist/strategies.mjs +13 -372
- package/package.json +18 -5
- package/dist/chunk-V3YMKE4I.mjs +0 -271
package/README.md
CHANGED
package/dist/ai.d.mts
CHANGED
|
@@ -1,9 +1,34 @@
|
|
|
1
1
|
import { Signal, AiPayload, SignalAnalysis, AiPromptPair } from '@tradejs/types';
|
|
2
2
|
|
|
3
|
+
interface AiChatMessage$1 {
|
|
4
|
+
role: 'system' | 'user';
|
|
5
|
+
content: string;
|
|
6
|
+
}
|
|
7
|
+
interface InvokeAiChatOptions$1 {
|
|
8
|
+
messages: AiChatMessage$1[];
|
|
9
|
+
userName?: string;
|
|
10
|
+
model?: string;
|
|
11
|
+
temperature?: number;
|
|
12
|
+
}
|
|
13
|
+
declare const DEFAULT_AI_MODEL = "openai/gpt-5-mini";
|
|
14
|
+
declare const getOpenRouterModelKwargs: (apiEndpoint?: string | null) => Record<string, unknown>;
|
|
15
|
+
declare const resetAiRuntimeCache: () => void;
|
|
16
|
+
|
|
3
17
|
declare const MAX_AI_SERIES_POINTS = 5;
|
|
4
18
|
declare const trimSeriesDeep: (value: any) => any;
|
|
5
19
|
declare const buildCompactAiIndicatorsSnapshot: (value: any) => any;
|
|
6
20
|
|
|
21
|
+
interface AiChatMessage extends AiChatMessage$1 {
|
|
22
|
+
/** @deprecated Provider formatting is retained for API compatibility only. */
|
|
23
|
+
format?: 'plain' | 'text-block';
|
|
24
|
+
}
|
|
25
|
+
type InvokeAiChatOptions = Omit<InvokeAiChatOptions$1, 'messages'> & {
|
|
26
|
+
messages: AiChatMessage[];
|
|
27
|
+
};
|
|
28
|
+
declare const invokeAiChat: (options: InvokeAiChatOptions) => Promise<{
|
|
29
|
+
content: string | object;
|
|
30
|
+
}>;
|
|
31
|
+
|
|
7
32
|
type DeterministicAiGateContext = {
|
|
8
33
|
approvalAllowedNow?: boolean;
|
|
9
34
|
deterministicQuality?: number;
|
|
@@ -22,12 +47,9 @@ interface AiRequestOptions {
|
|
|
22
47
|
payload?: AiPayload;
|
|
23
48
|
model?: string;
|
|
24
49
|
}
|
|
25
|
-
declare const DEFAULT_AI_MODEL = "openai/gpt-5-mini";
|
|
26
|
-
declare const getOpenRouterModelKwargs: (apiEndpoint?: string | null) => Record<string, unknown>;
|
|
27
|
-
declare const resetAiRuntimeCache: () => void;
|
|
28
50
|
declare const buildAiPrompts: (signal: Signal) => AiPromptPair;
|
|
29
51
|
declare const runAiPrompt: ({ systemPrompt, humanPrompt }: AiPromptPair, options?: AiRequestOptions) => Promise<Partial<SignalAnalysis>>;
|
|
30
52
|
declare const runAiPromptLocal: (signal: Signal, options?: Omit<AiRequestOptions, "model" | "userName">) => Promise<Partial<SignalAnalysis>>;
|
|
31
53
|
declare const askAI: (signal: Signal, options?: AiRequestOptions) => Promise<Partial<SignalAnalysis>>;
|
|
32
54
|
|
|
33
|
-
export { DEFAULT_AI_MODEL, MAX_AI_SERIES_POINTS, askAI, buildAiHumanPrompt, buildAiPayload, buildAiPrompts, buildAiSystemPrompt, buildCompactAiIndicatorsSnapshot, getDeterministicAiGateContext, getOpenRouterModelKwargs, resetAiRuntimeCache, runAiPrompt, runAiPromptLocal, trimSeriesDeep };
|
|
55
|
+
export { type AiChatMessage, DEFAULT_AI_MODEL, type InvokeAiChatOptions, MAX_AI_SERIES_POINTS, askAI, buildAiHumanPrompt, buildAiPayload, buildAiPrompts, buildAiSystemPrompt, buildCompactAiIndicatorsSnapshot, getDeterministicAiGateContext, getOpenRouterModelKwargs, invokeAiChat, resetAiRuntimeCache, runAiPrompt, runAiPromptLocal, trimSeriesDeep };
|
package/dist/ai.d.ts
CHANGED
|
@@ -1,9 +1,34 @@
|
|
|
1
1
|
import { Signal, AiPayload, SignalAnalysis, AiPromptPair } from '@tradejs/types';
|
|
2
2
|
|
|
3
|
+
interface AiChatMessage$1 {
|
|
4
|
+
role: 'system' | 'user';
|
|
5
|
+
content: string;
|
|
6
|
+
}
|
|
7
|
+
interface InvokeAiChatOptions$1 {
|
|
8
|
+
messages: AiChatMessage$1[];
|
|
9
|
+
userName?: string;
|
|
10
|
+
model?: string;
|
|
11
|
+
temperature?: number;
|
|
12
|
+
}
|
|
13
|
+
declare const DEFAULT_AI_MODEL = "openai/gpt-5-mini";
|
|
14
|
+
declare const getOpenRouterModelKwargs: (apiEndpoint?: string | null) => Record<string, unknown>;
|
|
15
|
+
declare const resetAiRuntimeCache: () => void;
|
|
16
|
+
|
|
3
17
|
declare const MAX_AI_SERIES_POINTS = 5;
|
|
4
18
|
declare const trimSeriesDeep: (value: any) => any;
|
|
5
19
|
declare const buildCompactAiIndicatorsSnapshot: (value: any) => any;
|
|
6
20
|
|
|
21
|
+
interface AiChatMessage extends AiChatMessage$1 {
|
|
22
|
+
/** @deprecated Provider formatting is retained for API compatibility only. */
|
|
23
|
+
format?: 'plain' | 'text-block';
|
|
24
|
+
}
|
|
25
|
+
type InvokeAiChatOptions = Omit<InvokeAiChatOptions$1, 'messages'> & {
|
|
26
|
+
messages: AiChatMessage[];
|
|
27
|
+
};
|
|
28
|
+
declare const invokeAiChat: (options: InvokeAiChatOptions) => Promise<{
|
|
29
|
+
content: string | object;
|
|
30
|
+
}>;
|
|
31
|
+
|
|
7
32
|
type DeterministicAiGateContext = {
|
|
8
33
|
approvalAllowedNow?: boolean;
|
|
9
34
|
deterministicQuality?: number;
|
|
@@ -22,12 +47,9 @@ interface AiRequestOptions {
|
|
|
22
47
|
payload?: AiPayload;
|
|
23
48
|
model?: string;
|
|
24
49
|
}
|
|
25
|
-
declare const DEFAULT_AI_MODEL = "openai/gpt-5-mini";
|
|
26
|
-
declare const getOpenRouterModelKwargs: (apiEndpoint?: string | null) => Record<string, unknown>;
|
|
27
|
-
declare const resetAiRuntimeCache: () => void;
|
|
28
50
|
declare const buildAiPrompts: (signal: Signal) => AiPromptPair;
|
|
29
51
|
declare const runAiPrompt: ({ systemPrompt, humanPrompt }: AiPromptPair, options?: AiRequestOptions) => Promise<Partial<SignalAnalysis>>;
|
|
30
52
|
declare const runAiPromptLocal: (signal: Signal, options?: Omit<AiRequestOptions, "model" | "userName">) => Promise<Partial<SignalAnalysis>>;
|
|
31
53
|
declare const askAI: (signal: Signal, options?: AiRequestOptions) => Promise<Partial<SignalAnalysis>>;
|
|
32
54
|
|
|
33
|
-
export { DEFAULT_AI_MODEL, MAX_AI_SERIES_POINTS, askAI, buildAiHumanPrompt, buildAiPayload, buildAiPrompts, buildAiSystemPrompt, buildCompactAiIndicatorsSnapshot, getDeterministicAiGateContext, getOpenRouterModelKwargs, resetAiRuntimeCache, runAiPrompt, runAiPromptLocal, trimSeriesDeep };
|
|
55
|
+
export { type AiChatMessage, DEFAULT_AI_MODEL, type InvokeAiChatOptions, MAX_AI_SERIES_POINTS, askAI, buildAiHumanPrompt, buildAiPayload, buildAiPrompts, buildAiSystemPrompt, buildCompactAiIndicatorsSnapshot, getDeterministicAiGateContext, getOpenRouterModelKwargs, invokeAiChat, resetAiRuntimeCache, runAiPrompt, runAiPromptLocal, trimSeriesDeep };
|
package/dist/ai.js
CHANGED
|
@@ -40,17 +40,15 @@ __export(ai_exports, {
|
|
|
40
40
|
buildCompactAiIndicatorsSnapshot: () => buildCompactAiIndicatorsSnapshot,
|
|
41
41
|
getDeterministicAiGateContext: () => getDeterministicAiGateContext,
|
|
42
42
|
getOpenRouterModelKwargs: () => getOpenRouterModelKwargs,
|
|
43
|
+
invokeAiChat: () => invokeAiChat,
|
|
43
44
|
resetAiRuntimeCache: () => resetAiRuntimeCache,
|
|
44
45
|
runAiPrompt: () => runAiPrompt,
|
|
45
46
|
runAiPromptLocal: () => runAiPromptLocal,
|
|
46
47
|
trimSeriesDeep: () => trimSeriesDeep
|
|
47
48
|
});
|
|
48
49
|
module.exports = __toCommonJS(ai_exports);
|
|
49
|
-
var
|
|
50
|
-
var import_aiEndpoints = require("@tradejs/core/aiEndpoints");
|
|
51
|
-
var import_aiModels = require("@tradejs/core/aiModels");
|
|
50
|
+
var import_aiLanguages2 = require("@tradejs/core/aiLanguages");
|
|
52
51
|
var import_redis = require("@tradejs/infra/redis");
|
|
53
|
-
var import_userSettings = require("@tradejs/infra/userSettings");
|
|
54
52
|
|
|
55
53
|
// src/aiShared.ts
|
|
56
54
|
var MAX_AI_SERIES_POINTS = 5;
|
|
@@ -757,7 +755,149 @@ var postProcessLocalAiAnalysisByStrategy = (signal, analysis, payload = buildAiP
|
|
|
757
755
|
}) ?? strategyAnalysis;
|
|
758
756
|
};
|
|
759
757
|
|
|
758
|
+
// src/aiProvider.ts
|
|
759
|
+
var import_aiEndpoints = require("@tradejs/core/aiEndpoints");
|
|
760
|
+
var import_aiModels = require("@tradejs/core/aiModels");
|
|
761
|
+
var import_aiLanguages = require("@tradejs/core/aiLanguages");
|
|
762
|
+
var import_userSettings = require("@tradejs/infra/userSettings");
|
|
763
|
+
var DEFAULT_AI_MODEL = "openai/gpt-5-mini";
|
|
764
|
+
var userSettingsCache = /* @__PURE__ */ new Map();
|
|
765
|
+
var aiModelCache = /* @__PURE__ */ new Map();
|
|
766
|
+
var normalizeResponseContent = (content) => {
|
|
767
|
+
if (typeof content === "string") return content;
|
|
768
|
+
if (content && typeof content === "object" && !Array.isArray(content)) {
|
|
769
|
+
return content;
|
|
770
|
+
}
|
|
771
|
+
if (Array.isArray(content)) {
|
|
772
|
+
return content.map(
|
|
773
|
+
(part) => typeof part?.text === "string" ? part.text : ""
|
|
774
|
+
).join("\n").trim();
|
|
775
|
+
}
|
|
776
|
+
return String(content ?? "");
|
|
777
|
+
};
|
|
778
|
+
var getAiInvocationError = (error) => {
|
|
779
|
+
const details = error instanceof Error && error.message.trim() ? error.message.trim() : String(error);
|
|
780
|
+
const isEmptyCompletion = error instanceof TypeError && /Cannot read properties of undefined \(reading ['"]message['"]\)/.test(
|
|
781
|
+
details
|
|
782
|
+
);
|
|
783
|
+
const wrapped = new Error(
|
|
784
|
+
isEmptyCompletion ? "AI provider returned an empty chat completion" : `AI model invocation failed: ${details}`
|
|
785
|
+
);
|
|
786
|
+
wrapped.cause = error;
|
|
787
|
+
return wrapped;
|
|
788
|
+
};
|
|
789
|
+
var isEmptyResponseContent = (content) => typeof content === "string" ? content.trim().length === 0 : Object.keys(content).length === 0;
|
|
790
|
+
var getAiModelCacheKey = (userName, modelName, temperature) => `${userName}::${modelName}::${temperature}`;
|
|
791
|
+
var resolveAiModelName = (settings, requestedModelName) => {
|
|
792
|
+
const explicitModelName = requestedModelName?.trim() ?? "";
|
|
793
|
+
if (explicitModelName) return explicitModelName;
|
|
794
|
+
return settings.AI_MODEL?.trim() || DEFAULT_AI_MODEL;
|
|
795
|
+
};
|
|
796
|
+
var getOpenRouterModelKwargs = (apiEndpoint) => {
|
|
797
|
+
const endpoint = String(apiEndpoint ?? "").trim();
|
|
798
|
+
if (!endpoint) return {};
|
|
799
|
+
let hostname = "";
|
|
800
|
+
try {
|
|
801
|
+
hostname = new URL(endpoint).hostname;
|
|
802
|
+
} catch {
|
|
803
|
+
hostname = endpoint;
|
|
804
|
+
}
|
|
805
|
+
return hostname.toLowerCase().includes("openrouter") ? { provider: { ignore: ["azure"] } } : {};
|
|
806
|
+
};
|
|
807
|
+
var getAiUserSettings = async (userName = "root") => {
|
|
808
|
+
let settingsPromise = userSettingsCache.get(userName);
|
|
809
|
+
if (!settingsPromise) {
|
|
810
|
+
settingsPromise = (0, import_userSettings.getUserSettings)(userName).then((settings2) => {
|
|
811
|
+
const endpoint = (0, import_aiEndpoints.normalizeAiEndpoint)(settings2.AI_API_ENDPOINT);
|
|
812
|
+
return {
|
|
813
|
+
...settings2,
|
|
814
|
+
AI_API_ENDPOINT: endpoint,
|
|
815
|
+
AI_MODEL: (0, import_aiModels.normalizeAiModel)(settings2.AI_MODEL, endpoint),
|
|
816
|
+
AI_RESPONSE_LANGUAGE: (0, import_aiLanguages.normalizeAiResponseLanguage)(
|
|
817
|
+
settings2.AI_RESPONSE_LANGUAGE
|
|
818
|
+
)
|
|
819
|
+
};
|
|
820
|
+
});
|
|
821
|
+
settingsPromise.catch(() => userSettingsCache.delete(userName));
|
|
822
|
+
userSettingsCache.set(userName, settingsPromise);
|
|
823
|
+
}
|
|
824
|
+
const settings = await settingsPromise;
|
|
825
|
+
if (!settings.AI_API_KEY || !settings.AI_API_ENDPOINT) {
|
|
826
|
+
throw new Error(`AI settings are incomplete for user ${userName}`);
|
|
827
|
+
}
|
|
828
|
+
return settings;
|
|
829
|
+
};
|
|
830
|
+
var getAiModel = async (userName = "root", requestedModelName, temperature = 0.2) => {
|
|
831
|
+
const settings = await getAiUserSettings(userName);
|
|
832
|
+
const modelName = resolveAiModelName(settings, requestedModelName);
|
|
833
|
+
const cacheKey = getAiModelCacheKey(userName, modelName, temperature);
|
|
834
|
+
let modelPromise = aiModelCache.get(cacheKey);
|
|
835
|
+
if (!modelPromise) {
|
|
836
|
+
modelPromise = import("@langchain/openai").then(({ ChatOpenAI }) => {
|
|
837
|
+
const modelKwargs = getOpenRouterModelKwargs(settings.AI_API_ENDPOINT);
|
|
838
|
+
return new ChatOpenAI({
|
|
839
|
+
temperature,
|
|
840
|
+
modelName,
|
|
841
|
+
apiKey: settings.AI_API_KEY,
|
|
842
|
+
...Object.keys(modelKwargs).length ? { modelKwargs } : {},
|
|
843
|
+
configuration: {
|
|
844
|
+
baseURL: settings.AI_API_ENDPOINT,
|
|
845
|
+
defaultHeaders: {
|
|
846
|
+
"HTTP-Referer": "https://tradejs.dev",
|
|
847
|
+
"X-Title": "Inv"
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
});
|
|
851
|
+
});
|
|
852
|
+
modelPromise.catch(() => aiModelCache.delete(cacheKey));
|
|
853
|
+
aiModelCache.set(cacheKey, modelPromise);
|
|
854
|
+
}
|
|
855
|
+
try {
|
|
856
|
+
return await modelPromise;
|
|
857
|
+
} catch (error) {
|
|
858
|
+
aiModelCache.delete(cacheKey);
|
|
859
|
+
userSettingsCache.delete(userName);
|
|
860
|
+
throw error;
|
|
861
|
+
}
|
|
862
|
+
};
|
|
863
|
+
var resetAiRuntimeCache = () => {
|
|
864
|
+
aiModelCache.clear();
|
|
865
|
+
userSettingsCache.clear();
|
|
866
|
+
};
|
|
867
|
+
var invokeAiChatWithUserMessageEncoding = async ({
|
|
868
|
+
messages,
|
|
869
|
+
userName = "root",
|
|
870
|
+
model,
|
|
871
|
+
temperature = 0.2
|
|
872
|
+
}, resolveUserMessageEncoding) => {
|
|
873
|
+
const [{ HumanMessage, SystemMessage }, aiModel] = await Promise.all([
|
|
874
|
+
import("@langchain/core/messages"),
|
|
875
|
+
getAiModel(userName, model, temperature)
|
|
876
|
+
]);
|
|
877
|
+
const providerMessages = messages.map(
|
|
878
|
+
(message) => message.role === "system" ? new SystemMessage(message.content) : new HumanMessage(
|
|
879
|
+
resolveUserMessageEncoding(message) === "text-block" ? { content: [{ type: "text", text: message.content }] } : message.content
|
|
880
|
+
)
|
|
881
|
+
);
|
|
882
|
+
try {
|
|
883
|
+
const response = await aiModel.invoke(providerMessages);
|
|
884
|
+
const content = normalizeResponseContent(response?.content);
|
|
885
|
+
if (isEmptyResponseContent(content)) {
|
|
886
|
+
throw new Error("AI provider returned an empty chat completion");
|
|
887
|
+
}
|
|
888
|
+
return { content };
|
|
889
|
+
} catch (error) {
|
|
890
|
+
throw getAiInvocationError(error);
|
|
891
|
+
}
|
|
892
|
+
};
|
|
893
|
+
var invokeAiPromptChat = (options) => invokeAiChatWithUserMessageEncoding(options, () => "text-block");
|
|
894
|
+
var invokeCompatibleAiChat = (options) => invokeAiChatWithUserMessageEncoding(
|
|
895
|
+
options,
|
|
896
|
+
(message) => message.format ?? "plain"
|
|
897
|
+
);
|
|
898
|
+
|
|
760
899
|
// src/ai.ts
|
|
900
|
+
var invokeAiChat = (options) => invokeCompatibleAiChat(options);
|
|
761
901
|
var parseAIResponse = (input) => {
|
|
762
902
|
try {
|
|
763
903
|
if (typeof input === "object" && input !== null) return input;
|
|
@@ -770,18 +910,6 @@ var parseAIResponse = (input) => {
|
|
|
770
910
|
return {};
|
|
771
911
|
}
|
|
772
912
|
};
|
|
773
|
-
var normalizeResponseContent = (content) => {
|
|
774
|
-
if (typeof content === "string" || content && typeof content === "object") {
|
|
775
|
-
if (typeof content !== "object" || !Array.isArray(content)) {
|
|
776
|
-
return content;
|
|
777
|
-
}
|
|
778
|
-
}
|
|
779
|
-
if (Array.isArray(content)) {
|
|
780
|
-
const text = content.map((part) => typeof part?.text === "string" ? part.text : "").join("\n").trim();
|
|
781
|
-
return text;
|
|
782
|
-
}
|
|
783
|
-
return String(content ?? "");
|
|
784
|
-
};
|
|
785
913
|
var normalizeAnalysis = (raw) => {
|
|
786
914
|
const direction = raw?.direction === "LONG" || raw?.direction === "SHORT" ? raw.direction : null;
|
|
787
915
|
const qualityNum = typeof raw?.quality === "number" ? Math.max(1, Math.min(5, Math.round(raw.quality))) : void 0;
|
|
@@ -1015,120 +1143,6 @@ Trade payload:
|
|
|
1015
1143
|
${JSON.stringify(payload)}
|
|
1016
1144
|
${buildAiHumanPromptAddonByStrategy(signal, payload)}
|
|
1017
1145
|
`;
|
|
1018
|
-
var getAiInvocationError = (error) => {
|
|
1019
|
-
const details = error instanceof Error && error.message.trim() ? error.message.trim() : String(error);
|
|
1020
|
-
const isEmptyCompletion = error instanceof TypeError && /Cannot read properties of undefined \(reading ['"]message['"]\)/.test(
|
|
1021
|
-
details
|
|
1022
|
-
);
|
|
1023
|
-
const wrapped = new Error(
|
|
1024
|
-
isEmptyCompletion ? "AI provider returned an empty chat completion" : `AI model invocation failed: ${details}`
|
|
1025
|
-
);
|
|
1026
|
-
wrapped.cause = error;
|
|
1027
|
-
return wrapped;
|
|
1028
|
-
};
|
|
1029
|
-
var isEmptyResponseContent = (content) => typeof content === "string" ? content.trim().length === 0 : Object.keys(content).length === 0;
|
|
1030
|
-
var DEFAULT_AI_MODEL = "openai/gpt-5-mini";
|
|
1031
|
-
var userSettingsCache = /* @__PURE__ */ new Map();
|
|
1032
|
-
var aiModelCache = /* @__PURE__ */ new Map();
|
|
1033
|
-
var getAiModelCacheKey = (userName, modelName) => `${userName}::${modelName}`;
|
|
1034
|
-
var resolveAiModelName = (settings, requestedModelName) => {
|
|
1035
|
-
const explicitModelName = typeof requestedModelName === "string" ? requestedModelName.trim() : "";
|
|
1036
|
-
if (explicitModelName) {
|
|
1037
|
-
return explicitModelName;
|
|
1038
|
-
}
|
|
1039
|
-
const settingsModelName = typeof settings.AI_MODEL === "string" ? settings.AI_MODEL.trim() : "";
|
|
1040
|
-
return settingsModelName || DEFAULT_AI_MODEL;
|
|
1041
|
-
};
|
|
1042
|
-
var getOpenRouterModelKwargs = (apiEndpoint) => {
|
|
1043
|
-
const endpoint = String(apiEndpoint ?? "").trim();
|
|
1044
|
-
if (!endpoint) {
|
|
1045
|
-
return {};
|
|
1046
|
-
}
|
|
1047
|
-
let hostname = "";
|
|
1048
|
-
try {
|
|
1049
|
-
hostname = new URL(endpoint).hostname;
|
|
1050
|
-
} catch {
|
|
1051
|
-
hostname = endpoint;
|
|
1052
|
-
}
|
|
1053
|
-
if (!hostname.toLowerCase().includes("openrouter")) {
|
|
1054
|
-
return {};
|
|
1055
|
-
}
|
|
1056
|
-
return {
|
|
1057
|
-
provider: {
|
|
1058
|
-
ignore: ["azure"]
|
|
1059
|
-
}
|
|
1060
|
-
};
|
|
1061
|
-
};
|
|
1062
|
-
var getAiSettings = async (userName = "root") => {
|
|
1063
|
-
let settingsPromise = userSettingsCache.get(userName);
|
|
1064
|
-
if (!settingsPromise) {
|
|
1065
|
-
settingsPromise = (0, import_userSettings.getUserSettings)(userName).then((settings2) => {
|
|
1066
|
-
const endpoint = (0, import_aiEndpoints.normalizeAiEndpoint)(settings2.AI_API_ENDPOINT);
|
|
1067
|
-
return {
|
|
1068
|
-
...settings2,
|
|
1069
|
-
AI_API_ENDPOINT: endpoint,
|
|
1070
|
-
AI_MODEL: (0, import_aiModels.normalizeAiModel)(settings2.AI_MODEL, endpoint),
|
|
1071
|
-
AI_RESPONSE_LANGUAGE: (0, import_aiLanguages.normalizeAiResponseLanguage)(
|
|
1072
|
-
settings2.AI_RESPONSE_LANGUAGE
|
|
1073
|
-
)
|
|
1074
|
-
};
|
|
1075
|
-
});
|
|
1076
|
-
settingsPromise.catch(() => {
|
|
1077
|
-
userSettingsCache.delete(userName);
|
|
1078
|
-
});
|
|
1079
|
-
userSettingsCache.set(userName, settingsPromise);
|
|
1080
|
-
}
|
|
1081
|
-
const settings = await settingsPromise;
|
|
1082
|
-
if (!settings.AI_API_KEY || !settings.AI_API_ENDPOINT) {
|
|
1083
|
-
throw new Error(`AI settings are incomplete for user ${userName}`);
|
|
1084
|
-
}
|
|
1085
|
-
return settings;
|
|
1086
|
-
};
|
|
1087
|
-
var createAiModel = async (userName = "root", requestedModelName) => {
|
|
1088
|
-
const settings = await getAiSettings(userName);
|
|
1089
|
-
const modelName = resolveAiModelName(settings, requestedModelName);
|
|
1090
|
-
const cacheKey = getAiModelCacheKey(userName, modelName);
|
|
1091
|
-
let modelPromise = aiModelCache.get(cacheKey);
|
|
1092
|
-
if (!modelPromise) {
|
|
1093
|
-
modelPromise = (async () => {
|
|
1094
|
-
const { ChatOpenAI } = await import("@langchain/openai");
|
|
1095
|
-
const modelKwargs = getOpenRouterModelKwargs(settings.AI_API_ENDPOINT);
|
|
1096
|
-
return new ChatOpenAI({
|
|
1097
|
-
temperature: 0.2,
|
|
1098
|
-
modelName,
|
|
1099
|
-
apiKey: settings.AI_API_KEY,
|
|
1100
|
-
...Object.keys(modelKwargs).length ? { modelKwargs } : {},
|
|
1101
|
-
configuration: {
|
|
1102
|
-
baseURL: settings.AI_API_ENDPOINT,
|
|
1103
|
-
defaultHeaders: {
|
|
1104
|
-
"HTTP-Referer": "https://tradejs.dev",
|
|
1105
|
-
"X-Title": "Inv"
|
|
1106
|
-
}
|
|
1107
|
-
}
|
|
1108
|
-
});
|
|
1109
|
-
})();
|
|
1110
|
-
modelPromise.catch(() => {
|
|
1111
|
-
aiModelCache.delete(cacheKey);
|
|
1112
|
-
});
|
|
1113
|
-
aiModelCache.set(cacheKey, modelPromise);
|
|
1114
|
-
}
|
|
1115
|
-
return modelPromise;
|
|
1116
|
-
};
|
|
1117
|
-
var getAiModel = async (userName = "root", requestedModelName) => {
|
|
1118
|
-
const settings = await getAiSettings(userName);
|
|
1119
|
-
const resolvedModelName = resolveAiModelName(settings, requestedModelName);
|
|
1120
|
-
try {
|
|
1121
|
-
return await createAiModel(userName, resolvedModelName);
|
|
1122
|
-
} catch (error) {
|
|
1123
|
-
aiModelCache.delete(getAiModelCacheKey(userName, resolvedModelName));
|
|
1124
|
-
userSettingsCache.delete(userName);
|
|
1125
|
-
throw error;
|
|
1126
|
-
}
|
|
1127
|
-
};
|
|
1128
|
-
var resetAiRuntimeCache = () => {
|
|
1129
|
-
aiModelCache.clear();
|
|
1130
|
-
userSettingsCache.clear();
|
|
1131
|
-
};
|
|
1132
1146
|
var buildAiPrompts = (signal) => {
|
|
1133
1147
|
const payload = buildAiPayload(signal);
|
|
1134
1148
|
return {
|
|
@@ -1137,42 +1151,23 @@ var buildAiPrompts = (signal) => {
|
|
|
1137
1151
|
};
|
|
1138
1152
|
};
|
|
1139
1153
|
var runAiPrompt = async ({ systemPrompt, humanPrompt }, options = {}) => {
|
|
1140
|
-
const
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
getAiSettings(options.userName)
|
|
1144
|
-
]);
|
|
1145
|
-
const messages = [];
|
|
1146
|
-
const responseLanguage = (0, import_aiLanguages.getAiResponseLanguagePromptName)(
|
|
1147
|
-
settings.AI_RESPONSE_LANGUAGE || import_aiLanguages.DEFAULT_AI_RESPONSE_LANGUAGE
|
|
1148
|
-
);
|
|
1149
|
-
messages.push(new SystemMessage(systemPrompt));
|
|
1150
|
-
messages.push(
|
|
1151
|
-
new SystemMessage(
|
|
1152
|
-
`Write all user-visible text fields in ${responseLanguage}. Keep field names and JSON syntax unchanged.`
|
|
1153
|
-
)
|
|
1154
|
-
);
|
|
1155
|
-
messages.push(
|
|
1156
|
-
new HumanMessage({
|
|
1157
|
-
content: [
|
|
1158
|
-
{
|
|
1159
|
-
type: "text",
|
|
1160
|
-
text: humanPrompt
|
|
1161
|
-
}
|
|
1162
|
-
]
|
|
1163
|
-
})
|
|
1154
|
+
const settings = await getAiUserSettings(options.userName);
|
|
1155
|
+
const responseLanguage = (0, import_aiLanguages2.getAiResponseLanguagePromptName)(
|
|
1156
|
+
settings.AI_RESPONSE_LANGUAGE || import_aiLanguages2.DEFAULT_AI_RESPONSE_LANGUAGE
|
|
1164
1157
|
);
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1158
|
+
const response = await invokeAiPromptChat({
|
|
1159
|
+
userName: options.userName,
|
|
1160
|
+
model: options.model,
|
|
1161
|
+
messages: [
|
|
1162
|
+
{ role: "system", content: systemPrompt },
|
|
1163
|
+
{
|
|
1164
|
+
role: "system",
|
|
1165
|
+
content: `Write all user-visible text fields in ${responseLanguage}. Keep field names and JSON syntax unchanged.`
|
|
1166
|
+
},
|
|
1167
|
+
{ role: "user", content: humanPrompt }
|
|
1168
|
+
]
|
|
1169
|
+
});
|
|
1170
|
+
const parsed = parseAIResponse(response.content);
|
|
1176
1171
|
const normalized = normalizeAnalysis(parsed);
|
|
1177
1172
|
if (!options.signal) {
|
|
1178
1173
|
return normalized;
|
|
@@ -1231,6 +1226,7 @@ var askAI = async (signal, options = {}) => {
|
|
|
1231
1226
|
buildCompactAiIndicatorsSnapshot,
|
|
1232
1227
|
getDeterministicAiGateContext,
|
|
1233
1228
|
getOpenRouterModelKwargs,
|
|
1229
|
+
invokeAiChat,
|
|
1234
1230
|
resetAiRuntimeCache,
|
|
1235
1231
|
runAiPrompt,
|
|
1236
1232
|
runAiPromptLocal,
|
package/dist/ai.mjs
CHANGED
|
@@ -9,11 +9,12 @@ import {
|
|
|
9
9
|
buildCompactAiIndicatorsSnapshot,
|
|
10
10
|
getDeterministicAiGateContext,
|
|
11
11
|
getOpenRouterModelKwargs,
|
|
12
|
+
invokeAiChat,
|
|
12
13
|
resetAiRuntimeCache,
|
|
13
14
|
runAiPrompt,
|
|
14
15
|
runAiPromptLocal,
|
|
15
16
|
trimSeriesDeep
|
|
16
|
-
} from "./chunk-
|
|
17
|
+
} from "./chunk-KOQSLFT2.mjs";
|
|
17
18
|
import "./chunk-WS5DYEVZ.mjs";
|
|
18
19
|
import "./chunk-Y6FXYEAI.mjs";
|
|
19
20
|
export {
|
|
@@ -27,6 +28,7 @@ export {
|
|
|
27
28
|
buildCompactAiIndicatorsSnapshot,
|
|
28
29
|
getDeterministicAiGateContext,
|
|
29
30
|
getOpenRouterModelKwargs,
|
|
31
|
+
invokeAiChat,
|
|
30
32
|
resetAiRuntimeCache,
|
|
31
33
|
runAiPrompt,
|
|
32
34
|
runAiPromptLocal,
|