icex-dsh-fixer 0.2.4 → 0.2.6

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 (3) hide show
  1. package/client.js +73 -0
  2. package/index.js +74 -1
  3. package/package.json +1 -1
package/client.js CHANGED
@@ -777,6 +777,10 @@ window.__ModuleLoader__.load({
777
777
  'rail.jump': 'Jump to input {index}: {prompt}',
778
778
  'rail.input': 'Input {index}',
779
779
  'rail.fallback': 'Turn {turn}',
780
+ 'menu.deleteSession': 'Delete session',
781
+ 'menu.deleting': 'Deleting…',
782
+ 'menu.deleteConfirm': 'Delete this session? Its log moves to the trash folder.',
783
+ 'menu.deleteFailed': 'Delete failed: {message}',
780
784
  }
781
785
 
782
786
  const zh = {
@@ -798,6 +802,10 @@ window.__ModuleLoader__.load({
798
802
  'rail.label': '输入锚点',
799
803
  'rail.jump': '跳转到第 {index} 条输入:{prompt}',
800
804
  'rail.input': '第 {index} 条输入',
805
+ 'menu.deleteSession': '删除会话',
806
+ 'menu.deleting': '删除中…',
807
+ 'menu.deleteConfirm': '删除这个会话?日志会移到回收站目录,可以再捞回来。',
808
+ 'menu.deleteFailed': '删除失败:{message}',
801
809
  'rail.fallback': '第 {turn} 轮',
802
810
  }
803
811
 
@@ -867,6 +875,63 @@ window.__ModuleLoader__.load({
867
875
  )
868
876
  }
869
877
 
878
+ /**
879
+ * Session row menu item: delete one session by moving its log to the trash.
880
+ * The slot hands over the session id (as a string or a props object).
881
+ */
882
+ function DeleteSessionItem(props) {
883
+ const t = translator(props !== null && typeof props === 'object' ? props.t : undefined)
884
+ const sessionId = typeof props === 'string'
885
+ ? props
886
+ : props !== null && typeof props === 'object'
887
+ ? props.sessionId ?? props.id ?? props.value
888
+ : undefined
889
+ const busyState = useState(false)
890
+ const busy = busyState[0]
891
+ if (typeof sessionId !== 'string' || sessionId === '') return null
892
+
893
+ function remove() {
894
+ if (typeof window === 'undefined') return
895
+ if (window.confirm(t('menu.deleteConfirm')) !== true) return
896
+ busyState[1](true)
897
+ fetch('/api/icex-fixer/delete-session', {
898
+ method: 'POST',
899
+ credentials: 'same-origin',
900
+ headers: { 'content-type': 'application/json' },
901
+ body: JSON.stringify({ id: sessionId }),
902
+ }).then(function (response) {
903
+ return response.json().then(function (body) {
904
+ if (body.ok !== true) throw new Error(body.error ?? String(response.status))
905
+ window.location.reload()
906
+ })
907
+ }).catch(function (error) {
908
+ busyState[1](false)
909
+ window.alert(t('menu.deleteFailed', { message: String(error.message ?? error) }))
910
+ })
911
+ }
912
+
913
+ return h('button', {
914
+ type: 'button',
915
+ role: 'menuitem',
916
+ disabled: busy,
917
+ className: 'icex-menu-danger',
918
+ style: {
919
+ display: 'block',
920
+ width: '100%',
921
+ padding: '6px 10px',
922
+ border: 0,
923
+ borderRadius: 'var(--dsw-radius-sm, 6px)',
924
+ background: 'transparent',
925
+ color: 'var(--dsw-alias-state-error-primary)',
926
+ textAlign: 'left',
927
+ font: 'inherit',
928
+ cursor: busy ? 'default' : 'pointer',
929
+ opacity: busy ? 0.6 : 1,
930
+ },
931
+ onClick: remove,
932
+ }, busy ? t('menu.deleting') : t('menu.deleteSession'))
933
+ }
934
+
870
935
  function apply(ctx) {
871
936
  runtimeContext = ctx
872
937
  ctx.effect(installStyles, 'icex-dsh-fixer: styles')
@@ -899,6 +964,14 @@ window.__ModuleLoader__.load({
899
964
  locale: NS,
900
965
  }, SettingsPage)
901
966
  })
967
+ ctx.slots.inject('sidebar.workspaces.session.menu.item', function () {
968
+ return ctx.slots.register({
969
+ name: 'sidebar.workspaces.session.menu.item',
970
+ id: 'icex-fixer-delete-session',
971
+ order: 900,
972
+ locale: NS,
973
+ }, DeleteSessionItem)
974
+ })
902
975
  }
903
976
 
904
977
  const plugin = {
package/index.js CHANGED
@@ -6,7 +6,8 @@
6
6
  * key events and native window bounds are not available to a browser plugin.
7
7
  */
8
8
  import { spawn } from 'node:child_process'
9
- import { existsSync } from 'node:fs'
9
+ import { existsSync, mkdirSync, readdirSync, renameSync } from 'node:fs'
10
+ import { homedir } from 'node:os'
10
11
  import { dirname, join } from 'node:path'
11
12
  import { fileURLToPath } from 'node:url'
12
13
 
@@ -18,6 +19,7 @@ const helper = join(here, 'native', 'icex-summon')
18
19
  * @param ctx - owning Cordis context.
19
20
  */
20
21
  export function apply(ctx) {
22
+ registerSessionDeleteRoute(ctx)
21
23
  if (process.platform !== 'darwin') return
22
24
  if (!existsSync(helper)) {
23
25
  console.warn('[icex-dsh-fixer] native summon helper is missing: ' + helper)
@@ -54,3 +56,74 @@ export function apply(ctx) {
54
56
  }
55
57
  }, 'icex-dsh-fixer: native summon helper')
56
58
  }
59
+
60
+ /** Session logs live under the DSH home; deleted logs move to a trash folder. */
61
+ const DSH_HOME = process.env.DSH_HOME && process.env.DSH_HOME !== '' ? process.env.DSH_HOME : join(homedir(), '.dsh')
62
+ const SESSIONS_ROOT = join(DSH_HOME, 'sessions')
63
+ const TRASH_ROOT = join(DSH_HOME, 'trash', 'sessions')
64
+
65
+ /** JSON response helper for the session-delete route. */
66
+ function jsonResponse(body, status = 200) {
67
+ return new Response(JSON.stringify(body), {
68
+ status,
69
+ headers: { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' },
70
+ })
71
+ }
72
+
73
+ /** Every on-disk directory for one session id, across project slug directories. */
74
+ function sessionDirectories(id) {
75
+ if (!/^[A-Za-z0-9][A-Za-z0-9._-]{0,200}$/.test(id)) return []
76
+ if (!existsSync(SESSIONS_ROOT)) return []
77
+ const found = []
78
+ for (const entry of readdirSync(SESSIONS_ROOT, { withFileTypes: true })) {
79
+ if (!entry.isDirectory() || entry.name.startsWith('.')) continue
80
+ const dir = join(SESSIONS_ROOT, entry.name, id)
81
+ if (existsSync(dir)) found.push(dir)
82
+ }
83
+ return found
84
+ }
85
+
86
+ /** Move a session's logs to the trash; the row disappears from the next list read. */
87
+ async function handleDeleteSession(request) {
88
+ let body
89
+ try {
90
+ body = await request.json()
91
+ } catch (error) {
92
+ return jsonResponse({ ok: false, error: 'body is not JSON' }, 400)
93
+ }
94
+ const id = typeof body?.id === 'string' ? body.id.trim() : ''
95
+ if (id === '') return jsonResponse({ ok: false, error: 'id is required' }, 400)
96
+
97
+ const sessions = request.ctx?.get?.('sessions')
98
+ try {
99
+ if (sessions !== undefined && typeof sessions.get === 'function' && sessions.get(id) !== undefined) {
100
+ return jsonResponse({ ok: false, error: 'session is open; close it first', code: 'live' }, 409)
101
+ }
102
+ } catch (error) {
103
+ // A registry probe must never block deletion of a closed session.
104
+ }
105
+
106
+ const dirs = sessionDirectories(id)
107
+ if (dirs.length === 0) return jsonResponse({ ok: false, error: 'session log not found', code: 'missing' }, 404)
108
+
109
+ mkdirSync(TRASH_ROOT, { recursive: true })
110
+ const moved = []
111
+ for (const dir of dirs) {
112
+ const target = join(TRASH_ROOT, id + '-' + Date.now() + '-' + moved.length)
113
+ renameSync(dir, target)
114
+ moved.push(target)
115
+ }
116
+ return jsonResponse({ ok: true, moved })
117
+ }
118
+
119
+ /** Claim the session-delete route when this host carries a browser connection. */
120
+ function registerSessionDeleteRoute(ctx) {
121
+ const connection = ctx.get('connection')
122
+ if (connection === undefined || connection.fetch === undefined || typeof connection.fetch.register !== 'function') return
123
+ ctx.effect(() => connection.fetch.register({
124
+ path: '/api/icex-fixer/delete-session',
125
+ methods: ['POST'],
126
+ requestBody: 'buffered',
127
+ fetch: (request) => handleDeleteSession(request),
128
+ }), 'icex-dsh-fixer: session delete route')
129
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "icex-dsh-fixer",
3
- "version": "0.2.4",
3
+ "version": "0.2.6",
4
4
  "description": "Adjust the conversation width, navigate user inputs from a left anchor rail, and summon the window with a right-Option double tap.",
5
5
  "type": "module",
6
6
  "exports": {