@dajiaoai/algeo-sdk 2.10.0 → 2.11.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/README.md CHANGED
@@ -192,6 +192,38 @@ const editor = await create(document.getElementById('editor-root'), {
192
192
  });
193
193
  ```
194
194
 
195
+ ### 接入自有资源库
196
+
197
+ 编辑模式支持由宿主提供只读图片资源库。图片仍由接入方存储和鉴权,SDK 只在内嵌页需要数据时调用 `query`:
198
+
199
+ ```typescript
200
+ const editor = await createEditor(document.getElementById('editor-root'), {
201
+ auth: { appId: 'xxxx' },
202
+ resourceLibrary: {
203
+ query: async (params, { signal }) => {
204
+ const response = await fetch(
205
+ `/api/material/list?page=${params.page}&pageSize=${params.pageSize}`,
206
+ { signal },
207
+ );
208
+ if (!response.ok) throw new Error('资源库请求失败');
209
+
210
+ const data = await response.json();
211
+ return {
212
+ items: data.items,
213
+ pageInfo: {
214
+ page: params.page,
215
+ pageSize: params.pageSize,
216
+ hasNext: data.hasNext,
217
+ total: data.total,
218
+ },
219
+ };
220
+ },
221
+ },
222
+ });
223
+ ```
224
+
225
+ `query` 返回的资源必须具有非空且不重复的 `id`、非空 `name`、`image/*` MIME 类型和完整 HTTP(S) URL。`width`、`height`、`size` 如提供,必须是正整数。搜索条件变化、面板关闭或 SDK 销毁时,请求可能通过 `signal` 被取消。
226
+
195
227
  ## API
196
228
 
197
229
  ### 常量
@@ -565,9 +597,16 @@ const items = await editor.slides.exportLatex({
565
597
  interface ExportedLatex {
566
598
  index: number; // 1-based 画板索引
567
599
  code: string;
600
+ assets: Array<{ path: string; blob: Blob; mediaType: string }>;
601
+ blob: Blob; // 有图片时为 ZIP,否则为 TeX
602
+ fileName: string;
603
+ format: 'tex' | 'zip';
568
604
  }
569
605
  ```
570
606
 
607
+ `blob` 可直接下载;当画板包含图片时,ZIP 内同时包含 `slide-N.tex` 和
608
+ TikZ 通过相对路径引用的 `images/*` 文件。
609
+
571
610
  #### `editor.history`
572
611
 
573
612
  | 方法 | 说明 |
@@ -3865,6 +3865,12 @@ var AlgeoError = class extends Error {
3865
3865
  this.name = "AlgeoError";
3866
3866
  }
3867
3867
  };
3868
+ objectType({
3869
+ text: stringType().optional(),
3870
+ images: arrayType(stringType().trim().min(1)).optional(),
3871
+ openPanel: booleanType().optional(),
3872
+ focus: booleanType().optional()
3873
+ }).passthrough();
3868
3874
  var openAiChatMessageV1Schema = objectType({
3869
3875
  role: stringType(),
3870
3876
  content: unionType([
@@ -3878,9 +3884,23 @@ var openAiChatMessageV1Schema = objectType({
3878
3884
  name: stringType().optional()
3879
3885
  }).passthrough();
3880
3886
  objectType({
3881
- model_id: stringType(),
3882
- messages: arrayType(unionType([openAiChatMessageV1Schema, recordType(unknownType())])),
3883
- extra_openai_params: recordType(unknownType()).optional()
3887
+ model: stringType().trim().min(1),
3888
+ input: unionType([
3889
+ stringType(),
3890
+ arrayType(unionType([openAiChatMessageV1Schema, recordType(unknownType())]))
3891
+ ]).optional(),
3892
+ previous_response_id: stringType().trim().min(1).optional(),
3893
+ instructions: stringType().optional(),
3894
+ temperature: numberType().min(0).max(2).optional(),
3895
+ max_output_tokens: numberType().int().positive().optional(),
3896
+ metadata: recordType(unknownType()).optional()
3897
+ }).passthrough().superRefine((value, ctx) => {
3898
+ if (!("input" in value) && !("previous_response_id" in value)) {
3899
+ ctx.addIssue({
3900
+ code: ZodIssueCode.custom,
3901
+ message: "input or previous_response_id is required."
3902
+ });
3903
+ }
3884
3904
  });
3885
3905
  objectType({
3886
3906
  id: stringType(),
@@ -4011,7 +4031,7 @@ function convertToLatest(content) {
4011
4031
  }
4012
4032
 
4013
4033
  /** SDK 版本号,构建时由 rollup 注入 */
4014
- const VERSION = '2.10.0';
4034
+ const VERSION = '2.11.0';
4015
4035
  const DEFAULT_EMBED_BASE = 'https://dajiaoai.com';
4016
4036
  const DEFAULT_PRESENTATION_PATH = '/embed/present';
4017
4037
  const DEFAULT_EDITOR_PATH = '/embed/edit';
@@ -4050,6 +4070,21 @@ function isAiRequestMessage(msg) {
4050
4070
  typeof msg.payload === 'object' &&
4051
4071
  msg.payload !== null);
4052
4072
  }
