clanka 0.23.1 → 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.
Files changed (45) hide show
  1. package/README.md +24 -1
  2. package/dist/Acp.test.js +5 -4
  3. package/dist/Acp.test.js.map +1 -1
  4. package/dist/Agent.js +2 -2
  5. package/dist/Agent.js.map +1 -1
  6. package/dist/AgentBashJobs.test.js +5 -4
  7. package/dist/AgentBashJobs.test.js.map +1 -1
  8. package/dist/AgentExecutor.test.js +1 -1
  9. package/dist/AgentExecutor.test.js.map +1 -1
  10. package/dist/AgentSubagents.test.js +19 -16
  11. package/dist/AgentSubagents.test.js.map +1 -1
  12. package/dist/ModelLayer.d.ts +31 -6
  13. package/dist/ModelLayer.d.ts.map +1 -1
  14. package/dist/ModelLayer.js +49 -7
  15. package/dist/ModelLayer.js.map +1 -1
  16. package/dist/ModelLayer.test.js +39 -0
  17. package/dist/ModelLayer.test.js.map +1 -1
  18. package/dist/ModelsRepo.d.ts +7 -5
  19. package/dist/ModelsRepo.d.ts.map +1 -1
  20. package/dist/ModelsRepo.js +12 -4
  21. package/dist/ModelsRepo.js.map +1 -1
  22. package/dist/OpenAiCompat.d.ts +59 -0
  23. package/dist/OpenAiCompat.d.ts.map +1 -0
  24. package/dist/OpenAiCompat.js +96 -0
  25. package/dist/OpenAiCompat.js.map +1 -0
  26. package/dist/OpenAiCompat.test.d.ts +2 -0
  27. package/dist/OpenAiCompat.test.d.ts.map +1 -0
  28. package/dist/OpenAiCompat.test.js +110 -0
  29. package/dist/OpenAiCompat.test.js.map +1 -0
  30. package/dist/bin/cli.mjs +108 -51
  31. package/dist/bin/cli.mjs.map +1 -1
  32. package/dist/cli.js +38 -31
  33. package/dist/cli.js.map +1 -1
  34. package/package.json +1 -1
  35. package/src/Acp.test.ts +8 -4
  36. package/src/Agent.ts +2 -2
  37. package/src/AgentBashJobs.test.ts +16 -5
  38. package/src/AgentExecutor.test.ts +1 -1
  39. package/src/AgentSubagents.test.ts +25 -23
  40. package/src/ModelLayer.test.ts +62 -0
  41. package/src/ModelLayer.ts +60 -7
  42. package/src/ModelsRepo.ts +12 -4
  43. package/src/OpenAiCompat.test.ts +166 -0
  44. package/src/OpenAiCompat.ts +175 -0
  45. package/src/cli.ts +51 -39
package/src/ModelLayer.ts CHANGED
@@ -16,6 +16,7 @@ import * as Codex from "./Codex.ts"
16
16
  import * as Compaction from "./Compaction.ts"
17
17
  import * as Copilot from "./Copilot.ts"
18
18
  import * as ModelsRepo from "./ModelsRepo.ts"
19
+ import * as OpenAiCompat from "./OpenAiCompat.ts"
19
20
  import * as Xai from "./Xai.ts"
20
21
 
21
22
  /**
@@ -87,13 +88,23 @@ const languageModel = (
87
88
  return Copilot.model(model, options).pipe(
88
89
  Layer.provide(Copilot.layerClient),
89
90
  )
91
+ case "openai-compat":
92
+ return OpenAiCompat.model(model, options).pipe(
93
+ Layer.provide(
94
+ OpenAiCompat.layerClient({
95
+ apiKey: openai.apiKey,
96
+ apiUrl: openai.apiUrl,
97
+ }),
98
+ ),
99
+ )
90
100
  }
91
101
  }
92
102
 
93
103
  /**
94
104
  * The model layer for a provider and model id. `effort` is `null` for models
95
105
  * that take no reasoning effort, and `undefined` to keep the provider's
96
- * default effort. `fastMode` requests the `priority` service tier and is
106
+ * default effort. `openai` is the client for the `openai` API-key mode and
107
+ * for the `openai-compat` provider. `fastMode` requests the `priority` service tier and is
97
108
  * ignored for providers without fast mode; it also applies to the
98
109
  * `auxiliary` models.
99
110
  *
@@ -132,18 +143,59 @@ export const make = (
132
143
  const isProvider = Schema.is(ModelsRepo.Provider)
133
144
 
134
145
  /**
135
- * Parses a `<provider>:<model>` id as advertised over ACP.
146
+ * Parses a `<provider>:<model>` id as advertised over ACP. The model id is
147
+ * everything after the first colon, so it may hold colons itself.
136
148
  *
137
149
  * @since 1.0.0
138
150
  * @category Parsing
139
151
  */
