-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathskill-card.gts
59 lines (54 loc) · 1.75 KB
/
skill-card.gts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import BooleanField from './boolean';
import CodeRefField from './code-ref';
import MarkdownField from './markdown';
import StringField from './string';
import {
CardDef,
Component,
field,
FieldDef,
contains,
containsMany,
} from './card-api';
import RobotIcon from '@cardstack/boxel-icons/robot';
export class CommandField extends FieldDef {
static displayName = 'CommandField';
@field codeRef = contains(CodeRefField, {
description: 'An absolute code reference to the command to be executed',
});
@field requiresApproval = contains(BooleanField, {
description:
'If true, this command will require human approval before it is executed in the host.',
});
@field functionName = contains(StringField, {
description: 'The name of the function to be executed',
computeVia: function (this: CommandField) {
if (!this.codeRef?.module || !this.codeRef?.name) {
return '';
}
// Simple hash function that works in all environments
const djb2 = (str: string): string => {
let hash = 5381;
for (let i = 0; i < str.length; i++) {
const char = str.charCodeAt(i);
hash = (hash << 5) - hash + char;
hash = hash & hash; // Convert to 32-bit integer
}
return Math.abs(hash).toString(16).slice(0, 4);
};
const input = `${this.codeRef.module}#${this.codeRef.name}`;
return `${this.codeRef.name}_${djb2(input)}`;
},
});
}
export class SkillCard extends CardDef {
static displayName = 'Skill';
static icon = RobotIcon;
@field instructions = contains(MarkdownField);
@field commands = containsMany(CommandField);
static embedded = class Embedded extends Component<typeof this> {
<template>
<@fields.title />
</template>
};
}