@chat-ads/chatads-sdk 0.1.3 → 0.1.5
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 +14 -9
- package/dist/client.d.ts +0 -2
- package/dist/client.js +12 -10
- package/dist/examples/basic.d.ts +1 -0
- package/dist/examples/basic.js +22 -0
- package/dist/index.d.ts +1 -1
- package/dist/models.d.ts +32 -18
- package/dist/models.js +8 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -27,7 +27,6 @@ const client = new ChatAdsClient({
|
|
|
27
27
|
const response = await client.analyze({
|
|
28
28
|
message: "Looking for CRM tools for a 10-person sales team",
|
|
29
29
|
ip: "8.8.8.8",
|
|
30
|
-
userAgent: "Mozilla/5.0",
|
|
31
30
|
});
|
|
32
31
|
|
|
33
32
|
if (response.success && response.data?.ad) {
|
|
@@ -41,15 +40,12 @@ if (response.success && response.data?.ad) {
|
|
|
41
40
|
|
|
42
41
|
```ts
|
|
43
42
|
const result = await client.analyzeMessage("Need scheduling ideas", {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
extraFields: {
|
|
47
|
-
formId: "lead-gen-7",
|
|
48
|
-
},
|
|
43
|
+
country: "US",
|
|
44
|
+
message_analysis: "balanced",
|
|
49
45
|
});
|
|
50
46
|
```
|
|
51
47
|
|
|
52
|
-
- Reserved payload keys (like `message`, `
|
|
48
|
+
- Reserved payload keys (like `message`, `country`, etc.) cannot be overwritten inside `extraFields`; the client throws if it detects a collision.
|
|
53
49
|
- Pass `raiseOnFailure: true` to throw `ChatAdsAPIError` when the API returns `success: false` with HTTP 200 responses.
|
|
54
50
|
- Retries honor `Retry-After` headers and exponential backoff (`maxRetries` + `retryBackoffFactorMs`).
|
|
55
51
|
|
|
@@ -95,23 +91,32 @@ try {
|
|
|
95
91
|
"success": true,
|
|
96
92
|
"data": {
|
|
97
93
|
"matched": true,
|
|
94
|
+
"filled": true,
|
|
98
95
|
"ad": {
|
|
99
96
|
"product": "CRM Pro",
|
|
100
97
|
"link": "https://getchatads.com/example",
|
|
101
98
|
"message": "Try CRM Pro for your sales team",
|
|
102
99
|
"category": "Software"
|
|
103
|
-
}
|
|
100
|
+
},
|
|
101
|
+
"keyword": "CRM tools",
|
|
102
|
+
"intent_score": 0.85,
|
|
103
|
+
"intent_level": "high"
|
|
104
104
|
},
|
|
105
105
|
"error": null,
|
|
106
106
|
"meta": {
|
|
107
107
|
"request_id": "req_123",
|
|
108
|
+
"extraction_method": "llm",
|
|
109
|
+
"message_analysis_used": "balanced",
|
|
110
|
+
"fill_priority_used": "coverage",
|
|
111
|
+
"min_intent_used": "low",
|
|
108
112
|
"processing_time_ms": 42.3,
|
|
109
113
|
"usage": {
|
|
110
114
|
"monthly_requests": 120,
|
|
111
115
|
"free_tier_limit": 1000,
|
|
112
116
|
"free_tier_remaining": 880,
|
|
113
117
|
"is_free_tier": false,
|
|
114
|
-
"
|
|
118
|
+
"daily_requests": 20,
|
|
119
|
+
"daily_limit": 100
|
|
115
120
|
}
|
|
116
121
|
}
|
|
117
122
|
}
|
package/dist/client.d.ts
CHANGED
|
@@ -12,7 +12,6 @@ export interface ChatAdsClientOptions {
|
|
|
12
12
|
raiseOnFailure?: boolean;
|
|
13
13
|
fetchImplementation?: FetchLike;
|
|
14
14
|
logger?: Logger;
|
|
15
|
-
userAgent?: string;
|
|
16
15
|
}
|
|
17
16
|
export interface AnalyzeOptions {
|
|
18
17
|
timeoutMs?: number;
|
|
@@ -32,7 +31,6 @@ export declare class ChatAdsClient {
|
|
|
32
31
|
private readonly raiseOnFailure;
|
|
33
32
|
private readonly fetchImpl;
|
|
34
33
|
private readonly logger?;
|
|
35
|
-
private readonly userAgent?;
|
|
36
34
|
constructor(options: ChatAdsClientOptions);
|
|
37
35
|
analyze(payload: FunctionItemPayload, options?: AnalyzeOptions): Promise<ChatAdsResponseEnvelope>;
|
|
38
36
|
analyzeMessage(message: string, extra?: AnalyzeMessageOptions, options?: AnalyzeOptions): Promise<ChatAdsResponseEnvelope>;
|
package/dist/client.js
CHANGED
|
@@ -4,13 +4,19 @@ const DEFAULT_ENDPOINT = "/v1/chatads/messages";
|
|
|
4
4
|
const DEFAULT_TIMEOUT_MS = 10000;
|
|
5
5
|
const DEFAULT_BACKOFF_FACTOR = 500; // ms
|
|
6
6
|
const DEFAULT_RETRYABLE_STATUSES = new Set([408, 409, 425, 429, 500, 502, 503, 504]);
|
|
7
|
+
/**
|
|
8
|
+
* Field aliases for normalizing optional field names.
|
|
9
|
+
* Only includes the 6 optional fields from the OpenAPI spec.
|
|
10
|
+
*/
|
|
7
11
|
const FIELD_ALIASES = {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
messageanalysis: "message_analysis",
|
|
13
|
+
message_analysis: "message_analysis",
|
|
14
|
+
fillpriority: "fill_priority",
|
|
15
|
+
fill_priority: "fill_priority",
|
|
16
|
+
minintent: "min_intent",
|
|
17
|
+
min_intent: "min_intent",
|
|
18
|
+
skipmessageanalysis: "skip_message_analysis",
|
|
19
|
+
skip_message_analysis: "skip_message_analysis",
|
|
14
20
|
};
|
|
15
21
|
export class ChatAdsClient {
|
|
16
22
|
constructor(options) {
|
|
@@ -30,7 +36,6 @@ export class ChatAdsClient {
|
|
|
30
36
|
throw new ChatAdsSDKError("Global fetch implementation not found. Pass fetchImplementation explicitly or upgrade to Node 18+");
|
|
31
37
|
}
|
|
32
38
|
this.logger = options.logger;
|
|
33
|
-
this.userAgent = options.userAgent;
|
|
34
39
|
}
|
|
35
40
|
async analyze(payload, options) {
|
|
36
41
|
const body = buildPayload(payload);
|
|
@@ -53,9 +58,6 @@ export class ChatAdsClient {
|
|
|
53
58
|
"x-api-key": this.apiKey,
|
|
54
59
|
...lowercaseHeaders(options?.headers),
|
|
55
60
|
};
|
|
56
|
-
if (this.userAgent) {
|
|
57
|
-
headers["user-agent"] = this.userAgent;
|
|
58
|
-
}
|
|
59
61
|
let attempt = 0;
|
|
60
62
|
// eslint-disable-next-line no-constant-condition
|
|
61
63
|
while (true) {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { ChatAdsClient } from "@chat-ads/chatads-sdk";
|
|
2
|
+
async function main() {
|
|
3
|
+
const apiKey = process.env.CHATADS_API_KEY;
|
|
4
|
+
if (!apiKey) {
|
|
5
|
+
throw new Error("CHATADS_API_KEY env var is required");
|
|
6
|
+
}
|
|
7
|
+
const client = new ChatAdsClient({
|
|
8
|
+
apiKey,
|
|
9
|
+
baseUrl: process.env.CHATADS_BASE_URL ?? "https://chatads--chatads-api-fastapiserver-serve.modal.run",
|
|
10
|
+
maxRetries: 1,
|
|
11
|
+
raiseOnFailure: true,
|
|
12
|
+
});
|
|
13
|
+
const response = await client.analyze({
|
|
14
|
+
message: "A great home gym always includes a yoga mat",
|
|
15
|
+
ip: "",
|
|
16
|
+
});
|
|
17
|
+
console.log(JSON.stringify(response, null, 2));
|
|
18
|
+
}
|
|
19
|
+
main().catch((error) => {
|
|
20
|
+
console.error(error);
|
|
21
|
+
process.exit(1);
|
|
22
|
+
});
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { ChatAdsClient } from "./client.js";
|
|
2
2
|
export { ChatAdsAPIError, ChatAdsSDKError } from "./errors.js";
|
|
3
3
|
export type { ChatAdsClientOptions, AnalyzeOptions, AnalyzeMessageOptions, } from "./client.js";
|
|
4
|
-
export type { ChatAdsResponseEnvelope, ChatAdsAd, ChatAdsData, ChatAdsError, ChatAdsMeta, UsageInfo, FunctionItemPayload, FunctionItemOptionalFields, } from "./models.js";
|
|
4
|
+
export type { ChatAdsResponseEnvelope, ChatAdsAd, ChatAdsData, ChatAdsError, ChatAdsMeta, UsageInfo, FunctionItemPayload, FunctionItemOptionalFields, MessageAnalysis, FillPriority, MinIntent, } from "./models.js";
|
package/dist/models.d.ts
CHANGED
|
@@ -1,19 +1,23 @@
|
|
|
1
|
+
export type MessageAnalysis = "fast" | "balanced" | "thorough";
|
|
2
|
+
export type FillPriority = "speed" | "coverage";
|
|
3
|
+
export type MinIntent = "any" | "low" | "medium" | "high";
|
|
4
|
+
/**
|
|
5
|
+
* Optional fields for ChatAds API requests.
|
|
6
|
+
* Only these 6 optional fields are supported (plus required `message`).
|
|
7
|
+
*/
|
|
1
8
|
export type FunctionItemOptionalFields = {
|
|
2
|
-
|
|
3
|
-
pageTitle?: string;
|
|
4
|
-
referrer?: string;
|
|
5
|
-
address?: string;
|
|
6
|
-
email?: string;
|
|
7
|
-
type?: string;
|
|
8
|
-
domain?: string;
|
|
9
|
-
userAgent?: string;
|
|
9
|
+
/** Client IP address for geo-detection (max 64 characters) */
|
|
10
10
|
ip?: string;
|
|
11
|
-
|
|
12
|
-
company?: string;
|
|
13
|
-
name?: string;
|
|
11
|
+
/** ISO 3166-1 alpha-2 country code for geo-targeting */
|
|
14
12
|
country?: string;
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
/** Keyword extraction method. Default: "balanced" */
|
|
14
|
+
message_analysis?: MessageAnalysis;
|
|
15
|
+
/** URL resolution fallback behavior. Default: "coverage" */
|
|
16
|
+
fill_priority?: FillPriority;
|
|
17
|
+
/** Minimum purchase intent level. Default: "low" */
|
|
18
|
+
min_intent?: MinIntent;
|
|
19
|
+
/** Skip NLP/LLM extraction and use message directly as search query. Default: false */
|
|
20
|
+
skip_message_analysis?: boolean;
|
|
17
21
|
};
|
|
18
22
|
export type FunctionItemPayload = {
|
|
19
23
|
message: string;
|
|
@@ -27,8 +31,13 @@ export interface ChatAdsAd {
|
|
|
27
31
|
}
|
|
28
32
|
export interface ChatAdsData {
|
|
29
33
|
matched: boolean;
|
|
34
|
+
filled?: boolean;
|
|
30
35
|
ad?: ChatAdsAd | null;
|
|
36
|
+
keyword?: string | null;
|
|
31
37
|
reason?: string | null;
|
|
38
|
+
intent_score?: number | null;
|
|
39
|
+
intent_level?: string | null;
|
|
40
|
+
min_intent_required?: string | null;
|
|
32
41
|
}
|
|
33
42
|
export interface ChatAdsError {
|
|
34
43
|
code: string;
|
|
@@ -37,20 +46,21 @@ export interface ChatAdsError {
|
|
|
37
46
|
}
|
|
38
47
|
export interface UsageInfo {
|
|
39
48
|
monthly_requests: number;
|
|
40
|
-
free_tier_limit
|
|
41
|
-
free_tier_remaining
|
|
49
|
+
free_tier_limit?: number | null;
|
|
50
|
+
free_tier_remaining?: number | null;
|
|
42
51
|
is_free_tier: boolean;
|
|
43
|
-
has_credit_card: boolean;
|
|
44
52
|
daily_requests?: number | null;
|
|
45
53
|
daily_limit?: number | null;
|
|
46
|
-
minute_requests?: number | null;
|
|
47
|
-
minute_limit?: number | null;
|
|
48
54
|
}
|
|
49
55
|
export interface ChatAdsMeta {
|
|
50
56
|
request_id: string;
|
|
51
57
|
user_id?: string | null;
|
|
52
58
|
country?: string | null;
|
|
53
59
|
language?: string | null;
|
|
60
|
+
extraction_method?: "llm" | "nlp" | "skip" | null;
|
|
61
|
+
message_analysis_used?: "fast" | "balanced" | "thorough" | "skip" | null;
|
|
62
|
+
fill_priority_used?: "speed" | "coverage" | "skip" | null;
|
|
63
|
+
min_intent_used?: "any" | "low" | "medium" | "high" | null;
|
|
54
64
|
processing_time_ms?: number | null;
|
|
55
65
|
usage?: UsageInfo | null;
|
|
56
66
|
[key: string]: unknown;
|
|
@@ -62,4 +72,8 @@ export interface ChatAdsResponseEnvelope {
|
|
|
62
72
|
meta: ChatAdsMeta;
|
|
63
73
|
[key: string]: unknown;
|
|
64
74
|
}
|
|
75
|
+
/**
|
|
76
|
+
* Reserved payload keys that cannot be used in extraFields.
|
|
77
|
+
* These are the 7 allowed request fields per the OpenAPI spec.
|
|
78
|
+
*/
|
|
65
79
|
export declare const RESERVED_PAYLOAD_KEYS: ReadonlySet<string>;
|
package/dist/models.js
CHANGED
|
@@ -1,18 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reserved payload keys that cannot be used in extraFields.
|
|
3
|
+
* These are the 7 allowed request fields per the OpenAPI spec.
|
|
4
|
+
*/
|
|
1
5
|
export const RESERVED_PAYLOAD_KEYS = new Set([
|
|
2
6
|
"message",
|
|
3
|
-
"pageUrl",
|
|
4
|
-
"pageTitle",
|
|
5
|
-
"referrer",
|
|
6
|
-
"address",
|
|
7
|
-
"email",
|
|
8
|
-
"type",
|
|
9
|
-
"domain",
|
|
10
|
-
"userAgent",
|
|
11
7
|
"ip",
|
|
12
|
-
"reason",
|
|
13
|
-
"company",
|
|
14
|
-
"name",
|
|
15
8
|
"country",
|
|
16
|
-
"
|
|
17
|
-
"
|
|
9
|
+
"message_analysis",
|
|
10
|
+
"fill_priority",
|
|
11
|
+
"min_intent",
|
|
12
|
+
"skip_message_analysis",
|
|
18
13
|
]);
|