@upstash/workflow 0.2.11 → 0.2.13

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.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-XVNSBBDC.mjs";
6
6
 
7
7
  // platforms/svelte.ts
8
8
  var telemetry = {
@@ -2,8 +2,47 @@ import { PublishRequest, FlowControl, Client, Receiver, HTTPMethods as HTTPMetho
2
2
  import { ZodType, z } from 'zod';
3
3
  import * as ai from 'ai';
4
4
  import { CoreTool, generateText } from 'ai';
5
- import * as _ai_sdk_openai from '@ai-sdk/openai';
5
+ import { createOpenAI } from '@ai-sdk/openai';
6
6
 
7
+ declare const LOG_LEVELS: readonly ["DEBUG", "INFO", "SUBMIT", "WARN", "ERROR"];
8
+ type LogLevel = (typeof LOG_LEVELS)[number];
9
+ type ChatLogEntry = {
10
+ timestamp: number;
11
+ workflowRunId: string;
12
+ logLevel: LogLevel;
13
+ eventType: "ENDPOINT_START" | "SUBMIT_THIRD_PARTY_RESULT" | "CREATE_CONTEXT" | "SUBMIT_FIRST_INVOCATION" | "RUN_SINGLE" | "RUN_PARALLEL" | "SUBMIT_STEP" | "SUBMIT_CLEANUP" | "RESPONSE_WORKFLOW" | "RESPONSE_DEFAULT" | "ERROR";
14
+ details: unknown;
15
+ };
16
+ type WorkflowLoggerOptions = {
17
+ logLevel: LogLevel;
18
+ logOutput: "console";
19
+ };
20
+ declare class WorkflowLogger {
21
+ private logs;
22
+ private options;
23
+ private workflowRunId?;
24
+ constructor(options: WorkflowLoggerOptions);
25
+ log(level: LogLevel, eventType: ChatLogEntry["eventType"], details?: unknown): Promise<void>;
26
+ setWorkflowRunId(workflowRunId: string): void;
27
+ private writeToConsole;
28
+ private shouldLog;
29
+ static getLogger(verbose?: boolean | WorkflowLogger): WorkflowLogger | undefined;
30
+ }
31
+
32
+ type HeadersResponse = {
33
+ headers: Record<string, string>;
34
+ contentType: string;
35
+ };
36
+
37
+ type StepParams = {
38
+ context: WorkflowContext;
39
+ } & Pick<HeaderParams, "telemetry"> & Required<Pick<HeaderParams, "step" | "invokeCount">>;
40
+ type GetHeaderParams = StepParams;
41
+ type GetBodyParams = StepParams & Omit<HeadersResponse, "contentType">;
42
+ type SubmitStepParams = StepParams & Pick<HeadersResponse, "headers"> & {
43
+ body: string;
44
+ isParallel: boolean;
45
+ };
7
46
  /**
8
47
  * Base class outlining steps. Basically, each step kind (run/sleep/sleepUntil)
9
48
  * should have two methods: getPlanStep & getResultStep.
@@ -44,31 +83,11 @@ declare abstract class BaseLazyStep<TResult = unknown> {
44
83
  parseOut(out: unknown): TResult;
45
84
  protected safeParseOut(out: string): TResult;
46
85
  protected static tryParsing(stepOut: unknown): any;
47
- }
48
-
49
- declare const LOG_LEVELS: readonly ["DEBUG", "INFO", "SUBMIT", "WARN", "ERROR"];
50
- type LogLevel = (typeof LOG_LEVELS)[number];
51
- type ChatLogEntry = {
52
- timestamp: number;
53
- workflowRunId: string;
54
- logLevel: LogLevel;
55
- eventType: "ENDPOINT_START" | "SUBMIT_THIRD_PARTY_RESULT" | "CREATE_CONTEXT" | "SUBMIT_FIRST_INVOCATION" | "RUN_SINGLE" | "RUN_PARALLEL" | "SUBMIT_STEP" | "SUBMIT_CLEANUP" | "RESPONSE_WORKFLOW" | "RESPONSE_DEFAULT" | "ERROR";
56
- details: unknown;
57
- };
58
- type WorkflowLoggerOptions = {
59
- logLevel: LogLevel;
60
- logOutput: "console";
61
- };
62
- declare class WorkflowLogger {
63
- private logs;
64
- private options;
65
- private workflowRunId?;
66
- constructor(options: WorkflowLoggerOptions);
67
- log(level: LogLevel, eventType: ChatLogEntry["eventType"], details?: unknown): Promise<void>;
68
- setWorkflowRunId(workflowRunId: string): void;
69
- private writeToConsole;
70
- private shouldLog;
71
- static getLogger(verbose?: boolean | WorkflowLogger): WorkflowLogger | undefined;
86
+ getBody({ step }: GetBodyParams): string;
87
+ getHeaders({ context, telemetry, invokeCount, step }: GetHeaderParams): HeadersResponse;
88
+ submitStep({ context, body, headers }: SubmitStepParams): Promise<{
89
+ messageId: string;
90
+ }[]>;
72
91
  }
73
92
 
74
93
  declare class AutoExecutor {
@@ -156,12 +175,6 @@ declare class AutoExecutor {
156
175
  * @returns parallel call state
157
176
  */
158
177
  protected getParallelCallState(parallelStepCount: number, initialStepCount: number): ParallelCallState;
159
- /**
160
- * sends the steps to QStash as batch
161
- *
162
- * @param steps steps to send
163
- */
164
- private submitStepsToQStash;
165
178
  /**
166
179
  * Get the promise by executing the lazt steps list. If there is a single
167
180
  * step, we call `runSingle`. Otherwise `runParallel` is called.
@@ -406,16 +419,46 @@ declare class WorkflowApi extends BaseWorkflowApi {
406
419
  }
407
420
 
408
421
  /**
409
- * creates an AI SDK openai client with a custom
410
- * fetch implementation which uses context.call.
422
+ * An Agent which utilizes the model and tools available to it
423
+ * to achieve a given task
411
424
  *
412
- * @param context workflow context
413
- * @returns ai sdk openai
425
+ * @param name Name of the agent
426
+ * @param background Background of the agent
427
+ * @param model LLM model to use
428
+ * @param tools tools available to the agent
429
+ * @param maxSteps number of times the agent can call the LLM at most. If
430
+ * the agent abruptly stops execution after calling tools, you may need
431
+ * to increase maxSteps
432
+ * @param temparature temparature used when calling the LLM
414
433
  */
415
- declare const createWorkflowOpenAI: (context: WorkflowContext, config?: {
416
- baseURL?: string;
417
- apiKey?: string;
418
- }) => _ai_sdk_openai.OpenAIProvider;
434
+ declare class Agent {
435
+ readonly name: AgentParameters["name"];
436
+ readonly tools: AgentParameters["tools"];
437
+ readonly maxSteps: AgentParameters["maxSteps"];
438
+ readonly background: AgentParameters["background"];
439
+ readonly model: AgentParameters["model"];
440
+ readonly temparature: AgentParameters["temparature"];
441
+ private readonly context;
442
+ constructor({ tools, maxSteps, background, name, model, temparature }: AgentParameters, context: WorkflowContext);
443
+ /**
444
+ * Trigger the agent by passing a prompt
445
+ *
446
+ * @param prompt task to assign to the agent
447
+ * @returns Response as `{ text: string }`
448
+ */
449
+ call({ prompt }: {
450
+ prompt: string;
451
+ }): Promise<{
452
+ text: string;
453
+ }>;
454
+ /**
455
+ * Convert the agent to a tool which can be used by other agents.
456
+ *
457
+ * @returns the agent as a tool
458
+ */
459
+ asTool(): AISDKTool;
460
+ }
461
+
419
462
  declare class WorkflowTool<TSchema extends ZodType = ZodType> implements LangchainTool {
420
463
  /**
421
464
  * description of the tool
@@ -541,53 +584,18 @@ type MultiAgentTaskParams = TaskParams & {
541
584
  */
542
585
  background?: string;
543
586
  };
544
- type ModelParams = Parameters<ReturnType<typeof createWorkflowOpenAI>>;
587
+ type ModelParams = Parameters<ReturnType<typeof createOpenAI>>;
588
+ type AgentCallParams = Pick<CallSettings, "flowControl" | "retries" | "timeout">;
545
589
  type CustomModelSettings = ModelParams["1"] & {
546
590
  baseURL?: string;
547
591
  apiKey?: string;
592
+ } & {
593
+ callSettings: AgentCallParams;
548
594
  };
549
595
  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
- }
596
+ type ProviderFunction = (params: {
597
+ fetch: typeof fetch;
598
+ }) => any;
591
599
 
592
600
  /**
593
601
  * An Agent Task
@@ -700,6 +708,14 @@ declare class WorkflowAgents {
700
708
  * creates an openai model for agents
701
709
  */
702
710
  openai(...params: CustomModelParams): ai.LanguageModelV1;
711
+ AISDKModel: <TProvider extends ProviderFunction>({ context, provider, providerParams, agentCallParams, }: {
712
+ context: WorkflowContext;
713
+ provider: TProvider;
714
+ providerParams?: Omit<Required<Parameters<TProvider>>[0], "fetch">;
715
+ agentCallParams? /**
716
+ * creates an openai model for agents
717
+ */: AgentCallParams;
718
+ }) => ReturnType<TProvider>;
703
719
  }
