Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Statscraft 0.61.0, release v0.61.0

面向 AI 智能体的 Statscraft

Statscraft 的设计使拥有 shell 的 AI 智能体可以完成分析工作:一切都是语法,每个结果都是 JSON,每个错误都有行号和列号,会话在各步骤之间保留数据。本页说明智能体应如何工作,并提供一段可以粘贴到您的智能体中的指令。

能运行 MCP 服务器的智能体应用可以通过工具而不是 shell 使用同样的循环:参见 MCP 服务器。运行一次 statscraft connect,即可将其添加到您计算机上的智能体中;连接您的 AI 智能体说明了它会更改什么以及如何撤销。

工作循环

  1. 查看数据。 statscraft describe FILE --json 给出数据字典:变量名、类型、标签、值标签、缺失值、有效计数、权重变量和多重响应集。编写语法前先阅读它,确保名称和代码正确。
  2. 查阅语法。 statscraft help 列出支持的命令;statscraft help COMMAND 给出语法、规则和一个示例。只使用帮助中显示的内容。帮助中未列出的命令会失败,并给出清楚的错误。
  3. 将语法写入文件(job.sps),每条命令占一行或一个块,每条都以句点结尾。
  4. 检查语法。 statscraft check -s NAME --json job.sps 解析每条命令,并在会话数据的副本上运行,因此还能发现未知变量和类型错误,而且不写入任何内容。
  5. 分段运行。 statscraft run -s NAME --json --lines FIRST-LAST job.sps 运行这些行中的命令,并把结果保留在会话中。下一个范围在此基础上继续。行号仍为文件中的行号。会话在多次运行之间保留数据、设置、宏和命名数据集,就像窗口一样(会话)。statscraft describe -s NAME --json 在 session 下显示保留的状态。
  6. 读取 JSON。 ok 表示运行是否成功。changes 列出新增、更改和删除的变量,以及运行前后的个案数;dataset 给出数据集大小;commands[].tables 包含带原始数字的结果;commands[].charts 包含命令生成的图表,每个图表以其类型、标签和数字表示;commands[].warnings 指出命令在何处的行为与要求不同。
  7. 在错误指向的位置修正。 失败时,errors[] 给出 kind、message、line,对于语法错误还给出 column、expected,通常还有 hint。修正那一行,然后再次运行同一范围。失败的运行不保存任何内容(包括设置),因此重试从相同状态开始。

一个简短的会话:

statscraft describe survey.sav --json
statscraft help RECODE
statscraft check -s work --json job.sps
statscraft run -s work --json --lines 1-1 job.sps
statscraft run -s work --json --lines 4-6 job.sps
statscraft run -s work --json -e "FREQUENCIES VARIABLES=age_group."
statscraft describe -s work --json

语法错误会告诉智能体出错的位置和内容:

{"ok":false,"errors":[{"kind":"syntax","message":"unexpected end of command","line":1,"column":18,"expected":["a value","a variable","a function","'('"]}],"saved":false}

拼错的命令会附带建议:

{"ok":false,"errors":[{"kind":"syntax","message":"unknown command FREQUENCIS","line":1,"column":1,"hint":"did you mean FREQUENCIES?"}],"saved":false}

所有字段参见命令行。

良好习惯:

  • 每一步都使用 --json,并先读取 ok 字段;成功时退出代码为 0,失败时为 1。
  • 将整个分析放在一个 .sps 文件中,并按行范围运行,这样该文件就是所做工作的记录,还可以用 statscraft run job.sps 从头再次运行。
  • 要重新开始,请使用新的会话名称,或删除 .statscraft/sessions/NAME/。
  • JSON 中的数字是原始值。只在报告时再进行舍入。
  • 当用户需要 Excel 格式的表格时,在运行时添加 --xlsx tables.xlsx。
  • 要交付输出本身,在运行的最后加上 OUTPUT SAVE OUTFILE='results.scout'.:用户可以在窗口中通过 File > Open output 打开它。用户交给您的 .scout 文件是纯 JSON:直接读取即可,其中每条命令都附有其语法和表格(输出文件)。要将其转换为工作簿或网页,请运行 OUTPUT OPEN FILE='results.scout'. 和 OUTPUT EXPORT。
  • 需要展示图表时添加 --vega-lite:这样每个图表都会附带一个 Vega-Lite 规范,数字内嵌其中,可直接用于任何 Vega-Lite 查看器。不要根据自己计算的数字绘制图表。

A dataset stored in an OpenStatSpec database is edited in place with statscraft plan and a database, or the MCP tool apply_plan: preview the plan first, then apply it with actor naming who makes the change, then read the dataset back with statscraft db import instead of assuming the result. On Dolt, put the edits of one step into one plan, since an apply needs a clean working set and never commits. See plan and Editing a dataset in a database.

When one step depends on the numbers of another, or a run returns far more than you need, write the steps as one JavaScript function and run it with statscraft code (or the MCP tool run_code): it calls the same tools, keeps only what you return, and still leaves every run’s syntax in the session’s trail. See Code mode.

在打开的窗口中工作

