@yolk-sdk/knowledge 0.1.0-canary.13 → 0.1.0-canary.130
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 +160 -80
- package/dist/agent.d.mts +118 -19
- package/dist/agent.d.mts.map +1 -1
- package/dist/agent.mjs +256 -34
- package/dist/agent.mjs.map +1 -1
- package/dist/chunking.d.mts +13 -16
- package/dist/chunking.d.mts.map +1 -1
- package/dist/chunking.mjs +4 -4
- package/dist/chunking.mjs.map +1 -1
- package/dist/context.d.mts +4 -13
- package/dist/context.d.mts.map +1 -1
- package/dist/context.mjs +17 -13
- package/dist/context.mjs.map +1 -1
- package/dist/documents.d.mts +128 -78
- package/dist/documents.d.mts.map +1 -1
- package/dist/documents.mjs +89 -61
- package/dist/documents.mjs.map +1 -1
- package/dist/embeddings.d.mts +4 -7
- package/dist/embeddings.d.mts.map +1 -1
- package/dist/embeddings.mjs +1 -2
- package/dist/embeddings.mjs.map +1 -1
- package/dist/errors.d.mts +19 -22
- package/dist/errors.d.mts.map +1 -1
- package/dist/errors.mjs +12 -13
- package/dist/errors.mjs.map +1 -1
- package/dist/extraction.d.mts +4 -7
- package/dist/extraction.d.mts.map +1 -1
- package/dist/extraction.mjs +1 -2
- package/dist/extraction.mjs.map +1 -1
- package/dist/files.d.mts +19 -0
- package/dist/files.d.mts.map +1 -0
- package/dist/files.mjs +6 -0
- package/dist/files.mjs.map +1 -0
- package/dist/index.d.mts +4 -4
- package/dist/index.mjs +4 -4
- package/dist/ingestion.d.mts +21 -23
- package/dist/ingestion.d.mts.map +1 -1
- package/dist/ingestion.mjs +12 -17
- package/dist/ingestion.mjs.map +1 -1
- package/dist/search.d.mts +13 -17
- package/dist/search.d.mts.map +1 -1
- package/dist/search.mjs +9 -12
- package/dist/search.mjs.map +1 -1
- package/dist/store.d.mts +92 -29
- package/dist/store.d.mts.map +1 -1
- package/dist/store.mjs +3 -3
- package/dist/store.mjs.map +1 -1
- package/dist/summarization.d.mts +6 -9
- package/dist/summarization.d.mts.map +1 -1
- package/dist/summarization.mjs +1 -2
- package/dist/summarization.mjs.map +1 -1
- package/package.json +11 -41
- package/src/agent.ts +393 -65
- package/src/chunking.ts +27 -8
- package/src/context.ts +21 -16
- package/src/documents.ts +140 -71
- package/src/errors.ts +4 -4
- package/src/extraction.ts +4 -3
- package/src/files.ts +24 -0
- package/src/index.ts +6 -3
- package/src/ingestion.ts +46 -27
- package/src/search.ts +90 -62
- package/src/store.ts +139 -25
- package/src/summarization.ts +4 -3
- package/dist/artifacts.d.mts +0 -37
- package/dist/artifacts.d.mts.map +0 -1
- package/dist/artifacts.mjs +0 -28
- package/dist/artifacts.mjs.map +0 -1
- package/dist/links.d.mts +0 -17
- package/dist/links.d.mts.map +0 -1
- package/dist/links.mjs +0 -24
- package/dist/links.mjs.map +0 -1
- package/dist/provenance.d.mts +0 -20
- package/dist/provenance.d.mts.map +0 -1
- package/dist/provenance.mjs +0 -26
- package/dist/provenance.mjs.map +0 -1
- package/dist/records.d.mts +0 -59
- package/dist/records.d.mts.map +0 -1
- package/dist/records.mjs +0 -64
- package/dist/records.mjs.map +0 -1
- package/dist/representations.d.mts +0 -36
- package/dist/representations.d.mts.map +0 -1
- package/dist/representations.mjs +0 -44
- package/dist/representations.mjs.map +0 -1
- package/dist/search-store.d.mts +0 -77
- package/dist/search-store.d.mts.map +0 -1
- package/dist/search-store.mjs +0 -7
- package/dist/search-store.mjs.map +0 -1
- package/dist/vector-store.d.mts +0 -3
- package/dist/vector-store.mjs +0 -2
- package/src/artifacts.ts +0 -48
- package/src/links.ts +0 -23
- package/src/provenance.ts +0 -25
- package/src/records.ts +0 -76
- package/src/representations.ts +0 -51
- package/src/search-store.ts +0 -98
- package/src/vector-store.ts +0 -6
package/src/documents.ts
CHANGED
|
@@ -1,109 +1,178 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as Schema from 'effect/Schema'
|
|
2
2
|
|
|
3
|
-
const NonEmptyTrimmedString = Schema.Trimmed.pipe(Schema.check(Schema.isNonEmpty()))
|
|
4
|
-
|
|
5
|
-
const NonNegativeInteger = Schema.Int.pipe(Schema.check(Schema.isGreaterThanOrEqualTo(0)))
|
|
3
|
+
export const NonEmptyTrimmedString = Schema.Trimmed.pipe(Schema.check(Schema.isNonEmpty()))
|
|
4
|
+
|
|
5
|
+
export const NonNegativeInteger = Schema.Int.pipe(Schema.check(Schema.isGreaterThanOrEqualTo(0)))
|
|
6
|
+
|
|
7
|
+
export const PositiveInteger = Schema.Int.pipe(Schema.check(Schema.isGreaterThan(0)))
|
|
6
8
|
|
|
7
9
|
export const KnowledgeMetadataSchema = Schema.Record(Schema.String, Schema.Unknown)
|
|
10
|
+
|
|
8
11
|
export type KnowledgeMetadata = Schema.Schema.Type<typeof KnowledgeMetadataSchema>
|
|
9
12
|
|
|
10
|
-
export const
|
|
13
|
+
export const KnowledgeAvailabilitySchema = Schema.Literals(['pinned', 'searchable', 'archived'])
|
|
14
|
+
|
|
15
|
+
export type KnowledgeAvailability = Schema.Schema.Type<typeof KnowledgeAvailabilitySchema>
|
|
16
|
+
|
|
17
|
+
export const KnowledgeDocumentStatusSchema = Schema.Literals(['processing', 'ready', 'error'])
|
|
18
|
+
|
|
11
19
|
export type KnowledgeDocumentStatus = Schema.Schema.Type<typeof KnowledgeDocumentStatusSchema>
|
|
12
20
|
|
|
13
|
-
export const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
ref: NonEmptyTrimmedString,
|
|
17
|
-
name: Schema.optional(NonEmptyTrimmedString),
|
|
18
|
-
mediaType: Schema.optional(NonEmptyTrimmedString)
|
|
19
|
-
}),
|
|
20
|
-
Schema.Struct({
|
|
21
|
-
_tag: Schema.Literal('Url'),
|
|
22
|
-
url: NonEmptyTrimmedString
|
|
23
|
-
}),
|
|
24
|
-
Schema.Struct({
|
|
25
|
-
_tag: Schema.Literal('Text'),
|
|
26
|
-
label: Schema.optional(NonEmptyTrimmedString)
|
|
27
|
-
})
|
|
28
|
-
])
|
|
29
|
-
export type KnowledgeSource = Schema.Schema.Type<typeof KnowledgeSourceSchema>
|
|
21
|
+
export const KnowledgeScopeId = NonEmptyTrimmedString.pipe(Schema.brand('KnowledgeScopeId'))
|
|
22
|
+
|
|
23
|
+
export type KnowledgeScopeId = typeof KnowledgeScopeId.Type
|
|
30
24
|
|
|
31
|
-
export const
|
|
32
|
-
|
|
33
|
-
|
|
25
|
+
export const KnowledgeDocumentId = NonEmptyTrimmedString.pipe(Schema.brand('KnowledgeDocumentId'))
|
|
26
|
+
|
|
27
|
+
export type KnowledgeDocumentId = typeof KnowledgeDocumentId.Type
|
|
28
|
+
|
|
29
|
+
export const KnowledgeScopeSchema = Schema.Struct({
|
|
30
|
+
id: KnowledgeScopeId,
|
|
31
|
+
kind: Schema.optional(NonEmptyTrimmedString)
|
|
34
32
|
})
|
|
35
|
-
export type KnowledgeEmbeddingConfig = Schema.Schema.Type<typeof KnowledgeEmbeddingConfigSchema>
|
|
36
33
|
|
|
37
|
-
export
|
|
38
|
-
|
|
39
|
-
|
|
34
|
+
export type KnowledgeScope = Schema.Schema.Type<typeof KnowledgeScopeSchema>
|
|
35
|
+
|
|
36
|
+
export const KnowledgeDocumentSchema = Schema.Struct({
|
|
37
|
+
id: KnowledgeDocumentId,
|
|
38
|
+
slug: NonEmptyTrimmedString,
|
|
39
|
+
title: NonEmptyTrimmedString,
|
|
40
|
+
purpose: NonEmptyTrimmedString,
|
|
41
|
+
origin: NonEmptyTrimmedString,
|
|
42
|
+
content: NonEmptyTrimmedString,
|
|
43
|
+
status: KnowledgeDocumentStatusSchema,
|
|
44
|
+
availability: KnowledgeAvailabilitySchema,
|
|
45
|
+
summary: Schema.optional(Schema.String),
|
|
46
|
+
errorMessage: Schema.optional(Schema.String),
|
|
47
|
+
reviewedAt: Schema.optional(Schema.DateTimeUtc),
|
|
48
|
+
metadata: Schema.optional(KnowledgeMetadataSchema),
|
|
49
|
+
createdAt: Schema.DateTimeUtc,
|
|
50
|
+
updatedAt: Schema.DateTimeUtc
|
|
40
51
|
})
|
|
41
|
-
export type KnowledgeChunkingConfig = Schema.Schema.Type<typeof KnowledgeChunkingConfigSchema>
|
|
42
52
|
|
|
43
|
-
export
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
53
|
+
export type KnowledgeDocument = Schema.Schema.Type<typeof KnowledgeDocumentSchema>
|
|
54
|
+
|
|
55
|
+
export const CreateKnowledgeDocumentInputSchema = Schema.Struct({
|
|
56
|
+
scope: KnowledgeScopeSchema,
|
|
57
|
+
slug: NonEmptyTrimmedString,
|
|
58
|
+
title: NonEmptyTrimmedString,
|
|
59
|
+
purpose: NonEmptyTrimmedString,
|
|
60
|
+
origin: NonEmptyTrimmedString,
|
|
61
|
+
content: NonEmptyTrimmedString,
|
|
62
|
+
availability: KnowledgeAvailabilitySchema,
|
|
63
|
+
summary: Schema.optional(Schema.String),
|
|
48
64
|
metadata: Schema.optional(KnowledgeMetadataSchema)
|
|
49
65
|
})
|
|
50
|
-
export type KnowledgeCollection = Schema.Schema.Type<typeof KnowledgeCollectionSchema>
|
|
51
66
|
|
|
52
|
-
export
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
67
|
+
export type CreateKnowledgeDocumentInput = Schema.Schema.Type<
|
|
68
|
+
typeof CreateKnowledgeDocumentInputSchema
|
|
69
|
+
>
|
|
70
|
+
|
|
71
|
+
export const UpdateKnowledgeDocumentInputSchema = Schema.Struct({
|
|
72
|
+
scope: KnowledgeScopeSchema,
|
|
73
|
+
id: KnowledgeDocumentId,
|
|
74
|
+
slug: Schema.optional(NonEmptyTrimmedString),
|
|
57
75
|
title: Schema.optional(NonEmptyTrimmedString),
|
|
76
|
+
purpose: Schema.optional(NonEmptyTrimmedString),
|
|
77
|
+
origin: Schema.optional(NonEmptyTrimmedString),
|
|
78
|
+
content: Schema.optional(NonEmptyTrimmedString),
|
|
79
|
+
status: Schema.optional(KnowledgeDocumentStatusSchema),
|
|
80
|
+
availability: Schema.optional(KnowledgeAvailabilitySchema),
|
|
58
81
|
summary: Schema.optional(Schema.String),
|
|
59
82
|
errorMessage: Schema.optional(Schema.String),
|
|
60
|
-
|
|
61
|
-
tokenCount: Schema.optional(NonNegativeInteger),
|
|
62
|
-
chunkCount: Schema.optional(NonNegativeInteger),
|
|
83
|
+
reviewedAt: Schema.optional(Schema.DateTimeUtc),
|
|
63
84
|
metadata: Schema.optional(KnowledgeMetadataSchema)
|
|
64
85
|
})
|
|
65
|
-
|
|
86
|
+
|
|
87
|
+
export type UpdateKnowledgeDocumentInput = Schema.Schema.Type<
|
|
88
|
+
typeof UpdateKnowledgeDocumentInputSchema
|
|
89
|
+
>
|
|
90
|
+
|
|
91
|
+
export const KnowledgeFileSchema = Schema.Struct({
|
|
92
|
+
id: NonEmptyTrimmedString,
|
|
93
|
+
documentId: KnowledgeDocumentId,
|
|
94
|
+
storageKey: NonEmptyTrimmedString,
|
|
95
|
+
mediaType: Schema.optional(NonEmptyTrimmedString),
|
|
96
|
+
byteSize: Schema.optional(NonNegativeInteger),
|
|
97
|
+
checksum: Schema.optional(NonEmptyTrimmedString),
|
|
98
|
+
metadata: Schema.optional(KnowledgeMetadataSchema),
|
|
99
|
+
createdAt: Schema.DateTimeUtc
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
export type KnowledgeFile = Schema.Schema.Type<typeof KnowledgeFileSchema>
|
|
66
103
|
|
|
67
104
|
export const KnowledgeChunkSchema = Schema.Struct({
|
|
68
105
|
id: NonEmptyTrimmedString,
|
|
69
|
-
|
|
70
|
-
documentId:
|
|
106
|
+
scopeId: KnowledgeScopeId,
|
|
107
|
+
documentId: KnowledgeDocumentId,
|
|
71
108
|
content: NonEmptyTrimmedString,
|
|
72
109
|
position: NonNegativeInteger,
|
|
73
110
|
tokenCount: PositiveInteger,
|
|
74
111
|
metadata: Schema.optional(KnowledgeMetadataSchema)
|
|
75
112
|
})
|
|
113
|
+
|
|
76
114
|
export type KnowledgeChunk = Schema.Schema.Type<typeof KnowledgeChunkSchema>
|
|
77
115
|
|
|
116
|
+
export const KnowledgeSearchScope = Schema.TaggedStruct('KnowledgeScope', {
|
|
117
|
+
id: KnowledgeScopeId
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
export const KnowledgeSearchScopes = Schema.TaggedStruct('KnowledgeScopes', {
|
|
121
|
+
ids: Schema.NonEmptyArray(KnowledgeScopeId)
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
export const KnowledgeSearchScopeSchema = Schema.Union([
|
|
125
|
+
KnowledgeSearchScope,
|
|
126
|
+
KnowledgeSearchScopes
|
|
127
|
+
])
|
|
128
|
+
|
|
129
|
+
export type KnowledgeSearchScope = typeof KnowledgeSearchScopeSchema.Type
|
|
130
|
+
|
|
131
|
+
export const KnowledgeFileSource = Schema.TaggedStruct('File', {
|
|
132
|
+
ref: NonEmptyTrimmedString,
|
|
133
|
+
name: Schema.optional(NonEmptyTrimmedString),
|
|
134
|
+
mediaType: Schema.optional(NonEmptyTrimmedString)
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
export const KnowledgeUrlSource = Schema.TaggedStruct('Url', {
|
|
138
|
+
url: NonEmptyTrimmedString
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
export const KnowledgeTextSource = Schema.TaggedStruct('Text', {
|
|
142
|
+
label: Schema.optional(NonEmptyTrimmedString)
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
export const KnowledgeSourceSchema = Schema.Union([
|
|
146
|
+
KnowledgeFileSource,
|
|
147
|
+
KnowledgeUrlSource,
|
|
148
|
+
KnowledgeTextSource
|
|
149
|
+
])
|
|
150
|
+
|
|
151
|
+
export type KnowledgeSource = typeof KnowledgeSourceSchema.Type
|
|
152
|
+
|
|
153
|
+
export const IndexedKnowledgeDocumentSchema = Schema.Struct({
|
|
154
|
+
id: KnowledgeDocumentId,
|
|
155
|
+
scopeId: KnowledgeScopeId,
|
|
156
|
+
source: KnowledgeSourceSchema,
|
|
157
|
+
status: KnowledgeDocumentStatusSchema,
|
|
158
|
+
title: Schema.optional(NonEmptyTrimmedString),
|
|
159
|
+
summary: Schema.optional(Schema.String),
|
|
160
|
+
errorMessage: Schema.optional(Schema.String),
|
|
161
|
+
contentHash: Schema.optional(NonEmptyTrimmedString),
|
|
162
|
+
tokenCount: Schema.optional(NonNegativeInteger),
|
|
163
|
+
chunkCount: Schema.optional(NonNegativeInteger),
|
|
164
|
+
metadata: Schema.optional(KnowledgeMetadataSchema)
|
|
165
|
+
})
|
|
166
|
+
|
|
167
|
+
export type IndexedKnowledgeDocument = Schema.Schema.Type<typeof IndexedKnowledgeDocumentSchema>
|
|
168
|
+
|
|
78
169
|
export const ExtractedKnowledgeDocumentSchema = Schema.Struct({
|
|
79
170
|
content: NonEmptyTrimmedString,
|
|
80
171
|
title: Schema.optional(NonEmptyTrimmedString),
|
|
81
172
|
summary: Schema.optional(Schema.String),
|
|
82
173
|
metadata: Schema.optional(KnowledgeMetadataSchema)
|
|
83
174
|
})
|
|
175
|
+
|
|
84
176
|
export type ExtractedKnowledgeDocument = Schema.Schema.Type<typeof ExtractedKnowledgeDocumentSchema>
|
|
85
177
|
|
|
86
|
-
export const
|
|
87
|
-
Schema.Struct({ _tag: Schema.Literal('KnowledgeCollection'), id: NonEmptyTrimmedString }),
|
|
88
|
-
Schema.Struct({ _tag: Schema.Literal('KnowledgeCollections'), ids: Schema.Array(NonEmptyTrimmedString) })
|
|
89
|
-
])
|
|
90
|
-
export type KnowledgeSearchScope = Schema.Schema.Type<typeof KnowledgeSearchScopeSchema>
|
|
91
|
-
|
|
92
|
-
export const defaultKnowledgeChunkingConfig: KnowledgeChunkingConfig = {
|
|
93
|
-
strategy: 'sentence-token',
|
|
94
|
-
maxTokens: 512
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
export const makeKnowledgeCollection = (input: {
|
|
98
|
-
readonly id: string
|
|
99
|
-
readonly label?: string
|
|
100
|
-
readonly embeddingConfig: KnowledgeEmbeddingConfig
|
|
101
|
-
readonly chunkingConfig?: KnowledgeChunkingConfig
|
|
102
|
-
readonly metadata?: KnowledgeMetadata
|
|
103
|
-
}): KnowledgeCollection => ({
|
|
104
|
-
id: input.id,
|
|
105
|
-
label: input.label,
|
|
106
|
-
embeddingConfig: input.embeddingConfig,
|
|
107
|
-
chunkingConfig: input.chunkingConfig ?? defaultKnowledgeChunkingConfig,
|
|
108
|
-
metadata: input.metadata
|
|
109
|
-
})
|
|
178
|
+
export const defaultKnowledgeChunkMaxTokens = 512
|
package/src/errors.ts
CHANGED
|
@@ -38,7 +38,7 @@ export class KnowledgeSearchError extends Data.TaggedError('KnowledgeSearchError
|
|
|
38
38
|
readonly cause?: unknown
|
|
39
39
|
}> {}
|
|
40
40
|
|
|
41
|
-
export class KnowledgeStoreError extends Schema.
|
|
41
|
+
export class KnowledgeStoreError extends Schema.TaggedError<KnowledgeStoreError>()(
|
|
42
42
|
'KnowledgeStoreError',
|
|
43
43
|
{
|
|
44
44
|
message: Schema.String,
|
|
@@ -46,15 +46,15 @@ export class KnowledgeStoreError extends Schema.TaggedErrorClass<KnowledgeStoreE
|
|
|
46
46
|
}
|
|
47
47
|
) {}
|
|
48
48
|
|
|
49
|
-
export class
|
|
50
|
-
'
|
|
49
|
+
export class KnowledgeFileError extends Schema.TaggedError<KnowledgeFileError>()(
|
|
50
|
+
'KnowledgeFileError',
|
|
51
51
|
{
|
|
52
52
|
message: Schema.String,
|
|
53
53
|
cause: Schema.optional(Schema.Unknown)
|
|
54
54
|
}
|
|
55
55
|
) {}
|
|
56
56
|
|
|
57
|
-
export class KnowledgeContextError extends Schema.
|
|
57
|
+
export class KnowledgeContextError extends Schema.TaggedError<KnowledgeContextError>()(
|
|
58
58
|
'KnowledgeContextError',
|
|
59
59
|
{
|
|
60
60
|
message: Schema.String,
|
package/src/extraction.ts
CHANGED
|
@@ -16,6 +16,7 @@ export type KnowledgeExtractorApi = {
|
|
|
16
16
|
) => Effect.Effect<ExtractedKnowledgeDocument, KnowledgeExtractionError>
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
export class KnowledgeExtractor extends Context.Service<
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
export class KnowledgeExtractor extends Context.Service<
|
|
20
|
+
KnowledgeExtractor,
|
|
21
|
+
KnowledgeExtractorApi
|
|
22
|
+
>()('@yolk-sdk/knowledge/KnowledgeExtractor') {}
|
package/src/files.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Context } from 'effect'
|
|
2
|
+
import type { Effect } from 'effect'
|
|
3
|
+
import type { KnowledgeFileError } from './errors.ts'
|
|
4
|
+
|
|
5
|
+
export type PutKnowledgeFileInput = {
|
|
6
|
+
readonly storageKey: string
|
|
7
|
+
readonly mediaType?: string
|
|
8
|
+
readonly bytes: Uint8Array
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export type GetKnowledgeFileInput = {
|
|
12
|
+
readonly storageKey: string
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type KnowledgeFileBlobStoreApi = {
|
|
16
|
+
readonly putFile: (input: PutKnowledgeFileInput) => Effect.Effect<void, KnowledgeFileError>
|
|
17
|
+
readonly getFile: (input: GetKnowledgeFileInput) => Effect.Effect<Uint8Array, KnowledgeFileError>
|
|
18
|
+
readonly deleteFile: (input: GetKnowledgeFileInput) => Effect.Effect<void, KnowledgeFileError>
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export class KnowledgeFileBlobStore extends Context.Service<
|
|
22
|
+
KnowledgeFileBlobStore,
|
|
23
|
+
KnowledgeFileBlobStoreApi
|
|
24
|
+
>()('@yolk-sdk/knowledge/KnowledgeFileBlobStore') {}
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
export { buildKnowledgeContext } from './context.ts'
|
|
2
|
-
|
|
3
|
-
export {
|
|
4
|
-
|
|
2
|
+
|
|
3
|
+
export { KnowledgeStore, SearchIndexStore } from './store.ts'
|
|
4
|
+
|
|
5
|
+
export { KnowledgeFileBlobStore } from './files.ts'
|
|
6
|
+
|
|
7
|
+
export { KnowledgeStoreError, KnowledgeFileError, KnowledgeContextError } from './errors.ts'
|
package/src/ingestion.ts
CHANGED
|
@@ -1,24 +1,27 @@
|
|
|
1
1
|
import { Array as Arr, Effect } from 'effect'
|
|
2
2
|
import { KnowledgeChunker } from './chunking.ts'
|
|
3
|
+
import { defaultKnowledgeChunkMaxTokens } from './documents.ts'
|
|
4
|
+
import type { KnowledgeDocumentId, KnowledgeScopeId } from './documents.ts'
|
|
3
5
|
import { KnowledgeEmbedder } from './embeddings.ts'
|
|
4
6
|
import { KnowledgeExtractor } from './extraction.ts'
|
|
5
7
|
import { KnowledgeSummarizer } from './summarization.ts'
|
|
6
8
|
import type { LoadedKnowledgeSource } from './extraction.ts'
|
|
7
9
|
import { KnowledgeIngestionError } from './errors.ts'
|
|
8
|
-
import { SearchIndexStore } from './
|
|
10
|
+
import { SearchIndexStore } from './store.ts'
|
|
9
11
|
|
|
10
12
|
export type IngestKnowledgeDocumentInput = {
|
|
11
|
-
readonly
|
|
12
|
-
readonly documentId:
|
|
13
|
+
readonly scopeId: KnowledgeScopeId
|
|
14
|
+
readonly documentId: KnowledgeDocumentId
|
|
13
15
|
readonly source: LoadedKnowledgeSource
|
|
14
16
|
readonly contentHash?: string
|
|
17
|
+
readonly maxTokens?: number
|
|
15
18
|
}
|
|
16
19
|
|
|
17
20
|
const markErrorBestEffort = (input: IngestKnowledgeDocumentInput, error: KnowledgeIngestionError) =>
|
|
18
21
|
Effect.gen(function* () {
|
|
19
22
|
const store = yield* SearchIndexStore
|
|
20
23
|
yield* store.markDocumentError({
|
|
21
|
-
|
|
24
|
+
scopeId: input.scopeId,
|
|
22
25
|
documentId: input.documentId,
|
|
23
26
|
message: error.message
|
|
24
27
|
})
|
|
@@ -27,7 +30,7 @@ const markErrorBestEffort = (input: IngestKnowledgeDocumentInput, error: Knowled
|
|
|
27
30
|
export const ingestKnowledgeDocument = (input: IngestKnowledgeDocumentInput) =>
|
|
28
31
|
Effect.gen(function* () {
|
|
29
32
|
yield* Effect.annotateCurrentSpan({
|
|
30
|
-
'knowledge_search.
|
|
33
|
+
'knowledge_search.scope_id': input.scopeId,
|
|
31
34
|
'knowledge_search.document_id': input.documentId,
|
|
32
35
|
'knowledge_search.source_type': input.source.source._tag
|
|
33
36
|
})
|
|
@@ -36,19 +39,12 @@ export const ingestKnowledgeDocument = (input: IngestKnowledgeDocumentInput) =>
|
|
|
36
39
|
const chunker = yield* KnowledgeChunker
|
|
37
40
|
const embedder = yield* KnowledgeEmbedder
|
|
38
41
|
const summarizer = yield* KnowledgeSummarizer
|
|
39
|
-
const collection = yield* store
|
|
40
|
-
.getSet(input.collectionId)
|
|
41
|
-
.pipe(
|
|
42
|
-
Effect.mapError(
|
|
43
|
-
error => new KnowledgeIngestionError({ message: error.message, stage: 'store', cause: error })
|
|
44
|
-
)
|
|
45
|
-
)
|
|
46
42
|
|
|
47
43
|
yield* store
|
|
48
44
|
.upsertDocument({
|
|
49
45
|
document: {
|
|
50
46
|
id: input.documentId,
|
|
51
|
-
|
|
47
|
+
scopeId: input.scopeId,
|
|
52
48
|
source: input.source.source,
|
|
53
49
|
status: 'processing',
|
|
54
50
|
metadata: input.source.metadata
|
|
@@ -56,7 +52,8 @@ export const ingestKnowledgeDocument = (input: IngestKnowledgeDocumentInput) =>
|
|
|
56
52
|
})
|
|
57
53
|
.pipe(
|
|
58
54
|
Effect.mapError(
|
|
59
|
-
error =>
|
|
55
|
+
error =>
|
|
56
|
+
new KnowledgeIngestionError({ message: error.message, stage: 'store', cause: error })
|
|
60
57
|
)
|
|
61
58
|
)
|
|
62
59
|
|
|
@@ -64,21 +61,23 @@ export const ingestKnowledgeDocument = (input: IngestKnowledgeDocumentInput) =>
|
|
|
64
61
|
.extract(input.source)
|
|
65
62
|
.pipe(
|
|
66
63
|
Effect.mapError(
|
|
67
|
-
error =>
|
|
64
|
+
error =>
|
|
65
|
+
new KnowledgeIngestionError({ message: error.message, stage: 'extract', cause: error })
|
|
68
66
|
)
|
|
69
67
|
)
|
|
70
68
|
|
|
71
69
|
const chunks = yield* chunker
|
|
72
70
|
.chunk({
|
|
73
|
-
|
|
71
|
+
scopeId: input.scopeId,
|
|
74
72
|
documentId: input.documentId,
|
|
75
73
|
content: extracted.content,
|
|
76
|
-
maxTokens:
|
|
74
|
+
maxTokens: input.maxTokens ?? defaultKnowledgeChunkMaxTokens,
|
|
77
75
|
metadata: extracted.metadata
|
|
78
76
|
})
|
|
79
77
|
.pipe(
|
|
80
78
|
Effect.mapError(
|
|
81
|
-
error =>
|
|
79
|
+
error =>
|
|
80
|
+
new KnowledgeIngestionError({ message: error.message, stage: 'chunk', cause: error })
|
|
82
81
|
)
|
|
83
82
|
)
|
|
84
83
|
|
|
@@ -86,7 +85,12 @@ export const ingestKnowledgeDocument = (input: IngestKnowledgeDocumentInput) =>
|
|
|
86
85
|
{
|
|
87
86
|
embeddings: embedder.embedTexts(chunks.map(chunk => chunk.content)).pipe(
|
|
88
87
|
Effect.mapError(
|
|
89
|
-
error =>
|
|
88
|
+
error =>
|
|
89
|
+
new KnowledgeIngestionError({
|
|
90
|
+
message: error.message,
|
|
91
|
+
stage: 'embed',
|
|
92
|
+
cause: error
|
|
93
|
+
})
|
|
90
94
|
)
|
|
91
95
|
),
|
|
92
96
|
summary: summarizer
|
|
@@ -98,7 +102,11 @@ export const ingestKnowledgeDocument = (input: IngestKnowledgeDocumentInput) =>
|
|
|
98
102
|
.pipe(
|
|
99
103
|
Effect.mapError(
|
|
100
104
|
error =>
|
|
101
|
-
new KnowledgeIngestionError({
|
|
105
|
+
new KnowledgeIngestionError({
|
|
106
|
+
message: error.message,
|
|
107
|
+
stage: 'summarize',
|
|
108
|
+
cause: error
|
|
109
|
+
})
|
|
102
110
|
)
|
|
103
111
|
)
|
|
104
112
|
},
|
|
@@ -107,7 +115,10 @@ export const ingestKnowledgeDocument = (input: IngestKnowledgeDocumentInput) =>
|
|
|
107
115
|
|
|
108
116
|
if (indexed.embeddings.length !== chunks.length) {
|
|
109
117
|
return yield* Effect.fail(
|
|
110
|
-
new KnowledgeIngestionError({
|
|
118
|
+
new KnowledgeIngestionError({
|
|
119
|
+
message: 'Embedding count did not match chunk count',
|
|
120
|
+
stage: 'embed'
|
|
121
|
+
})
|
|
111
122
|
)
|
|
112
123
|
}
|
|
113
124
|
|
|
@@ -115,22 +126,25 @@ export const ingestKnowledgeDocument = (input: IngestKnowledgeDocumentInput) =>
|
|
|
115
126
|
chunk,
|
|
116
127
|
embedding
|
|
117
128
|
}))
|
|
129
|
+
|
|
118
130
|
yield* store
|
|
119
131
|
.replaceDocumentChunks({
|
|
120
|
-
|
|
132
|
+
scopeId: input.scopeId,
|
|
121
133
|
documentId: input.documentId,
|
|
122
134
|
chunks: indexedChunks
|
|
123
135
|
})
|
|
124
136
|
.pipe(
|
|
125
137
|
Effect.mapError(
|
|
126
|
-
error =>
|
|
138
|
+
error =>
|
|
139
|
+
new KnowledgeIngestionError({ message: error.message, stage: 'store', cause: error })
|
|
127
140
|
)
|
|
128
141
|
)
|
|
129
142
|
|
|
130
143
|
const tokenCount = chunks.reduce((total, chunk) => total + chunk.tokenCount, 0)
|
|
144
|
+
|
|
131
145
|
return yield* store
|
|
132
146
|
.markDocumentReady({
|
|
133
|
-
|
|
147
|
+
scopeId: input.scopeId,
|
|
134
148
|
documentId: input.documentId,
|
|
135
149
|
title: indexed.summary.title ?? extracted.title,
|
|
136
150
|
summary: indexed.summary.summary ?? extracted.summary,
|
|
@@ -140,16 +154,21 @@ export const ingestKnowledgeDocument = (input: IngestKnowledgeDocumentInput) =>
|
|
|
140
154
|
})
|
|
141
155
|
.pipe(
|
|
142
156
|
Effect.mapError(
|
|
143
|
-
error =>
|
|
157
|
+
error =>
|
|
158
|
+
new KnowledgeIngestionError({ message: error.message, stage: 'store', cause: error })
|
|
144
159
|
)
|
|
145
160
|
)
|
|
146
161
|
}).pipe(
|
|
147
162
|
Effect.withSpan('knowledge_search.ingestDocument'),
|
|
148
|
-
Effect.catch(error =>
|
|
163
|
+
Effect.catch(error =>
|
|
164
|
+
markErrorBestEffort(input, error).pipe(Effect.flatMap(() => Effect.fail(error)))
|
|
165
|
+
)
|
|
149
166
|
)
|
|
150
167
|
|
|
151
168
|
export type KnowledgeIngestionPipeline = {
|
|
152
|
-
readonly ingest: (
|
|
169
|
+
readonly ingest: (
|
|
170
|
+
input: IngestKnowledgeDocumentInput
|
|
171
|
+
) => ReturnType<typeof ingestKnowledgeDocument>
|
|
153
172
|
}
|
|
154
173
|
|
|
155
174
|
export const makeIngestionPipeline = (): KnowledgeIngestionPipeline => ({
|