@@ -11,7 +11,7 @@ import {
1111 IOutlineValidationArgs ,
1212 IOutlineResult ,
1313} from "../../interfaces/Outline.interface" ;
14- import { getErrorMessage , randomString , str } from "functools-kit" ;
14+ import { getErrorMessage , randomString } from "functools-kit" ;
1515
1616const METHOD_NAME = "function.target.json" ;
1717
@@ -32,11 +32,23 @@ class OutlineHistory implements IOutlineHistory {
3232 * @param {string } [prompt] - An optional system prompt to initialize the history with.
3333 * @constructor
3434 */
35- constructor ( prompt ?: string ) {
36- prompt && this . messages . push ( {
37- role : "system" ,
38- content : prompt
39- } )
35+ constructor ( prompt ?: string | string [ ] ) {
36+ if ( ! prompt ) {
37+ return ;
38+ }
39+ if ( typeof prompt === "string" ) {
40+ this . messages . push ( {
41+ role : "system" ,
42+ content : prompt ,
43+ } ) ;
44+ return ;
45+ }
46+ prompt . forEach ( ( content ) => {
47+ this . messages . push ( {
48+ role : "system" ,
49+ content,
50+ } ) ;
51+ } ) ;
4052 }
4153
4254 /**
@@ -93,20 +105,25 @@ const jsonInternal = beginContext(
93105 const resultId = randomString ( ) ;
94106
95107 const {
96- getStructuredOutput,
108+ getOutlineHistory,
109+ completion,
97110 validations = [ ] ,
98111 maxAttempts = MAX_ATTEMPTS ,
99112 format,
100113 prompt,
101- callbacks,
114+ callbacks : outlineCallbacks ,
102115 } = swarm . outlineSchemaService . get ( outlineName ) ;
103116
117+ swarm . completionValidationService . validate ( completion , METHOD_NAME ) ;
118+
119+ const { getCompletion, callbacks : completionCallbacks } =
120+ swarm . completionSchemaService . get ( completion ) ;
121+
104122 let errorMessage : string = "" ;
105123 let history : OutlineHistory ;
106124
107- const systemPrompt = str . newline (
108- typeof prompt === "function" ? await prompt ( outlineName ) : prompt
109- ) ;
125+ const systemPrompt =
126+ typeof prompt === "function" ? await prompt ( outlineName ) : prompt ;
110127
111128 for ( let attempt = 0 ; attempt < maxAttempts ; attempt ++ ) {
112129 history = new OutlineHistory ( systemPrompt ) ;
@@ -116,41 +133,52 @@ const jsonInternal = beginContext(
116133 param,
117134 history,
118135 } ;
119- if ( callbacks ?. onAttempt ) {
120- callbacks . onAttempt ( inputArgs ) ;
136+ if ( outlineCallbacks ?. onAttempt ) {
137+ outlineCallbacks . onAttempt ( inputArgs ) ;
121138 }
122- const data = await getStructuredOutput ( inputArgs ) ;
123- const validationArgs : IOutlineValidationArgs = {
124- ...inputArgs ,
125- data,
126- } ;
127- let isValid = true ;
128- for ( const validation of validations ) {
129- const validate =
130- typeof validation === "object" ? validation . validate : validation ;
131- try {
139+ await getOutlineHistory ( inputArgs ) ;
140+ const messages = await history . list ( ) ;
141+ try {
142+ const output = await getCompletion ( {
143+ messages : await history . list ( ) ,
144+ mode : "tool" ,
145+ outlineName,
146+ } ) ;
147+ if ( completionCallbacks ?. onComplete ) {
148+ completionCallbacks . onComplete (
149+ {
150+ messages,
151+ mode : "tool" ,
152+ outlineName,
153+ } ,
154+ output
155+ ) ;
156+ }
157+ const data = JSON . parse ( output . content ) as IOutlineData ;
158+ const validationArgs : IOutlineValidationArgs = {
159+ ...inputArgs ,
160+ data,
161+ } ;
162+ for ( const validation of validations ) {
163+ const validate =
164+ typeof validation === "object" ? validation . validate : validation ;
132165 await validate ( validationArgs ) ;
133- } catch ( error ) {
134- isValid = false ;
135- errorMessage = getErrorMessage ( error ) ;
136- break ;
137166 }
167+ const result = {
168+ isValid : true ,
169+ attempt,
170+ param,
171+ history : await history . list ( ) ,
172+ data,
173+ resultId,
174+ } ;
175+ if ( outlineCallbacks ?. onValidDocument ) {
176+ outlineCallbacks . onValidDocument ( result ) ;
177+ }
178+ return result ;
179+ } catch ( error ) {
180+ errorMessage = getErrorMessage ( error ) ;
138181 }
139- if ( ! isValid ) {
140- continue ;
141- }
142- const result = {
143- isValid : true ,
144- attempt,
145- param,
146- history : await history . list ( ) ,
147- data,
148- resultId,
149- } ;
150- if ( callbacks ?. onValidDocument ) {
151- callbacks . onValidDocument ( result ) ;
152- }
153- return result ;
154182 }
155183 const result = {
156184 isValid : false ,
@@ -161,8 +189,8 @@ const jsonInternal = beginContext(
161189 data : null ,
162190 resultId,
163191 } ;
164- if ( callbacks ?. onInvalidDocument ) {
165- callbacks . onInvalidDocument ( result ) ;
192+ if ( outlineCallbacks ?. onInvalidDocument ) {
193+ outlineCallbacks . onInvalidDocument ( result ) ;
166194 }
167195 return result ;
168196 }
0 commit comments