http_request.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package dify_invocation
  2. import (
  3. "github.com/langgenius/dify-plugin-daemon/internal/utils/requests"
  4. "github.com/langgenius/dify-plugin-daemon/internal/utils/stream"
  5. )
  6. func Request[T any](method string, path string, options ...requests.HttpOptions) (*T, error) {
  7. options = append(options,
  8. requests.HttpHeader(map[string]string{
  9. "X-Inner-Api-Key": PLUGIN_INNER_API_KEY,
  10. }),
  11. requests.HttpWriteTimeout(5000),
  12. requests.HttpReadTimeout(60000),
  13. )
  14. return requests.RequestAndParse[T](client, difyPath(path), method, options...)
  15. }
  16. func StreamResponse[T any](method string, path string, options ...requests.HttpOptions) (*stream.StreamResponse[T], error) {
  17. options = append(
  18. options, requests.HttpHeader(map[string]string{
  19. "X-Inner-Api-Key": PLUGIN_INNER_API_KEY,
  20. }),
  21. requests.HttpWriteTimeout(5000),
  22. requests.HttpReadTimeout(60000),
  23. )
  24. return requests.RequestAndParseStream[T](client, difyPath(path), method, options...)
  25. }
  26. func InvokeModel(payload *InvokeModelRequest) (*stream.StreamResponse[InvokeModelResponseChunk], error) {
  27. return StreamResponse[InvokeModelResponseChunk]("POST", "invoke/model", requests.HttpPayloadJson(payload))
  28. }
  29. func InvokeTool(payload *InvokeToolRequest) (*stream.StreamResponse[InvokeToolResponseChunk], error) {
  30. return StreamResponse[InvokeToolResponseChunk]("POST", "invoke/tool", requests.HttpPayloadJson(payload))
  31. }
  32. func InvokeNode[T WorkflowNodeData](payload *InvokeNodeRequest[T]) (*InvokeNodeResponse, error) {
  33. return Request[InvokeNodeResponse]("POST", "invoke/node", requests.HttpPayloadJson(payload))
  34. }