@springbrand/agent-runtime 0.1.3-alpha.1 → 0.1.3-alpha.10

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.
Files changed (76) hide show
  1. package/package.json +12 -3
  2. package/src/adapter/cloudflare/index.ts +56 -0
  3. package/src/adapter/cloudflare/resources/runtime-resources.ts +89 -0
  4. package/src/adapter/cloudflare/sandbox/adapter.ts +1513 -0
  5. package/src/adapter/cloudflare/sandbox/id.ts +23 -0
  6. package/src/adapter/cloudflare/sandbox/policy.ts +15 -0
  7. package/src/adapter/cloudflare/subagent/definition.ts +574 -0
  8. package/src/adapter/cloudflare/subagent/runner.ts +175 -0
  9. package/src/adapter/cloudflare/subagent/tools.ts +254 -0
  10. package/src/adapter/cloudflare/universal-agent/hooks.ts +35 -0
  11. package/src/adapter/cloudflare/universal-agent/preparation.ts +277 -0
  12. package/src/adapter/cloudflare/universal-agent/tools.ts +80 -0
  13. package/src/adapter/cloudflare/workspace/git-fs.ts +178 -0
  14. package/src/adapter/cloudflare/workspace/publisher.ts +31 -0
  15. package/src/adapter/cloudflare/workspace/scoped-workspace.ts +376 -0
  16. package/src/adapter/cloudflare/workspace/version-control.ts +374 -0
  17. package/src/agent-tool-runtime.ts +152 -0
  18. package/src/db/agent-tool.repo.ts +27 -0
  19. package/src/db/index.ts +33 -0
  20. package/src/db/interaction.repo.ts +185 -0
  21. package/src/db/schema.ts +25 -1
  22. package/src/db/submission.repo.ts +63 -1
  23. package/src/index.ts +57 -21
  24. package/src/kernel/approval-lifecycle.ts +41 -6
  25. package/src/kernel/bindings.ts +73 -9
  26. package/src/kernel/interaction-lifecycle.ts +395 -0
  27. package/src/kernel/public-contracts.ts +2 -0
  28. package/src/kernel/recoverable-chat-agent.ts +104 -6
  29. package/src/kernel/runtime-assembly-view.ts +37 -0
  30. package/src/kernel/runtime-assembly.ts +41 -0
  31. package/src/kernel/runtime-config.ts +4 -0
  32. package/src/kernel/runtime-load.ts +191 -0
  33. package/src/kernel/state.ts +12 -1
  34. package/src/kernel/submission-lifecycle.ts +33 -2
  35. package/src/layers/orchestration/temporary-agent/core.ts +12 -1
  36. package/src/layers/orchestration/temporary-agent/runner.ts +1 -2
  37. package/src/lib/mcp.ts +7 -3
  38. package/src/lib/prompt.ts +1 -1
  39. package/src/lib/telemetry-dev.ts +7 -4
  40. package/src/pi/assembly/context.ts +3 -3
  41. package/src/pi/assembly/extensions.ts +11 -22
  42. package/src/pi/assembly/snapshot.ts +6 -3
  43. package/src/pi/message/contract.ts +7 -0
  44. package/src/pi/message/conversion.ts +9 -1
  45. package/src/pi/runtime-adapter/assembly.ts +26 -31
  46. package/src/pi/runtime-adapter/execution.ts +198 -15
  47. package/src/pi/runtime-adapter/index.ts +24 -8
  48. package/src/pi/runtime-adapter/models.ts +382 -35
  49. package/src/pi/runtime-adapter/recovery.ts +188 -1
  50. package/src/pi/runtime-adapter/transcript.ts +61 -3
  51. package/src/pi/tool/ai-adapter.ts +58 -1
  52. package/src/pi/tool/base.ts +190 -12
  53. package/src/pi/tool/compiler.ts +34 -1
  54. package/src/pi/tool/core-host.ts +19 -24
  55. package/src/pi/tool/core.ts +30 -120
  56. package/src/pi/tool/gateway.ts +54 -0
  57. package/src/pi/tool/index.ts +2 -0
  58. package/src/pi/tool/mcp.ts +96 -68
  59. package/src/pi/tool/schedule.ts +41 -19
  60. package/src/pi/tool/skill.ts +126 -420
  61. package/src/pi/tool/subagent.ts +14 -2
  62. package/src/pi/tool/web-fetch.ts +281 -0
  63. package/src/pi/tool/web-search/api.ts +34 -18
  64. package/src/pi/tool/web-search/web-search.ts +0 -1
  65. package/src/pi/tool/workspace-revision.ts +64 -0
  66. package/src/pi/tool/workspace-sandbox.ts +105 -263
  67. package/src/pi/turn/index.ts +20 -0
  68. package/src/pi/turn/interaction.ts +181 -0
  69. package/src/pi/turn/tool-recovery.ts +244 -1
  70. package/src/runtime-agent-context.ts +112 -0
  71. package/src/runtime-agent.ts +568 -321
  72. package/src/{plugins.ts → runtime-assembler.ts} +372 -398
  73. package/src/runtime-definition.ts +175 -0
  74. package/src/runtime.ts +835 -204
  75. package/src/tool-registry.ts +143 -0
  76. package/src/workspace-versioning.ts +46 -0
