|
3 | 3 | // @description Adds the magic of AI to Amazon shopping |
4 | 4 | // @author KudoAI |
5 | 5 | // @namespace https://kudoai.com |
6 | | -// @version 2025.1.26.1 |
| 6 | +// @version 2025.1.26.2 |
7 | 7 | // @license MIT |
8 | 8 | // @icon https://amazongpt.kudoai.com/assets/images/icons/amazongpt/black-gold-teal/icon48.png?v=0fddfc7 |
9 | 9 | // @icon64 https://amazongpt.kudoai.com/assets/images/icons/amazongpt/black-gold-teal/icon64.png?v=0fddfc7 |
|
226 | 226 |
|
227 | 227 | // Init API data |
228 | 228 | const apis = Object.assign(Object.create(null), await new Promise(resolve => xhr({ |
229 | | - method: 'GET', url: 'https://assets.aiwebextensions.com/data/ai-chat-apis.json?v=b529a64', |
| 229 | + method: 'GET', url: 'https://assets.aiwebextensions.com/data/ai-chat-apis.json?v=556cbfe', |
230 | 230 | onload: resp => resolve(JSON.parse(resp.responseText)) |
231 | 231 | }))) |
232 | 232 | apis.AIchatOS.userID = '#/chat/' + Date.now() |
|
2655 | 2655 | clearTimeout(this.timeout) ; this.timeout = setTimeout(handleProcessCompletion, 1500) } |
2656 | 2656 |
|
2657 | 2657 | // Process/accumulate reply chunk |
2658 | | - let replyChunk = '' |
2659 | | - if (callerAPI == 'GPTforLove') { // extract parentID + deltas |
2660 | | - const chunkObjs = respChunk.trim().split('\n').map(line => JSON.parse(line)) |
2661 | | - if (typeof chunkObjs[0].text == 'undefined') // error response |
2662 | | - replyChunk = JSON.stringify(chunkObjs[0]) // for fail flag check |
2663 | | - else { // AI response |
2664 | | - apis.GPTforLove.parentID = chunkObjs[0].id || null // for contextual replies |
2665 | | - chunkObjs.forEach(obj => replyChunk += obj.delta || '') // accumulate AI reply text |
2666 | | - if (respChunk.includes('"finish_reason":"stop"')) isDone = true |
2667 | | - } |
2668 | | - } else if (callerAPI == 'MixerBox AI') { // extract/normalize AI reply data |
2669 | | - replyChunk = [...respChunk.matchAll(/data:(.*)/g)] // arrayify data |
2670 | | - .filter(match => !/message_(?:start|end)|done/.test(match)) // exclude signals |
2671 | | - .map(match => // normalize whitespace |
2672 | | - match[1].replace(/\[SPACE\]/g, ' ').replace(/\[NEWLINE\]/g, '\n')) |
2673 | | - .join('') // stringify AI reply text |
2674 | | - if (/data:(?:message_end|done)/.test(respChunk)) isDone = true |
2675 | | - } else replyChunk = respChunk // no processing required for all other APIs |
2676 | | - textToShow += replyChunk |
| 2658 | + if (!apis[callerAPI].parsingRequired) textToShow += respChunk |
| 2659 | + else { // parse structured chunk(s) |
| 2660 | + let replyChunk = '' |
| 2661 | + if (callerAPI == 'GPTforLove') { // extract parentID + deltas |
| 2662 | + const chunkObjs = respChunk.trim().split('\n').map(line => JSON.parse(line)) |
| 2663 | + if (typeof chunkObjs[0].text == 'undefined') // error response |
| 2664 | + replyChunk = JSON.stringify(chunkObjs[0]) // for fail flag check |
| 2665 | + else { // AI response |
| 2666 | + apis.GPTforLove.parentID = chunkObjs[0].id || null // for contextual replies |
| 2667 | + chunkObjs.forEach(obj => replyChunk += obj.delta || '') // accumulate AI reply text |
| 2668 | + if (respChunk.includes('"finish_reason":"stop"')) isDone = true |
| 2669 | + } |
| 2670 | + } else if (callerAPI == 'MixerBox AI') { // extract/normalize AI reply data |
| 2671 | + replyChunk = [...respChunk.matchAll(/data:(.*)/g)] // arrayify data |
| 2672 | + .filter(match => !/message_(?:start|end)|done/.test(match)) // exclude signals |
| 2673 | + .map(match => // normalize whitespace |
| 2674 | + match[1].replace(/\[SPACE\]/g, ' ').replace(/\[NEWLINE\]/g, '\n')) |
| 2675 | + .join('') // stringify AI reply text |
| 2676 | + if (/data:(?:message_end|done)/.test(respChunk)) isDone = true |
| 2677 | + } textToShow += replyChunk |
| 2678 | + } |
2677 | 2679 |
|
2678 | 2680 | // Show accumulated reply chunks |
2679 | 2681 | try { |
|
2737 | 2739 | } catch (err) { handleProcessError(err) } |
2738 | 2740 | } |
2739 | 2741 | } else if (resp.responseText) { // show response from proxy API |
2740 | | - if (callerAPI == 'GPTforLove') { |
2741 | | - try { |
2742 | | - const chunkLines = resp.responseText.trim().split('\n'), |
2743 | | - lastChunkObj = JSON.parse(chunkLines[chunkLines.length -1]) |
2744 | | - apis.GPTforLove.parentID = lastChunkObj.id || null |
2745 | | - textToShow = lastChunkObj.text ; handleProcessCompletion() |
2746 | | - } catch (err) { handleProcessError(err) } |
2747 | | - } else if (callerAPI == 'MixerBox AI') { |
2748 | | - try { |
2749 | | - textToShow = [...resp.responseText.matchAll(/data:(.*)/g)] // arrayify data |
2750 | | - .filter(match => !/message_(?:start|end)|done/.test(match)) // exclude signals |
2751 | | - .map(match => // normalize whitespace |
2752 | | - match[1].replace(/\[SPACE\]/g, ' ').replace(/\[NEWLINE\]/g, '\n')) |
2753 | | - .join('') // stringify AI reply text |
2754 | | - handleProcessCompletion() |
2755 | | - } catch (err) { handleProcessError(err) } |
2756 | | - } else { // no processing required for all other APIs |
2757 | | - textToShow = resp.responseText ; handleProcessCompletion() } |
| 2742 | + if (!apis[callerAPI].parsingRequired) { textToShow = resp.responseText ; handleProcessCompletion } |
| 2743 | + else { // parse structured responseText |
| 2744 | + if (callerAPI == 'GPTforLove') { |
| 2745 | + try { |
| 2746 | + const chunkLines = resp.responseText.trim().split('\n'), |
| 2747 | + lastChunkObj = JSON.parse(chunkLines[chunkLines.length -1]) |
| 2748 | + apis.GPTforLove.parentID = lastChunkObj.id || null |
| 2749 | + textToShow = lastChunkObj.text ; handleProcessCompletion() |
| 2750 | + } catch (err) { handleProcessError(err) } |
| 2751 | + } else if (callerAPI == 'MixerBox AI') { |
| 2752 | + try { |
| 2753 | + textToShow = [...resp.responseText.matchAll(/data:(.*)/g)] // arrayify data |
| 2754 | + .filter(match => !/message_(?:start|end)|done/.test(match)) // exclude signals |
| 2755 | + .map(match => // normalize whitespace |
| 2756 | + match[1].replace(/\[SPACE\]/g, ' ').replace(/\[NEWLINE\]/g, '\n')) |
| 2757 | + .join('') // stringify AI reply text |
| 2758 | + handleProcessCompletion() |
| 2759 | + } catch (err) { handleProcessError(err) } |
| 2760 | + } |
| 2761 | + } |
2758 | 2762 | } else if (caller.status != 'done') { // proxy 200 response failure |
2759 | 2763 | log.info('Response text', resp.responseText) ; api.tryNew(caller) } |
2760 | 2764 |
|
|
0 commit comments