GeezerVison is showing github.com — feat(server): a thread turn can return a typed result (#38874) · pingdotgg/t3code@2770bf3
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/cli/thread.ts +48-4Lines changed: 48 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -528,6 +528,7 @@ type WaitOutcome = | 528 | 528 | readonly type: "ended"; | 529 | 529 | readonly runId: RunId | null; | 530 | 530 | readonly status: OrchestratorMcpThreadStatus; | | 531 | + readonly output?: Schema.Json; | 531 | 532 | } | 532 | 533 | | { readonly type: "needs_input" }; | 533 | 534 |
| @@ -598,7 +599,12 @@ export const waitForRun = Effect.fn("cli.thread.waitForRun")(function* ( | 598 | 599 | OrchestratorMcpThreadWaitResult, | 599 | 600 | ); | 600 | 601 | const ended = (waited: OrchestratorMcpThreadWaitResult) => | 601 | | - ({ type: "ended", runId: waited.runId, status: waited.status }) satisfies WaitOutcome; | | 602 | + ({ | | 603 | + type: "ended", | | 604 | + runId: waited.runId, | | 605 | + status: waited.status, | | 606 | + ...(waited.output === undefined ? {} : { output: waited.output }), | | 607 | + }) satisfies WaitOutcome; | 602 | 608 | const ownRun = Effect.gen(function* () { | 603 | 609 | while (true) { | 604 | 610 | const waited = yield* waitSlice(WAIT_SLICE_MS); | @@ -641,7 +647,13 @@ export const waitForRun = Effect.fn("cli.thread.waitForRun")(function* ( | 641 | 647 | if (after.sent) return outcome; | 642 | 648 | if (!waited.timedOut) { | 643 | 649 | if (result !== undefined && after.later.some((run) => run.runId === result.runId)) { | 644 | | - return { type: "ended", runId: result.runId, status: result.status } satisfies WaitOutcome; | | 650 | + // A run that work woke was not asked for the result; the run before it holds it. | | 651 | + return { | | 652 | + type: "ended", | | 653 | + runId: result.runId, | | 654 | + status: result.status, | | 655 | + ...(outcome.output === undefined ? {} : { output: outcome.output }), | | 656 | + } satisfies WaitOutcome; | 645 | 657 | } | 646 | 658 | if (!workOutlivedRun) return outcome; | 647 | 659 | if (after.later.some((run) => !TERMINAL_RUN_STATUSES.has(run.status))) continue; | @@ -684,7 +696,15 @@ const finishRun = Effect.fn("cli.thread.finishRun")(function* ( | 684 | 696 | ? null | 685 | 697 | : yield* runResultText(tools, input.threadId, runId, "error"); | 686 | 698 | if (input.json) { | 687 | | - yield* printJson({ threadId: input.threadId, runId, status, reply, error }); | | 699 | + const output = outcome.type === "ended" ? outcome.output : undefined; | | 700 | + yield* printJson({ | | 701 | + threadId: input.threadId, | | 702 | + runId, | | 703 | + status, | | 704 | + reply, | | 705 | + error, | | 706 | + ...(output === undefined ? {} : { output }), | | 707 | + }); | 688 | 708 | } else if (reply !== null) { | 689 | 709 | yield* Console.log(reply); | 690 | 710 | } | @@ -778,6 +798,25 @@ const waitFlag = Flag.Boolean("wait").pipe( | 778 | 798 | ), | 779 | 799 | Flag.withDefault(false), | 780 | 800 | ); | | 801 | +const outputSchemaFlag = Flag.String("output-schema").pipe( | | 802 | + Flag.withDescription( | | 803 | + "File holding a JSON Schema. The agent must return data matching it; --wait --json prints it as `output`.", | | 804 | + ), | | 805 | + Flag.optional, | | 806 | +); | | 807 | +const decodeOutputSchema = Schema.decodeUnknownEffect( | | 808 | + Schema.fromJsonString(Schema.Record(Schema.String, Schema.Json)), | | 809 | +); | | 810 | +const readOutputSchema = Effect.fn("cli.thread.readOutputSchema")(function* ( | | 811 | + path: Option.Option<string>, | | 812 | +) { | | 813 | + if (Option.isNone(path)) return {}; | | 814 | + const fileSystem = yield* FileSystem.FileSystem; | | 815 | + const usage = () => | | 816 | + new ThreadUsageError({ detail: `${path.value} does not hold a JSON Schema object.` }); | | 817 | + const text = yield* fileSystem.readFileString(path.value).pipe(Effect.mapError(usage)); | | 818 | + return { outputSchema: yield* decodeOutputSchema(text).pipe(Effect.mapError(usage)) }; | | 819 | +}); | 781 | 820 | const messageArgument = Argument.String("message").pipe( | 782 | 821 | Argument.withDescription("Message for the agent, or - to read it from stdin."), | 783 | 822 | ); | @@ -860,13 +899,15 @@ const threadStartCommand = Command.make("start", { | 860 | 899 | Flag.withDescription("Branch name for the new worktree."), | 861 | 900 | Flag.optional, | 862 | 901 | ), | | 902 | + outputSchema: outputSchemaFlag, | 863 | 903 | wait: waitFlag, | 864 | 904 | json: jsonFlag, | 865 | 905 | }).pipe( | 866 | 906 | Command.withDescription("Start a thread in a project and print its id."), | 867 | 907 | Command.withHandler((flags) => | 868 | 908 | Effect.gen(function* () { | 869 | 909 | const message = yield* readMessageArgument(flags.message); | | 910 | + const output = yield* readOutputSchema(flags.outputSchema); | 870 | 911 | const workspaceStrategy = yield* launchWorkspaceStrategy(flags); | 871 | 912 | const modelSelection = yield* launchModelSelection(flags); | 872 | 913 | const title = yield* decodeTitle( | @@ -882,6 +923,7 @@ const threadStartCommand = Command.make("start", { | 882 | 923 | projectId: project.id, | 883 | 924 | title, | 884 | 925 | message, | | 926 | + ...output, | 885 | 927 | ...(workspaceStrategy === undefined ? {} : { workspaceStrategy }), | 886 | 928 | ...(modelSelection === undefined ? {} : { modelSelection }), | 887 | 929 | }, | @@ -907,6 +949,7 @@ const threadSendCommand = Command.make("send", { | 907 | 949 | ...projectLocationFlags, | 908 | 950 | thread: threadArgument, | 909 | 951 | message: messageArgument, | | 952 | + outputSchema: outputSchemaFlag, | 910 | 953 | wait: waitFlag, | 911 | 954 | json: jsonFlag, | 912 | 955 | }).pipe( | @@ -917,14 +960,15 @@ const threadSendCommand = Command.make("send", { | 917 | 960 | Effect.gen(function* () { | 918 | 961 | const threadId = yield* decodeThreadId(flags.thread); | 919 | 962 | const message = yield* readMessageArgument(flags.message); | | 963 | + const output = yield* readOutputSchema(flags.outputSchema); | 920 | 964 | yield* withThreadTools( | 921 | 965 | flags, | 922 | 966 | Effect.fn("cli.thread.send")(function* (tools) { | 923 | 967 | // Work already running when this is sent is not this message's to wait for. | 924 | 968 | const followWakes = flags.wait ? yield* threadIsIdle(tools, threadId) : false; | 925 | 969 | const sent = yield* tools.call( | 926 | 970 | "t3_thread_send", | 927 | | - { threadId, message, mode: "queue" }, | | 971 | + { threadId, message, mode: "queue", ...output }, | 928 | 972 | OrchestratorMcpThreadSendResult, | 929 | 973 | ); | 930 | 974 | if (flags.wait) { | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/mcp/OrchestratorMcpService.ts +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -261,6 +261,8 @@ export function threadManagementFailure(error: unknown): OrchestratorMcpFailure | 261 | 261 | "thread_not_sendable", | 262 | 262 | `${error.message} Send with mode: "auto" or "queue" to start a new turn there.`, | 263 | 263 | ); | | 264 | + case "ThreadManagementRunOutputRejectedError": | | 265 | + return failure("invalid_request", error.message); | 264 | 266 | case "ThreadManagementThreadNotInterruptibleError": | 265 | 267 | return failure("thread_not_interruptible", error.message); | 266 | 268 | case "ThreadManagementProjectionLoadError": | @@ -3656,6 +3658,7 @@ const make = Effect.gen(function* () { | 3656 | 3658 | attachments: [], | 3657 | 3659 | mode, | 3658 | 3660 | ...(input.freshContext ? { freshContext: true } : {}), | | 3661 | + ...(input.outputSchema === undefined ? {} : { outputSchema: input.outputSchema }), | 3659 | 3662 | createdBy: "agent", | 3660 | 3663 | creationSource: "mcp", | 3661 | 3664 | }) | @@ -3691,6 +3694,7 @@ const make = Effect.gen(function* () { | 3691 | 3694 | Math.max(1, input.timeoutMs ?? DEFAULT_WAIT_TIMEOUT_MS), | 3692 | 3695 | ), | 3693 | 3696 | ...(input.untilDelegatedWorkSettled === true ? { untilDelegatedWorkSettled: true } : {}), | | 3697 | + ...(input.untilOutput === true ? { untilOutput: true } : {}), | 3694 | 3698 | }) | 3695 | 3699 | .pipe(Effect.mapError(threadManagementFailure)); | 3696 | 3700 | const status = result.run === null ? "idle" : runOutcome(result.run); | @@ -3727,6 +3731,7 @@ const make = Effect.gen(function* () { | 3727 | 3731 | : { | 3728 | 3732 | resultRun: { runId: result.resultRun.id, status: runOutcome(result.resultRun) }, | 3729 | 3733 | }), | | 3734 | + ...(result.run?.output === undefined ? {} : { output: result.run.output.value }), | 3730 | 3735 | } satisfies OrchestratorMcpThreadWaitResult; | 3731 | 3736 | }), | 3732 | 3737 | interruptThread: Effect.fn("OrchestratorMcpService.interruptThread")(function* (scope, input) { | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/mcp/toolkits/project/handlers.ts +14Lines changed: 14 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -16,6 +16,7 @@ import * as FileSystem from "effect/FileSystem"; | 16 | 16 | import * as Option from "effect/Option"; | 17 | 17 | import * as Schema from "effect/Schema"; | 18 | 18 | import { OrchestratorRuntimeModeAboveCeilingError } from "../../../orchestration-v2/Orchestrator.ts"; | | 19 | +import { outputRequestProblem } from "../../../orchestration-v2/runOutput.ts"; | 19 | 20 | import * as ThreadLaunch from "../../../orchestration-v2/ThreadLaunchService.ts"; | 20 | 21 | import * as ThreadMessageIntake from "../../../orchestration-v2/ThreadMessageIntake.ts"; | 21 | 22 | import * as Claims from "../../../orchestration-v2/AttachmentClaims.ts"; | @@ -224,6 +225,18 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, { | 224 | 225 | const threadId = ThreadId.make(commandId); | 225 | 226 | const messageId = MessageId.make(commandId); | 226 | 227 | const attachments = input.attachments ?? []; | | 228 | + const schemaProblem = | | 229 | + input.outputSchema === undefined | | 230 | + ? null | | 231 | + : outputRequestProblem(input.outputSchema, { | | 232 | + text: input.message ?? "", | | 233 | + attachments: input.attachments, | | 234 | + }); | | 235 | + if (schemaProblem !== null || (input.outputSchema !== undefined && !input.message)) | | 236 | + return yield* new OrchestratorMcpFailure({ | | 237 | + code: "invalid_request", | | 238 | + message: schemaProblem ?? "outputSchema needs a message that asks for the result.", | | 239 | + }); | 227 | 240 | if (attachments.some((attachment) => !Claims.attachmentIsPendingUpload(attachment))) | 228 | 241 | return yield* new OrchestratorMcpFailure({ | 229 | 242 | code: "invalid_request", | @@ -427,6 +440,7 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, { | 427 | 440 | ...(caller === undefined ? {} : { senderThreadId: caller.id }), | 428 | 441 | text: input.message ?? "", | 429 | 442 | attachments, | | 443 | + ...(input.outputSchema === undefined ? {} : { outputSchema: input.outputSchema }), | 430 | 444 | }, | 431 | 445 | }), | 432 | 446 | createdBy: "agent", | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/mcp/toolkits/project/tools.ts +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -156,6 +156,11 @@ const ThreadLaunchTool = Tool.make("t3_thread_launch", { | 156 | 156 | }), | 157 | 157 | ), | 158 | 158 | attachments: Schema.optional(Schema.Array(McpAttachmentInput).check(Schema.isMaxLength(8))), | | 159 | + outputSchema: Schema.optional( | | 160 | + Schema.Record(Schema.String, Schema.Json).annotate({ | | 161 | + description: "As in `t3_thread_send`. Needs `message`.", | | 162 | + }), | | 163 | + ), | 159 | 164 | clientRequestId: Schema.optional(OrchestratorMcpClientRequestId), | 160 | 165 | /** Set by a linked environment launching its agent's delegated task here; ignored otherwise. */ | 161 | 166 | delegatedFrom: Schema.optional(OrchestrationV2DelegatedFrom), | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/mcp/toolkits/thread/handlers.ts +26Lines changed: 26 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -283,6 +283,32 @@ export const layer = McpToolAccess.toLayer(ThreadToolkit, { | 283 | 283 | }), | 284 | 284 | ), | 285 | 285 | ), | | 286 | + t3_submit_output: McpToolAccess.actsAsCaller((input) => | | 287 | + Effect.gen(function* () { | | 288 | + const { scope, threads } = yield* readCaller(); | | 289 | + if (scope.thread === undefined) return yield* unavailable(); | | 290 | + return yield* threads | | 291 | + .submitRunOutput({ | | 292 | + threadId: scope.thread.threadId, | | 293 | + commandId: yield* newCommandId(), | | 294 | + json: input.json, | | 295 | + }) | | 296 | + .pipe( | | 297 | + Effect.mapError((error) => | | 298 | + error._tag === "ThreadManagementRunOutputRejectedError" | | 299 | + ? new OrchestratorMcpFailure({ | | 300 | + code: "invalid_request", | | 301 | + // The agent wrote the value, so it is told what in it did not fit. | | 302 | + message: | | 303 | + error.cause instanceof Error | | 304 | + ? `${error.message}\n${error.cause.message.slice(0, 2000)}` | | 305 | + : error.message, | | 306 | + }) | | 307 | + : unavailable(), | | 308 | + ), | | 309 | + ); | | 310 | + }), | | 311 | + ), | 286 | 312 | t3_queue: McpToolAccess.either( | 287 | 313 | (input) => input.action === "list" || input.action === "read", | 288 | 314 | McpToolAccess.reads((input) => | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/mcp/toolkits/thread/tools.ts +15-1Lines changed: 15 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -23,7 +23,7 @@ import { | 23 | 23 | } from "@t3tools/contracts"; | 24 | 24 | import * as SecureRandom from "effect/crypto/SecureRandom"; | 25 | 25 | import * as Schema from "effect/Schema"; | 26 | | -import { Tool, Toolkit } from "effect/ai"; | | 26 | +import { McpSchema, Tool, Toolkit } from "effect/ai"; | 27 | 27 |
| 28 | 28 | import * as ThreadSearch from "../../../orchestration-v2/ThreadSearch.ts"; | 29 | 29 | import * as ScheduledTaskService from "../../../scheduledTasks/ScheduledTaskService.ts"; | @@ -241,6 +241,19 @@ const ScheduledTaskRunTool = Tool.make("run_scheduled_task_now", { | 241 | 241 | .annotate(Tool.Destructive, true) | 242 | 242 | .annotate(Tool.OpenWorld, true); | 243 | 243 |
| | 244 | +const SubmitOutputTool = Tool.make("t3_submit_output", { | | 245 | + ...commandTool, | | 246 | + description: | | 247 | + "Return this turn's result as data, when the message asked for one. `json`: JSON text matching the schema it gave. A rejection says what did not fit; fix it and call again.", | | 248 | + // Stored on the run, which every client of the thread holds. | | 249 | + parameters: Schema.Struct({ json: Schema.String.check(Schema.isMaxLength(200000)) }), | | 250 | + success: Schema.Struct({ runId: RunId }), | | 251 | +}) | | 252 | + // It writes only the caller's own run, so a read-only or plan-mode agent can still answer. | | 253 | + .annotate(Tool.Readonly, true) | | 254 | + .annotate(Tool.Destructive, false) | | 255 | + .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients); | | 256 | + | 244 | 257 | export const ThreadToolkit = Toolkit.make( | 245 | 258 | ScheduledTaskRunTool, | 246 | 259 | ThreadSearchTool, | @@ -249,4 +262,5 @@ export const ThreadToolkit = Toolkit.make( | 249 | 262 | ThreadOrganizeTool, | 250 | 263 | PendingRequestTool, | 251 | 264 | QueueTool, | | 265 | + SubmitOutputTool, | 252 | 266 | ); |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2537,6 +2537,7 @@ export const CLAUDE_READ_ONLY_T3_MCP_ALLOWED_TOOLS: ReadonlyArray<string> = [ | 2537 | 2537 | "mcp__t3-code__html_preview", | 2538 | 2538 | "mcp__t3-code__html_render", | 2539 | 2539 | "mcp__t3-code__send_to_user", | | 2540 | + "mcp__t3-code__t3_submit_output", | 2540 | 2541 | ]; | 2541 | 2542 |
| 2542 | 2543 | // request_secret can wait an hour; exceed Claude's default 60 s timeout. | |
|