@copilotkitnext/shared 1.53.0 → 1.53.1-next.1

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/index.cjs CHANGED
@@ -12,4 +12,5 @@ exports.TranscriptionErrors = require_transcription_errors.TranscriptionErrors;
12
12
  exports.finalizeRunEvents = require_finalize_events.finalizeRunEvents;
13
13
  exports.logger = require_logger.logger;
14
14
  exports.partialJSONParse = require_utils.partialJSONParse;
15
+ exports.phoenixExponentialBackoff = require_utils.phoenixExponentialBackoff;
15
16
  exports.randomUUID = require_utils.randomUUID;
package/dist/index.d.cts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { AgentDescription, MaybePromise, NonEmptyRecord, RuntimeInfo } from "./types.cjs";
2
- import { partialJSONParse, randomUUID } from "./utils.cjs";
2
+ import { partialJSONParse, phoenixExponentialBackoff, randomUUID } from "./utils.cjs";
3
3
  import { logger } from "./logger.cjs";
4
4
  import { AG_UI_CHANNEL_EVENT, DEFAULT_AGENT_ID } from "./constants.cjs";
5
5
  import { finalizeRunEvents } from "./finalize-events.cjs";
6
6
  import { TranscriptionErrorCode, TranscriptionErrorResponse, TranscriptionErrors } from "./transcription-errors.cjs";
7
- export { AG_UI_CHANNEL_EVENT, type AgentDescription, DEFAULT_AGENT_ID, type MaybePromise, type NonEmptyRecord, type RuntimeInfo, TranscriptionErrorCode, type TranscriptionErrorResponse, TranscriptionErrors, finalizeRunEvents, logger, partialJSONParse, randomUUID };
7
+ export { AG_UI_CHANNEL_EVENT, type AgentDescription, DEFAULT_AGENT_ID, type MaybePromise, type NonEmptyRecord, type RuntimeInfo, TranscriptionErrorCode, type TranscriptionErrorResponse, TranscriptionErrors, finalizeRunEvents, logger, partialJSONParse, phoenixExponentialBackoff, randomUUID };
package/dist/index.d.mts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { AgentDescription, MaybePromise, NonEmptyRecord, RuntimeInfo } from "./types.mjs";
2
- import { partialJSONParse, randomUUID } from "./utils.mjs";
2
+ import { partialJSONParse, phoenixExponentialBackoff, randomUUID } from "./utils.mjs";
3
3
  import { logger } from "./logger.mjs";
4
4
  import { AG_UI_CHANNEL_EVENT, DEFAULT_AGENT_ID } from "./constants.mjs";
5
5
  import { finalizeRunEvents } from "./finalize-events.mjs";
6
6
  import { TranscriptionErrorCode, TranscriptionErrorResponse, TranscriptionErrors } from "./transcription-errors.mjs";
7
- export { AG_UI_CHANNEL_EVENT, type AgentDescription, DEFAULT_AGENT_ID, type MaybePromise, type NonEmptyRecord, type RuntimeInfo, TranscriptionErrorCode, type TranscriptionErrorResponse, TranscriptionErrors, finalizeRunEvents, logger, partialJSONParse, randomUUID };
7
+ export { AG_UI_CHANNEL_EVENT, type AgentDescription, DEFAULT_AGENT_ID, type MaybePromise, type NonEmptyRecord, type RuntimeInfo, TranscriptionErrorCode, type TranscriptionErrorResponse, TranscriptionErrors, finalizeRunEvents, logger, partialJSONParse, phoenixExponentialBackoff, randomUUID };
package/dist/index.mjs CHANGED
@@ -1,7 +1,7 @@
1
- import { partialJSONParse, randomUUID } from "./utils.mjs";
1
+ import { partialJSONParse, phoenixExponentialBackoff, randomUUID } from "./utils.mjs";
2
2
  import { logger } from "./logger.mjs";
3
3
  import { AG_UI_CHANNEL_EVENT, DEFAULT_AGENT_ID } from "./constants.mjs";
4
4
  import { finalizeRunEvents } from "./finalize-events.mjs";
5
5
  import { TranscriptionErrorCode, TranscriptionErrors } from "./transcription-errors.mjs";
6
6
 
7
- export { AG_UI_CHANNEL_EVENT, DEFAULT_AGENT_ID, TranscriptionErrorCode, TranscriptionErrors, finalizeRunEvents, logger, partialJSONParse, randomUUID };
7
+ export { AG_UI_CHANNEL_EVENT, DEFAULT_AGENT_ID, TranscriptionErrorCode, TranscriptionErrors, finalizeRunEvents, logger, partialJSONParse, phoenixExponentialBackoff, randomUUID };
package/dist/index.umd.js CHANGED
@@ -44,6 +44,19 @@ partial_json = __toESM(partial_json);
44
44
  return {};
45
45
  }
46
46
  }
