@illalabs/sdk 0.4.0-canary.56299752 → 0.4.0-canary.842eb5db
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/README.md +68 -9
- package/dist/src/chat/Chat.d.ts +31 -2
- package/dist/src/chat/Chat.d.ts.map +1 -1
- package/dist/src/chat/Chat.js +87 -28
- package/dist/src/chat/Chat.js.map +1 -1
- package/dist/src/context/ContextManager.js +1 -1
- package/dist/src/context/ContextManager.js.map +1 -1
- package/dist/src/interfaces/chat.interface.d.ts +52 -1
- package/dist/src/interfaces/chat.interface.d.ts.map +1 -1
- package/dist/src/interfaces/coreApiProvider.interface.d.ts +13 -1
- package/dist/src/interfaces/coreApiProvider.interface.d.ts.map +1 -1
- package/dist/src/interfaces/index.d.ts +1 -1
- package/dist/src/interfaces/index.d.ts.map +1 -1
- package/dist/src/internal.d.ts +1 -0
- package/dist/src/internal.d.ts.map +1 -1
- package/dist/src/internal.js +1 -0
- package/dist/src/internal.js.map +1 -1
- package/dist/src/prompt/Prompt.d.ts +2 -10
- package/dist/src/prompt/Prompt.d.ts.map +1 -1
- package/dist/src/prompt/Prompt.js +9 -22
- package/dist/src/prompt/Prompt.js.map +1 -1
- package/dist/src/prompt/index.d.ts +1 -1
- package/dist/src/prompt/index.d.ts.map +1 -1
- package/dist/src/prompt/types.d.ts +7 -9
- package/dist/src/prompt/types.d.ts.map +1 -1
- package/dist/src/prompt/utils/cloneToolResult.d.ts +6 -6
- package/dist/src/prompt/utils/cloneToolResult.d.ts.map +1 -1
- package/dist/src/prompt/utils/cloneToolResult.js +11 -16
- package/dist/src/prompt/utils/cloneToolResult.js.map +1 -1
- package/dist/src/prompt/utils/index.d.ts +1 -3
- package/dist/src/prompt/utils/index.d.ts.map +1 -1
- package/dist/src/prompt/utils/index.js +1 -3
- package/dist/src/prompt/utils/index.js.map +1 -1
- package/dist/src/providers/coreApiProvider/CoreApiProvider.d.ts +40 -1
- package/dist/src/providers/coreApiProvider/CoreApiProvider.d.ts.map +1 -1
- package/dist/src/providers/coreApiProvider/CoreApiProvider.js +124 -2
- package/dist/src/providers/coreApiProvider/CoreApiProvider.js.map +1 -1
- package/dist/src/providers/coreApiProvider/index.d.ts +1 -1
- package/dist/src/providers/coreApiProvider/index.d.ts.map +1 -1
- package/dist/src/providers/coreApiProvider/types.d.ts +44 -0
- package/dist/src/providers/coreApiProvider/types.d.ts.map +1 -1
- package/dist/src/sdk.d.ts +102 -28
- package/dist/src/sdk.d.ts.map +1 -1
- package/dist/src/sdk.js +90 -23
- package/dist/src/sdk.js.map +1 -1
- package/dist/src/streaming/errors/SSEParse.d.ts +15 -0
- package/dist/src/streaming/errors/SSEParse.d.ts.map +1 -0
- package/dist/src/streaming/errors/SSEParse.js +23 -0
- package/dist/src/streaming/errors/SSEParse.js.map +1 -0
- package/dist/src/streaming/errors/StreamingHttpError.d.ts +18 -0
- package/dist/src/streaming/errors/StreamingHttpError.d.ts.map +1 -0
- package/dist/src/streaming/errors/StreamingHttpError.js +27 -0
- package/dist/src/streaming/errors/StreamingHttpError.js.map +1 -0
- package/dist/src/streaming/errors/StreamingResponseBodyNull.d.ts +13 -0
- package/dist/src/streaming/errors/StreamingResponseBodyNull.d.ts.map +1 -0
- package/dist/src/streaming/errors/StreamingResponseBodyNull.js +21 -0
- package/dist/src/streaming/errors/StreamingResponseBodyNull.js.map +1 -0
- package/dist/src/streaming/errors/StreamingServerError.d.ts +23 -0
- package/dist/src/streaming/errors/StreamingServerError.d.ts.map +1 -0
- package/dist/src/streaming/errors/StreamingServerError.js +33 -0
- package/dist/src/streaming/errors/StreamingServerError.js.map +1 -0
- package/dist/src/streaming/errors/index.d.ts +5 -0
- package/dist/src/streaming/errors/index.d.ts.map +1 -0
- package/dist/src/streaming/errors/index.js +5 -0
- package/dist/src/streaming/errors/index.js.map +1 -0
- package/dist/src/streaming/index.d.ts +4 -0
- package/dist/src/streaming/index.d.ts.map +1 -0
- package/dist/src/streaming/index.js +3 -0
- package/dist/src/streaming/index.js.map +1 -0
- package/dist/src/streaming/parseSSE.d.ts +23 -0
- package/dist/src/streaming/parseSSE.d.ts.map +1 -0
- package/dist/src/streaming/parseSSE.js +152 -0
- package/dist/src/streaming/parseSSE.js.map +1 -0
- package/dist/src/streaming/types.d.ts +6 -0
- package/dist/src/streaming/types.d.ts.map +1 -0
- package/dist/src/streaming/types.js +6 -0
- package/dist/src/streaming/types.js.map +1 -0
- package/dist/src/telemetry/TelemetryClient.d.ts +140 -0
- package/dist/src/telemetry/TelemetryClient.d.ts.map +1 -0
- package/dist/src/telemetry/TelemetryClient.js +225 -0
- package/dist/src/telemetry/TelemetryClient.js.map +1 -0
- package/dist/src/telemetry/index.d.ts +2 -0
- package/dist/src/telemetry/index.d.ts.map +1 -0
- package/dist/src/telemetry/index.js +2 -0
- package/dist/src/telemetry/index.js.map +1 -0
- package/package.json +3 -5
- package/dist/src/prompt/utils/normalizeTextPrompt.d.ts +0 -14
- package/dist/src/prompt/utils/normalizeTextPrompt.d.ts.map +0 -1
- package/dist/src/prompt/utils/normalizeTextPrompt.js +0 -22
- package/dist/src/prompt/utils/normalizeTextPrompt.js.map +0 -1
- package/dist/src/prompt/utils/normalizeToolResult.d.ts +0 -14
- package/dist/src/prompt/utils/normalizeToolResult.d.ts.map +0 -1
- package/dist/src/prompt/utils/normalizeToolResult.js +0 -27
- package/dist/src/prompt/utils/normalizeToolResult.js.map +0 -1
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { SSEParse } from "./SSEParse.js";
|
|
2
|
+
export { StreamingHttpError } from "./StreamingHttpError.js";
|
|
3
|
+
export { StreamingResponseBodyNull } from "./StreamingResponseBodyNull.js";
|
|
4
|
+
export { StreamingServerError } from "./StreamingServerError.js";
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/streaming/errors/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAC7D,OAAO,EAAE,yBAAyB,EAAE,MAAM,gCAAgC,CAAC;AAC3E,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { SSEParse } from "./SSEParse.js";
|
|
2
|
+
export { StreamingHttpError } from "./StreamingHttpError.js";
|
|
3
|
+
export { StreamingResponseBodyNull } from "./StreamingResponseBodyNull.js";
|
|
4
|
+
export { StreamingServerError } from "./StreamingServerError.js";
|
|
5
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/streaming/errors/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAC7D,OAAO,EAAE,yBAAyB,EAAE,MAAM,gCAAgC,CAAC;AAC3E,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { SSEParse, StreamingHttpError, StreamingResponseBodyNull, StreamingServerError, } from "./errors/index.js";
|
|
2
|
+
export { parseSSEStream } from "./parseSSE.js";
|
|
3
|
+
export type { ErrorEventData, ErrorStreamEvent, ResultStreamEvent, SSEStreamEndEvent, SSEStreamEndEventData, SSEStreamEvent, StreamEventType, TelemetryStreamEvent, } from "./types.js";
|
|
4
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/streaming/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,QAAQ,EACR,kBAAkB,EAClB,yBAAyB,EACzB,oBAAoB,GACvB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAC/C,YAAY,EACR,cAAc,EACd,gBAAgB,EAChB,iBAAiB,EACjB,iBAAiB,EACjB,qBAAqB,EACrB,cAAc,EACd,eAAe,EACf,oBAAoB,GACvB,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/streaming/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,QAAQ,EACR,kBAAkB,EAClB,yBAAyB,EACzB,oBAAoB,GACvB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { SSEStreamEvent } from "@illalabs/interfaces";
|
|
2
|
+
/**
|
|
3
|
+
* Async generator that parses SSE events from a ReadableStream.
|
|
4
|
+
*
|
|
5
|
+
* @param stream - The ReadableStream from a fetch response body
|
|
6
|
+
* @param signal - Optional AbortSignal to allow mid-stream cancellation
|
|
7
|
+
* @yields SSEStreamEvent objects as they are parsed from the stream
|
|
8
|
+
* @throws SSEParse if event data cannot be parsed
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const controller = new AbortController();
|
|
13
|
+
* for await (const event of parseSSEStream(response.body!, controller.signal)) {
|
|
14
|
+
* if (event.type === 'telemetry') {
|
|
15
|
+
* console.log('Telemetry:', event.data.type);
|
|
16
|
+
* } else if (event.type === 'result') {
|
|
17
|
+
* console.log('Result:', event.data.text);
|
|
18
|
+
* }
|
|
19
|
+
* }
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export declare function parseSSEStream(stream: ReadableStream<Uint8Array>, signal?: AbortSignal): AsyncGenerator<SSEStreamEvent>;
|
|
23
|
+
//# sourceMappingURL=parseSSE.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parseSSE.d.ts","sourceRoot":"","sources":["../../../src/streaming/parseSSE.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAgH3D;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAuB,cAAc,CACjC,MAAM,EAAE,cAAc,CAAC,UAAU,CAAC,EAClC,MAAM,CAAC,EAAE,WAAW,GACrB,cAAc,CAAC,cAAc,CAAC,CAsChC"}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { errorEventDataSchema, resultEventDataSchema, streamEndEventDataSchema, telemetryEventSchema, } from "@illalabs/interfaces";
|
|
2
|
+
import { ZodError } from "zod";
|
|
3
|
+
import { SSEParse } from "./errors/index.js";
|
|
4
|
+
/**
|
|
5
|
+
* Parses a single SSE message into its event type and data.
|
|
6
|
+
*/
|
|
7
|
+
function parseSSEMessage(message) {
|
|
8
|
+
const lines = message.split("\n");
|
|
9
|
+
let eventType = "message";
|
|
10
|
+
const dataLines = [];
|
|
11
|
+
for (const line of lines) {
|
|
12
|
+
if (line.startsWith("event:")) {
|
|
13
|
+
eventType = line.slice(6).trim();
|
|
14
|
+
}
|
|
15
|
+
else if (line.startsWith("data:")) {
|
|
16
|
+
const value = line.slice(5);
|
|
17
|
+
dataLines.push(value.startsWith(" ") ? value.slice(1) : value);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
const data = dataLines.join("\n");
|
|
21
|
+
return data ? { event: eventType, data } : null;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Formats a Zod validation error into a human-readable message.
|
|
25
|
+
*/
|
|
26
|
+
function formatZodError(error) {
|
|
27
|
+
return error.issues
|
|
28
|
+
.map((issue) => {
|
|
29
|
+
const path = issue.path.length > 0 ? `${issue.path.join(".")}: ` : "";
|
|
30
|
+
return `${path}:${issue.message}`;
|
|
31
|
+
})
|
|
32
|
+
.join("; ");
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Converts a parsed SSE message into a typed SSEStreamEvent.
|
|
36
|
+
* Validates the parsed data at runtime using Zod schemas.
|
|
37
|
+
*/
|
|
38
|
+
function toStreamEvent(event, data) {
|
|
39
|
+
let parsed;
|
|
40
|
+
try {
|
|
41
|
+
parsed = JSON.parse(data);
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
throw new SSEParse(`Failed to parse JSON data: ${data}`, error);
|
|
45
|
+
}
|
|
46
|
+
try {
|
|
47
|
+
switch (event) {
|
|
48
|
+
case "telemetry": {
|
|
49
|
+
const validated = telemetryEventSchema.parse(parsed);
|
|
50
|
+
return { type: "telemetry", data: validated };
|
|
51
|
+
}
|
|
52
|
+
case "result": {
|
|
53
|
+
const validated = resultEventDataSchema.parse(parsed);
|
|
54
|
+
return { type: "result", data: validated };
|
|
55
|
+
}
|
|
56
|
+
case "error": {
|
|
57
|
+
const validated = errorEventDataSchema.parse(parsed);
|
|
58
|
+
return { type: "error", data: validated };
|
|
59
|
+
}
|
|
60
|
+
case "stream_end": {
|
|
61
|
+
const validated = streamEndEventDataSchema.parse(parsed);
|
|
62
|
+
return { type: "stream_end", data: validated };
|
|
63
|
+
}
|
|
64
|
+
default: {
|
|
65
|
+
// Try to infer event type from data.type field if present
|
|
66
|
+
if (typeof parsed === "object" && parsed !== null && "type" in parsed) {
|
|
67
|
+
const typedData = parsed;
|
|
68
|
+
switch (typedData.type) {
|
|
69
|
+
case "result": {
|
|
70
|
+
const validated = resultEventDataSchema.parse(parsed);
|
|
71
|
+
return { type: "result", data: validated };
|
|
72
|
+
}
|
|
73
|
+
case "error": {
|
|
74
|
+
const validated = errorEventDataSchema.parse(parsed);
|
|
75
|
+
return { type: "error", data: validated };
|
|
76
|
+
}
|
|
77
|
+
case "stream_end": {
|
|
78
|
+
const validated = streamEndEventDataSchema.parse(parsed);
|
|
79
|
+
return { type: "stream_end", data: validated };
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
// Default to telemetry for unknown event types
|
|
84
|
+
const validated = telemetryEventSchema.parse(parsed);
|
|
85
|
+
return { type: "telemetry", data: validated };
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
catch (error) {
|
|
90
|
+
if (error instanceof ZodError) {
|
|
91
|
+
throw new SSEParse(`Invalid event data for "${event}": ${formatZodError(error)}`, error);
|
|
92
|
+
}
|
|
93
|
+
throw error;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Async generator that parses SSE events from a ReadableStream.
|
|
98
|
+
*
|
|
99
|
+
* @param stream - The ReadableStream from a fetch response body
|
|
100
|
+
* @param signal - Optional AbortSignal to allow mid-stream cancellation
|
|
101
|
+
* @yields SSEStreamEvent objects as they are parsed from the stream
|
|
102
|
+
* @throws SSEParse if event data cannot be parsed
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```typescript
|
|
106
|
+
* const controller = new AbortController();
|
|
107
|
+
* for await (const event of parseSSEStream(response.body!, controller.signal)) {
|
|
108
|
+
* if (event.type === 'telemetry') {
|
|
109
|
+
* console.log('Telemetry:', event.data.type);
|
|
110
|
+
* } else if (event.type === 'result') {
|
|
111
|
+
* console.log('Result:', event.data.text);
|
|
112
|
+
* }
|
|
113
|
+
* }
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
116
|
+
export async function* parseSSEStream(stream, signal) {
|
|
117
|
+
const reader = stream.getReader();
|
|
118
|
+
const decoder = new TextDecoder();
|
|
119
|
+
let buffer = "";
|
|
120
|
+
try {
|
|
121
|
+
while (true) {
|
|
122
|
+
if (signal?.aborted) {
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
const { done, value } = await reader.read();
|
|
126
|
+
if (done) {
|
|
127
|
+
if (buffer.trim()) {
|
|
128
|
+
const parsed = parseSSEMessage(buffer);
|
|
129
|
+
if (parsed) {
|
|
130
|
+
yield toStreamEvent(parsed.event, parsed.data);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
buffer += decoder.decode(value, { stream: true });
|
|
136
|
+
const messages = buffer.split("\n\n");
|
|
137
|
+
buffer = messages.pop() ?? "";
|
|
138
|
+
for (const message of messages) {
|
|
139
|
+
if (!message.trim())
|
|
140
|
+
continue;
|
|
141
|
+
const parsed = parseSSEMessage(message);
|
|
142
|
+
if (parsed) {
|
|
143
|
+
yield toStreamEvent(parsed.event, parsed.data);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
finally {
|
|
149
|
+
reader.releaseLock();
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
//# sourceMappingURL=parseSSE.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parseSSE.js","sourceRoot":"","sources":["../../../src/streaming/parseSSE.ts"],"names":[],"mappings":"AACA,OAAO,EACH,oBAAoB,EACpB,qBAAqB,EACrB,wBAAwB,EACxB,oBAAoB,GACvB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,QAAQ,EAAE,MAAM,KAAK,CAAC;AAE/B,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAE7C;;GAEG;AACH,SAAS,eAAe,CAAC,OAAe;IACpC,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAClC,IAAI,SAAS,GAAG,SAAS,CAAC;IAC1B,MAAM,SAAS,GAAa,EAAE,CAAC;IAE/B,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACvB,IAAI,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE,CAAC;YAC5B,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;QACrC,CAAC;aAAM,IAAI,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,EAAE,CA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AAI,MAAM,CAAC,IAAI,EAAE,EAAE,CAAC;oBAChB,MAAM,MAAM,GAAG,eAAe,CAAC,MAAM,CAAC,CAAC;oBACvC,IAAI,MAAM,EAAE,CAAC;wBACT,MAAM,aAAa,CAAC,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC;oBACnD,CAAC;gBACL,CAAC;gBACD,MAAM;YACV,CAAC;YAED,MAAM,IAAI,OAAO,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC;YAClD,MAAM,QAAQ,GAAG,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;YACtC,MAAM,GAAG,QAAQ,CAAC,GAAG,EAAE,IAAI,EAAE,CAAC;YAE9B,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE,CAAC;gBAC7B,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE;oBAAE,SAAS;gBAC9B,MAAM,MAAM,GAAG,eAAe,CAAC,OAAO,CAAC,CAAC;gBACxC,IAAI,MAAM,EAAE,CAAC;oBACT,MAAM,aAAa,CAAC,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC;gBACnD,CAAC;YACL,CAAC;QACL,CAAC;IACL,CAAC;YAAS,CAAC;QACP,MAAM,CAAC,WAAW,EAAE,CAAC;IACzB,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Streaming types for SSE chat endpoint.
|
|
3
|
+
* Re-exports types from @illalabs/interfaces for SDK consumers.
|
|
4
|
+
*/
|
|
5
|
+
export type { ErrorEventData, ErrorStreamEvent, ResultStreamEvent, SSEStreamEndEvent, SSEStreamEndEventData, SSEStreamEvent, StreamEventType, TelemetryStreamEvent, } from "@illalabs/interfaces";
|
|
6
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/streaming/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,YAAY,EACR,cAAc,EACd,gBAAgB,EAChB,iBAAiB,EACjB,iBAAiB,EACjB,qBAAqB,EACrB,cAAc,EACd,eAAe,EACf,oBAAoB,GACvB,MAAM,sBAAsB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/streaming/types.ts"],"names":[],"mappings":"AAAA;;;GAGG"}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import type { OrchestratorPhase, TelemetryEvent, TelemetryEventType } from "@illalabs/interfaces";
|
|
2
|
+
/**
|
|
3
|
+
* Configuration for the TelemetryClient
|
|
4
|
+
*/
|
|
5
|
+
export type TelemetryClientConfig = {
|
|
6
|
+
/** Base URL of the API (e.g., "https://api.example.com/v1/chat") */
|
|
7
|
+
baseUrl: string;
|
|
8
|
+
/** Called when an event is received from the stream */
|
|
9
|
+
onEvent?: (event: TelemetryEvent) => void;
|
|
10
|
+
/** Called when the stream completes successfully */
|
|
11
|
+
onComplete?: () => void;
|
|
12
|
+
/** Called when an error occurs */
|
|
13
|
+
onError?: (error: Error) => void;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* State of the telemetry stream
|
|
17
|
+
*/
|
|
18
|
+
export type TelemetryStreamState = {
|
|
19
|
+
/** Whether the stream is currently connected */
|
|
20
|
+
isConnected: boolean;
|
|
21
|
+
/** Whether the stream has completed */
|
|
22
|
+
isComplete: boolean;
|
|
23
|
+
/** All events received from the stream */
|
|
24
|
+
events: TelemetryEvent[];
|
|
25
|
+
/** The last event received */
|
|
26
|
+
lastEvent: TelemetryEvent | null;
|
|
27
|
+
/** Any error that occurred */
|
|
28
|
+
error: Error | null;
|
|
29
|
+
/** Current phase of orchestration */
|
|
30
|
+
currentPhase: OrchestratorPhase;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Error thrown when the telemetry stream fails to connect or encounters an error
|
|
34
|
+
*/
|
|
35
|
+
export declare class TelemetryStreamFailed extends Error {
|
|
36
|
+
/** The request ID that was being streamed */
|
|
37
|
+
readonly requestId?: string;
|
|
38
|
+
/**
|
|
39
|
+
* Creates a new TelemetryStreamFailed error
|
|
40
|
+
* @param message - Description of what went wrong
|
|
41
|
+
* @param requestId - The request ID that was being streamed
|
|
42
|
+
*/
|
|
43
|
+
constructor(message: string, requestId?: string);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Browser-only client for consuming telemetry SSE streams.
|
|
47
|
+
* This client connects to the telemetry endpoint and emits events as they arrive.
|
|
48
|
+
*
|
|
49
|
+
* **Note**: This client requires the native `EventSource` API, which is only available
|
|
50
|
+
* in browser environments. For Node.js usage, you must provide a polyfill such as
|
|
51
|
+
* the `eventsource` package before instantiating this client.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```typescript
|
|
55
|
+
* const client = new TelemetryClient({
|
|
56
|
+
* baseUrl: 'https://api.example.com/v1/chat',
|
|
57
|
+
* onEvent: (event) => console.log('Event:', event.type),
|
|
58
|
+
* onComplete: () => console.log('Stream complete'),
|
|
59
|
+
* onError: (error) => console.error('Error:', error),
|
|
60
|
+
* });
|
|
61
|
+
*
|
|
62
|
+
* client.connect('request-uuid');
|
|
63
|
+
*
|
|
64
|
+
* // Later, disconnect
|
|
65
|
+
* client.disconnect();
|
|
66
|
+
* ```
|
|
67
|
+
*
|
|
68
|
+
* @example Node.js usage with polyfill
|
|
69
|
+
* ```typescript
|
|
70
|
+
* // Install: pnpm add eventsource
|
|
71
|
+
* // At the top of your entry file:
|
|
72
|
+
* import EventSource from 'eventsource';
|
|
73
|
+
* globalThis.EventSource = EventSource as unknown as typeof globalThis.EventSource;
|
|
74
|
+
*
|
|
75
|
+
* // Then use TelemetryClient normally
|
|
76
|
+
* const client = new TelemetryClient({ ... });
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
export declare class TelemetryClient {
|
|
80
|
+
private eventSource;
|
|
81
|
+
private config;
|
|
82
|
+
private currentRequestId;
|
|
83
|
+
private _isConnected;
|
|
84
|
+
private _isComplete;
|
|
85
|
+
/**
|
|
86
|
+
* Creates a new TelemetryClient instance
|
|
87
|
+
* @param config - Configuration including base URL and event callbacks
|
|
88
|
+
*/
|
|
89
|
+
constructor(config: TelemetryClientConfig);
|
|
90
|
+
/**
|
|
91
|
+
* Whether the client is currently connected to a stream
|
|
92
|
+
*/
|
|
93
|
+
get isConnected(): boolean;
|
|
94
|
+
/**
|
|
95
|
+
* Whether the current stream has completed
|
|
96
|
+
*/
|
|
97
|
+
get isComplete(): boolean;
|
|
98
|
+
/**
|
|
99
|
+
* Connect to a telemetry stream for a specific request
|
|
100
|
+
* @param requestId - The request ID to subscribe to
|
|
101
|
+
* @throws {TelemetryStreamFailed} If EventSource is not available in the environment
|
|
102
|
+
*/
|
|
103
|
+
connect(requestId: string): void;
|
|
104
|
+
/**
|
|
105
|
+
* Disconnect from the current stream
|
|
106
|
+
*/
|
|
107
|
+
disconnect(): void;
|
|
108
|
+
/**
|
|
109
|
+
* Close the EventSource without clearing the requestId
|
|
110
|
+
* (used when stream ends normally)
|
|
111
|
+
*/
|
|
112
|
+
private closeEventSource;
|
|
113
|
+
/**
|
|
114
|
+
* Derive orchestrator phase from a telemetry event.
|
|
115
|
+
* This static method provides a consistent mapping from events to phases.
|
|
116
|
+
*
|
|
117
|
+
* @param event - The telemetry event to derive phase from
|
|
118
|
+
* @returns The current orchestrator phase
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* ```typescript
|
|
122
|
+
* const phase = TelemetryClient.getPhaseFromEvent(event);
|
|
123
|
+
* console.log('Current phase:', phase); // e.g., "thinking", "executing_tools"
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
126
|
+
static getPhaseFromEvent(event: TelemetryEvent | null): OrchestratorPhase;
|
|
127
|
+
/**
|
|
128
|
+
* Get a human-readable label for an orchestrator phase
|
|
129
|
+
* @param phase - The phase to get label for
|
|
130
|
+
* @returns Human-readable label
|
|
131
|
+
*/
|
|
132
|
+
static getPhaseLabel(phase: OrchestratorPhase): string;
|
|
133
|
+
/**
|
|
134
|
+
* Check if an event type indicates the stream should end
|
|
135
|
+
* @param type - The event type to check
|
|
136
|
+
* @returns True if the event indicates stream completion
|
|
137
|
+
*/
|
|
138
|
+
static isTerminalEvent(type: TelemetryEventType): boolean;
|
|
139
|
+
}
|
|
140
|
+
//# sourceMappingURL=TelemetryClient.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TelemetryClient.d.ts","sourceRoot":"","sources":["../../../src/telemetry/TelemetryClient.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,cAAc,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAElG;;GAEG;AACH,MAAM,MAAM,qBAAqB,GAAG;IAChC,oEAAoE;IACpE,OAAO,EAAE,MAAM,CAAC;IAChB,uDAAuD;IACvD,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,cAAc,KAAK,IAAI,CAAC;IAC1C,oDAAoD;IACpD,UAAU,CAAC,EAAE,MAAM,IAAI,CAAC;IACxB,kCAAkC;IAClC,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;CACpC,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,oBAAoB,GAAG;IAC/B,gDAAgD;IAChD,WAAW,EAAE,OAAO,CAAC;IACrB,uCAAuC;IACvC,UAAU,EAAE,OAAO,CAAC;IACpB,0CAA0C;IAC1C,MAAM,EAAE,cAAc,EAAE,CAAC;IACzB,8BAA8B;IAC9B,SAAS,EAAE,cAAc,GAAG,IAAI,CAAC;IACjC,8BAA8B;IAC9B,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;IACpB,qCAAqC;IACrC,YAAY,EAAE,iBAAiB,CAAC;CACnC,CAAC;AAEF;;GAEG;AACH,qBAAa,qBAAsB,SAAQ,KAAK;IAC5C,6CAA6C;IAC7C,SAAgB,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnC;;;;OAIG;gBACS,OAAO,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM;CAKlD;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,qBAAa,eAAe;IACxB,OAAO,CAAC,WAAW,CAA4B;IAC/C,OAAO,CAAC,MAAM,CAAwB;IACtC,OAAO,CAAC,gBAAgB,CAAuB;IAC/C,OAAO,CAAC,YAAY,CAAS;IAC7B,OAAO,CAAC,WAAW,CAAS;IAE5B;;;OAGG;gBACS,MAAM,EAAE,qBAAqB;IAIzC;;OAEG;IACH,IAAI,WAAW,IAAI,OAAO,CAEzB;IAED;;OAEG;IACH,IAAI,UAAU,IAAI,OAAO,CAExB;IAED;;;;OAIG;IACH,OAAO,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI;IAiEhC;;OAEG;IACH,UAAU,IAAI,IAAI;IAKlB;;;OAGG;IACH,OAAO,CAAC,gBAAgB;IAQxB;;;;;;;;;;;;OAYG;IACH,MAAM,CAAC,iBAAiB,CAAC,KAAK,EAAE,cAAc,GAAG,IAAI,GAAG,iBAAiB;IA0BzE;;;;OAIG;IACH,MAAM,CAAC,aAAa,CAAC,KAAK,EAAE,iBAAiB,GAAG,MAAM;IAuBtD;;;;OAIG;IACH,MAAM,CAAC,eAAe,CAAC,IAAI,EAAE,kBAAkB,GAAG,OAAO;CAG5D"}
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error thrown when the telemetry stream fails to connect or encounters an error
|
|
3
|
+
*/
|
|
4
|
+
export class TelemetryStreamFailed extends Error {
|
|
5
|
+
/** The request ID that was being streamed */
|
|
6
|
+
requestId;
|
|
7
|
+
/**
|
|
8
|
+
* Creates a new TelemetryStreamFailed error
|
|
9
|
+
* @param message - Description of what went wrong
|
|
10
|
+
* @param requestId - The request ID that was being streamed
|
|
11
|
+
*/
|
|
12
|
+
constructor(message, requestId) {
|
|
13
|
+
super(`Telemetry stream failed: ${message}`);
|
|
14
|
+
this.name = "TelemetryStreamFailed";
|
|
15
|
+
this.requestId = requestId;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Browser-only client for consuming telemetry SSE streams.
|
|
20
|
+
* This client connects to the telemetry endpoint and emits events as they arrive.
|
|
21
|
+
*
|
|
22
|
+
* **Note**: This client requires the native `EventSource` API, which is only available
|
|
23
|
+
* in browser environments. For Node.js usage, you must provide a polyfill such as
|
|
24
|
+
* the `eventsource` package before instantiating this client.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```typescript
|
|
28
|
+
* const client = new TelemetryClient({
|
|
29
|
+
* baseUrl: 'https://api.example.com/v1/chat',
|
|
30
|
+
* onEvent: (event) => console.log('Event:', event.type),
|
|
31
|
+
* onComplete: () => console.log('Stream complete'),
|
|
32
|
+
* onError: (error) => console.error('Error:', error),
|
|
33
|
+
* });
|
|
34
|
+
*
|
|
35
|
+
* client.connect('request-uuid');
|
|
36
|
+
*
|
|
37
|
+
* // Later, disconnect
|
|
38
|
+
* client.disconnect();
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
* @example Node.js usage with polyfill
|
|
42
|
+
* ```typescript
|
|
43
|
+
* // Install: pnpm add eventsource
|
|
44
|
+
* // At the top of your entry file:
|
|
45
|
+
* import EventSource from 'eventsource';
|
|
46
|
+
* globalThis.EventSource = EventSource as unknown as typeof globalThis.EventSource;
|
|
47
|
+
*
|
|
48
|
+
* // Then use TelemetryClient normally
|
|
49
|
+
* const client = new TelemetryClient({ ... });
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
export class TelemetryClient {
|
|
53
|
+
eventSource = null;
|
|
54
|
+
config;
|
|
55
|
+
currentRequestId = null;
|
|
56
|
+
_isConnected = false;
|
|
57
|
+
_isComplete = false;
|
|
58
|
+
/**
|
|
59
|
+
* Creates a new TelemetryClient instance
|
|
60
|
+
* @param config - Configuration including base URL and event callbacks
|
|
61
|
+
*/
|
|
62
|
+
constructor(config) {
|
|
63
|
+
this.config = config;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Whether the client is currently connected to a stream
|
|
67
|
+
*/
|
|
68
|
+
get isConnected() {
|
|
69
|
+
return this._isConnected;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Whether the current stream has completed
|
|
73
|
+
*/
|
|
74
|
+
get isComplete() {
|
|
75
|
+
return this._isComplete;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Connect to a telemetry stream for a specific request
|
|
79
|
+
* @param requestId - The request ID to subscribe to
|
|
80
|
+
* @throws {TelemetryStreamFailed} If EventSource is not available in the environment
|
|
81
|
+
*/
|
|
82
|
+
connect(requestId) {
|
|
83
|
+
// Avoid reconnecting to the same request
|
|
84
|
+
if (this.currentRequestId === requestId && this._isConnected) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
// Runtime check for EventSource availability
|
|
88
|
+
if (typeof EventSource === "undefined") {
|
|
89
|
+
const error = new TelemetryStreamFailed("EventSource is not available. This client requires a browser environment or an EventSource polyfill (e.g., 'eventsource' package for Node.js).", requestId);
|
|
90
|
+
this.config.onError?.(error);
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
this.disconnect();
|
|
94
|
+
const url = `${this.config.baseUrl}/telemetry/${requestId}`;
|
|
95
|
+
this.currentRequestId = requestId;
|
|
96
|
+
this._isComplete = false;
|
|
97
|
+
try {
|
|
98
|
+
const eventSource = new EventSource(url);
|
|
99
|
+
this.eventSource = eventSource;
|
|
100
|
+
eventSource.onopen = () => {
|
|
101
|
+
this._isConnected = true;
|
|
102
|
+
};
|
|
103
|
+
eventSource.onmessage = (event) => {
|
|
104
|
+
try {
|
|
105
|
+
const data = JSON.parse(event.data);
|
|
106
|
+
this.config.onEvent?.(data);
|
|
107
|
+
if (TelemetryClient.isTerminalEvent(data.type)) {
|
|
108
|
+
this._isComplete = true;
|
|
109
|
+
this.config.onComplete?.();
|
|
110
|
+
this.closeEventSource();
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
catch {
|
|
114
|
+
// Ignore malformed JSON - SSE can send comments or other data
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
eventSource.onerror = () => {
|
|
118
|
+
// Check if this is a normal close after stream_end
|
|
119
|
+
if (this.eventSource === null) {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
const error = new TelemetryStreamFailed("Connection error", requestId);
|
|
123
|
+
this._isConnected = false;
|
|
124
|
+
this.config.onError?.(error);
|
|
125
|
+
this.closeEventSource();
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
catch (error) {
|
|
129
|
+
const err = new TelemetryStreamFailed(error instanceof Error ? error.message : String(error), requestId);
|
|
130
|
+
this.config.onError?.(err);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Disconnect from the current stream
|
|
135
|
+
*/
|
|
136
|
+
disconnect() {
|
|
137
|
+
this.closeEventSource();
|
|
138
|
+
this.currentRequestId = null;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Close the EventSource without clearing the requestId
|
|
142
|
+
* (used when stream ends normally)
|
|
143
|
+
*/
|
|
144
|
+
closeEventSource() {
|
|
145
|
+
if (this.eventSource) {
|
|
146
|
+
this.eventSource.close();
|
|
147
|
+
this.eventSource = null;
|
|
148
|
+
}
|
|
149
|
+
this._isConnected = false;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Derive orchestrator phase from a telemetry event.
|
|
153
|
+
* This static method provides a consistent mapping from events to phases.
|
|
154
|
+
*
|
|
155
|
+
* @param event - The telemetry event to derive phase from
|
|
156
|
+
* @returns The current orchestrator phase
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* ```typescript
|
|
160
|
+
* const phase = TelemetryClient.getPhaseFromEvent(event);
|
|
161
|
+
* console.log('Current phase:', phase); // e.g., "thinking", "executing_tools"
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
164
|
+
static getPhaseFromEvent(event) {
|
|
165
|
+
if (!event)
|
|
166
|
+
return "idle";
|
|
167
|
+
switch (event.type) {
|
|
168
|
+
case "connected":
|
|
169
|
+
case "iteration_start":
|
|
170
|
+
case "llm_start":
|
|
171
|
+
return "thinking";
|
|
172
|
+
case "llm_end":
|
|
173
|
+
case "tool_call_batch":
|
|
174
|
+
return "calling_tools";
|
|
175
|
+
case "tool_result":
|
|
176
|
+
return "executing_tools";
|
|
177
|
+
case "iteration_end":
|
|
178
|
+
case "scratchpad_reset":
|
|
179
|
+
return "processing_results";
|
|
180
|
+
case "finish":
|
|
181
|
+
case "stream_end":
|
|
182
|
+
return "complete";
|
|
183
|
+
case "error":
|
|
184
|
+
return "error";
|
|
185
|
+
default:
|
|
186
|
+
return "thinking";
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Get a human-readable label for an orchestrator phase
|
|
191
|
+
* @param phase - The phase to get label for
|
|
192
|
+
* @returns Human-readable label
|
|
193
|
+
*/
|
|
194
|
+
static getPhaseLabel(phase) {
|
|
195
|
+
switch (phase) {
|
|
196
|
+
case "idle":
|
|
197
|
+
return "Idle";
|
|
198
|
+
case "thinking":
|
|
199
|
+
return "Thinking...";
|
|
200
|
+
case "calling_tools":
|
|
201
|
+
return "Calling tools...";
|
|
202
|
+
case "executing_tools":
|
|
203
|
+
return "Executing tools...";
|
|
204
|
+
case "processing_results":
|
|
205
|
+
return "Processing results...";
|
|
206
|
+
case "complete":
|
|
207
|
+
return "Complete";
|
|
208
|
+
case "error":
|
|
209
|
+
return "Error";
|
|
210
|
+
case "streaming":
|
|
211
|
+
return "Streaming...";
|
|
212
|
+
default:
|
|
213
|
+
return "Unknown";
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Check if an event type indicates the stream should end
|
|
218
|
+
* @param type - The event type to check
|
|
219
|
+
* @returns True if the event indicates stream completion
|
|
220
|
+
*/
|
|
221
|
+
static isTerminalEvent(type) {
|
|
222
|
+
return type === "stream_end" || type === "finish" || type === "error";
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
//# sourceMappingURL=TelemetryClient.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TelemetryClient.js","sourceRoot":"","sources":["../../../src/telemetry/TelemetryClient.ts"],"names":[],"mappings":"AAkCA;;GAEG;AACH,MAAM,OAAO,qBAAsB,SAAQ,KAAK;IAC5C,6CAA6C;IAC7B,SAAS,CAAU;IAEnC;;;;OAIG;IACH,YAAY,OAAe,EAAE,SAAkB;QAC3C,KAAK,CAAC,4BAA4B,OAAO,EAAE,CAAC,CAAC;QAC7C,IAAI,CAAC,IAAI,GAAG,uBAAuB,CAAC;QACpC,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAC/B,CAAC;CACJ;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,MAAM,OAAO,eAAe;IAChB,WAAW,GAAuB,IAAI,CAAC;IACvC,MAAM,CAAwB;IAC9B,gBAAgB,GAAkB,IAAI,CAAC;IACvC,YAAY,GAAG,KAAK,CAAC;IACrB,WAAW,GAAG,KAAK,CAAC;IAE5B;;;OAGG;IACH,YAAY,MAA6B;QACrC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACzB,CAAC;IAED;;OAEG;IACH,IAAI,WAAW;QACX,OAAO,IAAI,CAAC,YAAY,CAAC;IAC7B,CAAC;IAED;;OAEG;IACH,IAAI,UAAU;QACV,OAAO,IAAI,CAAC,WAAW,CAAC;IAC5B,CAAC;IAED;;;;OAIG;IACH,OAAO,CAAC,SAAiB;QACrB,yCAAyC;QACzC,IAAI,IAAI,CAAC,gBAAgB,KAAK,SAAS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1B,KAAK,oBAAoB,GAC5B,MAAM,sBAAsB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/telemetry/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,eAAe,EACf,qBAAqB,GAGxB,MAAM,sBAAsB,CAAC"}
|