@upstash/workflow 0.2.4 → 0.2.5-agents
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/agents.d.mts +5 -0
- package/agents.d.ts +5 -0
- package/agents.js +245 -0
- package/agents.mjs +12 -0
- package/astro.d.mts +4 -1
- package/astro.d.ts +4 -1
- package/astro.js +216 -0
- package/astro.mjs +2 -1
- package/chunk-PU5J4TNC.mjs +251 -0
- package/{chunk-ETDFMXER.mjs → chunk-RFX5YRRT.mjs} +9 -34
- package/cloudflare.d.mts +4 -1
- package/cloudflare.d.ts +4 -1
- package/cloudflare.js +216 -0
- package/cloudflare.mjs +2 -1
- package/express.d.mts +4 -1
- package/express.d.ts +4 -1
- package/express.js +216 -0
- package/express.mjs +5 -3
- package/h3.d.mts +4 -1
- package/h3.d.ts +4 -1
- package/h3.js +216 -0
- package/h3.mjs +2 -1
- package/hono.d.mts +4 -1
- package/hono.d.ts +4 -1
- package/hono.js +216 -0
- package/hono.mjs +2 -1
- package/index.d.mts +5 -2
- package/index.d.ts +5 -2
- package/index.js +216 -0
- package/index.mjs +2 -1
- package/nextjs.d.mts +4 -1
- package/nextjs.d.ts +4 -1
- package/nextjs.js +216 -0
- package/nextjs.mjs +2 -1
- package/package.json +1 -1
- package/solidjs.d.mts +4 -1
- package/solidjs.d.ts +4 -1
- package/solidjs.js +216 -0
- package/solidjs.mjs +2 -1
- package/svelte.d.mts +4 -1
- package/svelte.d.ts +4 -1
- package/svelte.js +216 -0
- package/svelte.mjs +2 -1
- package/{types-Bt4-paRy.d.mts → types-BEyIoCRe.d.mts} +76 -1
- package/{types-Bt4-paRy.d.ts → types-BEyIoCRe.d.ts} +76 -1
package/hono.js
CHANGED
|
@@ -1632,6 +1632,216 @@ var WorkflowApi = class extends BaseWorkflowApi {
|
|
|
1632
1632
|
}
|
|
1633
1633
|
};
|
|
1634
1634
|
|
|
1635
|
+
// src/agents/adapters.ts
|
|
1636
|
+
var import_openai2 = require("@ai-sdk/openai");
|
|
1637
|
+
var import_ai = require("ai");
|
|
1638
|
+
var AGENT_NAME_HEADER = "upstash-agent-name";
|
|
1639
|
+
var createWorkflowOpenAI = (context) => {
|
|
1640
|
+
return (0, import_openai2.createOpenAI)({
|
|
1641
|
+
compatibility: "strict",
|
|
1642
|
+
fetch: async (input, init) => {
|
|
1643
|
+
try {
|
|
1644
|
+
const headers = init?.headers ? Object.fromEntries(new Headers(init.headers).entries()) : {};
|
|
1645
|
+
const body = init?.body ? JSON.parse(init.body) : void 0;
|
|
1646
|
+
const agentName = headers[AGENT_NAME_HEADER];
|
|
1647
|
+
const stepName = agentName ? `Call Agent ${agentName}` : "Call Agent";
|
|
1648
|
+
const responseInfo = await context.call(stepName, {
|
|
1649
|
+
url: input.toString(),
|
|
1650
|
+
method: init?.method,
|
|
1651
|
+
headers,
|
|
1652
|
+
body
|
|
1653
|
+
});
|
|
1654
|
+
const responseHeaders = new Headers(
|
|
1655
|
+
Object.entries(responseInfo.header).reduce(
|
|
1656
|
+
(acc, [key, values]) => {
|
|
1657
|
+
acc[key] = values.join(", ");
|
|
1658
|
+
return acc;
|
|
1659
|
+
},
|
|
1660
|
+
{}
|
|
1661
|
+
)
|
|
1662
|
+
);
|
|
1663
|
+
return new Response(JSON.stringify(responseInfo.body), {
|
|
1664
|
+
status: responseInfo.status,
|
|
1665
|
+
headers: responseHeaders
|
|
1666
|
+
});
|
|
1667
|
+
} catch (error) {
|
|
1668
|
+
if (error instanceof Error && error.name === "WorkflowAbort") {
|
|
1669
|
+
throw error;
|
|
1670
|
+
} else {
|
|
1671
|
+
console.error("Error in fetch implementation:", error);
|
|
1672
|
+
throw error;
|
|
1673
|
+
}
|
|
1674
|
+
}
|
|
1675
|
+
}
|
|
1676
|
+
});
|
|
1677
|
+
};
|
|
1678
|
+
var wrapTools = ({
|
|
1679
|
+
context,
|
|
1680
|
+
tools
|
|
1681
|
+
}) => {
|
|
1682
|
+
return Object.fromEntries(
|
|
1683
|
+
Object.entries(tools).map((toolInfo) => {
|
|
1684
|
+
const [toolName, tool3] = toolInfo;
|
|
1685
|
+
const aiSDKTool = convertToAISDKTool(tool3);
|
|
1686
|
+
const execute = aiSDKTool.execute;
|
|
1687
|
+
if (execute) {
|
|
1688
|
+
const wrappedExecute = (...params) => {
|
|
1689
|
+
return context.run(`Run tool ${toolName}`, () => execute(...params));
|
|
1690
|
+
};
|
|
1691
|
+
aiSDKTool.execute = wrappedExecute;
|
|
1692
|
+
}
|
|
1693
|
+
return [toolName, aiSDKTool];
|
|
1694
|
+
})
|
|
1695
|
+
);
|
|
1696
|
+
};
|
|
1697
|
+
var convertToAISDKTool = (tool3) => {
|
|
1698
|
+
const isLangchainTool = "invoke" in tool3;
|
|
1699
|
+
return isLangchainTool ? convertLangchainTool(tool3) : tool3;
|
|
1700
|
+
};
|
|
1701
|
+
var convertLangchainTool = (langchainTool) => {
|
|
1702
|
+
return (0, import_ai.tool)({
|
|
1703
|
+
description: langchainTool.description,
|
|
1704
|
+
parameters: langchainTool.schema,
|
|
1705
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1706
|
+
execute: async (param) => langchainTool.invoke(param)
|
|
1707
|
+
});
|
|
1708
|
+
};
|
|
1709
|
+
|
|
1710
|
+
// src/agents/agent.ts
|
|
1711
|
+
var import_zod = require("zod");
|
|
1712
|
+
var import_ai2 = require("ai");
|
|
1713
|
+
var Agent = class {
|
|
1714
|
+
name;
|
|
1715
|
+
tools;
|
|
1716
|
+
maxSteps;
|
|
1717
|
+
background;
|
|
1718
|
+
model;
|
|
1719
|
+
constructor({ tools, maxSteps, background, name, model }) {
|
|
1720
|
+
this.name = name;
|
|
1721
|
+
this.tools = tools ?? {};
|
|
1722
|
+
this.maxSteps = maxSteps;
|
|
1723
|
+
this.background = background;
|
|
1724
|
+
this.model = model;
|
|
1725
|
+
}
|
|
1726
|
+
async call({ prompt }) {
|
|
1727
|
+
try {
|
|
1728
|
+
return await (0, import_ai2.generateText)({
|
|
1729
|
+
model: this.model,
|
|
1730
|
+
tools: this.tools,
|
|
1731
|
+
maxSteps: this.maxSteps,
|
|
1732
|
+
system: this.background,
|
|
1733
|
+
prompt,
|
|
1734
|
+
headers: {
|
|
1735
|
+
[AGENT_NAME_HEADER]: this.name
|
|
1736
|
+
}
|
|
1737
|
+
});
|
|
1738
|
+
} catch (error) {
|
|
1739
|
+
if (error instanceof import_ai2.ToolExecutionError) {
|
|
1740
|
+
if (error.cause instanceof Error && error.cause.name === "WorkflowAbort") {
|
|
1741
|
+
throw error.cause;
|
|
1742
|
+
} else if (error.cause instanceof import_ai2.ToolExecutionError && error.cause.cause instanceof Error && error.cause.cause.name === "WorkflowAbort") {
|
|
1743
|
+
throw error.cause.cause;
|
|
1744
|
+
} else {
|
|
1745
|
+
throw error;
|
|
1746
|
+
}
|
|
1747
|
+
} else {
|
|
1748
|
+
throw error;
|
|
1749
|
+
}
|
|
1750
|
+
}
|
|
1751
|
+
}
|
|
1752
|
+
asTool() {
|
|
1753
|
+
const toolDescriptions = Object.values(this.tools).map((tool3) => tool3.description).join("\n");
|
|
1754
|
+
return (0, import_ai2.tool)({
|
|
1755
|
+
parameters: import_zod.z.object({ prompt: import_zod.z.string() }),
|
|
1756
|
+
execute: async ({ prompt }) => {
|
|
1757
|
+
return await this.call({ prompt });
|
|
1758
|
+
},
|
|
1759
|
+
description: `An AI Agent with the following background: ${this.background}Has access to the following tools: ${toolDescriptions}`
|
|
1760
|
+
});
|
|
1761
|
+
}
|
|
1762
|
+
};
|
|
1763
|
+
var MANAGER_AGENT_PROMPT = `You are an AI agent who orchestrates other AI Agents.
|
|
1764
|
+
These other agents have tools available to them.
|
|
1765
|
+
Given a prompt, utilize these agents to address requests.
|
|
1766
|
+
Don't always call all the agents provided to you at the same time. You can call one and use it's response to call another.
|
|
1767
|
+
`;
|
|
1768
|
+
var ManagerAgent = class extends Agent {
|
|
1769
|
+
agents;
|
|
1770
|
+
constructor({
|
|
1771
|
+
maxSteps,
|
|
1772
|
+
background = MANAGER_AGENT_PROMPT,
|
|
1773
|
+
agents,
|
|
1774
|
+
model,
|
|
1775
|
+
name = "manager llm"
|
|
1776
|
+
}) {
|
|
1777
|
+
super({
|
|
1778
|
+
background,
|
|
1779
|
+
maxSteps,
|
|
1780
|
+
tools: Object.fromEntries(agents.map((agent) => [agent.name, agent.asTool()])),
|
|
1781
|
+
name,
|
|
1782
|
+
model
|
|
1783
|
+
});
|
|
1784
|
+
this.agents = agents;
|
|
1785
|
+
}
|
|
1786
|
+
};
|
|
1787
|
+
|
|
1788
|
+
// src/agents/task.ts
|
|
1789
|
+
var Task = class {
|
|
1790
|
+
context;
|
|
1791
|
+
taskParameters;
|
|
1792
|
+
constructor({
|
|
1793
|
+
context,
|
|
1794
|
+
taskParameters
|
|
1795
|
+
}) {
|
|
1796
|
+
this.context = context;
|
|
1797
|
+
this.taskParameters = taskParameters;
|
|
1798
|
+
}
|
|
1799
|
+
async run() {
|
|
1800
|
+
const { prompt, ...otherParams } = this.taskParameters;
|
|
1801
|
+
const safePrompt = await this.context.run("Get Prompt", () => prompt);
|
|
1802
|
+
if ("agent" in otherParams) {
|
|
1803
|
+
const agent = otherParams.agent;
|
|
1804
|
+
const result = await agent.call({
|
|
1805
|
+
prompt: safePrompt
|
|
1806
|
+
});
|
|
1807
|
+
return { text: result.text };
|
|
1808
|
+
} else {
|
|
1809
|
+
const { agents, maxSteps, model, background } = otherParams;
|
|
1810
|
+
const managerAgent = new ManagerAgent({
|
|
1811
|
+
model,
|
|
1812
|
+
maxSteps,
|
|
1813
|
+
agents,
|
|
1814
|
+
name: "Manager LLM",
|
|
1815
|
+
background
|
|
1816
|
+
});
|
|
1817
|
+
const result = await managerAgent.call({ prompt: safePrompt });
|
|
1818
|
+
return { text: result.text };
|
|
1819
|
+
}
|
|
1820
|
+
}
|
|
1821
|
+
};
|
|
1822
|
+
|
|
1823
|
+
// src/agents/index.ts
|
|
1824
|
+
var WorkflowAgents = class {
|
|
1825
|
+
context;
|
|
1826
|
+
constructor({ context }) {
|
|
1827
|
+
this.context = context;
|
|
1828
|
+
}
|
|
1829
|
+
agent(params) {
|
|
1830
|
+
const wrappedTools = wrapTools({ context: this.context, tools: params.tools });
|
|
1831
|
+
return new Agent({
|
|
1832
|
+
...params,
|
|
1833
|
+
tools: wrappedTools
|
|
1834
|
+
});
|
|
1835
|
+
}
|
|
1836
|
+
task(taskParameters) {
|
|
1837
|
+
return new Task({ context: this.context, taskParameters });
|
|
1838
|
+
}
|
|
1839
|
+
openai(...params) {
|
|
1840
|
+
const openai2 = createWorkflowOpenAI(this.context);
|
|
1841
|
+
return openai2(...params);
|
|
1842
|
+
}
|
|
1843
|
+
};
|
|
1844
|
+
|
|
1635
1845
|
// src/context/context.ts
|
|
1636
1846
|
var WorkflowContext = class {
|
|
1637
1847
|
executor;
|
|
@@ -2017,6 +2227,11 @@ var WorkflowContext = class {
|
|
|
2017
2227
|
context: this
|
|
2018
2228
|
});
|
|
2019
2229
|
}
|
|
2230
|
+
get agents() {
|
|
2231
|
+
return new WorkflowAgents({
|
|
2232
|
+
context: this
|
|
2233
|
+
});
|
|
2234
|
+
}
|
|
2020
2235
|
};
|
|
2021
2236
|
|
|
2022
2237
|
// src/logger.ts
|
|
@@ -2221,6 +2436,7 @@ var checkIfLastOneIsDuplicate = async (steps, debug) => {
|
|
|
2221
2436
|
if (step.stepId === lastStepId && step.targetStep === lastTargetStepId) {
|
|
2222
2437
|
const message = `Upstash Workflow: The step '${step.stepName}' with id '${step.stepId}' has run twice during workflow execution. Rest of the workflow will continue running as usual.`;
|
|
2223
2438
|
await debug?.log("WARN", "RESPONSE_DEFAULT", message);
|
|
2439
|
+
console.log(steps);
|
|
2224
2440
|
console.warn(message);
|
|
2225
2441
|
return true;
|
|
2226
2442
|
}
|
package/hono.mjs
CHANGED
package/index.d.mts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
import { R as RouteFunction, W as WorkflowServeOptions, N as NotifyResponse, a as Waiter, S as Step } from './types-
|
|
2
|
-
export { A as AsyncStepFunction, C as CallResponse, q as CallSettings, D as Duration, k as FailureFunctionPayload, F as FinishCondition, H as HeaderParams, L as LogLevel, o as NotifyStepResponse, P as ParallelCallState, j as PublicServeOptions, g as RawStep, l as RequiredExceptFields, i as StepFunction, f as StepType, e as StepTypes, h as SyncStepFunction, T as Telemetry, p as WaitEventOptions, m as WaitRequest, n as WaitStepResponse, c as WorkflowClient, b as WorkflowContext, s as WorkflowLogger, r as WorkflowLoggerOptions, d as WorkflowReceiver } from './types-
|
|
1
|
+
import { R as RouteFunction, W as WorkflowServeOptions, N as NotifyResponse, a as Waiter, S as Step } from './types-BEyIoCRe.mjs';
|
|
2
|
+
export { A as AsyncStepFunction, C as CallResponse, q as CallSettings, D as Duration, k as FailureFunctionPayload, F as FinishCondition, H as HeaderParams, L as LogLevel, o as NotifyStepResponse, P as ParallelCallState, j as PublicServeOptions, g as RawStep, l as RequiredExceptFields, i as StepFunction, f as StepType, e as StepTypes, h as SyncStepFunction, T as Telemetry, p as WaitEventOptions, m as WaitRequest, n as WaitStepResponse, c as WorkflowClient, b as WorkflowContext, s as WorkflowLogger, r as WorkflowLoggerOptions, d as WorkflowReceiver } from './types-BEyIoCRe.mjs';
|
|
3
3
|
import { Client as Client$1, QstashError } from '@upstash/qstash';
|
|
4
|
+
import 'ai';
|
|
5
|
+
import '@ai-sdk/openai';
|
|
6
|
+
import 'langchain/tools';
|
|
4
7
|
|
|
5
8
|
/**
|
|
6
9
|
* Creates an async method that handles incoming requests and runs the provided
|
package/index.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
import { R as RouteFunction, W as WorkflowServeOptions, N as NotifyResponse, a as Waiter, S as Step } from './types-
|
|
2
|
-
export { A as AsyncStepFunction, C as CallResponse, q as CallSettings, D as Duration, k as FailureFunctionPayload, F as FinishCondition, H as HeaderParams, L as LogLevel, o as NotifyStepResponse, P as ParallelCallState, j as PublicServeOptions, g as RawStep, l as RequiredExceptFields, i as StepFunction, f as StepType, e as StepTypes, h as SyncStepFunction, T as Telemetry, p as WaitEventOptions, m as WaitRequest, n as WaitStepResponse, c as WorkflowClient, b as WorkflowContext, s as WorkflowLogger, r as WorkflowLoggerOptions, d as WorkflowReceiver } from './types-
|
|
1
|
+
import { R as RouteFunction, W as WorkflowServeOptions, N as NotifyResponse, a as Waiter, S as Step } from './types-BEyIoCRe.js';
|
|
2
|
+
export { A as AsyncStepFunction, C as CallResponse, q as CallSettings, D as Duration, k as FailureFunctionPayload, F as FinishCondition, H as HeaderParams, L as LogLevel, o as NotifyStepResponse, P as ParallelCallState, j as PublicServeOptions, g as RawStep, l as RequiredExceptFields, i as StepFunction, f as StepType, e as StepTypes, h as SyncStepFunction, T as Telemetry, p as WaitEventOptions, m as WaitRequest, n as WaitStepResponse, c as WorkflowClient, b as WorkflowContext, s as WorkflowLogger, r as WorkflowLoggerOptions, d as WorkflowReceiver } from './types-BEyIoCRe.js';
|
|
3
3
|
import { Client as Client$1, QstashError } from '@upstash/qstash';
|
|
4
|
+
import 'ai';
|
|
5
|
+
import '@ai-sdk/openai';
|
|
6
|
+
import 'langchain/tools';
|
|
4
7
|
|
|
5
8
|
/**
|
|
6
9
|
* Creates an async method that handles incoming requests and runs the provided
|
package/index.js
CHANGED
|
@@ -1645,6 +1645,216 @@ var WorkflowApi = class extends BaseWorkflowApi {
|
|
|
1645
1645
|
}
|
|
1646
1646
|
};
|
|
1647
1647
|
|
|
1648
|
+
// src/agents/adapters.ts
|
|
1649
|
+
var import_openai2 = require("@ai-sdk/openai");
|
|
1650
|
+
var import_ai = require("ai");
|
|
1651
|
+
var AGENT_NAME_HEADER = "upstash-agent-name";
|
|
1652
|
+
var createWorkflowOpenAI = (context) => {
|
|
1653
|
+
return (0, import_openai2.createOpenAI)({
|
|
1654
|
+
compatibility: "strict",
|
|
1655
|
+
fetch: async (input, init) => {
|
|
1656
|
+
try {
|
|
1657
|
+
const headers = init?.headers ? Object.fromEntries(new Headers(init.headers).entries()) : {};
|
|
1658
|
+
const body = init?.body ? JSON.parse(init.body) : void 0;
|
|
1659
|
+
const agentName = headers[AGENT_NAME_HEADER];
|
|
1660
|
+
const stepName = agentName ? `Call Agent ${agentName}` : "Call Agent";
|
|
1661
|
+
const responseInfo = await context.call(stepName, {
|
|
1662
|
+
url: input.toString(),
|
|
1663
|
+
method: init?.method,
|
|
1664
|
+
headers,
|
|
1665
|
+
body
|
|
1666
|
+
});
|
|
1667
|
+
const responseHeaders = new Headers(
|
|
1668
|
+
Object.entries(responseInfo.header).reduce(
|
|
1669
|
+
(acc, [key, values]) => {
|
|
1670
|
+
acc[key] = values.join(", ");
|
|
1671
|
+
return acc;
|
|
1672
|
+
},
|
|
1673
|
+
{}
|
|
1674
|
+
)
|
|
1675
|
+
);
|
|
1676
|
+
return new Response(JSON.stringify(responseInfo.body), {
|
|
1677
|
+
status: responseInfo.status,
|
|
1678
|
+
headers: responseHeaders
|
|
1679
|
+
});
|
|
1680
|
+
} catch (error) {
|
|
1681
|
+
if (error instanceof Error && error.name === "WorkflowAbort") {
|
|
1682
|
+
throw error;
|
|
1683
|
+
} else {
|
|
1684
|
+
console.error("Error in fetch implementation:", error);
|
|
1685
|
+
throw error;
|
|
1686
|
+
}
|
|
1687
|
+
}
|
|
1688
|
+
}
|
|
1689
|
+
});
|
|
1690
|
+
};
|
|
1691
|
+
var wrapTools = ({
|
|
1692
|
+
context,
|
|
1693
|
+
tools
|
|
1694
|
+
}) => {
|
|
1695
|
+
return Object.fromEntries(
|
|
1696
|
+
Object.entries(tools).map((toolInfo) => {
|
|
1697
|
+
const [toolName, tool3] = toolInfo;
|
|
1698
|
+
const aiSDKTool = convertToAISDKTool(tool3);
|
|
1699
|
+
const execute = aiSDKTool.execute;
|
|
1700
|
+
if (execute) {
|
|
1701
|
+
const wrappedExecute = (...params) => {
|
|
1702
|
+
return context.run(`Run tool ${toolName}`, () => execute(...params));
|
|
1703
|
+
};
|
|
1704
|
+
aiSDKTool.execute = wrappedExecute;
|
|
1705
|
+
}
|
|
1706
|
+
return [toolName, aiSDKTool];
|
|
1707
|
+
})
|
|
1708
|
+
);
|
|
1709
|
+
};
|
|
1710
|
+
var convertToAISDKTool = (tool3) => {
|
|
1711
|
+
const isLangchainTool = "invoke" in tool3;
|
|
1712
|
+
return isLangchainTool ? convertLangchainTool(tool3) : tool3;
|
|
1713
|
+
};
|
|
1714
|
+
var convertLangchainTool = (langchainTool) => {
|
|
1715
|
+
return (0, import_ai.tool)({
|
|
1716
|
+
description: langchainTool.description,
|
|
1717
|
+
parameters: langchainTool.schema,
|
|
1718
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1719
|
+
execute: async (param) => langchainTool.invoke(param)
|
|
1720
|
+
});
|
|
1721
|
+
};
|
|
1722
|
+
|
|
1723
|
+
// src/agents/agent.ts
|
|
1724
|
+
var import_zod = require("zod");
|
|
1725
|
+
var import_ai2 = require("ai");
|
|
1726
|
+
var Agent = class {
|
|
1727
|
+
name;
|
|
1728
|
+
tools;
|
|
1729
|
+
maxSteps;
|
|
1730
|
+
background;
|
|
1731
|
+
model;
|
|
1732
|
+
constructor({ tools, maxSteps, background, name, model }) {
|
|
1733
|
+
this.name = name;
|
|
1734
|
+
this.tools = tools ?? {};
|
|
1735
|
+
this.maxSteps = maxSteps;
|
|
1736
|
+
this.background = background;
|
|
1737
|
+
this.model = model;
|
|
1738
|
+
}
|
|
1739
|
+
async call({ prompt }) {
|
|
1740
|
+
try {
|
|
1741
|
+
return await (0, import_ai2.generateText)({
|
|
1742
|
+
model: this.model,
|
|
1743
|
+
tools: this.tools,
|
|
1744
|
+
maxSteps: this.maxSteps,
|
|
1745
|
+
system: this.background,
|
|
1746
|
+
prompt,
|
|
1747
|
+
headers: {
|
|
1748
|
+
[AGENT_NAME_HEADER]: this.name
|
|
1749
|
+
}
|
|
1750
|
+
});
|
|
1751
|
+
} catch (error) {
|
|
1752
|
+
if (error instanceof import_ai2.ToolExecutionError) {
|
|
1753
|
+
if (error.cause instanceof Error && error.cause.name === "WorkflowAbort") {
|
|
1754
|
+
throw error.cause;
|
|
1755
|
+
} else if (error.cause instanceof import_ai2.ToolExecutionError && error.cause.cause instanceof Error && error.cause.cause.name === "WorkflowAbort") {
|
|
1756
|
+
throw error.cause.cause;
|
|
1757
|
+
} else {
|
|
1758
|
+
throw error;
|
|
1759
|
+
}
|
|
1760
|
+
} else {
|
|
1761
|
+
throw error;
|
|
1762
|
+
}
|
|
1763
|
+
}
|
|
1764
|
+
}
|
|
1765
|
+
asTool() {
|
|
1766
|
+
const toolDescriptions = Object.values(this.tools).map((tool3) => tool3.description).join("\n");
|
|
1767
|
+
return (0, import_ai2.tool)({
|
|
1768
|
+
parameters: import_zod.z.object({ prompt: import_zod.z.string() }),
|
|
1769
|
+
execute: async ({ prompt }) => {
|
|
1770
|
+
return await this.call({ prompt });
|
|
1771
|
+
},
|
|
1772
|
+
description: `An AI Agent with the following background: ${this.background}Has access to the following tools: ${toolDescriptions}`
|
|
1773
|
+
});
|
|
1774
|
+
}
|
|
1775
|
+
};
|
|
1776
|
+
var MANAGER_AGENT_PROMPT = `You are an AI agent who orchestrates other AI Agents.
|
|
1777
|
+
These other agents have tools available to them.
|
|
1778
|
+
Given a prompt, utilize these agents to address requests.
|
|
1779
|
+
Don't always call all the agents provided to you at the same time. You can call one and use it's response to call another.
|
|
1780
|
+
`;
|
|
1781
|
+
var ManagerAgent = class extends Agent {
|
|
1782
|
+
agents;
|
|
1783
|
+
constructor({
|
|
1784
|
+
maxSteps,
|
|
1785
|
+
background = MANAGER_AGENT_PROMPT,
|
|
1786
|
+
agents,
|
|
1787
|
+
model,
|
|
1788
|
+
name = "manager llm"
|
|
1789
|
+
}) {
|
|
1790
|
+
super({
|
|
1791
|
+
background,
|
|
1792
|
+
maxSteps,
|
|
1793
|
+
tools: Object.fromEntries(agents.map((agent) => [agent.name, agent.asTool()])),
|
|
1794
|
+
name,
|
|
1795
|
+
model
|
|
1796
|
+
});
|
|
1797
|
+
this.agents = agents;
|
|
1798
|
+
}
|
|
1799
|
+
};
|
|
1800
|
+
|
|
1801
|
+
// src/agents/task.ts
|
|
1802
|
+
var Task = class {
|
|
1803
|
+
context;
|
|
1804
|
+
taskParameters;
|
|
1805
|
+
constructor({
|
|
1806
|
+
context,
|
|
1807
|
+
taskParameters
|
|
1808
|
+
}) {
|
|
1809
|
+
this.context = context;
|
|
1810
|
+
this.taskParameters = taskParameters;
|
|
1811
|
+
}
|
|
1812
|
+
async run() {
|
|
1813
|
+
const { prompt, ...otherParams } = this.taskParameters;
|
|
1814
|
+
const safePrompt = await this.context.run("Get Prompt", () => prompt);
|
|
1815
|
+
if ("agent" in otherParams) {
|
|
1816
|
+
const agent = otherParams.agent;
|
|
1817
|
+
const result = await agent.call({
|
|
1818
|
+
prompt: safePrompt
|
|
1819
|
+
});
|
|
1820
|
+
return { text: result.text };
|
|
1821
|
+
} else {
|
|
1822
|
+
const { agents, maxSteps, model, background } = otherParams;
|
|
1823
|
+
const managerAgent = new ManagerAgent({
|
|
1824
|
+
model,
|
|
1825
|
+
maxSteps,
|
|
1826
|
+
agents,
|
|
1827
|
+
name: "Manager LLM",
|
|
1828
|
+
background
|
|
1829
|
+
});
|
|
1830
|
+
const result = await managerAgent.call({ prompt: safePrompt });
|
|
1831
|
+
return { text: result.text };
|
|
1832
|
+
}
|
|
1833
|
+
}
|
|
1834
|
+
};
|
|
1835
|
+
|
|
1836
|
+
// src/agents/index.ts
|
|
1837
|
+
var WorkflowAgents = class {
|
|
1838
|
+
context;
|
|
1839
|
+
constructor({ context }) {
|
|
1840
|
+
this.context = context;
|
|
1841
|
+
}
|
|
1842
|
+
agent(params) {
|
|
1843
|
+
const wrappedTools = wrapTools({ context: this.context, tools: params.tools });
|
|
1844
|
+
return new Agent({
|
|
1845
|
+
...params,
|
|
1846
|
+
tools: wrappedTools
|
|
1847
|
+
});
|
|
1848
|
+
}
|
|
1849
|
+
task(taskParameters) {
|
|
1850
|
+
return new Task({ context: this.context, taskParameters });
|
|
1851
|
+
}
|
|
1852
|
+
openai(...params) {
|
|
1853
|
+
const openai2 = createWorkflowOpenAI(this.context);
|
|
1854
|
+
return openai2(...params);
|
|
1855
|
+
}
|
|
1856
|
+
};
|
|
1857
|
+
|
|
1648
1858
|
// src/context/context.ts
|
|
1649
1859
|
var WorkflowContext = class {
|
|
1650
1860
|
executor;
|
|
@@ -2030,6 +2240,11 @@ var WorkflowContext = class {
|
|
|
2030
2240
|
context: this
|
|
2031
2241
|
});
|
|
2032
2242
|
}
|
|
2243
|
+
get agents() {
|
|
2244
|
+
return new WorkflowAgents({
|
|
2245
|
+
context: this
|
|
2246
|
+
});
|
|
2247
|
+
}
|
|
2033
2248
|
};
|
|
2034
2249
|
|
|
2035
2250
|
// src/logger.ts
|
|
@@ -2234,6 +2449,7 @@ var checkIfLastOneIsDuplicate = async (steps, debug) => {
|
|
|
2234
2449
|
if (step.stepId === lastStepId && step.targetStep === lastTargetStepId) {
|
|
2235
2450
|
const message = `Upstash Workflow: The step '${step.stepName}' with id '${step.stepId}' has run twice during workflow execution. Rest of the workflow will continue running as usual.`;
|
|
2236
2451
|
await debug?.log("WARN", "RESPONSE_DEFAULT", message);
|
|
2452
|
+
console.log(steps);
|
|
2237
2453
|
console.warn(message);
|
|
2238
2454
|
return true;
|
|
2239
2455
|
}
|
package/index.mjs
CHANGED
package/nextjs.d.mts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { NextApiHandler } from 'next';
|
|
2
|
-
import { R as RouteFunction, j as PublicServeOptions } from './types-
|
|
2
|
+
import { R as RouteFunction, j as PublicServeOptions } from './types-BEyIoCRe.mjs';
|
|
3
3
|
import '@upstash/qstash';
|
|
4
|
+
import 'ai';
|
|
5
|
+
import '@ai-sdk/openai';
|
|
6
|
+
import 'langchain/tools';
|
|
4
7
|
|
|
5
8
|
/**
|
|
6
9
|
* Serve method to serve a Upstash Workflow in a Nextjs project
|
package/nextjs.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { NextApiHandler } from 'next';
|
|
2
|
-
import { R as RouteFunction, j as PublicServeOptions } from './types-
|
|
2
|
+
import { R as RouteFunction, j as PublicServeOptions } from './types-BEyIoCRe.js';
|
|
3
3
|
import '@upstash/qstash';
|
|
4
|
+
import 'ai';
|
|
5
|
+
import '@ai-sdk/openai';
|
|
6
|
+
import 'langchain/tools';
|
|
4
7
|
|
|
5
8
|
/**
|
|
6
9
|
* Serve method to serve a Upstash Workflow in a Nextjs project
|