@superblocksteam/cli 2.0.3-next.203 → 2.0.3-next.204

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -14,7 +14,7 @@ $ npm install -g @superblocksteam/cli
14
14
  $ superblocks COMMAND
15
15
  running command...
16
16
  $ superblocks (--version)
17
- @superblocksteam/cli/2.0.3-next.203 linux-x64 node-v20.19.0
17
+ @superblocksteam/cli/2.0.3-next.204 linux-x64 node-v20.19.0
18
18
  $ superblocks --help [COMMAND]
19
19
  USAGE
20
20
  $ superblocks COMMAND
@@ -32,7 +32,7 @@ var content = "### FULL EXAMPLES\n";
32
32
 
33
33
  // ../../../vite-plugin-file-sync/dist/ai-service/prompts/generated/subprompts/superblocks-api.js
34
34
  init_cjs_shims();
35
- var content2 = '### APIs\n\nThe Superblocks framework allows you to create backend APIs. The high level structure for creating APIs is as follows:\n\n1. APIs are defined using TypeScript files that live inside the apis directory inside the page they are scoped to. Example: /pages/Page1/apis/myApi.ts\n2. This pattern is a declarative workflow builder, where you define each API step, its configuration, and its execution order within the API workflow.\n3. To make the API available for use, you must import it into the scope file and register it with `SbApi()`, then import and destructure it in your page component for use.\n\n#### CRITICAL VARIABLE SCOPING RULES\n\n**\u{1F6A8} EXTREMELY IMPORTANT**: Variables referenced in API blocks can ONLY come from these sources:\n\n1. **Outputs of previous blocks** in the same API (accessed via the block name)\n2. **Page entities defined in the scope file** (passed as destructured parameters)\n3. **Never reference variables that don\'t exist** - this is the #1 cause of API generation errors\n\n**\u274C WRONG - Variables that don\'t exist in scope:**\n\n```ts\nnew PostgreSQL("insert_data", "postgres-integration-id", {\n statement: ({ SelectedCustomerIdVar, ProductNameInput, IssueTypeDropdown }) =>\n `INSERT INTO issues VALUES (${SelectedCustomerIdVar.value}, \'${ProductNameInput.value}\', \'${IssueTypeDropdown.selectedOptionValue}\')`,\n // \u274C ERROR: SelectedCustomerIdVar, ProductNameInput, IssueTypeDropdown are not defined anywhere!\n});\n```\n\n**\u2705 CORRECT - Variables from scope entities:**\n\n```ts\n// First, define in scope.ts:\nexport const Page1Scope = createSbScope<{\n SelectedCustomerIdVar: any;\n ProductNameInput: any;\n IssueTypeDropdown: any;\n}>(\n () => ({\n // Register the API\n submitIssueApi: SbApi({}),\n }),\n { name: "Page1" },\n);\n\n// Then use in API:\nnew PostgreSQL("insert_data", "postgres-integration-id", {\n statement: ({ SelectedCustomerIdVar, ProductNameInput, IssueTypeDropdown }) =>\n `INSERT INTO issues VALUES (${SelectedCustomerIdVar.value}, \'${ProductNameInput.value}\', \'${IssueTypeDropdown.selectedOptionValue}\')`,\n // \u2705 CORRECT: These are page entities defined in the scope\n});\n```\n\n**\u2705 CORRECT - Variables from previous blocks:**\n\n```ts\nexport default new Api("processOrderApi", [\n new JavaScript("get_customer_data", {\n fn: () => ({ customerId: 123, customerName: "John Doe" }),\n }),\n new PostgreSQL("insert_order", "postgres-integration-id", {\n statement: ({ get_customer_data }) =>\n `INSERT INTO orders VALUES (${get_customer_data.output.customerId}, \'${get_customer_data.output.customerName}\')`,\n // \u2705 CORRECT: get_customer_data is a previous block in this API\n }),\n]);\n```\n\n#### CRITICAL MENTAL MODEL: APIs Access Page Scope (They Don\'t Define Parameters)\n\n**\u{1F6A8} FUNDAMENTAL MISCONCEPTION TO AVOID:**\n\n\u274C **WRONG THINKING**: "APIs define their input parameters like traditional backend services"\n\n```ts\n// WRONG - This is NOT how Superblocks APIs work!\nfunction submitOrder(customerId, productName) {\n // \u274C APIs don\'t define parameters!\n // API logic here\n}\n```\n\n\u2705 **CORRECT THINKING**: "APIs are frontend-coupled functions that automatically access the page\'s existing scope"\n\n```ts\n// CORRECT - APIs inherit page scope automatically\nnew PostgreSQL("insert_order", "postgres-integration-id", {\n statement: ({ SelectedCustomerIdVar, ProductNameInput }) =>\n // \u2191 This is NOT defining parameters - this is accessing existing page scope!\n // These variables must ALREADY exist in your page scope\n `INSERT INTO orders VALUES (${SelectedCustomerIdVar.value}, \'${ProductNameInput.value}\')`,\n});\n```\n\n**KEY CONCEPTS:**\n\n1. **APIs are frontend-aware**: They\'re tightly coupled to your page, not independent backend services\n2. **No parameter definition**: APIs cannot define their own input parameters\n3. **Scope inheritance only**: APIs automatically access whatever exists in your page scope\n4. **Mandatory order**: Page Scope \u2192 Components \u2192 APIs (scope must exist first)\n\n**The Flow:**\n\n```\nPage Scope Variables \u2192 APIs Automatically Access \u2192 Use in API Logic\n(Must exist first) \u2192 (No parameter passing) \u2192 (Just destructure from scope)\n```\n\n**Contrasting Examples:**\n\n\u274C **Traditional Backend API (NOT how Superblocks works):**\n\n```ts\n// This is how traditional APIs work - NOT Superblocks!\nfunction createOrder(customerId, productName, quantity) {\n // \u274C Defines own parameters\n return database.insert({\n customer_id: customerId,\n product: productName,\n qty: quantity,\n });\n}\n\n// Called like: createOrder(123, "Widget", 5) - parameters passed in\n```\n\n\u2705 **Superblocks API (Frontend-coupled):**\n\n```ts\n// This is how Superblocks APIs work - inherits page scope\nnew PostgreSQL("insert_order", "postgres-integration-id", {\n statement: ({ CustomerIdInput, ProductNameInput, QuantityInput }) => {\n // \u2191 NOT defining parameters! These must exist in page scope already\n return `INSERT INTO orders VALUES (${CustomerIdInput.value}, \'${ProductNameInput.value}\', ${QuantityInput.value})`;\n },\n});\n\n// No "calling with parameters" - scope variables are automatically available\n```\n\n#### Rules\n\n1. CRITICAL: The name of the API must be consistent across the API\'s TypeScript definition, the API\'s file name, references in page files, and the key used to register it in the scope file. See the consistent use of \'myApi\' below as an example.\n2. ALWAYS import ALL API classes from the superblocks library at the top of every API file. Use this complete import statement for every API file:\n3. When using database integrations (PostgreSQL, Snowflake, Databricks), the integration_id parameter should be the actual integration ID from your Superblocks workspace, not a placeholder string.\n4. **CRITICAL**: DO NOT reference variables that are not in scope. The ONLY things in scope are (1) the outputs of previous blocks that are in lexical scope and (2) page entities defined in the scope file.\n\n```ts\nimport {\n Api,\n JavaScript,\n Python,\n Databricks,\n Snowflake,\n PostgreSQL,\n RestApi,\n Email,\n Conditional,\n TryCatch,\n Variables,\n Loop,\n Parallel,\n Throw,\n Return,\n} from "@superblocksteam/library";\n```\n\n#### Examples\n\n##### Complete Example: Scope \u2192 Components \u2192 API Flow\n\nThis example shows the complete flow from defining variables in scope, to binding them to components, to using them in APIs.\n\n**Step 1: Define entities in scope file**\n\n```ts\n// /pages/Page1/scope.ts\nimport { createSbScope, SbApi } from "@superblocksteam/library";\n\nexport const Page1Scope = createSbScope<{\n CustomerNameInput: any;\n ProductNameInput: any;\n IssueTypeDropdown: any;\n IssueNotesInput: any;\n}>(\n () => ({\n // Register the API\n submitProductIssueApi: SbApi({}),\n }),\n {\n name: "Page1",\n },\n);\n\nexport const Page1 = Page1Scope.entities;\n```\n\n**Step 2: Use entities in page components**\n\n```tsx\n// /pages/Page1/index.tsx\nimport {\n SbPage,\n SbSection,\n SbColumn,\n SbInput,\n SbDropdown,\n SbButton,\n SbEventFlow,\n registerPage,\n} from "@superblocksteam/library";\nimport { Page1, Page1Scope } from "./scope";\n\nconst Page1Component = () => {\n const {\n CustomerNameInput,\n ProductNameInput,\n IssueTypeDropdown,\n IssueNotesInput,\n submitProductIssueApi,\n } = Page1;\n\n return (\n <SbPage name="Page1" height={Dim.fill()} width={Dim.fill()}>\n <SbSection height={Dim.fill()}>\n <SbColumn width={Dim.fill()}>\n <SbInput bind={CustomerNameInput} label="Customer Name" />\n <SbInput bind={ProductNameInput} label="Product Name" />\n <SbDropdown\n bind={IssueTypeDropdown}\n label="Issue Type"\n options={[\n { label: "Defect", value: "defect" },\n { label: "Complaint", value: "complaint" },\n { label: "Return", value: "return" },\n ]}\n />\n <SbInput bind={IssueNotesInput} label="Notes" multiline={true} />\n <SbButton\n label="Submit Issue"\n onClick={SbEventFlow.runApis([submitProductIssueApi])}\n />\n </SbColumn>\n </SbSection>\n </SbPage>\n );\n};\n\nexport default registerPage(Page1Component, Page1Scope);\n```\n\n**Step 3: Create API that uses the scope entities**\n\n```ts\n// /pages/Page1/apis/submitProductIssueApi.ts\n\nimport {\n Api,\n JavaScript,\n Python,\n Databricks,\n Snowflake,\n PostgreSQL,\n RestApi,\n Email,\n Conditional,\n TryCatch,\n Variables,\n Loop,\n Parallel,\n Throw,\n Return,\n} from "@superblocksteam/library";\n\nexport default new Api("submitProductIssueApi", [\n new Conditional("validate_inputs", {\n if: {\n when: ({\n CustomerNameInput,\n ProductNameInput,\n IssueTypeDropdown,\n }): boolean =>\n !CustomerNameInput.value ||\n !ProductNameInput.value ||\n !IssueTypeDropdown.selectedOptionValue,\n then: [\n new Throw("validation_error", {\n error: "Customer name, product name, and issue type are required",\n }),\n ],\n },\n }),\n new PostgreSQL("insert_issue", "your-postgresql-integration-id", {\n statement: ({\n CustomerNameInput,\n ProductNameInput,\n IssueTypeDropdown,\n IssueNotesInput,\n }) =>\n `INSERT INTO product_issues \n (customer_name, product_name, issue_type, notes, status, date_reported, created_by)\n VALUES (\n \'${CustomerNameInput.value}\', \n \'${ProductNameInput.value}\', \n \'${IssueTypeDropdown.selectedOptionValue}\', \n \'${IssueNotesInput.value || ""}\', \n \'Open\', \n NOW(), \n 1\n )`,\n }),\n new JavaScript("return_success", {\n fn: ({ insert_issue }) => ({\n success: true,\n message: "Issue submitted successfully",\n issueId: insert_issue.output?.insertId || null,\n }),\n }),\n]);\n```\n\n##### \u274C COMMON MISTAKES TO AVOID\n\n**\u274C WRONG: Using undefined variables**\n\n```ts\n// This is WRONG - these variables don\'t exist!\nexport default new Api("badExampleApi", [\n new PostgreSQL("insert_data", "postgres-integration-id", {\n statement: ({\n SelectedCustomerIdVar,\n ProductNameInput,\n IssueTypeDropdown,\n }) =>\n `INSERT INTO issues VALUES (${SelectedCustomerIdVar.value}, \'${ProductNameInput.value}\', \'${IssueTypeDropdown.selectedOptionValue}\')`,\n // \u274C ERROR: SelectedCustomerIdVar, ProductNameInput, IssueTypeDropdown are not defined in scope!\n }),\n]);\n```\n\n**\u274C WRONG: Mixing up variable names**\n\n```ts\n// Scope defines CustomerNameInput but API tries to use CustomerName\nexport default new Api("badExampleApi", [\n new PostgreSQL("insert_data", "postgres-integration-id", {\n statement: (\n { CustomerName }, // \u274C ERROR: Should be CustomerNameInput\n ) => `INSERT INTO issues VALUES (\'${CustomerName.value}\')`,\n }),\n]);\n```\n\n**\u274C WRONG: Not destructuring function parameters**\n\n```ts\n// This is WRONG - you must destructure the parameters\nexport default new Api("badExampleApi", [\n new PostgreSQL("insert_data", "postgres-integration-id", {\n statement: (\n state, // \u274C ERROR: Should destructure { CustomerNameInput }\n ) => `INSERT INTO issues VALUES (\'${state.CustomerNameInput.value}\')`,\n }),\n]);\n```\n\n##### Creating and registering a Superblocks API\n\nCreate the API by adding the myApi.ts file:\n\n```ts\n// /pages/Page1/apis/myApi.ts\n\nimport {\n Api,\n JavaScript,\n Python,\n Databricks,\n Snowflake,\n PostgreSQL,\n RestApi,\n Email,\n Conditional,\n TryCatch,\n Variables,\n Loop,\n Parallel,\n Throw,\n Return,\n} from "@superblocksteam/library";\n\nexport default new Api("myApi", [\n new JavaScript("retrieve_orders", {\n fn: () => {\n return [\n {\n id: "ORD-001",\n customerName: "John Smith",\n total: 149.99,\n },\n {\n id: "ORD-002",\n customerName: "Sarah Jones",\n total: 89.5,\n },\n ];\n },\n }),\n]);\n```\n\nThen register the myApi API in the scope file:\n\n```ts\n// /pages/Page1/scope.ts\n\nimport { createSbScope, SbApi } from "@superblocksteam/library";\n\nexport const Page1Scope = createSbScope(\n () => ({\n // Register the API in the scope\n retrieveOrdersApi: SbApi({}),\n }),\n {\n name: "Page1",\n },\n);\n\nexport const Page1 = Page1Scope.entities;\n```\n\nThen use the API in your page component:\n\n```tsx\n// /pages/Page1/index.tsx\n\nimport {\n SbPage,\n SbSection,\n SbColumn,\n SbButton,\n SbTable,\n sbComputed,\n SbEventFlow,\n registerPage,\n} from "@superblocksteam/library";\nimport { Page1, Page1Scope } from "./scope";\n\nconst Page1Component = () => {\n const { retrieveOrdersApi } = Page1;\n\n return (\n <SbPage name="Page1" height={Dim.fill()} width={Dim.fill()}>\n <SbSection height={Dim.fill()}>\n <SbColumn width={Dim.fill()}>\n <SbButton\n // APIs can be invoked with the SbEventFlow API\n onClick={SbEventFlow.runApis([retrieveOrdersApi])}\n label="Fetch Data"\n />\n {/* Access API response using sbComputed */}\n <SbTable tableData={sbComputed(() => retrieveOrdersApi.response)} />\n </SbColumn>\n </SbSection>\n </SbPage>\n );\n};\n\nexport default registerPage(Page1Component, Page1Scope);\n```\n\n##### Referencing the output of a previous block\n\nThink hard about how you access the output of previous steps. You MUST use the output property of the previous step variable. There is no other way to access the output of a previous step (other than using a Variable block, but that is not what you want in this case and should only be used in very specific cases).\n\n```ts\n// Path to this api would be: /pages/Page1/apis/getOrdersApi.ts\n\nimport {\n Api,\n JavaScript,\n Python,\n Databricks,\n Snowflake,\n PostgreSQL,\n RestApi,\n Email,\n Conditional,\n TryCatch,\n Variables,\n Loop,\n Parallel,\n Throw,\n Return,\n} from "@superblocksteam/library";\n\nexport default new Api("getOrdersApi", [\n new JavaScript("retrieve_orders", {\n fn: () => {\n return [\n {\n id: 1,\n customer: "John Smith",\n date: "2024-01-15",\n total: 199.99,\n status: "Pending",\n },\n {\n id: 2,\n customer: "Jane Doe",\n date: "2024-01-14",\n total: 149.99,\n status: "Shipped",\n },\n {\n id: 3,\n customer: "Bob Wilson",\n date: "2024-01-13",\n total: 299.99,\n status: "Delivered",\n },\n ];\n },\n }),\n new JavaScript("format_orders", {\n fn: ({ retrieve_orders }) => {\n return retrieve_orders.output.map((order) => ({\n ...order,\n date: new Date(order.date).toLocaleDateString(),\n }));\n },\n }),\n]);\n```\n\nThen you would register the API in your scope file and use it in your page component:\n\n```ts\n// /pages/Page1/scope.ts\nexport const Page1Scope = createSbScope(\n () => ({\n getOrdersApi: SbApi({}),\n }),\n {\n name: "Page1",\n },\n);\n```\n\n```tsx\n// /pages/Page1/index.tsx\nimport {\n SbPage,\n SbSection,\n SbColumn,\n SbTable,\n sbComputed,\n registerPage,\n} from "@superblocksteam/library";\nimport { Page1, Page1Scope } from "./scope";\n\nconst Page1Component = () => {\n const { getOrdersApi } = Page1;\n\n return (\n <SbPage name="Page1" height={Dim.fill()} width={Dim.fill()}>\n <SbSection height={Dim.fill()}>\n <SbColumn width={Dim.fill()}>\n <SbTable tableData={sbComputed(() => getOrdersApi.response)} />\n </SbColumn>\n </SbSection>\n </SbPage>\n );\n};\n\nexport default registerPage(Page1Component, Page1Scope);\n```\n\n##### Ensuring variable existence in application\n\n**\u{1F6A8} CRITICAL**: APIs cannot create their own variables - they only access what already exists in page scope!\n\nWhen creating an API that references variables like `FirstNameInput`, `LastNameInput`, and `SelectedUserIdVar`, these variables MUST exist in your page scope BEFORE you write the API. APIs don\'t define parameters - they inherit scope.\n\n**Mandatory Flow: Scope \u2192 Components \u2192 APIs**\n\n**STEP 1: Create variables in scope FIRST** (APIs cannot access variables that don\'t exist)\n\nSince you\'ve determined that we\'ll use input components to take in the first name and last name, you MUST ensure that you use the same names for the entities in the `scope.ts` file as the variable names in the API.\n\n```ts\n// /pages/Page1/scope.ts\n\nimport {\n createSbScope,\n SbApi,\n SbVariable,\n SbVariablePersistence,\n Global,\n} from "@superblocksteam/library";\n\nexport const Page1Scope = createSbScope<{\n FirstNameInput: any;\n LastNameInput: any;\n}>(\n // register non-component entities in the scope\n ({\n entities: {\n FirstNameInput,\n LastNameInput,\n handlePeopleUpdates,\n SelectedUserIdVar,\n },\n }) => ({\n handlePeopleUpdatesApi: SbApi({}),\n SelectedUserIdVar: SbVariable({\n defaultValue: Global.user.id,\n persistence: SbVariablePersistence.TEMPORARY,\n }),\n }),\n // configure page options\n {\n name: "Page1",\n },\n);\n\nexport const Page1 = Page1Scope.entities;\n```\n\nThen, use the variables in your page component:\n\n```tsx\n// /pages/Page1/index.tsx\n\nimport {\n SbPage,\n SbInput,\n SbEventFlow,\n registerPage,\n} from "@superblocksteam/library";\nimport { Page1, Page1Scope } from "./scope";\n\nconst Page1Component = () => {\n const {\n handlePeopleUpdatesApi,\n FirstNameInput,\n LastNameInput,\n SelectedUserIdVar,\n } = Page1;\n\n return (\n <SbPage name="Page1">\n <SbInput\n label="First Name"\n bind={FirstNameInput}\n minLength={1}\n inputType="TEXT"\n />\n <SbInput\n label="Last Name"\n bind={LastNameInput}\n minLength={1}\n inputType="TEXT"\n />\n {/* The rest of the page... */}\n </SbPage>\n );\n};\n\nexport default registerPage(Page1Component, Page1Scope);\n```\n\nFinally, create the API that references these variables:\n\n```ts\n// /pages/Page1/apis/handlePeopleUpdatesApi.ts\n\nimport {\n Api,\n JavaScript,\n Python,\n Databricks,\n Snowflake,\n PostgreSQL,\n RestApi,\n Email,\n Conditional,\n TryCatch,\n Variables,\n Loop,\n Parallel,\n Throw,\n Return,\n} from "@superblocksteam/library";\n\nexport default new Api("handlePeopleUpdatesApi", [\n new Conditional("validate", {\n if: {\n when: ({ FirstNameInput, LastNameInput }): boolean =>\n !FirstNameInput.isValid || !LastNameInput.isValid,\n then: [\n new Throw("reject", {\n error: "either the first name or last name is invalid",\n }),\n ],\n },\n }),\n new PostgreSQL("update", "your-postgresql-integration-id", {\n statement: ({ FirstNameInput, LastNameInput, SelectedUserIdVar }) =>\n `UPDATE people SET first_name = \'${FirstNameInput.value}\', last_name = \'${LastNameInput.value}\' WHERE id = ${SelectedUserIdVar.value}`,\n }),\n]);\n```\n\n#### The Superblocks API TypeScript Type\n\nBelow is the full TypeScript spec for the APIs you create:\n\n````ts\n// @superblocksteam/library\n\nexport type JsonValue =\n | undefined\n | null\n | number\n | string\n | boolean\n | JsonValue[]\n | object;\nexport type State = { [key: string]: JsonValue };\nexport type Binding<T> = T | ((state: State) => T);\ntype Integrations = { id: string; description: string; metadata: JsonValue }[];\n\nclass Block {\n constructor(name: string) {}\n public run(): { output: JsonValue } {\n /* ... */\n }\n}\n\nclass Integration extends Block {\n constructor(name: string, integration_id: string) {}\n}\n\ntype State = Record<string, JsonValue>;\n\nclass JavaScript extends Integration {\n constructor(\n name: string,\n config: {\n fn: (\n {\n /* ... */\n },\n ) => JsonValue;\n },\n ) {\n super(name, "javascript");\n }\n}\n\nclass Python extends Integration {\n constructor(\n name: string,\n config: {\n // We want to just put the python function body here. The scope is the same as it would be if it were a JavaScript integration.\n fn: string;\n },\n ) {\n super(name, "python");\n }\n}\n\nclass Databricks extends Integration {\n static integrations: Integrations = [\n /* ... */\n ];\n\n /**\n * @param {string} name The name of the block.\n * @param {string} integration_id The id of the integration.\n * @param {object} config The config object.\n * @returns {void}\n */\n constructor(\n name: string,\n integration_id: string,\n config: {\n statement: Binding<string>;\n },\n ) {\n super(name, integration_id);\n }\n}\n\nclass Snowflake extends Integration {\n static integrations: Integrations = [\n /* ... */\n ];\n\n /**\n * @param {string} name The name of the block.\n * @param {string} integration_id The id of the integration.\n * @param {object} config The config object.\n * @returns {void}\n */\n constructor(\n name: string,\n integration_id: string,\n config: {\n statement: Binding<string>;\n },\n ) {\n super(name, integration_id);\n }\n}\n\nclass PostgreSQL extends Integration {\n static integrations: Integrations = [\n /* ... */\n ];\n\n /**\n * @param {string} name The name of the block.\n * @param {string} integration_id The id of the integration.\n * @param {object} config The config object.\n * @returns {void}\n */\n constructor(\n name: string,\n integration_id: string,\n config: {\n statement: Binding<string>;\n },\n ) {\n super(name, integration_id);\n }\n}\n\nclass RestApi extends Integration {\n static integrations: Integrations = [\n /* ... */\n ];\n\n constructor(\n name: string,\n // If you need to make a request that is detached from an integration, you MUST set this to "restapi".\n integration: string = "restapi",\n config: {\n method: string;\n url: Binding<string>;\n headers?: { key: Binding<string>; value: Binding<string> }[];\n params?: { key: Binding<string>; value: Binding<string> }[];\n body?: Binding<string>;\n },\n openapi?: {\n path: string; // This is the path exactly as it appears in the OpenAPI spec.\n },\n ) {\n super(name, integration);\n }\n}\n\nclass GitHub extends RestApi {\n constructor(\n name: string,\n integration: string,\n config: {\n method: string;\n url: Binding<string>;\n headers?: { key: Binding<string>; value: Binding<string> }[];\n params?: { key: Binding<string>; value: Binding<string> }[];\n body?: Binding<string>;\n },\n openapi?: {\n path: string; // This is the path exactly as it appears in the OpenAPI spec.\n },\n ) {\n super(name, integration, config, openapi);\n }\n}\n\nclass Jira extends RestApi {\n constructor(\n name: string,\n integration: string,\n config: {\n method: string;\n url: Binding<string>;\n headers?: { key: Binding<string>; value: Binding<string> }[];\n params?: { key: Binding<string>; value: Binding<string> }[];\n body?: Binding<string>;\n },\n openapi?: {\n path: string; // This is the path exactly as it appears in the OpenAPI spec.\n },\n ) {\n super(name, integration, config, openapi);\n }\n}\n\nclass Email extends Integration {\n constructor(\n name: string,\n config: {\n from: Binding<string>;\n to: Binding<string>;\n subject: Binding<string>;\n cc?: Binding<string>;\n bcc?: Binding<string>;\n body?: Binding<string>;\n },\n ) {\n super(name);\n }\n}\n\nexport type Condition = {\n when: boolean | ((state: State) => boolean);\n then: Block[];\n};\n\nexport type Conditions = {\n if: Condition;\n elif?: Condition[];\n else?: Block[];\n};\n\nclass Conditional extends Block {\n constructor(name: string, config: Conditions) {\n super(name);\n }\n}\n\nclass TryCatch extends Block {\n constructor(\n name: string,\n config: {\n try: Block[];\n catch: Block[];\n finally?: Block[];\n variables: { error: string };\n },\n ) {\n super(name);\n }\n}\n\n/**\n * A Superblocks variable has the following access pattern:\n *\n * How to retrieve the value of a variable:\n * ```ts\n * CORRECT\n * my_variable.value\n *\n * // INCORRECT\n * my_variable\n * ```\n *\n * How to set the value of a variable:\n * ```ts\n * CORRECT\n * my_variable.set(value)\n *\n * // INCORRECT\n * my_variable = value\n * ```\n *\n */\n\nclass Variables extends Block {\n constructor(\n name: string,\n variables: {\n // The name of the variable.\n key: string;\n // The value of the variable.\n value: Binding<JsonValue>;\n }[],\n ) {\n super(name);\n }\n}\n\nclass Loop extends Block {\n constructor(\n name: string,\n config: {\n over: Binding<JsonValue[]>;\n variables: {\n // What the variable name for the current item is.\n item: string;\n // What the variable name for the current index is.\n index: string;\n };\n blocks: Block[];\n },\n ) {\n super(name);\n }\n}\n\nclass Parallel extends Block {\n constructor(\n name: string,\n config: {\n over: Binding<JsonValue[]>;\n variables: {\n // What the variable name for the current item is.\n item: string;\n };\n blocks: Block[];\n },\n ) {\n super(name);\n }\n}\n\nclass Throw extends Block {\n constructor(\n name: string,\n config: {\n error: Binding<JsonValue>;\n },\n ) {\n super(name);\n }\n}\n\nclass Return extends Block {\n constructor(\n name: string,\n config: {\n data: Binding<JsonValue>;\n },\n ) {\n super(name);\n }\n}\n\nclass Api {\n constructor(name: string, steps: Block[]) {}\n public get response(): JsonValue {\n /* ... */\n }\n public get error(): string | undefined {\n /* ... */\n }\n public run(): void {\n /* ... */\n }\n public cancel(): void {\n /* ... */\n }\n}\n````\n\n#### Rules for using Superblocks APIs\n\nThink hard about the following important rules for correctly using Superblocks APIs:\n\n- You MUST use a destructured object to access page scope variables in dynamic block fields. This syntax is NOT defining function parameters - it\'s accessing the inherited page scope.\n\n```ts\n// CORRECT: destructuring to access page scope variables that must already exist\n({ Dropdown1, TextInput1 }) => Dropdown1.selectedOptionsValue + TextInput1.value\n// \u2191 These variables (Dropdown1, TextInput1) must exist in your page scope!\n\n// INCORRECT: trying to use scope object directly\n(state) => state.Dropdown1.selectedOptionsValue + state.TextInput1.value\n// \u2191 This syntax doesn\'t work in Superblocks\n```\n\n- DO NOT reference variables that are not in scope or that don\'t exist. The ONLY things in scope are (1) the outputs of previous blocks that are in lexical scope and (2) page entities.\n\n- The result of each scope is the result of the last block in that scope. In the following example, the value of `sendEmail.response` is the result of the `return_summary` block. Use this information to carefully ensure that the last block in your API is the one that returns the value you want.\n\n```ts\nexport default new Api("sendEmailApi", [\n new Email("send_email", {\n from: "noreply@company.com",\n to: "test@test.com",\n subject: "Test Email",\n body: "This is a test email",\n }),\n new JavaScript("return_summary", {\n fn: () => "Email sent successfully!",\n }),\n]);\n```\n\n- Block outputs are immutable. Do not mutate the output of a block.\n\n- Backend APIs CANNOT mutate frontend state inside of the API\n\n- APIs are registered in scope files using `SbApi()` and then accessed in page components by destructuring from the scope entities. Make sure you name the key used in registerScope the same as the imported API, but do not pass the imported Api into the SbApi() call.\n\n- To access API responses in your UI, use `sbComputed(() => apiName.response)` or `sbComputed(() => apiName.error)`.\n\n- You will not always be told which integrations to use in your API; you will have to determine that yourself based on the data you need to fetch.\n\n- Never add comments to code you (the ai) generate. User added comments are fine - leave those!\n';
35
+ var content2 = '### APIs\n\nThe Superblocks framework allows you to create backend APIs. The high level structure for creating APIs is as follows:\n\n1. APIs are defined using TypeScript files that live inside the apis directory inside the page they are scoped to. Example: /pages/Page1/apis/myApi.ts\n2. This pattern is a declarative workflow builder, where you define each API step, its configuration, and its execution order within the API workflow.\n3. To make the API available for use, you must import it into the scope file and register it with `SbApi()`, then import and destructure it in your page component for use.\n\n#### CRITICAL VARIABLE SCOPING RULES\n\n**\u{1F6A8} EXTREMELY IMPORTANT**: Variables referenced in API blocks can ONLY come from these sources:\n\n1. **Outputs of previous blocks** in the same API (accessed via the block name)\n2. **Page entities defined in the scope file** (passed as destructured parameters)\n3. **Never reference variables that don\'t exist** - this is the #1 cause of API generation errors\n\n**\u274C WRONG - Variables that don\'t exist in scope:**\n\n```ts\nnew PostgreSQL("insert_data", "postgres-integration-id", {\n statement: ({ SelectedCustomerIdVar, ProductNameInput, IssueTypeDropdown }) =>\n `INSERT INTO issues VALUES (${SelectedCustomerIdVar.value}, \'${ProductNameInput.value}\', \'${IssueTypeDropdown.selectedOptionValue}\')`,\n // \u274C ERROR: SelectedCustomerIdVar, ProductNameInput, IssueTypeDropdown are not defined anywhere!\n});\n```\n\n**\u2705 CORRECT - Variables from scope entities:**\n\n```ts\n// First, define in scope.ts:\nexport const Page1Scope = createSbScope<{\n SelectedCustomerIdVar: any;\n ProductNameInput: any;\n IssueTypeDropdown: any;\n}>(\n () => ({\n // Register the API\n submitIssueApi: SbApi({}),\n }),\n { name: "Page1" },\n);\n\n// Then use in API:\nnew PostgreSQL("insert_data", "postgres-integration-id", {\n statement: ({ SelectedCustomerIdVar, ProductNameInput, IssueTypeDropdown }) =>\n `INSERT INTO issues VALUES (${SelectedCustomerIdVar.value}, \'${ProductNameInput.value}\', \'${IssueTypeDropdown.selectedOptionValue}\')`,\n // \u2705 CORRECT: These are page entities defined in the scope\n});\n```\n\n**\u2705 CORRECT - Variables from previous blocks:**\n\n```ts\nexport default new Api("processOrderApi", [\n new JavaScript("get_customer_data", {\n fn: () => ({ customerId: 123, customerName: "John Doe" }),\n }),\n new PostgreSQL("insert_order", "postgres-integration-id", {\n statement: ({ get_customer_data }) =>\n `INSERT INTO orders VALUES (${get_customer_data.output.customerId}, \'${get_customer_data.output.customerName}\')`,\n // \u2705 CORRECT: get_customer_data is a previous block in this API\n }),\n]);\n```\n\n#### CRITICAL MENTAL MODEL: APIs Access Page Scope (They Don\'t Define Parameters)\n\n**\u{1F6A8} FUNDAMENTAL MISCONCEPTION TO AVOID:**\n\n\u274C **WRONG THINKING**: "APIs define their input parameters like traditional backend services"\n\n```ts\n// WRONG - This is NOT how Superblocks APIs work!\nfunction submitOrder(customerId, productName) {\n // \u274C APIs don\'t define parameters!\n // API logic here\n}\n```\n\n\u2705 **CORRECT THINKING**: "APIs are frontend-coupled functions that automatically access the page\'s existing scope"\n\n```ts\n// CORRECT - APIs inherit page scope automatically\nnew PostgreSQL("insert_order", "postgres-integration-id", {\n statement: ({ SelectedCustomerIdVar, ProductNameInput }) =>\n // \u2191 This is NOT defining parameters - this is accessing existing page scope!\n // These variables must ALREADY exist in your page scope\n `INSERT INTO orders VALUES (${SelectedCustomerIdVar.value}, \'${ProductNameInput.value}\')`,\n});\n```\n\n**KEY CONCEPTS:**\n\n1. **APIs are frontend-aware**: They\'re tightly coupled to your page, not independent backend services\n2. **No parameter definition**: APIs cannot define their own input parameters\n3. **Scope inheritance only**: APIs automatically access whatever exists in your page scope\n4. **Mandatory order**: Page Scope \u2192 Components \u2192 APIs (scope must exist first)\n\n**The Flow:**\n\n```\nPage Scope Variables \u2192 APIs Automatically Access \u2192 Use in API Logic\n(Must exist first) \u2192 (No parameter passing) \u2192 (Just destructure from scope)\n```\n\n**Contrasting Examples:**\n\n\u274C **Traditional Backend API (NOT how Superblocks works):**\n\n```ts\n// This is how traditional APIs work - NOT Superblocks!\nfunction createOrder(customerId, productName, quantity) {\n // \u274C Defines own parameters\n return database.insert({\n customer_id: customerId,\n product: productName,\n qty: quantity,\n });\n}\n\n// Called like: createOrder(123, "Widget", 5) - parameters passed in\n```\n\n\u2705 **Superblocks API (Frontend-coupled):**\n\n```ts\n// This is how Superblocks APIs work - inherits page scope\nnew PostgreSQL("insert_order", "postgres-integration-id", {\n statement: ({ CustomerIdInput, ProductNameInput, QuantityInput }) => {\n // \u2191 NOT defining parameters! These must exist in page scope already\n return `INSERT INTO orders VALUES (${CustomerIdInput.value}, \'${ProductNameInput.value}\', ${QuantityInput.value})`;\n },\n});\n\n// No "calling with parameters" - scope variables are automatically available\n```\n\n#### Rules\n\n1. CRITICAL: The name of the API must be consistent across the API\'s TypeScript definition, the API\'s file name, references in page files, and the key used to register it in the scope file. See the consistent use of \'myApi\' below as an example.\n2. ALWAYS import ALL API classes from the superblocks library at the top of every API file. Use this complete import statement for every API file:\n3. When using database integrations (PostgreSQL, Snowflake, Databricks), the integration_id parameter should be the actual integration ID from your Superblocks workspace, not a placeholder string.\n4. **CRITICAL**: DO NOT reference variables that are not in scope. The ONLY things in scope are (1) the outputs of previous blocks that are in lexical scope and (2) page entities defined in the scope file.\n\n```ts\nimport {\n Api,\n JavaScript,\n Python,\n Databricks,\n Snowflake,\n PostgreSQL,\n RestApi,\n Email,\n Conditional,\n TryCatch,\n Variables,\n Loop,\n Parallel,\n Throw,\n Return,\n} from "@superblocksteam/library";\n```\n\n#### Examples\n\n##### Complete Example: Scope \u2192 Components \u2192 API Flow\n\nThis example shows the complete flow from defining variables in scope, to binding them to components, to using them in APIs.\n\n**Step 1: Define entities in scope file**\n\n```ts\n// /pages/Page1/scope.ts\nimport { createSbScope, SbApi } from "@superblocksteam/library";\n\nexport const Page1Scope = createSbScope<{\n CustomerNameInput: any;\n ProductNameInput: any;\n IssueTypeDropdown: any;\n IssueNotesInput: any;\n}>(\n () => ({\n // Register the API\n submitProductIssueApi: SbApi({}),\n }),\n {\n name: "Page1",\n },\n);\n\nexport const Page1 = Page1Scope.entities;\n```\n\n**Step 2: Use entities in page components**\n\n```tsx\n// /pages/Page1/index.tsx\nimport {\n SbPage,\n SbSection,\n SbColumn,\n SbInput,\n SbDropdown,\n SbButton,\n SbEventFlow,\n registerPage,\n} from "@superblocksteam/library";\nimport { Page1, Page1Scope } from "./scope";\n\nconst Page1Component = () => {\n const {\n CustomerNameInput,\n ProductNameInput,\n IssueTypeDropdown,\n IssueNotesInput,\n submitProductIssueApi,\n } = Page1;\n\n return (\n <SbPage name="Page1" height={Dim.fill()} width={Dim.fill()}>\n <SbSection height={Dim.fill()}>\n <SbColumn width={Dim.fill()}>\n <SbInput bind={CustomerNameInput} label="Customer Name" />\n <SbInput bind={ProductNameInput} label="Product Name" />\n <SbDropdown\n bind={IssueTypeDropdown}\n label="Issue Type"\n options={[\n { label: "Defect", value: "defect" },\n { label: "Complaint", value: "complaint" },\n { label: "Return", value: "return" },\n ]}\n />\n <SbInput bind={IssueNotesInput} label="Notes" multiline={true} />\n <SbButton\n label="Submit Issue"\n onClick={SbEventFlow.runApis([submitProductIssueApi])}\n />\n </SbColumn>\n </SbSection>\n </SbPage>\n );\n};\n\nexport default registerPage(Page1Component, Page1Scope);\n```\n\n**Step 3: Create API that uses the scope entities**\n\n```ts\n// /pages/Page1/apis/submitProductIssueApi.ts\n\nimport {\n Api,\n JavaScript,\n Python,\n Databricks,\n Snowflake,\n PostgreSQL,\n RestApi,\n Email,\n Conditional,\n TryCatch,\n Variables,\n Loop,\n Parallel,\n Throw,\n Return,\n} from "@superblocksteam/library";\n\nexport default new Api("submitProductIssueApi", [\n new Conditional("validate_inputs", {\n if: {\n when: ({\n CustomerNameInput,\n ProductNameInput,\n IssueTypeDropdown,\n }): boolean =>\n !CustomerNameInput.value ||\n !ProductNameInput.value ||\n !IssueTypeDropdown.selectedOptionValue,\n then: [\n new Throw("validation_error", {\n error: "Customer name, product name, and issue type are required",\n }),\n ],\n },\n }),\n new PostgreSQL("insert_issue", "your-postgresql-integration-id", {\n statement: ({\n CustomerNameInput,\n ProductNameInput,\n IssueTypeDropdown,\n IssueNotesInput,\n }) =>\n `INSERT INTO product_issues \n (customer_name, product_name, issue_type, notes, status, date_reported, created_by)\n VALUES (\n \'${CustomerNameInput.value}\', \n \'${ProductNameInput.value}\', \n \'${IssueTypeDropdown.selectedOptionValue}\', \n \'${IssueNotesInput.value || ""}\', \n \'Open\', \n NOW(), \n 1\n )`,\n }),\n new JavaScript("return_success", {\n fn: ({ insert_issue }) => ({\n success: true,\n message: "Issue submitted successfully",\n issueId: insert_issue.output?.insertId || null,\n }),\n }),\n]);\n```\n\n##### \u274C COMMON MISTAKES TO AVOID\n\n**\u274C WRONG: Using undefined variables**\n\n```ts\n// This is WRONG - these variables don\'t exist!\nexport default new Api("badExampleApi", [\n new PostgreSQL("insert_data", "postgres-integration-id", {\n statement: ({\n SelectedCustomerIdVar,\n ProductNameInput,\n IssueTypeDropdown,\n }) =>\n `INSERT INTO issues VALUES (${SelectedCustomerIdVar.value}, \'${ProductNameInput.value}\', \'${IssueTypeDropdown.selectedOptionValue}\')`,\n // \u274C ERROR: SelectedCustomerIdVar, ProductNameInput, IssueTypeDropdown are not defined in scope!\n }),\n]);\n```\n\n**\u274C WRONG: Mixing up variable names**\n\n```ts\n// Scope defines CustomerNameInput but API tries to use CustomerName\nexport default new Api("badExampleApi", [\n new PostgreSQL("insert_data", "postgres-integration-id", {\n statement: (\n { CustomerName }, // \u274C ERROR: Should be CustomerNameInput\n ) => `INSERT INTO issues VALUES (\'${CustomerName.value}\')`,\n }),\n]);\n```\n\n**\u274C WRONG: Not destructuring function parameters**\n\n```ts\n// This is WRONG - you must destructure the parameters\nexport default new Api("badExampleApi", [\n new PostgreSQL("insert_data", "postgres-integration-id", {\n statement: (\n state, // \u274C ERROR: Should destructure { CustomerNameInput }\n ) => `INSERT INTO issues VALUES (\'${state.CustomerNameInput.value}\')`,\n }),\n]);\n```\n\n##### Creating and registering a Superblocks API\n\nCreate the API by adding the myApi.ts file:\n\n```ts\n// /pages/Page1/apis/myApi.ts\n\nimport {\n Api,\n JavaScript,\n Python,\n Databricks,\n Snowflake,\n PostgreSQL,\n RestApi,\n Email,\n Conditional,\n TryCatch,\n Variables,\n Loop,\n Parallel,\n Throw,\n Return,\n} from "@superblocksteam/library";\n\nexport default new Api("myApi", [\n new JavaScript("retrieve_orders", {\n fn: () => {\n return [\n {\n id: "ORD-001",\n customerName: "John Smith",\n total: 149.99,\n },\n {\n id: "ORD-002",\n customerName: "Sarah Jones",\n total: 89.5,\n },\n ];\n },\n }),\n]);\n```\n\nThen register the myApi API in the scope file:\n\n```ts\n// /pages/Page1/scope.ts\n\nimport { createSbScope, SbApi } from "@superblocksteam/library";\n\nexport const Page1Scope = createSbScope(\n () => ({\n // Register the API in the scope\n retrieveOrdersApi: SbApi({}),\n }),\n {\n name: "Page1",\n },\n);\n\nexport const Page1 = Page1Scope.entities;\n```\n\nThen use the API in your page component:\n\n```tsx\n// /pages/Page1/index.tsx\n\nimport {\n SbPage,\n SbSection,\n SbColumn,\n SbButton,\n SbTable,\n sbComputed,\n SbEventFlow,\n registerPage,\n} from "@superblocksteam/library";\nimport { Page1, Page1Scope } from "./scope";\n\nconst Page1Component = () => {\n const { retrieveOrdersApi } = Page1;\n\n return (\n <SbPage name="Page1" height={Dim.fill()} width={Dim.fill()}>\n <SbSection height={Dim.fill()}>\n <SbColumn width={Dim.fill()}>\n <SbButton\n // APIs can be invoked with the SbEventFlow API\n onClick={SbEventFlow.runApis([retrieveOrdersApi])}\n label="Fetch Data"\n />\n {/* Access API response using sbComputed */}\n <SbTable tableData={sbComputed(() => retrieveOrdersApi.response)} />\n </SbColumn>\n </SbSection>\n </SbPage>\n );\n};\n\nexport default registerPage(Page1Component, Page1Scope);\n```\n\n##### Referencing the output of a previous block\n\nThink hard about how you access the output of previous steps. You MUST use the output property of the previous step variable. There is no other way to access the output of a previous step (other than using a Variable block, but that is not what you want in this case and should only be used in very specific cases).\n\n```ts\n// Path to this api would be: /pages/Page1/apis/getOrdersApi.ts\n\nimport {\n Api,\n JavaScript,\n Python,\n Databricks,\n Snowflake,\n PostgreSQL,\n RestApi,\n Email,\n Conditional,\n TryCatch,\n Variables,\n Loop,\n Parallel,\n Throw,\n Return,\n} from "@superblocksteam/library";\n\nexport default new Api("getOrdersApi", [\n new JavaScript("retrieve_orders", {\n fn: () => {\n return [\n {\n id: 1,\n customer: "John Smith",\n date: "2024-01-15",\n total: 199.99,\n status: "Pending",\n },\n {\n id: 2,\n customer: "Jane Doe",\n date: "2024-01-14",\n total: 149.99,\n status: "Shipped",\n },\n {\n id: 3,\n customer: "Bob Wilson",\n date: "2024-01-13",\n total: 299.99,\n status: "Delivered",\n },\n ];\n },\n }),\n new JavaScript("format_orders", {\n fn: ({ retrieve_orders }) => {\n return retrieve_orders.output.map((order) => ({\n ...order,\n date: new Date(order.date).toLocaleDateString(),\n }));\n },\n }),\n]);\n```\n\nThen you would register the API in your scope file and use it in your page component:\n\n```ts\n// /pages/Page1/scope.ts\nexport const Page1Scope = createSbScope(\n () => ({\n getOrdersApi: SbApi({}),\n }),\n {\n name: "Page1",\n },\n);\n```\n\n```tsx\n// /pages/Page1/index.tsx\nimport {\n SbPage,\n SbSection,\n SbColumn,\n SbTable,\n sbComputed,\n registerPage,\n} from "@superblocksteam/library";\nimport { Page1, Page1Scope } from "./scope";\n\nconst Page1Component = () => {\n const { getOrdersApi } = Page1;\n\n return (\n <SbPage name="Page1" height={Dim.fill()} width={Dim.fill()}>\n <SbSection height={Dim.fill()}>\n <SbColumn width={Dim.fill()}>\n <SbTable tableData={sbComputed(() => getOrdersApi.response)} />\n </SbColumn>\n </SbSection>\n </SbPage>\n );\n};\n\nexport default registerPage(Page1Component, Page1Scope);\n```\n\n##### Ensuring variable existence in application\n\n**\u{1F6A8} CRITICAL**: APIs cannot create their own variables - they only access what already exists in page scope!\n\nWhen creating an API that references variables like `FirstNameInput`, `LastNameInput`, and `SelectedUserIdVar`, these variables MUST exist in your page scope BEFORE you write the API. APIs don\'t define parameters - they inherit scope.\n\n**Mandatory Flow: Scope \u2192 Components \u2192 APIs**\n\n**STEP 1: Create variables in scope FIRST** (APIs cannot access variables that don\'t exist)\n\nSince you\'ve determined that we\'ll use input components to take in the first name and last name, you MUST ensure that you use the same names for the entities in the `scope.ts` file as the variable names in the API.\n\n```ts\n// /pages/Page1/scope.ts\n\nimport {\n createSbScope,\n SbApi,\n SbVariable,\n SbVariablePersistence,\n Global,\n} from "@superblocksteam/library";\n\nexport const Page1Scope = createSbScope<{\n FirstNameInput: any;\n LastNameInput: any;\n}>(\n // register non-component entities in the scope\n ({\n entities: {\n FirstNameInput,\n LastNameInput,\n handlePeopleUpdates,\n SelectedUserIdVar,\n },\n }) => ({\n handlePeopleUpdatesApi: SbApi({}),\n SelectedUserIdVar: SbVariable({\n defaultValue: Global.user.id,\n persistence: SbVariablePersistence.TEMPORARY,\n }),\n }),\n // configure page options\n {\n name: "Page1",\n },\n);\n\nexport const Page1 = Page1Scope.entities;\n```\n\nThen, use the variables in your page component:\n\n```tsx\n// /pages/Page1/index.tsx\n\nimport {\n SbPage,\n SbInput,\n SbEventFlow,\n registerPage,\n} from "@superblocksteam/library";\nimport { Page1, Page1Scope } from "./scope";\n\nconst Page1Component = () => {\n const {\n handlePeopleUpdatesApi,\n FirstNameInput,\n LastNameInput,\n SelectedUserIdVar,\n } = Page1;\n\n return (\n <SbPage name="Page1">\n <SbInput\n label="First Name"\n bind={FirstNameInput}\n minLength={1}\n inputType="TEXT"\n />\n <SbInput\n label="Last Name"\n bind={LastNameInput}\n minLength={1}\n inputType="TEXT"\n />\n {/* The rest of the page... */}\n </SbPage>\n );\n};\n\nexport default registerPage(Page1Component, Page1Scope);\n```\n\nFinally, create the API that references these variables:\n\n```ts\n// /pages/Page1/apis/handlePeopleUpdatesApi.ts\n\nimport {\n Api,\n JavaScript,\n Python,\n Databricks,\n Snowflake,\n PostgreSQL,\n RestApi,\n Email,\n Conditional,\n TryCatch,\n Variables,\n Loop,\n Parallel,\n Throw,\n Return,\n} from "@superblocksteam/library";\n\nexport default new Api("handlePeopleUpdatesApi", [\n new Conditional("validate", {\n if: {\n when: ({ FirstNameInput, LastNameInput }): boolean =>\n !FirstNameInput.isValid || !LastNameInput.isValid,\n then: [\n new Throw("reject", {\n error: "either the first name or last name is invalid",\n }),\n ],\n },\n }),\n new PostgreSQL("update", "your-postgresql-integration-id", {\n statement: ({ FirstNameInput, LastNameInput, SelectedUserIdVar }) =>\n `UPDATE people SET first_name = \'${FirstNameInput.value}\', last_name = \'${LastNameInput.value}\' WHERE id = ${SelectedUserIdVar.value}`,\n }),\n]);\n```\n\n#### The Superblocks API TypeScript Type\n\nBelow is the full TypeScript spec for the APIs you create:\n\n````ts\n// @superblocksteam/library\n\nexport type JsonValue =\n | undefined\n | null\n | number\n | string\n | boolean\n | JsonValue[]\n | object;\nexport type State = { [key: string]: JsonValue };\nexport type Binding<T> = T | ((state: State) => T);\ntype Integrations = { id: string; description: string; metadata: JsonValue }[];\n\nclass Block {\n constructor(name: string) {}\n public run(): { output: JsonValue } {\n /* ... */\n }\n}\n\nclass Integration extends Block {\n constructor(name: string, integration_id: string) {}\n}\n\ntype State = Record<string, JsonValue>;\n\nclass JavaScript extends Integration {\n constructor(\n name: string,\n config: {\n fn: (\n {\n /* ... */\n },\n ) => JsonValue;\n },\n ) {\n super(name, "javascript");\n }\n}\n\nclass Python extends Integration {\n constructor(\n name: string,\n config: {\n // We want to just put the python function body here. The scope is the same as it would be if it were a JavaScript integration.\n fn: string;\n },\n ) {\n super(name, "python");\n }\n}\n\nclass Databricks extends Integration {\n static integrations: Integrations = [\n /* ... */\n ];\n\n /**\n * @param {string} name The name of the block.\n * @param {string} integration_id The id of the integration.\n * @param {object} config The config object.\n * @returns {void}\n */\n constructor(\n name: string,\n integration_id: string,\n config: {\n statement: Binding<string>;\n },\n ) {\n super(name, integration_id);\n }\n}\n\nclass Snowflake extends Integration {\n static integrations: Integrations = [\n /* ... */\n ];\n\n /**\n * @param {string} name The name of the block.\n * @param {string} integration_id The id of the integration.\n * @param {object} config The config object.\n * @returns {void}\n */\n constructor(\n name: string,\n integration_id: string,\n config: {\n statement: Binding<string>;\n },\n ) {\n super(name, integration_id);\n }\n}\n\nclass PostgreSQL extends Integration {\n static integrations: Integrations = [\n /* ... */\n ];\n\n /**\n * @param {string} name The name of the block.\n * @param {string} integration_id The id of the integration.\n * @param {object} config The config object.\n * @returns {void}\n */\n constructor(\n name: string,\n integration_id: string,\n config: {\n statement: Binding<string>;\n },\n ) {\n super(name, integration_id);\n }\n}\n\nclass RestApi extends Integration {\n static integrations: Integrations = [\n /* ... */\n ];\n\n constructor(\n name: string,\n // If you need to make a request that is detached from an integration, you MUST set this to "restapi".\n integration: string = "restapi",\n config: {\n method: string;\n url: Binding<string>;\n headers?: { key: Binding<string>; value: Binding<string> }[];\n params?: { key: Binding<string>; value: Binding<string> }[];\n body?: Binding<string>;\n },\n openapi?: {\n path: string; // This is the path exactly as it appears in the OpenAPI spec.\n },\n ) {\n super(name, integration);\n }\n}\n\nclass GitHub extends RestApi {\n constructor(\n name: string,\n integration: string,\n config: {\n method: string;\n url: Binding<string>;\n headers?: { key: Binding<string>; value: Binding<string> }[];\n params?: { key: Binding<string>; value: Binding<string> }[];\n body?: Binding<string>;\n },\n openapi?: {\n path: string; // This is the path exactly as it appears in the OpenAPI spec.\n },\n ) {\n super(name, integration, config, openapi);\n }\n}\n\nclass Jira extends RestApi {\n constructor(\n name: string,\n integration: string,\n config: {\n method: string;\n url: Binding<string>;\n headers?: { key: Binding<string>; value: Binding<string> }[];\n params?: { key: Binding<string>; value: Binding<string> }[];\n body?: Binding<string>;\n },\n openapi?: {\n path: string; // This is the path exactly as it appears in the OpenAPI spec.\n },\n ) {\n super(name, integration, config, openapi);\n }\n}\n\nclass Email extends Integration {\n constructor(\n name: string,\n config: {\n from: Binding<string>;\n to: Binding<string>;\n subject: Binding<string>;\n cc?: Binding<string>;\n bcc?: Binding<string>;\n body?: Binding<string>;\n },\n ) {\n super(name);\n }\n}\n\nexport type Condition = {\n when: boolean | ((state: State) => boolean);\n then: Block[];\n};\n\nexport type Conditions = {\n if: Condition;\n elif?: Condition[];\n else?: Block[];\n};\n\nclass Conditional extends Block {\n constructor(name: string, config: Conditions) {\n super(name);\n }\n}\n\nclass TryCatch extends Block {\n constructor(\n name: string,\n config: {\n try: Block[];\n catch: Block[];\n finally?: Block[];\n variables: { error: string };\n },\n ) {\n super(name);\n }\n}\n\n/**\n * A Superblocks variable has the following access pattern:\n *\n * How to retrieve the value of a variable:\n * ```ts\n * CORRECT\n * my_variable.value\n *\n * // INCORRECT\n * my_variable\n * ```\n *\n * How to set the value of a variable:\n * ```ts\n * CORRECT\n * my_variable.set(value)\n *\n * // INCORRECT\n * my_variable = value\n * ```\n *\n */\n\nclass Variables extends Block {\n constructor(\n name: string,\n variables: {\n // The name of the variable.\n key: string;\n // The value of the variable.\n value: Binding<JsonValue>;\n }[],\n ) {\n super(name);\n }\n}\n\nclass Loop extends Block {\n constructor(\n name: string,\n config: {\n over: Binding<JsonValue[]>;\n variables: {\n // What the variable name for the current item is.\n item: string;\n // What the variable name for the current index is.\n index: string;\n };\n blocks: Block[];\n },\n ) {\n super(name);\n }\n}\n\nclass Parallel extends Block {\n constructor(\n name: string,\n config: {\n over: Binding<JsonValue[]>;\n variables: {\n // What the variable name for the current item is.\n item: string;\n };\n blocks: Block[];\n },\n ) {\n super(name);\n }\n}\n\nclass Throw extends Block {\n constructor(\n name: string,\n config: {\n error: Binding<JsonValue>;\n },\n ) {\n super(name);\n }\n}\n\nclass Return extends Block {\n constructor(\n name: string,\n config: {\n data: Binding<JsonValue>;\n },\n ) {\n super(name);\n }\n}\n\nclass Api {\n constructor(\n name: string,\n steps: Block[],\n authorization:\n | {\n type: "AUTHORIZATION_TYPE_APP_USERS";\n }\n | {\n type: "AUTHORIZATION_TYPE_JS_EXPRESSION";\n expression: Binding<boolean>;\n } = { type: "AUTHORIZATION_TYPE_APP_USERS" },\n ) {\n /* ... */\n }\n public get response(): JsonValue {\n /* ... */\n }\n public get error(): string | undefined {\n /* ... */\n }\n public run(): void {\n /* ... */\n }\n public cancel(): void {\n /* ... */\n }\n}\n````\n\n#### Rules for using Superblocks APIs\n\nThink hard about the following important rules for correctly using Superblocks APIs:\n\n- You MUST use a destructured object to access page scope variables in dynamic block fields. This syntax is NOT defining function parameters - it\'s accessing the inherited page scope.\n\n```ts\n// CORRECT: destructuring to access page scope variables that must already exist\n({ Dropdown1, TextInput1 }) => Dropdown1.selectedOptionsValue + TextInput1.value\n// \u2191 These variables (Dropdown1, TextInput1) must exist in your page scope!\n\n// INCORRECT: trying to use scope object directly\n(state) => state.Dropdown1.selectedOptionsValue + state.TextInput1.value\n// \u2191 This syntax doesn\'t work in Superblocks\n```\n\n- DO NOT reference variables that are not in scope or that don\'t exist. The ONLY things in scope are (1) the outputs of previous blocks that are in lexical scope and (2) page entities.\n\n- The result of each scope is the result of the last block in that scope. In the following example, the value of `sendEmail.response` is the result of the `return_summary` block. Use this information to carefully ensure that the last block in your API is the one that returns the value you want.\n\n```ts\nexport default new Api("sendEmailApi", [\n new Email("send_email", {\n from: "noreply@company.com",\n to: "test@test.com",\n subject: "Test Email",\n body: "This is a test email",\n }),\n new JavaScript("return_summary", {\n fn: () => "Email sent successfully!",\n }),\n]);\n```\n\n- Block outputs are immutable. Do not mutate the output of a block.\n\n- Backend APIs CANNOT mutate frontend state inside of the API\n\n- APIs are registered in scope files using `SbApi()` and then accessed in page components by destructuring from the scope entities. Make sure you name the key used in registerScope the same as the imported API, but do not pass the imported Api into the SbApi() call.\n\n- To access API responses in your UI, use `sbComputed(() => apiName.response)` or `sbComputed(() => apiName.error)`.\n\n- You will not always be told which integrations to use in your API; you will have to determine that yourself based on the data you need to fetch.\n\n- Never add comments to code you (the ai) generate. User added comments are fine - leave those!\n';
36
36
 
