Skip to content
Open
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
12 changes: 1 addition & 11 deletions packages/bedrock-sdk/src/core/streaming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { fromBase64, toBase64 } from '@smithy/util-base64';
import { streamCollector } from '@smithy/fetch-http-handler';
import { EventStreamSerdeContext, SerdeContext } from '@smithy/types';
import { Stream as CoreStream, ServerSentEvent } from '@anthropic-ai/sdk/streaming';
import { AnthropicError } from '@anthropic-ai/sdk/error';
import { AnthropicError, isAbortError } from '@anthropic-ai/sdk/error';
import { APIError, BaseAnthropic } from '@anthropic-ai/sdk';
import { de_ResponseStream } from '../AWS_restJson1';
import { ReadableStreamToAsyncIterable } from '../internal/shims';
Expand Down Expand Up @@ -105,13 +105,3 @@ export class Stream<Item> extends CoreStream<Item> {
}
}

function isAbortError(err: unknown) {
return (
typeof err === 'object' &&
err !== null &&
// Spec-compliant fetch implementations
(('name' in err && (err as any).name === 'AbortError') ||
// Expo fetch
('message' in err && String((err as any).message).includes('FetchRequestCanceledException')))
);
}
1 change: 1 addition & 0 deletions src/core/error.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

import { castToError } from '../internal/errors';
export { isAbortError } from '../internal/errors';
import type { ErrorType } from '../resources/shared';

export class AnthropicError extends Error {}
Expand Down
4 changes: 1 addition & 3 deletions src/core/streaming.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AnthropicError } from './error';
import { type ReadableStream } from '../internal/shim-types';
import { makeReadableStream } from '../internal/shims';
import { findDoubleNewlineIndex, LineDecoder } from '../internal/decoders/line';
import { findDoubleNewlineIndex, LineDecoder, type Bytes } from '../internal/decoders/line';
import { ReadableStreamToAsyncIterable } from '../internal/shims';
import { isAbortError } from '../internal/errors';
import { safeJSON } from '../internal/utils/values';
Expand All @@ -12,8 +12,6 @@ import type { BaseAnthropic } from '../client';
import { APIError } from './error';
import type { ErrorType } from '../resources/shared';

type Bytes = string | ArrayBuffer | Uint8Array | null | undefined;

