@upstash/workflow 0.2.5-agents → 0.2.5-agents-3
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/astro.d.mts +1 -2
- package/astro.d.ts +1 -2
- package/astro.js +132 -58
- package/astro.mjs +1 -2
- package/{chunk-RFX5YRRT.mjs → chunk-42MM2EPQ.mjs} +342 -27
- package/cloudflare.d.mts +1 -2
- package/cloudflare.d.ts +1 -2
- package/cloudflare.js +132 -58
- package/cloudflare.mjs +1 -2
- package/express.d.mts +1 -2
- package/express.d.ts +1 -2
- package/express.js +132 -58
- package/express.mjs +3 -5
- package/h3.d.mts +1 -2
- package/h3.d.ts +1 -2
- package/h3.js +132 -58
- package/h3.mjs +1 -2
- package/hono.d.mts +1 -2
- package/hono.d.ts +1 -2
- package/hono.js +132 -58
- package/hono.mjs +1 -2
- package/index.d.mts +2 -3
- package/index.d.ts +2 -3
- package/index.js +132 -58
- package/index.mjs +1 -2
- package/nextjs.d.mts +1 -2
- package/nextjs.d.ts +1 -2
- package/nextjs.js +132 -58
- package/nextjs.mjs +1 -2
- package/package.json +1 -1
- package/solidjs.d.mts +1 -2
- package/solidjs.d.ts +1 -2
- package/solidjs.js +132 -58
- package/solidjs.mjs +1 -2
- package/svelte.d.mts +1 -2
- package/svelte.d.ts +1 -2
- package/svelte.js +132 -58
- package/svelte.mjs +1 -2
- package/{types-BEyIoCRe.d.mts → types-CalpUeFX.d.mts} +176 -13
- package/{types-BEyIoCRe.d.ts → types-CalpUeFX.d.ts} +176 -13
- package/agents.d.mts +0 -5
- package/agents.d.ts +0 -5
- package/agents.js +0 -245
- package/agents.mjs +0 -12
- package/chunk-PU5J4TNC.mjs +0 -251
package/index.js
CHANGED
|
@@ -839,29 +839,16 @@ var triggerWorkflowDelete = async (workflowContext, debug, cancel = false) => {
|
|
|
839
839
|
await debug?.log("SUBMIT", "SUBMIT_CLEANUP", {
|
|
840
840
|
deletedWorkflowRunId: workflowContext.workflowRunId
|
|
841
841
|
});
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
);
|
|
853
|
-
return { deleted: true };
|
|
854
|
-
} catch (error) {
|
|
855
|
-
if (error instanceof import_qstash3.QstashError && error.status === 404) {
|
|
856
|
-
await debug?.log("WARN", "SUBMIT_CLEANUP", {
|
|
857
|
-
message: `Failed to remove workflow run ${workflowContext.workflowRunId} as it doesn't exist.`,
|
|
858
|
-
name: error.name,
|
|
859
|
-
errorMessage: error.message
|
|
860
|
-
});
|
|
861
|
-
return { deleted: false };
|
|
862
|
-
}
|
|
863
|
-
throw error;
|
|
864
|
-
}
|
|
842
|
+
await workflowContext.qstashClient.http.request({
|
|
843
|
+
path: ["v2", "workflows", "runs", `${workflowContext.workflowRunId}?cancel=${cancel}`],
|
|
844
|
+
method: "DELETE",
|
|
845
|
+
parseResponseAsJson: false
|
|
846
|
+
});
|
|
847
|
+
await debug?.log(
|
|
848
|
+
"SUBMIT",
|
|
849
|
+
"SUBMIT_CLEANUP",
|
|
850
|
+
`workflow run ${workflowContext.workflowRunId} deleted.`
|
|
851
|
+
);
|
|
865
852
|
};
|
|
866
853
|
var recreateUserHeaders = (headers) => {
|
|
867
854
|
const filteredHeaders = new Headers();
|
|
@@ -1648,7 +1635,22 @@ var WorkflowApi = class extends BaseWorkflowApi {
|
|
|
1648
1635
|
// src/agents/adapters.ts
|
|
1649
1636
|
var import_openai2 = require("@ai-sdk/openai");
|
|
1650
1637
|
var import_ai = require("ai");
|
|
1638
|
+
|
|
1639
|
+
// src/agents/constants.ts
|
|
1651
1640
|
var AGENT_NAME_HEADER = "upstash-agent-name";
|
|
1641
|
+
var MANAGER_AGENT_PROMPT = `You are an agent orchestrating other AI Agents.
|
|
1642
|
+
|
|
1643
|
+
These other agents have tools available to them.
|
|
1644
|
+
|
|
1645
|
+
Given a prompt, utilize these agents to address requests.
|
|
1646
|
+
|
|
1647
|
+
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.
|
|
1648
|
+
|
|
1649
|
+
Avoid calling the same agent twice in one turn. Instead, prefer to call it once but provide everything
|
|
1650
|
+
you need from that agent.
|
|
1651
|
+
`;
|
|
1652
|
+
|
|
1653
|
+
// src/agents/adapters.ts
|
|
1652
1654
|
var createWorkflowOpenAI = (context) => {
|
|
1653
1655
|
return (0, import_openai2.createOpenAI)({
|
|
1654
1656
|
compatibility: "strict",
|
|
@@ -1715,30 +1717,49 @@ var convertLangchainTool = (langchainTool) => {
|
|
|
1715
1717
|
return (0, import_ai.tool)({
|
|
1716
1718
|
description: langchainTool.description,
|
|
1717
1719
|
parameters: langchainTool.schema,
|
|
1718
|
-
|
|
1719
|
-
execute: async (param) => langchainTool.invoke(param)
|
|
1720
|
+
execute: async (...param) => langchainTool.invoke(...param)
|
|
1720
1721
|
});
|
|
1721
1722
|
};
|
|
1722
1723
|
|
|
1723
1724
|
// src/agents/agent.ts
|
|
1724
1725
|
var import_zod = require("zod");
|
|
1725
1726
|
var import_ai2 = require("ai");
|
|
1727
|
+
|
|
1728
|
+
// src/serve/utils.ts
|
|
1729
|
+
var isDisabledWorkflowContext = (context) => {
|
|
1730
|
+
return "disabled" in context;
|
|
1731
|
+
};
|
|
1732
|
+
|
|
1733
|
+
// src/agents/agent.ts
|
|
1726
1734
|
var Agent = class {
|
|
1727
1735
|
name;
|
|
1728
1736
|
tools;
|
|
1729
1737
|
maxSteps;
|
|
1730
1738
|
background;
|
|
1731
1739
|
model;
|
|
1732
|
-
|
|
1740
|
+
temparature;
|
|
1741
|
+
context;
|
|
1742
|
+
constructor({ tools, maxSteps, background, name, model, temparature = 0.1 }, context) {
|
|
1733
1743
|
this.name = name;
|
|
1734
1744
|
this.tools = tools ?? {};
|
|
1735
1745
|
this.maxSteps = maxSteps;
|
|
1736
1746
|
this.background = background;
|
|
1737
1747
|
this.model = model;
|
|
1748
|
+
this.temparature = temparature;
|
|
1749
|
+
this.context = context;
|
|
1738
1750
|
}
|
|
1751
|
+
/**
|
|
1752
|
+
* Trigger the agent by passing a prompt
|
|
1753
|
+
*
|
|
1754
|
+
* @param prompt task to assign to the agent
|
|
1755
|
+
* @returns Response as `{ text: string }`
|
|
1756
|
+
*/
|
|
1739
1757
|
async call({ prompt }) {
|
|
1740
1758
|
try {
|
|
1741
|
-
|
|
1759
|
+
if (isDisabledWorkflowContext(this.context)) {
|
|
1760
|
+
await this.context.sleep("abort", 0);
|
|
1761
|
+
}
|
|
1762
|
+
const result = await (0, import_ai2.generateText)({
|
|
1742
1763
|
model: this.model,
|
|
1743
1764
|
tools: this.tools,
|
|
1744
1765
|
maxSteps: this.maxSteps,
|
|
@@ -1746,8 +1767,10 @@ var Agent = class {
|
|
|
1746
1767
|
prompt,
|
|
1747
1768
|
headers: {
|
|
1748
1769
|
[AGENT_NAME_HEADER]: this.name
|
|
1749
|
-
}
|
|
1770
|
+
},
|
|
1771
|
+
temperature: this.temparature
|
|
1750
1772
|
});
|
|
1773
|
+
return { text: result.text };
|
|
1751
1774
|
} catch (error) {
|
|
1752
1775
|
if (error instanceof import_ai2.ToolExecutionError) {
|
|
1753
1776
|
if (error.cause instanceof Error && error.cause.name === "WorkflowAbort") {
|
|
@@ -1762,6 +1785,11 @@ var Agent = class {
|
|
|
1762
1785
|
}
|
|
1763
1786
|
}
|
|
1764
1787
|
}
|
|
1788
|
+
/**
|
|
1789
|
+
* Convert the agent to a tool which can be used by other agents.
|
|
1790
|
+
*
|
|
1791
|
+
* @returns the agent as a tool
|
|
1792
|
+
*/
|
|
1765
1793
|
asTool() {
|
|
1766
1794
|
const toolDescriptions = Object.values(this.tools).map((tool3) => tool3.description).join("\n");
|
|
1767
1795
|
return (0, import_ai2.tool)({
|
|
@@ -1773,27 +1801,37 @@ var Agent = class {
|
|
|
1773
1801
|
});
|
|
1774
1802
|
}
|
|
1775
1803
|
};
|
|
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
1804
|
var ManagerAgent = class extends Agent {
|
|
1782
1805
|
agents;
|
|
1806
|
+
/**
|
|
1807
|
+
* A manager agent which coordinates agents available to it to achieve a
|
|
1808
|
+
* given task
|
|
1809
|
+
*
|
|
1810
|
+
* @param name Name of the agent
|
|
1811
|
+
* @param background Background of the agent. If not passed, default will be used.
|
|
1812
|
+
* @param model LLM model to use
|
|
1813
|
+
* @param agents: List of agents available to the agent
|
|
1814
|
+
* @param maxSteps number of times the manager agent can call the LLM at most.
|
|
1815
|
+
* If the agent abruptly stops execution after calling other agents, you may
|
|
1816
|
+
* need to increase maxSteps
|
|
1817
|
+
*/
|
|
1783
1818
|
constructor({
|
|
1784
|
-
maxSteps,
|
|
1785
|
-
background = MANAGER_AGENT_PROMPT,
|
|
1786
1819
|
agents,
|
|
1820
|
+
background = MANAGER_AGENT_PROMPT,
|
|
1787
1821
|
model,
|
|
1822
|
+
maxSteps,
|
|
1788
1823
|
name = "manager llm"
|
|
1789
|
-
}) {
|
|
1790
|
-
super(
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1824
|
+
}, context) {
|
|
1825
|
+
super(
|
|
1826
|
+
{
|
|
1827
|
+
background,
|
|
1828
|
+
maxSteps,
|
|
1829
|
+
tools: Object.fromEntries(agents.map((agent) => [agent.name, agent.asTool()])),
|
|
1830
|
+
name,
|
|
1831
|
+
model
|
|
1832
|
+
},
|
|
1833
|
+
context
|
|
1834
|
+
);
|
|
1797
1835
|
this.agents = agents;
|
|
1798
1836
|
}
|
|
1799
1837
|
};
|
|
@@ -1809,25 +1847,32 @@ var Task = class {
|
|
|
1809
1847
|
this.context = context;
|
|
1810
1848
|
this.taskParameters = taskParameters;
|
|
1811
1849
|
}
|
|
1850
|
+
/**
|
|
1851
|
+
* Run the agents to complete the task
|
|
1852
|
+
*
|
|
1853
|
+
* @returns Result of the task as { text: string }
|
|
1854
|
+
*/
|
|
1812
1855
|
async run() {
|
|
1813
1856
|
const { prompt, ...otherParams } = this.taskParameters;
|
|
1814
|
-
const safePrompt = await this.context.run("Get Prompt", () => prompt);
|
|
1815
1857
|
if ("agent" in otherParams) {
|
|
1816
1858
|
const agent = otherParams.agent;
|
|
1817
1859
|
const result = await agent.call({
|
|
1818
|
-
prompt
|
|
1860
|
+
prompt
|
|
1819
1861
|
});
|
|
1820
1862
|
return { text: result.text };
|
|
1821
1863
|
} else {
|
|
1822
1864
|
const { agents, maxSteps, model, background } = otherParams;
|
|
1823
|
-
const managerAgent = new ManagerAgent(
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
|
|
1865
|
+
const managerAgent = new ManagerAgent(
|
|
1866
|
+
{
|
|
1867
|
+
model,
|
|
1868
|
+
maxSteps,
|
|
1869
|
+
agents,
|
|
1870
|
+
name: "Manager LLM",
|
|
1871
|
+
background
|
|
1872
|
+
},
|
|
1873
|
+
this.context
|
|
1874
|
+
);
|
|
1875
|
+
const result = await managerAgent.call({ prompt });
|
|
1831
1876
|
return { text: result.text };
|
|
1832
1877
|
}
|
|
1833
1878
|
}
|
|
@@ -1839,16 +1884,45 @@ var WorkflowAgents = class {
|
|
|
1839
1884
|
constructor({ context }) {
|
|
1840
1885
|
this.context = context;
|
|
1841
1886
|
}
|
|
1887
|
+
/**
|
|
1888
|
+
* Defines an agent
|
|
1889
|
+
*
|
|
1890
|
+
* ```ts
|
|
1891
|
+
* const researcherAgent = context.agents.agent({
|
|
1892
|
+
* model,
|
|
1893
|
+
* name: 'academic',
|
|
1894
|
+
* maxSteps: 2,
|
|
1895
|
+
* tools: {
|
|
1896
|
+
* wikiTool: new WikipediaQueryRun({
|
|
1897
|
+
* topKResults: 1,
|
|
1898
|
+
* maxDocContentLength: 500,
|
|
1899
|
+
* })
|
|
1900
|
+
* },
|
|
1901
|
+
* background:
|
|
1902
|
+
* 'You are researcher agent with access to Wikipedia. ' +
|
|
1903
|
+
* 'Utilize Wikipedia as much as possible for correct information',
|
|
1904
|
+
* });
|
|
1905
|
+
* ```
|
|
1906
|
+
*
|
|
1907
|
+
* @param params agent parameters
|
|
1908
|
+
* @returns
|
|
1909
|
+
*/
|
|
1842
1910
|
agent(params) {
|
|
1843
1911
|
const wrappedTools = wrapTools({ context: this.context, tools: params.tools });
|
|
1844
|
-
return new Agent(
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1912
|
+
return new Agent(
|
|
1913
|
+
{
|
|
1914
|
+
...params,
|
|
1915
|
+
tools: wrappedTools
|
|
1916
|
+
},
|
|
1917
|
+
this.context
|
|
1918
|
+
);
|
|
1848
1919
|
}
|
|
1849
1920
|
task(taskParameters) {
|
|
1850
1921
|
return new Task({ context: this.context, taskParameters });
|
|
1851
1922
|
}
|
|
1923
|
+
/**
|
|
1924
|
+
* creates an openai model for agents
|
|
1925
|
+
*/
|
|
1852
1926
|
openai(...params) {
|
|
1853
1927
|
const openai2 = createWorkflowOpenAI(this.context);
|
|
1854
1928
|
return openai2(...params);
|
|
@@ -2325,6 +2399,7 @@ function decodeBase64(base64) {
|
|
|
2325
2399
|
var import_qstash8 = require("@upstash/qstash");
|
|
2326
2400
|
var DisabledWorkflowContext = class _DisabledWorkflowContext extends WorkflowContext {
|
|
2327
2401
|
static disabledMessage = "disabled-qstash-worklfow-run";
|
|
2402
|
+
disabled = true;
|
|
2328
2403
|
/**
|
|
2329
2404
|
* overwrite the WorkflowContext.addStep method to always raise WorkflowAbort
|
|
2330
2405
|
* error in order to stop the execution whenever we encounter a step.
|
|
@@ -2449,7 +2524,6 @@ var checkIfLastOneIsDuplicate = async (steps, debug) => {
|
|
|
2449
2524
|
if (step.stepId === lastStepId && step.targetStep === lastTargetStepId) {
|
|
2450
2525
|
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.`;
|
|
2451
2526
|
await debug?.log("WARN", "RESPONSE_DEFAULT", message);
|
|
2452
|
-
console.log(steps);
|
|
2453
2527
|
console.warn(message);
|
|
2454
2528
|
return true;
|
|
2455
2529
|
}
|
package/index.mjs
CHANGED
package/nextjs.d.mts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
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-CalpUeFX.mjs';
|
|
3
3
|
import '@upstash/qstash';
|
|
4
4
|
import 'ai';
|
|
5
5
|
import '@ai-sdk/openai';
|
|
6
|
-
import 'langchain/tools';
|
|
7
6
|
|
|
8
7
|
/**
|
|
9
8
|
* Serve method to serve a Upstash Workflow in a Nextjs project
|
package/nextjs.d.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
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-CalpUeFX.js';
|
|
3
3
|
import '@upstash/qstash';
|
|
4
4
|
import 'ai';
|
|
5
5
|
import '@ai-sdk/openai';
|
|
6
|
-
import 'langchain/tools';
|
|
7
6
|
|
|
8
7
|
/**
|
|
9
8
|
* Serve method to serve a Upstash Workflow in a Nextjs project
|
package/nextjs.js
CHANGED
|
@@ -827,29 +827,16 @@ var triggerWorkflowDelete = async (workflowContext, debug, cancel = false) => {
|
|
|
827
827
|
await debug?.log("SUBMIT", "SUBMIT_CLEANUP", {
|
|
828
828
|
deletedWorkflowRunId: workflowContext.workflowRunId
|
|
829
829
|
});
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
);
|
|
841
|
-
return { deleted: true };
|
|
842
|
-
} catch (error) {
|
|
843
|
-
if (error instanceof import_qstash3.QstashError && error.status === 404) {
|
|
844
|
-
await debug?.log("WARN", "SUBMIT_CLEANUP", {
|
|
845
|
-
message: `Failed to remove workflow run ${workflowContext.workflowRunId} as it doesn't exist.`,
|
|
846
|
-
name: error.name,
|
|
847
|
-
errorMessage: error.message
|
|
848
|
-
});
|
|
849
|
-
return { deleted: false };
|
|
850
|
-
}
|
|
851
|
-
throw error;
|
|
852
|
-
}
|
|
830
|
+
await workflowContext.qstashClient.http.request({
|
|
831
|
+
path: ["v2", "workflows", "runs", `${workflowContext.workflowRunId}?cancel=${cancel}`],
|
|
832
|
+
method: "DELETE",
|
|
833
|
+
parseResponseAsJson: false
|
|
834
|
+
});
|
|
835
|
+
await debug?.log(
|
|
836
|
+
"SUBMIT",
|
|
837
|
+
"SUBMIT_CLEANUP",
|
|
838
|
+
`workflow run ${workflowContext.workflowRunId} deleted.`
|
|
839
|
+
);
|
|
853
840
|
};
|
|
854
841
|
var recreateUserHeaders = (headers) => {
|
|
855
842
|
const filteredHeaders = new Headers();
|
|
@@ -1636,7 +1623,22 @@ var WorkflowApi = class extends BaseWorkflowApi {
|
|
|
1636
1623
|
// src/agents/adapters.ts
|
|
1637
1624
|
var import_openai2 = require("@ai-sdk/openai");
|
|
1638
1625
|
var import_ai = require("ai");
|
|
1626
|
+
|
|
1627
|
+
// src/agents/constants.ts
|
|
1639
1628
|
var AGENT_NAME_HEADER = "upstash-agent-name";
|
|
1629
|
+
var MANAGER_AGENT_PROMPT = `You are an agent orchestrating other AI Agents.
|
|
1630
|
+
|
|
1631
|
+
These other agents have tools available to them.
|
|
1632
|
+
|
|
1633
|
+
Given a prompt, utilize these agents to address requests.
|
|
1634
|
+
|
|
1635
|
+
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.
|
|
1636
|
+
|
|
1637
|
+
Avoid calling the same agent twice in one turn. Instead, prefer to call it once but provide everything
|
|
1638
|
+
you need from that agent.
|
|
1639
|
+
`;
|
|
1640
|
+
|
|
1641
|
+
// src/agents/adapters.ts
|
|
1640
1642
|
var createWorkflowOpenAI = (context) => {
|
|
1641
1643
|
return (0, import_openai2.createOpenAI)({
|
|
1642
1644
|
compatibility: "strict",
|
|
@@ -1703,30 +1705,49 @@ var convertLangchainTool = (langchainTool) => {
|
|
|
1703
1705
|
return (0, import_ai.tool)({
|
|
1704
1706
|
description: langchainTool.description,
|
|
1705
1707
|
parameters: langchainTool.schema,
|
|
1706
|
-
|
|
1707
|
-
execute: async (param) => langchainTool.invoke(param)
|
|
1708
|
+
execute: async (...param) => langchainTool.invoke(...param)
|
|
1708
1709
|
});
|
|
1709
1710
|
};
|
|
1710
1711
|
|
|
1711
1712
|
// src/agents/agent.ts
|
|
1712
1713
|
var import_zod = require("zod");
|
|
1713
1714
|
var import_ai2 = require("ai");
|
|
1715
|
+
|
|
1716
|
+
// src/serve/utils.ts
|
|
1717
|
+
var isDisabledWorkflowContext = (context) => {
|
|
1718
|
+
return "disabled" in context;
|
|
1719
|
+
};
|
|
1720
|
+
|
|
1721
|
+
// src/agents/agent.ts
|
|
1714
1722
|
var Agent = class {
|
|
1715
1723
|
name;
|
|
1716
1724
|
tools;
|
|
1717
1725
|
maxSteps;
|
|
1718
1726
|
background;
|
|
1719
1727
|
model;
|
|
1720
|
-
|
|
1728
|
+
temparature;
|
|
1729
|
+
context;
|
|
1730
|
+
constructor({ tools, maxSteps, background, name, model, temparature = 0.1 }, context) {
|
|
1721
1731
|
this.name = name;
|
|
1722
1732
|
this.tools = tools ?? {};
|
|
1723
1733
|
this.maxSteps = maxSteps;
|
|
1724
1734
|
this.background = background;
|
|
1725
1735
|
this.model = model;
|
|
1736
|
+
this.temparature = temparature;
|
|
1737
|
+
this.context = context;
|
|
1726
1738
|
}
|
|
1739
|
+
/**
|
|
1740
|
+
* Trigger the agent by passing a prompt
|
|
1741
|
+
*
|
|
1742
|
+
* @param prompt task to assign to the agent
|
|
1743
|
+
* @returns Response as `{ text: string }`
|
|
1744
|
+
*/
|
|
1727
1745
|
async call({ prompt }) {
|
|
1728
1746
|
try {
|
|
1729
|
-
|
|
1747
|
+
if (isDisabledWorkflowContext(this.context)) {
|
|
1748
|
+
await this.context.sleep("abort", 0);
|
|
1749
|
+
}
|
|
1750
|
+
const result = await (0, import_ai2.generateText)({
|
|
1730
1751
|
model: this.model,
|
|
1731
1752
|
tools: this.tools,
|
|
1732
1753
|
maxSteps: this.maxSteps,
|
|
@@ -1734,8 +1755,10 @@ var Agent = class {
|
|
|
1734
1755
|
prompt,
|
|
1735
1756
|
headers: {
|
|
1736
1757
|
[AGENT_NAME_HEADER]: this.name
|
|
1737
|
-
}
|
|
1758
|
+
},
|
|
1759
|
+
temperature: this.temparature
|
|
1738
1760
|
});
|
|
1761
|
+
return { text: result.text };
|
|
1739
1762
|
} catch (error) {
|
|
1740
1763
|
if (error instanceof import_ai2.ToolExecutionError) {
|
|
1741
1764
|
if (error.cause instanceof Error && error.cause.name === "WorkflowAbort") {
|
|
@@ -1750,6 +1773,11 @@ var Agent = class {
|
|
|
1750
1773
|
}
|
|
1751
1774
|
}
|
|
1752
1775
|
}
|
|
1776
|
+
/**
|
|
1777
|
+
* Convert the agent to a tool which can be used by other agents.
|
|
1778
|
+
*
|
|
1779
|
+
* @returns the agent as a tool
|
|
1780
|
+
*/
|
|
1753
1781
|
asTool() {
|
|
1754
1782
|
const toolDescriptions = Object.values(this.tools).map((tool3) => tool3.description).join("\n");
|
|
1755
1783
|
return (0, import_ai2.tool)({
|
|
@@ -1761,27 +1789,37 @@ var Agent = class {
|
|
|
1761
1789
|
});
|
|
1762
1790
|
}
|
|
1763
1791
|
};
|
|
1764
|
-
var MANAGER_AGENT_PROMPT = `You are an AI agent who orchestrates other AI Agents.
|
|
1765
|
-
These other agents have tools available to them.
|
|
1766
|
-
Given a prompt, utilize these agents to address requests.
|
|
1767
|
-
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.
|
|
1768
|
-
`;
|
|
1769
1792
|
var ManagerAgent = class extends Agent {
|
|
1770
1793
|
agents;
|
|
1794
|
+
/**
|
|
1795
|
+
* A manager agent which coordinates agents available to it to achieve a
|
|
1796
|
+
* given task
|
|
1797
|
+
*
|
|
1798
|
+
* @param name Name of the agent
|
|
1799
|
+
* @param background Background of the agent. If not passed, default will be used.
|
|
1800
|
+
* @param model LLM model to use
|
|
1801
|
+
* @param agents: List of agents available to the agent
|
|
1802
|
+
* @param maxSteps number of times the manager agent can call the LLM at most.
|
|
1803
|
+
* If the agent abruptly stops execution after calling other agents, you may
|
|
1804
|
+
* need to increase maxSteps
|
|
1805
|
+
*/
|
|
1771
1806
|
constructor({
|
|
1772
|
-
maxSteps,
|
|
1773
|
-
background = MANAGER_AGENT_PROMPT,
|
|
1774
1807
|
agents,
|
|
1808
|
+
background = MANAGER_AGENT_PROMPT,
|
|
1775
1809
|
model,
|
|
1810
|
+
maxSteps,
|
|
1776
1811
|
name = "manager llm"
|
|
1777
|
-
}) {
|
|
1778
|
-
super(
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1812
|
+
}, context) {
|
|
1813
|
+
super(
|
|
1814
|
+
{
|
|
1815
|
+
background,
|
|
1816
|
+
maxSteps,
|
|
1817
|
+
tools: Object.fromEntries(agents.map((agent) => [agent.name, agent.asTool()])),
|
|
1818
|
+
name,
|
|
1819
|
+
model
|
|
1820
|
+
},
|
|
1821
|
+
context
|
|
1822
|
+
);
|
|
1785
1823
|
this.agents = agents;
|
|
1786
1824
|
}
|
|
1787
1825
|
};
|
|
@@ -1797,25 +1835,32 @@ var Task = class {
|
|
|
1797
1835
|
this.context = context;
|
|
1798
1836
|
this.taskParameters = taskParameters;
|
|
1799
1837
|
}
|
|
1838
|
+
/**
|
|
1839
|
+
* Run the agents to complete the task
|
|
1840
|
+
*
|
|
1841
|
+
* @returns Result of the task as { text: string }
|
|
1842
|
+
*/
|
|
1800
1843
|
async run() {
|
|
1801
1844
|
const { prompt, ...otherParams } = this.taskParameters;
|
|
1802
|
-
const safePrompt = await this.context.run("Get Prompt", () => prompt);
|
|
1803
1845
|
if ("agent" in otherParams) {
|
|
1804
1846
|
const agent = otherParams.agent;
|
|
1805
1847
|
const result = await agent.call({
|
|
1806
|
-
prompt
|
|
1848
|
+
prompt
|
|
1807
1849
|
});
|
|
1808
1850
|
return { text: result.text };
|
|
1809
1851
|
} else {
|
|
1810
1852
|
const { agents, maxSteps, model, background } = otherParams;
|
|
1811
|
-
const managerAgent = new ManagerAgent(
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1853
|
+
const managerAgent = new ManagerAgent(
|
|
1854
|
+
{
|
|
1855
|
+
model,
|
|
1856
|
+
maxSteps,
|
|
1857
|
+
agents,
|
|
1858
|
+
name: "Manager LLM",
|
|
1859
|
+
background
|
|
1860
|
+
},
|
|
1861
|
+
this.context
|
|
1862
|
+
);
|
|
1863
|
+
const result = await managerAgent.call({ prompt });
|
|
1819
1864
|
return { text: result.text };
|
|
1820
1865
|
}
|
|
1821
1866
|
}
|
|
@@ -1827,16 +1872,45 @@ var WorkflowAgents = class {
|
|
|
1827
1872
|
constructor({ context }) {
|
|
1828
1873
|
this.context = context;
|
|
1829
1874
|
}
|
|
1875
|
+
/**
|
|
1876
|
+
* Defines an agent
|
|
1877
|
+
*
|
|
1878
|
+
* ```ts
|
|
1879
|
+
* const researcherAgent = context.agents.agent({
|
|
1880
|
+
* model,
|
|
1881
|
+
* name: 'academic',
|
|
1882
|
+
* maxSteps: 2,
|
|
1883
|
+
* tools: {
|
|
1884
|
+
* wikiTool: new WikipediaQueryRun({
|
|
1885
|
+
* topKResults: 1,
|
|
1886
|
+
* maxDocContentLength: 500,
|
|
1887
|
+
* })
|
|
1888
|
+
* },
|
|
1889
|
+
* background:
|
|
1890
|
+
* 'You are researcher agent with access to Wikipedia. ' +
|
|
1891
|
+
* 'Utilize Wikipedia as much as possible for correct information',
|
|
1892
|
+
* });
|
|
1893
|
+
* ```
|
|
1894
|
+
*
|
|
1895
|
+
* @param params agent parameters
|
|
1896
|
+
* @returns
|
|
1897
|
+
*/
|
|
1830
1898
|
agent(params) {
|
|
1831
1899
|
const wrappedTools = wrapTools({ context: this.context, tools: params.tools });
|
|
1832
|
-
return new Agent(
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1900
|
+
return new Agent(
|
|
1901
|
+
{
|
|
1902
|
+
...params,
|
|
1903
|
+
tools: wrappedTools
|
|
1904
|
+
},
|
|
1905
|
+
this.context
|
|
1906
|
+
);
|
|
1836
1907
|
}
|
|
1837
1908
|
task(taskParameters) {
|
|
1838
1909
|
return new Task({ context: this.context, taskParameters });
|
|
1839
1910
|
}
|
|
1911
|
+
/**
|
|
1912
|
+
* creates an openai model for agents
|
|
1913
|
+
*/
|
|
1840
1914
|
openai(...params) {
|
|
1841
1915
|
const openai2 = createWorkflowOpenAI(this.context);
|
|
1842
1916
|
return openai2(...params);
|
|
@@ -2313,6 +2387,7 @@ function decodeBase64(base64) {
|
|
|
2313
2387
|
var import_qstash8 = require("@upstash/qstash");
|
|
2314
2388
|
var DisabledWorkflowContext = class _DisabledWorkflowContext extends WorkflowContext {
|
|
2315
2389
|
static disabledMessage = "disabled-qstash-worklfow-run";
|
|
2390
|
+
disabled = true;
|
|
2316
2391
|
/**
|
|
2317
2392
|
* overwrite the WorkflowContext.addStep method to always raise WorkflowAbort
|
|
2318
2393
|
* error in order to stop the execution whenever we encounter a step.
|
|
@@ -2437,7 +2512,6 @@ var checkIfLastOneIsDuplicate = async (steps, debug) => {
|
|
|
2437
2512
|
if (step.stepId === lastStepId && step.targetStep === lastTargetStepId) {
|
|
2438
2513
|
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.`;
|
|
2439
2514
|
await debug?.log("WARN", "RESPONSE_DEFAULT", message);
|
|
2440
|
-
console.log(steps);
|
|
2441
2515
|
console.warn(message);
|
|
2442
2516
|
return true;
|
|
2443
2517
|
}
|
package/nextjs.mjs
CHANGED
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"name":"@upstash/workflow","version":"v0.2.5-agents","description":"Durable, Reliable and Performant Serverless Functions","main":"./index.js","module":"./index.mjs","types":"./index.d.ts","files":["./*"],"exports":{".":{"import":"./index.mjs","require":"./index.js"},"./dist/nextjs":{"import":"./nextjs.mjs","require":"./nextjs.js"},"./nextjs":{"import":"./nextjs.mjs","require":"./nextjs.js"},"./h3":{"import":"./h3.mjs","require":"./h3.js"},"./svelte":{"import":"./svelte.mjs","require":"./svelte.js"},"./solidjs":{"import":"./solidjs.mjs","require":"./solidjs.js"},"./workflow":{"import":"./workflow.mjs","require":"./workflow.js"},"./hono":{"import":"./hono.mjs","require":"./hono.js"},"./cloudflare":{"import":"./cloudflare.mjs","require":"./cloudflare.js"},"./astro":{"import":"./astro.mjs","require":"./astro.js"},"./express":{"import":"./express.mjs","require":"./express.js"}
|
|
1
|
+
{"name":"@upstash/workflow","version":"v0.2.5-agents-3","description":"Durable, Reliable and Performant Serverless Functions","main":"./index.js","module":"./index.mjs","types":"./index.d.ts","files":["./*"],"exports":{".":{"import":"./index.mjs","require":"./index.js"},"./dist/nextjs":{"import":"./nextjs.mjs","require":"./nextjs.js"},"./nextjs":{"import":"./nextjs.mjs","require":"./nextjs.js"},"./h3":{"import":"./h3.mjs","require":"./h3.js"},"./svelte":{"import":"./svelte.mjs","require":"./svelte.js"},"./solidjs":{"import":"./solidjs.mjs","require":"./solidjs.js"},"./workflow":{"import":"./workflow.mjs","require":"./workflow.js"},"./hono":{"import":"./hono.mjs","require":"./hono.js"},"./cloudflare":{"import":"./cloudflare.mjs","require":"./cloudflare.js"},"./astro":{"import":"./astro.mjs","require":"./astro.js"},"./express":{"import":"./express.mjs","require":"./express.js"}},"scripts":{"build":"tsup && cp README.md ./dist/ && cp package.json ./dist/ && cp LICENSE ./dist/","test":"bun test src","fmt":"prettier --write .","lint":"tsc && eslint \"{src,platforms}/**/*.{js,ts,tsx}\" --quiet --fix","check-exports":"bun run build && cd dist && attw -P"},"repository":{"type":"git","url":"git+https://github.com/upstash/workflow-ts.git"},"keywords":["upstash","qstash","workflow","serverless"],"author":"Cahid Arda Oz","license":"MIT","bugs":{"url":"https://github.com/upstash/workflow-ts/issues"},"homepage":"https://github.com/upstash/workflow-ts#readme","devDependencies":{"@commitlint/cli":"^19.5.0","@commitlint/config-conventional":"^19.5.0","@eslint/js":"^9.11.1","@solidjs/start":"^1.0.8","@sveltejs/kit":"^2.6.1","@types/bun":"^1.1.10","@types/express":"^5.0.0","astro":"^4.16.7","eslint":"^9.11.1","eslint-plugin-unicorn":"^55.0.0","express":"^4.21.1","globals":"^15.10.0","h3":"^1.12.0","hono":"^4.6.3","husky":"^9.1.6","next":"^14.2.14","prettier":"3.3.3","tsup":"^8.3.0","typescript":"^5.7.2","typescript-eslint":"^8.18.0"},"dependencies":{"@ai-sdk/openai":"^1.0.15","@upstash/qstash":"^2.7.20","ai":"^4.0.30","zod":"^3.24.1"},"directories":{"example":"examples"}}
|
package/solidjs.d.mts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { APIEvent } from '@solidjs/start/server';
|
|
2
|
-
import { R as RouteFunction, j as PublicServeOptions } from './types-
|
|
2
|
+
import { R as RouteFunction, j as PublicServeOptions } from './types-CalpUeFX.mjs';
|
|
3
3
|
import '@upstash/qstash';
|
|
4
4
|
import 'ai';
|
|
5
5
|
import '@ai-sdk/openai';
|
|
6
|
-
import 'langchain/tools';
|
|
7
6
|
|
|
8
7
|
/**
|
|
9
8
|
* Serve method to serve a Upstash Workflow in a Nextjs project
|