@springbrand/agent-runtime 0.3.0-alpha.2 → 0.3.0-alpha.21

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 (131) hide show
  1. package/package.json +11 -9
  2. package/src/adapter/cloudflare/agent.ts +233 -179
  3. package/src/adapter/cloudflare/define-agent.ts +252 -104
  4. package/src/adapter/cloudflare/index.ts +18 -0
  5. package/src/adapter/cloudflare/scheduling/schedule-rule.ts +322 -0
  6. package/src/adapter/cloudflare/scheduling/zoned-cron.ts +11 -6
  7. package/src/adapter/cloudflare/schema.ts +21 -2
  8. package/src/adapter/cloudflare/subagent/definition.ts +25 -13
  9. package/src/adapter/cloudflare/subagent/runner.ts +43 -50
  10. package/src/adapter/cloudflare/subagent/tools.ts +4 -4
  11. package/src/adapter/cloudflare/universal-agent/preparation.ts +3 -3
  12. package/src/adapter/cloudflare/universal-agent/tools.ts +4 -6
  13. package/src/adapter/cloudflare/workspace/version-control.ts +1 -1
  14. package/src/config/activation.ts +13 -15
  15. package/src/config/agent-context.ts +1 -1
  16. package/src/config/assembler.ts +48 -31
  17. package/src/config/assembly-snapshot.ts +1 -1
  18. package/src/config/definition.ts +1 -1
  19. package/src/config/harness-runtime.ts +77 -49
  20. package/src/config/index.ts +1 -1
  21. package/src/config/model-guidance.ts +20 -0
  22. package/src/config/ports.ts +57 -7
  23. package/src/config/{pinning.ts → prepared-runtime.ts} +59 -123
  24. package/src/config/public-contracts.ts +2 -2
  25. package/src/config/runtime-config.ts +2 -1
  26. package/src/context/assembly.ts +1 -1
  27. package/src/context/extension/assembly.ts +2 -2
  28. package/src/context/memory/budget/gate.ts +19 -3
  29. package/src/conversation/harness-resume.ts +89 -238
  30. package/src/conversation/harness-stream.ts +87 -71
  31. package/src/conversation/history.ts +154 -152
  32. package/src/conversation/message/contract.ts +28 -4
  33. package/src/conversation/message/conversion.ts +46 -5
  34. package/src/conversation/message/projection.ts +61 -21
  35. package/src/conversation/session-entry.ts +29 -0
  36. package/src/conversation/state.ts +23 -2
  37. package/src/harness/governance.ts +206 -0
  38. package/src/harness/index.ts +3 -0
  39. package/src/harness/session-harness.ts +269 -445
  40. package/src/harness/submission-operation.ts +15 -40
  41. package/src/index.ts +18 -8
  42. package/src/lib/artifacts.ts +18 -0
  43. package/src/lib/execution-level.ts +13 -0
  44. package/src/lib/prompt.ts +9 -10
  45. package/src/lib/token-estimate.ts +8 -0
  46. package/src/model-protocol/openrouter.ts +41 -0
  47. package/src/models/index.ts +200 -35
  48. package/src/models/openrouter/messages.ts +13 -8
  49. package/src/models/openrouter/openai.ts +200 -0
  50. package/src/models/openrouter/protocol-response.ts +85 -0
  51. package/src/models/provider-streams.ts +45 -0
  52. package/src/session/cloudflare/migration.ts +382 -864
  53. package/src/session/cloudflare/storage.ts +13 -536
  54. package/src/session/index.ts +22 -35
  55. package/src/skills/springbrand-worker-website/index.ts +1 -1
  56. package/src/submission/admission.ts +20 -30
  57. package/src/submission/contracts.ts +42 -2
  58. package/src/submission/delivery.ts +1 -1
  59. package/src/submission/finalization.ts +36 -15
  60. package/src/submission/lifecycle.ts +28 -15
  61. package/src/submission/repository.ts +97 -49
  62. package/src/submission/steer-delivery.ts +32 -76
  63. package/src/submission/unattended.ts +40 -0
  64. package/src/telemetry/contract.ts +0 -1
  65. package/src/telemetry/coordinator.ts +1 -1
  66. package/src/telemetry/runtime-telemetry.ts +0 -4
  67. package/src/telemetry/subagent-lifecycle.ts +1 -1
  68. package/src/telemetry/turn-events.ts +35 -25
  69. package/src/tools/README.md +44 -0
  70. package/src/tools/{ai-adapter.ts → adapters/ai-to-pi.ts} +6 -9
  71. package/src/tools/{declared.ts → adapters/declared.ts} +6 -2
  72. package/src/tools/{nested-tools.ts → adapters/pi-to-ai.ts} +4 -4
  73. package/src/tools/assistant/ask-user.ts +319 -0
  74. package/src/tools/assistant/candidate.ts +15 -0
  75. package/src/tools/assistant/followups.ts +34 -0
  76. package/src/tools/assistant/index.ts +19 -0
  77. package/src/tools/assistant/memory.ts +73 -0
  78. package/src/tools/assistant/plan.ts +98 -0
  79. package/src/tools/{time.ts → assistant/time.ts} +1 -1
  80. package/src/tools/{core-host.ts → codemode/adapter.ts} +50 -53
  81. package/src/tools/{core.ts → codemode/index.ts} +36 -92
  82. package/src/tools/codemode/workspace-connector.ts +198 -0
  83. package/src/tools/extension/index.ts +42 -0
  84. package/src/tools/index.ts +29 -26
  85. package/src/tools/{gateway.ts → mcp/gateway.ts} +19 -19
  86. package/src/tools/{mcp.ts → mcp/index.ts} +5 -5
  87. package/src/tools/runtime/activation.ts +40 -0
  88. package/src/tools/{approval → runtime/approval}/repository.ts +13 -12
  89. package/src/tools/runtime/compiler.ts +246 -0
  90. package/src/tools/runtime/contracts.ts +140 -0
  91. package/src/tools/{decision-gate.ts → runtime/decision-gate.ts} +149 -39
  92. package/src/tools/{human-decisions.ts → runtime/human-decisions.ts} +4 -4
  93. package/src/tools/{interaction → runtime/interaction}/repository.ts +1 -7
  94. package/src/tools/runtime/result.ts +22 -0
  95. package/src/tools/runtime/unattended-error.ts +4 -0
  96. package/src/tools/sandbox/index.ts +228 -0
  97. package/src/tools/schedule/index.ts +301 -0
  98. package/src/tools/skill/contracts.ts +20 -0
  99. package/src/tools/skill/index.ts +99 -0
  100. package/src/tools/skill/resources.ts +322 -0
  101. package/src/tools/skill/scripts.ts +168 -0
  102. package/src/tools/{subagent-authority.ts → subagent/authority.ts} +31 -31
  103. package/src/tools/{subagent.ts → subagent/index.ts} +34 -37
  104. package/src/tools/{subagent-run-repository.ts → subagent/repository.ts} +1 -1
  105. package/src/tools/{subagent-runtime.ts → subagent/runtime.ts} +1 -1
  106. package/src/tools/{temporary-agent → subagent/temporary}/approvals.ts +4 -7
  107. package/src/tools/{temporary-agent → subagent/temporary}/core.ts +2 -3
  108. package/src/tools/{temporary-agent → subagent/temporary}/runner.ts +1 -1
  109. package/src/tools/{temporary-agent → subagent/temporary}/workspace.ts +1 -4
  110. package/src/tools/{web-fetch.ts → web/fetch.ts} +4 -13
  111. package/src/tools/web/index.ts +2 -0
  112. package/src/tools/web/search/api.ts +1322 -0
  113. package/src/tools/web/search/index.ts +2 -0
  114. package/src/tools/{web-search/web-search.ts → web/search/search.ts} +5 -7
  115. package/src/tools/{workspace-sandbox.ts → workspace/index.ts} +12 -272
  116. package/src/tools/{workspace-revision.ts → workspace/revision.ts} +3 -3
  117. package/src/tools/base.ts +0 -534
  118. package/src/tools/compiler.ts +0 -327
  119. package/src/tools/schedule.ts +0 -261
  120. package/src/tools/skill.ts +0 -565
  121. package/src/tools/web-search/api.ts +0 -1318
  122. package/src/tools/web-search/index.ts +0 -2
  123. package/src/tools/workspace-state-connector.ts +0 -146
  124. package/src/tools/{approval → runtime/approval}/index.ts +0 -0
  125. package/src/tools/{interaction → runtime/interaction}/index.ts +0 -0
  126. package/src/tools/{temporary-agent → subagent/temporary}/coordinator.ts +0 -0
  127. package/src/tools/{subagent-types → subagent/types}/contract.ts +0 -0
  128. package/src/tools/{subagent-types → subagent/types}/extract/index.ts +0 -0
  129. package/src/tools/{subagent-types → subagent/types}/fanout/index.ts +0 -0
  130. package/src/tools/{subagent-types → subagent/types}/registry.ts +1 -1
  131. /package/src/tools/{workspace-versioning.ts → workspace/versioning.ts} +0 -0
