@musnows/scriverse 1.1.0 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/ai.js CHANGED
@@ -2438,6 +2438,7 @@ export class AiManager {
2438
2438
  retryPolicy;
2439
2439
  retrySleep;
2440
2440
  liteLlmPriceCache;
2441
+ conversationTitleGenerations = new Map();
2441
2442
  taskControllers = new Map();
2442
2443
  autoRunStarting = new Map();
2443
2444
  autoRunTimers = new Map();
@@ -5141,13 +5142,13 @@ export class AiManager {
5141
5142
  const titleSettings = this.store.getWorkAiSettings(input.workId);
5142
5143
  const titleModelId = typeof titleSettings.titleGenerationModelId === "string" ? titleSettings.titleGenerationModelId : "";
5143
5144
  const defaultTitle = firstUserContent ? defaultAiConversationTitle(firstUserContent) : "";
5144
- const isCompletingSecondAssistantTurn = conversationBefore?.messages.at(-1)?.role === "user"
5145
- && userMessages.length === 2
5146
- && assistantMessages.length === 1;
5145
+ const isCompletingFirstAssistantTurn = conversationBefore?.messages.at(-1)?.role === "user"
5146
+ && userMessages.length === 1
5147
+ && assistantMessages.length === 0;
5147
5148
  const shouldGenerateTitle = Boolean(input.conversationId
5148
5149
  && firstUserContent
5149
5150
  && titleModelId
5150
- && isCompletingSecondAssistantTurn
5151
+ && isCompletingFirstAssistantTurn
5151
5152
  && (conversationBefore?.title === "新对话" || conversationBefore?.title === defaultTitle));
5152
5153
  const processStartedAt = process.hrtime.bigint();
5153
5154
  let persistedConversationMessage = null;
@@ -5258,14 +5259,26 @@ export class AiManager {
5258
5259
  });
5259
5260
  }
5260
5261
  }
5262
+ let conversationTitleGenerationStarted = false;
5261
5263
  if (shouldGenerateTitle && conversationMessage && input.conversationId) {
5262
- void this.generateConversationTitle(input.workId, input.conversationId, titleModelId, [
5264
+ conversationTitleGenerationStarted = true;
5265
+ const generation = this.generateConversationTitle(input.workId, input.conversationId, titleModelId, [
5263
5266
  ...(conversationBefore?.messages ?? []),
5264
5267
  { role: "assistant", content: generated.content }
5265
5268
  ], defaultTitle).catch((error) => {
5266
5269
  logger.warn("ai.conversation_title.failed", { workId: input.workId, conversationId: input.conversationId, error: aiErrorForLog(error) });
5270
+ return null;
5271
+ });
5272
+ this.conversationTitleGenerations.set(input.conversationId, generation);
5273
+ void generation.then(() => {
5274
+ if (this.conversationTitleGenerations.get(input.conversationId) === generation) {
5275
+ this.conversationTitleGenerations.delete(input.conversationId);
5276
+ }
5267
5277
  });
5268
5278
  }
5279
+ const updatedConversation = input.conversationId
5280
+ ? this.store.getAiConversationSummary(input.conversationId)
5281
+ : null;
5269
5282
  return {
5270
5283
  callId: generated.callId,
5271
5284
  content: generated.content,
@@ -5277,10 +5290,15 @@ export class AiManager {
5277
5290
  toolCalls: generated.toolCalls,
5278
5291
  processSteps: generated.processSteps,
5279
5292
  contextUsage: generated.contextUsage,
5293
+ conversationTitle: updatedConversation?.title ?? "新对话",
5294
+ conversationTitleGenerationStarted,
5280
5295
  roleplayMemoriesCommitted: committedRoleplayMemories,
5281
5296
  ...(conversationMessage ? { conversationMessage } : {})
5282
5297
  };
5283
5298
  }
5299
+ async waitForConversationTitle(conversationId) {
5300
+ await this.conversationTitleGenerations.get(conversationId);
5301
+ }
5284
5302
  async resumeUserQuestion(input, stream = {}) {
5285
5303
  const answeredItems = (input.answers ?? []).map((answer) => ({
5286
5304
  question: String(answer.question ?? ""),