clanka 0.23.2 → 0.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,175 @@
1
+ /**
2
+ * The `openai-compat` provider: any OpenAI-compatible chat completions
3
+ * endpoint, such as a model proxy, through `@effect/ai-openai-compat`.
4
+ *
5
+ * @since 1.0.0
6
+ */
7
+ import { OpenAiClient, OpenAiLanguageModel } from "@effect/ai-openai-compat"
8
+ import * as Effect from "effect/Effect"
9
+ import * as HttpClient from "effect/http/HttpClient"
10
+ import * as HttpClientResponse from "effect/http/HttpClientResponse"
11
+ import * as Layer from "effect/Layer"
12
+ import * as Option from "effect/Option"
13
+ import * as Schema from "effect/Schema"
14
+ import * as Struct from "effect/Struct"
15
+ import type * as LanguageModel from "effect/ai/LanguageModel"
16
+ import * as Model from "effect/ai/Model"
17
+ import { AgentModelConfig } from "./Agent.ts"
18
+ import * as Compaction from "./Compaction.ts"
19
+ import * as ModelsRepo from "./ModelsRepo.ts"
20
+
21
+ /**
22
+ * The client for an endpoint. Without `apiUrl` it talks to
23
+ * `https://api.openai.com/v1`.
24
+ *
25
+ * @since 1.0.0
26
+ * @category Layers
27
+ */
28
+ export const layerClient = (
29
+ options: OpenAiClient.Options,
30
+ ): Layer.Layer<OpenAiClient.OpenAiClient, never, HttpClient.HttpClient> =>
31
+ OpenAiClient.layer(options)
32
+
33
+ /**
34
+ * Options for a model. The effort of `reasoning` is sent as the chat
35
+ * completions `reasoning_effort`; `reasoning: null` or no effort sends no
36
+ * reasoning options, which keeps the endpoint's default.
37
+ *
38
+ * @since 1.0.0
39
+ * @category Models
40
+ */
41
+ export type ModelOptions = OpenAiLanguageModel.Config["Service"] & {
42
+ readonly reasoning?:
43
+ { readonly effort?: string | undefined } | null | undefined
44
+ } & typeof AgentModelConfig.Service
45
+
46
+ /**
47
+ * @since 1.0.0
48
+ * @category Layers
49
+ */
50
+ export const model = (
51
+ model: string,
52
+ options?: ModelOptions | undefined,
53
+ ): Model.Model<
54
+ "openai-compat",
55
+ LanguageModel.LanguageModel,
56
+ OpenAiClient.OpenAiClient
57
+ > =>
58
+ Model.make(
59
+ "openai-compat",
60
+ model,
61
+ Layer.mergeAll(
62
+ OpenAiLanguageModel.layer({
63
+ model,
64
+ config: {
65
+ // Chat completions endpoints read `reasoning_effort`; the
66
+ // Responses-style `reasoning` object is ignored or rejected.
67
+ ...Struct.omit(options ?? {}, [
68
+ "reasoning",
69
+ ...AgentModelConfig.optionKeys,
70
+ ]),
71
+ ...(options?.reasoning?.effort === undefined
72
+ ? undefined
73
+ : { reasoning_effort: options.reasoning.effort }),
74
+ },
75
+ }),
76
+ AgentModelConfig.layer(
77
+ Struct.pick(options ?? {}, AgentModelConfig.optionKeys),
78
+ ),
79
+ // Cap compaction summaries; sent as chat completions `max_tokens`.
80
+ Layer.succeed(Compaction.SummarizerTransform, (effect) =>
81
+ OpenAiLanguageModel.withConfigOverride(effect, {
82
+ max_output_tokens: Compaction.summarizerMaxOutputTokens,
83
+ }),
84
+ ),
85
+ ),
86
+ )
87
+
88
+ // The fields of a `GET /models` response the catalog reads. Entries are
89
+ // decoded one by one, so a malformed entry drops that model only.
90
+ const ModelsResponse = Schema.Struct({
91
+ data: Schema.Array(Schema.Unknown),
92
+ })
93
+
94
+ const ModelEntry = Schema.Struct({
95
+ id: Schema.String,
96
+ created: Schema.optionalKey(Schema.Finite),
97
+ })
98
+
99
+ const decodeEntry = Schema.decodeUnknownOption(ModelEntry)
100
+
101
+ /**
102
+ * The models the endpoint lists at `GET /models`, newest first, as
103
+ * `openai-compat` catalog entries named by their id. The listing carries no
104
+ * reasoning efforts, so the entries leave `efforts` out. Never fails: logs a warning and serves no models when the
105
+ * endpoint cannot be listed.
106
+ *
107
+ * @since 1.0.0
108
+ * @category Models
109
+ */
110
+ export const models: Effect.Effect<
111
+ ReadonlyArray<ModelsRepo.ModelInfo>,
112
+ never,
113
+ OpenAiClient.OpenAiClient
114
+ > = Effect.gen(function* () {
115
+ const { client } = yield* OpenAiClient.OpenAiClient
116
+ const response = yield* HttpClient.filterStatusOk(client)
117
+ .get("/models")
118
+ .pipe(Effect.flatMap(HttpClientResponse.schemaBodyJson(ModelsResponse)))
119
+ return response.data
120
+ .flatMap((entry) => decodeEntry(entry).pipe(Option.toArray))
121
+ .toSorted(
122
+ (a, b) => (b.created ?? 0) - (a.created ?? 0) || a.id.localeCompare(b.id),
123
+ )
124
+ .map(
125
+ (entry) =>
126
+ new ModelsRepo.ModelInfo({
127
+ provider: "openai-compat",
128
+ id: entry.id,
129
+ name: entry.id,
130
+ }),
131
+ )
132
+ }).pipe(
133
+ Effect.timeout("15 seconds"),
134
+ Effect.catch((error) =>
135
+ Effect.logWarning("Failed to list the openai-compat models", error).pipe(
136
+ Effect.as([]),
137
+ ),
138
+ ),
139
+ )
140
+
141
+ /**
142
+ * The `ModelsRepo` in context followed by the models `client` lists at
143
+ * `GET /models`. The endpoint is listed once, on first use.
144
+ *
145
+ * @since 1.0.0
146
+ * @category Layers
147
+ */
148
+ export const layerModelsRepo = (
149
+ client: OpenAiClient.Options,
150
+ ): Layer.Layer<
151
+ ModelsRepo.ModelsRepo,
152
+ never,
153
+ ModelsRepo.ModelsRepo | HttpClient.HttpClient
154
+ > =>
155
+ Layer.effect(
156
+ ModelsRepo.ModelsRepo,
157
+ Effect.gen(function* () {
158
+ const repo = yield* ModelsRepo.ModelsRepo
159
+ const listed = yield* Effect.cached(
160
+ Effect.provideService(
161
+ models,
162
+ OpenAiClient.OpenAiClient,
163
+ yield* OpenAiClient.OpenAiClient,
164
+ ),
165
+ )
166
+ return ModelsRepo.ModelsRepo.of({
167
+ models: Effect.zipWith(
168
+ repo.models,
169
+ listed,
170
+ (catalog, compat) => [...catalog, ...compat],
171
+ { concurrent: true },
172
+ ),
173
+ })
174
+ }),
175
+ ).pipe(Layer.provide(layerClient(client)))
package/src/cli.ts CHANGED
@@ -16,6 +16,7 @@ import * as McpConfig from "./McpConfig.ts"
16
16
  import * as McpServers from "./McpServers.ts"