@@ -0,0 +1,143 @@
1
+ import type { ExecutionLevel } from "./lib/execution-level";
2
+ import type {
3
+ RuntimeMemoryPort,
4
+ WorkspacePort,
5
+ } from "./kernel/bindings";
6
+ import type { RuntimeExtensionConfig } from "./kernel/extensions";
7
+ import type { RuntimeDegradation } from "./kernel/degradation";
8
+ import type { PiToolCandidate } from "./pi/tool/compiler";
9
+
10
+ /** Tool Surface 筛选策略(不含 Manifest deny 列表)。 */
11
+ export interface ToolSurfaceSelectionPolicy {
12
+ readonly allowsTool?: (name: string) => boolean;
13
+ readonly allowsExtension?: (extension: RuntimeExtensionConfig) => boolean;
14
+ }
15
+
16
+ /** 模型调用 Tool 时传给 `execute` 的上下文。 */
17
+ export interface ToolContext {
18
+ readonly toolCallId: string;
19
+ readonly signal: AbortSignal;
20
+ }
21
+
22
+ /** Definition 作者声明的一个 Tool。 */
23
+ export interface ToolSpec {
24
+ readonly label: string;
25
+ readonly description: string;
26
+ readonly parameters: unknown;
27
+ readonly requiredExecutionLevel: ExecutionLevel;
28
+ /** Require a fresh human decision for every call, regardless of execution level. */
29
+ readonly alwaysRequiresApproval?: boolean;
30
+ readonly execute: (
31
+ input: unknown,
32
+ ctx: ToolContext,
33
+ ) => Promise<unknown>;
34
+ }
35
+
36
+ /** 一次装配声明的全部 Tool,键即模型可见的工具名。 */
37
+ export type ToolRegistry = Record<string, ToolSpec>;
38
+
39
+ export function emptyToolRegistry(): ToolRegistry {
40
+ return {};
41
+ }
42
+
43
+ export function mergeToolRegistries(...registries: ToolRegistry[]): ToolRegistry {
44
+ const merged: ToolRegistry = {};
45
+ for (const registry of registries) {
46
+ for (const [name, spec] of Object.entries(registry)) {
47
+ if (name in merged) {
48
+ throw new Error(`Duplicate Runtime Tool: ${name}`);
49
+ }
50
+ merged[name] = spec;
51
+ }
52
+ }
53
+ return merged;
54
+ }
55
+
56
+ export function toolRegistryFromPiCandidates(
57
+ candidates:
58
+ | readonly PiToolCandidate[]
59
+ | Record<string, PiToolCandidate>,
60
+ ): ToolRegistry {
61
+ const list = Array.isArray(candidates)
62
+ ? candidates
63
+ : Object.values(candidates);
64
+ const registry: ToolRegistry = {};
65
+ for (const candidate of list) {
66
+ const name = candidate.tool.name;
67
+ if (!name.trim()) {
68
+ throw new Error("Pi Tool name must not be empty");
69
+ }
70
+ registry[name] = piCandidateToToolSpec(candidate);
71
+ }
72
+ return registry;
73
+ }
74
+
75
+ export function piCandidateToToolSpec(candidate: PiToolCandidate): ToolSpec {
76
+ const name = candidate.tool.name;
77
+ return {
78
+ label: candidate.tool.label ?? name,
79
+ description: candidate.tool.description,
80
+ parameters: candidate.tool.parameters,
81
+ requiredExecutionLevel: candidate.requiredExecutionLevel,
82
+ ...(candidate.alwaysRequiresApproval
83
+ ? { alwaysRequiresApproval: true }
84
+ : {}),
85
+ execute: async (input, ctx) => {
86
+ const execute = candidate.tool.execute;
87
+ if (typeof execute !== "function") {
88
+ throw new Error(`Tool "${name}" is not executable`);
89
+ }
90
+ const result = await execute(
91
+ ctx.toolCallId,
92
+ input,
93
+ ctx.signal,
94
+ undefined,
95
+ );
96
+ return result.details ?? result;
97
+ },
98
+ };
99
+ }
100
+
101
+ /** Kernel 绑定仍需要的执行后端;不出现在 Definition 公开类型里。 */
102
+ export interface RuntimeToolBindings {
103
+ readonly workspace?: WorkspacePort;
104
+ readonly memory?: RuntimeMemoryPort;
105
+ }
106
+
107
+ /**
108
+ * Worker `tools()` 可返回纯注册表,或附带 Kernel 绑定与降级信息。
109
+ */
110
+ export interface ToolAssemblyResult {
111
+ readonly tools: ToolRegistry;
112
+ readonly bindings?: RuntimeToolBindings;
113
+ readonly memoryProfile?: import("./kernel/profile").RuntimeMemoryProfile;
114
+ readonly enabledSubagents?: readonly string[];
115
+ readonly degradations?: readonly RuntimeDegradation[];
116
+ readonly surfacePolicy?: ToolSurfaceSelectionPolicy;
117
+ readonly commitGuard?: () => Promise<void>;
118
+ /** 释放本次装配创建的外部资源;临时 Agent 终止后由 Runtime 调用。 */
119
+ readonly dispose?: () => Promise<void>;
120
+ }
121
+
122
+ export function normalizeToolAssembly(
123
+ input: ToolRegistry | ToolAssemblyResult,
124
+ ): ToolAssemblyResult {
125
+ if ("tools" in input && typeof input.tools === "object" && input.tools !== null) {
126
+ return input as ToolAssemblyResult;
127
+ }
128
+ return { tools: input as ToolRegistry };
129
+ }
130
+
131
+ /** @deprecated 使用 `ToolSpec`。 */
132
+ export type PlatformToolSpec = ToolSpec;
133
+
134
+ /** @deprecated 使用 `ToolContext`。 */
135
+ export type PlatformToolContext = ToolContext;
136
+
137
+ /** @deprecated 使用 `ToolRegistry`。 */
138
+ export type PlatformToolRegistry = ToolRegistry;
139
+
140
+ /** @deprecated 使用 `emptyToolRegistry`。 */
141
+ export function emptyPlatformToolRegistry(): ToolRegistry {
142
+ return emptyToolRegistry();
143
+ }
@@ -0,0 +1,46 @@
1
+ export type WorkspaceRevision = {
2
+ revision: string;
3
+ treeHash: string;
4
+ reason: string;
5
+ createdAt: number;
6
+ };
7
+
8
+ export type WorkspaceRevisionManifest = {
9
+ revision: string;
10
+ treeHash: string;
11
+ files: readonly {
12
+ path: string;
13
+ size: number;
14
+ }[];
15
+ };
16
+
17
+ export interface WorkspaceVersionPort {
18
+ current(): Promise<WorkspaceRevision | null>;
19
+ checkpoint(input: { reason: string }): Promise<{
20
+ revision: string | null;
21
+ treeHash: string | null;
22
+ changed: boolean;
23
+ }>;
24
+ list(input?: { limit?: number }): Promise<readonly WorkspaceRevision[]>;
25
+ manifest(revision: string): Promise<WorkspaceRevisionManifest>;
26
+ readFile(revision: string, path: string): Promise<Uint8Array>;
27
+ restore(input: { revision: string; reason: string }): Promise<{
28
+ revision: string;
29
+ treeHash: string;
30
+ }>;
31
+ }
32
+
33
+ /** Host-scoped restore action exposed to the Agent Runtime. */
34
+ export interface WorkspaceRevisionRestorePort {
35
+ restore(target: string): Promise<{
36
+ restoredFrom: string;
37
+ revision: string;
38
+ treeHash: string;
39
+ }>;
40
+ }
41
+
42
+ export class WorkspaceRevisionNotFoundError extends Error {
43
+ constructor() {
44
+ super("workspace_revision_not_found");
45
+ }
46
+ }