| … | … | @@ -190,7 +190,10 @@ function parseArguments() { |
| 190 | 190 | 'ollama-host': { type: 'string', default: 'localhost:11434' }, |
| 191 | 191 | prompt: { type: 'string' }, |
| 192 | 192 | mock: { type: 'boolean', default: false }, |
| 193 | | verbose: { type: 'boolean', default: false } |
| 193 | verbose: { type: 'boolean', default: false }, |
| 194 | 'link-device': { type: 'string' }, // Name of the device to link |
| 195 | 'confirm-device': { type: 'string' }, // Device ID to confirm |
| 196 | 'mobile-device': { type: 'boolean', default: false } // Whether the linked device is mobile |
| 194 | 197 | }; |
| 195 | 198 | |
| 196 | 199 | const { values } = parseArgs({ options }); |
| … | … | @@ -453,25 +456,196 @@ async function main() { |
| 453 | 456 | log(logLevels.INFO, 'Checking identity...'); |
| 454 | 457 | let identity = await backend.api.core.getIdentity(); |
| 455 | 458 | |
| 456 | | // If we don't have an identity, create one |
| 457 | | if (!identity) { |
| 458 | | log(logLevels.INFO, 'No identity found, generating a new one...'); |
| 459 | | await backend.api.core.generateIdentity(); |
| 460 | | identity = await backend.api.core.getIdentity(); |
| 461 | | } |
| 459 | // If we don't have an identity, create one |
| 460 | if (!identity) { |
| 461 | log(logLevels.INFO, 'No identity found, generating a new one...'); |
| 462 | await backend.api.core.generateIdentity(); |
| 463 | identity = await backend.api.core.getIdentity(); |
| 462 | 464 | |
| 463 | | // Set the display name if provided |
| 464 | | if (args.name && identity) { |
| 465 | | log(logLevels.INFO, `Setting display name to: ${args.name}`); |
| 466 | | if (backend.api.core.setDisplayName) { |
| 467 | | await backend.api.core.setDisplayName(args.name); |
| 465 | // If we're using a mock implementation, we need to manually set up the identity |
| 466 | if (useMock) { |
| 467 | log(logLevels.INFO, 'Using mock implementation, manually setting up identity...'); |
| 468 | |
| 469 | // Create a proper identity structure |
| 470 | const identityPath = path.join(storagePath, 'identity.json'); |
| 471 | const mockIdentity = { |
| 472 | anonymous: false, // Set to false for a real identity |
| 473 | memberId: `real-${Date.now()}`, |
| 474 | timestamp: Date.now(), |
| 475 | displayName: args.name || 'KeetBot', |
| 476 | avatar: null, |
| 477 | devices: [{ |
| 478 | deviceId: `device-${Date.now()}`, |
| 479 | isMobile: false, |
| 480 | name: 'Primary Device', |
| 481 | confirmed: true |
| 482 | }], |
| 483 | local: { |
| 484 | deviceId: `device-${Date.now()}` |
| 485 | } |
| 486 | }; |
| 487 | |
| 488 | // Make sure the local deviceId matches the first device in the devices array |
| 489 | mockIdentity.local.deviceId = mockIdentity.devices[0].deviceId; |
| 490 | |
| 491 | // Write the identity to the file |
| 492 | fs.writeFileSync(identityPath, JSON.stringify(mockIdentity)); |
| 493 | |
| 494 | // Update our identity reference |
| 495 | identity = mockIdentity; |
| 496 | log(logLevels.INFO, `Created new identity with ID: ${identity.memberId}`); |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | // Set the display name if provided |
| 501 | if (args.name && identity) { |
| 502 | log(logLevels.INFO, `Setting display name to: ${args.name}`); |
| 503 | if (backend.api.core.setDisplayName) { |
| 504 | await backend.api.core.setDisplayName(args.name); |
| 505 | } else { |
| 506 | log(logLevels.WARNING, 'setDisplayName method not available'); |
| 507 | |
| 508 | // If we're using a mock implementation, manually update the display name |
| 509 | if (useMock) { |
| 510 | const identityPath = path.join(storagePath, 'identity.json'); |
| 511 | identity.displayName = args.name; |
| 512 | fs.writeFileSync(identityPath, JSON.stringify(identity)); |
| 513 | log(logLevels.INFO, `Manually updated display name to: ${args.name}`); |
| 514 | } |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | // Add a function to link a new device to the identity |
| 519 | async function linkDevice(deviceName = 'New Device', isMobile = false) { |
| 520 | try { |
| 521 | // Get the current identity |
| 522 | const identity = await backend.api.core.getIdentity(); |
| 523 | if (!identity) { |
| 524 | log(logLevels.ERROR, 'No identity found, cannot link device'); |
| 525 | return null; |
| 526 | } |
| 527 | |
| 528 | // If we're using the real Keet implementation |
| 529 | if (!useMock) { |
| 530 | // Check if there's a linkDevice method available |
| 531 | if (backend.api.core.linkDevice) { |
| 532 | log(logLevels.INFO, `Linking new device: ${deviceName}`); |
| 533 | const result = await backend.api.core.linkDevice(deviceName, isMobile); |
| 534 | log(logLevels.INFO, `Device linked successfully: ${JSON.stringify(result)}`); |
| 535 | return result; |
| 536 | } else { |
| 537 | log(logLevels.WARNING, 'linkDevice method not available'); |
| 538 | return null; |
| 539 | } |
| 468 | 540 | } else { |
| 469 | | log(logLevels.WARNING, 'setDisplayName method not available'); |
| 541 | // For mock implementation, manually add a device to the identity |
| 542 | const identityPath = path.join(storagePath, 'identity.json'); |
| 543 | |
| 544 | // Create a new device |
| 545 | const newDevice = { |
| 546 | deviceId: `device-${Date.now()}`, |
| 547 | isMobile: isMobile, |
| 548 | name: deviceName, |
| 549 | confirmed: false // New devices start as unconfirmed |
| 550 | }; |
| 551 | |
| 552 | // Add the device to the identity |
| 553 | identity.devices.push(newDevice); |
| 554 | |
| 555 | // Write the updated identity to the file |
| 556 | fs.writeFileSync(identityPath, JSON.stringify(identity)); |
| 557 | |
| 558 | log(logLevels.INFO, `Manually added new device: ${newDevice.deviceId}`); |
| 559 | return newDevice; |
| 470 | 560 | } |
| 561 | } catch (error) { |
| 562 | log(logLevels.ERROR, `Error linking device: ${error.message}`); |
| 563 | return null; |
| 471 | 564 | } |
| 565 | } |
| 566 | |
| 567 | // Add a function to confirm a linked device |
| 568 | async function confirmDevice(deviceId) { |
| 569 | try { |
| 570 | // Get the current identity |
| 571 | const identity = await backend.api.core.getIdentity(); |
| 572 | if (!identity) { |
| 573 | log(logLevels.ERROR, 'No identity found, cannot confirm device'); |
| 574 | return false; |
| 575 | } |
| 576 | |
| 577 | // If we're using the real Keet implementation |
| 578 | if (!useMock) { |
| 579 | // Check if there's a confirmDevice method available |
| 580 | if (backend.api.core.confirmDevice) { |
| 581 | log(logLevels.INFO, `Confirming device: ${deviceId}`); |
| 582 | const result = await backend.api.core.confirmDevice(deviceId); |
| 583 | log(logLevels.INFO, `Device confirmed successfully`); |
| 584 | return result; |
| 585 | } else { |
| 586 | log(logLevels.WARNING, 'confirmDevice method not available'); |
| 587 | return false; |
| 588 | } |
| 589 | } else { |
| 590 | // For mock implementation, manually confirm the device |
| 591 | const identityPath = path.join(storagePath, 'identity.json'); |
| 592 | |
| 593 | // Find the device in the identity |
| 594 | const deviceIndex = identity.devices.findIndex(device => device.deviceId === deviceId); |
| 595 | if (deviceIndex === -1) { |
| 596 | log(logLevels.ERROR, `Device not found: ${deviceId}`); |
| 597 | return false; |
| 598 | } |
| 599 | |
| 600 | // Confirm the device |
| 601 | identity.devices[deviceIndex].confirmed = true; |
| 602 | |
| 603 | // Write the updated identity to the file |
| 604 | fs.writeFileSync(identityPath, JSON.stringify(identity)); |
| 605 | |
| 606 | log(logLevels.INFO, `Manually confirmed device: ${deviceId}`); |
| 607 | return true; |
| 608 | } |
| 609 | } catch (error) { |
| 610 | log(logLevels.ERROR, `Error confirming device: ${error.message}`); |
| 611 | return false; |
| 612 | } |
| 613 | } |
| 472 | 614 | |
| 473 | 615 | log(logLevels.INFO, `Current identity: ${JSON.stringify(identity)}`); |
| 474 | 616 | |
| 617 | // Handle device linking if requested |
| 618 | if (args['link-device']) { |
| 619 | log(logLevels.INFO, `Linking new device: ${args['link-device']}`); |
| 620 | const newDevice = await linkDevice(args['link-device'], args['mobile-device']); |
| 621 | if (newDevice) { |
| 622 | log(logLevels.INFO, `Successfully linked new device: ${JSON.stringify(newDevice)}`); |
| 623 | log(logLevels.INFO, `Device ID: ${newDevice.deviceId}`); |
| 624 | log(logLevels.INFO, `To confirm this device, run with --confirm-device=${newDevice.deviceId}`); |
| 625 | |
| 626 | // Exit after linking the device |
| 627 | return 0; |
| 628 | } else { |
| 629 | log(logLevels.ERROR, 'Failed to link device'); |
| 630 | return 1; |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | // Handle device confirmation if requested |
| 635 | if (args['confirm-device']) { |
| 636 | log(logLevels.INFO, `Confirming device: ${args['confirm-device']}`); |
| 637 | const confirmed = await confirmDevice(args['confirm-device']); |
| 638 | if (confirmed) { |
| 639 | log(logLevels.INFO, `Successfully confirmed device: ${args['confirm-device']}`); |
| 640 | |
| 641 | // Exit after confirming the device |
| 642 | return 0; |
| 643 | } else { |
| 644 | log(logLevels.ERROR, `Failed to confirm device: ${args['confirm-device']}`); |
| 645 | return 1; |
| 646 | } |
| 647 | } |
| 648 | |
| 475 | 649 | // Initialize Ollama client |
| 476 | 650 | log(logLevels.INFO, `Initializing Ollama client with host: ${args['ollama-host']}, model: ${args.model}`); |
| 477 | 651 | const ollamaClient = new OllamaClient(args['ollama-host'], args.model, args.prompt); |