sillytavern-utils-lib 1.0.65 → 1.0.66
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/prompt-builder.cjs +5 -5
- package/dist/prompt-builder.d.ts.map +1 -1
- package/dist/prompt-builder.js +207 -199
- package/dist/prompt-message-utils.cjs +2 -0
- package/dist/prompt-message-utils.d.ts +6 -0
- package/dist/prompt-message-utils.d.ts.map +1 -0
- package/dist/prompt-message-utils.js +26 -0
- package/package.json +1 -1
package/dist/prompt-builder.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var Re=Object.defineProperty;var Se=(u,r,o)=>r in u?Re(u,r,{enumerable:!0,configurable:!0,writable:!0,value:o}):u[r]=o;var U=(u,r,o)=>Se(u,typeof r!="symbol"?r+"":r,o);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const n=require("./config.cjs"),Oe=require("./tokenizer.cjs"),I=require("./prompt-message-utils.cjs"),de=require("../../../regex/engine.js"),_e=require("../../../../world-info.js"),a=require("../../../../../script.js"),he=require("../../../../power-user.js"),Pe=require("../../../../group-chats.js");class Ae{constructor(r){U(this,"messages",[]);U(this,"tokenizer");U(this,"maxContext");U(this,"currentTokenCount",0);this.tokenizer=new Oe.Tokenizer,this.maxContext=r}getTokenCount(r){var m,g;const o=I.getMessageText(r);return o?((g=(m=r.source)==null?void 0:m.extra)==null?void 0:g.token_count)??this.tokenizer.encode(o).length:0}canFit(r){return this.currentTokenCount+this.getTokenCount(r)<=this.maxContext}add(r){const o=I.sanitizePromptMessage(r);if(!o)return!0;r=o;const m=this.getTokenCount(r);return this.currentTokenCount+m>this.maxContext?!1:(this.messages.push(r),this.currentTokenCount+=m,!0)}addFront(r){const o=I.sanitizePromptMessage(r);if(!o)return!0;r=o;const m=this.getTokenCount(r);return this.currentTokenCount+m>this.maxContext?!1:(this.messages.unshift(r),this.currentTokenCount+=m,!0)}addMany(r){const o=I.sanitizePromptMessages(r),m=o.map(d=>this.getTokenCount(d)),g=m.reduce((d,y)=>d+y,0);if(this.currentTokenCount+g<=this.maxContext)return this.messages.push(...o),this.currentTokenCount+=g,!0;let v=0;const f=[];for(let d=o.length-1;d>=0;d--){const y=o[d],E=m[d];if(this.currentTokenCount+v+E<=this.maxContext)f.unshift(y),v+=E;else break}return f.length>0&&(this.messages.push(...f),this.currentTokenCount+=v),f.length===o.length}insert(r,o){const m=I.sanitizePromptMessage(o);if(!m)return!0;o=m;const g=this.getTokenCount(o);return this.currentTokenCount+g>this.maxContext?!1:(this.messages.splice(r,0,o),this.currentTokenCount+=g,!0)}getMessages(){return this.messages}}async function Ue(u,{targetCharacterId:r,presetName:o,instructName:m,contextName:g,syspromptName:v,maxContext:f,includeNames:d,ignoreCharacterFields:y,ignoreAuthorNote:E,ignoreWorldInfo:J,messageIndexesBetween:w}={}){var ee,te,oe,se,ne,re,ie,ae,pe,ce,le,me,ue,fe;if(!["textgenerationwebui","openai"].includes(u))throw new Error("Unsupported API");const s=SillyTavern.getContext();let{description:F,personality:$,persona:K,scenario:H,mesExamples:ye,system:x,jailbreak:xe}=y?{description:"",personality:"",persona:"",scenario:"",mesExamples:"",system:"",jailbreak:""}:s.getCharacterCardFields({chid:r});const R=u==="textgenerationwebui"?(ee=s.getPresetManager("instruct"))==null?void 0:ee.getCompletionPresetByName(m):void 0,W=!!(R!=null&&R.enabled);let M=n.st_parseMesExamples(ye,W);function Me(){var t,l;if(typeof f=="number")return f;if(!f||f==="active"||!o)return n.st_getMaxContextSize();if(typeof f=="number")return f;let e;if(u==="textgenerationwebui"){const i=(t=s.getPresetManager("textgenerationwebui"))==null?void 0:t.getCompletionPresetByName(o);e=i==null?void 0:i.max_length}else{const i=(l=s.getPresetManager("openai"))==null?void 0:l.getCompletionPresetByName(o);e=i==null?void 0:i.openai_max_context}return typeof e=="number"?e:n.st_getMaxContextSize()}let C=[];const G=Me();if(G<=0)return{result:[],warnings:C};const c=new Ae(G),Ce=s.ToolManager.isToolCallingSupported(),L=(w==null?void 0:w.start)??0,V=w!=null&&w.end?w.end+1:void 0;let b=L===-1&&V===0?[]:s.chat.slice(L,V).filter(e=>{var t;return!e.is_system||Ce&&Array.isArray((t=e.extra)==null?void 0:t.tool_invocations)});b=await Promise.all(b.map(async(e,t)=>{var z,D;let l=e.mes,i=e.is_user?de.regex_placement.USER_INPUT:de.regex_placement.AI_OUTPUT,p={isPrompt:!0,depth:b.length-t-1},_=n.st_getRegexedString(l,i,p);return _=await n.st_appendFileContent(e,_),(z=e==null?void 0:e.extra)!=null&&z.append_title&&((D=e==null?void 0:e.extra)!=null&&D.title)&&(_=`${_}
|
|
2
2
|
|
|
3
|
-
${
|
|
4
|
-
`)})}if(!
|
|
5
|
-
`)}),
|
|
6
|
-
`)})),{result:c.getMessages(),warnings:
|
|
3
|
+
${e.extra.title}`),{...e,mes:_,index:t}}));const Te=b.map(e=>_e.world_info_include_names?`${e.name}: ${e.mes}`:e.mes).reverse(),{worldInfoString:Ie,worldInfoBefore:N,worldInfoAfter:j,worldInfoExamples:ke,worldInfoDepth:we,anBefore:X,anAfter:Y}=J?{worldInfoString:"",worldInfoBefore:"",worldInfoAfter:"",worldInfoExamples:[],worldInfoDepth:[],anBefore:[],anAfter:[]}:await s.getWorldInfoPrompt(Te,G,!1);for(const e of ke){const t=e.content;if(t.length===0)continue;const l=n.st_baseChatReplace(t,a.name1,a.name2),i=n.st_parseMesExamples(l,W);e.position===_e.wi_anchor_position.before?M.unshift(...i):M.push(...i)}function Z(){const e=[];for(let t=b.length-1;t>=0;t--){const l=b[t],i=l.name==="System"&&!l.is_user?"system":l.is_user?"user":"assistant";e.unshift({role:i,content:d&&i!="system"?`${l.name}: ${l.mes}`:l.mes,source:l})}c.addMany(e)}if(u==="textgenerationwebui"){const e=[...M];M&&(M=n.st_formatInstructModeExamples(M,a.name1,a.name2));const t=(te=s.getPresetManager("sysprompt"))==null?void 0:te.getCompletionPresetByName(v);t&&(x=s.powerUserSettings.prefer_character_prompt&&x?x:n.st_baseChatReplace(t.content,a.name1,a.name2),x=W?n.st_formatInstructModeSystemPrompt(s.substituteParams(x,a.name1,a.name2,t.content),R):x);const l={description:F,personality:$,persona:s.powerUserSettings.persona_description_position==he.persona_description_positions.IN_PROMPT?K:"",scenario:H,system:x,char:a.name2,user:a.name1,wiBefore:N,wiAfter:j,loreBefore:N,loreAfter:j,mesExamples:M.join(""),mesExamplesRaw:e.join("")},i=(oe=s.getPresetManager("context"))==null?void 0:oe.getCompletionPresetByName(g);let p=n.st_renderStoryString(l,{customInstructSettings:R,customStoryString:i==null?void 0:i.story_string});p&&c.add({role:"system",content:p,ignoreInstruct:!0}),Z()}else{let e=function(P){const k=h.find(Q=>Q.identifier===P);if(k)return k;const ge=p.prompts.find(Q=>Q.identifier===P);if(ge)return ge},t=n.st_setOpenAIMessages(b),l=n.st_setOpenAIMessageExamples(M);async function i(){let[P,k]=await n.st_prepareOpenAIMessages({name2:a.name2,charDescription:F,charPersonality:$,Scenario:H,worldInfoBefore:N,worldInfoAfter:j,extensionPrompts:s.extensionPrompts,bias:"",type:"normal",quietPrompt:void 0,quietImage:void 0,cyclePrompt:"",systemPromptOverride:x,jailbreakPromptOverride:xe,personaDescription:K,messages:t,messageExamples:l},!1);c.addMany(P)}if(!o)return C.push("No preset name provided. Using default preset."),await i(),{result:c.getMessages(),warnings:C};const p=(se=s.getPresetManager("openai"))==null?void 0:se.getCompletionPresetByName(o);if(!p)return console.warn(`Preset not found: ${o}. Using current preset.`),C.push(`Preset not found: ${o}. Using current preset.`),i(),{result:c.getMessages(),warnings:C};let _=(ne=p.prompt_order)==null?void 0:ne.find(P=>P.character_id===a.this_chid);if(!_&&p.prompt_order&&p.prompt_order.length>0&&(_=p.prompt_order[p.prompt_order.length-1]),!_)return console.warn(`No prompt order found for preset: ${o}. Using current preset.`),C.push(`No prompt order found for preset: ${o}. Using current preset.`),i(),{result:c.getMessages(),warnings:C};const z=H&&p.scenario_format?s.substituteParams(p.scenario_format):"",D=$&&p.personality_format?s.substituteParams(p.personality_format):"",ve=s.substituteParams(p.group_nudge_prompt),Ee=p.impersonation_prompt?s.substituteParams(p.impersonation_prompt):"",h=[];J||h.push({role:"system",content:n.st_formatWorldInfo(N,{wiFormat:p.wi_format}),identifier:"worldInfoBefore"},{role:"system",content:n.st_formatWorldInfo(j,{wiFormat:p.wi_format}),identifier:"worldInfoAfter"}),y||h.push({role:"system",content:F,identifier:"charDescription"},{role:"system",content:D,identifier:"charPersonality"},{role:"system",content:z,identifier:"scenario"}),h.push({role:"system",content:Ee,identifier:"impersonate"},{role:"system",content:ve,identifier:"groupNudge"});const S=s.extensionPrompts["1_memory"];S&&S.value&&h.push({role:n.st_getPromptRole(S.role),content:S.value,identifier:"summary",position:n.st_getPromptPosition(S.position)});const O=s.extensionPrompts["2_floating_prompt"];!E&&O&&O.value&&h.push({role:n.st_getPromptRole(O.role),content:O.value,identifier:"authorsNote",position:n.st_getPromptPosition(O.position)});const q=s.extensionPrompts["3_vectors"];q&&q.value&&h.push({role:"system",content:q.value,identifier:"vectorsMemory",position:n.st_getPromptPosition(q.position)});const A=s.extensionPrompts["4_vectors_data_bank"];A&&A.value&&h.push({role:n.st_getPromptRole(A.role),content:A.value,identifier:"vectorsDataBank",position:n.st_getPromptPosition(A.position)});const B=s.extensionPrompts.chromadb;B&&B.value&&h.push({role:"system",content:B.value,identifier:"smartContext",position:n.st_getPromptPosition(B.position)}),!y&&s.powerUserSettings.persona_description&&s.powerUserSettings.persona_description_position===he.persona_description_positions.IN_PROMPT&&h.push({role:"system",content:s.powerUserSettings.persona_description,identifier:"personaDescription"}),_.order.forEach(P=>{if(!P.enabled)return;const k=e(P.identifier);if(k&&k.content){c.add({role:k.role??"system",content:s.substituteParams(k.content)});return}P.identifier==="chatHistory"&&Z()})}const be=["1_memory","2_floating_prompt","3_vectors","4_vectors_data_bank","chromadb","PERSONA_DESCRIPTION","QUIET_PROMPT","DEPTH_PROMPT"];for(const e in s.extensionPrompts)if(Object.hasOwn(s.extensionPrompts,e)){const t=s.extensionPrompts[e];if(be.includes(e)||!s.extensionPrompts[e].value||![a.extension_prompt_types.BEFORE_PROMPT,a.extension_prompt_types.IN_PROMPT].includes(t.position)||typeof t.filter=="function"&&!await t.filter())continue;const i={role:n.st_getPromptRole(t.role)??"system",content:t.value};if(t.position===a.extension_prompt_types.BEFORE_PROMPT)c.insert(t.depth,i);else if(t.position===a.extension_prompt_types.IN_PROMPT){const p=c.getMessages();c.insert(p.length-t.depth,i)}}for(const e of we){const t=c.getMessages();c.insert(t.length-e.depth,{role:n.st_getPromptRole(e.role),content:e.entries.join(`
|
|
4
|
+
`)})}if(!y){const e=n.st_getGroupDepthPrompts(Pe.selected_group,Number(a.this_chid));if(Pe.selected_group&&Array.isArray(e)&&e.length>0)e.filter(t=>t.text).forEach((t,l)=>{const i=c.getMessages();c.insert(i.length-t.depth,{role:t.role,content:t.text})});else{const t=n.st_baseChatReplace((ce=(pe=(ae=(ie=(re=s.characters[a.this_chid])==null?void 0:re.data)==null?void 0:ie.extensions)==null?void 0:ae.depth_prompt)==null?void 0:pe.prompt)==null?void 0:ce.trim(),a.name1,a.name2)||"";if(t){const l=a.depth_prompt_depth_default,i=((fe=(ue=(me=(le=s.characters[a.this_chid])==null?void 0:le.data)==null?void 0:me.extensions)==null?void 0:ue.depth_prompt)==null?void 0:fe.role)??a.depth_prompt_role_default,p=c.getMessages();c.insert(p.length-l,{role:n.st_getPromptRole(i),content:t})}}}let T=-1;if(!E){const e=n.st_getAuthorNote();if(e.prompt){e.prompt=n.st_baseChatReplace(e.prompt,a.name1,a.name2);const t={role:n.st_getPromptRole(e.role),content:e.prompt};switch(e.position){case a.extension_prompt_types.IN_PROMPT:c.insert(1,t),T=1;break;case a.extension_prompt_types.IN_CHAT:T=c.getMessages().length-e.depth,c.insert(T,t);break;case a.extension_prompt_types.BEFORE_PROMPT:c.addFront(t),T=0;break}}}return T>=0&&(X.length>0&&(c.insert(T,{role:"system",content:X.join(`
|
|
5
|
+
`)}),T++),Y.length>0&&c.insert(T+1,{role:"system",content:Y.join(`
|
|
6
|
+
`)})),{result:c.getMessages(),warnings:C}}exports.buildPrompt=Ue;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prompt-builder.d.ts","sourceRoot":"","sources":["../src/prompt-builder.ts"],"names":[],"mappings":"AA+BA,OAAO,EAAE,qBAAqB,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"prompt-builder.d.ts","sourceRoot":"","sources":["../src/prompt-builder.ts"],"names":[],"mappings":"AA+BA,OAAO,EAAE,qBAAqB,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAOtE,MAAM,WAAW,OAAQ,SAAQ,qBAAqB;IACpD,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB;AAED,MAAM,WAAW,kBAAkB;IACjC,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,UAAU,CAAC,EAAE,MAAM,GAAG,QAAQ,GAAG,QAAQ,CAAC;IAC1C,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,qBAAqB,CAAC,EAAE,OAAO,CAAC;IAChC,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B,eAAe,CAAC,EAAE,OAAO,CAAC;IAE1B;;OAEG;IACH,qBAAqB,CAAC,EAAE;QACtB,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,GAAG,CAAC,EAAE,MAAM,CAAC;KACd,CAAC;CACH;AA0GD;;;;;;GAMG;AACH,wBAAsB,WAAW,CAC/B,GAAG,EAAE,MAAM,EACX,EACE,iBAAiB,EACjB,UAAU,EACV,YAAY,EACZ,WAAW,EACX,aAAa,EACb,UAAU,EACV,YAAY,EACZ,qBAAqB,EACrB,gBAAgB,EAChB,eAAe,EACf,qBAAqB,GACtB,GAAE,kBAAuB,GACzB,OAAO,CAAC;IAAE,MAAM,EAAE,OAAO,EAAE,CAAC;IAAC,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAA;CAAE,CAAC,CAsgBrD"}
|
package/dist/prompt-builder.js
CHANGED
|
@@ -1,80 +1,88 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
import { st_parseMesExamples as
|
|
5
|
-
import { Tokenizer as
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
this
|
|
1
|
+
var Dt = Object.defineProperty;
|
|
2
|
+
var zt = (l, n, o) => n in l ? Dt(l, n, { enumerable: !0, configurable: !0, writable: !0, value: o }) : l[n] = o;
|
|
3
|
+
var I = (l, n, o) => zt(l, typeof n != "symbol" ? n + "" : n, o);
|
|
4
|
+
import { st_parseMesExamples as Mt, st_getRegexedString as Bt, st_appendFileContent as Ft, st_baseChatReplace as q, st_formatInstructModeExamples as $t, st_formatInstructModeSystemPrompt as jt, st_renderStoryString as Ht, st_setOpenAIMessages as qt, st_setOpenAIMessageExamples as Wt, st_formatWorldInfo as Tt, st_getPromptPosition as D, st_getPromptRole as b, st_getGroupDepthPrompts as Gt, st_getAuthorNote as Qt, st_getMaxContextSize as X, st_prepareOpenAIMessages as Jt } from "./config.js";
|
|
5
|
+
import { Tokenizer as Kt } from "./tokenizer.js";
|
|
6
|
+
import { getMessageText as Lt, sanitizePromptMessage as Y, sanitizePromptMessages as Vt } from "./prompt-message-utils.js";
|
|
7
|
+
import { regex_placement as Ct } from "../../../regex/engine.js";
|
|
8
|
+
import { world_info_include_names as Xt, wi_anchor_position as Yt } from "../../../../world-info.js";
|
|
9
|
+
import { name1 as v, name2 as C, this_chid as W, extension_prompt_types as E, depth_prompt_role_default as Zt, depth_prompt_depth_default as te } from "../../../../../script.js";
|
|
10
|
+
import { persona_description_positions as kt } from "../../../../power-user.js";
|
|
11
|
+
import { selected_group as wt } from "../../../../group-chats.js";
|
|
12
|
+
class ee {
|
|
13
|
+
constructor(n) {
|
|
14
|
+
I(this, "messages", []);
|
|
15
|
+
I(this, "tokenizer");
|
|
16
|
+
I(this, "maxContext");
|
|
17
|
+
I(this, "currentTokenCount", 0);
|
|
18
|
+
this.tokenizer = new Kt(), this.maxContext = n;
|
|
18
19
|
}
|
|
19
|
-
getTokenCount(
|
|
20
|
-
var
|
|
21
|
-
|
|
20
|
+
getTokenCount(n) {
|
|
21
|
+
var c, m;
|
|
22
|
+
const o = Lt(n);
|
|
23
|
+
return o ? ((m = (c = n.source) == null ? void 0 : c.extra) == null ? void 0 : m.token_count) ?? this.tokenizer.encode(o).length : 0;
|
|
22
24
|
}
|
|
23
|
-
canFit(
|
|
24
|
-
return this.currentTokenCount + this.getTokenCount(
|
|
25
|
+
canFit(n) {
|
|
26
|
+
return this.currentTokenCount + this.getTokenCount(n) <= this.maxContext;
|
|
25
27
|
}
|
|
26
|
-
add(
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
add(n) {
|
|
29
|
+
const o = Y(n);
|
|
30
|
+
if (!o) return !0;
|
|
31
|
+
n = o;
|
|
32
|
+
const c = this.getTokenCount(n);
|
|
33
|
+
return this.currentTokenCount + c > this.maxContext ? !1 : (this.messages.push(n), this.currentTokenCount += c, !0);
|
|
30
34
|
}
|
|
31
|
-
addFront(
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
+
addFront(n) {
|
|
36
|
+
const o = Y(n);
|
|
37
|
+
if (!o) return !0;
|
|
38
|
+
n = o;
|
|
39
|
+
const c = this.getTokenCount(n);
|
|
40
|
+
return this.currentTokenCount + c > this.maxContext ? !1 : (this.messages.unshift(n), this.currentTokenCount += c, !0);
|
|
35
41
|
}
|
|
36
|
-
addMany(
|
|
37
|
-
const o =
|
|
38
|
-
if (this.currentTokenCount +
|
|
39
|
-
return this.messages.push(...o), this.currentTokenCount +=
|
|
40
|
-
let
|
|
42
|
+
addMany(n) {
|
|
43
|
+
const o = Vt(n), c = o.map((f) => this.getTokenCount(f)), m = c.reduce((f, _) => f + _, 0);
|
|
44
|
+
if (this.currentTokenCount + m <= this.maxContext)
|
|
45
|
+
return this.messages.push(...o), this.currentTokenCount += m, !0;
|
|
46
|
+
let O = 0;
|
|
41
47
|
const u = [];
|
|
42
|
-
for (let
|
|
43
|
-
const
|
|
44
|
-
if (this.currentTokenCount +
|
|
45
|
-
u.unshift(
|
|
48
|
+
for (let f = o.length - 1; f >= 0; f--) {
|
|
49
|
+
const _ = o[f], S = c[f];
|
|
50
|
+
if (this.currentTokenCount + O + S <= this.maxContext)
|
|
51
|
+
u.unshift(_), O += S;
|
|
46
52
|
else
|
|
47
53
|
break;
|
|
48
54
|
}
|
|
49
|
-
return u.length > 0 && (this.messages.push(...u), this.currentTokenCount +=
|
|
55
|
+
return u.length > 0 && (this.messages.push(...u), this.currentTokenCount += O), u.length === o.length;
|
|
50
56
|
}
|
|
51
|
-
insert(
|
|
52
|
-
|
|
57
|
+
insert(n, o) {
|
|
58
|
+
const c = Y(o);
|
|
59
|
+
if (!c) return !0;
|
|
60
|
+
o = c;
|
|
53
61
|
const m = this.getTokenCount(o);
|
|
54
|
-
return this.currentTokenCount + m > this.maxContext ? !1 : (this.messages.splice(
|
|
62
|
+
return this.currentTokenCount + m > this.maxContext ? !1 : (this.messages.splice(n, 0, o), this.currentTokenCount += m, !0);
|
|
55
63
|
}
|
|
56
64
|
getMessages() {
|
|
57
65
|
return this.messages;
|
|
58
66
|
}
|
|
59
67
|
}
|
|
60
|
-
async function
|
|
61
|
-
targetCharacterId:
|
|
68
|
+
async function fe(l, {
|
|
69
|
+
targetCharacterId: n,
|
|
62
70
|
presetName: o,
|
|
63
|
-
instructName:
|
|
64
|
-
contextName:
|
|
65
|
-
syspromptName:
|
|
71
|
+
instructName: c,
|
|
72
|
+
contextName: m,
|
|
73
|
+
syspromptName: O,
|
|
66
74
|
maxContext: u,
|
|
67
|
-
includeNames:
|
|
68
|
-
ignoreCharacterFields:
|
|
69
|
-
ignoreAuthorNote:
|
|
70
|
-
ignoreWorldInfo:
|
|
71
|
-
messageIndexesBetween:
|
|
75
|
+
includeNames: f,
|
|
76
|
+
ignoreCharacterFields: _,
|
|
77
|
+
ignoreAuthorNote: S,
|
|
78
|
+
ignoreWorldInfo: Z,
|
|
79
|
+
messageIndexesBetween: k
|
|
72
80
|
} = {}) {
|
|
73
|
-
var
|
|
81
|
+
var it, at, pt, ct, lt, ut, mt, ft, dt, gt, ht, _t, Pt, yt;
|
|
74
82
|
if (!["textgenerationwebui", "openai"].includes(l))
|
|
75
83
|
throw new Error("Unsupported API");
|
|
76
|
-
const
|
|
77
|
-
let { description: G, personality: Q, persona:
|
|
84
|
+
const s = SillyTavern.getContext();
|
|
85
|
+
let { description: G, personality: Q, persona: tt, scenario: J, mesExamples: bt, system: P, jailbreak: vt } = _ ? {
|
|
78
86
|
description: "",
|
|
79
87
|
personality: "",
|
|
80
88
|
persona: "",
|
|
@@ -82,12 +90,12 @@ async function ce(l, {
|
|
|
82
90
|
mesExamples: "",
|
|
83
91
|
system: "",
|
|
84
92
|
jailbreak: ""
|
|
85
|
-
} :
|
|
86
|
-
chid:
|
|
93
|
+
} : s.getCharacterCardFields({
|
|
94
|
+
chid: n
|
|
87
95
|
});
|
|
88
|
-
const
|
|
89
|
-
let
|
|
90
|
-
function
|
|
96
|
+
const A = l === "textgenerationwebui" ? (it = s.getPresetManager("instruct")) == null ? void 0 : it.getCompletionPresetByName(c) : void 0, K = !!(A != null && A.enabled);
|
|
97
|
+
let y = Mt(bt, K);
|
|
98
|
+
function Et() {
|
|
91
99
|
var e, p;
|
|
92
100
|
if (typeof u == "number")
|
|
93
101
|
return u;
|
|
@@ -99,37 +107,37 @@ async function ce(l, {
|
|
|
99
107
|
return u;
|
|
100
108
|
let t;
|
|
101
109
|
if (l === "textgenerationwebui") {
|
|
102
|
-
const r = (e =
|
|
110
|
+
const r = (e = s.getPresetManager("textgenerationwebui")) == null ? void 0 : e.getCompletionPresetByName(o);
|
|
103
111
|
t = r == null ? void 0 : r.max_length;
|
|
104
112
|
} else {
|
|
105
|
-
const r = (p =
|
|
113
|
+
const r = (p = s.getPresetManager("openai")) == null ? void 0 : p.getCompletionPresetByName(o);
|
|
106
114
|
t = r == null ? void 0 : r.openai_max_context;
|
|
107
115
|
}
|
|
108
116
|
return typeof t == "number" ? t : X();
|
|
109
117
|
}
|
|
110
|
-
let
|
|
111
|
-
const L =
|
|
118
|
+
let x = [];
|
|
119
|
+
const L = Et();
|
|
112
120
|
if (L <= 0)
|
|
113
|
-
return { result: [], warnings:
|
|
114
|
-
const a = new
|
|
115
|
-
let
|
|
121
|
+
return { result: [], warnings: x };
|
|
122
|
+
const a = new ee(L), Ot = s.ToolManager.isToolCallingSupported(), et = (k == null ? void 0 : k.start) ?? 0, ot = k != null && k.end ? k.end + 1 : void 0;
|
|
123
|
+
let w = et === -1 && ot === 0 ? [] : s.chat.slice(et, ot).filter((t) => {
|
|
116
124
|
var e;
|
|
117
|
-
return !t.is_system ||
|
|
125
|
+
return !t.is_system || Ot && Array.isArray((e = t.extra) == null ? void 0 : e.tool_invocations);
|
|
118
126
|
});
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
var
|
|
122
|
-
let p = t.mes, r = t.is_user ?
|
|
123
|
-
return
|
|
127
|
+
w = await Promise.all(
|
|
128
|
+
w.map(async (t, e) => {
|
|
129
|
+
var F, $;
|
|
130
|
+
let p = t.mes, r = t.is_user ? Ct.USER_INPUT : Ct.AI_OUTPUT, i = { isPrompt: !0, depth: w.length - e - 1 }, d = Bt(p, r, i);
|
|
131
|
+
return d = await Ft(t, d), (F = t == null ? void 0 : t.extra) != null && F.append_title && (($ = t == null ? void 0 : t.extra) != null && $.title) && (d = `${d}
|
|
124
132
|
|
|
125
133
|
${t.extra.title}`), {
|
|
126
134
|
...t,
|
|
127
|
-
mes:
|
|
135
|
+
mes: d,
|
|
128
136
|
index: e
|
|
129
137
|
};
|
|
130
138
|
})
|
|
131
139
|
);
|
|
132
|
-
const
|
|
140
|
+
const St = w.map((t) => Xt ? `${t.name}: ${t.mes}` : t.mes).reverse(), { worldInfoString: oe, worldInfoBefore: z, worldInfoAfter: B, worldInfoExamples: At, worldInfoDepth: Rt, anBefore: st, anAfter: nt } = Z ? {
|
|
133
141
|
worldInfoString: "",
|
|
134
142
|
worldInfoBefore: "",
|
|
135
143
|
worldInfoAfter: "",
|
|
@@ -137,169 +145,169 @@ ${t.extra.title}`), {
|
|
|
137
145
|
worldInfoDepth: [],
|
|
138
146
|
anBefore: [],
|
|
139
147
|
anAfter: []
|
|
140
|
-
} : await
|
|
141
|
-
for (const t of
|
|
148
|
+
} : await s.getWorldInfoPrompt(St, L, !1);
|
|
149
|
+
for (const t of At) {
|
|
142
150
|
const e = t.content;
|
|
143
151
|
if (e.length === 0)
|
|
144
152
|
continue;
|
|
145
|
-
const p = q(e,
|
|
146
|
-
t.position ===
|
|
153
|
+
const p = q(e, v, C), r = Mt(p, K);
|
|
154
|
+
t.position === Yt.before ? y.unshift(...r) : y.push(...r);
|
|
147
155
|
}
|
|
148
156
|
function rt() {
|
|
149
157
|
const t = [];
|
|
150
|
-
for (let e =
|
|
151
|
-
const p =
|
|
158
|
+
for (let e = w.length - 1; e >= 0; e--) {
|
|
159
|
+
const p = w[e], r = p.name === "System" && !p.is_user ? "system" : p.is_user ? "user" : "assistant";
|
|
152
160
|
t.unshift({
|
|
153
161
|
role: r,
|
|
154
|
-
content:
|
|
162
|
+
content: f && r != "system" ? `${p.name}: ${p.mes}` : p.mes,
|
|
155
163
|
source: p
|
|
156
164
|
});
|
|
157
165
|
}
|
|
158
166
|
a.addMany(t);
|
|
159
167
|
}
|
|
160
168
|
if (l === "textgenerationwebui") {
|
|
161
|
-
const t = [...
|
|
162
|
-
|
|
163
|
-
const e = (
|
|
164
|
-
e && (
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
) :
|
|
169
|
+
const t = [...y];
|
|
170
|
+
y && (y = $t(y, v, C));
|
|
171
|
+
const e = (at = s.getPresetManager("sysprompt")) == null ? void 0 : at.getCompletionPresetByName(O);
|
|
172
|
+
e && (P = s.powerUserSettings.prefer_character_prompt && P ? P : q(e.content, v, C), P = K ? jt(
|
|
173
|
+
s.substituteParams(P, v, C, e.content),
|
|
174
|
+
A
|
|
175
|
+
) : P);
|
|
168
176
|
const p = {
|
|
169
177
|
description: G,
|
|
170
178
|
personality: Q,
|
|
171
|
-
persona:
|
|
179
|
+
persona: s.powerUserSettings.persona_description_position == kt.IN_PROMPT ? tt : "",
|
|
172
180
|
scenario: J,
|
|
173
|
-
system:
|
|
174
|
-
char:
|
|
175
|
-
user:
|
|
176
|
-
wiBefore:
|
|
177
|
-
wiAfter:
|
|
178
|
-
loreBefore:
|
|
179
|
-
loreAfter:
|
|
180
|
-
mesExamples:
|
|
181
|
+
system: P,
|
|
182
|
+
char: C,
|
|
183
|
+
user: v,
|
|
184
|
+
wiBefore: z,
|
|
185
|
+
wiAfter: B,
|
|
186
|
+
loreBefore: z,
|
|
187
|
+
loreAfter: B,
|
|
188
|
+
mesExamples: y.join(""),
|
|
181
189
|
mesExamplesRaw: t.join("")
|
|
182
|
-
}, r = (
|
|
183
|
-
let i =
|
|
184
|
-
customInstructSettings:
|
|
190
|
+
}, r = (pt = s.getPresetManager("context")) == null ? void 0 : pt.getCompletionPresetByName(m);
|
|
191
|
+
let i = Ht(p, {
|
|
192
|
+
customInstructSettings: A,
|
|
185
193
|
customStoryString: r == null ? void 0 : r.story_string
|
|
186
194
|
});
|
|
187
195
|
i && a.add({ role: "system", content: i, ignoreInstruct: !0 }), rt();
|
|
188
196
|
} else {
|
|
189
|
-
let t = function(
|
|
190
|
-
const
|
|
191
|
-
if (
|
|
192
|
-
return
|
|
193
|
-
const
|
|
194
|
-
if (
|
|
195
|
-
return
|
|
196
|
-
}, e =
|
|
197
|
+
let t = function(h) {
|
|
198
|
+
const T = g.find((V) => V.identifier === h);
|
|
199
|
+
if (T)
|
|
200
|
+
return T;
|
|
201
|
+
const xt = i.prompts.find((V) => V.identifier === h);
|
|
202
|
+
if (xt)
|
|
203
|
+
return xt;
|
|
204
|
+
}, e = qt(w), p = Wt(y);
|
|
197
205
|
async function r() {
|
|
198
|
-
let [
|
|
206
|
+
let [h, T] = await Jt(
|
|
199
207
|
{
|
|
200
|
-
name2:
|
|
208
|
+
name2: C,
|
|
201
209
|
charDescription: G,
|
|
202
210
|
charPersonality: Q,
|
|
203
211
|
Scenario: J,
|
|
204
|
-
worldInfoBefore:
|
|
205
|
-
worldInfoAfter:
|
|
206
|
-
extensionPrompts:
|
|
212
|
+
worldInfoBefore: z,
|
|
213
|
+
worldInfoAfter: B,
|
|
214
|
+
extensionPrompts: s.extensionPrompts,
|
|
207
215
|
bias: "",
|
|
208
216
|
type: "normal",
|
|
209
217
|
quietPrompt: void 0,
|
|
210
218
|
quietImage: void 0,
|
|
211
219
|
cyclePrompt: "",
|
|
212
|
-
systemPromptOverride:
|
|
213
|
-
jailbreakPromptOverride:
|
|
214
|
-
personaDescription:
|
|
220
|
+
systemPromptOverride: P,
|
|
221
|
+
jailbreakPromptOverride: vt,
|
|
222
|
+
personaDescription: tt,
|
|
215
223
|
messages: e,
|
|
216
224
|
messageExamples: p
|
|
217
225
|
},
|
|
218
226
|
!1
|
|
219
227
|
);
|
|
220
|
-
a.addMany(
|
|
228
|
+
a.addMany(h);
|
|
221
229
|
}
|
|
222
230
|
if (!o)
|
|
223
|
-
return
|
|
224
|
-
const i = (
|
|
231
|
+
return x.push("No preset name provided. Using default preset."), await r(), { result: a.getMessages(), warnings: x };
|
|
232
|
+
const i = (ct = s.getPresetManager("openai")) == null ? void 0 : ct.getCompletionPresetByName(o);
|
|
225
233
|
if (!i)
|
|
226
|
-
return console.warn(`Preset not found: ${o}. Using current preset.`),
|
|
227
|
-
let
|
|
228
|
-
if (!
|
|
229
|
-
return console.warn(`No prompt order found for preset: ${o}. Using current preset.`),
|
|
230
|
-
const
|
|
231
|
-
|
|
234
|
+
return console.warn(`Preset not found: ${o}. Using current preset.`), x.push(`Preset not found: ${o}. Using current preset.`), r(), { result: a.getMessages(), warnings: x };
|
|
235
|
+
let d = (lt = i.prompt_order) == null ? void 0 : lt.find((h) => h.character_id === W);
|
|
236
|
+
if (!d && i.prompt_order && i.prompt_order.length > 0 && (d = i.prompt_order[i.prompt_order.length - 1]), !d)
|
|
237
|
+
return console.warn(`No prompt order found for preset: ${o}. Using current preset.`), x.push(`No prompt order found for preset: ${o}. Using current preset.`), r(), { result: a.getMessages(), warnings: x };
|
|
238
|
+
const F = J && i.scenario_format ? s.substituteParams(i.scenario_format) : "", $ = Q && i.personality_format ? s.substituteParams(i.personality_format) : "", Ut = s.substituteParams(i.group_nudge_prompt), It = i.impersonation_prompt ? s.substituteParams(i.impersonation_prompt) : "", g = [];
|
|
239
|
+
Z || g.push(
|
|
232
240
|
{
|
|
233
241
|
role: "system",
|
|
234
|
-
content:
|
|
242
|
+
content: Tt(z, { wiFormat: i.wi_format }),
|
|
235
243
|
identifier: "worldInfoBefore"
|
|
236
244
|
},
|
|
237
245
|
{
|
|
238
246
|
role: "system",
|
|
239
|
-
content:
|
|
247
|
+
content: Tt(B, { wiFormat: i.wi_format }),
|
|
240
248
|
identifier: "worldInfoAfter"
|
|
241
249
|
}
|
|
242
|
-
),
|
|
250
|
+
), _ || g.push(
|
|
243
251
|
{ role: "system", content: G, identifier: "charDescription" },
|
|
244
|
-
{ role: "system", content:
|
|
245
|
-
{ role: "system", content:
|
|
246
|
-
),
|
|
247
|
-
{ role: "system", content:
|
|
248
|
-
{ role: "system", content:
|
|
252
|
+
{ role: "system", content: $, identifier: "charPersonality" },
|
|
253
|
+
{ role: "system", content: F, identifier: "scenario" }
|
|
254
|
+
), g.push(
|
|
255
|
+
{ role: "system", content: It, identifier: "impersonate" },
|
|
256
|
+
{ role: "system", content: Ut, identifier: "groupNudge" }
|
|
249
257
|
);
|
|
250
|
-
const
|
|
251
|
-
|
|
252
|
-
role:
|
|
253
|
-
content:
|
|
258
|
+
const R = s.extensionPrompts["1_memory"];
|
|
259
|
+
R && R.value && g.push({
|
|
260
|
+
role: b(R.role),
|
|
261
|
+
content: R.value,
|
|
254
262
|
identifier: "summary",
|
|
255
|
-
position:
|
|
263
|
+
position: D(R.position)
|
|
256
264
|
});
|
|
257
|
-
const
|
|
258
|
-
!
|
|
259
|
-
role:
|
|
260
|
-
content:
|
|
265
|
+
const N = s.extensionPrompts["2_floating_prompt"];
|
|
266
|
+
!S && N && N.value && g.push({
|
|
267
|
+
role: b(N.role),
|
|
268
|
+
content: N.value,
|
|
261
269
|
identifier: "authorsNote",
|
|
262
|
-
position:
|
|
270
|
+
position: D(N.position)
|
|
263
271
|
});
|
|
264
|
-
const
|
|
265
|
-
|
|
272
|
+
const j = s.extensionPrompts["3_vectors"];
|
|
273
|
+
j && j.value && g.push({
|
|
266
274
|
role: "system",
|
|
267
|
-
content:
|
|
275
|
+
content: j.value,
|
|
268
276
|
identifier: "vectorsMemory",
|
|
269
|
-
position:
|
|
277
|
+
position: D(j.position)
|
|
270
278
|
});
|
|
271
|
-
const
|
|
272
|
-
|
|
273
|
-
role:
|
|
274
|
-
content:
|
|
279
|
+
const U = s.extensionPrompts["4_vectors_data_bank"];
|
|
280
|
+
U && U.value && g.push({
|
|
281
|
+
role: b(U.role),
|
|
282
|
+
content: U.value,
|
|
275
283
|
identifier: "vectorsDataBank",
|
|
276
|
-
position:
|
|
284
|
+
position: D(U.position)
|
|
277
285
|
});
|
|
278
|
-
const H =
|
|
279
|
-
H && H.value &&
|
|
286
|
+
const H = s.extensionPrompts.chromadb;
|
|
287
|
+
H && H.value && g.push({
|
|
280
288
|
role: "system",
|
|
281
289
|
content: H.value,
|
|
282
290
|
identifier: "smartContext",
|
|
283
|
-
position:
|
|
284
|
-
}), !
|
|
291
|
+
position: D(H.position)
|
|
292
|
+
}), !_ && s.powerUserSettings.persona_description && s.powerUserSettings.persona_description_position === kt.IN_PROMPT && g.push({
|
|
285
293
|
role: "system",
|
|
286
|
-
content:
|
|
294
|
+
content: s.powerUserSettings.persona_description,
|
|
287
295
|
identifier: "personaDescription"
|
|
288
|
-
}),
|
|
289
|
-
if (!
|
|
296
|
+
}), d.order.forEach((h) => {
|
|
297
|
+
if (!h.enabled)
|
|
290
298
|
return;
|
|
291
|
-
const
|
|
292
|
-
if (
|
|
299
|
+
const T = t(h.identifier);
|
|
300
|
+
if (T && T.content) {
|
|
293
301
|
a.add({
|
|
294
|
-
role:
|
|
295
|
-
content:
|
|
302
|
+
role: T.role ?? "system",
|
|
303
|
+
content: s.substituteParams(T.content)
|
|
296
304
|
});
|
|
297
305
|
return;
|
|
298
306
|
}
|
|
299
|
-
|
|
307
|
+
h.identifier === "chatHistory" && rt();
|
|
300
308
|
});
|
|
301
309
|
}
|
|
302
|
-
const
|
|
310
|
+
const Nt = [
|
|
303
311
|
"1_memory",
|
|
304
312
|
"2_floating_prompt",
|
|
305
313
|
"3_vectors",
|
|
@@ -309,74 +317,74 @@ ${t.extra.title}`), {
|
|
|
309
317
|
"QUIET_PROMPT",
|
|
310
318
|
"DEPTH_PROMPT"
|
|
311
319
|
];
|
|
312
|
-
for (const t in
|
|
313
|
-
if (Object.hasOwn(
|
|
314
|
-
const e =
|
|
315
|
-
if (
|
|
320
|
+
for (const t in s.extensionPrompts)
|
|
321
|
+
if (Object.hasOwn(s.extensionPrompts, t)) {
|
|
322
|
+
const e = s.extensionPrompts[t];
|
|
323
|
+
if (Nt.includes(t) || !s.extensionPrompts[t].value || ![E.BEFORE_PROMPT, E.IN_PROMPT].includes(e.position) || typeof e.filter == "function" && !await e.filter()) continue;
|
|
316
324
|
const r = {
|
|
317
|
-
role:
|
|
325
|
+
role: b(e.role) ?? "system",
|
|
318
326
|
content: e.value
|
|
319
327
|
};
|
|
320
|
-
if (e.position ===
|
|
328
|
+
if (e.position === E.BEFORE_PROMPT)
|
|
321
329
|
a.insert(e.depth, r);
|
|
322
|
-
else if (e.position ===
|
|
330
|
+
else if (e.position === E.IN_PROMPT) {
|
|
323
331
|
const i = a.getMessages();
|
|
324
332
|
a.insert(i.length - e.depth, r);
|
|
325
333
|
}
|
|
326
334
|
}
|
|
327
|
-
for (const t of
|
|
335
|
+
for (const t of Rt) {
|
|
328
336
|
const e = a.getMessages();
|
|
329
337
|
a.insert(e.length - t.depth, {
|
|
330
|
-
role:
|
|
338
|
+
role: b(t.role),
|
|
331
339
|
content: t.entries.join(`
|
|
332
340
|
`)
|
|
333
341
|
});
|
|
334
342
|
}
|
|
335
|
-
if (!
|
|
336
|
-
const t =
|
|
337
|
-
if (
|
|
343
|
+
if (!_) {
|
|
344
|
+
const t = Gt(wt, Number(W));
|
|
345
|
+
if (wt && Array.isArray(t) && t.length > 0)
|
|
338
346
|
t.filter((e) => e.text).forEach((e, p) => {
|
|
339
347
|
const r = a.getMessages();
|
|
340
348
|
a.insert(r.length - e.depth, { role: e.role, content: e.text });
|
|
341
349
|
});
|
|
342
350
|
else {
|
|
343
351
|
const e = q(
|
|
344
|
-
(dt = (ft = (mt = (ut =
|
|
345
|
-
|
|
346
|
-
|
|
352
|
+
(gt = (dt = (ft = (mt = (ut = s.characters[W]) == null ? void 0 : ut.data) == null ? void 0 : mt.extensions) == null ? void 0 : ft.depth_prompt) == null ? void 0 : dt.prompt) == null ? void 0 : gt.trim(),
|
|
353
|
+
v,
|
|
354
|
+
C
|
|
347
355
|
) || "";
|
|
348
356
|
if (e) {
|
|
349
|
-
const p =
|
|
357
|
+
const p = te, r = ((yt = (Pt = (_t = (ht = s.characters[W]) == null ? void 0 : ht.data) == null ? void 0 : _t.extensions) == null ? void 0 : Pt.depth_prompt) == null ? void 0 : yt.role) ?? Zt, i = a.getMessages();
|
|
350
358
|
a.insert(i.length - p, {
|
|
351
|
-
role:
|
|
359
|
+
role: b(r),
|
|
352
360
|
content: e
|
|
353
361
|
});
|
|
354
362
|
}
|
|
355
363
|
}
|
|
356
364
|
}
|
|
357
|
-
let
|
|
358
|
-
if (!
|
|
359
|
-
const t =
|
|
365
|
+
let M = -1;
|
|
366
|
+
if (!S) {
|
|
367
|
+
const t = Qt();
|
|
360
368
|
if (t.prompt) {
|
|
361
|
-
t.prompt = q(t.prompt,
|
|
362
|
-
const e = { role:
|
|
369
|
+
t.prompt = q(t.prompt, v, C);
|
|
370
|
+
const e = { role: b(t.role), content: t.prompt };
|
|
363
371
|
switch (t.position) {
|
|
364
|
-
case
|
|
365
|
-
a.insert(1, e),
|
|
372
|
+
case E.IN_PROMPT:
|
|
373
|
+
a.insert(1, e), M = 1;
|
|
366
374
|
break;
|
|
367
|
-
case
|
|
368
|
-
|
|
375
|
+
case E.IN_CHAT:
|
|
376
|
+
M = a.getMessages().length - t.depth, a.insert(M, e);
|
|
369
377
|
break;
|
|
370
|
-
case
|
|
371
|
-
a.addFront(e),
|
|
378
|
+
case E.BEFORE_PROMPT:
|
|
379
|
+
a.addFront(e), M = 0;
|
|
372
380
|
break;
|
|
373
381
|
}
|
|
374
382
|
}
|
|
375
383
|
}
|
|
376
|
-
return
|
|
377
|
-
`) }),
|
|
378
|
-
`) })), { result: a.getMessages(), warnings:
|
|
384
|
+
return M >= 0 && (st.length > 0 && (a.insert(M, { role: "system", content: st.join(`
|
|
385
|
+
`) }), M++), nt.length > 0 && a.insert(M + 1, { role: "system", content: nt.join(`
|
|
386
|
+
`) })), { result: a.getMessages(), warnings: x };
|
|
379
387
|
}
|
|
380
388
|
export {
|
|
381
|
-
|
|
389
|
+
fe as buildPrompt
|
|
382
390
|
};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function r(e){return typeof e=="string"&&e.trim().length>0}function o(e){const t=e.content;return typeof t=="string"?t:Array.isArray(t)?t.map(n=>n.type==="text"?n.text:"").filter(r).join(`
|
|
2
|
+
`):""}function s(e){const t=e.content;if(typeof t=="string")return r(t)?e:null;if(Array.isArray(t)){const n=t.filter(i=>i.type!=="text"||r(i.text));return n.length>0?{...e,content:n}:null}return null}function u(e){return e.map(t=>s(t)).filter(t=>t!==null)}exports.getMessageText=o;exports.sanitizePromptMessage=s;exports.sanitizePromptMessages=u;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ChatCompletionMessage } from './types/index.js';
|
|
2
|
+
|
|
3
|
+
export declare function getMessageText(message: ChatCompletionMessage): string;
|
|
4
|
+
export declare function sanitizePromptMessage<T extends ChatCompletionMessage>(message: T): T | null;
|
|
5
|
+
export declare function sanitizePromptMessages<T extends ChatCompletionMessage>(messages: T[]): T[];
|
|
6
|
+
//# sourceMappingURL=prompt-message-utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prompt-message-utils.d.ts","sourceRoot":"","sources":["../src/prompt-message-utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,kBAAkB,CAAC;AAYzD,wBAAgB,cAAc,CAAC,OAAO,EAAE,qBAAqB,GAAG,MAAM,CAerE;AAED,wBAAgB,qBAAqB,CAAC,CAAC,SAAS,qBAAqB,EAAE,OAAO,EAAE,CAAC,GAAG,CAAC,GAAG,IAAI,CAa3F;AAED,wBAAgB,sBAAsB,CAAC,CAAC,SAAS,qBAAqB,EAAE,QAAQ,EAAE,CAAC,EAAE,GAAG,CAAC,EAAE,CAI1F"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
function r(n) {
|
|
2
|
+
return typeof n == "string" && n.trim().length > 0;
|
|
3
|
+
}
|
|
4
|
+
function s(n) {
|
|
5
|
+
const t = n.content;
|
|
6
|
+
return typeof t == "string" ? t : Array.isArray(t) ? t.map((e) => e.type === "text" ? e.text : "").filter(r).join(`
|
|
7
|
+
`) : "";
|
|
8
|
+
}
|
|
9
|
+
function o(n) {
|
|
10
|
+
const t = n.content;
|
|
11
|
+
if (typeof t == "string")
|
|
12
|
+
return r(t) ? n : null;
|
|
13
|
+
if (Array.isArray(t)) {
|
|
14
|
+
const e = t.filter((i) => i.type !== "text" || r(i.text));
|
|
15
|
+
return e.length > 0 ? { ...n, content: e } : null;
|
|
16
|
+
}
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
function u(n) {
|
|
20
|
+
return n.map((t) => o(t)).filter((t) => t !== null);
|
|
21
|
+
}
|
|
22
|
+
export {
|
|
23
|
+
s as getMessageText,
|
|
24
|
+
o as sanitizePromptMessage,
|
|
25
|
+
u as sanitizePromptMessages
|
|
26
|
+
};
|