|
7 | 7 |
|
8 | 8 | var _dg = M._docgen; |
9 | 9 |
|
| 10 | + // Helper: read multi-select search providers from card pills |
| 11 | + function getCardSearchProviders(blockIndex) { |
| 12 | + var panel = document.querySelector('.ai-search-pills-panel[data-ai-index="' + blockIndex + '"]'); |
| 13 | + if (!panel) return []; |
| 14 | + var providers = []; |
| 15 | + panel.querySelectorAll('.ai-card-search-check:checked').forEach(function (cb) { |
| 16 | + providers.push(cb.value); |
| 17 | + }); |
| 18 | + return providers; |
| 19 | + } |
| 20 | + |
10 | 21 | // ============================================== |
11 | 22 | // MODEL READINESS — ensure model is loaded before generation |
12 | 23 | // ============================================== |
|
766 | 777 | } |
767 | 778 | } |
768 | 779 |
|
| 780 | + // Read per-card multi-select search providers and perform web search if enabled |
| 781 | + var cardSearchProviders = getCardSearchProviders(blockIndex); |
| 782 | + var searchContext = ''; |
| 783 | + if (cardSearchProviders.length > 0 && M.webSearch) { |
| 784 | + try { |
| 785 | + var searchResults = await M.webSearch.performMultiSearch(block.prompt, 5, cardSearchProviders); |
| 786 | + searchContext = M.webSearch.formatResultsForLLM(searchResults); |
| 787 | + } catch (_) { /* search failed, proceed without */ } |
| 788 | + } |
| 789 | + |
769 | 790 | var hasImages = workerAttachments.length > 0; |
| 791 | + var userPrompt = buildPrompt(block, prevContent, memoryContext, hasImages, varsContext); |
| 792 | + if (searchContext) { |
| 793 | + userPrompt = 'Web research results:\n' + searchContext + '\n\nUse the above search results to inform your response. Cite sources when relevant.\n\n' + userPrompt; |
| 794 | + } |
| 795 | + |
| 796 | + var useThinking = block.type === 'Think' || block.think; |
770 | 797 | result = await M.requestAiTask({ |
771 | 798 | taskType: 'generate', |
772 | 799 | context: prevContent.substring(Math.max(0, prevContent.length - 3000)), |
773 | | - userPrompt: buildPrompt(block, prevContent, memoryContext, hasImages, varsContext), |
774 | | - enableThinking: block.type === 'Think', |
| 800 | + userPrompt: userPrompt, |
| 801 | + enableThinking: useThinking, |
775 | 802 | silent: true, |
776 | 803 | attachments: workerAttachments |
777 | 804 | }); |
| 805 | + |
| 806 | + // When Think is ON, refine: pass the result back and ask the model to enrich it |
| 807 | + if (useThinking) { |
| 808 | + var draft = cleanGeneratedOutput(result); |
| 809 | + console.log('[DocGen Think] 🧠 Refining output (' + draft.length + ' chars)...'); |
| 810 | + var refinePrompt = 'Here is a draft response:\n\n' + draft + '\n\n' |
| 811 | + + 'Improve this content by adding important details, examples, or missing information. ' |
| 812 | + + 'Keep the same structure and tone. Output the complete improved version.'; |
| 813 | + result = await M.requestAiTask({ |
| 814 | + taskType: 'generate', |
| 815 | + context: draft.substring(0, 2000), |
| 816 | + userPrompt: refinePrompt, |
| 817 | + enableThinking: false, |
| 818 | + silent: true, |
| 819 | + attachments: [] |
| 820 | + }); |
| 821 | + console.log('[DocGen Think] 🧠 Refinement done (' + cleanGeneratedOutput(result).length + ' chars)'); |
| 822 | + } |
778 | 823 | } |
779 | 824 |
|
780 | 825 | _dg.activeBlockOps.delete(blockIndex); |
|
900 | 945 | return; |
901 | 946 | } |
902 | 947 |
|
903 | | - var searchSelect = document.querySelector('.ai-agent-search-select[data-ai-index="' + blockIndex + '"]'); |
904 | | - var searchProvider = searchSelect ? searchSelect.value : 'off'; |
| 948 | + var cardSearchProviders = getCardSearchProviders(blockIndex); |
905 | 949 |
|
906 | 950 | _dg.activeBlockOps.add(blockIndex); |
907 | 951 | var agentCard = document.querySelector('.ai-placeholder-card[data-ai-index="' + blockIndex + '"]'); |
|
913 | 957 | var statusEl = stepEls[stepIdx].querySelector('.ai-agent-step-status'); |
914 | 958 | if (statusEl) { |
915 | 959 | if (status === 'running') statusEl.textContent = '⏳'; |
| 960 | + else if (status === 'reasoning') statusEl.textContent = '🤔'; |
916 | 961 | else if (status === 'done') statusEl.textContent = '✅'; |
917 | 962 | else if (status === 'error') statusEl.textContent = '❌'; |
918 | 963 | else statusEl.textContent = '⏸'; |
|
927 | 972 | var allResults = []; |
928 | 973 | var docContext = text.substring(0, block.start); |
929 | 974 |
|
| 975 | + // Check if Think mode is enabled for this Agent card |
| 976 | + var thinkToggle = agentCard ? agentCard.querySelector('.ai-think-toggle') : null; |
| 977 | + var useThinking = thinkToggle && thinkToggle.classList.contains('active'); |
| 978 | + |
930 | 979 | for (var i = 0; i < steps.length; i++) { |
931 | 980 | updateStepStatus(i, 'running'); |
932 | 981 |
|
933 | 982 | var stepPrompt = 'You are an expert writer. This is step ' + steps[i].number |
934 | 983 | + ' of ' + steps.length + ' in a multi-step writing flow.\n\n'; |
935 | 984 |
|
936 | | - if (searchProvider !== 'off' && M.webSearch) { |
| 985 | + if (cardSearchProviders.length > 0 && M.webSearch) { |
937 | 986 | try { |
938 | | - var searchResults = await M.webSearch.performSearch(steps[i].description); |
| 987 | + var searchResults = await M.webSearch.performMultiSearch(steps[i].description, 5, cardSearchProviders); |
939 | 988 | var searchContext = M.webSearch.formatResultsForLLM(searchResults); |
940 | 989 | stepPrompt += 'Web research results:\n' + searchContext + '\n\n'; |
941 | 990 | } catch (_) { /* search failed, proceed without */ } |
|
995 | 1044 | taskType: 'generate', |
996 | 1045 | context: accumulatedContext || docContext.substring(Math.max(0, docContext.length - 2000)), |
997 | 1046 | userPrompt: stepPrompt, |
998 | | - enableThinking: false, |
| 1047 | + enableThinking: useThinking, |
999 | 1048 | silent: true, |
1000 | 1049 | attachments: agentAttachments |
1001 | 1050 | }); |
1002 | 1051 |
|
| 1052 | + // When Think is ON, refine each step's output |
| 1053 | + if (useThinking) { |
| 1054 | + var draft = cleanGeneratedOutput(result); |
| 1055 | + console.log('[Agent Think] 🧠 Step ' + steps[i].number + ' refining (' + draft.length + ' chars)...'); |
| 1056 | + var refinePrompt = 'Here is a draft for step ' + steps[i].number + ':\n\n' + draft + '\n\n' |
| 1057 | + + 'Improve this content by adding important details, examples, or missing information. ' |
| 1058 | + + 'Keep the same structure and tone. Output the complete improved version.'; |
| 1059 | + result = await M.requestAiTask({ |
| 1060 | + taskType: 'generate', |
| 1061 | + context: draft.substring(0, 2000), |
| 1062 | + userPrompt: refinePrompt, |
| 1063 | + enableThinking: false, |
| 1064 | + silent: true, |
| 1065 | + attachments: [] |
| 1066 | + }); |
| 1067 | + console.log('[Agent Think] 🧠 Step ' + steps[i].number + ' refined (' + cleanGeneratedOutput(result).length + ' chars)'); |
| 1068 | + } |
| 1069 | + |
1003 | 1070 | var cleaned = cleanGeneratedOutput(result); |
1004 | 1071 | accumulatedContext += '\n\n' + cleaned; |
1005 | 1072 | allResults.push(cleaned); |
|
0 commit comments