File tree 1 file changed +21
-7
lines changed
1 file changed +21
-7
lines changed Original file line number Diff line number Diff line change @@ -9,13 +9,27 @@ 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 actionExamples : ActionExample [ ] [ ] = actionsData
13
- . sort ( ( ) => 0.5 - Math . random ( ) )
14
- . map ( ( action : Action ) =>
15
- action . examples . sort ( ( ) => 0.5 - Math . random ( ) ) . slice ( 0 , 5 )
16
- )
17
- . flat ( )
18
- . slice ( 0 , count ) ;
12
+ const data : ActionExample [ ] [ ] [ ] = actionsData . map ( ( action : Action ) => [
13
+ ...action . examples ,
14
+ ] ) ;
15
+
16
+ const actionExamples : ActionExample [ ] [ ] = [ ] ;
17
+ let length = data . length ;
18
+ for ( let i = 0 ; i < count && length ; i ++ ) {
19
+ const actionId = i % length ;
20
+ const examples = data [ actionId ] ;
21
+ if ( examples . length ) {
22
+ const rand = ~ ~ ( Math . random ( ) * examples . length ) ;
23
+ actionExamples [ i ] = examples . splice ( rand , 1 ) [ 0 ] ;
24
+ } else {
25
+ i -- ;
26
+ }
27
+
28
+ if ( examples . length == 0 ) {
29
+ data . splice ( actionId , 1 ) ;
30
+ length -- ;
31
+ }
32
+ }
19
33
20
34
const formattedExamples = actionExamples . map ( ( example ) => {
21
35
const exampleNames = Array . from ( { length : 5 } , ( ) =>
You can’t perform that action at this time.
0 commit comments