Skip to content
Open
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions packages/usdk/lib/chat.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@ import {
join,
leave,
} from '../util/connect-utils.mjs';
import { warnIfAgentsUseNewerSDKVersion } from './version.mjs';

//

export const chat = async (args, opts) => {
// console.log('got chat args', args);
const agentSpecs = await parseAgentSpecs(args._[0]);

try {
await warnIfAgentsUseNewerSDKVersion(agentSpecs, opts);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably shouldn't wait here as it will slow down the chat starting.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe cache it for a period and don't do it during chat, but on any usdk command.

} catch (error) {
// do nothing
}

const room = args.room;
const browser = args.browser;
const runtime = args.runtime ?? 'node';
Expand Down
74 changes: 73 additions & 1 deletion packages/usdk/lib/version.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { execSync } from 'child_process';
import pc from 'picocolors';
import packageJson from '../package.json' with { type: 'json' };
import { makeAnonymousClient } from '../packages/upstreet-agent/packages/react-agents/util/supabase-client.mjs';

// Get the current version
export const version = () => packageJson.version;
Expand All @@ -13,4 +14,75 @@ export function getLatestVersion() {
// console.log(pc.red('Error checking version. '), error.message);
console.log(pc.red('Error checking latest usdk version on the registry.'));
}
}
}

export const isLatestSDKVersion = () => {
const ver = version();
const latestVersion = getLatestVersion();
return latestVersion === ver;
};


export async function warnIfAgentsUseNewerSDKVersion(agentSpecs, opts) {
const ver = version();

// get agent versions in parallel
const agentVersionResults = await Promise.all(
agentSpecs.map(agentSpec =>
getAgentVersion(agentSpec, opts.jwt)
.then(version => ({guid: agentSpec.guid, version}))
)
);

// check versions and warn about mismatches
for (const {guid, version} of agentVersionResults) {
if (version && version > ver) {
console.warn(pc.yellow(
`Warning: Agent ${guid} was created with SDK version ${version}, but you're currently using SDK version ${ver}. ` +
`This version mismatch may cause unexpected behavior. Please update your USDK to the latest version using 'npm install usdk -g'.`
));
}
}
}


const getAgentVersionFromGuid = async (guid, jwt) => {
const supabase = makeAnonymousClient(jwt);
const { data, error } = await supabase
.from('assets')
.select('version')
.eq('id', guid);
return data?.[0]?.version;
};

const getAgentVersionFromPath = async (p) => {
const makeEnoent = () => new Error('not an agent directory: ' + p);

const wranglerTomlPath = path.join(p, 'wrangler.toml');
try {
const wranglerTomString = await fs.promises.readFile(wranglerTomlPath, 'utf8');
const wranglerToml = toml.parse(wranglerTomString);
const agentJsonString = wranglerToml.vars.AGENT_JSON;
const agentJson = agentJsonString && JSON.parse(agentJsonString);
const version = agentJson?.version;
if (version) {
return version;
} else {
throw makeEnoent();
}
} catch (err) {
if (err.code === 'ENOENT') {
throw makeEnoent();
} else {
throw err;
}
}
};

export const getAgentVersion = async (args, jwt) => {
if (args.directory) {
return await getAgentVersionFromPath(args.directory, jwt);
} else {
return await getAgentVersionFromGuid(args.guid, jwt);
}
};