@gea-ai/agent-sdk 0.1.260920-alpha.3 → 0.1.260922-alpha.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.
@@ -103,12 +103,44 @@ export declare function createWorkerAgentTool(input: {
103
103
  output: NoInfer<{
104
104
  runId: string;
105
105
  sessionId: string;
106
+ } | {
107
+ error: "subagent_concurrency_limit";
108
+ limit: number;
109
+ active: number;
110
+ pendingRunIds: string[];
111
+ pendingAdmissions: number;
112
+ message: string;
113
+ } | {
114
+ error: "remote_admission_unknown" | "remote_attachment_unconfirmed" | "remote_run_rejected";
115
+ reservationId: string;
116
+ runId?: string | undefined;
117
+ sessionId?: string | undefined;
118
+ message: string;
119
+ } | {
120
+ error: "child_busy";
121
+ message: string;
106
122
  }>;
107
123
  }) => import("@ai-sdk/provider-utils").ToolResultOutput | PromiseLike<import("@ai-sdk/provider-utils").ToolResultOutput>) | undefined;
108
124
  } & {
109
125
  outputSchema?: import("ai").FlexibleSchema<{
110
126
  runId: string;
111
127
  sessionId: string;
128
+ } | {
129
+ error: "subagent_concurrency_limit";
130
+ limit: number;
131
+ active: number;
132
+ pendingRunIds: string[];
133
+ pendingAdmissions: number;
134
+ message: string;
135
+ } | {
136
+ error: "remote_admission_unknown" | "remote_attachment_unconfirmed" | "remote_run_rejected";
137
+ reservationId: string;
138
+ runId?: string | undefined;
139
+ sessionId?: string | undefined;
140
+ message: string;
141
+ } | {
142
+ error: "child_busy";
143
+ message: string;
112
144
  }> | undefined;
113
145
  execute: import("ai").ToolExecuteFunction<{
114
146
  sessionId?: string | undefined;
@@ -132,6 +164,22 @@ export declare function createWorkerAgentTool(input: {
132
164
  }, {
133
165
  runId: string;
134
166
  sessionId: string;
167
+ } | {
168
+ error: "subagent_concurrency_limit";
169
+ limit: number;
170
+ active: number;
171
+ pendingRunIds: string[];
172
+ pendingAdmissions: number;
173
+ message: string;
174
+ } | {
175
+ error: "remote_admission_unknown" | "remote_attachment_unconfirmed" | "remote_run_rejected";
176
+ reservationId: string;
177
+ runId?: string | undefined;
178
+ sessionId?: string | undefined;
179
+ message: string;
180
+ } | {
181
+ error: "child_busy";
182
+ message: string;
135
183
  }, NoInfer<import("@ai-sdk/provider-utils").Context>>;
136
184
  } & {
137
185
  description?: string | ((options: {
@@ -190,6 +238,22 @@ export declare function createWorkerAgentTool(input: {
190
238
  }, {
191
239
  runId: string;
192
240
  sessionId: string;
241
+ } | {
242
+ error: "subagent_concurrency_limit";
243
+ limit: number;
244
+ active: number;
245
+ pendingRunIds: string[];
246
+ pendingAdmissions: number;
247
+ message: string;
248
+ } | {
249
+ error: "remote_admission_unknown" | "remote_attachment_unconfirmed" | "remote_run_rejected";
250
+ reservationId: string;
251
+ runId?: string | undefined;
252
+ sessionId?: string | undefined;
253
+ message: string;
254
+ } | {
255
+ error: "child_busy";
256
+ message: string;
193
257
  }, NoInfer<import("@ai-sdk/provider-utils").Context>>;
194
258
  }) | ({
195
259
  title?: string;
@@ -287,12 +351,44 @@ export declare function createWorkerAgentTool(input: {
287
351
  output: NoInfer<{
288
352
  runId: string;
289
353
  sessionId: string;
354
+ } | {
355
+ error: "subagent_concurrency_limit";
356
+ limit: number;
357
+ active: number;
358
+ pendingRunIds: string[];
359
+ pendingAdmissions: number;
360
+ message: string;
361
+ } | {
362
+ error: "remote_admission_unknown" | "remote_attachment_unconfirmed" | "remote_run_rejected";
363
+ reservationId: string;
364
+ runId?: string | undefined;
365
+ sessionId?: string | undefined;
366
+ message: string;
367
+ } | {
368
+ error: "child_busy";
369
+ message: string;
290
370
  }>;
291
371
  }) => import("@ai-sdk/provider-utils").ToolResultOutput | PromiseLike<import("@ai-sdk/provider-utils").ToolResultOutput>) | undefined;
292
372
  } & {
293
373
  outputSchema?: import("ai").FlexibleSchema<{
294
374
  runId: string;
295
375
  sessionId: string;
376
+ } | {
377
+ error: "subagent_concurrency_limit";
378
+ limit: number;
379
+ active: number;
380
+ pendingRunIds: string[];
381
+ pendingAdmissions: number;
382
+ message: string;
383
+ } | {
384
+ error: "remote_admission_unknown" | "remote_attachment_unconfirmed" | "remote_run_rejected";
385
+ reservationId: string;
386
+ runId?: string | undefined;
387
+ sessionId?: string | undefined;
388
+ message: string;
389
+ } | {
390
+ error: "child_busy";
391
+ message: string;
296
392
  }> | undefined;
297
393
  execute: import("ai").ToolExecuteFunction<{
298
394
  sessionId?: string | undefined;
@@ -316,6 +412,22 @@ export declare function createWorkerAgentTool(input: {
316
412
  }, {
317
413
  runId: string;
318
414
  sessionId: string;
415
+ } | {
416
+ error: "subagent_concurrency_limit";
417
+ limit: number;
418
+ active: number;
419
+ pendingRunIds: string[];
420
+ pendingAdmissions: number;
421
+ message: string;
422
+ } | {
423
+ error: "remote_admission_unknown" | "remote_attachment_unconfirmed" | "remote_run_rejected";
424
+ reservationId: string;
425
+ runId?: string | undefined;
426
+ sessionId?: string | undefined;
427
+ message: string;
428
+ } | {
429
+ error: "child_busy";
430
+ message: string;
319
431
  }, NoInfer<import("@ai-sdk/provider-utils").Context>>;
320
432
  } & {
321
433
  description?: string | ((options: {
@@ -374,6 +486,22 @@ export declare function createWorkerAgentTool(input: {
374
486
  }, {
375
487
  runId: string;
376
488
  sessionId: string;
489
+ } | {
490
+ error: "subagent_concurrency_limit";
491
+ limit: number;
492
+ active: number;
493
+ pendingRunIds: string[];
494
+ pendingAdmissions: number;
495
+ message: string;
496
+ } | {
497
+ error: "remote_admission_unknown" | "remote_attachment_unconfirmed" | "remote_run_rejected";
498
+ reservationId: string;
499
+ runId?: string | undefined;
500
+ sessionId?: string | undefined;
501
+ message: string;
502
+ } | {
503
+ error: "child_busy";
504
+ message: string;
377
505
  }, NoInfer<import("@ai-sdk/provider-utils").Context>>;
378
506
  }) | ({
379
507
  title?: string;
@@ -471,12 +599,44 @@ export declare function createWorkerAgentTool(input: {
471
599
  output: NoInfer<{
472
600
  runId: string;
473
601
  sessionId: string;
602
+ } | {
603
+ error: "subagent_concurrency_limit";
604
+ limit: number;
605
+ active: number;
606
+ pendingRunIds: string[];
607
+ pendingAdmissions: number;
608
+ message: string;
609
+ } | {
610
+ error: "remote_admission_unknown" | "remote_attachment_unconfirmed" | "remote_run_rejected";
611
+ reservationId: string;
612
+ runId?: string | undefined;
613
+ sessionId?: string | undefined;
614
+ message: string;
615
+ } | {
616
+ error: "child_busy";
617
+ message: string;
474
618
  }>;
475
619
  }) => import("@ai-sdk/provider-utils").ToolResultOutput | PromiseLike<import("@ai-sdk/provider-utils").ToolResultOutput>) | undefined;
476
620
  } & {
477
621
  outputSchema?: import("ai").FlexibleSchema<{
478
622
  runId: string;
479
623
  sessionId: string;
624
+ } | {
625
+ error: "subagent_concurrency_limit";
626
+ limit: number;
627
+ active: number;
628
+ pendingRunIds: string[];
629
+ pendingAdmissions: number;
630
+ message: string;
631
+ } | {
632
+ error: "remote_admission_unknown" | "remote_attachment_unconfirmed" | "remote_run_rejected";
633
+ reservationId: string;
634
+ runId?: string | undefined;
635
+ sessionId?: string | undefined;
636
+ message: string;
637
+ } | {
638
+ error: "child_busy";
639
+ message: string;
480
640
  }> | undefined;
481
641
  execute: import("ai").ToolExecuteFunction<{
482
642
  sessionId?: string | undefined;
@@ -500,6 +660,22 @@ export declare function createWorkerAgentTool(input: {
500
660
  }, {
501
661
  runId: string;
502
662
  sessionId: string;
663
+ } | {
664
+ error: "subagent_concurrency_limit";
665
+ limit: number;
666
+ active: number;
667
+ pendingRunIds: string[];
668
+ pendingAdmissions: number;
669
+ message: string;
670
+ } | {
671
+ error: "remote_admission_unknown" | "remote_attachment_unconfirmed" | "remote_run_rejected";
672
+ reservationId: string;
673
+ runId?: string | undefined;
674
+ sessionId?: string | undefined;
675
+ message: string;
676
+ } | {
677
+ error: "child_busy";
678
+ message: string;
503
679
  }, NoInfer<import("@ai-sdk/provider-utils").Context>>;
504
680
  } & {
505
681
  type: 'provider';
@@ -534,6 +710,22 @@ export declare function createWorkerAgentTool(input: {
534
710
  }, {
535
711
  runId: string;
536
712
  sessionId: string;
713
+ } | {
714
+ error: "subagent_concurrency_limit";
715
+ limit: number;
716
+ active: number;
717
+ pendingRunIds: string[];
718
+ pendingAdmissions: number;
719
+ message: string;
720
+ } | {
721
+ error: "remote_admission_unknown" | "remote_attachment_unconfirmed" | "remote_run_rejected";
722
+ reservationId: string;
723
+ runId?: string | undefined;
724
+ sessionId?: string | undefined;
725
+ message: string;
726
+ } | {
727
+ error: "child_busy";
728
+ message: string;
537
729
  }, NoInfer<import("@ai-sdk/provider-utils").Context>>;
538
730
  }) | ({
539
731
  title?: string;
@@ -631,12 +823,44 @@ export declare function createWorkerAgentTool(input: {
631
823
  output: NoInfer<{
632
824
  runId: string;
633
825
  sessionId: string;
826
+ } | {
827
+ error: "subagent_concurrency_limit";
828
+ limit: number;
829
+ active: number;
830
+ pendingRunIds: string[];
831
+ pendingAdmissions: number;
832
+ message: string;
833
+ } | {
834
+ error: "remote_admission_unknown" | "remote_attachment_unconfirmed" | "remote_run_rejected";
835
+ reservationId: string;
836
+ runId?: string | undefined;
837
+ sessionId?: string | undefined;
838
+ message: string;
839
+ } | {
840
+ error: "child_busy";
841
+ message: string;
634
842
  }>;
635
843
  }) => import("@ai-sdk/provider-utils").ToolResultOutput | PromiseLike<import("@ai-sdk/provider-utils").ToolResultOutput>) | undefined;
636
844
  } & {
637
845
  outputSchema?: import("ai").FlexibleSchema<{
638
846
  runId: string;
639
847
  sessionId: string;
848
+ } | {
849
+ error: "subagent_concurrency_limit";
850
+ limit: number;
851
+ active: number;
852
+ pendingRunIds: string[];
853
+ pendingAdmissions: number;
854
+ message: string;
855
+ } | {
856
+ error: "remote_admission_unknown" | "remote_attachment_unconfirmed" | "remote_run_rejected";
857
+ reservationId: string;
858
+ runId?: string | undefined;
859
+ sessionId?: string | undefined;
860
+ message: string;
861
+ } | {
862
+ error: "child_busy";
863
+ message: string;
640
864
  }> | undefined;
641
865
  execute: import("ai").ToolExecuteFunction<{
642
866
  sessionId?: string | undefined;
@@ -660,6 +884,22 @@ export declare function createWorkerAgentTool(input: {
660
884
  }, {
661
885
  runId: string;
662
886
  sessionId: string;
887
+ } | {
888
+ error: "subagent_concurrency_limit";
889
+ limit: number;
890
+ active: number;
891
+ pendingRunIds: string[];
892
+ pendingAdmissions: number;
893
+ message: string;
894
+ } | {
895
+ error: "remote_admission_unknown" | "remote_attachment_unconfirmed" | "remote_run_rejected";
896
+ reservationId: string;
897
+ runId?: string | undefined;
898
+ sessionId?: string | undefined;
899
+ message: string;
900
+ } | {
901
+ error: "child_busy";
902
+ message: string;
663
903
  }, NoInfer<import("@ai-sdk/provider-utils").Context>>;
664
904
  } & {
665
905
  type: 'provider';
@@ -694,6 +934,22 @@ export declare function createWorkerAgentTool(input: {
694
934
  }, {
695
935
  runId: string;
696
936
  sessionId: string;
937
+ } | {
938
+ error: "subagent_concurrency_limit";
939
+ limit: number;
940
+ active: number;
941
+ pendingRunIds: string[];
942
+ pendingAdmissions: number;
943
+ message: string;
944
+ } | {
945
+ error: "remote_admission_unknown" | "remote_attachment_unconfirmed" | "remote_run_rejected";
946
+ reservationId: string;
947
+ runId?: string | undefined;
948
+ sessionId?: string | undefined;
949
+ message: string;
950
+ } | {
951
+ error: "child_busy";
952
+ message: string;
697
953
  }, NoInfer<import("@ai-sdk/provider-utils").Context>>;
698
954
  });
699
955
  export declare function createWorkerRunCancelTool({ host, }: {
@@ -1,9 +1,9 @@
1
1
  import { agentHttpCancellationSchema } from "@gea-ai/contract/agent-http-api";
2
2
  import { z } from "zod";
3
- import { TaggedError } from "better-result";
3
+ import { Result, TaggedError } from "better-result";
4
4
  import { parseJsonEventStream } from "@ai-sdk/provider-utils";
5
5
  import { tool, uiMessageChunkSchema } from "ai";
6
- import { agentCallInputSchema, agentCallTextMessageSchema, agentRunCallResultSchema, getAgentCallFileIds, } from "@gea-ai/contract/agent-worker-tools";
6
+ import { agentSubagentConcurrencyErrorSchema, agentRemoteAdmissionErrorSchema, agentCallInputSchema, agentCallTextMessageSchema, agentRunCallResultSchema, getAgentCallFileIds, } from "@gea-ai/contract/agent-worker-tools";
7
7
  import { captureTraceContext } from "./trace-context.js";
8
8
  /** The host opens a fresh Worker invocation; only its visible answer returns to the caller. */
9
9
  export function createWorkerAgentTool(input) {
@@ -33,8 +33,37 @@ export function createWorkerAgentTool(input) {
33
33
  }),
34
34
  signal: options.abortSignal,
35
35
  });
36
- if (!response.ok)
36
+ if (!response.ok) {
37
+ const body = await Result.tryPromise(() => response.json());
38
+ if (body.isOk()) {
39
+ const value = body.value;
40
+ const limit = agentSubagentConcurrencyErrorSchema.safeParse(value);
41
+ if (limit.success)
42
+ return {
43
+ ...limit.data,
44
+ message: limit.data.pendingRunIds.length
45
+ ? "Subagent concurrency is full. Call runWait with pendingRunIds before retrying agent. Cancellation releases capacity only after terminal cleanup."
46
+ : "Remote admissions still hold all capacity. Their outcomes must be resolved before retrying; do not resubmit an uncertain remote Run.",
47
+ };
48
+ const remote = agentRemoteAdmissionErrorSchema.safeParse(value);
49
+ if (remote.success)
50
+ return {
51
+ ...remote.data,
52
+ message: remote.data.error === "remote_run_rejected"
53
+ ? "The remote Run was not admitted. Resolve the remote service failure before retrying."
54
+ : "Remote Run admission is uncertain and its capacity is retained. Do not resubmit; reconcile the reservation with the remote service first.",
55
+ };
56
+ const busy = z
57
+ .object({ error: z.literal("child_busy") })
58
+ .safeParse(value);
59
+ if (busy.success)
60
+ return {
61
+ error: "child_busy",
62
+ message: "This child Session has unfinished work. Wait for its Run before continuing the Session.",
63
+ };
64
+ }
37
65
  throw new Error(`Agent invocation was rejected (${response.status}).`);
66
+ }
38
67
  return agentRunCallResultSchema.parse(await response.json());
39
68
  },
40
69
  });
@@ -19,19 +19,6 @@ export declare function executeChannelBehavior(input: {
19
19
  environment: AgentChannelFetchEnvironment;
20
20
  signal: AbortSignal;
21
21
  }): Promise<{
22
- sequence: number;
23
- duplicate: boolean;
24
- } | {
25
- state: AgentChannelJson;
26
- intents: {
27
- key: string;
28
- payload: AgentChannelJson;
29
- eventId: string;
30
- operationId: string;
31
- }[];
32
- cursor: number;
33
- idempotencyWindowMs?: number | undefined;
34
- } | {
35
22
  status: "sent";
36
23
  receipt?: AgentChannelJson | undefined;
37
24
  } | {
@@ -56,6 +43,19 @@ export declare function executeChannelBehavior(input: {
56
43
  message: string;
57
44
  retryable: boolean;
58
45
  };
46
+ } | {
47
+ sequence: number;
48
+ duplicate: boolean;
49
+ } | {
50
+ state: AgentChannelJson;
51
+ intents: {
52
+ key: string;
53
+ payload: AgentChannelJson;
54
+ eventId: string;
55
+ operationId: string;
56
+ }[];
57
+ cursor: number;
58
+ idempotencyWindowMs?: number | undefined;
59
59
  } | {
60
60
  events: {
61
61
  id: string;
@@ -1,3 +1,4 @@
1
+ import { Result } from "better-result";
1
2
  import type { AgentCallMessage, AgentChildRun, AgentChildRunDestination, AgentChildRunOutcome } from "@gea-ai/contract/agent-worker-tools";
2
3
  import type { StudioArtifactInputFile } from "@gea-ai/contract/studio-artifacts";
3
4
  /** Internal transport shared by hosted and local Run dispatch; not a public API client. */
@@ -5,6 +6,14 @@ export type RemoteRunFetch = (path: string, init?: RequestInit) => Promise<Respo
5
6
  export type RemoteRunDestination = Extract<AgentChildRunDestination, {
6
7
  type: "remote";
7
8
  }>;
9
+ declare const RemoteRunAdmissionError_base: import("better-result").TaggedErrorClass<"RemoteRunAdmissionError", {
10
+ message: string;
11
+ cause: unknown;
12
+ sessionId?: string;
13
+ definitive: boolean;
14
+ }>;
15
+ declare class RemoteRunAdmissionError extends RemoteRunAdmissionError_base {
16
+ }
8
17
  export declare function submitRemoteRun(fetch: RemoteRunFetch, input: {
9
18
  destination: RemoteRunDestination;
10
19
  sessionId?: string;
@@ -12,9 +21,10 @@ export declare function submitRemoteRun(fetch: RemoteRunFetch, input: {
12
21
  artifacts?: Array<StudioArtifactInputFile & {
13
22
  id: string;
14
23
  }>;
15
- }): Promise<{
24
+ }): Promise<Result<{
16
25
  runId: string;
17
26
  sessionId: string;
18
- }>;
27
+ }, RemoteRunAdmissionError>>;
19
28
  export declare function cancelRemoteRun(fetch: RemoteRunFetch, run: AgentChildRun): Promise<void>;
20
29
  export declare function observeRemoteRun(fetch: RemoteRunFetch, run: AgentChildRun, signal: AbortSignal): Promise<AgentChildRunOutcome>;
30
+ export {};
@@ -1,3 +1,4 @@
1
+ import { Result, TaggedError } from "better-result";
1
2
  import { z } from "zod";
2
3
  import { agentHttpRunSchema, agentHttpMessageResourceSchema, } from "@gea-ai/contract/agent-http-api";
3
4
  const sessionSchema = z.object({ id: z.uuid() });
@@ -12,28 +13,44 @@ async function responseJson(response) {
12
13
  }
13
14
  return response.json();
14
15
  }
16
+ class RemoteRunAdmissionError extends TaggedError("RemoteRunAdmissionError")() {
17
+ }
15
18
  export async function submitRemoteRun(fetch, input) {
16
- const sessionId = input.sessionId ??
17
- sessionSchema.parse(await responseJson(await fetch("/sessions", {
18
- method: "POST",
19
- headers: { "content-type": "application/json" },
20
- body: JSON.stringify({
21
- agent_id: input.destination.agentId,
22
- environment: input.destination.environment,
23
- }),
24
- }))).id;
25
- // No automatic replay: a lost response does not prove that Run admission failed.
26
- const run = agentHttpRunSchema.parse(await responseJson(await fetch(`/sessions/${sessionId}/runs`, {
27
- method: "POST",
28
- headers: { "content-type": "application/json" },
29
- body: JSON.stringify({
30
- input: input.message,
31
- stream: false,
19
+ let sessionId = input.sessionId;
20
+ let submitting = false;
21
+ let rejected = false;
22
+ return Result.tryPromise({
23
+ try: async () => {
24
+ sessionId ??= sessionSchema.parse(await responseJson(await fetch("/sessions", {
25
+ method: "POST",
26
+ headers: { "content-type": "application/json" },
27
+ body: JSON.stringify({
28
+ agent_id: input.destination.agentId,
29
+ environment: input.destination.environment,
30
+ }),
31
+ }))).id;
32
+ submitting = true;
33
+ // No automatic replay: a lost response does not prove Run admission failed.
34
+ const response = await fetch(`/sessions/${sessionId}/runs`, {
35
+ method: "POST",
36
+ headers: { "content-type": "application/json" },
37
+ body: JSON.stringify({ input: input.message, stream: false }),
38
+ });
39
+ rejected = [400, 401, 403, 404, 405, 409, 413, 415, 422, 429].includes(response.status);
40
+ const run = agentHttpRunSchema.parse(await responseJson(response));
41
+ if (run.session_id !== sessionId)
42
+ throw new Error("Remote Run Session mismatch.");
43
+ return { runId: run.id, sessionId };
44
+ },
45
+ catch: (cause) => new RemoteRunAdmissionError({
46
+ cause,
47
+ sessionId,
48
+ definitive: !submitting || rejected,
49
+ message: !submitting || rejected
50
+ ? "Remote Run was not admitted."
51
+ : "Remote Run admission is unknown; do not resubmit.",
32
52
  }),
33
- })));
34
- if (run.session_id !== sessionId)
35
- throw new Error("Remote Run Session mismatch.");
36
- return { runId: run.id, sessionId };
53
+ });
37
54
  }
38
55
  export async function cancelRemoteRun(fetch, run) {
39
56
  const response = await fetch(`/runs/${run.id}/cancel`, { method: "POST" });