-
Notifications
You must be signed in to change notification settings - Fork 820
Integrates You.com chatbot in web-mode using session cookie. Supports conversation history + "create" chatMode (image generation). #834
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
6497f76
837499f
25317c5
5e460e4
cfaba62
2a5c650
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,114 @@ | ||||||||||||
// path/to/services/apis/you-web.mjs | ||||||||||||
|
||||||||||||
import { pushRecord, setAbortController } from '../../services/apis/shared.mjs' | ||||||||||||
import { fetchSSE } from '../../utils/fetch-sse.mjs' | ||||||||||||
import { getUserConfig, youWebModelKeys } from '../../config/index.mjs' | ||||||||||||
import { getConversationPairs } from '../../utils/get-conversation-pairs.mjs' | ||||||||||||
import { getModelValue, isUsingModelName } from '../../utils/model-name-convert.mjs' | ||||||||||||
|
||||||||||||
/** | ||||||||||||
* @param {Runtime.Port} port | ||||||||||||
* @param {string} question | ||||||||||||
* @param {Session} session | ||||||||||||
* @param {string} sessionCookie | ||||||||||||
*/ | ||||||||||||
export async function generateAnswersWithYouWebApi(port, question, session, sessionCookie) { | ||||||||||||
const { controller, cleanController } = setAbortController(port) | ||||||||||||
const config = await getUserConfig() | ||||||||||||
const model = getModelValue(session) | ||||||||||||
|
||||||||||||
const apiUrl = 'https://you.com/api/streamingSearch' | ||||||||||||
|
||||||||||||
// Use the existing chatId from the session or generate a new one | ||||||||||||
if (!session.chatId) { | ||||||||||||
session.chatId = generateUUID() // You need a function to generate UUIDs | ||||||||||||
} | ||||||||||||
|
||||||||||||
// Always include the conversation history | ||||||||||||
const conversationContext = getConversationPairs(session.conversationRecords, false) | ||||||||||||
|
||||||||||||
const traceId = `${session.chatId}|${session.messageId}|${new Date().toISOString()}` | ||||||||||||
|
||||||||||||
const params = new URLSearchParams() | ||||||||||||
params.append('page', '1') | ||||||||||||
params.append('count', '10') | ||||||||||||
params.append('safeSearch', 'Off') | ||||||||||||
params.append('q', question) | ||||||||||||
params.append('chatId', session.chatId) // Use the existing or new chatId | ||||||||||||
params.append('traceId', traceId) | ||||||||||||
params.append('conversationTurnId', session.messageId) | ||||||||||||
params.append('selectedAiModel', model) | ||||||||||||
|
||||||||||||
// Conditional chatMode based on modelName | ||||||||||||
let chatMode = 'custom' | ||||||||||||
if (isUsingModelName('create', session)) { | ||||||||||||
chatMode = 'create' | ||||||||||||
} | ||||||||||||
params.append('selectedChatMode', chatMode) | ||||||||||||
|
||||||||||||
params.append('pastChatLength', session.conversationRecords.length.toString()) | ||||||||||||
params.append('queryTraceId', traceId) | ||||||||||||
params.append('use_personalization_extraction', 'false') | ||||||||||||
params.append('domain', 'youchat') | ||||||||||||
params.append('mkt', 'en-US') | ||||||||||||
params.append('chat', JSON.stringify(conversationContext)) | ||||||||||||
|
||||||||||||
const url = `${apiUrl}?${params.toString()}` | ||||||||||||
|
||||||||||||
let answer = '' | ||||||||||||
|
||||||||||||
await fetchSSE(url, { | ||||||||||||
method: 'GET', | ||||||||||||
signal: controller.signal, | ||||||||||||
headers: { | ||||||||||||
'Content-Type': 'text/event-stream;charset=utf-8', | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] For SSE GET requests, it's more appropriate to set an
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||
Cookie: `sessionKey=${sessionCookie}`, | ||||||||||||
'User-Agent': navigator.userAgent, | ||||||||||||
}, | ||||||||||||
onMessage(message) { | ||||||||||||
if (message.trim() === '[DONE]') { | ||||||||||||
finishMessage() | ||||||||||||
return | ||||||||||||
} | ||||||||||||
|
||||||||||||
let data | ||||||||||||
try { | ||||||||||||
data = JSON.parse(message) | ||||||||||||
} catch (error) { | ||||||||||||
console.error('SSE message parse error:', error) | ||||||||||||
return | ||||||||||||
} | ||||||||||||
|
||||||||||||
if (data.youChatToken) { | ||||||||||||
answer += data.youChatToken | ||||||||||||
port.postMessage({ answer: answer, done: false, session: null }) | ||||||||||||
} | ||||||||||||
}, | ||||||||||||
onStart() { | ||||||||||||
// Handle start if needed | ||||||||||||
}, | ||||||||||||
onEnd() { | ||||||||||||
finishMessage() | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Calling
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||
port.postMessage({ done: true }) | ||||||||||||
cleanController() | ||||||||||||
}, | ||||||||||||
onError(error) { | ||||||||||||
cleanController() | ||||||||||||
port.postMessage({ error: error.message, done: true }) | ||||||||||||
}, | ||||||||||||
}) | ||||||||||||
|
||||||||||||
function finishMessage() { | ||||||||||||
pushRecord(session, question, answer) | ||||||||||||
port.postMessage({ answer: answer, done: true, session: session }) | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
// Helper function to generate UUIDs (you can use a library for this if you prefer) | ||||||||||||
function generateUUID() { | ||||||||||||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { | ||||||||||||
var r = (Math.random() * 16) | 0, | ||||||||||||
v = c == 'x' ? r : (r & 0x3) | 0x8 | ||||||||||||
return v.toString(16) | ||||||||||||
}) | ||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Duplicate
create
key in theModels
object (also defined at line 251). Please remove or consolidate one of the definitions to avoid key collisions.Copilot uses AI. Check for mistakes.