@upstash/workflow 0.2.22 → 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/solidjs.js CHANGED
@@ -178,12 +178,11 @@ var WORKFLOW_PROTOCOL_VERSION_HEADER = "Upstash-Workflow-Sdk-Version";
178
178
  var DEFAULT_CONTENT_TYPE = "application/json";
179
179
  var NO_CONCURRENCY = 1;
180
180
  var DEFAULT_RETRIES = 3;
181
- var VERSION = "v0.2.22";
181
+ var VERSION = "v0.3.0-rc";
182
182
  var SDK_TELEMETRY = `@upstash/workflow@${VERSION}`;
183
183
  var TELEMETRY_HEADER_SDK = "Upstash-Telemetry-Sdk";
184
184
  var TELEMETRY_HEADER_FRAMEWORK = "Upstash-Telemetry-Framework";
185
185
  var TELEMETRY_HEADER_RUNTIME = "Upstash-Telemetry-Runtime";
186
- var TELEMETRY_HEADER_AGENT = "Upstash-Telemetry-Agent";
187
186
 
188
187
  // src/context/auto-executor.ts
189
188
  var import_qstash5 = require("@upstash/qstash");
@@ -556,9 +555,9 @@ var Ok = class {
556
555
  }
557
556
  safeUnwrap() {
558
557
  const value = this.value;
559
- return function* () {
558
+ return (function* () {
560
559
  return value;
561
- }();
560
+ })();
562
561
  }
