1
+ // Import the OpenAI class from the 'openai' package.
1
2
import { OpenAI } from 'openai' ;
3
+
4
+ // Import the FssTool type from the '@lit-protocol/fss-tool' package.
2
5
import type { FssTool } from '@lit-protocol/fss-tool' ;
3
6
7
+ /**
8
+ * Parses and validates parameters from a user's intent for a given tool.
9
+ * This function uses OpenAI's API to extract parameter values and ensures they conform to the tool's validation rules.
10
+ *
11
+ * @template TParams - A generic type representing the tool's parameter structure.
12
+ * @template TPolicy - A generic type representing the tool's policy structure.
13
+ * @param openai - An instance of the OpenAI client.
14
+ * @param openAiModel - The name of the OpenAI model to use for parsing.
15
+ * @param intent - The user's intent as a string.
16
+ * @param tool - The tool for which parameters are being parsed.
17
+ * @returns A Promise that resolves to an object containing:
18
+ * - foundParams: A partial object of the parsed parameters.
19
+ * - missingParams: An array of parameter names that could not be parsed.
20
+ * - validationErrors: An array of validation errors for invalid parameters.
21
+ */
4
22
export async function parseToolParametersFromIntent <
5
23
TParams extends Record < string , any > ,
6
24
TPolicy extends { type : string }
@@ -14,6 +32,7 @@ export async function parseToolParametersFromIntent<
14
32
missingParams : Array < keyof TParams > ;
15
33
validationErrors : Array < { param : string ; error : string } > ;
16
34
} > {
35
+ // Use OpenAI's API to parse parameters from the user's intent.
17
36
const completion = await openai . chat . completions . create ( {
18
37
model : openAiModel ,
19
38
messages : [
@@ -65,12 +84,14 @@ export async function parseToolParametersFromIntent<
65
84
response_format : { type : 'json_object' } ,
66
85
} ) ;
67
86
87
+ // Parse the result from OpenAI's response.
68
88
const result = JSON . parse ( completion . choices [ 0 ] . message . content || '{}' ) ;
69
89
70
- // Validate found parameters
90
+ // Validate the found parameters using the tool's validation function.
71
91
const foundParams = result . foundParams || { } ;
72
92
const validationResult = tool . parameters . validate ( foundParams ) ;
73
93
94
+ // If validation passes, return the found and missing parameters.
74
95
if ( validationResult === true ) {
75
96
return {
76
97
foundParams,
@@ -80,12 +101,12 @@ export async function parseToolParametersFromIntent<
80
101
} ;
81
102
}
82
103
83
- // If validation fails, only treat invalid params as missing
104
+ // If validation fails, filter out invalid parameters and add them to missingParams.
84
105
const invalidParams = new Set ( validationResult . map ( ( error ) => error . param ) ) ;
85
106
const filteredParams : Partial < TParams > = { } ;
86
107
const missingParams = new Set < keyof TParams > ( result . missingParams || [ ] ) ;
87
108
88
- // Keep only valid params in foundParams
109
+ // Keep only valid parameters in foundParams.
89
110
Object . entries ( foundParams ) . forEach ( ( [ param , value ] ) => {
90
111
if ( ! invalidParams . has ( param ) ) {
91
112
filteredParams [ param as keyof TParams ] = value as TParams [ keyof TParams ] ;
@@ -94,6 +115,7 @@ export async function parseToolParametersFromIntent<
94
115
}
95
116
} ) ;
96
117
118
+ // Return the filtered parameters, missing parameters, and validation errors.
97
119
return {
98
120
foundParams : filteredParams ,
99
121
missingParams : Array . from ( missingParams ) ,
0 commit comments