Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
refactor [tasks-fe] - use create task endpoint in compose task (#759)
Daniel Kweon4dbb03373680
diff --git a/js/app/packages/block-md/component/ComposeTask.tsx b/js/app/packages/block-md/component/ComposeTask.tsxindex 608cd8369f..2b34e91892 100644--- a/js/app/packages/block-md/component/ComposeTask.tsx+++ b/js/app/packages/block-md/component/ComposeTask.tsx@@ -26,7 +26,7 @@ import type {import { TextButton } from '@core/component/TextButton';import { toast } from '@core/component/Toast/Toast';import { itemToSafeName } from '@core/constant/allBlocks';-import { createMarkdownFile } from '@core/util/create';+import { createTask } from '@core/util/create';import { filterMap } from '@core/util/list';import { isErr } from '@core/util/maybeResult';import XIcon from '@icon/regular/x.svg';@@ -47,7 +47,7 @@ const COMPOSER_PROPERTIES = [];/**- * Make a task and append props.+ * Make a task and append props using the create_task endpoint.* @param taskTitle Title string* @param taskContent content markdown string* @param properties Stored prop value map@@ -60,39 +60,35 @@ async function createTaskWithProperties(properties: Array<[string, PropertyApiValues]>,definitions: Map<string, PropertyDefinition>) {- const res = await createMarkdownFile({+ // Convert properties to API format (filter out null values)+ const propertyValues = properties.flatMap(([id, value]) => {+ const isMultiSelect = definitions.get(id)?.is_multi_select ?? false;+ const apiValue = propertyValueToApi(value, isMultiSelect);+ if (apiValue === null) return [];+ return [{ propertyId: id, value: apiValue }];+ });++ const documentId = await createTask({title: taskTitle,content: taskContent,- isTask: true,+ propertyValues: propertyValues.length > 0 ? propertyValues : undefined,});- if (!res) {+ if (!documentId) {toast.failure('Failed to create Task');return null;}- const propRequests = properties.map(([id, value]) => {- const isMultiSelect = definitions.get(id)?.is_multi_select ?? false;- return propertiesServiceClient.setEntityProperty({- entity_id: res,- entity_type: 'TASK',- property_id: id,- body: {- value: propertyValueToApi(value, isMultiSelect),- },- });- });-- await Promise.allSettled(propRequests);-toast.embed(- () => <TaskToastPreview id={res} title={taskTitle} body={taskContent} />,+ () => (+ <TaskToastPreview id={documentId} title={taskTitle} body={taskContent} />+ ),{duration: 2_000,});- return res;+ return documentId;}/**diff --git a/js/app/packages/core/util/create.ts b/js/app/packages/core/util/create.tsindex bfcdae95db..70c642bceb 100644--- a/js/app/packages/core/util/create.ts+++ b/js/app/packages/core/util/create.ts@@ -8,6 +8,7 @@ import { cognitionApiServiceClient } from '@service-cognition/client';import type { CreateChatRequest } from '@service-cognition/generated/schemas';import { staticFileClient } from '@service-static-files/client';import { storageServiceClient } from '@service-storage/client';+import type { PropertyInput } from '@service-storage/generated/schemas/propertyInput';import { postNewHistoryItem } from '@service-storage/history';import { uploadToPresignedUrl } from '@service-storage/util/uploadToPresignedUrl';import { syncServiceClient } from '@service-sync/client';@@ -84,15 +85,50 @@ type CreateTaskArgs = {title?: string;content?: string;projectId?: string;+ propertyValues?: PropertyInput[];};/**- * Initializes a new task (markdown file with isTask flag) in dss & sync_service+ * Creates a task with optional properties using the create_task endpoint.+ * Content is initialized via sync service.*/export async function createTask(args?: CreateTaskArgs): Promise<string | undefined> {- return createMarkdownFile({ ...args, isTask: true });+ // Convert content to loro snapshot for sync service+ const markdownState = await createMarkdownStateFromContent(args?.content);+ const snapshot = await rawStateToLoroSnapshot(+ MARKDOWN_LORO_SCHEMA,+ markdownState as any+ );++ if (!snapshot) return;++ // Create task with properties in one call+ const result = await storageServiceClient.createTask({+ taskName: args?.title ?? '',+ projectId: args?.projectId,+ propertyValues: args?.propertyValues,+ });++ invalidateUserQuota();++ if (isErr(result)) return;++ const { documentId } = result[1];++ // Initialize sync service with content+ const syncRes = await syncServiceClient.initializeFromSnapshot({+ snapshot,+ documentId,+ });++ if (isErr(syncRes)) {+ console.error('Failed to initialize task content in sync service');+ // Task was created, just without content - still return the id+ }++ return documentId;}export async function createCodeFileFromText({diff --git a/js/app/packages/service-clients/service-storage/client.ts b/js/app/packages/service-clients/service-storage/client.tsindex 42309f0faf..c53cf64437 100644--- a/js/app/packages/service-clients/service-storage/client.ts+++ b/js/app/packages/service-clients/service-storage/client.ts@@ -43,6 +43,8 @@ import type { CreateDocumentHandler200 as CreateDocumentResponse } from './generimport type { CreateDocumentRequest } from './generated/schemas/createDocumentRequest';import type { CreateInstructionsDocumentResponse } from './generated/schemas/createInstructionsDocumentResponse';import type { CreateProjectResponse } from './generated/schemas/createProjectResponse';+import type { CreateTaskHandler200 as CreateTaskResponse } from './generated/schemas/createTaskHandler200';+import type { CreateTaskRequest } from './generated/schemas/createTaskRequest';import type { CreateUnthreadedAnchorResponse } from './generated/schemas/createUnthreadedAnchorResponse';import type { DeleteCommentResponse } from './generated/schemas/deleteCommentResponse';import type { DeleteUnthreadedAnchorResponse } from './generated/schemas/deleteUnthreadedAnchorResponse';@@ -456,6 +458,31 @@ export const storageServiceClient = {});},+ /**+ * Creates a task with properties in a single call.+ * NOTE: Content must be initialized separately via sync service (initializeFromSnapshot).+ */+ async createTask(request: CreateTaskRequest) {+ const result = await dssFetch<CreateTaskResponse>(+ `/documents/create_task`,+ {+ method: 'POST',+ body: JSON.stringify(request),+ }+ );++ if (!isOk(result)) {+ const err = result[0];+ if (err[0].message.includes('403')) {+ showPaywall(PaywallKey.FILE_LIMIT);+ }+ return result;+ }++ const [, response] = result;+ return ok({ documentId: response.documentId });+ },+async createTextDocument({ text, ...docArgs }) {const buffer = utf8Encode(text);const sha = await contentHash(buffer);