clanka 0.17.3 → 0.18.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.
Files changed (100) hide show
  1. package/README.md +24 -0
  2. package/dist/Acp.d.ts.map +1 -1
  3. package/dist/Acp.js +5 -0
  4. package/dist/Acp.js.map +1 -1
  5. package/dist/Agent.d.ts +11 -0
  6. package/dist/Agent.d.ts.map +1 -1
  7. package/dist/Agent.js +35 -15
  8. package/dist/Agent.js.map +1 -1
  9. package/dist/Agent.test.js +23 -0
  10. package/dist/Agent.test.js.map +1 -1
  11. package/dist/AgentExecutor.d.ts.map +1 -1
  12. package/dist/AgentExecutor.js +40 -8
  13. package/dist/AgentExecutor.js.map +1 -1
  14. package/dist/AgentExecutor.test.js +51 -2
  15. package/dist/AgentExecutor.test.js.map +1 -1
  16. package/dist/AgentOutput.d.ts +23 -4
  17. package/dist/AgentOutput.d.ts.map +1 -1
  18. package/dist/AgentOutput.js +19 -0
  19. package/dist/AgentOutput.js.map +1 -1
  20. package/dist/AgentTools.d.ts +4 -4
  21. package/dist/AgentTools.d.ts.map +1 -1
  22. package/dist/AgentTools.js +7 -2
  23. package/dist/AgentTools.js.map +1 -1
  24. package/dist/AgentTools.test.js +22 -0
  25. package/dist/AgentTools.test.js.map +1 -1
  26. package/dist/ClaudeAgent.d.ts.map +1 -1
  27. package/dist/ClaudeAgent.js +37 -4
  28. package/dist/ClaudeAgent.js.map +1 -1
  29. package/dist/ClaudeAgent.test.js +82 -0
  30. package/dist/ClaudeAgent.test.js.map +1 -1
  31. package/dist/Codex.js +9 -5
  32. package/dist/Codex.js.map +1 -1
  33. package/dist/Codex.test.js +26 -7
  34. package/dist/Codex.test.js.map +1 -1
  35. package/dist/Compaction.d.ts +41 -6
  36. package/dist/Compaction.d.ts.map +1 -1
  37. package/dist/Compaction.js +52 -11
  38. package/dist/Compaction.js.map +1 -1
  39. package/dist/Compaction.test.js +5 -4
  40. package/dist/Compaction.test.js.map +1 -1
  41. package/dist/CompactionTransport.test.js +11 -0
  42. package/dist/CompactionTransport.test.js.map +1 -1
  43. package/dist/Copilot.d.ts.map +1 -1
  44. package/dist/Copilot.js +3 -6
  45. package/dist/Copilot.js.map +1 -1
  46. package/dist/ModelLayer.d.ts +35 -6
  47. package/dist/ModelLayer.d.ts.map +1 -1
  48. package/dist/ModelLayer.js +30 -18
  49. package/dist/ModelLayer.js.map +1 -1
  50. package/dist/ModelLayer.test.js +11 -3
  51. package/dist/ModelLayer.test.js.map +1 -1
  52. package/dist/OutputFormatter.d.ts.map +1 -1
  53. package/dist/OutputFormatter.js +3 -0
  54. package/dist/OutputFormatter.js.map +1 -1
  55. package/dist/Xai.d.ts.map +1 -1
  56. package/dist/Xai.js +4 -12
  57. package/dist/Xai.js.map +1 -1
  58. package/dist/bin/{Effect-DNYYQ84C.mjs → Effect-5XYMLOiB.mjs} +31 -30
  59. package/dist/bin/Effect-5XYMLOiB.mjs.map +1 -0
  60. package/dist/bin/{Latch-BSlIwuxI.mjs → Latch-BnrpWMd1.mjs} +2 -2
  61. package/dist/bin/{Latch-BSlIwuxI.mjs.map → Latch-BnrpWMd1.mjs.map} +1 -1
  62. package/dist/bin/{McpClient-C7ctqJWo.mjs → McpClient-DoPNaNzW.mjs} +3 -3
  63. package/dist/bin/{McpClient-C7ctqJWo.mjs.map → McpClient-DoPNaNzW.mjs.map} +1 -1
  64. package/dist/bin/{OutputFormatter-DXej05JW.mjs → OutputFormatter-Dqxyk2qD.mjs} +4 -3
  65. package/dist/bin/{OutputFormatter-DXej05JW.mjs.map → OutputFormatter-Dqxyk2qD.mjs.map} +1 -1
  66. package/dist/bin/{Schema-CSQN_Zso.mjs → Schema-Box_9VZG.mjs} +6 -3
  67. package/dist/bin/{Schema-CSQN_Zso.mjs.map → Schema-Box_9VZG.mjs.map} +1 -1
  68. package/dist/bin/{SemanticSearch-BiwCnpKX.mjs → SemanticSearch-CH125AHH.mjs} +4 -4
  69. package/dist/bin/{SemanticSearch-BiwCnpKX.mjs.map → SemanticSearch-CH125AHH.mjs.map} +1 -1
  70. package/dist/bin/{Stream-Dwvj9X5X.mjs → Stream-C51usiew.mjs} +3 -3
  71. package/dist/bin/{Stream-Dwvj9X5X.mjs.map → Stream-C51usiew.mjs.map} +1 -1
  72. package/dist/bin/cli.mjs +337 -229
  73. package/dist/bin/cli.mjs.map +1 -1
  74. package/dist/bin/{upgrade-BSNjCnra.mjs → upgrade-HMRGSqq8.mjs} +4 -4
  75. package/dist/bin/{upgrade-BSNjCnra.mjs.map → upgrade-HMRGSqq8.mjs.map} +1 -1
  76. package/dist/cli.js +45 -5
  77. package/dist/cli.js.map +1 -1
  78. package/package.json +1 -1
  79. package/src/Acp.ts +7 -0
  80. package/src/Agent.test.ts +27 -0
  81. package/src/Agent.ts +49 -15
  82. package/src/AgentExecutor.test.ts +80 -2
  83. package/src/AgentExecutor.ts +70 -10
  84. package/src/AgentOutput.ts +23 -0
  85. package/src/AgentTools.test.ts +28 -0
  86. package/src/AgentTools.ts +7 -2
  87. package/src/ClaudeAgent.test.ts +113 -0
  88. package/src/ClaudeAgent.ts +51 -4
  89. package/src/Codex.test.ts +37 -9
  90. package/src/Codex.ts +25 -20
  91. package/src/Compaction.test.ts +18 -15
  92. package/src/Compaction.ts +89 -20
  93. package/src/CompactionTransport.test.ts +36 -0
  94. package/src/Copilot.ts +5 -6
  95. package/src/ModelLayer.test.ts +17 -2
  96. package/src/ModelLayer.ts +68 -22
  97. package/src/OutputFormatter.ts +3 -0
  98. package/src/Xai.ts +8 -12
  99. package/src/cli.ts +97 -5
  100. package/dist/bin/Effect-DNYYQ84C.mjs.map +0 -1
