@copilotkitnext/shared 1.51.5-next.0 → 1.51.5-next.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.
Files changed (58) hide show
  1. package/dist/_virtual/_rolldown/runtime.cjs +29 -0
  2. package/dist/constants.cjs +7 -0
  3. package/dist/constants.cjs.map +1 -0
  4. package/dist/constants.d.cts +5 -0
  5. package/dist/constants.d.cts.map +1 -0
  6. package/dist/constants.d.mts +5 -0
  7. package/dist/constants.d.mts.map +1 -0
  8. package/dist/constants.mjs +6 -0
  9. package/dist/constants.mjs.map +1 -0
  10. package/dist/finalize-events.cjs +106 -0
  11. package/dist/finalize-events.cjs.map +1 -0
  12. package/dist/finalize-events.d.cts +11 -0
  13. package/dist/finalize-events.d.cts.map +1 -0
  14. package/dist/finalize-events.d.mts +11 -0
  15. package/dist/finalize-events.d.mts.map +1 -0
  16. package/dist/finalize-events.mjs +105 -0
  17. package/dist/finalize-events.mjs.map +1 -0
  18. package/dist/index.cjs +14 -0
  19. package/dist/index.d.cts +7 -0
  20. package/dist/index.d.mts +7 -84
  21. package/dist/index.mjs +6 -213
  22. package/dist/index.umd.js +245 -2
  23. package/dist/index.umd.js.map +1 -1
  24. package/dist/logger.cjs +7 -0
  25. package/dist/logger.cjs.map +1 -0
  26. package/dist/logger.d.cts +5 -0
  27. package/dist/logger.d.cts.map +1 -0
  28. package/dist/logger.d.mts +5 -0
  29. package/dist/logger.d.mts.map +1 -0
  30. package/dist/logger.mjs +6 -0
  31. package/dist/logger.mjs.map +1 -0
  32. package/dist/transcription-errors.cjs +84 -0
  33. package/dist/transcription-errors.cjs.map +1 -0
  34. package/dist/transcription-errors.d.cts +52 -0
  35. package/dist/transcription-errors.d.cts.map +1 -0
  36. package/dist/transcription-errors.d.mts +52 -0
  37. package/dist/transcription-errors.d.mts.map +1 -0
  38. package/dist/transcription-errors.mjs +82 -0
  39. package/dist/transcription-errors.mjs.map +1 -0
  40. package/dist/types.d.cts +22 -0
  41. package/dist/types.d.cts.map +1 -0
  42. package/dist/types.d.mts +22 -0
  43. package/dist/types.d.mts.map +1 -0
  44. package/dist/utils.cjs +21 -0
  45. package/dist/utils.cjs.map +1 -0
  46. package/dist/utils.d.cts +6 -0
  47. package/dist/utils.d.cts.map +1 -0
  48. package/dist/utils.d.mts +6 -0
  49. package/dist/utils.d.mts.map +1 -0
  50. package/dist/utils.mjs +18 -0
  51. package/dist/utils.mjs.map +1 -0
  52. package/package.json +16 -12
  53. package/tsdown.config.ts +36 -0
  54. package/dist/index.d.ts +0 -84
  55. package/dist/index.js +0 -257
  56. package/dist/index.js.map +0 -1
  57. package/dist/index.mjs.map +0 -1
  58. package/rollup.config.mjs +0 -36
