Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

index.ts

734 lines20.0 KB

1import { Command } from "commander";
2import inquirer from "inquirer";
3import chalk from "chalk";
4import ora from "ora";
5import * as dotenv from "dotenv";
6import { AI } from "@tanstack/ai";
7import { OpenAIAdapter } from "@tanstack/ai-openai";
8import { AnthropicAdapter } from "@tanstack/ai-anthropic";
9import { OllamaAdapter } from "@tanstack/ai-ollama";
10import { GeminiAdapter } from "@tanstack/ai-gemini";
11import type { AIAdapter, Message, ToolCall } from "@tanstack/ai";
12import {
13 getApiKeyUrl,
14 saveApiKeyToEnv,
15 maskApiKey,
16 validateApiKey,
17} from "./utils.js";
18import { AVAILABLE_TOOLS, listTools } from "./tools.js";
19​
20// Load environment variables
21dotenv.config();
22​
23const program = new Command();
24​
25program
26 .name("tanstack-ai")
27 .description("TanStack AI CLI - Open source AI SDK demo")
28 .version("0.1.0");
29​
30program
31 .command("chat")
32 .description("Interactive chat with AI models")
33 .option(
34 "-p, --provider <provider>",
35 "AI provider (openai, anthropic, ollama, gemini)",
36 "openai"
37 )
38 .option("-m, --model <model>", "Model to use")
39 .option(
40 "-k, --api-key <key>",
41 "API key (can also be set via environment variable)"
42 )
43 .option("-d, --debug", "Show raw JSON stream chunks (for debugging)")
44 .action(async (options) => {
45 await runChat(options);
46 });
47​
48program
49 .command("generate")
50 .description("Generate text from a prompt")
51 .option(
52 "-p, --provider <provider>",
53 "AI provider (openai, anthropic, ollama, gemini)",
54 "openai"
55 )
56 .option("-m, --model <model>", "Model to use")
57 .option(
58 "-k, --api-key <key>",
59 "API key (can also be set via environment variable)"
60 )
61 .option("--prompt <prompt>", "Text prompt")
62 .action(async (options) => {
63 await runGenerate(options);
64 });
65​
66program
67 .command("summarize")
68 .description("Summarize text")
69 .option(
70 "-p, --provider <provider>",
71 "AI provider (openai, anthropic, ollama, gemini)",
72 "openai"
73 )
74 .option("-m, --model <model>", "Model to use")
75 .option(
76 "-k, --api-key <key>",
77 "API key (can also be set via environment variable)"
78 )
79 .option("--text <text>", "Text to summarize")
80 .option(
81 "--style <style>",
82 "Summary style (bullet-points, paragraph, concise)",
83 "paragraph"
84 )
85 .action(async (options) => {
86 await runSummarize(options);
87 });
88​
89program
90 .command("embed")
91 .description("Generate embeddings for text")
92 .option(
93 "-p, --provider <provider>",
94 "AI provider (openai, ollama, gemini)",
95 "openai"
96 )
97 .option("-m, --model <model>", "Model to use")
98 .option(
99 "-k, --api-key <key>",
100 "API key (can also be set via environment variable)"
101 )
102 .option("--text <text>", "Text to embed")
103 .action(async (options) => {
104 await runEmbed(options);
105 });
106​
107program
108 .command("tools")
109 .description("Interactive chat with tool/function calling")
110 .option(
111 "-p, --provider <provider>",
112 "AI provider (openai, anthropic)",
113 "openai"
114 )
115 .option("-m, --model <model>", "Model to use")
116 .option(
117 "-k, --api-key <key>",
118 "API key (can also be set via environment variable)"
119 )
120 .option("-d, --debug", "Show raw JSON stream chunks (for debugging)")
121 .action(async (options) => {
122 await runTools(options);
123 });
124​
125async function promptForApiKey(
126 provider: string,
127 envVarName: string
128): Promise<string> {
129 console.log(chalk.yellow(`\n⚠️ No API key found for ${provider}.\n`));
130​
131 const apiUrl = getApiKeyUrl(provider.toLowerCase().replace(" ", ""));
132 if (apiUrl) {
133 console.log(chalk.cyan(`📝 Get your API key at: ${apiUrl}\n`));
134 }
135​
136 const { apiKey } = await inquirer.prompt([
137 {
138 type: "password",
139 name: "apiKey",
140 message: `Enter your ${provider} API key:`,
141 mask: "*",
142 validate: (input) => {
143 if (!input || input.trim().length === 0) {
144 return "API key is required";
145 }
146 return true;
147 },
148 },
149 ]);
150​
151 // Ask if they want to save it
152 const { saveKey } = await inquirer.prompt([
153 {
154 type: "confirm",
155 name: "saveKey",
156 message: "Would you like to save this API key to your .env file?",
157 default: true,
158 },
159 ]);
160​
161 if (saveKey) {
162 const saved = await saveApiKeyToEnv(envVarName, apiKey);
163 if (saved) {
164 console.log(
165 chalk.gray(
166 `\nNote: The .env file has been updated. Make sure it's in your .gitignore!\n`
167 )
168 );
169 }
170 } else {
171 console.log(
172 chalk.gray(
173 `\nTo set it permanently, add ${envVarName}=${maskApiKey(
174 apiKey
175 )} to your .env file\n`
176 )
177 );
178 }
179​
180 return apiKey;
182​
183async function createAdapter(
184 provider: string,
185 apiKey?: string
186): Promise<AIAdapter> {
187 let adapter: AIAdapter;
188 let attempts = 0;
189 const maxAttempts = 3;
190​
191 while (attempts < maxAttempts) {
192 try {
193 switch (provider.toLowerCase()) {
194 case "openai":
195 let openaiKey = apiKey || process.env.OPENAI_API_KEY;
196 if (!openaiKey || attempts > 0) {
197 openaiKey = await promptForApiKey("OpenAI", "OPENAI_API_KEY");
198 apiKey = undefined; // Clear to force re-prompt if needed
199 }
200 adapter = new OpenAIAdapter({ apiKey: openaiKey });
201 break;
202​
203 case "anthropic":
204 let anthropicKey = apiKey || process.env.ANTHROPIC_API_KEY;
205 if (!anthropicKey || attempts > 0) {
206 anthropicKey = await promptForApiKey(
207 "Anthropic",
208 "ANTHROPIC_API_KEY"
209 );
210 apiKey = undefined;
211 }
212 adapter = new AnthropicAdapter({ apiKey: anthropicKey });
213 break;
214​
215 case "ollama":
216 // Ollama doesn't require an API key, just the host
217 const ollamaHost =
218 process.env.OLLAMA_HOST || "http://localhost:11434";
219 console.log(chalk.gray(`\nConnecting to Ollama at ${ollamaHost}\n`));
220 adapter = new OllamaAdapter({ host: ollamaHost });
221 break;
222​
223 case "gemini":
224 let geminiKey = apiKey || process.env.GOOGLE_API_KEY;
225 if (!geminiKey || attempts > 0) {
226 geminiKey = await promptForApiKey(
227 "Google Gemini",
228 "GOOGLE_API_KEY"
229 );
230 apiKey = undefined;
231 }
232 adapter = new GeminiAdapter({ apiKey: geminiKey });
233 break;
234​
235 default:
236 console.error(chalk.red(`Unknown provider: ${provider}`));
237 process.exit(1);
238 }
239​
240 // Validate the adapter
241 if (provider.toLowerCase() !== "ollama" || attempts === 0) {
242 const spinner = ora("Validating API key...").start();
243 try {
244 const isValid = await validateApiKey(adapter, provider);
245 if (!isValid) {
246 spinner.fail(chalk.red("Invalid API key"));
247 attempts++;
248 if (attempts < maxAttempts) {
249 console.log(
250 chalk.yellow(
251 `\nPlease try again (${
252 maxAttempts - attempts
253 } attempts remaining)\n`
254 )
255 );
256 continue;
257 } else {
258 console.error(
259 chalk.red(
260 "\nMaximum attempts reached. Please check your API key."
261 )
262 );
263 process.exit(1);
264 }
265 }
266 spinner.succeed(chalk.green("API key validated"));
267 } catch (error: any) {
268 // Network or other errors
269 spinner.warn(
270 chalk.yellow("Could not validate API key (network issue?)")
271 );
272 console.log(chalk.gray("Proceeding anyway...\n"));
273 }
274 }
275​
276 return adapter;
277 } catch (error) {
278 console.error(chalk.red(`\nError creating adapter: ${error}\n`));
279 attempts++;
280 if (attempts >= maxAttempts) {
281 process.exit(1);
282 }
283 }
284 }
285​
286 process.exit(1);
288​
289async function runChat(options: any) {
290 console.log(chalk.cyan("\n=== TanStack AI CLI ==="));
291 console.log(chalk.gray(`Provider: ${options.provider}`));
292​
293 const adapter = await createAdapter(options.provider, options.apiKey);
294 const ai = new AI(adapter);
295​
296 console.log(chalk.green(`\n✅ Connected to ${options.provider}\n`));
297 console.log(chalk.cyan(`🤖 TanStack AI Chat`));
298 console.log(chalk.gray('Type "exit" to quit\n'));
299​
300 const messages: Message[] = [];
301​
302 if (options.provider === "openai" || options.provider === "anthropic") {
303 messages.push({
304 role: "system",
305 content:
306 "You are a helpful AI assistant powered by TanStack AI, an open-source AI SDK.",
307 });
308 }
309​
310 while (true) {
311 const { prompt } = await inquirer.prompt([
312 {
313 type: "input",
314 name: "prompt",
315 message: chalk.green("You:"),
316 },
317 ]);
318​
319 if (prompt.toLowerCase() === "exit") {
320 console.log(chalk.yellow("\nGoodbye! 👋"));
321 break;
322 }
323​
324 messages.push({ role: "user", content: prompt });
325​
326 const spinner = ora("Thinking...").start();
327​
328 try {
329 const model = options.model || getDefaultModel(options.provider);
330​
331 // Use structured streaming for ALL providers
332 spinner.text = "Assistant:";
333 spinner.stopAndPersist({ symbol: chalk.blue("🤖") });
334​
335 let fullContent = "";
336 let totalTokens = 0;
337​
338 if (options.debug) {
339 console.log(chalk.gray("\n--- Streaming JSON Chunks ---\n"));
340 }
341​
342 // Stream with structured JSON chunks
343 for await (const chunk of ai.streamChat({
344 model,
345 messages,
346 temperature: 0.7,
347 maxTokens: 1000,
348 })) {
349 // Debug mode: show raw JSON
350 if (options.debug) {
351 console.log(chalk.gray(JSON.stringify(chunk)));
352 }
353​
354 if (chunk.type === "content") {
355 // Write the delta (new token) to stdout
356 if (!options.debug) {
357 process.stdout.write(chunk.delta);
358 }
359 fullContent = chunk.content;
360 } else if (chunk.type === "tool_call") {
361 // Handle tool calls
362 const toolName = chunk.toolCall.function.name;
363 const toolArgs = chunk.toolCall.function.arguments;
364 console.log(chalk.cyan(`\n🔧 Tool call: ${toolName}`));
365 if (toolArgs) {
366 console.log(chalk.gray(` Arguments: ${toolArgs}`));
367 }
368 } else if (chunk.type === "done") {
369 // Show token usage
370 if (chunk.usage) {
371 totalTokens = chunk.usage.totalTokens;
372 if (options.debug) {
373 console.log(
374 chalk.green(`\n✅ Done! Reason: ${chunk.finishReason}`)
375 );
376 console.log(
377 chalk.gray(` Prompt tokens: ${chunk.usage.promptTokens}`)
378 );
379 console.log(
380 chalk.gray(
381 ` Completion tokens: ${chunk.usage.completionTokens}`
382 )
383 );
384 console.log(
385 chalk.gray(` Total tokens: ${chunk.usage.totalTokens}`)
386 );
387 }
388 }
389 } else if (chunk.type === "error") {
390 console.error(chalk.red(`\n❌ Error: ${chunk.error.message}`));
391 if (chunk.error.code) {
392 console.error(chalk.gray(` Code: ${chunk.error.code}`));
393 }
394 }
395 }
396​
397 if (options.debug) {
398 console.log(chalk.gray("\n--- End of Stream ---\n"));
399 if (fullContent) {
400 console.log(chalk.blue("Full response:"), fullContent);
401 }
402 } else {
403 console.log("\n");
404 }
405​
406 if (totalTokens > 0 && !options.debug) {
407 console.log(chalk.gray(`[Tokens: ${totalTokens}]\n`));
408 }
409​
410 // Add the full response to conversation history
411 messages.push({
412 role: "assistant",
413 content: fullContent,
414 });
415 } catch (error) {
416 spinner.stop();
417 console.error(chalk.red("\nError:"), error);
418 }
419 }
421​
422async function runGenerate(options: any) {
423 const adapter = await createAdapter(options.provider, options.apiKey);
424 const ai = new AI(adapter);
425​
426 let prompt = options.prompt;
427 if (!prompt) {
428 const result = await inquirer.prompt([
429 {
430 type: "input",
431 name: "prompt",
432 message: "Enter your prompt:",
433 },
434 ]);
435 prompt = result.prompt;
436 }
437​
438 const spinner = ora("Generating...").start();
439​
440 try {
441 const model = options.model || getDefaultModel(options.provider, "text");
442 const response = await ai.generateText({
443 model,
444 prompt,
445 temperature: 0.7,
446 maxTokens: 500,
447 });
448​
449 spinner.stop();
450 console.log(chalk.cyan("\n📝 Generated Text:\n"));
451 console.log(response.text);
452 console.log(chalk.gray(`\n[Tokens: ${response.usage.totalTokens}]`));
453 } catch (error) {
454 spinner.stop();
455 console.error(chalk.red("\nError:"), error);
456 }
458​
459async function runSummarize(options: any) {
460 const adapter = await createAdapter(options.provider, options.apiKey);
461 const ai = new AI(adapter);
462​
463 let text = options.text;
464 if (!text) {
465 const result = await inquirer.prompt([
466 {
467 type: "editor",
468 name: "text",
469 message: "Enter the text to summarize:",
470 },
471 ]);
472 text = result.text;
473 }
474​
475 const spinner = ora("Summarizing...").start();
476​
477 try {
478 const model = options.model || getDefaultModel(options.provider);
479 const response = await ai.summarize({
480 model,
481 text,
482 style: options.style,
483 maxLength: 300,
484 });
485​
486 spinner.stop();
487 console.log(chalk.cyan(`\n📄 Summary (${options.style}):\n`));
488 console.log(response.summary);
489 console.log(chalk.gray(`\n[Tokens: ${response.usage.totalTokens}]`));
490 } catch (error) {
491 spinner.stop();
492 console.error(chalk.red("\nError:"), error);
493 }
495​
496async function runEmbed(options: any) {
497 const adapter = await createAdapter(options.provider, options.apiKey);
498 const ai = new AI(adapter);
499​
500 let text = options.text;
501 if (!text) {
502 const result = await inquirer.prompt([
503 {
504 type: "input",
505 name: "text",
506 message: "Enter the text to embed:",
507 },
508 ]);
509 text = result.text;
510 }
511​
512 const spinner = ora("Generating embeddings...").start();
513​
514 try {
515 const model = options.model || getDefaultEmbeddingModel(options.provider);
516 const response = await ai.embed({
517 model,
518 input: text,
519 });
520​
521 spinner.stop();
522 console.log(chalk.cyan("\n🔢 Embeddings:\n"));
523 console.log(chalk.gray(`Dimensions: ${response.embeddings[0].length}`));
524 console.log(
525 chalk.gray(
526 `First 10 values: [${response.embeddings[0]
527 .slice(0, 10)
528 .map((v) => v.toFixed(4))
529 .join(", ")}...]`
530 )
531 );
532 console.log(chalk.gray(`\n[Tokens: ${response.usage.totalTokens}]`));
533 } catch (error) {
534 spinner.stop();
535 console.error(chalk.red("\nError:"), error);
536 }
538​
539async function runTools(options: any) {
540 console.log(chalk.cyan("\n=== TanStack AI - Tool Calling Demo ==="));
541 console.log(chalk.gray(`Provider: ${options.provider}`));
542​
543 if (options.provider !== "openai" && options.provider !== "anthropic") {
544 console.log(
545 chalk.yellow(
546 "\n⚠️ Tool calling is currently only supported with OpenAI and Anthropic."
547 )
548 );
549 console.log(chalk.gray("Ollama and Gemini coming soon!\n"));
550 return;
551 }
552​
553 // Enable adapter-level debugging if CLI debug is on
554 if (options.debug) {
555 process.env.DEBUG_TOOLS = "true";
556 }
557​
558 const adapter = await createAdapter(options.provider, options.apiKey);
559 const ai = new AI(adapter);
560​
561 console.log(chalk.green(`\n✅ Connected to ${options.provider}\n`));
562 console.log(chalk.cyan("🛠️ TanStack AI - Function Calling"));
563 console.log(
564 chalk.gray('Type "exit" to quit, "tools" to list available tools\n')
565 );
566​
567 console.log(chalk.magenta("Available tools:"));
568 console.log(chalk.gray(listTools()));
569 console.log("");
570​
571 console.log(chalk.yellow("💡 Try these prompts:"));
572 console.log(chalk.gray(" - What's 123 * 456?"));
573 console.log(chalk.gray(" - What's the weather in Paris?"));
574 console.log(chalk.gray(" - Search for React tutorials"));
575 console.log(chalk.gray(" - What time is it in Tokyo?"));
576 console.log("");
577​
578 const messages: Message[] = [
579 {
580 role: "system",
581 content: `You are a helpful AI assistant with access to the following tools:
582​
583${listTools()}
584​
585IMPORTANT: You MUST use the appropriate tool when the user asks for:
586- Weather information → use get_weather
587- Mathematical calculations → use calculate
588- Search queries → use search
589- Time information → use get_current_time
590​
591Do not attempt to answer these questions without using the tools. Always call the appropriate tool to get accurate, real-time information.`,
592 },
593 ];
594​
595 while (true) {
596 const { prompt } = await inquirer.prompt([
597 {
598 type: "input",
599 name: "prompt",
600 message: chalk.green("You:"),
601 },
602 ]);
603​
604 if (prompt.toLowerCase() === "exit") {
605 console.log(chalk.yellow("\nGoodbye! 👋"));
606 break;
607 }
608​
609 if (prompt.toLowerCase() === "tools") {
610 console.log(chalk.magenta("\nAvailable tools:"));
611 console.log(chalk.gray(listTools()));
612 console.log("");
613 continue;
614 }
615​
616 messages.push({ role: "user", content: prompt });
617​
618 const spinner = ora("Thinking...").start();
619​
620 try {
621 const model =
622 options.model ||
623 (options.provider === "anthropic"
624 ? "claude-3-5-sonnet-20241022"
625 : "gpt-3.5-turbo-0125");
626​
627 spinner.text = "Assistant:";
628 spinner.stopAndPersist({ symbol: chalk.blue("🤖") });
629​
630 let fullContent = "";
631 let totalTokens = 0;
632​
633 if (options.debug) {
634 console.log(chalk.gray("\n--- Streaming JSON Chunks ---\n"));
635 }
636​
637 // streamChat automatically executes tools!
638 for await (const chunk of ai.streamChat({
639 model,
640 messages,
641 temperature: 0.7,
642 maxTokens: 1000,
643 tools: AVAILABLE_TOOLS,
644 toolChoice: "auto",
645 maxIterations: 5,
646 })) {
647 if (options.debug) {
648 console.log(chalk.gray(JSON.stringify(chunk)));
649 }
650​
651 if (chunk.type === "content") {
652 // Tool execution messages
653 if (
654 chunk.content.startsWith("[Tool ") &&
655 chunk.content.includes("executed]")
656 ) {
657 if (!options.debug) {
658 const toolName = chunk.content.match(
659 /\[Tool (\w+) executed\]/
660 )?.[1];
661 console.log(chalk.cyan(`\n🔧 Executed tool: ${toolName}`));
662 }
663 } else if (!options.debug && chunk.delta) {
664 // Regular content
665 process.stdout.write(chunk.delta);
666 fullContent = chunk.content;
667 } else if (chunk.delta) {
668 fullContent = chunk.content;
669 }
670 } else if (chunk.type === "tool_call") {
671 // Show tool being called
672 if (!options.debug && chunk.toolCall.function.name) {
673 console.log(
674 chalk.magenta(`\n→ Calling ${chunk.toolCall.function.name}...`)
675 );
676 }
677 } else if (chunk.type === "done") {
678 if (chunk.usage) {
679 totalTokens = chunk.usage.totalTokens;
680 }
681 } else if (chunk.type === "error") {
682 console.error(chalk.red(`\n❌ Error: ${chunk.error.message}`));
683 }
684 }
685​
686 if (!options.debug) {
687 console.log("\n");
688 }
689​
690 if (totalTokens > 0 && !options.debug) {
691 console.log(chalk.gray(`[Tokens: ${totalTokens}]\n`));
692 }
693 } catch (error) {
694 spinner.stop();
695 console.error(chalk.red("\nError:"), error);
696 }
697 }
699​
700function getDefaultModel(provider: string, type: string = "chat"): string {
701 switch (provider.toLowerCase()) {
702 case "openai":
703 return type === "text" ? "gpt-3.5-turbo-instruct" : "gpt-3.5-turbo";
704 case "anthropic":
705 return "claude-3-sonnet-20240229";
706 case "ollama":
707 return "llama2";
708 case "gemini":
709 return "gemini-pro";
710 default:
711 return "";
712 }
714​
715function getDefaultEmbeddingModel(provider: string): string {
716 switch (provider.toLowerCase()) {
717 case "openai":
718 return "text-embedding-ada-002";
719 case "ollama":
720 return "nomic-embed-text";
721 case "gemini":
722 return "embedding-001";
723 default:
724 return "";
725 }
727​
728// Parse arguments
729program.parse();
730​
731// Show help if no command provided
732if (!process.argv.slice(2).length) {
733 program.outputHelp();