@cortexkit/pi-magic-context 0.41.0 → 0.41.2
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/historian-calibration-extension.js +20 -14
- package/dist/{index-3k71e196.js → index-q2vgz0j1.js} +87 -2
- package/dist/{index-ts6nabp0.js → index-ys39wb43.js} +2129 -959
- package/dist/index.js +5420 -5110
- package/dist/{safe-notification-target-g8fx80va.js → safe-notification-target-hjcb423r.js} +1 -1
- package/dist/subagent-entry.js +2 -2
- package/dist/transformers-node-wasm.js +25676 -0
- package/dist/transformers-web.js +24570 -0
- package/dist/transformers.node-b1wa0hgx.js +28808 -0
- package/package.json +10 -5
- /package/dist/{index-wnrygn5h.js → index-y63nbvkk.js} +0 -0
|
@@ -12,13 +12,15 @@ function finiteNumber(value) {
|
|
|
12
12
|
function calibrateHistorianProviderPayload(payload, temperature, maxOutputTokens) {
|
|
13
13
|
if (typeof payload !== "object" || payload === null || Array.isArray(payload))
|
|
14
14
|
return payload;
|
|
15
|
+
if (temperature === undefined && maxOutputTokens === undefined)
|
|
16
|
+
return payload;
|
|
15
17
|
const calibrated = { ...payload };
|
|
16
18
|
const generationConfig = calibrated.generationConfig;
|
|
17
19
|
if (typeof generationConfig === "object" && generationConfig !== null && !Array.isArray(generationConfig)) {
|
|
18
20
|
calibrated.generationConfig = {
|
|
19
21
|
...generationConfig,
|
|
20
|
-
temperature,
|
|
21
|
-
maxOutputTokens
|
|
22
|
+
...temperature !== undefined ? { temperature } : {},
|
|
23
|
+
...maxOutputTokens !== undefined ? { maxOutputTokens } : {}
|
|
22
24
|
};
|
|
23
25
|
return calibrated;
|
|
24
26
|
}
|
|
@@ -26,27 +28,31 @@ function calibrateHistorianProviderPayload(payload, temperature, maxOutputTokens
|
|
|
26
28
|
if (typeof inferenceConfig === "object" && inferenceConfig !== null && !Array.isArray(inferenceConfig)) {
|
|
27
29
|
calibrated.inferenceConfig = {
|
|
28
30
|
...inferenceConfig,
|
|
29
|
-
temperature,
|
|
30
|
-
maxTokens: maxOutputTokens
|
|
31
|
+
...temperature !== undefined ? { temperature } : {},
|
|
32
|
+
...maxOutputTokens !== undefined ? { maxTokens: maxOutputTokens } : {}
|
|
31
33
|
};
|
|
32
34
|
return calibrated;
|
|
33
35
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
calibrated
|
|
39
|
-
|
|
40
|
-
calibrated
|
|
41
|
-
|
|
42
|
-
calibrated
|
|
36
|
+
if (temperature !== undefined) {
|
|
37
|
+
calibrated.temperature = temperature;
|
|
38
|
+
}
|
|
39
|
+
if (maxOutputTokens !== undefined) {
|
|
40
|
+
if ("max_output_tokens" in calibrated) {
|
|
41
|
+
calibrated.max_output_tokens = maxOutputTokens;
|
|
42
|
+
} else if ("max_completion_tokens" in calibrated) {
|
|
43
|
+
calibrated.max_completion_tokens = maxOutputTokens;
|
|
44
|
+
} else if ("max_tokens" in calibrated) {
|
|
45
|
+
calibrated.max_tokens = maxOutputTokens;
|
|
46
|
+
} else if ("maxTokens" in calibrated) {
|
|
47
|
+
calibrated.maxTokens = maxOutputTokens;
|
|
48
|
+
}
|
|
43
49
|
}
|
|
44
50
|
return calibrated;
|
|
45
51
|
}
|
|
46
52
|
function historianCalibrationExtension(pi) {
|
|
47
53
|
const temperature = finiteNumber(process.env[HISTORIAN_TEMPERATURE_ENV]);
|
|
48
54
|
const maxOutputTokens = finiteNumber(process.env[HISTORIAN_MAX_OUTPUT_TOKENS_ENV]);
|
|
49
|
-
if (temperature === undefined
|
|
55
|
+
if (temperature === undefined && maxOutputTokens === undefined)
|
|
50
56
|
return;
|
|
51
57
|
pi.on("before_provider_request", (event) => calibrateHistorianProviderPayload(event.payload, temperature, maxOutputTokens));
|
|
52
58
|
}
|
|
@@ -314,6 +314,11 @@ var buffer = [];
|
|
|
314
314
|
var flushTimer = null;
|
|
315
315
|
var FLUSH_INTERVAL_MS = 500;
|
|
316
316
|
var BUFFER_SIZE_LIMIT = 50;
|
|
317
|
+
var MAX_LOG_FILE_BYTES = 32 * 1024 * 1024;
|
|
318
|
+
var SIZE_CHECK_INTERVAL_FLUSHES = 64;
|
|
319
|
+
var activeLogFile = null;
|
|
320
|
+
var activeLogSize = null;
|
|
321
|
+
var flushesSinceSizeCheck = 0;
|
|
317
322
|
var swallowedWriteCount = 0;
|
|
318
323
|
var lastErrorMessage = null;
|
|
319
324
|
var lastErrorTime = null;
|
|
@@ -327,6 +332,73 @@ function recordSwallowedWrite(error) {
|
|
|
327
332
|
function ensureDir(filePath) {
|
|
328
333
|
fs.mkdirSync(path2.dirname(filePath), { recursive: true });
|
|
329
334
|
}
|
|
335
|
+
function isMissingFile(error) {
|
|
336
|
+
return typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT";
|
|
337
|
+
}
|
|
338
|
+
function getCurrentLogSize(logFile) {
|
|
339
|
+
if (activeLogFile === logFile && activeLogSize !== null && flushesSinceSizeCheck < SIZE_CHECK_INTERVAL_FLUSHES) {
|
|
340
|
+
return activeLogSize;
|
|
341
|
+
}
|
|
342
|
+
try {
|
|
343
|
+
const stat = fs.statSync(logFile);
|
|
344
|
+
if (!stat.isFile()) {
|
|
345
|
+
throw new Error(`Magic Context log path is not a regular file: ${logFile}`);
|
|
346
|
+
}
|
|
347
|
+
fs.chmodSync(logFile, 384);
|
|
348
|
+
activeLogFile = logFile;
|
|
349
|
+
activeLogSize = stat.size;
|
|
350
|
+
flushesSinceSizeCheck = 0;
|
|
351
|
+
return stat.size;
|
|
352
|
+
} catch (error) {
|
|
353
|
+
if (!isMissingFile(error))
|
|
354
|
+
throw error;
|
|
355
|
+
activeLogFile = logFile;
|
|
356
|
+
activeLogSize = 0;
|
|
357
|
+
flushesSinceSizeCheck = 0;
|
|
358
|
+
return 0;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
function capLogData(data) {
|
|
362
|
+
if (Buffer.byteLength(data) <= MAX_LOG_FILE_BYTES)
|
|
363
|
+
return data;
|
|
364
|
+
let bounded = Buffer.from(data).subarray(0, MAX_LOG_FILE_BYTES).toString("utf8");
|
|
365
|
+
while (Buffer.byteLength(bounded) > MAX_LOG_FILE_BYTES) {
|
|
366
|
+
bounded = bounded.slice(0, -1);
|
|
367
|
+
}
|
|
368
|
+
return bounded;
|
|
369
|
+
}
|
|
370
|
+
function writeBoundedPredecessor(logFile, predecessorPath, size) {
|
|
371
|
+
const predecessorFd = fs.openSync(predecessorPath, "w", 384);
|
|
372
|
+
try {
|
|
373
|
+
fs.fchmodSync(predecessorFd, 384);
|
|
374
|
+
const bytesToCopy = Math.min(size, MAX_LOG_FILE_BYTES);
|
|
375
|
+
const sourceFd = fs.openSync(logFile, "r");
|
|
376
|
+
try {
|
|
377
|
+
const chunk = Buffer.allocUnsafe(Math.min(64 * 1024, bytesToCopy));
|
|
378
|
+
let remaining = bytesToCopy;
|
|
379
|
+
let position = Math.max(0, size - bytesToCopy);
|
|
380
|
+
while (remaining > 0) {
|
|
381
|
+
const bytesRead = fs.readSync(sourceFd, chunk, 0, Math.min(chunk.length, remaining), position);
|
|
382
|
+
if (bytesRead === 0)
|
|
383
|
+
break;
|
|
384
|
+
fs.writeSync(predecessorFd, chunk, 0, bytesRead);
|
|
385
|
+
remaining -= bytesRead;
|
|
386
|
+
position += bytesRead;
|
|
387
|
+
}
|
|
388
|
+
} finally {
|
|
389
|
+
fs.closeSync(sourceFd);
|
|
390
|
+
}
|
|
391
|
+
} finally {
|
|
392
|
+
fs.closeSync(predecessorFd);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
function rotateLogFile(logFile, size) {
|
|
396
|
+
const predecessorPath = `${logFile}.1`;
|
|
397
|
+
writeBoundedPredecessor(logFile, predecessorPath, size);
|
|
398
|
+
fs.truncateSync(logFile, 0);
|
|
399
|
+
activeLogSize = 0;
|
|
400
|
+
flushesSinceSizeCheck = 0;
|
|
401
|
+
}
|
|
330
402
|
function flush() {
|
|
331
403
|
if (flushTimer) {
|
|
332
404
|
clearTimeout(flushTimer);
|
|
@@ -334,13 +406,26 @@ function flush() {
|
|
|
334
406
|
}
|
|
335
407
|
if (buffer.length === 0)
|
|
336
408
|
return;
|
|
337
|
-
const
|
|
409
|
+
const bufferedData = buffer.join("");
|
|
338
410
|
buffer = [];
|
|
339
411
|
try {
|
|
412
|
+
const data = capLogData(bufferedData);
|
|
340
413
|
const logFile = getMagicContextLogPath();
|
|
341
414
|
ensureDir(logFile);
|
|
342
|
-
|
|
415
|
+
let currentSize = getCurrentLogSize(logFile);
|
|
416
|
+
const dataSize = Buffer.byteLength(data);
|
|
417
|
+
if (currentSize > 0 && currentSize + dataSize > MAX_LOG_FILE_BYTES) {
|
|
418
|
+
rotateLogFile(logFile, currentSize);
|
|
419
|
+
currentSize = 0;
|
|
420
|
+
}
|
|
421
|
+
fs.appendFileSync(logFile, data, { encoding: "utf8", mode: 384 });
|
|
422
|
+
activeLogFile = logFile;
|
|
423
|
+
activeLogSize = currentSize + dataSize;
|
|
424
|
+
flushesSinceSizeCheck++;
|
|
343
425
|
} catch (error) {
|
|
426
|
+
activeLogFile = null;
|
|
427
|
+
activeLogSize = null;
|
|
428
|
+
flushesSinceSizeCheck = 0;
|
|
344
429
|
recordSwallowedWrite(error);
|
|
345
430
|
}
|
|
346
431
|
}
|