cocoindex 0.2.3__cp311-abi3-manylinux_2_28_aarch64.whl
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.
- cocoindex/__init__.py +92 -0
- cocoindex/_engine.abi3.so +0 -0
- cocoindex/auth_registry.py +51 -0
- cocoindex/cli.py +697 -0
- cocoindex/convert.py +621 -0
- cocoindex/flow.py +1205 -0
- cocoindex/functions.py +357 -0
- cocoindex/index.py +29 -0
- cocoindex/lib.py +32 -0
- cocoindex/llm.py +46 -0
- cocoindex/op.py +628 -0
- cocoindex/py.typed +0 -0
- cocoindex/runtime.py +37 -0
- cocoindex/setting.py +181 -0
- cocoindex/setup.py +92 -0
- cocoindex/sources.py +102 -0
- cocoindex/subprocess_exec.py +279 -0
- cocoindex/targets.py +135 -0
- cocoindex/tests/__init__.py +0 -0
- cocoindex/tests/conftest.py +38 -0
- cocoindex/tests/test_convert.py +1543 -0
- cocoindex/tests/test_optional_database.py +249 -0
- cocoindex/tests/test_transform_flow.py +207 -0
- cocoindex/tests/test_typing.py +429 -0
- cocoindex/tests/test_validation.py +134 -0
- cocoindex/typing.py +473 -0
- cocoindex/user_app_loader.py +51 -0
- cocoindex/utils.py +20 -0
- cocoindex/validation.py +104 -0
- cocoindex-0.2.3.dist-info/METADATA +262 -0
- cocoindex-0.2.3.dist-info/RECORD +34 -0
- cocoindex-0.2.3.dist-info/WHEEL +4 -0
- cocoindex-0.2.3.dist-info/entry_points.txt +2 -0
- cocoindex-0.2.3.dist-info/licenses/LICENSE +201 -0
cocoindex/cli.py
ADDED
@@ -0,0 +1,697 @@
|
|
1
|
+
import atexit
|
2
|
+
import asyncio
|
3
|
+
import datetime
|
4
|
+
import importlib.util
|
5
|
+
import os
|
6
|
+
import signal
|
7
|
+
import threading
|
8
|
+
from types import FrameType
|
9
|
+
from typing import Any, Iterable
|
10
|
+
|
11
|
+
import click
|
12
|
+
import watchfiles
|
13
|
+
from dotenv import find_dotenv, load_dotenv
|
14
|
+
from rich.console import Console
|
15
|
+
from rich.panel import Panel
|
16
|
+
from rich.table import Table
|
17
|
+
|
18
|
+
from . import flow, lib, setting
|
19
|
+
from .setup import flow_names_with_setup
|
20
|
+
from .runtime import execution_context
|
21
|
+
from .subprocess_exec import add_user_app
|
22
|
+
from .user_app_loader import load_user_app
|
23
|
+
|
24
|
+
# Create ServerSettings lazily upon first call, as environment variables may be loaded from files, etc.
|
25
|
+
COCOINDEX_HOST = "https://cocoindex.io"
|
26
|
+
|
27
|
+
|
28
|
+
def _parse_app_flow_specifier(specifier: str) -> tuple[str, str | None]:
|
29
|
+
"""Parses 'module_or_path[:flow_name]' into (module_or_path, flow_name | None)."""
|
30
|
+
parts = specifier.split(":", 1) # Split only on the first colon
|
31
|
+
app_ref = parts[0]
|
32
|
+
|
33
|
+
if not app_ref:
|
34
|
+
raise click.BadParameter(
|
35
|
+
f"Application module/path part is missing or invalid in specifier: '{specifier}'. "
|
36
|
+
"Expected format like 'myapp.py' or 'myapp:MyFlow'.",
|
37
|
+
param_hint="APP_SPECIFIER",
|
38
|
+
)
|
39
|
+
|
40
|
+
if len(parts) == 1:
|
41
|
+
return app_ref, None
|
42
|
+
|
43
|
+
flow_ref_part = parts[1]
|
44
|
+
|
45
|
+
if not flow_ref_part: # Handles empty string after colon
|
46
|
+
return app_ref, None
|
47
|
+
|
48
|
+
if not flow_ref_part.isidentifier():
|
49
|
+
raise click.BadParameter(
|
50
|
+
f"Invalid format for flow name part ('{flow_ref_part}') in specifier '{specifier}'. "
|
51
|
+
"If a colon separates the application from the flow name, the flow name should typically be "
|
52
|
+
"a valid identifier (e.g., alphanumeric with underscores, not starting with a number).",
|
53
|
+
param_hint="APP_SPECIFIER",
|
54
|
+
)
|
55
|
+
return app_ref, flow_ref_part
|
56
|
+
|
57
|
+
|
58
|
+
def _get_app_ref_from_specifier(
|
59
|
+
specifier: str,
|
60
|
+
) -> str:
|
61
|
+
"""
|
62
|
+
Parses the APP_TARGET to get the application reference (path or module).
|
63
|
+
Issues a warning if a flow name component is also provided in it.
|
64
|
+
"""
|
65
|
+
app_ref, flow_ref = _parse_app_flow_specifier(specifier)
|
66
|
+
|
67
|
+
if flow_ref is not None:
|
68
|
+
click.echo(
|
69
|
+
click.style(
|
70
|
+
f"Ignoring flow name '{flow_ref}' in '{specifier}': "
|
71
|
+
f"this command operates on the entire app/module '{app_ref}'.",
|
72
|
+
fg="yellow",
|
73
|
+
),
|
74
|
+
err=True,
|
75
|
+
)
|
76
|
+
return app_ref
|
77
|
+
|
78
|
+
|
79
|
+
def _load_user_app(app_target: str) -> None:
|
80
|
+
load_user_app(app_target)
|
81
|
+
add_user_app(app_target)
|
82
|
+
|
83
|
+
|
84
|
+
def _initialize_cocoindex_in_process() -> None:
|
85
|
+
settings = setting.Settings.from_env()
|
86
|
+
lib.init(settings)
|
87
|
+
atexit.register(lib.stop)
|
88
|
+
|
89
|
+
|
90
|
+
@click.group()
|
91
|
+
@click.version_option(package_name="cocoindex", message="%(prog)s version %(version)s")
|
92
|
+
@click.option(
|
93
|
+
"--env-file",
|
94
|
+
type=click.Path(
|
95
|
+
exists=True, file_okay=True, dir_okay=False, readable=True, resolve_path=True
|
96
|
+
),
|
97
|
+
help="Path to a .env file to load environment variables from. "
|
98
|
+
"If not provided, attempts to load '.env' from the current directory.",
|
99
|
+
default=None,
|
100
|
+
show_default=False,
|
101
|
+
)
|
102
|
+
def cli(env_file: str | None = None) -> None:
|
103
|
+
"""
|
104
|
+
CLI for Cocoindex.
|
105
|
+
"""
|
106
|
+
dotenv_path = env_file or find_dotenv(usecwd=True)
|
107
|
+
|
108
|
+
if load_dotenv(dotenv_path=dotenv_path):
|
109
|
+
loaded_env_path = os.path.abspath(dotenv_path)
|
110
|
+
click.echo(f"Loaded environment variables from: {loaded_env_path}\n", err=True)
|
111
|
+
|
112
|
+
try:
|
113
|
+
_initialize_cocoindex_in_process()
|
114
|
+
except Exception as e:
|
115
|
+
raise click.ClickException(f"Failed to initialize CocoIndex library: {e}")
|
116
|
+
|
117
|
+
|
118
|
+
@cli.command()
|
119
|
+
@click.argument("app_target", type=str, required=False)
|
120
|
+
def ls(app_target: str | None) -> None:
|
121
|
+
"""
|
122
|
+
List all flows.
|
123
|
+
|
124
|
+
If APP_TARGET (path/to/app.py or a module) is provided, lists flows
|
125
|
+
defined in the app and their backend setup status.
|
126
|
+
|
127
|
+
If APP_TARGET is omitted, lists all flows that have a persisted
|
128
|
+
setup in the backend.
|
129
|
+
"""
|
130
|
+
persisted_flow_names = flow_names_with_setup()
|
131
|
+
if app_target:
|
132
|
+
app_ref = _get_app_ref_from_specifier(app_target)
|
133
|
+
_load_user_app(app_ref)
|
134
|
+
|
135
|
+
current_flow_names = set(flow.flow_names())
|
136
|
+
|
137
|
+
if not current_flow_names:
|
138
|
+
click.echo(f"No flows are defined in '{app_ref}'.")
|
139
|
+
return
|
140
|
+
|
141
|
+
has_missing = False
|
142
|
+
persisted_flow_names_set = set(persisted_flow_names)
|
143
|
+
for name in sorted(current_flow_names):
|
144
|
+
if name in persisted_flow_names_set:
|
145
|
+
click.echo(name)
|
146
|
+
else:
|
147
|
+
click.echo(f"{name} [+]")
|
148
|
+
has_missing = True
|
149
|
+
|
150
|
+
if has_missing:
|
151
|
+
click.echo("")
|
152
|
+
click.echo("Notes:")
|
153
|
+
click.echo(
|
154
|
+
" [+]: Flows present in the current process, but missing setup."
|
155
|
+
)
|
156
|
+
|
157
|
+
else:
|
158
|
+
if not persisted_flow_names:
|
159
|
+
click.echo("No persisted flow setups found in the backend.")
|
160
|
+
return
|
161
|
+
|
162
|
+
for name in sorted(persisted_flow_names):
|
163
|
+
click.echo(name)
|
164
|
+
|
165
|
+
|
166
|
+
@cli.command()
|
167
|
+
@click.argument("app_flow_specifier", type=str)
|
168
|
+
@click.option(
|
169
|
+
"--color/--no-color", default=True, help="Enable or disable colored output."
|
170
|
+
)
|
171
|
+
@click.option("--verbose", is_flag=True, help="Show verbose output with full details.")
|
172
|
+
def show(app_flow_specifier: str, color: bool, verbose: bool) -> None:
|
173
|
+
"""
|
174
|
+
Show the flow spec and schema.
|
175
|
+
|
176
|
+
APP_FLOW_SPECIFIER: Specifies the application and optionally the target flow.
|
177
|
+
Can be one of the following formats:
|
178
|
+
|
179
|
+
\b
|
180
|
+
- path/to/your_app.py
|
181
|
+
- an_installed.module_name
|
182
|
+
- path/to/your_app.py:SpecificFlowName
|
183
|
+
- an_installed.module_name:SpecificFlowName
|
184
|
+
|
185
|
+
:SpecificFlowName can be omitted only if the application defines a single flow.
|
186
|
+
"""
|
187
|
+
app_ref, flow_ref = _parse_app_flow_specifier(app_flow_specifier)
|
188
|
+
_load_user_app(app_ref)
|
189
|
+
|
190
|
+
fl = _flow_by_name(flow_ref)
|
191
|
+
console = Console(no_color=not color)
|
192
|
+
console.print(fl._render_spec(verbose=verbose))
|
193
|
+
console.print()
|
194
|
+
table = Table(
|
195
|
+
title=f"Schema for Flow: {fl.name}",
|
196
|
+
title_style="cyan",
|
197
|
+
header_style="bold magenta",
|
198
|
+
)
|
199
|
+
table.add_column("Field", style="cyan")
|
200
|
+
table.add_column("Type", style="green")
|
201
|
+
table.add_column("Attributes", style="yellow")
|
202
|
+
for field_name, field_type, attr_str in fl._get_schema():
|
203
|
+
table.add_row(field_name, field_type, attr_str)
|
204
|
+
console.print(table)
|
205
|
+
|
206
|
+
|
207
|
+
def _setup_flows(
|
208
|
+
flow_iter: Iterable[flow.Flow],
|
209
|
+
*,
|
210
|
+
force: bool,
|
211
|
+
quiet: bool = False,
|
212
|
+
always_show_setup: bool = False,
|
213
|
+
) -> None:
|
214
|
+
setup_bundle = flow.make_setup_bundle(flow_iter)
|
215
|
+
description, is_up_to_date = setup_bundle.describe()
|
216
|
+
if always_show_setup or not is_up_to_date:
|
217
|
+
click.echo(description)
|
218
|
+
if is_up_to_date:
|
219
|
+
if not quiet:
|
220
|
+
click.echo("Setup is already up to date.")
|
221
|
+
return
|
222
|
+
if not force and not click.confirm(
|
223
|
+
"Changes need to be pushed. Continue? [yes/N]",
|
224
|
+
default=False,
|
225
|
+
show_default=False,
|
226
|
+
):
|
227
|
+
return
|
228
|
+
setup_bundle.apply(report_to_stdout=not quiet)
|
229
|
+
|
230
|
+
|
231
|
+
def _show_no_live_update_hint() -> None:
|
232
|
+
click.secho(
|
233
|
+
"NOTE: No change capture mechanism exists. See https://cocoindex.io/docs/core/flow_methods#live-update for more details.\n",
|
234
|
+
fg="yellow",
|
235
|
+
)
|
236
|
+
|
237
|
+
|
238
|
+
async def _update_all_flows_with_hint_async(
|
239
|
+
options: flow.FlowLiveUpdaterOptions,
|
240
|
+
) -> None:
|
241
|
+
await flow.update_all_flows_async(options)
|
242
|
+
if options.live_mode:
|
243
|
+
_show_no_live_update_hint()
|
244
|
+
|
245
|
+
|
246
|
+
@cli.command()
|
247
|
+
@click.argument("app_target", type=str)
|
248
|
+
@click.option(
|
249
|
+
"-f",
|
250
|
+
"--force",
|
251
|
+
is_flag=True,
|
252
|
+
show_default=True,
|
253
|
+
default=False,
|
254
|
+
help="Force setup without confirmation prompts.",
|
255
|
+
)
|
256
|
+
def setup(app_target: str, force: bool) -> None:
|
257
|
+
"""
|
258
|
+
Check and apply backend setup changes for flows, including the internal storage and target (to export to).
|
259
|
+
|
260
|
+
APP_TARGET: path/to/app.py or installed_module.
|
261
|
+
"""
|
262
|
+
app_ref = _get_app_ref_from_specifier(app_target)
|
263
|
+
_load_user_app(app_ref)
|
264
|
+
_setup_flows(flow.flows().values(), force=force, always_show_setup=True)
|
265
|
+
|
266
|
+
|
267
|
+
@cli.command("drop")
|
268
|
+
@click.argument("app_target", type=str, required=False)
|
269
|
+
@click.argument("flow_name", type=str, nargs=-1)
|
270
|
+
@click.option(
|
271
|
+
"-f",
|
272
|
+
"--force",
|
273
|
+
is_flag=True,
|
274
|
+
show_default=True,
|
275
|
+
default=False,
|
276
|
+
help="Force drop without confirmation prompts.",
|
277
|
+
)
|
278
|
+
def drop(app_target: str | None, flow_name: tuple[str, ...], force: bool) -> None:
|
279
|
+
"""
|
280
|
+
Drop the backend setup for flows.
|
281
|
+
|
282
|
+
\b
|
283
|
+
Modes of operation:
|
284
|
+
1. Drop all flows defined in an app: `cocoindex drop <APP_TARGET>`
|
285
|
+
2. Drop specific named flows: `cocoindex drop <APP_TARGET> [FLOW_NAME...]`
|
286
|
+
"""
|
287
|
+
app_ref = None
|
288
|
+
|
289
|
+
if not app_target:
|
290
|
+
raise click.UsageError(
|
291
|
+
"Missing arguments. You must either provide an APP_TARGET (to target app-specific flows) "
|
292
|
+
"or use the --all flag."
|
293
|
+
)
|
294
|
+
|
295
|
+
app_ref = _get_app_ref_from_specifier(app_target)
|
296
|
+
_load_user_app(app_ref)
|
297
|
+
|
298
|
+
flows: Iterable[flow.Flow]
|
299
|
+
if flow_name:
|
300
|
+
flows = []
|
301
|
+
for name in flow_name:
|
302
|
+
try:
|
303
|
+
flows.append(flow.flow_by_name(name))
|
304
|
+
except KeyError:
|
305
|
+
click.echo(
|
306
|
+
f"Warning: Failed to get flow `{name}`. Ignored.",
|
307
|
+
err=True,
|
308
|
+
)
|
309
|
+
else:
|
310
|
+
flows = flow.flows().values()
|
311
|
+
|
312
|
+
flow_full_names = ", ".join(fl.full_name for fl in flows)
|
313
|
+
click.echo(
|
314
|
+
f"Preparing to drop specified flows: {flow_full_names} (in '{app_ref}').",
|
315
|
+
err=True,
|
316
|
+
)
|
317
|
+
|
318
|
+
if not flows:
|
319
|
+
click.echo("No flows identified for the drop operation.")
|
320
|
+
return
|
321
|
+
|
322
|
+
setup_bundle = flow.make_drop_bundle(flows)
|
323
|
+
description, is_up_to_date = setup_bundle.describe()
|
324
|
+
click.echo(description)
|
325
|
+
if is_up_to_date:
|
326
|
+
click.echo("No flows need to be dropped.")
|
327
|
+
return
|
328
|
+
if not force and not click.confirm(
|
329
|
+
f"\nThis will apply changes to drop setup for: {flow_full_names}. Continue? [yes/N]",
|
330
|
+
default=False,
|
331
|
+
show_default=False,
|
332
|
+
):
|
333
|
+
click.echo("Drop operation aborted by user.")
|
334
|
+
return
|
335
|
+
setup_bundle.apply(report_to_stdout=True)
|
336
|
+
|
337
|
+
|
338
|
+
@cli.command()
|
339
|
+
@click.argument("app_flow_specifier", type=str)
|
340
|
+
@click.option(
|
341
|
+
"-L",
|
342
|
+
"--live",
|
343
|
+
is_flag=True,
|
344
|
+
show_default=True,
|
345
|
+
default=False,
|
346
|
+
help="Continuously watch changes from data sources and apply to the target index.",
|
347
|
+
)
|
348
|
+
@click.option(
|
349
|
+
"--setup",
|
350
|
+
is_flag=True,
|
351
|
+
show_default=True,
|
352
|
+
default=False,
|
353
|
+
help="Automatically setup backends for the flow if it's not setup yet.",
|
354
|
+
)
|
355
|
+
@click.option(
|
356
|
+
"-f",
|
357
|
+
"--force",
|
358
|
+
is_flag=True,
|
359
|
+
show_default=True,
|
360
|
+
default=False,
|
361
|
+
help="Force setup without confirmation prompts.",
|
362
|
+
)
|
363
|
+
@click.option(
|
364
|
+
"-q",
|
365
|
+
"--quiet",
|
366
|
+
is_flag=True,
|
367
|
+
show_default=True,
|
368
|
+
default=False,
|
369
|
+
help="Avoid printing anything to the standard output, e.g. statistics.",
|
370
|
+
)
|
371
|
+
def update(
|
372
|
+
app_flow_specifier: str,
|
373
|
+
live: bool,
|
374
|
+
setup: bool, # pylint: disable=redefined-outer-name
|
375
|
+
force: bool,
|
376
|
+
quiet: bool,
|
377
|
+
) -> None:
|
378
|
+
"""
|
379
|
+
Update the index to reflect the latest data from data sources.
|
380
|
+
|
381
|
+
APP_FLOW_SPECIFIER: path/to/app.py, module, path/to/app.py:FlowName, or module:FlowName.
|
382
|
+
If :FlowName is omitted, updates all flows.
|
383
|
+
"""
|
384
|
+
app_ref, flow_name = _parse_app_flow_specifier(app_flow_specifier)
|
385
|
+
_load_user_app(app_ref)
|
386
|
+
|
387
|
+
if live:
|
388
|
+
click.secho(
|
389
|
+
"NOTE: Flow code changes will NOT be reflected until you restart to load the new code.\n",
|
390
|
+
fg="yellow",
|
391
|
+
)
|
392
|
+
|
393
|
+
options = flow.FlowLiveUpdaterOptions(live_mode=live, print_stats=not quiet)
|
394
|
+
if flow_name is None:
|
395
|
+
if setup:
|
396
|
+
_setup_flows(
|
397
|
+
flow.flows().values(),
|
398
|
+
force=force,
|
399
|
+
quiet=quiet,
|
400
|
+
)
|
401
|
+
execution_context.run(_update_all_flows_with_hint_async(options))
|
402
|
+
else:
|
403
|
+
fl = flow.flow_by_name(flow_name)
|
404
|
+
if setup:
|
405
|
+
_setup_flows((fl,), force=force, quiet=quiet)
|
406
|
+
with flow.FlowLiveUpdater(fl, options) as updater:
|
407
|
+
updater.wait()
|
408
|
+
if options.live_mode:
|
409
|
+
_show_no_live_update_hint()
|
410
|
+
|
411
|
+
|
412
|
+
@cli.command()
|
413
|
+
@click.argument("app_flow_specifier", type=str)
|
414
|
+
@click.option(
|
415
|
+
"-o",
|
416
|
+
"--output-dir",
|
417
|
+
type=str,
|
418
|
+
required=False,
|
419
|
+
help="The directory to dump the output to.",
|
420
|
+
)
|
421
|
+
@click.option(
|
422
|
+
"--cache/--no-cache",
|
423
|
+
is_flag=True,
|
424
|
+
show_default=True,
|
425
|
+
default=True,
|
426
|
+
help="Use already-cached intermediate data if available.",
|
427
|
+
)
|
428
|
+
def evaluate(
|
429
|
+
app_flow_specifier: str, output_dir: str | None, cache: bool = True
|
430
|
+
) -> None:
|
431
|
+
"""
|
432
|
+
Evaluate the flow and dump flow outputs to files.
|
433
|
+
|
434
|
+
Instead of updating the index, it dumps what should be indexed to files.
|
435
|
+
Mainly used for evaluation purpose.
|
436
|
+
|
437
|
+
\b
|
438
|
+
APP_FLOW_SPECIFIER: Specifies the application and optionally the target flow.
|
439
|
+
Can be one of the following formats:
|
440
|
+
- path/to/your_app.py
|
441
|
+
- an_installed.module_name
|
442
|
+
- path/to/your_app.py:SpecificFlowName
|
443
|
+
- an_installed.module_name:SpecificFlowName
|
444
|
+
|
445
|
+
:SpecificFlowName can be omitted only if the application defines a single flow.
|
446
|
+
"""
|
447
|
+
app_ref, flow_ref = _parse_app_flow_specifier(app_flow_specifier)
|
448
|
+
_load_user_app(app_ref)
|
449
|
+
|
450
|
+
fl = _flow_by_name(flow_ref)
|
451
|
+
if output_dir is None:
|
452
|
+
output_dir = f"eval_{setting.get_app_namespace(trailing_delimiter='_')}{fl.name}_{datetime.datetime.now().strftime('%y%m%d_%H%M%S')}"
|
453
|
+
options = flow.EvaluateAndDumpOptions(output_dir=output_dir, use_cache=cache)
|
454
|
+
fl.evaluate_and_dump(options)
|
455
|
+
|
456
|
+
|
457
|
+
@cli.command()
|
458
|
+
@click.argument("app_target", type=str)
|
459
|
+
@click.option(
|
460
|
+
"-a",
|
461
|
+
"--address",
|
462
|
+
type=str,
|
463
|
+
help="The address to bind the server to, in the format of IP:PORT. "
|
464
|
+
"If unspecified, the address specified in COCOINDEX_SERVER_ADDRESS will be used.",
|
465
|
+
)
|
466
|
+
@click.option(
|
467
|
+
"-c",
|
468
|
+
"--cors-origin",
|
469
|
+
type=str,
|
470
|
+
help="The origins of the clients (e.g. CocoInsight UI) to allow CORS from. "
|
471
|
+
"Multiple origins can be specified as a comma-separated list. "
|
472
|
+
"e.g. `https://cocoindex.io,http://localhost:3000`. "
|
473
|
+
"Origins specified in COCOINDEX_SERVER_CORS_ORIGINS will also be included.",
|
474
|
+
)
|
475
|
+
@click.option(
|
476
|
+
"-ci",
|
477
|
+
"--cors-cocoindex",
|
478
|
+
is_flag=True,
|
479
|
+
show_default=True,
|
480
|
+
default=False,
|
481
|
+
help=f"Allow {COCOINDEX_HOST} to access the server.",
|
482
|
+
)
|
483
|
+
@click.option(
|
484
|
+
"-cl",
|
485
|
+
"--cors-local",
|
486
|
+
type=int,
|
487
|
+
help="Allow http://localhost:<port> to access the server.",
|
488
|
+
)
|
489
|
+
@click.option(
|
490
|
+
"-L",
|
491
|
+
"--live-update",
|
492
|
+
is_flag=True,
|
493
|
+
show_default=True,
|
494
|
+
default=False,
|
495
|
+
help="Continuously watch changes from data sources and apply to the target index.",
|
496
|
+
)
|
497
|
+
@click.option(
|
498
|
+
"--setup",
|
499
|
+
is_flag=True,
|
500
|
+
show_default=True,
|
501
|
+
default=False,
|
502
|
+
help="Automatically setup backends for the flow if it's not setup yet.",
|
503
|
+
)
|
504
|
+
@click.option(
|
505
|
+
"-f",
|
506
|
+
"--force",
|
507
|
+
is_flag=True,
|
508
|
+
show_default=True,
|
509
|
+
default=False,
|
510
|
+
help="Force setup without confirmation prompts.",
|
511
|
+
)
|
512
|
+
@click.option(
|
513
|
+
"-q",
|
514
|
+
"--quiet",
|
515
|
+
is_flag=True,
|
516
|
+
show_default=True,
|
517
|
+
default=False,
|
518
|
+
help="Avoid printing anything to the standard output, e.g. statistics.",
|
519
|
+
)
|
520
|
+
@click.option(
|
521
|
+
"-r",
|
522
|
+
"--reload",
|
523
|
+
is_flag=True,
|
524
|
+
show_default=True,
|
525
|
+
default=False,
|
526
|
+
help="Enable auto-reload on code changes.",
|
527
|
+
)
|
528
|
+
def server(
|
529
|
+
app_target: str,
|
530
|
+
address: str | None,
|
531
|
+
live_update: bool,
|
532
|
+
setup: bool, # pylint: disable=redefined-outer-name
|
533
|
+
force: bool,
|
534
|
+
quiet: bool,
|
535
|
+
cors_origin: str | None,
|
536
|
+
cors_cocoindex: bool,
|
537
|
+
cors_local: int | None,
|
538
|
+
reload: bool,
|
539
|
+
) -> None:
|
540
|
+
"""
|
541
|
+
Start a HTTP server providing REST APIs.
|
542
|
+
|
543
|
+
It will allow tools like CocoInsight to access the server.
|
544
|
+
|
545
|
+
APP_TARGET: path/to/app.py or installed_module.
|
546
|
+
"""
|
547
|
+
app_ref = _get_app_ref_from_specifier(app_target)
|
548
|
+
args = (
|
549
|
+
app_ref,
|
550
|
+
address,
|
551
|
+
cors_origin,
|
552
|
+
cors_cocoindex,
|
553
|
+
cors_local,
|
554
|
+
live_update,
|
555
|
+
setup,
|
556
|
+
force,
|
557
|
+
quiet,
|
558
|
+
)
|
559
|
+
|
560
|
+
if reload:
|
561
|
+
watch_paths = {os.getcwd()}
|
562
|
+
if os.path.isfile(app_ref):
|
563
|
+
watch_paths.add(os.path.dirname(os.path.abspath(app_ref)))
|
564
|
+
else:
|
565
|
+
try:
|
566
|
+
spec = importlib.util.find_spec(app_ref)
|
567
|
+
if spec and spec.origin:
|
568
|
+
watch_paths.add(os.path.dirname(os.path.abspath(spec.origin)))
|
569
|
+
except ImportError:
|
570
|
+
pass
|
571
|
+
|
572
|
+
watchfiles.run_process(
|
573
|
+
*watch_paths,
|
574
|
+
target=_reloadable_server_target,
|
575
|
+
args=args,
|
576
|
+
watch_filter=watchfiles.PythonFilter(),
|
577
|
+
callback=lambda changes: click.secho(
|
578
|
+
f"\nDetected changes in {len(changes)} file(s), reloading server...\n",
|
579
|
+
fg="cyan",
|
580
|
+
),
|
581
|
+
)
|
582
|
+
else:
|
583
|
+
click.secho(
|
584
|
+
"NOTE: Flow code changes will NOT be reflected until you restart to load the new code. Use --reload to enable auto-reload.\n",
|
585
|
+
fg="yellow",
|
586
|
+
)
|
587
|
+
_run_server(*args)
|
588
|
+
|
589
|
+
|
590
|
+
def _reloadable_server_target(*args: Any, **kwargs: Any) -> None:
|
591
|
+
"""Reloadable target for the watchfiles process."""
|
592
|
+
_initialize_cocoindex_in_process()
|
593
|
+
_run_server(*args, **kwargs)
|
594
|
+
|
595
|
+
|
596
|
+
def _run_server(
|
597
|
+
app_ref: str,
|
598
|
+
address: str | None = None,
|
599
|
+
cors_origin: str | None = None,
|
600
|
+
cors_cocoindex: bool = False,
|
601
|
+
cors_local: int | None = None,
|
602
|
+
live_update: bool = False,
|
603
|
+
run_setup: bool = False,
|
604
|
+
force: bool = False,
|
605
|
+
quiet: bool = False,
|
606
|
+
) -> None:
|
607
|
+
"""Helper function to run the server with specified settings."""
|
608
|
+
_load_user_app(app_ref)
|
609
|
+
|
610
|
+
server_settings = setting.ServerSettings.from_env()
|
611
|
+
cors_origins: set[str] = set(server_settings.cors_origins or [])
|
612
|
+
if cors_origin is not None:
|
613
|
+
cors_origins.update(setting.ServerSettings.parse_cors_origins(cors_origin))
|
614
|
+
if cors_cocoindex:
|
615
|
+
cors_origins.add(COCOINDEX_HOST)
|
616
|
+
if cors_local is not None:
|
617
|
+
cors_origins.add(f"http://localhost:{cors_local}")
|
618
|
+
server_settings.cors_origins = list(cors_origins)
|
619
|
+
|
620
|
+
if address is not None:
|
621
|
+
server_settings.address = address
|
622
|
+
|
623
|
+
if run_setup:
|
624
|
+
_setup_flows(
|
625
|
+
flow.flows().values(),
|
626
|
+
force=force,
|
627
|
+
quiet=quiet,
|
628
|
+
)
|
629
|
+
|
630
|
+
lib.start_server(server_settings)
|
631
|
+
|
632
|
+
if COCOINDEX_HOST in cors_origins:
|
633
|
+
click.echo(f"Open CocoInsight at: {COCOINDEX_HOST}/cocoinsight")
|
634
|
+
|
635
|
+
click.secho("Press Ctrl+C to stop the server.", fg="yellow")
|
636
|
+
|
637
|
+
if live_update:
|
638
|
+
options = flow.FlowLiveUpdaterOptions(live_mode=True, print_stats=not quiet)
|
639
|
+
asyncio.run_coroutine_threadsafe(
|
640
|
+
_update_all_flows_with_hint_async(options), execution_context.event_loop
|
641
|
+
)
|
642
|
+
|
643
|
+
shutdown_event = threading.Event()
|
644
|
+
|
645
|
+
def handle_signal(signum: int, frame: FrameType | None) -> None:
|
646
|
+
shutdown_event.set()
|
647
|
+
|
648
|
+
signal.signal(signal.SIGINT, handle_signal)
|
649
|
+
signal.signal(signal.SIGTERM, handle_signal)
|
650
|
+
shutdown_event.wait()
|
651
|
+
|
652
|
+
|
653
|
+
def _flow_name(name: str | None) -> str:
|
654
|
+
names = flow.flow_names()
|
655
|
+
available = ", ".join(sorted(names))
|
656
|
+
if name is not None:
|
657
|
+
if name not in names:
|
658
|
+
raise click.BadParameter(
|
659
|
+
f"Flow '{name}' not found.\nAvailable: {available if names else 'None'}"
|
660
|
+
)
|
661
|
+
return name
|
662
|
+
if len(names) == 0:
|
663
|
+
raise click.UsageError("No flows available in the loaded application.")
|
664
|
+
elif len(names) == 1:
|
665
|
+
return names[0]
|
666
|
+
else:
|
667
|
+
console = Console()
|
668
|
+
index = 0
|
669
|
+
|
670
|
+
while True:
|
671
|
+
console.clear()
|
672
|
+
console.print(
|
673
|
+
Panel.fit("Select a Flow", title_align="left", border_style="cyan")
|
674
|
+
)
|
675
|
+
for i, fname in enumerate(names):
|
676
|
+
console.print(
|
677
|
+
f"> [bold green]{fname}[/bold green]"
|
678
|
+
if i == index
|
679
|
+
else f" {fname}"
|
680
|
+
)
|
681
|
+
|
682
|
+
key = click.getchar()
|
683
|
+
if key == "\x1b[A": # Up arrow
|
684
|
+
index = (index - 1) % len(names)
|
685
|
+
elif key == "\x1b[B": # Down arrow
|
686
|
+
index = (index + 1) % len(names)
|
687
|
+
elif key in ("\r", "\n"): # Enter
|
688
|
+
console.clear()
|
689
|
+
return names[index]
|
690
|
+
|
691
|
+
|
692
|
+
def _flow_by_name(name: str | None) -> flow.Flow:
|
693
|
+
return flow.flow_by_name(_flow_name(name))
|
694
|
+
|
695
|
+
|
696
|
+
if __name__ == "__main__":
|
697
|
+
cli()
|