Accept complete thread reference inputs
Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
184d4e0715
commit
bd72a1e003
5 files changed
+76
-30
No files matched your search
@@ -19,6 +19,7 @@ import { ProjectionSnapshotQuery } from "../../../orchestration/Services/Project
|
||||
import {
|
||||
buildThreadReferencePage,
|
||||
hasUserThreadReference,
|
||||
normalizeThreadReferenceThreadId,
|
||||
threadRead,
|
||||
ThreadReferenceToolkitHandlersLive,
|
||||
} from "./handlers.ts";
|
||||
@@ -208,19 +209,44 @@ it("only authorizes user-supplied references from the invoking environment", ()
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("normalizes model-facing thread reference inputs", () => {
|
||||
expect(normalizeThreadReferenceThreadId(thread.id, environmentId)).toBe(thread.id);
|
||||
expect(
|
||||
normalizeThreadReferenceThreadId(ThreadId.make(`${environmentId}/${thread.id}`), environmentId),
|
||||
).toBe(thread.id);
|
||||
expect(
|
||||
normalizeThreadReferenceThreadId(
|
||||
ThreadId.make(`t3-thread:///${environmentId}/${thread.id}`),
|
||||
environmentId,
|
||||
),
|
||||
).toBe(thread.id);
|
||||
expect(
|
||||
normalizeThreadReferenceThreadId(
|
||||
ThreadId.make(`t3-thread:///environment-2/${thread.id}`),
|
||||
environmentId,
|
||||
),
|
||||
).toBe(`t3-thread:///environment-2/${thread.id}`);
|
||||
});
|
||||
|
||||
it.effect("reads a referenced thread through the MCP toolkit", () =>
|
||||
Effect.gen(function* () {
|
||||
const server = yield* McpServer.McpServer;
|
||||
const result = yield* server.callTool({
|
||||
name: "thread_read",
|
||||
arguments: { threadId: thread.id },
|
||||
});
|
||||
expect(result.isError).toBe(false);
|
||||
expect(result.structuredContent).toMatchObject({
|
||||
threadId: thread.id,
|
||||
title: thread.title,
|
||||
totalMessages: 2,
|
||||
});
|
||||
for (const threadId of [
|
||||
thread.id,
|
||||
`${environmentId}/${thread.id}`,
|
||||
`t3-thread:///${environmentId}/${thread.id}`,
|
||||
]) {
|
||||
const result = yield* server.callTool({
|
||||
name: "thread_read",
|
||||
arguments: { threadId },
|
||||
});
|
||||
expect(result.isError).toBe(false);
|
||||
expect(result.structuredContent).toMatchObject({
|
||||
threadId: thread.id,
|
||||
title: thread.title,
|
||||
totalMessages: 2,
|
||||
});
|
||||
}
|
||||
}).pipe(
|
||||
Effect.provideService(McpInvocationContext.McpInvocationContext, invocation),
|
||||
Effect.provideService(McpSchema.McpServerClient, mcpServerClient),
|
||||
|
||||
@@ -8,9 +8,10 @@ import {
|
||||
ThreadReferenceUnavailableError,
|
||||
type EnvironmentId,
|
||||
type OrchestrationThread,
|
||||
type ThreadId,
|
||||
ThreadId,
|
||||
} from "@t3tools/contracts";
|
||||
import { collectComposerThreadReferences } from "@t3tools/shared/composerInlineTokens";
|
||||
import { parseComposerThreadLink } from "@t3tools/shared/composerTrigger";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Option from "effect/Option";
|
||||
|
||||
@@ -116,38 +117,56 @@ export function hasUserThreadReference(
|
||||
);
|
||||
}
|
||||
|
||||
export function normalizeThreadReferenceThreadId(
|
||||
input: ThreadId,
|
||||
environmentId: EnvironmentId,
|
||||
): ThreadId {
|
||||
const parsedLink = parseComposerThreadLink(input);
|
||||
if (parsedLink?.environmentId === environmentId) {
|
||||
return ThreadId.make(parsedLink.threadId);
|
||||
}
|
||||
|
||||
const environmentPrefix = `${environmentId}/`;
|
||||
if (input.startsWith(environmentPrefix)) {
|
||||
const threadId = input.slice(environmentPrefix.length);
|
||||
if (threadId && !threadId.includes("/")) {
|
||||
try {
|
||||
return ThreadId.make(decodeURIComponent(threadId));
|
||||
} catch {
|
||||
return input;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return input;
|
||||
}
|
||||
|
||||
export const threadRead = Effect.fn("ThreadReferenceToolkit.threadRead")(function* (
|
||||
input: ThreadReferenceReadInput,
|
||||
) {
|
||||
const invocation = yield* McpInvocationContext.McpInvocationContext;
|
||||
const threadId = normalizeThreadReferenceThreadId(input.threadId, invocation.environmentId);
|
||||
const normalizedInput = { ...input, threadId };
|
||||
if (!invocation.capabilities.has("thread-reference")) {
|
||||
return yield* new ThreadReferenceUnavailableError({ threadId: input.threadId });
|
||||
return yield* new ThreadReferenceUnavailableError({ threadId });
|
||||
}
|
||||
const snapshotQuery = yield* ProjectionSnapshotQuery;
|
||||
const sourceThreadOption = yield* snapshotQuery
|
||||
.getThreadDetailById(invocation.threadId)
|
||||
.pipe(
|
||||
Effect.mapError(
|
||||
(cause) => new ThreadReferenceUnavailableError({ threadId: input.threadId, cause }),
|
||||
),
|
||||
);
|
||||
.pipe(Effect.mapError((cause) => new ThreadReferenceUnavailableError({ threadId, cause })));
|
||||
if (
|
||||
Option.isNone(sourceThreadOption) ||
|
||||
!hasUserThreadReference(sourceThreadOption.value, invocation.environmentId, input.threadId)
|
||||
!hasUserThreadReference(sourceThreadOption.value, invocation.environmentId, threadId)
|
||||
) {
|
||||
return yield* new ThreadReferenceUnavailableError({ threadId: input.threadId });
|
||||
return yield* new ThreadReferenceUnavailableError({ threadId });
|
||||
}
|
||||
const threadOption = yield* snapshotQuery
|
||||
.getThreadDetailById(input.threadId)
|
||||
.pipe(
|
||||
Effect.mapError(
|
||||
(cause) => new ThreadReferenceUnavailableError({ threadId: input.threadId, cause }),
|
||||
),
|
||||
);
|
||||
.getThreadDetailById(threadId)
|
||||
.pipe(Effect.mapError((cause) => new ThreadReferenceUnavailableError({ threadId, cause })));
|
||||
if (Option.isNone(threadOption)) {
|
||||
return yield* new ThreadReferenceNotFoundError({ threadId: input.threadId });
|
||||
return yield* new ThreadReferenceNotFoundError({ threadId });
|
||||
}
|
||||
const page = buildThreadReferencePage(threadOption.value, input);
|
||||
const page = buildThreadReferencePage(threadOption.value, normalizedInput);
|
||||
if ("_tag" in page) {
|
||||
return yield* page;
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ import { ProjectionSnapshotQuery } from "../../../orchestration/Services/Project
|
||||
|
||||
export const ThreadReadTool = Tool.make("thread_read", {
|
||||
description:
|
||||
"Read a T3 Code chat thread referenced by a t3-thread link in the user's message. Pass the threadId from that link. The transcript is paginated; when nextCursor is non-null, call thread_read again with that cursor to continue. Do not call this for unrelated threads that the user did not reference.",
|
||||
"Read a T3 Code chat thread referenced by a t3-thread link in the user's message. For t3-thread:///ENVIRONMENT_ID/THREAD_ID, pass the final THREAD_ID as threadId. The full t3-thread link and ENVIRONMENT_ID/THREAD_ID are also accepted. The transcript is paginated; when nextCursor is non-null, call thread_read again with that cursor to continue. Do not call this for unrelated threads that the user did not reference.",
|
||||
parameters: ThreadReferenceReadInput,
|
||||
success: ThreadReferenceReadResult,
|
||||
failure: ThreadReferenceReadError,
|
||||
|
||||
@@ -15,7 +15,7 @@ const T3_CODE_THREAD_REFERENCE_INSTRUCTIONS = `
|
||||
|
||||
## T3 Code thread references
|
||||
|
||||
The user may reference another chat with a \`t3-thread\` link. Do not treat the link as a web URL and do not assume its contents. Use the read-only \`thread_read\` tool from the \`t3-code\` MCP server with the referenced thread id. Follow \`nextCursor\` until you have read as much of the transcript as the task requires.
|
||||
The user may reference another chat with a \`t3-thread\` link. Do not treat the link as a web URL and do not assume its contents. Use the read-only \`thread_read\` tool from the \`t3-code\` MCP server. For \`t3-thread:///ENVIRONMENT_ID/THREAD_ID\`, pass the final \`THREAD_ID\` path segment as \`threadId\`; the tool also accepts the full link. Follow \`nextCursor\` until you have read as much of the transcript as the task requires.
|
||||
`;
|
||||
|
||||
export const CODEX_PLAN_MODE_DEVELOPER_INSTRUCTIONS = `<collaboration_mode># Plan Mode (Conversational)
|
||||
|
||||
@@ -22,7 +22,8 @@ export type ThreadReferenceCursor = typeof ThreadReferenceCursor.Type;
|
||||
|
||||
export const ThreadReferenceReadInput = Schema.Struct({
|
||||
threadId: ThreadId.annotate({
|
||||
description: "Thread id from a t3-thread reference in the user's message.",
|
||||
description:
|
||||
"Referenced thread id. Prefer the final THREAD_ID path segment; full t3-thread:///ENVIRONMENT_ID/THREAD_ID links and ENVIRONMENT_ID/THREAD_ID are also accepted.",
|
||||
}),
|
||||
cursor: Schema.optional(ThreadReferenceCursor).annotate({
|
||||
description: "nextCursor from the previous page. Omit for the first page.",
|
||||
|
||||
Reference in new issue
Block a user