47
+ /**
48
+ * Returns an exponential backoff function suitable for Phoenix.js
49
+ * `reconnectAfterMs` and `rejoinAfterMs` options.
50
+ *
51
+ * @param baseMs - Initial delay for the first retry attempt.
52
+ * @param maxMs - Upper bound — delays are capped at this value.
53
+ *
54
+ * Phoenix calls the returned function with a 1-based `tries` count.
55
+ * The delay doubles on each attempt: baseMs, 2×baseMs, 4×baseMs, …, maxMs.
56
+ */
57
+ function phoenixExponentialBackoff(baseMs, maxMs) {
58
+ return (tries) => Math.min(baseMs * 2 ** (tries - 1), maxMs);
59
+ }
47
60
 
48
61
  //#endregion
49
62
  //#region src/logger.ts
@@ -243,6 +256,7 @@ exports.TranscriptionErrors = TranscriptionErrors;
243
256
  exports.finalizeRunEvents = finalizeRunEvents;
244
257
  exports.logger = logger;
245
258
  exports.partialJSONParse = partialJSONParse;
259
+ exports.phoenixExponentialBackoff = phoenixExponentialBackoff;
246
260
  exports.randomUUID = randomUUID;
247
261
  });
248
262
  //# sourceMappingURL=index.umd.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.umd.js","names":["PartialJSON","EventType"],"sources":["../src/utils.ts","../src/logger.ts","../src/constants.ts","../src/finalize-events.ts","../src/transcription-errors.ts"],"sourcesContent":["import { v4 as uuidv4 } from \"uuid\";\nimport * as PartialJSON from \"partial-json\";\n\nexport function randomUUID() {\n return uuidv4();\n}\n\nexport function partialJSONParse(json: string) {\n try {\n return PartialJSON.parse(json);\n } catch (error) {\n return {};\n }\n}\n","export const logger = console;\n","export const DEFAULT_AGENT_ID = \"default\";\n\n/** Phoenix channel event name used for all AG-UI events. */\nexport const AG_UI_CHANNEL_EVENT = \"ag-ui\";\n","import { BaseEvent, EventType, RunErrorEvent } from \"@ag-ui/client\";\nimport { randomUUID } from \"./utils\";\n\ninterface FinalizeRunOptions {\n stopRequested?: boolean;\n interruptionMessage?: string;\n}\n\nconst defaultStopMessage = \"Run stopped by user\";\nconst defaultAbruptEndMessage = \"Run ended without emitting a terminal event\";\n\nexport function finalizeRunEvents(\n events: BaseEvent[],\n options: FinalizeRunOptions = {},\n): BaseEvent[] {\n const { stopRequested = false, interruptionMessage } = options;\n\n const resolvedStopMessage = interruptionMessage ?? defaultStopMessage;\n const resolvedAbruptMessage =\n interruptionMessage && interruptionMessage !== defaultStopMessage\n ? interruptionMessage\n : defaultAbruptEndMessage;\n\n const appended: BaseEvent[] = [];\n\n const openMessageIds = new Set<string>();\n const openToolCalls = new Map<\n string,\n {\n hasEnd: boolean;\n hasResult: boolean;\n }\n >();\n\n for (const event of events) {\n switch (event.type) {\n case EventType.TEXT_MESSAGE_START: {\n const messageId = (event as { messageId?: string }).messageId;\n if (typeof messageId === \"string\") {\n openMessageIds.add(messageId);\n }\n break;\n }\n case EventType.TEXT_MESSAGE_END: {\n const messageId = (event as { messageId?: string }).messageId;\n if (typeof messageId === \"string\") {\n openMessageIds.delete(messageId);\n }\n break;\n }\n case EventType.TOOL_CALL_START: {\n const toolCallId = (event as { toolCallId?: string }).toolCallId;\n if (typeof toolCallId === \"string\") {\n openToolCalls.set(toolCallId, {\n hasEnd: false,\n hasResult: false,\n });\n }\n break;\n }\n case EventType.TOOL_CALL_END: {\n const toolCallId = (event as { toolCallId?: string }).toolCallId;\n const info = toolCallId ? openToolCalls.get(toolCallId) : undefined;\n if (info) {\n info.hasEnd = true;\n }\n break;\n }\n case EventType.TOOL_CALL_RESULT: {\n const toolCallId = (event as { toolCallId?: string }).toolCallId;\n const info = toolCallId ? openToolCalls.get(toolCallId) : undefined;\n if (info) {\n info.hasResult = true;\n }\n break;\n }\n default:\n break;\n }\n }\n\n const hasRunFinished = events.some(\n (event) => event.type === EventType.RUN_FINISHED,\n );\n const hasRunError = events.some(\n (event) => event.type === EventType.RUN_ERROR,\n );\n const hasTerminalEvent = hasRunFinished || hasRunError;\n const terminalEventMissing = !hasTerminalEvent;\n\n for (const messageId of openMessageIds) {\n const endEvent = {\n type: EventType.TEXT_MESSAGE_END,\n messageId,\n } as BaseEvent;\n events.push(endEvent);\n appended.push(endEvent);\n }\n\n for (const [toolCallId, info] of openToolCalls) {\n if (!info.hasEnd) {\n const endEvent = {\n type: EventType.TOOL_CALL_END,\n toolCallId,\n } as BaseEvent;\n events.push(endEvent);\n appended.push(endEvent);\n }\n\n if (terminalEventMissing && !info.hasResult) {\n const resultEvent = {\n type: EventType.TOOL_CALL_RESULT,\n toolCallId,\n messageId: `${toolCallId ?? randomUUID()}-result`,\n role: \"tool\",\n content: JSON.stringify(\n stopRequested\n ? {\n status: \"stopped\",\n reason: \"stop_requested\",\n message: resolvedStopMessage,\n }\n : {\n status: \"error\",\n reason: \"missing_terminal_event\",\n message: resolvedAbruptMessage,\n },\n ),\n } as BaseEvent;\n events.push(resultEvent);\n appended.push(resultEvent);\n }\n }\n\n if (terminalEventMissing) {\n if (stopRequested) {\n const finishedEvent = {\n type: EventType.RUN_FINISHED,\n } as BaseEvent;\n events.push(finishedEvent);\n appended.push(finishedEvent);\n } else {\n const errorEvent: RunErrorEvent = {\n type: EventType.RUN_ERROR,\n message: resolvedAbruptMessage,\n code: \"INCOMPLETE_STREAM\",\n };\n events.push(errorEvent);\n appended.push(errorEvent);\n }\n }\n\n return appended;\n}\n","/**\n * Error codes for transcription HTTP responses.\n * Uses snake_case to align with existing CopilotKitCoreErrorCode pattern.\n * These codes are returned by the runtime and parsed by the client.\n */\nexport enum TranscriptionErrorCode {\n /** Transcription service not configured in runtime */\n SERVICE_NOT_CONFIGURED = \"service_not_configured\",\n /** Audio format not supported */\n INVALID_AUDIO_FORMAT = \"invalid_audio_format\",\n /** Audio file is too long */\n AUDIO_TOO_LONG = \"audio_too_long\",\n /** Audio file is empty or too short */\n AUDIO_TOO_SHORT = \"audio_too_short\",\n /** Rate limited by transcription provider */\n RATE_LIMITED = \"rate_limited\",\n /** Authentication failed with transcription provider */\n AUTH_FAILED = \"auth_failed\",\n /** Transcription provider returned an error */\n PROVIDER_ERROR = \"provider_error\",\n /** Network error during transcription */\n NETWORK_ERROR = \"network_error\",\n /** Invalid request format */\n INVALID_REQUEST = \"invalid_request\",\n}\n\n/**\n * Error response format returned by the transcription endpoint.\n */\nexport interface TranscriptionErrorResponse {\n error: TranscriptionErrorCode;\n message: string;\n retryable?: boolean;\n}\n\n/**\n * Helper functions to create transcription error responses.\n * Used by the runtime to return consistent error responses.\n */\nexport const TranscriptionErrors = {\n serviceNotConfigured: (): TranscriptionErrorResponse => ({\n error: TranscriptionErrorCode.SERVICE_NOT_CONFIGURED,\n message: \"Transcription service is not configured\",\n retryable: false,\n }),\n\n invalidAudioFormat: (\n format: string,\n supported: string[],\n ): TranscriptionErrorResponse => ({\n error: TranscriptionErrorCode.INVALID_AUDIO_FORMAT,\n message: `Unsupported audio format: ${format}. Supported: ${supported.join(\", \")}`,\n retryable: false,\n }),\n\n invalidRequest: (details: string): TranscriptionErrorResponse => ({\n error: TranscriptionErrorCode.INVALID_REQUEST,\n message: details,\n retryable: false,\n }),\n\n rateLimited: (): TranscriptionErrorResponse => ({\n error: TranscriptionErrorCode.RATE_LIMITED,\n message: \"Rate limited. Please try again later.\",\n retryable: true,\n }),\n\n authFailed: (): TranscriptionErrorResponse => ({\n error: TranscriptionErrorCode.AUTH_FAILED,\n message: \"Authentication failed with transcription provider\",\n retryable: false,\n }),\n\n providerError: (message: string): TranscriptionErrorResponse => ({\n error: TranscriptionErrorCode.PROVIDER_ERROR,\n message,\n retryable: true,\n }),\n\n networkError: (\n message: string = \"Network error during transcription\",\n ): TranscriptionErrorResponse => ({\n error: TranscriptionErrorCode.NETWORK_ERROR,\n message,\n retryable: true,\n }),\n\n audioTooLong: (): TranscriptionErrorResponse => ({\n error: TranscriptionErrorCode.AUDIO_TOO_LONG,\n message: \"Audio file is too long\",\n retryable: false,\n }),\n\n audioTooShort: (): TranscriptionErrorResponse => ({\n error: TranscriptionErrorCode.AUDIO_TOO_SHORT,\n message: \"Audio is too short to transcribe\",\n retryable: false,\n }),\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;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ACA,eACgC;GAChC,OAAO,uBAAuB;GAC9B,SAAS,6BAA6B,OAAO,eAAe,UAAU,KAAK,KAAK;GAChF,WAAW;GACZ;EAED,iBAAiB,aAAiD;GAChE,OAAO,uBAAuB;GAC9B,SAAS;GACT,WAAW;GACZ;EAED,oBAAgD;GAC9C,OAAO,uBAAuB;GAC9B,SAAS;GACT,WAAW;GACZ;EAED,mBAA+C;GAC7C,OAAO,uBAAuB;GAC9B,SAAS;GACT,WAAW;GACZ;EAED,gBAAgB,aAAiD;GAC/D,OAAO,uBAAuB;GAC9B;GACA,WAAW;GACZ;EAED,eACE,UAAkB,0CACc;GAChC,OAAO,uBAAuB;GAC9B;GACA,WAAW;GACZ;EAED,qBAAiD;GAC/C,OAAO,uBAAuB;GAC9B,SAAS;GACT,WAAW;GACZ;EAED,sBAAkD;GAChD,OAAO,uBAAuB;GAC9B,SAAS;GACT,WAAW;GACZ;EACF"}
