Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ts/packages/agents/markdown/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"yjs": "^13.6.8"
},
"devDependencies": {
"@jest/globals": "^29.7.0",
"@milkdown/ctx": "^7.3.6",
"@types/debug": "^4.1.12",
"@types/express": "^4.17.17",
Expand Down
45 changes: 32 additions & 13 deletions ts/packages/agents/markdown/src/view/route/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,23 @@ function broadcastEvent(event: Record<string, unknown>): void {
}
}

function getBoundRevision(): string | null {
if (!filePath || !boundRelativePath) {
return null;
}
try {
return readBoundDocument({
token: bindingToken ?? undefined,
root: currentRoot,
relativePath: boundRelativePath,
filePath,
}).revision;
} catch (error) {
debug(`Unable to read current binding revision: ${error}`);
return null;
}
}

function notifyBindingToParent(): void {
process.send?.({
type: "bindingUpdated",
Expand Down Expand Up @@ -1598,19 +1615,8 @@ app.get("/events", (req: Request, res: Response) => {
res.flushHeaders();

clients.push(res);
let revision: string | null = null;
if (filePath && boundRelativePath) {
try {
revision = readBoundDocument({
token: bindingToken ?? undefined,
root: currentRoot,
relativePath: boundRelativePath,
filePath,
}).revision;
} catch (error) {
debug(`Unable to read binding bootstrap revision: ${error}`);
}
}
const clientRole = clients.length === 1 ? "primary" : "secondary";
const revision = getBoundRevision();
res.write(
`data: ${JSON.stringify({
type: "bindingBootstrap",
Expand All @@ -1619,12 +1625,25 @@ app.get("/events", (req: Request, res: Response) => {
documentName: filePath ? path.basename(filePath, ".md") : null,
boundRelativePath,
revision,
clientRole,
timestamp: Date.now(),
})}\n\n`,
);

req.on("close", () => {
const wasPrimary = clients[0] === res;
clients = clients.filter((client) => client !== res);
if (wasPrimary && clients[0]) {
safeWriteToResponse(
clients[0],
`data: ${JSON.stringify({
type: "primaryElected",
bindingToken,
revision: getBoundRevision(),
timestamp: Date.now(),
})}\n\n`,
);
}
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,18 @@ export class CollaborationManager {
`[COLLAB] Retrieved collaboration info for document: ${collabInfo.currentDocument}`,
);

// Never derive a room key from the display basename:
// same-basename files in different folders would collide.
const authoritativeDocumentId =
typeof collabInfo.currentDocumentId === "string" &&
collabInfo.currentDocumentId.length > 0
? collabInfo.currentDocumentId
: COLLABORATION_CONFIG.DEFAULT_DOCUMENT_ID;
const config = {
websocketServerUrl:
collabInfo.websocketServerUrl ||
COLLABORATION_CONFIG.DEFAULT_WEBSOCKET_URL,
documentId: collabInfo.currentDocument
? collabInfo.currentDocument.replace(".md", "")
: COLLABORATION_CONFIG.DEFAULT_DOCUMENT_ID,
documentId: authoritativeDocumentId,
fallbackToLocal: true,
};

Expand Down
Loading
Loading