@rune-kit/rune 2.2.0 → 2.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +72 -40
- package/compiler/__tests__/pack-split.test.js +145 -0
- package/compiler/bin/rune.js +26 -9
- package/compiler/doctor.js +42 -0
- package/compiler/emitter.js +27 -4
- package/compiler/parser.js +41 -3
- package/compiler/transformer.js +10 -6
- package/compiler/transforms/compliance.js +40 -0
- package/docs/ANTIGRAVITY-GAP-ANALYSIS.md +369 -0
- package/docs/ARCHITECTURE.md +332 -0
- package/docs/COMMUNITY-PACKS.md +109 -0
- package/docs/CONTRIBUTING-L4.md +215 -0
- package/docs/CROSS-IDE-ANALYSIS.md +164 -0
- package/docs/EXTENSION-TEMPLATE.md +108 -0
- package/docs/MESH-RULES.md +34 -0
- package/docs/MULTI-PLATFORM.md +804 -0
- package/docs/SKILL-DEPTH-AUDIT.md +191 -0
- package/docs/SKILL-TEMPLATE.md +72 -0
- package/docs/TRADE-MATRIX.md +327 -0
- package/docs/VERSIONING.md +91 -0
- package/docs/VISION.md +263 -0
- package/docs/assets/demo-subtitles.srt +215 -0
- package/docs/assets/end-card.html +276 -0
- package/docs/assets/mesh-diagram.html +654 -0
- package/docs/assets/thumbnail.html +295 -0
- package/docs/guides/cli.md +403 -0
- package/docs/guides/index.html +1346 -0
- package/docs/index.html +674 -0
- package/docs/references/claudekit-analysis.md +414 -0
- package/docs/references/voltagent-analysis.md +189 -0
- package/docs/script.js +277 -0
- package/docs/skills/index.html +832 -0
- package/docs/style.css +583 -0
- package/docs/video-demo-plan.md +172 -0
- package/extensions/ai-ml/PACK.md +38 -474
- package/extensions/ai-ml/skills/ai-agents.md +172 -0
- package/extensions/ai-ml/skills/code-sandbox.md +187 -0
- package/extensions/ai-ml/skills/deep-research.md +146 -0
- package/extensions/ai-ml/skills/embedding-search.md +66 -0
- package/extensions/ai-ml/skills/fine-tuning-guide.md +74 -0
- package/extensions/ai-ml/skills/llm-architect.md +125 -0
- package/extensions/ai-ml/skills/llm-integration.md +64 -0
- package/extensions/ai-ml/skills/prompt-patterns.md +72 -0
- package/extensions/ai-ml/skills/rag-patterns.md +66 -0
- package/extensions/ai-ml/skills/web-extraction.md +114 -0
- package/extensions/analytics/PACK.md +19 -484
- package/extensions/analytics/skills/ab-testing.md +72 -0
- package/extensions/analytics/skills/dashboard-patterns.md +83 -0
- package/extensions/analytics/skills/data-validation.md +68 -0
- package/extensions/analytics/skills/funnel-analysis.md +81 -0
- package/extensions/analytics/skills/sql-patterns.md +57 -0
- package/extensions/analytics/skills/statistical-analysis.md +79 -0
- package/extensions/analytics/skills/tracking-setup.md +71 -0
- package/extensions/backend/PACK.md +44 -618
- package/extensions/backend/skills/api-patterns.md +84 -0
- package/extensions/backend/skills/async-pipeline.md +193 -0
- package/extensions/backend/skills/auth-patterns.md +97 -0
- package/extensions/backend/skills/background-jobs.md +133 -0
- package/extensions/backend/skills/caching-patterns.md +108 -0
- package/extensions/backend/skills/cli-generation.md +133 -0
- package/extensions/backend/skills/database-patterns.md +87 -0
- package/extensions/backend/skills/middleware-patterns.md +104 -0
- package/extensions/chrome-ext/PACK.md +19 -921
- package/extensions/chrome-ext/skills/cws-preflight.md +143 -0
- package/extensions/chrome-ext/skills/cws-publish.md +104 -0
- package/extensions/chrome-ext/skills/ext-ai-integration.md +251 -0
- package/extensions/chrome-ext/skills/ext-messaging.md +139 -0
- package/extensions/chrome-ext/skills/ext-storage.md +133 -0
- package/extensions/chrome-ext/skills/mv3-scaffold.md +164 -0
- package/extensions/content/PACK.md +43 -335
- package/extensions/content/skills/blog-patterns.md +88 -0
- package/extensions/content/skills/cms-integration.md +131 -0
- package/extensions/content/skills/content-scoring.md +107 -0
- package/extensions/content/skills/i18n.md +83 -0
- package/extensions/content/skills/mdx-authoring.md +137 -0
- package/extensions/content/skills/reference.md +1014 -0
- package/extensions/content/skills/seo-patterns.md +67 -0
- package/extensions/content/skills/video-repurpose.md +153 -0
- package/extensions/devops/PACK.md +38 -457
- package/extensions/devops/skills/chaos-testing.md +67 -0
- package/extensions/devops/skills/ci-cd.md +75 -0
- package/extensions/devops/skills/docker.md +58 -0
- package/extensions/devops/skills/edge-serverless.md +163 -0
- package/extensions/devops/skills/infra-as-code.md +158 -0
- package/extensions/devops/skills/kubernetes.md +110 -0
- package/extensions/devops/skills/monitoring.md +57 -0
- package/extensions/devops/skills/server-setup.md +64 -0
- package/extensions/devops/skills/ssl-domain.md +42 -0
- package/extensions/ecommerce/PACK.md +62 -226
- package/extensions/ecommerce/skills/cart-system.md +79 -0
- package/extensions/ecommerce/skills/inventory-mgmt.md +102 -0
- package/extensions/ecommerce/skills/order-management.md +126 -0
- package/extensions/ecommerce/skills/payment-integration.md +472 -0
- package/extensions/ecommerce/skills/shopify-dev.md +69 -0
- package/extensions/ecommerce/skills/subscription-billing.md +93 -0
- package/extensions/ecommerce/skills/tax-compliance.md +117 -0
- package/extensions/gamedev/PACK.md +66 -317
- package/extensions/gamedev/skills/asset-pipeline.md +74 -0
- package/extensions/gamedev/skills/audio-system.md +129 -0
- package/extensions/gamedev/skills/camera-system.md +87 -0
- package/extensions/gamedev/skills/ecs.md +98 -0
- package/extensions/gamedev/skills/game-loops.md +72 -0
- package/extensions/gamedev/skills/input-system.md +199 -0
- package/extensions/gamedev/skills/multiplayer.md +180 -0
- package/extensions/gamedev/skills/particles.md +105 -0
- package/extensions/gamedev/skills/physics-engine.md +89 -0
- package/extensions/gamedev/skills/scene-management.md +146 -0
- package/extensions/gamedev/skills/threejs-patterns.md +90 -0
- package/extensions/gamedev/skills/webgl.md +71 -0
- package/extensions/mobile/PACK.md +56 -223
- package/extensions/mobile/skills/app-store-connect.md +152 -0
- package/extensions/mobile/skills/app-store-prep.md +66 -0
- package/extensions/mobile/skills/deep-linking.md +109 -0
- package/extensions/mobile/skills/flutter.md +60 -0
- package/extensions/mobile/skills/ios-build-pipeline.md +142 -0
- package/extensions/mobile/skills/native-bridge.md +66 -0
- package/extensions/mobile/skills/ota-updates.md +97 -0
- package/extensions/mobile/skills/push-notifications.md +111 -0
- package/extensions/mobile/skills/react-native.md +82 -0
- package/extensions/saas/PACK.md +26 -720
- package/extensions/saas/skills/billing-integration.md +121 -0
- package/extensions/saas/skills/feature-flags.md +130 -0
- package/extensions/saas/skills/multi-tenant.md +103 -0
- package/extensions/saas/skills/onboarding-flow.md +139 -0
- package/extensions/saas/skills/subscription-flow.md +95 -0
- package/extensions/saas/skills/team-management.md +144 -0
- package/extensions/security/PACK.md +10 -448
- package/extensions/security/skills/api-security.md +140 -0
- package/extensions/security/skills/compliance.md +68 -0
- package/extensions/security/skills/owasp-audit.md +64 -0
- package/extensions/security/skills/pentest-patterns.md +77 -0
- package/extensions/security/skills/secret-mgmt.md +65 -0
- package/extensions/security/skills/supply-chain.md +65 -0
- package/extensions/trading/PACK.md +18 -535
- package/extensions/trading/skills/chart-components.md +55 -0
- package/extensions/trading/skills/experiment-loop.md +125 -0
- package/extensions/trading/skills/fintech-patterns.md +47 -0
- package/extensions/trading/skills/indicator-library.md +58 -0
- package/extensions/trading/skills/quant-analysis.md +111 -0
- package/extensions/trading/skills/realtime-data.md +58 -0
- package/extensions/trading/skills/trade-logic.md +104 -0
- package/extensions/ui/PACK.md +36 -853
- package/extensions/ui/skills/a11y-audit.md +91 -0
- package/extensions/ui/skills/animation-patterns.md +106 -0
- package/extensions/ui/skills/component-patterns.md +75 -0
- package/extensions/ui/skills/design-decision.md +108 -0
- package/extensions/ui/skills/design-system.md +68 -0
- package/extensions/ui/skills/landing-patterns.md +155 -0
- package/extensions/ui/skills/palette-picker.md +173 -0
- package/extensions/ui/skills/react-health.md +90 -0
- package/extensions/ui/skills/type-system.md +125 -0
- package/extensions/ui/skills/web-vitals.md +153 -0
- package/extensions/zalo/PACK.md +117 -0
- package/extensions/zalo/skills/zalo-oa-mcp.md +317 -0
- package/extensions/zalo/skills/zalo-oa-messaging.md +429 -0
- package/extensions/zalo/skills/zalo-oa-setup.md +236 -0
- package/extensions/zalo/skills/zalo-oa-webhook.md +189 -0
- package/extensions/zalo/skills/zalo-personal-messaging.md +194 -0
- package/extensions/zalo/skills/zalo-personal-setup.md +153 -0
- package/extensions/zalo/skills/zalo-rate-guard.md +219 -0
- package/hooks/.gitkeep +0 -0
- package/hooks/auto-format/index.cjs +48 -0
- package/hooks/context-watch/index.cjs +68 -0
- package/hooks/hooks.json +99 -0
- package/hooks/metrics-collector/index.cjs +42 -0
- package/hooks/post-session-reflect/index.cjs +153 -0
- package/hooks/pre-compact/index.cjs +95 -0
- package/hooks/pre-tool-guard/index.cjs +68 -0
- package/hooks/run-hook +17 -0
- package/hooks/run-hook.cjs +16 -0
- package/hooks/run-hook.cmd +1 -0
- package/hooks/secrets-scan/index.cjs +100 -0
- package/hooks/session-start/index.cjs +65 -0
- package/hooks/typecheck/index.cjs +65 -0
- package/package.json +9 -4
- package/references/ui-pro-max-data/LICENSE-UI-PRO-MAX +21 -0
- package/references/ui-pro-max-data/charts.csv +26 -0
- package/references/ui-pro-max-data/colors.csv +162 -0
- package/references/ui-pro-max-data/styles.csv +85 -0
- package/references/ui-pro-max-data/typography.csv +74 -0
- package/references/ui-pro-max-data/ui-reasoning.csv +162 -0
- package/references/ui-pro-max-data/ux-guidelines.csv +100 -0
- package/skills/ba/SKILL.md +10 -0
- package/skills/brainstorm/SKILL.md +63 -1
- package/skills/completion-gate/SKILL.md +34 -1
- package/skills/context-engine/SKILL.md +13 -0
- package/skills/cook/SKILL.md +155 -5
- package/skills/debug/SKILL.md +56 -1
- package/skills/design/SKILL.md +11 -0
- package/skills/fix/SKILL.md +26 -1
- package/skills/mcp-builder/SKILL.md +48 -1
- package/skills/plan/SKILL.md +23 -6
- package/skills/review/SKILL.md +44 -5
- package/skills/review-intake/SKILL.md +17 -1
- package/skills/skill-forge/SKILL.md +38 -3
- package/skills/skill-router/SKILL.md +89 -7
- package/skills/team/SKILL.md +24 -1
- package/skills/test/SKILL.md +28 -1
- package/skills/verification/SKILL.md +98 -1
package/extensions/ai-ml/PACK.md
CHANGED
|
@@ -1,19 +1,20 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: "@rune/ai-ml"
|
|
3
|
-
description: AI/ML integration patterns — LLM integration, RAG pipelines, embeddings,
|
|
3
|
+
description: AI/ML integration patterns — LLM integration, RAG pipelines, embeddings, fine-tuning workflows, stateful AI agents, code execution sandboxes, web extraction, and deep research loops.
|
|
4
4
|
metadata:
|
|
5
5
|
author: runedev
|
|
6
|
-
version: "0.
|
|
6
|
+
version: "0.4.0"
|
|
7
7
|
layer: L4
|
|
8
8
|
price: "$15"
|
|
9
9
|
target: AI engineers
|
|
10
|
+
format: split
|
|
10
11
|
---
|
|
11
12
|
|
|
12
13
|
# @rune/ai-ml
|
|
13
14
|
|
|
14
15
|
## Purpose
|
|
15
16
|
|
|
16
|
-
AI-powered features fail in predictable ways: LLM calls without retry logic that crash on rate limits, RAG pipelines that retrieve irrelevant chunks because the chunking strategy ignores document structure, embedding search that returns semantic matches with zero keyword overlap,
|
|
17
|
+
AI-powered features fail in predictable ways: LLM calls without retry logic that crash on rate limits, RAG pipelines that retrieve irrelevant chunks because the chunking strategy ignores document structure, embedding search that returns semantic matches with zero keyword overlap, fine-tuning runs that overfit because the eval set leaked into training data, AI agents that leak state across requests or lose progress on crashes, and code interpreters that execute untrusted LLM output without isolation. This pack codifies production patterns for each — from API client resilience to retrieval quality to model evaluation to agent state management to secure sandboxed execution — so AI features ship with the reliability of traditional software.
|
|
17
18
|
|
|
18
19
|
## Triggers
|
|
19
20
|
|
|
@@ -22,447 +23,27 @@ AI-powered features fail in predictable ways: LLM calls without retry logic that
|
|
|
22
23
|
- `/rune rag-patterns` — build or audit RAG pipeline
|
|
23
24
|
- `/rune embedding-search` — implement or optimize semantic search
|
|
24
25
|
- `/rune fine-tuning-guide` — prepare and execute fine-tuning workflow
|
|
26
|
+
- `/rune ai-agents` — design and build stateful AI agents
|
|
27
|
+
- `/rune code-sandbox` — set up secure code execution for AI
|
|
28
|
+
- `/rune web-extraction` — build structured data extraction from web pages
|
|
29
|
+
- `/rune deep-research` — implement iterative AI research loops with convergence
|
|
25
30
|
- Called by `cook` (L1) when AI/ML task detected
|
|
26
31
|
- Called by `plan` (L2) when AI architecture decisions needed
|
|
27
32
|
|
|
28
33
|
## Skills Included
|
|
29
34
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
**Step 3 — Emit robust LLM client**
|
|
43
|
-
Emit: typed client wrapper with exponential backoff retry, structured output via Zod schema + function calling, streaming with proper error boundaries, token budget management, and prompt version registry.
|
|
44
|
-
|
|
45
|
-
#### Example
|
|
46
|
-
|
|
47
|
-
```typescript
|
|
48
|
-
// Robust LLM client — retry, structured output, fallback chain
|
|
49
|
-
import OpenAI from 'openai';
|
|
50
|
-
import { z } from 'zod';
|
|
51
|
-
|
|
52
|
-
const client = new OpenAI();
|
|
53
|
-
|
|
54
|
-
const SentimentSchema = z.object({
|
|
55
|
-
sentiment: z.enum(['positive', 'negative', 'neutral']),
|
|
56
|
-
confidence: z.number().min(0).max(1),
|
|
57
|
-
reasoning: z.string(),
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
async function analyzeSentiment(text: string, attempt = 0): Promise<z.infer<typeof SentimentSchema>> {
|
|
61
|
-
const models = ['gpt-4o-mini', 'gpt-4o'] as const; // fallback chain
|
|
62
|
-
const model = attempt >= 2 ? models[1] : models[0];
|
|
63
|
-
|
|
64
|
-
try {
|
|
65
|
-
const response = await client.chat.completions.create({
|
|
66
|
-
model,
|
|
67
|
-
messages: [
|
|
68
|
-
{ role: 'system', content: 'Analyze sentiment. Return JSON matching the schema.' },
|
|
69
|
-
{ role: 'user', content: text },
|
|
70
|
-
],
|
|
71
|
-
response_format: { type: 'json_object' },
|
|
72
|
-
max_tokens: 200,
|
|
73
|
-
timeout: 10_000,
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
return SentimentSchema.parse(JSON.parse(response.choices[0].message.content!));
|
|
77
|
-
} catch (err) {
|
|
78
|
-
if (err instanceof OpenAI.RateLimitError && attempt < 3) {
|
|
79
|
-
await new Promise(r => setTimeout(r, Math.pow(2, attempt) * 1000));
|
|
80
|
-
return analyzeSentiment(text, attempt + 1);
|
|
81
|
-
}
|
|
82
|
-
throw err;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
```
|
|
86
|
-
|
|
87
|
-
---
|
|
88
|
-
|
|
89
|
-
### rag-patterns
|
|
90
|
-
|
|
91
|
-
RAG pipeline patterns — document chunking, embedding generation, vector store setup, retrieval strategies, reranking.
|
|
92
|
-
|
|
93
|
-
#### Workflow
|
|
94
|
-
|
|
95
|
-
**Step 1 — Detect RAG components**
|
|
96
|
-
Use Grep to find vector store usage: `PineconeClient`, `pgvector`, `Weaviate`, `ChromaClient`, `QdrantClient`. Find embedding calls: `embeddings.create`, `embed()`. Read the ingestion pipeline and retrieval logic to map the full RAG flow.
|
|
97
|
-
|
|
98
|
-
**Step 2 — Audit retrieval quality**
|
|
99
|
-
Check for: fixed-size chunking that splits mid-sentence (context loss), no overlap between chunks (boundary information lost), embeddings generated without metadata (no filtering capability), retrieval without reranking (relevance drops after top-3), no chunk deduplication, and context window overflow (retrieved chunks exceed model limit).
|
|
100
|
-
|
|
101
|
-
**Step 3 — Emit RAG pipeline**
|
|
102
|
-
Emit: recursive text splitter with semantic boundaries, embedding generation with metadata, vector upsert with namespace, retrieval with reranking, and context window budget management.
|
|
103
|
-
|
|
104
|
-
#### Example
|
|
105
|
-
|
|
106
|
-
```typescript
|
|
107
|
-
// RAG pipeline — recursive chunking + pgvector + reranking
|
|
108
|
-
import { RecursiveCharacterTextSplitter } from 'langchain/text_splitter';
|
|
109
|
-
import { OpenAIEmbeddings } from '@langchain/openai';
|
|
110
|
-
import { PGVectorStore } from '@langchain/community/vectorstores/pgvector';
|
|
111
|
-
|
|
112
|
-
// Ingestion: chunk → embed → store
|
|
113
|
-
async function ingestDocument(doc: { content: string; metadata: Record<string, string> }) {
|
|
114
|
-
const splitter = new RecursiveCharacterTextSplitter({
|
|
115
|
-
chunkSize: 1000,
|
|
116
|
-
chunkOverlap: 200,
|
|
117
|
-
separators: ['\n## ', '\n### ', '\n\n', '\n', '. ', ' '],
|
|
118
|
-
});
|
|
119
|
-
const chunks = await splitter.createDocuments(
|
|
120
|
-
[doc.content],
|
|
121
|
-
[doc.metadata],
|
|
122
|
-
);
|
|
123
|
-
|
|
124
|
-
const embeddings = new OpenAIEmbeddings({ model: 'text-embedding-3-small' });
|
|
125
|
-
await PGVectorStore.fromDocuments(chunks, embeddings, {
|
|
126
|
-
postgresConnectionOptions: { connectionString: process.env.DATABASE_URL },
|
|
127
|
-
tableName: 'documents',
|
|
128
|
-
});
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
// Retrieval: query → vector search → rerank → top-k
|
|
132
|
-
async function retrieve(query: string, topK = 5) {
|
|
133
|
-
const store = await PGVectorStore.initialize(embeddings, pgConfig);
|
|
134
|
-
const candidates = await store.similaritySearch(query, topK * 3); // over-retrieve
|
|
135
|
-
|
|
136
|
-
// Rerank with Cohere
|
|
137
|
-
const { results } = await cohere.rerank({
|
|
138
|
-
model: 'rerank-english-v3.0',
|
|
139
|
-
query,
|
|
140
|
-
documents: candidates.map(c => c.pageContent),
|
|
141
|
-
topN: topK,
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
return results.map(r => candidates[r.index]);
|
|
145
|
-
}
|
|
146
|
-
```
|
|
147
|
-
|
|
148
|
-
---
|
|
149
|
-
|
|
150
|
-
### embedding-search
|
|
151
|
-
|
|
152
|
-
Embedding-based search — semantic search, hybrid search (BM25 + vector), similarity thresholds, index optimization.
|
|
153
|
-
|
|
154
|
-
#### Workflow
|
|
155
|
-
|
|
156
|
-
**Step 1 — Detect search implementation**
|
|
157
|
-
Use Grep to find search code: `similarity_search`, `vector_search`, `fts`, `tsvector`, `BM25`. Read search handlers to understand: query flow, ranking strategy, and result formatting.
|
|
158
|
-
|
|
159
|
-
**Step 2 — Audit search quality**
|
|
160
|
-
Check for: pure vector search without keyword fallback (misses exact matches), no similarity threshold (returns irrelevant results at low scores), missing query embedding cache (repeated queries re-embed), no hybrid scoring (BM25 for exact + vector for semantic), and unoptimized vector index (HNSW parameters not tuned).
|
|
161
|
-
|
|
162
|
-
**Step 3 — Emit hybrid search**
|
|
163
|
-
Emit: combined BM25 + vector search with reciprocal rank fusion, similarity threshold filtering, query embedding cache, and HNSW index tuning.
|
|
164
|
-
|
|
165
|
-
#### Example
|
|
166
|
-
|
|
167
|
-
```typescript
|
|
168
|
-
// Hybrid search — BM25 + vector with reciprocal rank fusion
|
|
169
|
-
async function hybridSearch(query: string, limit = 10) {
|
|
170
|
-
// Parallel: keyword (BM25) + semantic (vector)
|
|
171
|
-
const [keywordResults, vectorResults] = await Promise.all([
|
|
172
|
-
db.execute(sql`
|
|
173
|
-
SELECT id, content, ts_rank(search_vector, plainto_tsquery(${query})) AS bm25_score
|
|
174
|
-
FROM documents
|
|
175
|
-
WHERE search_vector @@ plainto_tsquery(${query})
|
|
176
|
-
ORDER BY bm25_score DESC LIMIT ${limit * 2}
|
|
177
|
-
`),
|
|
178
|
-
db.execute(sql`
|
|
179
|
-
SELECT id, content, 1 - (embedding <=> ${await getEmbedding(query)}) AS vector_score
|
|
180
|
-
FROM documents
|
|
181
|
-
ORDER BY embedding <=> ${await getEmbedding(query)}
|
|
182
|
-
LIMIT ${limit * 2}
|
|
183
|
-
`),
|
|
184
|
-
]);
|
|
185
|
-
|
|
186
|
-
// Reciprocal rank fusion (k=60)
|
|
187
|
-
const scores = new Map<string, number>();
|
|
188
|
-
const K = 60;
|
|
189
|
-
keywordResults.forEach((r, i) => scores.set(r.id, (scores.get(r.id) || 0) + 1 / (K + i + 1)));
|
|
190
|
-
vectorResults.forEach((r, i) => scores.set(r.id, (scores.get(r.id) || 0) + 1 / (K + i + 1)));
|
|
191
|
-
|
|
192
|
-
return [...scores.entries()]
|
|
193
|
-
.sort((a, b) => b[1] - a[1])
|
|
194
|
-
.slice(0, limit)
|
|
195
|
-
.filter(([_, score]) => score > 0.01); // threshold
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
// Embedding cache (avoid re-embedding repeated queries)
|
|
199
|
-
const embeddingCache = new Map<string, number[]>();
|
|
200
|
-
async function getEmbedding(text: string): Promise<number[]> {
|
|
201
|
-
const cached = embeddingCache.get(text);
|
|
202
|
-
if (cached) return cached;
|
|
203
|
-
const { data } = await openai.embeddings.create({ model: 'text-embedding-3-small', input: text });
|
|
204
|
-
embeddingCache.set(text, data[0].embedding);
|
|
205
|
-
return data[0].embedding;
|
|
206
|
-
}
|
|
207
|
-
```
|
|
208
|
-
|
|
209
|
-
---
|
|
210
|
-
|
|
211
|
-
### fine-tuning-guide
|
|
212
|
-
|
|
213
|
-
Fine-tuning workflows — dataset preparation, training configuration, evaluation metrics, deployment, A/B testing.
|
|
214
|
-
|
|
215
|
-
#### Workflow
|
|
216
|
-
|
|
217
|
-
**Step 1 — Audit training data**
|
|
218
|
-
Use Read to examine the dataset files. Check for: data format (JSONL with `messages` array), train/eval split (eval must not overlap with train), sufficient examples (minimum 50, recommended 200+), balanced class distribution, and PII in training data.
|
|
219
|
-
|
|
220
|
-
**Step 2 — Prepare and validate dataset**
|
|
221
|
-
Emit: JSONL formatter that validates each example, train/eval splitter with stratification, token count estimator (cost preview), and data quality checks (duplicate detection, format validation).
|
|
222
|
-
|
|
223
|
-
**Step 3 — Execute fine-tuning and evaluate**
|
|
224
|
-
Emit: fine-tune API call with hyperparameters, evaluation script that compares base vs fine-tuned on held-out set, and A/B deployment configuration.
|
|
225
|
-
|
|
226
|
-
#### Example
|
|
227
|
-
|
|
228
|
-
```python
|
|
229
|
-
# Fine-tuning workflow — prepare, train, evaluate
|
|
230
|
-
import json
|
|
231
|
-
import openai
|
|
232
|
-
from sklearn.model_selection import train_test_split
|
|
233
|
-
|
|
234
|
-
# Step 1: Prepare JSONL dataset
|
|
235
|
-
def prepare_dataset(examples: list[dict], output_prefix: str):
|
|
236
|
-
train, eval_set = train_test_split(examples, test_size=0.2, random_state=42)
|
|
237
|
-
|
|
238
|
-
for split_name, split_data in [("train", train), ("eval", eval_set)]:
|
|
239
|
-
path = f"{output_prefix}_{split_name}.jsonl"
|
|
240
|
-
with open(path, "w") as f:
|
|
241
|
-
for ex in split_data:
|
|
242
|
-
f.write(json.dumps({"messages": [
|
|
243
|
-
{"role": "system", "content": ex["system"]},
|
|
244
|
-
{"role": "user", "content": ex["input"]},
|
|
245
|
-
{"role": "assistant", "content": ex["output"]},
|
|
246
|
-
]}) + "\n")
|
|
247
|
-
print(f"Wrote {len(split_data)} examples to {path}")
|
|
248
|
-
|
|
249
|
-
# Step 2: Launch fine-tuning
|
|
250
|
-
def start_fine_tune(train_file: str, eval_file: str):
|
|
251
|
-
train_id = openai.files.create(file=open(train_file, "rb"), purpose="fine-tune").id
|
|
252
|
-
eval_id = openai.files.create(file=open(eval_file, "rb"), purpose="fine-tune").id
|
|
253
|
-
|
|
254
|
-
job = openai.fine_tuning.jobs.create(
|
|
255
|
-
training_file=train_id,
|
|
256
|
-
validation_file=eval_id,
|
|
257
|
-
model="gpt-4o-mini-2024-07-18",
|
|
258
|
-
hyperparameters={"n_epochs": 3, "batch_size": "auto", "learning_rate_multiplier": "auto"},
|
|
259
|
-
)
|
|
260
|
-
print(f"Fine-tuning job: {job.id} — status: {job.status}")
|
|
261
|
-
return job
|
|
262
|
-
|
|
263
|
-
# Step 3: Evaluate base vs fine-tuned
|
|
264
|
-
def evaluate(base_model: str, ft_model: str, eval_set: list[dict]) -> dict:
|
|
265
|
-
results = {"base": {"correct": 0}, "finetuned": {"correct": 0}}
|
|
266
|
-
for ex in eval_set:
|
|
267
|
-
for label, model in [("base", base_model), ("finetuned", ft_model)]:
|
|
268
|
-
response = openai.chat.completions.create(
|
|
269
|
-
model=model, messages=ex["messages"][:2], max_tokens=500,
|
|
270
|
-
)
|
|
271
|
-
if response.choices[0].message.content.strip() == ex["messages"][2]["content"].strip():
|
|
272
|
-
results[label]["correct"] += 1
|
|
273
|
-
for label in results:
|
|
274
|
-
results[label]["accuracy"] = results[label]["correct"] / len(eval_set)
|
|
275
|
-
return results
|
|
276
|
-
```
|
|
277
|
-
|
|
278
|
-
---
|
|
279
|
-
|
|
280
|
-
### llm-architect
|
|
281
|
-
|
|
282
|
-
LLM system architecture — model selection, prompt engineering patterns, evaluation frameworks, cost optimization, multi-model routing, and guardrail design.
|
|
283
|
-
|
|
284
|
-
#### Workflow
|
|
285
|
-
|
|
286
|
-
**Step 1 — Assess LLM requirements**
|
|
287
|
-
Understand the use case: what does the LLM need to do? Classify into:
|
|
288
|
-
- **Generation**: open-ended text (blog, email, creative writing)
|
|
289
|
-
- **Extraction**: structured data from unstructured input (JSON from text, entities, classification)
|
|
290
|
-
- **Reasoning**: multi-step logic (math, code generation, planning)
|
|
291
|
-
- **Conversation**: multi-turn dialogue with memory
|
|
292
|
-
- **Agentic**: tool use, function calling, autonomous task execution
|
|
293
|
-
|
|
294
|
-
For each class, identify: latency requirements (real-time < 2s, async < 30s, batch), accuracy requirements (critical = needs eval suite, casual = spot check), cost sensitivity (per-call budget), and data sensitivity (PII, HIPAA, can data leave the network?).
|
|
295
|
-
|
|
296
|
-
**Step 2 — Model selection matrix**
|
|
297
|
-
Based on requirements, recommend model tier:
|
|
298
|
-
|
|
299
|
-
| Requirement | Recommended | Fallback |
|
|
300
|
-
|------------|-------------|----------|
|
|
301
|
-
| Fast + cheap (classification, routing) | Haiku / GPT-4o-mini | Local (Llama 3) |
|
|
302
|
-
| Balanced (code, summaries, RAG) | Sonnet / GPT-4o | Haiku with retry |
|
|
303
|
-
| Deep reasoning (architecture, math) | Opus / o1 | Sonnet with chain-of-thought |
|
|
304
|
-
| On-premise required | Llama 3 / Mistral | Ollama local deployment |
|
|
305
|
-
| Multimodal (vision + text) | Sonnet / GPT-4o | Local LLaVA |
|
|
306
|
-
|
|
307
|
-
Emit: primary model, fallback model, estimated cost per 1K calls, and latency p50/p99.
|
|
308
|
-
|
|
309
|
-
**Step 3 — Prompt architecture**
|
|
310
|
-
Design the prompt structure:
|
|
311
|
-
- **System prompt**: Role definition, constraints, output format. Keep under 500 tokens for cost efficiency.
|
|
312
|
-
- **Few-shot examples**: 2-3 examples for extraction/classification tasks. Format matches expected output exactly.
|
|
313
|
-
- **Chain-of-thought**: For reasoning tasks, explicitly request step-by-step thinking before final answer.
|
|
314
|
-
- **Structured output**: JSON mode or tool use for extraction. Define schema with Zod/Pydantic for validation.
|
|
315
|
-
|
|
316
|
-
**Step 4 — Guardrails and evaluation**
|
|
317
|
-
Design safety and quality layers:
|
|
318
|
-
- **Input guardrails**: PII detection, prompt injection detection, topic filtering
|
|
319
|
-
- **Output guardrails**: Schema validation, hallucination checks, toxicity filtering
|
|
320
|
-
- **Evaluation framework**: Define eval dataset (50+ examples), metrics (accuracy, latency, cost), and regression threshold (new prompt must not drop > 2% on any metric)
|
|
321
|
-
|
|
322
|
-
Save architecture doc to `.rune/ai/llm-architecture.md`.
|
|
323
|
-
|
|
324
|
-
#### Example
|
|
325
|
-
|
|
326
|
-
```typescript
|
|
327
|
-
// Multi-model router with fallback
|
|
328
|
-
interface ModelConfig {
|
|
329
|
-
id: string;
|
|
330
|
-
provider: 'anthropic' | 'openai' | 'local';
|
|
331
|
-
costPer1kTokens: number;
|
|
332
|
-
maxTokens: number;
|
|
333
|
-
latencyP50Ms: number;
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
const MODELS: Record<string, ModelConfig> = {
|
|
337
|
-
fast: {
|
|
338
|
-
id: 'claude-haiku-4-5-20251001',
|
|
339
|
-
provider: 'anthropic',
|
|
340
|
-
costPer1kTokens: 0.001,
|
|
341
|
-
maxTokens: 4096,
|
|
342
|
-
latencyP50Ms: 200,
|
|
343
|
-
},
|
|
344
|
-
balanced: {
|
|
345
|
-
id: 'claude-sonnet-4-6',
|
|
346
|
-
provider: 'anthropic',
|
|
347
|
-
costPer1kTokens: 0.01,
|
|
348
|
-
maxTokens: 8192,
|
|
349
|
-
latencyP50Ms: 800,
|
|
350
|
-
},
|
|
351
|
-
deep: {
|
|
352
|
-
id: 'claude-opus-4-6',
|
|
353
|
-
provider: 'anthropic',
|
|
354
|
-
costPer1kTokens: 0.05,
|
|
355
|
-
maxTokens: 16384,
|
|
356
|
-
latencyP50Ms: 2000,
|
|
357
|
-
},
|
|
358
|
-
};
|
|
359
|
-
|
|
360
|
-
type TaskComplexity = 'trivial' | 'standard' | 'complex';
|
|
361
|
-
|
|
362
|
-
function selectModel(complexity: TaskComplexity): ModelConfig {
|
|
363
|
-
const map: Record<TaskComplexity, string> = {
|
|
364
|
-
trivial: 'fast',
|
|
365
|
-
standard: 'balanced',
|
|
366
|
-
complex: 'deep',
|
|
367
|
-
};
|
|
368
|
-
return MODELS[map[complexity]];
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
// Prompt architecture template
|
|
372
|
-
const systemPrompt = `You are a ${role} assistant.
|
|
373
|
-
|
|
374
|
-
CONSTRAINTS:
|
|
375
|
-
- ${constraints.join('\n- ')}
|
|
376
|
-
|
|
377
|
-
OUTPUT FORMAT:
|
|
378
|
-
Return valid JSON matching this schema:
|
|
379
|
-
${JSON.stringify(outputSchema, null, 2)}
|
|
380
|
-
|
|
381
|
-
Do not include explanations outside the JSON.`;
|
|
382
|
-
|
|
383
|
-
// Guardrail: validate structured output
|
|
384
|
-
import { z } from 'zod';
|
|
385
|
-
|
|
386
|
-
const OutputSchema = z.object({
|
|
387
|
-
classification: z.enum(['positive', 'negative', 'neutral']),
|
|
388
|
-
confidence: z.number().min(0).max(1),
|
|
389
|
-
reasoning: z.string().max(200),
|
|
390
|
-
});
|
|
391
|
-
|
|
392
|
-
function validateOutput(raw: string): z.infer<typeof OutputSchema> {
|
|
393
|
-
const parsed = JSON.parse(raw);
|
|
394
|
-
return OutputSchema.parse(parsed); // throws if invalid
|
|
395
|
-
}
|
|
396
|
-
```
|
|
397
|
-
|
|
398
|
-
---
|
|
399
|
-
|
|
400
|
-
### prompt-patterns
|
|
401
|
-
|
|
402
|
-
Reusable prompt engineering patterns — structured output, chain-of-thought, self-critique, tool use orchestration, and multi-turn memory management.
|
|
403
|
-
|
|
404
|
-
#### Workflow
|
|
405
|
-
|
|
406
|
-
**Step 1 — Identify the pattern**
|
|
407
|
-
Match the user's task to a proven prompt pattern:
|
|
408
|
-
- **Extraction**: Use JSON mode + schema definition + few-shot examples
|
|
409
|
-
- **Classification**: Use enum output + confidence score + chain-of-thought
|
|
410
|
-
- **Summarization**: Use structured summary template + length constraint + key point extraction
|
|
411
|
-
- **Code generation**: Use system prompt with language constraints + test-driven output format
|
|
412
|
-
- **Agent loop**: Use ReAct pattern (Thought → Action → Observation → repeat)
|
|
413
|
-
- **Self-critique**: Use generate → critique → revise loop for quality-sensitive output
|
|
414
|
-
|
|
415
|
-
**Step 2 — Apply the pattern**
|
|
416
|
-
Generate the prompt following the selected pattern. Include:
|
|
417
|
-
- System prompt (role + constraints + output format)
|
|
418
|
-
- User message template (input variables marked with `{{variable}}`)
|
|
419
|
-
- Few-shot examples (2-3, matching exact output format)
|
|
420
|
-
- Validation schema (Zod/Pydantic for structured output)
|
|
421
|
-
|
|
422
|
-
**Step 3 — Test harness**
|
|
423
|
-
Emit a test file with 5+ test cases that validate the prompt produces correct output for known inputs. Include edge cases: empty input, very long input, ambiguous input, adversarial input.
|
|
424
|
-
|
|
425
|
-
#### Example
|
|
426
|
-
|
|
427
|
-
```typescript
|
|
428
|
-
// Pattern: ReAct Agent Loop
|
|
429
|
-
const REACT_SYSTEM = `You are an agent that solves tasks using available tools.
|
|
430
|
-
|
|
431
|
-
For each step, output EXACTLY this JSON format:
|
|
432
|
-
{"thought": "reasoning about what to do next",
|
|
433
|
-
"action": "tool_name",
|
|
434
|
-
"action_input": "input for the tool"}
|
|
435
|
-
|
|
436
|
-
After receiving an observation, continue with the next thought.
|
|
437
|
-
When you have the final answer, output:
|
|
438
|
-
{"thought": "I have the answer", "final_answer": "the answer"}
|
|
439
|
-
|
|
440
|
-
Available tools:
|
|
441
|
-
{{tools}}`;
|
|
442
|
-
|
|
443
|
-
// Pattern: Self-Critique Loop
|
|
444
|
-
async function generateWithCritique(prompt: string, maxRounds = 2) {
|
|
445
|
-
let output = await llm.generate(prompt);
|
|
446
|
-
|
|
447
|
-
for (let i = 0; i < maxRounds; i++) {
|
|
448
|
-
const critique = await llm.generate(
|
|
449
|
-
`Review this output for errors, omissions, and improvements:\n\n${output}\n\n` +
|
|
450
|
-
`List specific issues. If no issues, respond with "APPROVED".`
|
|
451
|
-
);
|
|
452
|
-
|
|
453
|
-
if (critique.includes('APPROVED')) break;
|
|
454
|
-
|
|
455
|
-
output = await llm.generate(
|
|
456
|
-
`Original output:\n${output}\n\nCritique:\n${critique}\n\n` +
|
|
457
|
-
`Revise the output to address all issues in the critique.`
|
|
458
|
-
);
|
|
459
|
-
}
|
|
460
|
-
|
|
461
|
-
return output;
|
|
462
|
-
}
|
|
463
|
-
```
|
|
464
|
-
|
|
465
|
-
---
|
|
35
|
+
| Skill | Model | Description |
|
|
36
|
+
|-------|-------|-------------|
|
|
37
|
+
| [llm-integration](skills/llm-integration.md) | sonnet | API client wrappers, streaming, structured output, retry + fallback chain, prompt versioning |
|
|
38
|
+
| [rag-patterns](skills/rag-patterns.md) | sonnet | Document chunking, embedding generation, vector store setup, retrieval, reranking |
|
|
39
|
+
| [embedding-search](skills/embedding-search.md) | sonnet | Semantic search, hybrid BM25 + vector, similarity thresholds, index optimization |
|
|
40
|
+
| [fine-tuning-guide](skills/fine-tuning-guide.md) | sonnet | Dataset preparation, training config, evaluation metrics, deployment, A/B testing |
|
|
41
|
+
| [llm-architect](skills/llm-architect.md) | opus | Model selection, prompt engineering, evaluation frameworks, cost optimization, guardrails |
|
|
42
|
+
| [prompt-patterns](skills/prompt-patterns.md) | sonnet | Structured output, chain-of-thought, self-critique, ReAct, multi-turn memory management |
|
|
43
|
+
| [ai-agents](skills/ai-agents.md) | sonnet | Stateful agents, RPC methods, scheduling, multi-agent coordination, MCP integration, HITL |
|
|
44
|
+
| [code-sandbox](skills/code-sandbox.md) | sonnet | Container isolation, resource limits, timeout enforcement, stateful sessions, output capture |
|
|
45
|
+
| [web-extraction](skills/web-extraction.md) | sonnet | Schema-driven extraction, anti-bot handling, prompt injection defense, multi-entity dedup |
|
|
46
|
+
| [deep-research](skills/deep-research.md) | sonnet | Iterative research loop with convergence, source attribution, confidence scoring |
|
|
466
47
|
|
|
467
48
|
## Connections
|
|
468
49
|
|
|
@@ -470,48 +51,31 @@ async function generateWithCritique(prompt: string, maxRounds = 2) {
|
|
|
470
51
|
Calls → research (L3): lookup model documentation and best practices
|
|
471
52
|
Calls → docs-seeker (L3): API reference for LLM providers
|
|
472
53
|
Calls → verification (L3): validate pipeline correctness
|
|
54
|
+
Calls → @rune/devops (L4): ai-agents → edge-serverless for agent deployment (Workers, Lambda)
|
|
55
|
+
Calls → @rune/backend (L4): ai-agents → API patterns for agent endpoints and WebSocket handlers
|
|
56
|
+
Calls → sentinel (L2): code-sandbox security audit on container isolation
|
|
473
57
|
Called By ← cook (L1): when AI/ML task detected
|
|
474
58
|
Called By ← plan (L2): when AI architecture decisions needed
|
|
475
59
|
Called By ← review (L2): when AI code under review
|
|
60
|
+
Called By ← mcp-builder (L2): ai-agents feeds MCP server patterns for agent-based MCP
|
|
61
|
+
ai-agents → code-sandbox: agents use sandboxes for executing LLM-generated code safely
|
|
62
|
+
code-sandbox → ai-agents: sandbox results feed back into agent state and conversation
|
|
63
|
+
web-extraction → rag-patterns: extracted structured data feeds into RAG ingestion pipeline
|
|
64
|
+
deep-research → web-extraction: research loop uses extraction for each discovered URL
|
|
65
|
+
deep-research → embedding-search: relevance scoring uses embeddings for semantic similarity
|
|
476
66
|
```
|
|
477
67
|
|
|
478
|
-
## Tech Stack Support
|
|
479
|
-
|
|
480
|
-
| Provider | SDK | Vector Store | Notes |
|
|
481
|
-
|----------|-----|-------------|-------|
|
|
482
|
-
| OpenAI | openai v4+ | pgvector | Most common, JSON mode + function calling |
|
|
483
|
-
| Anthropic | @anthropic-ai/sdk | Pinecone | Tool use + long context |
|
|
484
|
-
| Cohere | cohere-ai | Weaviate | Reranking + embed v3 |
|
|
485
|
-
| Local (Ollama) | ollama-js | ChromaDB | Self-hosted, privacy-sensitive |
|
|
486
|
-
|
|
487
|
-
## Constraints
|
|
488
|
-
|
|
489
|
-
1. MUST implement retry with exponential backoff on all LLM API calls — rate limits are guaranteed at scale.
|
|
490
|
-
2. MUST validate LLM output against a schema (Zod/Pydantic) — never trust raw text parsing for structured data.
|
|
491
|
-
3. MUST separate training and evaluation datasets — eval set leaking into training invalidates all metrics.
|
|
492
|
-
4. MUST set similarity thresholds on vector search — returning all results regardless of score degrades quality.
|
|
493
|
-
5. MUST NOT embed sensitive/PII data without explicit consent — embeddings are not easily deletable from vector stores.
|
|
494
|
-
|
|
495
68
|
## Sharp Edges
|
|
496
69
|
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
## Done When
|
|
507
|
-
|
|
508
|
-
- LLM client has retry, structured output, streaming, and fallback chain
|
|
509
|
-
- RAG pipeline ingests, chunks, embeds, stores, retrieves, and reranks correctly
|
|
510
|
-
- Hybrid search returns relevant results for both keyword and semantic queries
|
|
511
|
-
- Fine-tuning dataset validated, model trained, and eval shows improvement over base
|
|
512
|
-
- All API calls handle rate limits and timeouts gracefully
|
|
513
|
-
- Structured report emitted for each skill invoked
|
|
70
|
+
- **Rate limits**: MUST implement exponential backoff retry on all LLM API calls — guaranteed at scale.
|
|
71
|
+
- **Schema validation**: MUST validate LLM output with Zod/Pydantic — never trust raw text parsing.
|
|
72
|
+
- **Eval leakage**: MUST separate training and evaluation datasets — leakage invalidates all metrics.
|
|
73
|
+
- **Similarity thresholds**: MUST set thresholds on vector search — unrestricted results degrade quality.
|
|
74
|
+
- **PII in embeddings**: MUST NOT embed sensitive data without consent — not easily deletable from vector stores.
|
|
75
|
+
- **Embedding model pinning**: Pin model version in index metadata — dimension mismatch on upgrade is CRITICAL.
|
|
76
|
+
- **Prompt injection**: Web pages may contain adversarial content targeting extraction LLMs — system prompt must block.
|
|
77
|
+
- **Sandbox escape**: Use rootless Docker or gVisor for high-security code execution environments.
|
|
514
78
|
|
|
515
79
|
## Cost Profile
|
|
516
80
|
|
|
517
|
-
~
|
|
81
|
+
~24,000–40,000 tokens per full pack run (all 10 skills). Individual skill: ~2,500–5,000 tokens. Sonnet default. Use haiku for code detection scans; escalate to sonnet for pipeline design, extraction strategy, and research loop orchestration.
|