1
+ {"version":3,"file":"index.umd.js","names":["PartialJSON","EventType"],"sources":["../src/utils.ts","../src/logger.ts","../src/constants.ts","../src/finalize-events.ts","../src/transcription-errors.ts"],"sourcesContent":["import { v4 as uuidv4 } from \"uuid\";\nimport * as PartialJSON from \"partial-json\";\n\nexport function randomUUID() {\n return uuidv4();\n}\n\nexport function partialJSONParse(json: string) {\n try {\n return PartialJSON.parse(json);\n } catch (error) {\n return {};\n }\n}\n\n/**\n * Returns an exponential backoff function suitable for Phoenix.js\n * `reconnectAfterMs` and `rejoinAfterMs` options.\n *\n * @param baseMs - Initial delay for the first retry attempt.\n * @param maxMs - Upper bound — delays are capped at this value.\n *\n * Phoenix calls the returned function with a 1-based `tries` count.\n * The delay doubles on each attempt: baseMs, 2×baseMs, 4×baseMs, …, maxMs.\n */\nexport function phoenixExponentialBackoff(\n baseMs: number,\n maxMs: number,\n): (tries: number) => number {\n return (tries: number) => Math.min(baseMs * 2 ** (tries - 1), maxMs);\n}\n","export const logger = console;\n","export const DEFAULT_AGENT_ID = \"default\";\n\n/** Phoenix channel event name used for all AG-UI events. */\nexport const AG_UI_CHANNEL_EVENT = \"ag-ui\";\n","import { BaseEvent, EventType, RunErrorEvent } from \"@ag-ui/client\";\nimport { randomUUID } from \"./utils\";\n\ninterface FinalizeRunOptions {\n stopRequested?: boolean;\n interruptionMessage?: string;\n}\n\nconst defaultStopMessage = \"Run stopped by user\";\nconst defaultAbruptEndMessage = \"Run ended without emitting a terminal event\";\n\nexport function finalizeRunEvents(\n events: BaseEvent[],\n options: FinalizeRunOptions = {},\n): BaseEvent[] {\n const { stopRequested = false, interruptionMessage } = options;\n\n const resolvedStopMessage = interruptionMessage ?? defaultStopMessage;\n const resolvedAbruptMessage =\n interruptionMessage && interruptionMessage !== defaultStopMessage\n ? interruptionMessage\n : defaultAbruptEndMessage;\n\n const appended: BaseEvent[] = [];\n\n const openMessageIds = new Set<string>();\n const openToolCalls = new Map<\n string,\n {\n hasEnd: boolean;\n hasResult: boolean;\n }\n >();\n\n for (const event of events) {\n switch (event.type) {\n case EventType.TEXT_MESSAGE_START: {\n const messageId = (event as { messageId?: string }).messageId;\n if (typeof messageId === \"string\") {\n openMessageIds.add(messageId);\n }\n break;\n }\n case EventType.TEXT_MESSAGE_END: {\n const messageId = (event as { messageId?: string }).messageId;\n if (typeof messageId === \"string\") {\n openMessageIds.delete(messageId);\n }\n break;\n }\n case EventType.TOOL_CALL_START: {\n const toolCallId = (event as { toolCallId?: string }).toolCallId;\n if (typeof toolCallId === \"string\") {\n openToolCalls.set(toolCallId, {\n hasEnd: false,\n hasResult: false,\n });\n }\n break;\n }\n case EventType.TOOL_CALL_END: {\n const toolCallId = (event as { toolCallId?: string }).toolCallId;\n const info = toolCallId ? openToolCalls.get(toolCallId) : undefined;\n if (info) {\n info.hasEnd = true;\n }\n break;\n }\n case EventType.TOOL_CALL_RESULT: {\n const toolCallId = (event as { toolCallId?: string }).toolCallId;\n const info = toolCallId ? openToolCalls.get(toolCallId) : undefined;\n if (info) {\n info.hasResult = true;\n }\n break;\n }\n default:\n break;\n }\n }\n\n const hasRunFinished = events.some(\n (event) => event.type === EventType.RUN_FINISHED,\n );\n const hasRunError = events.some(\n (event) => event.type === EventType.RUN_ERROR,\n );\n const hasTerminalEvent = hasRunFinished || hasRunError;\n const terminalEventMissing = !hasTerminalEvent;\n\n for (const messageId of openMessageIds) {\n const endEvent = {\n type: EventType.TEXT_MESSAGE_END,\n messageId,\n } as BaseEvent;\n events.push(endEvent);\n appended.push(endEvent);\n }\n\n for (const [toolCallId, info] of openToolCalls) {\n if (!info.hasEnd) {\n const endEvent = {\n type: EventType.TOOL_CALL_END,\n toolCallId,\n } as BaseEvent;\n events.push(endEvent);\n appended.push(endEvent);\n }\n\n if (terminalEventMissing && !info.hasResult) {\n const resultEvent = {\n type: EventType.TOOL_CALL_RESULT,\n toolCallId,\n messageId: `${toolCallId ?? randomUUID()}-result`,\n role: \"tool\",\n content: JSON.stringify(\n stopRequested\n ? {\n status: \"stopped\",\n reason: \"stop_requested\",\n message: resolvedStopMessage,\n }\n : {\n status: \"error\",\n reason: \"missing_terminal_event\",\n message: resolvedAbruptMessage,\n },\n ),\n } as BaseEvent;\n events.push(resultEvent);\n appended.push(resultEvent);\n }\n }\n\n if (terminalEventMissing) {\n if (stopRequested) {\n const finishedEvent = {\n type: EventType.RUN_FINISHED,\n } as BaseEvent;\n events.push(finishedEvent);\n appended.push(finishedEvent);\n } else {\n const errorEvent: RunErrorEvent = {\n type: EventType.RUN_ERROR,\n message: resolvedAbruptMessage,\n code: \"INCOMPLETE_STREAM\",\n };\n events.push(errorEvent);\n appended.push(errorEvent);\n }\n }\n\n return appended;\n}\n","/**\n * Error codes for transcription HTTP responses.\n * Uses snake_case to align with existing CopilotKitCoreErrorCode pattern.\n * These codes are returned by the runtime and parsed by the client.\n */\nexport enum TranscriptionErrorCode {\n /** Transcription service not configured in runtime */\n SERVICE_NOT_CONFIGURED = \"service_not_configured\",\n /** Audio format not supported */\n INVALID_AUDIO_FORMAT = \"invalid_audio_format\",\n /** Audio file is too long */\n AUDIO_TOO_LONG = \"audio_too_long\",\n /** Audio file is empty or too short */\n AUDIO_TOO_SHORT = \"audio_too_short\",\n /** Rate limited by transcription provider */\n RATE_LIMITED = \"rate_limited\",\n /** Authentication failed with transcription provider */\n AUTH_FAILED = \"auth_failed\",\n /** Transcription provider returned an error */\n PROVIDER_ERROR = \"provider_error\",\n /** Network error during transcription */\n NETWORK_ERROR = \"network_error\",\n /** Invalid request format */\n INVALID_REQUEST = \"invalid_request\",\n}\n\n/**\n * Error response format returned by the transcription endpoint.\n */\nexport interface TranscriptionErrorResponse {\n error: TranscriptionErrorCode;\n message: string;\n retryable?: boolean;\n}\n\n/**\n * Helper functions to create transcription error responses.\n * Used by the runtime to return consistent error responses.\n */\nexport const TranscriptionErrors = {\n serviceNotConfigured: (): TranscriptionErrorResponse => ({\n error: TranscriptionErrorCode.SERVICE_NOT_CONFIGURED,\n message: \"Transcription service is not configured\",\n retryable: false,\n }),\n\n invalidAudioFormat: (\n format: string,\n supported: string[],\n ): TranscriptionErrorResponse => ({\n error: TranscriptionErrorCode.INVALID_AUDIO_FORMAT,\n message: `Unsupported audio format: ${format}. Supported: ${supported.join(\", \")}`,\n retryable: false,\n }),\n\n invalidRequest: (details: string): TranscriptionErrorResponse => ({\n error: TranscriptionErrorCode.INVALID_REQUEST,\n message: details,\n retryable: false,\n }),\n\n rateLimited: (): TranscriptionErrorResponse => ({\n error: TranscriptionErrorCode.RATE_LIMITED,\n message: \"Rate limited. Please try again later.\",\n retryable: true,\n }),\n\n authFailed: (): TranscriptionErrorResponse => ({\n error: TranscriptionErrorCode.AUTH_FAILED,\n message: \"Authentication failed with transcription provider\",\n retryable: false,\n }),\n\n providerError: (message: string): TranscriptionErrorResponse => ({\n error: TranscriptionErrorCode.PROVIDER_ERROR,\n message,\n retryable: true,\n }),\n\n networkError: (\n message: string = \"Network error during transcription\",\n ): TranscriptionErrorResponse => ({\n error: TranscriptionErrorCode.NETWORK_ERROR,\n message,\n retryable: true,\n }),\n\n audioTooLong: (): TranscriptionErrorResponse => ({\n error: TranscriptionErrorCode.AUDIO_TOO_LONG,\n message: \"Audio file is too long\",\n retryable: false,\n }),\n\n audioTooShort: (): TranscriptionErrorResponse => ({\n error: TranscriptionErrorCode.AUDIO_TOO_SHORT,\n message: \"Audio is too short to transcribe\",\n retryable: false,\n }),\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAGA,SAAgB,aAAa;AAC3B,uBAAe;;CAGjB,SAAgB,iBAAiB,MAAc;AAC7C,MAAI;AACF,UAAOA,aAAY,MAAM,KAAK;WACvB,OAAO;AACd,UAAO,EAAE;;;;;;;;;;;;;CAcb,SAAgB,0BACd,QACA,OAC2B;AAC3B,UAAQ,UAAkB,KAAK,IAAI,SAAS,MAAM,QAAQ,IAAI,MAAM;;;;;CC7BtE,MAAa,SAAS;;;;CCAtB,MAAa,mBAAmB;;CAGhC,MAAa,sBAAsB;;;;CCKnC,MAAM,qBAAqB;CAC3B,MAAM,0BAA0B;CAEhC,SAAgB,kBACd,QACA,UAA8B,EAAE,EACnB;EACb,MAAM,EAAE,gBAAgB,OAAO,wBAAwB;EAEvD,MAAM,sBAAsB,uFAAuB;EACnD,MAAM,wBACJ,uBAAuB,wBAAwB,qBAC3C,sBACA;EAEN,MAAM,WAAwB,EAAE;EAEhC,MAAM,iCAAiB,IAAI,KAAa;EACxC,MAAM,gCAAgB,IAAI,KAMvB;AAEH,OAAK,MAAM,SAAS,OAClB,SAAQ,MAAM,MAAd;GACE,KAAKC,wBAAU,oBAAoB;IACjC,MAAM,YAAa,MAAiC;AACpD,QAAI,OAAO,cAAc,SACvB,gBAAe,IAAI,UAAU;AAE/B;;GAEF,KAAKA,wBAAU,kBAAkB;IAC/B,MAAM,YAAa,MAAiC;AACpD,QAAI,OAAO,cAAc,SACvB,gBAAe,OAAO,UAAU;AAElC;;GAEF,KAAKA,wBAAU,iBAAiB;IAC9B,MAAM,aAAc,MAAkC;AACtD,QAAI,OAA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,cAAc;AAC1B,YAAS,KAAK,cAAc;SACvB;GACL,MAAM,aAA4B;IAChC,MAAMA,wBAAU;IAChB,SAAS;IACT,MAAM;IACP;AACD,UAAO,KAAK,WAAW;AACvB,YAAS,KAAK,WAAW;;AAI7B,SAAO;;;;;;;;;;CCnJT,IAAY,0EAAL;;AAEL;;AAEA;;AAEA;;AAEA;;AAEA;;AAEA;;AAEA;;AAEA;;AAEA;;;;;;;CAgBF,MAAa,sBAAsB;EACjC,6BAAyD;GACvD,OAAO,uBAAuB;GAC9B,SAAS;GACT,WAAW;GACZ;EAED,qBACE,QACA,eACgC;GAChC,OAAO,uBAAuB;GAC9B,SAAS,6BAA6B,OAAO,eAAe,UAAU,KAAK,KAAK;GAChF,WAAW;GACZ;EAED,iBAAiB,aAAiD;GAChE,OAAO,uBAAuB;GAC9B,SAAS;GACT,WAAW;GACZ;EAED,oBAAgD;GAC9C,OAAO,uBAAuB;GAC9B,SAAS;GACT,WAAW;GACZ;EAED,mBAA+C;GAC7C,OAAO,uBAAuB;GAC9B,SAAS;GACT,WAAW;GACZ;EAED,gBAAgB,aAAiD;GAC/D,OAAO,uBAAuB;GAC9B;GACA,WAAW;GACZ;EAED,eACE,UAAkB,0CACc;GAChC,OAAO,uBAAuB;GAC9B;GACA,WAAW;GACZ;EAED,qBAAiD;GAC/C,OAAO,uBAAuB;GAC9B,SAAS;GACT,WAAW;GACZ;EAED,sBAAkD;GAChD,OAAO,uBAAuB;GAC9B,SAAS;GACT,WAAW;GACZ;EACF"}
package/dist/utils.cjs CHANGED
@@ -14,8 +14,22 @@ function partialJSONParse(json) {
14
14
  return {};
15
15
  }
16
16
  }
