dsh-marker 0.1.3 → 0.1.4

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/client.js CHANGED
@@ -691,14 +691,27 @@ window.__ModuleLoader__.load({
691
691
  *
692
692
  * The overlay's "new session" target creates a conversation the user is not
693
693
  * looking at; without this the mention chip waits in the queue until they
694
- * find it in the sidebar. Navigation is best-effort: the conversation
695
- * service is duck-typed because its selection API is not part of any
696
- * stable contract, and a build without one keeps the queued delivery — the
697
- * chip lands the moment the user opens the session by hand.
694
+ * find it in the sidebar. Navigation is best-effort: the primary path is
695
+ * the `uiWorkspace` service (its `openSession` is what the workspace
696
+ * browser itself calls); `uiConversation` fallbacks are duck-typed because
697
+ * their selection APIs are not part of any stable contract, and a build
698
+ * with none keeps the queued delivery — the chip lands the moment the user
699
+ * opens the session by hand.
698
700
  * @param {string} sessionId
699
701
  */
700
702
  function navigateToSession(sessionId) {
701
- if (typeof sessionId !== 'string' || sessionId === '') return
703
+ if (typeof sessionId === 'string' && sessionId !== '') {
704
+ try {
705
+ const navigation = window.__DSH_ANNO_CONTEXT__?.get?.('uiWorkspace')
706
+ if (navigation && typeof navigation.openSession === 'function') {
707
+ navigation.openSession(sessionId)
708
+ trace('session-nav')
709
+ return
710
+ }
711
+ } catch {
712
+ // A closed-over service that threw is the same as none.
713
+ }
714
+ }
702
715
  try {
703
716
  const conversation = window.__DSH_ANNO_CONTEXT__?.uiConversation
704
717
  const opener =
package/index.js CHANGED
@@ -76,6 +76,7 @@ export async function apply(ctx, config = {}) {
76
76
  const provisioner = createProvisioner({
77
77
  get: (service) => ctx.get(service),
78
78
  logger,
79
+ catalog,
79
80
  onCreated: ({ sessionId }) => {
80
81
  // Same knock as a queued mention: the page that should show this Session
81
82
  // learns to look for it now, and a missed event costs latency, not state.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-marker",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "Visual annotation bridge: mark text, elements, or regions in Firefox/Zen and hand them to DeepSeek Harness as first-class context",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/src/provision.js CHANGED
@@ -6,11 +6,14 @@
6
6
  * Host can honour, because only it owns `sessionController.create` and the
7
7
  * workspace registry. Three workspace modes:
8
8
  *
9
- * - `default` no location, exactly what the Client's own new-session button does;
9
+ * - `default` the deployment's first-use Workspace ("默认工作区"), joined by
10
+ * its fixed title when the registry has it; a bare no-location
11
+ * create otherwise;
10
12
  * - `existing` one registered workspace by id, the Session joins it;
11
- * - `new` an absolute path (and optional title), registered first — the
12
- * registry creates missing directories recursively and resolves
13
- * idempotently when the directory is already registered.
13
+ * - `new` an absolute path (and optional title), registered first — a
14
+ * missing directory is created here (the registry itself will
15
+ * not), and an already-registered directory resolves
16
+ * idempotently.
14
17
  *
15
18
  * Nothing here is queued. A session that does not exist yet has no outbox
16
19
  * entry to replay, so a failure is answered while the composer is still open
@@ -19,19 +22,30 @@
19
22
  * @module dsh-marker/provision
20
23
  */
21
24
 
25
+ import { mkdir } from 'node:fs/promises'
26
+
22
27
  import { LIMITS, SESSION_CREATE_PATH, validateSessionCreateRequest } from './protocol.js'
23
28
 
24
29
  /** The Client picks its workspace rows per open; a second of staleness is invisible. */
25
30
  const WORKSPACES_TTL_MS = 1_000
26
31
 
32
+ /**
33
+ * Fixed first-use Workspace title (DSH's `DEFAULT_WORKSPACE_DIRECTORY`, on the
34
+ * Documents path). Language-neutral by design, so a title comparison is enough.
35
+ */
36
+ const DEFAULT_WORKSPACE_TITLE = 'default-workspace'
37
+
27
38
  /**
28
39
  * @param {object} options
29
40
  * @param {(name: string) => any} options.get Cordis service lookup for the mounting context.
41
+ * @param {{ list: (options?: object) => Promise<{ items?: any[] }> } | undefined} [options.catalog]
42
+ * Session catalog, when one is mounted; its blank rows are what "reuse the
43
+ * workspace's blank Session" is answered from.
30
44
  * @param {(body: any) => void | Promise<void>} options.onCreated Side effect after one created session (push, focus hints).
31
45
  * @param {{ warn?: (...args: any[]) => void, info?: (...args: any[]) => void }} [options.logger]
32
46
  * @param {() => number} [options.now] Clock, injected by tests.
33
47
  */
34
- export function createProvisioner({ get, onCreated, logger, now = Date.now }) {
48
+ export function createProvisioner({ get, catalog, onCreated, logger, now = Date.now }) {
35
49
  /** @type {{ at: number, items: any[] } | null} */
36
50
  let workspaceCache = null
37
51
 
@@ -74,13 +88,25 @@ export function createProvisioner({ get, onCreated, logger, now = Date.now }) {
74
88
  if (workspace === null) throw new Error('that workspace no longer exists on this host')
75
89
  // `workspaceId` joins the Session to the workspace record; `cwd` is the
76
90
  // standalone alternative and the two are exclusive by contract.
77
- request = { workspaceId: workspace.id }
91
+ request = await requestFor(workspace)
78
92
  } else if (body.mode === 'new') {
79
93
  workspace = await registerWorkspace(String(body.path), typeof body.title === 'string' ? body.title : undefined)
80
- request = { workspaceId: workspace.id }
94
+ request = await requestFor(workspace)
95
+ } else {
96
+ // `default` is the row the Client labels "默认工作区": the deployment's
97
+ // first-use Workspace, recognised by its fixed title. A Session created
98
+ // without it belongs to no Workspace row and never shows in that group,
99
+ // so join it when the registry has it. A registry that cannot be read
100
+ // (missing, or a failed scan) keeps the bare no-location create: this
101
+ // mode has always worked without one.
102
+ try {
103
+ const items = await listWorkspaces()
104
+ workspace = items.find((item) => item.title === DEFAULT_WORKSPACE_TITLE) ?? null
105
+ } catch {
106
+ workspace = null
107
+ }
108
+ if (workspace !== null) request = await requestFor(workspace)
81
109
  }
82
- // `default` sends no location: the controller resolves the deployment's
83
- // default, the same path the Client's own new-session button takes.
84
110
 
85
111
  const value = await controller.create(request)
86
112
  const sessionId = typeof value?.sessionId === 'string' ? value.sessionId : ''
@@ -96,8 +122,50 @@ export function createProvisioner({ get, onCreated, logger, now = Date.now }) {
96
122
  return items.find((item) => item.id === workspaceId) ?? null
97
123
  }
98
124
 
125
+ /**
126
+ * Join the workspace, reusing its blank Session when the catalog can name
127
+ * one: annotations that target "a new conversation here" must append to the
128
+ * same conversation, not mint a sibling per send. The controller owns the
129
+ * final say — it re-checks that the named Session is still blank and still a
130
+ * workspace member before reusing it, so a stale answer here can only cost a
131
+ * fresh Session, never a wrong reuse.
132
+ * @param {{ id: string, title: string, path: string }} workspace
133
+ */
134
+ async function requestFor(workspace) {
135
+ const blank = await findBlankIn(workspace)
136
+ return { workspaceId: workspace.id, ...(blank === undefined ? {} : { sessionId: blank }) }
137
+ }
138
+
139
+ /**
140
+ * The workspace's most recently active blank Session, when the catalog can
141
+ * name one. A failed lookup is a warning, not a failed create.
142
+ * @param {{ id: string, title: string, path: string }} workspace
143
+ * @returns {Promise<string | undefined>}
144
+ */
145
+ async function findBlankIn(workspace) {
146
+ if (catalog === undefined) return undefined
147
+ try {
148
+ const value = await catalog.list({ query: workspace.title, limit: 50 })
149
+ const rows = Array.isArray(value?.items) ? value.items : []
150
+ let best = null
151
+ for (const row of rows) {
152
+ if (row?.workspace?.id !== workspace.id || row.blank !== true) continue
153
+ if (best === null || Number(row.updatedAt ?? 0) > Number(best.updatedAt ?? 0)) best = row
154
+ }
155
+ return best !== null && typeof best.id === 'string' ? best.id : undefined
156
+ } catch (error) {
157
+ logger?.warn?.(`annotations: blank session lookup failed (${messageOf(error)})`)
158
+ return undefined
159
+ }
160
+ }
161
+
99
162
  /**
100
163
  * Register (or idempotently resolve) one workspace over an absolute path.
164
+ *
165
+ * The registry canonicalises through `realpath` and does not create the
166
+ * directory (verified against DSH 0.2.2), while the browser half cannot make
167
+ * one at all — so a missing directory is made here and the registration
168
+ * retried once before the failure is reported.
101
169
  * @param {string} path
102
170
  * @param {string | undefined} title
103
171
  */
@@ -105,14 +173,36 @@ export function createProvisioner({ get, onCreated, logger, now = Date.now }) {
105
173
  const registry = get('workspaceRegistry')
106
174
  if (registry === undefined) throw new Error('the workspace registry is not mounted in this profile')
107
175
  try {
108
- const workspace = await registry.create(path, title)
109
- workspaceCache = null
110
- return { id: workspace.id, title: workspace.title, path: workspace.path }
176
+ return await register(registry, path, title)
111
177
  } catch (error) {
112
- throw new Error(`the workspace could not be created (${messageOf(error)})`)
178
+ const message = messageOf(error)
179
+ if (!/ENOENT/.test(message)) throw new Error(`the workspace could not be created (${message})`)
180
+ try {
181
+ await mkdir(path, { recursive: true })
182
+ } catch (madeError) {
183
+ throw new Error(`the workspace directory could not be created (${messageOf(madeError)})`)
184
+ }
185
+ try {
186
+ return await register(registry, path, title)
187
+ } catch (again) {
188
+ throw new Error(`the workspace could not be created (${messageOf(again)})`)
189
+ }
113
190
  }
114
191
  }
115
192
 
193
+ /**
194
+ * One registration attempt, bare: the ENOENT test in {@link registerWorkspace}
195
+ * must see the registry's own error, not this module's wrapping of it.
196
+ * @param {any} registry
197
+ * @param {string} path
198
+ * @param {string | undefined} title
199
+ */
200
+ async function register(registry, path, title) {
201
+ const workspace = await registry.create(path, title)
202
+ workspaceCache = null
203
+ return { id: workspace.id, title: workspace.title, path: workspace.path }
204
+ }
205
+
116
206
  return { listWorkspaces, createSession }
117
207
  }
118
208