704
720
 
705
721
  /**
@@ -934,6 +950,11 @@ declare class WorkflowContext<TInitialPayload = unknown> {
934
950
  * }
935
951
  */
936
952
  call<TResult = unknown, TBody = unknown>(stepName: string, settings: CallSettings<TBody>): Promise<CallResponse<TResult>>;
953
+ call<TResult extends {
954
+ workflowRunId: string;
955
+ } = {
956
+ workflowRunId: string;
957
+ }, TBody = unknown>(stepName: string, settings: LazyInvokeStepParams<TBody, unknown> & Pick<CallSettings, "timeout">): Promise<CallResponse<TResult>>;
937
958
  /**
938
959
  * Pauses workflow execution until a specific event occurs or a timeout is reached.
939
960
  *
@@ -1150,6 +1171,13 @@ type WorkflowServeOptions<TResponse extends Response = Response, TInitialPayload
1150
1171
  * Url to call if QStash retries are exhausted while executing the workflow
1151
1172
  */
1152
1173
  failureUrl?: string;
1174
+ /**
1175
+ * Error handler called when an error occurs in the workflow. This is
1176
+ * different from `failureFunction` in that it is called when an error
1177
+ * occurs in the workflow, while `failureFunction` is called when QStash
1178
+ * retries are exhausted.
1179
+ */
1180
+ onError?: (error: Error) => void;
1153
1181
  /**
1154
1182
  * Failure function called when QStash retries are exhausted while executing
1155
1183
  * the workflow. Will overwrite `failureUrl` parameter with the workflow
@@ -2,8 +2,47 @@ import { PublishRequest, FlowControl, Client, Receiver, HTTPMethods as HTTPMetho
2
2
  import { ZodType, z } from 'zod';
3
3
  import * as ai from 'ai';
4
4
  import { CoreTool, generateText } from 'ai';
5
- import * as _ai_sdk_openai from '@ai-sdk/openai';
5
+ import { createOpenAI } from '@ai-sdk/openai';
6
6
 
7
+ declare const LOG_LEVELS: readonly ["DEBUG", "INFO", "SUBMIT", "WARN", "ERROR"];
8
+ type LogLevel = (typeof LOG_LEVELS)[number];
9
+ type ChatLogEntry = {
10
+ timestamp: number;
11
+ workflowRunId: string;
12
+ logLevel: LogLevel;
13
+ eventType: "ENDPOINT_START" | "SUBMIT_THIRD_PARTY_RESULT" | "CREATE_CONTEXT" | "SUBMIT_FIRST_INVOCATION" | "RUN_SINGLE" | "RUN_PARALLEL" | "SUBMIT_STEP" | "SUBMIT_CLEANUP" | "RESPONSE_WORKFLOW" | "RESPONSE_DEFAULT" | "ERROR";
14
+ details: unknown;
15
+ };
16
+ type WorkflowLoggerOptions = {
17
+ logLevel: LogLevel;
18
+ logOutput: "console";
19
+ };
20
+ declare class WorkflowLogger {
21
+ private logs;
22
+ private options;
23
+ private workflowRunId?;
24
+ constructor(options: WorkflowLoggerOptions);
25
+ log(level: LogLevel, eventType: ChatLogEntry["eventType"], details?: unknown): Promise<void>;
26
+ setWorkflowRunId(workflowRunId: string): void;
27
+ private writeToConsole;
28
+ private shouldLog;
29
+ static getLogger(verbose?: boolean | WorkflowLogger): WorkflowLogger | undefined;
30
+ }
31
+
32
+ type HeadersResponse = {
33
+ headers: Record<string, string>;
34
+ contentType: string;
35
+ };
36
+
37
+ type StepParams = {
38
+ context: WorkflowContext;
39
+ } & Pick<HeaderParams, "telemetry"> & Required<Pick<HeaderParams, "step" | "invokeCount">>;
40
+ type GetHeaderParams = StepParams;
41
+ type GetBodyParams = StepParams & Omit<HeadersResponse, "contentType">;
42
+ type SubmitStepParams = StepParams & Pick<HeadersResponse, "headers"> & {
43
+ body: string;
44
+ isParallel: boolean;
45
+ };
7
46
  /**
8
47
  * Base class outlining steps. Basically, each step kind (run/sleep/sleepUntil)
9
48
  * should have two methods: getPlanStep & getResultStep.
@@ -44,31 +83,11 @@ declare abstract class BaseLazyStep<TResult = unknown> {
44
83
  parseOut(out: unknown): TResult;
45
84
  protected safeParseOut(out: string): TResult;
46
85
  protected static tryParsing(stepOut: unknown): any;
47
- }
48
-
49
- declare const LOG_LEVELS: readonly ["DEBUG", "INFO", "SUBMIT", "WARN", "ERROR"];
50
- type LogLevel = (typeof LOG_LEVELS)[number];
51
- type ChatLogEntry = {
52
- timestamp: number;
53
- workflowRunId: string;
54
- logLevel: LogLevel;
55
- eventType: "ENDPOINT_START" | "SUBMIT_THIRD_PARTY_RESULT" | "CREATE_CONTEXT" | "SUBMIT_FIRST_INVOCATION" | "RUN_SINGLE" | "RUN_PARALLEL" | "SUBMIT_STEP" | "SUBMIT_CLEANUP" | "RESPONSE_WORKFLOW" | "RESPONSE_DEFAULT" | "ERROR";
56
- details: unknown;
57
- };
58
- type WorkflowLoggerOptions = {
59
- logLevel: LogLevel;
60
- logOutput: "console";
61
- };
62
- declare class WorkflowLogger {
63
- private logs;
64
- private options;
65
- private workflowRunId?;
66
- constructor(options: WorkflowLoggerOptions);
67
- log(level: LogLevel, eventType: ChatLogEntry["eventType"], details?: unknown): Promise<void>;
68
- setWorkflowRunId(workflowRunId: string): void;
69
- private writeToConsole;
70
- private shouldLog;
71
- static getLogger(verbose?: boolean | WorkflowLogger): WorkflowLogger | undefined;
86
+ getBody({ step }: GetBodyParams): string;
87
+ getHeaders({ context, telemetry, invokeCount, step }: GetHeaderParams): HeadersResponse;
88
+ submitStep({ context, body, headers }: SubmitStepParams): Promise<{
89
+ messageId: string;
90
+ }[]>;
72
91
  }
73
92
 
74
93
  declare class AutoExecutor {
@@ -156,12 +175,6 @@ declare class AutoExecutor {
156
175
  * @returns parallel call state
157
176
  */
