Hybrid Knowledge Ingestion Workflow (Google Docs/Sheets + Local Documents)
SharedOperations959 words5 min read
Hybrid Knowledge Ingestion Workflow (Google Docs/Sheets Private)
Phần tiêu đề “Hybrid Knowledge Ingestion Workflow (Google Docs/Sheets Private)”Operational reference only.
Knowledge Base content remains under docs/Shared/Knowledge Base/.
Nếu bạn chỉ cần dùng repo docs hoặc sửa docs canonical bình thường, không cần đọc file này.
Ở file này:
ingest= kéo nội dung từ nguồn private vào repo theo pipeline đồng bộmirror= bản copy markdown bám theo nguồn gốc, chưa phải lúc nào cũng là bản canonical cuốistaging= vùng trung gian để review trước khi promotepromote= đưa phần đã review vào docs canonical
Mục tiêu
Phần tiêu đề “Mục tiêu”Xây cơ chế thu thập tài liệu private từ Google Docs/Sheets, chuẩn hóa nội dung, phát hiện liên kết chéo và đưa về Knowledge Base dưới dạng cấu trúc logic hơn.
Nguyên tắc bảo mật
Phần tiêu đề “Nguyên tắc bảo mật”- File nguồn giữ private/local, không cần public.
- Cấp quyền tối thiểu:
documents.readonlyspreadsheets.readonlydrive.metadata.readonly
- Không commit token/key vào git.
- Mọi output đều giữ traceability về nguồn (
source_id,source_url,auth_mode,fetched_at).
Mô hình Hybrid
Phần tiêu đề “Mô hình Hybrid”oauth_access_token:- Dùng cho cá nhân biên tập (đọc đúng quyền user).
- Token đọc từ env
GOOGLE_OAUTH_ACCESS_TOKEN.
oauth_refresh_token(khuyến nghị):- Dùng quyền account thật của bạn, không cần share từng file.
- Token có thể tự refresh để chạy sync lâu dài.
- Đọc từ
oauthCredentialsFile(ví dụ.secrets/google-oauth-refresh-token.json).
service_account:- Dùng cho job automation ổn định.
- Share file/folder cho email service account.
- Key đọc từ
GOOGLE_SERVICE_ACCOUNT_KEY_FILEhoặcGOOGLE_SERVICE_ACCOUNT_JSON.
Lấy OAuth access token nhanh (local, ngắn hạn)
Phần tiêu đề “Lấy OAuth access token nhanh (local, ngắn hạn)”export GOOGLE_OAUTH_ACCESS_TOKEN=\"$(gcloud auth print-access-token)\"Lưu ý: token này ngắn hạn, phù hợp để chạy local/manual ingest.
Cách 2: OAuth refresh token (khuyến nghị)
Phần tiêu đề “Cách 2: OAuth refresh token (khuyến nghị)”Bước 1: tạo OAuth client (1 lần)
Phần tiêu đề “Bước 1: tạo OAuth client (1 lần)”- Google Cloud Console -> APIs & Services -> Credentials -> Create Credentials -> OAuth client ID.
- Loại client:
Desktop app. - Tải file JSON client và lưu vào:
.secrets/google-oauth-client.json
Bước 2: bootstrap refresh token (1 lần)
Phần tiêu đề “Bước 2: bootstrap refresh token (1 lần)”npm run kb:oauth:setup- Lệnh in URL xác thực.
- Mở URL, approve, rồi copy redirect URL cuối cùng.
- Exchange code:
node scripts/kb-oauth-setup.mjs --redirect-url "<PASTE_REDIRECT_URL>"- Kết quả:
.secrets/google-oauth-refresh-token.json
Bước 3: cấu hình ingest
Phần tiêu đề “Bước 3: cấu hình ingest”authMode:oauth_refresh_tokenoauthCredentialsFile:.secrets/google-oauth-refresh-token.json
Cấu hình nguồn
Phần tiêu đề “Cấu hình nguồn”- Copy file mẫu:
config/knowledge-ingest/sources.example.json
- Tạo local config:
config/knowledge-ingest/sources.local.json
- Khai báo từng nguồn:
kind:gdochoặcgsheetid: Google file ID hoặc full Google URLauthMode:oauth_refresh_token,oauth_access_token, hoặcservice_accountoutputMode: dùngcloneđể mirror thuần nội dungtargetPath: file đích cố định trong repo (để sync ghi đè cùng 1 file)
Lệnh ingest
Phần tiêu đề “Lệnh ingest”# thêm nhanh 1 source từ Google URL (auto gdoc/gsheet + clone targetPath)node scripts/kb-add-source.mjs --url "<GOOGLE_URL>"
# thêm hàng loạt từ 1 Drive folder (auto Docs/Sheets), target vào subfolder theo areanode scripts/kb-add-folder-sources.mjs --folder "<DRIVE_FOLDER_URL>" --target-subdir "Teaching tool"
# dry runnode scripts/kb-ingest.mjs --config config/knowledge-ingest/sources.local.json --dry-run
# ingest toàn bộ sourcenode scripts/kb-ingest.mjs --config config/knowledge-ingest/sources.local.json
# guard naming/layout cho Imported stagingnpm run kb:guard:imported
# ingest theo source key/idnode scripts/kb-ingest.mjs --config config/knowledge-ingest/sources.local.json --only product-discovery-master-docLocal Office/PDF convert
Phần tiêu đề “Local Office/PDF convert”Lane này dùng cho file đã nằm trong repo/local Drive, ví dụ .pptx, .pdf, .docx, .xlsx, .xls.
# scan local documents without writing markdownnpm run kb:local:scan
# convert all detected local documentsnpm run kb:local:convert
# convert a focused folder/filenpm run kb:local:convert -- --root "docs/Command/Resources/Documents/Exercise with AI 2026/Speaking" --only "Brief - Speaking LMS New.pptx"Contract:
- Source files stay in
docs/Command/Resources/Documents/**. - Converted Markdown is written beside each source file under a sibling
_converted/folder. - Filename preserves the source filename and appends
.convert.md(example:Brief - Speaking LMS New.pptx.convert.md). - Path/content hashes stay in frontmatter as
source_path_hashandsource_sha256. - For
.pptx,--keep-data-urisis enabled by config so slide images remain self-contained.
Output
Phần tiêu đề “Output”- Markdown staging:
docs/Shared/Knowledge Base/Imported/NB_IMP_*.md
- Local converted Markdown:
docs/Command/Resources/Documents/**/_converted/*.convert.md
- Index tổng hợp:
- Raw normalized cache:
.cache/kb-ingest/raw/<source-id>.json
- Run summary:
.cache/kb-ingest/last-run.json
Chu trình review đề xuất
Phần tiêu đề “Chu trình review đề xuất”- Ingest vào
Imported/(staging). - Reviewer kiểm tra:
- category đúng chưa
- missing context
- link chéo có hợp lệ
- Promote nội dung đã duyệt sang khu vực chính trong
docs/Shared/Knowledge Base/.
Mirror 1-1 (khuyến nghị cho nhu cầu đồng bộ)
Phần tiêu đề “Mirror 1-1 (khuyến nghị cho nhu cầu đồng bộ)”- Mỗi source gán
targetPathcố định. - Chạy lại ingest sẽ ghi đè file đó, tạo hiệu ứng đồng bộ 2 chiều theo thời gian (Google -> repo).
- Với Google Docs private, nội dung mirror là markdown đã convert từ cấu trúc Doc, không thêm phần tóm tắt nếu
outputMode=clone. - Naming chuẩn cho imported mirror:
NB_IMP_<TitleToken>_<ShortId>.md- ví dụ:
NB_IMP_Linearthinking_17ab81FwfH.md
- Folder import behavior:
kb-add-folder-sourcesquét đệ quy toàn bộ subfolder.- Chỉ add các file Google Docs/Sheets (hoặc shortcut trỏ đến Docs/Sheets).
- Nếu báo
Cannot access folder ..., cần cấp quyền folder cho đúng account đã cấp OAuth refresh token.
Rủi ro hiện tại (PoC)
Phần tiêu đề “Rủi ro hiện tại (PoC)”- Heuristic phân loại nội dung vẫn đơn giản.
- Chưa có dedup semantic giữa các source trùng nội dung.
Nâng cấp nên làm tiếp
Phần tiêu đề “Nâng cấp nên làm tiếp”- Thêm resolver tự follow link Google để crawl depth=1..2.
- Thêm semantic merge + confidence score trước khi ghi markdown cuối.
- Bổ sung CI check cho schema config ingest.