17
+ /**
18
+ * Returns an exponential backoff function suitable for Phoenix.js
19
+ * `reconnectAfterMs` and `rejoinAfterMs` options.
20
+ *
21
+ * @param baseMs - Initial delay for the first retry attempt.
22
+ * @param maxMs - Upper bound — delays are capped at this value.
23
+ *
24
+ * Phoenix calls the returned function with a 1-based `tries` count.
25
+ * The delay doubles on each attempt: baseMs, 2×baseMs, 4×baseMs, …, maxMs.
26
+ */
27
+ function phoenixExponentialBackoff(baseMs, maxMs) {
28
+ return (tries) => Math.min(baseMs * 2 ** (tries - 1), maxMs);
29
+ }
17
30
 
18
31
  //#endregion
19
32
  exports.partialJSONParse = partialJSONParse;
33
+ exports.phoenixExponentialBackoff = phoenixExponentialBackoff;
20
34
  exports.randomUUID = randomUUID;
21
35
  //# sourceMappingURL=utils.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.cjs","names":["PartialJSON"],"sources":["../src/utils.ts"],"sourcesContent":["import { v4 as uuidv4 } from \"uuid\";\nimport * as PartialJSON from \"partial-json\";\n\nexport function randomUUID() {\n return uuidv4();\n}\n\nexport function partialJSONParse(json: string) {\n try {\n return PartialJSON.parse(json);\n } catch (error) {\n return {};\n }\n}\n"],"mappings":";;;;;;AAGA,SAAgB,aAAa;AAC3B,sBAAe;;AAGjB,SAAgB,iBAAiB,MAAc;AAC7C,KAAI;AACF,SAAOA,aAAY,MAAM,KAAK;UACvB,OAAO;AACd,SAAO,EAAE"}
