Skip to content

Commit 92f5665

Browse files
committed
Handle file upload when runtime active
1 parent 0c5246c commit 92f5665

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

packages/agent-core/src/runtime-agent.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import type {
2626
import type {
2727
CellData,
2828
ExecutionQueueData,
29+
FileData,
2930
RuntimeSessionData,
3031
} from "@runtimed/schema";
3132

@@ -46,7 +47,7 @@ export class RuntimeAgent {
4647
activeExecutions = new Map<string, AbortController>();
4748
cancellationHandlers: CancellationHandler[] = [];
4849
renewalInterval?: ReturnType<typeof setInterval>;
49-
50+
fileUpload?: (id: string) => void;
5051
artifactClient: IArtifactClient;
5152

5253
config: RuntimeConfig;
@@ -64,6 +65,10 @@ export class RuntimeAgent {
6465
this.handlers = handlers;
6566
}
6667

68+
onFileUpload(cb: (id: string) => void) {
69+
this.fileUpload = cb;
70+
}
71+
6772
/**
6873
* Start the runtime agent - connects to LiveStore and begins processing
6974
*/
@@ -295,6 +300,14 @@ export class RuntimeAgent {
295300
}
296301
);
297302

303+
// Watch for file uploads
304+
const fileUploadedQuery$ = queryDb(
305+
tables.files.select().where({ notebookId: this.config.notebookId }),
306+
{
307+
label: "fileUploaded",
308+
}
309+
);
310+
298311
// Watch for cancelled executions
299312
const cancelledWorkQuery$ = queryDb(
300313
tables.executionQueue.select().where({ status: "cancelled" }),
@@ -434,13 +447,25 @@ export class RuntimeAgent {
434447
},
435448
});
436449

450+
const fileUploadedSub = this.store.subscribe(fileUploadedQuery$, {
451+
onUpdate: (entries: readonly FileData[]) => {
452+
if (this.isShuttingDown) return;
453+
if (entries.length === 0) return;
454+
455+
for (const entry of entries) {
456+
this.fileUpload?.(entry.id);
457+
}
458+
},
459+
});
460+
437461
// Store subscriptions for cleanup
438462
this.subscriptions.push(
439463
assignedWorkSub,
440464
pendingWorkSub,
441465
cancelledWorkSub,
442466
completedExecutionsSub,
443-
failedExecutionsSub
467+
failedExecutionsSub,
468+
fileUploadedSub
444469
);
445470
}
446471

packages/pyodide-runtime/src/index.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,18 +259,27 @@ export class PyodideRuntimeAgent extends LocalRuntimeAgent {
259259

260260
// Send uploaded files to worker
261261
if (this.agent) {
262-
const files = this.agent.store.query(
263-
tables.files
264-
.select()
265-
.where({ notebookId: this.agent.config.notebookId })
262+
const agent = this.agent;
263+
const files = agent.store.query(
264+
tables.files.select().where({ notebookId: agent.config.notebookId })
266265
);
267266
if (files.length > 0) {
268-
const agent = this.agent;
269267
const filesWithUrls = files.map((file) => ({
270268
...file,
271269
url: agent.artifactClient.getArtifactUrl(file.id),
272270
}));
273271
this.sendWorkerMessage("files", { files: filesWithUrls });
272+
this.agent.onFileUpload((id) => {
273+
const files = agent.store.query(
274+
tables.files
275+
.select()
276+
.where({ notebookId: agent.config.notebookId, id })
277+
);
278+
const file = files[0];
279+
if (file) {
280+
this.sendWorkerMessage("files", { files: [file] });
281+
}
282+
});
274283
}
275284
}
276285

packages/schema/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,6 +1331,7 @@ export type OutputData = typeof tables.outputs.Type;
13311331
export type RuntimeSessionData = typeof tables.runtimeSessions.Type;
13321332
export type ExecutionQueueData = typeof tables.executionQueue.Type;
13331333
export type UiStateData = typeof tables.uiState.Type;
1334+
export type FileData = typeof tables.files.Type;
13341335

13351336
// Type guards for MediaContainer
13361337
export function isInlineContainer<T>(

0 commit comments

Comments
 (0)