4073
+ function isResourceLibraryRequestMessage(msg) {
4074
+ if (typeof msg !== 'object' || msg === null) {
4075
+ return false;
4076
+ }
4077
+ const value = msg;
4078
+ if (typeof value.requestId !== 'string') {
4079
+ return false;
4080
+ }
4081
+ if (value.type === 'resourceLibraryCancel') {
4082
+ return true;
4083
+ }
4084
+ return (value.type === 'resourceLibraryQuery' &&
4085
+ typeof value.params === 'object' &&
4086
+ value.params !== null);
4087
+ }
4053
4088
  function isEmbedEventMessage(msg) {
4054
4089
  if (typeof msg !== 'object' || msg == null || !('type' in msg)) {
4055
4090
  return false;
@@ -4256,7 +4291,9 @@ class EmbeddedTarget {
4256
4291
  pending.reject(new AlgeoError(err?.message ?? '未知错误', err?.code ?? EMBED_ERROR_CODES.UNKNOWN_ERROR, err?.details));
4257
4292
  return;
4258
4293
  }
4259
- if ((isSaveRequestMessage(data) || isAiRequestMessage(data)) &&
4294
+ if ((isSaveRequestMessage(data) ||
4295
+ isAiRequestMessage(data) ||
4296
+ isResourceLibraryRequestMessage(data)) &&
4260
4297
  this.handleRequestMessage(data, iframe.contentWindow)) {
4261
4298
  return;
4262
4299
  }
@@ -4445,6 +4482,7 @@ class EmbeddedPresentation extends EmbeddedTarget {
4445
4482
  class EmbeddedEditor extends EmbeddedTarget {
4446
4483
  constructor(container) {
4447
4484
  super(container, 'editor');
4485
+ this.resourceLibraryRequests = new Map();
4448
4486
  this.currentSlideIndex = 0;
4449
4487
  this.slideCount = 0;
4450
4488
  this.historyCount = 0;
@@ -4549,11 +4587,15 @@ class EmbeddedEditor extends EmbeddedTarget {
4549
4587
  throw new AlgeoError('编辑模式需要提供 auth.appId。', EMBED_ERROR_CODES.MISSING_APP_ID);
4550
4588
  }
4551
4589
  this.uiConfig = options.ui ? { ...options.ui } : {};
4590
+ this.resourceLibrary = options.resourceLibrary;
4552
4591
  await this.init({
4553
4592
  baseUrl,
4554
4593
  auth: options.auth,
4555
4594
  initialId: options.shareId,
4556
4595
  });
4596
+ await this.post('setResourceLibraryAvailability', {
4597
+ available: Boolean(this.resourceLibrary),
4598
+ });
4557
4599
  if (Object.keys(this.uiConfig).length > 0) {
4558
4600
  await this.mode.setUiConfig(this.uiConfig);
4559
4601
  }
@@ -4589,6 +4631,15 @@ class EmbeddedEditor extends EmbeddedTarget {
4589
4631
  this.currentSlideIndex = Math.min(this.currentSlideIndex, Math.max(this.slideCount - 1, 0));
4590
4632
  }
4591
4633
  handleRequestMessage(message, sourceWindow) {
4634
+ if (message.type === 'resourceLibraryCancel') {
4635
+ this.resourceLibraryRequests.get(message.requestId)?.abort();
4636
+ this.resourceLibraryRequests.delete(message.requestId);
4637
+ return true;
4638
+ }
4639
+ if (message.type === 'resourceLibraryQuery') {
4640
+ void this.handleResourceLibraryQuery(message, sourceWindow);
4641
+ return true;
4642
+ }
4592
4643
  if (message.type === 'save') {
4593
4644
  void this.handleSaveRequest(message, sourceWindow);
4594
4645
  return true;
@@ -4600,9 +4651,116 @@ class EmbeddedEditor extends EmbeddedTarget {
4600
4651
  return false;
4601
4652
  }
4602
4653
  async destroy() {
4654
+ this.resourceLibraryRequests.forEach((controller) => controller.abort());
4655
+ this.resourceLibraryRequests.clear();
4603
4656
  this.cancelActiveAi('destroyed', true);
4604
4657
  await super.destroy();
4605
4658
  }
4659
+ async handleResourceLibraryQuery(message, sourceWindow) {
4660
+ const controller = new AbortController();
4661
+ this.resourceLibraryRequests.set(message.requestId, controller);
4662
+ const respond = (payload) => {
4663
+ if (!controller.signal.aborted) {
4664
+ sourceWindow.postMessage(payload, '*');
4665
+ }
4666
+ };
4667
+ try {
4668
+ if (!this.resourceLibrary) {
4669
+ throw new AlgeoError('宿主未配置 resourceLibrary.query', EMBED_ERROR_CODES.BAD_REQUEST);
4670
+ }
4671
+ this.validateResourceLibraryQuery(message.params);
4672
+ const result = await this.resourceLibrary.query(message.params, {
4673
+ signal: controller.signal,
4674
+ });
4675
+ if (controller.signal.aborted)
4676
+ return;
4677
+ this.validateResourceLibraryResult(result, message.params);
4678
+ respond({
4679
+ type: 'response',
4680
+ requestId: message.requestId,
4681
+ success: true,
4682
+ result,
4683
+ });
4684
+ }
4685
+ catch (error) {
4686
+ if (controller.signal.aborted)
4687
+ return;
4688
+ respond({
4689
+ type: 'response',
4690
+ requestId: message.requestId,
4691
+ success: false,
4692
+ error: {
4693
+ code: error instanceof AlgeoError
4694
+ ? error.code
4695
+ : EMBED_ERROR_CODES.UNKNOWN_ERROR,
4696
+ message: error instanceof Error ? error.message : String(error),
4697
+ },
4698
+ });
4699
+ }
4700
+ finally {
4701
+ if (this.resourceLibraryRequests.get(message.requestId) === controller) {
4702
+ this.resourceLibraryRequests.delete(message.requestId);
4703
+ }
4704
+ }
4705
+ }
4706
+ validateResourceLibraryQuery(params) {
4707
+ if (!Number.isInteger(params.page) ||
4708
+ params.page < 1 ||
4709
+ !Number.isInteger(params.pageSize) ||
4710
+ params.pageSize < 1 ||
4711
+ params.pageSize > 100 ||
4712
+ (params.keyword !== undefined && typeof params.keyword !== 'string') ||
4713
+ (params.mediaTypes !== undefined &&
4714
+ (!Array.isArray(params.mediaTypes) ||
4715
+ params.mediaTypes.some((value) => typeof value !== 'string')))) {
4716
+ throw new AlgeoError('资源库查询参数无效', EMBED_ERROR_CODES.BAD_REQUEST);
4717
+ }
4718
+ }
4719
+ validateResourceLibraryResult(result, params) {
4720
+ const invalid = () => {
4721
+ throw new AlgeoError('资源数据格式无效', EMBED_ERROR_CODES.BAD_REQUEST);
4722
+ };
4723
+ if (!result || !Array.isArray(result.items) || !result.pageInfo)
4724
+ invalid();
4725
+ const pageInfo = result.pageInfo;
4726
+ if (pageInfo.page !== params.page ||
4727
+ pageInfo.pageSize !== params.pageSize ||
4728
+ typeof pageInfo.hasNext !== 'boolean' ||
4729
+ (pageInfo.total !== undefined &&
4730
+ (!Number.isInteger(pageInfo.total) || pageInfo.total < 0)))
4731
+ invalid();
4732
+ const ids = new Set();
4733
+ for (const item of result.items) {
4734
+ if (!item ||
4735
+ typeof item.id !== 'string' ||
4736
+ !item.id.trim() ||
4737
+ ids.has(item.id) ||
4738
+ typeof item.name !== 'string' ||
4739
+ !item.name.trim() ||
4740
+ typeof item.mediaType !== 'string' ||
4741
+ !item.mediaType.startsWith('image/') ||
4742
+ !this.isHttpUrl(item.url) ||
4743
+ (item.thumbnailUrl !== undefined && !this.isHttpUrl(item.thumbnailUrl)))
4744
+ invalid();
4745
+ ids.add(item.id);
4746
+ for (const value of [item.width, item.height, item.size]) {
4747
+ if (value !== undefined && (!Number.isInteger(value) || value <= 0)) {
4748
+ invalid();
4749
+ }
4750
+ }
4751
+ }
4752
+ }
4753
+ isHttpUrl(value) {
4754
+ if (typeof value !== 'string')
4755
+ return false;
4756
+ try {
4757
+ const url = new URL(value);
4758
+ return url.protocol === 'http:' || url.protocol === 'https:';
4759
+ }
4760
+ catch {
4761
+ return false;
4762
+ }
4763
+ }
4606
4764
  async loadContent(content, source) {
4607
4765
  await this.post('loadContent', { content });
4608
4766
  this.currentContent = content;
@@ -3863,6 +3863,12 @@ var AlgeoError = class extends Error {
3863
3863
  this.name = "AlgeoError";
3864
3864
  }
3865
3865
  };
3866
+ objectType({
3867
+ text: stringType().optional(),
3868
+ images: arrayType(stringType().trim().min(1)).optional(),
3869
+ openPanel: booleanType().optional(),
3870
+ focus: booleanType().optional()
3871
+ }).passthrough();
3866
3872
  var openAiChatMessageV1Schema = objectType({
3867
3873
  role: stringType(),
3868
3874
  content: unionType([
@@ -3876,9 +3882,23 @@ var openAiChatMessageV1Schema = objectType({
3876
3882
  name: stringType().optional()
3877
3883
  }).passthrough();
3878
3884
  objectType({
3879
- model_id: stringType(),
3880
- messages: arrayType(unionType([openAiChatMessageV1Schema, recordType(unknownType())])),
3881
- extra_openai_params: recordType(unknownType()).optional()
3885
+ model: stringType().trim().min(1),
3886
+ input: unionType([
3887
+ stringType(),
3888
+ arrayType(unionType([openAiChatMessageV1Schema, recordType(unknownType())]))
3889
+ ]).optional(),
3890
+ previous_response_id: stringType().trim().min(1).optional(),
3891
+ instructions: stringType().optional(),
3892
+ temperature: numberType().min(0).max(2).optional(),
3893
+ max_output_tokens: numberType().int().positive().optional(),
3894
+ metadata: recordType(unknownType()).optional()
3895
+ }).passthrough().superRefine((value, ctx) => {
3896
+ if (!("input" in value) && !("previous_response_id" in value)) {
3897
+ ctx.addIssue({
3898
+ code: ZodIssueCode.custom,
3899
+ message: "input or previous_response_id is required."
3900
+ });
3901
+ }
3882
3902
  });
3883
3903
  objectType({
3884
3904
  id: stringType(),
@@ -4009,7 +4029,7 @@ function convertToLatest(content) {
4009
4029
  }
4010
4030
 
4011
4031
  /** SDK 版本号,构建时由 rollup 注入 */
4012
- const VERSION = '2.10.0';
4032
+ const VERSION = '2.11.0';
4013
4033
  const DEFAULT_EMBED_BASE = 'https://dajiaoai.com';
4014
4034
  const DEFAULT_PRESENTATION_PATH = '/embed/present';
4015
4035
  const DEFAULT_EDITOR_PATH = '/embed/edit';
@@ -4048,6 +4068,21 @@ function isAiRequestMessage(msg) {
4048
4068
  typeof msg.payload === 'object' &&
4049
4069
  msg.payload !== null);
4050
4070
  }
4071
+ function isResourceLibraryRequestMessage(msg) {
4072
+ if (typeof msg !== 'object' || msg === null) {
4073
+ return false;
4074
+ }
4075
+ const value = msg;
4076
+ if (typeof value.requestId !== 'string') {
4077
+ return false;
4078
+ }
4079
+ if (value.type === 'resourceLibraryCancel') {
4080
+ return true;
4081
+ }
4082
+ return (value.type === 'resourceLibraryQuery' &&
4083
+ typeof value.params === 'object' &&
4084
+ value.params !== null);
4085
+ }
4051
4086
  function isEmbedEventMessage(msg) {
4052
4087
  if (typeof msg !== 'object' || msg == null || !('type' in msg)) {
4053
4088
  return false;
@@ -4254,7 +4289,9 @@ class EmbeddedTarget {
4254
4289
  pending.reject(new AlgeoError(err?.message ?? '未知错误', err?.code ?? EMBED_ERROR_CODES.UNKNOWN_ERROR, err?.details));
4255
4290
  return;
4256
4291
  }
4257
- if ((isSaveRequestMessage(data) || isAiRequestMessage(data)) &&
4292
+ if ((isSaveRequestMessage(data) ||
4293
+ isAiRequestMessage(data) ||
4294
+ isResourceLibraryRequestMessage(data)) &&
4258
4295
  this.handleRequestMessage(data, iframe.contentWindow)) {
4259
4296
  return;
4260
4297
  }
@@ -4443,6 +4480,7 @@ class EmbeddedPresentation extends EmbeddedTarget {
4443
4480
  class EmbeddedEditor extends EmbeddedTarget {
4444
4481
  constructor(container) {
4445
4482
  super(container, 'editor');
4483
+ this.resourceLibraryRequests = new Map();
4446
4484
  this.currentSlideIndex = 0;
4447
4485
  this.slideCount = 0;
4448
4486
  this.historyCount = 0;
@@ -4547,11 +4585,15 @@ class EmbeddedEditor extends EmbeddedTarget {
4547
4585
  throw new AlgeoError('编辑模式需要提供 auth.appId。', EMBED_ERROR_CODES.MISSING_APP_ID);
4548
4586
  }
4549
4587
  this.uiConfig = options.ui ? { ...options.ui } : {};
4588
+ this.resourceLibrary = options.resourceLibrary;
4550
4589
  await this.init({
4551
4590
  baseUrl,
4552
4591
  auth: options.auth,
4553
4592
  initialId: options.shareId,
4554
4593
  });
4594
+ await this.post('setResourceLibraryAvailability', {
4595
+ available: Boolean(this.resourceLibrary),
4596
+ });
4555
4597
  if (Object.keys(this.uiConfig).length > 0) {
4556
4598
  await this.mode.setUiConfig(this.uiConfig);
4557
4599
  }
@@ -4587,6 +4629,15 @@ class EmbeddedEditor extends EmbeddedTarget {
4587
4629
  this.currentSlideIndex = Math.min(this.currentSlideIndex, Math.max(this.slideCount - 1, 0));
4588
4630
  }
4589
4631
  handleRequestMessage(message, sourceWindow) {
4632
+ if (message.type === 'resourceLibraryCancel') {
4633
+ this.resourceLibraryRequests.get(message.requestId)?.abort();
4634
+ this.resourceLibraryRequests.delete(message.requestId);
4635
+ return true;
4636
+ }
4637
+ if (message.type === 'resourceLibraryQuery') {
4638
+ void this.handleResourceLibraryQuery(message, sourceWindow);
4639
+ return true;
4640
+ }
4590
4641
  if (message.type === 'save') {
4591
4642
  void this.handleSaveRequest(message, sourceWindow);
4592
4643
  return true;
@@ -4598,9 +4649,116 @@ class EmbeddedEditor extends EmbeddedTarget {
4598
4649
  return false;
4599
4650
  }
4600
4651
  async destroy() {
4652
+ this.resourceLibraryRequests.forEach((controller) => controller.abort());
4653
+ this.resourceLibraryRequests.clear();
4601
4654
  this.cancelActiveAi('destroyed', true);
4602
4655
  await super.destroy();
4603
4656
  }
4657
+ async handleResourceLibraryQuery(message, sourceWindow) {
4658
+ const controller = new AbortController();
4659
+ this.resourceLibraryRequests.set(message.requestId, controller);
4660
+ const respond = (payload) => {
4661
+ if (!controller.signal.aborted) {
4662
+ sourceWindow.postMessage(payload, '*');
4663
+ }
4664
+ };
4665
+ try {
4666
+ if (!this.resourceLibrary) {
4667
+ throw new AlgeoError('宿主未配置 resourceLibrary.query', EMBED_ERROR_CODES.BAD_REQUEST);
4668
+ }
4669
+ this.validateResourceLibraryQuery(message.params);
4670
+ const result = await this.resourceLibrary.query(message.params, {
4671
+ signal: controller.signal,
4672
+ });
4673
+ if (controller.signal.aborted)
4674
+ return;
4675
+ this.validateResourceLibraryResult(result, message.params);
4676
+ respond({
4677
+ type: 'response',
4678
+ requestId: message.requestId,
4679
+ success: true,
4680
+ result,
4681
+ });
4682
+ }
4683
+ catch (error) {
4684
+ if (controller.signal.aborted)
4685
+ return;
4686
+ respond({
4687
+ type: 'response',
4688
+ requestId: message.requestId,
4689
+ success: false,
4690
+ error: {
4691
+ code: error instanceof AlgeoError
4692
+ ? error.code
4693
+ : EMBED_ERROR_CODES.UNKNOWN_ERROR,
4694
+ message: error instanceof Error ? error.message : String(error),
4695
+ },
4696
+ });
4697
+ }
4698
+ finally {
4699
+ if (this.resourceLibraryRequests.get(message.requestId) === controller) {
4700
+ this.resourceLibraryRequests.delete(message.requestId);
4701
+ }
4702
+ }
4703
+ }
4704
+ validateResourceLibraryQuery(params) {
4705
+ if (!Number.isInteger(params.page) ||
4706
+ params.page < 1 ||
4707
+ !Number.isInteger(params.pageSize) ||
4708
+ params.pageSize < 1 ||
4709
+ params.pageSize > 100 ||
4710
+ (params.keyword !== undefined && typeof params.keyword !== 'string') ||
4711
+ (params.mediaTypes !== undefined &&
4712
+ (!Array.isArray(params.mediaTypes) ||
4713
+ params.mediaTypes.some((value) => typeof value !== 'string')))) {
4714
+ throw new AlgeoError('资源库查询参数无效', EMBED_ERROR_CODES.BAD_REQUEST);
4715
+ }
4716
+ }
4717
+ validateResourceLibraryResult(result, params) {
4718
+ const invalid = () => {
4719
+ throw new AlgeoError('资源数据格式无效', EMBED_ERROR_CODES.BAD_REQUEST);
4720
+ };
4721
+ if (!result || !Array.isArray(result.items) || !result.pageInfo)
4722
+ invalid();
4723
+ const pageInfo = result.pageInfo;
4724
+ if (pageInfo.page !== params.page ||
4725
+ pageInfo.pageSize !== params.pageSize ||
4726
+ typeof pageInfo.hasNext !== 'boolean' ||
4727
+ (pageInfo.total !== undefined &&
4728
+ (!Number.isInteger(pageInfo.total) || pageInfo.total < 0)))
4729
+ invalid();
4730
+ const ids = new Set();
4731
+ for (const item of result.items) {
4732
+ if (!item ||
4733
+ typeof item.id !== 'string' ||
4734
+ !item.id.trim() ||
4735
+ ids.has(item.id) ||
4736
+ typeof item.name !== 'string' ||
4737
+ !item.name.trim() ||
4738
+ typeof item.mediaType !== 'string' ||
4739
+ !item.mediaType.startsWith('image/') ||
4740
+ !this.isHttpUrl(item.url) ||
4741
+ (item.thumbnailUrl !== undefined && !this.isHttpUrl(item.thumbnailUrl)))
4742
+ invalid();
4743
+ ids.add(item.id);
4744
+ for (const value of [item.width, item.height, item.size]) {
4745
+ if (value !== undefined && (!Number.isInteger(value) || value <= 0)) {
4746
+ invalid();
4747
+ }
4748
+ }
4749
+ }
4750
+ }
4751
+ isHttpUrl(value) {
4752
+ if (typeof value !== 'string')
4753
+ return false;
4754
+ try {
4755
+ const url = new URL(value);
4756
+ return url.protocol === 'http:' || url.protocol === 'https:';
4757
+ }
4758
+ catch {
4759
+ return false;
4760
+ }
4761
+ }
4604
4762
  async loadContent(content, source) {
4605
4763
  await this.post('loadContent', { content });
4606
4764
  this.currentContent = content;
@@ -3869,6 +3869,12 @@
3869
3869
  this.name = "AlgeoError";
3870
3870
  }
3871
3871
  };
3872
+ objectType({
3873
+ text: stringType().optional(),
3874
+ images: arrayType(stringType().trim().min(1)).optional(),
3875
+ openPanel: booleanType().optional(),
3876
+ focus: booleanType().optional()
3877
+ }).passthrough();
3872
3878
  var openAiChatMessageV1Schema = objectType({
3873
3879
  role: stringType(),
3874
3880
  content: unionType([
@@ -3882,9 +3888,23 @@
3882
3888
  name: stringType().optional()
3883
3889
  }).passthrough();
3884
3890
  objectType({
3885
- model_id: stringType(),
3886
- messages: arrayType(unionType([openAiChatMessageV1Schema, recordType(unknownType())])),
3887
- extra_openai_params: recordType(unknownType()).optional()
3891
+ model: stringType().trim().min(1),
3892
+ input: unionType([
3893
+ stringType(),
3894
+ arrayType(unionType([openAiChatMessageV1Schema, recordType(unknownType())]))
3895
+ ]).optional(),
3896
+ previous_response_id: stringType().trim().min(1).optional(),
3897
+ instructions: stringType().optional(),
3898
+ temperature: numberType().min(0).max(2).optional(),
3899
+ max_output_tokens: numberType().int().positive().optional(),
3900
+ metadata: recordType(unknownType()).optional()
3901
+ }).passthrough().superRefine((value, ctx) => {
3902
+ if (!("input" in value) && !("previous_response_id" in value)) {
3903
+ ctx.addIssue({
3904
+ code: ZodIssueCode.custom,
3905
+ message: "input or previous_response_id is required."
3906
+ });
3907
+ }
3888
3908
  });
3889
3909
  objectType({
3890
3910
  id: stringType(),
@@ -4015,7 +4035,7 @@
4015
4035
  }
4016
4036
 
4017
4037
  /** SDK 版本号,构建时由 rollup 注入 */
4018
- const VERSION = '2.10.0';
4038
+ const VERSION = '2.11.0';
4019
4039
  const DEFAULT_EMBED_BASE = 'https://dajiaoai.com';
4020
4040
  const DEFAULT_PRESENTATION_PATH = '/embed/present';
4021
4041
  const DEFAULT_EDITOR_PATH = '/embed/edit';
@@ -4054,6 +4074,21 @@
4054
4074
  typeof msg.payload === 'object' &&
4055
4075
  msg.payload !== null);
4056
4076
  }
4077
+ function isResourceLibraryRequestMessage(msg) {
4078
+ if (typeof msg !== 'object' || msg === null) {
4079
+ return false;
4080
+ }
4081
+ const value = msg;
4082
+ if (typeof value.requestId !== 'string') {
4083
+ return false;
4084
+ }
4085
+ if (value.type === 'resourceLibraryCancel') {
4086
+ return true;
4087
+ }
4088
+ return (value.type === 'resourceLibraryQuery' &&
4089
+ typeof value.params === 'object' &&
4090
+ value.params !== null);
4091
+ }
4057
4092
  function isEmbedEventMessage(msg) {
4058
4093
  if (typeof msg !== 'object' || msg == null || !('type' in msg)) {
4059
4094
  return false;
@@ -4260,7 +4295,9 @@
4260
4295
  pending.reject(new AlgeoError(err?.message ?? '未知错误', err?.code ?? EMBED_ERROR_CODES.UNKNOWN_ERROR, err?.details));
4261
4296
  return;
4262
4297
  }
4263
- if ((isSaveRequestMessage(data) || isAiRequestMessage(data)) &&
4298
+ if ((isSaveRequestMessage(data) ||
4299
+ isAiRequestMessage(data) ||
4300
+ isResourceLibraryRequestMessage(data)) &&
4264
4301
  this.handleRequestMessage(data, iframe.contentWindow)) {
4265
4302
  return;
4266
4303
  }
@@ -4449,6 +4486,7 @@
4449
4486
  class EmbeddedEditor extends EmbeddedTarget {
4450
4487
  constructor(container) {
4451
4488
  super(container, 'editor');
4489
+ this.resourceLibraryRequests = new Map();
4452
4490
  this.currentSlideIndex = 0;
4453
4491
  this.slideCount = 0;
4454
4492
  this.historyCount = 0;
@@ -4553,11 +4591,15 @@
4553
4591
  throw new AlgeoError('编辑模式需要提供 auth.appId。', EMBED_ERROR_CODES.MISSING_APP_ID);
4554
4592
  }
4555
4593
  this.uiConfig = options.ui ? { ...options.ui } : {};
4594
+ this.resourceLibrary = options.resourceLibrary;
4556
4595
  await this.init({
4557
4596
  baseUrl,
4558
4597
  auth: options.auth,
4559
4598
  initialId: options.shareId,
4560
4599
  });
4600
+ await this.post('setResourceLibraryAvailability', {
4601
+ available: Boolean(this.resourceLibrary),
4602
+ });
4561
4603
  if (Object.keys(this.uiConfig).length > 0) {
4562
4604
  await this.mode.setUiConfig(this.uiConfig);
4563
4605
  }
@@ -4593,6 +4635,15 @@
4593
4635
  this.currentSlideIndex = Math.min(this.currentSlideIndex, Math.max(this.slideCount - 1, 0));
4594
4636
  }
4595
4637
  handleRequestMessage(message, sourceWindow) {
4638
+ if (message.type === 'resourceLibraryCancel') {
4639
+ this.resourceLibraryRequests.get(message.requestId)?.abort();
4640
+ this.resourceLibraryRequests.delete(message.requestId);
4641
+ return true;
4642
+ }
4643
+ if (message.type === 'resourceLibraryQuery') {
4644
+ void this.handleResourceLibraryQuery(message, sourceWindow);
4645
+ return true;
4646
+ }
4596
4647
  if (message.type === 'save') {
4597
4648
  void this.handleSaveRequest(message, sourceWindow);
4598
4649
  return true;
@@ -4604,9 +4655,116 @@
4604
4655
  return false;
4605
4656
  }
4606
4657
  async destroy() {
4658
+ this.resourceLibraryRequests.forEach((controller) => controller.abort());
4659
+ this.resourceLibraryRequests.clear();
4607
4660
  this.cancelActiveAi('destroyed', true);
4608
4661
  await super.destroy();
4609
4662
  }
4663
+ async handleResourceLibraryQuery(message, sourceWindow) {
4664
+ const controller = new AbortController();
4665
+ this.resourceLibraryRequests.set(message.requestId, controller);
4666
+ const respond = (payload) => {
4667
+ if (!controller.signal.aborted) {
4668
+ sourceWindow.postMessage(payload, '*');
4669
+ }
4670
+ };
4671
+ try {
4672
+ if (!this.resourceLibrary) {
4673
+ throw new AlgeoError('宿主未配置 resourceLibrary.query', EMBED_ERROR_CODES.BAD_REQUEST);
4674
+ }
4675
+ this.validateResourceLibraryQuery(message.params);
4676
+ const result = await this.resourceLibrary.query(message.params, {
4677
+ signal: controller.signal,
4678
+ });
4679
+ if (controller.signal.aborted)
4680
+ return;
4681
+ this.validateResourceLibraryResult(result, message.params);
4682
+ respond({
4683
+ type: 'response',
4684
+ requestId: message.requestId,
4685
+ success: true,
4686
+ result,
4687
+ });
4688
+ }
4689
+ catch (error) {
4690
+ if (controller.signal.aborted)
4691
+ return;
4692
+ respond({
4693
+ type: 'response',
4694
+ requestId: message.requestId,
4695
+ success: false,
4696
+ error: {
4697
+ code: error instanceof AlgeoError
4698
+ ? error.code
4699
+ : EMBED_ERROR_CODES.UNKNOWN_ERROR,
4700
+ message: error instanceof Error ? error.message : String(error),
4701
+ },
4702
+ });
4703
+ }
4704
+ finally {
4705
+ if (this.resourceLibraryRequests.get(message.requestId) === controller) {
4706
+ this.resourceLibraryRequests.delete(message.requestId);
4707
+ }
4708
+ }
4709
+ }
4710
+ validateResourceLibraryQuery(params) {
4711
+ if (!Number.isInteger(params.page) ||
4712
+ params.page < 1 ||
4713
+ !Number.isInteger(params.pageSize) ||
4714
+ params.pageSize < 1 ||
4715
+ params.pageSize > 100 ||
4716
+ (params.keyword !== undefined && typeof params.keyword !== 'string') ||
4717
+ (params.mediaTypes !== undefined &&
4718
+ (!Array.isArray(params.mediaTypes) ||
4719
+ params.mediaTypes.some((value) => typeof value !== 'string')))) {
4720
+ throw new AlgeoError('资源库查询参数无效', EMBED_ERROR_CODES.BAD_REQUEST);
4721
+ }
4722
+ }
4723
+ validateResourceLibraryResult(result, params) {
4724
+ const invalid = () => {
4725
+ throw new AlgeoError('资源数据格式无效', EMBED_ERROR_CODES.BAD_REQUEST);
4726
+ };
4727
+ if (!result || !Array.isArray(result.items) || !result.pageInfo)
4728
+ invalid();
4729
+ const pageInfo = result.pageInfo;
4730
+ if (pageInfo.page !== params.page ||
4731
+ pageInfo.pageSize !== params.pageSize ||
4732
+ typeof pageInfo.hasNext !== 'boolean' ||
4733
+ (pageInfo.total !== undefined &&
4734
+ (!Number.isInteger(pageInfo.total) || pageInfo.total < 0)))
4735
+ invalid();
4736
+ const ids = new Set();
4737
+ for (const item of result.items) {
4738
+ if (!item ||
4739
+ typeof item.id !== 'string' ||
4740
+ !item.id.trim() ||
4741
+ ids.has(item.id) ||
4742
+ typeof item.name !== 'string' ||
4743
+ !item.name.trim() ||
4744
+ typeof item.mediaType !== 'string' ||
4745
+ !item.mediaType.startsWith('image/') ||
4746
+ !this.isHttpUrl(item.url) ||
4747
+ (item.thumbnailUrl !== undefined && !this.isHttpUrl(item.thumbnailUrl)))
4748
+ invalid();
4749
+ ids.add(item.id);
4750
+ for (const value of [item.width, item.height, item.size]) {
4751
+ if (value !== undefined && (!Number.isInteger(value) || value <= 0)) {
4752
+ invalid();
4753
+ }
4754
+ }
4755
+ }
4756
+ }
4757
+ isHttpUrl(value) {
4758
+ if (typeof value !== 'string')
4759
+ return false;
4760
+ try {
4761
+ const url = new URL(value);
4762
+ return url.protocol === 'http:' || url.protocol === 'https:';
4763
+ }
4764
+ catch {
4765
+ return false;
4766
+ }
4767
+ }
4610
4768
  async loadContent(content, source) {
4611
4769
  await this.post('loadContent', { content });
4612
4770
  this.currentContent = content;
@@ -1,6 +1,8 @@
1
1
  import { EmbeddedTarget } from './embedded-target';
2
2
  import { type AiApi, type AiCancelEvent, type AlgeoEditorCreateOptions, type ContentChangeEvent, type DocumentApi, type EmbedRequestMessage, type EmbeddedEditorEventListenerMap, type EmbeddedEditorEventMap, type EmbeddedEditorEventName, type HistoryApi, type ModeApi, type SaveEvent, type SlidesApi } from './shared';
3
3
  export declare class EmbeddedEditor extends EmbeddedTarget<EmbeddedEditorEventMap, EmbeddedEditorEventName, EmbeddedEditorEventListenerMap> {
4
+ private resourceLibrary?;
5
+ private readonly resourceLibraryRequests;
4
6
  readonly document: DocumentApi;
5
7
  readonly slides: SlidesApi;
6
8
  readonly history: HistoryApi;
@@ -21,6 +23,10 @@ export declare class EmbeddedEditor extends EmbeddedTarget<EmbeddedEditorEventMa
21
23
  } | AiCancelEvent): void;
22
24
  protected handleRequestMessage(message: EmbedRequestMessage, sourceWindow: Window): boolean;
23
25
  destroy(): Promise<void>;
26
+ private handleResourceLibraryQuery;
27
+ private validateResourceLibraryQuery;
28
+ private validateResourceLibraryResult;
29
+ private isHttpUrl;
24
30
  private loadContent;
25
31
  private switchTo;
26
32
  private addSlide;
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  export { AlgeoError, EMBED_ERROR_CODES, VERSION } from './shared';
2
2
  export type { FileContentLatest } from '@dajiaoai/algeo-protocol';
3
3
  export { convertToLatest } from '@dajiaoai/algeo-protocol';
4
- export type { AiApi, AiCancelEvent, AiRawSseEventV1, AiRequestEvent, AiRunPayloadV1, AiStreamEventV1, AlgeoCreateOptions, AlgeoEditorAuthOptions, AlgeoEditorCreateOptions, AlgeoEditorSaveResult, AlgeoEditorUiConfig, AlgeoEmbedMode, AlgeoErrorPayload, AlgeoPresentationCreateOptions, AlgeoPresentationUiConfig, ContentChangeEvent, DocumentApi, EmbeddedEditorEventListenerMap, EmbeddedEditorEventMap, EmbeddedEditorEventName, EmbeddedPresentationEventListenerMap, EmbeddedPresentationEventMap, EmbeddedPresentationEventName, ExportImageFormat, ExportImageOptions, ExportImageSizeMode, ExportImageViewMode, ExportImageContainMode, ExportViewBounds, ExportPadding, ExportSlideImageOptions, ExportedSlideImage, ExportSlideImageResult, ExportLatexOptions, ExportedLatex, ExportLatexResult, GetContentResult, GetSlideCountResult, HistoryApi, LoadFileResult, LoadShareByIdResult, ModeApi, PresentationModeApi, ReadyEvent, ReplResult, SlideChangeEvent, SlideIndexResult, SlidesApi, SwitchSlideResult, SaveEvent, } from './shared';
4
+ export type { AiApi, AiCancelEvent, AiRawSseEventV1, AiRequestEvent, AiRunPayloadV1, AiStreamEventV1, AlgeoCreateOptions, AlgeoEditorAuthOptions, AlgeoEditorCreateOptions, AlgeoEditorSaveResult, AlgeoEditorUiConfig, AlgeoEmbedMode, AlgeoErrorPayload, AlgeoPresentationCreateOptions, AlgeoPresentationUiConfig, ContentChangeEvent, DocumentApi, EmbeddedEditorEventListenerMap, EmbeddedEditorEventMap, EmbeddedEditorEventName, EmbeddedPresentationEventListenerMap, EmbeddedPresentationEventMap, EmbeddedPresentationEventName, ExportImageFormat, ExportImageOptions, ExportImageSizeMode, ExportImageViewMode, ExportImageContainMode, ExportViewBounds, ExportPadding, ExportSlideImageOptions, ExportedSlideImage, ExportSlideImageResult, ExportLatexOptions, ExportedLatex, ExportLatexResult, GetContentResult, GetSlideCountResult, HistoryApi, LoadFileResult, LoadShareByIdResult, ModeApi, PresentationModeApi, ReadyEvent, ResourceLibraryItem, ResourceLibraryPageInfo, ResourceLibraryProvider, ResourceLibraryQuery, ResourceLibraryQueryContext, ResourceLibraryResult, ReplResult, SlideChangeEvent, SlideIndexResult, SlidesApi, SwitchSlideResult, SaveEvent, } from './shared';
5
5
  export { EmbeddedPresentation } from './embedded-presentation';
6
6
  export { EmbeddedEditor } from './embedded-editor';
7
7
  export { AlgeoSdk, create, createEditor, createPresentation, type EmbeddedInstance, } from './sdk';
package/dist/shared.d.ts CHANGED
@@ -44,6 +44,42 @@ export interface AlgeoEditorCreateOptions {
44
44
  shareId?: string;
45
45
  initialContent?: FileContentLatest;
46
46
  ui?: AlgeoEditorUiConfig;
47
+ /** 接入方提供的只读图片资源库。 */
48
+ resourceLibrary?: ResourceLibraryProvider;
49
+ }
50
+ export interface ResourceLibraryQuery {
51
+ /** 页码,从 1 开始。 */
52
+ page: number;
53
+ /** 每页数量,最大 100。 */
54
+ pageSize: number;
55
+ keyword?: string;
56
+ mediaTypes?: string[];
57
+ }
58
+ export interface ResourceLibraryQueryContext {
59
+ signal: AbortSignal;
60
+ }
61
+ export interface ResourceLibraryProvider {
62
+ query(params: ResourceLibraryQuery, context: ResourceLibraryQueryContext): Promise<ResourceLibraryResult>;
63
+ }
64
+ export interface ResourceLibraryItem {
65
+ id: string;
66
+ name: string;
67
+ mediaType: string;
68
+ url: string;
69
+ thumbnailUrl?: string;
70
+ width?: number;
71
+ height?: number;
72
+ size?: number;
73
+ }
74
+ export interface ResourceLibraryPageInfo {
75
+ page: number;
76
+ pageSize: number;
77
+ hasNext: boolean;
78
+ total?: number;
79
+ }
80
+ export interface ResourceLibraryResult {
81
+ items: ResourceLibraryItem[];
82
+ pageInfo: ResourceLibraryPageInfo;
47
83
  }
48
84
  export interface AlgeoPresentationCreateOptions {
49
85
  auth?: AlgeoEditorAuthOptions;
@@ -120,6 +156,15 @@ export interface AiRequestMessage {
120
156
  requestId: string;
121
157
  payload: AiRunPayloadV1;
122
158
  }
159
+ export interface ResourceLibraryQueryMessage {
160
+ type: 'resourceLibraryQuery';
161
+ requestId: string;
162
+ params: ResourceLibraryQuery;
163
+ }
164
+ export interface ResourceLibraryCancelMessage {
165
+ type: 'resourceLibraryCancel';
166
+ requestId: string;
167
+ }
123
168
  export interface EmbeddedEditorEventMap {
124
169
  ready: ReadyEvent;
125
170
  contentChange: ContentChangeEvent;
@@ -256,6 +301,16 @@ export interface ExportLatexOptions {
256
301
  export interface ExportedLatex {
257
302
  index: number;
258
303
  code: string;
304
+ /** TikZ 通过 path 引用的图片资源;调用方应保持相对目录结构落盘。 */
305
+ assets: Array<{
306
+ path: string;
307
+ blob: Blob;
308
+ mediaType: string;
309
+ }>;
310
+ /** 可直接下载的导出文件;包含图片时为 ZIP,否则为 TeX。 */
311
+ blob: Blob;
312
+ fileName: string;
313
+ format: 'tex' | 'zip';
259
314
  }
260
315
  export interface ExportLatexResult {
261
316
  items: ExportedLatex[];
@@ -321,9 +376,10 @@ export declare function generateRequestId(): string;
321
376
  export declare function isResponseMessage(msg: unknown): msg is EmbedResponseMessage;
322
377
  export declare function isReadyMessage(msg: unknown): msg is EmbedReadyMessage;
323
378
  export type EmbedEventMessage = ContentChangeEvent | SlideChangeEvent | SaveSuccessEvent | AiCancelEvent;
324
- export type EmbedRequestMessage = SaveRequestMessage | AiRequestMessage;
379
+ export type EmbedRequestMessage = SaveRequestMessage | AiRequestMessage | ResourceLibraryQueryMessage | ResourceLibraryCancelMessage;
325
380
  export declare function isSaveRequestMessage(msg: unknown): msg is SaveRequestMessage;
326
381
  export declare function isAiRequestMessage(msg: unknown): msg is AiRequestMessage;
382
+ export declare function isResourceLibraryRequestMessage(msg: unknown): msg is ResourceLibraryQueryMessage | ResourceLibraryCancelMessage;
327
383
  export declare function isEmbedEventMessage(msg: unknown): msg is EmbedEventMessage;
328
384
  export declare function normalizeBaseUrl(baseUrl: string): string;
329
385
  export declare function normalizeMode(mode?: AlgeoEmbedMode): AlgeoEmbedMode;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dajiaoai/algeo-sdk",
3
- "version": "2.10.0",
3
+ "version": "2.11.0",
4
4
  "description": "大角几何内嵌画板 SDK,支持 iframe 挂载与 postMessage 通信",
5
5
  "main": "dist/algeo-sdk.cjs.js",
6
6
  "module": "dist/algeo-sdk.esm.js",