This repository was archived by the owner on Feb 24, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwireflow.sh
More file actions
executable file
·504 lines (463 loc) · 16.1 KB
/
wireflow.sh
File metadata and controls
executable file
·504 lines (463 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
#!/usr/bin/env bash
set -e
# =============================================================================
# WireFlow: Build Reproducible AI Workflows Anywhere in the Terminal
# =============================================================================
#
# WireFlow is a command-line interface for building reproducible AI workflows
# for research, development, and analysis, featuring flexible configuration
# cascades, comprehensive document processing (PDFs, Office files, images),
# workflow dependencies and chaining, and intelligent context management.
#
# =============================================================================
WIREFLOW_VERSION="0.7.3"
# =============================================================================
# Script name -- as called by the user
SCRIPT_NAME="$(basename ${0})"
# Source function libraries
SCRIPT_DIR="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
source "$SCRIPT_DIR/lib/api.sh"
source "$SCRIPT_DIR/lib/batch.sh"
source "$SCRIPT_DIR/lib/config.sh"
source "$SCRIPT_DIR/lib/core.sh"
source "$SCRIPT_DIR/lib/execute.sh"
source "$SCRIPT_DIR/lib/help.sh"
source "$SCRIPT_DIR/lib/models.sh"
source "$SCRIPT_DIR/lib/openai.sh"
source "$SCRIPT_DIR/lib/output.sh"
source "$SCRIPT_DIR/lib/pipeline.sh"
source "$SCRIPT_DIR/lib/run.sh"
source "$SCRIPT_DIR/lib/task.sh"
source "$SCRIPT_DIR/lib/utils.sh"
# =============================================================================
# Execution Mode Flags (set by CLI, checked during execution)
# =============================================================================
DRY_RUN="${WIREFLOW_DRY_RUN:-false}"
STREAM_MODE="false" # run.sh keeps false, task.sh sets true at start
COUNT_TOKENS="false" # --count-tokens flag
AUTO_DEPS="true" # --no-auto-deps sets false (run mode only)
# =============================================================================
# Global Configuration Setup -- Builtin Defaults
# =============================================================================
# Config cascade: Set builtin values for config parameters
# Model profile system: PROFILE selects tier, MODEL_* defines each tier's model
# MODEL (if non-empty) overrides the profile system entirely
BUILTIN_PROFILE="balanced"
BUILTIN_MODEL_FAST="claude-haiku-4-5"
BUILTIN_MODEL_BALANCED="claude-sonnet-4-5"
BUILTIN_MODEL_DEEP="claude-opus-4-5"
BUILTIN_MODEL="" # Empty = use profile system; non-empty = explicit override
# Extended thinking parameters
BUILTIN_ENABLE_THINKING="false"
BUILTIN_THINKING_BUDGET=10000
# Effort parameter (Opus 4.5 only; high = API default)
BUILTIN_EFFORT="high"
# Provider selection: anthropic (default) or openai (for OpenAI-compatible servers)
BUILTIN_PROVIDER="anthropic"
# OpenAI-compatible provider settings
BUILTIN_OPENAI_BASE_URL=""
BUILTIN_OPENAI_API_KEY="fake-api-key"
BUILTIN_OPENAI_MODEL=""
BUILTIN_OPENAI_MODEL_FAST=""
BUILTIN_OPENAI_MODEL_BALANCED=""
BUILTIN_OPENAI_MODEL_DEEP=""
# Other API parameters
BUILTIN_TEMPERATURE=1.0
BUILTIN_MAX_TOKENS=16000
BUILTIN_ENABLE_CITATIONS="false"
BUILTIN_OUTPUT_FORMAT="md"
BUILTIN_SYSTEM_PROMPTS=(base)
# Initialize config source tracking:
# builtin → global → ancestor → project → workflow → CLI
declare -A CONFIG_SOURCE_MAP
CONFIG_KEYS=(
"PROFILE" "PROVIDER"
"MODEL" "MODEL_BALANCED" "MODEL_FAST" "MODEL_DEEP"
"ENABLE_THINKING" "THINKING_BUDGET" "EFFORT" "ENABLE_CITATIONS"
"TEMPERATURE" "MAX_TOKENS"
"SYSTEM_PROMPTS" "OUTPUT_FORMAT"
"OPENAI_BASE_URL" "OPENAI_API_KEY"
"OPENAI_MODEL" "OPENAI_MODEL_BALANCED" "OPENAI_MODEL_FAST" "OPENAI_MODEL_DEEP"
)
for key in "${CONFIG_KEYS[@]}"; do
builtin_key="BUILTIN_$key"
# Check if builtin is an array
if declare -p "$builtin_key" 2>/dev/null | grep -q '^declare -a'; then
# Copy array from builtin
declare -n builtin_arr="$builtin_key"
declare -n target_arr="$key"
target_arr=("${builtin_arr[@]}")
unset -n builtin_arr target_arr
else
# Copy scalar from builtin
printf -v "$key" '%s' "${!builtin_key}"
fi
CONFIG_SOURCE_MAP[$key]="builtin"
done
# Global config directory and file paths (respect pre-set values for testing)
GLOBAL_CONFIG_DIR="${GLOBAL_CONFIG_DIR:-${XDG_CONFIG_HOME:-$HOME/.config}/wireflow}"
GLOBAL_CONFIG_FILE="${GLOBAL_CONFIG_FILE:-$GLOBAL_CONFIG_DIR/config}"
# =============================================================================
# User-Environment Variables Setup
# =============================================================================
# Set builtin values for user-env variables
BUILTIN_WIREFLOW_PROMPT_PREFIX="$GLOBAL_CONFIG_DIR/prompts/system"
BUILTIN_WIREFLOW_TASK_PREFIX="$GLOBAL_CONFIG_DIR/prompts/tasks"
BUILTIN_ANTHROPIC_API_KEY=""
# Initialize user-env source tracking
declare -A USER_ENV_SOURCE_MAP
USER_ENV_KEYS=(
"WIREFLOW_PROMPT_PREFIX" "WIREFLOW_TASK_PREFIX" "ANTHROPIC_API_KEY"
)
for key in "${USER_ENV_KEYS[@]}"; do
if [[ -z "${!key}" ]]; then
builtin_key="BUILTIN_$key"
# Check if builtin is an array
if declare -p "$builtin_key" 2>/dev/null | grep -q '^declare -a'; then
# Copy array from builtin
declare -n builtin_arr="$builtin_key"
declare -n target_arr="$key"
target_arr=("${builtin_arr[@]}")
unset -n builtin_arr target_arr
else
# Copy scalar from builtin
printf -v "$key" '%s' "${!builtin_key}"
fi
USER_ENV_SOURCE_MAP[$key]="builtin"
else
# Value is already set in the shell environment
USER_ENV_SOURCE_MAP[$key]="env"
fi
done
# =============================================================================
# Initiate Config Cascade - Create/Load Global Config
# =============================================================================
ensure_global_config || true
load_global_config
# =============================================================================
# Project-Level Configuration Setup
# =============================================================================
# Initialize project-level variables
# CONTEXT: Array of context file paths (globs expand at config source time)
declare -a CONTEXT=()
# Initialize project-level source tracking:
# project → workflow → CLI
declare -A PROJECT_SOURCE_MAP
PROJECT_KEYS=(
"CONTEXT"
)
for key in "${PROJECT_KEYS[@]}"; do
PROJECT_SOURCE_MAP[$key]="unset"
done
# =============================================================================
# Workflow-Specific Configuration Setup
# =============================================================================
# Initialize workflow-specific variables
declare -a DEPENDS_ON=()
declare -a INPUT=()
declare EXPORT_PATH=""
declare BATCH_MODE="false"
# Initialize workflow-specific source tracking:
# workflow → CLI
declare -A WORKFLOW_SOURCE_MAP
WORKFLOW_KEYS=(
"DEPENDS_ON"
"INPUT"
"EXPORT_PATH"
"BATCH_MODE"
)
for key in "${WORKFLOW_KEYS[@]}"; do
WORKFLOW_SOURCE_MAP[$key]="unset"
done
# =============================================================================
# Content Block Arrays for API Request Building
# =============================================================================
# These arrays accumulate JSON content blocks during context aggregation
# and are used by execute.sh to build the final API request.
declare -a SYSTEM_BLOCKS=()
declare -a CONTEXT_BLOCKS=()
declare -a DEPENDENCY_BLOCKS=()
declare -a INPUT_BLOCKS=()
declare -a IMAGE_BLOCKS=()
declare -a DOCUMENT_INDEX_MAP=()
# =============================================================================
# Subcommand Dispatcher
# =============================================================================
# Require subcommand
if [[ $# -eq 0 ]]; then
echo "Error: missing subcommand name" >&2
show_help
exit 1
fi
# Handle all quick help calls (-h|--help)
if [[ $# -eq 2 ]] && [[ "$2" == "-h" || "$2" == "--help" ]]; then
if type -t "show_quick_help_$1" &>/dev/null; then
show_quick_help_$1
exit 0
fi
echo "Error: unknown subcommand: $1" >&2
show_help
exit 1
fi
# Parse subcommand
cmd="$1"
shift
case "$cmd" in
init)
init_project "$1"
exit 0
;;
new|edit|config|run|cat|open|list)
# These are all the commands which require a verified project root
PROJECT_ROOT="$(find_project_root)" || {
echo "Error: No project found" >&2
echo "" >&2
echo "Run '$SCRIPT_NAME init' to initialize a project." >&2
echo "Run '$SCRIPT_NAME help init' for details." >&2
exit 1
}
# Project-level paths
WIREFLOW_DIR="$PROJECT_ROOT/.workflow"
PROJECT_CONFIG="$WIREFLOW_DIR/config"
PROJECT_FILE="$WIREFLOW_DIR/project.txt"
OUTPUT_DIR="$WIREFLOW_DIR/output"
RUN_DIR="$WIREFLOW_DIR/run"
CACHE_DIR="$WIREFLOW_DIR/cache"
# Config cascade: Propagate through ancestors and current project
load_ancestor_configs || true
load_project_config || true
case "$cmd" in
new)
while [[ $# -gt 0 ]]; do
case "$1" in
--from-task)
if [[ -z "$2" ]]; then
echo "Error: Task template name required"
show_quick_help_new
exit 1
fi
NEW_FROM_TASK="$2"
shift
shift
;;
*)
# First non-flag argument is workflow name
if [[ -n "${WORKFLOW_NAME:-}" ]]; then
echo "Error: Too many arguments"
show_quick_help_new
exit 1
fi
WORKFLOW_NAME="$1"
shift
esac
done
if [[ -z "${WORKFLOW_NAME:-}" ]]; then
echo "Error: Workflow name required" >&2
show_quick_help_new
exit 1
fi
WORKFLOW_DIR="$RUN_DIR/$WORKFLOW_NAME"
new_workflow "$WORKFLOW_NAME" "$WORKFLOW_DIR" "$NEW_FROM_TASK"
exit 0
;;
edit)
WORKFLOW_NAME="${1:-}"
if [[ -n "$WORKFLOW_NAME" ]]; then
check_workflow_dir
edit_workflow
exit 0
fi
edit_project
exit 0
;;
config)
WORKFLOW_NAME="${1:-}"
if [[ -n "$WORKFLOW_NAME" ]]; then
config_workflow
exit 0
fi
config_project
exit 0
;;
list)
cmd_list
exit 0
;;
run)
if [[ $# -eq 0 ]]; then
echo "Error: Workflow name required" >&2
echo "Usage: $SCRIPT_NAME run <name> [options]" >&2
exit 1
fi
# Delegate to run mode handler
cmd_run "$@"
exit $?
;;
cat)
if [[ -z "$1" ]]; then
echo "Error: Workflow name required" >&2
echo "Usage: $SCRIPT_NAME cat NAME"
exit 1
fi
cat_workflow "$1"
exit 0
;;
open)
if [[ -z "$1" ]]; then
echo "Error: Workflow name required" >&2
echo "Usage: $SCRIPT_NAME open NAME"
exit 1
fi
open_workflow "$1"
exit 0
;;
esac
;;
task)
# Execute named task template
if [[ $# -eq 0 ]]; then
echo "Error: Task name required" >&2
show_quick_help_task
exit 1
fi
# Delegate to task execution handler
cmd_task "$@"
exit $?
;;
batch)
# Batch processing subcommand
cmd_batch "$@"
exit $?
;;
tasks)
# Manage task templates
if [[ $# -eq 0 ]]; then
# No subcommand - default to list
list_tasks
else
subcmd="$1"
shift
case "$subcmd" in
list)
list_tasks
;;
show)
if [[ $# -eq 0 ]]; then
echo "Error: Task name required" >&2
echo "Usage: $SCRIPT_NAME tasks show <name>" >&2
exit 1
fi
show_task "$1"
;;
edit)
if [[ $# -eq 0 ]]; then
echo "Error: Task name required" >&2
echo "Usage: $SCRIPT_NAME tasks edit <name>" >&2
exit 1
fi
edit_task "$1"
;;
*)
echo "Error: Unknown tasks subcommand: '$subcmd'" >&2
show_quick_help_tasks
exit 1
;;
esac
fi
exit 0
;;
prompts)
# Manage system prompt files
if [[ $# -eq 0 ]]; then
# No subcommand - default to list
list_prompts
else
subcmd="$1"
shift
case "$subcmd" in
list)
list_prompts
;;
show)
if [[ $# -eq 0 ]]; then
echo "Error: Prompt name required" >&2
echo "Usage: $SCRIPT_NAME prompts show <name>" >&2
exit 1
fi
show_prompt "$1"
;;
edit)
if [[ $# -eq 0 ]]; then
echo "Error: Prompt name required" >&2
echo "Usage: $SCRIPT_NAME prompts edit <name>" >&2
exit 1
fi
edit_prompt "$1"
;;
*)
echo "Error: Unknown prompts subcommand: '$subcmd'" >&2
show_quick_help_prompts
exit 1
;;
esac
fi
exit 0
;;
models)
# Display provider settings and available models
cmd_models "$@"
exit $?
;;
shell)
# Shell integration (install wfw + completions)
cmd_shell "$@"
exit $?
;;
find-project-root)
find_project_root
exit $?
;;
help)
if [[ -z "$1" ]]; then
show_help
exit 0
fi
# Dispatch subcommand help functions
case "$1" in
init) show_help_init ;;
new) show_help_new ;;
edit) show_help_edit ;;
config) show_help_config ;;
run) show_help_run ;;
task) show_help_task ;;
batch) show_help_batch ;;
tasks) show_help_tasks ;;
prompts) show_help_prompts ;;
models) show_help_models ;;
cat) show_help_cat ;;
open) show_help_open ;;
list) show_help_list ;;
shell) show_help_shell ;;
help) show_help ;;
*)
echo "Error: Unknown subcommand: $1"
show_help
exit 1
;;
esac
exit 0
;;
--help|-h)
show_help
exit 0
;;
--version|-v)
echo "wireflow version $WIREFLOW_VERSION"
exit 0
;;
*)
echo "Error: Unknown subcommand: $cmd" >&2
echo "Run '$SCRIPT_NAME help' to see available subcommands." >&2
exit 1
;;
esac