trustgraph/ts/packages/base/src/spec/request-response-spec.ts

53 lines
1.8 KiB
TypeScript
Raw Normal View History

/**
* Request/response specification declares a request/response client for a flow.
*
* Enables FlowProcessor handlers to make request/response calls to other services
* (e.g., calling the prompt service or LLM from within a knowledge extraction handler).
*
* Python reference: trustgraph-base/trustgraph/base/prompt_client_spec.py
*/
2026-05-12 08:06:58 -05:00
import { Effect } from "effect";
import type { Spec } from "./types.js";
import type { PubSubBackend } from "../backend/types.js";
import type { Flow, FlowDefinition } from "../processor/flow.js";
2026-05-12 08:06:58 -05:00
import {
RequestResponseFactory,
type EffectRequestResponse,
} from "../messaging/runtime.js";
export class RequestResponseSpec<TReq, TRes> implements Spec {
2026-05-12 08:06:58 -05:00
public readonly name: string;
private readonly requestTopicName: string;
private readonly responseTopicName: string;
2026-05-12 08:06:58 -05:00
constructor(
name: string,
requestTopicName: string,
responseTopicName: string,
) {
this.name = name;
this.requestTopicName = requestTopicName;
this.responseTopicName = responseTopicName;
}
2026-05-12 08:06:58 -05:00
addEffect(flow: Flow, definition: FlowDefinition) {
const spec = this;
return Effect.gen(function* () {
const requestTopic = definition.topics?.[spec.requestTopicName] ?? spec.requestTopicName;
const responseTopic = definition.topics?.[spec.responseTopicName] ?? spec.responseTopicName;
const factory = yield* RequestResponseFactory;
const requestor = yield* factory.make<TReq, TRes>({
requestTopic,
responseTopic,
subscription: `${flow.processorId}-${flow.name}-${spec.name}`,
});
flow.registerRequestor(spec.name, requestor as EffectRequestResponse<unknown, unknown>);
});
2026-05-12 08:06:58 -05:00
}
2026-05-12 08:06:58 -05:00
async add(flow: Flow, pubsub: PubSubBackend, definition: FlowDefinition): Promise<void> {
await flow.runInCompatibilityScope(this.addEffect(flow, definition), pubsub);
}
}