tool_declaration_test.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  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. }