1- import { tracked } from "@glimmer/tracking" ;
21import { ajax } from "discourse/lib/ajax" ;
32import RestModel from "discourse/models/rest" ;
43
@@ -63,32 +62,39 @@ const SYSTEM_ATTRIBUTES = [
6362 "allow_chat_direct_messages" ,
6463] ;
6564
66- class ToolOption {
67- @tracked value = null ;
68- }
69-
7065export default class AiPersona extends RestModel {
7166 // this code is here to convert the wire schema to easier to work with object
7267 // on the wire we pass in/out tools as an Array.
7368 // [[ToolName, {option1: value, option2: value}, force], ToolName2, ToolName3]
7469 // So we rework this into a "tools" property and nested toolOptions
7570 init ( properties ) {
7671 this . forcedTools = [ ] ;
72+ this . toolOptions = { } ;
73+
7774 if ( properties . tools ) {
7875 properties . tools = properties . tools . map ( ( tool ) => {
7976 if ( typeof tool === "string" ) {
8077 return tool ;
8178 } else {
8279 let [ toolId , options , force ] = tool ;
83- for ( let optionId in options ) {
80+ const mappedOptions = { } ;
81+
82+ for ( const optionId in options ) {
8483 if ( ! options . hasOwnProperty ( optionId ) ) {
8584 continue ;
8685 }
87- this . getToolOption ( toolId , optionId ) . value = options [ optionId ] ;
86+
87+ mappedOptions [ optionId ] = options [ optionId ] ;
88+ }
89+
90+ if ( Object . keys ( mappedOptions ) . length > 0 ) {
91+ this . toolOptions [ toolId ] = mappedOptions ;
8892 }
93+
8994 if ( force ) {
9095 this . forcedTools . push ( toolId ) ;
9196 }
97+
9298 return toolId ;
9399 }
94100 } ) ;
@@ -109,63 +115,41 @@ export default class AiPersona extends RestModel {
109115 return this . user ;
110116 }
111117
112- getToolOption ( toolId , optionId ) {
113- this . toolOptions ||= { } ;
114- this . toolOptions [ toolId ] ||= { } ;
115- return ( this . toolOptions [ toolId ] [ optionId ] ||= new ToolOption ( ) ) ;
116- }
117-
118- populateToolOptions ( attrs ) {
119- if ( ! attrs . tools ) {
120- return ;
121- }
122- let toolsWithOptions = [ ] ;
123- attrs . tools . forEach ( ( toolId ) => {
124- if ( typeof toolId !== "string" ) {
125- toolId = toolId [ 0 ] ;
126- }
127-
128- let force = this . forcedTools . includes ( toolId ) ;
129- if ( this . toolOptions && this . toolOptions [ toolId ] ) {
130- let options = this . toolOptions [ toolId ] ;
131- let optionsWithValues = { } ;
132- for ( let optionId in options ) {
133- if ( ! options . hasOwnProperty ( optionId ) ) {
134- continue ;
135- }
136- let option = options [ optionId ] ;
137- optionsWithValues [ optionId ] = option . value ;
138- }
139- toolsWithOptions . push ( [ toolId , optionsWithValues , force ] ) ;
140- } else {
141- toolsWithOptions . push ( [ toolId , { } , force ] ) ;
142- }
118+ flattenedToolStructure ( data ) {
119+ return data . tools . map ( ( tName ) => {
120+ return [ tName , data . toolOptions [ tName ] , data . forcedTools . includes ( tName ) ] ;
143121 } ) ;
144- attrs . tools = toolsWithOptions ;
145122 }
146123
147124 updateProperties ( ) {
148- let attrs = this . system
125+ const attrs = this . system
149126 ? this . getProperties ( SYSTEM_ATTRIBUTES )
150127 : this . getProperties ( CREATE_ATTRIBUTES ) ;
151128 attrs . id = this . id ;
152- this . populateToolOptions ( attrs ) ;
129+
153130 return attrs ;
154131 }
155132
156133 createProperties ( ) {
157- let attrs = this . getProperties ( CREATE_ATTRIBUTES ) ;
158- this . populateToolOptions ( attrs ) ;
159- return attrs ;
134+ return this . getProperties ( CREATE_ATTRIBUTES ) ;
160135 }
161136
162- workingCopy ( ) {
163- let attrs = this . getProperties ( CREATE_ATTRIBUTES ) ;
164- this . populateToolOptions ( attrs ) ;
137+ fromPOJO ( data ) {
138+ const dataClone = JSON . parse ( JSON . stringify ( data ) ) ;
139+
140+ const persona = AiPersona . create ( dataClone ) ;
141+ persona . tools = this . flattenedToolStructure ( dataClone ) ;
165142
166- const persona = AiPersona . create ( attrs ) ;
167- persona . forcedTools = ( this . forcedTools || [ ] ) . slice ( ) ;
168- persona . forced_tool_count = this . forced_tool_count || - 1 ;
169143 return persona ;
170144 }
145+
146+ toPOJO ( ) {
147+ const attrs = this . getProperties ( CREATE_ATTRIBUTES ) ;
148+ attrs . tools = this . tools ;
149+ attrs . forcedTools = ( this . forcedTools || [ ] ) . slice ( ) ;
150+ attrs . toolOptions = Object . assign ( { } , this . toolOptions ) ;
151+ attrs . forced_tool_count = this . forced_tool_count || - 1 ;
152+
153+ return attrs ;
154+ }
171155}
0 commit comments