@fallom/trace 0.2.4 → 0.2.6
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 +66 -2
- package/dist/{chunk-W6M2RQ3W.mjs → chunk-KFD5AQ7V.mjs} +59 -2
- package/dist/index.d.mts +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +59 -2
- package/dist/index.mjs +2 -2
- package/dist/{models-JKMOBZUO.mjs → models-SEFDGZU2.mjs} +1 -1
- package/package.json +1 -1
- package/dist/chunk-2BP4H4AD.mjs +0 -3012
- package/dist/chunk-6MSTRIK4.mjs +0 -255
- package/dist/chunk-7P6ASYW6.mjs +0 -9
- package/dist/chunk-H2EACSBT.mjs +0 -255
- package/dist/chunk-IGJD7GBO.mjs +0 -248
- package/dist/chunk-K7HYYE4Y.mjs +0 -2930
- package/dist/chunk-KAZ5NEU2.mjs +0 -2237
- package/dist/chunk-KMA4IPED.mjs +0 -252
- package/dist/chunk-VNUUS74T.mjs +0 -242
- package/dist/models-2Y6DRQPS.mjs +0 -9
- package/dist/models-BUHMMTWK.mjs +0 -9
- package/dist/models-JIO5LVMB.mjs +0 -8
- package/dist/prompts-67DJ33I4.mjs +0 -14
- package/dist/prompts-ODF4KO2E.mjs +0 -14
- package/dist/prompts-VAN5E3L4.mjs +0 -14
- package/dist/prompts-XSZHTCX7.mjs +0 -15
- package/dist/prompts-ZSLS4DHO.mjs +0 -14
package/dist/chunk-KMA4IPED.mjs
DELETED
|
@@ -1,252 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
__export
|
|
3
|
-
} from "./chunk-7P6ASYW6.mjs";
|
|
4
|
-
|
|
5
|
-
// src/prompts.ts
|
|
6
|
-
var prompts_exports = {};
|
|
7
|
-
__export(prompts_exports, {
|
|
8
|
-
clearPromptContext: () => clearPromptContext,
|
|
9
|
-
get: () => get,
|
|
10
|
-
getAB: () => getAB,
|
|
11
|
-
getPromptContext: () => getPromptContext,
|
|
12
|
-
init: () => init
|
|
13
|
-
});
|
|
14
|
-
import { createHash } from "crypto";
|
|
15
|
-
var apiKey = null;
|
|
16
|
-
var baseUrl = "https://prompts.fallom.com";
|
|
17
|
-
var initialized = false;
|
|
18
|
-
var syncInterval = null;
|
|
19
|
-
var debugMode = false;
|
|
20
|
-
var promptCache = /* @__PURE__ */ new Map();
|
|
21
|
-
var promptABCache = /* @__PURE__ */ new Map();
|
|
22
|
-
var promptContext = null;
|
|
23
|
-
var SYNC_TIMEOUT = 2e3;
|
|
24
|
-
function log(msg) {
|
|
25
|
-
if (debugMode) {
|
|
26
|
-
console.log(`[Fallom Prompts] ${msg}`);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
function init(options = {}) {
|
|
30
|
-
apiKey = options.apiKey || process.env.FALLOM_API_KEY || null;
|
|
31
|
-
baseUrl = options.baseUrl || process.env.FALLOM_PROMPTS_URL || process.env.FALLOM_BASE_URL || "https://prompts.fallom.com";
|
|
32
|
-
initialized = true;
|
|
33
|
-
if (!apiKey) {
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
fetchAll().catch(() => {
|
|
37
|
-
});
|
|
38
|
-
if (!syncInterval) {
|
|
39
|
-
syncInterval = setInterval(() => {
|
|
40
|
-
fetchAll().catch(() => {
|
|
41
|
-
});
|
|
42
|
-
}, 3e4);
|
|
43
|
-
syncInterval.unref();
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
function ensureInit() {
|
|
47
|
-
if (!initialized) {
|
|
48
|
-
try {
|
|
49
|
-
init();
|
|
50
|
-
} catch {
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
async function fetchAll() {
|
|
55
|
-
await Promise.all([fetchPrompts(), fetchPromptABTests()]);
|
|
56
|
-
}
|
|
57
|
-
async function fetchPrompts(timeout = SYNC_TIMEOUT) {
|
|
58
|
-
if (!apiKey) return;
|
|
59
|
-
try {
|
|
60
|
-
const controller = new AbortController();
|
|
61
|
-
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
62
|
-
const resp = await fetch(`${baseUrl}/prompts`, {
|
|
63
|
-
headers: { Authorization: `Bearer ${apiKey}` },
|
|
64
|
-
signal: controller.signal
|
|
65
|
-
});
|
|
66
|
-
clearTimeout(timeoutId);
|
|
67
|
-
if (resp.ok) {
|
|
68
|
-
const data = await resp.json();
|
|
69
|
-
for (const p of data.prompts || []) {
|
|
70
|
-
if (!promptCache.has(p.key)) {
|
|
71
|
-
promptCache.set(p.key, { versions: /* @__PURE__ */ new Map(), current: null });
|
|
72
|
-
}
|
|
73
|
-
const cached = promptCache.get(p.key);
|
|
74
|
-
cached.versions.set(p.version, {
|
|
75
|
-
systemPrompt: p.system_prompt,
|
|
76
|
-
userTemplate: p.user_template
|
|
77
|
-
});
|
|
78
|
-
cached.current = p.version;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
} catch {
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
async function fetchPromptABTests(timeout = SYNC_TIMEOUT) {
|
|
85
|
-
if (!apiKey) return;
|
|
86
|
-
try {
|
|
87
|
-
const controller = new AbortController();
|
|
88
|
-
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
89
|
-
const resp = await fetch(`${baseUrl}/prompt-ab-tests`, {
|
|
90
|
-
headers: { Authorization: `Bearer ${apiKey}` },
|
|
91
|
-
signal: controller.signal
|
|
92
|
-
});
|
|
93
|
-
clearTimeout(timeoutId);
|
|
94
|
-
if (resp.ok) {
|
|
95
|
-
const data = await resp.json();
|
|
96
|
-
for (const t of data.prompt_ab_tests || []) {
|
|
97
|
-
if (!promptABCache.has(t.key)) {
|
|
98
|
-
promptABCache.set(t.key, { versions: /* @__PURE__ */ new Map(), current: null });
|
|
99
|
-
}
|
|
100
|
-
const cached = promptABCache.get(t.key);
|
|
101
|
-
cached.versions.set(t.version, { variants: t.variants });
|
|
102
|
-
cached.current = t.version;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
} catch {
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
function replaceVariables(template, variables) {
|
|
109
|
-
if (!variables) return template;
|
|
110
|
-
return template.replace(/\{\{(\s*\w+\s*)\}\}/g, (match, varName) => {
|
|
111
|
-
const key = varName.trim();
|
|
112
|
-
return key in variables ? String(variables[key]) : match;
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
function setPromptContext(ctx) {
|
|
116
|
-
promptContext = ctx;
|
|
117
|
-
}
|
|
118
|
-
function getPromptContext() {
|
|
119
|
-
const ctx = promptContext;
|
|
120
|
-
promptContext = null;
|
|
121
|
-
return ctx;
|
|
122
|
-
}
|
|
123
|
-
async function get(promptKey, options = {}) {
|
|
124
|
-
const { variables, version, debug = false } = options;
|
|
125
|
-
debugMode = debug;
|
|
126
|
-
ensureInit();
|
|
127
|
-
log(`get() called: promptKey=${promptKey}`);
|
|
128
|
-
let promptData = promptCache.get(promptKey);
|
|
129
|
-
if (!promptData) {
|
|
130
|
-
log("Not in cache, fetching...");
|
|
131
|
-
await fetchPrompts(SYNC_TIMEOUT);
|
|
132
|
-
promptData = promptCache.get(promptKey);
|
|
133
|
-
}
|
|
134
|
-
if (!promptData) {
|
|
135
|
-
throw new Error(
|
|
136
|
-
`Prompt '${promptKey}' not found. Check that it exists in your Fallom dashboard.`
|
|
137
|
-
);
|
|
138
|
-
}
|
|
139
|
-
const targetVersion = version ?? promptData.current;
|
|
140
|
-
const content = promptData.versions.get(targetVersion);
|
|
141
|
-
if (!content) {
|
|
142
|
-
throw new Error(
|
|
143
|
-
`Prompt '${promptKey}' version ${targetVersion} not found.`
|
|
144
|
-
);
|
|
145
|
-
}
|
|
146
|
-
const system = replaceVariables(content.systemPrompt, variables);
|
|
147
|
-
const user = replaceVariables(content.userTemplate, variables);
|
|
148
|
-
setPromptContext({
|
|
149
|
-
promptKey,
|
|
150
|
-
promptVersion: targetVersion
|
|
151
|
-
});
|
|
152
|
-
log(`\u2705 Got prompt: ${promptKey} v${targetVersion}`);
|
|
153
|
-
return {
|
|
154
|
-
key: promptKey,
|
|
155
|
-
version: targetVersion,
|
|
156
|
-
system,
|
|
157
|
-
user
|
|
158
|
-
};
|
|
159
|
-
}
|
|
160
|
-
async function getAB(abTestKey, sessionId, options = {}) {
|
|
161
|
-
const { variables, debug = false } = options;
|
|
162
|
-
debugMode = debug;
|
|
163
|
-
ensureInit();
|
|
164
|
-
log(`getAB() called: abTestKey=${abTestKey}, sessionId=${sessionId}`);
|
|
165
|
-
let abData = promptABCache.get(abTestKey);
|
|
166
|
-
if (!abData) {
|
|
167
|
-
log("Not in cache, fetching...");
|
|
168
|
-
await fetchPromptABTests(SYNC_TIMEOUT);
|
|
169
|
-
abData = promptABCache.get(abTestKey);
|
|
170
|
-
}
|
|
171
|
-
if (!abData) {
|
|
172
|
-
throw new Error(
|
|
173
|
-
`Prompt A/B test '${abTestKey}' not found. Check that it exists in your Fallom dashboard.`
|
|
174
|
-
);
|
|
175
|
-
}
|
|
176
|
-
const currentVersion = abData.current;
|
|
177
|
-
const versionData = abData.versions.get(currentVersion);
|
|
178
|
-
if (!versionData) {
|
|
179
|
-
throw new Error(`Prompt A/B test '${abTestKey}' has no current version.`);
|
|
180
|
-
}
|
|
181
|
-
const { variants } = versionData;
|
|
182
|
-
log(`A/B test '${abTestKey}' has ${variants?.length ?? 0} variants`);
|
|
183
|
-
log(`Version data: ${JSON.stringify(versionData, null, 2)}`);
|
|
184
|
-
if (!variants || variants.length === 0) {
|
|
185
|
-
throw new Error(
|
|
186
|
-
`Prompt A/B test '${abTestKey}' has no variants configured.`
|
|
187
|
-
);
|
|
188
|
-
}
|
|
189
|
-
const hashBytes = createHash("md5").update(sessionId).digest();
|
|
190
|
-
const hashVal = hashBytes.readUInt32BE(0) % 1e6;
|
|
191
|
-
let cumulative = 0;
|
|
192
|
-
let selectedVariant = variants[variants.length - 1];
|
|
193
|
-
let selectedIndex = variants.length - 1;
|
|
194
|
-
for (let i = 0; i < variants.length; i++) {
|
|
195
|
-
cumulative += variants[i].weight * 1e4;
|
|
196
|
-
if (hashVal < cumulative) {
|
|
197
|
-
selectedVariant = variants[i];
|
|
198
|
-
selectedIndex = i;
|
|
199
|
-
break;
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
const promptKey = selectedVariant.prompt_key;
|
|
203
|
-
const promptVersion = selectedVariant.prompt_version;
|
|
204
|
-
let promptData = promptCache.get(promptKey);
|
|
205
|
-
if (!promptData) {
|
|
206
|
-
await fetchPrompts(SYNC_TIMEOUT);
|
|
207
|
-
promptData = promptCache.get(promptKey);
|
|
208
|
-
}
|
|
209
|
-
if (!promptData) {
|
|
210
|
-
throw new Error(
|
|
211
|
-
`Prompt '${promptKey}' (from A/B test '${abTestKey}') not found.`
|
|
212
|
-
);
|
|
213
|
-
}
|
|
214
|
-
const targetVersion = promptVersion ?? promptData.current;
|
|
215
|
-
const content = promptData.versions.get(targetVersion);
|
|
216
|
-
if (!content) {
|
|
217
|
-
throw new Error(
|
|
218
|
-
`Prompt '${promptKey}' version ${targetVersion} not found.`
|
|
219
|
-
);
|
|
220
|
-
}
|
|
221
|
-
const system = replaceVariables(content.systemPrompt, variables);
|
|
222
|
-
const user = replaceVariables(content.userTemplate, variables);
|
|
223
|
-
setPromptContext({
|
|
224
|
-
promptKey,
|
|
225
|
-
promptVersion: targetVersion,
|
|
226
|
-
abTestKey,
|
|
227
|
-
variantIndex: selectedIndex
|
|
228
|
-
});
|
|
229
|
-
log(
|
|
230
|
-
`\u2705 Got prompt from A/B: ${promptKey} v${targetVersion} (variant ${selectedIndex})`
|
|
231
|
-
);
|
|
232
|
-
return {
|
|
233
|
-
key: promptKey,
|
|
234
|
-
version: targetVersion,
|
|
235
|
-
system,
|
|
236
|
-
user,
|
|
237
|
-
abTestKey,
|
|
238
|
-
variantIndex: selectedIndex
|
|
239
|
-
};
|
|
240
|
-
}
|
|
241
|
-
function clearPromptContext() {
|
|
242
|
-
promptContext = null;
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
export {
|
|
246
|
-
init,
|
|
247
|
-
getPromptContext,
|
|
248
|
-
get,
|
|
249
|
-
getAB,
|
|
250
|
-
clearPromptContext,
|
|
251
|
-
prompts_exports
|
|
252
|
-
};
|
package/dist/chunk-VNUUS74T.mjs
DELETED
|
@@ -1,242 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __export = (target, all) => {
|
|
3
|
-
for (var name in all)
|
|
4
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
5
|
-
};
|
|
6
|
-
|
|
7
|
-
// src/prompts.ts
|
|
8
|
-
var prompts_exports = {};
|
|
9
|
-
__export(prompts_exports, {
|
|
10
|
-
clearPromptContext: () => clearPromptContext,
|
|
11
|
-
get: () => get,
|
|
12
|
-
getAB: () => getAB,
|
|
13
|
-
getPromptContext: () => getPromptContext,
|
|
14
|
-
init: () => init
|
|
15
|
-
});
|
|
16
|
-
import { createHash } from "crypto";
|
|
17
|
-
var apiKey = null;
|
|
18
|
-
var baseUrl = "https://spans.fallom.com";
|
|
19
|
-
var initialized = false;
|
|
20
|
-
var syncInterval = null;
|
|
21
|
-
var debugMode = false;
|
|
22
|
-
var promptCache = /* @__PURE__ */ new Map();
|
|
23
|
-
var promptABCache = /* @__PURE__ */ new Map();
|
|
24
|
-
var promptContext = null;
|
|
25
|
-
var SYNC_TIMEOUT = 2e3;
|
|
26
|
-
function log(msg) {
|
|
27
|
-
if (debugMode) {
|
|
28
|
-
console.log(`[Fallom Prompts] ${msg}`);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
function init(options = {}) {
|
|
32
|
-
apiKey = options.apiKey || process.env.FALLOM_API_KEY || null;
|
|
33
|
-
baseUrl = options.baseUrl || process.env.FALLOM_BASE_URL || "https://spans.fallom.com";
|
|
34
|
-
initialized = true;
|
|
35
|
-
if (!apiKey) {
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
fetchAll().catch(() => {
|
|
39
|
-
});
|
|
40
|
-
if (!syncInterval) {
|
|
41
|
-
syncInterval = setInterval(() => {
|
|
42
|
-
fetchAll().catch(() => {
|
|
43
|
-
});
|
|
44
|
-
}, 3e4);
|
|
45
|
-
syncInterval.unref();
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
function ensureInit() {
|
|
49
|
-
if (!initialized) {
|
|
50
|
-
try {
|
|
51
|
-
init();
|
|
52
|
-
} catch {
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
async function fetchAll() {
|
|
57
|
-
await Promise.all([fetchPrompts(), fetchPromptABTests()]);
|
|
58
|
-
}
|
|
59
|
-
async function fetchPrompts(timeout = SYNC_TIMEOUT) {
|
|
60
|
-
if (!apiKey) return;
|
|
61
|
-
try {
|
|
62
|
-
const controller = new AbortController();
|
|
63
|
-
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
64
|
-
const resp = await fetch(`${baseUrl}/prompts`, {
|
|
65
|
-
headers: { Authorization: `Bearer ${apiKey}` },
|
|
66
|
-
signal: controller.signal
|
|
67
|
-
});
|
|
68
|
-
clearTimeout(timeoutId);
|
|
69
|
-
if (resp.ok) {
|
|
70
|
-
const data = await resp.json();
|
|
71
|
-
for (const p of data.prompts || []) {
|
|
72
|
-
if (!promptCache.has(p.key)) {
|
|
73
|
-
promptCache.set(p.key, { versions: /* @__PURE__ */ new Map(), current: null });
|
|
74
|
-
}
|
|
75
|
-
const cached = promptCache.get(p.key);
|
|
76
|
-
cached.versions.set(p.version, {
|
|
77
|
-
systemPrompt: p.system_prompt,
|
|
78
|
-
userTemplate: p.user_template
|
|
79
|
-
});
|
|
80
|
-
cached.current = p.version;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
} catch {
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
async function fetchPromptABTests(timeout = SYNC_TIMEOUT) {
|
|
87
|
-
if (!apiKey) return;
|
|
88
|
-
try {
|
|
89
|
-
const controller = new AbortController();
|
|
90
|
-
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
91
|
-
const resp = await fetch(`${baseUrl}/prompt-ab-tests`, {
|
|
92
|
-
headers: { Authorization: `Bearer ${apiKey}` },
|
|
93
|
-
signal: controller.signal
|
|
94
|
-
});
|
|
95
|
-
clearTimeout(timeoutId);
|
|
96
|
-
if (resp.ok) {
|
|
97
|
-
const data = await resp.json();
|
|
98
|
-
for (const t of data.prompt_ab_tests || []) {
|
|
99
|
-
if (!promptABCache.has(t.key)) {
|
|
100
|
-
promptABCache.set(t.key, { versions: /* @__PURE__ */ new Map(), current: null });
|
|
101
|
-
}
|
|
102
|
-
const cached = promptABCache.get(t.key);
|
|
103
|
-
cached.versions.set(t.version, { variants: t.variants });
|
|
104
|
-
cached.current = t.version;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
} catch {
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
function replaceVariables(template, variables) {
|
|
111
|
-
if (!variables) return template;
|
|
112
|
-
return template.replace(/\{\{(\s*\w+\s*)\}\}/g, (match, varName) => {
|
|
113
|
-
const key = varName.trim();
|
|
114
|
-
return key in variables ? String(variables[key]) : match;
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
function setPromptContext(ctx) {
|
|
118
|
-
promptContext = ctx;
|
|
119
|
-
}
|
|
120
|
-
function getPromptContext() {
|
|
121
|
-
const ctx = promptContext;
|
|
122
|
-
promptContext = null;
|
|
123
|
-
return ctx;
|
|
124
|
-
}
|
|
125
|
-
async function get(promptKey, options = {}) {
|
|
126
|
-
const { variables, version, debug = false } = options;
|
|
127
|
-
debugMode = debug;
|
|
128
|
-
ensureInit();
|
|
129
|
-
log(`get() called: promptKey=${promptKey}`);
|
|
130
|
-
let promptData = promptCache.get(promptKey);
|
|
131
|
-
if (!promptData) {
|
|
132
|
-
log("Not in cache, fetching...");
|
|
133
|
-
await fetchPrompts(SYNC_TIMEOUT);
|
|
134
|
-
promptData = promptCache.get(promptKey);
|
|
135
|
-
}
|
|
136
|
-
if (!promptData) {
|
|
137
|
-
throw new Error(
|
|
138
|
-
`Prompt '${promptKey}' not found. Check that it exists in your Fallom dashboard.`
|
|
139
|
-
);
|
|
140
|
-
}
|
|
141
|
-
const targetVersion = version ?? promptData.current;
|
|
142
|
-
const content = promptData.versions.get(targetVersion);
|
|
143
|
-
if (!content) {
|
|
144
|
-
throw new Error(`Prompt '${promptKey}' version ${targetVersion} not found.`);
|
|
145
|
-
}
|
|
146
|
-
const system = replaceVariables(content.systemPrompt, variables);
|
|
147
|
-
const user = replaceVariables(content.userTemplate, variables);
|
|
148
|
-
setPromptContext({
|
|
149
|
-
promptKey,
|
|
150
|
-
promptVersion: targetVersion
|
|
151
|
-
});
|
|
152
|
-
log(`\u2705 Got prompt: ${promptKey} v${targetVersion}`);
|
|
153
|
-
return {
|
|
154
|
-
key: promptKey,
|
|
155
|
-
version: targetVersion,
|
|
156
|
-
system,
|
|
157
|
-
user
|
|
158
|
-
};
|
|
159
|
-
}
|
|
160
|
-
async function getAB(abTestKey, sessionId, options = {}) {
|
|
161
|
-
const { variables, debug = false } = options;
|
|
162
|
-
debugMode = debug;
|
|
163
|
-
ensureInit();
|
|
164
|
-
log(`getAB() called: abTestKey=${abTestKey}, sessionId=${sessionId}`);
|
|
165
|
-
let abData = promptABCache.get(abTestKey);
|
|
166
|
-
if (!abData) {
|
|
167
|
-
log("Not in cache, fetching...");
|
|
168
|
-
await fetchPromptABTests(SYNC_TIMEOUT);
|
|
169
|
-
abData = promptABCache.get(abTestKey);
|
|
170
|
-
}
|
|
171
|
-
if (!abData) {
|
|
172
|
-
throw new Error(
|
|
173
|
-
`Prompt A/B test '${abTestKey}' not found. Check that it exists in your Fallom dashboard.`
|
|
174
|
-
);
|
|
175
|
-
}
|
|
176
|
-
const currentVersion = abData.current;
|
|
177
|
-
const versionData = abData.versions.get(currentVersion);
|
|
178
|
-
if (!versionData) {
|
|
179
|
-
throw new Error(`Prompt A/B test '${abTestKey}' has no current version.`);
|
|
180
|
-
}
|
|
181
|
-
const { variants } = versionData;
|
|
182
|
-
const hashBytes = createHash("md5").update(sessionId).digest();
|
|
183
|
-
const hashVal = hashBytes.readUInt32BE(0) % 1e6;
|
|
184
|
-
let cumulative = 0;
|
|
185
|
-
let selectedVariant = variants[variants.length - 1];
|
|
186
|
-
let selectedIndex = variants.length - 1;
|
|
187
|
-
for (let i = 0; i < variants.length; i++) {
|
|
188
|
-
cumulative += variants[i].weight * 1e4;
|
|
189
|
-
if (hashVal < cumulative) {
|
|
190
|
-
selectedVariant = variants[i];
|
|
191
|
-
selectedIndex = i;
|
|
192
|
-
break;
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
const promptKey = selectedVariant.prompt_key;
|
|
196
|
-
const promptVersion = selectedVariant.prompt_version;
|
|
197
|
-
let promptData = promptCache.get(promptKey);
|
|
198
|
-
if (!promptData) {
|
|
199
|
-
await fetchPrompts(SYNC_TIMEOUT);
|
|
200
|
-
promptData = promptCache.get(promptKey);
|
|
201
|
-
}
|
|
202
|
-
if (!promptData) {
|
|
203
|
-
throw new Error(
|
|
204
|
-
`Prompt '${promptKey}' (from A/B test '${abTestKey}') not found.`
|
|
205
|
-
);
|
|
206
|
-
}
|
|
207
|
-
const targetVersion = promptVersion ?? promptData.current;
|
|
208
|
-
const content = promptData.versions.get(targetVersion);
|
|
209
|
-
if (!content) {
|
|
210
|
-
throw new Error(`Prompt '${promptKey}' version ${targetVersion} not found.`);
|
|
211
|
-
}
|
|
212
|
-
const system = replaceVariables(content.systemPrompt, variables);
|
|
213
|
-
const user = replaceVariables(content.userTemplate, variables);
|
|
214
|
-
setPromptContext({
|
|
215
|
-
promptKey,
|
|
216
|
-
promptVersion: targetVersion,
|
|
217
|
-
abTestKey,
|
|
218
|
-
variantIndex: selectedIndex
|
|
219
|
-
});
|
|
220
|
-
log(`\u2705 Got prompt from A/B: ${promptKey} v${targetVersion} (variant ${selectedIndex})`);
|
|
221
|
-
return {
|
|
222
|
-
key: promptKey,
|
|
223
|
-
version: targetVersion,
|
|
224
|
-
system,
|
|
225
|
-
user,
|
|
226
|
-
abTestKey,
|
|
227
|
-
variantIndex: selectedIndex
|
|
228
|
-
};
|
|
229
|
-
}
|
|
230
|
-
function clearPromptContext() {
|
|
231
|
-
promptContext = null;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
export {
|
|
235
|
-
__export,
|
|
236
|
-
init,
|
|
237
|
-
getPromptContext,
|
|
238
|
-
get,
|
|
239
|
-
getAB,
|
|
240
|
-
clearPromptContext,
|
|
241
|
-
prompts_exports
|
|
242
|
-
};
|
package/dist/models-2Y6DRQPS.mjs
DELETED
package/dist/models-BUHMMTWK.mjs
DELETED
package/dist/models-JIO5LVMB.mjs
DELETED