From 0b8040a3be7bdb930588e3c8af2c9fa2c5cccb10 Mon Sep 17 00:00:00 2001 From: Kit Langton Date: Wed, 6 May 2026 13:30:56 -0400 Subject: [PATCH] refactor(llm): move system helpers onto schema --- packages/llm/src/llm.ts | 11 +++-------- packages/llm/src/schema.ts | 14 ++++++++++++-- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/packages/llm/src/llm.ts b/packages/llm/src/llm.ts index bca1b0be74..60415b87a7 100644 --- a/packages/llm/src/llm.ts +++ b/packages/llm/src/llm.ts @@ -10,10 +10,10 @@ import { HttpOptions, LLMRequest, Message, + SystemPart, ToolChoice, ToolDefinition, type ContentPart, - type SystemPart, ToolCallPart, ToolResultPart, } from "./schema" @@ -50,12 +50,7 @@ export const limits = modelLimits export const text = Message.text -export const system = (value: string): SystemPart => ({ type: "text", text: value }) - -const systemParts = (input?: string | SystemPart | ReadonlyArray) => { - if (input === undefined) return [] - return typeof input === "string" ? [system(input)] : Array.isArray(input) ? [...input] : [input] -} +export const system = SystemPart.make export const message = Message.make @@ -97,7 +92,7 @@ export const request = (input: RequestInput) => { } = input return new LLMRequest({ ...rest, - system: systemParts(requestSystem), + system: SystemPart.content(requestSystem), messages: [...(messages?.map(message) ?? []), ...(prompt === undefined ? [] : [user(prompt)])], tools: tools?.map(toolDefinition) ?? [], toolChoice: requestToolChoice ? toolChoice(requestToolChoice) : undefined, diff --git a/packages/llm/src/schema.ts b/packages/llm/src/schema.ts index ccb3e26e7f..29ad8f3e22 100644 --- a/packages/llm/src/schema.ts +++ b/packages/llm/src/schema.ts @@ -253,13 +253,23 @@ export class CacheHint extends Schema.Class("LLM.CacheHint")({ ttlSeconds: Schema.optional(Schema.Number), }) {} -export const SystemPart = Schema.Struct({ +const systemPartSchema = Schema.Struct({ type: Schema.Literal("text"), text: Schema.String, cache: Schema.optional(CacheHint), metadata: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)), }).annotate({ identifier: "LLM.SystemPart" }) -export type SystemPart = Schema.Schema.Type +export type SystemPart = Schema.Schema.Type + +const makeSystemPart = (text: string): SystemPart => ({ type: "text", text }) + +export const SystemPart = Object.assign(systemPartSchema, { + make: makeSystemPart, + content: (input?: string | SystemPart | ReadonlyArray) => { + if (input === undefined) return [] + return typeof input === "string" ? [makeSystemPart(input)] : Array.isArray(input) ? [...input] : [input] + }, +}) export const TextPart = Schema.Struct({ type: Schema.Literal("text"),