feat: add Docker entrypoints, LLM providers, pipeline hardening, workbench pages
Phase 9 — four parallel workstreams:
- Stream A: 14 Docker entrypoints for containerized deployment
- Stream B: Pipeline hardening — robust JSON parsing, LLM retry logic,
consumer negative-ack, FalkorDB test import fix
- Stream C: Azure OpenAI, OpenAI-compatible, and Mistral LLM providers
- Stream D: Workbench Prompts, Token Cost, Knowledge Cores pages +
Settings feature switches
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-07 03:22:55 -05:00
|
|
|
|
/**
|
|
|
|
|
|
* Mistral text completion service.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Env:
|
|
|
|
|
|
* MISTRAL_TOKEN (required – Mistral API key)
|
|
|
|
|
|
* MISTRAL_MODEL (default: ministral-8b-latest)
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
import { Mistral } from "@mistralai/mistralai";
|
2026-06-02 02:34:03 -05:00
|
|
|
|
import { NodeRuntime } from "@effect/platform-node";
|
feat: add Docker entrypoints, LLM providers, pipeline hardening, workbench pages
Phase 9 — four parallel workstreams:
- Stream A: 14 Docker entrypoints for containerized deployment
- Stream B: Pipeline hardening — robust JSON parsing, LLM retry logic,
consumer negative-ack, FalkorDB test import fix
- Stream C: Azure OpenAI, OpenAI-compatible, and Mistral LLM providers
- Stream D: Workbench Prompts, Token Cost, Knowledge Cores pages +
Settings feature switches
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-07 03:22:55 -05:00
|
|
|
|
import {
|
2026-06-01 16:22:25 -05:00
|
|
|
|
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,
|
feat: add Docker entrypoints, LLM providers, pipeline hardening, workbench pages
Phase 9 — four parallel workstreams:
- Stream A: 14 Docker entrypoints for containerized deployment
- Stream B: Pipeline hardening — robust JSON parsing, LLM retry logic,
consumer negative-ack, FalkorDB test import fix
- Stream C: Azure OpenAI, OpenAI-compatible, and Mistral LLM providers
- Stream D: Workbench Prompts, Token Cost, Knowledge Cores pages +
Settings feature switches
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-07 03:22:55 -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";
|
feat: add Docker entrypoints, LLM providers, pipeline hardening, workbench pages
Phase 9 — four parallel workstreams:
- Stream A: 14 Docker entrypoints for containerized deployment
- Stream B: Pipeline hardening — robust JSON parsing, LLM retry logic,
consumer negative-ack, FalkorDB test import fix
- Stream C: Azure OpenAI, OpenAI-compatible, and Mistral LLM providers
- Stream D: Workbench Prompts, Token Cost, Knowledge Cores pages +
Settings feature switches
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-07 03:22:55 -05:00
|
|
|
|
|
2026-06-01 20:26:47 -05:00
|
|
|
|
export type MistralProcessorConfig = ProcessorConfig & {
|
|
|
|
|
|
model?: string;
|
|
|
|
|
|
apiKey?: string;
|
|
|
|
|
|
temperature?: number;
|
|
|
|
|
|
maxOutput?: number;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-06-01 23:19:54 -05:00
|
|
|
|
type ResolvedMistralConfig = {
|
|
|
|
|
|
readonly defaultModel: string;
|
|
|
|
|
|
readonly defaultTemperature: number;
|
|
|
|
|
|
readonly maxOutput: number;
|
|
|
|
|
|
readonly apiKey: string;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const loadMistralConfig = Effect.fn("loadMistralConfig")(function*(config: MistralProcessorConfig) {
|
|
|
|
|
|
const apiKey = yield* requiredString(
|
|
|
|
|
|
config.apiKey ?? (yield* optionalStringConfig("Mistral", "MISTRAL_TOKEN")),
|
|
|
|
|
|
"Mistral",
|
|
|
|
|
|
"MISTRAL_TOKEN",
|
|
|
|
|
|
"Mistral API key not specified",
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
defaultModel:
|
|
|
|
|
|
config.model ??
|
|
|
|
|
|
(yield* optionalStringConfig("Mistral", "MISTRAL_MODEL")) ??
|
|
|
|
|
|
"ministral-8b-latest",
|
|
|
|
|
|
defaultTemperature: config.temperature ?? 0.0,
|
|
|
|
|
|
maxOutput: config.maxOutput ?? 4096,
|
|
|
|
|
|
apiKey,
|
|
|
|
|
|
} satisfies ResolvedMistralConfig;
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const mapMistralError = (error: unknown): TextCompletionRuntimeError =>
|
|
|
|
|
|
providerStatusError("Mistral", error);
|
|
|
|
|
|
|
2026-06-01 20:26:47 -05:00
|
|
|
|
export function makeMistralProvider(config: MistralProcessorConfig): LlmProvider {
|
2026-06-01 23:19:54 -05:00
|
|
|
|
const {
|
|
|
|
|
|
defaultModel,
|
|
|
|
|
|
defaultTemperature,
|
|
|
|
|
|
maxOutput,
|
|
|
|
|
|
apiKey,
|
|
|
|
|
|
} = Effect.runSync(loadMistralConfig(config)) satisfies ResolvedMistralConfig;
|
feat: add Docker entrypoints, LLM providers, pipeline hardening, workbench pages
Phase 9 — four parallel workstreams:
- Stream A: 14 Docker entrypoints for containerized deployment
- Stream B: Pipeline hardening — robust JSON parsing, LLM retry logic,
consumer negative-ack, FalkorDB test import fix
- Stream C: Azure OpenAI, OpenAI-compatible, and Mistral LLM providers
- Stream D: Workbench Prompts, Token Cost, Knowledge Cores pages +
Settings feature switches
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-07 03:22:55 -05:00
|
|
|
|
|
2026-06-01 20:26:47 -05:00
|
|
|
|
const client = new Mistral({ apiKey });
|
feat: add Docker entrypoints, LLM providers, pipeline hardening, workbench pages
Phase 9 — four parallel workstreams:
- Stream A: 14 Docker entrypoints for containerized deployment
- Stream B: Pipeline hardening — robust JSON parsing, LLM retry logic,
consumer negative-ack, FalkorDB test import fix
- Stream C: Azure OpenAI, OpenAI-compatible, and Mistral LLM providers
- Stream D: Workbench Prompts, Token Cost, Knowledge Cores pages +
Settings feature switches
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-07 03:22:55 -05:00
|
|
|
|
|
2026-06-01 23:19:54 -05:00
|
|
|
|
Effect.runSync(Effect.log("[Mistral] 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.chat.complete({
|
|
|
|
|
|
model: modelName,
|
|
|
|
|
|
messages: [
|
|
|
|
|
|
{ role: "system", content: system },
|
|
|
|
|
|
{ role: "user", content: prompt },
|
|
|
|
|
|
],
|
|
|
|
|
|
temperature: temp,
|
|
|
|
|
|
maxTokens: maxOutput,
|
|
|
|
|
|
}),
|
|
|
|
|
|
catch: mapMistralError,
|
|
|
|
|
|
}).pipe(
|
|
|
|
|
|
Effect.map((resp): LlmResult => ({
|
|
|
|
|
|
text: (resp.choices?.[0]?.message?.content as string) ?? "",
|
|
|
|
|
|
inToken: resp.usage?.promptTokens ?? 0,
|
|
|
|
|
|
outToken: resp.usage?.completionTokens ?? 0,
|
|
|
|
|
|
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.tryPromise({
|
|
|
|
|
|
try: () =>
|
|
|
|
|
|
client.chat.stream({
|
|
|
|
|
|
model: modelName,
|
|
|
|
|
|
messages: [
|
|
|
|
|
|
{ role: "system", content: system },
|
|
|
|
|
|
{ role: "user", content: prompt },
|
|
|
|
|
|
],
|
|
|
|
|
|
temperature: temp,
|
|
|
|
|
|
maxTokens: maxOutput,
|
|
|
|
|
|
}),
|
|
|
|
|
|
catch: mapMistralError,
|
|
|
|
|
|
}),
|
|
|
|
|
|
).pipe(
|
|
|
|
|
|
Stream.flatMap((mistralStream) => {
|
|
|
|
|
|
const iterator = mistralStream[Symbol.asyncIterator]();
|
2026-06-01 20:26:47 -05:00
|
|
|
|
let totalInputTokens = 0;
|
|
|
|
|
|
let totalOutputTokens = 0;
|
|
|
|
|
|
|
2026-06-01 23:19:54 -05:00
|
|
|
|
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: mapMistralError,
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (next.done === true) {
|
|
|
|
|
|
return [{
|
|
|
|
|
|
text: "",
|
|
|
|
|
|
inToken: totalInputTokens,
|
|
|
|
|
|
outToken: totalOutputTokens,
|
|
|
|
|
|
model: modelName,
|
|
|
|
|
|
isFinal: true,
|
|
|
|
|
|
}, "done"] as const;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const chunk = next.value;
|
|
|
|
|
|
const delta = chunk.data?.choices?.[0]?.delta;
|
|
|
|
|
|
const content = delta?.content;
|
|
|
|
|
|
if (chunk.data?.usage !== undefined) {
|
|
|
|
|
|
totalInputTokens = chunk.data.usage.promptTokens ?? 0;
|
|
|
|
|
|
totalOutputTokens = chunk.data.usage.completionTokens ?? 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (typeof content === "string" && content.length > 0) {
|
|
|
|
|
|
return [{
|
|
|
|
|
|
text: content,
|
|
|
|
|
|
inToken: null,
|
|
|
|
|
|
outToken: null,
|
|
|
|
|
|
model: modelName,
|
|
|
|
|
|
isFinal: false,
|
|
|
|
|
|
}, "pulling"] as const;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
},
|
|
|
|
|
|
);
|
|
|
|
|
|
}),
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
return toAsyncGenerator(Stream.toAsyncIterable(stream), mapMistralError);
|
2026-06-01 20:26:47 -05:00
|
|
|
|
},
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
feat: add Docker entrypoints, LLM providers, pipeline hardening, workbench pages
Phase 9 — four parallel workstreams:
- Stream A: 14 Docker entrypoints for containerized deployment
- Stream B: Pipeline hardening — robust JSON parsing, LLM retry logic,
consumer negative-ack, FalkorDB test import fix
- Stream C: Azure OpenAI, OpenAI-compatible, and Mistral LLM providers
- Stream D: Workbench Prompts, Token Cost, Knowledge Cores pages +
Settings feature switches
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-07 03:22:55 -05:00
|
|
|
|
|
2026-06-01 20:26:47 -05:00
|
|
|
|
export type MistralProcessor = ReturnType<typeof makeMistralProcessor>;
|
|
|
|
|
|
|
2026-06-01 23:19:54 -05:00
|
|
|
|
export function makeMistralProcessor(
|
|
|
|
|
|
config: MistralProcessorConfig,
|
|
|
|
|
|
): ReturnType<typeof makeLlmService> {
|
2026-06-01 20:26:47 -05:00
|
|
|
|
return makeLlmService(config, makeMistralProvider(config));
|
feat: add Docker entrypoints, LLM providers, pipeline hardening, workbench pages
Phase 9 — four parallel workstreams:
- Stream A: 14 Docker entrypoints for containerized deployment
- Stream B: Pipeline hardening — robust JSON parsing, LLM retry logic,
consumer negative-ack, FalkorDB test import fix
- Stream C: Azure OpenAI, OpenAI-compatible, and Mistral LLM providers
- Stream D: Workbench Prompts, Token Cost, Knowledge Cores pages +
Settings feature switches
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-07 03:22:55 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-01 20:26:47 -05:00
|
|
|
|
export const MistralProcessor = makeMistralProcessor;
|
|
|
|
|
|
|
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(makeMistralProvider(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 mistralTextCompletionRuntime = 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 mistralTextCompletionRuntime.runPromise(program);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function runMain(): void {
|
|
|
|
|
|
NodeRuntime.runMain(program);
|
feat: add Docker entrypoints, LLM providers, pipeline hardening, workbench pages
Phase 9 — four parallel workstreams:
- Stream A: 14 Docker entrypoints for containerized deployment
- Stream B: Pipeline hardening — robust JSON parsing, LLM retry logic,
consumer negative-ack, FalkorDB test import fix
- Stream C: Azure OpenAI, OpenAI-compatible, and Mistral LLM providers
- Stream D: Workbench Prompts, Token Cost, Knowledge Cores pages +
Settings feature switches
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-07 03:22:55 -05:00
|
|
|
|
}
|