@providerprotocol/ai 0.0.37 → 0.0.39
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/README.md +94 -3
- package/dist/anthropic/index.d.ts +3 -2
- package/dist/cerebras/index.d.ts +3 -2
- package/dist/chunk-3Q5VELKG.js +124 -0
- package/dist/chunk-3Q5VELKG.js.map +1 -0
- package/dist/{chunk-LTEMH3CI.js → chunk-5XPRVUOK.js} +6 -4
- package/dist/{chunk-LTEMH3CI.js.map → chunk-5XPRVUOK.js.map} +1 -1
- package/dist/chunk-7ULSRWDH.js +83 -0
- package/dist/chunk-7ULSRWDH.js.map +1 -0
- package/dist/{chunk-YQLR3XOA.js → chunk-BIBMNP7Y.js} +1 -75
- package/dist/chunk-BIBMNP7Y.js.map +1 -0
- package/dist/{chunk-7GTWHZY2.js → chunk-IDZR4ROP.js} +5 -3
- package/dist/{chunk-7GTWHZY2.js.map → chunk-IDZR4ROP.js.map} +1 -1
- package/dist/{chunk-4RX4VQCB.js → chunk-IIMTP3XC.js} +2 -2
- package/dist/{chunk-ZRVNAET3.js → chunk-KNBODIQU.js} +6 -3
- package/dist/chunk-KNBODIQU.js.map +1 -0
- package/dist/{chunk-FYSZFIZS.js → chunk-O32SBS6S.js} +5 -3
- package/dist/{chunk-FYSZFIZS.js.map → chunk-O32SBS6S.js.map} +1 -1
- package/dist/{chunk-IK6NRCW5.js → chunk-RDC5GYST.js} +7 -7
- package/dist/{chunk-5IWHCXKN.js → chunk-SAMIK4WZ.js} +2 -2
- package/dist/{chunk-EPB3GQNL.js → chunk-U6M3MXNI.js} +11 -2
- package/dist/chunk-U6M3MXNI.js.map +1 -0
- package/dist/{chunk-2YXFLRQ6.js → chunk-WNB5PSY6.js} +2 -2
- package/dist/{chunk-CRP6Y7NF.js → chunk-ZDYEDI2A.js} +2 -2
- package/dist/{embedding-CwZ1ZNWv.d.ts → embedding-iNQCeXfk.d.ts} +1 -1
- package/dist/google/index.d.ts +3 -2
- package/dist/groq/index.d.ts +3 -2
- package/dist/http/index.d.ts +4 -3
- package/dist/{image-stream-CeQHtjxS.d.ts → image-stream-ARno6XlS.d.ts} +1 -1
- package/dist/index.d.ts +8 -7
- package/dist/index.js +11 -2
- package/dist/index.js.map +1 -1
- package/dist/{llm-DS_-l71X.d.ts → llm-CZqlijjK.d.ts} +16 -9
- package/dist/middleware/logging/index.d.ts +3 -2
- package/dist/middleware/parsed-object/index.d.ts +3 -2
- package/dist/middleware/persistence/index.d.ts +3 -2
- package/dist/middleware/pubsub/index.d.ts +5 -4
- package/dist/middleware/pubsub/index.js +49 -3
- package/dist/middleware/pubsub/index.js.map +1 -1
- package/dist/middleware/pubsub/server/express/index.d.ts +3 -2
- package/dist/middleware/pubsub/server/express/index.js +2 -2
- package/dist/middleware/pubsub/server/fastify/index.d.ts +3 -2
- package/dist/middleware/pubsub/server/fastify/index.js +2 -2
- package/dist/middleware/pubsub/server/h3/index.d.ts +3 -2
- package/dist/middleware/pubsub/server/h3/index.js +2 -2
- package/dist/middleware/pubsub/server/index.d.ts +3 -2
- package/dist/middleware/pubsub/server/index.js +5 -5
- package/dist/middleware/pubsub/server/webapi/index.d.ts +3 -2
- package/dist/middleware/pubsub/server/webapi/index.js +2 -2
- package/dist/moonshot/index.d.ts +511 -0
- package/dist/moonshot/index.js +1090 -0
- package/dist/moonshot/index.js.map +1 -0
- package/dist/ollama/index.d.ts +3 -2
- package/dist/openai/index.d.ts +3 -2
- package/dist/openrouter/index.d.ts +3 -2
- package/dist/proxy/index.d.ts +5 -4
- package/dist/proxy/index.js +12 -10
- package/dist/proxy/index.js.map +1 -1
- package/dist/proxy/server/express/index.d.ts +5 -4
- package/dist/proxy/server/express/index.js +3 -2
- package/dist/proxy/server/fastify/index.d.ts +5 -4
- package/dist/proxy/server/fastify/index.js +3 -2
- package/dist/proxy/server/h3/index.d.ts +19 -17
- package/dist/proxy/server/h3/index.js +3 -2
- package/dist/proxy/server/index.d.ts +5 -4
- package/dist/proxy/server/index.js +9 -8
- package/dist/proxy/server/webapi/index.d.ts +5 -4
- package/dist/proxy/server/webapi/index.js +3 -2
- package/dist/responses/index.d.ts +3 -2
- package/dist/{retry-CgoBNa51.d.ts → retry-C1eJbEMV.d.ts} +1 -1
- package/dist/{stream-sXhBtWjl.d.ts → stream-DVVUIKpz.d.ts} +3 -416
- package/dist/tool-D22EhP5F.d.ts +507 -0
- package/dist/{types-Cr4F0tVy.d.ts → types-CyXF0J7C.d.ts} +16 -3
- package/dist/utils/index.d.ts +65 -1
- package/dist/utils/index.js +15 -1
- package/dist/xai/index.d.ts +3 -2
- package/package.json +22 -3
- package/dist/chunk-EPB3GQNL.js.map +0 -1
- package/dist/chunk-YQLR3XOA.js.map +0 -1
- package/dist/chunk-ZRVNAET3.js.map +0 -1
- /package/dist/{chunk-4RX4VQCB.js.map → chunk-IIMTP3XC.js.map} +0 -0
- /package/dist/{chunk-IK6NRCW5.js.map → chunk-RDC5GYST.js.map} +0 -0
- /package/dist/{chunk-5IWHCXKN.js.map → chunk-SAMIK4WZ.js.map} +0 -0
- /package/dist/{chunk-2YXFLRQ6.js.map → chunk-WNB5PSY6.js.map} +0 -0
- /package/dist/{chunk-CRP6Y7NF.js.map → chunk-ZDYEDI2A.js.map} +0 -0
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import {
|
|
2
2
|
resolveImageResult,
|
|
3
3
|
serializeImageResult,
|
|
4
|
-
serializeImageStreamEvent
|
|
4
|
+
serializeImageStreamEvent
|
|
5
|
+
} from "./chunk-BIBMNP7Y.js";
|
|
6
|
+
import {
|
|
5
7
|
serializeStreamEvent,
|
|
6
8
|
serializeTurn
|
|
7
|
-
} from "./chunk-
|
|
9
|
+
} from "./chunk-7ULSRWDH.js";
|
|
8
10
|
|
|
9
11
|
// src/providers/proxy/server/fastify.ts
|
|
10
12
|
function sendJSON(turn, reply) {
|
|
@@ -91,4 +93,4 @@ export {
|
|
|
91
93
|
sendError,
|
|
92
94
|
fastify
|
|
93
95
|
};
|
|
94
|
-
//# sourceMappingURL=chunk-
|
|
96
|
+
//# sourceMappingURL=chunk-IDZR4ROP.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/providers/proxy/server/fastify.ts"],"sourcesContent":["/**\n * @fileoverview Fastify adapter for proxy server.\n *\n * Provides utilities for using PP proxy with Fastify servers.\n * These adapters convert PP types to Fastify-compatible responses.\n *\n * @module providers/proxy/server/fastify\n */\n\nimport type { Turn } from '../../../types/turn.ts';\nimport type { StreamResult } from '../../../types/stream.ts';\nimport type { EmbeddingResult } from '../../../types/embedding.ts';\nimport type { ImageResult } from '../../../types/image.ts';\nimport { serializeTurn, serializeStreamEvent } from '../serialization.ts';\nimport { serializeImageResult, serializeImageStreamEvent } from '../serialization.media.ts';\nimport { resolveImageResult, type ImageStreamLike } from './image-stream.ts';\n\n/**\n * Fastify Reply interface (minimal type to avoid dependency).\n */\ninterface FastifyReply {\n header(name: string, value: string): FastifyReply;\n status(code: number): FastifyReply;\n send(payload: unknown): FastifyReply;\n raw: {\n write(chunk: string): boolean;\n end(): void;\n };\n}\n\n/**\n * Send a Turn as JSON response.\n *\n * @param turn - The completed inference turn\n * @param reply - Fastify reply object\n *\n * @example\n * ```typescript\n * const turn = await instance.generate(messages);\n * return fastifyAdapter.sendJSON(turn, reply);\n * ```\n */\nexport function sendJSON(turn: Turn, reply: FastifyReply): FastifyReply {\n return reply\n .header('Content-Type', 'application/json')\n .send(serializeTurn(turn));\n}\n\n/**\n * Send an EmbeddingResult as JSON response.\n *\n * @param result - The embedding result\n * @param reply - Fastify reply object\n */\nexport function sendEmbeddingJSON(result: EmbeddingResult, reply: FastifyReply): FastifyReply {\n return reply\n .header('Content-Type', 'application/json')\n .send(result);\n}\n\n/**\n * Send an ImageResult as JSON response.\n *\n * @param result - The image result\n * @param reply - Fastify reply object\n */\nexport function sendImageJSON(result: ImageResult, reply: FastifyReply): FastifyReply {\n return reply\n .header('Content-Type', 'application/json')\n .send(serializeImageResult(result));\n}\n\n/**\n * Stream a StreamResult as Server-Sent Events.\n *\n * @param stream - The StreamResult from instance.stream()\n * @param reply - Fastify reply object\n *\n * @example\n * ```typescript\n * const stream = instance.stream(messages);\n * return fastifyAdapter.streamSSE(stream, reply);\n * ```\n */\nexport function streamSSE(stream: StreamResult, reply: FastifyReply): FastifyReply {\n reply\n .header('Content-Type', 'text/event-stream')\n .header('Cache-Control', 'no-cache')\n .header('Connection', 'keep-alive');\n\n const raw = reply.raw;\n\n (async () => {\n try {\n for await (const event of stream) {\n const serialized = serializeStreamEvent(event);\n raw.write(`data: ${JSON.stringify(serialized)}\\n\\n`);\n }\n\n const turn = await stream.turn;\n raw.write(`data: ${JSON.stringify(serializeTurn(turn))}\\n\\n`);\n raw.write('data: [DONE]\\n\\n');\n } catch (error) {\n const message = error instanceof Error ? error.message : String(error);\n raw.write(`data: ${JSON.stringify({ error: message })}\\n\\n`);\n } finally {\n raw.end();\n }\n })();\n\n return reply;\n}\n\n/**\n * Stream an ImageStreamResult as Server-Sent Events.\n *\n * @param stream - The ImageStreamResult or ImageProviderStreamResult from image().stream()\n * @param reply - Fastify reply object\n */\nexport function streamImageSSE(stream: ImageStreamLike, reply: FastifyReply): FastifyReply {\n reply\n .header('Content-Type', 'text/event-stream')\n .header('Cache-Control', 'no-cache')\n .header('Connection', 'keep-alive');\n\n const raw = reply.raw;\n\n (async () => {\n try {\n for await (const event of stream) {\n const serialized = serializeImageStreamEvent(event);\n raw.write(`data: ${JSON.stringify(serialized)}\\n\\n`);\n }\n\n const result = await resolveImageResult(stream);\n raw.write(`data: ${JSON.stringify(serializeImageResult(result))}\\n\\n`);\n raw.write('data: [DONE]\\n\\n');\n } catch (error) {\n const message = error instanceof Error ? error.message : String(error);\n raw.write(`data: ${JSON.stringify({ error: message })}\\n\\n`);\n } finally {\n raw.end();\n }\n })();\n\n return reply;\n}\n\n/**\n * Send an error response.\n *\n * @param message - Error message\n * @param status - HTTP status code\n * @param reply - Fastify reply object\n */\nexport function sendError(message: string, status: number, reply: FastifyReply): FastifyReply {\n return reply.status(status).send({ error: message });\n}\n\n/**\n * Fastify adapter utilities.\n *\n * @example Basic usage\n * ```typescript\n * import Fastify from 'fastify';\n * import { llm } from '@providerprotocol/ai';\n * import { anthropic } from '@providerprotocol/ai/anthropic';\n * import { parseBody } from '@providerprotocol/ai/proxy';\n * import { fastify as fastifyAdapter } from '@providerprotocol/ai/proxy/server';\n *\n * const app = Fastify();\n *\n * app.post('/api/ai', async (request, reply) => {\n * const { messages, system, params } = parseBody(request.body);\n * const instance = llm({ model: anthropic('claude-sonnet-4-20250514'), system });\n *\n * if (request.headers.accept?.includes('text/event-stream')) {\n * return fastifyAdapter.streamSSE(instance.stream(messages), reply);\n * } else {\n * const turn = await instance.generate(messages);\n * return fastifyAdapter.sendJSON(turn, reply);\n * }\n * });\n * ```\n *\n * @example API Gateway with authentication\n * ```typescript\n * import Fastify from 'fastify';\n * import { llm } from '@providerprotocol/ai';\n * import { anthropic } from '@providerprotocol/ai/anthropic';\n * import { ExponentialBackoff, RoundRobinKeys } from '@providerprotocol/ai/http';\n * import { parseBody } from '@providerprotocol/ai/proxy';\n * import { fastify as fastifyAdapter } from '@providerprotocol/ai/proxy/server';\n *\n * const app = Fastify();\n *\n * // Server manages AI provider keys - users never see them\n * const claude = llm({\n * model: anthropic('claude-sonnet-4-20250514'),\n * config: {\n * apiKey: new RoundRobinKeys([process.env.ANTHROPIC_KEY_1!, process.env.ANTHROPIC_KEY_2!]),\n * retryStrategy: new ExponentialBackoff({ maxAttempts: 3 }),\n * },\n * });\n *\n * // Auth hook for your platform\n * app.addHook('preHandler', async (request, reply) => {\n * const token = request.headers.authorization?.replace('Bearer ', '');\n * const user = await validatePlatformToken(token);\n * if (!user) {\n * reply.status(401).send({ error: 'Unauthorized' });\n * return;\n * }\n * request.user = user;\n * });\n *\n * app.post('/api/ai', async (request, reply) => {\n * // Track usage per user\n * // await trackUsage(request.user.id);\n *\n * const { messages, system, params } = parseBody(request.body);\n *\n * if (params?.stream) {\n * return fastifyAdapter.streamSSE(claude.stream(messages, { system }), reply);\n * }\n * const turn = await claude.generate(messages, { system });\n * return fastifyAdapter.sendJSON(turn, reply);\n * });\n * ```\n */\nexport const fastify = {\n sendJSON,\n sendEmbeddingJSON,\n sendImageJSON,\n streamSSE,\n streamImageSSE,\n sendError,\n};\n"],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../src/providers/proxy/server/fastify.ts"],"sourcesContent":["/**\n * @fileoverview Fastify adapter for proxy server.\n *\n * Provides utilities for using PP proxy with Fastify servers.\n * These adapters convert PP types to Fastify-compatible responses.\n *\n * @module providers/proxy/server/fastify\n */\n\nimport type { Turn } from '../../../types/turn.ts';\nimport type { StreamResult } from '../../../types/stream.ts';\nimport type { EmbeddingResult } from '../../../types/embedding.ts';\nimport type { ImageResult } from '../../../types/image.ts';\nimport { serializeTurn, serializeStreamEvent } from '../serialization.ts';\nimport { serializeImageResult, serializeImageStreamEvent } from '../serialization.media.ts';\nimport { resolveImageResult, type ImageStreamLike } from './image-stream.ts';\n\n/**\n * Fastify Reply interface (minimal type to avoid dependency).\n */\ninterface FastifyReply {\n header(name: string, value: string): FastifyReply;\n status(code: number): FastifyReply;\n send(payload: unknown): FastifyReply;\n raw: {\n write(chunk: string): boolean;\n end(): void;\n };\n}\n\n/**\n * Send a Turn as JSON response.\n *\n * @param turn - The completed inference turn\n * @param reply - Fastify reply object\n *\n * @example\n * ```typescript\n * const turn = await instance.generate(messages);\n * return fastifyAdapter.sendJSON(turn, reply);\n * ```\n */\nexport function sendJSON(turn: Turn, reply: FastifyReply): FastifyReply {\n return reply\n .header('Content-Type', 'application/json')\n .send(serializeTurn(turn));\n}\n\n/**\n * Send an EmbeddingResult as JSON response.\n *\n * @param result - The embedding result\n * @param reply - Fastify reply object\n */\nexport function sendEmbeddingJSON(result: EmbeddingResult, reply: FastifyReply): FastifyReply {\n return reply\n .header('Content-Type', 'application/json')\n .send(result);\n}\n\n/**\n * Send an ImageResult as JSON response.\n *\n * @param result - The image result\n * @param reply - Fastify reply object\n */\nexport function sendImageJSON(result: ImageResult, reply: FastifyReply): FastifyReply {\n return reply\n .header('Content-Type', 'application/json')\n .send(serializeImageResult(result));\n}\n\n/**\n * Stream a StreamResult as Server-Sent Events.\n *\n * @param stream - The StreamResult from instance.stream()\n * @param reply - Fastify reply object\n *\n * @example\n * ```typescript\n * const stream = instance.stream(messages);\n * return fastifyAdapter.streamSSE(stream, reply);\n * ```\n */\nexport function streamSSE(stream: StreamResult, reply: FastifyReply): FastifyReply {\n reply\n .header('Content-Type', 'text/event-stream')\n .header('Cache-Control', 'no-cache')\n .header('Connection', 'keep-alive');\n\n const raw = reply.raw;\n\n (async () => {\n try {\n for await (const event of stream) {\n const serialized = serializeStreamEvent(event);\n raw.write(`data: ${JSON.stringify(serialized)}\\n\\n`);\n }\n\n const turn = await stream.turn;\n raw.write(`data: ${JSON.stringify(serializeTurn(turn))}\\n\\n`);\n raw.write('data: [DONE]\\n\\n');\n } catch (error) {\n const message = error instanceof Error ? error.message : String(error);\n raw.write(`data: ${JSON.stringify({ error: message })}\\n\\n`);\n } finally {\n raw.end();\n }\n })();\n\n return reply;\n}\n\n/**\n * Stream an ImageStreamResult as Server-Sent Events.\n *\n * @param stream - The ImageStreamResult or ImageProviderStreamResult from image().stream()\n * @param reply - Fastify reply object\n */\nexport function streamImageSSE(stream: ImageStreamLike, reply: FastifyReply): FastifyReply {\n reply\n .header('Content-Type', 'text/event-stream')\n .header('Cache-Control', 'no-cache')\n .header('Connection', 'keep-alive');\n\n const raw = reply.raw;\n\n (async () => {\n try {\n for await (const event of stream) {\n const serialized = serializeImageStreamEvent(event);\n raw.write(`data: ${JSON.stringify(serialized)}\\n\\n`);\n }\n\n const result = await resolveImageResult(stream);\n raw.write(`data: ${JSON.stringify(serializeImageResult(result))}\\n\\n`);\n raw.write('data: [DONE]\\n\\n');\n } catch (error) {\n const message = error instanceof Error ? error.message : String(error);\n raw.write(`data: ${JSON.stringify({ error: message })}\\n\\n`);\n } finally {\n raw.end();\n }\n })();\n\n return reply;\n}\n\n/**\n * Send an error response.\n *\n * @param message - Error message\n * @param status - HTTP status code\n * @param reply - Fastify reply object\n */\nexport function sendError(message: string, status: number, reply: FastifyReply): FastifyReply {\n return reply.status(status).send({ error: message });\n}\n\n/**\n * Fastify adapter utilities.\n *\n * @example Basic usage\n * ```typescript\n * import Fastify from 'fastify';\n * import { llm } from '@providerprotocol/ai';\n * import { anthropic } from '@providerprotocol/ai/anthropic';\n * import { parseBody } from '@providerprotocol/ai/proxy';\n * import { fastify as fastifyAdapter } from '@providerprotocol/ai/proxy/server';\n *\n * const app = Fastify();\n *\n * app.post('/api/ai', async (request, reply) => {\n * const { messages, system, params } = parseBody(request.body);\n * const instance = llm({ model: anthropic('claude-sonnet-4-20250514'), system });\n *\n * if (request.headers.accept?.includes('text/event-stream')) {\n * return fastifyAdapter.streamSSE(instance.stream(messages), reply);\n * } else {\n * const turn = await instance.generate(messages);\n * return fastifyAdapter.sendJSON(turn, reply);\n * }\n * });\n * ```\n *\n * @example API Gateway with authentication\n * ```typescript\n * import Fastify from 'fastify';\n * import { llm } from '@providerprotocol/ai';\n * import { anthropic } from '@providerprotocol/ai/anthropic';\n * import { ExponentialBackoff, RoundRobinKeys } from '@providerprotocol/ai/http';\n * import { parseBody } from '@providerprotocol/ai/proxy';\n * import { fastify as fastifyAdapter } from '@providerprotocol/ai/proxy/server';\n *\n * const app = Fastify();\n *\n * // Server manages AI provider keys - users never see them\n * const claude = llm({\n * model: anthropic('claude-sonnet-4-20250514'),\n * config: {\n * apiKey: new RoundRobinKeys([process.env.ANTHROPIC_KEY_1!, process.env.ANTHROPIC_KEY_2!]),\n * retryStrategy: new ExponentialBackoff({ maxAttempts: 3 }),\n * },\n * });\n *\n * // Auth hook for your platform\n * app.addHook('preHandler', async (request, reply) => {\n * const token = request.headers.authorization?.replace('Bearer ', '');\n * const user = await validatePlatformToken(token);\n * if (!user) {\n * reply.status(401).send({ error: 'Unauthorized' });\n * return;\n * }\n * request.user = user;\n * });\n *\n * app.post('/api/ai', async (request, reply) => {\n * // Track usage per user\n * // await trackUsage(request.user.id);\n *\n * const { messages, system, params } = parseBody(request.body);\n *\n * if (params?.stream) {\n * return fastifyAdapter.streamSSE(claude.stream(messages, { system }), reply);\n * }\n * const turn = await claude.generate(messages, { system });\n * return fastifyAdapter.sendJSON(turn, reply);\n * });\n * ```\n */\nexport const fastify = {\n sendJSON,\n sendEmbeddingJSON,\n sendImageJSON,\n streamSSE,\n streamImageSSE,\n sendError,\n};\n"],"mappings":";;;;;;;;;;;AA0CO,SAAS,SAAS,MAAY,OAAmC;AACtE,SAAO,MACJ,OAAO,gBAAgB,kBAAkB,EACzC,KAAK,cAAc,IAAI,CAAC;AAC7B;AAQO,SAAS,kBAAkB,QAAyB,OAAmC;AAC5F,SAAO,MACJ,OAAO,gBAAgB,kBAAkB,EACzC,KAAK,MAAM;AAChB;AAQO,SAAS,cAAc,QAAqB,OAAmC;AACpF,SAAO,MACJ,OAAO,gBAAgB,kBAAkB,EACzC,KAAK,qBAAqB,MAAM,CAAC;AACtC;AAcO,SAAS,UAAU,QAAsB,OAAmC;AACjF,QACG,OAAO,gBAAgB,mBAAmB,EAC1C,OAAO,iBAAiB,UAAU,EAClC,OAAO,cAAc,YAAY;AAEpC,QAAM,MAAM,MAAM;AAElB,GAAC,YAAY;AACX,QAAI;AACF,uBAAiB,SAAS,QAAQ;AAChC,cAAM,aAAa,qBAAqB,KAAK;AAC7C,YAAI,MAAM,SAAS,KAAK,UAAU,UAAU,CAAC;AAAA;AAAA,CAAM;AAAA,MACrD;AAEA,YAAM,OAAO,MAAM,OAAO;AAC1B,UAAI,MAAM,SAAS,KAAK,UAAU,cAAc,IAAI,CAAC,CAAC;AAAA;AAAA,CAAM;AAC5D,UAAI,MAAM,kBAAkB;AAAA,IAC9B,SAAS,OAAO;AACd,YAAM,UAAU,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK;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/middleware/pubsub/server/h3.ts
|
|
6
6
|
async function streamSubscriber(streamId, adapter, event) {
|
|
@@ -28,4 +28,4 @@ export {
|
|
|
28
28
|
streamSubscriber,
|
|
29
29
|
h3
|
|
30
30
|
};
|
|
31
|
-
//# sourceMappingURL=chunk-
|
|
31
|
+
//# sourceMappingURL=chunk-IIMTP3XC.js.map
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import {
|
|
2
2
|
resolveImageResult,
|
|
3
3
|
serializeImageResult,
|
|
4
|
-
serializeImageStreamEvent
|
|
4
|
+
serializeImageStreamEvent
|
|
5
|
+
} from "./chunk-BIBMNP7Y.js";
|
|
6
|
+
import {
|
|
5
7
|
serializeStreamEvent,
|
|
6
8
|
serializeTurn
|
|
7
|
-
} from "./chunk-
|
|
9
|
+
} from "./chunk-7ULSRWDH.js";
|
|
8
10
|
|
|
9
11
|
// src/providers/proxy/server/h3.ts
|
|
10
12
|
function sendJSON(turn, event) {
|
|
@@ -130,6 +132,7 @@ function createImageSSEStream(stream) {
|
|
|
130
132
|
});
|
|
131
133
|
}
|
|
132
134
|
function sendError(message, status, event) {
|
|
135
|
+
event.node.res.statusCode = status;
|
|
133
136
|
return { error: message, statusCode: status };
|
|
134
137
|
}
|
|
135
138
|
var h3 = {
|
|
@@ -154,4 +157,4 @@ export {
|
|
|
154
157
|
sendError,
|
|
155
158
|
h3
|
|
156
159
|
};
|
|
157
|
-
//# sourceMappingURL=chunk-
|
|
160
|
+
//# sourceMappingURL=chunk-KNBODIQU.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/providers/proxy/server/h3.ts"],"sourcesContent":["/**\n * @fileoverview H3/Nitro/Nuxt adapter for proxy server.\n *\n * Provides utilities for using PP proxy with H3-based servers\n * (Nuxt, Nitro, or standalone H3).\n *\n * @module providers/proxy/server/h3\n */\n\nimport type { Turn } from '../../../types/turn.ts';\nimport type { StreamResult } from '../../../types/stream.ts';\nimport type { EmbeddingResult } from '../../../types/embedding.ts';\nimport type { ImageResult } from '../../../types/image.ts';\nimport { serializeTurn, serializeStreamEvent } from '../serialization.ts';\nimport { serializeImageResult, serializeImageStreamEvent } from '../serialization.media.ts';\nimport { resolveImageResult, type ImageStreamLike } from './image-stream.ts';\n\n/**\n * H3 Event interface (minimal type to avoid dependency).\n */\ninterface H3Event {\n node: {\n res: {\n statusCode: number;\n setHeader(name: string, value: string): void;\n write(chunk: string): boolean;\n end(): void;\n };\n };\n}\n\n/**\n * Send a Turn as JSON response.\n *\n * @param turn - The completed inference turn\n * @param event - H3 event object\n * @returns Serialized turn data\n *\n * @example\n * ```typescript\n * const turn = await instance.generate(messages);\n * return h3Adapter.sendJSON(turn, event);\n * ```\n */\nexport function sendJSON(turn: Turn, event: H3Event): unknown {\n event.node.res.setHeader('Content-Type', 'application/json');\n return serializeTurn(turn);\n}\n\n/**\n * Send an EmbeddingResult as JSON response.\n *\n * @param result - The embedding result\n * @param event - H3 event object\n * @returns Serialized result data\n */\nexport function sendEmbeddingJSON(result: EmbeddingResult, event: H3Event): unknown {\n event.node.res.setHeader('Content-Type', 'application/json');\n return result;\n}\n\n/**\n * Send an ImageResult as JSON response.\n *\n * @param result - The image result\n * @param event - H3 event object\n * @returns Serialized image result data\n */\nexport function sendImageJSON(result: ImageResult, event: H3Event): unknown {\n event.node.res.setHeader('Content-Type', 'application/json');\n return serializeImageResult(result);\n}\n\n/**\n * Stream a StreamResult as Server-Sent Events.\n *\n * Note: For better H3/Nuxt integration, prefer using `createSSEStream` with `sendStream`:\n * ```typescript\n * import { sendStream } from 'h3';\n * return sendStream(event, h3Adapter.createSSEStream(stream));\n * ```\n *\n * @param stream - The StreamResult from instance.stream()\n * @param event - H3 event object\n */\nexport function streamSSE(stream: StreamResult, event: H3Event): void {\n const res = event.node.res;\n res.setHeader('Content-Type', 'text/event-stream');\n res.setHeader('Cache-Control', 'no-cache');\n res.setHeader('Connection', 'keep-alive');\n\n (async () => {\n try {\n for await (const evt of stream) {\n const serialized = serializeStreamEvent(evt);\n res.write(`data: ${JSON.stringify(serialized)}\\n\\n`);\n }\n\n const turn = await stream.turn;\n res.write(`data: ${JSON.stringify(serializeTurn(turn))}\\n\\n`);\n res.write('data: [DONE]\\n\\n');\n } catch (error) {\n const message = error instanceof Error ? error.message : String(error);\n res.write(`data: ${JSON.stringify({ error: message })}\\n\\n`);\n } finally {\n res.end();\n }\n })();\n}\n\n/**\n * Stream an ImageStreamResult as Server-Sent Events.\n *\n * @param stream - The ImageStreamResult or ImageProviderStreamResult from image().stream()\n * @param event - H3 event object\n */\nexport function streamImageSSE(stream: ImageStreamLike, event: H3Event): void {\n const res = event.node.res;\n res.setHeader('Content-Type', 'text/event-stream');\n res.setHeader('Cache-Control', 'no-cache');\n res.setHeader('Connection', 'keep-alive');\n\n (async () => {\n try {\n for await (const evt of stream) {\n const serialized = serializeImageStreamEvent(evt);\n res.write(`data: ${JSON.stringify(serialized)}\\n\\n`);\n }\n\n const result = await resolveImageResult(stream);\n res.write(`data: ${JSON.stringify(serializeImageResult(result))}\\n\\n`);\n res.write('data: [DONE]\\n\\n');\n } catch (error) {\n const message = error instanceof Error ? error.message : String(error);\n res.write(`data: ${JSON.stringify({ error: message })}\\n\\n`);\n } finally {\n res.end();\n }\n })();\n}\n\n/**\n * Create a ReadableStream for H3's sendStream utility.\n *\n * Use this with H3's sendStream for better integration:\n * ```typescript\n * import { sendStream } from 'h3';\n * return sendStream(event, h3Adapter.createSSEStream(stream));\n * ```\n *\n * @param stream - The StreamResult from instance.stream()\n * @returns A ReadableStream of SSE data\n */\nexport function createSSEStream(stream: StreamResult): ReadableStream<Uint8Array> {\n const encoder = new TextEncoder();\n\n return new ReadableStream({\n async start(controller) {\n try {\n for await (const event of stream) {\n const serialized = serializeStreamEvent(event);\n controller.enqueue(encoder.encode(`data: ${JSON.stringify(serialized)}\\n\\n`));\n }\n\n const turn = await stream.turn;\n controller.enqueue(encoder.encode(`data: ${JSON.stringify(serializeTurn(turn))}\\n\\n`));\n controller.enqueue(encoder.encode('data: [DONE]\\n\\n'));\n } catch (error) {\n const message = error instanceof Error ? error.message : String(error);\n controller.enqueue(encoder.encode(`data: ${JSON.stringify({ error: message })}\\n\\n`));\n } finally {\n controller.close();\n }\n },\n });\n}\n\n/**\n * Create a ReadableStream for image SSE data.\n *\n * @param stream - The ImageStreamResult or ImageProviderStreamResult from image().stream()\n * @returns A ReadableStream of SSE data\n */\nexport function createImageSSEStream(stream: ImageStreamLike): ReadableStream<Uint8Array> {\n const encoder = new TextEncoder();\n\n return new ReadableStream({\n async start(controller) {\n try {\n for await (const event of stream) {\n const serialized = serializeImageStreamEvent(event);\n controller.enqueue(encoder.encode(`data: ${JSON.stringify(serialized)}\\n\\n`));\n }\n\n const result = await resolveImageResult(stream);\n controller.enqueue(encoder.encode(`data: ${JSON.stringify(serializeImageResult(result))}\\n\\n`));\n controller.enqueue(encoder.encode('data: [DONE]\\n\\n'));\n } catch (error) {\n const message = error instanceof Error ? error.message : String(error);\n controller.enqueue(encoder.encode(`data: ${JSON.stringify({ error: message })}\\n\\n`));\n } finally {\n controller.close();\n }\n },\n });\n}\n\n/**\n * Send an error response with proper HTTP status.\n *\n * @param message - Error message\n * @param status - HTTP status code\n * @param event - H3 event object\n * @returns Error object for H3 to serialize\n */\nexport function sendError(message: string, status: number, event: H3Event): { error: string; statusCode: number } {\n event.node.res.statusCode = status;\n return { error: message, statusCode: status };\n}\n\n/**\n * H3/Nitro/Nuxt adapter utilities.\n *\n * @example Basic usage\n * ```typescript\n * // Nuxt server route: server/api/ai.post.ts\n * import { sendStream } from 'h3';\n * import { llm } from '@providerprotocol/ai';\n * import { anthropic } from '@providerprotocol/ai/anthropic';\n * import { parseBody, h3 as h3Adapter } from '@providerprotocol/ai/proxy';\n *\n * export default defineEventHandler(async (event) => {\n * const body = await readBody(event);\n * const { messages, system, params } = parseBody(body);\n * const instance = llm({ model: anthropic('claude-sonnet-4-20250514'), system });\n *\n * const wantsStream = getHeader(event, 'accept')?.includes('text/event-stream');\n * if (wantsStream) {\n * return sendStream(event, h3Adapter.createSSEStream(instance.stream(messages)));\n * } else {\n * const turn = await instance.generate(messages);\n * return h3Adapter.sendJSON(turn, event);\n * }\n * });\n * ```\n *\n * @example API Gateway with authentication (Nuxt)\n * ```typescript\n * // server/api/ai.post.ts\n * import { sendStream } from 'h3';\n * import { llm } from '@providerprotocol/ai';\n * import { anthropic } from '@providerprotocol/ai/anthropic';\n * import { ExponentialBackoff, RoundRobinKeys } from '@providerprotocol/ai/http';\n * import { parseBody, h3 as h3Adapter } from '@providerprotocol/ai/proxy';\n *\n * // Server manages AI provider keys - users never see them\n * const claude = llm({\n * model: anthropic('claude-sonnet-4-20250514'),\n * config: {\n * apiKey: new RoundRobinKeys([\n * process.env.ANTHROPIC_KEY_1!,\n * process.env.ANTHROPIC_KEY_2!,\n * ]),\n * retryStrategy: new ExponentialBackoff({ maxAttempts: 3 }),\n * },\n * });\n *\n * export default defineEventHandler(async (event) => {\n * // Authenticate with your platform credentials\n * const token = getHeader(event, 'authorization')?.replace('Bearer ', '');\n * const user = await validatePlatformToken(token);\n * if (!user) {\n * throw createError({ statusCode: 401, message: 'Unauthorized' });\n * }\n *\n * // Track usage per user\n * // await trackUsage(user.id);\n *\n * const body = await readBody(event);\n * const { messages, system, params } = parseBody(body);\n *\n * if (params?.stream) {\n * return sendStream(event, h3Adapter.createSSEStream(claude.stream(messages, { system })));\n * }\n * const turn = await claude.generate(messages, { system });\n * return h3Adapter.sendJSON(turn, event);\n * });\n * ```\n */\nexport const h3 = {\n sendJSON,\n sendEmbeddingJSON,\n sendImageJSON,\n streamSSE,\n streamImageSSE,\n createSSEStream,\n createImageSSEStream,\n sendError,\n};\n"],"mappings":";;;;;;;;;;;AA4CO,SAAS,SAAS,MAAY,OAAyB;AAC5D,QAAM,KAAK,IAAI,UAAU,gBAAgB,kBAAkB;AAC3D,SAAO,cAAc,IAAI;AAC3B;AASO,SAAS,kBAAkB,QAAyB,OAAyB;AAClF,QAAM,KAA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ChC,gBAAM,aAAa,0BAA0B,KAAK;AAClD,qBAAW,QAAQ,QAAQ,OAAO,SAAS,KAAK,UAAU,UAAU,CAAC;AAAA;AAAA,CAAM,CAAC;AAAA,QAC9E;AAEA,cAAM,SAAS,MAAM,mBAAmB,MAAM;AAC9C,mBAAW,QAAQ,QAAQ,OAAO,SAAS,KAAK,UAAU,qBAAqB,MAAM,CAAC,CAAC;AAAA;AAAA,CAAM,CAAC;AAC9F,mBAAW,QAAQ,QAAQ,OAAO,kBAAkB,CAAC;AAAA,MACvD,SAAS,OAAO;AACd,cAAM,UAAU,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK;AACrE,mBAAW,QAAQ,QAAQ,OAAO,SAAS,KAAK,UAAU,EAAE,OAAO,QAAQ,CAAC,CAAC;AAAA;AAAA,CAAM,CAAC;AAAA,MACtF,UAAE;AACA,mBAAW,MAAM;AAAA,MACnB;AAAA,IACF;AAAA,EACF,CAAC;AACH;AAUO,SAAS,UAAU,SAAiB,QAAgB,OAAuD;AAChH,QAAM,KAAK,IAAI,aAAa;AAC5B,SAAO,EAAE,OAAO,SAAS,YAAY,OAAO;AAC9C;AAuEO,IAAM,KAAK;AAAA,EAChB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;","names":[]}
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import {
|
|
2
2
|
resolveImageResult,
|
|
3
3
|
serializeImageResult,
|
|
4
|
-
serializeImageStreamEvent
|
|
4
|
+
serializeImageStreamEvent
|
|
5
|
+
} from "./chunk-BIBMNP7Y.js";
|
|
6
|
+
import {
|
|
5
7
|
serializeStreamEvent,
|
|
6
8
|
serializeTurn
|
|
7
|
-
} from "./chunk-
|
|
9
|
+
} from "./chunk-7ULSRWDH.js";
|
|
8
10
|
|
|
9
11
|
// src/providers/proxy/server/express.ts
|
|
10
12
|
function sendJSON(turn, res) {
|
|
@@ -94,4 +96,4 @@ export {
|
|
|
94
96
|
sendError,
|
|
95
97
|
express
|
|
96
98
|
};
|
|
97
|
-
//# sourceMappingURL=chunk-
|
|
99
|
+
//# sourceMappingURL=chunk-O32SBS6S.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/providers/proxy/server/express.ts"],"sourcesContent":["/**\n * @fileoverview Express/Connect adapter for proxy server.\n *\n * Provides utilities for using PP proxy with Express.js or Connect-based servers.\n * These adapters convert PP types to Express-compatible responses.\n *\n * @module providers/proxy/server/express\n */\n\nimport type { Turn } from '../../../types/turn.ts';\nimport type { StreamResult } from '../../../types/stream.ts';\nimport type { EmbeddingResult } from '../../../types/embedding.ts';\nimport type { ImageResult } from '../../../types/image.ts';\nimport { serializeTurn, serializeStreamEvent } from '../serialization.ts';\nimport { serializeImageResult, serializeImageStreamEvent } from '../serialization.media.ts';\nimport { resolveImageResult, type ImageStreamLike } from './image-stream.ts';\n\n/**\n * Express Response interface (minimal type to avoid dependency).\n */\ninterface ExpressResponse {\n setHeader(name: string, value: string): void;\n status(code: number): ExpressResponse;\n write(chunk: string): boolean;\n end(): void;\n json(body: unknown): void;\n}\n\n/**\n * Send a Turn as JSON response.\n *\n * @param turn - The completed inference turn\n * @param res - Express response object\n *\n * @example\n * ```typescript\n * const turn = await instance.generate(messages);\n * expressAdapter.sendJSON(turn, res);\n * ```\n */\nexport function sendJSON(turn: Turn, res: ExpressResponse): void {\n res.setHeader('Content-Type', 'application/json');\n res.json(serializeTurn(turn));\n}\n\n/**\n * Send an EmbeddingResult as JSON response.\n *\n * @param result - The embedding result\n * @param res - Express response object\n */\nexport function sendEmbeddingJSON(result: EmbeddingResult, res: ExpressResponse): void {\n res.setHeader('Content-Type', 'application/json');\n res.json(result);\n}\n\n/**\n * Send an ImageResult as JSON response.\n *\n * @param result - The image result\n * @param res - Express response object\n */\nexport function sendImageJSON(result: ImageResult, res: ExpressResponse): void {\n res.setHeader('Content-Type', 'application/json');\n res.json(serializeImageResult(result));\n}\n\n/**\n * Stream a StreamResult as Server-Sent Events.\n *\n * @param stream - The StreamResult from instance.stream()\n * @param res - Express response object\n *\n * @example\n * ```typescript\n * const stream = instance.stream(messages);\n * expressAdapter.streamSSE(stream, res);\n * ```\n */\nexport function streamSSE(stream: StreamResult, res: ExpressResponse): void {\n res.setHeader('Content-Type', 'text/event-stream');\n res.setHeader('Cache-Control', 'no-cache');\n res.setHeader('Connection', 'keep-alive');\n\n (async () => {\n try {\n for await (const event of stream) {\n const serialized = serializeStreamEvent(event);\n res.write(`data: ${JSON.stringify(serialized)}\\n\\n`);\n }\n\n const turn = await stream.turn;\n res.write(`data: ${JSON.stringify(serializeTurn(turn))}\\n\\n`);\n res.write('data: [DONE]\\n\\n');\n } catch (error) {\n const message = error instanceof Error ? error.message : String(error);\n res.write(`data: ${JSON.stringify({ error: message })}\\n\\n`);\n } finally {\n res.end();\n }\n })();\n}\n\n/**\n * Stream an ImageStreamResult as Server-Sent Events.\n *\n * @param stream - The ImageStreamResult or ImageProviderStreamResult from image().stream()\n * @param res - Express response object\n */\nexport function streamImageSSE(stream: ImageStreamLike, res: ExpressResponse): void {\n res.setHeader('Content-Type', 'text/event-stream');\n res.setHeader('Cache-Control', 'no-cache');\n res.setHeader('Connection', 'keep-alive');\n\n (async () => {\n try {\n for await (const event of stream) {\n const serialized = serializeImageStreamEvent(event);\n res.write(`data: ${JSON.stringify(serialized)}\\n\\n`);\n }\n\n const result = await resolveImageResult(stream);\n res.write(`data: ${JSON.stringify(serializeImageResult(result))}\\n\\n`);\n res.write('data: [DONE]\\n\\n');\n } catch (error) {\n const message = error instanceof Error ? error.message : String(error);\n res.write(`data: ${JSON.stringify({ error: message })}\\n\\n`);\n } finally {\n res.end();\n }\n })();\n}\n\n/**\n * Send an error response.\n *\n * @param message - Error message\n * @param status - HTTP status code\n * @param res - Express response object\n */\nexport function sendError(message: string, status: number, res: ExpressResponse): void {\n res.status(status).json({ error: message });\n}\n\n/**\n * Express adapter utilities.\n *\n * @example Basic usage\n * ```typescript\n * import express from 'express';\n * import { llm } from '@providerprotocol/ai';\n * import { anthropic } from '@providerprotocol/ai/anthropic';\n * import { parseBody } from '@providerprotocol/ai/proxy';\n * import { express as expressAdapter } from '@providerprotocol/ai/proxy/server';\n *\n * const app = express();\n * app.use(express.json());\n *\n * app.post('/api/ai', async (req, res) => {\n * const { messages, system, params } = parseBody(req.body);\n * const instance = llm({ model: anthropic('claude-sonnet-4-20250514'), system });\n *\n * if (req.headers.accept?.includes('text/event-stream')) {\n * expressAdapter.streamSSE(instance.stream(messages), res);\n * } else {\n * const turn = await instance.generate(messages);\n * expressAdapter.sendJSON(turn, res);\n * }\n * });\n * ```\n *\n * @example API Gateway with authentication\n * ```typescript\n * import express from 'express';\n * import { llm } from '@providerprotocol/ai';\n * import { anthropic } from '@providerprotocol/ai/anthropic';\n * import { ExponentialBackoff, RoundRobinKeys } from '@providerprotocol/ai/http';\n * import { parseBody } from '@providerprotocol/ai/proxy';\n * import { express as expressAdapter } from '@providerprotocol/ai/proxy/server';\n *\n * const app = express();\n * app.use(express.json());\n *\n * // Your platform's auth middleware\n * async function authMiddleware(req, res, next) {\n * const token = req.headers.authorization?.replace('Bearer ', '');\n * const user = await validatePlatformToken(token);\n * if (!user) return res.status(401).json({ error: 'Unauthorized' });\n * req.user = user;\n * next();\n * }\n *\n * // Server manages AI provider keys - users never see them\n * const claude = llm({\n * model: anthropic('claude-sonnet-4-20250514'),\n * config: {\n * apiKey: new RoundRobinKeys([process.env.ANTHROPIC_KEY_1!, process.env.ANTHROPIC_KEY_2!]),\n * retryStrategy: new ExponentialBackoff({ maxAttempts: 3 }),\n * },\n * });\n *\n * app.post('/api/ai', authMiddleware, async (req, res) => {\n * // Track usage per user\n * // await trackUsage(req.user.id);\n *\n * const { messages, system, params } = parseBody(req.body);\n *\n * if (params?.stream) {\n * expressAdapter.streamSSE(claude.stream(messages, { system }), res);\n * } else {\n * const turn = await claude.generate(messages, { system });\n * expressAdapter.sendJSON(turn, res);\n * }\n * });\n * ```\n */\nexport const express = {\n sendJSON,\n sendEmbeddingJSON,\n sendImageJSON,\n streamSSE,\n streamImageSSE,\n sendError,\n};\n"],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../src/providers/proxy/server/express.ts"],"sourcesContent":["/**\n * @fileoverview Express/Connect adapter for proxy server.\n *\n * Provides utilities for using PP proxy with Express.js or Connect-based servers.\n * These adapters convert PP types to Express-compatible responses.\n *\n * @module providers/proxy/server/express\n */\n\nimport type { Turn } from '../../../types/turn.ts';\nimport type { StreamResult } from '../../../types/stream.ts';\nimport type { EmbeddingResult } from '../../../types/embedding.ts';\nimport type { ImageResult } from '../../../types/image.ts';\nimport { serializeTurn, serializeStreamEvent } from '../serialization.ts';\nimport { serializeImageResult, serializeImageStreamEvent } from '../serialization.media.ts';\nimport { resolveImageResult, type ImageStreamLike } from './image-stream.ts';\n\n/**\n * Express Response interface (minimal type to avoid dependency).\n */\ninterface ExpressResponse {\n setHeader(name: string, value: string): void;\n status(code: number): ExpressResponse;\n write(chunk: string): boolean;\n end(): void;\n json(body: unknown): void;\n}\n\n/**\n * Send a Turn as JSON response.\n *\n * @param turn - The completed inference turn\n * @param res - Express response object\n *\n * @example\n * ```typescript\n * const turn = await instance.generate(messages);\n * expressAdapter.sendJSON(turn, res);\n * ```\n */\nexport function sendJSON(turn: Turn, res: ExpressResponse): void {\n res.setHeader('Content-Type', 'application/json');\n res.json(serializeTurn(turn));\n}\n\n/**\n * Send an EmbeddingResult as JSON response.\n *\n * @param result - The embedding result\n * @param res - Express response object\n */\nexport function sendEmbeddingJSON(result: EmbeddingResult, res: ExpressResponse): void {\n res.setHeader('Content-Type', 'application/json');\n res.json(result);\n}\n\n/**\n * Send an ImageResult as JSON response.\n *\n * @param result - The image result\n * @param res - Express response object\n */\nexport function sendImageJSON(result: ImageResult, res: ExpressResponse): void {\n res.setHeader('Content-Type', 'application/json');\n res.json(serializeImageResult(result));\n}\n\n/**\n * Stream a StreamResult as Server-Sent Events.\n *\n * @param stream - The StreamResult from instance.stream()\n * @param res - Express response object\n *\n * @example\n * ```typescript\n * const stream = instance.stream(messages);\n * expressAdapter.streamSSE(stream, res);\n * ```\n */\nexport function streamSSE(stream: StreamResult, res: ExpressResponse): void {\n res.setHeader('Content-Type', 'text/event-stream');\n res.setHeader('Cache-Control', 'no-cache');\n res.setHeader('Connection', 'keep-alive');\n\n (async () => {\n try {\n for await (const event of stream) {\n const serialized = serializeStreamEvent(event);\n res.write(`data: ${JSON.stringify(serialized)}\\n\\n`);\n }\n\n const turn = await stream.turn;\n res.write(`data: ${JSON.stringify(serializeTurn(turn))}\\n\\n`);\n res.write('data: [DONE]\\n\\n');\n } catch (error) {\n const message = error instanceof Error ? error.message : String(error);\n res.write(`data: ${JSON.stringify({ error: message })}\\n\\n`);\n } finally {\n res.end();\n }\n })();\n}\n\n/**\n * Stream an ImageStreamResult as Server-Sent Events.\n *\n * @param stream - The ImageStreamResult or ImageProviderStreamResult from image().stream()\n * @param res - Express response object\n */\nexport function streamImageSSE(stream: ImageStreamLike, res: ExpressResponse): void {\n res.setHeader('Content-Type', 'text/event-stream');\n res.setHeader('Cache-Control', 'no-cache');\n res.setHeader('Connection', 'keep-alive');\n\n (async () => {\n try {\n for await (const event of stream) {\n const serialized = serializeImageStreamEvent(event);\n res.write(`data: ${JSON.stringify(serialized)}\\n\\n`);\n }\n\n const result = await resolveImageResult(stream);\n res.write(`data: ${JSON.stringify(serializeImageResult(result))}\\n\\n`);\n res.write('data: [DONE]\\n\\n');\n } catch (error) {\n const message = error instanceof Error ? error.message : String(error);\n res.write(`data: ${JSON.stringify({ error: message })}\\n\\n`);\n } finally {\n res.end();\n }\n })();\n}\n\n/**\n * Send an error response.\n *\n * @param message - Error message\n * @param status - HTTP status code\n * @param res - Express response object\n */\nexport function sendError(message: string, status: number, res: ExpressResponse): void {\n res.status(status).json({ error: message });\n}\n\n/**\n * Express adapter utilities.\n *\n * @example Basic usage\n * ```typescript\n * import express from 'express';\n * import { llm } from '@providerprotocol/ai';\n * import { anthropic } from '@providerprotocol/ai/anthropic';\n * import { parseBody } from '@providerprotocol/ai/proxy';\n * import { express as expressAdapter } from '@providerprotocol/ai/proxy/server';\n *\n * const app = express();\n * app.use(express.json());\n *\n * app.post('/api/ai', async (req, res) => {\n * const { messages, system, params } = parseBody(req.body);\n * const instance = llm({ model: anthropic('claude-sonnet-4-20250514'), system });\n *\n * if (req.headers.accept?.includes('text/event-stream')) {\n * expressAdapter.streamSSE(instance.stream(messages), res);\n * } else {\n * const turn = await instance.generate(messages);\n * expressAdapter.sendJSON(turn, res);\n * }\n * });\n * ```\n *\n * @example API Gateway with authentication\n * ```typescript\n * import express from 'express';\n * import { llm } from '@providerprotocol/ai';\n * import { anthropic } from '@providerprotocol/ai/anthropic';\n * import { ExponentialBackoff, RoundRobinKeys } from '@providerprotocol/ai/http';\n * import { parseBody } from '@providerprotocol/ai/proxy';\n * import { express as expressAdapter } from '@providerprotocol/ai/proxy/server';\n *\n * const app = express();\n * app.use(express.json());\n *\n * // Your platform's auth middleware\n * async function authMiddleware(req, res, next) {\n * const token = req.headers.authorization?.replace('Bearer ', '');\n * const user = await validatePlatformToken(token);\n * if (!user) return res.status(401).json({ error: 'Unauthorized' });\n * req.user = user;\n * next();\n * }\n *\n * // Server manages AI provider keys - users never see them\n * const claude = llm({\n * model: anthropic('claude-sonnet-4-20250514'),\n * config: {\n * apiKey: new RoundRobinKeys([process.env.ANTHROPIC_KEY_1!, process.env.ANTHROPIC_KEY_2!]),\n * retryStrategy: new ExponentialBackoff({ maxAttempts: 3 }),\n * },\n * });\n *\n * app.post('/api/ai', authMiddleware, async (req, res) => {\n * // Track usage per user\n * // await trackUsage(req.user.id);\n *\n * const { messages, system, params } = parseBody(req.body);\n *\n * if (params?.stream) {\n * expressAdapter.streamSSE(claude.stream(messages, { system }), res);\n * } else {\n * const turn = await claude.generate(messages, { system });\n * expressAdapter.sendJSON(turn, res);\n * }\n * });\n * ```\n */\nexport const express = {\n sendJSON,\n send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AI;AACF,uBAAiB,SAAS,QAAQ;AAChC,cAAM,aAAa,0BAA0B,KAAK;AAClD,YAAI,MAAM,SAAS,KAAK,UAAU,UAAU,CAAC;AAAA;AAAA,CAAM;AAAA,MACrD;AAEA,YAAM,SAAS,MAAM,mBAAmB,MAAM;AAC9C,UAAI,MAAM,SAAS,KAAK,UAAU,qBAAqB,MAAM,CAAC,CAAC;AAAA;AAAA,CAAM;AACrE,UAAI,MAAM,kBAAkB;AAAA,IAC9B,SAAS,OAAO;AACd,YAAM,UAAU,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK;AACrE,UAAI,MAAM,SAAS,KAAK,UAAU,EAAE,OAAO,QAAQ,CAAC,CAAC;AAAA;AAAA,CAAM;AAAA,IAC7D,UAAE;AACA,UAAI,IAAI;AAAA,IACV;AAAA,EACF,GAAG;AACL;AASO,SAAS,UAAU,SAAiB,QAAgB,KAA4B;AACrF,MAAI,OAAO,MAAM,EAAE,KAAK,EAAE,OAAO,QAAQ,CAAC;AAC5C;AA0EO,IAAM,UAAU;AAAA,EACrB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;","names":[]}
|
|
@@ -1,15 +1,15 @@
|
|
|
1
|
+
import {
|
|
2
|
+
express
|
|
3
|
+
} from "./chunk-O32SBS6S.js";
|
|
1
4
|
import {
|
|
2
5
|
h3
|
|
3
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-KNBODIQU.js";
|
|
4
7
|
import {
|
|
5
8
|
fastify
|
|
6
|
-
} from "./chunk-
|
|
9
|
+
} from "./chunk-IDZR4ROP.js";
|
|
7
10
|
import {
|
|
8
11
|
webapi
|
|
9
|
-
} from "./chunk-
|
|
10
|
-
import {
|
|
11
|
-
express
|
|
12
|
-
} from "./chunk-FYSZFIZS.js";
|
|
12
|
+
} from "./chunk-5XPRVUOK.js";
|
|
13
13
|
|
|
14
14
|
// src/providers/proxy/server/index.ts
|
|
15
15
|
var server = {
|
|
@@ -26,4 +26,4 @@ var server = {
|
|
|
26
26
|
export {
|
|
27
27
|
server
|
|
28
28
|
};
|
|
29
|
-
//# sourceMappingURL=chunk-
|
|
29
|
+
//# sourceMappingURL=chunk-RDC5GYST.js.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
runSubscriberStream
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-U6M3MXNI.js";
|
|
4
4
|
|
|
5
5
|
// src/middleware/pubsub/server/express.ts
|
|
6
6
|
async function streamSubscriber(streamId, adapter, res) {
|
|
@@ -27,4 +27,4 @@ export {
|
|
|
27
27
|
streamSubscriber,
|
|
28
28
|
express
|
|
29
29
|
};
|
|
30
|
-
//# sourceMappingURL=chunk-
|
|
30
|
+
//# sourceMappingURL=chunk-SAMIK4WZ.js.map
|
|
@@ -24,6 +24,7 @@ async function runSubscriberStream(streamId, adapter, writer, options = {}) {
|
|
|
24
24
|
let resolveWait = null;
|
|
25
25
|
let completed = false;
|
|
26
26
|
let lastSentCursor = -1;
|
|
27
|
+
let finalData = void 0;
|
|
27
28
|
const onEvent = (event, cursor) => {
|
|
28
29
|
queue.push({ event, cursor: cursor ?? null });
|
|
29
30
|
resolveWait?.();
|
|
@@ -32,7 +33,10 @@ async function runSubscriberStream(streamId, adapter, writer, options = {}) {
|
|
|
32
33
|
completed = true;
|
|
33
34
|
resolveWait?.();
|
|
34
35
|
};
|
|
35
|
-
const
|
|
36
|
+
const onFinalData = (data) => {
|
|
37
|
+
finalData = data;
|
|
38
|
+
};
|
|
39
|
+
const unsubscribe = adapter.subscribe(streamId, onEvent, onComplete, onFinalData);
|
|
36
40
|
const onAbort = () => {
|
|
37
41
|
completed = true;
|
|
38
42
|
resolveWait?.();
|
|
@@ -98,6 +102,11 @@ async function runSubscriberStream(streamId, adapter, writer, options = {}) {
|
|
|
98
102
|
unsubscribe();
|
|
99
103
|
}
|
|
100
104
|
if (!signal?.aborted) {
|
|
105
|
+
if (finalData !== void 0) {
|
|
106
|
+
writer.write(`data: ${JSON.stringify(finalData)}
|
|
107
|
+
|
|
108
|
+
`);
|
|
109
|
+
}
|
|
101
110
|
writer.write("data: [DONE]\n\n");
|
|
102
111
|
}
|
|
103
112
|
writer.end();
|
|
@@ -115,4 +124,4 @@ async function runSubscriberStream(streamId, adapter, writer, options = {}) {
|
|
|
115
124
|
export {
|
|
116
125
|
runSubscriberStream
|
|
117
126
|
};
|
|
118
|
-
//# sourceMappingURL=chunk-
|
|
127
|
+
//# sourceMappingURL=chunk-U6M3MXNI.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/middleware/pubsub/server/shared.ts"],"sourcesContent":["/**\n * @fileoverview Shared utilities for pub-sub server adapters.\n *\n * @module middleware/pubsub/server/shared\n * @internal\n */\n\nimport type { StreamEvent } from '../../../types/stream.ts';\nimport type { PubSubAdapter } from '../types.ts';\nimport { serializeStreamEvent } from '../../../stream/serialization.ts';\n\n/**\n * Writer interface for abstracting how data is written to responses.\n * @internal\n */\nexport interface StreamWriter {\n write(data: string): void;\n end(): void;\n}\n\n/**\n * Options for runSubscriberStream.\n * @internal\n */\nexport interface StreamOptions {\n signal?: AbortSignal;\n}\n\n/**\n * Formats a stream event as an SSE data line.\n */\nexport function formatSSE(event: StreamEvent): string {\n const serialized = serializeStreamEvent(event);\n return `data: ${JSON.stringify(serialized)}\\n\\n`;\n}\n\n/**\n * Core subscriber stream logic shared across all adapters.\n *\n * Handles:\n * 1. Subscribing to live events and completion signal\n * 2. Replaying buffered events (empty if stream just started)\n * 3. Processing live events until completion signal\n * 4. Final cleanup\n * 5. Client disconnect via AbortSignal\n *\n * @internal\n */\nexport async function runSubscriberStream(\n streamId: string,\n adapter: PubSubAdapter,\n writer: StreamWriter,\n options: StreamOptions = {}\n): Promise<void> {\n const { signal } = options;\n\n if (signal?.aborted) {\n writer.end();\n return;\n }\n\n try {\n if (signal?.aborted) {\n writer.end();\n return;\n }\n\n const queue: Array<{ event: StreamEvent; cursor: number | null }> = [];\n let resolveWait: (() => void) | null = null;\n let completed = false;\n let lastSentCursor = -1;\n let finalData: unknown = undefined;\n\n const onEvent = (event: StreamEvent, cursor?: number): void => {\n queue.push({ event, cursor: cursor ?? null });\n resolveWait?.();\n };\n\n const onComplete = (): void => {\n completed = true;\n resolveWait?.();\n };\n\n const onFinalData = (data: unknown): void => {\n finalData = data;\n };\n\n const unsubscribe = adapter.subscribe(streamId, onEvent, onComplete, onFinalData);\n\n const onAbort = (): void => {\n completed = true;\n resolveWait?.();\n };\n signal?.addEventListener('abort', onAbort);\n\n const drainQueue = (): void => {\n while (queue.length > 0 && !signal?.aborted) {\n const item = queue.shift();\n if (!item) break;\n const { event, cursor } = item;\n if (cursor !== null && cursor <= lastSentCursor) continue;\n writer.write(formatSSE(event));\n if (cursor !== null && cursor > lastSentCursor) {\n lastSentCursor = cursor;\n }\n }\n };\n\n const dropReplayDuplicates = (): void => {\n if (queue.length === 0) return;\n const filtered: Array<{ event: StreamEvent; cursor: number | null }> = [];\n for (const item of queue) {\n if (item.cursor !== null && item.cursor <= lastSentCursor) continue;\n filtered.push(item);\n }\n queue.length = 0;\n queue.push(...filtered);\n };\n\n const waitForSignal = (): Promise<void> => new Promise((resolve) => {\n let settled = false;\n\n const settle = (): void => {\n if (settled) return;\n settled = true;\n resolveWait = null;\n resolve();\n };\n\n resolveWait = settle;\n\n if (completed || signal?.aborted || queue.length > 0) {\n settle();\n }\n });\n\n try {\n const events = await adapter.getEvents(streamId);\n\n for (const event of events) {\n if (signal?.aborted) break;\n writer.write(formatSSE(event));\n }\n\n lastSentCursor = events.length - 1;\n dropReplayDuplicates();\n\n if (signal?.aborted) {\n writer.end();\n return;\n }\n\n // Wait for events or completion signal\n while (!completed && !signal?.aborted) {\n drainQueue();\n if (completed || signal?.aborted) break;\n await waitForSignal();\n }\n\n if (!signal?.aborted) {\n drainQueue();\n }\n } finally {\n signal?.removeEventListener('abort', onAbort);\n unsubscribe();\n }\n\n if (!signal?.aborted) {\n // Emit final data (Turn) if available before [DONE]\n if (finalData !== undefined) {\n writer.write(`data: ${JSON.stringify(finalData)}\\n\\n`);\n }\n writer.write('data: [DONE]\\n\\n');\n }\n writer.end();\n } catch (error) {\n if (!signal?.aborted) {\n const errorMsg = error instanceof Error ? error.message : String(error);\n writer.write(`data: ${JSON.stringify({ error: errorMsg })}\\n\\n`);\n }\n writer.end();\n }\n}\n"],"mappings":";;;;;AA+BO,SAAS,UAAU,OAA4B;AACpD,QAAM,aAAa,qBAAqB,KAAK;AAC7C,SAAO,SAAS,KAAK,UAAU,UAAU,CAAC;AAAA;AAAA;AAC5C;AAcA,eAAsB,oBACpB,UACA,SACA,QACA,UAAyB,CAAC,GACX;AACf,QAAM,EAAE,OAAO,IAAI;AAEnB,MAAI,QAAQ,SAAS;AACnB,WAAO,IAAI;AACX;AAAA,EACF;AAEA,MAAI;AACF,QAAI,QAAQ,SAAS;AACnB,aAAO,IAAI;AACX;AAAA,IACF;AAEA,UAAM,QAA8D,CAAC;AACrE,QAAI,cAAmC;AACvC,QAAI,YAAY;AAChB,QAAI,iBAAiB;AACrB,QAAI,YAAqB;AAEzB,UAAM,UAAU,CAAC,OAAoB,WAA0B;AAC7D,YAAM,KAAK,EAAE,OAAO,QAAQ,UAAU,KAAK,CAAC;AAC5C,oBAAc;AAAA,IAChB;AAEA,UAAM,aAAa,MAAY;AAC7B,kBAAY;AACZ,oBAAc;AAAA,IAChB;AAEA,UAAM,cAAc,CAAC,SAAwB;AAC3C,kBAAY;AAAA,IACd;AAEA,UAAM,cAAc,QAAQ,UAAU,UAAU,SAAS,YAAY,WAAW;AAEhF,UAAM,UAAU,MAAY;AAC1B,kBAAY;AACZ,oBAAc;AAAA,IAChB;AACA,YAAQ,iBAAiB,SAAS,OAAO;AAEzC,UAAM,aAAa,MAAY;AAC7B,aAAO,MAAM,SAAS,KAAK,CAAC,QAAQ,SAAS;AAC3C,cAAM,OAAO,MAAM,MAAM;AACzB,YAAI,CAAC,KAAM;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AAI,CAAC,QAAQ,SAAS;AAEpB,UAAI,cAAc,QAAW;AAC3B,eAAO,MAAM,SAAS,KAAK,UAAU,SAAS,CAAC;AAAA;AAAA,CAAM;AAAA,MACvD;AACA,aAAO,MAAM,kBAAkB;AAAA,IACjC;AACA,WAAO,IAAI;AAAA,EACb,SAAS,OAAO;AACd,QAAI,CAAC,QAAQ,SAAS;AACpB,YAAM,WAAW,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK;AACtE,aAAO,MAAM,SAAS,KAAK,UAAU,EAAE,OAAO,SAAS,CAAC,CAAC;AAAA;AAAA,CAAM;AAAA,IACjE;AACA,WAAO,IAAI;AAAA,EACb;AACF;","names":[]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
runSubscriberStream
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-U6M3MXNI.js";
|
|
4
4
|
|
|
5
5
|
// src/middleware/pubsub/server/webapi.ts
|
|
6
6
|
function createSubscriberStream(streamId, adapter) {
|
|
@@ -46,4 +46,4 @@ export {
|
|
|
46
46
|
createSubscriberStream,
|
|
47
47
|
webapi
|
|
48
48
|
};
|
|
49
|
-
//# sourceMappingURL=chunk-
|
|
49
|
+
//# sourceMappingURL=chunk-WNB5PSY6.js.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
runSubscriberStream
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-U6M3MXNI.js";
|
|
4
4
|
|
|
5
5
|
// src/middleware/pubsub/server/fastify.ts
|
|
6
6
|
async function streamSubscriber(streamId, adapter, reply) {
|
|
@@ -28,4 +28,4 @@ export {
|
|
|
28
28
|
streamSubscriber,
|
|
29
29
|
fastify
|
|
30
30
|
};
|
|
31
|
-
//# sourceMappingURL=chunk-
|
|
31
|
+
//# sourceMappingURL=chunk-ZDYEDI2A.js.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { P as ProviderIdentity, c as ProviderConfig, M as Middleware, E as EmbeddingInput, d as EmbeddingUsage, B as BoundEmbeddingModel } from './llm-
|
|
1
|
+
import { P as ProviderIdentity, c as ProviderConfig, M as Middleware, E as EmbeddingInput, d as EmbeddingUsage, B as BoundEmbeddingModel } from './llm-CZqlijjK.js';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* @fileoverview Embedding types for vector embedding generation.
|
package/dist/google/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { c as ProviderConfig, e as Provider } from '../llm-
|
|
2
|
-
import '../stream-
|
|
1
|
+
import { c as ProviderConfig, e as Provider } from '../llm-CZqlijjK.js';
|
|
2
|
+
import '../stream-DVVUIKpz.js';
|
|
3
|
+
import '../tool-D22EhP5F.js';
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* Provider-specific parameters for Google Gemini API requests.
|
package/dist/groq/index.d.ts
CHANGED
package/dist/http/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
export { D as DynamicKey, E as ExponentialBackoff, L as LinearBackoff, N as NoRetry, a as RetryAfterStrategy, R as RoundRobinKeys, T as TokenBucket, W as WeightedKeys, m as maskApiKey, r as resolveApiKey } from '../retry-
|
|
2
|
-
import { c as ProviderConfig, f as Modality, U as UPPError, g as ErrorCode } from '../llm-
|
|
3
|
-
import '../stream-
|
|
1
|
+
export { D as DynamicKey, E as ExponentialBackoff, L as LinearBackoff, N as NoRetry, a as RetryAfterStrategy, R as RoundRobinKeys, T as TokenBucket, W as WeightedKeys, m as maskApiKey, r as resolveApiKey } from '../retry-C1eJbEMV.js';
|
|
2
|
+
import { c as ProviderConfig, f as Modality, U as UPPError, g as ErrorCode } from '../llm-CZqlijjK.js';
|
|
3
|
+
import '../stream-DVVUIKpz.js';
|
|
4
|
+
import '../tool-D22EhP5F.js';
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* HTTP fetch utilities with retry, timeout, and error normalization.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { L as LLMOptions, i as LLMInstance, j as ImageOptions, k as ImageInstance, l as LLMHandler, m as EmbeddingHandler, n as ImageHandler, e as Provider, h as ModelReference } from './llm-
|
|
2
|
-
export { a0 as AnyRequest, a1 as AnyResponse, B as BoundEmbeddingModel, X as BoundImageModel, A as BoundLLMModel, E as EmbeddingInput, r as EmbeddingProvider, t as EmbeddingRequest, u as EmbeddingResponse, d as EmbeddingUsage, v as EmbeddingVector, g as ErrorCode, H as GeneratedImage, o as Image, Q as ImageCapabilities, F as ImageEditInput, V as ImageEditRequest, G as ImageGenerateOptions, D as ImageInput, Y as ImageModelInput, s as ImageProvider, b as ImageProviderStreamResult, S as ImageRequest, W as ImageResponse, N as ImageResult, O as ImageStreamEvent, I as ImageStreamResult, J as ImageUsage, C as InferenceInput, K as KeyStrategy, w as LLMCapabilities, q as LLMProvider, x as LLMRequest, y as LLMResponse, z as LLMStreamResult, M as Middleware, Z as MiddlewareContext, $ as MiddlewareModality, f as Modality, p as ModalityType, c as ProviderConfig, P as ProviderIdentity, R as RetryStrategy, _ as StreamContext, T as Thread, a as ThreadJSON, U as UPPError } from './llm-
|
|
3
|
-
import { E as EmbeddingOptions, a as EmbeddingInstance } from './embedding-
|
|
4
|
-
export { b as EmbedOptions, c as Embedding, h as EmbeddingInputType, g as EmbeddingModelInput, e as EmbeddingProgress, d as EmbeddingResult, f as EmbeddingStream } from './embedding-
|
|
5
|
-
import { D as DocumentSource,
|
|
6
|
-
export {
|
|
7
|
-
export {
|
|
1
|
+
import { L as LLMOptions, i as LLMInstance, j as ImageOptions, k as ImageInstance, l as LLMHandler, m as EmbeddingHandler, n as ImageHandler, e as Provider, h as ModelReference } from './llm-CZqlijjK.js';
|
|
2
|
+
export { a0 as AnyRequest, a1 as AnyResponse, B as BoundEmbeddingModel, X as BoundImageModel, A as BoundLLMModel, E as EmbeddingInput, r as EmbeddingProvider, t as EmbeddingRequest, u as EmbeddingResponse, d as EmbeddingUsage, v as EmbeddingVector, g as ErrorCode, H as GeneratedImage, o as Image, Q as ImageCapabilities, F as ImageEditInput, V as ImageEditRequest, G as ImageGenerateOptions, D as ImageInput, Y as ImageModelInput, s as ImageProvider, b as ImageProviderStreamResult, S as ImageRequest, W as ImageResponse, N as ImageResult, O as ImageStreamEvent, I as ImageStreamResult, J as ImageUsage, C as InferenceInput, K as KeyStrategy, w as LLMCapabilities, q as LLMProvider, x as LLMRequest, y as LLMResponse, z as LLMStreamResult, M as Middleware, Z as MiddlewareContext, $ as MiddlewareModality, f as Modality, p as ModalityType, c as ProviderConfig, P as ProviderIdentity, R as RetryStrategy, _ as StreamContext, T as Thread, a as ThreadJSON, U as UPPError } from './llm-CZqlijjK.js';
|
|
3
|
+
import { E as EmbeddingOptions, a as EmbeddingInstance } from './embedding-iNQCeXfk.js';
|
|
4
|
+
export { b as EmbedOptions, c as Embedding, h as EmbeddingInputType, g as EmbeddingModelInput, e as EmbeddingProgress, d as EmbeddingResult, f as EmbeddingStream } from './embedding-iNQCeXfk.js';
|
|
5
|
+
import { D as DocumentSource, h as DocumentBlock, i as AudioBlock, V as VideoBlock } from './stream-DVVUIKpz.js';
|
|
6
|
+
export { A as AssistantContent, e as AssistantMessage, B as BinaryBlock, C as ContentBlock, k as ContentBlockType, m as DocumentSourceType, E as EventDelta, c as ImageBlock, I as ImageSource, l as ImageSourceType, M as Message, a as MessageJSON, H as MessageMetadata, J as MessageOptions, y as MessageRole, d as MessageType, R as ReasoningBlock, S as StreamEvent, O as StreamEventType, g as StreamResult, j as TextBlock, f as TokenUsage, x as ToolResultMessage, T as Turn, U as UserContent, w as UserMessage, N as aggregateUsage, _ as contentBlockStart, $ as contentBlockStop, P as createStreamResult, K as createTurn, L as emptyUsage, F as isAssistantMessage, s as isAudioBlock, v as isBinaryBlock, q as isDocumentBlock, p as isImageBlock, o as isReasoningBlock, n as isTextBlock, G as isToolResultMessage, z as isUserMessage, u as isVideoBlock, Y as messageStart, Z as messageStop, X as objectDelta, r as reasoning, t as text, Q as textDelta, W as toolCallDelta, a1 as toolExecutionEnd, a0 as toolExecutionStart } from './stream-DVVUIKpz.js';
|
|
7
|
+
export { A as AfterCallResult, B as BeforeCallResult, J as JSONSchema, g as JSONSchemaProperty, h as JSONSchemaPropertyType, S as Structure, b as Tool, c as ToolCall, e as ToolExecution, j as ToolMetadata, d as ToolResult, f as ToolUseStrategy, Z as ZodLike, i as ZodV3Like, a as ZodV4Like } from './tool-D22EhP5F.js';
|
|
8
|
+
export { D as DynamicKey, E as ExponentialBackoff, L as LinearBackoff, N as NoRetry, a as RetryAfterStrategy, R as RoundRobinKeys, T as TokenBucket, W as WeightedKeys } from './retry-C1eJbEMV.js';
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
11
|
* @fileoverview LLM instance factory and streaming logic for the Universal Provider Protocol.
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import {
|
|
2
|
+
resolveStructure,
|
|
3
|
+
resolveTools
|
|
4
|
+
} from "./chunk-3Q5VELKG.js";
|
|
1
5
|
import {
|
|
2
6
|
ExponentialBackoff,
|
|
3
7
|
LinearBackoff,
|
|
@@ -311,6 +315,9 @@ function inputToMessage(input) {
|
|
|
311
315
|
throw new Error("Invalid inference input");
|
|
312
316
|
}
|
|
313
317
|
function parseInputs(historyOrInput, inputs) {
|
|
318
|
+
if (historyOrInput === void 0 && inputs.length === 0) {
|
|
319
|
+
return { history: [], messages: [] };
|
|
320
|
+
}
|
|
314
321
|
if (typeof historyOrInput === "object" && historyOrInput !== null && "messages" in historyOrInput && Array.isArray(historyOrInput.messages)) {
|
|
315
322
|
const thread = historyOrInput;
|
|
316
323
|
const newMessages2 = inputs.map(inputToMessage);
|
|
@@ -814,11 +821,13 @@ function llm(options) {
|
|
|
814
821
|
config: explicitConfig = {},
|
|
815
822
|
params,
|
|
816
823
|
system,
|
|
817
|
-
tools,
|
|
824
|
+
tools: toolsInput,
|
|
818
825
|
toolStrategy,
|
|
819
|
-
structure,
|
|
826
|
+
structure: structureInput,
|
|
820
827
|
middleware = []
|
|
821
828
|
} = options;
|
|
829
|
+
const structure = structureInput ? resolveStructure(structureInput) : void 0;
|
|
830
|
+
const tools = toolsInput ? resolveTools(toolsInput) : void 0;
|
|
822
831
|
const providerConfig = modelRef.providerConfig ?? {};
|
|
823
832
|
const config = {
|
|
824
833
|
...providerConfig,
|