140
152
  export const parseAcpModelId = (modelId: string) => {
141
- const [provider, model, ...rest] = modelId.split(":")
142
- return isProvider(provider) && model !== undefined && rest.length === 0
153
+ const index = modelId.indexOf(":")
154
+ const provider = modelId.slice(0, index)
155
+ const model = modelId.slice(index + 1)
156
+ return index !== -1 && isProvider(provider) && model !== ""
143
157
  ? Option.some({ provider, model })
144
158
  : Option.none()
145
159
  }
146
160
 
161
+ const isThoughtLevel = Schema.is(ThoughtLevel)
162
+
163
+ /**
164
+ * Parses `<model>[/<effort>]`. The last segment is the effort only when it
165
+ * is a `ThoughtLevel`, so model ids may hold slashes, as in
166
+ * `moonshotai/Kimi-K2-Instruct`.
167
+ *
168
+ * @since 1.0.0
169
+ * @category Parsing
170
+ */
171
+ export const parseModel = (
172
+ value: string,
173
+ ): { readonly model: string; readonly effort: ThoughtLevel | undefined } => {
174
+ const index = value.lastIndexOf("/")
175
+ const effort = value.slice(index + 1)
176
+ return index > 0 && isThoughtLevel(effort)
177
+ ? { model: value.slice(0, index), effort }
178
+ : { model: value, effort: undefined }
179
+ }
180
+
181
+ /**
182
+ * Parses `<provider>/<model>[/<effort>]`, as the CLI model flags take it.
183
+ * See `parseModel` for the model and effort.
184
+ *
185
+ * @since 1.0.0
186
+ * @category Parsing
187
+ */
188
+ export const parseModelSpec = (
189
+ value: string,
190
+ ): Option.Option<ModelSpec & { readonly effort: ThoughtLevel | undefined }> => {
191
+ const index = value.indexOf("/")
192
+ const provider = value.slice(0, index)
193
+ const { model, effort } = parseModel(value.slice(index + 1))
194
+ return index !== -1 && isProvider(provider) && model !== ""
195
+ ? Option.some({ provider, model, effort })
196
+ : Option.none()
197
+ }
198
+
147
199
  /**
148
200
  * An `Acp.Options.supportsFastMode` callback for `<provider>:<model>` ids.
149
201
  *
@@ -194,11 +246,12 @@ const resolveEffort = (
194
246
 
195
247
  /**
196
248
  * An `Acp.Options.makeModel` callback. Ids that do not parse as
197
- * `<provider>:<model>` are unknown models. A model the catalog lists without
249
+ * `<provider>:<model>` are unknown models. A model the catalog lists with no
198
250
  * efforts runs with no reasoning options, whatever the session thought level.
199
251
  * A listed model that does not support the session thought level runs at the
200
252
  * supported effort nearest to it in `ThoughtLevel` order, the higher one on a
201
- * tie. Unlisted models run at the session thought level. Fast mode applies
253
+ * tie. Unlisted models, and listed models whose efforts are unknown, run at
254
+ * the session thought level. Fast mode applies
202
255
  * to OpenAI models only. Every session uses the same `auxiliary` models.
203
256
  *
204
257
  * @since 1.0.0
@@ -220,7 +273,7 @@ export const makeAcp =
220
273
  return make(
221
274
  provider,
222
275
  model,
223
- info === undefined
276
+ info?.efforts === undefined
224
277
  ? thoughtLevel
225
278
  : resolveEffort(info.efforts, thoughtLevel),
226
279
  options.openai,
package/src/ModelsRepo.ts CHANGED
@@ -23,7 +23,13 @@ import { ThoughtLevel } from "./Acp.ts"
23
23
  * @since 1.0.0
24
24
  * @category Models
25
25
  */
