Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(skill-burger): normalize recorded app results before piping

Afloata0f025375b64
diff --git a/server/skill-burger/app-results.test.ts b/server/skill-burger/app-results.test.ts
new file mode 100644
index 0000000000..fec59b20ee
--- /dev/null
+++ b/server/skill-burger/app-results.test.ts
@@ -0,0 +1,73 @@
+import { expect, it } from "vitest";
+
+import { draftWorkflow } from "../../shared/skill-burger/draft.ts";
+import type { RunMessage } from "../../shared/skill-burger/layers.ts";
+import { appToolAnswer } from "../connector-app-tools.ts";
+import { toolDetailPreview } from "../tool-summary.ts";
+import { createExecutors } from "./executors.ts";
+import { startRun } from "./runner.ts";
+
+const response = (results: Array<{ slug: string; data: unknown }>) => ({
+ jsonrpc: "2.0", id: 1,
+ result: { content: [{ type: "text", text: JSON.stringify({ successful: true, data: {
+ results: results.map(({ slug, data }) => ({ tool_slug: slug, response: { successful: true, data } })),
+ } }) }] },
+});
+const row = (id: string, name: string, input: unknown, output?: string): RunMessage => ({
+ id, role: "bot", kind: "activity", tool: { name, ok: true, input: JSON.stringify(input), ...(output ? { output } : {}) },
+});
+const ask: RunMessage = { id: "ask", role: "user", kind: "text", text: "Draft a reply to the latest invoice" };
+const recordedData = { messages: [{ threadId: "recorded-thread" }] };
+const draftArgs = { to: "[email protected]", subject: "Invoice", body: "Thanks", thread_id: "recorded-thread" };
+
+async function replay(messages: RunMessage[], expected = { tool: "GMAIL_CREATE_EMAIL_DRAFT", field: "thread_id", value: "current-thread" }) {
+ const calls: Array<{ tool_slug: string; arguments: Record<string, unknown> }> = [];
+ const workflow = draftWorkflow(messages)!.workflow;
+ const result = await startRun({ name: "reply", botId: "b", threadId: "t", sha256: "test", workflow }, {
+ exec: createExecutors({ botId: "b", cwd: ".", internal: async (kind, _path, body) => {
+ expect(kind).toBe("connectors");
+ const { tools } = (body as { params: { arguments: { tools: typeof calls } } }).params.arguments;
+ calls.push(...tools);
+ return { ok: true, body: response(tools.map((tool) => ({
+ slug: tool.tool_slug,
+ data: tool.tool_slug === "GMAIL_FETCH_EMAILS"
+ ? { messages: [{ threadId: tool.arguments.query === "sent" ? "current-sent-thread" : "current-thread" }] }
+ : { id: "current-draft" },
+ }))) };
+ } }),
+ events: { started() {}, ended() {} }, save() {},
+ });
+ expect(result).toMatchObject({ ok: true, status: "ok" });
+ expect(calls.at(-1)).toMatchObject({ tool_slug: expected.tool, arguments: { [expected.field]: expected.value } });
+}
+
+it.each(["data", "content", "result"])("pipes a plain app's recorded %s through the actual app executor", async (shape) => {
+ const data = { id: "recorded-draft" };
+ const frame = appToolAnswer("GMAIL_CREATE_EMAIL_DRAFT", response([{ slug: "GMAIL_CREATE_EMAIL_DRAFT", data }]));
+ const result = frame.result as { content: unknown[] };
+ const output = toolDetailPreview(shape === "data" ? data : shape === "content" ? result.content : result)!;
+ expect(output).toContain("recorded-draft");
+ await replay([
+ ask,
+ row("create", "mcp__composio__gmail_create_draft", draftArgs, output),
+ row("update", "mcp__composio__gmail_update_draft", { ...draftArgs, draft_id: "recorded-draft", body: "Revised draft" }),
+ ], { tool: "GMAIL_UPDATE_DRAFT", field: "draft_id", value: "current-draft" });
+});
+
+it.each([false, true])("pipes the selected multi-execute result with another read present: %s", async (batched) => {
+ const tools = [
+ ...(batched ? [{ tool_slug: "GMAIL_FETCH_EMAILS", arguments: { query: "sent" } }] : []),
+ { tool_slug: "GMAIL_FETCH_EMAILS", arguments: { query: "invoice" } },
+ ];
+ const output = response(tools.map((tool) => ({
+ slug: tool.tool_slug,
+ data: tool.arguments.query === "sent" ? { messages: [{ threadId: "recorded-sent-thread" }] } : recordedData,
+ }))).result.content[0]!.text;
+ await replay([
+ ask,
+ row("read", "mcp__composio__COMPOSIO_MULTI_EXECUTE_TOOL", { tools }, output),
+ row("draft", "mcp__composio__COMPOSIO_MULTI_EXECUTE_TOOL", { tools: [{ tool_slug: "GMAIL_CREATE_EMAIL_DRAFT", arguments: {
+ recipient_email: draftArgs.to, subject: draftArgs.subject, body: draftArgs.body, thread_id: draftArgs.thread_id,
+ } }] }),
+ ]);
+});
diff --git a/shared/skill-burger/draft.test.ts b/shared/skill-burger/draft.test.ts
index 91702a7e13..3bae475a10 100644
--- a/shared/skill-burger/draft.test.ts
+++ b/shared/skill-burger/draft.test.ts
@@ -123,7 +123,7 @@ describe("draftWorkflow", () => {
const forward = draft.workflow.steps[1]!;
expect(forward).toMatchObject({ kind: "app", app: "gmail", tool: "GMAIL_FORWARD_MESSAGE" });
expect(forward.kind === "app" && forward.args).toEqual({
- message_id: { $step: "s1", path: "data.results.0.response.data.messages.0.id" },
+ message_id: { $step: "s1", path: "messages.0.id" },
recipient_email: { $arg: "recipient" },
});
});
diff --git a/shared/skill-burger/draft.ts b/shared/skill-burger/draft.ts
index 9e624dcb1b..859e9fefcd 100644
--- a/shared/skill-burger/draft.ts
+++ b/shared/skill-burger/draft.ts
@@ -52,10 +52,25 @@ function parsedOutput(layer: BurgerLayer): unknown {
if (!layer.output) return undefined;
let value: unknown;
try { value = JSON.parse(layer.output); } catch { return layer.output; }
- // One tool of a multi-app call answers in its own slot of the shared result.
+ if (layer.kind !== "app" || layer.replay !== "exact") return value;
+ const frame = isRecord(value) && isRecord(value.result) ? value.result : value;
+ const content = Array.isArray(frame) ? frame : isRecord(frame) && Array.isArray(frame.content) ? frame.content : undefined;
+ const part = content?.find((one) => isRecord(one) && one.type === "text" && "text" in one);
+ if (isRecord(part)) {
+ value = part.text;
+ if (typeof value === "string") {
+ try { value = JSON.parse(value); } catch { return value; }
+ }
+ }
+ if (layer.tool.includes(".")) return value;
const index = /#(\d+)$/.exec(layer.id)?.[1];
const results = isRecord(value) && isRecord(value.data) && Array.isArray(value.data.results) ? value.data.results : undefined;
- return index !== undefined && results ? results[Number(index)] : value;
+ if (results) {
+ const result = index !== undefined ? results[Number(index)] : results.find((one) => isRecord(one) && one.tool_slug === layer.tool) ?? (results.length === 1 ? results[0] : undefined);
+ const response = isRecord(result) && isRecord(result.response) ? result.response : undefined;
+ return response && "data" in response ? response.data : value;
+ }
+ return isRecord(value) && "data" in value ? value.data : value;
}
/** Where `target` sits in `value`, as a dotted path; "" for the value itself. */