dsh-practical-frontend 0.0.0-stage → 0.3.0

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.
@@ -0,0 +1,3 @@
1
+ - insert:
2
+ - id: practical-frontend
3
+ name: 'dsh-practical-frontend'
package/icon.svg ADDED
@@ -0,0 +1,6 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" width="32" height="32" role="img" aria-label="Practical frontend tweaks">
2
+ <rect x="1" y="1" width="30" height="30" rx="8" fill="#0f1115" fill-opacity="0.07"/>
3
+ <rect x="6" y="7" width="20" height="4.5" rx="2.25" fill="#e5484d" fill-opacity="0.6"/>
4
+ <rect x="6" y="13.75" width="20" height="4.5" rx="2.25" fill="#ffb224" fill-opacity="0.6"/>
5
+ <rect x="6" y="20.5" width="20" height="4.5" rx="2.25" fill="#0091ff" fill-opacity="0.6"/>
6
+ </svg>
package/index.js ADDED
@@ -0,0 +1,383 @@
1
+ /**
2
+ * Host half of the practical-frontend bundle.
3
+ *
4
+ * It owns two durable records the browser half reads and writes:
5
+ * - the workspace → colour map behind the sidebar tint, and
6
+ * - the list of bookmarked conversation excerpts behind the 书签 panel.
7
+ *
8
+ * Both live beside the other plugin data under the Harness home, so they
9
+ * survive a reload, a new GUI port, and a Desktop restart. The endpoints are
10
+ * plain same-origin JSON routes on the existing web server; the browser half
11
+ * never learns a filesystem path.
12
+ */
13
+
14
+ import { randomUUID } from 'node:crypto'
15
+ import { mkdir, readFile, rename, writeFile } from 'node:fs/promises'
16
+ import { homedir } from 'node:os'
17
+ import { dirname, join, resolve } from 'node:path'
18
+
19
+ /** Same-origin endpoints shared with `client.js`. */
20
+ const COLOR_ROUTE = '/dsh-practical-frontend/project-colors'
21
+ const BOOKMARK_ROUTE = '/dsh-practical-frontend/bookmarks'
22
+
23
+ /** Upper bound for one colour-map request body. */
24
+ const MAX_COLOR_BODY_BYTES = 64 * 1024
25
+
26
+ /** Upper bound for one bookmark request body: one excerpt plus its metadata. */
27
+ const MAX_BOOKMARK_BODY_BYTES = 1024 * 1024
28
+
29
+ /** Only these writer fields are accepted from the browser. */
30
+ const MAX_WORKSPACES = 2000
31
+ const MAX_BOOKMARKS = 5000
32
+ const MAX_TEXT = 20000
33
+ const MAX_TITLE = 300
34
+ const MAX_NOTE = 2000
35
+ const MAX_SESSION_ID = 200
36
+ const MAX_WORKSPACE_ID = 200
37
+ const MAX_PATH = 1000
38
+ const MAX_ANCHOR_KEY = 200
39
+
40
+ /**
41
+ * Resolve one record path the way the rest of the Harness resolves its home.
42
+ *
43
+ * @param file - file name inside this plugin's data directory.
44
+ * @returns absolute path of the record.
45
+ */
46
+ function stateFilePath(file) {
47
+ const configured = process.env.DSH_HOME?.trim()
48
+ const home = configured !== undefined && configured.length > 0
49
+ ? resolve(configured)
50
+ : join(homedir(), '.dsh')
51
+ return join(home, 'plugin-data', 'dsh-practical-frontend', file)
52
+ }
53
+
54
+ /**
55
+ * Keep only the shape the browser half understands: workspace id → palette id.
56
+ *
57
+ * @param value - whatever was on disk.
58
+ * @returns a fresh record with string keys and string values.
59
+ */
60
+ function normalizeRecord(value) {
61
+ const record = {}
62
+ if (value === null || typeof value !== 'object') return record
63
+ const source = value.colors !== null && typeof value.colors === 'object' ? value.colors : value
64
+ for (const [key, color] of Object.entries(source)) {
65
+ if (key === 'colors' || typeof color !== 'string' || color.length > 32) continue
66
+ record[key] = color
67
+ if (Object.keys(record).length >= MAX_WORKSPACES) break
68
+ }
69
+ return record
70
+ }
71
+
72
+ /**
73
+ * Keep one string field bounded and free of NUL.
74
+ *
75
+ * @param value - whatever the browser sent.
76
+ * @param maximum - code-unit cap.
77
+ * @returns the field, or an empty string when it has the wrong type.
78
+ */
79
+ function stringField(value, maximum) {
80
+ if (typeof value !== 'string') return ''
81
+ return value.replaceAll('\u0000', '').slice(0, maximum)
82
+ }
83
+
84
+ /**
85
+ * Read one trimmed string field.
86
+ *
87
+ * @param value - whatever the browser sent.
88
+ * @param maximum - code-unit cap.
89
+ * @returns the trimmed field, or an empty string.
90
+ */
91
+ function trimmedField(value, maximum) {
92
+ return stringField(value, maximum).trim()
93
+ }
94
+
95
+ /**
96
+ * Keep one excerpt bounded by user-perceived characters so a long selection is
97
+ * never cut through a surrogate pair.
98
+ *
99
+ * @param value - whatever the browser sent.
100
+ * @param maximum - character cap.
101
+ * @returns the excerpt.
102
+ */
103
+ function clipText(value, maximum) {
104
+ if (typeof value !== 'string') return ''
105
+ const clean = value.replaceAll('\u0000', '')
106
+ if (clean.length <= maximum) return clean
107
+ return Array.from(clean).slice(0, maximum).join('')
108
+ }
109
+
110
+ /**
111
+ * Read one finite timestamp.
112
+ *
113
+ * @param value - whatever was on disk.
114
+ * @param fallback - value used when the field is not a usable timestamp.
115
+ * @returns a whole millisecond timestamp.
116
+ */
117
+ function timeField(value, fallback) {
118
+ return typeof value === 'number' && Number.isFinite(value) && value > 0
119
+ ? Math.trunc(value)
120
+ : fallback
121
+ }
122
+
123
+ /**
124
+ * Normalize one bookmarked excerpt.
125
+ *
126
+ * A bookmark without an id or any text is not meaningful and is dropped; the
127
+ * Host never keeps a record it cannot show in the panel.
128
+ *
129
+ * @param value - one entry from disk or from a request.
130
+ * @param now - clock used for missing timestamps.
131
+ * @returns the normalized bookmark, or null when the entry is unusable.
132
+ */
133
+ function normalizeBookmark(value, now) {
134
+ if (value === null || typeof value !== 'object') return null
135
+ const id = trimmedField(value.id, 120)
136
+ const text = clipText(value.text, MAX_TEXT)
137
+ if (id === '' || text.trim() === '') return null
138
+ return {
139
+ id,
140
+ title: trimmedField(value.title, MAX_TITLE),
141
+ text,
142
+ note: trimmedField(value.note, MAX_NOTE),
143
+ sessionId: trimmedField(value.sessionId, MAX_SESSION_ID),
144
+ sessionTitle: trimmedField(value.sessionTitle, MAX_TITLE),
145
+ workspaceId: trimmedField(value.workspaceId, MAX_WORKSPACE_ID),
146
+ workspaceTitle: trimmedField(value.workspaceTitle, MAX_TITLE),
147
+ workspacePath: trimmedField(value.workspacePath, MAX_PATH),
148
+ role: trimmedField(value.role, 32),
149
+ turn: Number.isSafeInteger(value.turn) && value.turn >= 0 ? value.turn : null,
150
+ anchorStartKey: trimmedField(value.anchorStartKey, MAX_ANCHOR_KEY),
151
+ anchorStartOffset: Number.isSafeInteger(value.anchorStartOffset) && value.anchorStartOffset >= 0
152
+ ? value.anchorStartOffset
153
+ : null,
154
+ anchorEndKey: trimmedField(value.anchorEndKey, MAX_ANCHOR_KEY),
155
+ anchorEndOffset: Number.isSafeInteger(value.anchorEndOffset) && value.anchorEndOffset >= 0
156
+ ? value.anchorEndOffset
157
+ : null,
158
+ createdAt: timeField(value.createdAt, now),
159
+ updatedAt: timeField(value.updatedAt, now),
160
+ }
161
+ }
162
+
163
+ /**
164
+ * Read a complete bookmark list, newest first.
165
+ *
166
+ * @param value - whatever was on disk.
167
+ * @returns a fresh, bounded list without duplicate ids.
168
+ */
169
+ function normalizeBookmarks(value, now = Date.now()) {
170
+ const source = Array.isArray(value)
171
+ ? value
172
+ : value !== null && typeof value === 'object' && Array.isArray(value.bookmarks)
173
+ ? value.bookmarks
174
+ : []
175
+ const seen = new Set()
176
+ const bookmarks = []
177
+ for (const entry of source) {
178
+ const bookmark = normalizeBookmark(entry, now)
179
+ if (bookmark === null || seen.has(bookmark.id)) continue
180
+ seen.add(bookmark.id)
181
+ bookmarks.push(bookmark)
182
+ if (bookmarks.length >= MAX_BOOKMARKS) break
183
+ }
184
+ bookmarks.sort((left, right) => right.createdAt - left.createdAt)
185
+ return bookmarks
186
+ }
187
+
188
+ /**
189
+ * Read one JSON request body.
190
+ *
191
+ * @param request - incoming request.
192
+ * @param maximum - body-size cap in bytes.
193
+ * @returns the parsed body, or an empty object for an empty body.
194
+ */
195
+ async function readJsonBody(request, maximum = MAX_COLOR_BODY_BYTES) {
196
+ const chunks = []
197
+ let total = 0
198
+ for await (const chunk of request) {
199
+ const buffer = Buffer.from(chunk)
200
+ total += buffer.length
201
+ if (total > maximum) throw new Error('request body too large')
202
+ chunks.push(buffer)
203
+ }
204
+ const text = Buffer.concat(chunks).toString('utf8')
205
+ if (text.trim() === '') return {}
206
+ const parsed = JSON.parse(text)
207
+ return parsed !== null && typeof parsed === 'object' ? parsed : {}
208
+ }
209
+
210
+ /**
211
+ * Write a JSON response.
212
+ *
213
+ * @param response - server response.
214
+ * @param status - HTTP status.
215
+ * @param body - JSON-serializable payload.
216
+ */
217
+ function writeJson(response, status, body) {
218
+ if (response.headersSent === true) {
219
+ response.destroy?.()
220
+ return
221
+ }
222
+ response.writeHead(status, { 'content-type': 'application/json; charset=utf-8' })
223
+ response.end(JSON.stringify(body))
224
+ }
225
+
226
+ /**
227
+ * Create one serialized atomic writer for a JSON record.
228
+ *
229
+ * Two rapid picks or saves cannot interleave because every write waits for the
230
+ * previous one; a failed write is logged and leaves the previous file intact.
231
+ *
232
+ * @param file - absolute record path.
233
+ * @param logger - Host logger, when available.
234
+ * @param label - record name used in the warning.
235
+ * @returns a function that writes one complete value.
236
+ */
237
+ function createJsonWriter(file, logger, label) {
238
+ let pending = Promise.resolve()
239
+ return (value) => {
240
+ pending = pending.then(async () => {
241
+ await mkdir(dirname(file), { recursive: true })
242
+ const temporary = `${file}.${process.pid}.tmp`
243
+ await writeFile(temporary, `${JSON.stringify(value, null, 2)}\n`, 'utf8')
244
+ await rename(temporary, file)
245
+ }).catch((error) => {
246
+ logger?.warn('practical-frontend: writing %s failed: %s', label, error?.message ?? error)
247
+ })
248
+ return pending
249
+ }
250
+ }
251
+
252
+ /**
253
+ * Mount the colour and bookmark endpoints once the web server exists.
254
+ *
255
+ * @param ctx - Host context.
256
+ */
257
+ export function apply(ctx) {
258
+ ctx.inject(['webServer'], (webCtx) => {
259
+ const colorsFile = stateFilePath('workspace-colors.json')
260
+ const bookmarksFile = stateFilePath('bookmarks.json')
261
+ const writeColors = createJsonWriter(colorsFile, webCtx.logger, 'workspace colours')
262
+ const writeBookmarks = createJsonWriter(bookmarksFile, webCtx.logger, 'bookmarks')
263
+
264
+ const readRecord = async () => {
265
+ try {
266
+ return normalizeRecord(JSON.parse(await readFile(colorsFile, 'utf8')))
267
+ } catch {
268
+ return {}
269
+ }
270
+ }
271
+
272
+ const readBookmarks = async () => {
273
+ try {
274
+ return normalizeBookmarks(JSON.parse(await readFile(bookmarksFile, 'utf8')))
275
+ } catch {
276
+ return []
277
+ }
278
+ }
279
+
280
+ webCtx.effect(() => webCtx.webServer.register({
281
+ kind: 'exact',
282
+ path: COLOR_ROUTE,
283
+ handler: async (request, response) => {
284
+ try {
285
+ if (request.method === 'GET') {
286
+ writeJson(response, 200, { colors: await readRecord() })
287
+ return
288
+ }
289
+ if (request.method === 'POST') {
290
+ const body = await readJsonBody(request, MAX_COLOR_BODY_BYTES)
291
+ const workspaceId = typeof body.workspaceId === 'string' ? body.workspaceId.trim() : ''
292
+ if (workspaceId === '' || workspaceId.length > MAX_WORKSPACE_ID) {
293
+ writeJson(response, 400, { error: 'workspaceId is required' })
294
+ return
295
+ }
296
+ const record = await readRecord()
297
+ if (typeof body.color === 'string' && body.color !== '') record[workspaceId] = body.color
298
+ else delete record[workspaceId]
299
+ await writeColors({ version: 1, colors: record })
300
+ writeJson(response, 200, { colors: record })
301
+ return
302
+ }
303
+ writeJson(response, 405, { error: 'method not allowed' })
304
+ } catch (error) {
305
+ writeJson(response, 500, { error: String(error?.message ?? error) })
306
+ }
307
+ },
308
+ }), 'practical-frontend: workspace colour endpoints')
309
+
310
+ webCtx.effect(() => webCtx.webServer.register({
311
+ kind: 'exact',
312
+ path: BOOKMARK_ROUTE,
313
+ handler: async (request, response) => {
314
+ try {
315
+ if (request.method === 'GET') {
316
+ writeJson(response, 200, { bookmarks: await readBookmarks() })
317
+ return
318
+ }
319
+ if (request.method === 'POST') {
320
+ const body = await readJsonBody(request, MAX_BOOKMARK_BODY_BYTES)
321
+ const action = typeof body.action === 'string' ? body.action : 'save'
322
+ const current = await readBookmarks()
323
+ const now = Date.now()
324
+
325
+ if (action === 'delete') {
326
+ const id = trimmedField(body.id, 120)
327
+ if (id === '') {
328
+ writeJson(response, 400, { error: 'id is required' })
329
+ return
330
+ }
331
+ const next = current.filter((bookmark) => bookmark.id !== id)
332
+ await writeBookmarks({ version: 1, bookmarks: next })
333
+ writeJson(response, 200, { bookmarks: next })
334
+ return
335
+ }
336
+
337
+ if (action !== 'save') {
338
+ writeJson(response, 400, { error: 'unknown action' })
339
+ return
340
+ }
341
+
342
+ const incoming = body.bookmark
343
+ if (incoming === null || typeof incoming !== 'object') {
344
+ writeJson(response, 400, { error: 'bookmark is required' })
345
+ return
346
+ }
347
+ const requestedId = trimmedField(incoming.id, 120)
348
+ const existing = requestedId === ''
349
+ ? undefined
350
+ : current.find((bookmark) => bookmark.id === requestedId)
351
+ // A client-supplied id is kept when it is new, which lets an
352
+ // optimistic row survive the round trip; anything else gets a Host id.
353
+ const id = existing !== undefined
354
+ ? existing.id
355
+ : requestedId !== '' && current.every((bookmark) => bookmark.id !== requestedId)
356
+ ? requestedId
357
+ : randomUUID()
358
+ const normalized = normalizeBookmark({
359
+ ...(existing ?? {}),
360
+ ...incoming,
361
+ id,
362
+ createdAt: existing?.createdAt ?? now,
363
+ updatedAt: now,
364
+ }, now)
365
+ if (normalized === null) {
366
+ writeJson(response, 400, { error: 'bookmark text is required' })
367
+ return
368
+ }
369
+ const next = [normalized, ...current.filter((bookmark) => bookmark.id !== normalized.id)]
370
+ next.sort((left, right) => right.createdAt - left.createdAt)
371
+ if (next.length > MAX_BOOKMARKS) next.length = MAX_BOOKMARKS
372
+ await writeBookmarks({ version: 1, bookmarks: next })
373
+ writeJson(response, 200, { bookmarks: next })
374
+ return
375
+ }
376
+ writeJson(response, 405, { error: 'method not allowed' })
377
+ } catch (error) {
378
+ writeJson(response, 500, { error: String(error?.message ?? error) })
379
+ }
380
+ },
381
+ }), 'practical-frontend: bookmark endpoints')
382
+ })
383
+ }
package/locale/en.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "meta": {
3
+ "title": "Practical Frontend Tweaks",
4
+ "description": "Tint workspace rows and bookmark conversation excerpts, grouped by project and highlighted in place."
5
+ }
6
+ }
package/locale/zh.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "meta": {
3
+ "title": "实用前端改造",
4
+ "description": "给侧边栏里的工作区设置背景色,并把对话片段收藏成按项目归类、可高亮定位的书签。"
5
+ }
6
+ }
package/package.json CHANGED
@@ -1,6 +1,63 @@
1
1
  {
2
2
  "name": "dsh-practical-frontend",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.3.0",
4
+ "description": "Practical front-end tweaks for DeepSeek Harness: tint workspace rows and bookmark conversation excerpts with in-place highlighting.",
5
+ "keywords": [
6
+ "dsh",
7
+ "dsh-plugin",
8
+ "deepseek-harness",
9
+ "sidebar",
10
+ "workspace",
11
+ "bookmark",
12
+ "highlight"
13
+ ],
14
+ "license": "MIT",
15
+ "author": {
16
+ "name": "kilokite"
17
+ },
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/kilokite/dsh-Practical-Frontend.git"
21
+ },
22
+ "homepage": "https://github.com/kilokite/dsh-Practical-Frontend#readme",
23
+ "bugs": {
24
+ "url": "https://github.com/kilokite/dsh-Practical-Frontend/issues"
25
+ },
26
+ "type": "module",
27
+ "exports": {
28
+ ".": "./index.js",
29
+ "./client": "./client.js",
30
+ "./locale/*.json": "./locale/*.json",
31
+ "./icon": "./icon.svg",
32
+ "./package.json": "./package.json"
33
+ },
34
+ "files": [
35
+ "index.js",
36
+ "client.js",
37
+ "cordis.patch.yml",
38
+ "locale/*.json",
39
+ "icon.svg",
40
+ "README.md",
41
+ "LICENSE"
42
+ ],
43
+ "icon": "./icon.svg",
44
+ "dsh": {
45
+ "manifestVersion": 1,
46
+ "bundle": { "patch": "./cordis.patch.yml" },
47
+ "client": {
48
+ "platform": "web",
49
+ "immediately": true,
50
+ "inject": [
51
+ "@deepseek-ai/dsh-client-locale",
52
+ "@deepseek-ai/dsh-client-ui-layout",
53
+ "@deepseek-ai/dsh-client-ui-sidebar",
54
+ "@deepseek-ai/dsh-client-ui-workspace",
55
+ "@deepseek-ai/dsh-api-session-controller",
56
+ "@deepseek-ai/dsh-api-workspace-controller"
57
+ ]
58
+ }
59
+ },
60
+ "publishConfig": {
61
+ "access": "public"
62
+ }
63
+ }