forked from gsuuon/model.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.lua
43 lines (34 loc) · 1.02 KB
/
util.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
local sse = require('model.util.sse')
local M = {}
---@deprecated Use require('model.util.sse').client
function M.iter_sse_items(raw_data, fn)
local util = require('model.util')
local items = util.string.split_pattern(raw_data, 'data:')
-- FIXME it seems like sometimes we don't get the two newlines?
for _, item in ipairs(items) do
if #item > 0 then
fn(item)
end
end
end
---@deprecated Use require('model.util.sse').client
function M.iter_sse_messages(fn)
local pending_output = ''
return function(raw)
raw = raw:gsub('\r', '') -- handle some providers using \r
pending_output = pending_output .. '\n' .. raw
pending_output = pending_output:gsub('(.-)\n\n', function(message)
fn(sse.parse_message(message))
return '' -- replace the matched part with empty
end)
end
end
---@deprecated Use require('model.util.sse').client
function M.iter_sse_data(fn)
return M.iter_sse_messages(function(message)
if message.data ~= nil then
fn(message.data)
end
end)
end
return M