Config API

取得租戶設定,包含規則和其他設定。

Config API 讓前端可以從 Lens OS 後端取得規則(指令捷徑)和其他設定。

概觀

Config API 讓你的前端可以取得在 Lens OS 主控台中設定的規則,這些規則可以在規則選單中顯示以便快速存取。

設定

src/app/api/lens/agent/config/route.ts
import { NextResponse } from "next/server";
import { auth } from "@/auth";
import { LensClient } from "@lens-os/sdk";

const client = new LensClient({ apiKey: process.env.LENS_OS_API_KEY! });

export async function GET() {
  const session = await auth();
  if (!session?.user) {
    return NextResponse.json(
      { success: false, error: "Unauthorized" },
      { status: 401 }
    );
  }

  try {
    const config = await client.getConfig();
    return NextResponse.json({ success: true, config });
  } catch (err) {
    console.error("[Config API] Error:", err);
    return NextResponse.json(
      { success: false, error: "Failed to fetch config" },
      { status: 500 }
    );
  }
}

回應格式

回應
{
  success: true,
  config: {
    rules: [
      {
        id: "rule-1",
        name: "search",
        displayName: "Search Products",
        description: "Search product database",
        prompt: "Please help me search for:",
      },
      // ...
    ],
    systemPrompt: "You are a helpful assistant...",
    // ...other config
  }
}

前端使用方式

透過 useLensAgent hook,你可以直接從 config 取得已在 Lens OS 主控台設定好的 rules、skills 等資料,不需要自行管理狀態。

取得 rules 並顯示選單
'use client';
import { useLensAgent } from '@lens-os/sdk/react';

export function RulesMenu() {
  const { config } = useLensAgent();

  // 從 config 取得已設定的 rules
  const rules = config?.rules ?? [];

  return (
    <ul>
      {rules.map((rule) => (
        <li key={rule.id}>
          <button onClick={() => sendMessage(rule.prompt)}>
            {rule.displayName}
          </button>
        </li>
      ))}
    </ul>
  );
}

Note

config 由 SDK 自動在 session 初始化時載入,無需手動呼叫 /api/lens/agent/config。在 Lens OS 主控台更新設定後,可能需要幾分鐘才會生效。