Split generateRaw into two functions exposing generateRawData (#5249) * Split generateRaw into two functions exposing generateRawData * Concise types * Improve comments * Update public/script.js Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update public/script.js Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update public/script.js Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Add GenerateRawParams typedef for improved documentation and clarity --------- Co-authored-by: Cohee <18619528+Cohee1207@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

7418d272a78c12d86afbd0f941c7590ba95ca0e5

Kristy Aurelia <120577588+KrsityKu@users.noreply.github.com>

Signed
2 files changed, +39 -17Showing whitespace changes
public/script.js+37 -17
@@ -3829,8 +3829,6 @@ export function createRawPrompt(prompt, api, instructOverride, quietToLoud, syst
38293829}
38303830
38313831/**
3832- * Generates a message using the provided prompt.
3833- * If the prompt is an array of chat-style messages and not using chat completion, it will be converted to a text prompt.
38343832 * @typedef {object} GenerateRawParams
38353833 * @prop {string | object[]} [prompt] Prompt to generate a message from. Can be a string or an array of chat-style messages, i.e. [{role: '', content: ''}, ...]
38363834 * @prop {string} [api] API to use. Main API is used if not specified.
@@ -3841,15 +3839,15 @@ export function createRawPrompt(prompt, api, instructOverride, quietToLoud, syst
38413839 * @prop {boolean} [trimNames] Whether to allow trimming "{{user}}:" and "{{char}}:" from the response.
38423840 * @prop {string} [prefill] An optional prefill for the prompt.
38433841 * @prop {object} [jsonSchema] JSON schema to use for the structured generation. Usually requires a special instruction.
3844- * @param {GenerateRawParams} params Parameters for generating a message
3845- * @returns {Promise<string>} Generated message
38463842 */
3847-export async function generateRaw({ prompt = '', api = null, instructOverride = false, quietToLoud = false, systemPrompt = '', responseLength = null, trimNames = true, prefill = '', jsonSchema = null } = {}) {
3848- if (arguments.length > 0 && typeof arguments[0] !== 'object') {
3849- console.trace('generateRaw called with positional arguments. Please use an object instead.');
3850- [prompt, api, instructOverride, quietToLoud, systemPrompt, responseLength, trimNames, prefill, jsonSchema] = arguments;
3851- }
38523843
3844+/**
3845+ * Generates a raw data object using the provided prompt.
3846+ * This used to be part of `generateRaw`, but separating it out allows extensions to access other data such as reasoning message.
3847+ * @param {GenerateRawParams} params Parameters for generating a message
3848+ * @returns {Promise<object | string>} Raw API response data, or a JSON string extracted from the response when `jsonSchema` is provided.
3849+ */
3850+export async function generateRawData({ prompt = '', api = null, instructOverride = false, quietToLoud = false, systemPrompt = '', responseLength = null, prefill = '', jsonSchema = null } = {}) {
38533851 if (!api) {
38543852 api = main_api;
38553853 }
@@ -3952,9 +3950,38 @@ export async function generateRaw({ prompt = '', api = null, instructOverride =
39523950 return extractJsonFromData(data, { mainApi: api });
39533951 }
39543952
3953+ return data;
3954+ } finally {
3955+ eventSource.removeListener(event_types.GENERATION_STOPPED, abortHook);
3956+ if (responseLengthCustomized && TempResponseLength.isCustomized()) {
3957+ TempResponseLength.restore(api);
3958+ TempResponseLength.removeEventHook(api, eventHook);
3959+ }
3960+ }
3961+}
3962+
3963+/**
3964+ * Generates a message using the provided prompt.
3965+ * If the prompt is an array of chat-style messages and not using chat completion, it will be converted to a text prompt.
3966+ * @param {GenerateRawParams} params Parameters for generating a message
3967+ * @returns {Promise<string>} Generated output: a cleaned-up message string when `jsonSchema` is not provided, or an extracted JSON string conforming to `jsonSchema` when it is.
3968+ */
3969+export async function generateRaw({ prompt = '', api = null, instructOverride = false, quietToLoud = false, systemPrompt = '', responseLength = null, trimNames = true, prefill = '', jsonSchema = null } = {}) {
3970+ if (arguments.length > 0 && typeof arguments[0] !== 'object') {
3971+ console.trace('generateRaw called with positional arguments. Please use an object instead.');
3972+ [prompt, api, instructOverride, quietToLoud, systemPrompt, responseLength, trimNames, prefill, jsonSchema] = arguments;
3973+ }
3974+
3975+ const data = await generateRawData({ prompt, api, instructOverride, quietToLoud, systemPrompt, responseLength, prefill, jsonSchema });
3976+
3977+ // JSON string (matching the provided schema) will already be extracted.
3978+ if (jsonSchema) {
3979+ return data;
3980+ }
3981+
39553982 // format result, exclude user prompt bias
39563983 const message = cleanUpMessage({
39573984 getMessage: extractMessageFromData(data, api),
39583985 isImpersonate: false,
39593986 isContinue: false,
39603987 displayIncompleteSentences: true,
@@ -3968,13 +3995,6 @@ export async function generateRaw({ prompt = '', api = null, instructOverride =
39683995 }
39693996
39703997 return message;
3971- } finally {
3972- eventSource.removeListener(event_types.GENERATION_STOPPED, abortHook);
3973- if (responseLengthCustomized && TempResponseLength.isCustomized()) {
3974- TempResponseLength.restore(api);
3975- TempResponseLength.removeEventHook(api, eventHook);
3976- }
3977- }
39783998}
39793999
39804000class TempResponseLength {
public/scripts/st-context.js+2 -0
@@ -53,6 +53,7 @@ import {
5353 swipe_right,
5454 swipe_left,
5555 generateRaw,
56+ generateRawData,
5657 showSwipeButtons,
5758 hideSwipeButtons,
5859 deleteMessage,
@@ -195,6 +196,7 @@ export function getContext() {
195196 getTokenizerModel,
196197 generateQuietPrompt,
197198 generateRaw,
199+ generateRawData,
198200 writeExtensionField,
199201 getThumbnailUrl,
200202 selectCharacterById,