full rewrite chat function

Signed-off-by: xiehuc <xiehuc@gmail.com>