@@ -13,7 +13,7 @@ import { usePluginAttachment } from "@/runtime/plugin/host-apis"
1313import { createPathHelpers } from "./path"
1414import { artifactKind , artifactMime } from "@opencode/util/artifact"
1515import { usePlatform } from "@/runtime/platform/platform"
16- import { fileStreamUrl , machineFileStreamUrl } from "@/runtime/server/api"
16+ import { fileStreamUrl , machineFileStreamUrl , serverRelativeUrl } from "@/runtime/server/api"
1717import { fileContentFromBytes , headStreamedFile , streamedFileContent , streamedKinds } from "./artifact"
1818import { approxBytes , createFileContentCache } from "./content-cache"
1919import {
@@ -225,7 +225,8 @@ export const { use: useFile, provider: FileProvider, context: FileContext } = cr
225225
226226 // Media plays from the server's ranged reads, so only a HEAD runs here, for the file's size. A failed HEAD has no
227227 // body; the full read that follows reports the server's error. The API's request queue bounds the download's wait
228- // for a response, and its body may take as long as it needs. A file on a paired machine reads through the server.
228+ // for a response, and its body may take as long as it needs. A file on a paired machine reads through the server,
229+ // which with a password streams it from a URL carrying a ticket for that path; one it fails to issue downloads.
229230 const read = ( file : string , request : { path : string ; location : { directory : string } } , signal : AbortSignal ) => {
230231 const machine = isMachinePath ( file )
231232
@@ -235,17 +236,48 @@ export const { use: useFile, provider: FileProvider, context: FileContext } = cr
235236 : serverSDK . api . file . read ( request , { signal } )
236237 ) . then ( ( data ) => fileContentFromBytes ( file , data ) )
237238
238- const url = ! streamedKinds . has ( artifactKind ( file ) )
239- ? undefined
240- : machine
241- ? machineFileStreamUrl ( serverSDK . http , file )
242- : fileStreamUrl ( serverSDK . http , { path : request . path , directory : request . location . directory } )
239+ // When the URL's ticket expires, a minute early so a load near the end still gets a working one.
240+ let expires : number | undefined
243241
244- if ( ! url ) return download ( )
242+ const streamUrl = ( ) : Promise < string | undefined > => {
243+ if ( ! streamedKinds . has ( artifactKind ( file ) ) ) return Promise . resolve ( undefined )
245244
246- return headStreamedFile ( platform . fetch ?? fetch , url , signal ) . then ( ( response ) =>
247- response . ok ? streamedFileContent ( file , url , response ) : download ( ) ,
248- )
245+ if ( ! machine )
246+ return Promise . resolve (
247+ fileStreamUrl ( serverSDK . http , { path : request . path , directory : request . location . directory } ) ,
248+ )
249+
250+ if ( ! serverSDK . http . password ) return Promise . resolve ( machineFileStreamUrl ( serverSDK . http , file ) )
251+
252+ return serverSDK . api . experimental . machine . file . ticket ( { path : file } , { signal } ) . then (
253+ ( ticket ) => {
254+ expires = ticket . expires === undefined ? undefined : ticket . expires - 60_000
255+
256+ return serverRelativeUrl ( serverSDK . http , ticket . url )
257+ } ,
258+ ( ) => undefined ,
259+ )
260+ }
261+
262+ return streamUrl ( ) . then ( ( url ) => {
263+ if ( ! url ) return download ( )
264+
265+ return headStreamedFile ( platform . fetch ?? fetch , url , signal ) . then ( ( response ) => {
266+ if ( ! response . ok ) return download ( )
267+
268+ const content = streamedFileContent ( file , url , response )
269+
270+ // A ticketed URL stops working when its ticket expires, so the content goes stale then: the next load reads
271+ // it again with a new ticket.
272+ if ( expires !== undefined )
273+ setTimeout (
274+ ( ) => dispatch ( { type : "content.expired" , path : file , content } ) ,
275+ Math . max ( 0 , expires - Date . now ( ) ) ,
276+ )
277+
278+ return content
279+ } )
280+ } )
249281 }
250282
251283 // Built-in and virtual skills register `skill://` (or legacy `/builtin/`) paths.
0 commit comments