Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(soup): syncservice wakeup on enity with evth mount (#410)

Seamus93bff91946cf
diff --git a/js/app/packages/app/component/command/KonsoleItem.tsx b/js/app/packages/app/component/command/KonsoleItem.tsx
index 5b9365dee9..0eecd1d9ed 100644
--- a/js/app/packages/app/component/command/KonsoleItem.tsx
+++ b/js/app/packages/app/component/command/KonsoleItem.tsx
@@ -32,6 +32,7 @@ import {
EntityIcon,
type EntityWithValidIcon,
} from 'core/component/EntityIcon';
+import { syncServiceClient } from 'service-sync/client';
import {
type Component,
createMemo,
@@ -39,6 +40,8 @@ import {
type JSX,
type JSXElement,
Match,
+ onCleanup,
+ onMount,
type Setter,
Switch,
} from 'solid-js';
@@ -519,6 +522,15 @@ export function CommandItemCard(props: CommandItemProps) {
return name && name.length > 55 ? `${name.slice(0, 52)}...` : name;
};
+ onMount(() => {
+ if (blockName() === 'md') {
+ syncServiceClient.safeWakeup(props.item.data.id);
+ onCleanup(() => {
+ syncServiceClient.cancelWakeup(props.item.data.id);
+ });
+ }
+ });
+
const CommandItemContainer = ({ children }: { children?: JSXElement }) => {
const optionKeyPressed = createMemo(() => {
return pressedKeys().has('opt');
diff --git a/js/app/packages/core/signal/preview.ts b/js/app/packages/core/signal/preview.ts
index c61b2586ac..74819d7a32 100644
--- a/js/app/packages/core/signal/preview.ts
+++ b/js/app/packages/core/signal/preview.ts
@@ -241,7 +241,7 @@ async function fetchDocumentPreviews(ids: string[]): Promise<PreviewItem[]> {
switch (doc.type) {
case 'access':
if (doc.file_type === 'md') {
- syncServiceClient.wakeup({ documentId: doc.document_id });
+ syncServiceClient.safeWakeup(doc.document_id);
}
return {
...base,
diff --git a/js/app/packages/macro-entity/src/components/EntityWithEverything.tsx b/js/app/packages/macro-entity/src/components/EntityWithEverything.tsx
index 9fd467e563..4f8cb671c2 100644
--- a/js/app/packages/macro-entity/src/components/EntityWithEverything.tsx
+++ b/js/app/packages/macro-entity/src/components/EntityWithEverything.tsx
@@ -14,6 +14,7 @@ import { unifiedListMarkdownTheme } from 'core/component/LexicalMarkdown/theme';
import { UserIcon } from 'core/component/UserIcon';
import { emailToId, useDisplayName } from 'core/user';
import { onKeyDownClick, onKeyUpClick } from 'core/util/click';
+import { syncServiceClient } from 'service-sync/client';
import type { ParentProps, Ref } from 'solid-js';
import {
createDeferred,
@@ -217,6 +218,15 @@ export function EntityWithEverything(
return props.entity.search.contentHitData ?? [];
};
+ onMount(() => {
+ if (props.entity.type === 'document' && props.entity.fileType === 'md') {
+ syncServiceClient.safeWakeup(props.entity.id);
+ onCleanup(() => {
+ syncServiceClient.cancelWakeup(props.entity.id);
+ });
+ }
+ });
+
const EntityTitle = () => {
if (props.entity.type === 'email') {
const isLikelyEmail = (value?: string) =>
diff --git a/js/app/packages/macro-entity/src/queries/dss.ts b/js/app/packages/macro-entity/src/queries/dss.ts
index 3150a8ae39..042ff97af8 100644
--- a/js/app/packages/macro-entity/src/queries/dss.ts
+++ b/js/app/packages/macro-entity/src/queries/dss.ts
@@ -22,7 +22,6 @@ import { platformFetch } from 'core/util/platformFetch';
import type { GetItemsSoupParams } from 'service-storage/generated/schemas/getItemsSoupParams';
import type { SoupPage } from 'service-storage/generated/schemas/soupPage';
import { useInstructionsMdIdQuery } from 'service-storage/instructionsMd';
-import { syncServiceClient } from 'service-sync/client';
import type { Accessor } from 'solid-js';
import type {
ChatEntity,
@@ -54,11 +53,6 @@ const fetchPaginatedDocumentsGet = async ({
throw new Error('Failed to fetch documents', { cause: response });
const result: SoupPage = await response.json();
- result.items.forEach((item) => {
- if (item.tag === 'document' && item.data.fileType === 'md') {
- syncServiceClient.wakeup({ documentId: item.data.id });
- }
- });
return result;
};
@@ -90,11 +84,6 @@ const fetchPaginatedDocumentsPost = async ({
throw new Error('Failed to fetch documents', { cause: response });
const result: SoupPage = await response.json();
- result.items.forEach((item) => {
- if (item.tag === 'document' && item.data.fileType === 'md') {
- syncServiceClient.wakeup({ documentId: item.data.id });
- }
- });
return result;
};
diff --git a/js/app/packages/service-sync/client.ts b/js/app/packages/service-sync/client.ts
index ceee52a827..705742602b 100644
--- a/js/app/packages/service-sync/client.ts
+++ b/js/app/packages/service-sync/client.ts
@@ -18,6 +18,11 @@ import { InitializeFromSnapshotRequest } from './generated/schema';
const SYNC_SERVICE_WORKER_URL = `${SYNC_SERVICE_HOSTS['worker']}`;
+const WAKEUP_TTL = 55 * 1000; // 55 seconds - cloudflare ttl is 60
+
+const pendingWakeups = new Map<string, ReturnType<typeof setTimeout>>();
+const recentWakeups = new Map<string, number>();
+
export function syncFetch(
url: string,
init?: SafeFetchInit
@@ -48,6 +53,48 @@ export const syncServiceClient = {
method: 'GET',
});
},
+ async safeWakeup(id: string, delay: number = 200) {
+ const lastWakeup = recentWakeups.get(id);
+ if (lastWakeup && Date.now() - lastWakeup < WAKEUP_TTL) {
+ return;
+ }
+
+ const existingTimeout = pendingWakeups.get(id);
+ if (existingTimeout) {
+ // let the first timeout handle it.
+ return;
+ }
+
+ const timeout = setTimeout(async () => {
+ try {
+ await syncFetch(`/document/${id}/wakeup`, {
+ method: 'GET',
+ });
+ recentWakeups.set(id, Date.now());
+
+ // prune
+ const now = Date.now();
+ for (const [key, timestamp] of recentWakeups.entries()) {
+ if (now - timestamp >= WAKEUP_TTL) {
+ recentWakeups.delete(key);
+ }
+ }
+ } catch (error) {
+ console.error(`Failed to wakeup document ${id}:`, error);
+ } finally {
+ pendingWakeups.delete(id);
+ }
+ }, delay);
+
+ pendingWakeups.set(id, timeout);
+ },
+ cancelWakeup(id: string) {
+ const timeout = pendingWakeups.get(id);
+ if (timeout) {
+ clearTimeout(timeout);
+ pendingWakeups.delete(id);
+ }
+ },
async exists(args: { documentId: string }) {
const res = await syncFetch(`/document/${args.documentId}/exists`, {
method: 'GET',