@treeseed/core 0.4.9 → 0.4.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 (41) hide show
  1. package/README.md +1 -2
  2. package/dist/agent.d.ts +0 -1
  3. package/dist/agent.js +0 -2
  4. package/dist/agents/spec-types.d.ts +10 -10
  5. package/dist/api/agent-routes.d.ts +2 -2
  6. package/dist/api/agent-routes.js +51 -125
  7. package/dist/api/app.js +56 -4
  8. package/dist/api/auth/d1-store.d.ts +1 -0
  9. package/dist/api/auth/d1-store.js +21 -1
  10. package/dist/api/config.js +4 -0
  11. package/dist/api/http.d.ts +4 -0
  12. package/dist/api/http.js +7 -0
  13. package/dist/api/index.d.ts +1 -1
  14. package/dist/api/index.js +2 -2
  15. package/dist/api/operations-routes.d.ts +1 -0
  16. package/dist/api/operations-routes.js +6 -1
  17. package/dist/api/railway.d.ts +4 -0
  18. package/dist/api/sdk-dispatch.d.ts +2 -11
  19. package/dist/api/sdk-dispatch.js +1 -133
  20. package/dist/api/sdk-routes.d.ts +1 -0
  21. package/dist/api/sdk-routes.js +5 -1
  22. package/dist/api/types.d.ts +32 -16
  23. package/dist/dev.js +24 -1
  24. package/dist/index.d.ts +0 -1
  25. package/dist/index.js +0 -2
  26. package/dist/scripts/test-smoke.js +0 -1
  27. package/dist/services/common.d.ts +37 -4
  28. package/dist/services/common.js +135 -17
  29. package/dist/services/index.d.ts +1 -1
  30. package/dist/services/index.js +3 -2
  31. package/dist/services/remote-runner.d.ts +23 -0
  32. package/dist/services/remote-runner.js +105 -0
  33. package/dist/services/workday-report.js +13 -17
  34. package/dist/services/workday-start.d.ts +5 -1
  35. package/dist/services/workday-start.js +7 -13
  36. package/dist/services/worker.js +38 -57
  37. package/package.json +7 -11
  38. package/dist/api/gateway.d.ts +0 -5
  39. package/dist/api/gateway.js +0 -35
  40. package/dist/services/manager.d.ts +0 -4
  41. package/dist/services/manager.js +0 -199
@@ -2,5 +2,6 @@ import type { Hono } from 'hono';
2
2
  import { executeHttpWorkflowOperation } from './operations.ts';
3
3
  export declare function registerOperationRoutes(app: Hono<any>, options: {
4
4
  scope: string;
5
+ prefix?: string;
5
6
  executeOperation?: typeof executeHttpWorkflowOperation;
6
7
  }): void;
@@ -3,7 +3,12 @@ import { executeHttpWorkflowOperation, isHttpWorkflowOperationAllowed } from "./
3
3
  import { jsonError, requireScope } from "./http.js";