@@ -0,0 +1,85 @@
1
+ import {
2
+ lazyStream,
3
+ type Api,
4
+ type AssistantMessage,
5
+ type AssistantMessageEventStream,
6
+ type Model,
7
+ type ProviderStreams,
8
+ type StreamOptions,
9
+ } from "@earendil-works/pi-ai";
10
+ import type { RuntimeModelProtocol } from "../../config/ports";
11
+ import { overlayProviderStreams } from "../provider-streams";
12
+
13
+ export type OpenRouterProtocol = Extract<RuntimeModelProtocol, `openrouter-${string}`>;
14
+
15
+ /** Native output required by OpenRouter's stateless continuation. */
16
+ export interface ModelProtocolResponse {
17
+ readonly output: readonly Readonly<Record<string, unknown>>[];
18
+ }
19
+
20
+ declare module "@earendil-works/pi-ai" {
21
+ interface AssistantMessage {
22
+ protocolResponse?: ModelProtocolResponse;
23
+ }
24
+ }
25
+
26
+ function record(value: unknown): Record<string, unknown> | undefined {
27
+ return value !== null && typeof value === "object" && !Array.isArray(value)
28
+ ? value as Record<string, unknown>
29
+ : undefined;
30
+ }
31
+
32
+ function streamWithProtocolResponse<T extends StreamOptions>(
33
+ model: Model<Api>,
34
+ options: T | undefined,
35
+ start: (options: T) => AssistantMessageEventStream,
36
+ ): AssistantMessageEventStream {
37
+ return lazyStream(model, async () => {
38
+ let nativeResponse: ModelProtocolResponse | undefined;
39
+ const source = start({
40
+ ...options,
41
+ onProviderStreamEvent: async (data: unknown, activeModel: Model<Api>) => {
42
+ const event = record(data);
43
+ if (event?.type === "response.created") nativeResponse = undefined;
44
+ if (event?.type === "response.completed" || event?.type === "response.incomplete") {
45
+ const output = record(event.response)?.output;
46
+ if (Array.isArray(output)) {
47
+ const items = output.flatMap((value) => {
48
+ const item = record(value);
49
+ return item ? [item] : [];
50
+ });
51
+ nativeResponse = items.length > 0 ? { output: items } : undefined;
52
+ }
53
+ }
54
+ await options?.onProviderStreamEvent?.(data, activeModel);
55
+ },
56
+ } as T);
57
+ return (async function* () {
58
+ let terminal = false;
59
+ for await (const event of source) {
60
+ if (event.type === "done" || event.type === "error") {
61
+ terminal = true;
62
+ if (nativeResponse) {
63
+ const message = event.type === "done" ? event.message : event.error;
64
+ message.protocolResponse = nativeResponse;
65
+ }
66
+ }
67
+ yield event;
68
+ }
69
+ if (!terminal) throw new Error("Provider stream ended without a terminal event");
70
+ })();
71
+ });
72
+ }
73
+
74
+ export function withModelProtocolResponse(
75
+ api: ProviderStreams,
76
+ protocol: OpenRouterProtocol,
77
+ ): ProviderStreams {
78
+ if (protocol !== "openrouter-responses") return api;
79
+ return overlayProviderStreams(api, {
80
+ stream: (model, context, options) =>
81
+ streamWithProtocolResponse(model, options, (requestOptions) => api.stream(model, context, requestOptions)),
82
+ streamSimple: (model, context, options) =>
83
+ streamWithProtocolResponse(model, options, (requestOptions) => api.streamSimple(model, context, requestOptions)),
84
+ });
85
+ }
@@ -0,0 +1,45 @@
1
+ import type {
2
+ Api,
3
+ Model,
4
+ ProviderEnv,
5
+ ProviderStreams,
6
+ } from "@earendil-works/pi-ai";
7
+
8
+ /**
9
+ * ProviderStreams 是普通对象,不是 class。完整适配先 spread 底层全部方法,再覆盖需要装饰的入口。
10
+ * 只手写 stream / streamSimple 会丢掉 Pi 后续挂上的 fetchDeferred / cancelDeferred。
11
+ */
12
+ export function overlayProviderStreams(
13
+ base: ProviderStreams,
14
+ overlay: Partial<ProviderStreams>,
15
+ ): ProviderStreams {
16
+ return { ...base, ...overlay };
17
+ }
18
+
19
+ /**
20
+ * 在保留其余方法的前提下,把每个已知的 model 入口改写到 remap 后的模型。
21
+ * 未知的未来方法仍通过 spread 透传,避免再出现白名单丢方法。
22
+ */
23
+ export function remapProviderStreamModels(
24
+ streams: ProviderStreams,
25
+ remap: (model: Model<Api>, env: ProviderEnv | undefined) => Model<Api>,
26
+ ): ProviderStreams {
27
+ return overlayProviderStreams(streams, {
28
+ stream: (model, context, options) =>
29
+ streams.stream(remap(model, options?.env), context, options),
30
+ streamSimple: (model, context, options) =>
31
+ streams.streamSimple(remap(model, options?.env), context, options),
32
+ ...(streams.fetchDeferred
33
+ ? {
34
+ fetchDeferred: (model, handle, options) =>
35
+ streams.fetchDeferred!(remap(model, options?.env), handle, options),
36
+ }
37
+ : {}),
38
+ ...(streams.cancelDeferred
39
+ ? {
40
+ cancelDeferred: (model, handle, options) =>
41
+ streams.cancelDeferred!(remap(model, options?.env), handle, options),
42
+ }
43
+ : {}),
44
+ });
45
+ }