@parcha/agentrun-client 0.1.0-beta.49 → 0.1.0-beta.50

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 CHANGED
@@ -108,6 +108,18 @@ checkout; these new exports are not a guarantee about the current npm release.
108
108
  | Claude Agent SDK `query` and custom tools | [Preserve the native query, add checked learning](../../docs/tutorials/claude-sdk-learning.md) |
109
109
  | Codex SDK client, project and tools | [Preserve native threads, add checked learning](../../docs/tutorials/codex-sdk-learning.md) |
110
110
 
111
+ For OpenClaw, Capy or another platform that executes its own agents, use `BackendLearningClient` from
112
+ `@parcha/agentrun-client/learning` with `{runtime: "external", platform, input, instructions}`. The required platform
113
+ label is 1–80 Unicode characters without surrounding whitespace, controls or unpaired surrogates. These sources
114
+ report runs and receive owner corrections; they cannot supply a workflow bridge. See [external platform reporting](../../docs/tutorials/platform-sdk-runs.md).
115
+ `BackendLearningSource` accepts either the existing, extensible `BackendLearningStart` interface or an `ExternalLearningStart`.
116
+
117
+ The generated `StartSdkRunBody` type is now a union: external starts require `platform` and exclude workflow fields.
118
+ If your code extends this generated type with an interface, use a type intersection such as
119
+ `type JobStart = StartSdkRunBody & { jobId: string }` instead. To refer to SDK workflow fields, first select
120
+ `Extract<StartSdkRunBody, { runtime: "claude-agent-sdk" | "codex-sdk" | "eve" }>` or narrow a value by its `runtime`.
121
+ The higher-level `BackendLearningStart` remains an interface that applications can extend.
122
+
111
123
  Your backend still starts and cancels tasks. The wrapper records original source policy/input, assistant text,
112
124
  tool names/status and usage. With an explicit workflow bridge, bounded arguments/results for its registered tools also supply compilation evidence; other raw tool payloads and SDK credentials are not sent. AgentRun supplies the
113
125
  owner's corrections (`corrections.md`, whole) to the next fresh task, and compiles a workflow once the agent has
@@ -1,5 +1,15 @@
1
1
  import { AgentRun, type ClientOptions } from './client.js';
