@keystrokehq/keystroke 0.1.20 → 0.1.23
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/dist/agent.cjs +72 -54
- package/dist/agent.cjs.map +1 -1
- package/dist/agent.d.cts +2 -2
- package/dist/agent.d.mts +2 -2
- package/dist/agent.mjs +71 -52
- package/dist/agent.mjs.map +1 -1
- package/dist/config.d.cts +3 -1
- package/dist/config.d.cts.map +1 -1
- package/dist/config.d.mts +3 -1
- package/dist/config.d.mts.map +1 -1
- package/dist/{dist-KpQEao0i.mjs → dist-B1Pjv-cM.mjs} +666 -21
- package/dist/dist-B1Pjv-cM.mjs.map +1 -0
- package/dist/{dist-BFCT4aiu.cjs → dist-D69ocJ7u.cjs} +728 -23
- package/dist/dist-D69ocJ7u.cjs.map +1 -0
- package/dist/{index-Dlu1kaci.d.cts → index-BQOZkLWG.d.cts} +10 -34
- package/dist/index-BQOZkLWG.d.cts.map +1 -0
- package/dist/{index-I9DneAMW.d.mts → index-CGf1NwA4.d.mts} +10 -34
- package/dist/index-CGf1NwA4.d.mts.map +1 -0
- package/dist/{index-BUYoOHa1.d.mts → index-WHR4qX4x.d.mts} +10 -3
- package/dist/index-WHR4qX4x.d.mts.map +1 -0
- package/dist/{index-Dzm0OSN4.d.cts → index-wpyFrBvl.d.cts} +10 -3
- package/dist/index-wpyFrBvl.d.cts.map +1 -0
- package/dist/{token-dA7JRmgB.cjs → token-DZEJHEnH.cjs} +2 -2
- package/dist/{token-dA7JRmgB.cjs.map → token-DZEJHEnH.cjs.map} +1 -1
- package/dist/{token-shJjdG3B.mjs → token-D_x8iwF8.mjs} +2 -2
- package/dist/{token-shJjdG3B.mjs.map → token-D_x8iwF8.mjs.map} +1 -1
- package/dist/trigger.cjs +1 -1
- package/dist/trigger.d.cts +2 -2
- package/dist/trigger.d.mts +2 -2
- package/dist/trigger.mjs +1 -1
- package/dist/workflow.cjs +3 -1
- package/dist/workflow.d.cts +2 -2
- package/dist/workflow.d.mts +2 -2
- package/dist/workflow.mjs +2 -2
- package/package.json +1 -1
- package/dist/dist-BFCT4aiu.cjs.map +0 -1
- package/dist/dist-BekOBuw8.cjs +0 -665
- package/dist/dist-BekOBuw8.cjs.map +0 -1
- package/dist/dist-C-1uJk5f.mjs +0 -612
- package/dist/dist-C-1uJk5f.mjs.map +0 -1
- package/dist/dist-KpQEao0i.mjs.map +0 -1
- package/dist/index-BUYoOHa1.d.mts.map +0 -1
- package/dist/index-Dlu1kaci.d.cts.map +0 -1
- package/dist/index-Dzm0OSN4.d.cts.map +0 -1
- package/dist/index-I9DneAMW.d.mts.map +0 -1
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
const require_chunk = require("./chunk-emK7D4bc.cjs");
|
|
2
2
|
const require_dist$3 = require("./dist-BKL5duJl.cjs");
|
|
3
|
+
const require_dist$4 = require("./dist-DqZUy3u_.cjs");
|
|
3
4
|
let zod = require("zod");
|
|
4
5
|
let node_async_hooks = require("node:async_hooks");
|
|
5
6
|
let zod_v4 = require("zod/v4");
|
|
@@ -2180,7 +2181,7 @@ function asSchema(schema) {
|
|
|
2180
2181
|
type: "object",
|
|
2181
2182
|
properties: {},
|
|
2182
2183
|
additionalProperties: false
|
|
2183
|
-
}) : isSchema(schema) ? schema : "~standard" in schema ? schema["~standard"].vendor === "zod" ? zodSchema(schema) : standardSchema(schema) : schema();
|
|
2184
|
+
}) : isSchema(schema) ? schema : "~standard" in schema ? schema["~standard"].vendor === "zod" ? zodSchema$1(schema) : standardSchema(schema) : schema();
|
|
2184
2185
|
}
|
|
2185
2186
|
function standardSchema(standardSchema2) {
|
|
2186
2187
|
return jsonSchema(() => addAdditionalPropertiesToJsonSchema(standardSchema2["~standard"].jsonSchema.input({ target: "draft-07" })), { validate: async (value) => {
|
|
@@ -2232,7 +2233,7 @@ function zod4Schema(zodSchema2, options) {
|
|
|
2232
2233
|
function isZod4Schema(zodSchema2) {
|
|
2233
2234
|
return "_zod" in zodSchema2;
|
|
2234
2235
|
}
|
|
2235
|
-
function zodSchema(zodSchema2, options) {
|
|
2236
|
+
function zodSchema$1(zodSchema2, options) {
|
|
2236
2237
|
if (isZod4Schema(zodSchema2)) return zod4Schema(zodSchema2, options);
|
|
2237
2238
|
else return zod3Schema(zodSchema2, options);
|
|
2238
2239
|
}
|
|
@@ -2693,7 +2694,7 @@ var require_get_vercel_oidc_token = /* @__PURE__ */ require_chunk.__commonJSMin(
|
|
|
2693
2694
|
err = error;
|
|
2694
2695
|
}
|
|
2695
2696
|
try {
|
|
2696
|
-
const [{ getTokenPayload, isExpired }, { refreshToken }] = await Promise.all([await Promise.resolve().then(() => /* @__PURE__ */ require_chunk.__toESM(require_token_util())), await Promise.resolve().then(() => /* @__PURE__ */ require_chunk.__toESM(require("./token-
|
|
2697
|
+
const [{ getTokenPayload, isExpired }, { refreshToken }] = await Promise.all([await Promise.resolve().then(() => /* @__PURE__ */ require_chunk.__toESM(require_token_util())), await Promise.resolve().then(() => /* @__PURE__ */ require_chunk.__toESM(require("./token-DZEJHEnH.cjs").default))]);
|
|
2697
2698
|
if (!token || isExpired(getTokenPayload(token), options?.expirationBufferMs)) {
|
|
2698
2699
|
await refreshToken(options);
|
|
2699
2700
|
token = getVercelOidcTokenSync();
|
|
@@ -3277,7 +3278,7 @@ var GatewayRateLimitError = class extends (_b4 = GatewayError, _a4$1 = symbol4$1
|
|
|
3277
3278
|
var name4$1 = "GatewayModelNotFoundError";
|
|
3278
3279
|
var marker5$1 = `vercel.ai.gateway.error.${name4$1}`;
|
|
3279
3280
|
var symbol5$1 = Symbol.for(marker5$1);
|
|
3280
|
-
var modelNotFoundParamSchema = lazySchema(() => zodSchema(zod_v4.z.object({ modelId: zod_v4.z.string() })));
|
|
3281
|
+
var modelNotFoundParamSchema = lazySchema(() => zodSchema$1(zod_v4.z.object({ modelId: zod_v4.z.string() })));
|
|
3281
3282
|
var _a5$1, _b5;
|
|
3282
3283
|
var GatewayModelNotFoundError = class extends (_b5 = GatewayError, _a5$1 = symbol5$1, _b5) {
|
|
3283
3284
|
constructor({ message = "Model not found", statusCode = 404, modelId, cause, generationId } = {}) {
|
|
@@ -3458,7 +3459,7 @@ async function createGatewayErrorFromResponse({ response, statusCode, defaultMes
|
|
|
3458
3459
|
});
|
|
3459
3460
|
}
|
|
3460
3461
|
}
|
|
3461
|
-
var gatewayErrorResponseSchema = lazySchema(() => zodSchema(zod_v4.z.object({
|
|
3462
|
+
var gatewayErrorResponseSchema = lazySchema(() => zodSchema$1(zod_v4.z.object({
|
|
3462
3463
|
error: zod_v4.z.object({
|
|
3463
3464
|
message: zod_v4.z.string(),
|
|
3464
3465
|
type: zod_v4.z.string().nullish(),
|
|
@@ -3556,7 +3557,7 @@ async function parseAuthMethod(headers) {
|
|
|
3556
3557
|
});
|
|
3557
3558
|
return result.success ? result.value : void 0;
|
|
3558
3559
|
}
|
|
3559
|
-
var gatewayAuthMethodSchema = lazySchema(() => zodSchema(zod_v4.z.union([zod_v4.z.literal("api-key"), zod_v4.z.literal("oidc")])));
|
|
3560
|
+
var gatewayAuthMethodSchema = lazySchema(() => zodSchema$1(zod_v4.z.union([zod_v4.z.literal("api-key"), zod_v4.z.literal("oidc")])));
|
|
3560
3561
|
var KNOWN_MODEL_TYPES = [
|
|
3561
3562
|
"embedding",
|
|
3562
3563
|
"image",
|
|
@@ -3606,7 +3607,7 @@ var GatewayFetchMetadata = class {
|
|
|
3606
3607
|
}
|
|
3607
3608
|
}
|
|
3608
3609
|
};
|
|
3609
|
-
var gatewayAvailableModelsResponseSchema = lazySchema(() => zodSchema(zod_v4.z.object({ models: zod_v4.z.array(zod_v4.z.object({
|
|
3610
|
+
var gatewayAvailableModelsResponseSchema = lazySchema(() => zodSchema$1(zod_v4.z.object({ models: zod_v4.z.array(zod_v4.z.object({
|
|
3610
3611
|
id: zod_v4.z.string(),
|
|
3611
3612
|
name: zod_v4.z.string(),
|
|
3612
3613
|
description: zod_v4.z.string().nullish(),
|
|
@@ -3628,7 +3629,7 @@ var gatewayAvailableModelsResponseSchema = lazySchema(() => zodSchema(zod_v4.z.o
|
|
|
3628
3629
|
}),
|
|
3629
3630
|
modelType: zod_v4.z.string().nullish()
|
|
3630
3631
|
})).transform((models) => models.filter((m) => m.modelType == null || KNOWN_MODEL_TYPES.includes(m.modelType))) })));
|
|
3631
|
-
var gatewayCreditsResponseSchema = lazySchema(() => zodSchema(zod_v4.z.object({
|
|
3632
|
+
var gatewayCreditsResponseSchema = lazySchema(() => zodSchema$1(zod_v4.z.object({
|
|
3632
3633
|
balance: zod_v4.z.string(),
|
|
3633
3634
|
total_used: zod_v4.z.string()
|
|
3634
3635
|
}).transform(({ balance, total_used }) => ({
|
|
@@ -3668,7 +3669,7 @@ var GatewaySpendReport = class {
|
|
|
3668
3669
|
}
|
|
3669
3670
|
}
|
|
3670
3671
|
};
|
|
3671
|
-
var gatewaySpendReportResponseSchema = lazySchema(() => zodSchema(zod_v4.z.object({ results: zod_v4.z.array(zod_v4.z.object({
|
|
3672
|
+
var gatewaySpendReportResponseSchema = lazySchema(() => zodSchema$1(zod_v4.z.object({ results: zod_v4.z.array(zod_v4.z.object({
|
|
3672
3673
|
day: zod_v4.z.string().optional(),
|
|
3673
3674
|
hour: zod_v4.z.string().optional(),
|
|
3674
3675
|
user: zod_v4.z.string().optional(),
|
|
@@ -3718,7 +3719,7 @@ var GatewayGenerationInfoFetcher = class {
|
|
|
3718
3719
|
}
|
|
3719
3720
|
}
|
|
3720
3721
|
};
|
|
3721
|
-
var gatewayGenerationInfoResponseSchema = lazySchema(() => zodSchema(zod_v4.z.object({ data: zod_v4.z.object({
|
|
3722
|
+
var gatewayGenerationInfoResponseSchema = lazySchema(() => zodSchema$1(zod_v4.z.object({ data: zod_v4.z.object({
|
|
3722
3723
|
id: zod_v4.z.string(),
|
|
3723
3724
|
total_cost: zod_v4.z.number(),
|
|
3724
3725
|
upstream_inference_cost: zod_v4.z.number(),
|
|
@@ -3970,7 +3971,7 @@ var gatewayEmbeddingWarningSchema = zod_v4.z.discriminatedUnion("type", [
|
|
|
3970
3971
|
message: zod_v4.z.string()
|
|
3971
3972
|
})
|
|
3972
3973
|
]);
|
|
3973
|
-
var gatewayEmbeddingResponseSchema = lazySchema(() => zodSchema(zod_v4.z.object({
|
|
3974
|
+
var gatewayEmbeddingResponseSchema = lazySchema(() => zodSchema$1(zod_v4.z.object({
|
|
3974
3975
|
embeddings: zod_v4.z.array(zod_v4.z.array(zod_v4.z.number())),
|
|
3975
3976
|
usage: zod_v4.z.object({ tokens: zod_v4.z.number() }).nullish(),
|
|
3976
3977
|
warnings: zod_v4.z.array(gatewayEmbeddingWarningSchema).optional(),
|
|
@@ -4322,7 +4323,7 @@ var gatewayRerankingWarningSchema = zod_v4.z.discriminatedUnion("type", [
|
|
|
4322
4323
|
message: zod_v4.z.string()
|
|
4323
4324
|
})
|
|
4324
4325
|
]);
|
|
4325
|
-
var gatewayRerankingResponseSchema = lazySchema(() => zodSchema(zod_v4.z.object({
|
|
4326
|
+
var gatewayRerankingResponseSchema = lazySchema(() => zodSchema$1(zod_v4.z.object({
|
|
4326
4327
|
ranking: zod_v4.z.array(zod_v4.z.object({
|
|
4327
4328
|
index: zod_v4.z.number(),
|
|
4328
4329
|
relevanceScore: zod_v4.z.number()
|
|
@@ -4557,7 +4558,7 @@ function toGatewayRealtimeUrl(baseURL, modelId) {
|
|
|
4557
4558
|
}
|
|
4558
4559
|
var exaSearchToolFactory = createProviderExecutedToolFactory({
|
|
4559
4560
|
id: "gateway.exa_search",
|
|
4560
|
-
inputSchema: lazySchema(() => zodSchema(zod.z.object({
|
|
4561
|
+
inputSchema: lazySchema(() => zodSchema$1(zod.z.object({
|
|
4561
4562
|
query: zod.z.string().describe("Natural-language web search query. This is required."),
|
|
4562
4563
|
type: zod.z.enum([
|
|
4563
4564
|
"auto",
|
|
@@ -4620,7 +4621,7 @@ var exaSearchToolFactory = createProviderExecutedToolFactory({
|
|
|
4620
4621
|
}).optional()
|
|
4621
4622
|
}).optional().describe("Controls extracted page content and freshness.")
|
|
4622
4623
|
}))),
|
|
4623
|
-
outputSchema: lazySchema(() => zodSchema(zod.z.union([zod.z.object({
|
|
4624
|
+
outputSchema: lazySchema(() => zodSchema$1(zod.z.union([zod.z.object({
|
|
4624
4625
|
requestId: zod.z.string(),
|
|
4625
4626
|
searchType: zod.z.string().optional(),
|
|
4626
4627
|
resolvedSearchType: zod.z.string().optional(),
|
|
@@ -4663,7 +4664,7 @@ var exaSearchToolFactory = createProviderExecutedToolFactory({
|
|
|
4663
4664
|
var exaSearch = (config = {}) => exaSearchToolFactory(config);
|
|
4664
4665
|
var parallelSearchToolFactory = createProviderExecutedToolFactory({
|
|
4665
4666
|
id: "gateway.parallel_search",
|
|
4666
|
-
inputSchema: lazySchema(() => zodSchema(zod.z.object({
|
|
4667
|
+
inputSchema: lazySchema(() => zodSchema$1(zod.z.object({
|
|
4667
4668
|
objective: zod.z.string().describe("Natural-language description of the web research goal, including source or freshness guidance and broader context from the task. Maximum 5000 characters."),
|
|
4668
4669
|
search_queries: zod.z.array(zod.z.string()).optional().describe("Optional search queries to supplement the objective. Maximum 200 characters per query."),
|
|
4669
4670
|
mode: zod.z.enum(["one-shot", "agentic"]).optional().describe("Mode preset: \"one-shot\" for comprehensive results with longer excerpts (default), \"agentic\" for concise, token-efficient results for multi-step workflows."),
|
|
@@ -4679,7 +4680,7 @@ var parallelSearchToolFactory = createProviderExecutedToolFactory({
|
|
|
4679
4680
|
}).optional().describe("Excerpt configuration for controlling result length."),
|
|
4680
4681
|
fetch_policy: zod.z.object({ max_age_seconds: zod.z.number().optional().describe("Maximum age in seconds for cached content. Set to 0 to always fetch fresh content.") }).optional().describe("Fetch policy for controlling content freshness.")
|
|
4681
4682
|
}))),
|
|
4682
|
-
outputSchema: lazySchema(() => zodSchema(zod.z.union([zod.z.object({
|
|
4683
|
+
outputSchema: lazySchema(() => zodSchema$1(zod.z.union([zod.z.object({
|
|
4683
4684
|
searchId: zod.z.string(),
|
|
4684
4685
|
results: zod.z.array(zod.z.object({
|
|
4685
4686
|
url: zod.z.string(),
|
|
@@ -4704,7 +4705,7 @@ var parallelSearchToolFactory = createProviderExecutedToolFactory({
|
|
|
4704
4705
|
var parallelSearch = (config = {}) => parallelSearchToolFactory(config);
|
|
4705
4706
|
var perplexitySearchToolFactory = createProviderExecutedToolFactory({
|
|
4706
4707
|
id: "gateway.perplexity_search",
|
|
4707
|
-
inputSchema: lazySchema(() => zodSchema(zod.z.object({
|
|
4708
|
+
inputSchema: lazySchema(() => zodSchema$1(zod.z.object({
|
|
4708
4709
|
query: zod.z.union([zod.z.string(), zod.z.array(zod.z.string())]).describe("Search query (string) or multiple queries (array of up to 5 strings). Multi-query searches return combined results from all queries."),
|
|
4709
4710
|
max_results: zod.z.number().optional().describe("Maximum number of search results to return (1-20, default: 10)"),
|
|
4710
4711
|
max_tokens_per_page: zod.z.number().optional().describe("Maximum number of tokens to extract per search result page (256-2048, default: 2048)"),
|
|
@@ -4723,7 +4724,7 @@ var perplexitySearchToolFactory = createProviderExecutedToolFactory({
|
|
|
4723
4724
|
"year"
|
|
4724
4725
|
]).optional().describe("Filter results by relative time period. Cannot be used with search_after_date or search_before_date.")
|
|
4725
4726
|
}))),
|
|
4726
|
-
outputSchema: lazySchema(() => zodSchema(zod.z.union([zod.z.object({
|
|
4727
|
+
outputSchema: lazySchema(() => zodSchema$1(zod.z.union([zod.z.object({
|
|
4727
4728
|
results: zod.z.array(zod.z.object({
|
|
4728
4729
|
title: zod.z.string(),
|
|
4729
4730
|
url: zod.z.string(),
|
|
@@ -46108,12 +46109,668 @@ async function resolveActionCredentials(requirements, options) {
|
|
|
46108
46109
|
return parseResolvedCredentials(requirements, await resolveCredentials(requirements, options.consumer, options.contextOverride));
|
|
46109
46110
|
}
|
|
46110
46111
|
//#endregion
|
|
46112
|
+
//#region ../workflow/dist/index.mjs
|
|
46113
|
+
var RunCanceledError = class extends Error {
|
|
46114
|
+
constructor(runId) {
|
|
46115
|
+
super(runId ? `Workflow run ${runId} was canceled` : "Workflow run was canceled");
|
|
46116
|
+
this.name = "RunCanceledError";
|
|
46117
|
+
}
|
|
46118
|
+
};
|
|
46119
|
+
function isRunCanceledError(error) {
|
|
46120
|
+
return error instanceof RunCanceledError;
|
|
46121
|
+
}
|
|
46122
|
+
function executeWorkflowStep(state) {
|
|
46123
|
+
const handle = require_dist$4.getWorkflowRunHandle();
|
|
46124
|
+
if (!handle?.workflowRunner) throw new Error(`Workflow "${state.workflow.slug}" can only be called as a step inside a running workflow. Run it standalone with executeWorkflow(...).`);
|
|
46125
|
+
return handle.workflowRunner(state.workflow, state.input, { id: state.id });
|
|
46126
|
+
}
|
|
46127
|
+
function createWorkflowStepInvocation(state) {
|
|
46128
|
+
return {
|
|
46129
|
+
stepId(id) {
|
|
46130
|
+
return createWorkflowStepInvocation({
|
|
46131
|
+
...state,
|
|
46132
|
+
id
|
|
46133
|
+
});
|
|
46134
|
+
},
|
|
46135
|
+
then(onfulfilled, onrejected) {
|
|
46136
|
+
return executeWorkflowStep(state).then(onfulfilled, onrejected);
|
|
46137
|
+
}
|
|
46138
|
+
};
|
|
46139
|
+
}
|
|
46140
|
+
const zodSchema = zod.z.custom((v) => v instanceof zod.z.ZodType, "must be a Zod schema");
|
|
46141
|
+
/** Runtime validation for an unbranded workflow definition. */
|
|
46142
|
+
const workflowCoreSchema = zod.z.object({
|
|
46143
|
+
slug: zod.z.string().trim().min(1),
|
|
46144
|
+
name: zod.z.string().optional(),
|
|
46145
|
+
description: zod.z.string().optional(),
|
|
46146
|
+
subscription: zod.z.object({ mode: zod.z.enum(["system", "subscribable"]).optional() }).optional(),
|
|
46147
|
+
input: zodSchema,
|
|
46148
|
+
output: zodSchema,
|
|
46149
|
+
run: zod.z.function()
|
|
46150
|
+
});
|
|
46151
|
+
const WORKFLOW = Symbol.for("keystroke.workflow");
|
|
46152
|
+
/**
|
|
46153
|
+
* Validates brand + shape via `workflowCoreSchema` so discovery and guards
|
|
46154
|
+
* reject malformed definitions.
|
|
46155
|
+
*/
|
|
46156
|
+
function isWorkflow(value) {
|
|
46157
|
+
if (typeof value !== "object" || value === null) return false;
|
|
46158
|
+
if (!(WORKFLOW in value) || value[WORKFLOW] !== true) return false;
|
|
46159
|
+
return workflowCoreSchema.safeParse(value).success;
|
|
46160
|
+
}
|
|
46161
|
+
function defineWorkflow(def) {
|
|
46162
|
+
const result = workflowCoreSchema.safeParse(def);
|
|
46163
|
+
if (!result.success) throw new Error(`Invalid workflow definition: ${formatIssues(result.error.issues)}`);
|
|
46164
|
+
const body = def.run;
|
|
46165
|
+
const workflow = {
|
|
46166
|
+
...result.data,
|
|
46167
|
+
[WORKFLOW]: true
|
|
46168
|
+
};
|
|
46169
|
+
workflow.run = ((input, ctx) => {
|
|
46170
|
+
if (ctx !== void 0) return body(input, ctx);
|
|
46171
|
+
return createWorkflowStepInvocation({
|
|
46172
|
+
workflow,
|
|
46173
|
+
input
|
|
46174
|
+
});
|
|
46175
|
+
});
|
|
46176
|
+
return workflow;
|
|
46177
|
+
}
|
|
46178
|
+
function formatIssues(issues) {
|
|
46179
|
+
return issues.map((issue) => {
|
|
46180
|
+
return `${issue.path.length > 0 ? `${issue.path.join(".")}: ` : ""}${issue.message}`;
|
|
46181
|
+
}).join("; ");
|
|
46182
|
+
}
|
|
46183
|
+
function serializeWorkflowError(error) {
|
|
46184
|
+
if (error instanceof Error) return {
|
|
46185
|
+
name: error.name,
|
|
46186
|
+
message: error.message
|
|
46187
|
+
};
|
|
46188
|
+
return { message: String(error) };
|
|
46189
|
+
}
|
|
46190
|
+
/** Rebuild an Error from a recorded error so replay re-raises the same failure. */
|
|
46191
|
+
function deserializeWorkflowError(data) {
|
|
46192
|
+
const serialized = data;
|
|
46193
|
+
const error = new Error(serialized?.message ?? "Workflow step failed");
|
|
46194
|
+
if (serialized?.name) error.name = serialized.name;
|
|
46195
|
+
return error;
|
|
46196
|
+
}
|
|
46197
|
+
const UNIT_MS = {
|
|
46198
|
+
ms: 1,
|
|
46199
|
+
s: 1e3,
|
|
46200
|
+
m: 6e4,
|
|
46201
|
+
h: 36e5,
|
|
46202
|
+
d: 864e5
|
|
46203
|
+
};
|
|
46204
|
+
/** Resolve a sleep duration to an absolute resume time. */
|
|
46205
|
+
function resolveResumeAt(duration, now = /* @__PURE__ */ new Date()) {
|
|
46206
|
+
if (duration instanceof Date) return duration;
|
|
46207
|
+
if (typeof duration === "number") return new Date(now.getTime() + Math.max(0, duration));
|
|
46208
|
+
return new Date(now.getTime() + parseDurationToMs(duration));
|
|
46209
|
+
}
|
|
46210
|
+
function parseDurationToMs(value) {
|
|
46211
|
+
const match = /^(\d+(?:\.\d+)?)\s*(ms|s|m|h|d)$/.exec(value.trim());
|
|
46212
|
+
if (!match) throw new Error(`Invalid sleep duration "${value}". Use a number of ms, a Date, or a string like "5s", "10m", "1h".`);
|
|
46213
|
+
const amount = Number(match[1]);
|
|
46214
|
+
const unit = match[2];
|
|
46215
|
+
return Math.round(amount * UNIT_MS[unit]);
|
|
46216
|
+
}
|
|
46217
|
+
/** Indexes replay events by correlationId for O(1) cache-hit checks during replay. */
|
|
46218
|
+
var EventsConsumer = class {
|
|
46219
|
+
byCorrelationId = /* @__PURE__ */ new Map();
|
|
46220
|
+
constructor(events) {
|
|
46221
|
+
for (const event of events) {
|
|
46222
|
+
if (!event.correlationId) continue;
|
|
46223
|
+
const list = this.byCorrelationId.get(event.correlationId);
|
|
46224
|
+
if (list) list.push(event);
|
|
46225
|
+
else this.byCorrelationId.set(event.correlationId, [event]);
|
|
46226
|
+
}
|
|
46227
|
+
}
|
|
46228
|
+
events(correlationId) {
|
|
46229
|
+
return this.byCorrelationId.get(correlationId) ?? [];
|
|
46230
|
+
}
|
|
46231
|
+
hasEventType(correlationId, type) {
|
|
46232
|
+
return this.events(correlationId).some((event) => event.type === type);
|
|
46233
|
+
}
|
|
46234
|
+
/**
|
|
46235
|
+
* Cache lookup for a step. Returns the recorded output on a `step_completed`,
|
|
46236
|
+
* re-raises the recorded error on a terminal `step_failed` (so a permanently
|
|
46237
|
+
* failed step is not re-executed), and otherwise reports a miss — including
|
|
46238
|
+
* when only `step_retrying` events exist (the step re-runs on the next pass).
|
|
46239
|
+
*/
|
|
46240
|
+
getStepResult(correlationId) {
|
|
46241
|
+
const events = this.events(correlationId);
|
|
46242
|
+
const failed = events.find((event) => event.type === "step_failed");
|
|
46243
|
+
if (failed) throw deserializeWorkflowError(failed.data);
|
|
46244
|
+
const completed = events.find((event) => event.type === "step_completed");
|
|
46245
|
+
if (completed) return {
|
|
46246
|
+
completed: true,
|
|
46247
|
+
result: completed.data
|
|
46248
|
+
};
|
|
46249
|
+
return { completed: false };
|
|
46250
|
+
}
|
|
46251
|
+
/** Number of prior `step_retrying` events recorded for a step (= failed attempts so far). */
|
|
46252
|
+
countRetrying(correlationId) {
|
|
46253
|
+
return this.events(correlationId).filter((event) => event.type === "step_retrying").length;
|
|
46254
|
+
}
|
|
46255
|
+
isSleepCompleted(correlationId) {
|
|
46256
|
+
return this.hasEventType(correlationId, "sleep_completed");
|
|
46257
|
+
}
|
|
46258
|
+
/** Returns the persisted resumeAt for a scheduled-but-not-completed sleep, if any. */
|
|
46259
|
+
getSleepResumeAt(correlationId) {
|
|
46260
|
+
const scheduled = this.events(correlationId).find((event) => event.type === "sleep_scheduled");
|
|
46261
|
+
if (!scheduled) return;
|
|
46262
|
+
const data = scheduled.data;
|
|
46263
|
+
return data?.resumeAt ? new Date(data.resumeAt) : void 0;
|
|
46264
|
+
}
|
|
46265
|
+
getHookToken(correlationId) {
|
|
46266
|
+
return (this.events(correlationId).find((event) => event.type === "hook_created")?.data)?.token;
|
|
46267
|
+
}
|
|
46268
|
+
getHookResult(correlationId) {
|
|
46269
|
+
const resumed = this.events(correlationId).find((event) => event.type === "hook_resumed");
|
|
46270
|
+
if (!resumed) return { resolved: false };
|
|
46271
|
+
return {
|
|
46272
|
+
resolved: true,
|
|
46273
|
+
payload: resumed.data?.payload
|
|
46274
|
+
};
|
|
46275
|
+
}
|
|
46276
|
+
};
|
|
46277
|
+
/** A promise that never settles — returned by a suspending primitive so the body parks. */
|
|
46278
|
+
function createPendingPromise() {
|
|
46279
|
+
return new Promise(() => {});
|
|
46280
|
+
}
|
|
46281
|
+
/**
|
|
46282
|
+
* Collects pending items requested within a single tick and resolves once, so
|
|
46283
|
+
* parallel suspensions (e.g. Promise.all of two sleeps) batch into one suspension.
|
|
46284
|
+
*/
|
|
46285
|
+
var SuspensionCoordinator = class {
|
|
46286
|
+
items = /* @__PURE__ */ new Map();
|
|
46287
|
+
scheduled = false;
|
|
46288
|
+
resolve;
|
|
46289
|
+
promise;
|
|
46290
|
+
constructor() {
|
|
46291
|
+
this.promise = new Promise((resolve) => {
|
|
46292
|
+
this.resolve = resolve;
|
|
46293
|
+
});
|
|
46294
|
+
}
|
|
46295
|
+
request(item) {
|
|
46296
|
+
this.items.set(item.correlationId, item);
|
|
46297
|
+
if (!this.scheduled) {
|
|
46298
|
+
this.scheduled = true;
|
|
46299
|
+
queueMicrotask(() => {
|
|
46300
|
+
this.resolve([...this.items.values()]);
|
|
46301
|
+
});
|
|
46302
|
+
}
|
|
46303
|
+
}
|
|
46304
|
+
waitForSuspension() {
|
|
46305
|
+
return this.promise;
|
|
46306
|
+
}
|
|
46307
|
+
};
|
|
46308
|
+
function createReplayState(params) {
|
|
46309
|
+
return {
|
|
46310
|
+
runId: params.runId,
|
|
46311
|
+
consumer: params.consumer,
|
|
46312
|
+
coordinator: params.coordinator,
|
|
46313
|
+
eventLog: params.eventLog,
|
|
46314
|
+
newEvents: [],
|
|
46315
|
+
now: params.now ?? /* @__PURE__ */ new Date(),
|
|
46316
|
+
hookBaseUrl: params.hookBaseUrl,
|
|
46317
|
+
sleepCounter: 0,
|
|
46318
|
+
hookCounter: 0,
|
|
46319
|
+
stepOccurrences: /* @__PURE__ */ new Map(),
|
|
46320
|
+
stepCorrelationIds: /* @__PURE__ */ new Set()
|
|
46321
|
+
};
|
|
46322
|
+
}
|
|
46323
|
+
/**
|
|
46324
|
+
* Allocate the correlation id for a step. An explicit `.stepId(x)` maps to
|
|
46325
|
+
* `step:x` (and must be unique within a run); otherwise the key is
|
|
46326
|
+
* `step:<actionKey>#<occurrence>` so that two calls to the same action get
|
|
46327
|
+
* distinct, replay-stable ids and inserting an unrelated call never shifts the
|
|
46328
|
+
* ids of later calls (unlike a single global ordinal).
|
|
46329
|
+
*/
|
|
46330
|
+
function nextStepCorrelationId(state, actionKey, explicitId) {
|
|
46331
|
+
if (explicitId !== void 0) {
|
|
46332
|
+
const correlationId = `step:${explicitId}`;
|
|
46333
|
+
if (state.stepCorrelationIds.has(correlationId)) throw new Error(`Duplicate step id "${explicitId}" in workflow run ${state.runId}`);
|
|
46334
|
+
state.stepCorrelationIds.add(correlationId);
|
|
46335
|
+
return correlationId;
|
|
46336
|
+
}
|
|
46337
|
+
const occurrence = state.stepOccurrences.get(actionKey) ?? 0;
|
|
46338
|
+
state.stepOccurrences.set(actionKey, occurrence + 1);
|
|
46339
|
+
const correlationId = `step:${actionKey}#${occurrence}`;
|
|
46340
|
+
state.stepCorrelationIds.add(correlationId);
|
|
46341
|
+
return correlationId;
|
|
46342
|
+
}
|
|
46343
|
+
function createSleep(state) {
|
|
46344
|
+
return function sleep(duration) {
|
|
46345
|
+
const correlationId = `sleep#${state.sleepCounter++}`;
|
|
46346
|
+
if (state.consumer.isSleepCompleted(correlationId)) return Promise.resolve();
|
|
46347
|
+
const scheduledResumeAt = state.consumer.getSleepResumeAt(correlationId);
|
|
46348
|
+
const resumeAt = scheduledResumeAt ?? resolveResumeAt(duration, state.now);
|
|
46349
|
+
if (scheduledResumeAt && resumeAt.getTime() <= state.now.getTime()) {
|
|
46350
|
+
state.newEvents.push({
|
|
46351
|
+
id: `sleep_completed:${state.runId}:${correlationId}`,
|
|
46352
|
+
runId: state.runId,
|
|
46353
|
+
type: "sleep_completed",
|
|
46354
|
+
correlationId
|
|
46355
|
+
});
|
|
46356
|
+
return Promise.resolve();
|
|
46357
|
+
}
|
|
46358
|
+
if (!scheduledResumeAt) state.newEvents.push({
|
|
46359
|
+
id: `sleep_scheduled:${state.runId}:${correlationId}`,
|
|
46360
|
+
runId: state.runId,
|
|
46361
|
+
type: "sleep_scheduled",
|
|
46362
|
+
correlationId,
|
|
46363
|
+
data: { resumeAt: resumeAt.toISOString() }
|
|
46364
|
+
});
|
|
46365
|
+
state.coordinator.request({
|
|
46366
|
+
kind: "sleep",
|
|
46367
|
+
correlationId,
|
|
46368
|
+
resumeAt
|
|
46369
|
+
});
|
|
46370
|
+
return createPendingPromise();
|
|
46371
|
+
};
|
|
46372
|
+
}
|
|
46373
|
+
function createHook(state) {
|
|
46374
|
+
return function hook(options) {
|
|
46375
|
+
const correlationId = `hook#${state.hookCounter++}`;
|
|
46376
|
+
const token = options?.token ?? state.consumer.getHookToken(correlationId) ?? `hook_${crypto.randomUUID()}`;
|
|
46377
|
+
const resumeUrl = state.hookBaseUrl ? `${state.hookBaseUrl}/hooks/${token}/resume` : `/hooks/${token}/resume`;
|
|
46378
|
+
const schema = options?.schema ? zod.z.toJSONSchema(options.schema) : void 0;
|
|
46379
|
+
return {
|
|
46380
|
+
token,
|
|
46381
|
+
resumeUrl,
|
|
46382
|
+
then(onFulfilled, onRejected) {
|
|
46383
|
+
const result = state.consumer.getHookResult(correlationId);
|
|
46384
|
+
if (result.resolved) {
|
|
46385
|
+
const payload = options?.schema ? options.schema.parse(result.payload) : result.payload;
|
|
46386
|
+
return Promise.resolve(payload).then(onFulfilled, onRejected);
|
|
46387
|
+
}
|
|
46388
|
+
if (!state.consumer.hasEventType(correlationId, "hook_created")) state.newEvents.push({
|
|
46389
|
+
id: `hook_created:${state.runId}:${correlationId}`,
|
|
46390
|
+
runId: state.runId,
|
|
46391
|
+
type: "hook_created",
|
|
46392
|
+
correlationId,
|
|
46393
|
+
data: schema ? {
|
|
46394
|
+
token,
|
|
46395
|
+
schema
|
|
46396
|
+
} : { token }
|
|
46397
|
+
});
|
|
46398
|
+
state.coordinator.request({
|
|
46399
|
+
kind: "hook",
|
|
46400
|
+
correlationId,
|
|
46401
|
+
token,
|
|
46402
|
+
...schema ? { schema } : {}
|
|
46403
|
+
});
|
|
46404
|
+
return createPendingPromise().then(onFulfilled, onRejected);
|
|
46405
|
+
}
|
|
46406
|
+
};
|
|
46407
|
+
};
|
|
46408
|
+
}
|
|
46409
|
+
/**
|
|
46410
|
+
* Shared durable-step shell: resolve a stable `correlation_id`, short-circuit
|
|
46411
|
+
* from the event log on a cache hit, otherwise execute and append
|
|
46412
|
+
* `step_completed` immediately (per-step crash durability). A thrown step
|
|
46413
|
+
* appends `step_retrying` (non-fatal, re-run on the next attempt) and
|
|
46414
|
+
* rethrows; the terminal `step_failed` is written by the job handler once the
|
|
46415
|
+
* queue exhausts retries.
|
|
46416
|
+
*/
|
|
46417
|
+
async function runDurableStep(state, options) {
|
|
46418
|
+
const { runId, consumer, eventLog } = state;
|
|
46419
|
+
require_dist$4.getRunSignal().throwIfAborted();
|
|
46420
|
+
const correlationId = nextStepCorrelationId(state, options.key, options.id);
|
|
46421
|
+
const cached = consumer.getStepResult(correlationId);
|
|
46422
|
+
const metadata = {
|
|
46423
|
+
runId,
|
|
46424
|
+
[options.metadataKey]: options.key,
|
|
46425
|
+
correlationId
|
|
46426
|
+
};
|
|
46427
|
+
if (cached.completed) {
|
|
46428
|
+
await withSpan({
|
|
46429
|
+
kind: options.kind,
|
|
46430
|
+
name: options.key,
|
|
46431
|
+
refId: `${runId}:${correlationId}`,
|
|
46432
|
+
metadata: {
|
|
46433
|
+
...metadata,
|
|
46434
|
+
replayed: true
|
|
46435
|
+
}
|
|
46436
|
+
}, async () => {
|
|
46437
|
+
await logSystem("info", options.replayMessage, metadata);
|
|
46438
|
+
});
|
|
46439
|
+
return options.parseCached ? options.parseCached(cached.result) : cached.result;
|
|
46440
|
+
}
|
|
46441
|
+
return withSpan({
|
|
46442
|
+
kind: options.kind,
|
|
46443
|
+
name: options.key,
|
|
46444
|
+
refId: `${runId}:${correlationId}`,
|
|
46445
|
+
metadata
|
|
46446
|
+
}, async () => captureConsole(async () => {
|
|
46447
|
+
try {
|
|
46448
|
+
const result = await options.execute(correlationId);
|
|
46449
|
+
await eventLog.append({
|
|
46450
|
+
id: `step_completed:${runId}:${correlationId}`,
|
|
46451
|
+
runId,
|
|
46452
|
+
type: "step_completed",
|
|
46453
|
+
correlationId,
|
|
46454
|
+
data: result
|
|
46455
|
+
});
|
|
46456
|
+
return result;
|
|
46457
|
+
} catch (error) {
|
|
46458
|
+
const attempt = consumer.countRetrying(correlationId);
|
|
46459
|
+
await eventLog.append({
|
|
46460
|
+
id: `step_retrying:${runId}:${correlationId}:${attempt}`,
|
|
46461
|
+
runId,
|
|
46462
|
+
type: "step_retrying",
|
|
46463
|
+
correlationId,
|
|
46464
|
+
data: serializeWorkflowError(error)
|
|
46465
|
+
});
|
|
46466
|
+
state.failedCorrelationId = correlationId;
|
|
46467
|
+
throw error;
|
|
46468
|
+
}
|
|
46469
|
+
}));
|
|
46470
|
+
}
|
|
46471
|
+
function withCredentialScopeOverride(requirements, scope) {
|
|
46472
|
+
if (!requirements || !scope) return requirements;
|
|
46473
|
+
return require_dist$3.normalizeCredentialList(requirements).map((requirement) => ({
|
|
46474
|
+
...requirement,
|
|
46475
|
+
scope
|
|
46476
|
+
}));
|
|
46477
|
+
}
|
|
46478
|
+
/** Builds the per-run action runner; durability lives in {@link runDurableStep}. */
|
|
46479
|
+
function createActionRunner(state, options = {}) {
|
|
46480
|
+
return (action, input, runOptions) => runDurableStep(state, {
|
|
46481
|
+
kind: "action",
|
|
46482
|
+
key: action.slug,
|
|
46483
|
+
id: runOptions?.id,
|
|
46484
|
+
metadataKey: "actionKey",
|
|
46485
|
+
replayMessage: "action replayed from checkpoint",
|
|
46486
|
+
parseCached: (cached) => action.output.parse(cached),
|
|
46487
|
+
execute: async (correlationId) => {
|
|
46488
|
+
const requirements = withCredentialScopeOverride(require_dist$4.getActionCredentialRequirements(action), runOptions?.credentialScope);
|
|
46489
|
+
const credentials = requirements?.length ? await resolveActionCredentials(requirements, {
|
|
46490
|
+
resolveCredentials: options.resolveCredentials,
|
|
46491
|
+
context: options.credentialContext,
|
|
46492
|
+
oauthAdapter: options.oauthAdapter,
|
|
46493
|
+
consumer: {
|
|
46494
|
+
kind: "action",
|
|
46495
|
+
name: action.slug,
|
|
46496
|
+
id: correlationId
|
|
46497
|
+
}
|
|
46498
|
+
}) : {};
|
|
46499
|
+
return require_dist$4.runWithMcpCredentialContext({
|
|
46500
|
+
...options.mcpCredentialContext,
|
|
46501
|
+
consumerId: correlationId
|
|
46502
|
+
}, () => require_dist$4.executeAction(action, input, credentials));
|
|
46503
|
+
}
|
|
46504
|
+
});
|
|
46505
|
+
}
|
|
46506
|
+
/**
|
|
46507
|
+
* Builds the per-run agent runner: each `agent.prompt()` in a workflow body
|
|
46508
|
+
* becomes a durable step keyed `step:<agentKey>#<occurrence>` (same scheme as
|
|
46509
|
+
* actions). The actual prompt execution is delegated to `runAgent`, supplied
|
|
46510
|
+
* by the host (server) via `executeWorkflow({ runAgent })`.
|
|
46511
|
+
*/
|
|
46512
|
+
function createAgentStepRunner(state, runAgent) {
|
|
46513
|
+
return (agent, input, options) => {
|
|
46514
|
+
const slug = agent.slug;
|
|
46515
|
+
return runDurableStep(state, {
|
|
46516
|
+
kind: "agent_session",
|
|
46517
|
+
key: slug,
|
|
46518
|
+
id: options?.id,
|
|
46519
|
+
metadataKey: "agentKey",
|
|
46520
|
+
replayMessage: "agent step replayed from checkpoint",
|
|
46521
|
+
execute: (_correlationId) => runAgent(agent, input, options?.runPrompt)
|
|
46522
|
+
});
|
|
46523
|
+
};
|
|
46524
|
+
}
|
|
46525
|
+
/**
|
|
46526
|
+
* Builds the per-run LLM runner: each `promptLlm()` in a workflow body becomes a
|
|
46527
|
+
* durable step keyed `step:promptLlm#<occurrence>`. The actual LLM call is
|
|
46528
|
+
* delegated to `runLlm`, supplied by the host (server) via `executeWorkflow({ runLlm })`.
|
|
46529
|
+
*/
|
|
46530
|
+
function createLlmStepRunner(state, runLlm) {
|
|
46531
|
+
return (opts) => runDurableStep(state, {
|
|
46532
|
+
kind: "llm",
|
|
46533
|
+
key: "promptLlm",
|
|
46534
|
+
id: opts.stepId,
|
|
46535
|
+
metadataKey: "llmKey",
|
|
46536
|
+
replayMessage: "llm step replayed from checkpoint",
|
|
46537
|
+
parseCached: (cached) => opts.outputSchema ? opts.outputSchema.parse(cached) : cached,
|
|
46538
|
+
execute: (_correlationId) => runLlm(opts)
|
|
46539
|
+
});
|
|
46540
|
+
}
|
|
46541
|
+
/**
|
|
46542
|
+
* Builds the per-run sub-workflow runner: each `workflow.run(input)` inside a
|
|
46543
|
+
* workflow body becomes a durable step keyed `step:<slug>#<occurrence>`. The
|
|
46544
|
+
* sub-workflow's body runs inline in the same replay state (its action/agent/
|
|
46545
|
+
* LLM steps and `ctx.sleep`/`ctx.hook` stay durable), and its validated output
|
|
46546
|
+
* is checkpointed so a replay skips the whole sub-workflow once it completes.
|
|
46547
|
+
*/
|
|
46548
|
+
function createWorkflowStepRunner(state, ctx) {
|
|
46549
|
+
return (workflowUnknown, input, options) => {
|
|
46550
|
+
const workflow = workflowUnknown;
|
|
46551
|
+
const parsedInput = workflow.input.parse(input);
|
|
46552
|
+
return runDurableStep(state, {
|
|
46553
|
+
kind: "workflow_run",
|
|
46554
|
+
key: workflow.slug,
|
|
46555
|
+
id: options?.id,
|
|
46556
|
+
metadataKey: "workflowKey",
|
|
46557
|
+
replayMessage: "workflow step replayed from checkpoint",
|
|
46558
|
+
parseCached: (cached) => workflow.output.parse(cached),
|
|
46559
|
+
execute: async () => {
|
|
46560
|
+
const output = await workflow.run(parsedInput, ctx);
|
|
46561
|
+
return workflow.output.parse(output);
|
|
46562
|
+
}
|
|
46563
|
+
});
|
|
46564
|
+
};
|
|
46565
|
+
}
|
|
46566
|
+
const storage = new node_async_hooks.AsyncLocalStorage();
|
|
46567
|
+
require_dist$4.registerWorkflowRunGetter(() => {
|
|
46568
|
+
const store = storage.getStore();
|
|
46569
|
+
if (!store) return;
|
|
46570
|
+
return {
|
|
46571
|
+
actionRunner: store.actionRunner,
|
|
46572
|
+
agentRunner: store.agentRunner,
|
|
46573
|
+
llmRunner: store.llmRunner,
|
|
46574
|
+
workflowRunner: store.workflowRunner
|
|
46575
|
+
};
|
|
46576
|
+
});
|
|
46577
|
+
function runWithWorkflowContext(store, fn) {
|
|
46578
|
+
return storage.run(store, fn);
|
|
46579
|
+
}
|
|
46580
|
+
/** In-memory durable log for inline execution and tests. */
|
|
46581
|
+
var MemoryEventLog = class {
|
|
46582
|
+
events = [];
|
|
46583
|
+
ids = /* @__PURE__ */ new Set();
|
|
46584
|
+
async append(event) {
|
|
46585
|
+
const id = event.id ?? crypto.randomUUID();
|
|
46586
|
+
if (this.ids.has(id)) return false;
|
|
46587
|
+
this.ids.add(id);
|
|
46588
|
+
this.events.push({
|
|
46589
|
+
id,
|
|
46590
|
+
runId: event.runId,
|
|
46591
|
+
seq: this.events.length,
|
|
46592
|
+
type: event.type,
|
|
46593
|
+
correlationId: event.correlationId ?? null,
|
|
46594
|
+
data: event.data ?? null
|
|
46595
|
+
});
|
|
46596
|
+
return true;
|
|
46597
|
+
}
|
|
46598
|
+
async listReplay(runId) {
|
|
46599
|
+
return this.events.filter((event) => event.runId === runId).map((event) => ({ ...event }));
|
|
46600
|
+
}
|
|
46601
|
+
};
|
|
46602
|
+
/**
|
|
46603
|
+
* The single way to run a workflow: replay its event log from the top, execute
|
|
46604
|
+
* un-cached primitives, and either complete/fail or suspend at the first
|
|
46605
|
+
* un-satisfied sleep/hook. New events (sleep_scheduled/sleep_completed/
|
|
46606
|
+
* hook_created/run_completed/run_failed) are flushed before returning.
|
|
46607
|
+
*
|
|
46608
|
+
* Durability comes entirely from the log — a suspended run resumes by calling
|
|
46609
|
+
* this again with the same `runId` and event log once the sleep is due or the
|
|
46610
|
+
* hook is resumed.
|
|
46611
|
+
*/
|
|
46612
|
+
async function executeWorkflow(workflow, input, options = {}) {
|
|
46613
|
+
const runId = options.runId ?? crypto.randomUUID();
|
|
46614
|
+
const eventLog = options.eventLog ?? new MemoryEventLog();
|
|
46615
|
+
const signal = require_dist$4.getRunSignal();
|
|
46616
|
+
const consumer = new EventsConsumer(await eventLog.listReplay(runId));
|
|
46617
|
+
const coordinator = new SuspensionCoordinator();
|
|
46618
|
+
const state = createReplayState({
|
|
46619
|
+
runId,
|
|
46620
|
+
consumer,
|
|
46621
|
+
coordinator,
|
|
46622
|
+
eventLog,
|
|
46623
|
+
hookBaseUrl: options.hookBaseUrl,
|
|
46624
|
+
now: options.now
|
|
46625
|
+
});
|
|
46626
|
+
const actionRunner = createActionRunner(state, {
|
|
46627
|
+
resolveCredentials: options.resolveCredentials,
|
|
46628
|
+
credentialContext: options.credentialContext,
|
|
46629
|
+
oauthAdapter: options.oauthAdapter,
|
|
46630
|
+
mcpCredentialContext: { assignmentTarget: {
|
|
46631
|
+
type: "workflow",
|
|
46632
|
+
key: workflow.slug
|
|
46633
|
+
} }
|
|
46634
|
+
});
|
|
46635
|
+
const ctx = {
|
|
46636
|
+
runId,
|
|
46637
|
+
sleep: createSleep(state),
|
|
46638
|
+
hook: createHook(state),
|
|
46639
|
+
...options.context
|
|
46640
|
+
};
|
|
46641
|
+
const agentRunner = options.runAgent ? createAgentStepRunner(state, options.runAgent) : void 0;
|
|
46642
|
+
const llmRunner = options.runLlm ? createLlmStepRunner(state, options.runLlm) : void 0;
|
|
46643
|
+
const workflowRunner = createWorkflowStepRunner(state, ctx);
|
|
46644
|
+
const validatedInput = workflow.input.parse(input);
|
|
46645
|
+
const bodyPromise = runWithWorkflowContext({
|
|
46646
|
+
actionRunner,
|
|
46647
|
+
agentRunner,
|
|
46648
|
+
llmRunner,
|
|
46649
|
+
workflowRunner,
|
|
46650
|
+
runId
|
|
46651
|
+
}, () => captureConsole(async () => workflow.run(validatedInput, ctx)));
|
|
46652
|
+
let onAbort;
|
|
46653
|
+
const waitForAbort = signal.aborted ? Promise.resolve({ type: "canceled" }) : new Promise((resolve) => {
|
|
46654
|
+
onAbort = () => resolve({ type: "canceled" });
|
|
46655
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
46656
|
+
});
|
|
46657
|
+
const outcome = await Promise.race([
|
|
46658
|
+
bodyPromise.then((output) => ({
|
|
46659
|
+
type: "done",
|
|
46660
|
+
output
|
|
46661
|
+
}), (error) => ({
|
|
46662
|
+
type: "error",
|
|
46663
|
+
error
|
|
46664
|
+
})),
|
|
46665
|
+
coordinator.waitForSuspension().then((items) => ({
|
|
46666
|
+
type: "suspended",
|
|
46667
|
+
items
|
|
46668
|
+
})),
|
|
46669
|
+
waitForAbort
|
|
46670
|
+
]);
|
|
46671
|
+
if (onAbort) signal.removeEventListener("abort", onAbort);
|
|
46672
|
+
let result;
|
|
46673
|
+
if (outcome.type === "suspended") {
|
|
46674
|
+
bodyPromise.catch(() => {});
|
|
46675
|
+
result = {
|
|
46676
|
+
status: "suspended",
|
|
46677
|
+
items: outcome.items
|
|
46678
|
+
};
|
|
46679
|
+
} else if (outcome.type === "done") {
|
|
46680
|
+
const output = workflow.output.parse(outcome.output);
|
|
46681
|
+
state.newEvents.push({
|
|
46682
|
+
id: `run_completed:${runId}`,
|
|
46683
|
+
runId,
|
|
46684
|
+
type: "run_completed",
|
|
46685
|
+
data: { output }
|
|
46686
|
+
});
|
|
46687
|
+
result = {
|
|
46688
|
+
status: "completed",
|
|
46689
|
+
output
|
|
46690
|
+
};
|
|
46691
|
+
} else if (outcome.type === "canceled" || isRunCanceledError(outcome.error)) {
|
|
46692
|
+
bodyPromise.catch(() => {});
|
|
46693
|
+
state.newEvents.push({
|
|
46694
|
+
id: `run_canceled:${runId}`,
|
|
46695
|
+
runId,
|
|
46696
|
+
type: "run_canceled"
|
|
46697
|
+
});
|
|
46698
|
+
result = { status: "canceled" };
|
|
46699
|
+
} else result = {
|
|
46700
|
+
status: "failed",
|
|
46701
|
+
error: outcome.error,
|
|
46702
|
+
failedCorrelationId: state.failedCorrelationId
|
|
46703
|
+
};
|
|
46704
|
+
for (const event of state.newEvents) await eventLog.append(event);
|
|
46705
|
+
return result;
|
|
46706
|
+
}
|
|
46707
|
+
function promptLlm(prompt, opts) {
|
|
46708
|
+
const handle = require_dist$4.getWorkflowRunHandle();
|
|
46709
|
+
if (!handle?.llmRunner) throw new Error("promptLlm must run inside a workflow with an injected llm executor (executeWorkflow({ runLlm })).");
|
|
46710
|
+
return handle.llmRunner({
|
|
46711
|
+
prompt,
|
|
46712
|
+
...opts
|
|
46713
|
+
});
|
|
46714
|
+
}
|
|
46715
|
+
let executor;
|
|
46716
|
+
function registerWorkflowToolExecutor(fn) {
|
|
46717
|
+
if (executor) throw new Error("workflow tool executor already registered");
|
|
46718
|
+
executor = fn;
|
|
46719
|
+
}
|
|
46720
|
+
function getWorkflowToolExecutor() {
|
|
46721
|
+
if (!executor) throw new Error("workflow tool executor not registered");
|
|
46722
|
+
return executor;
|
|
46723
|
+
}
|
|
46724
|
+
function assertWorkflowToolInput(workflow) {
|
|
46725
|
+
if (!("shape" in workflow.input) || typeof workflow.input.shape !== "object") throw new Error(`Workflow "${workflow.slug}" cannot be used as an agent tool: input schema must be a Zod object.`);
|
|
46726
|
+
}
|
|
46727
|
+
function resolveWorkflowTool(workflow) {
|
|
46728
|
+
assertWorkflowToolInput(workflow);
|
|
46729
|
+
const key = workflow.slug;
|
|
46730
|
+
return require_dist$3.defineTool({
|
|
46731
|
+
name: key,
|
|
46732
|
+
label: key,
|
|
46733
|
+
description: workflow.description ?? key,
|
|
46734
|
+
parameters: workflow.input,
|
|
46735
|
+
async execute(toolCallId, params) {
|
|
46736
|
+
const output = await withSpan({
|
|
46737
|
+
kind: "tool_call",
|
|
46738
|
+
name: key,
|
|
46739
|
+
refId: toolCallId,
|
|
46740
|
+
metadata: {
|
|
46741
|
+
tool: key,
|
|
46742
|
+
type: "workflow"
|
|
46743
|
+
}
|
|
46744
|
+
}, () => getWorkflowToolExecutor()(workflow, params, { workflowKey: key }));
|
|
46745
|
+
return {
|
|
46746
|
+
content: [{
|
|
46747
|
+
type: "text",
|
|
46748
|
+
text: JSON.stringify(output)
|
|
46749
|
+
}],
|
|
46750
|
+
details: output
|
|
46751
|
+
};
|
|
46752
|
+
}
|
|
46753
|
+
});
|
|
46754
|
+
}
|
|
46755
|
+
//#endregion
|
|
46111
46756
|
Object.defineProperty(exports, "MESSAGE_EVENT_TYPE", {
|
|
46112
46757
|
enumerable: true,
|
|
46113
46758
|
get: function() {
|
|
46114
46759
|
return MESSAGE_EVENT_TYPE;
|
|
46115
46760
|
}
|
|
46116
46761
|
});
|
|
46762
|
+
Object.defineProperty(exports, "MemoryEventLog", {
|
|
46763
|
+
enumerable: true,
|
|
46764
|
+
get: function() {
|
|
46765
|
+
return MemoryEventLog;
|
|
46766
|
+
}
|
|
46767
|
+
});
|
|
46768
|
+
Object.defineProperty(exports, "RunCanceledError", {
|
|
46769
|
+
enumerable: true,
|
|
46770
|
+
get: function() {
|
|
46771
|
+
return RunCanceledError;
|
|
46772
|
+
}
|
|
46773
|
+
});
|
|
46117
46774
|
Object.defineProperty(exports, "ToolLoopAgent", {
|
|
46118
46775
|
enumerable: true,
|
|
46119
46776
|
get: function() {
|
|
@@ -46204,12 +46861,30 @@ Object.defineProperty(exports, "defineMcp", {
|
|
|
46204
46861
|
return defineMcp;
|
|
46205
46862
|
}
|
|
46206
46863
|
});
|
|
46864
|
+
Object.defineProperty(exports, "defineWorkflow", {
|
|
46865
|
+
enumerable: true,
|
|
46866
|
+
get: function() {
|
|
46867
|
+
return defineWorkflow;
|
|
46868
|
+
}
|
|
46869
|
+
});
|
|
46870
|
+
Object.defineProperty(exports, "deserializeWorkflowError", {
|
|
46871
|
+
enumerable: true,
|
|
46872
|
+
get: function() {
|
|
46873
|
+
return deserializeWorkflowError;
|
|
46874
|
+
}
|
|
46875
|
+
});
|
|
46207
46876
|
Object.defineProperty(exports, "enrichCredentialError", {
|
|
46208
46877
|
enumerable: true,
|
|
46209
46878
|
get: function() {
|
|
46210
46879
|
return enrichCredentialError;
|
|
46211
46880
|
}
|
|
46212
46881
|
});
|
|
46882
|
+
Object.defineProperty(exports, "executeWorkflow", {
|
|
46883
|
+
enumerable: true,
|
|
46884
|
+
get: function() {
|
|
46885
|
+
return executeWorkflow;
|
|
46886
|
+
}
|
|
46887
|
+
});
|
|
46213
46888
|
Object.defineProperty(exports, "failAgentSession", {
|
|
46214
46889
|
enumerable: true,
|
|
46215
46890
|
get: function() {
|
|
@@ -46276,6 +46951,18 @@ Object.defineProperty(exports, "isMcp", {
|
|
|
46276
46951
|
return isMcp;
|
|
46277
46952
|
}
|
|
46278
46953
|
});
|
|
46954
|
+
Object.defineProperty(exports, "isRunCanceledError", {
|
|
46955
|
+
enumerable: true,
|
|
46956
|
+
get: function() {
|
|
46957
|
+
return isRunCanceledError;
|
|
46958
|
+
}
|
|
46959
|
+
});
|
|
46960
|
+
Object.defineProperty(exports, "isWorkflow", {
|
|
46961
|
+
enumerable: true,
|
|
46962
|
+
get: function() {
|
|
46963
|
+
return isWorkflow;
|
|
46964
|
+
}
|
|
46965
|
+
});
|
|
46279
46966
|
Object.defineProperty(exports, "jsonSchema", {
|
|
46280
46967
|
enumerable: true,
|
|
46281
46968
|
get: function() {
|
|
@@ -46288,16 +46975,16 @@ Object.defineProperty(exports, "listMessageEvents", {
|
|
|
46288
46975
|
return listMessageEvents;
|
|
46289
46976
|
}
|
|
46290
46977
|
});
|
|
46291
|
-
Object.defineProperty(exports, "
|
|
46978
|
+
Object.defineProperty(exports, "output_exports", {
|
|
46292
46979
|
enumerable: true,
|
|
46293
46980
|
get: function() {
|
|
46294
|
-
return
|
|
46981
|
+
return output_exports;
|
|
46295
46982
|
}
|
|
46296
46983
|
});
|
|
46297
|
-
Object.defineProperty(exports, "
|
|
46984
|
+
Object.defineProperty(exports, "promptLlm", {
|
|
46298
46985
|
enumerable: true,
|
|
46299
46986
|
get: function() {
|
|
46300
|
-
return
|
|
46987
|
+
return promptLlm;
|
|
46301
46988
|
}
|
|
46302
46989
|
});
|
|
46303
46990
|
Object.defineProperty(exports, "readUIMessageStream", {
|
|
@@ -46312,6 +46999,12 @@ Object.defineProperty(exports, "recordLlmUsageFromAssistantMessage", {
|
|
|
46312
46999
|
return recordLlmUsageFromAssistantMessage;
|
|
46313
47000
|
}
|
|
46314
47001
|
});
|
|
47002
|
+
Object.defineProperty(exports, "registerWorkflowToolExecutor", {
|
|
47003
|
+
enumerable: true,
|
|
47004
|
+
get: function() {
|
|
47005
|
+
return registerWorkflowToolExecutor;
|
|
47006
|
+
}
|
|
47007
|
+
});
|
|
46315
47008
|
Object.defineProperty(exports, "require_token_error", {
|
|
46316
47009
|
enumerable: true,
|
|
46317
47010
|
get: function() {
|
|
@@ -46342,6 +47035,18 @@ Object.defineProperty(exports, "resolveRunSourceFromTraceContext", {
|
|
|
46342
47035
|
return resolveRunSourceFromTraceContext;
|
|
46343
47036
|
}
|
|
46344
47037
|
});
|
|
47038
|
+
Object.defineProperty(exports, "resolveWorkflowTool", {
|
|
47039
|
+
enumerable: true,
|
|
47040
|
+
get: function() {
|
|
47041
|
+
return resolveWorkflowTool;
|
|
47042
|
+
}
|
|
47043
|
+
});
|
|
47044
|
+
Object.defineProperty(exports, "serializeWorkflowError", {
|
|
47045
|
+
enumerable: true,
|
|
47046
|
+
get: function() {
|
|
47047
|
+
return serializeWorkflowError;
|
|
47048
|
+
}
|
|
47049
|
+
});
|
|
46345
47050
|
Object.defineProperty(exports, "setSessionLiveMessage", {
|
|
46346
47051
|
enumerable: true,
|
|
46347
47052
|
get: function() {
|
|
@@ -46385,4 +47090,4 @@ Object.defineProperty(exports, "withSpan", {
|
|
|
46385
47090
|
}
|
|
46386
47091
|
});
|
|
46387
47092
|
|
|
46388
|
-
//# sourceMappingURL=dist-
|
|
47093
|
+
//# sourceMappingURL=dist-D69ocJ7u.cjs.map
|