@mengruo/dsh-vision-toolkit 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.
Files changed (80) hide show
  1. package/README.i18n.yaml +2 -2
  2. package/README.md +4 -0
  3. package/README.zh.md +4 -0
  4. package/docs/requirements-traceability/README.i18n.yaml +2 -2
  5. package/docs/requirements-traceability/README.md +1 -1
  6. package/docs/requirements-traceability/README.zh.md +1 -1
  7. package/lib/artifact-access.js +20 -2
  8. package/lib/artifact-access.js.map +1 -1
  9. package/lib/client.js +22 -7
  10. package/lib/client.js.map +1 -1
  11. package/lib/config.js +34 -0
  12. package/lib/config.js.map +1 -1
  13. package/lib/errors.js +25 -2
  14. package/lib/errors.js.map +1 -1
  15. package/lib/evidence-cache.js +2 -1
  16. package/lib/evidence-cache.js.map +1 -1
  17. package/lib/exposure.js +14 -1
  18. package/lib/exposure.js.map +1 -1
  19. package/lib/image-input-variants.js +22 -12
  20. package/lib/image-input-variants.js.map +1 -1
  21. package/lib/index.js +53 -6
  22. package/lib/index.js.map +1 -1
  23. package/lib/paste-images.js +67 -19
  24. package/lib/paste-images.js.map +1 -1
  25. package/lib/paths.js +214 -28
  26. package/lib/paths.js.map +1 -1
  27. package/lib/runtime-manager.js +76 -10
  28. package/lib/runtime-manager.js.map +1 -1
  29. package/lib/runtime.js +80 -12
  30. package/lib/runtime.js.map +1 -1
  31. package/lib/storage-history.js +154 -0
  32. package/lib/storage-history.js.map +1 -0
  33. package/lib/types/artifact-access.d.ts.map +1 -1
  34. package/lib/types/client/index.d.ts +6 -1
  35. package/lib/types/client/index.d.ts.map +1 -1
  36. package/lib/types/client/paste-images.d.ts +2 -0
  37. package/lib/types/client/paste-images.d.ts.map +1 -1
  38. package/lib/types/config.d.ts +22 -0
  39. package/lib/types/config.d.ts.map +1 -1
  40. package/lib/types/errors.d.ts +18 -2
  41. package/lib/types/errors.d.ts.map +1 -1
  42. package/lib/types/evidence-cache.d.ts +1 -1
  43. package/lib/types/evidence-cache.d.ts.map +1 -1
  44. package/lib/types/exposure.d.ts.map +1 -1
  45. package/lib/types/image-input-variants.d.ts +5 -3
  46. package/lib/types/image-input-variants.d.ts.map +1 -1
  47. package/lib/types/index.d.ts.map +1 -1
  48. package/lib/types/paste-images.d.ts +12 -4
  49. package/lib/types/paste-images.d.ts.map +1 -1
  50. package/lib/types/paths.d.ts +31 -5
  51. package/lib/types/paths.d.ts.map +1 -1
  52. package/lib/types/runtime-manager.d.ts +28 -4
  53. package/lib/types/runtime-manager.d.ts.map +1 -1
  54. package/lib/types/runtime.d.ts +19 -1
  55. package/lib/types/runtime.d.ts.map +1 -1
  56. package/lib/types/storage-history.d.ts +63 -0
  57. package/lib/types/storage-history.d.ts.map +1 -0
  58. package/lib/types/upstream.d.ts.map +1 -1
  59. package/lib/types/web.d.ts.map +1 -1
  60. package/lib/upstream.js +31 -8
  61. package/lib/upstream.js.map +1 -1
  62. package/lib/web.js +9 -3
  63. package/lib/web.js.map +1 -1
  64. package/package.json +1 -1
  65. package/src/artifact-access.ts +22 -2
  66. package/src/client/index.tsx +18 -3
  67. package/src/client/paste-images.tsx +14 -4
  68. package/src/config.ts +61 -0
  69. package/src/errors.ts +25 -2
  70. package/src/evidence-cache.ts +2 -1
  71. package/src/exposure.ts +16 -2
  72. package/src/image-input-variants.ts +21 -6
  73. package/src/index.ts +65 -6
  74. package/src/paste-images.ts +81 -19
  75. package/src/paths.ts +249 -28
  76. package/src/runtime-manager.ts +93 -10
  77. package/src/runtime.ts +79 -11
  78. package/src/storage-history.ts +172 -0
  79. package/src/upstream.ts +32 -7
  80. package/src/web.ts +9 -2
@@ -58,6 +58,8 @@ interface PasteOccurrence {
58
58
  source: string
59
59
  ref: string
60
60
  offset: number
61
+ /** DSH rc.8+ stores the full @label text; older releases used one placeholder. */
62
+ length?: number
61
63
  label: string
62
64
  }
63
65
 
@@ -166,6 +168,10 @@ function pasteLabel(file: File, index: number): string {
166
168
  return file.name.trim() || `clipboard-image-${index + 1}`
167
169
  }
168
170
 
171
+ function occurrenceEnd(occurrence: PasteOccurrence): number {
172
+ return occurrence.offset + (occurrence.length ?? 1)
173
+ }
174
+
169
175
  /** Owns browser File objects until DSH serializes the corresponding text references. */