158
177
  protected getParallelCallState(parallelStepCount: number, initialStepCount: number): ParallelCallState;
159
- /**
160
- * sends the steps to QStash as batch
161
- *
162
- * @param steps steps to send
163
- */
164
- private submitStepsToQStash;
165
178
  /**
166
179
  * Get the promise by executing the lazt steps list. If there is a single
167
180
  * step, we call `runSingle`. Otherwise `runParallel` is called.
@@ -406,16 +419,46 @@ declare class WorkflowApi extends BaseWorkflowApi {
406
419
  }
407
420
 
408
421
  /**
409
- * creates an AI SDK openai client with a custom
410
- * fetch implementation which uses context.call.
422
+ * An Agent which utilizes the model and tools available to it
423
+ * to achieve a given task
411
424
  *
412
- * @param context workflow context
413
- * @returns ai sdk openai
425
+ * @param name Name of the agent
426
+ * @param background Background of the agent
427
+ * @param model LLM model to use
428
+ * @param tools tools available to the agent
429
+ * @param maxSteps number of times the agent can call the LLM at most. If
430
+ * the agent abruptly stops execution after calling tools, you may need
431
+ * to increase maxSteps
432
+ * @param temparature temparature used when calling the LLM
414
433
  */
415
- declare const createWorkflowOpenAI: (context: WorkflowContext, config?: {
416
- baseURL?: string;
417
- apiKey?: string;
418
- }) => _ai_sdk_openai.OpenAIProvider;
434
+ declare class Agent {
435
+ readonly name: AgentParameters["name"];
436
+ readonly tools: AgentParameters["tools"];
437
+ readonly maxSteps: AgentParameters["maxSteps"];
438
+ readonly background: AgentParameters["background"];
439
+ readonly model: AgentParameters["model"];
440
+ readonly temparature: AgentParameters["temparature"];
441
+ private readonly context;
442
+ constructor({ tools, maxSteps, background, name, model, temparature }: AgentParameters, context: WorkflowContext);
443
+ /**
444
+ * Trigger the agent by passing a prompt
445
+ *
446
+ * @param prompt task to assign to the agent
447
+ * @returns Response as `{ text: string }`
448
+ */
449
+ call({ prompt }: {
450
+ prompt: string;
451
+ }): Promise<{
452
+ text: string;
453
+ }>;
454
+ /**
455
+ * Convert the agent to a tool which can be used by other agents.
456
+ *
457
+ * @returns the agent as a tool
458
+ */
459
+ asTool(): AISDKTool;
460
+ }
461
+
419
462
  declare class WorkflowTool<TSchema extends ZodType = ZodType> implements LangchainTool {
420
463
  /**
421
464
  * description of the tool
@@ -541,53 +584,18 @@ type MultiAgentTaskParams = TaskParams & {
541
584
  */
542
585
  background?: string;
543
586
  };
