Rayfin 自定义函数(UDF)开发
rayfin-functions
当编写 Rayfin 用户自定义函数(UDF)、从前端代码调用函数,或使用 functions CLI 命令时使用。触发词:rayfin functions、udf.func、UserDataFunctions、RayfinContext、FabricContext、AppFunctionsSchema、functions init、dev functions apply、client.functions、invoke、func start、function_app.ts、types.ts、rayfin/functions/、FunctionClient、FunctionsSchema、fabric-user-data-functions、functionsBaseUrl、rayfinLocalDev、resolveRayfinFunctionsBaseUrl、VITE_RAYFIN_FUNCTIONS_URL
- License 许可
- MIT
- 所属仓库
- microsoft/rayfin
- 最近更新
- 2026-11-12
- Stars
- ★ 662
- Forks
- 77
01 前言 Front Matter
- name
- rayfin-functions
- metadata
- { "author": "microsoft", "version": "0.2.0" }
- description
- Use when writing Rayfin user-defined functions (UDFs), invoking functions from frontend code, or using functions CLI commands. Triggers: rayfin functions, udf.func, UserDataFunctions, RayfinContext, FabricContext, AppFunctionsSchema, functions init, dev functions apply, client.functions, invoke, func start, function_app.ts, types.ts, rayfin/functions/, FunctionClient, FunctionsSchema, fabric-user-data-functions, functionsBaseUrl, rayfinLocalDev, resolveRayfinFunctionsBaseUrl, VITE_RAYFIN_FUNCTIONS_URL
03 SKILL.md 正文
Rayfin Functions
Server-side user-defined functions (UDFs) that run in the Fabric runtime and are invocable from your frontend via RayfinClient. Write trusted backend logic once; get a type-safe client call on the frontend.
The full reference lives in the Rayfin guide. Read the doc for the task you're doing before writing code — don't guess API shapes, endpoints, or CLI flags. Read a doc off disk from node_modules/@microsoft/rayfin-guide/assets/docs/<path>, or fetch it with rayfin docs get --module guide --path '<path>' — or get_doc with module: "guide" via MCP.
Where to read
| Task | Doc path |
|---|---|
| Overview, auth configuration and identities, project structure, first function | functions/index.md |
udf.func() API, RayfinContext, data access, importing entities |
functions/writing-functions.md |
How types.ts is generated and kept in sync |
functions/typegen.md |
| Reading secrets inside a function | functions/secrets.md |
Calling functions from the frontend (client.functions.*.invoke) |
functions/invoking-from-frontend.md |
| Connecting to external resources — audiences, recipe chooser, per-resource walkthroughs | functions/connections/index.md |
| CLI: scaffold, local host, deploy | cli/functions/index.md, init.md, dev-apply.md, deploy.md |
Connections and secrets
Read functions/connections/index.md before writing any connection code, and functions/secrets.md before reading a secret. Both are the source of truth; the summary below exists only so you know what to look for.
Enabled Functions require services.functions.auth.type: application.
Read Application authentication in functions/index.md for the configuration, identity model, and deployment requirements.
- Generic connections are declared on the context annotation —
RayfinContext<AppSchema, AudienceType.Sql | AudienceType.Storage>— and read asctx.Tokens.Sql. The annotation is what registers the binding, so the third argument ofudf.func()stays[]. - Supported audiences are
Sql,Storage,Fabric,AzureAI, andADO. ctx.Tokens.*uses the app identity for external resources;ctx.getDataClient()uses the caller's Rayfin token for Rayfin DB access. Keep tokens server-side.- Audiences are resolved by the TypeScript compiler at deploy time, so a type alias works — but prefer literal
AudienceType.Sql, and keep atsconfig.jsonin the functions project. Without one the CLI reads the annotation syntactically, cannot resolve aliases, and warns. - Secrets are declared with
rayfin secret set, recorded inrayfin.yml, and read asctx.Secrets.STRIPE_API_KEY. The CLI generatesrayfin/functions/src/secrets.generated.ts; never hand-edit it and never hard-code a secret name. ctx.getToken()andctx.getSecret()are deprecated and flagged by@typescript-eslint/no-deprecated. Usectx.Tokensandctx.Secrets.
Get the Fabric info for the user
Connection code needs real endpoints — the SQL server or the OneLake path. Resolve these yourself; don't ask the user to paste connection strings. When the user names a Fabric item ("connect to lakehouseA in workspaceB"), turn the workspace + item display names into coordinates:
- If a Fabric CLI (e.g.
fab) or Fabric MCP is already available in the workspace, prefer it — it wraps the same endpoints. - Otherwise call the Fabric REST API directly: base
https://api.fabric.microsoft.com/v1, token fromaz account get-access-token --resource https://api.fabric.microsoft.com.
Only fall back to asking the user when the name matches multiple items, the lookup returns 403 (no access), or you can't determine the workspace. The full lookup workflow is in functions/connections/get-fabric-info.md.
Local functions routing
Read functions/invoking-from-frontend.md before changing frontend routing.
Bundled Vite templates already use rayfinLocalDev() from
@microsoft/rayfin-local-dev. Leave functionsBaseUrl undefined so the SDK
uses /functions/<name>/invoke; the adapter forwards that same-origin route to
the local Functions host selected by rayfin dev.
For custom or older Vite projects, add the adapter as documented. Use
resolveRayfinFunctionsBaseUrl() only when existing code must explicitly
provide a Functions base URL.
For other frameworks, pass the framework-mapped Functions URL only during development.
Guardrails
- Import
RayfinContextfrom@microsoft/fabric-user-data-functions— not from@microsoft/rayfin-functions. - Register every function with
udf.func(name, handler, [])— don't export bare functions. - Use
import typefor data-entity imports — a runtime import pulls the decorator runtime into the functions bundle. - Never hand-edit
rayfin/functions/src/types.ts— typegen regenerates it and will overwrite your edits. - Never expose a local Functions URL in production; leave
functionsBaseUrlundefined soinvoke()targets the deployed backend. - Install resource SDK packages in
rayfin/functions/package.json, not the project root. - Prefer secrets via
rayfin secret set <NAME>read throughctx.Secrets; useprocess.envonly for local debugging.
04 同仓库更多 Skills
Rayfin 应用创建与入门
rayfin-getting-started
在启动或创建新的 Rayfin 应用时使用,或在出现 Rayfin 任务但尚未进入 Rayfin 项目时使用。它会通过 Rayfin CLI 带你进入项目,然后移交给项目内权威的、版本锁定的 rayfin 技能/文档,由它们负责所有项目内工作。触发词:构建 Rayfin 应用、启动 Rayfin 项目、创建新的 Rayfin 应用、create-rayfin、npm create @microsoft/rayfin、rayfin init、scaffold rayfin、rayfin CLI、rayfin template、universal app、awesome-rayfin gallery、get started with Rayfin。
数据可视化图表实现
visuals
当用户想要在项目中加入图表、图形、数据网格或其他数据可视化表示时使用。使用 VegaVisual 和 DataGrid 组件创建这些可视化,利用共享的 DataTable 输入、格式设置、主题和交互性。涵盖宿主应用消费的 onInteraction 选择/点击事件、用于分层叠加和参考线的命名多表数据输入,以及 Vega-Lite 原生选择。触发词:chart、graph、plot、dashboard、KPI、metric、trend、scorecard、data grid、visualize。
Power BI 语义模型探查
schema-discovery
使用 DAX INFO 函数探索 Power BI 语义模型架构。包括渐进式元数据发现策略、范围估计,以及针对表、列、度量值和关系的缩小范围技巧。
Power BI 查询设计
query-design
将 DAX 数据获取与 TypeScript 呈现相分离。指导何时使用 DAX、TypeScript 或 Vega-Lite 来进行聚合、总计行、FORMAT()、SELECTCOLUMNS、BLANK 处理、筛选、多粒度查询、交叉筛选与交叉高亮的子集/覆盖查询,以及格式字符串。
Universal App 函数能力开发
functions-capability
应用并构建 Universal App 的函数能力。用于可信的服务器端逻辑、密钥(secrets)、外部 API、自定义模型端点、已发布的 Fabric 数据智能体、Webhook 或特权操作。
Fabric 语义模型 DAX 查询
fabric-sdk
如何通过运行中的应用里的 Rayfin connectors 客户端查询 Fabric 数据。使用 getRayfinClient().connectors.<name>.executeQuery 对语义模型执行 DAX 查询,并使用 toQueryResult 规范化响应。