|
| 1 | +package org.foedusprogramme.alexandrite.agent.routing |
| 2 | + |
| 3 | +import org.foedusprogramme.alexandrite.agent.config.AgentDirectory |
| 4 | +import org.foedusprogramme.alexandrite.sdk.chat.AgentChatKey |
| 5 | +import org.foedusprogramme.alexandrite.sdk.chat.AgentId |
| 6 | +import org.foedusprogramme.alexandrite.sdk.chat.ChannelInstanceId |
| 7 | +import org.foedusprogramme.alexandrite.sdk.chat.ChatAddress |
| 8 | +import org.foedusprogramme.alexandrite.sdk.di.Singleton |
| 9 | + |
| 10 | +/** Chats of one agent that share the conversation kept at [anchor]. */ |
| 11 | +internal data class LinkGroup(val anchor: ChatAddress, val members: List<ChatAddress>) { |
| 12 | + val chats: List<ChatAddress> get() = listOf(anchor) + members |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * The home chats and linked groups of the agents, read without suspending. |
| 17 | + * |
| 18 | + * A change that breaks a rule throws [IllegalArgumentException] with a message for the user and changes nothing. |
| 19 | + */ |
| 20 | +@Singleton |
| 21 | +internal class ChatTopology(private val directory: AgentDirectory) { |
| 22 | + private val lock = Any() |
| 23 | + |
| 24 | + @Volatile |
| 25 | + private var current = Topology( |
| 26 | + directory.agents.associate { agent -> |
| 27 | + agent.id to |
| 28 | + agent.attachments.mapNotNull { attachment -> attachment.home?.let { attachment.instance to it } } |
| 29 | + .toMap() |
| 30 | + }, |
| 31 | + directory.agents.associate { agent -> |
| 32 | + agent.id to agent.linkedChats.map { LinkGroup(it.first(), it.drop(1)) } |
| 33 | + }, |
| 34 | + ) |
| 35 | + |
| 36 | + /** The agent whose home [chat] is, else the one whose home its parent is, null when it is no home chat. */ |
| 37 | + fun homeAgent(chat: ChatAddress): AgentId? = current.let { it.homeAgents[chat] ?: it.homeAgents[chat.parent] } |
| 38 | + |
| 39 | + fun home(agent: AgentId, instance: ChannelInstanceId): ChatAddress? = current.homes[agent]?.get(instance) |
| 40 | + |
| 41 | + fun groups(agent: AgentId): List<LinkGroup> = current.groups[agent].orEmpty() |
| 42 | + |
| 43 | + /** The group of [agent] that holds [chat], null when none does. */ |
| 44 | + fun group(agent: AgentId, chat: ChatAddress): LinkGroup? = current.groupOf[agent]?.get(chat) |
| 45 | + |
| 46 | + /** Where [agent] keeps its conversation with [chat]. */ |
| 47 | + fun key(agent: AgentId, chat: ChatAddress): AgentChatKey = AgentChatKey(agent, group(agent, chat)?.anchor ?: chat) |
| 48 | + |
| 49 | + /** Makes [chat] the home of [agent] on [instance], or leaves the instance without one when [chat] is null. */ |
| 50 | + fun setHome(agent: AgentId, instance: ChannelInstanceId, chat: ChatAddress?) { |
| 51 | + synchronized(lock) { |
| 52 | + val topology = current |
| 53 | + requireAttached(agent, instance) |
| 54 | + if (chat != null) { |
| 55 | + require(chat.instance == instance) { "$chat is no chat of $instance." } |
| 56 | + val other = topology.homeAgents[chat] |
| 57 | + require(other == null || other == agent) { "$chat is the home chat of agent '$other'." } |
| 58 | + } |
| 59 | + val homes = topology.homes[agent].orEmpty().toMutableMap() |
| 60 | + if (chat == null) homes.remove(instance) else homes[instance] = chat |
| 61 | + current = Topology(topology.homes + (agent to homes), topology.groups) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /** Links [member] into the group of [chat], which [chat] anchors when it is in none, and returns that group. */ |
| 66 | + fun link(agent: AgentId, chat: ChatAddress, member: ChatAddress): LinkGroup = synchronized(lock) { |
| 67 | + val topology = current |
| 68 | + requireAttached(agent, chat.instance) |
| 69 | + requireAttached(agent, member.instance) |
| 70 | + require(member != chat) { "A chat cannot be linked to itself." } |
| 71 | + val linked = topology.groupOf[agent]?.get(member) |
| 72 | + require(linked == null) { "$member is linked to ${linked?.anchor} already: unlink it first." } |
| 73 | + val group = topology.groupOf[agent]?.get(chat) |
| 74 | + val grown = group?.copy(members = group.members + member) ?: LinkGroup(chat, listOf(member)) |
| 75 | + val groups = topology.groups[agent].orEmpty() |
| 76 | + val next = if (group == null) groups + grown else groups.map { if (it == group) grown else it } |
| 77 | + current = Topology(topology.homes, topology.groups + (agent to next)) |
| 78 | + grown |
| 79 | + } |
| 80 | + |
| 81 | + /** Takes [member] out of its group of [agent], and returns whether it was in one. */ |
| 82 | + fun unlink(agent: AgentId, member: ChatAddress): Boolean = synchronized(lock) { |
| 83 | + val topology = current |
| 84 | + val group = topology.groupOf[agent]?.get(member) ?: return false |
| 85 | + require(group.anchor != member) { "$member anchors its group: unlink the other chats first." } |
| 86 | + val shrunk = group.copy(members = group.members - member) |
| 87 | + val next = topology.groups.getValue(agent).mapNotNull { |
| 88 | + if (it != group) it else shrunk.takeIf { shrunk.members.isNotEmpty() } |
| 89 | + } |
| 90 | + current = Topology(topology.homes, topology.groups + (agent to next)) |
| 91 | + true |
| 92 | + } |
| 93 | + |
| 94 | + private fun requireAttached(agent: AgentId, instance: ChannelInstanceId) { |
| 95 | + require(directory.agent(agent)?.attachments.orEmpty().any { it.instance == instance }) { |
| 96 | + "Agent '$agent' does not serve $instance." |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +private class Topology( |
| 102 | + val homes: Map<AgentId, Map<ChannelInstanceId, ChatAddress>>, |
| 103 | + val groups: Map<AgentId, List<LinkGroup>>, |
| 104 | +) { |
| 105 | + val homeAgents: Map<ChatAddress, AgentId> = |
| 106 | + homes.flatMap { (agent, byInstance) -> byInstance.values.map { it to agent } }.toMap() |
| 107 | + |
| 108 | + val groupOf: Map<AgentId, Map<ChatAddress, LinkGroup>> = |
| 109 | + groups.mapValues { (_, groups) -> groups.flatMap { group -> group.chats.map { it to group } }.toMap() } |
| 110 | +} |
0 commit comments