2026-04-05 21:09:33 -05:00
|
|
|
/**
|
|
|
|
|
* Anthropic Claude text completion service.
|
|
|
|
|
*
|
|
|
|
|
* Python reference: trustgraph-flow/trustgraph/model/text_completion/claude/llm.py
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import Anthropic from "@anthropic-ai/sdk";
|
2026-06-02 02:34:03 -05:00
|
|
|
import { NodeRuntime } from "@effect/platform-node";
|
2026-06-01 16:22:25 -05:00
|
|
|
import {
|
|
|
|
|
Llm,
|
2026-06-01 20:26:47 -05:00
|
|
|
makeLlmService,
|
2026-06-01 16:22:25 -05:00
|
|
|
makeFlowProcessorProgram,
|
|
|
|
|
makeLlmServiceShape,
|
|
|
|
|
makeLlmSpecs,
|
2026-06-01 20:26:47 -05:00
|
|
|
type LlmProvider,
|
2026-06-01 16:22:25 -05:00
|
|
|
type ProcessorConfig,
|
|
|
|
|
type LlmResult,
|
|
|
|
|
type LlmChunk,
|
|
|
|
|
} from "@trustgraph/base";
|
2026-06-02 02:34:03 -05:00
|
|
|
import { Effect, Layer, ManagedRuntime, Stream } from "effect";
|
2026-06-01 23:19:54 -05:00
|
|
|
import {
|
|
|
|
|
optionalStringConfig,
|
|
|
|
|
providerStatusError,
|
|
|
|
|
requiredString,
|
|
|
|
|
toAsyncGenerator,
|
|
|
|
|
type TextCompletionRuntimeError,
|
|
|
|
|
} from "./common.ts";
|
2026-04-05 21:09:33 -05:00
|
|
|
|
2026-06-01 20:26:47 -05:00
|
|
|
export type ClaudeProcessorConfig = ProcessorConfig & {
|
|
|
|
|
model?: string;
|
|
|
|
|
apiKey?: string;
|
|
|
|
|
temperature?: number;
|
|
|
|
|
maxOutput?: number;
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-01 23:19:54 -05:00
|
|
|
type ResolvedClaudeConfig = {
|
|
|
|
|
readonly defaultModel: string;
|
|
|
|
|
readonly defaultTemperature: number;
|
|
|
|
|
readonly maxOutput: number;
|
|
|
|
|
readonly apiKey: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const loadClaudeConfig = Effect.fn("loadClaudeConfig")(function*(config: ClaudeProcessorConfig) {
|
|
|
|
|
const apiKey = yield* requiredString(
|
|
|
|
|
config.apiKey ?? (yield* optionalStringConfig("Claude", "CLAUDE_KEY")),
|
|
|
|
|
"Claude",
|
|
|
|
|
"CLAUDE_KEY",
|
|
|
|
|
"Claude API key not specified",
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
defaultModel: config.model ?? "claude-sonnet-4-20250514",
|
|
|
|
|
defaultTemperature: config.temperature ?? 0.0,
|
|
|
|
|
maxOutput: config.maxOutput ?? 8192,
|
|
|
|
|
apiKey,
|
|
|
|
|
} satisfies ResolvedClaudeConfig;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const mapClaudeError = (error: unknown): TextCompletionRuntimeError =>
|
|
|
|
|
providerStatusError("Claude", error);
|
|
|
|
|
|
2026-06-01 20:26:47 -05:00
|
|
|
export function makeClaudeProvider(config: ClaudeProcessorConfig): LlmProvider {
|
2026-06-01 23:19:54 -05:00
|
|
|
const {
|
|
|
|
|
defaultModel,
|
|
|
|
|
defaultTemperature,
|
|
|
|
|
maxOutput,
|
|
|
|
|
apiKey,
|
|
|
|
|
} = Effect.runSync(loadClaudeConfig(config)) satisfies ResolvedClaudeConfig;
|
2026-04-05 21:09:33 -05:00
|
|
|
|
2026-06-01 20:26:47 -05:00
|
|
|
const client = new Anthropic({ apiKey });
|
2026-04-05 21:09:33 -05:00
|
|
|
|
2026-06-01 23:19:54 -05:00
|
|
|
Effect.runSync(Effect.log("[Claude] LLM service initialized"));
|
2026-06-01 20:26:47 -05:00
|
|
|
|
|
|
|
|
return {
|
2026-06-01 23:19:54 -05:00
|
|
|
generateContent: (
|
2026-06-01 20:26:47 -05:00
|
|
|
system: string,
|
|
|
|
|
prompt: string,
|
|
|
|
|
model?: string,
|
|
|
|
|
temperature?: number,
|
|
|
|
|
): Promise<LlmResult> => {
|
|
|
|
|
const modelName = model ?? defaultModel;
|
|
|
|
|
const temp = temperature ?? defaultTemperature;
|
|
|
|
|
|
2026-06-01 23:19:54 -05:00
|
|
|
return Effect.runPromise(
|
|
|
|
|
Effect.tryPromise({
|
|
|
|
|
try: () =>
|
|
|
|
|
client.messages.create({
|
|
|
|
|
model: modelName,
|
|
|
|
|
max_tokens: maxOutput,
|
|
|
|
|
temperature: temp,
|
|
|
|
|
system,
|
|
|
|
|
messages: [
|
|
|
|
|
{ role: "user", content: prompt },
|
|
|
|
|
],
|
|
|
|
|
}),
|
|
|
|
|
catch: mapClaudeError,
|
|
|
|
|
}).pipe(
|
|
|
|
|
Effect.map((response): LlmResult => {
|
|
|
|
|
const firstContent = response.content[0];
|
|
|
|
|
const text = firstContent?.type === "text"
|
|
|
|
|
? firstContent.text
|
|
|
|
|
: "";
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
text,
|
|
|
|
|
inToken: response.usage.input_tokens,
|
|
|
|
|
outToken: response.usage.output_tokens,
|
|
|
|
|
model: modelName,
|
|
|
|
|
};
|
|
|
|
|
}),
|
|
|
|
|
),
|
|
|
|
|
);
|
2026-06-01 20:26:47 -05:00
|
|
|
},
|
|
|
|
|
supportsStreaming: () => true,
|
2026-06-01 23:19:54 -05:00
|
|
|
generateContentStream: (
|
2026-06-01 20:26:47 -05:00
|
|
|
system: string,
|
|
|
|
|
prompt: string,
|
|
|
|
|
model?: string,
|
|
|
|
|
temperature?: number,
|
2026-06-01 23:19:54 -05:00
|
|
|
): AsyncGenerator<LlmChunk> => {
|
2026-06-01 20:26:47 -05:00
|
|
|
const modelName = model ?? defaultModel;
|
|
|
|
|
const temp = temperature ?? defaultTemperature;
|
|
|
|
|
|
2026-06-01 23:19:54 -05:00
|
|
|
const stream = Stream.fromEffect(
|
|
|
|
|
Effect.try({
|
|
|
|
|
try: () =>
|
|
|
|
|
client.messages.stream({
|
2026-06-01 20:26:47 -05:00
|
|
|
model: modelName,
|
2026-06-01 23:19:54 -05:00
|
|
|
max_tokens: maxOutput,
|
|
|
|
|
temperature: temp,
|
|
|
|
|
system,
|
|
|
|
|
messages: [
|
|
|
|
|
{ role: "user", content: prompt },
|
|
|
|
|
],
|
|
|
|
|
}),
|
|
|
|
|
catch: mapClaudeError,
|
|
|
|
|
}),
|
|
|
|
|
).pipe(
|
|
|
|
|
Stream.flatMap((anthropicStream) => {
|
|
|
|
|
const iterator = anthropicStream[Symbol.asyncIterator]();
|
|
|
|
|
|
|
|
|
|
return Stream.unfold<"pulling" | "done", LlmChunk, TextCompletionRuntimeError, never>(
|
|
|
|
|
"pulling",
|
|
|
|
|
(state) => {
|
2026-06-02 02:55:06 -05:00
|
|
|
if (state === "done") return Effect.as(Effect.void, undefined);
|
2026-06-01 23:19:54 -05:00
|
|
|
|
|
|
|
|
return Effect.gen(function* () {
|
|
|
|
|
while (true) {
|
|
|
|
|
const next = yield* Effect.tryPromise({
|
|
|
|
|
try: () => iterator.next(),
|
|
|
|
|
catch: mapClaudeError,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (next.done === true) {
|
|
|
|
|
const finalMessage = yield* Effect.tryPromise({
|
|
|
|
|
try: () => anthropicStream.finalMessage(),
|
|
|
|
|
catch: mapClaudeError,
|
|
|
|
|
});
|
|
|
|
|
return [{
|
|
|
|
|
text: "",
|
|
|
|
|
inToken: finalMessage.usage.input_tokens,
|
|
|
|
|
outToken: finalMessage.usage.output_tokens,
|
|
|
|
|
model: modelName,
|
|
|
|
|
isFinal: true,
|
|
|
|
|
}, "done"] as const;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const event = next.value;
|
|
|
|
|
if (
|
|
|
|
|
event.type === "content_block_delta" &&
|
|
|
|
|
event.delta.type === "text_delta"
|
|
|
|
|
) {
|
|
|
|
|
return [{
|
|
|
|
|
text: event.delta.text,
|
|
|
|
|
inToken: null,
|
|
|
|
|
outToken: null,
|
|
|
|
|
model: modelName,
|
|
|
|
|
isFinal: false,
|
|
|
|
|
}, "pulling"] as const;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return toAsyncGenerator(Stream.toAsyncIterable(stream), mapClaudeError);
|
2026-06-01 20:26:47 -05:00
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type ClaudeProcessor = ReturnType<typeof makeClaudeProcessor>;
|
|
|
|
|
|
2026-06-01 23:19:54 -05:00
|
|
|
export function makeClaudeProcessor(
|
|
|
|
|
config: ClaudeProcessorConfig,
|
|
|
|
|
): ReturnType<typeof makeLlmService> {
|
2026-06-01 20:26:47 -05:00
|
|
|
return makeLlmService(config, makeClaudeProvider(config));
|
2026-04-05 21:09:33 -05:00
|
|
|
}
|
|
|
|
|
|
2026-06-01 20:26:47 -05:00
|
|
|
export const ClaudeProcessor = makeClaudeProcessor;
|
|
|
|
|
|
2026-06-01 16:22:25 -05:00
|
|
|
export const program = makeFlowProcessorProgram<ProcessorConfig, never, Llm>({
|
2026-05-12 08:06:58 -05:00
|
|
|
id: "text-completion",
|
2026-06-01 16:22:25 -05:00
|
|
|
specs: () => makeLlmSpecs(),
|
|
|
|
|
layer: (config) =>
|
|
|
|
|
Layer.succeed(
|
|
|
|
|
Llm,
|
2026-06-01 20:26:47 -05:00
|
|
|
Llm.of(makeLlmServiceShape(makeClaudeProvider(config))),
|
2026-06-01 16:22:25 -05:00
|
|
|
),
|
2026-05-12 08:06:58 -05:00
|
|
|
});
|
|
|
|
|
|
2026-06-02 02:34:03 -05:00
|
|
|
const claudeTextCompletionRuntime = ManagedRuntime.make(Layer.empty);
|
|
|
|
|
|
2026-06-01 23:19:54 -05:00
|
|
|
export function run(): Promise<void> {
|
2026-06-02 02:34:03 -05:00
|
|
|
return claudeTextCompletionRuntime.runPromise(program);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function runMain(): void {
|
|
|
|
|
NodeRuntime.runMain(program);
|
2026-04-05 21:09:33 -05:00
|
|
|
}
|