@akira-yylm/session-calendar 1.1.2 → 2.0.2

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/index.js CHANGED
@@ -25,12 +25,108 @@
25
25
  * throwing so the browser can show its own retry state.
26
26
  */
27
27
 
28
- /** `webServer` carries the route; `sessionPersistence` owns the headers. */
28
+ /** `webServer` carries the routes; `sessionPersistence` owns the headers. */
29
+ import { mkdir, readFile, rename, writeFile } from 'node:fs/promises'
30
+ import { dirname } from 'node:path'
31
+ import { homedir } from 'node:os'
32
+
29
33
  export const inject = ['webServer', 'sessionPersistence']
30
34
 
31
35
  /** Same-origin path the Client half fetches. */
32
36
  const ROUTE_PATH = '/dsh-session-calendar/created.json'
33
37
 
38
+ /**
39
+ * Appearance settings, kept in a file instead of the browser.
40
+ *
41
+ * Desktop starts the Web application on an ephemeral port (`--port 0`), so
42
+ * every launch has a new origin — and `localStorage` is per origin. Storing the
43
+ * look here keeps it across restarts, plugin reinstalls and port changes.
44
+ */
45
+ const SETTINGS_PATH = '/dsh-session-calendar/settings.json'
46
+
47
+ /** Largest settings payload accepted; photos ride inside it as data URLs. */
48
+ const MAX_SETTINGS_BYTES = 8_000_000
49
+
50
+ /**
51
+ * Where the settings file lives.
52
+ *
53
+ * Deliberately outside the plugin package and outside the browser: uninstalling
54
+ * or reinstalling the plugin never touches `~/.dsh`, so a reinstall finds the
55
+ * file on its own. `DSH_SESSION_CALENDAR_SETTINGS` relocates it (an absolute
56
+ * path, or `~` for the home directory) when someone wants the file somewhere
57
+ * they back up.
58
+ */
59
+ /**
60
+ * The settings folder: a plain directory inside DSH's own home, named so it is
61
+ * obvious what it belongs to. It lives outside the plugin package and nothing in
62
+ * this plugin ever deletes it, so uninstalling or reinstalling never touches it.
63
+ */
64
+ function settingsFolder() {
65
+ const home = process.env.DSH_HOME ?? homedir() + '/.dsh'
66
+ return home + '/会话日历配置文件'
67
+ }
68
+
69
+ /**
70
+ * Where the settings file lives.
71
+ *
72
+ * `DSH_SESSION_CALENDAR_SETTINGS` relocates it (an absolute path, or `~` for the
73
+ * home directory) when someone wants the file somewhere they back up.
74
+ */
75
+ function settingsFile() {
76
+ const override = typeof process.env.DSH_SESSION_CALENDAR_SETTINGS === 'string'
77
+ ? process.env.DSH_SESSION_CALENDAR_SETTINGS.trim()
78
+ : ''
79
+ if (override !== '') {
80
+ if (override === '~') return homedir() + '/settings.json'
81
+ if (override.startsWith('~/')) return homedir() + override.slice(1)
82
+ return override
83
+ }
84
+ return settingsFolder() + '/settings.json'
85
+ }
86
+
87
+ /** Where 2.0.0's earlier builds kept the same payload; read once, then migrated. */
88
+ function legacySettingsFile() {
89
+ const home = process.env.DSH_HOME ?? homedir() + '/.dsh'
90
+ return home + '/storages/session-calendar/settings.json'
91
+ }
92
+
93
+ /** Parse one settings file, or null when it is missing or unreadable. */
94
+ async function readOneSettingsFile(file) {
95
+ try {
96
+ const parsed = JSON.parse(await readFile(file, 'utf8'))
97
+ return parsed !== null && typeof parsed === 'object' ? parsed : null
98
+ } catch {
99
+ return null
100
+ }
101
+ }
102
+
103
+ async function readSettingsFile() {
104
+ const current = await readOneSettingsFile(settingsFile())
105
+ if (current !== null) return current
106
+ // A file written by the previous build moves to the named folder on first read.
107
+ if (settingsFile() !== legacySettingsFile()) {
108
+ const older = await readOneSettingsFile(legacySettingsFile())
109
+ if (older !== null) {
110
+ try {
111
+ await writeSettingsFile(older)
112
+ } catch {
113
+ // Reading still works; the copy is only a migration convenience.
114
+ }
115
+ return older
116
+ }
117
+ }
118
+ return null
119
+ }
120
+
121
+ /** Write atomically: a crash mid-write must not leave a half file behind. */
122
+ async function writeSettingsFile(value) {
123
+ const file = settingsFile()
124
+ await mkdir(dirname(file), { recursive: true })
125
+ const temporary = file + '.tmp'
126
+ await writeFile(temporary, JSON.stringify(value), 'utf8')
127
+ await rename(temporary, file)
128
+ }
129
+
34
130
  /** Wire version of the response body. */
