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 {
|
2026-06-01 20:26:47 -05:00
|
|
|
makeLlmService,
|
2026-06-01 16:22:25 -05:00
|
|
|
makeFlowProcessorProgram,
|
|
|
|
|
makeLlmSpecs,
|
2026-06-02 05:09:15 -05:00
|
|
|
type Llm,
|
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 {
|
2026-06-02 04:33:48 -05:00
|
|
|
llmStreamPart,
|
2026-06-02 05:09:15 -05:00
|
|
|
makeTextCompletionLayer,
|
2026-06-01 23:19:54 -05:00
|
|
|
optionalStringConfig,
|
|
|
|
|
providerStatusError,
|
|
|
|
|
requiredString,
|
2026-06-02 04:33:48 -05:00
|
|
|
streamTextCompletionChunks,
|
2026-06-01 23:19:54 -05:00
|
|
|
toAsyncGenerator,
|
2026-06-02 05:09:15 -05:00
|
|
|
type TextCompletionConfigError,
|
2026-06-01 23:19:54 -05:00
|
|
|
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-02 05:09:15 -05:00
|
|
|
const makeClaudeProviderFromClient = (
|
|
|
|
|
resolved: ResolvedClaudeConfig,
|
|
|
|
|
client: Anthropic,
|
|
|
|
|
): LlmProvider => {
|
2026-06-01 23:19:54 -05:00
|
|
|
const {
|
|
|
|
|
defaultModel,
|
|
|
|
|
defaultTemperature,
|
|
|
|
|
maxOutput,
|
2026-06-02 05:09:15 -05:00
|
|
|
} = resolved;
|
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(
|
2026-06-02 04:33:48 -05:00
|
|
|
Stream.flatMap((anthropicStream) =>
|
|
|
|
|
streamTextCompletionChunks(anthropicStream, {
|
|
|
|
|
model: modelName,
|
|
|
|
|
mapError: mapClaudeError,
|
|
|
|
|
extract: (event) =>
|
|
|
|
|
event.type === "content_block_delta" && event.delta.type === "text_delta"
|
|
|
|
|
? llmStreamPart({ text: event.delta.text })
|
|
|
|
|
: llmStreamPart({}),
|
|
|
|
|
finalTokens: Effect.tryPromise({
|
|
|
|
|
try: () => anthropicStream.finalMessage(),
|
|
|
|
|
catch: mapClaudeError,
|
|
|
|
|
}).pipe(
|
|
|
|
|
Effect.map((finalMessage) => ({
|
|
|
|
|
inToken: finalMessage.usage.input_tokens,
|
|
|
|
|
outToken: finalMessage.usage.output_tokens,
|
|
|
|
|
})),
|
|
|
|
|
),
|
|
|
|
|
})
|
|
|
|
|
),
|
2026-06-01 23:19:54 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return toAsyncGenerator(Stream.toAsyncIterable(stream), mapClaudeError);
|
2026-06-01 20:26:47 -05:00
|
|
|
},
|
2026-06-02 05:09:15 -05:00
|
|
|
} satisfies LlmProvider;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export function makeClaudeProvider(config: ClaudeProcessorConfig): LlmProvider {
|
|
|
|
|
return Effect.runSync(makeClaudeProviderEffect(config));
|
2026-06-01 20:26:47 -05:00
|
|
|
}
|
|
|
|
|
|
2026-06-02 05:09:15 -05:00
|
|
|
export const makeClaudeProviderEffect = Effect.fn("makeClaudeProvider")(function*(
|
|
|
|
|
config: ClaudeProcessorConfig,
|
|
|
|
|
) {
|
|
|
|
|
const resolved = yield* loadClaudeConfig(config);
|
|
|
|
|
const client = yield* Effect.try({
|
|
|
|
|
try: () => new Anthropic({ apiKey: resolved.apiKey }),
|
|
|
|
|
catch: mapClaudeError,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
yield* Effect.log("[Claude] LLM service initialized");
|
|
|
|
|
return makeClaudeProviderFromClient(resolved, client);
|
|
|
|
|
});
|
|
|
|
|
|
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-02 05:09:15 -05:00
|
|
|
export const program = makeFlowProcessorProgram<
|
|
|
|
|
ClaudeProcessorConfig,
|
|
|
|
|
TextCompletionConfigError | TextCompletionRuntimeError,
|
|
|
|
|
Llm
|
|
|
|
|
>({
|
2026-05-12 08:06:58 -05:00
|
|
|
id: "text-completion",
|
2026-06-01 16:22:25 -05:00
|
|
|
specs: () => makeLlmSpecs(),
|
2026-06-02 05:09:15 -05:00
|
|
|
layer: (config) => makeTextCompletionLayer(makeClaudeProviderEffect(config)),
|
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
|
|
|
}
|