@ai-sdk/cohere
Advanced tools
Comparing version 0.0.25 to 0.0.27
@@ -32,3 +32,3 @@ "use strict"; | ||
// src/cohere-chat-language-model.ts | ||
var import_provider2 = require("@ai-sdk/provider"); | ||
var import_provider3 = require("@ai-sdk/provider"); | ||
var import_provider_utils2 = require("@ai-sdk/provider-utils"); | ||
@@ -155,2 +155,89 @@ var import_zod2 = require("zod"); | ||
// src/cohere-prepare-tools.ts | ||
var import_provider2 = require("@ai-sdk/provider"); | ||
function prepareTools(mode) { | ||
var _a; | ||
const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0; | ||
const toolWarnings = []; | ||
if (tools == null) { | ||
return { tools: void 0, force_single_step: void 0, toolWarnings }; | ||
} | ||
const cohereTools = []; | ||
for (const tool of tools) { | ||
if (tool.type === "provider-defined") { | ||
toolWarnings.push({ type: "unsupported-tool", tool }); | ||
} else { | ||
const { properties, required } = tool.parameters; | ||
const parameterDefinitions = {}; | ||
if (properties) { | ||
for (const [key, value] of Object.entries(properties)) { | ||
if (typeof value === "object" && value !== null) { | ||
const { type: JSONType, description } = value; | ||
let type2; | ||
if (typeof JSONType === "string") { | ||
switch (JSONType) { | ||
case "string": | ||
type2 = "str"; | ||
break; | ||
case "number": | ||
type2 = "float"; | ||
break; | ||
case "integer": | ||
type2 = "int"; | ||
break; | ||
case "boolean": | ||
type2 = "bool"; | ||
break; | ||
default: | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
functionality: `Unsupported tool parameter type: ${JSONType}` | ||
}); | ||
} | ||
} else { | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
functionality: `Unsupported tool parameter type: ${JSONType}` | ||
}); | ||
} | ||
parameterDefinitions[key] = { | ||
required: required ? required.includes(key) : false, | ||
type: type2, | ||
description | ||
}; | ||
} | ||
} | ||
} | ||
cohereTools.push({ | ||
name: tool.name, | ||
description: tool.description, | ||
parameterDefinitions | ||
}); | ||
} | ||
} | ||
const toolChoice = mode.toolChoice; | ||
if (toolChoice == null) { | ||
return { tools: cohereTools, force_single_step: false, toolWarnings }; | ||
} | ||
const type = toolChoice.type; | ||
switch (type) { | ||
case "auto": | ||
return { tools: cohereTools, force_single_step: false, toolWarnings }; | ||
case "required": | ||
return { tools: cohereTools, force_single_step: true, toolWarnings }; | ||
case "none": | ||
return { tools: void 0, force_single_step: false, toolWarnings }; | ||
case "tool": | ||
return { | ||
tools: cohereTools.filter((tool) => tool.name === toolChoice.toolName), | ||
force_single_step: true, | ||
toolWarnings | ||
}; | ||
default: { | ||
const _exhaustiveCheck = type; | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
functionality: `Unsupported tool choice type: ${_exhaustiveCheck}` | ||
}); | ||
} | ||
} | ||
} | ||
// src/cohere-chat-language-model.ts | ||
@@ -208,6 +295,12 @@ var CohereChatLanguageModel = class { | ||
case "regular": { | ||
return { ...baseArgs, ...prepareToolsAndToolChoice(mode) }; | ||
const { tools, force_single_step, toolWarnings } = prepareTools(mode); | ||
return { | ||
...baseArgs, | ||
tools, | ||
force_single_step, | ||
warnings: toolWarnings | ||
}; | ||
} | ||
case "object-json": { | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
throw new import_provider3.UnsupportedFunctionalityError({ | ||
functionality: "object-json mode" | ||
@@ -217,3 +310,3 @@ }); | ||
case "object-tool": { | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
throw new import_provider3.UnsupportedFunctionalityError({ | ||
functionality: "object-tool mode" | ||
@@ -230,3 +323,3 @@ }); | ||
var _a; | ||
const args = this.getArgs(options); | ||
const { warnings, ...args } = this.getArgs(options); | ||
const { responseHeaders, value: response } = await (0, import_provider_utils2.postJsonToApi)({ | ||
@@ -269,14 +362,13 @@ url: `${this.config.baseURL}/chat`, | ||
rawResponse: { headers: responseHeaders }, | ||
warnings: void 0 | ||
warnings, | ||
request: { body: JSON.stringify(args) } | ||
}; | ||
} | ||
async doStream(options) { | ||
const args = this.getArgs(options); | ||
const { warnings, ...args } = this.getArgs(options); | ||
const body = { ...args, stream: true }; | ||
const { responseHeaders, value: response } = await (0, import_provider_utils2.postJsonToApi)({ | ||
url: `${this.config.baseURL}/chat`, | ||
headers: (0, import_provider_utils2.combineHeaders)(this.config.headers(), options.headers), | ||
body: { | ||
...args, | ||
stream: true | ||
}, | ||
body, | ||
failedResponseHandler: cohereFailedResponseHandler, | ||
@@ -398,3 +490,4 @@ successfulResponseHandler: (0, import_provider_utils2.createJsonStreamResponseHandler)( | ||
rawResponse: { headers: responseHeaders }, | ||
warnings: [] | ||
warnings, | ||
request: { body: JSON.stringify(body) } | ||
}; | ||
@@ -469,82 +562,5 @@ } | ||
]); | ||
function prepareToolsAndToolChoice(mode) { | ||
var _a; | ||
const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0; | ||
if (tools == null) { | ||
return { tools: void 0 }; | ||
} | ||
const mappedTools = tools.map((tool) => { | ||
const { properties, required } = tool.parameters; | ||
const parameterDefinitions = {}; | ||
if (properties) { | ||
for (const [key, value] of Object.entries(properties)) { | ||
if (typeof value === "object" && value !== null) { | ||
const { type: JSONType, description } = value; | ||
let type2; | ||
if (typeof JSONType === "string") { | ||
switch (JSONType) { | ||
case "string": | ||
type2 = "str"; | ||
break; | ||
case "number": | ||
type2 = "float"; | ||
break; | ||
case "integer": | ||
type2 = "int"; | ||
break; | ||
case "boolean": | ||
type2 = "bool"; | ||
break; | ||
default: | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
functionality: "tool call parameter of non-primitive type" | ||
}); | ||
} | ||
} else { | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
functionality: "tool call parameter of non-primitive type" | ||
}); | ||
} | ||
parameterDefinitions[key] = { | ||
required: required ? required.includes(key) : false, | ||
type: type2, | ||
description | ||
}; | ||
} | ||
} | ||
} | ||
return { | ||
name: tool.name, | ||
description: tool.description, | ||
parameterDefinitions | ||
}; | ||
}); | ||
const toolChoice = mode.toolChoice; | ||
if (toolChoice == null) { | ||
return { tools: mappedTools, force_single_step: false }; | ||
} | ||
const type = toolChoice.type; | ||
switch (type) { | ||
case "auto": | ||
return { tools: mappedTools, force_single_step: false }; | ||
case "required": | ||
return { tools: mappedTools, force_single_step: true }; | ||
case "none": | ||
return { tools: void 0, force_single_step: false }; | ||
case "tool": | ||
return { | ||
tools: mappedTools.filter((tool) => tool.name === toolChoice.toolName), | ||
force_single_step: true | ||
}; | ||
default: { | ||
const _exhaustiveCheck = type; | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
functionality: `Unsupported tool choice type: ${_exhaustiveCheck}` | ||
}); | ||
} | ||
} | ||
} | ||
// src/cohere-embedding-model.ts | ||
var import_provider3 = require("@ai-sdk/provider"); | ||
var import_provider4 = require("@ai-sdk/provider"); | ||
var import_provider_utils3 = require("@ai-sdk/provider-utils"); | ||
@@ -571,3 +587,3 @@ var import_zod3 = require("zod"); | ||
if (values.length > this.maxEmbeddingsPerCall) { | ||
throw new import_provider3.TooManyEmbeddingValuesForCallError({ | ||
throw new import_provider4.TooManyEmbeddingValuesForCallError({ | ||
provider: this.provider, | ||
@@ -574,0 +590,0 @@ modelId: this.modelId, |
# @ai-sdk/cohere | ||
## 0.0.27 | ||
### Patch Changes | ||
- 3b1b69a: feat: provider-defined tools | ||
- Updated dependencies [aa98cdb] | ||
- Updated dependencies [1486128] | ||
- Updated dependencies [7b937c5] | ||
- Updated dependencies [3b1b69a] | ||
- Updated dependencies [811a317] | ||
- @ai-sdk/provider-utils@1.0.22 | ||
- @ai-sdk/provider@0.0.26 | ||
## 0.0.26 | ||
### Patch Changes | ||
- b9b0d7b: feat (ai): access raw request body | ||
- Updated dependencies [b9b0d7b] | ||
- @ai-sdk/provider@0.0.25 | ||
- @ai-sdk/provider-utils@1.0.21 | ||
## 0.0.25 | ||
@@ -4,0 +26,0 @@ |
@@ -32,3 +32,3 @@ "use strict"; | ||
// src/cohere-chat-language-model.ts | ||
var import_provider2 = require("@ai-sdk/provider"); | ||
var import_provider3 = require("@ai-sdk/provider"); | ||
var import_provider_utils2 = require("@ai-sdk/provider-utils"); | ||
@@ -155,2 +155,89 @@ var import_zod2 = require("zod"); | ||
// src/cohere-prepare-tools.ts | ||
var import_provider2 = require("@ai-sdk/provider"); | ||
function prepareTools(mode) { | ||
var _a; | ||
const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0; | ||
const toolWarnings = []; | ||
if (tools == null) { | ||
return { tools: void 0, force_single_step: void 0, toolWarnings }; | ||
} | ||
const cohereTools = []; | ||
for (const tool of tools) { | ||
if (tool.type === "provider-defined") { | ||
toolWarnings.push({ type: "unsupported-tool", tool }); | ||
} else { | ||
const { properties, required } = tool.parameters; | ||
const parameterDefinitions = {}; | ||
if (properties) { | ||
for (const [key, value] of Object.entries(properties)) { | ||
if (typeof value === "object" && value !== null) { | ||
const { type: JSONType, description } = value; | ||
let type2; | ||
if (typeof JSONType === "string") { | ||
switch (JSONType) { | ||
case "string": | ||
type2 = "str"; | ||
break; | ||
case "number": | ||
type2 = "float"; | ||
break; | ||
case "integer": | ||
type2 = "int"; | ||
break; | ||
case "boolean": | ||
type2 = "bool"; | ||
break; | ||
default: | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
functionality: `Unsupported tool parameter type: ${JSONType}` | ||
}); | ||
} | ||
} else { | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
functionality: `Unsupported tool parameter type: ${JSONType}` | ||
}); | ||
} | ||
parameterDefinitions[key] = { | ||
required: required ? required.includes(key) : false, | ||
type: type2, | ||
description | ||
}; | ||
} | ||
} | ||
} | ||
cohereTools.push({ | ||
name: tool.name, | ||
description: tool.description, | ||
parameterDefinitions | ||
}); | ||
} | ||
} | ||
const toolChoice = mode.toolChoice; | ||
if (toolChoice == null) { | ||
return { tools: cohereTools, force_single_step: false, toolWarnings }; | ||
} | ||
const type = toolChoice.type; | ||
switch (type) { | ||
case "auto": | ||
return { tools: cohereTools, force_single_step: false, toolWarnings }; | ||
case "required": | ||
return { tools: cohereTools, force_single_step: true, toolWarnings }; | ||
case "none": | ||
return { tools: void 0, force_single_step: false, toolWarnings }; | ||
case "tool": | ||
return { | ||
tools: cohereTools.filter((tool) => tool.name === toolChoice.toolName), | ||
force_single_step: true, | ||
toolWarnings | ||
}; | ||
default: { | ||
const _exhaustiveCheck = type; | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
functionality: `Unsupported tool choice type: ${_exhaustiveCheck}` | ||
}); | ||
} | ||
} | ||
} | ||
// src/cohere-chat-language-model.ts | ||
@@ -208,6 +295,12 @@ var CohereChatLanguageModel = class { | ||
case "regular": { | ||
return { ...baseArgs, ...prepareToolsAndToolChoice(mode) }; | ||
const { tools, force_single_step, toolWarnings } = prepareTools(mode); | ||
return { | ||
...baseArgs, | ||
tools, | ||
force_single_step, | ||
warnings: toolWarnings | ||
}; | ||
} | ||
case "object-json": { | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
throw new import_provider3.UnsupportedFunctionalityError({ | ||
functionality: "object-json mode" | ||
@@ -217,3 +310,3 @@ }); | ||
case "object-tool": { | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
throw new import_provider3.UnsupportedFunctionalityError({ | ||
functionality: "object-tool mode" | ||
@@ -230,3 +323,3 @@ }); | ||
var _a; | ||
const args = this.getArgs(options); | ||
const { warnings, ...args } = this.getArgs(options); | ||
const { responseHeaders, value: response } = await (0, import_provider_utils2.postJsonToApi)({ | ||
@@ -269,14 +362,13 @@ url: `${this.config.baseURL}/chat`, | ||
rawResponse: { headers: responseHeaders }, | ||
warnings: void 0 | ||
warnings, | ||
request: { body: JSON.stringify(args) } | ||
}; | ||
} | ||
async doStream(options) { | ||
const args = this.getArgs(options); | ||
const { warnings, ...args } = this.getArgs(options); | ||
const body = { ...args, stream: true }; | ||
const { responseHeaders, value: response } = await (0, import_provider_utils2.postJsonToApi)({ | ||
url: `${this.config.baseURL}/chat`, | ||
headers: (0, import_provider_utils2.combineHeaders)(this.config.headers(), options.headers), | ||
body: { | ||
...args, | ||
stream: true | ||
}, | ||
body, | ||
failedResponseHandler: cohereFailedResponseHandler, | ||
@@ -398,3 +490,4 @@ successfulResponseHandler: (0, import_provider_utils2.createJsonStreamResponseHandler)( | ||
rawResponse: { headers: responseHeaders }, | ||
warnings: [] | ||
warnings, | ||
request: { body: JSON.stringify(body) } | ||
}; | ||
@@ -469,82 +562,5 @@ } | ||
]); | ||
function prepareToolsAndToolChoice(mode) { | ||
var _a; | ||
const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0; | ||
if (tools == null) { | ||
return { tools: void 0 }; | ||
} | ||
const mappedTools = tools.map((tool) => { | ||
const { properties, required } = tool.parameters; | ||
const parameterDefinitions = {}; | ||
if (properties) { | ||
for (const [key, value] of Object.entries(properties)) { | ||
if (typeof value === "object" && value !== null) { | ||
const { type: JSONType, description } = value; | ||
let type2; | ||
if (typeof JSONType === "string") { | ||
switch (JSONType) { | ||
case "string": | ||
type2 = "str"; | ||
break; | ||
case "number": | ||
type2 = "float"; | ||
break; | ||
case "integer": | ||
type2 = "int"; | ||
break; | ||
case "boolean": | ||
type2 = "bool"; | ||
break; | ||
default: | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
functionality: "tool call parameter of non-primitive type" | ||
}); | ||
} | ||
} else { | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
functionality: "tool call parameter of non-primitive type" | ||
}); | ||
} | ||
parameterDefinitions[key] = { | ||
required: required ? required.includes(key) : false, | ||
type: type2, | ||
description | ||
}; | ||
} | ||
} | ||
} | ||
return { | ||
name: tool.name, | ||
description: tool.description, | ||
parameterDefinitions | ||
}; | ||
}); | ||
const toolChoice = mode.toolChoice; | ||
if (toolChoice == null) { | ||
return { tools: mappedTools, force_single_step: false }; | ||
} | ||
const type = toolChoice.type; | ||
switch (type) { | ||
case "auto": | ||
return { tools: mappedTools, force_single_step: false }; | ||
case "required": | ||
return { tools: mappedTools, force_single_step: true }; | ||
case "none": | ||
return { tools: void 0, force_single_step: false }; | ||
case "tool": | ||
return { | ||
tools: mappedTools.filter((tool) => tool.name === toolChoice.toolName), | ||
force_single_step: true | ||
}; | ||
default: { | ||
const _exhaustiveCheck = type; | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
functionality: `Unsupported tool choice type: ${_exhaustiveCheck}` | ||
}); | ||
} | ||
} | ||
} | ||
// src/cohere-embedding-model.ts | ||
var import_provider3 = require("@ai-sdk/provider"); | ||
var import_provider4 = require("@ai-sdk/provider"); | ||
var import_provider_utils3 = require("@ai-sdk/provider-utils"); | ||
@@ -571,3 +587,3 @@ var import_zod3 = require("zod"); | ||
if (values.length > this.maxEmbeddingsPerCall) { | ||
throw new import_provider3.TooManyEmbeddingValuesForCallError({ | ||
throw new import_provider4.TooManyEmbeddingValuesForCallError({ | ||
provider: this.provider, | ||
@@ -574,0 +590,0 @@ modelId: this.modelId, |
{ | ||
"name": "@ai-sdk/cohere", | ||
"version": "0.0.25", | ||
"version": "0.0.27", | ||
"license": "Apache-2.0", | ||
@@ -22,4 +22,4 @@ "sideEffects": false, | ||
"dependencies": { | ||
"@ai-sdk/provider": "0.0.24", | ||
"@ai-sdk/provider-utils": "1.0.20" | ||
"@ai-sdk/provider": "0.0.26", | ||
"@ai-sdk/provider-utils": "1.0.22" | ||
}, | ||
@@ -26,0 +26,0 @@ "devDependencies": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
156080
2017
+ Added@ai-sdk/provider@0.0.26(transitive)
+ Added@ai-sdk/provider-utils@1.0.22(transitive)
+ Addednanoid@3.3.8(transitive)
- Removed@ai-sdk/provider@0.0.24(transitive)
- Removed@ai-sdk/provider-utils@1.0.20(transitive)
- Removednanoid@3.3.6(transitive)
Updated@ai-sdk/provider@0.0.26