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
import {
PdfCoParseSchema as CoParseSchema,
PdfModificationDataOnServerSchema,
PdfSegmentSchema as TSegmentSchema,
} from '@coparse/document-processing-types';
import {
fetchErrorsSvc,
nonNullShape,
type ServiceClient,
Svc,
withFetchErrors,
} from '@core/service';
import { z } from 'zod';
import * as schemas from './generated/zod';
import { ITEM_TYPES } from './itemType';
const _ChatMessageSchema = z.object({
content: z.string().describe('Content of the message'),
id: z.number().describe('The chat message id'),
role: z.string().describe('Whether the chat is from the user or system'),
});
const _ChatResponseSchema = z.object({});
const ProcessingResultTypeSchema = z.enum(['PREPROCESS', 'SPLIT_TEXTS']);
/** Available document processing result variants. */
export type ProcessingResultType = z.infer<typeof ProcessingResultTypeSchema>;
const DocxDocumentPartLocation = z.object({
sha: z.string(),
url: z.string(),
});
const GetWriterPartsResponse = z.object({
presignedUrls: z.array(DocxDocumentPartLocation),
});
const AnnotationsSvc = new Svc('Annotations Service')
.use('fetchErrors', fetchErrorsSvc)
.fn('getAnchors', {
description: schemas.getDocumentAnchorsParams.description!,
args: {
documentId: schemas.getDocumentAnchorsParams.shape.document_id,
},
result: schemas.getDocumentAnchorsResponse.shape,
throws: withFetchErrors(),
})
.fn('createAnchor', {
description: schemas.createAnchorParams.description!,
args: {
documentId: schemas.createAnchorParams.shape.document_id,
body: schemas.createAnchorBody,
},
result: schemas.createAnchorResponse,
throws: withFetchErrors(),
})
.fn('deleteAnchor', {
description: schemas.deleteAnchorBody.description!,
args: { body: schemas.deleteAnchorBody },
result: schemas.deleteAnchorResponse,
throws: withFetchErrors(),
modifies: true,
})
.fn('editAnchor', {
description: schemas.editAnchorBody.description!,
args: { body: schemas.editAnchorBody },
result: schemas.editAnchorResponse,
throws: withFetchErrors(),
modifies: true,
});
const ProjectsSvc = new Svc('Projects Service')
.use('fetchErrors', fetchErrorsSvc)
.fn('getAll', {
description: 'Get all projects',
result: { data: z.array(z.any()) }, // Temporary workaround until we have a schema on the clientside
throws: withFetchErrors(),
})
.fn('getPending', {
description: 'Get all pending projects',
result: {
data: schemas.getPendingProjectsHandlerResponse.shape.data,
},
throws: withFetchErrors(),
})
.fn('create', {
description: 'Create a new project',
args: {
name: z.string(),
projectParentId: z.string(),
sharePermission: z.null(),
},
result: schemas.createProjectHandlerResponse.shape.data.shape,
modifies: true,
throws: withFetchErrors(),
})
.fn('getProject', {
description: 'Get a project',
args: schemas.getProjectHandlerParams.shape,
result: schemas.getProjectHandlerResponse.shape.data.shape,
throws: withFetchErrors(),
})
.fn('delete', {
description: 'Delete a project',
args: { id: z.string() },
result: { success: z.boolean() },
modifies: true,
throws: withFetchErrors(),
})
.fn('edit', {
description: 'Edit a project',
args: schemas.editProjectV2Body.extend(schemas.editProjectV2Params.shape)
.shape,
result: { success: z.boolean() },
modifies: true,
throws: withFetchErrors(),
})
.fn('getContent', {
description: 'Get project content',
args: { id: z.string() },
result: schemas.getProjectContentHandlerResponse.shape,
throws: withFetchErrors(),
})
.fn('getPermissions', {
description:
schemas.getProjectPermissionsV2Params.description ??
'Get project permissions',
args: schemas.getProjectPermissionsV2Params.shape,
result: schemas.getProjectPermissionsV2Response.shape,
throws: withFetchErrors(),
})
.fn('getUserAccessLevel', {
description:
schemas.getProjectUserAccessLevelParams.description ??
'Get project user access level',
args: schemas.getProjectUserAccessLevelParams.shape,
result: schemas.getProjectUserAccessLevelResponse,
throws: withFetchErrors(),
})
.fn('getPreview', {
description: 'Get project preview',
args: schemas.getBatchProjectPreviewBody.shape,
result: schemas.getBatchProjectPreviewResponse.shape,
throws: withFetchErrors(),
})
.fn('createUploadZipRequest', {
description: 'Create a request id for uploading a zip file',
args: schemas.uploadExtractFolderHandlerBody.shape,
result: schemas.uploadExtractFolderHandlerResponse.shape.data.shape,
throws: withFetchErrors(),
})
.fn('permanentlyDelete', {
description: 'Permanently delete a project',
args: {
id: schemas.permanentlyDeleteProjectParams.shape.id,
},
result: schemas.permanentlyDeleteProjectResponse.shape.data.shape,
modifies: true,
throws: withFetchErrors(),
})
.fn('revertDelete', {
description: 'Revert the deletion of a project',
args: {
id: schemas.revertDeleteProjectParams.shape.id,
},
result: schemas.revertDeleteProjectResponse.shape.data.shape,
modifies: true,
throws: withFetchErrors(),
});
const ViewsSvc = new Svc('Views Service')
.use('fetchErrors', fetchErrorsSvc)
.fn('getSavedViews', {
description: 'Get the list of saved views',
result: schemas.getViewsHandlerResponse.shape,
throws: withFetchErrors(),
})
.fn('createSavedView', {
description: 'Create a saved view',
args: schemas.createViewHandlerBody.shape,
modifies: true,
result: schemas.createViewHandlerResponse.shape,
throws: withFetchErrors(),
})
.fn('excludeDefaultView', {
description: 'exclude a default view',
args: schemas.excludeDefaultViewHandlerBody.shape,
modifies: true,
throws: withFetchErrors(),
})
.fn('patchView', {
description: 'patch a view',
args: {
...schemas.patchViewHandlerParams.shape,
...schemas.patchViewHandlerBody.shape,
},
modifies: true,
throws: withFetchErrors(),
})
.fn('deleteView', {
description: 'patch a view',
args: {
savedViewId: z.string(),
},
modifies: true,
throws: withFetchErrors(),
});
const FavoritesSvc = new Svc('Favorites Service')
.use('fetchErrors', fetchErrorsSvc)
.fn('getFavorites', {
description: "Get the user's favorites",
result: schemas.listFavoritesResponse.shape,
throws: withFetchErrors(),
})
.fn('addFavorite', {
description: "Favorite an entity in the user's collection",
args: schemas.addFavoriteBody.shape,
modifies: true,
result: schemas.addFavoriteResponse.shape,
throws: withFetchErrors(),
})
.fn('removeFavoriteByEntity', {
description: 'Remove a favorite by entity',
args: {
entityType: schemas.removeFavoriteByEntityParams.shape.entity_type,
entityId: schemas.removeFavoriteByEntityParams.shape.entity_id,
},
modifies: true,
throws: withFetchErrors(),
})
.fn('reorderFavorites', {
description: "Persist a manual order for the user's favorites",
args: schemas.reorderFavoritesBody.shape,
modifies: true,
throws: withFetchErrors(),
});
const PermissionsTokensSvc = new Svc('Permissions Tokens Service')
.use('fetchErrors', fetchErrorsSvc)
.fn('createPermissionToken', {
description: 'creates a permission token for a document',
args: schemas.getDocumentPermissionsTokenParams.shape,
result: schemas.getDocumentPermissionsTokenResponse.shape,
throws: withFetchErrors(),
})
.fn('validatePermissionToken', {
description: 'gets a permission token for a document',
args: schemas.validateDocumentPermissionsTokenBody.shape,
result: schemas.validateDocumentPermissionsTokenResponse.shape,
throws: withFetchErrors(),
});
const CollabSurfacesSvc = new Svc('Collab Surfaces Service')
.use('fetchErrors', fetchErrorsSvc)
.fn('ensure', {
description:
'idempotently ensures a collab surface exists (load-or-create) attached to a parent entity, with its sync-service session initialized',
args: schemas.ensureCollabSurfaceBody.extend(
schemas.ensureCollabSurfaceParams.shape
).shape,
result: schemas.ensureCollabSurfaceResponse.shape,
modifies: true,
throws: withFetchErrors(),
})
.fn('get', {
description: 'fetches a collab surface',
args: schemas.getCollabSurfaceParams.shape,
result: schemas.getCollabSurfaceResponse.shape,
throws: withFetchErrors(),
})
.fn('createToken', {
description: 'mints a sync-service connection token for a collab surface',
args: schemas.createCollabSurfaceTokenParams.shape,
result: schemas.createCollabSurfaceTokenResponse.shape,
throws: withFetchErrors(),
})
.fn('delete', {
description: 'soft-deletes a collab surface',
args: schemas.deleteCollabSurfaceParams.shape,
modifies: true,
throws: withFetchErrors(),
});
export type GetDocumentPermissionsTokenResponse = z.infer<
typeof schemas.getDocumentPermissionsTokenResponse
>;
export type ValidateDocumentPermissionsTokenResponse = z.infer<
typeof schemas.validateDocumentPermissionsTokenResponse
>;
export type CollabSurfaceResponse = z.infer<
typeof schemas.getCollabSurfaceResponse
>;
export type CollabSurfaceTokenResponse = z.infer<
typeof schemas.createCollabSurfaceTokenResponse
>;
export const StorageService = new Svc('Document++ Storage Service API')
.err('INVALID_RESPONSE', { description: 'Invalid response from server' })
.err('INVALID_DATA', { description: 'Invalid data provided' })
.err('INVALID_DOCUMENT', {
description: 'The document is missing necessary information',
fatal: true,
})
.err('INVALID_FILETYPE', {
description: 'The document accessed is not the correct type',
fatal: true,
})
.use('fetchErrors', fetchErrorsSvc)
.fn('ping', {
description: 'Ping the DSS server',
result: {
success: z.boolean().describe('Indicates if the ping was successful'),
},
throws: withFetchErrors(),
})
.fn('getUsersHistory', {
description: schemas.getHistoryHandlerResponse.description!,
result: {
data: schemas.getHistoryHandlerResponse.shape.data,
},
throws: withFetchErrors(),
})
.fn('upsertItemToUserHistory', {
description: schemas.upsertHistoryHandlerResponse.description!,
args: {
itemId: schemas.upsertHistoryHandlerParams.shape.item_id,
itemType: z.enum(ITEM_TYPES),
},
result: schemas.upsertHistoryHandlerResponse.shape.data.shape,
modifies: true,
access: { exclude: ['ai'] },
throws: withFetchErrors(),
})
.fn('removeItemFromUserHistory', {
description: schemas.deleteHistoryHandlerResponse.description!,
args: {
itemId: schemas.deleteHistoryHandlerParams.shape.item_id,
itemType: z.enum(ITEM_TYPES),
},
result: schemas.deleteHistoryHandlerResponse.shape.data.shape,
modifies: true,
access: { exclude: ['ai'] },
throws: withFetchErrors(),
})
.fn('editDocument', {
description: schemas.editDocumentParams.description!,
args: {
documentId: schemas.editDocumentParams.shape.document_id,
...schemas.editDocumentBody.shape,
},
result: schemas.editDocumentResponse.shape.data.shape,
modifies: true,
throws: withFetchErrors(),
})
.fn('getUserDocuments', {
description: schemas.getUserDocumentsHandlerResponse.description!,
args: schemas.getUserDocumentsHandlerQueryParams.shape,
result:
schemas.getUserDocumentsHandlerResponse.shape.data.unwrap().options[1]
.shape,
throws: withFetchErrors(),
})
.fn('initializeUserDocuments', {
description: schemas.initializeUserDocumentsResponse.description ?? '',
args: {},
result: schemas.initializeUserDocumentsResponse.shape,
throws: withFetchErrors(),
})
.fn('deleteDocument', {
description: schemas.deleteDocumentResponse.description!,
args: {
documentId: schemas.deleteDocumentParams.shape.document_id,
},
result: schemas.deleteDocumentResponse.shape,
modifies: true,
access: { exclude: ['ai'] },
throws: withFetchErrors(),
})
.fn('getPins', {
description: schemas.getPinsHandlerResponse.description!,
// The pins handler takes no query params: it always returns all pins.
args: {},
result: schemas.getPinsHandlerResponse.shape.data.unwrap().options[1].shape,
throws: withFetchErrors(),
})
.fn('pinItem', {
description: schemas.addPinHandlerResponse.description!,
args: {
id: z.string().describe('ID of the item to pin'),
...schemas.addPinHandlerBody.shape,
},
result: schemas.addPinHandlerResponse.shape.data.shape,
modifies: true,
throws: withFetchErrors(),
})
.fn('removePin', {
description: schemas.removePinHandlerResponse.description!,
args: {
id: z.string().describe('ID of the item to unpin'),
...schemas.removePinHandlerBody.shape,
},
result: schemas.removePinHandlerResponse.shape.data.shape,
modifies: true,
access: { exclude: ['ai'] },
throws: withFetchErrors(),
})
.fn('reorderPins', {
description: schemas.reorderPinsHandlerResponse.description!,
args: {
pins: schemas.reorderPinsHandlerBody,
},
result: schemas.reorderPinsHandlerResponse.shape.data.shape,
modifies: true,
throws: withFetchErrors(),
})
.fn('getDocumentMetadata', {
description: schemas.getDocumentVersionResponse.description!,
args: {
documentId: schemas.getDocumentVersionParams.shape.document_id,
documentVersionId:
schemas.getDocumentVersionParams.shape.document_version_id.optional(),
},
result: schemas.getDocumentVersionResponse.shape.data.shape,
throws: withFetchErrors(),
})
.fn('getDocumentGithubPullRequests', {
description: 'Get GitHub pull requests associated with a task document',
args: {
documentId: schemas.getDocumentGithubPullRequestsParams.shape.document_id,
},
result: schemas.getDocumentGithubPullRequestsResponse.shape,
throws: withFetchErrors(),
})
.fn('createDocument', {
description: schemas.createDocumentResponse.description!,
args: schemas.createDocumentBody.shape,
result: {
metadata:
schemas.createDocumentResponse.shape.data._def.left.shape
.documentMetadata,
presignedUrl:
schemas.createDocumentResponse.shape.data._def.left.shape.presignedUrl,
contentType:
schemas.createDocumentResponse.shape.data._def.right.shape.contentType,
fileType:
schemas.createDocumentResponse.shape.data._def.right.shape.fileType,
},
modifies: true,
throws: withFetchErrors('INVALID_RESPONSE'),
})
.fn('createSpreadsheetDocument', {
description: 'Create a native spreadsheet with content in sync-service.',
args: {
documentName: z.string(),
projectId: z.string().optional(),
sha: z.string(),
},
result: {
metadata:
schemas.createDocumentResponse.shape.data._def.left.shape
.documentMetadata,
},
modifies: true,
throws: withFetchErrors('INVALID_RESPONSE'),
})
.fn('copyDocument', {
description: schemas.copyDocumentResponse.description!,
args: {
documentId: schemas.copyDocumentParams.shape.document_id,
...schemas.copyDocumentQueryParams.shape,
...schemas.copyDocumentBody.shape,
},
result: schemas.copyDocumentResponse.shape.data.shape.documentMetadata,
modifies: true,
throws: withFetchErrors(),
})
.fn('uploadModificationData', {
description: 'Upload modification data',
result: {
success: z.boolean().describe('Indicates if the upload was successful'),
},
args: {} /** TODO: This seems variable... do we have a schema? */,
modifies: true,
throws: withFetchErrors(),
})
.fn('getDocumentProcessingResult', {
description: schemas.getDocumentProcessingResultParams.description!,
args: {
documentId: schemas.getDocumentProcessingResultParams.shape.document_id,
type: ProcessingResultTypeSchema,
},
result: {
preprocess: CoParseSchema.optional(),
splitTexts: TSegmentSchema.array().optional(),
},
throws: withFetchErrors('INVALID_RESPONSE'),
})
.fn('getJobProcessingResult', {
description: schemas.jobProcessingResultHandlerResponse.description!,
args: {
documentId: schemas.jobProcessingResultHandlerParams.shape.document_id,
jobId: schemas.jobProcessingResultHandlerParams.shape.job_id,
type: ProcessingResultTypeSchema,
},
result: {
preprocess: CoParseSchema.optional(),
splitTexts: TSegmentSchema.array().optional(),
},
throws: withFetchErrors('INVALID_RESPONSE'),
})
.fn('listDocuments', {
description: schemas.getDocumentListHandlerResponse.description!,
result: {
documents: schemas.getDocumentListHandlerResponse.shape.data,
},
throws: withFetchErrors(),
})
.fn('pdfSave', {
description: schemas.saveDocumentHandlerResponse.description!,
args: {
documentId: schemas.saveDocumentHandlerParams.shape.document_id,
modificationData: PdfModificationDataOnServerSchema,
sha: z.string(),
},
result:
schemas.saveDocumentHandlerResponse.shape.data.shape.documentMetadata
.shape,
modifies: true,
throws: withFetchErrors('INVALID_RESPONSE', 'INVALID_DATA'),
})
.fn('simpleSave', {
description: schemas.simpleSaveResponse.description!,
args: {
documentId: schemas.simpleSaveParams.shape.document_id,
file: z.instanceof(Blob),
},
result: schemas.simpleSaveResponse.shape.data.shape.documentMetadata.shape,
modifies: true,
throws: withFetchErrors('INVALID_RESPONSE', 'INVALID_DATA'),
})
.fn('simpleSaveText', {
description: schemas.simpleSaveResponse.description!,
args: {
documentId: schemas.simpleSaveParams.shape.document_id,
text: z.string(),
mimeType: z
.string()
.optional()
.describe(
'The mime type of the text, like "text/plain" or "text/markdown"'
),
},
result: schemas.simpleSaveResponse.shape.data.shape.documentMetadata.shape,
modifies: true,
throws: withFetchErrors('INVALID_RESPONSE', 'INVALID_DATA'),
})
.fn('getDocxFile', {
description: 'Gets the metadata and part information for a docx file',
args: {
documentId: schemas.getDocumentVersionParams.shape.document_id,
documentVersionId:
schemas.getDocumentVersionParams.shape.document_version_id.optional(),
withoutParts: z
.boolean()
.optional()
.describe('When true, parts in the result will be an empty array'),
},
result: {
parts: GetWriterPartsResponse.shape.presignedUrls,
metadata: z.object({
...schemas.getDocumentVersionResponse.shape.data.shape.documentMetadata
.shape,
documentBom: nonNullShape(
schemas.getDocumentVersionResponse.shape.data.shape.documentMetadata
.shape.documentBom
),
}),
userAccessLevel:
schemas.getDocumentVersionResponse.shape.data.shape.userAccessLevel.describe(
'The level of access the user currently has for this document'
),
canEdit: z
.boolean()
.describe('Whether the current user can edit the document'),
},
throws: withFetchErrors('INVALID_DOCUMENT', 'INVALID_FILETYPE'),
})
.fn('getBinaryDocument', {
description:
'Gets the access level, blob URL, and metadata for a binary document',
args: {
documentId: schemas.getDocumentVersionParams.shape.document_id,
},
result: schemas.getDocumentVersionResponse.shape.data.extend({
blobUrl: z.string().describe('The presigned url of the binary blob'),
}).shape,
throws: withFetchErrors('INVALID_DOCUMENT'),
})
.fn('getTextDocument', {
description:
'Gets the access level, text, and metadata for a non-binary (text) document',
args: {
documentId: schemas.getDocumentVersionParams.shape.document_id,
},
result: {
text: z.string().describe('The text of the document'),
documentMetadata:
schemas.getDocumentVersionResponse.shape.data.shape.documentMetadata,
userAccessLevel:
schemas.getDocumentVersionResponse.shape.data.shape.userAccessLevel.describe(
'The level of access the user currently has for this document'
),
},
throws: withFetchErrors('INVALID_DOCUMENT'),
})
.fn('getWriterPartUrls', {
description:
'Get the presigned URLs for the parts of a docx writer document',
args: {
uuid: z.string().describe(`Document UUID`),
versionId: z.string().describe(`Document Version ID`),
},
result: GetWriterPartsResponse.shape,
throws: withFetchErrors(),
})
.fn('getDocumentLocation', {
description: 'Get the presigned URL(s) for the document. aka location',
args: {
documentId:
schemas.getLocationHandlerParams.shape.document_id.describe(
`Document UUID`
),
versionId: schemas.getLocationHandlerQueryParams.shape.document_version_id
.optional()
.describe(`A specific document version id to get the location for.`),
},
result: z.object({ data: z.any() }).shape,
throws: withFetchErrors(),
})
.fn('getDocumentPermissions', {
description: 'Get the document share permissions',
args: schemas.getDocumentPermissionsV2Params.shape,
result:
schemas.getDocumentPermissionsV2Response.shape.documentPermissions.shape,
throws: withFetchErrors(),
})
.fn('getDocumentViewers', {
description: 'Get the list of users who have viewed a given document',
args: schemas.getDocumentViewsHandlerParams.shape,
result: schemas.getDocumentViewsHandlerResponse.shape,
throws: withFetchErrors(),
})
.fn('getBatchDocumentPreviews', {
description: 'Get a list of previews for a list of document',
args: schemas.getBatchPreviewHandlerBody.shape,
result: schemas.getBatchPreviewHandlerResponse.shape,
throws: withFetchErrors(),
})
.fn('upsertDocumentViewLocation', {
description: 'Set the view location for a document',
args: {
documentId:
schemas.upsertUserDocumentViewLocationParams.shape.document_id,
location: schemas.upsertUserDocumentViewLocationBody.shape.location,
},
result: schemas.upsertUserDocumentViewLocationResponse.shape,
modifies: true,
throws: withFetchErrors(),
})
.fn('deleteDocumentViewLocation', {
description: 'Delete the view location for a document',
args: {
documentId:
schemas.deleteUserDocumentViewLocationParams.shape.document_id,
},
result: schemas.deleteUserDocumentViewLocationResponse.shape,
modifies: true,
throws: withFetchErrors(),
})
.fn('getDeletedItems', {
description: 'Get the list of deleted items',
result: schemas.recentlyDeletedResponse.shape.data.shape,
throws: withFetchErrors(),
})
.fn('permanentlyDeleteDocument', {
description: 'Permanently delete a document',
args: {
documentId: schemas.permanentlyDeleteDocumentParams.shape.document_id,
},
result: schemas.permanentlyDeleteDocumentResponse.shape.data.shape,
modifies: true,
throws: withFetchErrors(),
})
.fn('revertDocumentDelete', {
description: 'Revert the deletion of a document',
args: {
documentId: schemas.revertDeleteDocumentParams.shape.document_id,
},
result: schemas.revertDeleteDocumentResponse.shape.data.shape,
modifies: true,
throws: withFetchErrors(),
})
.fn('exportDocument', {
description: 'Export a document',
args: {
documentId: schemas.exportDocumentParams.shape.document_id,
},
result: schemas.exportDocumentResponse.shape,
throws: withFetchErrors(),
})
.fn('editThread', {
description: schemas.editThreadV2Params.description!,
args: {
threadId: schemas.editThreadV2Params.shape.thread_id,
...schemas.editThreadV2Body.shape,
},
result: schemas.editThreadV2Response.shape.data.shape,
modifies: true,
throws: withFetchErrors(),
})
.use('annotations', AnnotationsSvc)
.use('projects', ProjectsSvc)
.use('permissionsTokens', PermissionsTokensSvc)
.use('collabSurfaces', CollabSurfacesSvc)
.use('views', ViewsSvc)
.use('favorites', FavoritesSvc);
export type StorageService = typeof StorageService;
export type StorageServiceClient = ServiceClient<StorageService>;