> Discover all available pages from the documentation index: https://mastra.zisheng.pro/ja/llms.txt # ドキュメントマネージャーを構築する このガイドでは、プロジェクトのドキュメントを保守するドキュメントマネージャーを構築します。このマネージャーは、誤って上書きすることを防ぎながら、適切に構造化された Markdown ファイルを作成し、ドキュメントを整理された状態に保ちます。Workspace の Filesystem をセットアップし、ドキュメント管理の指示を与えた Agent を作成します。その後、会話形式のプロンプトを使用してドキュメントを生成、更新します。 ## 前提条件 - Node.js `v22.13.0` 以降がインストールされていること - サポートされている[モデル Provider](https://mastra.zisheng.pro/ja/models) の API キー - 既存の Mastra プロジェクト(新しいプロジェクトをセットアップするには、[インストールガイド](https://mastra.zisheng.pro/ja/guides/getting-started/quickstart)に従ってください) ## Workspace をセットアップする Workspace はローカル Filesystem を使用してドキュメントファイルを管理します。Agent は Workspace ディレクトリ内のファイルを読み書きします。`src/mastra/index.ts` ファイルで、[`Workspace`](https://mastra.zisheng.pro/ja/reference/workspace/workspace-class) クラスと [`LocalFilesystem`](https://mastra.zisheng.pro/ja/reference/workspace/local-filesystem) クラスを import します。 ```typescript import { Mastra } from '@mastra/core' import { resolve } from 'node:path' import { Workspace, LocalFilesystem } from '@mastra/core/workspace' const workspace = new Workspace({ filesystem: new LocalFilesystem({ basePath: resolve(import.meta.dirname, '../../workspace'), }), }) export const mastra = new Mastra({ workspace, }) ``` プロジェクトのルートに、`workspace` という新しいフォルダーを作成します。すべてのドキュメントファイルはここに保存され、Agent によって管理されます。 ## ドキュメントのサンプルを追加する `workspace` ディレクトリ内に、次のフォルダーを作成します。 - `docs/guides/`:ハウツーガイド用 - `docs/api/`:API リファレンスドキュメント用 - `docs/tutorials/`:ステップ別チュートリアル用 ドキュメントのインデックスとして `workspace/docs/README.md` を作成します。 ```markdown # Project Documentation Welcome to the documentation! ## Sections - [Guides](./guides/): How-to guides - [API](./api/): API reference - [Tutorials](./tutorials/): Step-by-step tutorials ``` 既存のドキュメントスタイルを Agent が確認できるように、サンプルガイドを追加します。 ````markdown # Getting Started Quickstart guide for the project. ## Installation ```bash npm2yarn npm install example-package ``` ## Quick example ```typescript import { Example } from 'example-package' const example = new Example() example.run() ``` ```` ## ドキュメントマネージャーを作成する 次に、ドキュメントマネージャー Agent を作成します。この Agent には、Workspace 内で Markdown ファイルを作成、更新するための指示を与えます。新しいファイル `src/mastra/agents/docs-manager.ts` を作成し、Agent を定義します。 ```typescript import { Agent } from '@mastra/core/agent' export const docsManager = new Agent({ id: 'docs-manager', name: 'Docs Manager', instructions: `You are a documentation manager that creates and maintains markdown docs. When creating new docs: 1. Ask for topic and target audience 2. Create well-structured markdown with clear sections 3. Include relevant code examples with syntax highlighting 4. Save in the appropriate directory: - /docs/guides/ for user guides and how-tos - /docs/api/ for API reference - /docs/tutorials/ for step-by-step tutorials When updating existing docs: 1. ALWAYS read the file first 2. Make targeted updates without removing unrelated content 3. Preserve existing structure and formatting Use kebab-case naming for files (getting-started.md). Always explain what you're creating and why.`, model: 'openai/gpt-5.6-sol', }) ``` `src/mastra/index.ts` 内で Agent を import し、`Mastra` インスタンスに登録します。 ```typescript import { Mastra } from '@mastra/core' import { resolve } from 'node:path' import { Workspace, LocalFilesystem } from '@mastra/core/workspace' import { docsManager } from './agents/docs-manager' const workspace = new Workspace({ filesystem: new LocalFilesystem({ basePath: resolve(import.meta.dirname, '../../workspace'), }), }) export const mastra = new Mastra({ workspace, agents: { docsManager }, }) ``` ## ドキュメントマネージャーをテストする [Studio](https://mastra.zisheng.pro/ja/docs/studio/overview) を起動し、Agent と対話して動作を確認します。 **npm**: ```bash npm run dev ``` **pnpm**: ```bash pnpm run dev ``` **Yarn**: ```bash yarn dev ``` **Bun**: ```bash bun run dev ``` [localhost:4111](http://localhost:4111) を開き、ドキュメントマネージャーに移動します。 ### 新しいドキュメントを作成する チュートリアルを作成するよう Agent に依頼します。 ```text Create a tutorial for setting up authentication. Cover installation, configuration, and a basic example. ``` Agent は `docs/tutorials/authentication-setup.md` のようなファイルを作成します。Agent のレスポンスは非決定的なため、正確な内容は異なりますが、次のような内容が表示されます。 ````md # Authentication Setup Learn how to add authentication to your application. ## Installation Install the auth package: ```bash npm2yarn npm install @example/auth ``` ## Configuration Create a config file: ```typescript // auth.config.ts export const authConfig = { provider: 'oauth', clientId: process.env.AUTH_CLIENT_ID, secret: process.env.AUTH_SECRET, } ``` ## Basic example ```typescript import { createAuth } from '@example/auth' import { authConfig } from './auth.config' const auth = createAuth(authConfig) app.get('/protected', auth.requireAuth(), (req, res) => { res.json({ user: req.user }) }) ``` ```` ### 既存のドキュメントを更新する 既存のドキュメントを更新してみます。 ```text Update the getting started guide to include a section on configuration after the Quick Example ``` Agent は既存の `getting-started.md` ファイルを読み、適切な挿入位置を見つけます。既存の内容を崩すことなく、新しいセクションを追加します。 ### ドキュメントを整理する インデックスを作成するよう Agent に依頼します。 ```text List all tutorial files and create an index page that links to all of them ``` Agent は、利用可能なすべてのチュートリアルへのリンクを含む `/docs/tutorials/index.md` のようなファイルを作成します。 ## 次のステップ このマネージャーは、次のように拡張できます。 - 関連ドキュメントを見つけるために BM25 またはベクトル検索を追加する - ドキュメントテンプレート用の Skill を作成する - ソースコードからドキュメントを自動生成する仕組みを構築する - GitHub と統合し、コミット時にドキュメントを更新する - リンクと書式をチェックする検証を追加する