2
0

(openai) Enable setVariable function in tools

Closes #1178
This commit is contained in:
Baptiste Arnaud
2024-01-22 09:22:28 +01:00
parent b438c174c4
commit 42008f8c18
24 changed files with 258 additions and 43 deletions

View File

@@ -50,6 +50,7 @@ const nextConfig = {
if (nextRuntime === 'edge') {
config.resolve.alias['minio'] = false
config.resolve.alias['got'] = false
config.resolve.alias['qrcode'] = false
return config
}
// These packages are imports from the integrations definition files that can be ignored for the client.

View File

@@ -1,6 +1,7 @@
import React from 'react'
import { Text } from '@chakra-ui/react'
import { ScriptBlock } from '@typebot.io/schemas'
import { defaultScriptOptions } from '@typebot.io/schemas/features/blocks/logic/script/constants'
type Props = {
options: ScriptBlock['options']
@@ -10,6 +11,6 @@ export const ScriptNodeContent = ({
options: { name, content } = {},
}: Props) => (
<Text color={content ? 'currentcolor' : 'gray.500'} noOfLines={1}>
{content ? `Run ${name}` : 'Configure...'}
{content ? `Run ${name ?? defaultScriptOptions.name}` : 'Configure...'}
</Text>
)

View File

@@ -4,6 +4,7 @@ import React from 'react'
import { TextInput } from '@/components/inputs'
import { ScriptBlock } from '@typebot.io/schemas'
import { defaultScriptOptions } from '@typebot.io/schemas/features/blocks/logic/script/constants'
import { SwitchWithLabel } from '@/components/inputs/SwitchWithLabel'
type Props = {
options: ScriptBlock['options']
@@ -13,9 +14,13 @@ type Props = {
export const ScriptSettings = ({ options, onOptionsChange }: Props) => {
const handleNameChange = (name: string) =>
onOptionsChange({ ...options, name })
const handleCodeChange = (content: string) =>
onOptionsChange({ ...options, content })
const updateClientExecution = (isExecutedOnClient: boolean) =>
onOptionsChange({ ...options, isExecutedOnClient })
return (
<Stack spacing={4}>
<TextInput
@@ -31,6 +36,15 @@ export const ScriptSettings = ({ options, onOptionsChange }: Props) => {
lang="javascript"
onChange={handleCodeChange}
/>
<SwitchWithLabel
label="Execute on client?"
moreInfoContent="Check this if you need access to client variables like `window` or `document`."
initialValue={
options?.isExecutedOnClient ??
defaultScriptOptions.isExecutedOnClient
}
onCheckChange={updateClientExecution}
/>
</Stack>
</Stack>
)

View File

@@ -137,11 +137,6 @@ const SetVariableValue = ({
case undefined:
return (
<>
<CodeEditor
defaultValue={options?.expressionToEvaluate ?? ''}
onChange={updateExpression}
lang="javascript"
/>
<SwitchWithLabel
label="Execute on client?"
moreInfoContent="Check this if you need access to client-only variables like `window` or `document`."
@@ -151,6 +146,11 @@ const SetVariableValue = ({
}
onCheckChange={updateClientExecution}
/>
<CodeEditor
defaultValue={options?.expressionToEvaluate ?? ''}
onChange={updateExpression}
lang="javascript"
/>
</>
)
case 'Map item with same index': {

View File

@@ -43,7 +43,9 @@ A more useful example would be, of course, to call an API to get the weather of
<img src="/images/blocks/integrations/openai/tools.png" alt="OpenAI tools" />
</Frame>
As you can see, the code block expects the body of the Javascript function. You can use the `return` keyword to return values.
As you can see, the code block expects the body of the Javascript function. You should use the `return` keyword to return value to give back to OpenAI as the result of the function.
If you'd like to set variables directly in this code block, you can use the [`setVariable` function](../logic/script#setvariable-function).
## Ask assistant

View File

@@ -3,9 +3,9 @@ title: Script block
icon: code
---
The "Script" block allows you to execute Javascript code. If you want to set a variable value with Javascript, use the [Set variable block](./set-variable) instead. You can't set a variable with the script block.
The "Script" block allows you to execute Javascript code.
**It doesn't allow you to create a custom visual block**
<Info>This block doesn't allow you to create a custom visual block</Info>
<Frame>
<img src="/images/blocks/logic/code.png" width="600" alt="Code block" />
@@ -18,6 +18,22 @@ You need to write `console.log({{My variable}})` instead of `console.log("{{My v
</Info>
## `setVariable` function
If you want to set a variable value with Javascript, the [Set variable block](./set-variable) is more appropriate for most cases.
However, if you'd like to set variables with the script blocks, you can use the `setVariable` function in your script:
```js
if({{My variable}} === 'foo') {
setVariable('My variable', 'bar')
} else {
setVariable('My variable', 'other')
}
```
The `setVariable` function is only available in script executed on the server, so it won't work if the `Execute on client?` is checked.
## Examples
### Reload page

View File

@@ -371,6 +371,9 @@
"content": {
"type": "string"
},
"isExecutedOnClient": {
"type": "boolean"
},
"shouldExecuteInParentContext": {
"type": "boolean"
}
@@ -6580,6 +6583,9 @@
"content": {
"type": "string"
},
"isExecutedOnClient": {
"type": "boolean"
},
"shouldExecuteInParentContext": {
"type": "boolean"
}
@@ -11101,6 +11107,9 @@
"content": {
"type": "string"
},
"isExecutedOnClient": {
"type": "boolean"
},
"shouldExecuteInParentContext": {
"type": "boolean"
}
@@ -24814,6 +24823,9 @@
"content": {
"type": "string"
},
"isExecutedOnClient": {
"type": "boolean"
},
"shouldExecuteInParentContext": {
"type": "boolean"
}
@@ -28073,6 +28085,9 @@
"content": {
"type": "string"
},
"isExecutedOnClient": {
"type": "boolean"
},
"shouldExecuteInParentContext": {
"type": "boolean"
}
@@ -30882,6 +30897,9 @@
"content": {
"type": "string"
},
"isExecutedOnClient": {
"type": "boolean"
},
"shouldExecuteInParentContext": {
"type": "boolean"
}

View File

@@ -4527,6 +4527,9 @@
"content": {
"type": "string"
},
"isExecutedOnClient": {
"type": "boolean"
},
"shouldExecuteInParentContext": {
"type": "boolean"
}
@@ -8289,6 +8292,9 @@
"content": {
"type": "string"
},
"isExecutedOnClient": {
"type": "boolean"
},
"shouldExecuteInParentContext": {
"type": "boolean"
}

View File

@@ -50,6 +50,7 @@ const nextConfig = {
if (nextRuntime === 'edge') {
config.resolve.alias['minio'] = false
config.resolve.alias['got'] = false
config.resolve.alias['qrcode'] = false
return config
}
// These packages are imports from the integrations definition files that can be ignored for the client.

View File

@@ -134,6 +134,7 @@ export async function POST(req: Request) {
credentials.iv
)
const variables: ReadOnlyVariableStore = {
list: () => state.typebotsQueue[0].typebot.variables,
get: (id: string) => {
const variable = state.typebotsQueue[0].typebot.variables.find(
(variable) => variable.id === id