pisha-server 0.0.0-stage → 0.0.12

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.
Files changed (129) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +40 -2
  3. package/accounts/accounts.js +754 -0
  4. package/accounts/index.js +1 -0
  5. package/accounts/passwords.js +41 -0
  6. package/accounts/totp.js +54 -0
  7. package/boards/boards.js +1087 -0
  8. package/boards/github.js +212 -0
  9. package/boards/index.js +1 -0
  10. package/boards/sync.js +980 -0
  11. package/companies.js +294 -0
  12. package/company-export.js +101 -0
  13. package/company.js +82 -0
  14. package/dashboard/dashboard.js +408 -0
  15. package/dashboard/index.js +1 -0
  16. package/data-upgrade.js +69 -0
  17. package/install/linux.sh +181 -0
  18. package/install/macos.sh +121 -0
  19. package/install/windows.bat +54 -0
  20. package/machines/index.js +3 -0
  21. package/machines/machines.js +910 -0
  22. package/messaging/inbox.js +252 -0
  23. package/messaging/index.js +3 -0
  24. package/messaging/messaging.js +551 -0
  25. package/messaging/plans.js +67 -0
  26. package/observation/index.js +1 -0
  27. package/observation/observation.js +231 -0
  28. package/organization/index.js +1 -0
  29. package/organization/organization.js +218 -0
  30. package/package.json +55 -4
  31. package/permissions/index.js +1 -0
  32. package/permissions/permissions.js +211 -0
  33. package/pisha-host.mjs +3303 -0
  34. package/pisha-host.mjs.sig +1 -0
  35. package/platform/acme.js +207 -0
  36. package/platform/basics.js +14 -0
  37. package/platform/config.js +154 -0
  38. package/platform/control.js +93 -0
  39. package/platform/credentials.js +151 -0
  40. package/platform/database.js +251 -0
  41. package/platform/errors.js +13 -0
  42. package/platform/files.js +50 -0
  43. package/platform/http.js +207 -0
  44. package/platform/index.js +58 -0
  45. package/platform/limits.js +122 -0
  46. package/platform/listen.js +183 -0
  47. package/platform/roles.js +6 -0
  48. package/platform/stream.js +19 -0
  49. package/platform/text.js +83 -0
  50. package/platform/ws.js +186 -0
  51. package/routes.js +405 -0
  52. package/server.js +65 -0
  53. package/sessions/index.js +1 -0
  54. package/sessions/sessions.js +533 -0
  55. package/ui/LICENSE-affectui.txt +26 -0
  56. package/ui/addmachine.js +151 -0
  57. package/ui/api.js +20 -0
  58. package/ui/app.js +143 -0
  59. package/ui/attention.js +78 -0
  60. package/ui/back.js +168 -0
  61. package/ui/boards.js +958 -0
  62. package/ui/chat.js +388 -0
  63. package/ui/content.js +157 -0
  64. package/ui/dialogs.js +135 -0
  65. package/ui/doing.js +112 -0
  66. package/ui/dom.js +6 -0
  67. package/ui/ds-base.css +146 -0
  68. package/ui/ds-components.css +229 -0
  69. package/ui/fonts/LICENSE-space-grotesk.txt +93 -0
  70. package/ui/fonts/space-grotesk-latin-700-normal.woff2 +0 -0
  71. package/ui/fonts/space-grotesk-latin-ext-700-normal.woff2 +0 -0
  72. package/ui/format.js +25 -0
  73. package/ui/i18n.js +2145 -0
  74. package/ui/icons/apple-touch-icon.png +0 -0
  75. package/ui/icons/icon-192.png +0 -0
  76. package/ui/icons/icon-512.png +0 -0
  77. package/ui/icons/maskable-512.png +0 -0
  78. package/ui/icons.js +41 -0
  79. package/ui/inbox.js +535 -0
  80. package/ui/index.html +105 -0
  81. package/ui/machines.js +261 -0
  82. package/ui/manifest.webmanifest +16 -0
  83. package/ui/mosaic.js +189 -0
  84. package/ui/nav.js +142 -0
  85. package/ui/newteam.js +223 -0
  86. package/ui/org.js +669 -0
  87. package/ui/overlays.css +126 -0
  88. package/ui/palette.js +180 -0
  89. package/ui/people.js +371 -0
  90. package/ui/permissions.js +202 -0
  91. package/ui/qr.js +158 -0
  92. package/ui/router.js +173 -0
  93. package/ui/routes.js +47 -0
  94. package/ui/screen-boards.css +241 -0
  95. package/ui/screen-inbox.css +201 -0
  96. package/ui/screen-machines.css +76 -0
  97. package/ui/screen-mosaic.css +26 -0
  98. package/ui/screen-newteam.css +90 -0
  99. package/ui/screen-org.css +119 -0
  100. package/ui/screen-session.css +99 -0
  101. package/ui/screen-signin.css +28 -0
  102. package/ui/screen-team.css +80 -0
  103. package/ui/screen-teams.css +41 -0
  104. package/ui/screen-usage.css +60 -0
  105. package/ui/session.js +42 -0
  106. package/ui/shell.css +122 -0
  107. package/ui/signin.js +120 -0
  108. package/ui/squads.js +371 -0
  109. package/ui/state.js +59 -0
  110. package/ui/stream.js +269 -0
  111. package/ui/styles.css +343 -0
  112. package/ui/team.js +115 -0
  113. package/ui/teams.js +58 -0
  114. package/ui/terminal.js +261 -0
  115. package/ui/theme.js +32 -0
  116. package/ui/timeline.js +163 -0
  117. package/ui/topbar.js +211 -0
  118. package/ui/usage.js +175 -0
  119. package/ui/voice-parse.js +98 -0
  120. package/ui/voice.js +320 -0
  121. package/usage/index.js +1 -0
  122. package/usage/usage.js +127 -0
  123. package/vendor/InterVariable.woff2 +0 -0
  124. package/vendor/LICENSE-inter +92 -0
  125. package/vendor/LICENSE-xterm +21 -0
  126. package/vendor/README.md +8 -0
  127. package/vendor/addon-fit.js +2 -0
  128. package/vendor/xterm.css +218 -0
  129. package/vendor/xterm.js +2 -0
