(line string, state *conversationPatchState, model string)
| 1528 | } |
| 1529 | |
| 1530 | func parseConversationEvent(line string, state *conversationPatchState, model string) (conversationStreamEvent, bool) { |
| 1531 | var raw map[string]interface{} |
| 1532 | if err := json.Unmarshal([]byte(line), &raw); err != nil { |
| 1533 | return conversationStreamEvent{}, false |
| 1534 | } |
| 1535 | |
| 1536 | if chunk, ok := chatCompletionChunkFromRaw(raw, model); ok { |
| 1537 | event := conversationStreamEvent{ |
| 1538 | chunk: &chunk, |
| 1539 | text: firstChunkContent(chunk), |
| 1540 | role: firstChunkRole(chunk), |
| 1541 | conversationID: chunk.ConversationID, |
| 1542 | channel: channelFromValue(raw), |
| 1543 | finishReason: firstChunkFinishReason(chunk), |
| 1544 | } |
| 1545 | event.isStop = event.finishReason != "" |
| 1546 | return event, true |
| 1547 | } |
| 1548 | |
| 1549 | var direct chatgpt_types.ChatGPTResponse |
| 1550 | if err := json.Unmarshal([]byte(line), &direct); err == nil && isUsableConversationResponse(direct) { |
| 1551 | channel := channelFromValue(raw) |
| 1552 | state.channel = firstNonEmpty(channel, state.channel) |
| 1553 | return conversationStreamEvent{response: direct, messageID: direct.Message.ID, channel: state.channel}, true |
| 1554 | } |
| 1555 | |
| 1556 | if response, ok := responseFromValue(raw["v"]); ok { |
| 1557 | state.response = response |
| 1558 | if channel := channelFromValue(raw["v"]); channel != "" { |
| 1559 | state.channel = channel |
| 1560 | } |
| 1561 | return conversationStreamEvent{response: state.response, messageID: state.response.Message.ID, channel: state.channel}, true |
| 1562 | } |
| 1563 | if text, ok := raw["v"].(string); ok && raw["p"] == nil && raw["o"] == nil { |
| 1564 | ensureConversationPatchDefaults(state) |
| 1565 | current, _ := state.response.Message.Content.Parts[0].(string) |
| 1566 | state.response.Message.Content.Parts[0] = current + text |
| 1567 | return conversationStreamEvent{response: state.response, messageID: state.response.Message.ID, channel: state.channel}, true |
| 1568 | } |
| 1569 | |
| 1570 | if patchPath, ok := raw["p"].(string); ok { |
| 1571 | patchOperation, _ := raw["o"].(string) |
| 1572 | if applyConversationPatch(state, patchPath, patchOperation, raw["v"]) { |
| 1573 | return conversationStreamEvent{response: state.response, messageID: state.response.Message.ID, channel: state.channel}, true |
| 1574 | } |
| 1575 | } |
| 1576 | |
| 1577 | return conversationStreamEvent{}, false |
| 1578 | } |
| 1579 | |
| 1580 | func chatCompletionChunkFromRaw(raw map[string]interface{}, model string) (official_types.ChatCompletionChunk, bool) { |
| 1581 | choices, ok := raw["choices"].([]interface{}) |
no test coverage detected