@anionex/dsh-turn-rewind 0.3.11 → 0.3.12

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.
@@ -1,4 +1,5 @@
1
1
  import { randomUUID } from 'node:crypto'
2
+ import { readFile } from 'node:fs/promises'
2
3
  import type { Context } from '@deepseek-ai/cordis'
3
4
  import { createDeadline } from './deadline.js'
4
5
  import { ChangeLedgerError, errorMessage } from './errors.js'
@@ -839,6 +840,93 @@ function json(response: HttpResponseLike, status: number, value: unknown): void
839
840
  response.end(`${JSON.stringify(value)}\n`)
840
841
  }
841
842
 
843
+ /** Same-origin route that replays one stored attachment's bytes to the browser. */
844
+ export const ATTACHMENT_HTTP_PATH = '/turn-rewind/attachment-bytes'
845
+
846
+ /** Shape of a durable attachment reference as stored on a user message. */
847
+ interface AttachmentRefLike {
848
+ readonly attachmentId: string
849
+ readonly name?: string
850
+ readonly mediaType?: string
851
+ }
852
+
853
+ /**
854
+ * Attachment store capability needed to turn a durable reference back into bytes.
855
+ *
856
+ * Both readers are provider-owned: a deployment without a local store rejects
857
+ * them, which this route reports as an unavailable attachment rather than a
858
+ * crash.
859
+ */
860
+ interface AttachmentStoreLike {
861
+ fileHostPath(ref: { readonly attachmentId: string }): string | undefined
862
+ imageHostPath(ref: { readonly attachmentId: string }): string | undefined
863
+ }
864
+
865
+ /** Register the attachment-bytes route consumed by the rewound composer draft. */
866
+ export function installAttachmentHttp(ctx: Context): void {
867
+ ctx.effect(() => (ctx as unknown as { webServer: HttpServerLike }).webServer.register({
868
+ kind: 'exact',
869
+ path: ATTACHMENT_HTTP_PATH,
870
+ handler: createAttachmentHttpHandler(ctx),
871
+ }), 'change-ledger.attachmentHttp')
872
+ }
873
+
874
+ /**
875
+ * Build the attachment-bytes route as a testable unit.
876
+ *
877
+ * Bytes travel in a JSON envelope shared by Web and Desktop carriers;
878
+ * the browser rebuilds a `File` from the verified message reference.
879
+ */
880
+ export function createAttachmentHttpHandler(
881
+ ctx: Pick<Context, 'get' | 'sessions' | 'sessionQuery'>,
882
+ ): (request: HttpRequestLike, response: HttpResponseLike) => Promise<void> {
883
+ return async (request, response) => {
884
+ if (request.method !== 'GET') {
885
+ json(response, 405, { ok: false, error: 'method not allowed' })
886
+ return
887
+ }
888
+ try {
889
+ const url = new URL(request.url ?? ATTACHMENT_HTTP_PATH, 'http://dsh.local')
890
+ const attachmentId = requiredText(url.searchParams.get('attachmentId'), 'attachmentId')
891
+ const sessionId = requiredText(url.searchParams.get('sessionId'), 'sessionId')
892
+ const messageSeq = Number(url.searchParams.get('messageSeq'))
893
+ if (!url.searchParams.has('messageSeq') || !Number.isSafeInteger(messageSeq) || messageSeq < 0) throw new Error('invalid messageSeq')
894
+ const kind = url.searchParams.get('kind')
895
+ if (kind !== 'image' && kind !== 'file') throw new Error('invalid attachment kind')
896
+ const session = await readSession(ctx, sessionId)
897
+ const message = getSessionEvents(session).find(event => event.seq === messageSeq && event.type === 'user/message' && isDirectUserMessage(event))
898
+ const blocks = Array.isArray(message?.data.content) ? message.data.content : []
899
+ const block = blocks.find(value => value?.type === kind && value.attachment?.attachmentId === attachmentId)
900
+ if (block === undefined) {
901
+ json(response, 404, { ok: false, error: 'attachment is not referenced by the selected message' })
902
+ return
903
+ }
904
+ const store = ctx.get('attachments') as AttachmentStoreLike | undefined
905
+ if (store === undefined) {
906
+ json(response, 503, { ok: false, error: 'attachment store is unavailable' })
907
+ return
908
+ }
909
+ const ref = block.attachment as AttachmentRefLike
910
+ const hostPath = kind === 'image' ? store.imageHostPath(ref) : store.fileHostPath(ref)
911
+ if (hostPath === undefined) {
912
+ json(response, 503, { ok: false, error: 'attachment is not available from the local store' })
913
+ return
914
+ }
915
+ const bytes = await readFile(hostPath)
916
+ json(response, 200, {
917
+ ok: true,
918
+ attachmentId,
919
+ kind,
920
+ ...(ref.mediaType === undefined ? {} : { mediaType: ref.mediaType }),
921
+ data: bytes.toString('base64'),
922
+ })
923
+ } catch (error) {
924
+ const message = errorMessage(error)
925
+ json(response, message.includes('ENOENT') ? 404 : 400, { ok: false, error: 'unable to restore the selected attachment' })
926
+ }
927
+ }
928
+ }
929
+
842
930
  export const MANAGE_HTTP_PATH = '/turn-rewind/manage'
843
931
 
844
932
  /** Register the same-origin storage-management endpoint consumed by the settings card. */