TypeScript 殺瘋了,開發 AI 應用新趨勢!
隨著 AI 技術的迅猛發展,越來越多開發者開始構建基于大模型(LLM)、多智能體協作、瀏覽器端推理等新型應用。在這一浪潮中,TypeScript 憑借其強大的類型系統、成熟的工具鏈和活躍的生態,正逐步成為現代 AI 應用開發的主流選擇之一。
- 根據 Y Combinator 統計,約有 60% 至 70% 的 AI Agent 初創公司采用 TypeScript 開發。
- GitHub 數據顯示,近兩年內,TypeScript 在機器學習和 AI 項目中的使用量增長超過 150%。
本文將介紹三款基于 TypeScript 的熱門 AI 應用開發工具 !
OpenAI Agents JS
OpenAI Agents JS 是 OpenAI 推出的 JavaScript/TypeScript SDK,專為構建支持語音交互與多智能體協作的 AI 應用而設計。它是官方 Agents SDK 的 JS/TS 版本,輕量且功能強大,適用于構建復雜的代理系統。
OpenAI Agents JS 的核心功能如下:
- 多智能體協作:支持多個代理的協同工作與動態控制流轉。
- 工具集成:支持結構化輸出、并行調用與函數插件系統。
- 語音支持:通過 WebRTC/WebSocket 構建實時語音智能體,提供瀏覽器優化版本。
- 安全機制:支持輸入/輸出驗證與防護機制。
- 調試與追蹤:內置可視化調試器與運行追蹤工具。
舉個例子:
import { Agent, run, tool } from'@openai/agents';
// 定義一個工具
const getWeather = tool({
name: 'get_weather',
description: '獲取指定城市的天氣',
parameters: { type: 'object', properties: { city: { type: 'string' }}, required: ['city'] },
async execute({ city }) {
return`現在 ${city} 的天氣是晴朗。`;
},
});
// 創建并運行 Agent
const agent = new Agent({
name: '天氣助理',
instructions: '你是一個能提供實時天氣信息的智能助手。',
tools: [getWeather],
});
const result = await run(agent, '告訴我今天北京的天氣');
console.log(result.finalOutput);
Github:https://github.com/openai/openai-agents-js
Mastra
Mastra.ai 是由 Gatsby 創始人推出的開源 TypeScript AI 代理框架,致力于為前端開發者提供完整的 AI 工作流與部署解決方案。它解決了傳統 AI 工具偏向 Python 的痛點,為 JS/TS 社區提供了類型安全且現代化的開發體驗。
Mastra.ai 的核心功能如下:
- 智能代理:支持工具調用、記憶、RAG 能力、任務分解和外部 API 調用。
- 流程引擎:基于 XState 構建流程圖,支持暫停、恢復、調試與可視化。
- RAG 向量檢索:支持 embedding、索引、檢索、rerank,兼容多種向量庫。
- 評估工具:基于 LLM、規則或統計方法,自動評估輸出結果。
- 本地開發與 Playground:內建對話、日志、prompt 調試與 CLI 工具。
- 部署靈活:支持 Vercel、Cloudflare Workers、Netlify、Node.js、React/Next.js 等環境。
舉個例子:創建GitHub倉庫信息代理
import { createTool } from"@mastra/core/tools";
import { z } from"zod";
exportconst githubRepoTool = createTool({
id: "get-github-repo-info",
description: "獲取 GitHub 公共倉庫的基本信息",
inputSchema: z.object({
owner: z.string().describe("GitHub 用戶名或組織"),
repo: z.string().describe("倉庫名稱"),
}),
outputSchema: z.object({
stars: z.number(),
forks: z.number(),
issues: z.number(),
license: z.string().nullable(),
lastPush: z.string(),
description: z.string().nullable(),
}),
execute: async ({ context }) => {
const res = await fetch(`https://api.github.com/repos/${context.owner}/${context.repo}`);
if (res.status === 404) thrownewError(`倉庫 ${context.owner}/${context.repo} 未找到`);
const data = await res.json();
return {
stars: data.stargazers_count,
forks: data.forks_count,
issues: data.open_issues_count,
license: data.license?.name ?? null,
lastPush: data.pushed_at,
description: data.description ?? null,
};
},
});
Github:https://github.com/mastra-ai/mastra
VoltAgent
VoltAgent 是一個現代 TypeScript AI 代理框架,專注于提升 JS/TS 開發者在構建、調試、部署 AI 應用過程中的體驗。相比傳統的復雜代碼或無代碼平臺,VoltAgent 提供結構化編程與可視化調試的雙重優勢。
VoltAgent 的核心功能如下:
- Agent 引擎與多智能體系統:支持
@voltagent/core
模塊定義 agent,可通過 supervisor 協同多個 agent。 - 可視化調試與觀察性:本地 VoltOps 控制臺支持思維鏈可視化,兼容 LangFuse、LangSmith 等平臺。
- 插件系統與集成能力:可調用 API、數據庫、RAG 檢索工具,內建語音交互與外部平臺接入能力。
- Memory 與 RAG 支持:支持持久化上下文,兼容多種向量數據庫與檢索機制。
舉個例子:
import { VoltAgent, Agent } from"@voltagent/core";
import { VercelAIProvider } from"@voltagent/vercel-ai";
import { openai } from"@ai-sdk/openai";
// 定義一個簡單的智能體
const agent = new Agent({
name: "my-agent",
description: "A helpful assistant that answers questions without using tools",
llm: new VercelAIProvider(),
model: openai("gpt-4o-mini"),
});
// 初始化 VoltAgent
new VoltAgent({
agents: {
agent,
},
});
Github:https://github.com/VoltAgent/voltagent。