useInferenceResponse.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import {computed, reactive, ref} from 'vue'
  2. export function useInferenceResponse() {
  3. const status = ref(null)
  4. const think = computed(() => buffer('think'))
  5. const content = computed(() => buffer('content'))
  6. let chunks = reactive({'think': [], 'content': []})
  7. function append(chunk, index, type) {
  8. setStatus(type)
  9. type = type === 2 ? 'think' : 'content'
  10. chunks[type].push({index: index, chunk: chunk})
  11. }
  12. function setStatus(value) {
  13. status.value = value
  14. }
  15. function reset() {
  16. status.value = null
  17. chunks.think = []
  18. chunks.content = []
  19. }
  20. function buffer(type) {
  21. if (!chunks[type].length) return '';
  22. const [{index: firstIndex}] = chunks[type].slice().sort((a, b) => a.index - b.index);
  23. return chunks[type]
  24. .slice()
  25. .sort((a, b) => a.index - b.index)
  26. .filter(({index}, i) => index === firstIndex + i)
  27. .map(({chunk}) => chunk)
  28. .join('');
  29. }
  30. return {think, content, status, append, reset, setStatus}
  31. }