37
37
  // ../../../vite-plugin-file-sync/dist/ai-service/prompts/generated/subprompts/superblocks-components-rules.js
38
38
  init_cjs_shims();
package/dist/index.js CHANGED
@@ -292446,7 +292446,7 @@ var templates = {
292446
292446
  "shim-loader.mjs": 'import path from "path";\nimport { pathToFileURL } from "url";\n\nconst overrideMap = {\n "@superblocksteam/library": "./dist/superblocks-library-shim/index.js",\n};\n\nexport async function resolve(specifier, context, nextResolve) {\n if (overrideMap[specifier]) {\n const fullPath = path.resolve(overrideMap[specifier]);\n return {\n shortCircuit: true,\n url: pathToFileURL(fullPath).href,\n };\n }\n\n return nextResolve(specifier, context);\n}\n',
292447
292447
  "src/do-eval-to-sdk.ts": 'import fs from "fs";\nimport path from "path";\nimport { fileURLToPath } from "url";\n\n// finds all JavaScript files in the `dist/apis-to-transform` directory,\n// imports each module, converts its JSON representation to SDK,\n// and outputs a JSON map with filenames as keys and SDK content as values.\n\nasync function main() {\n const __filename = fileURLToPath(import.meta.url);\n const __dirname = path.dirname(__filename);\n\n const apisDir = path.resolve(__dirname, "./to-sdk");\n\n const files = fs.readdirSync(apisDir);\n\n const jsFiles = files.filter(\n (file) => file.endsWith(".js") && file !== "__template__.js",\n );\n\n const results: Record<string, string> = {};\n\n for (const jsFile of jsFiles) {\n const filePath = path.join(apisDir, jsFile);\n const absolutePath = path.resolve(filePath);\n\n try {\n const fileUrl = new URL(`file://${absolutePath}`);\n const module = await import(fileUrl.href);\n\n const defaultExport = module.default;\n\n if (defaultExport && typeof defaultExport.toSDK === "function") {\n const sdkData = await defaultExport.toSDK();\n\n results[jsFile] = sdkData;\n } else {\n console.warn(`${jsFile}: Default export doesn\'t have a toSDK method`);\n }\n } catch (error) {\n console.error(`Error processing ${jsFile}:`, error);\n }\n }\n\n console.log(JSON.stringify(results, null, 2));\n}\n\nmain().catch((error) => {\n console.error("Error:", error);\n process.exit(1);\n});\n',
292448
292448
  "src/do-eval-to-yaml.ts": 'import fs from "fs";\nimport path from "path";\nimport { fileURLToPath } from "url";\nimport yaml from "yaml";\n\n// finds all JavaScript files in the `dist/apis-to-transform` directory,\n// imports each module, converts its JSON representation to YAML,\n// and outputs a JSON map with filenames as keys and YAML content as values.\n\nasync function main() {\n const __filename = fileURLToPath(import.meta.url);\n const __dirname = path.dirname(__filename);\n\n const apisDir = path.resolve(__dirname, "./to-yaml");\n\n const files = fs.readdirSync(apisDir);\n\n const jsFiles = files.filter((file) => file.endsWith(".js"));\n\n const results: Record<string, string> = {};\n\n for (const jsFile of jsFiles) {\n const filePath = path.join(apisDir, jsFile);\n const absolutePath = path.resolve(filePath);\n\n try {\n const fileUrl = new URL(`file://${absolutePath}`);\n const module = await import(fileUrl.href);\n\n const defaultExport = module.default;\n\n if (defaultExport && typeof defaultExport.toJSON === "function") {\n const jsonData = await defaultExport.toJSON();\n\n const yamlContent = yaml.stringify(jsonData);\n\n results[jsFile] = yamlContent;\n } else {\n console.warn(`${jsFile}: Default export doesn\'t have a toJSON method`);\n }\n } catch (error) {\n console.error(`Error processing ${jsFile}:`, error);\n }\n }\n\n console.log(JSON.stringify(results, null, 2));\n}\n\nmain().catch((error) => {\n console.error("Error:", error);\n process.exit(1);\n});\n',
292449
- "src/superblocks-library-shim/index.ts": '/* eslint-disable */\nimport typescript_eslint_parser from "@typescript-eslint/parser";\nimport { CallExpression, ExpressionStatement, FunctionExpression, Literal, Node, parse, Program, UnaryExpression } from "acorn";\nimport { ESLint } from "eslint";\nimport { format } from "prettier";\n\nexport type JsonValue =\n | undefined\n | null\n | number\n | string\n | boolean\n | Array<JsonValue>\n | { [key: string]: JsonValue };\nexport type State = { [key: string]: JsonValue };\nexport type Binding<T> = T | ((state: State) => T);\n\nasync function binding(binding: Binding<string>): Promise<string> {\n if (typeof binding === "function") {\n const { body, node } = await toJS(binding);\n\n if (node?.type === \'Literal\' && typeof (node as Literal).value === \'string\') {\n return `\\`${(node as Literal).value}\\``;\n }\n\n if (node?.type === \'TemplateLiteral\') {\n return body;\n }\n\n return `\\`\\$\\{${body}\\}\\``;\n }\n\n return `\\`${binding}\\``;\n}\n\ninterface Codec {\n toJSON(): Promise<JsonValue>;\n toSDK(entities: string[]): Promise<string>;\n}\n\nexport abstract class Block implements Codec {\n protected name: string;\n\n constructor(name: string) {\n this.name = name;\n }\n\n public abstract toJSON(): Promise<JsonValue>;\n public abstract toSDK(entities: string[]): Promise<string>;\n\n public static fromJSON(json: any, entities: string[]): Block {\n if (json?.step) {\n return Integration.fromJSON(json, entities);\n }\n\n if ("conditional" in json) {\n return Conditional.fromJSON(json, entities);\n }\n\n if ("loop" in json) {\n return Loop.fromJSON(json, entities);\n }\n\n if ("parallel" in json) {\n return Parallel.fromJSON(json, entities);\n }\n\n if ("tryCatch" in json) {\n return TryCatch.fromJSON(json, entities);\n }\n\n if ("variables" in json) {\n return Variables.fromJSON(json, entities);\n }\n\n if ("throw" in json) {\n return Throw.fromJSON(json, entities);\n }\n\n if ("return" in json) {\n return Return.fromJSON(json, entities);\n }\n\n throw new Error("mysterious block");\n }\n}\n\nexport abstract class Integration extends Block {\n protected integration: string;\n\n constructor(name: string, integration: string) {\n super(name);\n\n this.integration = integration;\n }\n\n public abstract toJSON(): Promise<JsonValue>;\n public abstract toSDK(entities: string[]): Promise<string>;\n\n public static fromJSON(json: any, entities: string[]): Block {\n if ("javascript" in json?.step) {\n return JavaScript.fromJSON(json, entities);\n }\n\n if ("python" in json?.step) {\n return Python.fromJSON(json, entities);\n }\n\n if ("postgres" in json?.step) {\n return PostgreSQL.fromJSON(json, entities);\n }\n\n if ("databricks" in json?.step) {\n return Databricks.fromJSON(json, entities);\n }\n\n if ("email" in json?.step) {\n return Email.fromJSON(json, entities);\n }\n\n if ("restapi" in json?.step || "restapiintegration" in json?.step) {\n return RestApi.fromJSON(json, entities);\n }\n\n if ("jira" in json?.step) {\n return Jira.fromJSON(json, entities);\n }\n\n if ("github" in json?.step) {\n return GitHub.fromJSON(json, entities);\n }\n\n throw new Error("mysterious integration");\n }\n}\n\nexport class Python extends Integration {\n private fn: string;\n\n constructor(name: string, config: {\n fn: string;\n }) {\n super(name, "python");\n\n this.fn = config.fn;\n }\n\n public async toJSON(): Promise<JsonValue> {\n return {\n name: this.name,\n step: {\n integration: "python",\n python: {\n body: this.fn,\n },\n },\n };\n }\n\n public async toSDK(entities: string[]): Promise<string> {\n return `new Python("${this.name}", { fn: ${this.fn} })`;\n }\n\n public static fromJSON(json: any, entities: string[]): Python {\n return new Python(json?.name, {\n fn: json?.step?.python?.body,\n });\n }\n}\n\nexport class JavaScript extends Integration {\n private fn: (_: State) => JsonValue;\n\n constructor(\n name: string,\n config: {\n fn: (_: State) => JsonValue;\n },\n ) {\n super(name, "javascript");\n\n this.fn = config.fn;\n }\n\n public async toJSON(): Promise<JsonValue> {\n return {\n name: this.name,\n step: {\n integration: "javascript",\n javascript: {\n body: (await toJSBody(this.fn, { block: false, function: true })).body,\n },\n },\n };\n }\n\n public async toSDK(entities: string[]): Promise<string> {\n return `new JavaScript("${this.name}", { fn: ${signatureV2((await toJSBody(this.fn)).body, entities)} })`;\n }\n\n public static fromJSON(json: any, entities: string[]): JavaScript {\n const args = [json?.step?.javascript?.body]\n const references = referenced(json?.step?.javascript?.body, entities)\n\n if (references.length > 0) {\n args.unshift(`{ ${references.join(", ")} }`)\n }\n\n return new JavaScript(json?.name, {\n fn: new Function(...args) as (_: State) => JsonValue,\n });\n }\n}\n\n// TODO(Frank): There is way too much in common between Snowflake and PostgreSQL. Need to add a parent class for these.\nexport class Snowflake extends Integration {\n private statement: Binding<string>;\n\n constructor(\n name: string,\n integration: string,\n config: {\n statement: Binding<string>;\n },\n ) {\n super(name, integration);\n\n this.statement = config.statement;\n }\n\n public async toJSON(): Promise<JsonValue> {\n return {\n name: this.name,\n step: {\n integration: this.integration,\n snowflake: {\n body: await binding(this.statement),\n usePreparedSql: false,\n },\n },\n };\n }\n\n public async toSDK(entities: string[]): Promise<string> {\n return `new Snowflake("${this.name}", "${this.integration}", { statement: ${typeof this.statement === "function" ? signatureV2((await toJSBody(this.statement)).body, entities) : `"${this.statement}"`} })`;\n }\n\n public static fromJSON(json: any, entities: string[]): Snowflake {\n return new Snowflake(json?.name, json?.step?.integration, {\n statement: fromBinding(\n json?.step?.snowflake?.body as string | undefined,\n entities,\n ),\n });\n }\n}\n\nexport class PostgreSQL extends Integration {\n private statement: Binding<string>;\n\n constructor(\n name: string,\n integration: string,\n config: {\n statement: Binding<string>;\n },\n ) {\n super(name, integration);\n\n this.statement = config.statement;\n }\n\n public async toJSON(): Promise<JsonValue> {\n return {\n name: this.name,\n step: {\n integration: this.integration,\n postgres: {\n body: await binding(this.statement),\n usePreparedSql: false,\n operation: "run_sql",\n },\n },\n };\n }\n\n public async toSDK(entities: string[]): Promise<string> {\n return `new PostgreSQL("${this.name}", "${this.integration}", { statement: ${typeof this.statement === "function" ? signatureV2((await toJSBody(this.statement)).body, entities) : `"${this.statement}"`} })`;\n }\n\n public static fromJSON(json: any, entities: string[]): PostgreSQL {\n return new PostgreSQL(json?.name, json?.step?.integration, {\n statement: fromBinding(\n json?.step?.postgres?.body as string | undefined,\n entities,\n ),\n });\n }\n}\n\nexport class RestApi extends Integration {\n private method: string;\n private url: Binding<string>;\n private headers?: { key: Binding<string>; value: Binding<string> }[];\n private params?: { key: Binding<string>; value: Binding<string> }[];\n private body?: Binding<string>;\n private openapi?: {\n path: string;\n }\n \n protected type: string;\n\n constructor(\n name: string,\n integration: string = "restapi",\n config: {\n method: string;\n url: Binding<string>;\n headers?: { key: Binding<string>; value: Binding<string> }[];\n params?: { key: Binding<string>; value: Binding<string> }[];\n body?: Binding<string>;\n },\n openapi?: {\n path: string;\n }\n ) {\n super(name, integration);\n\n this.method = config.method;\n this.url = config.url;\n this.headers = config.headers;\n this.params = config.params;\n this.body = config.body;\n this.openapi = openapi;\n this.type = this.isIntegration() ? "restapiintegration" : "restapi"\n }\n\n public async toJSON(): Promise<JsonValue> {\n const openApiAction = (): string => {\n if (!this.isIntegration()) {\n return undefined;\n }\n\n if (this.openapi?.path) {\n let path: string = this.openapi.path.trim();\n\n if (path.startsWith("`") && path.endsWith("`")) {\n path = path.slice(1, -1);\n }\n\n return `${this.method} ${path}`;\n }\n\n return \'genericHttpRequest\';\n }\n\n return {\n name: this.name,\n step: {\n integration: this.integration,\n [this.type]: {\n httpMethod: this.method,\n openApiAction: openApiAction(),\n ...(this.url ? { [this.isIntegration() ? "urlPath" : "path"]: await binding(this.url) } : {}),\n ...(this.headers ? { headers: await Promise.all(this.headers.map(async (header) => ({ key: await binding(header.key), value: await binding(header.value) }))) } : {}),\n ...(this.params ? { params: await Promise.all(this.params.map(async (param) => ({ key: await binding(param.key), value: await binding(param.value) }))) } : {}),\n ...(this.body ? { body: await binding(this.body) } : {}),\n\n // Other params that I might need to add.\n responseType: "auto",\n bodyType: "jsonBody",\n },\n },\n };\n }\n\n private isIntegration(): boolean {\n return this.integration !== "restapi";\n }\n\n public async toSDK(entities: string[]): Promise<string> {\n return `new RestApi(${await this.constructorParametersString(entities)})`;\n }\n\n protected async constructorParametersString(entities: string[]): Promise<string> {\n const parts: string[] = [`method: "${this.method}"`];\n\n if (this.url) {\n parts.push(\n `url: ${\n typeof this.url === "function"\n ? signatureV2((await toJSBody(this.url)).body, entities)\n : `"${this.url}"`\n }`,\n );\n }\n\n if (this.headers) {\n const headers = (\n await Promise.all(this.headers.map(async (h) =>\n `{ key: ${\n typeof h.key === "function"\n ? signatureV2((await toJSBody(h.key)).body, entities)\n : `"${h.key}"`\n }, value: ${\n typeof h.value === "function"\n ? signatureV2((await toJSBody(h.value)).body, entities)\n : `"${h.value}"`\n } }`,\n ))\n ).join(", ");\n parts.push(`headers: [${headers}]`);\n }\n\n if (this.params) {\n const params = (\n await Promise.all(this.params.map(async (p) =>\n `{ key: ${\n typeof p.key === "function"\n ? signatureV2((await toJSBody(p.key)).body, entities)\n : `"${p.key}"`\n }, value: ${\n typeof p.value === "function"\n ? signatureV2((await toJSBody(p.value)).body, entities)\n : `"${p.value}"`\n } }`,\n ))\n ).join(", ");\n parts.push(`params: [${params}]`);\n }\n\n if (this.body) {\n parts.push(\n `body: ${\n typeof this.body === "function"\n ? signatureV2((await toJSBody(this.body)).body, entities)\n : JSON.stringify(this.body)\n }`,\n );\n }\n\n return `"${this.name}", "${this.integration}", { ${parts.join(", ")} }, ${JSON.stringify(this.openapi)}`\n }\n\n public static fromJSON(json: any, entities: string[]): RestApi {\n const config = json?.step?.[json?.step?.integration === "restapi" ? "restapi" : "restapiintegration"]\n\n if (json?.step?.integration === "restapiintegration") {\n return new RestApi(json?.name, json?.step?.integration, OpenApi.configFromJSON(config, entities), OpenApi.openapiFromJSON(config))\n }\n\n return new RestApi(json?.name, json?.step?.integration, {\n method: config?.httpMethod,\n url: fromBinding(config?.path, entities),\n headers: config?.headers?.map((header: any) => ({ key: fromBinding(header.key, entities), value: fromBinding(header.value, entities) })),\n params: config?.params?.map((param: any) => ({ key: fromBinding(param.key, entities), value: fromBinding(param.value, entities) })),\n body: fromBinding(config?.body, entities),\n }, OpenApi.openapiFromJSON(config));\n }\n}\n\nexport class OpenApi extends RestApi {\n constructor(\n name: string,\n integration: string,\n config: {\n method: string;\n url: Binding<string>;\n headers?: { key: Binding<string>; value: Binding<string> }[];\n params?: { key: Binding<string>; value: Binding<string> }[];\n body?: Binding<string>;\n },\n openapi?: {\n path: string;\n }\n ) {\n super(name, integration, config, openapi);\n }\n\n public static configFromJSON(json: any, entities: string[]): {\n method: string;\n url: Binding<string>;\n headers?: { key: Binding<string>; value: Binding<string> }[];\n params?: { key: Binding<string>; value: Binding<string> }[];\n body?: Binding<string>;\n } {\n return {\n method: json?.httpMethod,\n url: fromBinding(json?.urlPath, entities),\n headers: json?.headers?.map((header: any) => ({ key: fromBinding(header.key, entities), value: fromBinding(header.value, entities) })),\n params: json?.params?.map((param: any) => ({ key: fromBinding(param.key, entities), value: fromBinding(param.value, entities) })),\n body: fromBinding(json?.body, entities),\n }\n }\n\n public static openapiFromJSON(json: any): {\n path: string;\n } {\n return json?.openApiAction && json.openApiAction !== "" && json.openApiAction !== "genericHttpRequest" ? { path: json.openApiAction.split(" ")[1] } : undefined\n }\n}\n\nexport class Jira extends OpenApi {\n constructor(\n name: string,\n integration: string,\n config: {\n method: string;\n url: Binding<string>;\n headers?: { key: Binding<string>; value: Binding<string> }[];\n params?: { key: Binding<string>; value: Binding<string> }[];\n body?: Binding<string>;\n },\n openapi?: {\n path: string;\n }\n ) {\n super(name, integration, config, openapi);\n this.type = "jira"\n }\n\n public async toSDK(entities: string[]): Promise<string> {\n return `new Jira(${await this.constructorParametersString(entities)})`;\n }\n\n public static fromJSON(json: any, entities: string[]): Jira {\n return new Jira(json?.name, json?.step?.integration, OpenApi.configFromJSON(json?.step?.jira, entities), OpenApi.openapiFromJSON(json?.step?.jira))\n }\n}\n\nexport class GitHub extends OpenApi {\n constructor(\n name: string,\n integration: string,\n config: {\n method: string;\n url: Binding<string>;\n headers?: { key: Binding<string>; value: Binding<string> }[];\n params?: { key: Binding<string>; value: Binding<string> }[];\n body?: Binding<string>;\n },\n openapi?: {\n path: string;\n }\n ) {\n super(name, integration, config, openapi);\n this.type = "github"\n }\n\n public async toSDK(entities: string[]): Promise<string> {\n return `new GitHub(${await this.constructorParametersString(entities)})`;\n }\n\n public static fromJSON(json: any, entities: string[]): GitHub {\n return new GitHub(json?.name, json?.step?.integration, OpenApi.configFromJSON(json?.step?.github, entities), OpenApi.openapiFromJSON(json?.step?.github))\n }\n}\n\nexport class Email extends Integration {\n private subject: Binding<string>;\n private from: Binding<string>; // comma separated list\n private to: Binding<string>; // comma separated list\n private cc?: Binding<string>; // comma separated list\n private bcc?: Binding<string>; // comma separated list\n private body: Binding<string>;\n\n constructor(\n name: string,\n config: {\n from: Binding<string>;\n to: Binding<string>;\n subject: Binding<string>;\n cc?: Binding<string>;\n bcc?: Binding<string>;\n body?: Binding<string>;\n },\n ) {\n super(name, "email");\n\n this.from = config.from;\n this.to = config.to;\n this.subject = config.subject;\n this.cc = config.cc;\n this.bcc = config.bcc;\n this.body = config.body;\n }\n\n public async toJSON(): Promise<JsonValue> {\n return {\n name: this.name,\n step: {\n integration: "email",\n email: {\n ...(this.from ? { emailFrom: await binding(this.from) } : {}),\n ...(this.to ? { emailTo: await binding(this.to) } : {}),\n ...(this.cc ? { emailCc: await binding(this.cc) } : {}),\n ...(this.bcc ? { emailBcc: await binding(this.bcc) } : {}),\n ...(this.subject ? { emailSubject: await binding(this.subject) } : {}),\n ...(this.body ? { emailBody: await binding(this.body) } : {}),\n },\n },\n };\n }\n\n public async toSDK(entities: string[]): Promise<string> {\n const parts: string[] = [];\n\n if (this.from) {\n parts.push(\n `from: ${\n typeof this.from === "function"\n ? signatureV2((await toJSBody(this.from)).body, entities)\n : `"${this.from}"`\n }`,\n );\n }\n\n if (this.to) {\n parts.push(\n `to: ${\n typeof this.to === "function"\n ? signatureV2((await toJSBody(this.to)).body, entities)\n : `"${this.to}"`\n }`,\n );\n }\n\n if (this.cc) {\n parts.push(\n `cc: ${\n typeof this.cc === "function"\n ? signatureV2((await toJSBody(this.cc)).body, entities)\n : `"${this.cc}"`\n }`,\n );\n }\n\n if (this.bcc) {\n parts.push(\n `bcc: ${\n typeof this.bcc === "function"\n ? signatureV2((await toJSBody(this.bcc)).body, entities)\n : `"${this.bcc}"`\n }`,\n );\n }\n\n if (this.subject) {\n parts.push(\n `subject: ${\n typeof this.subject === "function"\n ? signatureV2((await toJSBody(this.subject)).body, entities)\n : `"${this.subject}"`\n }`,\n );\n }\n\n if (this.body) {\n parts.push(\n `body: ${\n typeof this.body === "function"\n ? signatureV2((await toJSBody(this.body)).body, entities)\n : `"${this.body}"`\n }`,\n );\n }\n\n return `new Email("${this.name}", { ${parts.join(", ")} })`;\n }\n\n public static fromJSON(json: any, entities: string[]): Email {\n return new Email(json?.name, {\n from: fromBinding(json?.step?.email?.emailFrom, entities),\n to: fromBinding(json?.step?.email?.emailTo, entities),\n subject: fromBinding(json?.step?.email?.emailSubject, entities),\n body: fromBinding(json?.step?.email?.emailBody, entities),\n cc: json?.step?.email?.emailCc ? fromBinding(json?.step?.email?.emailCc, entities) : undefined,\n bcc: json?.step?.email?.emailBcc ? fromBinding(json?.step?.email?.emailBcc, entities) : undefined,\n });\n }\n}\n\nexport class Databricks extends Integration {\n private statement: Binding<string>;\n\n constructor(\n name: string,\n integration: string,\n config: {\n statement: Binding<string>;\n },\n ) {\n super(name, integration);\n\n this.statement = config.statement;\n }\n\n public async toJSON(): Promise<JsonValue> {\n return {\n name: this.name,\n step: {\n integration: this.integration,\n databricks: {\n runSql: {\n sqlBody: await binding(this.statement),\n useParameterized: false,\n },\n },\n },\n };\n }\n\n public async toSDK(entities: string[]): Promise<string> {\n return `new Databricks("${this.name}", "${this.integration}", { statement: ${typeof this.statement === "function" ? signatureV2((await toJSBody(this.statement)).body, entities) : `"${this.statement}"`} })`;\n }\n\n public static fromJSON(json: any, entities: string[]): Databricks {\n return new Databricks(json?.name, json?.step?.integration, {\n statement: fromBinding(\n json?.step?.databricks?.runSql?.sqlBody as string,\n entities,\n ),\n });\n }\n}\n\nexport type Condition = {\n when: Binding<boolean>;\n then: Block[];\n};\n\nasync function conditionToSDK(condition: Condition, entities: string[]): Promise<string> {\n return `{ when: ${typeof condition.when === "function" ? signatureV2((await toJSBody(condition.when)).body, entities) : condition.when}, then: [${(await Promise.all(condition.then.map(async (block) => await block.toSDK(entities)))).join(",")}] }`\n}\n\nexport type Conditions = {\n if: Condition;\n elif?: Condition[];\n else?: Block[];\n};\n\nexport class Conditional extends Block {\n public conditions: Conditions;\n\n constructor(name: string, config: Conditions) {\n super(name);\n\n this.conditions = config;\n }\n\n public async toJSON(): Promise<JsonValue> {\n return {\n name: this.name,\n conditional: {\n if: {\n condition: ensureIIFE((await toJS<boolean>(this.conditions.if.when)).body),\n blocks: await Promise.all(this.conditions.if.then.map(async (block) => await block.toJSON())),\n },\n ...await (async () =>\n this.conditions.elif\n ? {\n elseIf: await Promise.all(this.conditions.elif.map(async (condition) => ({\n condition: ensureIIFE((await toJS<boolean>(condition.when)).body),\n blocks: await Promise.all(condition.then.map(async (block) => await block.toJSON())),\n }))),\n }\n : { elseIf: [] })(),\n else: this.conditions.else ? {\n blocks: await Promise.all(this.conditions.else.map(async (block) => await block.toJSON()))\n } : undefined,\n },\n };\n }\n\n public static fromJSON(json: any, entities: string[]): Conditional {\n return new Conditional(json?.name, {\n if: {\n when: fromJS<boolean>(json?.conditional?.if?.condition, entities),\n then: json?.conditional?.if?.blocks.map((block: any) =>\n Block.fromJSON(block, entities),\n ),\n },\n elif: json?.conditional?.elseIf ? json?.conditional?.elseIf?.map((condition: any) => ({\n when: fromJS<boolean>(condition.condition, entities),\n then: condition.blocks.map((block: any) => Block.fromJSON(block, entities)),\n })) : [],\n else: json?.conditional?.else ? json?.conditional?.else?.blocks?.map((block: any) => Block.fromJSON(block, entities)) : [],\n });\n }\n\n public async toSDK(entities: string[]): Promise<string> {\n return `new Conditional("${this.name}", { if: ${await conditionToSDK(this.conditions.if, entities)}, elif: [${this.conditions.elif ? (await Promise.all(this.conditions.elif?.map(async (condition) => await conditionToSDK(condition, entities)))).join(",") : \'\'}], else: ${this.conditions.else ? (await Promise.all(this.conditions.else?.map(async (block) => await block.toSDK(entities)))).join(",") : "undefined"} })`;\n }\n}\n\nexport class Parallel extends Block {\n private over: Binding<JsonValue[]>;\n private blocks: Block[];\n private variables: { item: string };\n\n constructor(\n name: string,\n config: {\n over: Binding<JsonValue[]>;\n variables: { item: string };\n blocks: Block[];\n },\n ) {\n super(name);\n\n this.over = config.over;\n this.blocks = config.blocks;\n this.variables = config.variables;\n }\n\n public async toJSON(): Promise<JsonValue> {\n return {\n name: this.name,\n parallel: {\n dynamic: {\n blocks: await Promise.all(this.blocks?.map(async (block) => await block.toJSON())),\n paths: ensureIIFE((await toJS<JsonValue[]>(this.over)).body),\n variables: this.variables,\n },\n wait: "WAIT_ALL",\n },\n };\n }\n\n public async toSDK(entities: string[]): Promise<string> {\n return `new Parallel("${this.name}", { over: ${typeof this.over === "function" ? signatureV2((await toJSBody(this.over)).body, entities) : this.over}, variables: ${this.variables}, blocks: ${await Promise.all(this.blocks.map(async (block) => await block.toSDK(entities)).join(","))} })`;\n }\n\n public static fromJSON(json: any, entities: string[]): Parallel {\n return new Parallel(json?.name, {\n over: fromJS<JsonValue[]>(json?.parallel?.dynamic?.paths, entities),\n variables: {\n item: json?.parallel?.dynamic?.variables?.item,\n },\n blocks: json?.parallel?.dynamic?.blocks.map((block: any) =>\n Block.fromJSON(block, entities),\n ),\n });\n }\n}\n\nexport class Loop extends Block {\n private over: Binding<JsonValue[]>; // NOTE(Frank): Only for each loops are supported at the moment.\n private blocks: Block[];\n private variables: { item: string; index: string };\n\n constructor(\n name: string,\n config: {\n over: Binding<JsonValue[]>;\n variables: { item: string; index: string };\n blocks: Block[];\n },\n ) {\n super(name);\n\n this.over = config.over;\n this.variables = config.variables;\n this.blocks = config.blocks;\n }\n\n public async toJSON(): Promise<JsonValue> {\n return {\n name: this.name,\n loop: {\n range: ensureIIFE((await toJS<JsonValue[]>(this.over)).body),\n type: "TYPE_FOREACH",\n variables: this.variables,\n blocks: await Promise.all(this.blocks.map(async (block) => await block.toJSON())),\n },\n };\n }\n\n public async toSDK(entities: string[]): Promise<string> {\n return `new Loop("${this.name}", { over: ${typeof this.over === "function" ? signatureV2((await toJSBody(this.over)).body, entities) : this.over}, variables: ${this.variables}, blocks: ${await Promise.all(this.blocks.map(async (block) => await block.toSDK(entities)).join(","))} })`;\n }\n\n public static fromJSON(json: any, entities: string[]): Loop {\n return new Loop(json?.name, {\n over: fromJS<JsonValue[]>(json?.loop?.range, entities),\n variables: {\n item: json?.loop?.variables?.item,\n index: json?.loop?.variables?.index,\n },\n blocks: json?.loop?.blocks.map((block: any) =>\n Block.fromJSON(block, entities),\n ),\n });\n }\n}\n\nexport class Variables extends Block {\n private variables: { key: string, value: Binding<JsonValue> }[];\n\n constructor(\n name: string,\n variables: { key: string, value: Binding<JsonValue> }[],\n ) {\n super(name);\n\n this.variables = variables;\n }\n\n public async toJSON(): Promise<JsonValue> {\n return {\n name: this.name,\n variables: {\n items: await Promise.all(this.variables.map(async (variable) => {\n return {\n key: variable.key,\n value: (await toJS<JsonValue>(variable.value)).body,\n type: "TYPE_SIMPLE",\n mode: "MODE_READWRITE",\n };\n }))\n },\n };\n }\n \n public async toSDK(entities: string[]): Promise<string> {\n return `new Variables("${this.name}", [${\n (await Promise.all(\n this.variables?.map(async (variable) => (\n `{ key: "${variable.key}", value: ${typeof variable.value === "function" ? signatureV2((await toJSBody(variable.value)).body, entities) : variable.value} }`\n ))\n )).join(",")}])`;\n }\n\n public static fromJSON(json: any, entities: string[]): Variables {\n return new Variables(json?.name, json?.variables?.items?.map((variable: any) => ({\n key: variable.key,\n value: fromBinding(variable.value, entities),\n })));\n }\n}\n\nexport class TryCatch extends Block {\n private try: Block[];\n private catch: Block[];\n private finally?: Block[];\n private variables: { error: string };\n\n constructor(\n name: string,\n config: {\n try: Block[];\n catch: Block[];\n finally?: Block[];\n variables: { error: string };\n },\n ) {\n super(name);\n\n this.try = config.try;\n this.catch = config.catch;\n this.finally = config.finally;\n this.variables = config.variables;\n }\n\n public async toJSON(): Promise<JsonValue> {\n return {\n name: this.name,\n tryCatch: {\n try: { blocks: await Promise.all(this.try.map(async (block) => await block.toJSON())) },\n catch: { blocks: await Promise.all(this.catch.map(async (block) => await block.toJSON())) },\n finally: { blocks: this.finally ? await Promise.all(this.finally.map(async (block) => await block.toJSON())) : [] },\n variables: this.variables,\n },\n };\n }\n\n public async toSDK(entities: string[]): Promise<string> {\n return `new TryCatch("${this.name}", { try: ${await Promise.all(this.try.map(async (block) => await block.toSDK(entities)).join(","))}, catch: ${await Promise.all(this.catch.map(async (block) => await block.toSDK(entities)).join(","))}, finally: ${this.finally?.map(async (block) => await block.toSDK(entities)).join(", ")} })`;\n }\n\n public static fromJSON(json: any, entities: string[]): TryCatch {\n return new TryCatch(json?.name, {\n try: json?.tryCatch?.try.blocks.map((block: any) => Block.fromJSON(block, entities)),\n catch: json?.tryCatch?.catch.blocks.map((block: any) => Block.fromJSON(block, entities)),\n finally: json?.tryCatch?.finally?.blocks.map((block: any) => Block.fromJSON(block, entities)),\n variables: {\n error: json?.tryCatch?.variables?.error,\n },\n });\n }\n}\n\nexport class Throw extends Block {\n private error: Binding<JsonValue>;\n\n constructor(\n name: string,\n config: {\n error: Binding<JsonValue>;\n },\n ) {\n super(name);\n\n this.error = config.error;\n }\n\n public async toJSON(): Promise<JsonValue> {\n return {\n name: this.name,\n throw: {\n error: (await toJS<JsonValue>(this.error)).body,\n },\n };\n }\n\n public async toSDK(entities: string[]): Promise<string> {\n return `new Throw("${this.name}", { error: ${typeof this.error === "function" ? signatureV2((await toJSBody(this.error)).body, entities) : JSON.stringify(this.error)} })`;\n }\n\n public static fromJSON(json: any, entities: string[]): Throw {\n return new Throw(json?.name, {\n error: fromJS(json?.throw?.error, entities),\n });\n }\n}\n\nexport class Return extends Block {\n private data: Binding<JsonValue>;\n\n constructor(\n name: string,\n config: {\n data: Binding<JsonValue>;\n },\n ) {\n super(name);\n\n this.data = config.data;\n }\n\n public async toJSON(): Promise<JsonValue> {\n return {\n name: this.name,\n return: {\n data: (await toJS<JsonValue>(this.data)).body,\n },\n };\n }\n\n public async toSDK(entities: string[]): Promise<string> {\n return `new Return("${this.name}", { data: ${typeof this.data === "function" ? signatureV2((await toJSBody(this.data)).body, entities) : JSON.stringify(this.data)} })`;\n }\n\n public static fromJSON(json: any, entities: string[]): Return {\n return new Return(json?.name, {\n data: fromJS(json?.return?.data, entities),\n });\n }\n}\n\nexport class Api {\n private blocks: Block[];\n private name: string;\n\n constructor(name: string, blocks: Block[]) {\n this.name = name;\n this.blocks = blocks;\n }\n\n public async toSDK(entities: string[]): Promise<string> {\n const imports =\n "import { Api, JavaScript, Python, PostgreSQL, Snowflake, RestApi, Databricks, Conditional, Parallel, Loop, TryCatch, Variables, Throw, Return } from \'@superblocksteam/library\'";\n return `${imports}\\n\\nexport default new Api("${this.name}", [${(await Promise.all(this.blocks.map(async (block) => await block.toSDK(entities)))).join(",")}])`;\n }\n\n public static fromJSON(json: any, entities: string[]): Api {\n // NOTE(Frank): Would probs unmarshal this into the protobuf types and then do the conversion.\n return new Api(\n json?.metadata?.name as string,\n (json.blocks as any[]).map((block) => Block.fromJSON(block, entities)),\n );\n }\n\n public async toJSON(): Promise<JsonValue> {\n const api: {\n metadata: {\n name: string;\n timestamps: {\n updated: string;\n };\n };\n trigger: {\n application: {};\n };\n blocks: JsonValue[];\n } = {\n metadata: {\n name: this.name,\n timestamps: {\n updated: new Date().toISOString(),\n },\n },\n trigger: {\n application: {},\n },\n blocks: [],\n };\n\n api.blocks = await Promise.all(this.blocks.map(async (block) => await block.toJSON()));\n\n return api;\n }\n}\n\nfunction referenced(data: string, entities: string[]): string[] {\n return entities.reduce((acc: string[], entity: string) => {\n if (data.includes(entity)) {\n acc.push(entity);\n }\n return acc;\n }, []);\n}\n\nexport function signatureV2(data: string, entities: string[] = []): string {\n return `({ ${referenced(data, entities).join(", ")} }) => ${data}`;\n}\n\n/**\n * Converts a string binding, which comprise 99% of dynamic integration fields, into a function returning an interpolated string.\n *\n * fromBinding(`https://${ \'goo\' + \'gle\' }.com/${ Dropdown1.value }`, [\'Dropdown1\']) -> ({ Dropdown1 }) => `https://${\'goo\' + \'gle\'}.com/${Dropdown1.value}`\n *\n * @param value - The value to convert.\n * @param entities - The master list of possible entities that could be referenced in the binding.\n *\n * @returns The converted value.\n */\nexport function fromBinding(value: string, entities: string[]): Binding<string> {\n if (!value) {\n return "";\n }\n\n if (!value.startsWith("`") || !value.endsWith("`")) {\n return value;\n }\n\n const args: string[] = [`return ${value}`]\n const references: string[] = referenced(value, entities);\n\n if (references.length === 0) {\n try {\n return eval(value) as string;\n } catch (e) {\n console.error(`We couldn\'t tranform the binding ${value} to the SDK. Ensure the AI hasn\'t hallucinated a variable.`);\n throw e;\n }\n }\n\n // only add the arguments if we have references\n args.unshift(`{ ${references} }`);\n\n return new Function(...args) as (_: State) => string;\n}\n\nasync function beautifyAndOptionallyCondense<T extends JsonValue>(value: ((state: State) => T), node: Node, options: { condense?: boolean, block?: boolean } = { condense: true, block: true }) {\n let start: number = node.start;\n let end: number = node.end;\n\n if (!options.block) {\n start += 1;\n end -= 1;\n }\n \n const beautified = await beautify(value.toString().slice(start, end))\n\n if (options.condense) {\n return beautified.replace(/\\s*\\n\\s*/g, \' \').replace(/\\s+/g, \' \').trim();\n }\n\n return beautified\n}\n\nfunction createAst(value: string): Program {\n return parse(value, {\n ecmaVersion: "latest",\n sourceType: "script",\n preserveParens: false,\n });\n}\n\nexport async function toJSBody<T extends JsonValue>(value: ((state: State) => T), options: { block?: boolean, condense?: boolean, function?: boolean } = { block: true, condense: true, function: false }): Promise<{body: string, iife: boolean, node?: Node}> {\n if (typeof value !== "function") {\n return {\n body: `\\`${JSON.stringify(value)}\\``,\n iife: true,\n }\n }\n\n // parse the code\n const ast = createAst(value.toString());\n const program = ast.body[0];\n\n switch (program.type) {\n case "ExpressionStatement":\n const body = ((program as ExpressionStatement).expression as FunctionExpression).body;\n\n if (body.type === \'BlockStatement\') {\n return {\n body: await beautifyAndOptionallyCondense(value, body, options),\n iife: true,\n node: body,\n }\n }\n\n return {\n body: `${options.function ? "return " : ""}${value.toString().slice(body.start, body.end)}`,\n iife: options.function,\n node: body,\n }\n case "FunctionDeclaration":\n let contents = value.toString().slice(program.body.start, program.body.end)\n\n if (program.body.type !== \'BlockStatement\') {\n return {\n body: contents,\n iife: false,\n node: program.body,\n }\n }\n \n if (!options.function && program.body.body.length === 1 && program.body.body[0].type === \'ReturnStatement\') {\n return {\n body: value.toString().slice(program.body.body[0].argument.start, program.body.body[0].argument.end),\n iife: false, // This should be false but we need to deal with the nested string template case.\n node: program.body.body[0].argument,\n }\n }\n\n return {\n body: await beautifyAndOptionallyCondense(value, program.body, options),\n iife: true,\n node: program.body,\n }\n default:\n throw new Error("you found a case we haven\'t handled yet")\n }\n}\n\nexport async function toJS<T extends JsonValue>(value: Binding<T>): Promise<{body: string, node?: Node}> {\n if (typeof value !== "function") {\n return {\n body: JSON.stringify(value),\n }\n }\n\n const { body, iife, node } = await toJSBody(value);\n\n if (iife) {\n return {\n body: `(() => ${body})()`,\n node,\n }\n }\n\n return { body, node }\n}\n\nexport function fromJS<T extends JsonValue>(\n value: string,\n entities: string[] = [],\n): Binding<T> {\n // determine if there are entities referenced in the value\n const refs = referenced(value, entities).join(", ")\n\n // remove any surrounding whitespace\n value = value.trim()\n\n let unaryOps: string[] = [];\n let call = (createAst(value).body[0] as ExpressionStatement).expression;\n\n while (call?.type === \'UnaryExpression\') {\n unaryOps.push((call as UnaryExpression).operator);\n call = (call as unknown as UnaryExpression).argument as CallExpression;\n }\n \n const fn = (call as CallExpression).callee;\n let body = value;\n let prefix = "return ";\n\n // handle iife\n if (fn?.type === "ArrowFunctionExpression") {\n // i\'m not happy with this but I don\'t see another immediate way to handle this.\n // this will result in a correct result but one that isn\'t idemopotent.\n if (unaryOps.length > 0) {\n return new Function(`{ ${refs} }`, `${prefix}${body}`) as (_: State) => T;\n }\n\n\n body = value.slice(fn.body.start, fn.body.end)\n\n if (fn.body.type === \'BlockStatement\') {\n // drop the opening "{" and closing "}"\n body = body.slice(1, -1)\n prefix = ""\n } \n } else if (refs.length === 0) {\n // we\'re not an iife nor do we have references.\n // we might have to revisit this implementation since this technically reduces to a static value down to it\'s most minimal form.\n return eval(value) as T\n }\n\n // we\'re either an iife or a static value with refs.\n return new Function(`{ ${refs} }`, `${prefix}${body}`) as (_: State) => T;\n}\n\nexport async function beautify(code: string): Promise<string> {\n const eslint = new ESLint({\n fix: true,\n overrideConfigFile: true,\n baseConfig: {\n languageOptions: {\n parser: typescript_eslint_parser,\n parserOptions: {\n ecmaVersion: 2020,\n sourceType: "module",\n },\n },\n rules: {\n "arrow-body-style": "error",\n },\n },\n });\n\n code = (await eslint.lintText(code))[0]?.output || code;\n\n return await format(code, {\n parser: "typescript",\n tabWidth: 2,\n // singleQuote: true,\n trailingComma: "none",\n semi: true,\n bracketSpacing: true,\n arrowParens: "always",\n bracketSameLine: false,\n embeddedLanguageFormatting: "auto",\n quoteProps: "as-needed",\n insertPragma: false,\n requirePragma: false,\n useTabs: false,\n endOfLine: "auto",\n arrowFunctionParentheses: "always",\n // plugins: ["prettier-plugin-organize-imports"],\n });\n}\n\nexport function ensureIIFE(value: string): string {\n // We write the IIFE so we can be loose with the fomratting here. \n // For example, we\'d never get ( ( ) =>)().\n if (value.startsWith("(() => ") && value.endsWith(")()")) {\n return value\n }\n\n return `(() => ${value})()`\n}',
292449
+ "src/superblocks-library-shim/index.ts": '/* eslint-disable */\nimport typescript_eslint_parser from "@typescript-eslint/parser";\nimport { CallExpression, ExpressionStatement, FunctionExpression, Literal, Node, parse, Program, UnaryExpression } from "acorn";\nimport { ESLint } from "eslint";\nimport { format } from "prettier";\n\nexport type JsonValue =\n | undefined\n | null\n | number\n | string\n | boolean\n | Array<JsonValue>\n | { [key: string]: JsonValue };\nexport type State = { [key: string]: JsonValue };\nexport type Binding<T> = T | ((state: State) => T);\n\nasync function binding(binding: Binding<string>): Promise<string> {\n if (typeof binding === "function") {\n const { body, node } = await toJS(binding);\n\n if (node?.type === \'Literal\' && typeof (node as Literal).value === \'string\') {\n return `\\`${(node as Literal).value}\\``;\n }\n\n if (node?.type === \'TemplateLiteral\') {\n return body;\n }\n\n return `\\`\\$\\{${body}\\}\\``;\n }\n\n return `\\`${binding}\\``;\n}\n\ninterface Codec {\n toJSON(): Promise<JsonValue>;\n toSDK(entities: string[]): Promise<string>;\n}\n\nexport abstract class Block implements Codec {\n protected name: string;\n\n constructor(name: string) {\n this.name = name;\n }\n\n public abstract toJSON(): Promise<JsonValue>;\n public abstract toSDK(entities: string[]): Promise<string>;\n\n public static fromJSON(json: any, entities: string[]): Block {\n if (json?.step) {\n return Integration.fromJSON(json, entities);\n }\n\n if ("conditional" in json) {\n return Conditional.fromJSON(json, entities);\n }\n\n if ("loop" in json) {\n return Loop.fromJSON(json, entities);\n }\n\n if ("parallel" in json) {\n return Parallel.fromJSON(json, entities);\n }\n\n if ("tryCatch" in json) {\n return TryCatch.fromJSON(json, entities);\n }\n\n if ("variables" in json) {\n return Variables.fromJSON(json, entities);\n }\n\n if ("throw" in json) {\n return Throw.fromJSON(json, entities);\n }\n\n if ("return" in json) {\n return Return.fromJSON(json, entities);\n }\n\n throw new Error("mysterious block");\n }\n}\n\nexport abstract class Integration extends Block {\n protected integration: string;\n\n constructor(name: string, integration: string) {\n super(name);\n\n this.integration = integration;\n }\n\n public abstract toJSON(): Promise<JsonValue>;\n public abstract toSDK(entities: string[]): Promise<string>;\n\n public static fromJSON(json: any, entities: string[]): Block {\n if ("javascript" in json?.step) {\n return JavaScript.fromJSON(json, entities);\n }\n\n if ("python" in json?.step) {\n return Python.fromJSON(json, entities);\n }\n\n if ("postgres" in json?.step) {\n return PostgreSQL.fromJSON(json, entities);\n }\n\n if ("databricks" in json?.step) {\n return Databricks.fromJSON(json, entities);\n }\n\n if ("email" in json?.step) {\n return Email.fromJSON(json, entities);\n }\n\n if ("restapi" in json?.step || "restapiintegration" in json?.step) {\n return RestApi.fromJSON(json, entities);\n }\n\n if ("jira" in json?.step) {\n return Jira.fromJSON(json, entities);\n }\n\n if ("github" in json?.step) {\n return GitHub.fromJSON(json, entities);\n }\n\n throw new Error("mysterious integration");\n }\n}\n\nexport class Python extends Integration {\n private fn: string;\n\n constructor(name: string, config: {\n fn: string;\n }) {\n super(name, "python");\n\n this.fn = config.fn;\n }\n\n public async toJSON(): Promise<JsonValue> {\n return {\n name: this.name,\n step: {\n integration: "python",\n python: {\n body: this.fn,\n },\n },\n };\n }\n\n public async toSDK(entities: string[]): Promise<string> {\n return `new Python("${this.name}", { fn: ${this.fn} })`;\n }\n\n public static fromJSON(json: any, entities: string[]): Python {\n return new Python(json?.name, {\n fn: json?.step?.python?.body,\n });\n }\n}\n\nexport class JavaScript extends Integration {\n private fn: (_: State) => JsonValue;\n\n constructor(\n name: string,\n config: {\n fn: (_: State) => JsonValue;\n },\n ) {\n super(name, "javascript");\n\n this.fn = config.fn;\n }\n\n public async toJSON(): Promise<JsonValue> {\n return {\n name: this.name,\n step: {\n integration: "javascript",\n javascript: {\n body: (await toJSBody(this.fn, { block: false, function: true })).body,\n },\n },\n };\n }\n\n public async toSDK(entities: string[]): Promise<string> {\n return `new JavaScript("${this.name}", { fn: ${signatureV2((await toJSBody(this.fn)).body, entities)} })`;\n }\n\n public static fromJSON(json: any, entities: string[]): JavaScript {\n const args = [json?.step?.javascript?.body]\n const references = referenced(json?.step?.javascript?.body, entities)\n\n if (references.length > 0) {\n args.unshift(`{ ${references.join(", ")} }`)\n }\n\n return new JavaScript(json?.name, {\n fn: new Function(...args) as (_: State) => JsonValue,\n });\n }\n}\n\n// TODO(Frank): There is way too much in common between Snowflake and PostgreSQL. Need to add a parent class for these.\nexport class Snowflake extends Integration {\n private statement: Binding<string>;\n\n constructor(\n name: string,\n integration: string,\n config: {\n statement: Binding<string>;\n },\n ) {\n super(name, integration);\n\n this.statement = config.statement;\n }\n\n public async toJSON(): Promise<JsonValue> {\n return {\n name: this.name,\n step: {\n integration: this.integration,\n snowflake: {\n body: await binding(this.statement),\n usePreparedSql: false,\n },\n },\n };\n }\n\n public async toSDK(entities: string[]): Promise<string> {\n return `new Snowflake("${this.name}", "${this.integration}", { statement: ${typeof this.statement === "function" ? signatureV2((await toJSBody(this.statement)).body, entities) : `"${this.statement}"`} })`;\n }\n\n public static fromJSON(json: any, entities: string[]): Snowflake {\n return new Snowflake(json?.name, json?.step?.integration, {\n statement: fromBinding(\n json?.step?.snowflake?.body as string | undefined,\n entities,\n ),\n });\n }\n}\n\nexport class PostgreSQL extends Integration {\n private statement: Binding<string>;\n\n constructor(\n name: string,\n integration: string,\n config: {\n statement: Binding<string>;\n },\n ) {\n super(name, integration);\n\n this.statement = config.statement;\n }\n\n public async toJSON(): Promise<JsonValue> {\n return {\n name: this.name,\n step: {\n integration: this.integration,\n postgres: {\n body: await binding(this.statement),\n usePreparedSql: false,\n operation: "run_sql",\n },\n },\n };\n }\n\n public async toSDK(entities: string[]): Promise<string> {\n return `new PostgreSQL("${this.name}", "${this.integration}", { statement: ${typeof this.statement === "function" ? signatureV2((await toJSBody(this.statement)).body, entities) : `"${this.statement}"`} })`;\n }\n\n public static fromJSON(json: any, entities: string[]): PostgreSQL {\n return new PostgreSQL(json?.name, json?.step?.integration, {\n statement: fromBinding(\n json?.step?.postgres?.body as string | undefined,\n entities,\n ),\n });\n }\n}\n\nexport class RestApi extends Integration {\n private method: string;\n private url: Binding<string>;\n private headers?: { key: Binding<string>; value: Binding<string> }[];\n private params?: { key: Binding<string>; value: Binding<string> }[];\n private body?: Binding<string>;\n private openapi?: {\n path: string;\n }\n \n protected type: string;\n\n constructor(\n name: string,\n integration: string = "restapi",\n config: {\n method: string;\n url: Binding<string>;\n headers?: { key: Binding<string>; value: Binding<string> }[];\n params?: { key: Binding<string>; value: Binding<string> }[];\n body?: Binding<string>;\n },\n openapi?: {\n path: string;\n }\n ) {\n super(name, integration);\n\n this.method = config.method;\n this.url = config.url;\n this.headers = config.headers;\n this.params = config.params;\n this.body = config.body;\n this.openapi = openapi;\n this.type = this.isIntegration() ? "restapiintegration" : "restapi"\n }\n\n public async toJSON(): Promise<JsonValue> {\n const openApiAction = (): string => {\n if (!this.isIntegration()) {\n return undefined;\n }\n\n if (this.openapi?.path) {\n let path: string = this.openapi.path.trim();\n\n if (path.startsWith("`") && path.endsWith("`")) {\n path = path.slice(1, -1);\n }\n\n return `${this.method} ${path}`;\n }\n\n return \'genericHttpRequest\';\n }\n\n return {\n name: this.name,\n step: {\n integration: this.integration,\n [this.type]: {\n httpMethod: this.method,\n openApiAction: openApiAction(),\n ...(this.url ? { [this.isIntegration() ? "urlPath" : "path"]: await binding(this.url) } : {}),\n ...(this.headers ? { headers: await Promise.all(this.headers.map(async (header) => ({ key: await binding(header.key), value: await binding(header.value) }))) } : {}),\n ...(this.params ? { params: await Promise.all(this.params.map(async (param) => ({ key: await binding(param.key), value: await binding(param.value) }))) } : {}),\n ...(this.body ? { body: await binding(this.body) } : {}),\n\n // Other params that I might need to add.\n responseType: "auto",\n bodyType: "jsonBody",\n },\n },\n };\n }\n\n private isIntegration(): boolean {\n return this.integration !== "restapi";\n }\n\n public async toSDK(entities: string[]): Promise<string> {\n return `new RestApi(${await this.constructorParametersString(entities)})`;\n }\n\n protected async constructorParametersString(entities: string[]): Promise<string> {\n const parts: string[] = [`method: "${this.method}"`];\n\n if (this.url) {\n parts.push(\n `url: ${\n typeof this.url === "function"\n ? signatureV2((await toJSBody(this.url)).body, entities)\n : `"${this.url}"`\n }`,\n );\n }\n\n if (this.headers) {\n const headers = (\n await Promise.all(this.headers.map(async (h) =>\n `{ key: ${\n typeof h.key === "function"\n ? signatureV2((await toJSBody(h.key)).body, entities)\n : `"${h.key}"`\n }, value: ${\n typeof h.value === "function"\n ? signatureV2((await toJSBody(h.value)).body, entities)\n : `"${h.value}"`\n } }`,\n ))\n ).join(", ");\n parts.push(`headers: [${headers}]`);\n }\n\n if (this.params) {\n const params = (\n await Promise.all(this.params.map(async (p) =>\n `{ key: ${\n typeof p.key === "function"\n ? signatureV2((await toJSBody(p.key)).body, entities)\n : `"${p.key}"`\n }, value: ${\n typeof p.value === "function"\n ? signatureV2((await toJSBody(p.value)).body, entities)\n : `"${p.value}"`\n } }`,\n ))\n ).join(", ");\n parts.push(`params: [${params}]`);\n }\n\n if (this.body) {\n parts.push(\n `body: ${\n typeof this.body === "function"\n ? signatureV2((await toJSBody(this.body)).body, entities)\n : JSON.stringify(this.body)\n }`,\n );\n }\n\n return `"${this.name}", "${this.integration}", { ${parts.join(", ")} }, ${JSON.stringify(this.openapi)}`\n }\n\n public static fromJSON(json: any, entities: string[]): RestApi {\n const config = json?.step?.[json?.step?.integration === "restapi" ? "restapi" : "restapiintegration"]\n\n if (json?.step?.integration === "restapiintegration") {\n return new RestApi(json?.name, json?.step?.integration, OpenApi.configFromJSON(config, entities), OpenApi.openapiFromJSON(config))\n }\n\n return new RestApi(json?.name, json?.step?.integration, {\n method: config?.httpMethod,\n url: fromBinding(config?.path, entities),\n headers: config?.headers?.map((header: any) => ({ key: fromBinding(header.key, entities), value: fromBinding(header.value, entities) })),\n params: config?.params?.map((param: any) => ({ key: fromBinding(param.key, entities), value: fromBinding(param.value, entities) })),\n body: fromBinding(config?.body, entities),\n }, OpenApi.openapiFromJSON(config));\n }\n}\n\nexport class OpenApi extends RestApi {\n constructor(\n name: string,\n integration: string,\n config: {\n method: string;\n url: Binding<string>;\n headers?: { key: Binding<string>; value: Binding<string> }[];\n params?: { key: Binding<string>; value: Binding<string> }[];\n body?: Binding<string>;\n },\n openapi?: {\n path: string;\n }\n ) {\n super(name, integration, config, openapi);\n }\n\n public static configFromJSON(json: any, entities: string[]): {\n method: string;\n url: Binding<string>;\n headers?: { key: Binding<string>; value: Binding<string> }[];\n params?: { key: Binding<string>; value: Binding<string> }[];\n body?: Binding<string>;\n } {\n return {\n method: json?.httpMethod,\n url: fromBinding(json?.urlPath, entities),\n headers: json?.headers?.map((header: any) => ({ key: fromBinding(header.key, entities), value: fromBinding(header.value, entities) })),\n params: json?.params?.map((param: any) => ({ key: fromBinding(param.key, entities), value: fromBinding(param.value, entities) })),\n body: fromBinding(json?.body, entities),\n }\n }\n\n public static openapiFromJSON(json: any): {\n path: string;\n } {\n return json?.openApiAction && json.openApiAction !== "" && json.openApiAction !== "genericHttpRequest" ? { path: json.openApiAction.split(" ")[1] } : undefined\n }\n}\n\nexport class Jira extends OpenApi {\n constructor(\n name: string,\n integration: string,\n config: {\n method: string;\n url: Binding<string>;\n headers?: { key: Binding<string>; value: Binding<string> }[];\n params?: { key: Binding<string>; value: Binding<string> }[];\n body?: Binding<string>;\n },\n openapi?: {\n path: string;\n }\n ) {\n super(name, integration, config, openapi);\n this.type = "jira"\n }\n\n public async toSDK(entities: string[]): Promise<string> {\n return `new Jira(${await this.constructorParametersString(entities)})`;\n }\n\n public static fromJSON(json: any, entities: string[]): Jira {\n return new Jira(json?.name, json?.step?.integration, OpenApi.configFromJSON(json?.step?.jira, entities), OpenApi.openapiFromJSON(json?.step?.jira))\n }\n}\n\nexport class GitHub extends OpenApi {\n constructor(\n name: string,\n integration: string,\n config: {\n method: string;\n url: Binding<string>;\n headers?: { key: Binding<string>; value: Binding<string> }[];\n params?: { key: Binding<string>; value: Binding<string> }[];\n body?: Binding<string>;\n },\n openapi?: {\n path: string;\n }\n ) {\n super(name, integration, config, openapi);\n this.type = "github"\n }\n\n public async toSDK(entities: string[]): Promise<string> {\n return `new GitHub(${await this.constructorParametersString(entities)})`;\n }\n\n public static fromJSON(json: any, entities: string[]): GitHub {\n return new GitHub(json?.name, json?.step?.integration, OpenApi.configFromJSON(json?.step?.github, entities), OpenApi.openapiFromJSON(json?.step?.github))\n }\n}\n\nexport class Email extends Integration {\n private subject: Binding<string>;\n private from: Binding<string>; // comma separated list\n private to: Binding<string>; // comma separated list\n private cc?: Binding<string>; // comma separated list\n private bcc?: Binding<string>; // comma separated list\n private body: Binding<string>;\n\n constructor(\n name: string,\n config: {\n from: Binding<string>;\n to: Binding<string>;\n subject: Binding<string>;\n cc?: Binding<string>;\n bcc?: Binding<string>;\n body?: Binding<string>;\n },\n ) {\n super(name, "email");\n\n this.from = config.from;\n this.to = config.to;\n this.subject = config.subject;\n this.cc = config.cc;\n this.bcc = config.bcc;\n this.body = config.body;\n }\n\n public async toJSON(): Promise<JsonValue> {\n return {\n name: this.name,\n step: {\n integration: "email",\n email: {\n ...(this.from ? { emailFrom: await binding(this.from) } : {}),\n ...(this.to ? { emailTo: await binding(this.to) } : {}),\n ...(this.cc ? { emailCc: await binding(this.cc) } : {}),\n ...(this.bcc ? { emailBcc: await binding(this.bcc) } : {}),\n ...(this.subject ? { emailSubject: await binding(this.subject) } : {}),\n ...(this.body ? { emailBody: await binding(this.body) } : {}),\n },\n },\n };\n }\n\n public async toSDK(entities: string[]): Promise<string> {\n const parts: string[] = [];\n\n if (this.from) {\n parts.push(\n `from: ${\n typeof this.from === "function"\n ? signatureV2((await toJSBody(this.from)).body, entities)\n : `"${this.from}"`\n }`,\n );\n }\n\n if (this.to) {\n parts.push(\n `to: ${\n typeof this.to === "function"\n ? signatureV2((await toJSBody(this.to)).body, entities)\n : `"${this.to}"`\n }`,\n );\n }\n\n if (this.cc) {\n parts.push(\n `cc: ${\n typeof this.cc === "function"\n ? signatureV2((await toJSBody(this.cc)).body, entities)\n : `"${this.cc}"`\n }`,\n );\n }\n\n if (this.bcc) {\n parts.push(\n `bcc: ${\n typeof this.bcc === "function"\n ? signatureV2((await toJSBody(this.bcc)).body, entities)\n : `"${this.bcc}"`\n }`,\n );\n }\n\n if (this.subject) {\n parts.push(\n `subject: ${\n typeof this.subject === "function"\n ? signatureV2((await toJSBody(this.subject)).body, entities)\n : `"${this.subject}"`\n }`,\n );\n }\n\n if (this.body) {\n parts.push(\n `body: ${\n typeof this.body === "function"\n ? signatureV2((await toJSBody(this.body)).body, entities)\n : `"${this.body}"`\n }`,\n );\n }\n\n return `new Email("${this.name}", { ${parts.join(", ")} })`;\n }\n\n public static fromJSON(json: any, entities: string[]): Email {\n return new Email(json?.name, {\n from: fromBinding(json?.step?.email?.emailFrom, entities),\n to: fromBinding(json?.step?.email?.emailTo, entities),\n subject: fromBinding(json?.step?.email?.emailSubject, entities),\n body: fromBinding(json?.step?.email?.emailBody, entities),\n cc: json?.step?.email?.emailCc ? fromBinding(json?.step?.email?.emailCc, entities) : undefined,\n bcc: json?.step?.email?.emailBcc ? fromBinding(json?.step?.email?.emailBcc, entities) : undefined,\n });\n }\n}\n\nexport class Databricks extends Integration {\n private statement: Binding<string>;\n\n constructor(\n name: string,\n integration: string,\n config: {\n statement: Binding<string>;\n },\n ) {\n super(name, integration);\n\n this.statement = config.statement;\n }\n\n public async toJSON(): Promise<JsonValue> {\n return {\n name: this.name,\n step: {\n integration: this.integration,\n databricks: {\n runSql: {\n sqlBody: await binding(this.statement),\n useParameterized: false,\n },\n },\n },\n };\n }\n\n public async toSDK(entities: string[]): Promise<string> {\n return `new Databricks("${this.name}", "${this.integration}", { statement: ${typeof this.statement === "function" ? signatureV2((await toJSBody(this.statement)).body, entities) : `"${this.statement}"`} })`;\n }\n\n public static fromJSON(json: any, entities: string[]): Databricks {\n return new Databricks(json?.name, json?.step?.integration, {\n statement: fromBinding(\n json?.step?.databricks?.runSql?.sqlBody as string,\n entities,\n ),\n });\n }\n}\n\nexport type Condition = {\n when: Binding<boolean>;\n then: Block[];\n};\n\nasync function conditionToSDK(condition: Condition, entities: string[]): Promise<string> {\n return `{ when: ${typeof condition.when === "function" ? signatureV2((await toJSBody(condition.when)).body, entities) : condition.when}, then: [${(await Promise.all(condition.then.map(async (block) => await block.toSDK(entities)))).join(",")}] }`\n}\n\nexport type Conditions = {\n if: Condition;\n elif?: Condition[];\n else?: Block[];\n};\n\nexport class Conditional extends Block {\n public conditions: Conditions;\n\n constructor(name: string, config: Conditions) {\n super(name);\n\n this.conditions = config;\n }\n\n public async toJSON(): Promise<JsonValue> {\n return {\n name: this.name,\n conditional: {\n if: {\n condition: ensureIIFE((await toJS<boolean>(this.conditions.if.when)).body),\n blocks: await Promise.all(this.conditions.if.then.map(async (block) => await block.toJSON())),\n },\n ...await (async () =>\n this.conditions.elif\n ? {\n elseIf: await Promise.all(this.conditions.elif.map(async (condition) => ({\n condition: ensureIIFE((await toJS<boolean>(condition.when)).body),\n blocks: await Promise.all(condition.then.map(async (block) => await block.toJSON())),\n }))),\n }\n : { elseIf: [] })(),\n else: this.conditions.else ? {\n blocks: await Promise.all(this.conditions.else.map(async (block) => await block.toJSON()))\n } : undefined,\n },\n };\n }\n\n public static fromJSON(json: any, entities: string[]): Conditional {\n return new Conditional(json?.name, {\n if: {\n when: fromJS<boolean>(json?.conditional?.if?.condition, entities),\n then: json?.conditional?.if?.blocks.map((block: any) =>\n Block.fromJSON(block, entities),\n ),\n },\n elif: json?.conditional?.elseIf ? json?.conditional?.elseIf?.map((condition: any) => ({\n when: fromJS<boolean>(condition.condition, entities),\n then: condition.blocks.map((block: any) => Block.fromJSON(block, entities)),\n })) : [],\n else: json?.conditional?.else ? json?.conditional?.else?.blocks?.map((block: any) => Block.fromJSON(block, entities)) : [],\n });\n }\n\n public async toSDK(entities: string[]): Promise<string> {\n return `new Conditional("${this.name}", { if: ${await conditionToSDK(this.conditions.if, entities)}, elif: [${this.conditions.elif ? (await Promise.all(this.conditions.elif?.map(async (condition) => await conditionToSDK(condition, entities)))).join(",") : \'\'}], else: ${this.conditions.else ? (await Promise.all(this.conditions.else?.map(async (block) => await block.toSDK(entities)))).join(",") : "undefined"} })`;\n }\n}\n\nexport class Parallel extends Block {\n private over: Binding<JsonValue[]>;\n private blocks: Block[];\n private variables: { item: string };\n\n constructor(\n name: string,\n config: {\n over: Binding<JsonValue[]>;\n variables: { item: string };\n blocks: Block[];\n },\n ) {\n super(name);\n\n this.over = config.over;\n this.blocks = config.blocks;\n this.variables = config.variables;\n }\n\n public async toJSON(): Promise<JsonValue> {\n return {\n name: this.name,\n parallel: {\n dynamic: {\n blocks: await Promise.all(this.blocks?.map(async (block) => await block.toJSON())),\n paths: ensureIIFE((await toJS<JsonValue[]>(this.over)).body),\n variables: this.variables,\n },\n wait: "WAIT_ALL",\n },\n };\n }\n\n public async toSDK(entities: string[]): Promise<string> {\n return `new Parallel("${this.name}", { over: ${typeof this.over === "function" ? signatureV2((await toJSBody(this.over)).body, entities) : this.over}, variables: ${this.variables}, blocks: ${await Promise.all(this.blocks.map(async (block) => await block.toSDK(entities)).join(","))} })`;\n }\n\n public static fromJSON(json: any, entities: string[]): Parallel {\n return new Parallel(json?.name, {\n over: fromJS<JsonValue[]>(json?.parallel?.dynamic?.paths, entities),\n variables: {\n item: json?.parallel?.dynamic?.variables?.item,\n },\n blocks: json?.parallel?.dynamic?.blocks.map((block: any) =>\n Block.fromJSON(block, entities),\n ),\n });\n }\n}\n\nexport class Loop extends Block {\n private over: Binding<JsonValue[]>; // NOTE(Frank): Only for each loops are supported at the moment.\n private blocks: Block[];\n private variables: { item: string; index: string };\n\n constructor(\n name: string,\n config: {\n over: Binding<JsonValue[]>;\n variables: { item: string; index: string };\n blocks: Block[];\n },\n ) {\n super(name);\n\n this.over = config.over;\n this.variables = config.variables;\n this.blocks = config.blocks;\n }\n\n public async toJSON(): Promise<JsonValue> {\n return {\n name: this.name,\n loop: {\n range: ensureIIFE((await toJS<JsonValue[]>(this.over)).body),\n type: "TYPE_FOREACH",\n variables: this.variables,\n blocks: await Promise.all(this.blocks.map(async (block) => await block.toJSON())),\n },\n };\n }\n\n public async toSDK(entities: string[]): Promise<string> {\n return `new Loop("${this.name}", { over: ${typeof this.over === "function" ? signatureV2((await toJSBody(this.over)).body, entities) : this.over}, variables: ${this.variables}, blocks: ${await Promise.all(this.blocks.map(async (block) => await block.toSDK(entities)).join(","))} })`;\n }\n\n public static fromJSON(json: any, entities: string[]): Loop {\n return new Loop(json?.name, {\n over: fromJS<JsonValue[]>(json?.loop?.range, entities),\n variables: {\n item: json?.loop?.variables?.item,\n index: json?.loop?.variables?.index,\n },\n blocks: json?.loop?.blocks.map((block: any) =>\n Block.fromJSON(block, entities),\n ),\n });\n }\n}\n\nexport class Variables extends Block {\n private variables: { key: string, value: Binding<JsonValue> }[];\n\n constructor(\n name: string,\n variables: { key: string, value: Binding<JsonValue> }[],\n ) {\n super(name);\n\n this.variables = variables;\n }\n\n public async toJSON(): Promise<JsonValue> {\n return {\n name: this.name,\n variables: {\n items: await Promise.all(this.variables.map(async (variable) => {\n return {\n key: variable.key,\n value: (await toJS<JsonValue>(variable.value)).body,\n type: "TYPE_SIMPLE",\n mode: "MODE_READWRITE",\n };\n }))\n },\n };\n }\n \n public async toSDK(entities: string[]): Promise<string> {\n return `new Variables("${this.name}", [${\n (await Promise.all(\n this.variables?.map(async (variable) => (\n `{ key: "${variable.key}", value: ${typeof variable.value === "function" ? signatureV2((await toJSBody(variable.value)).body, entities) : variable.value} }`\n ))\n )).join(",")}])`;\n }\n\n public static fromJSON(json: any, entities: string[]): Variables {\n return new Variables(json?.name, json?.variables?.items?.map((variable: any) => ({\n key: variable.key,\n value: fromBinding(variable.value, entities),\n })));\n }\n}\n\nexport class TryCatch extends Block {\n private try: Block[];\n private catch: Block[];\n private finally?: Block[];\n private variables: { error: string };\n\n constructor(\n name: string,\n config: {\n try: Block[];\n catch: Block[];\n finally?: Block[];\n variables: { error: string };\n },\n ) {\n super(name);\n\n this.try = config.try;\n this.catch = config.catch;\n this.finally = config.finally;\n this.variables = config.variables;\n }\n\n public async toJSON(): Promise<JsonValue> {\n return {\n name: this.name,\n tryCatch: {\n try: { blocks: await Promise.all(this.try.map(async (block) => await block.toJSON())) },\n catch: { blocks: await Promise.all(this.catch.map(async (block) => await block.toJSON())) },\n finally: { blocks: this.finally ? await Promise.all(this.finally.map(async (block) => await block.toJSON())) : [] },\n variables: this.variables,\n },\n };\n }\n\n public async toSDK(entities: string[]): Promise<string> {\n return `new TryCatch("${this.name}", { try: ${await Promise.all(this.try.map(async (block) => await block.toSDK(entities)).join(","))}, catch: ${await Promise.all(this.catch.map(async (block) => await block.toSDK(entities)).join(","))}, finally: ${this.finally?.map(async (block) => await block.toSDK(entities)).join(", ")} })`;\n }\n\n public static fromJSON(json: any, entities: string[]): TryCatch {\n return new TryCatch(json?.name, {\n try: json?.tryCatch?.try.blocks.map((block: any) => Block.fromJSON(block, entities)),\n catch: json?.tryCatch?.catch.blocks.map((block: any) => Block.fromJSON(block, entities)),\n finally: json?.tryCatch?.finally?.blocks.map((block: any) => Block.fromJSON(block, entities)),\n variables: {\n error: json?.tryCatch?.variables?.error,\n },\n });\n }\n}\n\nexport class Throw extends Block {\n private error: Binding<JsonValue>;\n\n constructor(\n name: string,\n config: {\n error: Binding<JsonValue>;\n },\n ) {\n super(name);\n\n this.error = config.error;\n }\n\n public async toJSON(): Promise<JsonValue> {\n return {\n name: this.name,\n throw: {\n error: (await toJS<JsonValue>(this.error)).body,\n },\n };\n }\n\n public async toSDK(entities: string[]): Promise<string> {\n return `new Throw("${this.name}", { error: ${typeof this.error === "function" ? signatureV2((await toJSBody(this.error)).body, entities) : JSON.stringify(this.error)} })`;\n }\n\n public static fromJSON(json: any, entities: string[]): Throw {\n return new Throw(json?.name, {\n error: fromJS(json?.throw?.error, entities),\n });\n }\n}\n\nexport class Return extends Block {\n private data: Binding<JsonValue>;\n\n constructor(\n name: string,\n config: {\n data: Binding<JsonValue>;\n },\n ) {\n super(name);\n\n this.data = config.data;\n }\n\n public async toJSON(): Promise<JsonValue> {\n return {\n name: this.name,\n return: {\n data: (await toJS<JsonValue>(this.data)).body,\n },\n };\n }\n\n public async toSDK(entities: string[]): Promise<string> {\n return `new Return("${this.name}", { data: ${typeof this.data === "function" ? signatureV2((await toJSBody(this.data)).body, entities) : JSON.stringify(this.data)} })`;\n }\n\n public static fromJSON(json: any, entities: string[]): Return {\n return new Return(json?.name, {\n data: fromJS(json?.return?.data, entities),\n });\n }\n}\n\nexport type Authorization = {\n type: "AUTHORIZATION_TYPE_APP_USERS",\n} | {\n type: "AUTHORIZATION_TYPE_JS_EXPRESSION",\n expression: Binding<boolean>,\n}\n\nexport class Api {\n private blocks: Block[];\n private name: string;\n private authorization?: Authorization;\n\n constructor(name: string, blocks: Block[] = [], authorization: Authorization = { type: "AUTHORIZATION_TYPE_APP_USERS" }) {\n this.name = name;\n this.blocks = blocks;\n this.authorization = authorization;\n }\n\n public async toSDK(entities: string[]): Promise<string> {\n const imports =\n "import { Api, JavaScript, Python, PostgreSQL, Snowflake, RestApi, Databricks, Conditional, Parallel, Loop, TryCatch, Variables, Throw, Return } from \'@superblocksteam/library\'";\n return `${imports}\\n\\nexport default new Api("${this.name}", [${(await Promise.all(this.blocks.map(async (block) => await block.toSDK(entities)))).join(",")}], ${this.authorization.type === "AUTHORIZATION_TYPE_JS_EXPRESSION" ? `{ type: "AUTHORIZATION_TYPE_JS_EXPRESSION", expression: ${typeof this.authorization.expression === "function" ? signatureV2((await toJSBody(this.authorization.expression)).body, entities) : this.authorization.expression} }` : `{ type: "AUTHORIZATION_TYPE_APP_USERS" }`})`;\n }\n\n public static fromJSON(json: any, entities: string[]): Api {\n // NOTE(Frank): Would probs unmarshal this into the protobuf types and then do the conversion.\n return new Api(\n json?.metadata?.name as string,\n (json.blocks as any[]).map((block) => Block.fromJSON(block, entities)),\n { type: json.authorization.type, expression: json.authorization.type === "AUTHORIZATION_TYPE_JS_EXPRESSION" ? fromJS(json.authorization.expression, entities) : undefined },\n );\n }\n\n public async toJSON(): Promise<JsonValue> {\n const api: {\n metadata: {\n name: string;\n timestamps: {\n updated: string;\n };\n };\n authorization: {\n type: "AUTHORIZATION_TYPE_APP_USERS" | "AUTHORIZATION_TYPE_JS_EXPRESSION",\n expression?: string,\n };\n trigger: {\n application: {};\n };\n blocks: JsonValue[];\n } = {\n metadata: {\n name: this.name,\n timestamps: {\n updated: new Date().toISOString(),\n },\n },\n authorization: {\n type: this.authorization.type,\n ...{expression: this.authorization.type === "AUTHORIZATION_TYPE_JS_EXPRESSION" ? `(() => ${(await toJS<boolean>(this.authorization.expression)).body})()` : undefined},\n },\n trigger: {\n application: {},\n },\n blocks: [],\n };\n\n api.blocks = await Promise.all(this.blocks.map(async (block) => await block.toJSON()));\n\n return api;\n }\n}\n\nfunction referenced(data: string, entities: string[]): string[] {\n return entities.reduce((acc: string[], entity: string) => {\n if (data.includes(entity)) {\n acc.push(entity);\n }\n return acc;\n }, []);\n}\n\nexport function signatureV2(data: string, entities: string[] = []): string {\n return `({ ${referenced(data, entities).join(", ")} }) => ${data}`;\n}\n\n/**\n * Converts a string binding, which comprise 99% of dynamic integration fields, into a function returning an interpolated string.\n *\n * fromBinding(`https://${ \'goo\' + \'gle\' }.com/${ Dropdown1.value }`, [\'Dropdown1\']) -> ({ Dropdown1 }) => `https://${\'goo\' + \'gle\'}.com/${Dropdown1.value}`\n *\n * @param value - The value to convert.\n * @param entities - The master list of possible entities that could be referenced in the binding.\n *\n * @returns The converted value.\n */\nexport function fromBinding(value: string, entities: string[]): Binding<string> {\n if (!value) {\n return "";\n }\n\n if (!value.startsWith("`") || !value.endsWith("`")) {\n return value;\n }\n\n const args: string[] = [`return ${value}`]\n const references: string[] = referenced(value, entities);\n\n if (references.length === 0) {\n try {\n return eval(value) as string;\n } catch (e) {\n console.error(`We couldn\'t tranform the binding ${value} to the SDK. Ensure the AI hasn\'t hallucinated a variable.`);\n throw e;\n }\n }\n\n // only add the arguments if we have references\n args.unshift(`{ ${references} }`);\n\n return new Function(...args) as (_: State) => string;\n}\n\nasync function beautifyAndOptionallyCondense<T extends JsonValue>(value: ((state: State) => T), node: Node, options: { condense?: boolean, block?: boolean } = { condense: true, block: true }) {\n let start: number = node.start;\n let end: number = node.end;\n\n if (!options.block) {\n start += 1;\n end -= 1;\n }\n \n const beautified = await beautify(value.toString().slice(start, end))\n\n if (options.condense) {\n return beautified.replace(/\\s*\\n\\s*/g, \' \').replace(/\\s+/g, \' \').trim();\n }\n\n return beautified\n}\n\nfunction createAst(value: string): Program {\n return parse(value, {\n ecmaVersion: "latest",\n sourceType: "script",\n preserveParens: false,\n });\n}\n\nexport async function toJSBody<T extends JsonValue>(value: ((state: State) => T), options: { block?: boolean, condense?: boolean, function?: boolean } = { block: true, condense: true, function: false }): Promise<{body: string, iife: boolean, node?: Node}> {\n if (typeof value !== "function") {\n return {\n body: `\\`${JSON.stringify(value)}\\``,\n iife: true,\n }\n }\n\n // parse the code\n const ast = createAst(value.toString());\n const program = ast.body[0];\n\n switch (program.type) {\n case "ExpressionStatement":\n const body = ((program as ExpressionStatement).expression as FunctionExpression).body;\n\n if (body.type === \'BlockStatement\') {\n return {\n body: await beautifyAndOptionallyCondense(value, body, options),\n iife: true,\n node: body,\n }\n }\n\n return {\n body: `${options.function ? "return " : ""}${value.toString().slice(body.start, body.end)}`,\n iife: options.function,\n node: body,\n }\n case "FunctionDeclaration":\n let contents = value.toString().slice(program.body.start, program.body.end)\n\n if (program.body.type !== \'BlockStatement\') {\n return {\n body: contents,\n iife: false,\n node: program.body,\n }\n }\n \n if (!options.function && program.body.body.length === 1 && program.body.body[0].type === \'ReturnStatement\') {\n return {\n body: value.toString().slice(program.body.body[0].argument.start, program.body.body[0].argument.end),\n iife: false, // This should be false but we need to deal with the nested string template case.\n node: program.body.body[0].argument,\n }\n }\n\n return {\n body: await beautifyAndOptionallyCondense(value, program.body, options),\n iife: true,\n node: program.body,\n }\n default:\n throw new Error("you found a case we haven\'t handled yet")\n }\n}\n\nexport async function toJS<T extends JsonValue>(value: Binding<T>): Promise<{body: string, node?: Node}> {\n if (typeof value !== "function") {\n return {\n body: JSON.stringify(value),\n }\n }\n\n const { body, iife, node } = await toJSBody(value);\n\n if (iife) {\n return {\n body: `(() => ${body})()`,\n node,\n }\n }\n\n return { body, node }\n}\n\nexport function fromJS<T extends JsonValue>(\n value: string,\n entities: string[] = [],\n): Binding<T> {\n // determine if there are entities referenced in the value\n const refs = referenced(value, entities).join(", ")\n\n // remove any surrounding whitespace\n value = value.trim()\n\n let unaryOps: string[] = [];\n let call = (createAst(value).body[0] as ExpressionStatement).expression;\n\n while (call?.type === \'UnaryExpression\') {\n unaryOps.push((call as UnaryExpression).operator);\n call = (call as unknown as UnaryExpression).argument as CallExpression;\n }\n \n const fn = (call as CallExpression).callee;\n let body = value;\n let prefix = "return ";\n\n // handle iife\n if (fn?.type === "ArrowFunctionExpression") {\n // i\'m not happy with this but I don\'t see another immediate way to handle this.\n // this will result in a correct result but one that isn\'t idemopotent.\n if (unaryOps.length > 0) {\n return new Function(`{ ${refs} }`, `${prefix}${body}`) as (_: State) => T;\n }\n\n\n body = value.slice(fn.body.start, fn.body.end)\n\n if (fn.body.type === \'BlockStatement\') {\n // drop the opening "{" and closing "}"\n body = body.slice(1, -1)\n prefix = ""\n } \n } else if (refs.length === 0) {\n // we\'re not an iife nor do we have references.\n // we might have to revisit this implementation since this technically reduces to a static value down to it\'s most minimal form.\n return eval(value) as T\n }\n\n // we\'re either an iife or a static value with refs.\n return new Function(`{ ${refs} }`, `${prefix}${body}`) as (_: State) => T;\n}\n\nexport async function beautify(code: string): Promise<string> {\n const eslint = new ESLint({\n fix: true,\n overrideConfigFile: true,\n baseConfig: {\n languageOptions: {\n parser: typescript_eslint_parser,\n parserOptions: {\n ecmaVersion: 2020,\n sourceType: "module",\n },\n },\n rules: {\n "arrow-body-style": "error",\n },\n },\n });\n\n code = (await eslint.lintText(code))[0]?.output || code;\n\n return await format(code, {\n parser: "typescript",\n tabWidth: 2,\n // singleQuote: true,\n trailingComma: "none",\n semi: true,\n bracketSpacing: true,\n arrowParens: "always",\n bracketSameLine: false,\n embeddedLanguageFormatting: "auto",\n quoteProps: "as-needed",\n insertPragma: false,\n requirePragma: false,\n useTabs: false,\n endOfLine: "auto",\n arrowFunctionParentheses: "always",\n // plugins: ["prettier-plugin-organize-imports"],\n });\n}\n\nexport function ensureIIFE(value: string): string {\n // We write the IIFE so we can be loose with the fomratting here. \n // For example, we\'d never get ( ( ) =>)().\n if (value.startsWith("(() => ") && value.endsWith(")()")) {\n return value\n }\n\n return `(() => ${value})()`\n}',
292450
292450
  "src/to-sdk/__template__.ts": 'import { Api } from "@superblocksteam/library";\n\nconst json = {};\n\nconst entities = [];\n\nconst api = Api.fromJSON(json, entities);\n\nexport default {\n toSDK: async () => await api.toSDK(entities),\n};\n',
292451
292451
  "tsconfig.json": '{\n "compilerOptions": {\n "target": "ES2020",\n "module": "ESNext",\n "moduleResolution": "bundler",\n "baseUrl": ".",\n "outDir": "dist",\n "rootDir": "src",\n "esModuleInterop": true,\n "resolveJsonModule": true,\n "skipLibCheck": true,\n "types": [\n "node"\n ],\n "paths": {\n "@superblocksteam/library": [\n "src/superblocks-library-shim"\n ]\n }\n },\n "include": [\n "src"\n ],\n "exclude": [\n "node_modules",\n "dist",\n ]\n}'
292452
292452
  }
