@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/h3.js
CHANGED
|
@@ -102,21 +102,16 @@ function hasProp(obj, prop) {
|
|
|
102
102
|
return false;
|
|
103
103
|
}
|
|
104
104
|
}
|
|
105
|
-
var __defProp$2 = Object.defineProperty;
|
|
106
|
-
var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
107
|
-
var __publicField$2 = (obj, key, value) => {
|
|
108
|
-
__defNormalProp$2(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
109
|
-
return value;
|
|
110
|
-
};
|
|
111
105
|
var H3Error = class extends Error {
|
|
106
|
+
static __h3_error__ = true;
|
|
107
|
+
statusCode = 500;
|
|
108
|
+
fatal = false;
|
|
109
|
+
unhandled = false;
|
|
110
|
+
statusMessage;
|
|
111
|
+
data;
|
|
112
|
+
cause;
|
|
112
113
|
constructor(message, opts = {}) {
|
|
113
114
|
super(message, opts);
|
|
114
|
-
__publicField$2(this, "statusCode", 500);
|
|
115
|
-
__publicField$2(this, "fatal", false);
|
|
116
|
-
__publicField$2(this, "unhandled", false);
|
|
117
|
-
__publicField$2(this, "statusMessage");
|
|
118
|
-
__publicField$2(this, "data");
|
|
119
|
-
__publicField$2(this, "cause");
|
|
120
115
|
if (opts.cause && !this.cause) {
|
|
121
116
|
this.cause = opts.cause;
|
|
122
117
|
}
|
|
@@ -135,7 +130,6 @@ var H3Error = class extends Error {
|
|
|
135
130
|
return obj;
|
|
136
131
|
}
|
|
137
132
|
};
|
|
138
|
-
__publicField$2(H3Error, "__h3_error__", true);
|
|
139
133
|
function createError(input) {
|
|
140
134
|
if (typeof input === "string") {
|
|
141
135
|
return new H3Error(input);
|
|
@@ -258,6 +252,9 @@ function readRawBody(event, encoding = "utf8") {
|
|
|
258
252
|
if (_resolved instanceof URLSearchParams) {
|
|
259
253
|
return Buffer.from(_resolved.toString());
|
|
260
254
|
}
|
|
255
|
+
if (_resolved instanceof FormData) {
|
|
256
|
+
return new Response(_resolved).bytes().then((uint8arr) => Buffer.from(uint8arr));
|
|
257
|
+
}
|
|
261
258
|
return Buffer.from(_resolved);
|
|
262
259
|
});
|
|
263
260
|
return encoding ? promise2.then((buff) => buff.toString(encoding)) : promise2;
|
|
@@ -339,7 +336,95 @@ var H3Headers = globalThis.Headers;
|
|
|
339
336
|
var H3Response = globalThis.Response;
|
|
340
337
|
|
|
341
338
|
// src/client/utils.ts
|
|
339
|
+
var import_qstash2 = require("@upstash/qstash");
|
|
340
|
+
|
|
341
|
+
// src/error.ts
|
|
342
342
|
var import_qstash = require("@upstash/qstash");
|
|
343
|
+
var WorkflowError = class extends import_qstash.QstashError {
|
|
344
|
+
constructor(message) {
|
|
345
|
+
super(message);
|
|
346
|
+
this.name = "WorkflowError";
|
|
347
|
+
}
|
|
348
|
+
};
|
|
349
|
+
var WorkflowAbort = class extends Error {
|
|
350
|
+
stepInfo;
|
|
351
|
+
stepName;
|
|
352
|
+
/**
|
|
353
|
+
* whether workflow is to be canceled on abort
|
|
354
|
+
*/
|
|
355
|
+
cancelWorkflow;
|
|
356
|
+
/**
|
|
357
|
+
*
|
|
358
|
+
* @param stepName name of the aborting step
|
|
359
|
+
* @param stepInfo step information
|
|
360
|
+
* @param cancelWorkflow
|
|
361
|
+
*/
|
|
362
|
+
constructor(stepName, stepInfo, cancelWorkflow = false) {
|
|
363
|
+
super(
|
|
364
|
+
`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}'.`
|
|
365
|
+
);
|
|
366
|
+
this.name = "WorkflowAbort";
|
|
367
|
+
this.stepName = stepName;
|
|
368
|
+
this.stepInfo = stepInfo;
|
|
369
|
+
this.cancelWorkflow = cancelWorkflow;
|
|
370
|
+
}
|
|
371
|
+
};
|
|
372
|
+
var WorkflowNonRetryableError = class extends WorkflowAbort {
|
|
373
|
+
/**
|
|
374
|
+
* @param message error message to be displayed
|
|
375
|
+
*/
|
|
376
|
+
constructor(message) {
|
|
377
|
+
super("fail", void 0, false);
|
|
378
|
+
this.name = "WorkflowNonRetryableError";
|
|
379
|
+
if (message) this.message = message;
|
|
380
|
+
}
|
|
381
|
+
};
|
|
382
|
+
var WorkflowRetryAfterError = class extends WorkflowAbort {
|
|
383
|
+
retryAfter;
|
|
384
|
+
/**
|
|
385
|
+
* @param retryAfter time in seconds after which the workflow should be retried
|
|
386
|
+
* @param message error message to be displayed
|
|
387
|
+
*/
|
|
388
|
+
constructor(message, retryAfter) {
|
|
389
|
+
super("retry", void 0, false);
|
|
390
|
+
this.name = "WorkflowRetryAfterError";
|
|
391
|
+
this.retryAfter = retryAfter;
|
|
392
|
+
if (message) this.message = message;
|
|
393
|
+
}
|
|
394
|
+
};
|
|
395
|
+
var formatWorkflowError = (error) => {
|
|
396
|
+
return error instanceof Error ? {
|
|
397
|
+
error: error.name,
|
|
398
|
+
message: error.message,
|
|
399
|
+
stack: error.stack
|
|
400
|
+
} : {
|
|
401
|
+
error: "Error",
|
|
402
|
+
message: `An error occured while executing workflow: '${typeof error === "string" ? error : JSON.stringify(error)}'`
|
|
403
|
+
};
|
|
404
|
+
};
|
|
405
|
+
function getConstructorName(obj) {
|
|
406
|
+
if (obj === null || obj === void 0) {
|
|
407
|
+
return null;
|
|
408
|
+
}
|
|
409
|
+
const ctor = obj.constructor;
|
|
410
|
+
if (!ctor || ctor.name === "Object") {
|
|
411
|
+
return null;
|
|
412
|
+
}
|
|
413
|
+
return ctor.name;
|
|
414
|
+
}
|
|
415
|
+
function getConstructorNames(obj) {
|
|
416
|
+
const proto = Object.getPrototypeOf(obj);
|
|
417
|
+
const name = getConstructorName(proto);
|
|
418
|
+
if (name === null) {
|
|
419
|
+
return [];
|
|
420
|
+
}
|
|
421
|
+
return [name, ...getConstructorNames(proto)];
|
|
422
|
+
}
|
|
423
|
+
function isInstanceOf(v, ctor) {
|
|
424
|
+
return getConstructorNames(v).includes(ctor.name);
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
// src/client/utils.ts
|
|
343
428
|
var makeNotifyRequest = async (requester, eventId, eventData) => {
|
|
344
429
|
const result = await requester.request({
|
|
345
430
|
path: ["v2", "notify", eventId],
|
|
@@ -379,7 +464,7 @@ var getSteps = async (requester, workflowRunId, messageId, debug) => {
|
|
|
379
464
|
return { steps: filteredSteps, workflowRunEnded: false };
|
|
380
465
|
}
|
|
381
466
|
} catch (error) {
|
|
382
|
-
if (error
|
|
467
|
+
if (isInstanceOf(error, import_qstash2.QstashError) && error.status === 404) {
|
|
383
468
|
await debug?.log("WARN", "ENDPOINT_START", {
|
|
384
469
|
message: "Couldn't fetch workflow run steps. This can happen if the workflow run succesfully ends before some callback is executed.",
|
|
385
470
|
error
|
|
@@ -404,64 +489,11 @@ var WORKFLOW_PROTOCOL_VERSION_HEADER = "Upstash-Workflow-Sdk-Version";
|
|
|
404
489
|
var DEFAULT_CONTENT_TYPE = "application/json";
|
|
405
490
|
var NO_CONCURRENCY = 1;
|
|
406
491
|
var DEFAULT_RETRIES = 3;
|
|
407
|
-
var VERSION = "v0.
|
|
492
|
+
var VERSION = "v0.3.0-rc";
|
|
408
493
|
var SDK_TELEMETRY = `@upstash/workflow@${VERSION}`;
|
|
409
494
|
var TELEMETRY_HEADER_SDK = "Upstash-Telemetry-Sdk";
|
|
410
495
|
var TELEMETRY_HEADER_FRAMEWORK = "Upstash-Telemetry-Framework";
|
|
411
496
|
var TELEMETRY_HEADER_RUNTIME = "Upstash-Telemetry-Runtime";
|
|
412
|
-
var TELEMETRY_HEADER_AGENT = "Upstash-Telemetry-Agent";
|
|
413
|
-
|
|
414
|
-
// src/error.ts
|
|
415
|
-
var import_qstash2 = require("@upstash/qstash");
|
|
416
|
-
var WorkflowError = class extends import_qstash2.QstashError {
|
|
417
|
-
constructor(message) {
|
|
418
|
-
super(message);
|
|
419
|
-
this.name = "WorkflowError";
|
|
420
|
-
}
|
|
421
|
-
};
|
|
422
|
-
var WorkflowAbort = class extends Error {
|
|
423
|
-
stepInfo;
|
|
424
|
-
stepName;
|
|
425
|
-
/**
|
|
426
|
-
* whether workflow is to be canceled on abort
|
|
427
|
-
*/
|
|
428
|
-
cancelWorkflow;
|
|
429
|
-
/**
|
|
430
|
-
*
|
|
431
|
-
* @param stepName name of the aborting step
|
|
432
|
-
* @param stepInfo step information
|
|
433
|
-
* @param cancelWorkflow
|
|
434
|
-
*/
|
|
435
|
-
constructor(stepName, stepInfo, cancelWorkflow = false) {
|
|
436
|
-
super(
|
|
437
|
-
`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}'.`
|
|
438
|
-
);
|
|
439
|
-
this.name = "WorkflowAbort";
|
|
440
|
-
this.stepName = stepName;
|
|
441
|
-
this.stepInfo = stepInfo;
|
|
442
|
-
this.cancelWorkflow = cancelWorkflow;
|
|
443
|
-
}
|
|
444
|
-
};
|
|
445
|
-
var WorkflowNonRetryableError = class extends WorkflowAbort {
|
|
446
|
-
/**
|
|
447
|
-
* @param message error message to be displayed
|
|
448
|
-
*/
|
|
449
|
-
constructor(message) {
|
|
450
|
-
super("fail", void 0, false);
|
|
451
|
-
this.name = "WorkflowNonRetryableError";
|
|
452
|
-
if (message) this.message = message;
|
|
453
|
-
}
|
|
454
|
-
};
|
|
455
|
-
var formatWorkflowError = (error) => {
|
|
456
|
-
return error instanceof Error ? {
|
|
457
|
-
error: error.name,
|
|
458
|
-
message: error.message,
|
|
459
|
-
stack: error.stack
|
|
460
|
-
} : {
|
|
461
|
-
error: "Error",
|
|
462
|
-
message: `An error occured while executing workflow: '${typeof error === "string" ? error : JSON.stringify(error)}'`
|
|
463
|
-
};
|
|
464
|
-
};
|
|
465
497
|
|
|
466
498
|
// src/context/auto-executor.ts
|
|
467
499
|
var import_qstash5 = require("@upstash/qstash");
|
|
@@ -834,9 +866,9 @@ var Ok = class {
|
|
|
834
866
|
}
|
|
835
867
|
safeUnwrap() {
|
|
836
868
|
const value = this.value;
|
|
837
|
-
return function* () {
|
|
869
|
+
return (function* () {
|
|
838
870
|
return value;
|
|
839
|
-
}();
|
|
871
|
+
})();
|
|
840
872
|
}
|
|
841
873
|
_unsafeUnwrap(_) {
|
|
842
874
|
return this.value;
|
|
@@ -895,10 +927,10 @@ var Err = class {
|
|
|
895
927
|
}
|
|
896
928
|
safeUnwrap() {
|
|
897
929
|
const error = this.error;
|
|
898
|
-
return function* () {
|
|
930
|
+
return (function* () {
|
|
899
931
|
yield err(error);
|
|
900
932
|
throw new Error("Do not use this generator out of `safeTry`");
|
|
901
|
-
}();
|
|
933
|
+
})();
|
|
902
934
|
}
|
|
903
935
|
_unsafeUnwrap(config) {
|
|
904
936
|
throw createNeverThrowError("Called `_unsafeUnwrap` on an Err", this, config);
|
|
@@ -1022,17 +1054,17 @@ var triggerRouteFunction = async ({
|
|
|
1022
1054
|
return ok("workflow-finished");
|
|
1023
1055
|
} catch (error) {
|
|
1024
1056
|
const error_ = error;
|
|
1025
|
-
if (error
|
|
1057
|
+
if (isInstanceOf(error, import_qstash3.QstashError) && error.status === 400) {
|
|
1026
1058
|
await debug?.log("WARN", "RESPONSE_WORKFLOW", {
|
|
1027
1059
|
message: `tried to append to a cancelled workflow. exiting without publishing.`,
|
|
1028
1060
|
name: error.name,
|
|
1029
1061
|
errorMessage: error.message
|
|
1030
1062
|
});
|
|
1031
1063
|
return ok("workflow-was-finished");
|
|
1032
|
-
} else if (
|
|
1033
|
-
return err(error_);
|
|
1034
|
-
} else if (error_ instanceof WorkflowNonRetryableError) {
|
|
1064
|
+
} else if (isInstanceOf(error_, WorkflowNonRetryableError) || isInstanceOf(error_, WorkflowRetryAfterError)) {
|
|
1035
1065
|
return ok(error_);
|
|
1066
|
+
} else if (!isInstanceOf(error_, WorkflowAbort)) {
|
|
1067
|
+
return err(error_);
|
|
1036
1068
|
} else if (error_.cancelWorkflow) {
|
|
1037
1069
|
await onCancel();
|
|
1038
1070
|
return ok("workflow-finished");
|
|
@@ -1865,20 +1897,6 @@ var LazyInvokeStep = class extends BaseLazyStep {
|
|
|
1865
1897
|
}
|
|
1866
1898
|
};
|
|
1867
1899
|
|
|
1868
|
-
// src/agents/constants.ts
|
|
1869
|
-
var AGENT_NAME_HEADER = "upstash-agent-name";
|
|
1870
|
-
var MANAGER_AGENT_PROMPT = `You are an agent orchestrating other AI Agents.
|
|
1871
|
-
|
|
1872
|
-
These other agents have tools available to them.
|
|
1873
|
-
|
|
1874
|
-
Given a prompt, utilize these agents to address requests.
|
|
1875
|
-
|
|
1876
|
-
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.
|
|
1877
|
-
|
|
1878
|
-
Avoid calling the same agent twice in one turn. Instead, prefer to call it once but provide everything
|
|
1879
|
-
you need from that agent.
|
|
1880
|
-
`;
|
|
1881
|
-
|
|
1882
1900
|
// src/qstash/headers.ts
|
|
1883
1901
|
var WorkflowHeaders = class {
|
|
1884
1902
|
userHeaders;
|
|
@@ -1927,8 +1945,7 @@ var WorkflowHeaders = class {
|
|
|
1927
1945
|
[WORKFLOW_URL_HEADER]: this.workflowConfig.workflowUrl,
|
|
1928
1946
|
[WORKFLOW_FEATURE_HEADER]: "LazyFetch,InitialBody,WF_DetectTrigger" + (this.keepTriggerConfig ? ",WF_TriggerOnConfig" : ""),
|
|
1929
1947
|
[WORKFLOW_PROTOCOL_VERSION_HEADER]: WORKFLOW_PROTOCOL_VERSION,
|
|
1930
|
-
...this.workflowConfig.telemetry ? getTelemetryHeaders(this.workflowConfig.telemetry) : {}
|
|
1931
|
-
...this.workflowConfig.telemetry && this.stepInfo?.lazyStep instanceof LazyCallStep && this.stepInfo.lazyStep.headers[AGENT_NAME_HEADER] ? { [TELEMETRY_HEADER_AGENT]: "true" } : {}
|
|
1948
|
+
...this.workflowConfig.telemetry ? getTelemetryHeaders(this.workflowConfig.telemetry) : {}
|
|
1932
1949
|
};
|
|
1933
1950
|
if (this.stepInfo?.lazyStep.stepType !== "Call") {
|
|
1934
1951
|
this.headers.rawHeaders[`Upstash-Forward-${WORKFLOW_PROTOCOL_VERSION_HEADER}`] = WORKFLOW_PROTOCOL_VERSION;
|
|
@@ -2339,7 +2356,7 @@ var AutoExecutor = class _AutoExecutor {
|
|
|
2339
2356
|
});
|
|
2340
2357
|
throw new WorkflowAbort(parallelStep.stepName, resultStep);
|
|
2341
2358
|
} catch (error) {
|
|
2342
|
-
if (error
|
|
2359
|
+
if (isInstanceOf(error, WorkflowAbort) || isInstanceOf(error, import_qstash5.QstashError) && error.status === 400) {
|
|
2343
2360
|
throw error;
|
|
2344
2361
|
}
|
|
2345
2362
|
throw new WorkflowError(
|
|
@@ -2446,7 +2463,7 @@ var validateParallelSteps = (lazySteps, stepsFromRequest) => {
|
|
|
2446
2463
|
validateStep(lazySteps[index], stepFromRequest);
|
|
2447
2464
|
}
|
|
2448
2465
|
} catch (error) {
|
|
2449
|
-
if (error
|
|
2466
|
+
if (isInstanceOf(error, WorkflowError)) {
|
|
2450
2467
|
const lazyStepNames = lazySteps.map((lazyStep) => lazyStep.stepName);
|
|
2451
2468
|
const lazyStepTypes = lazySteps.map((lazyStep) => lazyStep.stepType);
|
|
2452
2469
|
const requestStepNames = stepsFromRequest.map((step) => step.stepName);
|
|
@@ -2591,309 +2608,6 @@ var WorkflowApi = class extends BaseWorkflowApi {
|
|
|
2591
2608
|
}
|
|
2592
2609
|
};
|
|
2593
2610
|
|
|
2594
|
-
// src/agents/index.ts
|
|
2595
|
-
var import_openai2 = require("@ai-sdk/openai");
|
|
2596
|
-
|
|
2597
|
-
// src/agents/adapters.ts
|
|
2598
|
-
var import_ai = require("ai");
|
|
2599
|
-
var fetchWithContextCall = async (context, agentCallParams, ...params) => {
|
|
2600
|
-
const [input, init] = params;
|
|
2601
|
-
try {
|
|
2602
|
-
const headers = init?.headers ? Object.fromEntries(new Headers(init.headers).entries()) : {};
|
|
2603
|
-
const body = init?.body ? JSON.parse(init.body) : void 0;
|
|
2604
|
-
const agentName = headers[AGENT_NAME_HEADER];
|
|
2605
|
-
const stepName = agentName ? `Call Agent ${agentName}` : "Call Agent";
|
|
2606
|
-
const responseInfo = await context.call(stepName, {
|
|
2607
|
-
url: input.toString(),
|
|
2608
|
-
method: init?.method,
|
|
2609
|
-
headers,
|
|
2610
|
-
body,
|
|
2611
|
-
timeout: agentCallParams?.timeout,
|
|
2612
|
-
retries: agentCallParams?.retries,
|
|
2613
|
-
retryDelay: agentCallParams?.retryDelay,
|
|
2614
|
-
flowControl: agentCallParams?.flowControl
|
|
2615
|
-
});
|
|
2616
|
-
const responseHeaders = new Headers(
|
|
2617
|
-
Object.entries(responseInfo.header).reduce(
|
|
2618
|
-
(acc, [key, values]) => {
|
|
2619
|
-
acc[key] = values.join(", ");
|
|
2620
|
-
return acc;
|
|
2621
|
-
},
|
|
2622
|
-
{}
|
|
2623
|
-
)
|
|
2624
|
-
);
|
|
2625
|
-
return new Response(JSON.stringify(responseInfo.body), {
|
|
2626
|
-
status: responseInfo.status,
|
|
2627
|
-
headers: responseHeaders
|
|
2628
|
-
});
|
|
2629
|
-
} catch (error) {
|
|
2630
|
-
if (error instanceof Error && error.name === "WorkflowAbort") {
|
|
2631
|
-
throw error;
|
|
2632
|
-
} else {
|
|
2633
|
-
console.error("Error in fetch implementation:", error);
|
|
2634
|
-
throw error;
|
|
2635
|
-
}
|
|
2636
|
-
}
|
|
2637
|
-
};
|
|
2638
|
-
var createWorkflowModel = ({
|
|
2639
|
-
context,
|
|
2640
|
-
provider,
|
|
2641
|
-
providerParams,
|
|
2642
|
-
agentCallParams
|
|
2643
|
-
}) => {
|
|
2644
|
-
return provider({
|
|
2645
|
-
fetch: (...params) => fetchWithContextCall(context, agentCallParams, ...params),
|
|
2646
|
-
...providerParams
|
|
2647
|
-
});
|
|
2648
|
-
};
|
|
2649
|
-
var wrapTools = ({
|
|
2650
|
-
context,
|
|
2651
|
-
tools
|
|
2652
|
-
}) => {
|
|
2653
|
-
return Object.fromEntries(
|
|
2654
|
-
Object.entries(tools).map((toolInfo) => {
|
|
2655
|
-
const [toolName, tool3] = toolInfo;
|
|
2656
|
-
const executeAsStep = "executeAsStep" in tool3 ? tool3.executeAsStep : true;
|
|
2657
|
-
const aiSDKTool = convertToAISDKTool(tool3);
|
|
2658
|
-
const execute = aiSDKTool.execute;
|
|
2659
|
-
if (execute && executeAsStep) {
|
|
2660
|
-
const wrappedExecute = (...params) => {
|
|
2661
|
-
return context.run(`Run tool ${toolName}`, () => execute(...params));
|
|
2662
|
-
};
|
|
2663
|
-
aiSDKTool.execute = wrappedExecute;
|
|
2664
|
-
}
|
|
2665
|
-
return [toolName, aiSDKTool];
|
|
2666
|
-
})
|
|
2667
|
-
);
|
|
2668
|
-
};
|
|
2669
|
-
var convertToAISDKTool = (tool3) => {
|
|
2670
|
-
const isLangchainTool = "invoke" in tool3;
|
|
2671
|
-
return isLangchainTool ? convertLangchainTool(tool3) : tool3;
|
|
2672
|
-
};
|
|
2673
|
-
var convertLangchainTool = (langchainTool) => {
|
|
2674
|
-
return (0, import_ai.tool)({
|
|
2675
|
-
description: langchainTool.description,
|
|
2676
|
-
parameters: langchainTool.schema,
|
|
2677
|
-
execute: async (...param) => langchainTool.invoke(...param)
|
|
2678
|
-
});
|
|
2679
|
-
};
|
|
2680
|
-
|
|
2681
|
-
// src/agents/agent.ts
|
|
2682
|
-
var import_zod = require("zod");
|
|
2683
|
-
var import_ai2 = require("ai");
|
|
2684
|
-
|
|
2685
|
-
// src/serve/utils.ts
|
|
2686
|
-
var isDisabledWorkflowContext = (context) => {
|
|
2687
|
-
return "disabled" in context;
|
|
2688
|
-
};
|
|
2689
|
-
|
|
2690
|
-
// src/agents/agent.ts
|
|
2691
|
-
var Agent = class {
|
|
2692
|
-
name;
|
|
2693
|
-
tools;
|
|
2694
|
-
maxSteps;
|
|
2695
|
-
background;
|
|
2696
|
-
model;
|
|
2697
|
-
temparature;
|
|
2698
|
-
context;
|
|
2699
|
-
constructor({ tools, maxSteps, background, name, model, temparature = 0.1 }, context) {
|
|
2700
|
-
this.name = name;
|
|
2701
|
-
this.tools = tools ?? {};
|
|
2702
|
-
this.maxSteps = maxSteps;
|
|
2703
|
-
this.background = background;
|
|
2704
|
-
this.model = model;
|
|
2705
|
-
this.temparature = temparature;
|
|
2706
|
-
this.context = context;
|
|
2707
|
-
}
|
|
2708
|
-
/**
|
|
2709
|
-
* Trigger the agent by passing a prompt
|
|
2710
|
-
*
|
|
2711
|
-
* @param prompt task to assign to the agent
|
|
2712
|
-
* @returns Response as `{ text: string }`
|
|
2713
|
-
*/
|
|
2714
|
-
async call({ prompt }) {
|
|
2715
|
-
try {
|
|
2716
|
-
if (isDisabledWorkflowContext(this.context)) {
|
|
2717
|
-
await this.context.sleep("abort", 0);
|
|
2718
|
-
}
|
|
2719
|
-
const result = await (0, import_ai2.generateText)({
|
|
2720
|
-
model: this.model,
|
|
2721
|
-
tools: this.tools,
|
|
2722
|
-
maxSteps: this.maxSteps,
|
|
2723
|
-
system: this.background,
|
|
2724
|
-
prompt,
|
|
2725
|
-
headers: {
|
|
2726
|
-
[AGENT_NAME_HEADER]: this.name
|
|
2727
|
-
},
|
|
2728
|
-
temperature: this.temparature
|
|
2729
|
-
});
|
|
2730
|
-
return { text: result.text };
|
|
2731
|
-
} catch (error) {
|
|
2732
|
-
if (error instanceof import_ai2.ToolExecutionError) {
|
|
2733
|
-
if (error.cause instanceof Error && error.cause.name === "WorkflowAbort") {
|
|
2734
|
-
throw error.cause;
|
|
2735
|
-
} else if (error.cause instanceof import_ai2.ToolExecutionError && error.cause.cause instanceof Error && error.cause.cause.name === "WorkflowAbort") {
|
|
2736
|
-
throw error.cause.cause;
|
|
2737
|
-
} else {
|
|
2738
|
-
throw error;
|
|
2739
|
-
}
|
|
2740
|
-
} else {
|
|
2741
|
-
throw error;
|
|
2742
|
-
}
|
|
2743
|
-
}
|
|
2744
|
-
}
|
|
2745
|
-
/**
|
|
2746
|
-
* Convert the agent to a tool which can be used by other agents.
|
|
2747
|
-
*
|
|
2748
|
-
* @returns the agent as a tool
|
|
2749
|
-
*/
|
|
2750
|
-
asTool() {
|
|
2751
|
-
const toolDescriptions = Object.values(this.tools).map((tool3) => tool3.description).join("\n");
|
|
2752
|
-
return (0, import_ai2.tool)({
|
|
2753
|
-
parameters: import_zod.z.object({ prompt: import_zod.z.string() }),
|
|
2754
|
-
execute: async ({ prompt }) => {
|
|
2755
|
-
return await this.call({ prompt });
|
|
2756
|
-
},
|
|
2757
|
-
description: `An AI Agent with the following background: ${this.background}Has access to the following tools: ${toolDescriptions}`
|
|
2758
|
-
});
|
|
2759
|
-
}
|
|
2760
|
-
};
|
|
2761
|
-
var ManagerAgent = class extends Agent {
|
|
2762
|
-
agents;
|
|
2763
|
-
/**
|
|
2764
|
-
* A manager agent which coordinates agents available to it to achieve a
|
|
2765
|
-
* given task
|
|
2766
|
-
*
|
|
2767
|
-
* @param name Name of the agent
|
|
2768
|
-
* @param background Background of the agent. If not passed, default will be used.
|
|
2769
|
-
* @param model LLM model to use
|
|
2770
|
-
* @param agents: List of agents available to the agent
|
|
2771
|
-
* @param maxSteps number of times the manager agent can call the LLM at most.
|
|
2772
|
-
* If the agent abruptly stops execution after calling other agents, you may
|
|
2773
|
-
* need to increase maxSteps
|
|
2774
|
-
*/
|
|
2775
|
-
constructor({
|
|
2776
|
-
agents,
|
|
2777
|
-
background = MANAGER_AGENT_PROMPT,
|
|
2778
|
-
model,
|
|
2779
|
-
maxSteps,
|
|
2780
|
-
name = "manager llm"
|
|
2781
|
-
}, context) {
|
|
2782
|
-
super(
|
|
2783
|
-
{
|
|
2784
|
-
background,
|
|
2785
|
-
maxSteps,
|
|
2786
|
-
tools: Object.fromEntries(agents.map((agent) => [agent.name, agent.asTool()])),
|
|
2787
|
-
name,
|
|
2788
|
-
model
|
|
2789
|
-
},
|
|
2790
|
-
context
|
|
2791
|
-
);
|
|
2792
|
-
this.agents = agents;
|
|
2793
|
-
}
|
|
2794
|
-
};
|
|
2795
|
-
|
|
2796
|
-
// src/agents/task.ts
|
|
2797
|
-
var Task = class {
|
|
2798
|
-
context;
|
|
2799
|
-
taskParameters;
|
|
2800
|
-
constructor({
|
|
2801
|
-
context,
|
|
2802
|
-
taskParameters
|
|
2803
|
-
}) {
|
|
2804
|
-
this.context = context;
|
|
2805
|
-
this.taskParameters = taskParameters;
|
|
2806
|
-
}
|
|
2807
|
-
/**
|
|
2808
|
-
* Run the agents to complete the task
|
|
2809
|
-
*
|
|
2810
|
-
* @returns Result of the task as { text: string }
|
|
2811
|
-
*/
|
|
2812
|
-
async run() {
|
|
2813
|
-
const { prompt, ...otherParams } = this.taskParameters;
|
|
2814
|
-
if ("agent" in otherParams) {
|
|
2815
|
-
const agent = otherParams.agent;
|
|
2816
|
-
const result = await agent.call({
|
|
2817
|
-
prompt
|
|
2818
|
-
});
|
|
2819
|
-
return { text: result.text };
|
|
2820
|
-
} else {
|
|
2821
|
-
const { agents, maxSteps, model, background } = otherParams;
|
|
2822
|
-
const managerAgent = new ManagerAgent(
|
|
2823
|
-
{
|
|
2824
|
-
model,
|
|
2825
|
-
maxSteps,
|
|
2826
|
-
agents,
|
|
2827
|
-
name: "Manager LLM",
|
|
2828
|
-
background
|
|
2829
|
-
},
|
|
2830
|
-
this.context
|
|
2831
|
-
);
|
|
2832
|
-
const result = await managerAgent.call({ prompt });
|
|
2833
|
-
return { text: result.text };
|
|
2834
|
-
}
|
|
2835
|
-
}
|
|
2836
|
-
};
|
|
2837
|
-
|
|
2838
|
-
// src/agents/index.ts
|
|
2839
|
-
var WorkflowAgents = class {
|
|
2840
|
-
context;
|
|
2841
|
-
constructor({ context }) {
|
|
2842
|
-
this.context = context;
|
|
2843
|
-
}
|
|
2844
|
-
/**
|
|
2845
|
-
* Defines an agent
|
|
2846
|
-
*
|
|
2847
|
-
* ```ts
|
|
2848
|
-
* const researcherAgent = context.agents.agent({
|
|
2849
|
-
* model,
|
|
2850
|
-
* name: 'academic',
|
|
2851
|
-
* maxSteps: 2,
|
|
2852
|
-
* tools: {
|
|
2853
|
-
* wikiTool: new WikipediaQueryRun({
|
|
2854
|
-
* topKResults: 1,
|
|
2855
|
-
* maxDocContentLength: 500,
|
|
2856
|
-
* })
|
|
2857
|
-
* },
|
|
2858
|
-
* background:
|
|
2859
|
-
* 'You are researcher agent with access to Wikipedia. ' +
|
|
2860
|
-
* 'Utilize Wikipedia as much as possible for correct information',
|
|
2861
|
-
* });
|
|
2862
|
-
* ```
|
|
2863
|
-
*
|
|
2864
|
-
* @param params agent parameters
|
|
2865
|
-
* @returns
|
|
2866
|
-
*/
|
|
2867
|
-
agent(params) {
|
|
2868
|
-
const wrappedTools = wrapTools({ context: this.context, tools: params.tools });
|
|
2869
|
-
return new Agent(
|
|
2870
|
-
{
|
|
2871
|
-
...params,
|
|
2872
|
-
tools: wrappedTools
|
|
2873
|
-
},
|
|
2874
|
-
this.context
|
|
2875
|
-
);
|
|
2876
|
-
}
|
|
2877
|
-
task(taskParameters) {
|
|
2878
|
-
return new Task({ context: this.context, taskParameters });
|
|
2879
|
-
}
|
|
2880
|
-
/**
|
|
2881
|
-
* creates an openai model for agents
|
|
2882
|
-
*/
|
|
2883
|
-
openai(...params) {
|
|
2884
|
-
const [model, settings] = params;
|
|
2885
|
-
const { baseURL, apiKey, callSettings, ...otherSettings } = settings ?? {};
|
|
2886
|
-
const openaiModel = this.AISDKModel({
|
|
2887
|
-
context: this.context,
|
|
2888
|
-
provider: import_openai2.createOpenAI,
|
|
2889
|
-
providerParams: { baseURL, apiKey, compatibility: "strict" },
|
|
2890
|
-
agentCallParams: callSettings
|
|
2891
|
-
});
|
|
2892
|
-
return openaiModel(model, otherSettings);
|
|
2893
|
-
}
|
|
2894
|
-
AISDKModel = createWorkflowModel;
|
|
2895
|
-
};
|
|
2896
|
-
|
|
2897
2611
|
// src/serve/serve-many.ts
|
|
2898
2612
|
var getWorkflowId = (url) => {
|
|
2899
2613
|
const components = url.split("/");
|
|
@@ -3202,7 +2916,7 @@ var WorkflowContext = class {
|
|
|
3202
2916
|
* @returns result of the step function
|
|
3203
2917
|
*/
|
|
3204
2918
|
async run(stepName, stepFunction) {
|
|
3205
|
-
const wrappedStepFunction = () => this.executor.wrapStep(stepName, stepFunction);
|
|
2919
|
+
const wrappedStepFunction = (() => this.executor.wrapStep(stepName, stepFunction));
|
|
3206
2920
|
return await this.addStep(new LazyFunctionStep(stepName, wrappedStepFunction));
|
|
3207
2921
|
}
|
|
3208
2922
|
/**
|
|
@@ -3373,11 +3087,6 @@ var WorkflowContext = class {
|
|
|
3373
3087
|
context: this
|
|
3374
3088
|
});
|
|
3375
3089
|
}
|
|
3376
|
-
get agents() {
|
|
3377
|
-
return new WorkflowAgents({
|
|
3378
|
-
context: this
|
|
3379
|
-
});
|
|
3380
|
-
}
|
|
3381
3090
|
};
|
|
3382
3091
|
|
|
3383
3092
|
// src/logger.ts
|
|
@@ -3481,7 +3190,7 @@ var DisabledWorkflowContext = class _DisabledWorkflowContext extends WorkflowCon
|
|
|
3481
3190
|
try {
|
|
3482
3191
|
await routeFunction(disabledContext);
|
|
3483
3192
|
} catch (error) {
|
|
3484
|
-
if (error
|
|
3193
|
+
if (isInstanceOf(error, WorkflowAbort) && error.stepName === this.disabledMessage || isInstanceOf(error, WorkflowNonRetryableError) || isInstanceOf(error, WorkflowRetryAfterError)) {
|
|
3485
3194
|
return ok("step-found");
|
|
3486
3195
|
}
|
|
3487
3196
|
console.warn(
|
|
@@ -3734,13 +3443,24 @@ var processOptions = (options) => {
|
|
|
3734
3443
|
},
|
|
3735
3444
|
status: 489
|
|
3736
3445
|
});
|
|
3737
|
-
} else if (detailedFinishCondition?.condition === "
|
|
3738
|
-
return new Response(detailedFinishCondition.result
|
|
3739
|
-
status: 200,
|
|
3446
|
+
} else if (detailedFinishCondition?.condition === "retry-after-error") {
|
|
3447
|
+
return new Response(JSON.stringify(formatWorkflowError(detailedFinishCondition.result)), {
|
|
3740
3448
|
headers: {
|
|
3449
|
+
"Retry-After": detailedFinishCondition.result.retryAfter.toString(),
|
|
3741
3450
|
[WORKFLOW_PROTOCOL_VERSION_HEADER]: WORKFLOW_PROTOCOL_VERSION
|
|
3742
|
-
}
|
|
3451
|
+
},
|
|
3452
|
+
status: 429
|
|
3743
3453
|
});
|
|
3454
|
+
} else if (detailedFinishCondition?.condition === "failure-callback") {
|
|
3455
|
+
return new Response(
|
|
3456
|
+
JSON.stringify({ result: detailedFinishCondition.result ?? void 0 }),
|
|
3457
|
+
{
|
|
3458
|
+
status: 200,
|
|
3459
|
+
headers: {
|
|
3460
|
+
[WORKFLOW_PROTOCOL_VERSION_HEADER]: WORKFLOW_PROTOCOL_VERSION
|
|
3461
|
+
}
|
|
3462
|
+
}
|
|
3463
|
+
);
|
|
3744
3464
|
}
|
|
3745
3465
|
return new Response(JSON.stringify({ workflowRunId }), {
|
|
3746
3466
|
status: 200,
|
|
@@ -3950,12 +3670,18 @@ var serveBase = (routeFunction, telemetry2, options) => {
|
|
|
3950
3670
|
},
|
|
3951
3671
|
debug
|
|
3952
3672
|
});
|
|
3953
|
-
if (result.isOk() && result.value
|
|
3673
|
+
if (result.isOk() && isInstanceOf(result.value, WorkflowNonRetryableError)) {
|
|
3954
3674
|
return onStepFinish(workflowRunId, result.value, {
|
|
3955
3675
|
condition: "non-retryable-error",
|
|
3956
3676
|
result: result.value
|
|
3957
3677
|
});
|
|
3958
3678
|
}
|
|
3679
|
+
if (result.isOk() && isInstanceOf(result.value, WorkflowRetryAfterError)) {
|
|
3680
|
+
return onStepFinish(workflowRunId, result.value, {
|
|
3681
|
+
condition: "retry-after-error",
|
|
3682
|
+
result: result.value
|
|
3683
|
+
});
|
|
3684
|
+
}
|
|
3959
3685
|
if (result.isErr()) {
|
|
3960
3686
|
await debug?.log("ERROR", "ERROR", { error: result.error.message });
|
|
3961
3687
|
throw result.error;
|
|
@@ -3986,7 +3712,7 @@ var serveBase = (routeFunction, telemetry2, options) => {
|
|
|
3986
3712
|
const errorMessage = `Error while running onError callback: '${formattedOnErrorError.message}'.
|
|
3987
3713
|
Original error: '${formattedError.message}'`;
|
|
3988
3714
|
console.error(errorMessage);
|
|
3989
|
-
return new Response(errorMessage, {
|
|
3715
|
+
return new Response(JSON.stringify({ error: errorMessage }), {
|
|
3990
3716
|
status: 500,
|
|
3991
3717
|
headers: {
|
|
3992
3718
|
[WORKFLOW_PROTOCOL_VERSION_HEADER]: WORKFLOW_PROTOCOL_VERSION
|