@@ -0,0 +1,211 @@
1
+ import { randomBytes } from 'node:crypto'
2
+ import { bad, now, oneLine, cleanText } from '../platform/index.js'
3
+
4
+ // Permission profiles: what the agents of a team may do without asking,
5
+ // written in the dashboard and turned into Claude Code's own permission rules
6
+ // (allow, ask, deny), which its machine passes to each session it opens from
7
+ // the dashboard (claude --settings). Claude Code enforces them, not the model;
8
+ // what the user wrote in their own words goes to the session's system prompt
9
+ // too, as the reason. Each machine decides how far the dashboard may go
10
+ // ("permissions_from_dashboard" in its settings.json): pisha-host checks
11
+ // every rule again and refuses what it doesn't take.
12
+ //
13
+ // A profile is a few capabilities, each allowed, always asked or forbidden
14
+ // (left out: Claude Code decides, as without a profile), and rules of its own.
15
+
16
+ // What each capability is, as rules. deny: what forbidding it denies (when
17
+ // it isn't the same list).
18
+ export const CAPABILITIES = {
19
+ edit: { rules: ['Edit(./**)'], deny: ['Edit(**)'] },
20
+ tests: {
21
+ rules: [
22
+ 'Bash(npm test *)', 'Bash(npm run test *)', 'Bash(npm run build *)', 'Bash(npm run lint *)', 'Bash(pnpm test *)', 'Bash(pnpm run test *)',
23
+ 'Bash(yarn test *)', 'Bash(npx vitest *)', 'Bash(npx jest *)', 'Bash(npx playwright test *)', 'Bash(node --test *)', 'Bash(pytest *)',
24
+ 'Bash(python -m pytest *)', 'Bash(python3 -m pytest *)', 'Bash(go test *)', 'Bash(go build *)', 'Bash(cargo test *)', 'Bash(cargo build *)',
25
+ 'Bash(make test *)', 'Bash(php artisan test *)', 'Bash(vendor/bin/phpunit *)', 'Bash(composer test *)', 'Bash(dotnet test *)', 'Bash(mvn test *)',
26
+ ],
27
+ },
28
+ install: {
29
+ rules: [
30
+ 'Bash(npm install *)', 'Bash(npm ci *)', 'Bash(pnpm install *)', 'Bash(pnpm add *)', 'Bash(yarn install *)', 'Bash(yarn add *)',
31
+ 'Bash(pip install *)', 'Bash(pip3 install *)', 'Bash(composer install *)', 'Bash(composer require *)', 'Bash(bundle install *)',
32
+ 'Bash(go mod download *)', 'Bash(cargo fetch *)',
33
+ ],
34
+ },
35
+ commit: {
36
+ rules: [
37
+ 'Bash(git status *)', 'Bash(git diff *)', 'Bash(git log *)', 'Bash(git show *)', 'Bash(git add *)', 'Bash(git commit *)',
38
+ 'Bash(git restore *)', 'Bash(git stash *)', 'Bash(git switch *)', 'Bash(git branch *)',
39
+ ],
40
+ deny: ['Bash(git commit *)'],
41
+ },
42
+ push: { rules: ['Bash(git push *)'] },
43
+ history: { rules: ['Bash(git push --force *)', 'Bash(git push -f *)', 'Bash(git push --force-with-lease *)', 'Bash(git reset --hard *)', 'Bash(git clean *)', 'Bash(git rebase *)'] },
44
+ remove: { rules: ['Bash(rm *)', 'Bash(rmdir *)', 'Bash(git rm *)'] },
45
+ web: { rules: ['WebFetch', 'WebSearch'] },
46
+ docker: { rules: ['Bash(docker *)', 'Bash(docker-compose *)'] },
47
+ sudo: { rules: ['Bash(sudo *)'] },
48
+ }
49
+ export const CAPABILITY_IDS = Object.keys(CAPABILITIES)
50
+ const STATES = ['allow', 'ask', 'deny']
51
+
52
+ // Two to start from, as they are (a copy can be changed).
53
+ export const BUILTIN_PROFILES = [
54
+ {
55
+ id: 'readonly', builtin: true, name: 'Read only', notes: '',
56
+ caps: { edit: 'deny', install: 'deny', commit: 'deny', push: 'deny', history: 'deny', remove: 'deny', docker: 'deny', sudo: 'deny' },
57
+ extra: { allow: [], ask: [], deny: [] },
58
+ },
59
+ {
60
+ id: 'dev', builtin: true, name: 'Development', notes: '',
61
+ caps: { edit: 'allow', tests: 'allow', install: 'allow', commit: 'allow', push: 'ask', history: 'deny', remove: 'ask', sudo: 'deny' },
62
+ extra: { allow: [], ask: [], deny: [] },
63
+ },
64
+ ]
65
+
66
+ // A rule as Claude Code writes them: a tool, and what it applies to. (Its
67
+ // machine checks each one again, with what it takes from the dashboard.)
68
+ export const RULE_RE = /^(?:(?:Bash|PowerShell|Read|Edit|Write|NotebookEdit|WebFetch|WebSearch|Glob|Grep|Agent|Skill)(?:\((?:[^()\x00-\x1f\x7f]|\([^()\x00-\x1f\x7f]*\)){1,300}\))?|mcp__[A-Za-z0-9_-]+(?:__(?:[A-Za-z0-9_-]+|\*))?)$/
69
+ const RULES_MAX = 100
70
+ const NOTES_MAX = 2000
71
+ const PROFILES_MAX = 50
72
+ const ID_RE = /^(?:readonly|dev|p_[A-Za-z0-9_-]{8,24})$/
73
+ export const NONE = 'none' // a session that follows no profile, whatever its team's
74
+
75
+ export function createPermissions(platform, { sessions }) {
76
+ const { db, broadcast } = platform
77
+ const { NAME_RE } = sessions
78
+
79
+ db.exec(`CREATE TABLE IF NOT EXISTS permission_profiles (
80
+ id TEXT PRIMARY KEY, name TEXT NOT NULL, notes TEXT, caps TEXT NOT NULL, extra TEXT NOT NULL, updated_at INTEGER NOT NULL)`)
81
+ for (const [table, column] of [['squads', 'profile'], ['identities', 'permission_profile']]) {
82
+ if (!db.prepare(`PRAGMA table_info(${table})`).all().some(c => c.name === column)) db.exec(`ALTER TABLE ${table} ADD COLUMN ${column} TEXT`)
83
+ }
84
+
85
+ const parse = (s, dflt) => { try { const v = JSON.parse(s); return v && typeof v === 'object' ? v : dflt } catch { return dflt } }
86
+ function listProfiles() {
87
+ const own = db.prepare('SELECT * FROM permission_profiles ORDER BY name COLLATE NOCASE').all()
88
+ .map(r => ({ id: r.id, builtin: false, name: r.name, notes: r.notes || '', caps: parse(r.caps, {}), extra: { allow: [], ask: [], deny: [], ...parse(r.extra, {}) }, updated_at: r.updated_at }))
89
+ return [...BUILTIN_PROFILES, ...own]
90
+ }
91
+ const profileById = id => listProfiles().find(p => p.id === id) ?? null
92
+
93
+ // What the dashboard sends, checked: anything unexpected is refused.
94
+ function cleanRules(list, what) {
95
+ if (list == null) return []
96
+ if (!Array.isArray(list)) throw bad(`${what} must be a list of rules`)
97
+ const out = []
98
+ for (const raw of list) {
99
+ const r = typeof raw === 'string' ? raw.trim() : ''
100
+ if (!r) continue
101
+ if (!RULE_RE.test(r)) throw bad(`"${cleanText(r.slice(0, 80))}" is not a Claude Code permission rule (e.g. Bash(make deploy *), Edit(./docs/**), WebFetch(domain:example.com))`)
102
+ if (!out.includes(r)) out.push(r)
103
+ }
104
+ return out
105
+ }
106
+ function cleanProfile(p) {
107
+ const name = oneLine(p.name, 40)
108
+ if (!name) throw bad('the profile needs a name')
109
+ const notes = typeof p.notes === 'string' ? cleanText(p.notes.slice(0, NOTES_MAX * 2)).trim().slice(0, NOTES_MAX) : ''
110
+ const caps = {}
111
+ for (const [k, v] of Object.entries(p.caps && typeof p.caps === 'object' ? p.caps : {})) {
112
+ if (!CAPABILITY_IDS.includes(k)) throw bad(`unknown capability "${cleanText(String(k).slice(0, 40))}"`)
113
+ if (v == null || v === '' || v === 'default') continue
114
+ if (!STATES.includes(v)) throw bad(`${k}: must be allow, ask or deny`)
115
+ caps[k] = v
116
+ }
117
+ const e = p.extra && typeof p.extra === 'object' ? p.extra : {}
118
+ const extra = { allow: cleanRules(e.allow, 'allow'), ask: cleanRules(e.ask, 'ask'), deny: cleanRules(e.deny, 'deny') }
119
+ if (extra.allow.length + extra.ask.length + extra.deny.length > RULES_MAX) throw bad(`at most ${RULES_MAX} rules of its own`)
120
+ return { name, notes, caps, extra }
121
+ }
122
+
123
+ function saveProfile(p) {
124
+ const id = p.id == null || p.id === '' ? null : String(p.id)
125
+ if (id && !ID_RE.test(id)) throw bad('invalid profile id')
126
+ if (id && BUILTIN_PROFILES.some(b => b.id === id)) throw bad('the built-in profiles are kept as they are: make a copy to change it')
127
+ const v = cleanProfile(p)
128
+ const others = listProfiles().filter(x => x.id !== id)
129
+ if (others.some(x => x.name.toLowerCase() === v.name.toLowerCase())) throw bad(`there is already a profile called "${v.name}"`)
130
+ if (id && !db.prepare('SELECT 1 FROM permission_profiles WHERE id = ?').get(id)) throw bad('profile not found')
131
+ if (!id && db.prepare('SELECT COUNT(*) AS n FROM permission_profiles').get().n >= PROFILES_MAX) throw bad(`at most ${PROFILES_MAX} profiles`)
132
+ const newId = id ?? `p_${randomBytes(9).toString('base64url')}`
133
+ db.prepare(`INSERT INTO permission_profiles (id, name, notes, caps, extra, updated_at) VALUES (?, ?, ?, ?, ?, ?)
134
+ ON CONFLICT(id) DO UPDATE SET name = excluded.name, notes = excluded.notes, caps = excluded.caps, extra = excluded.extra, updated_at = excluded.updated_at`)
135
+ .run(newId, v.name, v.notes || null, JSON.stringify(v.caps), JSON.stringify(v.extra), now())
136
+ broadcastAll()
137
+ return profileById(newId)
138
+ }
139
+ function deleteProfile(p) {
140
+ const id = String(p.id ?? '')
141
+ if (BUILTIN_PROFILES.some(b => b.id === id)) throw bad('the built-in profiles cannot be deleted')
142
+ if (!db.prepare('DELETE FROM permission_profiles WHERE id = ?').run(id).changes) throw bad('profile not found')
143
+ // (Teams and sessions that had it follow none now, as before it existed.)
144
+ db.prepare('UPDATE squads SET profile = NULL WHERE profile = ?').run(id)
145
+ db.prepare('UPDATE identities SET permission_profile = NULL WHERE permission_profile = ?').run(id)
146
+ broadcastAll()
147
+ return { id, deleted: true }
148
+ }
149
+
150
+ // Which profile a team follows (its coordinator's name), or a session on
151
+ // its own (NONE: none, whatever its team's; null: its team's).
152
+ function assignProfile(p) {
153
+ const profile = p.profile == null || p.profile === '' ? null : String(p.profile)
154
+ if (profile && profile !== NONE && !profileById(profile)) throw bad('profile not found')
155
+ if (p.coordinator != null) {
156
+ const coordinator = String(p.coordinator)
157
+ if (!NAME_RE.test(coordinator)) throw bad('invalid coordinator')
158
+ if (profile === NONE) throw bad('a team follows a profile or none (leave it empty)')
159
+ db.prepare(`INSERT INTO squads (coordinator, profile, updated_at) VALUES (?, ?, ?)
160
+ ON CONFLICT(coordinator) DO UPDATE SET profile = excluded.profile, updated_at = excluded.updated_at`).run(coordinator, profile, now())
161
+ broadcast('profiles', state())
162
+ return { coordinator, profile }
163
+ }
164
+ const name = String(p.session ?? '')
165
+ if (!NAME_RE.test(name) || !db.prepare('SELECT 1 FROM identities WHERE name = ?').get(name)) throw bad('session not found')
166
+ db.prepare('UPDATE identities SET permission_profile = ? WHERE name = ?').run(profile, name)
167
+ broadcast('profiles', state())
168
+ return { session: name, profile }
169
+ }
170
+
171
+ // What the dashboard shows: the profiles, and who follows which.
172
+ function state() {
173
+ return {
174
+ profiles: listProfiles(),
175
+ teams: Object.fromEntries(db.prepare('SELECT coordinator, profile FROM squads WHERE profile IS NOT NULL').all().map(r => [r.coordinator, r.profile])),
176
+ sessions: Object.fromEntries(db.prepare('SELECT name, permission_profile FROM identities WHERE permission_profile IS NOT NULL').all().map(r => [r.name, r.permission_profile])),
177
+ }
178
+ }
179
+ const broadcastAll = () => broadcast('profiles', state())
180
+
181
+ // The profile a session opened from the dashboard follows: its own, else its
182
+ // team's (a coordinator's own squad; a worker's coordinator's). null: none.
183
+ function effectiveProfile(name, role, coordinator) {
184
+ const own = db.prepare('SELECT permission_profile FROM identities WHERE name = ?').get(name)?.permission_profile ?? null
185
+ if (own === NONE) return null
186
+ const team = role === 'coordinator' ? name : role === 'worker' ? coordinator : null
187
+ const id = own ?? (team ? db.prepare('SELECT profile FROM squads WHERE coordinator = ?').get(team)?.profile ?? null : null)
188
+ return id ? profileById(id) : null
189
+ }
190
+
191
+ // A profile as Claude Code's rules (and its words, for the system prompt).
192
+ // A capability forbidden wins over one allowed that shares a rule.
193
+ function compile(profile) {
194
+ if (!profile) return null
195
+ const allow = [], ask = [], deny = []
196
+ const add = (list, rules) => { for (const r of rules) if (!list.includes(r)) list.push(r) }
197
+ for (const id of CAPABILITY_IDS) {
198
+ const state = profile.caps?.[id]
199
+ const cap = CAPABILITIES[id]
200
+ if (state === 'allow') add(allow, cap.rules)
201
+ else if (state === 'ask') add(ask, cap.rules)
202
+ else if (state === 'deny') add(deny, cap.deny ?? cap.rules)
203
+ }
204
+ add(allow, profile.extra?.allow ?? [])
205
+ add(ask, profile.extra?.ask ?? [])
206
+ add(deny, profile.extra?.deny ?? [])
207
+ return { id: profile.id, name: profile.name, allow, ask, deny, notes: profile.notes || '' }
208
+ }
209
+
210
+ return { listProfiles, saveProfile, deleteProfile, assignProfile, permissionsState: state, effectiveProfile, compile, NONE }
211
+ }