export type ServerSentEvent = {
event: string | null;
data: string;
Expand Down
1 change: 0 additions & 1 deletion src/helpers/beta/json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { AutoParseableBetaOutputFormat } from '../../lib/beta-parser';
import { AnthropicError } from '../..';
import { transformJSONSchema } from '../../lib/transform-json-schema';

type NoInfer<T> = T extends infer R ? R : never;

/**
* Creates a Tool with a provided JSON schema that can be passed
Expand Down
1 change: 0 additions & 1 deletion src/helpers/json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { AutoParseableOutputFormat } from '../lib/parser';
import { AnthropicError } from '../core/error';
import { transformJSONSchema } from '../lib/transform-json-schema';

type NoInfer<T> = T extends infer R ? R : never;

/**
* Creates a JSON schema output format object from the given JSON schema.
Expand Down
32 changes: 0 additions & 32 deletions src/internal/stream-utils.ts

This file was deleted.

23 changes: 6 additions & 17 deletions src/lib/beta-parser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Logger } from '../client';
import { AnthropicError } from '../core/error';
import { parseAccumulatedFormat } from './core-parser';
import {
BetaContentBlock,
BetaJSONOutputFormat,
Expand All @@ -9,8 +9,7 @@ import {
MessageCreateParams,
} from '../resources/beta/messages/messages';

// vendored from typefest just to make things look a bit nicer on hover
type Simplify<T> = { [KeyType in keyof T]: T[KeyType] } & {};
import { Simplify } from './type-utils';

type AutoParseableBetaOutputConfig = Omit<BetaOutputConfig, 'format'> & {
format?: BetaJSONOutputFormat | AutoParseableBetaOutputFormat<any> | null;
Expand Down Expand Up @@ -131,18 +130,8 @@ function parseBetaOutputFormat<Params extends BetaParseableMessageCreateParams>(
params: Params,
content: string,
): ExtractParsedContentFromBetaParams<Params> | null {
const outputFormat = getOutputFormat(params);
if (outputFormat?.type !== 'json_schema') {
return null;
}

try {
if ('parse' in outputFormat) {
return outputFormat.parse(content);
}

return JSON.parse(content);
} catch (error) {
throw new AnthropicError(`Failed to parse structured output: ${error}`);
}
return parseAccumulatedFormat(
getOutputFormat(params),
content,
) as ExtractParsedContentFromBetaParams<Params> | null;
}
29 changes: 29 additions & 0 deletions src/lib/core-parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { AnthropicError } from '../core/error';

/**
* Shared parse-step runtime used by both parser.ts (stable) and beta-parser.ts.
*
* Accepts the resolved format object (already extracted from params by the
* caller's getOutputFormat) and the accumulated text content. Returns the
* parsed value, or null when the format is absent or non-parseable.
*
* Throws AnthropicError if the format is json_schema but parsing fails —
* same behaviour that lived in each parser previously.
*/
export function parseAccumulatedFormat(
format: { type: string; parse?: (content: string) => unknown } | null | undefined,
content: string,
): unknown | null {
if (format?.type !== 'json_schema') {
return null;
}

try {
if ('parse' in (format as object)) {
return (format as { parse: (c: string) => unknown }).parse(content);
}
return JSON.parse(content);
} catch (error) {
throw new AnthropicError(`Failed to parse structured output: ${error}`);
}
}
23 changes: 6 additions & 17 deletions src/lib/parser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Logger } from '../client';
import { AnthropicError } from '../core/error';
import { parseAccumulatedFormat } from './core-parser';
import {
ContentBlock,
JSONOutputFormat,
Expand All @@ -9,8 +9,7 @@ import {
MessageCreateParams,
} from '../resources/messages/messages';

// vendored from typefest just to make things look a bit nicer on hover
type Simplify<T> = { [KeyType in keyof T]: T[KeyType] } & {};
import { Simplify } from './type-utils';

type AutoParseableOutputConfig = Omit<OutputConfig, 'format'> & {
format?: JSONOutputFormat | AutoParseableOutputFormat<any> | null;
Expand Down Expand Up @@ -108,18 +107,8 @@ function parseOutputFormat<Params extends ParseableMessageCreateParams>(
params: Params,
content: string,
): ExtractParsedContentFromParams<Params> | null {
const outputFormat = getOutputFormat(params);
if (outputFormat?.type !== 'json_schema') {
return null;
}

try {
if ('parse' in outputFormat) {
return outputFormat.parse(content);
}

return JSON.parse(content);
} catch (error) {
throw new AnthropicError(`Failed to parse structured output: ${error}`);
}
return parseAccumulatedFormat(
getOutputFormat(params),
content,
) as ExtractParsedContentFromParams<Params> | null;
}
2 changes: 1 addition & 1 deletion src/lib/tools/BetaRunnableTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
BetaToolTextEditor20250728,
} from '../../resources/beta';

export type Promisable<T> = T | Promise<T>;
export type { Promisable } from '../type-utils';

/**
* Tool types that can be implemented on the client.
Expand Down
3 changes: 1 addition & 2 deletions src/lib/tools/BetaToolRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { RequestOptions } from '../../internal/request-options';
import { buildHeaders } from '../../internal/headers';
import { CompactionControl, DEFAULT_SUMMARY_PROMPT, DEFAULT_TOKEN_THRESHOLD } from './CompactionControl';
import { collectStainlessHelpers } from '../stainless-helper-header';
import { Simplify } from '../type-utils';

/**
* Just Promise.withResolvers(), which is not available in all environments.
Expand Down Expand Up @@ -467,8 +468,6 @@ async function generateToolResponse(
};
}

// vendored from typefest just to make things look a bit nicer on hover
type Simplify<T> = { [KeyType in keyof T]: T[KeyType] } & {};

/**
* Parameters for creating a ToolRunner, extending MessageCreateParams with runnable tools.
Expand Down
Loading