@tangle-network/agent-app 3.0.28 → 3.0.30

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.
@@ -46,92 +46,9 @@ import "../chunk-T3C2BX7N.js";
46
46
  import "../chunk-TXD5HXLE.js";
47
47
 
48
48
  // src/assistant/sse.ts
49
- var SSEChunkParser = class {
50
- buffer = "";
51
- current = {};
52
- push(chunk) {
53
- this.buffer += chunk;
54
- const lines = this.buffer.split("\n");
55
- this.buffer = lines.pop() ?? "";
56
- return this.processLines(lines);
57
- }
58
- flush() {
59
- const lines = this.buffer ? [this.buffer] : [];
60
- this.buffer = "";
61
- const events = this.processLines(lines);
62
- const finalEvent = this.parseCurrent();
63
- if (finalEvent) {
64
- events.push(finalEvent);
65
- this.current = {};
66
- }
67
- return events;
68
- }
69
- processLines(lines) {
70
- const events = [];
71
- for (const rawLine of lines) {
72
- const line = rawLine.endsWith("\r") ? rawLine.slice(0, -1) : rawLine;
73
- if (line.startsWith(":")) continue;
74
- if (line === "") {
75
- const parsed = this.parseCurrent();
76
- if (parsed) events.push(parsed);
77
- this.current = {};
78
- continue;
79
- }
80
- if (line.startsWith("id:")) {
81
- this.current.id = line.slice(3).trim();
82
- } else if (line.startsWith("event:")) {
83
- this.current.event = line.slice(6).trim();
84
- } else if (line.startsWith("data:")) {
85
- let value = line.slice(5);
86
- if (value.startsWith(" ")) value = value.slice(1);
87
- this.current.data = this.current.data !== void 0 ? `${this.current.data}
88
- ${value}` : value;
89
- }
90
- }
91
- return events;
92
- }
93
- parseCurrent() {
94
- if (this.current.data === void 0) return null;
95
- const rawData = this.current.data.trim();
96
- if (!rawData) return null;
97
- let data;
98
- try {
99
- data = JSON.parse(rawData);
100
- } catch {
101
- data = rawData;
102
- }
103
- return {
104
- data,
105
- rawData,
106
- eventId: this.current.id,
107
- eventType: this.current.event
108
- };
109
- }
110
- };
49
+ import { parseSSEStream } from "@tangle-network/agent-core/sse";
111
50
  async function readSSEEvents(body, onEvent) {
112
- const reader = body.getReader();
113
- const decoder = new TextDecoder();
114
- const parser = new SSEChunkParser();
115
- try {
116
- while (true) {
117
- const { done, value } = await reader.read();
118
- if (done) break;
119
- for (const event of parser.push(decoder.decode(value, { stream: true }))) {
120
- onEvent(event);
121
- }
122
- }
123
- const tail = decoder.decode();
124
- if (tail) {
125
- for (const event of parser.push(tail)) onEvent(event);
126
- }
127
- for (const event of parser.flush()) onEvent(event);
128
- } catch (err) {
129
- await reader.cancel(err).catch(() => {
130
- });
131
- throw err;
132
- } finally {
133
- reader.releaseLock();
134
- }
51
+ for await (const event of parseSSEStream(body)) onEvent(event);
135
52
  }
136
53
 
137
54
  // src/assistant/client.ts