File size: 2,156 Bytes
25d91a4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | import type { LlmRuntime } from "@openclaw/ai";
import type { Model } from "./types.js";
const MODEL_LLM_RUNTIME = Symbol("openclaw.modelLlmRuntime");
const streamLlmRuntimes = new WeakMap<object, LlmRuntime>();
type ModelCompletionOwner = {
run: <T>(run: () => Promise<T>) => Promise<T>;
assertCurrent: () => void;
};
type ModelRuntimeBinding = {
runtime?: LlmRuntime;
completionTransport?: Model;
completionOwner?: ModelCompletionOwner;
};
type RuntimeBoundModel = Model & {
[MODEL_LLM_RUNTIME]?: ModelRuntimeBinding;
};
function bindModelRuntime(model: Model, binding: ModelRuntimeBinding): Model {
const bound: RuntimeBoundModel = { ...model };
Object.defineProperty(bound, MODEL_LLM_RUNTIME, {
value: binding,
enumerable: false,
});
return bound;
}
/** Carries the prepared lifecycle runtime without changing the serialized model shape. */
export function bindModelLlmRuntime(
model: Model,
runtime: LlmRuntime,
completionTransport?: Model,
): Model {
return bindModelRuntime(model, {
runtime,
completionTransport,
completionOwner: getModelCompletionOwner(model),
});
}
export function bindModelCompletionOwner(
model: RuntimeBoundModel,
completionOwner: ModelCompletionOwner,
): Model {
return bindModelRuntime(model, { ...model[MODEL_LLM_RUNTIME], completionOwner });
}
export function getModelCompletionOwner(
model: RuntimeBoundModel,
): ModelCompletionOwner | undefined {
return model[MODEL_LLM_RUNTIME]?.completionOwner;
}
export function getModelLlmRuntime(model: RuntimeBoundModel): LlmRuntime | undefined {
return model[MODEL_LLM_RUNTIME]?.runtime;
}
export function getModelCompletionTransport(model: RuntimeBoundModel): Model | undefined {
return model[MODEL_LLM_RUNTIME]?.completionTransport;
}
/** Associates a prepared stream entry point with the runtime that owns it. */
export function bindStreamLlmRuntime(streamFn: object, runtime: LlmRuntime): void {
streamLlmRuntimes.set(streamFn, runtime);
}
export function getStreamLlmRuntime(streamFn: object | undefined): LlmRuntime | undefined {
return streamFn ? streamLlmRuntimes.get(streamFn) : undefined;
}
|