package/src/Codex.ts CHANGED
@@ -73,32 +73,37 @@ const layerModel = (
73
73
  model: (string & {}) | OpenAiLanguageModel.Model,
74
74
  options?: ModelOptions | undefined,
75
75
  ) =>
76
- OpenAiLanguageModel.layer({
77
- model,
78
- config: {
79
- ...Struct.omit(options ?? {}, [
80
- "reasoning",
81
- "systemPromptTransform",
82
- "supportsImages",
83
- ]),
84
- store: false,
85
- reasoning:
86
- options?.reasoning === null
87
- ? undefined
88
- : {
89
- effort: "medium",
90
- summary: "auto",
91
- ...(options?.reasoning as any),
92
- },
93
- },
94
- }).pipe(
76
+ // One prompt cache key per model layer, so the requests of a session, and
77
+ // of the sub-agents sharing its system prompt, are routed to the same
78
+ // cache. A key in `options` wins.
79
+ Layer.suspend(() =>
80
+ OpenAiLanguageModel.layer({
81
+ model,
82
+ config: {
83
+ ...Struct.omit(options ?? {}, [
84
+ "reasoning",
85
+ ...AgentModelConfig.optionKeys,
86
+ ]),
87
+ prompt_cache_key: options?.prompt_cache_key ?? crypto.randomUUID(),
88
+ store: false,
89
+ reasoning:
90
+ options?.reasoning === null
91
+ ? undefined
92
+ : {
93
+ effort: "medium",
94
+ summary: "auto",
95
+ ...(options?.reasoning as any),
96
+ },
97
+ },
98
+ }),
99
+ ).pipe(
95
100
  Layer.merge(
96
101
  AgentModelConfig.layer({
102
+ ...Struct.pick(options ?? {}, AgentModelConfig.optionKeys),
97
103
  systemPromptTransform: (system, effect) =>
98
104
  OpenAiLanguageModel.withConfigOverride(effect, {
99
105
  instructions: system,
100
106
  }),
101
- supportsImages: options?.supportsImages,
102
107
  }),
103
108
  // No Compaction.SummarizerTransform: Codex rejects max_output_tokens,
104
109
  // so compaction summaries have no configured output-token cap.
@@ -184,17 +184,20 @@ describe("Compaction.estimateTokens", () => {
184
184
  })
185
185
 
186
186
  describe("Compaction.CompactionConfig", () => {
187
- it.effect("defaults to a 236k window with a 16k reserve", () =>
188
- Effect.gen(function* () {
189
- const config = yield* Compaction.CompactionConfig
190
- assert.deepStrictEqual(Compaction.defaultConfig, {
191
- enabled: true,
192
- contextWindow: 236_000,
193
- reserveTokens: 16_000,
194
- keepRecentTokens: 20_000,
195
- })
196
- assert.deepStrictEqual(config, Compaction.defaultConfig)
197
- }),
187
+ it.effect(
188
+ "defaults to a 236k window, a 16k reserve and a 160k threshold",
189
+ () =>
190
+ Effect.gen(function* () {
191
+ const config = yield* Compaction.CompactionConfig
192
+ assert.deepStrictEqual(Compaction.defaultConfig, {
193
+ enabled: true,
194
+ contextWindow: 236_000,
195
+ reserveTokens: 16_000,
196
+ thresholdTokens: 160_000,
197
+ keepRecentTokens: 20_000,
198
+ })
199
+ assert.deepStrictEqual(config, Compaction.defaultConfig)
200
+ }),
198
201
  )
199
202
  })
200
203
 
@@ -207,9 +210,9 @@ describe("Compaction.shouldCompact", () => {
207
210
  const small = Prompt.fromMessages([user("hi")])
208
211
 
209
212
  for (const [tokens, expected] of [
210
- [219_999, false],
211
- [220_000, false],
212
- [220_001, true],
213
+ [159_999, false],
214
+ [160_000, false],
215
+ [160_001, true],
213
216
  ] as const) {
214
217
  it(`returns ${expected} with default config and ${tokens} reported tokens`, () => {
215
218
  assert.strictEqual(
@@ -823,4 +826,4 @@ describe("Compaction.rewrite", () => {
823
826
  )
824
827
  }),
825
828
  )
826
- })
829
+ })
package/src/Compaction.ts CHANGED
@@ -7,8 +7,8 @@
7
7
  * (`capOutput`). It runs on every surface and is not affected by the
8
8
  * compaction kill switch.
9
9
  * 2. Auto-compaction of the live Prompt (`compactIfNeeded` before a model
10
- * call, `compactAfterOverflow` after a context-length error). Both rewrite
11
- * the Prompt to `system + <compaction-summary> user message + kept tail`.
10
+ * call, `compact` after a context-length error). Both rewrite the Prompt
11
+ * to `system + <compaction-summary> user message + kept tail`.
12
12
  *
13
13
  * @since 1.0.0
14
14
  */
@@ -21,6 +21,7 @@ import * as Predicate from "effect/Predicate"
21
21
  import * as Stream from "effect/Stream"
22
22
  import * as AiError from "effect/ai/AiError"
23
23
  import * as LanguageModel from "effect/ai/LanguageModel"
24
+ import type * as Model from "effect/ai/Model"
24
25
  import * as Prompt from "effect/ai/Prompt"
25
26
  import type * as AgentOutput from "./AgentOutput.ts"
26
27
  import * as Image from "./Image.ts"
@@ -35,9 +36,11 @@ import * as Image from "./Image.ts"
35
36
  * - `enabled`: kill switch for both compact paths. Does **not** disable the
36
37
  * `execute` output cap.
37
38
  * - `contextWindow`: assumed model context window in tokens.
38
- * - `reserveTokens`: headroom kept free below `contextWindow`. Compaction
39
- * triggers once the last reported `contextTokens` (or the estimate) exceeds
40
- * `contextWindow - reserveTokens`.
39
+ * - `reserveTokens`: headroom kept free below `contextWindow`.
40
+ * - `thresholdTokens`: compaction triggers once the last reported
41
+ * `contextTokens` (or the estimate) exceeds this many tokens, or
42
+ * `contextWindow - reserveTokens` when that is lower. Compacting well below
43
+ * the context window keeps every turn's input smaller.
41
44
  * - `keepRecentTokens`: size of the recent tail preserved verbatim after a
42
45
  * compaction.
43
46
  *
@@ -48,6 +51,7 @@ export interface CompactionConfigService {
48
51
  readonly enabled: boolean
49
52
  readonly contextWindow: number
50
53
  readonly reserveTokens: number
54
+ readonly thresholdTokens: number
51
55
  readonly keepRecentTokens: number
52
56
  }
53
57
 
@@ -59,6 +63,7 @@ export const defaultConfig: CompactionConfigService = {
59
63
  enabled: true,
60
64
  contextWindow: 236_000,
61
65
  reserveTokens: 16_000,
66
+ thresholdTokens: 160_000,
62
67
  keepRecentTokens: 20_000,
63
68
  }
64
69
 
@@ -89,6 +94,48 @@ export class SummarizerTransform extends Context.Reference<
89
94
  defaultValue: () => identity,
90
95
  }) {}
91
96
 
97
+ /**
98
+ * The model the summarizer runs on. Without it, the summarizer runs on the
99
+ * agent's own model.
100
+ *
101
+ * The layer is built for each summarizer call, so a stateful model such as
102
+ * Claude Agent never shares its session with an agent. It replaces the
103
+ * agent's `SummarizerTransform` with its own, or with the identity when it
104
+ * has none.
105
+ *
106
+ * @since 1.0.0
107
+ * @category Configuration
108
+ */
109
+ export class SummarizerModel extends Context.Service<
110
+ SummarizerModel,
111
+ Layer.Layer<
112
+ LanguageModel.LanguageModel | Model.ProviderName | Model.ModelName
113
+ >
114
+ >()("clanka/Compaction/SummarizerModel") {}
115
+
116
+ /**
117
+ * Run the summarizer on `layer` instead of the agent's model.
118
+ *
119
+ * @since 1.0.0
120
+ * @category Configuration
121
+ */
122
+ export const layerSummarizerModel = <E, R>(
123
+ layer: Layer.Layer<
124
+ LanguageModel.LanguageModel | Model.ProviderName | Model.ModelName,
125
+ E,
126
+ R
127
+ >,
128
+ ): Layer.Layer<SummarizerModel, never, R> =>
129
+ Layer.effect(
130
+ SummarizerModel,
131
+ Effect.gen(function* () {
132
+ const services = yield* Effect.context<R>()
133
+ return Layer.fresh(Layer.orDie(layer)).pipe(
134
+ Layer.provide(Layer.succeedContext(services)),
135
+ )
136
+ }),
137
+ )
138
+
92
139
  // =============================================================================
93
140
  // Constants
94
141
  // =============================================================================
@@ -253,6 +300,16 @@ export const estimateTokens = (prompt: Prompt.Prompt): number => {
253
300
  export const estimateMessageTokens = (message: Prompt.Message): number =>
254
301
  Math.ceil(estimateMessageChars(message) / 4)
255
302
 
303
+ /**
304
+ * The token count above which the threshold trigger fires: `thresholdTokens`,
305
+ * or `contextWindow - reserveTokens` when that is lower.
306
+ *
307
+ * @since 1.0.0
308
+ * @category Tokens
309
+ */
310
+ export const compactionThreshold = (config: CompactionConfigService): number =>
311
+ Math.min(config.thresholdTokens, config.contextWindow - config.reserveTokens)
312
+
256
313
  /**
257
314
  * Whether the threshold trigger fires for the next model call.
258
315
  *
@@ -272,7 +329,7 @@ export const shouldCompact = (options: {
272
329
  }): boolean => {
273
330
  if (!options.config.enabled) return false
274
331
  const tokens = options.contextTokens ?? estimateTokens(options.prompt)
275
- return tokens > options.config.contextWindow - options.config.reserveTokens
332
+ return tokens > compactionThreshold(options.config)
276
333
  }
277
334
 
278
335
  // =============================================================================
@@ -727,7 +784,8 @@ export interface CompactionResult {
727
784
  * Returns `None` without calling the model when compaction is disabled or
728
785
  * when `split` finds nothing to summarize. Otherwise `onStart` runs, the
729
786
  * summarizer is called with `streamText` (the Codex backend rejects
730
- * non-streaming requests) through `SummarizerTransform`, and the prompt is
787
+ * non-streaming requests) through `SummarizerTransform`, on the
788
+ * `SummarizerModel` when one is provided, and the prompt is
731
789
  * rewritten to `system + summary + trimKept(kept)`.
732
790
  *
733
791
  * Fails with the summarizer's `AiError`. Callers decide whether that is fatal.
@@ -759,21 +817,32 @@ export const compact: (options: {
759
817
  yield* options.onStart(options.reason)
760
818
  }
761
819
 
762
- const ai = yield* LanguageModel.LanguageModel
763
- const transform = yield* SummarizerTransform
764
- const summary = (yield* transform(
765
- ai
766
- .streamText({
767
- prompt: summarizerPrompt({ previousSummary, messages: toSummarize }),
768
- })
769
- .pipe(
770
- Stream.runFold(
771
- () => "",
772
- (text, part) =>
773
- part.type === "text-delta" ? text + part.delta : text,
820
+ const summarize = Effect.gen(function* () {
821
+ const ai = yield* LanguageModel.LanguageModel
822
+ const transform = yield* SummarizerTransform
823
+ return yield* transform(
824
+ ai
825
+ .streamText({
826
+ prompt: summarizerPrompt({ previousSummary, messages: toSummarize }),
827
+ })
828
+ .pipe(
829
+ Stream.runFold(
830
+ () => "",
831
+ (text, part) =>
832
+ part.type === "text-delta" ? text + part.delta : text,
833
+ ),
774
834
  ),
835
+ )
836
+ })
837
+ const summarizerModel = yield* Effect.serviceOption(SummarizerModel)
838
+ const summary = (yield* Option.match(summarizerModel, {
839
+ onNone: () => summarize,
840
+ onSome: (model) =>
841
+ summarize.pipe(
842
+ Effect.provide(model),
843
+ Effect.provideService(SummarizerTransform, identity),
775
844
  ),
776
- )).trim()
845
+ })).trim()
777
846
 
778
847
  if (summary.length === 0) {
779
848
  return yield* AiError.make({
@@ -15,6 +15,7 @@ import * as HttpClientResponse from "effect/http/HttpClientResponse"
15
15
  import * as Codex from "./Codex.ts"
16
16
  import * as Compaction from "./Compaction.ts"
17
17
  import * as Copilot from "./Copilot.ts"
18
+ import * as Xai from "./Xai.ts"
18
19
 
19
20
  const user = (text: string) =>
20
21
  Prompt.makeMessage("user", {
@@ -225,4 +226,39 @@ describe("Compaction provider transport", () => {
225
226
  assert.strictEqual(continuation.stream, true)
226
227
  }),
227
228
  )
229
+
230
+ it.effect(
231
+ "summarizes on the SummarizerModel without the agent model's transform",
232
+ () =>
233
+ Effect.gen(function* () {
234
+ const http = captureHttp()
235
+ yield* compact.pipe(
236
+ Effect.provide(
237
+ Layer.mergeAll(
238
+ // xAI caps summaries with max_output_tokens on the same
239
+ // client Codex uses, so a leaked transform would show up.
240
+ Xai.model("grok-4.6").pipe(
241
+ Layer.provide(
242
+ OpenAiClient.layer({ apiUrl: "https://xai.test" }),
243
+ ),
244
+ ),
245
+ Compaction.layerSummarizerModel(
246
+ Codex.model("gpt-6-astra").pipe(
247
+ Layer.provide(
248
+ OpenAiClient.layer({ apiUrl: "https://codex.test" }),
249
+ ),
250
+ ),
251
+ ),
252
+ ).pipe(Layer.provide(http.layer)),
253
+ ),
254
+ Effect.exit,
255
+ )
256
+ assert.strictEqual(http.requests.length, 1)
257
+ assert.strictEqual(
258
+ http.requests[0]!.url,
259
+ "https://codex.test/responses",
260
+ )
261
+ assert.isFalse("max_output_tokens" in body(http.requests[0]!))
262
+ }),
263
+ )
228
264
  })
package/src/Copilot.ts CHANGED
@@ -42,14 +42,13 @@ export const model = (
42
42
  config: Struct.omit(
43
43
  options ?? {},
44
44
  options?.reasoning === null
45
- ? ["reasoning", "systemPromptTransform", "supportsImages"]
46
- : ["systemPromptTransform", "supportsImages"],
45
+ ? ["reasoning", ...AgentModelConfig.optionKeys]
46
+ : AgentModelConfig.optionKeys,
47
47
  ),
48
48
  }),
49
- AgentModelConfig.layer({
50
- systemPromptTransform: options?.systemPromptTransform,
51
- supportsImages: options?.supportsImages,
52
- }),
49
+ AgentModelConfig.layer(
50
+ Struct.pick(options ?? {}, AgentModelConfig.optionKeys),
51
+ ),
53
52
  // Cap compaction summaries; Copilot honours max_output_tokens.
54
53
  Layer.succeed(Compaction.SummarizerTransform, (effect) =>
55
54
  OpenAiLanguageModel.withConfigOverride(effect, {
@@ -11,6 +11,7 @@ import * as HttpClientResponse from "effect/http/HttpClientResponse"
11
11
  import * as KeyValueStore from "effect/persistence/KeyValueStore"
12
12
  import * as Socket from "effect/socket/Socket"
13
13
  import type * as Acp from "./Acp.ts"
14
+ import * as Agent from "./Agent.ts"
14
15
  import { DeviceCodeHandler } from "./DeviceCodeHandler.ts"
15
16
  import * as ModelLayer from "./ModelLayer.ts"
16
17
  import * as ModelsRepo from "./ModelsRepo.ts"
@@ -40,6 +41,7 @@ const makeModel = ModelLayer.makeAcp({
40
41
  apiKey: Redacted.make("test-key"),
41
42
  apiUrl: "https://codex.test",
42
43
  },
44
+ auxiliary: { subagent: { provider: "openai", model: "gpt-4.1" } },
43
45
  })
44
46
 
45
47
  // The other providers' login and socket services, which the API-key openai
@@ -55,11 +57,12 @@ const unused = Layer.mergeAll(
55
57
  )
56
58
 
57
59
  // The request body the model for `modelId` sends at session thought level
58
- // `thoughtLevel`.
60
+ // `thoughtLevel`, or its sub-agent model with `subagent`.
59
61
  const requestBody = Effect.fnUntraced(function* (
60
62
  modelId: string,
61
63
  thoughtLevel: Acp.ThoughtLevel = "high",
62
64
  fastMode = false,
65
+ subagent = false,
63
66
  ) {
64
67
  const requests: Array<HttpClientRequest.HttpClientRequest> = []
65
68
  const client = HttpClient.make((request) =>
@@ -72,9 +75,12 @@ const requestBody = Effect.fnUntraced(function* (
72
75
  }),
73
76
  )
74
77
  const model = Option.getOrThrow(makeModel(modelId, thoughtLevel, fastMode))
75
- yield* Effect.gen(function* () {
78
+ const send = Effect.gen(function* () {
76
79
  const ai = yield* LanguageModel.LanguageModel
77
80
  yield* ai.streamText({ prompt: "hello" }).pipe(Stream.runDrain, Effect.exit)
81
+ })
82
+ yield* Effect.gen(function* () {
83
+ yield* subagent ? Effect.provide(send, yield* Agent.SubagentModel) : send
78
84
  }).pipe(
79
85
  Effect.provide(
80
86
  model.pipe(
@@ -129,6 +135,15 @@ describe("ModelLayer.makeAcp", () => {
129
135
  }),
130
136
  )
131
137
 
138
+ it.effect("runs sub-agents on the auxiliary sub-agent model", () =>
139
+ Effect.gen(function* () {
140
+ assert.strictEqual(
141
+ (yield* requestBody("openai:gpt-6-astra", "high", false, true)).model,
142
+ "gpt-4.1",
143
+ )
144
+ }),
145
+ )
146
+
132
147
  it("rejects ids that are not <provider>:<model>", () => {
133
148
  assert.isTrue(Option.isNone(makeModel("nope", "high")))
134
149
  })
package/src/ModelLayer.ts CHANGED
@@ -5,8 +5,6 @@
5
5
  * @since 1.0.0
6
6
  */
7
7
  import { OpenAiClient } from "@effect/ai-openai"
8
- import type * as LanguageModel from "effect/ai/LanguageModel"
9
- import type * as Model from "effect/ai/Model"
10
8
  import * as Effect from "effect/Effect"
11
9
  import * as Layer from "effect/Layer"
12
10
  import * as Option from "effect/Option"
@@ -15,18 +13,11 @@ import { ThoughtLevel } from "./Acp.ts"
15
13
  import * as Agent from "./Agent.ts"
16
14
  import * as ClaudeAgent from "./ClaudeAgent.ts"
17
15
  import * as Codex from "./Codex.ts"
16
+ import * as Compaction from "./Compaction.ts"
18
17
  import * as Copilot from "./Copilot.ts"
19
18
  import * as ModelsRepo from "./ModelsRepo.ts"
20
19
  import * as Xai from "./Xai.ts"
21
20
 
22
- const withSubagentModel = <E, R>(
23
- model: Layer.Layer<
24
- LanguageModel.LanguageModel | Model.ProviderName | Model.ModelName,
25
- E,
26
- R
27
- >,
28
- ) => Layer.merge(model, Agent.layerSubagentModel(model))
29
-
30
21
  /**
31
22
  * Whether a provider supports fast mode. Only OpenAI does, through the
32
23
  * `priority` service tier.
@@ -38,20 +29,37 @@ export const supportsFastMode = (provider: ModelsRepo.Provider) =>
38
29
  provider === "openai"
39
30
 
40
31
  /**
41
- * The model layer for a provider and model id. `effort` is `null` for models
32
+ * A provider, model id and reasoning effort. `effort` is `null` for models
42
33
  * that take no reasoning effort, and `undefined` to keep the provider's
43
- * default effort. `fastMode` requests the `priority` service tier and is
44
- * ignored for providers without fast mode.
34
+ * default effort.
45
35
  *
46
36
  * @since 1.0.0
47
- * @category Layers
37
+ * @category Models
48
38
  */
49
- export const make = (
39
+ export interface ModelSpec {
40
+ readonly provider: ModelsRepo.Provider
41
+ readonly model: string
42
+ readonly effort?: string | null | undefined
43
+ }
44
+
45
+ /**
46
+ * The models sub-agents and the compaction summarizer run on, when they
47
+ * should not run on the agent's own model.
48
+ *
49
+ * @since 1.0.0
50
+ * @category Models
51
+ */
52
+ export interface AuxiliaryModels {
53
+ readonly subagent?: ModelSpec | undefined
54
+ readonly summarizer?: ModelSpec | undefined
55
+ }
56
+
57
+ const languageModel = (
50
58
  provider: ModelsRepo.Provider,
51
59
  model: string,
52
60
  effort: string | null | undefined,
53
61
  openai: OpenAiClient.Options,
54
- fastMode = false,
62
+ fastMode: boolean,
55
63
  ) => {
56
64
  const options =
57
65
  effort === undefined
@@ -63,25 +71,61 @@ export const make = (
63
71
  switch (provider) {
64
72
  case "openai":
65
73
  return openai.apiKey !== undefined
66
- ? withSubagentModel(Codex.model(model, openaiOptions)).pipe(
74
+ ? Codex.model(model, openaiOptions).pipe(
67
75
  Layer.provide(OpenAiClient.layer(openai)),
68
76
  )
69
- : withSubagentModel(Codex.modelWebSocket(model, openaiOptions)).pipe(
77
+ : Codex.modelWebSocket(model, openaiOptions).pipe(
70
78
  Layer.provide(Codex.layerClient),
71
79
  )
72
80
  case "xai":
73
- return withSubagentModel(Xai.modelWebSocket(model, options)).pipe(
81
+ return Xai.modelWebSocket(model, options).pipe(
74
82
  Layer.provide(Xai.layerClient),
75
83
  )
76
84
  case "claude":
77
- return withSubagentModel(ClaudeAgent.model(model, { effort }))
85
+ return ClaudeAgent.model(model, { effort })
78
86
  case "copilot":
79
- return withSubagentModel(Copilot.model(model, options)).pipe(
87
+ return Copilot.model(model, options).pipe(
80
88
  Layer.provide(Copilot.layerClient),
81
89
  )
82
90
  }
83
91
  }
84
92
 
93
+ /**
94
+ * The model layer for a provider and model id. `effort` is `null` for models
95
+ * that take no reasoning effort, and `undefined` to keep the provider's
96
+ * default effort. `fastMode` requests the `priority` service tier and is
97
+ * ignored for providers without fast mode; it also applies to the
98
+ * `auxiliary` models.
99
+ *
100
+ * Sub-agents run on `auxiliary.subagent`, or on this model when it is not
101
+ * set. The compaction summarizer runs on `auxiliary.summarizer`, or on the
102
+ * model of the agent that compacts when it is not set.
103
+ *
104
+ * @since 1.0.0
105
+ * @category Layers
106
+ */
107
+ export const make = (
108
+ provider: ModelsRepo.Provider,
109
+ model: string,
110
+ effort: string | null | undefined,
111
+ openai: OpenAiClient.Options,
112
+ fastMode = false,
113
+ auxiliary?: AuxiliaryModels,
114
+ ) => {
115
+ const fromSpec = (spec: ModelSpec) =>
116
+ languageModel(spec.provider, spec.model, spec.effort, openai, fastMode)
117
+ const main = languageModel(provider, model, effort, openai, fastMode)
118
+ return Layer.mergeAll(
119
+ main,
120
+ Agent.layerSubagentModel(
121
+ auxiliary?.subagent === undefined ? main : fromSpec(auxiliary.subagent),
122
+ ),
123
+ auxiliary?.summarizer === undefined
124
+ ? Layer.empty
125
+ : Compaction.layerSummarizerModel(fromSpec(auxiliary.summarizer)),
126
+ )
127
+ }
128
+
85
129
  const isProvider = Schema.is(ModelsRepo.Provider)
86
130
 
87
131
  /**
@@ -152,7 +196,7 @@ const resolveEffort = (
152
196
  * A listed model that does not support the session thought level runs at the
153
197
  * supported effort nearest to it in `ThoughtLevel` order, the higher one on a
154
198
  * tie. Unlisted models run at the session thought level. Fast mode applies
155
- * to OpenAI models only.
199
+ * to OpenAI models only. Every session uses the same `auxiliary` models.
156
200
  *
157
201
  * @since 1.0.0
158
202
  * @category ACP
@@ -161,6 +205,7 @@ export const makeAcp =
161
205
  (options: {
162
206
  readonly models: Effect.Effect<ReadonlyArray<ModelsRepo.ModelInfo>>
163
207
  readonly openai: OpenAiClient.Options
208
+ readonly auxiliary?: AuxiliaryModels | undefined
164
209
  }) =>
165
210
  (modelId: string, thoughtLevel: ThoughtLevel, fastMode = false) =>
166
211
  Option.map(parseAcpModelId(modelId), ({ provider, model }) =>
@@ -177,6 +222,7 @@ export const makeAcp =
177
222
  : resolveEffort(info.efforts, thoughtLevel),
178
223
  options.openai,
179
224
  fastMode,
225
+ options.auxiliary,
180
226
  )
181
227
  }),
182
228
  ),
@@ -117,6 +117,9 @@ ${chalk.dim(output.summary)}\n\n`
117
117
  case "CompactionEnded": {
118
118
  return `${prefix}${chalkInfoHeading(`${infoIcon} Context compacted (${output.reason}):`)} ~${numberFormat.format(output.tokensBefore)} tokens to ~${numberFormat.format(output.tokensAfter)}\n\n`
119
119
  }
120
+ case "SessionRestarted": {
121
+ return `${prefix}${chalkInfoHeading(`${infoIcon} Session restarted (${output.reason}):`)} ${numberFormat.format(output.contextTokens)} context tokens resent, ${numberFormat.format(output.cacheWrite)} cache write\n\n`
122
+ }
120
123
  case "EnvironmentUpdated": {
121
124
  return `${prefix}${chalkInfoHeading(`${infoIcon} Environment update:`)}\n\n${chalk.dim(truncate(output.update))}\n\n`
122
125
  }
package/src/Xai.ts CHANGED
@@ -52,8 +52,7 @@ export const model = (
52
52
  config: {
53
53
  ...Struct.omit(options ?? {}, [
54
54
  "reasoning",
55
- "systemPromptTransform",
56
- "supportsImages",
55
+ ...AgentModelConfig.optionKeys,
57
56
  ]),
58
57
  store: options?.store ?? false,
59
58
  reasoning:
@@ -62,10 +61,9 @@ export const model = (
62
61
  : { effort: "high", ...options?.reasoning },
63
62
  },
64
63
  }),
65
- AgentModelConfig.layer({
66
- systemPromptTransform: options?.systemPromptTransform,
67
- supportsImages: options?.supportsImages,
68
- }),
64
+ AgentModelConfig.layer(
65
+ Struct.pick(options ?? {}, AgentModelConfig.optionKeys),
66
+ ),
69
67
  // Cap compaction summaries; xAI honours max_output_tokens.
70
68
  Layer.succeed(Compaction.SummarizerTransform, (effect) =>
71
69
  OpenAiLanguageModel.withConfigOverride(effect, {
@@ -98,8 +96,7 @@ export const modelWebSocket = (
98
96
  config: {
99
97
  ...Struct.omit(options ?? {}, [
100
98
  "reasoning",
101
- "systemPromptTransform",
102
- "supportsImages",
99
+ ...AgentModelConfig.optionKeys,
103
100
  ]),
104
101
  // xAI needs stored responses to resolve previous_response_id.
105
102
  store: true,
@@ -110,10 +107,9 @@ export const modelWebSocket = (
110
107
  : { effort: "high", ...options?.reasoning },
111
108
  },
112
109
  }),
113
- AgentModelConfig.layer({
114
- systemPromptTransform: options?.systemPromptTransform,
115
- supportsImages: options?.supportsImages,
116
- }),
110
+ AgentModelConfig.layer(
111
+ Struct.pick(options ?? {}, AgentModelConfig.optionKeys),
112
+ ),
117
113
  // Cap compaction summaries; xAI honours max_output_tokens.
118
114
  Layer.succeed(Compaction.SummarizerTransform, (effect) =>
119
115
  OpenAiLanguageModel.withConfigOverride(effect, {