@@ -296529,7 +296529,7 @@ init_cjs_shims();
296529
296529
  init_cjs_shims();
296530
296530
  var generated = {};
296531
296531
  try {
296532
- generated = await import("./generated-C6SMCIBJ.js");
296532
+ generated = await import("./generated-45HB5XLB.js");
296533
296533
  } catch (_error) {
296534
296534
  getLogger().warn("[ai-service] Generated markdown modules not found. Run `pnpm generate:markdown` first.");
296535
296535
  }
@@ -321924,7 +321924,7 @@ var import_util28 = __toESM(require_dist3(), 1);
321924
321924
  // ../sdk/package.json
321925
321925
  var package_default = {
321926
321926
  name: "@superblocksteam/sdk",
321927
- version: "2.0.3-next.203",
321927
+ version: "2.0.3-next.204",
321928
321928
  type: "module",
321929
321929
  description: "Superblocks JS SDK",
321930
321930
  homepage: "https://www.superblocks.com",
@@ -321966,8 +321966,8 @@ var package_default = {
321966
321966
  "@rollup/wasm-node": "^4.35.0",
321967
321967
  "@superblocksteam/bucketeer-sdk": "0.5.0",
321968
321968
  "@superblocksteam/shared": "0.9160.0",
321969
- "@superblocksteam/util": "2.0.3-next.203",
321970
- "@superblocksteam/vite-plugin-file-sync": "2.0.3-next.203",
321969
+ "@superblocksteam/util": "2.0.3-next.204",
321970
+ "@superblocksteam/vite-plugin-file-sync": "2.0.3-next.204",
321971
321971
  "@vitejs/plugin-react": "^4.3.4",
321972
321972
  axios: "^1.4.0",
321973
321973
  chokidar: "^4.0.3",
@@ -328941,7 +328941,7 @@ async function startVite({ app, httpServer: httpServer2, root: root2, mode, port
328941
328941
  };
328942
328942
  const isCustomBuildEnabled2 = await isCustomComponentsEnabled();
328943
328943
  const customFolder = path34.join(root2, "custom");
328944
- const cdnUrl = "https://assets-cdn.superblocks.com/library/2.0.3-next.203";
328944
+ const cdnUrl = "https://assets-cdn.superblocks.com/library/2.0.3-next.204";
328945
328945
  const env3 = loadEnv(mode, root2, "");
328946
328946
  const hmrPort = await getFreePort();
328947
328947
  const hmrOptions = {
@@ -571,5 +571,5 @@
571
571
  "strict": true
572
572
  }
573
573
  },
574
- "version": "2.0.3-next.203"
574
+ "version": "2.0.3-next.204"
575
575
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@superblocksteam/cli",
3
- "version": "2.0.3-next.203",
3
+ "version": "2.0.3-next.204",
4
4
  "type": "module",
5
5
  "description": "Official Superblocks CLI",
6
6
  "homepage": "https://www.superblocks.com",
@@ -42,9 +42,9 @@
42
42
  "devDependencies": {
43
43
  "@eslint/js": "^9.16.0",
44
44
  "@oclif/test": "^4.1.11",
45
- "@superblocksteam/sdk": "2.0.3-next.203",
45
+ "@superblocksteam/sdk": "2.0.3-next.204",
46
46
  "@superblocksteam/shared": "0.9160.0",
47
- "@superblocksteam/util": "2.0.3-next.203",
47
+ "@superblocksteam/util": "2.0.3-next.204",
48
48
  "@types/babel__core": "^7.20.0",
49
49
  "@types/chai": "^4",
50
50
  "@types/fs-extra": "^11.0.1",