Browse Source

exit properly

Dax Raad 9 months ago
parent
commit
2ed17f4877
6 changed files with 35 additions and 48 deletions
  1. 0 2
      js/src/index.ts
  2. 1 5
      js/src/server/server.ts
  3. 6 16
      js/src/session/session.ts
  4. 5 0
      js/src/share/share.ts
  5. 16 19
      js/src/storage/storage.ts
  6. 7 6
      js/src/util/log.ts

+ 0 - 2
js/src/index.ts

@@ -62,8 +62,6 @@ cli
           );
         }
       }
-
-      process.exit(0);
     });
   });
 

+ 1 - 5
js/src/server/server.ts

@@ -83,11 +83,7 @@ export namespace Server {
               description: "Successfully created session",
               content: {
                 "application/json": {
-                  schema: resolver(
-                    Session.Info.openapi({
-                      ref: "Session.Info",
-                    }),
-                  ),
+                  schema: resolver(z.custom<Session.Message[]>()),
                 },
               },
             },

+ 6 - 16
js/src/session/session.ts

@@ -100,15 +100,9 @@ export namespace Session {
       return match;
     }
     const result = [] as Message[];
-    const list = await Storage.list("session/message/" + sessionID)
-      .then((x) => x.toArray())
-      .catch(() => {});
-    if (!list) return result;
-    for (const item of list) {
-      const messageID = path.basename(item.path, ".json");
-      const read = await Storage.readJSON<Message>(
-        "session/message/" + sessionID + "/" + messageID,
-      );
+    const list = Storage.list("session/message/" + sessionID);
+    for await (const p of list) {
+      const read = await Storage.readJSON<Message>(p);
       result.push(read);
     }
     state().messages.set(sessionID, result);
@@ -116,13 +110,8 @@ export namespace Session {
   }
 
   export async function* list() {
-    try {
-      const result = await Storage.list("session/info");
-      for await (const item of result) {
-        yield path.basename(item.path, ".json");
-      }
-    } catch {
-      return;
+    for await (const item of Storage.list("session/info")) {
+      yield path.basename(item, ".json");
     }
   }
 
@@ -272,6 +261,7 @@ export namespace Session {
     session.tokens.input += usage.inputTokens || 0;
     session.tokens.output += usage.outputTokens || 0;
     session.tokens.reasoning += usage.reasoningTokens || 0;
+    console.log(session);
     await update(session);
     return next;
   }

+ 5 - 0
js/src/share/share.ts

@@ -22,6 +22,11 @@ export namespace Share {
           key: payload.properties.key,
           content: JSON.stringify(payload.properties.content),
         }),
+      }).then((x) => {
+        log.info("synced", {
+          key: payload.properties.key,
+          status: x.status,
+        });
       });
     });
   });

+ 16 - 19
js/src/storage/storage.ts

@@ -28,31 +28,28 @@ export namespace Storage {
     };
   });
 
-  function expose<T extends keyof FileStorage>(key: T) {
-    const fn = FileStorage.prototype[key];
-    return async (
-      ...args: Parameters<typeof fn>
-    ): Promise<ReturnType<typeof fn>> => {
-      const { storage } = await state();
-      const match = storage[key];
-      // @ts-ignore
-      return match.call(storage, ...args);
-    };
-  }
-
-  export const write = expose("write");
-  export const read = expose("read");
-  export const list = expose("list");
-  export const readToString = expose("readToString");
-
   export async function readJSON<T>(key: string) {
-    const data = await readToString(key + ".json");
+    const storage = await state().then((x) => x.storage);
+    const data = await storage.readToString(key + ".json");
     return JSON.parse(data) as T;
   }
 
   export async function writeJSON<T>(key: string, content: T) {
+    const storage = await state().then((x) => x.storage);
     const json = JSON.stringify(content);
-    await write(key + ".json", json);
+    await storage.write(key + ".json", json);
     Bus.publish(Event.Write, { key, content });
   }
+
+  export async function* list(prefix: string) {
+    try {
+      const storage = await state().then((x) => x.storage);
+      const list = storage.list(prefix);
+      for await (const item of list) {
+        yield item.path.slice(0, -5);
+      }
+    } catch {
+      return;
+    }
+  }
 }

+ 7 - 6
js/src/util/log.ts

@@ -1,5 +1,6 @@
 import path from "node:path";
 import { AppPath } from "../app/path";
+import fs from "fs/promises";
 export namespace Log {
   const write = {
     out: (msg: string) => {
@@ -11,12 +12,12 @@ export namespace Log {
   };
 
   export async function file(directory: string) {
-    const out = Bun.file(
-      path.join(AppPath.data(directory), "opencode.out.log"),
-    );
-    const err = Bun.file(
-      path.join(AppPath.data(directory), "opencode.err.log"),
-    );
+    const outPath = path.join(AppPath.data(directory), "opencode.out.log");
+    const errPath = path.join(AppPath.data(directory), "opencode.err.log");
+    await fs.truncate(outPath);
+    await fs.truncate(errPath);
+    const out = Bun.file(outPath);
+    const err = Bun.file(errPath);
     const outWriter = out.writer();
     const errWriter = err.writer();
     write["out"] = (msg) => {