mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-07-02 02:58:10 +02:00
Remove RAG requestor Promise bridges
This commit is contained in:
parent
88db18fbda
commit
5979d38b99
11 changed files with 249 additions and 293 deletions
|
|
@ -16,6 +16,9 @@
|
|||
* Python reference: trustgraph-flow/trustgraph/agent/react/service.py
|
||||
*/
|
||||
|
||||
import {
|
||||
NodeRuntime,
|
||||
} from "@effect/platform-node";
|
||||
import {
|
||||
makeFlowProcessor,
|
||||
makeConsumerSpec,
|
||||
|
|
@ -39,16 +42,13 @@ import {
|
|||
type ToolRequest,
|
||||
type ToolResponse,
|
||||
type EffectConfigHandler,
|
||||
type EffectRequestOptions,
|
||||
type EffectRequestResponse,
|
||||
type FlowRequestOptions,
|
||||
type FlowRequestor,
|
||||
type FlowResourceNotFoundError,
|
||||
type MessagingDeliveryError,
|
||||
type Spec,
|
||||
} from "@trustgraph/base";
|
||||
import { Context, Effect, Layer, Ref } from "effect";
|
||||
import {Context, Effect, Layer, ManagedRuntime, Ref} from "effect";
|
||||
import * as O from "effect/Option";
|
||||
import * as Predicate from "effect/Predicate";
|
||||
import * as S from "effect/Schema";
|
||||
|
||||
import {
|
||||
|
|
@ -106,29 +106,6 @@ export class AgentRuntime extends Context.Service<AgentRuntime, AgentRuntimeServ
|
|||
"@trustgraph/flow/agent/react/service/AgentRuntime",
|
||||
) {}
|
||||
|
||||
const toEffectRequestOptions = <TRes>(
|
||||
options: FlowRequestOptions<TRes> | undefined,
|
||||
): EffectRequestOptions<TRes> | undefined => {
|
||||
if (options === undefined) return undefined;
|
||||
return {
|
||||
...(options.timeoutMs === undefined ? {} : { timeoutMs: options.timeoutMs }),
|
||||
...(options.recipient === undefined
|
||||
? {}
|
||||
: {
|
||||
recipient: (response: TRes) =>
|
||||
Effect.promise(() => options.recipient?.(response) ?? Promise.resolve(true)),
|
||||
}),
|
||||
};
|
||||
};
|
||||
|
||||
const toPromiseRequestor = <TReq, TRes>(
|
||||
requestor: EffectRequestResponse<TReq, TRes>,
|
||||
): FlowRequestor<TReq, TRes> => ({
|
||||
request: (request, options) =>
|
||||
Effect.runPromise(requestor.request(request, toEffectRequestOptions(options))),
|
||||
stop: () => Effect.runPromise(requestor.stop),
|
||||
});
|
||||
|
||||
const buildConfiguredTool = (
|
||||
toolId: string,
|
||||
data: ToolConfigEntry,
|
||||
|
|
@ -137,7 +114,7 @@ const buildConfiguredTool = (
|
|||
const implType = data.type ?? "";
|
||||
const name = data.name ?? "";
|
||||
const description = data.description ?? "";
|
||||
const config = { ...data } as Record<string, unknown>;
|
||||
const config: Record<string, unknown> = { ...data };
|
||||
|
||||
if (name.length === 0) {
|
||||
yield* Effect.logWarning(`[AgentService] Skipping tool with no name: ${toolId}`);
|
||||
|
|
@ -277,12 +254,13 @@ const wireTools = Effect.fn("AgentService.wireTools")(function* (
|
|||
const mcpTool = yield* flowCtx.flow.requestorEffect<ToolRequest, ToolResponse>("mcp-tool");
|
||||
|
||||
return tools.map((tool) => {
|
||||
const implType = tool.config?.type as string | undefined;
|
||||
const rawImplType = tool.config?.type;
|
||||
const implType = Predicate.isString(rawImplType) ? rawImplType : undefined;
|
||||
|
||||
switch (implType) {
|
||||
case "knowledge-query": {
|
||||
const live = createKnowledgeQueryTool(
|
||||
toPromiseRequestor(graphRag),
|
||||
graphRag,
|
||||
collection,
|
||||
onExplain,
|
||||
);
|
||||
|
|
@ -290,21 +268,21 @@ const wireTools = Effect.fn("AgentService.wireTools")(function* (
|
|||
}
|
||||
case "document-query": {
|
||||
const live = createDocumentQueryTool(
|
||||
toPromiseRequestor(docRag),
|
||||
docRag,
|
||||
collection,
|
||||
);
|
||||
return { ...tool, execute: live.execute };
|
||||
}
|
||||
case "triples-query": {
|
||||
const live = createTriplesQueryTool(
|
||||
toPromiseRequestor(triples),
|
||||
triples,
|
||||
collection,
|
||||
);
|
||||
return { ...tool, execute: live.execute };
|
||||
}
|
||||
case "mcp-tool": {
|
||||
const live = createMcpTool(
|
||||
toPromiseRequestor(mcpTool),
|
||||
mcpTool,
|
||||
tool.name,
|
||||
tool.description,
|
||||
tool.args,
|
||||
|
|
@ -328,16 +306,16 @@ const defaultTools = Effect.fn("AgentService.defaultTools")(function* (
|
|||
|
||||
return [
|
||||
createKnowledgeQueryTool(
|
||||
toPromiseRequestor(graphRag),
|
||||
graphRag,
|
||||
collection,
|
||||
onExplain,
|
||||
),
|
||||
createDocumentQueryTool(
|
||||
toPromiseRequestor(docRag),
|
||||
docRag,
|
||||
collection,
|
||||
),
|
||||
createTriplesQueryTool(
|
||||
toPromiseRequestor(triples),
|
||||
triples,
|
||||
collection,
|
||||
),
|
||||
];
|
||||
|
|
@ -433,7 +411,7 @@ const onAgentRequest = Effect.fn("AgentService.onRequest")(function* (
|
|||
content: "",
|
||||
explain_id: explain.explainId,
|
||||
explain_triples: explain.triples,
|
||||
} as AgentResponse);
|
||||
});
|
||||
}
|
||||
|
||||
yield* responseProducer.send(requestId, {
|
||||
|
|
@ -630,6 +608,12 @@ export const program = makeFlowProcessorProgram<ProcessorConfig, never, AgentRun
|
|||
layer: () => AgentRuntimeLive,
|
||||
});
|
||||
|
||||
const agentRuntime = ManagedRuntime.make(Layer.empty);
|
||||
|
||||
export function run(): Promise<void> {
|
||||
return Effect.runPromise(program);
|
||||
return agentRuntime.runPromise(program);
|
||||
}
|
||||
|
||||
export function runMain(): void {
|
||||
NodeRuntime.runMain(program);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
*/
|
||||
|
||||
import type {
|
||||
FlowRequestor,
|
||||
EffectRequestResponse,
|
||||
GraphRagRequest,
|
||||
GraphRagResponse,
|
||||
DocumentRagRequest,
|
||||
|
|
@ -18,13 +18,16 @@ import type {
|
|||
Term,
|
||||
Triple,
|
||||
} from "@trustgraph/base";
|
||||
import {Term as TermSchema} from "@trustgraph/base";
|
||||
import { Effect } from "effect";
|
||||
import * as O from "effect/Option";
|
||||
import * as Predicate from "effect/Predicate";
|
||||
import * as S from "effect/Schema";
|
||||
|
||||
import type { AgentTool, ToolArg } from "./types.js";
|
||||
|
||||
const decodeJsonUnknown = S.decodeUnknownOption(S.UnknownFromJsonString);
|
||||
const decodeTerm = S.decodeUnknownOption(TermSchema);
|
||||
|
||||
/**
|
||||
* Format a Term to a human-readable string.
|
||||
|
|
@ -71,7 +74,7 @@ export interface ExplainData {
|
|||
* Query the knowledge graph for information about entities and their relationships.
|
||||
*/
|
||||
export function createKnowledgeQueryTool(
|
||||
client: FlowRequestor<GraphRagRequest, GraphRagResponse>,
|
||||
client: EffectRequestResponse<GraphRagRequest, GraphRagResponse>,
|
||||
collection?: string,
|
||||
onExplain?: (data: ExplainData) => void,
|
||||
): AgentTool {
|
||||
|
|
@ -93,19 +96,14 @@ export function createKnowledgeQueryTool(
|
|||
query: question,
|
||||
...(collection !== undefined ? { collection } : {}),
|
||||
};
|
||||
const res = yield* Effect.tryPromise(() => client.request(request));
|
||||
const res = yield* client.request(request);
|
||||
yield* Effect.log(`[KnowledgeQuery] Response (${res.response?.length ?? 0} chars): ${res.error !== undefined ? `ERROR: ${res.error.message}` : `${res.response?.slice(0, 300)}...`}`);
|
||||
|
||||
// Extract explain data if embedded in the response
|
||||
const rawRes = res as Record<string, unknown>;
|
||||
if (
|
||||
rawRes.message_type === "explain" &&
|
||||
rawRes.explain_triples !== undefined &&
|
||||
onExplain !== undefined
|
||||
) {
|
||||
const explainTriples = res.explain_triples;
|
||||
if (res.message_type === "explain" && explainTriples !== undefined && onExplain !== undefined) {
|
||||
yield* Effect.sync(() => onExplain({
|
||||
explainId: (rawRes.explain_id as string) ?? "",
|
||||
triples: rawRes.explain_triples as Triple[],
|
||||
explainId: res.explain_id ?? "",
|
||||
triples: Array.from(explainTriples),
|
||||
}));
|
||||
}
|
||||
|
||||
|
|
@ -119,7 +117,7 @@ export function createKnowledgeQueryTool(
|
|||
* Search documents for relevant information.
|
||||
*/
|
||||
export function createDocumentQueryTool(
|
||||
client: FlowRequestor<DocumentRagRequest, DocumentRagResponse>,
|
||||
client: EffectRequestResponse<DocumentRagRequest, DocumentRagResponse>,
|
||||
collection?: string,
|
||||
): AgentTool {
|
||||
return {
|
||||
|
|
@ -139,13 +137,24 @@ export function createDocumentQueryTool(
|
|||
query: question,
|
||||
...(collection !== undefined ? { collection } : {}),
|
||||
};
|
||||
const res = yield* Effect.tryPromise(() => client.request(request));
|
||||
const res = yield* client.request(request);
|
||||
if (res.error !== undefined) return `Error: ${res.error.message}`;
|
||||
return res.response;
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
const objectProperty = (value: object, key: string): unknown =>
|
||||
Predicate.hasProperty(value, key) ? value[key] : undefined;
|
||||
|
||||
const termFromUnknown = (value: unknown): Term | undefined => {
|
||||
if (Predicate.isString(value)) {
|
||||
return { type: "LITERAL", value };
|
||||
}
|
||||
const decoded = decodeTerm(value);
|
||||
return O.isSome(decoded) ? decoded.value : undefined;
|
||||
};
|
||||
|
||||
/**
|
||||
* Parse triples query input. Accepts JSON with optional s, p, o fields.
|
||||
*/
|
||||
|
|
@ -166,30 +175,21 @@ function parseTriplesInput(input: string): {
|
|||
};
|
||||
}
|
||||
|
||||
const parsed = decoded.value as Record<string, unknown>;
|
||||
const toTerm = (val: unknown): Term | undefined => {
|
||||
if (typeof val === "string") {
|
||||
return { type: "LITERAL", value: val };
|
||||
}
|
||||
if (typeof val === "object" && val !== null && "type" in val) {
|
||||
return val as Term;
|
||||
}
|
||||
return undefined;
|
||||
};
|
||||
|
||||
const result: {
|
||||
s?: Term;
|
||||
p?: Term;
|
||||
o?: Term;
|
||||
limit?: number;
|
||||
} = {};
|
||||
const s = toTerm(parsed.subject ?? parsed.s);
|
||||
const p = toTerm(parsed.predicate ?? parsed.p);
|
||||
const o = toTerm(parsed.object ?? parsed.o);
|
||||
const parsed = decoded.value;
|
||||
const s = termFromUnknown(objectProperty(parsed, "subject") ?? objectProperty(parsed, "s"));
|
||||
const p = termFromUnknown(objectProperty(parsed, "predicate") ?? objectProperty(parsed, "p"));
|
||||
const o = termFromUnknown(objectProperty(parsed, "object") ?? objectProperty(parsed, "o"));
|
||||
const limit = objectProperty(parsed, "limit");
|
||||
if (s !== undefined) result.s = s;
|
||||
if (p !== undefined) result.p = p;
|
||||
if (o !== undefined) result.o = o;
|
||||
if (typeof parsed.limit === "number") result.limit = parsed.limit;
|
||||
if (Predicate.isNumber(limit)) result.limit = limit;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
@ -197,7 +197,7 @@ function parseTriplesInput(input: string): {
|
|||
* Query for specific triples (subject-predicate-object relationships) in the knowledge graph.
|
||||
*/
|
||||
export function createTriplesQueryTool(
|
||||
client: FlowRequestor<TriplesQueryRequest, TriplesQueryResponse>,
|
||||
client: EffectRequestResponse<TriplesQueryRequest, TriplesQueryResponse>,
|
||||
collection?: string,
|
||||
): AgentTool {
|
||||
return {
|
||||
|
|
@ -231,7 +231,7 @@ export function createTriplesQueryTool(
|
|||
...(o !== undefined ? { o } : {}),
|
||||
...(collection !== undefined ? { collection } : {}),
|
||||
};
|
||||
const res = yield* Effect.tryPromise(() => client.request(request));
|
||||
const res = yield* client.request(request);
|
||||
|
||||
if (res.error !== undefined) return `Error: ${res.error.message}`;
|
||||
|
||||
|
|
@ -255,7 +255,7 @@ export function createTriplesQueryTool(
|
|||
* this function just wraps it as an AgentTool the ReAct agent can invoke.
|
||||
*/
|
||||
export function createMcpTool(
|
||||
client: FlowRequestor<ToolRequest, ToolResponse>,
|
||||
client: EffectRequestResponse<ToolRequest, ToolResponse>,
|
||||
toolName: string,
|
||||
description: string,
|
||||
args: ToolArg[],
|
||||
|
|
@ -265,7 +265,7 @@ export function createMcpTool(
|
|||
description,
|
||||
args,
|
||||
execute: (input: string): Promise<string> => Effect.runPromise(Effect.gen(function* () {
|
||||
const res = yield* Effect.tryPromise(() => client.request({ name: toolName, parameters: input }));
|
||||
const res = yield* client.request({ name: toolName, parameters: input });
|
||||
if (res.error !== undefined) return `Error: ${res.error.message}`;
|
||||
if (res.text !== undefined) return res.text;
|
||||
if (res.object !== undefined) return res.object;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue