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