tool_declaration_test.go 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  1. package plugin_entities
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/langgenius/dify-plugin-daemon/internal/utils/parser"
  6. )
  7. func TestFullFunctionToolProvider_Validate(t *testing.T) {
  8. const json_data = `
  9. {
  10. "identity": {
  11. "author": "author",
  12. "name": "name",
  13. "description": {
  14. "en_US": "description",
  15. "zh_Hans": "描述",
  16. "pt_BR": "descrição"
  17. },
  18. "icon": "icon",
  19. "label": {
  20. "en_US": "label",
  21. "zh_Hans": "标签",
  22. "pt_BR": "etiqueta"
  23. },
  24. "tags": [
  25. "image",
  26. "videos"
  27. ]
  28. },
  29. "credentials_schema": [
  30. {
  31. "name": "api_key",
  32. "type": "secret-input",
  33. "required": false,
  34. "default": "default",
  35. "label": {
  36. "en_US": "API Key",
  37. "zh_Hans": "API 密钥",
  38. "pt_BR": "Chave da API"
  39. },
  40. "helper": {
  41. "en_US": "API Key",
  42. "zh_Hans": "API 密钥",
  43. "pt_BR": "Chave da API"
  44. },
  45. "url": "https://example.com",
  46. "placeholder": {
  47. "en_US": "API Key",
  48. "zh_Hans": "API 密钥",
  49. "pt_BR": "Chave da API"
  50. }
  51. }
  52. ],
  53. "tools": [
  54. {
  55. "identity": {
  56. "author": "author",
  57. "name": "tool",
  58. "label": {
  59. "en_US": "label",
  60. "zh_Hans": "标签",
  61. "pt_BR": "etiqueta"
  62. }
  63. },
  64. "description": {
  65. "human": {
  66. "en_US": "description",
  67. "zh_Hans": "描述",
  68. "pt_BR": "descrição"
  69. },
  70. "llm": "description"
  71. },
  72. "parameters": [
  73. {
  74. "name": "parameter",
  75. "type": "string",
  76. "label": {
  77. "en_US": "label",
  78. "zh_Hans": "标签",
  79. "pt_BR": "etiqueta"
  80. },
  81. "human_description": {
  82. "en_US": "description",
  83. "zh_Hans": "描述",
  84. "pt_BR": "descrição"
  85. },
  86. "form": "llm",
  87. "required": true,
  88. "default": "default",
  89. "options": [
  90. {
  91. "value": "value",
  92. "label": {
  93. "en_US": "label",
  94. "zh_Hans": "标签",
  95. "pt_BR": "etiqueta"
  96. }
  97. }
  98. ]
  99. }
  100. ]
  101. }
  102. ]
  103. }
  104. `
  105. const yaml_data = `identity:
  106. author: author
  107. name: name
  108. description:
  109. en_US: description
  110. zh_Hans: 描述
  111. pt_BR: descrição
  112. icon: icon
  113. label:
  114. en_US: label
  115. zh_Hans: 标签
  116. pt_BR: etiqueta
  117. tags:
  118. - image
  119. - videos
  120. credentials_schema:
  121. - name: api_key
  122. type: secret-input
  123. required: false
  124. default: default
  125. label:
  126. en_US: API Key
  127. zh_Hans: API 密钥
  128. pt_BR: Chave da API
  129. helper:
  130. en_US: API Key
  131. zh_Hans: API 密钥
  132. pt_BR: Chave da API
  133. url: https://example.com
  134. placeholder:
  135. en_US: API Key
  136. zh_Hans: API 密钥
  137. pt_BR: Chave da API
  138. tools:
  139. - identity:
  140. author: author
  141. name: tool
  142. label:
  143. en_US: label
  144. zh_Hans: 标签
  145. pt_BR: etiqueta
  146. description:
  147. human:
  148. en_US: description
  149. zh_Hans: 描述
  150. pt_BR: descrição
  151. llm: description
  152. parameters:
  153. - name: parameter
  154. type: string
  155. label:
  156. en_US: label
  157. zh_Hans: 标签
  158. pt_BR: etiqueta
  159. human_description:
  160. en_US: description
  161. zh_Hans: 描述
  162. pt_BR: descrição
  163. form: llm
  164. required: true
  165. default: default
  166. options:
  167. - value: value
  168. label:
  169. en_US: label
  170. zh_Hans: 标签
  171. pt_BR: etiqueta
  172. `
  173. jsonDeclaration, jsonErr := parser.UnmarshalJsonBytes[ToolProviderDeclaration]([]byte(json_data))
  174. if jsonErr != nil {
  175. t.Errorf("UnmarshalToolProviderConfiguration() error for JSON = %v", jsonErr)
  176. return
  177. }
  178. if len(jsonDeclaration.CredentialsSchema) != 1 {
  179. t.Errorf("UnmarshalToolProviderConfiguration() error for JSON: incorrect CredentialsSchema length")
  180. return
  181. }
  182. yamlDeclaration, yamlErr := parser.UnmarshalYamlBytes[ToolProviderDeclaration]([]byte(yaml_data))
  183. if yamlErr != nil {
  184. t.Errorf("UnmarshalToolProviderConfiguration() error for YAML = %v", yamlErr)
  185. return
  186. }
  187. if len(yamlDeclaration.CredentialsSchema) != 1 {
  188. t.Errorf("UnmarshalToolProviderConfiguration() error for YAML: incorrect CredentialsSchema length")
  189. return
  190. }
  191. }
  192. func TestToolProviderWithMapCredentials_Validate(t *testing.T) {
  193. const json_data = `
  194. {
  195. "identity": {
  196. "author": "author",
  197. "name": "name",
  198. "description": {
  199. "en_US": "description",
  200. "zh_Hans": "描述",
  201. "pt_BR": "descrição"
  202. },
  203. "icon": "icon",
  204. "label": {
  205. "en_US": "label",
  206. "zh_Hans": "标签",
  207. "pt_BR": "etiqueta"
  208. },
  209. "tags": [
  210. "image",
  211. "videos"
  212. ]
  213. },
  214. "credentials_schema": {
  215. "api_key": {
  216. "type": "secret-input",
  217. "required": false,
  218. "default": "default",
  219. "label": {
  220. "en_US": "API Key",
  221. "zh_Hans": "API 密钥",
  222. "pt_BR": "Chave da API"
  223. },
  224. "helper": {
  225. "en_US": "API Key",
  226. "zh_Hans": "API 密钥",
  227. "pt_BR": "Chave da API"
  228. },
  229. "url": "https://example.com",
  230. "placeholder": {
  231. "en_US": "API Key",
  232. "zh_Hans": "API 密钥",
  233. "pt_BR": "Chave da API"
  234. }
  235. }
  236. },
  237. "tools": [
  238. {
  239. "identity": {
  240. "author": "author",
  241. "name": "tool",
  242. "label": {
  243. "en_US": "label",
  244. "zh_Hans": "标签",
  245. "pt_BR": "etiqueta"
  246. }
  247. },
  248. "description": {
  249. "human": {
  250. "en_US": "description",
  251. "zh_Hans": "描述",
  252. "pt_BR": "descrição"
  253. },
  254. "llm": "description"
  255. },
  256. "parameters": [
  257. {
  258. "name": "parameter",
  259. "type": "string",
  260. "label": {
  261. "en_US": "label",
  262. "zh_Hans": "标签",
  263. "pt_BR": "etiqueta"
  264. },
  265. "human_description": {
  266. "en_US": "description",
  267. "zh_Hans": "描述",
  268. "pt_BR": "descrição"
  269. },
  270. "form": "llm",
  271. "required": true,
  272. "default": "default",
  273. "options": [
  274. {
  275. "value": "value",
  276. "label": {
  277. "en_US": "label",
  278. "zh_Hans": "标签",
  279. "pt_BR": "etiqueta"
  280. }
  281. }
  282. ]
  283. }
  284. ]
  285. }
  286. ]
  287. }
  288. `
  289. const yaml_data = `identity:
  290. author: author
  291. name: name
  292. description:
  293. en_US: description
  294. zh_Hans: 描述
  295. pt_BR: descrição
  296. icon: icon
  297. label:
  298. en_US: label
  299. zh_Hans: 标签
  300. pt_BR: etiqueta
  301. tags:
  302. - image
  303. - videos
  304. credentials_schema:
  305. api_key:
  306. type: secret-input
  307. required: false
  308. default: default
  309. label:
  310. en_US: API Key
  311. zh_Hans: API 密钥
  312. pt_BR: Chave da API
  313. helper:
  314. en_US: API Key
  315. zh_Hans: API 密钥
  316. pt_BR: Chave da API
  317. url: https://example.com
  318. placeholder:
  319. en_US: API Key
  320. zh_Hans: API 密钥
  321. pt_BR: Chave da API
  322. tools:
  323. - identity:
  324. author: author
  325. name: tool
  326. label:
  327. en_US: label
  328. zh_Hans: 标签
  329. pt_BR: etiqueta
  330. description:
  331. human:
  332. en_US: description
  333. zh_Hans: 描述
  334. pt_BR: descrição
  335. llm: description
  336. parameters:
  337. - name: parameter
  338. type: string
  339. label:
  340. en_US: label
  341. zh_Hans: 标签
  342. pt_BR: etiqueta
  343. human_description:
  344. en_US: description
  345. zh_Hans: 描述
  346. pt_BR: descrição
  347. form: llm
  348. required: true
  349. default: default
  350. options:
  351. - value: value
  352. label:
  353. en_US: label
  354. zh_Hans: 标签
  355. pt_BR: etiqueta
  356. `
  357. jsonDeclaration, jsonErr := parser.UnmarshalJsonBytes[ToolProviderDeclaration]([]byte(json_data))
  358. if jsonErr != nil {
  359. t.Errorf("UnmarshalToolProviderConfiguration() error for JSON = %v", jsonErr)
  360. return
  361. }
  362. if len(jsonDeclaration.CredentialsSchema) != 1 {
  363. t.Errorf("UnmarshalToolProviderConfiguration() error for JSON: incorrect CredentialsSchema length")
  364. return
  365. }
  366. if len(jsonDeclaration.Tools) != 1 {
  367. t.Errorf("UnmarshalToolProviderConfiguration() error for JSON: incorrect Tools length")
  368. return
  369. }
  370. yamlDeclaration, yamlErr := parser.UnmarshalYamlBytes[ToolProviderDeclaration]([]byte(yaml_data))
  371. if yamlErr != nil {
  372. t.Errorf("UnmarshalToolProviderConfiguration() error for YAML = %v", yamlErr)
  373. return
  374. }
  375. if len(yamlDeclaration.CredentialsSchema) != 1 {
  376. t.Errorf("UnmarshalToolProviderConfiguration() error for YAML: incorrect CredentialsSchema length")
  377. return
  378. }
  379. if len(yamlDeclaration.Tools) != 1 {
  380. t.Errorf("UnmarshalToolProviderConfiguration() error for YAML: incorrect Tools length")
  381. return
  382. }
  383. }
  384. func TestWithoutAuthorToolProvider_Validate(t *testing.T) {
  385. const data = `
  386. {
  387. "identity": {
  388. "name": "name",
  389. "description": {
  390. "en_US": "description",
  391. "zh_Hans": "描述",
  392. "pt_BR": "descrição"
  393. },
  394. "icon": "icon",
  395. "label": {
  396. "en_US": "label",
  397. "zh_Hans": "标签",
  398. "pt_BR": "etiqueta"
  399. },
  400. "tags": [
  401. "image",
  402. "videos"
  403. ]
  404. },
  405. "credentials_schema": [
  406. {
  407. "name": "api_key",
  408. "type": "secret-input",
  409. "required": false,
  410. "default": "default",
  411. "label": {
  412. "en_US": "API Key",
  413. "zh_Hans": "API 密钥",
  414. "pt_BR": "Chave da API"
  415. },
  416. "helper": {
  417. "en_US": "API Key",
  418. "zh_Hans": "API 密钥",
  419. "pt_BR": "Chave da API"
  420. },
  421. "url": "https://example.com",
  422. "placeholder": {
  423. "en_US": "API Key",
  424. "zh_Hans": "API 密钥",
  425. "pt_BR": "Chave da API"
  426. }
  427. }
  428. ]
  429. },
  430. "tools": [
  431. ]
  432. }
  433. `
  434. _, err := UnmarshalToolProviderDeclaration([]byte(data))
  435. if err == nil {
  436. t.Errorf("UnmarshalToolProviderConfiguration() error = %v, wantErr %v", err, true)
  437. return
  438. }
  439. }
  440. func TestWithoutNameToolProvider_Validate(t *testing.T) {
  441. const data = `
  442. {
  443. "identity": {
  444. "author": "author",
  445. "description": {
  446. "en_US": "description",
  447. "zh_Hans": "描述",
  448. "pt_BR": "descrição"
  449. },
  450. "icon": "icon",
  451. "label": {
  452. "en_US": "label",
  453. "zh_Hans": "标签",
  454. "pt_BR": "etiqueta"
  455. },
  456. "tags": [
  457. "image",
  458. "videos"
  459. ]
  460. },
  461. "credentials_schema": [
  462. {
  463. "name": "api_key",
  464. "type": "secret-input",
  465. "type": "secret-input",
  466. "required": false,
  467. "default": "default",
  468. "label": {
  469. "en_US": "API Key",
  470. "zh_Hans": "API 密钥",
  471. "pt_BR": "Chave da API"
  472. },
  473. "helper": {
  474. "en_US": "API Key",
  475. "zh_Hans": "API 密钥",
  476. "pt_BR": "Chave da API"
  477. },
  478. "url": "https://example.com",
  479. "placeholder": {
  480. "en_US": "API Key",
  481. "zh_Hans": "API 密钥",
  482. "pt_BR": "Chave da API"
  483. }
  484. }
  485. ],
  486. "tools": [
  487. ]
  488. }
  489. `
  490. _, err := UnmarshalToolProviderDeclaration([]byte(data))
  491. if err == nil {
  492. t.Errorf("UnmarshalToolProviderConfiguration() error = %v, wantErr %v", err, true)
  493. return
  494. }
  495. }
  496. func TestWithoutDescriptionToolProvider_Validate(t *testing.T) {
  497. const data = `
  498. {
  499. "identity": {
  500. "author": "author",
  501. "name": "name",
  502. "icon": "icon",
  503. "label": {
  504. "en_US": "label",
  505. "zh_Hans": "标签",
  506. "pt_BR": "etiqueta"
  507. },
  508. "tags": [
  509. "image",
  510. "videos"
  511. ]
  512. },
  513. "credentials_schema": [
  514. {
  515. "name": "api_key",
  516. "type": "secret-input",
  517. "required": false,
  518. "default": "default",
  519. "label": {
  520. "en_US": "API Key",
  521. "zh_Hans": "API 密钥",
  522. "pt_BR": "Chave da API"
  523. },
  524. "helper": {
  525. "en_US": "API Key",
  526. "zh_Hans": "API 密钥",
  527. "pt_BR": "Chave da API"
  528. },
  529. "url": "https://example.com",
  530. "placeholder": {
  531. "en_US": "API Key",
  532. "zh_Hans": "API 密钥",
  533. "pt_BR": "Chave da API"
  534. }
  535. }
  536. ],
  537. "tools": [
  538. ]
  539. }
  540. `
  541. _, err := UnmarshalToolProviderDeclaration([]byte(data))
  542. if err == nil {
  543. t.Errorf("UnmarshalToolProviderConfiguration() error = %v, wantErr %v", err, true)
  544. return
  545. }
  546. }
  547. func TestWrongCredentialTypeToolProvider_Validate(t *testing.T) {
  548. const data = `
  549. {
  550. "identity": {
  551. "author": "author",
  552. "name": "name",
  553. "description": {
  554. "en_US": "description",
  555. "zh_Hans": "描述",
  556. "pt_BR": "descrição"
  557. },
  558. "icon": "icon",
  559. "label": {
  560. "en_US": "label",
  561. "zh_Hans": "标签",
  562. "pt_BR": "etiqueta"
  563. },
  564. "tags": [
  565. "image",
  566. "videos"
  567. ]
  568. },
  569. "credentials_schema": [
  570. {
  571. "name": "api_key",
  572. "type": "wrong",
  573. "required": false,
  574. "default": "default",
  575. "label": {
  576. "en_US": "API Key",
  577. "zh_Hans": "API 密钥",
  578. "pt_BR": "Chave da API"
  579. },
  580. "helper": {
  581. "en_US": "API Key",
  582. "zh_Hans": "API 密钥",
  583. "pt_BR": "Chave da API"
  584. },
  585. "url": "https://example.com",
  586. "placeholder": {
  587. "en_US": "API Key",
  588. "zh_Hans": "API 密钥",
  589. "pt_BR": "Chave da API"
  590. }
  591. }
  592. ],
  593. "tools": [
  594. ]
  595. }
  596. `
  597. _, err := UnmarshalToolProviderDeclaration([]byte(data))
  598. if err == nil {
  599. t.Errorf("UnmarshalToolProviderConfiguration() error = %v, wantErr %v", err, true)
  600. return
  601. }
  602. }
  603. func TestWrongIdentityTagsToolProvider_Validate(t *testing.T) {
  604. const data = `
  605. {
  606. "identity": {
  607. "author": "author",
  608. "name": "name",
  609. "description": {
  610. "en_US": "description",
  611. "zh_Hans": "描述",
  612. "pt_BR": "descrição"
  613. },
  614. "icon": "icon",
  615. "label": {
  616. "en_US": "label",
  617. "zh_Hans": "标签",
  618. "pt_BR": "etiqueta"
  619. },
  620. "tags": [
  621. "wrong",
  622. "videos"
  623. ]
  624. },
  625. "credentials_schema": [
  626. {
  627. "name": "api_key",
  628. "type": "secret-input",
  629. "required": false,
  630. "default": "default",
  631. "label": {
  632. "en_US": "API Key",
  633. "zh_Hans": "API 密钥",
  634. "pt_BR": "Chave da API"
  635. },
  636. "helper": {
  637. "en_US": "API Key",
  638. "zh_Hans": "API 密钥",
  639. "pt_BR": "Chave da API"
  640. },
  641. "url": "https://example.com",
  642. "placeholder": {
  643. "en_US": "API Key",
  644. "zh_Hans": "API 密钥",
  645. "pt_BR": "Chave da API"
  646. }
  647. }
  648. ],
  649. "tools": [
  650. ]
  651. }
  652. `
  653. _, err := UnmarshalToolProviderDeclaration([]byte(data))
  654. if err == nil {
  655. t.Errorf("UnmarshalToolProviderConfiguration() error = %v, wantErr %v", err, true)
  656. return
  657. }
  658. }
  659. func TestWrongToolParameterTypeToolProvider_Validate(t *testing.T) {
  660. const data = `
  661. {
  662. "identity": {
  663. "author": "author",
  664. "name": "name",
  665. "description": {
  666. "en_US": "description",
  667. "zh_Hans": "描述",
  668. "pt_BR": "descrição"
  669. },
  670. "icon": "icon",
  671. "label": {
  672. "en_US": "label",
  673. "zh_Hans": "标签",
  674. "pt_BR": "etiqueta"
  675. },
  676. "tags": []
  677. },
  678. "credentials_schema": [],
  679. "tools": [
  680. {
  681. "identity": {
  682. "author": "author",
  683. "name": "tool",
  684. "label": {
  685. "en_US": "label",
  686. "zh_Hans": "标签",
  687. "pt_BR": "etiqueta"
  688. }
  689. },
  690. "description": {
  691. "human": {
  692. "en_US": "description",
  693. "zh_Hans": "描述",
  694. "pt_BR": "descrição"
  695. },
  696. "llm": "description"
  697. },
  698. "parameters": [
  699. {
  700. "name": "parameter",
  701. "type": "wrong",
  702. "label": {
  703. "en_US": "label",
  704. "zh_Hans": "标签",
  705. "pt_BR": "etiqueta"
  706. },
  707. "human_description": {
  708. "en_US": "description",
  709. "zh_Hans": "描述",
  710. "pt_BR": "descrição"
  711. },
  712. "form": "llm",
  713. "required": true,
  714. "default": "default",
  715. "options": []
  716. }
  717. ]
  718. }
  719. ]
  720. }
  721. `
  722. _, err := UnmarshalToolProviderDeclaration([]byte(data))
  723. if err == nil {
  724. t.Errorf("UnmarshalToolProviderConfiguration() error = %v, wantErr %v", err, true)
  725. return
  726. }
  727. }
  728. func TestWrongToolParameterFormToolProvider_Validate(t *testing.T) {
  729. const data = `
  730. {
  731. "identity": {
  732. "author": "author",
  733. "name": "name",
  734. "description": {
  735. "en_US": "description",
  736. "zh_Hans": "描述",
  737. "pt_BR": "descrição"
  738. },
  739. "icon": "icon",
  740. "label": {
  741. "en_US": "label",
  742. "zh_Hans": "标签",
  743. "pt_BR": "etiqueta"
  744. },
  745. "tags": []
  746. },
  747. "credentials_schema": [],
  748. "tools": [
  749. {
  750. "identity": {
  751. "author": "author",
  752. "name": "tool",
  753. "label": {
  754. "en_US": "label",
  755. "zh_Hans": "标签",
  756. "pt_BR": "etiqueta"
  757. }
  758. },
  759. "description": {
  760. "human": {
  761. "en_US": "description",
  762. "zh_Hans": "描述",
  763. "pt_BR": "descrição"
  764. },
  765. "llm": "description"
  766. },
  767. "parameters": [
  768. {
  769. "name": "parameter",
  770. "type": "string",
  771. "label": {
  772. "en_US": "label",
  773. "zh_Hans": "标签",
  774. "pt_BR": "etiqueta"
  775. },
  776. "human_description": {
  777. "en_US": "description",
  778. "zh_Hans": "描述",
  779. "pt_BR": "descrição"
  780. },
  781. "form": "wrong",
  782. "required": true,
  783. "default": "default",
  784. "options": []
  785. }
  786. ]
  787. }
  788. ]
  789. }
  790. `
  791. _, err := UnmarshalToolProviderDeclaration([]byte(data))
  792. if err == nil {
  793. t.Errorf("UnmarshalToolProviderConfiguration() error = %v, wantErr %v", err, true)
  794. return
  795. }
  796. }
  797. func TestJSONSchemaTypeToolProvider_Validate(t *testing.T) {
  798. const data = `
  799. {
  800. "identity": {
  801. "author": "author",
  802. "name": "name",
  803. "description": {
  804. "en_US": "description",
  805. "zh_Hans": "描述",
  806. "pt_BR": "descrição"
  807. },
  808. "icon": "icon",
  809. "label": {
  810. "en_US": "label",
  811. "zh_Hans": "标签",
  812. "pt_BR": "etiqueta"
  813. },
  814. "tags": []
  815. },
  816. "credentials_schema": [],
  817. "tools": [
  818. {
  819. "identity": {
  820. "author": "author",
  821. "name": "tool",
  822. "label": {
  823. "en_US": "label",
  824. "zh_Hans": "标签",
  825. "pt_BR": "etiqueta"
  826. }
  827. },
  828. "description": {
  829. "human": {
  830. "en_US": "description",
  831. "zh_Hans": "描述",
  832. "pt_BR": "descrição"
  833. },
  834. "llm": "description"
  835. },
  836. "output_schema": {
  837. "type": "object",
  838. "properties": {
  839. "name": {
  840. "type": "string"
  841. }
  842. }
  843. }
  844. }
  845. ]
  846. }
  847. `
  848. _, err := UnmarshalToolProviderDeclaration([]byte(data))
  849. if err != nil {
  850. t.Errorf("UnmarshalToolProviderConfiguration() error = %v, wantErr %v", err, true)
  851. return
  852. }
  853. }
  854. func TestWrongJSONSchemaToolProvider_Validate(t *testing.T) {
  855. const data = `
  856. {
  857. "identity": {
  858. "author": "author",
  859. "name": "name",
  860. "description": {
  861. "en_US": "description",
  862. "zh_Hans": "描述",
  863. "pt_BR": "descrição"
  864. },
  865. "icon": "icon",
  866. "label": {
  867. "en_US": "label",
  868. "zh_Hans": "标签",
  869. "pt_BR": "etiqueta"
  870. },
  871. "tags": []
  872. },
  873. "credentials_schema": [],
  874. "tools": [
  875. {
  876. "identity": {
  877. "author": "author",
  878. "name": "tool",
  879. "label": {
  880. "en_US": "label",
  881. "zh_Hans": "标签",
  882. "pt_BR": "etiqueta"
  883. }
  884. },
  885. "description": {
  886. "human": {
  887. "en_US": "description",
  888. "zh_Hans": "描述",
  889. "pt_BR": "descrição"
  890. },
  891. "llm": "description"
  892. },
  893. "output_schema": {
  894. "type": "object",
  895. "properties": {
  896. "name": {
  897. "type": "aaa"
  898. }
  899. }
  900. }
  901. }
  902. ]
  903. }
  904. `
  905. _, err := UnmarshalToolProviderDeclaration([]byte(data))
  906. if err == nil {
  907. t.Errorf("UnmarshalToolProviderConfiguration() error = %v, wantErr %v", err, true)
  908. return
  909. }
  910. }
  911. func TestWrongAppSelectorScopeToolProvider_Validate(t *testing.T) {
  912. const data = `
  913. {
  914. "identity": {
  915. "author": "author",
  916. "name": "name",
  917. "description": {
  918. "en_US": "description",
  919. "zh_Hans": "描述",
  920. "pt_BR": "descrição"
  921. },
  922. "icon": "icon",
  923. "label": {
  924. "en_US": "label",
  925. "zh_Hans": "标签",
  926. "pt_BR": "etiqueta"
  927. },
  928. "tags": []
  929. },
  930. "credentials_schema": [
  931. {
  932. "name": "api_key",
  933. "type": "app-selector",
  934. "scope": "wrong",
  935. "required": false,
  936. "default": null,
  937. "label": {
  938. "en_US": "app-selector",
  939. "zh_Hans": "app-selector",
  940. "pt_BR": "app-selector"
  941. },
  942. "helper": {
  943. "en_US": "app-selector",
  944. "zh_Hans": "app-selector",
  945. "pt_BR": "app-selector"
  946. },
  947. "url": "https://example.com",
  948. "placeholder": {
  949. "en_US": "app-selector",
  950. "zh_Hans": "app-selector",
  951. "pt_BR": "app-selector"
  952. }
  953. }
  954. ],
  955. "tools": [
  956. {
  957. "identity": {
  958. "author": "author",
  959. "name": "tool",
  960. "label": {
  961. "en_US": "label",
  962. "zh_Hans": "标签",
  963. "pt_BR": "etiqueta"
  964. }
  965. },
  966. "description": {
  967. "human": {
  968. "en_US": "description",
  969. "zh_Hans": "描述",
  970. "pt_BR": "descrição"
  971. },
  972. "llm": "description"
  973. },
  974. "parameters": [
  975. {
  976. "name": "parameter-app-selector",
  977. "label": {
  978. "en_US": "label",
  979. "zh_Hans": "标签",
  980. "pt_BR": "etiqueta"
  981. },
  982. "human_description": {
  983. "en_US": "description",
  984. "zh_Hans": "描述",
  985. "pt_BR": "descrição"
  986. },
  987. "type": "app-selector",
  988. "form": "llm",
  989. "scope": "wrong",
  990. "required": true,
  991. "default": "default",
  992. "options": []
  993. }
  994. ]
  995. }
  996. ]
  997. }
  998. `
  999. _, err := UnmarshalToolProviderDeclaration([]byte(data))
  1000. if err == nil {
  1001. t.Errorf("UnmarshalToolProviderConfiguration() error = %v, wantErr %v", err, true)
  1002. return
  1003. }
  1004. str := err.Error()
  1005. if !strings.Contains(str, "is_scope") {
  1006. t.Errorf("UnmarshalToolProviderConfiguration() error = %v, wantErr %v", err, true)
  1007. return
  1008. }
  1009. if !strings.Contains(str, "ToolProviderDeclaration.Tools[0].Parameters[0].Scope") {
  1010. t.Errorf("UnmarshalToolProviderConfiguration() error = %v, wantErr %v", err, true)
  1011. return
  1012. }
  1013. }
  1014. func TestAppSelectorScopeToolProvider_Validate(t *testing.T) {
  1015. const data = `
  1016. {
  1017. "identity": {
  1018. "author": "author",
  1019. "name": "name",
  1020. "description": {
  1021. "en_US": "description",
  1022. "zh_Hans": "描述",
  1023. "pt_BR": "descrição"
  1024. },
  1025. "icon": "icon",
  1026. "label": {
  1027. "en_US": "label",
  1028. "zh_Hans": "标签",
  1029. "pt_BR": "etiqueta"
  1030. },
  1031. "tags": []
  1032. },
  1033. "credentials_schema": [
  1034. {
  1035. "name": "app-selector",
  1036. "type": "app-selector",
  1037. "scope": "all",
  1038. "required": false,
  1039. "default": null,
  1040. "label": {
  1041. "en_US": "app-selector",
  1042. "zh_Hans": "app-selector",
  1043. "pt_BR": "app-selector"
  1044. },
  1045. "helper": {
  1046. "en_US": "app-selector",
  1047. "zh_Hans": "app-selector",
  1048. "pt_BR": "app-selector"
  1049. },
  1050. "url": "https://example.com",
  1051. "placeholder": {
  1052. "en_US": "app-selector",
  1053. "zh_Hans": "app-selector",
  1054. "pt_BR": "app-selector"
  1055. }
  1056. }
  1057. ],
  1058. "tools": [
  1059. {
  1060. "identity": {
  1061. "author": "author",
  1062. "name": "tool",
  1063. "label": {
  1064. "en_US": "label",
  1065. "zh_Hans": "标签",
  1066. "pt_BR": "etiqueta"
  1067. }
  1068. },
  1069. "description": {
  1070. "human": {
  1071. "en_US": "description",
  1072. "zh_Hans": "描述",
  1073. "pt_BR": "descrição"
  1074. },
  1075. "llm": "description"
  1076. },
  1077. "parameters": [
  1078. {
  1079. "name": "parameter-app-selector",
  1080. "label": {
  1081. "en_US": "label",
  1082. "zh_Hans": "标签",
  1083. "pt_BR": "etiqueta"
  1084. },
  1085. "human_description": {
  1086. "en_US": "description",
  1087. "zh_Hans": "描述",
  1088. "pt_BR": "descrição"
  1089. },
  1090. "type": "app-selector",
  1091. "form": "llm",
  1092. "scope": "all",
  1093. "required": true,
  1094. "default": "default",
  1095. "options": []
  1096. }
  1097. ]
  1098. }
  1099. ]
  1100. }
  1101. `
  1102. _, err := UnmarshalToolProviderDeclaration([]byte(data))
  1103. if err != nil {
  1104. t.Errorf("UnmarshalToolProviderConfiguration() error = %v, wantErr %v", err, true)
  1105. return
  1106. }
  1107. }
  1108. func TestParameterScope_Validate(t *testing.T) {
  1109. config := ToolParameter{
  1110. Name: "test",
  1111. Type: TOOL_PARAMETER_TYPE_MODEL_SELECTOR,
  1112. Scope: parser.ToPtr("llm& document&tool-call"),
  1113. Required: true,
  1114. Label: I18nObject{
  1115. ZhHans: "模型",
  1116. EnUS: "Model",
  1117. },
  1118. HumanDescription: I18nObject{
  1119. ZhHans: "请选择模型",
  1120. EnUS: "Please select a model",
  1121. },
  1122. LLMDescription: "please select a model",
  1123. Form: TOOL_PARAMETER_FORM_FORM,
  1124. }
  1125. data := parser.MarshalJsonBytes(config)
  1126. if _, err := parser.UnmarshalJsonBytes[ToolParameter](data); err != nil {
  1127. t.Errorf("ParameterScope_Validate() error = %v", err)
  1128. }
  1129. }
  1130. func TestInvalidJSONSchemaToolProvider_Validate(t *testing.T) {
  1131. type Test struct {
  1132. Text ToolOutputSchema `json:"text" validate:"json_schema"`
  1133. }
  1134. data := parser.MarshalJsonBytes(Test{
  1135. Text: map[string]any{
  1136. "text": "text",
  1137. },
  1138. })
  1139. if _, err := parser.UnmarshalJsonBytes[Test](data); err == nil {
  1140. t.Errorf("TestInvalidJSONSchemaToolProvider_Validate() error = %v", err)
  1141. }
  1142. data = parser.MarshalJsonBytes(Test{
  1143. Text: map[string]any{
  1144. "type": "object",
  1145. "properties": map[string]any{
  1146. "text": map[string]any{
  1147. "type": "object",
  1148. },
  1149. },
  1150. },
  1151. })
  1152. if _, err := parser.UnmarshalJsonBytes[Test](data); err == nil {
  1153. t.Errorf("TestInvalidJSONSchemaToolProvider_Validate() error = %v", err)
  1154. }
  1155. data = parser.MarshalJsonBytes(Test{
  1156. Text: map[string]any{
  1157. "type": "array",
  1158. "properties": map[string]any{
  1159. "a": map[string]any{
  1160. "type": "object",
  1161. },
  1162. },
  1163. },
  1164. })
  1165. if _, err := parser.UnmarshalJsonBytes[Test](data); err == nil {
  1166. t.Errorf("TestInvalidJSONSchemaToolProvider_Validate() error = %v", err)
  1167. }
  1168. data = parser.MarshalJsonBytes(Test{
  1169. Text: map[string]any{
  1170. "type": "object",
  1171. "properties": map[string]any{
  1172. "json": map[string]any{
  1173. "type": "object",
  1174. },
  1175. },
  1176. },
  1177. })
  1178. if _, err := parser.UnmarshalJsonBytes[Test](data); err == nil {
  1179. t.Errorf("TestInvalidJSONSchemaToolProvider_Validate() error = %v", err)
  1180. }
  1181. data = parser.MarshalJsonBytes(Test{
  1182. Text: map[string]any{
  1183. "type": "object",
  1184. "properties": map[string]any{
  1185. "files": map[string]any{
  1186. "type": "object",
  1187. },
  1188. },
  1189. },
  1190. })
  1191. if _, err := parser.UnmarshalJsonBytes[Test](data); err == nil {
  1192. t.Errorf("TestInvalidJSONSchemaToolProvider_Validate() error = %v", err)
  1193. }
  1194. data = parser.MarshalJsonBytes(Test{
  1195. Text: map[string]any{
  1196. "type": "object",
  1197. "properties": map[string]any{
  1198. "aaa": map[string]any{
  1199. "type": "object",
  1200. },
  1201. },
  1202. },
  1203. })
  1204. if _, err := parser.UnmarshalJsonBytes[Test](data); err != nil {
  1205. t.Errorf("TestInvalidJSONSchemaToolProvider_Validate() error = %v", err)
  1206. }
  1207. }
  1208. func TestToolName_Validate(t *testing.T) {
  1209. data := parser.MarshalJsonBytes(ToolProviderIdentity{
  1210. Author: "author",
  1211. Name: "tool-name",
  1212. Description: I18nObject{
  1213. EnUS: "description",
  1214. ZhHans: "描述",
  1215. },
  1216. Icon: "icon",
  1217. Label: I18nObject{
  1218. EnUS: "label",
  1219. ZhHans: "标签",
  1220. },
  1221. })
  1222. if _, err := parser.UnmarshalJsonBytes[ToolProviderIdentity](data); err != nil {
  1223. t.Errorf("TestToolName_Validate() error = %v", err)
  1224. }
  1225. data = parser.MarshalJsonBytes(ToolProviderIdentity{
  1226. Author: "author",
  1227. Name: "tool AA",
  1228. Label: I18nObject{
  1229. EnUS: "label",
  1230. ZhHans: "标签",
  1231. },
  1232. })
  1233. if _, err := parser.UnmarshalJsonBytes[ToolProviderIdentity](data); err == nil {
  1234. t.Errorf("TestToolName_Validate() error = %v", err)
  1235. }
  1236. data = parser.MarshalJsonBytes(ToolIdentity{
  1237. Author: "author",
  1238. Name: "tool-name-123",
  1239. Label: I18nObject{
  1240. EnUS: "label",
  1241. ZhHans: "标签",
  1242. },
  1243. })
  1244. if _, err := parser.UnmarshalJsonBytes[ToolIdentity](data); err != nil {
  1245. t.Errorf("TestToolName_Validate() error = %v", err)
  1246. }
  1247. data = parser.MarshalJsonBytes(ToolIdentity{
  1248. Author: "author",
  1249. Name: "tool name-123",
  1250. Label: I18nObject{
  1251. EnUS: "label",
  1252. ZhHans: "标签",
  1253. },
  1254. })
  1255. if _, err := parser.UnmarshalJsonBytes[ToolIdentity](data); err == nil {
  1256. t.Errorf("TestToolName_Validate() error = %v", err)
  1257. }
  1258. }