createChatParticipant method
Future<ChatParticipant>
createChatParticipant(
- String id, {
- required ChatRequestHandler handler,
- String? description,
- String? fullName,
- int timeoutSeconds = 300,
Create a chat participant with a request handler
Example:
final participant = await chat.createChatParticipant(
'myExtension.helper',
handler: (request, context, stream) async {
await stream.markdown('Hello from my participant!');
return ChatResult(metadata: {'completed': true});
},
);
Implementation
Future<ChatParticipant> createChatParticipant(
String id, {
required ChatRequestHandler handler,
String? description,
String? fullName,
int timeoutSeconds = 300,
}) async {
// Store the handler
_registerHandler(id, handler);
final result = await _adapter.sendRequest(
'executeScriptVce',
{
'script':
'''
// Create a map to track active chat streams
if (typeof chatStreams === 'undefined') {
globalThis.chatStreams = new Map();
}
const participant = vscode.chat.createChatParticipant('${id.replaceAll("'", "\\'")}', async (request, context, stream, token) => {
try {
// Generate unique stream ID
const streamId = 'stream_' + Date.now() + '_' + Math.random();
chatStreams.set(streamId, stream);
// Send request to Dart handler
const result = await bridge.sendRequest('handleChatRequest', {
participantId: '${id.replaceAll("'", "\\'")}',
streamId: streamId,
request: {
prompt: request.prompt,
command: request.command || '',
references: request.references || []
},
context: {
history: context.history || []
}
});
// Clean up stream
chatStreams.delete(streamId);
return result;
} catch (error) {
stream.error(error);
throw error;
}
});
${description != null ? "participant.description = '${description.replaceAll("'", "\\'")}';" : ''}
${fullName != null ? "participant.fullName = '${fullName.replaceAll("'", "\\'")}';" : ''}
return {
id: participant.id,
description: participant.description,
fullName: participant.fullName
};
''',
},
scriptName: 'createChatParticipant',
timeout: Duration(seconds: timeoutSeconds),
);
return ChatParticipant.fromJson(result);
}