luckytri 1.0.8 → 1.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.en.md +1 -1
- package/README.md +1 -1
- package/docs/en/CHANGELOG.md +44 -24
- package/docs/en/architecture.md +4 -0
- package/docs/en/cognition.md +34 -0
- package/docs/en/guide.md +8 -6
- package/docs/zh/CHANGELOG.md +44 -24
- package/docs/zh/architecture.md +4 -0
- package/docs/zh/cognition.md +34 -0
- package/docs/zh/guide.md +8 -6
- package/package.json +112 -112
- package/public/app/assets/ChatsPage-DH4ZPRXB.js +3 -0
- package/public/app/assets/{Empty-C7yzOBzy.js → Empty-DRsHeX-L.js} +1 -1
- package/public/app/assets/Field-BfIUcaNa.js +1 -0
- package/public/app/assets/HeartPage-BxgSUJJ_.js +1 -0
- package/public/app/assets/LifePage-CAKr8h_q.js +1 -0
- package/public/app/assets/MemoryPage-CFq8L_TC.js +1 -0
- package/public/app/assets/NaturePage-BU1iDno3.js +3 -0
- package/public/app/assets/PeoplePage-C6i_nAfr.js +1 -0
- package/public/app/assets/PluginsPage-DRV8lzk3.js +2 -0
- package/public/app/assets/SystemPage-21AlQBiC.js +1 -0
- package/public/app/assets/SystemPage-CMcuUdLm.css +1 -0
- package/public/app/assets/TimePage-DUzjLnZV.js +3 -0
- package/public/app/assets/index-WAvrlAcO.js +4 -0
- package/public/app/assets/{knowledge-BJOIyuJo.js → knowledge-V_blbx9R.js} +1 -1
- package/public/app/assets/media-C7j5HqxK.js +1 -0
- package/public/app/assets/{mind-DtTVrW6Z.js → mind-BBAstMqH.js} +1 -1
- package/public/app/index.html +3 -3
- package/public/guide.en.html +13 -5
- package/public/guide.html +12 -5
- package/public/tutorial.en.md +3 -1
- package/public/tutorial.md +3 -1
- package/server/core/backoff.js +123 -0
- package/server/core/context-builder.js +307 -288
- package/server/core/contribution-check.js +50 -0
- package/server/core/conversation-cues.js +377 -385
- package/server/core/conversation-grounding.js +547 -304
- package/server/core/dialogue-context.js +247 -244
- package/server/core/dialogue-contract.js +33 -9
- package/server/core/embedding.js +246 -0
- package/server/core/expression-strategy.js +90 -0
- package/server/core/initiative-context.js +88 -68
- package/server/core/model-json.js +223 -190
- package/server/core/model-manager.js +256 -56
- package/server/core/model-presets.js +1045 -900
- package/server/core/orchestrator.js +41 -3
- package/server/core/persona-manager.js +364 -359
- package/server/core/reader-check.js +166 -62
- package/server/core/reply-delivery.js +688 -537
- package/server/core/repository.js +36 -0
- package/server/core/response-generator.js +101 -92
- package/server/core/response-validator.js +18 -3
- package/server/core/retired-prompts.js +53 -51
- package/server/core/time-expressions.js +143 -0
- package/server/core/turn-processor.js +175 -5
- package/server/core/turn.js +299 -44
- package/server/core/understanding.js +85 -0
- package/server/core/working-memory.js +149 -0
- package/server/i18n/decisions.js +225 -209
- package/server/i18n/errors.js +14 -0
- package/server/i18n/models.js +120 -111
- package/server/knowledge/api.js +362 -342
- package/server/knowledge/embedding-profile.js +158 -114
- package/server/knowledge/manager.js +6 -1
- package/server/mind/api.js +68 -0
- package/server/mind/continuity.js +634 -562
- package/server/mind/day-planner.js +52 -3
- package/server/mind/episodes.js +242 -0
- package/server/mind/expression-grounding.js +24 -21
- package/server/mind/index.js +13 -2
- package/server/mind/initiative.js +356 -338
- package/server/mind/life/index.js +6 -0
- package/server/mind/meetings.js +1112 -1069
- package/server/mind/memory.js +1091 -888
- package/server/mind/recall/index.js +776 -0
- package/server/mind/recall/schema.js +132 -0
- package/server/mind/recall/sources.js +220 -0
- package/server/mind/recall/store.js +368 -0
- package/server/mind/salience.js +202 -201
- package/server/mind/schema.js +568 -567
- package/server/mind/self.js +537 -496
- package/server/mind/time/activity-recall.js +65 -0
- package/server/mind/time/index.js +6 -2
- package/server/mind/time/tasks.js +36 -10
- package/server/mind/time/works.js +528 -527
- package/server/mind/view.js +63 -17
- package/server/storage/archive.js +1 -1
- package/public/app/assets/ChatsPage-CshdtTzV.js +0 -3
- package/public/app/assets/Field-BjFv4Rfn.js +0 -1
- package/public/app/assets/HeartPage-eqo6ru16.js +0 -1
- package/public/app/assets/LifePage-CN8GE5NK.js +0 -1
- package/public/app/assets/MemoryPage-CCpMN4Wg.js +0 -1
- package/public/app/assets/NaturePage-C0JqdBTR.js +0 -3
- package/public/app/assets/PeoplePage-BXGBbO4u.js +0 -1
- package/public/app/assets/PluginsPage-Dksp5AV5.js +0 -2
- package/public/app/assets/SystemPage-BenIyTvk.css +0 -1
- package/public/app/assets/SystemPage-Cd7aa8eE.js +0 -1
- package/public/app/assets/TimePage-Bs_zImb4.js +0 -3
- package/public/app/assets/index-CyRtYxP3.js +0 -4
- package/public/app/assets/media-Bwtx8blz.js +0 -1
|
@@ -54,6 +54,26 @@ const CIRCUIT_FAILURES = 3;
|
|
|
54
54
|
const CIRCUIT_BASE_MS = 30000;
|
|
55
55
|
const CIRCUIT_MAX_MS = 120000;
|
|
56
56
|
const FALLBACK_STATUS = new Set([401, 402, 403, 404, ...RETRYABLE_STATUS]);
|
|
57
|
+
// A bad key, an account without credit, a refused tier or a missing model will
|
|
58
|
+
// not fix itself in a minute. Such a profile leaves the backup list for hours
|
|
59
|
+
// instead of being asked, and billed for a failure, on every step.
|
|
60
|
+
const UNAVAILABLE_STATUS = new Set([401, 402, 403, 404]);
|
|
61
|
+
export const UNAVAILABLE_MS = 6 * 3600000;
|
|
62
|
+
// Her own life and upkeep wait behind the conversation in front of her.
|
|
63
|
+
export const BACKGROUND_STAGES = new Set([
|
|
64
|
+
"memory",
|
|
65
|
+
"summary",
|
|
66
|
+
"reflection",
|
|
67
|
+
"expression",
|
|
68
|
+
"expression_novelty",
|
|
69
|
+
"expression_grounding",
|
|
70
|
+
"daily",
|
|
71
|
+
"weekly",
|
|
72
|
+
"plugin",
|
|
73
|
+
]);
|
|
74
|
+
const BACKGROUND_RATE_PAUSE_MS = 60000;
|
|
75
|
+
export const stagePriority = (stage) =>
|
|
76
|
+
BACKGROUND_STAGES.has(stage) ? "background" : "foreground";
|
|
57
77
|
|
|
58
78
|
export function defaultModel(s) {
|
|
59
79
|
return {
|
|
@@ -214,7 +234,7 @@ function applyGenerationControls(
|
|
|
214
234
|
} else if (style === "kimi-effort") {
|
|
215
235
|
if (!off) body.reasoning_effort = profile.reasoningEffort;
|
|
216
236
|
} else if (style === "volcengine") {
|
|
217
|
-
//
|
|
237
|
+
// Volcengine's Responses endpoint accepts reasoning.effort. Keep sampling
|
|
218
238
|
// parameters out of these reasoning models; "none" uses minimal effort.
|
|
219
239
|
body.reasoning_effort =
|
|
220
240
|
off || QUIET_STAGES.has(stage)
|
|
@@ -491,6 +511,7 @@ const REUSABLE_KEYS = ["memories", "knowledge"];
|
|
|
491
511
|
const VOLATILE_KEYS = [
|
|
492
512
|
"quoted",
|
|
493
513
|
"recalled",
|
|
514
|
+
"working",
|
|
494
515
|
"speakers",
|
|
495
516
|
"watermark",
|
|
496
517
|
"batchIds",
|
|
@@ -781,9 +802,20 @@ export function isTransientModelFailure(error) {
|
|
|
781
802
|
if (RETRYABLE_STATUS.has(Number(error.status))) return true;
|
|
782
803
|
return isTransientNetworkError(error) || isTimeoutError(error);
|
|
783
804
|
}
|
|
805
|
+
// A refusal about the account rather than this request. A 403 can also be a
|
|
806
|
+
// content decision about one message, which must not take the model away.
|
|
807
|
+
export function accountRefusal(error) {
|
|
808
|
+
const status = Number(error?.status);
|
|
809
|
+
if (!UNAVAILABLE_STATUS.has(status)) return false;
|
|
810
|
+
if (status === 401 || status === 402) return true;
|
|
811
|
+
return /tier|quota|balance|credit|fund|余额|额度|permission|not allowed|access|subscription|model.{0,20}(?:not|does not) exist|not found|不存在|无权/i.test(
|
|
812
|
+
String(error?.detail || error?.message || ""),
|
|
813
|
+
);
|
|
814
|
+
}
|
|
784
815
|
export function shouldFallback(error) {
|
|
785
816
|
if (!error || error instanceof SyntaxError) return false;
|
|
786
817
|
if (error.circuitOpen || error.timeout || error.networkFailure) return true;
|
|
818
|
+
if (error.unavailable) return true;
|
|
787
819
|
if (FALLBACK_STATUS.has(Number(error.status))) return true;
|
|
788
820
|
return isTransientNetworkError(error) || isTimeoutError(error);
|
|
789
821
|
}
|
|
@@ -808,15 +840,20 @@ export function withFallback(models, fallback) {
|
|
|
808
840
|
);
|
|
809
841
|
} catch (error) {
|
|
810
842
|
let last = error;
|
|
843
|
+
// Background work tries one backup at most; a failing afternoon of
|
|
844
|
+
// upkeep must not walk the whole library on every attempt.
|
|
845
|
+
let hops = BACKGROUND_STAGES.has(stage) ? 1 : chain.length;
|
|
811
846
|
for (const next of chain) {
|
|
812
847
|
if (
|
|
813
848
|
!next ||
|
|
814
849
|
next.id === profile?.id ||
|
|
815
850
|
stage === "test" ||
|
|
816
851
|
!shouldFallback(last) ||
|
|
817
|
-
(images?.length && !next.vision)
|
|
852
|
+
(images?.length && !next.vision) ||
|
|
853
|
+
models.available?.(next) === false
|
|
818
854
|
)
|
|
819
855
|
continue;
|
|
856
|
+
if (hops-- <= 0) break;
|
|
820
857
|
trace?.steps?.push(
|
|
821
858
|
`主模型 ${profile?.label || profile?.model} 请求失败(${String(last.message || "").slice(0, 120)}),这一步改用备用模型 ${next.label || next.model}`,
|
|
822
859
|
);
|
|
@@ -850,7 +887,49 @@ export class ModelManager {
|
|
|
850
887
|
this.maxConcurrent = Math.max(1, Number(maxConcurrent) || 3);
|
|
851
888
|
this.requestLanes = new Map();
|
|
852
889
|
this.originCooldowns = new Map();
|
|
890
|
+
this.backgroundPauses = new Map();
|
|
853
891
|
this.circuits = new Map();
|
|
892
|
+
this.unavailable = new Map();
|
|
893
|
+
}
|
|
894
|
+
healthKey(profile) {
|
|
895
|
+
let origin = "";
|
|
896
|
+
try {
|
|
897
|
+
origin = new URL(profile.baseUrl).origin;
|
|
898
|
+
} catch {
|
|
899
|
+
origin = String(profile.baseUrl || "");
|
|
900
|
+
}
|
|
901
|
+
const key = String(profile.apiKey || "");
|
|
902
|
+
// A changed key or address is a new attempt, not the same refusal.
|
|
903
|
+
return `${origin}|${profile.model}|${key.length}:${key.slice(-6)}`;
|
|
904
|
+
}
|
|
905
|
+
available(profile, now = Date.now()) {
|
|
906
|
+
const state = this.unavailable.get(this.healthKey(profile));
|
|
907
|
+
if (!state) return true;
|
|
908
|
+
if (state.until <= now) {
|
|
909
|
+
this.unavailable.delete(this.healthKey(profile));
|
|
910
|
+
return true;
|
|
911
|
+
}
|
|
912
|
+
return false;
|
|
913
|
+
}
|
|
914
|
+
markUnavailable(profile, error, now = Date.now()) {
|
|
915
|
+
this.unavailable.set(this.healthKey(profile), {
|
|
916
|
+
until: now + UNAVAILABLE_MS,
|
|
917
|
+
status: Number(error.status) || null,
|
|
918
|
+
reason: String(error.message || "").slice(0, 200),
|
|
919
|
+
label: profile.label || profile.model,
|
|
920
|
+
});
|
|
921
|
+
}
|
|
922
|
+
health(now = Date.now()) {
|
|
923
|
+
return [...this.unavailable.entries()]
|
|
924
|
+
.filter(([, state]) => state.until > now)
|
|
925
|
+
.map(([key, state]) => ({ key, ...state }));
|
|
926
|
+
}
|
|
927
|
+
pauseBackground(endpoint, delayMs) {
|
|
928
|
+
const key = new URL(endpoint).origin;
|
|
929
|
+
this.backgroundPauses.set(
|
|
930
|
+
key,
|
|
931
|
+
Math.max(this.backgroundPauses.get(key) || 0, Date.now() + delayMs),
|
|
932
|
+
);
|
|
854
933
|
}
|
|
855
934
|
deferOrigin(endpoint, delayMs) {
|
|
856
935
|
const key = new URL(endpoint).origin;
|
|
@@ -861,16 +940,40 @@ export class ModelManager {
|
|
|
861
940
|
Math.max(this.originCooldowns.get(key) || 0, Date.now() + delay),
|
|
862
941
|
);
|
|
863
942
|
}
|
|
864
|
-
|
|
943
|
+
// One lane per gateway. The conversation goes first; background work may
|
|
944
|
+
// hold all but one slot, so a reply never waits behind her own afternoon.
|
|
945
|
+
// `lane` separates request kinds that should not queue behind each other.
|
|
946
|
+
async withRequestSlot(
|
|
947
|
+
endpoint,
|
|
948
|
+
request,
|
|
949
|
+
{ priority = "foreground", lane: laneName = "" } = {},
|
|
950
|
+
) {
|
|
865
951
|
const key = new URL(endpoint).origin;
|
|
866
|
-
|
|
952
|
+
const background = priority === "background";
|
|
953
|
+
if (background)
|
|
954
|
+
while (true) {
|
|
955
|
+
const remaining = (this.backgroundPauses.get(key) || 0) - Date.now();
|
|
956
|
+
if (remaining <= 0) break;
|
|
957
|
+
await new Promise((resolve) => setTimeout(resolve, remaining));
|
|
958
|
+
}
|
|
959
|
+
const laneKey = laneName ? `${key}#${laneName}` : key;
|
|
960
|
+
let lane = this.requestLanes.get(laneKey);
|
|
867
961
|
if (!lane) {
|
|
868
|
-
lane = { active: 0, waiting: [] };
|
|
869
|
-
this.requestLanes.set(
|
|
962
|
+
lane = { active: 0, background: 0, waiting: [], waitingBackground: [] };
|
|
963
|
+
this.requestLanes.set(laneKey, lane);
|
|
870
964
|
}
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
965
|
+
const backgroundLimit = Math.max(1, this.maxConcurrent - 1);
|
|
966
|
+
const free = () =>
|
|
967
|
+
lane.active < this.maxConcurrent &&
|
|
968
|
+
(!background ||
|
|
969
|
+
(lane.background < backgroundLimit && !lane.waiting.length));
|
|
970
|
+
if (free()) {
|
|
971
|
+
lane.active++;
|
|
972
|
+
if (background) lane.background++;
|
|
973
|
+
} else
|
|
974
|
+
await new Promise((resolve) =>
|
|
975
|
+
(background ? lane.waitingBackground : lane.waiting).push(resolve),
|
|
976
|
+
);
|
|
874
977
|
try {
|
|
875
978
|
while (true) {
|
|
876
979
|
const blockedUntil = this.originCooldowns.get(key) || 0;
|
|
@@ -884,11 +987,19 @@ export class ModelManager {
|
|
|
884
987
|
return await request();
|
|
885
988
|
} finally {
|
|
886
989
|
lane.active--;
|
|
990
|
+
if (background) lane.background--;
|
|
887
991
|
const next = lane.waiting.shift();
|
|
888
992
|
if (next) {
|
|
889
993
|
lane.active++;
|
|
890
994
|
next();
|
|
891
|
-
} else if (
|
|
995
|
+
} else if (
|
|
996
|
+
lane.waitingBackground.length &&
|
|
997
|
+
lane.background < backgroundLimit
|
|
998
|
+
) {
|
|
999
|
+
lane.active++;
|
|
1000
|
+
lane.background++;
|
|
1001
|
+
lane.waitingBackground.shift()();
|
|
1002
|
+
} else if (!lane.active) this.requestLanes.delete(laneKey);
|
|
892
1003
|
}
|
|
893
1004
|
}
|
|
894
1005
|
// Consecutive failures of one model on one gateway pause further requests
|
|
@@ -999,6 +1110,18 @@ export class ModelManager {
|
|
|
999
1110
|
);
|
|
1000
1111
|
const key = profile.apiKey || process.env.LLM_API_KEY;
|
|
1001
1112
|
if (!key) throw Error("模型尚未配置 API Key");
|
|
1113
|
+
if (stage !== "test" && !this.available(profile)) {
|
|
1114
|
+
const state = this.unavailable.get(this.healthKey(profile));
|
|
1115
|
+
const label = profile.label || profile.model;
|
|
1116
|
+
const minutes = Math.ceil((state.until - Date.now()) / 60000);
|
|
1117
|
+
throw Object.assign(
|
|
1118
|
+
new Error(
|
|
1119
|
+
`${label} 暂时不可用(${state.reason}),约 ${minutes} 分钟后再试`,
|
|
1120
|
+
),
|
|
1121
|
+
{ unavailable: true, status: state?.status ?? null },
|
|
1122
|
+
);
|
|
1123
|
+
}
|
|
1124
|
+
const priority = stagePriority(stage);
|
|
1002
1125
|
const outputCap = options.fullOutput
|
|
1003
1126
|
? profile.maxOutputTokens
|
|
1004
1127
|
: outputLimit(profile, stage);
|
|
@@ -1051,32 +1174,45 @@ export class ModelManager {
|
|
|
1051
1174
|
try {
|
|
1052
1175
|
raw = await withTransientRequestRetry(
|
|
1053
1176
|
() =>
|
|
1054
|
-
this.withRequestSlot(
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
r
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1177
|
+
this.withRequestSlot(
|
|
1178
|
+
endpoint,
|
|
1179
|
+
async () => {
|
|
1180
|
+
const started = Date.now();
|
|
1181
|
+
let r;
|
|
1182
|
+
try {
|
|
1183
|
+
r = await this.fetcher(endpoint, {
|
|
1184
|
+
method: "POST",
|
|
1185
|
+
headers,
|
|
1186
|
+
body: payload,
|
|
1187
|
+
signal: AbortSignal.timeout(profile.timeoutMs),
|
|
1188
|
+
});
|
|
1189
|
+
} catch (error) {
|
|
1190
|
+
if (error && typeof error === "object")
|
|
1191
|
+
error.attemptElapsed = Date.now() - started;
|
|
1192
|
+
throw error;
|
|
1193
|
+
}
|
|
1194
|
+
if (!r.ok) {
|
|
1195
|
+
const error = await httpError(r);
|
|
1066
1196
|
error.attemptElapsed = Date.now() - started;
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1197
|
+
if (error.retryableRequest)
|
|
1198
|
+
this.deferOrigin(endpoint, error.retryDelayMs);
|
|
1199
|
+
if (error.status === 429)
|
|
1200
|
+
this.pauseBackground(
|
|
1201
|
+
endpoint,
|
|
1202
|
+
Math.max(
|
|
1203
|
+
BACKGROUND_RATE_PAUSE_MS,
|
|
1204
|
+
error.retryDelayMs * 4,
|
|
1205
|
+
),
|
|
1206
|
+
);
|
|
1207
|
+
throw error;
|
|
1208
|
+
}
|
|
1209
|
+
return await r.json();
|
|
1210
|
+
},
|
|
1211
|
+
{ priority },
|
|
1212
|
+
),
|
|
1078
1213
|
{
|
|
1079
|
-
|
|
1214
|
+
// A rate-limited gateway is not hammered by her own upkeep.
|
|
1215
|
+
retries: priority === "background" ? 1 : 3,
|
|
1080
1216
|
onRetry: (count, error, waitMs) =>
|
|
1081
1217
|
this.noteRetry(entry, endpoint, count, error, waitMs),
|
|
1082
1218
|
},
|
|
@@ -1084,6 +1220,8 @@ export class ModelManager {
|
|
|
1084
1220
|
} catch (error) {
|
|
1085
1221
|
if (guarded && isAvailabilityFailure(error))
|
|
1086
1222
|
this.recordFailure(circuit);
|
|
1223
|
+
if (guarded && accountRefusal(error))
|
|
1224
|
+
this.markUnavailable(profile, error);
|
|
1087
1225
|
throw requestFailure(error, entry, profile);
|
|
1088
1226
|
}
|
|
1089
1227
|
if (guarded) this.recordSuccess(circuit);
|
|
@@ -1134,7 +1272,7 @@ export class ModelManager {
|
|
|
1134
1272
|
return await this.call(
|
|
1135
1273
|
profile,
|
|
1136
1274
|
stage,
|
|
1137
|
-
system
|
|
1275
|
+
`${system}\n上一份输出未通过 JSON 解析,请重新组织同一任务的结果:只输出一个完整的 JSON 值。同一对象的每个字段只能写一次,多条 bubbles 合并到同一个数组,不用后一个字段覆盖前面的回答。`,
|
|
1138
1276
|
data,
|
|
1139
1277
|
trace,
|
|
1140
1278
|
images,
|
|
@@ -1151,32 +1289,94 @@ export class ModelManager {
|
|
|
1151
1289
|
entry.elapsed = Date.now() - entry.started;
|
|
1152
1290
|
}
|
|
1153
1291
|
}
|
|
1154
|
-
|
|
1292
|
+
// Vectors travel in their own lane: a reply waiting for a recall vector must
|
|
1293
|
+
// not queue behind long chat completions on the same gateway. Two request
|
|
1294
|
+
// shapes exist: OpenAI-compatible batches, and multimodal endpoints that
|
|
1295
|
+
// take one item per request.
|
|
1296
|
+
async embed(
|
|
1297
|
+
profile,
|
|
1298
|
+
texts,
|
|
1299
|
+
{
|
|
1300
|
+
priority = "foreground",
|
|
1301
|
+
timeoutMs = profile.timeoutMs || 90000,
|
|
1302
|
+
onUsage,
|
|
1303
|
+
} = {},
|
|
1304
|
+
) {
|
|
1155
1305
|
const key = profile.isolatedEmbeddingKey
|
|
1156
1306
|
? process.env.EMBEDDING_API_KEY || profile.apiKey
|
|
1157
1307
|
: profile.apiKey || process.env.LLM_API_KEY;
|
|
1158
1308
|
if (!key) throw Error("模型尚未配置 API Key");
|
|
1159
1309
|
const model = profile.embeddingModel || profile.model;
|
|
1160
|
-
const
|
|
1161
|
-
const
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1310
|
+
const multimodal = profile.protocol === "multimodal";
|
|
1311
|
+
const endpoint =
|
|
1312
|
+
profile.baseUrl.replace(/\/$/, "") +
|
|
1313
|
+
(multimodal ? "/embeddings/multimodal" : "/embeddings");
|
|
1314
|
+
const circuit = `${new URL(endpoint).origin}|embed:${model}`;
|
|
1315
|
+
this.checkCircuit(circuit, {});
|
|
1316
|
+
const send = (input) =>
|
|
1317
|
+
withTransientRequestRetry(
|
|
1318
|
+
() =>
|
|
1319
|
+
this.withRequestSlot(
|
|
1320
|
+
endpoint,
|
|
1321
|
+
async () => {
|
|
1322
|
+
const r = await this.fetcher(endpoint, {
|
|
1323
|
+
method: "POST",
|
|
1324
|
+
headers: {
|
|
1325
|
+
"Content-Type": "application/json",
|
|
1326
|
+
Authorization: `Bearer ${key}`,
|
|
1327
|
+
},
|
|
1328
|
+
body: JSON.stringify({
|
|
1329
|
+
model,
|
|
1330
|
+
input,
|
|
1331
|
+
encoding_format: "float",
|
|
1332
|
+
...(profile.dimensions
|
|
1333
|
+
? { dimensions: profile.dimensions }
|
|
1334
|
+
: {}),
|
|
1335
|
+
}),
|
|
1336
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
1337
|
+
});
|
|
1338
|
+
if (!r.ok) {
|
|
1339
|
+
const error = await httpError(r);
|
|
1340
|
+
error.message = `向量接口失败 HTTP ${r.status}`;
|
|
1341
|
+
const limit = String(error.detail || "").match(
|
|
1342
|
+
/max\s*(\d+)\s*,\s*got/i,
|
|
1343
|
+
);
|
|
1344
|
+
if (limit) error.batchLimit = Number(limit[1]);
|
|
1345
|
+
throw error;
|
|
1346
|
+
}
|
|
1347
|
+
return await r.json();
|
|
1348
|
+
},
|
|
1349
|
+
{ priority, lane: "embedding" },
|
|
1350
|
+
),
|
|
1351
|
+
{ retries: priority === "background" ? 2 : 1, deadlineMs: timeoutMs },
|
|
1352
|
+
);
|
|
1353
|
+
let raw;
|
|
1354
|
+
try {
|
|
1355
|
+
raw = multimodal
|
|
1356
|
+
? await Promise.all(
|
|
1357
|
+
texts.map((text) => send([{ type: "text", text }])),
|
|
1358
|
+
).then((answers) => ({
|
|
1359
|
+
data: answers.map((answer, index) => ({
|
|
1360
|
+
index,
|
|
1361
|
+
embedding: Array.isArray(answer.data)
|
|
1362
|
+
? answer.data[0]?.embedding
|
|
1363
|
+
: answer.data?.embedding,
|
|
1364
|
+
})),
|
|
1365
|
+
usage: {
|
|
1366
|
+
total_tokens: answers.reduce(
|
|
1367
|
+
(sum, answer) =>
|
|
1368
|
+
sum + (Number(answer.usage?.total_tokens) || 0),
|
|
1369
|
+
0,
|
|
1370
|
+
),
|
|
1371
|
+
},
|
|
1372
|
+
}))
|
|
1373
|
+
: await send(texts);
|
|
1374
|
+
} catch (error) {
|
|
1375
|
+
if (isAvailabilityFailure(error)) this.recordFailure(circuit);
|
|
1376
|
+
throw requestFailure(error, {}, { timeoutMs });
|
|
1377
|
+
}
|
|
1378
|
+
this.recordSuccess(circuit);
|
|
1379
|
+
onUsage?.(raw.usage || null);
|
|
1180
1380
|
const rows = Array.isArray(raw.data) ? raw.data : [];
|
|
1181
1381
|
if (rows.length !== texts.length)
|
|
1182
1382
|
throw Error("向量接口返回数量与输入不一致");
|