pisha-server 0.0.0-stage → 0.0.11
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/LICENSE +21 -0
- package/README.md +40 -2
- package/accounts/accounts.js +754 -0
- package/accounts/index.js +1 -0
- package/accounts/passwords.js +41 -0
- package/accounts/totp.js +54 -0
- package/boards/boards.js +1079 -0
- package/boards/github.js +212 -0
- package/boards/index.js +1 -0
- package/boards/sync.js +980 -0
- package/companies.js +294 -0
- package/company-export.js +101 -0
- package/company.js +79 -0
- package/dashboard/dashboard.js +407 -0
- package/dashboard/index.js +1 -0
- package/data-upgrade.js +69 -0
- package/install/linux.sh +181 -0
- package/install/macos.sh +121 -0
- package/install/windows.bat +54 -0
- package/machines/index.js +3 -0
- package/machines/machines.js +910 -0
- package/messaging/inbox.js +252 -0
- package/messaging/index.js +3 -0
- package/messaging/messaging.js +551 -0
- package/messaging/plans.js +67 -0
- package/observation/index.js +1 -0
- package/observation/observation.js +224 -0
- package/organization/index.js +1 -0
- package/organization/organization.js +218 -0
- package/package.json +54 -4
- package/permissions/index.js +1 -0
- package/permissions/permissions.js +211 -0
- package/pisha-host.mjs +3297 -0
- package/pisha-host.mjs.sig +1 -0
- package/platform/acme.js +207 -0
- package/platform/basics.js +14 -0
- package/platform/config.js +154 -0
- package/platform/control.js +93 -0
- package/platform/credentials.js +151 -0
- package/platform/database.js +251 -0
- package/platform/errors.js +13 -0
- package/platform/files.js +50 -0
- package/platform/http.js +207 -0
- package/platform/index.js +58 -0
- package/platform/limits.js +122 -0
- package/platform/listen.js +183 -0
- package/platform/roles.js +6 -0
- package/platform/stream.js +19 -0
- package/platform/text.js +83 -0
- package/platform/ws.js +186 -0
- package/routes.js +405 -0
- package/server.js +65 -0
- package/sessions/index.js +1 -0
- package/sessions/sessions.js +533 -0
- package/ui/LICENSE-affectui.txt +26 -0
- package/ui/addmachine.js +151 -0
- package/ui/api.js +20 -0
- package/ui/app.js +143 -0
- package/ui/attention.js +78 -0
- package/ui/back.js +166 -0
- package/ui/boards.js +958 -0
- package/ui/chat.js +388 -0
- package/ui/content.js +157 -0
- package/ui/dialogs.js +135 -0
- package/ui/doing.js +112 -0
- package/ui/dom.js +6 -0
- package/ui/ds-base.css +146 -0
- package/ui/ds-components.css +229 -0
- package/ui/fonts/LICENSE-space-grotesk.txt +93 -0
- package/ui/fonts/space-grotesk-latin-700-normal.woff2 +0 -0
- package/ui/fonts/space-grotesk-latin-ext-700-normal.woff2 +0 -0
- package/ui/format.js +25 -0
- package/ui/i18n.js +2093 -0
- package/ui/icons/apple-touch-icon.png +0 -0
- package/ui/icons/icon-192.png +0 -0
- package/ui/icons/icon-512.png +0 -0
- package/ui/icons/maskable-512.png +0 -0
- package/ui/icons.js +40 -0
- package/ui/inbox.js +535 -0
- package/ui/index.html +103 -0
- package/ui/machines.js +261 -0
- package/ui/manifest.webmanifest +16 -0
- package/ui/mosaic.js +189 -0
- package/ui/nav.js +141 -0
- package/ui/newteam.js +223 -0
- package/ui/org.js +653 -0
- package/ui/overlays.css +126 -0
- package/ui/palette.js +179 -0
- package/ui/people.js +370 -0
- package/ui/permissions.js +202 -0
- package/ui/qr.js +158 -0
- package/ui/router.js +171 -0
- package/ui/routes.js +46 -0
- package/ui/screen-boards.css +241 -0
- package/ui/screen-inbox.css +201 -0
- package/ui/screen-machines.css +76 -0
- package/ui/screen-mosaic.css +26 -0
- package/ui/screen-newteam.css +90 -0
- package/ui/screen-org.css +113 -0
- package/ui/screen-session.css +99 -0
- package/ui/screen-signin.css +28 -0
- package/ui/screen-team.css +80 -0
- package/ui/screen-teams.css +41 -0
- package/ui/session.js +42 -0
- package/ui/shell.css +122 -0
- package/ui/signin.js +120 -0
- package/ui/squads.js +370 -0
- package/ui/state.js +59 -0
- package/ui/stream.js +268 -0
- package/ui/styles.css +343 -0
- package/ui/team.js +115 -0
- package/ui/teams.js +58 -0
- package/ui/terminal.js +261 -0
- package/ui/theme.js +32 -0
- package/ui/timeline.js +163 -0
- package/ui/topbar.js +205 -0
- package/ui/voice-parse.js +98 -0
- package/ui/voice.js +320 -0
- package/vendor/InterVariable.woff2 +0 -0
- package/vendor/LICENSE-inter +92 -0
- package/vendor/LICENSE-xterm +21 -0
- package/vendor/README.md +8 -0
- package/vendor/addon-fit.js +2 -0
- package/vendor/xterm.css +218 -0
- package/vendor/xterm.js +2 -0
package/ui/org.js
ADDED
|
@@ -0,0 +1,653 @@
|
|
|
1
|
+
import { store, $ } from './dom.js'
|
|
2
|
+
import { T } from './i18n.js'
|
|
3
|
+
import { state, sessions, hosts, linksOf, linkListeners, companyPref, can } from './state.js'
|
|
4
|
+
import { now, esc } from './format.js'
|
|
5
|
+
import { sortedSessions, byPresence, status, agentMark } from './content.js'
|
|
6
|
+
import { setHtml, soon } from './nav.js'
|
|
7
|
+
import { currentRoute } from './router.js'
|
|
8
|
+
import { roleTag, renderTop, personaTag } from './topbar.js'
|
|
9
|
+
import { alertBox } from './mosaic.js'
|
|
10
|
+
import { closeMenu, confirmDialog, post, runAction, dialog } from './dialogs.js'
|
|
11
|
+
import { terminalOf, postJson, hostAction } from './machines.js'
|
|
12
|
+
import {
|
|
13
|
+
personaName, newSessionDialog, addWorkerTo, saveTemplateDialog, reopenable, reopen, personaOptions,
|
|
14
|
+
} from './squads.js'
|
|
15
|
+
import { icon } from './icons.js'
|
|
16
|
+
|
|
17
|
+
// org chart: each squad as a card (its coordinator and workers), the links
|
|
18
|
+
// between squads as lines, and what each one is for on hover. From here
|
|
19
|
+
// squads and agents are added, described, moved, linked, closed and
|
|
20
|
+
// deleted, through the same actions as everywhere else in the dashboard.
|
|
21
|
+
|
|
22
|
+
const ORG_GAP_X = 72, ORG_GAP_Y = 48, ORG_PAD = 32, ORG_FOLD = 6
|
|
23
|
+
// A team: a tray around its coordinator's card, and its workers' cards in
|
|
24
|
+
// rows below it (three a row), the first row joined to it by lines.
|
|
25
|
+
const CARD_W = 232, LEAD_W = 300, CARD_GAP = 14, PER_ROW = 3, CREW_PAD = 14, TRAY = 6, HEAD_H = 56
|
|
26
|
+
// (Each team's size as last drawn: where the next one goes, and the lines between teams.)
|
|
27
|
+
const orgSize = new Map()
|
|
28
|
+
export let orgZoom = (() => { const z = Number(store.get(companyPref('pishaOrgZoom'))); return z >= 0.4 && z <= 1.6 ? z : 1 })()
|
|
29
|
+
let orgDrag = null // a card or a new link being dragged
|
|
30
|
+
let orgPos = new Map() // coordinator -> {x, y, h}, as drawn
|
|
31
|
+
const orgOpen = new Set() // squads showing all their workers
|
|
32
|
+
const orgSaved = new Set() // squads placed automatically, already saved
|
|
33
|
+
let orgTipKey = null
|
|
34
|
+
|
|
35
|
+
const squadTitle = name => state.squadMeta.get(name)?.title || name
|
|
36
|
+
|
|
37
|
+
// What a session is for: what was written for it here, else what its persona says.
|
|
38
|
+
const purposeOf = s => s?.description || s?.persona?.description || ''
|
|
39
|
+
const personaOf = s => s?.persona?.name || personaName(s?.opened_persona) || ''
|
|
40
|
+
const recentOf = l => (l.recent ?? []).filter(t => now() - t < 3600_000).length
|
|
41
|
+
const orgNarrow = () => matchMedia('(max-width: 900px)').matches
|
|
42
|
+
|
|
43
|
+
// Squads as in the sidebar, plus the sessions opened here that haven't
|
|
44
|
+
// joined yet (Claude Code still starting, or waiting for its login): by the
|
|
45
|
+
// role and coordinator they were opened with.
|
|
46
|
+
const orgRole = s => s.role ?? s.opened_role ?? null
|
|
47
|
+
const orgCoordOf = s => s.coordinator ?? terminalOf(s.name)?.term.coordinator ?? null
|
|
48
|
+
export function orgSquads() {
|
|
49
|
+
const list = sortedSessions()
|
|
50
|
+
const coords = list.filter(s => orgRole(s) === 'coordinator')
|
|
51
|
+
const names = new Set(coords.map(c => c.name))
|
|
52
|
+
return {
|
|
53
|
+
squads: coords.map(c => ({ coord: c, workers: list.filter(s => orgRole(s) === 'worker' && orgCoordOf(s) === c.name).sort(byPresence) })),
|
|
54
|
+
orphans: list.filter(s => orgRole(s) !== 'coordinator' && orgRole(s) !== 'solo' && !(orgRole(s) === 'worker' && names.has(orgCoordOf(s)))),
|
|
55
|
+
solos: list.filter(s => orgRole(s) === 'solo'),
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const shownOf = q => (orgOpen.has(q.coord.name) ? q.workers : q.workers.slice(0, ORG_FOLD))
|
|
60
|
+
// How wide its people are, inside the tray; the team's width and (until drawn) height.
|
|
61
|
+
const rowWidth = n => (n ? n * CARD_W + (n - 1) * CARD_GAP : 0)
|
|
62
|
+
const crewWidth = q => Math.max(LEAD_W, rowWidth(Math.min(PER_ROW, shownOf(q).length)))
|
|
63
|
+
function orgCardSize(q) {
|
|
64
|
+
const seen = orgSize.get(q.coord.name)
|
|
65
|
+
const n = shownOf(q).length
|
|
66
|
+
return seen ?? { w: crewWidth(q) + 2 * (CREW_PAD + TRAY), h: HEAD_H + 2 * CREW_PAD + 150 + (n ? 34 + Math.ceil(n / PER_ROW) * (160 + CARD_GAP) : 0) + 60 }
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Where each card goes: where it was put, or, for a new squad, the lowest
|
|
70
|
+
// free spot in a column (all of them with "arrange").
|
|
71
|
+
function orgLayout(squads, { arrange = false } = {}) {
|
|
72
|
+
const pos = new Map()
|
|
73
|
+
const loose = []
|
|
74
|
+
for (const q of squads) {
|
|
75
|
+
const m = state.squadMeta.get(q.coord.name)
|
|
76
|
+
if (!arrange && Number.isFinite(m?.x) && Number.isFinite(m?.y)) pos.set(q.coord.name, { x: m.x, y: m.y, ...orgCardSize(q) })
|
|
77
|
+
else loose.push(q)
|
|
78
|
+
}
|
|
79
|
+
if (loose.length) {
|
|
80
|
+
// (Side by side from the left, a row at a time, below the ones already placed.)
|
|
81
|
+
const width = ($('org').clientWidth || 1200) / orgZoom
|
|
82
|
+
let y = ORG_PAD
|
|
83
|
+
for (const p of pos.values()) y = Math.max(y, p.y + p.h + ORG_GAP_Y)
|
|
84
|
+
let x = ORG_PAD, shelf = 0
|
|
85
|
+
for (const q of loose) {
|
|
86
|
+
const size = orgCardSize(q)
|
|
87
|
+
if (x > ORG_PAD && x + size.w > width - ORG_PAD) { x = ORG_PAD; y += shelf + ORG_GAP_Y; shelf = 0 }
|
|
88
|
+
pos.set(q.coord.name, { x, y, ...size, auto: true })
|
|
89
|
+
x += size.w + ORG_GAP_X
|
|
90
|
+
shelf = Math.max(shelf, size.h)
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return pos
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// A link's line, between the two cards' sides (or top and bottom, when one
|
|
97
|
+
// is above the other), and the middle point for its label.
|
|
98
|
+
function orgLinkPath(p, q) {
|
|
99
|
+
const pc = p.x + p.w / 2, qc = q.x + q.w / 2
|
|
100
|
+
if (Math.abs(pc - qc) >= (p.w + q.w) / 2 + 20) {
|
|
101
|
+
const right = qc > pc
|
|
102
|
+
const x1 = right ? p.x + p.w : p.x, x2 = right ? q.x : q.x + q.w
|
|
103
|
+
const y1 = p.y + HEAD_H / 2, y2 = q.y + HEAD_H / 2
|
|
104
|
+
const d = Math.max(40, Math.abs(x2 - x1) / 2) * (right ? 1 : -1)
|
|
105
|
+
return { d: `M${x1} ${y1} C${x1 + d} ${y1} ${x2 - d} ${y2} ${x2} ${y2}`, mx: (x1 + x2) / 2, my: (y1 + y2) / 2 }
|
|
106
|
+
}
|
|
107
|
+
const down = q.y > p.y
|
|
108
|
+
const y1 = down ? p.y + p.h : p.y, y2 = down ? q.y : q.y + q.h
|
|
109
|
+
const d = Math.max(30, Math.abs(y2 - y1) / 2) * (down ? 1 : -1)
|
|
110
|
+
return { d: `M${pc} ${y1} C${pc} ${y1 + d} ${qc} ${y2 - d} ${qc} ${y2}`, mx: (pc + qc) / 2, my: (y1 + y2) / 2 }
|
|
111
|
+
}
|
|
112
|
+
function orgLinesHtml() {
|
|
113
|
+
let paths = '', labels = ''
|
|
114
|
+
for (const l of state.links) {
|
|
115
|
+
const p = orgPos.get(l.a), q = orgPos.get(l.b)
|
|
116
|
+
if (!p || !q) continue
|
|
117
|
+
const { d, mx, my } = orgLinkPath(p, q)
|
|
118
|
+
const n = recentOf(l)
|
|
119
|
+
// (Amber from three quarters of what two coordinators may send each other in an hour.)
|
|
120
|
+
const cls = n >= 60 ? 'full' : n >= 45 ? 'warn' : ''
|
|
121
|
+
paths += `<path class="l ${cls}" d="${d}"/><path class="hit" d="${d}" data-la="${esc(l.a)}" data-lb="${esc(l.b)}"/>`
|
|
122
|
+
labels += `<button class="org-label ${cls}" style="left:${Math.round(mx)}px;top:${Math.round(my)}px" data-la="${esc(l.a)}" data-lb="${esc(l.b)}" title="${esc(T.orgLinkRecent(n))}">${n}</button>`
|
|
123
|
+
}
|
|
124
|
+
return { paths, labels }
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// What an agent is on: its task in its coordinator's plan (since when), else
|
|
128
|
+
// the last thing its coordinator handed it, else, for a coordinator, its plan.
|
|
129
|
+
const handed = new Map() // worker -> { from, text, at }: the last message its coordinator sent it
|
|
130
|
+
const flashes = new Map() // worker -> when something was just handed to it (a light runs to it)
|
|
131
|
+
export function noteHandoff(m) {
|
|
132
|
+
if (m.system || !m.from || !m.target || !m.text) return
|
|
133
|
+
const w = sessions.get(m.target)
|
|
134
|
+
if (!w || orgCoordOf(w) !== m.from) return
|
|
135
|
+
handed.set(m.target, { from: m.from, text: m.text, at: m.created_at ?? now() })
|
|
136
|
+
flashes.set(m.target, now())
|
|
137
|
+
if (currentRoute().view !== 'org') return
|
|
138
|
+
soon(renderOrg)
|
|
139
|
+
setTimeout(() => { flashes.delete(m.target); if (currentRoute().view === 'org') soon(renderOrg) }, 2200)
|
|
140
|
+
}
|
|
141
|
+
function taskOf(s, kind) {
|
|
142
|
+
const coord = kind === 'coord' ? s.name : orgCoordOf(s)
|
|
143
|
+
const plan = coord ? state.plans.get(coord) : null
|
|
144
|
+
const t = plan?.tasks.find(x => x.who === s.name && x.state === 'doing')
|
|
145
|
+
if (t) return { title: t.title, ref: t.ref, since: t.since }
|
|
146
|
+
const h = handed.get(s.name)
|
|
147
|
+
if (h) return { title: h.text.replace(/\s+/g, ' '), from: h.from, since: h.at }
|
|
148
|
+
if (kind === 'coord' && plan?.title) return { title: plan.title }
|
|
149
|
+
return null
|
|
150
|
+
}
|
|
151
|
+
// Its state, as a pill: at work, waiting on you, idle, away.
|
|
152
|
+
function stateOf(s) {
|
|
153
|
+
const a = s.activity ?? {}
|
|
154
|
+
if (!s.online) return { cls: 'off', label: T.orgSt.off }
|
|
155
|
+
if (status(s).waiting) return { cls: 'need', label: T.orgSt.need }
|
|
156
|
+
if (['tool', 'working', 'compacting'].includes(a.state)) return { cls: 'work', label: T.orgSt.work }
|
|
157
|
+
return { cls: '', label: T.orgSt.idle }
|
|
158
|
+
}
|
|
159
|
+
// "claude-opus-5-5" as "Claude Opus 5.5"; other models as they come.
|
|
160
|
+
const modelName = m => (/^claude-/.test(m ?? '') ? m.replace(/^claude-([a-z]+)-(\d+)-(\d+).*$/, (_, f, a, b) => `Claude ${f[0].toUpperCase()}${f.slice(1)} ${a}.${b}`) : m ?? '')
|
|
161
|
+
const kTokens = n => (n >= 1000 ? `${Math.round(n / 1000)}k` : String(n))
|
|
162
|
+
const clockHtml = since => (since ? `<time class="oa-clock" data-since="${Number(since)}"></time>` : '')
|
|
163
|
+
|
|
164
|
+
function orgAgent(s, kind) {
|
|
165
|
+
const a = s.activity ?? {}
|
|
166
|
+
const st = stateOf(s)
|
|
167
|
+
const pct = a.context_pct
|
|
168
|
+
const window = a.context_window ?? (Number.isFinite(pct) && pct > 0 && a.context_tokens ? Math.round(a.context_tokens * 100 / pct) : null)
|
|
169
|
+
const task = taskOf(s, kind)
|
|
170
|
+
const model = [modelName(a.model), personaOf(s)].filter(Boolean).join(' · ')
|
|
171
|
+
const flash = flashes.has(s.name) && now() - flashes.get(s.name) < 2200
|
|
172
|
+
return `<div class="org-agent ${kind}${flash ? ' got' : ''}" data-orgtip="agent:${esc(s.name)}" data-orgopen="${esc(s.name)}" tabindex="0">
|
|
173
|
+
<div class="oa-h"><span class="oa-av">${agentMark(s)}<span class="oa-rl"><span class="role ${kind === 'coord' ? 'coord' : 'worker'}" aria-hidden="true"></span></span></span>
|
|
174
|
+
<span class="oa-who"><span class="oa-nm"><b>${esc(s.name)}</b><span class="oa-st ${st.cls}">${st.cls === 'work' ? '<span class="oa-spin" aria-hidden="true"></span>' : st.cls ? '<span class="oa-pd" aria-hidden="true"></span>' : ''}${esc(st.label)}</span></span>
|
|
175
|
+
${model ? `<span class="oa-model">${esc(model)}</span>` : ''}${kind === 'coord' ? `<span class="oa-lead">${esc(T.roleName.coordinator)}</span>` : ''}</span>
|
|
176
|
+
<button class="org-more" data-orgmenu="agent" data-name="${esc(s.name)}" aria-label="${esc(T.orgAgentMenu(s.name))}" aria-haspopup="menu">${icon('more')}</button></div>
|
|
177
|
+
<div class="oa-task${task ? '' : ' none'}" title="${esc(task?.title ?? '')}">${task?.from ? `<span class="oa-from">↳ ${esc(task.from)}</span>` : ''}${task?.ref ? `<span class="oa-ref">${esc(task.ref)}</span>` : ''}<span class="oa-tt">${esc(task?.title ?? T.orgNoTask)}</span>${clockHtml(task?.since)}</div>
|
|
178
|
+
<div class="oa-now">${esc(status(s).text)}</div>
|
|
179
|
+
${Number.isFinite(pct) ? `<div class="oa-ctx" title="${esc(T.contextPct(Math.round(pct)))}"><span class="oa-bar"><i class="${pct >= 95 ? 'hot' : pct >= 80 ? 'warm' : ''}" style="width:${Math.max(2, Math.min(100, pct))}%"></i></span>${a.context_tokens && window ? `${kTokens(a.context_tokens)} / ${kTokens(window)}` : `${Math.round(pct)}%`}</div>` : ''}
|
|
180
|
+
</div>`
|
|
181
|
+
}
|
|
182
|
+
// The lines from a coordinator down to its first row of workers (one lit while something is handed to it).
|
|
183
|
+
function orgWires(q) {
|
|
184
|
+
const first = shownOf(q).slice(0, PER_ROW)
|
|
185
|
+
if (!first.length) return ''
|
|
186
|
+
const inner = crewWidth(q), mid = inner / 2, start = (inner - rowWidth(first.length)) / 2
|
|
187
|
+
const paths = first.map((w, i) => {
|
|
188
|
+
const x = start + i * (CARD_W + CARD_GAP) + CARD_W / 2
|
|
189
|
+
const d = Math.abs(x - mid) < 1 ? `M${mid} 0 V34` : `M${mid} 0 V14 Q${mid} 18 ${mid + Math.sign(x - mid) * 4} 18 H${x - Math.sign(x - mid) * 4} Q${x} 18 ${x} 22 V34`
|
|
190
|
+
return `<path d="${d}"/>${flashes.has(w.name) ? `<path class="run" d="${d}"/>` : ''}`
|
|
191
|
+
}).join('')
|
|
192
|
+
return `<svg class="oa-wires" width="${inner}" height="34" viewBox="0 0 ${inner} 34" aria-hidden="true">${paths}<circle cx="${mid}" cy="1.5" r="3"/></svg>`
|
|
193
|
+
}
|
|
194
|
+
function orgCard(q, p) {
|
|
195
|
+
const c = q.coord, m = state.squadMeta.get(c.name) ?? {}
|
|
196
|
+
const shown = shownOf(q)
|
|
197
|
+
const all = [c, ...q.workers]
|
|
198
|
+
const working = all.filter(x => stateOf(x).cls === 'work').length, waiting = all.filter(x => stateOf(x).cls === 'need').length
|
|
199
|
+
const on = q.workers.filter(w => w.online).length
|
|
200
|
+
return `<div class="org-card" data-squad="${esc(c.name)}" style="left:${p.x}px;top:${p.y}px">
|
|
201
|
+
<div class="org-head" data-orgtip="squad:${esc(c.name)}" tabindex="0"><span class="org-tic">${icon('teams')}</span>
|
|
202
|
+
<span class="org-tt"><span class="org-title">${esc(m.title || c.name)}</span>${m.goal ? `<span class="org-goal">${esc(m.goal)}</span>` : ''}</span>
|
|
203
|
+
<span class="org-counts" title="${esc(T.squadCount(on, q.workers.length))}">${working ? `<span class="work">${esc(T.orgWorkingN(working))}</span>` : ''}${waiting ? `<span class="need">${esc(T.orgWaitingN(waiting))}</span>` : ''}<span>${esc(T.orgAgentsN(all.length))}</span></span>
|
|
204
|
+
<button class="org-more" data-orgmenu="squad" data-name="${esc(c.name)}" aria-label="${esc(T.orgSquadMenu(c.name))}" aria-haspopup="menu">${icon('more')}</button></div>
|
|
205
|
+
<div class="org-crew" style="--crew:${crewWidth(q)}px">
|
|
206
|
+
${orgAgent(c, 'coord')}
|
|
207
|
+
${orgWires(q)}
|
|
208
|
+
${shown.length ? `<div class="org-row">${shown.map(w => orgAgent(w, 'worker')).join('')}</div>` : ''}
|
|
209
|
+
${q.workers.length > ORG_FOLD ? `<button class="org-fold" data-orgfold="${esc(c.name)}">${orgOpen.has(c.name) ? T.orgLess : T.orgMore(q.workers.length - shown.length)}</button>` : ''}
|
|
210
|
+
${can('operator') ? `<button class="org-add" data-orgact="addagent" data-name="${esc(c.name)}">${icon('plus')} ${T.orgAddAgent}</button>` : ''}
|
|
211
|
+
</div>
|
|
212
|
+
${can('manager') ? `<span class="org-handle" data-orghandle="${esc(c.name)}" title="${esc(T.orgLinkHandle)}"></span>` : ''}</div>`
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// After a draw: each team's real size (for the next layout and the lines between them), and the clocks.
|
|
216
|
+
function measureOrg() {
|
|
217
|
+
let changed = false
|
|
218
|
+
for (const el of $('org').querySelectorAll('.org-card[data-squad]')) {
|
|
219
|
+
const name = el.dataset.squad, size = { w: el.offsetWidth, h: el.offsetHeight }
|
|
220
|
+
const was = orgSize.get(name)
|
|
221
|
+
if (!was || was.w !== size.w || was.h !== size.h) { orgSize.set(name, size); changed = true }
|
|
222
|
+
const p = orgPos.get(name)
|
|
223
|
+
if (p) Object.assign(p, size)
|
|
224
|
+
}
|
|
225
|
+
if (changed && $('org').querySelector('.org-lines')) drawOrgLines()
|
|
226
|
+
tickOrg()
|
|
227
|
+
return changed
|
|
228
|
+
}
|
|
229
|
+
const orgClock = ms => { const t = Math.max(0, Math.floor(ms / 1000)), h = Math.floor(t / 3600), m = Math.floor(t / 60) % 60; return t >= 86400 ? `${Math.floor(t / 86400)} d` : h ? `${h}:${String(m).padStart(2, '0')}:${String(t % 60).padStart(2, '0')}` : `${m}:${String(t % 60).padStart(2, '0')}` }
|
|
230
|
+
function tickOrg() { for (const el of $('org').querySelectorAll('[data-since]')) el.textContent = orgClock(now() - Number(el.dataset.since)) }
|
|
231
|
+
setInterval(() => { if (!$('org').hidden && currentRoute().view === 'org') tickOrg() }, 1000)
|
|
232
|
+
|
|
233
|
+
export function renderOrg(settling = false) {
|
|
234
|
+
if (orgDrag) return // (drawn again when the drag ends)
|
|
235
|
+
const { squads, orphans, solos } = orgSquads()
|
|
236
|
+
orgPos = orgLayout(squads)
|
|
237
|
+
const fresh = squads.filter(q => orgPos.get(q.coord.name).auto && !orgSaved.has(q.coord.name))
|
|
238
|
+
let w = 0, h = 0
|
|
239
|
+
for (const p of orgPos.values()) { w = Math.max(w, p.x + p.w); h = Math.max(h, p.y + p.h) }
|
|
240
|
+
w += ORG_PAD + 240; h += ORG_PAD + 160 // (room to drag a card further)
|
|
241
|
+
const { paths, labels } = orgLinesHtml()
|
|
242
|
+
const extra = [[T.noCoordinator, orphans], [T.standalone, solos]].filter(([, l]) => l.length)
|
|
243
|
+
.map(([label, l]) => `<div class="org-extra"><div class="org-extra-h">${esc(label)}</div>${l.map(s => orgAgent(s, 'loose')).join('')}</div>`).join('')
|
|
244
|
+
setHtml($('org'), `${squads.length ? '' : `<div class="org-empty"><p>${T.orgEmpty}</p>${can('operator') ? `<button class="btn primary" data-orgact="newsquad">${T.orgNewSquad}</button>` : ''}</div>`}
|
|
245
|
+
<div class="org-wrap" style="width:${Math.round(w * orgZoom)}px;height:${squads.length ? Math.round(h * orgZoom) : 0}px">
|
|
246
|
+
<div class="org-stage" style="width:${w}px;height:${h}px;transform:scale(${orgZoom})">
|
|
247
|
+
<svg class="org-lines" width="${w}" height="${h}">${paths}</svg>
|
|
248
|
+
${squads.map(q => orgCard(q, orgPos.get(q.coord.name))).join('')}
|
|
249
|
+
<div class="org-labels">${labels}</div>
|
|
250
|
+
</div>
|
|
251
|
+
</div>${extra ? `<div class="org-extras">${extra}</div>` : ''}`)
|
|
252
|
+
const resized = measureOrg()
|
|
253
|
+
// New squads: placed again once their real size is known, then they stay where they were put.
|
|
254
|
+
if (fresh.length && resized && settling !== true) return renderOrg(true)
|
|
255
|
+
if (fresh.length) {
|
|
256
|
+
for (const q of fresh) orgSaved.add(q.coord.name)
|
|
257
|
+
if (can('manager')) postJson('/api/org/positions', { positions: fresh.map(q => ({ coordinator: q.coord.name, x: orgPos.get(q.coord.name).x, y: orgPos.get(q.coord.name).y })) }).catch(() => {})
|
|
258
|
+
}
|
|
259
|
+
if (orgTipKey) {
|
|
260
|
+
const el = $('org').querySelector(`[data-orgtip="${CSS.escape(orgTipKey)}"]`)
|
|
261
|
+
if (el) showOrgTip(el); else hideOrgTip()
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// During a drag: only the moved card and the lines.
|
|
266
|
+
function drawOrgLines() {
|
|
267
|
+
const { paths, labels } = orgLinesHtml()
|
|
268
|
+
const svg = $('org').querySelector('.org-lines')
|
|
269
|
+
const tmp = svg.querySelector('.tmp')?.outerHTML ?? ''
|
|
270
|
+
svg.innerHTML = paths + tmp
|
|
271
|
+
$('org').querySelector('.org-labels').innerHTML = labels
|
|
272
|
+
}
|
|
273
|
+
const stagePoint = e => {
|
|
274
|
+
const r = $('org').querySelector('.org-stage').getBoundingClientRect()
|
|
275
|
+
return { x: (e.clientX - r.left) / orgZoom, y: (e.clientY - r.top) / orgZoom }
|
|
276
|
+
}
|
|
277
|
+
function endOrgDrag() {
|
|
278
|
+
if (!orgDrag) return
|
|
279
|
+
$('org').querySelectorAll('.drop').forEach(x => x.classList.remove('drop'))
|
|
280
|
+
$('org').querySelector('.org-lines .tmp')?.remove()
|
|
281
|
+
orgDrag = null
|
|
282
|
+
$('org')._html = undefined // (the DOM was changed by hand: drawn again in full)
|
|
283
|
+
renderOrg()
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// Hover (or keyboard focus): what a squad or an agent is for.
|
|
287
|
+
function orgTipHtml(key) {
|
|
288
|
+
const i = key.indexOf(':')
|
|
289
|
+
const kind = key.slice(0, i), name = key.slice(i + 1)
|
|
290
|
+
if (kind === 'agent') {
|
|
291
|
+
const s = sessions.get(name)
|
|
292
|
+
if (!s) return ''
|
|
293
|
+
const where = terminalOf(name)
|
|
294
|
+
const host = where?.host ?? hosts.get(s.host_id)
|
|
295
|
+
const folder = where?.term.folder ?? s.activity?.cwd
|
|
296
|
+
const purpose = purposeOf(s)
|
|
297
|
+
const pct = s.activity?.context_pct
|
|
298
|
+
return `<div class="org-tip-t">${esc(name)} ${roleTag(orgRole(s))}</div>
|
|
299
|
+
<p>${purpose ? esc(purpose) : `<i>${T.orgNoPurpose}</i>`}</p>
|
|
300
|
+
${personaOf(s) ? `<div>${esc(personaOf(s))}${s.description && s.persona?.description ? ` — <span class="m">${esc(s.persona.description)}</span>` : ''}</div>` : ''}
|
|
301
|
+
<div class="m"><span class="dot ${status(s).dot}"></span> ${esc(status(s).text)}</div>
|
|
302
|
+
${host || folder ? `<div class="m">${[host ? T.onHost(host.name) : '', folder].filter(Boolean).map(esc).join(' · ')}</div>` : ''}
|
|
303
|
+
${Number.isFinite(pct) ? `<div class="m">${esc(T.contextPct(Math.round(pct)))}</div>` : ''}`
|
|
304
|
+
}
|
|
305
|
+
const c = sessions.get(name)
|
|
306
|
+
if (!c) return ''
|
|
307
|
+
const m = state.squadMeta.get(name) ?? {}
|
|
308
|
+
const workers = orgSquads().squads.find(q => q.coord.name === name)?.workers ?? []
|
|
309
|
+
const linked = linksOf(name)
|
|
310
|
+
return `<div class="org-tip-t">${esc(m.title || name)}${m.title ? ` <span class="m">· ${esc(name)}</span>` : ''}</div>
|
|
311
|
+
<p>${m.goal ? esc(m.goal) : `<i>${T.orgNoGoal}</i>`}</p>
|
|
312
|
+
${personaOf(c) ? `<div>${esc(personaOf(c))}</div>` : ''}
|
|
313
|
+
<div class="m">${esc(T.squadCount(workers.filter(w => w.online).length, workers.length))}</div>
|
|
314
|
+
${c.online && !workers.some(w => w.online) ? `<div class="m">${esc(T.orgSelfWorking)}</div>` : ''}
|
|
315
|
+
${linked.length ? `<div class="m">${linked.map(n => esc(squadTitle(n))).join(', ')}</div>` : ''}`
|
|
316
|
+
}
|
|
317
|
+
const orgTip = document.createElement('div')
|
|
318
|
+
orgTip.className = 'org-tip'
|
|
319
|
+
orgTip.hidden = true
|
|
320
|
+
orgTip.setAttribute('role', 'tooltip')
|
|
321
|
+
document.body.appendChild(orgTip)
|
|
322
|
+
function showOrgTip(el) {
|
|
323
|
+
const html = orgTipHtml(el.dataset.orgtip)
|
|
324
|
+
if (!html) return hideOrgTip()
|
|
325
|
+
orgTipKey = el.dataset.orgtip
|
|
326
|
+
setHtml(orgTip, html)
|
|
327
|
+
orgTip.hidden = false
|
|
328
|
+
const r = el.getBoundingClientRect()
|
|
329
|
+
const w = orgTip.offsetWidth, h = orgTip.offsetHeight
|
|
330
|
+
const left = r.right + 10 + w <= innerWidth ? r.right + 10 : Math.max(8, r.left - 10 - w)
|
|
331
|
+
orgTip.style.left = `${left}px`
|
|
332
|
+
orgTip.style.top = `${Math.max(8, Math.min(r.top, innerHeight - h - 8))}px`
|
|
333
|
+
}
|
|
334
|
+
export function hideOrgTip() { orgTip.hidden = true; orgTipKey = null }
|
|
335
|
+
$('org').addEventListener('pointerover', e => { const el = e.target.closest('[data-orgtip]'); if (el && !orgDrag && !state.menuEl && el.dataset.orgtip !== orgTipKey) showOrgTip(el) })
|
|
336
|
+
$('org').addEventListener('pointerout', e => { const el = e.target.closest('[data-orgtip]'); if (el && !el.contains(e.relatedTarget)) hideOrgTip() })
|
|
337
|
+
$('org').addEventListener('focusin', e => { const el = e.target.closest('[data-orgtip]'); if (el) showOrgTip(el) })
|
|
338
|
+
$('org').addEventListener('focusout', hideOrgTip)
|
|
339
|
+
$('org').addEventListener('scroll', hideOrgTip)
|
|
340
|
+
|
|
341
|
+
// Dragging a card by its header moves it; dragging its handle onto another
|
|
342
|
+
// card links the two squads.
|
|
343
|
+
$('org').addEventListener('pointerdown', e => {
|
|
344
|
+
if (e.button !== 0 || orgNarrow()) return
|
|
345
|
+
const handle = e.target.closest('[data-orghandle]')
|
|
346
|
+
const head = !handle && !e.target.closest('button') && e.target.closest('.org-head')
|
|
347
|
+
if (!handle && !head) return
|
|
348
|
+
e.preventDefault()
|
|
349
|
+
hideOrgTip()
|
|
350
|
+
if (handle) {
|
|
351
|
+
const from = handle.dataset.orghandle
|
|
352
|
+
orgDrag = { kind: 'link', from }
|
|
353
|
+
$('org').querySelector('.org-lines').insertAdjacentHTML('beforeend', '<path class="l tmp" d=""/>')
|
|
354
|
+
} else {
|
|
355
|
+
const card = head.closest('.org-card')
|
|
356
|
+
if (!can('manager')) return
|
|
357
|
+
orgDrag = { kind: 'card', name: card.dataset.squad, card, px: e.clientX, py: e.clientY, x: parseFloat(card.style.left), y: parseFloat(card.style.top), moved: false }
|
|
358
|
+
}
|
|
359
|
+
})
|
|
360
|
+
addEventListener('pointermove', e => {
|
|
361
|
+
if (!orgDrag) return
|
|
362
|
+
if (orgDrag.kind === 'card') {
|
|
363
|
+
const dx = (e.clientX - orgDrag.px) / orgZoom, dy = (e.clientY - orgDrag.py) / orgZoom
|
|
364
|
+
if (!orgDrag.moved && Math.hypot(dx, dy) < 4) return
|
|
365
|
+
orgDrag.moved = true
|
|
366
|
+
const x = Math.max(0, Math.round(orgDrag.x + dx)), y = Math.max(0, Math.round(orgDrag.y + dy))
|
|
367
|
+
orgDrag.card.style.left = `${x}px`
|
|
368
|
+
orgDrag.card.style.top = `${y}px`
|
|
369
|
+
Object.assign(orgPos.get(orgDrag.name), { x, y })
|
|
370
|
+
drawOrgLines()
|
|
371
|
+
return
|
|
372
|
+
}
|
|
373
|
+
const p = orgPos.get(orgDrag.from)
|
|
374
|
+
if (!p) return
|
|
375
|
+
const to = stagePoint(e)
|
|
376
|
+
const x1 = p.x + p.w, y1 = p.y + HEAD_H / 2
|
|
377
|
+
$('org').querySelector('.org-lines .tmp')?.setAttribute('d', `M${x1} ${y1} C${x1 + 60} ${y1} ${to.x - 60} ${to.y} ${to.x} ${to.y}`)
|
|
378
|
+
const over = document.elementFromPoint(e.clientX, e.clientY)?.closest('.org-card')
|
|
379
|
+
$('org').querySelectorAll('.org-card.drop').forEach(x => { if (x !== over) x.classList.remove('drop') })
|
|
380
|
+
if (over && over.dataset.squad !== orgDrag.from) over.classList.add('drop')
|
|
381
|
+
})
|
|
382
|
+
addEventListener('pointerup', async e => {
|
|
383
|
+
const d = orgDrag
|
|
384
|
+
if (!d) return
|
|
385
|
+
if (d.kind === 'card') {
|
|
386
|
+
if (d.moved) {
|
|
387
|
+
const p = orgPos.get(d.name)
|
|
388
|
+
state.squadMeta.set(d.name, { ...(state.squadMeta.get(d.name) ?? { coordinator: d.name }), x: p.x, y: p.y })
|
|
389
|
+
postJson('/api/org/positions', { positions: [{ coordinator: d.name, x: p.x, y: p.y }] }).catch(err => alertBox(err.message))
|
|
390
|
+
}
|
|
391
|
+
endOrgDrag()
|
|
392
|
+
// (Its header pressed without moving it: the team, as its name in the list.)
|
|
393
|
+
if (!d.moved) openSquad(d.name)
|
|
394
|
+
return
|
|
395
|
+
}
|
|
396
|
+
const to = document.elementFromPoint(e.clientX, e.clientY)?.closest('.org-card')?.dataset.squad
|
|
397
|
+
endOrgDrag()
|
|
398
|
+
if (!to || to === d.from) return
|
|
399
|
+
try {
|
|
400
|
+
const r = await postJson('/api/links/add', { a: d.from, b: to })
|
|
401
|
+
if (r?.warnings?.length) alertBox(r.warnings.join(' · '))
|
|
402
|
+
} catch (err) { alertBox(err.message) }
|
|
403
|
+
})
|
|
404
|
+
addEventListener('keydown', e => { if (e.key === 'Escape' && orgDrag) endOrgDrag() })
|
|
405
|
+
addEventListener('resize', () => { if (currentRoute().view === 'org') soon(renderOrg) })
|
|
406
|
+
|
|
407
|
+
$('org').addEventListener('click', e => {
|
|
408
|
+
const menu = e.target.closest('[data-orgmenu]')
|
|
409
|
+
if (menu) return menu.dataset.orgmenu === 'squad' ? squadMenu(menu, menu.dataset.name) : agentMenu(menu, menu.dataset.name)
|
|
410
|
+
const fold = e.target.closest('[data-orgfold]')
|
|
411
|
+
if (fold) { const n = fold.dataset.orgfold; if (orgOpen.has(n)) orgOpen.delete(n); else orgOpen.add(n); return renderOrg() }
|
|
412
|
+
const link = e.target.closest('[data-la]')
|
|
413
|
+
if (link) return linkMenu(link, link.dataset.la, link.dataset.lb)
|
|
414
|
+
// (A header pressed where it can't be dragged: on a phone, or without the right to move it.)
|
|
415
|
+
const head = e.target.closest('.org-head')
|
|
416
|
+
if (head && !e.target.closest('button') && (orgNarrow() || !can('manager'))) openSquad(head.closest('.org-card').dataset.squad)
|
|
417
|
+
})
|
|
418
|
+
$('org').addEventListener('keydown', e => {
|
|
419
|
+
const head = e.target.closest?.('.org-head')
|
|
420
|
+
if (head && e.target === head && (e.key === 'Enter' || e.key === ' ')) { e.preventDefault(); openSquad(head.closest('.org-card').dataset.squad) }
|
|
421
|
+
const agent = e.target.closest?.('[data-orgopen]')
|
|
422
|
+
if (agent && e.target === agent && (e.key === 'Enter' || e.key === ' ')) { e.preventDefault(); openAgent(agent.dataset.orgopen) }
|
|
423
|
+
})
|
|
424
|
+
// A team's page: what its header leads to, here as in the list.
|
|
425
|
+
const openSquad = name => { location.hash = `#/q/${encodeURIComponent(name)}` }
|
|
426
|
+
// An agent pressed: its terminal, where it runs (else its session).
|
|
427
|
+
function openAgent(name) {
|
|
428
|
+
const where = terminalOf(name)
|
|
429
|
+
location.hash = where?.host?.online && can('operator') ? `#/t/${encodeURIComponent(where.host.id)}/${encodeURIComponent(name)}` : `#/s/${encodeURIComponent(name)}`
|
|
430
|
+
}
|
|
431
|
+
$('org').addEventListener('click', e => {
|
|
432
|
+
const a = e.target.closest('[data-orgopen]')
|
|
433
|
+
if (a && !e.target.closest('button')) openAgent(a.dataset.orgopen)
|
|
434
|
+
})
|
|
435
|
+
|
|
436
|
+
// (The top bar's buttons too.)
|
|
437
|
+
document.addEventListener('click', e => {
|
|
438
|
+
const act = e.target.closest('[data-orgact]')
|
|
439
|
+
if (act) {
|
|
440
|
+
if (act.dataset.orgact === 'newsquad') location.hash = '#/new'
|
|
441
|
+
if (act.dataset.orgact === 'addagent') addWorkerTo(act.dataset.name)
|
|
442
|
+
if (act.dataset.orgact === 'arrange') arrangeOrg()
|
|
443
|
+
}
|
|
444
|
+
const z = e.target.closest('[data-orgzoom]')
|
|
445
|
+
if (z) {
|
|
446
|
+
const v = z.dataset.orgzoom
|
|
447
|
+
orgZoom = v === '1' ? 1 : Math.min(1.6, Math.max(0.4, Math.round((orgZoom + (v === '+' ? 0.1 : -0.1)) * 10) / 10))
|
|
448
|
+
store.set(companyPref('pishaOrgZoom'), String(orgZoom))
|
|
449
|
+
renderOrg()
|
|
450
|
+
renderTop()
|
|
451
|
+
}
|
|
452
|
+
})
|
|
453
|
+
|
|
454
|
+
function arrangeOrg() {
|
|
455
|
+
const { squads } = orgSquads()
|
|
456
|
+
if (!squads.length) return
|
|
457
|
+
const pos = orgLayout(squads, { arrange: true })
|
|
458
|
+
for (const [name, p] of pos) state.squadMeta.set(name, { ...(state.squadMeta.get(name) ?? { coordinator: name }), x: p.x, y: p.y })
|
|
459
|
+
renderOrg()
|
|
460
|
+
postJson('/api/org/positions', { positions: [...pos].map(([coordinator, p]) => ({ coordinator, x: p.x, y: p.y })) }).catch(err => alertBox(err.message))
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
// A small menu next to what was clicked: [{label, run, danger, disabled}].
|
|
464
|
+
function orgMenuAt(anchor, items, head = '') {
|
|
465
|
+
closeMenu()
|
|
466
|
+
hideOrgTip()
|
|
467
|
+
state.menuEl = document.createElement('div')
|
|
468
|
+
state.menuEl.className = 'menu'
|
|
469
|
+
state.menuEl.innerHTML = (head ? `<div class="hint">${head}</div>` : '') +
|
|
470
|
+
items.filter(Boolean).map((it, i) => `<button data-i="${i}" class="${it.danger ? 'danger' : ''}" ${it.disabled ? 'disabled' : ''}>${esc(it.label)}</button>`).join('')
|
|
471
|
+
document.body.appendChild(state.menuEl)
|
|
472
|
+
const r = anchor.getBoundingClientRect()
|
|
473
|
+
const w = state.menuEl.offsetWidth
|
|
474
|
+
state.menuEl.style.top = `${Math.min(r.bottom + 4, innerHeight - state.menuEl.offsetHeight - 8)}px`
|
|
475
|
+
state.menuEl.style.left = `${Math.max(8, Math.min(r.right - w, innerWidth - w - 8))}px`
|
|
476
|
+
const list = items.filter(Boolean)
|
|
477
|
+
state.menuEl.addEventListener('click', e => {
|
|
478
|
+
const b = e.target.closest('button')
|
|
479
|
+
if (!b || b.disabled) return
|
|
480
|
+
closeMenu()
|
|
481
|
+
list[Number(b.dataset.i)].run()
|
|
482
|
+
})
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
function squadMenu(anchor, name) {
|
|
486
|
+
const workers = orgSquads().squads.find(q => q.coord.name === name)?.workers ?? []
|
|
487
|
+
const members = [...workers.map(w => w.name), name]
|
|
488
|
+
const running = members.filter(n => terminalOf(n))
|
|
489
|
+
const anyOn = members.some(n => sessions.get(n)?.online)
|
|
490
|
+
orgMenuAt(anchor, [
|
|
491
|
+
can('manager') ? { label: T.orgEditSquad, run: () => squadDialog(name) } : null,
|
|
492
|
+
can('operator') ? { label: T.addWorkerTo(name), run: () => addWorkerTo(name) } : null,
|
|
493
|
+
can('manager') ? { label: T.links(linksOf(name).length), run: () => linksDialog(name) } : null,
|
|
494
|
+
{ label: T.viewSquad, run: () => { location.hash = `#/q/${encodeURIComponent(name)}` } },
|
|
495
|
+
can('manager') ? { label: T.saveTemplateTitle(name), run: () => saveTemplateDialog(name) } : null,
|
|
496
|
+
!can('operator') ? null : { label: T.orgCloseSquad, danger: true, disabled: !running.length, run: () => confirmDialog({
|
|
497
|
+
title: T.orgCloseSquadTitle(squadTitle(name)), text: T.orgCloseSquadText(running.map(n => `<b>${esc(n)}</b>`).join(', ')), confirm: T.orgCloseSquad, danger: true,
|
|
498
|
+
run: async () => { for (const n of running) { const w = terminalOf(n); if (w) await postJson('/api/terminals/kill', { host: w.host.id, term: n }) } },
|
|
499
|
+
}) },
|
|
500
|
+
!can('manager') ? null : { label: T.orgDeleteSquad, danger: true, disabled: anyOn || running.length > 0, run: () => confirmDialog({
|
|
501
|
+
title: T.orgDeleteSquadTitle(squadTitle(name)), text: T.orgDeleteSquadText(members.map(n => `<b>${esc(n)}</b>`).join(', ')), confirm: T.orgDeleteSquad, danger: true,
|
|
502
|
+
run: async () => { for (const n of members) if (sessions.has(n)) await post('delete', n) },
|
|
503
|
+
}) },
|
|
504
|
+
])
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
function agentMenu(anchor, name) {
|
|
508
|
+
const s = sessions.get(name)
|
|
509
|
+
if (!s) return
|
|
510
|
+
const where = terminalOf(name)
|
|
511
|
+
const role = s.opened_role ?? s.role
|
|
512
|
+
orgMenuAt(anchor, [
|
|
513
|
+
can('manager') ? { label: T.orgDescribe, run: () => describeDialog(name) } : null,
|
|
514
|
+
can('operator') && s.host_id && (role === 'worker' || role === 'coordinator') ? { label: T.orgReshape, disabled: !hosts.get(s.host_id)?.online, run: () => reshapeDialog(name) } : null,
|
|
515
|
+
where && can('operator') ? { label: T.terminal, run: () => { location.hash = `#/t/${encodeURIComponent(where.host.id)}/${encodeURIComponent(name)}` } } : null,
|
|
516
|
+
{ label: T.openChat, run: () => { location.hash = `#/s/${encodeURIComponent(name)}/chat` } },
|
|
517
|
+
reopenable(s) && can('operator') ? { label: T.reopen, run: () => reopen(name) } : null,
|
|
518
|
+
where && can('operator') ? { label: T.closeSession, danger: true, run: () => hostAction('kill', where.host.id, name) } : null,
|
|
519
|
+
can('manager') ? { label: T.deleteSession, danger: true, disabled: s.online, run: () => runAction('delete', name) } : null,
|
|
520
|
+
])
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
function linkMenu(anchor, a, b) {
|
|
524
|
+
const l = state.links.find(x => x.a === a && x.b === b)
|
|
525
|
+
if (!l || !can('manager')) return
|
|
526
|
+
orgMenuAt(anchor, [
|
|
527
|
+
{ label: T.unlink, danger: true, run: () => postJson('/api/links/delete', { a, b }).then(r => { if (r?.warnings?.length) alertBox(r.warnings.join(' · ')) }).catch(err => alertBox(err.message)) },
|
|
528
|
+
], `${esc(squadTitle(a))} ↔ ${esc(squadTitle(b))}<br>${esc(T.orgLinkRecent(recentOf(l)))}`)
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
export function squadDialog(name) {
|
|
532
|
+
const m = state.squadMeta.get(name) ?? {}
|
|
533
|
+
const d = dialog(`<h3>${esc(T.orgEditSquadTitle(name))}</h3>
|
|
534
|
+
<label class="field"><span>${T.orgSquadTitle}</span><input type="text" name="title" maxlength="40" value="${esc(m.title ?? '')}" placeholder="${esc(name)}" autocomplete="off"></label>
|
|
535
|
+
<label class="field"><span>${T.orgGoal}</span><textarea name="goal" maxlength="300" rows="3">${esc(m.goal ?? '')}</textarea><span class="hint">${T.orgGoalHint}</span></label>
|
|
536
|
+
<div class="err" style="color:var(--err);font-size:13px;min-height:1.2em"></div>
|
|
537
|
+
<div class="actions"><button class="btn" data-cancel>${T.cancel}</button><button class="btn primary" data-ok>${T.save}</button></div>`)
|
|
538
|
+
const f = n => d.el.querySelector(`[name=${n}]`)
|
|
539
|
+
f('title').focus()
|
|
540
|
+
d.el.querySelector('[data-ok]').addEventListener('click', async () => {
|
|
541
|
+
try { await postJson('/api/org/squad', { coordinator: name, title: f('title').value, goal: f('goal').value }); d.close() } catch (err) { d.el.querySelector('.err').textContent = err.message }
|
|
542
|
+
})
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
function describeDialog(name) {
|
|
546
|
+
const s = sessions.get(name)
|
|
547
|
+
if (!s) return
|
|
548
|
+
const d = dialog(`<h3>${esc(T.orgDescribeTitle(name))}</h3>
|
|
549
|
+
<label class="field"><span>${T.orgPurpose}</span><textarea name="desc" maxlength="300" rows="3" placeholder="${esc(s.persona?.description ?? '')}">${esc(s.description ?? '')}</textarea><span class="hint">${T.orgDescribeHint}</span></label>
|
|
550
|
+
<div class="err" style="color:var(--err);font-size:13px;min-height:1.2em"></div>
|
|
551
|
+
<div class="actions"><button class="btn" data-cancel>${T.cancel}</button><button class="btn primary" data-ok>${T.save}</button></div>`)
|
|
552
|
+
const ta = d.el.querySelector('textarea')
|
|
553
|
+
ta.focus()
|
|
554
|
+
d.el.querySelector('[data-ok]').addEventListener('click', async () => {
|
|
555
|
+
try { await postJson('/api/org/describe', { name, description: ta.value }); d.close() } catch (err) { d.el.querySelector('.err').textContent = err.message }
|
|
556
|
+
})
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
// Another persona, or (a worker) another squad: both are fixed when a
|
|
560
|
+
// session starts (its system prompt, its coordinator), so the session is
|
|
561
|
+
// closed and opened again with them, in the same folder.
|
|
562
|
+
function reshapeDialog(name) {
|
|
563
|
+
const s = sessions.get(name)
|
|
564
|
+
const h = hosts.get(s?.host_id)
|
|
565
|
+
if (!s || !h?.online) return alertBox(T.orgNeedsMachine)
|
|
566
|
+
const where = terminalOf(name)
|
|
567
|
+
const folder = where?.term.folder ?? s.activity?.cwd
|
|
568
|
+
if (!folder) return alertBox(T.orgNoFolder(name))
|
|
569
|
+
const role = s.opened_role ?? s.role
|
|
570
|
+
const coords = orgSquads().squads.map(q => q.coord).filter(x => x.name !== name)
|
|
571
|
+
const resume = h.info?.allow_resume === true
|
|
572
|
+
const was = where?.term.persona?.id ?? s.opened_persona ?? ''
|
|
573
|
+
const d = dialog(`<h3>${esc(T.orgReshapeTitle(name))}</h3><p class="hint" style="font-size:12.5px;margin:-2px 0 12px">${T.orgReshapeHint(esc(folder))}</p>
|
|
574
|
+
<label class="field"><span>${T.persona}</span><select name="persona"><option value="">${T.personaNone}</option></select></label>
|
|
575
|
+
<div class="persona-hint"></div>
|
|
576
|
+
${role === 'worker' ? `<label class="field"><span>${T.orgSquad}</span><select name="coord">${coords.map(c => `<option value="${esc(c.name)}" ${c.name === s.coordinator ? 'selected' : ''}>${esc(squadTitle(c.name))}${squadTitle(c.name) !== c.name ? ` (${esc(c.name)})` : ''}</option>`).join('')}</select></label>` : ''}
|
|
577
|
+
${resume ? `<label style="display:flex;gap:8px;align-items:center;font-size:13px;margin:4px 0 10px"><input type="checkbox" name="resume" checked> ${T.orgResume}</label>` : `<p class="hint" style="font-size:12px;color:var(--muted)">${T.orgNoResume}</p>`}
|
|
578
|
+
<div class="err" style="color:var(--err);font-size:13px;min-height:1.2em"></div>
|
|
579
|
+
<div class="actions"><button class="btn" data-cancel>${T.cancel}</button><button class="btn primary" data-ok>${T.orgReopenBtn}</button></div>`, { wide: true })
|
|
580
|
+
const f = n => d.el.querySelector(`[name=${n}]`)
|
|
581
|
+
const hint = d.el.querySelector('.persona-hint')
|
|
582
|
+
let list = []
|
|
583
|
+
const note = () => { const p = list.find(x => x.id === f('persona').value); hint.textContent = p?.description ?? '' }
|
|
584
|
+
f('persona').addEventListener('change', note)
|
|
585
|
+
postJson('/api/hosts/personas', { host: h.id, folder }).then(r => {
|
|
586
|
+
list = r.personas ?? []
|
|
587
|
+
f('persona').innerHTML = `<option value="">${T.personaNone}</option>${personaOptions(list, was, false)}`
|
|
588
|
+
note()
|
|
589
|
+
}).catch(err => { hint.textContent = `${T.personaLoadFailed}: ${err.message}` })
|
|
590
|
+
const ok = d.el.querySelector('[data-ok]')
|
|
591
|
+
ok.addEventListener('click', async () => {
|
|
592
|
+
const spec = { name, role, folder }
|
|
593
|
+
if (role === 'worker') spec.coordinator = f('coord')?.value || orgCoordOf(s)
|
|
594
|
+
if (f('persona').value) spec.persona = f('persona').value
|
|
595
|
+
if (where?.term.compact_window) spec.compact_window = where.term.compact_window
|
|
596
|
+
if (f('resume')?.checked) spec.resume = true
|
|
597
|
+
ok.disabled = true
|
|
598
|
+
ok.textContent = T.orgReopening
|
|
599
|
+
try {
|
|
600
|
+
if (where) await postJson('/api/terminals/kill', { host: h.id, term: name })
|
|
601
|
+
// (Until its terminal is gone and it is no longer connected: the name is free again.)
|
|
602
|
+
const t0 = Date.now()
|
|
603
|
+
while (terminalOf(name) || sessions.get(name)?.online) {
|
|
604
|
+
if (Date.now() - t0 > 20_000) throw new Error(T.orgStillOn(name))
|
|
605
|
+
await new Promise(r => setTimeout(r, 300))
|
|
606
|
+
}
|
|
607
|
+
await postJson('/api/terminals/open', { host: h.id, spec })
|
|
608
|
+
d.close()
|
|
609
|
+
location.hash = `#/t/${encodeURIComponent(h.id)}/${encodeURIComponent(name)}`
|
|
610
|
+
} catch (err) {
|
|
611
|
+
d.el.querySelector('.err').textContent = err.message
|
|
612
|
+
ok.disabled = false
|
|
613
|
+
ok.textContent = T.orgReopenBtn
|
|
614
|
+
}
|
|
615
|
+
})
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
// links between squads: two coordinators that may message each other. Set
|
|
619
|
+
// here; the machine of each one is told while it runs.
|
|
620
|
+
export function linksDialog(coord) {
|
|
621
|
+
const body = () => {
|
|
622
|
+
const mine = linksOf(coord)
|
|
623
|
+
const others = sortedSessions().filter(s => s.role === 'coordinator' && s.name !== coord && !mine.includes(s.name))
|
|
624
|
+
return `${mine.length
|
|
625
|
+
? `<div class="tpl-list">${mine.map(n => { const o = sessions.get(n); return `<div class="tpl-row"><span class="dot ${o ? status(o).dot : ''}"></span><span class="n">${esc(n)}</span><span class="m">${personaTag(o?.persona)}</span><button class="btn danger" data-unlink="${esc(n)}">${T.unlink}</button></div>` }).join('')}</div>`
|
|
626
|
+
: `<p class="hint">${T.noLinks}</p>`}
|
|
627
|
+
${others.length
|
|
628
|
+
? `<label class="field"><span>${T.linkWith}</span><span style="display:flex;gap:8px"><select name="peer" style="flex:1">${others.map(o => `<option value="${esc(o.name)}">${esc(o.name)}</option>`).join('')}</select><button class="btn primary" data-link>${T.linkAdd}</button></span></label>`
|
|
629
|
+
: `<p class="hint">${T.noOtherCoordinators}</p>`}`
|
|
630
|
+
}
|
|
631
|
+
const d = dialog(`<h3>${esc(T.linksTitle(coord))}</h3><p class="hint" style="font-size:12.5px;margin:-2px 0 12px">${T.linksHint}</p>
|
|
632
|
+
<div data-body>${body()}</div>
|
|
633
|
+
<div class="err" style="color:var(--err);font-size:13px;min-height:1.2em"></div>
|
|
634
|
+
<div class="actions"><button class="btn" data-cancel>${T.done}</button></div>`, { wide: true })
|
|
635
|
+
const err = d.el.querySelector('.err')
|
|
636
|
+
const refresh = () => setHtml(d.el.querySelector('[data-body]'), body())
|
|
637
|
+
linkListeners.add(refresh)
|
|
638
|
+
const closed = d.el.onclose
|
|
639
|
+
d.el.onclose = () => { linkListeners.delete(refresh); closed?.() }
|
|
640
|
+
const change = async (path, other) => {
|
|
641
|
+
err.textContent = ''
|
|
642
|
+
try {
|
|
643
|
+
const r = await postJson(path, { a: coord, b: other })
|
|
644
|
+
// (The link is set; a machine that couldn't apply it now says why.)
|
|
645
|
+
if (r?.warnings?.length) err.textContent = r.warnings.join(' · ')
|
|
646
|
+
} catch (e) { err.textContent = e.message }
|
|
647
|
+
}
|
|
648
|
+
d.el.addEventListener('click', e => {
|
|
649
|
+
if (e.target.closest('[data-link]')) return change('/api/links/add', d.el.querySelector('[name=peer]').value)
|
|
650
|
+
const u = e.target.closest('[data-unlink]')
|
|
651
|
+
if (u) change('/api/links/delete', u.dataset.unlink)
|
|
652
|
+
})
|
|
653
|
+
}
|