|
| 1 | +import openai from "../../openai.app.mjs"; |
| 2 | +import common from "../common/common.mjs"; |
| 3 | +import constants from "../../common/constants.mjs"; |
| 4 | +import { parseArray } from "../../common/helpers.mjs"; |
| 5 | + |
| 6 | +export default { |
| 7 | + ...common, |
| 8 | + key: "openai-chat-with-responses-api", |
| 9 | + name: "Chat With Responses API", |
| 10 | + version: "0.0.1", |
| 11 | + description: "Send a chat via the Responses API, mixing built-in tools and MCP server tools. [See the documentation](https://platform.openai.com/docs/guides/tools?api-mode=responses).", |
| 12 | + type: "action", |
| 13 | + props: { |
| 14 | + openai, |
| 15 | + modelId: { |
| 16 | + description: "Model used to generate the response", |
| 17 | + propDefinition: [ |
| 18 | + openai, |
| 19 | + "chatCompletionModelId", |
| 20 | + ], |
| 21 | + }, |
| 22 | + input: { |
| 23 | + description: "Text input to the model used to generate a response", |
| 24 | + propDefinition: [ |
| 25 | + openai, |
| 26 | + "input", |
| 27 | + ], |
| 28 | + }, |
| 29 | + instructions: { |
| 30 | + description: "Inserts a system (or developer) message as the first item in the model's context", |
| 31 | + propDefinition: [ |
| 32 | + openai, |
| 33 | + "instructions", |
| 34 | + ], |
| 35 | + }, |
| 36 | + previousResponseId: { |
| 37 | + type: "string", |
| 38 | + label: "Previous Response ID", |
| 39 | + description: "The unique ID of the previous response to the model. Use this to create multi-turn conversations", |
| 40 | + optional: true, |
| 41 | + }, |
| 42 | + truncation: { |
| 43 | + type: "string", |
| 44 | + label: "Truncation", |
| 45 | + description: |
| 46 | + "Specifies the truncation mode for the response if it exceeds the context window", |
| 47 | + default: "auto", |
| 48 | + options: [ |
| 49 | + "auto", |
| 50 | + "disabled", |
| 51 | + ], |
| 52 | + optional: true, |
| 53 | + }, |
| 54 | + responseFormat: { |
| 55 | + type: "string", |
| 56 | + label: "Response Format", |
| 57 | + description: "- `text`: Returns unstructured text output.\n- `json_schema`: Enforces a specific structure using a JSON schema.", |
| 58 | + options: [ |
| 59 | + "text", |
| 60 | + "json_schema", |
| 61 | + ], |
| 62 | + default: "text", |
| 63 | + optional: true, |
| 64 | + reloadProps: true, |
| 65 | + }, |
| 66 | + builtInTools: { |
| 67 | + type: "string[]", |
| 68 | + label: "Built-In Tools", |
| 69 | + description: "Which of OpenAI's first-party tools to enable (web search, file search, code interpreter).", |
| 70 | + options: [ |
| 71 | + { |
| 72 | + label: "Web Search", |
| 73 | + value: "web_search_preview", |
| 74 | + }, |
| 75 | + { |
| 76 | + label: "File Search", |
| 77 | + value: "file_search", |
| 78 | + }, |
| 79 | + { |
| 80 | + label: "Code Interpreter", |
| 81 | + value: "code_interpreter", |
| 82 | + }, |
| 83 | + ], |
| 84 | + default: [], |
| 85 | + }, |
| 86 | + otherTools: { |
| 87 | + type: "string[]", |
| 88 | + label: "Other Tools", |
| 89 | + description: "Other tools to include in the chat. [See the documentation](https://platform.openai.com/docs/guides/tools-remote-mcp). Example: `{ type: \"mcp\", server_label: \"gmail\", server_url: \"https://remote.mcp.pipedream.net\", headers: {}, require_approval: \"never\" }`", |
| 90 | + optional: true, |
| 91 | + }, |
| 92 | + }, |
| 93 | + additionalProps() { |
| 94 | + const props = {}; |
| 95 | + if (this.responseFormat === constants.CHAT_RESPONSE_FORMAT.JSON_SCHEMA.value) { |
| 96 | + props.jsonSchema = { |
| 97 | + type: "string", |
| 98 | + label: "JSON Schema", |
| 99 | + description: |
| 100 | + "Define the schema that the model's output must adhere to.", |
| 101 | + }; |
| 102 | + } |
| 103 | + return props; |
| 104 | + }, |
| 105 | + async run({ $ }) { |
| 106 | + const { |
| 107 | + builtInTools, |
| 108 | + otherTools, |
| 109 | + modelId, |
| 110 | + input, |
| 111 | + instructions, |
| 112 | + previousResponseId, |
| 113 | + truncation, |
| 114 | + responseFormat, |
| 115 | + jsonSchema, |
| 116 | + } = this; |
| 117 | + |
| 118 | + const tools = builtInTools.map((tool) => ({ |
| 119 | + type: tool, |
| 120 | + })); |
| 121 | + |
| 122 | + if (otherTools) { |
| 123 | + tools.push(...parseArray(otherTools)); |
| 124 | + } |
| 125 | + |
| 126 | + const data = { |
| 127 | + model: modelId, |
| 128 | + input, |
| 129 | + instructions, |
| 130 | + previous_response_id: previousResponseId, |
| 131 | + truncation, |
| 132 | + tools, |
| 133 | + }; |
| 134 | + |
| 135 | + if (responseFormat === constants.CHAT_RESPONSE_FORMAT.JSON_SCHEMA.value) { |
| 136 | + try { |
| 137 | + data.text = { |
| 138 | + format: { |
| 139 | + type: responseFormat, |
| 140 | + ...JSON.parse(jsonSchema), |
| 141 | + }, |
| 142 | + }; |
| 143 | + } catch { |
| 144 | + throw new Error("Invalid JSON format in the provided JSON Schema"); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + const response = await this.openai.responses({ |
| 149 | + $, |
| 150 | + data, |
| 151 | + debug: true, |
| 152 | + }); |
| 153 | + |
| 154 | + if (response) { |
| 155 | + $.export("$summary", `Successfully sent chat to OpenAI Responses API with ID \`${response.id}\`.`); |
| 156 | + $.export("chat_responses", response.output); |
| 157 | + } |
| 158 | + |
| 159 | + return response; |
| 160 | + }, |
| 161 | +}; |
0 commit comments