@graphysdk/agents-sdk 0.0.0-beta-20260219191134
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/dist/index.d.ts +209 -0
- package/dist/index.js +1 -0
- package/dist/index.mjs +1 -0
- package/package.json +52 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import * as _graphysdk_core_node from '@graphysdk/core/node';
|
|
2
|
+
import { GraphConfig } from '@graphysdk/core/node';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
|
|
5
|
+
interface ProgressEvent {
|
|
6
|
+
type: 'progress';
|
|
7
|
+
percentage: number;
|
|
8
|
+
message?: string;
|
|
9
|
+
metadata?: Record<string, JsonValue>;
|
|
10
|
+
}
|
|
11
|
+
interface CompleteEvent<T> {
|
|
12
|
+
type: 'complete';
|
|
13
|
+
data: T;
|
|
14
|
+
}
|
|
15
|
+
interface ErrorEvent {
|
|
16
|
+
type: 'error';
|
|
17
|
+
error: string;
|
|
18
|
+
code?: string;
|
|
19
|
+
retryable?: boolean;
|
|
20
|
+
}
|
|
21
|
+
type SSEEvent<T> = ProgressEvent | CompleteEvent<T> | ErrorEvent;
|
|
22
|
+
interface Logger {
|
|
23
|
+
log: (...args: Array<JsonValue | unknown>) => void;
|
|
24
|
+
warn: (...args: Array<JsonValue | unknown>) => void;
|
|
25
|
+
error: (...args: Array<JsonValue | unknown>) => void;
|
|
26
|
+
debug: (...args: Array<JsonValue | unknown>) => void;
|
|
27
|
+
}
|
|
28
|
+
interface RetryConfig {
|
|
29
|
+
attempts: number;
|
|
30
|
+
delay: number;
|
|
31
|
+
backoff: number;
|
|
32
|
+
}
|
|
33
|
+
interface ClientConfig {
|
|
34
|
+
apiKey: string;
|
|
35
|
+
baseUrl: string;
|
|
36
|
+
timeout?: number;
|
|
37
|
+
logger?: Logger;
|
|
38
|
+
retryConfig?: RetryConfig;
|
|
39
|
+
}
|
|
40
|
+
type JsonValue = string | number | boolean | null | JsonObject | JsonArray;
|
|
41
|
+
interface JsonObject {
|
|
42
|
+
[key: string]: JsonValue;
|
|
43
|
+
}
|
|
44
|
+
type JsonArray = JsonValue[];
|
|
45
|
+
|
|
46
|
+
type ProgressHandler = (progress: ProgressEvent) => void;
|
|
47
|
+
|
|
48
|
+
declare const MetadataSchema: z.ZodObject<{
|
|
49
|
+
callId: z.ZodString;
|
|
50
|
+
locale: z.ZodOptional<z.ZodEnum<{
|
|
51
|
+
EN_GB: "EN_GB";
|
|
52
|
+
EN_US: "EN_US";
|
|
53
|
+
}>>;
|
|
54
|
+
}, z.core.$strip>;
|
|
55
|
+
type Metadata = z.infer<typeof MetadataSchema>;
|
|
56
|
+
declare const AiChartTypeEnum: z.ZodEnum<{
|
|
57
|
+
line: "line";
|
|
58
|
+
bar: "bar";
|
|
59
|
+
groupedBar: "groupedBar";
|
|
60
|
+
stackedBar: "stackedBar";
|
|
61
|
+
"100StackedBar": "100StackedBar";
|
|
62
|
+
column: "column";
|
|
63
|
+
groupedColumn: "groupedColumn";
|
|
64
|
+
stackedColumn: "stackedColumn";
|
|
65
|
+
"100StackedColumn": "100StackedColumn";
|
|
66
|
+
combo: "combo";
|
|
67
|
+
pie: "pie";
|
|
68
|
+
donut: "donut";
|
|
69
|
+
funnel: "funnel";
|
|
70
|
+
heatmap: "heatmap";
|
|
71
|
+
scatter: "scatter";
|
|
72
|
+
waterfall: "waterfall";
|
|
73
|
+
table: "table";
|
|
74
|
+
}>;
|
|
75
|
+
type AiChartType = z.infer<typeof AiChartTypeEnum>;
|
|
76
|
+
declare const SuggestionSchema: z.ZodObject<{
|
|
77
|
+
dataPrepPrompt: z.ZodString;
|
|
78
|
+
chartType: z.ZodEnum<{
|
|
79
|
+
line: "line";
|
|
80
|
+
bar: "bar";
|
|
81
|
+
groupedBar: "groupedBar";
|
|
82
|
+
stackedBar: "stackedBar";
|
|
83
|
+
"100StackedBar": "100StackedBar";
|
|
84
|
+
column: "column";
|
|
85
|
+
groupedColumn: "groupedColumn";
|
|
86
|
+
stackedColumn: "stackedColumn";
|
|
87
|
+
"100StackedColumn": "100StackedColumn";
|
|
88
|
+
combo: "combo";
|
|
89
|
+
pie: "pie";
|
|
90
|
+
donut: "donut";
|
|
91
|
+
funnel: "funnel";
|
|
92
|
+
heatmap: "heatmap";
|
|
93
|
+
scatter: "scatter";
|
|
94
|
+
waterfall: "waterfall";
|
|
95
|
+
table: "table";
|
|
96
|
+
}>;
|
|
97
|
+
summary: z.ZodString;
|
|
98
|
+
}, z.core.$strip>;
|
|
99
|
+
type Suggestion = z.infer<typeof SuggestionSchema>;
|
|
100
|
+
type GenerateGraphNarrativeParams = {
|
|
101
|
+
config: GraphConfig;
|
|
102
|
+
userPrompt: string;
|
|
103
|
+
metadata?: Metadata;
|
|
104
|
+
};
|
|
105
|
+
declare const GenerateGraphNarrativeResponseSchema: z.ZodObject<{
|
|
106
|
+
title: z.ZodString;
|
|
107
|
+
subtitle: z.ZodString;
|
|
108
|
+
caption: z.ZodNullable<z.ZodString>;
|
|
109
|
+
}, z.core.$strip>;
|
|
110
|
+
type GenerateGraphNarrativeResponse = z.infer<typeof GenerateGraphNarrativeResponseSchema>;
|
|
111
|
+
type GenerateGraphSuggestionsParams = {
|
|
112
|
+
data: string;
|
|
113
|
+
metadata?: Metadata;
|
|
114
|
+
};
|
|
115
|
+
declare const GenerateGraphSuggestionsResponseSchema: z.ZodObject<{
|
|
116
|
+
config: z.ZodType<GraphConfig, unknown, z.core.$ZodTypeInternals<GraphConfig, unknown>>;
|
|
117
|
+
suggestions: z.ZodArray<z.ZodObject<{
|
|
118
|
+
dataPrepPrompt: z.ZodString;
|
|
119
|
+
chartType: z.ZodEnum<{
|
|
120
|
+
line: "line";
|
|
121
|
+
bar: "bar";
|
|
122
|
+
groupedBar: "groupedBar";
|
|
123
|
+
stackedBar: "stackedBar";
|
|
124
|
+
"100StackedBar": "100StackedBar";
|
|
125
|
+
column: "column";
|
|
126
|
+
groupedColumn: "groupedColumn";
|
|
127
|
+
stackedColumn: "stackedColumn";
|
|
128
|
+
"100StackedColumn": "100StackedColumn";
|
|
129
|
+
combo: "combo";
|
|
130
|
+
pie: "pie";
|
|
131
|
+
donut: "donut";
|
|
132
|
+
funnel: "funnel";
|
|
133
|
+
heatmap: "heatmap";
|
|
134
|
+
scatter: "scatter";
|
|
135
|
+
waterfall: "waterfall";
|
|
136
|
+
table: "table";
|
|
137
|
+
}>;
|
|
138
|
+
summary: z.ZodString;
|
|
139
|
+
}, z.core.$strip>>;
|
|
140
|
+
description: z.ZodNullable<z.ZodString>;
|
|
141
|
+
}, z.core.$strip>;
|
|
142
|
+
type GenerateGraphSuggestionsResponse = z.infer<typeof GenerateGraphSuggestionsResponseSchema>;
|
|
143
|
+
type GenerateGraphParams = {
|
|
144
|
+
config: GraphConfig;
|
|
145
|
+
userPrompt: string;
|
|
146
|
+
metadata?: Metadata;
|
|
147
|
+
};
|
|
148
|
+
declare const GenerateGraphResponseSchema: z.ZodObject<{
|
|
149
|
+
config: z.ZodType<GraphConfig, unknown, z.core.$ZodTypeInternals<GraphConfig, unknown>>;
|
|
150
|
+
response: z.ZodObject<{
|
|
151
|
+
message: z.ZodString;
|
|
152
|
+
steps: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
153
|
+
}, z.core.$strip>;
|
|
154
|
+
}, z.core.$strip>;
|
|
155
|
+
type GenerateGraphResponse = z.infer<typeof GenerateGraphResponseSchema>;
|
|
156
|
+
|
|
157
|
+
declare class GraphyAiSdk {
|
|
158
|
+
private client;
|
|
159
|
+
constructor(config: ClientConfig);
|
|
160
|
+
generateGraph(data: GenerateGraphParams, onProgress?: ProgressHandler, signal?: AbortSignal): Promise<{
|
|
161
|
+
config: _graphysdk_core_node.GraphConfig;
|
|
162
|
+
response: {
|
|
163
|
+
message: string;
|
|
164
|
+
steps?: string[] | undefined;
|
|
165
|
+
};
|
|
166
|
+
}>;
|
|
167
|
+
generateGraphStream(data: GenerateGraphParams, signal?: AbortSignal): Promise<AsyncIterableIterator<SSEEvent<{
|
|
168
|
+
config: _graphysdk_core_node.GraphConfig;
|
|
169
|
+
response: {
|
|
170
|
+
message: string;
|
|
171
|
+
steps?: string[] | undefined;
|
|
172
|
+
};
|
|
173
|
+
}>>>;
|
|
174
|
+
generateNarrative(data: GenerateGraphNarrativeParams, onProgress?: ProgressHandler, signal?: AbortSignal): Promise<{
|
|
175
|
+
title: string;
|
|
176
|
+
subtitle: string;
|
|
177
|
+
caption: string | null;
|
|
178
|
+
}>;
|
|
179
|
+
generateNarrativeStream(data: GenerateGraphNarrativeParams, signal?: AbortSignal): Promise<AsyncIterableIterator<SSEEvent<{
|
|
180
|
+
title: string;
|
|
181
|
+
subtitle: string;
|
|
182
|
+
caption: string | null;
|
|
183
|
+
}>>>;
|
|
184
|
+
generateSuggestions(data: GenerateGraphSuggestionsParams, onProgress?: ProgressHandler, signal?: AbortSignal): Promise<{
|
|
185
|
+
config: _graphysdk_core_node.GraphConfig;
|
|
186
|
+
suggestions: {
|
|
187
|
+
dataPrepPrompt: string;
|
|
188
|
+
chartType: "line" | "bar" | "groupedBar" | "stackedBar" | "100StackedBar" | "column" | "groupedColumn" | "stackedColumn" | "100StackedColumn" | "combo" | "pie" | "donut" | "funnel" | "heatmap" | "scatter" | "waterfall" | "table";
|
|
189
|
+
summary: string;
|
|
190
|
+
}[];
|
|
191
|
+
description: string | null;
|
|
192
|
+
}>;
|
|
193
|
+
generateSuggestionsStream(data: GenerateGraphSuggestionsParams, signal?: AbortSignal): Promise<AsyncIterableIterator<SSEEvent<{
|
|
194
|
+
config: _graphysdk_core_node.GraphConfig;
|
|
195
|
+
suggestions: {
|
|
196
|
+
dataPrepPrompt: string;
|
|
197
|
+
chartType: "line" | "bar" | "groupedBar" | "stackedBar" | "100StackedBar" | "column" | "groupedColumn" | "stackedColumn" | "100StackedColumn" | "combo" | "pie" | "donut" | "funnel" | "heatmap" | "scatter" | "waterfall" | "table";
|
|
198
|
+
summary: string;
|
|
199
|
+
}[];
|
|
200
|
+
description: string | null;
|
|
201
|
+
}>>>;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
declare class GraphyApiError extends Error {
|
|
205
|
+
constructor(message: string);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export { AiChartTypeEnum, GenerateGraphNarrativeResponseSchema, GenerateGraphResponseSchema, GenerateGraphSuggestionsResponseSchema, GraphyAiSdk, GraphyApiError, SuggestionSchema };
|
|
209
|
+
export type { AiChartType, ClientConfig, CompleteEvent, ErrorEvent, GenerateGraphNarrativeParams, GenerateGraphNarrativeResponse, GenerateGraphParams, GenerateGraphResponse, GenerateGraphSuggestionsParams, GenerateGraphSuggestionsResponse, Logger, ProgressEvent, ProgressHandler, RetryConfig, SSEEvent, Suggestion };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var t=require("tslib"),e=require("zod");class r extends Error{constructor(t){super(t),this.name="GraphyApiError"}}class o{constructor(t){if(this.tag="[GRAPHY]",!t.apiKey||0===t.apiKey.trim().length)throw new Error("apiKey is required and must not be empty");const e=new URL(t.baseUrl);if(!["http:","https:"].includes(e.protocol))throw new Error(`Invalid baseUrl protocol: ${e.protocol}. Only http(s) is allowed.`);this.apiKey=t.apiKey,this.baseUrl=e.origin+e.pathname.replace(/\/$/,""),this.timeout=t.timeout||6e4,this.retryConfig=t.retryConfig||{attempts:3,delay:1e3,backoff:2},this.logger=t.logger||{log:console.log,warn:console.warn,error:console.error,debug:console.debug}}static isProgressEvent(t){return"progress"===t.type}static isCompleteEvent(t){return"complete"===t.type}static isErrorEvent(t){return"error"===t.type}stream(e,r,o){return t.__awaiter(this,void 0,void 0,function*(){const t=yield this.makeRequest(e,r,o);return this.parseSSE(t)})}fetch(e,i,s,n){return t.__awaiter(this,void 0,void 0,function*(){var a,c,l,h;const g=Date.now();this.logger.debug(`${this.tag} Fetch: ${e}`);try{const y=yield this.makeRequest(e,i,n),f=y.headers.get("content-type");if(null==f?void 0:f.includes("text/event-stream")){try{for(var u,d=!0,p=t.__asyncValues(this.parseSSE(y));!(a=(u=yield p.next()).done);d=!0){h=u.value,d=!1;const t=h;if(o.isProgressEvent(t)&&s&&s(t),o.isCompleteEvent(t))return t.data;if(o.isErrorEvent(t)){const o=Date.now()-g;throw this.logger.error(`${this.tag} Server error: ${e} ${o}ms`,t.error),new r(t.error)}}}catch(t){c={error:t}}finally{try{d||a||!(l=p.return)||(yield l.call(p))}finally{if(c)throw c.error}}const i=Date.now()-g;throw this.logger.error(`${this.tag} Stream incomplete: ${e} ${i}ms`),new r("Stream ended without completion")}return yield y.json()}catch(t){const o=Date.now()-g;if(this.logger.error(`${this.tag} Fetch failed: ${e} ${o}ms`,t),t instanceof r)throw t;throw t}finally{const t=Date.now()-g;this.logger.debug(`${this.tag} Fetch completed: ${e} ${t}ms`)}})}ping(){return t.__awaiter(this,void 0,void 0,function*(){const t=Date.now();try{return yield this.makeRequest("/health",void 0,void 0,this.retryConfig.attempts,"GET"),{ok:!0,latency:Date.now()-t}}catch(e){return{ok:!1,latency:Date.now()-t}}})}sleep(e){return t.__awaiter(this,void 0,void 0,function*(){return new Promise(t=>setTimeout(t,e))})}parseSSE(e){return t.__asyncGenerator(this,arguments,function*(){if(!e.body)throw new r("Response body is null");const o=e.body.getReader(),i=new TextDecoder;let s="",n="",a="";try{for(;;){const{done:e,value:r}=yield t.__await(o.read());if(e)break;s+=i.decode(r,{stream:!0});const c=s.split(/\r?\n/);s=c.pop()||"";for(const e of c)if(e.startsWith("event: "))n=e.slice(7).trim();else if(e.startsWith("data: "))a+=(a?"\n":"")+e.slice(6);else if(""===e.trim()&&a){try{const e=JSON.parse(a);let r;if("progress"===n){const t=e;r=Object.assign({type:"progress"},t)}else if("complete"===n)r={type:"complete",data:e};else{if("error"!==n){n="",a="";continue}{const t=e;r=Object.assign({type:"error"},t)}}if(yield yield t.__await(r),"complete"===n||"error"===n)return yield t.__await(void 0)}catch(t){this.logger.warn("Invalid SSE data format",a)}n="",a=""}}}finally{o.releaseLock()}})}makeRequest(e,o,i){return t.__awaiter(this,arguments,void 0,function*(t,e,o,i=1,s="POST"){if(!t.startsWith("/"))throw new r("Invalid endpoint: must start with /");const n=new URL(t,this.baseUrl);if(n.origin!==new URL(this.baseUrl).origin)throw new r("Endpoint resolved to unexpected origin");this.logger.debug(`${this.tag} Request ${i}/${this.retryConfig.attempts}: ${n}`);const a=new AbortController,c=setTimeout(()=>a.abort(),this.timeout),l=()=>a.abort();o&&o.addEventListener("abort",l);try{const c=yield fetch(n,Object.assign({method:s,headers:{"Content-Type":"application/json",Accept:"application/json, text/event-stream","Cache-Control":"no-cache",Authorization:`Bearer ${this.apiKey}`,"User-Agent":"Graphy-SDK/1.0"},signal:a.signal},"POST"===s&&{body:JSON.stringify(e)}));if(!c.ok){const n=c.status>=500||429===c.status;let a=`HTTP ${c.status}`;try{const t=yield c.json();if("object"==typeof t&&null!==t){const e=t;"string"==typeof e.message?a=e.message:"string"==typeof e.error&&(a=e.error)}}catch(t){}const l=new r(a);if(n&&i<this.retryConfig.attempts&&!(null==o?void 0:o.aborted)){const r=this.retryConfig.delay*Math.pow(this.retryConfig.backoff,i-1);return this.logger.warn(`${this.tag} Retrying ${t} in ${r}ms (attempt ${i+1})`),yield this.sleep(r),this.makeRequest(t,e,o,i+1,s)}throw this.logger.error(`${this.tag} Request failed: ${t} (${c.status}) - ${a}`),l}return this.logger.debug(`${this.tag} Connected: ${t}`),c}catch(n){if(n instanceof r)throw n;if(n instanceof Error&&"AbortError"===n.name)throw this.logger.debug(`${this.tag} Aborted: ${t}`),n;if(i<this.retryConfig.attempts&&!(null==o?void 0:o.aborted)){const r=this.retryConfig.delay*Math.pow(this.retryConfig.backoff,i-1);return this.logger.warn(`${this.tag} Network error, retrying ${t} in ${r}ms`),yield this.sleep(r),this.makeRequest(t,e,o,i+1,s)}throw this.logger.error(`${this.tag} Network error: ${t}`,n),new r("Network error")}finally{clearTimeout(c),o&&o.removeEventListener("abort",l)}})}}const i=e.z.enum(["EN_GB","EN_US"]),s=e.z.custom(t=>null!=t&&"object"==typeof t);e.z.object({callId:e.z.string(),locale:i.optional()});const n=e.z.enum(["line","bar","groupedBar","stackedBar","100StackedBar","column","groupedColumn","stackedColumn","100StackedColumn","combo","pie","donut","funnel","heatmap","scatter","waterfall","table"]),a=e.z.object({dataPrepPrompt:e.z.string(),chartType:n,summary:e.z.string()}),c=e.z.object({title:e.z.string(),subtitle:e.z.string(),caption:e.z.string().nullable()}),l=e.z.object({config:s,suggestions:e.z.array(a),description:e.z.string().nullable()}),h=e.z.object({config:s,response:e.z.object({message:e.z.string(),steps:e.z.array(e.z.string()).optional()})}),g="/api/v0";exports.AiChartTypeEnum=n,exports.GenerateGraphNarrativeResponseSchema=c,exports.GenerateGraphResponseSchema=h,exports.GenerateGraphSuggestionsResponseSchema=l,exports.GraphyAiSdk=class{constructor(t){this.client=new o(t)}generateGraph(e,r,o){return t.__awaiter(this,void 0,void 0,function*(){const t=yield this.client.fetch(`${g}/generate`,e,r,o);return h.parse(t)})}generateGraphStream(e,r){return t.__awaiter(this,void 0,void 0,function*(){return this.client.stream(`${g}/generate`,e,r)})}generateNarrative(e,r,o){return t.__awaiter(this,void 0,void 0,function*(){const t=yield this.client.fetch(`${g}/narrative`,e,r,o);return c.parse(t)})}generateNarrativeStream(e,r){return t.__awaiter(this,void 0,void 0,function*(){return this.client.stream(`${g}/narrative`,e,r)})}generateSuggestions(e,r,o){return t.__awaiter(this,void 0,void 0,function*(){const t=yield this.client.fetch(`${g}/suggestions`,e,r,o);return l.parse(t)})}generateSuggestionsStream(e,r){return t.__awaiter(this,void 0,void 0,function*(){return this.client.stream(`${g}/suggestions`,e,r)})}},exports.GraphyApiError=r,exports.SuggestionSchema=a;
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{__awaiter as t,__asyncValues as e,__asyncGenerator as r,__await as o}from"tslib";import{z as i}from"zod";class s extends Error{constructor(t){super(t),this.name="GraphyApiError"}}class n{constructor(t){if(this.tag="[GRAPHY]",!t.apiKey||0===t.apiKey.trim().length)throw new Error("apiKey is required and must not be empty");const e=new URL(t.baseUrl);if(!["http:","https:"].includes(e.protocol))throw new Error(`Invalid baseUrl protocol: ${e.protocol}. Only http(s) is allowed.`);this.apiKey=t.apiKey,this.baseUrl=e.origin+e.pathname.replace(/\/$/,""),this.timeout=t.timeout||6e4,this.retryConfig=t.retryConfig||{attempts:3,delay:1e3,backoff:2},this.logger=t.logger||{log:console.log,warn:console.warn,error:console.error,debug:console.debug}}static isProgressEvent(t){return"progress"===t.type}static isCompleteEvent(t){return"complete"===t.type}static isErrorEvent(t){return"error"===t.type}stream(e,r,o){return t(this,void 0,void 0,function*(){const t=yield this.makeRequest(e,r,o);return this.parseSSE(t)})}fetch(r,o,i,a){return t(this,void 0,void 0,function*(){var t,l,c,h;const g=Date.now();this.logger.debug(`${this.tag} Fetch: ${r}`);try{const f=yield this.makeRequest(r,o,a),y=f.headers.get("content-type");if(null==y?void 0:y.includes("text/event-stream")){try{for(var u,d=!0,p=e(this.parseSSE(f));!(t=(u=yield p.next()).done);d=!0){h=u.value,d=!1;const t=h;if(n.isProgressEvent(t)&&i&&i(t),n.isCompleteEvent(t))return t.data;if(n.isErrorEvent(t)){const e=Date.now()-g;throw this.logger.error(`${this.tag} Server error: ${r} ${e}ms`,t.error),new s(t.error)}}}catch(t){l={error:t}}finally{try{d||t||!(c=p.return)||(yield c.call(p))}finally{if(l)throw l.error}}const o=Date.now()-g;throw this.logger.error(`${this.tag} Stream incomplete: ${r} ${o}ms`),new s("Stream ended without completion")}return yield f.json()}catch(t){const e=Date.now()-g;if(this.logger.error(`${this.tag} Fetch failed: ${r} ${e}ms`,t),t instanceof s)throw t;throw t}finally{const t=Date.now()-g;this.logger.debug(`${this.tag} Fetch completed: ${r} ${t}ms`)}})}ping(){return t(this,void 0,void 0,function*(){const t=Date.now();try{return yield this.makeRequest("/health",void 0,void 0,this.retryConfig.attempts,"GET"),{ok:!0,latency:Date.now()-t}}catch(e){return{ok:!1,latency:Date.now()-t}}})}sleep(e){return t(this,void 0,void 0,function*(){return new Promise(t=>setTimeout(t,e))})}parseSSE(t){return r(this,arguments,function*(){if(!t.body)throw new s("Response body is null");const e=t.body.getReader(),r=new TextDecoder;let i="",n="",a="";try{for(;;){const{done:t,value:s}=yield o(e.read());if(t)break;i+=r.decode(s,{stream:!0});const l=i.split(/\r?\n/);i=l.pop()||"";for(const t of l)if(t.startsWith("event: "))n=t.slice(7).trim();else if(t.startsWith("data: "))a+=(a?"\n":"")+t.slice(6);else if(""===t.trim()&&a){try{const t=JSON.parse(a);let e;if("progress"===n){const r=t;e=Object.assign({type:"progress"},r)}else if("complete"===n)e={type:"complete",data:t};else{if("error"!==n){n="",a="";continue}{const r=t;e=Object.assign({type:"error"},r)}}if(yield yield o(e),"complete"===n||"error"===n)return yield o(void 0)}catch(t){this.logger.warn("Invalid SSE data format",a)}n="",a=""}}}finally{e.releaseLock()}})}makeRequest(e,r,o){return t(this,arguments,void 0,function*(t,e,r,o=1,i="POST"){if(!t.startsWith("/"))throw new s("Invalid endpoint: must start with /");const n=new URL(t,this.baseUrl);if(n.origin!==new URL(this.baseUrl).origin)throw new s("Endpoint resolved to unexpected origin");this.logger.debug(`${this.tag} Request ${o}/${this.retryConfig.attempts}: ${n}`);const a=new AbortController,l=setTimeout(()=>a.abort(),this.timeout),c=()=>a.abort();r&&r.addEventListener("abort",c);try{const l=yield fetch(n,Object.assign({method:i,headers:{"Content-Type":"application/json",Accept:"application/json, text/event-stream","Cache-Control":"no-cache",Authorization:`Bearer ${this.apiKey}`,"User-Agent":"Graphy-SDK/1.0"},signal:a.signal},"POST"===i&&{body:JSON.stringify(e)}));if(!l.ok){const n=l.status>=500||429===l.status;let a=`HTTP ${l.status}`;try{const t=yield l.json();if("object"==typeof t&&null!==t){const e=t;"string"==typeof e.message?a=e.message:"string"==typeof e.error&&(a=e.error)}}catch(t){}const c=new s(a);if(n&&o<this.retryConfig.attempts&&!(null==r?void 0:r.aborted)){const s=this.retryConfig.delay*Math.pow(this.retryConfig.backoff,o-1);return this.logger.warn(`${this.tag} Retrying ${t} in ${s}ms (attempt ${o+1})`),yield this.sleep(s),this.makeRequest(t,e,r,o+1,i)}throw this.logger.error(`${this.tag} Request failed: ${t} (${l.status}) - ${a}`),c}return this.logger.debug(`${this.tag} Connected: ${t}`),l}catch(n){if(n instanceof s)throw n;if(n instanceof Error&&"AbortError"===n.name)throw this.logger.debug(`${this.tag} Aborted: ${t}`),n;if(o<this.retryConfig.attempts&&!(null==r?void 0:r.aborted)){const s=this.retryConfig.delay*Math.pow(this.retryConfig.backoff,o-1);return this.logger.warn(`${this.tag} Network error, retrying ${t} in ${s}ms`),yield this.sleep(s),this.makeRequest(t,e,r,o+1,i)}throw this.logger.error(`${this.tag} Network error: ${t}`,n),new s("Network error")}finally{clearTimeout(l),r&&r.removeEventListener("abort",c)}})}}const a=i.enum(["EN_GB","EN_US"]),l=i.custom(t=>null!=t&&"object"==typeof t);i.object({callId:i.string(),locale:a.optional()});const c=i.enum(["line","bar","groupedBar","stackedBar","100StackedBar","column","groupedColumn","stackedColumn","100StackedColumn","combo","pie","donut","funnel","heatmap","scatter","waterfall","table"]),h=i.object({dataPrepPrompt:i.string(),chartType:c,summary:i.string()}),g=i.object({title:i.string(),subtitle:i.string(),caption:i.string().nullable()}),u=i.object({config:l,suggestions:i.array(h),description:i.string().nullable()}),d=i.object({config:l,response:i.object({message:i.string(),steps:i.array(i.string()).optional()})}),p="/api/v0";class f{constructor(t){this.client=new n(t)}generateGraph(e,r,o){return t(this,void 0,void 0,function*(){const t=yield this.client.fetch(`${p}/generate`,e,r,o);return d.parse(t)})}generateGraphStream(e,r){return t(this,void 0,void 0,function*(){return this.client.stream(`${p}/generate`,e,r)})}generateNarrative(e,r,o){return t(this,void 0,void 0,function*(){const t=yield this.client.fetch(`${p}/narrative`,e,r,o);return g.parse(t)})}generateNarrativeStream(e,r){return t(this,void 0,void 0,function*(){return this.client.stream(`${p}/narrative`,e,r)})}generateSuggestions(e,r,o){return t(this,void 0,void 0,function*(){const t=yield this.client.fetch(`${p}/suggestions`,e,r,o);return u.parse(t)})}generateSuggestionsStream(e,r){return t(this,void 0,void 0,function*(){return this.client.stream(`${p}/suggestions`,e,r)})}}export{c as AiChartTypeEnum,g as GenerateGraphNarrativeResponseSchema,d as GenerateGraphResponseSchema,u as GenerateGraphSuggestionsResponseSchema,f as GraphyAiSdk,s as GraphyApiError,h as SuggestionSchema};
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@graphysdk/agents-sdk",
|
|
3
|
+
"author": "Graphy",
|
|
4
|
+
"version": "0.0.0-beta-20260219191134",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"files": [
|
|
8
|
+
"dist",
|
|
9
|
+
"README.md"
|
|
10
|
+
],
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"local": "./src/index.ts",
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.mjs",
|
|
17
|
+
"require": "./dist/index.js"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"peerDependencies": {
|
|
21
|
+
"@graphysdk/core": "0.0.0-beta-20260219191134"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"zod": "^4.3.6"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@rollup/plugin-commonjs": "^28.0.6",
|
|
28
|
+
"@rollup/plugin-node-resolve": "^16.0.1",
|
|
29
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
30
|
+
"@rollup/plugin-typescript": "^12.1.4",
|
|
31
|
+
"lodash-es": "^4.17.21",
|
|
32
|
+
"rollup": "^4.50.0",
|
|
33
|
+
"rollup-plugin-clear": "^2.0.7",
|
|
34
|
+
"rollup-plugin-dts": "^6.2.3",
|
|
35
|
+
"rollup-plugin-node-externals": "^8.1.1",
|
|
36
|
+
"tslib": "^2.8.1",
|
|
37
|
+
"vitest": "^3.0.8",
|
|
38
|
+
"@graphytools/eslint-config": "0.0.1",
|
|
39
|
+
"@graphytools/typescript-config": "0.0.1"
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "rollup -c --bundleConfigAsCjs",
|
|
43
|
+
"build:dev": "rollup -c --bundleConfigAsCjs --watch",
|
|
44
|
+
"test": "TZ=utc vitest run",
|
|
45
|
+
"test:watch": "TZ=utc vitest",
|
|
46
|
+
"lint": "eslint .",
|
|
47
|
+
"format": "prettier --write .",
|
|
48
|
+
"format:check": "prettier --check .",
|
|
49
|
+
"typecheck": "tsc --noEmit",
|
|
50
|
+
"yalc:push": "yalc push --private"
|
|
51
|
+
}
|
|
52
|
+
}
|