Edits history of script submission #8989 for ' Add fragment to conversation (dust)'

  • nativets
    One script reply has been approved by the moderators
    Ap­pro­ved
    type Dust = {
    	apiKey: string
    	workspaceId: string
    }
    
    export async function main(
    	resource: Dust,
    	conversationId: string,
    	body: {
    		title: string
    		content: string | Buffer
    		url: string | null
    		contentType:
    			| 'text/plain'
    			| 'text/csv'
    			| 'text/tsv'
    			| 'text/comma-separated-values'
    			| 'text/tab-separated-values'
    			| 'text/markdown'
    			| 'application/pdf'
    			| 'file_attachment'
    			| 'image/jpeg'
    			| 'image/png'
    		context: {
    			username: string
    			timezone: string
    			fullName: string | null
    			email: string | null
    			profilePictureUrl: string | null
    			origin?: 'slack' | 'web' | 'api'
    		} | null
    	}
    ) {
    	const endpoint = `https://dust.tt/api/v1/w/${resource.workspaceId}/assistant/conversations/${conversationId}/content_fragments`
    
    	let content: string
    
    	// Convert Buffer to base64 string for binary content types
    	if (body.content instanceof Buffer) {
    		content = body.content.toString('base64')
    	} else {
    		content = body.content
    	}
    
    	const requestBody = {
    		...body,
    		content
    	}
    
    	const response = await fetch(endpoint, {
    		method: 'POST',
    		headers: {
    			'Content-Type': 'application/json',
    			Authorization: `Bearer ${resource.apiKey}`
    		},
    		body: JSON.stringify(requestBody)
    	})
    
    	if (!response.ok) {
    		throw new Error(`HTTP error! status: ${response.status}`)
    	}
    
    	const data = await response.json()
    	return data.contentFragment
    }
    

    Submitted by hugo697 652 days ago