docs(public): showcase foreign v2, OKF and EKC
Rewrite the public foreign reference, add bilingual complex Python notebook and OKF/EKC pages, and expand both homepages with executable foreign, notebook, knowledge and preflight examples.
This commit is contained in:
1 parent
81f9ea0072
commit
e6f05c7748
12 files changed
+1015
-20
No files matched your search
@@ -11,7 +11,7 @@ English-named files export English APIs; Chinese-named files export Chinese APIs
|
||||
|
||||
No extra allow flags: importing a module means you intend to use it. Under marqdo view or static export, exit soft-fails and long sleeps are clamped so the console stays up.
|
||||
|
||||
Modules: text (text / 文本), table (table / 表), files (fs / 文件), time (time / 时间), system (sys / 系统), json (json only), net (net / 网络), math (math / 数学), foreign (foreign / 外联), plugin (plugin / 插件), writeback (writeback / 自写回), subtask (subtask / 子任务).
|
||||
Modules: text (text / 文本), table (table / 表), files (fs / 文件), time (time / 时间), system (sys / 系统), json (json only), net (net / 网络), math (math / 数学), foreign (foreign / 外联), plugin (plugin / 插件), writeback (writeback / 自写回), subtask (subtask / 子任务). Foreign v2 adds structured invoke records, JSON calls, parallel fan-out, persistent Python/Node kernels, an explicit state bridge and document-native notebook sessions — see stdlib/foreign.mq.md and features/19-foreign-notebook.mq.md.
|
||||
|
||||
From v0.1.2, official `lib/*.mq.md` is embedded in the `marqdo` binary (disk `lib/` or `MARQDO_LIB` still overrides). Standalone `.exe` works without a separate stdlib zip.
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ description: 如何导入官方 lib 模块
|
||||
|
||||
导入即表示要用对应能力,无需再加允许旗标。marqdo view 或静态导出里,退出会软失败、长睡眠会被钳制,以免拖垮控制台。
|
||||
|
||||
模块:文本(text / 文本)、表(table / 表)、文件(fs / 文件)、时间(time / 时间)、系统(sys / 系统)、json(仅 json)、网络(net / 网络)、数学(math / 数学)、外联(foreign / 外联)、插件(plugin / 插件)、自写回(writeback / 自写回)、子任务(subtask / 子任务)。
|
||||
模块:文本(text / 文本)、表(table / 表)、文件(fs / 文件)、时间(time / 时间)、系统(sys / 系统)、json(仅 json)、网络(net / 网络)、数学(math / 数学)、外联(foreign / 外联)、插件(plugin / 插件)、自写回(writeback / 自写回)、子任务(subtask / 子任务)。外联 v2 新增结构化执行记录、JSON 调用、并行扇出、Python/Node 持久内核、显式状态桥与文档原生 notebook 会话 — 见 stdlib/外联.mq.md 与 features/19-外联Notebook.mq.md。
|
||||
|
||||
自 v0.1.2 起,官方 `lib/*.mq.md` 内置于 `marqdo` 二进制(磁盘 `lib/` 或 `MARQDO_LIB` 仍可覆盖)。单独下载 `.exe` 即可导入标准库,无需再解压 stdlib 包。
|
||||
|
||||
|
||||
+134
-9
@@ -1,20 +1,145 @@
|
||||
---
|
||||
title: lib/foreign — external languages
|
||||
description: Run local interpreters from Marqdo (English)
|
||||
title: lib/foreign — foreign kernels v2
|
||||
description: Structured execution, JSON calls, parallel fan-out, persistent kernels and an explicit state bridge
|
||||
import foreign:lib/foreign.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
Import lib/foreign.mq.md. Bind a code fence like a formula: empty-RHS assign then a ```lang block. The value type is code. Call run code=`name` to execute via your local interpreter. run_lang runs an inline source string.
|
||||
`lib/foreign` runs code fences and inline sources with local interpreters. v2 keeps the Markdown surface unchanged while adding structured records, JSON in/out, parallel fan-out, persistent Python/Node kernels and an explicit state bridge.
|
||||
|
||||
`hi` =
|
||||
```python
|
||||
print("hello-from-python")
|
||||
The same file is still a document: a bound fence is readable Markdown, and `foreign.*` turns it into an executable unit.
|
||||
|
||||
---
|
||||
|
||||
Probe before you run.
|
||||
|
||||
**node = > foreign.probe lang="node"**
|
||||
|
||||
> print text=[ok](`node`)
|
||||
> print text=[version](`node`)
|
||||
|
||||
`probe` returns `{lang, cmd, path, ok, version, error}`. Omit `lang` to inspect every known language.
|
||||
|
||||
---
|
||||
|
||||
A structured record, not just stdout.
|
||||
|
||||
`marker` =
|
||||
```node
|
||||
console.log("hello from a foreign kernel");
|
||||
```
|
||||
|
||||
Default command is python (Windows) or python3 (Unix). Override with set_cmd, env MARQDO_FOREIGN_PYTHON, or the command box in live view. Failures ask you to check that configuration.
|
||||
**record = > foreign.invoke code=`marker`**
|
||||
|
||||
**`out` = [foreign.run] code=`hi`**
|
||||
> print text=[ok](`record`)
|
||||
> print text=[stdout](`record`)
|
||||
> print text=[code](`record`)
|
||||
|
||||
> print text=`out`
|
||||
`invoke` never throws on a process failure. Read `ok`, `code`, `stdout`, `stderr`, `ms`, `timed_out`, `truncated`, `result` and `error` from one record.
|
||||
|
||||
---
|
||||
|
||||
JSON in, JSON out.
|
||||
|
||||
`double` =
|
||||
```node
|
||||
let input = "";
|
||||
process.stdin.on("data", chunk => { input += chunk; });
|
||||
process.stdin.on("end", () => {
|
||||
console.log(JSON.stringify(JSON.parse(input) * 2));
|
||||
});
|
||||
```
|
||||
|
||||
**single = > foreign.call code=`double` input=21**
|
||||
|
||||
> print text=`single`
|
||||
|
||||
The input is encoded as JSON on stdin; the last stdout value is parsed as JSON. Use `output="json"` with `invoke` when you also want the raw record.
|
||||
|
||||
---
|
||||
|
||||
Parallel fan-out.
|
||||
|
||||
`numbers` =
|
||||
|
||||
| value |
|
||||
|-------|
|
||||
| 1 |
|
||||
| 2 |
|
||||
| 3 |
|
||||
| 4 |
|
||||
|
||||
**doubled = > foreign.call_many code=`double` inputs=`numbers` concurrency=4**
|
||||
|
||||
> print text=`doubled`
|
||||
|
||||
`call_many` applies one program to every input, runs independently, preserves input order, and returns parsed JSON results. `run_many` does the same for full invoke-option maps.
|
||||
|
||||
---
|
||||
|
||||
A persistent kernel.
|
||||
|
||||
**session = > foreign.session_open lang="node"**
|
||||
|
||||
**first = > foreign.session_eval id=`session` source="let total = 0; for (let i = 1; i <= 5; i += 1) total += i;"**
|
||||
|
||||
**second = > foreign.session_eval id=`session` source="total * 2"**
|
||||
|
||||
> print text=[result](`second`)
|
||||
|
||||
Variables created in the first evaluation survive into the second. The kernel is not a shell: use `session_eval`, `session_get`, `session_names`, then `session_close`.
|
||||
|
||||
---
|
||||
|
||||
The explicit state bridge.
|
||||
|
||||
**> foreign.session_set id=`session` name="rate" value=0.08**
|
||||
|
||||
**rate = > foreign.session_get id=`session` name="rate"**
|
||||
|
||||
> print text=`rate`
|
||||
|
||||
**> foreign.session_close id=`session`**
|
||||
|
||||
This is a JSON copy, not shared memory. The kernel receives a value; Marqdo receives a value. There is no implicit mirroring and no hidden global namespace.
|
||||
|
||||
---
|
||||
|
||||
Notebook sessions.
|
||||
|
||||
A document can declare named kernels and a cell order without new Markdown markers:
|
||||
|
||||
```text
|
||||
`内核表` =
|
||||
|
||||
| 名称 | 语言 | 命令 | 说明 |
|
||||
|------|------|------|------|
|
||||
| 分析 | python | | shared analysis |
|
||||
| 复核 | python | | isolated review |
|
||||
|
||||
`会话表` =
|
||||
|
||||
| 单元 | 内核 | 说明 |
|
||||
|------|------|------|
|
||||
| 准备数据 | 分析 | write state |
|
||||
| 计算指标 | 分析 | read state |
|
||||
| 独立复核 | 复核 | isolated kernel |
|
||||
```
|
||||
|
||||
Live `view` then adds Run All, Step, kernel switching, a journal and per-cell state. See the complete Python walkthrough in [`features/19-foreign-notebook.mq.md`](../features/19-foreign-notebook.mq.md).
|
||||
|
||||
---
|
||||
|
||||
Safety defaults.
|
||||
|
||||
| Boundary | Default |
|
||||
|---|---|
|
||||
| Execution | requires the host execution capability |
|
||||
| Shell | no shell; arguments are split explicitly |
|
||||
| Output | bounded, with truncated |
|
||||
| Timeout | process group killed on Unix / taskkill /T on Windows |
|
||||
| CWD | file sandbox root; no implicit escape |
|
||||
| Parallelism | capped fan-out; each item isolated |
|
||||
|
||||
Read the full API in [../../lib/foreign.mq.md](../../lib/foreign.mq.md) and the design in [../../doc/design/foreign-kernel.md](../../doc/design/foreign-kernel.md).
|
||||
+134
-9
@@ -1,20 +1,145 @@
|
||||
---
|
||||
title: lib/外联 — 外部语言
|
||||
description: 从 Marqdo 调用本机解释器(中文)
|
||||
title: lib/外联 — 外联内核 v2
|
||||
description: 结构化执行、JSON 调用、并行扇出、持久内核与显式状态桥
|
||||
导入 外联:lib/外联.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
导入 lib/外联.mq.md。代码围栏与公式同构:空 RHS 赋值后跟 lang 代码块,得到 code 类型。用 运行 代码=`名` 调用本机解释器。
|
||||
`lib/外联` 用本机解释器运行代码围栏和内联源码。v2 不改 Markdown 表面,新增结构化记录、JSON 进出、并行扇出、Python/Node 持久内核和显式状态桥。
|
||||
|
||||
`hi` =
|
||||
```python
|
||||
print("hello-from-python")
|
||||
文件仍然是一篇文档:绑定的代码围栏是可读的 Markdown,`外联.*` 把它变成可执行单元。
|
||||
|
||||
---
|
||||
|
||||
先探测,再运行。
|
||||
|
||||
**node = > 外联.探测 语言="node"**
|
||||
|
||||
> 打印 内容=[ok](`node`)
|
||||
> 打印 内容=[version](`node`)
|
||||
|
||||
`探测` 返回 `{lang, cmd, path, ok, version, error}`。省略语言时探测所有已知语言。
|
||||
|
||||
---
|
||||
|
||||
结构化记录,而不只是 stdout。
|
||||
|
||||
`标记` =
|
||||
```node
|
||||
console.log("来自外联内核的问候");
|
||||
```
|
||||
|
||||
默认命令可用 设置命令 或 MARQDO_FOREIGN_PYTHON 覆盖;live view 里可改命令框再点 Run。
|
||||
**记录 = > 外联.执行 代码=`标记`**
|
||||
|
||||
**`out` = [外联.运行] 代码=`hi`**
|
||||
> 打印 内容=[ok](`记录`)
|
||||
> 打印 内容=[stdout](`记录`)
|
||||
> 打印 内容=[code](`记录`)
|
||||
|
||||
> print text=`out`
|
||||
`执行` 在进程失败时不抛错,而是在一条记录里给出 `ok`、`code`、`stdout`、`stderr`、`ms`、`timed_out`、`truncated`、`result` 与 `error`。
|
||||
|
||||
---
|
||||
|
||||
JSON 进,JSON 出。
|
||||
|
||||
`双倍` =
|
||||
```node
|
||||
let input = "";
|
||||
process.stdin.on("data", chunk => { input += chunk; });
|
||||
process.stdin.on("end", () => {
|
||||
console.log(JSON.stringify(JSON.parse(input) * 2));
|
||||
});
|
||||
```
|
||||
|
||||
**单个 = > 外联.调用 代码=`双倍` 输入=21**
|
||||
|
||||
> 打印 内容=`单个`
|
||||
|
||||
输入编码成 JSON 写到 stdin;stdout 的最后一个值再解析成 JSON。想同时保留原始记录时,用 `执行` 的 `输出="json"`。
|
||||
|
||||
---
|
||||
|
||||
并行扇出。
|
||||
|
||||
`数值表` =
|
||||
|
||||
| 数值 |
|
||||
|------|
|
||||
| 1 |
|
||||
| 2 |
|
||||
| 3 |
|
||||
| 4 |
|
||||
|
||||
**翻倍 = > 外联.批量调用 代码=`双倍` 输入表=`数值表` 并发=4**
|
||||
|
||||
> 打印 内容=`翻倍`
|
||||
|
||||
`批量调用` 对每个输入独立运行同一程序,保持输入顺序并返回解析后的 JSON 列表;`批量运行` 对完整 invoke 选项表做同样的事。
|
||||
|
||||
---
|
||||
|
||||
持久内核。
|
||||
|
||||
**会话 = > 外联.会话开启 语言="node"**
|
||||
|
||||
**第一次 = > 外联.会话求值 标识=`会话` 源码="let total = 0; for (let i = 1; i <= 5; i += 1) total += i;"**
|
||||
|
||||
**第二次 = > 外联.会话求值 标识=`会话` 源码="total * 2"**
|
||||
|
||||
> 打印 内容=[result](`第二次`)
|
||||
|
||||
第一次求值创建的变量会保留到第二次。内核不是 shell:用 `会话求值`、`会话取值`、`会话变量`,最后 `会话关闭`。
|
||||
|
||||
---
|
||||
|
||||
显式状态桥。
|
||||
|
||||
**> 外联.会话设值 标识=`会话` 名称="rate" 值=0.08**
|
||||
|
||||
**利率 = > 外联.会话取值 标识=`会话` 名称="rate"**
|
||||
|
||||
> 打印 内容=`利率`
|
||||
|
||||
**> 外联.会话关闭 标识=`会话`**
|
||||
|
||||
这是 JSON 拷贝,不是共享内存:内核收到一个值,Marqdo 取回一个值。没有隐式镜像,也没有隐藏的全局命名空间。
|
||||
|
||||
---
|
||||
|
||||
Notebook 会话。
|
||||
|
||||
文档可以直接声明命名内核和单元顺序,不需要新增 Markdown 标记:
|
||||
|
||||
```text
|
||||
`内核表` =
|
||||
|
||||
| 名称 | 语言 | 命令 | 说明 |
|
||||
|------|------|------|------|
|
||||
| 分析 | python | | 共享分析 |
|
||||
| 复核 | python | | 独立复核 |
|
||||
|
||||
`会话表` =
|
||||
|
||||
| 单元 | 内核 | 说明 |
|
||||
|------|------|------|
|
||||
| 准备数据 | 分析 | 写入状态 |
|
||||
| 计算指标 | 分析 | 读取状态 |
|
||||
| 独立复核 | 复核 | 隔离内核 |
|
||||
```
|
||||
|
||||
live `view` 会提供 Run All、Step、切换内核、journal 和单元状态。完整 Python 示例见 [`features/19-外联Notebook.mq.md`](../features/19-外联Notebook.mq.md)。
|
||||
|
||||
---
|
||||
|
||||
安全默认值。
|
||||
|
||||
| 边界 | 默认 |
|
||||
|---|---|
|
||||
| 执行 | 需要宿主执行能力 |
|
||||
| Shell | 不经过 shell;参数显式切分 |
|
||||
| 输出 | 有界,带 truncated |
|
||||
| 超时 | Unix 杀进程组 / Windows taskkill /T |
|
||||
| 工作目录 | 文件沙箱根目录;不隐式越界 |
|
||||
| 并行 | 扇出有上限;每个任务隔离 |
|
||||
|
||||
完整 API 见 [../../lib/外联.mq.md](../../lib/外联.mq.md),设计见 [../../doc/design/foreign-kernel.md](../../doc/design/foreign-kernel.md)。
|
||||
Reference in new issue
Block a user