@gea-ai/agent-sdk 0.1.260923-alpha.2 → 0.1.260923-alpha.3

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.
@@ -2688,6 +2688,7 @@ function replaceLastAssistantInteraction(input) {
2688
2688
  ];
2689
2689
  }
2690
2690
  export async function runAgentWorkerRequest(input) {
2691
+ const requestStartedAt = performance.now();
2691
2692
  const sessionChatId = input.inboxExecution || input.sessionExecution
2692
2693
  ? runRequestSchema.parse(await input.request.clone().json()).chatId
2693
2694
  : undefined;
@@ -2708,6 +2709,7 @@ export async function runAgentWorkerRequest(input) {
2708
2709
  }, async (signal, waitUntil) => {
2709
2710
  const invocationTraceContext = captureTraceContext();
2710
2711
  const invocationSpan = trace.getActiveSpan();
2712
+ invocationSpan?.setAttribute("gea.startup.pre_trace_ms", Math.round(performance.now() - requestStartedAt));
2711
2713
  const invocationTiming = {};
2712
2714
  const cancellation = new AbortController();
2713
2715
  let stopComputer;
@@ -2919,6 +2921,8 @@ export async function runAgentWorkerRequest(input) {
2919
2921
  await finish();
2920
2922
  return response;
2921
2923
  }
2924
+ invocationSpan?.setAttribute("gea.startup.response_headers_ms", Math.round(performance.now() - requestStartedAt));
2925
+ let receivedFirstChunk = false;
2922
2926
  const reader = response.body.getReader();
2923
2927
  const cancel = async (reason) => {
2924
2928
  signal.removeEventListener("abort", onAbort);
@@ -2942,6 +2946,10 @@ export async function runAgentWorkerRequest(input) {
2942
2946
  try {
2943
2947
  const next = await reader.read();
2944
2948
  if (!next.done) {
2949
+ if (!receivedFirstChunk && next.value.byteLength > 0) {
2950
+ receivedFirstChunk = true;
2951
+ invocationSpan?.setAttribute("gea.startup.first_chunk_ms", Math.round(performance.now() - requestStartedAt));
2952
+ }
2945
2953
  controller.enqueue(next.value);
2946
2954
  return;
2947
2955
  }
@@ -2972,6 +2980,7 @@ export async function runAgentWorkerRequest(input) {
2972
2980
  }
2973
2981
  async function executeAgentWorkerRequest(input, waitUntil, ownComputer, ownSessionOutcome, invocationTiming) {
2974
2982
  const invocationTrace = activeTraceRef();
2983
+ const startupTracing = createAgentTracing(input.request.signal);
2975
2984
  const host = input.bindings
2976
2985
  ? createAgentWorkerHostAdapter(input.bindings)
2977
2986
  : input.host;
@@ -3010,14 +3019,14 @@ async function executeAgentWorkerRequest(input, waitUntil, ownComputer, ownSessi
3010
3019
  if (sessionExecution) {
3011
3020
  if (sessionExecution.runId !== payload.runId)
3012
3021
  throw new Error("Session Run identity mismatch.");
3013
- const admission = await requestSessionJson({
3022
+ const admission = await startupTracing.withSpan("agent.startup.session_renew", () => requestSessionJson({
3014
3023
  body: { ...sessionExecution, leaseOwner: "worker" },
3015
3024
  chatId: payload.chatId,
3016
3025
  method: "POST",
3017
3026
  namespace: input.sessionNamespace,
3018
3027
  path: "/v2/turns/renew",
3019
3028
  schema: agentSessionExecutionStartResultSchema,
3020
- });
3029
+ }));
3021
3030
  // Renewal atomically validates the attempt and returns cancellation intent;
3022
3031
  // a separate metadata read before it only adds a network round trip.
3023
3032
  if (admission.turn.cancelRequestedAt)
@@ -3106,29 +3115,6 @@ async function executeAgentWorkerRequest(input, waitUntil, ownComputer, ownSessi
3106
3115
  });
3107
3116
  return result;
3108
3117
  };
3109
- const initializationPromise = input.initialization
3110
- ? Promise.resolve(input.initialization)
3111
- : input.parentRunId !== undefined ||
3112
- !!input.snapshot.subagents?.length ||
3113
- !!Object.keys(input.snapshot.agentReferences ?? {}).length ||
3114
- requiresToolInitialization(input.snapshot, input.hookRuntime)
3115
- ? requireHostJson(host, "/tools/initialize", {
3116
- headers: { "x-gea-agent-runs": "1" },
3117
- }).then((value) => agentWorkerToolInitializationSchema.parse(value))
3118
- : Promise.resolve(agentWorkerToolInitializationSchema.parse({
3119
- connectors: [],
3120
- conversation: {
3121
- canListHistory: false,
3122
- canRead: false,
3123
- canSend: false,
3124
- canStartChild: false,
3125
- },
3126
- runtime: {
3127
- auth: { current: null },
3128
- environment: "production",
3129
- variables: {},
3130
- },
3131
- }));
3132
3118
  const configuredModel = input.snapshot.model ?? payload.model;
3133
3119
  if (!configuredModel) {
3134
3120
  throw new Error("Agent Package does not declare a public model id.");
@@ -3144,9 +3130,54 @@ async function executeAgentWorkerRequest(input, waitUntil, ownComputer, ownSessi
3144
3130
  };
3145
3131
  }
3146
3132
  }
3147
- const [identityValue, initialization] = await Promise.all([
3148
- requireHostJson(host, "/context"),
3149
- initializationPromise,
3133
+ // These reads depend only on the admitted request, not each other or the
3134
+ // Computer. Resolve them together before allocating resources or writing input.
3135
+ const [identityValue, initialization, engine, initialCoreConnection, model, storedContext,] = await Promise.all([
3136
+ startupTracing.withSpan("agent.startup.identity", () => requireHostJson(host, "/context")),
3137
+ input.initialization
3138
+ ? Promise.resolve(input.initialization)
3139
+ : input.parentRunId !== undefined ||
3140
+ !!input.snapshot.subagents?.length ||
3141
+ !!Object.keys(input.snapshot.agentReferences ?? {}).length ||
3142
+ requiresToolInitialization(input.snapshot, input.hookRuntime)
3143
+ ? startupTracing.withSpan("agent.startup.tools", () => requireHostJson(host, "/tools/initialize", {
3144
+ headers: { "x-gea-agent-runs": "1" },
3145
+ }).then((value) => agentWorkerToolInitializationSchema.parse(value)))
3146
+ : Promise.resolve(agentWorkerToolInitializationSchema.parse({
3147
+ connectors: [],
3148
+ conversation: {
3149
+ canListHistory: false,
3150
+ canRead: false,
3151
+ canSend: false,
3152
+ canStartChild: false,
3153
+ },
3154
+ runtime: {
3155
+ auth: { current: null },
3156
+ environment: "production",
3157
+ variables: {},
3158
+ },
3159
+ })),
3160
+ input.engine === "ai-sdk"
3161
+ ? undefined
3162
+ : input.engine === undefined || input.engine === "agent-core"
3163
+ ? startupTracing.withSpan("agent.startup.engine", async () => (await import("./agent-core.js")).agentCore())
3164
+ : input.engine,
3165
+ input.engine !== "ai-sdk"
3166
+ ? startupTracing.withSpan("agent.startup.model", () => createGeaCoreConnection(host, configuredModel, requestOptions, input.request.signal, input.snapshot.maxOutputTokens))
3167
+ : undefined,
3168
+ input.engine !== "ai-sdk"
3169
+ ? undefined
3170
+ : startupTracing.withSpan("agent.startup.model", () => createGeaAI(host, configuredModel, requestOptions.extraBody, {
3171
+ kind: "agent-dev",
3172
+ packageName: input.snapshot.name,
3173
+ }, requestOptions.requestBody)),
3174
+ startupTracing.withSpan("agent.startup.model_context", () => requestSessionJson({
3175
+ chatId: payload.chatId,
3176
+ method: "GET",
3177
+ namespace: input.sessionNamespace,
3178
+ path: sessionExecution ? "/v2/model-context" : "/v1/model-context",
3179
+ schema: agentSessionGetModelContextResultSchema,
3180
+ })),
3150
3181
  ]);
3151
3182
  const identity = agentToolIdentitySchema.parse(identityValue);
3152
3183
  if (input.snapshot.artifacts?.scope === "custom" &&
@@ -3211,7 +3242,7 @@ async function executeAgentWorkerRequest(input, waitUntil, ownComputer, ownSessi
3211
3242
  let computerRoots = defaultComputerRoots;
3212
3243
  let mountedSkillRoot;
3213
3244
  if (input.bindings && input.snapshot.computer.enabled) {
3214
- const scope = await resolveAgentComputerScope({
3245
+ const scope = await startupTracing.withSpan("agent.startup.computer_scope", () => resolveAgentComputerScope({
3215
3246
  namespace: input.sessionNamespace,
3216
3247
  chatId: payload.chatId,
3217
3248
  principal: initialization.runtime.auth.current,
@@ -3219,14 +3250,14 @@ async function executeAgentWorkerRequest(input, waitUntil, ownComputer, ownSessi
3219
3250
  policy: input.snapshot.computer.filesystem?.scope ?? "chat",
3220
3251
  resolver: input.scopeResolver,
3221
3252
  signal: input.request.signal,
3222
- });
3253
+ }));
3223
3254
  const scopedBindings = input.bindings;
3224
3255
  let computer = bindAgentComputerScope(scopedBindings.computer, scope);
3225
3256
  if (input.snapshot.computer.provider) {
3226
- const retained = await readSessionComputer(input.sessionNamespace, payload.chatId);
3257
+ const retained = await startupTracing.withSpan("agent.startup.computer_reference", () => readSessionComputer(input.sessionNamespace, payload.chatId));
3227
3258
  if (retained && !retained.reference)
3228
3259
  throw new Error("Session Computer preparation has not completed. Prepare it before starting a Run.");
3229
- const opened = await createAgentComputerSession({
3260
+ const opened = await startupTracing.withSpan("agent.startup.computer_open", () => createAgentComputerSession({
3230
3261
  computer,
3231
3262
  retain: Boolean(retained),
3232
3263
  reference: retained?.reference ?? undefined,
@@ -3241,7 +3272,7 @@ async function executeAgentWorkerRequest(input, waitUntil, ownComputer, ownSessi
3241
3272
  metadata: payload.metadata,
3242
3273
  scope,
3243
3274
  },
3244
- });
3275
+ }));
3245
3276
  ownComputer(opened.stop);
3246
3277
  computer = opened.computer;
3247
3278
  computerRoots = opened.roots;
@@ -3587,30 +3618,9 @@ async function executeAgentWorkerRequest(input, waitUntil, ownComputer, ownSessi
3587
3618
  throw new Error(`Agent Worker Tool runtime is missing: ${toolName}`);
3588
3619
  }
3589
3620
  }
3590
- const engine = input.engine === "ai-sdk"
3591
- ? undefined
3592
- : input.engine === undefined || input.engine === "agent-core"
3593
- ? (await import("./agent-core.js")).agentCore()
3594
- : input.engine;
3595
- const coreModule = engine?.module();
3596
- const [initialCoreConnection, model, storedContext] = await Promise.all([
3597
- engine
3598
- ? createGeaCoreConnection(host, configuredModel, requestOptions, input.request.signal, input.snapshot.maxOutputTokens)
3599
- : undefined,
3600
- engine
3601
- ? undefined
3602
- : createGeaAI(host, configuredModel, requestOptions.extraBody, {
3603
- kind: "agent-dev",
3604
- packageName: input.snapshot.name,
3605
- }, requestOptions.requestBody),
3606
- requestSessionJson({
3607
- chatId: payload.chatId,
3608
- method: "GET",
3609
- namespace: input.sessionNamespace,
3610
- path: sessionExecution ? "/v2/model-context" : "/v1/model-context",
3611
- schema: agentSessionGetModelContextResultSchema,
3612
- }),
3613
- ]);
3621
+ const coreModule = engine
3622
+ ? await startupTracing.withSpan("agent.startup.engine_module", () => engine.module())
3623
+ : undefined;
3614
3624
  let coreConnection = initialCoreConnection;
3615
3625
  // Model file parts can contain URL and byte values that ordinary JSON cannot preserve.
3616
3626
  const runtimeContext = decodeAgentModelProxyValue(payload.runtimeContext);
@@ -3945,7 +3955,7 @@ async function executeAgentWorkerRequest(input, waitUntil, ownComputer, ownSessi
3945
3955
  return pending;
3946
3956
  };
3947
3957
  if (startsNewTurn && findLatestUserMessage(currentTurn)) {
3948
- await persistModelContext();
3958
+ await startupTracing.withSpan("agent.startup.initial_checkpoint", persistModelContext);
3949
3959
  }
3950
3960
  const writeContextArtifact = async (artifact) => {
3951
3961
  const output = { id: artifact.sha256, content: artifact.content };
@@ -4097,7 +4107,7 @@ async function executeAgentWorkerRequest(input, waitUntil, ownComputer, ownSessi
4097
4107
  messages: prepared.slice(instructionCount),
4098
4108
  };
4099
4109
  };
4100
- const initialPrompt = await preparePrompt(withTurnImages(assembleAgentWorkerContext({ context, runtimeContext }), turnImages));
4110
+ const initialPrompt = await startupTracing.withSpan("agent.startup.prompt", () => preparePrompt(withTurnImages(assembleAgentWorkerContext({ context, runtimeContext }), turnImages)));
4101
4111
  const modelCancellation = new AbortController();
4102
4112
  let failure;
4103
4113
  let pendingMainCall;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gea-ai/agent-sdk",
3
- "version": "0.1.260923-alpha.2",
3
+ "version": "0.1.260923-alpha.3",
4
4
  "private": false,
5
5
  "homepage": "https://musegea.com/developers",
6
6
  "license": "Apache-2.0",
@@ -99,7 +99,7 @@
99
99
  "@ai-sdk/otel": "1.0.9",
100
100
  "@ai-sdk/provider": "4.0.1",
101
101
  "@ai-sdk/provider-utils": "5.0.2",
102
- "@gea-ai/contract": "0.1.260923-alpha.2",
102
+ "@gea-ai/contract": "0.1.260923-alpha.3",
103
103
  "@opentelemetry/api": "1.9.1",
104
104
  "@opentelemetry/context-async-hooks": "2.10.0",
105
105
  "@opentelemetry/core": "2.10.0",