Skip to content

Commit

Permalink
Merge branch 'feat-improve-ner-types' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
louistiti committed Nov 12, 2023
2 parents 4d03e1b + b2c3758 commit 2a15434
Show file tree
Hide file tree
Showing 25 changed files with 667 additions and 291 deletions.
12 changes: 12 additions & 0 deletions app/src/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,17 @@ small {
.bubble-container.leon {
text-align: left;
}
.show-more {
margin: 3px;
text-decoration: underline;
}
.show-more:hover {
cursor: pointer;
text-decoration: none;
}
.show-all {
max-height: 100% !important;
}

.bubble {
padding: 6px 12px;
Expand All @@ -297,6 +308,7 @@ small {
text-align: left;
opacity: 0;
animation: fadeIn 0.2s ease-in forwards;
overflow: hidden;
}
#feed .me .bubble {
background-color: #1c75db;
Expand Down
19 changes: 19 additions & 0 deletions app/src/js/chatbot.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const MAXIMUM_HEIGHT_TO_SHOW_SEE_MORE = 340

export default class Chatbot {
constructor() {
this.et = new EventTarget()
Expand Down Expand Up @@ -95,6 +97,23 @@ export default class Chatbot {

this.feed.appendChild(container).appendChild(bubble)

if (container.clientHeight > MAXIMUM_HEIGHT_TO_SHOW_SEE_MORE) {
bubble.style.maxHeight = `${MAXIMUM_HEIGHT_TO_SHOW_SEE_MORE}px`
const showMore = document.createElement('p')
const showMoreText = 'Show more'

showMore.className = 'show-more'
showMore.innerHTML = showMoreText

container.appendChild(showMore)

showMore.addEventListener('click', () => {
bubble.classList.toggle('show-all')
showMore.innerHTML =
showMore.innerHTML === showMoreText ? 'Show less' : showMoreText
})
}

if (save) {
this.saveBubble(who, string)
}
Expand Down
1 change: 1 addition & 0 deletions app/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"compilerOptions": {
"useDefineForClassFields": true,
"skipLibCheck": true,
"module": "ESNext",
/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
Expand Down
118 changes: 57 additions & 61 deletions bridges/nodejs/src/sdk/leon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,78 +22,71 @@ class Leon {
* @example setAnswerData('key', { name: 'Leon' })
*/
public setAnswerData(
answerKey: string | undefined,
answerKey: string,
data: AnswerData = null
): AnswerConfig | null | undefined {
if (answerKey) {
try {
// In case the answer key is a raw answer
if (SKILL_CONFIG.answers == null || !SKILL_CONFIG.answers[answerKey]) {
return answerKey
}
): AnswerConfig {
try {
// In case the answer key is a raw answer
if (SKILL_CONFIG.answers == null || !SKILL_CONFIG.answers[answerKey]) {
return answerKey
}

const answers = SKILL_CONFIG.answers[answerKey] ?? ''
let answer: AnswerConfig
const answers = SKILL_CONFIG.answers[answerKey] ?? ''
let answer: AnswerConfig

if (Array.isArray(answers)) {
answer = answers[Math.floor(Math.random() * answers.length)] ?? ''
} else {
answer = answers
}
if (Array.isArray(answers)) {
answer = answers[Math.floor(Math.random() * answers.length)] ?? ''
} else {
answer = answers
}

if (data) {
for (const key in data) {
// In case the answer needs speech and text differentiation
if (typeof answer !== 'string' && answer.text) {
answer.text = answer.text.replaceAll(
`%${key}%`,
String(data[key])
)
answer.speech = answer.speech.replaceAll(
`%${key}%`,
String(data[key])
)
} else {
answer = (answer as string).replaceAll(
`%${key}%`,
String(data[key])
)
}
if (data != null) {
for (const key in data) {
// In case the answer needs speech and text differentiation
if (typeof answer !== 'string' && answer.text) {
answer.text = answer.text.replaceAll(`%${key}%`, String(data[key]))
answer.speech = answer.speech.replaceAll(
`%${key}%`,
String(data[key])
)
} else {
answer = (answer as string).replaceAll(
`%${key}%`,
String(data[key])
)
}
}
}

if (SKILL_CONFIG.variables) {
const { variables } = SKILL_CONFIG

for (const key in variables) {
// In case the answer needs speech and text differentiation
if (typeof answer !== 'string' && answer.text) {
answer.text = answer.text.replaceAll(
`%${key}%`,
String(variables[key])
)
answer.speech = answer.speech.replaceAll(
`%${key}%`,
String(variables[key])
)
} else {
answer = (answer as string).replaceAll(
`%${key}%`,
String(variables[key])
)
}
if (SKILL_CONFIG.variables) {
const { variables } = SKILL_CONFIG

for (const key in variables) {
// In case the answer needs speech and text differentiation
if (typeof answer !== 'string' && answer.text) {
answer.text = answer.text.replaceAll(
`%${key}%`,
String(variables[key])
)
answer.speech = answer.speech.replaceAll(
`%${key}%`,
String(variables[key])
)
} else {
answer = (answer as string).replaceAll(
`%${key}%`,
String(variables[key])
)
}
}
}

return answer
} catch (e) {
console.error('Error while setting answer data:', e)
return answer
} catch (e) {
console.error('Error while setting answer data:', e)

return null
}
throw e
}

return undefined
}

/**
Expand All @@ -112,7 +105,10 @@ class Leon {
answerInput.widget && !answerInput.key
? 'widget'
: (answerInput.key as string),
answer: this.setAnswerData(answerInput.key, answerInput.data) ?? '',
answer:
answerInput.key != null
? this.setAnswerData(answerInput.key, answerInput.data)
: '',
core: answerInput.core
}
}
Expand Down
77 changes: 37 additions & 40 deletions bridges/python/src/sdk/leon.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,60 +16,57 @@ def __init__(self) -> None:
Leon.instance = self

@staticmethod
def set_answer_data(answer_key: Union[str, None], data: Union[AnswerData, None] = None) -> Union[str, AnswerConfig, None]:
def set_answer_data(answer_key: str, data: Union[AnswerData, None] = None) -> Union[str, AnswerConfig]:
"""
Apply data to the answer
:param answer_key: The answer key
:param data: The data to apply
"""
if answer_key:
try:
# In case the answer key is a raw answer
if SKILL_CONFIG.get('answers') is None or SKILL_CONFIG['answers'].get(answer_key) is None:
return answer_key

answers = SKILL_CONFIG['answers'].get(answer_key, '')
if isinstance(answers, list):
answer = answers[random.randrange(len(answers))]
else:
answer = answers

if data:
for key, value in data.items():
# In case the answer needs speech and text differentiation
if not isinstance(answer, str) and answer.get('text'):
answer['text'] = answer['text'].replace('%{}%'.format(key), str(value))
answer['speech'] = answer['speech'].replace('%{}%'.format(key), str(value))
else:
answer = answer.replace('%{}%'.format(key), str(value))

if SKILL_CONFIG.get('variables'):
for key, value in SKILL_CONFIG['variables'].items():
# In case the answer needs speech and text differentiation
if not isinstance(answer, str) and answer.get('text'):
answer['text'] = answer['text'].replace('%{}%'.format(key), str(value))
answer['speech'] = answer['speech'].replace('%{}%'.format(key), str(value))
else:
answer = answer.replace('%{}%'.format(key), str(value))

return answer
except Exception as e:
print('Error while setting answer data:', e)

return None

return None
try:
# In case the answer key is a raw answer
if SKILL_CONFIG.get('answers') is None or SKILL_CONFIG['answers'].get(answer_key) is None:
return answer_key

answers = SKILL_CONFIG['answers'].get(answer_key, '')
if isinstance(answers, list):
answer = answers[random.randrange(len(answers))]
else:
answer = answers

if data:
for key, value in data.items():
# In case the answer needs speech and text differentiation
if not isinstance(answer, str) and answer.get('text'):
answer['text'] = answer['text'].replace('%{}%'.format(key), str(value))
answer['speech'] = answer['speech'].replace('%{}%'.format(key), str(value))
else:
answer = answer.replace('%{}%'.format(key), str(value))

if SKILL_CONFIG.get('variables'):
for key, value in SKILL_CONFIG['variables'].items():
# In case the answer needs speech and text differentiation
if not isinstance(answer, str) and answer.get('text'):
answer['text'] = answer['text'].replace('%{}%'.format(key), str(value))
answer['speech'] = answer['speech'].replace('%{}%'.format(key), str(value))
else:
answer = answer.replace('%{}%'.format(key), str(value))

return answer
except Exception as e:
print('Error while setting answer data:', e)
raise e

def answer(self, answer_input: AnswerInput) -> None:
"""
Send an answer to the core
:param answer_input: The answer input
"""
try:
key = answer_input.get('key')
output = {
'output': {
'codes': 'widget' if answer_input.get('widget') and not answer_input.get('key') else answer_input.get('key'),
'answer': self.set_answer_data(answer_input.get('key'), answer_input.get('data')) or '',
'answer': self.set_answer_data(key, answer_input.get('data')) if key is not None else '',
'core': answer_input.get('core')
}
}
Expand All @@ -82,7 +79,7 @@ def answer(self, answer_input: AnswerInput) -> None:
**output
}

# Temporize for the data buffer output on the core
# "Temporize" for the data buffer output on the core
sleep(0.1)

sys.stdout.write(json.dumps(answer_object))
Expand Down
10 changes: 10 additions & 0 deletions core/skills-endpoints.json
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@
"route": "/api/action/leon/greeting/run",
"params": []
},
{
"method": "GET",
"route": "/api/action/leon/help/help",
"params": []
},
{
"method": "GET",
"route": "/api/action/leon/introduction/introduce_leon",
Expand Down Expand Up @@ -215,6 +220,11 @@
"route": "/api/action/utilities/date_time/days_countdown",
"params": []
},
{
"method": "GET",
"route": "/api/action/utilities/date_time/current_date_time_with_time_zone",
"params": []
},
{
"method": "GET",
"route": "/api/action/utilities/have_i_been_pwned/run",
Expand Down
Loading

0 comments on commit 2a15434

Please sign in to comment.