|
1 | 1 | local cjson = require("cjson") |
2 | 2 | local types |
3 | 3 | types = require("tableshape").types |
| 4 | +local response_mt = { |
| 5 | + __index = { |
| 6 | + get_output_text = function(self) |
| 7 | + local parts = { } |
| 8 | + if self.output then |
| 9 | + local _list_0 = self.output |
| 10 | + for _index_0 = 1, #_list_0 do |
| 11 | + local block = _list_0[_index_0] |
| 12 | + if block.content then |
| 13 | + local _list_1 = block.content |
| 14 | + for _index_1 = 1, #_list_1 do |
| 15 | + local item = _list_1[_index_1] |
| 16 | + if item.type == "output_text" and item.text then |
| 17 | + table.insert(parts, item.text) |
| 18 | + end |
| 19 | + end |
| 20 | + end |
| 21 | + end |
| 22 | + elseif self.content then |
| 23 | + local _list_0 = self.content |
| 24 | + for _index_0 = 1, #_list_0 do |
| 25 | + local item = _list_0[_index_0] |
| 26 | + if item.type == "output_text" and item.text then |
| 27 | + table.insert(parts, item.text) |
| 28 | + end |
| 29 | + end |
| 30 | + end |
| 31 | + return table.concat(parts) |
| 32 | + end |
| 33 | + }, |
| 34 | + __tostring = function(self) |
| 35 | + return self:get_output_text() |
| 36 | + end |
| 37 | +} |
| 38 | +local add_response_helpers |
| 39 | +add_response_helpers = function(response) |
| 40 | + if response then |
| 41 | + setmetatable(response, response_mt) |
| 42 | + end |
| 43 | + return response |
| 44 | +end |
4 | 45 | local empty = (types["nil"] + types.literal(cjson.null)):describe("nullable") |
5 | 46 | local input_content_item = types.one_of({ |
6 | 47 | types.partial({ |
@@ -107,44 +148,6 @@ parse_response_stream_chunk = function(chunk) |
107 | 148 | } |
108 | 149 | end |
109 | 150 | end |
110 | | -local extract_output_text |
111 | | -extract_output_text = function(response) |
112 | | - if not (response) then |
113 | | - return "" |
114 | | - end |
115 | | - local parts = { } |
116 | | - if response.output then |
117 | | - local _list_0 = response.output |
118 | | - for _index_0 = 1, #_list_0 do |
119 | | - local block = _list_0[_index_0] |
120 | | - if block.content then |
121 | | - local _list_1 = block.content |
122 | | - for _index_1 = 1, #_list_1 do |
123 | | - local item = _list_1[_index_1] |
124 | | - if item.type == "output_text" and item.text then |
125 | | - table.insert(parts, item.text) |
126 | | - end |
127 | | - end |
128 | | - end |
129 | | - end |
130 | | - elseif response.content then |
131 | | - local _list_0 = response.content |
132 | | - for _index_0 = 1, #_list_0 do |
133 | | - local item = _list_0[_index_0] |
134 | | - if item.type == "output_text" and item.text then |
135 | | - table.insert(parts, item.text) |
136 | | - end |
137 | | - end |
138 | | - end |
139 | | - return table.concat(parts) |
140 | | -end |
141 | | -local add_response_helpers |
142 | | -add_response_helpers = function(response) |
143 | | - if response then |
144 | | - response.output_text = extract_output_text(response) |
145 | | - end |
146 | | - return response |
147 | | -end |
148 | 151 | local create_response_stream_filter |
149 | 152 | create_response_stream_filter = function(chunk_callback) |
150 | 153 | assert(types["function"](chunk_callback), "Must provide chunk_callback function when streaming response") |
|
228 | 231 | table.insert(accumulated_text, chunk.text_delta) |
229 | 232 | end |
230 | 233 | if chunk.response then |
231 | | - final_response = add_response_helpers(chunk.response) |
232 | | - chunk.response = final_response |
| 234 | + add_response_helpers(chunk.response) |
| 235 | + final_response = chunk.response |
233 | 236 | end |
234 | 237 | if stream_callback then |
235 | 238 | return stream_callback(chunk) |
@@ -284,6 +287,5 @@ return { |
284 | 287 | ResponsesChatSession = ResponsesChatSession, |
285 | 288 | parse_responses_response = parse_responses_response, |
286 | 289 | parse_response_stream_chunk = parse_response_stream_chunk, |
287 | | - create_response_stream_filter = create_response_stream_filter, |
288 | | - extract_output_text = extract_output_text |
| 290 | + create_response_stream_filter = create_response_stream_filter |
289 | 291 | } |
0 commit comments