@tensorpass/aiconnect 0.0.0-stage → 0.1.1
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 +202 -0
- package/NOTICE +4 -0
- package/README.md +18 -2
- package/dist/callback.d.ts +2 -0
- package/dist/callback.js +4 -0
- package/dist/callback.js.map +1 -0
- package/dist/chatContent.d.ts +9 -0
- package/dist/chatContent.js +23 -0
- package/dist/chatContent.js.map +1 -0
- package/dist/cli/loopbackSignIn.d.ts +11 -0
- package/dist/cli/loopbackSignIn.js +94 -0
- package/dist/cli/loopbackSignIn.js.map +1 -0
- package/dist/cli/main.d.ts +2 -0
- package/dist/cli/main.js +41 -0
- package/dist/cli/main.js.map +1 -0
- package/dist/cli/siteKeyCommand.d.ts +11 -0
- package/dist/cli/siteKeyCommand.js +31 -0
- package/dist/cli/siteKeyCommand.js.map +1 -0
- package/dist/cli/siteKeyPlacement.d.ts +3 -0
- package/dist/cli/siteKeyPlacement.js +56 -0
- package/dist/cli/siteKeyPlacement.js.map +1 -0
- package/dist/client.d.ts +43 -0
- package/dist/client.js +211 -0
- package/dist/client.js.map +1 -0
- package/dist/connectionState.d.ts +31 -0
- package/dist/connectionState.js +4 -0
- package/dist/connectionState.js.map +1 -0
- package/dist/guards/answerGuard.d.ts +14 -0
- package/dist/guards/answerGuard.js +189 -0
- package/dist/guards/answerGuard.js.map +1 -0
- package/dist/guards/chatGuard.d.ts +18 -0
- package/dist/guards/chatGuard.js +62 -0
- package/dist/guards/chatGuard.js.map +1 -0
- package/dist/guards/envelopes.d.ts +8 -0
- package/dist/guards/envelopes.js +49 -0
- package/dist/guards/envelopes.js.map +1 -0
- package/dist/guards/index.d.ts +8 -0
- package/dist/guards/index.js +8 -0
- package/dist/guards/index.js.map +1 -0
- package/dist/guards/policy.d.ts +28 -0
- package/dist/guards/policy.js +20 -0
- package/dist/guards/policy.js.map +1 -0
- package/dist/guards/questionGuard.d.ts +8 -0
- package/dist/guards/questionGuard.js +53 -0
- package/dist/guards/questionGuard.js.map +1 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/dist/mcpToolSource.d.ts +13 -0
- package/dist/mcpToolSource.js +54 -0
- package/dist/mcpToolSource.js.map +1 -0
- package/dist/toolLoop.d.ts +18 -0
- package/dist/toolLoop.js +153 -0
- package/dist/toolLoop.js.map +1 -0
- package/dist/types.d.ts +98 -0
- package/dist/types.js +6 -0
- package/dist/types.js.map +1 -0
- package/dist/visitorBrowser.d.ts +17 -0
- package/dist/visitorBrowser.js +72 -0
- package/dist/visitorBrowser.js.map +1 -0
- package/dist/walletProvider.d.ts +25 -0
- package/dist/walletProvider.js +42 -0
- package/dist/walletProvider.js.map +1 -0
- package/dist/webAccounts/finishSignIn.d.ts +2 -0
- package/dist/webAccounts/finishSignIn.js +46 -0
- package/dist/webAccounts/finishSignIn.js.map +1 -0
- package/dist/webAccounts/openAiCompatibleSession.d.ts +15 -0
- package/dist/webAccounts/openAiCompatibleSession.js +84 -0
- package/dist/webAccounts/openAiCompatibleSession.js.map +1 -0
- package/dist/webAccounts/openAiWire.d.ts +39 -0
- package/dist/webAccounts/openAiWire.js +133 -0
- package/dist/webAccounts/openAiWire.js.map +1 -0
- package/dist/webAccounts/openRouterAccount.d.ts +3 -0
- package/dist/webAccounts/openRouterAccount.js +64 -0
- package/dist/webAccounts/openRouterAccount.js.map +1 -0
- package/dist/webAccounts/pkce.d.ts +2 -0
- package/dist/webAccounts/pkce.js +17 -0
- package/dist/webAccounts/pkce.js.map +1 -0
- package/dist/webAccounts/serverSentEvents.d.ts +1 -0
- package/dist/webAccounts/serverSentEvents.js +62 -0
- package/dist/webAccounts/serverSentEvents.js.map +1 -0
- package/dist/webAccounts/signInWindow.d.ts +27 -0
- package/dist/webAccounts/signInWindow.js +164 -0
- package/dist/webAccounts/signInWindow.js.map +1 -0
- package/dist/webAccounts/tensorPassAccount.d.ts +9 -0
- package/dist/webAccounts/tensorPassAccount.js +128 -0
- package/dist/webAccounts/tensorPassAccount.js.map +1 -0
- package/dist/webAccounts/tensorPassPlan.d.ts +27 -0
- package/dist/webAccounts/tensorPassPlan.js +36 -0
- package/dist/webAccounts/tensorPassPlan.js.map +1 -0
- package/dist/webAccounts/webAccount.d.ts +30 -0
- package/dist/webAccounts/webAccount.js +19 -0
- package/dist/webAccounts/webAccount.js.map +1 -0
- package/dist/webAccounts/webAccountSource.d.ts +43 -0
- package/dist/webAccounts/webAccountSource.js +216 -0
- package/dist/webAccounts/webAccountSource.js.map +1 -0
- package/dist/webAccounts/webAccountStore.d.ts +27 -0
- package/dist/webAccounts/webAccountStore.js +84 -0
- package/dist/webAccounts/webAccountStore.js.map +1 -0
- package/dist/wireProtocols/anthropicMessages.d.ts +2 -0
- package/dist/wireProtocols/anthropicMessages.js +181 -0
- package/dist/wireProtocols/anthropicMessages.js.map +1 -0
- package/dist/wireProtocols/eventStream.d.ts +4 -0
- package/dist/wireProtocols/eventStream.js +32 -0
- package/dist/wireProtocols/eventStream.js.map +1 -0
- package/dist/wireProtocols/geminiContents.d.ts +2 -0
- package/dist/wireProtocols/geminiContents.js +201 -0
- package/dist/wireProtocols/geminiContents.js.map +1 -0
- package/dist/wireProtocols/openAiChat.d.ts +2 -0
- package/dist/wireProtocols/openAiChat.js +161 -0
- package/dist/wireProtocols/openAiChat.js.map +1 -0
- package/dist/wireProtocols/openAiResponses.d.ts +2 -0
- package/dist/wireProtocols/openAiResponses.js +226 -0
- package/dist/wireProtocols/openAiResponses.js.map +1 -0
- package/dist/wireProtocols/walletAnswer.d.ts +7 -0
- package/dist/wireProtocols/walletAnswer.js +50 -0
- package/dist/wireProtocols/walletAnswer.js.map +1 -0
- package/dist/wireProtocols/walletFetch.d.ts +3 -0
- package/dist/wireProtocols/walletFetch.js +101 -0
- package/dist/wireProtocols/walletFetch.js.map +1 -0
- package/dist/wireProtocols/wireProtocol.d.ts +31 -0
- package/dist/wireProtocols/wireProtocol.js +44 -0
- package/dist/wireProtocols/wireProtocol.js.map +1 -0
- package/package.json +61 -4
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"walletFetch.js","sourceRoot":"","sources":["../../src/wireProtocols/walletFetch.ts"],"names":[],"mappings":"AAAA,oGAAoG;AACpG,4FAA4F;AAC5F,8FAA8F;AAC9F,+FAA+F;AAC/F,+EAA+E;AAG/E,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAC3D,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAM9C,MAAM,MAAM,GAA+C;IACzD,EAAE,IAAI,EAAE,sBAAsB,EAAE,QAAQ,EAAE,UAAU,EAAE;IACtD,EAAE,IAAI,EAAE,cAAc,EAAE,QAAQ,EAAE,eAAe,EAAE;IACnD,EAAE,IAAI,EAAE,aAAa,EAAE,QAAQ,EAAE,iBAAiB,EAAE;IACpD,EAAE,IAAI,EAAE,6BAA6B,EAAE,QAAQ,EAAE,cAAc,EAAE;CAClE,CAAC;AAEF,MAAM,YAAY,GAAG,EAAE,cAAc,EAAE,kBAAkB,EAAE,CAAC;AAE5D,SAAS,WAAW,CAAC,OAAgB,EAAE,GAAQ;IAC7C,OAAO,OAAO,CAAC,MAAM,KAAK,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC;AACj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CAAC;YACzD,OAAO,MAAM,MAAM,CAAC,QAAQ,EAAE,IAAI,EAAE,MAAM,gBAAgB,EAAE,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;QAChF,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,OAAO,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;gBAC3B,MAAM,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC;YAC9B,CAAC;YACD,MAAM,OAAO,GAAG,WAAW,CAAC,KAAK,CAAC,CAAC;YACnC,OAAO,YAAY,CAAC,OAAO,CAAC,MAAM,EAAE,QAAQ,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC;QAC/D,CAAC;IACH,CAAC,CAAC;AACJ,CAAC","sourcesContent":["// A `fetch` for the AI SDKs. It recognises the protocol an SDK speaks from the path of its request,\n// reads the request into the wallet's chat format, asks the wallet, and answers in the same\n// protocol, streamed or whole. Nothing reaches the network: the host the SDK targets does not\n// matter, and its API key is never read. An answer that fails before any text comes back as an\n// HTTP error with the wallet's status, so each SDK raises its own typed error.\n\nimport type { ProviderSession } from '../types.ts';\nimport { WalletRequestError } from '../walletProvider.ts';\nimport { anthropicMessages } from './anthropicMessages.ts';\nimport { eventStream } from './eventStream.ts';\nimport { geminiContents } from './geminiContents.ts';\nimport { openAiChat } from './openAiChat.ts';\nimport { openAiResponses } from './openAiResponses.ts';\nimport { askWallet } from './walletAnswer.ts';\nimport type { WalletAnswer } from './walletAnswer.ts';\nimport type { AnswerContext, WireProtocol, WireRequest } from './wireProtocol.ts';\n\nexport type WalletFetch = (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;\n\nconst ROUTES: { path: RegExp; protocol: WireProtocol }[] = [\n { path: /\\/chat\\/completions$/, protocol: openAiChat },\n { path: /\\/responses$/, protocol: openAiResponses },\n { path: /\\/messages$/, protocol: anthropicMessages },\n { path: /:(stream)?generateContent$/i, protocol: geminiContents },\n];\n\nconst JSON_HEADERS = { 'content-type': 'application/json' };\n\nfunction protocolFor(request: Request, url: URL): WireProtocol | undefined {\n return request.method === 'POST' ? ROUTES.find((route) => route.path.test(url.pathname))?.protocol : undefined;\n}\n\nfunction jsonResponse(status: number, body: unknown): Response {\n return new Response(JSON.stringify(body), { status, headers: JSON_HEADERS });\n}\n\n// An endpoint the wallet does not answer, in the error shape the OpenAI, Anthropic and Google\n// SDKs all read.\nfunction notAnswered(request: Request, url: URL): Response {\n const message = `AI Wallet answers chat requests only, not ${request.method} ${url.pathname}`;\n return jsonResponse(404, { type: 'error', error: { type: 'not_found_error', message, code: 404 } });\n}\n\n// The wallet's errors carry their status; anything else is a failure of the page itself.\nfunction walletError(error: unknown): WalletRequestError {\n const { message, status } = error as { message?: string; status?: unknown };\n return new WalletRequestError(message ?? String(error), typeof status === 'number' ? status : 500);\n}\n\n// The SDK's body may be a stream, which a request built from it must read in half duplex.\nfunction asRequest(input: RequestInfo | URL, init?: RequestInit): Request {\n const streamed = init?.body instanceof ReadableStream;\n return new Request(input, streamed ? { ...init, duplex: 'half' } as RequestInit : init);\n}\n\nasync function jsonBody(request: Request): Promise<Record<string, unknown>> {\n try {\n return await request.json() as Record<string, unknown>;\n } catch {\n throw new WalletRequestError('The request body is not JSON', 400);\n }\n}\n\nfunction streamResponse(protocol: WireProtocol, answer: WalletAnswer, context: AnswerContext, stop: () => void) {\n const body = eventStream(async (writer) => {\n try {\n await protocol.stream(answer, writer, context);\n } catch (error) {\n if (!protocol.streamError || (error as Error)?.name === 'AbortError') {\n throw error;\n }\n const { data, event } = protocol.streamError(walletError(error));\n writer.send(data, event);\n }\n }, stop);\n return new Response(body, { status: 200, headers: { 'content-type': 'text/event-stream' } });\n}\n\nasync function answer(protocol: WireProtocol, wire: WireRequest, session: ProviderSession, signal: AbortSignal) {\n // Stopped by the SDK's signal, or by the SDK cancelling the stream it reads.\n const controller = new AbortController();\n signal.addEventListener('abort', () => controller.abort(signal.reason), { once: true });\n const stop = () => controller.abort(new DOMException('The SDK stopped reading the answer', 'AbortError'));\n const context = { id: crypto.randomUUID(), created: Math.floor(Date.now() / 1000), wire };\n const walletAnswer = askWallet(session, { ...wire.request, signal: controller.signal });\n if (!wire.stream) {\n return jsonResponse(200, protocol.body(await walletAnswer.result, context));\n }\n // The stream opens once the wallet names who answers; a failure before that is an HTTP error.\n await walletAnswer.started;\n return streamResponse(protocol, walletAnswer, context, stop);\n}\n\nexport function createWalletFetch(connectedSession: () => Promise<ProviderSession>): WalletFetch {\n return async (input, init) => {\n const request = asRequest(input, init);\n request.signal.throwIfAborted();\n const url = new URL(request.url);\n const protocol = protocolFor(request, url);\n if (!protocol) {\n return notAnswered(request, url);\n }\n try {\n const wire = protocol.read(await jsonBody(request), url);\n return await answer(protocol, wire, await connectedSession(), request.signal);\n } catch (error) {\n if (request.signal.aborted) {\n throw request.signal.reason;\n }\n const failure = walletError(error);\n return jsonResponse(failure.status, protocol.error(failure));\n }\n };\n}\n"]}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { ChatRequest, ChatResponse, ContentPart, JsonSchema } from '../types.ts';
|
|
2
|
+
import { WalletRequestError } from '../walletProvider.ts';
|
|
3
|
+
import type { EventWriter } from './eventStream.ts';
|
|
4
|
+
import type { WalletAnswer } from './walletAnswer.ts';
|
|
5
|
+
export interface WireRequest {
|
|
6
|
+
request: Pick<ChatRequest, 'messages' | 'tools' | 'options'>;
|
|
7
|
+
stream: boolean;
|
|
8
|
+
includeUsage?: boolean;
|
|
9
|
+
}
|
|
10
|
+
export interface AnswerContext {
|
|
11
|
+
id: string;
|
|
12
|
+
created: number;
|
|
13
|
+
wire: WireRequest;
|
|
14
|
+
}
|
|
15
|
+
export interface StreamError {
|
|
16
|
+
data: unknown;
|
|
17
|
+
event?: string;
|
|
18
|
+
}
|
|
19
|
+
export interface WireProtocol {
|
|
20
|
+
read(body: Record<string, unknown>, url: URL): WireRequest;
|
|
21
|
+
stream(answer: WalletAnswer, write: EventWriter, context: AnswerContext): Promise<void>;
|
|
22
|
+
body(response: ChatResponse, context: AnswerContext): unknown;
|
|
23
|
+
error(error: WalletRequestError): unknown;
|
|
24
|
+
streamError?(error: WalletRequestError): StreamError;
|
|
25
|
+
}
|
|
26
|
+
export declare const EMPTY_PARAMETERS: JsonSchema;
|
|
27
|
+
export declare function errorType(status: number): string;
|
|
28
|
+
export declare function unsupported(what: string): WalletRequestError;
|
|
29
|
+
export declare function userContent(parts: ContentPart[]): string | ContentPart[];
|
|
30
|
+
export declare function parseToolArguments(raw: string | undefined): Record<string, unknown>;
|
|
31
|
+
export declare function toolNameOf(names: Map<string, string>, callId: string): string;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// What every protocol an AI SDK speaks provides to `AiConnect.fetch`: reading the SDK's request
|
|
2
|
+
// into the wallet's chat format, and writing the wallet's answer back the way the provider's own
|
|
3
|
+
// servers write it, streamed or whole, with errors in the provider's shape.
|
|
4
|
+
import { WalletRequestError } from "../walletProvider.js";
|
|
5
|
+
// A function the SDK declares without parameters takes an empty object.
|
|
6
|
+
export const EMPTY_PARAMETERS = { type: 'object', properties: {} };
|
|
7
|
+
const ERROR_TYPES = {
|
|
8
|
+
400: 'invalid_request_error',
|
|
9
|
+
401: 'authentication_error',
|
|
10
|
+
403: 'permission_error',
|
|
11
|
+
404: 'not_found_error',
|
|
12
|
+
429: 'rate_limit_error',
|
|
13
|
+
};
|
|
14
|
+
// The error type OpenAI and Anthropic name for a status.
|
|
15
|
+
export function errorType(status) {
|
|
16
|
+
return ERROR_TYPES[status] ?? 'api_error';
|
|
17
|
+
}
|
|
18
|
+
export function unsupported(what) {
|
|
19
|
+
return new WalletRequestError(`AI Wallet does not take ${what}`, 400);
|
|
20
|
+
}
|
|
21
|
+
// A single text stays a string; images or several parts stay parts.
|
|
22
|
+
export function userContent(parts) {
|
|
23
|
+
return parts.length === 1 && parts[0].type === 'text' ? parts[0].text : parts;
|
|
24
|
+
}
|
|
25
|
+
export function parseToolArguments(raw) {
|
|
26
|
+
if (!raw?.trim()) {
|
|
27
|
+
return {};
|
|
28
|
+
}
|
|
29
|
+
try {
|
|
30
|
+
return JSON.parse(raw);
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
throw new WalletRequestError(`Tool call arguments are not JSON: ${raw.slice(0, 100)}`, 400);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
// The name of the tool a result answers, read from the call earlier in the conversation.
|
|
37
|
+
export function toolNameOf(names, callId) {
|
|
38
|
+
const name = names.get(callId);
|
|
39
|
+
if (!name) {
|
|
40
|
+
throw new WalletRequestError(`A tool result answers ${callId}, which no earlier tool call made`, 400);
|
|
41
|
+
}
|
|
42
|
+
return name;
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=wireProtocol.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wireProtocol.js","sourceRoot":"","sources":["../../src/wireProtocols/wireProtocol.ts"],"names":[],"mappings":"AAAA,gGAAgG;AAChG,iGAAiG;AACjG,4EAA4E;AAG5E,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAiC1D,wEAAwE;AACxE,MAAM,CAAC,MAAM,gBAAgB,GAAe,EAAE,IAAI,EAAE,QAAQ,EAAE,UAAU,EAAE,EAAE,EAAE,CAAC;AAE/E,MAAM,WAAW,GAA2B;IAC1C,GAAG,EAAE,uBAAuB;IAC5B,GAAG,EAAE,sBAAsB;IAC3B,GAAG,EAAE,kBAAkB;IACvB,GAAG,EAAE,iBAAiB;IACtB,GAAG,EAAE,kBAAkB;CACxB,CAAC;AAEF,yDAAyD;AACzD,MAAM,UAAU,SAAS,CAAC,MAAc;IACtC,OAAO,WAAW,CAAC,MAAM,CAAC,IAAI,WAAW,CAAC;AAC5C,CAAC;AAED,MAAM,UAAU,WAAW,CAAC,IAAY;IACtC,OAAO,IAAI,kBAAkB,CAAC,2BAA2B,IAAI,EAAE,EAAE,GAAG,CAAC,CAAC;AACxE,CAAC;AAED,oEAAoE;AACpE,MAAM,UAAU,WAAW,CAAC,KAAoB;IAC9C,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC,IAAI,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,KAAK,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC;AAChF,CAAC;AAED,MAAM,UAAU,kBAAkB,CAAC,GAAuB;IACxD,IAAI,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE,CAAC;QACjB,OAAO,EAAE,CAAC;IACZ,CAAC;IACD,IAAI,CAAC;QACH,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAA4B,CAAC;IACpD,CAAC;IAAC,MAAM,CAAC;QACP,MAAM,IAAI,kBAAkB,CAAC,qCAAqC,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,EAAE,GAAG,CAAC,CAAC;IAC9F,CAAC;AACH,CAAC;AAED,yFAAyF;AACzF,MAAM,UAAU,UAAU,CAAC,KAA0B,EAAE,MAAc;IACnE,MAAM,IAAI,GAAG,KAAK,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IAC/B,IAAI,CAAC,IAAI,EAAE,CAAC;QACV,MAAM,IAAI,kBAAkB,CAAC,yBAAyB,MAAM,mCAAmC,EAAE,GAAG,CAAC,CAAC;IACxG,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC","sourcesContent":["// What every protocol an AI SDK speaks provides to `AiConnect.fetch`: reading the SDK's request\n// into the wallet's chat format, and writing the wallet's answer back the way the provider's own\n// servers write it, streamed or whole, with errors in the provider's shape.\n\nimport type { ChatRequest, ChatResponse, ContentPart, JsonSchema } from '../types.ts';\nimport { WalletRequestError } from '../walletProvider.ts';\nimport type { EventWriter } from './eventStream.ts';\nimport type { WalletAnswer } from './walletAnswer.ts';\n\nexport interface WireRequest {\n request: Pick<ChatRequest, 'messages' | 'tools' | 'options'>;\n stream: boolean;\n // OpenAI streams the usage only to a request that asks for it.\n includeUsage?: boolean;\n}\n\n// The answer being written: its id, its creation time in seconds and the request it answers. A\n// stream reads the model that answers from the answer's start.\nexport interface AnswerContext {\n id: string;\n created: number;\n wire: WireRequest;\n}\n\nexport interface StreamError {\n data: unknown;\n event?: string;\n}\n\nexport interface WireProtocol {\n read(body: Record<string, unknown>, url: URL): WireRequest;\n stream(answer: WalletAnswer, write: EventWriter, context: AnswerContext): Promise<void>;\n body(response: ChatResponse, context: AnswerContext): unknown;\n error(error: WalletRequestError): unknown;\n // How the protocol reports a failure after the stream started; without it the stream breaks.\n streamError?(error: WalletRequestError): StreamError;\n}\n\n// A function the SDK declares without parameters takes an empty object.\nexport const EMPTY_PARAMETERS: JsonSchema = { type: 'object', properties: {} };\n\nconst ERROR_TYPES: Record<number, string> = {\n 400: 'invalid_request_error',\n 401: 'authentication_error',\n 403: 'permission_error',\n 404: 'not_found_error',\n 429: 'rate_limit_error',\n};\n\n// The error type OpenAI and Anthropic name for a status.\nexport function errorType(status: number): string {\n return ERROR_TYPES[status] ?? 'api_error';\n}\n\nexport function unsupported(what: string): WalletRequestError {\n return new WalletRequestError(`AI Wallet does not take ${what}`, 400);\n}\n\n// A single text stays a string; images or several parts stay parts.\nexport function userContent(parts: ContentPart[]): string | ContentPart[] {\n return parts.length === 1 && parts[0].type === 'text' ? parts[0].text : parts;\n}\n\nexport function parseToolArguments(raw: string | undefined): Record<string, unknown> {\n if (!raw?.trim()) {\n return {};\n }\n try {\n return JSON.parse(raw) as Record<string, unknown>;\n } catch {\n throw new WalletRequestError(`Tool call arguments are not JSON: ${raw.slice(0, 100)}`, 400);\n }\n}\n\n// The name of the tool a result answers, read from the call earlier in the conversation.\nexport function toolNameOf(names: Map<string, string>, callId: string): string {\n const name = names.get(callId);\n if (!name) {\n throw new WalletRequestError(`A tool result answers ${callId}, which no earlier tool call made`, 400);\n }\n return name;\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,63 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tensorpass/aiconnect",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Headless SDK: connect a visitor's own AI account and run the site's tools with it.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"license": "Apache-2.0",
|
|
8
|
+
"homepage": "https://tensorpass.app/docs",
|
|
9
|
+
"bugs": {
|
|
10
|
+
"email": "hello@tensorpass.app"
|
|
11
|
+
},
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"default": "./dist/index.js"
|
|
16
|
+
},
|
|
17
|
+
"./guards": {
|
|
18
|
+
"types": "./dist/guards/index.d.ts",
|
|
19
|
+
"default": "./dist/guards/index.js"
|
|
20
|
+
},
|
|
21
|
+
"./callback": {
|
|
22
|
+
"types": "./dist/callback.d.ts",
|
|
23
|
+
"default": "./dist/callback.js"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"bin": {
|
|
27
|
+
"aiconnect": "dist/cli/main.js"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"dist",
|
|
31
|
+
"LICENSE",
|
|
32
|
+
"NOTICE",
|
|
33
|
+
"README.md"
|
|
34
|
+
],
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "rm -rf dist tsconfig.tsbuildinfo tsconfig.cli.tsbuildinfo && tsc -b tsconfig.json tsconfig.cli.json"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@modelcontextprotocol/client": "^2.4.0"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@ai-sdk/anthropic": "^4.0.99",
|
|
43
|
+
"@ai-sdk/google": "^4.0.114",
|
|
44
|
+
"@ai-sdk/openai": "^4.0.111",
|
|
45
|
+
"@anthropic-ai/sdk": "^1.6.0",
|
|
46
|
+
"@google/genai": "^3.0.0",
|
|
47
|
+
"@langchain/core": "^1.3.5",
|
|
48
|
+
"@langchain/openai": "^1.8.2",
|
|
49
|
+
"@modelcontextprotocol/node": "^2.1.2",
|
|
50
|
+
"@modelcontextprotocol/sdk": "^1.32.2",
|
|
51
|
+
"@modelcontextprotocol/server": "^2.4.0",
|
|
52
|
+
"ai": "^7.0.165",
|
|
53
|
+
"openai": "^7.53.0"
|
|
54
|
+
},
|
|
55
|
+
"publishConfig": {
|
|
56
|
+
"access": "public"
|
|
57
|
+
},
|
|
58
|
+
"repository": {
|
|
59
|
+
"type": "git",
|
|
60
|
+
"url": "git+https://github.com/tensorpass/aiconnect.git",
|
|
61
|
+
"directory": "packages/core"
|
|
62
|
+
}
|
|
63
|
+
}
|