Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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 docs/content/docs/references/meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"[@xsai/utils-chat](https://doc.deno.land/https://esm.sh/@xsai/utils-chat)",
"[@xsai/utils-reasoning](https://doc.deno.land/https://esm.sh/@xsai/utils-reasoning)",
"[@xsai/utils-stream](https://doc.deno.land/https://esm.sh/@xsai/utils-stream)",
"[@xsai-ext/messages](https://doc.deno.land/https://esm.sh/@xsai-ext/messages)",
"[@xsai-ext/providers](https://doc.deno.land/https://esm.sh/@xsai-ext/providers)",
"[@xsai-ext/responses](https://doc.deno.land/https://esm.sh/@xsai-ext/responses)",
"[@xsai-ext/telemetry](https://doc.deno.land/https://esm.sh/@xsai-ext/telemetry)",
Expand Down
1 change: 1 addition & 0 deletions packages-ext/messages/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://xsai.js.org/docs/packages-ext/messages
48 changes: 48 additions & 0 deletions packages-ext/messages/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "@xsai-ext/messages",
"type": "module",
"version": "0.4.4",
"description": "extra-small AI SDK.",
"author": "Moeru AI",
"license": "MIT",
"homepage": "https://xsai.js.org",
"repository": {
"type": "git",
"url": "git+https://github.com/moeru-ai/xsai.git",
"directory": "packages-ext/messages"
},
"bugs": "https://github.com/moeru-ai/xsai/issues",
"keywords": [
"xsai",
"anthropic",
"ai"
],
"sideEffects": false,
"exports": "./src/index.ts",
"files": [
"dist"
],
"publishConfig": {
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./package.json": "./package.json"
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts"
},
"scripts": {
"build": "pkgroll",
"test": "vitest run"
},
"dependencies": {
"@xsai/shared": "workspace:~",
"eventsource-parser": "catalog:"
},
"devDependencies": {
"@standard-schema/spec": "catalog:",
"zod": "catalog:schema-dev"
}
Comment on lines +45 to +48
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The @standard-schema/spec package is used for types in the public API of this package (e.g., in src/types/anthropic-tool.ts). Therefore, it should be listed under dependencies instead of devDependencies to ensure its types are available to consumers of this package.

}
10 changes: 10 additions & 0 deletions packages-ext/messages/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export type * from './types/anthropic-message'
export type * from './types/anthropic-tool'
export type * from './types/finish-reason'
export type * from './types/messages-options'
export type * from './types/step'
export type * from './types/streaming-event'
export type * from './types/usage'
export * from './utils/count-tokens'
export * from './utils/messages'
export * from './utils/tool'
169 changes: 169 additions & 0 deletions packages-ext/messages/src/types/anthropic-message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
import type { AnthropicCacheControl, AnthropicTool, AnthropicToolChoice } from './anthropic-tool'
import type { AnthropicStopReason } from './finish-reason'
import type { Usage } from './usage'

export interface AnthropicAssistantMessageParam {
content: (AnthropicRedactedThinkingBlock | AnthropicTextBlockParam | AnthropicThinkingBlockParam | AnthropicToolUseBlock)[] | AnthropicRedactedThinkingBlock[] | AnthropicTextBlockParam[] | AnthropicThinkingBlockParam[] | AnthropicToolUseBlock[] | string
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The union type for content is unnecessarily verbose. A type like (A | B)[] | A[] | B[] can be simplified to (A | B)[]. Applying this simplification will make the type definition cleaner and easier to read.

Suggested change
content: (AnthropicRedactedThinkingBlock | AnthropicTextBlockParam | AnthropicThinkingBlockParam | AnthropicToolUseBlock)[] | AnthropicRedactedThinkingBlock[] | AnthropicTextBlockParam[] | AnthropicThinkingBlockParam[] | AnthropicToolUseBlock[] | string
content: (AnthropicRedactedThinkingBlock | AnthropicTextBlockParam | AnthropicThinkingBlockParam | AnthropicToolUseBlock)[] | string

role: 'assistant'
}

export interface AnthropicBase64ImageSource {
data: string
media_type: 'image/gif' | 'image/jpeg' | 'image/png' | 'image/webp'
type: 'base64'
}