2
- export type BackendSdkRuntime = 'claude-agent-sdk' | 'codex-sdk' | 'eve';
2
+ /** Where a backend run comes from. External platforms report execution through these calls; their display label
3
+ * never selects a managed runtime or grants workflow capabilities. */
4
+ export declare const BACKEND_SDK_RUNTIMES: readonly ["claude-agent-sdk", "codex-sdk", "eve", "external"];
5
+ export type BackendSdkRuntime = (typeof BACKEND_SDK_RUNTIMES)[number];
6
+ export type BackendSdkBinding = {
7
+ runtime: Exclude<BackendSdkRuntime, 'external'>;
8
+ platform?: never;
9
+ } | {
10
+ runtime: 'external';
11
+ platform: string;
12
+ };
3
13
  export interface BackendWorkflowToolManifest {
4
14
  name: string;
5
15
  description: string;
@@ -80,7 +90,7 @@ type SdkReceipt = Awaited<ReturnType<AgentRun['sdkRuns']['start']>>;
80
90
  export interface BackendLearningCheckpoint {
81
91
  baseUrl: string;
82
92
  agentId: string;
83
- source: BackendLearningStart;
93
+ source: BackendLearningSource;
84
94
  receipt: SdkReceipt;
85
95
  /** A completed shadow result is replayed verbatim after transport ambiguity; it never reruns a host callback. */
86
96
  shadow?: {
@@ -108,12 +118,19 @@ export interface BackendLearningSubmitOptions {
108
118
  /** Persist the private updated receipt before the first shadow-result upload. */
109
119
  onCheckpoint?(checkpoint: BackendLearningCheckpoint): void | Promise<void>;
110
120
  }
121
+ /** The existing SDK start remains an interface so applications can extend it with their own task fields. */
111
122
  export interface BackendLearningStart {
112
- runtime: BackendSdkRuntime;
123
+ runtime: Exclude<BackendSdkRuntime, 'external'>;
124
+ platform?: never;
113
125
  input: string;
114
126
  instructions: string;
115
127
  model?: string;
116
128
  }
129
+ export interface ExternalLearningStart extends Omit<BackendLearningStart, 'runtime' | 'platform'> {
130
+ runtime: 'external';
131
+ platform: string;
132
+ }
133
+ export type BackendLearningSource = BackendLearningStart | ExternalLearningStart;
117
134
  export type SdkLearningEvent = {
118
135
  type: 'text';
119
136
  text: string;
@@ -202,7 +219,7 @@ export declare class BackendLearningError extends Error {
202
219
  export declare class BackendLearningClient {
203
220
  #private;
204
221
  constructor(config: BackendLearningConfig);
205
- begin(source: BackendLearningStart, signal?: AbortSignal, options?: BackendLearningBeginOptions): Promise<BackendLearningSession>;
222
+ begin(source: BackendLearningSource, signal?: AbortSignal, options?: BackendLearningBeginOptions): Promise<BackendLearningSession>;
206
223
  /** Restore a persisted receipt without registering or admitting a second native turn. */
207
224
  resume(checkpoint: BackendLearningCheckpoint): BackendLearningSession;
208
225
  /** Observe completion immediately; the original SDK call can return without waiting for the run's learning. */
package/dist/learning.js CHANGED
@@ -2,6 +2,11 @@
2
2
  // policy, task and selected execution evidence to AgentRun; reflection and checked learning run on the server.
3
3
  import { AgentRun } from './client.js';
4
4
  import { AgentRunRefused, AgentRunTimeout, AgentRunUsage } from './errors.js';
5
+ /** Where a backend run comes from. External platforms report execution through these calls; their display label
6
+ * never selects a managed runtime or grants workflow capabilities. */
7
+ export const BACKEND_SDK_RUNTIMES = ['claude-agent-sdk', 'codex-sdk', 'eve', 'external'];
8
+ /** Only these SDK integrations support an explicit host workflow bridge. */
9
+ const WORKFLOW_RUNTIMES = ['claude-agent-sdk', 'codex-sdk', 'eve'];
5
10
  /** A persistence or learning failure, separate from the application's original SDK result or error. */
6
11
  export class BackendLearningError extends Error {
7
12
  runId;
@@ -21,6 +26,20 @@ const duration = (value, fallback, name) => {
21
26
  };
22
27
  const clone = (value) => JSON.parse(JSON.stringify(value));
23
28
  const size = (text) => new TextEncoder().encode(text).byteLength;
29
+ const bindingOf = (source) => {
30
+ if (!BACKEND_SDK_RUNTIMES.includes(source.runtime))
31
+ throw new AgentRunUsage('Unsupported backend SDK runtime.');
32
+ if (source.runtime === 'external') {
33
+ const { platform } = source;
34
+ if (typeof platform !== 'string' || !platform || platform.trim() !== platform || [...platform].length > 80 || /[\u0000-\u001f\u007f-\u009f\ud800-\udfff]/u.test(platform))
35
+ throw new AgentRunUsage('An external platform must be 1–80 Unicode characters without surrounding whitespace, control characters or unpaired surrogates.');
36
+ return { runtime: 'external', platform };
37
+ }
38
+ if (source.platform !== undefined)
39
+ throw new AgentRunUsage('A platform label is only allowed for the external runtime.');
40
+ return { runtime: source.runtime };
41
+ };
42
+ const sameBinding = (left, right) => left.runtime === right.runtime && left.platform === right.platform;
24
43
  const statusOf = (value, runId) => {
25
44
  const row = value;
26
45
  if (!row || row.run_id !== runId || !['running', 'processing', 'completed', 'failed', 'cancelled'].includes(row.state ?? '')) {
@@ -77,7 +96,7 @@ export class BackendLearningClient {
77
96
  #pending = new Set();
78
97
  #errors = [];
79
98
  #registration;
80
- #runtime;
99
+ #binding;
81
100
  constructor(config) {
82
101
  if (typeof config.agentId !== 'string' || !config.agentId.trim())
83
102
  throw new AgentRunUsage('agentId must be a stable, nonempty agent id.');
@@ -90,15 +109,16 @@ export class BackendLearningClient {
90
109
  if (options.idempotencyKey !== undefined && (!options.idempotencyKey.trim() || options.idempotencyKey.length > 256 || /[^\x21-\x7e]/.test(options.idempotencyKey)))
91
110
  throw new AgentRunUsage('idempotencyKey must be a nonempty printable ASCII string of at most 256 characters.');
92
111
  signal?.throwIfAborted();
93
- if (source.runtime !== 'claude-agent-sdk' && source.runtime !== 'codex-sdk' && source.runtime !== 'eve')
94
- throw new AgentRunUsage('Unsupported backend SDK runtime.');
112
+ const binding = bindingOf(source);
113
+ if (this.#config.workflows && !WORKFLOW_RUNTIMES.includes(binding.runtime))
114
+ throw new AgentRunUsage('External runs cannot use a workflow bridge; report their execution from the platform.');
95
115
  if (typeof source.input !== 'string' || typeof source.instructions !== 'string')
96
116
  throw new AgentRunUsage('SDK input and source instructions must be strings.');
97
117
  // Freeze the source before the registration request: caller mutation cannot change the policy after validation.
98
118
  const start = clone({ ...source, ...(this.#config.workflows ? { workflow_tools: [...this.#config.workflows.catalog], workflow_adapters: [...this.#config.workflows.adapters] } : {}) });
99
- if (this.#runtime !== undefined && this.#runtime !== start.runtime)
100
- throw new AgentRunUsage('A backend learning client belongs to one SDK runtime.');
101
- this.#runtime = start.runtime;
119
+ if (this.#binding !== undefined && !sameBinding(this.#binding, binding))
120
+ throw new AgentRunUsage('A backend learning client belongs to one runtime and platform.');
121
+ this.#binding = binding;
102
122
  this.#registration ??= this.#register(start).catch(error => { this.#registration = undefined; throw error; });
103
123
  await waitFor(this.#registration, signal);
104
124
  signal?.throwIfAborted();
@@ -134,11 +154,14 @@ export class BackendLearningClient {
134
154
  const saved = clone(checkpoint), { receipt, source } = saved;
135
155
  if (saved.baseUrl !== this.#owner.baseUrl || saved.agentId !== this.#config.agentId || receipt?.agent_id !== saved.agentId ||
136
156
  typeof receipt.run_id !== 'string' || !receipt.run_id || typeof receipt.token !== 'string' || !receipt.token || !receipt.learning ||
137
- !source || !['claude-agent-sdk', 'codex-sdk', 'eve'].includes(source.runtime) || typeof source.input !== 'string' || typeof source.instructions !== 'string')
157
+ !source || typeof source.input !== 'string' || typeof source.instructions !== 'string')
138
158
  throw new AgentRunUsage('The SDK receipt belongs to another server or agent, or has no original source.');
139
- if (this.#runtime !== undefined && this.#runtime !== source.runtime)
140
- throw new AgentRunUsage('A backend learning client belongs to one SDK runtime.');
141
- this.#runtime = source.runtime;
159
+ const binding = bindingOf(source);
160
+ if (this.#config.workflows && !WORKFLOW_RUNTIMES.includes(binding.runtime))
161
+ throw new AgentRunUsage('External runs cannot use a workflow bridge; report their execution from the platform.');
162
+ if (this.#binding !== undefined && !sameBinding(this.#binding, binding))
163
+ throw new AgentRunUsage('A backend learning client belongs to one runtime and platform.');
164
+ this.#binding = binding;
142
165
  const scoped = new AgentRun({ baseUrl: saved.baseUrl, apiKey: receipt.token,
143
166
  fetch: this.#config.fetch, timeoutMs: this.#config.timeoutMs, maxRetries: this.#config.maxRetries });
144
167
  const session = new BackendLearningSession(scoped, {
@@ -167,7 +190,7 @@ export class BackendLearningClient {
167
190
  const on = learning === true || (typeof learning === 'object' && learning !== null);
168
191
  const definition = {
169
192
  id: agentId, name: this.#config.name ?? agentId, instructions: source.instructions,
170
- sdk: { runtime: source.runtime }, learning: on ? (learning === true ? true : { ...learning, agree_on: learning.agree_on ? [...learning.agree_on] : undefined }) : false,
193
+ sdk: bindingOf(source), learning: on ? (learning === true ? true : { ...learning, agree_on: learning.agree_on ? [...learning.agree_on] : undefined }) : false,
171
194
  };
172
195
  try {
173
196
  await this.#owner.agents.create({ definition }, { 'Idempotency-Key': uuid() });
@@ -179,8 +202,9 @@ export class BackendLearningClient {
179
202
  }
180
203
  agent = (await read()).body;
181
204
  }
182
- if (agent.definition.sdk?.runtime !== source.runtime)
183
- throw new AgentRunUsage('The existing agent uses a different runtime; choose its matching SDK wrapper or a new agentId.');
205
+ const sdk = agent.definition.sdk;
206
+ if (!sdk || sdk.runtime !== source.runtime || (sdk.runtime === 'external' ? sdk.platform : undefined) !== source.platform)
207
+ throw new AgentRunUsage('The existing agent uses a different runtime or platform; choose its matching source or a new agentId.');
184
208
  }
185
209
  /** Observe completion immediately; the original SDK call can return without waiting for the run's learning. */
186
210
  track(promise) {
@@ -119,6 +119,9 @@ export type DefinitionInput = {
119
119
  max_cost_usd?: number;
120
120
  sdk?: {
121
121
  runtime: "claude-agent-sdk" | "codex-sdk" | "eve";
122
+ } | {
123
+ runtime: "external";
124
+ platform: string;
122
125
  };
123
126
  native?: {
124
127
  runtime: "claude-code" | "codex";
@@ -204,6 +207,9 @@ export type DefinitionInput = {
204
207
  provider?: "auto" | "native" | "exa-keyless" | "parallel" | "off";
205
208
  permission?: "allow" | "ask" | "deny";
206
209
  };
210
+ web_save?: {
211
+ permission?: "allow" | "ask" | "deny";
212
+ };
207
213
  email_search?: {
208
214
  permission?: "allow" | "ask" | "deny";
209
215
  scope?: "thread" | "mailbox";
@@ -344,6 +350,19 @@ export type RunSummary = {
344
350
  cache_write_tokens?: number | null;
345
351
  cost_usd?: number | null;
346
352
  } | null;
353
+ } | {
354
+ runtime: "external";
355
+ platform: string;
356
+ model: string | null;
357
+ session_id: string | null;
358
+ learning_revision: string | null;
359
+ usage: {
360
+ input_tokens?: number | null;
361
+ output_tokens?: number | null;
362
+ cache_read_tokens?: number | null;
363
+ cache_write_tokens?: number | null;
364
+ cost_usd?: number | null;
365
+ } | null;
347
366
  };
348
367
  native?: {
349
368
  runtime: string;
@@ -601,6 +620,19 @@ export type Run = {
601
620
  cache_write_tokens?: number | null;
602
621
  cost_usd?: number | null;
603
622
  } | null;
623
+ } | {
624
+ runtime: "external";
625
+ platform: string;
626
+ model: string | null;
627
+ session_id: string | null;
628
+ learning_revision: string | null;
629
+ usage: {
630
+ input_tokens?: number | null;
631
+ output_tokens?: number | null;
632
+ cache_read_tokens?: number | null;
633
+ cache_write_tokens?: number | null;
634
+ cost_usd?: number | null;
635
+ } | null;
604
636
  };
605
637
  native?: {
606
638
  runtime: string;
@@ -3517,12 +3549,12 @@ export type GetRunUsageResponse = {
3517
3549
  upper_bound?: true;
3518
3550
  } | null;
3519
3551
  lines: Array<{
3520
- meter: "tokens" | "tools" | "browser_time";
3552
+ meter: "tokens" | "tools" | "browser_time" | "compute_time";
3521
3553
  model: string | null;
3522
3554
  provider: string | null;
3523
3555
  charged_by: string | null;
3524
3556
  quantity: number | null;
3525
- unit: "token" | "call" | "second";
3557
+ unit: "token" | "call" | "second" | "vcpu_second" | "gib_second" | "gpu_second" | "gb_month";
3526
3558
  basis: "reported" | "estimate" | "unpriced";
3527
3559
  usd: number | null;
3528
3560
  so_far_usd?: number;
@@ -3537,6 +3569,11 @@ export type GetRunUsageResponse = {
3537
3569
  };
3538
3570
  unmetered_requests?: number;
3539
3571
  not_metered_calls?: number;
3572
+ resource?: "vcpu" | "memory" | "gpu" | "disk";
3573
+ gpu_type?: string | null;
3574
+ unheld_quantity?: number | null;
3575
+ open_computers?: number;
3576
+ measured_to?: string;
3540
3577
  }> | null;
3541
3578
  browser_sessions: Array<{
3542
3579
  provider: string;
@@ -3563,9 +3600,11 @@ export type GetRunUsageResponse = {
3563
3600
  } | null;
3564
3601
  cpus: number | null;
3565
3602
  memory_mb: number | null;
3603
+ disk_gb: number | null;
3566
3604
  started_at: string;
3567
3605
  ended_at: string | null;
3568
3606
  kept: boolean;
3607
+ booked_seconds: number;
3569
3608
  }> | null;
3570
3609
  reason: "running" | "not_booked" | "reopened" | null;
3571
3610
  };
@@ -3650,6 +3689,12 @@ export type StartSdkRunBody = {
3650
3689
  price_usd?: number;
3651
3690
  }>;
3652
3691
  workflow_adapters?: Array<"runNode" | "runJudge">;
3692
+ } | {
3693
+ runtime: "external";
3694
+ platform: string;
3695
+ input: string;
3696
+ instructions: string;
3697
+ model?: string;
3653
3698
  };
3654
3699
  export interface StartSdkRunParams {
3655
3700
  "Idempotency-Key": string;
@@ -3921,6 +3966,7 @@ export type ApplyTemplateResponse = {
3921
3966
  template: string;
3922
3967
  version: string;
3923
3968
  agent_id: string;
3969
+ revision_version: number;
3924
3970
  servers: Record<string, string | null>;
3925
3971
  trigger_ids: Array<string>;
3926
3972
  warnings: Array<InputIssue>;
@@ -4193,6 +4239,9 @@ export type PutToolSettingsBody = {
4193
4239
  provider?: "auto" | "native" | "exa-keyless" | "parallel" | "off";
4194
4240
  permission?: "allow" | "ask" | "deny";
4195
4241
  };
4242
+ web_save?: {
4243
+ permission?: "allow" | "ask" | "deny";
4244
+ };
4196
4245
  email_search?: {
4197
4246
  permission?: "allow" | "ask" | "deny";
4198
4247
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@parcha/agentrun-client",
3
- "version": "0.1.0-beta.49",
3
+ "version": "0.1.0-beta.50",
4
4
  "description": "A typed TypeScript client for a self-hosted AgentRun server: a method per operation of its API, typed from its OpenAPI document, with waiting for a run and following its timeline. No dependencies; fetch only.",
5
5
  "keywords": [
6
6
  "agentrun",
@@ -83,6 +83,6 @@
83
83
  }
84
84
  },
85
85
  "devDependencies": {
86
- "@parcha/agentrun-dsl": "0.1.0-beta.49"
86
+ "@parcha/agentrun-dsl": "0.1.0-beta.50"
87
87
  }
88
88
  }