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.

unified-list: folders view use soup post request endpoint to fetch folders (#288)

Caleb Taylor41cdd827b1b3
diff --git a/js/app/packages/app/component/UnifiedListView.tsx b/js/app/packages/app/component/UnifiedListView.tsx
index 0454a0f552..bc9312e199 100644
--- a/js/app/packages/app/component/UnifiedListView.tsx
+++ b/js/app/packages/app/component/UnifiedListView.tsx
@@ -32,7 +32,8 @@ import { ContextMenu } from '@kobalte/core/context-menu';
import { supportedExtensions } from '@lexical-core/utils';
import {
createChannelsQuery,
- createDssInfiniteQuery,
+ createDssInfiniteQueryGet,
+ createDssInfiniteQueryPost,
createEmailsInfiniteQuery,
createFilterComposer,
createProjectFilterFn,
@@ -75,7 +76,10 @@ import type {
UnifiedSearchIndex,
UnifiedSearchRequestFilters,
} from '@service-search/generated/models';
-import type { GetItemsSoupParams } from '@service-storage/generated/schemas';
+import type {
+ GetItemsSoupParams,
+ PostSoupRequest,
+} from '@service-storage/generated/schemas';
import { debounce } from '@solid-primitives/scheduled';
import stringify from 'json-stable-stringify';
import {
@@ -129,6 +133,7 @@ import {
type SortOptions,
VIEWCONFIG_BASE,
VIEWCONFIG_DEFAULTS_IDS,
+ VIEWCONFIG_DEFAULTS_IDS_ENUM,
type ViewConfigBase,
type ViewData,
} from './ViewConfig';
@@ -637,6 +642,27 @@ export function UnifiedListView(props: UnifiedListViewProps) {
sort_method: sortType(),
})
);
+ const GARBAGE_UUID = '00000000-0000-0000-0000-000000000000';
+ const dssQueryPOSTRequestBody = createMemo(
+ (): PostSoupRequest => ({
+ channel_filters: {
+ channel_ids: [GARBAGE_UUID],
+ },
+ document_filters: {
+ document_ids: entityTypeFilter().includes('document')
+ ? []
+ : [GARBAGE_UUID],
+ },
+ chat_filters: {
+ chat_ids: [GARBAGE_UUID],
+ },
+ email_filters: {
+ recipients: [GARBAGE_UUID],
+ },
+ limit: props.defaultDisplayOptions?.limit ?? 100,
+ sort_method: sortType(),
+ })
+ );
const emailQueryParams = createMemo((): FetchPaginatedEmailsParams => {
const sort = sortType();
return {
@@ -692,7 +718,18 @@ export function UnifiedListView(props: UnifiedListViewProps) {
return false;
});
- const disableDssInfiniteQuery = createMemo(() => {
+ const disableDssInfiniteQueryGET = createMemo(() => {
+ if (view().id === VIEWCONFIG_DEFAULTS_IDS_ENUM.folders) return true;
+
+ const typeFilter = entityTypeFilter();
+ if (typeFilter.length === 0) return false;
+ const dssTypes = ['document', 'chat', 'project'];
+ const hasDssTypes = typeFilter.some((t) => dssTypes.includes(t));
+ return !hasDssTypes;
+ });
+ const disableDssInfiniteQueryPost = createMemo(() => {
+ if (view().id !== VIEWCONFIG_DEFAULTS_IDS_ENUM.folders) return true;
+
const typeFilter = entityTypeFilter();
if (typeFilter.length === 0) return false;
const dssTypes = ['document', 'chat', 'project'];
@@ -709,8 +746,12 @@ export function UnifiedListView(props: UnifiedListViewProps) {
const channelsQuery = createChannelsQuery({
disabled: disableChannelsQuery,
});
- const dssInfiniteQuery = createDssInfiniteQuery(dssQueryParams, {
- disabled: disableDssInfiniteQuery,
+ const dssInfiniteQueryGET = createDssInfiniteQueryGet(dssQueryParams, {
+ disabled: disableDssInfiniteQueryGET,
+ });
+ const dssInfiniteQueryPOST = createDssInfiniteQueryPost(dssQueryParams, {
+ disabled: disableDssInfiniteQueryPost,
+ requestBody: dssQueryPOSTRequestBody,
});
const emailsInfiniteQuery = createEmailsInfiniteQuery(emailQueryParams, {
refetchInterval: () => emailRefetchInterval(),
@@ -790,7 +831,11 @@ export function UnifiedListView(props: UnifiedListViewProps) {
>({
entityInfiniteQueries: [
{
- query: dssInfiniteQuery,
+ query: dssInfiniteQueryGET,
+ operations: { filter: true, search: true },
+ },
+ {
+ query: dssInfiniteQueryPOST,
operations: { filter: true, search: true },
},
{
diff --git a/js/app/packages/app/component/ViewConfig.tsx b/js/app/packages/app/component/ViewConfig.tsx
index 546664d51e..fd8fc4c98f 100644
--- a/js/app/packages/app/component/ViewConfig.tsx
+++ b/js/app/packages/app/component/ViewConfig.tsx
@@ -260,6 +260,11 @@ export const VIEWCONFIG_DEFAULTS = Object.fromEntries(
export const VIEWCONFIG_DEFAULTS_IDS = Object.keys(
VIEWCONFIG_DEFAULTS
) as View[];
+export const VIEWCONFIG_DEFAULTS_IDS_ENUM = Object.fromEntries(
+ Object.entries(VIEWCONFIG_DEFAULTS).map(([key]) => {
+ return [key, key];
+ })
+) as Record<View, string>;
export const VIEWCONFIG_FILTER_SHOW_OPTIONS: readonly FilterOptions['notificationFilter'][] =
['all', 'unread', 'notDone'] as const;
diff --git a/js/app/packages/macro-entity/src/index.tsx b/js/app/packages/macro-entity/src/index.tsx
index dc35af6556..ad769e7938 100644
--- a/js/app/packages/macro-entity/src/index.tsx
+++ b/js/app/packages/macro-entity/src/index.tsx
@@ -17,7 +17,8 @@ export {
createChatsInfiniteQuery,
createDeleteDssItemMutation,
createDocumentsInfiniteQuery,
- createDssInfiniteQuery,
+ createDssInfiniteQueryGet,
+ createDssInfiniteQueryPost,
} from './queries/dss';
export { createEmailsInfiniteQuery } from './queries/email';
export type { EntityInfiniteQuery, EntityQuery } from './queries/entity';
diff --git a/js/app/packages/macro-entity/src/queries/dss.ts b/js/app/packages/macro-entity/src/queries/dss.ts
index d2adf8ef7f..f020973af3 100644
--- a/js/app/packages/macro-entity/src/queries/dss.ts
+++ b/js/app/packages/macro-entity/src/queries/dss.ts
@@ -1,14 +1,25 @@
import { deleteItem } from '@core/component/FileList/itemOperations';
import { useItemOperations } from '@core/component/FileList/useItemOperations';
import type { ItemType } from '@service-storage/client';
-import { hashKey, useInfiniteQuery, useMutation } from '@tanstack/solid-query';
+import {
+ PostItemsSoupParams,
+ PostSoupRequest,
+ SoupApiSort,
+} from '@service-storage/generated/schemas';
+import {
+ hashKey,
+ InfiniteData,
+ UseQueryResult,
+ useInfiniteQuery,
+ useMutation,
+} from '@tanstack/solid-query';
import { SERVER_HOSTS } from 'core/constant/servers';
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 Accessor } from 'solid-js';
import type {
ChatEntity,
DocumentEntity,
@@ -19,7 +30,7 @@ import { createApiTokenQuery } from './auth';
import { queryClient } from './client';
import { type DssQueryKey, dssQueryKeyHashFn, queryKeys } from './key';
-const fetchPaginatedDocuments = async ({
+const fetchPaginatedDocumentsGet = async ({
apiToken,
...params
}: GetItemsSoupParams & {
@@ -46,14 +57,50 @@ const fetchPaginatedDocuments = async ({
return result;
};
-export function createDssInfiniteQuery(
- args?: Accessor<GetItemsSoupParams>,
+const fetchPaginatedDocumentsPost = async ({
+ apiToken,
+ params,
+ requestBody,
+}: {
+ apiToken?: string;
+ requestBody?: PostSoupRequest;
+ params?: PostItemsSoupParams;
+}) => {
+ if (!apiToken) throw new Error('No API token provided');
+ const Authorization = `Bearer ${apiToken}`;
+
+ const url = new URL(`${SERVER_HOSTS['document-storage-service']}/items/soup`);
+ if (params) {
+ Object.entries(params).forEach(([key, value]) => {
+ if (value) url.searchParams.set(key, value.toString());
+ });
+ }
+
+ const response = await platformFetch(url, {
+ headers: { Authorization, 'Content-Type': 'application/json' },
+ method: 'POST',
+ body: requestBody ? JSON.stringify(requestBody) : undefined,
+ });
+ if (!response.ok)
+ throw new Error('Failed to fetch documents', { cause: response });
+
+ const result: SoupPage = await response.json();
+ result.items.forEach((item) => {
+ if (item.type === 'document' && item.fileType === 'md') {
+ syncServiceClient.wakeup({ documentId: item.id });
+ }
+ });
+ return result;
+};
+
+export function createDssInfiniteQueryGet(
+ _params?: Accessor<GetItemsSoupParams>,
options?: {
disabled?: Accessor<boolean>;
}
) {
const params = () => {
- const argParams = args?.();
+ const argParams = _params?.();
const limit =
argParams?.limit && argParams.limit > 0 && argParams.limit <= 500
? argParams.limit
@@ -77,58 +124,138 @@ export function createDssInfiniteQuery(
queryKey,
queryHash: hashKey(queryKey),
queryFn: ({ pageParam }) =>
- fetchPaginatedDocuments({ apiToken: authQuery.data, ...pageParam }),
+ fetchPaginatedDocumentsGet({ apiToken: authQuery.data, ...pageParam }),
initialPageParam: params(),
getNextPageParam: ({ next_cursor: cursor }) =>
cursor ? { ...params(), cursor } : undefined,
- select: (data) =>
- data.pages.flatMap(({ items }) =>
- items
- .filter(
- (item) =>
- item.type !== 'document' ||
- !instructionsIdQuery.isSuccess ||
- item.id !== instructionsIdQuery.data
- )
- .map((item): DocumentEntity | ChatEntity | ProjectEntity => {
- if (item.type === 'chat') {
- return {
- ...item,
- name: item.name || 'New Chat',
- frecencyScore: item.frecency_score,
- viewedAt: item.viewedAt ?? undefined,
- projectId: item.projectId ?? undefined,
- };
- }
-
- if (item.type === 'project') {
- return {
- createdAt: item.createdAt,
- updatedAt: item.updatedAt,
- id: item.id,
- ownerId: item.ownerId,
- frecencyScore: item.frecency_score,
- viewedAt: item.viewedAt ?? undefined,
- parentId: item.parentId ?? undefined,
- type: item.type,
- name: item.name || 'New Project',
- };
- }
-
- return {
- ...item,
- name: item.name || 'New Note',
- frecencyScore: item.frecency_score,
- viewedAt: item.viewedAt ?? undefined,
- fileType: item.fileType ?? undefined,
- projectId: item.projectId ?? undefined,
- };
- })
- ),
+ select: (data) => selectData(data, { instructionsIdQuery }),
enabled: authQuery.isSuccess && !options?.disabled?.(),
};
});
}
+export function createDssInfiniteQueryPost(
+ _params?: Accessor<PostItemsSoupParams>,
+ options?: {
+ disabled?: Accessor<boolean>;
+ requestBody?: Accessor<PostSoupRequest>;
+ }
+) {
+ const params = () => {
+ const argParams = _params?.();
+ let limit = 100;
+ let sort_method = undefined;
+ const requestBody = options?.requestBody;
+
+ if (requestBody) {
+ const body = requestBody();
+ if (body?.limit) {
+ limit = body.limit;
+ }
+ if (body?.sort_method) {
+ sort_method = body.sort_method;
+ }
+ }
+
+ return {
+ ...argParams,
+ limit,
+ sort_method,
+ };
+ };
+
+ const authQuery = createApiTokenQuery();
+ const instructionsIdQuery = useInstructionsMdIdQuery();
+
+ return useInfiniteQuery(() => {
+ const requestBody = options?.requestBody?.();
+ // Only include document_filters in query key for granular refetching
+ // This ensures the query only refetches when document_filters/document_ids changes
+ const documentFilters = requestBody?.document_filters;
+ const queryKey = queryKeys.dss({
+ infinite: true,
+ ...params(),
+ // Include only document_filters in query key so query refetches only when document filters change
+ documentFilters: documentFilters
+ ? JSON.stringify(documentFilters)
+ : undefined,
+ });
+
+ return {
+ queryKey,
+ queryHash: hashKey(queryKey),
+ queryFn: ({ pageParam }) => {
+ return fetchPaginatedDocumentsPost({
+ apiToken: authQuery.data,
+ requestBody: requestBody,
+ params: { cursor: pageParam.cursor },
+ });
+ },
+ initialPageParam: params(),
+ getNextPageParam: ({ next_cursor: cursor }) =>
+ cursor ? { ...params(), cursor } : undefined,
+ select: (data) => selectData(data, { instructionsIdQuery }),
+ enabled: authQuery.isSuccess && !options?.disabled?.(),
+ };
+ });
+}
+const selectData: (
+ data: InfiniteData<
+ SoupPage,
+ {
+ limit: number;
+ expand?: boolean;
+ sort_method?: SoupApiSort;
+ cursor?: string;
+ }
+ >,
+ options: {
+ instructionsIdQuery: UseQueryResult<string | null | undefined, Error>;
+ }
+) => (DocumentEntity | ChatEntity | ProjectEntity)[] = (data, options) => {
+ return data.pages.flatMap(({ items }) =>
+ items
+ .filter(
+ (item) =>
+ item.type !== 'document' ||
+ !options.instructionsIdQuery.isSuccess ||
+ item.id !== options.instructionsIdQuery.data
+ )
+ .map((item): DocumentEntity | ChatEntity | ProjectEntity => {
+ if (item.type === 'chat') {
+ return {
+ ...item,
+ name: item.name || 'New Chat',
+ frecencyScore: item.frecency_score,
+ viewedAt: item.viewedAt ?? undefined,
+ projectId: item.projectId ?? undefined,
+ };
+ }
+
+ if (item.type === 'project') {
+ return {
+ createdAt: item.createdAt,
+ updatedAt: item.updatedAt,
+ id: item.id,
+ ownerId: item.ownerId,
+ frecencyScore: item.frecency_score,
+ viewedAt: item.viewedAt ?? undefined,
+ parentId: item.parentId ?? undefined,
+ type: item.type,
+ name: item.name || 'New Project',
+ };
+ }
+
+ return {
+ ...item,
+ name: item.name || 'New Note',
+ frecencyScore: item.frecency_score,
+ viewedAt: item.viewedAt ?? undefined,
+ fileType: item.fileType ?? undefined,
+ projectId: item.projectId ?? undefined,
+ };
+ })
+ );
+};
export function createDocumentsInfiniteQuery(
args?: GetItemsSoupParams | Accessor<GetItemsSoupParams>
@@ -160,7 +287,7 @@ export function createDocumentsInfiniteQuery(
}) as DssQueryKey
),
queryFn: ({ pageParam }) =>
- fetchPaginatedDocuments({ apiToken: authQuery.data, ...pageParam }),
+ fetchPaginatedDocumentsGet({ apiToken: authQuery.data, ...pageParam }),
initialPageParam: params(),
getNextPageParam: ({ next_cursor: cursor }) =>
cursor ? { ...params(), cursor } : undefined,
@@ -206,7 +333,7 @@ export function createChatsInfiniteQuery(
queryKeys.chat({ infinite: true, ...params() }) as DssQueryKey
),
queryFn: ({ pageParam }) =>
- fetchPaginatedDocuments({ apiToken: authQuery.data, ...pageParam }),
+ fetchPaginatedDocumentsGet({ apiToken: authQuery.data, ...pageParam }),
initialPageParam: params(),
getNextPageParam: ({ next_cursor: cursor }) =>
cursor ? { ...params(), cursor } : undefined,