فهرست منبع

refactor(simulation): type backend RPC methods

James Long 1 ماه پیش
والد
کامیت
adabb01caf
2فایلهای تغییر یافته به همراه12 افزوده شده و 2 حذف شده
  1. 1 2
      packages/simulation/src/backend/control.ts
  2. 11 0
      packages/simulation/src/protocol/index.ts

+ 1 - 2
packages/simulation/src/backend/control.ts

@@ -29,7 +29,7 @@ function parseRequest(input: string | Buffer) {
 }
 
 async function handle(socket: ControlSocket, request: SimulationProtocol.JsonRpc.Request): Promise<unknown> {
-  switch (request.method) {
+  switch (SimulationProtocol.Backend.decodeMethod(request.method)) {
     case "llm.attach": {
       socket.data.unsubscribe?.()
       socket.data.unsubscribe = SimulationLLMExchange.subscribe((exchange) => {
@@ -62,7 +62,6 @@ async function handle(socket: ControlSocket, request: SimulationProtocol.JsonRpc
     case "network.log":
       return { entries: SimulationNetwork.log() }
   }
-  throw new Error(`Unknown simulation control method: ${request.method}`)
 }
 
 export function start(endpoint: string) {

+ 11 - 0
packages/simulation/src/protocol/index.ts

@@ -111,6 +111,17 @@ export namespace Frontend {
 }
 
 export namespace Backend {
+  export const Method = Schema.Literals([
+    "llm.attach",
+    "llm.chunk",
+    "llm.finish",
+    "llm.disconnect",
+    "llm.pending",
+    "network.log",
+  ])
+  export type Method = Schema.Schema.Type<typeof Method>
+  export const decodeMethod = Schema.decodeUnknownSync(Method)
+
   export const Item = Schema.Union([
     Schema.Struct({ type: Schema.Literal("textDelta"), text: Schema.String }),
     Schema.Struct({ type: Schema.Literal("reasoningDelta"), text: Schema.String }),