Skip to content

Commit b2ff889

Browse files
jeevikasirwanijcortes
authored andcommitted
solves issue #17031
1 parent 4a4c5a2 commit b2ff889

File tree

4 files changed

+235
-25
lines changed

4 files changed

+235
-25
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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+
};

components/openai/common/helpers.mjs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,50 @@ export function parse(value) {
3737
throw new ConfigurationError("Make sure the schema contains a valid JSON object.");
3838
}
3939
}
40+
41+
const parseJson = (input, maxDepth = 100) => {
42+
const seen = new WeakSet();
43+
const parse = (value) => {
44+
if (maxDepth <= 0) {
45+
return value;
46+
}
47+
if (typeof(value) === "string") {
48+
// Only parse if the string looks like a JSON object or array
49+
const trimmed = value.trim();
50+
if (
51+
(trimmed.startsWith("{") && trimmed.endsWith("}")) ||
52+
(trimmed.startsWith("[") && trimmed.endsWith("]"))
53+
) {
54+
try {
55+
return parseJson(JSON.parse(value), maxDepth - 1);
56+
} catch (e) {
57+
return value;
58+
}
59+
}
60+
return value;
61+
} else if (typeof(value) === "object" && value !== null) {
62+
if (seen.has(value)) {
63+
return value;
64+
}
65+
seen.add(value);
66+
return Object.entries(value)
67+
.reduce((acc, [
68+
key,
69+
val,
70+
]) => Object.assign(acc, {
71+
[key]: parse(val),
72+
}), {});
73+
}
74+
return value;
75+
};
76+
77+
return parse(input);
78+
};
79+
80+
export function parseArray (input, maxDepth = 100) {
81+
if (!Array.isArray(input)) {
82+
return input;
83+
}
84+
85+
return input.map((item) => parseJson(item, maxDepth));
86+
}

components/openai/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/openai",
3-
"version": "1.0.2",
3+
"version": "1.1.0",
44
"description": "Pipedream OpenAI Components",
55
"main": "openai.app.mjs",
66
"keywords": [
@@ -15,8 +15,8 @@
1515
},
1616
"dependencies": {
1717
"@pipedream/platform": "^3.1.0",
18+
"@pipedream/sdk": "^1.7.0",
1819
"@pipedream/types": "^0.1.4",
19-
"axios": "^1.6.2",
2020
"bottleneck": "^2.19.5",
2121
"form-data": "^4.0.0",
2222
"got": "^12.6.0",

pnpm-lock.yaml

Lines changed: 25 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)