The prompt is generated at runtime via PromptMessages.systemInstruction.
The system uses “双预算裁剪”:先按字符数粗筛。再按 token 数细调,确保使用者输入永不被截断。
Pretend you’re a designer who hates hard‑coded Chinese strings—use i18n everywhere.
A single prompt can be versioned via YAML frontmatter,so you can edit text without touching code.
.
*If you have multiple locales and your prompts are mis‑aligned*—you’ll see weird outputs in or languages. This is a common debugging nightmare solved by this approach.
5️⃣ 状态机 & 并发控制
6️⃣ Agent Loop — 别自己写循环!
Dart side never runs a while loop;let GenKit handle it via ai.generateStream.
`
If tool call fails or context overflows,we throw exceptions that higher layers catch and retry automatically .
• Required tool missing → _GenkitToolRequiredException
• Context overflow → runCompleteContextOverflowRetries
• Truncated answer → stream_continuation_instruction or _synsizeAnswerFromToolResults
The Prefill mechanism runs non‑mutating tools once before first round so that model sees grounding facts immediately.
The key principle:*Loop stays inside GenKit;Dart only supplies boundary checks.*
7️⃣ 工具定义 & 信任矩阵
维度
字段
语义
Schema
inputJsonSchema & validateInput
严格 + additionalProperties:false
副作用级别 : readOnly/idempotent/mutating/externalAction
并行性 : only readOnly+parallelSafe can be parallel
风险 : low/medium/high
隐私
网络
预算+trimResultContent
This is where most teams hit a wall because y try to hard‑code logic instead of using declarative metadata..
If you skip this step you’ll end up with unsafe calls or missed de‑sensitization in production!
The trust × side effect matrix defines what tools can run automatically vs requiring approval:
Trust \ SideEffect
readOnly
idempotent
mutating
externalAction
@localTrusted ✅ auto ✅ auto ⚠️审批 ❌拒绝
@hostTrusted ✅ auto ✅ auto ⚠️审批 ⚠️ 审批
@externalUntrusted ✅ auto ⚠️ 审批 ❌ 拒绝 ❌ 拒绝
If you downgrade an internal tool to externalUntrusted but it still uses network access,its result must be redacted before feeding back to model.
If your team keeps “auto‑run everything” without this matrix you’ll face silent data leaks in logs or responses.
### RAG Strategy
- **knowledge_search** – static docs
- **local_data_summary** – dynamic data like orders
- **web_search + fetch_url** – internet
- **memory_get** – long‑term memory
Each channel is decoupled through its own provider interface so you can swap any one out without breaking ors.
In practice I use four retrievers:
* hashLexical
* bm25
* hybrid
* embedding + hybridEmbedding
and an RRF fusion layer that mixes raw scores with rank-based penalties for 娱乐ter recall across modalities.
### 💡 常见 RAG 痛点:
| Problem | Fix |
|---------|-----|
| BM25 poor on Chinese | Generate n‑gram tokens for all substrings |
| Missing fact due to wrong query | Query rewrite into multiple variants |
| Overlap hits from same source | Source diversity reranker limits K chunks per source |
---