สร้างขั้นตอน Google Workspace Studio ที่กำหนดเองใน Node.js บน Cloud Run

1. บทนำ

Google Workspace Studio ช่วยให้คุณปรับกระบวนการทางธุรกิจในแอปพลิเคชัน Google Workspace เช่น Gmail, Google Chat, Google ไดรฟ์ และ Google ชีตให้เป็นอัตโนมัติได้ เมื่อเวิร์กโฟลว์ต้องเริ่มต้นจากเว็บแอปพลิเคชันภายนอกหรือเรียกใช้ตรรกะที่กำหนดเองในสภาพแวดล้อมระบบคลาวด์ของคุณเอง คุณสามารถขยาย Google Workspace Studio ด้วยส่วนเสริมของ Google Workspace แบบ HTTP ใน Node.js บน Cloud Run ได้ดังนี้

  1. ขั้นตอนเริ่มต้นที่กำหนดเอง (workflowTrigger): เริ่มโฟลว์ Google Workspace Studio เมื่อเกิดเหตุการณ์ภายนอก เช่น ลูกค้าส่งคำขอในเว็บพอร์ทัลภายใน โดยเรียกใช้ notifyUri ของทริกเกอร์ (หรือ triggers.fire REST API ของ Google Workspace Studio)
  2. ขั้นตอนการดำเนินการที่กำหนดเอง (workflowAction): รับตัวแปรจากขั้นตอนก่อนหน้าในโฟลว์ เรียกใช้ตรรกะที่กำหนดเองหรือการอนุมาน AI ในบริการ Node.js และแสดงผลตัวแปรผลลัพธ์กลับไปยัง Google Workspace Studio

ใน Codelab นี้ คุณจะได้สร้างและติดตั้งใช้งานส่วนขยาย HTTP ของ Node.js ที่สมบูรณ์สำหรับ Google Workspace Studio ใน Cloud Run, ใช้ Cloud Firestore เพื่อคงการสมัครใช้บริการทริกเกอร์โฟลว์ที่ใช้งานอยู่และคำขอแจ้งปัญหาที่ส่งในการแก้ไขคอนเทนเนอร์ต่างๆ, ใช้ Cloud Tasks เพื่อลองส่งการแจ้งเตือนทริกเกอร์อีกครั้งโดยอัตโนมัติหาก notifyUri ไม่แสดงการตอบกลับว่าสำเร็จ และจัดประเภทความเร่งด่วนของคำขอแจ้งปัญหาในขั้นตอนการกระทำที่กำหนดเองโดยใช้ Gemini 3.8 Flash (gemini-3.8-flash) ในแพลตฟอร์ม Gemini Enterprise Agent พร้อมการทำงานสำรองที่ยืดหยุ่นซึ่งจับคู่คีย์เวิร์ด

สิ่งที่คุณจะสร้าง

  • เว็บแอป Support Quick Portal และส่วนเสริม HTTP ของ Google Workspace ที่สร้างด้วย Node.js, Express, Firestore และ Cloud Tasks ใน Cloud Run
  • ขั้นตอนเริ่มต้นที่กำหนดเองของคำขอแจ้งปัญหาใหม่ (supportTicketTrigger) ซึ่งจะคงการสมัครใช้บริการโฟลว์ที่ใช้งานอยู่ในคอลเล็กชัน studio_triggers Firestore, บันทึกคำขอแจ้งปัญหาที่ส่งในคอลเล็กชัน support_tickets Firestore และเริ่มทำงานเมื่อใดก็ตามที่มีการส่งคำขอแจ้งปัญหาในเว็บพอร์ทัล โดยส่ง ticketTitle และ ticketDescription ไปยังโฟลว์ Google Workspace Studio
  • ผู้ปฏิบัติงานลองใหม่ของ Cloud Tasks (คิว studio-trigger-retries และ POST /api/tasks/fire-trigger) ที่จัดคิวและลองเรียกใช้ทริกเกอร์ที่ล้มเหลวอีกครั้งด้วย Exponential Backoff โดยใช้ userOAuthToken ล่าสุดที่จัดเก็บไว้ใน Firestore เพื่อรับประกันการส่งไปยัง notifyUri
  • ขั้นตอนการดำเนินการที่กำหนดเองตรวจหาความเร่งด่วน (urgencyDetectorStep) ซึ่งจะจัดประเภทความเร่งด่วนของตั๋วโดยใช้ Gemini 3.8 Flash (gemini-3.8-flash) ในแพลตฟอร์ม Agent ของ Gemini Enterprise (aiplatform.googleapis.com) พร้อมการทำงานสำรองอัตโนมัติเป็นการจับคู่คีย์เวิร์ด (urgent, blocker, critical)

สถาปัตยกรรม

แผนภาพต่อไปนี้แสดงวิธีที่ Support Quick Portal, ส่วนเสริม Node.js ใน Cloud Run, Cloud Firestore, Cloud Tasks (studio-trigger-retries), Gemini 3.8 Flash และ Google Workspace Studio โต้ตอบกันตั้งแต่การลงทะเบียนโฟลว์ไปจนถึงการส่งทริกเกอร์ที่ยืดหยุ่นและการดำเนินการ

สถาปัตยกรรมส่วนเสริม Node.js ของ Google Workspace Studio ที่มี Firestore, Cloud Tasks และ Gemini 3.8 Flash

สิ่งที่คุณจะได้เรียนรู้

  • วิธีกำหนดค่าไฟล์ Manifest ของการติดตั้งใช้งานส่วนเสริมของ Google Workspace แบบ HTTP (deployment.json) ด้วยองค์ประกอบ workflowTrigger และ workflowAction
  • วิธีคงการสมัครใช้บริการทริกเกอร์ Google Workspace Studio (triggerId, notifyUri และ userOAuthToken) ไว้ในคอลเล็กชัน studio_triggers Firestore และเริ่มขั้นตอนโดยใช้ notifyUri / triggers.fire
  • วิธีใช้ Cloud Tasks (@google-cloud/tasks) เพื่อลองส่งการแจ้งเตือนทริกเกอร์ที่ล้มเหลวอีกครั้งด้วย Exponential Backoff จนกว่า notifyUri จะแสดงการตอบกลับว่าสำเร็จ 2xx
  • วิธีสร้างขั้นตอนที่กำหนดเองของ workflowAction ใน Node.js โดยใช้เอาต์พุตที่มีโครงสร้างของ Gemini 3.8 Flash (gemini-3.8-flash) บนแพลตฟอร์ม Agent ของ Gemini Enterprise พร้อมการสำรองคีย์เวิร์ด