17
17
  import * as ModelLayer from "./ModelLayer.ts"
18
18
  import * as ModelsRepo from "./ModelsRepo.ts"
19
+ import * as OpenAiCompat from "./OpenAiCompat.ts"
19
20
  import * as Stream from "effect/Stream"
20
21
  import * as Stdio from "effect/Stdio"
21
22
  import { pipe } from "effect/Function"
@@ -26,6 +27,7 @@ import * as Schema from "effect/Schema"
26
27
  import * as Config from "effect/Config"
27
28
  import * as KeyValueStore from "effect/persistence/KeyValueStore"
28
29
  import * as Option from "effect/Option"
30
+ import type * as Redacted from "effect/Redacted"
29
31
  import { OpenAiClient, OpenAiEmbeddingModel } from "@effect/ai-openai"
30
32
  import * as DeviceCodeHandler from "./DeviceCodeHandler.ts"
31
33
  import packageJson from "../package.json" with { type: "json" }
@@ -68,7 +70,9 @@ type Provider = ModelsRepo.Provider
68
70
  const providers = ModelsRepo.Provider.literals
69
71
 
70
72
  // Pick a model from the catalog, filtering by name as the user types, then
71
- // one of its reasoning efforts.
73
+ // one of its reasoning efforts. A model whose efforts are unknown runs at the
74
+ // provider's default effort. A provider with no models takes a typed
75
+ // <model>[/<effort>].
72
76
  const selectModel = Effect.fnUntraced(function* (provider: Provider) {
73
77
  const repo = yield* ModelsRepo.ModelsRepo
74
78
  const catalog = (yield* repo.models).filter(
@@ -78,6 +82,11 @@ const selectModel = Effect.fnUntraced(function* (provider: Provider) {
78
82
  catalog.length > 0
79
83
  ? catalog
80
84
  : ModelsRepo.fallback.filter((model) => model.provider === provider)
85
+ if (models.length === 0) {
86
+ return ModelLayer.parseModel(
87
+ yield* Prompt.String({ message: "Enter a model id" }),
88
+ )
89
+ }
81
90
  const defaultId = ModelsRepo.fallback.find(
82
91
  (model) => model.provider === provider,
83
92
  )?.id
@@ -90,6 +99,9 @@ const selectModel = Effect.fnUntraced(function* (provider: Provider) {
90
99
  selected: model.id === defaultId,
91
100
  })),
92
101
  })
102
+ if (model.efforts === undefined) {
103
+ return { model: model.id, effort: undefined }
104
+ }
93
105
  if (model.efforts.length === 0) {
94
106
  return { model: model.id, effort: null }
95
107
  }
@@ -104,30 +116,12 @@ const selectModel = Effect.fnUntraced(function* (provider: Provider) {
104
116
  return { model: model.id, effort }
105
117
  })
106
118
 
107
- const isProvider = Schema.is(ModelsRepo.Provider)
108
-
109
- const isThoughtLevel = Schema.is(Acp.ThoughtLevel)
110
-
111
- // `<provider>/<model>[/<effort>]` as accepted by the `acp` --model flag.
112
- const parseModelId = (modelId: string) => {
113
- const [provider, model, effort, ...rest] = modelId.split("/")
114
- return isProvider(provider) &&
115
- model !== undefined &&
116
- (effort === undefined || isThoughtLevel(effort)) &&
117
- rest.length === 0
118
- ? Option.some({
119
- model: `${provider}:${model}`,
120
- thoughtLevel: Option.fromUndefinedOr(effort),
121
- })
122
- : Option.none()
123
- }
124
-
125
119
  const ModelId = Schema.String.pipe(
126
120
  Schema.check(
127
121
  Schema.makeFilter(
128
122
  (modelId: string) =>
129
- Option.isSome(parseModelId(modelId)) ||
130
- `Invalid model "${modelId}", expected <provider>/<model>[/<effort>] with effort one of ${Acp.ThoughtLevel.literals.join(", ")}`,
123
+ Option.isSome(ModelLayer.parseModelSpec(modelId)) ||
124
+ `Invalid model "${modelId}", expected <provider>/<model>[/<effort>] with provider one of ${providers.join(", ")}`,
131
125
  ),
132
126
  ),
133
127
  )
@@ -145,14 +139,9 @@ const auxiliaryModel = (options: {
145
139
  ),
146
140
  Flag.withFallbackConfig(Config.String(options.env)),
147
141
  Flag.withSchema(ModelId),
148
- Flag.map((modelId): ModelLayer.ModelSpec => {
149
- const [provider, model, effort] = modelId.split("/") as [
150
- Provider,
151
- string,
152
- string | undefined,
153
- ]
154
- return { provider, model, effort }
155
- }),
142
+ Flag.map((modelId) =>
143
+ Option.getOrThrow(ModelLayer.parseModelSpec(modelId)),
144
+ ),
156
145
  Flag.optional,
157
146
  )
158
147
 
@@ -200,6 +189,10 @@ const provider = Flag.Literals("provider", providers).pipe(
200
189
  title: "claude",
201
190
  value: "claude",
202
191
  },
192
+ {
193
+ title: "openai-compat",
194
+ value: "openai-compat",
195
+ },
203
196
  ],
204
197
  }),
