clanka 0.12.0 → 0.12.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.map +1 -1
- package/dist/Acp.js +10 -0
- package/dist/Acp.js.map +1 -1
- package/dist/Acp.test.js +62 -0
- package/dist/Acp.test.js.map +1 -1
- package/dist/bin/cli.mjs +5 -1
- package/dist/bin/cli.mjs.map +1 -1
- package/package.json +1 -1
- package/src/Acp.test.ts +106 -0
- package/src/Acp.ts +13 -0
package/package.json
CHANGED
package/src/Acp.test.ts
CHANGED
|
@@ -1,13 +1,19 @@
|
|
|
1
1
|
import { assert, describe, it } from "@effect/vitest"
|
|
2
2
|
import * as NodeServices from "@effect/platform-node/NodeServices"
|
|
3
|
+
import type * as Cause from "effect/Cause"
|
|
3
4
|
import * as Context from "effect/Context"
|
|
5
|
+
import * as Deferred from "effect/Deferred"
|
|
4
6
|
import * as Effect from "effect/Effect"
|
|
5
7
|
import * as FileSystem from "effect/FileSystem"
|
|
6
8
|
import * as Fiber from "effect/Fiber"
|
|
7
9
|
import * as Layer from "effect/Layer"
|
|
10
|
+
import * as Logger from "effect/Logger"
|
|
8
11
|
import * as Option from "effect/Option"
|
|
9
12
|
import * as Path from "effect/Path"
|
|
13
|
+
import * as Queue from "effect/Queue"
|
|
10
14
|
import * as Schedule from "effect/Schedule"
|
|
15
|
+
import * as Sink from "effect/Sink"
|
|
16
|
+
import * as Stdio from "effect/Stdio"
|
|
11
17
|
import * as Stream from "effect/Stream"
|
|
12
18
|
import * as FetchHttpClient from "effect/http/FetchHttpClient"
|
|
13
19
|
import * as Prompt from "effect/ai/Prompt"
|
|
@@ -952,6 +958,106 @@ await changeDirectory("sub")`),
|
|
|
952
958
|
),
|
|
953
959
|
),
|
|
954
960
|
)
|
|
961
|
+
|
|
962
|
+
it.effect("reports prompts still running when stdin ends", () =>
|
|
963
|
+
withStore(
|
|
964
|
+
Effect.gen(function* () {
|
|
965
|
+
const modelCalled = yield* Deferred.make<void>()
|
|
966
|
+
const languageModel = yield* LanguageModel.make({
|
|
967
|
+
generateText: () => Effect.succeed([]),
|
|
968
|
+
streamText: () =>
|
|
969
|
+
Stream.fromEffect(Deferred.succeed(modelCalled, undefined)).pipe(
|
|
970
|
+
Stream.drain,
|
|
971
|
+
Stream.concat(Stream.never),
|
|
972
|
+
),
|
|
973
|
+
})
|
|
974
|
+
const modelLayer = Layer.mergeAll(
|
|
975
|
+
Layer.succeed(LanguageModel.LanguageModel, languageModel),
|
|
976
|
+
Layer.succeed(Model.ProviderName, "test"),
|
|
977
|
+
Layer.succeed(Model.ModelName, "model"),
|
|
978
|
+
)
|
|
979
|
+
|
|
980
|
+
const decoder = new TextDecoder()
|
|
981
|
+
const decode = (chunk: string | Uint8Array) =>
|
|
982
|
+
typeof chunk === "string" ? chunk : decoder.decode(chunk)
|
|
983
|
+
const stdin = yield* Queue.unbounded<Uint8Array, Cause.Done>()
|
|
984
|
+
const stdout = yield* Queue.unbounded<string>()
|
|
985
|
+
const stdoutLines: Array<string> = []
|
|
986
|
+
// Both the stderr sink and log output reach stderr in the CLI.
|
|
987
|
+
const stderr: Array<string> = []
|
|
988
|
+
const stdio = Stdio.layerTest({
|
|
989
|
+
stdin: Stream.fromQueue(stdin),
|
|
990
|
+
stdout: () =>
|
|
991
|
+
Sink.forEach((chunk) =>
|
|
992
|
+
Effect.sync(() => {
|
|
993
|
+
for (const line of decode(chunk).split("\n")) {
|
|
994
|
+
if (line === "") continue
|
|
995
|
+
stdoutLines.push(line)
|
|
996
|
+
Queue.offerUnsafe(stdout, line)
|
|
997
|
+
}
|
|
998
|
+
}),
|
|
999
|
+
),
|
|
1000
|
+
stderr: () =>
|
|
1001
|
+
Sink.forEach((chunk) =>
|
|
1002
|
+
Effect.sync(() => {
|
|
1003
|
+
stderr.push(decode(chunk))
|
|
1004
|
+
}),
|
|
1005
|
+
),
|
|
1006
|
+
})
|
|
1007
|
+
const logger = Logger.layer([
|
|
1008
|
+
Logger.make((options) => {
|
|
1009
|
+
stderr.push(Logger.formatSimple.log(options))
|
|
1010
|
+
}),
|
|
1011
|
+
])
|
|
1012
|
+
|
|
1013
|
+
const send = (message: object) =>
|
|
1014
|
+
Queue.offer(
|
|
1015
|
+
stdin,
|
|
1016
|
+
new TextEncoder().encode(
|
|
1017
|
+
`${JSON.stringify({ jsonrpc: "2.0", ...message })}\n`,
|
|
1018
|
+
),
|
|
1019
|
+
)
|
|
1020
|
+
const response = (id: number) =>
|
|
1021
|
+
Queue.take(stdout).pipe(
|
|
1022
|
+
Effect.map((line): Message => JSON.parse(line)),
|
|
1023
|
+
Effect.repeat({ until: (message) => message.id === id }),
|
|
1024
|
+
)
|
|
1025
|
+
|
|
1026
|
+
const server = yield* Acp.runStdio({
|
|
1027
|
+
version: "test",
|
|
1028
|
+
defaultModel: "test:model",
|
|
1029
|
+
defaultThoughtLevel: "medium",
|
|
1030
|
+
makeAgent: () =>
|
|
1031
|
+
Agent.make.pipe(
|
|
1032
|
+
Effect.provideService(AgentExecutor.AgentExecutor, executor),
|
|
1033
|
+
),
|
|
1034
|
+
makeModel: () =>
|
|
1035
|
+
Option.some(
|
|
1036
|
+
Layer.merge(modelLayer, Agent.layerSubagentModel(modelLayer)),
|
|
1037
|
+
),
|
|
1038
|
+
}).pipe(Effect.provide([stdio, logger]), Effect.forkChild)
|
|
1039
|
+
|
|
1040
|
+
yield* send({ id: 1, method: "session/new", params: { cwd: "/tmp" } })
|
|
1041
|
+
const sessionId: string = (yield* response(1)).result.sessionId
|
|
1042
|
+
yield* send({
|
|
1043
|
+
id: 2,
|
|
1044
|
+
method: "session/prompt",
|
|
1045
|
+
params: { sessionId, prompt: [{ type: "text", text: "hello" }] },
|
|
1046
|
+
})
|
|
1047
|
+
yield* Deferred.await(modelCalled)
|
|
1048
|
+
yield* Queue.end(stdin)
|
|
1049
|
+
yield* Fiber.join(server)
|
|
1050
|
+
|
|
1051
|
+
for (const line of stdoutLines) {
|
|
1052
|
+
assert.strictEqual(JSON.parse(line).jsonrpc, "2.0")
|
|
1053
|
+
}
|
|
1054
|
+
assert.isTrue(
|
|
1055
|
+
stderr.some((line) => line.includes(sessionId)),
|
|
1056
|
+
`expected a stderr diagnostic naming session ${sessionId}, got: ${JSON.stringify(stderr)}`,
|
|
1057
|
+
)
|
|
1058
|
+
}),
|
|
1059
|
+
),
|
|
1060
|
+
)
|
|
955
1061
|
})
|
|
956
1062
|
|
|
957
1063
|
// --- MCP servers ---------------------------------------------------------------
|
package/src/Acp.ts
CHANGED
|
@@ -418,6 +418,19 @@ export const make = Effect.fnUntraced(function* <RAgent, RModel>(
|
|
|
418
418
|
const scope = yield* Effect.scope
|
|
419
419
|
const sessions = new Map<string, Session>()
|
|
420
420
|
|
|
421
|
+
// Whatever ends the server, name the prompts it leaves unfinished. An
|
|
422
|
+
// interrupted prompt keeps its `running` fiber.
|
|
423
|
+
yield* Effect.addFinalizer(() => {
|
|
424
|
+
const running = Array.from(sessions.values())
|
|
425
|
+
.filter((session) => session.running !== undefined)
|
|
426
|
+
.map((session) => session.id)
|
|
427
|
+
return running.length === 0
|
|
428
|
+
? Effect.void
|
|
429
|
+
: Effect.logWarning("ACP server stopping with prompts in progress").pipe(
|
|
430
|
+
Effect.annotateLogs({ sessions: running.join(", ") }),
|
|
431
|
+
)
|
|
432
|
+
})
|
|
433
|
+
|
|
421
434
|
const notify = (method: string, params: object) =>
|
|
422
435
|
options.send({ jsonrpc: "2.0", method, params })
|
|
423
436
|
|