Function
A function is a script/piece of code to be executed. Functions can be executed during the lifecycle of a conversation, or called automatically by an AI character. Executing a function produces a function invocation.
Example function
{
"id": "659db1a6ce50583474be7200",
"characterId": "659a21168ad10b77542a3587",
"createdAt": "2024-01-09T20:50:46.870Z",
"code": "async function createProduct2() {\n if (!ctx.conversation) {\n return;\n }\n\n const state = JSON.parse(ctx.conversation.state);\n const userToken = state[\"userToken\"];\n const apiKeys = state[\"apiKeys\"];\n console.log(userToken);\n console.log(apiKeys);\n\n if (!userToken || !apiKeys) {\n await ctx.character.sendMessage(\"No user token or API keys found\", \"TEXT\");\n return;\n }\n\n console.log(\">>>>args\");\n console.log(ctx.args);\n console.log(typeof ctx.args);\n\n const res = await fetch(\n\t\t`https://prodapi.retailloop.co/api/v1/product/create${apiKeys}`,\n\t\t{\n\t\t\theaders: {\n\t\t\t\t\"Authorization\": userToken,\n\t\t\t},\n\t\t\tmethod: \"POST\",\n body: JSON.stringify(ctx.args || {}),\n\t\t}\n\t);\n const json = await res.json();\n\n if (json.error) {\n if (typeof json.error === \"string\") {\n await ctx.character.sendMessage(json.error, \"TEXT\");\n } else {\n await ctx.character.sendMessage(json.error?.message || \"Could not sign in\", \"TEXT\");\n }\n return;\n }\n\n console.log(json.data.name);\n\n const image = json.data.images[0];\n console.log(image);\n\n const newProductForm = {\n schema: '{' +\n '\"$schema\":\"http://json-schema.org/draft-07/schema#\",' +\n '\"type\":\"object\",' +\n '\"title\":\"Created Product\",' +\n '\"description\":\"Successfully created new product\",' +\n '\"properties\":{' +\n (image ? '\"image\":{\"type\":\"string\",\"title\":\"Images\",\"description\":\"\",\"properties\":{},\"format\":\"image\",\"default\":\"' + image +'\"},' : '') +\n '\"name\":{\"type\":\"string\",\"title\":\"\",\"description\":\"\",\"properties\":{},\"default\":\"' + json.data.name +'\",\"format\":\"label\"},' +\n '\"description\":{\"type\":\"string\",\"title\":\"\",\"description\":\"\",\"properties\":{},\"default\":\"' + json.data.description +'\",\"format\":\"label\"}},' +\n '\"required\":[]}',\n actions: [{\n isPrimary: true,\n type: \"EXTERNAL_LINK\",\n payload: `https://app.retailloop.co/inventory/products/${json.data.id}`,\n text: \"View\"\n }]\n };\n\n await ctx.character.sendMessage(JSON.stringify(newProductForm), \"UI_FORM\");\n\n return true;\n}\n\ncreateProduct2().catch(console.log);",
"description": "Function for creating a new product",
"name": "createProduct",
"parameters": "{\"$schema\":\"http://json-schema.org/draft-07/schema#\",\"type\":\"object\",\"title\":\"\",\"description\":\"\",\"properties\":{\"name\":{\"type\":\"string\",\"title\":\"\",\"description\":\"\",\"properties\":{}},\"description\":{\"type\":\"string\",\"title\":\"\",\"description\":\"\",\"properties\":{}},\"quantity\":{\"type\":\"string\",\"title\":\"\",\"description\":\"\",\"properties\":{}},\"Quantity_type\":{\"type\":\"string\",\"title\":\"\",\"description\":\"\",\"properties\":{}},\"price\":{\"type\":\"string\",\"title\":\"\",\"description\":\"\",\"properties\":{}},\"currency\":{\"type\":\"string\",\"title\":\"\",\"description\":\"\",\"properties\":{}},\"discountPrice\":{\"type\":\"string\",\"title\":\"\",\"description\":\"\",\"properties\":{}},\"discountPriceCurrency\":{\"type\":\"string\",\"title\":\"\",\"description\":\"\",\"properties\":{}},\"images\":{\"type\":\"string\",\"title\":\"\",\"description\":\"\",\"properties\":{}},\"barcode\":{\"type\":\"string\",\"title\":\"\",\"description\":\"\",\"properties\":{}},\"categories\":{\"type\":\"string\",\"title\":\"\",\"description\":\"\",\"properties\":{}},\"sku_code\":{\"type\":\"string\",\"title\":\"\",\"description\":\"\",\"properties\":{}}},\"required\":[]}",
"trigger": "ON_CHARACTER_CALL",
"updatedAt": "2024-01-09T20:53:12.302Z",
"variables": "{\"API_URL\":{\"value\":\"https://api.com\", \"isSecret\": false}}"
}
Properties
id string
The unique ID of the function. You can find your function's ID by visiting the functions page on ProteusAI. The function ID is located under the name of the function.
character Character can be included
The character to which the function belongs.
characterId string
The ID of the character to which the function belongs.
code string
The script to be executed.
description string optional
The description of the function.
invocations FunctionInvocation[] can be included
An array of invocation objects produced by executing this function.
logs FunctionLog[] can be included
An array of log objects produced by executing this function. A log object is produced whenever console.xxx
(e.g. console.log
) is called.
name string
The name of the function.
trigger FunctionTrigger optional
An event that can cause this function to be invoked.
variables string optional
A stringified JSON object that represents the function's environment variables. Environment variables can be
accessed via process.env
during the function's runtime. For instance, the environment variable DATABASE_URL
can be read via process.env.DATABASE_URL
.