clanka 0.25.2 → 0.26.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.
- package/dist/Acp.d.ts +5 -37
- package/dist/Acp.d.ts.map +1 -1
- package/dist/Acp.js +33 -48
- package/dist/Acp.js.map +1 -1
- package/dist/Acp.test.js +62 -1
- package/dist/Acp.test.js.map +1 -1
- package/dist/AcpImage.test.js +3 -2
- package/dist/AcpImage.test.js.map +1 -1
- package/dist/AcpSessionStore.d.ts +114 -0
- package/dist/AcpSessionStore.d.ts.map +1 -0
- package/dist/AcpSessionStore.js +327 -0
- package/dist/AcpSessionStore.js.map +1 -0
- package/dist/AcpSessionStore.test.d.ts +2 -0
- package/dist/AcpSessionStore.test.d.ts.map +1 -0
- package/dist/AcpSessionStore.test.js +135 -0
- package/dist/AcpSessionStore.test.js.map +1 -0
- package/dist/Agent.d.ts.map +1 -1
- package/dist/Agent.js +5 -1
- package/dist/Agent.js.map +1 -1
- package/dist/Agent.test.js +20 -0
- package/dist/Agent.test.js.map +1 -1
- package/dist/ScriptFormatting.d.ts +3 -0
- package/dist/ScriptFormatting.d.ts.map +1 -0
- package/dist/ScriptFormatting.js +61 -0
- package/dist/ScriptFormatting.js.map +1 -0
- package/dist/ScriptFormatting.test.d.ts +2 -0
- package/dist/ScriptFormatting.test.d.ts.map +1 -0
- package/dist/ScriptFormatting.test.js +60 -0
- package/dist/ScriptFormatting.test.js.map +1 -0
- package/dist/bin/{AiError-CsvdOsKR.mjs → AiError-DBaKz35C.mjs} +2 -2
- package/dist/bin/{Latch-B3e9629m.mjs → Latch-DOdbsGMQ.mjs} +1 -1
- package/dist/bin/{McpClient-BCxZZgnA.mjs → McpClient-B-FFG6Pg.mjs} +1 -1
- package/dist/bin/{OutputFormatter-ByMjyBhe.mjs → OutputFormatter-DhhP2i-7.mjs} +2 -2
- package/dist/bin/{Schema-BfUmMefy.mjs → Schema-CFdSO6iH.mjs} +11 -1
- package/dist/bin/{SemanticSearch-DMzIsoZb.mjs → SemanticSearch-C3a7LAQr.mjs} +6 -6
- package/dist/bin/cli.mjs +392 -143
- package/dist/bin/{upgrade-t590zYhO.mjs → upgrade-C4aOEWJH.mjs} +2 -2
- package/dist/cli.js +10 -3
- 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 +120 -1
- package/src/Acp.ts +41 -55
- package/src/AcpImage.test.ts +7 -2
- package/src/AcpSessionStore.test.ts +220 -0
- package/src/AcpSessionStore.ts +455 -0
- package/src/Agent.test.ts +36 -0
- package/src/Agent.ts +5 -1
- package/src/ScriptFormatting.test.ts +66 -0
- package/src/ScriptFormatting.ts +57 -0
- package/src/cli.ts +20 -9
- package/src/index.ts +5 -0
package/src/Agent.test.ts
CHANGED
|
@@ -1961,3 +1961,39 @@ describe("Agent session restarts", () => {
|
|
|
1961
1961
|
}),
|
|
1962
1962
|
)
|
|
1963
1963
|
})
|
|
1964
|
+
|
|
1965
|
+
it.effect(
|
|
1966
|
+
"formats displayed scripts without changing execution or history",
|
|
1967
|
+
() =>
|
|
1968
|
+
Effect.gen(function* () {
|
|
1969
|
+
const script = "const n=1;console.log(n);"
|
|
1970
|
+
const executed: Array<string> = []
|
|
1971
|
+
const result = yield* runAgentCollect({
|
|
1972
|
+
conversationMode: true,
|
|
1973
|
+
executor: makeExecutor(({ script }) => {
|
|
1974
|
+
executed.push(script)
|
|
1975
|
+
return Stream.empty
|
|
1976
|
+
}),
|
|
1977
|
+
respond: (_call, index) =>
|
|
1978
|
+
Stream.fromIterable(
|
|
1979
|
+
index === 0
|
|
1980
|
+
? [toolCall("format", script), finish(10)]
|
|
1981
|
+
: text("done"),
|
|
1982
|
+
),
|
|
1983
|
+
})
|
|
1984
|
+
assert.deepStrictEqual(executed, [script])
|
|
1985
|
+
assert.deepStrictEqual(
|
|
1986
|
+
outputsOfTag(result.outputs, "ScriptDelta").map((part) => part.delta),
|
|
1987
|
+
["const n=1;\nconsole.log(n);"],
|
|
1988
|
+
)
|
|
1989
|
+
const call = result.history.content
|
|
1990
|
+
.flatMap((message) =>
|
|
1991
|
+
message.role === "assistant" ? message.content : [],
|
|
1992
|
+
)
|
|
1993
|
+
.find((part) => part.type === "tool-call")
|
|
1994
|
+
assert.deepStrictEqual(
|
|
1995
|
+
call?.type === "tool-call" ? call.params : undefined,
|
|
1996
|
+
{ script },
|
|
1997
|
+
)
|
|
1998
|
+
}),
|
|
1999
|
+
)
|
package/src/Agent.ts
CHANGED
|
@@ -8,6 +8,7 @@ import * as AgentSkills from "./AgentSkills.ts"
|
|
|
8
8
|
import * as McpServers from "./McpServers.ts"
|
|
9
9
|
import { ToolkitRenderer } from "./ToolkitRenderer.ts"
|
|
10
10
|
import { stripWrappingCodeFence } from "./ScriptExtraction.ts"
|
|
11
|
+
import { formatScript } from "./ScriptFormatting.ts"
|
|
11
12
|
import type * as Path from "effect/Path"
|
|
12
13
|
import type * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"
|
|
13
14
|
import type * as HttpClient from "effect/http/HttpClient"
|
|
@@ -1484,7 +1485,10 @@ ${prompt}`,
|
|
|
1484
1485
|
}).pipe(
|
|
1485
1486
|
Effect.provideService(ScriptExecutor, (script) => {
|
|
1486
1487
|
maybeSend({ agentId, part: new ScriptStart() })
|
|
1487
|
-
maybeSend({
|
|
1488
|
+
maybeSend({
|
|
1489
|
+
agentId,
|
|
1490
|
+
part: new ScriptDelta({ delta: formatScript(script) }),
|
|
1491
|
+
})
|
|
1488
1492
|
return executeScript(script)
|
|
1489
1493
|
}),
|
|
1490
1494
|
Effect.ensuring(Scope.close(runScope, Exit.void)),
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { describe, expect, it } from "@effect/vitest"
|
|
2
|
+
import { formatScript } from "./ScriptFormatting.ts"
|
|
3
|
+
|
|
4
|
+
describe("formatScript", () => {
|
|
5
|
+
it("removes separator spaces only at inserted line breaks", () => {
|
|
6
|
+
const input = [
|
|
7
|
+
"console.log(await ls('.')); console.log(await readFile({path: 'README.md'}));\t console.log(await readFile({path: 'package.json'}));",
|
|
8
|
+
'if (true) { console.log(" literal; "); /* comment; */ console.log(` template; `); }',
|
|
9
|
+
"if (true) {",
|
|
10
|
+
" console.log(1);",
|
|
11
|
+
"\tconsole.log(2);",
|
|
12
|
+
"}",
|
|
13
|
+
].join("\n")
|
|
14
|
+
const expected = [
|
|
15
|
+
"console.log(await ls('.'));",
|
|
16
|
+
"console.log(await readFile({path: 'README.md'}));",
|
|
17
|
+
"console.log(await readFile({path: 'package.json'}));",
|
|
18
|
+
"if (true) {",
|
|
19
|
+
'console.log(" literal; ");',
|
|
20
|
+
"/* comment; */ console.log(` template; `);",
|
|
21
|
+
"}",
|
|
22
|
+
"if (true) {",
|
|
23
|
+
" console.log(1);",
|
|
24
|
+
"\tconsole.log(2);",
|
|
25
|
+
"}",
|
|
26
|
+
].join("\n")
|
|
27
|
+
expect(formatScript(input)).toBe(expected)
|
|
28
|
+
expect(formatScript(expected)).toBe(expected)
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
it("formats scripts larger than the parser default buffer", () => {
|
|
32
|
+
const declaration = `const data="${"x".repeat(40_000)}";`
|
|
33
|
+
expect(formatScript(declaration + "console.log(data);")).toBe(
|
|
34
|
+
declaration + "\nconsole.log(data);",
|
|
35
|
+
)
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it("adds line breaks to compact statements and blocks without expanding for headers", () => {
|
|
39
|
+
const input = "let n=0;for(let i=0;i<2;i++){n+=i;}console.log(n);"
|
|
40
|
+
const output = "let n=0;\nfor(let i=0;i<2;i++){\nn+=i;\n}\nconsole.log(n);"
|
|
41
|
+
expect(formatScript(input)).toBe(output)
|
|
42
|
+
expect(formatScript(output)).toBe(output)
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
it("preserves literals, comments, and line-sensitive expressions", () => {
|
|
46
|
+
const input = [
|
|
47
|
+
`const text="😀;{}";const re=/[;{}]/g;`,
|
|
48
|
+
"const template=`raw;{} ${(() => { return 1; })()}`;",
|
|
49
|
+
"// comment ;{}",
|
|
50
|
+
"const obj={value: 1};/* comment ;{} */obj.value++;",
|
|
51
|
+
"function f(){return {value: 1};}console.log(text, re, template, f());",
|
|
52
|
+
].join("\n")
|
|
53
|
+
const output = formatScript(input)
|
|
54
|
+
expect(output).toContain(`const text="😀;{}";\nconst re=/[;{}]/g;`)
|
|
55
|
+
expect(output).toContain("`raw;{} ${(() => { return 1; })()}`")
|
|
56
|
+
expect(output).toContain("// comment ;{}\n")
|
|
57
|
+
expect(output).toContain("/* comment ;{} */")
|
|
58
|
+
expect(output).toContain("return {value: 1};")
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
it("leaves incomplete scripts and existing whitespace alone", () => {
|
|
62
|
+
expect(formatScript("const x = {;")).toBe("const x = {;")
|
|
63
|
+
const input = "if (true) {\r\n console.log(1);\r\n}\r\n"
|
|
64
|
+
expect(formatScript(input)).toBe(input)
|
|
65
|
+
})
|
|
66
|
+
})
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import TreeSitter, { type SyntaxNode } from "tree-sitter"
|
|
2
|
+
import JavaScript from "tree-sitter-javascript"
|
|
3
|
+
|
|
4
|
+
const parser = new TreeSitter()
|
|
5
|
+
parser.setLanguage(JavaScript)
|
|
6
|
+
|
|
7
|
+
/** Adds display-only line breaks, leaving execution and model history untouched. */
|
|
8
|
+
export const formatScript = (script: string): string => {
|
|
9
|
+
try {
|
|
10
|
+
const tree = parser.parse(script, undefined, {
|
|
11
|
+
// Include room for the UTF-16 terminator, even for large file contents.
|
|
12
|
+
bufferSize: Math.max(32 * 1024, script.length + 1),
|
|
13
|
+
})
|
|
14
|
+
if (tree.rootNode.hasError) return script
|
|
15
|
+
const breaks = new Set<number>()
|
|
16
|
+
const visit = (node: SyntaxNode): void => {
|
|
17
|
+
// Treat literals as opaque, including code inside template substitutions.
|
|
18
|
+
if (
|
|
19
|
+
["string", "template_string", "regex", "comment"].includes(node.type)
|
|
20
|
+
) {
|
|
21
|
+
return
|
|
22
|
+
}
|
|
23
|
+
if (node.type === "for_statement") {
|
|
24
|
+
const body = node.childForFieldName("body")
|
|
25
|
+
if (body) visit(body)
|
|
26
|
+
return
|
|
27
|
+
}
|
|
28
|
+
if (node.type === ";") breaks.add(node.endIndex)
|
|
29
|
+
if (node.type === "statement_block") {
|
|
30
|
+
breaks.add(node.startIndex + 1)
|
|
31
|
+
}
|
|
32
|
+
if (node.type === "program" || node.type === "statement_block") {
|
|
33
|
+
for (const child of node.namedChildren) {
|
|
34
|
+
if (child.type !== "comment") breaks.add(child.endIndex)
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
for (const child of node.children) visit(child)
|
|
38
|
+
}
|
|
39
|
+
visit(tree.rootNode)
|
|
40
|
+
|
|
41
|
+
let output = ""
|
|
42
|
+
let start = 0
|
|
43
|
+
const newline = script.includes("\r\n") ? "\r\n" : "\n"
|
|
44
|
+
for (const index of [...breaks].toSorted((a, b) => a - b)) {
|
|
45
|
+
// Keep existing newlines / indentation, and avoid adding a trailing line.
|
|
46
|
+
const rest = script.slice(index)
|
|
47
|
+
if (/^[ \t]*(?:[\r\n]|$)/.test(rest)) continue
|
|
48
|
+
output += script.slice(start, index) + newline
|
|
49
|
+
start = index
|
|
50
|
+
// Separators on the original line are not indentation for the new one.
|
|
51
|
+
while (script[start] === " " || script[start] === "\t") start++
|
|
52
|
+
}
|
|
53
|
+
return output + script.slice(start)
|
|
54
|
+
} catch {
|
|
55
|
+
return script
|
|
56
|
+
}
|
|
57
|
+
}
|
package/src/cli.ts
CHANGED
|
@@ -9,6 +9,7 @@ import * as NodeServices from "@effect/platform-node/NodeServices"
|
|
|
9
9
|
import * as NodeRuntime from "@effect/platform-node/NodeRuntime"
|
|
10
10
|
import * as GlobalWebSocket from "./GlobalWebSocket.ts"
|
|
11
11
|
import * as Acp from "./Acp.ts"
|
|
12
|
+
import * as AcpSessionStore from "./AcpSessionStore.ts"
|
|
12
13
|
import * as AgentExecutor from "./AgentExecutor.ts"
|
|
13
14
|
import * as Agent from "./Agent.ts"
|
|
14
15
|
import * as Compaction from "./Compaction.ts"
|
|
@@ -340,18 +341,27 @@ const mcpSummary = (
|
|
|
340
341
|
)
|
|
341
342
|
.join(", ")}`
|
|
342
343
|
|
|
344
|
+
const configDirectory = Effect.gen(function* () {
|
|
345
|
+
const path = yield* Path.Path
|
|
346
|
+
const configHome = yield* Config.NonEmptyString("XDG_CONFIG_HOME").pipe(
|
|
347
|
+
Config.orElse(() =>
|
|
348
|
+
Config.NonEmptyString("HOME").pipe(
|
|
349
|
+
Config.map((home) => path.join(home, ".config")),
|
|
350
|
+
),
|
|
351
|
+
),
|
|
352
|
+
)
|
|
353
|
+
return path.join(configHome, "clanka")
|
|
354
|
+
})
|
|
355
|
+
|
|
343
356
|
const Kvs = Layer.unwrap(
|
|
357
|
+
Effect.map(configDirectory, KeyValueStore.layerFileSystem),
|
|
358
|
+
).pipe(Layer.provide(NodeServices.layer))
|
|
359
|
+
|
|
360
|
+
const AcpSessions = Layer.unwrap(
|
|
344
361
|
Effect.gen(function* () {
|
|
345
362
|
const path = yield* Path.Path
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
Config.orElse(() =>
|
|
349
|
-
Config.NonEmptyString("HOME").pipe(
|
|
350
|
-
Config.map((home) => path.join(home, ".config")),
|
|
351
|
-
),
|
|
352
|
-
),
|
|
353
|
-
)
|
|
354
|
-
return KeyValueStore.layerFileSystem(path.join(configHome, "clanka"))
|
|
363
|
+
const directory = yield* configDirectory
|
|
364
|
+
return AcpSessionStore.layerFileSystem(path.join(directory, "sessions"))
|
|
355
365
|
}),
|
|
356
366
|
).pipe(Layer.provide(NodeServices.layer))
|
|
357
367
|
|
|
@@ -477,6 +487,7 @@ const acp = Command.make("acp", {
|
|
|
477
487
|
),
|
|
478
488
|
Command.provide((flags) =>
|
|
479
489
|
Layer.mergeAll(
|
|
490
|
+
AcpSessions,
|
|
480
491
|
Agent.ConversationMode.layer(false),
|
|
481
492
|
Layer.succeed(Logger.LogToStderr, true),
|
|
482
493
|
DeviceCodeHandler.layerLog,
|