createConversation method

  1. @override
Future<KletsoConversation> createConversation(
  1. KletsoSession session, {
  2. String? agentId,
  3. String? title,
})
override

POST /v1/conversations.

Implementation

@override
Future<KletsoConversation> createConversation(
  KletsoSession session, {
  String? agentId,
  String? title,
}) async {
  await _latency();
  final s = _auth(session);
  final seed = scenario.seedConversationLog && _conversations.isEmpty;
  final conv = _Conversation(
    id: seed ? 'conv_01J8DEMO000001' : _id('conv'),
    endUserId: s.endUserId,
    agentId: agentId ?? KletsoFakeBackend.agentId,
    title: title,
    createdAt: _now(),
    backend: this,
  );
  _conversations[conv.id] = conv;
  if (seed) {
    final script = KletsoConversationScript(start: _now());
    for (final e in script.build()) {
      conv.log.add(e);
      conv.seq = e.seq;
    }
    conv.status = KletsoConversationStatus.handoff;
  } else {
    conv.add(KletsoEventTypes.conversationCreated, <String, Object?>{
      'conversation': conv.info.toJson(),
    });
    if (scenario.greeting) unawaited(conv.reply(_Reply.greeting()));
  }
  return conv.info;
}