@maximem/synap-js-sdk 0.3.0 → 0.4.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/MIGRATING.md +147 -0
- package/README.md +95 -82
- package/bin/synap-js-sdk.js +45 -122
- package/dist/anticipation-cache-ARYEEKMU.cjs +10 -0
- package/dist/anticipation-cache-ARYEEKMU.cjs.map +1 -0
- package/dist/anticipation-cache-BMVF5sXb.d.cts +190 -0
- package/dist/anticipation-cache-BMVF5sXb.d.ts +190 -0
- package/dist/anticipation-cache-H7HEVWQC.js +10 -0
- package/dist/anticipation-cache-H7HEVWQC.js.map +1 -0
- package/dist/chunk-5SC3ELNK.js +856 -0
- package/dist/chunk-5SC3ELNK.js.map +1 -0
- package/dist/chunk-6LDUDKHS.js +832 -0
- package/dist/chunk-6LDUDKHS.js.map +1 -0
- package/dist/chunk-HR5Y3KXS.cjs +172 -0
- package/dist/chunk-HR5Y3KXS.cjs.map +1 -0
- package/dist/chunk-QBCVMPGK.js +28 -0
- package/dist/chunk-QBCVMPGK.js.map +1 -0
- package/dist/chunk-RB57I2IQ.cjs +832 -0
- package/dist/chunk-RB57I2IQ.cjs.map +1 -0
- package/dist/chunk-T3NZBIV2.js +172 -0
- package/dist/chunk-T3NZBIV2.js.map +1 -0
- package/dist/chunk-VXRCBMCY.cjs +856 -0
- package/dist/chunk-VXRCBMCY.cjs.map +1 -0
- package/dist/chunk-ZKOJDUN4.cjs +28 -0
- package/dist/chunk-ZKOJDUN4.cjs.map +1 -0
- package/dist/env-55ULHNJZ.cjs +11 -0
- package/dist/env-55ULHNJZ.cjs.map +1 -0
- package/dist/env-QCUKKW36.js +11 -0
- package/dist/env-QCUKKW36.js.map +1 -0
- package/dist/grpc/index.cjs +87 -0
- package/dist/grpc/index.cjs.map +1 -0
- package/dist/grpc/index.d.cts +162 -0
- package/dist/grpc/index.d.ts +162 -0
- package/dist/grpc/index.js +87 -0
- package/dist/grpc/index.js.map +1 -0
- package/dist/index.cjs +2702 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +1491 -0
- package/dist/index.d.ts +1491 -0
- package/dist/index.js +2702 -0
- package/dist/index.js.map +1 -0
- package/dist/stream-client-ME5N5NK4.js +7 -0
- package/dist/stream-client-ME5N5NK4.js.map +1 -0
- package/dist/stream-client-ZN6ADZBR.cjs +7 -0
- package/dist/stream-client-ZN6ADZBR.cjs.map +1 -0
- package/package.json +68 -23
- package/.env.example +0 -39
- package/bridge/synap_bridge.py +0 -768
- package/src/bridge-manager.js +0 -262
- package/src/errors.js +0 -168
- package/src/index.js +0 -13
- package/src/runtime.js +0 -161
- package/src/setup-typescript.js +0 -161
- package/src/synap-client.js +0 -464
- package/types/index.d.ts +0 -475
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ListeningNotActiveError
|
|
3
|
+
} from "../chunk-T3NZBIV2.js";
|
|
4
|
+
import {
|
|
5
|
+
GrpcStreamClient,
|
|
6
|
+
PROTO_SHA
|
|
7
|
+
} from "../chunk-5SC3ELNK.js";
|
|
8
|
+
|
|
9
|
+
// src/grpc/index.ts
|
|
10
|
+
async function checkGrpcAvailability() {
|
|
11
|
+
const isNode = typeof process !== "undefined" && process.versions?.node !== void 0 && typeof globalThis.EdgeRuntime === "undefined";
|
|
12
|
+
if (!isNode) {
|
|
13
|
+
return {
|
|
14
|
+
available: false,
|
|
15
|
+
reason: "not-node",
|
|
16
|
+
detail: "gRPC needs raw TCP and node:http2, which Edge runtimes and Workers do not provide. HTTP context fetching works normally here; only the anticipation stream is unavailable. Reaching Edge would require a gRPC-Web or Connect endpoint on the server, which does not exist yet."
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
try {
|
|
20
|
+
await import(
|
|
21
|
+
/* @vite-ignore */
|
|
22
|
+
"@grpc/grpc-js"
|
|
23
|
+
);
|
|
24
|
+
return { available: true };
|
|
25
|
+
} catch (error) {
|
|
26
|
+
return {
|
|
27
|
+
available: false,
|
|
28
|
+
reason: "module-missing",
|
|
29
|
+
detail: `@grpc/grpc-js is an optional dependency and is not installed. Run \`npm install @grpc/grpc-js\` to enable the anticipation stream. (${error?.message ?? String(error)})`
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
async function listen(options) {
|
|
34
|
+
if (!options.conversationId) {
|
|
35
|
+
throw new ListeningNotActiveError("conversation_id is required to start a listening stream");
|
|
36
|
+
}
|
|
37
|
+
const availability = await checkGrpcAvailability();
|
|
38
|
+
if (!availability.available) {
|
|
39
|
+
throw new ListeningNotActiveError(availability.detail);
|
|
40
|
+
}
|
|
41
|
+
const [{ GrpcStreamClient: StreamClient }, { AnticipationCache }, { getEnv }] = await Promise.all([
|
|
42
|
+
import("../stream-client-ME5N5NK4.js"),
|
|
43
|
+
import("../anticipation-cache-H7HEVWQC.js"),
|
|
44
|
+
import("../env-QCUKKW36.js")
|
|
45
|
+
]);
|
|
46
|
+
const apiKey = options.apiKey ?? getEnv("SYNAP_API_KEY") ?? "";
|
|
47
|
+
if (apiKey === "") {
|
|
48
|
+
throw new ListeningNotActiveError(
|
|
49
|
+
"No Synap API key found. Set SYNAP_API_KEY or pass apiKey."
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
const cache = new AnticipationCache();
|
|
53
|
+
const client = new StreamClient(
|
|
54
|
+
{
|
|
55
|
+
apiKey,
|
|
56
|
+
clientId: options.clientId ?? getEnv("SYNAP_CLIENT_ID") ?? "",
|
|
57
|
+
instanceId: options.instanceId ?? getEnv("SYNAP_INSTANCE_ID") ?? ""
|
|
58
|
+
},
|
|
59
|
+
cache,
|
|
60
|
+
{
|
|
61
|
+
...options.host !== void 0 ? { host: options.host } : {},
|
|
62
|
+
...options.port !== void 0 ? { port: options.port } : {},
|
|
63
|
+
...options.useTls !== void 0 ? { useTls: options.useTls } : {},
|
|
64
|
+
...options.onBundle !== void 0 ? { onContext: options.onBundle } : {}
|
|
65
|
+
}
|
|
66
|
+
);
|
|
67
|
+
await client.connect();
|
|
68
|
+
client.sendSessionControl({
|
|
69
|
+
action: "start",
|
|
70
|
+
conversation_id: options.conversationId,
|
|
71
|
+
...options.userId !== void 0 ? { user_id: options.userId } : {},
|
|
72
|
+
...options.customerId !== void 0 ? { customer_id: options.customerId } : {}
|
|
73
|
+
});
|
|
74
|
+
return {
|
|
75
|
+
stop: () => client.disconnect(),
|
|
76
|
+
get active() {
|
|
77
|
+
return client.isConnected;
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
export {
|
|
82
|
+
GrpcStreamClient,
|
|
83
|
+
PROTO_SHA,
|
|
84
|
+
checkGrpcAvailability,
|
|
85
|
+
listen
|
|
86
|
+
};
|
|
87
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/grpc/index.ts"],"sourcesContent":["/**\n * gRPC anticipation stream: availability and loading.\n *\n * This is a SUBPATH export (`@maximem/synap-js-sdk/grpc`) and is never\n * imported by the main entry point. Three reasons, all of which have bitten\n * someone already:\n *\n * 1. `@grpc/grpc-js` plus its protos is roughly 5MB. HTTP-only and Edge users\n * should not pay for it (gotcha G-Q).\n * 2. grpc-js is built on `node:http2`, `node:net` and `node:tls`. Merely\n * importing it on Edge or Workers fails the *build*, not the call. Keeping\n * it behind a subpath plus a lazy `import()` means importing the SDK on\n * Edge stays safe.\n * 3. It is an `optionalDependency`, so it may legitimately be absent.\n *\n * ## Proto loading\n *\n * The stream is live. The proto ships as an INLINED JSON descriptor\n * (./descriptor.ts) loaded through `proto-loader.fromJSON`, not through\n * `loadSync(path)`. proto-loader's file-based path reads the `.proto` off disk\n * at runtime, which breaks under webpack/turbopack bundling and Vercel's `nft`\n * file tracing: at deploy time rather than at test time (gotcha G-P). Inlining\n * gets the same result as ts-proto codegen without a codegen toolchain.\n */\n\nimport { ListeningNotActiveError } from '../errors.js';\nexport { GrpcStreamClient } from './stream-client.js';\nexport type {\n StreamState, StreamCredentials, StreamClientOptions, ConversationEventInput,\n} from './stream-client.js';\nexport { PROTO_SHA } from './descriptor.js';\n\nexport type GrpcAvailability =\n | { available: true }\n | { available: false; reason: 'not-node' | 'module-missing'; detail: string };\n\n/**\n * Whether a gRPC stream could run here.\n *\n * Deliberately does not throw: callers use this to decide whether to offer the\n * stream at all, and a hard failure at that point is unhelpful.\n */\nexport async function checkGrpcAvailability(): Promise<GrpcAvailability> {\n const isNode =\n typeof process !== 'undefined' &&\n process.versions?.node !== undefined &&\n typeof (globalThis as { EdgeRuntime?: unknown }).EdgeRuntime === 'undefined';\n\n if (!isNode) {\n return {\n available: false,\n reason: 'not-node',\n detail:\n 'gRPC needs raw TCP and node:http2, which Edge runtimes and Workers do ' +\n 'not provide. HTTP context fetching works normally here; only the ' +\n 'anticipation stream is unavailable. Reaching Edge would require a ' +\n 'gRPC-Web or Connect endpoint on the server, which does not exist yet.',\n };\n }\n\n try {\n await import(/* @vite-ignore */ '@grpc/grpc-js');\n return { available: true };\n } catch (error) {\n return {\n available: false,\n reason: 'module-missing',\n detail:\n '@grpc/grpc-js is an optional dependency and is not installed. Run ' +\n '`npm install @grpc/grpc-js` to enable the anticipation stream. ' +\n `(${(error as Error)?.message ?? String(error)})`,\n };\n }\n}\n\nexport interface ListenOptions {\n conversationId: string;\n userId?: string;\n customerId?: string;\n onBundle?: (bundle: unknown) => void;\n}\n\nexport interface ListeningSession {\n stop(): Promise<void>;\n readonly active: boolean;\n}\n\n/**\n * Start the anticipation stream directly, without a SynapClient.\n *\n * Most callers want `client.instance.listen()` instead, which shares the\n * client's credentials and anticipation cache. This exists for the case where\n * only the stream is wanted.\n *\n * **Opt-in, and off by default.** The removed wrapper called `listen()`\n * unconditionally during init, so every user had a stream whether they asked\n * for one or not. Making it opt-in is the right default, but it is not a\n * neutral change: fewer streams means fewer anticipation hits and therefore\n * MORE billed cloud fetches for existing users. That has to be announced\n * rather than discovered (gotcha G-Y).\n */\nexport async function listen(\n options: ListenOptions & {\n apiKey?: string;\n clientId?: string;\n instanceId?: string;\n host?: string;\n port?: number;\n useTls?: boolean;\n },\n): Promise<ListeningSession> {\n if (!options.conversationId) {\n // conversation_id is not a scope tier, but the stream genuinely requires\n // one: it is what the server keys the anticipation session on.\n throw new ListeningNotActiveError('conversation_id is required to start a listening stream');\n }\n\n const availability = await checkGrpcAvailability();\n if (!availability.available) {\n throw new ListeningNotActiveError(availability.detail);\n }\n\n const [{ GrpcStreamClient: StreamClient }, { AnticipationCache }, { getEnv }] = await Promise.all([\n import('./stream-client.js'),\n import('../context/anticipation-cache.js'),\n import('../util/env.js'),\n ]);\n\n const apiKey = options.apiKey ?? getEnv('SYNAP_API_KEY') ?? '';\n if (apiKey === '') {\n throw new ListeningNotActiveError(\n 'No Synap API key found. Set SYNAP_API_KEY or pass apiKey.',\n );\n }\n\n const cache = new AnticipationCache();\n const client = new StreamClient(\n {\n apiKey,\n clientId: options.clientId ?? getEnv('SYNAP_CLIENT_ID') ?? '',\n instanceId: options.instanceId ?? getEnv('SYNAP_INSTANCE_ID') ?? '',\n },\n cache,\n {\n ...(options.host !== undefined ? { host: options.host } : {}),\n ...(options.port !== undefined ? { port: options.port } : {}),\n ...(options.useTls !== undefined ? { useTls: options.useTls } : {}),\n ...(options.onBundle !== undefined ? { onContext: options.onBundle } : {}),\n },\n );\n await client.connect();\n client.sendSessionControl({\n action: 'start',\n conversation_id: options.conversationId,\n ...(options.userId !== undefined ? { user_id: options.userId } : {}),\n ...(options.customerId !== undefined ? { customer_id: options.customerId } : {}),\n });\n\n return {\n stop: () => client.disconnect(),\n get active() { return client.isConnected; },\n };\n}\n"],"mappings":";;;;;;;;;AA0CA,eAAsB,wBAAmD;AACvE,QAAM,SACJ,OAAO,YAAY,eACnB,QAAQ,UAAU,SAAS,UAC3B,OAAQ,WAAyC,gBAAgB;AAEnE,MAAI,CAAC,QAAQ;AACX,WAAO;AAAA,MACL,WAAW;AAAA,MACX,QAAQ;AAAA,MACR,QACE;AAAA,I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