@workflowai/code-generator
Advanced tools
Comparing version 1.0.5 to 1.0.6
import { JsonSchemaObject } from '@workflowai/schema'; | ||
export type FileDataProvider = 'fs-promise' | 'fetch' | 'axios'; | ||
type GeneratedCode = { | ||
@@ -17,2 +18,3 @@ language: 'bash' | 'typescript'; | ||
input: Record<string, unknown>; | ||
output: Record<string, unknown>; | ||
}; | ||
@@ -23,4 +25,6 @@ api?: { | ||
}; | ||
fileDataProvider?: FileDataProvider; | ||
}; | ||
type GetPlaygroundSnippetsResult = { | ||
isUsingFileDataProvider: boolean; | ||
installSdk: GeneratedCode; | ||
@@ -30,4 +34,5 @@ initializeClient: GeneratedCode; | ||
runTask: GeneratedCode; | ||
importTaskRun: GeneratedCode; | ||
}; | ||
export declare const getPlaygroundSnippets: (config: GetPlaygroundSnippetsConfig) => Promise<GetPlaygroundSnippetsResult>; | ||
export {}; |
@@ -23,6 +23,24 @@ "use strict"; | ||
}; | ||
const base64DataToFileDataProvider = (str, fileDataProvider) => { | ||
return str.replace(/"[-A-Za-z0-9+/]{50,}={0,3}"/g, { | ||
'fs-promise': 'readFile("/path/to/file")', | ||
fetch: '(await fetch("http://url.to/file")).arrayBuffer()', | ||
axios: '(await axios.get("http://url.to/file", { responseType: "arraybuffer" })).data', | ||
}[fileDataProvider]); | ||
}; | ||
const getPlaygroundSnippets = async (config) => { | ||
const { taskId, taskName, schema, groupId, example, api } = config; | ||
const { taskId, taskName, schema, groupId, example, api, fileDataProvider = 'fs-promise', } = { | ||
...config, | ||
}; | ||
const taskFunctionName = validVarName(taskName || taskId); | ||
const _stringifiedInput = JSON.stringify(example.input, null, 2); | ||
const beautifiedInput = base64DataToFileDataProvider(_stringifiedInput, fileDataProvider); | ||
const isUsingFileDataProvider = beautifiedInput !== _stringifiedInput; | ||
const fileProviderImport = { | ||
'fs-promise': 'import { readFile } from "fs/promises"\n', | ||
fetch: '', | ||
axios: 'import axios from "axios"\n', | ||
}[fileDataProvider]; | ||
return { | ||
isUsingFileDataProvider, | ||
installSdk: { | ||
@@ -73,11 +91,34 @@ language: 'bash', | ||
code: ` | ||
import { TaskInput } from "@workflowai/workflowai" | ||
${[ | ||
'import { TaskInput } from "@workflowai/workflowai"', | ||
isUsingFileDataProvider && fileProviderImport, | ||
] | ||
.filter(Boolean) | ||
.join('\n')} | ||
${`const input: TaskInput<typeof ${taskFunctionName}> = ${(0, beautify_1.beautifyTypescript)(JSON.stringify(example.input))}`} | ||
const input: TaskInput<typeof ${taskFunctionName}> = ${beautifiedInput} | ||
const output = await ${taskFunctionName}(input) | ||
console.log(output) | ||
`.trim(), | ||
}, | ||
importTaskRun: { | ||
language: 'typescript', | ||
code: ` | ||
${[ | ||
'import { TaskInput, TaskOutput } from "@workflowai/workflowai"', | ||
isUsingFileDataProvider && fileProviderImport, | ||
] | ||
.filter(Boolean) | ||
.join('\n')} | ||
const input: TaskInput<typeof ${taskFunctionName}> = ${beautifiedInput} | ||
const ouput: TaskOutput<typeof ${taskFunctionName}> = ${(0, beautify_1.beautifyTypescript)(JSON.stringify(example.output))} | ||
await ${taskFunctionName}.importRun(input, output) | ||
`.trim(), | ||
}, | ||
}; | ||
}; | ||
exports.getPlaygroundSnippets = getPlaygroundSnippets; |
@@ -20,6 +20,24 @@ import { inputSchemaToZod, outputSchemaToZod, } from '@workflowai/schema'; | ||
}; | ||
const base64DataToFileDataProvider = (str, fileDataProvider) => { | ||
return str.replace(/"[-A-Za-z0-9+/]{50,}={0,3}"/g, { | ||
'fs-promise': 'readFile("/path/to/file")', | ||
fetch: '(await fetch("http://url.to/file")).arrayBuffer()', | ||
axios: '(await axios.get("http://url.to/file", { responseType: "arraybuffer" })).data', | ||
}[fileDataProvider]); | ||
}; | ||
export const getPlaygroundSnippets = async (config) => { | ||
const { taskId, taskName, schema, groupId, example, api } = config; | ||
const { taskId, taskName, schema, groupId, example, api, fileDataProvider = 'fs-promise', } = { | ||
...config, | ||
}; | ||
const taskFunctionName = validVarName(taskName || taskId); | ||
const _stringifiedInput = JSON.stringify(example.input, null, 2); | ||
const beautifiedInput = base64DataToFileDataProvider(_stringifiedInput, fileDataProvider); | ||
const isUsingFileDataProvider = beautifiedInput !== _stringifiedInput; | ||
const fileProviderImport = { | ||
'fs-promise': 'import { readFile } from "fs/promises"\n', | ||
fetch: '', | ||
axios: 'import axios from "axios"\n', | ||
}[fileDataProvider]; | ||
return { | ||
isUsingFileDataProvider, | ||
installSdk: { | ||
@@ -70,10 +88,33 @@ language: 'bash', | ||
code: ` | ||
import { TaskInput } from "@workflowai/workflowai" | ||
${[ | ||
'import { TaskInput } from "@workflowai/workflowai"', | ||
isUsingFileDataProvider && fileProviderImport, | ||
] | ||
.filter(Boolean) | ||
.join('\n')} | ||
${`const input: TaskInput<typeof ${taskFunctionName}> = ${beautifyTypescript(JSON.stringify(example.input))}`} | ||
const input: TaskInput<typeof ${taskFunctionName}> = ${beautifiedInput} | ||
const output = await ${taskFunctionName}(input) | ||
console.log(output) | ||
`.trim(), | ||
}, | ||
importTaskRun: { | ||
language: 'typescript', | ||
code: ` | ||
${[ | ||
'import { TaskInput, TaskOutput } from "@workflowai/workflowai"', | ||
isUsingFileDataProvider && fileProviderImport, | ||
] | ||
.filter(Boolean) | ||
.join('\n')} | ||
const input: TaskInput<typeof ${taskFunctionName}> = ${beautifiedInput} | ||
const ouput: TaskOutput<typeof ${taskFunctionName}> = ${beautifyTypescript(JSON.stringify(example.output))} | ||
await ${taskFunctionName}.importRun(input, output) | ||
`.trim(), | ||
}, | ||
}; | ||
}; |
import { JsonSchemaObject } from '@workflowai/schema'; | ||
export type FileDataProvider = 'fs-promise' | 'fetch' | 'axios'; | ||
type GeneratedCode = { | ||
@@ -17,2 +18,3 @@ language: 'bash' | 'typescript'; | ||
input: Record<string, unknown>; | ||
output: Record<string, unknown>; | ||
}; | ||
@@ -23,4 +25,6 @@ api?: { | ||
}; | ||
fileDataProvider?: FileDataProvider; | ||
}; | ||
type GetPlaygroundSnippetsResult = { | ||
isUsingFileDataProvider: boolean; | ||
installSdk: GeneratedCode; | ||
@@ -30,2 +34,3 @@ initializeClient: GeneratedCode; | ||
runTask: GeneratedCode; | ||
importTaskRun: GeneratedCode; | ||
}; | ||
@@ -32,0 +37,0 @@ export declare const getPlaygroundSnippets: (config: GetPlaygroundSnippetsConfig) => Promise<GetPlaygroundSnippetsResult>; |
{ | ||
"name": "@workflowai/code-generator", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "workflowAI code-generator", | ||
@@ -5,0 +5,0 @@ "author": "workflowAI", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15822
347