agentlink-sdk 1.0.8 → 1.0.9
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.mts +36 -1
- package/dist/index.d.ts +36 -1
- package/dist/index.js +65 -0
- package/dist/index.mjs +65 -0
- package/package.json +1 -1
- package/scripts/test-llm-schema.ts +2 -2
package/dist/index.d.mts
CHANGED
|
@@ -120,6 +120,17 @@ declare function verifyWhitelist(serverUrl: string, origin?: string): Promise<bo
|
|
|
120
120
|
*/
|
|
121
121
|
declare function fetchWhitelistInfo(serverUrl: string, includeAll?: boolean): Promise<any>;
|
|
122
122
|
|
|
123
|
+
/**
|
|
124
|
+
* GET /api/capabilities 返回的能力描述
|
|
125
|
+
*/
|
|
126
|
+
interface CapabilitiesResponse {
|
|
127
|
+
structureData: boolean;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* 请求 server 的 GET /api/capabilities,返回当前实例是否提供 structureData 等能力
|
|
131
|
+
*/
|
|
132
|
+
declare function fetchCapabilities(serverUrl: string): Promise<CapabilitiesResponse>;
|
|
133
|
+
|
|
123
134
|
/**
|
|
124
135
|
* 自动遍历 rawData 推断出体积受控的 schema,供 LLM 使用
|
|
125
136
|
*/
|
|
@@ -148,6 +159,30 @@ interface InferSchemaOptions {
|
|
|
148
159
|
*/
|
|
149
160
|
declare function inferSchema(rawData: any, options?: InferSchemaOptions): InferredSchema;
|
|
150
161
|
|
|
162
|
+
/**
|
|
163
|
+
* POST /api/structureData 返回的结构化结果(与 server AIProcessedData 一致)
|
|
164
|
+
*/
|
|
165
|
+
interface StructureDataResult {
|
|
166
|
+
source: string;
|
|
167
|
+
category: string;
|
|
168
|
+
content: string;
|
|
169
|
+
tags: string[];
|
|
170
|
+
images?: string[];
|
|
171
|
+
metadata?: Record<string, unknown>;
|
|
172
|
+
}
|
|
173
|
+
interface FetchStructureDataOptions {
|
|
174
|
+
systemPrompt?: string;
|
|
175
|
+
temperature?: number;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* 从 SSE 响应文本中解析最后一条 result 或 error
|
|
179
|
+
*/
|
|
180
|
+
declare function parseSSEResult(sseText: string): StructureDataResult;
|
|
181
|
+
/**
|
|
182
|
+
* 请求 server 的 POST /api/structureData,传入 schema,返回结构化结果
|
|
183
|
+
*/
|
|
184
|
+
declare function fetchStructureData(serverUrl: string, schema: InferredSchema, options?: FetchStructureDataOptions): Promise<StructureDataResult>;
|
|
185
|
+
|
|
151
186
|
/**
|
|
152
187
|
* Base64 数据隔离和替换工具
|
|
153
188
|
* 用于在 LLM 处理前替换 base64 图片数据,减少输入大小
|
|
@@ -175,4 +210,4 @@ declare function restoreBase64(data: any, replacements: Map<string, string>): an
|
|
|
175
210
|
*/
|
|
176
211
|
declare function extractImages(data: any): string[];
|
|
177
212
|
|
|
178
|
-
export { AgentLinkClient, type AgentLinkClientOptions, DEFAULT_SERVER_URL, type InferSchemaOptions, type InferredSchema, type SenderInfo, type URLData, type WhitelistInfo, type WhitelistResponse, base64ToUint8Array, compress, decodeDataFromUrl, decompress, encodeDataToUrl, extractAndReplaceBase64, extractImages, fetchWhitelistInfo, inferSchema, restoreBase64, uint8ArrayToBase64, verifyWhitelist };
|
|
213
|
+
export { AgentLinkClient, type AgentLinkClientOptions, type CapabilitiesResponse, DEFAULT_SERVER_URL, type FetchStructureDataOptions, type InferSchemaOptions, type InferredSchema, type SenderInfo, type StructureDataResult, type URLData, type WhitelistInfo, type WhitelistResponse, base64ToUint8Array, compress, decodeDataFromUrl, decompress, encodeDataToUrl, extractAndReplaceBase64, extractImages, fetchCapabilities, fetchStructureData, fetchWhitelistInfo, inferSchema, parseSSEResult, restoreBase64, uint8ArrayToBase64, verifyWhitelist };
|
package/dist/index.d.ts
CHANGED
|
@@ -120,6 +120,17 @@ declare function verifyWhitelist(serverUrl: string, origin?: string): Promise<bo
|
|
|
120
120
|
*/
|
|
121
121
|
declare function fetchWhitelistInfo(serverUrl: string, includeAll?: boolean): Promise<any>;
|
|
122
122
|
|
|
123
|
+
/**
|
|
124
|
+
* GET /api/capabilities 返回的能力描述
|
|
125
|
+
*/
|
|
126
|
+
interface CapabilitiesResponse {
|
|
127
|
+
structureData: boolean;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* 请求 server 的 GET /api/capabilities,返回当前实例是否提供 structureData 等能力
|
|
131
|
+
*/
|
|
132
|
+
declare function fetchCapabilities(serverUrl: string): Promise<CapabilitiesResponse>;
|
|
133
|
+
|
|
123
134
|
/**
|
|
124
135
|
* 自动遍历 rawData 推断出体积受控的 schema,供 LLM 使用
|
|
125
136
|
*/
|
|
@@ -148,6 +159,30 @@ interface InferSchemaOptions {
|
|
|
148
159
|
*/
|
|
149
160
|
declare function inferSchema(rawData: any, options?: InferSchemaOptions): InferredSchema;
|
|
150
161
|
|
|
162
|
+
/**
|
|
163
|
+
* POST /api/structureData 返回的结构化结果(与 server AIProcessedData 一致)
|
|
164
|
+
*/
|
|
165
|
+
interface StructureDataResult {
|
|
166
|
+
source: string;
|
|
167
|
+
category: string;
|
|
168
|
+
content: string;
|
|
169
|
+
tags: string[];
|
|
170
|
+
images?: string[];
|
|
171
|
+
metadata?: Record<string, unknown>;
|
|
172
|
+
}
|
|
173
|
+
interface FetchStructureDataOptions {
|
|
174
|
+
systemPrompt?: string;
|
|
175
|
+
temperature?: number;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* 从 SSE 响应文本中解析最后一条 result 或 error
|
|
179
|
+
*/
|
|
180
|
+
declare function parseSSEResult(sseText: string): StructureDataResult;
|
|
181
|
+
/**
|
|
182
|
+
* 请求 server 的 POST /api/structureData,传入 schema,返回结构化结果
|
|
183
|
+
*/
|
|
184
|
+
declare function fetchStructureData(serverUrl: string, schema: InferredSchema, options?: FetchStructureDataOptions): Promise<StructureDataResult>;
|
|
185
|
+
|
|
151
186
|
/**
|
|
152
187
|
* Base64 数据隔离和替换工具
|
|
153
188
|
* 用于在 LLM 处理前替换 base64 图片数据,减少输入大小
|
|
@@ -175,4 +210,4 @@ declare function restoreBase64(data: any, replacements: Map<string, string>): an
|
|
|
175
210
|
*/
|
|
176
211
|
declare function extractImages(data: any): string[];
|
|
177
212
|
|
|
178
|
-
export { AgentLinkClient, type AgentLinkClientOptions, DEFAULT_SERVER_URL, type InferSchemaOptions, type InferredSchema, type SenderInfo, type URLData, type WhitelistInfo, type WhitelistResponse, base64ToUint8Array, compress, decodeDataFromUrl, decompress, encodeDataToUrl, extractAndReplaceBase64, extractImages, fetchWhitelistInfo, inferSchema, restoreBase64, uint8ArrayToBase64, verifyWhitelist };
|
|
213
|
+
export { AgentLinkClient, type AgentLinkClientOptions, type CapabilitiesResponse, DEFAULT_SERVER_URL, type FetchStructureDataOptions, type InferSchemaOptions, type InferredSchema, type SenderInfo, type StructureDataResult, type URLData, type WhitelistInfo, type WhitelistResponse, base64ToUint8Array, compress, decodeDataFromUrl, decompress, encodeDataToUrl, extractAndReplaceBase64, extractImages, fetchCapabilities, fetchStructureData, fetchWhitelistInfo, inferSchema, parseSSEResult, restoreBase64, uint8ArrayToBase64, verifyWhitelist };
|
package/dist/index.js
CHANGED
|
@@ -29,8 +29,11 @@ __export(index_exports, {
|
|
|
29
29
|
encodeDataToUrl: () => encodeDataToUrl,
|
|
30
30
|
extractAndReplaceBase64: () => extractAndReplaceBase64,
|
|
31
31
|
extractImages: () => extractImages,
|
|
32
|
+
fetchCapabilities: () => fetchCapabilities,
|
|
33
|
+
fetchStructureData: () => fetchStructureData,
|
|
32
34
|
fetchWhitelistInfo: () => fetchWhitelistInfo,
|
|
33
35
|
inferSchema: () => inferSchema,
|
|
36
|
+
parseSSEResult: () => parseSSEResult,
|
|
34
37
|
restoreBase64: () => restoreBase64,
|
|
35
38
|
uint8ArrayToBase64: () => uint8ArrayToBase64,
|
|
36
39
|
verifyWhitelist: () => verifyWhitelist
|
|
@@ -344,6 +347,68 @@ _AgentLinkClient.whitelistCache = /* @__PURE__ */ new Map();
|
|
|
344
347
|
_AgentLinkClient.CACHE_TTL = 60 * 60 * 1e3;
|
|
345
348
|
var AgentLinkClient = _AgentLinkClient;
|
|
346
349
|
|
|
350
|
+
// src/utils/capabilities.ts
|
|
351
|
+
async function fetchCapabilities(serverUrl) {
|
|
352
|
+
const base = serverUrl.replace(/\/$/, "");
|
|
353
|
+
const res = await fetch(`${base}/api/capabilities`);
|
|
354
|
+
if (!res.ok) {
|
|
355
|
+
throw new Error(`Capabilities request failed: ${res.status} ${res.statusText}`);
|
|
356
|
+
}
|
|
357
|
+
const data = await res.json();
|
|
358
|
+
return {
|
|
359
|
+
structureData: !!data.structureData
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
// src/utils/structureData.ts
|
|
364
|
+
function parseSSEResult(sseText) {
|
|
365
|
+
const lines = sseText.split("\n").filter((l) => l.startsWith("data: "));
|
|
366
|
+
for (let i = lines.length - 1; i >= 0; i--) {
|
|
367
|
+
const data = lines[i].replace(/^data: /, "").trim();
|
|
368
|
+
try {
|
|
369
|
+
const json = JSON.parse(data);
|
|
370
|
+
if (json.result) {
|
|
371
|
+
const r = json.result;
|
|
372
|
+
return {
|
|
373
|
+
source: String(r.source ?? ""),
|
|
374
|
+
category: String(r.category ?? ""),
|
|
375
|
+
content: String(r.content ?? ""),
|
|
376
|
+
tags: Array.isArray(r.tags) ? r.tags.map(String) : [],
|
|
377
|
+
images: r.images,
|
|
378
|
+
metadata: r.metadata
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
if (json.error) {
|
|
382
|
+
throw new Error(String(json.error));
|
|
383
|
+
}
|
|
384
|
+
} catch (e) {
|
|
385
|
+
if (e instanceof Error && e.message !== "Unexpected end of JSON input") {
|
|
386
|
+
throw e;
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
throw new Error("No result in SSE stream");
|
|
391
|
+
}
|
|
392
|
+
async function fetchStructureData(serverUrl, schema, options) {
|
|
393
|
+
const base = serverUrl.replace(/\/$/, "");
|
|
394
|
+
const body = { schema };
|
|
395
|
+
if (options?.systemPrompt != null) body.systemPrompt = options.systemPrompt;
|
|
396
|
+
if (options?.temperature != null) body.temperature = options.temperature;
|
|
397
|
+
const res = await fetch(`${base}/api/structureData`, {
|
|
398
|
+
method: "POST",
|
|
399
|
+
headers: { "Content-Type": "application/json" },
|
|
400
|
+
body: JSON.stringify(body)
|
|
401
|
+
});
|
|
402
|
+
if (!res.ok) {
|
|
403
|
+
const err = await res.json().catch(() => ({}));
|
|
404
|
+
throw new Error(
|
|
405
|
+
err.error || `StructureData request failed: ${res.status} ${res.statusText}`
|
|
406
|
+
);
|
|
407
|
+
}
|
|
408
|
+
const text = await res.text();
|
|
409
|
+
return parseSSEResult(text);
|
|
410
|
+
}
|
|
411
|
+
|
|
347
412
|
// src/utils/base64.ts
|
|
348
413
|
var DATA_URL_REGEX = /data:image\/[^;]+;base64,[A-Za-z0-9+/=]+/gi;
|
|
349
414
|
function extractAndReplaceBase64(data) {
|
package/dist/index.mjs
CHANGED
|
@@ -305,6 +305,68 @@ _AgentLinkClient.whitelistCache = /* @__PURE__ */ new Map();
|
|
|
305
305
|
_AgentLinkClient.CACHE_TTL = 60 * 60 * 1e3;
|
|
306
306
|
var AgentLinkClient = _AgentLinkClient;
|
|
307
307
|
|
|
308
|
+
// src/utils/capabilities.ts
|
|
309
|
+
async function fetchCapabilities(serverUrl) {
|
|
310
|
+
const base = serverUrl.replace(/\/$/, "");
|
|
311
|
+
const res = await fetch(`${base}/api/capabilities`);
|
|
312
|
+
if (!res.ok) {
|
|
313
|
+
throw new Error(`Capabilities request failed: ${res.status} ${res.statusText}`);
|
|
314
|
+
}
|
|
315
|
+
const data = await res.json();
|
|
316
|
+
return {
|
|
317
|
+
structureData: !!data.structureData
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// src/utils/structureData.ts
|
|
322
|
+
function parseSSEResult(sseText) {
|
|
323
|
+
const lines = sseText.split("\n").filter((l) => l.startsWith("data: "));
|
|
324
|
+
for (let i = lines.length - 1; i >= 0; i--) {
|
|
325
|
+
const data = lines[i].replace(/^data: /, "").trim();
|
|
326
|
+
try {
|
|
327
|
+
const json = JSON.parse(data);
|
|
328
|
+
if (json.result) {
|
|
329
|
+
const r = json.result;
|
|
330
|
+
return {
|
|
331
|
+
source: String(r.source ?? ""),
|
|
332
|
+
category: String(r.category ?? ""),
|
|
333
|
+
content: String(r.content ?? ""),
|
|
334
|
+
tags: Array.isArray(r.tags) ? r.tags.map(String) : [],
|
|
335
|
+
images: r.images,
|
|
336
|
+
metadata: r.metadata
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
if (json.error) {
|
|
340
|
+
throw new Error(String(json.error));
|
|
341
|
+
}
|
|
342
|
+
} catch (e) {
|
|
343
|
+
if (e instanceof Error && e.message !== "Unexpected end of JSON input") {
|
|
344
|
+
throw e;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
throw new Error("No result in SSE stream");
|
|
349
|
+
}
|
|
350
|
+
async function fetchStructureData(serverUrl, schema, options) {
|
|
351
|
+
const base = serverUrl.replace(/\/$/, "");
|
|
352
|
+
const body = { schema };
|
|
353
|
+
if (options?.systemPrompt != null) body.systemPrompt = options.systemPrompt;
|
|
354
|
+
if (options?.temperature != null) body.temperature = options.temperature;
|
|
355
|
+
const res = await fetch(`${base}/api/structureData`, {
|
|
356
|
+
method: "POST",
|
|
357
|
+
headers: { "Content-Type": "application/json" },
|
|
358
|
+
body: JSON.stringify(body)
|
|
359
|
+
});
|
|
360
|
+
if (!res.ok) {
|
|
361
|
+
const err = await res.json().catch(() => ({}));
|
|
362
|
+
throw new Error(
|
|
363
|
+
err.error || `StructureData request failed: ${res.status} ${res.statusText}`
|
|
364
|
+
);
|
|
365
|
+
}
|
|
366
|
+
const text = await res.text();
|
|
367
|
+
return parseSSEResult(text);
|
|
368
|
+
}
|
|
369
|
+
|
|
308
370
|
// src/utils/base64.ts
|
|
309
371
|
var DATA_URL_REGEX = /data:image\/[^;]+;base64,[A-Za-z0-9+/=]+/gi;
|
|
310
372
|
function extractAndReplaceBase64(data) {
|
|
@@ -479,8 +541,11 @@ export {
|
|
|
479
541
|
encodeDataToUrl,
|
|
480
542
|
extractAndReplaceBase64,
|
|
481
543
|
extractImages,
|
|
544
|
+
fetchCapabilities,
|
|
545
|
+
fetchStructureData,
|
|
482
546
|
fetchWhitelistInfo,
|
|
483
547
|
inferSchema,
|
|
548
|
+
parseSSEResult,
|
|
484
549
|
restoreBase64,
|
|
485
550
|
uint8ArrayToBase64,
|
|
486
551
|
verifyWhitelist
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* 复用包内 inferSchema、extractImages,仅发送 schema 请求 LLM,再按前端方式回填拼接。
|
|
4
4
|
*
|
|
5
5
|
* LLM 配置(二选一,在脚本内通过环境变量配置):
|
|
6
|
-
* - 方式 A:AGENTLINK_SERVER_URL — 请求该地址的 /api/
|
|
6
|
+
* - 方式 A:AGENTLINK_SERVER_URL — 请求该地址的 /api/structureData(需先启动 apps/server)
|
|
7
7
|
* - 方式 B:LLM_API_URL + LLM_API_KEY(及可选 LLM_MODEL)— 直接请求 OpenAI 兼容 API
|
|
8
8
|
*
|
|
9
9
|
* 运行:npm run test:llm-schema 或 npx tsx scripts/test-llm-schema.ts
|
|
@@ -24,7 +24,7 @@ interface AIProcessedData {
|
|
|
24
24
|
function getApiUrl(): string {
|
|
25
25
|
const serverUrl = process.env.AGENTLINK_SERVER_URL;
|
|
26
26
|
const llmUrl = process.env.LLM_API_URL;
|
|
27
|
-
if (serverUrl) return `${serverUrl.replace(/\/$/, '')}/api/
|
|
27
|
+
if (serverUrl) return `${serverUrl.replace(/\/$/, '')}/api/structureData`;
|
|
28
28
|
if (llmUrl) return ''; // 表示用方式 B,脚本内直接调 LLM
|
|
29
29
|
throw new Error(
|
|
30
30
|
'请配置 LLM:设置 AGENTLINK_SERVER_URL(方式 A)或 LLM_API_URL + LLM_API_KEY(方式 B)'
|