Skip to content
This repository has been archived by the owner on Apr 13, 2024. It is now read-only.

WIP: Launching Puter apps #74

Closed
wants to merge 3 commits into from
Closed
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
6 changes: 2 additions & 4 deletions src/main_puter.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,15 @@ window.main_shell = async () => {
if ( config['puter.auth.token'] ) {
await puterSDK.setAuthToken(config['puter.auth.token']);
}
const source_without_trailing_slash =
(config.source && config.source.replace(/\/$/, ''))
|| 'https://api.puter.com';
await puterSDK.setAPIOrigin(source_without_trailing_slash);
await puterSDK.setAPIOrigin(config['puter.api_origin']);

const ptt = new XDocumentPTT();
await launchPuterShell(new Context({
ptt,
config, puterSDK,
externs: new Context({ puterSDK }),
platform: new Context({
name: 'puter',
filesystem: CreateFilesystemProvider({ puterSDK }),
drivers: CreateDriversProvider({ puterSDK }),
env: CreateEnvProvider({ config }),
Expand Down
8 changes: 4 additions & 4 deletions src/puter-shell/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { BetterReader } from 'dev-pty';
import { MultiWriter } from '../ansi-shell/ioutil/MultiWriter.js';
import { CompositeCommandProvider } from './providers/CompositeCommandProvider.js';
import { ScriptCommandProvider } from './providers/ScriptCommandProvider.js';
import { PuterAppCommandProvider } from './providers/PuterAppCommandProvider.js';

const argparser_registry = {
[SimpleArgParser.name]: SimpleArgParser
Expand Down Expand Up @@ -76,15 +77,14 @@ export const launchPuterShell = async (ctx) => {
const sdkv2 = globalThis.puter;
if ( ctx.platform.name !== 'node' ) {
await sdkv2.setAuthToken(config['puter.auth.token']);
const source_without_trailing_slash =
(config.source && config.source.replace(/\/$/, ''))
|| 'https://api.puter.com';
await sdkv2.setAPIOrigin(source_without_trailing_slash);
await sdkv2.setAPIOrigin(config['puter.api_origin']);
}

// const commandProvider = new BuiltinCommandProvider();
const commandProvider = new CompositeCommandProvider([
new BuiltinCommandProvider(),
// PuterAppCommandProvider is only usable on Puter
...(ctx.platform.name === 'puter' ? [new PuterAppCommandProvider()] : []),
new ScriptCommandProvider(),
]);

Expand Down
69 changes: 69 additions & 0 deletions src/puter-shell/providers/PuterAppCommandProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (C) 2024 Puter Technologies Inc.
*
* This file is part of Phoenix Shell.
*
* Phoenix Shell is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
const BUILTIN_APPS = [
'explorer',
];

function launch(name, path) {
return {
name,
path,
// TODO: Parameters and options?
async execute(ctx) {
console.log(`%cTrying to launch app: ${name}`, 'color:green');
const args = {}; // TODO: Passed-in parameters and options would go here
await puter.ui.launchApp(name, args);
}
};
}

export class PuterAppCommandProvider {

async lookup (id) {
// Built-in apps will not be returned by the fetch query below, so we handle them separately.
if (BUILTIN_APPS.includes(id)) {
return launch(id, 'Built-in Puter app');
}

const request = await fetch(`${puter.APIOrigin}/drivers/call`, {
"headers": {
"Content-Type": "application/json",
"Authorization": `Bearer ${puter.authToken}`,
},
"body": JSON.stringify({ interface: 'puter-apps', method: 'read', args: { id: { name: id } } }),
"method": "POST",
});

const { success, result } = await request.json();

if (!success) return;

const { name, index_url } = result;
return launch(name, index_url);
}

// Only a single Puter app can match a given name
async lookupAll (...a) {
const result = await this.lookup(...a);
if ( result ) {
return [ result ];
}
return undefined;
}
}