35
131
  const WIRE_VERSION = 2
36
132
 
@@ -194,4 +290,78 @@ export function apply(ctx) {
194
290
  }),
195
291
  'session-calendar: creation-time route',
196
292
  )
293
+
294
+ ctx.effect(
295
+ () => ctx.webServer.register({
296
+ kind: 'exact',
297
+ path: SETTINGS_PATH,
298
+ /**
299
+ * A `handler` route, not a `resource`: the web carrier answers 405 to
300
+ * every method but GET/HEAD on resource routes, and saving the look needs
301
+ * a PUT. The handler owns the whole exchange, so it reads the body itself.
302
+ */
303
+ handler: async (request, response) => {
304
+ const method = request.method ?? 'GET'
305
+ const sendJson = (status, value) => {
306
+ const body = JSON.stringify(value)
307
+ response.writeHead(status, {
308
+ 'content-type': 'application/json; charset=utf-8',
309
+ 'cache-control': 'no-store',
310
+ 'content-length': Buffer.byteLength(body),
311
+ })
312
+ response.end(body)
313
+ }
314
+ if (method === 'GET' || method === 'HEAD') {
315
+ let stored = null
316
+ try {
317
+ stored = await readSettingsFile()
318
+ } catch {
319
+ stored = null
320
+ }
321
+ const body = JSON.stringify({ version: 1, settings: stored })
322
+ response.writeHead(200, {
323
+ 'content-type': 'application/json; charset=utf-8',
324
+ 'cache-control': 'no-store',
325
+ 'content-length': Buffer.byteLength(body),
326
+ })
327
+ response.end(method === 'HEAD' ? undefined : body)
328
+ return
329
+ }
330
+ if (method === 'PUT' || method === 'POST') {
331
+ const chunks = []
332
+ let size = 0
333
+ for await (const chunk of request) {
334
+ size += chunk.length
335
+ if (size > MAX_SETTINGS_BYTES) {
336
+ response.writeHead(413, { 'content-type': 'application/json; charset=utf-8' })
337
+ response.end(JSON.stringify({ version: 1, error: 'too large' }))
338
+ return
339
+ }
340
+ chunks.push(chunk)
341
+ }
342
+ let parsed
343
+ try {
344
+ parsed = JSON.parse(Buffer.concat(chunks).toString('utf8'))
345
+ } catch {
346
+ sendJson(400, { version: 1, error: 'invalid JSON' })
347
+ return
348
+ }
349
+ if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {
350
+ sendJson(400, { version: 1, error: 'expected an object' })
351
+ return
352
+ }
353
+ try {
354
+ await writeSettingsFile(parsed)
355
+ sendJson(200, { version: 1, ok: true })
356
+ } catch (error) {
357
+ sendJson(500, { version: 1, error: error instanceof Error ? error.message : String(error) })
358
+ }
359
+ return
360
+ }
361
+ response.writeHead(405, { allow: 'GET, HEAD, PUT, POST' })
362
+ response.end()
363
+ },
364
+ }),
365
+ 'session-calendar: settings route',
366
+ )
197
367
  }
