2727{% endblock %}
2828
2929{% block imports %}
30+ {% if agentStyle == " class" %}
31+ import { ToolLoopAgent, tool, jsonSchema, stepCountIs } from "ai";
32+ {% elif isStreaming %}
33+ import { streamText, tool, jsonSchema, stepCountIs } from "ai";
34+ {% else %}
3035import { generateText, tool, jsonSchema, stepCountIs } from "ai";
36+ {% endif %}
37+ {% if provider == " anthropic" %}
38+ import { createAnthropic } from "@ai-sdk/anthropic";
39+ {% else %}
3140import { createOpenAI } from "@ai-sdk/openai";
41+ {% endif %}
3242{% endblock %}
3343
3444{% block dynamic_imports %}
45+ {% if provider == " anthropic" %}
46+ const anthropic = createAnthropic({ apiKey: env.ANTHROPIC_API_KEY });
47+ {% else %}
3548 const openai = createOpenAI({ apiKey: env.OPENAI_API_KEY });
49+ {% endif %}
3650{% endblock %}
3751
52+ {# Helper macro for model reference #}
53+ {% macro modelRef (input ) %}
54+ {% if provider == " anthropic" %} anthropic({% if causeAPIError %} "invalid-model"{% else %} "{{ input .model }} "{% endif %} ){% else %} openai({% if causeAPIError %} "invalid-model"{% else %} "{{ input .model }} "{% endif %} ){% endif %}
55+ {% endmacro %}
56+
3857{% block test %}
3958{% if agent and agent .tools and agent .tools .length > 0 %}
4059 const tools = {
@@ -70,10 +89,106 @@ import { createOpenAI } from "@ai-sdk/openai";
7089 // Request {{ loop .index }} {% if loop .length > 1 %} of {{ loop .length }} {% endif %}
7190
7291 try {
92+ {% if agentStyle == " class" %}
93+ // ToolLoopAgent class-based approach
94+ const agent = new ToolLoopAgent({
95+ model: {{ modelRef (input ) | trim }} ,
96+ {% if agent and agent .tools and agent .tools .length > 0 %}
97+ tools,
98+ {% endif %}
99+ {% if system_content %}
100+ instructions: "{{ system_content }} ",
101+ {% endif %}
102+ stopWhen: stepCountIs(10),
103+ experimental_telemetry: {
104+ isEnabled: true,
105+ functionId: "{{ agent .name if agent else ' assistant' }} ",
106+ recordInputs: true,
107+ recordOutputs: true,
108+ },
109+ });
110+
111+ {% if isStreaming %}
112+ {% if user_content is iterable and user_content is not string %}
113+ const { textStream } = await agent.stream({
114+ messages: [
115+ {
116+ role: "user",
117+ content: {{ renderVercelContent (user_content ) }} ,
118+ },
119+ ],
120+ });
121+ {% else %}
122+ const { textStream } = await agent.stream({
123+ prompt: "{{ user_content }} ",
124+ });
125+ {% endif %}
126+ const chunks = [];
127+ for await (const chunk of textStream) {
128+ chunks.push(chunk);
129+ }
130+ console.log("Response:", chunks.join(""));
131+ {% else %}
132+ {% if user_content is iterable and user_content is not string %}
133+ const { text } = await agent.generate({
134+ messages: [
135+ {
136+ role: "user",
137+ content: {{ renderVercelContent (user_content ) }} ,
138+ },
139+ ],
140+ });
141+ {% else %}
142+ const { text } = await agent.generate({
143+ prompt: "{{ user_content }} ",
144+ });
145+ {% endif %}
146+ console.log("Response:", text);
147+ {% endif %}
148+ {% else %}
149+ // Function-based approach
150+ {% if isStreaming %}
151+ {% if user_content is iterable and user_content is not string %}
152+ const { textStream } = streamText({
153+ model: {{ modelRef (input ) | trim }} ,
154+ {% if system_content %}
155+ system: "{{ system_content }} ",
156+ {% endif %}
157+ messages: [
158+ {
159+ role: "user",
160+ content: {{ renderVercelContent (user_content ) }} ,
161+ },
162+ ],
163+ {% if agent and agent .tools and agent .tools .length > 0 %}
164+ tools,
165+ stopWhen: stepCountIs(10),
166+ {% endif %}
167+ experimental_telemetry: { isEnabled: true, recordInputs: true, recordOutputs: true },
168+ });
169+ {% else %}
170+ const { textStream } = streamText({
171+ model: {{ modelRef (input ) | trim }} ,
172+ {% if system_content %}
173+ system: "{{ system_content }} ",
174+ {% endif %}
175+ prompt: "{{ user_content }} ",
176+ {% if agent and agent .tools and agent .tools .length > 0 %}
177+ tools,
178+ stopWhen: stepCountIs(10),
179+ {% endif %}
180+ experimental_telemetry: { isEnabled: true, recordInputs: true, recordOutputs: true },
181+ });
182+ {% endif %}
183+ const chunks = [];
184+ for await (const chunk of textStream) {
185+ chunks.push(chunk);
186+ }
187+ console.log("Response:", chunks.join(""));
188+ {% else %}
73189{% if user_content is iterable and user_content is not string %}
74- // Multimodal content - use messages array
75190 const { text } = await generateText({
76- model: openai( {% if causeAPIError %} "invalid-model" {% else %} " {{ input . model }} " {% endif %} ) ,
191+ model: {{ modelRef ( input ) | trim }} ,
77192{% if system_content %}
78193 system: "{{ system_content }} ",
79194{% endif %}
@@ -90,9 +205,8 @@ import { createOpenAI } from "@ai-sdk/openai";
90205 experimental_telemetry: { isEnabled: true, recordInputs: true, recordOutputs: true },
91206 });
92207{% else %}
93- // Simple text prompt
94208 const { text } = await generateText({
95- model: openai( {% if causeAPIError %} "invalid-model" {% else %} " {{ input . model }} " {% endif %} ) ,
209+ model: {{ modelRef ( input ) | trim }} ,
96210{% if system_content %}
97211 system: "{{ system_content }} ",
98212{% endif %}
@@ -105,6 +219,8 @@ import { createOpenAI } from "@ai-sdk/openai";
105219 });
106220{% endif %}
107221 console.log("Response:", text);
222+ {% endif %}
223+ {% endif %}
108224 } catch (error) {
109225 Sentry.captureException(error);
110226 console.error("Error:", error.message);
0 commit comments