ข้อกำหนดเบื้องต้น

  • บัญชี Google Workspace ที่มีสิทธิ์เข้าถึง Google Workspace Studio
  • โปรเจ็กต์ Google Cloud ที่เปิดใช้การเรียกเก็บเงิน ซึ่งสร้างขึ้นภายในองค์กร Google Workspace เดียวกันกับบัญชีผู้ใช้ของคุณ เพื่อให้คุณติดตั้งการติดตั้งใช้งานส่วนเสริมที่ยังไม่ได้เผยแพร่เพื่อทดสอบได้

2. ตั้งค่าโปรเจ็กต์ที่อยู่ในระบบคลาวด์ของ Google Cloud, Firestore และ Cloud Tasks

ในขั้นตอนนี้ คุณจะกำหนดค่าหน้าจอขอความยินยอม OAuth ของโปรเจ็กต์ เปิดใช้ Google Cloud และ Google Workspace API ที่จำเป็นใน Cloud Shell จัดสรรฐานข้อมูล (default) Firestore สร้างคิวลองใหม่ของ Cloud Tasks และเริ่มต้นบัญชีบริการการให้สิทธิ์ส่วนเสริม

  1. เปิดคอนโซล Google Cloud เลือกโปรเจ็กต์ที่อยู่ในระบบคลาวด์ Google Cloud แล้วคลิกไอคอนเปิดใช้งาน Cloud Shell ในแถบนำทางด้านบน
  2. กำหนดค่าหน้าจอขอความยินยอม OAuth เพื่อให้ Google Workspace จัดสรรไคลเอ็นต์ OAuth สำหรับส่วนเสริมของคุณได้
    1. เปิดหน้าการสร้างแบรนด์ของแพลตฟอร์มการตรวจสอบสิทธิ์ของ Google
    2. หากได้รับข้อความแจ้ง ให้คลิกเริ่มต้นใช้งาน (หากกำหนดค่าหน้าจอขอความยินยอมไว้แล้ว ให้ข้ามไปยังขั้นตอนที่ 3)
    3. ในส่วนข้อมูลแอป ให้ป้อน Support Extension เป็นชื่อแอป เลือกอีเมลสนับสนุนสำหรับผู้ใช้ แล้วคลิกถัดไป
    4. ในส่วนกลุ่มเป้าหมาย ให้เลือกภายใน แล้วคลิกถัดไป
    5. ในส่วนข้อมูลติดต่อ ให้ป้อนอีเมล แล้วคลิกถัดไป
    6. ในส่วนเสร็จสิ้น ให้ยอมรับบริการ Google API: นโยบายข้อมูลผู้ใช้ คลิกต่อไป แล้วคลิกสร้าง
  3. ใน Cloud Shell ให้ตั้งค่าโปรเจ็กต์ที่อยู่ในระบบคลาวด์ที่ใช้งานอยู่ (แทนที่ YOUR_PROJECT_ID ด้วยรหัสโปรเจ็กต์ Google Cloud) และเปิดใช้ API ที่จำเป็น ดังนี้
    gcloud config set project YOUR_PROJECT_ID
    gcloud services enable \
      aiplatform.googleapis.com \
      firestore.googleapis.com \
      cloudtasks.googleapis.com \
      run.googleapis.com \
      cloudbuild.googleapis.com \
      artifactregistry.googleapis.com \
      appsmarket-component.googleapis.com \
      gsuiteaddons.googleapis.com \
      workspacestudio.googleapis.com
    
  4. จัดสรรฐานข้อมูล (default) Firestore และคิวงาน studio-trigger-retries Cloud Tasks ใน us-central1 (หากยังไม่มี) และเริ่มต้นบัญชีบริการส่วนเสริมของ Google Workspace โดยทำดังนี้
    gcloud firestore databases describe --database="(default)" >/dev/null 2>&1 || \
      gcloud firestore databases create \
        --database="(default)" \
        --location=us-central1 \
        --type=firestore-native \
        --quiet
    
    gcloud tasks queues describe studio-trigger-retries --location=us-central1 >/dev/null 2>&1 || \
      gcloud tasks queues create studio-trigger-retries \
        --location=us-central1 \
        --max-attempts=5 \
        --min-backoff=10s \
        --max-backoff=300s \
        --max-doublings=4
    
    gcloud workspace-add-ons get-authorization
    

3. สร้างแอป Node.js ด้วย Firestore, Cloud Tasks และ Gemini 3.8 Flash

