@upstash/workflow 0.2.11 → 0.2.12

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/svelte.js CHANGED
@@ -134,7 +134,7 @@ var formatWorkflowError = (error) => {
134
134
  message: error.message
135
135
  } : {
136
136
  error: "Error",
137
- message: "An error occured while executing workflow."
137
+ message: `An error occured while executing workflow: '${typeof error === "string" ? error : JSON.stringify(error)}'`
138
138
  };
139
139
  };
140
140
 
@@ -2034,6 +2034,9 @@ var WorkflowApi = class extends BaseWorkflowApi {
2034
2034
  }
2035
2035
  };
2036
2036
 
2037
+ // src/agents/index.ts
2038
+ var import_openai3 = require("@ai-sdk/openai");
2039
+
2037
2040
  // src/agents/adapters.ts
2038
2041
  var import_openai2 = require("@ai-sdk/openai");
2039
2042
  var import_ai = require("ai");
@@ -2053,46 +2056,49 @@ you need from that agent.
2053
2056
  `;
2054
2057
 
2055
2058
  // src/agents/adapters.ts
2056
- var createWorkflowOpenAI = (context, config) => {
2057
- const { baseURL, apiKey } = config ?? {};
2058
- return (0, import_openai2.createOpenAI)({
2059
- baseURL,
2060
- apiKey,
2061
- compatibility: "strict",
2062
- fetch: async (input, init) => {
2063
- try {
2064
- const headers = init?.headers ? Object.fromEntries(new Headers(init.headers).entries()) : {};
2065
- const body = init?.body ? JSON.parse(init.body) : void 0;
2066
- const agentName = headers[AGENT_NAME_HEADER];
2067
- const stepName = agentName ? `Call Agent ${agentName}` : "Call Agent";
2068
- const responseInfo = await context.call(stepName, {
2069
- url: input.toString(),
2070
- method: init?.method,
2071
- headers,
2072
- body
2073
- });
2074
- const responseHeaders = new Headers(
2075
- Object.entries(responseInfo.header).reduce(
2076
- (acc, [key, values]) => {
2077
- acc[key] = values.join(", ");
2078
- return acc;
2079
- },
2080
- {}
2081
- )
2082
- );
2083
- return new Response(JSON.stringify(responseInfo.body), {
2084
- status: responseInfo.status,
2085
- headers: responseHeaders
2086
- });
2087
- } catch (error) {
2088
- if (error instanceof Error && error.name === "WorkflowAbort") {
2089
- throw error;
2090
- } else {
2091
- console.error("Error in fetch implementation:", error);
2092
- throw error;
2093
- }
2094
- }
2059
+ var fetchWithContextCall = async (context, ...params) => {
2060
+ const [input, init] = params;
2061
+ try {
2062
+ const headers = init?.headers ? Object.fromEntries(new Headers(init.headers).entries()) : {};
2063
+ const body = init?.body ? JSON.parse(init.body) : void 0;
2064
+ const agentName = headers[AGENT_NAME_HEADER];
2065
+ const stepName = agentName ? `Call Agent ${agentName}` : "Call Agent";
2066
+ const responseInfo = await context.call(stepName, {
2067
+ url: input.toString(),
2068
+ method: init?.method,
2069
+ headers,
2070
+ body
2071
+ });
2072
+ const responseHeaders = new Headers(
2073
+ Object.entries(responseInfo.header).reduce(
2074
+ (acc, [key, values]) => {
2075
+ acc[key] = values.join(", ");
2076
+ return acc;
2077
+ },
2078
+ {}
2079
+ )
2080
+ );
2081
+ return new Response(JSON.stringify(responseInfo.body), {
2082
+ status: responseInfo.status,
2083
+ headers: responseHeaders
2084
+ });
2085
+ } catch (error) {
2086
+ if (error instanceof Error && error.name === "WorkflowAbort") {
2087
+ throw error;
2088
+ } else {
2089
+ console.error("Error in fetch implementation:", error);
2090
+ throw error;
2095
2091
  }
2092
+ }
2093
+ };
2094
+ var createWorkflowModel = ({
2095
+ context,
2096
+ provider,
2097
+ providerParams
2098
+ }) => {
2099
+ return provider({
2100
+ fetch: (...params) => fetchWithContextCall(context, ...params),
2101
+ ...providerParams
2096
2102
  });
2097
2103
  };
2098
2104
  var wrapTools = ({
@@ -2332,9 +2338,14 @@ var WorkflowAgents = class {
2332
2338
  openai(...params) {
2333
2339
  const [model, settings] = params;
2334
2340
  const { baseURL, apiKey, ...otherSettings } = settings ?? {};
2335
- const openai2 = createWorkflowOpenAI(this.context, { baseURL, apiKey });
2336
- return openai2(model, otherSettings);
2341
+ const openaiModel = this.AISDKModel({
2342
+ context: this.context,
2343
+ provider: import_openai3.createOpenAI,
2344
+ providerParams: { baseURL, apiKey, compatibility: "strict" }
2345
+ });
2346
+ return openaiModel(model, otherSettings);
2337
2347
  }
2348
+ AISDKModel = createWorkflowModel;
2338
2349
  };
2339
2350
 
2340
2351
  // src/context/context.ts
@@ -3057,6 +3068,7 @@ var processOptions = (options) => {
3057
3068
  retries: DEFAULT_RETRIES,
3058
3069
  useJSONContent: false,
3059
3070
  disableTelemetry: false,
3071
+ onError: console.error,
3060
3072
  ...options
3061
3073
  };
3062
3074
  };
@@ -3106,7 +3118,8 @@ var serveBase = (routeFunction, telemetry2, options) => {
3106
3118
  retries,
3107
3119
  useJSONContent,
3108
3120
  disableTelemetry,
3109
- flowControl
3121
+ flowControl,
3122
+ onError
3110
3123
  } = processOptions(options);
3111
3124
  telemetry2 = disableTelemetry ? void 0 : telemetry2;
3112
3125
  const debug = WorkflowLogger.getLogger(verbose);
@@ -3235,8 +3248,19 @@ var serveBase = (routeFunction, telemetry2, options) => {
3235
3248
  try {
3236
3249
  return await handler(request);
3237
3250
  } catch (error) {
3238
- console.error(error);
3239
- return new Response(JSON.stringify(formatWorkflowError(error)), {
3251
+ const formattedError = formatWorkflowError(error);
3252
+ try {
3253
+ onError?.(error);
3254
+ } catch (onErrorError) {
3255
+ const formattedOnErrorError = formatWorkflowError(onErrorError);
3256
+ const errorMessage = `Error while running onError callback: '${formattedOnErrorError.message}'.
3257
+ Original error: '${formattedError.message}'`;
3258
+ console.error(errorMessage);
3259
+ return new Response(errorMessage, {
3260
+ status: 500
3261
+ });
3262
+ }
3263
+ return new Response(JSON.stringify(formattedError), {
3240
3264
  status: 500
3241
3265
  });
3242
3266
  }
package/svelte.mjs CHANGED
@@ -2,7 +2,7 @@ import {
2
2
  SDK_TELEMETRY,
3
3
  serveBase,
4
4
  serveManyBase
5
- } from "./chunk-WQAJ2RSZ.mjs";
5
+ } from "./chunk-4GTHIL7S.mjs";
6
6
 
7
7
  // platforms/svelte.ts
8
8
  var telemetry = {
@@ -405,6 +405,47 @@ declare class WorkflowApi extends BaseWorkflowApi {
405
405
  get anthropic(): AnthropicAPI;
406
406
  }
407
407
 
408
+ /**
409
+ * An Agent which utilizes the model and tools available to it
410
+ * to achieve a given task
411
+ *
412
+ * @param name Name of the agent
413
+ * @param background Background of the agent
414
+ * @param model LLM model to use
415
+ * @param tools tools available to the agent
416
+ * @param maxSteps number of times the agent can call the LLM at most. If
417
+ * the agent abruptly stops execution after calling tools, you may need
418
+ * to increase maxSteps
419
+ * @param temparature temparature used when calling the LLM
420
+ */
421
+ declare class Agent {
422
+ readonly name: AgentParameters["name"];
423
+ readonly tools: AgentParameters["tools"];
424
+ readonly maxSteps: AgentParameters["maxSteps"];
425
+ readonly background: AgentParameters["background"];
426
+ readonly model: AgentParameters["model"];
427
+ readonly temparature: AgentParameters["temparature"];
428
+ private readonly context;
429
+ constructor({ tools, maxSteps, background, name, model, temparature }: AgentParameters, context: WorkflowContext);
430
+ /**
431
+ * Trigger the agent by passing a prompt
432
+ *
433
+ * @param prompt task to assign to the agent
434
+ * @returns Response as `{ text: string }`
435
+ */
436
+ call({ prompt }: {
437
+ prompt: string;
438
+ }): Promise<{
439
+ text: string;
440
+ }>;
441
+ /**
442
+ * Convert the agent to a tool which can be used by other agents.
443
+ *
444
+ * @returns the agent as a tool
445
+ */
446
+ asTool(): AISDKTool;
447
+ }
448
+
408
449
  /**
409
450
  * creates an AI SDK openai client with a custom
410
451
  * fetch implementation which uses context.call.
@@ -547,47 +588,9 @@ type CustomModelSettings = ModelParams["1"] & {
547
588
  apiKey?: string;
548
589
  };
549
590
  type CustomModelParams = [ModelParams[0], CustomModelSettings?];
550
-
551
- /**
552
- * An Agent which utilizes the model and tools available to it
553
- * to achieve a given task
554
- *
555
- * @param name Name of the agent
556
- * @param background Background of the agent
557
- * @param model LLM model to use
558
- * @param tools tools available to the agent
559
- * @param maxSteps number of times the agent can call the LLM at most. If
560
- * the agent abruptly stops execution after calling tools, you may need
561
- * to increase maxSteps
562
- * @param temparature temparature used when calling the LLM
563
- */
564
- declare class Agent {
565
- readonly name: AgentParameters["name"];
566
- readonly tools: AgentParameters["tools"];
567
- readonly maxSteps: AgentParameters["maxSteps"];
568
- readonly background: AgentParameters["background"];
569
- readonly model: AgentParameters["model"];
570
- readonly temparature: AgentParameters["temparature"];
571
- private readonly context;
572
- constructor({ tools, maxSteps, background, name, model, temparature }: AgentParameters, context: WorkflowContext);
573
- /**
574
- * Trigger the agent by passing a prompt
575
- *
576
- * @param prompt task to assign to the agent
577
- * @returns Response as `{ text: string }`
578
- */
579
- call({ prompt }: {
580
- prompt: string;
581
- }): Promise<{
582
- text: string;
583
- }>;
584
- /**
585
- * Convert the agent to a tool which can be used by other agents.
586
- *
587
- * @returns the agent as a tool
588
- */
589
- asTool(): AISDKTool;
590
- }
591
+ type ProviderFunction = (params: {
592
+ fetch: typeof fetch;
593
+ }) => any;
591
594
 
592
595
  /**
593
596
  * An Agent Task
@@ -700,6 +703,11 @@ declare class WorkflowAgents {
700
703
  * creates an openai model for agents
701
704
  */
702
705
  openai(...params: CustomModelParams): ai.LanguageModelV1;
706
+ AISDKModel: <TProvider extends ProviderFunction>({ context, provider, providerParams, }: {
707
+ context: WorkflowContext;
708
+ provider: TProvider;
709
+ providerParams?: Omit<Required<Parameters<TProvider>>[0], "fetch">;
710
+ }) => ReturnType<TProvider>;
703
711
  }
704
712
 
705
713
  /**
@@ -1150,6 +1158,13 @@ type WorkflowServeOptions<TResponse extends Response = Response, TInitialPayload
1150
1158
  * Url to call if QStash retries are exhausted while executing the workflow
1151
1159
  */
1152
1160
  failureUrl?: string;
1161
+ /**
1162
+ * Error handler called when an error occurs in the workflow. This is
1163
+ * different from `failureFunction` in that it is called when an error
1164
+ * occurs in the workflow, while `failureFunction` is called when QStash
1165
+ * retries are exhausted.
1166
+ */
1167
+ onError?: (error: Error) => void;
1153
1168
  /**
1154
1169
  * Failure function called when QStash retries are exhausted while executing
1155
1170
  * the workflow. Will overwrite `failureUrl` parameter with the workflow
@@ -405,6 +405,47 @@ declare class WorkflowApi extends BaseWorkflowApi {
405
405
  get anthropic(): AnthropicAPI;
406
406
  }
407
407
 
408
+ /**
409
+ * An Agent which utilizes the model and tools available to it
410
+ * to achieve a given task
411
+ *
412
+ * @param name Name of the agent
413
+ * @param background Background of the agent
414
+ * @param model LLM model to use
415
+ * @param tools tools available to the agent
416
+ * @param maxSteps number of times the agent can call the LLM at most. If
417
+ * the agent abruptly stops execution after calling tools, you may need
418
+ * to increase maxSteps
419
+ * @param temparature temparature used when calling the LLM
420
+ */
421
+ declare class Agent {
422
+ readonly name: AgentParameters["name"];
423
+ readonly tools: AgentParameters["tools"];
424
+ readonly maxSteps: AgentParameters["maxSteps"];
425
+ readonly background: AgentParameters["background"];
426
+ readonly model: AgentParameters["model"];
427
+ readonly temparature: AgentParameters["temparature"];
428
+ private readonly context;
429
+ constructor({ tools, maxSteps, background, name, model, temparature }: AgentParameters, context: WorkflowContext);
430
+ /**
431
+ * Trigger the agent by passing a prompt
432
+ *
433
+ * @param prompt task to assign to the agent
434
+ * @returns Response as `{ text: string }`
435
+ */
436
+ call({ prompt }: {
437
+ prompt: string;
438
+ }): Promise<{
439
+ text: string;
440
+ }>;
441
+ /**
442
+ * Convert the agent to a tool which can be used by other agents.
443
+ *
444
+ * @returns the agent as a tool
445
+ */
446
+ asTool(): AISDKTool;
447
+ }
448
+
408
449
  /**
409
450
  * creates an AI SDK openai client with a custom
410
451
  * fetch implementation which uses context.call.
@@ -547,47 +588,9 @@ type CustomModelSettings = ModelParams["1"] & {
547
588
  apiKey?: string;
548
589
  };
549
590
  type CustomModelParams = [ModelParams[0], CustomModelSettings?];
550
-
551
- /**
552
- * An Agent which utilizes the model and tools available to it
553
- * to achieve a given task
554
- *
555
- * @param name Name of the agent
556
- * @param background Background of the agent
557
- * @param model LLM model to use
558
- * @param tools tools available to the agent
559
- * @param maxSteps number of times the agent can call the LLM at most. If
560
- * the agent abruptly stops execution after calling tools, you may need
561
- * to increase maxSteps
562
- * @param temparature temparature used when calling the LLM
563
- */
564
- declare class Agent {
565
- readonly name: AgentParameters["name"];
566
- readonly tools: AgentParameters["tools"];
567
- readonly maxSteps: AgentParameters["maxSteps"];
568
- readonly background: AgentParameters["background"];
569
- readonly model: AgentParameters["model"];
570
- readonly temparature: AgentParameters["temparature"];
571
- private readonly context;
572
- constructor({ tools, maxSteps, background, name, model, temparature }: AgentParameters, context: WorkflowContext);
573
- /**
574
- * Trigger the agent by passing a prompt
575
- *
576
- * @param prompt task to assign to the agent
577
- * @returns Response as `{ text: string }`
578
- */
579
- call({ prompt }: {
580
- prompt: string;
581
- }): Promise<{
582
- text: string;
583
- }>;
584
- /**
585
- * Convert the agent to a tool which can be used by other agents.
586
- *
587
- * @returns the agent as a tool
588
- */
589
- asTool(): AISDKTool;
590
- }
591
+ type ProviderFunction = (params: {
592
+ fetch: typeof fetch;
593
+ }) => any;
591
594
 
592
595
  /**
593
596
  * An Agent Task
@@ -700,6 +703,11 @@ declare class WorkflowAgents {
700
703
  * creates an openai model for agents
701
704
  */
702
705
  openai(...params: CustomModelParams): ai.LanguageModelV1;
706
+ AISDKModel: <TProvider extends ProviderFunction>({ context, provider, providerParams, }: {
707
+ context: WorkflowContext;
708
+ provider: TProvider;
709
+ providerParams?: Omit<Required<Parameters<TProvider>>[0], "fetch">;
710
+ }) => ReturnType<TProvider>;
703
711
  }
704
712
 
705
713
  /**
@@ -1150,6 +1158,13 @@ type WorkflowServeOptions<TResponse extends Response = Response, TInitialPayload
1150
1158
  * Url to call if QStash retries are exhausted while executing the workflow
1151
1159
  */
1152
1160
  failureUrl?: string;
1161
+ /**
1162
+ * Error handler called when an error occurs in the workflow. This is
1163
+ * different from `failureFunction` in that it is called when an error
1164
+ * occurs in the workflow, while `failureFunction` is called when QStash
1165
+ * retries are exhausted.
1166
+ */
1167
+ onError?: (error: Error) => void;
1153
1168
  /**
1154
1169
  * Failure function called when QStash retries are exhausted while executing
1155
1170
  * the workflow. Will overwrite `failureUrl` parameter with the workflow