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