@adep/runtime 0.1.0

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.
package/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # @adep/runtime — AgentDeploy 运行时内核
2
+
3
+ > 云函数执行器 + 本地模拟运行时 + `cloud.db` / `cloud.storage` 能力装配。**零平台重栈依赖**(无 elysia / drizzle / better-auth / nuxt / bun:sqlite / postgres / ioredis),供 server、CLI(`adep dev`)、浏览器离线工作区三方复用。
4
+
5
+ ```bash
6
+ npm install @adep/runtime
7
+ # 或
8
+ pnpm add @adep/runtime
9
+ ```
10
+
11
+ ## 能力总览
12
+
13
+ | 能力 | 说明 |
14
+ | -------------- | ---------------------------------------------------------------------------------- |
15
+ | 函数执行器 | 在受限沙箱内加载 / 执行用户云函数(含超时、内存限制、OOM) |
16
+ | 能力装配 | 把 `cloud.db` / `cloud.storage` / `cloud.fetch` / `cloud.chain` 装配进 `ctx.cloud` |
17
+ | 本地模拟运行时 | 让 `adep dev` 在本地跑出与线上一致的执行语义 |
18
+ | 依赖解析 | 函数依赖的 classify / manifest 解析 |
19
+ | 工具函数 | 下载签名 / 快照解析 / ProjectDriver 方言工厂等 |
20
+
21
+ ## 用法
22
+
23
+ ```ts
24
+ import { createCloudContainer } from '@adep/runtime'
25
+ import type { FunctionContext } from '@adep/types'
26
+
27
+ const container = createCloudContainer(/* project driver */)
28
+ const ctx = { cloud: container, params: { id: '1' } } satisfies FunctionContext
29
+
30
+ // 执行一个函数模块
31
+ const result = await container.functions.invoke('my-fn', ctx)
32
+ ```
33
+
34
+ ## 说明
35
+
36
+ - 运行时依赖 `@adep/types` 作为类型契约,二者解耦演进。
37
+ - 浏览器端复用见 `@adep/web-container`(开发工作区形态);对外发布以本包为主。
@@ -0,0 +1,39 @@
1
+ /**
2
+ * 方言适配层(DB-002,PRD §2.7.3 / ADR-0006)。
3
+ *
4
+ * 构建器是 M6 切 PG 的第四条替换缝:方言差异全部封装在 `QueryDialect` 里,
5
+ * 上层(builder / 用户函数代码)只依赖本接口,切库时零改动。
6
+ *
7
+ * 三处已知差异(任务单):
8
+ * 1. 占位符:SQLite `?` vs PG `$1`;
9
+ * 2. 自增主键返回:SQLite 经 last_insert_rowid(驱动层取回,无需 SQL 尾巴)vs PG `RETURNING id`;
10
+ * 3. Upsert 语法:SQLite `ON CONFLICT ... DO ...` vs PG 同为 `ON CONFLICT` 但赋值引用形态不同。
11
+ * PG 运行时驱动属 M6,本文件只把差异**文字**留位(`lastInsertIdClause` / `upsertConflictClause`),
12
+ * 不实现 PG 连接;二者当前不被公共 API 调用,由单测直接断言方言缝,防止死代码漂移。
13
+ */
14
+ export interface QueryDialect {
15
+ readonly name: 'sqlite' | 'postgres';
16
+ /** 第 index(0 起)个参数的占位符文本。 */
17
+ placeholder(index: number): string;
18
+ /** 引用已过白名单的标识符(内部 `"` → `""`)。 */
19
+ quote(identifier: string): string;
20
+ /** INSERT 后取回自增主键的 SQL 尾巴(SQLite 空串 + 驱动 lastInsertRowid;PG `RETURNING id`)。 */
21
+ lastInsertIdClause(): string;
22
+ /** Upsert 冲突子句(M6 预留缝;当前无公开 upsert 方法)。 */
23
+ upsertConflictClause(columns: readonly string[]): string;
24
+ }
25
+ /** SQLite 方言(M1 唯一运行时方言,DB-000 硬约束 1)。 */
26
+ export declare const sqliteDialect: QueryDialect;
27
+ /**
28
+ * PG 方言(M6 桩实现位留空:连接驱动不存在,只编译 SQL 文字)。
29
+ * 上层代码用 `createCloudDb(driver, { dialect: postgresDialect })` 即可切方言。
30
+ */
31
+ export declare const postgresDialect: QueryDialect;
32
+ /**
33
+ * 按驱动引擎选定方言(DB-007):PG 项目库驱动 → postgresDialect,其余 → sqliteDialect。
34
+ * 上层模块(connection 元信息写读 / owned 变更流 / 快照逆操作等自带 SQL 的 helper)
35
+ * 一律经本函数取占位符,禁止各自硬编码 `?`(那会在 PG 形态下静默生成非法 SQL)。
36
+ */
37
+ export declare function dialectFor(driver: {
38
+ readonly engine: 'sqlite' | 'pg';
39
+ }): QueryDialect;
@@ -0,0 +1,14 @@
1
+ /**
2
+ * 裸 SQL 安全守卫(DB-002,规范 §6.1 第 4/5 条)。
3
+ *
4
+ * `cloud.db.query` 是受控裸 SQL 逃生口:只允许**单条只读语句**。
5
+ * 检查在参数绑定之外的**文本层**完成(strip 字面量与注释后扫描),
6
+ * 因为 `sqlite_master`、`ATTACH` 这类词无法靠参数化挡住。
7
+ */
8
+ import { DbError } from '../provision/identifier';
9
+ /** DB 域统一不安全操作错误(路由层转信封)。 */
10
+ export declare function unsafeOperation(message: string): DbError;
11
+ /** 剥离字符串字面量(单 / 双引号、SQLite 双引号转义)与注释(行注释与块注释)。 */
12
+ export declare function stripLiterals(sql: string): string;
13
+ /** 断言裸 SQL 安全:单语句、只读、禁跨库、禁系统表。不满足抛 DB_UNSAFE_OP。 */
14
+ export declare function assertReadOnlyQuery(sql: string): void;
@@ -0,0 +1,16 @@
1
+ /**
2
+ * cloud.db 运行时查询构建器工厂(DB-002)。
3
+ *
4
+ * 每个项目库由独立 `ProjectDriver` 绑定(DB-001 连接层隔离),本工厂把驱动包成用户侧 `CloudDb`:
5
+ * - 句柄闭包封装:不暴露连接串 / 库文件 / 切换项目的方法(验收 Scenario 越权 + 静态扫描);
6
+ * - `transaction`:BEGIN → fn(tx 句柄) → COMMIT;抛错回滚并向上传播;
7
+ * **事务进行中**,通过事务外句柄写同一库 → 抛 `DB_UNSAFE_OP`(契约 §cloud-db 注释)。
8
+ */
9
+ import type { CloudDb } from '@adep/types';
10
+ import type { ProjectDriver } from '../../db/project-driver';
11
+ import type { QueryDialect } from './dialect';
12
+ export interface CreateCloudDbOptions {
13
+ /** 方言适配器;缺省按驱动引擎分派(DB-007:sqlite → sqliteDialect / pg → postgresDialect)。 */
14
+ dialect?: QueryDialect;
15
+ }
16
+ export declare function createCloudDb(driver: ProjectDriver, options?: CreateCloudDbOptions): CloudDb;
@@ -0,0 +1,67 @@
1
+ /**
2
+ * owned 表能力(DB-006):保留列、变更流表与读写 helper。
3
+ *
4
+ * owned 表 = 平台「拥有并同步」的用户表:行主键 ULID(时间有序、离线可生成)、
5
+ * 保留 `__owner_key` 分区列、`__changes` 追加式变更流(供时间点回滚 / 离线同步重放)。
6
+ *
7
+ * 存储约定:
8
+ * - 变更流物理表 = `_adep_changes_<table>`(`_adep_` 平台保留段:sql-console 文本守卫
9
+ * 拒绝访问、usage / inspect / 表列表均排除——与 `_adep_console_log` 同款纪律);
10
+ * - `__owner_key` 是 owned 表上**唯一**允许的 `__` 前缀用户列,其余 `__*` 一律拒绝。
11
+ *
12
+ * 安全(任务单三类断言):
13
+ * - 参数化:`appendChange` 与 owner_key 过滤全部 `?` 绑定,零字面量拼接;
14
+ * - 标识符白名单:`assertUserColumn` 在**任何 SQL 执行之前**拒绝 `__*`(除 `__owner_key`)
15
+ * 与 `_adep_*` 前缀;
16
+ * - 原子性:写 + 变更流追加用 `SAVEPOINT` 包裹(`runOwnedWrite`),可安全嵌在外层事务内。
17
+ */
18
+ import type { ChangeOp, ChangeQuery, ChangeRecord, Row } from '@adep/types';
19
+ import type { ProjectDriver } from '../../db/project-driver';
20
+ /** owned 表行主键列名(ULID,TEXT PRIMARY KEY)。 */
21
+ export declare const OWNED_PK = "id";
22
+ /** owned 表保留分区列(行归属某设备/租户;可空 = 未归属)。 */
23
+ export declare const OWNED_OWNER_KEY = "__owner_key";
24
+ /** 变更流物理表前缀(`_adep_` 平台保留段,sql-console 守卫 / usage / inspect 均排除)。 */
25
+ export declare const CHANGE_LOG_PREFIX = "_adep_changes_";
26
+ /** 变更流表名的最大长度上限 = 白名单上限(63);owned 表名因此 ≤ 49 字符。 */
27
+ export declare function changeLogTable(table: string): string;
28
+ /**
29
+ * 用户可见列白名单:在 `assertIdentifier` 之上追加保留前缀拒绝。
30
+ * - `__*`:平台保留(`__owner_key` 是 owned 分区列,唯一放行);
31
+ * - `_adep_*`:平台内部表 / 元数据段。
32
+ */
33
+ export declare function assertUserColumn(name: string): void;
34
+ /** 变更流表建表 DDL(IF NOT EXISTS,幂等;DB-007:AUTOINCREMENT 为 SQLite 专属 → PG 用 identity)。 */
35
+ export declare function changeLogDdl(table: string, driver?: {
36
+ readonly engine: 'sqlite' | 'pg';
37
+ }): string;
38
+ /** 确保变更流表存在(建 owned 表时调用;幂等)。 */
39
+ export declare function ensureChangeLog(driver: ProjectDriver, table: string): Promise<void>;
40
+ /**
41
+ * 从 CREATE TABLE 语句识别 owned 声明(DB-006 建表钩子):
42
+ * DDL 含 `__owner_key` 列即视为声明 owned,返回目标表名;非 owned 返回 null。
43
+ * 基于 `stripLiterals`(剥离字面量与注释)检测,注释 / 字符串里的 `__owner_key` 不会误判。
44
+ * 表名解析失败(异常 DDL 形态)→ 返回 null,由执行层走普通建表(不强制 owned)。
45
+ */
46
+ export declare function ownedCreateTable(sql: string): string | null;
47
+ /** 该表是否登记为 owned(依据:变更流物理表是否存在;系统目录查询按引擎分派)。 */
48
+ export declare function hasChangeLog(driver: ProjectDriver, table: string): Promise<boolean>;
49
+ /** 断言表是 owned(无变更流表 → DB_UNSAFE_OP);.owned() / changes() 共用。 */
50
+ export declare function assertOwned(driver: ProjectDriver, table: string): Promise<void>;
51
+ /**
52
+ * 追加一条变更记录(参数化绑定,零字面量拼接)。
53
+ * `before` / `after` 序列化为 JSON 文本存储;`op` / `id` / `__owner_key` 冗余列便于过滤。
54
+ * 占位符经 dialectFor 按引擎分派(DB-007:sqlite `?` / pg `$n`)。
55
+ */
56
+ export declare function appendChange(driver: ProjectDriver, table: string, op: ChangeOp, id: string, ownerKey: string | null, before: Row | null, after: Row | null): Promise<void>;
57
+ /**
58
+ * 以 SAVEPOINT 包裹「写 + 变更流追加」,保证原子性。
59
+ * SAVEPOINT 可安全嵌套在外层事务(cloud.db.transaction / 链事务)内,无冲突。
60
+ * 回调为 async(DB-007 驱动 async 化),SAVEPOINT / RELEASE 语句本身无方言差异。
61
+ */
62
+ export declare function runOwnedWrite(driver: ProjectDriver, fn: (d: ProjectDriver) => Promise<void>): Promise<void>;
63
+ /**
64
+ * 读取 owned 表变更流:按 `seq` 升序(可重放顺序),支持增量(afterSeq)与分区(ownerKey)过滤。
65
+ * 表非 owned → DB_UNSAFE_OP;过滤条件全部按方言占位符参数化绑定。
66
+ */
67
+ export declare function readChanges(driver: ProjectDriver, table: string, query?: ChangeQuery): Promise<ChangeRecord[]>;
@@ -0,0 +1,50 @@
1
+ /**
2
+ * 单表链式 Builder(DB-002)。
3
+ *
4
+ * 不可变:每个链式方法返回**新 Builder**(克隆 state),复用同一实例派生多个查询是安全的。
5
+ * 编译目标:`{ sql, params }`——SQL 只含方言占位符,值全部走 params 数组(安全硬约束 1)。
6
+ * 安全硬约束(规范 §6.1):
7
+ * 1. 值参数化绑定,构建器内部零字符串拼接(标识符除外,见下);
8
+ * 2. 标识符先过白名单再引用(`assertIdentifier` + 方言 quote);
9
+ * 3. update / delete 无 where 直接阻断;
10
+ * 4. 单语句(由编译形状天然保证:永远只产出一条语句);
11
+ * 5. 禁跨库 / 禁系统表 / 禁 ATTACH(`query` 逃生口由 guards 承担)。
12
+ */
13
+ import type { CloudDbTable, SqlOperator, SqlValue } from '@adep/types';
14
+ import type { ProjectDriver } from '../../db/project-driver';
15
+ import type { QueryDialect } from './dialect';
16
+ export interface WhereClause {
17
+ column: string;
18
+ operator: SqlOperator;
19
+ value: SqlValue | readonly SqlValue[];
20
+ }
21
+ export interface QueryState {
22
+ table: string;
23
+ columns: readonly string[];
24
+ wheres: readonly WhereClause[];
25
+ orderBys: readonly {
26
+ column: string;
27
+ direction: 'asc' | 'desc';
28
+ }[];
29
+ limit: number | undefined;
30
+ offset: number | undefined;
31
+ /** owned 声明(DB-006):true = 显式 `.owned()`;undefined = 写时按变更流表存在性自检。 */
32
+ owned?: boolean;
33
+ }
34
+ export interface CompiledQuery {
35
+ sql: string;
36
+ params: SqlValue[];
37
+ }
38
+ export declare function compileWhere(wheres: readonly WhereClause[], dialect: QueryDialect, params: SqlValue[]): string;
39
+ export declare function compileSelect(state: QueryState, dialect: QueryDialect): CompiledQuery;
40
+ export declare function compileCount(state: QueryState, dialect: QueryDialect): CompiledQuery;
41
+ export declare function compileInsert(table: string, row: Record<string, SqlValue>, dialect: QueryDialect): CompiledQuery;
42
+ export declare function compileInsertMany(table: string, rows: ReadonlyArray<Record<string, SqlValue>>, dialect: QueryDialect): CompiledQuery;
43
+ export declare function compileUpdate(table: string, wheres: readonly WhereClause[], values: Record<string, SqlValue>, dialect: QueryDialect): CompiledQuery;
44
+ export declare function compileDelete(table: string, wheres: readonly WhereClause[], dialect: QueryDialect): CompiledQuery;
45
+ /** 写操作守卫:事务进行中时,禁止通过**事务外**句柄写同一项目库(契约 §cloud-db 注释)。 */
46
+ export type WriteGuard = () => void;
47
+ export interface TableBuilderOptions {
48
+ guardWrite?: WriteGuard;
49
+ }
50
+ export declare function createTableBuilder(table: string, driver: ProjectDriver, dialect: QueryDialect, options?: TableBuilderOptions): CloudDbTable;
@@ -0,0 +1,12 @@
1
+ /** ULID 合法字符集(用于校验:`^[0-9A-HJKMNP-TV-Z]{26}$`)。 */
2
+ export declare const ULID_PATTERN: RegExp;
3
+ /**
4
+ * 对 16 字符 Base32 字符串 +1 递增(带进位;全满回绕到 0)。
5
+ * 导出供单测确定性覆盖进位 / 全回绕分支(生产路径经 `ulid()` 调用,输入恒来自 `encodeRandom`)。
6
+ */
7
+ export declare function incrBase32(prev: string): string;
8
+ /**
9
+ * 生成一个 ULID(26 字符)。`now` 可注入便于测试时间序(缺省 Date.now())。
10
+ * 同毫秒内连续调用返回严格递增的值(单调);跨毫秒用新随机数。
11
+ */
12
+ export declare function ulid(now?: number): string;
@@ -0,0 +1,20 @@
1
+ /**
2
+ * DB 域错误与标识符白名单(DB-001,规范 §6.1 安全硬约束)。
3
+ *
4
+ * 标识符(表名 / 列名 / 索引名)一律先过白名单再进入 SQL:
5
+ * 不合规 → `DB_UNSAFE_OP`,**在任何 SQL 执行之前**抛出(验收 Scenario 6)。
6
+ * 白名单通过后的标识符以双引号包裹(`"` → `""`)作为最后一道防线——
7
+ * 这条引用规则与参数化绑定不同:标识符无法参数化,只能白名单 + 引用。
8
+ */
9
+ /** DB 域错误:路由层把它转成统一错误信封。 */
10
+ export declare class DbError extends Error {
11
+ readonly status: number;
12
+ readonly code: string;
13
+ constructor(status: number, code: string, message: string);
14
+ }
15
+ /** 标识符白名单:字母 / 下划线开头,仅字母数字下划线,最长 63(与 SQLite 标识符上限对齐)。 */
16
+ export declare const IDENTIFIER_PATTERN: RegExp;
17
+ /** 白名单不过即抛 `DB_UNSAFE_OP`,不执行任何 SQL。 */
18
+ export declare function assertIdentifier(name: string): void;
19
+ /** 白名单 + 双引号引用(内部引用转义)。只对已过白名单的标识符使用。 */
20
+ export declare function quoteIdentifier(name: string): string;
@@ -0,0 +1,15 @@
1
+ import type { CapabilityBundle } from '@adep/types';
2
+ import type { ProjectDriver } from '../../db/project-driver';
3
+ export interface CreateDbCapabilityOptions {
4
+ /**
5
+ * 写终端(insert / insertMany / update / delete)执行前的项目级配额钩子(BILL-005,§2.7.3)。
6
+ * 由 provider 绑定 projectId + dataDir 注入(方言无关 usageStats 量当前用量,超限抛 429 拒写)。
7
+ * 缺省不注入 → 不做写前配额门(fail-open 过渡)。
8
+ */
9
+ beforeWrite?: (driver: ProjectDriver) => Promise<void>;
10
+ }
11
+ /**
12
+ * 按驱动产出一组 `cloud.db` 能力。驱动已绑定项目库(连接层隔离,DB-001);
13
+ * `projectId` 在装配期绑定,函数侧不可感知。内部无共享可变状态污染跨项目调用。
14
+ */
15
+ export declare function createDbCapability(driver: ProjectDriver, options?: CreateDbCapabilityOptions): CapabilityBundle;
@@ -0,0 +1,59 @@
1
+ /**
2
+ * 项目库驱动端口(DB-001,DB-007 扩展定位抽象):业务域对「项目库连接」的唯一依赖面(纯类型)。
3
+ *
4
+ * 本文件自 `server/db/repo/interface.ts` 拆分而来(CLI 发布重构):接口里还驻留平台元数据库
5
+ * 一族(`PlatformDb` / 各 Repository / `TransactionRunner`),那些依赖 drizzle schema,属重栈;
6
+ * 而 `ProjectDriver` 一族是**零平台依赖**的纯类型端口,被 `@adep/runtime` 的 cloud.db 布局消费。
7
+ * 因此把这一族下沉到 runtime 包,`server/db/repo/interface.ts` re-export 本文件以保持既有 import
8
+ * 兼容(`ProjectDriver` / `ProjectTarget` / `ProjectDriverFactory` / `ProjectDbLocator`)。
9
+ *
10
+ * 隔离纪律(PRD §4.2 / 规范 §6.1):驱动在 open 时绑定**单个**项目库目标,平台侧不提供
11
+ * ATTACH / 多库句柄——跨项目隔离在连接层成立,而非靠 SQL 里 WHERE 过滤。
12
+ * 实现侧:Bun 为 driver/bun.ts 的 openProjectSqliteFile;测试用 node:sqlite 适配(server/db/__tests__)。
13
+ * 参数一律经绑定传递(实现不得拼接)。
14
+ *
15
+ * 执行方法为 async(DB-007):PG 是网络 I/O,同步契约无法承载;SQLite 实现返回已决 Promise。
16
+ */
17
+ /** 项目库驱动(runtime 侧结构化类型):`ProjectDriver` 引擎形态由构建器方言 / 系统目录查询分派。 */
18
+ export interface ProjectDriver {
19
+ /** 项目库引擎形态(DB-007):构建器方言 / 系统目录查询据此分派。 */
20
+ readonly engine: 'sqlite' | 'pg';
21
+ /** 执行写语句(DDL / DML),返回受影响行数。 */
22
+ run(sql: string, params?: readonly unknown[]): Promise<{
23
+ changes: number;
24
+ }>;
25
+ /** 执行查询,返回对象行。 */
26
+ all(sql: string, params?: readonly unknown[]): Promise<Array<Record<string, unknown>>>;
27
+ /** 执行单行查询,无行返回 null。 */
28
+ get(sql: string, params?: readonly unknown[]): Promise<Record<string, unknown> | null>;
29
+ /** 关闭连接(PG 形态需异步释放网络连接,故为 Promise)。 */
30
+ close(): Promise<void>;
31
+ }
32
+ /**
33
+ * 项目库连接目标(DB-007 定位抽象):sqlite 文件路径 或 PG schema 名。
34
+ * 由平台侧从 projectId 推导(locator),用户输入永远到不了连接目标
35
+ * (DB-001 评审检查单 ①③ 的目标侧落点)。
36
+ */
37
+ export type ProjectTarget = {
38
+ readonly kind: 'sqlite';
39
+ readonly file: string;
40
+ } | {
41
+ readonly kind: 'pg';
42
+ readonly schema: string;
43
+ };
44
+ /**
45
+ * 项目库驱动工厂:接收**已由平台侧判定的连接目标**,返回绑定该目标的驱动。
46
+ * 工厂只接受目标参数——不存在「接受任意库名/attached 库」的重载(DB-001 评审检查单 ①)。
47
+ */
48
+ export type ProjectDriverFactory = (target: ProjectTarget) => Promise<ProjectDriver>;
49
+ /**
50
+ * 项目库目标定位器(DB-007):projectId → 连接目标的唯一判定入口。
51
+ * 引擎按部署级配置选定且**按项目固定**(§2.7 启动后不可改):已供给项目按其既有引擎
52
+ * 返回目标(sqlite 探文件 / pg 探 schema),未供给项目由 `ensure` 按部署配置给出新目标。
53
+ */
54
+ export interface ProjectDbLocator {
55
+ /** 已供给项目的连接目标;从未供给 → null(纯探测,不产生任何创建副作用)。 */
56
+ locate(projectId: string): Promise<ProjectTarget | null>;
57
+ /** 供给目标(幂等;sqlite 形态由 open 隐式建文件,pg 形态执行 schema/角色 DDL)。 */
58
+ ensure(projectId: string): Promise<ProjectTarget>;
59
+ }
@@ -0,0 +1,33 @@
1
+ /** 清单在函数文件组中的固定路径。 */
2
+ export declare const MANIFEST_PATH = "package.json";
3
+ /** 每个清单的依赖条目上限(防巨型清单拖垮安装步骤;体积另有 DEPS_MAX_INSTALL_MB 兜底)。 */
4
+ export declare const MANIFEST_MAX_DEPS = 100;
5
+ /** 判定 spec 是否为语义化版本范围(见模块头注口径)。 */
6
+ export declare function isVersionRangeSpec(spec: string): boolean;
7
+ /** 判定 spec 是否为精确版本(无范围语义,重装恒定)。 */
8
+ export declare function isExactVersion(spec: string): boolean;
9
+ /** 解析清单 JSON 的 dependencies;形态 / 键名 / spec 非法一律 400(发布即失败,可操作)。 */
10
+ export declare function parseManifestDependencies(content: string): Record<string, string>;
11
+ /** 执行侧宽松解析:清单损坏(草稿态)按无清单处理,错误在发布时才 fail loud。 */
12
+ export declare function tryParseManifestDependencies(content: string | undefined): Record<string, string>;
13
+ export interface ClassifiedDependencies {
14
+ /** 命中内置白名单:零安装直用(秒级冷启动口径)。 */
15
+ builtinDeps: Record<string, string>;
16
+ /** 自定义依赖:发布时装载到项目隔离目录。 */
17
+ customDeps: Record<string, string>;
18
+ }
19
+ export declare function classifyDependencies(dependencies: Readonly<Record<string, string>>, builtin: ReadonlyArray<string>): ClassifiedDependencies;
20
+ /**
21
+ * 裸说明符的包根名:`dayjs/plugin/utc` → `dayjs`,`@scope/pkg/sub` → `@scope/pkg`。
22
+ * 白名单与清单匹配都发生在包根粒度(子路径随包放行)。
23
+ */
24
+ export declare function splitBareSpecifier(request: string): string;
25
+ /**
26
+ * 依赖集合的内容寻址键:发布侧(pinned 清单)与执行侧(版本快照内的清单)走同一函数,
27
+ * 恒等输入恒等输出——这是「执行时定位到发布时装载的那个目录」的唯一约定。
28
+ */
29
+ export declare function depsKeyOf(dependencies: Readonly<Record<string, string>>): string;
30
+ /** 清单的规范化序列化(键排序,确定性):写入版本快照的 pinned 形态。 */
31
+ export declare function serializeManifest(dependencies: Readonly<Record<string, string>>): string;
32
+ /** 配置里的逗号分隔名单(内置依赖白名单 / registry 白名单)解析为去空白数组。 */
33
+ export declare function parseNameList(value: string): string[];
@@ -0,0 +1,20 @@
1
+ /** worker 侧裸模块解析所需的最小信息(经 workerData 结构化克隆传入)。 */
2
+ export interface WorkerDeps {
3
+ /** 项目自定义依赖目录(内含 node_modules);null = 无自定义依赖。 */
4
+ dir: string | null;
5
+ /** 内置白名单(包根名):映射平台 node_modules。 */
6
+ builtin: readonly string[];
7
+ /** 清单声明的自定义依赖(包根名):映射项目隔离目录。 */
8
+ custom: readonly string[];
9
+ }
10
+ export interface ExecutionDepsConfig {
11
+ /** DEPS_DIR(绝对或相对进程 cwd)。 */
12
+ rootDir: string;
13
+ /** 内置依赖白名单(FUNCTIONS_BUILTIN_DEPS)。 */
14
+ builtin: readonly string[];
15
+ }
16
+ /**
17
+ * 由执行入参定位依赖:manifestContent 为版本快照(或草稿)内的 package.json 原文。
18
+ * 内置依赖不需要任何声明即可用(零安装直用);自定义依赖必须出现在清单里。
19
+ */
20
+ export declare function resolveExecutionDeps(config: ExecutionDepsConfig, projectId: string, manifestContent: string | undefined): WorkerDeps;
@@ -0,0 +1,28 @@
1
+ /**
2
+ * 云函数域规则(FN-001):命名、路径、体积的**域级**校验。
3
+ *
4
+ * 放在域层而非 Elysia t.* 的原因(规范 §5 边界的补集):函数名 / 路径规则是**领域约束**,
5
+ * 失败要返回带规则说明的 400 `FN_*` 错误(验收标准要求 message 说明命名规则),
6
+ * 而不是结构校验的通用 `VALIDATION_FAILED` 信封。请求体的**结构**形状仍由 t.* 声明(OpenAPI 可见)。
7
+ */
8
+ export declare const FUNCTION_NAME_PATTERN: RegExp;
9
+ export declare const FUNCTION_NAME_RULE = "\u51FD\u6570\u540D\u9700\u4EE5\u5C0F\u5199\u5B57\u6BCD\u5F00\u5934\uFF0C\u4EC5\u542B\u5C0F\u5199\u5B57\u6BCD / \u6570\u5B57 / \u8FDE\u5B57\u7B26\uFF0C\u6700\u957F 63 \u4F4D";
10
+ /** 单函数源码总量上限(PRD §2.1.3:≤ 256KB,超出走后续对象存储方案)。 */
11
+ export declare const MAX_TOTAL_SOURCE_BYTES: number;
12
+ /** 文件数上限:草稿是 IDE 工作集,不是文件托管。 */
13
+ export declare const MAX_FILES = 50;
14
+ /** 入口文件(FN-002 执行器按此触发)。 */
15
+ export declare const ENTRY_FILE = "index.ts";
16
+ /** FN_* 域错误:路由层把它转成统一信封响应。 */
17
+ export declare class FnError extends Error {
18
+ readonly status: number;
19
+ readonly code: string;
20
+ constructor(status: number, code: string, message: string);
21
+ }
22
+ export declare function validateFunctionName(name: string): void;
23
+ export interface DraftFile {
24
+ path: string;
25
+ content: string;
26
+ }
27
+ /** 校验草稿文件组:路径形态、数量、总量、入口文件存在。 */
28
+ export declare function validateDraftFiles(files: Readonly<Record<string, string>>): DraftFile[];
@@ -0,0 +1,18 @@
1
+ /**
2
+ * FunctionExecutor 契约(FN-002,能力缝 Definition 层)。
3
+ *
4
+ * M1 实现 = WorkerFunctionExecutor(worker_threads + vm + 资源限额);
5
+ * M6 容器池实现同接口替换(Definition/Provider/Consumer 三件套的 Definition)。
6
+ *
7
+ * **CORE-018**:跨域共享契约已上浮——纯类型在 `@adep/types`(packages/types/function-executor),
8
+ * 运行时(ExecutorError / 限额错误码)在 `shared/executor-runtime`,能力键在 `shared/capability-keys`。
9
+ * 本文件只做 **re-export 兼容层**(functions 域内部既有 import 保持可用),不含任何定义。
10
+ *
11
+ * 限额口径(PRD §3.2.1 / 任务单):默认 128MB / 10s;超时 → `FN_EXEC_TIMEOUT`,
12
+ * 内存耗尽 → `FN_EXEC_OOM`;两者都是 504/5xx 级执行失败,函数代码不可见。
13
+ */
14
+ export type { CapabilityBundle, CapabilityRpcHandler, ExecuteInput, ExecuteResult, ExecutorCapability, ExecutorFunction, ExecutorProject, ExecutorRequest, FunctionExecutor, } from '@adep/types';
15
+ export { RPC_CAPABILITY_KEY } from '../../shared/capability-keys';
16
+ export { ExecutorError, OOM_CODE, TIMEOUT_CODE, DEFAULT_TIMEOUT_MS, DEFAULT_MEMORY_LIMIT_MB, normalizeHttpStatus, isHttpStatus, } from '../../shared/executor-runtime';
17
+ export type { ChainRequest, ChainSpec, ChainStep } from '@adep/types';
18
+ export { CHAIN_CAPABILITY_KEY, DB_RPC } from '../../shared/capability-keys';
@@ -0,0 +1,30 @@
1
+ import type { ExecuteInput, ExecuteResult, FunctionExecutor } from './executor';
2
+ import type { ExecutionDepsConfig } from '../deps/resolve';
3
+ /**
4
+ * worker 入口的运行期定位:dev / vitest 直接加载 TS 源;bun build 产物里是独立 bundle
5
+ * (worker-entry.js,随 build 脚本的第二入口产出)。存在性探测保证两侧都能起 worker。
6
+ *
7
+ * 历史坑(Web IDE 运行报 FN_EXEC_ERROR 复现):`import.meta.url` 在 nitro dev(.nuxt/dev/index.mjs)
8
+ * 与 build 产物(.output/app/main.js)里都指向合并后的主 bundle,不再与 worker-entry 同目录,
9
+ * 仅靠同目录探测会找不到入口。因此除同级探测外,还要从 bundle 目录向仓库 / 产物根逐级上溯,
10
+ * 用固定包前缀等价命中 .output/app/packages/runtime/...(build 独立第二入口)与
11
+ * 仓库根 packages/runtime/...(dev 直载源)两种布局。`baseDir` 仅测试注入,运行时缺省取 import.meta.url。
12
+ */
13
+ export declare function resolveWorkerEntry(baseDir?: string): URL;
14
+ /** 依赖解析配置(FN-009);缺省 = 零依赖形态(M1 行为:裸模块全部拒绝)。 */
15
+ export interface WorkerExecutorOptions {
16
+ deps?: ExecutionDepsConfig;
17
+ /** 沙箱受控网络配置(GAME-007);缺省 = 不注入 fetch(FN-002 无网络安全默认)。 */
18
+ fetch?: {
19
+ allowlist: readonly string[];
20
+ timeoutMs: number;
21
+ maxResponseBytes?: number;
22
+ };
23
+ }
24
+ export declare class WorkerFunctionExecutor implements FunctionExecutor {
25
+ private readonly depsConfig;
26
+ private readonly fetchConfig;
27
+ constructor(options?: WorkerExecutorOptions);
28
+ execute(input: ExecuteInput): Promise<ExecuteResult>;
29
+ dispose(): Promise<void>;
30
+ }
@@ -0,0 +1,35 @@
1
+ /**
2
+ * `@adep/runtime` —— 运行时内核(零重栈依赖)。
3
+ *
4
+ * 平台云函数的「执行器 + 本地模拟运行时 + 能力装配」在三处复用(server 网关/触发器/MCP /
5
+ * 组合根项目库装配、packages/cli 的 `adep dev`、以及浏览器侧离线工作区契约):
6
+ * 此前这些实现栖身 `server/domains/*`,CLI 为复用不受控地 import server 内部实现,导致
7
+ * `packages/cli` 无法独立发布 npm。本包把这一块抽成**零平台重栈**(无 elysia / drizzle-orm /
8
+ * better-auth / nuxt / bun:sqlite / postgres / ioredis)的独立包,三者共用:
9
+ *
10
+ * - `functions/runtime/executor`:`ShopFunctionExecutor` 与 `ExecutorError`、限额码、worker 执行器;
11
+ * - `functions/deps/*`:依赖清单解析 / 执行侧依赖定位;
12
+ * - `database/builder` + `database/sdk/cloud`:`cloud.db` 链式查询构建器与能力装配;
13
+ * - `storage/*`:`cloud.storage` 驱动接口、本地驱动、签名 URL;
14
+ * - `db/project-driver`:项目库驱动端口(`ProjectDriver` / `ProjectTarget` / `ProjectDriverFactory` / `ProjectDbLocator`);
15
+ * - `functions/boundary`(`shared/sim-contract.ts`):本地模拟运行时契约期望表。
16
+ *
17
+ * 重栈(server-only:elysia / drizzle / bun:sqlite / postgres / better-auth / nuxt)**不进入**本包。
18
+ *
19
+ * > 本文件刻意只 re-export 纯类型与纯值;具体子模块按 `@adep/runtime/<subpath>` 深导入
20
+ * > (worker 链路用 `.ts` 显式扩展名,见 `functions/runtime/worker-entry.ts` 头注释)。
21
+ */
22
+ export type { ProjectDriver, ProjectTarget, ProjectDriverFactory, ProjectDbLocator, } from './db/project-driver';
23
+ export type { CapabilityBundle, CapabilityRpcHandler, ExecuteInput, ExecuteResult, ExecutorCapability, ExecutorFunction, ExecutorProject, ExecutorRequest, FunctionExecutor, } from '@adep/types';
24
+ export { ExecutorError, TIMEOUT_CODE, OOM_CODE, DEFAULT_TIMEOUT_MS, DEFAULT_MEMORY_LIMIT_MB, } from './shared/executor-runtime';
25
+ export { RPC_CAPABILITY_KEY, CHAIN_CAPABILITY_KEY, DB_RPC } from './shared/capability-keys';
26
+ export { WorkerFunctionExecutor, type WorkerExecutorOptions, } from './functions/runtime/worker-executor';
27
+ export { createDbCapability, type CreateDbCapabilityOptions } from './database/sdk/cloud';
28
+ export { createCloudDb, type CreateCloudDbOptions } from './database/builder';
29
+ export { createStorageCapability } from './storage/cloud';
30
+ export { createLocalStorageDriver, type LocalStorageOptions } from './storage/driver/local';
31
+ export type { StorageDriver, StoredFileMeta, FileVisibility } from './storage/driver';
32
+ export { StorageError, STORAGE_CODES } from './storage/driver';
33
+ export { signDownloadUrl, verifyDownloadSignature } from './storage/signature';
34
+ export type { DownloadSignerConfig } from './storage/signature';
35
+ export { resolveFunctionSource, parseSnapshot } from './shared/function-source';