Skip to content

Commit 19f53cb

Browse files
committed
Successfully embedded Python langchain
1 parent 57856af commit 19f53cb

File tree

9 files changed

+132
-12
lines changed

9 files changed

+132
-12
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,11 @@ iOSInjectionProject/
123123

124124
# End of
125125
https://www.toptal.com/developers/gitignore/api/xcode,macos,swift,swiftpackagemanager
126+
126127
Secrets.xcconfig
128+
Python.xcframework
129+
python-stdlib
130+
site-packages/*
131+
!site-packages/requirements.txt
132+
!site-packages/install.sh
133+

Copilot for Xcode.xcodeproj/project.pbxproj

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838
C87B03AC293B2CF300C77EAE /* XcodeKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C81458902939EFDC00135263 /* XcodeKit.framework */; };
3939
C87B03AD293B2CF300C77EAE /* XcodeKit.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = C81458902939EFDC00135263 /* XcodeKit.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
4040
C882175C294187EF00A22FD3 /* Client in Frameworks */ = {isa = PBXBuildFile; productRef = C882175B294187EF00A22FD3 /* Client */; };
41+
C8A3AE522A2884DA0046E809 /* Python.xcframework in Frameworks */ = {isa = PBXBuildFile; fileRef = C8A3AE512A2883430046E809 /* Python.xcframework */; };
42+
C8A3AE592A2885A70046E809 /* InitializePython.swift in Sources */ = {isa = PBXBuildFile; fileRef = C8A3AE582A2885A70046E809 /* InitializePython.swift */; };
43+
C8A3AE5B2A288AF90046E809 /* site-packages in Resources */ = {isa = PBXBuildFile; fileRef = C8A3AE5A2A288AF90046E809 /* site-packages */; };
44+
C8A3B1772A288FA90046E809 /* python-stdlib in Resources */ = {isa = PBXBuildFile; fileRef = C8A3B1762A288FA90046E809 /* python-stdlib */; };
4145
C8C8B60929AFA35F00034BEE /* CopilotForXcodeExtensionService.app in Embed XPCService */ = {isa = PBXBuildFile; fileRef = C861E60E2994F6070056CB02 /* CopilotForXcodeExtensionService.app */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };
4246
C8DCF00029CE11D500FDDDD7 /* ChatWithSelection.swift in Sources */ = {isa = PBXBuildFile; fileRef = C8DCEFFF29CE11D500FDDDD7 /* ChatWithSelection.swift */; };
4347
/* End PBXBuildFile section */
@@ -165,6 +169,10 @@
165169
C87B03A8293B262600C77EAE /* NextSuggestionCommand.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NextSuggestionCommand.swift; sourceTree = "<group>"; };
166170
C87B03AA293B262E00C77EAE /* PreviousSuggestionCommand.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreviousSuggestionCommand.swift; sourceTree = "<group>"; };
167171
C887BC832965D96000931567 /* DEVELOPMENT.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = DEVELOPMENT.md; sourceTree = "<group>"; };
172+
C8A3AE512A2883430046E809 /* Python.xcframework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcframework; path = Python.xcframework; sourceTree = "<group>"; };
173+
C8A3AE582A2885A70046E809 /* InitializePython.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InitializePython.swift; sourceTree = "<group>"; };
174+
C8A3AE5A2A288AF90046E809 /* site-packages */ = {isa = PBXFileReference; lastKnownFileType = folder; path = "site-packages"; sourceTree = "<group>"; };
175+
C8A3B1762A288FA90046E809 /* python-stdlib */ = {isa = PBXFileReference; lastKnownFileType = folder; path = "python-stdlib"; sourceTree = "<group>"; };
168176
C8CD828229B88006008D044D /* TestPlan.xctestplan */ = {isa = PBXFileReference; lastKnownFileType = text; path = TestPlan.xctestplan; sourceTree = "<group>"; };
169177
C8DCEFFF29CE11D500FDDDD7 /* ChatWithSelection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatWithSelection.swift; sourceTree = "<group>"; };
170178
/* End PBXFileReference section */
@@ -201,6 +209,7 @@
201209
buildActionMask = 2147483647;
202210
files = (
203211
C861E61E2994F6150056CB02 /* Service in Frameworks */,
212+
C8A3AE522A2884DA0046E809 /* Python.xcframework in Frameworks */,
204213
);
205214
runOnlyForDeploymentPostprocessing = 0;
206215
};
@@ -255,6 +264,9 @@
255264
C81458922939EFDC00135263 /* EditorExtension */,
256265
C8216B71298036EC00AD38C7 /* Helper */,
257266
C861E60F2994F6070056CB02 /* ExtensionService */,
267+
C8A3AE512A2883430046E809 /* Python.xcframework */,
268+
C8A3B1762A288FA90046E809 /* python-stdlib */,
269+
C8A3AE5A2A288AF90046E809 /* site-packages */,
258270
C814588D2939EFDC00135263 /* Frameworks */,
259271
C8189B172938972F00C9DCDA /* Products */,
260272
);
@@ -306,6 +318,7 @@
306318
C81291D92994FE7900196E12 /* Info.plist */,
307319
C861E61F2994F6390056CB02 /* ServiceDelegate.swift */,
308320
C861E6102994F6070056CB02 /* AppDelegate.swift */,
321+
C8A3AE582A2885A70046E809 /* InitializePython.swift */,
309322
C81291D52994FE6900196E12 /* Main.storyboard */,
310323
C861E6142994F6080056CB02 /* Assets.xcassets */,
311324
C861E6192994F6080056CB02 /* ExtensionService.entitlements */,
@@ -390,6 +403,8 @@
390403
C861E60A2994F6070056CB02 /* Sources */,
391404
C861E60B2994F6070056CB02 /* Frameworks */,
392405
C861E60C2994F6070056CB02 /* Resources */,
406+
C8A3AE572A28852D0046E809 /* Sign Python STD */,
407+
C8A3B1782A2894E10046E809 /* Sign Python Site Packages */,
393408
);
394409
buildRules = (
395410
);
@@ -474,11 +489,52 @@
474489
files = (
475490
C861E6152994F6080056CB02 /* Assets.xcassets in Resources */,
476491
C81291D72994FE6900196E12 /* Main.storyboard in Resources */,
492+
C8A3B1772A288FA90046E809 /* python-stdlib in Resources */,
493+
C8A3AE5B2A288AF90046E809 /* site-packages in Resources */,
477494
);
478495
runOnlyForDeploymentPostprocessing = 0;
479496
};
480497
/* End PBXResourcesBuildPhase section */
481498