当用户打开了 Statscraft 窗口时,智能体可以在窗口中工作,而不使用文件会话:会话名称 gui 会把语法发送到窗口,窗口像点击自己的 Run 按钮一样运行它。用户会看到每个表格出现在输出窗口中,每条命令出现在日志中,并且可以在智能体的两次运行之间继续在窗口中工作。实时窗口会话说明了其工作方式。

由于用户也可能在窗口中更改数据,智能体在依赖先前看到的名称或计数之前,应再次运行 statscraft describe -s gui --json。

每次成功的运行都会把其语法留在窗口中:语法被添加到名为 Agent 的语法窗口,位于一行带时间的注释之下(* Agent 14:32.)。该窗口在第一次运行时打开;用户关闭它后,下次运行时会再次打开。用户可以阅读所做的工作、修改它、按 Ctrl+Enter 运行并保存。失败的运行不添加任何内容。JSON 中的 trail 给出该窗口的名称。文件会话也会这样做,写入其文件夹中的 syntax.sps,每行注释带有日期,trail 给出该文件的路径。

智能体还可以通过 MCP 服务器读取、编辑、运行和保存用户的语法窗口;参见 MCP 服务器。当用户要求修正或解释其语法时,请读取该窗口,说明您打算如何修改,在窗口中编辑并从那里运行,这样修正会保留在用户的文件中。

窗口可以同时包含多个数据集和输出文档。在 session 下,describe 按用户看到的样子列出它们:active_dataset 是所有命令作用的数据集,datasets 列出每个打开的数据集及其大小(从窗口打开的文件会得到名称 DataSet1、DataSet2 等),output_documents 列出输出文档,designated_output 是接收后续表格的文档。运行 JSON 中的每条命令都在 commands[].document 中给出其文档。要处理另一个数据集,请运行 DATASET ACTIVATE name.;要把表格发送到其他位置,请运行 OUTPUT NEW NAME=name. 或 OUTPUT ACTIVATE name.。窗口忙碌时,智能体发起的运行会等待,待窗口空闲后再运行。

粘贴到智能体中的指令

You can analyze data with Statscraft, a statistics program driven by the established
statistics command syntax (COMPUTE, RECODE, FREQUENCIES, CTABLES and so on). Use the `statscraft` command line in the shell.

- Start with `statscraft describe <file> --json` to learn the variables, labels and missing values.
- Look up syntax with `statscraft help` and `statscraft help <COMMAND>`. Use only commands and
  options that the help lists.
- Write the syntax to a .sps file. Before running, check it:
  `statscraft check -s work --json <file>.sps`.
- Run it in steps that keep the data between them:
  `statscraft run -s work --json --lines FIRST-LAST <file>.sps`, or
  `statscraft run -s work --json -e "<syntax>"` for a single command. The session keeps data,
  settings, macros and named datasets between runs; `statscraft describe -s work --json` shows them.
- Read the JSON. If "ok" is false, fix the line and column given in "errors" (use "expected" and
  "hint") and run the same lines again; a failed run changes nothing. Read "warnings" of every
  command and "changes" to confirm what happened to the data.
- If I have the Statscraft window open, use `-s gui` instead of `-s work`, so I see every result.
  Each run that works is added to the syntax window Agent there; with `-s work` it goes to
  `.statscraft/sessions/work/syntax.sps`. Tell me where ("trail" in the JSON).
- Do not write data files unless I ask; nothing is saved to disk without a SAVE command.
- Report numbers from the "tables" in the JSON; do not compute them yourself.

实时会话的安全性

窗口只接受同一台计算机上同一用户的程序发送的语法:

  • 它只在 127.0.0.1 的一个随机 TCP 端口上监听,因此其他计算机无法访问它。
  • 启动时,它会生成一个随机令牌,并把地址和令牌写入 statscraft/gui.json,该文件在 Linux 上位于 $XDG_RUNTIME_DIR,在 Windows 上位于 %LOCALAPPDATA%,其他系统上位于 ~/.config。该文件只有该用户可以读取。
  • 每个请求都必须携带该令牌;不带令牌的请求会被拒绝。命令行从该文件读取令牌,并只将其发送到回环地址。
  • 大于 64 MB 的请求会被拒绝。
  • 窗口关闭时会删除它的文件。如果打开了两个窗口,后打开的窗口持有该文件,命令行和 MCP 会与它通信;它关闭后,如果先打开的窗口仍然开着,文件会回到那个窗口。

这在实际中意味着:任何以您的身份运行的程序都可以驱动窗口,正如它也可以直接运行 statscraft 一样。发送到窗口的语法可以做语法能做的一切:读取文件,以及用 SAVE 或 AGGREGATE OUTFILE 写入文件。同一连接还允许程序读取和更改您语法窗口中的文本、在新窗口中打开 .sps 文件,以及将语法窗口保存到文件;除非请求明确要求,保存不会覆盖另一个已存在的文件。请像对待能在您的 shell 中运行命令的智能体一样,对待能使用 -s gui 的智能体。

Syntax can also read a dataset from an OpenStatSpec database with GET DATA /TYPE=OPENSTATSPEC, a server included. An agent can then read any dataset the database account can read. The password never appears in the syntax, so it never lands in the Agent syntax window or a trail: a URL with one is refused, and Statscraft takes it from the STATSCRAFT_DB_PASSWORD environment variable of the window or the server. Give the agent an account that may only read when reading is all it needs.