open method

Future<RagSession> open({
  1. required ModelRef embeddingModel,
  2. ModelRef? llmModel,
  3. RagConfig? config,
})

Open a session over embeddingModel, optionally generating with llmModel.

final s = await RunAnywhere.rag.open(embeddingModel: ModelRef('minilm'));
await s.ingest(const RagDocument('the sky is blue'));

The native RAG pipeline is process-wide (one pipeline, not a per-session handle — matching RN/Web), so a second concurrent session is rejected instead of silently replacing the first. Close the active session before opening another.

Throws SDKException when a session is already open, a model cannot be loaded, or the index cannot be created.

Implementation

Future<RagSession> open({
  required ModelRef embeddingModel,
  ModelRef? llmModel,
  RagConfig? config,
}) async {
  if (!DartBridge.isInitialized) {
    throw SDKException.notInitialized();
  }
  if (RagSession._active != null) {
    throw SDKException.invalidState(
      'A RAG session is already open; close it before opening another',
    );
  }
  await DartBridge.ensureServicesReady();
  // The RAG backend registers itself here so callers never do backend wiring.
  DartBridgeRAG.shared.register();
  await ModelGate.ensureLoaded(
    modelId: embeddingModel.id,
    category: ModelCategory.MODEL_CATEGORY_EMBEDDING,
  );
  if (llmModel != null) {
    await ModelGate.ensureLoaded(
      modelId: llmModel.id,
      category: ModelCategory.MODEL_CATEGORY_LANGUAGE,
    );
  }

  final effective = config ?? RagConfig();
  try {
    await DartBridgeRAG.shared.createPipelineAsync(
      effective.toProto(
        embeddingModelId: embeddingModel.id,
        llmModelId: llmModel?.id,
      ),
    );
  } catch (error) {
    throw SDKException.invalidState('RAG session creation failed: $error');
  }
  return RagSession._(effective, generates: llmModel != null);
}