1
+ {"version":3,"file":"utils.cjs","names":["PartialJSON"],"sources":["../src/utils.ts"],"sourcesContent":["import { v4 as uuidv4 } from \"uuid\";\nimport * as PartialJSON from \"partial-json\";\n\nexport function randomUUID() {\n return uuidv4();\n}\n\nexport function partialJSONParse(json: string) {\n try {\n return PartialJSON.parse(json);\n } catch (error) {\n return {};\n }\n}\n\n/**\n * Returns an exponential backoff function suitable for Phoenix.js\n * `reconnectAfterMs` and `rejoinAfterMs` options.\n *\n * @param baseMs - Initial delay for the first retry attempt.\n * @param maxMs - Upper bound — delays are capped at this value.\n *\n * Phoenix calls the returned function with a 1-based `tries` count.\n * The delay doubles on each attempt: baseMs, 2×baseMs, 4×baseMs, …, maxMs.\n */\nexport function phoenixExponentialBackoff(\n baseMs: number,\n maxMs: number,\n): (tries: number) => number {\n return (tries: number) => Math.min(baseMs * 2 ** (tries - 1), maxMs);\n}\n"],"mappings":";;;;;;AAGA,SAAgB,aAAa;AAC3B,sBAAe;;AAGjB,SAAgB,iBAAiB,MAAc;AAC7C,KAAI;AACF,SAAOA,aAAY,MAAM,KAAK;UACvB,OAAO;AACd,SAAO,EAAE;;;;;;;;;;;;;AAcb,SAAgB,0BACd,QACA,OAC2B;AAC3B,SAAQ,UAAkB,KAAK,IAAI,SAAS,MAAM,QAAQ,IAAI,MAAM"}
package/dist/utils.d.cts CHANGED
@@ -1,6 +1,17 @@
1
1
  //#region src/utils.d.ts
