71 lines
1.9 KiB
GDScript
71 lines
1.9 KiB
GDScript
class_name ConversationIntentCatalog
|
|
extends Resource
|
|
|
|
@export var intents: Array[ConversationIntentDefinition] = []
|
|
|
|
var _intents_by_id: Dictionary = {}
|
|
var _errors: Array[String] = []
|
|
|
|
|
|
func rebuild() -> Array[String]:
|
|
_intents_by_id.clear()
|
|
_errors.clear()
|
|
for definition in intents:
|
|
if definition == null:
|
|
_errors.append("Intent catalog contains a null definition")
|
|
continue
|
|
for error in definition.validate():
|
|
_errors.append("Intent '%s': %s" % [definition.intent_id, error])
|
|
if _intents_by_id.has(definition.intent_id):
|
|
_errors.append("Duplicate intent '%s'" % definition.intent_id)
|
|
else:
|
|
_intents_by_id[definition.intent_id] = definition
|
|
for definition in intents:
|
|
if definition == null:
|
|
continue
|
|
for response_intent_id in definition.response_intent_ids:
|
|
if not _intents_by_id.has(response_intent_id):
|
|
_errors.append(
|
|
(
|
|
"Intent '%s' references unknown response '%s'"
|
|
% [definition.intent_id, response_intent_id]
|
|
)
|
|
)
|
|
_errors.sort()
|
|
return get_errors()
|
|
|
|
|
|
func is_valid() -> bool:
|
|
return rebuild().is_empty()
|
|
|
|
|
|
func get_errors() -> Array[String]:
|
|
return _errors.duplicate()
|
|
|
|
|
|
func has_intent(intent_id: StringName) -> bool:
|
|
_ensure_built()
|
|
return _intents_by_id.has(intent_id)
|
|
|
|
|
|
func get_response_intent_ids(intent_id: StringName) -> Array[StringName]:
|
|
_ensure_built()
|
|
var definition := _intents_by_id.get(intent_id) as ConversationIntentDefinition
|
|
return definition.response_intent_ids.duplicate() if definition != null else []
|
|
|
|
|
|
func get_intent_ids() -> Array[StringName]:
|
|
_ensure_built()
|
|
var result: Array[StringName] = []
|
|
for intent_id in _intents_by_id:
|
|
result.append(intent_id)
|
|
result.sort_custom(
|
|
func(left: StringName, right: StringName) -> bool: return String(left) < String(right)
|
|
)
|
|
return result
|
|
|
|
|
|
func _ensure_built() -> void:
|
|
if _intents_by_id.is_empty() and not intents.is_empty():
|
|
rebuild()
|