@@ -4,6 +4,8 @@ import type {
4
4
} from "@rivetkit/engine-runner" ;
5
5
import { Runner } from "@rivetkit/engine-runner" ;
6
6
import * as cbor from "cbor-x" ;
7
+ import type { Context as HonoContext } from "hono" ;
8
+ import { streamSSE } from "hono/streaming" ;
7
9
import { WSContext } from "hono/ws" ;
8
10
import invariant from "invariant" ;
9
11
import { lookupInRegistry } from "@/actor/definition" ;
@@ -63,6 +65,9 @@ export class EngineActorDriver implements ActorDriver {
63
65
#actorRouter: ActorRouter ;
64
66
#version: number = 1 ; // Version for the runner protocol
65
67
68
+ #runnerStarted: PromiseWithResolvers < undefined > = Promise . withResolvers ( ) ;
69
+ #runnerStopped: PromiseWithResolvers < undefined > = Promise . withResolvers ( ) ;
70
+
66
71
constructor (
67
72
registryConfig : RegistryConfig ,
68
73
runConfig : RunConfig ,
@@ -111,6 +116,8 @@ export class EngineActorDriver implements ActorDriver {
111
116
runnerName : this . #config. runnerName ,
112
117
} ) ;
113
118
}
119
+
120
+ this . #runnerStarted. resolve ( undefined ) ;
114
121
} ,
115
122
onDisconnected : ( ) => {
116
123
logger ( ) . warn ( {
@@ -120,7 +127,9 @@ export class EngineActorDriver implements ActorDriver {
120
127
} ) ;
121
128
hasDisconnected = true ;
122
129
} ,
123
- onShutdown : ( ) => { } ,
130
+ onShutdown : ( ) => {
131
+ this . #runnerStopped. resolve ( undefined ) ;
132
+ } ,
124
133
fetch : this . #runnerFetch. bind ( this ) ,
125
134
websocket : this . #runnerWebSocket. bind ( this ) ,
126
135
onActorStart : this . #runnerOnActorStart. bind ( this ) ,
@@ -381,4 +390,17 @@ export class EngineActorDriver implements ActorDriver {
381
390
logger ( ) . info ( { msg : "stopping engine actor driver" } ) ;
382
391
await this . #runner. shutdown ( immediate ) ;
383
392
}
393
+
394
+ async serverlessHandleStart ( c : HonoContext ) : Promise < Response > {
395
+ await this . #runnerStarted. promise ;
396
+
397
+ return streamSSE ( c , async ( stream ) => {
398
+ // Runner id should be set if the runner started
399
+ const runnerId = this . #runner. runnerId ;
400
+ invariant ( runnerId , "runnerId not set" ) ;
401
+ stream . writeSSE ( { data : runnerId } ) ;
402
+
403
+ return this . #runnerStopped. promise ;
404
+ } ) ;
405
+ }
384
406
}
0 commit comments