|
| 1 | +import openai from "../../openai.app.mjs"; |
| 2 | +import common from "../common/common.mjs"; |
| 3 | +import constants from "../../common/constants.mjs"; |
| 4 | +import axios from "axios"; |
| 5 | + |
| 6 | +export default { |
| 7 | + ...common, |
| 8 | + key: "openai-chat-using-responses-api", |
| 9 | + name: "Chat with OpenAI Responses API", |
| 10 | + version: "0.0.1", |
| 11 | + description: |
| 12 | + "Send a chat via the Responses API, mixing built-in tools and MCP servers.", |
| 13 | + type: "action", |
| 14 | + props: { |
| 15 | + openai, |
| 16 | + modelId: { |
| 17 | + type: "string", |
| 18 | + label: "Model", |
| 19 | + description: "Model used to generate the response", |
| 20 | + default: "gpt-4o", |
| 21 | + options: [ |
| 22 | + "gpt-4o", |
| 23 | + "gpt-4o-mini", |
| 24 | + ], |
| 25 | + }, |
| 26 | + input: { |
| 27 | + type: "string", |
| 28 | + label: "Chat Input", |
| 29 | + description: "Text input to the model used to generate a response", |
| 30 | + }, |
| 31 | + instructions: { |
| 32 | + type: "string", |
| 33 | + label: "Instructions", |
| 34 | + description: |
| 35 | + "Inserts a system (or developer) message as the first item in the model's context", |
| 36 | + optional: true, |
| 37 | + }, |
| 38 | + previousResponseId: { |
| 39 | + type: "string", |
| 40 | + label: "Previous Response ID", |
| 41 | + description: |
| 42 | + "The unique ID of the previous response to the model. Use this to create multi-turn conversations", |
| 43 | + optional: true, |
| 44 | + }, |
| 45 | + truncation: { |
| 46 | + type: "string", |
| 47 | + label: "Truncation", |
| 48 | + description: |
| 49 | + "Specifies the truncation mode for the response if it exceeds the context window", |
| 50 | + default: "auto", |
| 51 | + options: [ |
| 52 | + "auto", |
| 53 | + "disabled", |
| 54 | + ], |
| 55 | + optional: true, |
| 56 | + }, |
| 57 | + responseFormat: { |
| 58 | + type: "string", |
| 59 | + label: "Response Format", |
| 60 | + description: |
| 61 | + "- **text**: Returns unstructured text output.\n- **json_schema**: Enforces a specific structure using a JSON schema.", |
| 62 | + options: [ |
| 63 | + "text", |
| 64 | + "json_schema", |
| 65 | + ], |
| 66 | + default: "text", |
| 67 | + optional: true, |
| 68 | + reloadProps: true, |
| 69 | + }, |
| 70 | + skipThisStep: { |
| 71 | + type: "boolean", |
| 72 | + label: "Skip This Step", |
| 73 | + description: |
| 74 | + "Pass in a boolean custom expression to skip execution at runtime", |
| 75 | + default: false, |
| 76 | + optional: true, |
| 77 | + }, |
| 78 | + builtInTools: { |
| 79 | + type: "string[]", |
| 80 | + label: "Built-In Tools", |
| 81 | + description: |
| 82 | + "Which of OpenAI’s first-party tools to enable (web search, file search, code interpreter).", |
| 83 | + options: [ |
| 84 | + { |
| 85 | + label: "Web Search", |
| 86 | + value: "web_search_preview", |
| 87 | + }, |
| 88 | + { |
| 89 | + label: "File Search", |
| 90 | + value: "file_search", |
| 91 | + }, |
| 92 | + { |
| 93 | + label: "Code Interpreter", |
| 94 | + value: "code_interpreter", |
| 95 | + }, |
| 96 | + ], |
| 97 | + default: [], |
| 98 | + }, |
| 99 | + mcpServers: { |
| 100 | + type: "string[]", |
| 101 | + label: "MCP Server URLs", |
| 102 | + description: |
| 103 | + "Enter your MCP server base URLs. To set one up, visit https://mcp.pipedream.com/ and click “Create new server.", |
| 104 | + optional: true, |
| 105 | + }, |
| 106 | + mcpTools: { |
| 107 | + type: "string[]", |
| 108 | + label: "MCP Tools to Enable", |
| 109 | + description: |
| 110 | + "Select which tools from your MCP servers to include in the chat.", |
| 111 | + optional: true, |
| 112 | + async options({ mcpServers }) { |
| 113 | + if (!mcpServers?.length) return []; |
| 114 | + const all = []; |
| 115 | + for (let url of mcpServers) { |
| 116 | + url = url.replace(/\/$/, ""); |
| 117 | + const { data } = await axios.get(`${url}/tools`); |
| 118 | + for (const t of data.tools) { |
| 119 | + all.push({ |
| 120 | + label: t.name, |
| 121 | + value: t.id, |
| 122 | + }); |
| 123 | + } |
| 124 | + } |
| 125 | + return all; |
| 126 | + }, |
| 127 | + }, |
| 128 | + }, |
| 129 | + additionalProps() { |
| 130 | + const props = {}; |
| 131 | + if ( |
| 132 | + this.responseFormat === |
| 133 | + constants.CHAT_RESPONSE_FORMAT.JSON_SCHEMA.value |
| 134 | + ) { |
| 135 | + props.jsonSchema = { |
| 136 | + type: "string", |
| 137 | + label: "JSON Schema", |
| 138 | + description: |
| 139 | + "Define the schema that the model's output must adhere to.", |
| 140 | + }; |
| 141 | + } |
| 142 | + return props; |
| 143 | + }, |
| 144 | + methods: { |
| 145 | + ...common.methods, |
| 146 | + }, |
| 147 | + async run({ $ }) { |
| 148 | + if (this.skipThisStep) { |
| 149 | + $.export("$summary", "Step execution skipped"); |
| 150 | + return; |
| 151 | + } |
| 152 | + const tools = []; |
| 153 | + for (const id of this.builtInTools) { |
| 154 | + tools.push({ |
| 155 | + type: id, |
| 156 | + }); |
| 157 | + } |
| 158 | + for (const id of this.mcpTools || []) { |
| 159 | + tools.push({ |
| 160 | + type: id, |
| 161 | + }); |
| 162 | + } |
| 163 | + const data = { |
| 164 | + model: this.modelId, |
| 165 | + input: this.input, |
| 166 | + instructions: this.instructions, |
| 167 | + previous_response_id: this.previousResponseId, |
| 168 | + truncation: this.truncation, |
| 169 | + tools, |
| 170 | + }; |
| 171 | + if ( |
| 172 | + this.responseFormat === |
| 173 | + constants.CHAT_RESPONSE_FORMAT.JSON_SCHEMA.value |
| 174 | + ) { |
| 175 | + try { |
| 176 | + data.text = { |
| 177 | + format: { |
| 178 | + type: this.responseFormat, |
| 179 | + ...JSON.parse(this.jsonSchema), |
| 180 | + }, |
| 181 | + }; |
| 182 | + } catch { |
| 183 | + throw new Error("Invalid JSON format in the provided JSON Schema"); |
| 184 | + } |
| 185 | + } |
| 186 | + const response = await this.openai.responses({ |
| 187 | + $, |
| 188 | + data, |
| 189 | + }); |
| 190 | + if (response) { |
| 191 | + $.export("$summary", `Chat sent (id: ${response.id})`); |
| 192 | + $.export("chat_responses", response.output); |
| 193 | + } |
| 194 | + return response; |
| 195 | + }, |
| 196 | +}; |
0 commit comments