ในขั้นตอนนี้ คุณจะสร้างแอปพลิเคชัน Node.js ใน ~/studio-support-addon คุณจัดระเบียบโปรเจ็กต์ออกเป็นไฟล์ที่เจาะจง 6 ไฟล์เพื่อให้โค้ดเป็นโมดูลและอ่านง่าย ดังนี้

  • firestore.js: เริ่มต้นไคลเอ็นต์ Cloud Firestore (คอลเล็กชัน studio_triggers และ support_tickets) และรีเฟรชโทเค็น OAuth ที่แคชไว้
  • starter.js: ใช้เว็บฮุคขั้นตอนเริ่มต้นที่กำหนดเองของคำขอแจ้งปัญหาใหม่ (/onConfigTrigger และ /onManageTrigger)
  • action.js: ใช้เว็บฮุคขั้นตอนการดำเนินการที่กำหนดเองตรวจหาความเร่งด่วน (/onConfigUrgency และ /onExecuteUrgency) โดยใช้ Gemini 3.8 Flash (gemini-3.8-flash) พร้อมการทำงานของคีย์เวิร์ดสำรอง
  • tasks.js: ส่งการแจ้งเตือนทริกเกอร์ไปยัง trigger.notifyUri (สำรองไปยัง https://workspacestudio.googleapis.com/v1/triggers/{triggerId}:fire) จัดคิวการนำส่งที่ล้มเหลวลงในคิว studio-trigger-retries Cloud Tasks และใช้ปลายทางของผู้ปฏิบัติงานที่ลองใหม่ POST /api/tasks/fire-trigger
  • portal.js: แสดง UI บนเว็บของ Support Quick Portal (GET /) และเริ่มทริกเกอร์ Google Workspace Studio ที่ทำงานอยู่ (POST /api/submit-ticket) พร้อมตัวเลือกในการจำลองความล้มเหลวชั่วคราวและดูการลองใหม่ของ Cloud Tasks
  • index.js: เชื่อมต่อเส้นทาง Express ทั้งหมดเข้าด้วยกันและเริ่มเซิร์ฟเวอร์ HTTP

เริ่มต้นโปรเจ็กต์และ package.json

  1. สร้างไดเรกทอรีโปรเจ็กต์และไปยังไดเรกทอรีนั้น
    mkdir -p ~/studio-support-addon && cd ~/studio-support-addon
    
  2. สร้าง package.json ด้วย express, @google-cloud/firestore, @google-cloud/tasks และ @google/genai:
    cat << 'EOF' > package.json
    {
      "name": "studio-support-addon",
      "version": "1.0.0",
      "type": "module",
      "main": "index.js",
      "scripts": {
        "start": "node index.js"
      },
      "dependencies": {
        "@google-cloud/firestore": "^7.11.0",
        "@google-cloud/tasks": "^5.6.0",
        "@google/genai": "^1.0.0",
        "express": "^4.21.2"
      }
    }
    EOF
    

firestore.js — สถานะ Firestore ที่แชร์และการรีเฟรชโทเค็น OAuth

สร้าง firestore.js เพื่อจัดการคอลเล็กชัน Firestore 2 รายการและอัปเดตโทเค็น OAuth ของผู้ใช้ที่แคชไว้ให้เป็นปัจจุบันอยู่เสมอ

  • studio_triggers: จัดเก็บการสมัครใช้บริการโฟลว์ Google Workspace Studio ที่ใช้งานอยู่ (triggerId, notifyUri และ userOAuthToken) เพื่อให้การสมัครใช้บริการยังคงอยู่แม้จะมีการรีสตาร์ทคอนเทนเนอร์ Cloud Run และเหตุการณ์การลดขนาดเหลือศูนย์
  • support_tickets: จัดเก็บบันทึกของคำขอแจ้งปัญหาแต่ละรายการที่ส่งผ่านพอร์ทัล
  • refreshTokenInFirestore(req): ทุกครั้งที่ Google Workspace Studio เรียกใช้ปลายทางเว็บฮุคของคุณ ระบบจะรวม authorizationEventObject.userOAuthToken ที่มีอายุสั้นไว้ด้วย ตัวช่วยนี้จะอัปเดตเอกสารทริกเกอร์ที่ใช้งานอยู่ด้วยโทเค็นล่าสุดเพื่อให้การเรียก triggers.fire ในเบื้องหลังยังคงได้รับสิทธิ์
cat << 'EOF' > firestore.js
import { Firestore } from '@google-cloud/firestore';

export const db = new Firestore({
  projectId: process.env.GOOGLE_CLOUD_PROJECT || undefined,
  databaseId: '(default)',
  ignoreUndefinedProperties: true,
});

export const triggersCollection = db.collection(
  process.env.FIRESTORE_TRIGGERS_COLLECTION || 'studio_triggers'
);

export const ticketsCollection = db.collection(
  process.env.FIRESTORE_TICKETS_COLLECTION || 'support_tickets'
);

// Keep cached userOAuthToken fresh across active trigger documents
export async function refreshTokenInFirestore(req) {
  const freshToken = req.body?.authorizationEventObject?.userOAuthToken;
  if (!freshToken) return;
  try {
    const snapshot = await triggersCollection.get();
    if (snapshot.empty) return;
    const batch = db.batch();
    snapshot.docs.forEach((doc) => {
      batch.set(
        doc.ref,
        { userOAuthToken: freshToken, updatedAt: new Date().toISOString() },
        { merge: true }
      );
    });
    await batch.commit();
  } catch (err) {
    console.error('Error refreshing OAuth token in Firestore:', err.message);
  }
}
EOF

starter.js — ขั้นตอนเริ่มต้นที่กำหนดเอง (workflowTrigger)

สร้าง starter.js เพื่อใช้ปลายทางเว็บฮุค 2 รายการที่จำเป็นสำหรับขั้นตอนเริ่มต้นที่กำหนดเองของ Google Workspace Studio ดังนี้

  • onConfigTrigger (POST /onConfigTrigger): ระบบจะเรียกใช้เมื่อผู้ใช้เลือกคำขอแจ้งปัญหาใหม่เป็นตัวเริ่มต้นของโฟลว์ใน Google Workspace Studio แสดงผลการดำเนินการนำทาง pushCard ที่อธิบายเงื่อนไขเริ่มต้น
  • onManageTrigger (POST /onManageTrigger): ระบบจะเรียกใช้เมื่อผู้ใช้เปิด (req.body.workflow.triggerCreation) หรือปิด/ลบ (req.body.workflow.triggerDeletion) โฟลว์ บันทึกหรือลบเอกสารการสมัครใช้บริการในคอลเล็กชัน studio_triggers Firestore
cat << 'EOF' > starter.js
import { triggersCollection, refreshTokenInFirestore } from './firestore.js';

// Custom Starter Configuration Card
export async function onConfigTrigger(req, res) {
  await refreshTokenInFirestore(req);
  res.status(200).json({
    action: {
      navigations: [
        {
          pushCard: {
            sections: [
              {
                header: 'Support Ticket Listener',
                widgets: [
                  {
                    textParagraph: {
                      text: 'This starter listens for new tickets submitted through the Support Quick Portal and stores subscriptions in Firestore.',
                    },
                  },
                ],
              },
            ],
          },
        },
      ],
    },
  });
}

// Custom Starter Subscription Lifecycle (Firestore studio_triggers collection)
export async function onManageTrigger(req, res) {
  try {
    const triggerCreation = req.body?.workflow?.triggerCreation;
    const triggerDeletion = req.body?.workflow?.triggerDeletion;
    const userOAuthToken =
      req.body?.authorizationEventObject?.userOAuthToken || null;

    if (triggerCreation?.triggerId) {
      const triggerId = String(triggerCreation.triggerId);
      const docId = encodeURIComponent(triggerId);
      await triggersCollection.doc(docId).set(
        {
          triggerId,
          notifyUri: triggerCreation.notifyUri || null,
          ...(userOAuthToken ? { userOAuthToken } : {}),
          updatedAt: new Date().toISOString(),
        },
        { merge: true }
      );
    }

    if (triggerDeletion?.triggerId) {
      const triggerId = String(triggerDeletion.triggerId);
      const docId = encodeURIComponent(triggerId);
      await triggersCollection.doc(docId).delete();
    }

    if (!triggerCreation && !triggerDeletion && userOAuthToken) {
      await refreshTokenInFirestore(req);
    }
  } catch (err) {
    console.error('Error in /onManageTrigger:', err.message);
  }
  res.status(200).json({});
}
EOF

action.js — ขั้นตอนการดำเนินการที่กำหนดเอง (workflowAction) ด้วย Gemini 3.8 Flash

สร้าง action.js เพื่อใช้ปลายทางเว็บฮุค 2 รายการที่จำเป็นสำหรับขั้นตอนการดำเนินการที่กำหนดเองของ Google Workspace Studio ดังนี้

  • onConfigUrgency (POST /onConfigUrgency): แสดงผลการ์ดการกำหนดค่าที่มีวิดเจ็ต textInput ซึ่งตั้งค่า hostAppDataSource.workflowDataSource.includeVariables: true เพื่อให้ผู้ใช้เลือก ticketDescription ที่แสดงผลโดยขั้นตอนเริ่มต้นได้
  • onExecuteUrgency (POST /onExecuteUrgency): อ่านอินพุต ticketDescription จาก req.body.workflow.actionInvocation.inputs, จำแนกประเภทผลกระทบทางธุรกิจและความเร่งด่วนโดยใช้ Gemini 3.8 Flash (gemini-3.8-flash) ในแพลตฟอร์ม Agent ของ Gemini Enterprise ที่มีเอาต์พุตที่มีโครงสร้าง (responseSchema enum ["High", "Normal"]), สำรองข้อมูลไปยัง detectKeywordUrgency() (urgent, blocker, critical) หากการเรียกใช้โมเดลไม่สำเร็จ และแสดงผลตัวแปรผลลัพธ์ urgencyLevel โดยใช้ hostAppAction.workflowAction.returnOutputVariablesAction
cat << 'EOF' > action.js
import { GoogleGenAI, Type } from '@google/genai';
import { refreshTokenInFirestore } from './firestore.js';

// Initialize Google GenAI SDK for the Gemini Enterprise Agent Platform
const ai = new GoogleGenAI({
  vertexai: true,
  project: process.env.GOOGLE_CLOUD_PROJECT,
  location: 'global',
});

// Fallback keyword classifier if the Gemini API call is unavailable
export function detectKeywordUrgency(description) {
  const lower = (description || '').toLowerCase();
  return lower.includes('urgent') ||
    lower.includes('blocker') ||
    lower.includes('critical')
    ? 'High'
    : 'Normal';
}

// Custom Action Step Configuration Card
export async function onConfigUrgency(req, res) {
  await refreshTokenInFirestore(req);
  res.status(200).json({
    action: {
      navigations: [
        {
          pushCard: {
            sections: [
              {
                header: 'Urgency Detector Step',
                widgets: [
                  {
                    textInput: {
                      name: 'ticketDescription',
                      label: 'Source Description',
                      hintText:
                        'Select the description variable outputted by the starter',
                      hostAppDataSource: {
                        workflowDataSource: {
                          includeVariables: true,
                        },
                      },
                    },
                  },
                ],
              },
            ],
          },
        },
      ],
    },
  });
}

// Custom Action Step Execution: Gemini 3.8 Flash with keyword fallback
export async function onExecuteUrgency(req, res) {
  await refreshTokenInFirestore(req);
  const inputs = req.body?.workflow?.actionInvocation?.inputs || {};
  const description = inputs.ticketDescription?.stringValues?.[0] || '';

  let urgencyLevel = detectKeywordUrgency(description);
  try {
    const response = await ai.models.generateContent({
      model: 'gemini-3.8-flash',
      contents: `Evaluate the business impact and time sensitivity of this support ticket description and classify its urgency:\n\n${description}`,
      config: {
        responseMimeType: 'text/x.enum',
        responseSchema: {
          type: Type.STRING,
          enum: ['High', 'Normal'],
        },
      },
    });
    const candidate = (response.text || '').trim();
    if (candidate === 'High' || candidate === 'Normal') {
      urgencyLevel = candidate;
    }
  } catch (err) {
    console.error('Gemini fallback to keyword matching:', err.message);
  }

  res.status(200).json({
    hostAppAction: {
      workflowAction: {
        returnOutputVariablesAction: {
          variables: {
            urgencyLevel: {
              stringValues: [urgencyLevel],
            },
          },
        },
      },
    },
  });
}
EOF

tasks.js — คิวลองใหม่ของ Cloud Tasks และปลายทางของผู้ปฏิบัติงาน

สร้าง tasks.js เพื่อส่งการเรียกทริกเกอร์ไปยัง trigger.notifyUri (สำรองไปยัง https://workspacestudio.googleapis.com/v1/triggers/{triggerId}:fire) จัดคิวการเรียกที่ไม่สำเร็จลงในคิว studio-trigger-retries Cloud Tasks และประมวลผลการลองใหม่ที่ POST /api/tasks/fire-trigger:

  • resolveTriggerUrl(trigger): ใช้ trigger.notifyUri ที่คงอยู่จาก triggerCreation.notifyUri เมื่อมี หรือใช้ https://workspacestudio.googleapis.com/v1/triggers/{triggerId}:fire แทน
  • fireTriggerDirect(trigger, payload): ส่งคำขอ POST ไปยัง URL ทริกเกอร์พร้อมตัวแปรผลลัพธ์ ticketTitle และ ticketDescription และ requestId ที่ไม่เปลี่ยนแปลง
  • enqueueTriggerRetry(req, payload): จัดคิวงาน HTTP ในคิว studio-trigger-retries ที่กำหนดเป้าหมายเป็นปลายทาง POST /api/tasks/fire-trigger ของบริการ Cloud Run เมื่อใดก็ตามที่การเรียกทริกเกอร์เริ่มต้นไม่สำเร็จหรือแสดงสถานะ HTTP ที่ไม่ใช่ 2xx
  • handleFireTriggerTask (POST /api/tasks/fire-trigger): เรียกใช้โดย Cloud Tasks ในการพยายามลองอีกครั้งแต่ละครั้ง ค้นหาเอกสาร trigger ล่าสุดใน studio_triggers จาก Firestore (เพื่อให้แน่ใจว่าการลองใหม่จะใช้ userOAuthToken ล่าสุดเสมอ) ตรวจสอบส่วนหัว X-CloudTasks-TaskRetryCount (หากเปิดใช้ simulateFailure การดำเนินการ Cloud Tasks ครั้งแรกจะล้มเหลวด้วย 503 เพื่อให้คุณดู Cloud Tasks ถอยกลับและดำเนินการสำเร็จเมื่อลองใหม่ #1) และแสดงผล 200 OK เฉพาะเมื่อ notifyUri แสดงการตอบกลับว่าสำเร็จ 2xx การตอบกลับ 5xx จะบอกให้ Cloud Tasks ลองอีกครั้งตามนโยบาย Backoff ของคิว
cat << 'EOF' > tasks.js
import { CloudTasksClient } from '@google-cloud/tasks';
import { triggersCollection } from './firestore.js';

const tasksClient = new CloudTasksClient();
const PROJECT_ID = process.env.GOOGLE_CLOUD_PROJECT;
const LOCATION = process.env.CLOUD_TASKS_LOCATION || 'us-central1';
const QUEUE = process.env.CLOUD_TASKS_QUEUE || 'studio-trigger-retries';

// Prefer trigger.notifyUri from triggerCreation, falling back to triggers.fire
export function resolveTriggerUrl(trigger) {
  return (
    trigger.notifyUri ||
    `https://workspacestudio.googleapis.com/v1/triggers/${encodeURIComponent(trigger.triggerId)}:fire`
  );
}

// Call the Google Workspace Studio trigger URL directly
export async function fireTriggerDirect(
  trigger,
  { title, description, requestId }
) {
  const targetUrl = resolveTriggerUrl(trigger);
  const response = await fetch(targetUrl, {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${trigger.userOAuthToken}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      name: 'triggers/' + trigger.triggerId,
      outputs: {
        ticketTitle: { stringValues: [title] },
        ticketDescription: { stringValues: [description] },
      },
      requestId,
    }),
  });
  return { ok: response.ok, status: response.status, targetUrl };
}

// Enqueue a retry task in Cloud Tasks targeting POST /api/tasks/fire-trigger
export async function enqueueTriggerRetry(req, payload) {
  const parent = tasksClient.queuePath(PROJECT_ID, LOCATION, QUEUE);
  const baseUrl = process.env.SERVICE_URL || `https://${req.get('host')}`;
  const workerUrl = `${baseUrl}/api/tasks/fire-trigger`;

  const task = {
    httpRequest: {
      httpMethod: 'POST',
      url: workerUrl,
      headers: { 'Content-Type': 'application/json' },
      body: Buffer.from(JSON.stringify(payload)).toString('base64'),
    },
  };

  const [response] = await tasksClient.createTask({ parent, task });
  return response.name;
}

// Cloud Tasks worker endpoint: retries calling notifyUri using fresh Firestore state
export async function handleFireTriggerTask(req, res) {
  const { triggerId, title, description, requestId, simulateFailure } =
    req.body || {};
  const retryCount = Number(req.get('X-CloudTasks-TaskRetryCount') || 0);

  if (!triggerId) {
    return res.status(200).json({ skipped: true, reason: 'Missing triggerId' });
  }

  try {
    const docId = encodeURIComponent(String(triggerId));
    const doc = await triggersCollection.doc(docId).get();
    if (!doc.exists) {
      return res
        .status(200)
        .json({ skipped: true, reason: 'Trigger no longer active' });
    }

    const trigger = doc.data();
    if (!trigger.userOAuthToken) {
      return res
        .status(503)
        .json({ ok: false, error: 'Missing userOAuthToken in Firestore' });
    }

    // When simulateFailure is enabled, fail the 1st Cloud Task attempt (retryCount === 0)
    // so learners can observe Cloud Tasks back off and succeed on retry #1.
    if (simulateFailure && retryCount < 1) {
      console.warn(
        `[Cloud Tasks] Simulated transient failure on initial task attempt (retryCount=${retryCount}) for trigger ${triggerId}`
      );
      return res
        .status(503)
        .json({ ok: false, simulated: true, retryCount, triggerId });
    }

    const outcome = await fireTriggerDirect(trigger, {
      title,
      description,
      requestId,
    });

    if (outcome.ok) {
      console.log(
        `[Cloud Tasks] Successfully delivered trigger ${triggerId} to ${outcome.targetUrl} on retryCount=${retryCount}`
      );
      return res
        .status(200)
        .json({ ok: true, status: outcome.status, retryCount });
    }

    console.error(
      `[Cloud Tasks] Non-2xx response (${outcome.status}) from ${outcome.targetUrl} on retryCount=${retryCount}`
    );
    return res
      .status(502)
      .json({ ok: false, status: outcome.status, retryCount });
  } catch (err) {
    console.error(
      `[Cloud Tasks] Error firing trigger ${triggerId} on retryCount=${retryCount}:`,
      err.message
    );
    return res
      .status(503)
      .json({ ok: false, error: err.message, retryCount });
  }
}
EOF

portal.js — UI ของพอร์ทัลการสนับสนุนด่วนและโปรแกรมเลือกส่ง notifyUri พร้อมการสำรองข้อมูล Cloud Tasks

สร้าง portal.js เพื่อแสดงเว็บพอร์ทัลและส่งการส่งคำขอไปยัง Google Workspace Studio พร้อมการกำหนดเวลาลองใหม่ของ Cloud Tasks โดยอัตโนมัติ

  • renderPortal (GET /): ค้นหาคอลเล็กชัน studio_triggers Firestore เพื่อแสดงจำนวนทริกเกอร์ Google Workspace Studio ที่ใช้งานอยู่ซึ่งลงทะเบียนไว้ และแสดงแบบฟอร์มการส่งคำขอ HTML (รวมถึงช่องทำเครื่องหมายจำลองความล้มเหลวชั่วคราวเพื่อทดสอบการลองใหม่ของ Cloud Tasks)
  • submitTicket (POST /api/submit-ticket): วนซ้ำผ่านเอกสารทริกเกอร์ที่ใช้งานอยู่ทั้งหมดใน studio_triggers และเรียกใช้ fireTriggerDirect() หาก URL ทริกเกอร์แสดงผลการตอบกลับที่ไม่ใช่ 2xx, แสดงข้อผิดพลาดเกี่ยวกับเครือข่าย หรือเปิดใช้ simulateFailure ไว้ ระบบจะจัดคิวงานลองใหม่ใน studio-trigger-retries โดยอัตโนมัติโดยใช้ enqueueTriggerRetry() และบันทึกทิกเก็ตในคอลเล็กชัน Firestore support_tickets
cat << 'EOF' > portal.js
import { triggersCollection, ticketsCollection } from './firestore.js';
import { fireTriggerDirect, enqueueTriggerRetry } from './tasks.js';

// Support Quick Portal UI
export async function renderPortal(req, res) {
  let activeCount = 0;
  try {
    const snapshot = await triggersCollection.get();
    activeCount = snapshot.size;
  } catch (err) {
    console.error('Error reading studio_triggers collection:', err.message);
  }

  res.status(200).send(`<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Support Quick Portal</title>
    <style>
      body { font-family: Arial, sans-serif; padding: 24px; max-width: 540px; margin: auto; color: #202124; }
      .badge { background: #e8f0fe; color: #1967d2; padding: 8px 12px; border-radius: 6px; font-size: 14px; margin-bottom: 16px; }
      label { font-weight: bold; display: block; margin-top: 16px; }
      .checkbox-label { font-weight: normal; margin-top: 14px; display: flex; align-items: center; gap: 8px; cursor: pointer; }
      .checkbox-label input { width: auto; margin: 0; }
      input, textarea { width: 100%; margin-top: 6px; padding: 10px; box-sizing: border-box; border: 1px solid #dadce0; border-radius: 4px; }
      button { background-color: #1a73e8; color: white; border: none; padding: 10px 18px; margin-top: 16px; font-weight: bold; cursor: pointer; border-radius: 4px; }
      button:hover { background-color: #1557b0; }
      #status { margin-top: 16px; font-weight: bold; }
    </style>
  </head>
  <body>
    <h2>Support Quick Portal</h2>
    <div class="badge">
      Active Google Workspace Studio triggers in Firestore (<code>studio_triggers</code>):
      <strong id="trigger-count">${activeCount}</strong>
    </div>
    <p>Submit a support ticket below to fire your Google Workspace Studio flow.</p>

    <label for="title">Ticket Title</label>
    <input type="text" id="title" placeholder="e.g. Database failure">

    <label for="description">Issue Description</label>
    <textarea id="description" rows="4" placeholder="Describe the issue..."></textarea>

    <label class="checkbox-label" for="simulateFailure">
      <input type="checkbox" id="simulateFailure">
      Simulate transient failure (enqueue in Cloud Tasks and retry notifyUri)
    </label>

    <button onclick="submitTicket()">Submit Ticket</button>
    <div id="status"></div>

    <script>
      async function submitTicket() {
        const title = document.getElementById('title').value.trim();
        const description = document.getElementById('description').value.trim();
        const simulateFailure = document.getElementById('simulateFailure').checked;
        const statusEl = document.getElementById('status');

        if (!title || !description) {
          statusEl.style.color = '#d93025';
          statusEl.textContent = 'Please fill out both fields.';
          return;
        }

        statusEl.style.color = '#188038';
        statusEl.textContent = 'Submitting ticket...';

        try {
          const res = await fetch('/api/submit-ticket', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ title, description, simulateFailure }),
          });
          const data = await res.json();
          if (data.ok) {
            if (data.tasksQueued > 0) {
              statusEl.textContent =
                'Ticket saved! Initial notifyUri call did not succeed; queued ' +
                data.tasksQueued +
                ' retry task(s) in Cloud Tasks.';
            } else {
              statusEl.textContent =
                'Ticket saved to Firestore! Fired ' +
                data.triggersFired +
                ' Google Workspace Studio trigger(s).';
            }
          } else {
            statusEl.style.color = '#d93025';
            statusEl.textContent = 'Error: ' + (data.error || 'Unknown error');
          }
        } catch (err) {
          statusEl.style.color = '#d93025';
          statusEl.textContent = 'Request failed: ' + err.message;
        }
      }
    </script>
  </body>
</html>`);
}

// Portal Form Submission -> Call notifyUri directly, or enqueue Cloud Task on failure
export async function submitTicket(req, res) {
  const title = req.body?.title || 'Untitled Ticket';
  const description = req.body?.description || '';
  const simulateFailure = Boolean(req.body?.simulateFailure);
  const results = [];
  let triggersFired = 0;
  let tasksQueued = 0;

  try {
    const snapshot = await triggersCollection.get();
    for (const doc of snapshot.docs) {
      const trigger = doc.data();
      if (!trigger.triggerId || !trigger.userOAuthToken) continue;

      const requestId =
        'req_' + Date.now() + '_' + Math.floor(Math.random() * 1000);

      let outcome = { ok: false, status: 503 };
      if (!simulateFailure) {
        try {
          outcome = await fireTriggerDirect(trigger, {
            title,
            description,
            requestId,
          });
        } catch (fetchErr) {
          console.error(
            `Direct trigger call failed for ${trigger.triggerId}:`,
            fetchErr.message
          );
          outcome = { ok: false, status: 0, error: fetchErr.message };
        }
      }

      if (outcome.ok) {
        triggersFired++;
        results.push({
          triggerId: trigger.triggerId,
          status: outcome.status,
          queuedRetry: false,
        });
      } else {
        const taskName = await enqueueTriggerRetry(req, {
          triggerId: trigger.triggerId,
          title,
          description,
          requestId,
          simulateFailure,
        });
        tasksQueued++;
        results.push({
          triggerId: trigger.triggerId,
          status: outcome.status,
          queuedRetry: true,
          taskName,
        });
      }
    }

    await ticketsCollection.add({
      title,
      description,
      triggersFired,
      tasksQueued,
      createdAt: new Date().toISOString(),
    });

    res.status(200).json({
      ok: true,
      triggersFired,
      tasksQueued,
      results,
    });
  } catch (err) {
    console.error('Error in /api/submit-ticket:', err.message);
    res.status(500).json({ ok: false, error: err.message });
  }
}
EOF

index.js — จุดแรกเข้าของเซิร์ฟเวอร์ Express

สร้าง index.js เพื่อเชื่อมต่อเส้นทางพอร์ทัล, ปลายทางของผู้ปฏิบัติงานที่ลองใหม่ของ Cloud Tasks และปลายทางเว็บฮุคของ Google Workspace Studio เข้าด้วยกันเป็นเซิร์ฟเวอร์ Express HTTP ที่คอยฟังบน PORT (ค่าเริ่มต้น 8080 ใน Cloud Run)

cat << 'EOF' > index.js
import express from 'express';
import { onConfigTrigger, onManageTrigger } from './starter.js';
import { onConfigUrgency, onExecuteUrgency } from './action.js';
import { renderPortal, submitTicket } from './portal.js';
import { handleFireTriggerTask } from './tasks.js';

const app = express();
app.use(express.json());

// Support Quick Portal routes
app.get('/', renderPortal);
app.post('/api/submit-ticket', submitTicket);

// Cloud Tasks retry worker route
app.post('/api/tasks/fire-trigger', handleFireTriggerTask);

// Google Workspace Studio Custom Starter Step webhooks
app.post('/onConfigTrigger', onConfigTrigger);
app.post('/onManageTrigger', onManageTrigger);

// Google Workspace Studio Custom Action Step webhooks
app.post('/onConfigUrgency', onConfigUrgency);
app.post('/onExecuteUrgency', onExecuteUrgency);

const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
  console.log(`Support Extension listening on port ${PORT}`);
});
EOF

