/**
 * Resolve bound character display name from workflow variable and/or project meta.
 */
export function resolveBoundCharacterName({ boundCharacterVar, characterId, characters = [] }) {
  if (boundCharacterVar) {
    const nameMatch = String(boundCharacterVar).match(/名称[：:]\s*(.+?)(?:\n|$)/);
    if (nameMatch?.[1]?.trim()) {
      return nameMatch[1].trim();
    }
  }

  if (characterId && characters.length > 0) {
    const byId = characters.find((c) => c.id === characterId);
    if (byId?.name) return byId.name;
    const byName = characters.find((c) => c.name === characterId);
    if (byName?.name) return byName.name;
  }

  return null;
}

/** Index of the last user message in stepMessages, or -1. */
export function findLastUserMessageIndex(stepMessages = []) {
  for (let i = stepMessages.length - 1; i >= 0; i -= 1) {
    if (stepMessages[i]?.role === 'user') return i;
  }
  return -1;
}
