ai*
This commit is contained in:
88
ai_fields/static/src/model/relational_model/record.js
Normal file
88
ai_fields/static/src/model/relational_model/record.js
Normal file
@@ -0,0 +1,88 @@
|
||||
import { _t } from "@web/core/l10n/translation";
|
||||
import { x2ManyCommands } from "@web/core/orm_plugin";
|
||||
import { patch } from "@web/core/utils/patch";
|
||||
import { Record } from "@web/model/relational_model/record";
|
||||
import { parseServerValue } from "@web/model/relational_model/utils";
|
||||
import { markup } from "@odoo/owl";
|
||||
|
||||
const notificationTitles = [
|
||||
_t("I couldn't quite make sense of that"),
|
||||
_t("Hmm... that was a bit fuzzy for me"),
|
||||
_t("Missing a few pieces of the puzzle"),
|
||||
_t("I gave it a shot, but came up empty"),
|
||||
_t("My circuits didn't quite get it"),
|
||||
_t("I couldn't connect the dots"),
|
||||
_t("That was a bit over my head"),
|
||||
_t("Felt like a riddle I couldn’t solve"),
|
||||
];
|
||||
|
||||
function getRandomToasterTitle() {
|
||||
const index = Math.floor(Math.random() * notificationTitles.length);
|
||||
return notificationTitles[index];
|
||||
}
|
||||
|
||||
patch(Record.prototype, {
|
||||
computeAiField(fieldName) {
|
||||
return this.model.mutex.exec(() => this._computeAiField(fieldName));
|
||||
},
|
||||
|
||||
computeAiProperty(fullName) {
|
||||
return this.model.mutex.exec(() => this._computeAiProperty(fullName));
|
||||
},
|
||||
|
||||
async _computeAiField(fieldName) {
|
||||
const field = this.fields[fieldName];
|
||||
if (!field?.ai) {
|
||||
throw new Error("Cannot compute a non-AI field using AI");
|
||||
}
|
||||
let value;
|
||||
try {
|
||||
value = await this.model.orm.call(this.resModel, "get_ai_field_value", [
|
||||
this.resId || [],
|
||||
fieldName,
|
||||
this._getChanges(),
|
||||
]);
|
||||
} catch (e) {
|
||||
if (e.exceptionName === "odoo.addons.ai.utils.ai_fields_tools.UnresolvedQuery") {
|
||||
this.model.notification.add(e.data.message, {
|
||||
autocloseDelay: 7000,
|
||||
title: `🤖 ${getRandomToasterTitle()}`,
|
||||
type: "warning",
|
||||
});
|
||||
value = field.type === "many2many" && [x2ManyCommands.set([])];
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
await this._update({ [fieldName]: parseServerValue(field, value) });
|
||||
},
|
||||
|
||||
async _computeAiProperty(fullName) {
|
||||
const property = this.fields[fullName];
|
||||
if (!property?.ai) {
|
||||
throw new Error("Cannot compute a non-AI property using AI");
|
||||
}
|
||||
let value;
|
||||
try {
|
||||
value = await this.model.orm.call(this.resModel, "get_ai_property_value", [
|
||||
this.resId || [],
|
||||
fullName,
|
||||
this._getChanges(),
|
||||
]);
|
||||
} catch (e) {
|
||||
if (e.exceptionName === "odoo.addons.ai.utils.ai_fields_tools.UnresolvedQuery") {
|
||||
this.model.notification.add(e.data.message, {
|
||||
autocloseDelay: 7000,
|
||||
title: `🤖 ${getRandomToasterTitle()}`,
|
||||
type: "warning",
|
||||
});
|
||||
return false;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
if (property.type === "html") {
|
||||
return markup(value);
|
||||
}
|
||||
return value;
|
||||
},
|
||||
});
|
||||
207
ai_fields/static/src/views/fields/ai_fields/ai_fields.js
Normal file
207
ai_fields/static/src/views/fields/ai_fields/ai_fields.js
Normal file
@@ -0,0 +1,207 @@
|
||||
import { HtmlField, htmlField } from "@html_editor/fields/html_field";
|
||||
import { _t } from "@web/core/l10n/translation";
|
||||
import { registry } from "@web/core/registry";
|
||||
import { useService } from "@web/core/utils/hooks";
|
||||
import { BooleanField, booleanField } from "@web/views/fields/boolean/boolean_field";
|
||||
import { CharField, charField } from "@web/views/fields/char/char_field";
|
||||
import { DateTimeField, dateField, dateTimeField } from "@web/views/fields/datetime/datetime_field";
|
||||
import { FloatField, floatField } from "@web/views/fields/float/float_field";
|
||||
import { IntegerField, integerField } from "@web/views/fields/integer/integer_field";
|
||||
import {
|
||||
Many2ManyTagsAvatarField,
|
||||
many2ManyTagsAvatarField,
|
||||
} from "@web/views/fields/many2many_tags_avatar/many2many_tags_avatar_field";
|
||||
import {
|
||||
Many2ManyTagsField,
|
||||
many2ManyTagsField,
|
||||
} from "@web/views/fields/many2many_tags/many2many_tags_field";
|
||||
import {
|
||||
Many2OneAvatarField,
|
||||
many2OneAvatarField,
|
||||
} from "@web/views/fields/many2one_avatar/many2one_avatar_field";
|
||||
import { buildM2OFieldDescription, Many2OneField } from "@web/views/fields/many2one/many2one_field";
|
||||
import { MonetaryField, monetaryField } from "@web/views/fields/monetary/monetary_field";
|
||||
import { SelectionField, selectionField } from "@web/views/fields/selection/selection_field";
|
||||
import { TextField, textField } from "@web/views/fields/text/text_field";
|
||||
|
||||
const AiFieldMixin = (fieldClass) => {
|
||||
return class extends fieldClass {
|
||||
setup() {
|
||||
super.setup();
|
||||
this.notification = useService("notification");
|
||||
}
|
||||
|
||||
get isAiComputed() {
|
||||
return this.props.record.fields[this.props.name].ai;
|
||||
}
|
||||
|
||||
async onAiClick() {
|
||||
await this.props.record.computeAiField(this.props.name);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
class AiBooleanField extends AiFieldMixin(BooleanField) {
|
||||
static template = "ai_fields.AiBooleanField";
|
||||
}
|
||||
|
||||
const aiBooleanField = {
|
||||
...booleanField,
|
||||
component: AiBooleanField,
|
||||
displayName: _t("AI Checkbox"),
|
||||
};
|
||||
|
||||
registry.category("fields").add("ai_boolean", aiBooleanField);
|
||||
|
||||
class AiCharField extends AiFieldMixin(CharField) {
|
||||
static template = "ai_fields.AiCharField";
|
||||
}
|
||||
|
||||
const aiCharField = {
|
||||
...charField,
|
||||
component: AiCharField,
|
||||
displayName: _t("AI Text"),
|
||||
};
|
||||
|
||||
registry.category("fields").add("ai_char", aiCharField);
|
||||
|
||||
class AiDateTimeField extends AiFieldMixin(DateTimeField) {
|
||||
static template = "ai_fields.AiDateTimeField";
|
||||
}
|
||||
|
||||
const aiDateField = {
|
||||
...dateField,
|
||||
component: AiDateTimeField,
|
||||
displayName: _t("AI Date"),
|
||||
};
|
||||
|
||||
registry.category("fields").add("ai_date", aiDateField);
|
||||
|
||||
const aiDateTimeField = {
|
||||
...dateTimeField,
|
||||
component: AiDateTimeField,
|
||||
displayName: _t("AI Date & Time"),
|
||||
};
|
||||
|
||||
registry.category("fields").add("ai_datetime", aiDateTimeField);
|
||||
|
||||
class AiFloatField extends AiFieldMixin(FloatField) {
|
||||
static template = "ai_fields.AiFloatField";
|
||||
}
|
||||
|
||||
const aiFloatField = {
|
||||
...floatField,
|
||||
component: AiFloatField,
|
||||
displayName: _t("AI Float"),
|
||||
};
|
||||
|
||||
registry.category("fields").add("ai_float", aiFloatField);
|
||||
|
||||
class AiHtmlField extends AiFieldMixin(HtmlField) {
|
||||
static template = "ai_fields.AiHtmlField";
|
||||
}
|
||||
|
||||
const aiHtmlField = {
|
||||
...htmlField,
|
||||
additionalClasses: ["o_field_html"],
|
||||
component: AiHtmlField,
|
||||
displayName: _t("AI Html"),
|
||||
};
|
||||
|
||||
registry.category("fields").add("ai_html", aiHtmlField);
|
||||
|
||||
class AiIntegerField extends AiFieldMixin(IntegerField) {
|
||||
static template = "ai_fields.AiIntegerField";
|
||||
}
|
||||
|
||||
const aiIntegerField = {
|
||||
...integerField,
|
||||
component: AiIntegerField,
|
||||
displayName: _t("AI Integer"),
|
||||
};
|
||||
|
||||
registry.category("fields").add("ai_integer", aiIntegerField);
|
||||
|
||||
class AiMany2ManyTagsAvatarField extends AiFieldMixin(Many2ManyTagsAvatarField) {
|
||||
static template = "ai_fields.AiMany2ManyTagsAvatarField";
|
||||
}
|
||||
|
||||
const aiMany2ManyTagsAvatarField = {
|
||||
...many2ManyTagsAvatarField,
|
||||
component: AiMany2ManyTagsAvatarField,
|
||||
displayName: _t("AI Many2Many Tags Avatar"),
|
||||
};
|
||||
|
||||
registry.category("fields").add("ai_many2many_tags_avatar", aiMany2ManyTagsAvatarField);
|
||||
|
||||
class AiMany2ManyTagsField extends AiFieldMixin(Many2ManyTagsField) {
|
||||
static template = "ai_fields.AiMany2ManyTagsField";
|
||||
}
|
||||
|
||||
const aiMany2ManyTagsField = {
|
||||
...many2ManyTagsField,
|
||||
component: AiMany2ManyTagsField,
|
||||
displayName: _t("AI Many2Many Tags"),
|
||||
};
|
||||
|
||||
registry.category("fields").add("ai_many2many_tags", aiMany2ManyTagsField);
|
||||
|
||||
class AiMany2OneAvatarField extends AiFieldMixin(Many2OneAvatarField) {
|
||||
static template = "ai_fields.AiMany2OneAvatarField";
|
||||
}
|
||||
|
||||
const aiMany2OneAvatarField = {
|
||||
...many2OneAvatarField,
|
||||
component: AiMany2OneAvatarField,
|
||||
displayName: _t("AI Many2One"),
|
||||
};
|
||||
|
||||
registry.category("fields").add("ai_many2one_avatar", aiMany2OneAvatarField);
|
||||
|
||||
class AiMany2OneField extends AiFieldMixin(Many2OneField) {
|
||||
static template = "ai_fields.AiMany2OneField";
|
||||
}
|
||||
|
||||
const aiMany2OneField = {
|
||||
...buildM2OFieldDescription(AiMany2OneField),
|
||||
displayName: _t("AI Many2One"),
|
||||
};
|
||||
|
||||
registry.category("fields").add("ai_many2one", aiMany2OneField);
|
||||
|
||||
class AiMonetaryField extends AiFieldMixin(MonetaryField) {
|
||||
static template = "ai_fields.AiMonetaryField";
|
||||
}
|
||||
|
||||
const aiMonetaryField = {
|
||||
...monetaryField,
|
||||
component: AiMonetaryField,
|
||||
displayName: _t("AI Monetary"),
|
||||
};
|
||||
|
||||
registry.category("fields").add("ai_monetary", aiMonetaryField);
|
||||
|
||||
class AiSelectionField extends AiFieldMixin(SelectionField) {
|
||||
static template = "ai_fields.AiSelectionField";
|
||||
}
|
||||
|
||||
const aiSelectionField = {
|
||||
...selectionField,
|
||||
component: AiSelectionField,
|
||||
displayName: _t("AI Selection"),
|
||||
};
|
||||
|
||||
registry.category("fields").add("ai_selection", aiSelectionField);
|
||||
|
||||
class AiTextField extends AiFieldMixin(TextField) {
|
||||
static template = "ai_fields.AiTextField";
|
||||
}
|
||||
|
||||
const aiTextField = {
|
||||
...textField,
|
||||
additionalClasses: ["o_field_text"],
|
||||
component: AiTextField,
|
||||
displayName: _t("AI Text"),
|
||||
};
|
||||
|
||||
registry.category("fields").add("ai_text", aiTextField);
|
||||
@@ -0,0 +1,5 @@
|
||||
.o_field_ai_text {
|
||||
& > div > div > textarea {
|
||||
resize: none;
|
||||
}
|
||||
}
|
||||
143
ai_fields/static/src/views/fields/ai_fields/ai_fields.xml
Normal file
143
ai_fields/static/src/views/fields/ai_fields/ai_fields.xml
Normal file
@@ -0,0 +1,143 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<templates xml:space="preserve">
|
||||
|
||||
<t t-name="ai_fields.ComputeAiFieldBtn">
|
||||
<button class="btn btn-light flex-shrink-0 align-self-start p-0 me-2"
|
||||
t-on-click="this.onAiClick"
|
||||
title="Refresh value">
|
||||
<img src="/ai/static/description/icon.png" width="20" alt="ai icon"/>
|
||||
</button>
|
||||
</t>
|
||||
|
||||
<t t-name="ai_fields.AiBooleanField">
|
||||
<div t-if="!this.props.readonly and this.isAiComputed" class="d-inline-flex w-100">
|
||||
<t t-call="ai_fields.ComputeAiFieldBtn"/>
|
||||
<t t-call="web.BooleanField"/>
|
||||
</div>
|
||||
<t t-else="" t-call="web.BooleanField"/>
|
||||
</t>
|
||||
|
||||
<t t-name="ai_fields.AiCharField">
|
||||
<div t-if="!this.props.readonly and this.isAiComputed" class="d-inline-flex w-100">
|
||||
<t t-call="ai_fields.ComputeAiFieldBtn"/>
|
||||
<t t-call="web.CharField"/>
|
||||
</div>
|
||||
<t t-else="" t-call="web.CharField"/>
|
||||
</t>
|
||||
|
||||
<t t-name="ai_fields.AiDateTimeField">
|
||||
<div t-if="!this.props.readonly and this.isAiComputed" class="d-inline-flex w-100">
|
||||
<t t-call="ai_fields.ComputeAiFieldBtn"/>
|
||||
<div class="flex-grow-1">
|
||||
<t t-call="web.DateTimeField"/>
|
||||
</div>
|
||||
</div>
|
||||
<t t-else="">
|
||||
<t t-call="web.DateTimeField"/>
|
||||
</t>
|
||||
</t>
|
||||
|
||||
<t t-name="ai_fields.AiFloatField">
|
||||
<div t-if="!this.props.readonly and this.isAiComputed" class="d-inline-flex w-100">
|
||||
<t t-call="ai_fields.ComputeAiFieldBtn"/>
|
||||
<t t-call="web.FloatField"/>
|
||||
</div>
|
||||
<t t-else="" t-call="web.FloatField"/>
|
||||
</t>
|
||||
|
||||
<t t-name="ai_fields.AiHtmlField">
|
||||
<div t-if="!this.props.readonly and this.isAiComputed" class="d-inline-flex w-100">
|
||||
<t t-call="ai_fields.ComputeAiFieldBtn"/>
|
||||
<div class="flex-grow-1">
|
||||
<t t-call="html_editor.HtmlField"/>
|
||||
</div>
|
||||
</div>
|
||||
<t t-else="" t-call="html_editor.HtmlField"/>
|
||||
</t>
|
||||
|
||||
<t t-name="ai_fields.AiIntegerField">
|
||||
<div t-if="!this.props.readonly and this.isAiComputed" class="d-inline-flex w-100">
|
||||
<t t-call="ai_fields.ComputeAiFieldBtn"/>
|
||||
<t t-call="web.IntegerField"/>
|
||||
</div>
|
||||
<t t-else="" t-call="web.IntegerField"/>
|
||||
</t>
|
||||
|
||||
<t t-name="ai_fields.AiMany2ManyTagsAvatarField">
|
||||
<div t-if="!this.props.readonly and this.isAiComputed" class="d-inline-flex w-100">
|
||||
<t t-call="ai_fields.ComputeAiFieldBtn"/>
|
||||
<t t-call="web.Many2ManyTagsAvatarField"/>
|
||||
</div>
|
||||
<t t-else="" t-call="web.Many2ManyTagsAvatarField"/>
|
||||
</t>
|
||||
|
||||
<t t-name="ai_fields.AiMany2ManyTagsField">
|
||||
<div t-if="!this.props.readonly and this.isAiComputed" class="d-inline-flex w-100">
|
||||
<t t-call="ai_fields.ComputeAiFieldBtn"/>
|
||||
<t t-call="web.Many2ManyTagsField"/>
|
||||
</div>
|
||||
<t t-else="" t-call="web.Many2ManyTagsField"/>
|
||||
</t>
|
||||
|
||||
<t t-name="ai_fields.AiMany2OneAvatarField">
|
||||
<div t-if="!this.props.readonly and this.isAiComputed" class="d-inline-flex w-100">
|
||||
<t t-call="ai_fields.ComputeAiFieldBtn"/>
|
||||
<div class="flex-grow-1">
|
||||
<t t-call="web.Many2OneAvatarField"/>
|
||||
</div>
|
||||
</div>
|
||||
<t t-else="" t-call="web.Many2OneAvatarField"/>
|
||||
</t>
|
||||
|
||||
<t t-name="ai_fields.AiMany2OneField">
|
||||
<div t-if="!this.props.readonly and this.isAiComputed" class="d-inline-flex w-100">
|
||||
<t t-call="ai_fields.ComputeAiFieldBtn"/>
|
||||
<div class="flex-grow-1">
|
||||
<t t-call="web.Many2OneField"/>
|
||||
</div>
|
||||
</div>
|
||||
<t t-else="" t-call="web.Many2OneField"/>
|
||||
</t>
|
||||
|
||||
<t t-name="ai_fields.AiMonetaryField">
|
||||
<div t-if="!this.props.readonly and this.isAiComputed" class="d-inline-flex w-100">
|
||||
<t t-call="ai_fields.ComputeAiFieldBtn"/>
|
||||
<t t-call="web.MonetaryField"/>
|
||||
</div>
|
||||
<t t-else="" t-call="web.MonetaryField"/>
|
||||
</t>
|
||||
|
||||
<t t-inherit="web.PropertiesField" t-inherit-mode="extension">
|
||||
<xpath expr="//PropertyValue" position="replace">
|
||||
<div t-if="!this.props.readonly and propertyConfiguration.ai and propertyConfiguration.system_prompt"
|
||||
class="d-flex">
|
||||
<button class="btn btn-light d-flex flex-shrink-0 align-self-baseline p-0 m-0 me-2"
|
||||
t-on-click="() => this.onAiClick(propertyConfiguration.name)"
|
||||
title="Refresh value">
|
||||
<img src="/ai/static/description/icon.png" width="20" alt="ai icon"/>
|
||||
</button>
|
||||
<div class="flex-fill">$0</div>
|
||||
</div>
|
||||
<t t-else="">$0</t>
|
||||
</xpath>
|
||||
</t>
|
||||
|
||||
<t t-name="ai_fields.AiSelectionField">
|
||||
<div t-if="!this.props.readonly and this.isAiComputed" class="d-inline-flex w-100">
|
||||
<t t-call="ai_fields.ComputeAiFieldBtn"/>
|
||||
<t t-call="web.SelectionField"/>
|
||||
</div>
|
||||
<t t-else="" t-call="web.SelectionField"/>
|
||||
</t>
|
||||
|
||||
<t t-name="ai_fields.AiTextField">
|
||||
<div t-if="!this.props.readonly and this.isAiComputed" class="d-inline-flex w-100">
|
||||
<t t-call="ai_fields.ComputeAiFieldBtn"/>
|
||||
<div class="flex-grow-1">
|
||||
<t t-call="web.TextField"/>
|
||||
</div>
|
||||
</div>
|
||||
<t t-else="" t-call="web.TextField"/>
|
||||
</t>
|
||||
|
||||
</templates>
|
||||
@@ -0,0 +1,53 @@
|
||||
import { t } from "@odoo/owl";
|
||||
import { AiPrompt } from "@ai/ai_prompt/ai_prompt";
|
||||
import { patch } from "@web/core/utils/patch";
|
||||
import { PropertiesField } from "@web/views/fields/properties/properties_field";
|
||||
import {
|
||||
PropertyDefinition,
|
||||
propertyDefinitionProps,
|
||||
} from "@web/views/fields/properties/property_definition";
|
||||
|
||||
propertyDefinitionProps.propertiesModel = t.string();
|
||||
|
||||
patch(PropertiesField.prototype, {
|
||||
get additionalPropertyDefinitionProps() {
|
||||
return {
|
||||
...super.additionalPropertyDefinitionProps,
|
||||
propertiesModel: this.props.record.resModel,
|
||||
};
|
||||
},
|
||||
async onAiClick(propertyName) {
|
||||
const value = await this.props.record.computeAiProperty(
|
||||
`${this.props.name}.${propertyName}`
|
||||
);
|
||||
this.onPropertyValueChange(propertyName, value);
|
||||
},
|
||||
|
||||
async onPropertyDefinitionChange(propertyDefinition) {
|
||||
const propertyIndex = this._getPropertyIndex(propertyDefinition.name);
|
||||
const oldDefinition = this.propertiesList[propertyIndex];
|
||||
|
||||
if (!oldDefinition.ai && propertyDefinition.ai) {
|
||||
const lastRecord = await this.orm.call(
|
||||
this.props.record._config.resModel,
|
||||
"search",
|
||||
[],
|
||||
{ domain: [], limit: 1, order: "id DESC" }
|
||||
);
|
||||
const resId = lastRecord[0] || 0;
|
||||
if (this.props.record._config.resId) {
|
||||
propertyDefinition.ai_domain = [
|
||||
"|",
|
||||
["id", ">=", resId - 50],
|
||||
["id", "=", this.props.record._config.resId],
|
||||
];
|
||||
} else {
|
||||
propertyDefinition.ai_domain = [["id", ">=", resId - 50]];
|
||||
}
|
||||
}
|
||||
|
||||
return await super.onPropertyDefinitionChange(...arguments);
|
||||
},
|
||||
});
|
||||
|
||||
PropertyDefinition.components = { ...PropertyDefinition.components, AiPrompt };
|
||||
@@ -0,0 +1,38 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<templates xml:space="preserve">
|
||||
<t t-inherit="web.PropertyDefinition" t-inherit-mode="extension">
|
||||
<xpath expr="//div[hasclass('o_field_property_definition_value')]" position="attributes">
|
||||
<attribute name="t-if" add="and !this.state.propertyDefinition.ai" separator=" "/>
|
||||
</xpath>
|
||||
<xpath expr="//div[hasclass('o_modal_container')]" position="inside">
|
||||
<div t-if="!['separator', 'signature'].includes(this.state.propertyDefinition.type)" class="d-contents mb-3 mb-sm-0">
|
||||
<label t-att-for="this.getUniqueDomID('ai')" class="o_form_label align-self-center text-900">
|
||||
AI
|
||||
</label>
|
||||
<CheckBox
|
||||
id="this.getUniqueDomID('ai')"
|
||||
value="this.props.propertyDefinition.ai"
|
||||
disabled="this.props.readonly"
|
||||
onChange="(v) => this.onAttributeChange('ai', v)"
|
||||
/>
|
||||
</div>
|
||||
<div t-if="this.state.propertyDefinition.ai" class="d-contents mb-3 mb-sm-0">
|
||||
<label t-att-for="this.getUniqueDomID('prompt')" class="o_form_label align-self-center text-900 mb-auto">
|
||||
Prompt
|
||||
</label>
|
||||
<h6 t-if="this.props.readonly" t-out="this.state.propertyDefinition.system_prompt" class="text-truncate"/>
|
||||
<AiPrompt
|
||||
t-else=""
|
||||
comodel="this.state.propertyDefinition.comodel"
|
||||
domain="this.state.propertyDefinition.domain or ''"
|
||||
model="this.props.propertiesModel"
|
||||
updatePrompt="(v) => this.onAttributeChange('system_prompt', v)"
|
||||
placeholder.translate="Write a prompt, use '/field' to insert dynamic values"
|
||||
prompt="this.state.propertyDefinition.system_prompt || ''"
|
||||
readonly="this.props.readonly"
|
||||
aiFieldPath="this.props.fieldName + '.' + this.state.propertyDefinition.name"
|
||||
/>
|
||||
</div>
|
||||
</xpath>
|
||||
</t>
|
||||
</templates>
|
||||
439
ai_fields/static/tests/ai_fields.test.js
Normal file
439
ai_fields/static/tests/ai_fields.test.js
Normal file
@@ -0,0 +1,439 @@
|
||||
import { defineMailModels } from "@mail/../tests/mail_test_helpers";
|
||||
import {
|
||||
Command,
|
||||
defineModels,
|
||||
fields,
|
||||
makeServerError,
|
||||
models,
|
||||
mountView,
|
||||
patchWithCleanup,
|
||||
} from "@web/../tests/web_test_helpers";
|
||||
import { Record } from "@web/model/relational_model/record";
|
||||
|
||||
import { animationFrame, beforeEach, click, expect, test } from "@odoo/hoot";
|
||||
|
||||
let unresolveRequest = false;
|
||||
|
||||
class ParentModel extends models.Model {
|
||||
_name = "ai_parent";
|
||||
|
||||
properties_definition = fields.PropertiesDefinition();
|
||||
|
||||
_records = [
|
||||
{
|
||||
id: 1,
|
||||
properties_definition: [
|
||||
{
|
||||
type: "char",
|
||||
ai: true,
|
||||
system_prompt: "hello",
|
||||
name: "ai_char",
|
||||
string: "AI Char",
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
class AiModel extends models.Model {
|
||||
_name = "ai_model";
|
||||
|
||||
currency_id = fields.Many2one({ relation: "currency" });
|
||||
|
||||
bool = fields.Boolean({ ai: "bool prompt" });
|
||||
char = fields.Char({ ai: "char prompt" });
|
||||
datetime = fields.Datetime({ ai: "2024-03-26 12:00:00" });
|
||||
float = fields.Float({ ai: 42.5 });
|
||||
html = fields.Html({ ai: "<p>html prompt</p>" });
|
||||
integer = fields.Integer({ ai: 32 });
|
||||
many2many = fields.Many2many({ ai: [Command.set([1, 2])], relation: "ai_model" });
|
||||
many2one = fields.Many2one({ ai: { id: 2, display_name: "ai_model,2" }, relation: "ai_model" });
|
||||
monetary = fields.Monetary({ ai: 99.99, currency_field: "currency_id" });
|
||||
selection = fields.Selection({
|
||||
ai: "v2",
|
||||
selection: [
|
||||
["v1", "Val 1"],
|
||||
["v2", "Val 2"],
|
||||
],
|
||||
});
|
||||
text = fields.Text({ ai: "text prompt" });
|
||||
|
||||
parent_id = fields.Many2one({ relation: "ai_parent" });
|
||||
properties = fields.Properties({
|
||||
definition_record: "parent_id",
|
||||
definition_record_field: "properties_definition",
|
||||
});
|
||||
|
||||
_records = [{ id: 1, parent_id: 1 }, { id: 2 }];
|
||||
|
||||
async get_ai_field_value(rec, fieldName, changes) {
|
||||
const field = this._fields[fieldName];
|
||||
if (!field) {
|
||||
throw new Error("Unknown field " + fieldName);
|
||||
}
|
||||
if (unresolveRequest) {
|
||||
throw makeServerError({
|
||||
errorName: "odoo.addons.ai.utils.ai_fields_tools.UnresolvedQuery",
|
||||
description: "The value could not be resolved",
|
||||
});
|
||||
}
|
||||
if (field.type === "boolean") {
|
||||
return true;
|
||||
}
|
||||
return field.ai;
|
||||
}
|
||||
|
||||
async get_ai_property_value(rec, fullName, changes) {
|
||||
const [fname, pname] = fullName.split(".");
|
||||
const field = this._fields[fname];
|
||||
if (!field) {
|
||||
throw new Error("Unknown field " + fname);
|
||||
}
|
||||
if (unresolveRequest) {
|
||||
throw makeServerError({
|
||||
errorName: "odoo.addons.ai.utils.ai_fields_tools.UnresolvedQuery",
|
||||
description: "The value could not be resolved",
|
||||
});
|
||||
}
|
||||
return pname;
|
||||
}
|
||||
}
|
||||
|
||||
class Currency extends models.Model {
|
||||
_name = "currency";
|
||||
}
|
||||
|
||||
defineModels([ParentModel, AiModel, Currency]);
|
||||
defineMailModels();
|
||||
|
||||
beforeEach(() => {
|
||||
patchWithCleanup(Record.prototype, {
|
||||
async computeAiField(fname) {
|
||||
const res = await super.computeAiField(fname);
|
||||
expect.step(fname + " computed");
|
||||
return res;
|
||||
},
|
||||
|
||||
async save(options) {
|
||||
const res = await super.save(options);
|
||||
expect.step("save");
|
||||
return res;
|
||||
},
|
||||
|
||||
async computeAiProperty(fullName) {
|
||||
const res = await super.computeAiProperty(fullName);
|
||||
expect.step(fullName + " computed");
|
||||
return res;
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
test("AI Fields - Boolean Field", async () => {
|
||||
await mountView({
|
||||
type: "form",
|
||||
resId: 1,
|
||||
resModel: "ai_model",
|
||||
arch: `<form>
|
||||
<field name="bool" widget="ai_boolean"/>
|
||||
</form>`,
|
||||
});
|
||||
expect(".o_field_ai_boolean").toHaveCount(1);
|
||||
expect(".o_field_ai_boolean input").not.toBeChecked();
|
||||
expect(".o_field_ai_boolean .btn[title='Refresh value']").toHaveCount(1);
|
||||
await click(".o_field_ai_boolean .btn[title='Refresh value']");
|
||||
await expect.waitForSteps(["bool computed"]);
|
||||
await animationFrame();
|
||||
await animationFrame();
|
||||
expect(".o_field_ai_boolean input").toBeChecked();
|
||||
await click(".o_form_button_save");
|
||||
await expect.waitForSteps(["save"]);
|
||||
await animationFrame();
|
||||
expect(".o_field_ai_boolean input").toBeChecked();
|
||||
});
|
||||
|
||||
test("AI Fields - Char Field", async () => {
|
||||
await mountView({
|
||||
type: "form",
|
||||
resId: 1,
|
||||
resModel: "ai_model",
|
||||
arch: `<form>
|
||||
<field name="char" widget="ai_char"/>
|
||||
</form>`,
|
||||
});
|
||||
expect(".o_field_ai_char").toHaveCount(1);
|
||||
expect(".o_field_ai_char input").toHaveValue("");
|
||||
expect(".o_field_ai_char .btn[title='Refresh value']").toHaveCount(1);
|
||||
await click(".o_field_ai_char .btn[title='Refresh value']");
|
||||
await expect.waitForSteps(["char computed"]);
|
||||
await animationFrame();
|
||||
expect(".o_field_ai_char input").toHaveValue("char prompt");
|
||||
await click(".o_form_button_save");
|
||||
await expect.waitForSteps(["save"]);
|
||||
await animationFrame();
|
||||
expect(".o_field_ai_char input").toHaveValue("char prompt");
|
||||
});
|
||||
|
||||
test("AI Fields - Datetime Field", async () => {
|
||||
await mountView({
|
||||
type: "form",
|
||||
resId: 1,
|
||||
resModel: "ai_model",
|
||||
arch: `<form>
|
||||
<field name="datetime" widget="ai_datetime"/>
|
||||
</form>`,
|
||||
});
|
||||
expect(".o_field_ai_datetime").toHaveCount(1);
|
||||
expect(".o_field_ai_datetime input").toHaveValue("");
|
||||
expect(".o_field_ai_datetime .btn[title='Refresh value']").toHaveCount(1);
|
||||
await click(".o_field_ai_datetime .btn[title='Refresh value']");
|
||||
await expect.waitForSteps(["datetime computed"]);
|
||||
await animationFrame();
|
||||
expect(".o_field_ai_datetime button[data-field=datetime]").toHaveValue("03/26/2024 13:00:00");
|
||||
await click(".o_form_button_save");
|
||||
await expect.waitForSteps(["save"]);
|
||||
await animationFrame();
|
||||
expect(".o_field_ai_datetime button[data-field=datetime]").toHaveValue("03/26/2024 13:00:00");
|
||||
});
|
||||
|
||||
test("AI Fields - Float Field", async () => {
|
||||
await mountView({
|
||||
type: "form",
|
||||
resId: 1,
|
||||
resModel: "ai_model",
|
||||
arch: `<form>
|
||||
<field name="float" widget="ai_float"/>
|
||||
</form>`,
|
||||
});
|
||||
expect(".o_field_ai_float").toHaveCount(1);
|
||||
expect(".o_field_ai_float input").toHaveValue("0.00");
|
||||
expect(".o_field_ai_float .btn[title='Refresh value']").toHaveCount(1);
|
||||
await click(".o_field_ai_float .btn[title='Refresh value']");
|
||||
await expect.waitForSteps(["float computed"]);
|
||||
await animationFrame();
|
||||
expect(".o_field_ai_float input").toHaveValue("42.50");
|
||||
await click(".o_form_button_save");
|
||||
await expect.waitForSteps(["save"]);
|
||||
await animationFrame();
|
||||
expect(".o_field_ai_float input").toHaveValue("42.50");
|
||||
});
|
||||
|
||||
test("AI Fields - Html Field", async () => {
|
||||
await mountView({
|
||||
type: "form",
|
||||
resId: 1,
|
||||
resModel: "ai_model",
|
||||
arch: `<form>
|
||||
<field name="html" widget="ai_html"/>
|
||||
</form>`,
|
||||
});
|
||||
expect(".o_field_ai_html").toHaveCount(1);
|
||||
expect(".o_field_ai_html .odoo-editor-editable").toHaveText("");
|
||||
expect(".o_field_ai_html .btn[title='Refresh value']").toHaveCount(1);
|
||||
await click(".o_field_ai_html .btn[title='Refresh value']");
|
||||
await expect.waitForSteps(["html computed"]);
|
||||
await animationFrame();
|
||||
await animationFrame();
|
||||
expect(".o_field_ai_html .odoo-editor-editable p").toHaveText("html prompt");
|
||||
await click(".o_form_button_save");
|
||||
await expect.waitForSteps(["save"]);
|
||||
await animationFrame();
|
||||
expect(".o_field_ai_html .odoo-editor-editable p").toHaveText("html prompt");
|
||||
});
|
||||
|
||||
test("AI Fields - Integer Field", async () => {
|
||||
await mountView({
|
||||
type: "form",
|
||||
resId: 1,
|
||||
resModel: "ai_model",
|
||||
arch: `<form>
|
||||
<field name="integer" widget="ai_integer"/>
|
||||
</form>`,
|
||||
});
|
||||
expect(".o_field_ai_integer").toHaveCount(1);
|
||||
expect(".o_field_ai_integer input").toHaveValue("0");
|
||||
expect(".o_field_ai_integer .btn[title='Refresh value']").toHaveCount(1);
|
||||
await click(".o_field_ai_integer .btn[title='Refresh value']");
|
||||
await expect.waitForSteps(["integer computed"]);
|
||||
await animationFrame();
|
||||
expect(".o_field_ai_integer input").toHaveValue("32");
|
||||
await click(".o_form_button_save");
|
||||
await expect.waitForSteps(["save"]);
|
||||
await animationFrame();
|
||||
expect(".o_field_ai_integer input").toHaveValue("32");
|
||||
});
|
||||
|
||||
test("AI Fields - Many2ManyTags Field", async () => {
|
||||
await mountView({
|
||||
type: "form",
|
||||
resId: 1,
|
||||
resModel: "ai_model",
|
||||
arch: `<form>
|
||||
<field name="many2many" widget="ai_many2many_tags"/>
|
||||
</form>`,
|
||||
});
|
||||
expect(".o_field_ai_many2many_tags").toHaveCount(1);
|
||||
expect(".o_field_ai_many2many_tags .o_tag").toHaveCount(0);
|
||||
expect(".o_field_ai_many2many_tags .btn[title='Refresh value']").toHaveCount(1);
|
||||
await click(".o_field_ai_many2many_tags .btn[title='Refresh value']");
|
||||
await expect.waitForSteps(["many2many computed"]);
|
||||
await animationFrame();
|
||||
expect(".o_field_ai_many2many_tags .o_tag").toHaveCount(2);
|
||||
expect(".o_field_ai_many2many_tags .o_tag:first").toHaveText("ai_model,1");
|
||||
expect(".o_field_ai_many2many_tags .o_tag:last").toHaveText("ai_model,2");
|
||||
await click(".o_form_button_save");
|
||||
await expect.waitForSteps(["save"]);
|
||||
await animationFrame();
|
||||
expect(".o_field_ai_many2many_tags .o_tag").toHaveCount(2);
|
||||
expect(".o_field_ai_many2many_tags .o_tag:first").toHaveText("ai_model,1");
|
||||
expect(".o_field_ai_many2many_tags .o_tag:last").toHaveText("ai_model,2");
|
||||
});
|
||||
|
||||
test("AI Fields - Many2One Field", async () => {
|
||||
await mountView({
|
||||
type: "form",
|
||||
resId: 1,
|
||||
resModel: "ai_model",
|
||||
arch: `<form>
|
||||
<field name="many2one" widget="ai_many2one"/>
|
||||
</form>`,
|
||||
});
|
||||
expect(".o_field_ai_many2one").toHaveCount(1);
|
||||
expect(".o_field_ai_many2one input").toHaveValue("");
|
||||
expect(".o_field_ai_many2one .btn[title='Refresh value']").toHaveCount(1);
|
||||
await click(".o_field_ai_many2one .btn[title='Refresh value']");
|
||||
await expect.waitForSteps(["many2one computed"]);
|
||||
await animationFrame();
|
||||
expect(".o_field_ai_many2one input").toHaveValue("ai_model,2");
|
||||
await click(".o_form_button_save");
|
||||
await expect.waitForSteps(["save"]);
|
||||
await animationFrame();
|
||||
expect(".o_field_ai_many2one input").toHaveValue("ai_model,2");
|
||||
});
|
||||
|
||||
test("AI Fields - Monetary Field", async () => {
|
||||
await mountView({
|
||||
type: "form",
|
||||
resId: 1,
|
||||
resModel: "ai_model",
|
||||
arch: `<form>
|
||||
<field name="monetary" widget="ai_monetary" options="{'currency_field': 'currency_id'}"/>
|
||||
</form>`,
|
||||
});
|
||||
expect(".o_field_ai_monetary").toHaveCount(1);
|
||||
expect(".o_field_ai_monetary input").toHaveValue("0.00");
|
||||
expect(".o_field_ai_monetary .btn[title='Refresh value']").toHaveCount(1);
|
||||
await click(".o_field_ai_monetary .btn[title='Refresh value']");
|
||||
await expect.waitForSteps(["monetary computed"]);
|
||||
await animationFrame();
|
||||
expect(".o_field_ai_monetary input").toHaveValue("99.99");
|
||||
await click(".o_form_button_save");
|
||||
await expect.waitForSteps(["save"]);
|
||||
await animationFrame();
|
||||
expect(".o_field_ai_monetary input").toHaveValue("99.99");
|
||||
});
|
||||
|
||||
test("AI Fields - Selection Field", async () => {
|
||||
await mountView({
|
||||
type: "form",
|
||||
resId: 1,
|
||||
resModel: "ai_model",
|
||||
arch: `<form>
|
||||
<field name="selection" widget="ai_selection"/>
|
||||
</form>`,
|
||||
});
|
||||
expect(".o_field_ai_selection").toHaveCount(1);
|
||||
expect(".o_field_ai_selection input").toHaveValue("");
|
||||
expect(".o_field_ai_selection .btn[title='Refresh value']").toHaveCount(1);
|
||||
await click(".o_field_ai_selection .btn[title='Refresh value']");
|
||||
await expect.waitForSteps(["selection computed"]);
|
||||
await animationFrame();
|
||||
expect(".o_field_ai_selection input").toHaveValue("Val 2");
|
||||
await click(".o_form_button_save");
|
||||
await expect.waitForSteps(["save"]);
|
||||
await animationFrame();
|
||||
expect(".o_field_ai_selection input").toHaveValue("Val 2");
|
||||
});
|
||||
|
||||
test("AI Fields - Text Field", async () => {
|
||||
await mountView({
|
||||
type: "form",
|
||||
resId: 1,
|
||||
resModel: "ai_model",
|
||||
arch: `<form>
|
||||
<field name="text" widget="ai_text"/>
|
||||
</form>`,
|
||||
});
|
||||
expect(".o_field_ai_text").toHaveCount(1);
|
||||
expect(".o_field_ai_text textarea").toHaveValue("");
|
||||
expect(".o_field_ai_text .btn[title='Refresh value']").toHaveCount(1);
|
||||
await click(".o_field_ai_text .btn[title='Refresh value']");
|
||||
await expect.waitForSteps(["text computed"]);
|
||||
await animationFrame();
|
||||
expect(".o_field_ai_text textarea").toHaveValue("text prompt");
|
||||
await click(".o_form_button_save");
|
||||
await expect.waitForSteps(["save"]);
|
||||
await animationFrame();
|
||||
expect(".o_field_ai_text textarea").toHaveValue("text prompt");
|
||||
});
|
||||
|
||||
test("AI Fields - Unresolved Request", async () => {
|
||||
await mountView({
|
||||
type: "form",
|
||||
resId: 1,
|
||||
resModel: "ai_model",
|
||||
arch: `<form>
|
||||
<field name="char" widget="ai_char"/>
|
||||
</form>`,
|
||||
});
|
||||
unresolveRequest = true;
|
||||
expect(".o_field_ai_char").toHaveCount(1);
|
||||
expect(".o_field_ai_char input").toHaveValue("");
|
||||
expect(".o_field_ai_char .btn[title='Refresh value']").toHaveCount(1);
|
||||
await click(".o_field_ai_char .btn[title='Refresh value']");
|
||||
await expect.waitForSteps(["char computed"]);
|
||||
await animationFrame();
|
||||
expect(".o_notification .o_notification_content").toHaveText(
|
||||
/The value could not be resolved$/
|
||||
);
|
||||
expect(".o_field_ai_char input").toHaveValue("");
|
||||
});
|
||||
|
||||
test("AI Properties - Resolved Request", async () => {
|
||||
await mountView({
|
||||
type: "form",
|
||||
resId: 1,
|
||||
resModel: "ai_model",
|
||||
arch: `<form>
|
||||
<field name="properties"/>
|
||||
</form>`,
|
||||
});
|
||||
unresolveRequest = false;
|
||||
expect(".o_field_properties .o_property_field_value").toHaveCount(1);
|
||||
expect(".o_field_properties .o_property_field_value input").toHaveValue("");
|
||||
await click(".o_field_properties .btn[title='Refresh value']");
|
||||
await expect.waitForSteps(["properties.ai_char computed"]);
|
||||
await animationFrame();
|
||||
expect(".o_notification .o_notification_content").toHaveCount(0);
|
||||
expect(".o_field_properties .o_property_field_value input").toHaveValue("ai_char");
|
||||
});
|
||||
|
||||
test("AI Properties - Unresolved Request", async () => {
|
||||
await mountView({
|
||||
type: "form",
|
||||
resId: 1,
|
||||
resModel: "ai_model",
|
||||
arch: `<form>
|
||||
<field name="properties"/>
|
||||
</form>`,
|
||||
});
|
||||
unresolveRequest = true;
|
||||
expect(".o_field_properties .o_property_field_value").toHaveCount(1);
|
||||
expect(".o_field_properties .o_property_field_value input").toHaveValue("");
|
||||
await click(".o_field_properties .btn[title='Refresh value']");
|
||||
await expect.waitForSteps(["properties.ai_char computed"]);
|
||||
await animationFrame();
|
||||
expect(".o_notification .o_notification_content").toHaveText(
|
||||
/The value could not be resolved$/
|
||||
);
|
||||
expect(".o_field_properties .o_property_field_value input").toHaveValue("");
|
||||
});
|
||||
Reference in New Issue
Block a user