package/locale/en.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "meta": {
3
3
  "title": "Session calendar",
4
- "description": "Adds **one button, 'Session calendar'** (rounded, a faint brand-blue wash with the calendar mark) below the 'Free session' row; it opens a small calendar beside the sidebar with a full-width **First / Latest switch** under the month title — First files a session on the day its first conversation happened, Latest on the day you last messaged it, and the choice is remembered. A day lists its sessions grouped by their workspaces, and the day frames and dots take the workspace colours involved (a gradient when several). Clicking a row opens that session and leaves the calendar open. Entry: the 'Session calendar' button in the left sidebar; inside the calendar use ‹ › to page months, 'Today' to return, and × to close, and (with focus in the card) the arrow keys to move a day or a week. Usage: click a date, read the day's sessions grouped in sidebar order (title plus the active rule's time), then click any row to open it; clicking outside or pressing Esc also dismisses. Turning the plugin off removes the button and the window and leaves DSH sessions, workspaces, and their order untouched. Data: reads only bounded slices of this machine's session logs (the session object's creation time and the first user message at the head); 'Latest' reuses DSH's own session summary `updatedAt`; titles and grouping come from the page's existing session and workspace data, nothing is uploaded, no session is written, and command or status records are excluded from ordering; days follow the browser's local time zone; subagent, blank, and archived sessions are hidden by the sidebar's rules; workspace colours are assigned in registry order from an eight-colour family; the button follows the New-session row's visibility."
4
+ "description": "Adds **one button, 'Session calendar'** (rounded, a faint brand-blue wash with the calendar mark) below the 'Free session' row; it opens a small calendar beside the sidebar with a full-width **First / Latest switch** under the month title — First files a session on the day its first conversation happened, Latest on the day you last messaged it, and the choice is remembered. A day lists its sessions grouped by their workspaces, and the day frames and dots take the workspace colours involved (a gradient when several). Clicking a row opens that session and leaves the calendar open. A cog in the title row opens an appearance editor: the background can be a colour (with opacity) or a local photo (zoom, drag to position, its own opacity); the colour family can be added to, removed and reordered; the selected colour, borders and text are editable; the font dropdown is searchable (Chinese, pinyin and English all match) and lists weight variants; the sidebar button has its own icon/text/border/background colours; profiles can be saved, renamed, switched and reset. Notes: the look lives in ~/.dsh/会话日历配置文件/settings.json on this machine (never deleted by the plugin, so reinstalling keeps it) and is never uploaded (localStorage is only a cache, so restarts keep it); photos are compressed to a 2400px longest edge at WebP 0.92 (JPEG 0.9 where unsupported) and every profile keeps its own copy, so a write that does not fit the quota is skipped silently; the photo is blended with the colour mode's colour and opacity; the font only affects this plugin. Entry: the 'Session calendar' button in the left sidebar; inside the calendar use ‹ › to page months, 'Today' to return, and × to close, and (with focus in the card) the arrow keys to move a day or a week. Usage: click a date, read the day's sessions grouped in sidebar order (title plus the active rule's time), then click any row to open it; clicking outside or pressing Esc also dismisses. Turning the plugin off removes the button and the window and leaves DSH sessions, workspaces, and their order untouched. Data: reads only bounded slices of this machine's session logs (the session object's creation time and the first user message at the head); 'Latest' reuses DSH's own session summary `updatedAt`; titles and grouping come from the page's existing session and workspace data, nothing is uploaded, no session is written, and command or status records are excluded from ordering; days follow the browser's local time zone; subagent, blank, and archived sessions are hidden by the sidebar's rules; workspace colours are assigned in registry order from an eight-colour family; the button follows the New-session row's visibility."
5
5
  }
