toolcraft 0.0.84 → 0.0.86
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/cli.d.ts +2 -1
- package/dist/cli.js +19 -7
- package/dist/design/render-markdown-plaintext.d.ts +2 -0
- package/dist/design/render-markdown-plaintext.js +1 -0
- package/dist/error-report.js +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +11 -0
- package/dist/mcp-proxy.d.ts +1 -1
- package/dist/mcp-proxy.js +2 -17
- package/dist/node-require-shim.d.ts +1 -0
- package/dist/node-require-shim.js +4 -0
- package/dist/project-root.d.ts +1 -0
- package/dist/project-root.js +15 -0
- package/node_modules/toolcraft-design/dist/index.d.ts +2 -2
- package/node_modules/toolcraft-design/dist/index.js +1 -1
- package/node_modules/toolcraft-design/dist/render-markdown-plaintext.d.ts +2 -0
- package/node_modules/toolcraft-design/dist/render-markdown-plaintext.js +1 -0
- package/node_modules/toolcraft-design/dist/terminal-markdown/index.d.ts +2 -0
- package/node_modules/toolcraft-design/dist/terminal-markdown/index.js +1 -0
- package/node_modules/toolcraft-design/dist/terminal-markdown/parser/inline.js +38 -2
- package/node_modules/toolcraft-design/dist/terminal-markdown/plaintext-renderer.d.ts +11 -0
- package/node_modules/toolcraft-design/dist/terminal-markdown/plaintext-renderer.js +228 -0
- package/node_modules/toolcraft-design/package.json +4 -0
- package/package.json +6 -2
package/dist/cli.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import "./node-require-shim.js";
|
|
1
2
|
import { configureTheme } from "toolcraft-design";
|
|
2
3
|
import type { Group, LogLevel, RuntimeLoggerInput } from "./index.js";
|
|
3
4
|
import { type ErrorReportsOption } from "./error-report.js";
|
|
4
|
-
import type { HumanInLoopRuntimeOptions } from "./human-in-loop/
|
|
5
|
+
import type { HumanInLoopRuntimeOptions } from "./human-in-loop/types.js";
|
|
5
6
|
export { configureTheme };
|
|
6
7
|
type Casing = "kebab" | "snake";
|
|
7
8
|
export interface CLIControls {
|
package/dist/cli.js
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
|
+
import "./node-require-shim.js";
|
|
1
2
|
import { access, lstat, readFile, rename, unlink, writeFile } from "node:fs/promises";
|
|
2
3
|
import path from "node:path";
|
|
3
4
|
import { Command as CommanderCommand, CommanderError, InvalidArgumentError, Option } from "commander";
|
|
4
5
|
import { cancel, configureTheme, confirm, createLogger, formatCommandList, formatOptionList, getTheme, helpFormatterPlain, isCancel, note, promptText, renderTable, resetOutputFormatCache, select, text } from "toolcraft-design";
|
|
5
|
-
import { ApprovalDeclinedError, UserError, assertCommandRequirements, getCommandSourcePath, resolveCommandSecrets } from "./index.js";
|
|
6
|
+
import { ApprovalDeclinedError, UserError, assertCommandRequirements, getCommandSourcePath, hasMcpProxyConfig, resolveCommandSecrets } from "./index.js";
|
|
6
7
|
import { hasOwnErrorCode } from "./error-codes.js";
|
|
7
|
-
import { mergeApprovalsGroup } from "./human-in-loop/approvals-commands.js";
|
|
8
8
|
import { writeErrorReport } from "./error-report.js";
|
|
9
|
-
import { invokeWithHumanInLoop } from "./human-in-loop/index.js";
|
|
10
|
-
import { resolveMcpProxies } from "./mcp-proxy.js";
|
|
11
9
|
import { getExpectedNumberDescription, isValidNumberSchemaValue } from "./number-schema.js";
|
|
12
10
|
import { findEntrypointPackageMetadata } from "./package-metadata.js";
|
|
13
11
|
import { redactHttpBody, redactHttpHeaderValue } from "./redaction.js";
|
|
@@ -33,6 +31,14 @@ const RESERVED_SERVICE_NAMES = new Set([
|
|
|
33
31
|
]);
|
|
34
32
|
const RESERVED_SERVICE_NAMES_MESSAGE = "Available reserved names: params, secrets, fetch, fs, env, diagnostics, progress, runtimeOptions, root.";
|
|
35
33
|
const NULL_OPTION_VALUE = Symbol("toolcraft.cli.null");
|
|
34
|
+
const optionalModulePaths = {
|
|
35
|
+
approvals: "./human-in-loop/approvals-commands.js",
|
|
36
|
+
humanInLoop: "./human-in-loop/gate.js",
|
|
37
|
+
mcpProxy: "./mcp-proxy.js"
|
|
38
|
+
};
|
|
39
|
+
function importOptionalModule(specifier) {
|
|
40
|
+
return import(specifier);
|
|
41
|
+
}
|
|
36
42
|
function inferProgramName(argv) {
|
|
37
43
|
const entrypoint = argv[1];
|
|
38
44
|
if (typeof entrypoint !== "string" || entrypoint.length === 0) {
|
|
@@ -2875,7 +2881,9 @@ async function executeCommand(state, services, requirementOptions, runtimeFetch,
|
|
|
2875
2881
|
throw new UserError("Operation cancelled.");
|
|
2876
2882
|
}
|
|
2877
2883
|
}
|
|
2878
|
-
const result =
|
|
2884
|
+
const result = state.command.humanInLoop
|
|
2885
|
+
? await (await importOptionalModule(optionalModulePaths.humanInLoop)).invokeWithHumanInLoop(state.command, context, runtimeOptions, state.commandPath)
|
|
2886
|
+
: await state.command.handler(context);
|
|
2879
2887
|
if (output === "rich" && runtime.isFixture) {
|
|
2880
2888
|
writeRichHeader(`${state.command.name} (fixture)`);
|
|
2881
2889
|
}
|
|
@@ -3419,8 +3427,12 @@ export async function runCLI(roots, options = {}) {
|
|
|
3419
3427
|
let errorReportContext;
|
|
3420
3428
|
try {
|
|
3421
3429
|
const normalizedRoot = normalizeRoots(roots, argv);
|
|
3422
|
-
const root = options.approvals === true
|
|
3423
|
-
|
|
3430
|
+
const root = options.approvals === true
|
|
3431
|
+
? (await importOptionalModule(optionalModulePaths.approvals)).mergeApprovalsGroup(normalizedRoot)
|
|
3432
|
+
: normalizedRoot;
|
|
3433
|
+
if (hasMcpProxyConfig(root)) {
|
|
3434
|
+
await (await importOptionalModule(optionalModulePaths.mcpProxy)).resolveMcpProxies(root, { projectRoot: options.projectRoot });
|
|
3435
|
+
}
|
|
3424
3436
|
const casing = options.casing ?? "kebab";
|
|
3425
3437
|
const services = (options.services ?? {});
|
|
3426
3438
|
const runtimeOptions = options.humanInLoop ?? {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { renderMarkdownPlaintext, renderPlaintext } from "toolcraft-design/render-markdown-plaintext";
|
package/dist/error-report.js
CHANGED
|
@@ -4,8 +4,8 @@ import os from "node:os";
|
|
|
4
4
|
import path from "node:path";
|
|
5
5
|
import { CommanderError } from "commander";
|
|
6
6
|
import { ApprovalDeclinedError } from "./human-in-loop/types.js";
|
|
7
|
-
import { findProjectRoot } from "./mcp-proxy.js";
|
|
8
7
|
import { findPackageMetadata } from "./package-metadata.js";
|
|
8
|
+
import { findProjectRoot } from "./project-root.js";
|
|
9
9
|
import { isSensitiveName, redactHttpBody, redactHttpHeaderValue } from "./redaction.js";
|
|
10
10
|
import { UserError } from "./user-error.js";
|
|
11
11
|
const ERROR_REPORTS_ENV = "TOOLCRAFT_ERROR_REPORTS";
|
package/dist/index.d.ts
CHANGED
|
@@ -200,6 +200,7 @@ export declare function defineGroup<TServices extends object = EmptyServices, TN
|
|
|
200
200
|
humanInLoop?: TOwnHumanInLoop;
|
|
201
201
|
}): Group<TServices> & TypedGroupMetadata<TServices, TName, TChildren, TOwnScope, ResolveOwnHumanInLoopMode<TOwnHumanInLoop>>;
|
|
202
202
|
export declare function getCommandSourcePath(command: Command<any, any, any, any>): string | undefined;
|
|
203
|
+
export declare function hasMcpProxyConfig(group: Group<any>): boolean;
|
|
203
204
|
export { S, toJsonSchema } from "toolcraft-schema";
|
|
204
205
|
export { AuthenticationError, BadRequestError, ClientError, ConflictError, HttpError, InternalServerError, NotFoundError, PermissionDeniedError, RateLimitError, ServerError, ServiceUnavailableError, UnprocessableEntityError, createHttpError } from "./http-errors.js";
|
|
205
206
|
export type { HttpErrorRequest, HttpErrorResponse } from "./http-errors.js";
|
package/dist/index.js
CHANGED
|
@@ -469,6 +469,17 @@ export function defineGroup(config) {
|
|
|
469
469
|
export function getCommandSourcePath(command) {
|
|
470
470
|
return command[commandSourcePathSymbol];
|
|
471
471
|
}
|
|
472
|
+
export function hasMcpProxyConfig(group) {
|
|
473
|
+
const configSymbol = Object.getOwnPropertySymbols(group).find((candidate) => candidate.description === groupConfigSymbol.description);
|
|
474
|
+
const config = configSymbol === undefined
|
|
475
|
+
? undefined
|
|
476
|
+
: group[configSymbol];
|
|
477
|
+
if (config?.mcp !== undefined) {
|
|
478
|
+
return true;
|
|
479
|
+
}
|
|
480
|
+
const children = config?.children ?? group.children;
|
|
481
|
+
return children.some((child) => child.kind === "group" && hasMcpProxyConfig(child));
|
|
482
|
+
}
|
|
472
483
|
export { S, toJsonSchema } from "toolcraft-schema";
|
|
473
484
|
export { AuthenticationError, BadRequestError, ClientError, ConflictError, HttpError, InternalServerError, NotFoundError, PermissionDeniedError, RateLimitError, ServerError, ServiceUnavailableError, UnprocessableEntityError, createHttpError } from "./http-errors.js";
|
|
474
485
|
export { ApprovalDeclinedError, ToolcraftBugError, UserError };
|
package/dist/mcp-proxy.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import type { McpServerConfig } from "@poe-code/agent-mcp-config";
|
|
2
2
|
import { McpClient } from "tiny-mcp-client";
|
|
3
3
|
import type { Group } from "./index.js";
|
|
4
|
+
export { findProjectRoot } from "./project-root.js";
|
|
4
5
|
export interface ResolveMcpProxyOptions {
|
|
5
6
|
projectRoot?: string;
|
|
6
7
|
}
|
|
7
8
|
export declare function hasMcpProxyGroups(root: Group<any>): boolean;
|
|
8
|
-
export declare function findProjectRoot(from?: string): string | undefined;
|
|
9
9
|
export declare function resolveCachePath(name: string, projectRoot?: string): string;
|
|
10
10
|
export declare function parseRefreshEnv(value: string | undefined): "all" | Set<string> | undefined;
|
|
11
11
|
export declare function dialUpstream(name: string, config: McpServerConfig): Promise<McpClient>;
|
package/dist/mcp-proxy.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { existsSync } from "node:fs";
|
|
2
1
|
import { lstat, mkdir, readFile, rename, unlink, writeFile } from "node:fs/promises";
|
|
3
2
|
import path from "node:path";
|
|
4
3
|
import { createHash, randomUUID } from "node:crypto";
|
|
@@ -6,6 +5,8 @@ import { createLogger } from "toolcraft-design";
|
|
|
6
5
|
import { HttpTransport, McpClient, StdioTransport } from "tiny-mcp-client";
|
|
7
6
|
import { hasOwnErrorCode } from "./error-codes.js";
|
|
8
7
|
import { convertJsonSchema } from "./json-schema-converter.js";
|
|
8
|
+
import { findProjectRoot } from "./project-root.js";
|
|
9
|
+
export { findProjectRoot } from "./project-root.js";
|
|
9
10
|
const GROUP_CONFIG_SYMBOL_DESCRIPTION = "toolcraft.group.config";
|
|
10
11
|
const MCP_PROXY_SCHEMA_URL = "https://poe-platform.github.io/poe-code/schemas/toolcraft/mcp-proxy.schema.json";
|
|
11
12
|
const DEFAULT_CLIENT_INFO = {
|
|
@@ -400,22 +401,6 @@ function collectProxyGroups(root) {
|
|
|
400
401
|
export function hasMcpProxyGroups(root) {
|
|
401
402
|
return collectProxyGroups(root).length > 0;
|
|
402
403
|
}
|
|
403
|
-
export function findProjectRoot(from = process.cwd()) {
|
|
404
|
-
let current = process.cwd();
|
|
405
|
-
if (from !== current) {
|
|
406
|
-
current = path.resolve(from);
|
|
407
|
-
}
|
|
408
|
-
while (true) {
|
|
409
|
-
if (existsSync(path.join(current, "package.json"))) {
|
|
410
|
-
return current;
|
|
411
|
-
}
|
|
412
|
-
const parent = path.dirname(current);
|
|
413
|
-
if (parent === current) {
|
|
414
|
-
return undefined;
|
|
415
|
-
}
|
|
416
|
-
current = parent;
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
404
|
export function resolveCachePath(name, projectRoot) {
|
|
420
405
|
const resolvedProjectRoot = projectRoot ?? findProjectRoot();
|
|
421
406
|
if (resolvedProjectRoot === undefined) {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function findProjectRoot(from?: string): string | undefined;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
export function findProjectRoot(from = process.cwd()) {
|
|
4
|
+
let current = from === process.cwd() ? process.cwd() : path.resolve(from);
|
|
5
|
+
while (true) {
|
|
6
|
+
if (existsSync(path.join(current, "package.json"))) {
|
|
7
|
+
return current;
|
|
8
|
+
}
|
|
9
|
+
const parent = path.dirname(current);
|
|
10
|
+
if (parent === current) {
|
|
11
|
+
return undefined;
|
|
12
|
+
}
|
|
13
|
+
current = parent;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -44,8 +44,8 @@ export { promptTheme } from "./prompts/theme.js";
|
|
|
44
44
|
export * as staticRender from "./static/index.js";
|
|
45
45
|
export { SPINNER_FRAMES, renderSpinnerFrame, renderSpinnerStopped, renderMenu } from "./static/index.js";
|
|
46
46
|
export type { SpinnerFrameOptions, SpinnerStoppedOptions, MenuOption, RenderMenuOptions } from "./static/index.js";
|
|
47
|
-
export { parse, render, renderHtml, renderMarkdown, renderMarkdownHtml } from "./terminal-markdown/index.js";
|
|
48
|
-
export type { CodeToken, CodeTokenKind, HtmlRenderOptions, MdNode, RenderOptions } from "./terminal-markdown/index.js";
|
|
47
|
+
export { parse, render, renderHtml, renderMarkdown, renderMarkdownHtml, renderPlaintext, renderMarkdownPlaintext } from "./terminal-markdown/index.js";
|
|
48
|
+
export type { CodeToken, CodeTokenKind, HtmlRenderOptions, MdNode, PlaintextRenderOptions, RenderOptions } from "./terminal-markdown/index.js";
|
|
49
49
|
export { getTheme, resolveThemeName, resetThemeCache } from "./internal/theme-detect.js";
|
|
50
50
|
export type { ThemeEnv } from "./internal/theme-detect.js";
|
|
51
51
|
export { configureTheme, getThemeConfig, resetTheme } from "./internal/theme-state.js";
|
|
@@ -37,7 +37,7 @@ export { promptTheme } from "./prompts/theme.js";
|
|
|
37
37
|
export * as staticRender from "./static/index.js";
|
|
38
38
|
export { SPINNER_FRAMES, renderSpinnerFrame, renderSpinnerStopped, renderMenu } from "./static/index.js";
|
|
39
39
|
// Terminal markdown
|
|
40
|
-
export { parse, render, renderHtml, renderMarkdown, renderMarkdownHtml } from "./terminal-markdown/index.js";
|
|
40
|
+
export { parse, render, renderHtml, renderMarkdown, renderMarkdownHtml, renderPlaintext, renderMarkdownPlaintext } from "./terminal-markdown/index.js";
|
|
41
41
|
// Internal utilities (for advanced use)
|
|
42
42
|
export { getTheme, resolveThemeName, resetThemeCache } from "./internal/theme-detect.js";
|
|
43
43
|
export { configureTheme, getThemeConfig, resetTheme } from "./internal/theme-state.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { renderMarkdownPlaintext, renderPlaintext } from "./terminal-markdown/index.js";
|
|
@@ -3,6 +3,8 @@ import { type RenderOptions } from "./renderer.js";
|
|
|
3
3
|
export type { CodeToken, CodeTokenKind, MdNode } from "./ast.js";
|
|
4
4
|
export { renderHtml } from "./html-renderer.js";
|
|
5
5
|
export type { HtmlRenderOptions } from "./html-renderer.js";
|
|
6
|
+
export { renderPlaintext, renderMarkdownPlaintext } from "./plaintext-renderer.js";
|
|
7
|
+
export type { PlaintextRenderOptions } from "./plaintext-renderer.js";
|
|
6
8
|
export { parse } from "./parser.js";
|
|
7
9
|
export { render } from "./renderer.js";
|
|
8
10
|
export type { RenderOptions } from "./renderer.js";
|
|
@@ -2,6 +2,7 @@ import { parse } from "./parser.js";
|
|
|
2
2
|
import { renderHtml } from "./html-renderer.js";
|
|
3
3
|
import { render } from "./renderer.js";
|
|
4
4
|
export { renderHtml } from "./html-renderer.js";
|
|
5
|
+
export { renderPlaintext, renderMarkdownPlaintext } from "./plaintext-renderer.js";
|
|
5
6
|
export { parse } from "./parser.js";
|
|
6
7
|
export { render } from "./renderer.js";
|
|
7
8
|
export function renderMarkdown(markdown, options) {
|
|
@@ -853,9 +853,12 @@ function parseInlineHtmlTag(input, start, offsets) {
|
|
|
853
853
|
return null;
|
|
854
854
|
}
|
|
855
855
|
if (input[index] === ">") {
|
|
856
|
+
const tagEnd = index + 1;
|
|
857
|
+
const closingTagEnd = findClosingInlineHtmlTagEnd(input, tagEnd, tagName);
|
|
858
|
+
const nodeEnd = closingTagEnd ?? tagEnd;
|
|
856
859
|
return {
|
|
857
|
-
node: withRange({ type: "html", value: input.slice(start,
|
|
858
|
-
end:
|
|
860
|
+
node: withRange({ type: "html", value: input.slice(start, nodeEnd) }, offsets === undefined ? { start, end: nodeEnd } : createRange(offsets, start, nodeEnd)),
|
|
861
|
+
end: nodeEnd
|
|
859
862
|
};
|
|
860
863
|
}
|
|
861
864
|
if (input[index] === "/") {
|
|
@@ -907,6 +910,39 @@ function parseInlineHtmlTag(input, start, offsets) {
|
|
|
907
910
|
}
|
|
908
911
|
return null;
|
|
909
912
|
}
|
|
913
|
+
function findClosingInlineHtmlTagEnd(input, start, tagName) {
|
|
914
|
+
let index = start;
|
|
915
|
+
while (index < input.length) {
|
|
916
|
+
if (input[index] !== "<") {
|
|
917
|
+
index += 1;
|
|
918
|
+
continue;
|
|
919
|
+
}
|
|
920
|
+
const closingEnd = readClosingInlineHtmlTagEnd(input, index, tagName);
|
|
921
|
+
if (closingEnd !== undefined) {
|
|
922
|
+
return closingEnd;
|
|
923
|
+
}
|
|
924
|
+
index += 1;
|
|
925
|
+
}
|
|
926
|
+
return undefined;
|
|
927
|
+
}
|
|
928
|
+
function readClosingInlineHtmlTagEnd(input, start, tagName) {
|
|
929
|
+
let index = start + 1;
|
|
930
|
+
if (index >= input.length || input[index] !== "/") {
|
|
931
|
+
return undefined;
|
|
932
|
+
}
|
|
933
|
+
index += 1;
|
|
934
|
+
for (const expectedChar of tagName) {
|
|
935
|
+
if (index >= input.length || input[index]?.toLowerCase() !== expectedChar) {
|
|
936
|
+
return undefined;
|
|
937
|
+
}
|
|
938
|
+
index += 1;
|
|
939
|
+
}
|
|
940
|
+
index = skipHtmlWhitespace(input, index);
|
|
941
|
+
if (index >= input.length || input[index] !== ">") {
|
|
942
|
+
return undefined;
|
|
943
|
+
}
|
|
944
|
+
return index + 1;
|
|
945
|
+
}
|
|
910
946
|
function decodeEscapes(value) {
|
|
911
947
|
let result = "";
|
|
912
948
|
let index = 0;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { MdNode } from "./ast.js";
|
|
2
|
+
export interface PlaintextRenderOptions {
|
|
3
|
+
announceHeadings?: boolean;
|
|
4
|
+
announceCode?: boolean;
|
|
5
|
+
announceAlerts?: boolean;
|
|
6
|
+
showLinks?: boolean;
|
|
7
|
+
expandLinks?: boolean;
|
|
8
|
+
includeFrontmatter?: boolean;
|
|
9
|
+
}
|
|
10
|
+
export declare function renderPlaintext(ast: MdNode, options?: PlaintextRenderOptions): string;
|
|
11
|
+
export declare function renderMarkdownPlaintext(markdown: string, options?: PlaintextRenderOptions): string;
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import { stripAnsi } from "../internal/strip-ansi.js";
|
|
2
|
+
import { parse } from "./parser.js";
|
|
3
|
+
function renderInline(node, ctx) {
|
|
4
|
+
switch (node.type) {
|
|
5
|
+
case "text":
|
|
6
|
+
return stripAnsi(node.value);
|
|
7
|
+
case "emphasis":
|
|
8
|
+
case "strong":
|
|
9
|
+
return renderChildren(node.children, ctx);
|
|
10
|
+
case "strikethrough":
|
|
11
|
+
return "";
|
|
12
|
+
case "inlineCode":
|
|
13
|
+
return stripAnsi(node.value);
|
|
14
|
+
case "break":
|
|
15
|
+
return " ";
|
|
16
|
+
case "html":
|
|
17
|
+
return "";
|
|
18
|
+
case "link": {
|
|
19
|
+
const childText = renderChildren(node.children, ctx) || node.url;
|
|
20
|
+
if (ctx.expandLinks) {
|
|
21
|
+
return `${childText} ${stripAnsi(node.url)}`;
|
|
22
|
+
}
|
|
23
|
+
if (ctx.showLinks) {
|
|
24
|
+
return `${childText} (link)`;
|
|
25
|
+
}
|
|
26
|
+
return childText;
|
|
27
|
+
}
|
|
28
|
+
case "image":
|
|
29
|
+
return stripAnsi(node.alt);
|
|
30
|
+
case "footnoteReference": {
|
|
31
|
+
if (!ctx.footnoteOrder.includes(node.label)) {
|
|
32
|
+
ctx.footnoteOrder.push(node.label);
|
|
33
|
+
}
|
|
34
|
+
const index = ctx.footnoteOrder.indexOf(node.label) + 1;
|
|
35
|
+
return `[${index}]`;
|
|
36
|
+
}
|
|
37
|
+
default:
|
|
38
|
+
return "";
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
function renderChildren(nodes, ctx) {
|
|
42
|
+
return nodes.map((node) => renderInline(node, ctx)).join("");
|
|
43
|
+
}
|
|
44
|
+
function renderBlock(node, ctx) {
|
|
45
|
+
switch (node.type) {
|
|
46
|
+
case "root": {
|
|
47
|
+
collectFootnoteDefinitions(node.children, ctx);
|
|
48
|
+
const text = trimBlockSeparators(renderBlockChildren(node.children, ctx));
|
|
49
|
+
const footnotes = renderReferencedFootnotes(ctx);
|
|
50
|
+
return footnotes.length === 0 ? text : trimBlockSeparators(`${text}\n\n${footnotes}`);
|
|
51
|
+
}
|
|
52
|
+
case "paragraph":
|
|
53
|
+
return `${renderChildren(node.children, ctx)}\n\n`;
|
|
54
|
+
case "thematicBreak":
|
|
55
|
+
return "";
|
|
56
|
+
case "heading": {
|
|
57
|
+
const prefix = ctx.announceHeadings && node.depth === 1
|
|
58
|
+
? "Section: "
|
|
59
|
+
: ctx.announceHeadings && node.depth === 2
|
|
60
|
+
? "Subsection: "
|
|
61
|
+
: ctx.announceHeadings
|
|
62
|
+
? "Topic: "
|
|
63
|
+
: "";
|
|
64
|
+
return `${prefix}${renderChildren(node.children, ctx).trim()}\n\n`;
|
|
65
|
+
}
|
|
66
|
+
case "blockquote":
|
|
67
|
+
return `Quote: ${renderBlockChildren(node.children, ctx).trim()}\n\n`;
|
|
68
|
+
case "alert": {
|
|
69
|
+
const prefix = ctx.announceAlerts ? `${node.kind}: ` : "";
|
|
70
|
+
return `${prefix}${renderBlockChildren(node.children, ctx).trim()}\n\n`;
|
|
71
|
+
}
|
|
72
|
+
case "list": {
|
|
73
|
+
const items = node.children
|
|
74
|
+
.filter((child) => child.type === "listItem")
|
|
75
|
+
.map((child, index) => {
|
|
76
|
+
const text = renderBlock(child, ctx).trim();
|
|
77
|
+
return node.ordered ? `${getOrderedListPrefix(index)}${text}` : text;
|
|
78
|
+
});
|
|
79
|
+
return `${items.join(node.ordered ? " " : getUnorderedListSeparator(items.length))}\n\n`;
|
|
80
|
+
}
|
|
81
|
+
case "listItem": {
|
|
82
|
+
const text = renderBlockChildren(node.children, ctx).trim();
|
|
83
|
+
if (node.checked === true) {
|
|
84
|
+
return `done: ${text}`;
|
|
85
|
+
}
|
|
86
|
+
if (node.checked === false) {
|
|
87
|
+
return `to do: ${text}`;
|
|
88
|
+
}
|
|
89
|
+
return text;
|
|
90
|
+
}
|
|
91
|
+
case "table": {
|
|
92
|
+
const [headerRow, ...bodyRows] = node.children.filter((child) => child.type === "tableRow");
|
|
93
|
+
if (!headerRow) {
|
|
94
|
+
return "\n\n";
|
|
95
|
+
}
|
|
96
|
+
const headers = headerRow.children.map((cell) => cell.type === "tableCell" ? renderChildren(cell.children, ctx).trim() : "");
|
|
97
|
+
const sentences = bodyRows.flatMap((row) => row.children.flatMap((cell, index) => {
|
|
98
|
+
if (cell.type !== "tableCell") {
|
|
99
|
+
return [];
|
|
100
|
+
}
|
|
101
|
+
const header = headers[index]?.trim() ?? "";
|
|
102
|
+
const value = renderChildren(cell.children, ctx).trim();
|
|
103
|
+
return value === "" ? [] : `${header} is ${value}.`;
|
|
104
|
+
}));
|
|
105
|
+
return `${sentences.join(" ")}\n\n`;
|
|
106
|
+
}
|
|
107
|
+
case "tableRow":
|
|
108
|
+
case "tableCell":
|
|
109
|
+
return "";
|
|
110
|
+
case "code": {
|
|
111
|
+
const prefix = ctx.announceCode ? "Code: " : "";
|
|
112
|
+
const value = node.value.length === 0 && ctx.announceCode ? "Code:" : `${prefix}${node.value}`;
|
|
113
|
+
return `${value}\n\n`;
|
|
114
|
+
}
|
|
115
|
+
case "frontmatter": {
|
|
116
|
+
if (!ctx.includeFrontmatter) {
|
|
117
|
+
return "";
|
|
118
|
+
}
|
|
119
|
+
return `${Object.entries(node.data)
|
|
120
|
+
.map(([key, value]) => `${key}: ${String(value)}.`)
|
|
121
|
+
.join(" ")}\n\n`;
|
|
122
|
+
}
|
|
123
|
+
case "footnoteDefinition":
|
|
124
|
+
return "";
|
|
125
|
+
default:
|
|
126
|
+
return "";
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
function renderBlockChildren(nodes, ctx) {
|
|
130
|
+
return nodes
|
|
131
|
+
.map((node) => (isBlockNode(node) ? renderBlock(node, ctx) : renderInline(node, ctx)))
|
|
132
|
+
.join("");
|
|
133
|
+
}
|
|
134
|
+
function isBlockNode(node) {
|
|
135
|
+
switch (node.type) {
|
|
136
|
+
case "root":
|
|
137
|
+
case "paragraph":
|
|
138
|
+
case "thematicBreak":
|
|
139
|
+
case "heading":
|
|
140
|
+
case "blockquote":
|
|
141
|
+
case "alert":
|
|
142
|
+
case "list":
|
|
143
|
+
case "listItem":
|
|
144
|
+
case "table":
|
|
145
|
+
case "tableRow":
|
|
146
|
+
case "tableCell":
|
|
147
|
+
case "code":
|
|
148
|
+
case "frontmatter":
|
|
149
|
+
case "footnoteDefinition":
|
|
150
|
+
return true;
|
|
151
|
+
default:
|
|
152
|
+
return false;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
function collectFootnoteDefinitions(nodes, ctx) {
|
|
156
|
+
for (const node of nodes) {
|
|
157
|
+
if (node.type === "footnoteDefinition") {
|
|
158
|
+
ctx.footnoteDefinitions.set(node.label, renderBlockChildren(node.children, createFootnoteDefinitionContext(ctx)).trim());
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
if ("children" in node) {
|
|
162
|
+
collectFootnoteDefinitions(node.children, ctx);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
function createFootnoteDefinitionContext(ctx) {
|
|
167
|
+
return {
|
|
168
|
+
announceHeadings: ctx.announceHeadings,
|
|
169
|
+
announceCode: ctx.announceCode,
|
|
170
|
+
announceAlerts: ctx.announceAlerts,
|
|
171
|
+
showLinks: ctx.showLinks,
|
|
172
|
+
expandLinks: ctx.expandLinks,
|
|
173
|
+
includeFrontmatter: ctx.includeFrontmatter,
|
|
174
|
+
footnoteDefinitions: new Map(),
|
|
175
|
+
footnoteOrder: []
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
function renderReferencedFootnotes(ctx) {
|
|
179
|
+
return ctx.footnoteOrder
|
|
180
|
+
.map((label, index) => {
|
|
181
|
+
const definitionText = ctx.footnoteDefinitions.get(label);
|
|
182
|
+
return definitionText === undefined ? "" : `Note ${index + 1}: ${definitionText}.`;
|
|
183
|
+
})
|
|
184
|
+
.filter((value) => value.length > 0)
|
|
185
|
+
.join(" ");
|
|
186
|
+
}
|
|
187
|
+
function getOrderedListPrefix(index) {
|
|
188
|
+
switch (index) {
|
|
189
|
+
case 0:
|
|
190
|
+
return "First, ";
|
|
191
|
+
case 1:
|
|
192
|
+
return "Second, ";
|
|
193
|
+
case 2:
|
|
194
|
+
return "Third, ";
|
|
195
|
+
default:
|
|
196
|
+
return "Next, ";
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
function getUnorderedListSeparator(itemCount) {
|
|
200
|
+
return itemCount <= 3 ? ", " : "; ";
|
|
201
|
+
}
|
|
202
|
+
function trimBlockSeparators(value) {
|
|
203
|
+
let start = 0;
|
|
204
|
+
let end = value.length;
|
|
205
|
+
while (start < end && value[start] === "\n") {
|
|
206
|
+
start += 1;
|
|
207
|
+
}
|
|
208
|
+
while (end > start && value[end - 1] === "\n") {
|
|
209
|
+
end -= 1;
|
|
210
|
+
}
|
|
211
|
+
return value.slice(start, end);
|
|
212
|
+
}
|
|
213
|
+
export function renderPlaintext(ast, options) {
|
|
214
|
+
return renderBlock(ast, {
|
|
215
|
+
announceHeadings: options?.announceHeadings ?? true,
|
|
216
|
+
announceCode: options?.announceCode ?? true,
|
|
217
|
+
announceAlerts: options?.announceAlerts ?? true,
|
|
218
|
+
showLinks: options?.showLinks ?? false,
|
|
219
|
+
expandLinks: options?.expandLinks ?? false,
|
|
220
|
+
includeFrontmatter: options?.includeFrontmatter ?? false,
|
|
221
|
+
footnoteDefinitions: new Map(),
|
|
222
|
+
footnoteOrder: []
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
export function renderMarkdownPlaintext(markdown, options) {
|
|
226
|
+
const { ast } = parse(markdown);
|
|
227
|
+
return renderPlaintext(ast, options);
|
|
228
|
+
}
|
|
@@ -29,6 +29,10 @@
|
|
|
29
29
|
"types": "./dist/index.d.ts",
|
|
30
30
|
"import": "./dist/index.js"
|
|
31
31
|
},
|
|
32
|
+
"./render-markdown-plaintext": {
|
|
33
|
+
"types": "./dist/render-markdown-plaintext.d.ts",
|
|
34
|
+
"import": "./dist/render-markdown-plaintext.js"
|
|
35
|
+
},
|
|
32
36
|
"./*": {
|
|
33
37
|
"types": "./dist/*.d.ts",
|
|
34
38
|
"import": "./dist/*.js"
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "toolcraft",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.86",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -17,6 +17,10 @@
|
|
|
17
17
|
"types": "./dist/design.d.ts",
|
|
18
18
|
"import": "./dist/design.js"
|
|
19
19
|
},
|
|
20
|
+
"./design/render-markdown-plaintext": {
|
|
21
|
+
"types": "./dist/design/render-markdown-plaintext.d.ts",
|
|
22
|
+
"import": "./dist/design/render-markdown-plaintext.js"
|
|
23
|
+
},
|
|
20
24
|
"./design/*": {
|
|
21
25
|
"types": "./dist/design/*.d.ts",
|
|
22
26
|
"import": "./dist/design/*.js"
|
|
@@ -51,7 +55,7 @@
|
|
|
51
55
|
"postpack": "node ../../scripts/manage-bundled-workspace-deps.mjs cleanup . toolcraft-design @poe-code/frontmatter @poe-code/agent-mcp-config @poe-code/agent-human-in-loop @poe-code/task-list @poe-code/agent-defs @poe-code/config-mutations @poe-code/process-runner tiny-mcp-client mcp-oauth auth-store"
|
|
52
56
|
},
|
|
53
57
|
"dependencies": {
|
|
54
|
-
"toolcraft-schema": "0.0.
|
|
58
|
+
"toolcraft-schema": "0.0.86",
|
|
55
59
|
"commander": "^13.1.0",
|
|
56
60
|
"fast-string-width": "^3.0.2",
|
|
57
61
|
"fast-wrap-ansi": "^0.2.0",
|