@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
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
8
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
9
|
+
}) : x)(function(x) {
|
|
10
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
11
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
12
|
+
});
|
|
13
|
+
var __commonJS = (cb, mod) => function __require2() {
|
|
14
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
15
|
+
};
|
|
16
|
+
var __copyProps = (to, from, except, desc) => {
|
|
17
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
18
|
+
for (let key of __getOwnPropNames(from))
|
|
19
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
20
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
21
|
+
}
|
|
22
|
+
return to;
|
|
23
|
+
};
|
|
24
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
25
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
26
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
27
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
28
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
29
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
30
|
+
mod
|
|
31
|
+
));
|
|
32
|
+
|
|
33
|
+
// src/agents/adapters.ts
|
|
34
|
+
import { createOpenAI } from "@ai-sdk/openai";
|
|
35
|
+
import { tool } from "ai";
|
|
36
|
+
var AGENT_NAME_HEADER = "upstash-agent-name";
|
|
37
|
+
var createWorkflowOpenAI = (context) => {
|
|
38
|
+
return createOpenAI({
|
|
39
|
+
compatibility: "strict",
|
|
40
|
+
fetch: async (input, init) => {
|
|
41
|
+
try {
|
|
42
|
+
const headers = init?.headers ? Object.fromEntries(new Headers(init.headers).entries()) : {};
|
|
43
|
+
const body = init?.body ? JSON.parse(init.body) : void 0;
|
|
44
|
+
const agentName = headers[AGENT_NAME_HEADER];
|
|
45
|
+
const stepName = agentName ? `Call Agent ${agentName}` : "Call Agent";
|
|
46
|
+
const responseInfo = await context.call(stepName, {
|
|
47
|
+
url: input.toString(),
|
|
48
|
+
method: init?.method,
|
|
49
|
+
headers,
|
|
50
|
+
body
|
|
51
|
+
});
|
|
52
|
+
const responseHeaders = new Headers(
|
|
53
|
+
Object.entries(responseInfo.header).reduce(
|
|
54
|
+
(acc, [key, values]) => {
|
|
55
|
+
acc[key] = values.join(", ");
|
|
56
|
+
return acc;
|
|
57
|
+
},
|
|
58
|
+
{}
|
|
59
|
+
)
|
|
60
|
+
);
|
|
61
|
+
return new Response(JSON.stringify(responseInfo.body), {
|
|
62
|
+
status: responseInfo.status,
|
|
63
|
+
headers: responseHeaders
|
|
64
|
+
});
|
|
65
|
+
} catch (error) {
|
|
66
|
+
if (error instanceof Error && error.name === "WorkflowAbort") {
|
|
67
|
+
throw error;
|
|
68
|
+
} else {
|
|
69
|
+
console.error("Error in fetch implementation:", error);
|
|
70
|
+
throw error;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
};
|
|
76
|
+
var wrapTools = ({
|
|
77
|
+
context,
|
|
78
|
+
tools
|
|
79
|
+
}) => {
|
|
80
|
+
return Object.fromEntries(
|
|
81
|
+
Object.entries(tools).map((toolInfo) => {
|
|
82
|
+
const [toolName, tool3] = toolInfo;
|
|
83
|
+
const aiSDKTool = convertToAISDKTool(tool3);
|
|
84
|
+
const execute = aiSDKTool.execute;
|
|
85
|
+
if (execute) {
|
|
86
|
+
const wrappedExecute = (...params) => {
|
|
87
|
+
return context.run(`Run tool ${toolName}`, () => execute(...params));
|
|
88
|
+
};
|
|
89
|
+
aiSDKTool.execute = wrappedExecute;
|
|
90
|
+
}
|
|
91
|
+
return [toolName, aiSDKTool];
|
|
92
|
+
})
|
|
93
|
+
);
|
|
94
|
+
};
|
|
95
|
+
var convertToAISDKTool = (tool3) => {
|
|
96
|
+
const isLangchainTool = "invoke" in tool3;
|
|
97
|
+
return isLangchainTool ? convertLangchainTool(tool3) : tool3;
|
|
98
|
+
};
|
|
99
|
+
var convertLangchainTool = (langchainTool) => {
|
|
100
|
+
return tool({
|
|
101
|
+
description: langchainTool.description,
|
|
102
|
+
parameters: langchainTool.schema,
|
|
103
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
104
|
+
execute: async (param) => langchainTool.invoke(param)
|
|
105
|
+
});
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
// src/agents/agent.ts
|
|
109
|
+
import { z } from "zod";
|
|
110
|
+
import { generateText, tool as tool2, ToolExecutionError } from "ai";
|
|
111
|
+
var Agent = class {
|
|
112
|
+
name;
|
|
113
|
+
tools;
|
|
114
|
+
maxSteps;
|
|
115
|
+
background;
|
|
116
|
+
model;
|
|
117
|
+
constructor({ tools, maxSteps, background, name, model }) {
|
|
118
|
+
this.name = name;
|
|
119
|
+
this.tools = tools ?? {};
|
|
120
|
+
this.maxSteps = maxSteps;
|
|
121
|
+
this.background = background;
|
|
122
|
+
this.model = model;
|
|
123
|
+
}
|
|
124
|
+
async call({ prompt }) {
|
|
125
|
+
try {
|
|
126
|
+
return await generateText({
|
|
127
|
+
model: this.model,
|
|
128
|
+
tools: this.tools,
|
|
129
|
+
maxSteps: this.maxSteps,
|
|
130
|
+
system: this.background,
|
|
131
|
+
prompt,
|
|
132
|
+
headers: {
|
|
133
|
+
[AGENT_NAME_HEADER]: this.name
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
} catch (error) {
|
|
137
|
+
if (error instanceof ToolExecutionError) {
|
|
138
|
+
if (error.cause instanceof Error && error.cause.name === "WorkflowAbort") {
|
|
139
|
+
throw error.cause;
|
|
140
|
+
} else if (error.cause instanceof ToolExecutionError && error.cause.cause instanceof Error && error.cause.cause.name === "WorkflowAbort") {
|
|
141
|
+
throw error.cause.cause;
|
|
142
|
+
} else {
|
|
143
|
+
throw error;
|
|
144
|
+
}
|
|
145
|
+
} else {
|
|
146
|
+
throw error;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
asTool() {
|
|
151
|
+
const toolDescriptions = Object.values(this.tools).map((tool3) => tool3.description).join("\n");
|
|
152
|
+
return tool2({
|
|
153
|
+
parameters: z.object({ prompt: z.string() }),
|
|
154
|
+
execute: async ({ prompt }) => {
|
|
155
|
+
return await this.call({ prompt });
|
|
156
|
+
},
|
|
157
|
+
description: `An AI Agent with the following background: ${this.background}Has access to the following tools: ${toolDescriptions}`
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
var MANAGER_AGENT_PROMPT = `You are an AI agent who orchestrates other AI Agents.
|
|
162
|
+
These other agents have tools available to them.
|
|
163
|
+
Given a prompt, utilize these agents to address requests.
|
|
164
|
+
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.
|
|
165
|
+
`;
|
|
166
|
+
var ManagerAgent = class extends Agent {
|
|
167
|
+
agents;
|
|
168
|
+
constructor({
|
|
169
|
+
maxSteps,
|
|
170
|
+
background = MANAGER_AGENT_PROMPT,
|
|
171
|
+
agents,
|
|
172
|
+
model,
|
|
173
|
+
name = "manager llm"
|
|
174
|
+
}) {
|
|
175
|
+
super({
|
|
176
|
+
background,
|
|
177
|
+
maxSteps,
|
|
178
|
+
tools: Object.fromEntries(agents.map((agent) => [agent.name, agent.asTool()])),
|
|
179
|
+
name,
|
|
180
|
+
model
|
|
181
|
+
});
|
|
182
|
+
this.agents = agents;
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
// src/agents/task.ts
|
|
187
|
+
var Task = class {
|
|
188
|
+
context;
|
|
189
|
+
taskParameters;
|
|
190
|
+
constructor({
|
|
191
|
+
context,
|
|
192
|
+
taskParameters
|
|
193
|
+
}) {
|
|
194
|
+
this.context = context;
|
|
195
|
+
this.taskParameters = taskParameters;
|
|
196
|
+
}
|
|
197
|
+
async run() {
|
|
198
|
+
const { prompt, ...otherParams } = this.taskParameters;
|
|
199
|
+
const safePrompt = await this.context.run("Get Prompt", () => prompt);
|
|
200
|
+
if ("agent" in otherParams) {
|
|
201
|
+
const agent = otherParams.agent;
|
|
202
|
+
const result = await agent.call({
|
|
203
|
+
prompt: safePrompt
|
|
204
|
+
});
|
|
205
|
+
return { text: result.text };
|
|
206
|
+
} else {
|
|
207
|
+
const { agents, maxSteps, model, background } = otherParams;
|
|
208
|
+
const managerAgent = new ManagerAgent({
|
|
209
|
+
model,
|
|
210
|
+
maxSteps,
|
|
211
|
+
agents,
|
|
212
|
+
name: "Manager LLM",
|
|
213
|
+
background
|
|
214
|
+
});
|
|
215
|
+
const result = await managerAgent.call({ prompt: safePrompt });
|
|
216
|
+
return { text: result.text };
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
// src/agents/index.ts
|
|
222
|
+
var WorkflowAgents = class {
|
|
223
|
+
context;
|
|
224
|
+
constructor({ context }) {
|
|
225
|
+
this.context = context;
|
|
226
|
+
}
|
|
227
|
+
agent(params) {
|
|
228
|
+
const wrappedTools = wrapTools({ context: this.context, tools: params.tools });
|
|
229
|
+
return new Agent({
|
|
230
|
+
...params,
|
|
231
|
+
tools: wrappedTools
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
task(taskParameters) {
|
|
235
|
+
return new Task({ context: this.context, taskParameters });
|
|
236
|
+
}
|
|
237
|
+
openai(...params) {
|
|
238
|
+
const openai = createWorkflowOpenAI(this.context);
|
|
239
|
+
return openai(...params);
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
export {
|
|
244
|
+
__require,
|
|
245
|
+
__commonJS,
|
|
246
|
+
__toESM,
|
|
247
|
+
createWorkflowOpenAI,
|
|
248
|
+
Agent,
|
|
249
|
+
ManagerAgent,
|
|
250
|
+
WorkflowAgents
|
|
251
|
+
};
|
|
@@ -1,34 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
-
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
8
|
-
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
9
|
-
}) : x)(function(x) {
|
|
10
|
-
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
11
|
-
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
12
|
-
});
|
|
13
|
-
var __commonJS = (cb, mod) => function __require2() {
|
|
14
|
-
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
15
|
-
};
|
|
16
|
-
var __copyProps = (to, from, except, desc) => {
|
|
17
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
18
|
-
for (let key of __getOwnPropNames(from))
|
|
19
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
20
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
21
|
-
}
|
|
22
|
-
return to;
|
|
23
|
-
};
|
|
24
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
25
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
26
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
27
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
28
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
29
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
30
|
-
mod
|
|
31
|
-
));
|
|
1
|
+
import {
|
|
2
|
+
WorkflowAgents
|
|
3
|
+
} from "./chunk-PU5J4TNC.mjs";
|
|
32
4
|
|
|
33
5
|
// src/constants.ts
|
|
34
6
|
var WORKFLOW_ID_HEADER = "Upstash-Workflow-RunId";
|
|
@@ -2030,6 +2002,11 @@ var WorkflowContext = class {
|
|
|
2030
2002
|
context: this
|
|
2031
2003
|
});
|
|
2032
2004
|
}
|
|
2005
|
+
get agents() {
|
|
2006
|
+
return new WorkflowAgents({
|
|
2007
|
+
context: this
|
|
2008
|
+
});
|
|
2009
|
+
}
|
|
2033
2010
|
};
|
|
2034
2011
|
|
|
2035
2012
|
// src/logger.ts
|
|
@@ -2234,6 +2211,7 @@ var checkIfLastOneIsDuplicate = async (steps, debug) => {
|
|
|
2234
2211
|
if (step.stepId === lastStepId && step.targetStep === lastTargetStepId) {
|
|
2235
2212
|
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
2213
|
await debug?.log("WARN", "RESPONSE_DEFAULT", message);
|
|
2214
|
+
console.log(steps);
|
|
2237
2215
|
console.warn(message);
|
|
2238
2216
|
return true;
|
|
2239
2217
|
}
|
|
@@ -2593,9 +2571,6 @@ var serve = (routeFunction, options) => {
|
|
|
2593
2571
|
};
|
|
2594
2572
|
|
|
2595
2573
|
export {
|
|
2596
|
-
__require,
|
|
2597
|
-
__commonJS,
|
|
2598
|
-
__toESM,
|
|
2599
2574
|
makeNotifyRequest,
|
|
2600
2575
|
makeGetWaitersRequest,
|
|
2601
2576
|
SDK_TELEMETRY,
|
package/cloudflare.d.mts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
import { R as RouteFunction, j as PublicServeOptions } from './types-
|
|
1
|
+
import { R as RouteFunction, j as PublicServeOptions } from './types-BEyIoCRe.mjs';
|
|
2
2
|
import '@upstash/qstash';
|
|
3
|
+
import 'ai';
|
|
4
|
+
import '@ai-sdk/openai';
|
|
5
|
+
import 'langchain/tools';
|
|
3
6
|
|
|
4
7
|
type WorkflowBindings = {
|
|
5
8
|
QSTASH_TOKEN: string;
|
package/cloudflare.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
import { R as RouteFunction, j as PublicServeOptions } from './types-
|
|
1
|
+
import { R as RouteFunction, j as PublicServeOptions } from './types-BEyIoCRe.js';
|
|
2
2
|
import '@upstash/qstash';
|
|
3
|
+
import 'ai';
|
|
4
|
+
import '@ai-sdk/openai';
|
|
5
|
+
import 'langchain/tools';
|
|
3
6
|
|
|
4
7
|
type WorkflowBindings = {
|
|
5
8
|
QSTASH_TOKEN: string;
|
package/cloudflare.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/cloudflare.mjs
CHANGED
package/express.d.mts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
import { R as RouteFunction, W as WorkflowServeOptions } from './types-
|
|
1
|
+
import { R as RouteFunction, W as WorkflowServeOptions } from './types-BEyIoCRe.mjs';
|
|
2
2
|
import { Router } from 'express';
|
|
3
3
|
import '@upstash/qstash';
|
|
4
|
+
import 'ai';
|
|
5
|
+
import '@ai-sdk/openai';
|
|
6
|
+
import 'langchain/tools';
|
|
4
7
|
|
|
5
8
|
declare function serve<TInitialPayload = unknown>(routeFunction: RouteFunction<TInitialPayload>, options?: Omit<WorkflowServeOptions<globalThis.Response, TInitialPayload>, "onStepFinish">): Router;
|
|
6
9
|
|
package/express.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
import { R as RouteFunction, W as WorkflowServeOptions } from './types-
|
|
1
|
+
import { R as RouteFunction, W as WorkflowServeOptions } from './types-BEyIoCRe.js';
|
|
2
2
|
import { Router } from 'express';
|
|
3
3
|
import '@upstash/qstash';
|
|
4
|
+
import 'ai';
|
|
5
|
+
import '@ai-sdk/openai';
|
|
6
|
+
import 'langchain/tools';
|
|
4
7
|
|
|
5
8
|
declare function serve<TInitialPayload = unknown>(routeFunction: RouteFunction<TInitialPayload>, options?: Omit<WorkflowServeOptions<globalThis.Response, TInitialPayload>, "onStepFinish">): Router;
|
|
6
9
|
|