-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathknowledge-document.zod.ts
More file actions
97 lines (91 loc) · 3.7 KB
/
Copy pathknowledge-document.zod.ts
File metadata and controls
97 lines (91 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { z } from 'zod';
import { lazySchema } from '../shared/lazy-schema';
/**
* Knowledge Document / Chunk / Hit — canonical shapes shared by every
* `IKnowledgeAdapter` implementation.
*
* The framework does **not** prescribe chunk strategy or vector
* format. Adapters are free to chunk however they like; the framework
* only requires they round-trip these shapes when talking to the
* `IKnowledgeService`.
*
* See `content/docs/protocol/knowledge.mdx` for the full design.
*/
/**
* One logical document submitted to an adapter for indexing.
*
* For `object` sources, one ObjectQL record produces exactly one
* document. For `file` / `http` sources, the adapter is free to split
* a single file into many documents if that's the natural unit.
*/
export const KnowledgeDocumentSchema = lazySchema(() => z.object({
/** Globally-unique document id. Snake / kebab-safe. */
id: z.string().describe('Document id'),
/** Logical source this document belongs to. */
sourceId: z.string().describe('Owning KnowledgeSource id'),
/**
* Underlying ObjectQL record id when the source kind is `object`.
* Powers permission-aware retrieval: hits referencing a record are
* re-checked against the caller's `ExecutionContext`.
*/
sourceRecordId: z.string().optional(),
/** Document content. UTF-8 text. */
content: z.string(),
/** Optional human-readable title (defaults to id when absent). */
title: z.string().optional(),
/**
* Arbitrary key-value metadata. Adapters may use it for filtering
* (e.g. `{ status: 'active', tags: ['onboarding'] }`).
*/
metadata: z.record(z.string(), z.unknown()).default({}).optional(),
/**
* Optional permission descriptors for non-`object` sources. The
* framework treats this opaquely; permission-aware retrieval for
* `object` sources is handled by re-querying via the data engine.
*/
permissions: z.array(z.string()).optional(),
}));
/**
* Adapter-produced chunk of a document. The framework never sees the
* vector — only the chunk text + metadata. Used for upserts that
* pre-chunk on the caller side (rare; most adapters chunk internally).
*/
export const KnowledgeChunkSchema = lazySchema(() => z.object({
/** Chunk id (typically `${documentId}#${index}`). */
id: z.string(),
/** Owning document. */
documentId: z.string(),
/** Index within the document (0-based). */
index: z.number().int().nonnegative(),
/** Chunk text. */
content: z.string(),
/** Optional metadata override (falls back to the document's). */
metadata: z.record(z.string(), z.unknown()).optional(),
}));
/**
* A search hit returned to the caller. `score` semantics are
* adapter-specific (cosine / dot product / hybrid blend); higher is
* always better, normalised to `[0, 1]` when feasible.
*/
export const KnowledgeHitSchema = lazySchema(() => z.object({
/** Stable chunk id from the adapter. */
chunkId: z.string(),
/** Owning document id. */
documentId: z.string(),
/** Source this hit came from. */
sourceId: z.string(),
/** ObjectQL record id when applicable — used for RLS re-check. */
sourceRecordId: z.string().optional(),
/** Relevance score (higher = better). */
score: z.number(),
/** Snippet shown to the user / LLM. */
snippet: z.string(),
/** Optional title surfaced from the document. */
title: z.string().optional(),
/** Free-form metadata propagated from the document. */
metadata: z.record(z.string(), z.unknown()).default({}).optional(),
}));
export type KnowledgeDocument = z.infer<typeof KnowledgeDocumentSchema>;
export type KnowledgeChunk = z.infer<typeof KnowledgeChunkSchema>;
export type KnowledgeHit = z.infer<typeof KnowledgeHitSchema>;