@ag-ui/proto 0.0.27
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/.turbo/turbo-build.log +23 -0
- package/.turbo/turbo-generate.log +5 -0
- package/__tests__/message-events.test.ts +268 -0
- package/__tests__/proto.test.ts +194 -0
- package/__tests__/run-events.test.ts +277 -0
- package/__tests__/state-events.test.ts +237 -0
- package/__tests__/test-utils.ts +33 -0
- package/__tests__/tool-call-events.test.ts +178 -0
- package/dist/index.d.mts +15 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +2017 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1988 -0
- package/dist/index.mjs.map +1 -0
- package/jest.config.js +5 -0
- package/package.json +35 -0
- package/src/generated/events.ts +1601 -0
- package/src/generated/google/protobuf/struct.ts +467 -0
- package/src/generated/patch.ts +129 -0
- package/src/generated/src/events.ts +1575 -0
- package/src/generated/types.ts +285 -0
- package/src/index.ts +3 -0
- package/src/proto/events.proto +143 -0
- package/src/proto/patch.proto +21 -0
- package/src/proto/types.proto +22 -0
- package/src/proto.ts +91 -0
- package/tsconfig.json +12 -0
- package/tsup.config.ts +10 -0
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
// Code generated by protoc-gen-ts_proto. DO NOT EDIT.
|
|
2
|
+
// versions:
|
|
3
|
+
// protoc-gen-ts_proto v2.7.0
|
|
4
|
+
// protoc v5.29.3
|
|
5
|
+
// source: types.proto
|
|
6
|
+
|
|
7
|
+
/* eslint-disable */
|
|
8
|
+
import { BinaryReader, BinaryWriter } from "@bufbuild/protobuf/wire";
|
|
9
|
+
|
|
10
|
+
export const protobufPackage = "ag_ui";
|
|
11
|
+
|
|
12
|
+
export interface ToolCall {
|
|
13
|
+
id: string;
|
|
14
|
+
type: string;
|
|
15
|
+
function: ToolCall_Function | undefined;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface ToolCall_Function {
|
|
19
|
+
name: string;
|
|
20
|
+
arguments: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface Message {
|
|
24
|
+
id: string;
|
|
25
|
+
role: string;
|
|
26
|
+
content?: string | undefined;
|
|
27
|
+
name?: string | undefined;
|
|
28
|
+
toolCalls: ToolCall[];
|
|
29
|
+
toolCallId?: string | undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function createBaseToolCall(): ToolCall {
|
|
33
|
+
return { id: "", type: "", function: undefined };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export const ToolCall: MessageFns<ToolCall> = {
|
|
37
|
+
encode(message: ToolCall, writer: BinaryWriter = new BinaryWriter()): BinaryWriter {
|
|
38
|
+
if (message.id !== "") {
|
|
39
|
+
writer.uint32(10).string(message.id);
|
|
40
|
+
}
|
|
41
|
+
if (message.type !== "") {
|
|
42
|
+
writer.uint32(18).string(message.type);
|
|
43
|
+
}
|
|
44
|
+
if (message.function !== undefined) {
|
|
45
|
+
ToolCall_Function.encode(message.function, writer.uint32(26).fork()).join();
|
|
46
|
+
}
|
|
47
|
+
return writer;
|
|
48
|
+
},
|
|
49
|
+
|
|
50
|
+
decode(input: BinaryReader | Uint8Array, length?: number): ToolCall {
|
|
51
|
+
const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
|
|
52
|
+
let end = length === undefined ? reader.len : reader.pos + length;
|
|
53
|
+
const message = createBaseToolCall();
|
|
54
|
+
while (reader.pos < end) {
|
|
55
|
+
const tag = reader.uint32();
|
|
56
|
+
switch (tag >>> 3) {
|
|
57
|
+
case 1: {
|
|
58
|
+
if (tag !== 10) {
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
message.id = reader.string();
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
case 2: {
|
|
66
|
+
if (tag !== 18) {
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
message.type = reader.string();
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
case 3: {
|
|
74
|
+
if (tag !== 26) {
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
message.function = ToolCall_Function.decode(reader, reader.uint32());
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if ((tag & 7) === 4 || tag === 0) {
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
reader.skip(tag & 7);
|
|
86
|
+
}
|
|
87
|
+
return message;
|
|
88
|
+
},
|
|
89
|
+
|
|
90
|
+
create<I extends Exact<DeepPartial<ToolCall>, I>>(base?: I): ToolCall {
|
|
91
|
+
return ToolCall.fromPartial(base ?? ({} as any));
|
|
92
|
+
},
|
|
93
|
+
fromPartial<I extends Exact<DeepPartial<ToolCall>, I>>(object: I): ToolCall {
|
|
94
|
+
const message = createBaseToolCall();
|
|
95
|
+
message.id = object.id ?? "";
|
|
96
|
+
message.type = object.type ?? "";
|
|
97
|
+
message.function = (object.function !== undefined && object.function !== null)
|
|
98
|
+
? ToolCall_Function.fromPartial(object.function)
|
|
99
|
+
: undefined;
|
|
100
|
+
return message;
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
function createBaseToolCall_Function(): ToolCall_Function {
|
|
105
|
+
return { name: "", arguments: "" };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export const ToolCall_Function: MessageFns<ToolCall_Function> = {
|
|
109
|
+
encode(message: ToolCall_Function, writer: BinaryWriter = new BinaryWriter()): BinaryWriter {
|
|
110
|
+
if (message.name !== "") {
|
|
111
|
+
writer.uint32(10).string(message.name);
|
|
112
|
+
}
|
|
113
|
+
if (message.arguments !== "") {
|
|
114
|
+
writer.uint32(18).string(message.arguments);
|
|
115
|
+
}
|
|
116
|
+
return writer;
|
|
117
|
+
},
|
|
118
|
+
|
|
119
|
+
decode(input: BinaryReader | Uint8Array, length?: number): ToolCall_Function {
|
|
120
|
+
const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
|
|
121
|
+
let end = length === undefined ? reader.len : reader.pos + length;
|
|
122
|
+
const message = createBaseToolCall_Function();
|
|
123
|
+
while (reader.pos < end) {
|
|
124
|
+
const tag = reader.uint32();
|
|
125
|
+
switch (tag >>> 3) {
|
|
126
|
+
case 1: {
|
|
127
|
+
if (tag !== 10) {
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
message.name = reader.string();
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
case 2: {
|
|
135
|
+
if (tag !== 18) {
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
message.arguments = reader.string();
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
if ((tag & 7) === 4 || tag === 0) {
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
|
+
reader.skip(tag & 7);
|
|
147
|
+
}
|
|
148
|
+
return message;
|
|
149
|
+
},
|
|
150
|
+
|
|
151
|
+
create<I extends Exact<DeepPartial<ToolCall_Function>, I>>(base?: I): ToolCall_Function {
|
|
152
|
+
return ToolCall_Function.fromPartial(base ?? ({} as any));
|
|
153
|
+
},
|
|
154
|
+
fromPartial<I extends Exact<DeepPartial<ToolCall_Function>, I>>(object: I): ToolCall_Function {
|
|
155
|
+
const message = createBaseToolCall_Function();
|
|
156
|
+
message.name = object.name ?? "";
|
|
157
|
+
message.arguments = object.arguments ?? "";
|
|
158
|
+
return message;
|
|
159
|
+
},
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
function createBaseMessage(): Message {
|
|
163
|
+
return { id: "", role: "", content: undefined, name: undefined, toolCalls: [], toolCallId: undefined };
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export const Message: MessageFns<Message> = {
|
|
167
|
+
encode(message: Message, writer: BinaryWriter = new BinaryWriter()): BinaryWriter {
|
|
168
|
+
if (message.id !== "") {
|
|
169
|
+
writer.uint32(10).string(message.id);
|
|
170
|
+
}
|
|
171
|
+
if (message.role !== "") {
|
|
172
|
+
writer.uint32(18).string(message.role);
|
|
173
|
+
}
|
|
174
|
+
if (message.content !== undefined) {
|
|
175
|
+
writer.uint32(26).string(message.content);
|
|
176
|
+
}
|
|
177
|
+
if (message.name !== undefined) {
|
|
178
|
+
writer.uint32(34).string(message.name);
|
|
179
|
+
}
|
|
180
|
+
for (const v of message.toolCalls) {
|
|
181
|
+
ToolCall.encode(v!, writer.uint32(42).fork()).join();
|
|
182
|
+
}
|
|
183
|
+
if (message.toolCallId !== undefined) {
|
|
184
|
+
writer.uint32(50).string(message.toolCallId);
|
|
185
|
+
}
|
|
186
|
+
return writer;
|
|
187
|
+
},
|
|
188
|
+
|
|
189
|
+
decode(input: BinaryReader | Uint8Array, length?: number): Message {
|
|
190
|
+
const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
|
|
191
|
+
let end = length === undefined ? reader.len : reader.pos + length;
|
|
192
|
+
const message = createBaseMessage();
|
|
193
|
+
while (reader.pos < end) {
|
|
194
|
+
const tag = reader.uint32();
|
|
195
|
+
switch (tag >>> 3) {
|
|
196
|
+
case 1: {
|
|
197
|
+
if (tag !== 10) {
|
|
198
|
+
break;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
message.id = reader.string();
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
case 2: {
|
|
205
|
+
if (tag !== 18) {
|
|
206
|
+
break;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
message.role = reader.string();
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
case 3: {
|
|
213
|
+
if (tag !== 26) {
|
|
214
|
+
break;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
message.content = reader.string();
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
case 4: {
|
|
221
|
+
if (tag !== 34) {
|
|
222
|
+
break;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
message.name = reader.string();
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
case 5: {
|
|
229
|
+
if (tag !== 42) {
|
|
230
|
+
break;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
message.toolCalls.push(ToolCall.decode(reader, reader.uint32()));
|
|
234
|
+
continue;
|
|
235
|
+
}
|
|
236
|
+
case 6: {
|
|
237
|
+
if (tag !== 50) {
|
|
238
|
+
break;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
message.toolCallId = reader.string();
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
if ((tag & 7) === 4 || tag === 0) {
|
|
246
|
+
break;
|
|
247
|
+
}
|
|
248
|
+
reader.skip(tag & 7);
|
|
249
|
+
}
|
|
250
|
+
return message;
|
|
251
|
+
},
|
|
252
|
+
|
|
253
|
+
create<I extends Exact<DeepPartial<Message>, I>>(base?: I): Message {
|
|
254
|
+
return Message.fromPartial(base ?? ({} as any));
|
|
255
|
+
},
|
|
256
|
+
fromPartial<I extends Exact<DeepPartial<Message>, I>>(object: I): Message {
|
|
257
|
+
const message = createBaseMessage();
|
|
258
|
+
message.id = object.id ?? "";
|
|
259
|
+
message.role = object.role ?? "";
|
|
260
|
+
message.content = object.content ?? undefined;
|
|
261
|
+
message.name = object.name ?? undefined;
|
|
262
|
+
message.toolCalls = object.toolCalls?.map((e) => ToolCall.fromPartial(e)) || [];
|
|
263
|
+
message.toolCallId = object.toolCallId ?? undefined;
|
|
264
|
+
return message;
|
|
265
|
+
},
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;
|
|
269
|
+
|
|
270
|
+
export type DeepPartial<T> = T extends Builtin ? T
|
|
271
|
+
: T extends globalThis.Array<infer U> ? globalThis.Array<DeepPartial<U>>
|
|
272
|
+
: T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>>
|
|
273
|
+
: T extends {} ? { [K in keyof T]?: DeepPartial<T[K]> }
|
|
274
|
+
: Partial<T>;
|
|
275
|
+
|
|
276
|
+
type KeysOfUnion<T> = T extends T ? keyof T : never;
|
|
277
|
+
export type Exact<P, I extends P> = P extends Builtin ? P
|
|
278
|
+
: P & { [K in keyof P]: Exact<P[K], I[K]> } & { [K in Exclude<keyof I, KeysOfUnion<P>>]: never };
|
|
279
|
+
|
|
280
|
+
export interface MessageFns<T> {
|
|
281
|
+
encode(message: T, writer?: BinaryWriter): BinaryWriter;
|
|
282
|
+
decode(input: BinaryReader | Uint8Array, length?: number): T;
|
|
283
|
+
create<I extends Exact<DeepPartial<T>, I>>(base?: I): T;
|
|
284
|
+
fromPartial<I extends Exact<DeepPartial<T>, I>>(object: I): T;
|
|
285
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
syntax = "proto3";
|
|
2
|
+
|
|
3
|
+
package ag_ui;
|
|
4
|
+
|
|
5
|
+
import "google/protobuf/struct.proto";
|
|
6
|
+
import "patch.proto";
|
|
7
|
+
import "types.proto";
|
|
8
|
+
|
|
9
|
+
enum EventType {
|
|
10
|
+
TEXT_MESSAGE_START = 0;
|
|
11
|
+
TEXT_MESSAGE_CONTENT = 1;
|
|
12
|
+
TEXT_MESSAGE_END = 2;
|
|
13
|
+
TOOL_CALL_START = 3;
|
|
14
|
+
TOOL_CALL_ARGS = 4;
|
|
15
|
+
TOOL_CALL_END = 5;
|
|
16
|
+
STATE_SNAPSHOT = 6;
|
|
17
|
+
STATE_DELTA = 7;
|
|
18
|
+
MESSAGES_SNAPSHOT = 8;
|
|
19
|
+
RAW = 9;
|
|
20
|
+
CUSTOM = 10;
|
|
21
|
+
RUN_STARTED = 11;
|
|
22
|
+
RUN_FINISHED = 12;
|
|
23
|
+
RUN_ERROR = 13;
|
|
24
|
+
STEP_STARTED = 14;
|
|
25
|
+
STEP_FINISHED = 15;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
message BaseEvent {
|
|
29
|
+
EventType type = 1;
|
|
30
|
+
optional int64 timestamp = 2;
|
|
31
|
+
optional google.protobuf.Value raw_event = 3;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
message TextMessageStartEvent {
|
|
35
|
+
BaseEvent base_event = 1;
|
|
36
|
+
string message_id = 2;
|
|
37
|
+
optional string role = 3;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
message TextMessageContentEvent {
|
|
41
|
+
BaseEvent base_event = 1;
|
|
42
|
+
string message_id = 2;
|
|
43
|
+
string delta = 3;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
message TextMessageEndEvent {
|
|
47
|
+
BaseEvent base_event = 1;
|
|
48
|
+
string message_id = 2;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
message ToolCallStartEvent {
|
|
52
|
+
BaseEvent base_event = 1;
|
|
53
|
+
string tool_call_id = 2;
|
|
54
|
+
string tool_call_name = 3;
|
|
55
|
+
optional string parent_message_id = 4;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
message ToolCallArgsEvent {
|
|
59
|
+
BaseEvent base_event = 1;
|
|
60
|
+
string tool_call_id = 2;
|
|
61
|
+
string delta = 3;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
message ToolCallEndEvent {
|
|
65
|
+
BaseEvent base_event = 1;
|
|
66
|
+
string tool_call_id = 2;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
message StateSnapshotEvent {
|
|
70
|
+
BaseEvent base_event = 1;
|
|
71
|
+
google.protobuf.Value snapshot = 2;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
message StateDeltaEvent {
|
|
75
|
+
BaseEvent base_event = 1;
|
|
76
|
+
repeated JsonPatchOperation delta = 2;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
message MessagesSnapshotEvent {
|
|
80
|
+
BaseEvent base_event = 1;
|
|
81
|
+
repeated Message messages = 2;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
message RawEvent {
|
|
85
|
+
BaseEvent base_event = 1;
|
|
86
|
+
google.protobuf.Value event = 2;
|
|
87
|
+
optional string source = 3;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
message CustomEvent {
|
|
91
|
+
BaseEvent base_event = 1;
|
|
92
|
+
string name = 2;
|
|
93
|
+
optional google.protobuf.Value value = 3;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
message RunStartedEvent {
|
|
97
|
+
BaseEvent base_event = 1;
|
|
98
|
+
string thread_id = 2;
|
|
99
|
+
string run_id = 3;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
message RunFinishedEvent {
|
|
103
|
+
BaseEvent base_event = 1;
|
|
104
|
+
string thread_id = 2;
|
|
105
|
+
string run_id = 3;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
message RunErrorEvent {
|
|
109
|
+
BaseEvent base_event = 1;
|
|
110
|
+
optional string code = 2;
|
|
111
|
+
string message = 3;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
message StepStartedEvent {
|
|
115
|
+
BaseEvent base_event = 1;
|
|
116
|
+
string step_name = 2;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
message StepFinishedEvent {
|
|
120
|
+
BaseEvent base_event = 1;
|
|
121
|
+
string step_name = 2;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
message Event {
|
|
125
|
+
oneof event {
|
|
126
|
+
TextMessageStartEvent text_message_start = 1;
|
|
127
|
+
TextMessageContentEvent text_message_content = 2;
|
|
128
|
+
TextMessageEndEvent text_message_end = 3;
|
|
129
|
+
ToolCallStartEvent tool_call_start = 4;
|
|
130
|
+
ToolCallArgsEvent tool_call_args = 5;
|
|
131
|
+
ToolCallEndEvent tool_call_end = 6;
|
|
132
|
+
StateSnapshotEvent state_snapshot = 7;
|
|
133
|
+
StateDeltaEvent state_delta = 8;
|
|
134
|
+
MessagesSnapshotEvent messages_snapshot = 9;
|
|
135
|
+
RawEvent raw = 10;
|
|
136
|
+
CustomEvent custom = 11;
|
|
137
|
+
RunStartedEvent run_started = 12;
|
|
138
|
+
RunFinishedEvent run_finished = 13;
|
|
139
|
+
RunErrorEvent run_error = 14;
|
|
140
|
+
StepStartedEvent step_started = 15;
|
|
141
|
+
StepFinishedEvent step_finished = 16;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
syntax = "proto3";
|
|
2
|
+
|
|
3
|
+
import "google/protobuf/struct.proto";
|
|
4
|
+
|
|
5
|
+
package ag_ui;
|
|
6
|
+
|
|
7
|
+
enum JsonPatchOperationType {
|
|
8
|
+
ADD = 0;
|
|
9
|
+
REMOVE = 1;
|
|
10
|
+
REPLACE = 2;
|
|
11
|
+
MOVE = 3;
|
|
12
|
+
COPY = 4;
|
|
13
|
+
TEST = 5;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
message JsonPatchOperation {
|
|
17
|
+
JsonPatchOperationType op = 1;
|
|
18
|
+
string path = 2;
|
|
19
|
+
optional string from = 3;
|
|
20
|
+
optional google.protobuf.Value value = 4;
|
|
21
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
syntax = "proto3";
|
|
2
|
+
|
|
3
|
+
package ag_ui;
|
|
4
|
+
|
|
5
|
+
message ToolCall {
|
|
6
|
+
string id = 1;
|
|
7
|
+
string type = 2;
|
|
8
|
+
message Function {
|
|
9
|
+
string name = 1;
|
|
10
|
+
string arguments = 2;
|
|
11
|
+
}
|
|
12
|
+
Function function = 3;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
message Message {
|
|
16
|
+
string id = 1;
|
|
17
|
+
string role = 2;
|
|
18
|
+
optional string content = 3;
|
|
19
|
+
optional string name = 4;
|
|
20
|
+
repeated ToolCall tool_calls = 5;
|
|
21
|
+
optional string tool_call_id = 6;
|
|
22
|
+
}
|
package/src/proto.ts
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { BaseEvent, EventSchemas, EventType, Message } from "@ag-ui/core";
|
|
2
|
+
import * as protoEvents from "./generated/events";
|
|
3
|
+
import * as protoPatch from "./generated/patch";
|
|
4
|
+
|
|
5
|
+
function toCamelCase(str: string): string {
|
|
6
|
+
return str.toLowerCase().replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Encodes an event message to a protocol buffer binary format.
|
|
11
|
+
*/
|
|
12
|
+
export function encode(event: BaseEvent): Uint8Array {
|
|
13
|
+
const oneofField = toCamelCase(event.type);
|
|
14
|
+
const { type, timestamp, rawEvent, ...rest } = event as any;
|
|
15
|
+
|
|
16
|
+
// since protobuf does not support optional arrays, we need to ensure that the toolCalls array is always present
|
|
17
|
+
if (type === EventType.MESSAGES_SNAPSHOT) {
|
|
18
|
+
rest.messages = rest.messages.map((message: Message) => {
|
|
19
|
+
const untypedMessage = message as any;
|
|
20
|
+
if (untypedMessage.toolCalls === undefined) {
|
|
21
|
+
return { ...message, toolCalls: [] };
|
|
22
|
+
}
|
|
23
|
+
return message;
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// custom mapping for json patch operations
|
|
28
|
+
if (type === EventType.STATE_DELTA) {
|
|
29
|
+
rest.delta = rest.delta.map((operation: any) => ({
|
|
30
|
+
...operation,
|
|
31
|
+
op: protoPatch.JsonPatchOperationType[operation.op.toUpperCase()],
|
|
32
|
+
}));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const eventMessage = {
|
|
36
|
+
[oneofField]: {
|
|
37
|
+
baseEvent: {
|
|
38
|
+
type: protoEvents.EventType[event.type],
|
|
39
|
+
timestamp,
|
|
40
|
+
rawEvent,
|
|
41
|
+
},
|
|
42
|
+
...rest,
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
return protoEvents.Event.encode(eventMessage).finish();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Decodes a protocol buffer binary format to an event message.
|
|
50
|
+
* The format includes a 4-byte length prefix followed by the message.
|
|
51
|
+
*/
|
|
52
|
+
export function decode(data: Uint8Array): BaseEvent {
|
|
53
|
+
const event = protoEvents.Event.decode(data);
|
|
54
|
+
const decoded = Object.values(event).find((value) => value !== undefined);
|
|
55
|
+
if (!decoded) {
|
|
56
|
+
throw new Error("Invalid event");
|
|
57
|
+
}
|
|
58
|
+
decoded.type = protoEvents.EventType[decoded.baseEvent.type];
|
|
59
|
+
decoded.timestamp = decoded.baseEvent.timestamp;
|
|
60
|
+
decoded.rawEvent = decoded.baseEvent.rawEvent;
|
|
61
|
+
|
|
62
|
+
// we want tool calls to be optional, so we need to remove them if they are empty
|
|
63
|
+
if (decoded.type === EventType.MESSAGES_SNAPSHOT) {
|
|
64
|
+
for (const message of (decoded as any).messages as Message[]) {
|
|
65
|
+
const untypedMessage = message as any;
|
|
66
|
+
if (untypedMessage.toolCalls?.length === 0) {
|
|
67
|
+
untypedMessage.toolCalls = undefined;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// custom mapping for json patch operations
|
|
73
|
+
if (decoded.type === EventType.STATE_DELTA) {
|
|
74
|
+
for (const operation of (decoded as any).delta) {
|
|
75
|
+
operation.op = protoPatch.JsonPatchOperationType[operation.op].toLowerCase();
|
|
76
|
+
Object.keys(operation).forEach((key) => {
|
|
77
|
+
if (operation[key] === undefined) {
|
|
78
|
+
delete operation[key];
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
Object.keys(decoded).forEach((key) => {
|
|
85
|
+
if (decoded[key] === undefined) {
|
|
86
|
+
delete decoded[key];
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
return EventSchemas.parse(decoded);
|
|
91
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"outDir": "./dist",
|
|
5
|
+
"rootDir": "./src",
|
|
6
|
+
"declaration": true,
|
|
7
|
+
"declarationMap": true,
|
|
8
|
+
"sourceMap": true
|
|
9
|
+
},
|
|
10
|
+
"include": ["src/**/*"],
|
|
11
|
+
"exclude": ["node_modules", "dist", "**/__tests__/**"]
|
|
12
|
+
}
|