@superdurable/connector-fastgpt 0.1.0 → 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.
@@ -4,7 +4,7 @@
4
4
  "package": {
5
5
  "ecosystem": "npm",
6
6
  "name": "@superdurable/connector-fastgpt",
7
- "version": "0.1.0"
7
+ "version": "0.1.1"
8
8
  },
9
9
  "factories": [
10
10
  {
package/dist/index.d.ts CHANGED
@@ -74,7 +74,7 @@ declare class FastGPTClient {
74
74
  /** Send one bounded detail request. */
75
75
  get(call: Call, id: string): Promise<{
76
76
  response?: ProviderResponse;
77
- error?: 'credential' | 'transport' | 'tooLarge';
77
+ error?: 'credential' | 'validation' | 'transport' | 'tooLarge';
78
78
  retryAfterMs: number;
79
79
  }>;
80
80
  }
package/dist/index.js CHANGED
@@ -34,8 +34,17 @@ function newGetDatasetDetailsStep(configuration) {
34
34
  }
35
35
 
36
36
  // src/client.ts
37
- import { SecretString, newQueryBranch, newQueryRetry } from "@superdurable/connectors-sdk";
38
- import { BodyTooLargeError, parseRetryAfter, sendProviderRequest, validateBaseURL } from "@superdurable/connectors-sdk/providerhttp";
37
+ import {
38
+ SecretString,
39
+ newQueryBranch,
40
+ newQueryRetry
41
+ } from "@superdurable/connectors-sdk";
42
+ import {
43
+ BodyTooLargeError,
44
+ parseRetryAfter,
45
+ sendProviderRequest,
46
+ validateBaseURL
47
+ } from "@superdurable/connectors-sdk/providerhttp";
39
48
  var FastGPTClient = class {
40
49
  /** Validate settings without contacting FastGPT. */
41
50
  constructor(config, credentials, options = {}) {
@@ -44,7 +53,8 @@ var FastGPTClient = class {
44
53
  this.baseURL = validateBaseURL(config.baseUrl || "https://cloud.fastgpt.cn");
45
54
  this.timeoutMs = config.timeoutMs || 1e4;
46
55
  this.maxResponseBytes = config.maxResponseBytes || 2097152;
47
- if (!Number.isSafeInteger(this.timeoutMs) || this.timeoutMs <= 0 || !Number.isSafeInteger(this.maxResponseBytes) || this.maxResponseBytes <= 0) throw new TypeError("FastGPT request limits must be positive integers");
56
+ if (!Number.isSafeInteger(this.timeoutMs) || this.timeoutMs <= 0 || !Number.isSafeInteger(this.maxResponseBytes) || this.maxResponseBytes <= 0)
57
+ throw new TypeError("FastGPT request limits must be positive integers");
48
58
  this.send = options.send ?? sendProviderRequest;
49
59
  }
50
60
  credentials;
@@ -65,16 +75,36 @@ var FastGPTClient = class {
65
75
  } catch {
66
76
  return { error: "credential", retryAfterMs: 0 };
67
77
  }
68
- if (!apiKey || apiKey.length > 512 || /[^\x21-\x7e]/.test(apiKey)) return { error: "credential", retryAfterMs: 0 };
69
- const request = { url: `${this.baseURL}/api/core/dataset/detail?id=${encodeURIComponent(id)}`, method: "GET", headers: { Accept: "application/json", Authorization: `Bearer ${apiKey}` }, timeoutMs: this.timeoutMs, maxResponseBytes: this.maxResponseBytes, addressPolicy: this.baseURL.startsWith("https://") ? "publicHTTPS" : "privateNetworkHTTP", signal: call.context.cancellationSignal };
78
+ if (!apiKey || apiKey.length > 512 || /[^\x21-\x7e]/.test(apiKey))
79
+ return { error: "credential", retryAfterMs: 0 };
80
+ if (typeof id !== "string" || !id || Buffer.byteLength(id) > 128 || /^\p{White_Space}|\p{White_Space}$/u.test(id) || /[/?#&]/.test(id))
81
+ return { error: "validation", retryAfterMs: 0 };
82
+ const request = {
83
+ url: `${this.baseURL}/api/core/dataset/detail?${new URLSearchParams({ id }).toString().replace(/\*/g, "%2A").replace(/%7E/g, "~")}`,
84
+ method: "GET",
85
+ headers: { Accept: "application/json", Authorization: `Bearer ${apiKey}` },
86
+ timeoutMs: this.timeoutMs,
87
+ maxResponseBytes: this.maxResponseBytes,
88
+ addressPolicy: this.baseURL.startsWith("https://") ? "publicHTTPS" : "privateNetworkHTTP",
89
+ signal: call.context.cancellationSignal
90
+ };
70
91
  let response;
71
92
  try {
72
93
  response = await this.send(request);
73
94
  } catch (error) {
74
- if (error instanceof BodyTooLargeError && error.status !== void 0) return { response: { status: error.status, headers: error.headers ?? {}, body: new Uint8Array() }, error: "tooLarge", retryAfterMs: 0 };
95
+ if (error instanceof BodyTooLargeError && error.status !== void 0)
96
+ return {
97
+ response: { status: error.status, headers: error.headers ?? {}, body: new Uint8Array() },
98
+ error: "tooLarge",
99
+ retryAfterMs: parseRetryAfter(header(error.headers ?? {}, "retry-after"))
100
+ };
75
101
  return { error: "transport", retryAfterMs: 0 };
76
102
  }
77
- return { response, error: response.body.length > this.maxResponseBytes ? "tooLarge" : void 0, retryAfterMs: parseRetryAfter(header(response.headers, "retry-after")) };
103
+ return {
104
+ response,
105
+ error: response.body.length > this.maxResponseBytes ? "tooLarge" : void 0,
106
+ retryAfterMs: parseRetryAfter(header(response.headers, "retry-after"))
107
+ };
78
108
  }
79
109
  };
80
110
  var GetDatasetDetailsOperation = class {
@@ -90,27 +120,33 @@ var GetDatasetDetailsOperation = class {
90
120
  /** Classify one provider result using the Go contract. */
91
121
  async invoke(call, input) {
92
122
  const id = input?.id;
93
- if (typeof id !== "string" || !id || Buffer.byteLength(id) > 128 || id.trim() !== id || /[/?#&]/.test(id)) return branch("defect", "VALIDATION");
94
123
  const exchange = await this.client.get(call, id);
124
+ if (exchange.error === "validation") return branch("defect", "VALIDATION");
95
125
  if (exchange.error === "credential") return branch("defect", "AUTHENTICATION");
96
126
  if (exchange.error === "transport" || !exchange.response) return retry("AVAILABILITY");
97
127
  const { status, body } = exchange.response;
98
128
  if (status === 429) return retry("RATE_LIMIT", exchange.retryAfterMs);
99
129
  if (status === 408 || status >= 500) return retry("AVAILABILITY", exchange.retryAfterMs);
100
130
  if (exchange.error === "tooLarge") return branch("invalidResponse", "RESPONSE_TOO_LARGE");
101
- if (status !== 200) return branch("providerRejected", status === 401 ? "AUTHENTICATION" : status === 403 ? "AUTHORIZATION" : "PROVIDER_REJECTION");
102
- let wire;
103
- try {
104
- wire = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(body));
105
- } catch {
106
- return branch("invalidResponse", "PROTOCOL");
107
- }
108
- if (!isObject(wire) || !Number.isInteger(wire.code)) return branch("invalidResponse", "PROTOCOL");
109
- if (wire.code === 429) return retry("RATE_LIMIT", exchange.retryAfterMs);
110
- if (wire.code >= 500) return retry("AVAILABILITY", exchange.retryAfterMs);
111
- if (wire.code !== 200) return branch("providerRejected", wire.code === 401 ? "AUTHENTICATION" : wire.code === 403 ? "AUTHORIZATION" : "PROVIDER_REJECTION");
131
+ if (status !== 200)
132
+ return branch(
133
+ "providerRejected",
134
+ status === 401 ? "AUTHENTICATION" : status === 403 ? "AUTHORIZATION" : "PROVIDER_REJECTION"
135
+ );
136
+ const wire = providerRecord(body);
137
+ if (!wire || typeof wire.code !== "bigint") return branch("invalidResponse", "PROTOCOL");
112
138
  const detail = wire.data;
113
- if (!isObject(detail) || detail._id !== id || !valid(detail.name, 512) || !valid(detail.type, 128) || !valid(detail.status, 128)) return branch("invalidResponse", "PROTOCOL");
139
+ if (detail != null && (!isObject(detail) || ["_id", "name", "type", "status"].some((k) => detail[k] != null && typeof detail[k] !== "string")))
140
+ return branch("invalidResponse", "PROTOCOL");
141
+ if (wire.code === 429n) return retry("RATE_LIMIT", exchange.retryAfterMs);
142
+ if (wire.code >= 500n) return retry("AVAILABILITY", exchange.retryAfterMs);
143
+ if (wire.code !== 200n)
144
+ return branch(
145
+ "providerRejected",
146
+ wire.code === 401n ? "AUTHENTICATION" : wire.code === 403n ? "AUTHORIZATION" : "PROVIDER_REJECTION"
147
+ );
148
+ if (!isObject(detail) || detail._id !== id || !valid(detail.name, 512) || !valid(detail.type, 128) || !valid(detail.status, 128))
149
+ return branch("invalidResponse", "PROTOCOL");
114
150
  return newQueryBranch("found", { id, name: detail.name, type: detail.type, status: detail.status });
115
151
  }
116
152
  };
@@ -125,7 +161,12 @@ function header(headers, name) {
125
161
  return Array.isArray(value) ? value[0] ?? "" : value ?? "";
126
162
  }
127
163
  function failure(kind) {
128
- return { provider: "fastgpt", operation: "getDatasetDetails", kind, message: "FastGPT knowledge base query failed" };
164
+ return {
165
+ provider: "fastgpt",
166
+ operation: "getDatasetDetails",
167
+ kind,
168
+ message: "FastGPT knowledge base query failed"
169
+ };
129
170
  }
130
171
  function branch(name, kind) {
131
172
  return newQueryBranch(name, { id: "", name: "", type: "", status: "" }, {}, failure(kind));
@@ -133,6 +174,29 @@ function branch(name, kind) {
133
174
  function retry(kind, delay = 0) {
134
175
  return newQueryRetry(failure(kind), delay);
135
176
  }
177
+ function providerRecord(body) {
178
+ try {
179
+ const wire = JSON.parse(
180
+ new TextDecoder().decode(body),
181
+ (_key, value, context) => {
182
+ if (typeof value === "number") {
183
+ const token = context?.source;
184
+ if (!token || token.length > 20 || !/^-?(?:0|[1-9][0-9]*)$/.test(token)) return value;
185
+ const integer = BigInt(token);
186
+ return integer >= -(1n << 63n) && integer < 1n << 63n ? integer : value;
187
+ }
188
+ if (isObject(value))
189
+ return Object.fromEntries(
190
+ Object.entries(value).map(([key, field]) => [key.toLowerCase().replace(/ſ/g, "s"), field])
191
+ );
192
+ return value;
193
+ }
194
+ );
195
+ return isObject(wire) ? wire : void 0;
196
+ } catch {
197
+ return void 0;
198
+ }
199
+ }
136
200
 
137
201
  // src/project.ts
138
202
  function newProjectConnection2(project, connectionName) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@superdurable/connector-fastgpt",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "license": "MIT",
5
5
  "description": "fastgpt connector for Dex TypeScript applications",
6
6
  "repository": {