Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't treat non-URL CodeRef modules as URLs #2196

Merged
merged 2 commits into from
Feb 24, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions packages/base/code-ref.gts
Original file line number Diff line number Diff line change
@@ -16,6 +16,14 @@ import {
import { ResolvedCodeRef } from '@cardstack/runtime-common';
import CodeIcon from '@cardstack/boxel-icons/code';

function moduleIsUrlLike(module: string) {
return (
module.startsWith('http') ||
module.startsWith('.') ||
module.startsWith('/')
);
}

class BaseView extends Component<typeof CodeRefField> {
<template>
<div data-test-ref>
@@ -39,7 +47,9 @@ export default class CodeRefField extends FieldDef {
) {
return {
...codeRef,
...(opts?.maybeRelativeURL && !opts?.useAbsoluteURL
...(opts?.maybeRelativeURL &&
!opts?.useAbsoluteURL &&
moduleIsUrlLike(codeRef.module)
? { module: opts.maybeRelativeURL(codeRef.module) }
: {}),
};
@@ -70,12 +80,16 @@ function maybeSerializeCodeRef(
stack: CardDef[] = [],
) {
if (codeRef) {
// if a stack is passed in, use the containing card to resolve relative references
let moduleHref =
stack.length > 0
? new URL(codeRef.module, stack[0][relativeTo]).href
: codeRef.module;
return `${moduleHref}/${codeRef.name}`;
if (moduleIsUrlLike(codeRef.module)) {
// if a stack is passed in, use the containing card to resolve relative references
let moduleHref =
stack.length > 0
? new URL(codeRef.module, stack[0][relativeTo]).href
: codeRef.module;
return `${moduleHref}/${codeRef.name}`;
} else {
return `${codeRef.module}/${codeRef.name}`;
}
}
return undefined;
}
48 changes: 47 additions & 1 deletion packages/base/skill-card.gts
Original file line number Diff line number Diff line change
@@ -1,11 +1,57 @@
import {
CardDef,
Component,
FieldDef,
field,
contains,
containsMany,
} from './card-api';
import BooleanField from './boolean';
import CodeRefField from './code-ref';
import MarkdownField from './markdown';
import { CardDef, Component, field, contains } from './card-api';
import StringField from './string';
import RobotIcon from '@cardstack/boxel-icons/robot';
import { simpleHash } from '@cardstack/runtime-common';

function friendlyModuleName(fullModuleUrl: string) {
return fullModuleUrl
.split('/')
.pop()!
.replace(/\.gts$/, '');
}

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 '';
}

const hashed = simpleHash(`${this.codeRef.module}#${this.codeRef.name}`);
let name =
this.codeRef.name === 'default'
? friendlyModuleName(this.codeRef.module)
: this.codeRef.name;
return `${name}_${hashed.slice(0, 4)}`;
},
});
}

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 />
Original file line number Diff line number Diff line change
@@ -734,6 +734,27 @@ module(`Integration | realm indexing and querying`, function (hooks) {
},
},
},
'people-skill.json': {
data: {
attributes: {
instructions: 'How to win friends and influence people',
commands: [
{
codeRef: {
module: `@cardstack/boxel-host/commands/switch-submode`,
name: 'default',
},
},
],
},
meta: {
adoptsFrom: {
module: 'https://cardstack.com/base/skill-card',
name: 'SkillCard',
},
},
},
},
},
});
let indexer = realm.realmIndexQueryEngine;
@@ -807,6 +828,66 @@ module(`Integration | realm indexing and querying`, function (hooks) {
`search entry was an error: ${entry?.error.errorDetail.message}`,
);
}
entry = await indexer.cardDocument(new URL(`${testRealmURL}people-skill`));
if (entry?.type === 'doc') {
assert.deepEqual(entry.doc.data, {
id: `${testRealmURL}people-skill`,
type: 'card',
links: {
self: `${testRealmURL}people-skill`,
},
attributes: {
commands: [
{
codeRef: {
module: '@cardstack/boxel-host/commands/switch-submode',
name: 'default',
},
functionName: 'switch-submode_dd88',
requiresApproval: null,
},
],
description: null,
instructions: 'How to win friends and influence people',
thumbnailURL: null,
title: null,
},
meta: {
adoptsFrom: {
module: 'https://cardstack.com/base/skill-card',
name: 'SkillCard',
},
lastModified: adapter.lastModifiedMap.get(
`${testRealmURL}people-skill.json`,
),
resourceCreatedAt: adapter.resourceCreatedAtMap.get(
`${testRealmURL}people-skill.json`,
),
realmInfo: testRealmInfo,
realmURL: testRealmURL,
},
});
let instance = await indexer.instance(
new URL(`${testRealmURL}people-skill`),
);
assert.deepEqual(instance?.searchDoc, {
_cardType: 'Skill',
id: `${testRealmURL}people-skill`,
instructions: 'How to win friends and influence people',
commands: [
{
codeRef: `@cardstack/boxel-host/commands/switch-submode/default`,
functionName: 'switch-submode_dd88',
requiresApproval: false,
},
],
});
} else {
assert.ok(
false,
`search entry was an error: ${entry?.error.errorDetail.message}`,
);
}
});

test('can recover from rendering a card that has a template error', async function (assert) {
Loading