@particle-academy/prism 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +155 -0
- package/dist/audio/pending-request.d.ts +37 -0
- package/dist/audio/pending-request.d.ts.map +1 -0
- package/dist/audio/pending-request.js +91 -0
- package/dist/audio/pending-request.js.map +1 -0
- package/dist/audio/request.d.ts +37 -0
- package/dist/audio/request.d.ts.map +1 -0
- package/dist/audio/request.js +50 -0
- package/dist/audio/request.js.map +1 -0
- package/dist/audio/response.d.ts +24 -0
- package/dist/audio/response.d.ts.map +1 -0
- package/dist/audio/response.js +36 -0
- package/dist/audio/response.js.map +1 -0
- package/dist/batch/batch-job.d.ts +91 -0
- package/dist/batch/batch-job.d.ts.map +1 -0
- package/dist/batch/batch-job.js +156 -0
- package/dist/batch/batch-job.js.map +1 -0
- package/dist/batch/pending-request.d.ts +29 -0
- package/dist/batch/pending-request.d.ts.map +1 -0
- package/dist/batch/pending-request.js +73 -0
- package/dist/batch/pending-request.js.map +1 -0
- package/dist/batch/request.d.ts +73 -0
- package/dist/batch/request.d.ts.map +1 -0
- package/dist/batch/request.js +79 -0
- package/dist/batch/request.js.map +1 -0
- package/dist/embeddings/pending-request.d.ts +38 -0
- package/dist/embeddings/pending-request.d.ts.map +1 -0
- package/dist/embeddings/pending-request.js +86 -0
- package/dist/embeddings/pending-request.js.map +1 -0
- package/dist/embeddings/request.d.ts +27 -0
- package/dist/embeddings/request.d.ts.map +1 -0
- package/dist/embeddings/request.js +38 -0
- package/dist/embeddings/request.js.map +1 -0
- package/dist/embeddings/response.d.ts +19 -0
- package/dist/embeddings/response.d.ts.map +1 -0
- package/dist/embeddings/response.js +21 -0
- package/dist/embeddings/response.js.map +1 -0
- package/dist/enums.d.ts +52 -0
- package/dist/enums.d.ts.map +1 -0
- package/dist/enums.js +69 -0
- package/dist/enums.js.map +1 -0
- package/dist/errors.d.ts +119 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +115 -0
- package/dist/errors.js.map +1 -0
- package/dist/files/file-data.d.ts +53 -0
- package/dist/files/file-data.d.ts.map +1 -0
- package/dist/files/file-data.js +79 -0
- package/dist/files/file-data.js.map +1 -0
- package/dist/files/pending-request.d.ts +38 -0
- package/dist/files/pending-request.d.ts.map +1 -0
- package/dist/files/pending-request.js +96 -0
- package/dist/files/pending-request.js.map +1 -0
- package/dist/files/request.d.ts +77 -0
- package/dist/files/request.d.ts.map +1 -0
- package/dist/files/request.js +81 -0
- package/dist/files/request.js.map +1 -0
- package/dist/fim/pending-request.d.ts +31 -0
- package/dist/fim/pending-request.d.ts.map +1 -0
- package/dist/fim/pending-request.js +89 -0
- package/dist/fim/pending-request.js.map +1 -0
- package/dist/fim/request.d.ts +39 -0
- package/dist/fim/request.d.ts.map +1 -0
- package/dist/fim/request.js +65 -0
- package/dist/fim/request.js.map +1 -0
- package/dist/fim/response.d.ts +21 -0
- package/dist/fim/response.d.ts.map +1 -0
- package/dist/fim/response.js +30 -0
- package/dist/fim/response.js.map +1 -0
- package/dist/http/header-names.d.ts +36 -0
- package/dist/http/header-names.d.ts.map +1 -0
- package/dist/http/header-names.js +44 -0
- package/dist/http/header-names.js.map +1 -0
- package/dist/http/transport.d.ts +94 -0
- package/dist/http/transport.d.ts.map +1 -0
- package/dist/http/transport.js +131 -0
- package/dist/http/transport.js.map +1 -0
- package/dist/images/pending-request.d.ts +30 -0
- package/dist/images/pending-request.d.ts.map +1 -0
- package/dist/images/pending-request.js +66 -0
- package/dist/images/pending-request.js.map +1 -0
- package/dist/images/request.d.ts +19 -0
- package/dist/images/request.d.ts.map +1 -0
- package/dist/images/request.js +30 -0
- package/dist/images/request.js.map +1 -0
- package/dist/images/response.d.ts +29 -0
- package/dist/images/response.d.ts.map +1 -0
- package/dist/images/response.js +34 -0
- package/dist/images/response.js.map +1 -0
- package/dist/index.d.ts +108 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +84 -0
- package/dist/index.js.map +1 -0
- package/dist/internal/filters.d.ts +40 -0
- package/dist/internal/filters.d.ts.map +1 -0
- package/dist/internal/filters.js +83 -0
- package/dist/internal/filters.js.map +1 -0
- package/dist/json.d.ts +41 -0
- package/dist/json.d.ts.map +1 -0
- package/dist/json.js +75 -0
- package/dist/json.js.map +1 -0
- package/dist/moderation/pending-request.d.ts +24 -0
- package/dist/moderation/pending-request.d.ts.map +1 -0
- package/dist/moderation/pending-request.js +60 -0
- package/dist/moderation/pending-request.js.map +1 -0
- package/dist/moderation/request.d.ts +20 -0
- package/dist/moderation/request.d.ts.map +1 -0
- package/dist/moderation/request.js +31 -0
- package/dist/moderation/request.js.map +1 -0
- package/dist/moderation/response.d.ts +26 -0
- package/dist/moderation/response.d.ts.map +1 -0
- package/dist/moderation/response.js +34 -0
- package/dist/moderation/response.js.map +1 -0
- package/dist/prism.d.ts +43 -0
- package/dist/prism.d.ts.map +1 -0
- package/dist/prism.js +79 -0
- package/dist/prism.js.map +1 -0
- package/dist/providers/anthropic/anthropic.d.ts +67 -0
- package/dist/providers/anthropic/anthropic.d.ts.map +1 -0
- package/dist/providers/anthropic/anthropic.js +172 -0
- package/dist/providers/anthropic/anthropic.js.map +1 -0
- package/dist/providers/anthropic/build-request-body.d.ts +15 -0
- package/dist/providers/anthropic/build-request-body.d.ts.map +1 -0
- package/dist/providers/anthropic/build-request-body.js +51 -0
- package/dist/providers/anthropic/build-request-body.js.map +1 -0
- package/dist/providers/anthropic/maps/finish-reason-map.d.ts +14 -0
- package/dist/providers/anthropic/maps/finish-reason-map.d.ts.map +1 -0
- package/dist/providers/anthropic/maps/finish-reason-map.js +30 -0
- package/dist/providers/anthropic/maps/finish-reason-map.js.map +1 -0
- package/dist/providers/anthropic/maps/media-map.d.ts +25 -0
- package/dist/providers/anthropic/maps/media-map.d.ts.map +1 -0
- package/dist/providers/anthropic/maps/media-map.js +87 -0
- package/dist/providers/anthropic/maps/media-map.js.map +1 -0
- package/dist/providers/anthropic/maps/message-map.d.ts +31 -0
- package/dist/providers/anthropic/maps/message-map.d.ts.map +1 -0
- package/dist/providers/anthropic/maps/message-map.js +121 -0
- package/dist/providers/anthropic/maps/message-map.js.map +1 -0
- package/dist/providers/anthropic/maps/tool-choice-map.d.ts +11 -0
- package/dist/providers/anthropic/maps/tool-choice-map.d.ts.map +1 -0
- package/dist/providers/anthropic/maps/tool-choice-map.js +25 -0
- package/dist/providers/anthropic/maps/tool-choice-map.js.map +1 -0
- package/dist/providers/anthropic/maps/tool-map.d.ts +14 -0
- package/dist/providers/anthropic/maps/tool-map.d.ts.map +1 -0
- package/dist/providers/anthropic/maps/tool-map.js +22 -0
- package/dist/providers/anthropic/maps/tool-map.js.map +1 -0
- package/dist/providers/anthropic/parse-response.d.ts +21 -0
- package/dist/providers/anthropic/parse-response.d.ts.map +1 -0
- package/dist/providers/anthropic/parse-response.js +109 -0
- package/dist/providers/anthropic/parse-response.js.map +1 -0
- package/dist/providers/anthropic/rate-limits.d.ts +4 -0
- package/dist/providers/anthropic/rate-limits.d.ts.map +1 -0
- package/dist/providers/anthropic/rate-limits.js +47 -0
- package/dist/providers/anthropic/rate-limits.js.map +1 -0
- package/dist/providers/anthropic/stream-events.d.ts +27 -0
- package/dist/providers/anthropic/stream-events.d.ts.map +1 -0
- package/dist/providers/anthropic/stream-events.js +147 -0
- package/dist/providers/anthropic/stream-events.js.map +1 -0
- package/dist/providers/mistral/build-request-body.d.ts +32 -0
- package/dist/providers/mistral/build-request-body.d.ts.map +1 -0
- package/dist/providers/mistral/build-request-body.js +69 -0
- package/dist/providers/mistral/build-request-body.js.map +1 -0
- package/dist/providers/mistral/embeddings.d.ts +14 -0
- package/dist/providers/mistral/embeddings.d.ts.map +1 -0
- package/dist/providers/mistral/embeddings.js +41 -0
- package/dist/providers/mistral/embeddings.js.map +1 -0
- package/dist/providers/mistral/fim.d.ts +32 -0
- package/dist/providers/mistral/fim.d.ts.map +1 -0
- package/dist/providers/mistral/fim.js +70 -0
- package/dist/providers/mistral/fim.js.map +1 -0
- package/dist/providers/mistral/maps/finish-reason-map.d.ts +15 -0
- package/dist/providers/mistral/maps/finish-reason-map.d.ts.map +1 -0
- package/dist/providers/mistral/maps/finish-reason-map.js +32 -0
- package/dist/providers/mistral/maps/finish-reason-map.js.map +1 -0
- package/dist/providers/mistral/maps/media-map.d.ts +24 -0
- package/dist/providers/mistral/maps/media-map.d.ts.map +1 -0
- package/dist/providers/mistral/maps/media-map.js +41 -0
- package/dist/providers/mistral/maps/media-map.js.map +1 -0
- package/dist/providers/mistral/maps/message-map.d.ts +20 -0
- package/dist/providers/mistral/maps/message-map.d.ts.map +1 -0
- package/dist/providers/mistral/maps/message-map.js +83 -0
- package/dist/providers/mistral/maps/message-map.js.map +1 -0
- package/dist/providers/mistral/maps/tool-choice-map.d.ts +12 -0
- package/dist/providers/mistral/maps/tool-choice-map.d.ts.map +1 -0
- package/dist/providers/mistral/maps/tool-choice-map.js +25 -0
- package/dist/providers/mistral/maps/tool-choice-map.js.map +1 -0
- package/dist/providers/mistral/maps/tool-map.d.ts +15 -0
- package/dist/providers/mistral/maps/tool-map.d.ts.map +1 -0
- package/dist/providers/mistral/maps/tool-map.js +26 -0
- package/dist/providers/mistral/maps/tool-map.js.map +1 -0
- package/dist/providers/mistral/mistral.d.ts +90 -0
- package/dist/providers/mistral/mistral.d.ts.map +1 -0
- package/dist/providers/mistral/mistral.js +210 -0
- package/dist/providers/mistral/mistral.js.map +1 -0
- package/dist/providers/mistral/parse-response.d.ts +35 -0
- package/dist/providers/mistral/parse-response.d.ts.map +1 -0
- package/dist/providers/mistral/parse-response.js +114 -0
- package/dist/providers/mistral/parse-response.js.map +1 -0
- package/dist/providers/mistral/rate-limits.d.ts +24 -0
- package/dist/providers/mistral/rate-limits.d.ts.map +1 -0
- package/dist/providers/mistral/rate-limits.js +55 -0
- package/dist/providers/mistral/rate-limits.js.map +1 -0
- package/dist/providers/mistral/stream-events.d.ts +29 -0
- package/dist/providers/mistral/stream-events.d.ts.map +1 -0
- package/dist/providers/mistral/stream-events.js +142 -0
- package/dist/providers/mistral/stream-events.js.map +1 -0
- package/dist/providers/openai/audio.d.ts +16 -0
- package/dist/providers/openai/audio.d.ts.map +1 -0
- package/dist/providers/openai/audio.js +105 -0
- package/dist/providers/openai/audio.js.map +1 -0
- package/dist/providers/openai/batch.d.ts +29 -0
- package/dist/providers/openai/batch.d.ts.map +1 -0
- package/dist/providers/openai/batch.js +181 -0
- package/dist/providers/openai/batch.js.map +1 -0
- package/dist/providers/openai/build-request-body.d.ts +25 -0
- package/dist/providers/openai/build-request-body.d.ts.map +1 -0
- package/dist/providers/openai/build-request-body.js +66 -0
- package/dist/providers/openai/build-request-body.js.map +1 -0
- package/dist/providers/openai/build-structured-body.d.ts +16 -0
- package/dist/providers/openai/build-structured-body.d.ts.map +1 -0
- package/dist/providers/openai/build-structured-body.js +47 -0
- package/dist/providers/openai/build-structured-body.js.map +1 -0
- package/dist/providers/openai/embeddings.d.ts +6 -0
- package/dist/providers/openai/embeddings.d.ts.map +1 -0
- package/dist/providers/openai/embeddings.js +61 -0
- package/dist/providers/openai/embeddings.js.map +1 -0
- package/dist/providers/openai/files.d.ts +16 -0
- package/dist/providers/openai/files.d.ts.map +1 -0
- package/dist/providers/openai/files.js +80 -0
- package/dist/providers/openai/files.js.map +1 -0
- package/dist/providers/openai/images.d.ts +6 -0
- package/dist/providers/openai/images.d.ts.map +1 -0
- package/dist/providers/openai/images.js +55 -0
- package/dist/providers/openai/images.js.map +1 -0
- package/dist/providers/openai/maps/finish-reason-map.d.ts +21 -0
- package/dist/providers/openai/maps/finish-reason-map.d.ts.map +1 -0
- package/dist/providers/openai/maps/finish-reason-map.js +56 -0
- package/dist/providers/openai/maps/finish-reason-map.js.map +1 -0
- package/dist/providers/openai/maps/media-map.d.ts +25 -0
- package/dist/providers/openai/maps/media-map.d.ts.map +1 -0
- package/dist/providers/openai/maps/media-map.js +53 -0
- package/dist/providers/openai/maps/media-map.js.map +1 -0
- package/dist/providers/openai/maps/message-map.d.ts +21 -0
- package/dist/providers/openai/maps/message-map.d.ts.map +1 -0
- package/dist/providers/openai/maps/message-map.js +128 -0
- package/dist/providers/openai/maps/message-map.js.map +1 -0
- package/dist/providers/openai/maps/tool-choice-map.d.ts +14 -0
- package/dist/providers/openai/maps/tool-choice-map.d.ts.map +1 -0
- package/dist/providers/openai/maps/tool-choice-map.js +27 -0
- package/dist/providers/openai/maps/tool-choice-map.js.map +1 -0
- package/dist/providers/openai/maps/tool-map.d.ts +16 -0
- package/dist/providers/openai/maps/tool-map.d.ts.map +1 -0
- package/dist/providers/openai/maps/tool-map.js +34 -0
- package/dist/providers/openai/maps/tool-map.js.map +1 -0
- package/dist/providers/openai/moderation.d.ts +6 -0
- package/dist/providers/openai/moderation.d.ts.map +1 -0
- package/dist/providers/openai/moderation.js +26 -0
- package/dist/providers/openai/moderation.js.map +1 -0
- package/dist/providers/openai/openai.d.ts +103 -0
- package/dist/providers/openai/openai.d.ts.map +1 -0
- package/dist/providers/openai/openai.js +385 -0
- package/dist/providers/openai/openai.js.map +1 -0
- package/dist/providers/openai/parse-response.d.ts +21 -0
- package/dist/providers/openai/parse-response.d.ts.map +1 -0
- package/dist/providers/openai/parse-response.js +114 -0
- package/dist/providers/openai/parse-response.js.map +1 -0
- package/dist/providers/openai/rate-limits.d.ts +4 -0
- package/dist/providers/openai/rate-limits.d.ts.map +1 -0
- package/dist/providers/openai/rate-limits.js +47 -0
- package/dist/providers/openai/rate-limits.js.map +1 -0
- package/dist/providers/openai/stream-events.d.ts +13 -0
- package/dist/providers/openai/stream-events.d.ts.map +1 -0
- package/dist/providers/openai/stream-events.js +79 -0
- package/dist/providers/openai/stream-events.js.map +1 -0
- package/dist/providers/openai/structured-mode-resolver.d.ts +3 -0
- package/dist/providers/openai/structured-mode-resolver.d.ts.map +1 -0
- package/dist/providers/openai/structured-mode-resolver.js +41 -0
- package/dist/providers/openai/structured-mode-resolver.js.map +1 -0
- package/dist/providers/provider.d.ts +55 -0
- package/dist/providers/provider.d.ts.map +1 -0
- package/dist/providers/provider.js +73 -0
- package/dist/providers/provider.js.map +1 -0
- package/dist/providers/registry.d.ts +10 -0
- package/dist/providers/registry.d.ts.map +1 -0
- package/dist/providers/registry.js +27 -0
- package/dist/providers/registry.js.map +1 -0
- package/dist/providers/support/media.d.ts +16 -0
- package/dist/providers/support/media.d.ts.map +1 -0
- package/dist/providers/support/media.js +26 -0
- package/dist/providers/support/media.js.map +1 -0
- package/dist/schema/index.d.ts +114 -0
- package/dist/schema/index.d.ts.map +1 -0
- package/dist/schema/index.js +192 -0
- package/dist/schema/index.js.map +1 -0
- package/dist/streaming/events.d.ts +90 -0
- package/dist/streaming/events.d.ts.map +1 -0
- package/dist/streaming/events.js +152 -0
- package/dist/streaming/events.js.map +1 -0
- package/dist/streaming/sse.d.ts +17 -0
- package/dist/streaming/sse.d.ts.map +1 -0
- package/dist/streaming/sse.js +50 -0
- package/dist/streaming/sse.js.map +1 -0
- package/dist/structured/extract.d.ts +23 -0
- package/dist/structured/extract.d.ts.map +1 -0
- package/dist/structured/extract.js +43 -0
- package/dist/structured/extract.js.map +1 -0
- package/dist/structured/from-text.d.ts +16 -0
- package/dist/structured/from-text.d.ts.map +1 -0
- package/dist/structured/from-text.js +27 -0
- package/dist/structured/from-text.js.map +1 -0
- package/dist/structured/pending-request.d.ts +28 -0
- package/dist/structured/pending-request.d.ts.map +1 -0
- package/dist/structured/pending-request.js +48 -0
- package/dist/structured/pending-request.js.map +1 -0
- package/dist/structured/request.d.ts +28 -0
- package/dist/structured/request.d.ts.map +1 -0
- package/dist/structured/request.js +31 -0
- package/dist/structured/request.js.map +1 -0
- package/dist/structured/response.d.ts +44 -0
- package/dist/structured/response.d.ts.map +1 -0
- package/dist/structured/response.js +49 -0
- package/dist/structured/response.js.map +1 -0
- package/dist/text/pending-request.d.ts +77 -0
- package/dist/text/pending-request.d.ts.map +1 -0
- package/dist/text/pending-request.js +189 -0
- package/dist/text/pending-request.js.map +1 -0
- package/dist/text/request.d.ts +62 -0
- package/dist/text/request.d.ts.map +1 -0
- package/dist/text/request.js +117 -0
- package/dist/text/request.js.map +1 -0
- package/dist/text/response-builder.d.ts +16 -0
- package/dist/text/response-builder.d.ts.map +1 -0
- package/dist/text/response-builder.js +52 -0
- package/dist/text/response-builder.js.map +1 -0
- package/dist/text/response.d.ts +36 -0
- package/dist/text/response.d.ts.map +1 -0
- package/dist/text/response.js +40 -0
- package/dist/text/response.js.map +1 -0
- package/dist/text/step.d.ts +41 -0
- package/dist/text/step.d.ts.map +1 -0
- package/dist/text/step.js +46 -0
- package/dist/text/step.js.map +1 -0
- package/dist/tool.d.ts +34 -0
- package/dist/tool.d.ts.map +1 -0
- package/dist/tool.js +86 -0
- package/dist/tool.js.map +1 -0
- package/dist/value-objects/artifact.d.ts +14 -0
- package/dist/value-objects/artifact.d.ts.map +1 -0
- package/dist/value-objects/artifact.js +32 -0
- package/dist/value-objects/artifact.js.map +1 -0
- package/dist/value-objects/embedding.d.ts +24 -0
- package/dist/value-objects/embedding.d.ts.map +1 -0
- package/dist/value-objects/embedding.js +29 -0
- package/dist/value-objects/embedding.js.map +1 -0
- package/dist/value-objects/embeddings-usage.d.ts +16 -0
- package/dist/value-objects/embeddings-usage.d.ts.map +1 -0
- package/dist/value-objects/embeddings-usage.js +19 -0
- package/dist/value-objects/embeddings-usage.js.map +1 -0
- package/dist/value-objects/generated-audio.d.ts +20 -0
- package/dist/value-objects/generated-audio.d.ts.map +1 -0
- package/dist/value-objects/generated-audio.js +23 -0
- package/dist/value-objects/generated-audio.js.map +1 -0
- package/dist/value-objects/generated-image.d.ts +36 -0
- package/dist/value-objects/generated-image.d.ts.map +1 -0
- package/dist/value-objects/generated-image.js +35 -0
- package/dist/value-objects/generated-image.js.map +1 -0
- package/dist/value-objects/index.d.ts +13 -0
- package/dist/value-objects/index.d.ts.map +1 -0
- package/dist/value-objects/index.js +10 -0
- package/dist/value-objects/index.js.map +1 -0
- package/dist/value-objects/media/audio.d.ts +8 -0
- package/dist/value-objects/media/audio.d.ts.map +1 -0
- package/dist/value-objects/media/audio.js +9 -0
- package/dist/value-objects/media/audio.js.map +1 -0
- package/dist/value-objects/media/document.d.ts +44 -0
- package/dist/value-objects/media/document.d.ts.map +1 -0
- package/dist/value-objects/media/document.js +75 -0
- package/dist/value-objects/media/document.js.map +1 -0
- package/dist/value-objects/media/image.d.ts +13 -0
- package/dist/value-objects/media/image.d.ts.map +1 -0
- package/dist/value-objects/media/image.js +14 -0
- package/dist/value-objects/media/image.js.map +1 -0
- package/dist/value-objects/media/media.d.ts +119 -0
- package/dist/value-objects/media/media.d.ts.map +1 -0
- package/dist/value-objects/media/media.js +267 -0
- package/dist/value-objects/media/media.js.map +1 -0
- package/dist/value-objects/media/part.d.ts +25 -0
- package/dist/value-objects/media/part.d.ts.map +1 -0
- package/dist/value-objects/media/part.js +41 -0
- package/dist/value-objects/media/part.js.map +1 -0
- package/dist/value-objects/media/text.d.ts +24 -0
- package/dist/value-objects/media/text.d.ts.map +1 -0
- package/dist/value-objects/media/text.js +33 -0
- package/dist/value-objects/media/text.js.map +1 -0
- package/dist/value-objects/media/video.d.ts +7 -0
- package/dist/value-objects/media/video.d.ts.map +1 -0
- package/dist/value-objects/media/video.js +8 -0
- package/dist/value-objects/media/video.js.map +1 -0
- package/dist/value-objects/messages/assistant-message.d.ts +26 -0
- package/dist/value-objects/messages/assistant-message.d.ts.map +1 -0
- package/dist/value-objects/messages/assistant-message.js +37 -0
- package/dist/value-objects/messages/assistant-message.js.map +1 -0
- package/dist/value-objects/messages/index.d.ts +27 -0
- package/dist/value-objects/messages/index.d.ts.map +1 -0
- package/dist/value-objects/messages/index.js +32 -0
- package/dist/value-objects/messages/index.js.map +1 -0
- package/dist/value-objects/messages/system-message.d.ts +9 -0
- package/dist/value-objects/messages/system-message.d.ts.map +1 -0
- package/dist/value-objects/messages/system-message.js +17 -0
- package/dist/value-objects/messages/system-message.js.map +1 -0
- package/dist/value-objects/messages/tool-result-message.d.ts +14 -0
- package/dist/value-objects/messages/tool-result-message.d.ts.map +1 -0
- package/dist/value-objects/messages/tool-result-message.js +26 -0
- package/dist/value-objects/messages/tool-result-message.js.map +1 -0
- package/dist/value-objects/messages/user-message.d.ts +47 -0
- package/dist/value-objects/messages/user-message.d.ts.map +1 -0
- package/dist/value-objects/messages/user-message.js +87 -0
- package/dist/value-objects/messages/user-message.js.map +1 -0
- package/dist/value-objects/meta.d.ts +12 -0
- package/dist/value-objects/meta.d.ts.map +1 -0
- package/dist/value-objects/meta.js +28 -0
- package/dist/value-objects/meta.js.map +1 -0
- package/dist/value-objects/moderation-result.d.ts +20 -0
- package/dist/value-objects/moderation-result.d.ts.map +1 -0
- package/dist/value-objects/moderation-result.js +64 -0
- package/dist/value-objects/moderation-result.js.map +1 -0
- package/dist/value-objects/provider-rate-limit.d.ts +11 -0
- package/dist/value-objects/provider-rate-limit.d.ts.map +1 -0
- package/dist/value-objects/provider-rate-limit.js +34 -0
- package/dist/value-objects/provider-rate-limit.js.map +1 -0
- package/dist/value-objects/provider-tool.d.ts +19 -0
- package/dist/value-objects/provider-tool.d.ts.map +1 -0
- package/dist/value-objects/provider-tool.js +31 -0
- package/dist/value-objects/provider-tool.js.map +1 -0
- package/dist/value-objects/tool-call.d.ts +29 -0
- package/dist/value-objects/tool-call.d.ts.map +1 -0
- package/dist/value-objects/tool-call.js +112 -0
- package/dist/value-objects/tool-call.js.map +1 -0
- package/dist/value-objects/tool-result.d.ts +25 -0
- package/dist/value-objects/tool-result.d.ts.map +1 -0
- package/dist/value-objects/tool-result.js +45 -0
- package/dist/value-objects/tool-result.js.map +1 -0
- package/dist/value-objects/usage.d.ts +13 -0
- package/dist/value-objects/usage.d.ts.map +1 -0
- package/dist/value-objects/usage.js +31 -0
- package/dist/value-objects/usage.js.map +1 -0
- package/package.json +46 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { JsonValue } from '../../../json.js';
|
|
2
|
+
import type { Message, SystemMessage } from '../../../value-objects/messages/index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Map messages onto Anthropic's `messages` array.
|
|
5
|
+
*
|
|
6
|
+
* Anthropic differs from the Responses API in four ways that are easy to get
|
|
7
|
+
* subtly wrong:
|
|
8
|
+
*
|
|
9
|
+
* 1. System prompts do NOT go in the message list. They are a top-level
|
|
10
|
+
* `system` field, which is why {@link mapSystem} exists separately — a
|
|
11
|
+
* system message pushed into `messages` is rejected outright.
|
|
12
|
+
* 2. Tool calls stay ATTACHED to the assistant turn as `tool_use` content
|
|
13
|
+
* blocks. They do not become top-level items the way `function_call` does.
|
|
14
|
+
* 3. A tool result is a USER turn containing `tool_result` blocks, keyed by
|
|
15
|
+
* `tool_use_id` — the id of the CALL, not of the result. That is the
|
|
16
|
+
* opposite of the Responses API, where the result id is the key.
|
|
17
|
+
* 4. Turns must alternate. Consecutive tool results therefore travel as
|
|
18
|
+
* blocks inside ONE user turn rather than as several user turns.
|
|
19
|
+
*
|
|
20
|
+
* @throws PrismError code `unknown_message_type`
|
|
21
|
+
*/
|
|
22
|
+
export declare function mapMessages(messages: readonly Message[]): JsonValue[];
|
|
23
|
+
/**
|
|
24
|
+
* The top-level `system` field, or null when there is nothing to say.
|
|
25
|
+
*
|
|
26
|
+
* Several system prompts join with a blank line between them. Anthropic accepts
|
|
27
|
+
* an array of blocks here too, but a joined string is what the reference sends
|
|
28
|
+
* and the bytes are the contract.
|
|
29
|
+
*/
|
|
30
|
+
export declare function mapSystem(systemPrompts: readonly SystemMessage[]): string | null;
|
|
31
|
+
//# sourceMappingURL=message-map.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"message-map.d.ts","sourceRoot":"","sources":["../../../../src/providers/anthropic/maps/message-map.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAc,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAG9D,OAAO,KAAK,EAAoB,OAAO,EAAE,aAAa,EAAqB,MAAM,0CAA0C,CAAC;AAI5H;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,WAAW,CAAC,QAAQ,EAAE,SAAS,OAAO,EAAE,GAAG,SAAS,EAAE,CAiCrE;AAED;;;;;;GAMG;AACH,wBAAgB,SAAS,CAAC,aAAa,EAAE,SAAS,aAAa,EAAE,GAAG,MAAM,GAAG,IAAI,CAIhF"}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { canonicalJson, isJsonObject } from '../../../json.js';
|
|
2
|
+
import { PrismError } from '../../../errors.js';
|
|
3
|
+
import { mapDocument, mapImage } from './media-map.js';
|
|
4
|
+
/**
|
|
5
|
+
* Map messages onto Anthropic's `messages` array.
|
|
6
|
+
*
|
|
7
|
+
* Anthropic differs from the Responses API in four ways that are easy to get
|
|
8
|
+
* subtly wrong:
|
|
9
|
+
*
|
|
10
|
+
* 1. System prompts do NOT go in the message list. They are a top-level
|
|
11
|
+
* `system` field, which is why {@link mapSystem} exists separately — a
|
|
12
|
+
* system message pushed into `messages` is rejected outright.
|
|
13
|
+
* 2. Tool calls stay ATTACHED to the assistant turn as `tool_use` content
|
|
14
|
+
* blocks. They do not become top-level items the way `function_call` does.
|
|
15
|
+
* 3. A tool result is a USER turn containing `tool_result` blocks, keyed by
|
|
16
|
+
* `tool_use_id` — the id of the CALL, not of the result. That is the
|
|
17
|
+
* opposite of the Responses API, where the result id is the key.
|
|
18
|
+
* 4. Turns must alternate. Consecutive tool results therefore travel as
|
|
19
|
+
* blocks inside ONE user turn rather than as several user turns.
|
|
20
|
+
*
|
|
21
|
+
* @throws PrismError code `unknown_message_type`
|
|
22
|
+
*/
|
|
23
|
+
export function mapMessages(messages) {
|
|
24
|
+
const items = [];
|
|
25
|
+
for (const message of messages) {
|
|
26
|
+
switch (message.type) {
|
|
27
|
+
case 'system':
|
|
28
|
+
// Reaching here means a system message was passed as a MESSAGE rather
|
|
29
|
+
// than a system prompt. Anthropic has no system role, and silently
|
|
30
|
+
// demoting it to a user turn would change what the model was told.
|
|
31
|
+
throw PrismError.unknownMessageType('a system message in the message list; pass it as a system prompt');
|
|
32
|
+
case 'user':
|
|
33
|
+
items.push({
|
|
34
|
+
role: 'user',
|
|
35
|
+
content: [
|
|
36
|
+
{ type: 'text', text: message.text() },
|
|
37
|
+
...message.images().map(mapImage),
|
|
38
|
+
...message.documents().map(mapDocument),
|
|
39
|
+
],
|
|
40
|
+
...message.additionalAttributes,
|
|
41
|
+
});
|
|
42
|
+
break;
|
|
43
|
+
case 'assistant':
|
|
44
|
+
mapAssistantMessage(message, items);
|
|
45
|
+
break;
|
|
46
|
+
case 'tool_result':
|
|
47
|
+
mapToolResultMessage(message, items);
|
|
48
|
+
break;
|
|
49
|
+
default:
|
|
50
|
+
throw PrismError.unknownMessageType(describeMessage(message));
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return items;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* The top-level `system` field, or null when there is nothing to say.
|
|
57
|
+
*
|
|
58
|
+
* Several system prompts join with a blank line between them. Anthropic accepts
|
|
59
|
+
* an array of blocks here too, but a joined string is what the reference sends
|
|
60
|
+
* and the bytes are the contract.
|
|
61
|
+
*/
|
|
62
|
+
export function mapSystem(systemPrompts) {
|
|
63
|
+
const parts = systemPrompts.map((prompt) => prompt.content).filter((content) => content !== '');
|
|
64
|
+
return parts.length > 0 ? parts.join('\n\n') : null;
|
|
65
|
+
}
|
|
66
|
+
function mapAssistantMessage(message, items) {
|
|
67
|
+
const content = [];
|
|
68
|
+
// Text first. Anthropic reads content blocks in order, and a tool_use ahead
|
|
69
|
+
// of the reasoning that led to it reads as a model that decided first and
|
|
70
|
+
// explained afterwards.
|
|
71
|
+
if (message.content !== '') {
|
|
72
|
+
content.push({ type: 'text', text: message.content });
|
|
73
|
+
}
|
|
74
|
+
for (const toolCall of message.toolCalls) {
|
|
75
|
+
content.push({
|
|
76
|
+
type: 'tool_use',
|
|
77
|
+
// The CALL id. Anthropic echoes it back on the matching tool_result, and
|
|
78
|
+
// a mismatch here is rejected rather than ignored.
|
|
79
|
+
id: toolCall.id,
|
|
80
|
+
name: toolCall.name,
|
|
81
|
+
// An OBJECT, not a JSON string. The Responses API takes a document nested
|
|
82
|
+
// inside a document; Anthropic takes the arguments themselves.
|
|
83
|
+
input: toolCall.parsedArguments(),
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
// An assistant turn with neither text nor tool calls contributes nothing —
|
|
87
|
+
// Anthropic rejects an empty content array.
|
|
88
|
+
if (content.length > 0) {
|
|
89
|
+
items.push({ role: 'assistant', content });
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Tool results become blocks in ONE user turn.
|
|
94
|
+
*
|
|
95
|
+
* Appended to the previous turn when that turn is already a user turn, because
|
|
96
|
+
* Anthropic requires roles to alternate and two user turns in a row is a 400.
|
|
97
|
+
*/
|
|
98
|
+
function mapToolResultMessage(message, items) {
|
|
99
|
+
const blocks = message.toolResults.map((toolResult) => ({
|
|
100
|
+
type: 'tool_result',
|
|
101
|
+
tool_use_id: toolResult.toolCallId,
|
|
102
|
+
content: stringifyToolOutput(toolResult.result),
|
|
103
|
+
}));
|
|
104
|
+
if (blocks.length === 0) {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
const previous = items[items.length - 1];
|
|
108
|
+
if (previous !== undefined && previous.role === 'user' && Array.isArray(previous.content)) {
|
|
109
|
+
previous.content = [...previous.content, ...blocks];
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
items.push({ role: 'user', content: blocks });
|
|
113
|
+
}
|
|
114
|
+
/** A tool's return value as text. Objects are canonicalised, not inspected. */
|
|
115
|
+
function stringifyToolOutput(result) {
|
|
116
|
+
return typeof result === 'string' ? result : canonicalJson(result);
|
|
117
|
+
}
|
|
118
|
+
function describeMessage(message) {
|
|
119
|
+
return isJsonObject(message) && typeof message.type === 'string' ? message.type : typeof message;
|
|
120
|
+
}
|
|
121
|
+
//# sourceMappingURL=message-map.js.map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@@
|
|
|
1
|
+
import type { JsonObject } from '../../../json.js';
|
|
2
|
+
import { ToolChoice } from '../../../enums.js';
|
|
3
|
+
/**
|
|
4
|
+
* Map the shared tool choice onto Anthropic's object form.
|
|
5
|
+
*
|
|
6
|
+
* Anthropic takes an OBJECT where OpenAI takes a string, and it spells "the
|
|
7
|
+
* model must call something" as `any` rather than `required`. A named tool is
|
|
8
|
+
* `{ type: 'tool', name }`.
|
|
9
|
+
*/
|
|
10
|
+
export declare function mapToolChoice(choice: string | ToolChoice | null): JsonObject | null;
|
|
11
|
+
//# sourceMappingURL=tool-choice-map.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-choice-map.d.ts","sourceRoot":"","sources":["../../../../src/providers/anthropic/maps/tool-choice-map.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AACnD,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAE/C;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU,GAAG,IAAI,GAAG,UAAU,GAAG,IAAI,CAmBnF"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { ToolChoice } from '../../../enums.js';
|
|
2
|
+
/**
|
|
3
|
+
* Map the shared tool choice onto Anthropic's object form.
|
|
4
|
+
*
|
|
5
|
+
* Anthropic takes an OBJECT where OpenAI takes a string, and it spells "the
|
|
6
|
+
* model must call something" as `any` rather than `required`. A named tool is
|
|
7
|
+
* `{ type: 'tool', name }`.
|
|
8
|
+
*/
|
|
9
|
+
export function mapToolChoice(choice) {
|
|
10
|
+
if (choice === null) {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
if (choice === ToolChoice.Auto) {
|
|
14
|
+
return { type: 'auto' };
|
|
15
|
+
}
|
|
16
|
+
if (choice === ToolChoice.Any) {
|
|
17
|
+
return { type: 'any' };
|
|
18
|
+
}
|
|
19
|
+
if (choice === ToolChoice.None) {
|
|
20
|
+
return { type: 'none' };
|
|
21
|
+
}
|
|
22
|
+
// Anything else names a tool.
|
|
23
|
+
return { type: 'tool', name: String(choice) };
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=tool-choice-map.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-choice-map.js","sourceRoot":"","sources":["../../../../src/providers/anthropic/maps/tool-choice-map.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAE/C;;;;;;GAMG;AACH,MAAM,UAAU,aAAa,CAAC,MAAkC;IAC9D,IAAI,MAAM,KAAK,IAAI,EAAE,CAAC;QACpB,OAAO,IAAI,CAAC;IACd,CAAC;IAED,IAAI,MAAM,KAAK,UAAU,CAAC,IAAI,EAAE,CAAC;QAC/B,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC;IAC1B,CAAC;IAED,IAAI,MAAM,KAAK,UAAU,CAAC,GAAG,EAAE,CAAC;QAC9B,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;IACzB,CAAC;IAED,IAAI,MAAM,KAAK,UAAU,CAAC,IAAI,EAAE,CAAC;QAC/B,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC;IAC1B,CAAC;IAED,8BAA8B;IAC9B,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC;AAChD,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { JsonObject } from '../../../json.js';
|
|
2
|
+
import type { Tool } from '../../../tool.js';
|
|
3
|
+
/**
|
|
4
|
+
* Map tools onto Anthropic's `tools` array.
|
|
5
|
+
*
|
|
6
|
+
* Anthropic names the schema field `input_schema`, not `parameters`, and it is
|
|
7
|
+
* NOT optional the way OpenAI's is: a tool without one is rejected. So a
|
|
8
|
+
* parameterless tool sends an empty object schema rather than omitting the key.
|
|
9
|
+
*
|
|
10
|
+
* Declaration order is preserved. Tool order reaches the model and influences
|
|
11
|
+
* which tool it picks.
|
|
12
|
+
*/
|
|
13
|
+
export declare function mapTools(tools: readonly Tool[]): JsonObject[];
|
|
14
|
+
//# sourceMappingURL=tool-map.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-map.d.ts","sourceRoot":"","sources":["../../../../src/providers/anthropic/maps/tool-map.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AACnD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAE7C;;;;;;;;;GASG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,SAAS,IAAI,EAAE,GAAG,UAAU,EAAE,CAU7D"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Map tools onto Anthropic's `tools` array.
|
|
3
|
+
*
|
|
4
|
+
* Anthropic names the schema field `input_schema`, not `parameters`, and it is
|
|
5
|
+
* NOT optional the way OpenAI's is: a tool without one is rejected. So a
|
|
6
|
+
* parameterless tool sends an empty object schema rather than omitting the key.
|
|
7
|
+
*
|
|
8
|
+
* Declaration order is preserved. Tool order reaches the model and influences
|
|
9
|
+
* which tool it picks.
|
|
10
|
+
*/
|
|
11
|
+
export function mapTools(tools) {
|
|
12
|
+
return tools.map((tool) => ({
|
|
13
|
+
name: tool.name(),
|
|
14
|
+
description: tool.description(),
|
|
15
|
+
input_schema: {
|
|
16
|
+
type: 'object',
|
|
17
|
+
properties: tool.hasParameters() ? tool.parametersAsObject() : {},
|
|
18
|
+
required: [...tool.requiredParameters()],
|
|
19
|
+
},
|
|
20
|
+
}));
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=tool-map.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-map.js","sourceRoot":"","sources":["../../../../src/providers/anthropic/maps/tool-map.ts"],"names":[],"mappings":"AAGA;;;;;;;;;GASG;AACH,MAAM,UAAU,QAAQ,CAAC,KAAsB;IAC7C,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;QAC1B,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE;QACjB,WAAW,EAAE,IAAI,CAAC,WAAW,EAAE;QAC/B,YAAY,EAAE;YACZ,IAAI,EAAE,QAAQ;YACd,UAAU,EAAE,IAAI,CAAC,aAAa,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,kBAAkB,EAAE,CAAC,CAAC,CAAC,EAAE;YACjE,QAAQ,EAAE,CAAC,GAAG,IAAI,CAAC,kBAAkB,EAAE,CAAC;SACzC;KACF,CAAC,CAAC,CAAC;AACN,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { TextRequest } from '../../text/request.js';
|
|
2
|
+
import type { TextResponse } from '../../text/response.js';
|
|
3
|
+
import type { ProviderRateLimit } from '../../value-objects/provider-rate-limit.js';
|
|
4
|
+
export interface ParseTextResponseOptions {
|
|
5
|
+
rateLimits?: readonly ProviderRateLimit[];
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Turn a raw Anthropic Messages payload into a `TextResponse`.
|
|
9
|
+
*
|
|
10
|
+
* No HTTP happens here, so a stored payload can be replayed through exactly the
|
|
11
|
+
* code path a live call takes.
|
|
12
|
+
*
|
|
13
|
+
* @throws PrismError code `provider_response_error` when the payload is missing,
|
|
14
|
+
* empty, or carries an `error`.
|
|
15
|
+
* @throws PrismError code `max_tokens_exceeded` when generation was cut short.
|
|
16
|
+
* @throws PrismError code `tool_loop_not_supported` when the response stopped on
|
|
17
|
+
* tool use. Running tools is outside this port's slice, and returning a
|
|
18
|
+
* half-formed response would be worse than saying so.
|
|
19
|
+
*/
|
|
20
|
+
export declare function parseTextResponse(request: TextRequest, rawBody: unknown, options?: ParseTextResponseOptions): TextResponse;
|
|
21
|
+
//# sourceMappingURL=parse-response.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parse-response.d.ts","sourceRoot":"","sources":["../../../src/providers/anthropic/parse-response.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAEzD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAG3D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,4CAA4C,CAAC;AAKpF,MAAM,WAAW,wBAAwB;IACvC,UAAU,CAAC,EAAE,SAAS,iBAAiB,EAAE,CAAC;CAC3C;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,iBAAiB,CAC/B,OAAO,EAAE,WAAW,EACpB,OAAO,EAAE,OAAO,EAChB,OAAO,GAAE,wBAA6B,GACrC,YAAY,CAiBd"}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { isJsonObject } from '../../json.js';
|
|
2
|
+
import { PrismError } from '../../errors.js';
|
|
3
|
+
import { FinishReason } from '../../enums.js';
|
|
4
|
+
import { isTruthyByReferenceRules, readArray, readNullableNumber, readNumber, readObject, readString, whereNotNull, } from '../../internal/filters.js';
|
|
5
|
+
import { ResponseBuilder } from '../../text/response-builder.js';
|
|
6
|
+
import { TextStep } from '../../text/step.js';
|
|
7
|
+
import { Meta } from '../../value-objects/meta.js';
|
|
8
|
+
import { ToolCall } from '../../value-objects/tool-call.js';
|
|
9
|
+
import { Usage } from '../../value-objects/usage.js';
|
|
10
|
+
import { mapFinishReason } from './maps/finish-reason-map.js';
|
|
11
|
+
/**
|
|
12
|
+
* Turn a raw Anthropic Messages payload into a `TextResponse`.
|
|
13
|
+
*
|
|
14
|
+
* No HTTP happens here, so a stored payload can be replayed through exactly the
|
|
15
|
+
* code path a live call takes.
|
|
16
|
+
*
|
|
17
|
+
* @throws PrismError code `provider_response_error` when the payload is missing,
|
|
18
|
+
* empty, or carries an `error`.
|
|
19
|
+
* @throws PrismError code `max_tokens_exceeded` when generation was cut short.
|
|
20
|
+
* @throws PrismError code `tool_loop_not_supported` when the response stopped on
|
|
21
|
+
* tool use. Running tools is outside this port's slice, and returning a
|
|
22
|
+
* half-formed response would be worse than saying so.
|
|
23
|
+
*/
|
|
24
|
+
export function parseTextResponse(request, rawBody, options = {}) {
|
|
25
|
+
const data = validateResponse(rawBody);
|
|
26
|
+
const finishReason = mapFinishReason(data);
|
|
27
|
+
if (finishReason === FinishReason.ToolCalls) {
|
|
28
|
+
throw PrismError.toolLoopNotSupported();
|
|
29
|
+
}
|
|
30
|
+
if (finishReason === FinishReason.Length) {
|
|
31
|
+
throw PrismError.maxTokensExceeded(readString(data.stop_reason, ''), readString(data.type, ''));
|
|
32
|
+
}
|
|
33
|
+
const builder = new ResponseBuilder();
|
|
34
|
+
builder.addStep(buildStep(data, request, finishReason, options.rateLimits ?? []));
|
|
35
|
+
return builder.toResponse();
|
|
36
|
+
}
|
|
37
|
+
function validateResponse(rawBody) {
|
|
38
|
+
if (!isJsonObject(rawBody) || Object.keys(rawBody).length === 0) {
|
|
39
|
+
throw PrismError.providerResponseError('Anthropic returned an empty or non-object response body.');
|
|
40
|
+
}
|
|
41
|
+
// Anthropic reports failures with `type: "error"` at the top level, and the
|
|
42
|
+
// HTTP status is not always 4xx for them — so this is checked on the body
|
|
43
|
+
// rather than left to the caller's status check.
|
|
44
|
+
if (rawBody.type === 'error' || isTruthyByReferenceRules(rawBody.error)) {
|
|
45
|
+
const error = readObject(rawBody.error) ?? {};
|
|
46
|
+
throw PrismError.providerResponseError(`Anthropic error [${readString(error.type, 'unknown')}]: ${readString(error.message, 'unknown')}`);
|
|
47
|
+
}
|
|
48
|
+
return rawBody;
|
|
49
|
+
}
|
|
50
|
+
function buildStep(data, request, finishReason, rateLimits) {
|
|
51
|
+
const content = readArray(data.content);
|
|
52
|
+
const usage = readObject(data.usage) ?? {};
|
|
53
|
+
return new TextStep({
|
|
54
|
+
text: outputText(content),
|
|
55
|
+
finishReason,
|
|
56
|
+
toolCalls: mapToolCalls(content),
|
|
57
|
+
usage: new Usage(
|
|
58
|
+
// Anthropic reports cache tokens SEPARATELY from input_tokens rather than
|
|
59
|
+
// inside them, so unlike the OpenAI mapping nothing is subtracted here.
|
|
60
|
+
// Subtracting would under-report the prompt.
|
|
61
|
+
readNumber(usage.input_tokens, 0), readNumber(usage.output_tokens, 0), readNullableNumber(usage.cache_creation_input_tokens), readNullableNumber(usage.cache_read_input_tokens),
|
|
62
|
+
// Reasoning tokens, and a BREAKDOWN of output_tokens rather than an
|
|
63
|
+
// addition to them -- 1240 thinking inside 2820 output, not beside.
|
|
64
|
+
// Reported by the Moic Suite team against the live API; every Anthropic
|
|
65
|
+
// path in all three languages was leaving this null, so no cross-language
|
|
66
|
+
// check could see it. They agreed on something wrong.
|
|
67
|
+
readNullableNumber(readObject(usage.output_tokens_details)?.thinking_tokens)),
|
|
68
|
+
meta: new Meta(readString(data.id, ''), readString(data.model, ''), rateLimits, null),
|
|
69
|
+
messages: request.messages(),
|
|
70
|
+
systemPrompts: request.systemPrompts(),
|
|
71
|
+
additionalContent: whereNotNull({
|
|
72
|
+
thinking: thinkingText(content),
|
|
73
|
+
stopSequence: typeof data.stop_sequence === 'string' ? data.stop_sequence : null,
|
|
74
|
+
}),
|
|
75
|
+
raw: data,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Every text block joined, not just the first.
|
|
80
|
+
*
|
|
81
|
+
* Anthropic splits a reply across several text blocks when thinking or tool use
|
|
82
|
+
* interleaves with it, so taking `content[0]` returns a truncated answer that
|
|
83
|
+
* looks complete.
|
|
84
|
+
*/
|
|
85
|
+
function outputText(content) {
|
|
86
|
+
return content
|
|
87
|
+
.filter(isJsonObject)
|
|
88
|
+
.filter((block) => block.type === 'text')
|
|
89
|
+
.map((block) => readString(block.text, ''))
|
|
90
|
+
.join('');
|
|
91
|
+
}
|
|
92
|
+
function mapToolCalls(content) {
|
|
93
|
+
return content
|
|
94
|
+
.filter(isJsonObject)
|
|
95
|
+
.filter((block) => block.type === 'tool_use')
|
|
96
|
+
.map((block) => new ToolCall(readString(block.id, ''), readString(block.name, ''),
|
|
97
|
+
// An object on the wire, unlike the Responses API's JSON string.
|
|
98
|
+
isJsonObject(block.input) ? block.input : {}, null));
|
|
99
|
+
}
|
|
100
|
+
/** Extended-thinking blocks joined, or null when the model did not think. */
|
|
101
|
+
function thinkingText(content) {
|
|
102
|
+
const parts = content
|
|
103
|
+
.filter(isJsonObject)
|
|
104
|
+
.filter((block) => block.type === 'thinking')
|
|
105
|
+
.map((block) => readString(block.thinking, ''))
|
|
106
|
+
.filter((text) => text !== '');
|
|
107
|
+
return parts.length > 0 ? parts.join('') : null;
|
|
108
|
+
}
|
|
109
|
+
//# sourceMappingURL=parse-response.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parse-response.js","sourceRoot":"","sources":["../../../src/providers/anthropic/parse-response.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EACL,wBAAwB,EACxB,SAAS,EACT,kBAAkB,EAClB,UAAU,EACV,UAAU,EACV,UAAU,EACV,YAAY,GACb,MAAM,2BAA2B,CAAC;AAEnC,OAAO,EAAE,eAAe,EAAE,MAAM,gCAAgC,CAAC;AAEjE,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAC9C,OAAO,EAAE,IAAI,EAAE,MAAM,6BAA6B,CAAC;AAEnD,OAAO,EAAE,QAAQ,EAAE,MAAM,kCAAkC,CAAC;AAC5D,OAAO,EAAE,KAAK,EAAE,MAAM,8BAA8B,CAAC;AACrD,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CA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,KAAK,CAAC,IAAI,KAAK,UAAU,CAAC;SAC5C,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,UAAU,CAAC,KAAK,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;SAC9C,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,KAAK,EAAE,CAAC,CAAC;IAEjC,OAAO,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;AAClD,CAAC"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { ProviderRateLimit } from '../../value-objects/provider-rate-limit.js';
|
|
2
|
+
/** Read Anthropic's `anthropic-ratelimit-*` headers into rate-limit value objects. */
|
|
3
|
+
export declare function parseRateLimits(headers: Readonly<Record<string, string>>): ProviderRateLimit[];
|
|
4
|
+
//# sourceMappingURL=rate-limits.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rate-limits.d.ts","sourceRoot":"","sources":["../../../src/providers/anthropic/rate-limits.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,4CAA4C,CAAC;AAU/E,sFAAsF;AACtF,wBAAgB,eAAe,CAAC,OAAO,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,GAAG,iBAAiB,EAAE,CA8B9F"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { ProviderRateLimit } from '../../value-objects/provider-rate-limit.js';
|
|
2
|
+
import { foldHeaderNames } from '../../http/header-names.js';
|
|
3
|
+
/**
|
|
4
|
+
* Anthropic reports four buckets where OpenAI reports two, and the extra pair
|
|
5
|
+
* matters: input and output tokens are limited SEPARATELY, so a caller that
|
|
6
|
+
* only watched a combined figure would be surprised by which one ran out.
|
|
7
|
+
*/
|
|
8
|
+
const BUCKETS = ['requests', 'tokens', 'input-tokens', 'output-tokens'];
|
|
9
|
+
/** Read Anthropic's `anthropic-ratelimit-*` headers into rate-limit value objects. */
|
|
10
|
+
export function parseRateLimits(headers) {
|
|
11
|
+
const rateLimits = [];
|
|
12
|
+
// Folded first: HTTP field names are case-insensitive (RFC 9110 §5.1) and
|
|
13
|
+
// these are exact key lookups, so an `Anthropic-RateLimit-…` from a
|
|
14
|
+
// title-casing gateway used to match nothing and return an empty array —
|
|
15
|
+
// which is also what a response with no quota headers looks like.
|
|
16
|
+
const found = foldHeaderNames(headers);
|
|
17
|
+
for (const bucket of BUCKETS) {
|
|
18
|
+
const limit = found[`anthropic-ratelimit-${bucket}-limit`];
|
|
19
|
+
const remaining = found[`anthropic-ratelimit-${bucket}-remaining`];
|
|
20
|
+
// Both or neither. A bucket with a limit and no remaining tells a caller
|
|
21
|
+
// nothing actionable, and reporting it as zero remaining would be a lie.
|
|
22
|
+
if (limit === undefined || remaining === undefined) {
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
rateLimits.push(new ProviderRateLimit(bucket, toInteger(limit), toInteger(remaining), parseResetTime(found[`anthropic-ratelimit-${bucket}-reset`])));
|
|
26
|
+
}
|
|
27
|
+
return rateLimits;
|
|
28
|
+
}
|
|
29
|
+
function toInteger(value) {
|
|
30
|
+
const parsed = Number.parseInt(value, 10);
|
|
31
|
+
return Number.isNaN(parsed) ? 0 : parsed;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Anthropic sends an RFC 3339 timestamp where OpenAI sends a duration.
|
|
35
|
+
*
|
|
36
|
+
* An unparseable value becomes null rather than the epoch: a reset time of
|
|
37
|
+
* 1970 reads as "already reset" and would have a caller retry immediately into
|
|
38
|
+
* the same limit.
|
|
39
|
+
*/
|
|
40
|
+
function parseResetTime(value) {
|
|
41
|
+
if (value === undefined || value === '') {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
const parsed = new Date(value);
|
|
45
|
+
return Number.isNaN(parsed.getTime()) ? null : parsed;
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=rate-limits.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rate-limits.js","sourceRoot":"","sources":["../../../src/providers/anthropic/rate-limits.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,4CAA4C,CAAC;AAC/E,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAE7D;;;;GAIG;AACH,MAAM,OAAO,GAAG,CAAC,UAAU,EAAE,QAAQ,EAAE,cAAc,EAAE,eAAe,CAAU,CAAC;AAEjF,sFAAsF;AACtF,MAAM,UAAU,eAAe,CAAC,OAAyC;IACvE,MAAM,UAAU,GAAwB,EAAE,CAAC;IAE3C,0EAA0E;IAC1E,oEAAoE;IACpE,yEAAyE;IACzE,kEAAkE;IAClE,MAAM,KAAK,GAAG,eAAe,CAAC,OAAO,CAAC,CAAC;IAEvC,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;QAC7B,MAAM,KAAK,GAAG,KAAK,CAAC,uBAAuB,MAAM,QAAQ,CAAC,CAAC;QAC3D,MAAM,SAAS,GAAG,KAAK,CAAC,uBAAuB,MAAM,YAAY,CAAC,CAAC;QAEnE,yEAAyE;QACzE,yEAAyE;QACzE,IAAI,KAAK,KAAK,SAAS,IAAI,SAAS,KAAK,SAAS,EAAE,CAAC;YACnD,SAAS;QACX,CAAC;QAED,UAAU,CAAC,IAAI,CACb,IAAI,iBAAiB,CACnB,MAAM,EACN,SAAS,CAAC,KAAK,CAAC,EAChB,SAAS,CAAC,SAAS,CAAC,EACpB,cAAc,CAAC,KAAK,CAAC,uBAAuB,MAAM,QAAQ,CAAC,CAAC,CAC7D,CACF,CAAC;IACJ,CAAC;IAED,OAAO,UAAU,CAAC;AACpB,CAAC;AAED,SAAS,SAAS,CAAC,KAAa;IAC9B,MAAM,MAAM,GAAG,MAAM,CAAC,QAAQ,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;IAE1C,OAAO,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;AAC3C,CAAC;AAED;;;;;;GAMG;AACH,SAAS,cAAc,CAAC,KAAyB;IAC/C,IAAI,KAAK,KAAK,SAAS,IAAI,KAAK,KAAK,EAAE,EAAE,CAAC;QACxC,OAAO,IAAI,CAAC;IACd,CAAC;IAED,MAAM,MAAM,GAAG,IAAI,IAAI,CAAC,KAAK,CAAC,CAAC;IAE/B,OAAO,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC;AACxD,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type JsonObject } from '../../json.js';
|
|
2
|
+
import { type StreamEvent } from '../../streaming/events.js';
|
|
3
|
+
/**
|
|
4
|
+
* Anthropic's stream, which needs MEMORY where OpenAI's does not.
|
|
5
|
+
*
|
|
6
|
+
* OpenAI repeats the identifiers on every event, so a payload can be mapped on
|
|
7
|
+
* its own and the mapper is a pure function. Anthropic does not: a
|
|
8
|
+
* `content_block_delta` carries only an INDEX, the message id arrived back at
|
|
9
|
+
* `message_start`, the stop reason arrives at `message_delta` and the stream
|
|
10
|
+
* ends at `message_stop`. Mapping one payload in isolation is impossible.
|
|
11
|
+
*
|
|
12
|
+
* So this is a class with state, and that difference is the provider's, not a
|
|
13
|
+
* design choice worth hiding. The alternative — emitting events with empty ids,
|
|
14
|
+
* or an end event with no reason — would keep the shape uniform by making the
|
|
15
|
+
* contents wrong.
|
|
16
|
+
*/
|
|
17
|
+
export declare class AnthropicStreamMapper {
|
|
18
|
+
#private;
|
|
19
|
+
/**
|
|
20
|
+
* Zero or one event for this payload.
|
|
21
|
+
*
|
|
22
|
+
* `ping` and `content_block_start` for a text block produce nothing a
|
|
23
|
+
* consumer can use; they are dropped rather than surfaced as noise.
|
|
24
|
+
*/
|
|
25
|
+
map(payload: JsonObject): StreamEvent | null;
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=stream-events.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream-events.d.ts","sourceRoot":"","sources":["../../../src/providers/anthropic/stream-events.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,KAAK,UAAU,EAAE,MAAM,eAAe,CAAC;AAK9D,OAAO,EAGL,KAAK,WAAW,EAMjB,MAAM,2BAA2B,CAAC;AAGnC;;;;;;;;;;;;;GAaG;AACH,qBAAa,qBAAqB;;IAUhC;;;;;OAKG;IACH,GAAG,CAAC,OAAO,EAAE,UAAU,GAAG,WAAW,GAAG,IAAI;CAsI7C"}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { isJsonObject } from '../../json.js';
|
|
2
|
+
import { readNullableNumber } from '../../internal/filters.js';
|
|
3
|
+
import { FinishReason } from '../../enums.js';
|
|
4
|
+
import { ToolCall } from '../../value-objects/tool-call.js';
|
|
5
|
+
import { Usage } from '../../value-objects/usage.js';
|
|
6
|
+
import { ErrorEvent, StreamEndEvent, StreamStartEvent, TextCompleteEvent, TextDeltaEvent, TextStartEvent, ToolCallEvent, } from '../../streaming/events.js';
|
|
7
|
+
import { mapFinishReason } from './maps/finish-reason-map.js';
|
|
8
|
+
/**
|
|
9
|
+
* Anthropic's stream, which needs MEMORY where OpenAI's does not.
|
|
10
|
+
*
|
|
11
|
+
* OpenAI repeats the identifiers on every event, so a payload can be mapped on
|
|
12
|
+
* its own and the mapper is a pure function. Anthropic does not: a
|
|
13
|
+
* `content_block_delta` carries only an INDEX, the message id arrived back at
|
|
14
|
+
* `message_start`, the stop reason arrives at `message_delta` and the stream
|
|
15
|
+
* ends at `message_stop`. Mapping one payload in isolation is impossible.
|
|
16
|
+
*
|
|
17
|
+
* So this is a class with state, and that difference is the provider's, not a
|
|
18
|
+
* design choice worth hiding. The alternative — emitting events with empty ids,
|
|
19
|
+
* or an end event with no reason — would keep the shape uniform by making the
|
|
20
|
+
* contents wrong.
|
|
21
|
+
*/
|
|
22
|
+
export class AnthropicStreamMapper {
|
|
23
|
+
#messageId = '';
|
|
24
|
+
#stopReason = {};
|
|
25
|
+
#usage = null;
|
|
26
|
+
/** Accumulated text and tool JSON, per content-block index. */
|
|
27
|
+
#blocks = new Map();
|
|
28
|
+
/**
|
|
29
|
+
* Zero or one event for this payload.
|
|
30
|
+
*
|
|
31
|
+
* `ping` and `content_block_start` for a text block produce nothing a
|
|
32
|
+
* consumer can use; they are dropped rather than surfaced as noise.
|
|
33
|
+
*/
|
|
34
|
+
map(payload) {
|
|
35
|
+
switch (readString(payload.type)) {
|
|
36
|
+
case 'message_start':
|
|
37
|
+
return this.#messageStart(payload);
|
|
38
|
+
case 'content_block_start':
|
|
39
|
+
return this.#blockStart(payload);
|
|
40
|
+
case 'content_block_delta':
|
|
41
|
+
return this.#blockDelta(payload);
|
|
42
|
+
case 'content_block_stop':
|
|
43
|
+
return this.#blockStop(payload);
|
|
44
|
+
case 'message_delta':
|
|
45
|
+
this.#messageDelta(payload);
|
|
46
|
+
return null;
|
|
47
|
+
case 'message_stop':
|
|
48
|
+
return new StreamEndEvent(this.#finishReason(), this.#usage);
|
|
49
|
+
case 'error':
|
|
50
|
+
return new ErrorEvent(readString(readObject(payload.error)?.type) || 'unknown_error', readString(readObject(payload.error)?.message) || 'The provider reported an error mid-stream.');
|
|
51
|
+
default:
|
|
52
|
+
// `ping`, and anything Anthropic adds later. Ignored rather than fatal,
|
|
53
|
+
// for the same reason the OpenAI mapper ignores what it does not know:
|
|
54
|
+
// a provider's additive change must not become an outage.
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
#messageStart(payload) {
|
|
59
|
+
const message = readObject(payload.message);
|
|
60
|
+
this.#messageId = readString(message?.id);
|
|
61
|
+
this.#usage = usage(readObject(message?.usage));
|
|
62
|
+
return new StreamStartEvent(readString(message?.model));
|
|
63
|
+
}
|
|
64
|
+
#blockStart(payload) {
|
|
65
|
+
const index = readNumber(payload.index);
|
|
66
|
+
const block = readObject(payload.content_block);
|
|
67
|
+
const type = readString(block?.type);
|
|
68
|
+
this.#blocks.set(index, {
|
|
69
|
+
type,
|
|
70
|
+
text: '',
|
|
71
|
+
id: readString(block?.id),
|
|
72
|
+
name: readString(block?.name),
|
|
73
|
+
});
|
|
74
|
+
// A text block opening is worth announcing; a tool block opening is not,
|
|
75
|
+
// because the tool call is only meaningful once its arguments have arrived.
|
|
76
|
+
return type === 'text' ? new TextStartEvent(this.#messageId) : null;
|
|
77
|
+
}
|
|
78
|
+
#blockDelta(payload) {
|
|
79
|
+
const index = readNumber(payload.index);
|
|
80
|
+
const delta = readObject(payload.delta);
|
|
81
|
+
const block = this.#blocks.get(index);
|
|
82
|
+
if (block === undefined || delta === null) {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
// Text and tool arguments arrive through the same event with different
|
|
86
|
+
// delta types. Both accumulate; only text is worth emitting per chunk,
|
|
87
|
+
// because a half-parsed JSON fragment is not something a consumer can use.
|
|
88
|
+
if (readString(delta.type) === 'text_delta') {
|
|
89
|
+
const text = readString(delta.text);
|
|
90
|
+
block.text += text;
|
|
91
|
+
return new TextDeltaEvent(text, this.#messageId);
|
|
92
|
+
}
|
|
93
|
+
if (readString(delta.type) === 'input_json_delta') {
|
|
94
|
+
block.text += readString(delta.partial_json);
|
|
95
|
+
}
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
#blockStop(payload) {
|
|
99
|
+
const block = this.#blocks.get(readNumber(payload.index));
|
|
100
|
+
if (block === undefined) {
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
// Truthful because it was accumulated. Anthropic's `content_block_stop`
|
|
104
|
+
// carries no text of its own, so emitting a complete event without the
|
|
105
|
+
// memory above would mean emitting an empty one.
|
|
106
|
+
return block.type === 'text'
|
|
107
|
+
? new TextCompleteEvent(block.text, this.#messageId)
|
|
108
|
+
: new ToolCallEvent(new ToolCall(block.id, block.name, block.text), this.#messageId);
|
|
109
|
+
}
|
|
110
|
+
#messageDelta(payload) {
|
|
111
|
+
const delta = readObject(payload.delta);
|
|
112
|
+
if (delta !== null) {
|
|
113
|
+
this.#stopReason = delta;
|
|
114
|
+
}
|
|
115
|
+
// Anthropic reports output tokens here rather than at message_stop, and
|
|
116
|
+
// reports them CUMULATIVELY, so the last one wins rather than summing.
|
|
117
|
+
const tokens = readObject(payload.usage);
|
|
118
|
+
if (tokens !== null) {
|
|
119
|
+
// Rebuilt rather than mutated, so everything not restated here would be
|
|
120
|
+
// LOST. Prompt tokens were already carried forward for that reason; cache
|
|
121
|
+
// and thinking counts arrive at message_start and never again, so they
|
|
122
|
+
// need the same treatment.
|
|
123
|
+
this.#usage = new Usage(this.#usage?.promptTokens ?? 0, readNumber(tokens.output_tokens), this.#usage?.cacheWriteInputTokens ?? null, this.#usage?.cacheReadInputTokens ?? null, readNullableNumber(readObject(tokens.output_tokens_details)?.thinking_tokens) ??
|
|
124
|
+
this.#usage?.thoughtTokens ??
|
|
125
|
+
null);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
#finishReason() {
|
|
129
|
+
return this.#stopReason.stop_reason === undefined ? FinishReason.Stop : mapFinishReason(this.#stopReason);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
function usage(raw) {
|
|
133
|
+
if (raw === null) {
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
return new Usage(readNumber(raw.input_tokens), readNumber(raw.output_tokens), readNullableNumber(raw.cache_creation_input_tokens), readNullableNumber(raw.cache_read_input_tokens), readNullableNumber(readObject(raw.output_tokens_details)?.thinking_tokens));
|
|
137
|
+
}
|
|
138
|
+
function readObject(value) {
|
|
139
|
+
return isJsonObject(value) ? value : null;
|
|
140
|
+
}
|
|
141
|
+
function readString(value) {
|
|
142
|
+
return typeof value === 'string' ? value : '';
|
|
143
|
+
}
|
|
144
|
+
function readNumber(value) {
|
|
145
|
+
return typeof value === 'number' ? value : 0;
|
|
146
|
+
}
|
|
147
|
+
//# sourceMappingURL=stream-events.js.map
|