2
2
  declare function randomUUID(): string;
3
3
  declare function partialJSONParse(json: string): any;
4
+ /**
5
+ * Returns an exponential backoff function suitable for Phoenix.js
6
+ * `reconnectAfterMs` and `rejoinAfterMs` options.
7
+ *
8
+ * @param baseMs - Initial delay for the first retry attempt.
9
+ * @param maxMs - Upper bound — delays are capped at this value.
10
+ *
11
+ * Phoenix calls the returned function with a 1-based `tries` count.
12
+ * The delay doubles on each attempt: baseMs, 2×baseMs, 4×baseMs, …, maxMs.
13
+ */
14
+ declare function phoenixExponentialBackoff(baseMs: number, maxMs: number): (tries: number) => number;
4
15
  //#endregion
5
- export { partialJSONParse, randomUUID };
16
+ export { partialJSONParse, phoenixExponentialBackoff, randomUUID };
6
17
  //# sourceMappingURL=utils.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.cts","names":[],"sources":["../src/utils.ts"],"mappings":";iBAGgB,UAAA,CAAA;AAAA,iBAIA,gBAAA,CAAiB,IAAA"}
1
+ {"version":3,"file":"utils.d.cts","names":[],"sources":["../src/utils.ts"],"mappings":";iBAGgB,UAAA,CAAA;AAAA,iBAIA,gBAAA,CAAiB,IAAA;;;;;AAAjC;;;;;AAkBA;iBAAgB,yBAAA,CACd,MAAA,UACA,KAAA,YACE,KAAA"}
package/dist/utils.d.mts CHANGED
@@ -1,6 +1,17 @@
1
1
  //#region src/utils.d.ts