26
- export const Provider = Schema.Literals(["openai", "copilot", "xai", "claude"])
26
+ export const Provider = Schema.Literals([
27
+ "openai",
28
+ "copilot",
29
+ "xai",
30
+ "claude",
31
+ "openai-compat",
32
+ ])
27
33
 
28
34
  /**
29
35
  * @since 1.0.0
@@ -45,14 +51,16 @@ export class ModelInfo extends Schema.Class<ModelInfo>(
45
51
  id: Schema.String,
46
52
  name: Schema.String,
47
53
  /**
48
- * Reasoning efforts the model accepts; empty when it takes none.
54
+ * Reasoning efforts the model accepts; empty when it takes none, and
55
+ * absent when its source does not say.
49
56
  */
50
- efforts: Schema.Array(ThoughtLevel),
57
+ efforts: Schema.optionalKey(Schema.Array(ThoughtLevel)),
51
58
  }) {}
52
59
 
53
60
  /**
54
61
  * The built-in catalog, served when neither models.dev nor a cached catalog
55
- * is available.
62
+ * is available. It has no `openai-compat` model, since that provider's
63
+ * models depend on the endpoint.
56
64
  *
57
65
  * @since 1.0.0
58
66
  * @category Models
@@ -0,0 +1,166 @@
1
+ import { assert, describe, it } from "@effect/vitest"
2
+ import * as Effect from "effect/Effect"
3
+ import * as Layer from "effect/Layer"
4
+ import * as Redacted from "effect/Redacted"
5
+ import * as Stream from "effect/Stream"
6
+ import * as LanguageModel from "effect/ai/LanguageModel"
7
+ import * as HttpClient from "effect/http/HttpClient"
8
+ import type * as HttpClientRequest from "effect/http/HttpClientRequest"
9
+ import * as HttpClientResponse from "effect/http/HttpClientResponse"
10
+ import * as Compaction from "./Compaction.ts"
11
+ import * as ModelsRepo from "./ModelsRepo.ts"
12
+ import * as OpenAiCompat from "./OpenAiCompat.ts"
13
+
14
+ const client = {
15
+ apiUrl: "https://proxy.test/v1",
16
+ apiKey: Redacted.make("proxy-key"),
17
+ }
18
+
19
+ // An HttpClient that records its requests and answers each with `response`.
20
+ const recording = (response: () => Response) => {
21
+ const requests: Array<HttpClientRequest.HttpClientRequest> = []
22
+ const layer = Layer.succeed(
23
+ HttpClient.HttpClient,
24
+ HttpClient.make((request) =>
25
+ Effect.sync(() => {
26
+ requests.push(request)
27
+ return HttpClientResponse.fromWeb(request, response())
28
+ }),
29
+ ),
30
+ )
31
+ return { requests, layer }
32
+ }
33
+
34
+ const jsonBody = (request: HttpClientRequest.HttpClientRequest) => {
35
+ if (request.body._tag !== "Uint8Array") throw new Error("Expected JSON body")
36
+ return JSON.parse(new TextDecoder().decode(request.body.body)) as Record<
37
+ string,
38
+ unknown
39
+ >
40
+ }
41
+
42
+ // The requests a prompt sends, then the same prompt as a compaction summary.
43
+ const send = Effect.fnUntraced(function* (options?: OpenAiCompat.ModelOptions) {
44
+ const http = recording(() => new Response(null, { status: 400 }))
45
+ yield* Effect.gen(function* () {
46
+ const ai = yield* LanguageModel.LanguageModel
47
+ const summarize = yield* Compaction.SummarizerTransform
48
+ const request = ai.streamText({ prompt: "hello" }).pipe(Stream.runDrain)
49
+ yield* request.pipe(Effect.exit)
50
+ yield* summarize(request).pipe(Effect.exit)
51
+ }).pipe(
52
+ Effect.provide(
53
+ OpenAiCompat.model("kimi-k3", options).pipe(
54
+ Layer.provide(OpenAiCompat.layerClient(client)),
55
+ Layer.provide(http.layer),
56
+ ),
57
+ ),
58
+ )
59
+ assert.lengthOf(http.requests, 2)
60
+ return http.requests
61
+ })
62
+
63
+ describe("OpenAiCompat.model", () => {
64
+ it.effect("streams chat completions from the configured endpoint", () =>
65
+ Effect.gen(function* () {
66
+ const [request] = yield* send()
67
+ assert.strictEqual(request!.url, "https://proxy.test/v1/chat/completions")
68
+ assert.strictEqual(request!.headers.authorization, "Bearer proxy-key")
69
+ assert.strictEqual(jsonBody(request!).model, "kimi-k3")
70
+ }),
71
+ )
72
+
73
+ it.effect("sends the effort as reasoning_effort, and none when null", () =>
74
+ Effect.gen(function* () {
75
+ const [high] = yield* send({ reasoning: { effort: "high" } })
76
+ assert.strictEqual(jsonBody(high!).reasoning_effort, "high")
77
+ assert.isFalse("reasoning" in jsonBody(high!))
78
+ for (const options of [{ reasoning: null }, undefined]) {
79
+ const [request] = yield* send(options)
80
+ assert.isFalse("reasoning_effort" in jsonBody(request!))
81
+ assert.isFalse("reasoning" in jsonBody(request!))
82
+ }
83
+ }),
84
+ )
85
+
86
+ it.effect("caps compaction summaries with max_tokens", () =>
87
+ Effect.gen(function* () {
88
+ const [prompt, summary] = yield* send()
89
+ assert.isFalse("max_tokens" in jsonBody(prompt!))
90
+ assert.strictEqual(
91
+ jsonBody(summary!).max_tokens,
92
+ Compaction.summarizerMaxOutputTokens,
93
+ )
94
+ }),
95
+ )
96
+ })
97
+
98
+ const base = new ModelsRepo.ModelInfo({
99
+ provider: "openai",
100
+ id: "gpt-6-astra",
101
+ name: "GPT-6 Astra",
102
+ efforts: ["low", "high"],
103
+ })
104
+
105
+ // The catalog of a repo over `base` and an endpoint answering `response`.
106
+ const catalog = (response: () => Response) => {
107
+ const http = recording(response)
108
+ return Effect.gen(function* () {
109
+ const repo = yield* ModelsRepo.ModelsRepo
110
+ const first = yield* repo.models
111
+ assert.deepStrictEqual(yield* repo.models, first)
112
+ return { models: first, requests: http.requests }
113
+ }).pipe(
114
+ Effect.provide(
115
+ OpenAiCompat.layerModelsRepo(client).pipe(
116
+ Layer.provide([ModelsRepo.ModelsRepo.layer([base]), http.layer]),
117
+ ),
118
+ ),
119
+ )
120
+ }
121
+
122
+ describe("OpenAiCompat.layerModelsRepo", () => {
123
+ it.effect(
124
+ "appends the listed models, newest first, with unknown efforts",
125
+ () =>
126
+ Effect.gen(function* () {
127
+ const { models, requests } = yield* catalog(() =>
128
+ Response.json({
129
+ object: "list",
130
+ data: [
131
+ { id: "kimi-k2", object: "model", created: 1 },
132
+ { id: "kimi-k3", object: "model", created: 3 },
133
+ { object: "model" },
134
+ { id: "custom", object: "model" },
135
+ ],
136
+ }),
137
+ )
138
+ assert.lengthOf(requests, 1)
139
+ assert.strictEqual(requests[0]!.url, "https://proxy.test/v1/models")
140
+ assert.strictEqual(
141
+ requests[0]!.headers.authorization,
142
+ "Bearer proxy-key",
143
+ )
144
+ assert.deepStrictEqual(models, [
145
+ base,
146
+ ...["kimi-k3", "kimi-k2", "custom"].map(
147
+ (id) =>
148
+ new ModelsRepo.ModelInfo({
149
+ provider: "openai-compat",
150
+ id,
151
+ name: id,
152
+ }),
153
+ ),
154
+ ])
155
+ }),
156
+ )
157
+
158
+ it.effect("keeps the catalog when the endpoint cannot be listed", () =>
159
+ Effect.gen(function* () {
160
+ const { models } = yield* catalog(
161
+ () => new Response(null, { status: 401 }),
162
+ )
163
+ assert.deepStrictEqual(models, [base])
164
+ }),
165
+ )
166
+ })
@@ -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]),