6
- }
6
+ }
package/locale/zh.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "meta": {
3
3
  "title": "会话日历",
4
- "description": "在左侧导航「免费会话」下面增加**一个按钮「会话日历」**(圆角、品牌蓝淡底 + 日历图标),点开后在左侧栏右边弹出小窗日历:月份标题下面有「首次|最近」分段开关(整行、每格约 160px 宽)——首次 = 按会话第一次对话时间归日,最近 = 按你最近一次发消息的时间归日,开关的选择会记住。某一天的会话按各自所属的工作区分组列出,日期格边框与圆点是当天涉及的工作区颜色(多个工作区为渐变);点条目直接打开该会话且小窗保持打开。入口:左侧导航「会话日历」;小窗内用 ‹ › 翻月、「今天」回到当天、右上角 × 关闭;焦点在日历里时可用方向键(← → 前后一天、↑ ↓ 前后一周)。使用:点日期 → 下方按工作区顺序分组列出当天会话(标题 + 当前规则的时间)→ 点任意一条即打开;点小窗外面或按 Esc 也能收起。关闭插件后按钮与小窗消失,DSH 的会话、工作区和会话顺序都不受影响。数据:只读本机会话日志的有界切片(对象创建时间、开头第一条用户消息),「最近」直接用 DSH 会话摘要里的 `updatedAt`;标题与分组取自页面已有的会话/工作区数据,不上传、不改写任何会话;命令/状态类事件不计入排序;日期按浏览器本地时区划分;子代理会话、空白会话与已归档会话按左侧栏同样的规则隐藏;工作区颜色按注册表顺序从 8 色阶分配;入口跟随「新会话」那一行显隐。"
4
+ "description": "在左侧导航「免费会话」下面增加**一个按钮「会话日历」**(圆角、品牌蓝淡底 + 日历图标),点开后在左侧栏右边弹出小窗日历:月份标题下面有「首次|最近」分段开关(整行、每格约 160px 宽)——首次 = 按会话第一次对话时间归日,最近 = 按你最近一次发消息的时间归日,开关的选择会记住。某一天的会话按各自所属的工作区分组列出,日期格边框与圆点是当天涉及的工作区颜色(多个工作区为渐变);点条目直接打开该会话且小窗保持打开。标题行的齿轮打开外观设置:背景可用纯色(颜色 + 透明度)或本地照片(可缩放、拖动定位、单独调透明度),主界面色系可增删和排序,选中色/边框/文字可调,字体下拉菜单支持搜索(中文/拼音/英文互查)并按字重列出变体,侧栏入口的图标/字体/边框/背景可单独调色,配置文件可保存、重命名、切换并一键恢复默认。注意事项:外观存在本机文件 ~/.dsh/会话日历配置文件/settings.json(插件不删这个文件夹,卸载重装仍在)、不上传(localStorage 仅作缓存,重启/换端口不丢);照片入库前压到最长边 2400px/WebP 0.92(不支持 WebP 用 JPEG 0.9),每份配置各存一份,配额不足时该次保存会被静默跳过;照片的底沿用纯色的颜色与透明度;字体只作用于本插件。入口:左侧导航「会话日历」;小窗内用 ‹ › 翻月、「今天」回到当天、右上角 × 关闭;焦点在日历里时可用方向键(← → 前后一天、↑ ↓ 前后一周)。使用:点日期 → 下方按工作区顺序分组列出当天会话(标题 + 当前规则的时间)→ 点任意一条即打开;点小窗外面或按 Esc 也能收起。关闭插件后按钮与小窗消失,DSH 的会话、工作区和会话顺序都不受影响。数据:只读本机会话日志的有界切片(对象创建时间、开头第一条用户消息),「最近」直接用 DSH 会话摘要里的 `updatedAt`;标题与分组取自页面已有的会话/工作区数据,不上传、不改写任何会话;命令/状态类事件不计入排序;日期按浏览器本地时区划分;子代理会话、空白会话与已归档会话按左侧栏同样的规则隐藏;工作区颜色按注册表顺序从 8 色阶分配;入口跟随「新会话」那一行显隐。"
5
5
  }
6
- }
6
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@akira-yylm/session-calendar",
3
- "version": "1.1.2",
3
+ "version": "2.0.2",
4
4
  "type": "module",
5
5
  "description": "Session calendar: a month calendar beside the sidebar listing every session created on the selected day, grouped by workspace.",
6
6
  "license": "MIT",