-
-
Notifications
You must be signed in to change notification settings - Fork 56
[WIP] feat(messages): add Anthropic Messages API extension #272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
7ffefb4
fc4f12e
9efcf68
7b4c46c
4807c29
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| https://xsai.js.org/docs/packages-ext/messages |
| 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" | ||
| } | ||
| } | ||
| 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' |
| 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 | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The union type for
Suggested change
|
||||||
| 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 | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The union type for
Suggested change
|
||||||
| role: 'user' | ||||||
| } | ||||||
| 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 | ||
| } |
| 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 & {}) |
| 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[] | ||
| } |
| 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 | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
@standard-schema/specpackage is used for types in the public API of this package (e.g., insrc/types/anthropic-tool.ts). Therefore, it should be listed underdependenciesinstead ofdevDependenciesto ensure its types are available to consumers of this package.