|
9 | 9 | ProviderDriverKind, |
10 | 10 | ProviderInstanceId, |
11 | 11 | RuntimeRequestId, |
| 12 | + RuntimeTaskId, |
12 | 13 | type ThreadId, |
13 | 14 | TurnId, |
14 | 15 | } from "@t3tools/contracts"; |
@@ -67,6 +68,18 @@ import { |
67 | 68 | normalizeGrokReasoningEffort, |
68 | 69 | resolveGrokAcpBaseModelId, |
69 | 70 | } from "../acp/GrokAcpSupport.ts"; |
| 71 | +import { |
| 72 | + applyGrokSubagentUpdate, |
| 73 | + applyGrokWorkflowUpdate, |
| 74 | + emptyGrokSubagentTrackState, |
| 75 | + GROK_SESSION_NOTIFICATION_METHODS, |
| 76 | + parseXAiSubagentUpdate, |
| 77 | + parseXAiWorkflowUpdated, |
| 78 | + XAiSessionNotification, |
| 79 | + type GrokSessionNotificationMethod, |
| 80 | + type GrokSubagentTrackState, |
| 81 | + type GrokTaskEventSpec, |
| 82 | +} from "../acp/GrokAcpSubagents.ts"; |
70 | 83 | import { |
71 | 84 | extractGrokPlanMarkdownFromToolCallData, |
72 | 85 | extractXAiAskUserQuestions, |
@@ -162,6 +175,14 @@ interface GrokSessionContext { |
162 | 175 | promptResponsesReady: number; |
163 | 176 | currentModelId: string | undefined; |
164 | 177 | currentReasoningEffort: string | undefined; |
| 178 | + /** |
| 179 | + * Grok `agent()` / workflow ACP extras mapped onto task.* so the Agents |
| 180 | + * panel can list them the same way Claude Task and Codex collab children |
| 181 | + * already do. |
| 182 | + */ |
| 183 | + workflowTrack: GrokSubagentTrackState; |
| 184 | + /** First-seen spawn turn per task, reused after the parent turn settles. */ |
| 185 | + readonly taskSpawnTurnIds: Map<string, TurnId>; |
165 | 186 | stopped: boolean; |
166 | 187 | } |
167 | 188 |
|
@@ -399,6 +420,45 @@ export function makeGrokAdapter(grokSettings: GrokSettings, options?: GrokAdapte |
399 | 420 | const offerRuntimeEvent = (event: ProviderRuntimeEvent) => |
400 | 421 | PubSub.publish(runtimeEventPubSub, event).pipe(Effect.asVoid); |
401 | 422 |
|
| 423 | + const emitGrokTaskSpecs = (input: { |
| 424 | + readonly ctx: GrokSessionContext; |
| 425 | + readonly method: string; |
| 426 | + readonly payload: unknown; |
| 427 | + readonly specs: ReadonlyArray<GrokTaskEventSpec>; |
| 428 | + }) => |
| 429 | + Effect.forEach( |
| 430 | + input.specs, |
| 431 | + (spec) => |
| 432 | + Effect.gen(function* () { |
| 433 | + const taskIdValue = spec.payload.taskId; |
| 434 | + if (typeof taskIdValue !== "string" || taskIdValue.length === 0) { |
| 435 | + return; |
| 436 | + } |
| 437 | + const taskId = RuntimeTaskId.make(taskIdValue); |
| 438 | + let turnId = input.ctx.taskSpawnTurnIds.get(taskIdValue); |
| 439 | + if (turnId === undefined) { |
| 440 | + turnId = resolveNotificationTurnId(input.ctx); |
| 441 | + if (turnId !== undefined) { |
| 442 | + input.ctx.taskSpawnTurnIds.set(taskIdValue, turnId); |
| 443 | + } |
| 444 | + } |
| 445 | + yield* offerRuntimeEvent({ |
| 446 | + type: spec.type, |
| 447 | + ...(yield* makeEventStamp()), |
| 448 | + provider: PROVIDER, |
| 449 | + threadId: input.ctx.threadId, |
| 450 | + ...(turnId !== undefined ? { turnId } : {}), |
| 451 | + payload: { ...spec.payload, taskId }, |
| 452 | + raw: { |
| 453 | + source: "acp.grok.extension", |
| 454 | + method: input.method, |
| 455 | + payload: input.payload, |
| 456 | + }, |
| 457 | + } as ProviderRuntimeEvent); |
| 458 | + }), |
| 459 | + { discard: true }, |
| 460 | + ); |
| 461 | + |
402 | 462 | const getThreadSemaphore = (threadId: string) => |
403 | 463 | SynchronizedRef.modifyEffect(threadLocksRef, (current) => { |
404 | 464 | const existing: Option.Option<Semaphore.Semaphore> = Option.fromNullishOr( |
@@ -506,6 +566,40 @@ export function makeGrokAdapter(grokSettings: GrokSettings, options?: GrokAdapte |
506 | 566 | return ctx && turnId !== undefined ? signalTurnLiveness(ctx, turnId) : Effect.void; |
507 | 567 | }; |
508 | 568 |
|
| 569 | + const applyGrokSessionNotification = ( |
| 570 | + ctx: GrokSessionContext, |
| 571 | + method: GrokSessionNotificationMethod, |
| 572 | + params: unknown, |
| 573 | + ) => |
| 574 | + Effect.gen(function* () { |
| 575 | + const workflow = parseXAiWorkflowUpdated(params); |
| 576 | + if (workflow) { |
| 577 | + const applied = applyGrokWorkflowUpdate(ctx.workflowTrack, workflow); |
| 578 | + ctx.workflowTrack = applied.state; |
| 579 | + yield* emitGrokTaskSpecs({ |
| 580 | + ctx, |
| 581 | + method, |
| 582 | + payload: params, |
| 583 | + specs: applied.events, |
| 584 | + }); |
| 585 | + yield* signalSessionTurnLiveness(ctx.threadId, resolveNotificationTurnId(ctx)); |
| 586 | + return; |
| 587 | + } |
| 588 | + const subagent = parseXAiSubagentUpdate(params); |
| 589 | + if (!subagent) { |
| 590 | + return; |
| 591 | + } |
| 592 | + const applied = applyGrokSubagentUpdate(ctx.workflowTrack, subagent); |
| 593 | + ctx.workflowTrack = applied.state; |
| 594 | + yield* emitGrokTaskSpecs({ |
| 595 | + ctx, |
| 596 | + method, |
| 597 | + payload: params, |
| 598 | + specs: applied.events, |
| 599 | + }); |
| 600 | + yield* signalSessionTurnLiveness(ctx.threadId, resolveNotificationTurnId(ctx)); |
| 601 | + }); |
| 602 | + |
509 | 603 | const resumeSessionTurnLiveness = Effect.fn("GrokAdapter.resumeSessionTurnLiveness")(function* ( |
510 | 604 | threadId: ThreadId, |
511 | 605 | turnId: TurnId | undefined, |
@@ -1018,6 +1112,12 @@ export function makeGrokAdapter(grokSettings: GrokSettings, options?: GrokAdapte |
1018 | 1112 | }), |
1019 | 1113 | ), |
1020 | 1114 | ); |
| 1115 | + const pendingSessionNotifications: Array<{ |
| 1116 | + readonly method: GrokSessionNotificationMethod; |
| 1117 | + readonly params: unknown; |
| 1118 | + }> = []; |
| 1119 | + let sessionNotificationsReady = false; |
| 1120 | + const sessionNotificationLock = yield* Semaphore.make(1); |
1021 | 1121 | const started = yield* Effect.gen(function* () { |
1022 | 1122 | yield* Effect.forEach( |
1023 | 1123 | ["x.ai/ask_user_question", "_x.ai/ask_user_question"] as const, |
@@ -1121,6 +1221,34 @@ export function makeGrokAdapter(grokSettings: GrokSettings, options?: GrokAdapte |
1121 | 1221 | ), |
1122 | 1222 | { discard: true }, |
1123 | 1223 | ); |
| 1224 | + // Grok Build streams agent() / workflow runs over a private ACP |
| 1225 | + // channel. Claude maps Task/workflow_progress onto task.*; Codex |
| 1226 | + // maps collabAgent/* the same way. Keep that seam: parse here, |
| 1227 | + // emit canonical events, never a third Agents UI. |
| 1228 | + yield* Effect.forEach( |
| 1229 | + GROK_SESSION_NOTIFICATION_METHODS, |
| 1230 | + (method) => |
| 1231 | + acp.handleExtNotification(method, XAiSessionNotification, (params) => |
| 1232 | + mapAcpCallbackFailure( |
| 1233 | + Effect.gen(function* () { |
| 1234 | + yield* logNative(input.threadId, method, params); |
| 1235 | + if (!sessionNotificationsReady) { |
| 1236 | + pendingSessionNotifications.push({ method, params }); |
| 1237 | + return; |
| 1238 | + } |
| 1239 | + const liveCtx = sessions.get(input.threadId); |
| 1240 | + if (!liveCtx) { |
| 1241 | + pendingSessionNotifications.push({ method, params }); |
| 1242 | + return; |
| 1243 | + } |
| 1244 | + yield* sessionNotificationLock.withPermit( |
| 1245 | + applyGrokSessionNotification(liveCtx, method, params), |
| 1246 | + ); |
| 1247 | + }), |
| 1248 | + ), |
| 1249 | + ), |
| 1250 | + { discard: true }, |
| 1251 | + ); |
1124 | 1252 | yield* acp.handleRequestPermission((params) => |
1125 | 1253 | mapAcpCallbackFailure( |
1126 | 1254 | Effect.gen(function* () { |
@@ -1290,6 +1418,8 @@ export function makeGrokAdapter(grokSettings: GrokSettings, options?: GrokAdapte |
1290 | 1418 | requestedStartReasoningEffort !== undefined |
1291 | 1419 | ? normalizeGrokReasoningEffort(requestedStartReasoningEffort) |
1292 | 1420 | : currentStartReasoningEffort, |
| 1421 | + workflowTrack: emptyGrokSubagentTrackState(), |
| 1422 | + taskSpawnTurnIds: new Map(), |
1293 | 1423 | stopped: false, |
1294 | 1424 | }; |
1295 | 1425 |
|
@@ -1433,6 +1563,15 @@ export function makeGrokAdapter(grokSettings: GrokSettings, options?: GrokAdapte |
1433 | 1563 | sessions.set(input.threadId, ctx); |
1434 | 1564 | yield* runTurnLivenessWatchdog(ctx).pipe(Effect.forkIn(ctx.scope), Effect.asVoid); |
1435 | 1565 | sessionScopeTransferred = true; |
| 1566 | + yield* sessionNotificationLock.withPermit( |
| 1567 | + Effect.gen(function* () { |
| 1568 | + const batch = pendingSessionNotifications.splice(0); |
| 1569 | + sessionNotificationsReady = true; |
| 1570 | + yield* Effect.forEach(batch, (pending) => |
| 1571 | + applyGrokSessionNotification(ctx, pending.method, pending.params), |
| 1572 | + ); |
| 1573 | + }), |
| 1574 | + ); |
1436 | 1575 |
|
1437 | 1576 | yield* offerRuntimeEvent({ |
1438 | 1577 | type: "session.started", |
|
0 commit comments