@diyor28/context 1.0.0
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 +270 -0
- package/dist/__tests__/attachment-selector.test.d.ts +11 -0
- package/dist/__tests__/attachment-selector.test.d.ts.map +1 -0
- package/dist/__tests__/attachment-selector.test.js +449 -0
- package/dist/__tests__/attachment-selector.test.js.map +1 -0
- package/dist/__tests__/cache-breakpoints.test.d.ts +11 -0
- package/dist/__tests__/cache-breakpoints.test.d.ts.map +1 -0
- package/dist/__tests__/cache-breakpoints.test.js +398 -0
- package/dist/__tests__/cache-breakpoints.test.js.map +1 -0
- package/dist/__tests__/codecs.test.d.ts +7 -0
- package/dist/__tests__/codecs.test.d.ts.map +1 -0
- package/dist/__tests__/codecs.test.js +331 -0
- package/dist/__tests__/codecs.test.js.map +1 -0
- package/dist/__tests__/compactor.test.d.ts +11 -0
- package/dist/__tests__/compactor.test.d.ts.map +1 -0
- package/dist/__tests__/compactor.test.js +519 -0
- package/dist/__tests__/compactor.test.js.map +1 -0
- package/dist/__tests__/context-graph.test.d.ts +7 -0
- package/dist/__tests__/context-graph.test.d.ts.map +1 -0
- package/dist/__tests__/context-graph.test.js +262 -0
- package/dist/__tests__/context-graph.test.js.map +1 -0
- package/dist/__tests__/hash.test.d.ts +7 -0
- package/dist/__tests__/hash.test.d.ts.map +1 -0
- package/dist/__tests__/hash.test.js +228 -0
- package/dist/__tests__/hash.test.js.map +1 -0
- package/dist/__tests__/integration.test.d.ts +15 -0
- package/dist/__tests__/integration.test.d.ts.map +1 -0
- package/dist/__tests__/integration.test.js +728 -0
- package/dist/__tests__/integration.test.js.map +1 -0
- package/dist/__tests__/kind-order.test.d.ts +7 -0
- package/dist/__tests__/kind-order.test.d.ts.map +1 -0
- package/dist/__tests__/kind-order.test.js +243 -0
- package/dist/__tests__/kind-order.test.js.map +1 -0
- package/dist/__tests__/phase2-integration.test.d.ts +5 -0
- package/dist/__tests__/phase2-integration.test.d.ts.map +1 -0
- package/dist/__tests__/phase2-integration.test.js +222 -0
- package/dist/__tests__/phase2-integration.test.js.map +1 -0
- package/dist/__tests__/queries.test.d.ts +7 -0
- package/dist/__tests__/queries.test.d.ts.map +1 -0
- package/dist/__tests__/queries.test.js +254 -0
- package/dist/__tests__/queries.test.js.map +1 -0
- package/dist/__tests__/token-estimator.test.d.ts +7 -0
- package/dist/__tests__/token-estimator.test.d.ts.map +1 -0
- package/dist/__tests__/token-estimator.test.js +267 -0
- package/dist/__tests__/token-estimator.test.js.map +1 -0
- package/dist/adapters/anthropic-estimator.d.ts +38 -0
- package/dist/adapters/anthropic-estimator.d.ts.map +1 -0
- package/dist/adapters/anthropic-estimator.js +108 -0
- package/dist/adapters/anthropic-estimator.js.map +1 -0
- package/dist/adapters/attachment-resolver.d.ts +96 -0
- package/dist/adapters/attachment-resolver.d.ts.map +1 -0
- package/dist/adapters/attachment-resolver.js +176 -0
- package/dist/adapters/attachment-resolver.js.map +1 -0
- package/dist/adapters/attachment-selector.d.ts +59 -0
- package/dist/adapters/attachment-selector.d.ts.map +1 -0
- package/dist/adapters/attachment-selector.js +163 -0
- package/dist/adapters/attachment-selector.js.map +1 -0
- package/dist/adapters/gemini-estimator.d.ts +27 -0
- package/dist/adapters/gemini-estimator.d.ts.map +1 -0
- package/dist/adapters/gemini-estimator.js +80 -0
- package/dist/adapters/gemini-estimator.js.map +1 -0
- package/dist/adapters/index.d.ts +12 -0
- package/dist/adapters/index.d.ts.map +1 -0
- package/dist/adapters/index.js +28 -0
- package/dist/adapters/index.js.map +1 -0
- package/dist/adapters/memory-store.d.ts +139 -0
- package/dist/adapters/memory-store.d.ts.map +1 -0
- package/dist/adapters/memory-store.js +187 -0
- package/dist/adapters/memory-store.js.map +1 -0
- package/dist/adapters/openai-estimator.d.ts +35 -0
- package/dist/adapters/openai-estimator.d.ts.map +1 -0
- package/dist/adapters/openai-estimator.js +89 -0
- package/dist/adapters/openai-estimator.js.map +1 -0
- package/dist/adapters/summarizer.d.ts +121 -0
- package/dist/adapters/summarizer.d.ts.map +1 -0
- package/dist/adapters/summarizer.js +121 -0
- package/dist/adapters/summarizer.js.map +1 -0
- package/dist/adapters/token-estimator.d.ts +63 -0
- package/dist/adapters/token-estimator.d.ts.map +1 -0
- package/dist/adapters/token-estimator.js +37 -0
- package/dist/adapters/token-estimator.js.map +1 -0
- package/dist/builder/context-builder.d.ts +186 -0
- package/dist/builder/context-builder.d.ts.map +1 -0
- package/dist/builder/context-builder.js +305 -0
- package/dist/builder/context-builder.js.map +1 -0
- package/dist/builder/context-fork.d.ts +166 -0
- package/dist/builder/context-fork.d.ts.map +1 -0
- package/dist/builder/context-fork.js +282 -0
- package/dist/builder/context-fork.js.map +1 -0
- package/dist/builder/index.d.ts +6 -0
- package/dist/builder/index.d.ts.map +1 -0
- package/dist/builder/index.js +22 -0
- package/dist/builder/index.js.map +1 -0
- package/dist/codecs/base.d.ts +18 -0
- package/dist/codecs/base.d.ts.map +1 -0
- package/dist/codecs/base.js +39 -0
- package/dist/codecs/base.js.map +1 -0
- package/dist/codecs/conversation-history.codec.d.ts +81 -0
- package/dist/codecs/conversation-history.codec.d.ts.map +1 -0
- package/dist/codecs/conversation-history.codec.js +89 -0
- package/dist/codecs/conversation-history.codec.js.map +1 -0
- package/dist/codecs/index.d.ts +31 -0
- package/dist/codecs/index.d.ts.map +1 -0
- package/dist/codecs/index.js +71 -0
- package/dist/codecs/index.js.map +1 -0
- package/dist/codecs/redacted-stub.codec.d.ts +32 -0
- package/dist/codecs/redacted-stub.codec.d.ts.map +1 -0
- package/dist/codecs/redacted-stub.codec.js +64 -0
- package/dist/codecs/redacted-stub.codec.js.map +1 -0
- package/dist/codecs/structured-reference.codec.d.ts +40 -0
- package/dist/codecs/structured-reference.codec.d.ts.map +1 -0
- package/dist/codecs/structured-reference.codec.js +81 -0
- package/dist/codecs/structured-reference.codec.js.map +1 -0
- package/dist/codecs/system-rules.codec.d.ts +32 -0
- package/dist/codecs/system-rules.codec.d.ts.map +1 -0
- package/dist/codecs/system-rules.codec.js +62 -0
- package/dist/codecs/system-rules.codec.js.map +1 -0
- package/dist/codecs/tool-output.codec.d.ts +66 -0
- package/dist/codecs/tool-output.codec.d.ts.map +1 -0
- package/dist/codecs/tool-output.codec.js +95 -0
- package/dist/codecs/tool-output.codec.js.map +1 -0
- package/dist/codecs/tool-schema.codec.d.ts +36 -0
- package/dist/codecs/tool-schema.codec.d.ts.map +1 -0
- package/dist/codecs/tool-schema.codec.js +74 -0
- package/dist/codecs/tool-schema.codec.js.map +1 -0
- package/dist/codecs/unsafe-text.codec.d.ts +28 -0
- package/dist/codecs/unsafe-text.codec.d.ts.map +1 -0
- package/dist/codecs/unsafe-text.codec.js +63 -0
- package/dist/codecs/unsafe-text.codec.js.map +1 -0
- package/dist/graph/context-graph.d.ts +121 -0
- package/dist/graph/context-graph.d.ts.map +1 -0
- package/dist/graph/context-graph.js +166 -0
- package/dist/graph/context-graph.js.map +1 -0
- package/dist/graph/index.d.ts +8 -0
- package/dist/graph/index.d.ts.map +1 -0
- package/dist/graph/index.js +24 -0
- package/dist/graph/index.js.map +1 -0
- package/dist/graph/kind-order.d.ts +60 -0
- package/dist/graph/kind-order.d.ts.map +1 -0
- package/dist/graph/kind-order.js +113 -0
- package/dist/graph/kind-order.js.map +1 -0
- package/dist/graph/queries.d.ts +68 -0
- package/dist/graph/queries.d.ts.map +1 -0
- package/dist/graph/queries.js +240 -0
- package/dist/graph/queries.js.map +1 -0
- package/dist/graph/views.d.ts +90 -0
- package/dist/graph/views.d.ts.map +1 -0
- package/dist/graph/views.js +173 -0
- package/dist/graph/views.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +40 -0
- package/dist/index.js.map +1 -0
- package/dist/pipeline/compactor.d.ts +128 -0
- package/dist/pipeline/compactor.d.ts.map +1 -0
- package/dist/pipeline/compactor.js +346 -0
- package/dist/pipeline/compactor.js.map +1 -0
- package/dist/pipeline/index.d.ts +6 -0
- package/dist/pipeline/index.d.ts.map +1 -0
- package/dist/pipeline/index.js +22 -0
- package/dist/pipeline/index.js.map +1 -0
- package/dist/pipeline/summarizer.d.ts +18 -0
- package/dist/pipeline/summarizer.d.ts.map +1 -0
- package/dist/pipeline/summarizer.js +68 -0
- package/dist/pipeline/summarizer.js.map +1 -0
- package/dist/policies/default-policy.d.ts +29 -0
- package/dist/policies/default-policy.d.ts.map +1 -0
- package/dist/policies/default-policy.js +58 -0
- package/dist/policies/default-policy.js.map +1 -0
- package/dist/policies/index.d.ts +5 -0
- package/dist/policies/index.d.ts.map +1 -0
- package/dist/policies/index.js +21 -0
- package/dist/policies/index.js.map +1 -0
- package/dist/providers/anthropic-compiler.d.ts +58 -0
- package/dist/providers/anthropic-compiler.d.ts.map +1 -0
- package/dist/providers/anthropic-compiler.js +182 -0
- package/dist/providers/anthropic-compiler.js.map +1 -0
- package/dist/providers/capabilities.d.ts +54 -0
- package/dist/providers/capabilities.d.ts.map +1 -0
- package/dist/providers/capabilities.js +87 -0
- package/dist/providers/capabilities.js.map +1 -0
- package/dist/providers/gemini-compiler.d.ts +51 -0
- package/dist/providers/gemini-compiler.d.ts.map +1 -0
- package/dist/providers/gemini-compiler.js +206 -0
- package/dist/providers/gemini-compiler.js.map +1 -0
- package/dist/providers/index.d.ts +8 -0
- package/dist/providers/index.d.ts.map +1 -0
- package/dist/providers/index.js +24 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/providers/openai-compiler.d.ts +46 -0
- package/dist/providers/openai-compiler.d.ts.map +1 -0
- package/dist/providers/openai-compiler.js +149 -0
- package/dist/providers/openai-compiler.js.map +1 -0
- package/dist/types/attachment.d.ts +62 -0
- package/dist/types/attachment.d.ts.map +1 -0
- package/dist/types/attachment.js +6 -0
- package/dist/types/attachment.js.map +1 -0
- package/dist/types/block.d.ts +61 -0
- package/dist/types/block.d.ts.map +1 -0
- package/dist/types/block.js +8 -0
- package/dist/types/block.js.map +1 -0
- package/dist/types/codec.d.ts +58 -0
- package/dist/types/codec.d.ts.map +1 -0
- package/dist/types/codec.js +6 -0
- package/dist/types/codec.js.map +1 -0
- package/dist/types/compiled.d.ts +91 -0
- package/dist/types/compiled.d.ts.map +1 -0
- package/dist/types/compiled.js +6 -0
- package/dist/types/compiled.js.map +1 -0
- package/dist/types/hash.d.ts +24 -0
- package/dist/types/hash.d.ts.map +1 -0
- package/dist/types/hash.js +49 -0
- package/dist/types/hash.js.map +1 -0
- package/dist/types/index.d.ts +10 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +26 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/policy.d.ts +128 -0
- package/dist/types/policy.d.ts.map +1 -0
- package/dist/types/policy.js +55 -0
- package/dist/types/policy.js.map +1 -0
- package/package.json +55 -0
- package/postcss.config.js +4 -0
- package/src/__tests__/attachment-selector.test.ts +559 -0
- package/src/__tests__/cache-breakpoints.test.ts +566 -0
- package/src/__tests__/codecs.test.ts +417 -0
- package/src/__tests__/compactor.test.ts +608 -0
- package/src/__tests__/context-graph.test.ts +383 -0
- package/src/__tests__/hash.test.ts +274 -0
- package/src/__tests__/integration.test.ts +866 -0
- package/src/__tests__/kind-order.test.ts +312 -0
- package/src/__tests__/phase2-integration.test.ts +253 -0
- package/src/__tests__/queries.test.ts +387 -0
- package/src/__tests__/token-estimator.test.ts +326 -0
- package/src/adapters/anthropic-estimator.ts +125 -0
- package/src/adapters/attachment-resolver.ts +295 -0
- package/src/adapters/attachment-selector.ts +218 -0
- package/src/adapters/gemini-estimator.ts +93 -0
- package/src/adapters/index.ts +12 -0
- package/src/adapters/memory-store.ts +299 -0
- package/src/adapters/openai-estimator.ts +105 -0
- package/src/adapters/summarizer.ts +250 -0
- package/src/adapters/token-estimator.ts +74 -0
- package/src/builder/context-builder.ts +467 -0
- package/src/builder/context-fork.ts +471 -0
- package/src/builder/index.ts +6 -0
- package/src/codecs/base.ts +36 -0
- package/src/codecs/conversation-history.codec.ts +108 -0
- package/src/codecs/index.ts +57 -0
- package/src/codecs/redacted-stub.codec.ts +76 -0
- package/src/codecs/structured-reference.codec.ts +96 -0
- package/src/codecs/system-rules.codec.ts +74 -0
- package/src/codecs/tool-output.codec.ts +109 -0
- package/src/codecs/tool-schema.codec.ts +87 -0
- package/src/codecs/unsafe-text.codec.ts +74 -0
- package/src/graph/context-graph.ts +205 -0
- package/src/graph/index.ts +8 -0
- package/src/graph/kind-order.ts +125 -0
- package/src/graph/queries.ts +306 -0
- package/src/graph/views.ts +255 -0
- package/src/index.ts +31 -0
- package/src/pipeline/compactor.ts +563 -0
- package/src/pipeline/index.ts +6 -0
- package/src/pipeline/summarizer.ts +76 -0
- package/src/policies/default-policy.ts +69 -0
- package/src/policies/index.ts +5 -0
- package/src/providers/anthropic-compiler.ts +294 -0
- package/src/providers/capabilities.ts +144 -0
- package/src/providers/gemini-compiler.ts +272 -0
- package/src/providers/index.ts +8 -0
- package/src/providers/openai-compiler.ts +191 -0
- package/src/types/attachment.ts +86 -0
- package/src/types/block.ts +84 -0
- package/src/types/codec.ts +68 -0
- package/src/types/compiled.ts +109 -0
- package/src/types/hash.ts +58 -0
- package/src/types/index.ts +10 -0
- package/src/types/policy.ts +194 -0
- package/tsconfig.json +21 -0
- package/vitest.config.ts +21 -0
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Tool schema codec (kind: 'reference').
|
|
4
|
+
*
|
|
5
|
+
* Tool definitions for function calling.
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.ToolSchemaCodec = exports.ToolSchemaPayloadSchema = void 0;
|
|
9
|
+
const zod_1 = require("zod");
|
|
10
|
+
const base_js_1 = require("./base.js");
|
|
11
|
+
/**
|
|
12
|
+
* Tool schema payload schema (JSON Schema-like).
|
|
13
|
+
*/
|
|
14
|
+
exports.ToolSchemaPayloadSchema = zod_1.z.object({
|
|
15
|
+
/** Tool name */
|
|
16
|
+
name: zod_1.z.string(),
|
|
17
|
+
/** Tool description */
|
|
18
|
+
description: zod_1.z.string(),
|
|
19
|
+
/** Input schema (JSON Schema) */
|
|
20
|
+
inputSchema: zod_1.z.record(zod_1.z.unknown()),
|
|
21
|
+
/** Optional cache control hint */
|
|
22
|
+
cacheable: zod_1.z.boolean().optional(),
|
|
23
|
+
});
|
|
24
|
+
/**
|
|
25
|
+
* Tool schema codec implementation.
|
|
26
|
+
*/
|
|
27
|
+
exports.ToolSchemaCodec = {
|
|
28
|
+
codecId: 'tool-schema',
|
|
29
|
+
version: '1.0.0',
|
|
30
|
+
payloadSchema: exports.ToolSchemaPayloadSchema,
|
|
31
|
+
canonicalize(payload) {
|
|
32
|
+
// Sort keys for deterministic hashing
|
|
33
|
+
return (0, base_js_1.sortObjectKeys)({
|
|
34
|
+
name: payload.name,
|
|
35
|
+
description: payload.description.trim(),
|
|
36
|
+
inputSchema: (0, base_js_1.sortObjectKeys)(payload.inputSchema),
|
|
37
|
+
cacheable: payload.cacheable ?? false,
|
|
38
|
+
});
|
|
39
|
+
},
|
|
40
|
+
hash(canonicalized) {
|
|
41
|
+
return (0, base_js_1.defaultHash)(canonicalized);
|
|
42
|
+
},
|
|
43
|
+
render(block) {
|
|
44
|
+
const { name, description, inputSchema, cacheable } = block.payload;
|
|
45
|
+
return {
|
|
46
|
+
// Anthropic: tool definition
|
|
47
|
+
anthropic: {
|
|
48
|
+
name,
|
|
49
|
+
description,
|
|
50
|
+
input_schema: inputSchema,
|
|
51
|
+
...(cacheable && { cache_control: { type: 'ephemeral' } }),
|
|
52
|
+
},
|
|
53
|
+
// OpenAI: function definition
|
|
54
|
+
openai: {
|
|
55
|
+
type: 'function',
|
|
56
|
+
function: {
|
|
57
|
+
name,
|
|
58
|
+
description,
|
|
59
|
+
parameters: inputSchema,
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
// Gemini: function declaration
|
|
63
|
+
gemini: {
|
|
64
|
+
name,
|
|
65
|
+
description,
|
|
66
|
+
parameters: inputSchema,
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
},
|
|
70
|
+
validate(payload) {
|
|
71
|
+
return exports.ToolSchemaPayloadSchema.parse(payload);
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
//# sourceMappingURL=tool-schema.codec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-schema.codec.js","sourceRoot":"","sources":["../../src/codecs/tool-schema.codec.ts"],"names":[],"mappings":";AAAA;;;;GAIG;;;AAEH,6BAAwB;AAGxB,uCAAwD;AAExD;;GAEG;AACU,QAAA,uBAAuB,GAAG,OAAC,CAAC,MAAM,CAAC;IAC9C,gBAAgB;IAChB,IAAI,EAAE,OAAC,CAAC,MAAM,EAAE;IAEhB,uBAAuB;IACvB,WAAW,EAAE,OAAC,CAAC,MAAM,EAAE;IAEvB,iCAAiC;IACjC,WAAW,EAAE,OAAC,CAAC,MAAM,CAAC,OAAC,CAAC,OAAO,EAAE,CAAC;IAElC,kCAAkC;IAClC,SAAS,EAAE,OAAC,CAAC,OAAO,EAAE,CAAC,QAAQ,EAAE;CAClC,CAAC,CAAC;AAIH;;GAEG;AACU,QAAA,eAAe,GAAkC;IAC5D,OAAO,EAAE,aAAa;IACtB,OAAO,EAAE,OAAO;IAChB,aAAa,EAAE,+BAAuB;IAEtC,YAAY,CAAC,OAA0B;QACrC,sCAAsC;QACtC,OAAO,IAAA,wBAAc,EAAC;YACpB,IAAI,EAAE,OAAO,CAAC,IAAI;YAClB,WAAW,EAAE,OAAO,CAAC,WAAW,CAAC,IAAI,EAAE;YACvC,WAAW,EAAE,IAAA,wBAAc,EAAC,OAAO,CAAC,WAAsC,CAAC;YAC3E,SAAS,EAAE,OAAO,CAAC,SAAS,IAAI,KAAK;SACtC,CAAC,CAAC;IACL,CAAC;IAED,IAAI,CAAC,aAAsB;QACzB,OAAO,IAAA,qBAAW,EAAC,aAAa,CAAC,CAAC;IACpC,CAAC;IAED,MAAM,CAAC,KAAsC;QAC3C,MAAM,EAAE,IAAI,EAAE,WAAW,EAAE,WAAW,EAAE,SAAS,EAAE,GAAG,KAAK,CAAC,OAAO,CAAC;QAEpE,OAAO;YACL,6BAA6B;YAC7B,SAAS,EAAE;gBACT,IAAI;gBACJ,WAAW;gBACX,YAAY,EAAE,WAAW;gBACzB,GAAG,CAAC,SAAS,IAAI,EAAE,aAAa,EAAE,EAAE,IAAI,EAAE,WAAW,EAAE,EAAE,CAAC;aAC3D;YAED,8BAA8B;YAC9B,MAAM,EAAE;gBACN,IAAI,EAAE,UAAU;gBAChB,QAAQ,EAAE;oBACR,IAAI;oBACJ,WAAW;oBACX,UAAU,EAAE,WAAW;iBACxB;aACF;YAED,+BAA+B;YAC/B,MAAM,EAAE;gBACN,IAAI;gBACJ,WAAW;gBACX,UAAU,EAAE,WAAW;aACxB;SACF,CAAC;IACJ,CAAC;IAED,QAAQ,CAAC,OAAgB;QACvB,OAAO,+BAAuB,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IAChD,CAAC;CACF,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unsafe text codec (any kind).
|
|
3
|
+
*
|
|
4
|
+
* Raw text passthrough without validation. Use with caution.
|
|
5
|
+
*/
|
|
6
|
+
import { z } from 'zod';
|
|
7
|
+
import type { BlockCodec } from '../types/codec.js';
|
|
8
|
+
/**
|
|
9
|
+
* Unsafe text payload schema.
|
|
10
|
+
*/
|
|
11
|
+
export declare const UnsafeTextPayloadSchema: z.ZodObject<{
|
|
12
|
+
/** Raw text content */
|
|
13
|
+
text: z.ZodString;
|
|
14
|
+
/** Optional role override (user, assistant, system) */
|
|
15
|
+
role: z.ZodOptional<z.ZodEnum<["user", "assistant", "system"]>>;
|
|
16
|
+
}, "strip", z.ZodTypeAny, {
|
|
17
|
+
text: string;
|
|
18
|
+
role?: "user" | "assistant" | "system" | undefined;
|
|
19
|
+
}, {
|
|
20
|
+
text: string;
|
|
21
|
+
role?: "user" | "assistant" | "system" | undefined;
|
|
22
|
+
}>;
|
|
23
|
+
export type UnsafeTextPayload = z.infer<typeof UnsafeTextPayloadSchema>;
|
|
24
|
+
/**
|
|
25
|
+
* Unsafe text codec implementation.
|
|
26
|
+
*/
|
|
27
|
+
export declare const UnsafeTextCodec: BlockCodec<UnsafeTextPayload>;
|
|
28
|
+
//# sourceMappingURL=unsafe-text.codec.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"unsafe-text.codec.d.ts","sourceRoot":"","sources":["../../src/codecs/unsafe-text.codec.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EAAE,UAAU,EAAmB,MAAM,mBAAmB,CAAC;AAIrE;;GAEG;AACH,eAAO,MAAM,uBAAuB;IAClC,uBAAuB;;IAGvB,uDAAuD;;;;;;;;EAEvD,CAAC;AAEH,MAAM,MAAM,iBAAiB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,uBAAuB,CAAC,CAAC;AAExE;;GAEG;AACH,eAAO,MAAM,eAAe,EAAE,UAAU,CAAC,iBAAiB,CA8CzD,CAAC"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Unsafe text codec (any kind).
|
|
4
|
+
*
|
|
5
|
+
* Raw text passthrough without validation. Use with caution.
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.UnsafeTextCodec = exports.UnsafeTextPayloadSchema = void 0;
|
|
9
|
+
const zod_1 = require("zod");
|
|
10
|
+
const base_js_1 = require("./base.js");
|
|
11
|
+
/**
|
|
12
|
+
* Unsafe text payload schema.
|
|
13
|
+
*/
|
|
14
|
+
exports.UnsafeTextPayloadSchema = zod_1.z.object({
|
|
15
|
+
/** Raw text content */
|
|
16
|
+
text: zod_1.z.string(),
|
|
17
|
+
/** Optional role override (user, assistant, system) */
|
|
18
|
+
role: zod_1.z.enum(['user', 'assistant', 'system']).optional(),
|
|
19
|
+
});
|
|
20
|
+
/**
|
|
21
|
+
* Unsafe text codec implementation.
|
|
22
|
+
*/
|
|
23
|
+
exports.UnsafeTextCodec = {
|
|
24
|
+
codecId: 'unsafe-text',
|
|
25
|
+
version: '1.0.0',
|
|
26
|
+
payloadSchema: exports.UnsafeTextPayloadSchema,
|
|
27
|
+
canonicalize(payload) {
|
|
28
|
+
// Canonicalize: trim whitespace and normalize role
|
|
29
|
+
return {
|
|
30
|
+
text: payload.text.trim(),
|
|
31
|
+
role: payload.role ?? 'user',
|
|
32
|
+
};
|
|
33
|
+
},
|
|
34
|
+
hash(canonicalized) {
|
|
35
|
+
return (0, base_js_1.defaultHash)(canonicalized);
|
|
36
|
+
},
|
|
37
|
+
render(block) {
|
|
38
|
+
const { text, role = 'user' } = block.payload;
|
|
39
|
+
return {
|
|
40
|
+
// Anthropic: message with specified role
|
|
41
|
+
anthropic: {
|
|
42
|
+
role: role === 'system' ? 'user' : role,
|
|
43
|
+
content: role === 'system'
|
|
44
|
+
? [{ type: 'text', text }]
|
|
45
|
+
: text,
|
|
46
|
+
},
|
|
47
|
+
// OpenAI: message with specified role
|
|
48
|
+
openai: {
|
|
49
|
+
role: role === 'system' ? 'system' : role,
|
|
50
|
+
content: text,
|
|
51
|
+
},
|
|
52
|
+
// Gemini: convert role to user/model
|
|
53
|
+
gemini: {
|
|
54
|
+
role: role === 'assistant' ? 'model' : 'user',
|
|
55
|
+
parts: [{ text }],
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
},
|
|
59
|
+
validate(payload) {
|
|
60
|
+
return exports.UnsafeTextPayloadSchema.parse(payload);
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
//# sourceMappingURL=unsafe-text.codec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"un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Q,CAAC,CAAC,CAAC,IAAI;gBACzC,OAAO,EAAE,IAAI;aACd;YAED,qCAAqC;YACrC,MAAM,EAAE;gBACN,IAAI,EAAE,IAAI,KAAK,WAAW,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM;gBAC7C,KAAK,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC;aAClB;SACF,CAAC;IACJ,CAAC;IAED,QAAQ,CAAC,OAAgB;QACvB,OAAO,+BAAuB,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IAChD,CAAC;CACF,CAAC"}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ContextGraph: Core data structure for managing context blocks and relationships.
|
|
3
|
+
*
|
|
4
|
+
* Holds blocks, tracks derivation/reference edges, supports querying and view creation.
|
|
5
|
+
*/
|
|
6
|
+
import type { ContextBlock, BlockRef } from '../types/block.js';
|
|
7
|
+
import type { BlockQuery } from './queries.js';
|
|
8
|
+
import type { ContextView, ViewOptions } from './views.js';
|
|
9
|
+
/**
|
|
10
|
+
* Edge types for block relationships.
|
|
11
|
+
*/
|
|
12
|
+
export interface ContextGraphEdges {
|
|
13
|
+
/** Derivation edges: blockHash -> parent BlockRefs (provenance tracking) */
|
|
14
|
+
derivedFrom: Map<string, BlockRef[]>;
|
|
15
|
+
/** Reference edges: blockHash -> referenced blockHashes (lightweight citations) */
|
|
16
|
+
references: Map<string, string[]>;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* ContextGraph: Mutable graph of context blocks with relationship tracking.
|
|
20
|
+
*
|
|
21
|
+
* Responsibilities:
|
|
22
|
+
* - Store blocks by content-addressed hash
|
|
23
|
+
* - Track derivation and reference relationships
|
|
24
|
+
* - Support query-based block selection
|
|
25
|
+
* - Create deterministic views with stable ordering
|
|
26
|
+
*/
|
|
27
|
+
export declare class ContextGraph {
|
|
28
|
+
/** All blocks indexed by blockHash */
|
|
29
|
+
private readonly blocks;
|
|
30
|
+
/** Block relationship edges */
|
|
31
|
+
private readonly edges;
|
|
32
|
+
constructor();
|
|
33
|
+
/**
|
|
34
|
+
* Add a block to the graph.
|
|
35
|
+
* Idempotent: if block already exists (same hash), this is a no-op.
|
|
36
|
+
*
|
|
37
|
+
* @param block - Block to add
|
|
38
|
+
* @param derivedFrom - Optional parent blocks for provenance
|
|
39
|
+
* @param references - Optional referenced block hashes
|
|
40
|
+
*/
|
|
41
|
+
addBlock<TPayload>(block: ContextBlock<TPayload>, derivedFrom?: BlockRef[], references?: string[]): void;
|
|
42
|
+
/**
|
|
43
|
+
* Remove a block from the graph.
|
|
44
|
+
* Also removes associated edges.
|
|
45
|
+
*
|
|
46
|
+
* @param blockHash - Block hash to remove
|
|
47
|
+
* @returns True if block was removed, false if not found
|
|
48
|
+
*/
|
|
49
|
+
removeBlock(blockHash: string): boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Get a block by hash.
|
|
52
|
+
*
|
|
53
|
+
* @param blockHash - Block hash
|
|
54
|
+
* @returns Block if found, undefined otherwise
|
|
55
|
+
*/
|
|
56
|
+
getBlock<TPayload = unknown>(blockHash: string): ContextBlock<TPayload> | undefined;
|
|
57
|
+
/**
|
|
58
|
+
* Check if a block exists in the graph.
|
|
59
|
+
*
|
|
60
|
+
* @param blockHash - Block hash
|
|
61
|
+
* @returns True if block exists
|
|
62
|
+
*/
|
|
63
|
+
hasBlock(blockHash: string): boolean;
|
|
64
|
+
/**
|
|
65
|
+
* Get all blocks in the graph (unordered).
|
|
66
|
+
*
|
|
67
|
+
* @returns Array of all blocks
|
|
68
|
+
*/
|
|
69
|
+
getAllBlocks(): ContextBlock<unknown>[];
|
|
70
|
+
/**
|
|
71
|
+
* Get the number of blocks in the graph.
|
|
72
|
+
*
|
|
73
|
+
* @returns Block count
|
|
74
|
+
*/
|
|
75
|
+
getBlockCount(): number;
|
|
76
|
+
/**
|
|
77
|
+
* Get parent blocks (provenance) for a given block.
|
|
78
|
+
*
|
|
79
|
+
* @param blockHash - Block hash
|
|
80
|
+
* @returns Array of parent BlockRefs, or empty array if none
|
|
81
|
+
*/
|
|
82
|
+
getDerivedFrom(blockHash: string): BlockRef[];
|
|
83
|
+
/**
|
|
84
|
+
* Get referenced block hashes for a given block.
|
|
85
|
+
*
|
|
86
|
+
* @param blockHash - Block hash
|
|
87
|
+
* @returns Array of referenced hashes, or empty array if none
|
|
88
|
+
*/
|
|
89
|
+
getReferences(blockHash: string): string[];
|
|
90
|
+
/**
|
|
91
|
+
* Select blocks matching a query.
|
|
92
|
+
* Returns blocks in arbitrary order (use createView for deterministic ordering).
|
|
93
|
+
*
|
|
94
|
+
* @param query - Block query
|
|
95
|
+
* @returns Matching blocks
|
|
96
|
+
*/
|
|
97
|
+
select(query: BlockQuery): ContextBlock<unknown>[];
|
|
98
|
+
/**
|
|
99
|
+
* Create a deterministic view of the graph.
|
|
100
|
+
* View is immutable snapshot with stable ordering (KIND_ORDER + lexicographic).
|
|
101
|
+
*
|
|
102
|
+
* @param options - View options (query, token budget, etc.)
|
|
103
|
+
* @returns ContextView with ordered blocks
|
|
104
|
+
*/
|
|
105
|
+
createView(options: ViewOptions): Promise<ContextView>;
|
|
106
|
+
/**
|
|
107
|
+
* Clear all blocks and edges.
|
|
108
|
+
*/
|
|
109
|
+
clear(): void;
|
|
110
|
+
/**
|
|
111
|
+
* Get graph statistics.
|
|
112
|
+
*
|
|
113
|
+
* @returns Graph stats
|
|
114
|
+
*/
|
|
115
|
+
getStats(): {
|
|
116
|
+
blockCount: number;
|
|
117
|
+
derivationEdgeCount: number;
|
|
118
|
+
referenceEdgeCount: number;
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
//# sourceMappingURL=context-graph.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context-graph.d.ts","sourceRoot":"","sources":["../../src/graph/context-graph.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,QAAQ,EAAE,MA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AAU,GAAG,YAAY,CAAC,OAAO,CAAC,EAAE;IAKlD;;;;;;OAMG;IACG,UAAU,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC;IAI5D;;OAEG;IACH,KAAK,IAAI,IAAI;IAMb;;;;OAIG;IACH,QAAQ,IAAI;QACV,UAAU,EAAE,MAAM,CAAC;QACnB,mBAAmB,EAAE,MAAM,CAAC;QAC5B,kBAAkB,EAAE,MAAM,CAAC;KAC5B;CAOF"}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* ContextGraph: Core data structure for managing context blocks and relationships.
|
|
4
|
+
*
|
|
5
|
+
* Holds blocks, tracks derivation/reference edges, supports querying and view creation.
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.ContextGraph = void 0;
|
|
9
|
+
const views_js_1 = require("./views.js");
|
|
10
|
+
const queries_js_1 = require("./queries.js");
|
|
11
|
+
/**
|
|
12
|
+
* ContextGraph: Mutable graph of context blocks with relationship tracking.
|
|
13
|
+
*
|
|
14
|
+
* Responsibilities:
|
|
15
|
+
* - Store blocks by content-addressed hash
|
|
16
|
+
* - Track derivation and reference relationships
|
|
17
|
+
* - Support query-based block selection
|
|
18
|
+
* - Create deterministic views with stable ordering
|
|
19
|
+
*/
|
|
20
|
+
class ContextGraph {
|
|
21
|
+
/** All blocks indexed by blockHash */
|
|
22
|
+
blocks;
|
|
23
|
+
/** Block relationship edges */
|
|
24
|
+
edges;
|
|
25
|
+
constructor() {
|
|
26
|
+
this.blocks = new Map();
|
|
27
|
+
this.edges = {
|
|
28
|
+
derivedFrom: new Map(),
|
|
29
|
+
references: new Map(),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Add a block to the graph.
|
|
34
|
+
* Idempotent: if block already exists (same hash), this is a no-op.
|
|
35
|
+
*
|
|
36
|
+
* @param block - Block to add
|
|
37
|
+
* @param derivedFrom - Optional parent blocks for provenance
|
|
38
|
+
* @param references - Optional referenced block hashes
|
|
39
|
+
*/
|
|
40
|
+
addBlock(block, derivedFrom, references) {
|
|
41
|
+
const { blockHash } = block;
|
|
42
|
+
// Idempotent: skip if already exists
|
|
43
|
+
if (this.blocks.has(blockHash)) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
// Add block
|
|
47
|
+
this.blocks.set(blockHash, block);
|
|
48
|
+
// Add derivation edges
|
|
49
|
+
if (derivedFrom && derivedFrom.length > 0) {
|
|
50
|
+
this.edges.derivedFrom.set(blockHash, derivedFrom);
|
|
51
|
+
}
|
|
52
|
+
// Add reference edges
|
|
53
|
+
if (references && references.length > 0) {
|
|
54
|
+
this.edges.references.set(blockHash, references);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Remove a block from the graph.
|
|
59
|
+
* Also removes associated edges.
|
|
60
|
+
*
|
|
61
|
+
* @param blockHash - Block hash to remove
|
|
62
|
+
* @returns True if block was removed, false if not found
|
|
63
|
+
*/
|
|
64
|
+
removeBlock(blockHash) {
|
|
65
|
+
const existed = this.blocks.delete(blockHash);
|
|
66
|
+
// Clean up edges
|
|
67
|
+
this.edges.derivedFrom.delete(blockHash);
|
|
68
|
+
this.edges.references.delete(blockHash);
|
|
69
|
+
return existed;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Get a block by hash.
|
|
73
|
+
*
|
|
74
|
+
* @param blockHash - Block hash
|
|
75
|
+
* @returns Block if found, undefined otherwise
|
|
76
|
+
*/
|
|
77
|
+
getBlock(blockHash) {
|
|
78
|
+
return this.blocks.get(blockHash);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Check if a block exists in the graph.
|
|
82
|
+
*
|
|
83
|
+
* @param blockHash - Block hash
|
|
84
|
+
* @returns True if block exists
|
|
85
|
+
*/
|
|
86
|
+
hasBlock(blockHash) {
|
|
87
|
+
return this.blocks.has(blockHash);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Get all blocks in the graph (unordered).
|
|
91
|
+
*
|
|
92
|
+
* @returns Array of all blocks
|
|
93
|
+
*/
|
|
94
|
+
getAllBlocks() {
|
|
95
|
+
return Array.from(this.blocks.values());
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Get the number of blocks in the graph.
|
|
99
|
+
*
|
|
100
|
+
* @returns Block count
|
|
101
|
+
*/
|
|
102
|
+
getBlockCount() {
|
|
103
|
+
return this.blocks.size;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Get parent blocks (provenance) for a given block.
|
|
107
|
+
*
|
|
108
|
+
* @param blockHash - Block hash
|
|
109
|
+
* @returns Array of parent BlockRefs, or empty array if none
|
|
110
|
+
*/
|
|
111
|
+
getDerivedFrom(blockHash) {
|
|
112
|
+
return this.edges.derivedFrom.get(blockHash) ?? [];
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Get referenced block hashes for a given block.
|
|
116
|
+
*
|
|
117
|
+
* @param blockHash - Block hash
|
|
118
|
+
* @returns Array of referenced hashes, or empty array if none
|
|
119
|
+
*/
|
|
120
|
+
getReferences(blockHash) {
|
|
121
|
+
return this.edges.references.get(blockHash) ?? [];
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Select blocks matching a query.
|
|
125
|
+
* Returns blocks in arbitrary order (use createView for deterministic ordering).
|
|
126
|
+
*
|
|
127
|
+
* @param query - Block query
|
|
128
|
+
* @returns Matching blocks
|
|
129
|
+
*/
|
|
130
|
+
select(query) {
|
|
131
|
+
const allBlocks = this.getAllBlocks();
|
|
132
|
+
return allBlocks.filter((block) => (0, queries_js_1.matchesQuery)(block, query, this));
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Create a deterministic view of the graph.
|
|
136
|
+
* View is immutable snapshot with stable ordering (KIND_ORDER + lexicographic).
|
|
137
|
+
*
|
|
138
|
+
* @param options - View options (query, token budget, etc.)
|
|
139
|
+
* @returns ContextView with ordered blocks
|
|
140
|
+
*/
|
|
141
|
+
async createView(options) {
|
|
142
|
+
return (0, views_js_1.createContextView)(this, options);
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Clear all blocks and edges.
|
|
146
|
+
*/
|
|
147
|
+
clear() {
|
|
148
|
+
this.blocks.clear();
|
|
149
|
+
this.edges.derivedFrom.clear();
|
|
150
|
+
this.edges.references.clear();
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Get graph statistics.
|
|
154
|
+
*
|
|
155
|
+
* @returns Graph stats
|
|
156
|
+
*/
|
|
157
|
+
getStats() {
|
|
158
|
+
return {
|
|
159
|
+
blockCount: this.blocks.size,
|
|
160
|
+
derivationEdgeCount: this.edges.derivedFrom.size,
|
|
161
|
+
referenceEdgeCount: this.edges.references.size,
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
exports.ContextGraph = ContextGraph;
|
|
166
|
+
//# sourceMappingURL=context-graph.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context-graph.js","sourceRoot":"","sources":["../../src/graph/context-graph.ts"],"names":[],"mappings":";AAAA;;;;GAIG;;;AAKH,yCAA+C;AAC/C,6CAA4C;AAa5C;;;;;;;;GAQG;AACH,MAAa,YAAY;IACvB,sCAAsC;IACrB,MAAM,CAAqC;IAE5D,+BAA+B;IACd,KAAK,CAAoB;IAE1C;QACE,IAAI,CAAC,MAAM,GAAG,IAAI,GAAG,EAAE,CAAC;QACxB,IAAI,CAAC,KAAK,GAAG;YACX,WAAW,EAAE,IAAI,GAAG,EAAE;YACtB,UAAU,EAAE,IAAI,GAAG,EAAE;SACtB,CAAC;IACJ,CAAC;IAED;;;;;;;OAOG;IACH,QAAQ,CACN,KAA6B,EAC7B,WAAwB,EACxB,UAAqB;QAErB,MAAM,EAAE,SAAS,EAAE,GAAG,KAAK,CAA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+1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/graph/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,cAAc,iBAAiB,CAAC;AAChC,cAAc,oBAAoB,CAAC;AACnC,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Graph utilities for @foundry/context
|
|
4
|
+
*/
|
|
5
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
8
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
9
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
10
|
+
}
|
|
11
|
+
Object.defineProperty(o, k2, desc);
|
|
12
|
+
}) : (function(o, m, k, k2) {
|
|
13
|
+
if (k2 === undefined) k2 = k;
|
|
14
|
+
o[k2] = m[k];
|
|
15
|
+
}));
|
|
16
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
17
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
18
|
+
};
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
__exportStar(require("./kind-order.js"), exports);
|
|
21
|
+
__exportStar(require("./context-graph.js"), exports);
|
|
22
|
+
__exportStar(require("./queries.js"), exports);
|
|
23
|
+
__exportStar(require("./views.js"), exports);
|
|
24
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/graph/index.ts"],"names":[],"mappings":";AAAA;;GAEG;;;;;;;;;;;;;;;;AAEH,kDAAgC;AAChC,qDAAmC;AACnC,+CAA6B;AAC7B,6CAA2B"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* KIND_ORDER: Deterministic block ordering for context compilation.
|
|
3
|
+
*
|
|
4
|
+
* This is the single source of truth for block ordering.
|
|
5
|
+
* All context compilation MUST respect this order.
|
|
6
|
+
*/
|
|
7
|
+
import type { BlockKind, ContextBlock } from '../types/block.js';
|
|
8
|
+
/**
|
|
9
|
+
* Immutable block kind ordering (pinned → reference → memory → state → tool_output → history → turn).
|
|
10
|
+
*
|
|
11
|
+
* NEVER modify this array. It is the contract for deterministic compilation.
|
|
12
|
+
*/
|
|
13
|
+
export declare const KIND_ORDER: readonly BlockKind[];
|
|
14
|
+
/**
|
|
15
|
+
* Get kind index for ordering comparison.
|
|
16
|
+
* Returns -1 if kind is not in KIND_ORDER.
|
|
17
|
+
*
|
|
18
|
+
* @param kind - Block kind
|
|
19
|
+
* @returns Index in KIND_ORDER, or -1 if not found
|
|
20
|
+
*/
|
|
21
|
+
export declare function getKindIndex(kind: BlockKind): number;
|
|
22
|
+
/**
|
|
23
|
+
* Compare two block kinds for ordering.
|
|
24
|
+
* Returns negative if a < b, positive if a > b, zero if equal.
|
|
25
|
+
*
|
|
26
|
+
* @param a - First block kind
|
|
27
|
+
* @param b - Second block kind
|
|
28
|
+
* @returns Comparison result
|
|
29
|
+
*/
|
|
30
|
+
export declare function compareKinds(a: BlockKind, b: BlockKind): number;
|
|
31
|
+
/**
|
|
32
|
+
* Sort blocks by KIND_ORDER (stable sort, preserves relative order within same kind).
|
|
33
|
+
*
|
|
34
|
+
* @param blocks - Blocks to sort
|
|
35
|
+
* @returns Sorted blocks (new array)
|
|
36
|
+
*/
|
|
37
|
+
export declare function sortBlocksByKind<TPayload = unknown>(blocks: ContextBlock<TPayload>[]): ContextBlock<TPayload>[];
|
|
38
|
+
/**
|
|
39
|
+
* Validate that blocks are sorted by KIND_ORDER.
|
|
40
|
+
* Throws if blocks are not sorted correctly.
|
|
41
|
+
*
|
|
42
|
+
* @param blocks - Blocks to validate
|
|
43
|
+
*/
|
|
44
|
+
export declare function validateBlockOrder<TPayload = unknown>(blocks: ContextBlock<TPayload>[]): void;
|
|
45
|
+
/**
|
|
46
|
+
* Group blocks by kind (in KIND_ORDER).
|
|
47
|
+
* Returns a map of kind -> blocks.
|
|
48
|
+
*
|
|
49
|
+
* @param blocks - Blocks to group
|
|
50
|
+
* @returns Map of kind to blocks
|
|
51
|
+
*/
|
|
52
|
+
export declare function groupBlocksByKind<TPayload = unknown>(blocks: ContextBlock<TPayload>[]): Map<BlockKind, ContextBlock<TPayload>[]>;
|
|
53
|
+
/**
|
|
54
|
+
* Check if a kind is valid (exists in KIND_ORDER).
|
|
55
|
+
*
|
|
56
|
+
* @param kind - Block kind to check
|
|
57
|
+
* @returns True if valid
|
|
58
|
+
*/
|
|
59
|
+
export declare function isValidKind(kind: string): kind is BlockKind;
|
|
60
|
+
//# sourceMappingURL=kind-order.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"kind-order.d.ts","sourceRoot":"","sources":["../../src/graph/kind-order.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEjE;;;;GAIG;AACH,eAAO,MAAM,UAAU,EAAE,SAAS,SAAS,EAQhC,CAAC;AAEZ;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,SAAS,GAAG,MAAM,CAEpD;AAED;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,SAAS,GAAG,MAAM,CAa/D;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,QAAQ,GAAG,OAAO,EACjD,MAAM,EAAE,YAAY,CAAC,QAAQ,CAAC,EAAE,GAC/B,YAAY,CAAC,QAAQ,CAAC,EAAE,CAE1B;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,QAAQ,GAAG,OAAO,EACnD,MAAM,EAAE,YAAY,CAAC,QAAQ,CAAC,EAAE,GAC/B,IAAI,CAaN;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,QAAQ,GAAG,OAAO,EAClD,MAAM,EAAE,YAAY,CAAC,QAAQ,CAAC,EAAE,GAC/B,GAAG,CAAC,SAAS,EAAE,YAAY,CAAC,QAAQ,CAAC,EAAE,CAAC,CAY1C;AAED;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,IAAI,SAAS,CAE3D"}
|