Dax Raad 9 months ago
parent
commit
48f81fe4d3
5 changed files with 118 additions and 154 deletions
  1. 25 17
      js/src/app/config.ts
  2. 4 14
      js/src/server/server.ts
  3. 9 5
      js/src/session/session.ts
  4. 56 85
      pkg/client/gen/openapi.json
  5. 24 33
      pkg/client/generated-client.go

+ 25 - 17
js/src/app/config.ts

@@ -11,25 +11,33 @@ export namespace Config {
     return result;
   });
 
-  export const Model = z.object({
-    name: z.string().optional(),
-    cost: z.object({
-      input: z.number(),
-      inputCached: z.number(),
-      output: z.number(),
-      outputCached: z.number(),
-    }),
-    contextWindow: z.number(),
-    maxTokens: z.number().optional(),
-    attachment: z.boolean(),
-    reasoning: z.boolean().optional(),
-  });
+  export const Model = z
+    .object({
+      name: z.string().optional(),
+      cost: z.object({
+        input: z.number(),
+        inputCached: z.number(),
+        output: z.number(),
+        outputCached: z.number(),
+      }),
+      contextWindow: z.number(),
+      maxTokens: z.number().optional(),
+      attachment: z.boolean(),
+      reasoning: z.boolean().optional(),
+    })
+    .openapi({
+      ref: "model",
+    });
   export type Model = z.output<typeof Model>;
 
-  export const Provider = z.object({
-    options: z.record(z.string(), z.any()).optional(),
-    models: z.record(z.string(), Model),
-  });
+  export const Provider = z
+    .object({
+      options: z.record(z.string(), z.any()).optional(),
+      models: z.record(z.string(), Model),
+    })
+    .openapi({
+      ref: "provider",
+    });
   export type Provider = z.output<typeof Provider>;
 
   export const Info = z

+ 4 - 14
js/src/server/server.ts

@@ -10,16 +10,6 @@ import { Config } from "../app/config";
 import { LLM } from "../llm/llm";
 import { Message } from "../session/message";
 
-const SessionInfo = Session.Info.openapi({
-  ref: "Session.Info",
-});
-
-const ProviderInfo = Config.Provider.openapi({
-  ref: "Provider.Info",
-});
-
-type ProviderInfo = z.output<typeof ProviderInfo>;
-
 export namespace Server {
   const log = Log.create({ service: "server" });
   const PORT = 16713;
@@ -92,7 +82,7 @@ export namespace Server {
               description: "Successfully created session",
               content: {
                 "application/json": {
-                  schema: resolver(SessionInfo),
+                  schema: resolver(Session.Info),
                 },
               },
             },
@@ -112,7 +102,7 @@ export namespace Server {
               description: "Successfully shared session",
               content: {
                 "application/json": {
-                  schema: resolver(SessionInfo),
+                  schema: resolver(Session.Info),
                 },
               },
             },
@@ -244,7 +234,7 @@ export namespace Server {
               description: "List of providers",
               content: {
                 "application/json": {
-                  schema: resolver(z.record(z.string(), ProviderInfo)),
+                  schema: resolver(z.record(z.string(), Config.Provider)),
                 },
               },
             },
@@ -252,7 +242,7 @@ export namespace Server {
         }),
         async (c) => {
           const providers = await LLM.providers();
-          const result: Record<string, ProviderInfo> = {};
+          const result: Record<string, Config.Provider> = {};
           for (const [providerID, provider] of Object.entries(providers)) {
             result[providerID] = provider.info;
           }

+ 9 - 5
js/src/session/session.ts

@@ -24,11 +24,15 @@ import { Bus } from "../bus";
 export namespace Session {
   const log = Log.create({ service: "session" });
 
-  export const Info = z.object({
-    id: Identifier.schema("session"),
-    shareID: z.string().optional(),
-    title: z.string(),
-  });
+  export const Info = z
+    .object({
+      id: Identifier.schema("session"),
+      shareID: z.string().optional(),
+      title: z.string(),
+    })
+    .openapi({
+      ref: "session.info",
+    });
   export type Info = z.output<typeof Info>;
 
   export const Event = {

+ 56 - 85
pkg/client/gen/openapi.json

@@ -33,7 +33,7 @@
             "content": {
               "application/json": {
                 "schema": {
-                  "$ref": "#/components/schemas/Session.Info"
+                  "$ref": "#/components/schemas/session.info"
                 }
               }
             }
@@ -52,7 +52,7 @@
             "content": {
               "application/json": {
                 "schema": {
-                  "$ref": "#/components/schemas/Session.Info"
+                  "$ref": "#/components/schemas/session.info"
                 }
               }
             }
@@ -129,23 +129,7 @@
                 "schema": {
                   "type": "array",
                   "items": {
-                    "type": "object",
-                    "properties": {
-                      "id": {
-                        "type": "string",
-                        "pattern": "^ses"
-                      },
-                      "shareID": {
-                        "type": "string"
-                      },
-                      "title": {
-                        "type": "string"
-                      }
-                    },
-                    "required": [
-                      "id",
-                      "title"
-                    ]
+                    "$ref": "#/components/schemas/session.info"
                   }
                 }
               }
@@ -254,7 +238,7 @@
                 "schema": {
                   "type": "object",
                   "additionalProperties": {
-                    "$ref": "#/components/schemas/Provider.Info"
+                    "$ref": "#/components/schemas/provider"
                   }
                 }
               }
@@ -724,23 +708,7 @@
             "type": "object",
             "properties": {
               "info": {
-                "type": "object",
-                "properties": {
-                  "id": {
-                    "type": "string",
-                    "pattern": "^ses"
-                  },
-                  "shareID": {
-                    "type": "string"
-                  },
-                  "title": {
-                    "type": "string"
-                  }
-                },
-                "required": [
-                  "id",
-                  "title"
-                ]
+                "$ref": "#/components/schemas/session.info"
               }
             },
             "required": [
@@ -753,7 +721,7 @@
           "properties"
         ]
       },
