Skip to content
Draft
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
47 changes: 47 additions & 0 deletions js/plugins/anthropic/src/parts/input_json_part.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {
SupportedPart,
SupportedPartWhat,
SupportedPartWhen,
throwErrorWrongTypeForAbility,
} from './part';

const ID_DELTA = 'input_json_delta';

export const InputJsonPart: SupportedPart = {
abilities: [
{
id: ID_DELTA,
when: SupportedPartWhen.StreamDelta,
what: SupportedPartWhat.ContentBlock,
func: (delta) => {
if (delta.type !== ID_DELTA) {
throwErrorWrongTypeForAbility(
ID_DELTA,
SupportedPartWhen.StreamDelta,
SupportedPartWhat.ContentBlock
);
}

throw new Error(
`Anthropic streaming tool input (${ID_DELTA}) is not yet supported. Please disable streaming or upgrade this plugin.`
);
},
},
],
};
46 changes: 46 additions & 0 deletions js/plugins/anthropic/src/parts/mcp_tool_use.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { unsupportedBetaServerToolError } from '../utils';
import {
SupportedPart,
SupportedPartWhat,
SupportedPartWhen,
throwErrorWrongTypeForAbility,
} from './part';

const ID = 'mcp_tool_use';

export const McpToolUsePart: SupportedPart = {
abilities: [
{
id: ID,
when: SupportedPartWhen.NonStream,
what: SupportedPartWhat.ContentBlock,
func: (contentBlock) => {
if (contentBlock.type !== ID) {
throwErrorWrongTypeForAbility(
ID,
SupportedPartWhen.NonStream,
SupportedPartWhat.ContentBlock
);
}

throw new Error(unsupportedBetaServerToolError(contentBlock.type));
},
},
],
};
81 changes: 81 additions & 0 deletions js/plugins/anthropic/src/parts/part.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {
ContentBlock,
RawContentBlockDelta,
} from '@anthropic-ai/sdk/resources';
import {
BetaContentBlock,
BetaRawContentBlockDelta,
BetaRawMessageStreamEvent,
} from '@anthropic-ai/sdk/resources/beta.js';
import { MessageStreamEvent } from '@anthropic-ai/sdk/resources/messages.js';

export interface SupportedPart {
abilities: Ability[];
}

export interface Ability {
id: string;
when: (typeof SupportedPartWhen)[keyof typeof SupportedPartWhen];
what: (typeof SupportedPartWhat)[keyof typeof SupportedPartWhat];
func: (
chunk:
| MessageStreamEvent
| BetaRawMessageStreamEvent
| ContentBlock
| BetaContentBlock
| RawContentBlockDelta
| BetaRawContentBlockDelta
) => any;
}

export const SupportedPartWhen = {
StreamStart: 'stream_start' as const,
StreamDelta: 'stream_delta' as const,
StreamEnd: 'stream_end' as const,
NonStream: 'non_stream' as const,
};

export const SupportedPartWhat = {
ContentBlock: 'content_block' as const,
};

export function throwErrorWrongTypeForAbility(
partId: string,
chunk: (typeof SupportedPartWhen)[keyof typeof SupportedPartWhen],
what: (typeof SupportedPartWhat)[keyof typeof SupportedPartWhat]
): never {
switch (chunk) {
case SupportedPartWhen.StreamStart:
throw new Error(
`Part '${partId}' is not supported for stream start in ${what}`
);
case SupportedPartWhen.StreamDelta:
throw new Error(
`Part '${partId}' is not supported for stream delta in ${what}`
);
case SupportedPartWhen.StreamEnd:
throw new Error(
`Part '${partId}' is not supported for stream end in ${what}`
);
case SupportedPartWhen.NonStream:
throw new Error(
`Part '${partId}' is not supported for non stream in ${what}`
);
}
}
62 changes: 62 additions & 0 deletions js/plugins/anthropic/src/parts/redacted_thinking_part.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {
SupportedPart,
SupportedPartWhat,
SupportedPartWhen,
throwErrorWrongTypeForAbility,
} from './part';

const ID = 'redacted_thinking';

export const RedactedThinkingPart: SupportedPart = {
abilities: [
{
id: ID,
when: SupportedPartWhen.NonStream,
what: SupportedPartWhat.ContentBlock,
func: (contentBlock) => {
if (contentBlock.type !== ID) {
throwErrorWrongTypeForAbility(
ID,
SupportedPartWhen.NonStream,
SupportedPartWhat.ContentBlock
);
}

return { custom: { redactedThinking: contentBlock.data } };
},
},

{
id: ID,
when: SupportedPartWhen.StreamStart,
what: SupportedPartWhat.ContentBlock,
func: (contentBlock) => {
if (contentBlock.type !== ID) {
throwErrorWrongTypeForAbility(
ID,
SupportedPartWhen.StreamStart,
SupportedPartWhat.ContentBlock
);
}

return { custom: { redactedThinking: contentBlock.data } };
},
},
],
};
85 changes: 85 additions & 0 deletions js/plugins/anthropic/src/parts/server_tool_use_part.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {
SupportedPart,
SupportedPartWhat,
SupportedPartWhen,
throwErrorWrongTypeForAbility,
} from './part';

const ID = 'server_tool_use';

export const ServerToolUsePart: SupportedPart = {
abilities: [
{
id: ID,
when: SupportedPartWhen.NonStream,
what: SupportedPartWhat.ContentBlock,
func: (contentBlock) => {
if (contentBlock.type !== ID) {
throwErrorWrongTypeForAbility(
ID,
SupportedPartWhen.NonStream,
SupportedPartWhat.ContentBlock
);
}

const baseName = contentBlock.name ?? 'unknown_tool';
const serverToolName =
'server_name' in contentBlock && contentBlock.server_name
? `${contentBlock.server_name}/${baseName}`
: baseName;

return {
text: `[Anthropic server tool ${serverToolName}] input: ${JSON.stringify(contentBlock.input)}`,
custom: {
anthropicServerToolUse: {
id: contentBlock.id,
name: serverToolName,
input: contentBlock.input,
},
},
};
},
},

{
id: ID,
when: SupportedPartWhen.StreamStart,
what: SupportedPartWhat.ContentBlock,
func: (contentBlock) => {
if (contentBlock.type !== ID) {
throwErrorWrongTypeForAbility(
ID,
SupportedPartWhen.StreamStart,
SupportedPartWhat.ContentBlock
);
}
return {
text: `[Anthropic server tool ${contentBlock.name}] input: ${JSON.stringify(contentBlock.input)}`,
custom: {
anthropicServerToolUse: {
id: contentBlock.id,
name: contentBlock.name ?? 'unknown_tool',
input: contentBlock.input,
},
},
};
},
},
],
};
Loading