@copilotkitnext/shared 0.0.0-max-changeset-20260109174803
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/LICENSE +21 -0
- package/dist/index.d.mts +35 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.js +188 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +149 -0
- package/dist/index.mjs.map +1 -0
- package/eslint.config.mjs +3 -0
- package/package.json +42 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Atai Barkai
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
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
|
+
export { type AgentDescription, DEFAULT_AGENT_ID, type MaybePromise, type NonEmptyRecord, type RuntimeInfo, finalizeRunEvents, logger, partialJSONParse, randomUUID };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
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
|
+
export { type AgentDescription, DEFAULT_AGENT_ID, type MaybePromise, type NonEmptyRecord, type RuntimeInfo, finalizeRunEvents, logger, partialJSONParse, randomUUID };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
"use strict";
|
|
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 __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
DEFAULT_AGENT_ID: () => DEFAULT_AGENT_ID,
|
|
34
|
+
finalizeRunEvents: () => finalizeRunEvents,
|
|
35
|
+
logger: () => logger,
|
|
36
|
+
partialJSONParse: () => partialJSONParse,
|
|
37
|
+
randomUUID: () => randomUUID
|
|
38
|
+
});
|
|
39
|
+
module.exports = __toCommonJS(index_exports);
|
|
40
|
+
|
|
41
|
+
// src/utils.ts
|
|
42
|
+
var import_uuid = require("uuid");
|
|
43
|
+
var PartialJSON = __toESM(require("partial-json"));
|
|
44
|
+
function randomUUID() {
|
|
45
|
+
return (0, import_uuid.v4)();
|
|
46
|
+
}
|
|
47
|
+
function partialJSONParse(json) {
|
|
48
|
+
try {
|
|
49
|
+
return PartialJSON.parse(json);
|
|
50
|
+
} catch (error) {
|
|
51
|
+
return {};
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// src/logger.ts
|
|
56
|
+
var logger = console;
|
|
57
|
+
|
|
58
|
+
// src/constants.ts
|
|
59
|
+
var DEFAULT_AGENT_ID = "default";
|
|
60
|
+
|
|
61
|
+
// src/finalize-events.ts
|
|
62
|
+
var import_client = require("@ag-ui/client");
|
|
63
|
+
var defaultStopMessage = "Run stopped by user";
|
|
64
|
+
var defaultAbruptEndMessage = "Run ended without emitting a terminal event";
|
|
65
|
+
function finalizeRunEvents(events, options = {}) {
|
|
66
|
+
const { stopRequested = false, interruptionMessage } = options;
|
|
67
|
+
const resolvedStopMessage = interruptionMessage ?? defaultStopMessage;
|
|
68
|
+
const resolvedAbruptMessage = interruptionMessage && interruptionMessage !== defaultStopMessage ? interruptionMessage : defaultAbruptEndMessage;
|
|
69
|
+
const appended = [];
|
|
70
|
+
const openMessageIds = /* @__PURE__ */ new Set();
|
|
71
|
+
const openToolCalls = /* @__PURE__ */ new Map();
|
|
72
|
+
for (const event of events) {
|
|
73
|
+
switch (event.type) {
|
|
74
|
+
case import_client.EventType.TEXT_MESSAGE_START: {
|
|
75
|
+
const messageId = event.messageId;
|
|
76
|
+
if (typeof messageId === "string") {
|
|
77
|
+
openMessageIds.add(messageId);
|
|
78
|
+
}
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
case import_client.EventType.TEXT_MESSAGE_END: {
|
|
82
|
+
const messageId = event.messageId;
|
|
83
|
+
if (typeof messageId === "string") {
|
|
84
|
+
openMessageIds.delete(messageId);
|
|
85
|
+
}
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
case import_client.EventType.TOOL_CALL_START: {
|
|
89
|
+
const toolCallId = event.toolCallId;
|
|
90
|
+
if (typeof toolCallId === "string") {
|
|
91
|
+
openToolCalls.set(toolCallId, {
|
|
92
|
+
hasEnd: false,
|
|
93
|
+
hasResult: false
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
case import_client.EventType.TOOL_CALL_END: {
|
|
99
|
+
const toolCallId = event.toolCallId;
|
|
100
|
+
const info = toolCallId ? openToolCalls.get(toolCallId) : void 0;
|
|
101
|
+
if (info) {
|
|
102
|
+
info.hasEnd = true;
|
|
103
|
+
}
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
case import_client.EventType.TOOL_CALL_RESULT: {
|
|
107
|
+
const toolCallId = event.toolCallId;
|
|
108
|
+
const info = toolCallId ? openToolCalls.get(toolCallId) : void 0;
|
|
109
|
+
if (info) {
|
|
110
|
+
info.hasResult = true;
|
|
111
|
+
}
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
default:
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
const hasRunFinished = events.some((event) => event.type === import_client.EventType.RUN_FINISHED);
|
|
119
|
+
const hasRunError = events.some((event) => event.type === import_client.EventType.RUN_ERROR);
|
|
120
|
+
const hasTerminalEvent = hasRunFinished || hasRunError;
|
|
121
|
+
const terminalEventMissing = !hasTerminalEvent;
|
|
122
|
+
for (const messageId of openMessageIds) {
|
|
123
|
+
const endEvent = {
|
|
124
|
+
type: import_client.EventType.TEXT_MESSAGE_END,
|
|
125
|
+
messageId
|
|
126
|
+
};
|
|
127
|
+
events.push(endEvent);
|
|
128
|
+
appended.push(endEvent);
|
|
129
|
+
}
|
|
130
|
+
for (const [toolCallId, info] of openToolCalls) {
|
|
131
|
+
if (!info.hasEnd) {
|
|
132
|
+
const endEvent = {
|
|
133
|
+
type: import_client.EventType.TOOL_CALL_END,
|
|
134
|
+
toolCallId
|
|
135
|
+
};
|
|
136
|
+
events.push(endEvent);
|
|
137
|
+
appended.push(endEvent);
|
|
138
|
+
}
|
|
139
|
+
if (terminalEventMissing && !info.hasResult) {
|
|
140
|
+
const resultEvent = {
|
|
141
|
+
type: import_client.EventType.TOOL_CALL_RESULT,
|
|
142
|
+
toolCallId,
|
|
143
|
+
messageId: `${toolCallId ?? randomUUID()}-result`,
|
|
144
|
+
role: "tool",
|
|
145
|
+
content: JSON.stringify(
|
|
146
|
+
stopRequested ? {
|
|
147
|
+
status: "stopped",
|
|
148
|
+
reason: "stop_requested",
|
|
149
|
+
message: resolvedStopMessage
|
|
150
|
+
} : {
|
|
151
|
+
status: "error",
|
|
152
|
+
reason: "missing_terminal_event",
|
|
153
|
+
message: resolvedAbruptMessage
|
|
154
|
+
}
|
|
155
|
+
)
|
|
156
|
+
};
|
|
157
|
+
events.push(resultEvent);
|
|
158
|
+
appended.push(resultEvent);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
if (terminalEventMissing) {
|
|
162
|
+
if (stopRequested) {
|
|
163
|
+
const finishedEvent = {
|
|
164
|
+
type: import_client.EventType.RUN_FINISHED
|
|
165
|
+
};
|
|
166
|
+
events.push(finishedEvent);
|
|
167
|
+
appended.push(finishedEvent);
|
|
168
|
+
} else {
|
|
169
|
+
const errorEvent = {
|
|
170
|
+
type: import_client.EventType.RUN_ERROR,
|
|
171
|
+
message: resolvedAbruptMessage,
|
|
172
|
+
code: "INCOMPLETE_STREAM"
|
|
173
|
+
};
|
|
174
|
+
events.push(errorEvent);
|
|
175
|
+
appended.push(errorEvent);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return appended;
|
|
179
|
+
}
|
|
180
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
181
|
+
0 && (module.exports = {
|
|
182
|
+
DEFAULT_AGENT_ID,
|
|
183
|
+
finalizeRunEvents,
|
|
184
|
+
logger,
|
|
185
|
+
partialJSONParse,
|
|
186
|
+
randomUUID
|
|
187
|
+
});
|
|
188
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/utils.ts","../src/logger.ts","../src/constants.ts","../src/finalize-events.ts"],"sourcesContent":["export {\n type MaybePromise,\n type NonEmptyRecord,\n type AgentDescription,\n type RuntimeInfo,\n} from \"./types\";\n\nexport * from \"./utils\";\n\nexport { logger } from \"./logger\";\nexport { DEFAULT_AGENT_ID } from \"./constants\";\nexport { finalizeRunEvents } from \"./finalize-events\";\n","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","import {\n BaseEvent,\n EventType,\n RunErrorEvent,\n} 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((event) => event.type === EventType.RUN_FINISHED);\n const hasRunError = events.some((event) => event.type === EventType.RUN_ERROR);\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":";;;;;;;;;;;;;;;;;;;;;;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AAW;AACvB,eAAS,KAAK,WAAW;AAAA,IAC3B;AAAA,EACF;AAEA,MAAI,sBAAsB;AACxB,QAAI,eAAe;AACjB,YAAM,gBAAgB;AAAA,QACpB,MAAM,wBAAU;AAAA,MAClB;AACA,aAAO,KAAK,aAAa;AACzB,eAAS,KAAK,aAAa;AAAA,IAC7B,OAAO;AACL,YAAM,aAA4B;AAAA,QAChC,MAAM,wBAAU;AAAA,QAChB,SAAS;AAAA,QACT,MAAM;AAAA,MACR;AACA,aAAO,KAAK,UAAU;AACtB,eAAS,KAAK,UAAU;AAAA,IAC1B;AAAA,EACF;AAEA,SAAO;AACT;","names":["uuidv4"]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
// src/utils.ts
|
|
2
|
+
import { v4 as uuidv4 } from "uuid";
|
|
3
|
+
import * as PartialJSON from "partial-json";
|
|
4
|
+
function randomUUID() {
|
|
5
|
+
return uuidv4();
|
|
6
|
+
}
|
|
7
|
+
function partialJSONParse(json) {
|
|
8
|
+
try {
|
|
9
|
+
return PartialJSON.parse(json);
|
|
10
|
+
} catch (error) {
|
|
11
|
+
return {};
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// src/logger.ts
|
|
16
|
+
var logger = console;
|
|
17
|
+
|
|
18
|
+
// src/constants.ts
|
|
19
|
+
var DEFAULT_AGENT_ID = "default";
|
|
20
|
+
|
|
21
|
+
// src/finalize-events.ts
|
|
22
|
+
import {
|
|
23
|
+
EventType
|
|
24
|
+
} from "@ag-ui/client";
|
|
25
|
+
var defaultStopMessage = "Run stopped by user";
|
|
26
|
+
var defaultAbruptEndMessage = "Run ended without emitting a terminal event";
|
|
27
|
+
function finalizeRunEvents(events, options = {}) {
|
|
28
|
+
const { stopRequested = false, interruptionMessage } = options;
|
|
29
|
+
const resolvedStopMessage = interruptionMessage ?? defaultStopMessage;
|
|
30
|
+
const resolvedAbruptMessage = interruptionMessage && interruptionMessage !== defaultStopMessage ? interruptionMessage : defaultAbruptEndMessage;
|
|
31
|
+
const appended = [];
|
|
32
|
+
const openMessageIds = /* @__PURE__ */ new Set();
|
|
33
|
+
const openToolCalls = /* @__PURE__ */ new Map();
|
|
34
|
+
for (const event of events) {
|
|
35
|
+
switch (event.type) {
|
|
36
|
+
case EventType.TEXT_MESSAGE_START: {
|
|
37
|
+
const messageId = event.messageId;
|
|
38
|
+
if (typeof messageId === "string") {
|
|
39
|
+
openMessageIds.add(messageId);
|
|
40
|
+
}
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
case EventType.TEXT_MESSAGE_END: {
|
|
44
|
+
const messageId = event.messageId;
|
|
45
|
+
if (typeof messageId === "string") {
|
|
46
|
+
openMessageIds.delete(messageId);
|
|
47
|
+
}
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
case EventType.TOOL_CALL_START: {
|
|
51
|
+
const toolCallId = event.toolCallId;
|
|
52
|
+
if (typeof toolCallId === "string") {
|
|
53
|
+
openToolCalls.set(toolCallId, {
|
|
54
|
+
hasEnd: false,
|
|
55
|
+
hasResult: false
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
case EventType.TOOL_CALL_END: {
|
|
61
|
+
const toolCallId = event.toolCallId;
|
|
62
|
+
const info = toolCallId ? openToolCalls.get(toolCallId) : void 0;
|
|
63
|
+
if (info) {
|
|
64
|
+
info.hasEnd = true;
|
|
65
|
+
}
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
case EventType.TOOL_CALL_RESULT: {
|
|
69
|
+
const toolCallId = event.toolCallId;
|
|
70
|
+
const info = toolCallId ? openToolCalls.get(toolCallId) : void 0;
|
|
71
|
+
if (info) {
|
|
72
|
+
info.hasResult = true;
|
|
73
|
+
}
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
default:
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
const hasRunFinished = events.some((event) => event.type === EventType.RUN_FINISHED);
|
|
81
|
+
const hasRunError = events.some((event) => event.type === EventType.RUN_ERROR);
|
|
82
|
+
const hasTerminalEvent = hasRunFinished || hasRunError;
|
|
83
|
+
const terminalEventMissing = !hasTerminalEvent;
|
|
84
|
+
for (const messageId of openMessageIds) {
|
|
85
|
+
const endEvent = {
|
|
86
|
+
type: EventType.TEXT_MESSAGE_END,
|
|
87
|
+
messageId
|
|
88
|
+
};
|
|
89
|
+
events.push(endEvent);
|
|
90
|
+
appended.push(endEvent);
|
|
91
|
+
}
|
|
92
|
+
for (const [toolCallId, info] of openToolCalls) {
|
|
93
|
+
if (!info.hasEnd) {
|
|
94
|
+
const endEvent = {
|
|
95
|
+
type: EventType.TOOL_CALL_END,
|
|
96
|
+
toolCallId
|
|
97
|
+
};
|
|
98
|
+
events.push(endEvent);
|
|
99
|
+
appended.push(endEvent);
|
|
100
|
+
}
|
|
101
|
+
if (terminalEventMissing && !info.hasResult) {
|
|
102
|
+
const resultEvent = {
|
|
103
|
+
type: EventType.TOOL_CALL_RESULT,
|
|
104
|
+
toolCallId,
|
|
105
|
+
messageId: `${toolCallId ?? randomUUID()}-result`,
|
|
106
|
+
role: "tool",
|
|
107
|
+
content: JSON.stringify(
|
|
108
|
+
stopRequested ? {
|
|
109
|
+
status: "stopped",
|
|
110
|
+
reason: "stop_requested",
|
|
111
|
+
message: resolvedStopMessage
|
|
112
|
+
} : {
|
|
113
|
+
status: "error",
|
|
114
|
+
reason: "missing_terminal_event",
|
|
115
|
+
message: resolvedAbruptMessage
|
|
116
|
+
}
|
|
117
|
+
)
|
|
118
|
+
};
|
|
119
|
+
events.push(resultEvent);
|
|
120
|
+
appended.push(resultEvent);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
if (terminalEventMissing) {
|
|
124
|
+
if (stopRequested) {
|
|
125
|
+
const finishedEvent = {
|
|
126
|
+
type: EventType.RUN_FINISHED
|
|
127
|
+
};
|
|
128
|
+
events.push(finishedEvent);
|
|
129
|
+
appended.push(finishedEvent);
|
|
130
|
+
} else {
|
|
131
|
+
const errorEvent = {
|
|
132
|
+
type: EventType.RUN_ERROR,
|
|
133
|
+
message: resolvedAbruptMessage,
|
|
134
|
+
code: "INCOMPLETE_STREAM"
|
|
135
|
+
};
|
|
136
|
+
events.push(errorEvent);
|
|
137
|
+
appended.push(errorEvent);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return appended;
|
|
141
|
+
}
|
|
142
|
+
export {
|
|
143
|
+
DEFAULT_AGENT_ID,
|
|
144
|
+
finalizeRunEvents,
|
|
145
|
+
logger,
|
|
146
|
+
partialJSONParse,
|
|
147
|
+
randomUUID
|
|
148
|
+
};
|
|
149
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/utils.ts","../src/logger.ts","../src/constants.ts","../src/finalize-events.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","import {\n BaseEvent,\n EventType,\n RunErrorEvent,\n} 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((event) => event.type === EventType.RUN_FINISHED);\n const hasRunError = events.some((event) => event.type === EventType.RUN_ERROR);\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":";AAAA,SAAS,MAAM,cAAc;AAC7B,YAAY,iBAAiB;AAEtB,SAAS,aAAa;AAC3B,SAAO,OAAO;AAChB;AAEO,SAAS,iBAAiB,MAAc;AAC7C,MAAI;AACF,WAAmB,kBAAM,IAAI;AAAA,EAC/B,SAAS,OAAO;AACd,WAAO,CAAC;AAAA,EACV;AACF;;;ACbO,IAAM,SAAS;;;ACAf,IAAM,mBAAmB;;;ACAhC;AAAA,EAEE;AAAA,OAEK;AAQP,IAAM,qBAAqB;AAC3B,IAAM,0BAA0B;AAEzB,SAAS,kBACd,QACA,UAA8B,CAAC,GAClB;AACb,QAAM,EAAE,gBAAgB,OAAO,oBAAoB,IAAI;AAEvD,QAAM,sBAAsB,uBAAuB;AACnD,QAAM,wBACJ,uBAAuB,wBAAwB,qBAC3C,sBACA;AAEN,QAAM,WAAwB,CAAC;AAE/B,QAAM,iBAAiB,oBAAI,IAAY;AACvC,QAAM,gBAAgB,oBAAI,IAMxB;AAEF,aAAW,SAAS,QAAQ;AAC1B,YAAQ,MAAM,MAAM;AAAA,MAClB,KAAK,UAAU,oBAAoB;AACjC,cAAM,YAAa,MAAiC;AACpD,YAAI,OAAO,cAAc,UAAU;A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ChB;AAAA,MACF;AACA,aAAO,KAAK,QAAQ;AACpB,eAAS,KAAK,QAAQ;AAAA,IACxB;AAEA,QAAI,wBAAwB,CAAC,KAAK,WAAW;AAC3C,YAAM,cAAc;AAAA,QAClB,MAAM,UAAU;AAAA,QAChB;AAAA,QACA,WAAW,GAAG,cAAc,WAAW,CAAC;AAAA,QACxC,MAAM;AAAA,QACN,SAAS,KAAK;AAAA,UACZ,gBACI;AAAA,YACE,QAAQ;AAAA,YACR,QAAQ;AAAA,YACR,SAAS;AAAA,UACX,IACA;AAAA,YACE,QAAQ;AAAA,YACR,QAAQ;AAAA,YACR,SAAS;AAAA,UACX;AAAA,QACN;AAAA,MACF;AACA,aAAO,KAAK,WAAW;AACvB,eAAS,KAAK,WAAW;AAAA,IAC3B;AAAA,EACF;AAEA,MAAI,sBAAsB;AACxB,QAAI,eAAe;AACjB,YAAM,gBAAgB;AAAA,QACpB,MAAM,UAAU;AAAA,MAClB;AACA,aAAO,KAAK,aAAa;AACzB,eAAS,KAAK,aAAa;AAAA,IAC7B,OAAO;AACL,YAAM,aAA4B;AAAA,QAChC,MAAM,UAAU;AAAA,QAChB,SAAS;AAAA,QACT,MAAM;AAAA,MACR;AACA,aAAO,KAAK,UAAU;AACtB,eAAS,KAAK,UAAU;AAAA,IAC1B;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@copilotkitnext/shared",
|
|
3
|
+
"version": "0.0.0-max-changeset-20260109174803",
|
|
4
|
+
"description": "Shared utilities and types for CopilotKit2",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.mjs",
|
|
11
|
+
"require": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"publishConfig": {
|
|
15
|
+
"access": "public"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@types/node": "^22.15.3",
|
|
19
|
+
"eslint": "^9.30.0",
|
|
20
|
+
"tsup": "^8.5.0",
|
|
21
|
+
"typescript": "5.8.2",
|
|
22
|
+
"@copilotkitnext/eslint-config": "0.0.0-max-changeset-20260109174803",
|
|
23
|
+
"@copilotkitnext/typescript-config": "0.0.0-max-changeset-20260109174803"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"uuid": "^11.1.0",
|
|
27
|
+
"partial-json": "^0.1.7"
|
|
28
|
+
},
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"@ag-ui/client": "0.0.42"
|
|
31
|
+
},
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=18"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "tsup",
|
|
37
|
+
"dev": "tsup --watch",
|
|
38
|
+
"lint": "eslint . --max-warnings 0",
|
|
39
|
+
"check-types": "tsc --noEmit",
|
|
40
|
+
"clean": "rm -rf dist"
|
|
41
|
+
}
|
|
42
|
+
}
|