rdesign/frontend/node_modules/@copilotkitnext/shared/dist/utils.cjs

57 lines
2.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
let uuid = require("uuid");
let partial_json = require("partial-json");
partial_json = require_runtime.__toESM(partial_json);
//#region src/utils.ts
function randomUUID() {
return (0, uuid.v4)();
}
function partialJSONParse(json) {
try {
const parsed = partial_json.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
exports.partialJSONParse = partialJSONParse;
exports.phoenixExponentialBackoff = phoenixExponentialBackoff;
exports.randomUUID = randomUUID;
exports.safeParseToolArgs = safeParseToolArgs;
//# sourceMappingURL=utils.cjs.map