|
7 | 7 | * Contains auto-generated codegen templates for steps with stepHandler. |
8 | 8 | * These templates are used when exporting workflows to standalone projects. |
9 | 9 | * |
10 | | - * Generated templates: 10 |
| 10 | + * Generated templates: 12 |
11 | 11 | */ |
12 | 12 |
|
13 | 13 | /** |
@@ -567,6 +567,162 @@ export async function sendSlackMessageStep( |
567 | 567 | }; |
568 | 568 | } |
569 | 569 | } |
| 570 | +`, |
| 571 | + |
| 572 | + "superagent/guard": `import { fetchCredentials } from "./lib/credential-helper"; |
| 573 | +
|
| 574 | +function getErrorMessage(error: unknown): string { |
| 575 | + if (error instanceof Error) return error.message; |
| 576 | + return String(error); |
| 577 | +} |
| 578 | +
|
| 579 | +type GuardResult = { |
| 580 | + classification: GuardClassification; |
| 581 | + violationTypes: string[]; |
| 582 | + cweCodes: string[]; |
| 583 | + reasoning?: string; |
| 584 | +}; |
| 585 | +
|
| 586 | +export type SuperagentGuardCoreInput = { |
| 587 | + text: string; |
| 588 | +}; |
| 589 | +
|
| 590 | +export async function superagentGuardStep( |
| 591 | + input: SuperagentGuardCoreInput, |
| 592 | +): Promise<GuardResult> { |
| 593 | + "use step"; |
| 594 | + const credentials = await fetchCredentials("superagent"); |
| 595 | + const apiKey = credentials.SUPERAGENT_API_KEY; |
| 596 | +
|
| 597 | + if (!apiKey) { |
| 598 | + throw new Error("Superagent API Key is not configured."); |
| 599 | + } |
| 600 | +
|
| 601 | + try { |
| 602 | + const response = await fetch("https://app.superagent.sh/api/guard", { |
| 603 | + method: "POST", |
| 604 | + headers: { |
| 605 | + "Content-Type": "application/json", |
| 606 | + Authorization: \`Bearer \${apiKey}\`, |
| 607 | + }, |
| 608 | + body: JSON.stringify({ |
| 609 | + text: input.text, |
| 610 | + }), |
| 611 | + }); |
| 612 | +
|
| 613 | + if (!response.ok) { |
| 614 | + const error = await response.text(); |
| 615 | + throw new Error(\`Guard API error: \${error}\`); |
| 616 | + } |
| 617 | +
|
| 618 | + const data = await response.json(); |
| 619 | + const choice = data.choices?.[0]; |
| 620 | + const content = choice?.message?.content; |
| 621 | +
|
| 622 | + if (!content || typeof content !== "object") { |
| 623 | + throw new Error( |
| 624 | + "Invalid Guard API response: missing or invalid content structure", |
| 625 | + ); |
| 626 | + } |
| 627 | +
|
| 628 | + const classification = content.classification; |
| 629 | + if ( |
| 630 | + !classification || |
| 631 | + (classification !== "allow" && classification !== "block") |
| 632 | + ) { |
| 633 | + throw new Error( |
| 634 | + \`Invalid Guard API response: missing or invalid classification (received: \${JSON.stringify(classification)})\`, |
| 635 | + ); |
| 636 | + } |
| 637 | +
|
| 638 | + return { |
| 639 | + classification, |
| 640 | + violationTypes: content?.violation_types || [], |
| 641 | + cweCodes: content?.cwe_codes || [], |
| 642 | + reasoning: choice?.message?.reasoning, |
| 643 | + }; |
| 644 | + } catch (error) { |
| 645 | + throw new Error(\`Failed to analyze text: \${getErrorMessage(error)}\`); |
| 646 | + } |
| 647 | +} |
| 648 | +`, |
| 649 | + |
| 650 | + "superagent/redact": `import { fetchCredentials } from "./lib/credential-helper"; |
| 651 | +
|
| 652 | +function getErrorMessage(error: unknown): string { |
| 653 | + if (error instanceof Error) return error.message; |
| 654 | + return String(error); |
| 655 | +} |
| 656 | +
|
| 657 | +type RedactResult = { |
| 658 | + redactedText: string; |
| 659 | + reasoning?: string; |
| 660 | +}; |
| 661 | +
|
| 662 | +export type SuperagentRedactCoreInput = { |
| 663 | + text: string; |
| 664 | + entities?: string[] | string; |
| 665 | +}; |
| 666 | +
|
| 667 | +export async function superagentRedactStep( |
| 668 | + input: SuperagentRedactCoreInput, |
| 669 | +): Promise<RedactResult> { |
| 670 | + "use step"; |
| 671 | + const credentials = await fetchCredentials("superagent"); |
| 672 | + const apiKey = credentials.SUPERAGENT_API_KEY; |
| 673 | +
|
| 674 | + if (!apiKey) { |
| 675 | + throw new Error("Superagent API Key is not configured."); |
| 676 | + } |
| 677 | +
|
| 678 | + try { |
| 679 | + const body: { text: string; entities?: string[] } = { |
| 680 | + text: input.text, |
| 681 | + }; |
| 682 | +
|
| 683 | + if (input.entities) { |
| 684 | + let entitiesArray: string[]; |
| 685 | +
|
| 686 | + if (typeof input.entities === "string") { |
| 687 | + entitiesArray = input.entities.split(",").map((e) => e.trim()); |
| 688 | + } else if (Array.isArray(input.entities)) { |
| 689 | + entitiesArray = input.entities.map((e) => String(e).trim()); |
| 690 | + } else { |
| 691 | + entitiesArray = []; |
| 692 | + } |
| 693 | +
|
| 694 | + const validEntities = entitiesArray.filter((e) => e.length > 0); |
| 695 | +
|
| 696 | + if (validEntities.length > 0) { |
| 697 | + body.entities = validEntities; |
| 698 | + } |
| 699 | + } |
| 700 | +
|
| 701 | + const response = await fetch("https://app.superagent.sh/api/redact", { |
| 702 | + method: "POST", |
| 703 | + headers: { |
| 704 | + "Content-Type": "application/json", |
| 705 | + Authorization: \`Bearer \${apiKey}\`, |
| 706 | + }, |
| 707 | + body: JSON.stringify(body), |
| 708 | + }); |
| 709 | +
|
| 710 | + if (!response.ok) { |
| 711 | + const error = await response.text(); |
| 712 | + throw new Error(\`Redact API error: \${error}\`); |
| 713 | + } |
| 714 | +
|
| 715 | + const data = await response.json(); |
| 716 | + const choice = data.choices?.[0]; |
| 717 | +
|
| 718 | + return { |
| 719 | + redactedText: choice?.message?.content || input.text, |
| 720 | + reasoning: choice?.message?.reasoning, |
| 721 | + }; |
| 722 | + } catch (error) { |
| 723 | + throw new Error(\`Failed to redact text: \${getErrorMessage(error)}\`); |
| 724 | + } |
| 725 | +} |
570 | 726 | `, |
571 | 727 |
|
572 | 728 | "v0/create-chat": `import { createClient, type ChatsCreateResponse } from "v0-sdk"; |
|
0 commit comments