整合

Mastra

把 Thunderbit 包成 Mastra tool,給 agent 與 workflow 用

Mastra 是一個給 AI agent 與 workflow 用的 TypeScript 框架。它的 createTool 原語跟 /distill 對得剛剛好。

安裝

npm install @mastra/core zod

定義 tool

import { createTool } from '@mastra/core/tools';
import { z } from 'zod';

const API = 'https://openapi.thunderbit.com/openapi/v1';
const H = { Authorization: `Bearer ${process.env.THUNDERBIT_API_KEY}` };

export const readUrlTool = createTool({
  id: 'read-url',
  description:
    'Fetch a URL and return clean Markdown for the agent to read.',
  inputSchema: z.object({
    url: z.string().url(),
    renderMode: z.enum(['basic', 'advanced']).default('basic'),
  }),
  outputSchema: z.object({ markdown: z.string() }),
  execute: async ({ context }) => {
    const res = await fetch(`${API}/distill`, {
      method: 'POST',
      headers: { ...H, 'Content-Type': 'application/json' },
      body: JSON.stringify(context),
    });
    const json = await res.json();
    return { markdown: json.data.markdown };
  },
});

接進 Agent

import { Agent } from '@mastra/core/agent';
import { openai } from '@ai-sdk/openai';

export const researcher = new Agent({
  name: 'researcher',
  instructions: 'Use read-url to fetch any URL the user asks about.',
  model: openai('gpt-4o'),
  tools: { readUrlTool },
});

在 Workflow 中用

要做多步流水線,從 Mastra 的 Step 呼叫 /batch/distill,輪詢到 COMPLETED —— 詳見 Batch Job Lifecycle

相關文件