4
4
  function registerOperationRoutes(app, options) {
5
5
  const executeOperation = options.executeOperation ?? executeHttpWorkflowOperation;
6
- app.post("/operations/:operation", async (c) => {
6
+ const prefix = options.prefix ?? "";
7
+ function withPrefix(path) {
8
+ if (!prefix) return path;
9
+ return `${prefix}${path}`.replace(/\/{2,}/g, "/");
10
+ }
11
+ app.post(withPrefix("/operations/:operation"), async (c) => {
7
12
  const unauthorized = requireScope(c, options.scope);
8
13
  if (unauthorized) return unauthorized;
9
14
  const requestedOperation = c.req.param("operation");
@@ -22,7 +22,11 @@ export declare function createRailwayTreeseedApiServer(options?: ApiServerOption
22
22
  baseUrl: string;
23
23
  issuer: string;
24
24
  repoRoot: string;
25
+ projectId: string;
25
26
  authSecret: string;
27
+ projectApiKey?: string;
28
+ projectApiLabel: string;
29
+ projectApiPermissions: string[];
26
30
  cloudflareAccountId?: string;
27
31
  cloudflareApiToken?: string;
28
32
  d1DatabaseId?: string;
@@ -1,14 +1,5 @@
1
1
  import type { AgentSdk, RemoteSdkOperationRequest } from '@treeseed/sdk';
2
+ import { executeSdkOperation, findSdkOperation, listSdkOperationNames } from '@treeseed/sdk';
2
3
  import type { ApiConfig } from './types.ts';
3
- type JsonRecord = Record<string, unknown>;
4
- type SdkOperationHandler = (sdk: AgentSdk, input: JsonRecord) => Promise<unknown> | unknown;
5
- interface SdkOperationSpec {
6
- name: string;
7
- aliases?: string[];
8
- handler: SdkOperationHandler;
9
- }
10
- export declare function listSdkOperationNames(): string[];
11
- export declare function findSdkOperation(name: string): SdkOperationSpec;
4
+ export { executeSdkOperation, findSdkOperation, listSdkOperationNames, };
12
5
  export declare function resolveSdkInstance(sharedSdk: AgentSdk | undefined, config: ApiConfig, request: RemoteSdkOperationRequest): AgentSdk;
13
- export declare function executeSdkOperation(sdk: AgentSdk, operationName: string, input: JsonRecord): Promise<unknown>;
14
- export {};
@@ -1,142 +1,10 @@
1
- import { AgentSdk as AgentSdkClass } from "@treeseed/sdk";
2
- function passthrough(methodName) {
3
- return (sdk, input) => sdk[methodName](input);
4
- }
5
- const SDK_OPERATION_SPECS = [
6
- { name: "get", handler: passthrough("get") },
7
- { name: "read", handler: passthrough("read") },
8
- { name: "search", handler: passthrough("search") },
9
- { name: "follow", handler: passthrough("follow") },
10
- { name: "pick", handler: passthrough("pick") },
11
- { name: "create", handler: passthrough("create") },
12
- { name: "update", handler: passthrough("update") },
13
- { name: "claimMessage", aliases: ["claim-message"], handler: passthrough("claimMessage") },
14
- { name: "ackMessage", aliases: ["ack-message"], handler: passthrough("ackMessage") },
15
- { name: "createMessage", aliases: ["create-message"], handler: passthrough("createMessage") },
16
- { name: "recordRun", aliases: ["record-run"], handler: passthrough("recordRun") },
17
- { name: "getCursor", aliases: ["get-cursor"], handler: passthrough("getCursor") },
18
- { name: "upsertCursor", aliases: ["upsert-cursor"], handler: passthrough("upsertCursor") },
19
- { name: "releaseLease", aliases: ["release-lease"], handler: passthrough("releaseLease") },
20
- {
21
- name: "releaseAllLeases",
22
- aliases: ["release-all-leases"],
23
- handler: (sdk) => sdk.releaseAllLeases()
24
- },
25
- { name: "startWorkDay", aliases: ["start-work-day"], handler: passthrough("startWorkDay") },
26
- { name: "closeWorkDay", aliases: ["close-work-day"], handler: passthrough("closeWorkDay") },
27
- { name: "createTask", aliases: ["create-task"], handler: passthrough("createTask") },
28
- { name: "claimTask", aliases: ["claim-task"], handler: passthrough("claimTask") },
29
- {
30
- name: "recordTaskProgress",
31
- aliases: ["record-task-progress"],
32
- handler: passthrough("recordTaskProgress")
33
- },
34
- { name: "completeTask", aliases: ["complete-task"], handler: passthrough("completeTask") },
35
- { name: "failTask", aliases: ["fail-task"], handler: passthrough("failTask") },
36
- { name: "appendTaskEvent", aliases: ["append-task-event"], handler: passthrough("appendTaskEvent") },
37
- { name: "searchTasks", aliases: ["search-tasks"], handler: passthrough("searchTasks") },
38
- {
39
- name: "getManagerContext",
40
- aliases: ["get-manager-context"],
41
- handler: (sdk, input) => sdk.getManagerContext(String(input.taskId ?? input.id ?? ""))
42
- },
43
- { name: "createReport", aliases: ["create-report"], handler: passthrough("createReport") },
44
- {
45
- name: "listAgentSpecs",
46
- aliases: ["list-agent-specs"],
47
- handler: (sdk, input) => sdk.listAgentSpecs(input)
48
- },
49
- { name: "refreshGraph", aliases: ["refresh-graph"], handler: passthrough("refreshGraph") },
50
- {
51
- name: "searchFiles",
52
- aliases: ["search-files"],
53
- handler: (sdk, input) => sdk.searchFiles(String(input.query ?? ""), input.options)
54
- },
55
- {
56
- name: "searchSections",
57
- aliases: ["search-sections"],
58
- handler: (sdk, input) => sdk.searchSections(String(input.query ?? ""), input.options)
59
- },
60
- {
61
- name: "searchEntities",
62
- aliases: ["search-entities"],
63
- handler: (sdk, input) => sdk.searchEntities(String(input.query ?? ""), input.options)
64
- },
65
- {
66
- name: "getGraphNode",
67
- aliases: ["get-graph-node"],
68
- handler: (sdk, input) => sdk.getGraphNode(String(input.id ?? ""))
69
- },
70
- {
71
- name: "getNeighbors",
72
- aliases: ["get-neighbors"],
73
- handler: (sdk, input) => sdk.getNeighbors(String(input.id ?? ""), input.options)
74
- },
75
- {
76
- name: "followReferences",
77
- aliases: ["follow-references"],
78
- handler: (sdk, input) => sdk.followReferences(String(input.id ?? ""), input.options)
79
- },
80
- {
81
- name: "getBacklinks",
82
- aliases: ["get-backlinks"],
83
- handler: (sdk, input) => sdk.getBacklinks(String(input.id ?? ""), input.options)
84
- },
85
- {
86
- name: "getRelated",
87
- aliases: ["get-related"],
88
- handler: (sdk, input) => sdk.getRelated(String(input.id ?? ""), input.options)
89
- },
90
- {
91
- name: "getSubgraph",
92
- aliases: ["get-subgraph"],
93
- handler: (sdk, input) => sdk.getSubgraph(Array.isArray(input.seedIds) ? input.seedIds.map(String) : [], input.options)
94
- },
95
- { name: "resolveSeeds", aliases: ["resolve-seeds"], handler: passthrough("resolveSeeds") },
96
- { name: "queryGraph", aliases: ["query-graph"], handler: passthrough("queryGraph") },
97
- { name: "buildContextPack", aliases: ["build-context-pack"], handler: passthrough("buildContextPack") },
98
- {
99
- name: "parseGraphDsl",
100
- aliases: ["parse-graph-dsl"],
101
- handler: (sdk, input) => sdk.parseGraphDsl(String(input.source ?? input.query ?? ""))
102
- },
103
- {
104
- name: "resolveReference",
105
- aliases: ["resolve-reference"],
106
- handler: (sdk, input) => sdk.resolveReference(String(input.reference ?? ""), input.options)
107
- },
108
- {
109
- name: "explainReferenceChain",
110
- aliases: ["explain-reference-chain"],
111
- handler: (sdk, input) => sdk.explainReferenceChain(String(input.fromId ?? ""), String(input.toId ?? ""))
112
- }
113
- ];
114
- const SDK_OPERATION_INDEX = /* @__PURE__ */ new Map();
115
- for (const spec of SDK_OPERATION_SPECS) {
116
- SDK_OPERATION_INDEX.set(spec.name, spec);
117
- for (const alias of spec.aliases ?? []) {
118
- SDK_OPERATION_INDEX.set(alias, spec);
119
- }
120
- }
121
- function listSdkOperationNames() {
122
- return [...new Set(SDK_OPERATION_SPECS.map((entry) => entry.name))];
123
- }
124
- function findSdkOperation(name) {
125
- return SDK_OPERATION_INDEX.get(name) ?? null;
126
- }
1
+ import { AgentSdk as AgentSdkClass, executeSdkOperation, findSdkOperation, listSdkOperationNames } from "@treeseed/sdk";
127
2
  function resolveSdkInstance(sharedSdk, config, request) {
128
3
  if (!request.repoRoot || request.repoRoot === config.repoRoot) {
129
4
  return sharedSdk ?? new AgentSdkClass({ repoRoot: config.repoRoot });
130
5
  }
131
6
  return new AgentSdkClass({ repoRoot: request.repoRoot });
132
7
  }
133
- async function executeSdkOperation(sdk, operationName, input) {
134
- const spec = findSdkOperation(operationName);
135
- if (!spec) {
136
- throw new Error(`Unknown SDK operation "${operationName}".`);
137
- }
138
- return await spec.handler(sdk, input);
139
- }
140
8
  export {
141
9
  executeSdkOperation,
142
10
  findSdkOperation,
@@ -5,6 +5,7 @@ interface RegisterSdkRoutesOptions {
5
5
  config: ApiConfig;
6
6
  sharedSdk?: AgentSdk;
7
7
  scope: string;
8
+ prefix?: string;
8
9
  }
9
10
  export declare function registerSdkRoutes(app: Hono<any>, options: RegisterSdkRoutesOptions): void;
10
11
  export {};
@@ -1,7 +1,11 @@
1
1
  import { executeSdkOperation, resolveSdkInstance } from "./sdk-dispatch.js";
2
2
  import { jsonError, requireScope } from "./http.js";
3
+ function withPrefix(prefix, path) {
4
+ if (!prefix) return path;
5
+ return `${prefix}${path}`.replace(/\/{2,}/g, "/");
6
+ }
3
7
  function registerSdkRoutes(app, options) {
4
- app.post("/sdk/:operation", async (c) => {
8
+ app.post(withPrefix(options.prefix ?? "", "/sdk/:operation"), async (c) => {
5
9
  const unauthorized = requireScope(c, options.scope);
6
10
  if (unauthorized) return unauthorized;
7
11
  const operation = c.req.param("operation");
@@ -1,4 +1,5 @@
1
- import type { AgentSdk, SdkQueueMessageEnvelope } from '@treeseed/sdk';
1
+ import type { Hono } from 'hono';
2
+ import type { AgentSdk } from '@treeseed/sdk';
2
3
  import type { ApiPrincipal, ApiScope, DeviceCodeApproveRequest as SdkDeviceCodeApproveRequest, DeviceCodePollRequest, DeviceCodePollResponse, DeviceCodeStartRequest, DeviceCodeStartResponse, RemoteWorkflowOperationRequest as WorkflowHttpOperationRequest, RemoteWorkflowOperationResponse as ApiWorkflowOperationResponse, RemoteSdkOperationRequest as SdkHttpOperationRequest, TokenRefreshRequest, TokenRefreshResponse } from '@treeseed/sdk/remote';
3
4
  export type { ApiPrincipal, ApiScope, DeviceCodePollRequest, DeviceCodePollResponse, DeviceCodeStartRequest, DeviceCodeStartResponse, WorkflowHttpOperationRequest, ApiWorkflowOperationResponse, SdkHttpOperationRequest, TokenRefreshRequest, TokenRefreshResponse, };
4
5
  export type DeviceCodeApproveRequest = SdkDeviceCodeApproveRequest;
@@ -87,7 +88,11 @@ export interface ApiConfig {
87
88
  baseUrl: string;
88
89
  issuer: string;
89
90
  repoRoot: string;
91
+ projectId: string;
90
92
  authSecret: string;
93
+ projectApiKey?: string;
94
+ projectApiLabel: string;
95
+ projectApiPermissions: string[];
91
96
  cloudflareAccountId?: string;
92
97
  cloudflareApiToken?: string;
93
98
  d1DatabaseId?: string;
@@ -111,11 +116,11 @@ export interface AppVariables {
111
116
  principal: ApiPrincipal | null;
112
117
  actingUser: ApiPrincipal | null;
113
118
  credential: ApiCredential | null;
114
- actorType: 'anonymous' | 'user' | 'service';
119
+ actorType: 'anonymous' | 'user' | 'service' | 'project';
115
120
  permissionGrants: string[];
116
121
  }
117
122
  export interface ApiCredential {
118
- type: 'access_token' | 'personal_access_token' | 'service_secret' | 'service_token';
123
+ type: 'access_token' | 'personal_access_token' | 'service_secret' | 'service_token' | 'project_api_key' | 'team_api_key';
119
124
  id: string;
120
125
  label?: string;
121
126
  }
@@ -158,6 +163,28 @@ export interface ResolvedApiRuntimeProviders {
158
163
  };
159
164
  selections: ApiRuntimeProviderSelections;
160
165
  }
166
+ export interface ApiResolvedSettings {
167
+ config: ApiConfig;
168
+ surfaces: {
169
+ auth: boolean;
170
+ templates: boolean;
171
+ sdk: boolean;
172
+ agent: boolean;
173
+ operations: boolean;
174
+ };
175
+ scopes: {
176
+ authMe: ApiScope;
177
+ sdk: ApiScope;
178
+ agent: ApiScope;
179
+ operations: ApiScope;
180
+ };
181
+ }
182
+ export interface ApiAppRuntime {
183
+ resolved: ApiResolvedSettings;
184
+ runtimeProviders: ResolvedApiRuntimeProviders;
185
+ sharedSdk: AgentSdk;
186
+ internalPrefix: string;
187
+ }
161
188
  export interface ApiServerOptions {
162
189
  config?: Partial<ApiConfig>;
163
190
  runtimeProviders?: ApiRuntimeProviders;
@@ -176,18 +203,7 @@ export interface ApiServerOptions {
176
203
  agent: ApiScope;
177
204
  operations: ApiScope;
178
205
  }>;
206
+ internalPrefix?: string;
207
+ extendApp?: (app: Hono<any>, runtime: ApiAppRuntime) => void;
179
208
  log?: (message: string, details?: Record<string, unknown>) => void;
180
209
  }
181
- export interface GatewayQueueProducer {
182
- enqueue(request: {
183
- queueName?: string;
184
- message: SdkQueueMessageEnvelope;
185
- delaySeconds?: number;
186
- }): Promise<void>;
187
- }
188
- export interface GatewayServerOptions {
189
- sdk: AgentSdk;
190
- bearerToken: string;
191
- queueProducer?: GatewayQueueProducer;
192
- projectId?: string;
193
- }
package/dist/dev.js CHANGED
@@ -38,6 +38,28 @@ function resolveNodeEntrypoint(packageDir, sourceRelativePath, distRelativePath)
38
38
  args: [resolve(packageDir, distRelativePath)]
39
39
  };
40
40
  }
41
+ function resolveTenantApiEntrypoint(tenantRoot, runTsPath) {
42
+ const javascriptCandidates = [
43
+ resolve(tenantRoot, "src", "api", "server.js"),
44
+ resolve(tenantRoot, "src", "api", "server.mjs")
45
+ ];
46
+ for (const candidate of javascriptCandidates) {
47
+ if (existsSync(candidate)) {
48
+ return {
49
+ command: process.execPath,
50
+ args: [candidate]
51
+ };
52
+ }
53
+ }
54
+ const typescriptCandidate = resolve(tenantRoot, "src", "api", "server.ts");
55
+ if (existsSync(typescriptCandidate) && existsSync(runTsPath)) {
56
+ return {
57
+ command: process.execPath,
58
+ args: [runTsPath, typescriptCandidate]
59
+ };
60
+ }
61
+ return null;
62
+ }
41
63
  function withWatchArgs(args, watchPaths) {
42
64
  return watchPaths.flatMap((watchPath) => ["--watch-path", watchPath]).concat(args);
43
65
  }
@@ -55,12 +77,13 @@ function createTreeseedIntegratedDevPlan(options = {}) {
55
77
  const mergedEnv = { ...process.env, ...options.env ?? {} };
56
78
  const apiBaseUrl = mergedEnv.TREESEED_API_BASE_URL?.trim() || `http://${apiHost}:${apiPort}`;
57
79
  const sdkPackageRoot = resolvePackageRoot("@treeseed/sdk", tenantRoot);
80
+ const coreRunTsPath = resolve(packageRoot, "scripts", "run-ts.mjs");
58
81
  const webEntrypoint = resolveNodeEntrypoint(
59
82
  sdkPackageRoot,
60
83
  "scripts/tenant-astro-command.ts",
61
84
  "dist/scripts/tenant-astro-command.js"
62
85
  );
63
- const apiEntrypoint = resolveNodeEntrypoint(
86
+ const apiEntrypoint = resolveTenantApiEntrypoint(tenantRoot, coreRunTsPath) ?? resolveNodeEntrypoint(
64
87
  packageRoot,
65
88
  "src/api/server.ts",
66
89
  "dist/api/server.js"
package/dist/index.d.ts CHANGED
@@ -2,7 +2,6 @@ export { buildTreeseedSiteLayers, resolveTreeseedPageEntrypoint, resolveTreeseed
2
2
  export { buildTreeseedPlatformLayers, resolveTreeseedPlatformResource, TREESEED_PLATFORM_RESOURCE_KINDS, } from './platform-resources';
3
3
  export { parseSiteConfig } from './utils/site-config-schema.js';
4
4
  export { createTreeseedApiApp } from './api/app';
5
- export { createTreeseedGatewayApp } from './api/gateway';
6
5
  export { createRailwayTreeseedApiServer } from './api/railway';
7
6
  export { resolveApiConfig } from './api/config';
8
7
  export { createTreeseedIntegratedDevPlan, runTreeseedIntegratedDev, type TreeseedIntegratedDevCommand, type TreeseedIntegratedDevOptions, type TreeseedIntegratedDevPlan, type TreeseedIntegratedDevSurface, } from './dev';
package/dist/index.js CHANGED
@@ -12,7 +12,6 @@ import {
12
12
  } from "./platform-resources.js";
13
13
  import { parseSiteConfig } from "./utils/site-config-schema.js";
14
14
  import { createTreeseedApiApp } from "./api/app.js";
15
- import { createTreeseedGatewayApp } from "./api/gateway.js";
16
15
  import { createRailwayTreeseedApiServer } from "./api/railway.js";
17
16
  import { resolveApiConfig } from "./api/config.js";
18
17
  import {
@@ -26,7 +25,6 @@ export {
26
25
  buildTreeseedSiteLayers,
27
26
  createRailwayTreeseedApiServer,
28
27
  createTreeseedApiApp,
29
- createTreeseedGatewayApp,
30
28
  createTreeseedIntegratedDevPlan,
31
29
  parseSiteConfig,
32
30
  resolveApiConfig,
@@ -174,7 +174,6 @@ try {
174
174
  'await import("@treeseed/core/runtime-types");',
175
175
  'await import("@treeseed/core/contracts/messages");',
176
176
  'await import("@treeseed/core/contracts/run");',
177
- 'await import("@treeseed/core/services/manager");',
178
177
  'await import("@treeseed/core/services/worker");',
179
178
  'await import("@treeseed/core/services/workday-start");',
180
179
  'await import("@treeseed/core/services/workday-report");',
@@ -1,9 +1,43 @@
1
- import { AgentSdk } from '@treeseed/sdk/sdk';
2
- import { TreeseedGatewayClient, CloudflareQueuePullClient } from '@treeseed/sdk/remote';
1
+ import { AgentSdk } from '@treeseed/sdk';
2
+ import { CloudflareQueuePullClient } from '@treeseed/sdk/remote';
3
+ import type { SdkQueueMessageEnvelope } from '@treeseed/sdk';
3
4
  export declare function resolveServiceRepoRoot(): string;
4
5
  export declare function createServiceSdk(): AgentSdk;
5
- export declare function createGatewayClient(): TreeseedGatewayClient;
6
6
  export declare function createQueueClient(): CloudflareQueuePullClient;
7
+ export declare function createQueuePushClient(): {
8
+ enqueue(request: {
9
+ message: SdkQueueMessageEnvelope;
10
+ delaySeconds?: number;
11
+ }): Promise<void>;
12
+ };
13
+ export declare function queueEnvelopeForTask(task: Record<string, unknown>): SdkQueueMessageEnvelope;
14
+ export declare function enqueueTaskFromSdk(sdk: AgentSdk, request: {
15
+ taskId: string;
16
+ queueName?: string;
17
+ deliveryDelaySeconds?: number;
18
+ actor?: string;
19
+ }): Promise<{
20
+ ok: boolean;
21
+ taskId: string;
22
+ queued: boolean;
23
+ }>;
24
+ export declare function buildTaskContext(sdk: AgentSdk, taskId: string): Promise<{
25
+ agent: Record<string, unknown> | import("@treeseed/sdk").SdkContentEntry;
26
+ task: import("@treeseed/sdk").SdkTaskEntity | null;
27
+ workDay: import("@treeseed/sdk").SdkWorkDayEntity | null;
28
+ graph: Record<string, unknown> | null;
29
+ }>;
30
+ export declare function seedRootTasks(sdk: AgentSdk, workDayId: string): Promise<any[]>;
31
+ export declare function startAndSeedWorkday(sdk: AgentSdk, request: {
32
+ id?: string;
33
+ projectId: string;
34
+ capacityBudget: number;
35
+ actor?: string;
36
+ }): Promise<{
37
+ ok: boolean;
38
+ workDay: import("@treeseed/sdk").SdkWorkDayEntity;
39
+ seededTasks: any[];
40
+ }>;
7
41
  export declare function resolveManagerConfig(): {
8
42
  host: string;
9
43
  port: number;
@@ -16,5 +50,4 @@ export declare function resolveWorkerConfig(): {
16
50
  visibilityTimeoutMs: number;
17
51
  pollIntervalMs: number;
18
52
  leaseSeconds: number;
19
- managerBaseUrl: string;
20
53
  };
@@ -1,5 +1,6 @@
1
- import { AgentSdk } from "@treeseed/sdk/sdk";
2
- import { TreeseedGatewayClient, CloudflareQueuePullClient } from "@treeseed/sdk/remote";
1
+ import crypto from "node:crypto";
2
+ import { AgentSdk } from "@treeseed/sdk";
3
+ import { CloudflareQueuePullClient } from "@treeseed/sdk/remote";
3
4
  function integerFromEnv(name, fallback) {
4
5
  const value = process.env[name];
5
6
  if (!value) return fallback;
@@ -16,27 +17,140 @@ function createServiceSdk() {
16
17
  persistTo: process.env.TREESEED_AGENT_D1_PERSIST_TO ?? void 0
17
18
  });
18
19
  }
19
- function createGatewayClient() {
20
- const baseUrl = process.env.TREESEED_GATEWAY_BASE_URL?.trim();
21
- const bearerToken = process.env.TREESEED_GATEWAY_BEARER_TOKEN?.trim();
22
- if (!baseUrl || !bearerToken) {
23
- return null;
24
- }
25
- return new TreeseedGatewayClient({ baseUrl, bearerToken });
26
- }
27
- function createQueueClient() {
20
+ function createQueueClientConfig(token) {
28
21
  const accountId = process.env.CLOUDFLARE_ACCOUNT_ID?.trim();
29
22
  const queueId = process.env.TREESEED_QUEUE_ID?.trim();
30
- const token = process.env.TREESEED_QUEUE_PULL_TOKEN?.trim();
31
23
  if (!accountId || !queueId || !token) {
32
24
  return null;
33
25
  }
34
- return new CloudflareQueuePullClient({
26
+ return {
35
27
  accountId,
36
28
  queueId,
37
29
  token,
38
30
  apiBaseUrl: process.env.TREESEED_QUEUE_API_BASE_URL?.trim() || void 0
31
+ };
32
+ }
33
+ function createQueueClient() {
34
+ const config = createQueueClientConfig(process.env.TREESEED_QUEUE_PULL_TOKEN?.trim() || "");
35
+ if (!config) {
36
+ return null;
37
+ }
38
+ return new CloudflareQueuePullClient(config);
39
+ }
40
+ function createQueuePushClient() {
41
+ const config = createQueueClientConfig(
42
+ process.env.TREESEED_QUEUE_PUSH_TOKEN?.trim() || process.env.CLOUDFLARE_API_TOKEN?.trim() || ""
43
+ );
44
+ if (!config) {
45
+ return null;
46
+ }
47
+ const apiBaseUrl = config.apiBaseUrl ?? "https://api.cloudflare.com/client/v4/accounts";
48
+ const baseUrl = `${apiBaseUrl.replace(/\/$/u, "")}/${config.accountId}/queues/${config.queueId}`;
49
+ return {
50
+ async enqueue(request) {
51
+ const response = await fetch(`${baseUrl}/messages`, {
52
+ method: "POST",
53
+ headers: {
54
+ accept: "application/json",
55
+ authorization: `Bearer ${config.token}`,
56
+ "content-type": "application/json"
57
+ },
58
+ body: JSON.stringify({
59
+ body: request.message,
60
+ content_type: "json",
61
+ delay_seconds: request.delaySeconds ?? 0
62
+ })
63
+ });
64
+ const payload = await response.json().catch(() => ({}));
65
+ if (!response.ok || payload.success === false) {
66
+ throw new Error(payload.errors?.[0]?.message ?? `Queue request failed with ${response.status}.`);
67
+ }
68
+ }
69
+ };
70
+ }
71
+ function queueEnvelopeForTask(task) {
72
+ return {
73
+ messageId: crypto.randomUUID(),
74
+ taskId: String(task.id ?? ""),
75
+ workDayId: String(task.workDayId ?? task.work_day_id ?? ""),
76
+ agentId: String(task.agentId ?? task.agent_id ?? ""),
77
+ taskType: String(task.type ?? ""),
78
+ idempotencyKey: String(task.idempotencyKey ?? task.idempotency_key ?? ""),
79
+ attempt: Number(task.attemptCount ?? task.attempt_count ?? 0) + 1,
80
+ payloadRef: `d1:tasks/${String(task.id ?? "")}`,
81
+ graphVersion: task.graphVersion !== void 0 && task.graphVersion !== null ? String(task.graphVersion) : task.graph_version !== void 0 && task.graph_version !== null ? String(task.graph_version) : null,
82
+ budgetHint: 1
83
+ };
84
+ }
85
+ async function enqueueTaskFromSdk(sdk, request) {
86
+ const queue = createQueuePushClient();
87
+ if (!queue) {
88
+ throw new Error("Queue push client not configured.");
89
+ }
90
+ const task = await sdk.get({ model: "task", id: request.taskId });
91
+ if (!task.payload) {
92
+ throw new Error("Unknown task.");
93
+ }
94
+ await queue.enqueue({
95
+ message: queueEnvelopeForTask(task.payload),
96
+ delaySeconds: request.deliveryDelaySeconds ?? 0
97
+ });
98
+ await sdk.recordTaskProgress({
99
+ id: request.taskId,
100
+ state: "queued",
101
+ appendEvent: { kind: "queued", data: { queueName: request.queueName ?? null } },
102
+ actor: request.actor
39
103
  });
104
+ return { ok: true, taskId: request.taskId, queued: true };
105
+ }
106
+ async function buildTaskContext(sdk, taskId) {
107
+ const context = await sdk.getManagerContext(taskId);
108
+ const task = context.payload.task;
109
+ const agent = task ? (await sdk.get({ model: "agent", slug: String(task.agentId) })).payload : null;
110
+ return {
111
+ ...context.payload,
112
+ agent
113
+ };
114
+ }
115
+ async function seedRootTasks(sdk, workDayId) {
116
+ const specs = await sdk.listAgentSpecs({ enabled: true });
117
+ const created = [];
118
+ for (const spec of specs) {
119
+ const hasStartTrigger = spec.triggers.some((trigger) => trigger.type === "startup" || trigger.type === "schedule");
120
+ if (!hasStartTrigger) continue;
121
+ created.push(await sdk.createTask({
122
+ workDayId,
123
+ agentId: spec.slug,
124
+ type: "agent_root",
125
+ priority: 100,
126
+ idempotencyKey: `${workDayId}:${spec.slug}:root`,
127
+ payload: {
128
+ agentSlug: spec.slug,
129
+ handler: spec.handler,
130
+ triggerKinds: spec.triggers.map((entry) => entry.type)
131
+ },
132
+ graphVersion: null,
133
+ actor: "manager"
134
+ }));
135
+ }
136
+ return created;
137
+ }
138
+ async function startAndSeedWorkday(sdk, request) {
139
+ const graphRefresh = await sdk.refreshGraph();
140
+ const workDay = await sdk.startWorkDay({
141
+ id: request.id,
142
+ projectId: request.projectId,
143
+ capacityBudget: request.capacityBudget,
144
+ graphVersion: graphRefresh.snapshotRoot,
145
+ summary: { graphRefresh },
146
+ actor: request.actor ?? "manager"
147
+ });
148
+ const tasks = workDay.payload ? await seedRootTasks(sdk, String(workDay.payload.id)) : [];
149
+ return {
150
+ ok: true,
151
+ workDay: workDay.payload,
152
+ seededTasks: tasks.map((entry) => entry.payload).filter(Boolean)
153
+ };
40
154
  }
41
155
  function resolveManagerConfig() {
42
156
  return {
@@ -52,15 +166,19 @@ function resolveWorkerConfig() {
52
166
  batchSize: integerFromEnv("TREESEED_QUEUE_BATCH_SIZE", 1),
53
167
  visibilityTimeoutMs: integerFromEnv("TREESEED_QUEUE_VISIBILITY_TIMEOUT_MS", 12e4),
54
168
  pollIntervalMs: integerFromEnv("TREESEED_WORKER_POLL_INTERVAL_MS", 5e3),
55
- leaseSeconds: integerFromEnv("TREESEED_TASK_LEASE_SECONDS", 120),
56
- managerBaseUrl: process.env.TREESEED_MANAGER_BASE_URL?.trim() || `http://${process.env.TREESEED_MANAGER_HOST?.trim() || "manager.railway.internal"}:${integerFromEnv("TREESEED_MANAGER_PORT", 3100)}`
169
+ leaseSeconds: integerFromEnv("TREESEED_TASK_LEASE_SECONDS", 120)
57
170
  };
58
171
  }
59
172
  export {
60
- createGatewayClient,
173
+ buildTaskContext,
61
174
  createQueueClient,
175
+ createQueuePushClient,
62
176
  createServiceSdk,
177
+ enqueueTaskFromSdk,
178
+ queueEnvelopeForTask,
63
179
  resolveManagerConfig,
64
180
  resolveServiceRepoRoot,
65
- resolveWorkerConfig
181
+ resolveWorkerConfig,
182
+ seedRootTasks,
183
+ startAndSeedWorkday
66
184
  };
@@ -1,4 +1,4 @@
1
- export { createManagerApp } from './manager.ts';
2
1
  export { runWorkerCycle, startWorkerLoop } from './worker.ts';
2
+ export { runRemoteRunnerCycle, startRemoteRunnerLoop } from './remote-runner.ts';
3
3
  export { runWorkdayStart } from './workday-start.ts';
4
4
  export { runWorkdayReport } from './workday-report.ts';
@@ -1,11 +1,12 @@
1
- import { createManagerApp } from "./manager.js";
2
1
  import { runWorkerCycle, startWorkerLoop } from "./worker.js";
2
+ import { runRemoteRunnerCycle, startRemoteRunnerLoop } from "./remote-runner.js";
3
3
  import { runWorkdayStart } from "./workday-start.js";
4
4
  import { runWorkdayReport } from "./workday-report.js";
5
5
  export {
6
- createManagerApp,
6
+ runRemoteRunnerCycle,
7
7
  runWorkdayReport,
8
8
  runWorkdayStart,
9
9
  runWorkerCycle,
10
+ startRemoteRunnerLoop,
10
11
  startWorkerLoop
11
12
  };
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env node
2
+ import { AgentSdk } from '@treeseed/sdk';
3
+ export declare function resolveRemoteRunnerConfig(): {
4
+ marketBaseUrl: string;
5
+ projectId: string;
6
+ runnerToken: string;
7
+ runnerId: string;
8
+ batchSize: number;
9
+ pollIntervalMs: number;
10
+ };
11
+ export declare function runRemoteRunnerCycle(options?: {
12
+ sdk?: AgentSdk;
13
+ config?: ReturnType<typeof resolveRemoteRunnerConfig>;
14
+ fetchImpl?: typeof fetch;
15
+ }): Promise<{
16
+ ok: boolean;
17
+ processed: number;
18
+ }>;
19
+ export declare function startRemoteRunnerLoop(options?: {
20
+ sdk?: AgentSdk;
21
+ config?: ReturnType<typeof resolveRemoteRunnerConfig>;
22
+ fetchImpl?: typeof fetch;
23
+ }): Promise<void>;