@anhora/sdk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +30 -0
- package/dist/client.d.ts +36 -0
- package/dist/client.js +42 -0
- package/dist/connectors.d.ts +54 -0
- package/dist/connectors.js +89 -0
- package/dist/deployments.d.ts +19 -0
- package/dist/deployments.js +33 -0
- package/dist/embed.d.ts +16 -0
- package/dist/embed.js +18 -0
- package/dist/http.d.ts +8 -0
- package/dist/http.js +47 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +11 -0
- package/dist/legacy.d.ts +17 -0
- package/dist/legacy.js +20 -0
- package/dist/types.d.ts +30 -0
- package/dist/types.js +27 -0
- package/package.json +57 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Anhora
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# @anhora/sdk
|
|
2
|
+
|
|
3
|
+
Framework-agnostic Anhora API client: connectors ingest/sync, content upsert, deployment runtime, embed release config.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm install @anhora/sdk
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { createAnhora } from '@anhora/sdk'
|
|
11
|
+
|
|
12
|
+
const anhora = createAnhora({
|
|
13
|
+
apiBase: 'https://api.anhora.net/api',
|
|
14
|
+
ingestSecret: process.env.ANHORA_INGEST_SECRET,
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
await anhora.content.upsert({
|
|
18
|
+
widgetId: '…',
|
|
19
|
+
source: 'payload',
|
|
20
|
+
externalId: 'page-1',
|
|
21
|
+
title: 'Home',
|
|
22
|
+
content: '…',
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
await anhora.connectors.sync({ widgetId: '…' })
|
|
26
|
+
await anhora.deployments.getRuntime(deploymentKey, { origin: 'https://example.com' })
|
|
27
|
+
await anhora.embed.getWidgetRelease('stable')
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Docs: [anhora.net](https://anhora.net) · Payload plugin: [`@anhora/payload`](https://www.npmjs.com/package/@anhora/payload) · Next: [`@anhora/next`](https://www.npmjs.com/package/@anhora/next)
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { AnhoraConnectorsApi, AnhoraContentApi } from './connectors';
|
|
2
|
+
import { AnhoraDeploymentsApi, AnhoraWidgetsApi } from './deployments';
|
|
3
|
+
import { AnhoraEmbedApi } from './embed';
|
|
4
|
+
import type { AnhoraClientConfig } from './types';
|
|
5
|
+
export type AnhoraClient = {
|
|
6
|
+
readonly apiBase: string;
|
|
7
|
+
readonly connectors: AnhoraConnectorsApi;
|
|
8
|
+
readonly content: AnhoraContentApi;
|
|
9
|
+
readonly deployments: AnhoraDeploymentsApi;
|
|
10
|
+
readonly embed: AnhoraEmbedApi;
|
|
11
|
+
readonly widgets: AnhoraWidgetsApi;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Create the Anhora API client (framework-agnostic).
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* const anhora = createAnhora({
|
|
19
|
+
* apiBase: process.env.ANHORA_API_BASE,
|
|
20
|
+
* ingestSecret: process.env.ANHORA_INGEST_SECRET,
|
|
21
|
+
* })
|
|
22
|
+
*
|
|
23
|
+
* await anhora.content.upsert({
|
|
24
|
+
* widgetId: '...',
|
|
25
|
+
* source: 'payload',
|
|
26
|
+
* externalId: page.id,
|
|
27
|
+
* title: page.title,
|
|
28
|
+
* content: plainText,
|
|
29
|
+
* })
|
|
30
|
+
*
|
|
31
|
+
* await anhora.connectors.sync({ widgetId: '...' })
|
|
32
|
+
* await anhora.deployments.getRuntime(deploymentKey, { origin })
|
|
33
|
+
* await anhora.embed.getWidgetRelease('stable')
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export declare function createAnhora(config?: AnhoraClientConfig): AnhoraClient;
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createAnhora = createAnhora;
|
|
4
|
+
const connectors_1 = require("./connectors");
|
|
5
|
+
const deployments_1 = require("./deployments");
|
|
6
|
+
const embed_1 = require("./embed");
|
|
7
|
+
const http_1 = require("./http");
|
|
8
|
+
/**
|
|
9
|
+
* Create the Anhora API client (framework-agnostic).
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* const anhora = createAnhora({
|
|
14
|
+
* apiBase: process.env.ANHORA_API_BASE,
|
|
15
|
+
* ingestSecret: process.env.ANHORA_INGEST_SECRET,
|
|
16
|
+
* })
|
|
17
|
+
*
|
|
18
|
+
* await anhora.content.upsert({
|
|
19
|
+
* widgetId: '...',
|
|
20
|
+
* source: 'payload',
|
|
21
|
+
* externalId: page.id,
|
|
22
|
+
* title: page.title,
|
|
23
|
+
* content: plainText,
|
|
24
|
+
* })
|
|
25
|
+
*
|
|
26
|
+
* await anhora.connectors.sync({ widgetId: '...' })
|
|
27
|
+
* await anhora.deployments.getRuntime(deploymentKey, { origin })
|
|
28
|
+
* await anhora.embed.getWidgetRelease('stable')
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
function createAnhora(config = {}) {
|
|
32
|
+
const http = new http_1.AnhoraHttp(config);
|
|
33
|
+
const connectors = new connectors_1.AnhoraConnectorsApi(http);
|
|
34
|
+
return {
|
|
35
|
+
apiBase: http.apiBase,
|
|
36
|
+
connectors,
|
|
37
|
+
content: new connectors_1.AnhoraContentApi(connectors),
|
|
38
|
+
deployments: new deployments_1.AnhoraDeploymentsApi(http),
|
|
39
|
+
embed: new embed_1.AnhoraEmbedApi(http),
|
|
40
|
+
widgets: new deployments_1.AnhoraWidgetsApi(http),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { ConnectorIngestRequest, ConnectorIngestResponse, ConnectorSyncRequest, IngestCatalogItem, IngestKnowledgeItem } from '@anhora/shared-types';
|
|
2
|
+
import type { AnhoraHttp } from './http';
|
|
3
|
+
export type ConnectorSyncResponse = {
|
|
4
|
+
ok: true;
|
|
5
|
+
widgetId: string;
|
|
6
|
+
source: string;
|
|
7
|
+
results: Record<string, unknown>;
|
|
8
|
+
};
|
|
9
|
+
export type ContentUpsertInput = {
|
|
10
|
+
widgetId: string;
|
|
11
|
+
/** Source namespace for external ids, e.g. `payload`, `wordpress`. */
|
|
12
|
+
source: string;
|
|
13
|
+
externalId: string;
|
|
14
|
+
title: string;
|
|
15
|
+
content: string;
|
|
16
|
+
url?: string;
|
|
17
|
+
/** Replace prior knowledge with this source prefix. */
|
|
18
|
+
replace?: boolean;
|
|
19
|
+
};
|
|
20
|
+
export type ContentUpsertBatchInput = {
|
|
21
|
+
widgetId: string;
|
|
22
|
+
source: string;
|
|
23
|
+
items: Array<{
|
|
24
|
+
externalId: string;
|
|
25
|
+
title: string;
|
|
26
|
+
content: string;
|
|
27
|
+
url?: string;
|
|
28
|
+
}>;
|
|
29
|
+
replace?: boolean;
|
|
30
|
+
};
|
|
31
|
+
export declare class AnhoraConnectorsApi {
|
|
32
|
+
private readonly http;
|
|
33
|
+
constructor(http: AnhoraHttp);
|
|
34
|
+
ingest(body: ConnectorIngestRequest): Promise<ConnectorIngestResponse>;
|
|
35
|
+
sync(body: ConnectorSyncRequest): Promise<ConnectorSyncResponse>;
|
|
36
|
+
ingestCatalog(input: {
|
|
37
|
+
widgetId: string;
|
|
38
|
+
items: IngestCatalogItem[];
|
|
39
|
+
replace?: boolean;
|
|
40
|
+
}): Promise<ConnectorIngestResponse>;
|
|
41
|
+
ingestKnowledge(input: {
|
|
42
|
+
widgetId: string;
|
|
43
|
+
items: IngestKnowledgeItem[];
|
|
44
|
+
replace?: boolean;
|
|
45
|
+
knowledgeExternalIdPrefix?: string;
|
|
46
|
+
}): Promise<ConnectorIngestResponse>;
|
|
47
|
+
}
|
|
48
|
+
export declare class AnhoraContentApi {
|
|
49
|
+
private readonly connectors;
|
|
50
|
+
constructor(connectors: AnhoraConnectorsApi);
|
|
51
|
+
/** Convenience upsert for a single CMS/document knowledge row. */
|
|
52
|
+
upsert(input: ContentUpsertInput): Promise<ConnectorIngestResponse>;
|
|
53
|
+
upsertMany(input: ContentUpsertBatchInput): Promise<ConnectorIngestResponse>;
|
|
54
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AnhoraContentApi = exports.AnhoraConnectorsApi = void 0;
|
|
4
|
+
function withSourcePrefix(source, externalId) {
|
|
5
|
+
const trimmedSource = source.trim().replace(/:+$/u, '');
|
|
6
|
+
const trimmedId = externalId.trim();
|
|
7
|
+
if (!trimmedSource)
|
|
8
|
+
return trimmedId;
|
|
9
|
+
if (trimmedId.startsWith(`${trimmedSource}:`))
|
|
10
|
+
return trimmedId;
|
|
11
|
+
return `${trimmedSource}:${trimmedId}`;
|
|
12
|
+
}
|
|
13
|
+
class AnhoraConnectorsApi {
|
|
14
|
+
http;
|
|
15
|
+
constructor(http) {
|
|
16
|
+
this.http = http;
|
|
17
|
+
}
|
|
18
|
+
ingest(body) {
|
|
19
|
+
return this.http.request('/v1/connectors/ingest', {
|
|
20
|
+
method: 'POST',
|
|
21
|
+
body,
|
|
22
|
+
auth: 'ingest',
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
sync(body) {
|
|
26
|
+
return this.http.request('/v1/connectors/sync', {
|
|
27
|
+
method: 'POST',
|
|
28
|
+
body,
|
|
29
|
+
auth: 'ingest',
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
ingestCatalog(input) {
|
|
33
|
+
return this.ingest({
|
|
34
|
+
widgetId: input.widgetId,
|
|
35
|
+
catalog: input.items,
|
|
36
|
+
replace: input.replace ? { catalog: true } : undefined,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
ingestKnowledge(input) {
|
|
40
|
+
return this.ingest({
|
|
41
|
+
widgetId: input.widgetId,
|
|
42
|
+
knowledge: input.items,
|
|
43
|
+
replace: input.replace
|
|
44
|
+
? {
|
|
45
|
+
knowledge: true,
|
|
46
|
+
knowledgeExternalIdPrefix: input.knowledgeExternalIdPrefix,
|
|
47
|
+
}
|
|
48
|
+
: undefined,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
exports.AnhoraConnectorsApi = AnhoraConnectorsApi;
|
|
53
|
+
class AnhoraContentApi {
|
|
54
|
+
connectors;
|
|
55
|
+
constructor(connectors) {
|
|
56
|
+
this.connectors = connectors;
|
|
57
|
+
}
|
|
58
|
+
/** Convenience upsert for a single CMS/document knowledge row. */
|
|
59
|
+
upsert(input) {
|
|
60
|
+
return this.upsertMany({
|
|
61
|
+
widgetId: input.widgetId,
|
|
62
|
+
source: input.source,
|
|
63
|
+
replace: input.replace,
|
|
64
|
+
items: [
|
|
65
|
+
{
|
|
66
|
+
externalId: input.externalId,
|
|
67
|
+
title: input.title,
|
|
68
|
+
content: input.content,
|
|
69
|
+
url: input.url,
|
|
70
|
+
},
|
|
71
|
+
],
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
upsertMany(input) {
|
|
75
|
+
const prefix = input.source.trim().replace(/:+$/u, '');
|
|
76
|
+
return this.connectors.ingestKnowledge({
|
|
77
|
+
widgetId: input.widgetId,
|
|
78
|
+
knowledgeExternalIdPrefix: prefix ? `${prefix}:` : undefined,
|
|
79
|
+
replace: input.replace,
|
|
80
|
+
items: input.items.map((item) => ({
|
|
81
|
+
externalId: withSourcePrefix(input.source, item.externalId),
|
|
82
|
+
name: item.title,
|
|
83
|
+
content: item.content,
|
|
84
|
+
url: item.url,
|
|
85
|
+
})),
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
exports.AnhoraContentApi = AnhoraContentApi;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { PublicWidgetConfig, PublicWidgetRuntimeConfig } from '@anhora/shared-types';
|
|
2
|
+
import type { AnhoraHttp } from './http';
|
|
3
|
+
export declare class AnhoraDeploymentsApi {
|
|
4
|
+
private readonly http;
|
|
5
|
+
constructor(http: AnhoraHttp);
|
|
6
|
+
/**
|
|
7
|
+
* Browser/SSR runtime bootstrap for a Site Chat deployment key.
|
|
8
|
+
* In production the API requires a matching HTTPS `Origin`.
|
|
9
|
+
*/
|
|
10
|
+
getRuntime(deploymentKey: string, options?: {
|
|
11
|
+
origin?: string;
|
|
12
|
+
}): Promise<PublicWidgetRuntimeConfig>;
|
|
13
|
+
}
|
|
14
|
+
export declare class AnhoraWidgetsApi {
|
|
15
|
+
private readonly http;
|
|
16
|
+
constructor(http: AnhoraHttp);
|
|
17
|
+
/** Legacy public widget config by widget id (SaaS path). */
|
|
18
|
+
getConfig(widgetId: string): Promise<PublicWidgetConfig>;
|
|
19
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AnhoraWidgetsApi = exports.AnhoraDeploymentsApi = void 0;
|
|
4
|
+
class AnhoraDeploymentsApi {
|
|
5
|
+
http;
|
|
6
|
+
constructor(http) {
|
|
7
|
+
this.http = http;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Browser/SSR runtime bootstrap for a Site Chat deployment key.
|
|
11
|
+
* In production the API requires a matching HTTPS `Origin`.
|
|
12
|
+
*/
|
|
13
|
+
getRuntime(deploymentKey, options = {}) {
|
|
14
|
+
const key = encodeURIComponent(deploymentKey.trim());
|
|
15
|
+
return this.http.request(`/v1/deployments/${key}/runtime`, {
|
|
16
|
+
method: 'GET',
|
|
17
|
+
origin: options.origin,
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
exports.AnhoraDeploymentsApi = AnhoraDeploymentsApi;
|
|
22
|
+
class AnhoraWidgetsApi {
|
|
23
|
+
http;
|
|
24
|
+
constructor(http) {
|
|
25
|
+
this.http = http;
|
|
26
|
+
}
|
|
27
|
+
/** Legacy public widget config by widget id (SaaS path). */
|
|
28
|
+
getConfig(widgetId) {
|
|
29
|
+
const id = encodeURIComponent(widgetId.trim());
|
|
30
|
+
return this.http.request(`/v1/widgets/${id}/config`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
exports.AnhoraWidgetsApi = AnhoraWidgetsApi;
|
package/dist/embed.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { WidgetReleaseChannel, WidgetReleaseConfig } from '@anhora/shared-types';
|
|
2
|
+
import type { AnhoraHttp } from './http';
|
|
3
|
+
export type WidgetReleaseWithUrl = WidgetReleaseConfig & {
|
|
4
|
+
scriptUrl?: string;
|
|
5
|
+
cdnBaseUrl?: string;
|
|
6
|
+
};
|
|
7
|
+
export declare class AnhoraEmbedApi {
|
|
8
|
+
private readonly http;
|
|
9
|
+
constructor(http: AnhoraHttp);
|
|
10
|
+
/**
|
|
11
|
+
* Resolve stable/beta channel to a versioned widget script via the API.
|
|
12
|
+
* CDN JSON at `{cdn}/widget/channels/{channel}.json` is the primary path
|
|
13
|
+
* for the browser loader; this method is for servers/tooling.
|
|
14
|
+
*/
|
|
15
|
+
getWidgetRelease(channel?: WidgetReleaseChannel): Promise<WidgetReleaseWithUrl>;
|
|
16
|
+
}
|
package/dist/embed.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AnhoraEmbedApi = void 0;
|
|
4
|
+
class AnhoraEmbedApi {
|
|
5
|
+
http;
|
|
6
|
+
constructor(http) {
|
|
7
|
+
this.http = http;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Resolve stable/beta channel to a versioned widget script via the API.
|
|
11
|
+
* CDN JSON at `{cdn}/widget/channels/{channel}.json` is the primary path
|
|
12
|
+
* for the browser loader; this method is for servers/tooling.
|
|
13
|
+
*/
|
|
14
|
+
getWidgetRelease(channel = 'stable') {
|
|
15
|
+
return this.http.request(`/v1/embed/widget?channel=${encodeURIComponent(channel)}`);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.AnhoraEmbedApi = AnhoraEmbedApi;
|
package/dist/http.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { AnhoraClientConfig, RequestOptions } from './types';
|
|
2
|
+
export declare class AnhoraHttp {
|
|
3
|
+
readonly apiBase: string;
|
|
4
|
+
readonly ingestSecret?: string;
|
|
5
|
+
private readonly fetchImpl;
|
|
6
|
+
constructor(config?: AnhoraClientConfig);
|
|
7
|
+
request<T>(path: string, options?: RequestOptions): Promise<T>;
|
|
8
|
+
}
|
package/dist/http.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AnhoraHttp = void 0;
|
|
4
|
+
const types_1 = require("./types");
|
|
5
|
+
const types_2 = require("./types");
|
|
6
|
+
class AnhoraHttp {
|
|
7
|
+
apiBase;
|
|
8
|
+
ingestSecret;
|
|
9
|
+
fetchImpl;
|
|
10
|
+
constructor(config = {}) {
|
|
11
|
+
this.apiBase = (0, types_2.normalizeApiBase)(config.apiBase ?? 'https://api.anhora.net/api');
|
|
12
|
+
this.ingestSecret = config.ingestSecret?.trim() || undefined;
|
|
13
|
+
this.fetchImpl = config.fetch ?? fetch;
|
|
14
|
+
}
|
|
15
|
+
async request(path, options = {}) {
|
|
16
|
+
const headers = {
|
|
17
|
+
Accept: 'application/json',
|
|
18
|
+
...options.headers,
|
|
19
|
+
};
|
|
20
|
+
if (options.body !== undefined) {
|
|
21
|
+
headers['Content-Type'] = 'application/json';
|
|
22
|
+
}
|
|
23
|
+
if (options.auth === 'ingest') {
|
|
24
|
+
if (!this.ingestSecret) {
|
|
25
|
+
throw new Error('Anhora ingestSecret is required for connectors.ingest / connectors.sync');
|
|
26
|
+
}
|
|
27
|
+
headers['X-Anhora-Ingest-Secret'] = this.ingestSecret;
|
|
28
|
+
}
|
|
29
|
+
if (options.origin) {
|
|
30
|
+
headers.Origin = options.origin;
|
|
31
|
+
}
|
|
32
|
+
const response = await this.fetchImpl(`${this.apiBase}${path}`, {
|
|
33
|
+
method: options.method ?? (options.body !== undefined ? 'POST' : 'GET'),
|
|
34
|
+
headers,
|
|
35
|
+
body: options.body === undefined ? undefined : JSON.stringify(options.body),
|
|
36
|
+
});
|
|
37
|
+
if (!response.ok) {
|
|
38
|
+
const text = await response.text();
|
|
39
|
+
throw new types_1.AnhoraApiError(`Anhora API ${options.method ?? 'GET'} ${path} failed: ${response.status}`, response.status, text);
|
|
40
|
+
}
|
|
41
|
+
if (response.status === 204) {
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
return (await response.json());
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
exports.AnhoraHttp = AnhoraHttp;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { createAnhora } from './client';
|
|
2
|
+
export type { AnhoraClient } from './client';
|
|
3
|
+
export { AnhoraApiError, normalizeApiBase } from './types';
|
|
4
|
+
export type { AnhoraClientConfig, AnhoraFetch, RequestOptions, } from './types';
|
|
5
|
+
export type { ConnectorSyncResponse, ContentUpsertBatchInput, ContentUpsertInput, } from './connectors';
|
|
6
|
+
export type { WidgetReleaseWithUrl } from './embed';
|
|
7
|
+
/** @deprecated Use `createAnhora().widgets.getConfig` / `deployments.getRuntime`. */
|
|
8
|
+
export { AnhoraClient as LegacyAnhoraClient } from './legacy';
|
|
9
|
+
export type { AnhoraClientOptions as LegacyAnhoraClientOptions } from './legacy';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LegacyAnhoraClient = exports.normalizeApiBase = exports.AnhoraApiError = exports.createAnhora = void 0;
|
|
4
|
+
var client_1 = require("./client");
|
|
5
|
+
Object.defineProperty(exports, "createAnhora", { enumerable: true, get: function () { return client_1.createAnhora; } });
|
|
6
|
+
var types_1 = require("./types");
|
|
7
|
+
Object.defineProperty(exports, "AnhoraApiError", { enumerable: true, get: function () { return types_1.AnhoraApiError; } });
|
|
8
|
+
Object.defineProperty(exports, "normalizeApiBase", { enumerable: true, get: function () { return types_1.normalizeApiBase; } });
|
|
9
|
+
/** @deprecated Use `createAnhora().widgets.getConfig` / `deployments.getRuntime`. */
|
|
10
|
+
var legacy_1 = require("./legacy");
|
|
11
|
+
Object.defineProperty(exports, "LegacyAnhoraClient", { enumerable: true, get: function () { return legacy_1.AnhoraClient; } });
|
package/dist/legacy.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { ChatResponse, PublicWidgetConfig } from '@anhora/shared-types';
|
|
2
|
+
/**
|
|
3
|
+
* @deprecated Prefer `createAnhora({ apiBase })`.
|
|
4
|
+
* Kept temporarily for older snippets that used widgetId + /chat.
|
|
5
|
+
*/
|
|
6
|
+
export interface AnhoraClientOptions {
|
|
7
|
+
baseUrl: string;
|
|
8
|
+
widgetId: string;
|
|
9
|
+
}
|
|
10
|
+
/** @deprecated */
|
|
11
|
+
export declare class AnhoraClient {
|
|
12
|
+
private readonly anhora;
|
|
13
|
+
private readonly widgetId;
|
|
14
|
+
constructor(options: AnhoraClientOptions);
|
|
15
|
+
getWidgetConfig(): Promise<PublicWidgetConfig>;
|
|
16
|
+
sendMessage(_message: string, _sessionId?: string): Promise<ChatResponse>;
|
|
17
|
+
}
|
package/dist/legacy.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AnhoraClient = void 0;
|
|
4
|
+
const client_1 = require("./client");
|
|
5
|
+
/** @deprecated */
|
|
6
|
+
class AnhoraClient {
|
|
7
|
+
anhora;
|
|
8
|
+
widgetId;
|
|
9
|
+
constructor(options) {
|
|
10
|
+
this.widgetId = options.widgetId;
|
|
11
|
+
this.anhora = (0, client_1.createAnhora)({ apiBase: options.baseUrl });
|
|
12
|
+
}
|
|
13
|
+
getWidgetConfig() {
|
|
14
|
+
return this.anhora.widgets.getConfig(this.widgetId);
|
|
15
|
+
}
|
|
16
|
+
async sendMessage(_message, _sessionId) {
|
|
17
|
+
throw new Error('Legacy AnhoraClient.sendMessage is removed. Use the embed widget or POST /api/v1/chat/message with a deploymentKey.');
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
exports.AnhoraClient = AnhoraClient;
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export declare class AnhoraApiError extends Error {
|
|
2
|
+
readonly status: number;
|
|
3
|
+
readonly body: string;
|
|
4
|
+
constructor(message: string, status: number, body: string);
|
|
5
|
+
}
|
|
6
|
+
export type AnhoraFetch = typeof fetch;
|
|
7
|
+
export type AnhoraClientConfig = {
|
|
8
|
+
/**
|
|
9
|
+
* API base including `/api`, e.g. `https://api.anhora.net/api`.
|
|
10
|
+
* @default 'https://api.anhora.net/api'
|
|
11
|
+
*/
|
|
12
|
+
apiBase?: string;
|
|
13
|
+
/**
|
|
14
|
+
* Server-side ingest secret for `/connectors/ingest` and `/connectors/sync`.
|
|
15
|
+
* Sent as `X-Anhora-Ingest-Secret` (Bearer also accepted by the API).
|
|
16
|
+
*/
|
|
17
|
+
ingestSecret?: string;
|
|
18
|
+
/** Override fetch (tests, custom agents). */
|
|
19
|
+
fetch?: AnhoraFetch;
|
|
20
|
+
};
|
|
21
|
+
export type RequestOptions = {
|
|
22
|
+
method?: 'GET' | 'POST' | 'PUT' | 'DELETE';
|
|
23
|
+
body?: unknown;
|
|
24
|
+
headers?: Record<string, string>;
|
|
25
|
+
/** When true, attach ingest secret headers. */
|
|
26
|
+
auth?: 'ingest';
|
|
27
|
+
/** Browser Origin for deployment runtime checks. */
|
|
28
|
+
origin?: string;
|
|
29
|
+
};
|
|
30
|
+
export declare function normalizeApiBase(apiBase: string): string;
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AnhoraApiError = void 0;
|
|
4
|
+
exports.normalizeApiBase = normalizeApiBase;
|
|
5
|
+
class AnhoraApiError extends Error {
|
|
6
|
+
status;
|
|
7
|
+
body;
|
|
8
|
+
constructor(message, status, body) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = 'AnhoraApiError';
|
|
11
|
+
this.status = status;
|
|
12
|
+
this.body = body;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
exports.AnhoraApiError = AnhoraApiError;
|
|
16
|
+
function normalizeApiBase(apiBase) {
|
|
17
|
+
let normalized = apiBase.trim().replace(/\/+$/u, '');
|
|
18
|
+
if (!normalized) {
|
|
19
|
+
return 'https://api.anhora.net/api';
|
|
20
|
+
}
|
|
21
|
+
// Accept both `.../api` and `.../api/v1` from older snippets.
|
|
22
|
+
normalized = normalized.replace(/\/v\d+$/u, '');
|
|
23
|
+
if (!normalized.endsWith('/api')) {
|
|
24
|
+
normalized = `${normalized}/api`;
|
|
25
|
+
}
|
|
26
|
+
return normalized;
|
|
27
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@anhora/sdk",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Framework-agnostic Anhora API client (connectors, content ingest, deployment runtime)",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Anhora",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/denys-vodniakov/anhora.git",
|
|
10
|
+
"directory": "packages/sdk"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://anhora.net",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/denys-vodniakov/anhora/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"anhora",
|
|
18
|
+
"sdk",
|
|
19
|
+
"chat",
|
|
20
|
+
"knowledge"
|
|
21
|
+
],
|
|
22
|
+
"type": "commonjs",
|
|
23
|
+
"main": "./dist/index.js",
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"default": "./dist/index.js"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"dist",
|
|
33
|
+
"LICENSE",
|
|
34
|
+
"README.md"
|
|
35
|
+
],
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public"
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"@anhora/shared-types": "0.1.0"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"eslint": "^9.24.0",
|
|
44
|
+
"tsx": "^4.23.0",
|
|
45
|
+
"typescript": "^5.8.3",
|
|
46
|
+
"@anhora/eslint-config": "0.0.1"
|
|
47
|
+
},
|
|
48
|
+
"engines": {
|
|
49
|
+
"node": ">=20"
|
|
50
|
+
},
|
|
51
|
+
"scripts": {
|
|
52
|
+
"build": "tsc",
|
|
53
|
+
"lint": "eslint src/",
|
|
54
|
+
"typecheck": "tsc --noEmit",
|
|
55
|
+
"test": "node --import tsx --test test/**/*.spec.ts"
|
|
56
|
+
}
|
|
57
|
+
}
|