205
198
  ),
@@ -208,19 +201,36 @@ const provider = Flag.Literals("provider", providers).pipe(
208
201
  const model = Flag.String("model").pipe(Flag.withAlias("m"), Flag.optional)
209
202
 
210
203
  const apiKey = Flag.Redacted("api-key").pipe(
211
- Flag.withDescription("OpenAI API key (defaults to CLANKA_OPENAI_API_KEY)"),
204
+ Flag.withDescription(
205
+ "API key for the openai API-key mode and the openai-compat provider (defaults to CLANKA_OPENAI_API_KEY)",
206
+ ),
212
207
  Flag.withFallbackConfig(Config.Redacted("CLANKA_OPENAI_API_KEY")),
213
208
  Flag.optional,
214
209
  )
215
210
 
216
211
  const apiUrl = Flag.String("api-url").pipe(
217
212
  Flag.withDescription(
218
- "OpenAI API base URL for API-key mode (defaults to CLANKA_OPENAI_API_URL or https://api.openai.com/v1)",
213
+ "API base URL for the openai API-key mode and the openai-compat provider (defaults to CLANKA_OPENAI_API_URL or https://api.openai.com/v1). When set, the models it lists at /models are offered as openai-compat models.",
219
214
  ),
220
215
  Flag.withFallbackConfig(Config.String("CLANKA_OPENAI_API_URL")),
221
216
  Flag.optional,
222
217
  )
223
218
 
219
+ // Adds the models an --api-url endpoint lists to the catalog, as openai-compat
220
+ // models. Without --api-url the endpoint is OpenAI's, which is not listed.
221
+ const openAiCompatModels = (flags: {
222
+ readonly apiKey: Option.Option<Redacted.Redacted>
223
+ readonly apiUrl: Option.Option<string>
224
+ }) =>
225
+ Option.match(flags.apiUrl, {
226
+ onNone: () => Layer.empty,
227
+ onSome: (apiUrl) =>
228
+ OpenAiCompat.layerModelsRepo({
229
+ apiUrl,
230
+ apiKey: Option.getOrUndefined(flags.apiKey),
231
+ }),
232
+ })
233
+
224
234
  // OpenAI's `priority` service tier.
225
235
  const fast = Flag.Boolean("fast").pipe(
226
236
  Flag.withDescription(
@@ -388,7 +398,13 @@ const acpModel = Flag.String("model").pipe(
388
398
  ),
389
399
  Flag.withSchema(ModelId),
390
400
  Flag.withDefault("openai/gpt-6-astra"),
391
- Flag.map((modelId) => Option.getOrThrow(parseModelId(modelId))),
401
+ Flag.map((modelId) => {
402
+ const spec = Option.getOrThrow(ModelLayer.parseModelSpec(modelId))
403
+ return {
404
+ model: `${spec.provider}:${spec.model}`,
405
+ thoughtLevel: Option.fromUndefinedOr(spec.effort),
406
+ }
407
+ }),
392
408
  )
393
409
 
394
410
  // kiro launchers pass it to every `acp` run; clanka has no tool permissions to
@@ -465,6 +481,7 @@ const acp = Command.make("acp", {
465
481
  Layer.succeed(Logger.LogToStderr, true),
466
482
  DeviceCodeHandler.layerLog,
467
483
  compactionConfig(flags),
484
+ openAiCompatModels(flags),
468
485
  ),
469
486
  ),
470
487
  )
@@ -508,13 +525,7 @@ Command.make("clanka", {
508
525
  () => import("./OutputFormatter.ts"),
509
526
  )
510
527
  const { model, effort } = yield* Option.match(modelRaw, {
511
- onSome: (selected) => {
512
- const [model, effort] = selected.split("/") as [
513
- string,
514
- string | undefined,
515
- ]
516
- return Effect.succeed({ model, effort })
517
- },
528
+ onSome: (selected) => Effect.succeed(ModelLayer.parseModel(selected)),
518
529
  onNone: () => selectModel(provider),
519
530
  })
520
531
  const Model = ModelLayer.make(
@@ -583,6 +594,7 @@ Command.make("clanka", {
583
594
  Layer.mergeAll(
584
595
  Agent.ConversationMode.layer(Option.isNone(prompt)),
585
596
  compactionConfig(flags),
597
+ openAiCompatModels(flags),
586
598
  ),
587
599
  ),
588
600
  Command.withSubcommands([acp]),