Knowledge & Context
ContractSpec provides first-class support for knowledge management, enabling semantic search, RAG (Retrieval-Augmented Generation), and context-aware decision-making. Knowledge is organized into typed spaces with different trust levels and access controls.
Why knowledge matters
Modern applications need to:
- Understand context - Access relevant information to make informed decisions
- Learn from history - Use past interactions to improve future responses
- Trust sources - Distinguish between authoritative and reference information
- Scale knowledge - Handle growing amounts of documentation and data
- Maintain privacy - Keep tenant knowledge isolated and secure
Knowledge architecture
ContractSpec's knowledge system has three main components:
1. Knowledge Categories
Four trust levels that determine how knowledge is used:
canonicalGround truth - product specs, schemas, official policiesoperationalInternal docs - support tickets, runbooks, sales materialsexternalThird-party - PSP docs, regulations, integration guidesephemeralTemporary - agent scratchpads, session context, drafts2. Knowledge Spaces
Logical domains of knowledge defined globally:
type KnowledgeSpaceSpec = {
id: string;
label: string;
category: KnowledgeCategory;
storageStrategy: "vector" | "search" | "hybrid";
indexProvider: string; // e.g., "qdrant"
retentionPolicy: { days?: number };
intendedAudience: "agents" | "humans" | "admin-only";
};3. Knowledge Sources
Per-tenant data sources that feed knowledge spaces:
type KnowledgeSourceConfig = {
id: string;
tenantId: string;
spaceId: string;
kind: "uploaded-document" | "url" | "email"
| "notion" | "database-query" | "raw-text";
location: string;
syncPolicy: { interval?: string; webhook?: boolean };
lastSyncedAt?: string;
};How it works
Knowledge flows through ContractSpec in four stages:
- Define spaces - Create KnowledgeSpaceSpec for each logical domain (e.g., "Product Canon", "Support FAQ")
- Configure sources - Tenants connect their data sources (Notion, Gmail, uploads, etc.)
- Sync & index - Sources are synced, chunked, and indexed in vector databases
- Query & retrieve - Workflows and agents perform semantic search to find relevant context
Use cases
Support Agents
Answer customer questions using product documentation (canonical) and past support tickets (operational).
Invoice Generation
Generate invoices using product catalog (canonical) and pricing rules (operational).
Compliance Checking
Validate operations against regulatory requirements (external) and internal policies (canonical).
Semantic Search
Find relevant documents, tickets, or records using natural language queries.
Integration with workflows
Knowledge spaces are consumed by workflows through standard capabilities:
import { defineWorkflow } from '@lssm/lib.contracts';
import { OpenAIEmbeddings, VectorSearch, OpenAIChat } from './specs';
export const AnswerSupportQuestion = defineWorkflow({
meta: {
name: 'support.answerQuestion',
version: 1,
description: 'Answer support questions using RAG',
owners: ['team-support'],
tags: ['ai', 'support'],
stability: 'stable',
},
steps: [
{
id: 'embed-question',
operation: OpenAIEmbeddings,
inputs: (ctx, input) => ({
text: input.question,
}),
},
{
id: 'search-knowledge',
operation: VectorSearch,
inputs: (ctx, input, steps) => ({
collection: 'product-canon', // KnowledgeSpace
vector: steps['embed-question'].output.embedding,
limit: 5,
}),
},
{
id: 'generate-answer',
operation: OpenAIChat,
inputs: (ctx, input, steps) => ({
messages: [
{ role: 'system', content: 'Answer using the provided context' },
{
role: 'user',
content: `Context: ${steps['search-knowledge'].output.results}\nQuestion: ${input.question}`
},
],
}),
},
],
});