clanka 0.12.3 → 0.13.1
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.
- package/dist/Acp.d.ts +9 -0
- package/dist/Acp.d.ts.map +1 -1
- package/dist/Acp.js +21 -10
- package/dist/Acp.js.map +1 -1
- package/dist/Acp.test.js +17 -2
- package/dist/Acp.test.js.map +1 -1
- package/dist/Codex.d.ts +12 -2
- package/dist/Codex.d.ts.map +1 -1
- package/dist/Codex.js +7 -5
- package/dist/Codex.js.map +1 -1
- package/dist/Codex.test.d.ts +2 -0
- package/dist/Codex.test.d.ts.map +1 -0
- package/dist/Codex.test.js +37 -0
- package/dist/Codex.test.js.map +1 -0
- package/dist/Copilot.d.ts.map +1 -1
- package/dist/Copilot.js +4 -4
- package/dist/Copilot.js.map +1 -1
- package/dist/Copilot.test.d.ts +2 -0
- package/dist/Copilot.test.d.ts.map +1 -0
- package/dist/Copilot.test.js +32 -0
- package/dist/Copilot.test.js.map +1 -0
- package/dist/ModelLayer.d.ts +64 -0
- package/dist/ModelLayer.d.ts.map +1 -0
- package/dist/ModelLayer.js +96 -0
- package/dist/ModelLayer.js.map +1 -0
- package/dist/ModelLayer.test.d.ts +2 -0
- package/dist/ModelLayer.test.d.ts.map +1 -0
- package/dist/ModelLayer.test.js +83 -0
- package/dist/ModelLayer.test.js.map +1 -0
- package/dist/ModelsRepo.d.ts +80 -0
- package/dist/ModelsRepo.d.ts.map +1 -0
- package/dist/ModelsRepo.js +162 -0
- package/dist/ModelsRepo.js.map +1 -0
- package/dist/ModelsRepo.test.d.ts +2 -0
- package/dist/ModelsRepo.test.d.ts.map +1 -0
- package/dist/ModelsRepo.test.js +168 -0
- package/dist/ModelsRepo.test.js.map +1 -0
- package/dist/Xai.d.ts +12 -2
- package/dist/Xai.d.ts.map +1 -1
- package/dist/Xai.js +8 -2
- package/dist/Xai.js.map +1 -1
- package/dist/Xai.test.js +38 -7
- package/dist/Xai.test.js.map +1 -1
- package/dist/bin/{Effect-DuThyCJ1.mjs → Effect-4B1lzxy4.mjs} +2 -2
- package/dist/bin/{Effect-DuThyCJ1.mjs.map → Effect-4B1lzxy4.mjs.map} +1 -1
- package/dist/bin/{McpClient-CIiPyMDJ.mjs → McpClient-DMfHHEx3.mjs} +3 -3
- package/dist/bin/{McpClient-CIiPyMDJ.mjs.map → McpClient-DMfHHEx3.mjs.map} +1 -1
- package/dist/bin/{OutputFormatter-BvIjvUmT.mjs → OutputFormatter-DRycCEdr.mjs} +3 -3
- package/dist/bin/{OutputFormatter-BvIjvUmT.mjs.map → OutputFormatter-DRycCEdr.mjs.map} +1 -1
- package/dist/bin/{Schema-DcXUXpRg.mjs → Schema-D4fxUgEU.mjs} +3 -3
- package/dist/bin/{Schema-DcXUXpRg.mjs.map → Schema-D4fxUgEU.mjs.map} +1 -1
- package/dist/bin/{SemanticSearch-CdGA4CIC.mjs → SemanticSearch-BcLFv9qz.mjs} +4 -4
- package/dist/bin/{SemanticSearch-CdGA4CIC.mjs.map → SemanticSearch-BcLFv9qz.mjs.map} +1 -1
- package/dist/bin/{Stream-I9SRMlLj.mjs → Stream-i37nF1ep.mjs} +2 -2
- package/dist/bin/{Stream-I9SRMlLj.mjs.map → Stream-i37nF1ep.mjs.map} +1 -1
- package/dist/bin/cli.mjs +757 -420
- package/dist/bin/cli.mjs.map +1 -1
- package/dist/cli.js +59 -46
- package/dist/cli.js.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/Acp.test.ts +32 -1
- package/src/Acp.ts +32 -12
- package/src/Codex.test.ts +60 -0
- package/src/Codex.ts +26 -14
- package/src/Copilot.test.ts +55 -0
- package/src/Copilot.ts +7 -4
- package/src/ModelLayer.test.ts +122 -0
- package/src/ModelLayer.ts +149 -0
- package/src/ModelsRepo.test.ts +206 -0
- package/src/ModelsRepo.ts +260 -0
- package/src/Xai.test.ts +62 -7
- package/src/Xai.ts +27 -8
- package/src/cli.ts +74 -81
- package/src/index.ts +5 -0
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The catalog of chat models clanka can run, and a models.dev implementation.
|
|
3
|
+
*
|
|
4
|
+
* See `.specs/models-repo.md`.
|
|
5
|
+
*
|
|
6
|
+
* @since 1.0.0
|
|
7
|
+
*/
|
|
8
|
+
import * as Clock from "effect/Clock"
|
|
9
|
+
import * as Context from "effect/Context"
|
|
10
|
+
import * as Duration from "effect/Duration"
|
|
11
|
+
import * as Effect from "effect/Effect"
|
|
12
|
+
import * as HttpClient from "effect/http/HttpClient"
|
|
13
|
+
import * as HttpClientResponse from "effect/http/HttpClientResponse"
|
|
14
|
+
import * as Layer from "effect/Layer"
|
|
15
|
+
import * as Option from "effect/Option"
|
|
16
|
+
import * as KeyValueStore from "effect/persistence/KeyValueStore"
|
|
17
|
+
import * as Schema from "effect/Schema"
|
|
18
|
+
import { ThoughtLevel } from "./Acp.ts"
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* A clanka provider, as `--provider` takes it.
|
|
22
|
+
*
|
|
23
|
+
* @since 1.0.0
|
|
24
|
+
* @category Models
|
|
25
|
+
*/
|
|
26
|
+
export const Provider = Schema.Literals(["openai", "copilot", "xai"])
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @since 1.0.0
|
|
30
|
+
* @category Models
|
|
31
|
+
*/
|
|
32
|
+
export type Provider = typeof Provider.Type
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @since 1.0.0
|
|
36
|
+
* @category Models
|
|
37
|
+
*/
|
|
38
|
+
export class ModelInfo extends Schema.Class<ModelInfo>(
|
|
39
|
+
"clanka/ModelsRepo/ModelInfo",
|
|
40
|
+
)({
|
|
41
|
+
provider: Provider,
|
|
42
|
+
/**
|
|
43
|
+
* The model id as the provider API expects it, e.g. `gpt-6-astra`.
|
|
44
|
+
*/
|
|
45
|
+
id: Schema.String,
|
|
46
|
+
name: Schema.String,
|
|
47
|
+
/**
|
|
48
|
+
* Reasoning efforts the model accepts; empty when it takes none.
|
|
49
|
+
*/
|
|
50
|
+
efforts: Schema.Array(ThoughtLevel),
|
|
51
|
+
}) {}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The built-in catalog, served when neither models.dev nor a cached catalog
|
|
55
|
+
* is available.
|
|
56
|
+
*
|
|
57
|
+
* @since 1.0.0
|
|
58
|
+
* @category Models
|
|
59
|
+
*/
|
|
60
|
+
export const fallback: ReadonlyArray<ModelInfo> = [
|
|
61
|
+
new ModelInfo({
|
|
62
|
+
provider: "openai",
|
|
63
|
+
id: "gpt-6-astra",
|
|
64
|
+
name: "GPT-6 Astra",
|
|
65
|
+
efforts: ["low", "medium", "high", "xhigh", "max"],
|
|
66
|
+
}),
|
|
67
|
+
new ModelInfo({
|
|
68
|
+
provider: "copilot",
|
|
69
|
+
id: "gpt-6-astra",
|
|
70
|
+
name: "GPT-6 Astra",
|
|
71
|
+
efforts: ["low", "medium", "high", "xhigh", "max"],
|
|
72
|
+
}),
|
|
73
|
+
new ModelInfo({
|
|
74
|
+
provider: "xai",
|
|
75
|
+
id: "grok-4.6",
|
|
76
|
+
name: "Grok 4.6",
|
|
77
|
+
efforts: ["low", "medium", "high", "xhigh"],
|
|
78
|
+
}),
|
|
79
|
+
]
|
|
80
|
+
|
|
81
|
+
// The fields of a models.dev model entry the catalog reads. Entries are
|
|
82
|
+
// decoded one by one, so a malformed entry drops that model only.
|
|
83
|
+
const ModelsDevModel = Schema.Struct({
|
|
84
|
+
id: Schema.String,
|
|
85
|
+
name: Schema.String,
|
|
86
|
+
type: Schema.optionalKey(Schema.String),
|
|
87
|
+
status: Schema.optionalKey(Schema.String),
|
|
88
|
+
tool_call: Schema.optionalKey(Schema.Boolean),
|
|
89
|
+
reasoning_options: Schema.optionalKey(
|
|
90
|
+
Schema.Array(
|
|
91
|
+
Schema.Struct({
|
|
92
|
+
type: Schema.String,
|
|
93
|
+
values: Schema.optionalKey(Schema.Array(Schema.Unknown)),
|
|
94
|
+
}),
|
|
95
|
+
),
|
|
96
|
+
),
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
const ModelsDevProvider = Schema.Struct({
|
|
100
|
+
models: Schema.Record(Schema.String, Schema.Unknown),
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
// models.dev provider id to clanka provider, in catalog order.
|
|
104
|
+
const providerIds: ReadonlyArray<readonly [string, Provider]> = [
|
|
105
|
+
["openai", "openai"],
|
|
106
|
+
["github-copilot", "copilot"],
|
|
107
|
+
["xai", "xai"],
|
|
108
|
+
]
|
|
109
|
+
|
|
110
|
+
const ModelsDevApi = Schema.Struct(
|
|
111
|
+
Object.fromEntries(
|
|
112
|
+
providerIds.map(([id]) => [id, Schema.optionalKey(ModelsDevProvider)]),
|
|
113
|
+
),
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
const decodeModel = Schema.decodeUnknownOption(ModelsDevModel)
|
|
117
|
+
const isThoughtLevel = Schema.is(ThoughtLevel)
|
|
118
|
+
|
|
119
|
+
const toCatalog = (api: typeof ModelsDevApi.Type): ReadonlyArray<ModelInfo> =>
|
|
120
|
+
providerIds.flatMap(([id, provider]) =>
|
|
121
|
+
Object.values(api[id]?.models ?? {}).flatMap((entry) => {
|
|
122
|
+
const model = Option.getOrUndefined(decodeModel(entry))
|
|
123
|
+
if (
|
|
124
|
+
model === undefined ||
|
|
125
|
+
model.type !== undefined ||
|
|
126
|
+
model.status === "deprecated" ||
|
|
127
|
+
model.tool_call !== true
|
|
128
|
+
) {
|
|
129
|
+
return []
|
|
130
|
+
}
|
|
131
|
+
const effort = model.reasoning_options?.find(
|
|
132
|
+
(option) => option.type === "effort",
|
|
133
|
+
)
|
|
134
|
+
return [
|
|
135
|
+
new ModelInfo({
|
|
136
|
+
provider,
|
|
137
|
+
id: model.id,
|
|
138
|
+
name: model.name,
|
|
139
|
+
efforts: (effort?.values ?? []).filter(isThoughtLevel),
|
|
140
|
+
}),
|
|
141
|
+
]
|
|
142
|
+
}),
|
|
143
|
+
)
|
|
144
|
+
|
|
145
|
+
class CachedCatalog extends Schema.Class<CachedCatalog>(
|
|
146
|
+
"clanka/ModelsRepo/CachedCatalog",
|
|
147
|
+
)({
|
|
148
|
+
fetchedAt: Schema.Finite,
|
|
149
|
+
models: Schema.Array(ModelInfo),
|
|
150
|
+
}) {}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* @since 1.0.0
|
|
154
|
+
* @category Services
|
|
155
|
+
*/
|
|
156
|
+
export class ModelsRepo extends Context.Service<
|
|
157
|
+
ModelsRepo,
|
|
158
|
+
{
|
|
159
|
+
/**
|
|
160
|
+
* Every chat model clanka can run, in catalog order. Never fails: an
|
|
161
|
+
* implementation that cannot reach its source serves what it has.
|
|
162
|
+
*/
|
|
163
|
+
readonly models: Effect.Effect<ReadonlyArray<ModelInfo>>
|
|
164
|
+
}
|
|
165
|
+
>()("clanka/ModelsRepo") {
|
|
166
|
+
/**
|
|
167
|
+
* A fixed catalog.
|
|
168
|
+
*
|
|
169
|
+
* @since 1.0.0
|
|
170
|
+
* @category Layers
|
|
171
|
+
*/
|
|
172
|
+
static readonly layer = (
|
|
173
|
+
models: ReadonlyArray<ModelInfo>,
|
|
174
|
+
): Layer.Layer<ModelsRepo> =>
|
|
175
|
+
Layer.succeed(ModelsRepo, ModelsRepo.of({ models: Effect.succeed(models) }))
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* The catalog built from `https://models.dev/api.json`: the `openai`,
|
|
179
|
+
* `github-copilot` and `xai` text models that are not deprecated and accept
|
|
180
|
+
* tool calls, cached in the `KeyValueStore` for `ttl` (default 24 hours).
|
|
181
|
+
* Serves the cached catalog when the fetch fails, then `fallback`.
|
|
182
|
+
*
|
|
183
|
+
* @since 1.0.0
|
|
184
|
+
* @category Layers
|
|
185
|
+
*/
|
|
186
|
+
static readonly layerModelsDev = (
|
|
187
|
+
options?:
|
|
188
|
+
| {
|
|
189
|
+
readonly url?: string | undefined
|
|
190
|
+
readonly ttl?: Duration.Input | undefined
|
|
191
|
+
}
|
|
192
|
+
| undefined,
|
|
193
|
+
): Layer.Layer<
|
|
194
|
+
ModelsRepo,
|
|
195
|
+
never,
|
|
196
|
+
HttpClient.HttpClient | KeyValueStore.KeyValueStore
|
|
197
|
+
> =>
|
|
198
|
+
Layer.effect(
|
|
199
|
+
ModelsRepo,
|
|
200
|
+
Effect.gen(function* () {
|
|
201
|
+
const url = options?.url ?? "https://models.dev/api.json"
|
|
202
|
+
const ttl = Duration.toMillis(
|
|
203
|
+
Duration.fromInputUnsafe(options?.ttl ?? Duration.hours(24)),
|
|
204
|
+
)
|
|
205
|
+
const store = KeyValueStore.toSchemaStore(
|
|
206
|
+
KeyValueStore.prefix(
|
|
207
|
+
yield* KeyValueStore.KeyValueStore,
|
|
208
|
+
"models-dev/",
|
|
209
|
+
),
|
|
210
|
+
CachedCatalog,
|
|
211
|
+
)
|
|
212
|
+
const httpClient = (yield* HttpClient.HttpClient).pipe(
|
|
213
|
+
HttpClient.filterStatusOk,
|
|
214
|
+
)
|
|
215
|
+
|
|
216
|
+
const fetchCatalog = httpClient
|
|
217
|
+
.get(url)
|
|
218
|
+
.pipe(
|
|
219
|
+
Effect.flatMap(HttpClientResponse.schemaBodyJson(ModelsDevApi)),
|
|
220
|
+
Effect.map(toCatalog),
|
|
221
|
+
Effect.timeout("15 seconds"),
|
|
222
|
+
)
|
|
223
|
+
|
|
224
|
+
const load = Effect.gen(function* () {
|
|
225
|
+
const cached = yield* store.get(url).pipe(
|
|
226
|
+
Effect.map(Option.getOrUndefined),
|
|
227
|
+
Effect.catch(() => Effect.undefined),
|
|
228
|
+
)
|
|
229
|
+
const now = yield* Clock.currentTimeMillis
|
|
230
|
+
if (cached !== undefined && now - cached.fetchedAt < ttl) {
|
|
231
|
+
return cached.models
|
|
232
|
+
}
|
|
233
|
+
return yield* fetchCatalog.pipe(
|
|
234
|
+
Effect.tap((models) =>
|
|
235
|
+
store
|
|
236
|
+
.set(url, new CachedCatalog({ fetchedAt: now, models }))
|
|
237
|
+
.pipe(
|
|
238
|
+
Effect.catch((error) =>
|
|
239
|
+
Effect.logWarning(
|
|
240
|
+
"Failed to cache the models.dev catalog",
|
|
241
|
+
error,
|
|
242
|
+
),
|
|
243
|
+
),
|
|
244
|
+
),
|
|
245
|
+
),
|
|
246
|
+
Effect.catch((error) =>
|
|
247
|
+
Effect.logWarning(
|
|
248
|
+
cached !== undefined
|
|
249
|
+
? "Failed to refresh the models.dev catalog, using the cached catalog"
|
|
250
|
+
: "Failed to fetch the models.dev catalog, using the built-in catalog",
|
|
251
|
+
error,
|
|
252
|
+
).pipe(Effect.as(cached?.models ?? fallback)),
|
|
253
|
+
),
|
|
254
|
+
)
|
|
255
|
+
})
|
|
256
|
+
|
|
257
|
+
return ModelsRepo.of({ models: yield* Effect.cached(load) })
|
|
258
|
+
}),
|
|
259
|
+
)
|
|
260
|
+
}
|
package/src/Xai.test.ts
CHANGED
|
@@ -113,23 +113,72 @@ describe("Xai provider", () => {
|
|
|
113
113
|
)
|
|
114
114
|
assert.lengthOf(http.requests, 0)
|
|
115
115
|
assert.lengthOf(requests, 2)
|
|
116
|
-
for (const request of requests)
|
|
116
|
+
for (const request of requests) {
|
|
117
|
+
assert.strictEqual(request.store, true)
|
|
118
|
+
assert.strictEqual(request.reasoning?.effort, "high")
|
|
119
|
+
}
|
|
117
120
|
assert.isUndefined(requests[0]!.max_output_tokens)
|
|
118
121
|
assert.strictEqual(requests[1]!.max_output_tokens, 4000)
|
|
119
122
|
}),
|
|
120
123
|
)
|
|
121
124
|
}
|
|
122
125
|
|
|
126
|
+
it.effect("sends no WebSocket reasoning when reasoning is null", () =>
|
|
127
|
+
Effect.gen(function* () {
|
|
128
|
+
const requests: Array<
|
|
129
|
+
Parameters<
|
|
130
|
+
OpenAiClient.OpenAiSocket["Service"]["createResponseStream"]
|
|
131
|
+
>[0]
|
|
132
|
+
> = []
|
|
133
|
+
yield* Effect.gen(function* () {
|
|
134
|
+
const ai = yield* LanguageModel.LanguageModel
|
|
135
|
+
yield* ai
|
|
136
|
+
.streamText({ prompt: "hello" })
|
|
137
|
+
.pipe(Stream.runDrain, Effect.exit)
|
|
138
|
+
}).pipe(
|
|
139
|
+
Effect.provideService(OpenAiClient.OpenAiSocket, {
|
|
140
|
+
createResponseStream: (request) =>
|
|
141
|
+
Effect.sync(() => {
|
|
142
|
+
requests.push(request)
|
|
143
|
+
return [
|
|
144
|
+
HttpClientResponse.fromWeb(
|
|
145
|
+
HttpClientRequest.post("https://api.x.ai/v1/responses"),
|
|
146
|
+
new Response(),
|
|
147
|
+
),
|
|
148
|
+
Stream.empty,
|
|
149
|
+
] as const
|
|
150
|
+
}),
|
|
151
|
+
}),
|
|
152
|
+
Effect.provide(
|
|
153
|
+
Xai.modelWebSocket("grok-build-0.1", { reasoning: null }).pipe(
|
|
154
|
+
Layer.provide(
|
|
155
|
+
OpenAiClient.layer({ apiUrl: "https://api.x.ai/v1" }),
|
|
156
|
+
),
|
|
157
|
+
Layer.provide(capture().layer),
|
|
158
|
+
Layer.provide(
|
|
159
|
+
Layer.succeed(Socket.WebSocketConstructor, () => {
|
|
160
|
+
throw new Error("Unexpected live WebSocket")
|
|
161
|
+
}),
|
|
162
|
+
),
|
|
163
|
+
),
|
|
164
|
+
),
|
|
165
|
+
)
|
|
166
|
+
assert.lengthOf(requests, 1)
|
|
167
|
+
assert.isUndefined(requests[0]!.reasoning)
|
|
168
|
+
}),
|
|
169
|
+
)
|
|
170
|
+
|
|
123
171
|
it("exports Xai, but not XaiAuth, from the public entry point", () => {
|
|
124
172
|
assert.strictEqual(Public.Xai, Xai)
|
|
125
173
|
assert.isFalse("XaiAuth" in Public)
|
|
126
174
|
assert.strictEqual(Xai.model("grok-4.6").provider, "xai")
|
|
127
175
|
})
|
|
128
176
|
|
|
129
|
-
|
|
177
|
+
// `null` is how the CLI asks for no reasoning on models without efforts.
|
|
178
|
+
for (const effort of [undefined, "low", null] as const) {
|
|
130
179
|
it.effect(
|
|
131
180
|
"uses authenticated HTTP Responses with " +
|
|
132
|
-
(effort
|
|
181
|
+
(effort === undefined ? "default high" : (effort ?? "no")) +
|
|
133
182
|
" reasoning",
|
|
134
183
|
() =>
|
|
135
184
|
Effect.gen(function* () {
|
|
@@ -152,7 +201,9 @@ describe("Xai provider", () => {
|
|
|
152
201
|
Effect.provide(
|
|
153
202
|
Xai.model(
|
|
154
203
|
"grok-4.6",
|
|
155
|
-
effort === undefined
|
|
204
|
+
effort === undefined
|
|
205
|
+
? undefined
|
|
206
|
+
: { reasoning: effort === null ? null : { effort } },
|
|
156
207
|
).pipe(
|
|
157
208
|
Layer.provide(Xai.layerClient),
|
|
158
209
|
Layer.provide(http.layer),
|
|
@@ -175,9 +226,13 @@ describe("Xai provider", () => {
|
|
|
175
226
|
store: false,
|
|
176
227
|
stream: true,
|
|
177
228
|
})
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
}
|
|
229
|
+
if (effort === null) {
|
|
230
|
+
assert.isFalse("reasoning" in body(request))
|
|
231
|
+
} else {
|
|
232
|
+
assert.deepInclude(body(request).reasoning, {
|
|
233
|
+
effort: effort ?? "high",
|
|
234
|
+
})
|
|
235
|
+
}
|
|
181
236
|
assert.isFalse("max_output_tokens" in body(request))
|
|
182
237
|
}).pipe(Effect.provide(KeyValueStore.layerMemory)),
|
|
183
238
|
)
|
package/src/Xai.ts
CHANGED
|
@@ -20,15 +20,28 @@ export const layerClient = OpenAiClient.layer({
|
|
|
20
20
|
apiUrl: "https://api.x.ai/v1",
|
|
21
21
|
}).pipe(Layer.provide(XaiAuth.layerClient))
|
|
22
22
|
|
|
23
|
+
/**
|
|
24
|
+
* Options for a model. `reasoning: null` sends no reasoning options, for
|
|
25
|
+
* models that accept none; omitting `reasoning` keeps the default effort.
|
|
26
|
+
*
|
|
27
|
+
* @since 1.0.0
|
|
28
|
+
* @category Models
|
|
29
|
+
*/
|
|
30
|
+
export type ModelOptions = Omit<
|
|
31
|
+
OpenAiLanguageModel.Config["Service"],
|
|
32
|
+
"reasoning"
|
|
33
|
+
> & {
|
|
34
|
+
readonly reasoning?:
|
|
35
|
+
OpenAiLanguageModel.Config["Service"]["reasoning"] | null | undefined
|
|
36
|
+
} & typeof AgentModelConfig.Service
|
|
37
|
+
|
|
23
38
|
/**
|
|
24
39
|
* @since 1.0.0
|
|
25
40
|
* @category Layers
|
|
26
41
|
*/
|
|
27
42
|
export const model = (
|
|
28
43
|
model: string,
|
|
29
|
-
options?:
|
|
30
|
-
| (OpenAiLanguageModel.Config["Service"] & typeof AgentModelConfig.Service)
|
|
31
|
-
| undefined,
|
|
44
|
+
options?: ModelOptions | undefined,
|
|
32
45
|
): Model.Model<"xai", LanguageModel.LanguageModel, OpenAiClient.OpenAiClient> =>
|
|
33
46
|
Model.make(
|
|
34
47
|
"xai",
|
|
@@ -38,11 +51,15 @@ export const model = (
|
|
|
38
51
|
model,
|
|
39
52
|
config: {
|
|
40
53
|
...Struct.omit(options ?? {}, [
|
|
54
|
+
"reasoning",
|
|
41
55
|
"systemPromptTransform",
|
|
42
56
|
"supportsImages",
|
|
43
57
|
]),
|
|
44
58
|
store: options?.store ?? false,
|
|
45
|
-
reasoning:
|
|
59
|
+
reasoning:
|
|
60
|
+
options?.reasoning === null
|
|
61
|
+
? undefined
|
|
62
|
+
: { effort: "high", ...options?.reasoning },
|
|
46
63
|
},
|
|
47
64
|
}),
|
|
48
65
|
AgentModelConfig.layer({
|
|
@@ -64,9 +81,7 @@ export const model = (
|
|
|
64
81
|
*/
|
|
65
82
|
export const modelWebSocket = (
|
|
66
83
|
model: string,
|
|
67
|
-
options?:
|
|
68
|
-
| (OpenAiLanguageModel.Config["Service"] & typeof AgentModelConfig.Service)
|
|
69
|
-
| undefined,
|
|
84
|
+
options?: ModelOptions | undefined,
|
|
70
85
|
): Model.Model<
|
|
71
86
|
"xai",
|
|
72
87
|
| LanguageModel.LanguageModel
|
|
@@ -82,13 +97,17 @@ export const modelWebSocket = (
|
|
|
82
97
|
model,
|
|
83
98
|
config: {
|
|
84
99
|
...Struct.omit(options ?? {}, [
|
|
100
|
+
"reasoning",
|
|
85
101
|
"systemPromptTransform",
|
|
86
102
|
"supportsImages",
|
|
87
103
|
]),
|
|
88
104
|
// xAI needs stored responses to resolve previous_response_id.
|
|
89
105
|
store: true,
|
|
90
106
|
useItemReferences: false,
|
|
91
|
-
reasoning:
|
|
107
|
+
reasoning:
|
|
108
|
+
options?.reasoning === null
|
|
109
|
+
? undefined
|
|
110
|
+
: { effort: "high", ...options?.reasoning },
|
|
92
111
|
},
|
|
93
112
|
}),
|
|
94
113
|
AgentModelConfig.layer({
|
package/src/cli.ts
CHANGED
|
@@ -9,13 +9,12 @@ import * as NodeRuntime from "@effect/platform-node/NodeRuntime"
|
|
|
9
9
|
import * as GlobalWebSocket from "./GlobalWebSocket.ts"
|
|
10
10
|
import * as Acp from "./Acp.ts"
|
|
11
11
|
import * as AgentExecutor from "./AgentExecutor.ts"
|
|
12
|
-
import * as Codex from "./Codex.ts"
|
|
13
|
-
import * as Copilot from "./Copilot.ts"
|
|
14
|
-
import * as Xai from "./Xai.ts"
|
|
15
12
|
import * as Agent from "./Agent.ts"
|
|
16
13
|
import * as Compaction from "./Compaction.ts"
|
|
17
14
|
import * as McpConfig from "./McpConfig.ts"
|
|
18
15
|
import * as McpServers from "./McpServers.ts"
|
|
16
|
+
import * as ModelLayer from "./ModelLayer.ts"
|
|
17
|
+
import * as ModelsRepo from "./ModelsRepo.ts"
|
|
19
18
|
import * as Stream from "effect/Stream"
|
|
20
19
|
import * as Stdio from "effect/Stdio"
|
|
21
20
|
import { pipe } from "effect/Function"
|
|
@@ -23,8 +22,6 @@ import * as Layer from "effect/Layer"
|
|
|
23
22
|
import * as Logger from "effect/Logger"
|
|
24
23
|
import * as Path from "effect/Path"
|
|
25
24
|
import * as Schema from "effect/Schema"
|
|
26
|
-
import type * as LanguageModel from "effect/ai/LanguageModel"
|
|
27
|
-
import type * as Model from "effect/ai/Model"
|
|
28
25
|
import * as Config from "effect/Config"
|
|
29
26
|
import * as KeyValueStore from "effect/persistence/KeyValueStore"
|
|
30
27
|
import * as Option from "effect/Option"
|
|
@@ -41,44 +38,48 @@ const HttpClientLive = NodeHttpClient.layerNodeHttpNoAgent.pipe(
|
|
|
41
38
|
Layer.provide(NodeHttpClient.layerAgentOptions({ keepAlive: true })),
|
|
42
39
|
)
|
|
43
40
|
|
|
44
|
-
type Provider =
|
|
45
|
-
|
|
46
|
-
const providers
|
|
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
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
41
|
+
type Provider = ModelsRepo.Provider
|
|
42
|
+
|
|
43
|
+
const providers = ModelsRepo.Provider.literals
|
|
44
|
+
|
|
45
|
+
// Pick a model from the catalog, filtering by name as the user types, then
|
|
46
|
+
// one of its reasoning efforts.
|
|
47
|
+
const selectModel = Effect.fnUntraced(function* (provider: Provider) {
|
|
48
|
+
const repo = yield* ModelsRepo.ModelsRepo
|
|
49
|
+
const catalog = (yield* repo.models).filter(
|
|
50
|
+
(model) => model.provider === provider,
|
|
51
|
+
)
|
|
52
|
+
const models =
|
|
53
|
+
catalog.length > 0
|
|
54
|
+
? catalog
|
|
55
|
+
: ModelsRepo.fallback.filter((model) => model.provider === provider)
|
|
56
|
+
const defaultId = ModelsRepo.fallback.find(
|
|
57
|
+
(model) => model.provider === provider,
|
|
58
|
+
)?.id
|
|
59
|
+
const model = yield* Prompt.AutoComplete({
|
|
60
|
+
message: "Select a model",
|
|
61
|
+
choices: models.map((model) => ({
|
|
62
|
+
title: model.name,
|
|
63
|
+
description: model.id,
|
|
64
|
+
value: model,
|
|
65
|
+
selected: model.id === defaultId,
|
|
66
|
+
})),
|
|
67
|
+
})
|
|
68
|
+
if (model.efforts.length === 0) {
|
|
69
|
+
return { model: model.id, effort: null }
|
|
70
|
+
}
|
|
71
|
+
const effort = yield* Prompt.Select({
|
|
72
|
+
message: "Select a reasoning effort",
|
|
73
|
+
choices: model.efforts.map((effort) => ({
|
|
74
|
+
title: effort,
|
|
75
|
+
value: effort,
|
|
76
|
+
selected: effort === "medium",
|
|
77
|
+
})),
|
|
78
|
+
})
|
|
79
|
+
return { model: model.id, effort }
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
const isProvider = Schema.is(ModelsRepo.Provider)
|
|
82
83
|
|
|
83
84
|
// `<provider>/<model>/<effort>` as accepted by the `acp` --model flag.
|
|
84
85
|
const parseModelId = (modelId: string) => {
|
|
@@ -91,14 +92,6 @@ const parseModelId = (modelId: string) => {
|
|
|
91
92
|
: Option.none()
|
|
92
93
|
}
|
|
93
94
|
|
|
94
|
-
// `<provider>:<model>` as advertised over ACP.
|
|
95
|
-
const parseAcpModelId = (modelId: string) => {
|
|
96
|
-
const [provider, model, ...rest] = modelId.split(":")
|
|
97
|
-
return isProvider(provider) && model !== undefined && rest.length === 0
|
|
98
|
-
? Option.some({ provider, model })
|
|
99
|
-
: Option.none()
|
|
100
|
-
}
|
|
101
|
-
|
|
102
95
|
const ModelId = Schema.String.pipe(
|
|
103
96
|
Schema.check(
|
|
104
97
|
Schema.makeFilter(
|
|
@@ -272,21 +265,25 @@ const acp = Command.make("acp", {
|
|
|
272
265
|
"Run as an Agent Client Protocol (ACP) server over stdio",
|
|
273
266
|
),
|
|
274
267
|
Command.withHandler(({ model, apiKey, apiUrl }) =>
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
)
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
268
|
+
Effect.gen(function* () {
|
|
269
|
+
const repo = yield* ModelsRepo.ModelsRepo
|
|
270
|
+
return yield* Acp.runStdio({
|
|
271
|
+
version,
|
|
272
|
+
defaultModel: model.model,
|
|
273
|
+
defaultThoughtLevel: model.thoughtLevel,
|
|
274
|
+
availableModels: Effect.map(repo.models, ModelLayer.acpModels),
|
|
275
|
+
makeAgent: (cwd) =>
|
|
276
|
+
Agent.make.pipe(
|
|
277
|
+
Effect.provide(AgentExecutor.layerLocal({ directory: cwd })),
|
|
278
|
+
),
|
|
279
|
+
makeModel: ModelLayer.makeAcp({
|
|
280
|
+
models: repo.models,
|
|
281
|
+
openai: {
|
|
286
282
|
apiKey: Option.getOrUndefined(apiKey),
|
|
287
283
|
apiUrl: Option.getOrUndefined(apiUrl),
|
|
288
|
-
}
|
|
289
|
-
),
|
|
284
|
+
},
|
|
285
|
+
}),
|
|
286
|
+
})
|
|
290
287
|
}),
|
|
291
288
|
),
|
|
292
289
|
Command.provide(({ compaction }) =>
|
|
@@ -330,22 +327,17 @@ Command.make("clanka", {
|
|
|
330
327
|
const OutputFormatter = yield* Effect.promise(
|
|
331
328
|
() => import("./OutputFormatter.ts"),
|
|
332
329
|
)
|
|
333
|
-
const
|
|
334
|
-
onSome:
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
? Effect.succeed(value)
|
|
343
|
-
: Effect.fail("Invalid model")
|
|
344
|
-
},
|
|
345
|
-
}),
|
|
330
|
+
const { model, effort } = yield* Option.match(modelRaw, {
|
|
331
|
+
onSome: (selected) => {
|
|
332
|
+
const [model, effort] = selected.split("/") as [
|
|
333
|
+
string,
|
|
334
|
+
string | undefined,
|
|
335
|
+
]
|
|
336
|
+
return Effect.succeed({ model, effort })
|
|
337
|
+
},
|
|
338
|
+
onNone: () => selectModel(provider),
|
|
346
339
|
})
|
|
347
|
-
const
|
|
348
|
-
const Model = modelLayer(provider, model, reasoning, {
|
|
340
|
+
const Model = ModelLayer.make(provider, model, effort, {
|
|
349
341
|
apiKey: Option.getOrUndefined(apiKey),
|
|
350
342
|
apiUrl: Option.getOrUndefined(apiUrl),
|
|
351
343
|
})
|
|
@@ -411,8 +403,9 @@ Command.make("clanka", {
|
|
|
411
403
|
}),
|
|
412
404
|
Effect.provide([
|
|
413
405
|
NodeServices.layer,
|
|
414
|
-
|
|
415
|
-
|
|
406
|
+
ModelsRepo.ModelsRepo.layerModelsDev().pipe(
|
|
407
|
+
Layer.provideMerge([Kvs, HttpClientLive]),
|
|
408
|
+
),
|
|
416
409
|
GlobalWebSocket.layerWebSocketConstructor,
|
|
417
410
|
DeviceCodeHandler.layerConsole,
|
|
418
411
|
]),
|