170
176
  export class PasteImageController {
171
177
  private readonly records = new Map<string, PasteRecord>()
@@ -241,6 +247,7 @@ export class PasteImageController {
241
247
  if (before !== '' && !/\s$/u.test(before)) cursor = this.insertText(input, ' ', cursor)
242
248
  for (const [index, file] of files.entries()) {
243
249
  const ref = id()
250
+ const label = pasteLabel(file, index)
244
251
  const record: PasteRecord = { ref, file, batch, status: 'ready' }
245
252
  batch.records.push(record)
246
253
  this.records.set(ref, record)
@@ -248,11 +255,14 @@ export class PasteImageController {
248
255
  const accepted = input.insertReference({
249
256
  source: SOURCE,
250
257
  ref,
251
- label: pasteLabel(file, index),
252
- clipboardText: `[pasted image: ${pasteLabel(file, index)}]`,
258
+ label,
259
+ clipboardText: `[pasted image: ${label}]`,
253
260
  }, { start: cursor, end: cursor, draftRev: snapshot.draftRev })
254
261
  if (!accepted) throw new Error('The composer changed before pasted images could be inserted')
255
- cursor += 1
262
+ const inserted = input.state.getSnapshot().occurrences.find(occurrence =>
263
+ occurrence.source === SOURCE && occurrence.ref === ref)
264
+ if (inserted === undefined) throw new Error('The pasted image reference was not present after insertion')
265
+ cursor = occurrenceEnd(inserted)
256
266
  const hasNext = index + 1 < files.length
257
267
  const suffix = input.state.getSnapshot().draft.slice(cursor)
258
268
  if (hasNext || (suffix !== '' && !/^\s/u.test(suffix))) cursor = this.insertText(input, ' ', cursor)
@@ -594,7 +604,7 @@ export class PasteImageController {
594
604
  insertText: (text: string, span: { start: number; end: number; draftRev: number }) => boolean
595
605
  }).insertText('', {
596
606
  start: current.offset,
597
- end: current.offset + 1,
607
+ end: occurrenceEnd(current),
598
608
  draftRev: snapshot.draftRev,
599
609
  })
600
610
  if (!accepted) return
package/src/config.ts CHANGED
@@ -131,6 +131,14 @@ export interface VisionToolkitConfig {
131
131
  /** Optional Python 3.11+ bootstrap/interpreter override. */
132
132
  python?: string
133
133
  }
134
+ /**
135
+ * Optional shared storage root. When set, every workspace gets an isolated,
136
+ * automatically generated child directory below this root instead of writing
137
+ * `.dsh-vision-toolkit` into the workspace.
138
+ */
139
+ storageDir?: string
140
+ /** Internal read-only history used to keep persisted paths valid after storage moves. */
141
+ storageHistory?: string[]
134
142
  /** Extra directories (besides the workspace) inputs may come from. */
135
143
  allowedDirs?: string[]
136
144
  /**
@@ -204,6 +212,8 @@ export const Config: Schema<VisionToolkitConfig> = z.object({
204
212
  agentVisionToolkitPath: z.string(),
205
213
  python: z.string(),
206
214
  }),
215
+ storageDir: z.string(),
216
+ storageHistory: z.array(z.string()).default([]),
207
217
  allowedDirs: z.array(z.string()).default([]),
208
218
  imageInputVariants: z.object({
209
219
  enabled: z.boolean().default(true),
@@ -257,6 +267,8 @@ export interface ResolvedVisionToolkitConfig {
257
267
  agentVisionToolkitPath?: string
258
268
  python?: string
259
269
  }
270
+ storageDir?: string
271
+ storageHistory: string[]
260
272
  allowedDirs: string[]
261
273
  imageInputVariants: {
262
274
  enabled: boolean
@@ -455,6 +467,10 @@ export function resolveConfig(config: VisionToolkitConfig = {}): ResolvedVisionT
455
467
  if (python !== undefined && python.length === 0) {
456
468
  throw new VisionToolkitError('config', 'runtime.python must not be empty')
457
469
  }
470
+ const storageDir = config.storageDir?.trim()
471
+ const storageHistory = [...new Set((config.storageHistory ?? [])
472
+ .map(dir => dir.trim())
473
+ .filter(dir => dir.length > 0 && dir !== storageDir))]
458
474
  const allowedDirs = (config.allowedDirs ?? []).map(dir => dir.trim()).filter(dir => dir.length > 0)
459
475
  const imageInputVariants = config.imageInputVariants ?? {}
460
476
  const variantProviders = (imageInputVariants.providers ?? [])
@@ -492,6 +508,8 @@ export function resolveConfig(config: VisionToolkitConfig = {}): ResolvedVisionT
492
508
  ...(toolkitPath !== undefined ? { agentVisionToolkitPath: toolkitPath } : {}),
493
509
  ...(python !== undefined ? { python } : {}),
494
510
  },
511
+ ...(storageDir === undefined || storageDir.length === 0 ? {} : { storageDir }),
512
+ storageHistory,
495
513
  allowedDirs,
496
514
  imageInputVariants: {
497
515
  enabled: imageInputVariants.enabled ?? true,
@@ -502,6 +520,49 @@ export function resolveConfig(config: VisionToolkitConfig = {}): ResolvedVisionT
502
520
  }
503
521
  }
504
522
 
523
+ /** Merge prior storage roots into the next resolved generation's read-only history. */
524
+ export function retainedStorageHistory(
525
+ next: VisionToolkitConfig,
526
+ previous: VisionToolkitConfig,
527
+ ): string[] {
528
+ const resolvedNext = resolveConfig(next)
529
+ const resolvedPrevious = resolveConfig(previous)
530
+ return [...new Set([
531
+ ...resolvedPrevious.storageHistory,
532
+ ...resolvedNext.storageHistory,
533
+ ...(resolvedPrevious.storageDir === undefined ? [] : [resolvedPrevious.storageDir]),
534
+ ])].filter(storageDir => storageDir !== resolvedNext.storageDir)
535
+ }
536
+
537
+ export interface WatchedSettingsGeneration {
538
+ /** Configuration to activate now; omitted after a successful history writeback. */
539
+ config?: VisionToolkitConfig
540
+ /** Whether the derived history still needs plugin-owned durable persistence. */
541
+ requiresDurableStorageHistory?: boolean
542
+ /** Non-fatal internal-history persistence error. */
543
+ persistenceError?: unknown
544
+ }
545
+
546
+ /** Prepare one live Settings generation without letting internal history writeback block activation. */
547
+ export async function prepareWatchedSettingsGeneration(
548
+ next: VisionToolkitConfig,
549
+ previous: VisionToolkitConfig,
550
+ writable: boolean,
551
+ persistStorageHistory: (storageHistory: string[]) => Promise<void>,
552
+ ): Promise<WatchedSettingsGeneration> {
553
+ const storageHistory = retainedStorageHistory(next, previous)
554
+ if (JSON.stringify(storageHistory) === JSON.stringify(resolveConfig(next).storageHistory)) return { config: next }
555
+
556
+ const config = { ...next, storageHistory }
557
+ if (!writable) return { config, requiresDurableStorageHistory: true }
558
+ try {
559
+ await persistStorageHistory(storageHistory)
560
+ return {}
561
+ } catch (persistenceError) {
562
+ return { config, requiresDurableStorageHistory: true, persistenceError }
563
+ }
564
+ }
565
+
505
566
  /** Whether a resolved provider should use the bundled public key instead of DSH credentials. */
506
567
  export function isBuiltInFreeVisionProvider(provider: ResolvedVisionToolkitConfig['provider']): boolean {
507
568
  return String(provider.credential) === BUILT_IN_FREE_VISION_CREDENTIAL
package/src/errors.ts CHANGED
@@ -5,18 +5,41 @@
5
5
  * @module dsh-vision-toolkit/errors
6
6
  */
7
7
 
8
- /** Discriminant tag for every Vision Toolkit failure. */
8
+ /**
9
+ * Discriminant tag for every Vision Toolkit failure.
10
+ *
11
+ * The remote vision-provider failures are split by a machine-routable taxonomy
12
+ * so the failover loop can decide, per error, whether to retry the SAME
13
+ * provider or advance to the next one:
14
+ *
15
+ * - `auth` 401/403 — credential is wrong or unauthorized. Never retry.
16
+ * - `quota` 402 — account out of quota/unpaid. Never retry.
17
+ * - `rate_limit` 429 — throttled. Park and revisit, never retry in place.
18
+ * - `server` 5xx — transient provider fault. Worth a same-provider retry.
19
+ * - `network` connection refused / DNS / socket — transient. Worth a retry.
20
+ * - `region` provider unavailable in this region. Never retry.
21
+ * - `tos` rejected by content/safety policy. Never retry.
22
+ * - `invalid_request` 400/404/422 — bad request or unknown model. Never retry.
23
+ * - `service` fallback for unclassifiable remote failures. Never retry.
24
+ */
9
25
  export const VISION_TOOLKIT_ERROR_CODES = [
10
26
  'config',
11
27
  'input',
12
28
  'capacity',
29
+ 'auth',
30
+ 'quota',
31
+ 'rate_limit',
32
+ 'server',
33
+ 'network',
34
+ 'region',
35
+ 'tos',
36
+ 'invalid_request',
13
37
  'service',
14
38
  'runtime',
15
39
  'output',
16
40
  'timeout',
17
41
  'cancelled',
18
42
  'path',
19
- 'rate_limit',
20
43
  ] as const
21
44
 
22
45
  /** Stable machine-readable error category. */
@@ -73,7 +73,7 @@ function hash(value: string): string {
73
73
  return createHash('sha256').update(value).digest('hex')
74
74
  }
75
75
 
76
- /** Fingerprint every runtime setting that can change the generated description. */
76
+ /** Fingerprint every runtime setting that can change generated evidence or its embedded path. */
77
77
  export function evidenceRuntimeFingerprint(
78
78
  config: ResolvedVisionToolkitConfig,
79
79
  credentialSha256?: string,
@@ -115,6 +115,7 @@ export function evidenceRuntimeFingerprint(
115
115
  maxImageBytes: config.maxImageBytes,
116
116
  maxImagePixels: config.maxImagePixels,
117
117
  runtime: config.runtime,
118
+ storageDir: config.storageDir ?? null,
118
119
  }))
119
120
  }
120
121
 
package/src/exposure.ts CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
  import type { Agent } from '@deepseek-ai/dsh-agent'
10
10
  import type { ContentBlock } from '@deepseek-ai/dsh-llm'
11
- import type { Session } from '@deepseek-ai/dsh-session'
11
+ import type { Session, SessionEvent } from '@deepseek-ai/dsh-session'
12
12
  import { defineTool, type ToolDefinition } from '@deepseek-ai/dsh-tools'
13
13
  import type { Context } from '@deepseek-ai/cordis'
14
14
  import { VISION_SKILLS_CONTENT, VISION_SKILLS_NAME } from './skill.ts'
@@ -78,10 +78,24 @@ function isBundledSkillResult(value: unknown): boolean {
78
78
  && isBundledSkillContent(value.content)
79
79
  }
80
80
 
81
+ /**
82
+ * Iterate a Session's durable event log across runtime lines. dsh 0.1.2-alpha
83
+ * replaced the `events` getter with `snapshotEvents()` (session-log-read-intent):
84
+ * iterating the removed `session.events` throws `session.events is not iterable`
85
+ * and takes down `agent/created` → session restore. Prefer the snapshot accessor
86
+ * and fall back to the legacy rc-line `events` getter so both lines work.
87
+ */
88
+ function sessionEventLog(session: Session): readonly SessionEvent[] {
89
+ const snapshot = (session as { snapshotEvents?: () => readonly SessionEvent[] }).snapshotEvents
90
+ return typeof snapshot === 'function'
91
+ ? snapshot.call(session)
92
+ : (session as { events: readonly SessionEvent[] }).events
93
+ }
94
+
81
95
  /** Whether durable history proves that this Session loaded the bundled Skill. */
82
96
  function hasLoadedVisionSkill(session: Session): boolean {
83
97
  const nativeCalls = new Set<string>()
84
- for (const event of session.events) {
98
+ for (const event of sessionEventLog(session)) {
85
99
  if (event.type === 'user/message') {
86
100
  const source = event.data.source
87
101
  if (source.kind === 'skill-invocation'
@@ -142,18 +142,21 @@ async function materializeImage(
142
142
  data: Uint8Array,
143
143
  extension: string,
144
144
  sessionId: string | undefined,
145
+ storageDir: string | undefined,
145
146
  ): Promise<MaterializedImage> {
146
147
  const session = sessionId === undefined ? undefined : ctx.sessions.get(sessionId as never)
147
148
  const cwd = session?.header.cwd
148
149
  if (sessionId !== undefined && cwd !== undefined && isAbsolute(cwd)) {
149
- const root = await sessionPasteRoot(ctx, sessionId)
150
+ const root = await sessionPasteRoot(ctx, sessionId, storageDir)
150
151
  const identity = createHash('sha256')
151
152
  .update(`${sessionId}\u0000${String(block.attachment.attachmentId)}`)
152
153
  .digest('hex')
153
154
  .slice(0, 32)
154
- const file = join(root.visibleRoot, `attachment-${identity}${extension}`)
155
+ const filename = `attachment-${identity}${extension}`
156
+ const writePath = join(root.writeRoot, filename)
157
+ const file = join(root.visibleRoot, filename)
155
158
  try {
156
- await writeFile(file, Buffer.from(data), { mode: 0o600, flag: 'wx' })
159
+ await writeFile(writePath, Buffer.from(data), { mode: 0o600, flag: 'wx' })
157
160
  } catch (error) {
158
161
  if (!(error instanceof Error && 'code' in error && error.code === 'EEXIST')) throw error
159
162
  }
@@ -357,6 +360,7 @@ async function readImageBlock(
357
360
  block: ImageBlock,
358
361
  query: string,
359
362
  sessionId?: string,
363
+ storageDir?: string,
360
364
  ): Promise<ContentBlock> {
361
365
  const attachments = ctx.get('attachments')
362
366
  const current = runtime()
@@ -371,7 +375,7 @@ async function readImageBlock(
371
375
  let pathEvidence = ''
372
376
  try {
373
377
  const stored = await attachments.readImage(block.attachment)
374
- const materialized = await materializeImage(ctx, block, stored.data, extension, sessionId)
378
+ const materialized = await materializeImage(ctx, block, stored.data, extension, sessionId, storageDir)
375
379
  temporaryDirectory = materialized.temporaryDirectory
376
380
  if (materialized.persistent) pathEvidence = imagePathEvidence(materialized.file)
377
381
 
@@ -414,6 +418,7 @@ async function readImageBlock(
414
418
  * @param signal - the caller's cancellation for this conversion pass.
415
419
  * @param sessionId - the live Session identity, when available.
416
420
  * @param runtimeHash - stable fingerprint of the vision provider and evidence runtime.
421
+ * @param storageDir - optional shared plugin storage root.
417
422
  * @returns the rewritten message list.
418
423
  */
419
424
  export async function convertImagesToEvidence(
@@ -424,6 +429,7 @@ export async function convertImagesToEvidence(
424
429
  signal?: AbortSignal,
425
430
  sessionId?: string,
426
431
  runtimeHash = 'process-only-runtime',
432
+ storageDir?: string,
427
433
  ): Promise<Message[]> {
428
434
  const session = sessionId === undefined ? undefined : ctx.sessions.get(sessionId as never)
429
435
  const sessionIdentity = session === undefined
@@ -480,7 +486,7 @@ export async function convertImagesToEvidence(
480
486
  prompt: query,
481
487
  runtimeHash,
482
488
  }), () =>
483
- readImageBlock(ctx, runtime, block, query, sessionId)),
489
+ readImageBlock(ctx, runtime, block, query, sessionId, storageDir)),
484
490
  signal,
485
491
  ),
486
492
  signal,
@@ -517,6 +523,7 @@ export class ImageInputVariantAdapter extends LlmAdapter {
517
523
  private readonly runtime: () => VisionToolkitRuntime | undefined,
518
524
  private readonly cache: EvidenceCache,
519
525
  private readonly hidden: () => boolean = () => false,
526
+ private readonly startupStorageDirectory: () => string | undefined = () => undefined,
520
527
  ) {
521
528
  super()
522
529
  }
@@ -589,6 +596,7 @@ export class ImageInputVariantAdapter extends LlmAdapter {
589
596
 
590
597
  override async *stream(options: GenerateOptions): AsyncGenerator<StreamChunk> {
591
598
  const current = this.runtime()
599
+ const storageDir = current === undefined ? this.startupStorageDirectory() : current.storageDirectory
592
600
  let captured: CapturedEvidenceRuntime | undefined
593
601
  if (current !== undefined && options.messages.some(message => contentHasImage(message.content))) {
594
602
  try {
@@ -610,7 +618,12 @@ export class ImageInputVariantAdapter extends LlmAdapter {
610
618
  options.messages,
611
619
  options.signal,
612
620
  options.sessionId === undefined ? undefined : String(options.sessionId),
613
- captured?.evidenceFingerprint ?? 'process-only-runtime',
621
+ captured?.evidenceFingerprint ?? createHash('sha256')
622
+ .update('process-only-runtime')
623
+ .update('\0')
624
+ .update(storageDir ?? '')
625
+ .digest('hex'),
626
+ storageDir,
614
627
  )
615
628
  // Delegate through the host service under the upstream route: the variant
616
629
  // is a wire-only facade, and the upstream route owns retry and replay.
@@ -846,6 +859,7 @@ export function installImageInputVariants(
846
859
  ctx: Context,
847
860
  getConfig: () => ResolvedVisionToolkitConfig,
848
861
  getRuntime: () => VisionToolkitRuntime | undefined,
862
+ getStartupStorageDirectory: () => string | undefined = () => undefined,
849
863
  ): { dispose: () => void; reconcile: () => void } {
850
864
  const evidenceStore = new SessionEvidenceStore(ctx)
851
865
  const evidenceCache = new EvidenceCache(EVIDENCE_CACHE_LIMIT, evidenceStore)
@@ -991,6 +1005,7 @@ export function installImageInputVariants(
991
1005
  getRuntime,
992
1006
  evidenceCache,
993
1007
  () => getConfig().imageInputVariants.hidden,
1008
+ getStartupStorageDirectory,
994
1009
  ),
995
1010
  )
996
1011
  registrations.set(upstream, { dispose, retryPolicyKey: upstreamRetryPolicyKey })
package/src/index.ts CHANGED
@@ -18,13 +18,16 @@ import { ArtifactAccessController, prepareArtifactAccessKey } from './artifact-a
18
18
  import {
19
19
  Config,
20
20
  VISION_TOOLKIT_SETTINGS_NAMESPACE,
21
+ prepareWatchedSettingsGeneration,
21
22
  resolveConfig,
23
+ type ResolvedVisionToolkitConfig,
22
24
  type VisionToolkitConfig,
23
25
  } from './config.ts'
24
26
  import { VisionToolExposure } from './exposure.ts'
25
27
  import { createPasteTakeoverResolver, installImageInputVariants } from './image-input-variants.ts'
26
28
  import { VisionToolkitRuntimeManager } from './runtime-manager.ts'
27
29
  import { VISION_SKILLS_SKILL } from './skill.ts'
30
+ import { StorageHistoryStore } from './storage-history.ts'
28
31
  import { createVisionTools } from './tools.ts'
29
32
  import { PLUGIN_VERSION } from './version.ts'
30
33
  import { installVisionToolkitWeb, VisionToolkitWebBackend } from './web.ts'
@@ -51,8 +54,33 @@ export async function apply(ctx: Context, config: VisionToolkitConfig = {}): Pro
51
54
  const artifacts = new ArtifactAccessController(await prepareArtifactAccessKey())
52
55
  const lifecycle = new AbortController()
53
56
  const disposers: Array<() => void> = []
57
+ const storageHistory = new StorageHistoryStore(ctx)
58
+ disposers.push(() => { storageHistory.dispose() })
59
+ let storageHistoryWarningReported = false
54
60
  let operationalDisposers: { activationTool: () => void; exposure: () => void; skill: () => void } | undefined
55
61
 
62
+ const persistStorageHistory = async (candidate: VisionToolkitConfig, required: boolean): Promise<void> => {
63
+ try {
64
+ const persisted = await storageHistory.persist(candidate)
65
+ if (persisted) return
66
+ const error = new Error(
67
+ 'configured storage history requires @deepseek-ai/dsh-storage-domain when Settings cannot persist it',
68
+ )
69
+ if (required) throw error
70
+ if (!storageHistoryWarningReported) {
71
+ storageHistoryWarningReported = true
72
+ ctx.logger.warn('dsh-vision-toolkit: %s', error.message)
73
+ }
74
+ } catch (error) {
75
+ if (required) throw error
76
+ if (!storageHistoryWarningReported) {
77
+ storageHistoryWarningReported = true
78
+ const message = error instanceof Error ? error.message : String(error)
79
+ ctx.logger.warn('dsh-vision-toolkit: configured storage history was not persisted. %s', message)
80
+ }
81
+ }
82
+ }
83
+
56
84
  const ensureOperational = (): void => {
57
85
  if (!manager.ready || operationalDisposers !== undefined) return
58
86
  const exposure = new VisionToolExposure(ctx, () => createVisionTools(
@@ -84,10 +112,16 @@ export async function apply(ctx: Context, config: VisionToolkitConfig = {}): Pro
84
112
  }
85
113
  }
86
114
 
115
+ const initialConfig = await storageHistory.restore(settings.get())
87
116
  try {
88
- await manager.initialize(settings.get())
117
+ await manager.initialize(initialConfig, candidate => persistStorageHistory(candidate.config, false))
89
118
  ensureOperational()
90
119
  } catch (error) {
120
+ const resolvedInitial = resolveConfig(initialConfig)
121
+ if (resolvedInitial.storageDir === undefined
122
+ || manager.validatedStorageDirectory() === resolvedInitial.storageDir) {
123
+ await persistStorageHistory(initialConfig, false)
124
+ }
91
125
  const message = error instanceof Error ? error.message : String(error)
92
126
  ctx.logger.error(
93
127
  'dsh-vision-toolkit %s: runtime not ready; the vision-skills skill, activation bootstrap, and Agent-scoped visual tools are NOT registered. Settings remain available for repair. %s',
@@ -97,29 +131,54 @@ export async function apply(ctx: Context, config: VisionToolkitConfig = {}): Pro
97
131
  }
98
132
 
99
133
  const backend = new VisionToolkitWebBackend(ctx, manager, artifacts, ensureOperational)
134
+ const currentConfig = (): ResolvedVisionToolkitConfig => manager.ready
135
+ ? manager.currentConfig()
136
+ : resolveConfig(settings.get())
100
137
  const pastedImages = new PastedImageBackend(ctx, {
101
138
  maxUploadBytes: () => MAX_PASTE_IMAGE_BYTES,
139
+ storageGeneration: () => manager.storageGeneration(),
102
140
  })
103
141
  // Image-input variants register asynchronously once eligible routes exist;
104
142
  // the runtime getter stays lazy so variants appear even when the runtime
105
143
  // becomes ready after the first sweep.
106
144
  const variants = installImageInputVariants(
107
145
  ctx,
108
- () => resolveConfig(settings.get()),
146
+ currentConfig,
109
147
  () => manager.ready ? manager.current() : undefined,
148
+ () => manager.validatedStorageDirectory(),
110
149
  )
111
150
  installVisionToolkitWeb(
112
151
  ctx,
113
152
  backend,
114
153
  artifacts,
115
154
  pastedImages,
116
- createPasteTakeoverResolver(ctx, () => resolveConfig(settings.get())),
117
- () => ({ hidden: resolveConfig(settings.get()).imageInputVariants.hidden }),
155
+ createPasteTakeoverResolver(ctx, currentConfig),
156
+ () => ({ hidden: currentConfig().imageInputVariants.hidden }),
118
157
  )
119
158
  disposers.push(variants.dispose)
120
- disposers.push(settings.watch(async (next) => {
159
+ disposers.push(settings.watch(async (next, previous) => {
121
160
  try {
122
- await manager.reconfigure(next)
161
+ const prepared = await prepareWatchedSettingsGeneration(
162
+ next,
163
+ previous,
164
+ ctx.settings.writable,
165
+ storageHistory => settings.update({ storageHistory }),
166
+ )
167
+ if (prepared.persistenceError !== undefined) {
168
+ const message = prepared.persistenceError instanceof Error
169
+ ? prepared.persistenceError.message
170
+ : String(prepared.persistenceError)
171
+ ctx.logger.warn('dsh-vision-toolkit: activating Settings without persisting internal storage history. %s', message)
172
+ }
173
+ if (prepared.config === undefined) return
174
+ const candidate = await storageHistory.restore(prepared.config)
175
+ await manager.reconfigure(
176
+ candidate,
177
+ generation => persistStorageHistory(
178
+ generation.config,
179
+ prepared.requiresDurableStorageHistory === true,
180
+ ),
181
+ )
123
182
  ensureOperational()
124
183
  variants.reconcile()
125
184
  } catch (error) {
@@ -1,11 +1,13 @@
1
- /** Workspace-local storage for images pasted into the DSH Web composer. */
1
+ /** Plugin-managed storage for images pasted into the DSH Web composer. */
2
2
 
3
3
  import { createHash, randomUUID } from 'node:crypto'
4
- import { lstat, mkdir, open, realpath, rename, rm } from 'node:fs/promises'
4
+ import { constants as fsConstants } from 'node:fs'
5
+ import { copyFile, lstat, mkdir, open, realpath, rename, rm } from 'node:fs/promises'
5
6
  import type { IncomingMessage, ServerResponse } from 'node:http'
6
7
  import { basename, extname, isAbsolute, join, relative, resolve, sep } from 'node:path'
7
8
  import type { Context } from '@deepseek-ai/cordis'
8
9
  import type {} from '@deepseek-ai/dsh-session'
10
+ import { resolveWorkspaceStorage } from './paths.ts'
9
11
  import { sameOriginPost } from './web-request.ts'
10
12
 
11
13
  /** Exact route used by the browser paste integration. */
@@ -51,6 +53,7 @@ export interface PasteVerdict {
51
53
  }
52
54
 
53
55
  const MAX_NAME_BYTES = 180
56
+ const MAX_STORAGE_GENERATION_RETRIES = 4
54
57
 
55
58
  /**
56
59
  * Hard per-image upload ceiling for pastes. Files between the configured
@@ -151,7 +154,7 @@ export function safePastedImageName(raw: string, mediaType: string): string {
151
154
  export function ensurePathInside(root: string, target: string): void {
152
155
  const rel = relative(root, target)
153
156
  if (rel !== '' && (rel === '..' || rel.startsWith(`..${sep}`) || isAbsolute(rel))) {
154
- throw new Error(`resolved pasted-image path escapes its workspace root: ${target}`)
157
+ throw new Error(`resolved pasted-image path escapes its managed root: ${target}`)
155
158
  }
156
159
  }
157
160
 
@@ -168,7 +171,7 @@ async function ensureManagedDirectory(workspace: string, path: string): Promise<
168
171
  }
169
172
  const entry = await lstat(path)
170
173
  if (entry.isSymbolicLink()) {
171
- throw new Error(`resolved pasted-image path escapes its workspace root: symbolic link ${path}`)
174
+ throw new Error(`resolved pasted-image path escapes its managed root: symbolic link ${path}`)
172
175
  }
173
176
  if (!entry.isDirectory()) throw new Error(`pasted-image path is not a directory: ${path}`)
174
177
  const canonical = await realpath(path)
@@ -178,29 +181,33 @@ async function ensureManagedDirectory(workspace: string, path: string): Promise<
178
181
 
179
182
  /**
180
183
  * Resolve the managed per-session image directory used by both browser pastes
181
- * and native attachment bridging. Keeping both flows under the same workspace
182
- * root makes the resulting absolute path valid for the model's visual tools.
184
+ * and native attachment bridging. A configured shared storage root receives a
185
+ * stable workspace-specific child, keeping projects isolated without writing
186
+ * plugin files into the project directory.
183
187
  */
184
- export async function sessionPasteRoot(ctx: Context, sessionId: string): Promise<PasteRoot> {
188
+ export async function sessionPasteRoot(
189
+ ctx: Context,
190
+ sessionId: string,
191
+ storageDir?: string,
192
+ ): Promise<PasteRoot> {
185
193
  const session = ctx.sessions.get(sessionId as never)
186
194
  if (session === undefined) throw new Error(`live Session not found: ${sessionId}`)
187
195
  const cwd = session.header.cwd
188
196
  if (cwd === undefined || !isAbsolute(cwd)) throw new Error(`Session has no absolute workspace: ${sessionId}`)
189
197
 
190
- const visibleWorkspace = resolve(cwd)
191
- const workspace = await realpath(visibleWorkspace)
192
- const pluginRoot = join(visibleWorkspace, '.dsh-vision-toolkit')
193
- await ensureManagedDirectory(workspace, pluginRoot)
198
+ const storage = await resolveWorkspaceStorage(resolve(cwd), storageDir)
199
+ const pluginRoot = storage.root
194
200
  const temporaryRoot = join(pluginRoot, 'tmp')
195
- await ensureManagedDirectory(workspace, temporaryRoot)
201
+ await ensureManagedDirectory(pluginRoot, temporaryRoot)
196
202
  const requestedRoot = join(temporaryRoot, 'pasted-images')
197
- const root = await ensureManagedDirectory(workspace, requestedRoot)
203
+ const root = await ensureManagedDirectory(temporaryRoot, requestedRoot)
204
+ const visibleRoot = join(storage.visibleRoot, 'tmp', 'pasted-images')
198
205
 
199
206
  const sessionKey = createHash('sha256').update(sessionId).digest('hex').slice(0, 20)
200
207
  const requestedSessionRoot = join(requestedRoot, sessionKey)
201
208
  const sessionRoot = await ensureManagedDirectory(root, requestedSessionRoot)
202
209
  ensurePathInside(root, sessionRoot)
203
- return { writeRoot: sessionRoot, visibleRoot: requestedSessionRoot }
210
+ return { writeRoot: sessionRoot, visibleRoot: join(visibleRoot, sessionKey) }
204
211
  }
205
212
 
206
213
  async function writeImage(
@@ -240,9 +247,38 @@ async function writeImage(
240
247
  }
241
248
  }
242
249
 
250
+ async function copyImage(
251
+ source: string,
252
+ directory: string,
253
+ filename: string,
254
+ ): Promise<string> {
255
+ const id = randomUUID()
256
+ const finalPath = join(directory, `${id}-${filename}`)
257
+ const stagingPath = join(directory, `.${id}.partial`)
258
+ ensurePathInside(directory, finalPath)
259
+ ensurePathInside(directory, stagingPath)
260
+ try {
261
+ await copyFile(source, stagingPath, fsConstants.COPYFILE_EXCL)
262
+ await rename(stagingPath, finalPath)
263
+ return finalPath
264
+ } catch (error) {
265
+ await rm(stagingPath, { force: true }).catch(() => {})
266
+ throw error
267
+ }
268
+ }
269
+
270
+ export interface PasteStorageGeneration {
271
+ generation: number
272
+ storageDir?: string
273
+ }
274
+
275
+ class PasteStorageChangedError extends Error {}
276
+
243
277
  /** Runtime limit face kept separate for focused backend tests. */
244
278
  export interface PasteImageRuntime {
245
279
  maxUploadBytes(): number
280
+ storageDirectory?(): string | undefined
281
+ storageGeneration?(): PasteStorageGeneration
246
282
  }
247
283
 
248
284
  /** Same-origin, live-Session-bound image upload endpoint. */
@@ -252,6 +288,16 @@ export class PastedImageBackend {
252
288
  private readonly runtime: PasteImageRuntime,
253
289
  ) {}
254
290
 
291
+ private storageGeneration(): PasteStorageGeneration {
292
+ const current = this.runtime.storageGeneration?.()
293
+ if (current !== undefined) return current
294
+ const storageDir = this.runtime.storageDirectory?.()
295
+ return {
296
+ generation: 0,
297
+ ...(storageDir === undefined ? {} : { storageDir }),
298
+ }
299
+ }
300
+
255
301
  async handle(req: IncomingMessage, res: ServerResponse): Promise<void> {
256
302
  if (req.method !== 'POST') {
257
303
  res.setHeader('Allow', 'POST')
@@ -263,6 +309,7 @@ export class PastedImageBackend {
263
309
  return
264
310
  }
265
311
 
312
+ let managedPath: string | undefined
266
313
  try {
267
314
  const url = new URL(req.url ?? PASTE_IMAGES_ROUTE, 'http://dsh.internal')
268
315
  const sessionId = singleQuery(url, 'sessionId')
@@ -273,12 +320,27 @@ export class PastedImageBackend {
273
320
  if (contentLength !== undefined && Number(contentLength) !== size) {
274
321
  throw new TypeError('Content-Length does not match the declared size')
275
322
  }
276
- const directory = await sessionPasteRoot(this.ctx, sessionId)
277
- const writtenPath = await writeImage(req, directory.writeRoot, filename, size, this.runtime.maxUploadBytes())
278
- const absolutePath = join(directory.visibleRoot, basename(writtenPath))
279
- responseJson(res, 201, { ok: true, value: { absolutePath, filename, bytes: size } })
323
+ let storage = this.storageGeneration()
324
+ let directory = await sessionPasteRoot(this.ctx, sessionId, storage.storageDir)
325
+ managedPath = await writeImage(req, directory.writeRoot, filename, size, this.runtime.maxUploadBytes())
326
+ for (let attempt = 0; attempt < MAX_STORAGE_GENERATION_RETRIES; attempt += 1) {
327
+ const current = this.storageGeneration()
328
+ if (current.generation === storage.generation && current.storageDir === storage.storageDir) {
329
+ const absolutePath = join(directory.visibleRoot, basename(managedPath))
330
+ responseJson(res, 201, { ok: true, value: { absolutePath, filename, bytes: size } })
331
+ return
332
+ }
333
+ const nextDirectory = await sessionPasteRoot(this.ctx, sessionId, current.storageDir)
334
+ const migratedPath = await copyImage(managedPath, nextDirectory.writeRoot, filename)
335
+ await rm(managedPath, { force: true }).catch(() => {})
336
+ managedPath = migratedPath
337
+ directory = nextDirectory
338
+ storage = current
339
+ }
340
+ throw new PasteStorageChangedError('Vision Toolkit settings changed repeatedly during image copy; retry the paste')
280
341
  } catch (error) {
281
- const status = error instanceof RangeError ? 413 : 400
342
+ if (managedPath !== undefined) await rm(managedPath, { force: true }).catch(() => {})
343
+ const status = error instanceof PasteStorageChangedError ? 409 : error instanceof RangeError ? 413 : 400
282
344
  this.ctx.logger.warn('dsh-vision-toolkit pasted image rejected: %s', message(error))
283
345
  requestError(res, status, 'paste-image-rejected', message(error))
284
346
  }