Normalize term translation with Effect Match

This commit is contained in:
elpresidank 2026-06-02 09:11:33 -05:00
parent e311315556
commit 09d34fb4d4
11 changed files with 349 additions and 190 deletions

View file

@ -6,6 +6,7 @@ import {
GraphRagResponse,
Term,
TextCompletionRequest,
Triple,
loadProcessorRuntimeConfig,
} from "../index.js";
@ -40,6 +41,20 @@ describe("Effect schemas", () => {
}),
);
it.effect(
"decode triples with named graph strings",
Effect.fnUntraced(function* () {
const triple = yield* S.decodeUnknownEffect(Triple)({
s: { type: "IRI", iri: "urn:s" },
p: { type: "IRI", iri: "urn:p" },
o: { type: "LITERAL", value: "object" },
g: "urn:graph",
});
expect(triple.g).toBe("urn:graph");
}),
);
it.effect(
"preserve gateway response extension fields",
Effect.fnUntraced(function* () {

View file

@ -45,30 +45,28 @@ export type Triple = {
readonly s: Term;
readonly p: Term;
readonly o: Term;
readonly g?: Term;
readonly g?: string;
};
export const Triple: S.Codec<Triple, Triple> = S.suspend(() =>
S.Struct({
s: Term,
p: Term,
o: Term,
g: S.optionalKey(Term),
})
);
export const Triple: S.Codec<Triple, Triple> = S.Struct({
s: S.suspend((): S.Codec<Term, Term> => Term),
p: S.suspend((): S.Codec<Term, Term> => Term),
o: S.suspend((): S.Codec<Term, Term> => Term),
g: S.optionalKey(S.String),
});
export const TripleTerm: S.Codec<TripleTerm, TripleTerm> = S.suspend(() =>
S.Struct({
type: S.tag("TRIPLE"),
triple: Triple,
})
);
export const TripleTerm: S.Codec<TripleTerm, TripleTerm> = S.Struct({
type: S.tag("TRIPLE"),
triple: S.suspend((): S.Codec<Triple, Triple> => Triple),
});
export interface TripleTerm {
readonly type: "TRIPLE";
readonly triple: Triple;
}
export const Term: S.Codec<Term, Term> = S.suspend(() => S.Union([IriTerm, BlankTerm, LiteralTerm, TripleTerm]));
export const Term = S.Union([IriTerm, BlankTerm, LiteralTerm, TripleTerm]).pipe(
S.toTaggedUnion("type"),
);
export const Field = S.Struct({
name: S.String,