@@ -0,0 +1,29 @@
1
+ //#region \0rolldown/runtime.js
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") {
10
+ for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
11
+ key = keys[i];
12
+ if (!__hasOwnProp.call(to, key) && key !== except) {
13
+ __defProp(to, key, {
14
+ get: ((k) => from[k]).bind(null, key),
15
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
16
+ });
17
+ }
18
+ }
19
+ }
20
+ return to;
21
+ };
22
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
23
+ value: mod,
24
+ enumerable: true
25
+ }) : target, mod));
26
+
27
+ //#endregion
28
+
29
+ exports.__toESM = __toESM;
@@ -0,0 +1,7 @@
1
+
2
+ //#region src/constants.ts
3
+ const DEFAULT_AGENT_ID = "default";
4
+
5
+ //#endregion
6
+ exports.DEFAULT_AGENT_ID = DEFAULT_AGENT_ID;
7
+ //# sourceMappingURL=constants.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.cjs","names":[],"sources":["../src/constants.ts"],"sourcesContent":["export const DEFAULT_AGENT_ID = \"default\";\n"],"mappings":";;AAAA,MAAa,mBAAmB"}
@@ -0,0 +1,5 @@
1
+ //#region src/constants.d.ts
2
+ declare const DEFAULT_AGENT_ID = "default";
3
+ //#endregion
4
+ export { DEFAULT_AGENT_ID };
5
+ //# sourceMappingURL=constants.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.d.cts","names":[],"sources":["../src/constants.ts"],"mappings":";cAAa,gBAAA"}
@@ -0,0 +1,5 @@
1
+ //#region src/constants.d.ts
2
+ declare const DEFAULT_AGENT_ID = "default";
3
+ //#endregion
4
+ export { DEFAULT_AGENT_ID };
5
+ //# sourceMappingURL=constants.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.d.mts","names":[],"sources":["../src/constants.ts"],"mappings":";cAAa,gBAAA"}
@@ -0,0 +1,6 @@
1
+ //#region src/constants.ts
2
+ const DEFAULT_AGENT_ID = "default";
3
+
4
+ //#endregion
5
+ export { DEFAULT_AGENT_ID };
6
+ //# sourceMappingURL=constants.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.mjs","names":[],"sources":["../src/constants.ts"],"sourcesContent":["export const DEFAULT_AGENT_ID = \"default\";\n"],"mappings":";AAAA,MAAa,mBAAmB"}
@@ -0,0 +1,106 @@
1
+ const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
2
+ const require_utils = require('./utils.cjs');
3
+ let _ag_ui_client = require("@ag-ui/client");
4
+
5
+ //#region src/finalize-events.ts
6
+ const defaultStopMessage = "Run stopped by user";
7
+ const defaultAbruptEndMessage = "Run ended without emitting a terminal event";
8
+ function finalizeRunEvents(events, options = {}) {
9
+ const { stopRequested = false, interruptionMessage } = options;
10
+ const resolvedStopMessage = interruptionMessage ?? defaultStopMessage;
11
+ const resolvedAbruptMessage = interruptionMessage && interruptionMessage !== defaultStopMessage ? interruptionMessage : defaultAbruptEndMessage;
12
+ const appended = [];
13
+ const openMessageIds = /* @__PURE__ */ new Set();
14
+ const openToolCalls = /* @__PURE__ */ new Map();
15
+ for (const event of events) switch (event.type) {
16
+ case _ag_ui_client.EventType.TEXT_MESSAGE_START: {
17
+ const messageId = event.messageId;
18
+ if (typeof messageId === "string") openMessageIds.add(messageId);
19
+ break;
20
+ }
21
+ case _ag_ui_client.EventType.TEXT_MESSAGE_END: {
22
+ const messageId = event.messageId;
23
+ if (typeof messageId === "string") openMessageIds.delete(messageId);
24
+ break;
25
+ }
26
+ case _ag_ui_client.EventType.TOOL_CALL_START: {
27
+ const toolCallId = event.toolCallId;
28
+ if (typeof toolCallId === "string") openToolCalls.set(toolCallId, {
29
+ hasEnd: false,
30
+ hasResult: false
31
+ });
32
+ break;
33
+ }
34
+ case _ag_ui_client.EventType.TOOL_CALL_END: {
35
+ const toolCallId = event.toolCallId;
36
+ const info = toolCallId ? openToolCalls.get(toolCallId) : void 0;
37
+ if (info) info.hasEnd = true;
38
+ break;
39
+ }
40
+ case _ag_ui_client.EventType.TOOL_CALL_RESULT: {
41
+ const toolCallId = event.toolCallId;
42
+ const info = toolCallId ? openToolCalls.get(toolCallId) : void 0;
43
+ if (info) info.hasResult = true;
44
+ break;
45
+ }
46
+ default: break;
47
+ }
48
+ const hasRunFinished = events.some((event) => event.type === _ag_ui_client.EventType.RUN_FINISHED);
49
+ const hasRunError = events.some((event) => event.type === _ag_ui_client.EventType.RUN_ERROR);
50
+ const terminalEventMissing = !(hasRunFinished || hasRunError);
51
+ for (const messageId of openMessageIds) {
52
+ const endEvent = {
53
+ type: _ag_ui_client.EventType.TEXT_MESSAGE_END,
54
+ messageId
55
+ };
56
+ events.push(endEvent);
57
+ appended.push(endEvent);
58
+ }
59
+ for (const [toolCallId, info] of openToolCalls) {
60
+ if (!info.hasEnd) {
61
+ const endEvent = {
62
+ type: _ag_ui_client.EventType.TOOL_CALL_END,
63
+ toolCallId
64
+ };
65
+ events.push(endEvent);
66
+ appended.push(endEvent);
67
+ }
68
+ if (terminalEventMissing && !info.hasResult) {
69
+ const resultEvent = {
70
+ type: _ag_ui_client.EventType.TOOL_CALL_RESULT,
71
+ toolCallId,
72
+ messageId: `${toolCallId ?? require_utils.randomUUID()}-result`,
73
+ role: "tool",
74
+ content: JSON.stringify(stopRequested ? {
75
+ status: "stopped",
76
+ reason: "stop_requested",
77
+ message: resolvedStopMessage
78
+ } : {
79
+ status: "error",
80
+ reason: "missing_terminal_event",
81
+ message: resolvedAbruptMessage
82
+ })
83
+ };
84
+ events.push(resultEvent);
85
+ appended.push(resultEvent);
86
+ }
87
+ }
88
+ if (terminalEventMissing) if (stopRequested) {
89
+ const finishedEvent = { type: _ag_ui_client.EventType.RUN_FINISHED };
90
+ events.push(finishedEvent);
91
+ appended.push(finishedEvent);
92
+ } else {
93
+ const errorEvent = {
94
+ type: _ag_ui_client.EventType.RUN_ERROR,
95
+ message: resolvedAbruptMessage,
96
+ code: "INCOMPLETE_STREAM"
97
+ };
98
+ events.push(errorEvent);
99
+ appended.push(errorEvent);
100
+ }
101
+ return appended;
102
+ }
103
+
104
+ //#endregion
105
+ exports.finalizeRunEvents = finalizeRunEvents;
106
+ //# sourceMappingURL=finalize-events.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"finalize-events.cjs","names":["EventType","randomUUID"],"sources":["../src/finalize-events.ts"],"sourcesContent":["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"],"mappings":";;;;;AAQA,MAAM,qBAAqB;AAC3B,MAAM,0BAA0B;AAEhC,SAAgB,kBACd,QACA,UAA8B,EAAE,EACnB;CACb,MAAM,EAAE,gBAAgB,OAAO,wBAAwB;CAEvD,MAAM,sBAAsB,uBAAuB;CACnD,MAAM,wBACJ,uBAAuB,wBAAwB,qBAC3C,sBACA;CAEN,MAAM,WAAwB,EAAE;CAEhC,MAAM,iCAAiB,IAAI,KAAa;CACxC,MAAM,gCAAgB,IAAI,KAMvB;AAEH,MAAK,MAAM,SAAS,OAClB,SAAQ,MAAM,MAAd;EACE,KAAKA,wBAAU,oBAAoB;GACjC,MAAM,YAAa,MAAiC;AACpD,OAAI,OAAO,cAAc,SACvB,gBAAe,IAAI,UAAU;AAE/B;;EAEF,KAAKA,wBAAU,kBAAkB;GAC/B,MAAM,YAAa,MAAiC;AACpD,OA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AACxB,YAAS,KAAK,YAAY;;;AAI9B,KAAI,qBACF,KAAI,eAAe;EACjB,MAAM,gBAAgB,EACpB,MAAMD,wBAAU,cACjB;AACD,SAAO,KAAK,cAAc;AAC1B,WAAS,KAAK,cAAc;QACvB;EACL,MAAM,aAA4B;GAChC,MAAMA,wBAAU;GAChB,SAAS;GACT,MAAM;GACP;AACD,SAAO,KAAK,WAAW;AACvB,WAAS,KAAK,WAAW;;AAI7B,QAAO"}
@@ -0,0 +1,11 @@
1
+ import { BaseEvent } from "@ag-ui/client";
2
+
3
+ //#region src/finalize-events.d.ts
4
+ interface FinalizeRunOptions {
5
+ stopRequested?: boolean;
6
+ interruptionMessage?: string;
7
+ }
8
+ declare function finalizeRunEvents(events: BaseEvent[], options?: FinalizeRunOptions): BaseEvent[];
9
+ //#endregion
10
+ export { finalizeRunEvents };
11
+ //# sourceMappingURL=finalize-events.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"finalize-events.d.cts","names":[],"sources":["../src/finalize-events.ts"],"mappings":";;;UAGU,kBAAA;EACR,aAAA;EACA,mBAAA;AAAA;AAAA,iBAMc,iBAAA,CACd,MAAA,EAAQ,SAAA,IACR,OAAA,GAAS,kBAAA,GACR,SAAA"}
@@ -0,0 +1,11 @@
1
+ import { BaseEvent } from "@ag-ui/client";
2
+
3
+ //#region src/finalize-events.d.ts
4
+ interface FinalizeRunOptions {
5
+ stopRequested?: boolean;
6
+ interruptionMessage?: string;
7
+ }
8
+ declare function finalizeRunEvents(events: BaseEvent[], options?: FinalizeRunOptions): BaseEvent[];
9
+ //#endregion
10
+ export { finalizeRunEvents };
11
+ //# sourceMappingURL=finalize-events.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"finalize-events.d.mts","names":[],"sources":["../src/finalize-events.ts"],"mappings":";;;UAGU,kBAAA;EACR,aAAA;EACA,mBAAA;AAAA;AAAA,iBAMc,iBAAA,CACd,MAAA,EAAQ,SAAA,IACR,OAAA,GAAS,kBAAA,GACR,SAAA"}
@@ -0,0 +1,105 @@
1
+ import { randomUUID } from "./utils.mjs";
2
+ import { EventType } from "@ag-ui/client";
3
+
4
+ //#region src/finalize-events.ts
5
+ const defaultStopMessage = "Run stopped by user";
6
+ const defaultAbruptEndMessage = "Run ended without emitting a terminal event";
7
+ function finalizeRunEvents(events, options = {}) {
8
+ const { stopRequested = false, interruptionMessage } = options;
9
+ const resolvedStopMessage = interruptionMessage ?? defaultStopMessage;
10
+ const resolvedAbruptMessage = interruptionMessage && interruptionMessage !== defaultStopMessage ? interruptionMessage : defaultAbruptEndMessage;
11
+ const appended = [];
12
+ const openMessageIds = /* @__PURE__ */ new Set();
13
+ const openToolCalls = /* @__PURE__ */ new Map();
14
+ for (const event of events) switch (event.type) {
15
+ case EventType.TEXT_MESSAGE_START: {
16
+ const messageId = event.messageId;
17
+ if (typeof messageId === "string") openMessageIds.add(messageId);
18
+ break;
19
+ }
20
+ case EventType.TEXT_MESSAGE_END: {
21
+ const messageId = event.messageId;
22
+ if (typeof messageId === "string") openMessageIds.delete(messageId);
23
+ break;
24
+ }
25
+ case EventType.TOOL_CALL_START: {
26
+ const toolCallId = event.toolCallId;
27
+ if (typeof toolCallId === "string") openToolCalls.set(toolCallId, {
28
+ hasEnd: false,
29
+ hasResult: false
30
+ });
31
+ break;
32
+ }
33
+ case EventType.TOOL_CALL_END: {
34
+ const toolCallId = event.toolCallId;
35
+ const info = toolCallId ? openToolCalls.get(toolCallId) : void 0;
36
+ if (info) info.hasEnd = true;
37
+ break;
38
+ }
39
+ case EventType.TOOL_CALL_RESULT: {
40
+ const toolCallId = event.toolCallId;
41
+ const info = toolCallId ? openToolCalls.get(toolCallId) : void 0;
42
+ if (info) info.hasResult = true;
43
+ break;
44
+ }
45
+ default: break;
46
+ }
47
+ const hasRunFinished = events.some((event) => event.type === EventType.RUN_FINISHED);
48
+ const hasRunError = events.some((event) => event.type === EventType.RUN_ERROR);
49
+ const terminalEventMissing = !(hasRunFinished || hasRunError);
50
+ for (const messageId of openMessageIds) {
51
+ const endEvent = {
52
+ type: EventType.TEXT_MESSAGE_END,
53
+ messageId
54
+ };
55
+ events.push(endEvent);
56
+ appended.push(endEvent);
57
+ }
58
+ for (const [toolCallId, info] of openToolCalls) {
59
+ if (!info.hasEnd) {
60
+ const endEvent = {
61
+ type: EventType.TOOL_CALL_END,
62
+ toolCallId
63
+ };
64
+ events.push(endEvent);
65
+ appended.push(endEvent);
66
+ }
67
+ if (terminalEventMissing && !info.hasResult) {
68
+ const resultEvent = {
69
+ type: EventType.TOOL_CALL_RESULT,
70
+ toolCallId,
71
+ messageId: `${toolCallId ?? randomUUID()}-result`,
72
+ role: "tool",
73
+ content: JSON.stringify(stopRequested ? {
74
+ status: "stopped",
75
+ reason: "stop_requested",
76
+ message: resolvedStopMessage
77
+ } : {
78
+ status: "error",
79
+ reason: "missing_terminal_event",
80
+ message: resolvedAbruptMessage
81
+ })
82
+ };
83
+ events.push(resultEvent);
84
+ appended.push(resultEvent);
85
+ }
86
+ }
87
+ if (terminalEventMissing) if (stopRequested) {
88
+ const finishedEvent = { type: EventType.RUN_FINISHED };
89
+ events.push(finishedEvent);
90
+ appended.push(finishedEvent);
91
+ } else {
92
+ const errorEvent = {
93
+ type: EventType.RUN_ERROR,
94
+ message: resolvedAbruptMessage,
95
+ code: "INCOMPLETE_STREAM"
96
+ };
97
+ events.push(errorEvent);
98
+ appended.push(errorEvent);
99
+ }
100
+ return appended;
101
+ }
102
+
103
+ //#endregion
104
+ export { finalizeRunEvents };
105
+ //# sourceMappingURL=finalize-events.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"finalize-events.mjs","names":[],"sources":["../src/finalize-events.ts"],"sourcesContent":["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"],"mappings":";;;;AAQA,MAAM,qBAAqB;AAC3B,MAAM,0BAA0B;AAEhC,SAAgB,kBACd,QACA,UAA8B,EAAE,EACnB;CACb,MAAM,EAAE,gBAAgB,OAAO,wBAAwB;CAEvD,MAAM,sBAAsB,uBAAuB;CACnD,MAAM,wBACJ,uBAAuB,wBAAwB,qBAC3C,sBACA;CAEN,MAAM,WAAwB,EAAE;CAEhC,MAAM,iCAAiB,IAAI,KAAa;CACxC,MAAM,gCAAgB,IAAI,KAMvB;AAEH,MAAK,MAAM,SAAS,OAClB,SAAQ,MAAM,MAAd;EACE,KAAK,UAAU,oBAAoB;GACjC,MAAM,YAAa,MAAiC;AACpD,OAAI,OAAO,cAAc,SACvB,gBAAe,IAAI,UAAU;AAE/B;;EAEF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KACV,CACN;IACF;AACD,UAAO,KAAK,YAAY;AACxB,YAAS,KAAK,YAAY;;;AAI9B,KAAI,qBACF,KAAI,eAAe;EACjB,MAAM,gBAAgB,EACpB,MAAM,UAAU,cACjB;AACD,SAAO,KAAK,cAAc;AAC1B,WAAS,KAAK,cAAc;QACvB;EACL,MAAM,aAA4B;GAChC,MAAM,UAAU;GAChB,SAAS;GACT,MAAM;GACP;AACD,SAAO,KAAK,WAAW;AACvB,WAAS,KAAK,WAAW;;AAI7B,QAAO"}
package/dist/index.cjs ADDED
@@ -0,0 +1,14 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ const require_utils = require('./utils.cjs');
3
+ const require_logger = require('./logger.cjs');
4
+ const require_constants = require('./constants.cjs');
5
+ const require_finalize_events = require('./finalize-events.cjs');
6
+ const require_transcription_errors = require('./transcription-errors.cjs');
7
+
8
+ exports.DEFAULT_AGENT_ID = require_constants.DEFAULT_AGENT_ID;
9
+ exports.TranscriptionErrorCode = require_transcription_errors.TranscriptionErrorCode;
10
+ exports.TranscriptionErrors = require_transcription_errors.TranscriptionErrors;
11
+ exports.finalizeRunEvents = require_finalize_events.finalizeRunEvents;
12
+ exports.logger = require_logger.logger;
13
+ exports.partialJSONParse = require_utils.partialJSONParse;
14
+ exports.randomUUID = require_utils.randomUUID;
@@ -0,0 +1,7 @@
1
+ import { AgentDescription, MaybePromise, NonEmptyRecord, RuntimeInfo } from "./types.cjs";
2
+ import { partialJSONParse, randomUUID } from "./utils.cjs";
3
+ import { logger } from "./logger.cjs";
4
+ import { DEFAULT_AGENT_ID } from "./constants.cjs";
5
+ import { finalizeRunEvents } from "./finalize-events.cjs";
6
+ import { TranscriptionErrorCode, TranscriptionErrorResponse, TranscriptionErrors } from "./transcription-errors.cjs";
7
+ export { type AgentDescription, DEFAULT_AGENT_ID, type MaybePromise, type NonEmptyRecord, type RuntimeInfo, TranscriptionErrorCode, type TranscriptionErrorResponse, TranscriptionErrors, finalizeRunEvents, logger, partialJSONParse, randomUUID };
package/dist/index.d.mts CHANGED
@@ -1,84 +1,7 @@
1
- import { BaseEvent } from '@ag-ui/client';
2
-
3
- type MaybePromise<T> = T | PromiseLike<T>;
4
- /**
5
- * More specific utility for records with at least one key
6
- */
7
- type NonEmptyRecord<T> = T extends Record<string, unknown> ? keyof T extends never ? never : T : never;
8
- /**
9
- * Type representing an agent's basic information
10
- */
11
- interface AgentDescription {
12
- name: string;
13
- className: string;
14
- description: string;
15
- }
16
- interface RuntimeInfo {
17
- version: string;
18
- agents: Record<string, AgentDescription>;
19
- audioFileTranscriptionEnabled: boolean;
20
- }
21
-
22
- declare function randomUUID(): string;
23
- declare function partialJSONParse(json: string): any;
24
-
25
- declare const logger: Console;
26
-
27
- declare const DEFAULT_AGENT_ID = "default";
28
-
29
- interface FinalizeRunOptions {
30
- stopRequested?: boolean;
31
- interruptionMessage?: string;
32
- }
33
- declare function finalizeRunEvents(events: BaseEvent[], options?: FinalizeRunOptions): BaseEvent[];
34
-
35
- /**
36
- * Error codes for transcription HTTP responses.
37
- * Uses snake_case to align with existing CopilotKitCoreErrorCode pattern.
38
- * These codes are returned by the runtime and parsed by the client.
39
- */
40
- declare enum TranscriptionErrorCode {
41
- /** Transcription service not configured in runtime */
42
- SERVICE_NOT_CONFIGURED = "service_not_configured",
43
- /** Audio format not supported */
44
- INVALID_AUDIO_FORMAT = "invalid_audio_format",
45
- /** Audio file is too long */
46
- AUDIO_TOO_LONG = "audio_too_long",
47
- /** Audio file is empty or too short */
48
- AUDIO_TOO_SHORT = "audio_too_short",
49
- /** Rate limited by transcription provider */
50
- RATE_LIMITED = "rate_limited",
51
- /** Authentication failed with transcription provider */
52
- AUTH_FAILED = "auth_failed",
53
- /** Transcription provider returned an error */
54
- PROVIDER_ERROR = "provider_error",
55
- /** Network error during transcription */
56
- NETWORK_ERROR = "network_error",
57
- /** Invalid request format */
58
- INVALID_REQUEST = "invalid_request"
59
- }
60
- /**
61
- * Error response format returned by the transcription endpoint.
62
- */
63
- interface TranscriptionErrorResponse {
64
- error: TranscriptionErrorCode;
65
- message: string;
66
- retryable?: boolean;
67
- }
68
- /**
69
- * Helper functions to create transcription error responses.
70
- * Used by the runtime to return consistent error responses.
71
- */
72
- declare const TranscriptionErrors: {
73
- serviceNotConfigured: () => TranscriptionErrorResponse;
74
- invalidAudioFormat: (format: string, supported: string[]) => TranscriptionErrorResponse;
75
- invalidRequest: (details: string) => TranscriptionErrorResponse;
76
- rateLimited: () => TranscriptionErrorResponse;
77
- authFailed: () => TranscriptionErrorResponse;
78
- providerError: (message: string) => TranscriptionErrorResponse;
79
- networkError: (message?: string) => TranscriptionErrorResponse;
80
- audioTooLong: () => TranscriptionErrorResponse;
81
- audioTooShort: () => TranscriptionErrorResponse;
82
- };
83
-
84
- export { type AgentDescription, DEFAULT_AGENT_ID, type MaybePromise, type NonEmptyRecord, type RuntimeInfo, TranscriptionErrorCode, type TranscriptionErrorResponse, TranscriptionErrors, finalizeRunEvents, logger, partialJSONParse, randomUUID };
1
+ import { AgentDescription, MaybePromise, NonEmptyRecord, RuntimeInfo } from "./types.mjs";
2
+ import { partialJSONParse, randomUUID } from "./utils.mjs";
3
+ import { logger } from "./logger.mjs";
4
+ import { DEFAULT_AGENT_ID } from "./constants.mjs";
5
+ import { finalizeRunEvents } from "./finalize-events.mjs";
6
+ import { TranscriptionErrorCode, TranscriptionErrorResponse, TranscriptionErrors } from "./transcription-errors.mjs";
7
+ export { type AgentDescription, DEFAULT_AGENT_ID, type MaybePromise, type NonEmptyRecord, type RuntimeInfo, TranscriptionErrorCode, type TranscriptionErrorResponse, TranscriptionErrors, finalizeRunEvents, logger, partialJSONParse, randomUUID };