52 lines
1.8 KiB
JavaScript
52 lines
1.8 KiB
JavaScript
import { v4 } from "uuid";
|
||
import * as PartialJSON from "partial-json";
|
||
|
||
//#region src/utils.ts
|
||
function randomUUID() {
|
||
return v4();
|
||
}
|
||
function partialJSONParse(json) {
|
||
try {
|
||
const parsed = PartialJSON.parse(json);
|
||
if (typeof parsed === "object" && parsed !== null && !Array.isArray(parsed)) return parsed;
|
||
console.warn(`[CopilotKit] Tool arguments parsed to non-object (${typeof parsed}), falling back to empty object`);
|
||
return {};
|
||
} catch (error) {
|
||
return {};
|
||
}
|
||
}
|
||
/**
|
||
* Safely parses a JSON string into a plain object for tool arguments.
|
||
* Handles two failure modes:
|
||
* 1. Malformed JSON (SyntaxError from JSON.parse)
|
||
* 2. Valid JSON that isn't a plain object (e.g. "", [], null, 42, true)
|
||
* Falls back to an empty object for safety in both cases.
|
||
*/
|
||
function safeParseToolArgs(raw) {
|
||
try {
|
||
const parsed = JSON.parse(raw);
|
||
if (typeof parsed === "object" && parsed !== null && !Array.isArray(parsed)) return parsed;
|
||
console.warn(`[CopilotKit] Tool arguments parsed to non-object (${typeof parsed}), falling back to empty object`);
|
||
return {};
|
||
} catch {
|
||
console.warn("[CopilotKit] Failed to parse tool arguments, falling back to empty object");
|
||
return {};
|
||
}
|
||
}
|
||
/**
|
||
* Returns an exponential backoff function suitable for Phoenix.js
|
||
* `reconnectAfterMs` and `rejoinAfterMs` options.
|
||
*
|
||
* @param baseMs - Initial delay for the first retry attempt.
|
||
* @param maxMs - Upper bound — delays are capped at this value.
|
||
*
|
||
* Phoenix calls the returned function with a 1-based `tries` count.
|
||
* The delay doubles on each attempt: baseMs, 2×baseMs, 4×baseMs, …, maxMs.
|
||
*/
|
||
function phoenixExponentialBackoff(baseMs, maxMs) {
|
||
return (tries) => Math.min(baseMs * 2 ** (tries - 1), maxMs);
|
||
}
|
||
|
||
//#endregion
|
||
export { partialJSONParse, phoenixExponentialBackoff, randomUUID, safeParseToolArgs };
|
||
//# sourceMappingURL=utils.mjs.map
|