Browse Source

Add automatic CONTEXT.md loading and improve share sync reliability

🤖 Generated with opencode
Co-Authored-By: opencode <[email protected]>
Dax Raad 9 months ago
parent
commit
754cc66741
3 changed files with 33 additions and 11 deletions
  1. 4 1
      js/CONTEXT.md
  2. 9 0
      js/src/session/session.ts
  3. 20 10
      js/src/share/share.ts

+ 4 - 1
js/CONTEXT.md

@@ -1,6 +1,7 @@
 # OpenCode Context
 
 ## Build/Test Commands
+
 - `bun install` - Install dependencies
 - `bun run index.ts` - Run the application
 - `bun build src/index.ts --compile --outfile ./dist/opencode` - Build executable
@@ -9,6 +10,7 @@
 - `bun test --test-name-pattern <regex>` - Run tests matching pattern
 
 ## Code Style & Conventions
+
 - TypeScript with Bun runtime
 - ES modules (`"type": "module"`)
 - Namespace-based organization (e.g., `Tool.define`, `App.provide`)
@@ -21,4 +23,5 @@
 - Error handling: try/catch with structured logging
 - File organization: group by feature in `src/` with index files for exports
 - Test files: co-located in `test/` directory, use Bun's built-in test runner
-- Naming: camelCase for variables/functions, PascalCase for namespaces/types
+- Naming: camelCase for variables/functions, PascalCase for namespaces/types
+

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

@@ -141,6 +141,7 @@ export namespace Session {
         msg,
       );
     }
+    const app = await App.use();
     if (msgs.length === 0) {
       const system: Message = {
         id: Identifier.ascending("message"),
@@ -159,6 +160,14 @@ export namespace Session {
           tool: {},
         },
       };
+      const contextFile = Bun.file(path.join(app.root, "CONTEXT.md"));
+      if (await contextFile.exists()) {
+        const context = await contextFile.text();
+        system.parts.push({
+          type: "text",
+          text: context,
+        });
+      }
       msgs.push(system);
       state().messages.set(sessionID, msgs);
       generateText({

+ 20 - 10
js/src/share/share.ts

@@ -8,6 +8,7 @@ export namespace Share {
   const log = Log.create({ service: "share" });
 
   let queue: Promise<void> = Promise.resolve();
+  const pending = new Map<string, any>();
 
   const state = App.state("share", async () => {
     Bus.subscribe(Storage.Event.Write, async (payload) => {
@@ -17,23 +18,32 @@ export namespace Share {
       const session = await Session.get(sessionID);
       if (!session.shareID) return;
 
+      const key = payload.properties.key;
+      pending.set(key, payload.properties.content);
+
       queue = queue
-        .then(() =>
-          fetch(`${URL}/share_sync`, {
+        .then(async () => {
+          const content = pending.get(key);
+          if (content === undefined) return;
+          pending.delete(key);
+
+          return fetch(`${URL}/share_sync`, {
             method: "POST",
             body: JSON.stringify({
               sessionID: sessionID,
               shareID: session.shareID,
-              key: payload.properties.key,
-              content: JSON.stringify(payload.properties.content),
+              key: key,
+              content: JSON.stringify(content),
             }),
-          }),
-        )
-        .then((x) => {
-          log.info("synced", {
-            key: payload.properties.key,
-            status: x.status,
           });
+        })
+        .then((x) => {
+          if (x) {
+            log.info("synced", {
+              key: key,
+              status: x.status,
+            });
+          }
         });
     });
   });