-      "Session.Info": {
+      "session.info": {
         "type": "object",
         "properties": {
           "id": {
@@ -772,7 +740,7 @@
           "title"
         ]
       },
-      "Provider.Info": {
+      "provider": {
         "type": "object",
         "properties": {
           "options": {
@@ -782,58 +750,61 @@
           "models": {
             "type": "object",
             "additionalProperties": {
-              "type": "object",
-              "properties": {
-                "name": {
-                  "type": "string"
-                },
-                "cost": {
-                  "type": "object",
-                  "properties": {
-                    "input": {
-                      "type": "number"
-                    },
-                    "inputCached": {
-                      "type": "number"
-                    },
-                    "output": {
-                      "type": "number"
-                    },
-                    "outputCached": {
-                      "type": "number"
-                    }
-                  },
-                  "required": [
-                    "input",
-                    "inputCached",
-                    "output",
-                    "outputCached"
-                  ]
-                },
-                "contextWindow": {
-                  "type": "number"
-                },
-                "maxTokens": {
-                  "type": "number"
-                },
-                "attachment": {
-                  "type": "boolean"
-                },
-                "reasoning": {
-                  "type": "boolean"
-                }
-              },
-              "required": [
-                "cost",
-                "contextWindow",
-                "attachment"
-              ]
+              "$ref": "#/components/schemas/model"
             }
           }
         },
         "required": [
           "models"
         ]
+      },
+      "model": {
+        "type": "object",
+        "properties": {
+          "name": {
+            "type": "string"
+          },
+          "cost": {
+            "type": "object",
+            "properties": {
+              "input": {
+                "type": "number"
+              },
+              "inputCached": {
+                "type": "number"
+              },
+              "output": {
+                "type": "number"
+              },
+              "outputCached": {
+                "type": "number"
+              }
+            },
+            "required": [
+              "input",
+              "inputCached",
+              "output",
+              "outputCached"
+            ]
+          },
+          "contextWindow": {
+            "type": "number"
+          },
+          "maxTokens": {
+            "type": "number"
+          },
+          "attachment": {
+            "type": "boolean"
+          },
+          "reasoning": {
+            "type": "boolean"
+          }
+        },
+        "required": [
+          "cost",
+          "contextWindow",
+          "attachment"
+        ]
       }
     }
   }

+ 24 - 33
pkg/client/generated-client.go

@@ -49,11 +49,7 @@ type EventMessageUpdated struct {
 // EventSessionUpdated defines model for Event.session.updated.
 type EventSessionUpdated struct {
 	Properties struct {
-		Info struct {
-			Id      string  `json:"id"`
-			ShareID *string `json:"shareID,omitempty"`
-			Title   string  `json:"title"`
-		} `json:"info"`
+		Info SessionInfo `json:"info"`
 	} `json:"properties"`
 	Type string `json:"type"`
 }
@@ -174,25 +170,28 @@ type MessageToolInvocationToolResult struct {
 	ToolName   string       `json:"toolName"`
 }
 
-// ProviderInfo defines model for Provider.Info.
-type ProviderInfo struct {
-	Models map[string]struct {
-		Attachment    bool    `json:"attachment"`
-		ContextWindow float32 `json:"contextWindow"`
-		Cost          struct {
-			Input        float32 `json:"input"`
-			InputCached  float32 `json:"inputCached"`
-			Output       float32 `json:"output"`
-			OutputCached float32 `json:"outputCached"`
-		} `json:"cost"`
-		MaxTokens *float32 `json:"maxTokens,omitempty"`
-		Name      *string  `json:"name,omitempty"`
-		Reasoning *bool    `json:"reasoning,omitempty"`
-	} `json:"models"`
+// Model defines model for model.
+type Model struct {
+	Attachment    bool    `json:"attachment"`
+	ContextWindow float32 `json:"contextWindow"`
+	Cost          struct {
+		Input        float32 `json:"input"`
+		InputCached  float32 `json:"inputCached"`
+		Output       float32 `json:"output"`
+		OutputCached float32 `json:"outputCached"`
+	} `json:"cost"`
+	MaxTokens *float32 `json:"maxTokens,omitempty"`
+	Name      *string  `json:"name,omitempty"`
+	Reasoning *bool    `json:"reasoning,omitempty"`
+}
+
+// Provider defines model for provider.
+type Provider struct {
+	Models  map[string]Model        `json:"models"`
 	Options *map[string]interface{} `json:"options,omitempty"`
 }
 
-// SessionInfo defines model for Session.Info.
+// SessionInfo defines model for session.info.
 type SessionInfo struct {
 	Id      string  `json:"id"`
 	ShareID *string `json:"shareID,omitempty"`
@@ -1330,7 +1329,7 @@ func (r GetEventResponse) StatusCode() int {
 type PostProviderListResponse struct {
 	Body         []byte
 	HTTPResponse *http.Response
-	JSON200      *map[string]ProviderInfo
+	JSON200      *map[string]Provider
 }
 
 // Status returns HTTPResponse.Status
@@ -1418,11 +1417,7 @@ func (r PostSessionCreateResponse) StatusCode() int {
 type PostSessionListResponse struct {
 	Body         []byte
 	HTTPResponse *http.Response
-	JSON200      *[]struct {
-		Id      string  `json:"id"`
-		ShareID *string `json:"shareID,omitempty"`
-		Title   string  `json:"title"`
-	}
+	JSON200      *[]SessionInfo
 }
 
 // Status returns HTTPResponse.Status
@@ -1630,7 +1625,7 @@ func ParsePostProviderListResponse(rsp *http.Response) (*PostProviderListRespons
 
 	switch {
 	case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 200:
-		var dest map[string]ProviderInfo
+		var dest map[string]Provider
 		if err := json.Unmarshal(bodyBytes, &dest); err != nil {
 			return nil, err
 		}
@@ -1734,11 +1729,7 @@ func ParsePostSessionListResponse(rsp *http.Response) (*PostSessionListResponse,
 
 	switch {
 	case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 200:
-		var dest []struct {
-			Id      string  `json:"id"`
-			ShareID *string `json:"shareID,omitempty"`
-			Title   string  `json:"title"`
-		}
+		var dest []SessionInfo
 		if err := json.Unmarshal(bodyBytes, &dest); err != nil {
 			return nil, err
 		}