Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/server/src/environment/ServerEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ export const make = Effect.gen(function* () {
...(desktopAppUpdate ? { desktopAppUpdate: true, desktopAppChannelSwitch: true } : {}),
serverBrowser: true,
peerLinks: true,
threadInbox: true,
mcpModeLimitHeader: true,
linkFence: true,
mcpApprovalCodes: true,
Expand Down
8 changes: 8 additions & 0 deletions apps/server/src/mcp/OrchestratorMcpService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,7 @@ function listItemFromShell(
nowMs: number,
): OrchestratorMcpThreadListItem {
return {
...(shell.inbox === undefined ? {} : { inbox: shell.inbox }),
threadId: shell.id,
projectId: shell.projectId,
title: shell.title,
Expand Down Expand Up @@ -981,6 +982,7 @@ function threadDetail(
const latest = ThreadManagementService.latestRun(projection);
const active = ThreadManagementService.latestActiveRun(projection);
return {
...(projection.thread.inbox === undefined ? {} : { inbox: projection.thread.inbox }),
threadId: projection.thread.id,
projectId: projection.thread.projectId,
title: projection.thread.title,
Expand Down Expand Up @@ -3094,6 +3096,12 @@ const make = Effect.gen(function* () {
const created = yield* threadManagement
.dispatch({
type: "thread.create",
inboxOrigin: { kind: "agent", id: parent.thread.id },
...(request.inboxRouting !== undefined
? { inboxRouting: request.inboxRouting }
: parent.thread.inbox === undefined
? {}
: { inboxRouting: parent.thread.inbox }),
createdBy: "agent",
creationSource: "mcp",
commandId,
Expand Down
10 changes: 9 additions & 1 deletion apps/server/src/mcp/ThreadMetadataMcpService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ function metadataCommand(input: {
readonly update: ThreadMetadataMcpUpdateInput;
}): Extract<OrchestrationV2Command, { readonly type: "thread.metadata.update" }> {
switch (input.update.action) {
case "route":
return {
type: "thread.metadata.update",
commandId: input.commandId,
threadId: input.threadId,
inboxRouting: input.update.inboxRouting!,
};
case "rename":
return {
type: "thread.metadata.update",
Expand All @@ -88,10 +95,11 @@ function resultFromThread(input: {
readonly sequence: number;
readonly thread: Pick<
OrchestrationV2AppThread,
"id" | "title" | "titleRegeneration" | "linkedPullRequest" | "updatedAt"
"id" | "title" | "titleRegeneration" | "linkedPullRequest" | "updatedAt" | "inbox"
>;
}): ThreadMetadataMcpUpdateResult {
return {
...(input.thread.inbox === undefined ? {} : { inbox: input.thread.inbox }),
threadId: input.thread.id,
action: input.action,
commandId: input.commandId,
Expand Down
2 changes: 1 addition & 1 deletion apps/server/src/mcp/toolkits/orchestrator/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ const ThreadReadTool = Tool.make("t3_thread_read", {

const ThreadUpdateTool = Tool.make("t3_thread_update", {
description:
"Rename a thread (`action:'rename'` with `title`) or regenerate its title. Omit `threadId` for this thread. Workspace and branch cannot change here.",
"Route a thread with action:'route' and inboxRouting (owner, assignee, audience, attentionKind), or rename a thread (`action:'rename'` with `title`) or regenerate its title. Omit `threadId` for this thread. Workspace and branch cannot change here.",
// Keeps the input's action/field check, which rebuilding the struct would drop.
parameters: ThreadMetadataMcpUpdateInput.mapFields(
Struct.assign({ environmentId: OrchestratorMcpEnvironmentTarget }),
Expand Down
6 changes: 6 additions & 0 deletions apps/server/src/mcp/toolkits/project/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,12 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, {
});
}
const result = yield* ThreadMessageIntake.launchThread({
inboxOrigin: { kind: "agent", id: caller?.id ?? "agent" },
...(input.inboxRouting !== undefined
? { inboxRouting: input.inboxRouting }
: caller?.inbox === undefined
? {}
: { inboxRouting: caller.inbox }),
commandId,
threadId,
projectId,
Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/mcp/toolkits/project/tools.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { McpAttachmentInput } from "../attachment/input.ts";
import {
clampedPositiveInt,
ThreadInboxRoutingPatch,
NonNegativeInt,
ModelSelection,
TrimmedNonEmptyString,
Expand Down Expand Up @@ -139,6 +140,7 @@ const ThreadLaunchTool = Tool.make("t3_thread_launch", {
"No project: a new Scratch folder. Not with `projectId` or `workspaceStrategy`.",
}),
),
inboxRouting: Schema.optional(ThreadInboxRoutingPatch),
title: TrimmedNonEmptyString,
modelSelection: Schema.optional(ModelSelection),
runtimeMode: Schema.optional(RuntimeMode),
Expand Down
27 changes: 27 additions & 0 deletions apps/server/src/orchestration-v2/Orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import type {
OrchestrationV2ThreadHistoryPage,
ProjectId,
} from "@t3tools/contracts";
import { makeThreadInbox, routeThreadInbox } from "@t3tools/contracts";
import { ServerEnvironmentIdentity } from "../environment/ServerEnvironment.ts";
import {
latestExecutedRun,
latestRootProviderFailure,
Expand Down Expand Up @@ -3778,6 +3780,23 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
const now = yield* DateTime.now;
const emitEvent = emit(events, command);
const thread: OrchestrationV2AppThread = {
...(command.inboxOrigin === undefined &&
command.inboxRouting === undefined &&
command.createdBy !== "agent"
? {}
: {
inbox: makeThreadInbox({
actor:
command.inboxOrigin ??
(command.createdBy === "agent" ? { kind: "agent", id: "agent" } : null),
...(command.inboxRouting === undefined ? {} : { routing: command.inboxRouting }),
executorEnvironmentId: yield* Effect.serviceOption(ServerEnvironmentIdentity).pipe(
Effect.flatMap((identity) =>
Option.isSome(identity) ? identity.value.getEnvironmentId : Effect.succeed(null),
),
),
}),
}),
createdBy: command.createdBy,
creationSource: command.creationSource,
id: command.threadId,
Expand Down Expand Up @@ -4850,6 +4869,14 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
};
return {
...thread,
...(command.inboxRouting === undefined
? {}
: {
inbox: routeThreadInbox(
thread.inbox ?? makeThreadInbox({ actor: null }),
command.inboxRouting,
),
}),
...(command.title === undefined ? {} : { title: command.title }),
...(command.limitRecovery === undefined ? {} : { limitRecovery }),
...(command.limitRecovery !== undefined &&
Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/orchestration-v2/ProjectionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1785,6 +1785,7 @@ export function threadShellFromProjection(
});
return {
createdBy: projection.thread.createdBy,
...(projection.thread.inbox === undefined ? {} : { inbox: projection.thread.inbox }),
creationSource: projection.thread.creationSource,
id: projection.thread.id,
projectId: projection.thread.projectId,
Expand Down Expand Up @@ -2124,6 +2125,7 @@ function shellFromState(input: {
...(input.state.thread.historyOrigin === undefined
? {}
: { historyOrigin: input.state.thread.historyOrigin }),
...(input.state.thread.inbox === undefined ? {} : { inbox: input.state.thread.inbox }),
...(input.state.thread.linkOrigin === undefined
? {}
: { linkOrigin: input.state.thread.linkOrigin }),
Expand Down
9 changes: 9 additions & 0 deletions apps/server/src/orchestration-v2/SubagentProjection.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { assert, it } from "@effect/vitest";
import {
makeThreadInbox,
matchesInboxView,
CommandId,
EnvironmentId,
type ModelSelection,
Expand Down Expand Up @@ -89,6 +91,7 @@ function makeParentThread(): OrchestrationV2AppThread {
it("resets a subagent child's snooze, title job and remote delegation but keeps its workspace", () => {
const parentThread: OrchestrationV2AppThread = {
...makeParentThread(),
inbox: makeThreadInbox({ actor: { kind: "human", id: "jeff" } }),
titleRegeneration: { requestId: CommandId.make("command:parent-title"), startedAt: snoozedAt },
delegatedFrom: {
environmentId: EnvironmentId.make("linked-environment"),
Expand All @@ -111,6 +114,12 @@ it("resets a subagent child's snooze, title job and remote delegation but keeps
creationSource: "provider",
});

assert.equal(childThread.inbox?.owner, "jeff");
assert.deepEqual(childThread.inbox?.requestedBy, parentThread.inbox?.requestedBy);
assert.isFalse(matchesInboxView(childThread.inbox, "jeff"));
assert.isFalse(matchesInboxView(childThread.inbox, "kameron"));
assert.isTrue(matchesInboxView(childThread.inbox, "agents"));
assert.isTrue(matchesInboxView(childThread.inbox, "all"));
assert.isNull(childThread.snoozedUntil);
assert.isNull(childThread.snoozedAt);
assert.isNull(childThread.titleRegeneration);
Expand Down
32 changes: 32 additions & 0 deletions apps/server/src/orchestration-v2/ThreadLaunchService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3308,3 +3308,35 @@ it.effect("replays an accepted send after the project disables the instance", ()
}
}).pipe(Effect.provide(Layer.mergeAll(harness.layer, layerFiles)));
});

it.effect(
"persists founder routing in projections and shells and retains it on launch replay",
() => {
const harness = makeHarness();
return Effect.gen(function* () {
const launches = yield* ThreadLaunch.ThreadLaunchService;
const threads = yield* ThreadManagement.ThreadManagementService;
const input = {
...launchInput({ command: "inbox-launch", thread: "inbox-thread" }),
inboxOrigin: { kind: "human" as const, id: "jeff" as const },
};
const launched = yield* launches.launch(input);
const original = (yield* threads.getThreadProjection(launched.threadId)).thread.inbox;
assert.equal(original?.owner, "jeff");
assert.deepEqual((yield* threads.getThreadShell(launched.threadId))?.inbox, original);
yield* threads.dispatch({
type: "thread.metadata.update",
commandId: CommandId.make("route-inbox"),
threadId: launched.threadId,
inboxRouting: { assignee: { kind: "human", id: "kameron" }, attentionKind: "FYI" },
});
yield* launches.launch(input);
const routed = (yield* threads.getThreadProjection(launched.threadId)).thread.inbox;
assert.equal(routed?.owner, "kameron");
assert.equal(routed?.attentionKind, "FYI");
assert.deepEqual(routed?.origin, original?.origin);
assert.deepEqual(routed?.requestedBy, original?.requestedBy);
assert.deepEqual((yield* threads.getThreadShell(launched.threadId))?.inbox, routed);
}).pipe(Effect.provide(harness.layer));
},
);
6 changes: 6 additions & 0 deletions apps/server/src/orchestration-v2/ThreadLaunchService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import * as TerminalManager from "../terminal/Manager.ts";
import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
import {
CommandId,
type InboxActor,
type ThreadInboxRoutingPatch,
type ChatAttachment,
type MessageId,
type ModelSelection,
Expand Down Expand Up @@ -88,6 +90,8 @@ export interface ThreadLaunchInitialMessage {

export interface ThreadLaunchInput {
readonly commandId: CommandId;
readonly inboxOrigin?: InboxActor;
readonly inboxRouting?: ThreadInboxRoutingPatch;
readonly threadId?: ThreadId;
readonly reuseExistingThread?: boolean;
readonly projectId: ProjectId;
Expand Down Expand Up @@ -1321,6 +1325,8 @@ const make = Effect.gen(function* () {
...(initialMessage === undefined
? {}
: { initialMessage: { ...initialMessage, workspaceStrategy } }),
...(input.inboxOrigin === undefined ? {} : { inboxOrigin: input.inboxOrigin }),
...(input.inboxRouting === undefined ? {} : { inboxRouting: input.inboxRouting }),
});
const claimed = yield* claimDispatch.pipe(
Effect.mapError(
Expand Down
10 changes: 9 additions & 1 deletion apps/server/src/serverSettings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1823,6 +1823,7 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("server settings"
openai: SECRET_REDACTED,
xai: SECRET_REDACTED,
openrouter: "",
assemblyai: "",
});
assert.equal(legacy.sourceControlHosts.bitbucket?.accessToken, SECRET_REDACTED);
assert.equal(legacy.usageLimitSources[hub]?.managementKey, SECRET_REDACTED);
Expand Down Expand Up @@ -1853,6 +1854,7 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("server settings"
openai: usageReference,
xai: "literal-fixture",
openrouter: "",
assemblyai: "",
});
assert.deepEqual(echoed.easDeviceHosts[0]?.accessToken, easReference);
assert.deepEqual(echoed.simlockDeviceHosts[0]?.agentToken, simlockReference);
Expand Down Expand Up @@ -1938,7 +1940,12 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("server settings"
"ghe.acme.test": work,
});
assert.equal(removed.usageLimitSources[hub]?.managementKey, "");
assert.deepEqual(removed.cloudSpeechCredentials, { openai: "", xai: work, openrouter: "" });
assert.deepEqual(removed.cloudSpeechCredentials, {
openai: "",
xai: work,
openrouter: "",
assemblyai: "",
});
assert.equal(removed.easDeviceHosts[0]?.accessToken, "");
assert.deepEqual(Object.keys(removed.secretProviders), ["work"]);
}).pipe(Effect.provide(layerServerSettingsWithSecrets())),
Expand All @@ -1951,6 +1958,7 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("server settings"
"cloudSpeechCredentials.openai",
"cloudSpeechCredentials.xai",
"cloudSpeechCredentials.openrouter",
"cloudSpeechCredentials.assemblyai",
"easDeviceHosts.[].accessToken",
"simlockDeviceHosts.[].agentToken",
"usageLimitSources.*.managementKey",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,12 @@ it.effect("reads a collected reference again once it is invalidated", () =>
resolver,
Effect.succeed({
sourceControlHosts: hosts,
cloudSpeechCredentials: { openai: source("voice#key"), xai: "", openrouter: "" },
cloudSpeechCredentials: {
openai: source("voice#key"),
xai: "",
openrouter: "",
assemblyai: "",
},
usageLimitSources,
textGenerators: {},
easDeviceHosts,
Expand Down
54 changes: 53 additions & 1 deletion apps/server/src/speech/CloudSpeech.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ it.effect.each(["openai", "xai"] as const)("sends only to %s using its own saved
yield* service.setKey(provider === "openai" ? "xai" : "openai", Redacted.make("other"));
expect((yield* Effect.flip(transcribe()))._tag).toBe("CloudSpeechError");
yield* service.setKey(provider, Redacted.make("chosen"));
expect(yield* service.configured).toEqual({ openai: true, xai: true, openrouter: false });
expect(yield* service.configured).toEqual({
openai: true,
xai: true,
openrouter: false,
assemblyai: false,
});
yield* Effect.flip(transcribe(new Uint8Array(3)));
expect(upstream).not.toHaveBeenCalled();
expect(yield* transcribe()).toBe("hello");
Expand Down Expand Up @@ -198,3 +203,50 @@ it.effect("removing a key while a catalog read is pending prevents the upload",
expect(exit._tag).toBe("Failure");
}),
);

it.effect(
"AssemblyAI sends WAV and config only to the Sync API and bounds clips before upload",
() =>
Effect.gen(function* () {
const upstream = vi.fn<typeof fetch>(async () => Response.json({ text: "hello T3" }));
const service = yield* CloudSpeech.CloudSpeech.pipe(
Effect.provide(
CloudSpeech.layer.pipe(
Layer.provide(FetchHttpClient.layer),
Layer.provide(ServerSettings.ServerSettingsService.layerTest()),
Layer.provide(Layer.mock(SecretResolver.SecretResolver)({})),
),
),
);
yield* service.setKey("assemblyai", Redacted.make("assembly-fixture"));
const transcribe = (samples: number) =>
service
.transcribe("assemblyai", new Uint8Array(new Float32Array(samples).buffer), {
...DEFAULT_SPEECH_TRANSCRIPTION_OPTIONS,
speechLanguage: "en",
speechCustomWords: [{ term: "Closura", aliases: [] }],
})
.pipe(Effect.provideService(FetchHttpClient.Fetch, upstream));
expect(yield* transcribe(1600)).toBe("hello T3");
const [url, init] = upstream.mock.calls[0]!;
expect(String(url)).toBe("https://sync.assemblyai.com/v1/transcribe");
expect(init?.redirect).toBe("error");
const headers = new Headers(init?.headers);
expect(headers.get("authorization")).toBe("assembly-fixture");
expect(headers.get("X-AAI-Model")).toBe("universal-3-5-pro");
const form = init?.body as FormData;
expect(JSON.parse(String(form.get("config")))).toEqual({
language_code: "en",
word_boost: ["Closura"],
});
expect((form.get("audio") as Blob).type).toBe("audio/wav");
expect(yield* Effect.flip(transcribe(1_920_001))).toMatchObject({ reason: "audio-duration" });
expect(yield* Effect.flip(transcribe(100))).toMatchObject({ reason: "audio-duration" });
expect(upstream).toHaveBeenCalledTimes(1);
upstream.mockResolvedValueOnce(new Response("private diagnostic", { status: 401 }));
expect(yield* Effect.flip(transcribe(1600))).toMatchObject({ reason: "rejected-key" });
yield* service.setKey("assemblyai", null);
expect((yield* service.configured).assemblyai).toBe(false);
expect(yield* Effect.flip(transcribe(1600))).toMatchObject({ reason: "missing-key" });
}),
);
Loading
Loading