Glanceway Glanceway
所有資料來源

AI Prompt

生產力 v1.0.0

Send a prompt to an AI model and display the response

@codytseng #ai #llm

安裝與設定方式

  1. 先在 Mac 上安裝並開啟 Glanceway,再使用上方的安裝連結。
  2. 點選「安裝到 Glanceway」。若瀏覽器無法開啟應用程式,下載 .gwsrc 檔案並在資訊源管理中匯入。
  3. 填寫以下必填欄位並儲存,設定更新間隔,等待首次成功取得內容。

設定檢查

  • API_URL — 必填. Full URL for the OpenAI-compatible chat completions endpoint (e.g. https://api.openai.com/v1/chat/completions)
  • API_KEY — 必填. API key for authentication 填寫自己的存取憑證,請勿在截圖或公開檔案中分享。
  • MODEL — 必填. Model name (e.g. gpt-4o, deepseek-chat)
  • PROMPT — 必填. Prompt to send to the AI model

沒有顯示內容時如何排查

  • 對照目前版本設定表檢查欄位、清單項目與憑證。下方範例不包含可用的真實憑證。
  • 檢查網路及上游服務狀態。認證失敗或速率限制可能使更新失敗,請依上游限制調整間隔。
  • 空清單也可能代表沒有符合條件的條目。內容與更新時間取決於資料提供方及資訊源實作。

設定項目

名稱 鍵 類型 必填 預設值 描述
API URL API_URL string 是 — Full URL for the OpenAI-compatible chat completions endpoint (e.g. https://api.openai.com/v1/chat/completions)
API Key API_KEY secret 是 — API key for authentication
Model MODEL string 是 — Model name (e.g. gpt-4o, deepseek-chat)
Prompt PROMPT string 是 — Prompt to send to the AI model

原始碼

id: codytseng/ai-prompt
version: 1.0.0
name: AI Prompt
description: Send a prompt to an AI model and display the response
author: codytseng
author_url: https://github.com/codytseng
category: Productivity
tags:
  - ai
  - llm

config:
  - key: API_URL
    name: API URL
    type: string
    required: true
    description: Full URL for the OpenAI-compatible chat completions endpoint (e.g. https://api.openai.com/v1/chat/completions)
  - key: API_KEY
    name: API Key
    type: secret
    required: true
    description: API key for authentication
  - key: MODEL
    name: Model
    type: string
    required: true
    description: Model name (e.g. gpt-4o, deepseek-chat)
  - key: PROMPT
    name: Prompt
    type: string
    required: true
    description: Prompt to send to the AI model
function uuid() {
  return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
    const r = (Math.random() * 16) | 0;
    return (c === "x" ? r : (r & 0x3) | 0x8).toString(16);
  });
}

const SYSTEM_PROMPT = `You are a data source that returns structured information items. You MUST respond with ONLY a JSON array (no markdown, no code fences, no explanation). Each element in the array is an object with these fields:
- "id": (string, optional) a fixed identifier for the item. Only provide this if the item represents something that should be updated/overwritten on the next refresh. Omit it if each refresh should create new items.
- "title": (string, required) a short headline, MAX 50 characters. Keep it concise — this is only for a brief label or heading.
- "subtitle": (string, optional) the main content and details go here. This field has no length limit and should contain the bulk of the information.
- "url": (string, optional) a relevant link

Example response:
[{"title":"Fun Fact","subtitle":"Honey never spoils. Archaeologists have found 3,000-year-old honey in Egyptian tombs that was still perfectly edible."}]`;

module.exports = async (api) => {
  const apiUrl = api.config.get("API_URL");
  const apiKey = api.config.get("API_KEY");
  const model = api.config.get("MODEL");
  const prompt = api.config.get("PROMPT");

  async function fetchData() {
    const response = await api.fetch(apiUrl, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        Authorization: `Bearer ${apiKey}`,
      },
      body: JSON.stringify({
        model,
        messages: [
          { role: "system", content: SYSTEM_PROMPT },
          { role: "user", content: prompt },
        ],
      }),
    });

    if (!response.ok || !response.json) {
      throw new Error(
        `Failed to fetch AI response (HTTP ${response.status}) ${response.error}`,
      );
    }

    const content = response.json.choices[0]?.message?.content?.trim();
    if (!content) {
      throw new Error("No content in AI response");
    }

    let rawItems;
    try {
      rawItems = JSON.parse(content);
    } catch {
      // If the AI didn't return valid JSON, emit the raw text as a single item
      api.emit([
        {
          id: uuid(),
          title: content,
          subtitle: model,
          timestamp: new Date(),
        },
      ]);
      return;
    }

    if (!Array.isArray(rawItems) || rawItems.length === 0) {
      throw new Error("AI response is not a non-empty JSON array");
    }

    api.emit(
      rawItems
        .filter((item) => item.title)
        .map((item) => ({
          id: item.id || uuid(),
          title: item.title ?? item.subtitle ?? "Untitled",
          subtitle: item.title ? item.subtitle : "",
          url: item.url,
        })),
    );
  }

  await fetchData();

  return {
    refresh: fetchData,
  };
};