Coder Social home page Coder Social logo

intitni / copilotforxcodekit Goto Github PK

View Code? Open in Web Editor NEW
11.0 4.0 2.0 75 KB

Build extensions for Copilot for Xcode.

Home Page: https://swiftpackageindex.com/intitni/CopilotForXcodeKit/main/documentation/copilotforxcodekit

License: MIT License

Swift 100.00%

copilotforxcodekit's Introduction

CopilotForXcodeKit

CopilotforXcodeKit is a Swift package that allow you to build extensions for Copilot for Xcode.

Roadmap

  • In-app Configuration Screen
  • Suggestion Service
  • Custom Chat Tab
  • Chat Service
  • Prompt to Code Service
  • APIs for other Xcode Source Editor Extensions
  • Custom Models

Building an Extension

1. Create an Extension Target

Create a Generic Extension target. The extension should target macOS 13+. It can be sandboxed.

In the info.plist of the target, set ExtensionPointIdentifier to com.intii.CopilotForXcode.ExtensionService.Extension:

<key>EXAppExtensionAttributes</key>
<dict>
    <key>EXExtensionPointIdentifier</key>
    <string>com.intii.CopilotForXcode.ExtensionService.Extension</string>
</dict>

2. Add CopilotForXcodeKit as a Dependency

Add this repo as an dependency in Package.swift or via UI in Xcode.

3. Implement the Extension

Create a class that conforms to CopilotForXcodeExtension, and mark it with @main.

import CopilotForXcodeKit

@main
class Extension: CopilotForXcodeExtension {
    ...
}

3.1 Communicate via Global Connection

When the extension is turned on in Copilot for Xcode, the connection will be established. connectionDidActivate(connectedTo:) will be called and host will be set.

Once host is set, you can use it to communicate with Copilot for Xcode.

class Extension: CopilotForXcodeExtension {
    var host: HostServer?
    
    func connectionDidActivate(connectedTo host: HostServer) {
        Task {
            try await host.toast("Connected to Example Extension")
        }
    }
    
    ...
}

Copilot for Xcode will occasionally communicate with your extension to provide updates about Xcode. You can implement observer methods such as workspace(_:didOpenFileAt:) to receive this information.

3.2 Provide Configuration Screen

To provide a configuration screen that users can access from the extension list, you must

  • Create a type that conforms to CopilotForXcodeExtensionSceneConfiguration. Then, return a scene through the configurationScene property.
  • Return the configuration from the sceneConfiguration property of the extension.
struct SceneConfiguration: CopilotForXcodeExtensionSceneConfiguration {
    var configurationScene: ConfigurationScene<ConfigurationView>? {
        .init { viewModel in
            ConfigurationView(model: viewModel)
        } onConnection: { _ in
            return true
        }
    }
}

The ConfigurationView is instantiated when the configuration screen is opened, and the connection is established at this point.

You can then utilize the host property of the view model to communicate with Copilot for Xcode.

struct ConfigurationView: View {
    @ObservedObject var model: CopilotForXcodeSceneModel
    var body: some View {
        Button("Toast") {
            Task { @MainActor in
                try? await model.host?.toast("Hello")
            }
        }
    }
}

3.3 Provide Suggestion Service

To enable the suggestion service within your extension, you must:

  • Implement a type that conforms to the SuggestionServiceType protocol.
  • Provide an instance of this type by returning it from the suggestionService property.
class SuggestionService: SuggestionServiceType {
    var configuration: SuggestionServiceConfiguration {
        .init(acceptsRelevantCodeSnippets: true)
    }

    func getSuggestions(
        _ request: SuggestionRequest,
        workspace: WorkspaceInfo
    ) async throws -> [CodeSuggestion] {
        [
            .init(
                id: UUID().uuidString,
                text: "Hello World",
                position: request.cursorPosition,
                range: .init(start: request.cursorPosition, end: request.cursorPosition)
            ),
        ]
    }
    
    ...
}

If you need to maintain multiple suggestion services for different workspaces, such as those utilizing language servers, you will be responsible for managing the lifecycle of each service individually.

4. Debugging the Extension

To debug the extension, you have two options: running it directly in Xcode or simply building it. There is no practical difference between the two methods, as Xcode does not automatically attach to the extension's process (may be a bug).

Either way, once the extension is built, it will be available in Copilot for Xcode. Then you need to enable the extension:

  • Click "Extensions" in Copilot for Xcode.app.
  • Click "Select Extensions" to see all available extensions.
  • Enable the extension you want to debug.

After that, the extension process will start to run. You can attach to it from Xcode's Debug menu.

It is recommended to give the debug build a different bundle identifier to prevent conflicts with the release version.

When running the extension from Xcode, you will be prompted to choose a target application. Please select "Copilot for Xcode.app".

copilotforxcodekit's People

Contributors

intitni avatar

Stargazers

Wes Kroesbergen avatar Bob James avatar Kirill Kunst avatar  avatar MelodyK avatar Cliff Helsel avatar Yuriy Solodkyy avatar  avatar PPGame Group avatar Ronald Fornis Paglinawan avatar  avatar

Watchers

 avatar Cliff Helsel avatar  avatar  avatar

Forkers

xcltyt devm33

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.