clanka 0.26.1 → 0.28.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/README.md +2 -3
- package/dist/Acp.test.js +0 -1
- package/dist/Acp.test.js.map +1 -1
- package/dist/AcpImage.test.js +0 -1
- package/dist/AcpImage.test.js.map +1 -1
- package/dist/Agent.d.ts.map +1 -1
- package/dist/Agent.js +1 -2
- package/dist/Agent.js.map +1 -1
- package/dist/Agent.test.js +0 -1
- package/dist/Agent.test.js.map +1 -1
- package/dist/AgentCallerConfig.test.js +0 -1
- package/dist/AgentCallerConfig.test.js.map +1 -1
- package/dist/AgentExecutor.d.ts +2 -3
- package/dist/AgentExecutor.d.ts.map +1 -1
- package/dist/AgentExecutor.js +4 -13
- package/dist/AgentExecutor.js.map +1 -1
- package/dist/AgentImage.test.js +0 -1
- package/dist/AgentImage.test.js.map +1 -1
- package/dist/AgentSkills.test.js +0 -2
- package/dist/AgentSkills.test.js.map +1 -1
- package/dist/AgentTools.d.ts +2 -235
- package/dist/AgentTools.d.ts.map +1 -1
- package/dist/AgentTools.js +2 -32
- package/dist/AgentTools.js.map +1 -1
- package/dist/ClaudeAgent.test.js +0 -1
- package/dist/ClaudeAgent.test.js.map +1 -1
- package/dist/McpServers.d.ts +20 -1
- package/dist/McpServers.d.ts.map +1 -1
- package/dist/McpServers.js +12 -5
- package/dist/McpServers.js.map +1 -1
- package/dist/McpServers.test.js +115 -0
- package/dist/McpServers.test.js.map +1 -1
- package/dist/ScriptFormatting.d.ts +5 -0
- package/dist/ScriptFormatting.d.ts.map +1 -1
- package/dist/ScriptFormatting.js +255 -53
- package/dist/ScriptFormatting.js.map +1 -1
- package/dist/ScriptFormatting.test.js +16 -0
- package/dist/ScriptFormatting.test.js.map +1 -1
- package/dist/bin/{Latch-DOdbsGMQ.mjs → Latch-BE6_TYRL.mjs} +1 -1
- package/dist/bin/{McpClient-B-FFG6Pg.mjs → McpClient-DCt4v5W5.mjs} +2 -2
- package/dist/bin/{OutputFormatter-DhhP2i-7.mjs → OutputFormatter-DaFv_C_I.mjs} +2 -3
- package/dist/bin/{Schema-CFdSO6iH.mjs → Schema-B3MiG1aD.mjs} +204 -568
- package/dist/bin/cli.mjs +5098 -2472
- package/dist/bin/{upgrade-C4aOEWJH.mjs → upgrade-WqgIU_21.mjs} +3 -3
- package/dist/cli.js +2 -25
- package/dist/cli.js.map +1 -1
- package/dist/fixtures/McpFakeServer.d.ts +4 -0
- package/dist/fixtures/McpFakeServer.d.ts.map +1 -1
- package/dist/fixtures/McpFakeServer.js +13 -0
- package/dist/fixtures/McpFakeServer.js.map +1 -1
- package/dist/index.d.ts +0 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +0 -8
- package/dist/index.js.map +1 -1
- package/package.json +1 -7
- package/src/Acp.test.ts +0 -1
- package/src/AcpImage.test.ts +0 -1
- package/src/Agent.test.ts +0 -1
- package/src/Agent.ts +0 -4
- package/src/AgentCallerConfig.test.ts +0 -1
- package/src/AgentExecutor.ts +4 -16
- package/src/AgentImage.test.ts +0 -1
- package/src/AgentSkills.test.ts +0 -2
- package/src/AgentTools.ts +4 -41
- package/src/ClaudeAgent.test.ts +0 -1
- package/src/McpServers.test.ts +198 -0
- package/src/McpServers.ts +44 -4
- package/src/ScriptFormatting.test.ts +20 -0
- package/src/ScriptFormatting.ts +234 -47
- package/src/cli.ts +1 -56
- package/src/fixtures/McpFakeServer.ts +17 -1
- package/src/index.ts +0 -10
- package/dist/ChunkRepo.d.ts +0 -173
- package/dist/ChunkRepo.d.ts.map +0 -1
- package/dist/ChunkRepo.js +0 -168
- package/dist/ChunkRepo.js.map +0 -1
- package/dist/ChunkRepo.test.d.ts +0 -2
- package/dist/ChunkRepo.test.d.ts.map +0 -1
- package/dist/ChunkRepo.test.js +0 -63
- package/dist/ChunkRepo.test.js.map +0 -1
- package/dist/CodeChunker.d.ts +0 -84
- package/dist/CodeChunker.d.ts.map +0 -1
- package/dist/CodeChunker.js +0 -658
- package/dist/CodeChunker.js.map +0 -1
- package/dist/CodeChunker.test.d.ts +0 -2
- package/dist/CodeChunker.test.d.ts.map +0 -1
- package/dist/CodeChunker.test.js +0 -434
- package/dist/CodeChunker.test.js.map +0 -1
- package/dist/SemanticSearch/Service.d.ts +0 -31
- package/dist/SemanticSearch/Service.d.ts.map +0 -1
- package/dist/SemanticSearch/Service.js +0 -29
- package/dist/SemanticSearch/Service.js.map +0 -1
- package/dist/SemanticSearch.d.ts +0 -34
- package/dist/SemanticSearch.d.ts.map +0 -1
- package/dist/SemanticSearch.js +0 -171
- package/dist/SemanticSearch.js.map +0 -1
- package/dist/SemanticSearch.test.d.ts +0 -2
- package/dist/SemanticSearch.test.d.ts.map +0 -1
- package/dist/SemanticSearch.test.js +0 -34
- package/dist/SemanticSearch.test.js.map +0 -1
- package/dist/Sqlite.d.ts +0 -15
- package/dist/Sqlite.d.ts.map +0 -1
- package/dist/Sqlite.js +0 -44
- package/dist/Sqlite.js.map +0 -1
- package/dist/bin/AiError-DBaKz35C.mjs +0 -2723
- package/dist/bin/SemanticSearch-C3a7LAQr.mjs +0 -2558
- package/src/ChunkRepo.test.ts +0 -89
- package/src/ChunkRepo.ts +0 -260
- package/src/CodeChunker.test.ts +0 -493
- package/src/CodeChunker.ts +0 -1018
- package/src/SemanticSearch/Service.ts +0 -50
- package/src/SemanticSearch.test.ts +0 -51
- package/src/SemanticSearch.ts +0 -282
- package/src/Sqlite.ts +0 -60
package/src/McpServers.test.ts
CHANGED
|
@@ -21,6 +21,7 @@ import * as Toolkit from "effect/ai/Toolkit"
|
|
|
21
21
|
import * as RpcClient from "effect/rpc/RpcClient"
|
|
22
22
|
import * as RpcSerialization from "effect/rpc/RpcSerialization"
|
|
23
23
|
import * as RpcServer from "effect/rpc/RpcServer"
|
|
24
|
+
import type { OAuthClientProvider } from "@modelcontextprotocol/sdk/client/auth.js"
|
|
24
25
|
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js"
|
|
25
26
|
import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js"
|
|
26
27
|
import type { Transport } from "@modelcontextprotocol/sdk/shared/transport.js"
|
|
@@ -341,6 +342,33 @@ await taskComplete(JSON.stringify({ first, second: await attempt(mcp.remote.ping
|
|
|
341
342
|
)
|
|
342
343
|
}).pipe(Effect.scoped)
|
|
343
344
|
|
|
345
|
+
// An OAuth provider holding `token`, if any. Starting a login records the
|
|
346
|
+
// authorization URL and throws `loginError`, as a caller that cannot open a
|
|
347
|
+
// browser does.
|
|
348
|
+
const oauth = (options: {
|
|
349
|
+
readonly token?: string
|
|
350
|
+
readonly loginError: string
|
|
351
|
+
}) => {
|
|
352
|
+
const logins: Array<URL> = []
|
|
353
|
+
const provider: OAuthClientProvider = {
|
|
354
|
+
redirectUrl: "http://127.0.0.1/callback",
|
|
355
|
+
clientMetadata: { redirect_uris: ["http://127.0.0.1/callback"] },
|
|
356
|
+
clientInformation: () => ({ client_id: "clanka-test" }),
|
|
357
|
+
tokens: () =>
|
|
358
|
+
options.token === undefined
|
|
359
|
+
? undefined
|
|
360
|
+
: { access_token: options.token, token_type: "Bearer" },
|
|
361
|
+
saveTokens: () => {},
|
|
362
|
+
saveCodeVerifier: () => {},
|
|
363
|
+
codeVerifier: () => "verifier",
|
|
364
|
+
redirectToAuthorization: (url) => {
|
|
365
|
+
logins.push(url)
|
|
366
|
+
throw new Error(options.loginError)
|
|
367
|
+
},
|
|
368
|
+
}
|
|
369
|
+
return { provider, logins }
|
|
370
|
+
}
|
|
371
|
+
|
|
344
372
|
// --- lifecycle and prompt ----------------------------------------------------
|
|
345
373
|
|
|
346
374
|
describe("McpServers", () => {
|
|
@@ -1047,4 +1075,174 @@ console.log(await mcp.fake.screenshot({}))`
|
|
|
1047
1075
|
}
|
|
1048
1076
|
}).pipe(Effect.scoped),
|
|
1049
1077
|
)
|
|
1078
|
+
|
|
1079
|
+
// --- OAuth -------------------------------------------------------------------
|
|
1080
|
+
|
|
1081
|
+
it.live("http servers are sent the OAuth provider's token", () =>
|
|
1082
|
+
Effect.gen(function* () {
|
|
1083
|
+
const remote = yield* Effect.acquireRelease(
|
|
1084
|
+
Effect.promise(() =>
|
|
1085
|
+
listenHttp(makeFakeServer({ tools: named("ping") }), "/mcp", {
|
|
1086
|
+
authorize: (header) => header === "Bearer good",
|
|
1087
|
+
}),
|
|
1088
|
+
),
|
|
1089
|
+
(remote) => Effect.promise(remote.close),
|
|
1090
|
+
)
|
|
1091
|
+
const { provider, logins } = oauth({
|
|
1092
|
+
token: "good",
|
|
1093
|
+
loginError: "login required",
|
|
1094
|
+
})
|
|
1095
|
+
const asked: Array<string> = []
|
|
1096
|
+
const { summary, status } = yield* Effect.gen(function* () {
|
|
1097
|
+
const { summary } = yield* runAgent({
|
|
1098
|
+
script: "await taskComplete(await mcp.remote.ping({}))",
|
|
1099
|
+
})
|
|
1100
|
+
return { summary, status: yield* currentStatus }
|
|
1101
|
+
}).pipe(
|
|
1102
|
+
Effect.provide(
|
|
1103
|
+
McpServers.layer(
|
|
1104
|
+
{ remote: { type: "http", url: remote.url } },
|
|
1105
|
+
(name, config) => {
|
|
1106
|
+
asked.push(`${name} ${config.url}`)
|
|
1107
|
+
return provider
|
|
1108
|
+
},
|
|
1109
|
+
),
|
|
1110
|
+
),
|
|
1111
|
+
)
|
|
1112
|
+
assert.strictEqual(summary, "ping")
|
|
1113
|
+
assert.strictEqual(status["remote"]?._tag, "Connected")
|
|
1114
|
+
assert.deepStrictEqual(asked, [`remote ${remote.url}`])
|
|
1115
|
+
assert.isEmpty(logins)
|
|
1116
|
+
assert.isNotEmpty(remote.authorization)
|
|
1117
|
+
for (const header of remote.authorization) {
|
|
1118
|
+
assert.strictEqual(header, "Bearer good")
|
|
1119
|
+
}
|
|
1120
|
+
}).pipe(Effect.scoped),
|
|
1121
|
+
)
|
|
1122
|
+
|
|
1123
|
+
it.live(
|
|
1124
|
+
"a login the provider cannot start fails the server with its error",
|
|
1125
|
+
() =>
|
|
1126
|
+
Effect.gen(function* () {
|
|
1127
|
+
const remote = yield* Effect.acquireRelease(
|
|
1128
|
+
Effect.promise(() =>
|
|
1129
|
+
listenHttp(makeFakeServer({ tools: named("ping") }), "/mcp", {
|
|
1130
|
+
authorize: () => false,
|
|
1131
|
+
}),
|
|
1132
|
+
),
|
|
1133
|
+
(remote) => Effect.promise(remote.close),
|
|
1134
|
+
)
|
|
1135
|
+
const { provider, logins } = oauth({
|
|
1136
|
+
loginError: "remote needs login\nopen the browser to continue",
|
|
1137
|
+
})
|
|
1138
|
+
const { system, status } = yield* Effect.gen(function* () {
|
|
1139
|
+
const { system } = yield* runAgent({
|
|
1140
|
+
script: "await taskComplete(typeof mcp)",
|
|
1141
|
+
})
|
|
1142
|
+
return { system, status: yield* currentStatus }
|
|
1143
|
+
}).pipe(
|
|
1144
|
+
Effect.provide(
|
|
1145
|
+
McpServers.layer(
|
|
1146
|
+
{ remote: { type: "http", url: remote.url } },
|
|
1147
|
+
() => provider,
|
|
1148
|
+
),
|
|
1149
|
+
),
|
|
1150
|
+
)
|
|
1151
|
+
assert.strictEqual(
|
|
1152
|
+
failure(status, "remote"),
|
|
1153
|
+
"remote needs login\nopen the browser to continue",
|
|
1154
|
+
)
|
|
1155
|
+
// The prompt shows the first line.
|
|
1156
|
+
assert.include(
|
|
1157
|
+
system,
|
|
1158
|
+
"- remote: failed to connect: remote needs login",
|
|
1159
|
+
)
|
|
1160
|
+
assert.notInclude(system, "open the browser")
|
|
1161
|
+
assert.strictEqual(logins.length, 1)
|
|
1162
|
+
assert.strictEqual(logins[0]!.pathname, "/authorize")
|
|
1163
|
+
}).pipe(Effect.scoped),
|
|
1164
|
+
)
|
|
1165
|
+
|
|
1166
|
+
it.live(
|
|
1167
|
+
"a token rejected during a call fails the call with the provider's error",
|
|
1168
|
+
() =>
|
|
1169
|
+
Effect.gen(function* () {
|
|
1170
|
+
let revoked = false
|
|
1171
|
+
const remote = yield* Effect.acquireRelease(
|
|
1172
|
+
Effect.promise(() =>
|
|
1173
|
+
listenHttp(
|
|
1174
|
+
makeFakeServer({
|
|
1175
|
+
tools: [
|
|
1176
|
+
{ name: "ping", call: () => text("pong") },
|
|
1177
|
+
{
|
|
1178
|
+
name: "revoke",
|
|
1179
|
+
call: () => {
|
|
1180
|
+
revoked = true
|
|
1181
|
+
return text("revoked")
|
|
1182
|
+
},
|
|
1183
|
+
},
|
|
1184
|
+
],
|
|
1185
|
+
}),
|
|
1186
|
+
"/mcp",
|
|
1187
|
+
{ authorize: (header) => header === "Bearer good" && !revoked },
|
|
1188
|
+
),
|
|
1189
|
+
),
|
|
1190
|
+
(remote) => Effect.promise(remote.close),
|
|
1191
|
+
)
|
|
1192
|
+
const other = yield* Effect.acquireRelease(
|
|
1193
|
+
Effect.promise(() =>
|
|
1194
|
+
listenHttp(makeFakeServer({ tools: named("ping") })),
|
|
1195
|
+
),
|
|
1196
|
+
(other) => Effect.promise(other.close),
|
|
1197
|
+
)
|
|
1198
|
+
const { provider, logins } = oauth({
|
|
1199
|
+
token: "good",
|
|
1200
|
+
loginError: "remote needs login",
|
|
1201
|
+
})
|
|
1202
|
+
const { summary, status } = yield* Effect.gen(function* () {
|
|
1203
|
+
const { summary } = yield* runAgent({
|
|
1204
|
+
script: `${attempt}
|
|
1205
|
+
await mcp.remote.revoke({})
|
|
1206
|
+
const remote = await attempt(mcp.remote.ping({}))
|
|
1207
|
+
await taskComplete(JSON.stringify({ remote, other: await attempt(mcp.other.ping({})) }))`,
|
|
1208
|
+
})
|
|
1209
|
+
return { summary, status: yield* currentStatus }
|
|
1210
|
+
}).pipe(
|
|
1211
|
+
Effect.provide(
|
|
1212
|
+
McpServers.layer(
|
|
1213
|
+
{
|
|
1214
|
+
remote: { type: "http", url: remote.url },
|
|
1215
|
+
other: { type: "http", url: other.url },
|
|
1216
|
+
},
|
|
1217
|
+
(name) => (name === "remote" ? provider : undefined),
|
|
1218
|
+
),
|
|
1219
|
+
),
|
|
1220
|
+
)
|
|
1221
|
+
assert.deepStrictEqual(JSON.parse(summary), {
|
|
1222
|
+
remote: "ERR mcp.remote.ping: remote needs login",
|
|
1223
|
+
other: "ping",
|
|
1224
|
+
})
|
|
1225
|
+
assert.strictEqual(logins.length, 1)
|
|
1226
|
+
assert.strictEqual(status["other"]?._tag, "Connected")
|
|
1227
|
+
}).pipe(Effect.scoped),
|
|
1228
|
+
)
|
|
1229
|
+
|
|
1230
|
+
it.live("without an OAuth provider a 401 fails the server", () =>
|
|
1231
|
+
Effect.gen(function* () {
|
|
1232
|
+
const remote = yield* Effect.acquireRelease(
|
|
1233
|
+
Effect.promise(() =>
|
|
1234
|
+
listenHttp(makeFakeServer({ tools: named("ping") }), "/mcp", {
|
|
1235
|
+
authorize: () => false,
|
|
1236
|
+
}),
|
|
1237
|
+
),
|
|
1238
|
+
(remote) => Effect.promise(remote.close),
|
|
1239
|
+
)
|
|
1240
|
+
const status = yield* currentStatus.pipe(
|
|
1241
|
+
Effect.provide(
|
|
1242
|
+
McpServers.layer({ remote: { type: "http", url: remote.url } }),
|
|
1243
|
+
),
|
|
1244
|
+
)
|
|
1245
|
+
assert.include(failure(status, "remote"), "Streamable HTTP error")
|
|
1246
|
+
}).pipe(Effect.scoped),
|
|
1247
|
+
)
|
|
1050
1248
|
})
|
package/src/McpServers.ts
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
*/
|
|
10
10
|
import * as NodePath from "node:path"
|
|
11
11
|
import type { Readable } from "node:stream"
|
|
12
|
+
import type { OAuthClientProvider } from "@modelcontextprotocol/sdk/client/auth.js"
|
|
12
13
|
import { Client } from "@modelcontextprotocol/sdk/client/index.js"
|
|
13
14
|
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js"
|
|
14
15
|
import {
|
|
@@ -86,6 +87,26 @@ export const HttpServerConfig = Schema.Struct({
|
|
|
86
87
|
callTimeout: Schema.optionalKey(Timeout),
|
|
87
88
|
})
|
|
88
89
|
|
|
90
|
+
/**
|
|
91
|
+
* @since 1.0.0
|
|
92
|
+
* @category Config
|
|
93
|
+
*/
|
|
94
|
+
export type HttpServerConfig = typeof HttpServerConfig.Type
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Chooses the OAuth provider for an HTTP server, or none. The provider is
|
|
98
|
+
* passed to the SDK transport, which sends its token and calls it when the
|
|
99
|
+
* server answers 401. An error the provider throws fails the connection or
|
|
100
|
+
* the tool call with its message.
|
|
101
|
+
*
|
|
102
|
+
* @since 1.0.0
|
|
103
|
+
* @category Config
|
|
104
|
+
*/
|
|
105
|
+
export type AuthProviderFor = (
|
|
106
|
+
name: string,
|
|
107
|
+
config: HttpServerConfig,
|
|
108
|
+
) => OAuthClientProvider | undefined
|
|
109
|
+
|
|
89
110
|
/**
|
|
90
111
|
* @since 1.0.0
|
|
91
112
|
* @category Config
|
|
@@ -202,14 +223,20 @@ export class McpServers extends Context.Service<
|
|
|
202
223
|
* Connects every server when built and closes them with its scope. A server
|
|
203
224
|
* that cannot connect is `Failed`; building the layer never fails.
|
|
204
225
|
*
|
|
226
|
+
* `authProvider` is called once for each HTTP server, when the layer is
|
|
227
|
+
* built.
|
|
228
|
+
*
|
|
205
229
|
* @since 1.0.0
|
|
206
230
|
* @category Layers
|
|
207
231
|
*/
|
|
208
232
|
export const layer = (
|
|
209
233
|
config: Readonly<Record<string, ServerEntry>>,
|
|
234
|
+
authProvider?: AuthProviderFor | undefined,
|
|
210
235
|
): Layer.Layer<McpServers> =>
|
|
211
236
|
fromEntries(
|
|
212
|
-
Object.entries(config).map(([name, entry]) =>
|
|
237
|
+
Object.entries(config).map(([name, entry]) =>
|
|
238
|
+
toEntry(name, entry, authProvider),
|
|
239
|
+
),
|
|
213
240
|
)
|
|
214
241
|
|
|
215
242
|
/**
|
|
@@ -318,7 +345,11 @@ type Entry =
|
|
|
318
345
|
}
|
|
319
346
|
| { readonly name: string; readonly error: string }
|
|
320
347
|
|
|
321
|
-
const toEntry = (
|
|
348
|
+
const toEntry = (
|
|
349
|
+
name: string,
|
|
350
|
+
entry: ServerEntry,
|
|
351
|
+
authProvider: AuthProviderFor | undefined,
|
|
352
|
+
): Entry => {
|
|
322
353
|
switch (entry.type) {
|
|
323
354
|
case "unavailable":
|
|
324
355
|
return { name, error: entry.error }
|
|
@@ -348,6 +379,7 @@ const toEntry = (name: string, entry: ServerEntry): Entry => {
|
|
|
348
379
|
transport: new HttpTransport(
|
|
349
380
|
new URL(entry.url),
|
|
350
381
|
unwrapRecord(entry.headers),
|
|
382
|
+
authProvider?.(name, entry),
|
|
351
383
|
) as Transport,
|
|
352
384
|
stderr: undefined,
|
|
353
385
|
}),
|
|
@@ -365,7 +397,11 @@ const toEntry = (name: string, entry: ServerEntry): Entry => {
|
|
|
365
397
|
class HttpTransport extends StreamableHTTPClientTransport {
|
|
366
398
|
readonly reconnection: { maxRetries: number }
|
|
367
399
|
|
|
368
|
-
constructor(
|
|
400
|
+
constructor(
|
|
401
|
+
url: URL,
|
|
402
|
+
headers: Record<string, string>,
|
|
403
|
+
authProvider: OAuthClientProvider | undefined,
|
|
404
|
+
) {
|
|
369
405
|
// The SDK defaults.
|
|
370
406
|
const reconnection = {
|
|
371
407
|
initialReconnectionDelay: 1000,
|
|
@@ -373,7 +409,11 @@ class HttpTransport extends StreamableHTTPClientTransport {
|
|
|
373
409
|
reconnectionDelayGrowFactor: 1.5,
|
|
374
410
|
maxRetries: 2,
|
|
375
411
|
}
|
|
376
|
-
super(url, {
|
|
412
|
+
super(url, {
|
|
413
|
+
requestInit: { headers },
|
|
414
|
+
reconnectionOptions: reconnection,
|
|
415
|
+
...(authProvider === undefined ? {} : { authProvider }),
|
|
416
|
+
})
|
|
377
417
|
this.reconnection = reconnection
|
|
378
418
|
}
|
|
379
419
|
|
|
@@ -42,6 +42,14 @@ describe("formatScript", () => {
|
|
|
42
42
|
expect(formatScript(output)).toBe(output)
|
|
43
43
|
})
|
|
44
44
|
|
|
45
|
+
it("breaks statement blocks but not the expressions around them", () => {
|
|
46
|
+
const input = "const a=b/2;if(a){x()}else{y()}run(function(){go();});"
|
|
47
|
+
const output =
|
|
48
|
+
"const a=b/2;\nif(a){\nx()\n}else{\ny()\n}\nrun(function(){\ngo();\n});"
|
|
49
|
+
expect(formatScript(input)).toBe(output)
|
|
50
|
+
expect(formatScript(output)).toBe(output)
|
|
51
|
+
})
|
|
52
|
+
|
|
45
53
|
it("preserves literals, comments, and line-sensitive expressions", () => {
|
|
46
54
|
const input = [
|
|
47
55
|
`const text="😀;{}";const re=/[;{}]/g;`,
|
|
@@ -56,10 +64,22 @@ describe("formatScript", () => {
|
|
|
56
64
|
expect(output).toContain("// comment ;{}\n")
|
|
57
65
|
expect(output).toContain("/* comment ;{} */")
|
|
58
66
|
expect(output).toContain("return {value: 1};")
|
|
67
|
+
expect(formatScript("n=a.in/2;re=/x;y/;")).toBe("n=a.in/2;\nre=/x;y/;")
|
|
68
|
+
// A slash after ")" may start a regex or divide, so nothing is changed.
|
|
69
|
+
for (const input of [
|
|
70
|
+
"if (a) /x;y/.test(s);n=1;",
|
|
71
|
+
"for await (const x of []) /x;y/.test(s);n=1;",
|
|
72
|
+
"n=obj.with()/2;re=/x;y/;",
|
|
73
|
+
]) {
|
|
74
|
+
expect(formatScript(input)).toBe(input)
|
|
75
|
+
}
|
|
59
76
|
})
|
|
60
77
|
|
|
61
78
|
it("leaves incomplete scripts and existing whitespace alone", () => {
|
|
62
79
|
expect(formatScript("const x = {;")).toBe("const x = {;")
|
|
80
|
+
expect(formatScript("const x=1; /* unfinished")).toBe(
|
|
81
|
+
"const x=1; /* unfinished",
|
|
82
|
+
)
|
|
63
83
|
const input = "if (true) {\r\n console.log(1);\r\n}\r\n"
|
|
64
84
|
expect(formatScript(input)).toBe(input)
|
|
65
85
|
})
|
package/src/ScriptFormatting.ts
CHANGED
|
@@ -1,57 +1,244 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Display-only line breaks for model-generated scripts. A small scanner finds
|
|
3
|
+
* statement boundaries, skipping literals and comments, so no parser is
|
|
4
|
+
* needed. Scripts it cannot scan are returned unchanged.
|
|
5
|
+
*/
|
|
3
6
|
|
|
4
|
-
|
|
5
|
-
|
|
7
|
+
type Frame =
|
|
8
|
+
| { readonly type: "block"; readonly start: number; readonly opener: string }
|
|
9
|
+
| { readonly type: "brace" | "paren" | "bracket" | "template" | "subst" }
|
|
6
10
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
11
|
+
// Tokens after which `{` opens a statement block rather than an expression.
|
|
12
|
+
const blockOpeners = new Set([
|
|
13
|
+
"",
|
|
14
|
+
";",
|
|
15
|
+
"{",
|
|
16
|
+
"}",
|
|
17
|
+
")",
|
|
18
|
+
"=>",
|
|
19
|
+
"else",
|
|
20
|
+
"try",
|
|
21
|
+
"finally",
|
|
22
|
+
"do",
|
|
23
|
+
])
|
|
24
|
+
|
|
25
|
+
// Tokens after which `/` starts a regular expression rather than a division.
|
|
26
|
+
const regexKeywords = new Set([
|
|
27
|
+
"return",
|
|
28
|
+
"typeof",
|
|
29
|
+
"instanceof",
|
|
30
|
+
"in",
|
|
31
|
+
"of",
|
|
32
|
+
"new",
|
|
33
|
+
"delete",
|
|
34
|
+
"void",
|
|
35
|
+
"throw",
|
|
36
|
+
"case",
|
|
37
|
+
"do",
|
|
38
|
+
"else",
|
|
39
|
+
"yield",
|
|
40
|
+
"await",
|
|
41
|
+
])
|
|
42
|
+
|
|
43
|
+
// Words that continue the statement a block belongs to.
|
|
44
|
+
const continuations = new Set([
|
|
45
|
+
"else",
|
|
46
|
+
"catch",
|
|
47
|
+
"finally",
|
|
48
|
+
"instanceof",
|
|
49
|
+
"in",
|
|
50
|
+
"as",
|
|
51
|
+
"satisfies",
|
|
52
|
+
])
|
|
53
|
+
|
|
54
|
+
const isWord = (char: string) => /[\w$]/.test(char) || char.charCodeAt(0) > 127
|
|
55
|
+
|
|
56
|
+
const isSpace = (char: string) => /\s/.test(char)
|
|
57
|
+
|
|
58
|
+
// `prev` is "" at the start, a punctuator, a word, "id" for a property name,
|
|
59
|
+
// or "str" for a literal. After ")" a slash is not decided here: see below.
|
|
60
|
+
const regexAllowed = (prev: string) =>
|
|
61
|
+
prev === "" ||
|
|
62
|
+
regexKeywords.has(prev) ||
|
|
63
|
+
(!isWord(prev[0]!) && !["]", "}o", "++", "--"].includes(prev))
|
|
64
|
+
|
|
65
|
+
/** Returns the positions of statement boundaries, or undefined if the script cannot be scanned. */
|
|
66
|
+
const findBreaks = (script: string): Set<number> | undefined => {
|
|
67
|
+
const breaks = new Set<number>()
|
|
68
|
+
const stack: Array<Frame> = []
|
|
69
|
+
// Inside a template literal, including its substitutions, nothing is broken.
|
|
70
|
+
let opaque = 0
|
|
71
|
+
let prev = ""
|
|
72
|
+
let prevEnd = 0
|
|
73
|
+
const top = () => stack.at(-1)?.type
|
|
74
|
+
const inStatements = () => {
|
|
75
|
+
const type = top()
|
|
76
|
+
return type === undefined || type === "block" || type === "brace"
|
|
77
|
+
}
|
|
78
|
+
// Skips whitespace and comments, or returns undefined for an unterminated comment.
|
|
79
|
+
const skipTrivia = (from: number): number | undefined => {
|
|
80
|
+
let i = from
|
|
81
|
+
while (i < script.length) {
|
|
82
|
+
if (isSpace(script[i]!)) i++
|
|
83
|
+
else if (script.startsWith("//", i)) {
|
|
84
|
+
const end = script.indexOf("\n", i)
|
|
85
|
+
i = end === -1 ? script.length : end
|
|
86
|
+
} else if (script.startsWith("/*", i)) {
|
|
87
|
+
const end = script.indexOf("*/", i + 2)
|
|
88
|
+
if (end === -1) return undefined
|
|
89
|
+
i = end + 2
|
|
90
|
+
} else break
|
|
91
|
+
}
|
|
92
|
+
return i
|
|
93
|
+
}
|
|
94
|
+
const nextToken = (from: number): string => {
|
|
95
|
+
const i = skipTrivia(from)
|
|
96
|
+
if (i === undefined || i >= script.length) return ""
|
|
97
|
+
if (!isWord(script[i]!)) return script[i]!
|
|
98
|
+
let end = i
|
|
99
|
+
while (end < script.length && isWord(script[end]!)) end++
|
|
100
|
+
return script.slice(i, end)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
let i = 0
|
|
104
|
+
while (i < script.length) {
|
|
105
|
+
const char = script[i]!
|
|
106
|
+
if (top() === "template") {
|
|
107
|
+
if (char === "\\") i += 2
|
|
108
|
+
else if (char === "`") {
|
|
109
|
+
stack.pop()
|
|
110
|
+
opaque--
|
|
111
|
+
prev = "str"
|
|
112
|
+
prevEnd = ++i
|
|
113
|
+
} else if (script.startsWith("${", i)) {
|
|
114
|
+
stack.push({ type: "subst" })
|
|
115
|
+
prev = "("
|
|
116
|
+
i += 2
|
|
117
|
+
} else i++
|
|
118
|
+
continue
|
|
119
|
+
}
|
|
120
|
+
if (
|
|
121
|
+
isSpace(char) ||
|
|
122
|
+
script.startsWith("//", i) ||
|
|
123
|
+
script.startsWith("/*", i)
|
|
124
|
+
) {
|
|
125
|
+
const end = skipTrivia(i)
|
|
126
|
+
if (end === undefined) return undefined
|
|
127
|
+
i = end
|
|
128
|
+
continue
|
|
129
|
+
}
|
|
130
|
+
const start = i
|
|
131
|
+
if (char === '"' || char === "'") {
|
|
132
|
+
i++
|
|
133
|
+
while (script[i] !== char) {
|
|
134
|
+
if (i >= script.length || script[i] === "\n") return undefined
|
|
135
|
+
i += script[i] === "\\" ? 2 : 1
|
|
22
136
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
137
|
+
i++
|
|
138
|
+
prev = "str"
|
|
139
|
+
} else if (char === "`") {
|
|
140
|
+
stack.push({ type: "template" })
|
|
141
|
+
opaque++
|
|
142
|
+
i++
|
|
143
|
+
continue
|
|
144
|
+
} else if (char === "/" && prev === ")") {
|
|
145
|
+
// After a control header such as `if (x)` it starts a regex, after a
|
|
146
|
+
// call or a parenthesized expression it divides. Telling these apart
|
|
147
|
+
// needs a parser, so leave the script unchanged.
|
|
148
|
+
return undefined
|
|
149
|
+
} else if (char === "/" && regexAllowed(prev)) {
|
|
150
|
+
let inClass = false
|
|
151
|
+
i++
|
|
152
|
+
while (inClass || script[i] !== "/") {
|
|
153
|
+
if (i >= script.length || script[i] === "\n") return undefined
|
|
154
|
+
if (script[i] === "[") inClass = true
|
|
155
|
+
else if (script[i] === "]") inClass = false
|
|
156
|
+
i += script[i] === "\\" ? 2 : 1
|
|
27
157
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
158
|
+
i++
|
|
159
|
+
while (i < script.length && isWord(script[i]!)) i++
|
|
160
|
+
prev = "str"
|
|
161
|
+
} else if (isWord(char)) {
|
|
162
|
+
while (i < script.length && isWord(script[i]!)) i++
|
|
163
|
+
// A name after "." is a property, even when it is spelled like a keyword.
|
|
164
|
+
prev = prev === "." ? "id" : script.slice(start, i)
|
|
165
|
+
} else if (char === "{") {
|
|
166
|
+
if (blockOpeners.has(prev)) {
|
|
167
|
+
stack.push({ type: "block", start: i, opener: prev })
|
|
168
|
+
if (opaque === 0) breaks.add(i + 1)
|
|
169
|
+
} else {
|
|
170
|
+
stack.push({ type: "brace" })
|
|
171
|
+
}
|
|
172
|
+
prev = "{"
|
|
173
|
+
i++
|
|
174
|
+
} else if (char === "}") {
|
|
175
|
+
const frame = stack.pop()
|
|
176
|
+
if (
|
|
177
|
+
frame === undefined ||
|
|
178
|
+
!["block", "brace", "subst"].includes(frame.type)
|
|
179
|
+
) {
|
|
180
|
+
return undefined
|
|
31
181
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
182
|
+
i++
|
|
183
|
+
if (frame.type === "subst") continue
|
|
184
|
+
prev = "}o"
|
|
185
|
+
if (frame.type === "block") {
|
|
186
|
+
prev = "}"
|
|
187
|
+
if (opaque === 0) {
|
|
188
|
+
// End the block's last statement, then the statement the block ends.
|
|
189
|
+
if (prevEnd > frame.start + 1) breaks.add(prevEnd)
|
|
190
|
+
const next = nextToken(i)
|
|
191
|
+
if (
|
|
192
|
+
inStatements() &&
|
|
193
|
+
frame.opener !== "do" &&
|
|
194
|
+
(next === "" ||
|
|
195
|
+
next === "{" ||
|
|
196
|
+
next === '"' ||
|
|
197
|
+
next === "'" ||
|
|
198
|
+
(isWord(next[0]!) && !continuations.has(next)))
|
|
199
|
+
) {
|
|
200
|
+
breaks.add(i)
|
|
201
|
+
}
|
|
35
202
|
}
|
|
36
203
|
}
|
|
37
|
-
|
|
204
|
+
} else if (char === "(" || char === "[") {
|
|
205
|
+
stack.push({ type: char === "(" ? "paren" : "bracket" })
|
|
206
|
+
prev = char
|
|
207
|
+
i++
|
|
208
|
+
} else if (char === ")" || char === "]") {
|
|
209
|
+
if (top() !== (char === ")" ? "paren" : "bracket")) return undefined
|
|
210
|
+
stack.pop()
|
|
211
|
+
prev = char
|
|
212
|
+
i++
|
|
213
|
+
} else if (char === ";") {
|
|
214
|
+
if (opaque === 0 && inStatements()) breaks.add(i + 1)
|
|
215
|
+
prev = char
|
|
216
|
+
i++
|
|
217
|
+
} else {
|
|
218
|
+
const pair = script.slice(i, i + 2)
|
|
219
|
+
prev = pair === "=>" || pair === "++" || pair === "--" ? pair : char
|
|
220
|
+
i += prev.length
|
|
38
221
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
222
|
+
prevEnd = i
|
|
223
|
+
}
|
|
224
|
+
return stack.length === 0 ? breaks : undefined
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** Adds display-only line breaks, leaving execution and model history untouched. */
|
|
228
|
+
export const formatScript = (script: string): string => {
|
|
229
|
+
const breaks = findBreaks(script)
|
|
230
|
+
if (breaks === undefined) return script
|
|
231
|
+
let output = ""
|
|
232
|
+
let start = 0
|
|
233
|
+
const newline = script.includes("\r\n") ? "\r\n" : "\n"
|
|
234
|
+
for (const index of [...breaks].toSorted((a, b) => a - b)) {
|
|
235
|
+
// Keep existing newlines / indentation, and avoid adding a trailing line.
|
|
236
|
+
const rest = script.slice(index)
|
|
237
|
+
if (/^[ \t]*(?:[\r\n]|$)/.test(rest)) continue
|
|
238
|
+
output += script.slice(start, index) + newline
|
|
239
|
+
start = index
|
|
240
|
+
// Separators on the original line are not indentation for the new one.
|
|
241
|
+
while (script[start] === " " || script[start] === "\t") start++
|
|
56
242
|
}
|
|
243
|
+
return output + script.slice(start)
|
|
57
244
|
}
|