@upstash/workflow 0.2.22-rc → 0.3.0-rc
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/README.md +39 -0
- package/astro.d.mts +3 -5
- package/astro.d.ts +3 -5
- package/astro.js +126 -397
- package/astro.mjs +1 -1
- package/{chunk-CAQSUCHB.mjs → chunk-AGYYZKP7.mjs} +94 -400
- package/cloudflare.d.mts +3 -5
- package/cloudflare.d.ts +3 -5
- package/cloudflare.js +126 -397
- package/cloudflare.mjs +1 -1
- package/express.d.mts +3 -5
- package/express.d.ts +3 -5
- package/express.js +128 -398
- package/express.mjs +3 -2
- package/h3.d.mts +3 -5
- package/h3.d.ts +3 -5
- package/h3.js +136 -410
- package/h3.mjs +11 -14
- package/hono.d.mts +4 -6
- package/hono.d.ts +4 -6
- package/hono.js +126 -397
- package/hono.mjs +1 -1
- package/index.d.mts +11 -5
- package/index.d.ts +11 -5
- package/index.js +130 -433
- package/index.mjs +5 -6
- package/nextjs.d.mts +4 -6
- package/nextjs.d.ts +4 -6
- package/nextjs.js +128 -398
- package/nextjs.mjs +3 -2
- package/package.json +1 -1
- package/{serve-many-BNusWYgt.d.mts → serve-many-DEwKPF6H.d.mts} +1 -1
- package/{serve-many-CXqQP3RI.d.ts → serve-many-DVtHRxeg.d.ts} +1 -1
- package/solidjs.d.mts +1 -3
- package/solidjs.d.ts +1 -3
- package/solidjs.js +126 -397
- package/solidjs.mjs +1 -1
- package/svelte.d.mts +4 -6
- package/svelte.d.ts +4 -6
- package/svelte.js +126 -397
- package/svelte.mjs +1 -1
- package/tanstack.d.mts +3 -5
- package/tanstack.d.ts +3 -5
- package/tanstack.js +126 -397
- package/tanstack.mjs +1 -1
- package/{types-Q3dM0UlR.d.ts → types-DESkn7K9.d.mts} +15 -306
- package/{types-Q3dM0UlR.d.mts → types-DESkn7K9.d.ts} +15 -306
package/solidjs.js
CHANGED
|
@@ -25,7 +25,95 @@ __export(solidjs_exports, {
|
|
|
25
25
|
module.exports = __toCommonJS(solidjs_exports);
|
|
26
26
|
|
|
27
27
|
// src/client/utils.ts
|
|
28
|
+
var import_qstash2 = require("@upstash/qstash");
|
|
29
|
+
|
|
30
|
+
// src/error.ts
|
|
28
31
|
var import_qstash = require("@upstash/qstash");
|
|
32
|
+
var WorkflowError = class extends import_qstash.QstashError {
|
|
33
|
+
constructor(message) {
|
|
34
|
+
super(message);
|
|
35
|
+
this.name = "WorkflowError";
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var WorkflowAbort = class extends Error {
|
|
39
|
+
stepInfo;
|
|
40
|
+
stepName;
|
|
41
|
+
/**
|
|
42
|
+
* whether workflow is to be canceled on abort
|
|
43
|
+
*/
|
|
44
|
+
cancelWorkflow;
|
|
45
|
+
/**
|
|
46
|
+
*
|
|
47
|
+
* @param stepName name of the aborting step
|
|
48
|
+
* @param stepInfo step information
|
|
49
|
+
* @param cancelWorkflow
|
|
50
|
+
*/
|
|
51
|
+
constructor(stepName, stepInfo, cancelWorkflow = false) {
|
|
52
|
+
super(
|
|
53
|
+
`This is an Upstash Workflow error thrown after a step executes. It is expected to be raised. Make sure that you await for each step. Also, if you are using try/catch blocks, you should not wrap context.run/sleep/sleepUntil/call methods with try/catch. Aborting workflow after executing step '${stepName}'.`
|
|
54
|
+
);
|
|
55
|
+
this.name = "WorkflowAbort";
|
|
56
|
+
this.stepName = stepName;
|
|
57
|
+
this.stepInfo = stepInfo;
|
|
58
|
+
this.cancelWorkflow = cancelWorkflow;
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
var WorkflowNonRetryableError = class extends WorkflowAbort {
|
|
62
|
+
/**
|
|
63
|
+
* @param message error message to be displayed
|
|
64
|
+
*/
|
|
65
|
+
constructor(message) {
|
|
66
|
+
super("fail", void 0, false);
|
|
67
|
+
this.name = "WorkflowNonRetryableError";
|
|
68
|
+
if (message) this.message = message;
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
var WorkflowRetryAfterError = class extends WorkflowAbort {
|
|
72
|
+
retryAfter;
|
|
73
|
+
/**
|
|
74
|
+
* @param retryAfter time in seconds after which the workflow should be retried
|
|
75
|
+
* @param message error message to be displayed
|
|
76
|
+
*/
|
|
77
|
+
constructor(message, retryAfter) {
|
|
78
|
+
super("retry", void 0, false);
|
|
79
|
+
this.name = "WorkflowRetryAfterError";
|
|
80
|
+
this.retryAfter = retryAfter;
|
|
81
|
+
if (message) this.message = message;
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
var formatWorkflowError = (error) => {
|
|
85
|
+
return error instanceof Error ? {
|
|
86
|
+
error: error.name,
|
|
87
|
+
message: error.message,
|
|
88
|
+
stack: error.stack
|
|
89
|
+
} : {
|
|
90
|
+
error: "Error",
|
|
91
|
+
message: `An error occured while executing workflow: '${typeof error === "string" ? error : JSON.stringify(error)}'`
|
|
92
|
+
};
|
|
93
|
+
};
|
|
94
|
+
function getConstructorName(obj) {
|
|
95
|
+
if (obj === null || obj === void 0) {
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
const ctor = obj.constructor;
|
|
99
|
+
if (!ctor || ctor.name === "Object") {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
return ctor.name;
|
|
103
|
+
}
|
|
104
|
+
function getConstructorNames(obj) {
|
|
105
|
+
const proto = Object.getPrototypeOf(obj);
|
|
106
|
+
const name = getConstructorName(proto);
|
|
107
|
+
if (name === null) {
|
|
108
|
+
return [];
|
|
109
|
+
}
|
|
110
|
+
return [name, ...getConstructorNames(proto)];
|
|
111
|
+
}
|
|
112
|
+
function isInstanceOf(v, ctor) {
|
|
113
|
+
return getConstructorNames(v).includes(ctor.name);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// src/client/utils.ts
|
|
29
117
|
var makeNotifyRequest = async (requester, eventId, eventData) => {
|
|
30
118
|
const result = await requester.request({
|
|
31
119
|
path: ["v2", "notify", eventId],
|
|
@@ -65,7 +153,7 @@ var getSteps = async (requester, workflowRunId, messageId, debug) => {
|
|
|
65
153
|
return { steps: filteredSteps, workflowRunEnded: false };
|
|
66
154
|
}
|
|
67
155
|
} catch (error) {
|
|
68
|
-
if (error
|
|
156
|
+
if (isInstanceOf(error, import_qstash2.QstashError) && error.status === 404) {
|
|
69
157
|
await debug?.log("WARN", "ENDPOINT_START", {
|
|
70
158
|
message: "Couldn't fetch workflow run steps. This can happen if the workflow run succesfully ends before some callback is executed.",
|
|
71
159
|
error
|
|
@@ -90,64 +178,11 @@ var WORKFLOW_PROTOCOL_VERSION_HEADER = "Upstash-Workflow-Sdk-Version";
|
|
|
90
178
|
var DEFAULT_CONTENT_TYPE = "application/json";
|
|
91
179
|
var NO_CONCURRENCY = 1;
|
|
92
180
|
var DEFAULT_RETRIES = 3;
|
|
93
|
-
var VERSION = "v0.
|
|
181
|
+
var VERSION = "v0.3.0-rc";
|
|
94
182
|
var SDK_TELEMETRY = `@upstash/workflow@${VERSION}`;
|
|
95
183
|
var TELEMETRY_HEADER_SDK = "Upstash-Telemetry-Sdk";
|
|
96
184
|
var TELEMETRY_HEADER_FRAMEWORK = "Upstash-Telemetry-Framework";
|
|
97
185
|
var TELEMETRY_HEADER_RUNTIME = "Upstash-Telemetry-Runtime";
|
|
98
|
-
var TELEMETRY_HEADER_AGENT = "Upstash-Telemetry-Agent";
|
|
99
|
-
|
|
100
|
-
// src/error.ts
|
|
101
|
-
var import_qstash2 = require("@upstash/qstash");
|
|
102
|
-
var WorkflowError = class extends import_qstash2.QstashError {
|
|
103
|
-
constructor(message) {
|
|
104
|
-
super(message);
|
|
105
|
-
this.name = "WorkflowError";
|
|
106
|
-
}
|
|
107
|
-
};
|
|
108
|
-
var WorkflowAbort = class extends Error {
|
|
109
|
-
stepInfo;
|
|
110
|
-
stepName;
|
|
111
|
-
/**
|
|
112
|
-
* whether workflow is to be canceled on abort
|
|
113
|
-
*/
|
|
114
|
-
cancelWorkflow;
|
|
115
|
-
/**
|
|
116
|
-
*
|
|
117
|
-
* @param stepName name of the aborting step
|
|
118
|
-
* @param stepInfo step information
|
|
119
|
-
* @param cancelWorkflow
|
|
120
|
-
*/
|
|
121
|
-
constructor(stepName, stepInfo, cancelWorkflow = false) {
|
|
122
|
-
super(
|
|
123
|
-
`This is an Upstash Workflow error thrown after a step executes. It is expected to be raised. Make sure that you await for each step. Also, if you are using try/catch blocks, you should not wrap context.run/sleep/sleepUntil/call methods with try/catch. Aborting workflow after executing step '${stepName}'.`
|
|
124
|
-
);
|
|
125
|
-
this.name = "WorkflowAbort";
|
|
126
|
-
this.stepName = stepName;
|
|
127
|
-
this.stepInfo = stepInfo;
|
|
128
|
-
this.cancelWorkflow = cancelWorkflow;
|
|
129
|
-
}
|
|
130
|
-
};
|
|
131
|
-
var WorkflowNonRetryableError = class extends WorkflowAbort {
|
|
132
|
-
/**
|
|
133
|
-
* @param message error message to be displayed
|
|
134
|
-
*/
|
|
135
|
-
constructor(message) {
|
|
136
|
-
super("fail", void 0, false);
|
|
137
|
-
this.name = "WorkflowNonRetryableError";
|
|
138
|
-
if (message) this.message = message;
|
|
139
|
-
}
|
|
140
|
-
};
|
|
141
|
-
var formatWorkflowError = (error) => {
|
|
142
|
-
return error instanceof Error ? {
|
|
143
|
-
error: error.name,
|
|
144
|
-
message: error.message,
|
|
145
|
-
stack: error.stack
|
|
146
|
-
} : {
|
|
147
|
-
error: "Error",
|
|
148
|
-
message: `An error occured while executing workflow: '${typeof error === "string" ? error : JSON.stringify(error)}'`
|
|
149
|
-
};
|
|
150
|
-
};
|
|
151
186
|
|
|
152
187
|
// src/context/auto-executor.ts
|
|
153
188
|
var import_qstash5 = require("@upstash/qstash");
|
|
@@ -520,9 +555,9 @@ var Ok = class {
|
|
|
520
555
|
}
|
|
521
556
|
safeUnwrap() {
|
|
522
557
|
const value = this.value;
|
|
523
|
-
return function* () {
|
|
558
|
+
return (function* () {
|
|
524
559
|
return value;
|
|
525
|
-
}();
|
|
560
|
+
})();
|
|
526
561
|
}
|
|
527
562
|
_unsafeUnwrap(_) {
|
|
528
563
|
return this.value;
|
|
@@ -581,10 +616,10 @@ var Err = class {
|
|
|
581
616
|
}
|
|
582
617
|
safeUnwrap() {
|
|
583
618
|
const error = this.error;
|
|
584
|
-
return function* () {
|
|
619
|
+
return (function* () {
|
|
585
620
|
yield err(error);
|
|
586
621
|
throw new Error("Do not use this generator out of `safeTry`");
|
|
587
|
-
}();
|
|
622
|
+
})();
|
|
588
623
|
}
|
|
589
624
|
_unsafeUnwrap(config) {
|
|
590
625
|
throw createNeverThrowError("Called `_unsafeUnwrap` on an Err", this, config);
|
|
@@ -708,17 +743,17 @@ var triggerRouteFunction = async ({
|
|
|
708
743
|
return ok("workflow-finished");
|
|
709
744
|
} catch (error) {
|
|
710
745
|
const error_ = error;
|
|
711
|
-
if (error
|
|
746
|
+
if (isInstanceOf(error, import_qstash3.QstashError) && error.status === 400) {
|
|
712
747
|
await debug?.log("WARN", "RESPONSE_WORKFLOW", {
|
|
713
748
|
message: `tried to append to a cancelled workflow. exiting without publishing.`,
|
|
714
749
|
name: error.name,
|
|
715
750
|
errorMessage: error.message
|
|
716
751
|
});
|
|
717
752
|
return ok("workflow-was-finished");
|
|
718
|
-
} else if (
|
|
719
|
-
return err(error_);
|
|
720
|
-
} else if (error_ instanceof WorkflowNonRetryableError) {
|
|
753
|
+
} else if (isInstanceOf(error_, WorkflowNonRetryableError) || isInstanceOf(error_, WorkflowRetryAfterError)) {
|
|
721
754
|
return ok(error_);
|
|
755
|
+
} else if (!isInstanceOf(error_, WorkflowAbort)) {
|
|
756
|
+
return err(error_);
|
|
722
757
|
} else if (error_.cancelWorkflow) {
|
|
723
758
|
await onCancel();
|
|
724
759
|
return ok("workflow-finished");
|
|
@@ -1551,20 +1586,6 @@ var LazyInvokeStep = class extends BaseLazyStep {
|
|
|
1551
1586
|
}
|
|
1552
1587
|
};
|
|
1553
1588
|
|
|
1554
|
-
// src/agents/constants.ts
|
|
1555
|
-
var AGENT_NAME_HEADER = "upstash-agent-name";
|
|
1556
|
-
var MANAGER_AGENT_PROMPT = `You are an agent orchestrating other AI Agents.
|
|
1557
|
-
|
|
1558
|
-
These other agents have tools available to them.
|
|
1559
|
-
|
|
1560
|
-
Given a prompt, utilize these agents to address requests.
|
|
1561
|
-
|
|
1562
|
-
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.
|
|
1563
|
-
|
|
1564
|
-
Avoid calling the same agent twice in one turn. Instead, prefer to call it once but provide everything
|
|
1565
|
-
you need from that agent.
|
|
1566
|
-
`;
|
|
1567
|
-
|
|
1568
1589
|
// src/qstash/headers.ts
|
|
1569
1590
|
var WorkflowHeaders = class {
|
|
1570
1591
|
userHeaders;
|
|
@@ -1613,8 +1634,7 @@ var WorkflowHeaders = class {
|
|
|
1613
1634
|
[WORKFLOW_URL_HEADER]: this.workflowConfig.workflowUrl,
|
|
1614
1635
|
[WORKFLOW_FEATURE_HEADER]: "LazyFetch,InitialBody,WF_DetectTrigger" + (this.keepTriggerConfig ? ",WF_TriggerOnConfig" : ""),
|
|
1615
1636
|
[WORKFLOW_PROTOCOL_VERSION_HEADER]: WORKFLOW_PROTOCOL_VERSION,
|
|
1616
|
-
...this.workflowConfig.telemetry ? getTelemetryHeaders(this.workflowConfig.telemetry) : {}
|
|
1617
|
-
...this.workflowConfig.telemetry && this.stepInfo?.lazyStep instanceof LazyCallStep && this.stepInfo.lazyStep.headers[AGENT_NAME_HEADER] ? { [TELEMETRY_HEADER_AGENT]: "true" } : {}
|
|
1637
|
+
...this.workflowConfig.telemetry ? getTelemetryHeaders(this.workflowConfig.telemetry) : {}
|
|
1618
1638
|
};
|
|
1619
1639
|
if (this.stepInfo?.lazyStep.stepType !== "Call") {
|
|
1620
1640
|
this.headers.rawHeaders[`Upstash-Forward-${WORKFLOW_PROTOCOL_VERSION_HEADER}`] = WORKFLOW_PROTOCOL_VERSION;
|
|
@@ -2025,7 +2045,7 @@ var AutoExecutor = class _AutoExecutor {
|
|
|
2025
2045
|
});
|
|
2026
2046
|
throw new WorkflowAbort(parallelStep.stepName, resultStep);
|
|
2027
2047
|
} catch (error) {
|
|
2028
|
-
if (error
|
|
2048
|
+
if (isInstanceOf(error, WorkflowAbort) || isInstanceOf(error, import_qstash5.QstashError) && error.status === 400) {
|
|
2029
2049
|
throw error;
|
|
2030
2050
|
}
|
|
2031
2051
|
throw new WorkflowError(
|
|
@@ -2132,7 +2152,7 @@ var validateParallelSteps = (lazySteps, stepsFromRequest) => {
|
|
|
2132
2152
|
validateStep(lazySteps[index], stepFromRequest);
|
|
2133
2153
|
}
|
|
2134
2154
|
} catch (error) {
|
|
2135
|
-
if (error
|
|
2155
|
+
if (isInstanceOf(error, WorkflowError)) {
|
|
2136
2156
|
const lazyStepNames = lazySteps.map((lazyStep) => lazyStep.stepName);
|
|
2137
2157
|
const lazyStepTypes = lazySteps.map((lazyStep) => lazyStep.stepType);
|
|
2138
2158
|
const requestStepNames = stepsFromRequest.map((step) => step.stepName);
|
|
@@ -2277,309 +2297,6 @@ var WorkflowApi = class extends BaseWorkflowApi {
|
|
|
2277
2297
|
}
|
|
2278
2298
|
};
|
|
2279
2299
|
|
|
2280
|
-
// src/agents/index.ts
|
|
2281
|
-
var import_openai2 = require("@ai-sdk/openai");
|
|
2282
|
-
|
|
2283
|
-
// src/agents/adapters.ts
|
|
2284
|
-
var import_ai = require("ai");
|
|
2285
|
-
var fetchWithContextCall = async (context, agentCallParams, ...params) => {
|
|
2286
|
-
const [input, init] = params;
|
|
2287
|
-
try {
|
|
2288
|
-
const headers = init?.headers ? Object.fromEntries(new Headers(init.headers).entries()) : {};
|
|
2289
|
-
const body = init?.body ? JSON.parse(init.body) : void 0;
|
|
2290
|
-
const agentName = headers[AGENT_NAME_HEADER];
|
|
2291
|
-
const stepName = agentName ? `Call Agent ${agentName}` : "Call Agent";
|
|
2292
|
-
const responseInfo = await context.call(stepName, {
|
|
2293
|
-
url: input.toString(),
|
|
2294
|
-
method: init?.method,
|
|
2295
|
-
headers,
|
|
2296
|
-
body,
|
|
2297
|
-
timeout: agentCallParams?.timeout,
|
|
2298
|
-
retries: agentCallParams?.retries,
|
|
2299
|
-
retryDelay: agentCallParams?.retryDelay,
|
|
2300
|
-
flowControl: agentCallParams?.flowControl
|
|
2301
|
-
});
|
|
2302
|
-
const responseHeaders = new Headers(
|
|
2303
|
-
Object.entries(responseInfo.header).reduce(
|
|
2304
|
-
(acc, [key, values]) => {
|
|
2305
|
-
acc[key] = values.join(", ");
|
|
2306
|
-
return acc;
|
|
2307
|
-
},
|
|
2308
|
-
{}
|
|
2309
|
-
)
|
|
2310
|
-
);
|
|
2311
|
-
return new Response(JSON.stringify(responseInfo.body), {
|
|
2312
|
-
status: responseInfo.status,
|
|
2313
|
-
headers: responseHeaders
|
|
2314
|
-
});
|
|
2315
|
-
} catch (error) {
|
|
2316
|
-
if (error instanceof Error && error.name === "WorkflowAbort") {
|
|
2317
|
-
throw error;
|
|
2318
|
-
} else {
|
|
2319
|
-
console.error("Error in fetch implementation:", error);
|
|
2320
|
-
throw error;
|
|
2321
|
-
}
|
|
2322
|
-
}
|
|
2323
|
-
};
|
|
2324
|
-
var createWorkflowModel = ({
|
|
2325
|
-
context,
|
|
2326
|
-
provider,
|
|
2327
|
-
providerParams,
|
|
2328
|
-
agentCallParams
|
|
2329
|
-
}) => {
|
|
2330
|
-
return provider({
|
|
2331
|
-
fetch: (...params) => fetchWithContextCall(context, agentCallParams, ...params),
|
|
2332
|
-
...providerParams
|
|
2333
|
-
});
|
|
2334
|
-
};
|
|
2335
|
-
var wrapTools = ({
|
|
2336
|
-
context,
|
|
2337
|
-
tools
|
|
2338
|
-
}) => {
|
|
2339
|
-
return Object.fromEntries(
|
|
2340
|
-
Object.entries(tools).map((toolInfo) => {
|
|
2341
|
-
const [toolName, tool3] = toolInfo;
|
|
2342
|
-
const executeAsStep = "executeAsStep" in tool3 ? tool3.executeAsStep : true;
|
|
2343
|
-
const aiSDKTool = convertToAISDKTool(tool3);
|
|
2344
|
-
const execute = aiSDKTool.execute;
|
|
2345
|
-
if (execute && executeAsStep) {
|
|
2346
|
-
const wrappedExecute = (...params) => {
|
|
2347
|
-
return context.run(`Run tool ${toolName}`, () => execute(...params));
|
|
2348
|
-
};
|
|
2349
|
-
aiSDKTool.execute = wrappedExecute;
|
|
2350
|
-
}
|
|
2351
|
-
return [toolName, aiSDKTool];
|
|
2352
|
-
})
|
|
2353
|
-
);
|
|
2354
|
-
};
|
|
2355
|
-
var convertToAISDKTool = (tool3) => {
|
|
2356
|
-
const isLangchainTool = "invoke" in tool3;
|
|
2357
|
-
return isLangchainTool ? convertLangchainTool(tool3) : tool3;
|
|
2358
|
-
};
|
|
2359
|
-
var convertLangchainTool = (langchainTool) => {
|
|
2360
|
-
return (0, import_ai.tool)({
|
|
2361
|
-
description: langchainTool.description,
|
|
2362
|
-
parameters: langchainTool.schema,
|
|
2363
|
-
execute: async (...param) => langchainTool.invoke(...param)
|
|
2364
|
-
});
|
|
2365
|
-
};
|
|
2366
|
-
|
|
2367
|
-
// src/agents/agent.ts
|
|
2368
|
-
var import_zod = require("zod");
|
|
2369
|
-
var import_ai2 = require("ai");
|
|
2370
|
-
|
|
2371
|
-
// src/serve/utils.ts
|
|
2372
|
-
var isDisabledWorkflowContext = (context) => {
|
|
2373
|
-
return "disabled" in context;
|
|
2374
|
-
};
|
|
2375
|
-
|
|
2376
|
-
// src/agents/agent.ts
|
|
2377
|
-
var Agent = class {
|
|
2378
|
-
name;
|
|
2379
|
-
tools;
|
|
2380
|
-
maxSteps;
|
|
2381
|
-
background;
|
|
2382
|
-
model;
|
|
2383
|
-
temparature;
|
|
2384
|
-
context;
|
|
2385
|
-
constructor({ tools, maxSteps, background, name, model, temparature = 0.1 }, context) {
|
|
2386
|
-
this.name = name;
|
|
2387
|
-
this.tools = tools ?? {};
|
|
2388
|
-
this.maxSteps = maxSteps;
|
|
2389
|
-
this.background = background;
|
|
2390
|
-
this.model = model;
|
|
2391
|
-
this.temparature = temparature;
|
|
2392
|
-
this.context = context;
|
|
2393
|
-
}
|
|
2394
|
-
/**
|
|
2395
|
-
* Trigger the agent by passing a prompt
|
|
2396
|
-
*
|
|
2397
|
-
* @param prompt task to assign to the agent
|
|
2398
|
-
* @returns Response as `{ text: string }`
|
|
2399
|
-
*/
|
|
2400
|
-
async call({ prompt }) {
|
|
2401
|
-
try {
|
|
2402
|
-
if (isDisabledWorkflowContext(this.context)) {
|
|
2403
|
-
await this.context.sleep("abort", 0);
|
|
2404
|
-
}
|
|
2405
|
-
const result = await (0, import_ai2.generateText)({
|
|
2406
|
-
model: this.model,
|
|
2407
|
-
tools: this.tools,
|
|
2408
|
-
maxSteps: this.maxSteps,
|
|
2409
|
-
system: this.background,
|
|
2410
|
-
prompt,
|
|
2411
|
-
headers: {
|
|
2412
|
-
[AGENT_NAME_HEADER]: this.name
|
|
2413
|
-
},
|
|
2414
|
-
temperature: this.temparature
|
|
2415
|
-
});
|
|
2416
|
-
return { text: result.text };
|
|
2417
|
-
} catch (error) {
|
|
2418
|
-
if (error instanceof import_ai2.ToolExecutionError) {
|
|
2419
|
-
if (error.cause instanceof Error && error.cause.name === "WorkflowAbort") {
|
|
2420
|
-
throw error.cause;
|
|
2421
|
-
} else if (error.cause instanceof import_ai2.ToolExecutionError && error.cause.cause instanceof Error && error.cause.cause.name === "WorkflowAbort") {
|
|
2422
|
-
throw error.cause.cause;
|
|
2423
|
-
} else {
|
|
2424
|
-
throw error;
|
|
2425
|
-
}
|
|
2426
|
-
} else {
|
|
2427
|
-
throw error;
|
|
2428
|
-
}
|
|
2429
|
-
}
|
|
2430
|
-
}
|
|
2431
|
-
/**
|
|
2432
|
-
* Convert the agent to a tool which can be used by other agents.
|
|
2433
|
-
*
|
|
2434
|
-
* @returns the agent as a tool
|
|
2435
|
-
*/
|
|
2436
|
-
asTool() {
|
|
2437
|
-
const toolDescriptions = Object.values(this.tools).map((tool3) => tool3.description).join("\n");
|
|
2438
|
-
return (0, import_ai2.tool)({
|
|
2439
|
-
parameters: import_zod.z.object({ prompt: import_zod.z.string() }),
|
|
2440
|
-
execute: async ({ prompt }) => {
|
|
2441
|
-
return await this.call({ prompt });
|
|
2442
|
-
},
|
|
2443
|
-
description: `An AI Agent with the following background: ${this.background}Has access to the following tools: ${toolDescriptions}`
|
|
2444
|
-
});
|
|
2445
|
-
}
|
|
2446
|
-
};
|
|
2447
|
-
var ManagerAgent = class extends Agent {
|
|
2448
|
-
agents;
|
|
2449
|
-
/**
|
|
2450
|
-
* A manager agent which coordinates agents available to it to achieve a
|
|
2451
|
-
* given task
|
|
2452
|
-
*
|
|
2453
|
-
* @param name Name of the agent
|
|
2454
|
-
* @param background Background of the agent. If not passed, default will be used.
|
|
2455
|
-
* @param model LLM model to use
|
|
2456
|
-
* @param agents: List of agents available to the agent
|
|
2457
|
-
* @param maxSteps number of times the manager agent can call the LLM at most.
|
|
2458
|
-
* If the agent abruptly stops execution after calling other agents, you may
|
|
2459
|
-
* need to increase maxSteps
|
|
2460
|
-
*/
|
|
2461
|
-
constructor({
|
|
2462
|
-
agents,
|
|
2463
|
-
background = MANAGER_AGENT_PROMPT,
|
|
2464
|
-
model,
|
|
2465
|
-
maxSteps,
|
|
2466
|
-
name = "manager llm"
|
|
2467
|
-
}, context) {
|
|
2468
|
-
super(
|
|
2469
|
-
{
|
|
2470
|
-
background,
|
|
2471
|
-
maxSteps,
|
|
2472
|
-
tools: Object.fromEntries(agents.map((agent) => [agent.name, agent.asTool()])),
|
|
2473
|
-
name,
|
|
2474
|
-
model
|
|
2475
|
-
},
|
|
2476
|
-
context
|
|
2477
|
-
);
|
|
2478
|
-
this.agents = agents;
|
|
2479
|
-
}
|
|
2480
|
-
};
|
|
2481
|
-
|
|
2482
|
-
// src/agents/task.ts
|
|
2483
|
-
var Task = class {
|
|
2484
|
-
context;
|
|
2485
|
-
taskParameters;
|
|
2486
|
-
constructor({
|
|
2487
|
-
context,
|
|
2488
|
-
taskParameters
|
|
2489
|
-
}) {
|
|
2490
|
-
this.context = context;
|
|
2491
|
-
this.taskParameters = taskParameters;
|
|
2492
|
-
}
|
|
2493
|
-
/**
|
|
2494
|
-
* Run the agents to complete the task
|
|
2495
|
-
*
|
|
2496
|
-
* @returns Result of the task as { text: string }
|
|
2497
|
-
*/
|
|
2498
|
-
async run() {
|
|
2499
|
-
const { prompt, ...otherParams } = this.taskParameters;
|
|
2500
|
-
if ("agent" in otherParams) {
|
|
2501
|
-
const agent = otherParams.agent;
|
|
2502
|
-
const result = await agent.call({
|
|
2503
|
-
prompt
|
|
2504
|
-
});
|
|
2505
|
-
return { text: result.text };
|
|
2506
|
-
} else {
|
|
2507
|
-
const { agents, maxSteps, model, background } = otherParams;
|
|
2508
|
-
const managerAgent = new ManagerAgent(
|
|
2509
|
-
{
|
|
2510
|
-
model,
|
|
2511
|
-
maxSteps,
|
|
2512
|
-
agents,
|
|
2513
|
-
name: "Manager LLM",
|
|
2514
|
-
background
|
|
2515
|
-
},
|
|
2516
|
-
this.context
|
|
2517
|
-
);
|
|
2518
|
-
const result = await managerAgent.call({ prompt });
|
|
2519
|
-
return { text: result.text };
|
|
2520
|
-
}
|
|
2521
|
-
}
|
|
2522
|
-
};
|
|
2523
|
-
|
|
2524
|
-
// src/agents/index.ts
|
|
2525
|
-
var WorkflowAgents = class {
|
|
2526
|
-
context;
|
|
2527
|
-
constructor({ context }) {
|
|
2528
|
-
this.context = context;
|
|
2529
|
-
}
|
|
2530
|
-
/**
|
|
2531
|
-
* Defines an agent
|
|
2532
|
-
*
|
|
2533
|
-
* ```ts
|
|
2534
|
-
* const researcherAgent = context.agents.agent({
|
|
2535
|
-
* model,
|
|
2536
|
-
* name: 'academic',
|
|
2537
|
-
* maxSteps: 2,
|
|
2538
|
-
* tools: {
|
|
2539
|
-
* wikiTool: new WikipediaQueryRun({
|
|
2540
|
-
* topKResults: 1,
|
|
2541
|
-
* maxDocContentLength: 500,
|
|
2542
|
-
* })
|
|
2543
|
-
* },
|
|
2544
|
-
* background:
|
|
2545
|
-
* 'You are researcher agent with access to Wikipedia. ' +
|
|
2546
|
-
* 'Utilize Wikipedia as much as possible for correct information',
|
|
2547
|
-
* });
|
|
2548
|
-
* ```
|
|
2549
|
-
*
|
|
2550
|
-
* @param params agent parameters
|
|
2551
|
-
* @returns
|
|
2552
|
-
*/
|
|
2553
|
-
agent(params) {
|
|
2554
|
-
const wrappedTools = wrapTools({ context: this.context, tools: params.tools });
|
|
2555
|
-
return new Agent(
|
|
2556
|
-
{
|
|
2557
|
-
...params,
|
|
2558
|
-
tools: wrappedTools
|
|
2559
|
-
},
|
|
2560
|
-
this.context
|
|
2561
|
-
);
|
|
2562
|
-
}
|
|
2563
|
-
task(taskParameters) {
|
|
2564
|
-
return new Task({ context: this.context, taskParameters });
|
|
2565
|
-
}
|
|
2566
|
-
/**
|
|
2567
|
-
* creates an openai model for agents
|
|
2568
|
-
*/
|
|
2569
|
-
openai(...params) {
|
|
2570
|
-
const [model, settings] = params;
|
|
2571
|
-
const { baseURL, apiKey, callSettings, ...otherSettings } = settings ?? {};
|
|
2572
|
-
const openaiModel = this.AISDKModel({
|
|
2573
|
-
context: this.context,
|
|
2574
|
-
provider: import_openai2.createOpenAI,
|
|
2575
|
-
providerParams: { baseURL, apiKey, compatibility: "strict" },
|
|
2576
|
-
agentCallParams: callSettings
|
|
2577
|
-
});
|
|
2578
|
-
return openaiModel(model, otherSettings);
|
|
2579
|
-
}
|
|
2580
|
-
AISDKModel = createWorkflowModel;
|
|
2581
|
-
};
|
|
2582
|
-
|
|
2583
2300
|
// src/serve/serve-many.ts
|
|
2584
2301
|
var getNewUrlFromWorkflowId = (url, workflowId) => {
|
|
2585
2302
|
if (!workflowId) {
|
|
@@ -2821,7 +2538,7 @@ var WorkflowContext = class {
|
|
|
2821
2538
|
* @returns result of the step function
|
|
2822
2539
|
*/
|
|
2823
2540
|
async run(stepName, stepFunction) {
|
|
2824
|
-
const wrappedStepFunction = () => this.executor.wrapStep(stepName, stepFunction);
|
|
2541
|
+
const wrappedStepFunction = (() => this.executor.wrapStep(stepName, stepFunction));
|
|
2825
2542
|
return await this.addStep(new LazyFunctionStep(stepName, wrappedStepFunction));
|
|
2826
2543
|
}
|
|
2827
2544
|
/**
|
|
@@ -2992,11 +2709,6 @@ var WorkflowContext = class {
|
|
|
2992
2709
|
context: this
|
|
2993
2710
|
});
|
|
2994
2711
|
}
|
|
2995
|
-
get agents() {
|
|
2996
|
-
return new WorkflowAgents({
|
|
2997
|
-
context: this
|
|
2998
|
-
});
|
|
2999
|
-
}
|
|
3000
2712
|
};
|
|
3001
2713
|
|
|
3002
2714
|
// src/logger.ts
|
|
@@ -3100,7 +2812,7 @@ var DisabledWorkflowContext = class _DisabledWorkflowContext extends WorkflowCon
|
|
|
3100
2812
|
try {
|
|
3101
2813
|
await routeFunction(disabledContext);
|
|
3102
2814
|
} catch (error) {
|
|
3103
|
-
if (error
|
|
2815
|
+
if (isInstanceOf(error, WorkflowAbort) && error.stepName === this.disabledMessage || isInstanceOf(error, WorkflowNonRetryableError) || isInstanceOf(error, WorkflowRetryAfterError)) {
|
|
3104
2816
|
return ok("step-found");
|
|
3105
2817
|
}
|
|
3106
2818
|
console.warn(
|
|
@@ -3353,13 +3065,24 @@ var processOptions = (options) => {
|
|
|
3353
3065
|
},
|
|
3354
3066
|
status: 489
|
|
3355
3067
|
});
|
|
3356
|
-
} else if (detailedFinishCondition?.condition === "
|
|
3357
|
-
return new Response(detailedFinishCondition.result
|
|
3358
|
-
status: 200,
|
|
3068
|
+
} else if (detailedFinishCondition?.condition === "retry-after-error") {
|
|
3069
|
+
return new Response(JSON.stringify(formatWorkflowError(detailedFinishCondition.result)), {
|
|
3359
3070
|
headers: {
|
|
3071
|
+
"Retry-After": detailedFinishCondition.result.retryAfter.toString(),
|
|
3360
3072
|
[WORKFLOW_PROTOCOL_VERSION_HEADER]: WORKFLOW_PROTOCOL_VERSION
|
|
3361
|
-
}
|
|
3073
|
+
},
|
|
3074
|
+
status: 429
|
|
3362
3075
|
});
|
|
3076
|
+
} else if (detailedFinishCondition?.condition === "failure-callback") {
|
|
3077
|
+
return new Response(
|
|
3078
|
+
JSON.stringify({ result: detailedFinishCondition.result ?? void 0 }),
|
|
3079
|
+
{
|
|
3080
|
+
status: 200,
|
|
3081
|
+
headers: {
|
|
3082
|
+
[WORKFLOW_PROTOCOL_VERSION_HEADER]: WORKFLOW_PROTOCOL_VERSION
|
|
3083
|
+
}
|
|
3084
|
+
}
|
|
3085
|
+
);
|
|
3363
3086
|
}
|
|
3364
3087
|
return new Response(JSON.stringify({ workflowRunId }), {
|
|
3365
3088
|
status: 200,
|
|
@@ -3569,12 +3292,18 @@ var serveBase = (routeFunction, telemetry, options) => {
|
|
|
3569
3292
|
},
|
|
3570
3293
|
debug
|
|
3571
3294
|
});
|
|
3572
|
-
if (result.isOk() && result.value
|
|
3295
|
+
if (result.isOk() && isInstanceOf(result.value, WorkflowNonRetryableError)) {
|
|
3573
3296
|
return onStepFinish(workflowRunId, result.value, {
|
|
3574
3297
|
condition: "non-retryable-error",
|
|
3575
3298
|
result: result.value
|
|
3576
3299
|
});
|
|
3577
3300
|
}
|
|
3301
|
+
if (result.isOk() && isInstanceOf(result.value, WorkflowRetryAfterError)) {
|
|
3302
|
+
return onStepFinish(workflowRunId, result.value, {
|
|
3303
|
+
condition: "retry-after-error",
|
|
3304
|
+
result: result.value
|
|
3305
|
+
});
|
|
3306
|
+
}
|
|
3578
3307
|
if (result.isErr()) {
|
|
3579
3308
|
await debug?.log("ERROR", "ERROR", { error: result.error.message });
|
|
3580
3309
|
throw result.error;
|
|
@@ -3605,7 +3334,7 @@ var serveBase = (routeFunction, telemetry, options) => {
|
|
|
3605
3334
|
const errorMessage = `Error while running onError callback: '${formattedOnErrorError.message}'.
|
|
3606
3335
|
Original error: '${formattedError.message}'`;
|
|
3607
3336
|
console.error(errorMessage);
|
|
3608
|
-
return new Response(errorMessage, {
|
|
3337
|
+
return new Response(JSON.stringify({ error: errorMessage }), {
|
|
3609
3338
|
status: 500,
|
|
3610
3339
|
headers: {
|
|
3611
3340
|
[WORKFLOW_PROTOCOL_VERSION_HEADER]: WORKFLOW_PROTOCOL_VERSION
|
package/solidjs.mjs
CHANGED
package/svelte.d.mts
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
import * as _sveltejs_kit from '@sveltejs/kit';
|
|
2
2
|
import { RequestHandler } from '@sveltejs/kit';
|
|
3
|
-
import { R as RouteFunction, n as PublicServeOptions, y as InvokableWorkflow } from './types-
|
|
4
|
-
import { s as serveManyBase } from './serve-many-
|
|
3
|
+
import { R as RouteFunction, n as PublicServeOptions, y as InvokableWorkflow } from './types-DESkn7K9.mjs';
|
|
4
|
+
import { s as serveManyBase } from './serve-many-DEwKPF6H.mjs';
|
|
5
5
|
import '@upstash/qstash';
|
|
6
6
|
import 'zod';
|
|
7
|
-
import 'ai';
|
|
8
|
-
import '@ai-sdk/openai';
|
|
9
7
|
|
|
10
8
|
type RequireEnv<T> = T & {
|
|
11
9
|
env: PublicServeOptions["env"];
|
|
@@ -22,9 +20,9 @@ type RequireEnv<T> = T & {
|
|
|
22
20
|
declare const serve: <TInitialPayload = unknown, TResult = unknown>(routeFunction: RouteFunction<TInitialPayload, TResult>, options: RequireEnv<PublicServeOptions<TInitialPayload>>) => {
|
|
23
21
|
POST: RequestHandler;
|
|
24
22
|
};
|
|
25
|
-
declare const createWorkflow: <TInitialPayload, TResult>(
|
|
23
|
+
declare const createWorkflow: <TInitialPayload, TResult>(...params: Parameters<typeof serve<TInitialPayload, TResult>>) => InvokableWorkflow<TInitialPayload, TResult>;
|
|
26
24
|
declare const serveMany: (workflows: Parameters<typeof serveManyBase>[0]["workflows"], options?: Parameters<typeof serveManyBase>[0]["options"]) => {
|
|
27
|
-
POST: (event: _sveltejs_kit.RequestEvent<
|
|
25
|
+
POST: (event: _sveltejs_kit.RequestEvent<Record<string, string>, string | null>) => Promise<any>;
|
|
28
26
|
};
|
|
29
27
|
|
|
30
28
|
export { createWorkflow, serve, serveMany };
|