-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAiSpeechAssistantService.AssistantCustom.cs
More file actions
488 lines (362 loc) · 23 KB
/
AiSpeechAssistantService.AssistantCustom.cs
File metadata and controls
488 lines (362 loc) · 23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
using Serilog;
using System.Text;
using Newtonsoft.Json.Linq;
using System.Globalization;
using SmartTalk.Core.Domain.AISpeechAssistant;
using SmartTalk.Core.Domain.Restaurants;
using SmartTalk.Core.Domain.System;
using SmartTalk.Messages.Commands.AiSpeechAssistant;
using SmartTalk.Messages.Constants;
using SmartTalk.Messages.Dto.Agent;
using SmartTalk.Messages.Dto.AiSpeechAssistant;
using SmartTalk.Messages.Enums.Agent;
using SmartTalk.Messages.Enums.AiSpeechAssistant;
using SmartTalk.Messages.Enums.Caching;
using SmartTalk.Messages.Events.AiSpeechAssistant;
namespace SmartTalk.Core.Services.AiSpeechAssistant;
public partial interface IAiSpeechAssistantService
{
Task<AddAiSpeechAssistantResponse> AddAiSpeechAssistantAsync(AddAiSpeechAssistantCommand command, CancellationToken cancellationToken);
Task<AiSpeechAssistantKnowledgeAddedEvent> AddAiSpeechAssistantKnowledgeAsync(AddAiSpeechAssistantKnowledgeCommand command, CancellationToken cancellationToken);
Task<SwitchAiSpeechAssistantKnowledgeVersionResponse> SwitchAiSpeechAssistantKnowledgeVersionAsync(SwitchAiSpeechAssistantKnowledgeVersionCommand command, CancellationToken cancellationToken);
Task<UpdateAiSpeechAssistantResponse> UpdateAiSpeechAssistantAsync(UpdateAiSpeechAssistantCommand command, CancellationToken cancellationToken);
Task<DeleteAiSpeechAssistantResponse> DeleteAiSpeechAssistantAsync(DeleteAiSpeechAssistantCommand command, CancellationToken cancellationToken);
Task<UpdateAiSpeechAssistantNumberResponse> UpdateAiSpeechAssistantNumberAsync(UpdateAiSpeechAssistantNumberCommand command, CancellationToken cancellationToken);
Task<UpdateAiSpeechAssistantKnowledgeResponse> UpdateAiSpeechAssistantKnowledgeAsync(UpdateAiSpeechAssistantKnowledgeCommand command, CancellationToken cancellationToken);
Task<UpdateAiSpeechAssistantSessionResponse> UpdateAiSpeechAssistantSessionAsync(UpdateAiSpeechAssistantSessionCommand command, CancellationToken cancellationToken);
Task<AddAiSpeechAssistantSessionResponse> AddAiSpeechAssistantSessionAsync(AddAiSpeechAssistantSessionCommand command, CancellationToken cancellationToken);
}
public partial class AiSpeechAssistantService
{
public async Task<AddAiSpeechAssistantResponse> AddAiSpeechAssistantAsync(AddAiSpeechAssistantCommand command, CancellationToken cancellationToken)
{
var assistant = await InitialAiSpeechAssistantAsync(command, cancellationToken).ConfigureAwait(false);
return new AddAiSpeechAssistantResponse
{
Data = _mapper.Map<AiSpeechAssistantDto>(assistant)
};
}
public async Task<AiSpeechAssistantKnowledgeAddedEvent> AddAiSpeechAssistantKnowledgeAsync(AddAiSpeechAssistantKnowledgeCommand command, CancellationToken cancellationToken)
{
var prevKnowledge = await UpdatePreviousKnowledgeIfRequiredAsync(command.AssistantId, false, cancellationToken).ConfigureAwait(false);
var latestKnowledge = _mapper.Map<AiSpeechAssistantKnowledge>(command);
await InitialKnowledgeAsync(latestKnowledge, cancellationToken).ConfigureAwait(false);
await _aiSpeechAssistantDataProvider.AddAiSpeechAssistantKnowledgesAsync([latestKnowledge], cancellationToken: cancellationToken).ConfigureAwait(false);
return new AiSpeechAssistantKnowledgeAddedEvent
{
PrevKnowledge = _mapper.Map<AiSpeechAssistantKnowledgeDto>(prevKnowledge),
LatestKnowledge = _mapper.Map<AiSpeechAssistantKnowledgeDto>(latestKnowledge)
};
}
public async Task<SwitchAiSpeechAssistantKnowledgeVersionResponse> SwitchAiSpeechAssistantKnowledgeVersionAsync(SwitchAiSpeechAssistantKnowledgeVersionCommand command, CancellationToken cancellationToken)
{
var preKnowledge = await UpdatePreviousKnowledgeIfRequiredAsync(command.AssistantId, false, cancellationToken).ConfigureAwait(false);
if (preKnowledge == null) throw new Exception("Could not found the active knowledge!");
var currentKnowledge = await _aiSpeechAssistantDataProvider.GetAiSpeechAssistantKnowledgeAsync(
command.AssistantId, command.KnowledgeId, cancellationToken: cancellationToken).ConfigureAwait(false);
if (currentKnowledge == null) throw new Exception($"Could not found the knowledge by knowledge id: {command.KnowledgeId}!");
await UpdateKnowledgeStatusAsync(currentKnowledge, true, cancellationToken).ConfigureAwait(false);
return new SwitchAiSpeechAssistantKnowledgeVersionResponse
{
Data = _mapper.Map<AiSpeechAssistantKnowledgeDto>(currentKnowledge)
};
}
public async Task<UpdateAiSpeechAssistantResponse> UpdateAiSpeechAssistantAsync(UpdateAiSpeechAssistantCommand command, CancellationToken cancellationToken)
{
var assistant = await _aiSpeechAssistantDataProvider.GetAiSpeechAssistantAsync(command.AssistantId, cancellationToken).ConfigureAwait(false);
await UpdateAssistantNumberIfRequiredAsync(assistant.AnsweringNumberId, command.AnsweringNumberId, cancellationToken).ConfigureAwait(false);
_mapper.Map(command, assistant);
assistant.Channel = command.Channels == null ? null : string.Join(",", command.Channels.Select(x => (int)x));
await _aiSpeechAssistantDataProvider.UpdateAiSpeechAssistantsAsync([assistant], cancellationToken: cancellationToken).ConfigureAwait(false);
return new UpdateAiSpeechAssistantResponse
{
Data = _mapper.Map<AiSpeechAssistantDto>(assistant)
};
}
public async Task<DeleteAiSpeechAssistantResponse> DeleteAiSpeechAssistantAsync(DeleteAiSpeechAssistantCommand command, CancellationToken cancellationToken)
{
var assistants = await _aiSpeechAssistantDataProvider.DeleteAiSpeechAssistantsAsync(
command.AssistantIds, cancellationToken: cancellationToken).ConfigureAwait(false);
if (assistants.Count == 0) throw new Exception("Delete assistants failed.");
await UpdateNumbersStatusAsync(assistants.Where(x => x.AnsweringNumberId.HasValue).Select(x => x.AnsweringNumberId.Value).ToList(), false, cancellationToken).ConfigureAwait(false);
await DeleteAssistantRelatedInfoAsync(assistants.Select(x => x.AgentId).ToList(), cancellationToken).ConfigureAwait(false);
return new DeleteAiSpeechAssistantResponse
{
Data = _mapper.Map<List<AiSpeechAssistantDto>>(assistants)
};
}
public async Task<UpdateAiSpeechAssistantNumberResponse> UpdateAiSpeechAssistantNumberAsync(UpdateAiSpeechAssistantNumberCommand command, CancellationToken cancellationToken)
{
var assistant = await _aiSpeechAssistantDataProvider.GetAiSpeechAssistantAsync(command.AssistantId, cancellationToken).ConfigureAwait(false);
var number = await AssistantNumberSwitchAsync(assistant, cancellationToken).ConfigureAwait(false);
return new UpdateAiSpeechAssistantNumberResponse
{
Data = _mapper.Map<NumberPoolDto>(number)
};
}
public async Task<UpdateAiSpeechAssistantKnowledgeResponse> UpdateAiSpeechAssistantKnowledgeAsync(UpdateAiSpeechAssistantKnowledgeCommand command, CancellationToken cancellationToken)
{
var knowledgeId = command.KnowledgeId ?? (command.AssistantId.HasValue
? (await GetAiSpeechAssistantKnowledgeAsync(command.AssistantId.Value, cancellationToken).ConfigureAwait(false))?.Id : null);
Log.Information("Initial knowledge id if required, original:{OriginalKnowledgeId}, {knowledgeId}, {AssistantId}", command.KnowledgeId, knowledgeId, command.AssistantId);
if (knowledgeId == null)
throw new Exception("Could not find the knowledge id!");
var knowledge = await _aiSpeechAssistantDataProvider.GetAiSpeechAssistantKnowledgeAsync(knowledgeId: knowledgeId, cancellationToken: cancellationToken).ConfigureAwait(false);
if (knowledge == null) throw new Exception("Could not found the knowledge by knowledge id");
if(!string.IsNullOrWhiteSpace(command.Brief))
knowledge.Brief = command.Brief;
if (!string.IsNullOrWhiteSpace(command.Greetings))
knowledge.Greetings = command.Greetings;
if (command.VoiceType.HasValue)
await UpdateAssistantVoiceIfRequiredAsync(knowledge.AssistantId, command.ModelVoice, command.VoiceType.Value, command.MediaType, cancellationToken).ConfigureAwait(false);
await _aiSpeechAssistantDataProvider.UpdateAiSpeechAssistantKnowledgesAsync([knowledge], cancellationToken: cancellationToken).ConfigureAwait(false);
return new UpdateAiSpeechAssistantKnowledgeResponse
{
Data = _mapper.Map<AiSpeechAssistantKnowledgeDto>(knowledge),
};
}
public async Task<AddAiSpeechAssistantSessionResponse> AddAiSpeechAssistantSessionAsync(AddAiSpeechAssistantSessionCommand command, CancellationToken cancellationToken)
{
var sessionId = Guid.NewGuid();
var session = new AiSpeechAssistantSession
{
AssistantId = command.AssistantId,
SessionId = sessionId,
Count = 0
};
await _aiSpeechAssistantDataProvider.AddAiSpeechAssistantSessionAsync(session, cancellationToken: cancellationToken).ConfigureAwait(false);
return new AddAiSpeechAssistantSessionResponse { Data = sessionId };
}
public async Task<UpdateAiSpeechAssistantSessionResponse> UpdateAiSpeechAssistantSessionAsync(UpdateAiSpeechAssistantSessionCommand command, CancellationToken cancellationToken)
{
var session = await _aiSpeechAssistantDataProvider.GetAiSpeechAssistantSessionBySessionIdAsync(command.SessionId, cancellationToken).ConfigureAwait(false);
if (session == null) throw new Exception($"Could not found the session by session id: {command.SessionId}!");
await _redisSafeRunner.ExecuteWithLockAsync($"session-update-{command.SessionId}", async () =>
{
session.Count++;
await _aiSpeechAssistantDataProvider.UpdateAiSpeechAssistantSessionAsync(session, true, cancellationToken).ConfigureAwait(false);
}, wait: TimeSpan.FromSeconds(10), retry: TimeSpan.FromSeconds(1), server: RedisServer.System).ConfigureAwait(false);
return new UpdateAiSpeechAssistantSessionResponse
{
Data = _mapper.Map<AiSpeechAssistantSessionDto>(session)
};
}
private async Task UpdateAssistantVoiceIfRequiredAsync(int assistantId, string modelVoice, AiKidVoiceType? voiceType, AiSpeechAssistantMediaType? mediaType, CancellationToken cancellationToken)
{
var assistant = await _aiSpeechAssistantDataProvider.GetAiSpeechAssistantAsync(assistantId, cancellationToken).ConfigureAwait(false);
Log.Information("Get assistant for update model voice: {@Assistant}", assistant);
if (assistant != null)
{
assistant.ModelVoice = ModelVoiceMapping(modelVoice, voiceType, mediaType);
await _aiSpeechAssistantDataProvider.UpdateAiSpeechAssistantsAsync([assistant], cancellationToken: cancellationToken).ConfigureAwait(false);
}
}
private async Task<AiSpeechAssistantKnowledge> GetAiSpeechAssistantKnowledgeAsync(int assistantId, CancellationToken cancellationToken)
{
var knowledge = await _aiSpeechAssistantDataProvider
.GetAiSpeechAssistantKnowledgeAsync(assistantId: assistantId, cancellationToken: cancellationToken).ConfigureAwait(false);
Log.Information("Get knowledge by assistant when update the knowledge: {@Knowledge}", knowledge);
return knowledge;
}
private async Task<Domain.AISpeechAssistant.AiSpeechAssistant> InitialAiSpeechAssistantAsync(AddAiSpeechAssistantCommand command, CancellationToken cancellationToken)
{
var (agent, number) = await InitialAssistantRelatedInfoAsync(command , cancellationToken).ConfigureAwait(false);
var assistant = new Domain.AISpeechAssistant.AiSpeechAssistant
{
AgentId = agent.Id,
ModelVoice = ModelVoiceMapping(command.ModelVoice, command.VoiceType, command.MediaType),
Name = command.AssistantName,
AnsweringNumberId = number?.Id,
AnsweringNumber = number?.Number,
CreatedBy = _currentUser.Id.Value,
ModelUrl = GetDefaultModuleUrl(command.ModelUrl, command.ModelProvider, command.AgentType),
ModelProvider = command.ModelProvider,
ModelName = command.ModelName,
ModelLanguage = command.ModelLanguage,
Channel = command.Channels == null ? null : string.Join(",", command.Channels.Select(x => (int)x)),
IsDisplay = command.IsDisplay
};
await _aiSpeechAssistantDataProvider.AddAiSpeechAssistantsAsync([assistant], cancellationToken: cancellationToken).ConfigureAwait(false);
await InitialAssistantKnowledgeAsync(command, assistant, cancellationToken).ConfigureAwait(false);
return assistant;
}
private string GetDefaultModuleUrl(string modelUrl, AiSpeechAssistantProvider provider, AgentType type)
{
if (!string.IsNullOrEmpty(modelUrl)) return modelUrl;
return type switch
{
AgentType.AiKid => provider == AiSpeechAssistantProvider.OpenAi ? AiSpeechAssistantStore.AiKidDefaultUrl : AiSpeechAssistantStore.GoogleDefaultUrl,
_ => provider == AiSpeechAssistantProvider.OpenAi ? AiSpeechAssistantStore.DefaultUrl : AiSpeechAssistantStore.GoogleDefaultUrl
};
}
private string ModelVoiceMapping(string modelVoice, AiKidVoiceType? voiceType, AiSpeechAssistantMediaType? mediaType)
{
if (!string.IsNullOrEmpty(modelVoice)) return modelVoice;
if (!voiceType.HasValue) return "alloy";
return voiceType.Value switch
{
AiKidVoiceType.Male => mediaType.HasValue ? (mediaType == AiSpeechAssistantMediaType.Audio ? "ash" : "Puck") : "ash",
_ => mediaType.HasValue ? (mediaType == AiSpeechAssistantMediaType.Audio ? "alloy" : "Aoede") : "alloy"
};
}
private async Task<(Agent agent, NumberPool number)> InitialAssistantRelatedInfoAsync(AddAiSpeechAssistantCommand command, CancellationToken cancellationToken)
{
Restaurant domain = null;
if (command.AgentType != AgentType.AiKid)
{
domain = new Restaurant { Name = command.AssistantName };
await _restaurantDataProvider.AddRestaurantAsync(domain, cancellationToken: cancellationToken).ConfigureAwait(false);
}
var agent = new Agent
{
RelateId = domain?.Id ?? 0,
Type = command.AgentType,
SourceSystem = command.SourceSystem
};
await _agentDataProvider.AddAgentAsync(agent, cancellationToken: cancellationToken).ConfigureAwait(false);
if (command.AgentType == AgentType.AiKid && command.Uuid.HasValue)
await _aiSpeechAssistantDataProvider.AddAiKidAsync(new AiKid
{
AgentId = agent.Id,
KidUuid = command.Uuid.Value
}, cancellationToken: cancellationToken).ConfigureAwait(false);
if (!command.IsDisplay) return (agent, null);
var number = await _aiSpeechAssistantDataProvider.GetNumberAsync(isUsed: false, cancellationToken: cancellationToken).ConfigureAwait(false);
if (number != null)
{
number.IsUsed = true;
await _aiSpeechAssistantDataProvider.UpdateNumberPoolAsync([number], cancellationToken: cancellationToken).ConfigureAwait(false);
}
return (agent, number);
}
private async Task InitialAssistantKnowledgeAsync(AddAiSpeechAssistantCommand command, Domain.AISpeechAssistant.AiSpeechAssistant assistant, CancellationToken cancellationToken)
{
var knowledge = new AiSpeechAssistantKnowledge
{
Version = "1.0",
IsActive = true,
Json = command.Json,
AssistantId = assistant.Id,
Greetings = command.Greetings,
CreatedBy = _currentUser.Id.Value,
Prompt = GenerateKnowledgePrompt(command.Json)
};
await _aiSpeechAssistantDataProvider.AddAiSpeechAssistantKnowledgesAsync([knowledge], cancellationToken: cancellationToken).ConfigureAwait(false);
}
private async Task<AiSpeechAssistantKnowledge> UpdatePreviousKnowledgeIfRequiredAsync(int assistantId, bool isActive, CancellationToken cancellationToken)
{
var knowledge = await _aiSpeechAssistantDataProvider.GetAiSpeechAssistantKnowledgeAsync(assistantId, isActive: true, cancellationToken: cancellationToken).ConfigureAwait(false);
if (knowledge == null)
{
Log.Information("Could not found the previous knowledge.");
return null;
}
await UpdateKnowledgeStatusAsync(knowledge, isActive, cancellationToken).ConfigureAwait(false);
return knowledge;
}
private async Task UpdateKnowledgeStatusAsync(AiSpeechAssistantKnowledge knowledge, bool isActive, CancellationToken cancellationToken)
{
knowledge.IsActive = isActive;
await _aiSpeechAssistantDataProvider.UpdateAiSpeechAssistantKnowledgesAsync([knowledge], cancellationToken: cancellationToken).ConfigureAwait(false);
}
private async Task InitialKnowledgeAsync(AiSpeechAssistantKnowledge latestKnowledge, CancellationToken cancellationToken)
{
latestKnowledge.IsActive = true;
latestKnowledge.CreatedBy = _currentUser.Id.Value;
latestKnowledge.Prompt = GenerateKnowledgePrompt(latestKnowledge.Json);
latestKnowledge.Version = await HandleKnowledgeVersionAsync(latestKnowledge, cancellationToken).ConfigureAwait(false);
}
private string GenerateKnowledgePrompt(string json)
{
var prompt = new StringBuilder();
var jsonData = JObject.Parse(json);
var textInfo = CultureInfo.InvariantCulture.TextInfo;
foreach (var property in jsonData.Properties())
{
var key = textInfo.ToTitleCase(property.Name);
var value = property.Value;
if (value is JArray array)
{
var list = array.Select((item, index) => $"{index + 1}. {item.ToString()}").ToList();
prompt.AppendLine($"{key}:\n{string.Join("\n", list)}\n");
}
else
prompt.AppendLine($"{key}: {value}\n");
}
return prompt.ToString();
}
private async Task<string> HandleKnowledgeVersionAsync(AiSpeechAssistantKnowledge latestKnowledge, CancellationToken cancellationToken)
{
var knowledge = await _aiSpeechAssistantDataProvider.GetAiSpeechAssistantKnowledgeOrderByVersionAsync(latestKnowledge.AssistantId, cancellationToken).ConfigureAwait(false);
if (knowledge == null) return "1.0";
var versionParts = knowledge.Version.Split('.');
if (versionParts.Length != 2 || !int.TryParse(versionParts[0], out var majorVersion) || !int.TryParse(versionParts[1], out var minorVersion))
{
return "1.0";
}
if (minorVersion == 9)
{
majorVersion++;
minorVersion = 0;
}
else
minorVersion++;
return $"{majorVersion}.{minorVersion}";
}
private async Task UpdateNumbersStatusAsync(List<int> answeringNumberIds, bool isUsed, CancellationToken cancellationToken)
{
if (answeringNumberIds.Count == 0) return;
var numbers = await _aiSpeechAssistantDataProvider.GetNumbersAsync(answeringNumberIds, cancellationToken).ConfigureAwait(false);
numbers.ForEach(x => x.IsUsed = isUsed);
await _aiSpeechAssistantDataProvider.UpdateNumberPoolAsync(numbers, cancellationToken: cancellationToken).ConfigureAwait(false);
}
private async Task UpdateAssistantNumberIfRequiredAsync(int? preNumberId, int? currentNumberId, CancellationToken cancellationToken)
{
if (preNumberId == currentNumberId) return;
var numberIds = new List<int>();
if (preNumberId.HasValue) numberIds.Add(preNumberId.Value);
if (currentNumberId.HasValue) numberIds.Add(currentNumberId.Value);
if (numberIds.Count == 0) return;
var numbers = await _aiSpeechAssistantDataProvider.GetNumbersAsync(numberIds, cancellationToken).ConfigureAwait(false);
if (numbers.Count == 0) return;
foreach (var number in numbers)
{
if (number.Id == preNumberId) number.IsUsed = false;
if (number.Id == currentNumberId) number.IsUsed = true;
}
await _aiSpeechAssistantDataProvider.UpdateNumberPoolAsync(numbers, cancellationToken: cancellationToken).ConfigureAwait(false);
}
private async Task<NumberPool> AssistantNumberSwitchAsync(Domain.AISpeechAssistant.AiSpeechAssistant assistant, CancellationToken cancellationToken)
{
var number = assistant.AnsweringNumberId.HasValue
? await _aiSpeechAssistantDataProvider.GetNumberAsync(assistant.AnsweringNumberId.Value, cancellationToken: cancellationToken).ConfigureAwait(false)
: await _aiSpeechAssistantDataProvider.GetNumberAsync(isUsed: false, cancellationToken: cancellationToken).ConfigureAwait(false);
if (assistant.AnsweringNumberId.HasValue)
{
if (number == null) throw new Exception("Could not found the number");
number.IsUsed = false;
await _aiSpeechAssistantDataProvider.UpdateNumberPoolAsync([number], cancellationToken: cancellationToken).ConfigureAwait(false);
assistant.AnsweringNumberId = null;
assistant.AnsweringNumber = null;
}
else
{
if (number == null) throw new Exception("No available numbers.");
number.IsUsed = true;
await _aiSpeechAssistantDataProvider.UpdateNumberPoolAsync([number], cancellationToken: cancellationToken).ConfigureAwait(false);
assistant.AnsweringNumberId = number.Id;
assistant.AnsweringNumber = number.Number;
}
await _aiSpeechAssistantDataProvider.UpdateAiSpeechAssistantsAsync([assistant], cancellationToken: cancellationToken).ConfigureAwait(false);
return number;
}
private async Task DeleteAssistantRelatedInfoAsync(List<int> agentIds, CancellationToken cancellationToken)
{
var agents = await _agentDataProvider.GetAgentsAsync(agentIds: agentIds, cancellationToken: cancellationToken).ConfigureAwait(false);
if (agents.Count == 0) return;
var domains = await _restaurantDataProvider.GetRestaurantsAsync(agents.Select(x => x.RelateId).ToList(), cancellationToken).ConfigureAwait(false);
await _agentDataProvider.DeleteAgentsAsync(agents, cancellationToken: cancellationToken).ConfigureAwait(false);
if (domains.Count == 0) return;
await _restaurantDataProvider.DeleteRestaurantsAsync(domains, cancellationToken: cancellationToken).ConfigureAwait(false);
}
}