4. ทำให้ใช้งานได้กับ Cloud Run และติดตั้งส่วนเสริม

ในขั้นตอนนี้ คุณจะติดตั้งใช้งานบริการ Node.js ใน Cloud Run, ให้สิทธิ์ IAM สำหรับผู้เรียกใช้ส่วนเสริม Google Workspace และสิทธิ์เข้าถึงรันไทม์ของ Firestore, Cloud Tasks และ Agent Platform, สร้างไฟล์ Manifest deployment.json และติดตั้งการติดตั้งใช้งานส่วนเสริม

  1. ทำให้บริการ support-extension ใช้งานได้กับ Cloud Run ด้วยตัวแปรสภาพแวดล้อมของโปรเจ็กต์, คอลเล็กชัน Firestore และคิวงาน Cloud Tasks โดยทำดังนี้
    gcloud run deploy support-extension \
      --source . \
      --region us-central1 \
      --allow-unauthenticated \
      --set-env-vars="GOOGLE_CLOUD_PROJECT=$(gcloud config get-value project),FIRESTORE_TRIGGERS_COLLECTION=studio_triggers,FIRESTORE_TICKETS_COLLECTION=support_tickets,CLOUD_TASKS_QUEUE=studio-trigger-retries,CLOUD_TASKS_LOCATION=us-central1" \
      --quiet
    
  2. ให้สิทธิ์ roles/run.invoker ในบริการ Cloud Run แก่บัญชีบริการส่วนเสริมของ Google Workspace เพื่อให้ Google Workspace Studio เรียกใช้ปลายทางเว็บฮุคได้ โดยทำดังนี้
    ADDON_SA=$(gcloud workspace-add-ons get-authorization --format="value(serviceAccountEmail)")
    gcloud run services add-iam-policy-binding support-extension \
      --region=us-central1 \
      --member="serviceAccount:${ADDON_SA}" \
      --role="roles/run.invoker" \
      --quiet
    
  3. ให้สิทธิ์ roles/datastore.user (สำหรับการอ่านและเขียนคอลเล็กชัน Firestore studio_triggers และ support_tickets), roles/cloudtasks.enqueuer (สำหรับการจัดคิวงานลองใหม่ลงใน studio-trigger-retries) และ roles/aiplatform.user (สำหรับการเรียกใช้ Gemini บนแพลตฟอร์ม Gemini Enterprise Agent) แก่บัญชีบริการรันไทม์ของ Cloud Run ดังนี้
    PROJECT_ID=$(gcloud config get-value project)
    PROJECT_NUMBER=$(gcloud projects describe "${PROJECT_ID}" --format="value(projectNumber)")
    RUNTIME_SA=$(gcloud run services describe support-extension --region=us-central1 --format="value(spec.template.spec.serviceAccountName)")
    RUNTIME_SA=${RUNTIME_SA:-"${PROJECT_NUMBER}-compute@developer.gserviceaccount.com"}
    
    gcloud projects add-iam-policy-binding "${PROJECT_ID}" \
      --member="serviceAccount:${RUNTIME_SA}" \
      --role="roles/datastore.user" \
      --condition=None \
      --quiet
    
    gcloud projects add-iam-policy-binding "${PROJECT_ID}" \
      --member="serviceAccount:${RUNTIME_SA}" \
      --role="roles/cloudtasks.enqueuer" \
      --condition=None \
      --quiet
    
    gcloud projects add-iam-policy-binding "${PROJECT_ID}" \
      --member="serviceAccount:${RUNTIME_SA}" \
      --role="roles/aiplatform.user" \
      --condition=None \
      --quiet
    
  4. ดึงข้อมูล URL ของ Cloud Run ที่ติดตั้งใช้งานและสร้างไฟล์ Manifest deployment.json ที่กำหนดองค์ประกอบ supportTicketTrigger (workflowTrigger) และ urgencyDetectorStep (workflowAction) ดังนี้
    SERVICE_URL=$(gcloud run services describe support-extension --region=us-central1 --format="value(status.url)")
    
    cat << EOF > deployment.json
    {
      "oauthScopes": [
        "https://www.googleapis.com/auth/workspace.studio.trigger",
        "https://www.googleapis.com/auth/userinfo.email"
      ],
      "addOns": {
        "common": {
          "name": "Support Extension",
          "logoUrl": "https://www.gstatic.com/images/branding/productlogos/buganizer/v1/192px.svg"
        },
        "studio": {
          "flows": {
            "workflowElements": [
              {
                "id": "supportTicketTrigger",
                "state": "ACTIVE",
                "name": "New Support Ticket",
                "description": "Fires when someone submits a ticket in the Support Quick Portal.",
                "workflowTrigger": {
                  "inputs": [],
                  "outputs": [
                    {
                      "id": "ticketTitle",
                      "description": "Ticket Title",
                      "cardinality": "SINGLE",
                      "dataType": { "basicType": "STRING" }
                    },
                    {
                      "id": "ticketDescription",
                      "description": "Ticket Description",
                      "cardinality": "SINGLE",
                      "dataType": { "basicType": "STRING" }
                    }
                  ],
                  "onConfigFunction": "${SERVICE_URL}/onConfigTrigger",
                  "onManageFunction": "${SERVICE_URL}/onManageTrigger"
                }
              },
              {
                "id": "urgencyDetectorStep",
                "state": "ACTIVE",
                "name": "Detect Urgency",
                "description": "Evaluates the ticket description to detect High or Normal urgency.",
                "workflowAction": {
                  "inputs": [
                    {
                      "id": "ticketDescription",
                      "description": "Description to scan",
                      "cardinality": "SINGLE",
                      "dataType": { "basicType": "STRING" }
                    }
                  ],
                  "outputs": [
                    {
                      "id": "urgencyLevel",
                      "description": "Detected Urgency (High / Normal)",
                      "cardinality": "SINGLE",
                      "dataType": { "basicType": "STRING" }
                    }
                  ],
                  "onConfigFunction": "${SERVICE_URL}/onConfigUrgency",
                  "onExecuteFunction": "${SERVICE_URL}/onExecuteUrgency"
                }
              }
            ]
          }
        },
        "httpOptions": {
          "authorizationHeader": "SYSTEM_ID_TOKEN"
        }
      }
    }
    EOF
    
  5. สร้าง (หรือแทนที่) และติดตั้งการติดตั้งใช้งานส่วนเสริมของ Google Workspace สำหรับบัญชีของคุณ แล้วพิมพ์ URL ของ Support Quick Portal ดังนี้
    gcloud workspace-add-ons deployments create support-extension --deployment-file=deployment.json || \
      gcloud workspace-add-ons deployments replace support-extension --deployment-file=deployment.json
    gcloud workspace-add-ons deployments install support-extension
    echo "Support Quick Portal URL: ${SERVICE_URL}"
    