563
562
  _unsafeUnwrap(_) {
564
563
  return this.value;
@@ -617,10 +616,10 @@ var Err = class {
617
616
  }
618
617
  safeUnwrap() {
619
618
  const error = this.error;
620
- return function* () {
619
+ return (function* () {
621
620
  yield err(error);
622
621
  throw new Error("Do not use this generator out of `safeTry`");
623
- }();
622
+ })();
624
623
  }
625
624
  _unsafeUnwrap(config) {
626
625
  throw createNeverThrowError("Called `_unsafeUnwrap` on an Err", this, config);
@@ -1587,20 +1586,6 @@ var LazyInvokeStep = class extends BaseLazyStep {
1587
1586
  }
1588
1587
  };
1589
1588
 
1590
- // src/agents/constants.ts
1591
- var AGENT_NAME_HEADER = "upstash-agent-name";
1592
- var MANAGER_AGENT_PROMPT = `You are an agent orchestrating other AI Agents.
1593
-
1594
- These other agents have tools available to them.
1595
-
1596
- Given a prompt, utilize these agents to address requests.
1597
-
1598
- 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.
1599
-
1600
- Avoid calling the same agent twice in one turn. Instead, prefer to call it once but provide everything
1601
- you need from that agent.
1602
- `;
1603
-
1604
1589
  // src/qstash/headers.ts
1605
1590
  var WorkflowHeaders = class {
1606
1591
  userHeaders;
@@ -1649,8 +1634,7 @@ var WorkflowHeaders = class {
1649
1634
  [WORKFLOW_URL_HEADER]: this.workflowConfig.workflowUrl,
1650
1635
  [WORKFLOW_FEATURE_HEADER]: "LazyFetch,InitialBody,WF_DetectTrigger" + (this.keepTriggerConfig ? ",WF_TriggerOnConfig" : ""),
1651
1636
  [WORKFLOW_PROTOCOL_VERSION_HEADER]: WORKFLOW_PROTOCOL_VERSION,
1652
- ...this.workflowConfig.telemetry ? getTelemetryHeaders(this.workflowConfig.telemetry) : {},
1653
- ...this.workflowConfig.telemetry && this.stepInfo?.lazyStep instanceof LazyCallStep && this.stepInfo.lazyStep.headers[AGENT_NAME_HEADER] ? { [TELEMETRY_HEADER_AGENT]: "true" } : {}
1637
+ ...this.workflowConfig.telemetry ? getTelemetryHeaders(this.workflowConfig.telemetry) : {}
1654
1638
  };
1655
1639
  if (this.stepInfo?.lazyStep.stepType !== "Call") {
1656
1640
  this.headers.rawHeaders[`Upstash-Forward-${WORKFLOW_PROTOCOL_VERSION_HEADER}`] = WORKFLOW_PROTOCOL_VERSION;
@@ -2313,309 +2297,6 @@ var WorkflowApi = class extends BaseWorkflowApi {
2313
2297
  }
2314
2298
  };
2315
2299
 
2316
- // src/agents/index.ts
2317
- var import_openai2 = require("@ai-sdk/openai");
2318
-
2319
- // src/agents/adapters.ts
2320
- var import_ai = require("ai");
2321
- var fetchWithContextCall = async (context, agentCallParams, ...params) => {
2322
- const [input, init] = params;
2323
- try {
2324
- const headers = init?.headers ? Object.fromEntries(new Headers(init.headers).entries()) : {};
2325
- const body = init?.body ? JSON.parse(init.body) : void 0;
2326
- const agentName = headers[AGENT_NAME_HEADER];
2327
- const stepName = agentName ? `Call Agent ${agentName}` : "Call Agent";
2328
- const responseInfo = await context.call(stepName, {
2329
- url: input.toString(),
2330
- method: init?.method,
2331
- headers,
2332
- body,
2333
- timeout: agentCallParams?.timeout,
2334
- retries: agentCallParams?.retries,
2335
- retryDelay: agentCallParams?.retryDelay,
2336
- flowControl: agentCallParams?.flowControl
2337
- });
2338
- const responseHeaders = new Headers(
2339
- Object.entries(responseInfo.header).reduce(
2340
- (acc, [key, values]) => {
2341
- acc[key] = values.join(", ");
2342
- return acc;
2343
- },
2344
- {}
2345
- )
2346
- );
2347
- return new Response(JSON.stringify(responseInfo.body), {
2348
- status: responseInfo.status,
2349
- headers: responseHeaders
2350
- });
2351
- } catch (error) {
2352
- if (error instanceof Error && isInstanceOf(error, WorkflowAbort)) {
2353
- throw error;
2354
- } else {
2355
- console.error("Error in fetch implementation:", error);
2356
- throw error;
2357
- }
2358
- }
2359
- };
2360
- var createWorkflowModel = ({
2361
- context,
2362
- provider,
2363
- providerParams,
2364
- agentCallParams
2365
- }) => {
2366
- return provider({
2367
- fetch: (...params) => fetchWithContextCall(context, agentCallParams, ...params),
2368
- ...providerParams
2369
- });
2370
- };
2371
- var wrapTools = ({
2372
- context,
2373
- tools
2374
- }) => {
2375
- return Object.fromEntries(
2376
- Object.entries(tools).map((toolInfo) => {
2377
- const [toolName, tool3] = toolInfo;
2378
- const executeAsStep = "executeAsStep" in tool3 ? tool3.executeAsStep : true;
2379
- const aiSDKTool = convertToAISDKTool(tool3);
2380
- const execute = aiSDKTool.execute;
2381
- if (execute && executeAsStep) {
2382
- const wrappedExecute = (...params) => {
2383
- return context.run(`Run tool ${toolName}`, () => execute(...params));
2384
- };
2385
- aiSDKTool.execute = wrappedExecute;
2386
- }
2387
- return [toolName, aiSDKTool];
2388
- })
2389
- );
2390
- };
2391
- var convertToAISDKTool = (tool3) => {
2392
- const isLangchainTool = "invoke" in tool3;
2393
- return isLangchainTool ? convertLangchainTool(tool3) : tool3;
2394
- };
2395
- var convertLangchainTool = (langchainTool) => {
2396
- return (0, import_ai.tool)({
2397
- description: langchainTool.description,
2398
- parameters: langchainTool.schema,
2399
- execute: async (...param) => langchainTool.invoke(...param)
2400
- });
2401
- };
2402
-
2403
- // src/agents/agent.ts
2404
- var import_zod = require("zod");
2405
- var import_ai2 = require("ai");
2406
-
2407
- // src/serve/utils.ts
2408
- var isDisabledWorkflowContext = (context) => {
2409
- return "disabled" in context;
2410
- };
2411
-
2412
- // src/agents/agent.ts
2413
- var Agent = class {
2414
- name;
2415
- tools;
2416
- maxSteps;
2417
- background;
2418
- model;
2419
- temparature;
2420
- context;
2421
- constructor({ tools, maxSteps, background, name, model, temparature = 0.1 }, context) {
2422
- this.name = name;
2423
- this.tools = tools ?? {};
2424
- this.maxSteps = maxSteps;
2425
- this.background = background;
2426
- this.model = model;
2427
- this.temparature = temparature;
2428
- this.context = context;
2429
- }
2430
- /**
2431
- * Trigger the agent by passing a prompt
2432
- *
2433
- * @param prompt task to assign to the agent
2434
- * @returns Response as `{ text: string }`
2435
- */
2436
- async call({ prompt }) {
2437
- try {
2438
- if (isDisabledWorkflowContext(this.context)) {
2439
- await this.context.sleep("abort", 0);
2440
- }
2441
- const result = await (0, import_ai2.generateText)({
2442
- model: this.model,
2443
- tools: this.tools,
2444
- maxSteps: this.maxSteps,
2445
- system: this.background,
2446
- prompt,
2447
- headers: {
2448
- [AGENT_NAME_HEADER]: this.name
2449
- },
2450
- temperature: this.temparature
2451
- });
2452
- return { text: result.text };
2453
- } catch (error) {
2454
- if (isInstanceOf(error, import_ai2.ToolExecutionError)) {
2455
- if (error.cause instanceof Error && isInstanceOf(error.cause, WorkflowAbort)) {
2456
- throw error.cause;
2457
- } else if (isInstanceOf(error.cause, import_ai2.ToolExecutionError) && isInstanceOf(error.cause.cause, WorkflowAbort)) {
2458
- throw error.cause.cause;
2459
- } else {
2460
- throw error;
2461
- }
2462
- } else {
2463
- throw error;
2464
- }
2465
- }
2466
- }
2467
- /**
2468
- * Convert the agent to a tool which can be used by other agents.
2469
- *
2470
- * @returns the agent as a tool
2471
- */
2472
- asTool() {
2473
- const toolDescriptions = Object.values(this.tools).map((tool3) => tool3.description).join("\n");
2474
- return (0, import_ai2.tool)({
2475
- parameters: import_zod.z.object({ prompt: import_zod.z.string() }),
2476
- execute: async ({ prompt }) => {
2477
- return await this.call({ prompt });
2478
- },
2479
- description: `An AI Agent with the following background: ${this.background}Has access to the following tools: ${toolDescriptions}`
2480
- });
2481
- }
2482
- };
2483
- var ManagerAgent = class extends Agent {
2484
- agents;
2485
- /**
2486
- * A manager agent which coordinates agents available to it to achieve a
2487
- * given task
2488
- *
2489
- * @param name Name of the agent
2490
- * @param background Background of the agent. If not passed, default will be used.
2491
- * @param model LLM model to use
2492
- * @param agents: List of agents available to the agent
2493
- * @param maxSteps number of times the manager agent can call the LLM at most.
2494
- * If the agent abruptly stops execution after calling other agents, you may
2495
- * need to increase maxSteps
2496
- */
2497
- constructor({
2498
- agents,
2499
- background = MANAGER_AGENT_PROMPT,
2500
- model,
2501
- maxSteps,
2502
- name = "manager llm"
2503
- }, context) {
2504
- super(
2505
- {
2506
- background,
2507
- maxSteps,
2508
- tools: Object.fromEntries(agents.map((agent) => [agent.name, agent.asTool()])),
2509
- name,
2510
- model
2511
- },
2512
- context
2513
- );
2514
- this.agents = agents;
2515
- }
2516
- };
2517
-
2518
- // src/agents/task.ts
2519
- var Task = class {
2520
- context;
2521
- taskParameters;
2522
- constructor({
2523
- context,
2524
- taskParameters
2525
- }) {
2526
- this.context = context;
2527
- this.taskParameters = taskParameters;
2528
- }
2529
- /**
2530
- * Run the agents to complete the task
2531
- *
2532
- * @returns Result of the task as { text: string }
2533
- */
2534
- async run() {
2535
- const { prompt, ...otherParams } = this.taskParameters;
2536
- if ("agent" in otherParams) {
2537
- const agent = otherParams.agent;
2538
- const result = await agent.call({
2539
- prompt
2540
- });
2541
- return { text: result.text };
2542
- } else {
2543
- const { agents, maxSteps, model, background } = otherParams;
2544
- const managerAgent = new ManagerAgent(
2545
- {
2546
- model,
2547
- maxSteps,
2548
- agents,
2549
- name: "Manager LLM",
2550
- background
2551
- },
2552
- this.context
2553
- );
2554
- const result = await managerAgent.call({ prompt });
2555
- return { text: result.text };
2556
- }
2557
- }
2558
- };
2559
-
2560
- // src/agents/index.ts
2561
- var WorkflowAgents = class {
2562
- context;
2563
- constructor({ context }) {
2564
- this.context = context;
2565
- }
2566
- /**
2567
- * Defines an agent
2568
- *
2569
- * ```ts
2570
- * const researcherAgent = context.agents.agent({
2571
- * model,
2572
- * name: 'academic',
2573
- * maxSteps: 2,
2574
- * tools: {
2575
- * wikiTool: new WikipediaQueryRun({
2576
- * topKResults: 1,
2577
- * maxDocContentLength: 500,
2578
- * })
2579
- * },
2580
- * background:
2581
- * 'You are researcher agent with access to Wikipedia. ' +
2582
- * 'Utilize Wikipedia as much as possible for correct information',
2583
- * });
2584
- * ```
2585
- *
2586
- * @param params agent parameters
2587
- * @returns
2588
- */
2589
- agent(params) {
2590
- const wrappedTools = wrapTools({ context: this.context, tools: params.tools });
2591
- return new Agent(
2592
- {
2593
- ...params,
2594
- tools: wrappedTools
2595
- },
2596
- this.context
2597
- );
2598
- }
2599
- task(taskParameters) {
2600
- return new Task({ context: this.context, taskParameters });
2601
- }
2602
- /**
2603
- * creates an openai model for agents
2604
- */
2605
- openai(...params) {
2606
- const [model, settings] = params;
2607
- const { baseURL, apiKey, callSettings, ...otherSettings } = settings ?? {};
2608
- const openaiModel = this.AISDKModel({
2609
- context: this.context,
2610
- provider: import_openai2.createOpenAI,
2611
- providerParams: { baseURL, apiKey, compatibility: "strict" },
2612
- agentCallParams: callSettings
2613
- });
2614
- return openaiModel(model, otherSettings);
2615
- }
2616
- AISDKModel = createWorkflowModel;
2617
- };
2618
-
2619
2300
  // src/serve/serve-many.ts
2620
2301
  var getNewUrlFromWorkflowId = (url, workflowId) => {
2621
2302
  if (!workflowId) {
@@ -2857,7 +2538,7 @@ var WorkflowContext = class {
2857
2538
  * @returns result of the step function
2858
2539
  */
2859
2540
  async run(stepName, stepFunction) {
2860
- const wrappedStepFunction = () => this.executor.wrapStep(stepName, stepFunction);
2541
+ const wrappedStepFunction = (() => this.executor.wrapStep(stepName, stepFunction));
2861
2542
  return await this.addStep(new LazyFunctionStep(stepName, wrappedStepFunction));
2862
2543
  }
2863
2544
  /**
@@ -3028,11 +2709,6 @@ var WorkflowContext = class {
3028
2709
  context: this
3029
2710
  });
3030
2711
  }
3031
- get agents() {
3032
- return new WorkflowAgents({
3033
- context: this
3034
- });
3035
- }
3036
2712
  };
3037
2713
 
3038
2714
  // src/logger.ts
package/solidjs.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  SDK_TELEMETRY,
3
3
  serveBase
4
- } from "./chunk-BON2RKOR.mjs";
4
+ } from "./chunk-AGYYZKP7.mjs";
5
5
 
6
6
  // platforms/solidjs.ts
7
7
  var serve = (routeFunction, options) => {
package/svelte.d.mts CHANGED
@@ -1,11 +1,9 @@
1
1
  import * as _sveltejs_kit from '@sveltejs/kit';
2
2
  import { RequestHandler } from '@sveltejs/kit';
3
- import { R as RouteFunction, o as PublicServeOptions, z as InvokableWorkflow } from './types-9nCq6bRP.mjs';
4
- import { s as serveManyBase } from './serve-many-CctdYIfB.mjs';
3
+ import { R as RouteFunction, n as PublicServeOptions, y as InvokableWorkflow } from './types-DESkn7K9.mjs';
4
+ import { s as serveManyBase } from './serve-many-DEwKPF6H.mjs';
5
5
  import '@upstash/qstash';
6
6
  import 'zod';
7
- import 'ai';
8
- import '@ai-sdk/openai';
9
7
 
10
8
  type RequireEnv<T> = T & {
11
9
  env: PublicServeOptions["env"];
@@ -22,9 +20,9 @@ type RequireEnv<T> = T & {
22
20
  declare const serve: <TInitialPayload = unknown, TResult = unknown>(routeFunction: RouteFunction<TInitialPayload, TResult>, options: RequireEnv<PublicServeOptions<TInitialPayload>>) => {
23
21
  POST: RequestHandler;
24
22
  };
25
- declare const createWorkflow: <TInitialPayload, TResult>(routeFunction: RouteFunction<TInitialPayload, TResult>, options: RequireEnv<PublicServeOptions<TInitialPayload>>) => InvokableWorkflow<TInitialPayload, TResult>;
23
+ declare const createWorkflow: <TInitialPayload, TResult>(...params: Parameters<typeof serve<TInitialPayload, TResult>>) => InvokableWorkflow<TInitialPayload, TResult>;
26
24
  declare const serveMany: (workflows: Parameters<typeof serveManyBase>[0]["workflows"], options?: Parameters<typeof serveManyBase>[0]["options"]) => {
27
- POST: (event: _sveltejs_kit.RequestEvent<Partial<Record<string, string>>, string | null>) => Promise<any>;
25
+ POST: (event: _sveltejs_kit.RequestEvent<Record<string, string>, string | null>) => Promise<any>;
28
26
  };
29
27
 
30
28
  export { createWorkflow, serve, serveMany };
package/svelte.d.ts CHANGED
@@ -1,11 +1,9 @@
1
1
  import * as _sveltejs_kit from '@sveltejs/kit';
2
2
  import { RequestHandler } from '@sveltejs/kit';
3
- import { R as RouteFunction, o as PublicServeOptions, z as InvokableWorkflow } from './types-9nCq6bRP.js';
4
- import { s as serveManyBase } from './serve-many-BXDr30rl.js';
3
+ import { R as RouteFunction, n as PublicServeOptions, y as InvokableWorkflow } from './types-DESkn7K9.js';
4
+ import { s as serveManyBase } from './serve-many-DVtHRxeg.js';
5
5
  import '@upstash/qstash';
6
6
  import 'zod';
7
- import 'ai';
8
- import '@ai-sdk/openai';
9
7
 
10
8
  type RequireEnv<T> = T & {
11
9
  env: PublicServeOptions["env"];
@@ -22,9 +20,9 @@ type RequireEnv<T> = T & {
22
20
  declare const serve: <TInitialPayload = unknown, TResult = unknown>(routeFunction: RouteFunction<TInitialPayload, TResult>, options: RequireEnv<PublicServeOptions<TInitialPayload>>) => {
23
21
  POST: RequestHandler;
24
22
  };
25
- declare const createWorkflow: <TInitialPayload, TResult>(routeFunction: RouteFunction<TInitialPayload, TResult>, options: RequireEnv<PublicServeOptions<TInitialPayload>>) => InvokableWorkflow<TInitialPayload, TResult>;
23
+ declare const createWorkflow: <TInitialPayload, TResult>(...params: Parameters<typeof serve<TInitialPayload, TResult>>) => InvokableWorkflow<TInitialPayload, TResult>;
26
24
  declare const serveMany: (workflows: Parameters<typeof serveManyBase>[0]["workflows"], options?: Parameters<typeof serveManyBase>[0]["options"]) => {
27
- POST: (event: _sveltejs_kit.RequestEvent<Partial<Record<string, string>>, string | null>) => Promise<any>;
25
+ POST: (event: _sveltejs_kit.RequestEvent<Record<string, string>, string | null>) => Promise<any>;
28
26
  };
29
27
 
30
28
  export { createWorkflow, serve, serveMany };