@lota-sdk/core 0.4.22 → 0.4.23

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lota-sdk/core",
3
- "version": "0.4.22",
3
+ "version": "0.4.23",
4
4
  "files": [
5
5
  "src",
6
6
  "infrastructure/schema"
@@ -29,9 +29,10 @@
29
29
  "dependencies": {
30
30
  "@ai-sdk/devtools": "^0.0.16",
31
31
  "@ai-sdk/openai": "^3.0.54",
32
+ "@ai-sdk/provider": "^3.0.9",
32
33
  "@chat-adapter/slack": "^4.26.0",
33
34
  "@chat-adapter/state-ioredis": "^4.26.0",
34
- "@lota-sdk/shared": "0.4.22",
35
+ "@lota-sdk/shared": "0.4.23",
35
36
  "@mendable/firecrawl-js": "^4.20.0",
36
37
  "@surrealdb/node": "^3.0.3",
37
38
  "ai": "^6.0.170",
@@ -1,5 +1,6 @@
1
1
  import { devToolsMiddleware } from '@ai-sdk/devtools'
2
2
  import { createOpenAI } from '@ai-sdk/openai'
3
+ import type { JSONSchema7 } from '@ai-sdk/provider'
3
4
  import { wrapEmbeddingModel, wrapLanguageModel } from 'ai'
4
5
  import type { LanguageModelMiddleware } from 'ai'
5
6
  import { Cause, Clock, Context, Duration, Effect, Fiber, Layer, Semaphore } from 'effect'
@@ -16,6 +17,7 @@ type WrapStreamOptions = Parameters<NonNullable<LanguageModelMiddleware['wrapStr
16
17
  type AiGatewayLanguageModel = Parameters<typeof wrapLanguageModel>[0]['model']
17
18
  type AiGatewayEmbeddingModel = Parameters<typeof wrapEmbeddingModel>[0]['model']
18
19
  type AiGatewayCallOptions = WrapStreamOptions['params']
20
+ type AiGatewayFunctionTool = Extract<NonNullable<AiGatewayCallOptions['tools']>[number], { type: 'function' }>
19
21
  type AiGatewayGenerateResult = Awaited<ReturnType<WrapStreamOptions['doGenerate']>>
20
22
  type AiGatewayStreamResult = Awaited<ReturnType<WrapStreamOptions['doStream']>>
21
23
  type AiGatewayGeneratedContent = AiGatewayGenerateResult['content'][number]
@@ -900,52 +902,55 @@ const JSON_SCHEMA_PROPERTY_MAP_KEYS = new Set([
900
902
  'properties',
901
903
  ])
902
904
 
903
- function stripJsonSchemaFormatKeywords(value: unknown): unknown {
905
+ function removeJsonSchemaFormatKeywords(value: unknown): void {
904
906
  if (Array.isArray(value)) {
905
- return value.map((item) => stripJsonSchemaFormatKeywords(item))
907
+ for (const item of value) {
908
+ removeJsonSchemaFormatKeywords(item)
909
+ }
910
+ return
906
911
  }
907
912
 
908
913
  if (!isRecord(value)) {
909
- return value
914
+ return
910
915
  }
911
916
 
912
- let changed = false
913
- const nextValue: Record<string, unknown> = {}
917
+ delete value.format
914
918
 
915
919
  for (const [key, child] of Object.entries(value)) {
916
- if (key === 'format') {
917
- changed = true
918
- continue
919
- }
920
-
921
920
  if (JSON_SCHEMA_PROPERTY_MAP_KEYS.has(key) && isRecord(child)) {
922
- let mapChanged = false
923
- const nextMap: Record<string, unknown> = {}
924
- for (const [propertyName, propertySchema] of Object.entries(child)) {
925
- const nextPropertySchema = stripJsonSchemaFormatKeywords(propertySchema)
926
- mapChanged ||= nextPropertySchema !== propertySchema
927
- nextMap[propertyName] = nextPropertySchema
921
+ for (const propertySchema of Object.values(child)) {
922
+ removeJsonSchemaFormatKeywords(propertySchema)
928
923
  }
929
- changed ||= mapChanged
930
- nextValue[key] = mapChanged ? nextMap : child
931
924
  continue
932
925
  }
933
926
 
934
- const nextChild = stripJsonSchemaFormatKeywords(child)
935
- changed ||= nextChild !== child
936
- nextValue[key] = nextChild
927
+ removeJsonSchemaFormatKeywords(child)
937
928
  }
929
+ }
930
+
931
+ function cloneAiGatewayJsonSchema(schema: JSONSchema7): JSONSchema7 {
932
+ // eslint-disable-next-line typescript-eslint/no-unsafe-assignment -- structuredClone preserves the imported JSONSchema7 shape.
933
+ const nextSchema: JSONSchema7 = structuredClone(schema)
934
+ removeJsonSchemaFormatKeywords(nextSchema)
935
+ return nextSchema
936
+ }
938
937
 
939
- return changed ? nextValue : value
938
+ function isAiGatewayFunctionTool(
939
+ tool: NonNullable<AiGatewayCallOptions['tools']>[number],
940
+ ): tool is AiGatewayFunctionTool {
941
+ return isRecord(tool) && tool.type === 'function'
940
942
  }
941
943
 
942
944
  export function normalizeAiGatewayJsonSchemas(params: AiGatewayCallOptions): AiGatewayCallOptions {
943
945
  let nextParams = params
944
946
 
945
947
  if (params.responseFormat?.type === 'json' && params.responseFormat.schema) {
946
- const nextSchema = stripJsonSchemaFormatKeywords(params.responseFormat.schema)
947
- if (nextSchema !== params.responseFormat.schema) {
948
- nextParams = { ...nextParams, responseFormat: { ...params.responseFormat, schema: nextSchema } }
948
+ // eslint-disable-next-line typescript-eslint/no-unsafe-assignment -- cloneAiGatewayJsonSchema returns the same JSONSchema7 type.
949
+ const responseSchema: JSONSchema7 = cloneAiGatewayJsonSchema(params.responseFormat.schema)
950
+ nextParams = {
951
+ ...nextParams,
952
+ // eslint-disable-next-line typescript-eslint/no-unsafe-assignment -- responseSchema is JSONSchema7 for responseFormat.schema.
953
+ responseFormat: { ...params.responseFormat, schema: responseSchema },
949
954
  }
950
955
  }
951
956
 
@@ -955,13 +960,14 @@ export function normalizeAiGatewayJsonSchemas(params: AiGatewayCallOptions): AiG
955
960
  }
956
961
 
957
962
  const nextTools = sourceTools.map((tool) => {
958
- if (!isRecord(tool) || tool.type !== 'function') {
963
+ if (!isAiGatewayFunctionTool(tool)) {
959
964
  return tool
960
965
  }
961
966
 
962
- const inputSchema = (tool as { inputSchema: unknown }).inputSchema
963
- const nextInputSchema = stripJsonSchemaFormatKeywords(inputSchema)
964
- return { ...tool, inputSchema: nextInputSchema }
967
+ // eslint-disable-next-line typescript-eslint/no-unsafe-assignment -- function tool inputSchema is JSONSchema7.
968
+ const inputSchema: JSONSchema7 = cloneAiGatewayJsonSchema(tool.inputSchema)
969
+ // eslint-disable-next-line typescript-eslint/no-unsafe-assignment -- inputSchema is JSONSchema7 for function tool inputSchema.
970
+ return { ...tool, inputSchema }
965
971
  })
966
972
 
967
973
  return { ...nextParams, tools: nextTools as AiGatewayCallOptions['tools'] }