chore(llm): fix low-hanging lint warnings

This commit is contained in:
Kit Langton
2026-04-26 20:35:44 -04:00
parent 096c305a55
commit 03a97a64a3
7 changed files with 42 additions and 15 deletions
+10 -5
View File
@@ -12,7 +12,6 @@ import {
ToolChoice,
ToolDefinition,
type ContentPart,
type Protocol,
type ModelID as ModelIDType,
type ProviderID as ProviderIDType,
type ReasoningEffort,
@@ -50,7 +49,7 @@ export type ToolChoiceInput =
| string
export type ToolResultInput = Omit<ToolResultPart, "type" | "result"> & {
readonly result: ToolResultValue | unknown
readonly result: unknown
readonly resultType?: ToolResultValue["type"]
}
@@ -106,7 +105,7 @@ export const model = (input: ModelInput) => {
...rest,
id: ModelID.make(input.id),
provider: ProviderID.make(input.provider),
protocol: input.protocol as Protocol,
protocol: input.protocol,
capabilities: modelCapabilities instanceof ModelCapabilities ? modelCapabilities : capabilities(modelCapabilities),
limits: modelLimits instanceof ModelLimits ? modelLimits : limits(modelLimits),
})
@@ -119,8 +118,14 @@ export const tool = (input: ToolDefinition | ConstructorParameters<typeof ToolDe
export const toolCall = (input: Omit<ToolCallPart, "type">): ToolCallPart => ({ type: "tool-call", ...input })
const toolResultValue = (value: ToolResultValue | unknown, type: ToolResultValue["type"] = "json"): ToolResultValue => {
if (typeof value === "object" && value !== null && "type" in value && "value" in value) return value as ToolResultValue
const isRecord = (value: unknown): value is Record<string, unknown> =>
typeof value === "object" && value !== null && !Array.isArray(value)
const isToolResultValue = (value: unknown): value is ToolResultValue =>
isRecord(value) && (value.type === "text" || value.type === "json" || value.type === "error") && "value" in value
const toolResultValue = (value: unknown, type: ToolResultValue["type"] = "json"): ToolResultValue => {
if (isToolResultValue(value)) return value
return { type, value }
}
@@ -1,4 +1,4 @@
import { Effect, Schema, Stream } from "effect"
import { Effect, Schema } from "effect"
import type { HttpClientResponse } from "effect/unstable/http"
import { Adapter } from "../adapter"
import { capabilities, model as llmModel, type ModelInput } from "../llm"
@@ -8,7 +8,6 @@ import {
type FinishReason,
type LLMEvent,
type LLMRequest,
type TextPart,
type ToolCallPart,
type ToolDefinition,
type ToolResultPart,
+1 -1
View File
@@ -1,4 +1,4 @@
import { Effect, Schema, Stream } from "effect"
import { Effect, Schema } from "effect"
import type { HttpClientResponse } from "effect/unstable/http"
import { Adapter } from "../adapter"
import { capabilities, model as llmModel, type ModelInput } from "../llm"
+1 -2
View File
@@ -1,11 +1,10 @@
import { Effect, Schema, Stream } from "effect"
import { Effect, Schema } from "effect"
import type { HttpClientResponse } from "effect/unstable/http"
import { Adapter } from "../adapter"
import { capabilities, model as llmModel, type ModelInput } from "../llm"
import {
Usage,
type FinishReason,
type ContentPart,
type LLMEvent,
type LLMRequest,
type TextPart,
@@ -1,4 +1,4 @@
import { Effect, Schema, Stream } from "effect"
import { Effect, Schema } from "effect"
import type { HttpClientResponse } from "effect/unstable/http"
import { Adapter } from "../adapter"
import { capabilities, model as llmModel, type ModelInput } from "../llm"
@@ -322,7 +322,7 @@ const hostedToolResult = (item: OpenAIResponsesStreamItem) => {
}
const hostedToolEvents = (item: OpenAIResponsesStreamItem & { id: string }): ReadonlyArray<LLMEvent> => {
const name = HOSTED_TOOL_NAMES[item.type]!
const name = HOSTED_TOOL_NAMES[item.type]
return [
{ type: "tool-call", id: item.id, name, input: hostedToolInput(item), providerExecuted: true },
{ type: "tool-result", id: item.id, name, result: hostedToolResult(item), providerExecuted: true },
+10 -1
View File
@@ -51,6 +51,15 @@ export const toolResultText = (part: ToolResultPart) => {
return encodeJson(part.result.value)
}
const errorText = (error: unknown) => {
if (error instanceof Error) return error.message
if (typeof error === "string") return error
if (typeof error === "number" || typeof error === "boolean" || typeof error === "bigint") return String(error)
if (error === null) return "null"
if (error === undefined) return "undefined"
return "Unknown stream error"
}
const streamError = (adapter: string, message: string, cause: Cause.Cause<unknown>) => {
const failed = cause.reasons.find(Cause.isFailReason)?.error
if (failed instanceof ProviderChunkError) return failed
@@ -85,7 +94,7 @@ export const framed = <Frame, Chunk, State, Event>(input: {
readonly onHalt?: (state: State) => ReadonlyArray<Event>
}): Stream.Stream<Event, ProviderChunkError> => {
const bytes = input.response.stream.pipe(
Stream.mapError((error) => chunkError(input.adapter, input.readError, String(error))),
Stream.mapError((error) => chunkError(input.adapter, input.readError, errorText(error))),
)
return input.framing(bytes).pipe(
Stream.mapEffect(input.decodeChunk),
+17 -2
View File
@@ -43,6 +43,21 @@ export interface RunOptions<T extends Tools> {
readonly stopWhen?: (state: RuntimeState) => boolean
}
const requestInput = (request: LLMRequest): ConstructorParameters<typeof LLMRequest>[0] => ({
id: request.id,
model: request.model,
system: request.system,
messages: request.messages,
tools: request.tools,
toolChoice: request.toolChoice,
generation: request.generation,
reasoning: request.reasoning,
cache: request.cache,
responseFormat: request.responseFormat,
metadata: request.metadata,
native: request.native,
})
/**
* Run a model with a typed tool record. The runtime streams the model, on
* each `tool-call` event decodes the input against the tool's `parameters`
@@ -64,7 +79,7 @@ export const run = <T extends Tools>(
const tools = options.tools as Tools
const runtimeTools = toDefinitions(tools)
const initialRequest = new LLMRequest({
...options.request,
...requestInput(options.request),
tools: [
...options.request.tools.filter((tool) => !runtimeTools.some((runtimeTool) => runtimeTool.name === tool.name)),
...runtimeTools,
@@ -92,7 +107,7 @@ export const run = <T extends Tools>(
{ concurrency },
)
const followUp = new LLMRequest({
...request,
...requestInput(request),
messages: [
...request.messages,
LLM.assistant(state.assistantContent),