544
- type ModelParams = Parameters<ReturnType<typeof createWorkflowOpenAI>>;
587
+ type ModelParams = Parameters<ReturnType<typeof createOpenAI>>;
588
+ type AgentCallParams = Pick<CallSettings, "flowControl" | "retries" | "timeout">;
545
589
  type CustomModelSettings = ModelParams["1"] & {
546
590
  baseURL?: string;
547
591
  apiKey?: string;
592
+ } & {
593
+ callSettings: AgentCallParams;
548
594
  };
549
595
  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
- }
596
+ type ProviderFunction = (params: {
597
+ fetch: typeof fetch;
598
+ }) => any;
591
599
 
592
600
  /**
593
601
  * An Agent Task
@@ -700,6 +708,14 @@ declare class WorkflowAgents {
700
708
  * creates an openai model for agents
701
709
  */
702
710
  openai(...params: CustomModelParams): ai.LanguageModelV1;
711
+ AISDKModel: <TProvider extends ProviderFunction>({ context, provider, providerParams, agentCallParams, }: {
712
+ context: WorkflowContext;
713
+ provider: TProvider;
714
+ providerParams?: Omit<Required<Parameters<TProvider>>[0], "fetch">;
715
+ agentCallParams? /**
716
+ * creates an openai model for agents
717
+ */: AgentCallParams;
718
+ }) => ReturnType<TProvider>;
703
719
  }
