Skip to content

Commit 73ab364

Browse files
committed
added chunkstream method to chaatAi
1 parent 7347bc1 commit 73ab364

File tree

4 files changed

+129
-3
lines changed

4 files changed

+129
-3
lines changed

example/src/App.tsx

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,6 +1307,61 @@ const App = () => {
13071307
/>
13081308
</View>
13091309

1310+
<View style={styles.section}>
1311+
<Button
1312+
title={inprogress ? 'Getting...' : 'Test Stream Chunks'}
1313+
onPress={async () => {
1314+
try {
1315+
if (initComplete) {
1316+
// Step 1: Check connection to Blox
1317+
const isConnected = await fula.checkConnection();
1318+
console.log('Connection check:', isConnected);
1319+
1320+
if (isConnected) {
1321+
console.log('Initialization is completed. Starting ChatWithAI...');
1322+
1323+
// Step 2: Start Chat with AI
1324+
try {
1325+
const streamID = await fxAi.chatWithAI('deepseek-chart', 'Hello AI!');
1326+
console.log('ChatWithAI started, Stream ID:', streamID);
1327+
1328+
// Step 3: Use streamChunks to receive responses in real-time
1329+
let fullResponse = '';
1330+
const cleanup = fxAi.streamChunks(streamID, {
1331+
onChunk: (chunk) => {
1332+
console.log('Received chunk:', chunk);
1333+
fullResponse += chunk;
1334+
},
1335+
onComplete: () => {
1336+
console.log('Stream completed. Full response:', fullResponse);
1337+
},
1338+
onError: (error) => {
1339+
console.error('Stream error:', error);
1340+
},
1341+
});
1342+
1343+
// Optional: Clean up after some time (e.g., if you want to stop receiving chunks)
1344+
setTimeout(() => {
1345+
cleanup();
1346+
console.log('Cleaned up stream listeners');
1347+
}, 30000); // Cleanup after 30 seconds
1348+
1349+
} catch (startError) {
1350+
console.error('Error starting ChatWithAI:', startError);
1351+
}
1352+
} else {
1353+
console.log('Connection to Blox failed. Please check your connection.');
1354+
}
1355+
} else {
1356+
console.log('Wait for initialization to complete.');
1357+
}
1358+
} catch (e) {
1359+
console.error('Unexpected error:', e);
1360+
}
1361+
}}
1362+
color={inprogress ? 'green' : 'blue'}
1363+
/>
1364+
</View>
13101365

13111366
</ScrollView>
13121367
);

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@functionland/react-native-fula",
3-
"version": "1.55.1",
3+
"version": "1.55.2",
44
"description": "This package is a bridge to use the Fula libp2p protocols in the react-native which is using wnfs",
55
"type": "module",
66
"main": "lib/commonjs/index",

src/protocols/fx-ai.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,75 @@ import { DeviceEventEmitter } from 'react-native';
108108
}
109109
});
110110
});
111+
};
112+
113+
export interface StreamCallbacks {
114+
onChunk?: (chunk: string) => void;
115+
onComplete?: () => void;
116+
onError?: (error: Error) => void;
117+
}
118+
119+
export const streamChunks = (streamID: string, callbacks: StreamCallbacks): () => void => {
120+
let active = true;
121+
122+
const cleanup = () => {
123+
active = false;
124+
DeviceEventEmitter.removeAllListeners('onChunkReceived');
125+
DeviceEventEmitter.removeAllListeners('onStreamingCompleted');
126+
DeviceEventEmitter.removeAllListeners('onStreamError');
127+
};
128+
129+
const chunkHandler = (chunk: string) => {
130+
if (active && callbacks.onChunk) {
131+
callbacks.onChunk(chunk);
132+
}
133+
};
134+
135+
const completionHandler = () => {
136+
if (active) {
137+
cleanup();
138+
if (callbacks.onComplete) {
139+
callbacks.onComplete();
140+
}
141+
}
142+
};
143+
144+
const errorHandler = (error: string) => {
145+
if (active) {
146+
cleanup();
147+
if (error.includes('EOF')) {
148+
// Treat EOF as successful completion
149+
console.log('Stream completed with EOF');
150+
if (callbacks.onComplete) {
151+
callbacks.onComplete();
152+
}
153+
} else {
154+
console.error('Stream error:', error);
155+
if (callbacks.onError) {
156+
callbacks.onError(new Error(error || 'Unknown stream error'));
157+
}
158+
}
159+
}
160+
};
161+
162+
DeviceEventEmitter.addListener('onChunkReceived', chunkHandler);
163+
DeviceEventEmitter.addListener('onStreamingCompleted', completionHandler);
164+
DeviceEventEmitter.addListener('onStreamError', errorHandler);
165+
166+
Fula.streamChunks(streamID)
167+
.catch(error => {
168+
if (active) {
169+
cleanup();
170+
if (error.message.includes('EOF')) {
171+
if (callbacks.onComplete) {
172+
callbacks.onComplete();
173+
}
174+
} else if (callbacks.onError) {
175+
callbacks.onError(error);
176+
}
177+
}
178+
});
179+
180+
// Return a cleanup function that the caller can use to stop listening
181+
return cleanup;
111182
};

0 commit comments

Comments
 (0)