gitlab-ai-provider 5.3.3 → 6.0.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/dist/index.mjs CHANGED
@@ -171,7 +171,7 @@ var GitLabDirectAccessClient = class {
171
171
 
172
172
  // src/gitlab-anthropic-language-model.ts
173
173
  var GitLabAnthropicLanguageModel = class {
174
- specificationVersion = "v2";
174
+ specificationVersion = "v3";
175
175
  modelId;
176
176
  supportedUrls = {};
177
177
  config;
@@ -330,10 +330,14 @@ ${message.content}` : message.content;
330
330
  resultContent = part.output.value;
331
331
  } else if (part.output.type === "json") {
332
332
  resultContent = JSON.stringify(part.output.value);
333
+ } else if (part.output.type === "content") {
334
+ resultContent = JSON.stringify(part.output.value);
333
335
  } else if (part.output.type === "error-text") {
334
336
  resultContent = part.output.value;
335
337
  } else if (part.output.type === "error-json") {
336
338
  resultContent = JSON.stringify(part.output.value);
339
+ } else if (part.output.type === "execution-denied") {
340
+ resultContent = part.output.reason ?? "Tool execution denied.";
337
341
  } else {
338
342
  resultContent = JSON.stringify(part.output);
339
343
  }
@@ -356,18 +360,41 @@ ${message.content}` : message.content;
356
360
  * Convert Anthropic finish reason to AI SDK format
357
361
  */
358
362
  convertFinishReason(stopReason) {
359
- switch (stopReason) {
360
- case "end_turn":
361
- return "stop";
362
- case "stop_sequence":
363
- return "stop";
364
- case "max_tokens":
365
- return "length";
366
- case "tool_use":
367
- return "tool-calls";
368
- default:
369
- return "unknown";
370
- }
363
+ const unified = (() => {
364
+ switch (stopReason) {
365
+ case "end_turn":
366
+ return "stop";
367
+ case "stop_sequence":
368
+ return "stop";
369
+ case "max_tokens":
370
+ return "length";
371
+ case "tool_use":
372
+ return "tool-calls";
373
+ default:
374
+ return "other";
375
+ }
376
+ })();
377
+ return { unified, raw: stopReason ?? void 0 };
378
+ }
379
+ createUsage(params) {
380
+ const inputTotal = params?.inputTotal;
381
+ const outputTotal = params?.outputTotal;
382
+ const cacheRead = params?.cacheRead;
383
+ const cacheWrite = params?.cacheWrite;
384
+ return {
385
+ inputTokens: {
386
+ total: inputTotal,
387
+ noCache: inputTotal == null ? void 0 : Math.max(0, inputTotal - (cacheRead ?? 0) - (cacheWrite ?? 0)),
388
+ cacheRead,
389
+ cacheWrite
390
+ },
391
+ outputTokens: {
392
+ total: outputTotal,
393
+ text: outputTotal,
394
+ reasoning: params?.outputReasoning
395
+ },
396
+ raw: params?.raw
397
+ };
371
398
  }
372
399
  async doGenerate(options) {
373
400
  return this.doGenerateWithRetry(options, false);
@@ -407,11 +434,13 @@ ${message.content}` : message.content;
407
434
  });
408
435
  }
409
436
  }
410
- const usage = {
411
- inputTokens: response.usage.input_tokens,
412
- outputTokens: response.usage.output_tokens,
413
- totalTokens: response.usage.input_tokens + response.usage.output_tokens
414
- };
437
+ const rawUsage = response.usage;
438
+ const usage = this.createUsage({
439
+ inputTotal: response.usage.input_tokens,
440
+ outputTotal: response.usage.output_tokens,
441
+ cacheRead: rawUsage.cache_read_input_tokens,
442
+ cacheWrite: rawUsage.cache_creation_input_tokens
443
+ });
415
444
  return {
416
445
  content,
417
446
  finishReason: this.convertFinishReason(response.stop_reason),
@@ -467,12 +496,8 @@ ${message.content}` : message.content;
467
496
  const stream = new ReadableStream({
468
497
  start: async (controller) => {
469
498
  const contentBlocks = {};
470
- const usage = {
471
- inputTokens: 0,
472
- outputTokens: 0,
473
- totalTokens: 0
474
- };
475
- let finishReason = "unknown";
499
+ let usage = self.createUsage();
500
+ let finishReason = { unified: "other", raw: void 0 };
476
501
  try {
477
502
  const anthropicStream = client.messages.stream(requestBody, {
478
503
  signal: options.abortSignal
@@ -487,7 +512,14 @@ ${message.content}` : message.content;
487
512
  switch (event.type) {
488
513
  case "message_start":
489
514
  if (event.message.usage) {
490
- usage.inputTokens = event.message.usage.input_tokens;
515
+ usage = self.createUsage({
516
+ inputTotal: event.message.usage.input_tokens,
517
+ outputTotal: usage.outputTokens.total,
518
+ outputReasoning: usage.outputTokens.reasoning,
519
+ cacheRead: usage.inputTokens.cacheRead,
520
+ cacheWrite: usage.inputTokens.cacheWrite,
521
+ raw: usage.raw
522
+ });
491
523
  }
492
524
  controller.enqueue({
493
525
  type: "response-metadata",
@@ -559,8 +591,14 @@ ${message.content}` : message.content;
559
591
  }
560
592
  case "message_delta":
561
593
  if (event.usage) {
562
- usage.outputTokens = event.usage.output_tokens;
563
- usage.totalTokens = (usage.inputTokens || 0) + event.usage.output_tokens;
594
+ usage = self.createUsage({
595
+ inputTotal: usage.inputTokens.total,
596
+ outputTotal: event.usage.output_tokens,
597
+ outputReasoning: usage.outputTokens.reasoning,
598
+ cacheRead: usage.inputTokens.cacheRead,
599
+ cacheWrite: usage.inputTokens.cacheWrite,
600
+ raw: usage.raw
601
+ });
564
602
  }
565
603
  if (event.delta.stop_reason) {
566
604
  finishReason = self.convertFinishReason(event.delta.stop_reason);
@@ -760,7 +798,7 @@ var MODEL_ID_TO_ANTHROPIC_MODEL = Object.fromEntries(
760
798
 
761
799
  // src/gitlab-openai-language-model.ts
762
800
  var GitLabOpenAILanguageModel = class {
763
- specificationVersion = "v2";
801
+ specificationVersion = "v3";
764
802
  modelId;
765
803
  supportedUrls = {};
766
804
  config;
@@ -906,10 +944,14 @@ var GitLabOpenAILanguageModel = class {
906
944
  resultContent = part.output.value;
907
945
  } else if (part.output.type === "json") {
908
946
  resultContent = JSON.stringify(part.output.value);
947
+ } else if (part.output.type === "content") {
948
+ resultContent = JSON.stringify(part.output.value);
909
949
  } else if (part.output.type === "error-text") {
910
950
  resultContent = part.output.value;
911
951
  } else if (part.output.type === "error-json") {
912
952
  resultContent = JSON.stringify(part.output.value);
953
+ } else if (part.output.type === "execution-denied") {
954
+ resultContent = part.output.reason ?? "Tool execution denied.";
913
955
  } else {
914
956
  resultContent = JSON.stringify(part.output);
915
957
  }
@@ -925,18 +967,40 @@ var GitLabOpenAILanguageModel = class {
925
967
  return messages;
926
968
  }
927
969
  convertFinishReason(finishReason) {
928
- switch (finishReason) {
929
- case "stop":
930
- return "stop";
931
- case "length":
932
- return "length";
933
- case "tool_calls":
934
- return "tool-calls";
935
- case "content_filter":
936
- return "content-filter";
937
- default:
938
- return "unknown";
939
- }
970
+ const unified = (() => {
971
+ switch (finishReason) {
972
+ case "stop":
973
+ return "stop";
974
+ case "length":
975
+ return "length";
976
+ case "tool_calls":
977
+ return "tool-calls";
978
+ case "content_filter":
979
+ return "content-filter";
980
+ default:
981
+ return "other";
982
+ }
983
+ })();
984
+ return { unified, raw: finishReason ?? void 0 };
985
+ }
986
+ createUsage(params) {
987
+ const inputTotal = params?.inputTotal;
988
+ const outputTotal = params?.outputTotal;
989
+ const cacheRead = params?.cacheRead;
990
+ return {
991
+ inputTokens: {
992
+ total: inputTotal,
993
+ noCache: inputTotal == null ? void 0 : Math.max(0, inputTotal - (cacheRead ?? 0)),
994
+ cacheRead,
995
+ cacheWrite: void 0
996
+ },
997
+ outputTokens: {
998
+ total: outputTotal,
999
+ text: outputTotal,
1000
+ reasoning: params?.outputReasoning
1001
+ },
1002
+ raw: params?.raw
1003
+ };
940
1004
  }
941
1005
  /**
942
1006
  * Convert tools to Responses API format
@@ -953,7 +1017,7 @@ var GitLabOpenAILanguageModel = class {
953
1017
  name: tool.name,
954
1018
  description: tool.description || "",
955
1019
  parameters: schema,
956
- strict: false
1020
+ strict: tool.strict
957
1021
  };
958
1022
  });
959
1023
  }
@@ -1004,10 +1068,14 @@ var GitLabOpenAILanguageModel = class {
1004
1068
  resultContent = part.output.value;
1005
1069
  } else if (part.output.type === "json") {
1006
1070
  resultContent = JSON.stringify(part.output.value);
1071
+ } else if (part.output.type === "content") {
1072
+ resultContent = JSON.stringify(part.output.value);
1007
1073
  } else if (part.output.type === "error-text") {
1008
1074
  resultContent = part.output.value;
1009
1075
  } else if (part.output.type === "error-json") {
1010
1076
  resultContent = JSON.stringify(part.output.value);
1077
+ } else if (part.output.type === "execution-denied") {
1078
+ resultContent = part.output.reason ?? "Tool execution denied.";
1011
1079
  } else {
1012
1080
  resultContent = JSON.stringify(part.output);
1013
1081
  }
@@ -1036,20 +1104,23 @@ var GitLabOpenAILanguageModel = class {
1036
1104
  */
1037
1105
  convertResponsesStatus(status, hasToolCalls = false) {
1038
1106
  if (hasToolCalls) {
1039
- return "tool-calls";
1040
- }
1041
- switch (status) {
1042
- case "completed":
1043
- return "stop";
1044
- case "incomplete":
1045
- return "length";
1046
- case "cancelled":
1047
- return "stop";
1048
- case "failed":
1049
- return "error";
1050
- default:
1051
- return "unknown";
1052
- }
1107
+ return { unified: "tool-calls", raw: status };
1108
+ }
1109
+ const unified = (() => {
1110
+ switch (status) {
1111
+ case "completed":
1112
+ return "stop";
1113
+ case "incomplete":
1114
+ return "length";
1115
+ case "cancelled":
1116
+ return "stop";
1117
+ case "failed":
1118
+ return "error";
1119
+ default:
1120
+ return "other";
1121
+ }
1122
+ })();
1123
+ return { unified, raw: status };
1053
1124
  }
1054
1125
  async doGenerate(options) {
1055
1126
  if (this.useResponsesApi) {
@@ -1092,11 +1163,12 @@ var GitLabOpenAILanguageModel = class {
1092
1163
  }
1093
1164
  }
1094
1165
  }
1095
- const usage = {
1096
- inputTokens: response.usage?.prompt_tokens || 0,
1097
- outputTokens: response.usage?.completion_tokens || 0,
1098
- totalTokens: response.usage?.total_tokens || 0
1099
- };
1166
+ const usage = this.createUsage({
1167
+ inputTotal: response.usage?.prompt_tokens,
1168
+ outputTotal: response.usage?.completion_tokens,
1169
+ cacheRead: response.usage?.prompt_tokens_details?.cached_tokens,
1170
+ raw: { total_tokens: response.usage?.total_tokens }
1171
+ });
1100
1172
  return {
1101
1173
  content,
1102
1174
  finishReason: this.convertFinishReason(choice?.finish_reason),
@@ -1163,11 +1235,13 @@ var GitLabOpenAILanguageModel = class {
1163
1235
  });
1164
1236
  }
1165
1237
  }
1166
- const usage = {
1167
- inputTokens: response.usage?.input_tokens || 0,
1168
- outputTokens: response.usage?.output_tokens || 0,
1169
- totalTokens: response.usage?.total_tokens || 0
1170
- };
1238
+ const usage = this.createUsage({
1239
+ inputTotal: response.usage?.input_tokens,
1240
+ outputTotal: response.usage?.output_tokens,
1241
+ outputReasoning: response.usage?.output_tokens_details?.reasoning_tokens,
1242
+ cacheRead: response.usage?.input_tokens_details?.cached_tokens,
1243
+ raw: { total_tokens: response.usage?.total_tokens }
1244
+ });
1171
1245
  return {
1172
1246
  content,
1173
1247
  finishReason: this.convertResponsesStatus(response.status, hasToolCalls),
@@ -1226,12 +1300,8 @@ var GitLabOpenAILanguageModel = class {
1226
1300
  const stream = new ReadableStream({
1227
1301
  start: async (controller) => {
1228
1302
  const toolCalls = {};
1229
- const usage = {
1230
- inputTokens: 0,
1231
- outputTokens: 0,
1232
- totalTokens: 0
1233
- };
1234
- let finishReason = "unknown";
1303
+ let usage = self.createUsage();
1304
+ let finishReason = { unified: "other", raw: void 0 };
1235
1305
  let textStarted = false;
1236
1306
  const textId = "text-0";
1237
1307
  try {
@@ -1289,9 +1359,12 @@ var GitLabOpenAILanguageModel = class {
1289
1359
  finishReason = self.convertFinishReason(choice.finish_reason);
1290
1360
  }
1291
1361
  if (chunk.usage) {
1292
- usage.inputTokens = chunk.usage.prompt_tokens || 0;
1293
- usage.outputTokens = chunk.usage.completion_tokens || 0;
1294
- usage.totalTokens = chunk.usage.total_tokens || 0;
1362
+ usage = self.createUsage({
1363
+ inputTotal: chunk.usage.prompt_tokens,
1364
+ outputTotal: chunk.usage.completion_tokens,
1365
+ cacheRead: chunk.usage.prompt_tokens_details?.cached_tokens,
1366
+ raw: { total_tokens: chunk.usage.total_tokens }
1367
+ });
1295
1368
  }
1296
1369
  }
1297
1370
  if (textStarted) {
@@ -1371,12 +1444,8 @@ var GitLabOpenAILanguageModel = class {
1371
1444
  const stream = new ReadableStream({
1372
1445
  start: async (controller) => {
1373
1446
  const toolCalls = {};
1374
- const usage = {
1375
- inputTokens: 0,
1376
- outputTokens: 0,
1377
- totalTokens: 0
1378
- };
1379
- let finishReason = "unknown";
1447
+ let usage = self.createUsage();
1448
+ let finishReason = { unified: "other", raw: void 0 };
1380
1449
  let textStarted = false;
1381
1450
  const textId = "text-0";
1382
1451
  try {
@@ -1438,9 +1507,13 @@ var GitLabOpenAILanguageModel = class {
1438
1507
  const hasToolCalls2 = Object.keys(toolCalls).length > 0;
1439
1508
  finishReason = self.convertResponsesStatus(event.response.status, hasToolCalls2);
1440
1509
  if (event.response.usage) {
1441
- usage.inputTokens = event.response.usage.input_tokens || 0;
1442
- usage.outputTokens = event.response.usage.output_tokens || 0;
1443
- usage.totalTokens = event.response.usage.total_tokens || 0;
1510
+ usage = self.createUsage({
1511
+ inputTotal: event.response.usage.input_tokens,
1512
+ outputTotal: event.response.usage.output_tokens,
1513
+ outputReasoning: event.response.usage.output_tokens_details?.reasoning_tokens,
1514
+ cacheRead: event.response.usage.input_tokens_details?.cached_tokens,
1515
+ raw: { total_tokens: event.response.usage.total_tokens }
1516
+ });
1444
1517
  }
1445
1518
  }
1446
1519
  }
@@ -1448,8 +1521,8 @@ var GitLabOpenAILanguageModel = class {
1448
1521
  controller.enqueue({ type: "text-end", id: textId });
1449
1522
  }
1450
1523
  const hasToolCalls = Object.keys(toolCalls).length > 0;
1451
- if (hasToolCalls && finishReason === "stop") {
1452
- finishReason = "tool-calls";
1524
+ if (hasToolCalls && finishReason.unified === "stop") {
1525
+ finishReason = { unified: "tool-calls", raw: finishReason.raw };
1453
1526
  }
1454
1527
  for (const tc of Object.values(toolCalls)) {
1455
1528
  controller.enqueue({ type: "tool-input-end", id: tc.callId });
@@ -1509,7 +1582,7 @@ var GitLabOpenAILanguageModel = class {
1509
1582
  import WebSocket from "isomorphic-ws";
1510
1583
 
1511
1584
  // src/version.ts
1512
- var VERSION = true ? "5.3.2" : "0.0.0-dev";
1585
+ var VERSION = true ? "5.3.3" : "0.0.0-dev";
1513
1586
 
1514
1587
  // src/gitlab-workflow-types.ts
1515
1588
  var WorkflowType = /* @__PURE__ */ ((WorkflowType2) => {
@@ -2873,7 +2946,7 @@ function minimalSchema(schemaStr) {
2873
2946
  }
2874
2947
  }
2875
2948
  var GitLabWorkflowLanguageModel = class _GitLabWorkflowLanguageModel {
2876
- specificationVersion = "v2";
2949
+ specificationVersion = "v3";
2877
2950
  modelId;
2878
2951
  supportedUrls = {};
2879
2952
  config;
@@ -3174,16 +3247,34 @@ var GitLabWorkflowLanguageModel = class _GitLabWorkflowLanguageModel {
3174
3247
  get workflowId() {
3175
3248
  return this.currentWorkflowId;
3176
3249
  }
3250
+ createUsage(params) {
3251
+ return {
3252
+ inputTokens: {
3253
+ total: params?.inputTotal,
3254
+ noCache: params?.inputTotal,
3255
+ cacheRead: void 0,
3256
+ cacheWrite: void 0
3257
+ },
3258
+ outputTokens: {
3259
+ total: params?.outputTotal,
3260
+ text: params?.outputTotal,
3261
+ reasoning: void 0
3262
+ }
3263
+ };
3264
+ }
3265
+ createFinishReason(unified, raw) {
3266
+ return { unified, raw };
3267
+ }
3177
3268
  // ---------------------------------------------------------------------------
3178
- // LanguageModelV2 — doGenerate (non-streaming)
3269
+ // LanguageModelV3 — doGenerate (non-streaming)
3179
3270
  // ---------------------------------------------------------------------------
3180
3271
  async doGenerate(options) {
3181
3272
  const { stream } = await this.doStream(options);
3182
3273
  const reader = stream.getReader();
3183
3274
  const textParts = [];
3184
3275
  const toolCalls = [];
3185
- let finishReason = "unknown";
3186
- const usage = { inputTokens: 0, outputTokens: 0, totalTokens: 0 };
3276
+ let finishReason = { unified: "other", raw: void 0 };
3277
+ let usage = this.createUsage();
3187
3278
  try {
3188
3279
  while (true) {
3189
3280
  const { done, value } = await reader.read();
@@ -3203,9 +3294,7 @@ var GitLabWorkflowLanguageModel = class _GitLabWorkflowLanguageModel {
3203
3294
  case "finish":
3204
3295
  finishReason = value.finishReason;
3205
3296
  if (value.usage) {
3206
- usage.inputTokens = value.usage.inputTokens ?? 0;
3207
- usage.outputTokens = value.usage.outputTokens ?? 0;
3208
- usage.totalTokens = value.usage.totalTokens ?? 0;
3297
+ usage = value.usage;
3209
3298
  }
3210
3299
  break;
3211
3300
  case "error":
@@ -3224,7 +3313,7 @@ var GitLabWorkflowLanguageModel = class _GitLabWorkflowLanguageModel {
3224
3313
  return { content, finishReason, usage, warnings: [] };
3225
3314
  }
3226
3315
  // ---------------------------------------------------------------------------
3227
- // LanguageModelV2 — doStream (streaming)
3316
+ // LanguageModelV3 — doStream (streaming)
3228
3317
  // ---------------------------------------------------------------------------
3229
3318
  async doStream(options) {
3230
3319
  const goal = this.extractGoalFromPrompt(options.prompt);
@@ -3490,8 +3579,8 @@ var GitLabWorkflowLanguageModel = class _GitLabWorkflowLanguageModel {
3490
3579
  const outputTokens = Math.ceil(ss.streamedOutputChars / 4);
3491
3580
  controller.enqueue({
3492
3581
  type: "finish",
3493
- finishReason: "stop",
3494
- usage: { inputTokens, outputTokens, totalTokens: inputTokens + outputTokens }
3582
+ finishReason: this.createFinishReason("stop", "completed"),
3583
+ usage: this.createUsage({ inputTotal: inputTokens, outputTotal: outputTokens })
3495
3584
  });
3496
3585
  ss.streamClosed = true;
3497
3586
  controller.close();
@@ -3548,8 +3637,8 @@ var GitLabWorkflowLanguageModel = class _GitLabWorkflowLanguageModel {
3548
3637
  const outTok = Math.ceil(ss.streamedOutputChars / 4);
3549
3638
  controller.enqueue({
3550
3639
  type: "finish",
3551
- finishReason: "stop",
3552
- usage: { inputTokens: inTok, outputTokens: outTok, totalTokens: inTok + outTok }
3640
+ finishReason: this.createFinishReason("stop", "closed"),
3641
+ usage: this.createUsage({ inputTotal: inTok, outputTotal: outTok })
3553
3642
  });
3554
3643
  ss.streamClosed = true;
3555
3644
  controller.close();
@@ -3666,8 +3755,7 @@ var GitLabWorkflowLanguageModel = class _GitLabWorkflowLanguageModel {
3666
3755
  toolCallId: requestID,
3667
3756
  toolName,
3668
3757
  result: errorText,
3669
- isError: true,
3670
- providerExecuted: true
3758
+ isError: true
3671
3759
  });
3672
3760
  } else {
3673
3761
  safeEnqueue({
@@ -3679,8 +3767,7 @@ var GitLabWorkflowLanguageModel = class _GitLabWorkflowLanguageModel {
3679
3767
  title: toolTitle,
3680
3768
  metadata: toolMetadata
3681
3769
  },
3682
- isError: false,
3683
- providerExecuted: true
3770
+ isError: false
3684
3771
  });
3685
3772
  }
3686
3773
  } else {
@@ -3691,8 +3778,7 @@ var GitLabWorkflowLanguageModel = class _GitLabWorkflowLanguageModel {
3691
3778
  toolCallId: requestID,
3692
3779
  toolName,
3693
3780
  result: errorMsg,
3694
- isError: true,
3695
- providerExecuted: true
3781
+ isError: true
3696
3782
  });
3697
3783
  }
3698
3784
  } catch (error) {
@@ -3704,8 +3790,7 @@ var GitLabWorkflowLanguageModel = class _GitLabWorkflowLanguageModel {
3704
3790
  toolCallId: requestID,
3705
3791
  toolName,
3706
3792
  result: errorMsg,
3707
- isError: true,
3708
- providerExecuted: true
3793
+ isError: true
3709
3794
  });
3710
3795
  } finally {
3711
3796
  ss.pendingToolCount--;
@@ -4304,13 +4389,13 @@ function createGitLab(options = {}) {
4304
4389
  return createAgenticChatModel(modelId);
4305
4390
  };
4306
4391
  const provider = Object.assign((modelId) => createDefaultModel(modelId), {
4307
- specificationVersion: "v2",
4392
+ specificationVersion: "v3",
4308
4393
  languageModel: createDefaultModel,
4309
4394
  chat: createDefaultModel,
4310
4395
  agenticChat: createAgenticChatModel,
4311
4396
  workflowChat: createWorkflowChatModel
4312
4397
  });
4313
- provider.textEmbeddingModel = (modelId) => {
4398
+ provider.embeddingModel = (modelId) => {
4314
4399
  throw new GitLabError({
4315
4400
  message: `GitLab provider does not support text embedding models. Model ID: ${modelId}`
4316
4401
  });