export interface AnthropicBase64PDFSource {
data: string
media_type: 'application/pdf'
type: 'base64'
}

export type AnthropicContentBlock = AnthropicRedactedThinkingBlock | AnthropicTextBlock | AnthropicThinkingBlock | AnthropicToolUseBlock | (Record<string, unknown> & { type: string })

export interface AnthropicDocumentBlockParam {
cache_control?: AnthropicCacheControl | null
citations?: {
enabled?: boolean
}
context?: string
source: AnthropicBase64PDFSource | AnthropicPlainTextSource | AnthropicURLDocumentSource
title?: string
type: 'document'
}

export interface AnthropicImageBlockParam {
cache_control?: AnthropicCacheControl | null
source: AnthropicBase64ImageSource | AnthropicURLImageSource
type: 'image'
}

export interface AnthropicMessage {
content: AnthropicContentBlock[]
id: string
model: string
role: 'assistant'
stop_reason: AnthropicStopReason | null
stop_sequence: null | string
type: 'message'
usage: Usage
}

export interface AnthropicMessageCountTokensBody {
cache_control?: AnthropicCacheControl | null
messages: AnthropicMessageParam[]
model: string
system?: AnthropicSystemPrompt
thinking?: AnthropicThinkingConfig
tool_choice?: AnthropicToolChoice
tools?: AnthropicTool[]
}

export interface AnthropicMessageCreateBody {
cache_control?: AnthropicCacheControl | null
max_tokens: number
messages: AnthropicMessageParam[]
metadata?: AnthropicMetadata
model: string
service_tier?: 'auto' | 'standard_only'
stop_sequences?: string[]
stream?: boolean
system?: AnthropicSystemPrompt
temperature?: number
thinking?: AnthropicThinkingConfig
tool_choice?: AnthropicToolChoice
tools?: AnthropicTool[]
top_k?: number
top_p?: number
}

export type AnthropicMessageParam = AnthropicAssistantMessageParam | AnthropicUserMessageParam

export interface AnthropicMetadata {
user_id?: null | string
}

export interface AnthropicPlainTextSource {
data: string
media_type: 'text/plain'
type: 'text'
}

export interface AnthropicRedactedThinkingBlock {
data: string
type: 'redacted_thinking'
}

export type AnthropicSystemPrompt = AnthropicTextBlockParam[] | string

export interface AnthropicTextBlock {
citations?: unknown[]
text: string
type: 'text'
}

export interface AnthropicTextBlockParam {
cache_control?: AnthropicCacheControl | null
citations?: {
enabled?: boolean
}
text: string
type: 'text'
}

export interface AnthropicThinkingBlock {
signature: string
thinking: string
type: 'thinking'
}

export interface AnthropicThinkingBlockParam {
signature: string
thinking: string
type: 'thinking'
}

export type AnthropicThinkingConfig = AnthropicThinkingConfigAdaptive | AnthropicThinkingConfigDisabled | AnthropicThinkingConfigEnabled

export interface AnthropicThinkingConfigAdaptive {
type: 'adaptive'
}

export interface AnthropicThinkingConfigDisabled {
type: 'disabled'
}

export interface AnthropicThinkingConfigEnabled {
budget_tokens: number
type: 'enabled'
}

export interface AnthropicToolResultBlockParam {
cache_control?: AnthropicCacheControl | null
content?: AnthropicTextBlockParam[] | string
is_error?: boolean
tool_use_id: string
type: 'tool_result'
}

export interface AnthropicToolUseBlock {
id: string
input: Record<string, unknown>
name: string
type: 'tool_use'
}

export interface AnthropicURLDocumentSource {
type: 'url'
url: string
}

export interface AnthropicURLImageSource {
type: 'url'
url: string
}

