Loading js/GoogleChromeChatProvider.js +20 −6 Original line number Diff line number Diff line Loading @@ -297,11 +297,19 @@ const BrowserAiProvider = (function () { * * @param {string} prompt - The user prompt to send. * @param {Array} initialPrompts - Initial prompts including system messages. * @param {Object|null} responseConstraint - JSON Schema for structured output. * @returns {Promise<string>} The model's response. */ async function sendPrompt(prompt, initialPrompts = []) { async function sendPrompt(prompt, initialPrompts = [], responseConstraint = null) { const session = await createSession(initialPrompts); return await withTimeout(session.prompt(prompt), CONFIG.timeout); const options = {}; // Add responseConstraint for structured output if schema is provided. if (responseConstraint && Object.keys(responseConstraint).length > 0) { options.responseConstraint = responseConstraint; } return await withTimeout(session.prompt(prompt, options), CONFIG.timeout); } /** Loading Loading @@ -376,7 +384,7 @@ const BrowserAiProvider = (function () { * Extracts messages from a chat request, separated by role. * * @param {Object} request - The chat request object. * @returns {Object} Object containing initialPrompts and userPrompt. * @returns {Object} Object containing initialPrompts, userPrompt, and responseConstraint. */ function extractMessagesFromRequest(request) { const messages = request?.chatInput?.messages; Loading @@ -384,6 +392,11 @@ const BrowserAiProvider = (function () { const initialPrompts = []; let userPrompt = null; // Extract JSON schema for structured output (responseConstraint). const jsonSchema = request?.chatInput?.chat_structured_json_schema || null; // The schema may be wrapped with metadata (name, strict, schema) or be the schema directly. const responseConstraint = jsonSchema?.schema || jsonSchema; // Add system prompt if available. if (request?.chatInput?.system_prompt) { initialPrompts.push({ Loading @@ -405,7 +418,7 @@ const BrowserAiProvider = (function () { } } return { initialPrompts, userPrompt }; return { initialPrompts, userPrompt, responseConstraint }; } /** Loading @@ -415,7 +428,8 @@ const BrowserAiProvider = (function () { */ async function processRequest(request) { const uuid = request.requestUuid; const { initialPrompts, userPrompt } = extractMessagesFromRequest(request); const { initialPrompts, userPrompt, responseConstraint } = extractMessagesFromRequest(request); console.log('test', responseConstraint); if (!userPrompt) { console.error('[BrowserAiProvider] No user prompt found in request:', uuid); Loading @@ -430,7 +444,7 @@ const BrowserAiProvider = (function () { } try { const response = await sendPrompt(userPrompt, initialPrompts); const response = await sendPrompt(userPrompt, initialPrompts, responseConstraint); await storeResponse(uuid, response); } catch (error) { console.error('[BrowserAiProvider] Failed to process prompt:', error); Loading Loading
js/GoogleChromeChatProvider.js +20 −6 Original line number Diff line number Diff line Loading @@ -297,11 +297,19 @@ const BrowserAiProvider = (function () { * * @param {string} prompt - The user prompt to send. * @param {Array} initialPrompts - Initial prompts including system messages. * @param {Object|null} responseConstraint - JSON Schema for structured output. * @returns {Promise<string>} The model's response. */ async function sendPrompt(prompt, initialPrompts = []) { async function sendPrompt(prompt, initialPrompts = [], responseConstraint = null) { const session = await createSession(initialPrompts); return await withTimeout(session.prompt(prompt), CONFIG.timeout); const options = {}; // Add responseConstraint for structured output if schema is provided. if (responseConstraint && Object.keys(responseConstraint).length > 0) { options.responseConstraint = responseConstraint; } return await withTimeout(session.prompt(prompt, options), CONFIG.timeout); } /** Loading Loading @@ -376,7 +384,7 @@ const BrowserAiProvider = (function () { * Extracts messages from a chat request, separated by role. * * @param {Object} request - The chat request object. * @returns {Object} Object containing initialPrompts and userPrompt. * @returns {Object} Object containing initialPrompts, userPrompt, and responseConstraint. */ function extractMessagesFromRequest(request) { const messages = request?.chatInput?.messages; Loading @@ -384,6 +392,11 @@ const BrowserAiProvider = (function () { const initialPrompts = []; let userPrompt = null; // Extract JSON schema for structured output (responseConstraint). const jsonSchema = request?.chatInput?.chat_structured_json_schema || null; // The schema may be wrapped with metadata (name, strict, schema) or be the schema directly. const responseConstraint = jsonSchema?.schema || jsonSchema; // Add system prompt if available. if (request?.chatInput?.system_prompt) { initialPrompts.push({ Loading @@ -405,7 +418,7 @@ const BrowserAiProvider = (function () { } } return { initialPrompts, userPrompt }; return { initialPrompts, userPrompt, responseConstraint }; } /** Loading @@ -415,7 +428,8 @@ const BrowserAiProvider = (function () { */ async function processRequest(request) { const uuid = request.requestUuid; const { initialPrompts, userPrompt } = extractMessagesFromRequest(request); const { initialPrompts, userPrompt, responseConstraint } = extractMessagesFromRequest(request); console.log('test', responseConstraint); if (!userPrompt) { console.error('[BrowserAiProvider] No user prompt found in request:', uuid); Loading @@ -430,7 +444,7 @@ const BrowserAiProvider = (function () { } try { const response = await sendPrompt(userPrompt, initialPrompts); const response = await sendPrompt(userPrompt, initialPrompts, responseConstraint); await storeResponse(uuid, response); } catch (error) { console.error('[BrowserAiProvider] Failed to process prompt:', error); Loading