| 75 | } |
| 76 | |
| 77 | function createAgent() { |
| 78 | const agents = createMemo(() => sync.data.agent.filter((agent) => agent.mode !== "subagent" && !agent.hidden)) |
| 79 | const visibleAgents = createMemo(() => sync.data.agent.filter((agent) => !agent.hidden)) |
| 80 | const [agentStore, setAgentStore] = createStore({ |
| 81 | current: undefined as string | undefined, |
| 82 | }) |
| 83 | const colors = createMemo(() => [ |
| 84 | theme.secondary, |
| 85 | theme.accent, |
| 86 | theme.success, |
| 87 | theme.warning, |
| 88 | theme.primary, |
| 89 | theme.error, |
| 90 | theme.info, |
| 91 | ]) |
| 92 | return { |
| 93 | list() { |
| 94 | return agents() |
| 95 | }, |
| 96 | current() { |
| 97 | return agents().find((x) => x.name === agentStore.current) ?? agents().at(0) |
| 98 | }, |
| 99 | set(name: string) { |
| 100 | if (!agents().some((x) => x.name === name)) |
| 101 | return toast.show({ |
| 102 | variant: "warning", |
| 103 | message: `Agent not found: ${name}`, |
| 104 | duration: 3000, |
| 105 | }) |
| 106 | setAgentStore("current", name) |
| 107 | }, |
| 108 | move(direction: 1 | -1) { |
| 109 | batch(() => { |
| 110 | const current = this.current() |
| 111 | if (!current) return |
| 112 | let next = agents().findIndex((x) => x.name === current.name) + direction |
| 113 | if (next < 0) next = agents().length - 1 |
| 114 | if (next >= agents().length) next = 0 |
| 115 | const value = agents()[next] |
| 116 | setAgentStore("current", value.name) |
| 117 | }) |
| 118 | }, |
| 119 | color(name: string) { |
| 120 | const index = visibleAgents().findIndex((x) => x.name === name) |
| 121 | if (index === -1) return colors()[0] |
| 122 | const agent = visibleAgents()[index] |
| 123 | |
| 124 | if (agent?.color) { |
| 125 | const color = agent.color |
| 126 | if (color.startsWith("#")) return RGBA.fromHex(color) |
| 127 | // already validated by config, just satisfying TS here |
| 128 | return theme[color as keyof typeof theme] as RGBA |
| 129 | } |
| 130 | return colors()[index % colors().length] |
| 131 | }, |
| 132 | } |
| 133 | } |
| 134 | |