openSession method
Future<String>
openSession(
- String instructions, {
- Map<
Tool, ToolCallHandler> ? toolHandlers,
override
Implementation
@override
Future<String> openSession(
String instructions, {
Map<Tool, ToolCallHandler>? toolHandlers,
}) async {
if (instructions.trim().isEmpty) {
throw AppleFoundationException(
'Instructions cannot be empty',
code: 'INVALID_INSTRUCTIONS',
);
}
String? sessionId;
try {
final Map<String, dynamic> arguments = {
'instructions': instructions,
if (toolHandlers != null && toolHandlers.isNotEmpty)
'tools': toolHandlers.keys.map((tool) => tool.toJson()).toList(),
};
sessionId = await _invokeMethodWithTimeout<String>(
'openSession',
arguments,
);
if (sessionId == null || sessionId.isEmpty) {
throw AppleFoundationException(
'Failed to create session - received empty session ID',
code: 'SESSION_CREATION_FAILED',
);
}
if (toolHandlers != null && toolHandlers.isNotEmpty) {
_handlerManager.registerSession(
sessionId: sessionId,
toolHandlers: toolHandlers,
);
}
return sessionId;
} catch (e) {
if (sessionId != null) {
try {
_handlerManager.closeSession(sessionId);
} catch (_) {}
}
_logError('openSession', e);
if (e is AppleFoundationException) {
rethrow;
}
throw AppleFoundationException(
'Failed to open session: ${e.toString()}',
code: 'OPEN_SESSION_FAILED',
details: {'instructions': instructions},
);
}
}