fconvert 0.1.3 → 0.1.5
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/convert +0 -0
- package/dist/fconvert-picker +0 -0
- package/package.json +1 -1
- package/src/cli/commands/convert.ts +45 -2
- package/src/cli/output-picker.ts +13 -5
- package/src/planner/search.ts +42 -0
- package/tools/format-picker/main.go +35 -9
package/dist/convert
CHANGED
|
Binary file
|
package/dist/fconvert-picker
CHANGED
|
Binary file
|
package/package.json
CHANGED
|
@@ -7,10 +7,13 @@ import { ConsoleLogger } from "../../core/logger.ts";
|
|
|
7
7
|
import type { ConvertSummary, FileFormat } from "../../core/types.ts";
|
|
8
8
|
import { buildDefaultOutputPath, pickOutputFormatInteractive } from "../output-picker.ts";
|
|
9
9
|
import { ConversionEngine } from "../../executor/executor.ts";
|
|
10
|
+
import { Workspace } from "../../executor/workspace.ts";
|
|
10
11
|
import { detectInputFormat, resolveOutputFormat } from "../../formats/detect.ts";
|
|
11
12
|
import { FormatRegistry } from "../../formats/registry.ts";
|
|
12
13
|
import { HandlerRegistry } from "../../handlers/registry.ts";
|
|
14
|
+
import { ConversionGraph } from "../../planner/graph.ts";
|
|
13
15
|
import { explainRoute } from "../../planner/explain.ts";
|
|
16
|
+
import { findReachableFormats } from "../../planner/search.ts";
|
|
14
17
|
|
|
15
18
|
async function outputExists(path: string): Promise<boolean> {
|
|
16
19
|
try {
|
|
@@ -21,6 +24,37 @@ async function outputExists(path: string): Promise<boolean> {
|
|
|
21
24
|
}
|
|
22
25
|
}
|
|
23
26
|
|
|
27
|
+
async function findSelectableOutputFormats(input: {
|
|
28
|
+
formats: FormatRegistry;
|
|
29
|
+
inputFormat: FileFormat;
|
|
30
|
+
bundle: BundleResolver;
|
|
31
|
+
logger: ConsoleLogger;
|
|
32
|
+
strict: boolean;
|
|
33
|
+
maxSteps: number;
|
|
34
|
+
timeoutMs?: number;
|
|
35
|
+
}): Promise<FileFormat[]> {
|
|
36
|
+
const workspace = await Workspace.create("convert-probe-");
|
|
37
|
+
const probeHandlers = new HandlerRegistry();
|
|
38
|
+
try {
|
|
39
|
+
await probeHandlers.init({
|
|
40
|
+
workspace,
|
|
41
|
+
bundle: input.bundle,
|
|
42
|
+
logger: input.logger,
|
|
43
|
+
timeoutMs: input.timeoutMs,
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
const graph = new ConversionGraph(input.formats, probeHandlers.availableHandlers(), input.strict);
|
|
47
|
+
const reachable = findReachableFormats(graph, input.inputFormat.id, Math.max(1, input.maxSteps));
|
|
48
|
+
const candidates = input.formats
|
|
49
|
+
.all()
|
|
50
|
+
.filter((format) => reachable.has(format.id))
|
|
51
|
+
.sort((a, b) => a.id.localeCompare(b.id));
|
|
52
|
+
return candidates;
|
|
53
|
+
} finally {
|
|
54
|
+
await workspace.cleanup(false);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
24
58
|
export async function runConvertCommand(input: {
|
|
25
59
|
positionals: string[];
|
|
26
60
|
options: {
|
|
@@ -54,11 +88,21 @@ export async function runConvertCommand(input: {
|
|
|
54
88
|
|
|
55
89
|
const formats = new FormatRegistry();
|
|
56
90
|
const inputFormat = detectInputFormat(inputPath, input.options.from, formats);
|
|
91
|
+
const bundle = new BundleResolver();
|
|
57
92
|
let outputFormat: FileFormat;
|
|
58
93
|
if (explicitOutputPath || input.options.to) {
|
|
59
94
|
outputFormat = resolveOutputFormat(explicitOutputPath, input.options.to, formats);
|
|
60
95
|
} else {
|
|
61
|
-
|
|
96
|
+
const selectableFormats = await findSelectableOutputFormats({
|
|
97
|
+
formats,
|
|
98
|
+
inputFormat,
|
|
99
|
+
bundle,
|
|
100
|
+
logger,
|
|
101
|
+
strict: input.options.strict,
|
|
102
|
+
maxSteps: input.options.maxSteps,
|
|
103
|
+
timeoutMs: input.options.timeoutMs,
|
|
104
|
+
});
|
|
105
|
+
outputFormat = await pickOutputFormatInteractive(selectableFormats, inputPath);
|
|
62
106
|
}
|
|
63
107
|
|
|
64
108
|
const outputPath = explicitOutputPath ?? buildDefaultOutputPath(inputPath, outputFormat);
|
|
@@ -67,7 +111,6 @@ export async function runConvertCommand(input: {
|
|
|
67
111
|
throw new CliError(`Output exists: ${outputPath}. Use --force to overwrite.`, ExitCode.InvalidArgs);
|
|
68
112
|
}
|
|
69
113
|
|
|
70
|
-
const bundle = new BundleResolver();
|
|
71
114
|
const handlers = new HandlerRegistry();
|
|
72
115
|
const engine = new ConversionEngine(formats, handlers, bundle, logger);
|
|
73
116
|
|
package/src/cli/output-picker.ts
CHANGED
|
@@ -4,11 +4,11 @@ import { tmpdir } from "node:os";
|
|
|
4
4
|
import { basename, dirname, extname, join } from "node:path";
|
|
5
5
|
import { CliError, ExitCode } from "../core/errors.ts";
|
|
6
6
|
import type { FileFormat } from "../core/types.ts";
|
|
7
|
-
import { FormatRegistry } from "../formats/registry.ts";
|
|
8
7
|
|
|
9
8
|
interface PickerPayload {
|
|
10
9
|
prompt: string;
|
|
11
10
|
query: string;
|
|
11
|
+
preferred: string;
|
|
12
12
|
options: Array<{
|
|
13
13
|
id: string;
|
|
14
14
|
name: string;
|
|
@@ -86,7 +86,7 @@ export function buildDefaultOutputPath(inputPath: string, outputFormat: FileForm
|
|
|
86
86
|
}
|
|
87
87
|
|
|
88
88
|
export async function pickOutputFormatInteractive(
|
|
89
|
-
|
|
89
|
+
formats: FileFormat[],
|
|
90
90
|
inputPath: string,
|
|
91
91
|
): Promise<FileFormat> {
|
|
92
92
|
if (!process.stdin.isTTY || !process.stdout.isTTY) {
|
|
@@ -96,6 +96,13 @@ export async function pickOutputFormatInteractive(
|
|
|
96
96
|
);
|
|
97
97
|
}
|
|
98
98
|
|
|
99
|
+
if (formats.length === 0) {
|
|
100
|
+
throw new CliError(
|
|
101
|
+
"No reachable output formats detected for this input with current handlers.",
|
|
102
|
+
ExitCode.UnsupportedRoute,
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
|
|
99
106
|
const pickerBinary = await resolvePickerBinary();
|
|
100
107
|
if (!pickerBinary) {
|
|
101
108
|
throw new CliError(
|
|
@@ -110,8 +117,9 @@ export async function pickOutputFormatInteractive(
|
|
|
110
117
|
|
|
111
118
|
const payload: PickerPayload = {
|
|
112
119
|
prompt: "output format",
|
|
113
|
-
query:
|
|
114
|
-
|
|
120
|
+
query: "",
|
|
121
|
+
preferred: extname(inputPath).replace(/^\./, ""),
|
|
122
|
+
options: formats.map((format) => ({
|
|
115
123
|
id: format.id,
|
|
116
124
|
name: format.name,
|
|
117
125
|
extension: format.extension,
|
|
@@ -139,7 +147,7 @@ export async function pickOutputFormatInteractive(
|
|
|
139
147
|
}
|
|
140
148
|
|
|
141
149
|
const chosenId = (await readFile(resultPath, "utf8")).trim();
|
|
142
|
-
const format =
|
|
150
|
+
const format = formats.find((item) => item.id === chosenId);
|
|
143
151
|
if (!format) {
|
|
144
152
|
throw new CliError(`Picker returned unknown format: ${chosenId}`, ExitCode.InternalError);
|
|
145
153
|
}
|
package/src/planner/search.ts
CHANGED
|
@@ -75,3 +75,45 @@ export function findRoutes(
|
|
|
75
75
|
|
|
76
76
|
return routes.sort((a, b) => a.totalCost - b.totalCost);
|
|
77
77
|
}
|
|
78
|
+
|
|
79
|
+
interface ReachableState {
|
|
80
|
+
node: string;
|
|
81
|
+
steps: number;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function findReachableFormats(
|
|
85
|
+
graph: ConversionGraph,
|
|
86
|
+
fromId: string,
|
|
87
|
+
maxSteps: number,
|
|
88
|
+
): Set<string> {
|
|
89
|
+
const reached = new Set<string>();
|
|
90
|
+
const bestSteps = new Map<string, number>();
|
|
91
|
+
const queue: ReachableState[] = [{ node: fromId, steps: 0 }];
|
|
92
|
+
|
|
93
|
+
bestSteps.set(fromId, 0);
|
|
94
|
+
|
|
95
|
+
while (queue.length > 0) {
|
|
96
|
+
const current = queue.shift();
|
|
97
|
+
if (!current) {
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (current.steps >= maxSteps) {
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
for (const edge of graph.outgoing(current.node)) {
|
|
106
|
+
const nextSteps = current.steps + 1;
|
|
107
|
+
const known = bestSteps.get(edge.to.id);
|
|
108
|
+
if (typeof known === "number" && known <= nextSteps) {
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
bestSteps.set(edge.to.id, nextSteps);
|
|
113
|
+
reached.add(edge.to.id);
|
|
114
|
+
queue.push({ node: edge.to.id, steps: nextSteps });
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return reached;
|
|
119
|
+
}
|
|
@@ -18,9 +18,10 @@ type option struct {
|
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
type payload struct {
|
|
21
|
-
Prompt
|
|
22
|
-
Query
|
|
23
|
-
|
|
21
|
+
Prompt string `json:"prompt"`
|
|
22
|
+
Query string `json:"query"`
|
|
23
|
+
Preferred string `json:"preferred"`
|
|
24
|
+
Options []option `json:"options"`
|
|
24
25
|
}
|
|
25
26
|
|
|
26
27
|
type scoredOption struct {
|
|
@@ -31,6 +32,7 @@ type scoredOption struct {
|
|
|
31
32
|
type model struct {
|
|
32
33
|
prompt string
|
|
33
34
|
query string
|
|
35
|
+
preferred string
|
|
34
36
|
all []option
|
|
35
37
|
filtered []option
|
|
36
38
|
cursor int
|
|
@@ -93,6 +95,9 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
|
93
95
|
func (m model) View() string {
|
|
94
96
|
var builder strings.Builder
|
|
95
97
|
builder.WriteString(fmt.Sprintf("%s > %s\n", m.prompt, m.query))
|
|
98
|
+
if m.query == "" && m.preferred != "" {
|
|
99
|
+
builder.WriteString(fmt.Sprintf(" hint: original extension '.%s' is ranked first\n", m.preferred))
|
|
100
|
+
}
|
|
96
101
|
|
|
97
102
|
if len(m.filtered) == 0 {
|
|
98
103
|
builder.WriteString(" no matches\n")
|
|
@@ -139,7 +144,7 @@ func (m *model) refilter() {
|
|
|
139
144
|
scored := make([]scoredOption, 0, len(m.all))
|
|
140
145
|
|
|
141
146
|
for _, item := range m.all {
|
|
142
|
-
score := fuzzyScore(query, item)
|
|
147
|
+
score := fuzzyScore(query, item, m.preferred)
|
|
143
148
|
if score < 0 {
|
|
144
149
|
continue
|
|
145
150
|
}
|
|
@@ -166,9 +171,19 @@ func (m *model) refilter() {
|
|
|
166
171
|
}
|
|
167
172
|
}
|
|
168
173
|
|
|
169
|
-
func fuzzyScore(query string, item option) int {
|
|
174
|
+
func fuzzyScore(query string, item option, preferred string) int {
|
|
170
175
|
if query == "" {
|
|
171
|
-
|
|
176
|
+
score := 1
|
|
177
|
+
if preferred != "" {
|
|
178
|
+
normalizedPreferred := strings.ToLower(preferred)
|
|
179
|
+
if strings.EqualFold(item.Extension, normalizedPreferred) {
|
|
180
|
+
score += 25
|
|
181
|
+
}
|
|
182
|
+
if strings.EqualFold(item.ID, normalizedPreferred) {
|
|
183
|
+
score += 20
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return score
|
|
172
187
|
}
|
|
173
188
|
|
|
174
189
|
candidate := strings.ToLower(item.ID + " " + item.Extension + " " + item.Name)
|
|
@@ -207,6 +222,16 @@ func fuzzyScore(query string, item option) int {
|
|
|
207
222
|
if strings.HasPrefix(strings.ToLower(item.ID), query) {
|
|
208
223
|
score += 15
|
|
209
224
|
}
|
|
225
|
+
if preferred != "" {
|
|
226
|
+
normalizedPreferred := strings.ToLower(preferred)
|
|
227
|
+
if strings.EqualFold(item.Extension, normalizedPreferred) {
|
|
228
|
+
score += 8
|
|
229
|
+
}
|
|
230
|
+
if strings.EqualFold(item.ID, normalizedPreferred) {
|
|
231
|
+
score += 6
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
210
235
|
return score
|
|
211
236
|
}
|
|
212
237
|
|
|
@@ -244,9 +269,10 @@ func main() {
|
|
|
244
269
|
}
|
|
245
270
|
|
|
246
271
|
picker := model{
|
|
247
|
-
prompt:
|
|
248
|
-
query:
|
|
249
|
-
|
|
272
|
+
prompt: data.Prompt,
|
|
273
|
+
query: data.Query,
|
|
274
|
+
preferred: strings.TrimSpace(data.Preferred),
|
|
275
|
+
all: data.Options,
|
|
250
276
|
}
|
|
251
277
|
picker.refilter()
|
|
252
278
|
|