export interface AnthropicUserMessageParam {
content: (AnthropicDocumentBlockParam | AnthropicImageBlockParam | AnthropicTextBlockParam | AnthropicToolResultBlockParam)[] | AnthropicDocumentBlockParam[] | AnthropicImageBlockParam[] | AnthropicTextBlockParam[] | AnthropicToolResultBlockParam[] | string
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The union type for content is unnecessarily verbose. A type like (A | B)[] | A[] | B[] can be simplified to (A | B)[]. Applying this simplification will make the type definition cleaner and easier to read.

Suggested change
content: (AnthropicDocumentBlockParam | AnthropicImageBlockParam | AnthropicTextBlockParam | AnthropicToolResultBlockParam)[] | AnthropicDocumentBlockParam[] | AnthropicImageBlockParam[] | AnthropicTextBlockParam[] | AnthropicToolResultBlockParam[] | string
content: (AnthropicDocumentBlockParam | AnthropicImageBlockParam | AnthropicTextBlockParam | AnthropicToolResultBlockParam)[] | string

role: 'user'
}
50 changes: 50 additions & 0 deletions packages-ext/messages/src/types/anthropic-tool.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import type { StandardJSONSchemaV1 } from '@standard-schema/spec'

import type { AnthropicTextBlockParam } from './anthropic-message'

export interface AnthropicCacheControl {
type: 'ephemeral'
}

export interface AnthropicTool {
description?: string
input_schema: Record<string, unknown>
name: string
strict?: boolean
}

export type AnthropicToolChoice = AnthropicToolChoiceAny | AnthropicToolChoiceAuto | AnthropicToolChoiceNone | AnthropicToolChoiceTool

export interface AnthropicToolChoiceAny {
disable_parallel_tool_use?: boolean
type: 'any'
}

export interface AnthropicToolChoiceAuto {
disable_parallel_tool_use?: boolean
type: 'auto'
}

export interface AnthropicToolChoiceNone {
type: 'none'
}

export interface AnthropicToolChoiceTool {
disable_parallel_tool_use?: boolean
name: string
type: 'tool'
}

export interface ExecutableTool extends AnthropicTool {
execute: (input: unknown) => Promise<ToolExecuteResult> | ToolExecuteResult
}

export type ToolExecuteResult = AnthropicTextBlockParam[] | object | string | unknown[]

export interface ToolOptions<T extends StandardJSONSchemaV1> {
description?: string
execute: (input: StandardJSONSchemaV1.InferInput<T>) => Promise<ToolExecuteResult> | ToolExecuteResult
inputSchema: T
name: string
strict?: boolean
}
3 changes: 3 additions & 0 deletions packages-ext/messages/src/types/finish-reason.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type AnthropicStopReason = 'end_turn' | 'max_tokens' | 'model_context_window_exceeded' | 'pause_turn' | 'refusal' | 'stop_sequence' | 'tool_use' | (string & {})

export type FinishReason = 'content_filter' | 'length' | 'other' | 'stop' | 'tool-calls' | (string & {})
21 changes: 21 additions & 0 deletions packages-ext/messages/src/types/messages-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { AnthropicMessageCountTokensBody, AnthropicMessageCreateBody } from './anthropic-message'
import type { ExecutableTool } from './anthropic-tool'

export interface CommonAnthropicTransportOptions {
abortSignal?: AbortSignal
anthropicBeta?: string | string[]
anthropicVersion?: string
apiKey?: string
baseURL?: string | URL
fetch?: typeof globalThis.fetch
headers?: Record<string, string>
}

export interface CountTokensOptions extends CommonAnthropicTransportOptions, Omit<AnthropicMessageCountTokensBody, 'tools'> {
tools?: ExecutableTool[]
}

export interface MessagesOptions extends CommonAnthropicTransportOptions, Omit<AnthropicMessageCreateBody, 'stream' | 'tools'> {
stream?: never
tools?: ExecutableTool[]
}
14 changes: 14 additions & 0 deletions packages-ext/messages/src/types/step.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { AnthropicMessage, AnthropicToolResultBlockParam, AnthropicToolUseBlock } from './anthropic-message'
import type { AnthropicStopReason, FinishReason } from './finish-reason'
import type { Usage } from './usage'

export interface Step {
finishReason: FinishReason
message: AnthropicMessage
reasoningText?: string
stopReason: AnthropicStopReason | null
text?: string
toolResults: AnthropicToolResultBlockParam[]
toolUses: AnthropicToolUseBlock[]
usage?: Usage
}
Loading
Loading