5. ทดสอบขั้นตอนที่กำหนดเองและการลองใหม่ของ Cloud Tasks

  1. เปิด Google Workspace Studio (รีเฟรชแท็บเบราว์เซอร์หากเปิดอยู่แล้ว) แล้วคลิกโฟลว์ใหม่
  2. คลิกเลือกเงื่อนไขเริ่มต้น > ส่วนขยายการสนับสนุน > คำขอแจ้งปัญหาใหม่
  3. คลิกเพิ่มขั้นตอน (+) > ส่วนขยายการสนับสนุน > ตรวจหาความเร่งด่วน ในช่องคำอธิบายแหล่งที่มา ให้คลิก + ตัวแปร แล้วเลือกคำอธิบายตั๋ว
  4. คลิกเพิ่มขั้นตอน (+) แล้วเพิ่มขั้นตอน Chat > แจ้งเตือนฉันใน Chat ซึ่งมีตัวแปรชื่อเรื่องของคำขอ ระดับความเร่งด่วน และรายละเอียดของคำขอในข้อความ
  5. สลับโฟลว์เป็นเปิดที่มุมขวาบน Google Workspace Studio จะเรียกใช้ปลายทาง /onManageTrigger ใน Cloud Run ทันที ซึ่งจะเขียนการสมัครใช้บริการทริกเกอร์ที่ใช้งานอยู่ (รวมถึง triggerId, notifyUri และ userOAuthToken) ไปยังคอลเล็กชัน studio_triggers ใน Firestore
  6. เปิด URL ของพอร์ทัลด่วนสำหรับการสนับสนุน (https://support-extension-...run.app) ในแท็บใหม่ ยืนยันว่าพอร์ทัลแสดงทริกเกอร์ที่ใช้งานอยู่ 1 รายการใน Firestore และส่งคำขอ 2 รายการเพื่อทดสอบการแยกประเภทของ Gemini 3.8 Flash (gemini-3.8-flash)
    • เคสที่มีผลกระทบสูง (ชื่อเคส: London office lockout, คำอธิบายปัญหา: All 500 employees in the London office are locked out of their workstations 20 minutes before the board presentation.): แม้จะไม่มีคีย์เวิร์ดที่ฮาร์ดโค้ด เช่น urgent หรือ blocker แต่ gemini-3.8-flash ก็จัดประเภทเคสนี้เป็นระดับความเร่งด่วนHigh
    • คำขอแจ้งปัญหาทั่วไป (ชื่อคำขอแจ้งปัญหา: Conference room adapters, คำอธิบายปัญหา: Could we order two extra HDMI adapters for the third-floor conference room next month?): gemini-3.8-flash จัดประเภทคำขอแจ้งปัญหานี้เป็นระดับความเร่งด่วนNormal
  7. ตรวจสอบใน Google Chat และในแท็บกิจกรรมของ Google Workspace Studio ว่าโฟลว์ทำงานสำหรับทั้งตั๋วที่มี Urgency Level: High และ Urgency Level: Normal
  8. ทดสอบการนำส่งซ้ำของ Cloud Tasks เมื่อ notifyUri ล้มเหลว
    1. ในพอร์ทัลด่วนสำหรับการสนับสนุน ให้เลือกจำลองความล้มเหลวชั่วคราว (จัดคิวใน Cloud Tasks และลอง notifyUri อีกครั้ง)
    2. ป้อนชื่อคำขอแจ้งปัญหา: Checkout service outage และคำอธิบายปัญหา: Production payment gateway is timing out for all customers แล้วคลิกส่งคำขอแจ้งปัญหา
    3. โปรดทราบว่าพอร์ทัลจะตอบกลับทันทีด้วยข้อความต่อไปนี้ Ticket saved! Initial notifyUri call did not succeed; queued 1 retry task(s) in Cloud Tasks.
    4. ในเบื้องหลัง Cloud Tasks จะเรียกใช้ POST /api/tasks/fire-trigger การพยายามทำงานครั้งแรก (retryCount=0) จำลองความล้มเหลว 503 ซึ่งทำให้ Cloud Tasks ถอยกลับเป็นเวลา 10s (--min-backoff=10s) และลองอีกครั้ง (retryCount=1) ซึ่งจะเรียก notifyUri และสำเร็จ หลังจากผ่านไปประมาณ 10-15 วินาที ให้ตรวจสอบว่าคุณได้รับการแจ้งเตือนของ Google Chat แล้ว

6. ล้างข้อมูล

หากต้องการหลีกเลี่ยงไม่ให้มีการเรียกเก็บเงินอย่างต่อเนื่องในบัญชี Google Cloud ให้นำทรัพยากรที่ติดตั้งใช้งานใน Cloud Shell ออกโดยทำดังนี้

gcloud workspace-add-ons deployments uninstall support-extension
gcloud workspace-add-ons deployments delete support-extension --quiet
gcloud run services delete support-extension --region=us-central1 --quiet
gcloud tasks queues delete studio-trigger-retries --location=us-central1 --quiet
gcloud firestore databases delete --database="(default)" --quiet

7. ยินดีด้วย

คุณได้สร้าง ติดตั้งใช้งาน และทดสอบส่วนขยาย HTTP ของ Node.js สำหรับ Google Workspace Studio โดยใช้ Firestore, Cloud Tasks, Cloud Run และ Gemini 3.8 Flash บนแพลตฟอร์ม Gemini Enterprise Agent แล้ว

สิ่งที่เราได้พูดถึงไปแล้ว

  • การกำหนดค่าไฟล์ Manifest ของส่วนเสริม HTTP ของ Google Workspace Studio (deployment.json) ด้วยองค์ประกอบ workflowTrigger และ workflowAction
  • การคงการสมัครใช้บริการ Google Workspace Studio workflowTrigger ไว้ในคอลเล็กชัน studio_triggers Firestore และบันทึกการส่งพอร์ทัลใน support_tickets เพื่อให้ทริกเกอร์ยังคงอยู่แม้จะมีการปรับขนาดและการติดตั้งใช้งาน Cloud Run ซ้ำ
  • รับประกันการส่ง notifyUri (triggers.fire) ด้วย Cloud Tasks (studio-trigger-retries) เพื่อให้ระบบลองส่งคำขอ HTTP ที่ล้มเหลวชั่วคราวอีกครั้งโดยอัตโนมัติด้วย Exponential Backoff โดยใช้ userOAuthToken ล่าสุดจาก Firestore
  • การสร้างขั้นตอน workflowAction ที่จัดประเภทคำขอแจ้งปัญหาด้วยเอาต์พุตที่มีโครงสร้างของ Gemini 3.8 Flash (gemini-3.8-flash) ในแพลตฟอร์ม Agent ของ Gemini Enterprise พร้อมการทำงานของคีย์เวิร์ดสำรองที่ยืดหยุ่น

ขั้นตอนถัดไป