first commit
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
import { NodeOperationError } from 'n8n-workflow';
|
||||
import type { IExecuteFunctions, IDataObject } from 'n8n-workflow';
|
||||
|
||||
import type { IAirtopNodeExecutionData, IAirtopResponse } from '../../transport/types';
|
||||
|
||||
/**
|
||||
* Parse JSON when the 'Parse JSON Output' parameter is enabled
|
||||
* @param this - The execution context
|
||||
* @param index - The index of the node
|
||||
* @param response - The Airtop API response to parse
|
||||
* @returns The parsed output
|
||||
*/
|
||||
export function parseJsonIfPresent(
|
||||
this: IExecuteFunctions,
|
||||
index: number,
|
||||
response: IAirtopResponse,
|
||||
): IAirtopResponse {
|
||||
const parseJsonOutput = this.getNodeParameter('additionalFields.parseJsonOutput', index, false);
|
||||
const outputJsonSchema = this.getNodeParameter(
|
||||
'additionalFields.outputSchema',
|
||||
index,
|
||||
'',
|
||||
) as string;
|
||||
|
||||
if (!parseJsonOutput || !outputJsonSchema.startsWith('{')) {
|
||||
return response;
|
||||
}
|
||||
|
||||
try {
|
||||
const output = JSON.parse(response.data?.modelResponse ?? '') as IDataObject;
|
||||
return {
|
||||
sessionId: response.sessionId,
|
||||
windowId: response.windowId,
|
||||
output,
|
||||
};
|
||||
} catch (error) {
|
||||
throw new NodeOperationError(this.getNode(), 'Output is not a valid JSON');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up the output when used as a tool
|
||||
* @param output - The output to clean up
|
||||
* @returns The cleaned up output
|
||||
*/
|
||||
export function cleanOutputForToolUse(output: IAirtopNodeExecutionData[]) {
|
||||
const getOutput = (executionData: IAirtopNodeExecutionData) => {
|
||||
// Return error message
|
||||
if (executionData.json?.errors?.length) {
|
||||
const errorMessage = executionData.json?.errors[0].message as string;
|
||||
return {
|
||||
output: `Error: ${errorMessage}`,
|
||||
};
|
||||
}
|
||||
|
||||
// Return output parsed from JSON
|
||||
if (executionData.json?.output) {
|
||||
return executionData.json?.output;
|
||||
}
|
||||
|
||||
// Return model response
|
||||
if (executionData.json?.data?.modelResponse) {
|
||||
return {
|
||||
output: executionData.json?.data?.modelResponse,
|
||||
};
|
||||
}
|
||||
|
||||
// Return everything else
|
||||
return {
|
||||
output: { ...(executionData.json?.data ?? {}) },
|
||||
};
|
||||
};
|
||||
|
||||
return output.map((executionData) => ({
|
||||
...executionData,
|
||||
json: {
|
||||
...getOutput(executionData),
|
||||
},
|
||||
}));
|
||||
}
|
||||
Reference in New Issue
Block a user