2
2
  declare function randomUUID(): string;
3
3
  declare function partialJSONParse(json: string): any;
4
+ /**
5
+ * Returns an exponential backoff function suitable for Phoenix.js
6
+ * `reconnectAfterMs` and `rejoinAfterMs` options.
7
+ *
8
+ * @param baseMs - Initial delay for the first retry attempt.
9
+ * @param maxMs - Upper bound — delays are capped at this value.
10
+ *
11
+ * Phoenix calls the returned function with a 1-based `tries` count.
12
+ * The delay doubles on each attempt: baseMs, 2×baseMs, 4×baseMs, …, maxMs.
13
+ */
14
+ declare function phoenixExponentialBackoff(baseMs: number, maxMs: number): (tries: number) => number;
4
15
  //#endregion
5
- export { partialJSONParse, randomUUID };
16
+ export { partialJSONParse, phoenixExponentialBackoff, randomUUID };
6
17
  //# sourceMappingURL=utils.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.mts","names":[],"sources":["../src/utils.ts"],"mappings":";iBAGgB,UAAA,CAAA;AAAA,iBAIA,gBAAA,CAAiB,IAAA"}
1
+ {"version":3,"file":"utils.d.mts","names":[],"sources":["../src/utils.ts"],"mappings":";iBAGgB,UAAA,CAAA;AAAA,iBAIA,gBAAA,CAAiB,IAAA;;;;;AAAjC;;;;;AAkBA;iBAAgB,yBAAA,CACd,MAAA,UACA,KAAA,YACE,KAAA"}
package/dist/utils.mjs CHANGED
@@ -12,7 +12,20 @@ function partialJSONParse(json) {
12
12
  return {};
13
13
  }
