@upstash/workflow 0.2.5-agent-1 → 0.2.5-agents-2
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 +3 -1
- package/astro.d.ts +3 -1
- package/astro.js +292 -23
- package/astro.mjs +1 -1
- package/{chunk-ETDFMXER.mjs → chunk-VOM3CFYZ.mjs} +292 -23
- package/cloudflare.d.mts +3 -1
- package/cloudflare.d.ts +3 -1
- package/cloudflare.js +292 -23
- package/cloudflare.mjs +1 -1
- package/express.d.mts +3 -1
- package/express.d.ts +3 -1
- package/express.js +292 -23
- package/express.mjs +1 -1
- package/h3.d.mts +3 -1
- package/h3.d.ts +3 -1
- package/h3.js +292 -23
- package/h3.mjs +1 -1
- package/hono.d.mts +3 -1
- package/hono.d.ts +3 -1
- package/hono.js +292 -23
- package/hono.mjs +1 -1
- package/index.d.mts +4 -2
- package/index.d.ts +4 -2
- package/index.js +292 -23
- package/index.mjs +1 -1
- package/nextjs.d.mts +3 -1
- package/nextjs.d.ts +3 -1
- package/nextjs.js +292 -23
- package/nextjs.mjs +1 -1
- package/package.json +1 -1
- package/solidjs.d.mts +3 -1
- package/solidjs.d.ts +3 -1
- package/solidjs.js +292 -23
- package/solidjs.mjs +1 -1
- package/svelte.d.mts +3 -1
- package/svelte.d.ts +3 -1
- package/svelte.js +292 -23
- package/svelte.mjs +1 -1
- package/{types-Bt4-paRy.d.mts → types-D9gwTj2n.d.mts} +237 -0
- package/{types-Bt4-paRy.d.ts → types-D9gwTj2n.d.ts} +237 -0
package/solidjs.js
CHANGED
|
@@ -826,29 +826,16 @@ var triggerWorkflowDelete = async (workflowContext, debug, cancel = false) => {
|
|
|
826
826
|
await debug?.log("SUBMIT", "SUBMIT_CLEANUP", {
|
|
827
827
|
deletedWorkflowRunId: workflowContext.workflowRunId
|
|
828
828
|
});
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
);
|
|
840
|
-
return { deleted: true };
|
|
841
|
-
} catch (error) {
|
|
842
|
-
if (error instanceof import_qstash3.QstashError && error.status === 404) {
|
|
843
|
-
await debug?.log("WARN", "SUBMIT_CLEANUP", {
|
|
844
|
-
message: `Failed to remove workflow run ${workflowContext.workflowRunId} as it doesn't exist.`,
|
|
845
|
-
name: error.name,
|
|
846
|
-
errorMessage: error.message
|
|
847
|
-
});
|
|
848
|
-
return { deleted: false };
|
|
849
|
-
}
|
|
850
|
-
throw error;
|
|
851
|
-
}
|
|
829
|
+
await workflowContext.qstashClient.http.request({
|
|
830
|
+
path: ["v2", "workflows", "runs", `${workflowContext.workflowRunId}?cancel=${cancel}`],
|
|
831
|
+
method: "DELETE",
|
|
832
|
+
parseResponseAsJson: false
|
|
833
|
+
});
|
|
834
|
+
await debug?.log(
|
|
835
|
+
"SUBMIT",
|
|
836
|
+
"SUBMIT_CLEANUP",
|
|
837
|
+
`workflow run ${workflowContext.workflowRunId} deleted.`
|
|
838
|
+
);
|
|
852
839
|
};
|
|
853
840
|
var recreateUserHeaders = (headers) => {
|
|
854
841
|
const filteredHeaders = new Headers();
|
|
@@ -1632,6 +1619,283 @@ var WorkflowApi = class extends BaseWorkflowApi {
|
|
|
1632
1619
|
}
|
|
1633
1620
|
};
|
|
1634
1621
|
|
|
1622
|
+
// src/agents/adapters.ts
|
|
1623
|
+
var import_openai2 = require("@ai-sdk/openai");
|
|
1624
|
+
var import_ai = require("ai");
|
|
1625
|
+
|
|
1626
|
+
// src/agents/constants.ts
|
|
1627
|
+
var AGENT_NAME_HEADER = "upstash-agent-name";
|
|
1628
|
+
var MANAGER_AGENT_PROMPT = `You are an agent orchestrating other AI Agents.
|
|
1629
|
+
|
|
1630
|
+
These other agents have tools available to them.
|
|
1631
|
+
|
|
1632
|
+
Given a prompt, utilize these agents to address requests.
|
|
1633
|
+
|
|
1634
|
+
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.
|
|
1635
|
+
|
|
1636
|
+
Avoid calling the same agent twice in one turn. Instead, prefer to call it once but provide everything
|
|
1637
|
+
you need from that agent.
|
|
1638
|
+
`;
|
|
1639
|
+
|
|
1640
|
+
// src/agents/adapters.ts
|
|
1641
|
+
var createWorkflowOpenAI = (context) => {
|
|
1642
|
+
return (0, import_openai2.createOpenAI)({
|
|
1643
|
+
compatibility: "strict",
|
|
1644
|
+
fetch: async (input, init) => {
|
|
1645
|
+
try {
|
|
1646
|
+
const headers = init?.headers ? Object.fromEntries(new Headers(init.headers).entries()) : {};
|
|
1647
|
+
const body = init?.body ? JSON.parse(init.body) : void 0;
|
|
1648
|
+
const agentName = headers[AGENT_NAME_HEADER];
|
|
1649
|
+
const stepName = agentName ? `Call Agent ${agentName}` : "Call Agent";
|
|
1650
|
+
const responseInfo = await context.call(stepName, {
|
|
1651
|
+
url: input.toString(),
|
|
1652
|
+
method: init?.method,
|
|
1653
|
+
headers,
|
|
1654
|
+
body
|
|
1655
|
+
});
|
|
1656
|
+
const responseHeaders = new Headers(
|
|
1657
|
+
Object.entries(responseInfo.header).reduce(
|
|
1658
|
+
(acc, [key, values]) => {
|
|
1659
|
+
acc[key] = values.join(", ");
|
|
1660
|
+
return acc;
|
|
1661
|
+
},
|
|
1662
|
+
{}
|
|
1663
|
+
)
|
|
1664
|
+
);
|
|
1665
|
+
return new Response(JSON.stringify(responseInfo.body), {
|
|
1666
|
+
status: responseInfo.status,
|
|
1667
|
+
headers: responseHeaders
|
|
1668
|
+
});
|
|
1669
|
+
} catch (error) {
|
|
1670
|
+
if (error instanceof Error && error.name === "WorkflowAbort") {
|
|
1671
|
+
throw error;
|
|
1672
|
+
} else {
|
|
1673
|
+
console.error("Error in fetch implementation:", error);
|
|
1674
|
+
throw error;
|
|
1675
|
+
}
|
|
1676
|
+
}
|
|
1677
|
+
}
|
|
1678
|
+
});
|
|
1679
|
+
};
|
|
1680
|
+
var wrapTools = ({
|
|
1681
|
+
context,
|
|
1682
|
+
tools
|
|
1683
|
+
}) => {
|
|
1684
|
+
return Object.fromEntries(
|
|
1685
|
+
Object.entries(tools).map((toolInfo) => {
|
|
1686
|
+
const [toolName, tool3] = toolInfo;
|
|
1687
|
+
const aiSDKTool = convertToAISDKTool(tool3);
|
|
1688
|
+
const execute = aiSDKTool.execute;
|
|
1689
|
+
if (execute) {
|
|
1690
|
+
const wrappedExecute = (...params) => {
|
|
1691
|
+
return context.run(`Run tool ${toolName}`, () => execute(...params));
|
|
1692
|
+
};
|
|
1693
|
+
aiSDKTool.execute = wrappedExecute;
|
|
1694
|
+
}
|
|
1695
|
+
return [toolName, aiSDKTool];
|
|
1696
|
+
})
|
|
1697
|
+
);
|
|
1698
|
+
};
|
|
1699
|
+
var convertToAISDKTool = (tool3) => {
|
|
1700
|
+
const isLangchainTool = "invoke" in tool3;
|
|
1701
|
+
return isLangchainTool ? convertLangchainTool(tool3) : tool3;
|
|
1702
|
+
};
|
|
1703
|
+
var convertLangchainTool = (langchainTool) => {
|
|
1704
|
+
return (0, import_ai.tool)({
|
|
1705
|
+
description: langchainTool.description,
|
|
1706
|
+
parameters: langchainTool.schema,
|
|
1707
|
+
execute: async (...param) => langchainTool.invoke(...param)
|
|
1708
|
+
});
|
|
1709
|
+
};
|
|
1710
|
+
|
|
1711
|
+
// src/agents/agent.ts
|
|
1712
|
+
var import_zod = require("zod");
|
|
1713
|
+
var import_ai2 = require("ai");
|
|
1714
|
+
var Agent = class {
|
|
1715
|
+
name;
|
|
1716
|
+
tools;
|
|
1717
|
+
maxSteps;
|
|
1718
|
+
background;
|
|
1719
|
+
model;
|
|
1720
|
+
temparature;
|
|
1721
|
+
constructor({ tools, maxSteps, background, name, model, temparature = 0.1 }) {
|
|
1722
|
+
this.name = name;
|
|
1723
|
+
this.tools = tools ?? {};
|
|
1724
|
+
this.maxSteps = maxSteps;
|
|
1725
|
+
this.background = background;
|
|
1726
|
+
this.model = model;
|
|
1727
|
+
this.temparature = temparature;
|
|
1728
|
+
}
|
|
1729
|
+
/**
|
|
1730
|
+
* Trigger the agent by passing a prompt
|
|
1731
|
+
*
|
|
1732
|
+
* @param prompt task to assign to the agent
|
|
1733
|
+
* @returns Response as `{ text: string }`
|
|
1734
|
+
*/
|
|
1735
|
+
async call({ prompt }) {
|
|
1736
|
+
try {
|
|
1737
|
+
const result = await (0, import_ai2.generateText)({
|
|
1738
|
+
model: this.model,
|
|
1739
|
+
tools: this.tools,
|
|
1740
|
+
maxSteps: this.maxSteps,
|
|
1741
|
+
system: this.background,
|
|
1742
|
+
prompt,
|
|
1743
|
+
headers: {
|
|
1744
|
+
[AGENT_NAME_HEADER]: this.name
|
|
1745
|
+
},
|
|
1746
|
+
temperature: this.temparature
|
|
1747
|
+
});
|
|
1748
|
+
return { text: result.text };
|
|
1749
|
+
} catch (error) {
|
|
1750
|
+
if (error instanceof import_ai2.ToolExecutionError) {
|
|
1751
|
+
if (error.cause instanceof Error && error.cause.name === "WorkflowAbort") {
|
|
1752
|
+
throw error.cause;
|
|
1753
|
+
} else if (error.cause instanceof import_ai2.ToolExecutionError && error.cause.cause instanceof Error && error.cause.cause.name === "WorkflowAbort") {
|
|
1754
|
+
throw error.cause.cause;
|
|
1755
|
+
} else {
|
|
1756
|
+
throw error;
|
|
1757
|
+
}
|
|
1758
|
+
} else {
|
|
1759
|
+
throw error;
|
|
1760
|
+
}
|
|
1761
|
+
}
|
|
1762
|
+
}
|
|
1763
|
+
/**
|
|
1764
|
+
* Convert the agent to a tool which can be used by other agents.
|
|
1765
|
+
*
|
|
1766
|
+
* @returns the agent as a tool
|
|
1767
|
+
*/
|
|
1768
|
+
asTool() {
|
|
1769
|
+
const toolDescriptions = Object.values(this.tools).map((tool3) => tool3.description).join("\n");
|
|
1770
|
+
return (0, import_ai2.tool)({
|
|
1771
|
+
parameters: import_zod.z.object({ prompt: import_zod.z.string() }),
|
|
1772
|
+
execute: async ({ prompt }) => {
|
|
1773
|
+
return await this.call({ prompt });
|
|
1774
|
+
},
|
|
1775
|
+
description: `An AI Agent with the following background: ${this.background}Has access to the following tools: ${toolDescriptions}`
|
|
1776
|
+
});
|
|
1777
|
+
}
|
|
1778
|
+
};
|
|
1779
|
+
var ManagerAgent = class extends Agent {
|
|
1780
|
+
agents;
|
|
1781
|
+
/**
|
|
1782
|
+
* A manager agent which coordinates agents available to it to achieve a
|
|
1783
|
+
* given task
|
|
1784
|
+
*
|
|
1785
|
+
* @param name Name of the agent
|
|
1786
|
+
* @param background Background of the agent. If not passed, default will be used.
|
|
1787
|
+
* @param model LLM model to use
|
|
1788
|
+
* @param agents: List of agents available to the agent
|
|
1789
|
+
* @param maxSteps number of times the manager agent can call the LLM at most.
|
|
1790
|
+
* If the agent abruptly stops execution after calling other agents, you may
|
|
1791
|
+
* need to increase maxSteps
|
|
1792
|
+
*/
|
|
1793
|
+
constructor({
|
|
1794
|
+
agents,
|
|
1795
|
+
background = MANAGER_AGENT_PROMPT,
|
|
1796
|
+
model,
|
|
1797
|
+
maxSteps,
|
|
1798
|
+
name = "manager llm"
|
|
1799
|
+
}) {
|
|
1800
|
+
super({
|
|
1801
|
+
background,
|
|
1802
|
+
maxSteps,
|
|
1803
|
+
tools: Object.fromEntries(agents.map((agent) => [agent.name, agent.asTool()])),
|
|
1804
|
+
name,
|
|
1805
|
+
model
|
|
1806
|
+
});
|
|
1807
|
+
this.agents = agents;
|
|
1808
|
+
}
|
|
1809
|
+
};
|
|
1810
|
+
|
|
1811
|
+
// src/agents/task.ts
|
|
1812
|
+
var Task = class {
|
|
1813
|
+
context;
|
|
1814
|
+
taskParameters;
|
|
1815
|
+
constructor({
|
|
1816
|
+
context,
|
|
1817
|
+
taskParameters
|
|
1818
|
+
}) {
|
|
1819
|
+
this.context = context;
|
|
1820
|
+
this.taskParameters = taskParameters;
|
|
1821
|
+
}
|
|
1822
|
+
/**
|
|
1823
|
+
* Run the agents to complete the task
|
|
1824
|
+
*
|
|
1825
|
+
* @returns Result of the task as { text: string }
|
|
1826
|
+
*/
|
|
1827
|
+
async run() {
|
|
1828
|
+
const { prompt, ...otherParams } = this.taskParameters;
|
|
1829
|
+
const safePrompt = await this.context.run("Get Prompt", () => prompt);
|
|
1830
|
+
if ("agent" in otherParams) {
|
|
1831
|
+
const agent = otherParams.agent;
|
|
1832
|
+
const result = await agent.call({
|
|
1833
|
+
prompt: safePrompt
|
|
1834
|
+
});
|
|
1835
|
+
return { text: result.text };
|
|
1836
|
+
} else {
|
|
1837
|
+
const { agents, maxSteps, model, background } = otherParams;
|
|
1838
|
+
const managerAgent = new ManagerAgent({
|
|
1839
|
+
model,
|
|
1840
|
+
maxSteps,
|
|
1841
|
+
agents,
|
|
1842
|
+
name: "Manager LLM",
|
|
1843
|
+
background
|
|
1844
|
+
});
|
|
1845
|
+
const result = await managerAgent.call({ prompt: safePrompt });
|
|
1846
|
+
return { text: result.text };
|
|
1847
|
+
}
|
|
1848
|
+
}
|
|
1849
|
+
};
|
|
1850
|
+
|
|
1851
|
+
// src/agents/index.ts
|
|
1852
|
+
var WorkflowAgents = class {
|
|
1853
|
+
context;
|
|
1854
|
+
constructor({ context }) {
|
|
1855
|
+
this.context = context;
|
|
1856
|
+
}
|
|
1857
|
+
/**
|
|
1858
|
+
* Defines an agent
|
|
1859
|
+
*
|
|
1860
|
+
* ```ts
|
|
1861
|
+
* const researcherAgent = context.agents.agent({
|
|
1862
|
+
* model,
|
|
1863
|
+
* name: 'academic',
|
|
1864
|
+
* maxSteps: 2,
|
|
1865
|
+
* tools: {
|
|
1866
|
+
* wikiTool: new WikipediaQueryRun({
|
|
1867
|
+
* topKResults: 1,
|
|
1868
|
+
* maxDocContentLength: 500,
|
|
1869
|
+
* })
|
|
1870
|
+
* },
|
|
1871
|
+
* background:
|
|
1872
|
+
* 'You are researcher agent with access to Wikipedia. ' +
|
|
1873
|
+
* 'Utilize Wikipedia as much as possible for correct information',
|
|
1874
|
+
* });
|
|
1875
|
+
* ```
|
|
1876
|
+
*
|
|
1877
|
+
* @param params agent parameters
|
|
1878
|
+
* @returns
|
|
1879
|
+
*/
|
|
1880
|
+
agent(params) {
|
|
1881
|
+
const wrappedTools = wrapTools({ context: this.context, tools: params.tools });
|
|
1882
|
+
return new Agent({
|
|
1883
|
+
...params,
|
|
1884
|
+
tools: wrappedTools
|
|
1885
|
+
});
|
|
1886
|
+
}
|
|
1887
|
+
task(taskParameters) {
|
|
1888
|
+
return new Task({ context: this.context, taskParameters });
|
|
1889
|
+
}
|
|
1890
|
+
/**
|
|
1891
|
+
* creates an openai model for agents
|
|
1892
|
+
*/
|
|
1893
|
+
openai(...params) {
|
|
1894
|
+
const openai2 = createWorkflowOpenAI(this.context);
|
|
1895
|
+
return openai2(...params);
|
|
1896
|
+
}
|
|
1897
|
+
};
|
|
1898
|
+
|
|
1635
1899
|
// src/context/context.ts
|
|
1636
1900
|
var WorkflowContext = class {
|
|
1637
1901
|
executor;
|
|
@@ -2017,6 +2281,11 @@ var WorkflowContext = class {
|
|
|
2017
2281
|
context: this
|
|
2018
2282
|
});
|
|
2019
2283
|
}
|
|
2284
|
+
get agents() {
|
|
2285
|
+
return new WorkflowAgents({
|
|
2286
|
+
context: this
|
|
2287
|
+
});
|
|
2288
|
+
}
|
|
2020
2289
|
};
|
|
2021
2290
|
|
|
2022
2291
|
// src/logger.ts
|
package/solidjs.mjs
CHANGED
package/svelte.d.mts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { RequestHandler } from '@sveltejs/kit';
|
|
2
|
-
import { R as RouteFunction, j as PublicServeOptions } from './types-
|
|
2
|
+
import { R as RouteFunction, j as PublicServeOptions } from './types-D9gwTj2n.mjs';
|
|
3
3
|
import '@upstash/qstash';
|
|
4
|
+
import 'ai';
|
|
5
|
+
import '@ai-sdk/openai';
|
|
4
6
|
|
|
5
7
|
/**
|
|
6
8
|
* Serve method to serve a Upstash Workflow in a Nextjs project
|
package/svelte.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { RequestHandler } from '@sveltejs/kit';
|
|
2
|
-
import { R as RouteFunction, j as PublicServeOptions } from './types-
|
|
2
|
+
import { R as RouteFunction, j as PublicServeOptions } from './types-D9gwTj2n.js';
|
|
3
3
|
import '@upstash/qstash';
|
|
4
|
+
import 'ai';
|
|
5
|
+
import '@ai-sdk/openai';
|
|
4
6
|
|
|
5
7
|
/**
|
|
6
8
|
* Serve method to serve a Upstash Workflow in a Nextjs project
|
package/svelte.js
CHANGED
|
@@ -826,29 +826,16 @@ var triggerWorkflowDelete = async (workflowContext, debug, cancel = false) => {
|
|
|
826
826
|
await debug?.log("SUBMIT", "SUBMIT_CLEANUP", {
|
|
827
827
|
deletedWorkflowRunId: workflowContext.workflowRunId
|
|
828
828
|
});
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
);
|
|
840
|
-
return { deleted: true };
|
|
841
|
-
} catch (error) {
|
|
842
|
-
if (error instanceof import_qstash3.QstashError && error.status === 404) {
|
|
843
|
-
await debug?.log("WARN", "SUBMIT_CLEANUP", {
|
|
844
|
-
message: `Failed to remove workflow run ${workflowContext.workflowRunId} as it doesn't exist.`,
|
|
845
|
-
name: error.name,
|
|
846
|
-
errorMessage: error.message
|
|
847
|
-
});
|
|
848
|
-
return { deleted: false };
|
|
849
|
-
}
|
|
850
|
-
throw error;
|
|
851
|
-
}
|
|
829
|
+
await workflowContext.qstashClient.http.request({
|
|
830
|
+
path: ["v2", "workflows", "runs", `${workflowContext.workflowRunId}?cancel=${cancel}`],
|
|
831
|
+
method: "DELETE",
|
|
832
|
+
parseResponseAsJson: false
|
|
833
|
+
});
|
|
834
|
+
await debug?.log(
|
|
835
|
+
"SUBMIT",
|
|
836
|
+
"SUBMIT_CLEANUP",
|
|
837
|
+
`workflow run ${workflowContext.workflowRunId} deleted.`
|
|
838
|
+
);
|
|
852
839
|
};
|
|
853
840
|
var recreateUserHeaders = (headers) => {
|
|
854
841
|
const filteredHeaders = new Headers();
|
|
@@ -1632,6 +1619,283 @@ var WorkflowApi = class extends BaseWorkflowApi {
|
|
|
1632
1619
|
}
|
|
1633
1620
|
};
|
|
1634
1621
|
|
|
1622
|
+
// src/agents/adapters.ts
|
|
1623
|
+
var import_openai2 = require("@ai-sdk/openai");
|
|
1624
|
+
var import_ai = require("ai");
|
|
1625
|
+
|
|
1626
|
+
// src/agents/constants.ts
|
|
1627
|
+
var AGENT_NAME_HEADER = "upstash-agent-name";
|
|
1628
|
+
var MANAGER_AGENT_PROMPT = `You are an agent orchestrating other AI Agents.
|
|
1629
|
+
|
|
1630
|
+
These other agents have tools available to them.
|
|
1631
|
+
|
|
1632
|
+
Given a prompt, utilize these agents to address requests.
|
|
1633
|
+
|
|
1634
|
+
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.
|
|
1635
|
+
|
|
1636
|
+
Avoid calling the same agent twice in one turn. Instead, prefer to call it once but provide everything
|
|
1637
|
+
you need from that agent.
|
|
1638
|
+
`;
|
|
1639
|
+
|
|
1640
|
+
// src/agents/adapters.ts
|
|
1641
|
+
var createWorkflowOpenAI = (context) => {
|
|
1642
|
+
return (0, import_openai2.createOpenAI)({
|
|
1643
|
+
compatibility: "strict",
|
|
1644
|
+
fetch: async (input, init) => {
|
|
1645
|
+
try {
|
|
1646
|
+
const headers = init?.headers ? Object.fromEntries(new Headers(init.headers).entries()) : {};
|
|
1647
|
+
const body = init?.body ? JSON.parse(init.body) : void 0;
|
|
1648
|
+
const agentName = headers[AGENT_NAME_HEADER];
|
|
1649
|
+
const stepName = agentName ? `Call Agent ${agentName}` : "Call Agent";
|
|
1650
|
+
const responseInfo = await context.call(stepName, {
|
|
1651
|
+
url: input.toString(),
|
|
1652
|
+
method: init?.method,
|
|
1653
|
+
headers,
|
|
1654
|
+
body
|
|
1655
|
+
});
|
|
1656
|
+
const responseHeaders = new Headers(
|
|
1657
|
+
Object.entries(responseInfo.header).reduce(
|
|
1658
|
+
(acc, [key, values]) => {
|
|
1659
|
+
acc[key] = values.join(", ");
|
|
1660
|
+
return acc;
|
|
1661
|
+
},
|
|
1662
|
+
{}
|
|
1663
|
+
)
|
|
1664
|
+
);
|
|
1665
|
+
return new Response(JSON.stringify(responseInfo.body), {
|
|
1666
|
+
status: responseInfo.status,
|
|
1667
|
+
headers: responseHeaders
|
|
1668
|
+
});
|
|
1669
|
+
} catch (error) {
|
|
1670
|
+
if (error instanceof Error && error.name === "WorkflowAbort") {
|
|
1671
|
+
throw error;
|
|
1672
|
+
} else {
|
|
1673
|
+
console.error("Error in fetch implementation:", error);
|
|
1674
|
+
throw error;
|
|
1675
|
+
}
|
|
1676
|
+
}
|
|
1677
|
+
}
|
|
1678
|
+
});
|
|
1679
|
+
};
|
|
1680
|
+
var wrapTools = ({
|
|
1681
|
+
context,
|
|
1682
|
+
tools
|
|
1683
|
+
}) => {
|
|
1684
|
+
return Object.fromEntries(
|
|
1685
|
+
Object.entries(tools).map((toolInfo) => {
|
|
1686
|
+
const [toolName, tool3] = toolInfo;
|
|
1687
|
+
const aiSDKTool = convertToAISDKTool(tool3);
|
|
1688
|
+
const execute = aiSDKTool.execute;
|
|
1689
|
+
if (execute) {
|
|
1690
|
+
const wrappedExecute = (...params) => {
|
|
1691
|
+
return context.run(`Run tool ${toolName}`, () => execute(...params));
|
|
1692
|
+
};
|
|
1693
|
+
aiSDKTool.execute = wrappedExecute;
|
|
1694
|
+
}
|
|
1695
|
+
return [toolName, aiSDKTool];
|
|
1696
|
+
})
|
|
1697
|
+
);
|
|
1698
|
+
};
|
|
1699
|
+
var convertToAISDKTool = (tool3) => {
|
|
1700
|
+
const isLangchainTool = "invoke" in tool3;
|
|
1701
|
+
return isLangchainTool ? convertLangchainTool(tool3) : tool3;
|
|
1702
|
+
};
|
|
1703
|
+
var convertLangchainTool = (langchainTool) => {
|
|
1704
|
+
return (0, import_ai.tool)({
|
|
1705
|
+
description: langchainTool.description,
|
|
1706
|
+
parameters: langchainTool.schema,
|
|
1707
|
+
execute: async (...param) => langchainTool.invoke(...param)
|
|
1708
|
+
});
|
|
1709
|
+
};
|
|
1710
|
+
|
|
1711
|
+
// src/agents/agent.ts
|
|
1712
|
+
var import_zod = require("zod");
|
|
1713
|
+
var import_ai2 = require("ai");
|
|
1714
|
+
var Agent = class {
|
|
1715
|
+
name;
|
|
1716
|
+
tools;
|
|
1717
|
+
maxSteps;
|
|
1718
|
+
background;
|
|
1719
|
+
model;
|
|
1720
|
+
temparature;
|
|
1721
|
+
constructor({ tools, maxSteps, background, name, model, temparature = 0.1 }) {
|
|
1722
|
+
this.name = name;
|
|
1723
|
+
this.tools = tools ?? {};
|
|
1724
|
+
this.maxSteps = maxSteps;
|
|
1725
|
+
this.background = background;
|
|
1726
|
+
this.model = model;
|
|
1727
|
+
this.temparature = temparature;
|
|
1728
|
+
}
|
|
1729
|
+
/**
|
|
1730
|
+
* Trigger the agent by passing a prompt
|
|
1731
|
+
*
|
|
1732
|
+
* @param prompt task to assign to the agent
|
|
1733
|
+
* @returns Response as `{ text: string }`
|
|
1734
|
+
*/
|
|
1735
|
+
async call({ prompt }) {
|
|
1736
|
+
try {
|
|
1737
|
+
const result = await (0, import_ai2.generateText)({
|
|
1738
|
+
model: this.model,
|
|
1739
|
+
tools: this.tools,
|
|
1740
|
+
maxSteps: this.maxSteps,
|
|
1741
|
+
system: this.background,
|
|
1742
|
+
prompt,
|
|
1743
|
+
headers: {
|
|
1744
|
+
[AGENT_NAME_HEADER]: this.name
|
|
1745
|
+
},
|
|
1746
|
+
temperature: this.temparature
|
|
1747
|
+
});
|
|
1748
|
+
return { text: result.text };
|
|
1749
|
+
} catch (error) {
|
|
1750
|
+
if (error instanceof import_ai2.ToolExecutionError) {
|
|
1751
|
+
if (error.cause instanceof Error && error.cause.name === "WorkflowAbort") {
|
|
1752
|
+
throw error.cause;
|
|
1753
|
+
} else if (error.cause instanceof import_ai2.ToolExecutionError && error.cause.cause instanceof Error && error.cause.cause.name === "WorkflowAbort") {
|
|
1754
|
+
throw error.cause.cause;
|
|
1755
|
+
} else {
|
|
1756
|
+
throw error;
|
|
1757
|
+
}
|
|
1758
|
+
} else {
|
|
1759
|
+
throw error;
|
|
1760
|
+
}
|
|
1761
|
+
}
|
|
1762
|
+
}
|
|
1763
|
+
/**
|
|
1764
|
+
* Convert the agent to a tool which can be used by other agents.
|
|
1765
|
+
*
|
|
1766
|
+
* @returns the agent as a tool
|
|
1767
|
+
*/
|
|
1768
|
+
asTool() {
|
|
1769
|
+
const toolDescriptions = Object.values(this.tools).map((tool3) => tool3.description).join("\n");
|
|
1770
|
+
return (0, import_ai2.tool)({
|
|
1771
|
+
parameters: import_zod.z.object({ prompt: import_zod.z.string() }),
|
|
1772
|
+
execute: async ({ prompt }) => {
|
|
1773
|
+
return await this.call({ prompt });
|
|
1774
|
+
},
|
|
1775
|
+
description: `An AI Agent with the following background: ${this.background}Has access to the following tools: ${toolDescriptions}`
|
|
1776
|
+
});
|
|
1777
|
+
}
|
|
1778
|
+
};
|
|
1779
|
+
var ManagerAgent = class extends Agent {
|
|
1780
|
+
agents;
|
|
1781
|
+
/**
|
|
1782
|
+
* A manager agent which coordinates agents available to it to achieve a
|
|
1783
|
+
* given task
|
|
1784
|
+
*
|
|
1785
|
+
* @param name Name of the agent
|
|
1786
|
+
* @param background Background of the agent. If not passed, default will be used.
|
|
1787
|
+
* @param model LLM model to use
|
|
1788
|
+
* @param agents: List of agents available to the agent
|
|
1789
|
+
* @param maxSteps number of times the manager agent can call the LLM at most.
|
|
1790
|
+
* If the agent abruptly stops execution after calling other agents, you may
|
|
1791
|
+
* need to increase maxSteps
|
|
1792
|
+
*/
|
|
1793
|
+
constructor({
|
|
1794
|
+
agents,
|
|
1795
|
+
background = MANAGER_AGENT_PROMPT,
|
|
1796
|
+
model,
|
|
1797
|
+
maxSteps,
|
|
1798
|
+
name = "manager llm"
|
|
1799
|
+
}) {
|
|
1800
|
+
super({
|
|
1801
|
+
background,
|
|
1802
|
+
maxSteps,
|
|
1803
|
+
tools: Object.fromEntries(agents.map((agent) => [agent.name, agent.asTool()])),
|
|
1804
|
+
name,
|
|
1805
|
+
model
|
|
1806
|
+
});
|
|
1807
|
+
this.agents = agents;
|
|
1808
|
+
}
|
|
1809
|
+
};
|
|
1810
|
+
|
|
1811
|
+
// src/agents/task.ts
|
|
1812
|
+
var Task = class {
|
|
1813
|
+
context;
|
|
1814
|
+
taskParameters;
|
|
1815
|
+
constructor({
|
|
1816
|
+
context,
|
|
1817
|
+
taskParameters
|
|
1818
|
+
}) {
|
|
1819
|
+
this.context = context;
|
|
1820
|
+
this.taskParameters = taskParameters;
|
|
1821
|
+
}
|
|
1822
|
+
/**
|
|
1823
|
+
* Run the agents to complete the task
|
|
1824
|
+
*
|
|
1825
|
+
* @returns Result of the task as { text: string }
|
|
1826
|
+
*/
|
|
1827
|
+
async run() {
|
|
1828
|
+
const { prompt, ...otherParams } = this.taskParameters;
|
|
1829
|
+
const safePrompt = await this.context.run("Get Prompt", () => prompt);
|
|
1830
|
+
if ("agent" in otherParams) {
|
|
1831
|
+
const agent = otherParams.agent;
|
|
1832
|
+
const result = await agent.call({
|
|
1833
|
+
prompt: safePrompt
|
|
1834
|
+
});
|
|
1835
|
+
return { text: result.text };
|
|
1836
|
+
} else {
|
|
1837
|
+
const { agents, maxSteps, model, background } = otherParams;
|
|
1838
|
+
const managerAgent = new ManagerAgent({
|
|
1839
|
+
model,
|
|
1840
|
+
maxSteps,
|
|
1841
|
+
agents,
|
|
1842
|
+
name: "Manager LLM",
|
|
1843
|
+
background
|
|
1844
|
+
});
|
|
1845
|
+
const result = await managerAgent.call({ prompt: safePrompt });
|
|
1846
|
+
return { text: result.text };
|
|
1847
|
+
}
|
|
1848
|
+
}
|
|
1849
|
+
};
|
|
1850
|
+
|
|
1851
|
+
// src/agents/index.ts
|
|
1852
|
+
var WorkflowAgents = class {
|
|
1853
|
+
context;
|
|
1854
|
+
constructor({ context }) {
|
|
1855
|
+
this.context = context;
|
|
1856
|
+
}
|
|
1857
|
+
/**
|
|
1858
|
+
* Defines an agent
|
|
1859
|
+
*
|
|
1860
|
+
* ```ts
|
|
1861
|
+
* const researcherAgent = context.agents.agent({
|
|
1862
|
+
* model,
|
|
1863
|
+
* name: 'academic',
|
|
1864
|
+
* maxSteps: 2,
|
|
1865
|
+
* tools: {
|
|
1866
|
+
* wikiTool: new WikipediaQueryRun({
|
|
1867
|
+
* topKResults: 1,
|
|
1868
|
+
* maxDocContentLength: 500,
|
|
1869
|
+
* })
|
|
1870
|
+
* },
|
|
1871
|
+
* background:
|
|
1872
|
+
* 'You are researcher agent with access to Wikipedia. ' +
|
|
1873
|
+
* 'Utilize Wikipedia as much as possible for correct information',
|
|
1874
|
+
* });
|
|
1875
|
+
* ```
|
|
1876
|
+
*
|
|
1877
|
+
* @param params agent parameters
|
|
1878
|
+
* @returns
|
|
1879
|
+
*/
|
|
1880
|
+
agent(params) {
|
|
1881
|
+
const wrappedTools = wrapTools({ context: this.context, tools: params.tools });
|
|
1882
|
+
return new Agent({
|
|
1883
|
+
...params,
|
|
1884
|
+
tools: wrappedTools
|
|
1885
|
+
});
|
|
1886
|
+
}
|
|
1887
|
+
task(taskParameters) {
|
|
1888
|
+
return new Task({ context: this.context, taskParameters });
|
|
1889
|
+
}
|
|
1890
|
+
/**
|
|
1891
|
+
* creates an openai model for agents
|
|
1892
|
+
*/
|
|
1893
|
+
openai(...params) {
|
|
1894
|
+
const openai2 = createWorkflowOpenAI(this.context);
|
|
1895
|
+
return openai2(...params);
|
|
1896
|
+
}
|
|
1897
|
+
};
|
|
1898
|
+
|
|
1635
1899
|
// src/context/context.ts
|
|
1636
1900
|
var WorkflowContext = class {
|
|
1637
1901
|
executor;
|
|
@@ -2017,6 +2281,11 @@ var WorkflowContext = class {
|
|
|
2017
2281
|
context: this
|
|
2018
2282
|
});
|
|
2019
2283
|
}
|
|
2284
|
+
get agents() {
|
|
2285
|
+
return new WorkflowAgents({
|
|
2286
|
+
context: this
|
|
2287
|
+
});
|
|
2288
|
+
}
|
|
2020
2289
|
};
|
|
2021
2290
|
|
|
2022
2291
|
// src/logger.ts
|