@@ -9,70 +9,70 @@ import { Action, ActionExample } from "./types.ts";
9
9
* @returns A string containing formatted examples of conversations.
10
10
*/
11
11
export const composeActionExamples = ( actionsData : Action [ ] , count : number ) => {
12
- const data : ActionExample [ ] [ ] [ ] = actionsData . map ( ( action : Action ) => [
13
- ... action . examples ,
14
- ] ) ;
12
+ if ( ! actionsData . length ) return '' ;
13
+
14
+ const data : ActionExample [ ] [ ] [ ] = actionsData . map ( ( { examples } ) => [ ... examples ] ) ;
15
15
16
16
const actionExamples : ActionExample [ ] [ ] = [ ] ;
17
17
let length = data . length ;
18
+
18
19
for ( let i = 0 ; i < count && length ; i ++ ) {
19
20
const actionId = i % length ;
20
21
const examples = data [ actionId ] ;
21
- if ( examples . length ) {
22
- const rand = ~ ~ ( Math . random ( ) * examples . length ) ;
23
- actionExamples [ i ] = examples . splice ( rand , 1 ) [ 0 ] ;
22
+
23
+ if ( examples ?. length ) {
24
+ const [ example ] = examples . splice ( Math . floor ( Math . random ( ) * examples . length ) , 1 ) ;
25
+ actionExamples [ i ] = example ;
24
26
} else {
25
27
i -- ;
26
28
}
27
29
28
- if ( examples . length == 0 ) {
30
+ if ( ! examples . length ) {
29
31
data . splice ( actionId , 1 ) ;
30
32
length -- ;
31
33
}
32
34
}
33
35
34
36
const formattedExamples = actionExamples . map ( ( example ) => {
35
- const exampleNames = Array . from ( { length : 5 } , ( ) =>
36
- uniqueNamesGenerator ( { dictionaries : [ names ] } )
37
+ const exampleNames = Array . from (
38
+ { length : 5 } ,
39
+ ( ) => uniqueNamesGenerator ( { dictionaries : [ names ] } )
37
40
) ;
38
41
39
- return `\n${ example
40
- . map ( ( message ) => {
41
- let messageString = `${ message . user } : ${ message . content . text } ${ message . content . action ? ` (${ message . content . action } )` : "" } ` ;
42
- for ( let i = 0 ; i < exampleNames . length ; i ++ ) {
43
- messageString = messageString . replaceAll (
44
- `{{user${ i + 1 } }}` ,
45
- exampleNames [ i ]
46
- ) ;
47
- }
42
+ return example
43
+ . map ( ( { user, content : { text, action } } ) => {
44
+ const actionText = action ? ` (${ action } )` : '' ;
45
+ let messageString = `${ user } : ${ text } ${ actionText } ` ;
46
+
47
+ exampleNames . forEach ( ( name , index ) => {
48
+ messageString = messageString . replaceAll ( `{{user${ index + 1 } }}` , name ) ;
49
+ } ) ;
48
50
return messageString ;
49
51
} )
50
- . join ( "\n" ) } ` ;
52
+ . join ( '\n' ) ;
51
53
} ) ;
52
54
53
- return formattedExamples . join ( "\n" ) ;
55
+ return formattedExamples . length ? `\n ${ formattedExamples . join ( '\n' ) } ` : '' ;
54
56
} ;
55
57
56
58
/**
57
59
* Formats the names of the provided actions into a comma-separated string.
58
60
* @param actions - An array of `Action` objects from which to extract names.
59
61
* @returns A comma-separated string of action names.
60
62
*/
61
- export function formatActionNames ( actions : Action [ ] ) {
62
- return actions
63
+ export const formatActionNames = ( actions : Action [ ] ) : string =>
64
+ actions
63
65
. sort ( ( ) => 0.5 - Math . random ( ) )
64
- . map ( ( action : Action ) => ` ${ action . name } ` )
66
+ . map ( ( { name } ) => name )
65
67
. join ( ", " ) ;
66
- }
67
68
68
69
/**
69
70
* Formats the provided actions into a detailed string listing each action's name and description, separated by commas and newlines.
70
71
* @param actions - An array of `Action` objects to format.
71
72
* @returns A detailed string of actions, including names and descriptions.
72
73
*/
73
- export function formatActions ( actions : Action [ ] ) {
74
- return actions
74
+ export const formatActions = ( actions : Action [ ] ) : string =>
75
+ actions
75
76
. sort ( ( ) => 0.5 - Math . random ( ) )
76
- . map ( ( action : Action ) => `${ action . name } : ${ action . description } ` )
77
+ . map ( ( { name , description } ) => `${ name } : ${ description } ` )
77
78
. join ( ",\n" ) ;
78
- }
0 commit comments