14
14
  }
15
+ /**
16
+ * Returns an exponential backoff function suitable for Phoenix.js
17
+ * `reconnectAfterMs` and `rejoinAfterMs` options.
18
+ *
19
+ * @param baseMs - Initial delay for the first retry attempt.
20
+ * @param maxMs - Upper bound — delays are capped at this value.
21
+ *
22
+ * Phoenix calls the returned function with a 1-based `tries` count.
23
+ * The delay doubles on each attempt: baseMs, 2×baseMs, 4×baseMs, …, maxMs.
24
+ */
25
+ function phoenixExponentialBackoff(baseMs, maxMs) {
26
+ return (tries) => Math.min(baseMs * 2 ** (tries - 1), maxMs);
27
+ }
15
28
 
16
29
  //#endregion
17
- export { partialJSONParse, randomUUID };
30
+ export { partialJSONParse, phoenixExponentialBackoff, randomUUID };
18
31
  //# sourceMappingURL=utils.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.mjs","names":["uuidv4"],"sources":["../src/utils.ts"],"sourcesContent":["import { v4 as uuidv4 } from \"uuid\";\nimport * as PartialJSON from \"partial-json\";\n\nexport function randomUUID() {\n return uuidv4();\n}\n\nexport function partialJSONParse(json: string) {\n try {\n return PartialJSON.parse(json);\n } catch (error) {\n return {};\n }\n}\n"],"mappings":";;;;AAGA,SAAgB,aAAa;AAC3B,QAAOA,IAAQ;;AAGjB,SAAgB,iBAAiB,MAAc;AAC7C,KAAI;AACF,SAAO,YAAY,MAAM,KAAK;UACvB,OAAO;AACd,SAAO,EAAE"}
1
+ {"version":3,"file":"utils.mjs","names":["uuidv4"],"sources":["../src/utils.ts"],"sourcesContent":["import { v4 as uuidv4 } from \"uuid\";\nimport * as PartialJSON from \"partial-json\";\n\nexport function randomUUID() {\n return uuidv4();\n}\n\nexport function partialJSONParse(json: string) {\n try {\n return PartialJSON.parse(json);\n } catch (error) {\n return {};\n }\n}\n\n/**\n * Returns an exponential backoff function suitable for Phoenix.js\n * `reconnectAfterMs` and `rejoinAfterMs` options.\n *\n * @param baseMs - Initial delay for the first retry attempt.\n * @param maxMs - Upper bound — delays are capped at this value.\n *\n * Phoenix calls the returned function with a 1-based `tries` count.\n * The delay doubles on each attempt: baseMs, 2×baseMs, 4×baseMs, …, maxMs.\n */\nexport function phoenixExponentialBackoff(\n baseMs: number,\n maxMs: number,\n): (tries: number) => number {\n return (tries: number) => Math.min(baseMs * 2 ** (tries - 1), maxMs);\n}\n"],"mappings":";;;;AAGA,SAAgB,aAAa;AAC3B,QAAOA,IAAQ;;AAGjB,SAAgB,iBAAiB,MAAc;AAC7C,KAAI;AACF,SAAO,YAAY,MAAM,KAAK;UACvB,OAAO;AACd,SAAO,EAAE;;;;;;;;;;;;;AAcb,SAAgB,0BACd,QACA,OAC2B;AAC3B,SAAQ,UAAkB,KAAK,IAAI,SAAS,MAAM,QAAQ,IAAI,MAAM"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@copilotkitnext/shared",
3
- "version": "1.53.0",
3
+ "version": "1.53.1-next.1",
4
4
  "description": "Shared utilities and types for CopilotKit2",
5
5
  "main": "./dist/index.cjs",
6
6
  "types": "./dist/index.d.cts",
@@ -22,8 +22,8 @@
22
22
  "eslint": "^9.30.0",
23
23
  "tsdown": "^0.20.3",
24
24
  "typescript": "5.8.2",
25
- "@copilotkitnext/eslint-config": "1.53.0",
26
- "@copilotkitnext/typescript-config": "1.53.0"
25
+ "@copilotkitnext/eslint-config": "1.53.1-next.1",
26
+ "@copilotkitnext/typescript-config": "1.53.1-next.1"
27
27
  },
28
28
  "dependencies": {
29
29
  "@ag-ui/client": "0.0.47",