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
|
|
|
|
/**
|
|
|
|
|
|
* OpenAI-compatible text completion service (generic local server).
|
|
|
|
|
|
*
|
|
|
|
|
|
* Works with LM Studio, llama.cpp, vLLM, Ollama OpenAI-compat endpoint, etc.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Env:
|
|
|
|
|
|
* OPENAI_COMPAT_URL (required – e.g. http://localhost:1234/v1)
|
|
|
|
|
|
* OPENAI_COMPAT_KEY (default: sk-no-key-required)
|
|
|
|
|
|
* OPENAI_COMPAT_MODEL (default: default)
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
import OpenAI from "openai";
|
2026-06-02 02:34:03 -05:00
|
|
|
|
import { NodeRuntime } from "@effect/platform-node";
|
feat(ts): add real quality gates — Biome lint + effect-law ratchet + class inventory
- biome.json (2.4.16, linter-only) wired as "lint" in all six packages
- scripts/check-effect-laws.ts: Effect-native law enforcement encoding the
adapted beep-effect effect-first/schema-first laws (no native JSON/switch/
sort/fetch/timers, no process.env, no throw new, no Effect.run* outside
boundaries, no Schema-suffixed constants, no node:fs/path, AST-based
pure-data interface detection per law 38/39)
- ratcheting baseline allowlist (95 entries / 290 findings) that must shrink
to documented exemptions only; stale counts fail the gate
- root lint chains turbo lint + law check + native-class inventory
- fix all 163 initial Biome findings: import-type style, templates, two `any`s,
ten non-null assertions (librarian getService gate, A.matchRight in atoms,
ensureNode returning nodes, main.tsx mount guard)
Gates: lint, check:tsgo, build, test (force, 11 tasks) all green.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-11 06:40:01 -05:00
|
|
|
|
import type {
|
|
|
|
|
|
Llm,
|
|
|
|
|
|
LlmProvider,
|
|
|
|
|
|
ProcessorConfig,
|
|
|
|
|
|
LlmResult,
|
|
|
|
|
|
} from "@trustgraph/base";
|
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 20:26:47 -05:00
|
|
|
|
makeLlmService,
|
2026-06-01 16:22:25 -05:00
|
|
|
|
makeFlowProcessorProgram,
|
|
|
|
|
|
makeLlmSpecs,
|
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
|
|
|
|
} from "@trustgraph/base";
|
2026-06-06 10:33:10 -05:00
|
|
|
|
import { Effect, Stream } from "effect";
|
feat(ts): add real quality gates — Biome lint + effect-law ratchet + class inventory
- biome.json (2.4.16, linter-only) wired as "lint" in all six packages
- scripts/check-effect-laws.ts: Effect-native law enforcement encoding the
adapted beep-effect effect-first/schema-first laws (no native JSON/switch/
sort/fetch/timers, no process.env, no throw new, no Effect.run* outside
boundaries, no Schema-suffixed constants, no node:fs/path, AST-based
pure-data interface detection per law 38/39)
- ratcheting baseline allowlist (95 entries / 290 findings) that must shrink
to documented exemptions only; stale counts fail the gate
- root lint chains turbo lint + law check + native-class inventory
- fix all 163 initial Biome findings: import-type style, templates, two `any`s,
ten non-null assertions (librarian getService gate, A.matchRight in atoms,
ensureNode returning nodes, main.tsx mount guard)
Gates: lint, check:tsgo, build, test (force, 11 tasks) all green.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-11 06:40:01 -05:00
|
|
|
|
import type {
|
|
|
|
|
|
TextCompletionConfigError,
|
|
|
|
|
|
TextCompletionRuntimeError,
|
|
|
|
|
|
} from "./common.ts";
|
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
|
|
|
|
} 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 OpenAICompatibleProcessorConfig = ProcessorConfig & {
|
|
|
|
|
|
model?: string;
|
|
|
|
|
|
apiKey?: string;
|
|
|
|
|
|
baseUrl?: string;
|
|
|
|
|
|
temperature?: number;
|
|
|
|
|
|
maxOutput?: number;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-06-01 23:19:54 -05:00
|
|
|
|
type ResolvedOpenAICompatibleConfig = {
|
|
|
|
|
|
readonly defaultModel: string;
|
|
|
|
|
|
readonly defaultTemperature: number;
|
|
|
|
|
|
readonly maxOutput: number;
|
|
|
|
|
|
readonly apiKey: string;
|
|
|
|
|
|
readonly baseURL: string;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const loadOpenAICompatibleConfig = Effect.fn("loadOpenAICompatibleConfig")(function*(
|
|
|
|
|
|
config: OpenAICompatibleProcessorConfig,
|
|
|
|
|
|
) {
|
|
|
|
|
|
const defaultModel =
|
|
|
|
|
|
config.model ?? (yield* optionalStringConfig("OpenAI-Compatible", "OPENAI_COMPAT_MODEL")) ?? "default";
|
|
|
|
|
|
const baseURL = yield* requiredString(
|
|
|
|
|
|
config.baseUrl ?? (yield* optionalStringConfig("OpenAI-Compatible", "OPENAI_COMPAT_URL")),
|
|
|
|
|
|
"OpenAI-Compatible",
|
|
|
|
|
|
"OPENAI_COMPAT_URL",
|
|
|
|
|
|
"OpenAI-compatible server URL not specified (set OPENAI_COMPAT_URL)",
|
|
|
|
|
|
);
|
|
|
|
|
|
const apiKey =
|
|
|
|
|
|
config.apiKey ?? (yield* optionalStringConfig("OpenAI-Compatible", "OPENAI_COMPAT_KEY")) ?? "sk-no-key-required";
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
defaultModel,
|
|
|
|
|
|
defaultTemperature: config.temperature ?? 0.0,
|
|
|
|
|
|
maxOutput: config.maxOutput ?? 4096,
|
|
|
|
|
|
apiKey,
|
|
|
|
|
|
baseURL,
|
|
|
|
|
|
} satisfies ResolvedOpenAICompatibleConfig;
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const mapOpenAICompatibleError = (error: unknown): TextCompletionRuntimeError =>
|
|
|
|
|
|
providerStatusError("OpenAI-Compatible", error);
|
|
|
|
|
|
|
2026-06-02 05:09:15 -05:00
|
|
|
|
const makeOpenAICompatibleProviderFromClient = (
|
|
|
|
|
|
resolved: ResolvedOpenAICompatibleConfig,
|
|
|
|
|
|
client: OpenAI,
|
2026-06-06 10:33:10 -05:00
|
|
|
|
): LlmProvider<TextCompletionRuntimeError> => {
|
2026-06-01 23:19:54 -05:00
|
|
|
|
const {
|
|
|
|
|
|
defaultModel,
|
|
|
|
|
|
defaultTemperature,
|
|
|
|
|
|
maxOutput,
|
2026-06-02 05:09:15 -05:00
|
|
|
|
} = resolved;
|
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
|
|
|
|
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,
|
2026-06-06 10:33:10 -05:00
|
|
|
|
) => {
|
2026-06-01 20:26:47 -05:00
|
|
|
|
const modelName = model ?? defaultModel;
|
|
|
|
|
|
const temp = temperature ?? defaultTemperature;
|
|
|
|
|
|
|
2026-06-06 10:33:10 -05:00
|
|
|
|
return Effect.tryPromise({
|
|
|
|
|
|
try: () =>
|
|
|
|
|
|
client.chat.completions.create({
|
2026-06-01 23:19:54 -05:00
|
|
|
|
model: modelName,
|
2026-06-06 10:33:10 -05:00
|
|
|
|
messages: [
|
|
|
|
|
|
{ role: "system", content: system },
|
|
|
|
|
|
{ role: "user", content: prompt },
|
|
|
|
|
|
],
|
|
|
|
|
|
temperature: temp,
|
|
|
|
|
|
max_tokens: maxOutput,
|
|
|
|
|
|
}),
|
|
|
|
|
|
catch: mapOpenAICompatibleError,
|
|
|
|
|
|
}).pipe(
|
|
|
|
|
|
Effect.map((resp): LlmResult => ({
|
|
|
|
|
|
text: resp.choices[0].message.content ?? "",
|
|
|
|
|
|
inToken: resp.usage?.prompt_tokens ?? 0,
|
|
|
|
|
|
outToken: resp.usage?.completion_tokens ?? 0,
|
|
|
|
|
|
model: modelName,
|
|
|
|
|
|
})),
|
2026-06-01 23:19:54 -05:00
|
|
|
|
);
|
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-06 10:33:10 -05:00
|
|
|
|
) => {
|
2026-06-01 20:26:47 -05:00
|
|
|
|
const modelName = model ?? defaultModel;
|
|
|
|
|
|
const temp = temperature ?? defaultTemperature;
|
|
|
|
|
|
|
2026-06-06 10:33:10 -05:00
|
|
|
|
return Stream.fromEffect(
|
2026-06-01 23:19:54 -05:00
|
|
|
|
Effect.tryPromise({
|
|
|
|
|
|
try: () =>
|
|
|
|
|
|
client.chat.completions.create({
|
|
|
|
|
|
model: modelName,
|
|
|
|
|
|
messages: [
|
|
|
|
|
|
{ role: "system", content: system },
|
|
|
|
|
|
{ role: "user", content: prompt },
|
|
|
|
|
|
],
|
|
|
|
|
|
temperature: temp,
|
|
|
|
|
|
max_tokens: maxOutput,
|
|
|
|
|
|
stream: true,
|
|
|
|
|
|
}),
|
|
|
|
|
|
catch: mapOpenAICompatibleError,
|
|
|
|
|
|
}),
|
|
|
|
|
|
).pipe(
|
2026-06-02 04:33:48 -05:00
|
|
|
|
Stream.flatMap((openAIStream) =>
|
|
|
|
|
|
streamTextCompletionChunks(openAIStream, {
|
|
|
|
|
|
model: modelName,
|
|
|
|
|
|
mapError: mapOpenAICompatibleError,
|
|
|
|
|
|
extract: (chunk) =>
|
|
|
|
|
|
llmStreamPart({
|
|
|
|
|
|
text: chunk.choices[0]?.delta?.content,
|
|
|
|
|
|
inToken: chunk.usage?.prompt_tokens,
|
|
|
|
|
|
outToken: chunk.usage?.completion_tokens,
|
|
|
|
|
|
}),
|
|
|
|
|
|
})
|
|
|
|
|
|
),
|
2026-06-01 23:19:54 -05:00
|
|
|
|
);
|
2026-06-01 20:26:47 -05:00
|
|
|
|
},
|
2026-06-06 10:33:10 -05:00
|
|
|
|
} satisfies LlmProvider<TextCompletionRuntimeError>;
|
2026-06-02 05:09:15 -05:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export function makeOpenAICompatibleProvider(
|
|
|
|
|
|
config: OpenAICompatibleProcessorConfig,
|
2026-06-06 10:33:10 -05:00
|
|
|
|
): LlmProvider<TextCompletionRuntimeError> {
|
2026-06-11 07:47:44 -05:00
|
|
|
|
const resolved = {
|
|
|
|
|
|
defaultModel: config.model ?? "default",
|
|
|
|
|
|
defaultTemperature: config.temperature ?? 0.0,
|
|
|
|
|
|
maxOutput: config.maxOutput ?? 4096,
|
|
|
|
|
|
apiKey: config.apiKey ?? "sk-no-key-required",
|
|
|
|
|
|
baseURL: config.baseUrl ?? "http://localhost:1234/v1",
|
|
|
|
|
|
} satisfies ResolvedOpenAICompatibleConfig;
|
|
|
|
|
|
return makeOpenAICompatibleProviderFromClient(
|
|
|
|
|
|
resolved,
|
|
|
|
|
|
new OpenAI({ baseURL: resolved.baseURL, apiKey: resolved.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-02 05:09:15 -05:00
|
|
|
|
export const makeOpenAICompatibleProviderEffect = Effect.fn("makeOpenAICompatibleProvider")(function*(
|
|
|
|
|
|
config: OpenAICompatibleProcessorConfig,
|
|
|
|
|
|
) {
|
|
|
|
|
|
const resolved = yield* loadOpenAICompatibleConfig(config);
|
|
|
|
|
|
const client = yield* Effect.try({
|
|
|
|
|
|
try: () => new OpenAI({ baseURL: resolved.baseURL, apiKey: resolved.apiKey }),
|
|
|
|
|
|
catch: mapOpenAICompatibleError,
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
yield* Effect.log("[OpenAI-Compatible] LLM service initialized");
|
|
|
|
|
|
return makeOpenAICompatibleProviderFromClient(resolved, client);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-06-01 20:26:47 -05:00
|
|
|
|
export type OpenAICompatibleProcessor = ReturnType<typeof makeOpenAICompatibleProcessor>;
|
|
|
|
|
|
|
|
|
|
|
|
export function makeOpenAICompatibleProcessor(
|
|
|
|
|
|
config: OpenAICompatibleProcessorConfig,
|
|
|
|
|
|
): ReturnType<typeof makeLlmService> {
|
|
|
|
|
|
return makeLlmService(config, makeOpenAICompatibleProvider(config));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export const OpenAICompatibleProcessor = makeOpenAICompatibleProcessor;
|
|
|
|
|
|
|
2026-06-02 05:09:15 -05:00
|
|
|
|
export const program = makeFlowProcessorProgram<
|
|
|
|
|
|
OpenAICompatibleProcessorConfig,
|
|
|
|
|
|
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(makeOpenAICompatibleProviderEffect(config)),
|
2026-05-12 08:06:58 -05:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-06-02 02:34:03 -05:00
|
|
|
|
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
|
|
|
|
}
|