704
720
 
705
721
  /**
@@ -934,6 +950,11 @@ declare class WorkflowContext<TInitialPayload = unknown> {
934
950
  * }
935
951
  */
936
952
  call<TResult = unknown, TBody = unknown>(stepName: string, settings: CallSettings<TBody>): Promise<CallResponse<TResult>>;
953
+ call<TResult extends {
954
+ workflowRunId: string;
955
+ } = {
956
+ workflowRunId: string;
957
+ }, TBody = unknown>(stepName: string, settings: LazyInvokeStepParams<TBody, unknown> & Pick<CallSettings, "timeout">): Promise<CallResponse<TResult>>;
937
958
  /**
938
959
  * Pauses workflow execution until a specific event occurs or a timeout is reached.
939
960
  *
@@ -1150,6 +1171,13 @@ type WorkflowServeOptions<TResponse extends Response = Response, TInitialPayload
1150
1171
  * Url to call if QStash retries are exhausted while executing the workflow
1151
1172
  */
1152
1173
  failureUrl?: string;
1174
+ /**
1175
+ * Error handler called when an error occurs in the workflow. This is
1176
+ * different from `failureFunction` in that it is called when an error
1177
+ * occurs in the workflow, while `failureFunction` is called when QStash
1178
+ * retries are exhausted.
1179
+ */
1180
+ onError?: (error: Error) => void;
1153
1181
  /**
1154
1182
  * Failure function called when QStash retries are exhausted while executing
1155
1183
  * the workflow. Will overwrite `failureUrl` parameter with the workflow