499+
/* Begin PBXShellScriptBuildPhase section */
500+
C8A3AE572A28852D0046E809 /* Sign Python STD */ = {
501+
isa = PBXShellScriptBuildPhase;
502+
buildActionMask = 2147483647;
503+
files = (
504+
);
505+
inputFileListPaths = (
506+
);
507+
inputPaths = (
508+
);
509+
name = "Sign Python STD";
510+
outputFileListPaths = (
511+
);
512+
outputPaths = (
513+
);
514+
runOnlyForDeploymentPostprocessing = 0;
515+
shellPath = /bin/sh;
516+
shellScript = "set -e\necho \"Signing as $EXPANDED_CODE_SIGN_IDENTITY_NAME ($EXPANDED_CODE_SIGN_IDENTITY)\"\nfind \"$CODESIGNING_FOLDER_PATH/Contents/Resources/python-stdlib/lib-dynload\" -name \"*.so\" -exec /usr/bin/codesign --force --sign \"$EXPANDED_CODE_SIGN_IDENTITY\" -o runtime --timestamp=none --preserve-metadata=identifier,entitlements,flags --generate-entitlement-der {} \\;\n";
517+
};
518+
C8A3B1782A2894E10046E809 /* Sign Python Site Packages */ = {
519+
isa = PBXShellScriptBuildPhase;
520+
buildActionMask = 2147483647;
521+
files = (
522+
);
523+
inputFileListPaths = (
524+
);
525+
inputPaths = (
526+
);
527+
name = "Sign Python Site Packages";
528+
outputFileListPaths = (
529+
);
530+
outputPaths = (
531+
);
532+
runOnlyForDeploymentPostprocessing = 0;
533+
shellPath = /bin/sh;
534+
shellScript = "set -e\necho \"Signing as $EXPANDED_CODE_SIGN_IDENTITY_NAME ($EXPANDED_CODE_SIGN_IDENTITY)\"\nfind \"$CODESIGNING_FOLDER_PATH/Contents/Resources/site-packages\" -type f \\( -name \"*.so\" -o -name \"*.dylib\" \\) -exec /usr/bin/codesign --force --sign \"$EXPANDED_CODE_SIGN_IDENTITY\" -o runtime --timestamp=none --preserve-metadata=identifier,entitlements,flags --generate-entitlement-der {} \\;\n";
535+
};
536+
/* End PBXShellScriptBuildPhase section */
537+
482538
/* Begin PBXSourcesBuildPhase section */
483539
C81458882939EFDC00135263 /* Sources */ = {
484540
isa = PBXSourcesBuildPhase;
@@ -522,6 +578,7 @@
522578
isa = PBXSourcesBuildPhase;
523579
buildActionMask = 2147483647;
524580
files = (
581+
C8A3AE592A2885A70046E809 /* InitializePython.swift in Sources */,
525582
C861E6202994F63A0056CB02 /* ServiceDelegate.swift in Sources */,
526583
C861E6112994F6070056CB02 /* AppDelegate.swift in Sources */,
527584
);

Copilot for Xcode.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Core/Package.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ let package = Package(
5656
.package(url: "https://github.com/sparkle-project/Sparkle", from: "2.0.0"),
5757
.package(url: "https://github.com/alfianlosari/GPTEncoder", from: "1.0.4"),
5858
.package(url: "https://github.com/kishikawakatsumi/KeychainAccess", from: "4.2.2"),
59+
.package(url: "https://github.com/pvieito/PythonKit.git", branch: "master"),
5960
],
6061
targets: [
6162
// MARK: - Main
@@ -92,6 +93,7 @@ let package = Package(
9293
"ServiceUpdateMigration",
9394
"UserDefaultsObserver",
9495
.product(name: "AsyncAlgorithms", package: "swift-async-algorithms"),
96+
.product(name: "PythonKit", package: "PythonKit"),
9597
]
9698
),
9799
.testTarget(

ExtensionService/AppDelegate.swift

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,20 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
3030

3131
func applicationDidFinishLaunching(_: Notification) {
3232
if ProcessInfo.processInfo.environment["IS_UNIT_TEST"] == "YES" { return }
33-
_ = GraphicalUserInterfaceController.shared
34-
_ = RealtimeSuggestionController.shared
35-
_ = XcodeInspector.shared
36-
AXIsProcessTrustedWithOptions([
37-
kAXTrustedCheckOptionPrompt.takeRetainedValue() as NSString: true,
38-
] as CFDictionary)
39-
setupQuitOnUpdate()
40-
setupQuitOnUserTerminated()
33+
// _ = GraphicalUserInterfaceController.shared
34+
// _ = RealtimeSuggestionController.shared
35+
// _ = XcodeInspector.shared
36+
// AXIsProcessTrustedWithOptions([
37+
// kAXTrustedCheckOptionPrompt.takeRetainedValue() as NSString: true,
38+
// ] as CFDictionary)
39+
// setupQuitOnUpdate()
40+
// setupQuitOnUserTerminated()
4141
xpcListener = setupXPCListener()
4242
Logger.service.info("XPC Service started.")
4343
NSApp.setActivationPolicy(.accessory)
4444
buildStatusBarMenu()
4545
DependencyUpdater().update()
46+
initializePython()
4647
Task {
4748
do {
4849
try await ServiceUpdateMigrator().migrate()
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import Foundation
2+
import Python
3+
import PythonKit
4+
5+
@available( *, deprecated, message: "Testing" )
6+
func initializePython() {
7+
guard let sitePackagePath = Bundle.main.path(forResource: "site-packages", ofType: nil),
8+
let stdLibPath = Bundle.main.path(forResource: "python-stdlib", ofType: nil),
9+
let libDynloadPath = Bundle.main.path(
10+
forResource: "python-stdlib/lib-dynload",
11+
ofType: nil
12+
) else { return }
13+
setenv("PYTHONHOME", stdLibPath, 1)
14+
setenv("PYTHONPATH", "\(stdLibPath):\(libDynloadPath):\(sitePackagePath)", 1)
15+
Py_Initialize()
16+
17+
let sys = Python.import("sys")
18+
print("Python Version: \(sys.version_info.major).\(sys.version_info.minor)")
19+
print("Python Encoding: \(sys.getdefaultencoding().upper())")
20+
print("Python Path: \(sys.path)")
21+
22+
let llms = Python.import("langchain.llms")
23+
print(llms.OpenAI)
24+
}
25+

Tool/Package.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,24 @@ let package = Package(
1010
.library(name: "Terminal", targets: ["Terminal"]),
1111
.library(name: "LangChainService", targets: ["LangChainService"]),
1212
],
13-
dependencies: [],
13+
dependencies: [
14+
.package(url: "https://github.com/pvieito/PythonKit.git", branch: "master"),
15+
],
1416
targets: [
1517
// MARK: - Helpers
1618

17-
.target(name: "Terminal"),
19+
.target(
20+
name: "Terminal",
21+
dependencies: []
22+
),
1823

1924
// MARK: - Services
2025

2126
.target(
2227
name: "LangChainService",
23-
dependencies: []
28+
dependencies: [
29+
.product(name: "PythonKit", package: "PythonKit")
30+
]
2431
),
2532

2633
// MARK: - Tests
@@ -31,4 +38,3 @@ let package = Package(
3138
),
3239
]
3340
)
34-

site-packages/install.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
shopt -s extglob
4+
5+
rm -rfv !("requirements.txt"|"install.sh")
6+
python3.11 -m pip install -r requirements.txt -t .
7+
8+
rm pip/__init__.py
9+
cp ../pip_init.py pip/__init__.py
10+
find . -name "__pycache__" -exec rm -rf {} \;
11+
find "*.so" -delete

site-packages/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
openai
2+
langchain

0 commit comments

Comments
 (0)