Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#8066 Handle TABLE data type in AA api responses #8155

Merged
merged 4 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
120 changes: 120 additions & 0 deletions src/contrib/automationanywhere/aaUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,109 @@ describe("selectBotOutput", () => {
},
],
},
out_Table: {
type: "TABLE",
boolean: "",
string: "",
number: "",
dictionary: [],
table: {
schema: [
{
name: "Date",
type: "STRING",
subtype: "STRING",
},
{
name: "Items",
type: "STRING",
subtype: "STRING",
},
{
name: "Total",
type: "STRING",
subtype: "STRING",
},
],
rows: [
{
values: [
{
type: "STRING",
string: "2022-01-01",
number: "",
boolean: "",
dictionary: [],
},
{
type: "STRING",
string: "Foo, bar, baz",
number: "",
boolean: "",
dictionary: [],
},
{
type: "STRING",
string: "42",
number: "",
boolean: "",
dictionary: [],
},
],
},
{
values: [
{
type: "STRING",
string: "2022-01-02",
number: "",
boolean: "",
dictionary: [],
},
{
type: "STRING",
string: "Qux, quux",
number: "",
boolean: "",
dictionary: [],
},
{
type: "STRING",
string: "24",
number: "",
boolean: "",
dictionary: [],
},
],
},
{
values: [
{
type: "STRING",
string: "2022-01-03",
number: "",
boolean: "",
dictionary: [],
},
{
type: "STRING",
string: "Corge",
number: "",
boolean: "",
dictionary: [],
},
{
type: "STRING",
string: "12",
number: "",
boolean: "",
dictionary: [],
},
],
},
],
},
},
},
},
};
Expand All @@ -204,6 +307,23 @@ describe("selectBotOutput", () => {
baz: true,
qux: 42,
},
out_Table: [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

{
Date: "2022-01-01",
Items: "Foo, bar, baz",
Total: "42",
},
{
Date: "2022-01-02",
Items: "Qux, quux",
Total: "24",
},
{
Date: "2022-01-03",
Items: "Corge",
Total: "12",
},
],
});
});
});
31 changes: 28 additions & 3 deletions src/contrib/automationanywhere/aaUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ import {
type Execution,
type Interface,
type OutputValue,
type TableValue,
type Variable,
} from "@/contrib/automationanywhere/contract";
import { type JSONSchema7Type } from "json-schema";
import { mapValues } from "lodash";
import { mapValues, zipWith } from "lodash";
import { type Primitive } from "type-fest";
import { BusinessError } from "@/errors/businessErrors";
import { boolean } from "@/utils/typeUtils";
Expand Down Expand Up @@ -145,7 +146,21 @@ export function mapBotInput(data: UnknownObject) {
});
}

function mapBotOutput(value: OutputValue): Primitive | UnknownObject {
type BotOutput = Primitive | UnknownObject | UnknownObject[];

function mapBotTableValue(tableValue: TableValue): UnknownObject[] {
const { schema, rows } = tableValue;
return rows.map((row) => {
const newEntries: Array<[string, BotOutput]> = zipWith(
schema,
row.values,
(columnSchema, rowValue) => [columnSchema.name, mapBotOutput(rowValue)],
);
return Object.fromEntries(newEntries);
});
}

function mapBotOutput(value: OutputValue): BotOutput {
switch (value.type) {
case "STRING": {
return value.string;
Expand All @@ -165,6 +180,16 @@ function mapBotOutput(value: OutputValue): Primitive | UnknownObject {
);
}

case "TABLE": {
if (value.table == null) {
// This should never happen, but be defensive just in case,
// since it's an api payload
return [];
}

return mapBotTableValue(value.table);
}

default: {
const exhaustiveCheck: never = value.type;
throw new BusinessError(
Expand All @@ -176,7 +201,7 @@ function mapBotOutput(value: OutputValue): Primitive | UnknownObject {

export function selectBotOutput(
execution: Pick<Execution, "botOutVariables">,
): Record<string, Primitive | UnknownObject> {
): Record<string, BotOutput> {
return mapValues(execution.botOutVariables?.values ?? {}, (value) =>
mapBotOutput(value),
);
Expand Down
23 changes: 22 additions & 1 deletion src/contrib/automationanywhere/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const API_TASK_TYPE = "application/vnd.aa.headlessbot";

// Bots in the "Private" workspace are also referred to as Local bots
export type WorkspaceType = "public" | "private";
type VariableType = "STRING" | "NUMBER" | "BOOLEAN" | "DICTIONARY";
type VariableType = "STRING" | "NUMBER" | "BOOLEAN" | "DICTIONARY" | "TABLE";

export type Variable = {
name: string;
Expand Down Expand Up @@ -119,6 +119,25 @@ export const FAILURE_STATUSES = new Set([
"RUN_TIMED_OUT",
]);

// Note: The following AA response types were reverse-engineered from the
// Automation Anywhere Run Bot API response payloads.

type TableColumnSchema = {
BLoe marked this conversation as resolved.
Show resolved Hide resolved
name: string;
type: string;
subtype: string;
};

type TableRow = {
// eslint-disable-next-line @typescript-eslint/no-use-before-define -- recursive type
values: OutputValue[];
};

export type TableValue = {
schema: TableColumnSchema[];
rows: TableRow[];
};

export type OutputValue = {
type: VariableType;
string: string;
Expand All @@ -128,6 +147,8 @@ export type OutputValue = {
key: string;
value: OutputValue;
}>;
// Seems like table property is optional on the response value objects
table?: TableValue;
BLoe marked this conversation as resolved.
Show resolved Hide resolved
};

export type Activity = {
Expand Down